blob: 7755b01611187268d17144e34f0a0aeb1cb70eae [file] [log] [blame]
Finley Xiaoafa71602019-11-14 11:21:13 +08001// SPDX-License-Identifier: GPL-2.0
2/*
3 * (C) Copyright 2017-2019 Rockchip Electronics Co., Ltd
4 */
5#include <common.h>
6#include <bitfield.h>
7#include <clk-uclass.h>
8#include <dm.h>
9#include <div64.h>
10#include <errno.h>
Simon Glass0f2af882020-05-10 11:40:05 -060011#include <log.h>
Simon Glass9bc15642020-02-03 07:36:16 -070012#include <malloc.h>
Finley Xiaoafa71602019-11-14 11:21:13 +080013#include <syscon.h>
Simon Glass3ba929a2020-10-30 21:38:53 -060014#include <asm/global_data.h>
Finley Xiaoafa71602019-11-14 11:21:13 +080015#include <asm/arch/cru_rk3308.h>
16#include <asm/arch-rockchip/clock.h>
17#include <asm/arch-rockchip/hardware.h>
Simon Glass95588622020-12-22 19:30:28 -070018#include <dm/device-internal.h>
Finley Xiaoafa71602019-11-14 11:21:13 +080019#include <dm/lists.h>
20#include <dt-bindings/clock/rk3308-cru.h>
Simon Glass4dcacfc2020-05-10 11:40:13 -060021#include <linux/bitops.h>
Finley Xiaoafa71602019-11-14 11:21:13 +080022
23DECLARE_GLOBAL_DATA_PTR;
24
25enum {
26 VCO_MAX_HZ = 3200U * 1000000,
27 VCO_MIN_HZ = 800 * 1000000,
28 OUTPUT_MAX_HZ = 3200U * 1000000,
29 OUTPUT_MIN_HZ = 24 * 1000000,
30};
31
32#define DIV_TO_RATE(input_rate, div) ((input_rate) / ((div) + 1))
33
34#define RK3308_CPUCLK_RATE(_rate, _aclk_div, _pclk_div) \
35{ \
36 .rate = _rate##U, \
37 .aclk_div = _aclk_div, \
38 .pclk_div = _pclk_div, \
39}
40
41static struct rockchip_pll_rate_table rk3308_pll_rates[] = {
42 /* _mhz, _refdiv, _fbdiv, _postdiv1, _postdiv2, _dsmpd, _frac */
43 RK3036_PLL_RATE(1300000000, 6, 325, 1, 1, 1, 0),
44 RK3036_PLL_RATE(1200000000, 1, 50, 1, 1, 1, 0),
45 RK3036_PLL_RATE(816000000, 1, 68, 2, 1, 1, 0),
46 RK3036_PLL_RATE(748000000, 2, 187, 3, 1, 1, 0),
47};
48
49static struct rockchip_cpu_rate_table rk3308_cpu_rates[] = {
50 RK3308_CPUCLK_RATE(1200000000, 1, 5),
51 RK3308_CPUCLK_RATE(1008000000, 1, 5),
52 RK3308_CPUCLK_RATE(816000000, 1, 3),
53 RK3308_CPUCLK_RATE(600000000, 1, 3),
54 RK3308_CPUCLK_RATE(408000000, 1, 1),
55};
56
57static struct rockchip_pll_clock rk3308_pll_clks[] = {
58 [APLL] = PLL(pll_rk3328, PLL_APLL, RK3308_PLL_CON(0),
59 RK3308_MODE_CON, 0, 10, 0, rk3308_pll_rates),
60 [DPLL] = PLL(pll_rk3328, PLL_DPLL, RK3308_PLL_CON(8),
61 RK3308_MODE_CON, 2, 10, 0, NULL),
62 [VPLL0] = PLL(pll_rk3328, PLL_VPLL0, RK3308_PLL_CON(16),
63 RK3308_MODE_CON, 4, 10, 0, NULL),
64 [VPLL1] = PLL(pll_rk3328, PLL_VPLL1, RK3308_PLL_CON(24),
65 RK3308_MODE_CON, 6, 10, 0, NULL),
66};
67
68static ulong rk3308_armclk_set_clk(struct rk3308_clk_priv *priv, ulong hz)
69{
70 struct rk3308_cru *cru = priv->cru;
71 const struct rockchip_cpu_rate_table *rate;
72 ulong old_rate;
73
74 rate = rockchip_get_cpu_settings(rk3308_cpu_rates, hz);
75 if (!rate) {
76 printf("%s unsupport rate\n", __func__);
77 return -EINVAL;
78 }
79
80 /*
81 * select apll as cpu/core clock pll source and
82 * set up dependent divisors for PERI and ACLK clocks.
83 * core hz : apll = 1:1
84 */
85 old_rate = rockchip_pll_get_rate(&rk3308_pll_clks[APLL],
86 priv->cru, APLL);
87 if (old_rate > hz) {
88 if (rockchip_pll_set_rate(&rk3308_pll_clks[APLL],
89 priv->cru, APLL, hz))
90 return -EINVAL;
91 rk_clrsetreg(&cru->clksel_con[0],
92 CORE_CLK_PLL_SEL_MASK | CORE_DIV_CON_MASK |
93 CORE_ACLK_DIV_MASK | CORE_DBG_DIV_MASK,
94 rate->aclk_div << CORE_ACLK_DIV_SHIFT |
95 rate->pclk_div << CORE_DBG_DIV_SHIFT |
96 CORE_CLK_PLL_SEL_APLL << CORE_CLK_PLL_SEL_SHIFT |
97 0 << CORE_DIV_CON_SHIFT);
98 } else if (old_rate < hz) {
99 rk_clrsetreg(&cru->clksel_con[0],
100 CORE_CLK_PLL_SEL_MASK | CORE_DIV_CON_MASK |
101 CORE_ACLK_DIV_MASK | CORE_DBG_DIV_MASK,
102 rate->aclk_div << CORE_ACLK_DIV_SHIFT |
103 rate->pclk_div << CORE_DBG_DIV_SHIFT |
104 CORE_CLK_PLL_SEL_APLL << CORE_CLK_PLL_SEL_SHIFT |
105 0 << CORE_DIV_CON_SHIFT);
106 if (rockchip_pll_set_rate(&rk3308_pll_clks[APLL],
107 priv->cru, APLL, hz))
108 return -EINVAL;
109 }
110
111 return rockchip_pll_get_rate(&rk3308_pll_clks[APLL], priv->cru, APLL);
112}
113
114static void rk3308_clk_get_pll_rate(struct rk3308_clk_priv *priv)
115{
116 if (!priv->dpll_hz)
117 priv->dpll_hz = rockchip_pll_get_rate(&rk3308_pll_clks[DPLL],
118 priv->cru, DPLL);
119 if (!priv->vpll0_hz)
120 priv->vpll0_hz = rockchip_pll_get_rate(&rk3308_pll_clks[VPLL0],
121 priv->cru, VPLL0);
122 if (!priv->vpll1_hz)
123 priv->vpll1_hz = rockchip_pll_get_rate(&rk3308_pll_clks[VPLL1],
124 priv->cru, VPLL1);
125}
126
127static ulong rk3308_i2c_get_clk(struct clk *clk)
128{
129 struct rk3308_clk_priv *priv = dev_get_priv(clk->dev);
130 struct rk3308_cru *cru = priv->cru;
131 u32 div, con, con_id;
132
133 switch (clk->id) {
134 case SCLK_I2C0:
135 con_id = 25;
136 break;
137 case SCLK_I2C1:
138 con_id = 26;
139 break;
140 case SCLK_I2C2:
141 con_id = 27;
142 break;
143 case SCLK_I2C3:
144 con_id = 28;
145 break;
146 default:
147 printf("do not support this i2c bus\n");
148 return -EINVAL;
149 }
150
151 con = readl(&cru->clksel_con[con_id]);
Massimo Pegorer00a8fa32023-08-03 13:08:11 +0200152 div = (con & CLK_I2C_DIV_CON_MASK) >> CLK_I2C_DIV_CON_SHIFT;
Finley Xiaoafa71602019-11-14 11:21:13 +0800153
154 return DIV_TO_RATE(priv->dpll_hz, div);
155}
156
157static ulong rk3308_i2c_set_clk(struct clk *clk, uint hz)
158{
159 struct rk3308_clk_priv *priv = dev_get_priv(clk->dev);
160 struct rk3308_cru *cru = priv->cru;
161 u32 src_clk_div, con_id;
162
163 src_clk_div = DIV_ROUND_UP(priv->dpll_hz, hz);
164 assert(src_clk_div - 1 <= 127);
165
166 switch (clk->id) {
167 case SCLK_I2C0:
168 con_id = 25;
169 break;
170 case SCLK_I2C1:
171 con_id = 26;
172 break;
173 case SCLK_I2C2:
174 con_id = 27;
175 break;
176 case SCLK_I2C3:
177 con_id = 28;
178 break;
179 default:
180 printf("do not support this i2c bus\n");
181 return -EINVAL;
182 }
183 rk_clrsetreg(&cru->clksel_con[con_id],
184 CLK_I2C_PLL_SEL_MASK | CLK_I2C_DIV_CON_MASK,
185 CLK_I2C_PLL_SEL_DPLL << CLK_I2C_PLL_SEL_SHIFT |
186 (src_clk_div - 1) << CLK_I2C_DIV_CON_SHIFT);
187
188 return rk3308_i2c_get_clk(clk);
189}
190
191static ulong rk3308_mac_set_clk(struct clk *clk, uint hz)
192{
193 struct rk3308_clk_priv *priv = dev_get_priv(clk->dev);
194 struct rk3308_cru *cru = priv->cru;
195 u32 con = readl(&cru->clksel_con[43]);
196 ulong pll_rate;
197 u8 div;
198
199 if ((con >> MAC_PLL_SHIFT) & MAC_SEL_VPLL0)
200 pll_rate = rockchip_pll_get_rate(&rk3308_pll_clks[VPLL0],
201 priv->cru, VPLL0);
202 else if ((con >> MAC_PLL_SHIFT) & MAC_SEL_VPLL1)
203 pll_rate = rockchip_pll_get_rate(&rk3308_pll_clks[VPLL1],
204 priv->cru, VPLL1);
205 else
206 pll_rate = rockchip_pll_get_rate(&rk3308_pll_clks[DPLL],
207 priv->cru, DPLL);
208
209 /*default set 50MHZ for gmac*/
210 if (!hz)
211 hz = 50000000;
212
213 div = DIV_ROUND_UP(pll_rate, hz) - 1;
214 assert(div < 32);
215 rk_clrsetreg(&cru->clksel_con[43], MAC_DIV_MASK,
216 div << MAC_DIV_SHIFT);
217
218 return DIV_TO_RATE(pll_rate, div);
219}
220
221static int rk3308_mac_set_speed_clk(struct clk *clk, uint hz)
222{
223 struct rk3308_clk_priv *priv = dev_get_priv(clk->dev);
224 struct rk3308_cru *cru = priv->cru;
225
226 if (hz != 2500000 && hz != 25000000) {
227 debug("Unsupported mac speed:%d\n", hz);
228 return -EINVAL;
229 }
230
231 rk_clrsetreg(&cru->clksel_con[43], MAC_CLK_SPEED_SEL_MASK,
232 ((hz == 2500000) ? 0 : 1) << MAC_CLK_SPEED_SEL_SHIFT);
233
234 return 0;
235}
236
237static ulong rk3308_mmc_get_clk(struct clk *clk)
238{
239 struct rk3308_clk_priv *priv = dev_get_priv(clk->dev);
240 struct rk3308_cru *cru = priv->cru;
241 u32 div, con, con_id;
242
243 switch (clk->id) {
244 case HCLK_SDMMC:
245 case SCLK_SDMMC:
246 con_id = 39;
247 break;
248 case HCLK_EMMC:
249 case SCLK_EMMC:
250 case SCLK_EMMC_SAMPLE:
251 con_id = 41;
252 break;
253 default:
254 return -EINVAL;
255 }
256
257 con = readl(&cru->clksel_con[con_id]);
258 div = (con & EMMC_DIV_MASK) >> EMMC_DIV_SHIFT;
259
260 if ((con & EMMC_PLL_MASK) >> EMMC_PLL_SHIFT
261 == EMMC_SEL_24M)
262 return DIV_TO_RATE(OSC_HZ, div) / 2;
263 else
264 return DIV_TO_RATE(priv->vpll0_hz, div) / 2;
265}
266
267static ulong rk3308_mmc_set_clk(struct clk *clk, ulong set_rate)
268{
269 struct rk3308_clk_priv *priv = dev_get_priv(clk->dev);
270 struct rk3308_cru *cru = priv->cru;
271 int src_clk_div;
272 u32 con_id;
273
274 switch (clk->id) {
275 case HCLK_SDMMC:
276 case SCLK_SDMMC:
277 con_id = 39;
278 break;
279 case HCLK_EMMC:
280 case SCLK_EMMC:
281 con_id = 41;
282 break;
283 default:
284 return -EINVAL;
285 }
286 /* Select clk_sdmmc/emmc source from VPLL0 by default */
287 /* mmc clock defaulg div 2 internal, need provide double in cru */
288 src_clk_div = DIV_ROUND_UP(priv->vpll0_hz / 2, set_rate);
289
290 if (src_clk_div > 127) {
291 /* use 24MHz source for 400KHz clock */
292 src_clk_div = DIV_ROUND_UP(OSC_HZ / 2, set_rate);
293 rk_clrsetreg(&cru->clksel_con[con_id],
294 EMMC_PLL_MASK | EMMC_DIV_MASK | EMMC_CLK_SEL_MASK,
295 EMMC_CLK_SEL_EMMC << EMMC_CLK_SEL_SHIFT |
296 EMMC_SEL_24M << EMMC_PLL_SHIFT |
297 (src_clk_div - 1) << EMMC_DIV_SHIFT);
298 } else {
299 rk_clrsetreg(&cru->clksel_con[con_id],
300 EMMC_PLL_MASK | EMMC_DIV_MASK | EMMC_CLK_SEL_MASK,
301 EMMC_CLK_SEL_EMMC << EMMC_CLK_SEL_SHIFT |
302 EMMC_SEL_VPLL0 << EMMC_PLL_SHIFT |
303 (src_clk_div - 1) << EMMC_DIV_SHIFT);
304 }
305
306 return rk3308_mmc_get_clk(clk);
307}
308
309static ulong rk3308_saradc_get_clk(struct clk *clk)
310{
311 struct rk3308_clk_priv *priv = dev_get_priv(clk->dev);
312 struct rk3308_cru *cru = priv->cru;
313 u32 div, con;
314
315 con = readl(&cru->clksel_con[34]);
Massimo Pegorer00a8fa32023-08-03 13:08:11 +0200316 div = (con & CLK_SARADC_DIV_CON_MASK) >> CLK_SARADC_DIV_CON_SHIFT;
Finley Xiaoafa71602019-11-14 11:21:13 +0800317
318 return DIV_TO_RATE(OSC_HZ, div);
319}
320
321static ulong rk3308_saradc_set_clk(struct clk *clk, uint hz)
322{
323 struct rk3308_clk_priv *priv = dev_get_priv(clk->dev);
324 struct rk3308_cru *cru = priv->cru;
325 int src_clk_div;
326
327 src_clk_div = DIV_ROUND_UP(OSC_HZ, hz);
328 assert(src_clk_div - 1 <= 2047);
329
330 rk_clrsetreg(&cru->clksel_con[34],
331 CLK_SARADC_DIV_CON_MASK,
332 (src_clk_div - 1) << CLK_SARADC_DIV_CON_SHIFT);
333
334 return rk3308_saradc_get_clk(clk);
335}
336
337static ulong rk3308_tsadc_get_clk(struct clk *clk)
338{
339 struct rk3308_clk_priv *priv = dev_get_priv(clk->dev);
340 struct rk3308_cru *cru = priv->cru;
341 u32 div, con;
342
343 con = readl(&cru->clksel_con[33]);
Massimo Pegorer00a8fa32023-08-03 13:08:11 +0200344 div = (con & CLK_SARADC_DIV_CON_MASK) >> CLK_SARADC_DIV_CON_SHIFT;
Finley Xiaoafa71602019-11-14 11:21:13 +0800345
346 return DIV_TO_RATE(OSC_HZ, div);
347}
348
349static ulong rk3308_tsadc_set_clk(struct clk *clk, uint hz)
350{
351 struct rk3308_clk_priv *priv = dev_get_priv(clk->dev);
352 struct rk3308_cru *cru = priv->cru;
353 int src_clk_div;
354
355 src_clk_div = DIV_ROUND_UP(OSC_HZ, hz);
356 assert(src_clk_div - 1 <= 2047);
357
358 rk_clrsetreg(&cru->clksel_con[33],
359 CLK_SARADC_DIV_CON_MASK,
360 (src_clk_div - 1) << CLK_SARADC_DIV_CON_SHIFT);
361
362 return rk3308_tsadc_get_clk(clk);
363}
364
365static ulong rk3308_spi_get_clk(struct clk *clk)
366{
367 struct rk3308_clk_priv *priv = dev_get_priv(clk->dev);
368 struct rk3308_cru *cru = priv->cru;
369 u32 div, con, con_id;
370
371 switch (clk->id) {
372 case SCLK_SPI0:
373 con_id = 30;
374 break;
375 case SCLK_SPI1:
376 con_id = 31;
377 break;
378 case SCLK_SPI2:
379 con_id = 32;
380 break;
381 default:
382 printf("do not support this spi bus\n");
383 return -EINVAL;
384 }
385
386 con = readl(&cru->clksel_con[con_id]);
Massimo Pegorer00a8fa32023-08-03 13:08:11 +0200387 div = (con & CLK_SPI_DIV_CON_MASK) >> CLK_SPI_DIV_CON_SHIFT;
Finley Xiaoafa71602019-11-14 11:21:13 +0800388
389 return DIV_TO_RATE(priv->dpll_hz, div);
390}
391
392static ulong rk3308_spi_set_clk(struct clk *clk, uint hz)
393{
394 struct rk3308_clk_priv *priv = dev_get_priv(clk->dev);
395 struct rk3308_cru *cru = priv->cru;
396 u32 src_clk_div, con_id;
397
398 src_clk_div = DIV_ROUND_UP(priv->dpll_hz, hz);
399 assert(src_clk_div - 1 <= 127);
400
401 switch (clk->id) {
402 case SCLK_SPI0:
403 con_id = 30;
404 break;
405 case SCLK_SPI1:
406 con_id = 31;
407 break;
408 case SCLK_SPI2:
409 con_id = 32;
410 break;
411 default:
412 printf("do not support this spi bus\n");
413 return -EINVAL;
414 }
415
416 rk_clrsetreg(&cru->clksel_con[con_id],
417 CLK_SPI_PLL_SEL_MASK | CLK_SPI_DIV_CON_MASK,
418 CLK_SPI_PLL_SEL_DPLL << CLK_SPI_PLL_SEL_SHIFT |
419 (src_clk_div - 1) << CLK_SPI_DIV_CON_SHIFT);
420
421 return rk3308_spi_get_clk(clk);
422}
423
424static ulong rk3308_pwm_get_clk(struct clk *clk)
425{
426 struct rk3308_clk_priv *priv = dev_get_priv(clk->dev);
427 struct rk3308_cru *cru = priv->cru;
428 u32 div, con;
429
430 con = readl(&cru->clksel_con[29]);
Massimo Pegorer00a8fa32023-08-03 13:08:11 +0200431 div = (con & CLK_PWM_DIV_CON_MASK) >> CLK_PWM_DIV_CON_SHIFT;
Finley Xiaoafa71602019-11-14 11:21:13 +0800432
433 return DIV_TO_RATE(priv->dpll_hz, div);
434}
435
436static ulong rk3308_pwm_set_clk(struct clk *clk, uint hz)
437{
438 struct rk3308_clk_priv *priv = dev_get_priv(clk->dev);
439 struct rk3308_cru *cru = priv->cru;
440 int src_clk_div;
441
442 src_clk_div = DIV_ROUND_UP(priv->dpll_hz, hz);
443 assert(src_clk_div - 1 <= 127);
444
445 rk_clrsetreg(&cru->clksel_con[29],
446 CLK_PWM_PLL_SEL_MASK | CLK_PWM_DIV_CON_MASK,
447 CLK_PWM_PLL_SEL_DPLL << CLK_PWM_PLL_SEL_SHIFT |
448 (src_clk_div - 1) << CLK_PWM_DIV_CON_SHIFT);
449
450 return rk3308_pwm_get_clk(clk);
451}
452
Massimo Pegorer2d7b0d02023-08-03 13:08:12 +0200453static ulong rk3308_uart_get_clk(struct clk *clk)
454{
455 struct rk3308_clk_priv *priv = dev_get_priv(clk->dev);
456 struct rk3308_cru *cru = priv->cru;
457 u32 div, pll_sel, con, con_id, parent;
458
459 switch (clk->id) {
460 case SCLK_UART0:
461 con_id = 10;
462 break;
463 case SCLK_UART1:
464 con_id = 13;
465 break;
466 case SCLK_UART2:
467 con_id = 16;
468 break;
469 case SCLK_UART3:
470 con_id = 19;
471 break;
472 case SCLK_UART4:
473 con_id = 22;
474 break;
475 default:
476 printf("do not support this uart interface\n");
477 return -EINVAL;
478 }
479
480 con = readl(&cru->clksel_con[con_id]);
481 pll_sel = (con & CLK_UART_PLL_SEL_MASK) >> CLK_UART_PLL_SEL_SHIFT;
482 div = (con & CLK_UART_DIV_CON_MASK) >> CLK_UART_DIV_CON_SHIFT;
483
484 switch (pll_sel) {
485 case CLK_UART_PLL_SEL_DPLL:
486 parent = priv->dpll_hz;
487 break;
488 case CLK_UART_PLL_SEL_VPLL0:
489 parent = priv->vpll0_hz;
490 break;
491 case CLK_UART_PLL_SEL_VPLL1:
492 parent = priv->vpll0_hz;
493 break;
494 case CLK_UART_PLL_SEL_24M:
495 parent = OSC_HZ;
496 break;
497 default:
498 printf("do not support this uart pll sel\n");
499 return -EINVAL;
500 }
501
502 return DIV_TO_RATE(parent, div);
503}
504
Finley Xiaoafa71602019-11-14 11:21:13 +0800505static ulong rk3308_vop_get_clk(struct clk *clk)
506{
507 struct rk3308_clk_priv *priv = dev_get_priv(clk->dev);
508 struct rk3308_cru *cru = priv->cru;
509 u32 div, pll_sel, vol_sel, con, parent;
510
511 con = readl(&cru->clksel_con[8]);
512 vol_sel = (con & DCLK_VOP_SEL_MASK) >> DCLK_VOP_SEL_SHIFT;
513 pll_sel = (con & DCLK_VOP_PLL_SEL_MASK) >> DCLK_VOP_PLL_SEL_SHIFT;
514 div = con & DCLK_VOP_DIV_MASK;
515
516 if (vol_sel == DCLK_VOP_SEL_24M) {
517 parent = OSC_HZ;
518 } else if (vol_sel == DCLK_VOP_SEL_DIVOUT) {
519 switch (pll_sel) {
520 case DCLK_VOP_PLL_SEL_DPLL:
521 parent = priv->dpll_hz;
522 break;
523 case DCLK_VOP_PLL_SEL_VPLL0:
524 parent = priv->vpll0_hz;
525 break;
526 case DCLK_VOP_PLL_SEL_VPLL1:
527 parent = priv->vpll0_hz;
528 break;
529 default:
530 printf("do not support this vop pll sel\n");
531 return -EINVAL;
532 }
533 } else {
534 printf("do not support this vop sel\n");
535 return -EINVAL;
536 }
537
538 return DIV_TO_RATE(parent, div);
539}
540
541static ulong rk3308_vop_set_clk(struct clk *clk, ulong hz)
542{
543 struct rk3308_clk_priv *priv = dev_get_priv(clk->dev);
544 struct rk3308_cru *cru = priv->cru;
545 ulong pll_rate, now, best_rate = 0;
546 u32 i, div, best_div = 0, best_sel = 0;
547
548 for (i = 0; i <= DCLK_VOP_PLL_SEL_VPLL1; i++) {
549 switch (i) {
550 case DCLK_VOP_PLL_SEL_DPLL:
551 pll_rate = priv->dpll_hz;
552 break;
553 case DCLK_VOP_PLL_SEL_VPLL0:
554 pll_rate = priv->vpll0_hz;
555 break;
556 case DCLK_VOP_PLL_SEL_VPLL1:
557 pll_rate = priv->vpll1_hz;
558 break;
559 default:
560 printf("do not support this vop pll sel\n");
561 return -EINVAL;
562 }
563
564 div = DIV_ROUND_UP(pll_rate, hz);
565 if (div > 255)
566 continue;
567 now = pll_rate / div;
568 if (abs(hz - now) < abs(hz - best_rate)) {
569 best_rate = now;
570 best_div = div;
571 best_sel = i;
572 }
573 debug("pll_rate=%lu, best_rate=%lu, best_div=%u, best_sel=%u\n",
574 pll_rate, best_rate, best_div, best_sel);
575 }
576
577 if (best_rate != hz && hz == OSC_HZ) {
578 rk_clrsetreg(&cru->clksel_con[8],
579 DCLK_VOP_SEL_MASK,
580 DCLK_VOP_SEL_24M << DCLK_VOP_SEL_SHIFT);
581 } else if (best_rate) {
582 rk_clrsetreg(&cru->clksel_con[8],
583 DCLK_VOP_SEL_MASK | DCLK_VOP_PLL_SEL_MASK |
584 DCLK_VOP_DIV_MASK,
585 DCLK_VOP_SEL_DIVOUT << DCLK_VOP_SEL_SHIFT |
586 best_sel << DCLK_VOP_PLL_SEL_SHIFT |
587 (best_div - 1) << DCLK_VOP_DIV_SHIFT);
588 } else {
589 printf("do not support this vop freq\n");
590 return -EINVAL;
591 }
592
593 return rk3308_vop_get_clk(clk);
594}
595
596static ulong rk3308_bus_get_clk(struct rk3308_clk_priv *priv, ulong clk_id)
597{
598 struct rk3308_cru *cru = priv->cru;
599 u32 div, con, parent = priv->dpll_hz;
600
601 switch (clk_id) {
602 case ACLK_BUS:
603 con = readl(&cru->clksel_con[5]);
604 div = (con & BUS_ACLK_DIV_MASK) >> BUS_ACLK_DIV_SHIFT;
605 break;
606 case HCLK_BUS:
607 con = readl(&cru->clksel_con[6]);
608 div = (con & BUS_HCLK_DIV_MASK) >> BUS_HCLK_DIV_SHIFT;
609 break;
610 case PCLK_BUS:
611 case PCLK_WDT:
612 con = readl(&cru->clksel_con[6]);
613 div = (con & BUS_PCLK_DIV_MASK) >> BUS_PCLK_DIV_SHIFT;
614 break;
615 default:
616 return -ENOENT;
617 }
618
619 return DIV_TO_RATE(parent, div);
620}
621
622static ulong rk3308_bus_set_clk(struct rk3308_clk_priv *priv, ulong clk_id,
623 ulong hz)
624{
625 struct rk3308_cru *cru = priv->cru;
626 int src_clk_div;
627
628 src_clk_div = DIV_ROUND_UP(priv->dpll_hz, hz);
629 assert(src_clk_div - 1 <= 31);
630
631 /*
632 * select dpll as pd_bus bus clock source and
633 * set up dependent divisors for PCLK/HCLK and ACLK clocks.
634 */
635 switch (clk_id) {
636 case ACLK_BUS:
637 rk_clrsetreg(&cru->clksel_con[5],
638 BUS_PLL_SEL_MASK | BUS_ACLK_DIV_MASK,
639 BUS_PLL_SEL_DPLL << BUS_PLL_SEL_SHIFT |
640 (src_clk_div - 1) << BUS_ACLK_DIV_SHIFT);
641 break;
642 case HCLK_BUS:
643 rk_clrsetreg(&cru->clksel_con[6],
644 BUS_HCLK_DIV_MASK,
645 (src_clk_div - 1) << BUS_HCLK_DIV_SHIFT);
646 break;
647 case PCLK_BUS:
648 rk_clrsetreg(&cru->clksel_con[6],
649 BUS_PCLK_DIV_MASK,
650 (src_clk_div - 1) << BUS_PCLK_DIV_SHIFT);
651 break;
652 default:
653 printf("do not support this bus freq\n");
654 return -EINVAL;
655 }
656
657 return rk3308_bus_get_clk(priv, clk_id);
658}
659
660static ulong rk3308_peri_get_clk(struct rk3308_clk_priv *priv, ulong clk_id)
661{
662 struct rk3308_cru *cru = priv->cru;
663 u32 div, con, parent = priv->dpll_hz;
664
665 switch (clk_id) {
666 case ACLK_PERI:
667 con = readl(&cru->clksel_con[36]);
668 div = (con & PERI_ACLK_DIV_MASK) >> PERI_ACLK_DIV_SHIFT;
669 break;
670 case HCLK_PERI:
671 con = readl(&cru->clksel_con[37]);
672 div = (con & PERI_HCLK_DIV_MASK) >> PERI_HCLK_DIV_SHIFT;
673 break;
674 case PCLK_PERI:
675 con = readl(&cru->clksel_con[37]);
676 div = (con & PERI_PCLK_DIV_MASK) >> PERI_PCLK_DIV_SHIFT;
677 break;
678 default:
679 return -ENOENT;
680 }
681
682 return DIV_TO_RATE(parent, div);
683}
684
685static ulong rk3308_peri_set_clk(struct rk3308_clk_priv *priv, ulong clk_id,
686 ulong hz)
687{
688 struct rk3308_cru *cru = priv->cru;
689 int src_clk_div;
690
691 src_clk_div = DIV_ROUND_UP(priv->dpll_hz, hz);
692 assert(src_clk_div - 1 <= 31);
693
694 /*
695 * select dpll as pd_peri bus clock source and
696 * set up dependent divisors for PCLK/HCLK and ACLK clocks.
697 */
698 switch (clk_id) {
699 case ACLK_PERI:
700 rk_clrsetreg(&cru->clksel_con[36],
701 PERI_PLL_SEL_MASK | PERI_ACLK_DIV_MASK,
702 PERI_PLL_DPLL << PERI_PLL_SEL_SHIFT |
703 (src_clk_div - 1) << PERI_ACLK_DIV_SHIFT);
704 break;
705 case HCLK_PERI:
706 rk_clrsetreg(&cru->clksel_con[37],
707 PERI_HCLK_DIV_MASK,
708 (src_clk_div - 1) << PERI_HCLK_DIV_SHIFT);
709 break;
710 case PCLK_PERI:
711 rk_clrsetreg(&cru->clksel_con[37],
712 PERI_PCLK_DIV_MASK,
713 (src_clk_div - 1) << PERI_PCLK_DIV_SHIFT);
714 break;
715 default:
716 printf("do not support this peri freq\n");
717 return -EINVAL;
718 }
719
720 return rk3308_peri_get_clk(priv, clk_id);
721}
722
723static ulong rk3308_audio_get_clk(struct rk3308_clk_priv *priv, ulong clk_id)
724{
725 struct rk3308_cru *cru = priv->cru;
726 u32 div, con, parent = priv->vpll0_hz;
727
728 switch (clk_id) {
729 case HCLK_AUDIO:
730 con = readl(&cru->clksel_con[45]);
731 div = (con & AUDIO_HCLK_DIV_MASK) >> AUDIO_HCLK_DIV_SHIFT;
732 break;
733 case PCLK_AUDIO:
734 con = readl(&cru->clksel_con[45]);
735 div = (con & AUDIO_PCLK_DIV_MASK) >> AUDIO_PCLK_DIV_SHIFT;
736 break;
737 default:
738 return -ENOENT;
739 }
740
741 return DIV_TO_RATE(parent, div);
742}
743
744static ulong rk3308_audio_set_clk(struct rk3308_clk_priv *priv, ulong clk_id,
745 ulong hz)
746{
747 struct rk3308_cru *cru = priv->cru;
748 int src_clk_div;
749
750 src_clk_div = DIV_ROUND_UP(priv->vpll0_hz, hz);
751 assert(src_clk_div - 1 <= 31);
752
753 /*
754 * select vpll0 as audio bus clock source and
755 * set up dependent divisors for HCLK and PCLK clocks.
756 */
757 switch (clk_id) {
758 case HCLK_AUDIO:
759 rk_clrsetreg(&cru->clksel_con[45],
760 AUDIO_PLL_SEL_MASK | AUDIO_HCLK_DIV_MASK,
761 AUDIO_PLL_VPLL0 << AUDIO_PLL_SEL_SHIFT |
762 (src_clk_div - 1) << AUDIO_HCLK_DIV_SHIFT);
763 break;
764 case PCLK_AUDIO:
765 rk_clrsetreg(&cru->clksel_con[45],
766 AUDIO_PLL_SEL_MASK | AUDIO_PCLK_DIV_MASK,
767 AUDIO_PLL_VPLL0 << AUDIO_PLL_SEL_SHIFT |
768 (src_clk_div - 1) << AUDIO_PCLK_DIV_SHIFT);
769 break;
770 default:
771 printf("do not support this audio freq\n");
772 return -EINVAL;
773 }
774
775 return rk3308_peri_get_clk(priv, clk_id);
776}
777
778static ulong rk3308_crypto_get_clk(struct rk3308_clk_priv *priv, ulong clk_id)
779{
780 struct rk3308_cru *cru = priv->cru;
781 u32 div, con, parent;
782
783 switch (clk_id) {
784 case SCLK_CRYPTO:
785 con = readl(&cru->clksel_con[7]);
786 div = (con & CRYPTO_DIV_MASK) >> CRYPTO_DIV_SHIFT;
787 parent = priv->vpll0_hz;
788 break;
789 case SCLK_CRYPTO_APK:
790 con = readl(&cru->clksel_con[7]);
791 div = (con & CRYPTO_APK_DIV_MASK) >> CRYPTO_APK_DIV_SHIFT;
792 parent = priv->vpll0_hz;
793 break;
794 default:
795 return -ENOENT;
796 }
797
798 return DIV_TO_RATE(parent, div);
799}
800
801static ulong rk3308_crypto_set_clk(struct rk3308_clk_priv *priv, ulong clk_id,
802 ulong hz)
803{
804 struct rk3308_cru *cru = priv->cru;
805 int src_clk_div;
806
807 src_clk_div = DIV_ROUND_UP(priv->vpll0_hz, hz);
808 assert(src_clk_div - 1 <= 31);
809
810 /*
811 * select gpll as crypto clock source and
812 * set up dependent divisors for crypto clocks.
813 */
814 switch (clk_id) {
815 case SCLK_CRYPTO:
816 rk_clrsetreg(&cru->clksel_con[7],
817 CRYPTO_PLL_SEL_MASK | CRYPTO_DIV_MASK,
818 CRYPTO_PLL_SEL_VPLL0 << CRYPTO_PLL_SEL_SHIFT |
819 (src_clk_div - 1) << CRYPTO_DIV_SHIFT);
820 break;
821 case SCLK_CRYPTO_APK:
822 rk_clrsetreg(&cru->clksel_con[7],
823 CRYPTO_APK_PLL_SEL_MASK | CRYPTO_APK_DIV_MASK,
824 CRYPTO_PLL_SEL_VPLL0 << CRYPTO_APK_SEL_SHIFT |
825 (src_clk_div - 1) << CRYPTO_APK_DIV_SHIFT);
826 break;
827 default:
828 printf("do not support this peri freq\n");
829 return -EINVAL;
830 }
831
832 return rk3308_crypto_get_clk(priv, clk_id);
833}
834
835static ulong rk3308_clk_get_rate(struct clk *clk)
836{
837 struct rk3308_clk_priv *priv = dev_get_priv(clk->dev);
838 ulong rate = 0;
839
840 debug("%s id:%ld\n", __func__, clk->id);
841
842 switch (clk->id) {
843 case PLL_APLL:
844 case ARMCLK:
845 rate = rockchip_pll_get_rate(&rk3308_pll_clks[APLL],
846 priv->cru, APLL);
847 break;
848 case PLL_DPLL:
849 rate = rockchip_pll_get_rate(&rk3308_pll_clks[DPLL],
850 priv->cru, DPLL);
851 break;
852 case PLL_VPLL0:
853 rate = rockchip_pll_get_rate(&rk3308_pll_clks[VPLL0],
854 priv->cru, VPLL0);
855 break;
856 case PLL_VPLL1:
857 rate = rockchip_pll_get_rate(&rk3308_pll_clks[VPLL1],
858 priv->cru, VPLL1);
859 break;
860 case HCLK_SDMMC:
861 case HCLK_EMMC:
862 case SCLK_SDMMC:
863 case SCLK_EMMC:
864 case SCLK_EMMC_SAMPLE:
865 rate = rk3308_mmc_get_clk(clk);
866 break;
Massimo Pegorer2d7b0d02023-08-03 13:08:12 +0200867 case SCLK_UART0:
868 case SCLK_UART1:
869 case SCLK_UART2:
870 case SCLK_UART3:
871 case SCLK_UART4:
872 rate = rk3308_uart_get_clk(clk);
873 break;
Finley Xiaoafa71602019-11-14 11:21:13 +0800874 case SCLK_I2C0:
875 case SCLK_I2C1:
876 case SCLK_I2C2:
877 case SCLK_I2C3:
878 rate = rk3308_i2c_get_clk(clk);
879 break;
880 case SCLK_SARADC:
881 rate = rk3308_saradc_get_clk(clk);
882 break;
883 case SCLK_TSADC:
884 rate = rk3308_tsadc_get_clk(clk);
885 break;
886 case SCLK_SPI0:
887 case SCLK_SPI1:
888 rate = rk3308_spi_get_clk(clk);
889 break;
890 case SCLK_PWM0:
891 rate = rk3308_pwm_get_clk(clk);
892 break;
893 case DCLK_VOP:
894 rate = rk3308_vop_get_clk(clk);
895 break;
896 case ACLK_BUS:
897 case HCLK_BUS:
898 case PCLK_BUS:
899 case PCLK_WDT:
900 rate = rk3308_bus_get_clk(priv, clk->id);
901 break;
902 case ACLK_PERI:
903 case HCLK_PERI:
904 case PCLK_PERI:
905 rate = rk3308_peri_get_clk(priv, clk->id);
906 break;
907 case HCLK_AUDIO:
908 case PCLK_AUDIO:
909 rate = rk3308_audio_get_clk(priv, clk->id);
910 break;
911 case SCLK_CRYPTO:
912 case SCLK_CRYPTO_APK:
913 rate = rk3308_crypto_get_clk(priv, clk->id);
914 break;
915 default:
916 return -ENOENT;
917 }
918
919 return rate;
920}
921
922static ulong rk3308_clk_set_rate(struct clk *clk, ulong rate)
923{
924 struct rk3308_clk_priv *priv = dev_get_priv(clk->dev);
925 ulong ret = 0;
926
927 debug("%s %ld %ld\n", __func__, clk->id, rate);
928
929 switch (clk->id) {
930 case PLL_DPLL:
931 ret = rockchip_pll_set_rate(&rk3308_pll_clks[DPLL], priv->cru,
932 DPLL, rate);
933 priv->dpll_hz = rockchip_pll_get_rate(&rk3308_pll_clks[DPLL],
934 priv->cru, DPLL);
935 break;
936 case ARMCLK:
937 if (priv->armclk_hz)
938 rk3308_armclk_set_clk(priv, rate);
939 priv->armclk_hz = rate;
940 break;
941 case HCLK_SDMMC:
942 case HCLK_EMMC:
943 case SCLK_SDMMC:
944 case SCLK_EMMC:
945 ret = rk3308_mmc_set_clk(clk, rate);
946 break;
947 case SCLK_I2C0:
948 case SCLK_I2C1:
949 case SCLK_I2C2:
950 case SCLK_I2C3:
951 ret = rk3308_i2c_set_clk(clk, rate);
952 break;
953 case SCLK_MAC:
954 ret = rk3308_mac_set_clk(clk, rate);
955 break;
956 case SCLK_MAC_RMII:
957 ret = rk3308_mac_set_speed_clk(clk, rate);
958 break;
959 case SCLK_SARADC:
960 ret = rk3308_saradc_set_clk(clk, rate);
961 break;
962 case SCLK_TSADC:
963 ret = rk3308_tsadc_set_clk(clk, rate);
964 break;
965 case SCLK_SPI0:
966 case SCLK_SPI1:
967 ret = rk3308_spi_set_clk(clk, rate);
968 break;
969 case SCLK_PWM0:
970 ret = rk3308_pwm_set_clk(clk, rate);
971 break;
972 case DCLK_VOP:
973 ret = rk3308_vop_set_clk(clk, rate);
974 break;
975 case ACLK_BUS:
976 case HCLK_BUS:
977 case PCLK_BUS:
978 rate = rk3308_bus_set_clk(priv, clk->id, rate);
979 break;
980 case ACLK_PERI:
981 case HCLK_PERI:
982 case PCLK_PERI:
983 rate = rk3308_peri_set_clk(priv, clk->id, rate);
984 break;
985 case HCLK_AUDIO:
986 case PCLK_AUDIO:
987 rate = rk3308_audio_set_clk(priv, clk->id, rate);
988 break;
989 case SCLK_CRYPTO:
990 case SCLK_CRYPTO_APK:
991 ret = rk3308_crypto_set_clk(priv, clk->id, rate);
992 break;
993 default:
994 return -ENOENT;
995 }
996
997 return ret;
998}
999
Simon Glass3580f6d2021-08-07 07:24:03 -06001000#if CONFIG_IS_ENABLED(OF_REAL)
Finley Xiaoafa71602019-11-14 11:21:13 +08001001static int __maybe_unused rk3308_mac_set_parent(struct clk *clk, struct clk *parent)
1002{
1003 struct rk3308_clk_priv *priv = dev_get_priv(clk->dev);
1004
1005 /*
1006 * If the requested parent is in the same clock-controller and
1007 * the id is SCLK_MAC_SRC, switch to the internal clock.
1008 */
1009 if (parent->id == SCLK_MAC_SRC) {
1010 debug("%s: switching RMII to SCLK_MAC\n", __func__);
1011 rk_clrreg(&priv->cru->clksel_con[43], BIT(14));
1012 } else {
1013 debug("%s: switching RMII to CLKIN\n", __func__);
1014 rk_setreg(&priv->cru->clksel_con[43], BIT(14));
1015 }
1016
1017 return 0;
1018}
1019
1020static int __maybe_unused rk3308_clk_set_parent(struct clk *clk, struct clk *parent)
1021{
1022 switch (clk->id) {
1023 case SCLK_MAC:
1024 return rk3308_mac_set_parent(clk, parent);
1025 default:
1026 break;
1027 }
1028
1029 debug("%s: unsupported clk %ld\n", __func__, clk->id);
1030 return -ENOENT;
1031}
1032#endif
1033
1034static struct clk_ops rk3308_clk_ops = {
1035 .get_rate = rk3308_clk_get_rate,
1036 .set_rate = rk3308_clk_set_rate,
Simon Glass3580f6d2021-08-07 07:24:03 -06001037#if CONFIG_IS_ENABLED(OF_REAL)
Finley Xiaoafa71602019-11-14 11:21:13 +08001038 .set_parent = rk3308_clk_set_parent,
1039#endif
1040};
1041
1042static void rk3308_clk_init(struct udevice *dev)
1043{
1044 struct rk3308_clk_priv *priv = dev_get_priv(dev);
1045 int ret;
1046
1047 if (rockchip_pll_get_rate(&rk3308_pll_clks[APLL],
1048 priv->cru, APLL) != APLL_HZ) {
1049 ret = rk3308_armclk_set_clk(priv, APLL_HZ);
1050 if (ret < 0)
1051 printf("%s failed to set armclk rate\n", __func__);
1052 }
1053
1054 rk3308_clk_get_pll_rate(priv);
1055
1056 rk3308_bus_set_clk(priv, ACLK_BUS, BUS_ACLK_HZ);
1057 rk3308_bus_set_clk(priv, HCLK_BUS, BUS_HCLK_HZ);
1058 rk3308_bus_set_clk(priv, PCLK_BUS, BUS_PCLK_HZ);
1059
1060 rk3308_peri_set_clk(priv, ACLK_PERI, PERI_ACLK_HZ);
1061 rk3308_peri_set_clk(priv, HCLK_PERI, PERI_HCLK_HZ);
1062 rk3308_peri_set_clk(priv, PCLK_PERI, PERI_PCLK_HZ);
1063
1064 rk3308_audio_set_clk(priv, HCLK_AUDIO, AUDIO_HCLK_HZ);
1065 rk3308_audio_set_clk(priv, PCLK_AUDIO, AUDIO_PCLK_HZ);
1066}
1067
1068static int rk3308_clk_probe(struct udevice *dev)
1069{
1070 int ret;
1071
1072 rk3308_clk_init(dev);
1073
1074 /* Process 'assigned-{clocks/clock-parents/clock-rates}' properties */
Sean Anderson08d531c2021-06-11 00:16:07 -04001075 ret = clk_set_defaults(dev, CLK_DEFAULTS_POST);
Finley Xiaoafa71602019-11-14 11:21:13 +08001076 if (ret)
1077 debug("%s clk_set_defaults failed %d\n", __func__, ret);
1078
1079 return ret;
1080}
1081
Simon Glassaad29ae2020-12-03 16:55:21 -07001082static int rk3308_clk_of_to_plat(struct udevice *dev)
Finley Xiaoafa71602019-11-14 11:21:13 +08001083{
1084 struct rk3308_clk_priv *priv = dev_get_priv(dev);
1085
1086 priv->cru = dev_read_addr_ptr(dev);
1087
1088 return 0;
1089}
1090
1091static int rk3308_clk_bind(struct udevice *dev)
1092{
1093 int ret;
1094 struct udevice *sys_child;
1095 struct sysreset_reg *priv;
1096
1097 /* The reset driver does not have a device node, so bind it here */
1098 ret = device_bind_driver(dev, "rockchip_sysreset", "sysreset",
1099 &sys_child);
1100 if (ret) {
1101 debug("Warning: No sysreset driver: ret=%d\n", ret);
1102 } else {
1103 priv = malloc(sizeof(struct sysreset_reg));
1104 priv->glb_srst_fst_value = offsetof(struct rk3308_cru,
1105 glb_srst_fst);
1106 priv->glb_srst_snd_value = offsetof(struct rk3308_cru,
1107 glb_srst_snd);
Simon Glass95588622020-12-22 19:30:28 -07001108 dev_set_priv(sys_child, priv);
Finley Xiaoafa71602019-11-14 11:21:13 +08001109 }
1110
1111#if CONFIG_IS_ENABLED(RESET_ROCKCHIP)
1112 ret = offsetof(struct rk3308_cru, softrst_con[0]);
1113 ret = rockchip_reset_bind(dev, ret, 12);
1114 if (ret)
Eugen Hristevf1798262023-04-11 10:17:56 +03001115 debug("Warning: software reset driver bind failed\n");
Finley Xiaoafa71602019-11-14 11:21:13 +08001116#endif
1117
1118 return 0;
1119}
1120
1121static const struct udevice_id rk3308_clk_ids[] = {
1122 { .compatible = "rockchip,rk3308-cru" },
1123 { }
1124};
1125
1126U_BOOT_DRIVER(rockchip_rk3308_cru) = {
1127 .name = "rockchip_rk3308_cru",
1128 .id = UCLASS_CLK,
1129 .of_match = rk3308_clk_ids,
Simon Glass8a2b47f2020-12-03 16:55:17 -07001130 .priv_auto = sizeof(struct rk3308_clk_priv),
Simon Glassaad29ae2020-12-03 16:55:21 -07001131 .of_to_plat = rk3308_clk_of_to_plat,
Finley Xiaoafa71602019-11-14 11:21:13 +08001132 .ops = &rk3308_clk_ops,
1133 .bind = rk3308_clk_bind,
1134 .probe = rk3308_clk_probe,
1135};