blob: 81cdb8ee9c7610f36f846b1fe356a3818c625782 [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0
Simon Glass99c15652015-08-30 16:55:31 -06002/*
3 * (C) Copyright 2015 Google, Inc
Simon Glass99c15652015-08-30 16:55:31 -06004 */
5
6#include <common.h>
David Wuef4cf5a2017-09-20 14:28:19 +08007#include <bitfield.h>
Stephen Warren135aa952016-06-17 09:44:00 -06008#include <clk-uclass.h>
Simon Glass3dbfe5a2018-12-27 20:15:20 -07009#include <div64.h>
Simon Glass99c15652015-08-30 16:55:31 -060010#include <dm.h>
Simon Glass2d143bd2016-07-04 11:58:29 -060011#include <dt-structs.h>
Simon Glass99c15652015-08-30 16:55:31 -060012#include <errno.h>
Simon Glass336d4612020-02-03 07:36:16 -070013#include <malloc.h>
Simon Glass2d143bd2016-07-04 11:58:29 -060014#include <mapmem.h>
Simon Glass99c15652015-08-30 16:55:31 -060015#include <syscon.h>
16#include <asm/io.h>
Kever Yang15f09a12019-03-28 11:01:23 +080017#include <asm/arch-rockchip/clock.h>
Jagan Tekib52a1992020-01-09 14:22:17 +053018#include <asm/arch-rockchip/cru.h>
Kever Yang15f09a12019-03-28 11:01:23 +080019#include <asm/arch-rockchip/grf_rk3288.h>
20#include <asm/arch-rockchip/hardware.h>
Simon Glass898d6432016-01-21 19:43:38 -070021#include <dt-bindings/clock/rk3288-cru.h>
Simon Glass64b7faa2016-01-21 19:43:41 -070022#include <dm/device-internal.h>
Simon Glass99c15652015-08-30 16:55:31 -060023#include <dm/lists.h>
Simon Glass64b7faa2016-01-21 19:43:41 -070024#include <dm/uclass-internal.h>
Simon Glass61b29b82020-02-03 07:36:15 -070025#include <linux/err.h>
Heiko Stübnerabd01282016-07-22 23:51:06 +020026#include <linux/log2.h>
Simon Glass99c15652015-08-30 16:55:31 -060027
28DECLARE_GLOBAL_DATA_PTR;
29
Simon Glass2d143bd2016-07-04 11:58:29 -060030struct rk3288_clk_plat {
31#if CONFIG_IS_ENABLED(OF_PLATDATA)
32 struct dtd_rockchip_rk3288_cru dtd;
33#endif
34};
35
Simon Glass99c15652015-08-30 16:55:31 -060036struct pll_div {
37 u32 nr;
38 u32 nf;
39 u32 no;
40};
41
42enum {
43 VCO_MAX_HZ = 2200U * 1000000,
44 VCO_MIN_HZ = 440 * 1000000,
45 OUTPUT_MAX_HZ = 2200U * 1000000,
46 OUTPUT_MIN_HZ = 27500000,
47 FREF_MAX_HZ = 2200U * 1000000,
Heiko Stübnerc3f03ff2016-07-16 00:17:17 +020048 FREF_MIN_HZ = 269 * 1000,
Simon Glass99c15652015-08-30 16:55:31 -060049};
50
51enum {
52 /* PLL CON0 */
53 PLL_OD_MASK = 0x0f,
54
55 /* PLL CON1 */
56 PLL_NF_MASK = 0x1fff,
57
58 /* PLL CON2 */
59 PLL_BWADJ_MASK = 0x0fff,
60
61 /* PLL CON3 */
62 PLL_RESET_SHIFT = 5,
63
Simon Glassdae594f2016-01-21 19:45:17 -070064 /* CLKSEL0 */
Simon Glassdae594f2016-01-21 19:45:17 -070065 CORE_SEL_PLL_SHIFT = 15,
Simon Glassb223c1a2017-05-31 17:57:31 -060066 CORE_SEL_PLL_MASK = 1 << CORE_SEL_PLL_SHIFT,
Simon Glassdae594f2016-01-21 19:45:17 -070067 A17_DIV_SHIFT = 8,
Simon Glassb223c1a2017-05-31 17:57:31 -060068 A17_DIV_MASK = 0x1f << A17_DIV_SHIFT,
Simon Glassdae594f2016-01-21 19:45:17 -070069 MP_DIV_SHIFT = 4,
Simon Glassb223c1a2017-05-31 17:57:31 -060070 MP_DIV_MASK = 0xf << MP_DIV_SHIFT,
Simon Glassdae594f2016-01-21 19:45:17 -070071 M0_DIV_SHIFT = 0,
Simon Glassb223c1a2017-05-31 17:57:31 -060072 M0_DIV_MASK = 0xf << M0_DIV_SHIFT,
Simon Glassdae594f2016-01-21 19:45:17 -070073
Simon Glass99c15652015-08-30 16:55:31 -060074 /* CLKSEL1: pd bus clk pll sel: codec or general */
75 PD_BUS_SEL_PLL_MASK = 15,
76 PD_BUS_SEL_CPLL = 0,
77 PD_BUS_SEL_GPLL,
78
79 /* pd bus pclk div: pclk = pd_bus_aclk /(div + 1) */
80 PD_BUS_PCLK_DIV_SHIFT = 12,
Simon Glassb223c1a2017-05-31 17:57:31 -060081 PD_BUS_PCLK_DIV_MASK = 7 << PD_BUS_PCLK_DIV_SHIFT,
Simon Glass99c15652015-08-30 16:55:31 -060082
83 /* pd bus hclk div: aclk_bus: hclk_bus = 1:1 or 2:1 or 4:1 */
84 PD_BUS_HCLK_DIV_SHIFT = 8,
Simon Glassb223c1a2017-05-31 17:57:31 -060085 PD_BUS_HCLK_DIV_MASK = 3 << PD_BUS_HCLK_DIV_SHIFT,
Simon Glass99c15652015-08-30 16:55:31 -060086
87 /* pd bus aclk div: pd_bus_aclk = pd_bus_src_clk /(div0 * div1) */
88 PD_BUS_ACLK_DIV0_SHIFT = 3,
Simon Glassb223c1a2017-05-31 17:57:31 -060089 PD_BUS_ACLK_DIV0_MASK = 0x1f << PD_BUS_ACLK_DIV0_SHIFT,
Simon Glass99c15652015-08-30 16:55:31 -060090 PD_BUS_ACLK_DIV1_SHIFT = 0,
Simon Glassb223c1a2017-05-31 17:57:31 -060091 PD_BUS_ACLK_DIV1_MASK = 0x7 << PD_BUS_ACLK_DIV1_SHIFT,
Simon Glass99c15652015-08-30 16:55:31 -060092
93 /*
94 * CLKSEL10
95 * peripheral bus pclk div:
96 * aclk_bus: pclk_bus = 1:1 or 2:1 or 4:1 or 8:1
97 */
Simon Glassc87c1292016-01-21 19:45:15 -070098 PERI_SEL_PLL_SHIFT = 15,
Simon Glassb223c1a2017-05-31 17:57:31 -060099 PERI_SEL_PLL_MASK = 1 << PERI_SEL_PLL_SHIFT,
Simon Glassc87c1292016-01-21 19:45:15 -0700100 PERI_SEL_CPLL = 0,
101 PERI_SEL_GPLL,
102
Simon Glass99c15652015-08-30 16:55:31 -0600103 PERI_PCLK_DIV_SHIFT = 12,
Simon Glassb223c1a2017-05-31 17:57:31 -0600104 PERI_PCLK_DIV_MASK = 3 << PERI_PCLK_DIV_SHIFT,
Simon Glass99c15652015-08-30 16:55:31 -0600105
106 /* peripheral bus hclk div: aclk_bus: hclk_bus = 1:1 or 2:1 or 4:1 */
107 PERI_HCLK_DIV_SHIFT = 8,
Simon Glassb223c1a2017-05-31 17:57:31 -0600108 PERI_HCLK_DIV_MASK = 3 << PERI_HCLK_DIV_SHIFT,
Simon Glass99c15652015-08-30 16:55:31 -0600109
110 /*
111 * peripheral bus aclk div:
112 * aclk_periph = periph_clk_src / (peri_aclk_div_con + 1)
113 */
114 PERI_ACLK_DIV_SHIFT = 0,
Simon Glassb223c1a2017-05-31 17:57:31 -0600115 PERI_ACLK_DIV_MASK = 0x1f << PERI_ACLK_DIV_SHIFT,
Simon Glass99c15652015-08-30 16:55:31 -0600116
David Wuef4cf5a2017-09-20 14:28:19 +0800117 /*
118 * CLKSEL24
119 * saradc_div_con:
120 * clk_saradc=24MHz/(saradc_div_con+1)
121 */
122 CLK_SARADC_DIV_CON_SHIFT = 8,
123 CLK_SARADC_DIV_CON_MASK = GENMASK(15, 8),
124 CLK_SARADC_DIV_CON_WIDTH = 8,
125
Simon Glass99c15652015-08-30 16:55:31 -0600126 SOCSTS_DPLL_LOCK = 1 << 5,
127 SOCSTS_APLL_LOCK = 1 << 6,
128 SOCSTS_CPLL_LOCK = 1 << 7,
129 SOCSTS_GPLL_LOCK = 1 << 8,
130 SOCSTS_NPLL_LOCK = 1 << 9,
131};
132
Simon Glass99c15652015-08-30 16:55:31 -0600133#define DIV_TO_RATE(input_rate, div) ((input_rate) / ((div) + 1))
134
135#define PLL_DIVISORS(hz, _nr, _no) {\
136 .nr = _nr, .nf = (u32)((u64)hz * _nr * _no / OSC_HZ), .no = _no};\
137 _Static_assert(((u64)hz * _nr * _no / OSC_HZ) * OSC_HZ /\
138 (_nr * _no) == hz, #hz "Hz cannot be hit with PLL "\
139 "divisors on line " __stringify(__LINE__));
140
141/* Keep divisors as low as possible to reduce jitter and power usage */
142static const struct pll_div apll_init_cfg = PLL_DIVISORS(APLL_HZ, 1, 1);
143static const struct pll_div gpll_init_cfg = PLL_DIVISORS(GPLL_HZ, 2, 2);
144static const struct pll_div cpll_init_cfg = PLL_DIVISORS(CPLL_HZ, 1, 2);
145
Jagan Tekib52a1992020-01-09 14:22:17 +0530146static int rkclk_set_pll(struct rockchip_cru *cru, enum rk_clk_id clk_id,
Simon Glass99c15652015-08-30 16:55:31 -0600147 const struct pll_div *div)
148{
149 int pll_id = rk_pll_id(clk_id);
150 struct rk3288_pll *pll = &cru->pll[pll_id];
151 /* All PLLs have same VCO and output frequency range restrictions. */
152 uint vco_hz = OSC_HZ / 1000 * div->nf / div->nr * 1000;
153 uint output_hz = vco_hz / div->no;
154
Simon Glassc87c1292016-01-21 19:45:15 -0700155 debug("PLL at %x: nf=%d, nr=%d, no=%d, vco=%u Hz, output=%u Hz\n",
156 (uint)pll, div->nf, div->nr, div->no, vco_hz, output_hz);
Simon Glass99c15652015-08-30 16:55:31 -0600157 assert(vco_hz >= VCO_MIN_HZ && vco_hz <= VCO_MAX_HZ &&
158 output_hz >= OUTPUT_MIN_HZ && output_hz <= OUTPUT_MAX_HZ &&
159 (div->no == 1 || !(div->no % 2)));
160
Simon Glassc87c1292016-01-21 19:45:15 -0700161 /* enter reset */
Simon Glass99c15652015-08-30 16:55:31 -0600162 rk_setreg(&pll->con3, 1 << PLL_RESET_SHIFT);
163
Simon Glassb223c1a2017-05-31 17:57:31 -0600164 rk_clrsetreg(&pll->con0, CLKR_MASK | PLL_OD_MASK,
Simon Glass99c15652015-08-30 16:55:31 -0600165 ((div->nr - 1) << CLKR_SHIFT) | (div->no - 1));
166 rk_clrsetreg(&pll->con1, CLKF_MASK, div->nf - 1);
167 rk_clrsetreg(&pll->con2, PLL_BWADJ_MASK, (div->nf >> 1) - 1);
168
169 udelay(10);
170
Simon Glassc87c1292016-01-21 19:45:15 -0700171 /* return from reset */
Simon Glass99c15652015-08-30 16:55:31 -0600172 rk_clrreg(&pll->con3, 1 << PLL_RESET_SHIFT);
173
174 return 0;
175}
176
Jagan Tekib52a1992020-01-09 14:22:17 +0530177static int rkclk_configure_ddr(struct rockchip_cru *cru, struct rk3288_grf *grf,
Simon Glass99c15652015-08-30 16:55:31 -0600178 unsigned int hz)
179{
180 static const struct pll_div dpll_cfg[] = {
181 {.nf = 25, .nr = 2, .no = 1},
182 {.nf = 400, .nr = 9, .no = 2},
183 {.nf = 500, .nr = 9, .no = 2},
184 {.nf = 100, .nr = 3, .no = 1},
185 };
186 int cfg;
187
Simon Glass99c15652015-08-30 16:55:31 -0600188 switch (hz) {
189 case 300000000:
190 cfg = 0;
191 break;
192 case 533000000: /* actually 533.3P MHz */
193 cfg = 1;
194 break;
195 case 666000000: /* actually 666.6P MHz */
196 cfg = 2;
197 break;
198 case 800000000:
199 cfg = 3;
200 break;
201 default:
Simon Glassc87c1292016-01-21 19:45:15 -0700202 debug("Unsupported SDRAM frequency");
Simon Glass99c15652015-08-30 16:55:31 -0600203 return -EINVAL;
204 }
205
206 /* pll enter slow-mode */
Simon Glassb223c1a2017-05-31 17:57:31 -0600207 rk_clrsetreg(&cru->cru_mode_con, DPLL_MODE_MASK,
Simon Glass99c15652015-08-30 16:55:31 -0600208 DPLL_MODE_SLOW << DPLL_MODE_SHIFT);
209
210 rkclk_set_pll(cru, CLK_DDR, &dpll_cfg[cfg]);
211
212 /* wait for pll lock */
213 while (!(readl(&grf->soc_status[1]) & SOCSTS_DPLL_LOCK))
214 udelay(1);
215
216 /* PLL enter normal-mode */
Simon Glassb223c1a2017-05-31 17:57:31 -0600217 rk_clrsetreg(&cru->cru_mode_con, DPLL_MODE_MASK,
Simon Glass009741f2016-01-21 19:45:01 -0700218 DPLL_MODE_NORMAL << DPLL_MODE_SHIFT);
Simon Glass99c15652015-08-30 16:55:31 -0600219
220 return 0;
221}
222
Simon Glass830a6082016-01-21 19:45:02 -0700223#ifndef CONFIG_SPL_BUILD
224#define VCO_MAX_KHZ 2200000
225#define VCO_MIN_KHZ 440000
226#define FREF_MAX_KHZ 2200000
227#define FREF_MIN_KHZ 269
228
229static int pll_para_config(ulong freq_hz, struct pll_div *div, uint *ext_div)
230{
231 uint ref_khz = OSC_HZ / 1000, nr, nf = 0;
232 uint fref_khz;
233 uint diff_khz, best_diff_khz;
234 const uint max_nr = 1 << 6, max_nf = 1 << 12, max_no = 1 << 4;
235 uint vco_khz;
236 uint no = 1;
237 uint freq_khz = freq_hz / 1000;
238
239 if (!freq_hz) {
240 printf("%s: the frequency can not be 0 Hz\n", __func__);
241 return -EINVAL;
242 }
243
244 no = DIV_ROUND_UP(VCO_MIN_KHZ, freq_khz);
245 if (ext_div) {
246 *ext_div = DIV_ROUND_UP(no, max_no);
247 no = DIV_ROUND_UP(no, *ext_div);
248 }
249
250 /* only even divisors (and 1) are supported */
251 if (no > 1)
252 no = DIV_ROUND_UP(no, 2) * 2;
253
254 vco_khz = freq_khz * no;
255 if (ext_div)
256 vco_khz *= *ext_div;
257
258 if (vco_khz < VCO_MIN_KHZ || vco_khz > VCO_MAX_KHZ || no > max_no) {
259 printf("%s: Cannot find out a supported VCO for Frequency (%luHz).\n",
260 __func__, freq_hz);
261 return -1;
262 }
263
264 div->no = no;
265
266 best_diff_khz = vco_khz;
267 for (nr = 1; nr < max_nr && best_diff_khz; nr++) {
268 fref_khz = ref_khz / nr;
269 if (fref_khz < FREF_MIN_KHZ)
270 break;
271 if (fref_khz > FREF_MAX_KHZ)
272 continue;
273
274 nf = vco_khz / fref_khz;
275 if (nf >= max_nf)
276 continue;
277 diff_khz = vco_khz - nf * fref_khz;
278 if (nf + 1 < max_nf && diff_khz > fref_khz / 2) {
279 nf++;
280 diff_khz = fref_khz - diff_khz;
281 }
282
283 if (diff_khz >= best_diff_khz)
284 continue;
285
286 best_diff_khz = diff_khz;
287 div->nr = nr;
288 div->nf = nf;
289 }
290
291 if (best_diff_khz > 4 * 1000) {
292 printf("%s: Failed to match output frequency %lu, difference is %u Hz, exceed 4MHZ\n",
293 __func__, freq_hz, best_diff_khz * 1000);
294 return -EINVAL;
295 }
296
297 return 0;
298}
299
Jagan Tekib52a1992020-01-09 14:22:17 +0530300static int rockchip_mac_set_clk(struct rockchip_cru *cru, uint freq)
Sjoerd Simons0aefc0b2016-02-28 22:24:59 +0100301{
David Wu01c60ea2018-01-13 14:06:33 +0800302 ulong ret;
Sjoerd Simons0aefc0b2016-02-28 22:24:59 +0100303
David Wu01c60ea2018-01-13 14:06:33 +0800304 /*
305 * The gmac clock can be derived either from an external clock
306 * or can be generated from internally by a divider from SCLK_MAC.
307 */
308 if (readl(&cru->cru_clksel_con[21]) & RMII_EXTCLK_MASK) {
309 /* An external clock will always generate the right rate... */
310 ret = freq;
311 } else {
312 u32 con = readl(&cru->cru_clksel_con[21]);
313 ulong pll_rate;
314 u8 div;
315
316 if (((con >> EMAC_PLL_SHIFT) & EMAC_PLL_MASK) ==
317 EMAC_PLL_SELECT_GENERAL)
318 pll_rate = GPLL_HZ;
319 else if (((con >> EMAC_PLL_SHIFT) & EMAC_PLL_MASK) ==
320 EMAC_PLL_SELECT_CODEC)
321 pll_rate = CPLL_HZ;
322 else
323 pll_rate = NPLL_HZ;
324
325 div = DIV_ROUND_UP(pll_rate, freq) - 1;
326 if (div <= 0x1f)
327 rk_clrsetreg(&cru->cru_clksel_con[21], MAC_DIV_CON_MASK,
328 div << MAC_DIV_CON_SHIFT);
329 else
330 debug("Unsupported div for gmac:%d\n", div);
331
332 return DIV_TO_RATE(pll_rate, div);
333 }
334
335 return ret;
Sjoerd Simons0aefc0b2016-02-28 22:24:59 +0100336}
337
Jagan Tekib52a1992020-01-09 14:22:17 +0530338static int rockchip_vop_set_clk(struct rockchip_cru *cru, struct rk3288_grf *grf,
Simon Glass830a6082016-01-21 19:45:02 -0700339 int periph, unsigned int rate_hz)
340{
341 struct pll_div npll_config = {0};
342 u32 lcdc_div;
343 int ret;
344
345 ret = pll_para_config(rate_hz, &npll_config, &lcdc_div);
346 if (ret)
347 return ret;
348
Simon Glassb223c1a2017-05-31 17:57:31 -0600349 rk_clrsetreg(&cru->cru_mode_con, NPLL_MODE_MASK,
Simon Glass830a6082016-01-21 19:45:02 -0700350 NPLL_MODE_SLOW << NPLL_MODE_SHIFT);
351 rkclk_set_pll(cru, CLK_NEW, &npll_config);
352
353 /* waiting for pll lock */
354 while (1) {
355 if (readl(&grf->soc_status[1]) & SOCSTS_NPLL_LOCK)
356 break;
357 udelay(1);
358 }
359
Simon Glassb223c1a2017-05-31 17:57:31 -0600360 rk_clrsetreg(&cru->cru_mode_con, NPLL_MODE_MASK,
Simon Glass830a6082016-01-21 19:45:02 -0700361 NPLL_MODE_NORMAL << NPLL_MODE_SHIFT);
362
363 /* vop dclk source clk: npll,dclk_div: 1 */
364 switch (periph) {
365 case DCLK_VOP0:
366 rk_clrsetreg(&cru->cru_clksel_con[27], 0xff << 8 | 3 << 0,
367 (lcdc_div - 1) << 8 | 2 << 0);
368 break;
369 case DCLK_VOP1:
370 rk_clrsetreg(&cru->cru_clksel_con[29], 0xff << 8 | 3 << 6,
371 (lcdc_div - 1) << 8 | 2 << 6);
372 break;
373 }
374
375 return 0;
376}
Simon Glass3dbfe5a2018-12-27 20:15:20 -0700377
378static u32 rockchip_clk_gcd(u32 a, u32 b)
379{
380 while (b != 0) {
381 int r = b;
382
383 b = a % b;
384 a = r;
385 }
386 return a;
387}
388
Jagan Tekib52a1992020-01-09 14:22:17 +0530389static ulong rockchip_i2s_get_clk(struct rockchip_cru *cru, uint gclk_rate)
Simon Glass3dbfe5a2018-12-27 20:15:20 -0700390{
391 unsigned long long rate;
392 uint val;
393 int n, d;
394
395 val = readl(&cru->cru_clksel_con[8]);
396 n = (val & I2S0_FRAC_NUMER_MASK) >> I2S0_FRAC_NUMER_SHIFT;
397 d = (val & I2S0_FRAC_DENOM_MASK) >> I2S0_FRAC_DENOM_SHIFT;
398
399 rate = (unsigned long long)gclk_rate * n;
400 do_div(rate, d);
401
402 return (ulong)rate;
403}
404
Jagan Tekib52a1992020-01-09 14:22:17 +0530405static ulong rockchip_i2s_set_clk(struct rockchip_cru *cru, uint gclk_rate,
Simon Glass3dbfe5a2018-12-27 20:15:20 -0700406 uint freq)
407{
408 int n, d;
409 int v;
410
411 /* set frac divider */
412 v = rockchip_clk_gcd(gclk_rate, freq);
413 n = gclk_rate / v;
414 d = freq / v;
415 assert(freq == gclk_rate / n * d);
416 writel(d << I2S0_FRAC_NUMER_SHIFT | n << I2S0_FRAC_DENOM_SHIFT,
417 &cru->cru_clksel_con[8]);
418
419 return rockchip_i2s_get_clk(cru, gclk_rate);
420}
Simon Glassd3cb46a2017-05-31 17:57:32 -0600421#endif /* CONFIG_SPL_BUILD */
Simon Glass830a6082016-01-21 19:45:02 -0700422
Jagan Tekib52a1992020-01-09 14:22:17 +0530423static void rkclk_init(struct rockchip_cru *cru, struct rk3288_grf *grf)
Simon Glass99c15652015-08-30 16:55:31 -0600424{
425 u32 aclk_div;
426 u32 hclk_div;
427 u32 pclk_div;
428
429 /* pll enter slow-mode */
430 rk_clrsetreg(&cru->cru_mode_con,
Simon Glassb223c1a2017-05-31 17:57:31 -0600431 GPLL_MODE_MASK | CPLL_MODE_MASK,
Simon Glass99c15652015-08-30 16:55:31 -0600432 GPLL_MODE_SLOW << GPLL_MODE_SHIFT |
433 CPLL_MODE_SLOW << CPLL_MODE_SHIFT);
434
435 /* init pll */
436 rkclk_set_pll(cru, CLK_GENERAL, &gpll_init_cfg);
437 rkclk_set_pll(cru, CLK_CODEC, &cpll_init_cfg);
438
439 /* waiting for pll lock */
440 while ((readl(&grf->soc_status[1]) &
441 (SOCSTS_CPLL_LOCK | SOCSTS_GPLL_LOCK)) !=
442 (SOCSTS_CPLL_LOCK | SOCSTS_GPLL_LOCK))
443 udelay(1);
444
445 /*
446 * pd_bus clock pll source selection and
447 * set up dependent divisors for PCLK/HCLK and ACLK clocks.
448 */
449 aclk_div = GPLL_HZ / PD_BUS_ACLK_HZ - 1;
450 assert((aclk_div + 1) * PD_BUS_ACLK_HZ == GPLL_HZ && aclk_div < 0x1f);
451 hclk_div = PD_BUS_ACLK_HZ / PD_BUS_HCLK_HZ - 1;
452 assert((hclk_div + 1) * PD_BUS_HCLK_HZ ==
453 PD_BUS_ACLK_HZ && (hclk_div < 0x4) && (hclk_div != 0x2));
454
455 pclk_div = PD_BUS_ACLK_HZ / PD_BUS_PCLK_HZ - 1;
456 assert((pclk_div + 1) * PD_BUS_PCLK_HZ ==
457 PD_BUS_ACLK_HZ && pclk_div < 0x7);
458
459 rk_clrsetreg(&cru->cru_clksel_con[1],
Simon Glassb223c1a2017-05-31 17:57:31 -0600460 PD_BUS_PCLK_DIV_MASK | PD_BUS_HCLK_DIV_MASK |
461 PD_BUS_ACLK_DIV0_MASK | PD_BUS_ACLK_DIV1_MASK,
Simon Glass99c15652015-08-30 16:55:31 -0600462 pclk_div << PD_BUS_PCLK_DIV_SHIFT |
463 hclk_div << PD_BUS_HCLK_DIV_SHIFT |
464 aclk_div << PD_BUS_ACLK_DIV0_SHIFT |
465 0 << 0);
466
467 /*
468 * peri clock pll source selection and
469 * set up dependent divisors for PCLK/HCLK and ACLK clocks.
470 */
471 aclk_div = GPLL_HZ / PERI_ACLK_HZ - 1;
472 assert((aclk_div + 1) * PERI_ACLK_HZ == GPLL_HZ && aclk_div < 0x1f);
473
Heiko Stübnerabd01282016-07-22 23:51:06 +0200474 hclk_div = ilog2(PERI_ACLK_HZ / PERI_HCLK_HZ);
Simon Glass99c15652015-08-30 16:55:31 -0600475 assert((1 << hclk_div) * PERI_HCLK_HZ ==
476 PERI_ACLK_HZ && (hclk_div < 0x4));
477
Heiko Stübnerabd01282016-07-22 23:51:06 +0200478 pclk_div = ilog2(PERI_ACLK_HZ / PERI_PCLK_HZ);
Simon Glass99c15652015-08-30 16:55:31 -0600479 assert((1 << pclk_div) * PERI_PCLK_HZ ==
480 PERI_ACLK_HZ && (pclk_div < 0x4));
481
482 rk_clrsetreg(&cru->cru_clksel_con[10],
Simon Glassb223c1a2017-05-31 17:57:31 -0600483 PERI_PCLK_DIV_MASK | PERI_HCLK_DIV_MASK |
484 PERI_ACLK_DIV_MASK,
Simon Glassc87c1292016-01-21 19:45:15 -0700485 PERI_SEL_GPLL << PERI_SEL_PLL_SHIFT |
Simon Glass99c15652015-08-30 16:55:31 -0600486 pclk_div << PERI_PCLK_DIV_SHIFT |
487 hclk_div << PERI_HCLK_DIV_SHIFT |
488 aclk_div << PERI_ACLK_DIV_SHIFT);
489
490 /* PLL enter normal-mode */
491 rk_clrsetreg(&cru->cru_mode_con,
Simon Glassb223c1a2017-05-31 17:57:31 -0600492 GPLL_MODE_MASK | CPLL_MODE_MASK,
Simon Glass009741f2016-01-21 19:45:01 -0700493 GPLL_MODE_NORMAL << GPLL_MODE_SHIFT |
494 CPLL_MODE_NORMAL << CPLL_MODE_SHIFT);
Simon Glass99c15652015-08-30 16:55:31 -0600495}
Simon Glass99c15652015-08-30 16:55:31 -0600496
Jagan Tekib52a1992020-01-09 14:22:17 +0530497void rk3288_clk_configure_cpu(struct rockchip_cru *cru, struct rk3288_grf *grf)
Simon Glassdae594f2016-01-21 19:45:17 -0700498{
499 /* pll enter slow-mode */
Simon Glassb223c1a2017-05-31 17:57:31 -0600500 rk_clrsetreg(&cru->cru_mode_con, APLL_MODE_MASK,
Simon Glassdae594f2016-01-21 19:45:17 -0700501 APLL_MODE_SLOW << APLL_MODE_SHIFT);
502
503 rkclk_set_pll(cru, CLK_ARM, &apll_init_cfg);
504
505 /* waiting for pll lock */
506 while (!(readl(&grf->soc_status[1]) & SOCSTS_APLL_LOCK))
507 udelay(1);
508
509 /*
510 * core clock pll source selection and
511 * set up dependent divisors for MPAXI/M0AXI and ARM clocks.
512 * core clock select apll, apll clk = 1800MHz
513 * arm clk = 1800MHz, mpclk = 450MHz, m0clk = 900MHz
514 */
515 rk_clrsetreg(&cru->cru_clksel_con[0],
Simon Glassb223c1a2017-05-31 17:57:31 -0600516 CORE_SEL_PLL_MASK | A17_DIV_MASK | MP_DIV_MASK |
517 M0_DIV_MASK,
Simon Glassdae594f2016-01-21 19:45:17 -0700518 0 << A17_DIV_SHIFT |
519 3 << MP_DIV_SHIFT |
520 1 << M0_DIV_SHIFT);
521
522 /*
523 * set up dependent divisors for L2RAM/ATCLK and PCLK clocks.
524 * l2ramclk = 900MHz, atclk = 450MHz, pclk_dbg = 450MHz
525 */
526 rk_clrsetreg(&cru->cru_clksel_con[37],
Simon Glassb223c1a2017-05-31 17:57:31 -0600527 CLK_L2RAM_DIV_MASK | ATCLK_CORE_DIV_CON_MASK |
528 PCLK_CORE_DBG_DIV_MASK,
Simon Glassdae594f2016-01-21 19:45:17 -0700529 1 << CLK_L2RAM_DIV_SHIFT |
530 3 << ATCLK_CORE_DIV_CON_SHIFT |
531 3 << PCLK_CORE_DBG_DIV_SHIFT);
532
533 /* PLL enter normal-mode */
Simon Glassb223c1a2017-05-31 17:57:31 -0600534 rk_clrsetreg(&cru->cru_mode_con, APLL_MODE_MASK,
Simon Glassdae594f2016-01-21 19:45:17 -0700535 APLL_MODE_NORMAL << APLL_MODE_SHIFT);
536}
537
Simon Glass99c15652015-08-30 16:55:31 -0600538/* Get pll rate by id */
Jagan Tekib52a1992020-01-09 14:22:17 +0530539static uint32_t rkclk_pll_get_rate(struct rockchip_cru *cru,
Simon Glass99c15652015-08-30 16:55:31 -0600540 enum rk_clk_id clk_id)
541{
542 uint32_t nr, no, nf;
543 uint32_t con;
544 int pll_id = rk_pll_id(clk_id);
545 struct rk3288_pll *pll = &cru->pll[pll_id];
546 static u8 clk_shift[CLK_COUNT] = {
Simon Glass009741f2016-01-21 19:45:01 -0700547 0xff, APLL_MODE_SHIFT, DPLL_MODE_SHIFT, CPLL_MODE_SHIFT,
548 GPLL_MODE_SHIFT, NPLL_MODE_SHIFT
Simon Glass99c15652015-08-30 16:55:31 -0600549 };
550 uint shift;
551
552 con = readl(&cru->cru_mode_con);
553 shift = clk_shift[clk_id];
Simon Glassb223c1a2017-05-31 17:57:31 -0600554 switch ((con >> shift) & CRU_MODE_MASK) {
Simon Glass009741f2016-01-21 19:45:01 -0700555 case APLL_MODE_SLOW:
Simon Glass99c15652015-08-30 16:55:31 -0600556 return OSC_HZ;
Simon Glass009741f2016-01-21 19:45:01 -0700557 case APLL_MODE_NORMAL:
Simon Glass99c15652015-08-30 16:55:31 -0600558 /* normal mode */
559 con = readl(&pll->con0);
Simon Glassb223c1a2017-05-31 17:57:31 -0600560 no = ((con & CLKOD_MASK) >> CLKOD_SHIFT) + 1;
561 nr = ((con & CLKR_MASK) >> CLKR_SHIFT) + 1;
Simon Glass99c15652015-08-30 16:55:31 -0600562 con = readl(&pll->con1);
Simon Glassb223c1a2017-05-31 17:57:31 -0600563 nf = ((con & CLKF_MASK) >> CLKF_SHIFT) + 1;
Simon Glass99c15652015-08-30 16:55:31 -0600564
565 return (24 * nf / (nr * no)) * 1000000;
Simon Glass009741f2016-01-21 19:45:01 -0700566 case APLL_MODE_DEEP:
Simon Glass99c15652015-08-30 16:55:31 -0600567 default:
568 return 32768;
569 }
570}
571
Jagan Tekib52a1992020-01-09 14:22:17 +0530572static ulong rockchip_mmc_get_clk(struct rockchip_cru *cru, uint gclk_rate,
Simon Glass898d6432016-01-21 19:43:38 -0700573 int periph)
Simon Glass99c15652015-08-30 16:55:31 -0600574{
575 uint src_rate;
576 uint div, mux;
577 u32 con;
578
579 switch (periph) {
Simon Glass898d6432016-01-21 19:43:38 -0700580 case HCLK_EMMC:
Xu Ziyuan45112272017-04-16 17:44:45 +0800581 case SCLK_EMMC:
Simon Glass99c15652015-08-30 16:55:31 -0600582 con = readl(&cru->cru_clksel_con[12]);
Simon Glassb223c1a2017-05-31 17:57:31 -0600583 mux = (con & EMMC_PLL_MASK) >> EMMC_PLL_SHIFT;
584 div = (con & EMMC_DIV_MASK) >> EMMC_DIV_SHIFT;
Simon Glass99c15652015-08-30 16:55:31 -0600585 break;
Simon Glass898d6432016-01-21 19:43:38 -0700586 case HCLK_SDMMC:
Xu Ziyuan45112272017-04-16 17:44:45 +0800587 case SCLK_SDMMC:
Simon Glass898d6432016-01-21 19:43:38 -0700588 con = readl(&cru->cru_clksel_con[11]);
Simon Glassb223c1a2017-05-31 17:57:31 -0600589 mux = (con & MMC0_PLL_MASK) >> MMC0_PLL_SHIFT;
590 div = (con & MMC0_DIV_MASK) >> MMC0_DIV_SHIFT;
Simon Glass99c15652015-08-30 16:55:31 -0600591 break;
Simon Glass898d6432016-01-21 19:43:38 -0700592 case HCLK_SDIO0:
Xu Ziyuan45112272017-04-16 17:44:45 +0800593 case SCLK_SDIO0:
Simon Glass99c15652015-08-30 16:55:31 -0600594 con = readl(&cru->cru_clksel_con[12]);
Simon Glassb223c1a2017-05-31 17:57:31 -0600595 mux = (con & SDIO0_PLL_MASK) >> SDIO0_PLL_SHIFT;
596 div = (con & SDIO0_DIV_MASK) >> SDIO0_DIV_SHIFT;
Simon Glass99c15652015-08-30 16:55:31 -0600597 break;
598 default:
599 return -EINVAL;
600 }
601
Simon Glass542635a2016-01-21 19:43:39 -0700602 src_rate = mux == EMMC_PLL_SELECT_24MHZ ? OSC_HZ : gclk_rate;
Simon Glass99c15652015-08-30 16:55:31 -0600603 return DIV_TO_RATE(src_rate, div);
604}
605
Jagan Tekib52a1992020-01-09 14:22:17 +0530606static ulong rockchip_mmc_set_clk(struct rockchip_cru *cru, uint gclk_rate,
Simon Glass898d6432016-01-21 19:43:38 -0700607 int periph, uint freq)
Simon Glass99c15652015-08-30 16:55:31 -0600608{
609 int src_clk_div;
610 int mux;
611
Simon Glass542635a2016-01-21 19:43:39 -0700612 debug("%s: gclk_rate=%u\n", __func__, gclk_rate);
Kever Yang3a94d752017-07-27 12:54:01 +0800613 /* mmc clock default div 2 internal, need provide double in cru */
614 src_clk_div = DIV_ROUND_UP(gclk_rate / 2, freq);
Simon Glass99c15652015-08-30 16:55:31 -0600615
616 if (src_clk_div > 0x3f) {
Kever Yang3a94d752017-07-27 12:54:01 +0800617 src_clk_div = DIV_ROUND_UP(OSC_HZ / 2, freq);
Kever Yang217273c2017-07-27 12:54:02 +0800618 assert(src_clk_div < 0x40);
Simon Glass99c15652015-08-30 16:55:31 -0600619 mux = EMMC_PLL_SELECT_24MHZ;
620 assert((int)EMMC_PLL_SELECT_24MHZ ==
621 (int)MMC0_PLL_SELECT_24MHZ);
622 } else {
623 mux = EMMC_PLL_SELECT_GENERAL;
624 assert((int)EMMC_PLL_SELECT_GENERAL ==
625 (int)MMC0_PLL_SELECT_GENERAL);
626 }
627 switch (periph) {
Simon Glass898d6432016-01-21 19:43:38 -0700628 case HCLK_EMMC:
Xu Ziyuan45112272017-04-16 17:44:45 +0800629 case SCLK_EMMC:
Simon Glass99c15652015-08-30 16:55:31 -0600630 rk_clrsetreg(&cru->cru_clksel_con[12],
Simon Glassb223c1a2017-05-31 17:57:31 -0600631 EMMC_PLL_MASK | EMMC_DIV_MASK,
Simon Glass99c15652015-08-30 16:55:31 -0600632 mux << EMMC_PLL_SHIFT |
633 (src_clk_div - 1) << EMMC_DIV_SHIFT);
634 break;
Simon Glass898d6432016-01-21 19:43:38 -0700635 case HCLK_SDMMC:
Xu Ziyuan45112272017-04-16 17:44:45 +0800636 case SCLK_SDMMC:
Simon Glass99c15652015-08-30 16:55:31 -0600637 rk_clrsetreg(&cru->cru_clksel_con[11],
Simon Glassb223c1a2017-05-31 17:57:31 -0600638 MMC0_PLL_MASK | MMC0_DIV_MASK,
Simon Glass99c15652015-08-30 16:55:31 -0600639 mux << MMC0_PLL_SHIFT |
640 (src_clk_div - 1) << MMC0_DIV_SHIFT);
641 break;
Simon Glass898d6432016-01-21 19:43:38 -0700642 case HCLK_SDIO0:
Xu Ziyuan45112272017-04-16 17:44:45 +0800643 case SCLK_SDIO0:
Simon Glass99c15652015-08-30 16:55:31 -0600644 rk_clrsetreg(&cru->cru_clksel_con[12],
Simon Glassb223c1a2017-05-31 17:57:31 -0600645 SDIO0_PLL_MASK | SDIO0_DIV_MASK,
Simon Glass99c15652015-08-30 16:55:31 -0600646 mux << SDIO0_PLL_SHIFT |
647 (src_clk_div - 1) << SDIO0_DIV_SHIFT);
648 break;
649 default:
650 return -EINVAL;
651 }
652
Simon Glass542635a2016-01-21 19:43:39 -0700653 return rockchip_mmc_get_clk(cru, gclk_rate, periph);
Simon Glass99c15652015-08-30 16:55:31 -0600654}
655
Jagan Tekib52a1992020-01-09 14:22:17 +0530656static ulong rockchip_spi_get_clk(struct rockchip_cru *cru, uint gclk_rate,
Simon Glass898d6432016-01-21 19:43:38 -0700657 int periph)
Simon Glass99c15652015-08-30 16:55:31 -0600658{
659 uint div, mux;
660 u32 con;
661
662 switch (periph) {
Simon Glass898d6432016-01-21 19:43:38 -0700663 case SCLK_SPI0:
Simon Glass99c15652015-08-30 16:55:31 -0600664 con = readl(&cru->cru_clksel_con[25]);
Simon Glassb223c1a2017-05-31 17:57:31 -0600665 mux = (con & SPI0_PLL_MASK) >> SPI0_PLL_SHIFT;
666 div = (con & SPI0_DIV_MASK) >> SPI0_DIV_SHIFT;
Simon Glass99c15652015-08-30 16:55:31 -0600667 break;
Simon Glass898d6432016-01-21 19:43:38 -0700668 case SCLK_SPI1:
Simon Glass99c15652015-08-30 16:55:31 -0600669 con = readl(&cru->cru_clksel_con[25]);
Simon Glassb223c1a2017-05-31 17:57:31 -0600670 mux = (con & SPI1_PLL_MASK) >> SPI1_PLL_SHIFT;
671 div = (con & SPI1_DIV_MASK) >> SPI1_DIV_SHIFT;
Simon Glass99c15652015-08-30 16:55:31 -0600672 break;
Simon Glass898d6432016-01-21 19:43:38 -0700673 case SCLK_SPI2:
Simon Glass99c15652015-08-30 16:55:31 -0600674 con = readl(&cru->cru_clksel_con[39]);
Simon Glassb223c1a2017-05-31 17:57:31 -0600675 mux = (con & SPI2_PLL_MASK) >> SPI2_PLL_SHIFT;
676 div = (con & SPI2_DIV_MASK) >> SPI2_DIV_SHIFT;
Simon Glass99c15652015-08-30 16:55:31 -0600677 break;
678 default:
679 return -EINVAL;
680 }
681 assert(mux == SPI0_PLL_SELECT_GENERAL);
682
Simon Glass542635a2016-01-21 19:43:39 -0700683 return DIV_TO_RATE(gclk_rate, div);
Simon Glass99c15652015-08-30 16:55:31 -0600684}
685
Jagan Tekib52a1992020-01-09 14:22:17 +0530686static ulong rockchip_spi_set_clk(struct rockchip_cru *cru, uint gclk_rate,
Simon Glass898d6432016-01-21 19:43:38 -0700687 int periph, uint freq)
Simon Glass99c15652015-08-30 16:55:31 -0600688{
689 int src_clk_div;
690
Simon Glass542635a2016-01-21 19:43:39 -0700691 debug("%s: clk_general_rate=%u\n", __func__, gclk_rate);
Kever Yang217273c2017-07-27 12:54:02 +0800692 src_clk_div = DIV_ROUND_UP(gclk_rate, freq) - 1;
693 assert(src_clk_div < 128);
Simon Glass99c15652015-08-30 16:55:31 -0600694 switch (periph) {
Simon Glass898d6432016-01-21 19:43:38 -0700695 case SCLK_SPI0:
Simon Glass99c15652015-08-30 16:55:31 -0600696 rk_clrsetreg(&cru->cru_clksel_con[25],
Simon Glassb223c1a2017-05-31 17:57:31 -0600697 SPI0_PLL_MASK | SPI0_DIV_MASK,
Simon Glass99c15652015-08-30 16:55:31 -0600698 SPI0_PLL_SELECT_GENERAL << SPI0_PLL_SHIFT |
699 src_clk_div << SPI0_DIV_SHIFT);
700 break;
Simon Glass898d6432016-01-21 19:43:38 -0700701 case SCLK_SPI1:
Simon Glass99c15652015-08-30 16:55:31 -0600702 rk_clrsetreg(&cru->cru_clksel_con[25],
Simon Glassb223c1a2017-05-31 17:57:31 -0600703 SPI1_PLL_MASK | SPI1_DIV_MASK,
Simon Glass99c15652015-08-30 16:55:31 -0600704 SPI1_PLL_SELECT_GENERAL << SPI1_PLL_SHIFT |
705 src_clk_div << SPI1_DIV_SHIFT);
706 break;
Simon Glass898d6432016-01-21 19:43:38 -0700707 case SCLK_SPI2:
Simon Glass99c15652015-08-30 16:55:31 -0600708 rk_clrsetreg(&cru->cru_clksel_con[39],
Simon Glassb223c1a2017-05-31 17:57:31 -0600709 SPI2_PLL_MASK | SPI2_DIV_MASK,
Simon Glass99c15652015-08-30 16:55:31 -0600710 SPI2_PLL_SELECT_GENERAL << SPI2_PLL_SHIFT |
711 src_clk_div << SPI2_DIV_SHIFT);
712 break;
713 default:
714 return -EINVAL;
715 }
716
Simon Glass542635a2016-01-21 19:43:39 -0700717 return rockchip_spi_get_clk(cru, gclk_rate, periph);
Simon Glass99c15652015-08-30 16:55:31 -0600718}
719
Jagan Tekib52a1992020-01-09 14:22:17 +0530720static ulong rockchip_saradc_get_clk(struct rockchip_cru *cru)
David Wuef4cf5a2017-09-20 14:28:19 +0800721{
722 u32 div, val;
723
724 val = readl(&cru->cru_clksel_con[24]);
725 div = bitfield_extract(val, CLK_SARADC_DIV_CON_SHIFT,
726 CLK_SARADC_DIV_CON_WIDTH);
727
728 return DIV_TO_RATE(OSC_HZ, div);
729}
730
Jagan Tekib52a1992020-01-09 14:22:17 +0530731static ulong rockchip_saradc_set_clk(struct rockchip_cru *cru, uint hz)
David Wuef4cf5a2017-09-20 14:28:19 +0800732{
733 int src_clk_div;
734
735 src_clk_div = DIV_ROUND_UP(OSC_HZ, hz) - 1;
736 assert(src_clk_div < 128);
737
738 rk_clrsetreg(&cru->cru_clksel_con[24],
739 CLK_SARADC_DIV_CON_MASK,
740 src_clk_div << CLK_SARADC_DIV_CON_SHIFT);
741
742 return rockchip_saradc_get_clk(cru);
743}
744
Stephen Warren135aa952016-06-17 09:44:00 -0600745static ulong rk3288_clk_get_rate(struct clk *clk)
Simon Glass4f436732016-01-21 19:43:40 -0700746{
Stephen Warren135aa952016-06-17 09:44:00 -0600747 struct rk3288_clk_priv *priv = dev_get_priv(clk->dev);
Simon Glass4f436732016-01-21 19:43:40 -0700748 ulong new_rate, gclk_rate;
Simon Glass4f436732016-01-21 19:43:40 -0700749
Stephen Warren135aa952016-06-17 09:44:00 -0600750 gclk_rate = rkclk_pll_get_rate(priv->cru, CLK_GENERAL);
751 switch (clk->id) {
752 case 0 ... 63:
753 new_rate = rkclk_pll_get_rate(priv->cru, clk->id);
754 break;
Simon Glass4f436732016-01-21 19:43:40 -0700755 case HCLK_EMMC:
Simon Glass342999f2016-01-21 19:43:45 -0700756 case HCLK_SDMMC:
Simon Glass4f436732016-01-21 19:43:40 -0700757 case HCLK_SDIO0:
Xu Ziyuan45112272017-04-16 17:44:45 +0800758 case SCLK_EMMC:
759 case SCLK_SDMMC:
760 case SCLK_SDIO0:
Stephen Warren135aa952016-06-17 09:44:00 -0600761 new_rate = rockchip_mmc_get_clk(priv->cru, gclk_rate, clk->id);
Simon Glass4f436732016-01-21 19:43:40 -0700762 break;
763 case SCLK_SPI0:
764 case SCLK_SPI1:
765 case SCLK_SPI2:
Stephen Warren135aa952016-06-17 09:44:00 -0600766 new_rate = rockchip_spi_get_clk(priv->cru, gclk_rate, clk->id);
Simon Glass4f436732016-01-21 19:43:40 -0700767 break;
768 case PCLK_I2C0:
769 case PCLK_I2C1:
770 case PCLK_I2C2:
771 case PCLK_I2C3:
772 case PCLK_I2C4:
773 case PCLK_I2C5:
774 return gclk_rate;
Kever Yang4f0b8ef2016-08-12 17:57:05 +0800775 case PCLK_PWM:
776 return PD_BUS_PCLK_HZ;
David Wuef4cf5a2017-09-20 14:28:19 +0800777 case SCLK_SARADC:
778 new_rate = rockchip_saradc_get_clk(priv->cru);
779 break;
Simon Glass4f436732016-01-21 19:43:40 -0700780 default:
781 return -ENOENT;
782 }
783
784 return new_rate;
785}
786
Stephen Warren135aa952016-06-17 09:44:00 -0600787static ulong rk3288_clk_set_rate(struct clk *clk, ulong rate)
Simon Glass99c15652015-08-30 16:55:31 -0600788{
Stephen Warren135aa952016-06-17 09:44:00 -0600789 struct rk3288_clk_priv *priv = dev_get_priv(clk->dev);
Jagan Tekib52a1992020-01-09 14:22:17 +0530790 struct rockchip_cru *cru = priv->cru;
Simon Glass898d6432016-01-21 19:43:38 -0700791 ulong new_rate, gclk_rate;
Simon Glass99c15652015-08-30 16:55:31 -0600792
Stephen Warren135aa952016-06-17 09:44:00 -0600793 gclk_rate = rkclk_pll_get_rate(priv->cru, CLK_GENERAL);
794 switch (clk->id) {
Simon Glass3a8a42d2016-11-13 14:22:13 -0700795 case PLL_APLL:
796 /* We only support a fixed rate here */
797 if (rate != 1800000000)
798 return -EINVAL;
799 rk3288_clk_configure_cpu(priv->cru, priv->grf);
800 new_rate = rate;
801 break;
Stephen Warren135aa952016-06-17 09:44:00 -0600802 case CLK_DDR:
803 new_rate = rkclk_configure_ddr(priv->cru, priv->grf, rate);
804 break;
Simon Glass898d6432016-01-21 19:43:38 -0700805 case HCLK_EMMC:
806 case HCLK_SDMMC:
807 case HCLK_SDIO0:
Xu Ziyuan45112272017-04-16 17:44:45 +0800808 case SCLK_EMMC:
809 case SCLK_SDMMC:
810 case SCLK_SDIO0:
Stephen Warren135aa952016-06-17 09:44:00 -0600811 new_rate = rockchip_mmc_set_clk(cru, gclk_rate, clk->id, rate);
Simon Glass99c15652015-08-30 16:55:31 -0600812 break;
Simon Glass898d6432016-01-21 19:43:38 -0700813 case SCLK_SPI0:
814 case SCLK_SPI1:
815 case SCLK_SPI2:
Stephen Warren135aa952016-06-17 09:44:00 -0600816 new_rate = rockchip_spi_set_clk(cru, gclk_rate, clk->id, rate);
Simon Glass99c15652015-08-30 16:55:31 -0600817 break;
Simon Glass830a6082016-01-21 19:45:02 -0700818#ifndef CONFIG_SPL_BUILD
Simon Glass3dbfe5a2018-12-27 20:15:20 -0700819 case SCLK_I2S0:
820 new_rate = rockchip_i2s_set_clk(cru, gclk_rate, rate);
821 break;
Sjoerd Simons0aefc0b2016-02-28 22:24:59 +0100822 case SCLK_MAC:
David Wu01c60ea2018-01-13 14:06:33 +0800823 new_rate = rockchip_mac_set_clk(priv->cru, rate);
Sjoerd Simons0aefc0b2016-02-28 22:24:59 +0100824 break;
Simon Glass830a6082016-01-21 19:45:02 -0700825 case DCLK_VOP0:
826 case DCLK_VOP1:
Stephen Warren135aa952016-06-17 09:44:00 -0600827 new_rate = rockchip_vop_set_clk(cru, priv->grf, clk->id, rate);
Simon Glass830a6082016-01-21 19:45:02 -0700828 break;
829 case SCLK_EDP_24M:
830 /* clk_edp_24M source: 24M */
831 rk_setreg(&cru->cru_clksel_con[28], 1 << 15);
832
833 /* rst edp */
834 rk_setreg(&cru->cru_clksel_con[6], 1 << 15);
835 udelay(1);
836 rk_clrreg(&cru->cru_clksel_con[6], 1 << 15);
837 new_rate = rate;
838 break;
839 case ACLK_VOP0:
840 case ACLK_VOP1: {
841 u32 div;
842
843 /* vop aclk source clk: cpll */
844 div = CPLL_HZ / rate;
845 assert((div - 1 < 64) && (div * rate == CPLL_HZ));
846
Stephen Warren135aa952016-06-17 09:44:00 -0600847 switch (clk->id) {
Simon Glass830a6082016-01-21 19:45:02 -0700848 case ACLK_VOP0:
849 rk_clrsetreg(&cru->cru_clksel_con[31],
850 3 << 6 | 0x1f << 0,
851 0 << 6 | (div - 1) << 0);
852 break;
853 case ACLK_VOP1:
854 rk_clrsetreg(&cru->cru_clksel_con[31],
855 3 << 14 | 0x1f << 8,
856 0 << 14 | (div - 1) << 8);
857 break;
858 }
859 new_rate = rate;
860 break;
861 }
862 case PCLK_HDMI_CTRL:
863 /* enable pclk hdmi ctrl */
864 rk_clrreg(&cru->cru_clkgate_con[16], 1 << 9);
865
866 /* software reset hdmi */
867 rk_setreg(&cru->cru_clkgate_con[7], 1 << 9);
868 udelay(1);
869 rk_clrreg(&cru->cru_clkgate_con[7], 1 << 9);
870 new_rate = rate;
871 break;
872#endif
David Wuef4cf5a2017-09-20 14:28:19 +0800873 case SCLK_SARADC:
874 new_rate = rockchip_saradc_set_clk(priv->cru, rate);
875 break;
David Wu01c60ea2018-01-13 14:06:33 +0800876 case PLL_GPLL:
877 case PLL_CPLL:
878 case PLL_NPLL:
879 case ACLK_CPU:
880 case HCLK_CPU:
881 case PCLK_CPU:
882 case ACLK_PERI:
883 case HCLK_PERI:
884 case PCLK_PERI:
885 case SCLK_UART0:
886 return 0;
Simon Glass99c15652015-08-30 16:55:31 -0600887 default:
888 return -ENOENT;
889 }
890
891 return new_rate;
892}
893
Philipp Tomsich75b381a2018-01-25 15:27:10 +0100894static int __maybe_unused rk3288_gmac_set_parent(struct clk *clk, struct clk *parent)
David Wu01c60ea2018-01-13 14:06:33 +0800895{
896 struct rk3288_clk_priv *priv = dev_get_priv(clk->dev);
Jagan Tekib52a1992020-01-09 14:22:17 +0530897 struct rockchip_cru *cru = priv->cru;
David Wu01c60ea2018-01-13 14:06:33 +0800898 const char *clock_output_name;
899 int ret;
900
901 /*
902 * If the requested parent is in the same clock-controller and
903 * the id is SCLK_MAC_PLL ("mac_pll_src"), switch to the internal
904 * clock.
905 */
906 if ((parent->dev == clk->dev) && (parent->id == SCLK_MAC_PLL)) {
907 debug("%s: switching GAMC to SCLK_MAC_PLL\n", __func__);
908 rk_clrsetreg(&cru->cru_clksel_con[21], RMII_EXTCLK_MASK, 0);
909 return 0;
910 }
911
912 /*
913 * Otherwise, we need to check the clock-output-names of the
914 * requested parent to see if the requested id is "ext_gmac".
915 */
916 ret = dev_read_string_index(parent->dev, "clock-output-names",
917 parent->id, &clock_output_name);
918 if (ret < 0)
919 return -ENODATA;
920
921 /* If this is "ext_gmac", switch to the external clock input */
922 if (!strcmp(clock_output_name, "ext_gmac")) {
923 debug("%s: switching GMAC to external clock\n", __func__);
924 rk_clrsetreg(&cru->cru_clksel_con[21], RMII_EXTCLK_MASK,
925 RMII_EXTCLK_SELECT_EXT_CLK << RMII_EXTCLK_SHIFT);
926 return 0;
927 }
928
929 return -EINVAL;
930}
931
Philipp Tomsich75b381a2018-01-25 15:27:10 +0100932static int __maybe_unused rk3288_clk_set_parent(struct clk *clk, struct clk *parent)
David Wu01c60ea2018-01-13 14:06:33 +0800933{
934 switch (clk->id) {
935 case SCLK_MAC:
936 return rk3288_gmac_set_parent(clk, parent);
937 case SCLK_USBPHY480M_SRC:
938 return 0;
939 }
940
941 debug("%s: unsupported clk %ld\n", __func__, clk->id);
942 return -ENOENT;
943}
944
Simon Glass99c15652015-08-30 16:55:31 -0600945static struct clk_ops rk3288_clk_ops = {
946 .get_rate = rk3288_clk_get_rate,
947 .set_rate = rk3288_clk_set_rate,
Philipp Tomsich75b381a2018-01-25 15:27:10 +0100948#if CONFIG_IS_ENABLED(OF_CONTROL) && !CONFIG_IS_ENABLED(OF_PLATDATA)
David Wu01c60ea2018-01-13 14:06:33 +0800949 .set_parent = rk3288_clk_set_parent,
Philipp Tomsich75b381a2018-01-25 15:27:10 +0100950#endif
Simon Glass99c15652015-08-30 16:55:31 -0600951};
952
Simon Glass08fd82c2016-07-04 11:58:28 -0600953static int rk3288_clk_ofdata_to_platdata(struct udevice *dev)
Simon Glass99c15652015-08-30 16:55:31 -0600954{
Simon Glass2d143bd2016-07-04 11:58:29 -0600955#if !CONFIG_IS_ENABLED(OF_PLATDATA)
Simon Glass99c15652015-08-30 16:55:31 -0600956 struct rk3288_clk_priv *priv = dev_get_priv(dev);
957
Kever Yang995cde12018-02-11 11:53:08 +0800958 priv->cru = dev_read_addr_ptr(dev);
Simon Glass2d143bd2016-07-04 11:58:29 -0600959#endif
Simon Glass08fd82c2016-07-04 11:58:28 -0600960
961 return 0;
962}
963
964static int rk3288_clk_probe(struct udevice *dev)
965{
966 struct rk3288_clk_priv *priv = dev_get_priv(dev);
Simon Glassd3cb46a2017-05-31 17:57:32 -0600967 bool init_clocks = false;
Simon Glass08fd82c2016-07-04 11:58:28 -0600968
Simon Glass99c15652015-08-30 16:55:31 -0600969 priv->grf = syscon_get_first_range(ROCKCHIP_SYSCON_GRF);
Simon Glass08fd82c2016-07-04 11:58:28 -0600970 if (IS_ERR(priv->grf))
971 return PTR_ERR(priv->grf);
Simon Glass99c15652015-08-30 16:55:31 -0600972#ifdef CONFIG_SPL_BUILD
Simon Glass2d143bd2016-07-04 11:58:29 -0600973#if CONFIG_IS_ENABLED(OF_PLATDATA)
974 struct rk3288_clk_plat *plat = dev_get_platdata(dev);
975
976 priv->cru = map_sysmem(plat->dtd.reg[0], plat->dtd.reg[1]);
977#endif
Simon Glassd3cb46a2017-05-31 17:57:32 -0600978 init_clocks = true;
Simon Glass99c15652015-08-30 16:55:31 -0600979#endif
Simon Glassd3cb46a2017-05-31 17:57:32 -0600980 if (!(gd->flags & GD_FLG_RELOC)) {
981 u32 reg;
982
983 /*
984 * Init clocks in U-Boot proper if the NPLL is runnning. This
985 * indicates that a previous boot loader set up the clocks, so
986 * we need to redo it. U-Boot's SPL does not set this clock.
987 */
988 reg = readl(&priv->cru->cru_mode_con);
989 if (((reg & NPLL_MODE_MASK) >> NPLL_MODE_SHIFT) ==
990 NPLL_MODE_NORMAL)
991 init_clocks = true;
992 }
993
994 if (init_clocks)
995 rkclk_init(priv->cru, priv->grf);
Simon Glass99c15652015-08-30 16:55:31 -0600996
997 return 0;
998}
999
Simon Glass99c15652015-08-30 16:55:31 -06001000static int rk3288_clk_bind(struct udevice *dev)
1001{
Stephen Warren135aa952016-06-17 09:44:00 -06001002 int ret;
Kever Yangf24e36d2017-11-03 15:16:13 +08001003 struct udevice *sys_child;
1004 struct sysreset_reg *priv;
Simon Glass99c15652015-08-30 16:55:31 -06001005
1006 /* The reset driver does not have a device node, so bind it here */
Kever Yangf24e36d2017-11-03 15:16:13 +08001007 ret = device_bind_driver(dev, "rockchip_sysreset", "sysreset",
1008 &sys_child);
1009 if (ret) {
1010 debug("Warning: No sysreset driver: ret=%d\n", ret);
1011 } else {
1012 priv = malloc(sizeof(struct sysreset_reg));
Jagan Tekib52a1992020-01-09 14:22:17 +05301013 priv->glb_srst_fst_value = offsetof(struct rockchip_cru,
Kever Yangf24e36d2017-11-03 15:16:13 +08001014 cru_glb_srst_fst_value);
Jagan Tekib52a1992020-01-09 14:22:17 +05301015 priv->glb_srst_snd_value = offsetof(struct rockchip_cru,
Kever Yangf24e36d2017-11-03 15:16:13 +08001016 cru_glb_srst_snd_value);
1017 sys_child->priv = priv;
1018 }
Simon Glass99c15652015-08-30 16:55:31 -06001019
Heiko Stuebnera5ada252019-11-09 00:06:30 +01001020#if CONFIG_IS_ENABLED(RESET_ROCKCHIP)
Jagan Tekib52a1992020-01-09 14:22:17 +05301021 ret = offsetof(struct rockchip_cru, cru_softrst_con[0]);
Elaine Zhang538f67c2017-12-19 18:22:38 +08001022 ret = rockchip_reset_bind(dev, ret, 12);
1023 if (ret)
1024 debug("Warning: software reset driver bind faile\n");
1025#endif
1026
Simon Glass99c15652015-08-30 16:55:31 -06001027 return 0;
1028}
1029
1030static const struct udevice_id rk3288_clk_ids[] = {
1031 { .compatible = "rockchip,rk3288-cru" },
1032 { }
1033};
1034
Simon Glass2d143bd2016-07-04 11:58:29 -06001035U_BOOT_DRIVER(rockchip_rk3288_cru) = {
1036 .name = "rockchip_rk3288_cru",
Simon Glass99c15652015-08-30 16:55:31 -06001037 .id = UCLASS_CLK,
1038 .of_match = rk3288_clk_ids,
1039 .priv_auto_alloc_size = sizeof(struct rk3288_clk_priv),
Simon Glass2d143bd2016-07-04 11:58:29 -06001040 .platdata_auto_alloc_size = sizeof(struct rk3288_clk_plat),
Simon Glass99c15652015-08-30 16:55:31 -06001041 .ops = &rk3288_clk_ops,
1042 .bind = rk3288_clk_bind,
Simon Glass08fd82c2016-07-04 11:58:28 -06001043 .ofdata_to_platdata = rk3288_clk_ofdata_to_platdata,
Simon Glass99c15652015-08-30 16:55:31 -06001044 .probe = rk3288_clk_probe,
1045};