1 // SPDX-License-Identifier: GPL-2.0
3 * (C) Copyright 2015 Google, Inc
7 #include <clk-uclass.h>
10 #include <dt-structs.h>
16 #include <asm/global_data.h>
17 #include <asm/arch-rockchip/clock.h>
18 #include <asm/arch-rockchip/cru.h>
19 #include <asm/arch-rockchip/grf_rk3288.h>
20 #include <asm/arch-rockchip/hardware.h>
21 #include <dt-bindings/clock/rk3288-cru.h>
22 #include <dm/device-internal.h>
24 #include <dm/uclass-internal.h>
25 #include <linux/bitops.h>
26 #include <linux/delay.h>
27 #include <linux/err.h>
28 #include <linux/log2.h>
29 #include <linux/stringify.h>
31 DECLARE_GLOBAL_DATA_PTR;
33 struct rk3288_clk_plat {
34 #if CONFIG_IS_ENABLED(OF_PLATDATA)
35 struct dtd_rockchip_rk3288_cru dtd;
46 VCO_MAX_HZ = 2200U * 1000000,
47 VCO_MIN_HZ = 440 * 1000000,
48 OUTPUT_MAX_HZ = 2200U * 1000000,
49 OUTPUT_MIN_HZ = 27500000,
50 FREF_MAX_HZ = 2200U * 1000000,
51 FREF_MIN_HZ = 269 * 1000,
62 PLL_BWADJ_MASK = 0x0fff,
68 CORE_SEL_PLL_SHIFT = 15,
69 CORE_SEL_PLL_MASK = 1 << CORE_SEL_PLL_SHIFT,
71 A17_DIV_MASK = 0x1f << A17_DIV_SHIFT,
73 MP_DIV_MASK = 0xf << MP_DIV_SHIFT,
75 M0_DIV_MASK = 0xf << M0_DIV_SHIFT,
77 /* CLKSEL1: pd bus clk pll sel: codec or general */
78 PD_BUS_SEL_PLL_MASK = 15,
82 /* pd bus pclk div: pclk = pd_bus_aclk /(div + 1) */
83 PD_BUS_PCLK_DIV_SHIFT = 12,
84 PD_BUS_PCLK_DIV_MASK = 7 << PD_BUS_PCLK_DIV_SHIFT,
86 /* pd bus hclk div: aclk_bus: hclk_bus = 1:1 or 2:1 or 4:1 */
87 PD_BUS_HCLK_DIV_SHIFT = 8,
88 PD_BUS_HCLK_DIV_MASK = 3 << PD_BUS_HCLK_DIV_SHIFT,
90 /* pd bus aclk div: pd_bus_aclk = pd_bus_src_clk /(div0 * div1) */
91 PD_BUS_ACLK_DIV0_SHIFT = 3,
92 PD_BUS_ACLK_DIV0_MASK = 0x1f << PD_BUS_ACLK_DIV0_SHIFT,
93 PD_BUS_ACLK_DIV1_SHIFT = 0,
94 PD_BUS_ACLK_DIV1_MASK = 0x7 << PD_BUS_ACLK_DIV1_SHIFT,
98 * peripheral bus pclk div:
99 * aclk_bus: pclk_bus = 1:1 or 2:1 or 4:1 or 8:1
101 PERI_SEL_PLL_SHIFT = 15,
102 PERI_SEL_PLL_MASK = 1 << PERI_SEL_PLL_SHIFT,
106 PERI_PCLK_DIV_SHIFT = 12,
107 PERI_PCLK_DIV_MASK = 3 << PERI_PCLK_DIV_SHIFT,
109 /* peripheral bus hclk div: aclk_bus: hclk_bus = 1:1 or 2:1 or 4:1 */
110 PERI_HCLK_DIV_SHIFT = 8,
111 PERI_HCLK_DIV_MASK = 3 << PERI_HCLK_DIV_SHIFT,
114 * peripheral bus aclk div:
115 * aclk_periph = periph_clk_src / (peri_aclk_div_con + 1)
117 PERI_ACLK_DIV_SHIFT = 0,
118 PERI_ACLK_DIV_MASK = 0x1f << PERI_ACLK_DIV_SHIFT,
123 * clk_saradc=24MHz/(saradc_div_con+1)
125 CLK_SARADC_DIV_CON_SHIFT = 8,
126 CLK_SARADC_DIV_CON_MASK = GENMASK(15, 8),
127 CLK_SARADC_DIV_CON_WIDTH = 8,
129 SOCSTS_DPLL_LOCK = 1 << 5,
130 SOCSTS_APLL_LOCK = 1 << 6,
131 SOCSTS_CPLL_LOCK = 1 << 7,
132 SOCSTS_GPLL_LOCK = 1 << 8,
133 SOCSTS_NPLL_LOCK = 1 << 9,
136 #define DIV_TO_RATE(input_rate, div) ((input_rate) / ((div) + 1))
138 #define PLL_DIVISORS(hz, _nr, _no) {\
139 .nr = _nr, .nf = (u32)((u64)hz * _nr * _no / OSC_HZ), .no = _no};\
140 _Static_assert(((u64)hz * _nr * _no / OSC_HZ) * OSC_HZ /\
141 (_nr * _no) == hz, #hz "Hz cannot be hit with PLL "\
142 "divisors on line " __stringify(__LINE__));
144 /* Keep divisors as low as possible to reduce jitter and power usage */
145 static const struct pll_div apll_init_cfg = PLL_DIVISORS(APLL_HZ, 1, 1);
146 static const struct pll_div gpll_init_cfg = PLL_DIVISORS(GPLL_HZ, 2, 2);
147 static const struct pll_div cpll_init_cfg = PLL_DIVISORS(CPLL_HZ, 1, 2);
149 static int rkclk_set_pll(struct rockchip_cru *cru, enum rk_clk_id clk_id,
150 const struct pll_div *div)
152 int pll_id = rk_pll_id(clk_id);
153 struct rk3288_pll *pll = &cru->pll[pll_id];
154 /* All PLLs have same VCO and output frequency range restrictions. */
155 uint vco_hz = OSC_HZ / 1000 * div->nf / div->nr * 1000;
156 uint output_hz = vco_hz / div->no;
158 debug("PLL at %x: nf=%d, nr=%d, no=%d, vco=%u Hz, output=%u Hz\n",
159 (uint)pll, div->nf, div->nr, div->no, vco_hz, output_hz);
160 assert(vco_hz >= VCO_MIN_HZ && vco_hz <= VCO_MAX_HZ &&
161 output_hz >= OUTPUT_MIN_HZ && output_hz <= OUTPUT_MAX_HZ &&
162 (div->no == 1 || !(div->no % 2)));
165 rk_setreg(&pll->con3, 1 << PLL_RESET_SHIFT);
167 rk_clrsetreg(&pll->con0, CLKR_MASK | PLL_OD_MASK,
168 ((div->nr - 1) << CLKR_SHIFT) | (div->no - 1));
169 rk_clrsetreg(&pll->con1, CLKF_MASK, div->nf - 1);
170 rk_clrsetreg(&pll->con2, PLL_BWADJ_MASK, (div->nf >> 1) - 1);
174 /* return from reset */
175 rk_clrreg(&pll->con3, 1 << PLL_RESET_SHIFT);
180 static int rkclk_configure_ddr(struct rockchip_cru *cru, struct rk3288_grf *grf,
183 static const struct pll_div dpll_cfg[] = {
184 {.nf = 25, .nr = 2, .no = 1},
185 {.nf = 400, .nr = 9, .no = 2},
186 {.nf = 500, .nr = 9, .no = 2},
187 {.nf = 100, .nr = 3, .no = 1},
195 case 533000000: /* actually 533.3P MHz */
198 case 666000000: /* actually 666.6P MHz */
205 debug("Unsupported SDRAM frequency");
209 /* pll enter slow-mode */
210 rk_clrsetreg(&cru->cru_mode_con, DPLL_MODE_MASK,
211 DPLL_MODE_SLOW << DPLL_MODE_SHIFT);
213 rkclk_set_pll(cru, CLK_DDR, &dpll_cfg[cfg]);
215 /* wait for pll lock */
216 while (!(readl(&grf->soc_status[1]) & SOCSTS_DPLL_LOCK))
219 /* PLL enter normal-mode */
220 rk_clrsetreg(&cru->cru_mode_con, DPLL_MODE_MASK,
221 DPLL_MODE_NORMAL << DPLL_MODE_SHIFT);
226 #ifndef CONFIG_SPL_BUILD
227 #define VCO_MAX_KHZ 2200000
228 #define VCO_MIN_KHZ 440000
229 #define FREF_MAX_KHZ 2200000
230 #define FREF_MIN_KHZ 269
232 static int pll_para_config(ulong freq_hz, struct pll_div *div, uint *ext_div)
234 uint ref_khz = OSC_HZ / 1000, nr, nf = 0;
236 uint diff_khz, best_diff_khz;
237 const uint max_nr = 1 << 6, max_nf = 1 << 12, max_no = 1 << 4;
240 uint freq_khz = freq_hz / 1000;
243 printf("%s: the frequency can not be 0 Hz\n", __func__);
247 no = DIV_ROUND_UP(VCO_MIN_KHZ, freq_khz);
249 *ext_div = DIV_ROUND_UP(no, max_no);
250 no = DIV_ROUND_UP(no, *ext_div);
253 /* only even divisors (and 1) are supported */
255 no = DIV_ROUND_UP(no, 2) * 2;
257 vco_khz = freq_khz * no;
261 if (vco_khz < VCO_MIN_KHZ || vco_khz > VCO_MAX_KHZ || no > max_no) {
262 printf("%s: Cannot find out a supported VCO for Frequency (%luHz).\n",
269 best_diff_khz = vco_khz;
270 for (nr = 1; nr < max_nr && best_diff_khz; nr++) {
271 fref_khz = ref_khz / nr;
272 if (fref_khz < FREF_MIN_KHZ)
274 if (fref_khz > FREF_MAX_KHZ)
277 nf = vco_khz / fref_khz;
280 diff_khz = vco_khz - nf * fref_khz;
281 if (nf + 1 < max_nf && diff_khz > fref_khz / 2) {
283 diff_khz = fref_khz - diff_khz;
286 if (diff_khz >= best_diff_khz)
289 best_diff_khz = diff_khz;
294 if (best_diff_khz > 4 * 1000) {
295 printf("%s: Failed to match output frequency %lu, difference is %u Hz, exceed 4MHZ\n",
296 __func__, freq_hz, best_diff_khz * 1000);
303 static int rockchip_mac_set_clk(struct rockchip_cru *cru, uint freq)
308 * The gmac clock can be derived either from an external clock
309 * or can be generated from internally by a divider from SCLK_MAC.
311 if (readl(&cru->cru_clksel_con[21]) & RMII_EXTCLK_MASK) {
312 /* An external clock will always generate the right rate... */
315 u32 con = readl(&cru->cru_clksel_con[21]);
319 if (((con >> EMAC_PLL_SHIFT) & EMAC_PLL_MASK) ==
320 EMAC_PLL_SELECT_GENERAL)
322 else if (((con >> EMAC_PLL_SHIFT) & EMAC_PLL_MASK) ==
323 EMAC_PLL_SELECT_CODEC)
328 div = DIV_ROUND_UP(pll_rate, freq) - 1;
330 rk_clrsetreg(&cru->cru_clksel_con[21], MAC_DIV_CON_MASK,
331 div << MAC_DIV_CON_SHIFT);
333 debug("Unsupported div for gmac:%d\n", div);
335 return DIV_TO_RATE(pll_rate, div);
341 static int rockchip_vop_set_clk(struct rockchip_cru *cru, struct rk3288_grf *grf,
342 int periph, unsigned int rate_hz)
344 struct pll_div npll_config = {0};
348 ret = pll_para_config(rate_hz, &npll_config, &lcdc_div);
352 rk_clrsetreg(&cru->cru_mode_con, NPLL_MODE_MASK,
353 NPLL_MODE_SLOW << NPLL_MODE_SHIFT);
354 rkclk_set_pll(cru, CLK_NEW, &npll_config);
356 /* waiting for pll lock */
358 if (readl(&grf->soc_status[1]) & SOCSTS_NPLL_LOCK)
363 rk_clrsetreg(&cru->cru_mode_con, NPLL_MODE_MASK,
364 NPLL_MODE_NORMAL << NPLL_MODE_SHIFT);
366 /* vop dclk source clk: npll,dclk_div: 1 */
369 rk_clrsetreg(&cru->cru_clksel_con[27], 0xff << 8 | 3 << 0,
370 (lcdc_div - 1) << 8 | 2 << 0);
373 rk_clrsetreg(&cru->cru_clksel_con[29], 0xff << 8 | 3 << 6,
374 (lcdc_div - 1) << 8 | 2 << 6);
381 static u32 rockchip_clk_gcd(u32 a, u32 b)
392 static ulong rockchip_i2s_get_clk(struct rockchip_cru *cru, uint gclk_rate)
394 unsigned long long rate;
398 val = readl(&cru->cru_clksel_con[8]);
399 n = (val & I2S0_FRAC_NUMER_MASK) >> I2S0_FRAC_NUMER_SHIFT;
400 d = (val & I2S0_FRAC_DENOM_MASK) >> I2S0_FRAC_DENOM_SHIFT;
402 rate = (unsigned long long)gclk_rate * n;
408 static ulong rockchip_i2s_set_clk(struct rockchip_cru *cru, uint gclk_rate,
414 /* set frac divider */
415 v = rockchip_clk_gcd(gclk_rate, freq);
418 assert(freq == gclk_rate / n * d);
419 writel(d << I2S0_FRAC_NUMER_SHIFT | n << I2S0_FRAC_DENOM_SHIFT,
420 &cru->cru_clksel_con[8]);
422 return rockchip_i2s_get_clk(cru, gclk_rate);
424 #endif /* CONFIG_SPL_BUILD */
426 static void rkclk_init(struct rockchip_cru *cru, struct rk3288_grf *grf)
432 /* pll enter slow-mode */
433 rk_clrsetreg(&cru->cru_mode_con,
434 GPLL_MODE_MASK | CPLL_MODE_MASK,
435 GPLL_MODE_SLOW << GPLL_MODE_SHIFT |
436 CPLL_MODE_SLOW << CPLL_MODE_SHIFT);
439 rkclk_set_pll(cru, CLK_GENERAL, &gpll_init_cfg);
440 rkclk_set_pll(cru, CLK_CODEC, &cpll_init_cfg);
442 /* waiting for pll lock */
443 while ((readl(&grf->soc_status[1]) &
444 (SOCSTS_CPLL_LOCK | SOCSTS_GPLL_LOCK)) !=
445 (SOCSTS_CPLL_LOCK | SOCSTS_GPLL_LOCK))
449 * pd_bus clock pll source selection and
450 * set up dependent divisors for PCLK/HCLK and ACLK clocks.
452 aclk_div = GPLL_HZ / PD_BUS_ACLK_HZ - 1;
453 assert((aclk_div + 1) * PD_BUS_ACLK_HZ == GPLL_HZ && aclk_div < 0x1f);
454 hclk_div = PD_BUS_ACLK_HZ / PD_BUS_HCLK_HZ - 1;
455 assert((hclk_div + 1) * PD_BUS_HCLK_HZ ==
456 PD_BUS_ACLK_HZ && (hclk_div < 0x4) && (hclk_div != 0x2));
458 pclk_div = PD_BUS_ACLK_HZ / PD_BUS_PCLK_HZ - 1;
459 assert((pclk_div + 1) * PD_BUS_PCLK_HZ ==
460 PD_BUS_ACLK_HZ && pclk_div < 0x7);
462 rk_clrsetreg(&cru->cru_clksel_con[1],
463 PD_BUS_PCLK_DIV_MASK | PD_BUS_HCLK_DIV_MASK |
464 PD_BUS_ACLK_DIV0_MASK | PD_BUS_ACLK_DIV1_MASK,
465 pclk_div << PD_BUS_PCLK_DIV_SHIFT |
466 hclk_div << PD_BUS_HCLK_DIV_SHIFT |
467 aclk_div << PD_BUS_ACLK_DIV0_SHIFT |
471 * peri clock pll source selection and
472 * set up dependent divisors for PCLK/HCLK and ACLK clocks.
474 aclk_div = GPLL_HZ / PERI_ACLK_HZ - 1;
475 assert((aclk_div + 1) * PERI_ACLK_HZ == GPLL_HZ && aclk_div < 0x1f);
477 hclk_div = ilog2(PERI_ACLK_HZ / PERI_HCLK_HZ);
478 assert((1 << hclk_div) * PERI_HCLK_HZ ==
479 PERI_ACLK_HZ && (hclk_div < 0x4));
481 pclk_div = ilog2(PERI_ACLK_HZ / PERI_PCLK_HZ);
482 assert((1 << pclk_div) * PERI_PCLK_HZ ==
483 PERI_ACLK_HZ && (pclk_div < 0x4));
485 rk_clrsetreg(&cru->cru_clksel_con[10],
486 PERI_PCLK_DIV_MASK | PERI_HCLK_DIV_MASK |
488 PERI_SEL_GPLL << PERI_SEL_PLL_SHIFT |
489 pclk_div << PERI_PCLK_DIV_SHIFT |
490 hclk_div << PERI_HCLK_DIV_SHIFT |
491 aclk_div << PERI_ACLK_DIV_SHIFT);
493 /* PLL enter normal-mode */
494 rk_clrsetreg(&cru->cru_mode_con,
495 GPLL_MODE_MASK | CPLL_MODE_MASK,
496 GPLL_MODE_NORMAL << GPLL_MODE_SHIFT |
497 CPLL_MODE_NORMAL << CPLL_MODE_SHIFT);
500 void rk3288_clk_configure_cpu(struct rockchip_cru *cru, struct rk3288_grf *grf)
502 /* pll enter slow-mode */
503 rk_clrsetreg(&cru->cru_mode_con, APLL_MODE_MASK,
504 APLL_MODE_SLOW << APLL_MODE_SHIFT);
506 rkclk_set_pll(cru, CLK_ARM, &apll_init_cfg);
508 /* waiting for pll lock */
509 while (!(readl(&grf->soc_status[1]) & SOCSTS_APLL_LOCK))
513 * core clock pll source selection and
514 * set up dependent divisors for MPAXI/M0AXI and ARM clocks.
515 * core clock select apll, apll clk = 1800MHz
516 * arm clk = 1800MHz, mpclk = 450MHz, m0clk = 900MHz
518 rk_clrsetreg(&cru->cru_clksel_con[0],
519 CORE_SEL_PLL_MASK | A17_DIV_MASK | MP_DIV_MASK |
526 * set up dependent divisors for L2RAM/ATCLK and PCLK clocks.
527 * l2ramclk = 900MHz, atclk = 450MHz, pclk_dbg = 450MHz
529 rk_clrsetreg(&cru->cru_clksel_con[37],
530 CLK_L2RAM_DIV_MASK | ATCLK_CORE_DIV_CON_MASK |
531 PCLK_CORE_DBG_DIV_MASK,
532 1 << CLK_L2RAM_DIV_SHIFT |
533 3 << ATCLK_CORE_DIV_CON_SHIFT |
534 3 << PCLK_CORE_DBG_DIV_SHIFT);
536 /* PLL enter normal-mode */
537 rk_clrsetreg(&cru->cru_mode_con, APLL_MODE_MASK,
538 APLL_MODE_NORMAL << APLL_MODE_SHIFT);
541 /* Get pll rate by id */
542 static uint32_t rkclk_pll_get_rate(struct rockchip_cru *cru,
543 enum rk_clk_id clk_id)
547 int pll_id = rk_pll_id(clk_id);
548 struct rk3288_pll *pll = &cru->pll[pll_id];
549 static u8 clk_shift[CLK_COUNT] = {
550 0xff, APLL_MODE_SHIFT, DPLL_MODE_SHIFT, CPLL_MODE_SHIFT,
551 GPLL_MODE_SHIFT, NPLL_MODE_SHIFT
555 con = readl(&cru->cru_mode_con);
556 shift = clk_shift[clk_id];
557 switch ((con >> shift) & CRU_MODE_MASK) {
560 case APLL_MODE_NORMAL:
562 con = readl(&pll->con0);
563 no = ((con & CLKOD_MASK) >> CLKOD_SHIFT) + 1;
564 nr = ((con & CLKR_MASK) >> CLKR_SHIFT) + 1;
565 con = readl(&pll->con1);
566 nf = ((con & CLKF_MASK) >> CLKF_SHIFT) + 1;
568 return (24 * nf / (nr * no)) * 1000000;
575 static ulong rockchip_mmc_get_clk(struct rockchip_cru *cru, uint gclk_rate,
585 con = readl(&cru->cru_clksel_con[12]);
586 mux = (con & EMMC_PLL_MASK) >> EMMC_PLL_SHIFT;
587 div = (con & EMMC_DIV_MASK) >> EMMC_DIV_SHIFT;
591 con = readl(&cru->cru_clksel_con[11]);
592 mux = (con & MMC0_PLL_MASK) >> MMC0_PLL_SHIFT;
593 div = (con & MMC0_DIV_MASK) >> MMC0_DIV_SHIFT;
597 con = readl(&cru->cru_clksel_con[12]);
598 mux = (con & SDIO0_PLL_MASK) >> SDIO0_PLL_SHIFT;
599 div = (con & SDIO0_DIV_MASK) >> SDIO0_DIV_SHIFT;
605 src_rate = mux == EMMC_PLL_SELECT_24MHZ ? OSC_HZ : gclk_rate;
606 return DIV_TO_RATE(src_rate, div);
609 static ulong rockchip_mmc_set_clk(struct rockchip_cru *cru, uint gclk_rate,
610 int periph, uint freq)
615 debug("%s: gclk_rate=%u\n", __func__, gclk_rate);
616 /* mmc clock default div 2 internal, need provide double in cru */
617 src_clk_div = DIV_ROUND_UP(gclk_rate / 2, freq);
619 if (src_clk_div > 0x3f) {
620 src_clk_div = DIV_ROUND_UP(OSC_HZ / 2, freq);
621 assert(src_clk_div < 0x40);
622 mux = EMMC_PLL_SELECT_24MHZ;
623 assert((int)EMMC_PLL_SELECT_24MHZ ==
624 (int)MMC0_PLL_SELECT_24MHZ);
626 mux = EMMC_PLL_SELECT_GENERAL;
627 assert((int)EMMC_PLL_SELECT_GENERAL ==
628 (int)MMC0_PLL_SELECT_GENERAL);
633 rk_clrsetreg(&cru->cru_clksel_con[12],
634 EMMC_PLL_MASK | EMMC_DIV_MASK,
635 mux << EMMC_PLL_SHIFT |
636 (src_clk_div - 1) << EMMC_DIV_SHIFT);
640 rk_clrsetreg(&cru->cru_clksel_con[11],
641 MMC0_PLL_MASK | MMC0_DIV_MASK,
642 mux << MMC0_PLL_SHIFT |
643 (src_clk_div - 1) << MMC0_DIV_SHIFT);
647 rk_clrsetreg(&cru->cru_clksel_con[12],
648 SDIO0_PLL_MASK | SDIO0_DIV_MASK,
649 mux << SDIO0_PLL_SHIFT |
650 (src_clk_div - 1) << SDIO0_DIV_SHIFT);
656 return rockchip_mmc_get_clk(cru, gclk_rate, periph);
659 static ulong rockchip_spi_get_clk(struct rockchip_cru *cru, uint gclk_rate,
667 con = readl(&cru->cru_clksel_con[25]);
668 mux = (con & SPI0_PLL_MASK) >> SPI0_PLL_SHIFT;
669 div = (con & SPI0_DIV_MASK) >> SPI0_DIV_SHIFT;
672 con = readl(&cru->cru_clksel_con[25]);
673 mux = (con & SPI1_PLL_MASK) >> SPI1_PLL_SHIFT;
674 div = (con & SPI1_DIV_MASK) >> SPI1_DIV_SHIFT;
677 con = readl(&cru->cru_clksel_con[39]);
678 mux = (con & SPI2_PLL_MASK) >> SPI2_PLL_SHIFT;
679 div = (con & SPI2_DIV_MASK) >> SPI2_DIV_SHIFT;
684 assert(mux == SPI0_PLL_SELECT_GENERAL);
686 return DIV_TO_RATE(gclk_rate, div);
689 static ulong rockchip_spi_set_clk(struct rockchip_cru *cru, uint gclk_rate,
690 int periph, uint freq)
694 debug("%s: clk_general_rate=%u\n", __func__, gclk_rate);
695 src_clk_div = DIV_ROUND_UP(gclk_rate, freq) - 1;
696 assert(src_clk_div < 128);
699 rk_clrsetreg(&cru->cru_clksel_con[25],
700 SPI0_PLL_MASK | SPI0_DIV_MASK,
701 SPI0_PLL_SELECT_GENERAL << SPI0_PLL_SHIFT |
702 src_clk_div << SPI0_DIV_SHIFT);
705 rk_clrsetreg(&cru->cru_clksel_con[25],
706 SPI1_PLL_MASK | SPI1_DIV_MASK,
707 SPI1_PLL_SELECT_GENERAL << SPI1_PLL_SHIFT |
708 src_clk_div << SPI1_DIV_SHIFT);
711 rk_clrsetreg(&cru->cru_clksel_con[39],
712 SPI2_PLL_MASK | SPI2_DIV_MASK,
713 SPI2_PLL_SELECT_GENERAL << SPI2_PLL_SHIFT |
714 src_clk_div << SPI2_DIV_SHIFT);
720 return rockchip_spi_get_clk(cru, gclk_rate, periph);
723 static ulong rockchip_saradc_get_clk(struct rockchip_cru *cru)
727 val = readl(&cru->cru_clksel_con[24]);
728 div = bitfield_extract(val, CLK_SARADC_DIV_CON_SHIFT,
729 CLK_SARADC_DIV_CON_WIDTH);
731 return DIV_TO_RATE(OSC_HZ, div);
734 static ulong rockchip_saradc_set_clk(struct rockchip_cru *cru, uint hz)
738 src_clk_div = DIV_ROUND_UP(OSC_HZ, hz) - 1;
739 assert(src_clk_div < 128);
741 rk_clrsetreg(&cru->cru_clksel_con[24],
742 CLK_SARADC_DIV_CON_MASK,
743 src_clk_div << CLK_SARADC_DIV_CON_SHIFT);
745 return rockchip_saradc_get_clk(cru);
748 static ulong rk3288_clk_get_rate(struct clk *clk)
750 struct rk3288_clk_priv *priv = dev_get_priv(clk->dev);
751 ulong new_rate, gclk_rate;
753 gclk_rate = rkclk_pll_get_rate(priv->cru, CLK_GENERAL);
756 new_rate = rkclk_pll_get_rate(priv->cru, clk->id);
764 new_rate = rockchip_mmc_get_clk(priv->cru, gclk_rate, clk->id);
769 new_rate = rockchip_spi_get_clk(priv->cru, gclk_rate, clk->id);
780 return PD_BUS_PCLK_HZ;
782 new_rate = rockchip_saradc_get_clk(priv->cru);
791 static ulong rk3288_clk_set_rate(struct clk *clk, ulong rate)
793 struct rk3288_clk_priv *priv = dev_get_priv(clk->dev);
794 struct rockchip_cru *cru = priv->cru;
795 ulong new_rate, gclk_rate;
797 gclk_rate = rkclk_pll_get_rate(priv->cru, CLK_GENERAL);
800 /* We only support a fixed rate here */
801 if (rate != 1800000000)
803 rk3288_clk_configure_cpu(priv->cru, priv->grf);
807 new_rate = rkclk_configure_ddr(priv->cru, priv->grf, rate);
815 new_rate = rockchip_mmc_set_clk(cru, gclk_rate, clk->id, rate);
820 new_rate = rockchip_spi_set_clk(cru, gclk_rate, clk->id, rate);
822 #ifndef CONFIG_SPL_BUILD
824 new_rate = rockchip_i2s_set_clk(cru, gclk_rate, rate);
827 new_rate = rockchip_mac_set_clk(priv->cru, rate);
831 new_rate = rockchip_vop_set_clk(cru, priv->grf, clk->id, rate);
834 /* clk_edp_24M source: 24M */
835 rk_setreg(&cru->cru_clksel_con[28], 1 << 15);
838 rk_setreg(&cru->cru_clksel_con[6], 1 << 15);
840 rk_clrreg(&cru->cru_clksel_con[6], 1 << 15);
847 /* vop aclk source clk: cpll */
848 div = CPLL_HZ / rate;
849 assert((div - 1 < 64) && (div * rate == CPLL_HZ));
853 rk_clrsetreg(&cru->cru_clksel_con[31],
855 0 << 6 | (div - 1) << 0);
858 rk_clrsetreg(&cru->cru_clksel_con[31],
860 0 << 14 | (div - 1) << 8);
867 /* enable pclk hdmi ctrl */
868 rk_clrreg(&cru->cru_clkgate_con[16], 1 << 9);
870 /* software reset hdmi */
871 rk_setreg(&cru->cru_clkgate_con[7], 1 << 9);
873 rk_clrreg(&cru->cru_clkgate_con[7], 1 << 9);
878 new_rate = rockchip_saradc_set_clk(priv->cru, rate);
898 static int __maybe_unused rk3288_gmac_set_parent(struct clk *clk, struct clk *parent)
900 struct rk3288_clk_priv *priv = dev_get_priv(clk->dev);
901 struct rockchip_cru *cru = priv->cru;
902 const char *clock_output_name;
906 * If the requested parent is in the same clock-controller and
907 * the id is SCLK_MAC_PLL ("mac_pll_src"), switch to the internal
910 if ((parent->dev == clk->dev) && (parent->id == SCLK_MAC_PLL)) {
911 debug("%s: switching GAMC to SCLK_MAC_PLL\n", __func__);
912 rk_clrsetreg(&cru->cru_clksel_con[21], RMII_EXTCLK_MASK, 0);
917 * Otherwise, we need to check the clock-output-names of the
918 * requested parent to see if the requested id is "ext_gmac".
920 ret = dev_read_string_index(parent->dev, "clock-output-names",
921 parent->id, &clock_output_name);
925 /* If this is "ext_gmac", switch to the external clock input */
926 if (!strcmp(clock_output_name, "ext_gmac")) {
927 debug("%s: switching GMAC to external clock\n", __func__);
928 rk_clrsetreg(&cru->cru_clksel_con[21], RMII_EXTCLK_MASK,
929 RMII_EXTCLK_SELECT_EXT_CLK << RMII_EXTCLK_SHIFT);
936 static int __maybe_unused rk3288_clk_set_parent(struct clk *clk, struct clk *parent)
940 return rk3288_gmac_set_parent(clk, parent);
941 case SCLK_USBPHY480M_SRC:
945 debug("%s: unsupported clk %ld\n", __func__, clk->id);
949 static struct clk_ops rk3288_clk_ops = {
950 .get_rate = rk3288_clk_get_rate,
951 .set_rate = rk3288_clk_set_rate,
952 #if CONFIG_IS_ENABLED(OF_REAL)
953 .set_parent = rk3288_clk_set_parent,
957 static int rk3288_clk_of_to_plat(struct udevice *dev)
959 if (CONFIG_IS_ENABLED(OF_REAL)) {
960 struct rk3288_clk_priv *priv = dev_get_priv(dev);
962 priv->cru = dev_read_addr_ptr(dev);
968 static int rk3288_clk_probe(struct udevice *dev)
970 struct rk3288_clk_priv *priv = dev_get_priv(dev);
971 bool init_clocks = false;
973 priv->grf = syscon_get_first_range(ROCKCHIP_SYSCON_GRF);
974 if (IS_ERR(priv->grf))
975 return PTR_ERR(priv->grf);
976 #ifdef CONFIG_SPL_BUILD
977 #if CONFIG_IS_ENABLED(OF_PLATDATA)
978 struct rk3288_clk_plat *plat = dev_get_plat(dev);
980 priv->cru = map_sysmem(plat->dtd.reg[0], plat->dtd.reg[1]);
984 if (!(gd->flags & GD_FLG_RELOC)) {
988 * Init clocks in U-Boot proper if the NPLL is runnning. This
989 * indicates that a previous boot loader set up the clocks, so
990 * we need to redo it. U-Boot's SPL does not set this clock.
992 reg = readl(&priv->cru->cru_mode_con);
993 if (((reg & NPLL_MODE_MASK) >> NPLL_MODE_SHIFT) ==
999 rkclk_init(priv->cru, priv->grf);
1004 static int rk3288_clk_bind(struct udevice *dev)
1007 struct udevice *sys_child;
1008 struct sysreset_reg *priv;
1010 /* The reset driver does not have a device node, so bind it here */
1011 ret = device_bind_driver(dev, "rockchip_sysreset", "sysreset",
1014 debug("Warning: No sysreset driver: ret=%d\n", ret);
1016 priv = malloc(sizeof(struct sysreset_reg));
1017 priv->glb_srst_fst_value = offsetof(struct rockchip_cru,
1018 cru_glb_srst_fst_value);
1019 priv->glb_srst_snd_value = offsetof(struct rockchip_cru,
1020 cru_glb_srst_snd_value);
1021 dev_set_priv(sys_child, priv);
1024 #if CONFIG_IS_ENABLED(RESET_ROCKCHIP)
1025 ret = offsetof(struct rockchip_cru, cru_softrst_con[0]);
1026 ret = rockchip_reset_bind(dev, ret, 12);
1028 debug("Warning: software reset driver bind failed\n");
1034 static const struct udevice_id rk3288_clk_ids[] = {
1035 { .compatible = "rockchip,rk3288-cru" },
1039 U_BOOT_DRIVER(rockchip_rk3288_cru) = {
1040 .name = "rockchip_rk3288_cru",
1042 .of_match = rk3288_clk_ids,
1043 .priv_auto = sizeof(struct rk3288_clk_priv),
1044 .plat_auto = sizeof(struct rk3288_clk_plat),
1045 .ops = &rk3288_clk_ops,
1046 .bind = rk3288_clk_bind,
1047 .of_to_plat = rk3288_clk_of_to_plat,
1048 .probe = rk3288_clk_probe,