1 // SPDX-License-Identifier: GPL-2.0
7 * Gated clock implementation
10 #define LOG_CATEGORY UCLASS_CLK
14 #include <clk-uclass.h>
17 #include <dm/device.h>
18 #include <dm/device_compat.h>
19 #include <dm/devres.h>
20 #include <linux/bitops.h>
21 #include <linux/clk-provider.h>
22 #include <linux/err.h>
23 #include <linux/printk.h>
27 #define UBOOT_DM_CLK_GATE "clk_gate"
30 * DOC: basic gatable clock which can gate and ungate it's output
32 * Traits of this clock:
33 * prepare - clk_(un)prepare only ensures parent is (un)prepared
34 * enable - clk_enable and clk_disable are functional & control gating
35 * rate - inherits rate from parent. No clk_set_rate support
36 * parent - fixed parent. No clk_set_parent support
40 * It works on following logic:
42 * For enabling clock, enable = 1
43 * set2dis = 1 -> clear bit -> set = 0
44 * set2dis = 0 -> set bit -> set = 1
46 * For disabling clock, enable = 0
47 * set2dis = 1 -> set bit -> set = 1
48 * set2dis = 0 -> clear bit -> set = 0
50 * So, result is always: enable xor set2dis.
52 static void clk_gate_endisable(struct clk *clk, int enable)
54 struct clk_gate *gate = to_clk_gate(clk);
55 int set = gate->flags & CLK_GATE_SET_TO_DISABLE ? 1 : 0;
60 if (gate->flags & CLK_GATE_HIWORD_MASK) {
61 reg = BIT(gate->bit_idx + 16);
63 reg |= BIT(gate->bit_idx);
65 #if IS_ENABLED(CONFIG_SANDBOX_CLK_CCF)
66 reg = gate->io_gate_val;
68 reg = readl(gate->reg);
72 reg |= BIT(gate->bit_idx);
74 reg &= ~BIT(gate->bit_idx);
77 writel(reg, gate->reg);
80 static int clk_gate_enable(struct clk *clk)
82 clk_gate_endisable(clk, 1);
87 static int clk_gate_disable(struct clk *clk)
89 clk_gate_endisable(clk, 0);
94 int clk_gate_is_enabled(struct clk *clk)
96 struct clk_gate *gate = to_clk_gate(clk);
99 #if IS_ENABLED(CONFIG_SANDBOX_CLK_CCF)
100 reg = gate->io_gate_val;
102 reg = readl(gate->reg);
105 /* if a set bit disables this clk, flip it before masking */
106 if (gate->flags & CLK_GATE_SET_TO_DISABLE)
107 reg ^= BIT(gate->bit_idx);
109 reg &= BIT(gate->bit_idx);
114 const struct clk_ops clk_gate_ops = {
115 .enable = clk_gate_enable,
116 .disable = clk_gate_disable,
117 .get_rate = clk_generic_get_rate,
120 struct clk *clk_register_gate(struct device *dev, const char *name,
121 const char *parent_name, unsigned long flags,
122 void __iomem *reg, u8 bit_idx,
123 u8 clk_gate_flags, spinlock_t *lock)
125 struct clk_gate *gate;
129 if (clk_gate_flags & CLK_GATE_HIWORD_MASK) {
131 dev_err(dev, "gate bit exceeds LOWORD field\n");
132 return ERR_PTR(-EINVAL);
136 /* allocate the gate */
137 gate = kzalloc(sizeof(*gate), GFP_KERNEL);
139 return ERR_PTR(-ENOMEM);
141 /* struct clk_gate assignments */
143 gate->bit_idx = bit_idx;
144 gate->flags = clk_gate_flags;
145 #if IS_ENABLED(CONFIG_SANDBOX_CLK_CCF)
146 gate->io_gate_val = *(u32 *)reg;
152 ret = clk_register(clk, UBOOT_DM_CLK_GATE, name, parent_name);
161 U_BOOT_DRIVER(clk_gate) = {
162 .name = UBOOT_DM_CLK_GATE,
164 .ops = &clk_gate_ops,
165 .flags = DM_FLAG_PRE_RELOC,