1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * Copyright (C) 2016 Maxime Ripard
4 * Maxime Ripard <maxime.ripard@free-electrons.com>
5 */
6 #include "ccu.h"
7 #include "ccu_gate.h"
8 #include "ccu_mp.h"
9 #include <limits.h>
10
ilog2(unsigned int v)11 static inline unsigned int ilog2(unsigned int v)
12 {
13 unsigned int r;
14 unsigned int shift;
15 r = (v > 0xffff) << 4;
16 v >>= r;
17 shift = (v > 0xff) << 3;
18 v >>= shift;
19 r |= shift;
20 shift = (v > 0xf) << 2;
21 v >>= shift;
22 r |= shift;
23 shift = (v > 0x3) << 1;
24 v >>= shift;
25 r |= shift;
26 r |= (v >> 1);
27 return r;
28 }
29
ccu_mp_find_best(unsigned long parent,unsigned long rate,unsigned int max_m,unsigned int max_p,unsigned int * m,unsigned int * p)30 static void ccu_mp_find_best(unsigned long parent, unsigned long rate,
31 unsigned int max_m, unsigned int max_p,
32 unsigned int *m, unsigned int *p)
33 {
34 unsigned long best_rate = 0;
35 unsigned int best_m = 0, best_p = 0;
36 unsigned int _m, _p;
37
38 for (_p = 1; _p <= max_p; _p <<= 1)
39 {
40 for (_m = 1; _m <= max_m; _m++)
41 {
42 unsigned long tmp_rate = parent / _p / _m;
43
44 if (tmp_rate > rate)
45 {
46 continue;
47 }
48
49 if ((rate - tmp_rate) < (rate - best_rate))
50 {
51 best_rate = tmp_rate;
52 best_m = _m;
53 best_p = _p;
54 }
55 }
56 }
57
58 *m = best_m;
59 *p = best_p;
60 }
61
ccu_mp_find_best_with_parent_adj(struct clk_hw * hw,unsigned long * parent,unsigned long rate,unsigned int max_m,unsigned int max_p)62 static unsigned long ccu_mp_find_best_with_parent_adj(struct clk_hw *hw,
63 unsigned long *parent,
64 unsigned long rate,
65 unsigned int max_m,
66 unsigned int max_p)
67 {
68 unsigned long parent_rate_saved;
69 unsigned long parent_rate, now;
70 unsigned long best_rate = 0;
71 unsigned int _m, _p, div;
72 unsigned long maxdiv;
73
74 parent_rate_saved = *parent;
75
76 /*
77 * The maximum divider we can use without overflowing
78 * unsigned long in rate * m * p below
79 */
80 maxdiv = max_m * max_p;
81 maxdiv = min(ULONG_MAX / rate, maxdiv);
82
83 for (_p = 1; _p <= max_p; _p <<= 1)
84 {
85 for (_m = 1; _m <= max_m; _m++)
86 {
87 div = _m * _p;
88
89 if (div > maxdiv)
90 {
91 break;
92 }
93
94 if (rate * div == parent_rate_saved)
95 {
96 /*
97 * It's the most ideal case if the requested
98 * rate can be divided from parent clock without
99 * needing to change parent rate, so return the
100 * divider immediately.
101 */
102 *parent = parent_rate_saved;
103 return rate;
104 }
105
106 parent_rate = clk_hw_round_rate(hw, rate * div);
107 now = parent_rate / div;
108
109 if (now <= rate && now > best_rate)
110 {
111 best_rate = now;
112 *parent = parent_rate;
113
114 if (now == rate)
115 {
116 return rate;
117 }
118 }
119 }
120 }
121
122 return best_rate;
123 }
124
ccu_mp_round_rate(struct ccu_mux_internal * mux,struct clk_hw * hw,unsigned long * parent_rate,unsigned long rate,void * data)125 static unsigned long ccu_mp_round_rate(struct ccu_mux_internal *mux,
126 struct clk_hw *hw,
127 unsigned long *parent_rate,
128 unsigned long rate,
129 void *data)
130 {
131 struct ccu_mp *cmp = data;
132 unsigned int max_m, max_p;
133 unsigned int m, p;
134
135 if (cmp->common.features & CCU_FEATURE_FIXED_POSTDIV)
136 {
137 rate *= cmp->fixed_post_div;
138 }
139
140 max_m = cmp->m.max ? : 1 << cmp->m.width;
141 max_p = cmp->p.max ? : 1 << ((1 << cmp->p.width) - 1);
142
143 if (!(clk_hw_get_flags(hw) & CLK_SET_RATE_PARENT))
144 {
145 ccu_mp_find_best(*parent_rate, rate, max_m, max_p, &m, &p);
146 rate = *parent_rate / p / m;
147 }
148 else
149 {
150 rate = ccu_mp_find_best_with_parent_adj(hw, parent_rate, rate,
151 max_m, max_p);
152 }
153
154 if (cmp->common.features & CCU_FEATURE_FIXED_POSTDIV)
155 {
156 rate /= cmp->fixed_post_div;
157 }
158
159 return rate;
160 }
161
ccu_mp_disable(struct clk_hw * hw)162 static void ccu_mp_disable(struct clk_hw *hw)
163 {
164 struct ccu_mp *cmp = hw_to_ccu_mp(hw);
165
166 return ccu_gate_helper_disable(&cmp->common, cmp->enable);
167 }
168
ccu_mp_enable(struct clk_hw * hw)169 static int ccu_mp_enable(struct clk_hw *hw)
170 {
171 struct ccu_mp *cmp = hw_to_ccu_mp(hw);
172
173 return ccu_gate_helper_enable(&cmp->common, cmp->enable);
174 }
175
ccu_mp_is_enabled(struct clk_hw * hw)176 static int ccu_mp_is_enabled(struct clk_hw *hw)
177 {
178 struct ccu_mp *cmp = hw_to_ccu_mp(hw);
179
180 return ccu_gate_helper_is_enabled(&cmp->common, cmp->enable);
181 }
182
ccu_mp_recalc_rate(struct clk_hw * hw,unsigned long parent_rate)183 static unsigned long ccu_mp_recalc_rate(struct clk_hw *hw,
184 unsigned long parent_rate)
185 {
186 struct ccu_mp *cmp = hw_to_ccu_mp(hw);
187 unsigned long rate;
188 unsigned int m, p;
189 u32 reg;
190
191 /* Adjust parent_rate according to pre-dividers */
192 parent_rate = ccu_mux_helper_apply_prediv(&cmp->common, &cmp->mux, -1,
193 parent_rate);
194
195 reg = readl(cmp->common.base + cmp->common.reg);
196
197 m = reg >> cmp->m.shift;
198 m &= (1 << cmp->m.width) - 1;
199 m += cmp->m.offset;
200 if (!m)
201 {
202 m++;
203 }
204
205 p = reg >> cmp->p.shift;
206 p &= (1 << cmp->p.width) - 1;
207
208 rate = (parent_rate >> p) / m;
209 if (cmp->common.features & CCU_FEATURE_FIXED_POSTDIV)
210 {
211 rate /= cmp->fixed_post_div;
212 }
213
214 return rate;
215 }
216
ccu_mp_determine_rate(struct clk_hw * hw,struct clk_rate_request * req)217 static int ccu_mp_determine_rate(struct clk_hw *hw,
218 struct clk_rate_request *req)
219 {
220 struct ccu_mp *cmp = hw_to_ccu_mp(hw);
221
222 return ccu_mux_helper_determine_rate(&cmp->common, &cmp->mux,
223 req, ccu_mp_round_rate, cmp);
224 }
225
ccu_mp_set_rate(struct clk_hw * hw,unsigned long rate,unsigned long parent_rate)226 static int ccu_mp_set_rate(struct clk_hw *hw, unsigned long rate,
227 unsigned long parent_rate)
228 {
229 struct ccu_mp *cmp = hw_to_ccu_mp(hw);
230 unsigned int max_m, max_p;
231 unsigned int m, p;
232 u32 reg;
233 u32 __cspr;
234
235 /* Adjust parent_rate according to pre-dividers */
236 parent_rate = ccu_mux_helper_apply_prediv(&cmp->common, &cmp->mux, -1,
237 parent_rate);
238
239 max_m = cmp->m.max ? : 1 << cmp->m.width;
240 max_p = cmp->p.max ? : 1 << ((1 << cmp->p.width) - 1);
241
242 /* Adjust target rate according to post-dividers */
243 if (cmp->common.features & CCU_FEATURE_FIXED_POSTDIV)
244 {
245 rate = rate * cmp->fixed_post_div;
246 }
247
248 ccu_mp_find_best(parent_rate, rate, max_m, max_p, &m, &p);
249
250 __cspr = hal_spin_lock_irqsave(&cmp->common.lock);
251
252 reg = readl(cmp->common.base + cmp->common.reg);
253 reg &= ~GENMASK(cmp->m.width + cmp->m.shift - 1, cmp->m.shift);
254 reg &= ~GENMASK(cmp->p.width + cmp->p.shift - 1, cmp->p.shift);
255 reg |= (m - cmp->m.offset) << cmp->m.shift;
256 reg |= ilog2(p) << cmp->p.shift;
257
258 writel(reg, cmp->common.base + cmp->common.reg);
259
260 hal_spin_unlock_irqrestore(&cmp->common.lock, __cspr);
261
262 return 0;
263 }
264
ccu_mp_get_parent(struct clk_hw * hw)265 static u8 ccu_mp_get_parent(struct clk_hw *hw)
266 {
267 struct ccu_mp *cmp = hw_to_ccu_mp(hw);
268
269 return ccu_mux_helper_get_parent(&cmp->common, &cmp->mux);
270 }
271
ccu_mp_set_parent(struct clk_hw * hw,u8 index)272 static int ccu_mp_set_parent(struct clk_hw *hw, u8 index)
273 {
274 struct ccu_mp *cmp = hw_to_ccu_mp(hw);
275
276 return ccu_mux_helper_set_parent(&cmp->common, &cmp->mux, index);
277 }
278
279 const struct clk_ops ccu_mp_ops =
280 {
281 .disable = ccu_mp_disable,
282 .enable = ccu_mp_enable,
283 .is_enabled = ccu_mp_is_enabled,
284
285 .get_parent = ccu_mp_get_parent,
286 .set_parent = ccu_mp_set_parent,
287
288 .determine_rate = ccu_mp_determine_rate,
289 .recalc_rate = ccu_mp_recalc_rate,
290 .set_rate = ccu_mp_set_rate,
291 };
292
293 /*
294 * Support for MMC timing mode switching
295 *
296 * The MMC clocks on some SoCs support switching between old and
297 * new timing modes. A platform specific API is provided to query
298 * and set the timing mode on supported SoCs.
299 *
300 * In addition, a special class of ccu_mp_ops is provided, which
301 * takes in to account the timing mode switch. When the new timing
302 * mode is active, the clock output rate is halved. This new class
303 * is a wrapper around the generic ccu_mp_ops. When clock rates
304 * are passed through to ccu_mp_ops callbacks, they are doubled
305 * if the new timing mode bit is set, to account for the post
306 * divider. Conversely, when clock rates are passed back, they
307 * are halved if the mode bit is set.
308 */
309
ccu_mp_mmc_recalc_rate(struct clk_hw * hw,unsigned long parent_rate)310 static unsigned long ccu_mp_mmc_recalc_rate(struct clk_hw *hw,
311 unsigned long parent_rate)
312 {
313 unsigned long rate = ccu_mp_recalc_rate(hw, parent_rate);
314 struct ccu_common *cm = hw_to_ccu_common(hw);
315 u32 val = readl(cm->base + cm->reg);
316
317 if (val & CCU_MMC_NEW_TIMING_MODE)
318 {
319 return rate / 2;
320 }
321 return rate;
322 }
323
ccu_mp_mmc_determine_rate(struct clk_hw * hw,struct clk_rate_request * req)324 static int ccu_mp_mmc_determine_rate(struct clk_hw *hw,
325 struct clk_rate_request *req)
326 {
327 struct ccu_common *cm = hw_to_ccu_common(hw);
328 u32 val = readl(cm->base + cm->reg);
329 int ret;
330
331 /* adjust the requested clock rate */
332 if (val & CCU_MMC_NEW_TIMING_MODE)
333 {
334 req->rate *= 2;
335 req->min_rate *= 2;
336 req->max_rate *= 2;
337 }
338
339 ret = ccu_mp_determine_rate(hw, req);
340
341 /* re-adjust the requested clock rate back */
342 if (val & CCU_MMC_NEW_TIMING_MODE)
343 {
344 req->rate /= 2;
345 req->min_rate /= 2;
346 req->max_rate /= 2;
347 }
348
349 return ret;
350 }
351
ccu_mp_mmc_set_rate(struct clk_hw * hw,unsigned long rate,unsigned long parent_rate)352 static int ccu_mp_mmc_set_rate(struct clk_hw *hw, unsigned long rate,
353 unsigned long parent_rate)
354 {
355 struct ccu_common *cm = hw_to_ccu_common(hw);
356 u32 val = readl(cm->base + cm->reg);
357
358 if (val & CCU_MMC_NEW_TIMING_MODE)
359 {
360 rate *= 2;
361 }
362
363 return ccu_mp_set_rate(hw, rate, parent_rate);
364 }
365
366 const struct clk_ops ccu_mp_mmc_ops =
367 {
368 .disable = ccu_mp_disable,
369 .enable = ccu_mp_enable,
370 .is_enabled = ccu_mp_is_enabled,
371
372 .get_parent = ccu_mp_get_parent,
373 .set_parent = ccu_mp_set_parent,
374
375 .determine_rate = ccu_mp_mmc_determine_rate,
376 .recalc_rate = ccu_mp_mmc_recalc_rate,
377 .set_rate = ccu_mp_mmc_set_rate,
378 };
379