2 * (C) Copyright 2017 Rockchip Electronics Co., Ltd
4 * SPDX-License-Identifier: GPL-2.0
9 #include <clk-uclass.h>
13 #include <asm/arch/clock.h>
14 #include <asm/arch/cru_rk3328.h>
15 #include <asm/arch/hardware.h>
18 #include <dt-bindings/clock/rk3328-cru.h>
20 DECLARE_GLOBAL_DATA_PTR;
30 #define RATE_TO_DIV(input_rate, output_rate) \
31 ((input_rate) / (output_rate) - 1);
32 #define DIV_TO_RATE(input_rate, div) ((input_rate) / ((div) + 1))
34 #define PLL_DIVISORS(hz, _refdiv, _postdiv1, _postdiv2) {\
36 .fbdiv = (u32)((u64)hz * _refdiv * _postdiv1 * _postdiv2 / OSC_HZ),\
37 .postdiv1 = _postdiv1, .postdiv2 = _postdiv2};
39 static const struct pll_div gpll_init_cfg = PLL_DIVISORS(GPLL_HZ, 1, 4, 1);
40 static const struct pll_div cpll_init_cfg = PLL_DIVISORS(CPLL_HZ, 2, 2, 1);
42 static const struct pll_div apll_816_cfg = PLL_DIVISORS(816 * MHz, 1, 2, 1);
43 static const struct pll_div apll_600_cfg = PLL_DIVISORS(600 * MHz, 1, 3, 1);
45 static const struct pll_div *apll_cfgs[] = {
46 [APLL_816_MHZ] = &apll_816_cfg,
47 [APLL_600_MHZ] = &apll_600_cfg,
52 PLL_POSTDIV1_SHIFT = 12,
53 PLL_POSTDIV1_MASK = 0x7 << PLL_POSTDIV1_SHIFT,
55 PLL_FBDIV_MASK = 0xfff,
59 PLL_DSMPD_MASK = 1 << PLL_DSMPD_SHIFT,
61 PLL_LOCK_STATUS_SHIFT = 10,
62 PLL_LOCK_STATUS_MASK = 1 << PLL_LOCK_STATUS_SHIFT,
63 PLL_POSTDIV2_SHIFT = 6,
64 PLL_POSTDIV2_MASK = 0x7 << PLL_POSTDIV2_SHIFT,
66 PLL_REFDIV_MASK = 0x3f,
69 PLL_FRACDIV_SHIFT = 0,
70 PLL_FRACDIV_MASK = 0xffffff,
82 CLK_CORE_PLL_SEL_APLL = 0,
83 CLK_CORE_PLL_SEL_GPLL,
84 CLK_CORE_PLL_SEL_DPLL,
85 CLK_CORE_PLL_SEL_NPLL,
86 CLK_CORE_PLL_SEL_SHIFT = 6,
87 CLK_CORE_PLL_SEL_MASK = 3 << CLK_CORE_PLL_SEL_SHIFT,
88 CLK_CORE_DIV_SHIFT = 0,
89 CLK_CORE_DIV_MASK = 0x1f,
92 ACLKM_CORE_DIV_SHIFT = 4,
93 ACLKM_CORE_DIV_MASK = 0x7 << ACLKM_CORE_DIV_SHIFT,
94 PCLK_DBG_DIV_SHIFT = 0,
95 PCLK_DBG_DIV_MASK = 0xF << PCLK_DBG_DIV_SHIFT,
98 ACLK_PERIHP_PLL_SEL_CPLL = 0,
99 ACLK_PERIHP_PLL_SEL_GPLL,
100 ACLK_PERIHP_PLL_SEL_HDMIPHY,
101 ACLK_PERIHP_PLL_SEL_SHIFT = 6,
102 ACLK_PERIHP_PLL_SEL_MASK = 3 << ACLK_PERIHP_PLL_SEL_SHIFT,
103 ACLK_PERIHP_DIV_CON_SHIFT = 0,
104 ACLK_PERIHP_DIV_CON_MASK = 0x1f,
107 PCLK_PERIHP_DIV_CON_SHIFT = 4,
108 PCLK_PERIHP_DIV_CON_MASK = 0x7 << PCLK_PERIHP_DIV_CON_SHIFT,
109 HCLK_PERIHP_DIV_CON_SHIFT = 0,
110 HCLK_PERIHP_DIV_CON_MASK = 3 << HCLK_PERIHP_DIV_CON_SHIFT,
113 CLK_TSADC_DIV_CON_SHIFT = 0,
114 CLK_TSADC_DIV_CON_MASK = 0x3ff,
117 CLK_SARADC_DIV_CON_SHIFT = 0,
118 CLK_SARADC_DIV_CON_MASK = GENMASK(9, 0),
119 CLK_SARADC_DIV_CON_WIDTH = 10,
122 CLK_PWM_PLL_SEL_CPLL = 0,
123 CLK_PWM_PLL_SEL_GPLL,
124 CLK_PWM_PLL_SEL_SHIFT = 15,
125 CLK_PWM_PLL_SEL_MASK = 1 << CLK_PWM_PLL_SEL_SHIFT,
126 CLK_PWM_DIV_CON_SHIFT = 8,
127 CLK_PWM_DIV_CON_MASK = 0x7f << CLK_PWM_DIV_CON_SHIFT,
129 CLK_SPI_PLL_SEL_CPLL = 0,
130 CLK_SPI_PLL_SEL_GPLL,
131 CLK_SPI_PLL_SEL_SHIFT = 7,
132 CLK_SPI_PLL_SEL_MASK = 1 << CLK_SPI_PLL_SEL_SHIFT,
133 CLK_SPI_DIV_CON_SHIFT = 0,
134 CLK_SPI_DIV_CON_MASK = 0x7f << CLK_SPI_DIV_CON_SHIFT,
137 CLK_SDMMC_PLL_SEL_CPLL = 0,
138 CLK_SDMMC_PLL_SEL_GPLL,
139 CLK_SDMMC_PLL_SEL_24M,
140 CLK_SDMMC_PLL_SEL_USBPHY,
141 CLK_SDMMC_PLL_SHIFT = 8,
142 CLK_SDMMC_PLL_MASK = 0x3 << CLK_SDMMC_PLL_SHIFT,
143 CLK_SDMMC_DIV_CON_SHIFT = 0,
144 CLK_SDMMC_DIV_CON_MASK = 0xff << CLK_SDMMC_DIV_CON_SHIFT,
147 CLK_EMMC_PLL_SEL_CPLL = 0,
148 CLK_EMMC_PLL_SEL_GPLL,
149 CLK_EMMC_PLL_SEL_24M,
150 CLK_EMMC_PLL_SEL_USBPHY,
151 CLK_EMMC_PLL_SHIFT = 8,
152 CLK_EMMC_PLL_MASK = 0x3 << CLK_EMMC_PLL_SHIFT,
153 CLK_EMMC_DIV_CON_SHIFT = 0,
154 CLK_EMMC_DIV_CON_MASK = 0xff << CLK_EMMC_DIV_CON_SHIFT,
157 CLK_I2C_PLL_SEL_CPLL = 0,
158 CLK_I2C_PLL_SEL_GPLL,
159 CLK_I2C_DIV_CON_MASK = 0x7f,
160 CLK_I2C_PLL_SEL_MASK = 1,
161 CLK_I2C1_PLL_SEL_SHIFT = 15,
162 CLK_I2C1_DIV_CON_SHIFT = 8,
163 CLK_I2C0_PLL_SEL_SHIFT = 7,
164 CLK_I2C0_DIV_CON_SHIFT = 0,
167 CLK_I2C3_PLL_SEL_SHIFT = 15,
168 CLK_I2C3_DIV_CON_SHIFT = 8,
169 CLK_I2C2_PLL_SEL_SHIFT = 7,
170 CLK_I2C2_DIV_CON_SHIFT = 0,
173 #define VCO_MAX_KHZ (3200 * (MHz / KHz))
174 #define VCO_MIN_KHZ (800 * (MHz / KHz))
175 #define OUTPUT_MAX_KHZ (3200 * (MHz / KHz))
176 #define OUTPUT_MIN_KHZ (16 * (MHz / KHz))
179 * the div restructions of pll in integer mode, these are defined in
180 * * CRU_*PLL_CON0 or PMUCRU_*PLL_CON0
182 #define PLL_DIV_MIN 16
183 #define PLL_DIV_MAX 3200
186 * How to calculate the PLL(from TRM V0.3 Part 1 Page 63):
187 * Formulas also embedded within the Fractional PLL Verilog model:
188 * If DSMPD = 1 (DSM is disabled, "integer mode")
189 * FOUTVCO = FREF / REFDIV * FBDIV
190 * FOUTPOSTDIV = FOUTVCO / POSTDIV1 / POSTDIV2
192 * FOUTVCO = Fractional PLL non-divided output frequency
193 * FOUTPOSTDIV = Fractional PLL divided output frequency
194 * (output of second post divider)
195 * FREF = Fractional PLL input reference frequency, (the OSC_HZ 24MHz input)
196 * REFDIV = Fractional PLL input reference clock divider
197 * FBDIV = Integer value programmed into feedback divide
200 static void rkclk_set_pll(struct rk3328_cru *cru, enum rk_clk_id clk_id,
201 const struct pll_div *div)
204 u32 mode_shift, mode_mask;
210 pll_con = cru->apll_con;
211 mode_shift = APLL_MODE_SHIFT;
214 pll_con = cru->dpll_con;
215 mode_shift = DPLL_MODE_SHIFT;
218 pll_con = cru->cpll_con;
219 mode_shift = CPLL_MODE_SHIFT;
222 pll_con = cru->gpll_con;
223 mode_shift = GPLL_MODE_SHIFT;
226 pll_con = cru->npll_con;
227 mode_shift = NPLL_MODE_SHIFT;
232 mode_mask = 1 << mode_shift;
234 /* All 8 PLLs have same VCO and output frequency range restrictions. */
235 u32 vco_khz = OSC_HZ / 1000 * div->fbdiv / div->refdiv;
236 u32 output_khz = vco_khz / div->postdiv1 / div->postdiv2;
238 debug("PLL at %p: fbdiv=%d, refdiv=%d, postdiv1=%d, \
239 postdiv2=%d, vco=%u khz, output=%u khz\n",
240 pll_con, div->fbdiv, div->refdiv, div->postdiv1,
241 div->postdiv2, vco_khz, output_khz);
242 assert(vco_khz >= VCO_MIN_KHZ && vco_khz <= VCO_MAX_KHZ &&
243 output_khz >= OUTPUT_MIN_KHZ && output_khz <= OUTPUT_MAX_KHZ &&
244 div->fbdiv >= PLL_DIV_MIN && div->fbdiv <= PLL_DIV_MAX);
247 * When power on or changing PLL setting,
248 * we must force PLL into slow mode to ensure output stable clock.
250 rk_clrsetreg(&cru->mode_con, mode_mask, PLL_MODE_SLOW << mode_shift);
252 /* use integer mode */
253 rk_clrsetreg(&pll_con[1], PLL_DSMPD_MASK,
254 PLL_INTEGER_MODE << PLL_DSMPD_SHIFT);
256 rk_clrsetreg(&pll_con[0],
257 PLL_FBDIV_MASK | PLL_POSTDIV1_MASK,
258 (div->fbdiv << PLL_FBDIV_SHIFT) |
259 (div->postdiv1 << PLL_POSTDIV1_SHIFT));
260 rk_clrsetreg(&pll_con[1],
261 PLL_POSTDIV2_MASK | PLL_REFDIV_MASK,
262 (div->postdiv2 << PLL_POSTDIV2_SHIFT) |
263 (div->refdiv << PLL_REFDIV_SHIFT));
265 /* waiting for pll lock */
266 while (!(readl(&pll_con[1]) & (1 << PLL_LOCK_STATUS_SHIFT)))
269 /* pll enter normal mode */
270 rk_clrsetreg(&cru->mode_con, mode_mask, PLL_MODE_NORM << mode_shift);
273 static void rkclk_init(struct rk3328_cru *cru)
279 /* configure gpll cpll */
280 rkclk_set_pll(cru, CLK_GENERAL, &gpll_init_cfg);
281 rkclk_set_pll(cru, CLK_CODEC, &cpll_init_cfg);
283 /* configure perihp aclk, hclk, pclk */
284 aclk_div = GPLL_HZ / PERIHP_ACLK_HZ - 1;
285 hclk_div = PERIHP_ACLK_HZ / PERIHP_HCLK_HZ - 1;
286 pclk_div = PERIHP_ACLK_HZ / PERIHP_PCLK_HZ - 1;
288 rk_clrsetreg(&cru->clksel_con[28],
289 ACLK_PERIHP_PLL_SEL_MASK | ACLK_PERIHP_DIV_CON_MASK,
290 ACLK_PERIHP_PLL_SEL_GPLL << ACLK_PERIHP_PLL_SEL_SHIFT |
291 aclk_div << ACLK_PERIHP_DIV_CON_SHIFT);
292 rk_clrsetreg(&cru->clksel_con[29],
293 PCLK_PERIHP_DIV_CON_MASK | HCLK_PERIHP_DIV_CON_MASK,
294 pclk_div << PCLK_PERIHP_DIV_CON_SHIFT |
295 hclk_div << HCLK_PERIHP_DIV_CON_SHIFT);
298 void rk3328_configure_cpu(struct rk3328_cru *cru,
299 enum apll_frequencies apll_freq)
305 rkclk_set_pll(cru, CLK_ARM, apll_cfgs[apll_freq]);
307 clk_core_div = APLL_HZ / CLK_CORE_HZ - 1;
308 aclkm_div = APLL_HZ / ACLKM_CORE_HZ / (clk_core_div + 1) - 1;
309 pclk_dbg_div = APLL_HZ / PCLK_DBG_HZ / (clk_core_div + 1) - 1;
311 rk_clrsetreg(&cru->clksel_con[0],
312 CLK_CORE_PLL_SEL_MASK | CLK_CORE_DIV_MASK,
313 CLK_CORE_PLL_SEL_APLL << CLK_CORE_PLL_SEL_SHIFT |
314 clk_core_div << CLK_CORE_DIV_SHIFT);
316 rk_clrsetreg(&cru->clksel_con[1],
317 PCLK_DBG_DIV_MASK | ACLKM_CORE_DIV_MASK,
318 pclk_dbg_div << PCLK_DBG_DIV_SHIFT |
319 aclkm_div << ACLKM_CORE_DIV_SHIFT);
323 static ulong rk3328_i2c_get_clk(struct rk3328_cru *cru, ulong clk_id)
329 con = readl(&cru->clksel_con[34]);
330 div = con >> CLK_I2C0_DIV_CON_SHIFT & CLK_I2C_DIV_CON_MASK;
333 con = readl(&cru->clksel_con[34]);
334 div = con >> CLK_I2C1_DIV_CON_SHIFT & CLK_I2C_DIV_CON_MASK;
337 con = readl(&cru->clksel_con[35]);
338 div = con >> CLK_I2C2_DIV_CON_SHIFT & CLK_I2C_DIV_CON_MASK;
341 con = readl(&cru->clksel_con[35]);
342 div = con >> CLK_I2C3_DIV_CON_SHIFT & CLK_I2C_DIV_CON_MASK;
345 printf("do not support this i2c bus\n");
349 return DIV_TO_RATE(GPLL_HZ, div);
352 static ulong rk3328_i2c_set_clk(struct rk3328_cru *cru, ulong clk_id, uint hz)
356 src_clk_div = GPLL_HZ / hz;
357 assert(src_clk_div - 1 < 127);
361 rk_clrsetreg(&cru->clksel_con[34],
362 CLK_I2C_DIV_CON_MASK << CLK_I2C0_DIV_CON_SHIFT |
363 CLK_I2C_PLL_SEL_MASK << CLK_I2C0_PLL_SEL_SHIFT,
364 (src_clk_div - 1) << CLK_I2C0_DIV_CON_SHIFT |
365 CLK_I2C_PLL_SEL_GPLL << CLK_I2C0_PLL_SEL_SHIFT);
368 rk_clrsetreg(&cru->clksel_con[34],
369 CLK_I2C_DIV_CON_MASK << CLK_I2C1_DIV_CON_SHIFT |
370 CLK_I2C_PLL_SEL_MASK << CLK_I2C1_PLL_SEL_SHIFT,
371 (src_clk_div - 1) << CLK_I2C1_DIV_CON_SHIFT |
372 CLK_I2C_PLL_SEL_GPLL << CLK_I2C1_PLL_SEL_SHIFT);
375 rk_clrsetreg(&cru->clksel_con[35],
376 CLK_I2C_DIV_CON_MASK << CLK_I2C2_DIV_CON_SHIFT |
377 CLK_I2C_PLL_SEL_MASK << CLK_I2C2_PLL_SEL_SHIFT,
378 (src_clk_div - 1) << CLK_I2C2_DIV_CON_SHIFT |
379 CLK_I2C_PLL_SEL_GPLL << CLK_I2C2_PLL_SEL_SHIFT);
382 rk_clrsetreg(&cru->clksel_con[35],
383 CLK_I2C_DIV_CON_MASK << CLK_I2C3_DIV_CON_SHIFT |
384 CLK_I2C_PLL_SEL_MASK << CLK_I2C3_PLL_SEL_SHIFT,
385 (src_clk_div - 1) << CLK_I2C3_DIV_CON_SHIFT |
386 CLK_I2C_PLL_SEL_GPLL << CLK_I2C3_PLL_SEL_SHIFT);
389 printf("do not support this i2c bus\n");
393 return DIV_TO_RATE(GPLL_HZ, src_clk_div);
396 static ulong rk3328_mmc_get_clk(struct rk3328_cru *cru, uint clk_id)
398 u32 div, con, con_id;
412 con = readl(&cru->clksel_con[con_id]);
413 div = (con & CLK_EMMC_DIV_CON_MASK) >> CLK_EMMC_DIV_CON_SHIFT;
415 if ((con & CLK_EMMC_PLL_MASK) >> CLK_EMMC_PLL_SHIFT
416 == CLK_EMMC_PLL_SEL_24M)
417 return DIV_TO_RATE(OSC_HZ, div) / 2;
419 return DIV_TO_RATE(GPLL_HZ, div) / 2;
422 static ulong rk3328_mmc_set_clk(struct rk3328_cru *cru,
423 ulong clk_id, ulong set_rate)
440 /* Select clk_sdmmc/emmc source from GPLL by default */
441 /* mmc clock defaulg div 2 internal, need provide double in cru */
442 src_clk_div = DIV_ROUND_UP(GPLL_HZ / 2, set_rate);
444 if (src_clk_div > 127) {
445 /* use 24MHz source for 400KHz clock */
446 src_clk_div = DIV_ROUND_UP(OSC_HZ / 2, set_rate);
447 rk_clrsetreg(&cru->clksel_con[con_id],
448 CLK_EMMC_PLL_MASK | CLK_EMMC_DIV_CON_MASK,
449 CLK_EMMC_PLL_SEL_24M << CLK_EMMC_PLL_SHIFT |
450 (src_clk_div - 1) << CLK_EMMC_DIV_CON_SHIFT);
452 rk_clrsetreg(&cru->clksel_con[con_id],
453 CLK_EMMC_PLL_MASK | CLK_EMMC_DIV_CON_MASK,
454 CLK_EMMC_PLL_SEL_GPLL << CLK_EMMC_PLL_SHIFT |
455 (src_clk_div - 1) << CLK_EMMC_DIV_CON_SHIFT);
458 return rk3328_mmc_get_clk(cru, clk_id);
461 static ulong rk3328_pwm_get_clk(struct rk3328_cru *cru)
465 con = readl(&cru->clksel_con[24]);
466 div = (con & CLK_PWM_DIV_CON_MASK) >> CLK_PWM_DIV_CON_SHIFT;
468 return DIV_TO_RATE(GPLL_HZ, div);
471 static ulong rk3328_pwm_set_clk(struct rk3328_cru *cru, uint hz)
473 u32 div = GPLL_HZ / hz;
475 rk_clrsetreg(&cru->clksel_con[24],
476 CLK_PWM_PLL_SEL_MASK | CLK_PWM_DIV_CON_MASK,
477 CLK_PWM_PLL_SEL_GPLL << CLK_PWM_PLL_SEL_SHIFT |
478 (div - 1) << CLK_PWM_DIV_CON_SHIFT);
480 return DIV_TO_RATE(GPLL_HZ, div);
483 static ulong rk3328_saradc_get_clk(struct rk3328_cru *cru)
487 val = readl(&cru->clksel_con[23]);
488 div = bitfield_extract(val, CLK_SARADC_DIV_CON_SHIFT,
489 CLK_SARADC_DIV_CON_WIDTH);
491 return DIV_TO_RATE(OSC_HZ, div);
494 static ulong rk3328_saradc_set_clk(struct rk3328_cru *cru, uint hz)
498 src_clk_div = DIV_ROUND_UP(OSC_HZ, hz) - 1;
499 assert(src_clk_div < 128);
501 rk_clrsetreg(&cru->clksel_con[23],
502 CLK_SARADC_DIV_CON_MASK,
503 src_clk_div << CLK_SARADC_DIV_CON_SHIFT);
505 return rk3328_saradc_get_clk(cru);
508 static ulong rk3328_clk_get_rate(struct clk *clk)
510 struct rk3328_clk_priv *priv = dev_get_priv(clk->dev);
520 rate = rk3328_mmc_get_clk(priv->cru, clk->id);
526 rate = rk3328_i2c_get_clk(priv->cru, clk->id);
529 rate = rk3328_pwm_get_clk(priv->cru);
532 rate = rk3328_saradc_get_clk(priv->cru);
541 static ulong rk3328_clk_set_rate(struct clk *clk, ulong rate)
543 struct rk3328_clk_priv *priv = dev_get_priv(clk->dev);
553 ret = rk3328_mmc_set_clk(priv->cru, clk->id, rate);
559 ret = rk3328_i2c_set_clk(priv->cru, clk->id, rate);
562 ret = rk3328_pwm_set_clk(priv->cru, rate);
565 ret = rk3328_saradc_set_clk(priv->cru, rate);
574 static struct clk_ops rk3328_clk_ops = {
575 .get_rate = rk3328_clk_get_rate,
576 .set_rate = rk3328_clk_set_rate,
579 static int rk3328_clk_probe(struct udevice *dev)
581 struct rk3328_clk_priv *priv = dev_get_priv(dev);
583 rkclk_init(priv->cru);
588 static int rk3328_clk_ofdata_to_platdata(struct udevice *dev)
590 struct rk3328_clk_priv *priv = dev_get_priv(dev);
592 priv->cru = (struct rk3328_cru *)devfdt_get_addr(dev);
597 static int rk3328_clk_bind(struct udevice *dev)
600 struct udevice *sys_child;
601 struct sysreset_reg *priv;
603 /* The reset driver does not have a device node, so bind it here */
604 ret = device_bind_driver(dev, "rockchip_sysreset", "sysreset",
607 debug("Warning: No sysreset driver: ret=%d\n", ret);
609 priv = malloc(sizeof(struct sysreset_reg));
610 priv->glb_srst_fst_value = offsetof(struct rk3328_cru,
612 priv->glb_srst_snd_value = offsetof(struct rk3328_cru,
614 sys_child->priv = priv;
617 #if CONFIG_IS_ENABLED(CONFIG_RESET_ROCKCHIP)
618 ret = offsetof(struct rk3328_cru, softrst_con[0]);
619 ret = rockchip_reset_bind(dev, ret, 12);
621 debug("Warning: software reset driver bind faile\n");
627 static const struct udevice_id rk3328_clk_ids[] = {
628 { .compatible = "rockchip,rk3328-cru" },
632 U_BOOT_DRIVER(rockchip_rk3328_cru) = {
633 .name = "rockchip_rk3328_cru",
635 .of_match = rk3328_clk_ids,
636 .priv_auto_alloc_size = sizeof(struct rk3328_clk_priv),
637 .ofdata_to_platdata = rk3328_clk_ofdata_to_platdata,
638 .ops = &rk3328_clk_ops,
639 .bind = rk3328_clk_bind,
640 .probe = rk3328_clk_probe,