1 // SPDX-License-Identifier: GPL-2.0
3 * (C) Copyright 2017-2019 Rockchip Electronics Co., Ltd
7 #include <clk-uclass.h>
14 #include <asm/global_data.h>
16 #include <asm/arch/cru_rk3308.h>
17 #include <asm/arch-rockchip/clock.h>
18 #include <asm/arch-rockchip/hardware.h>
19 #include <dm/device-internal.h>
21 #include <dt-bindings/clock/rk3308-cru.h>
22 #include <linux/bitops.h>
24 DECLARE_GLOBAL_DATA_PTR;
27 VCO_MAX_HZ = 3200U * 1000000,
28 VCO_MIN_HZ = 800 * 1000000,
29 OUTPUT_MAX_HZ = 3200U * 1000000,
30 OUTPUT_MIN_HZ = 24 * 1000000,
33 #define DIV_TO_RATE(input_rate, div) ((input_rate) / ((div) + 1))
35 #define RK3308_CPUCLK_RATE(_rate, _aclk_div, _pclk_div) \
38 .aclk_div = _aclk_div, \
39 .pclk_div = _pclk_div, \
42 static struct rockchip_pll_rate_table rk3308_pll_rates[] = {
43 /* _mhz, _refdiv, _fbdiv, _postdiv1, _postdiv2, _dsmpd, _frac */
44 RK3036_PLL_RATE(1300000000, 6, 325, 1, 1, 1, 0),
45 RK3036_PLL_RATE(1200000000, 1, 50, 1, 1, 1, 0),
46 RK3036_PLL_RATE(816000000, 1, 68, 2, 1, 1, 0),
47 RK3036_PLL_RATE(748000000, 2, 187, 3, 1, 1, 0),
50 static struct rockchip_cpu_rate_table rk3308_cpu_rates[] = {
51 RK3308_CPUCLK_RATE(1200000000, 1, 5),
52 RK3308_CPUCLK_RATE(1008000000, 1, 5),
53 RK3308_CPUCLK_RATE(816000000, 1, 3),
54 RK3308_CPUCLK_RATE(600000000, 1, 3),
55 RK3308_CPUCLK_RATE(408000000, 1, 1),
58 static struct rockchip_pll_clock rk3308_pll_clks[] = {
59 [APLL] = PLL(pll_rk3328, PLL_APLL, RK3308_PLL_CON(0),
60 RK3308_MODE_CON, 0, 10, 0, rk3308_pll_rates),
61 [DPLL] = PLL(pll_rk3328, PLL_DPLL, RK3308_PLL_CON(8),
62 RK3308_MODE_CON, 2, 10, 0, NULL),
63 [VPLL0] = PLL(pll_rk3328, PLL_VPLL0, RK3308_PLL_CON(16),
64 RK3308_MODE_CON, 4, 10, 0, NULL),
65 [VPLL1] = PLL(pll_rk3328, PLL_VPLL1, RK3308_PLL_CON(24),
66 RK3308_MODE_CON, 6, 10, 0, NULL),
69 static ulong rk3308_armclk_set_clk(struct rk3308_clk_priv *priv, ulong hz)
71 struct rk3308_cru *cru = priv->cru;
72 const struct rockchip_cpu_rate_table *rate;
75 rate = rockchip_get_cpu_settings(rk3308_cpu_rates, hz);
77 printf("%s unsupport rate\n", __func__);
82 * select apll as cpu/core clock pll source and
83 * set up dependent divisors for PERI and ACLK clocks.
84 * core hz : apll = 1:1
86 old_rate = rockchip_pll_get_rate(&rk3308_pll_clks[APLL],
89 if (rockchip_pll_set_rate(&rk3308_pll_clks[APLL],
92 rk_clrsetreg(&cru->clksel_con[0],
93 CORE_CLK_PLL_SEL_MASK | CORE_DIV_CON_MASK |
94 CORE_ACLK_DIV_MASK | CORE_DBG_DIV_MASK,
95 rate->aclk_div << CORE_ACLK_DIV_SHIFT |
96 rate->pclk_div << CORE_DBG_DIV_SHIFT |
97 CORE_CLK_PLL_SEL_APLL << CORE_CLK_PLL_SEL_SHIFT |
98 0 << CORE_DIV_CON_SHIFT);
99 } else if (old_rate < hz) {
100 rk_clrsetreg(&cru->clksel_con[0],
101 CORE_CLK_PLL_SEL_MASK | CORE_DIV_CON_MASK |
102 CORE_ACLK_DIV_MASK | CORE_DBG_DIV_MASK,
103 rate->aclk_div << CORE_ACLK_DIV_SHIFT |
104 rate->pclk_div << CORE_DBG_DIV_SHIFT |
105 CORE_CLK_PLL_SEL_APLL << CORE_CLK_PLL_SEL_SHIFT |
106 0 << CORE_DIV_CON_SHIFT);
107 if (rockchip_pll_set_rate(&rk3308_pll_clks[APLL],
108 priv->cru, APLL, hz))
112 return rockchip_pll_get_rate(&rk3308_pll_clks[APLL], priv->cru, APLL);
115 static void rk3308_clk_get_pll_rate(struct rk3308_clk_priv *priv)
118 priv->dpll_hz = rockchip_pll_get_rate(&rk3308_pll_clks[DPLL],
121 priv->vpll0_hz = rockchip_pll_get_rate(&rk3308_pll_clks[VPLL0],
124 priv->vpll1_hz = rockchip_pll_get_rate(&rk3308_pll_clks[VPLL1],
128 static ulong rk3308_i2c_get_clk(struct clk *clk)
130 struct rk3308_clk_priv *priv = dev_get_priv(clk->dev);
131 struct rk3308_cru *cru = priv->cru;
132 u32 div, con, con_id;
148 printf("do not support this i2c bus\n");
152 con = readl(&cru->clksel_con[con_id]);
153 div = con >> CLK_I2C_DIV_CON_SHIFT & CLK_I2C_DIV_CON_MASK;
155 return DIV_TO_RATE(priv->dpll_hz, div);
158 static ulong rk3308_i2c_set_clk(struct clk *clk, uint hz)
160 struct rk3308_clk_priv *priv = dev_get_priv(clk->dev);
161 struct rk3308_cru *cru = priv->cru;
162 u32 src_clk_div, con_id;
164 src_clk_div = DIV_ROUND_UP(priv->dpll_hz, hz);
165 assert(src_clk_div - 1 <= 127);
181 printf("do not support this i2c bus\n");
184 rk_clrsetreg(&cru->clksel_con[con_id],
185 CLK_I2C_PLL_SEL_MASK | CLK_I2C_DIV_CON_MASK,
186 CLK_I2C_PLL_SEL_DPLL << CLK_I2C_PLL_SEL_SHIFT |
187 (src_clk_div - 1) << CLK_I2C_DIV_CON_SHIFT);
189 return rk3308_i2c_get_clk(clk);
192 static ulong rk3308_mac_set_clk(struct clk *clk, uint hz)
194 struct rk3308_clk_priv *priv = dev_get_priv(clk->dev);
195 struct rk3308_cru *cru = priv->cru;
196 u32 con = readl(&cru->clksel_con[43]);
200 if ((con >> MAC_PLL_SHIFT) & MAC_SEL_VPLL0)
201 pll_rate = rockchip_pll_get_rate(&rk3308_pll_clks[VPLL0],
203 else if ((con >> MAC_PLL_SHIFT) & MAC_SEL_VPLL1)
204 pll_rate = rockchip_pll_get_rate(&rk3308_pll_clks[VPLL1],
207 pll_rate = rockchip_pll_get_rate(&rk3308_pll_clks[DPLL],
210 /*default set 50MHZ for gmac*/
214 div = DIV_ROUND_UP(pll_rate, hz) - 1;
216 rk_clrsetreg(&cru->clksel_con[43], MAC_DIV_MASK,
217 div << MAC_DIV_SHIFT);
219 return DIV_TO_RATE(pll_rate, div);
222 static int rk3308_mac_set_speed_clk(struct clk *clk, uint hz)
224 struct rk3308_clk_priv *priv = dev_get_priv(clk->dev);
225 struct rk3308_cru *cru = priv->cru;
227 if (hz != 2500000 && hz != 25000000) {
228 debug("Unsupported mac speed:%d\n", hz);
232 rk_clrsetreg(&cru->clksel_con[43], MAC_CLK_SPEED_SEL_MASK,
233 ((hz == 2500000) ? 0 : 1) << MAC_CLK_SPEED_SEL_SHIFT);
238 static ulong rk3308_mmc_get_clk(struct clk *clk)
240 struct rk3308_clk_priv *priv = dev_get_priv(clk->dev);
241 struct rk3308_cru *cru = priv->cru;
242 u32 div, con, con_id;
251 case SCLK_EMMC_SAMPLE:
258 con = readl(&cru->clksel_con[con_id]);
259 div = (con & EMMC_DIV_MASK) >> EMMC_DIV_SHIFT;
261 if ((con & EMMC_PLL_MASK) >> EMMC_PLL_SHIFT
263 return DIV_TO_RATE(OSC_HZ, div) / 2;
265 return DIV_TO_RATE(priv->vpll0_hz, div) / 2;
268 static ulong rk3308_mmc_set_clk(struct clk *clk, ulong set_rate)
270 struct rk3308_clk_priv *priv = dev_get_priv(clk->dev);
271 struct rk3308_cru *cru = priv->cru;
287 /* Select clk_sdmmc/emmc source from VPLL0 by default */
288 /* mmc clock defaulg div 2 internal, need provide double in cru */
289 src_clk_div = DIV_ROUND_UP(priv->vpll0_hz / 2, set_rate);
291 if (src_clk_div > 127) {
292 /* use 24MHz source for 400KHz clock */
293 src_clk_div = DIV_ROUND_UP(OSC_HZ / 2, set_rate);
294 rk_clrsetreg(&cru->clksel_con[con_id],
295 EMMC_PLL_MASK | EMMC_DIV_MASK | EMMC_CLK_SEL_MASK,
296 EMMC_CLK_SEL_EMMC << EMMC_CLK_SEL_SHIFT |
297 EMMC_SEL_24M << EMMC_PLL_SHIFT |
298 (src_clk_div - 1) << EMMC_DIV_SHIFT);
300 rk_clrsetreg(&cru->clksel_con[con_id],
301 EMMC_PLL_MASK | EMMC_DIV_MASK | EMMC_CLK_SEL_MASK,
302 EMMC_CLK_SEL_EMMC << EMMC_CLK_SEL_SHIFT |
303 EMMC_SEL_VPLL0 << EMMC_PLL_SHIFT |
304 (src_clk_div - 1) << EMMC_DIV_SHIFT);
307 return rk3308_mmc_get_clk(clk);
310 static ulong rk3308_saradc_get_clk(struct clk *clk)
312 struct rk3308_clk_priv *priv = dev_get_priv(clk->dev);
313 struct rk3308_cru *cru = priv->cru;
316 con = readl(&cru->clksel_con[34]);
317 div = con >> CLK_SARADC_DIV_CON_SHIFT & CLK_SARADC_DIV_CON_MASK;
319 return DIV_TO_RATE(OSC_HZ, div);
322 static ulong rk3308_saradc_set_clk(struct clk *clk, uint hz)
324 struct rk3308_clk_priv *priv = dev_get_priv(clk->dev);
325 struct rk3308_cru *cru = priv->cru;
328 src_clk_div = DIV_ROUND_UP(OSC_HZ, hz);
329 assert(src_clk_div - 1 <= 2047);
331 rk_clrsetreg(&cru->clksel_con[34],
332 CLK_SARADC_DIV_CON_MASK,
333 (src_clk_div - 1) << CLK_SARADC_DIV_CON_SHIFT);
335 return rk3308_saradc_get_clk(clk);
338 static ulong rk3308_tsadc_get_clk(struct clk *clk)
340 struct rk3308_clk_priv *priv = dev_get_priv(clk->dev);
341 struct rk3308_cru *cru = priv->cru;
344 con = readl(&cru->clksel_con[33]);
345 div = con >> CLK_SARADC_DIV_CON_SHIFT & CLK_SARADC_DIV_CON_MASK;
347 return DIV_TO_RATE(OSC_HZ, div);
350 static ulong rk3308_tsadc_set_clk(struct clk *clk, uint hz)
352 struct rk3308_clk_priv *priv = dev_get_priv(clk->dev);
353 struct rk3308_cru *cru = priv->cru;
356 src_clk_div = DIV_ROUND_UP(OSC_HZ, hz);
357 assert(src_clk_div - 1 <= 2047);
359 rk_clrsetreg(&cru->clksel_con[33],
360 CLK_SARADC_DIV_CON_MASK,
361 (src_clk_div - 1) << CLK_SARADC_DIV_CON_SHIFT);
363 return rk3308_tsadc_get_clk(clk);
366 static ulong rk3308_spi_get_clk(struct clk *clk)
368 struct rk3308_clk_priv *priv = dev_get_priv(clk->dev);
369 struct rk3308_cru *cru = priv->cru;
370 u32 div, con, con_id;
383 printf("do not support this spi bus\n");
387 con = readl(&cru->clksel_con[con_id]);
388 div = con >> CLK_SPI_DIV_CON_SHIFT & CLK_SPI_DIV_CON_MASK;
390 return DIV_TO_RATE(priv->dpll_hz, div);
393 static ulong rk3308_spi_set_clk(struct clk *clk, uint hz)
395 struct rk3308_clk_priv *priv = dev_get_priv(clk->dev);
396 struct rk3308_cru *cru = priv->cru;
397 u32 src_clk_div, con_id;
399 src_clk_div = DIV_ROUND_UP(priv->dpll_hz, hz);
400 assert(src_clk_div - 1 <= 127);
413 printf("do not support this spi bus\n");
417 rk_clrsetreg(&cru->clksel_con[con_id],
418 CLK_SPI_PLL_SEL_MASK | CLK_SPI_DIV_CON_MASK,
419 CLK_SPI_PLL_SEL_DPLL << CLK_SPI_PLL_SEL_SHIFT |
420 (src_clk_div - 1) << CLK_SPI_DIV_CON_SHIFT);
422 return rk3308_spi_get_clk(clk);
425 static ulong rk3308_pwm_get_clk(struct clk *clk)
427 struct rk3308_clk_priv *priv = dev_get_priv(clk->dev);
428 struct rk3308_cru *cru = priv->cru;
431 con = readl(&cru->clksel_con[29]);
432 div = con >> CLK_PWM_DIV_CON_SHIFT & CLK_PWM_DIV_CON_MASK;
434 return DIV_TO_RATE(priv->dpll_hz, div);
437 static ulong rk3308_pwm_set_clk(struct clk *clk, uint hz)
439 struct rk3308_clk_priv *priv = dev_get_priv(clk->dev);
440 struct rk3308_cru *cru = priv->cru;
443 src_clk_div = DIV_ROUND_UP(priv->dpll_hz, hz);
444 assert(src_clk_div - 1 <= 127);
446 rk_clrsetreg(&cru->clksel_con[29],
447 CLK_PWM_PLL_SEL_MASK | CLK_PWM_DIV_CON_MASK,
448 CLK_PWM_PLL_SEL_DPLL << CLK_PWM_PLL_SEL_SHIFT |
449 (src_clk_div - 1) << CLK_PWM_DIV_CON_SHIFT);
451 return rk3308_pwm_get_clk(clk);
454 static ulong rk3308_vop_get_clk(struct clk *clk)
456 struct rk3308_clk_priv *priv = dev_get_priv(clk->dev);
457 struct rk3308_cru *cru = priv->cru;
458 u32 div, pll_sel, vol_sel, con, parent;
460 con = readl(&cru->clksel_con[8]);
461 vol_sel = (con & DCLK_VOP_SEL_MASK) >> DCLK_VOP_SEL_SHIFT;
462 pll_sel = (con & DCLK_VOP_PLL_SEL_MASK) >> DCLK_VOP_PLL_SEL_SHIFT;
463 div = con & DCLK_VOP_DIV_MASK;
465 if (vol_sel == DCLK_VOP_SEL_24M) {
467 } else if (vol_sel == DCLK_VOP_SEL_DIVOUT) {
469 case DCLK_VOP_PLL_SEL_DPLL:
470 parent = priv->dpll_hz;
472 case DCLK_VOP_PLL_SEL_VPLL0:
473 parent = priv->vpll0_hz;
475 case DCLK_VOP_PLL_SEL_VPLL1:
476 parent = priv->vpll0_hz;
479 printf("do not support this vop pll sel\n");
483 printf("do not support this vop sel\n");
487 return DIV_TO_RATE(parent, div);
490 static ulong rk3308_vop_set_clk(struct clk *clk, ulong hz)
492 struct rk3308_clk_priv *priv = dev_get_priv(clk->dev);
493 struct rk3308_cru *cru = priv->cru;
494 ulong pll_rate, now, best_rate = 0;
495 u32 i, div, best_div = 0, best_sel = 0;
497 for (i = 0; i <= DCLK_VOP_PLL_SEL_VPLL1; i++) {
499 case DCLK_VOP_PLL_SEL_DPLL:
500 pll_rate = priv->dpll_hz;
502 case DCLK_VOP_PLL_SEL_VPLL0:
503 pll_rate = priv->vpll0_hz;
505 case DCLK_VOP_PLL_SEL_VPLL1:
506 pll_rate = priv->vpll1_hz;
509 printf("do not support this vop pll sel\n");
513 div = DIV_ROUND_UP(pll_rate, hz);
516 now = pll_rate / div;
517 if (abs(hz - now) < abs(hz - best_rate)) {
522 debug("pll_rate=%lu, best_rate=%lu, best_div=%u, best_sel=%u\n",
523 pll_rate, best_rate, best_div, best_sel);
526 if (best_rate != hz && hz == OSC_HZ) {
527 rk_clrsetreg(&cru->clksel_con[8],
529 DCLK_VOP_SEL_24M << DCLK_VOP_SEL_SHIFT);
530 } else if (best_rate) {
531 rk_clrsetreg(&cru->clksel_con[8],
532 DCLK_VOP_SEL_MASK | DCLK_VOP_PLL_SEL_MASK |
534 DCLK_VOP_SEL_DIVOUT << DCLK_VOP_SEL_SHIFT |
535 best_sel << DCLK_VOP_PLL_SEL_SHIFT |
536 (best_div - 1) << DCLK_VOP_DIV_SHIFT);
538 printf("do not support this vop freq\n");
542 return rk3308_vop_get_clk(clk);
545 static ulong rk3308_bus_get_clk(struct rk3308_clk_priv *priv, ulong clk_id)
547 struct rk3308_cru *cru = priv->cru;
548 u32 div, con, parent = priv->dpll_hz;
552 con = readl(&cru->clksel_con[5]);
553 div = (con & BUS_ACLK_DIV_MASK) >> BUS_ACLK_DIV_SHIFT;
556 con = readl(&cru->clksel_con[6]);
557 div = (con & BUS_HCLK_DIV_MASK) >> BUS_HCLK_DIV_SHIFT;
561 con = readl(&cru->clksel_con[6]);
562 div = (con & BUS_PCLK_DIV_MASK) >> BUS_PCLK_DIV_SHIFT;
568 return DIV_TO_RATE(parent, div);
571 static ulong rk3308_bus_set_clk(struct rk3308_clk_priv *priv, ulong clk_id,
574 struct rk3308_cru *cru = priv->cru;
577 src_clk_div = DIV_ROUND_UP(priv->dpll_hz, hz);
578 assert(src_clk_div - 1 <= 31);
581 * select dpll as pd_bus bus clock source and
582 * set up dependent divisors for PCLK/HCLK and ACLK clocks.
586 rk_clrsetreg(&cru->clksel_con[5],
587 BUS_PLL_SEL_MASK | BUS_ACLK_DIV_MASK,
588 BUS_PLL_SEL_DPLL << BUS_PLL_SEL_SHIFT |
589 (src_clk_div - 1) << BUS_ACLK_DIV_SHIFT);
592 rk_clrsetreg(&cru->clksel_con[6],
594 (src_clk_div - 1) << BUS_HCLK_DIV_SHIFT);
597 rk_clrsetreg(&cru->clksel_con[6],
599 (src_clk_div - 1) << BUS_PCLK_DIV_SHIFT);
602 printf("do not support this bus freq\n");
606 return rk3308_bus_get_clk(priv, clk_id);
609 static ulong rk3308_peri_get_clk(struct rk3308_clk_priv *priv, ulong clk_id)
611 struct rk3308_cru *cru = priv->cru;
612 u32 div, con, parent = priv->dpll_hz;
616 con = readl(&cru->clksel_con[36]);
617 div = (con & PERI_ACLK_DIV_MASK) >> PERI_ACLK_DIV_SHIFT;
620 con = readl(&cru->clksel_con[37]);
621 div = (con & PERI_HCLK_DIV_MASK) >> PERI_HCLK_DIV_SHIFT;
624 con = readl(&cru->clksel_con[37]);
625 div = (con & PERI_PCLK_DIV_MASK) >> PERI_PCLK_DIV_SHIFT;
631 return DIV_TO_RATE(parent, div);
634 static ulong rk3308_peri_set_clk(struct rk3308_clk_priv *priv, ulong clk_id,
637 struct rk3308_cru *cru = priv->cru;
640 src_clk_div = DIV_ROUND_UP(priv->dpll_hz, hz);
641 assert(src_clk_div - 1 <= 31);
644 * select dpll as pd_peri bus clock source and
645 * set up dependent divisors for PCLK/HCLK and ACLK clocks.
649 rk_clrsetreg(&cru->clksel_con[36],
650 PERI_PLL_SEL_MASK | PERI_ACLK_DIV_MASK,
651 PERI_PLL_DPLL << PERI_PLL_SEL_SHIFT |
652 (src_clk_div - 1) << PERI_ACLK_DIV_SHIFT);
655 rk_clrsetreg(&cru->clksel_con[37],
657 (src_clk_div - 1) << PERI_HCLK_DIV_SHIFT);
660 rk_clrsetreg(&cru->clksel_con[37],
662 (src_clk_div - 1) << PERI_PCLK_DIV_SHIFT);
665 printf("do not support this peri freq\n");
669 return rk3308_peri_get_clk(priv, clk_id);
672 static ulong rk3308_audio_get_clk(struct rk3308_clk_priv *priv, ulong clk_id)
674 struct rk3308_cru *cru = priv->cru;
675 u32 div, con, parent = priv->vpll0_hz;
679 con = readl(&cru->clksel_con[45]);
680 div = (con & AUDIO_HCLK_DIV_MASK) >> AUDIO_HCLK_DIV_SHIFT;
683 con = readl(&cru->clksel_con[45]);
684 div = (con & AUDIO_PCLK_DIV_MASK) >> AUDIO_PCLK_DIV_SHIFT;
690 return DIV_TO_RATE(parent, div);
693 static ulong rk3308_audio_set_clk(struct rk3308_clk_priv *priv, ulong clk_id,
696 struct rk3308_cru *cru = priv->cru;
699 src_clk_div = DIV_ROUND_UP(priv->vpll0_hz, hz);
700 assert(src_clk_div - 1 <= 31);
703 * select vpll0 as audio bus clock source and
704 * set up dependent divisors for HCLK and PCLK clocks.
708 rk_clrsetreg(&cru->clksel_con[45],
709 AUDIO_PLL_SEL_MASK | AUDIO_HCLK_DIV_MASK,
710 AUDIO_PLL_VPLL0 << AUDIO_PLL_SEL_SHIFT |
711 (src_clk_div - 1) << AUDIO_HCLK_DIV_SHIFT);
714 rk_clrsetreg(&cru->clksel_con[45],
715 AUDIO_PLL_SEL_MASK | AUDIO_PCLK_DIV_MASK,
716 AUDIO_PLL_VPLL0 << AUDIO_PLL_SEL_SHIFT |
717 (src_clk_div - 1) << AUDIO_PCLK_DIV_SHIFT);
720 printf("do not support this audio freq\n");
724 return rk3308_peri_get_clk(priv, clk_id);
727 static ulong rk3308_crypto_get_clk(struct rk3308_clk_priv *priv, ulong clk_id)
729 struct rk3308_cru *cru = priv->cru;
730 u32 div, con, parent;
734 con = readl(&cru->clksel_con[7]);
735 div = (con & CRYPTO_DIV_MASK) >> CRYPTO_DIV_SHIFT;
736 parent = priv->vpll0_hz;
738 case SCLK_CRYPTO_APK:
739 con = readl(&cru->clksel_con[7]);
740 div = (con & CRYPTO_APK_DIV_MASK) >> CRYPTO_APK_DIV_SHIFT;
741 parent = priv->vpll0_hz;
747 return DIV_TO_RATE(parent, div);
750 static ulong rk3308_crypto_set_clk(struct rk3308_clk_priv *priv, ulong clk_id,
753 struct rk3308_cru *cru = priv->cru;
756 src_clk_div = DIV_ROUND_UP(priv->vpll0_hz, hz);
757 assert(src_clk_div - 1 <= 31);
760 * select gpll as crypto clock source and
761 * set up dependent divisors for crypto clocks.
765 rk_clrsetreg(&cru->clksel_con[7],
766 CRYPTO_PLL_SEL_MASK | CRYPTO_DIV_MASK,
767 CRYPTO_PLL_SEL_VPLL0 << CRYPTO_PLL_SEL_SHIFT |
768 (src_clk_div - 1) << CRYPTO_DIV_SHIFT);
770 case SCLK_CRYPTO_APK:
771 rk_clrsetreg(&cru->clksel_con[7],
772 CRYPTO_APK_PLL_SEL_MASK | CRYPTO_APK_DIV_MASK,
773 CRYPTO_PLL_SEL_VPLL0 << CRYPTO_APK_SEL_SHIFT |
774 (src_clk_div - 1) << CRYPTO_APK_DIV_SHIFT);
777 printf("do not support this peri freq\n");
781 return rk3308_crypto_get_clk(priv, clk_id);
784 static ulong rk3308_clk_get_rate(struct clk *clk)
786 struct rk3308_clk_priv *priv = dev_get_priv(clk->dev);
789 debug("%s id:%ld\n", __func__, clk->id);
794 rate = rockchip_pll_get_rate(&rk3308_pll_clks[APLL],
798 rate = rockchip_pll_get_rate(&rk3308_pll_clks[DPLL],
802 rate = rockchip_pll_get_rate(&rk3308_pll_clks[VPLL0],
806 rate = rockchip_pll_get_rate(&rk3308_pll_clks[VPLL1],
813 case SCLK_EMMC_SAMPLE:
814 rate = rk3308_mmc_get_clk(clk);
820 rate = rk3308_i2c_get_clk(clk);
823 rate = rk3308_saradc_get_clk(clk);
826 rate = rk3308_tsadc_get_clk(clk);
830 rate = rk3308_spi_get_clk(clk);
833 rate = rk3308_pwm_get_clk(clk);
836 rate = rk3308_vop_get_clk(clk);
842 rate = rk3308_bus_get_clk(priv, clk->id);
847 rate = rk3308_peri_get_clk(priv, clk->id);
851 rate = rk3308_audio_get_clk(priv, clk->id);
854 case SCLK_CRYPTO_APK:
855 rate = rk3308_crypto_get_clk(priv, clk->id);
864 static ulong rk3308_clk_set_rate(struct clk *clk, ulong rate)
866 struct rk3308_clk_priv *priv = dev_get_priv(clk->dev);
869 debug("%s %ld %ld\n", __func__, clk->id, rate);
873 ret = rockchip_pll_set_rate(&rk3308_pll_clks[DPLL], priv->cru,
875 priv->dpll_hz = rockchip_pll_get_rate(&rk3308_pll_clks[DPLL],
880 rk3308_armclk_set_clk(priv, rate);
881 priv->armclk_hz = rate;
887 ret = rk3308_mmc_set_clk(clk, rate);
893 ret = rk3308_i2c_set_clk(clk, rate);
896 ret = rk3308_mac_set_clk(clk, rate);
899 ret = rk3308_mac_set_speed_clk(clk, rate);
902 ret = rk3308_saradc_set_clk(clk, rate);
905 ret = rk3308_tsadc_set_clk(clk, rate);
909 ret = rk3308_spi_set_clk(clk, rate);
912 ret = rk3308_pwm_set_clk(clk, rate);
915 ret = rk3308_vop_set_clk(clk, rate);
920 rate = rk3308_bus_set_clk(priv, clk->id, rate);
925 rate = rk3308_peri_set_clk(priv, clk->id, rate);
929 rate = rk3308_audio_set_clk(priv, clk->id, rate);
932 case SCLK_CRYPTO_APK:
933 ret = rk3308_crypto_set_clk(priv, clk->id, rate);
942 #if CONFIG_IS_ENABLED(OF_CONTROL) && !CONFIG_IS_ENABLED(OF_PLATDATA)
943 static int __maybe_unused rk3308_mac_set_parent(struct clk *clk, struct clk *parent)
945 struct rk3308_clk_priv *priv = dev_get_priv(clk->dev);
948 * If the requested parent is in the same clock-controller and
949 * the id is SCLK_MAC_SRC, switch to the internal clock.
951 if (parent->id == SCLK_MAC_SRC) {
952 debug("%s: switching RMII to SCLK_MAC\n", __func__);
953 rk_clrreg(&priv->cru->clksel_con[43], BIT(14));
955 debug("%s: switching RMII to CLKIN\n", __func__);
956 rk_setreg(&priv->cru->clksel_con[43], BIT(14));
962 static int __maybe_unused rk3308_clk_set_parent(struct clk *clk, struct clk *parent)
966 return rk3308_mac_set_parent(clk, parent);
971 debug("%s: unsupported clk %ld\n", __func__, clk->id);
976 static struct clk_ops rk3308_clk_ops = {
977 .get_rate = rk3308_clk_get_rate,
978 .set_rate = rk3308_clk_set_rate,
979 #if CONFIG_IS_ENABLED(OF_CONTROL) && !CONFIG_IS_ENABLED(OF_PLATDATA)
980 .set_parent = rk3308_clk_set_parent,
984 static void rk3308_clk_init(struct udevice *dev)
986 struct rk3308_clk_priv *priv = dev_get_priv(dev);
989 if (rockchip_pll_get_rate(&rk3308_pll_clks[APLL],
990 priv->cru, APLL) != APLL_HZ) {
991 ret = rk3308_armclk_set_clk(priv, APLL_HZ);
993 printf("%s failed to set armclk rate\n", __func__);
996 rk3308_clk_get_pll_rate(priv);
998 rk3308_bus_set_clk(priv, ACLK_BUS, BUS_ACLK_HZ);
999 rk3308_bus_set_clk(priv, HCLK_BUS, BUS_HCLK_HZ);
1000 rk3308_bus_set_clk(priv, PCLK_BUS, BUS_PCLK_HZ);
1002 rk3308_peri_set_clk(priv, ACLK_PERI, PERI_ACLK_HZ);
1003 rk3308_peri_set_clk(priv, HCLK_PERI, PERI_HCLK_HZ);
1004 rk3308_peri_set_clk(priv, PCLK_PERI, PERI_PCLK_HZ);
1006 rk3308_audio_set_clk(priv, HCLK_AUDIO, AUDIO_HCLK_HZ);
1007 rk3308_audio_set_clk(priv, PCLK_AUDIO, AUDIO_PCLK_HZ);
1010 static int rk3308_clk_probe(struct udevice *dev)
1014 rk3308_clk_init(dev);
1016 /* Process 'assigned-{clocks/clock-parents/clock-rates}' properties */
1017 ret = clk_set_defaults(dev, 1);
1019 debug("%s clk_set_defaults failed %d\n", __func__, ret);
1024 static int rk3308_clk_of_to_plat(struct udevice *dev)
1026 struct rk3308_clk_priv *priv = dev_get_priv(dev);
1028 priv->cru = dev_read_addr_ptr(dev);
1033 static int rk3308_clk_bind(struct udevice *dev)
1036 struct udevice *sys_child;
1037 struct sysreset_reg *priv;
1039 /* The reset driver does not have a device node, so bind it here */
1040 ret = device_bind_driver(dev, "rockchip_sysreset", "sysreset",
1043 debug("Warning: No sysreset driver: ret=%d\n", ret);
1045 priv = malloc(sizeof(struct sysreset_reg));
1046 priv->glb_srst_fst_value = offsetof(struct rk3308_cru,
1048 priv->glb_srst_snd_value = offsetof(struct rk3308_cru,
1050 dev_set_priv(sys_child, priv);
1053 #if CONFIG_IS_ENABLED(RESET_ROCKCHIP)
1054 ret = offsetof(struct rk3308_cru, softrst_con[0]);
1055 ret = rockchip_reset_bind(dev, ret, 12);
1057 debug("Warning: software reset driver bind faile\n");
1063 static const struct udevice_id rk3308_clk_ids[] = {
1064 { .compatible = "rockchip,rk3308-cru" },
1068 U_BOOT_DRIVER(rockchip_rk3308_cru) = {
1069 .name = "rockchip_rk3308_cru",
1071 .of_match = rk3308_clk_ids,
1072 .priv_auto = sizeof(struct rk3308_clk_priv),
1073 .of_to_plat = rk3308_clk_of_to_plat,
1074 .ops = &rk3308_clk_ops,
1075 .bind = rk3308_clk_bind,
1076 .probe = rk3308_clk_probe,