blob: 7701a9734ee07ec0e89a1f780b21f3305527f7fc [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0
Kever Yang1cfd5502017-02-23 15:37:52 +08002/*
3 * (C) Copyright 2017 Rockchip Electronics Co., Ltd
Kever Yang1cfd5502017-02-23 15:37:52 +08004 */
5
David Wua9422232017-09-20 14:35:44 +08006#include <bitfield.h>
Kever Yang1cfd5502017-02-23 15:37:52 +08007#include <clk-uclass.h>
8#include <dm.h>
9#include <errno.h>
Simon Glass0f2af882020-05-10 11:40:05 -060010#include <log.h>
Simon Glass9bc15642020-02-03 07:36:16 -070011#include <malloc.h>
Kever Yang1cfd5502017-02-23 15:37:52 +080012#include <syscon.h>
Kever Yang9fbe17c2019-03-28 11:01:23 +080013#include <asm/arch-rockchip/clock.h>
14#include <asm/arch-rockchip/cru_rk3328.h>
15#include <asm/arch-rockchip/hardware.h>
16#include <asm/arch-rockchip/grf_rk3328.h>
Simon Glass95588622020-12-22 19:30:28 -070017#include <dm/device-internal.h>
Kever Yang1cfd5502017-02-23 15:37:52 +080018#include <dm/lists.h>
19#include <dt-bindings/clock/rk3328-cru.h>
Simon Glass4dcacfc2020-05-10 11:40:13 -060020#include <linux/bitops.h>
Simon Glassdbd79542020-05-10 11:40:11 -060021#include <linux/delay.h>
Kever Yang1cfd5502017-02-23 15:37:52 +080022
Kever Yang1cfd5502017-02-23 15:37:52 +080023struct pll_div {
24 u32 refdiv;
25 u32 fbdiv;
26 u32 postdiv1;
27 u32 postdiv2;
28 u32 frac;
29};
30
31#define RATE_TO_DIV(input_rate, output_rate) \
32 ((input_rate) / (output_rate) - 1);
33#define DIV_TO_RATE(input_rate, div) ((input_rate) / ((div) + 1))
34
35#define PLL_DIVISORS(hz, _refdiv, _postdiv1, _postdiv2) {\
36 .refdiv = _refdiv,\
37 .fbdiv = (u32)((u64)hz * _refdiv * _postdiv1 * _postdiv2 / OSC_HZ),\
38 .postdiv1 = _postdiv1, .postdiv2 = _postdiv2};
39
40static const struct pll_div gpll_init_cfg = PLL_DIVISORS(GPLL_HZ, 1, 4, 1);
41static const struct pll_div cpll_init_cfg = PLL_DIVISORS(CPLL_HZ, 2, 2, 1);
42
43static const struct pll_div apll_816_cfg = PLL_DIVISORS(816 * MHz, 1, 2, 1);
44static const struct pll_div apll_600_cfg = PLL_DIVISORS(600 * MHz, 1, 3, 1);
45
46static const struct pll_div *apll_cfgs[] = {
47 [APLL_816_MHZ] = &apll_816_cfg,
48 [APLL_600_MHZ] = &apll_600_cfg,
49};
50
51enum {
52 /* PLL_CON0 */
53 PLL_POSTDIV1_SHIFT = 12,
54 PLL_POSTDIV1_MASK = 0x7 << PLL_POSTDIV1_SHIFT,
55 PLL_FBDIV_SHIFT = 0,
56 PLL_FBDIV_MASK = 0xfff,
57
58 /* PLL_CON1 */
59 PLL_DSMPD_SHIFT = 12,
60 PLL_DSMPD_MASK = 1 << PLL_DSMPD_SHIFT,
61 PLL_INTEGER_MODE = 1,
62 PLL_LOCK_STATUS_SHIFT = 10,
63 PLL_LOCK_STATUS_MASK = 1 << PLL_LOCK_STATUS_SHIFT,
64 PLL_POSTDIV2_SHIFT = 6,
65 PLL_POSTDIV2_MASK = 0x7 << PLL_POSTDIV2_SHIFT,
66 PLL_REFDIV_SHIFT = 0,
67 PLL_REFDIV_MASK = 0x3f,
68
69 /* PLL_CON2 */
70 PLL_FRACDIV_SHIFT = 0,
71 PLL_FRACDIV_MASK = 0xffffff,
72
73 /* MODE_CON */
74 APLL_MODE_SHIFT = 0,
75 NPLL_MODE_SHIFT = 1,
76 DPLL_MODE_SHIFT = 4,
77 CPLL_MODE_SHIFT = 8,
78 GPLL_MODE_SHIFT = 12,
79 PLL_MODE_SLOW = 0,
80 PLL_MODE_NORM,
81
82 /* CLKSEL_CON0 */
83 CLK_CORE_PLL_SEL_APLL = 0,
84 CLK_CORE_PLL_SEL_GPLL,
85 CLK_CORE_PLL_SEL_DPLL,
86 CLK_CORE_PLL_SEL_NPLL,
87 CLK_CORE_PLL_SEL_SHIFT = 6,
88 CLK_CORE_PLL_SEL_MASK = 3 << CLK_CORE_PLL_SEL_SHIFT,
89 CLK_CORE_DIV_SHIFT = 0,
90 CLK_CORE_DIV_MASK = 0x1f,
91
92 /* CLKSEL_CON1 */
93 ACLKM_CORE_DIV_SHIFT = 4,
94 ACLKM_CORE_DIV_MASK = 0x7 << ACLKM_CORE_DIV_SHIFT,
95 PCLK_DBG_DIV_SHIFT = 0,
96 PCLK_DBG_DIV_MASK = 0xF << PCLK_DBG_DIV_SHIFT,
97
David Wuf01c5812018-01-13 14:02:36 +080098 /* CLKSEL_CON27 */
99 GMAC2IO_PLL_SEL_SHIFT = 7,
100 GMAC2IO_PLL_SEL_MASK = 1 << GMAC2IO_PLL_SEL_SHIFT,
101 GMAC2IO_PLL_SEL_CPLL = 0,
102 GMAC2IO_PLL_SEL_GPLL = 1,
103 GMAC2IO_CLK_DIV_MASK = 0x1f,
104 GMAC2IO_CLK_DIV_SHIFT = 0,
105
Kever Yang1cfd5502017-02-23 15:37:52 +0800106 /* CLKSEL_CON28 */
107 ACLK_PERIHP_PLL_SEL_CPLL = 0,
108 ACLK_PERIHP_PLL_SEL_GPLL,
109 ACLK_PERIHP_PLL_SEL_HDMIPHY,
110 ACLK_PERIHP_PLL_SEL_SHIFT = 6,
111 ACLK_PERIHP_PLL_SEL_MASK = 3 << ACLK_PERIHP_PLL_SEL_SHIFT,
112 ACLK_PERIHP_DIV_CON_SHIFT = 0,
113 ACLK_PERIHP_DIV_CON_MASK = 0x1f,
114
115 /* CLKSEL_CON29 */
116 PCLK_PERIHP_DIV_CON_SHIFT = 4,
117 PCLK_PERIHP_DIV_CON_MASK = 0x7 << PCLK_PERIHP_DIV_CON_SHIFT,
118 HCLK_PERIHP_DIV_CON_SHIFT = 0,
119 HCLK_PERIHP_DIV_CON_MASK = 3 << HCLK_PERIHP_DIV_CON_SHIFT,
120
121 /* CLKSEL_CON22 */
122 CLK_TSADC_DIV_CON_SHIFT = 0,
123 CLK_TSADC_DIV_CON_MASK = 0x3ff,
124
125 /* CLKSEL_CON23 */
126 CLK_SARADC_DIV_CON_SHIFT = 0,
David Wua9422232017-09-20 14:35:44 +0800127 CLK_SARADC_DIV_CON_MASK = GENMASK(9, 0),
128 CLK_SARADC_DIV_CON_WIDTH = 10,
Kever Yang1cfd5502017-02-23 15:37:52 +0800129
130 /* CLKSEL_CON24 */
131 CLK_PWM_PLL_SEL_CPLL = 0,
132 CLK_PWM_PLL_SEL_GPLL,
133 CLK_PWM_PLL_SEL_SHIFT = 15,
134 CLK_PWM_PLL_SEL_MASK = 1 << CLK_PWM_PLL_SEL_SHIFT,
135 CLK_PWM_DIV_CON_SHIFT = 8,
136 CLK_PWM_DIV_CON_MASK = 0x7f << CLK_PWM_DIV_CON_SHIFT,
137
138 CLK_SPI_PLL_SEL_CPLL = 0,
139 CLK_SPI_PLL_SEL_GPLL,
140 CLK_SPI_PLL_SEL_SHIFT = 7,
141 CLK_SPI_PLL_SEL_MASK = 1 << CLK_SPI_PLL_SEL_SHIFT,
142 CLK_SPI_DIV_CON_SHIFT = 0,
143 CLK_SPI_DIV_CON_MASK = 0x7f << CLK_SPI_DIV_CON_SHIFT,
144
145 /* CLKSEL_CON30 */
146 CLK_SDMMC_PLL_SEL_CPLL = 0,
147 CLK_SDMMC_PLL_SEL_GPLL,
148 CLK_SDMMC_PLL_SEL_24M,
149 CLK_SDMMC_PLL_SEL_USBPHY,
150 CLK_SDMMC_PLL_SHIFT = 8,
151 CLK_SDMMC_PLL_MASK = 0x3 << CLK_SDMMC_PLL_SHIFT,
152 CLK_SDMMC_DIV_CON_SHIFT = 0,
153 CLK_SDMMC_DIV_CON_MASK = 0xff << CLK_SDMMC_DIV_CON_SHIFT,
154
155 /* CLKSEL_CON32 */
156 CLK_EMMC_PLL_SEL_CPLL = 0,
157 CLK_EMMC_PLL_SEL_GPLL,
158 CLK_EMMC_PLL_SEL_24M,
159 CLK_EMMC_PLL_SEL_USBPHY,
160 CLK_EMMC_PLL_SHIFT = 8,
161 CLK_EMMC_PLL_MASK = 0x3 << CLK_EMMC_PLL_SHIFT,
162 CLK_EMMC_DIV_CON_SHIFT = 0,
163 CLK_EMMC_DIV_CON_MASK = 0xff << CLK_EMMC_DIV_CON_SHIFT,
164
165 /* CLKSEL_CON34 */
166 CLK_I2C_PLL_SEL_CPLL = 0,
167 CLK_I2C_PLL_SEL_GPLL,
168 CLK_I2C_DIV_CON_MASK = 0x7f,
169 CLK_I2C_PLL_SEL_MASK = 1,
170 CLK_I2C1_PLL_SEL_SHIFT = 15,
171 CLK_I2C1_DIV_CON_SHIFT = 8,
172 CLK_I2C0_PLL_SEL_SHIFT = 7,
173 CLK_I2C0_DIV_CON_SHIFT = 0,
174
175 /* CLKSEL_CON35 */
176 CLK_I2C3_PLL_SEL_SHIFT = 15,
177 CLK_I2C3_DIV_CON_SHIFT = 8,
178 CLK_I2C2_PLL_SEL_SHIFT = 7,
179 CLK_I2C2_DIV_CON_SHIFT = 0,
Jagan Teki350ab5d2024-01-17 13:21:47 +0530180
181 /* CLKSEL_CON40 */
182 CLK_HDMIPHY_DIV_CON_SHIFT = 3,
183 CLK_HDMIPHY_DIV_CON_MASK = 0x7 << CLK_HDMIPHY_DIV_CON_SHIFT,
Kever Yang1cfd5502017-02-23 15:37:52 +0800184};
185
186#define VCO_MAX_KHZ (3200 * (MHz / KHz))
187#define VCO_MIN_KHZ (800 * (MHz / KHz))
188#define OUTPUT_MAX_KHZ (3200 * (MHz / KHz))
189#define OUTPUT_MIN_KHZ (16 * (MHz / KHz))
190
191/*
192 * the div restructions of pll in integer mode, these are defined in
193 * * CRU_*PLL_CON0 or PMUCRU_*PLL_CON0
194 */
195#define PLL_DIV_MIN 16
196#define PLL_DIV_MAX 3200
197
198/*
199 * How to calculate the PLL(from TRM V0.3 Part 1 Page 63):
200 * Formulas also embedded within the Fractional PLL Verilog model:
201 * If DSMPD = 1 (DSM is disabled, "integer mode")
202 * FOUTVCO = FREF / REFDIV * FBDIV
203 * FOUTPOSTDIV = FOUTVCO / POSTDIV1 / POSTDIV2
204 * Where:
205 * FOUTVCO = Fractional PLL non-divided output frequency
206 * FOUTPOSTDIV = Fractional PLL divided output frequency
207 * (output of second post divider)
208 * FREF = Fractional PLL input reference frequency, (the OSC_HZ 24MHz input)
209 * REFDIV = Fractional PLL input reference clock divider
210 * FBDIV = Integer value programmed into feedback divide
211 *
212 */
213static void rkclk_set_pll(struct rk3328_cru *cru, enum rk_clk_id clk_id,
214 const struct pll_div *div)
215{
216 u32 *pll_con;
217 u32 mode_shift, mode_mask;
218
219 pll_con = NULL;
220 mode_shift = 0;
221 switch (clk_id) {
222 case CLK_ARM:
223 pll_con = cru->apll_con;
224 mode_shift = APLL_MODE_SHIFT;
225 break;
226 case CLK_DDR:
227 pll_con = cru->dpll_con;
228 mode_shift = DPLL_MODE_SHIFT;
229 break;
230 case CLK_CODEC:
231 pll_con = cru->cpll_con;
232 mode_shift = CPLL_MODE_SHIFT;
233 break;
234 case CLK_GENERAL:
235 pll_con = cru->gpll_con;
236 mode_shift = GPLL_MODE_SHIFT;
237 break;
238 case CLK_NEW:
239 pll_con = cru->npll_con;
240 mode_shift = NPLL_MODE_SHIFT;
241 break;
242 default:
243 break;
244 }
245 mode_mask = 1 << mode_shift;
246
247 /* All 8 PLLs have same VCO and output frequency range restrictions. */
248 u32 vco_khz = OSC_HZ / 1000 * div->fbdiv / div->refdiv;
249 u32 output_khz = vco_khz / div->postdiv1 / div->postdiv2;
250
251 debug("PLL at %p: fbdiv=%d, refdiv=%d, postdiv1=%d, \
252 postdiv2=%d, vco=%u khz, output=%u khz\n",
253 pll_con, div->fbdiv, div->refdiv, div->postdiv1,
254 div->postdiv2, vco_khz, output_khz);
255 assert(vco_khz >= VCO_MIN_KHZ && vco_khz <= VCO_MAX_KHZ &&
256 output_khz >= OUTPUT_MIN_KHZ && output_khz <= OUTPUT_MAX_KHZ &&
257 div->fbdiv >= PLL_DIV_MIN && div->fbdiv <= PLL_DIV_MAX);
258
259 /*
260 * When power on or changing PLL setting,
261 * we must force PLL into slow mode to ensure output stable clock.
262 */
263 rk_clrsetreg(&cru->mode_con, mode_mask, PLL_MODE_SLOW << mode_shift);
264
265 /* use integer mode */
266 rk_clrsetreg(&pll_con[1], PLL_DSMPD_MASK,
267 PLL_INTEGER_MODE << PLL_DSMPD_SHIFT);
268
269 rk_clrsetreg(&pll_con[0],
270 PLL_FBDIV_MASK | PLL_POSTDIV1_MASK,
271 (div->fbdiv << PLL_FBDIV_SHIFT) |
272 (div->postdiv1 << PLL_POSTDIV1_SHIFT));
273 rk_clrsetreg(&pll_con[1],
274 PLL_POSTDIV2_MASK | PLL_REFDIV_MASK,
275 (div->postdiv2 << PLL_POSTDIV2_SHIFT) |
276 (div->refdiv << PLL_REFDIV_SHIFT));
277
278 /* waiting for pll lock */
279 while (!(readl(&pll_con[1]) & (1 << PLL_LOCK_STATUS_SHIFT)))
280 udelay(1);
281
282 /* pll enter normal mode */
283 rk_clrsetreg(&cru->mode_con, mode_mask, PLL_MODE_NORM << mode_shift);
284}
285
286static void rkclk_init(struct rk3328_cru *cru)
287{
288 u32 aclk_div;
289 u32 hclk_div;
290 u32 pclk_div;
291
Simon South93c44852019-10-10 15:28:36 -0400292 rk3328_configure_cpu(cru, APLL_600_MHZ);
293
Kever Yang1cfd5502017-02-23 15:37:52 +0800294 /* configure gpll cpll */
295 rkclk_set_pll(cru, CLK_GENERAL, &gpll_init_cfg);
296 rkclk_set_pll(cru, CLK_CODEC, &cpll_init_cfg);
297
298 /* configure perihp aclk, hclk, pclk */
299 aclk_div = GPLL_HZ / PERIHP_ACLK_HZ - 1;
300 hclk_div = PERIHP_ACLK_HZ / PERIHP_HCLK_HZ - 1;
301 pclk_div = PERIHP_ACLK_HZ / PERIHP_PCLK_HZ - 1;
302
303 rk_clrsetreg(&cru->clksel_con[28],
304 ACLK_PERIHP_PLL_SEL_MASK | ACLK_PERIHP_DIV_CON_MASK,
305 ACLK_PERIHP_PLL_SEL_GPLL << ACLK_PERIHP_PLL_SEL_SHIFT |
306 aclk_div << ACLK_PERIHP_DIV_CON_SHIFT);
307 rk_clrsetreg(&cru->clksel_con[29],
308 PCLK_PERIHP_DIV_CON_MASK | HCLK_PERIHP_DIV_CON_MASK,
309 pclk_div << PCLK_PERIHP_DIV_CON_SHIFT |
310 hclk_div << HCLK_PERIHP_DIV_CON_SHIFT);
311}
312
313void rk3328_configure_cpu(struct rk3328_cru *cru,
314 enum apll_frequencies apll_freq)
315{
316 u32 clk_core_div;
317 u32 aclkm_div;
318 u32 pclk_dbg_div;
319
320 rkclk_set_pll(cru, CLK_ARM, apll_cfgs[apll_freq]);
321
322 clk_core_div = APLL_HZ / CLK_CORE_HZ - 1;
323 aclkm_div = APLL_HZ / ACLKM_CORE_HZ / (clk_core_div + 1) - 1;
324 pclk_dbg_div = APLL_HZ / PCLK_DBG_HZ / (clk_core_div + 1) - 1;
325
326 rk_clrsetreg(&cru->clksel_con[0],
327 CLK_CORE_PLL_SEL_MASK | CLK_CORE_DIV_MASK,
328 CLK_CORE_PLL_SEL_APLL << CLK_CORE_PLL_SEL_SHIFT |
329 clk_core_div << CLK_CORE_DIV_SHIFT);
330
331 rk_clrsetreg(&cru->clksel_con[1],
332 PCLK_DBG_DIV_MASK | ACLKM_CORE_DIV_MASK,
333 pclk_dbg_div << PCLK_DBG_DIV_SHIFT |
334 aclkm_div << ACLKM_CORE_DIV_SHIFT);
335}
336
Kever Yang1cfd5502017-02-23 15:37:52 +0800337static ulong rk3328_i2c_get_clk(struct rk3328_cru *cru, ulong clk_id)
338{
339 u32 div, con;
340
341 switch (clk_id) {
342 case SCLK_I2C0:
343 con = readl(&cru->clksel_con[34]);
344 div = con >> CLK_I2C0_DIV_CON_SHIFT & CLK_I2C_DIV_CON_MASK;
345 break;
346 case SCLK_I2C1:
347 con = readl(&cru->clksel_con[34]);
348 div = con >> CLK_I2C1_DIV_CON_SHIFT & CLK_I2C_DIV_CON_MASK;
349 break;
350 case SCLK_I2C2:
351 con = readl(&cru->clksel_con[35]);
352 div = con >> CLK_I2C2_DIV_CON_SHIFT & CLK_I2C_DIV_CON_MASK;
353 break;
354 case SCLK_I2C3:
355 con = readl(&cru->clksel_con[35]);
356 div = con >> CLK_I2C3_DIV_CON_SHIFT & CLK_I2C_DIV_CON_MASK;
357 break;
358 default:
359 printf("do not support this i2c bus\n");
360 return -EINVAL;
361 }
362
363 return DIV_TO_RATE(GPLL_HZ, div);
364}
365
366static ulong rk3328_i2c_set_clk(struct rk3328_cru *cru, ulong clk_id, uint hz)
367{
368 int src_clk_div;
369
370 src_clk_div = GPLL_HZ / hz;
371 assert(src_clk_div - 1 < 127);
372
373 switch (clk_id) {
374 case SCLK_I2C0:
375 rk_clrsetreg(&cru->clksel_con[34],
376 CLK_I2C_DIV_CON_MASK << CLK_I2C0_DIV_CON_SHIFT |
377 CLK_I2C_PLL_SEL_MASK << CLK_I2C0_PLL_SEL_SHIFT,
378 (src_clk_div - 1) << CLK_I2C0_DIV_CON_SHIFT |
379 CLK_I2C_PLL_SEL_GPLL << CLK_I2C0_PLL_SEL_SHIFT);
380 break;
381 case SCLK_I2C1:
382 rk_clrsetreg(&cru->clksel_con[34],
383 CLK_I2C_DIV_CON_MASK << CLK_I2C1_DIV_CON_SHIFT |
384 CLK_I2C_PLL_SEL_MASK << CLK_I2C1_PLL_SEL_SHIFT,
385 (src_clk_div - 1) << CLK_I2C1_DIV_CON_SHIFT |
386 CLK_I2C_PLL_SEL_GPLL << CLK_I2C1_PLL_SEL_SHIFT);
387 break;
388 case SCLK_I2C2:
389 rk_clrsetreg(&cru->clksel_con[35],
390 CLK_I2C_DIV_CON_MASK << CLK_I2C2_DIV_CON_SHIFT |
391 CLK_I2C_PLL_SEL_MASK << CLK_I2C2_PLL_SEL_SHIFT,
392 (src_clk_div - 1) << CLK_I2C2_DIV_CON_SHIFT |
393 CLK_I2C_PLL_SEL_GPLL << CLK_I2C2_PLL_SEL_SHIFT);
394 break;
395 case SCLK_I2C3:
396 rk_clrsetreg(&cru->clksel_con[35],
397 CLK_I2C_DIV_CON_MASK << CLK_I2C3_DIV_CON_SHIFT |
398 CLK_I2C_PLL_SEL_MASK << CLK_I2C3_PLL_SEL_SHIFT,
399 (src_clk_div - 1) << CLK_I2C3_DIV_CON_SHIFT |
400 CLK_I2C_PLL_SEL_GPLL << CLK_I2C3_PLL_SEL_SHIFT);
401 break;
402 default:
403 printf("do not support this i2c bus\n");
404 return -EINVAL;
405 }
406
407 return DIV_TO_RATE(GPLL_HZ, src_clk_div);
408}
409
David Wuf01c5812018-01-13 14:02:36 +0800410static ulong rk3328_gmac2io_set_clk(struct rk3328_cru *cru, ulong rate)
411{
412 struct rk3328_grf_regs *grf;
413 ulong ret;
414
415 grf = syscon_get_first_range(ROCKCHIP_SYSCON_GRF);
416
417 /*
418 * The RGMII CLK can be derived either from an external "clkin"
419 * or can be generated from internally by a divider from SCLK_MAC.
420 */
421 if (readl(&grf->mac_con[1]) & BIT(10) &&
422 readl(&grf->soc_con[4]) & BIT(14)) {
423 /* An external clock will always generate the right rate... */
424 ret = rate;
425 } else {
426 u32 con = readl(&cru->clksel_con[27]);
427 ulong pll_rate;
428 u8 div;
429
430 if ((con >> GMAC2IO_PLL_SEL_SHIFT) & GMAC2IO_PLL_SEL_GPLL)
431 pll_rate = GPLL_HZ;
432 else
433 pll_rate = CPLL_HZ;
434
435 div = DIV_ROUND_UP(pll_rate, rate) - 1;
436 if (div <= 0x1f)
437 rk_clrsetreg(&cru->clksel_con[27], GMAC2IO_CLK_DIV_MASK,
438 div << GMAC2IO_CLK_DIV_SHIFT);
439 else
440 debug("Unsupported div for gmac:%d\n", div);
441
442 return DIV_TO_RATE(pll_rate, div);
443 }
444
445 return ret;
446}
447
Kever Yang1cfd5502017-02-23 15:37:52 +0800448static ulong rk3328_mmc_get_clk(struct rk3328_cru *cru, uint clk_id)
449{
450 u32 div, con, con_id;
451
452 switch (clk_id) {
453 case HCLK_SDMMC:
Xu Ziyuan5a027632017-04-16 17:44:46 +0800454 case SCLK_SDMMC:
Kever Yang1cfd5502017-02-23 15:37:52 +0800455 con_id = 30;
456 break;
457 case HCLK_EMMC:
Xu Ziyuan5a027632017-04-16 17:44:46 +0800458 case SCLK_EMMC:
Kever Yang1cfd5502017-02-23 15:37:52 +0800459 con_id = 32;
460 break;
461 default:
462 return -EINVAL;
463 }
464 con = readl(&cru->clksel_con[con_id]);
465 div = (con & CLK_EMMC_DIV_CON_MASK) >> CLK_EMMC_DIV_CON_SHIFT;
466
467 if ((con & CLK_EMMC_PLL_MASK) >> CLK_EMMC_PLL_SHIFT
468 == CLK_EMMC_PLL_SEL_24M)
Kever Yang99b546d2017-07-27 12:54:01 +0800469 return DIV_TO_RATE(OSC_HZ, div) / 2;
Kever Yang1cfd5502017-02-23 15:37:52 +0800470 else
Kever Yang99b546d2017-07-27 12:54:01 +0800471 return DIV_TO_RATE(GPLL_HZ, div) / 2;
Kever Yang1cfd5502017-02-23 15:37:52 +0800472}
473
474static ulong rk3328_mmc_set_clk(struct rk3328_cru *cru,
475 ulong clk_id, ulong set_rate)
476{
477 int src_clk_div;
478 u32 con_id;
479
480 switch (clk_id) {
481 case HCLK_SDMMC:
Xu Ziyuan5a027632017-04-16 17:44:46 +0800482 case SCLK_SDMMC:
Kever Yang1cfd5502017-02-23 15:37:52 +0800483 con_id = 30;
484 break;
485 case HCLK_EMMC:
Xu Ziyuan5a027632017-04-16 17:44:46 +0800486 case SCLK_EMMC:
Kever Yang1cfd5502017-02-23 15:37:52 +0800487 con_id = 32;
488 break;
489 default:
490 return -EINVAL;
491 }
492 /* Select clk_sdmmc/emmc source from GPLL by default */
Kever Yang99b546d2017-07-27 12:54:01 +0800493 /* mmc clock defaulg div 2 internal, need provide double in cru */
494 src_clk_div = DIV_ROUND_UP(GPLL_HZ / 2, set_rate);
Kever Yang1cfd5502017-02-23 15:37:52 +0800495
496 if (src_clk_div > 127) {
497 /* use 24MHz source for 400KHz clock */
Kever Yang99b546d2017-07-27 12:54:01 +0800498 src_clk_div = DIV_ROUND_UP(OSC_HZ / 2, set_rate);
Kever Yang1cfd5502017-02-23 15:37:52 +0800499 rk_clrsetreg(&cru->clksel_con[con_id],
500 CLK_EMMC_PLL_MASK | CLK_EMMC_DIV_CON_MASK,
501 CLK_EMMC_PLL_SEL_24M << CLK_EMMC_PLL_SHIFT |
502 (src_clk_div - 1) << CLK_EMMC_DIV_CON_SHIFT);
503 } else {
504 rk_clrsetreg(&cru->clksel_con[con_id],
505 CLK_EMMC_PLL_MASK | CLK_EMMC_DIV_CON_MASK,
506 CLK_EMMC_PLL_SEL_GPLL << CLK_EMMC_PLL_SHIFT |
507 (src_clk_div - 1) << CLK_EMMC_DIV_CON_SHIFT);
508 }
509
510 return rk3328_mmc_get_clk(cru, clk_id);
511}
512
513static ulong rk3328_pwm_get_clk(struct rk3328_cru *cru)
514{
515 u32 div, con;
516
517 con = readl(&cru->clksel_con[24]);
518 div = (con & CLK_PWM_DIV_CON_MASK) >> CLK_PWM_DIV_CON_SHIFT;
519
520 return DIV_TO_RATE(GPLL_HZ, div);
521}
522
523static ulong rk3328_pwm_set_clk(struct rk3328_cru *cru, uint hz)
524{
525 u32 div = GPLL_HZ / hz;
526
527 rk_clrsetreg(&cru->clksel_con[24],
528 CLK_PWM_PLL_SEL_MASK | CLK_PWM_DIV_CON_MASK,
529 CLK_PWM_PLL_SEL_GPLL << CLK_PWM_PLL_SEL_SHIFT |
530 (div - 1) << CLK_PWM_DIV_CON_SHIFT);
531
532 return DIV_TO_RATE(GPLL_HZ, div);
533}
534
David Wua9422232017-09-20 14:35:44 +0800535static ulong rk3328_saradc_get_clk(struct rk3328_cru *cru)
536{
537 u32 div, val;
538
539 val = readl(&cru->clksel_con[23]);
540 div = bitfield_extract(val, CLK_SARADC_DIV_CON_SHIFT,
541 CLK_SARADC_DIV_CON_WIDTH);
542
543 return DIV_TO_RATE(OSC_HZ, div);
544}
545
546static ulong rk3328_saradc_set_clk(struct rk3328_cru *cru, uint hz)
547{
548 int src_clk_div;
549
550 src_clk_div = DIV_ROUND_UP(OSC_HZ, hz) - 1;
551 assert(src_clk_div < 128);
552
553 rk_clrsetreg(&cru->clksel_con[23],
554 CLK_SARADC_DIV_CON_MASK,
555 src_clk_div << CLK_SARADC_DIV_CON_SHIFT);
556
557 return rk3328_saradc_get_clk(cru);
558}
559
Johannes Krottmayer0f0237d2020-07-08 23:57:38 +0200560static ulong rk3328_spi_get_clk(struct rk3328_cru *cru)
561{
562 u32 div, val;
563
564 val = readl(&cru->clksel_con[24]);
565 div = (val & CLK_SPI_DIV_CON_MASK) >> CLK_SPI_DIV_CON_SHIFT;
566
567 return DIV_TO_RATE(OSC_HZ, div);
568}
569
570static ulong rk3328_spi_set_clk(struct rk3328_cru *cru, uint hz)
571{
572 u32 src_clk_div;
573
574 src_clk_div = GPLL_HZ / hz;
575 assert(src_clk_div < 128);
576
577 rk_clrsetreg(&cru->clksel_con[24],
578 CLK_PWM_PLL_SEL_MASK | CLK_PWM_DIV_CON_MASK,
579 CLK_PWM_PLL_SEL_GPLL << CLK_PWM_PLL_SEL_SHIFT |
580 (src_clk_div - 1) << CLK_PWM_DIV_CON_SHIFT);
581
582 return rk3328_spi_get_clk(cru);
583}
584
Simon Glass7ec24132024-09-29 19:49:48 -0600585#ifndef CONFIG_XPL_BUILD
Jagan Teki175ee822024-01-17 13:21:46 +0530586static ulong rk3328_vop_get_clk(struct rk3328_clk_priv *priv, ulong clk_id)
587{
588 struct rk3328_cru *cru = priv->cru;
589 u32 div, con, parent;
590
591 switch (clk_id) {
592 case ACLK_VOP_PRE:
593 con = readl(&cru->clksel_con[39]);
594 div = (con & ACLK_VOP_DIV_CON_MASK) >> ACLK_VOP_DIV_CON_SHIFT;
595 parent = GPLL_HZ;
596 break;
597 case ACLK_VIO_PRE:
598 con = readl(&cru->clksel_con[37]);
599 div = (con & ACLK_VIO_DIV_CON_MASK) >> ACLK_VIO_DIV_CON_SHIFT;
600 parent = GPLL_HZ;
601 break;
602 case DCLK_LCDC:
603 con = readl(&cru->clksel_con[40]);
604 div = (con & DCLK_LCDC_DIV_CON_MASK) >> DCLK_LCDC_DIV_CON_SHIFT;
605 parent = GPLL_HZ;
606 break;
607 default:
608 printf("%s: Unsupported vop get clk#%ld\n", __func__, clk_id);
609 return -ENOENT;
610 }
611
612 return DIV_TO_RATE(parent, div);
613}
614
615static ulong rk3328_vop_set_clk(struct rk3328_clk_priv *priv,
616 ulong clk_id, uint hz)
617{
618 struct rk3328_cru *cru = priv->cru;
619 int src_clk_div;
620 u32 con, parent;
621
622 src_clk_div = DIV_ROUND_UP(GPLL_HZ, hz);
623 assert(src_clk_div - 1 < 31);
624
625 switch (clk_id) {
626 case ACLK_VOP_PRE:
627 rk_clrsetreg(&cru->clksel_con[39],
628 ACLK_VOP_PLL_SEL_MASK | ACLK_VOP_DIV_CON_MASK,
629 ACLK_VOP_PLL_SEL_CPLL << ACLK_VOP_PLL_SEL_SHIFT |
630 (src_clk_div - 1) << ACLK_VOP_DIV_CON_SHIFT);
631 break;
632 case ACLK_VIO_PRE:
633 rk_clrsetreg(&cru->clksel_con[37],
634 ACLK_VIO_PLL_SEL_MASK | ACLK_VIO_DIV_CON_MASK,
635 ACLK_VIO_PLL_SEL_CPLL << ACLK_VIO_PLL_SEL_SHIFT |
636 (src_clk_div - 1) << ACLK_VIO_DIV_CON_SHIFT);
637 break;
638 case DCLK_LCDC:
639 con = readl(&cru->clksel_con[40]);
640 con = (con & DCLK_LCDC_SEL_MASK) >> DCLK_LCDC_SEL_SHIFT;
641 if (con) {
642 parent = readl(&cru->clksel_con[40]);
643 parent = (parent & DCLK_LCDC_PLL_SEL_MASK) >>
644 DCLK_LCDC_PLL_SEL_SHIFT;
645 if (parent)
646 src_clk_div = DIV_ROUND_UP(GPLL_HZ, hz);
647 else
648 src_clk_div = DIV_ROUND_UP(GPLL_HZ, hz);
649
650 rk_clrsetreg(&cru->clksel_con[40],
651 DCLK_LCDC_DIV_CON_MASK,
652 (src_clk_div - 1) <<
653 DCLK_LCDC_DIV_CON_SHIFT);
654 }
655 break;
656 default:
657 printf("%s: Unable to set vop clk#%ld\n", __func__, clk_id);
658 return -EINVAL;
659 }
660
661 return rk3328_vop_get_clk(priv, clk_id);
662}
663#endif
664
Jagan Teki350ab5d2024-01-17 13:21:47 +0530665static ulong rk3328_hdmiphy_get_clk(struct rk3328_cru *cru)
666{
667 u32 div, con;
668
669 con = readl(&cru->clksel_con[40]);
670 div = (con & CLK_HDMIPHY_DIV_CON_MASK) >> CLK_HDMIPHY_DIV_CON_SHIFT;
671
672 return DIV_TO_RATE(GPLL_HZ, div);
673}
674
Kever Yang1cfd5502017-02-23 15:37:52 +0800675static ulong rk3328_clk_get_rate(struct clk *clk)
676{
677 struct rk3328_clk_priv *priv = dev_get_priv(clk->dev);
678 ulong rate = 0;
679
680 switch (clk->id) {
681 case 0 ... 29:
682 return 0;
683 case HCLK_SDMMC:
684 case HCLK_EMMC:
Xu Ziyuan5a027632017-04-16 17:44:46 +0800685 case SCLK_SDMMC:
686 case SCLK_EMMC:
Kever Yang1cfd5502017-02-23 15:37:52 +0800687 rate = rk3328_mmc_get_clk(priv->cru, clk->id);
688 break;
689 case SCLK_I2C0:
690 case SCLK_I2C1:
691 case SCLK_I2C2:
692 case SCLK_I2C3:
693 rate = rk3328_i2c_get_clk(priv->cru, clk->id);
694 break;
695 case SCLK_PWM:
696 rate = rk3328_pwm_get_clk(priv->cru);
697 break;
David Wua9422232017-09-20 14:35:44 +0800698 case SCLK_SARADC:
699 rate = rk3328_saradc_get_clk(priv->cru);
700 break;
Johannes Krottmayer0f0237d2020-07-08 23:57:38 +0200701 case SCLK_SPI:
702 rate = rk3328_spi_get_clk(priv->cru);
703 break;
Jagan Teki350ab5d2024-01-17 13:21:47 +0530704 case PCLK_HDMIPHY:
705 rate = rk3328_hdmiphy_get_clk(priv->cru);
706 break;
Jonas Karlmanbe201322024-05-01 19:23:50 +0000707 case SCLK_USB3OTG_REF:
708 rate = OSC_HZ;
709 break;
Kever Yang1cfd5502017-02-23 15:37:52 +0800710 default:
711 return -ENOENT;
712 }
713
714 return rate;
715}
716
717static ulong rk3328_clk_set_rate(struct clk *clk, ulong rate)
718{
719 struct rk3328_clk_priv *priv = dev_get_priv(clk->dev);
720 ulong ret = 0;
721
722 switch (clk->id) {
723 case 0 ... 29:
724 return 0;
725 case HCLK_SDMMC:
726 case HCLK_EMMC:
Xu Ziyuan5a027632017-04-16 17:44:46 +0800727 case SCLK_SDMMC:
728 case SCLK_EMMC:
Kever Yang1cfd5502017-02-23 15:37:52 +0800729 ret = rk3328_mmc_set_clk(priv->cru, clk->id, rate);
730 break;
731 case SCLK_I2C0:
732 case SCLK_I2C1:
733 case SCLK_I2C2:
734 case SCLK_I2C3:
735 ret = rk3328_i2c_set_clk(priv->cru, clk->id, rate);
736 break;
David Wuf01c5812018-01-13 14:02:36 +0800737 case SCLK_MAC2IO:
738 ret = rk3328_gmac2io_set_clk(priv->cru, rate);
739 break;
Kever Yang1cfd5502017-02-23 15:37:52 +0800740 case SCLK_PWM:
741 ret = rk3328_pwm_set_clk(priv->cru, rate);
742 break;
David Wua9422232017-09-20 14:35:44 +0800743 case SCLK_SARADC:
744 ret = rk3328_saradc_set_clk(priv->cru, rate);
745 break;
Johannes Krottmayer0f0237d2020-07-08 23:57:38 +0200746 case SCLK_SPI:
747 ret = rk3328_spi_set_clk(priv->cru, rate);
748 break;
Simon Glass7ec24132024-09-29 19:49:48 -0600749#ifndef CONFIG_XPL_BUILD
David Wuf01c5812018-01-13 14:02:36 +0800750 case DCLK_LCDC:
Jagan Teki175ee822024-01-17 13:21:46 +0530751 case ACLK_VOP_PRE:
752 case ACLK_VIO_PRE:
753 rate = rk3328_vop_set_clk(priv, clk->id, rate);
754 break;
755#endif
David Wuf01c5812018-01-13 14:02:36 +0800756 case SCLK_PDM:
757 case SCLK_RTC32K:
758 case SCLK_UART0:
759 case SCLK_UART1:
760 case SCLK_UART2:
761 case SCLK_SDIO:
762 case SCLK_TSP:
763 case SCLK_WIFI:
764 case ACLK_BUS_PRE:
765 case HCLK_BUS_PRE:
766 case PCLK_BUS_PRE:
767 case ACLK_PERI_PRE:
768 case HCLK_PERI:
769 case PCLK_PERI:
David Wuf01c5812018-01-13 14:02:36 +0800770 case HCLK_VIO_PRE:
771 case ACLK_RGA_PRE:
772 case SCLK_RGA:
David Wuf01c5812018-01-13 14:02:36 +0800773 case ACLK_RKVDEC_PRE:
774 case ACLK_RKVENC:
775 case ACLK_VPU_PRE:
776 case SCLK_VDEC_CABAC:
777 case SCLK_VDEC_CORE:
778 case SCLK_VENC_CORE:
779 case SCLK_VENC_DSP:
780 case SCLK_EFUSE:
781 case PCLK_DDR:
782 case ACLK_GMAC:
783 case PCLK_GMAC:
Jonas Karlmanbe201322024-05-01 19:23:50 +0000784 case SCLK_USB3OTG_REF:
David Wuf01c5812018-01-13 14:02:36 +0800785 case SCLK_USB3OTG_SUSPEND:
Jagan Tekic46620f2023-06-06 22:39:17 +0530786 case USB480M:
David Wuf01c5812018-01-13 14:02:36 +0800787 return 0;
Kever Yang1cfd5502017-02-23 15:37:52 +0800788 default:
789 return -ENOENT;
790 }
791
792 return ret;
793}
794
David Wuf01c5812018-01-13 14:02:36 +0800795static int rk3328_gmac2io_set_parent(struct clk *clk, struct clk *parent)
796{
797 struct rk3328_grf_regs *grf;
798 const char *clock_output_name;
799 int ret;
800
801 grf = syscon_get_first_range(ROCKCHIP_SYSCON_GRF);
802
803 /*
804 * If the requested parent is in the same clock-controller and the id
805 * is SCLK_MAC2IO_SRC ("clk_mac2io_src"), switch to the internal clock.
806 */
807 if ((parent->dev == clk->dev) && (parent->id == SCLK_MAC2IO_SRC)) {
808 debug("%s: switching RGMII to SCLK_MAC2IO_SRC\n", __func__);
809 rk_clrreg(&grf->mac_con[1], BIT(10));
810 return 0;
811 }
812
813 /*
814 * Otherwise, we need to check the clock-output-names of the
815 * requested parent to see if the requested id is "gmac_clkin".
816 */
817 ret = dev_read_string_index(parent->dev, "clock-output-names",
818 parent->id, &clock_output_name);
819 if (ret < 0)
820 return -ENODATA;
821
822 /* If this is "gmac_clkin", switch to the external clock input */
823 if (!strcmp(clock_output_name, "gmac_clkin")) {
824 debug("%s: switching RGMII to CLKIN\n", __func__);
825 rk_setreg(&grf->mac_con[1], BIT(10));
826 return 0;
827 }
828
829 return -EINVAL;
830}
831
832static int rk3328_gmac2io_ext_set_parent(struct clk *clk, struct clk *parent)
833{
834 struct rk3328_grf_regs *grf;
835 const char *clock_output_name;
836 int ret;
837
838 grf = syscon_get_first_range(ROCKCHIP_SYSCON_GRF);
839
840 /*
841 * If the requested parent is in the same clock-controller and the id
842 * is SCLK_MAC2IO ("clk_mac2io"), switch to the internal clock.
843 */
844 if ((parent->dev == clk->dev) && (parent->id == SCLK_MAC2IO)) {
845 debug("%s: switching RGMII to SCLK_MAC2IO\n", __func__);
846 rk_clrreg(&grf->soc_con[4], BIT(14));
847 return 0;
848 }
849
850 /*
851 * Otherwise, we need to check the clock-output-names of the
852 * requested parent to see if the requested id is "gmac_clkin".
853 */
854 ret = dev_read_string_index(parent->dev, "clock-output-names",
855 parent->id, &clock_output_name);
856 if (ret < 0)
857 return -ENODATA;
858
859 /* If this is "gmac_clkin", switch to the external clock input */
860 if (!strcmp(clock_output_name, "gmac_clkin")) {
861 debug("%s: switching RGMII to CLKIN\n", __func__);
862 rk_setreg(&grf->soc_con[4], BIT(14));
863 return 0;
864 }
865
866 return -EINVAL;
867}
868
869static int rk3328_clk_set_parent(struct clk *clk, struct clk *parent)
870{
871 switch (clk->id) {
872 case SCLK_MAC2IO:
873 return rk3328_gmac2io_set_parent(clk, parent);
874 case SCLK_MAC2IO_EXT:
875 return rk3328_gmac2io_ext_set_parent(clk, parent);
876 case DCLK_LCDC:
Jagan Tekic46620f2023-06-06 22:39:17 +0530877 case USB480M:
David Wuf01c5812018-01-13 14:02:36 +0800878 case SCLK_PDM:
879 case SCLK_RTC32K:
880 case SCLK_UART0:
881 case SCLK_UART1:
882 case SCLK_UART2:
883 return 0;
884 }
885
886 debug("%s: unsupported clk %ld\n", __func__, clk->id);
887 return -ENOENT;
888}
889
Kever Yang1cfd5502017-02-23 15:37:52 +0800890static struct clk_ops rk3328_clk_ops = {
891 .get_rate = rk3328_clk_get_rate,
892 .set_rate = rk3328_clk_set_rate,
David Wuf01c5812018-01-13 14:02:36 +0800893 .set_parent = rk3328_clk_set_parent,
Kever Yang1cfd5502017-02-23 15:37:52 +0800894};
895
896static int rk3328_clk_probe(struct udevice *dev)
897{
898 struct rk3328_clk_priv *priv = dev_get_priv(dev);
899
900 rkclk_init(priv->cru);
901
902 return 0;
903}
904
Simon Glassaad29ae2020-12-03 16:55:21 -0700905static int rk3328_clk_of_to_plat(struct udevice *dev)
Kever Yang1cfd5502017-02-23 15:37:52 +0800906{
907 struct rk3328_clk_priv *priv = dev_get_priv(dev);
908
Kever Yangbb870a52018-02-11 11:53:09 +0800909 priv->cru = dev_read_addr_ptr(dev);
Kever Yang1cfd5502017-02-23 15:37:52 +0800910
911 return 0;
912}
913
914static int rk3328_clk_bind(struct udevice *dev)
915{
916 int ret;
Kever Yang4fbb6c22017-11-03 15:16:13 +0800917 struct udevice *sys_child;
918 struct sysreset_reg *priv;
Kever Yang1cfd5502017-02-23 15:37:52 +0800919
920 /* The reset driver does not have a device node, so bind it here */
Kever Yang4fbb6c22017-11-03 15:16:13 +0800921 ret = device_bind_driver(dev, "rockchip_sysreset", "sysreset",
922 &sys_child);
923 if (ret) {
924 debug("Warning: No sysreset driver: ret=%d\n", ret);
925 } else {
926 priv = malloc(sizeof(struct sysreset_reg));
927 priv->glb_srst_fst_value = offsetof(struct rk3328_cru,
928 glb_srst_fst_value);
929 priv->glb_srst_snd_value = offsetof(struct rk3328_cru,
930 glb_srst_snd_value);
Simon Glass95588622020-12-22 19:30:28 -0700931 dev_set_priv(sys_child, priv);
Kever Yang4fbb6c22017-11-03 15:16:13 +0800932 }
Kever Yang1cfd5502017-02-23 15:37:52 +0800933
Heiko Stuebner416f8d32019-11-09 00:06:30 +0100934#if CONFIG_IS_ENABLED(RESET_ROCKCHIP)
Elaine Zhang432976f2017-12-19 18:22:38 +0800935 ret = offsetof(struct rk3328_cru, softrst_con[0]);
936 ret = rockchip_reset_bind(dev, ret, 12);
937 if (ret)
Eugen Hristevf1798262023-04-11 10:17:56 +0300938 debug("Warning: software reset driver bind failed\n");
Elaine Zhang432976f2017-12-19 18:22:38 +0800939#endif
940
Kever Yang1cfd5502017-02-23 15:37:52 +0800941 return ret;
942}
943
944static const struct udevice_id rk3328_clk_ids[] = {
945 { .compatible = "rockchip,rk3328-cru" },
946 { }
947};
948
949U_BOOT_DRIVER(rockchip_rk3328_cru) = {
950 .name = "rockchip_rk3328_cru",
951 .id = UCLASS_CLK,
952 .of_match = rk3328_clk_ids,
Simon Glass8a2b47f2020-12-03 16:55:17 -0700953 .priv_auto = sizeof(struct rk3328_clk_priv),
Simon Glassaad29ae2020-12-03 16:55:21 -0700954 .of_to_plat = rk3328_clk_of_to_plat,
Kever Yang1cfd5502017-02-23 15:37:52 +0800955 .ops = &rk3328_clk_ops,
956 .bind = rk3328_clk_bind,
957 .probe = rk3328_clk_probe,
958};