/*
* R-Car MSTP clocks
*
* Copyright (C) 2013 Ideas On Board SPRL
*
* Contact: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; version 2 of the License.
*/
#include <linux/clk-provider.h>
#include <linux/clkdev.h>
#include <linux/io.h>
#include <linux/of.h>
#include <linux/of_address.h>
#include <linux/spinlock.h>
/*
* MSTP clocks. We can't use standard gate clocks as we need to poll on the
* status register when enabling the clock.
*/
#define MSTP_MAX_CLOCKS 32
/**
* struct mstp_clock_group - MSTP gating clocks group
*
* @data: clocks in this group
* @smstpcr: module stop control register
* @mstpsr: module stop status register (optional)
* @lock: protects writes to SMSTPCR
*/
struct mstp_clock_group {
struct clk_onecell_data data;
void __iomem *smstpcr;
void __iomem *mstpsr;
spinlock_t lock;
};
/**
* struct mstp_clock - MSTP gating clock
* @hw: handle between common and hardware-specific interfaces
* @bit_index: control bit index
* @group: MSTP clocks group
*/
struct mstp_clock {
struct clk_hw hw;
u32 bit_index;
struct mstp_clock_group *group;
};
#define to_mstp_clock(_hw) container_of(_hw, struct mstp_clock, hw)
static int cpg_mstp_clock_endisable(struct clk_hw *hw, bool enable)
{
struct mstp_clock *clock = to_mstp_clock(hw);
struct mstp_clock_group *group = clock->group;
u32 bitmask = BIT(clock->bit_index);
unsigned long flags;
unsigned int i;
u32 value;
spin_lock_irqsave(&group->lock, flags);
value = clk_readl(group->smstpcr);
if (enable)
value &= ~bitmask;
else
value |= bitmask;
clk_writel(value, group->smstpcr);
spin_unlock_irqrestore(&group->lock, flags);
if (!enable || !group->mstpsr)
return 0;
for (i = 1000; i > 0; --i) {
if (!(clk_readl(group->mstpsr) & bitmask))
break;
cpu_relax();
}
if (!i) {
pr_err("%s: failed to enable %p[%d]\n", __func__,
group->smstpcr, clock->bit_index);
return -ETIMEDOUT;
}
return 0;
}
static int cpg_mstp_clock_enable(struct clk_hw *hw)
{
return cpg_mstp_clock_endisable(hw, true);
}
static void cpg_mstp_clock_disable(struct clk_hw *hw)
{
cpg_mstp_clock_endisable(hw, false);
}
static int cpg_mstp_clock_is_enabled(struct clk_hw *hw)
{
struct mstp_clock *clock = to_mstp_clock(hw);
struct mstp_clock_group *group = clock->group;
u32 value;
if (group->mstpsr)
value = clk_readl(group->mstpsr);
else
value = clk_readl(group->smstpcr);
return !(value & BIT(clock->bit_index));
}
static const struct clk_ops cpg_mstp_clock_ops = {
.enable = cpg_mstp_clock_enable,
.disable = cpg_mstp_clock_disable,
.is_enabled = cpg_mstp_clock_is_enabled,
};
static struct clk * __init
cpg_mstp_clock_register(const char *name, const char *parent_name,
unsigned int index, struct mstp_clock_group *group)
{
struct clk_init_data init;
struct mstp_clock *clock;
struct clk *clk;
clock = kzalloc(sizeof(*clock), GFP_KERNEL);
if (!clock) {
pr_err("%s: failed to allocate MSTP clock.\n", __func__);
return ERR_PTR(-ENOMEM);
}
init.name = name;
init.ops = &cpg_mstp_clock_ops;
init.flags = CLK_IS_BASIC | CLK_SET_RATE_PARENT;
init.parent_names = &parent_name;
init.num_parents = 1;
clock->bit_index = index;
clock->group = group;
clock->hw.init = &init;
clk = clk_register(NULL, &clock->hw);
if (IS_ERR(clk))
kfree(clock);
return clk;
}
static void __init cpg_mstp_clocks_init(struct device_node *np)
{
struct mstp_clock_group *group;
const char *idxname;
struct clk **clks;
unsigned int i;
group = kzalloc(sizeof(*group), GFP_KERNEL);
clks = kmalloc(MSTP_MAX_CLOCKS * sizeof(*clks), GFP_KERNEL);
if (group == NULL || clks == NULL) {
kfree(group);
kfree(clks);
pr_err("%s: failed to allocate group\n", __func__);
return;
}
spin_lock_init(&group->lock);
group->data.clks = clks;
group->smstpcr = of_iomap(np, 0);
group->mstpsr = of_iomap(np, 1);
if (group->smstpcr == NULL) {
pr_err("%s: failed to remap SMSTPCR\n", __func__);
kfree(group);
kfree(clks);
return;
}
for (i = 0; i < MSTP_MAX_CLOCKS; ++i)
clks[i] = ERR_PTR(-ENOENT);
if (of_find_property(np, "clock-indices", &i))
idxname = "clock-indices";
else
idxname = "renesas,clock-indices";
for (i = 0; i < MSTP_MAX_CLOCKS; ++i) {
const char *parent_name;
const char *name;
u32 clkidx;
int ret;
/* Skip clocks with no name. */
ret = of_property_read_string_index(np, "clock-output-names",
i, &name);
if (ret < 0 || strlen(name) == 0)
continue;
parent_name = of_clk_get_parent_name(np, i);
ret = of_property_read_u32_index(np, idxname, i, &clkidx);
if (parent_name == NULL || ret < 0)
break;
if (clkidx >= MSTP_MAX_CLOCKS) {
pr_err("%s: invalid clock %s %s index %u)\n",
__func__, np->name, name, clkidx);
continue;
}
clks[clkidx] = cpg_mstp_clock_register(name, parent_name,
clkidx, group);
if (!IS_ERR(clks[clkidx])) {
group->data.clk_num = max(group->data.clk_num,
clkidx + 1);
/*
* Register a clkdev to let board code retrieve the
* clock by name and register aliases for non-DT
* devices.
*
* FIXME: Remove this when all devices that require a
* clock will be instantiated from DT.
*/
clk_register_clkdev(clks[clkidx], name, NULL);
} else {
pr_err("%s: failed to register %s %s clock (%ld)\n",
__func__, np->name, name, PTR_ERR(clks[clkidx]));
}
}
of_clk_add_provider(np, of_clk_src_onecell_get, &group->data);
}
CLK_OF_DECLARE(cpg_mstp_clks, "renesas,cpg-mstp-clocks", cpg_mstp_clocks_init);
邓凌佳
- 粉丝: 80
- 资源: 1万+
最新资源
- 西门子S7-200PLC程序和MCGS3层电梯三层电梯带组态仿真组态设计PLC设计
- 二维谷光子晶体拓扑激光器
- Matlab Simulink变压器饱和模型 励磁涌流模型 变压器具备饱和特性和磁滞特性,可以仿真剩磁,饱和磁通等特性 励磁涌流结果见图2 磁滞特性见图3 剩磁见图4 图6为自己封装的变压器参数转模块
- VCU控制软件simulink模型 控制模块包括:挡位管理、上下电、能量管理、扭矩管理等功能 可用于学习开发控制策略学习 模型附有详细的说明文档 模型有三处变量未赋值的报错但整体逻辑没有错误,可以自己
- 四旋翼飞行器编队避险飞行matlab仿真 附带参考文件
- Simulink直升机非线性动力学模型,直升机动力学仿真,MATLAB Simulink版本,黑鹰单旋翼直升机气动模型,包含源码 有两篇说明文献和使用说明
- STATCOM 级联H桥 5电平 simulink仿真
- FPGA SDI 视频解码PCIE传输 FPGA实现SDI视频解码PCIE传输 提供工程源码和QT上位机源码加 开发板FPGA型号:Xilinx–xc7k325tffg900-2; 开发环境:Viva
- 构网型逆变器控制,同步发电机VSG仿真simulink matlab模型一次调频 一次调频、阻抗、无功电压补偿等
- 增强型地热开采EGS 地热开采 基于COMSOL热流固耦合的地热开采、回灌模型,考虑天然裂缝引起的窜流,可分析不同注采条件下的压力及温度分布
- comsol 电流 电场 磁场 固体传热 流体传热 热流固耦合 地热抽取 裂隙流 传热传质 多孔介质 两项流
- PMSM电机的转速控制Simulink模型 PMSM电机的全状态参数观测 主要包括内容: 1)基于PID的PMSM电机转速控制模型; 2)基于滑模控制器(SMC)的PMSM电机转速控制模型; 3)PM
- 电池控制模型 充放电策略 BMS SOC 傅里叶分析 附赠详细注释和运行说明 BMS管理模型 包含SOC控制策略 电池充放电控制策略 电池参数状态电流峰值电压及SOC监控 含运行界面可以对仿真参数进行
- 两级式单相光伏并网仿真(注意版本matlab 2021a) 前级采用DC-DC变电路,通过MPPT控制DC-DC电路的pwm波来实现最大功率跟踪,mppt采用扰动观察法,后级采用桥式逆变,用spwm波
- 三相桥式(两电平)闭环并网仿真 拓扑:两电平逆变器 DC:800V AC:380V 控制:电流内环PI与前馈解耦 滤波器:LCL滤波器 调制:SPWM 功率等级:100kW THD<1% 结果: 电压
- 风储联合系统 直驱风机加储能系统
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈