blob: baa8122bd6ddcb246a144699b1b84292aa82eee5 [file] [log] [blame]
Simon Glass99c15652015-08-30 16:55:31 -06001/*
2 * (C) Copyright 2015 Google, Inc
3 *
4 * SPDX-License-Identifier: GPL-2.0
5 */
6
7#include <common.h>
David Wuef4cf5a2017-09-20 14:28:19 +08008#include <bitfield.h>
Stephen Warren135aa952016-06-17 09:44:00 -06009#include <clk-uclass.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 Glass2d143bd2016-07-04 11:58:29 -060013#include <mapmem.h>
Simon Glass99c15652015-08-30 16:55:31 -060014#include <syscon.h>
15#include <asm/io.h>
16#include <asm/arch/clock.h>
17#include <asm/arch/cru_rk3288.h>
18#include <asm/arch/grf_rk3288.h>
19#include <asm/arch/hardware.h>
Simon Glass898d6432016-01-21 19:43:38 -070020#include <dt-bindings/clock/rk3288-cru.h>
Simon Glass64b7faa2016-01-21 19:43:41 -070021#include <dm/device-internal.h>
Simon Glass99c15652015-08-30 16:55:31 -060022#include <dm/lists.h>
Simon Glass64b7faa2016-01-21 19:43:41 -070023#include <dm/uclass-internal.h>
Heiko Stübnerabd01282016-07-22 23:51:06 +020024#include <linux/log2.h>
Simon Glass99c15652015-08-30 16:55:31 -060025
26DECLARE_GLOBAL_DATA_PTR;
27
Simon Glass2d143bd2016-07-04 11:58:29 -060028struct rk3288_clk_plat {
29#if CONFIG_IS_ENABLED(OF_PLATDATA)
30 struct dtd_rockchip_rk3288_cru dtd;
31#endif
32};
33
Simon Glass99c15652015-08-30 16:55:31 -060034struct pll_div {
35 u32 nr;
36 u32 nf;
37 u32 no;
38};
39
40enum {
41 VCO_MAX_HZ = 2200U * 1000000,
42 VCO_MIN_HZ = 440 * 1000000,
43 OUTPUT_MAX_HZ = 2200U * 1000000,
44 OUTPUT_MIN_HZ = 27500000,
45 FREF_MAX_HZ = 2200U * 1000000,
Heiko Stübnerc3f03ff2016-07-16 00:17:17 +020046 FREF_MIN_HZ = 269 * 1000,
Simon Glass99c15652015-08-30 16:55:31 -060047};
48
49enum {
50 /* PLL CON0 */
51 PLL_OD_MASK = 0x0f,
52
53 /* PLL CON1 */
54 PLL_NF_MASK = 0x1fff,
55
56 /* PLL CON2 */
57 PLL_BWADJ_MASK = 0x0fff,
58
59 /* PLL CON3 */
60 PLL_RESET_SHIFT = 5,
61
Simon Glassdae594f2016-01-21 19:45:17 -070062 /* CLKSEL0 */
Simon Glassdae594f2016-01-21 19:45:17 -070063 CORE_SEL_PLL_SHIFT = 15,
Simon Glassb223c1a2017-05-31 17:57:31 -060064 CORE_SEL_PLL_MASK = 1 << CORE_SEL_PLL_SHIFT,
Simon Glassdae594f2016-01-21 19:45:17 -070065 A17_DIV_SHIFT = 8,
Simon Glassb223c1a2017-05-31 17:57:31 -060066 A17_DIV_MASK = 0x1f << A17_DIV_SHIFT,
Simon Glassdae594f2016-01-21 19:45:17 -070067 MP_DIV_SHIFT = 4,
Simon Glassb223c1a2017-05-31 17:57:31 -060068 MP_DIV_MASK = 0xf << MP_DIV_SHIFT,
Simon Glassdae594f2016-01-21 19:45:17 -070069 M0_DIV_SHIFT = 0,
Simon Glassb223c1a2017-05-31 17:57:31 -060070 M0_DIV_MASK = 0xf << M0_DIV_SHIFT,
Simon Glassdae594f2016-01-21 19:45:17 -070071
Simon Glass99c15652015-08-30 16:55:31 -060072 /* CLKSEL1: pd bus clk pll sel: codec or general */
73 PD_BUS_SEL_PLL_MASK = 15,
74 PD_BUS_SEL_CPLL = 0,
75 PD_BUS_SEL_GPLL,
76
77 /* pd bus pclk div: pclk = pd_bus_aclk /(div + 1) */
78 PD_BUS_PCLK_DIV_SHIFT = 12,
Simon Glassb223c1a2017-05-31 17:57:31 -060079 PD_BUS_PCLK_DIV_MASK = 7 << PD_BUS_PCLK_DIV_SHIFT,
Simon Glass99c15652015-08-30 16:55:31 -060080
81 /* pd bus hclk div: aclk_bus: hclk_bus = 1:1 or 2:1 or 4:1 */
82 PD_BUS_HCLK_DIV_SHIFT = 8,
Simon Glassb223c1a2017-05-31 17:57:31 -060083 PD_BUS_HCLK_DIV_MASK = 3 << PD_BUS_HCLK_DIV_SHIFT,
Simon Glass99c15652015-08-30 16:55:31 -060084
85 /* pd bus aclk div: pd_bus_aclk = pd_bus_src_clk /(div0 * div1) */
86 PD_BUS_ACLK_DIV0_SHIFT = 3,
Simon Glassb223c1a2017-05-31 17:57:31 -060087 PD_BUS_ACLK_DIV0_MASK = 0x1f << PD_BUS_ACLK_DIV0_SHIFT,
Simon Glass99c15652015-08-30 16:55:31 -060088 PD_BUS_ACLK_DIV1_SHIFT = 0,
Simon Glassb223c1a2017-05-31 17:57:31 -060089 PD_BUS_ACLK_DIV1_MASK = 0x7 << PD_BUS_ACLK_DIV1_SHIFT,
Simon Glass99c15652015-08-30 16:55:31 -060090
91 /*
92 * CLKSEL10
93 * peripheral bus pclk div:
94 * aclk_bus: pclk_bus = 1:1 or 2:1 or 4:1 or 8:1
95 */
Simon Glassc87c1292016-01-21 19:45:15 -070096 PERI_SEL_PLL_SHIFT = 15,
Simon Glassb223c1a2017-05-31 17:57:31 -060097 PERI_SEL_PLL_MASK = 1 << PERI_SEL_PLL_SHIFT,
Simon Glassc87c1292016-01-21 19:45:15 -070098 PERI_SEL_CPLL = 0,
99 PERI_SEL_GPLL,
100
Simon Glass99c15652015-08-30 16:55:31 -0600101 PERI_PCLK_DIV_SHIFT = 12,
Simon Glassb223c1a2017-05-31 17:57:31 -0600102 PERI_PCLK_DIV_MASK = 3 << PERI_PCLK_DIV_SHIFT,
Simon Glass99c15652015-08-30 16:55:31 -0600103
104 /* peripheral bus hclk div: aclk_bus: hclk_bus = 1:1 or 2:1 or 4:1 */
105 PERI_HCLK_DIV_SHIFT = 8,
Simon Glassb223c1a2017-05-31 17:57:31 -0600106 PERI_HCLK_DIV_MASK = 3 << PERI_HCLK_DIV_SHIFT,
Simon Glass99c15652015-08-30 16:55:31 -0600107
108 /*
109 * peripheral bus aclk div:
110 * aclk_periph = periph_clk_src / (peri_aclk_div_con + 1)
111 */
112 PERI_ACLK_DIV_SHIFT = 0,
Simon Glassb223c1a2017-05-31 17:57:31 -0600113 PERI_ACLK_DIV_MASK = 0x1f << PERI_ACLK_DIV_SHIFT,
Simon Glass99c15652015-08-30 16:55:31 -0600114
David Wuef4cf5a2017-09-20 14:28:19 +0800115 /*
116 * CLKSEL24
117 * saradc_div_con:
118 * clk_saradc=24MHz/(saradc_div_con+1)
119 */
120 CLK_SARADC_DIV_CON_SHIFT = 8,
121 CLK_SARADC_DIV_CON_MASK = GENMASK(15, 8),
122 CLK_SARADC_DIV_CON_WIDTH = 8,
123
Simon Glass99c15652015-08-30 16:55:31 -0600124 SOCSTS_DPLL_LOCK = 1 << 5,
125 SOCSTS_APLL_LOCK = 1 << 6,
126 SOCSTS_CPLL_LOCK = 1 << 7,
127 SOCSTS_GPLL_LOCK = 1 << 8,
128 SOCSTS_NPLL_LOCK = 1 << 9,
129};
130
Simon Glass99c15652015-08-30 16:55:31 -0600131#define DIV_TO_RATE(input_rate, div) ((input_rate) / ((div) + 1))
132
133#define PLL_DIVISORS(hz, _nr, _no) {\
134 .nr = _nr, .nf = (u32)((u64)hz * _nr * _no / OSC_HZ), .no = _no};\
135 _Static_assert(((u64)hz * _nr * _no / OSC_HZ) * OSC_HZ /\
136 (_nr * _no) == hz, #hz "Hz cannot be hit with PLL "\
137 "divisors on line " __stringify(__LINE__));
138
139/* Keep divisors as low as possible to reduce jitter and power usage */
140static const struct pll_div apll_init_cfg = PLL_DIVISORS(APLL_HZ, 1, 1);
141static const struct pll_div gpll_init_cfg = PLL_DIVISORS(GPLL_HZ, 2, 2);
142static const struct pll_div cpll_init_cfg = PLL_DIVISORS(CPLL_HZ, 1, 2);
143
144static int rkclk_set_pll(struct rk3288_cru *cru, enum rk_clk_id clk_id,
145 const struct pll_div *div)
146{
147 int pll_id = rk_pll_id(clk_id);
148 struct rk3288_pll *pll = &cru->pll[pll_id];
149 /* All PLLs have same VCO and output frequency range restrictions. */
150 uint vco_hz = OSC_HZ / 1000 * div->nf / div->nr * 1000;
151 uint output_hz = vco_hz / div->no;
152
Simon Glassc87c1292016-01-21 19:45:15 -0700153 debug("PLL at %x: nf=%d, nr=%d, no=%d, vco=%u Hz, output=%u Hz\n",
154 (uint)pll, div->nf, div->nr, div->no, vco_hz, output_hz);
Simon Glass99c15652015-08-30 16:55:31 -0600155 assert(vco_hz >= VCO_MIN_HZ && vco_hz <= VCO_MAX_HZ &&
156 output_hz >= OUTPUT_MIN_HZ && output_hz <= OUTPUT_MAX_HZ &&
157 (div->no == 1 || !(div->no % 2)));
158
Simon Glassc87c1292016-01-21 19:45:15 -0700159 /* enter reset */
Simon Glass99c15652015-08-30 16:55:31 -0600160 rk_setreg(&pll->con3, 1 << PLL_RESET_SHIFT);
161
Simon Glassb223c1a2017-05-31 17:57:31 -0600162 rk_clrsetreg(&pll->con0, CLKR_MASK | PLL_OD_MASK,
Simon Glass99c15652015-08-30 16:55:31 -0600163 ((div->nr - 1) << CLKR_SHIFT) | (div->no - 1));
164 rk_clrsetreg(&pll->con1, CLKF_MASK, div->nf - 1);
165 rk_clrsetreg(&pll->con2, PLL_BWADJ_MASK, (div->nf >> 1) - 1);
166
167 udelay(10);
168
Simon Glassc87c1292016-01-21 19:45:15 -0700169 /* return from reset */
Simon Glass99c15652015-08-30 16:55:31 -0600170 rk_clrreg(&pll->con3, 1 << PLL_RESET_SHIFT);
171
172 return 0;
173}
174
Simon Glass99c15652015-08-30 16:55:31 -0600175static int rkclk_configure_ddr(struct rk3288_cru *cru, struct rk3288_grf *grf,
176 unsigned int hz)
177{
178 static const struct pll_div dpll_cfg[] = {
179 {.nf = 25, .nr = 2, .no = 1},
180 {.nf = 400, .nr = 9, .no = 2},
181 {.nf = 500, .nr = 9, .no = 2},
182 {.nf = 100, .nr = 3, .no = 1},
183 };
184 int cfg;
185
Simon Glass99c15652015-08-30 16:55:31 -0600186 switch (hz) {
187 case 300000000:
188 cfg = 0;
189 break;
190 case 533000000: /* actually 533.3P MHz */
191 cfg = 1;
192 break;
193 case 666000000: /* actually 666.6P MHz */
194 cfg = 2;
195 break;
196 case 800000000:
197 cfg = 3;
198 break;
199 default:
Simon Glassc87c1292016-01-21 19:45:15 -0700200 debug("Unsupported SDRAM frequency");
Simon Glass99c15652015-08-30 16:55:31 -0600201 return -EINVAL;
202 }
203
204 /* pll enter slow-mode */
Simon Glassb223c1a2017-05-31 17:57:31 -0600205 rk_clrsetreg(&cru->cru_mode_con, DPLL_MODE_MASK,
Simon Glass99c15652015-08-30 16:55:31 -0600206 DPLL_MODE_SLOW << DPLL_MODE_SHIFT);
207
208 rkclk_set_pll(cru, CLK_DDR, &dpll_cfg[cfg]);
209
210 /* wait for pll lock */
211 while (!(readl(&grf->soc_status[1]) & SOCSTS_DPLL_LOCK))
212 udelay(1);
213
214 /* PLL enter normal-mode */
Simon Glassb223c1a2017-05-31 17:57:31 -0600215 rk_clrsetreg(&cru->cru_mode_con, DPLL_MODE_MASK,
Simon Glass009741f2016-01-21 19:45:01 -0700216 DPLL_MODE_NORMAL << DPLL_MODE_SHIFT);
Simon Glass99c15652015-08-30 16:55:31 -0600217
218 return 0;
219}
220
Simon Glass830a6082016-01-21 19:45:02 -0700221#ifndef CONFIG_SPL_BUILD
222#define VCO_MAX_KHZ 2200000
223#define VCO_MIN_KHZ 440000
224#define FREF_MAX_KHZ 2200000
225#define FREF_MIN_KHZ 269
226
227static int pll_para_config(ulong freq_hz, struct pll_div *div, uint *ext_div)
228{
229 uint ref_khz = OSC_HZ / 1000, nr, nf = 0;
230 uint fref_khz;
231 uint diff_khz, best_diff_khz;
232 const uint max_nr = 1 << 6, max_nf = 1 << 12, max_no = 1 << 4;
233 uint vco_khz;
234 uint no = 1;
235 uint freq_khz = freq_hz / 1000;
236
237 if (!freq_hz) {
238 printf("%s: the frequency can not be 0 Hz\n", __func__);
239 return -EINVAL;
240 }
241
242 no = DIV_ROUND_UP(VCO_MIN_KHZ, freq_khz);
243 if (ext_div) {
244 *ext_div = DIV_ROUND_UP(no, max_no);
245 no = DIV_ROUND_UP(no, *ext_div);
246 }
247
248 /* only even divisors (and 1) are supported */
249 if (no > 1)
250 no = DIV_ROUND_UP(no, 2) * 2;
251
252 vco_khz = freq_khz * no;
253 if (ext_div)
254 vco_khz *= *ext_div;
255
256 if (vco_khz < VCO_MIN_KHZ || vco_khz > VCO_MAX_KHZ || no > max_no) {
257 printf("%s: Cannot find out a supported VCO for Frequency (%luHz).\n",
258 __func__, freq_hz);
259 return -1;
260 }
261
262 div->no = no;
263
264 best_diff_khz = vco_khz;
265 for (nr = 1; nr < max_nr && best_diff_khz; nr++) {
266 fref_khz = ref_khz / nr;
267 if (fref_khz < FREF_MIN_KHZ)
268 break;
269 if (fref_khz > FREF_MAX_KHZ)
270 continue;
271
272 nf = vco_khz / fref_khz;
273 if (nf >= max_nf)
274 continue;
275 diff_khz = vco_khz - nf * fref_khz;
276 if (nf + 1 < max_nf && diff_khz > fref_khz / 2) {
277 nf++;
278 diff_khz = fref_khz - diff_khz;
279 }
280
281 if (diff_khz >= best_diff_khz)
282 continue;
283
284 best_diff_khz = diff_khz;
285 div->nr = nr;
286 div->nf = nf;
287 }
288
289 if (best_diff_khz > 4 * 1000) {
290 printf("%s: Failed to match output frequency %lu, difference is %u Hz, exceed 4MHZ\n",
291 __func__, freq_hz, best_diff_khz * 1000);
292 return -EINVAL;
293 }
294
295 return 0;
296}
297
David Wu01c60ea2018-01-13 14:06:33 +0800298static int rockchip_mac_set_clk(struct rk3288_cru *cru, uint freq)
Sjoerd Simons0aefc0b2016-02-28 22:24:59 +0100299{
David Wu01c60ea2018-01-13 14:06:33 +0800300 ulong ret;
Sjoerd Simons0aefc0b2016-02-28 22:24:59 +0100301
David Wu01c60ea2018-01-13 14:06:33 +0800302 /*
303 * The gmac clock can be derived either from an external clock
304 * or can be generated from internally by a divider from SCLK_MAC.
305 */
306 if (readl(&cru->cru_clksel_con[21]) & RMII_EXTCLK_MASK) {
307 /* An external clock will always generate the right rate... */
308 ret = freq;
309 } else {
310 u32 con = readl(&cru->cru_clksel_con[21]);
311 ulong pll_rate;
312 u8 div;
313
314 if (((con >> EMAC_PLL_SHIFT) & EMAC_PLL_MASK) ==
315 EMAC_PLL_SELECT_GENERAL)
316 pll_rate = GPLL_HZ;
317 else if (((con >> EMAC_PLL_SHIFT) & EMAC_PLL_MASK) ==
318 EMAC_PLL_SELECT_CODEC)
319 pll_rate = CPLL_HZ;
320 else
321 pll_rate = NPLL_HZ;
322
323 div = DIV_ROUND_UP(pll_rate, freq) - 1;
324 if (div <= 0x1f)
325 rk_clrsetreg(&cru->cru_clksel_con[21], MAC_DIV_CON_MASK,
326 div << MAC_DIV_CON_SHIFT);
327 else
328 debug("Unsupported div for gmac:%d\n", div);
329
330 return DIV_TO_RATE(pll_rate, div);
331 }
332
333 return ret;
Sjoerd Simons0aefc0b2016-02-28 22:24:59 +0100334}
335
Simon Glass830a6082016-01-21 19:45:02 -0700336static int rockchip_vop_set_clk(struct rk3288_cru *cru, struct rk3288_grf *grf,
337 int periph, unsigned int rate_hz)
338{
339 struct pll_div npll_config = {0};
340 u32 lcdc_div;
341 int ret;
342
343 ret = pll_para_config(rate_hz, &npll_config, &lcdc_div);
344 if (ret)
345 return ret;
346
Simon Glassb223c1a2017-05-31 17:57:31 -0600347 rk_clrsetreg(&cru->cru_mode_con, NPLL_MODE_MASK,
Simon Glass830a6082016-01-21 19:45:02 -0700348 NPLL_MODE_SLOW << NPLL_MODE_SHIFT);
349 rkclk_set_pll(cru, CLK_NEW, &npll_config);
350
351 /* waiting for pll lock */
352 while (1) {
353 if (readl(&grf->soc_status[1]) & SOCSTS_NPLL_LOCK)
354 break;
355 udelay(1);
356 }
357
Simon Glassb223c1a2017-05-31 17:57:31 -0600358 rk_clrsetreg(&cru->cru_mode_con, NPLL_MODE_MASK,
Simon Glass830a6082016-01-21 19:45:02 -0700359 NPLL_MODE_NORMAL << NPLL_MODE_SHIFT);
360
361 /* vop dclk source clk: npll,dclk_div: 1 */
362 switch (periph) {
363 case DCLK_VOP0:
364 rk_clrsetreg(&cru->cru_clksel_con[27], 0xff << 8 | 3 << 0,
365 (lcdc_div - 1) << 8 | 2 << 0);
366 break;
367 case DCLK_VOP1:
368 rk_clrsetreg(&cru->cru_clksel_con[29], 0xff << 8 | 3 << 6,
369 (lcdc_div - 1) << 8 | 2 << 6);
370 break;
371 }
372
373 return 0;
374}
Simon Glassd3cb46a2017-05-31 17:57:32 -0600375#endif /* CONFIG_SPL_BUILD */
Simon Glass830a6082016-01-21 19:45:02 -0700376
Simon Glass99c15652015-08-30 16:55:31 -0600377static void rkclk_init(struct rk3288_cru *cru, struct rk3288_grf *grf)
378{
379 u32 aclk_div;
380 u32 hclk_div;
381 u32 pclk_div;
382
383 /* pll enter slow-mode */
384 rk_clrsetreg(&cru->cru_mode_con,
Simon Glassb223c1a2017-05-31 17:57:31 -0600385 GPLL_MODE_MASK | CPLL_MODE_MASK,
Simon Glass99c15652015-08-30 16:55:31 -0600386 GPLL_MODE_SLOW << GPLL_MODE_SHIFT |
387 CPLL_MODE_SLOW << CPLL_MODE_SHIFT);
388
389 /* init pll */
390 rkclk_set_pll(cru, CLK_GENERAL, &gpll_init_cfg);
391 rkclk_set_pll(cru, CLK_CODEC, &cpll_init_cfg);
392
393 /* waiting for pll lock */
394 while ((readl(&grf->soc_status[1]) &
395 (SOCSTS_CPLL_LOCK | SOCSTS_GPLL_LOCK)) !=
396 (SOCSTS_CPLL_LOCK | SOCSTS_GPLL_LOCK))
397 udelay(1);
398
399 /*
400 * pd_bus clock pll source selection and
401 * set up dependent divisors for PCLK/HCLK and ACLK clocks.
402 */
403 aclk_div = GPLL_HZ / PD_BUS_ACLK_HZ - 1;
404 assert((aclk_div + 1) * PD_BUS_ACLK_HZ == GPLL_HZ && aclk_div < 0x1f);
405 hclk_div = PD_BUS_ACLK_HZ / PD_BUS_HCLK_HZ - 1;
406 assert((hclk_div + 1) * PD_BUS_HCLK_HZ ==
407 PD_BUS_ACLK_HZ && (hclk_div < 0x4) && (hclk_div != 0x2));
408
409 pclk_div = PD_BUS_ACLK_HZ / PD_BUS_PCLK_HZ - 1;
410 assert((pclk_div + 1) * PD_BUS_PCLK_HZ ==
411 PD_BUS_ACLK_HZ && pclk_div < 0x7);
412
413 rk_clrsetreg(&cru->cru_clksel_con[1],
Simon Glassb223c1a2017-05-31 17:57:31 -0600414 PD_BUS_PCLK_DIV_MASK | PD_BUS_HCLK_DIV_MASK |
415 PD_BUS_ACLK_DIV0_MASK | PD_BUS_ACLK_DIV1_MASK,
Simon Glass99c15652015-08-30 16:55:31 -0600416 pclk_div << PD_BUS_PCLK_DIV_SHIFT |
417 hclk_div << PD_BUS_HCLK_DIV_SHIFT |
418 aclk_div << PD_BUS_ACLK_DIV0_SHIFT |
419 0 << 0);
420
421 /*
422 * peri clock pll source selection and
423 * set up dependent divisors for PCLK/HCLK and ACLK clocks.
424 */
425 aclk_div = GPLL_HZ / PERI_ACLK_HZ - 1;
426 assert((aclk_div + 1) * PERI_ACLK_HZ == GPLL_HZ && aclk_div < 0x1f);
427
Heiko Stübnerabd01282016-07-22 23:51:06 +0200428 hclk_div = ilog2(PERI_ACLK_HZ / PERI_HCLK_HZ);
Simon Glass99c15652015-08-30 16:55:31 -0600429 assert((1 << hclk_div) * PERI_HCLK_HZ ==
430 PERI_ACLK_HZ && (hclk_div < 0x4));
431
Heiko Stübnerabd01282016-07-22 23:51:06 +0200432 pclk_div = ilog2(PERI_ACLK_HZ / PERI_PCLK_HZ);
Simon Glass99c15652015-08-30 16:55:31 -0600433 assert((1 << pclk_div) * PERI_PCLK_HZ ==
434 PERI_ACLK_HZ && (pclk_div < 0x4));
435
436 rk_clrsetreg(&cru->cru_clksel_con[10],
Simon Glassb223c1a2017-05-31 17:57:31 -0600437 PERI_PCLK_DIV_MASK | PERI_HCLK_DIV_MASK |
438 PERI_ACLK_DIV_MASK,
Simon Glassc87c1292016-01-21 19:45:15 -0700439 PERI_SEL_GPLL << PERI_SEL_PLL_SHIFT |
Simon Glass99c15652015-08-30 16:55:31 -0600440 pclk_div << PERI_PCLK_DIV_SHIFT |
441 hclk_div << PERI_HCLK_DIV_SHIFT |
442 aclk_div << PERI_ACLK_DIV_SHIFT);
443
444 /* PLL enter normal-mode */
445 rk_clrsetreg(&cru->cru_mode_con,
Simon Glassb223c1a2017-05-31 17:57:31 -0600446 GPLL_MODE_MASK | CPLL_MODE_MASK,
Simon Glass009741f2016-01-21 19:45:01 -0700447 GPLL_MODE_NORMAL << GPLL_MODE_SHIFT |
448 CPLL_MODE_NORMAL << CPLL_MODE_SHIFT);
Simon Glass99c15652015-08-30 16:55:31 -0600449}
Simon Glass99c15652015-08-30 16:55:31 -0600450
Heiko Stübnerb339b5d2016-07-16 00:17:16 +0200451void rk3288_clk_configure_cpu(struct rk3288_cru *cru, struct rk3288_grf *grf)
Simon Glassdae594f2016-01-21 19:45:17 -0700452{
453 /* pll enter slow-mode */
Simon Glassb223c1a2017-05-31 17:57:31 -0600454 rk_clrsetreg(&cru->cru_mode_con, APLL_MODE_MASK,
Simon Glassdae594f2016-01-21 19:45:17 -0700455 APLL_MODE_SLOW << APLL_MODE_SHIFT);
456
457 rkclk_set_pll(cru, CLK_ARM, &apll_init_cfg);
458
459 /* waiting for pll lock */
460 while (!(readl(&grf->soc_status[1]) & SOCSTS_APLL_LOCK))
461 udelay(1);
462
463 /*
464 * core clock pll source selection and
465 * set up dependent divisors for MPAXI/M0AXI and ARM clocks.
466 * core clock select apll, apll clk = 1800MHz
467 * arm clk = 1800MHz, mpclk = 450MHz, m0clk = 900MHz
468 */
469 rk_clrsetreg(&cru->cru_clksel_con[0],
Simon Glassb223c1a2017-05-31 17:57:31 -0600470 CORE_SEL_PLL_MASK | A17_DIV_MASK | MP_DIV_MASK |
471 M0_DIV_MASK,
Simon Glassdae594f2016-01-21 19:45:17 -0700472 0 << A17_DIV_SHIFT |
473 3 << MP_DIV_SHIFT |
474 1 << M0_DIV_SHIFT);
475
476 /*
477 * set up dependent divisors for L2RAM/ATCLK and PCLK clocks.
478 * l2ramclk = 900MHz, atclk = 450MHz, pclk_dbg = 450MHz
479 */
480 rk_clrsetreg(&cru->cru_clksel_con[37],
Simon Glassb223c1a2017-05-31 17:57:31 -0600481 CLK_L2RAM_DIV_MASK | ATCLK_CORE_DIV_CON_MASK |
482 PCLK_CORE_DBG_DIV_MASK,
Simon Glassdae594f2016-01-21 19:45:17 -0700483 1 << CLK_L2RAM_DIV_SHIFT |
484 3 << ATCLK_CORE_DIV_CON_SHIFT |
485 3 << PCLK_CORE_DBG_DIV_SHIFT);
486
487 /* PLL enter normal-mode */
Simon Glassb223c1a2017-05-31 17:57:31 -0600488 rk_clrsetreg(&cru->cru_mode_con, APLL_MODE_MASK,
Simon Glassdae594f2016-01-21 19:45:17 -0700489 APLL_MODE_NORMAL << APLL_MODE_SHIFT);
490}
491
Simon Glass99c15652015-08-30 16:55:31 -0600492/* Get pll rate by id */
493static uint32_t rkclk_pll_get_rate(struct rk3288_cru *cru,
494 enum rk_clk_id clk_id)
495{
496 uint32_t nr, no, nf;
497 uint32_t con;
498 int pll_id = rk_pll_id(clk_id);
499 struct rk3288_pll *pll = &cru->pll[pll_id];
500 static u8 clk_shift[CLK_COUNT] = {
Simon Glass009741f2016-01-21 19:45:01 -0700501 0xff, APLL_MODE_SHIFT, DPLL_MODE_SHIFT, CPLL_MODE_SHIFT,
502 GPLL_MODE_SHIFT, NPLL_MODE_SHIFT
Simon Glass99c15652015-08-30 16:55:31 -0600503 };
504 uint shift;
505
506 con = readl(&cru->cru_mode_con);
507 shift = clk_shift[clk_id];
Simon Glassb223c1a2017-05-31 17:57:31 -0600508 switch ((con >> shift) & CRU_MODE_MASK) {
Simon Glass009741f2016-01-21 19:45:01 -0700509 case APLL_MODE_SLOW:
Simon Glass99c15652015-08-30 16:55:31 -0600510 return OSC_HZ;
Simon Glass009741f2016-01-21 19:45:01 -0700511 case APLL_MODE_NORMAL:
Simon Glass99c15652015-08-30 16:55:31 -0600512 /* normal mode */
513 con = readl(&pll->con0);
Simon Glassb223c1a2017-05-31 17:57:31 -0600514 no = ((con & CLKOD_MASK) >> CLKOD_SHIFT) + 1;
515 nr = ((con & CLKR_MASK) >> CLKR_SHIFT) + 1;
Simon Glass99c15652015-08-30 16:55:31 -0600516 con = readl(&pll->con1);
Simon Glassb223c1a2017-05-31 17:57:31 -0600517 nf = ((con & CLKF_MASK) >> CLKF_SHIFT) + 1;
Simon Glass99c15652015-08-30 16:55:31 -0600518
519 return (24 * nf / (nr * no)) * 1000000;
Simon Glass009741f2016-01-21 19:45:01 -0700520 case APLL_MODE_DEEP:
Simon Glass99c15652015-08-30 16:55:31 -0600521 default:
522 return 32768;
523 }
524}
525
Simon Glass542635a2016-01-21 19:43:39 -0700526static ulong rockchip_mmc_get_clk(struct rk3288_cru *cru, uint gclk_rate,
Simon Glass898d6432016-01-21 19:43:38 -0700527 int periph)
Simon Glass99c15652015-08-30 16:55:31 -0600528{
529 uint src_rate;
530 uint div, mux;
531 u32 con;
532
533 switch (periph) {
Simon Glass898d6432016-01-21 19:43:38 -0700534 case HCLK_EMMC:
Xu Ziyuan45112272017-04-16 17:44:45 +0800535 case SCLK_EMMC:
Simon Glass99c15652015-08-30 16:55:31 -0600536 con = readl(&cru->cru_clksel_con[12]);
Simon Glassb223c1a2017-05-31 17:57:31 -0600537 mux = (con & EMMC_PLL_MASK) >> EMMC_PLL_SHIFT;
538 div = (con & EMMC_DIV_MASK) >> EMMC_DIV_SHIFT;
Simon Glass99c15652015-08-30 16:55:31 -0600539 break;
Simon Glass898d6432016-01-21 19:43:38 -0700540 case HCLK_SDMMC:
Xu Ziyuan45112272017-04-16 17:44:45 +0800541 case SCLK_SDMMC:
Simon Glass898d6432016-01-21 19:43:38 -0700542 con = readl(&cru->cru_clksel_con[11]);
Simon Glassb223c1a2017-05-31 17:57:31 -0600543 mux = (con & MMC0_PLL_MASK) >> MMC0_PLL_SHIFT;
544 div = (con & MMC0_DIV_MASK) >> MMC0_DIV_SHIFT;
Simon Glass99c15652015-08-30 16:55:31 -0600545 break;
Simon Glass898d6432016-01-21 19:43:38 -0700546 case HCLK_SDIO0:
Xu Ziyuan45112272017-04-16 17:44:45 +0800547 case SCLK_SDIO0:
Simon Glass99c15652015-08-30 16:55:31 -0600548 con = readl(&cru->cru_clksel_con[12]);
Simon Glassb223c1a2017-05-31 17:57:31 -0600549 mux = (con & SDIO0_PLL_MASK) >> SDIO0_PLL_SHIFT;
550 div = (con & SDIO0_DIV_MASK) >> SDIO0_DIV_SHIFT;
Simon Glass99c15652015-08-30 16:55:31 -0600551 break;
552 default:
553 return -EINVAL;
554 }
555
Simon Glass542635a2016-01-21 19:43:39 -0700556 src_rate = mux == EMMC_PLL_SELECT_24MHZ ? OSC_HZ : gclk_rate;
Simon Glass99c15652015-08-30 16:55:31 -0600557 return DIV_TO_RATE(src_rate, div);
558}
559
Simon Glass542635a2016-01-21 19:43:39 -0700560static ulong rockchip_mmc_set_clk(struct rk3288_cru *cru, uint gclk_rate,
Simon Glass898d6432016-01-21 19:43:38 -0700561 int periph, uint freq)
Simon Glass99c15652015-08-30 16:55:31 -0600562{
563 int src_clk_div;
564 int mux;
565
Simon Glass542635a2016-01-21 19:43:39 -0700566 debug("%s: gclk_rate=%u\n", __func__, gclk_rate);
Kever Yang3a94d752017-07-27 12:54:01 +0800567 /* mmc clock default div 2 internal, need provide double in cru */
568 src_clk_div = DIV_ROUND_UP(gclk_rate / 2, freq);
Simon Glass99c15652015-08-30 16:55:31 -0600569
570 if (src_clk_div > 0x3f) {
Kever Yang3a94d752017-07-27 12:54:01 +0800571 src_clk_div = DIV_ROUND_UP(OSC_HZ / 2, freq);
Kever Yang217273c2017-07-27 12:54:02 +0800572 assert(src_clk_div < 0x40);
Simon Glass99c15652015-08-30 16:55:31 -0600573 mux = EMMC_PLL_SELECT_24MHZ;
574 assert((int)EMMC_PLL_SELECT_24MHZ ==
575 (int)MMC0_PLL_SELECT_24MHZ);
576 } else {
577 mux = EMMC_PLL_SELECT_GENERAL;
578 assert((int)EMMC_PLL_SELECT_GENERAL ==
579 (int)MMC0_PLL_SELECT_GENERAL);
580 }
581 switch (periph) {
Simon Glass898d6432016-01-21 19:43:38 -0700582 case HCLK_EMMC:
Xu Ziyuan45112272017-04-16 17:44:45 +0800583 case SCLK_EMMC:
Simon Glass99c15652015-08-30 16:55:31 -0600584 rk_clrsetreg(&cru->cru_clksel_con[12],
Simon Glassb223c1a2017-05-31 17:57:31 -0600585 EMMC_PLL_MASK | EMMC_DIV_MASK,
Simon Glass99c15652015-08-30 16:55:31 -0600586 mux << EMMC_PLL_SHIFT |
587 (src_clk_div - 1) << EMMC_DIV_SHIFT);
588 break;
Simon Glass898d6432016-01-21 19:43:38 -0700589 case HCLK_SDMMC:
Xu Ziyuan45112272017-04-16 17:44:45 +0800590 case SCLK_SDMMC:
Simon Glass99c15652015-08-30 16:55:31 -0600591 rk_clrsetreg(&cru->cru_clksel_con[11],
Simon Glassb223c1a2017-05-31 17:57:31 -0600592 MMC0_PLL_MASK | MMC0_DIV_MASK,
Simon Glass99c15652015-08-30 16:55:31 -0600593 mux << MMC0_PLL_SHIFT |
594 (src_clk_div - 1) << MMC0_DIV_SHIFT);
595 break;
Simon Glass898d6432016-01-21 19:43:38 -0700596 case HCLK_SDIO0:
Xu Ziyuan45112272017-04-16 17:44:45 +0800597 case SCLK_SDIO0:
Simon Glass99c15652015-08-30 16:55:31 -0600598 rk_clrsetreg(&cru->cru_clksel_con[12],
Simon Glassb223c1a2017-05-31 17:57:31 -0600599 SDIO0_PLL_MASK | SDIO0_DIV_MASK,
Simon Glass99c15652015-08-30 16:55:31 -0600600 mux << SDIO0_PLL_SHIFT |
601 (src_clk_div - 1) << SDIO0_DIV_SHIFT);
602 break;
603 default:
604 return -EINVAL;
605 }
606
Simon Glass542635a2016-01-21 19:43:39 -0700607 return rockchip_mmc_get_clk(cru, gclk_rate, periph);
Simon Glass99c15652015-08-30 16:55:31 -0600608}
609
Simon Glass542635a2016-01-21 19:43:39 -0700610static ulong rockchip_spi_get_clk(struct rk3288_cru *cru, uint gclk_rate,
Simon Glass898d6432016-01-21 19:43:38 -0700611 int periph)
Simon Glass99c15652015-08-30 16:55:31 -0600612{
613 uint div, mux;
614 u32 con;
615
616 switch (periph) {
Simon Glass898d6432016-01-21 19:43:38 -0700617 case SCLK_SPI0:
Simon Glass99c15652015-08-30 16:55:31 -0600618 con = readl(&cru->cru_clksel_con[25]);
Simon Glassb223c1a2017-05-31 17:57:31 -0600619 mux = (con & SPI0_PLL_MASK) >> SPI0_PLL_SHIFT;
620 div = (con & SPI0_DIV_MASK) >> SPI0_DIV_SHIFT;
Simon Glass99c15652015-08-30 16:55:31 -0600621 break;
Simon Glass898d6432016-01-21 19:43:38 -0700622 case SCLK_SPI1:
Simon Glass99c15652015-08-30 16:55:31 -0600623 con = readl(&cru->cru_clksel_con[25]);
Simon Glassb223c1a2017-05-31 17:57:31 -0600624 mux = (con & SPI1_PLL_MASK) >> SPI1_PLL_SHIFT;
625 div = (con & SPI1_DIV_MASK) >> SPI1_DIV_SHIFT;
Simon Glass99c15652015-08-30 16:55:31 -0600626 break;
Simon Glass898d6432016-01-21 19:43:38 -0700627 case SCLK_SPI2:
Simon Glass99c15652015-08-30 16:55:31 -0600628 con = readl(&cru->cru_clksel_con[39]);
Simon Glassb223c1a2017-05-31 17:57:31 -0600629 mux = (con & SPI2_PLL_MASK) >> SPI2_PLL_SHIFT;
630 div = (con & SPI2_DIV_MASK) >> SPI2_DIV_SHIFT;
Simon Glass99c15652015-08-30 16:55:31 -0600631 break;
632 default:
633 return -EINVAL;
634 }
635 assert(mux == SPI0_PLL_SELECT_GENERAL);
636
Simon Glass542635a2016-01-21 19:43:39 -0700637 return DIV_TO_RATE(gclk_rate, div);
Simon Glass99c15652015-08-30 16:55:31 -0600638}
639
Simon Glass542635a2016-01-21 19:43:39 -0700640static ulong rockchip_spi_set_clk(struct rk3288_cru *cru, uint gclk_rate,
Simon Glass898d6432016-01-21 19:43:38 -0700641 int periph, uint freq)
Simon Glass99c15652015-08-30 16:55:31 -0600642{
643 int src_clk_div;
644
Simon Glass542635a2016-01-21 19:43:39 -0700645 debug("%s: clk_general_rate=%u\n", __func__, gclk_rate);
Kever Yang217273c2017-07-27 12:54:02 +0800646 src_clk_div = DIV_ROUND_UP(gclk_rate, freq) - 1;
647 assert(src_clk_div < 128);
Simon Glass99c15652015-08-30 16:55:31 -0600648 switch (periph) {
Simon Glass898d6432016-01-21 19:43:38 -0700649 case SCLK_SPI0:
Simon Glass99c15652015-08-30 16:55:31 -0600650 rk_clrsetreg(&cru->cru_clksel_con[25],
Simon Glassb223c1a2017-05-31 17:57:31 -0600651 SPI0_PLL_MASK | SPI0_DIV_MASK,
Simon Glass99c15652015-08-30 16:55:31 -0600652 SPI0_PLL_SELECT_GENERAL << SPI0_PLL_SHIFT |
653 src_clk_div << SPI0_DIV_SHIFT);
654 break;
Simon Glass898d6432016-01-21 19:43:38 -0700655 case SCLK_SPI1:
Simon Glass99c15652015-08-30 16:55:31 -0600656 rk_clrsetreg(&cru->cru_clksel_con[25],
Simon Glassb223c1a2017-05-31 17:57:31 -0600657 SPI1_PLL_MASK | SPI1_DIV_MASK,
Simon Glass99c15652015-08-30 16:55:31 -0600658 SPI1_PLL_SELECT_GENERAL << SPI1_PLL_SHIFT |
659 src_clk_div << SPI1_DIV_SHIFT);
660 break;
Simon Glass898d6432016-01-21 19:43:38 -0700661 case SCLK_SPI2:
Simon Glass99c15652015-08-30 16:55:31 -0600662 rk_clrsetreg(&cru->cru_clksel_con[39],
Simon Glassb223c1a2017-05-31 17:57:31 -0600663 SPI2_PLL_MASK | SPI2_DIV_MASK,
Simon Glass99c15652015-08-30 16:55:31 -0600664 SPI2_PLL_SELECT_GENERAL << SPI2_PLL_SHIFT |
665 src_clk_div << SPI2_DIV_SHIFT);
666 break;
667 default:
668 return -EINVAL;
669 }
670
Simon Glass542635a2016-01-21 19:43:39 -0700671 return rockchip_spi_get_clk(cru, gclk_rate, periph);
Simon Glass99c15652015-08-30 16:55:31 -0600672}
673
David Wuef4cf5a2017-09-20 14:28:19 +0800674static ulong rockchip_saradc_get_clk(struct rk3288_cru *cru)
675{
676 u32 div, val;
677
678 val = readl(&cru->cru_clksel_con[24]);
679 div = bitfield_extract(val, CLK_SARADC_DIV_CON_SHIFT,
680 CLK_SARADC_DIV_CON_WIDTH);
681
682 return DIV_TO_RATE(OSC_HZ, div);
683}
684
685static ulong rockchip_saradc_set_clk(struct rk3288_cru *cru, uint hz)
686{
687 int src_clk_div;
688
689 src_clk_div = DIV_ROUND_UP(OSC_HZ, hz) - 1;
690 assert(src_clk_div < 128);
691
692 rk_clrsetreg(&cru->cru_clksel_con[24],
693 CLK_SARADC_DIV_CON_MASK,
694 src_clk_div << CLK_SARADC_DIV_CON_SHIFT);
695
696 return rockchip_saradc_get_clk(cru);
697}
698
Stephen Warren135aa952016-06-17 09:44:00 -0600699static ulong rk3288_clk_get_rate(struct clk *clk)
Simon Glass4f436732016-01-21 19:43:40 -0700700{
Stephen Warren135aa952016-06-17 09:44:00 -0600701 struct rk3288_clk_priv *priv = dev_get_priv(clk->dev);
Simon Glass4f436732016-01-21 19:43:40 -0700702 ulong new_rate, gclk_rate;
Simon Glass4f436732016-01-21 19:43:40 -0700703
Stephen Warren135aa952016-06-17 09:44:00 -0600704 gclk_rate = rkclk_pll_get_rate(priv->cru, CLK_GENERAL);
705 switch (clk->id) {
706 case 0 ... 63:
707 new_rate = rkclk_pll_get_rate(priv->cru, clk->id);
708 break;
Simon Glass4f436732016-01-21 19:43:40 -0700709 case HCLK_EMMC:
Simon Glass342999f2016-01-21 19:43:45 -0700710 case HCLK_SDMMC:
Simon Glass4f436732016-01-21 19:43:40 -0700711 case HCLK_SDIO0:
Xu Ziyuan45112272017-04-16 17:44:45 +0800712 case SCLK_EMMC:
713 case SCLK_SDMMC:
714 case SCLK_SDIO0:
Stephen Warren135aa952016-06-17 09:44:00 -0600715 new_rate = rockchip_mmc_get_clk(priv->cru, gclk_rate, clk->id);
Simon Glass4f436732016-01-21 19:43:40 -0700716 break;
717 case SCLK_SPI0:
718 case SCLK_SPI1:
719 case SCLK_SPI2:
Stephen Warren135aa952016-06-17 09:44:00 -0600720 new_rate = rockchip_spi_get_clk(priv->cru, gclk_rate, clk->id);
Simon Glass4f436732016-01-21 19:43:40 -0700721 break;
722 case PCLK_I2C0:
723 case PCLK_I2C1:
724 case PCLK_I2C2:
725 case PCLK_I2C3:
726 case PCLK_I2C4:
727 case PCLK_I2C5:
728 return gclk_rate;
Kever Yang4f0b8ef2016-08-12 17:57:05 +0800729 case PCLK_PWM:
730 return PD_BUS_PCLK_HZ;
David Wuef4cf5a2017-09-20 14:28:19 +0800731 case SCLK_SARADC:
732 new_rate = rockchip_saradc_get_clk(priv->cru);
733 break;
Simon Glass4f436732016-01-21 19:43:40 -0700734 default:
735 return -ENOENT;
736 }
737
738 return new_rate;
739}
740
Stephen Warren135aa952016-06-17 09:44:00 -0600741static ulong rk3288_clk_set_rate(struct clk *clk, ulong rate)
Simon Glass99c15652015-08-30 16:55:31 -0600742{
Stephen Warren135aa952016-06-17 09:44:00 -0600743 struct rk3288_clk_priv *priv = dev_get_priv(clk->dev);
Simon Glass830a6082016-01-21 19:45:02 -0700744 struct rk3288_cru *cru = priv->cru;
Simon Glass898d6432016-01-21 19:43:38 -0700745 ulong new_rate, gclk_rate;
Simon Glass99c15652015-08-30 16:55:31 -0600746
Stephen Warren135aa952016-06-17 09:44:00 -0600747 gclk_rate = rkclk_pll_get_rate(priv->cru, CLK_GENERAL);
748 switch (clk->id) {
Simon Glass3a8a42d2016-11-13 14:22:13 -0700749 case PLL_APLL:
750 /* We only support a fixed rate here */
751 if (rate != 1800000000)
752 return -EINVAL;
753 rk3288_clk_configure_cpu(priv->cru, priv->grf);
754 new_rate = rate;
755 break;
Stephen Warren135aa952016-06-17 09:44:00 -0600756 case CLK_DDR:
757 new_rate = rkclk_configure_ddr(priv->cru, priv->grf, rate);
758 break;
Simon Glass898d6432016-01-21 19:43:38 -0700759 case HCLK_EMMC:
760 case HCLK_SDMMC:
761 case HCLK_SDIO0:
Xu Ziyuan45112272017-04-16 17:44:45 +0800762 case SCLK_EMMC:
763 case SCLK_SDMMC:
764 case SCLK_SDIO0:
Stephen Warren135aa952016-06-17 09:44:00 -0600765 new_rate = rockchip_mmc_set_clk(cru, gclk_rate, clk->id, rate);
Simon Glass99c15652015-08-30 16:55:31 -0600766 break;
Simon Glass898d6432016-01-21 19:43:38 -0700767 case SCLK_SPI0:
768 case SCLK_SPI1:
769 case SCLK_SPI2:
Stephen Warren135aa952016-06-17 09:44:00 -0600770 new_rate = rockchip_spi_set_clk(cru, gclk_rate, clk->id, rate);
Simon Glass99c15652015-08-30 16:55:31 -0600771 break;
Simon Glass830a6082016-01-21 19:45:02 -0700772#ifndef CONFIG_SPL_BUILD
Sjoerd Simons0aefc0b2016-02-28 22:24:59 +0100773 case SCLK_MAC:
David Wu01c60ea2018-01-13 14:06:33 +0800774 new_rate = rockchip_mac_set_clk(priv->cru, rate);
Sjoerd Simons0aefc0b2016-02-28 22:24:59 +0100775 break;
Simon Glass830a6082016-01-21 19:45:02 -0700776 case DCLK_VOP0:
777 case DCLK_VOP1:
Stephen Warren135aa952016-06-17 09:44:00 -0600778 new_rate = rockchip_vop_set_clk(cru, priv->grf, clk->id, rate);
Simon Glass830a6082016-01-21 19:45:02 -0700779 break;
780 case SCLK_EDP_24M:
781 /* clk_edp_24M source: 24M */
782 rk_setreg(&cru->cru_clksel_con[28], 1 << 15);
783
784 /* rst edp */
785 rk_setreg(&cru->cru_clksel_con[6], 1 << 15);
786 udelay(1);
787 rk_clrreg(&cru->cru_clksel_con[6], 1 << 15);
788 new_rate = rate;
789 break;
790 case ACLK_VOP0:
791 case ACLK_VOP1: {
792 u32 div;
793
794 /* vop aclk source clk: cpll */
795 div = CPLL_HZ / rate;
796 assert((div - 1 < 64) && (div * rate == CPLL_HZ));
797
Stephen Warren135aa952016-06-17 09:44:00 -0600798 switch (clk->id) {
Simon Glass830a6082016-01-21 19:45:02 -0700799 case ACLK_VOP0:
800 rk_clrsetreg(&cru->cru_clksel_con[31],
801 3 << 6 | 0x1f << 0,
802 0 << 6 | (div - 1) << 0);
803 break;
804 case ACLK_VOP1:
805 rk_clrsetreg(&cru->cru_clksel_con[31],
806 3 << 14 | 0x1f << 8,
807 0 << 14 | (div - 1) << 8);
808 break;
809 }
810 new_rate = rate;
811 break;
812 }
813 case PCLK_HDMI_CTRL:
814 /* enable pclk hdmi ctrl */
815 rk_clrreg(&cru->cru_clkgate_con[16], 1 << 9);
816
817 /* software reset hdmi */
818 rk_setreg(&cru->cru_clkgate_con[7], 1 << 9);
819 udelay(1);
820 rk_clrreg(&cru->cru_clkgate_con[7], 1 << 9);
821 new_rate = rate;
822 break;
823#endif
David Wuef4cf5a2017-09-20 14:28:19 +0800824 case SCLK_SARADC:
825 new_rate = rockchip_saradc_set_clk(priv->cru, rate);
826 break;
David Wu01c60ea2018-01-13 14:06:33 +0800827 case PLL_GPLL:
828 case PLL_CPLL:
829 case PLL_NPLL:
830 case ACLK_CPU:
831 case HCLK_CPU:
832 case PCLK_CPU:
833 case ACLK_PERI:
834 case HCLK_PERI:
835 case PCLK_PERI:
836 case SCLK_UART0:
837 return 0;
Simon Glass99c15652015-08-30 16:55:31 -0600838 default:
839 return -ENOENT;
840 }
841
842 return new_rate;
843}
844
David Wu01c60ea2018-01-13 14:06:33 +0800845static int rk3288_gmac_set_parent(struct clk *clk, struct clk *parent)
846{
847 struct rk3288_clk_priv *priv = dev_get_priv(clk->dev);
848 struct rk3288_cru *cru = priv->cru;
849 const char *clock_output_name;
850 int ret;
851
852 /*
853 * If the requested parent is in the same clock-controller and
854 * the id is SCLK_MAC_PLL ("mac_pll_src"), switch to the internal
855 * clock.
856 */
857 if ((parent->dev == clk->dev) && (parent->id == SCLK_MAC_PLL)) {
858 debug("%s: switching GAMC to SCLK_MAC_PLL\n", __func__);
859 rk_clrsetreg(&cru->cru_clksel_con[21], RMII_EXTCLK_MASK, 0);
860 return 0;
861 }
862
863 /*
864 * Otherwise, we need to check the clock-output-names of the
865 * requested parent to see if the requested id is "ext_gmac".
866 */
867 ret = dev_read_string_index(parent->dev, "clock-output-names",
868 parent->id, &clock_output_name);
869 if (ret < 0)
870 return -ENODATA;
871
872 /* If this is "ext_gmac", switch to the external clock input */
873 if (!strcmp(clock_output_name, "ext_gmac")) {
874 debug("%s: switching GMAC to external clock\n", __func__);
875 rk_clrsetreg(&cru->cru_clksel_con[21], RMII_EXTCLK_MASK,
876 RMII_EXTCLK_SELECT_EXT_CLK << RMII_EXTCLK_SHIFT);
877 return 0;
878 }
879
880 return -EINVAL;
881}
882
883static int rk3288_clk_set_parent(struct clk *clk, struct clk *parent)
884{
885 switch (clk->id) {
886 case SCLK_MAC:
887 return rk3288_gmac_set_parent(clk, parent);
888 case SCLK_USBPHY480M_SRC:
889 return 0;
890 }
891
892 debug("%s: unsupported clk %ld\n", __func__, clk->id);
893 return -ENOENT;
894}
895
Simon Glass99c15652015-08-30 16:55:31 -0600896static struct clk_ops rk3288_clk_ops = {
897 .get_rate = rk3288_clk_get_rate,
898 .set_rate = rk3288_clk_set_rate,
David Wu01c60ea2018-01-13 14:06:33 +0800899 .set_parent = rk3288_clk_set_parent,
Simon Glass99c15652015-08-30 16:55:31 -0600900};
901
Simon Glass08fd82c2016-07-04 11:58:28 -0600902static int rk3288_clk_ofdata_to_platdata(struct udevice *dev)
Simon Glass99c15652015-08-30 16:55:31 -0600903{
Simon Glass2d143bd2016-07-04 11:58:29 -0600904#if !CONFIG_IS_ENABLED(OF_PLATDATA)
Simon Glass99c15652015-08-30 16:55:31 -0600905 struct rk3288_clk_priv *priv = dev_get_priv(dev);
906
Simon Glassa821c4a2017-05-17 17:18:05 -0600907 priv->cru = (struct rk3288_cru *)devfdt_get_addr(dev);
Simon Glass2d143bd2016-07-04 11:58:29 -0600908#endif
Simon Glass08fd82c2016-07-04 11:58:28 -0600909
910 return 0;
911}
912
913static int rk3288_clk_probe(struct udevice *dev)
914{
915 struct rk3288_clk_priv *priv = dev_get_priv(dev);
Simon Glassd3cb46a2017-05-31 17:57:32 -0600916 bool init_clocks = false;
Simon Glass08fd82c2016-07-04 11:58:28 -0600917
Simon Glass99c15652015-08-30 16:55:31 -0600918 priv->grf = syscon_get_first_range(ROCKCHIP_SYSCON_GRF);
Simon Glass08fd82c2016-07-04 11:58:28 -0600919 if (IS_ERR(priv->grf))
920 return PTR_ERR(priv->grf);
Simon Glass99c15652015-08-30 16:55:31 -0600921#ifdef CONFIG_SPL_BUILD
Simon Glass2d143bd2016-07-04 11:58:29 -0600922#if CONFIG_IS_ENABLED(OF_PLATDATA)
923 struct rk3288_clk_plat *plat = dev_get_platdata(dev);
924
925 priv->cru = map_sysmem(plat->dtd.reg[0], plat->dtd.reg[1]);
926#endif
Simon Glassd3cb46a2017-05-31 17:57:32 -0600927 init_clocks = true;
Simon Glass99c15652015-08-30 16:55:31 -0600928#endif
Simon Glassd3cb46a2017-05-31 17:57:32 -0600929 if (!(gd->flags & GD_FLG_RELOC)) {
930 u32 reg;
931
932 /*
933 * Init clocks in U-Boot proper if the NPLL is runnning. This
934 * indicates that a previous boot loader set up the clocks, so
935 * we need to redo it. U-Boot's SPL does not set this clock.
936 */
937 reg = readl(&priv->cru->cru_mode_con);
938 if (((reg & NPLL_MODE_MASK) >> NPLL_MODE_SHIFT) ==
939 NPLL_MODE_NORMAL)
940 init_clocks = true;
941 }
942
943 if (init_clocks)
944 rkclk_init(priv->cru, priv->grf);
Simon Glass99c15652015-08-30 16:55:31 -0600945
946 return 0;
947}
948
Simon Glass99c15652015-08-30 16:55:31 -0600949static int rk3288_clk_bind(struct udevice *dev)
950{
Stephen Warren135aa952016-06-17 09:44:00 -0600951 int ret;
Kever Yangf24e36d2017-11-03 15:16:13 +0800952 struct udevice *sys_child;
953 struct sysreset_reg *priv;
Simon Glass99c15652015-08-30 16:55:31 -0600954
955 /* The reset driver does not have a device node, so bind it here */
Kever Yangf24e36d2017-11-03 15:16:13 +0800956 ret = device_bind_driver(dev, "rockchip_sysreset", "sysreset",
957 &sys_child);
958 if (ret) {
959 debug("Warning: No sysreset driver: ret=%d\n", ret);
960 } else {
961 priv = malloc(sizeof(struct sysreset_reg));
962 priv->glb_srst_fst_value = offsetof(struct rk3288_cru,
963 cru_glb_srst_fst_value);
964 priv->glb_srst_snd_value = offsetof(struct rk3288_cru,
965 cru_glb_srst_snd_value);
966 sys_child->priv = priv;
967 }
Simon Glass99c15652015-08-30 16:55:31 -0600968
Elaine Zhang538f67c2017-12-19 18:22:38 +0800969#if CONFIG_IS_ENABLED(CONFIG_RESET_ROCKCHIP)
970 ret = offsetof(struct rk3288_cru, cru_softrst_con[0]);
971 ret = rockchip_reset_bind(dev, ret, 12);
972 if (ret)
973 debug("Warning: software reset driver bind faile\n");
974#endif
975
Simon Glass99c15652015-08-30 16:55:31 -0600976 return 0;
977}
978
979static const struct udevice_id rk3288_clk_ids[] = {
980 { .compatible = "rockchip,rk3288-cru" },
981 { }
982};
983
Simon Glass2d143bd2016-07-04 11:58:29 -0600984U_BOOT_DRIVER(rockchip_rk3288_cru) = {
985 .name = "rockchip_rk3288_cru",
Simon Glass99c15652015-08-30 16:55:31 -0600986 .id = UCLASS_CLK,
987 .of_match = rk3288_clk_ids,
988 .priv_auto_alloc_size = sizeof(struct rk3288_clk_priv),
Simon Glass2d143bd2016-07-04 11:58:29 -0600989 .platdata_auto_alloc_size = sizeof(struct rk3288_clk_plat),
Simon Glass99c15652015-08-30 16:55:31 -0600990 .ops = &rk3288_clk_ops,
991 .bind = rk3288_clk_bind,
Simon Glass08fd82c2016-07-04 11:58:28 -0600992 .ofdata_to_platdata = rk3288_clk_ofdata_to_platdata,
Simon Glass99c15652015-08-30 16:55:31 -0600993 .probe = rk3288_clk_probe,
994};