• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Copyright (c) 2014 MundoReader S.L.
4  * Author: Heiko Stuebner <heiko@sntech.de>
5  *
6  * based on clk/samsung/clk-cpu.c
7  * Copyright (c) 2014 Samsung Electronics Co., Ltd.
8  * Author: Thomas Abraham <thomas.ab@samsung.com>
9  *
10  * A CPU clock is defined as a clock supplied to a CPU or a group of CPUs.
11  * The CPU clock is typically derived from a hierarchy of clock
12  * blocks which includes mux and divider blocks. There are a number of other
13  * auxiliary clocks supplied to the CPU domain such as the debug blocks and AXI
14  * clock for CPU domain. The rates of these auxiliary clocks are related to the
15  * CPU clock rate and this relation is usually specified in the hardware manual
16  * of the SoC or supplied after the SoC characterization.
17  *
18  * The below implementation of the CPU clock allows the rate changes of the CPU
19  * clock and the corresponding rate changes of the auxillary clocks of the CPU
20  * domain. The platform clock driver provides a clock register configuration
21  * for each configurable rate which is then used to program the clock hardware
22  * registers to acheive a fast co-oridinated rate change for all the CPU domain
23  * clocks.
24  *
25  * On a rate change request for the CPU clock, the rate change is propagated
26  * upto the PLL supplying the clock to the CPU domain clock blocks. While the
27  * CPU domain PLL is reconfigured, the CPU domain clocks are driven using an
28  * alternate clock source. If required, the alternate clock source is divided
29  * down in order to keep the output clock rate within the previous OPP limits.
30  */
31 
32 #include <linux/of.h>
33 #include <linux/slab.h>
34 #include <linux/io.h>
35 #include <linux/clk.h>
36 #include <linux/clk-provider.h>
37 #include "clk.h"
38 
39 /**
40  * struct rockchip_cpuclk: information about clock supplied to a CPU core.
41  * @hw:        handle between ccf and cpu clock.
42  * @alt_parent:    alternate parent clock to use when switching the speed
43  *        of the primary parent clock.
44  * @reg_base:    base register for cpu-clock values.
45  * @clk_nb:    clock notifier registered for changes in clock speed of the
46  *        primary parent clock.
47  * @rate_count:    number of rates in the rate_table
48  * @rate_table:    pll-rates and their associated dividers
49  * @reg_data:    cpu-specific register settings
50  * @lock:    clock lock
51  */
52 struct rockchip_cpuclk {
53     struct clk_hw hw;
54     struct clk_hw *pll_hw;
55 
56     struct clk_mux cpu_mux;
57     const struct clk_ops *cpu_mux_ops;
58 
59     struct clk *alt_parent;
60     void __iomem *reg_base;
61     struct notifier_block clk_nb;
62     unsigned int rate_count;
63     struct rockchip_cpuclk_rate_table *rate_table;
64     const struct rockchip_cpuclk_reg_data *reg_data;
65     spinlock_t *lock;
66 };
67 
68 #define to_rockchip_cpuclk_hw(hw) container_of(hw, struct rockchip_cpuclk, hw)
69 #define to_rockchip_cpuclk_nb(nb) container_of(nb, struct rockchip_cpuclk, clk_nb)
70 
rockchip_get_cpuclk_settings(struct rockchip_cpuclk * cpuclk,unsigned long rate)71 static const struct rockchip_cpuclk_rate_table *rockchip_get_cpuclk_settings(struct rockchip_cpuclk *cpuclk,
72                                                                              unsigned long rate)
73 {
74     const struct rockchip_cpuclk_rate_table *rate_table = cpuclk->rate_table;
75     int i;
76 
77     for (i = 0; i < cpuclk->rate_count; i++) {
78         if (rate == rate_table[i].prate) {
79             return &rate_table[i];
80         }
81     }
82 
83     return NULL;
84 }
85 
rockchip_cpuclk_recalc_rate(struct clk_hw * hw,unsigned long parent_rate)86 static unsigned long rockchip_cpuclk_recalc_rate(struct clk_hw *hw, unsigned long parent_rate)
87 {
88     struct rockchip_cpuclk *cpuclk = to_rockchip_cpuclk_hw(hw);
89     const struct rockchip_cpuclk_reg_data *reg_data = cpuclk->reg_data;
90     u32 clksel0 = readl_relaxed(cpuclk->reg_base + reg_data->core_reg[0]);
91 
92     clksel0 >>= reg_data->div_core_shift[0];
93     clksel0 &= reg_data->div_core_mask[0];
94     return parent_rate / (clksel0 + 1);
95 }
96 
97 static const struct clk_ops rockchip_cpuclk_ops = {
98     .recalc_rate = rockchip_cpuclk_recalc_rate,
99 };
100 
rockchip_cpuclk_set_dividers(struct rockchip_cpuclk * cpuclk,const struct rockchip_cpuclk_rate_table * rate)101 static void rockchip_cpuclk_set_dividers(struct rockchip_cpuclk *cpuclk, const struct rockchip_cpuclk_rate_table *rate)
102 {
103     int i;
104 
105     /* alternate parent is active now. set the dividers */
106     for (i = 0; i < ARRAY_SIZE(rate->divs); i++) {
107         const struct rockchip_cpuclk_clksel *clksel = &rate->divs[i];
108 
109         if (!clksel->reg) {
110             continue;
111         }
112 
113         pr_debug("%s: setting reg 0x%x to 0x%x\n", __func__, clksel->reg, clksel->val);
114         writel(clksel->val, cpuclk->reg_base + clksel->reg);
115     }
116 }
117 
rockchip_cpuclk_pre_rate_change(struct rockchip_cpuclk * cpuclk,struct clk_notifier_data * ndata)118 static int rockchip_cpuclk_pre_rate_change(struct rockchip_cpuclk *cpuclk, struct clk_notifier_data *ndata)
119 {
120     const struct rockchip_cpuclk_reg_data *reg_data = cpuclk->reg_data;
121     const struct rockchip_cpuclk_rate_table *rate;
122     unsigned long alt_prate, alt_div;
123     unsigned long flags;
124     int i = 0;
125 
126     /* check validity of the new rate */
127     rate = rockchip_get_cpuclk_settings(cpuclk, ndata->new_rate);
128     if (!rate) {
129         pr_err("%s: Invalid rate : %lu for cpuclk\n", __func__, ndata->new_rate);
130         return -EINVAL;
131     }
132 
133     rockchip_boost_enable_recovery_sw_low(cpuclk->pll_hw);
134 
135     alt_prate = clk_get_rate(cpuclk->alt_parent);
136 
137     spin_lock_irqsave(cpuclk->lock, flags);
138 
139     /*
140      * If the old parent clock speed is less than the clock speed
141      * of the alternate parent, then it should be ensured that at no point
142      * the armclk speed is more than the old_rate until the dividers are
143      * set.
144      */
145     if (alt_prate > ndata->old_rate) {
146         /* calculate dividers */
147         alt_div = DIV_ROUND_UP(alt_prate, ndata->old_rate) - 1;
148         if (alt_div > reg_data->div_core_mask[0]) {
149             pr_warn("%s: limiting alt-divider %lu to %d\n", __func__, alt_div, reg_data->div_core_mask[0]);
150             alt_div = reg_data->div_core_mask[0];
151         }
152 
153         /*
154          * Change parents and add dividers in a single transaction.
155          *
156          * NOTE: we do this in a single transaction so we're never
157          * dividing the primary parent by the extra dividers that were
158          * needed for the alt.
159          */
160         pr_debug("%s: setting div %lu as alt-rate %lu > old-rate %lu\n", __func__, alt_div, alt_prate, ndata->old_rate);
161 
162         for (i = 0; i < reg_data->num_cores; i++) {
163             writel(HIWORD_UPDATE(alt_div, reg_data->div_core_mask[i], reg_data->div_core_shift[i]),
164                    cpuclk->reg_base + reg_data->core_reg[i]);
165         }
166     }
167 
168     rockchip_boost_add_core_div(cpuclk->pll_hw, alt_prate);
169 
170     /* select alternate parent */
171     writel(HIWORD_UPDATE(reg_data->mux_core_alt, reg_data->mux_core_mask, reg_data->mux_core_shift),
172            cpuclk->reg_base + reg_data->core_reg[0]);
173 
174     spin_unlock_irqrestore(cpuclk->lock, flags);
175     return 0;
176 }
177 
rockchip_cpuclk_post_rate_change(struct rockchip_cpuclk * cpuclk,struct clk_notifier_data * ndata)178 static int rockchip_cpuclk_post_rate_change(struct rockchip_cpuclk *cpuclk, struct clk_notifier_data *ndata)
179 {
180     const struct rockchip_cpuclk_reg_data *reg_data = cpuclk->reg_data;
181     const struct rockchip_cpuclk_rate_table *rate;
182     unsigned long flags;
183     int i = 0;
184 
185     rate = rockchip_get_cpuclk_settings(cpuclk, ndata->new_rate);
186     if (!rate) {
187         pr_err("%s: Invalid rate : %lu for cpuclk\n", __func__, ndata->new_rate);
188         return -EINVAL;
189     }
190 
191     spin_lock_irqsave(cpuclk->lock, flags);
192 
193     if (ndata->old_rate < ndata->new_rate) {
194         rockchip_cpuclk_set_dividers(cpuclk, rate);
195     }
196 
197     /*
198      * post-rate change event, re-mux to primary parent and remove dividers.
199      *
200      * NOTE: we do this in a single transaction so we're never dividing the
201      * primary parent by the extra dividers that were needed for the alt.
202      */
203 
204     writel(HIWORD_UPDATE(reg_data->mux_core_main, reg_data->mux_core_mask, reg_data->mux_core_shift),
205            cpuclk->reg_base + reg_data->core_reg[0]);
206 
207     /* remove dividers */
208     for (i = 0; i < reg_data->num_cores; i++) {
209         writel(HIWORD_UPDATE(0, reg_data->div_core_mask[i], reg_data->div_core_shift[i]),
210                cpuclk->reg_base + reg_data->core_reg[i]);
211     }
212 
213     if (ndata->old_rate > ndata->new_rate) {
214         rockchip_cpuclk_set_dividers(cpuclk, rate);
215     }
216 
217     rockchip_boost_disable_recovery_sw(cpuclk->pll_hw);
218 
219     spin_unlock_irqrestore(cpuclk->lock, flags);
220     return 0;
221 }
222 
223 /*
224  * This clock notifier is called when the frequency of the parent clock
225  * of cpuclk is to be changed. This notifier handles the setting up all
226  * the divider clocks, remux to temporary parent and handling the safe
227  * frequency levels when using temporary parent.
228  */
rockchip_cpuclk_notifier_cb(struct notifier_block * nb,unsigned long event,void * data)229 static int rockchip_cpuclk_notifier_cb(struct notifier_block *nb, unsigned long event, void *data)
230 {
231     struct clk_notifier_data *ndata = data;
232     struct rockchip_cpuclk *cpuclk = to_rockchip_cpuclk_nb(nb);
233     int ret = 0;
234 
235     pr_debug("%s: event %lu, old_rate %lu, new_rate: %lu\n", __func__, event, ndata->old_rate, ndata->new_rate);
236     if (event == PRE_RATE_CHANGE) {
237         ret = rockchip_cpuclk_pre_rate_change(cpuclk, ndata);
238     } else if (event == POST_RATE_CHANGE) {
239         ret = rockchip_cpuclk_post_rate_change(cpuclk, ndata);
240     }
241 
242     return notifier_from_errno(ret);
243 }
244 
rockchip_clk_register_cpuclk(const char * name,u8 num_parents,struct clk * parent,struct clk * alt_parent,const struct rockchip_cpuclk_reg_data * reg_data,const struct rockchip_cpuclk_rate_table * rates,int nrates,void __iomem * reg_base,spinlock_t * lock)245 struct clk *rockchip_clk_register_cpuclk(const char *name, u8 num_parents, struct clk *parent, struct clk *alt_parent,
246                                          const struct rockchip_cpuclk_reg_data *reg_data,
247                                          const struct rockchip_cpuclk_rate_table *rates, int nrates,
248                                          void __iomem *reg_base, spinlock_t *lock)
249 {
250     struct rockchip_cpuclk *cpuclk;
251     struct clk_init_data init;
252     struct clk *clk, *cclk, *pll_clk;
253     const char *parent_name;
254     int ret;
255 
256     if (num_parents < 2) {
257         pr_err("%s: needs at least two parent clocks\n", __func__);
258         return ERR_PTR(-EINVAL);
259     }
260 
261     if (IS_ERR(parent) || IS_ERR(alt_parent)) {
262         pr_err("%s: invalid parent clock(s)\n", __func__);
263         return ERR_PTR(-EINVAL);
264     }
265 
266     cpuclk = kzalloc(sizeof(*cpuclk), GFP_KERNEL);
267     if (!cpuclk) {
268         return ERR_PTR(-ENOMEM);
269     }
270 
271     parent_name = clk_hw_get_name(__clk_get_hw(parent));
272     init.name = name;
273     init.parent_names = &parent_name;
274     init.num_parents = 1;
275     init.ops = &rockchip_cpuclk_ops;
276 
277     /* only allow rate changes when we have a rate table */
278     init.flags = (nrates > 0) ? CLK_SET_RATE_PARENT : 0;
279 
280     /* disallow automatic parent changes by ccf */
281     init.flags |= CLK_SET_RATE_NO_REPARENT;
282 
283     init.flags |= CLK_GET_RATE_NOCACHE;
284 
285     cpuclk->reg_base = reg_base;
286     cpuclk->lock = lock;
287     cpuclk->reg_data = reg_data;
288     cpuclk->clk_nb.notifier_call = rockchip_cpuclk_notifier_cb;
289     cpuclk->hw.init = &init;
290     if (reg_data->pll_name) {
291         pll_clk = clk_get_parent(parent);
292         if (!pll_clk) {
293             pr_err("%s: could not lookup pll clock: (%s)\n", __func__, reg_data->pll_name);
294             ret = -EINVAL;
295             goto free_cpuclk;
296         }
297         cpuclk->pll_hw = __clk_get_hw(pll_clk);
298         rockchip_boost_init(cpuclk->pll_hw);
299     }
300 
301     cpuclk->alt_parent = alt_parent;
302     if (!cpuclk->alt_parent) {
303         pr_err("%s: could not lookup alternate parent: (%d)\n", __func__, reg_data->mux_core_alt);
304         ret = -EINVAL;
305         goto free_cpuclk;
306     }
307 
308     ret = clk_prepare_enable(cpuclk->alt_parent);
309     if (ret) {
310         pr_err("%s: could not enable alternate parent\n", __func__);
311         goto free_cpuclk;
312     }
313 
314     clk = parent;
315     if (!clk) {
316         pr_err("%s: could not lookup parent clock: (%d) %s\n", __func__, reg_data->mux_core_main, parent_name);
317         ret = -EINVAL;
318         goto free_alt_parent;
319     }
320 
321     ret = clk_notifier_register(clk, &cpuclk->clk_nb);
322     if (ret) {
323         pr_err("%s: failed to register clock notifier for %s\n", __func__, name);
324         goto free_alt_parent;
325     }
326 
327     if (nrates > 0) {
328         cpuclk->rate_count = nrates;
329         cpuclk->rate_table = kmemdup(rates, sizeof(*rates) * nrates, GFP_KERNEL);
330         if (!cpuclk->rate_table) {
331             ret = -ENOMEM;
332             goto unregister_notifier;
333         }
334     }
335 
336     cclk = clk_register(NULL, &cpuclk->hw);
337     if (IS_ERR(cclk)) {
338         pr_err("%s: could not register cpuclk %s\n", __func__, name);
339         ret = PTR_ERR(cclk);
340         goto free_rate_table;
341     }
342 
343     return cclk;
344 
345 free_rate_table:
346     kfree(cpuclk->rate_table);
347 unregister_notifier:
348     clk_notifier_unregister(clk, &cpuclk->clk_nb);
349 free_alt_parent:
350     clk_disable_unprepare(cpuclk->alt_parent);
351 free_cpuclk:
352     kfree(cpuclk);
353     return ERR_PTR(ret);
354 }
355