blob: d7825c66493e267bdf5977f4628e7b4fdb1eb9e0 [file] [log] [blame]
Kever Yangba1033d2019-07-11 10:42:16 +02001// SPDX-License-Identifier: GPL-2.0
2/*
3 * (C) Copyright 2017 Rockchip Electronics Co., Ltd
4 */
5
Kever Yangba1033d2019-07-11 10:42:16 +02006#include <bitfield.h>
7#include <clk-uclass.h>
8#include <dm.h>
9#include <errno.h>
Simon Glass0f2af882020-05-10 11:40:05 -060010#include <log.h>
Simon Glass9bc15642020-02-03 07:36:16 -070011#include <malloc.h>
Kever Yangba1033d2019-07-11 10:42:16 +020012#include <syscon.h>
13#include <asm/arch-rockchip/clock.h>
14#include <asm/arch-rockchip/cru_px30.h>
15#include <asm/arch-rockchip/hardware.h>
Simon Glass3ba929a2020-10-30 21:38:53 -060016#include <asm/global_data.h>
Simon Glass95588622020-12-22 19:30:28 -070017#include <dm/device-internal.h>
Kever Yangba1033d2019-07-11 10:42:16 +020018#include <dm/lists.h>
19#include <dt-bindings/clock/px30-cru.h>
Simon Glass4dcacfc2020-05-10 11:40:13 -060020#include <linux/bitops.h>
Simon Glassdbd79542020-05-10 11:40:11 -060021#include <linux/delay.h>
Kever Yangba1033d2019-07-11 10:42:16 +020022
23DECLARE_GLOBAL_DATA_PTR;
24
25enum {
26 VCO_MAX_HZ = 3200U * 1000000,
27 VCO_MIN_HZ = 800 * 1000000,
28 OUTPUT_MAX_HZ = 3200U * 1000000,
29 OUTPUT_MIN_HZ = 24 * 1000000,
30};
31
32#define PX30_VOP_PLL_LIMIT 600000000
33
34#define PX30_PLL_RATE(_rate, _refdiv, _fbdiv, _postdiv1, \
35 _postdiv2, _dsmpd, _frac) \
36{ \
37 .rate = _rate##U, \
38 .fbdiv = _fbdiv, \
39 .postdiv1 = _postdiv1, \
40 .refdiv = _refdiv, \
41 .postdiv2 = _postdiv2, \
42 .dsmpd = _dsmpd, \
43 .frac = _frac, \
44}
45
46#define PX30_CPUCLK_RATE(_rate, _aclk_div, _pclk_div) \
47{ \
48 .rate = _rate##U, \
49 .aclk_div = _aclk_div, \
50 .pclk_div = _pclk_div, \
51}
52
53#define DIV_TO_RATE(input_rate, div) ((input_rate) / ((div) + 1))
54
55#define PX30_CLK_DUMP(_id, _name, _iscru) \
56{ \
57 .id = _id, \
58 .name = _name, \
59 .is_cru = _iscru, \
60}
61
62static struct pll_rate_table px30_pll_rates[] = {
63 /* _mhz, _refdiv, _fbdiv, _postdiv1, _postdiv2, _dsmpd, _frac */
64 PX30_PLL_RATE(1200000000, 1, 50, 1, 1, 1, 0),
65 PX30_PLL_RATE(1188000000, 2, 99, 1, 1, 1, 0),
66 PX30_PLL_RATE(1100000000, 12, 550, 1, 1, 1, 0),
67 PX30_PLL_RATE(1008000000, 1, 84, 2, 1, 1, 0),
68 PX30_PLL_RATE(1000000000, 6, 500, 2, 1, 1, 0),
69 PX30_PLL_RATE(816000000, 1, 68, 2, 1, 1, 0),
70 PX30_PLL_RATE(600000000, 1, 75, 3, 1, 1, 0),
71};
72
73static struct cpu_rate_table px30_cpu_rates[] = {
74 PX30_CPUCLK_RATE(1200000000, 1, 5),
75 PX30_CPUCLK_RATE(1008000000, 1, 5),
76 PX30_CPUCLK_RATE(816000000, 1, 3),
77 PX30_CPUCLK_RATE(600000000, 1, 3),
78 PX30_CPUCLK_RATE(408000000, 1, 1),
79};
80
81static u8 pll_mode_shift[PLL_COUNT] = {
82 APLL_MODE_SHIFT, DPLL_MODE_SHIFT, CPLL_MODE_SHIFT,
83 NPLL_MODE_SHIFT, GPLL_MODE_SHIFT
84};
85
86static u32 pll_mode_mask[PLL_COUNT] = {
87 APLL_MODE_MASK, DPLL_MODE_MASK, CPLL_MODE_MASK,
88 NPLL_MODE_MASK, GPLL_MODE_MASK
89};
90
91static struct pll_rate_table auto_table;
92
93static ulong px30_clk_get_pll_rate(struct px30_clk_priv *priv,
94 enum px30_pll_id pll_id);
95
96static struct pll_rate_table *pll_clk_set_by_auto(u32 drate)
97{
98 struct pll_rate_table *rate = &auto_table;
99 u32 ref_khz = OSC_HZ / KHz, refdiv, fbdiv = 0;
100 u32 postdiv1, postdiv2 = 1;
101 u32 fref_khz;
102 u32 diff_khz, best_diff_khz;
103 const u32 max_refdiv = 63, max_fbdiv = 3200, min_fbdiv = 16;
104 const u32 max_postdiv1 = 7, max_postdiv2 = 7;
105 u32 vco_khz;
106 u32 rate_khz = drate / KHz;
107
108 if (!drate) {
109 printf("%s: the frequency can't be 0 Hz\n", __func__);
110 return NULL;
111 }
112
113 postdiv1 = DIV_ROUND_UP(VCO_MIN_HZ / 1000, rate_khz);
114 if (postdiv1 > max_postdiv1) {
115 postdiv2 = DIV_ROUND_UP(postdiv1, max_postdiv1);
116 postdiv1 = DIV_ROUND_UP(postdiv1, postdiv2);
117 }
118
119 vco_khz = rate_khz * postdiv1 * postdiv2;
120
121 if (vco_khz < (VCO_MIN_HZ / KHz) || vco_khz > (VCO_MAX_HZ / KHz) ||
122 postdiv2 > max_postdiv2) {
123 printf("%s: Cannot find out a supported VCO for Freq (%uHz)\n",
124 __func__, rate_khz);
125 return NULL;
126 }
127
128 rate->postdiv1 = postdiv1;
129 rate->postdiv2 = postdiv2;
130
131 best_diff_khz = vco_khz;
132 for (refdiv = 1; refdiv < max_refdiv && best_diff_khz; refdiv++) {
133 fref_khz = ref_khz / refdiv;
134
135 fbdiv = vco_khz / fref_khz;
136 if (fbdiv >= max_fbdiv || fbdiv <= min_fbdiv)
137 continue;
138
139 diff_khz = vco_khz - fbdiv * fref_khz;
140 if (fbdiv + 1 < max_fbdiv && diff_khz > fref_khz / 2) {
141 fbdiv++;
142 diff_khz = fref_khz - diff_khz;
143 }
144
145 if (diff_khz >= best_diff_khz)
146 continue;
147
148 best_diff_khz = diff_khz;
149 rate->refdiv = refdiv;
150 rate->fbdiv = fbdiv;
151 }
152
153 if (best_diff_khz > 4 * (MHz / KHz)) {
154 printf("%s: Failed to match output frequency %u bestis %u Hz\n",
155 __func__, rate_khz,
156 best_diff_khz * KHz);
157 return NULL;
158 }
159
160 return rate;
161}
162
163static const struct pll_rate_table *get_pll_settings(unsigned long rate)
164{
165 unsigned int rate_count = ARRAY_SIZE(px30_pll_rates);
166 int i;
167
168 for (i = 0; i < rate_count; i++) {
169 if (rate == px30_pll_rates[i].rate)
170 return &px30_pll_rates[i];
171 }
172
173 return pll_clk_set_by_auto(rate);
174}
175
176static const struct cpu_rate_table *get_cpu_settings(unsigned long rate)
177{
178 unsigned int rate_count = ARRAY_SIZE(px30_cpu_rates);
179 int i;
180
181 for (i = 0; i < rate_count; i++) {
182 if (rate == px30_cpu_rates[i].rate)
183 return &px30_cpu_rates[i];
184 }
185
186 return NULL;
187}
188
189/*
190 * How to calculate the PLL(from TRM V0.3 Part 1 Page 63):
191 * Formulas also embedded within the Fractional PLL Verilog model:
192 * If DSMPD = 1 (DSM is disabled, "integer mode")
193 * FOUTVCO = FREF / REFDIV * FBDIV
194 * FOUTPOSTDIV = FOUTVCO / POSTDIV1 / POSTDIV2
195 * Where:
196 * FOUTVCO = Fractional PLL non-divided output frequency
197 * FOUTPOSTDIV = Fractional PLL divided output frequency
198 * (output of second post divider)
199 * FREF = Fractional PLL input reference frequency, (the OSC_HZ 24MHz input)
200 * REFDIV = Fractional PLL input reference clock divider
201 * FBDIV = Integer value programmed into feedback divide
202 *
203 */
204static int rkclk_set_pll(struct px30_pll *pll, unsigned int *mode,
205 enum px30_pll_id pll_id,
206 unsigned long drate)
207{
208 const struct pll_rate_table *rate;
209 uint vco_hz, output_hz;
210
211 rate = get_pll_settings(drate);
212 if (!rate) {
213 printf("%s unsupport rate\n", __func__);
214 return -EINVAL;
215 }
216
217 /* All PLLs have same VCO and output frequency range restrictions. */
218 vco_hz = OSC_HZ / 1000 * rate->fbdiv / rate->refdiv * 1000;
219 output_hz = vco_hz / rate->postdiv1 / rate->postdiv2;
220
221 debug("PLL at %p: fb=%d, ref=%d, pst1=%d, pst2=%d, vco=%u Hz, output=%u Hz\n",
222 pll, rate->fbdiv, rate->refdiv, rate->postdiv1,
223 rate->postdiv2, vco_hz, output_hz);
224 assert(vco_hz >= VCO_MIN_HZ && vco_hz <= VCO_MAX_HZ &&
225 output_hz >= OUTPUT_MIN_HZ && output_hz <= OUTPUT_MAX_HZ);
226
227 /*
228 * When power on or changing PLL setting,
229 * we must force PLL into slow mode to ensure output stable clock.
230 */
231 rk_clrsetreg(mode, pll_mode_mask[pll_id],
232 PLLMUX_FROM_XIN24M << pll_mode_shift[pll_id]);
233
234 /* use integer mode */
235 rk_setreg(&pll->con1, 1 << PLL_DSMPD_SHIFT);
236 /* Power down */
237 rk_setreg(&pll->con1, 1 << PLL_PD_SHIFT);
238
239 rk_clrsetreg(&pll->con0,
240 PLL_POSTDIV1_MASK | PLL_FBDIV_MASK,
241 (rate->postdiv1 << PLL_POSTDIV1_SHIFT) | rate->fbdiv);
242 rk_clrsetreg(&pll->con1, PLL_POSTDIV2_MASK | PLL_REFDIV_MASK,
243 (rate->postdiv2 << PLL_POSTDIV2_SHIFT |
244 rate->refdiv << PLL_REFDIV_SHIFT));
245
246 /* Power Up */
247 rk_clrreg(&pll->con1, 1 << PLL_PD_SHIFT);
248
249 /* waiting for pll lock */
250 while (!(readl(&pll->con1) & (1 << PLL_LOCK_STATUS_SHIFT)))
251 udelay(1);
252
253 rk_clrsetreg(mode, pll_mode_mask[pll_id],
254 PLLMUX_FROM_PLL << pll_mode_shift[pll_id]);
255
256 return 0;
257}
258
259static uint32_t rkclk_pll_get_rate(struct px30_pll *pll, unsigned int *mode,
260 enum px30_pll_id pll_id)
261{
262 u32 refdiv, fbdiv, postdiv1, postdiv2;
263 u32 con, shift, mask;
264
265 con = readl(mode);
266 shift = pll_mode_shift[pll_id];
267 mask = pll_mode_mask[pll_id];
268
269 switch ((con & mask) >> shift) {
270 case PLLMUX_FROM_XIN24M:
271 return OSC_HZ;
272 case PLLMUX_FROM_PLL:
273 /* normal mode */
274 con = readl(&pll->con0);
275 postdiv1 = (con & PLL_POSTDIV1_MASK) >> PLL_POSTDIV1_SHIFT;
276 fbdiv = (con & PLL_FBDIV_MASK) >> PLL_FBDIV_SHIFT;
277 con = readl(&pll->con1);
278 postdiv2 = (con & PLL_POSTDIV2_MASK) >> PLL_POSTDIV2_SHIFT;
279 refdiv = (con & PLL_REFDIV_MASK) >> PLL_REFDIV_SHIFT;
280 return (24 * fbdiv / (refdiv * postdiv1 * postdiv2)) * 1000000;
281 case PLLMUX_FROM_RTC32K:
282 default:
283 return 32768;
284 }
285}
286
287static ulong px30_i2c_get_clk(struct px30_clk_priv *priv, ulong clk_id)
288{
289 struct px30_cru *cru = priv->cru;
290 u32 div, con;
291
292 switch (clk_id) {
293 case SCLK_I2C0:
294 con = readl(&cru->clksel_con[49]);
295 div = con >> CLK_I2C0_DIV_CON_SHIFT & CLK_I2C_DIV_CON_MASK;
296 break;
297 case SCLK_I2C1:
298 con = readl(&cru->clksel_con[49]);
299 div = con >> CLK_I2C1_DIV_CON_SHIFT & CLK_I2C_DIV_CON_MASK;
300 break;
301 case SCLK_I2C2:
302 con = readl(&cru->clksel_con[50]);
303 div = con >> CLK_I2C2_DIV_CON_SHIFT & CLK_I2C_DIV_CON_MASK;
304 break;
305 case SCLK_I2C3:
306 con = readl(&cru->clksel_con[50]);
307 div = con >> CLK_I2C3_DIV_CON_SHIFT & CLK_I2C_DIV_CON_MASK;
308 break;
309 default:
310 printf("do not support this i2c bus\n");
311 return -EINVAL;
312 }
313
314 return DIV_TO_RATE(priv->gpll_hz, div);
315}
316
317static ulong px30_i2c_set_clk(struct px30_clk_priv *priv, ulong clk_id, uint hz)
318{
319 struct px30_cru *cru = priv->cru;
320 int src_clk_div;
321
322 src_clk_div = DIV_ROUND_UP(priv->gpll_hz, hz);
323 assert(src_clk_div - 1 <= 127);
324
325 switch (clk_id) {
326 case SCLK_I2C0:
327 rk_clrsetreg(&cru->clksel_con[49],
328 CLK_I2C_DIV_CON_MASK << CLK_I2C0_DIV_CON_SHIFT |
329 CLK_I2C_PLL_SEL_MASK << CLK_I2C0_PLL_SEL_SHIFT,
330 (src_clk_div - 1) << CLK_I2C0_DIV_CON_SHIFT |
331 CLK_I2C_PLL_SEL_GPLL << CLK_I2C0_PLL_SEL_SHIFT);
332 break;
333 case SCLK_I2C1:
334 rk_clrsetreg(&cru->clksel_con[49],
335 CLK_I2C_DIV_CON_MASK << CLK_I2C1_DIV_CON_SHIFT |
336 CLK_I2C_PLL_SEL_MASK << CLK_I2C1_PLL_SEL_SHIFT,
337 (src_clk_div - 1) << CLK_I2C1_DIV_CON_SHIFT |
338 CLK_I2C_PLL_SEL_GPLL << CLK_I2C1_PLL_SEL_SHIFT);
339 break;
340 case SCLK_I2C2:
341 rk_clrsetreg(&cru->clksel_con[50],
342 CLK_I2C_DIV_CON_MASK << CLK_I2C2_DIV_CON_SHIFT |
343 CLK_I2C_PLL_SEL_MASK << CLK_I2C2_PLL_SEL_SHIFT,
344 (src_clk_div - 1) << CLK_I2C2_DIV_CON_SHIFT |
345 CLK_I2C_PLL_SEL_GPLL << CLK_I2C2_PLL_SEL_SHIFT);
346 break;
347 case SCLK_I2C3:
348 rk_clrsetreg(&cru->clksel_con[50],
349 CLK_I2C_DIV_CON_MASK << CLK_I2C3_DIV_CON_SHIFT |
350 CLK_I2C_PLL_SEL_MASK << CLK_I2C3_PLL_SEL_SHIFT,
351 (src_clk_div - 1) << CLK_I2C3_DIV_CON_SHIFT |
352 CLK_I2C_PLL_SEL_GPLL << CLK_I2C3_PLL_SEL_SHIFT);
353 break;
354 default:
355 printf("do not support this i2c bus\n");
356 return -EINVAL;
357 }
358
359 return px30_i2c_get_clk(priv, clk_id);
360}
361
362/*
363 * calculate best rational approximation for a given fraction
364 * taking into account restricted register size, e.g. to find
365 * appropriate values for a pll with 5 bit denominator and
366 * 8 bit numerator register fields, trying to set up with a
367 * frequency ratio of 3.1415, one would say:
368 *
369 * rational_best_approximation(31415, 10000,
370 * (1 << 8) - 1, (1 << 5) - 1, &n, &d);
371 *
372 * you may look at given_numerator as a fixed point number,
373 * with the fractional part size described in given_denominator.
374 *
375 * for theoretical background, see:
376 * http://en.wikipedia.org/wiki/Continued_fraction
377 */
378static void rational_best_approximation(unsigned long given_numerator,
379 unsigned long given_denominator,
380 unsigned long max_numerator,
381 unsigned long max_denominator,
382 unsigned long *best_numerator,
383 unsigned long *best_denominator)
384{
385 unsigned long n, d, n0, d0, n1, d1;
386
387 n = given_numerator;
388 d = given_denominator;
389 n0 = 0;
390 d1 = 0;
391 n1 = 1;
392 d0 = 1;
393 for (;;) {
394 unsigned long t, a;
395
396 if (n1 > max_numerator || d1 > max_denominator) {
397 n1 = n0;
398 d1 = d0;
399 break;
400 }
401 if (d == 0)
402 break;
403 t = d;
404 a = n / d;
405 d = n % d;
406 n = t;
407 t = n0 + a * n1;
408 n0 = n1;
409 n1 = t;
410 t = d0 + a * d1;
411 d0 = d1;
412 d1 = t;
413 }
414 *best_numerator = n1;
415 *best_denominator = d1;
416}
417
418static ulong px30_i2s_get_clk(struct px30_clk_priv *priv, ulong clk_id)
419{
420 u32 con, fracdiv, gate;
421 u32 clk_src = priv->gpll_hz / 2;
422 unsigned long m, n;
423 struct px30_cru *cru = priv->cru;
424
425 switch (clk_id) {
426 case SCLK_I2S1:
427 con = readl(&cru->clksel_con[30]);
428 fracdiv = readl(&cru->clksel_con[31]);
429 gate = readl(&cru->clkgate_con[10]);
430 m = fracdiv & CLK_I2S1_FRAC_NUMERATOR_MASK;
431 m >>= CLK_I2S1_FRAC_NUMERATOR_SHIFT;
432 n = fracdiv & CLK_I2S1_FRAC_DENOMINATOR_MASK;
433 n >>= CLK_I2S1_FRAC_DENOMINATOR_SHIFT;
434 debug("con30: 0x%x, gate: 0x%x, frac: 0x%x\n",
435 con, gate, fracdiv);
436 break;
437 default:
438 printf("do not support this i2s bus\n");
439 return -EINVAL;
440 }
441
442 return clk_src * n / m;
443}
444
445static ulong px30_i2s_set_clk(struct px30_clk_priv *priv, ulong clk_id, uint hz)
446{
447 u32 clk_src;
448 unsigned long m, n, val;
449 struct px30_cru *cru = priv->cru;
450
451 clk_src = priv->gpll_hz / 2;
452 rational_best_approximation(hz, clk_src,
453 GENMASK(16 - 1, 0),
454 GENMASK(16 - 1, 0),
455 &m, &n);
456 switch (clk_id) {
457 case SCLK_I2S1:
458 rk_clrsetreg(&cru->clksel_con[30],
459 CLK_I2S1_PLL_SEL_MASK, CLK_I2S1_PLL_SEL_GPLL);
460 rk_clrsetreg(&cru->clksel_con[30],
461 CLK_I2S1_DIV_CON_MASK, 0x1);
462 rk_clrsetreg(&cru->clksel_con[30],
463 CLK_I2S1_SEL_MASK, CLK_I2S1_SEL_FRAC);
464 val = m << CLK_I2S1_FRAC_NUMERATOR_SHIFT | n;
465 writel(val, &cru->clksel_con[31]);
466 rk_clrsetreg(&cru->clkgate_con[10],
467 CLK_I2S1_OUT_MCLK_PAD_MASK,
468 CLK_I2S1_OUT_MCLK_PAD_ENABLE);
469 break;
470 default:
471 printf("do not support this i2s bus\n");
472 return -EINVAL;
473 }
474
475 return px30_i2s_get_clk(priv, clk_id);
476}
477
478static ulong px30_nandc_get_clk(struct px30_clk_priv *priv)
479{
480 struct px30_cru *cru = priv->cru;
481 u32 div, con;
482
483 con = readl(&cru->clksel_con[15]);
484 div = (con & NANDC_DIV_MASK) >> NANDC_DIV_SHIFT;
485
486 return DIV_TO_RATE(priv->gpll_hz, div);
487}
488
489static ulong px30_nandc_set_clk(struct px30_clk_priv *priv,
490 ulong set_rate)
491{
492 struct px30_cru *cru = priv->cru;
493 int src_clk_div;
494
495 /* Select nandc source from GPLL by default */
496 /* nandc clock defaulg div 2 internal, need provide double in cru */
497 src_clk_div = DIV_ROUND_UP(priv->gpll_hz, set_rate);
498 assert(src_clk_div - 1 <= 31);
499
500 rk_clrsetreg(&cru->clksel_con[15],
501 NANDC_CLK_SEL_MASK | NANDC_PLL_MASK |
502 NANDC_DIV_MASK,
503 NANDC_CLK_SEL_NANDC << NANDC_CLK_SEL_SHIFT |
504 NANDC_SEL_GPLL << NANDC_PLL_SHIFT |
505 (src_clk_div - 1) << NANDC_DIV_SHIFT);
506
507 return px30_nandc_get_clk(priv);
508}
509
510static ulong px30_mmc_get_clk(struct px30_clk_priv *priv, uint clk_id)
511{
512 struct px30_cru *cru = priv->cru;
513 u32 div, con, con_id;
514
515 switch (clk_id) {
516 case HCLK_SDMMC:
517 case SCLK_SDMMC:
518 con_id = 16;
519 break;
520 case HCLK_EMMC:
521 case SCLK_EMMC:
522 case SCLK_EMMC_SAMPLE:
523 con_id = 20;
524 break;
525 default:
526 return -EINVAL;
527 }
528
529 con = readl(&cru->clksel_con[con_id]);
530 div = (con & EMMC_DIV_MASK) >> EMMC_DIV_SHIFT;
531
532 if ((con & EMMC_PLL_MASK) >> EMMC_PLL_SHIFT
533 == EMMC_SEL_24M)
534 return DIV_TO_RATE(OSC_HZ, div) / 2;
535 else
536 return DIV_TO_RATE(priv->gpll_hz, div) / 2;
537}
538
539static ulong px30_mmc_set_clk(struct px30_clk_priv *priv,
540 ulong clk_id, ulong set_rate)
541{
542 struct px30_cru *cru = priv->cru;
543 int src_clk_div;
544 u32 con_id;
545
546 switch (clk_id) {
547 case HCLK_SDMMC:
548 case SCLK_SDMMC:
549 con_id = 16;
550 break;
551 case HCLK_EMMC:
552 case SCLK_EMMC:
553 con_id = 20;
554 break;
555 default:
556 return -EINVAL;
557 }
558
559 /* Select clk_sdmmc/emmc source from GPLL by default */
560 /* mmc clock defaulg div 2 internal, need provide double in cru */
561 src_clk_div = DIV_ROUND_UP(priv->gpll_hz / 2, set_rate);
562
563 if (src_clk_div > 127) {
564 /* use 24MHz source for 400KHz clock */
565 src_clk_div = DIV_ROUND_UP(OSC_HZ / 2, set_rate);
566 rk_clrsetreg(&cru->clksel_con[con_id],
567 EMMC_PLL_MASK | EMMC_DIV_MASK,
568 EMMC_SEL_24M << EMMC_PLL_SHIFT |
569 (src_clk_div - 1) << EMMC_DIV_SHIFT);
570 } else {
571 rk_clrsetreg(&cru->clksel_con[con_id],
572 EMMC_PLL_MASK | EMMC_DIV_MASK,
573 EMMC_SEL_GPLL << EMMC_PLL_SHIFT |
574 (src_clk_div - 1) << EMMC_DIV_SHIFT);
575 }
576 rk_clrsetreg(&cru->clksel_con[con_id + 1], EMMC_CLK_SEL_MASK,
577 EMMC_CLK_SEL_EMMC);
578
579 return px30_mmc_get_clk(priv, clk_id);
580}
581
Jon Lin8f20e732021-08-05 16:27:53 +0800582static ulong px30_sfc_get_clk(struct px30_clk_priv *priv, uint clk_id)
583{
584 struct px30_cru *cru = priv->cru;
585 u32 div, con;
586
587 con = readl(&cru->clksel_con[22]);
588 div = (con & SFC_DIV_CON_MASK) >> SFC_DIV_CON_SHIFT;
589
590 return DIV_TO_RATE(priv->gpll_hz, div);
591}
592
593static ulong px30_sfc_set_clk(struct px30_clk_priv *priv,
594 ulong clk_id, ulong set_rate)
595{
596 struct px30_cru *cru = priv->cru;
597 int src_clk_div;
598
599 src_clk_div = DIV_ROUND_UP(priv->gpll_hz, set_rate);
600 rk_clrsetreg(&cru->clksel_con[22],
601 SFC_PLL_SEL_MASK | SFC_DIV_CON_MASK,
602 0 << SFC_PLL_SEL_SHIFT |
603 (src_clk_div - 1) << SFC_DIV_CON_SHIFT);
604
605 return px30_sfc_get_clk(priv, clk_id);
606}
607
Kever Yangba1033d2019-07-11 10:42:16 +0200608static ulong px30_pwm_get_clk(struct px30_clk_priv *priv, ulong clk_id)
609{
610 struct px30_cru *cru = priv->cru;
611 u32 div, con;
612
613 switch (clk_id) {
614 case SCLK_PWM0:
615 con = readl(&cru->clksel_con[52]);
616 div = con >> CLK_PWM0_DIV_CON_SHIFT & CLK_PWM_DIV_CON_MASK;
617 break;
618 case SCLK_PWM1:
619 con = readl(&cru->clksel_con[52]);
620 div = con >> CLK_PWM1_DIV_CON_SHIFT & CLK_PWM_DIV_CON_MASK;
621 break;
622 default:
623 printf("do not support this pwm bus\n");
624 return -EINVAL;
625 }
626
627 return DIV_TO_RATE(priv->gpll_hz, div);
628}
629
630static ulong px30_pwm_set_clk(struct px30_clk_priv *priv, ulong clk_id, uint hz)
631{
632 struct px30_cru *cru = priv->cru;
633 int src_clk_div;
634
635 src_clk_div = DIV_ROUND_UP(priv->gpll_hz, hz);
636 assert(src_clk_div - 1 <= 127);
637
638 switch (clk_id) {
639 case SCLK_PWM0:
640 rk_clrsetreg(&cru->clksel_con[52],
641 CLK_PWM_DIV_CON_MASK << CLK_PWM0_DIV_CON_SHIFT |
642 CLK_PWM_PLL_SEL_MASK << CLK_PWM0_PLL_SEL_SHIFT,
643 (src_clk_div - 1) << CLK_PWM0_DIV_CON_SHIFT |
644 CLK_PWM_PLL_SEL_GPLL << CLK_PWM0_PLL_SEL_SHIFT);
645 break;
646 case SCLK_PWM1:
647 rk_clrsetreg(&cru->clksel_con[52],
648 CLK_PWM_DIV_CON_MASK << CLK_PWM1_DIV_CON_SHIFT |
649 CLK_PWM_PLL_SEL_MASK << CLK_PWM1_PLL_SEL_SHIFT,
650 (src_clk_div - 1) << CLK_PWM1_DIV_CON_SHIFT |
651 CLK_PWM_PLL_SEL_GPLL << CLK_PWM1_PLL_SEL_SHIFT);
652 break;
653 default:
654 printf("do not support this pwm bus\n");
655 return -EINVAL;
656 }
657
658 return px30_pwm_get_clk(priv, clk_id);
659}
660
661static ulong px30_saradc_get_clk(struct px30_clk_priv *priv)
662{
663 struct px30_cru *cru = priv->cru;
664 u32 div, con;
665
666 con = readl(&cru->clksel_con[55]);
667 div = con >> CLK_SARADC_DIV_CON_SHIFT & CLK_SARADC_DIV_CON_MASK;
668
669 return DIV_TO_RATE(OSC_HZ, div);
670}
671
672static ulong px30_saradc_set_clk(struct px30_clk_priv *priv, uint hz)
673{
674 struct px30_cru *cru = priv->cru;
675 int src_clk_div;
676
677 src_clk_div = DIV_ROUND_UP(OSC_HZ, hz);
678 assert(src_clk_div - 1 <= 2047);
679
680 rk_clrsetreg(&cru->clksel_con[55],
681 CLK_SARADC_DIV_CON_MASK,
682 (src_clk_div - 1) << CLK_SARADC_DIV_CON_SHIFT);
683
684 return px30_saradc_get_clk(priv);
685}
686
687static ulong px30_tsadc_get_clk(struct px30_clk_priv *priv)
688{
689 struct px30_cru *cru = priv->cru;
690 u32 div, con;
691
692 con = readl(&cru->clksel_con[54]);
693 div = con >> CLK_SARADC_DIV_CON_SHIFT & CLK_SARADC_DIV_CON_MASK;
694
695 return DIV_TO_RATE(OSC_HZ, div);
696}
697
698static ulong px30_tsadc_set_clk(struct px30_clk_priv *priv, uint hz)
699{
700 struct px30_cru *cru = priv->cru;
701 int src_clk_div;
702
703 src_clk_div = DIV_ROUND_UP(OSC_HZ, hz);
704 assert(src_clk_div - 1 <= 2047);
705
706 rk_clrsetreg(&cru->clksel_con[54],
707 CLK_SARADC_DIV_CON_MASK,
708 (src_clk_div - 1) << CLK_SARADC_DIV_CON_SHIFT);
709
710 return px30_tsadc_get_clk(priv);
711}
712
713static ulong px30_spi_get_clk(struct px30_clk_priv *priv, ulong clk_id)
714{
715 struct px30_cru *cru = priv->cru;
716 u32 div, con;
717
718 switch (clk_id) {
719 case SCLK_SPI0:
720 con = readl(&cru->clksel_con[53]);
721 div = con >> CLK_SPI0_DIV_CON_SHIFT & CLK_SPI_DIV_CON_MASK;
722 break;
723 case SCLK_SPI1:
724 con = readl(&cru->clksel_con[53]);
725 div = con >> CLK_SPI1_DIV_CON_SHIFT & CLK_SPI_DIV_CON_MASK;
726 break;
727 default:
728 printf("do not support this pwm bus\n");
729 return -EINVAL;
730 }
731
732 return DIV_TO_RATE(priv->gpll_hz, div);
733}
734
735static ulong px30_spi_set_clk(struct px30_clk_priv *priv, ulong clk_id, uint hz)
736{
737 struct px30_cru *cru = priv->cru;
738 int src_clk_div;
739
740 src_clk_div = DIV_ROUND_UP(priv->gpll_hz, hz);
741 assert(src_clk_div - 1 <= 127);
742
743 switch (clk_id) {
744 case SCLK_SPI0:
745 rk_clrsetreg(&cru->clksel_con[53],
746 CLK_SPI_DIV_CON_MASK << CLK_SPI0_DIV_CON_SHIFT |
747 CLK_SPI_PLL_SEL_MASK << CLK_SPI0_PLL_SEL_SHIFT,
748 (src_clk_div - 1) << CLK_SPI0_DIV_CON_SHIFT |
749 CLK_SPI_PLL_SEL_GPLL << CLK_SPI0_PLL_SEL_SHIFT);
750 break;
751 case SCLK_SPI1:
752 rk_clrsetreg(&cru->clksel_con[53],
753 CLK_SPI_DIV_CON_MASK << CLK_SPI1_DIV_CON_SHIFT |
754 CLK_SPI_PLL_SEL_MASK << CLK_SPI1_PLL_SEL_SHIFT,
755 (src_clk_div - 1) << CLK_SPI1_DIV_CON_SHIFT |
756 CLK_SPI_PLL_SEL_GPLL << CLK_SPI1_PLL_SEL_SHIFT);
757 break;
758 default:
759 printf("do not support this pwm bus\n");
760 return -EINVAL;
761 }
762
763 return px30_spi_get_clk(priv, clk_id);
764}
765
766static ulong px30_vop_get_clk(struct px30_clk_priv *priv, ulong clk_id)
767{
768 struct px30_cru *cru = priv->cru;
769 u32 div, con, parent;
770
771 switch (clk_id) {
772 case ACLK_VOPB:
773 case ACLK_VOPL:
774 con = readl(&cru->clksel_con[3]);
775 div = con & ACLK_VO_DIV_MASK;
776 parent = priv->gpll_hz;
777 break;
778 case DCLK_VOPB:
779 con = readl(&cru->clksel_con[5]);
780 div = con & DCLK_VOPB_DIV_MASK;
781 parent = rkclk_pll_get_rate(&cru->pll[CPLL], &cru->mode, CPLL);
782 break;
783 case DCLK_VOPL:
784 con = readl(&cru->clksel_con[8]);
785 div = con & DCLK_VOPL_DIV_MASK;
786 parent = rkclk_pll_get_rate(&cru->pll[NPLL], &cru->mode, NPLL);
787 break;
788 default:
789 return -ENOENT;
790 }
791
792 return DIV_TO_RATE(parent, div);
793}
794
795static ulong px30_vop_set_clk(struct px30_clk_priv *priv, ulong clk_id, uint hz)
796{
797 struct px30_cru *cru = priv->cru;
798 ulong npll_hz;
799 int src_clk_div;
800
801 switch (clk_id) {
802 case ACLK_VOPB:
803 case ACLK_VOPL:
804 src_clk_div = DIV_ROUND_UP(priv->gpll_hz, hz);
805 assert(src_clk_div - 1 <= 31);
806 rk_clrsetreg(&cru->clksel_con[3],
807 ACLK_VO_PLL_MASK | ACLK_VO_DIV_MASK,
808 ACLK_VO_SEL_GPLL << ACLK_VO_PLL_SHIFT |
809 (src_clk_div - 1) << ACLK_VO_DIV_SHIFT);
810 break;
811 case DCLK_VOPB:
812 if (hz < PX30_VOP_PLL_LIMIT) {
813 src_clk_div = DIV_ROUND_UP(PX30_VOP_PLL_LIMIT, hz);
814 if (src_clk_div % 2)
815 src_clk_div = src_clk_div - 1;
816 } else {
817 src_clk_div = 1;
818 }
819 assert(src_clk_div - 1 <= 255);
820 rkclk_set_pll(&cru->pll[CPLL], &cru->mode,
821 CPLL, hz * src_clk_div);
822 rk_clrsetreg(&cru->clksel_con[5],
823 DCLK_VOPB_SEL_MASK | DCLK_VOPB_PLL_SEL_MASK |
824 DCLK_VOPB_DIV_MASK,
825 DCLK_VOPB_SEL_DIVOUT << DCLK_VOPB_SEL_SHIFT |
826 DCLK_VOPB_PLL_SEL_CPLL << DCLK_VOPB_PLL_SEL_SHIFT |
827 (src_clk_div - 1) << DCLK_VOPB_DIV_SHIFT);
828 break;
829 case DCLK_VOPL:
830 npll_hz = px30_clk_get_pll_rate(priv, NPLL);
831 if (npll_hz >= PX30_VOP_PLL_LIMIT && npll_hz >= hz &&
832 npll_hz % hz == 0) {
833 src_clk_div = npll_hz / hz;
834 assert(src_clk_div - 1 <= 255);
835 } else {
836 if (hz < PX30_VOP_PLL_LIMIT) {
837 src_clk_div = DIV_ROUND_UP(PX30_VOP_PLL_LIMIT,
838 hz);
839 if (src_clk_div % 2)
840 src_clk_div = src_clk_div - 1;
841 } else {
842 src_clk_div = 1;
843 }
844 assert(src_clk_div - 1 <= 255);
845 rkclk_set_pll(&cru->pll[NPLL], &cru->mode, NPLL,
846 hz * src_clk_div);
847 }
848 rk_clrsetreg(&cru->clksel_con[8],
849 DCLK_VOPL_SEL_MASK | DCLK_VOPL_PLL_SEL_MASK |
850 DCLK_VOPL_DIV_MASK,
851 DCLK_VOPL_SEL_DIVOUT << DCLK_VOPL_SEL_SHIFT |
852 DCLK_VOPL_PLL_SEL_NPLL << DCLK_VOPL_PLL_SEL_SHIFT |
853 (src_clk_div - 1) << DCLK_VOPL_DIV_SHIFT);
854 break;
855 default:
856 printf("do not support this vop freq\n");
857 return -EINVAL;
858 }
859
860 return px30_vop_get_clk(priv, clk_id);
861}
862
863static ulong px30_bus_get_clk(struct px30_clk_priv *priv, ulong clk_id)
864{
865 struct px30_cru *cru = priv->cru;
866 u32 div, con, parent;
867
868 switch (clk_id) {
869 case ACLK_BUS_PRE:
870 con = readl(&cru->clksel_con[23]);
871 div = (con & BUS_ACLK_DIV_MASK) >> BUS_ACLK_DIV_SHIFT;
872 parent = priv->gpll_hz;
873 break;
874 case HCLK_BUS_PRE:
875 con = readl(&cru->clksel_con[24]);
876 div = (con & BUS_HCLK_DIV_MASK) >> BUS_HCLK_DIV_SHIFT;
877 parent = priv->gpll_hz;
878 break;
879 case PCLK_BUS_PRE:
880 case PCLK_WDT_NS:
881 parent = px30_bus_get_clk(priv, ACLK_BUS_PRE);
882 con = readl(&cru->clksel_con[24]);
883 div = (con & BUS_PCLK_DIV_MASK) >> BUS_PCLK_DIV_SHIFT;
884 break;
885 default:
886 return -ENOENT;
887 }
888
889 return DIV_TO_RATE(parent, div);
890}
891
892static ulong px30_bus_set_clk(struct px30_clk_priv *priv, ulong clk_id,
893 ulong hz)
894{
895 struct px30_cru *cru = priv->cru;
896 int src_clk_div;
897
898 /*
899 * select gpll as pd_bus bus clock source and
900 * set up dependent divisors for PCLK/HCLK and ACLK clocks.
901 */
902 switch (clk_id) {
903 case ACLK_BUS_PRE:
904 src_clk_div = DIV_ROUND_UP(priv->gpll_hz, hz);
905 assert(src_clk_div - 1 <= 31);
906 rk_clrsetreg(&cru->clksel_con[23],
907 BUS_PLL_SEL_MASK | BUS_ACLK_DIV_MASK,
908 BUS_PLL_SEL_GPLL << BUS_PLL_SEL_SHIFT |
909 (src_clk_div - 1) << BUS_ACLK_DIV_SHIFT);
910 break;
911 case HCLK_BUS_PRE:
912 src_clk_div = DIV_ROUND_UP(priv->gpll_hz, hz);
913 assert(src_clk_div - 1 <= 31);
914 rk_clrsetreg(&cru->clksel_con[24],
915 BUS_PLL_SEL_MASK | BUS_HCLK_DIV_MASK,
916 BUS_PLL_SEL_GPLL << BUS_PLL_SEL_SHIFT |
917 (src_clk_div - 1) << BUS_HCLK_DIV_SHIFT);
918 break;
919 case PCLK_BUS_PRE:
920 src_clk_div =
921 DIV_ROUND_UP(px30_bus_get_clk(priv, ACLK_BUS_PRE), hz);
922 assert(src_clk_div - 1 <= 3);
923 rk_clrsetreg(&cru->clksel_con[24],
924 BUS_PCLK_DIV_MASK,
925 (src_clk_div - 1) << BUS_PCLK_DIV_SHIFT);
926 break;
927 default:
928 printf("do not support this bus freq\n");
929 return -EINVAL;
930 }
931
932 return px30_bus_get_clk(priv, clk_id);
933}
934
935static ulong px30_peri_get_clk(struct px30_clk_priv *priv, ulong clk_id)
936{
937 struct px30_cru *cru = priv->cru;
938 u32 div, con, parent;
939
940 switch (clk_id) {
941 case ACLK_PERI_PRE:
942 con = readl(&cru->clksel_con[14]);
943 div = (con & PERI_ACLK_DIV_MASK) >> PERI_ACLK_DIV_SHIFT;
944 parent = priv->gpll_hz;
945 break;
946 case HCLK_PERI_PRE:
947 con = readl(&cru->clksel_con[14]);
948 div = (con & PERI_HCLK_DIV_MASK) >> PERI_HCLK_DIV_SHIFT;
949 parent = priv->gpll_hz;
950 break;
951 default:
952 return -ENOENT;
953 }
954
955 return DIV_TO_RATE(parent, div);
956}
957
958static ulong px30_peri_set_clk(struct px30_clk_priv *priv, ulong clk_id,
959 ulong hz)
960{
961 struct px30_cru *cru = priv->cru;
962 int src_clk_div;
963
964 src_clk_div = DIV_ROUND_UP(priv->gpll_hz, hz);
965 assert(src_clk_div - 1 <= 31);
966
967 /*
968 * select gpll as pd_peri bus clock source and
969 * set up dependent divisors for HCLK and ACLK clocks.
970 */
971 switch (clk_id) {
972 case ACLK_PERI_PRE:
973 rk_clrsetreg(&cru->clksel_con[14],
974 PERI_PLL_SEL_MASK | PERI_ACLK_DIV_MASK,
975 PERI_PLL_GPLL << PERI_PLL_SEL_SHIFT |
976 (src_clk_div - 1) << PERI_ACLK_DIV_SHIFT);
977 break;
978 case HCLK_PERI_PRE:
979 rk_clrsetreg(&cru->clksel_con[14],
980 PERI_PLL_SEL_MASK | PERI_HCLK_DIV_MASK,
981 PERI_PLL_GPLL << PERI_PLL_SEL_SHIFT |
982 (src_clk_div - 1) << PERI_HCLK_DIV_SHIFT);
983 break;
984 default:
985 printf("do not support this peri freq\n");
986 return -EINVAL;
987 }
988
989 return px30_peri_get_clk(priv, clk_id);
990}
991
992#ifndef CONFIG_SPL_BUILD
993static ulong px30_crypto_get_clk(struct px30_clk_priv *priv, ulong clk_id)
994{
995 struct px30_cru *cru = priv->cru;
996 u32 div, con, parent;
997
998 switch (clk_id) {
999 case SCLK_CRYPTO:
1000 con = readl(&cru->clksel_con[25]);
1001 div = (con & CRYPTO_DIV_MASK) >> CRYPTO_DIV_SHIFT;
1002 parent = priv->gpll_hz;
1003 break;
1004 case SCLK_CRYPTO_APK:
1005 con = readl(&cru->clksel_con[25]);
1006 div = (con & CRYPTO_APK_DIV_MASK) >> CRYPTO_APK_DIV_SHIFT;
1007 parent = priv->gpll_hz;
1008 break;
1009 default:
1010 return -ENOENT;
1011 }
1012
1013 return DIV_TO_RATE(parent, div);
1014}
1015
1016static ulong px30_crypto_set_clk(struct px30_clk_priv *priv, ulong clk_id,
1017 ulong hz)
1018{
1019 struct px30_cru *cru = priv->cru;
1020 int src_clk_div;
1021
1022 src_clk_div = DIV_ROUND_UP(priv->gpll_hz, hz);
1023 assert(src_clk_div - 1 <= 31);
1024
1025 /*
1026 * select gpll as crypto clock source and
1027 * set up dependent divisors for crypto clocks.
1028 */
1029 switch (clk_id) {
1030 case SCLK_CRYPTO:
1031 rk_clrsetreg(&cru->clksel_con[25],
1032 CRYPTO_PLL_SEL_MASK | CRYPTO_DIV_MASK,
1033 CRYPTO_PLL_SEL_GPLL << CRYPTO_PLL_SEL_SHIFT |
1034 (src_clk_div - 1) << CRYPTO_DIV_SHIFT);
1035 break;
1036 case SCLK_CRYPTO_APK:
1037 rk_clrsetreg(&cru->clksel_con[25],
1038 CRYPTO_APK_PLL_SEL_MASK | CRYPTO_APK_DIV_MASK,
1039 CRYPTO_PLL_SEL_GPLL << CRYPTO_APK_SEL_SHIFT |
1040 (src_clk_div - 1) << CRYPTO_APK_DIV_SHIFT);
1041 break;
1042 default:
1043 printf("do not support this peri freq\n");
1044 return -EINVAL;
1045 }
1046
1047 return px30_crypto_get_clk(priv, clk_id);
1048}
1049
1050static ulong px30_i2s1_mclk_get_clk(struct px30_clk_priv *priv, ulong clk_id)
1051{
1052 struct px30_cru *cru = priv->cru;
1053 u32 con;
1054
1055 con = readl(&cru->clksel_con[30]);
1056
1057 if (!(con & CLK_I2S1_OUT_SEL_MASK))
1058 return -ENOENT;
1059
1060 return 12000000;
1061}
1062
1063static ulong px30_i2s1_mclk_set_clk(struct px30_clk_priv *priv, ulong clk_id,
1064 ulong hz)
1065{
1066 struct px30_cru *cru = priv->cru;
1067
1068 if (hz != 12000000) {
1069 printf("do not support this i2s1_mclk freq\n");
1070 return -EINVAL;
1071 }
1072
1073 rk_clrsetreg(&cru->clksel_con[30], CLK_I2S1_OUT_SEL_MASK,
1074 CLK_I2S1_OUT_SEL_OSC);
1075 rk_clrsetreg(&cru->clkgate_con[10], CLK_I2S1_OUT_MCLK_PAD_MASK,
1076 CLK_I2S1_OUT_MCLK_PAD_ENABLE);
1077
1078 return px30_i2s1_mclk_get_clk(priv, clk_id);
1079}
1080
1081static ulong px30_mac_set_clk(struct px30_clk_priv *priv, uint hz)
1082{
1083 struct px30_cru *cru = priv->cru;
1084 u32 con = readl(&cru->clksel_con[22]);
1085 ulong pll_rate;
1086 u8 div;
1087
1088 if ((con >> GMAC_PLL_SEL_SHIFT) & GMAC_PLL_SEL_CPLL)
1089 pll_rate = px30_clk_get_pll_rate(priv, CPLL);
1090 else if ((con >> GMAC_PLL_SEL_SHIFT) & GMAC_PLL_SEL_NPLL)
1091 pll_rate = px30_clk_get_pll_rate(priv, NPLL);
1092 else
1093 pll_rate = priv->gpll_hz;
1094
1095 /*default set 50MHZ for gmac*/
1096 if (!hz)
1097 hz = 50000000;
1098
1099 div = DIV_ROUND_UP(pll_rate, hz) - 1;
1100 assert(div < 32);
1101 rk_clrsetreg(&cru->clksel_con[22], CLK_GMAC_DIV_MASK,
1102 div << CLK_GMAC_DIV_SHIFT);
1103
1104 return DIV_TO_RATE(pll_rate, div);
1105}
1106
1107static int px30_mac_set_speed_clk(struct px30_clk_priv *priv, uint hz)
1108{
1109 struct px30_cru *cru = priv->cru;
1110
1111 if (hz != 2500000 && hz != 25000000) {
1112 debug("Unsupported mac speed:%d\n", hz);
1113 return -EINVAL;
1114 }
1115
1116 rk_clrsetreg(&cru->clksel_con[23], RMII_CLK_SEL_MASK,
1117 ((hz == 2500000) ? 0 : 1) << RMII_CLK_SEL_SHIFT);
1118
1119 return 0;
1120}
1121
1122#endif
1123
1124static ulong px30_clk_get_pll_rate(struct px30_clk_priv *priv,
1125 enum px30_pll_id pll_id)
1126{
1127 struct px30_cru *cru = priv->cru;
1128
1129 return rkclk_pll_get_rate(&cru->pll[pll_id], &cru->mode, pll_id);
1130}
1131
1132static ulong px30_clk_set_pll_rate(struct px30_clk_priv *priv,
1133 enum px30_pll_id pll_id, ulong hz)
1134{
1135 struct px30_cru *cru = priv->cru;
1136
1137 if (rkclk_set_pll(&cru->pll[pll_id], &cru->mode, pll_id, hz))
1138 return -EINVAL;
1139 return rkclk_pll_get_rate(&cru->pll[pll_id], &cru->mode, pll_id);
1140}
1141
1142static ulong px30_armclk_set_clk(struct px30_clk_priv *priv, ulong hz)
1143{
1144 struct px30_cru *cru = priv->cru;
1145 const struct cpu_rate_table *rate;
1146 ulong old_rate;
1147
1148 rate = get_cpu_settings(hz);
1149 if (!rate) {
1150 printf("%s unsupport rate\n", __func__);
1151 return -EINVAL;
1152 }
1153
1154 /*
1155 * select apll as cpu/core clock pll source and
1156 * set up dependent divisors for PERI and ACLK clocks.
1157 * core hz : apll = 1:1
1158 */
1159 old_rate = px30_clk_get_pll_rate(priv, APLL);
1160 if (old_rate > hz) {
1161 if (rkclk_set_pll(&cru->pll[APLL], &cru->mode, APLL, hz))
1162 return -EINVAL;
1163 rk_clrsetreg(&cru->clksel_con[0],
1164 CORE_CLK_PLL_SEL_MASK | CORE_DIV_CON_MASK |
1165 CORE_ACLK_DIV_MASK | CORE_DBG_DIV_MASK,
1166 rate->aclk_div << CORE_ACLK_DIV_SHIFT |
1167 rate->pclk_div << CORE_DBG_DIV_SHIFT |
1168 CORE_CLK_PLL_SEL_APLL << CORE_CLK_PLL_SEL_SHIFT |
1169 0 << CORE_DIV_CON_SHIFT);
1170 } else if (old_rate < hz) {
1171 rk_clrsetreg(&cru->clksel_con[0],
1172 CORE_CLK_PLL_SEL_MASK | CORE_DIV_CON_MASK |
1173 CORE_ACLK_DIV_MASK | CORE_DBG_DIV_MASK,
1174 rate->aclk_div << CORE_ACLK_DIV_SHIFT |
1175 rate->pclk_div << CORE_DBG_DIV_SHIFT |
1176 CORE_CLK_PLL_SEL_APLL << CORE_CLK_PLL_SEL_SHIFT |
1177 0 << CORE_DIV_CON_SHIFT);
1178 if (rkclk_set_pll(&cru->pll[APLL], &cru->mode, APLL, hz))
1179 return -EINVAL;
1180 }
1181
1182 return px30_clk_get_pll_rate(priv, APLL);
1183}
1184
1185static ulong px30_clk_get_rate(struct clk *clk)
1186{
1187 struct px30_clk_priv *priv = dev_get_priv(clk->dev);
1188 ulong rate = 0;
1189
1190 if (!priv->gpll_hz && clk->id > ARMCLK) {
1191 printf("%s gpll=%lu\n", __func__, priv->gpll_hz);
1192 return -ENOENT;
1193 }
1194
1195 debug("%s %ld\n", __func__, clk->id);
1196 switch (clk->id) {
1197 case PLL_APLL:
1198 rate = px30_clk_get_pll_rate(priv, APLL);
1199 break;
1200 case PLL_DPLL:
1201 rate = px30_clk_get_pll_rate(priv, DPLL);
1202 break;
1203 case PLL_CPLL:
1204 rate = px30_clk_get_pll_rate(priv, CPLL);
1205 break;
1206 case PLL_NPLL:
1207 rate = px30_clk_get_pll_rate(priv, NPLL);
1208 break;
1209 case ARMCLK:
1210 rate = px30_clk_get_pll_rate(priv, APLL);
1211 break;
1212 case HCLK_SDMMC:
1213 case HCLK_EMMC:
1214 case SCLK_SDMMC:
1215 case SCLK_EMMC:
1216 case SCLK_EMMC_SAMPLE:
1217 rate = px30_mmc_get_clk(priv, clk->id);
1218 break;
Jon Lin8f20e732021-08-05 16:27:53 +08001219 case SCLK_SFC:
1220 rate = px30_sfc_get_clk(priv, clk->id);
1221 break;
Kever Yangba1033d2019-07-11 10:42:16 +02001222 case SCLK_I2C0:
1223 case SCLK_I2C1:
1224 case SCLK_I2C2:
1225 case SCLK_I2C3:
1226 rate = px30_i2c_get_clk(priv, clk->id);
1227 break;
1228 case SCLK_I2S1:
1229 rate = px30_i2s_get_clk(priv, clk->id);
1230 break;
1231 case SCLK_NANDC:
1232 rate = px30_nandc_get_clk(priv);
1233 break;
1234 case SCLK_PWM0:
1235 case SCLK_PWM1:
1236 rate = px30_pwm_get_clk(priv, clk->id);
1237 break;
1238 case SCLK_SARADC:
1239 rate = px30_saradc_get_clk(priv);
1240 break;
1241 case SCLK_TSADC:
1242 rate = px30_tsadc_get_clk(priv);
1243 break;
1244 case SCLK_SPI0:
1245 case SCLK_SPI1:
1246 rate = px30_spi_get_clk(priv, clk->id);
1247 break;
1248 case ACLK_VOPB:
1249 case ACLK_VOPL:
1250 case DCLK_VOPB:
1251 case DCLK_VOPL:
1252 rate = px30_vop_get_clk(priv, clk->id);
1253 break;
1254 case ACLK_BUS_PRE:
1255 case HCLK_BUS_PRE:
1256 case PCLK_BUS_PRE:
1257 case PCLK_WDT_NS:
1258 rate = px30_bus_get_clk(priv, clk->id);
1259 break;
1260 case ACLK_PERI_PRE:
1261 case HCLK_PERI_PRE:
1262 rate = px30_peri_get_clk(priv, clk->id);
1263 break;
1264#ifndef CONFIG_SPL_BUILD
1265 case SCLK_CRYPTO:
1266 case SCLK_CRYPTO_APK:
1267 rate = px30_crypto_get_clk(priv, clk->id);
1268 break;
1269#endif
1270 default:
1271 return -ENOENT;
1272 }
1273
1274 return rate;
1275}
1276
1277static ulong px30_clk_set_rate(struct clk *clk, ulong rate)
1278{
1279 struct px30_clk_priv *priv = dev_get_priv(clk->dev);
1280 ulong ret = 0;
1281
1282 if (!priv->gpll_hz && clk->id > ARMCLK) {
1283 printf("%s gpll=%lu\n", __func__, priv->gpll_hz);
1284 return -ENOENT;
1285 }
1286
1287 debug("%s %ld %ld\n", __func__, clk->id, rate);
1288 switch (clk->id) {
1289 case PLL_NPLL:
1290 ret = px30_clk_set_pll_rate(priv, NPLL, rate);
1291 break;
Chris Morganb9599102021-08-05 11:48:47 -05001292 case PLL_CPLL:
1293 ret = px30_clk_set_pll_rate(priv, CPLL, rate);
1294 break;
Kever Yangba1033d2019-07-11 10:42:16 +02001295 case ARMCLK:
1296 ret = px30_armclk_set_clk(priv, rate);
1297 break;
1298 case HCLK_SDMMC:
1299 case HCLK_EMMC:
1300 case SCLK_SDMMC:
1301 case SCLK_EMMC:
1302 ret = px30_mmc_set_clk(priv, clk->id, rate);
1303 break;
Jon Lin8f20e732021-08-05 16:27:53 +08001304 case SCLK_SFC:
1305 ret = px30_sfc_set_clk(priv, clk->id, rate);
1306 break;
Kever Yangba1033d2019-07-11 10:42:16 +02001307 case SCLK_I2C0:
1308 case SCLK_I2C1:
1309 case SCLK_I2C2:
1310 case SCLK_I2C3:
1311 ret = px30_i2c_set_clk(priv, clk->id, rate);
1312 break;
1313 case SCLK_I2S1:
1314 ret = px30_i2s_set_clk(priv, clk->id, rate);
1315 break;
1316 case SCLK_NANDC:
1317 ret = px30_nandc_set_clk(priv, rate);
1318 break;
1319 case SCLK_PWM0:
1320 case SCLK_PWM1:
1321 ret = px30_pwm_set_clk(priv, clk->id, rate);
1322 break;
1323 case SCLK_SARADC:
1324 ret = px30_saradc_set_clk(priv, rate);
1325 break;
1326 case SCLK_TSADC:
1327 ret = px30_tsadc_set_clk(priv, rate);
1328 break;
1329 case SCLK_SPI0:
1330 case SCLK_SPI1:
1331 ret = px30_spi_set_clk(priv, clk->id, rate);
1332 break;
1333 case ACLK_VOPB:
1334 case ACLK_VOPL:
1335 case DCLK_VOPB:
1336 case DCLK_VOPL:
1337 ret = px30_vop_set_clk(priv, clk->id, rate);
1338 break;
1339 case ACLK_BUS_PRE:
1340 case HCLK_BUS_PRE:
1341 case PCLK_BUS_PRE:
1342 ret = px30_bus_set_clk(priv, clk->id, rate);
1343 break;
1344 case ACLK_PERI_PRE:
1345 case HCLK_PERI_PRE:
1346 ret = px30_peri_set_clk(priv, clk->id, rate);
1347 break;
1348#ifndef CONFIG_SPL_BUILD
1349 case SCLK_CRYPTO:
1350 case SCLK_CRYPTO_APK:
1351 ret = px30_crypto_set_clk(priv, clk->id, rate);
1352 break;
1353 case SCLK_I2S1_OUT:
1354 ret = px30_i2s1_mclk_set_clk(priv, clk->id, rate);
1355 break;
1356 case SCLK_GMAC:
1357 case SCLK_GMAC_SRC:
1358 ret = px30_mac_set_clk(priv, rate);
1359 break;
1360 case SCLK_GMAC_RMII:
1361 ret = px30_mac_set_speed_clk(priv, rate);
1362 break;
1363#endif
1364 default:
1365 return -ENOENT;
1366 }
1367
1368 return ret;
1369}
1370
Simon Glass3580f6d2021-08-07 07:24:03 -06001371#if CONFIG_IS_ENABLED(OF_REAL)
Kever Yangba1033d2019-07-11 10:42:16 +02001372static int px30_gmac_set_parent(struct clk *clk, struct clk *parent)
1373{
1374 struct px30_clk_priv *priv = dev_get_priv(clk->dev);
1375 struct px30_cru *cru = priv->cru;
1376
1377 if (parent->id == SCLK_GMAC_SRC) {
1378 debug("%s: switching GAMC to SCLK_GMAC_SRC\n", __func__);
1379 rk_clrsetreg(&cru->clksel_con[23], RMII_EXTCLK_SEL_MASK,
1380 RMII_EXTCLK_SEL_INT << RMII_EXTCLK_SEL_SHIFT);
1381 } else {
1382 debug("%s: switching GMAC to external clock\n", __func__);
1383 rk_clrsetreg(&cru->clksel_con[23], RMII_EXTCLK_SEL_MASK,
1384 RMII_EXTCLK_SEL_EXT << RMII_EXTCLK_SEL_SHIFT);
1385 }
1386 return 0;
1387}
1388
1389static int px30_clk_set_parent(struct clk *clk, struct clk *parent)
1390{
1391 switch (clk->id) {
1392 case SCLK_GMAC:
1393 return px30_gmac_set_parent(clk, parent);
1394 default:
1395 return -ENOENT;
1396 }
1397}
1398#endif
1399
1400static int px30_clk_enable(struct clk *clk)
1401{
1402 switch (clk->id) {
1403 case HCLK_HOST:
Chris Morgana5798682022-03-25 12:09:22 -05001404 case HCLK_OTG:
1405 case HCLK_SFC:
Kever Yangba1033d2019-07-11 10:42:16 +02001406 case SCLK_GMAC:
1407 case SCLK_GMAC_RX_TX:
1408 case SCLK_MAC_REF:
1409 case SCLK_MAC_REFOUT:
Chris Morgana5798682022-03-25 12:09:22 -05001410 case SCLK_SFC:
Kever Yangba1033d2019-07-11 10:42:16 +02001411 case ACLK_GMAC:
1412 case PCLK_GMAC:
1413 case SCLK_GMAC_RMII:
1414 /* Required to successfully probe the Designware GMAC driver */
1415 return 0;
Quentin Schulz9b2f9d12022-11-14 11:33:46 +01001416 case PCLK_WDT_NS:
1417 /* Required to successfully probe the Designware watchdog driver */
1418 return 0;
Kever Yangba1033d2019-07-11 10:42:16 +02001419 }
1420
1421 debug("%s: unsupported clk %ld\n", __func__, clk->id);
1422 return -ENOENT;
1423}
1424
1425static struct clk_ops px30_clk_ops = {
1426 .get_rate = px30_clk_get_rate,
1427 .set_rate = px30_clk_set_rate,
Simon Glass3580f6d2021-08-07 07:24:03 -06001428#if CONFIG_IS_ENABLED(OF_REAL)
Kever Yangba1033d2019-07-11 10:42:16 +02001429 .set_parent = px30_clk_set_parent,
1430#endif
1431 .enable = px30_clk_enable,
1432};
1433
1434static void px30_clk_init(struct px30_clk_priv *priv)
1435{
1436 ulong npll_hz;
1437 int ret;
1438
1439 npll_hz = px30_clk_get_pll_rate(priv, NPLL);
1440 if (npll_hz != NPLL_HZ) {
1441 ret = px30_clk_set_pll_rate(priv, NPLL, NPLL_HZ);
1442 if (ret < 0)
1443 printf("%s failed to set npll rate\n", __func__);
1444 }
1445
1446 px30_bus_set_clk(priv, ACLK_BUS_PRE, ACLK_BUS_HZ);
1447 px30_bus_set_clk(priv, HCLK_BUS_PRE, HCLK_BUS_HZ);
1448 px30_bus_set_clk(priv, PCLK_BUS_PRE, PCLK_BUS_HZ);
1449 px30_peri_set_clk(priv, ACLK_PERI_PRE, ACLK_PERI_HZ);
1450 px30_peri_set_clk(priv, HCLK_PERI_PRE, HCLK_PERI_HZ);
1451}
1452
1453static int px30_clk_probe(struct udevice *dev)
1454{
1455 struct px30_clk_priv *priv = dev_get_priv(dev);
1456 struct clk clk_gpll;
1457 int ret;
1458
1459 if (px30_clk_get_pll_rate(priv, APLL) != APLL_HZ)
1460 px30_armclk_set_clk(priv, APLL_HZ);
1461
1462 /* get the GPLL rate from the pmucru */
1463 ret = clk_get_by_name(dev, "gpll", &clk_gpll);
1464 if (ret) {
1465 printf("%s: failed to get gpll clk from pmucru\n", __func__);
1466 return ret;
1467 }
1468
1469 priv->gpll_hz = clk_get_rate(&clk_gpll);
1470
1471 px30_clk_init(priv);
1472
1473 return 0;
1474}
1475
Simon Glassaad29ae2020-12-03 16:55:21 -07001476static int px30_clk_of_to_plat(struct udevice *dev)
Kever Yangba1033d2019-07-11 10:42:16 +02001477{
1478 struct px30_clk_priv *priv = dev_get_priv(dev);
1479
1480 priv->cru = dev_read_addr_ptr(dev);
1481
1482 return 0;
1483}
1484
1485static int px30_clk_bind(struct udevice *dev)
1486{
1487 int ret;
1488 struct udevice *sys_child;
1489 struct sysreset_reg *priv;
1490
1491 /* The reset driver does not have a device node, so bind it here */
1492 ret = device_bind_driver(dev, "rockchip_sysreset", "sysreset",
1493 &sys_child);
1494 if (ret) {
1495 debug("Warning: No sysreset driver: ret=%d\n", ret);
1496 } else {
1497 priv = malloc(sizeof(struct sysreset_reg));
1498 priv->glb_srst_fst_value = offsetof(struct px30_cru,
1499 glb_srst_fst);
1500 priv->glb_srst_snd_value = offsetof(struct px30_cru,
1501 glb_srst_snd);
Simon Glass95588622020-12-22 19:30:28 -07001502 dev_set_priv(sys_child, priv);
Kever Yangba1033d2019-07-11 10:42:16 +02001503 }
1504
1505#if CONFIG_IS_ENABLED(RESET_ROCKCHIP)
1506 ret = offsetof(struct px30_cru, softrst_con[0]);
1507 ret = rockchip_reset_bind(dev, ret, 12);
1508 if (ret)
Eugen Hristevf1798262023-04-11 10:17:56 +03001509 debug("Warning: software reset driver bind failed\n");
Kever Yangba1033d2019-07-11 10:42:16 +02001510#endif
1511
1512 return 0;
1513}
1514
1515static const struct udevice_id px30_clk_ids[] = {
1516 { .compatible = "rockchip,px30-cru" },
1517 { }
1518};
1519
1520U_BOOT_DRIVER(rockchip_px30_cru) = {
1521 .name = "rockchip_px30_cru",
1522 .id = UCLASS_CLK,
1523 .of_match = px30_clk_ids,
Simon Glass8a2b47f2020-12-03 16:55:17 -07001524 .priv_auto = sizeof(struct px30_clk_priv),
Simon Glassaad29ae2020-12-03 16:55:21 -07001525 .of_to_plat = px30_clk_of_to_plat,
Kever Yangba1033d2019-07-11 10:42:16 +02001526 .ops = &px30_clk_ops,
1527 .bind = px30_clk_bind,
1528 .probe = px30_clk_probe,
1529};
1530
1531static ulong px30_pclk_pmu_get_pmuclk(struct px30_pmuclk_priv *priv)
1532{
1533 struct px30_pmucru *pmucru = priv->pmucru;
1534 u32 div, con;
1535
1536 con = readl(&pmucru->pmu_clksel_con[0]);
1537 div = (con & CLK_PMU_PCLK_DIV_MASK) >> CLK_PMU_PCLK_DIV_SHIFT;
1538
1539 return DIV_TO_RATE(priv->gpll_hz, div);
1540}
1541
1542static ulong px30_pclk_pmu_set_pmuclk(struct px30_pmuclk_priv *priv, ulong hz)
1543{
1544 struct px30_pmucru *pmucru = priv->pmucru;
1545 int src_clk_div;
1546
1547 src_clk_div = DIV_ROUND_UP(priv->gpll_hz, hz);
1548 assert(src_clk_div - 1 <= 31);
1549
1550 rk_clrsetreg(&pmucru->pmu_clksel_con[0],
1551 CLK_PMU_PCLK_DIV_MASK,
1552 (src_clk_div - 1) << CLK_PMU_PCLK_DIV_SHIFT);
1553
1554 return px30_pclk_pmu_get_pmuclk(priv);
1555}
1556
1557static ulong px30_pmuclk_get_gpll_rate(struct px30_pmuclk_priv *priv)
1558{
1559 struct px30_pmucru *pmucru = priv->pmucru;
1560
1561 return rkclk_pll_get_rate(&pmucru->pll, &pmucru->pmu_mode, GPLL);
1562}
1563
1564static ulong px30_pmuclk_set_gpll_rate(struct px30_pmuclk_priv *priv, ulong hz)
1565{
1566 struct px30_pmucru *pmucru = priv->pmucru;
1567 ulong pclk_pmu_rate;
1568 u32 div;
1569
1570 if (priv->gpll_hz == hz)
1571 return priv->gpll_hz;
1572
1573 div = DIV_ROUND_UP(hz, priv->gpll_hz);
1574
1575 /* save clock rate */
1576 pclk_pmu_rate = px30_pclk_pmu_get_pmuclk(priv);
1577
1578 /* avoid rate too large, reduce rate first */
1579 px30_pclk_pmu_set_pmuclk(priv, pclk_pmu_rate / div);
1580
1581 /* change gpll rate */
1582 rkclk_set_pll(&pmucru->pll, &pmucru->pmu_mode, GPLL, hz);
1583 priv->gpll_hz = px30_pmuclk_get_gpll_rate(priv);
1584
1585 /* restore clock rate */
1586 px30_pclk_pmu_set_pmuclk(priv, pclk_pmu_rate);
1587
1588 return priv->gpll_hz;
1589}
1590
1591static ulong px30_pmuclk_get_rate(struct clk *clk)
1592{
1593 struct px30_pmuclk_priv *priv = dev_get_priv(clk->dev);
1594 ulong rate = 0;
1595
1596 debug("%s %ld\n", __func__, clk->id);
1597 switch (clk->id) {
1598 case PLL_GPLL:
1599 rate = px30_pmuclk_get_gpll_rate(priv);
1600 break;
1601 case PCLK_PMU_PRE:
1602 rate = px30_pclk_pmu_get_pmuclk(priv);
1603 break;
1604 default:
1605 return -ENOENT;
1606 }
1607
1608 return rate;
1609}
1610
1611static ulong px30_pmuclk_set_rate(struct clk *clk, ulong rate)
1612{
1613 struct px30_pmuclk_priv *priv = dev_get_priv(clk->dev);
1614 ulong ret = 0;
1615
1616 debug("%s %ld %ld\n", __func__, clk->id, rate);
1617 switch (clk->id) {
1618 case PLL_GPLL:
1619 ret = px30_pmuclk_set_gpll_rate(priv, rate);
1620 break;
1621 case PCLK_PMU_PRE:
1622 ret = px30_pclk_pmu_set_pmuclk(priv, rate);
1623 break;
1624 default:
1625 return -ENOENT;
1626 }
1627
1628 return ret;
1629}
1630
1631static struct clk_ops px30_pmuclk_ops = {
1632 .get_rate = px30_pmuclk_get_rate,
1633 .set_rate = px30_pmuclk_set_rate,
1634};
1635
1636static void px30_pmuclk_init(struct px30_pmuclk_priv *priv)
1637{
1638 priv->gpll_hz = px30_pmuclk_get_gpll_rate(priv);
1639 px30_pmuclk_set_gpll_rate(priv, GPLL_HZ);
1640
1641 px30_pclk_pmu_set_pmuclk(priv, PCLK_PMU_HZ);
1642}
1643
1644static int px30_pmuclk_probe(struct udevice *dev)
1645{
1646 struct px30_pmuclk_priv *priv = dev_get_priv(dev);
1647
1648 px30_pmuclk_init(priv);
1649
1650 return 0;
1651}
1652
Simon Glassaad29ae2020-12-03 16:55:21 -07001653static int px30_pmuclk_of_to_plat(struct udevice *dev)
Kever Yangba1033d2019-07-11 10:42:16 +02001654{
1655 struct px30_pmuclk_priv *priv = dev_get_priv(dev);
1656
1657 priv->pmucru = dev_read_addr_ptr(dev);
1658
1659 return 0;
1660}
1661
1662static const struct udevice_id px30_pmuclk_ids[] = {
1663 { .compatible = "rockchip,px30-pmucru" },
1664 { }
1665};
1666
1667U_BOOT_DRIVER(rockchip_px30_pmucru) = {
1668 .name = "rockchip_px30_pmucru",
1669 .id = UCLASS_CLK,
1670 .of_match = px30_pmuclk_ids,
Simon Glass8a2b47f2020-12-03 16:55:17 -07001671 .priv_auto = sizeof(struct px30_pmuclk_priv),
Simon Glassaad29ae2020-12-03 16:55:21 -07001672 .of_to_plat = px30_pmuclk_of_to_plat,
Kever Yangba1033d2019-07-11 10:42:16 +02001673 .ops = &px30_pmuclk_ops,
1674 .probe = px30_pmuclk_probe,
1675};