blob: 762c9d4f10ded9922a47deea18b611282edb6e72 [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 Glassf7ae49f2020-05-10 11:40:05 -060013#include <log.h>
Simon Glass336d4612020-02-03 07:36:16 -070014#include <malloc.h>
Simon Glass2d143bd2016-07-04 11:58:29 -060015#include <mapmem.h>
Simon Glass99c15652015-08-30 16:55:31 -060016#include <syscon.h>
17#include <asm/io.h>
Kever Yang15f09a12019-03-28 11:01:23 +080018#include <asm/arch-rockchip/clock.h>
Jagan Tekib52a1992020-01-09 14:22:17 +053019#include <asm/arch-rockchip/cru.h>
Kever Yang15f09a12019-03-28 11:01:23 +080020#include <asm/arch-rockchip/grf_rk3288.h>
21#include <asm/arch-rockchip/hardware.h>
Simon Glass898d6432016-01-21 19:43:38 -070022#include <dt-bindings/clock/rk3288-cru.h>
Simon Glass64b7faa2016-01-21 19:43:41 -070023#include <dm/device-internal.h>
Simon Glass99c15652015-08-30 16:55:31 -060024#include <dm/lists.h>
Simon Glass64b7faa2016-01-21 19:43:41 -070025#include <dm/uclass-internal.h>
Simon Glass61b29b82020-02-03 07:36:15 -070026#include <linux/err.h>
Heiko Stübnerabd01282016-07-22 23:51:06 +020027#include <linux/log2.h>
Simon Glass99c15652015-08-30 16:55:31 -060028
29DECLARE_GLOBAL_DATA_PTR;
30
Simon Glass2d143bd2016-07-04 11:58:29 -060031struct rk3288_clk_plat {
32#if CONFIG_IS_ENABLED(OF_PLATDATA)
33 struct dtd_rockchip_rk3288_cru dtd;
34#endif
35};
36
Simon Glass99c15652015-08-30 16:55:31 -060037struct pll_div {
38 u32 nr;
39 u32 nf;
40 u32 no;
41};
42
43enum {
44 VCO_MAX_HZ = 2200U * 1000000,
45 VCO_MIN_HZ = 440 * 1000000,
46 OUTPUT_MAX_HZ = 2200U * 1000000,
47 OUTPUT_MIN_HZ = 27500000,
48 FREF_MAX_HZ = 2200U * 1000000,
Heiko Stübnerc3f03ff2016-07-16 00:17:17 +020049 FREF_MIN_HZ = 269 * 1000,
Simon Glass99c15652015-08-30 16:55:31 -060050};
51
52enum {
53 /* PLL CON0 */
54 PLL_OD_MASK = 0x0f,
55
56 /* PLL CON1 */
57 PLL_NF_MASK = 0x1fff,
58
59 /* PLL CON2 */
60 PLL_BWADJ_MASK = 0x0fff,
61
62 /* PLL CON3 */
63 PLL_RESET_SHIFT = 5,
64
Simon Glassdae594f2016-01-21 19:45:17 -070065 /* CLKSEL0 */
Simon Glassdae594f2016-01-21 19:45:17 -070066 CORE_SEL_PLL_SHIFT = 15,
Simon Glassb223c1a2017-05-31 17:57:31 -060067 CORE_SEL_PLL_MASK = 1 << CORE_SEL_PLL_SHIFT,
Simon Glassdae594f2016-01-21 19:45:17 -070068 A17_DIV_SHIFT = 8,
Simon Glassb223c1a2017-05-31 17:57:31 -060069 A17_DIV_MASK = 0x1f << A17_DIV_SHIFT,
Simon Glassdae594f2016-01-21 19:45:17 -070070 MP_DIV_SHIFT = 4,
Simon Glassb223c1a2017-05-31 17:57:31 -060071 MP_DIV_MASK = 0xf << MP_DIV_SHIFT,
Simon Glassdae594f2016-01-21 19:45:17 -070072 M0_DIV_SHIFT = 0,
Simon Glassb223c1a2017-05-31 17:57:31 -060073 M0_DIV_MASK = 0xf << M0_DIV_SHIFT,
Simon Glassdae594f2016-01-21 19:45:17 -070074
Simon Glass99c15652015-08-30 16:55:31 -060075 /* CLKSEL1: pd bus clk pll sel: codec or general */
76 PD_BUS_SEL_PLL_MASK = 15,
77 PD_BUS_SEL_CPLL = 0,
78 PD_BUS_SEL_GPLL,
79
80 /* pd bus pclk div: pclk = pd_bus_aclk /(div + 1) */
81 PD_BUS_PCLK_DIV_SHIFT = 12,
Simon Glassb223c1a2017-05-31 17:57:31 -060082 PD_BUS_PCLK_DIV_MASK = 7 << PD_BUS_PCLK_DIV_SHIFT,
Simon Glass99c15652015-08-30 16:55:31 -060083
84 /* pd bus hclk div: aclk_bus: hclk_bus = 1:1 or 2:1 or 4:1 */
85 PD_BUS_HCLK_DIV_SHIFT = 8,
Simon Glassb223c1a2017-05-31 17:57:31 -060086 PD_BUS_HCLK_DIV_MASK = 3 << PD_BUS_HCLK_DIV_SHIFT,
Simon Glass99c15652015-08-30 16:55:31 -060087
88 /* pd bus aclk div: pd_bus_aclk = pd_bus_src_clk /(div0 * div1) */
89 PD_BUS_ACLK_DIV0_SHIFT = 3,
Simon Glassb223c1a2017-05-31 17:57:31 -060090 PD_BUS_ACLK_DIV0_MASK = 0x1f << PD_BUS_ACLK_DIV0_SHIFT,
Simon Glass99c15652015-08-30 16:55:31 -060091 PD_BUS_ACLK_DIV1_SHIFT = 0,
Simon Glassb223c1a2017-05-31 17:57:31 -060092 PD_BUS_ACLK_DIV1_MASK = 0x7 << PD_BUS_ACLK_DIV1_SHIFT,
Simon Glass99c15652015-08-30 16:55:31 -060093
94 /*
95 * CLKSEL10
96 * peripheral bus pclk div:
97 * aclk_bus: pclk_bus = 1:1 or 2:1 or 4:1 or 8:1
98 */
Simon Glassc87c1292016-01-21 19:45:15 -070099 PERI_SEL_PLL_SHIFT = 15,
Simon Glassb223c1a2017-05-31 17:57:31 -0600100 PERI_SEL_PLL_MASK = 1 << PERI_SEL_PLL_SHIFT,
Simon Glassc87c1292016-01-21 19:45:15 -0700101 PERI_SEL_CPLL = 0,
102 PERI_SEL_GPLL,
103
Simon Glass99c15652015-08-30 16:55:31 -0600104 PERI_PCLK_DIV_SHIFT = 12,
Simon Glassb223c1a2017-05-31 17:57:31 -0600105 PERI_PCLK_DIV_MASK = 3 << PERI_PCLK_DIV_SHIFT,
Simon Glass99c15652015-08-30 16:55:31 -0600106
107 /* peripheral bus hclk div: aclk_bus: hclk_bus = 1:1 or 2:1 or 4:1 */
108 PERI_HCLK_DIV_SHIFT = 8,
Simon Glassb223c1a2017-05-31 17:57:31 -0600109 PERI_HCLK_DIV_MASK = 3 << PERI_HCLK_DIV_SHIFT,
Simon Glass99c15652015-08-30 16:55:31 -0600110
111 /*
112 * peripheral bus aclk div:
113 * aclk_periph = periph_clk_src / (peri_aclk_div_con + 1)
114 */
115 PERI_ACLK_DIV_SHIFT = 0,
Simon Glassb223c1a2017-05-31 17:57:31 -0600116 PERI_ACLK_DIV_MASK = 0x1f << PERI_ACLK_DIV_SHIFT,
Simon Glass99c15652015-08-30 16:55:31 -0600117
David Wuef4cf5a2017-09-20 14:28:19 +0800118 /*
119 * CLKSEL24
120 * saradc_div_con:
121 * clk_saradc=24MHz/(saradc_div_con+1)
122 */
123 CLK_SARADC_DIV_CON_SHIFT = 8,
124 CLK_SARADC_DIV_CON_MASK = GENMASK(15, 8),
125 CLK_SARADC_DIV_CON_WIDTH = 8,
126
Simon Glass99c15652015-08-30 16:55:31 -0600127 SOCSTS_DPLL_LOCK = 1 << 5,
128 SOCSTS_APLL_LOCK = 1 << 6,
129 SOCSTS_CPLL_LOCK = 1 << 7,
130 SOCSTS_GPLL_LOCK = 1 << 8,
131 SOCSTS_NPLL_LOCK = 1 << 9,
132};
133
Simon Glass99c15652015-08-30 16:55:31 -0600134#define DIV_TO_RATE(input_rate, div) ((input_rate) / ((div) + 1))
135
136#define PLL_DIVISORS(hz, _nr, _no) {\
137 .nr = _nr, .nf = (u32)((u64)hz * _nr * _no / OSC_HZ), .no = _no};\
138 _Static_assert(((u64)hz * _nr * _no / OSC_HZ) * OSC_HZ /\
139 (_nr * _no) == hz, #hz "Hz cannot be hit with PLL "\
140 "divisors on line " __stringify(__LINE__));
141
142/* Keep divisors as low as possible to reduce jitter and power usage */
143static const struct pll_div apll_init_cfg = PLL_DIVISORS(APLL_HZ, 1, 1);
144static const struct pll_div gpll_init_cfg = PLL_DIVISORS(GPLL_HZ, 2, 2);
145static const struct pll_div cpll_init_cfg = PLL_DIVISORS(CPLL_HZ, 1, 2);
146
Jagan Tekib52a1992020-01-09 14:22:17 +0530147static int rkclk_set_pll(struct rockchip_cru *cru, enum rk_clk_id clk_id,
Simon Glass99c15652015-08-30 16:55:31 -0600148 const struct pll_div *div)
149{
150 int pll_id = rk_pll_id(clk_id);
151 struct rk3288_pll *pll = &cru->pll[pll_id];
152 /* All PLLs have same VCO and output frequency range restrictions. */
153 uint vco_hz = OSC_HZ / 1000 * div->nf / div->nr * 1000;
154 uint output_hz = vco_hz / div->no;
155
Simon Glassc87c1292016-01-21 19:45:15 -0700156 debug("PLL at %x: nf=%d, nr=%d, no=%d, vco=%u Hz, output=%u Hz\n",
157 (uint)pll, div->nf, div->nr, div->no, vco_hz, output_hz);
Simon Glass99c15652015-08-30 16:55:31 -0600158 assert(vco_hz >= VCO_MIN_HZ && vco_hz <= VCO_MAX_HZ &&
159 output_hz >= OUTPUT_MIN_HZ && output_hz <= OUTPUT_MAX_HZ &&
160 (div->no == 1 || !(div->no % 2)));
161
Simon Glassc87c1292016-01-21 19:45:15 -0700162 /* enter reset */
Simon Glass99c15652015-08-30 16:55:31 -0600163 rk_setreg(&pll->con3, 1 << PLL_RESET_SHIFT);
164
Simon Glassb223c1a2017-05-31 17:57:31 -0600165 rk_clrsetreg(&pll->con0, CLKR_MASK | PLL_OD_MASK,
Simon Glass99c15652015-08-30 16:55:31 -0600166 ((div->nr - 1) << CLKR_SHIFT) | (div->no - 1));
167 rk_clrsetreg(&pll->con1, CLKF_MASK, div->nf - 1);
168 rk_clrsetreg(&pll->con2, PLL_BWADJ_MASK, (div->nf >> 1) - 1);
169
170 udelay(10);
171
Simon Glassc87c1292016-01-21 19:45:15 -0700172 /* return from reset */
Simon Glass99c15652015-08-30 16:55:31 -0600173 rk_clrreg(&pll->con3, 1 << PLL_RESET_SHIFT);
174
175 return 0;
176}
177
Jagan Tekib52a1992020-01-09 14:22:17 +0530178static int rkclk_configure_ddr(struct rockchip_cru *cru, struct rk3288_grf *grf,
Simon Glass99c15652015-08-30 16:55:31 -0600179 unsigned int hz)
180{
181 static const struct pll_div dpll_cfg[] = {
182 {.nf = 25, .nr = 2, .no = 1},
183 {.nf = 400, .nr = 9, .no = 2},
184 {.nf = 500, .nr = 9, .no = 2},
185 {.nf = 100, .nr = 3, .no = 1},
186 };
187 int cfg;
188
Simon Glass99c15652015-08-30 16:55:31 -0600189 switch (hz) {
190 case 300000000:
191 cfg = 0;
192 break;
193 case 533000000: /* actually 533.3P MHz */
194 cfg = 1;
195 break;
196 case 666000000: /* actually 666.6P MHz */
197 cfg = 2;
198 break;
199 case 800000000:
200 cfg = 3;
201 break;
202 default:
Simon Glassc87c1292016-01-21 19:45:15 -0700203 debug("Unsupported SDRAM frequency");
Simon Glass99c15652015-08-30 16:55:31 -0600204 return -EINVAL;
205 }
206
207 /* pll enter slow-mode */
Simon Glassb223c1a2017-05-31 17:57:31 -0600208 rk_clrsetreg(&cru->cru_mode_con, DPLL_MODE_MASK,
Simon Glass99c15652015-08-30 16:55:31 -0600209 DPLL_MODE_SLOW << DPLL_MODE_SHIFT);
210
211 rkclk_set_pll(cru, CLK_DDR, &dpll_cfg[cfg]);
212
213 /* wait for pll lock */
214 while (!(readl(&grf->soc_status[1]) & SOCSTS_DPLL_LOCK))
215 udelay(1);
216
217 /* PLL enter normal-mode */
Simon Glassb223c1a2017-05-31 17:57:31 -0600218 rk_clrsetreg(&cru->cru_mode_con, DPLL_MODE_MASK,
Simon Glass009741f2016-01-21 19:45:01 -0700219 DPLL_MODE_NORMAL << DPLL_MODE_SHIFT);
Simon Glass99c15652015-08-30 16:55:31 -0600220
221 return 0;
222}
223
Simon Glass830a6082016-01-21 19:45:02 -0700224#ifndef CONFIG_SPL_BUILD
225#define VCO_MAX_KHZ 2200000
226#define VCO_MIN_KHZ 440000
227#define FREF_MAX_KHZ 2200000
228#define FREF_MIN_KHZ 269
229
230static int pll_para_config(ulong freq_hz, struct pll_div *div, uint *ext_div)
231{
232 uint ref_khz = OSC_HZ / 1000, nr, nf = 0;
233 uint fref_khz;
234 uint diff_khz, best_diff_khz;
235 const uint max_nr = 1 << 6, max_nf = 1 << 12, max_no = 1 << 4;
236 uint vco_khz;
237 uint no = 1;
238 uint freq_khz = freq_hz / 1000;
239
240 if (!freq_hz) {
241 printf("%s: the frequency can not be 0 Hz\n", __func__);
242 return -EINVAL;
243 }
244
245 no = DIV_ROUND_UP(VCO_MIN_KHZ, freq_khz);
246 if (ext_div) {
247 *ext_div = DIV_ROUND_UP(no, max_no);
248 no = DIV_ROUND_UP(no, *ext_div);
249 }
250
251 /* only even divisors (and 1) are supported */
252 if (no > 1)
253 no = DIV_ROUND_UP(no, 2) * 2;
254
255 vco_khz = freq_khz * no;
256 if (ext_div)
257 vco_khz *= *ext_div;
258
259 if (vco_khz < VCO_MIN_KHZ || vco_khz > VCO_MAX_KHZ || no > max_no) {
260 printf("%s: Cannot find out a supported VCO for Frequency (%luHz).\n",
261 __func__, freq_hz);
262 return -1;
263 }
264
265 div->no = no;
266
267 best_diff_khz = vco_khz;
268 for (nr = 1; nr < max_nr && best_diff_khz; nr++) {
269 fref_khz = ref_khz / nr;
270 if (fref_khz < FREF_MIN_KHZ)
271 break;
272 if (fref_khz > FREF_MAX_KHZ)
273 continue;
274
275 nf = vco_khz / fref_khz;
276 if (nf >= max_nf)
277 continue;
278 diff_khz = vco_khz - nf * fref_khz;
279 if (nf + 1 < max_nf && diff_khz > fref_khz / 2) {
280 nf++;
281 diff_khz = fref_khz - diff_khz;
282 }
283
284 if (diff_khz >= best_diff_khz)
285 continue;
286
287 best_diff_khz = diff_khz;
288 div->nr = nr;
289 div->nf = nf;
290 }
291
292 if (best_diff_khz > 4 * 1000) {
293 printf("%s: Failed to match output frequency %lu, difference is %u Hz, exceed 4MHZ\n",
294 __func__, freq_hz, best_diff_khz * 1000);
295 return -EINVAL;
296 }
297
298 return 0;
299}
300
Jagan Tekib52a1992020-01-09 14:22:17 +0530301static int rockchip_mac_set_clk(struct rockchip_cru *cru, uint freq)
Sjoerd Simons0aefc0b2016-02-28 22:24:59 +0100302{
David Wu01c60ea2018-01-13 14:06:33 +0800303 ulong ret;
Sjoerd Simons0aefc0b2016-02-28 22:24:59 +0100304
David Wu01c60ea2018-01-13 14:06:33 +0800305 /*
306 * The gmac clock can be derived either from an external clock
307 * or can be generated from internally by a divider from SCLK_MAC.
308 */
309 if (readl(&cru->cru_clksel_con[21]) & RMII_EXTCLK_MASK) {
310 /* An external clock will always generate the right rate... */
311 ret = freq;
312 } else {
313 u32 con = readl(&cru->cru_clksel_con[21]);
314 ulong pll_rate;
315 u8 div;
316
317 if (((con >> EMAC_PLL_SHIFT) & EMAC_PLL_MASK) ==
318 EMAC_PLL_SELECT_GENERAL)
319 pll_rate = GPLL_HZ;
320 else if (((con >> EMAC_PLL_SHIFT) & EMAC_PLL_MASK) ==
321 EMAC_PLL_SELECT_CODEC)
322 pll_rate = CPLL_HZ;
323 else
324 pll_rate = NPLL_HZ;
325
326 div = DIV_ROUND_UP(pll_rate, freq) - 1;
327 if (div <= 0x1f)
328 rk_clrsetreg(&cru->cru_clksel_con[21], MAC_DIV_CON_MASK,
329 div << MAC_DIV_CON_SHIFT);
330 else
331 debug("Unsupported div for gmac:%d\n", div);
332
333 return DIV_TO_RATE(pll_rate, div);
334 }
335
336 return ret;
Sjoerd Simons0aefc0b2016-02-28 22:24:59 +0100337}
338
Jagan Tekib52a1992020-01-09 14:22:17 +0530339static int rockchip_vop_set_clk(struct rockchip_cru *cru, struct rk3288_grf *grf,
Simon Glass830a6082016-01-21 19:45:02 -0700340 int periph, unsigned int rate_hz)
341{
342 struct pll_div npll_config = {0};
343 u32 lcdc_div;
344 int ret;
345
346 ret = pll_para_config(rate_hz, &npll_config, &lcdc_div);
347 if (ret)
348 return ret;
349
Simon Glassb223c1a2017-05-31 17:57:31 -0600350 rk_clrsetreg(&cru->cru_mode_con, NPLL_MODE_MASK,
Simon Glass830a6082016-01-21 19:45:02 -0700351 NPLL_MODE_SLOW << NPLL_MODE_SHIFT);
352 rkclk_set_pll(cru, CLK_NEW, &npll_config);
353
354 /* waiting for pll lock */
355 while (1) {
356 if (readl(&grf->soc_status[1]) & SOCSTS_NPLL_LOCK)
357 break;
358 udelay(1);
359 }
360
Simon Glassb223c1a2017-05-31 17:57:31 -0600361 rk_clrsetreg(&cru->cru_mode_con, NPLL_MODE_MASK,
Simon Glass830a6082016-01-21 19:45:02 -0700362 NPLL_MODE_NORMAL << NPLL_MODE_SHIFT);
363
364 /* vop dclk source clk: npll,dclk_div: 1 */
365 switch (periph) {
366 case DCLK_VOP0:
367 rk_clrsetreg(&cru->cru_clksel_con[27], 0xff << 8 | 3 << 0,
368 (lcdc_div - 1) << 8 | 2 << 0);
369 break;
370 case DCLK_VOP1:
371 rk_clrsetreg(&cru->cru_clksel_con[29], 0xff << 8 | 3 << 6,
372 (lcdc_div - 1) << 8 | 2 << 6);
373 break;
374 }
375
376 return 0;
377}
Simon Glass3dbfe5a2018-12-27 20:15:20 -0700378
379static u32 rockchip_clk_gcd(u32 a, u32 b)
380{
381 while (b != 0) {
382 int r = b;
383
384 b = a % b;
385 a = r;
386 }
387 return a;
388}
389
Jagan Tekib52a1992020-01-09 14:22:17 +0530390static ulong rockchip_i2s_get_clk(struct rockchip_cru *cru, uint gclk_rate)
Simon Glass3dbfe5a2018-12-27 20:15:20 -0700391{
392 unsigned long long rate;
393 uint val;
394 int n, d;
395
396 val = readl(&cru->cru_clksel_con[8]);
397 n = (val & I2S0_FRAC_NUMER_MASK) >> I2S0_FRAC_NUMER_SHIFT;
398 d = (val & I2S0_FRAC_DENOM_MASK) >> I2S0_FRAC_DENOM_SHIFT;
399
400 rate = (unsigned long long)gclk_rate * n;
401 do_div(rate, d);
402
403 return (ulong)rate;
404}
405
Jagan Tekib52a1992020-01-09 14:22:17 +0530406static ulong rockchip_i2s_set_clk(struct rockchip_cru *cru, uint gclk_rate,
Simon Glass3dbfe5a2018-12-27 20:15:20 -0700407 uint freq)
408{
409 int n, d;
410 int v;
411
412 /* set frac divider */
413 v = rockchip_clk_gcd(gclk_rate, freq);
414 n = gclk_rate / v;
415 d = freq / v;
416 assert(freq == gclk_rate / n * d);
417 writel(d << I2S0_FRAC_NUMER_SHIFT | n << I2S0_FRAC_DENOM_SHIFT,
418 &cru->cru_clksel_con[8]);
419
420 return rockchip_i2s_get_clk(cru, gclk_rate);
421}
Simon Glassd3cb46a2017-05-31 17:57:32 -0600422#endif /* CONFIG_SPL_BUILD */
Simon Glass830a6082016-01-21 19:45:02 -0700423
Jagan Tekib52a1992020-01-09 14:22:17 +0530424static void rkclk_init(struct rockchip_cru *cru, struct rk3288_grf *grf)
Simon Glass99c15652015-08-30 16:55:31 -0600425{
426 u32 aclk_div;
427 u32 hclk_div;
428 u32 pclk_div;
429
430 /* pll enter slow-mode */
431 rk_clrsetreg(&cru->cru_mode_con,
Simon Glassb223c1a2017-05-31 17:57:31 -0600432 GPLL_MODE_MASK | CPLL_MODE_MASK,
Simon Glass99c15652015-08-30 16:55:31 -0600433 GPLL_MODE_SLOW << GPLL_MODE_SHIFT |
434 CPLL_MODE_SLOW << CPLL_MODE_SHIFT);
435
436 /* init pll */
437 rkclk_set_pll(cru, CLK_GENERAL, &gpll_init_cfg);
438 rkclk_set_pll(cru, CLK_CODEC, &cpll_init_cfg);
439
440 /* waiting for pll lock */
441 while ((readl(&grf->soc_status[1]) &
442 (SOCSTS_CPLL_LOCK | SOCSTS_GPLL_LOCK)) !=
443 (SOCSTS_CPLL_LOCK | SOCSTS_GPLL_LOCK))
444 udelay(1);
445
446 /*
447 * pd_bus clock pll source selection and
448 * set up dependent divisors for PCLK/HCLK and ACLK clocks.
449 */
450 aclk_div = GPLL_HZ / PD_BUS_ACLK_HZ - 1;
451 assert((aclk_div + 1) * PD_BUS_ACLK_HZ == GPLL_HZ && aclk_div < 0x1f);
452 hclk_div = PD_BUS_ACLK_HZ / PD_BUS_HCLK_HZ - 1;
453 assert((hclk_div + 1) * PD_BUS_HCLK_HZ ==
454 PD_BUS_ACLK_HZ && (hclk_div < 0x4) && (hclk_div != 0x2));
455
456 pclk_div = PD_BUS_ACLK_HZ / PD_BUS_PCLK_HZ - 1;
457 assert((pclk_div + 1) * PD_BUS_PCLK_HZ ==
458 PD_BUS_ACLK_HZ && pclk_div < 0x7);
459
460 rk_clrsetreg(&cru->cru_clksel_con[1],
Simon Glassb223c1a2017-05-31 17:57:31 -0600461 PD_BUS_PCLK_DIV_MASK | PD_BUS_HCLK_DIV_MASK |
462 PD_BUS_ACLK_DIV0_MASK | PD_BUS_ACLK_DIV1_MASK,
Simon Glass99c15652015-08-30 16:55:31 -0600463 pclk_div << PD_BUS_PCLK_DIV_SHIFT |
464 hclk_div << PD_BUS_HCLK_DIV_SHIFT |
465 aclk_div << PD_BUS_ACLK_DIV0_SHIFT |
466 0 << 0);
467
468 /*
469 * peri clock pll source selection and
470 * set up dependent divisors for PCLK/HCLK and ACLK clocks.
471 */
472 aclk_div = GPLL_HZ / PERI_ACLK_HZ - 1;
473 assert((aclk_div + 1) * PERI_ACLK_HZ == GPLL_HZ && aclk_div < 0x1f);
474
Heiko Stübnerabd01282016-07-22 23:51:06 +0200475 hclk_div = ilog2(PERI_ACLK_HZ / PERI_HCLK_HZ);
Simon Glass99c15652015-08-30 16:55:31 -0600476 assert((1 << hclk_div) * PERI_HCLK_HZ ==
477 PERI_ACLK_HZ && (hclk_div < 0x4));
478
Heiko Stübnerabd01282016-07-22 23:51:06 +0200479 pclk_div = ilog2(PERI_ACLK_HZ / PERI_PCLK_HZ);
Simon Glass99c15652015-08-30 16:55:31 -0600480 assert((1 << pclk_div) * PERI_PCLK_HZ ==
481 PERI_ACLK_HZ && (pclk_div < 0x4));
482
483 rk_clrsetreg(&cru->cru_clksel_con[10],
Simon Glassb223c1a2017-05-31 17:57:31 -0600484 PERI_PCLK_DIV_MASK | PERI_HCLK_DIV_MASK |
485 PERI_ACLK_DIV_MASK,
Simon Glassc87c1292016-01-21 19:45:15 -0700486 PERI_SEL_GPLL << PERI_SEL_PLL_SHIFT |
Simon Glass99c15652015-08-30 16:55:31 -0600487 pclk_div << PERI_PCLK_DIV_SHIFT |
488 hclk_div << PERI_HCLK_DIV_SHIFT |
489 aclk_div << PERI_ACLK_DIV_SHIFT);
490
491 /* PLL enter normal-mode */
492 rk_clrsetreg(&cru->cru_mode_con,
Simon Glassb223c1a2017-05-31 17:57:31 -0600493 GPLL_MODE_MASK | CPLL_MODE_MASK,
Simon Glass009741f2016-01-21 19:45:01 -0700494 GPLL_MODE_NORMAL << GPLL_MODE_SHIFT |
495 CPLL_MODE_NORMAL << CPLL_MODE_SHIFT);
Simon Glass99c15652015-08-30 16:55:31 -0600496}
Simon Glass99c15652015-08-30 16:55:31 -0600497
Jagan Tekib52a1992020-01-09 14:22:17 +0530498void rk3288_clk_configure_cpu(struct rockchip_cru *cru, struct rk3288_grf *grf)
Simon Glassdae594f2016-01-21 19:45:17 -0700499{
500 /* pll enter slow-mode */
Simon Glassb223c1a2017-05-31 17:57:31 -0600501 rk_clrsetreg(&cru->cru_mode_con, APLL_MODE_MASK,
Simon Glassdae594f2016-01-21 19:45:17 -0700502 APLL_MODE_SLOW << APLL_MODE_SHIFT);
503
504 rkclk_set_pll(cru, CLK_ARM, &apll_init_cfg);
505
506 /* waiting for pll lock */
507 while (!(readl(&grf->soc_status[1]) & SOCSTS_APLL_LOCK))
508 udelay(1);
509
510 /*
511 * core clock pll source selection and
512 * set up dependent divisors for MPAXI/M0AXI and ARM clocks.
513 * core clock select apll, apll clk = 1800MHz
514 * arm clk = 1800MHz, mpclk = 450MHz, m0clk = 900MHz
515 */
516 rk_clrsetreg(&cru->cru_clksel_con[0],
Simon Glassb223c1a2017-05-31 17:57:31 -0600517 CORE_SEL_PLL_MASK | A17_DIV_MASK | MP_DIV_MASK |
518 M0_DIV_MASK,
Simon Glassdae594f2016-01-21 19:45:17 -0700519 0 << A17_DIV_SHIFT |
520 3 << MP_DIV_SHIFT |
521 1 << M0_DIV_SHIFT);
522
523 /*
524 * set up dependent divisors for L2RAM/ATCLK and PCLK clocks.
525 * l2ramclk = 900MHz, atclk = 450MHz, pclk_dbg = 450MHz
526 */
527 rk_clrsetreg(&cru->cru_clksel_con[37],
Simon Glassb223c1a2017-05-31 17:57:31 -0600528 CLK_L2RAM_DIV_MASK | ATCLK_CORE_DIV_CON_MASK |
529 PCLK_CORE_DBG_DIV_MASK,
Simon Glassdae594f2016-01-21 19:45:17 -0700530 1 << CLK_L2RAM_DIV_SHIFT |
531 3 << ATCLK_CORE_DIV_CON_SHIFT |
532 3 << PCLK_CORE_DBG_DIV_SHIFT);
533
534 /* PLL enter normal-mode */
Simon Glassb223c1a2017-05-31 17:57:31 -0600535 rk_clrsetreg(&cru->cru_mode_con, APLL_MODE_MASK,
Simon Glassdae594f2016-01-21 19:45:17 -0700536 APLL_MODE_NORMAL << APLL_MODE_SHIFT);
537}
538
Simon Glass99c15652015-08-30 16:55:31 -0600539/* Get pll rate by id */
Jagan Tekib52a1992020-01-09 14:22:17 +0530540static uint32_t rkclk_pll_get_rate(struct rockchip_cru *cru,
Simon Glass99c15652015-08-30 16:55:31 -0600541 enum rk_clk_id clk_id)
542{
543 uint32_t nr, no, nf;
544 uint32_t con;
545 int pll_id = rk_pll_id(clk_id);
546 struct rk3288_pll *pll = &cru->pll[pll_id];
547 static u8 clk_shift[CLK_COUNT] = {
Simon Glass009741f2016-01-21 19:45:01 -0700548 0xff, APLL_MODE_SHIFT, DPLL_MODE_SHIFT, CPLL_MODE_SHIFT,
549 GPLL_MODE_SHIFT, NPLL_MODE_SHIFT
Simon Glass99c15652015-08-30 16:55:31 -0600550 };
551 uint shift;
552
553 con = readl(&cru->cru_mode_con);
554 shift = clk_shift[clk_id];
Simon Glassb223c1a2017-05-31 17:57:31 -0600555 switch ((con >> shift) & CRU_MODE_MASK) {
Simon Glass009741f2016-01-21 19:45:01 -0700556 case APLL_MODE_SLOW:
Simon Glass99c15652015-08-30 16:55:31 -0600557 return OSC_HZ;
Simon Glass009741f2016-01-21 19:45:01 -0700558 case APLL_MODE_NORMAL:
Simon Glass99c15652015-08-30 16:55:31 -0600559 /* normal mode */
560 con = readl(&pll->con0);
Simon Glassb223c1a2017-05-31 17:57:31 -0600561 no = ((con & CLKOD_MASK) >> CLKOD_SHIFT) + 1;
562 nr = ((con & CLKR_MASK) >> CLKR_SHIFT) + 1;
Simon Glass99c15652015-08-30 16:55:31 -0600563 con = readl(&pll->con1);
Simon Glassb223c1a2017-05-31 17:57:31 -0600564 nf = ((con & CLKF_MASK) >> CLKF_SHIFT) + 1;
Simon Glass99c15652015-08-30 16:55:31 -0600565
566 return (24 * nf / (nr * no)) * 1000000;
Simon Glass009741f2016-01-21 19:45:01 -0700567 case APLL_MODE_DEEP:
Simon Glass99c15652015-08-30 16:55:31 -0600568 default:
569 return 32768;
570 }
571}
572
Jagan Tekib52a1992020-01-09 14:22:17 +0530573static ulong rockchip_mmc_get_clk(struct rockchip_cru *cru, uint gclk_rate,
Simon Glass898d6432016-01-21 19:43:38 -0700574 int periph)
Simon Glass99c15652015-08-30 16:55:31 -0600575{
576 uint src_rate;
577 uint div, mux;
578 u32 con;
579
580 switch (periph) {
Simon Glass898d6432016-01-21 19:43:38 -0700581 case HCLK_EMMC:
Xu Ziyuan45112272017-04-16 17:44:45 +0800582 case SCLK_EMMC:
Simon Glass99c15652015-08-30 16:55:31 -0600583 con = readl(&cru->cru_clksel_con[12]);
Simon Glassb223c1a2017-05-31 17:57:31 -0600584 mux = (con & EMMC_PLL_MASK) >> EMMC_PLL_SHIFT;
585 div = (con & EMMC_DIV_MASK) >> EMMC_DIV_SHIFT;
Simon Glass99c15652015-08-30 16:55:31 -0600586 break;
Simon Glass898d6432016-01-21 19:43:38 -0700587 case HCLK_SDMMC:
Xu Ziyuan45112272017-04-16 17:44:45 +0800588 case SCLK_SDMMC:
Simon Glass898d6432016-01-21 19:43:38 -0700589 con = readl(&cru->cru_clksel_con[11]);
Simon Glassb223c1a2017-05-31 17:57:31 -0600590 mux = (con & MMC0_PLL_MASK) >> MMC0_PLL_SHIFT;
591 div = (con & MMC0_DIV_MASK) >> MMC0_DIV_SHIFT;
Simon Glass99c15652015-08-30 16:55:31 -0600592 break;
Simon Glass898d6432016-01-21 19:43:38 -0700593 case HCLK_SDIO0:
Xu Ziyuan45112272017-04-16 17:44:45 +0800594 case SCLK_SDIO0:
Simon Glass99c15652015-08-30 16:55:31 -0600595 con = readl(&cru->cru_clksel_con[12]);
Simon Glassb223c1a2017-05-31 17:57:31 -0600596 mux = (con & SDIO0_PLL_MASK) >> SDIO0_PLL_SHIFT;
597 div = (con & SDIO0_DIV_MASK) >> SDIO0_DIV_SHIFT;
Simon Glass99c15652015-08-30 16:55:31 -0600598 break;
599 default:
600 return -EINVAL;
601 }
602
Simon Glass542635a2016-01-21 19:43:39 -0700603 src_rate = mux == EMMC_PLL_SELECT_24MHZ ? OSC_HZ : gclk_rate;
Simon Glass99c15652015-08-30 16:55:31 -0600604 return DIV_TO_RATE(src_rate, div);
605}
606
Jagan Tekib52a1992020-01-09 14:22:17 +0530607static ulong rockchip_mmc_set_clk(struct rockchip_cru *cru, uint gclk_rate,
Simon Glass898d6432016-01-21 19:43:38 -0700608 int periph, uint freq)
Simon Glass99c15652015-08-30 16:55:31 -0600609{
610 int src_clk_div;
611 int mux;
612
Simon Glass542635a2016-01-21 19:43:39 -0700613 debug("%s: gclk_rate=%u\n", __func__, gclk_rate);
Kever Yang3a94d752017-07-27 12:54:01 +0800614 /* mmc clock default div 2 internal, need provide double in cru */
615 src_clk_div = DIV_ROUND_UP(gclk_rate / 2, freq);
Simon Glass99c15652015-08-30 16:55:31 -0600616
617 if (src_clk_div > 0x3f) {
Kever Yang3a94d752017-07-27 12:54:01 +0800618 src_clk_div = DIV_ROUND_UP(OSC_HZ / 2, freq);
Kever Yang217273c2017-07-27 12:54:02 +0800619 assert(src_clk_div < 0x40);
Simon Glass99c15652015-08-30 16:55:31 -0600620 mux = EMMC_PLL_SELECT_24MHZ;
621 assert((int)EMMC_PLL_SELECT_24MHZ ==
622 (int)MMC0_PLL_SELECT_24MHZ);
623 } else {
624 mux = EMMC_PLL_SELECT_GENERAL;
625 assert((int)EMMC_PLL_SELECT_GENERAL ==
626 (int)MMC0_PLL_SELECT_GENERAL);
627 }
628 switch (periph) {
Simon Glass898d6432016-01-21 19:43:38 -0700629 case HCLK_EMMC:
Xu Ziyuan45112272017-04-16 17:44:45 +0800630 case SCLK_EMMC:
Simon Glass99c15652015-08-30 16:55:31 -0600631 rk_clrsetreg(&cru->cru_clksel_con[12],
Simon Glassb223c1a2017-05-31 17:57:31 -0600632 EMMC_PLL_MASK | EMMC_DIV_MASK,
Simon Glass99c15652015-08-30 16:55:31 -0600633 mux << EMMC_PLL_SHIFT |
634 (src_clk_div - 1) << EMMC_DIV_SHIFT);
635 break;
Simon Glass898d6432016-01-21 19:43:38 -0700636 case HCLK_SDMMC:
Xu Ziyuan45112272017-04-16 17:44:45 +0800637 case SCLK_SDMMC:
Simon Glass99c15652015-08-30 16:55:31 -0600638 rk_clrsetreg(&cru->cru_clksel_con[11],
Simon Glassb223c1a2017-05-31 17:57:31 -0600639 MMC0_PLL_MASK | MMC0_DIV_MASK,
Simon Glass99c15652015-08-30 16:55:31 -0600640 mux << MMC0_PLL_SHIFT |
641 (src_clk_div - 1) << MMC0_DIV_SHIFT);
642 break;
Simon Glass898d6432016-01-21 19:43:38 -0700643 case HCLK_SDIO0:
Xu Ziyuan45112272017-04-16 17:44:45 +0800644 case SCLK_SDIO0:
Simon Glass99c15652015-08-30 16:55:31 -0600645 rk_clrsetreg(&cru->cru_clksel_con[12],
Simon Glassb223c1a2017-05-31 17:57:31 -0600646 SDIO0_PLL_MASK | SDIO0_DIV_MASK,
Simon Glass99c15652015-08-30 16:55:31 -0600647 mux << SDIO0_PLL_SHIFT |
648 (src_clk_div - 1) << SDIO0_DIV_SHIFT);
649 break;
650 default:
651 return -EINVAL;
652 }
653
Simon Glass542635a2016-01-21 19:43:39 -0700654 return rockchip_mmc_get_clk(cru, gclk_rate, periph);
Simon Glass99c15652015-08-30 16:55:31 -0600655}
656
Jagan Tekib52a1992020-01-09 14:22:17 +0530657static ulong rockchip_spi_get_clk(struct rockchip_cru *cru, uint gclk_rate,
Simon Glass898d6432016-01-21 19:43:38 -0700658 int periph)
Simon Glass99c15652015-08-30 16:55:31 -0600659{
660 uint div, mux;
661 u32 con;
662
663 switch (periph) {
Simon Glass898d6432016-01-21 19:43:38 -0700664 case SCLK_SPI0:
Simon Glass99c15652015-08-30 16:55:31 -0600665 con = readl(&cru->cru_clksel_con[25]);
Simon Glassb223c1a2017-05-31 17:57:31 -0600666 mux = (con & SPI0_PLL_MASK) >> SPI0_PLL_SHIFT;
667 div = (con & SPI0_DIV_MASK) >> SPI0_DIV_SHIFT;
Simon Glass99c15652015-08-30 16:55:31 -0600668 break;
Simon Glass898d6432016-01-21 19:43:38 -0700669 case SCLK_SPI1:
Simon Glass99c15652015-08-30 16:55:31 -0600670 con = readl(&cru->cru_clksel_con[25]);
Simon Glassb223c1a2017-05-31 17:57:31 -0600671 mux = (con & SPI1_PLL_MASK) >> SPI1_PLL_SHIFT;
672 div = (con & SPI1_DIV_MASK) >> SPI1_DIV_SHIFT;
Simon Glass99c15652015-08-30 16:55:31 -0600673 break;
Simon Glass898d6432016-01-21 19:43:38 -0700674 case SCLK_SPI2:
Simon Glass99c15652015-08-30 16:55:31 -0600675 con = readl(&cru->cru_clksel_con[39]);
Simon Glassb223c1a2017-05-31 17:57:31 -0600676 mux = (con & SPI2_PLL_MASK) >> SPI2_PLL_SHIFT;
677 div = (con & SPI2_DIV_MASK) >> SPI2_DIV_SHIFT;
Simon Glass99c15652015-08-30 16:55:31 -0600678 break;
679 default:
680 return -EINVAL;
681 }
682 assert(mux == SPI0_PLL_SELECT_GENERAL);
683
Simon Glass542635a2016-01-21 19:43:39 -0700684 return DIV_TO_RATE(gclk_rate, div);
Simon Glass99c15652015-08-30 16:55:31 -0600685}
686
Jagan Tekib52a1992020-01-09 14:22:17 +0530687static ulong rockchip_spi_set_clk(struct rockchip_cru *cru, uint gclk_rate,
Simon Glass898d6432016-01-21 19:43:38 -0700688 int periph, uint freq)
Simon Glass99c15652015-08-30 16:55:31 -0600689{
690 int src_clk_div;
691
Simon Glass542635a2016-01-21 19:43:39 -0700692 debug("%s: clk_general_rate=%u\n", __func__, gclk_rate);
Kever Yang217273c2017-07-27 12:54:02 +0800693 src_clk_div = DIV_ROUND_UP(gclk_rate, freq) - 1;
694 assert(src_clk_div < 128);
Simon Glass99c15652015-08-30 16:55:31 -0600695 switch (periph) {
Simon Glass898d6432016-01-21 19:43:38 -0700696 case SCLK_SPI0:
Simon Glass99c15652015-08-30 16:55:31 -0600697 rk_clrsetreg(&cru->cru_clksel_con[25],
Simon Glassb223c1a2017-05-31 17:57:31 -0600698 SPI0_PLL_MASK | SPI0_DIV_MASK,
Simon Glass99c15652015-08-30 16:55:31 -0600699 SPI0_PLL_SELECT_GENERAL << SPI0_PLL_SHIFT |
700 src_clk_div << SPI0_DIV_SHIFT);
701 break;
Simon Glass898d6432016-01-21 19:43:38 -0700702 case SCLK_SPI1:
Simon Glass99c15652015-08-30 16:55:31 -0600703 rk_clrsetreg(&cru->cru_clksel_con[25],
Simon Glassb223c1a2017-05-31 17:57:31 -0600704 SPI1_PLL_MASK | SPI1_DIV_MASK,
Simon Glass99c15652015-08-30 16:55:31 -0600705 SPI1_PLL_SELECT_GENERAL << SPI1_PLL_SHIFT |
706 src_clk_div << SPI1_DIV_SHIFT);
707 break;
Simon Glass898d6432016-01-21 19:43:38 -0700708 case SCLK_SPI2:
Simon Glass99c15652015-08-30 16:55:31 -0600709 rk_clrsetreg(&cru->cru_clksel_con[39],
Simon Glassb223c1a2017-05-31 17:57:31 -0600710 SPI2_PLL_MASK | SPI2_DIV_MASK,
Simon Glass99c15652015-08-30 16:55:31 -0600711 SPI2_PLL_SELECT_GENERAL << SPI2_PLL_SHIFT |
712 src_clk_div << SPI2_DIV_SHIFT);
713 break;
714 default:
715 return -EINVAL;
716 }
717
Simon Glass542635a2016-01-21 19:43:39 -0700718 return rockchip_spi_get_clk(cru, gclk_rate, periph);
Simon Glass99c15652015-08-30 16:55:31 -0600719}
720
Jagan Tekib52a1992020-01-09 14:22:17 +0530721static ulong rockchip_saradc_get_clk(struct rockchip_cru *cru)
David Wuef4cf5a2017-09-20 14:28:19 +0800722{
723 u32 div, val;
724
725 val = readl(&cru->cru_clksel_con[24]);
726 div = bitfield_extract(val, CLK_SARADC_DIV_CON_SHIFT,
727 CLK_SARADC_DIV_CON_WIDTH);
728
729 return DIV_TO_RATE(OSC_HZ, div);
730}
731
Jagan Tekib52a1992020-01-09 14:22:17 +0530732static ulong rockchip_saradc_set_clk(struct rockchip_cru *cru, uint hz)
David Wuef4cf5a2017-09-20 14:28:19 +0800733{
734 int src_clk_div;
735
736 src_clk_div = DIV_ROUND_UP(OSC_HZ, hz) - 1;
737 assert(src_clk_div < 128);
738
739 rk_clrsetreg(&cru->cru_clksel_con[24],
740 CLK_SARADC_DIV_CON_MASK,
741 src_clk_div << CLK_SARADC_DIV_CON_SHIFT);
742
743 return rockchip_saradc_get_clk(cru);
744}
745
Stephen Warren135aa952016-06-17 09:44:00 -0600746static ulong rk3288_clk_get_rate(struct clk *clk)
Simon Glass4f436732016-01-21 19:43:40 -0700747{
Stephen Warren135aa952016-06-17 09:44:00 -0600748 struct rk3288_clk_priv *priv = dev_get_priv(clk->dev);
Simon Glass4f436732016-01-21 19:43:40 -0700749 ulong new_rate, gclk_rate;
Simon Glass4f436732016-01-21 19:43:40 -0700750
Stephen Warren135aa952016-06-17 09:44:00 -0600751 gclk_rate = rkclk_pll_get_rate(priv->cru, CLK_GENERAL);
752 switch (clk->id) {
753 case 0 ... 63:
754 new_rate = rkclk_pll_get_rate(priv->cru, clk->id);
755 break;
Simon Glass4f436732016-01-21 19:43:40 -0700756 case HCLK_EMMC:
Simon Glass342999f2016-01-21 19:43:45 -0700757 case HCLK_SDMMC:
Simon Glass4f436732016-01-21 19:43:40 -0700758 case HCLK_SDIO0:
Xu Ziyuan45112272017-04-16 17:44:45 +0800759 case SCLK_EMMC:
760 case SCLK_SDMMC:
761 case SCLK_SDIO0:
Stephen Warren135aa952016-06-17 09:44:00 -0600762 new_rate = rockchip_mmc_get_clk(priv->cru, gclk_rate, clk->id);
Simon Glass4f436732016-01-21 19:43:40 -0700763 break;
764 case SCLK_SPI0:
765 case SCLK_SPI1:
766 case SCLK_SPI2:
Stephen Warren135aa952016-06-17 09:44:00 -0600767 new_rate = rockchip_spi_get_clk(priv->cru, gclk_rate, clk->id);
Simon Glass4f436732016-01-21 19:43:40 -0700768 break;
769 case PCLK_I2C0:
770 case PCLK_I2C1:
771 case PCLK_I2C2:
772 case PCLK_I2C3:
773 case PCLK_I2C4:
774 case PCLK_I2C5:
775 return gclk_rate;
Kever Yang4f0b8ef2016-08-12 17:57:05 +0800776 case PCLK_PWM:
777 return PD_BUS_PCLK_HZ;
David Wuef4cf5a2017-09-20 14:28:19 +0800778 case SCLK_SARADC:
779 new_rate = rockchip_saradc_get_clk(priv->cru);
780 break;
Simon Glass4f436732016-01-21 19:43:40 -0700781 default:
782 return -ENOENT;
783 }
784
785 return new_rate;
786}
787
Stephen Warren135aa952016-06-17 09:44:00 -0600788static ulong rk3288_clk_set_rate(struct clk *clk, ulong rate)
Simon Glass99c15652015-08-30 16:55:31 -0600789{
Stephen Warren135aa952016-06-17 09:44:00 -0600790 struct rk3288_clk_priv *priv = dev_get_priv(clk->dev);
Jagan Tekib52a1992020-01-09 14:22:17 +0530791 struct rockchip_cru *cru = priv->cru;
Simon Glass898d6432016-01-21 19:43:38 -0700792 ulong new_rate, gclk_rate;
Simon Glass99c15652015-08-30 16:55:31 -0600793
Stephen Warren135aa952016-06-17 09:44:00 -0600794 gclk_rate = rkclk_pll_get_rate(priv->cru, CLK_GENERAL);
795 switch (clk->id) {
Simon Glass3a8a42d2016-11-13 14:22:13 -0700796 case PLL_APLL:
797 /* We only support a fixed rate here */
798 if (rate != 1800000000)
799 return -EINVAL;
800 rk3288_clk_configure_cpu(priv->cru, priv->grf);
801 new_rate = rate;
802 break;
Stephen Warren135aa952016-06-17 09:44:00 -0600803 case CLK_DDR:
804 new_rate = rkclk_configure_ddr(priv->cru, priv->grf, rate);
805 break;
Simon Glass898d6432016-01-21 19:43:38 -0700806 case HCLK_EMMC:
807 case HCLK_SDMMC:
808 case HCLK_SDIO0:
Xu Ziyuan45112272017-04-16 17:44:45 +0800809 case SCLK_EMMC:
810 case SCLK_SDMMC:
811 case SCLK_SDIO0:
Stephen Warren135aa952016-06-17 09:44:00 -0600812 new_rate = rockchip_mmc_set_clk(cru, gclk_rate, clk->id, rate);
Simon Glass99c15652015-08-30 16:55:31 -0600813 break;
Simon Glass898d6432016-01-21 19:43:38 -0700814 case SCLK_SPI0:
815 case SCLK_SPI1:
816 case SCLK_SPI2:
Stephen Warren135aa952016-06-17 09:44:00 -0600817 new_rate = rockchip_spi_set_clk(cru, gclk_rate, clk->id, rate);
Simon Glass99c15652015-08-30 16:55:31 -0600818 break;
Simon Glass830a6082016-01-21 19:45:02 -0700819#ifndef CONFIG_SPL_BUILD
Simon Glass3dbfe5a2018-12-27 20:15:20 -0700820 case SCLK_I2S0:
821 new_rate = rockchip_i2s_set_clk(cru, gclk_rate, rate);
822 break;
Sjoerd Simons0aefc0b2016-02-28 22:24:59 +0100823 case SCLK_MAC:
David Wu01c60ea2018-01-13 14:06:33 +0800824 new_rate = rockchip_mac_set_clk(priv->cru, rate);
Sjoerd Simons0aefc0b2016-02-28 22:24:59 +0100825 break;
Simon Glass830a6082016-01-21 19:45:02 -0700826 case DCLK_VOP0:
827 case DCLK_VOP1:
Stephen Warren135aa952016-06-17 09:44:00 -0600828 new_rate = rockchip_vop_set_clk(cru, priv->grf, clk->id, rate);
Simon Glass830a6082016-01-21 19:45:02 -0700829 break;
830 case SCLK_EDP_24M:
831 /* clk_edp_24M source: 24M */
832 rk_setreg(&cru->cru_clksel_con[28], 1 << 15);
833
834 /* rst edp */
835 rk_setreg(&cru->cru_clksel_con[6], 1 << 15);
836 udelay(1);
837 rk_clrreg(&cru->cru_clksel_con[6], 1 << 15);
838 new_rate = rate;
839 break;
840 case ACLK_VOP0:
841 case ACLK_VOP1: {
842 u32 div;
843
844 /* vop aclk source clk: cpll */
845 div = CPLL_HZ / rate;
846 assert((div - 1 < 64) && (div * rate == CPLL_HZ));
847
Stephen Warren135aa952016-06-17 09:44:00 -0600848 switch (clk->id) {
Simon Glass830a6082016-01-21 19:45:02 -0700849 case ACLK_VOP0:
850 rk_clrsetreg(&cru->cru_clksel_con[31],
851 3 << 6 | 0x1f << 0,
852 0 << 6 | (div - 1) << 0);
853 break;
854 case ACLK_VOP1:
855 rk_clrsetreg(&cru->cru_clksel_con[31],
856 3 << 14 | 0x1f << 8,
857 0 << 14 | (div - 1) << 8);
858 break;
859 }
860 new_rate = rate;
861 break;
862 }
863 case PCLK_HDMI_CTRL:
864 /* enable pclk hdmi ctrl */
865 rk_clrreg(&cru->cru_clkgate_con[16], 1 << 9);
866
867 /* software reset hdmi */
868 rk_setreg(&cru->cru_clkgate_con[7], 1 << 9);
869 udelay(1);
870 rk_clrreg(&cru->cru_clkgate_con[7], 1 << 9);
871 new_rate = rate;
872 break;
873#endif
David Wuef4cf5a2017-09-20 14:28:19 +0800874 case SCLK_SARADC:
875 new_rate = rockchip_saradc_set_clk(priv->cru, rate);
876 break;
David Wu01c60ea2018-01-13 14:06:33 +0800877 case PLL_GPLL:
878 case PLL_CPLL:
879 case PLL_NPLL:
880 case ACLK_CPU:
881 case HCLK_CPU:
882 case PCLK_CPU:
883 case ACLK_PERI:
884 case HCLK_PERI:
885 case PCLK_PERI:
886 case SCLK_UART0:
887 return 0;
Simon Glass99c15652015-08-30 16:55:31 -0600888 default:
889 return -ENOENT;
890 }
891
892 return new_rate;
893}
894
Philipp Tomsich75b381a2018-01-25 15:27:10 +0100895static int __maybe_unused rk3288_gmac_set_parent(struct clk *clk, struct clk *parent)
David Wu01c60ea2018-01-13 14:06:33 +0800896{
897 struct rk3288_clk_priv *priv = dev_get_priv(clk->dev);
Jagan Tekib52a1992020-01-09 14:22:17 +0530898 struct rockchip_cru *cru = priv->cru;
David Wu01c60ea2018-01-13 14:06:33 +0800899 const char *clock_output_name;
900 int ret;
901
902 /*
903 * If the requested parent is in the same clock-controller and
904 * the id is SCLK_MAC_PLL ("mac_pll_src"), switch to the internal
905 * clock.
906 */
907 if ((parent->dev == clk->dev) && (parent->id == SCLK_MAC_PLL)) {
908 debug("%s: switching GAMC to SCLK_MAC_PLL\n", __func__);
909 rk_clrsetreg(&cru->cru_clksel_con[21], RMII_EXTCLK_MASK, 0);
910 return 0;
911 }
912
913 /*
914 * Otherwise, we need to check the clock-output-names of the
915 * requested parent to see if the requested id is "ext_gmac".
916 */
917 ret = dev_read_string_index(parent->dev, "clock-output-names",
918 parent->id, &clock_output_name);
919 if (ret < 0)
920 return -ENODATA;
921
922 /* If this is "ext_gmac", switch to the external clock input */
923 if (!strcmp(clock_output_name, "ext_gmac")) {
924 debug("%s: switching GMAC to external clock\n", __func__);
925 rk_clrsetreg(&cru->cru_clksel_con[21], RMII_EXTCLK_MASK,
926 RMII_EXTCLK_SELECT_EXT_CLK << RMII_EXTCLK_SHIFT);
927 return 0;
928 }
929
930 return -EINVAL;
931}
932
Philipp Tomsich75b381a2018-01-25 15:27:10 +0100933static int __maybe_unused rk3288_clk_set_parent(struct clk *clk, struct clk *parent)
David Wu01c60ea2018-01-13 14:06:33 +0800934{
935 switch (clk->id) {
936 case SCLK_MAC:
937 return rk3288_gmac_set_parent(clk, parent);
938 case SCLK_USBPHY480M_SRC:
939 return 0;
940 }
941
942 debug("%s: unsupported clk %ld\n", __func__, clk->id);
943 return -ENOENT;
944}
945
Simon Glass99c15652015-08-30 16:55:31 -0600946static struct clk_ops rk3288_clk_ops = {
947 .get_rate = rk3288_clk_get_rate,
948 .set_rate = rk3288_clk_set_rate,
Philipp Tomsich75b381a2018-01-25 15:27:10 +0100949#if CONFIG_IS_ENABLED(OF_CONTROL) && !CONFIG_IS_ENABLED(OF_PLATDATA)
David Wu01c60ea2018-01-13 14:06:33 +0800950 .set_parent = rk3288_clk_set_parent,
Philipp Tomsich75b381a2018-01-25 15:27:10 +0100951#endif
Simon Glass99c15652015-08-30 16:55:31 -0600952};
953
Simon Glass08fd82c2016-07-04 11:58:28 -0600954static int rk3288_clk_ofdata_to_platdata(struct udevice *dev)
Simon Glass99c15652015-08-30 16:55:31 -0600955{
Simon Glass2d143bd2016-07-04 11:58:29 -0600956#if !CONFIG_IS_ENABLED(OF_PLATDATA)
Simon Glass99c15652015-08-30 16:55:31 -0600957 struct rk3288_clk_priv *priv = dev_get_priv(dev);
958
Kever Yang995cde12018-02-11 11:53:08 +0800959 priv->cru = dev_read_addr_ptr(dev);
Simon Glass2d143bd2016-07-04 11:58:29 -0600960#endif
Simon Glass08fd82c2016-07-04 11:58:28 -0600961
962 return 0;
963}
964
965static int rk3288_clk_probe(struct udevice *dev)
966{
967 struct rk3288_clk_priv *priv = dev_get_priv(dev);
Simon Glassd3cb46a2017-05-31 17:57:32 -0600968 bool init_clocks = false;
Simon Glass08fd82c2016-07-04 11:58:28 -0600969
Simon Glass99c15652015-08-30 16:55:31 -0600970 priv->grf = syscon_get_first_range(ROCKCHIP_SYSCON_GRF);
Simon Glass08fd82c2016-07-04 11:58:28 -0600971 if (IS_ERR(priv->grf))
972 return PTR_ERR(priv->grf);
Simon Glass99c15652015-08-30 16:55:31 -0600973#ifdef CONFIG_SPL_BUILD
Simon Glass2d143bd2016-07-04 11:58:29 -0600974#if CONFIG_IS_ENABLED(OF_PLATDATA)
975 struct rk3288_clk_plat *plat = dev_get_platdata(dev);
976
977 priv->cru = map_sysmem(plat->dtd.reg[0], plat->dtd.reg[1]);
978#endif
Simon Glassd3cb46a2017-05-31 17:57:32 -0600979 init_clocks = true;
Simon Glass99c15652015-08-30 16:55:31 -0600980#endif
Simon Glassd3cb46a2017-05-31 17:57:32 -0600981 if (!(gd->flags & GD_FLG_RELOC)) {
982 u32 reg;
983
984 /*
985 * Init clocks in U-Boot proper if the NPLL is runnning. This
986 * indicates that a previous boot loader set up the clocks, so
987 * we need to redo it. U-Boot's SPL does not set this clock.
988 */
989 reg = readl(&priv->cru->cru_mode_con);
990 if (((reg & NPLL_MODE_MASK) >> NPLL_MODE_SHIFT) ==
991 NPLL_MODE_NORMAL)
992 init_clocks = true;
993 }
994
995 if (init_clocks)
996 rkclk_init(priv->cru, priv->grf);
Simon Glass99c15652015-08-30 16:55:31 -0600997
998 return 0;
999}
1000
Simon Glass99c15652015-08-30 16:55:31 -06001001static int rk3288_clk_bind(struct udevice *dev)
1002{
Stephen Warren135aa952016-06-17 09:44:00 -06001003 int ret;
Kever Yangf24e36d2017-11-03 15:16:13 +08001004 struct udevice *sys_child;
1005 struct sysreset_reg *priv;
Simon Glass99c15652015-08-30 16:55:31 -06001006
1007 /* The reset driver does not have a device node, so bind it here */
Kever Yangf24e36d2017-11-03 15:16:13 +08001008 ret = device_bind_driver(dev, "rockchip_sysreset", "sysreset",
1009 &sys_child);
1010 if (ret) {
1011 debug("Warning: No sysreset driver: ret=%d\n", ret);
1012 } else {
1013 priv = malloc(sizeof(struct sysreset_reg));
Jagan Tekib52a1992020-01-09 14:22:17 +05301014 priv->glb_srst_fst_value = offsetof(struct rockchip_cru,
Kever Yangf24e36d2017-11-03 15:16:13 +08001015 cru_glb_srst_fst_value);
Jagan Tekib52a1992020-01-09 14:22:17 +05301016 priv->glb_srst_snd_value = offsetof(struct rockchip_cru,
Kever Yangf24e36d2017-11-03 15:16:13 +08001017 cru_glb_srst_snd_value);
1018 sys_child->priv = priv;
1019 }
Simon Glass99c15652015-08-30 16:55:31 -06001020
Heiko Stuebnera5ada252019-11-09 00:06:30 +01001021#if CONFIG_IS_ENABLED(RESET_ROCKCHIP)
Jagan Tekib52a1992020-01-09 14:22:17 +05301022 ret = offsetof(struct rockchip_cru, cru_softrst_con[0]);
Elaine Zhang538f67c2017-12-19 18:22:38 +08001023 ret = rockchip_reset_bind(dev, ret, 12);
1024 if (ret)
1025 debug("Warning: software reset driver bind faile\n");
1026#endif
1027
Simon Glass99c15652015-08-30 16:55:31 -06001028 return 0;
1029}
1030
1031static const struct udevice_id rk3288_clk_ids[] = {
1032 { .compatible = "rockchip,rk3288-cru" },
1033 { }
1034};
1035
Simon Glass2d143bd2016-07-04 11:58:29 -06001036U_BOOT_DRIVER(rockchip_rk3288_cru) = {
1037 .name = "rockchip_rk3288_cru",
Simon Glass99c15652015-08-30 16:55:31 -06001038 .id = UCLASS_CLK,
1039 .of_match = rk3288_clk_ids,
1040 .priv_auto_alloc_size = sizeof(struct rk3288_clk_priv),
Simon Glass2d143bd2016-07-04 11:58:29 -06001041 .platdata_auto_alloc_size = sizeof(struct rk3288_clk_plat),
Simon Glass99c15652015-08-30 16:55:31 -06001042 .ops = &rk3288_clk_ops,
1043 .bind = rk3288_clk_bind,
Simon Glass08fd82c2016-07-04 11:58:28 -06001044 .ofdata_to_platdata = rk3288_clk_ofdata_to_platdata,
Simon Glass99c15652015-08-30 16:55:31 -06001045 .probe = rk3288_clk_probe,
1046};