1 // SPDX-License-Identifier: GPL-2.0+
8 #include <linux/clk-provider.h>
9 #include <linux/errno.h>
10 #include <linux/export.h>
12 #include <linux/iopoll.h>
13 #include <linux/slab.h>
17 #define TIMEOUT_US 500U
19 #define CCM_DIV_SHIFT 0
20 #define CCM_DIV_WIDTH 8
21 #define CCM_MUX_SHIFT 8
22 #define CCM_MUX_MASK 3
23 #define CCM_OFF_SHIFT 24
24 #define CCM_BUSY_SHIFT 28
26 #define STAT_OFFSET 0x4
27 #define AUTHEN_OFFSET 0x30
29 #define TZ_NS_MASK BIT(9)
31 #define WHITE_LIST_SHIFT 16
33 static int imx93_clk_composite_wait_ready(struct clk_hw *hw, void __iomem *reg)
38 ret = readl_poll_timeout_atomic(reg + STAT_OFFSET, val, !(val & BIT(CCM_BUSY_SHIFT)),
41 pr_err("Slice[%s] busy timeout\n", clk_hw_get_name(hw));
46 static void imx93_clk_composite_gate_endisable(struct clk_hw *hw, int enable)
48 struct clk_gate *gate = to_clk_gate(hw);
53 spin_lock_irqsave(gate->lock, flags);
55 reg = readl(gate->reg);
58 reg &= ~BIT(gate->bit_idx);
60 reg |= BIT(gate->bit_idx);
62 writel(reg, gate->reg);
64 imx93_clk_composite_wait_ready(hw, gate->reg);
67 spin_unlock_irqrestore(gate->lock, flags);
70 static int imx93_clk_composite_gate_enable(struct clk_hw *hw)
72 imx93_clk_composite_gate_endisable(hw, 1);
77 static void imx93_clk_composite_gate_disable(struct clk_hw *hw)
80 * Skip disable the root clock gate if mcore enabled.
81 * The root clock may be used by the mcore.
86 imx93_clk_composite_gate_endisable(hw, 0);
89 static const struct clk_ops imx93_clk_composite_gate_ops = {
90 .enable = imx93_clk_composite_gate_enable,
91 .disable = imx93_clk_composite_gate_disable,
92 .is_enabled = clk_gate_is_enabled,
96 imx93_clk_composite_divider_recalc_rate(struct clk_hw *hw, unsigned long parent_rate)
98 return clk_divider_ops.recalc_rate(hw, parent_rate);
102 imx93_clk_composite_divider_round_rate(struct clk_hw *hw, unsigned long rate, unsigned long *prate)
104 return clk_divider_ops.round_rate(hw, rate, prate);
108 imx93_clk_composite_divider_determine_rate(struct clk_hw *hw, struct clk_rate_request *req)
110 return clk_divider_ops.determine_rate(hw, req);
113 static int imx93_clk_composite_divider_set_rate(struct clk_hw *hw, unsigned long rate,
114 unsigned long parent_rate)
116 struct clk_divider *divider = to_clk_divider(hw);
118 unsigned long flags = 0;
122 value = divider_get_val(rate, parent_rate, divider->table, divider->width, divider->flags);
127 spin_lock_irqsave(divider->lock, flags);
129 val = readl(divider->reg);
130 val &= ~(clk_div_mask(divider->width) << divider->shift);
131 val |= (u32)value << divider->shift;
132 writel(val, divider->reg);
134 ret = imx93_clk_composite_wait_ready(hw, divider->reg);
137 spin_unlock_irqrestore(divider->lock, flags);
142 static const struct clk_ops imx93_clk_composite_divider_ops = {
143 .recalc_rate = imx93_clk_composite_divider_recalc_rate,
144 .round_rate = imx93_clk_composite_divider_round_rate,
145 .determine_rate = imx93_clk_composite_divider_determine_rate,
146 .set_rate = imx93_clk_composite_divider_set_rate,
149 static u8 imx93_clk_composite_mux_get_parent(struct clk_hw *hw)
151 return clk_mux_ops.get_parent(hw);
154 static int imx93_clk_composite_mux_set_parent(struct clk_hw *hw, u8 index)
156 struct clk_mux *mux = to_clk_mux(hw);
157 u32 val = clk_mux_index_to_val(mux->table, mux->flags, index);
158 unsigned long flags = 0;
163 spin_lock_irqsave(mux->lock, flags);
165 reg = readl(mux->reg);
166 reg &= ~(mux->mask << mux->shift);
167 val = val << mux->shift;
169 writel(reg, mux->reg);
171 ret = imx93_clk_composite_wait_ready(hw, mux->reg);
174 spin_unlock_irqrestore(mux->lock, flags);
180 imx93_clk_composite_mux_determine_rate(struct clk_hw *hw, struct clk_rate_request *req)
182 return clk_mux_ops.determine_rate(hw, req);
185 static const struct clk_ops imx93_clk_composite_mux_ops = {
186 .get_parent = imx93_clk_composite_mux_get_parent,
187 .set_parent = imx93_clk_composite_mux_set_parent,
188 .determine_rate = imx93_clk_composite_mux_determine_rate,
191 struct clk_hw *imx93_clk_composite_flags(const char *name, const char * const *parent_names,
192 int num_parents, void __iomem *reg, u32 domain_id,
195 struct clk_hw *hw = ERR_PTR(-ENOMEM), *mux_hw;
196 struct clk_hw *div_hw, *gate_hw;
197 struct clk_divider *div = NULL;
198 struct clk_gate *gate = NULL;
199 struct clk_mux *mux = NULL;
203 mux = kzalloc(sizeof(*mux), GFP_KERNEL);
209 mux->shift = CCM_MUX_SHIFT;
210 mux->mask = CCM_MUX_MASK;
211 mux->lock = &imx_ccm_lock;
213 div = kzalloc(sizeof(*div), GFP_KERNEL);
219 div->shift = CCM_DIV_SHIFT;
220 div->width = CCM_DIV_WIDTH;
221 div->lock = &imx_ccm_lock;
222 div->flags = CLK_DIVIDER_ROUND_CLOSEST;
224 authen = readl(reg + AUTHEN_OFFSET);
225 if (!(authen & TZ_NS_MASK) || !(authen & BIT(WHITE_LIST_SHIFT + domain_id)))
229 hw = clk_hw_register_composite(NULL, name, parent_names, num_parents,
230 mux_hw, &clk_mux_ro_ops, div_hw,
231 &clk_divider_ro_ops, NULL, NULL, flags);
233 gate = kzalloc(sizeof(*gate), GFP_KERNEL);
239 gate->bit_idx = CCM_OFF_SHIFT;
240 gate->lock = &imx_ccm_lock;
241 gate->flags = CLK_GATE_SET_TO_DISABLE;
243 hw = clk_hw_register_composite(NULL, name, parent_names, num_parents,
244 mux_hw, &imx93_clk_composite_mux_ops, div_hw,
245 &imx93_clk_composite_divider_ops, gate_hw,
246 &imx93_clk_composite_gate_ops,
247 flags | CLK_SET_RATE_NO_REPARENT);
261 EXPORT_SYMBOL_GPL(imx93_clk_composite_flags);