blob: 1bb31b3313b53871d7e21ab74287eed6b262d89b [file] [log] [blame]
Elaine Zhanga8a2ca82019-10-25 09:42:17 +08001// SPDX-License-Identifier: GPL-2.0
2/*
3 * (C) Copyright 2018-2019 Rockchip Electronics Co., Ltd
4 */
5 #include <common.h>
6#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>
Elaine Zhanga8a2ca82019-10-25 09:42:17 +080011#include <asm/io.h>
12#include <asm/arch-rockchip/clock.h>
13#include <asm/arch-rockchip/hardware.h>
14#include <div64.h>
Simon Glassdbd79542020-05-10 11:40:11 -060015#include <linux/delay.h>
Elaine Zhanga8a2ca82019-10-25 09:42:17 +080016
17static struct rockchip_pll_rate_table rockchip_auto_table;
18
19#define PLL_MODE_MASK 0x3
20#define PLL_RK3328_MODE_MASK 0x1
21
22#define RK3036_PLLCON0_FBDIV_MASK 0xfff
23#define RK3036_PLLCON0_FBDIV_SHIFT 0
24#define RK3036_PLLCON0_POSTDIV1_MASK 0x7 << 12
25#define RK3036_PLLCON0_POSTDIV1_SHIFT 12
26#define RK3036_PLLCON1_REFDIV_MASK 0x3f
27#define RK3036_PLLCON1_REFDIV_SHIFT 0
28#define RK3036_PLLCON1_POSTDIV2_MASK 0x7 << 6
29#define RK3036_PLLCON1_POSTDIV2_SHIFT 6
30#define RK3036_PLLCON1_DSMPD_MASK 0x1 << 12
31#define RK3036_PLLCON1_DSMPD_SHIFT 12
32#define RK3036_PLLCON2_FRAC_MASK 0xffffff
33#define RK3036_PLLCON2_FRAC_SHIFT 0
Michal Suchanek45783492022-09-28 12:41:29 +020034#define RK3036_PLLCON1_PWRDOWN_SHIFT 13
Elaine Zhanga8a2ca82019-10-25 09:42:17 +080035
36#define MHZ 1000000
37#define KHZ 1000
38enum {
39 OSC_HZ = 24 * 1000000,
40 VCO_MAX_HZ = 3200U * 1000000,
41 VCO_MIN_HZ = 800 * 1000000,
42 OUTPUT_MAX_HZ = 3200U * 1000000,
43 OUTPUT_MIN_HZ = 24 * 1000000,
44};
45
46#define MIN_FOUTVCO_FREQ (800 * MHZ)
47#define MAX_FOUTVCO_FREQ (2000 * MHZ)
Jagan Teki7d1bf8d2023-01-30 20:27:37 +053048#define RK3588_VCO_MIN_HZ (2250UL * MHZ)
49#define RK3588_VCO_MAX_HZ (4500UL * MHZ)
50#define RK3588_FOUT_MIN_HZ (37UL * MHZ)
51#define RK3588_FOUT_MAX_HZ (4500UL * MHZ)
Elaine Zhanga8a2ca82019-10-25 09:42:17 +080052
53int gcd(int m, int n)
54{
55 int t;
56
57 while (m > 0) {
58 if (n > m) {
59 t = m;
60 m = n;
61 n = t;
62 } /* swap */
63 m -= n;
64 }
65 return n;
66}
67
68/*
69 * How to calculate the PLL(from TRM V0.3 Part 1 Page 63):
70 * Formulas also embedded within the Fractional PLL Verilog model:
71 * If DSMPD = 1 (DSM is disabled, "integer mode")
72 * FOUTVCO = FREF / REFDIV * FBDIV
73 * FOUTPOSTDIV = FOUTVCO / POSTDIV1 / POSTDIV2
74 * Where:
75 * FOUTVCO = Fractional PLL non-divided output frequency
76 * FOUTPOSTDIV = Fractional PLL divided output frequency
77 * (output of second post divider)
78 * FREF = Fractional PLL input reference frequency, (the OSC_HZ 24MHz input)
79 * REFDIV = Fractional PLL input reference clock divider
80 * FBDIV = Integer value programmed into feedback divide
81 *
82 */
83
84static int rockchip_pll_clk_set_postdiv(ulong fout_hz,
85 u32 *postdiv1,
86 u32 *postdiv2,
87 u32 *foutvco)
88{
89 ulong freq;
90
91 if (fout_hz < MIN_FOUTVCO_FREQ) {
92 for (*postdiv1 = 1; *postdiv1 <= 7; (*postdiv1)++) {
93 for (*postdiv2 = 1; *postdiv2 <= 7; (*postdiv2)++) {
94 freq = fout_hz * (*postdiv1) * (*postdiv2);
95 if (freq >= MIN_FOUTVCO_FREQ &&
96 freq <= MAX_FOUTVCO_FREQ) {
97 *foutvco = freq;
98 return 0;
99 }
100 }
101 }
102 printf("Can't FIND postdiv1/2 to make fout=%lu in 800~2000M.\n",
103 fout_hz);
104 } else {
105 *postdiv1 = 1;
106 *postdiv2 = 1;
107 }
108 return 0;
109}
110
111static struct rockchip_pll_rate_table *
112rockchip_pll_clk_set_by_auto(ulong fin_hz,
113 ulong fout_hz)
114{
115 struct rockchip_pll_rate_table *rate_table = &rockchip_auto_table;
116 /* FIXME set postdiv1/2 always 1*/
117 u32 foutvco = fout_hz;
118 ulong fin_64, frac_64;
119 u32 f_frac, postdiv1, postdiv2;
120 ulong clk_gcd = 0;
121
122 if (fin_hz == 0 || fout_hz == 0 || fout_hz == fin_hz)
123 return NULL;
124
125 rockchip_pll_clk_set_postdiv(fout_hz, &postdiv1, &postdiv2, &foutvco);
126 rate_table->postdiv1 = postdiv1;
127 rate_table->postdiv2 = postdiv2;
128 rate_table->dsmpd = 1;
129
130 if (fin_hz / MHZ * MHZ == fin_hz && fout_hz / MHZ * MHZ == fout_hz) {
131 fin_hz /= MHZ;
132 foutvco /= MHZ;
133 clk_gcd = gcd(fin_hz, foutvco);
134 rate_table->refdiv = fin_hz / clk_gcd;
135 rate_table->fbdiv = foutvco / clk_gcd;
136
137 rate_table->frac = 0;
138
139 debug("fin = %ld, fout = %ld, clk_gcd = %ld,\n",
140 fin_hz, fout_hz, clk_gcd);
141 debug("refdiv= %d,fbdiv= %d,postdiv1= %d,postdiv2= %d\n",
142 rate_table->refdiv,
143 rate_table->fbdiv, rate_table->postdiv1,
144 rate_table->postdiv2);
145 } else {
146 debug("frac div,fin_hz = %ld,fout_hz = %ld\n",
147 fin_hz, fout_hz);
148 debug("frac get postdiv1 = %d, postdiv2 = %d, foutvco = %d\n",
149 rate_table->postdiv1, rate_table->postdiv2, foutvco);
150 clk_gcd = gcd(fin_hz / MHZ, foutvco / MHZ);
151 rate_table->refdiv = fin_hz / MHZ / clk_gcd;
152 rate_table->fbdiv = foutvco / MHZ / clk_gcd;
153 debug("frac get refdiv = %d, fbdiv = %d\n",
154 rate_table->refdiv, rate_table->fbdiv);
155
156 rate_table->frac = 0;
157
158 f_frac = (foutvco % MHZ);
159 fin_64 = fin_hz;
160 fin_64 = fin_64 / rate_table->refdiv;
161 frac_64 = f_frac << 24;
162 frac_64 = frac_64 / fin_64;
163 rate_table->frac = frac_64;
164 if (rate_table->frac > 0)
165 rate_table->dsmpd = 0;
166 debug("frac = %x\n", rate_table->frac);
167 }
168 return rate_table;
169}
170
Elaine Zhangbb988a32023-10-12 18:18:28 +0800171static u32
172rockchip_rk3588_pll_k_get(u32 m, u32 p, u32 s, u64 fin_hz, u64 fvco)
173{
174 u64 fref, fout, ffrac;
175 u32 k = 0;
176
177 fref = fin_hz / p;
178 ffrac = fvco - (m * fref);
179 fout = ffrac * 65536;
180 k = fout / fref;
181 if (k > 32767) {
182 fref = fin_hz / p;
183 ffrac = ((m + 1) * fref) - fvco;
184 fout = ffrac * 65536;
185 k = ((fout * 10 / fref) + 7) / 10;
186 if (k > 32767)
187 k = 0;
188 else
189 k = ~k + 1;
190 }
191 return k;
192}
193
194static struct rockchip_pll_rate_table *
195rockchip_rk3588_pll_frac_by_auto(unsigned long fin_hz, unsigned long fout_hz)
196{
197 struct rockchip_pll_rate_table *rate_table = &rockchip_auto_table;
198 u32 p, m, s, k;
199 u64 fvco;
200
201 for (s = 0; s <= 6; s++) {
202 fvco = (u64)fout_hz << s;
203 if (fvco < RK3588_VCO_MIN_HZ || fvco > RK3588_VCO_MAX_HZ)
204 continue;
205 for (p = 1; p <= 4; p++) {
206 for (m = 64; m <= 1023; m++) {
207 if ((fvco >= m * fin_hz / p) &&
208 (fvco < (m + 1) * fin_hz / p)) {
209 k = rockchip_rk3588_pll_k_get(m, p, s,
210 fin_hz,
211 fvco);
212 if (!k)
213 continue;
214 rate_table->p = p;
215 rate_table->s = s;
216 rate_table->k = k;
217 if (k > 32767)
218 rate_table->m = m + 1;
219 else
220 rate_table->m = m;
221 return rate_table;
222 }
223 }
224 }
225 }
226 return NULL;
227}
228
Jagan Teki7d1bf8d2023-01-30 20:27:37 +0530229static struct rockchip_pll_rate_table *
230rk3588_pll_clk_set_by_auto(unsigned long fin_hz,
231 unsigned long fout_hz)
232{
233 struct rockchip_pll_rate_table *rate_table = &rockchip_auto_table;
234 u32 p, m, s;
Elaine Zhangbb988a32023-10-12 18:18:28 +0800235 ulong fvco;
Jagan Teki7d1bf8d2023-01-30 20:27:37 +0530236
237 if (fin_hz == 0 || fout_hz == 0 || fout_hz == fin_hz)
238 return NULL;
239
240 if (fout_hz > RK3588_FOUT_MAX_HZ || fout_hz < RK3588_FOUT_MIN_HZ)
241 return NULL;
242
243 if (fin_hz / MHZ * MHZ == fin_hz && fout_hz / MHZ * MHZ == fout_hz) {
244 for (s = 0; s <= 6; s++) {
245 fvco = fout_hz << s;
246 if (fvco < RK3588_VCO_MIN_HZ ||
247 fvco > RK3588_VCO_MAX_HZ)
248 continue;
249 for (p = 2; p <= 4; p++) {
250 for (m = 64; m <= 1023; m++) {
251 if (fvco == m * fin_hz / p) {
252 rate_table->p = p;
253 rate_table->m = m;
254 rate_table->s = s;
255 rate_table->k = 0;
256 return rate_table;
257 }
258 }
259 }
260 }
261 pr_err("CANNOT FIND Fout by auto,fout = %lu\n", fout_hz);
262 } else {
Elaine Zhangbb988a32023-10-12 18:18:28 +0800263 rate_table = rockchip_rk3588_pll_frac_by_auto(fin_hz, fout_hz);
264 if (!rate_table)
265 pr_err("CANNOT FIND Fout by auto,fout = %lu\n",
266 fout_hz);
267 else
268 return rate_table;
Jagan Teki7d1bf8d2023-01-30 20:27:37 +0530269 }
270 return NULL;
271}
272
Elaine Zhanga8a2ca82019-10-25 09:42:17 +0800273static const struct rockchip_pll_rate_table *
274rockchip_get_pll_settings(struct rockchip_pll_clock *pll, ulong rate)
275{
276 struct rockchip_pll_rate_table *rate_table = pll->rate_table;
277
278 while (rate_table->rate) {
279 if (rate_table->rate == rate)
280 break;
281 rate_table++;
282 }
Jagan Teki7d1bf8d2023-01-30 20:27:37 +0530283 if (rate_table->rate != rate) {
284 if (pll->type == pll_rk3588)
285 return rk3588_pll_clk_set_by_auto(24 * MHZ, rate);
286 else
287 return rockchip_pll_clk_set_by_auto(24 * MHZ, rate);
288 } else {
Elaine Zhanga8a2ca82019-10-25 09:42:17 +0800289 return rate_table;
Jagan Teki7d1bf8d2023-01-30 20:27:37 +0530290 }
Elaine Zhanga8a2ca82019-10-25 09:42:17 +0800291}
292
293static int rk3036_pll_set_rate(struct rockchip_pll_clock *pll,
294 void __iomem *base, ulong pll_id,
295 ulong drate)
296{
297 const struct rockchip_pll_rate_table *rate;
298
299 rate = rockchip_get_pll_settings(pll, drate);
300 if (!rate) {
301 printf("%s unsupport rate\n", __func__);
302 return -EINVAL;
303 }
304
305 debug("%s: rate settings for %lu fbdiv: %d, postdiv1: %d, refdiv: %d\n",
306 __func__, rate->rate, rate->fbdiv, rate->postdiv1, rate->refdiv);
307 debug("%s: rate settings for %lu postdiv2: %d, dsmpd: %d, frac: %d\n",
308 __func__, rate->rate, rate->postdiv2, rate->dsmpd, rate->frac);
309
310 /*
311 * When power on or changing PLL setting,
312 * we must force PLL into slow mode to ensure output stable clock.
313 */
314 rk_clrsetreg(base + pll->mode_offset,
315 pll->mode_mask << pll->mode_shift,
316 RKCLK_PLL_MODE_SLOW << pll->mode_shift);
317
318 /* Power down */
319 rk_setreg(base + pll->con_offset + 0x4,
Michal Suchanek45783492022-09-28 12:41:29 +0200320 1 << RK3036_PLLCON1_PWRDOWN_SHIFT);
Elaine Zhanga8a2ca82019-10-25 09:42:17 +0800321
322 rk_clrsetreg(base + pll->con_offset,
323 (RK3036_PLLCON0_POSTDIV1_MASK |
324 RK3036_PLLCON0_FBDIV_MASK),
325 (rate->postdiv1 << RK3036_PLLCON0_POSTDIV1_SHIFT) |
326 rate->fbdiv);
327 rk_clrsetreg(base + pll->con_offset + 0x4,
328 (RK3036_PLLCON1_POSTDIV2_MASK |
329 RK3036_PLLCON1_REFDIV_MASK),
330 (rate->postdiv2 << RK3036_PLLCON1_POSTDIV2_SHIFT |
331 rate->refdiv << RK3036_PLLCON1_REFDIV_SHIFT));
332 if (!rate->dsmpd) {
333 rk_clrsetreg(base + pll->con_offset + 0x4,
334 RK3036_PLLCON1_DSMPD_MASK,
335 rate->dsmpd << RK3036_PLLCON1_DSMPD_SHIFT);
336 writel((readl(base + pll->con_offset + 0x8) &
337 (~RK3036_PLLCON2_FRAC_MASK)) |
338 (rate->frac << RK3036_PLLCON2_FRAC_SHIFT),
339 base + pll->con_offset + 0x8);
340 }
341
342 /* Power Up */
343 rk_clrreg(base + pll->con_offset + 0x4,
Michal Suchanek45783492022-09-28 12:41:29 +0200344 1 << RK3036_PLLCON1_PWRDOWN_SHIFT);
Elaine Zhanga8a2ca82019-10-25 09:42:17 +0800345
346 /* waiting for pll lock */
347 while (!(readl(base + pll->con_offset + 0x4) & (1 << pll->lock_shift)))
348 udelay(1);
349
350 rk_clrsetreg(base + pll->mode_offset, pll->mode_mask << pll->mode_shift,
351 RKCLK_PLL_MODE_NORMAL << pll->mode_shift);
352 debug("PLL at %p: con0=%x con1= %x con2= %x mode= %x\n",
353 pll, readl(base + pll->con_offset),
354 readl(base + pll->con_offset + 0x4),
355 readl(base + pll->con_offset + 0x8),
356 readl(base + pll->mode_offset));
357
358 return 0;
359}
360
361static ulong rk3036_pll_get_rate(struct rockchip_pll_clock *pll,
362 void __iomem *base, ulong pll_id)
363{
364 u32 refdiv, fbdiv, postdiv1, postdiv2, dsmpd, frac;
365 u32 con = 0, shift, mask;
366 ulong rate;
367
368 con = readl(base + pll->mode_offset);
369 shift = pll->mode_shift;
370 mask = pll->mode_mask << shift;
371
372 switch ((con & mask) >> shift) {
373 case RKCLK_PLL_MODE_SLOW:
374 return OSC_HZ;
375 case RKCLK_PLL_MODE_NORMAL:
376 /* normal mode */
377 con = readl(base + pll->con_offset);
378 postdiv1 = (con & RK3036_PLLCON0_POSTDIV1_MASK) >>
379 RK3036_PLLCON0_POSTDIV1_SHIFT;
380 fbdiv = (con & RK3036_PLLCON0_FBDIV_MASK) >>
381 RK3036_PLLCON0_FBDIV_SHIFT;
382 con = readl(base + pll->con_offset + 0x4);
383 postdiv2 = (con & RK3036_PLLCON1_POSTDIV2_MASK) >>
384 RK3036_PLLCON1_POSTDIV2_SHIFT;
385 refdiv = (con & RK3036_PLLCON1_REFDIV_MASK) >>
386 RK3036_PLLCON1_REFDIV_SHIFT;
387 dsmpd = (con & RK3036_PLLCON1_DSMPD_MASK) >>
388 RK3036_PLLCON1_DSMPD_SHIFT;
389 con = readl(base + pll->con_offset + 0x8);
390 frac = (con & RK3036_PLLCON2_FRAC_MASK) >>
391 RK3036_PLLCON2_FRAC_SHIFT;
392 rate = (24 * fbdiv / (refdiv * postdiv1 * postdiv2)) * 1000000;
393 if (dsmpd == 0) {
394 u64 frac_rate = OSC_HZ * (u64)frac;
395
396 do_div(frac_rate, refdiv);
397 frac_rate >>= 24;
398 do_div(frac_rate, postdiv1);
399 do_div(frac_rate, postdiv1);
400 rate += frac_rate;
401 }
Jagan Teki7d1bf8d2023-01-30 20:27:37 +0530402 return rate;
403 case RKCLK_PLL_MODE_DEEP:
404 default:
405 return 32768;
406 }
407}
408
409#define RK3588_PLLCON(i) ((i) * 0x4)
410#define RK3588_PLLCON0_M_MASK 0x3ff << 0
411#define RK3588_PLLCON0_M_SHIFT 0
412#define RK3588_PLLCON1_P_MASK 0x3f << 0
413#define RK3588_PLLCON1_P_SHIFT 0
414#define RK3588_PLLCON1_S_MASK 0x7 << 6
415#define RK3588_PLLCON1_S_SHIFT 6
416#define RK3588_PLLCON2_K_MASK 0xffff
417#define RK3588_PLLCON2_K_SHIFT 0
418#define RK3588_PLLCON1_PWRDOWN BIT(13)
419#define RK3588_PLLCON6_LOCK_STATUS BIT(15)
420#define RK3588_B0PLL_CLKSEL_CON(i) ((i) * 0x4 + 0x50000 + 0x300)
421#define RK3588_B1PLL_CLKSEL_CON(i) ((i) * 0x4 + 0x52000 + 0x300)
422#define RK3588_LPLL_CLKSEL_CON(i) ((i) * 0x4 + 0x58000 + 0x300)
423#define RK3588_CORE_DIV_MASK 0x1f
424#define RK3588_CORE_L02_DIV_SHIFT 0
425#define RK3588_CORE_L13_DIV_SHIFT 7
426#define RK3588_CORE_B02_DIV_SHIFT 8
427#define RK3588_CORE_B13_DIV_SHIFT 0
428
429static int rk3588_pll_set_rate(struct rockchip_pll_clock *pll,
430 void __iomem *base, ulong pll_id,
431 ulong drate)
432{
433 const struct rockchip_pll_rate_table *rate;
434
435 rate = rockchip_get_pll_settings(pll, drate);
436 if (!rate) {
437 printf("%s unsupported rate\n", __func__);
438 return -EINVAL;
439 }
440
441 debug("%s: rate settings for %lu p: %d, m: %d, s: %d, k: %d\n",
442 __func__, rate->rate, rate->p, rate->m, rate->s, rate->k);
443
444 /*
445 * When power on or changing PLL setting,
446 * we must force PLL into slow mode to ensure output stable clock.
447 */
448 if (pll_id == 3)
449 rk_clrsetreg(base + 0x84c, 0x1 << 1, 0x1 << 1);
450
451 rk_clrsetreg(base + pll->mode_offset,
452 pll->mode_mask << pll->mode_shift,
453 RKCLK_PLL_MODE_SLOW << pll->mode_shift);
454 if (pll_id == 0)
455 rk_clrsetreg(base + RK3588_B0PLL_CLKSEL_CON(0),
456 pll->mode_mask << 6,
457 RKCLK_PLL_MODE_SLOW << 6);
458 else if (pll_id == 1)
459 rk_clrsetreg(base + RK3588_B1PLL_CLKSEL_CON(0),
460 pll->mode_mask << 6,
461 RKCLK_PLL_MODE_SLOW << 6);
462 else if (pll_id == 2)
463 rk_clrsetreg(base + RK3588_LPLL_CLKSEL_CON(5),
464 pll->mode_mask << 14,
465 RKCLK_PLL_MODE_SLOW << 14);
466
467 /* Power down */
468 rk_setreg(base + pll->con_offset + RK3588_PLLCON(1),
469 RK3588_PLLCON1_PWRDOWN);
470
471 rk_clrsetreg(base + pll->con_offset,
472 RK3588_PLLCON0_M_MASK,
473 (rate->m << RK3588_PLLCON0_M_SHIFT));
474 rk_clrsetreg(base + pll->con_offset + RK3588_PLLCON(1),
475 (RK3588_PLLCON1_P_MASK |
476 RK3588_PLLCON1_S_MASK),
477 (rate->p << RK3588_PLLCON1_P_SHIFT |
478 rate->s << RK3588_PLLCON1_S_SHIFT));
479 if (rate->k) {
480 rk_clrsetreg(base + pll->con_offset + RK3588_PLLCON(2),
481 RK3588_PLLCON2_K_MASK,
482 rate->k << RK3588_PLLCON2_K_SHIFT);
483 }
484 /* Power up */
485 rk_clrreg(base + pll->con_offset + RK3588_PLLCON(1),
486 RK3588_PLLCON1_PWRDOWN);
487
488 /* waiting for pll lock */
489 while (!(readl(base + pll->con_offset + RK3588_PLLCON(6)) &
490 RK3588_PLLCON6_LOCK_STATUS)) {
491 udelay(1);
492 debug("%s: wait pll lock, pll_id=%ld\n", __func__, pll_id);
493 }
494
495 rk_clrsetreg(base + pll->mode_offset, pll->mode_mask << pll->mode_shift,
496 RKCLK_PLL_MODE_NORMAL << pll->mode_shift);
497 if (pll_id == 0) {
498 rk_clrsetreg(base + RK3588_B0PLL_CLKSEL_CON(0),
499 pll->mode_mask << 6,
500 2 << 6);
501 rk_clrsetreg(base + RK3588_B0PLL_CLKSEL_CON(0),
502 RK3588_CORE_DIV_MASK << RK3588_CORE_B02_DIV_SHIFT,
503 0 << RK3588_CORE_B02_DIV_SHIFT);
504 rk_clrsetreg(base + RK3588_B0PLL_CLKSEL_CON(1),
505 RK3588_CORE_DIV_MASK << RK3588_CORE_B13_DIV_SHIFT,
506 0 << RK3588_CORE_B13_DIV_SHIFT);
507 } else if (pll_id == 1) {
508 rk_clrsetreg(base + RK3588_B1PLL_CLKSEL_CON(0),
509 pll->mode_mask << 6,
510 2 << 6);
511 rk_clrsetreg(base + RK3588_B1PLL_CLKSEL_CON(0),
512 RK3588_CORE_DIV_MASK << RK3588_CORE_B02_DIV_SHIFT,
513 0 << RK3588_CORE_B02_DIV_SHIFT);
514 rk_clrsetreg(base + RK3588_B1PLL_CLKSEL_CON(1),
515 RK3588_CORE_DIV_MASK << RK3588_CORE_B13_DIV_SHIFT,
516 0 << RK3588_CORE_B13_DIV_SHIFT);
517 } else if (pll_id == 2) {
518 rk_clrsetreg(base + RK3588_LPLL_CLKSEL_CON(5),
519 pll->mode_mask << 14,
520 2 << 14);
521 rk_clrsetreg(base + RK3588_LPLL_CLKSEL_CON(6),
522 RK3588_CORE_DIV_MASK << RK3588_CORE_L13_DIV_SHIFT,
523 0 << RK3588_CORE_L13_DIV_SHIFT);
524 rk_clrsetreg(base + RK3588_LPLL_CLKSEL_CON(6),
525 RK3588_CORE_DIV_MASK << RK3588_CORE_L02_DIV_SHIFT,
526 0 << RK3588_CORE_L02_DIV_SHIFT);
527 rk_clrsetreg(base + RK3588_LPLL_CLKSEL_CON(7),
528 RK3588_CORE_DIV_MASK << RK3588_CORE_L13_DIV_SHIFT,
529 0 << RK3588_CORE_L13_DIV_SHIFT);
530 rk_clrsetreg(base + RK3588_LPLL_CLKSEL_CON(7),
531 RK3588_CORE_DIV_MASK << RK3588_CORE_L02_DIV_SHIFT,
532 0 << RK3588_CORE_L02_DIV_SHIFT);
533 }
534
535 if (pll_id == 3)
536 rk_clrsetreg(base + 0x84c, 0x1 << 1, 0);
537
538 debug("PLL at %p: con0=%x con1= %x con2= %x mode= %x\n",
539 pll, readl(base + pll->con_offset),
540 readl(base + pll->con_offset + 0x4),
541 readl(base + pll->con_offset + 0x8),
542 readl(base + pll->mode_offset));
543
544 return 0;
545}
546
547static ulong rk3588_pll_get_rate(struct rockchip_pll_clock *pll,
548 void __iomem *base, ulong pll_id)
549{
550 u32 m, p, s, k;
551 u32 con = 0, shift, mode;
552 u64 rate, postdiv;
553
554 con = readl(base + pll->mode_offset);
555 shift = pll->mode_shift;
556 if (pll_id == 8)
557 mode = RKCLK_PLL_MODE_NORMAL;
558 else
559 mode = (con & (pll->mode_mask << shift)) >> shift;
560 switch (mode) {
561 case RKCLK_PLL_MODE_SLOW:
562 return OSC_HZ;
563 case RKCLK_PLL_MODE_NORMAL:
564 /* normal mode */
565 con = readl(base + pll->con_offset);
566 m = (con & RK3588_PLLCON0_M_MASK) >>
567 RK3588_PLLCON0_M_SHIFT;
568 con = readl(base + pll->con_offset + RK3588_PLLCON(1));
569 p = (con & RK3588_PLLCON1_P_MASK) >>
570 RK3036_PLLCON0_FBDIV_SHIFT;
571 s = (con & RK3588_PLLCON1_S_MASK) >>
572 RK3588_PLLCON1_S_SHIFT;
573 con = readl(base + pll->con_offset + RK3588_PLLCON(2));
574 k = (con & RK3588_PLLCON2_K_MASK) >>
575 RK3588_PLLCON2_K_SHIFT;
576
577 rate = OSC_HZ / p;
578 rate *= m;
Elaine Zhangbb988a32023-10-12 18:18:28 +0800579 if (k & BIT(15)) {
580 /* fractional mode */
581 u64 frac_rate64;
582
583 k = (~(k - 1)) & RK3588_PLLCON2_K_MASK;
584 frac_rate64 = OSC_HZ * k;
585 postdiv = p;
586 postdiv *= 65536;
587 do_div(frac_rate64, postdiv);
588 rate -= frac_rate64;
589 } else {
Jagan Teki7d1bf8d2023-01-30 20:27:37 +0530590 /* fractional mode */
591 u64 frac_rate64 = OSC_HZ * k;
592
Elaine Zhangbb988a32023-10-12 18:18:28 +0800593 postdiv = p;
594 postdiv *= 65536;
Jagan Teki7d1bf8d2023-01-30 20:27:37 +0530595 do_div(frac_rate64, postdiv);
596 rate += frac_rate64;
597 }
598 rate = rate >> s;
Elaine Zhanga8a2ca82019-10-25 09:42:17 +0800599 return rate;
600 case RKCLK_PLL_MODE_DEEP:
601 default:
602 return 32768;
603 }
604}
605
606ulong rockchip_pll_get_rate(struct rockchip_pll_clock *pll,
607 void __iomem *base,
608 ulong pll_id)
609{
610 ulong rate = 0;
611
612 switch (pll->type) {
613 case pll_rk3036:
614 pll->mode_mask = PLL_MODE_MASK;
615 rate = rk3036_pll_get_rate(pll, base, pll_id);
616 break;
617 case pll_rk3328:
618 pll->mode_mask = PLL_RK3328_MODE_MASK;
619 rate = rk3036_pll_get_rate(pll, base, pll_id);
620 break;
Jagan Teki7d1bf8d2023-01-30 20:27:37 +0530621 case pll_rk3588:
622 pll->mode_mask = PLL_MODE_MASK;
623 rate = rk3588_pll_get_rate(pll, base, pll_id);
624 break;
Elaine Zhanga8a2ca82019-10-25 09:42:17 +0800625 default:
626 printf("%s: Unknown pll type for pll clk %ld\n",
627 __func__, pll_id);
628 }
629 return rate;
630}
631
632int rockchip_pll_set_rate(struct rockchip_pll_clock *pll,
633 void __iomem *base, ulong pll_id,
634 ulong drate)
635{
636 int ret = 0;
637
638 if (rockchip_pll_get_rate(pll, base, pll_id) == drate)
639 return 0;
640
641 switch (pll->type) {
642 case pll_rk3036:
643 pll->mode_mask = PLL_MODE_MASK;
644 ret = rk3036_pll_set_rate(pll, base, pll_id, drate);
645 break;
646 case pll_rk3328:
647 pll->mode_mask = PLL_RK3328_MODE_MASK;
648 ret = rk3036_pll_set_rate(pll, base, pll_id, drate);
649 break;
Jagan Teki7d1bf8d2023-01-30 20:27:37 +0530650 case pll_rk3588:
651 pll->mode_mask = PLL_MODE_MASK;
652 ret = rk3588_pll_set_rate(pll, base, pll_id, drate);
653 break;
Elaine Zhanga8a2ca82019-10-25 09:42:17 +0800654 default:
655 printf("%s: Unknown pll type for pll clk %ld\n",
656 __func__, pll_id);
657 }
658 return ret;
659}
660
661const struct rockchip_cpu_rate_table *
662rockchip_get_cpu_settings(struct rockchip_cpu_rate_table *cpu_table,
663 ulong rate)
664{
665 struct rockchip_cpu_rate_table *ps = cpu_table;
666
667 while (ps->rate) {
668 if (ps->rate == rate)
669 break;
670 ps++;
671 }
672 if (ps->rate != rate)
673 return NULL;
674 else
675 return ps;
676}