1 // SPDX-License-Identifier: GPL-2.0
3 * (C) Copyright 2015 Google, Inc
4 * (C) 2017 Theobroma Systems Design und Consulting GmbH
8 #include <clk-uclass.h>
10 #include <dt-structs.h>
18 #include <asm/arch-rockchip/clock.h>
19 #include <asm/arch-rockchip/cru.h>
20 #include <asm/arch-rockchip/hardware.h>
22 #include <dt-bindings/clock/rk3399-cru.h>
23 #include <linux/delay.h>
25 #if CONFIG_IS_ENABLED(OF_PLATDATA)
26 struct rk3399_clk_plat {
27 struct dtd_rockchip_rk3399_cru dtd;
30 struct rk3399_pmuclk_plat {
31 struct dtd_rockchip_rk3399_pmucru dtd;
43 #define RATE_TO_DIV(input_rate, output_rate) \
44 ((input_rate) / (output_rate) - 1)
45 #define DIV_TO_RATE(input_rate, div) ((input_rate) / ((div) + 1))
47 #define PLL_DIVISORS(hz, _refdiv, _postdiv1, _postdiv2) {\
49 .fbdiv = (u32)((u64)hz * _refdiv * _postdiv1 * _postdiv2 / OSC_HZ),\
50 .postdiv1 = _postdiv1, .postdiv2 = _postdiv2};
52 #if defined(CONFIG_SPL_BUILD)
53 static const struct pll_div gpll_init_cfg = PLL_DIVISORS(GPLL_HZ, 2, 2, 1);
54 static const struct pll_div cpll_init_cfg = PLL_DIVISORS(CPLL_HZ, 1, 2, 2);
56 static const struct pll_div ppll_init_cfg = PLL_DIVISORS(PPLL_HZ, 2, 2, 1);
59 static const struct pll_div apll_l_1600_cfg = PLL_DIVISORS(1600 * MHz, 3, 1, 1);
60 static const struct pll_div apll_l_600_cfg = PLL_DIVISORS(600 * MHz, 1, 2, 1);
62 static const struct pll_div *apll_l_cfgs[] = {
63 [APLL_L_1600_MHZ] = &apll_l_1600_cfg,
64 [APLL_L_600_MHZ] = &apll_l_600_cfg,
67 static const struct pll_div apll_b_600_cfg = PLL_DIVISORS(600 * MHz, 1, 2, 1);
68 static const struct pll_div *apll_b_cfgs[] = {
69 [APLL_B_600_MHZ] = &apll_b_600_cfg,
74 PLL_FBDIV_MASK = 0xfff,
78 PLL_POSTDIV2_SHIFT = 12,
79 PLL_POSTDIV2_MASK = 0x7 << PLL_POSTDIV2_SHIFT,
80 PLL_POSTDIV1_SHIFT = 8,
81 PLL_POSTDIV1_MASK = 0x7 << PLL_POSTDIV1_SHIFT,
82 PLL_REFDIV_MASK = 0x3f,
86 PLL_LOCK_STATUS_SHIFT = 31,
87 PLL_LOCK_STATUS_MASK = 1 << PLL_LOCK_STATUS_SHIFT,
88 PLL_FRACDIV_MASK = 0xffffff,
89 PLL_FRACDIV_SHIFT = 0,
93 PLL_MODE_MASK = 3 << PLL_MODE_SHIFT,
98 PLL_DSMPD_MASK = 1 << PLL_DSMPD_SHIFT,
101 /* PMUCRU_CLKSEL_CON0 */
102 PMU_PCLK_DIV_CON_MASK = 0x1f,
103 PMU_PCLK_DIV_CON_SHIFT = 0,
105 /* PMUCRU_CLKSEL_CON1 */
106 SPI3_PLL_SEL_SHIFT = 7,
107 SPI3_PLL_SEL_MASK = 1 << SPI3_PLL_SEL_SHIFT,
108 SPI3_PLL_SEL_24M = 0,
109 SPI3_PLL_SEL_PPLL = 1,
110 SPI3_DIV_CON_SHIFT = 0x0,
111 SPI3_DIV_CON_MASK = 0x7f,
113 /* PMUCRU_CLKSEL_CON2 */
114 I2C_DIV_CON_MASK = 0x7f,
115 CLK_I2C8_DIV_CON_SHIFT = 8,
116 CLK_I2C0_DIV_CON_SHIFT = 0,
118 /* PMUCRU_CLKSEL_CON3 */
119 CLK_I2C4_DIV_CON_SHIFT = 0,
122 ACLKM_CORE_L_DIV_CON_SHIFT = 8,
123 ACLKM_CORE_L_DIV_CON_MASK = 0x1f << ACLKM_CORE_L_DIV_CON_SHIFT,
124 CLK_CORE_L_PLL_SEL_SHIFT = 6,
125 CLK_CORE_L_PLL_SEL_MASK = 3 << CLK_CORE_L_PLL_SEL_SHIFT,
126 CLK_CORE_L_PLL_SEL_ALPLL = 0x0,
127 CLK_CORE_L_PLL_SEL_ABPLL = 0x1,
128 CLK_CORE_L_PLL_SEL_DPLL = 0x10,
129 CLK_CORE_L_PLL_SEL_GPLL = 0x11,
130 CLK_CORE_L_DIV_MASK = 0x1f,
131 CLK_CORE_L_DIV_SHIFT = 0,
134 PCLK_DBG_L_DIV_SHIFT = 0x8,
135 PCLK_DBG_L_DIV_MASK = 0x1f << PCLK_DBG_L_DIV_SHIFT,
136 ATCLK_CORE_L_DIV_SHIFT = 0,
137 ATCLK_CORE_L_DIV_MASK = 0x1f << ATCLK_CORE_L_DIV_SHIFT,
140 ACLKM_CORE_B_DIV_CON_SHIFT = 8,
141 ACLKM_CORE_B_DIV_CON_MASK = 0x1f << ACLKM_CORE_B_DIV_CON_SHIFT,
142 CLK_CORE_B_PLL_SEL_SHIFT = 6,
143 CLK_CORE_B_PLL_SEL_MASK = 3 << CLK_CORE_B_PLL_SEL_SHIFT,
144 CLK_CORE_B_PLL_SEL_ALPLL = 0x0,
145 CLK_CORE_B_PLL_SEL_ABPLL = 0x1,
146 CLK_CORE_B_PLL_SEL_DPLL = 0x10,
147 CLK_CORE_B_PLL_SEL_GPLL = 0x11,
148 CLK_CORE_B_DIV_MASK = 0x1f,
149 CLK_CORE_B_DIV_SHIFT = 0,
152 PCLK_DBG_B_DIV_SHIFT = 0x8,
153 PCLK_DBG_B_DIV_MASK = 0x1f << PCLK_DBG_B_DIV_SHIFT,
154 ATCLK_CORE_B_DIV_SHIFT = 0,
155 ATCLK_CORE_B_DIV_MASK = 0x1f << ATCLK_CORE_B_DIV_SHIFT,
158 PCLK_PERIHP_DIV_CON_SHIFT = 12,
159 PCLK_PERIHP_DIV_CON_MASK = 0x7 << PCLK_PERIHP_DIV_CON_SHIFT,
160 HCLK_PERIHP_DIV_CON_SHIFT = 8,
161 HCLK_PERIHP_DIV_CON_MASK = 3 << HCLK_PERIHP_DIV_CON_SHIFT,
162 ACLK_PERIHP_PLL_SEL_SHIFT = 7,
163 ACLK_PERIHP_PLL_SEL_MASK = 1 << ACLK_PERIHP_PLL_SEL_SHIFT,
164 ACLK_PERIHP_PLL_SEL_CPLL = 0,
165 ACLK_PERIHP_PLL_SEL_GPLL = 1,
166 ACLK_PERIHP_DIV_CON_SHIFT = 0,
167 ACLK_PERIHP_DIV_CON_MASK = 0x1f,
170 ACLK_EMMC_PLL_SEL_SHIFT = 7,
171 ACLK_EMMC_PLL_SEL_MASK = 0x1 << ACLK_EMMC_PLL_SEL_SHIFT,
172 ACLK_EMMC_PLL_SEL_GPLL = 0x1,
173 ACLK_EMMC_DIV_CON_SHIFT = 0,
174 ACLK_EMMC_DIV_CON_MASK = 0x1f,
177 CLK_EMMC_PLL_SHIFT = 8,
178 CLK_EMMC_PLL_MASK = 0x7 << CLK_EMMC_PLL_SHIFT,
179 CLK_EMMC_PLL_SEL_GPLL = 0x1,
180 CLK_EMMC_PLL_SEL_24M = 0x5,
181 CLK_EMMC_DIV_CON_SHIFT = 0,
182 CLK_EMMC_DIV_CON_MASK = 0x7f << CLK_EMMC_DIV_CON_SHIFT,
185 PCLK_PERILP0_DIV_CON_SHIFT = 12,
186 PCLK_PERILP0_DIV_CON_MASK = 0x7 << PCLK_PERILP0_DIV_CON_SHIFT,
187 HCLK_PERILP0_DIV_CON_SHIFT = 8,
188 HCLK_PERILP0_DIV_CON_MASK = 3 << HCLK_PERILP0_DIV_CON_SHIFT,
189 ACLK_PERILP0_PLL_SEL_SHIFT = 7,
190 ACLK_PERILP0_PLL_SEL_MASK = 1 << ACLK_PERILP0_PLL_SEL_SHIFT,
191 ACLK_PERILP0_PLL_SEL_CPLL = 0,
192 ACLK_PERILP0_PLL_SEL_GPLL = 1,
193 ACLK_PERILP0_DIV_CON_SHIFT = 0,
194 ACLK_PERILP0_DIV_CON_MASK = 0x1f,
197 PCLK_PERILP1_DIV_CON_SHIFT = 8,
198 PCLK_PERILP1_DIV_CON_MASK = 0x7 << PCLK_PERILP1_DIV_CON_SHIFT,
199 HCLK_PERILP1_PLL_SEL_SHIFT = 7,
200 HCLK_PERILP1_PLL_SEL_MASK = 1 << HCLK_PERILP1_PLL_SEL_SHIFT,
201 HCLK_PERILP1_PLL_SEL_CPLL = 0,
202 HCLK_PERILP1_PLL_SEL_GPLL = 1,
203 HCLK_PERILP1_DIV_CON_SHIFT = 0,
204 HCLK_PERILP1_DIV_CON_MASK = 0x1f,
207 CLK_SARADC_DIV_CON_SHIFT = 8,
208 CLK_SARADC_DIV_CON_MASK = GENMASK(15, 8),
209 CLK_SARADC_DIV_CON_WIDTH = 8,
212 CLK_TSADC_SEL_X24M = 0x0,
213 CLK_TSADC_SEL_SHIFT = 15,
214 CLK_TSADC_SEL_MASK = 1 << CLK_TSADC_SEL_SHIFT,
215 CLK_TSADC_DIV_CON_SHIFT = 0,
216 CLK_TSADC_DIV_CON_MASK = 0x3ff,
218 /* CLKSEL_CON47 & CLKSEL_CON48 */
219 ACLK_VOP_PLL_SEL_SHIFT = 6,
220 ACLK_VOP_PLL_SEL_MASK = 0x3 << ACLK_VOP_PLL_SEL_SHIFT,
221 ACLK_VOP_PLL_SEL_CPLL = 0x1,
222 ACLK_VOP_DIV_CON_SHIFT = 0,
223 ACLK_VOP_DIV_CON_MASK = 0x1f << ACLK_VOP_DIV_CON_SHIFT,
225 /* CLKSEL_CON49 & CLKSEL_CON50 */
226 DCLK_VOP_DCLK_SEL_SHIFT = 11,
227 DCLK_VOP_DCLK_SEL_MASK = 1 << DCLK_VOP_DCLK_SEL_SHIFT,
228 DCLK_VOP_DCLK_SEL_DIVOUT = 0,
229 DCLK_VOP_PLL_SEL_SHIFT = 8,
230 DCLK_VOP_PLL_SEL_MASK = 3 << DCLK_VOP_PLL_SEL_SHIFT,
231 DCLK_VOP_PLL_SEL_VPLL = 0,
232 DCLK_VOP_DIV_CON_MASK = 0xff,
233 DCLK_VOP_DIV_CON_SHIFT = 0,
236 CLK_SPI_PLL_SEL_WIDTH = 1,
237 CLK_SPI_PLL_SEL_MASK = ((1 < CLK_SPI_PLL_SEL_WIDTH) - 1),
238 CLK_SPI_PLL_SEL_CPLL = 0,
239 CLK_SPI_PLL_SEL_GPLL = 1,
240 CLK_SPI_PLL_DIV_CON_WIDTH = 7,
241 CLK_SPI_PLL_DIV_CON_MASK = ((1 << CLK_SPI_PLL_DIV_CON_WIDTH) - 1),
243 CLK_SPI5_PLL_DIV_CON_SHIFT = 8,
244 CLK_SPI5_PLL_SEL_SHIFT = 15,
247 CLK_SPI1_PLL_SEL_SHIFT = 15,
248 CLK_SPI1_PLL_DIV_CON_SHIFT = 8,
249 CLK_SPI0_PLL_SEL_SHIFT = 7,
250 CLK_SPI0_PLL_DIV_CON_SHIFT = 0,
253 CLK_SPI4_PLL_SEL_SHIFT = 15,
254 CLK_SPI4_PLL_DIV_CON_SHIFT = 8,
255 CLK_SPI2_PLL_SEL_SHIFT = 7,
256 CLK_SPI2_PLL_DIV_CON_SHIFT = 0,
259 CLK_I2C_PLL_SEL_MASK = 1,
260 CLK_I2C_PLL_SEL_CPLL = 0,
261 CLK_I2C_PLL_SEL_GPLL = 1,
262 CLK_I2C5_PLL_SEL_SHIFT = 15,
263 CLK_I2C5_DIV_CON_SHIFT = 8,
264 CLK_I2C1_PLL_SEL_SHIFT = 7,
265 CLK_I2C1_DIV_CON_SHIFT = 0,
268 CLK_I2C6_PLL_SEL_SHIFT = 15,
269 CLK_I2C6_DIV_CON_SHIFT = 8,
270 CLK_I2C2_PLL_SEL_SHIFT = 7,
271 CLK_I2C2_DIV_CON_SHIFT = 0,
274 CLK_I2C7_PLL_SEL_SHIFT = 15,
275 CLK_I2C7_DIV_CON_SHIFT = 8,
276 CLK_I2C3_PLL_SEL_SHIFT = 7,
277 CLK_I2C3_DIV_CON_SHIFT = 0,
279 /* CRU_SOFTRST_CON4 */
280 RESETN_DDR0_REQ_SHIFT = 8,
281 RESETN_DDR0_REQ_MASK = 1 << RESETN_DDR0_REQ_SHIFT,
282 RESETN_DDRPHY0_REQ_SHIFT = 9,
283 RESETN_DDRPHY0_REQ_MASK = 1 << RESETN_DDRPHY0_REQ_SHIFT,
284 RESETN_DDR1_REQ_SHIFT = 12,
285 RESETN_DDR1_REQ_MASK = 1 << RESETN_DDR1_REQ_SHIFT,
286 RESETN_DDRPHY1_REQ_SHIFT = 13,
287 RESETN_DDRPHY1_REQ_MASK = 1 << RESETN_DDRPHY1_REQ_SHIFT,
290 #define VCO_MAX_KHZ (3200 * (MHz / KHz))
291 #define VCO_MIN_KHZ (800 * (MHz / KHz))
292 #define OUTPUT_MAX_KHZ (3200 * (MHz / KHz))
293 #define OUTPUT_MIN_KHZ (16 * (MHz / KHz))
296 * the div restructions of pll in integer mode, these are defined in
297 * * CRU_*PLL_CON0 or PMUCRU_*PLL_CON0
299 #define PLL_DIV_MIN 16
300 #define PLL_DIV_MAX 3200
303 * How to calculate the PLL(from TRM V0.3 Part 1 Page 63):
304 * Formulas also embedded within the Fractional PLL Verilog model:
305 * If DSMPD = 1 (DSM is disabled, "integer mode")
306 * FOUTVCO = FREF / REFDIV * FBDIV
307 * FOUTPOSTDIV = FOUTVCO / POSTDIV1 / POSTDIV2
309 * FOUTVCO = Fractional PLL non-divided output frequency
310 * FOUTPOSTDIV = Fractional PLL divided output frequency
311 * (output of second post divider)
312 * FREF = Fractional PLL input reference frequency, (the OSC_HZ 24MHz input)
313 * REFDIV = Fractional PLL input reference clock divider
314 * FBDIV = Integer value programmed into feedback divide
317 static void rkclk_set_pll(u32 *pll_con, const struct pll_div *div)
319 /* All 8 PLLs have same VCO and output frequency range restrictions. */
320 u32 vco_khz = OSC_HZ / 1000 * div->fbdiv / div->refdiv;
321 u32 output_khz = vco_khz / div->postdiv1 / div->postdiv2;
323 debug("PLL at %p: fbdiv=%d, refdiv=%d, postdiv1=%d, "
324 "postdiv2=%d, vco=%u khz, output=%u khz\n",
325 pll_con, div->fbdiv, div->refdiv, div->postdiv1,
326 div->postdiv2, vco_khz, output_khz);
327 assert(vco_khz >= VCO_MIN_KHZ && vco_khz <= VCO_MAX_KHZ &&
328 output_khz >= OUTPUT_MIN_KHZ && output_khz <= OUTPUT_MAX_KHZ &&
329 div->fbdiv >= PLL_DIV_MIN && div->fbdiv <= PLL_DIV_MAX);
332 * When power on or changing PLL setting,
333 * we must force PLL into slow mode to ensure output stable clock.
335 rk_clrsetreg(&pll_con[3], PLL_MODE_MASK,
336 PLL_MODE_SLOW << PLL_MODE_SHIFT);
338 /* use integer mode */
339 rk_clrsetreg(&pll_con[3], PLL_DSMPD_MASK,
340 PLL_INTEGER_MODE << PLL_DSMPD_SHIFT);
342 rk_clrsetreg(&pll_con[0], PLL_FBDIV_MASK,
343 div->fbdiv << PLL_FBDIV_SHIFT);
344 rk_clrsetreg(&pll_con[1],
345 PLL_POSTDIV2_MASK | PLL_POSTDIV1_MASK |
346 PLL_REFDIV_MASK | PLL_REFDIV_SHIFT,
347 (div->postdiv2 << PLL_POSTDIV2_SHIFT) |
348 (div->postdiv1 << PLL_POSTDIV1_SHIFT) |
349 (div->refdiv << PLL_REFDIV_SHIFT));
351 /* waiting for pll lock */
352 while (!(readl(&pll_con[2]) & (1 << PLL_LOCK_STATUS_SHIFT)))
355 /* pll enter normal mode */
356 rk_clrsetreg(&pll_con[3], PLL_MODE_MASK,
357 PLL_MODE_NORM << PLL_MODE_SHIFT);
360 static int pll_para_config(u32 freq_hz, struct pll_div *div)
362 u32 ref_khz = OSC_HZ / KHz, refdiv, fbdiv = 0;
363 u32 postdiv1, postdiv2 = 1;
365 u32 diff_khz, best_diff_khz;
366 const u32 max_refdiv = 63, max_fbdiv = 3200, min_fbdiv = 16;
367 const u32 max_postdiv1 = 7, max_postdiv2 = 7;
369 u32 freq_khz = freq_hz / KHz;
372 printf("%s: the frequency can't be 0 Hz\n", __func__);
376 postdiv1 = DIV_ROUND_UP(VCO_MIN_KHZ, freq_khz);
377 if (postdiv1 > max_postdiv1) {
378 postdiv2 = DIV_ROUND_UP(postdiv1, max_postdiv1);
379 postdiv1 = DIV_ROUND_UP(postdiv1, postdiv2);
382 vco_khz = freq_khz * postdiv1 * postdiv2;
384 if (vco_khz < VCO_MIN_KHZ || vco_khz > VCO_MAX_KHZ ||
385 postdiv2 > max_postdiv2) {
386 printf("%s: Cannot find out a supported VCO"
387 " for Frequency (%uHz).\n", __func__, freq_hz);
391 div->postdiv1 = postdiv1;
392 div->postdiv2 = postdiv2;
394 best_diff_khz = vco_khz;
395 for (refdiv = 1; refdiv < max_refdiv && best_diff_khz; refdiv++) {
396 fref_khz = ref_khz / refdiv;
398 fbdiv = vco_khz / fref_khz;
399 if (fbdiv >= max_fbdiv || fbdiv <= min_fbdiv)
401 diff_khz = vco_khz - fbdiv * fref_khz;
402 if (fbdiv + 1 < max_fbdiv && diff_khz > fref_khz / 2) {
404 diff_khz = fref_khz - diff_khz;
407 if (diff_khz >= best_diff_khz)
410 best_diff_khz = diff_khz;
411 div->refdiv = refdiv;
415 if (best_diff_khz > 4 * (MHz / KHz)) {
416 printf("%s: Failed to match output frequency %u, "
417 "difference is %u Hz,exceed 4MHZ\n", __func__, freq_hz,
418 best_diff_khz * KHz);
424 void rk3399_configure_cpu_l(struct rockchip_cru *cru,
425 enum apll_l_frequencies apll_l_freq)
431 /* Setup cluster L */
432 rkclk_set_pll(&cru->apll_l_con[0], apll_l_cfgs[apll_l_freq]);
434 aclkm_div = LPLL_HZ / ACLKM_CORE_L_HZ - 1;
435 assert((aclkm_div + 1) * ACLKM_CORE_L_HZ == LPLL_HZ &&
438 pclk_dbg_div = LPLL_HZ / PCLK_DBG_L_HZ - 1;
439 assert((pclk_dbg_div + 1) * PCLK_DBG_L_HZ == LPLL_HZ &&
440 pclk_dbg_div < 0x1f);
442 atclk_div = LPLL_HZ / ATCLK_CORE_L_HZ - 1;
443 assert((atclk_div + 1) * ATCLK_CORE_L_HZ == LPLL_HZ &&
446 rk_clrsetreg(&cru->clksel_con[0],
447 ACLKM_CORE_L_DIV_CON_MASK | CLK_CORE_L_PLL_SEL_MASK |
449 aclkm_div << ACLKM_CORE_L_DIV_CON_SHIFT |
450 CLK_CORE_L_PLL_SEL_ALPLL << CLK_CORE_L_PLL_SEL_SHIFT |
451 0 << CLK_CORE_L_DIV_SHIFT);
453 rk_clrsetreg(&cru->clksel_con[1],
454 PCLK_DBG_L_DIV_MASK | ATCLK_CORE_L_DIV_MASK,
455 pclk_dbg_div << PCLK_DBG_L_DIV_SHIFT |
456 atclk_div << ATCLK_CORE_L_DIV_SHIFT);
459 void rk3399_configure_cpu_b(struct rockchip_cru *cru,
460 enum apll_b_frequencies apll_b_freq)
466 /* Setup cluster B */
467 rkclk_set_pll(&cru->apll_b_con[0], apll_b_cfgs[apll_b_freq]);
469 aclkm_div = BPLL_HZ / ACLKM_CORE_B_HZ - 1;
470 assert((aclkm_div + 1) * ACLKM_CORE_B_HZ == BPLL_HZ &&
473 pclk_dbg_div = BPLL_HZ / PCLK_DBG_B_HZ - 1;
474 assert((pclk_dbg_div + 1) * PCLK_DBG_B_HZ == BPLL_HZ &&
475 pclk_dbg_div < 0x1f);
477 atclk_div = BPLL_HZ / ATCLK_CORE_B_HZ - 1;
478 assert((atclk_div + 1) * ATCLK_CORE_B_HZ == BPLL_HZ &&
481 rk_clrsetreg(&cru->clksel_con[2],
482 ACLKM_CORE_B_DIV_CON_MASK | CLK_CORE_B_PLL_SEL_MASK |
484 aclkm_div << ACLKM_CORE_B_DIV_CON_SHIFT |
485 CLK_CORE_B_PLL_SEL_ABPLL << CLK_CORE_B_PLL_SEL_SHIFT |
486 0 << CLK_CORE_B_DIV_SHIFT);
488 rk_clrsetreg(&cru->clksel_con[3],
489 PCLK_DBG_B_DIV_MASK | ATCLK_CORE_B_DIV_MASK,
490 pclk_dbg_div << PCLK_DBG_B_DIV_SHIFT |
491 atclk_div << ATCLK_CORE_B_DIV_SHIFT);
494 #define I2C_CLK_REG_MASK(bus) \
495 (I2C_DIV_CON_MASK << CLK_I2C ##bus## _DIV_CON_SHIFT | \
496 CLK_I2C_PLL_SEL_MASK << CLK_I2C ##bus## _PLL_SEL_SHIFT)
498 #define I2C_CLK_REG_VALUE(bus, clk_div) \
499 ((clk_div - 1) << CLK_I2C ##bus## _DIV_CON_SHIFT | \
500 CLK_I2C_PLL_SEL_GPLL << CLK_I2C ##bus## _PLL_SEL_SHIFT)
502 #define I2C_CLK_DIV_VALUE(con, bus) \
503 ((con >> CLK_I2C ##bus## _DIV_CON_SHIFT) & I2C_DIV_CON_MASK)
505 #define I2C_PMUCLK_REG_MASK(bus) \
506 (I2C_DIV_CON_MASK << CLK_I2C ##bus## _DIV_CON_SHIFT)
508 #define I2C_PMUCLK_REG_VALUE(bus, clk_div) \
509 ((clk_div - 1) << CLK_I2C ##bus## _DIV_CON_SHIFT)
511 static ulong rk3399_i2c_get_clk(struct rockchip_cru *cru, ulong clk_id)
517 con = readl(&cru->clksel_con[61]);
518 div = I2C_CLK_DIV_VALUE(con, 1);
521 con = readl(&cru->clksel_con[62]);
522 div = I2C_CLK_DIV_VALUE(con, 2);
525 con = readl(&cru->clksel_con[63]);
526 div = I2C_CLK_DIV_VALUE(con, 3);
529 con = readl(&cru->clksel_con[61]);
530 div = I2C_CLK_DIV_VALUE(con, 5);
533 con = readl(&cru->clksel_con[62]);
534 div = I2C_CLK_DIV_VALUE(con, 6);
537 con = readl(&cru->clksel_con[63]);
538 div = I2C_CLK_DIV_VALUE(con, 7);
541 printf("do not support this i2c bus\n");
545 return DIV_TO_RATE(GPLL_HZ, div);
548 static ulong rk3399_i2c_set_clk(struct rockchip_cru *cru, ulong clk_id, uint hz)
552 /* i2c0,4,8 src clock from ppll, i2c1,2,3,5,6,7 src clock from gpll*/
553 src_clk_div = GPLL_HZ / hz;
554 assert(src_clk_div - 1 < 127);
558 rk_clrsetreg(&cru->clksel_con[61], I2C_CLK_REG_MASK(1),
559 I2C_CLK_REG_VALUE(1, src_clk_div));
562 rk_clrsetreg(&cru->clksel_con[62], I2C_CLK_REG_MASK(2),
563 I2C_CLK_REG_VALUE(2, src_clk_div));
566 rk_clrsetreg(&cru->clksel_con[63], I2C_CLK_REG_MASK(3),
567 I2C_CLK_REG_VALUE(3, src_clk_div));
570 rk_clrsetreg(&cru->clksel_con[61], I2C_CLK_REG_MASK(5),
571 I2C_CLK_REG_VALUE(5, src_clk_div));
574 rk_clrsetreg(&cru->clksel_con[62], I2C_CLK_REG_MASK(6),
575 I2C_CLK_REG_VALUE(6, src_clk_div));
578 rk_clrsetreg(&cru->clksel_con[63], I2C_CLK_REG_MASK(7),
579 I2C_CLK_REG_VALUE(7, src_clk_div));
582 printf("do not support this i2c bus\n");
586 return rk3399_i2c_get_clk(cru, clk_id);
590 * RK3399 SPI clocks have a common divider-width (7 bits) and a single bit
591 * to select either CPLL or GPLL as the clock-parent. The location within
592 * the enclosing CLKSEL_CON (i.e. div_shift and sel_shift) are variable.
596 u8 reg; /* CLKSEL_CON[reg] register in CRU */
602 * The entries are numbered relative to their offset from SCLK_SPI0.
604 * Note that SCLK_SPI3 (which is configured via PMUCRU and requires different
605 * logic is not supported).
607 static const struct spi_clkreg spi_clkregs[] = {
609 .div_shift = CLK_SPI0_PLL_DIV_CON_SHIFT,
610 .sel_shift = CLK_SPI0_PLL_SEL_SHIFT, },
612 .div_shift = CLK_SPI1_PLL_DIV_CON_SHIFT,
613 .sel_shift = CLK_SPI1_PLL_SEL_SHIFT, },
615 .div_shift = CLK_SPI2_PLL_DIV_CON_SHIFT,
616 .sel_shift = CLK_SPI2_PLL_SEL_SHIFT, },
618 .div_shift = CLK_SPI4_PLL_DIV_CON_SHIFT,
619 .sel_shift = CLK_SPI4_PLL_SEL_SHIFT, },
621 .div_shift = CLK_SPI5_PLL_DIV_CON_SHIFT,
622 .sel_shift = CLK_SPI5_PLL_SEL_SHIFT, },
625 static ulong rk3399_spi_get_clk(struct rockchip_cru *cru, ulong clk_id)
627 const struct spi_clkreg *spiclk = NULL;
631 case SCLK_SPI0 ... SCLK_SPI5:
632 spiclk = &spi_clkregs[clk_id - SCLK_SPI0];
636 pr_err("%s: SPI clk-id %ld not supported\n", __func__, clk_id);
640 val = readl(&cru->clksel_con[spiclk->reg]);
641 div = bitfield_extract(val, spiclk->div_shift,
642 CLK_SPI_PLL_DIV_CON_WIDTH);
644 return DIV_TO_RATE(GPLL_HZ, div);
647 static ulong rk3399_spi_set_clk(struct rockchip_cru *cru, ulong clk_id, uint hz)
649 const struct spi_clkreg *spiclk = NULL;
652 src_clk_div = DIV_ROUND_UP(GPLL_HZ, hz) - 1;
653 assert(src_clk_div < 128);
656 case SCLK_SPI1 ... SCLK_SPI5:
657 spiclk = &spi_clkregs[clk_id - SCLK_SPI0];
661 pr_err("%s: SPI clk-id %ld not supported\n", __func__, clk_id);
665 rk_clrsetreg(&cru->clksel_con[spiclk->reg],
666 ((CLK_SPI_PLL_DIV_CON_MASK << spiclk->div_shift) |
667 (CLK_SPI_PLL_SEL_GPLL << spiclk->sel_shift)),
668 ((src_clk_div << spiclk->div_shift) |
669 (CLK_SPI_PLL_SEL_GPLL << spiclk->sel_shift)));
671 return rk3399_spi_get_clk(cru, clk_id);
674 static ulong rk3399_vop_set_clk(struct rockchip_cru *cru, ulong clk_id, u32 hz)
676 struct pll_div vpll_config = {0};
677 int aclk_vop = 198 * MHz;
678 void *aclkreg_addr, *dclkreg_addr;
683 aclkreg_addr = &cru->clksel_con[47];
684 dclkreg_addr = &cru->clksel_con[49];
687 aclkreg_addr = &cru->clksel_con[48];
688 dclkreg_addr = &cru->clksel_con[50];
693 /* vop aclk source clk: cpll */
694 div = CPLL_HZ / aclk_vop;
695 assert(div - 1 < 32);
697 rk_clrsetreg(aclkreg_addr,
698 ACLK_VOP_PLL_SEL_MASK | ACLK_VOP_DIV_CON_MASK,
699 ACLK_VOP_PLL_SEL_CPLL << ACLK_VOP_PLL_SEL_SHIFT |
700 (div - 1) << ACLK_VOP_DIV_CON_SHIFT);
702 /* vop dclk source from vpll, and equals to vpll(means div == 1) */
703 if (pll_para_config(hz, &vpll_config))
706 rkclk_set_pll(&cru->vpll_con[0], &vpll_config);
708 rk_clrsetreg(dclkreg_addr,
709 DCLK_VOP_DCLK_SEL_MASK | DCLK_VOP_PLL_SEL_MASK |
710 DCLK_VOP_DIV_CON_MASK,
711 DCLK_VOP_DCLK_SEL_DIVOUT << DCLK_VOP_DCLK_SEL_SHIFT |
712 DCLK_VOP_PLL_SEL_VPLL << DCLK_VOP_PLL_SEL_SHIFT |
713 (1 - 1) << DCLK_VOP_DIV_CON_SHIFT);
718 static ulong rk3399_mmc_get_clk(struct rockchip_cru *cru, uint clk_id)
725 con = readl(&cru->clksel_con[16]);
726 /* dwmmc controller have internal div 2 */
730 con = readl(&cru->clksel_con[21]);
737 div *= (con & CLK_EMMC_DIV_CON_MASK) >> CLK_EMMC_DIV_CON_SHIFT;
738 if ((con & CLK_EMMC_PLL_MASK) >> CLK_EMMC_PLL_SHIFT
739 == CLK_EMMC_PLL_SEL_24M)
740 return DIV_TO_RATE(OSC_HZ, div);
742 return DIV_TO_RATE(GPLL_HZ, div);
745 static ulong rk3399_mmc_set_clk(struct rockchip_cru *cru,
746 ulong clk_id, ulong set_rate)
749 int aclk_emmc = 198 * MHz;
754 /* Select clk_sdmmc source from GPLL by default */
755 /* mmc clock defaulg div 2 internal, provide double in cru */
756 src_clk_div = DIV_ROUND_UP(GPLL_HZ / 2, set_rate);
758 if (src_clk_div > 128) {
759 /* use 24MHz source for 400KHz clock */
760 src_clk_div = DIV_ROUND_UP(OSC_HZ / 2, set_rate);
761 assert(src_clk_div - 1 < 128);
762 rk_clrsetreg(&cru->clksel_con[16],
763 CLK_EMMC_PLL_MASK | CLK_EMMC_DIV_CON_MASK,
764 CLK_EMMC_PLL_SEL_24M << CLK_EMMC_PLL_SHIFT |
765 (src_clk_div - 1) << CLK_EMMC_DIV_CON_SHIFT);
767 rk_clrsetreg(&cru->clksel_con[16],
768 CLK_EMMC_PLL_MASK | CLK_EMMC_DIV_CON_MASK,
769 CLK_EMMC_PLL_SEL_GPLL << CLK_EMMC_PLL_SHIFT |
770 (src_clk_div - 1) << CLK_EMMC_DIV_CON_SHIFT);
774 /* Select aclk_emmc source from GPLL */
775 src_clk_div = DIV_ROUND_UP(GPLL_HZ, aclk_emmc);
776 assert(src_clk_div - 1 < 32);
778 rk_clrsetreg(&cru->clksel_con[21],
779 ACLK_EMMC_PLL_SEL_MASK | ACLK_EMMC_DIV_CON_MASK,
780 ACLK_EMMC_PLL_SEL_GPLL << ACLK_EMMC_PLL_SEL_SHIFT |
781 (src_clk_div - 1) << ACLK_EMMC_DIV_CON_SHIFT);
783 /* Select clk_emmc source from GPLL too */
784 src_clk_div = DIV_ROUND_UP(GPLL_HZ, set_rate);
785 assert(src_clk_div - 1 < 128);
787 rk_clrsetreg(&cru->clksel_con[22],
788 CLK_EMMC_PLL_MASK | CLK_EMMC_DIV_CON_MASK,
789 CLK_EMMC_PLL_SEL_GPLL << CLK_EMMC_PLL_SHIFT |
790 (src_clk_div - 1) << CLK_EMMC_DIV_CON_SHIFT);
795 return rk3399_mmc_get_clk(cru, clk_id);
798 static ulong rk3399_gmac_set_clk(struct rockchip_cru *cru, ulong rate)
803 * The RGMII CLK can be derived either from an external "clkin"
804 * or can be generated from internally by a divider from SCLK_MAC.
806 if (readl(&cru->clksel_con[19]) & BIT(4)) {
807 /* An external clock will always generate the right rate... */
811 * No platform uses an internal clock to date.
812 * Implement this once it becomes necessary and print an error
813 * if someone tries to use it (while it remains unimplemented).
815 pr_err("%s: internal clock is UNIMPLEMENTED\n", __func__);
822 #define PMUSGRF_DDR_RGN_CON16 0xff330040
823 static ulong rk3399_ddr_set_clk(struct rockchip_cru *cru,
826 struct pll_div dpll_cfg;
828 /* IC ECO bug, need to set this register */
829 writel(0xc000c000, PMUSGRF_DDR_RGN_CON16);
831 /* clk_ddrc == DPLL = 24MHz / refdiv * fbdiv / postdiv1 / postdiv2 */
834 dpll_cfg = (struct pll_div)
835 {.refdiv = 1, .fbdiv = 12, .postdiv1 = 3, .postdiv2 = 2};
838 dpll_cfg = (struct pll_div)
839 {.refdiv = 1, .fbdiv = 50, .postdiv1 = 6, .postdiv2 = 1};
842 dpll_cfg = (struct pll_div)
843 {.refdiv = 2, .fbdiv = 100, .postdiv1 = 4, .postdiv2 = 1};
846 dpll_cfg = (struct pll_div)
847 {.refdiv = 1, .fbdiv = 50, .postdiv1 = 3, .postdiv2 = 1};
850 dpll_cfg = (struct pll_div)
851 {.refdiv = 2, .fbdiv = 111, .postdiv1 = 2, .postdiv2 = 1};
854 dpll_cfg = (struct pll_div)
855 {.refdiv = 1, .fbdiv = 100, .postdiv1 = 3, .postdiv2 = 1};
858 dpll_cfg = (struct pll_div)
859 {.refdiv = 1, .fbdiv = 116, .postdiv1 = 3, .postdiv2 = 1};
862 pr_err("Unsupported SDRAM frequency!,%ld\n", set_rate);
864 rkclk_set_pll(&cru->dpll_con[0], &dpll_cfg);
869 static ulong rk3399_saradc_get_clk(struct rockchip_cru *cru)
873 val = readl(&cru->clksel_con[26]);
874 div = bitfield_extract(val, CLK_SARADC_DIV_CON_SHIFT,
875 CLK_SARADC_DIV_CON_WIDTH);
877 return DIV_TO_RATE(OSC_HZ, div);
880 static ulong rk3399_saradc_set_clk(struct rockchip_cru *cru, uint hz)
884 src_clk_div = DIV_ROUND_UP(OSC_HZ, hz) - 1;
885 assert(src_clk_div < 128);
887 rk_clrsetreg(&cru->clksel_con[26],
888 CLK_SARADC_DIV_CON_MASK,
889 src_clk_div << CLK_SARADC_DIV_CON_SHIFT);
891 return rk3399_saradc_get_clk(cru);
894 static ulong rk3399_clk_get_rate(struct clk *clk)
896 struct rk3399_clk_priv *priv = dev_get_priv(clk->dev);
905 rate = rk3399_mmc_get_clk(priv->cru, clk->id);
913 rate = rk3399_i2c_get_clk(priv->cru, clk->id);
915 case SCLK_SPI0...SCLK_SPI5:
916 rate = rk3399_spi_get_clk(priv->cru, clk->id);
928 case PCLK_EFUSE1024NS:
931 rate = rk3399_saradc_get_clk(priv->cru);
939 log_debug("Unknown clock %lu\n", clk->id);
946 static ulong rk3399_clk_set_rate(struct clk *clk, ulong rate)
948 struct rk3399_clk_priv *priv = dev_get_priv(clk->dev);
975 ret = rk3399_mmc_set_clk(priv->cru, clk->id, rate);
978 ret = rk3399_gmac_set_clk(priv->cru, rate);
986 ret = rk3399_i2c_set_clk(priv->cru, clk->id, rate);
988 case SCLK_SPI0...SCLK_SPI5:
989 ret = rk3399_spi_set_clk(priv->cru, clk->id, rate);
993 /* the PCLK gates for video are enabled by default */
997 ret = rk3399_vop_set_clk(priv->cru, clk->id, rate);
1003 * assigned-clocks handling won't require for vopl, so
1004 * return 0 to satisfy clk_set_defaults during device probe.
1008 ret = rk3399_ddr_set_clk(priv->cru, rate);
1010 case PCLK_EFUSE1024NS:
1013 ret = rk3399_saradc_set_clk(priv->cru, rate);
1021 log_debug("Unknown clock %lu\n", clk->id);
1028 static int __maybe_unused rk3399_gmac_set_parent(struct clk *clk,
1031 struct rk3399_clk_priv *priv = dev_get_priv(clk->dev);
1032 const char *clock_output_name;
1036 * If the requested parent is in the same clock-controller and
1037 * the id is SCLK_MAC ("clk_gmac"), switch to the internal clock.
1039 if (parent->dev == clk->dev && parent->id == SCLK_MAC) {
1040 debug("%s: switching RGMII to SCLK_MAC\n", __func__);
1041 rk_clrreg(&priv->cru->clksel_con[19], BIT(4));
1046 * Otherwise, we need to check the clock-output-names of the
1047 * requested parent to see if the requested id is "clkin_gmac".
1049 ret = dev_read_string_index(parent->dev, "clock-output-names",
1050 parent->id, &clock_output_name);
1054 /* If this is "clkin_gmac", switch to the external clock input */
1055 if (!strcmp(clock_output_name, "clkin_gmac")) {
1056 debug("%s: switching RGMII to CLKIN\n", __func__);
1057 rk_setreg(&priv->cru->clksel_con[19], BIT(4));
1064 static int __maybe_unused rk3399_clk_set_parent(struct clk *clk,
1069 return rk3399_gmac_set_parent(clk, parent);
1072 debug("%s: unsupported clk %ld\n", __func__, clk->id);
1076 static struct clk_ops rk3399_clk_ops = {
1077 .get_rate = rk3399_clk_get_rate,
1078 .set_rate = rk3399_clk_set_rate,
1079 #if CONFIG_IS_ENABLED(OF_CONTROL) && !CONFIG_IS_ENABLED(OF_PLATDATA)
1080 .set_parent = rk3399_clk_set_parent,
1084 #ifdef CONFIG_SPL_BUILD
1085 static void rkclk_init(struct rockchip_cru *cru)
1091 rk3399_configure_cpu_l(cru, APLL_L_600_MHZ);
1092 rk3399_configure_cpu_b(cru, APLL_B_600_MHZ);
1094 * some cru registers changed by bootrom, we'd better reset them to
1095 * reset/default values described in TRM to avoid confusion in kernel.
1096 * Please consider these three lines as a fix of bootrom bug.
1098 rk_clrsetreg(&cru->clksel_con[12], 0xffff, 0x4101);
1099 rk_clrsetreg(&cru->clksel_con[19], 0xffff, 0x033f);
1100 rk_clrsetreg(&cru->clksel_con[56], 0x0003, 0x0003);
1102 /* configure gpll cpll */
1103 rkclk_set_pll(&cru->gpll_con[0], &gpll_init_cfg);
1104 rkclk_set_pll(&cru->cpll_con[0], &cpll_init_cfg);
1106 /* configure perihp aclk, hclk, pclk */
1107 aclk_div = GPLL_HZ / PERIHP_ACLK_HZ - 1;
1108 assert((aclk_div + 1) * PERIHP_ACLK_HZ == GPLL_HZ && aclk_div < 0x1f);
1110 hclk_div = PERIHP_ACLK_HZ / PERIHP_HCLK_HZ - 1;
1111 assert((hclk_div + 1) * PERIHP_HCLK_HZ ==
1112 PERIHP_ACLK_HZ && (hclk_div < 0x4));
1114 pclk_div = PERIHP_ACLK_HZ / PERIHP_PCLK_HZ - 1;
1115 assert((pclk_div + 1) * PERIHP_PCLK_HZ ==
1116 PERIHP_ACLK_HZ && (pclk_div < 0x7));
1118 rk_clrsetreg(&cru->clksel_con[14],
1119 PCLK_PERIHP_DIV_CON_MASK | HCLK_PERIHP_DIV_CON_MASK |
1120 ACLK_PERIHP_PLL_SEL_MASK | ACLK_PERIHP_DIV_CON_MASK,
1121 pclk_div << PCLK_PERIHP_DIV_CON_SHIFT |
1122 hclk_div << HCLK_PERIHP_DIV_CON_SHIFT |
1123 ACLK_PERIHP_PLL_SEL_GPLL << ACLK_PERIHP_PLL_SEL_SHIFT |
1124 aclk_div << ACLK_PERIHP_DIV_CON_SHIFT);
1126 /* configure perilp0 aclk, hclk, pclk */
1127 aclk_div = GPLL_HZ / PERILP0_ACLK_HZ - 1;
1128 assert((aclk_div + 1) * PERILP0_ACLK_HZ == GPLL_HZ && aclk_div < 0x1f);
1130 hclk_div = PERILP0_ACLK_HZ / PERILP0_HCLK_HZ - 1;
1131 assert((hclk_div + 1) * PERILP0_HCLK_HZ ==
1132 PERILP0_ACLK_HZ && (hclk_div < 0x4));
1134 pclk_div = PERILP0_ACLK_HZ / PERILP0_PCLK_HZ - 1;
1135 assert((pclk_div + 1) * PERILP0_PCLK_HZ ==
1136 PERILP0_ACLK_HZ && (pclk_div < 0x7));
1138 rk_clrsetreg(&cru->clksel_con[23],
1139 PCLK_PERILP0_DIV_CON_MASK | HCLK_PERILP0_DIV_CON_MASK |
1140 ACLK_PERILP0_PLL_SEL_MASK | ACLK_PERILP0_DIV_CON_MASK,
1141 pclk_div << PCLK_PERILP0_DIV_CON_SHIFT |
1142 hclk_div << HCLK_PERILP0_DIV_CON_SHIFT |
1143 ACLK_PERILP0_PLL_SEL_GPLL << ACLK_PERILP0_PLL_SEL_SHIFT |
1144 aclk_div << ACLK_PERILP0_DIV_CON_SHIFT);
1146 /* perilp1 hclk select gpll as source */
1147 hclk_div = GPLL_HZ / PERILP1_HCLK_HZ - 1;
1148 assert((hclk_div + 1) * PERILP1_HCLK_HZ ==
1149 GPLL_HZ && (hclk_div < 0x1f));
1151 pclk_div = PERILP1_HCLK_HZ / PERILP1_HCLK_HZ - 1;
1152 assert((pclk_div + 1) * PERILP1_HCLK_HZ ==
1153 PERILP1_HCLK_HZ && (hclk_div < 0x7));
1155 rk_clrsetreg(&cru->clksel_con[25],
1156 PCLK_PERILP1_DIV_CON_MASK | HCLK_PERILP1_DIV_CON_MASK |
1157 HCLK_PERILP1_PLL_SEL_MASK,
1158 pclk_div << PCLK_PERILP1_DIV_CON_SHIFT |
1159 hclk_div << HCLK_PERILP1_DIV_CON_SHIFT |
1160 HCLK_PERILP1_PLL_SEL_GPLL << HCLK_PERILP1_PLL_SEL_SHIFT);
1164 static int rk3399_clk_probe(struct udevice *dev)
1166 #ifdef CONFIG_SPL_BUILD
1167 struct rk3399_clk_priv *priv = dev_get_priv(dev);
1169 #if CONFIG_IS_ENABLED(OF_PLATDATA)
1170 struct rk3399_clk_plat *plat = dev_get_platdata(dev);
1172 priv->cru = map_sysmem(plat->dtd.reg[0], plat->dtd.reg[1]);
1174 rkclk_init(priv->cru);
1179 static int rk3399_clk_ofdata_to_platdata(struct udevice *dev)
1181 #if !CONFIG_IS_ENABLED(OF_PLATDATA)
1182 struct rk3399_clk_priv *priv = dev_get_priv(dev);
1184 priv->cru = dev_read_addr_ptr(dev);
1189 static int rk3399_clk_bind(struct udevice *dev)
1192 struct udevice *sys_child;
1193 struct sysreset_reg *priv;
1195 /* The reset driver does not have a device node, so bind it here */
1196 ret = device_bind_driver(dev, "rockchip_sysreset", "sysreset",
1199 debug("Warning: No sysreset driver: ret=%d\n", ret);
1201 priv = malloc(sizeof(struct sysreset_reg));
1202 priv->glb_srst_fst_value = offsetof(struct rockchip_cru,
1203 glb_srst_fst_value);
1204 priv->glb_srst_snd_value = offsetof(struct rockchip_cru,
1205 glb_srst_snd_value);
1206 sys_child->priv = priv;
1209 #if CONFIG_IS_ENABLED(RESET_ROCKCHIP)
1210 ret = offsetof(struct rockchip_cru, softrst_con[0]);
1211 ret = rockchip_reset_bind(dev, ret, 21);
1213 debug("Warning: software reset driver bind faile\n");
1219 static const struct udevice_id rk3399_clk_ids[] = {
1220 { .compatible = "rockchip,rk3399-cru" },
1224 U_BOOT_DRIVER(clk_rk3399) = {
1225 .name = "rockchip_rk3399_cru",
1227 .of_match = rk3399_clk_ids,
1228 .priv_auto_alloc_size = sizeof(struct rk3399_clk_priv),
1229 .ofdata_to_platdata = rk3399_clk_ofdata_to_platdata,
1230 .ops = &rk3399_clk_ops,
1231 .bind = rk3399_clk_bind,
1232 .probe = rk3399_clk_probe,
1233 #if CONFIG_IS_ENABLED(OF_PLATDATA)
1234 .platdata_auto_alloc_size = sizeof(struct rk3399_clk_plat),
1238 static ulong rk3399_i2c_get_pmuclk(struct rk3399_pmucru *pmucru, ulong clk_id)
1244 con = readl(&pmucru->pmucru_clksel[2]);
1245 div = I2C_CLK_DIV_VALUE(con, 0);
1248 con = readl(&pmucru->pmucru_clksel[3]);
1249 div = I2C_CLK_DIV_VALUE(con, 4);
1252 con = readl(&pmucru->pmucru_clksel[2]);
1253 div = I2C_CLK_DIV_VALUE(con, 8);
1256 printf("do not support this i2c bus\n");
1260 return DIV_TO_RATE(PPLL_HZ, div);
1263 static ulong rk3399_i2c_set_pmuclk(struct rk3399_pmucru *pmucru, ulong clk_id,
1268 src_clk_div = PPLL_HZ / hz;
1269 assert(src_clk_div - 1 < 127);
1273 rk_clrsetreg(&pmucru->pmucru_clksel[2], I2C_PMUCLK_REG_MASK(0),
1274 I2C_PMUCLK_REG_VALUE(0, src_clk_div));
1277 rk_clrsetreg(&pmucru->pmucru_clksel[3], I2C_PMUCLK_REG_MASK(4),
1278 I2C_PMUCLK_REG_VALUE(4, src_clk_div));
1281 rk_clrsetreg(&pmucru->pmucru_clksel[2], I2C_PMUCLK_REG_MASK(8),
1282 I2C_PMUCLK_REG_VALUE(8, src_clk_div));
1285 printf("do not support this i2c bus\n");
1289 return DIV_TO_RATE(PPLL_HZ, src_clk_div);
1292 static ulong rk3399_pwm_get_clk(struct rk3399_pmucru *pmucru)
1296 /* PWM closk rate is same as pclk_pmu */
1297 con = readl(&pmucru->pmucru_clksel[0]);
1298 div = con & PMU_PCLK_DIV_CON_MASK;
1300 return DIV_TO_RATE(PPLL_HZ, div);
1303 static ulong rk3399_pmuclk_get_rate(struct clk *clk)
1305 struct rk3399_pmuclk_priv *priv = dev_get_priv(clk->dev);
1311 case PCLK_RKPWM_PMU:
1312 rate = rk3399_pwm_get_clk(priv->pmucru);
1317 rate = rk3399_i2c_get_pmuclk(priv->pmucru, clk->id);
1326 static ulong rk3399_pmuclk_set_rate(struct clk *clk, ulong rate)
1328 struct rk3399_pmuclk_priv *priv = dev_get_priv(clk->dev);
1334 * This has already been set up and we don't want/need
1335 * to change it here. Accept the request though, as the
1336 * device-tree has this in an 'assigned-clocks' list.
1342 ret = rk3399_i2c_set_pmuclk(priv->pmucru, clk->id, rate);
1351 static struct clk_ops rk3399_pmuclk_ops = {
1352 .get_rate = rk3399_pmuclk_get_rate,
1353 .set_rate = rk3399_pmuclk_set_rate,
1356 #ifndef CONFIG_SPL_BUILD
1357 static void pmuclk_init(struct rk3399_pmucru *pmucru)
1361 /* configure pmu pll(ppll) */
1362 rkclk_set_pll(&pmucru->ppll_con[0], &ppll_init_cfg);
1364 /* configure pmu pclk */
1365 pclk_div = PPLL_HZ / PMU_PCLK_HZ - 1;
1366 rk_clrsetreg(&pmucru->pmucru_clksel[0],
1367 PMU_PCLK_DIV_CON_MASK,
1368 pclk_div << PMU_PCLK_DIV_CON_SHIFT);
1372 static int rk3399_pmuclk_probe(struct udevice *dev)
1374 #if CONFIG_IS_ENABLED(OF_PLATDATA) || !defined(CONFIG_SPL_BUILD)
1375 struct rk3399_pmuclk_priv *priv = dev_get_priv(dev);
1378 #if CONFIG_IS_ENABLED(OF_PLATDATA)
1379 struct rk3399_pmuclk_plat *plat = dev_get_platdata(dev);
1381 priv->pmucru = map_sysmem(plat->dtd.reg[0], plat->dtd.reg[1]);
1384 #ifndef CONFIG_SPL_BUILD
1385 pmuclk_init(priv->pmucru);
1390 static int rk3399_pmuclk_ofdata_to_platdata(struct udevice *dev)
1392 #if !CONFIG_IS_ENABLED(OF_PLATDATA)
1393 struct rk3399_pmuclk_priv *priv = dev_get_priv(dev);
1395 priv->pmucru = dev_read_addr_ptr(dev);
1400 static int rk3399_pmuclk_bind(struct udevice *dev)
1402 #if CONFIG_IS_ENABLED(CONFIG_RESET_ROCKCHIP)
1405 ret = offsetof(struct rk3399_pmucru, pmucru_softrst_con[0]);
1406 ret = rockchip_reset_bind(dev, ret, 2);
1408 debug("Warning: software reset driver bind faile\n");
1413 static const struct udevice_id rk3399_pmuclk_ids[] = {
1414 { .compatible = "rockchip,rk3399-pmucru" },
1418 U_BOOT_DRIVER(rockchip_rk3399_pmuclk) = {
1419 .name = "rockchip_rk3399_pmucru",
1421 .of_match = rk3399_pmuclk_ids,
1422 .priv_auto_alloc_size = sizeof(struct rk3399_pmuclk_priv),
1423 .ofdata_to_platdata = rk3399_pmuclk_ofdata_to_platdata,
1424 .ops = &rk3399_pmuclk_ops,
1425 .probe = rk3399_pmuclk_probe,
1426 .bind = rk3399_pmuclk_bind,
1427 #if CONFIG_IS_ENABLED(OF_PLATDATA)
1428 .platdata_auto_alloc_size = sizeof(struct rk3399_pmuclk_plat),