2 * (C) Copyright 2015 Google, Inc
4 * SPDX-License-Identifier: GPL-2.0
13 #include <asm/arch/clock.h>
14 #include <asm/arch/cru_rk3288.h>
15 #include <asm/arch/grf_rk3288.h>
16 #include <asm/arch/hardware.h>
17 #include <dt-bindings/clock/rk3288-cru.h>
18 #include <dm/device-internal.h>
20 #include <dm/uclass-internal.h>
22 DECLARE_GLOBAL_DATA_PTR;
24 struct rk3288_clk_plat {
25 enum rk_clk_id clk_id;
28 struct rk3288_clk_priv {
29 struct rk3288_grf *grf;
30 struct rk3288_cru *cru;
41 VCO_MAX_HZ = 2200U * 1000000,
42 VCO_MIN_HZ = 440 * 1000000,
43 OUTPUT_MAX_HZ = 2200U * 1000000,
44 OUTPUT_MIN_HZ = 27500000,
45 FREF_MAX_HZ = 2200U * 1000000,
46 FREF_MIN_HZ = 269 * 1000000,
57 PLL_BWADJ_MASK = 0x0fff,
62 /* CLKSEL1: pd bus clk pll sel: codec or general */
63 PD_BUS_SEL_PLL_MASK = 15,
67 /* pd bus pclk div: pclk = pd_bus_aclk /(div + 1) */
68 PD_BUS_PCLK_DIV_SHIFT = 12,
69 PD_BUS_PCLK_DIV_MASK = 7,
71 /* pd bus hclk div: aclk_bus: hclk_bus = 1:1 or 2:1 or 4:1 */
72 PD_BUS_HCLK_DIV_SHIFT = 8,
73 PD_BUS_HCLK_DIV_MASK = 3,
75 /* pd bus aclk div: pd_bus_aclk = pd_bus_src_clk /(div0 * div1) */
76 PD_BUS_ACLK_DIV0_SHIFT = 3,
77 PD_BUS_ACLK_DIV0_MASK = 0x1f,
78 PD_BUS_ACLK_DIV1_SHIFT = 0,
79 PD_BUS_ACLK_DIV1_MASK = 0x7,
83 * peripheral bus pclk div:
84 * aclk_bus: pclk_bus = 1:1 or 2:1 or 4:1 or 8:1
86 PERI_PCLK_DIV_SHIFT = 12,
87 PERI_PCLK_DIV_MASK = 7,
89 /* peripheral bus hclk div: aclk_bus: hclk_bus = 1:1 or 2:1 or 4:1 */
90 PERI_HCLK_DIV_SHIFT = 8,
91 PERI_HCLK_DIV_MASK = 3,
94 * peripheral bus aclk div:
95 * aclk_periph = periph_clk_src / (peri_aclk_div_con + 1)
97 PERI_ACLK_DIV_SHIFT = 0,
98 PERI_ACLK_DIV_MASK = 0x1f,
101 DPLL_MODE_MASK = 0x3,
112 GPLL_MODE_SHIFT = 12,
117 NPLL_MODE_SHIFT = 14,
121 SOCSTS_DPLL_LOCK = 1 << 5,
122 SOCSTS_APLL_LOCK = 1 << 6,
123 SOCSTS_CPLL_LOCK = 1 << 7,
124 SOCSTS_GPLL_LOCK = 1 << 8,
125 SOCSTS_NPLL_LOCK = 1 << 9,
128 #define RATE_TO_DIV(input_rate, output_rate) \
129 ((input_rate) / (output_rate) - 1);
131 #define DIV_TO_RATE(input_rate, div) ((input_rate) / ((div) + 1))
133 #define PLL_DIVISORS(hz, _nr, _no) {\
134 .nr = _nr, .nf = (u32)((u64)hz * _nr * _no / OSC_HZ), .no = _no};\
135 _Static_assert(((u64)hz * _nr * _no / OSC_HZ) * OSC_HZ /\
136 (_nr * _no) == hz, #hz "Hz cannot be hit with PLL "\
137 "divisors on line " __stringify(__LINE__));
139 /* Keep divisors as low as possible to reduce jitter and power usage */
140 static const struct pll_div apll_init_cfg = PLL_DIVISORS(APLL_HZ, 1, 1);
141 static const struct pll_div gpll_init_cfg = PLL_DIVISORS(GPLL_HZ, 2, 2);
142 static const struct pll_div cpll_init_cfg = PLL_DIVISORS(CPLL_HZ, 1, 2);
144 int rkclk_get_clk(enum rk_clk_id clk_id, struct udevice **devp)
148 for (uclass_find_first_device(UCLASS_CLK, &dev);
150 uclass_find_next_device(&dev)) {
151 struct rk3288_clk_plat *plat = dev_get_platdata(dev);
153 if (plat->clk_id == clk_id) {
155 return device_probe(dev);
162 void *rockchip_get_cru(void)
164 struct rk3288_clk_priv *priv;
168 ret = rkclk_get_clk(CLK_GENERAL, &dev);
171 priv = dev_get_priv(dev);
175 static int rkclk_set_pll(struct rk3288_cru *cru, enum rk_clk_id clk_id,
176 const struct pll_div *div)
178 int pll_id = rk_pll_id(clk_id);
179 struct rk3288_pll *pll = &cru->pll[pll_id];
180 /* All PLLs have same VCO and output frequency range restrictions. */
181 uint vco_hz = OSC_HZ / 1000 * div->nf / div->nr * 1000;
182 uint output_hz = vco_hz / div->no;
184 debug("PLL at %p: nf=%d, nr=%d, no=%d, vco=%u Hz, output=%u Hz\n",
185 pll, div->nf, div->nr, div->no, vco_hz, output_hz);
186 assert(vco_hz >= VCO_MIN_HZ && vco_hz <= VCO_MAX_HZ &&
187 output_hz >= OUTPUT_MIN_HZ && output_hz <= OUTPUT_MAX_HZ &&
188 (div->no == 1 || !(div->no % 2)));
191 rk_setreg(&pll->con3, 1 << PLL_RESET_SHIFT);
193 rk_clrsetreg(&pll->con0,
194 CLKR_MASK << CLKR_SHIFT | PLL_OD_MASK,
195 ((div->nr - 1) << CLKR_SHIFT) | (div->no - 1));
196 rk_clrsetreg(&pll->con1, CLKF_MASK, div->nf - 1);
197 rk_clrsetreg(&pll->con2, PLL_BWADJ_MASK, (div->nf >> 1) - 1);
201 /* return form rest */
202 rk_clrreg(&pll->con3, 1 << PLL_RESET_SHIFT);
207 static inline unsigned int log2(unsigned int value)
209 return fls(value) - 1;
212 static int rkclk_configure_ddr(struct rk3288_cru *cru, struct rk3288_grf *grf,
215 static const struct pll_div dpll_cfg[] = {
216 {.nf = 25, .nr = 2, .no = 1},
217 {.nf = 400, .nr = 9, .no = 2},
218 {.nf = 500, .nr = 9, .no = 2},
219 {.nf = 100, .nr = 3, .no = 1},
223 debug("%s: cru=%p, grf=%p, hz=%u\n", __func__, cru, grf, hz);
228 case 533000000: /* actually 533.3P MHz */
231 case 666000000: /* actually 666.6P MHz */
238 debug("Unsupported SDRAM frequency, add to clock.c!");
242 /* pll enter slow-mode */
243 rk_clrsetreg(&cru->cru_mode_con, DPLL_MODE_MASK << DPLL_MODE_SHIFT,
244 DPLL_MODE_SLOW << DPLL_MODE_SHIFT);
246 rkclk_set_pll(cru, CLK_DDR, &dpll_cfg[cfg]);
248 /* wait for pll lock */
249 while (!(readl(&grf->soc_status[1]) & SOCSTS_DPLL_LOCK))
252 /* PLL enter normal-mode */
253 rk_clrsetreg(&cru->cru_mode_con, DPLL_MODE_MASK << DPLL_MODE_SHIFT,
254 DPLL_MODE_NORM << DPLL_MODE_SHIFT);
259 #ifdef CONFIG_SPL_BUILD
260 static void rkclk_init(struct rk3288_cru *cru, struct rk3288_grf *grf)
266 /* pll enter slow-mode */
267 rk_clrsetreg(&cru->cru_mode_con,
268 GPLL_MODE_MASK << GPLL_MODE_SHIFT |
269 CPLL_MODE_MASK << CPLL_MODE_SHIFT,
270 GPLL_MODE_SLOW << GPLL_MODE_SHIFT |
271 CPLL_MODE_SLOW << CPLL_MODE_SHIFT);
274 rkclk_set_pll(cru, CLK_GENERAL, &gpll_init_cfg);
275 rkclk_set_pll(cru, CLK_CODEC, &cpll_init_cfg);
277 /* waiting for pll lock */
278 while ((readl(&grf->soc_status[1]) &
279 (SOCSTS_CPLL_LOCK | SOCSTS_GPLL_LOCK)) !=
280 (SOCSTS_CPLL_LOCK | SOCSTS_GPLL_LOCK))
284 * pd_bus clock pll source selection and
285 * set up dependent divisors for PCLK/HCLK and ACLK clocks.
287 aclk_div = GPLL_HZ / PD_BUS_ACLK_HZ - 1;
288 assert((aclk_div + 1) * PD_BUS_ACLK_HZ == GPLL_HZ && aclk_div < 0x1f);
289 hclk_div = PD_BUS_ACLK_HZ / PD_BUS_HCLK_HZ - 1;
290 assert((hclk_div + 1) * PD_BUS_HCLK_HZ ==
291 PD_BUS_ACLK_HZ && (hclk_div < 0x4) && (hclk_div != 0x2));
293 pclk_div = PD_BUS_ACLK_HZ / PD_BUS_PCLK_HZ - 1;
294 assert((pclk_div + 1) * PD_BUS_PCLK_HZ ==
295 PD_BUS_ACLK_HZ && pclk_div < 0x7);
297 rk_clrsetreg(&cru->cru_clksel_con[1],
298 PD_BUS_PCLK_DIV_MASK << PD_BUS_PCLK_DIV_SHIFT |
299 PD_BUS_HCLK_DIV_MASK << PD_BUS_HCLK_DIV_SHIFT |
300 PD_BUS_ACLK_DIV0_MASK << PD_BUS_ACLK_DIV0_SHIFT |
301 PD_BUS_ACLK_DIV1_MASK << PD_BUS_ACLK_DIV1_SHIFT,
302 pclk_div << PD_BUS_PCLK_DIV_SHIFT |
303 hclk_div << PD_BUS_HCLK_DIV_SHIFT |
304 aclk_div << PD_BUS_ACLK_DIV0_SHIFT |
308 * peri clock pll source selection and
309 * set up dependent divisors for PCLK/HCLK and ACLK clocks.
311 aclk_div = GPLL_HZ / PERI_ACLK_HZ - 1;
312 assert((aclk_div + 1) * PERI_ACLK_HZ == GPLL_HZ && aclk_div < 0x1f);
314 hclk_div = log2(PERI_ACLK_HZ / PERI_HCLK_HZ);
315 assert((1 << hclk_div) * PERI_HCLK_HZ ==
316 PERI_ACLK_HZ && (hclk_div < 0x4));
318 pclk_div = log2(PERI_ACLK_HZ / PERI_PCLK_HZ);
319 assert((1 << pclk_div) * PERI_PCLK_HZ ==
320 PERI_ACLK_HZ && (pclk_div < 0x4));
322 rk_clrsetreg(&cru->cru_clksel_con[10],
323 PERI_PCLK_DIV_MASK << PERI_PCLK_DIV_SHIFT |
324 PERI_HCLK_DIV_MASK << PERI_HCLK_DIV_SHIFT |
325 PERI_ACLK_DIV_MASK << PERI_ACLK_DIV_SHIFT,
326 pclk_div << PERI_PCLK_DIV_SHIFT |
327 hclk_div << PERI_HCLK_DIV_SHIFT |
328 aclk_div << PERI_ACLK_DIV_SHIFT);
330 /* PLL enter normal-mode */
331 rk_clrsetreg(&cru->cru_mode_con,
332 GPLL_MODE_MASK << GPLL_MODE_SHIFT |
333 CPLL_MODE_MASK << CPLL_MODE_SHIFT,
334 GPLL_MODE_NORM << GPLL_MODE_SHIFT |
335 GPLL_MODE_NORM << CPLL_MODE_SHIFT);
339 /* Get pll rate by id */
340 static uint32_t rkclk_pll_get_rate(struct rk3288_cru *cru,
341 enum rk_clk_id clk_id)
345 int pll_id = rk_pll_id(clk_id);
346 struct rk3288_pll *pll = &cru->pll[pll_id];
347 static u8 clk_shift[CLK_COUNT] = {
348 0xff, APLL_WORK_SHIFT, DPLL_WORK_SHIFT, CPLL_WORK_SHIFT,
349 GPLL_WORK_SHIFT, NPLL_WORK_SHIFT
353 con = readl(&cru->cru_mode_con);
354 shift = clk_shift[clk_id];
355 switch ((con >> shift) & APLL_WORK_MASK) {
358 case APLL_WORK_NORMAL:
360 con = readl(&pll->con0);
361 no = ((con >> CLKOD_SHIFT) & CLKOD_MASK) + 1;
362 nr = ((con >> CLKR_SHIFT) & CLKR_MASK) + 1;
363 con = readl(&pll->con1);
364 nf = ((con >> CLKF_SHIFT) & CLKF_MASK) + 1;
366 return (24 * nf / (nr * no)) * 1000000;
373 static ulong rk3288_clk_get_rate(struct udevice *dev)
375 struct rk3288_clk_plat *plat = dev_get_platdata(dev);
376 struct rk3288_clk_priv *priv = dev_get_priv(dev);
378 debug("%s\n", dev->name);
379 return rkclk_pll_get_rate(priv->cru, plat->clk_id);
382 static ulong rk3288_clk_set_rate(struct udevice *dev, ulong rate)
384 struct rk3288_clk_plat *plat = dev_get_platdata(dev);
385 struct rk3288_clk_priv *priv = dev_get_priv(dev);
387 debug("%s\n", dev->name);
388 switch (plat->clk_id) {
390 rkclk_configure_ddr(priv->cru, priv->grf, rate);
399 static ulong rockchip_mmc_get_clk(struct rk3288_cru *cru, uint gclk_rate,
408 con = readl(&cru->cru_clksel_con[12]);
409 mux = (con >> EMMC_PLL_SHIFT) & EMMC_PLL_MASK;
410 div = (con >> EMMC_DIV_SHIFT) & EMMC_DIV_MASK;
413 con = readl(&cru->cru_clksel_con[11]);
414 mux = (con >> MMC0_PLL_SHIFT) & MMC0_PLL_MASK;
415 div = (con >> MMC0_DIV_SHIFT) & MMC0_DIV_MASK;
418 con = readl(&cru->cru_clksel_con[12]);
419 mux = (con >> SDIO0_PLL_SHIFT) & SDIO0_PLL_MASK;
420 div = (con >> SDIO0_DIV_SHIFT) & SDIO0_DIV_MASK;
426 src_rate = mux == EMMC_PLL_SELECT_24MHZ ? OSC_HZ : gclk_rate;
427 return DIV_TO_RATE(src_rate, div);
430 static ulong rockchip_mmc_set_clk(struct rk3288_cru *cru, uint gclk_rate,
431 int periph, uint freq)
436 debug("%s: gclk_rate=%u\n", __func__, gclk_rate);
437 src_clk_div = RATE_TO_DIV(gclk_rate, freq);
439 if (src_clk_div > 0x3f) {
440 src_clk_div = RATE_TO_DIV(OSC_HZ, freq);
441 mux = EMMC_PLL_SELECT_24MHZ;
442 assert((int)EMMC_PLL_SELECT_24MHZ ==
443 (int)MMC0_PLL_SELECT_24MHZ);
445 mux = EMMC_PLL_SELECT_GENERAL;
446 assert((int)EMMC_PLL_SELECT_GENERAL ==
447 (int)MMC0_PLL_SELECT_GENERAL);
451 rk_clrsetreg(&cru->cru_clksel_con[12],
452 EMMC_PLL_MASK << EMMC_PLL_SHIFT |
453 EMMC_DIV_MASK << EMMC_DIV_SHIFT,
454 mux << EMMC_PLL_SHIFT |
455 (src_clk_div - 1) << EMMC_DIV_SHIFT);
458 rk_clrsetreg(&cru->cru_clksel_con[11],
459 MMC0_PLL_MASK << MMC0_PLL_SHIFT |
460 MMC0_DIV_MASK << MMC0_DIV_SHIFT,
461 mux << MMC0_PLL_SHIFT |
462 (src_clk_div - 1) << MMC0_DIV_SHIFT);
465 rk_clrsetreg(&cru->cru_clksel_con[12],
466 SDIO0_PLL_MASK << SDIO0_PLL_SHIFT |
467 SDIO0_DIV_MASK << SDIO0_DIV_SHIFT,
468 mux << SDIO0_PLL_SHIFT |
469 (src_clk_div - 1) << SDIO0_DIV_SHIFT);
475 return rockchip_mmc_get_clk(cru, gclk_rate, periph);
478 static ulong rockchip_spi_get_clk(struct rk3288_cru *cru, uint gclk_rate,
486 con = readl(&cru->cru_clksel_con[25]);
487 mux = (con >> SPI0_PLL_SHIFT) & SPI0_PLL_MASK;
488 div = (con >> SPI0_DIV_SHIFT) & SPI0_DIV_MASK;
491 con = readl(&cru->cru_clksel_con[25]);
492 mux = (con >> SPI1_PLL_SHIFT) & SPI1_PLL_MASK;
493 div = (con >> SPI1_DIV_SHIFT) & SPI1_DIV_MASK;
496 con = readl(&cru->cru_clksel_con[39]);
497 mux = (con >> SPI2_PLL_SHIFT) & SPI2_PLL_MASK;
498 div = (con >> SPI2_DIV_SHIFT) & SPI2_DIV_MASK;
503 assert(mux == SPI0_PLL_SELECT_GENERAL);
505 return DIV_TO_RATE(gclk_rate, div);
508 static ulong rockchip_spi_set_clk(struct rk3288_cru *cru, uint gclk_rate,
509 int periph, uint freq)
513 debug("%s: clk_general_rate=%u\n", __func__, gclk_rate);
514 src_clk_div = RATE_TO_DIV(gclk_rate, freq);
517 rk_clrsetreg(&cru->cru_clksel_con[25],
518 SPI0_PLL_MASK << SPI0_PLL_SHIFT |
519 SPI0_DIV_MASK << SPI0_DIV_SHIFT,
520 SPI0_PLL_SELECT_GENERAL << SPI0_PLL_SHIFT |
521 src_clk_div << SPI0_DIV_SHIFT);
524 rk_clrsetreg(&cru->cru_clksel_con[25],
525 SPI1_PLL_MASK << SPI1_PLL_SHIFT |
526 SPI1_DIV_MASK << SPI1_DIV_SHIFT,
527 SPI1_PLL_SELECT_GENERAL << SPI1_PLL_SHIFT |
528 src_clk_div << SPI1_DIV_SHIFT);
531 rk_clrsetreg(&cru->cru_clksel_con[39],
532 SPI2_PLL_MASK << SPI2_PLL_SHIFT |
533 SPI2_DIV_MASK << SPI2_DIV_SHIFT,
534 SPI2_PLL_SELECT_GENERAL << SPI2_PLL_SHIFT |
535 src_clk_div << SPI2_DIV_SHIFT);
541 return rockchip_spi_get_clk(cru, gclk_rate, periph);
544 static ulong rk3288_get_periph_rate(struct udevice *dev, int periph)
546 struct rk3288_clk_priv *priv = dev_get_priv(dev);
547 struct udevice *gclk;
548 ulong new_rate, gclk_rate;
551 ret = rkclk_get_clk(CLK_GENERAL, &gclk);
554 gclk_rate = clk_get_rate(gclk);
559 new_rate = rockchip_mmc_get_clk(priv->cru, gclk_rate, periph);
564 new_rate = rockchip_spi_get_clk(priv->cru, gclk_rate, periph);
580 static ulong rk3288_set_periph_rate(struct udevice *dev, int periph, ulong rate)
582 struct rk3288_clk_priv *priv = dev_get_priv(dev);
583 struct udevice *gclk;
584 ulong new_rate, gclk_rate;
587 ret = rkclk_get_clk(CLK_GENERAL, &gclk);
590 gclk_rate = clk_get_rate(gclk);
595 new_rate = rockchip_mmc_set_clk(priv->cru, gclk_rate, periph,
601 new_rate = rockchip_spi_set_clk(priv->cru, gclk_rate, periph,
611 static struct clk_ops rk3288_clk_ops = {
612 .get_rate = rk3288_clk_get_rate,
613 .set_rate = rk3288_clk_set_rate,
614 .set_periph_rate = rk3288_set_periph_rate,
615 .get_periph_rate = rk3288_get_periph_rate,
618 static int rk3288_clk_probe(struct udevice *dev)
620 struct rk3288_clk_plat *plat = dev_get_platdata(dev);
621 struct rk3288_clk_priv *priv = dev_get_priv(dev);
623 if (plat->clk_id != CLK_OSC) {
624 struct rk3288_clk_priv *parent_priv = dev_get_priv(dev->parent);
626 priv->cru = parent_priv->cru;
627 priv->grf = parent_priv->grf;
630 priv->cru = (struct rk3288_cru *)dev_get_addr(dev);
631 priv->grf = syscon_get_first_range(ROCKCHIP_SYSCON_GRF);
632 #ifdef CONFIG_SPL_BUILD
633 rkclk_init(priv->cru, priv->grf);
639 static const char *const clk_name[CLK_COUNT] = {
648 static int rk3288_clk_bind(struct udevice *dev)
650 struct rk3288_clk_plat *plat = dev_get_platdata(dev);
653 /* We only need to set up the root clock */
654 if (dev->of_offset == -1) {
655 plat->clk_id = CLK_OSC;
659 /* Create devices for P main clocks */
660 for (pll = 1; pll < CLK_COUNT; pll++) {
661 struct udevice *child;
662 struct rk3288_clk_plat *cplat;
664 debug("%s %s\n", __func__, clk_name[pll]);
665 ret = device_bind_driver(dev, "clk_rk3288", clk_name[pll],
669 cplat = dev_get_platdata(child);
673 /* The reset driver does not have a device node, so bind it here */
674 ret = device_bind_driver(gd->dm_root, "rk3288_reset", "reset", &dev);
676 debug("Warning: No RK3288 reset driver: ret=%d\n", ret);
681 static const struct udevice_id rk3288_clk_ids[] = {
682 { .compatible = "rockchip,rk3288-cru" },
686 U_BOOT_DRIVER(clk_rk3288) = {
687 .name = "clk_rk3288",
689 .of_match = rk3288_clk_ids,
690 .priv_auto_alloc_size = sizeof(struct rk3288_clk_priv),
691 .platdata_auto_alloc_size = sizeof(struct rk3288_clk_plat),
692 .ops = &rk3288_clk_ops,
693 .bind = rk3288_clk_bind,
694 .probe = rk3288_clk_probe,