blob: 478195b10b0eb6175b86a8a0b599c1011af3671e [file] [log] [blame]
Simon Glass421358c2015-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>
Stephen Warrena9622432016-06-17 09:44:00 -06008#include <clk-uclass.h>
Simon Glass421358c2015-08-30 16:55:31 -06009#include <dm.h>
Simon Glass00c5fd42016-07-04 11:58:29 -060010#include <dt-structs.h>
Simon Glass421358c2015-08-30 16:55:31 -060011#include <errno.h>
Simon Glass00c5fd42016-07-04 11:58:29 -060012#include <mapmem.h>
Simon Glass421358c2015-08-30 16:55:31 -060013#include <syscon.h>
14#include <asm/io.h>
15#include <asm/arch/clock.h>
16#include <asm/arch/cru_rk3288.h>
17#include <asm/arch/grf_rk3288.h>
18#include <asm/arch/hardware.h>
Simon Glass8d32f4b2016-01-21 19:43:38 -070019#include <dt-bindings/clock/rk3288-cru.h>
Simon Glass344f3662016-01-21 19:43:41 -070020#include <dm/device-internal.h>
Simon Glass421358c2015-08-30 16:55:31 -060021#include <dm/lists.h>
Simon Glass344f3662016-01-21 19:43:41 -070022#include <dm/uclass-internal.h>
Heiko Stübner1b7dcc32016-07-22 23:51:06 +020023#include <linux/log2.h>
Simon Glass421358c2015-08-30 16:55:31 -060024
25DECLARE_GLOBAL_DATA_PTR;
26
Simon Glass00c5fd42016-07-04 11:58:29 -060027struct rk3288_clk_plat {
28#if CONFIG_IS_ENABLED(OF_PLATDATA)
29 struct dtd_rockchip_rk3288_cru dtd;
30#endif
31};
32
Simon Glass421358c2015-08-30 16:55:31 -060033struct pll_div {
34 u32 nr;
35 u32 nf;
36 u32 no;
37};
38
39enum {
40 VCO_MAX_HZ = 2200U * 1000000,
41 VCO_MIN_HZ = 440 * 1000000,
42 OUTPUT_MAX_HZ = 2200U * 1000000,
43 OUTPUT_MIN_HZ = 27500000,
44 FREF_MAX_HZ = 2200U * 1000000,
Heiko Stübner7f78c242016-07-16 00:17:17 +020045 FREF_MIN_HZ = 269 * 1000,
Simon Glass421358c2015-08-30 16:55:31 -060046};
47
48enum {
49 /* PLL CON0 */
50 PLL_OD_MASK = 0x0f,
51
52 /* PLL CON1 */
53 PLL_NF_MASK = 0x1fff,
54
55 /* PLL CON2 */
56 PLL_BWADJ_MASK = 0x0fff,
57
58 /* PLL CON3 */
59 PLL_RESET_SHIFT = 5,
60
Simon Glass94906e42016-01-21 19:45:17 -070061 /* CLKSEL0 */
Simon Glass94906e42016-01-21 19:45:17 -070062 CORE_SEL_PLL_SHIFT = 15,
Simon Glass303384f2017-05-31 17:57:31 -060063 CORE_SEL_PLL_MASK = 1 << CORE_SEL_PLL_SHIFT,
Simon Glass94906e42016-01-21 19:45:17 -070064 A17_DIV_SHIFT = 8,
Simon Glass303384f2017-05-31 17:57:31 -060065 A17_DIV_MASK = 0x1f << A17_DIV_SHIFT,
Simon Glass94906e42016-01-21 19:45:17 -070066 MP_DIV_SHIFT = 4,
Simon Glass303384f2017-05-31 17:57:31 -060067 MP_DIV_MASK = 0xf << MP_DIV_SHIFT,
Simon Glass94906e42016-01-21 19:45:17 -070068 M0_DIV_SHIFT = 0,
Simon Glass303384f2017-05-31 17:57:31 -060069 M0_DIV_MASK = 0xf << M0_DIV_SHIFT,
Simon Glass94906e42016-01-21 19:45:17 -070070
Simon Glass421358c2015-08-30 16:55:31 -060071 /* CLKSEL1: pd bus clk pll sel: codec or general */
72 PD_BUS_SEL_PLL_MASK = 15,
73 PD_BUS_SEL_CPLL = 0,
74 PD_BUS_SEL_GPLL,
75
76 /* pd bus pclk div: pclk = pd_bus_aclk /(div + 1) */
77 PD_BUS_PCLK_DIV_SHIFT = 12,
Simon Glass303384f2017-05-31 17:57:31 -060078 PD_BUS_PCLK_DIV_MASK = 7 << PD_BUS_PCLK_DIV_SHIFT,
Simon Glass421358c2015-08-30 16:55:31 -060079
80 /* pd bus hclk div: aclk_bus: hclk_bus = 1:1 or 2:1 or 4:1 */
81 PD_BUS_HCLK_DIV_SHIFT = 8,
Simon Glass303384f2017-05-31 17:57:31 -060082 PD_BUS_HCLK_DIV_MASK = 3 << PD_BUS_HCLK_DIV_SHIFT,
Simon Glass421358c2015-08-30 16:55:31 -060083
84 /* pd bus aclk div: pd_bus_aclk = pd_bus_src_clk /(div0 * div1) */
85 PD_BUS_ACLK_DIV0_SHIFT = 3,
Simon Glass303384f2017-05-31 17:57:31 -060086 PD_BUS_ACLK_DIV0_MASK = 0x1f << PD_BUS_ACLK_DIV0_SHIFT,
Simon Glass421358c2015-08-30 16:55:31 -060087 PD_BUS_ACLK_DIV1_SHIFT = 0,
Simon Glass303384f2017-05-31 17:57:31 -060088 PD_BUS_ACLK_DIV1_MASK = 0x7 << PD_BUS_ACLK_DIV1_SHIFT,
Simon Glass421358c2015-08-30 16:55:31 -060089
90 /*
91 * CLKSEL10
92 * peripheral bus pclk div:
93 * aclk_bus: pclk_bus = 1:1 or 2:1 or 4:1 or 8:1
94 */
Simon Glasse6a682b2016-01-21 19:45:15 -070095 PERI_SEL_PLL_SHIFT = 15,
Simon Glass303384f2017-05-31 17:57:31 -060096 PERI_SEL_PLL_MASK = 1 << PERI_SEL_PLL_SHIFT,
Simon Glasse6a682b2016-01-21 19:45:15 -070097 PERI_SEL_CPLL = 0,
98 PERI_SEL_GPLL,
99
Simon Glass421358c2015-08-30 16:55:31 -0600100 PERI_PCLK_DIV_SHIFT = 12,
Simon Glass303384f2017-05-31 17:57:31 -0600101 PERI_PCLK_DIV_MASK = 3 << PERI_PCLK_DIV_SHIFT,
Simon Glass421358c2015-08-30 16:55:31 -0600102
103 /* peripheral bus hclk div: aclk_bus: hclk_bus = 1:1 or 2:1 or 4:1 */
104 PERI_HCLK_DIV_SHIFT = 8,
Simon Glass303384f2017-05-31 17:57:31 -0600105 PERI_HCLK_DIV_MASK = 3 << PERI_HCLK_DIV_SHIFT,
Simon Glass421358c2015-08-30 16:55:31 -0600106
107 /*
108 * peripheral bus aclk div:
109 * aclk_periph = periph_clk_src / (peri_aclk_div_con + 1)
110 */
111 PERI_ACLK_DIV_SHIFT = 0,
Simon Glass303384f2017-05-31 17:57:31 -0600112 PERI_ACLK_DIV_MASK = 0x1f << PERI_ACLK_DIV_SHIFT,
Simon Glass421358c2015-08-30 16:55:31 -0600113
Simon Glass421358c2015-08-30 16:55:31 -0600114 SOCSTS_DPLL_LOCK = 1 << 5,
115 SOCSTS_APLL_LOCK = 1 << 6,
116 SOCSTS_CPLL_LOCK = 1 << 7,
117 SOCSTS_GPLL_LOCK = 1 << 8,
118 SOCSTS_NPLL_LOCK = 1 << 9,
119};
120
Simon Glass421358c2015-08-30 16:55:31 -0600121#define DIV_TO_RATE(input_rate, div) ((input_rate) / ((div) + 1))
122
123#define PLL_DIVISORS(hz, _nr, _no) {\
124 .nr = _nr, .nf = (u32)((u64)hz * _nr * _no / OSC_HZ), .no = _no};\
125 _Static_assert(((u64)hz * _nr * _no / OSC_HZ) * OSC_HZ /\
126 (_nr * _no) == hz, #hz "Hz cannot be hit with PLL "\
127 "divisors on line " __stringify(__LINE__));
128
129/* Keep divisors as low as possible to reduce jitter and power usage */
130static const struct pll_div apll_init_cfg = PLL_DIVISORS(APLL_HZ, 1, 1);
131static const struct pll_div gpll_init_cfg = PLL_DIVISORS(GPLL_HZ, 2, 2);
132static const struct pll_div cpll_init_cfg = PLL_DIVISORS(CPLL_HZ, 1, 2);
133
134static int rkclk_set_pll(struct rk3288_cru *cru, enum rk_clk_id clk_id,
135 const struct pll_div *div)
136{
137 int pll_id = rk_pll_id(clk_id);
138 struct rk3288_pll *pll = &cru->pll[pll_id];
139 /* All PLLs have same VCO and output frequency range restrictions. */
140 uint vco_hz = OSC_HZ / 1000 * div->nf / div->nr * 1000;
141 uint output_hz = vco_hz / div->no;
142
Simon Glasse6a682b2016-01-21 19:45:15 -0700143 debug("PLL at %x: nf=%d, nr=%d, no=%d, vco=%u Hz, output=%u Hz\n",
144 (uint)pll, div->nf, div->nr, div->no, vco_hz, output_hz);
Simon Glass421358c2015-08-30 16:55:31 -0600145 assert(vco_hz >= VCO_MIN_HZ && vco_hz <= VCO_MAX_HZ &&
146 output_hz >= OUTPUT_MIN_HZ && output_hz <= OUTPUT_MAX_HZ &&
147 (div->no == 1 || !(div->no % 2)));
148
Simon Glasse6a682b2016-01-21 19:45:15 -0700149 /* enter reset */
Simon Glass421358c2015-08-30 16:55:31 -0600150 rk_setreg(&pll->con3, 1 << PLL_RESET_SHIFT);
151
Simon Glass303384f2017-05-31 17:57:31 -0600152 rk_clrsetreg(&pll->con0, CLKR_MASK | PLL_OD_MASK,
Simon Glass421358c2015-08-30 16:55:31 -0600153 ((div->nr - 1) << CLKR_SHIFT) | (div->no - 1));
154 rk_clrsetreg(&pll->con1, CLKF_MASK, div->nf - 1);
155 rk_clrsetreg(&pll->con2, PLL_BWADJ_MASK, (div->nf >> 1) - 1);
156
157 udelay(10);
158
Simon Glasse6a682b2016-01-21 19:45:15 -0700159 /* return from reset */
Simon Glass421358c2015-08-30 16:55:31 -0600160 rk_clrreg(&pll->con3, 1 << PLL_RESET_SHIFT);
161
162 return 0;
163}
164
Simon Glass421358c2015-08-30 16:55:31 -0600165static int rkclk_configure_ddr(struct rk3288_cru *cru, struct rk3288_grf *grf,
166 unsigned int hz)
167{
168 static const struct pll_div dpll_cfg[] = {
169 {.nf = 25, .nr = 2, .no = 1},
170 {.nf = 400, .nr = 9, .no = 2},
171 {.nf = 500, .nr = 9, .no = 2},
172 {.nf = 100, .nr = 3, .no = 1},
173 };
174 int cfg;
175
Simon Glass421358c2015-08-30 16:55:31 -0600176 switch (hz) {
177 case 300000000:
178 cfg = 0;
179 break;
180 case 533000000: /* actually 533.3P MHz */
181 cfg = 1;
182 break;
183 case 666000000: /* actually 666.6P MHz */
184 cfg = 2;
185 break;
186 case 800000000:
187 cfg = 3;
188 break;
189 default:
Simon Glasse6a682b2016-01-21 19:45:15 -0700190 debug("Unsupported SDRAM frequency");
Simon Glass421358c2015-08-30 16:55:31 -0600191 return -EINVAL;
192 }
193
194 /* pll enter slow-mode */
Simon Glass303384f2017-05-31 17:57:31 -0600195 rk_clrsetreg(&cru->cru_mode_con, DPLL_MODE_MASK,
Simon Glass421358c2015-08-30 16:55:31 -0600196 DPLL_MODE_SLOW << DPLL_MODE_SHIFT);
197
198 rkclk_set_pll(cru, CLK_DDR, &dpll_cfg[cfg]);
199
200 /* wait for pll lock */
201 while (!(readl(&grf->soc_status[1]) & SOCSTS_DPLL_LOCK))
202 udelay(1);
203
204 /* PLL enter normal-mode */
Simon Glass303384f2017-05-31 17:57:31 -0600205 rk_clrsetreg(&cru->cru_mode_con, DPLL_MODE_MASK,
Simon Glass5562bf12016-01-21 19:45:01 -0700206 DPLL_MODE_NORMAL << DPLL_MODE_SHIFT);
Simon Glass421358c2015-08-30 16:55:31 -0600207
208 return 0;
209}
210
Simon Glass273afb22016-01-21 19:45:02 -0700211#ifndef CONFIG_SPL_BUILD
212#define VCO_MAX_KHZ 2200000
213#define VCO_MIN_KHZ 440000
214#define FREF_MAX_KHZ 2200000
215#define FREF_MIN_KHZ 269
216
217static int pll_para_config(ulong freq_hz, struct pll_div *div, uint *ext_div)
218{
219 uint ref_khz = OSC_HZ / 1000, nr, nf = 0;
220 uint fref_khz;
221 uint diff_khz, best_diff_khz;
222 const uint max_nr = 1 << 6, max_nf = 1 << 12, max_no = 1 << 4;
223 uint vco_khz;
224 uint no = 1;
225 uint freq_khz = freq_hz / 1000;
226
227 if (!freq_hz) {
228 printf("%s: the frequency can not be 0 Hz\n", __func__);
229 return -EINVAL;
230 }
231
232 no = DIV_ROUND_UP(VCO_MIN_KHZ, freq_khz);
233 if (ext_div) {
234 *ext_div = DIV_ROUND_UP(no, max_no);
235 no = DIV_ROUND_UP(no, *ext_div);
236 }
237
238 /* only even divisors (and 1) are supported */
239 if (no > 1)
240 no = DIV_ROUND_UP(no, 2) * 2;
241
242 vco_khz = freq_khz * no;
243 if (ext_div)
244 vco_khz *= *ext_div;
245
246 if (vco_khz < VCO_MIN_KHZ || vco_khz > VCO_MAX_KHZ || no > max_no) {
247 printf("%s: Cannot find out a supported VCO for Frequency (%luHz).\n",
248 __func__, freq_hz);
249 return -1;
250 }
251
252 div->no = no;
253
254 best_diff_khz = vco_khz;
255 for (nr = 1; nr < max_nr && best_diff_khz; nr++) {
256 fref_khz = ref_khz / nr;
257 if (fref_khz < FREF_MIN_KHZ)
258 break;
259 if (fref_khz > FREF_MAX_KHZ)
260 continue;
261
262 nf = vco_khz / fref_khz;
263 if (nf >= max_nf)
264 continue;
265 diff_khz = vco_khz - nf * fref_khz;
266 if (nf + 1 < max_nf && diff_khz > fref_khz / 2) {
267 nf++;
268 diff_khz = fref_khz - diff_khz;
269 }
270
271 if (diff_khz >= best_diff_khz)
272 continue;
273
274 best_diff_khz = diff_khz;
275 div->nr = nr;
276 div->nf = nf;
277 }
278
279 if (best_diff_khz > 4 * 1000) {
280 printf("%s: Failed to match output frequency %lu, difference is %u Hz, exceed 4MHZ\n",
281 __func__, freq_hz, best_diff_khz * 1000);
282 return -EINVAL;
283 }
284
285 return 0;
286}
287
Sjoerd Simons3ce69bf2016-02-28 22:24:59 +0100288static int rockchip_mac_set_clk(struct rk3288_cru *cru,
289 int periph, uint freq)
290{
291 /* Assuming mac_clk is fed by an external clock */
292 rk_clrsetreg(&cru->cru_clksel_con[21],
Simon Glass303384f2017-05-31 17:57:31 -0600293 RMII_EXTCLK_MASK,
Sjoerd Simons3ce69bf2016-02-28 22:24:59 +0100294 RMII_EXTCLK_SELECT_EXT_CLK << RMII_EXTCLK_SHIFT);
295
296 return 0;
297}
298
Simon Glass273afb22016-01-21 19:45:02 -0700299static int rockchip_vop_set_clk(struct rk3288_cru *cru, struct rk3288_grf *grf,
300 int periph, unsigned int rate_hz)
301{
302 struct pll_div npll_config = {0};
303 u32 lcdc_div;
304 int ret;
305
306 ret = pll_para_config(rate_hz, &npll_config, &lcdc_div);
307 if (ret)
308 return ret;
309
Simon Glass303384f2017-05-31 17:57:31 -0600310 rk_clrsetreg(&cru->cru_mode_con, NPLL_MODE_MASK,
Simon Glass273afb22016-01-21 19:45:02 -0700311 NPLL_MODE_SLOW << NPLL_MODE_SHIFT);
312 rkclk_set_pll(cru, CLK_NEW, &npll_config);
313
314 /* waiting for pll lock */
315 while (1) {
316 if (readl(&grf->soc_status[1]) & SOCSTS_NPLL_LOCK)
317 break;
318 udelay(1);
319 }
320
Simon Glass303384f2017-05-31 17:57:31 -0600321 rk_clrsetreg(&cru->cru_mode_con, NPLL_MODE_MASK,
Simon Glass273afb22016-01-21 19:45:02 -0700322 NPLL_MODE_NORMAL << NPLL_MODE_SHIFT);
323
324 /* vop dclk source clk: npll,dclk_div: 1 */
325 switch (periph) {
326 case DCLK_VOP0:
327 rk_clrsetreg(&cru->cru_clksel_con[27], 0xff << 8 | 3 << 0,
328 (lcdc_div - 1) << 8 | 2 << 0);
329 break;
330 case DCLK_VOP1:
331 rk_clrsetreg(&cru->cru_clksel_con[29], 0xff << 8 | 3 << 6,
332 (lcdc_div - 1) << 8 | 2 << 6);
333 break;
334 }
335
336 return 0;
337}
Simon Glass30ca6a42017-05-31 17:57:32 -0600338#endif /* CONFIG_SPL_BUILD */
Simon Glass273afb22016-01-21 19:45:02 -0700339
Simon Glass421358c2015-08-30 16:55:31 -0600340static void rkclk_init(struct rk3288_cru *cru, struct rk3288_grf *grf)
341{
342 u32 aclk_div;
343 u32 hclk_div;
344 u32 pclk_div;
345
346 /* pll enter slow-mode */
347 rk_clrsetreg(&cru->cru_mode_con,
Simon Glass303384f2017-05-31 17:57:31 -0600348 GPLL_MODE_MASK | CPLL_MODE_MASK,
Simon Glass421358c2015-08-30 16:55:31 -0600349 GPLL_MODE_SLOW << GPLL_MODE_SHIFT |
350 CPLL_MODE_SLOW << CPLL_MODE_SHIFT);
351
352 /* init pll */
353 rkclk_set_pll(cru, CLK_GENERAL, &gpll_init_cfg);
354 rkclk_set_pll(cru, CLK_CODEC, &cpll_init_cfg);
355
356 /* waiting for pll lock */
357 while ((readl(&grf->soc_status[1]) &
358 (SOCSTS_CPLL_LOCK | SOCSTS_GPLL_LOCK)) !=
359 (SOCSTS_CPLL_LOCK | SOCSTS_GPLL_LOCK))
360 udelay(1);
361
362 /*
363 * pd_bus clock pll source selection and
364 * set up dependent divisors for PCLK/HCLK and ACLK clocks.
365 */
366 aclk_div = GPLL_HZ / PD_BUS_ACLK_HZ - 1;
367 assert((aclk_div + 1) * PD_BUS_ACLK_HZ == GPLL_HZ && aclk_div < 0x1f);
368 hclk_div = PD_BUS_ACLK_HZ / PD_BUS_HCLK_HZ - 1;
369 assert((hclk_div + 1) * PD_BUS_HCLK_HZ ==
370 PD_BUS_ACLK_HZ && (hclk_div < 0x4) && (hclk_div != 0x2));
371
372 pclk_div = PD_BUS_ACLK_HZ / PD_BUS_PCLK_HZ - 1;
373 assert((pclk_div + 1) * PD_BUS_PCLK_HZ ==
374 PD_BUS_ACLK_HZ && pclk_div < 0x7);
375
376 rk_clrsetreg(&cru->cru_clksel_con[1],
Simon Glass303384f2017-05-31 17:57:31 -0600377 PD_BUS_PCLK_DIV_MASK | PD_BUS_HCLK_DIV_MASK |
378 PD_BUS_ACLK_DIV0_MASK | PD_BUS_ACLK_DIV1_MASK,
Simon Glass421358c2015-08-30 16:55:31 -0600379 pclk_div << PD_BUS_PCLK_DIV_SHIFT |
380 hclk_div << PD_BUS_HCLK_DIV_SHIFT |
381 aclk_div << PD_BUS_ACLK_DIV0_SHIFT |
382 0 << 0);
383
384 /*
385 * peri clock pll source selection and
386 * set up dependent divisors for PCLK/HCLK and ACLK clocks.
387 */
388 aclk_div = GPLL_HZ / PERI_ACLK_HZ - 1;
389 assert((aclk_div + 1) * PERI_ACLK_HZ == GPLL_HZ && aclk_div < 0x1f);
390
Heiko Stübner1b7dcc32016-07-22 23:51:06 +0200391 hclk_div = ilog2(PERI_ACLK_HZ / PERI_HCLK_HZ);
Simon Glass421358c2015-08-30 16:55:31 -0600392 assert((1 << hclk_div) * PERI_HCLK_HZ ==
393 PERI_ACLK_HZ && (hclk_div < 0x4));
394
Heiko Stübner1b7dcc32016-07-22 23:51:06 +0200395 pclk_div = ilog2(PERI_ACLK_HZ / PERI_PCLK_HZ);
Simon Glass421358c2015-08-30 16:55:31 -0600396 assert((1 << pclk_div) * PERI_PCLK_HZ ==
397 PERI_ACLK_HZ && (pclk_div < 0x4));
398
399 rk_clrsetreg(&cru->cru_clksel_con[10],
Simon Glass303384f2017-05-31 17:57:31 -0600400 PERI_PCLK_DIV_MASK | PERI_HCLK_DIV_MASK |
401 PERI_ACLK_DIV_MASK,
Simon Glasse6a682b2016-01-21 19:45:15 -0700402 PERI_SEL_GPLL << PERI_SEL_PLL_SHIFT |
Simon Glass421358c2015-08-30 16:55:31 -0600403 pclk_div << PERI_PCLK_DIV_SHIFT |
404 hclk_div << PERI_HCLK_DIV_SHIFT |
405 aclk_div << PERI_ACLK_DIV_SHIFT);
406
407 /* PLL enter normal-mode */
408 rk_clrsetreg(&cru->cru_mode_con,
Simon Glass303384f2017-05-31 17:57:31 -0600409 GPLL_MODE_MASK | CPLL_MODE_MASK,
Simon Glass5562bf12016-01-21 19:45:01 -0700410 GPLL_MODE_NORMAL << GPLL_MODE_SHIFT |
411 CPLL_MODE_NORMAL << CPLL_MODE_SHIFT);
Simon Glass421358c2015-08-30 16:55:31 -0600412}
Simon Glass421358c2015-08-30 16:55:31 -0600413
Heiko Stübner1bd4a542016-07-16 00:17:16 +0200414void rk3288_clk_configure_cpu(struct rk3288_cru *cru, struct rk3288_grf *grf)
Simon Glass94906e42016-01-21 19:45:17 -0700415{
416 /* pll enter slow-mode */
Simon Glass303384f2017-05-31 17:57:31 -0600417 rk_clrsetreg(&cru->cru_mode_con, APLL_MODE_MASK,
Simon Glass94906e42016-01-21 19:45:17 -0700418 APLL_MODE_SLOW << APLL_MODE_SHIFT);
419
420 rkclk_set_pll(cru, CLK_ARM, &apll_init_cfg);
421
422 /* waiting for pll lock */
423 while (!(readl(&grf->soc_status[1]) & SOCSTS_APLL_LOCK))
424 udelay(1);
425
426 /*
427 * core clock pll source selection and
428 * set up dependent divisors for MPAXI/M0AXI and ARM clocks.
429 * core clock select apll, apll clk = 1800MHz
430 * arm clk = 1800MHz, mpclk = 450MHz, m0clk = 900MHz
431 */
432 rk_clrsetreg(&cru->cru_clksel_con[0],
Simon Glass303384f2017-05-31 17:57:31 -0600433 CORE_SEL_PLL_MASK | A17_DIV_MASK | MP_DIV_MASK |
434 M0_DIV_MASK,
Simon Glass94906e42016-01-21 19:45:17 -0700435 0 << A17_DIV_SHIFT |
436 3 << MP_DIV_SHIFT |
437 1 << M0_DIV_SHIFT);
438
439 /*
440 * set up dependent divisors for L2RAM/ATCLK and PCLK clocks.
441 * l2ramclk = 900MHz, atclk = 450MHz, pclk_dbg = 450MHz
442 */
443 rk_clrsetreg(&cru->cru_clksel_con[37],
Simon Glass303384f2017-05-31 17:57:31 -0600444 CLK_L2RAM_DIV_MASK | ATCLK_CORE_DIV_CON_MASK |
445 PCLK_CORE_DBG_DIV_MASK,
Simon Glass94906e42016-01-21 19:45:17 -0700446 1 << CLK_L2RAM_DIV_SHIFT |
447 3 << ATCLK_CORE_DIV_CON_SHIFT |
448 3 << PCLK_CORE_DBG_DIV_SHIFT);
449
450 /* PLL enter normal-mode */
Simon Glass303384f2017-05-31 17:57:31 -0600451 rk_clrsetreg(&cru->cru_mode_con, APLL_MODE_MASK,
Simon Glass94906e42016-01-21 19:45:17 -0700452 APLL_MODE_NORMAL << APLL_MODE_SHIFT);
453}
454
Simon Glass421358c2015-08-30 16:55:31 -0600455/* Get pll rate by id */
456static uint32_t rkclk_pll_get_rate(struct rk3288_cru *cru,
457 enum rk_clk_id clk_id)
458{
459 uint32_t nr, no, nf;
460 uint32_t con;
461 int pll_id = rk_pll_id(clk_id);
462 struct rk3288_pll *pll = &cru->pll[pll_id];
463 static u8 clk_shift[CLK_COUNT] = {
Simon Glass5562bf12016-01-21 19:45:01 -0700464 0xff, APLL_MODE_SHIFT, DPLL_MODE_SHIFT, CPLL_MODE_SHIFT,
465 GPLL_MODE_SHIFT, NPLL_MODE_SHIFT
Simon Glass421358c2015-08-30 16:55:31 -0600466 };
467 uint shift;
468
469 con = readl(&cru->cru_mode_con);
470 shift = clk_shift[clk_id];
Simon Glass303384f2017-05-31 17:57:31 -0600471 switch ((con >> shift) & CRU_MODE_MASK) {
Simon Glass5562bf12016-01-21 19:45:01 -0700472 case APLL_MODE_SLOW:
Simon Glass421358c2015-08-30 16:55:31 -0600473 return OSC_HZ;
Simon Glass5562bf12016-01-21 19:45:01 -0700474 case APLL_MODE_NORMAL:
Simon Glass421358c2015-08-30 16:55:31 -0600475 /* normal mode */
476 con = readl(&pll->con0);
Simon Glass303384f2017-05-31 17:57:31 -0600477 no = ((con & CLKOD_MASK) >> CLKOD_SHIFT) + 1;
478 nr = ((con & CLKR_MASK) >> CLKR_SHIFT) + 1;
Simon Glass421358c2015-08-30 16:55:31 -0600479 con = readl(&pll->con1);
Simon Glass303384f2017-05-31 17:57:31 -0600480 nf = ((con & CLKF_MASK) >> CLKF_SHIFT) + 1;
Simon Glass421358c2015-08-30 16:55:31 -0600481
482 return (24 * nf / (nr * no)) * 1000000;
Simon Glass5562bf12016-01-21 19:45:01 -0700483 case APLL_MODE_DEEP:
Simon Glass421358c2015-08-30 16:55:31 -0600484 default:
485 return 32768;
486 }
487}
488
Simon Glassafe0cb02016-01-21 19:43:39 -0700489static ulong rockchip_mmc_get_clk(struct rk3288_cru *cru, uint gclk_rate,
Simon Glass8d32f4b2016-01-21 19:43:38 -0700490 int periph)
Simon Glass421358c2015-08-30 16:55:31 -0600491{
492 uint src_rate;
493 uint div, mux;
494 u32 con;
495
496 switch (periph) {
Simon Glass8d32f4b2016-01-21 19:43:38 -0700497 case HCLK_EMMC:
Xu Ziyuan42118e42017-04-16 17:44:45 +0800498 case SCLK_EMMC:
Simon Glass421358c2015-08-30 16:55:31 -0600499 con = readl(&cru->cru_clksel_con[12]);
Simon Glass303384f2017-05-31 17:57:31 -0600500 mux = (con & EMMC_PLL_MASK) >> EMMC_PLL_SHIFT;
501 div = (con & EMMC_DIV_MASK) >> EMMC_DIV_SHIFT;
Simon Glass421358c2015-08-30 16:55:31 -0600502 break;
Simon Glass8d32f4b2016-01-21 19:43:38 -0700503 case HCLK_SDMMC:
Xu Ziyuan42118e42017-04-16 17:44:45 +0800504 case SCLK_SDMMC:
Simon Glass8d32f4b2016-01-21 19:43:38 -0700505 con = readl(&cru->cru_clksel_con[11]);
Simon Glass303384f2017-05-31 17:57:31 -0600506 mux = (con & MMC0_PLL_MASK) >> MMC0_PLL_SHIFT;
507 div = (con & MMC0_DIV_MASK) >> MMC0_DIV_SHIFT;
Simon Glass421358c2015-08-30 16:55:31 -0600508 break;
Simon Glass8d32f4b2016-01-21 19:43:38 -0700509 case HCLK_SDIO0:
Xu Ziyuan42118e42017-04-16 17:44:45 +0800510 case SCLK_SDIO0:
Simon Glass421358c2015-08-30 16:55:31 -0600511 con = readl(&cru->cru_clksel_con[12]);
Simon Glass303384f2017-05-31 17:57:31 -0600512 mux = (con & SDIO0_PLL_MASK) >> SDIO0_PLL_SHIFT;
513 div = (con & SDIO0_DIV_MASK) >> SDIO0_DIV_SHIFT;
Simon Glass421358c2015-08-30 16:55:31 -0600514 break;
515 default:
516 return -EINVAL;
517 }
518
Simon Glassafe0cb02016-01-21 19:43:39 -0700519 src_rate = mux == EMMC_PLL_SELECT_24MHZ ? OSC_HZ : gclk_rate;
Simon Glass421358c2015-08-30 16:55:31 -0600520 return DIV_TO_RATE(src_rate, div);
521}
522
Simon Glassafe0cb02016-01-21 19:43:39 -0700523static ulong rockchip_mmc_set_clk(struct rk3288_cru *cru, uint gclk_rate,
Simon Glass8d32f4b2016-01-21 19:43:38 -0700524 int periph, uint freq)
Simon Glass421358c2015-08-30 16:55:31 -0600525{
526 int src_clk_div;
527 int mux;
528
Simon Glassafe0cb02016-01-21 19:43:39 -0700529 debug("%s: gclk_rate=%u\n", __func__, gclk_rate);
Kever Yang99b546d2017-07-27 12:54:01 +0800530 /* mmc clock default div 2 internal, need provide double in cru */
531 src_clk_div = DIV_ROUND_UP(gclk_rate / 2, freq);
Simon Glass421358c2015-08-30 16:55:31 -0600532
533 if (src_clk_div > 0x3f) {
Kever Yang99b546d2017-07-27 12:54:01 +0800534 src_clk_div = DIV_ROUND_UP(OSC_HZ / 2, freq);
Kever Yangf20995b2017-07-27 12:54:02 +0800535 assert(src_clk_div < 0x40);
Simon Glass421358c2015-08-30 16:55:31 -0600536 mux = EMMC_PLL_SELECT_24MHZ;
537 assert((int)EMMC_PLL_SELECT_24MHZ ==
538 (int)MMC0_PLL_SELECT_24MHZ);
539 } else {
540 mux = EMMC_PLL_SELECT_GENERAL;
541 assert((int)EMMC_PLL_SELECT_GENERAL ==
542 (int)MMC0_PLL_SELECT_GENERAL);
543 }
544 switch (periph) {
Simon Glass8d32f4b2016-01-21 19:43:38 -0700545 case HCLK_EMMC:
Xu Ziyuan42118e42017-04-16 17:44:45 +0800546 case SCLK_EMMC:
Simon Glass421358c2015-08-30 16:55:31 -0600547 rk_clrsetreg(&cru->cru_clksel_con[12],
Simon Glass303384f2017-05-31 17:57:31 -0600548 EMMC_PLL_MASK | EMMC_DIV_MASK,
Simon Glass421358c2015-08-30 16:55:31 -0600549 mux << EMMC_PLL_SHIFT |
550 (src_clk_div - 1) << EMMC_DIV_SHIFT);
551 break;
Simon Glass8d32f4b2016-01-21 19:43:38 -0700552 case HCLK_SDMMC:
Xu Ziyuan42118e42017-04-16 17:44:45 +0800553 case SCLK_SDMMC:
Simon Glass421358c2015-08-30 16:55:31 -0600554 rk_clrsetreg(&cru->cru_clksel_con[11],
Simon Glass303384f2017-05-31 17:57:31 -0600555 MMC0_PLL_MASK | MMC0_DIV_MASK,
Simon Glass421358c2015-08-30 16:55:31 -0600556 mux << MMC0_PLL_SHIFT |
557 (src_clk_div - 1) << MMC0_DIV_SHIFT);
558 break;
Simon Glass8d32f4b2016-01-21 19:43:38 -0700559 case HCLK_SDIO0:
Xu Ziyuan42118e42017-04-16 17:44:45 +0800560 case SCLK_SDIO0:
Simon Glass421358c2015-08-30 16:55:31 -0600561 rk_clrsetreg(&cru->cru_clksel_con[12],
Simon Glass303384f2017-05-31 17:57:31 -0600562 SDIO0_PLL_MASK | SDIO0_DIV_MASK,
Simon Glass421358c2015-08-30 16:55:31 -0600563 mux << SDIO0_PLL_SHIFT |
564 (src_clk_div - 1) << SDIO0_DIV_SHIFT);
565 break;
566 default:
567 return -EINVAL;
568 }
569
Simon Glassafe0cb02016-01-21 19:43:39 -0700570 return rockchip_mmc_get_clk(cru, gclk_rate, periph);
Simon Glass421358c2015-08-30 16:55:31 -0600571}
572
Simon Glassafe0cb02016-01-21 19:43:39 -0700573static ulong rockchip_spi_get_clk(struct rk3288_cru *cru, uint gclk_rate,
Simon Glass8d32f4b2016-01-21 19:43:38 -0700574 int periph)
Simon Glass421358c2015-08-30 16:55:31 -0600575{
576 uint div, mux;
577 u32 con;
578
579 switch (periph) {
Simon Glass8d32f4b2016-01-21 19:43:38 -0700580 case SCLK_SPI0:
Simon Glass421358c2015-08-30 16:55:31 -0600581 con = readl(&cru->cru_clksel_con[25]);
Simon Glass303384f2017-05-31 17:57:31 -0600582 mux = (con & SPI0_PLL_MASK) >> SPI0_PLL_SHIFT;
583 div = (con & SPI0_DIV_MASK) >> SPI0_DIV_SHIFT;
Simon Glass421358c2015-08-30 16:55:31 -0600584 break;
Simon Glass8d32f4b2016-01-21 19:43:38 -0700585 case SCLK_SPI1:
Simon Glass421358c2015-08-30 16:55:31 -0600586 con = readl(&cru->cru_clksel_con[25]);
Simon Glass303384f2017-05-31 17:57:31 -0600587 mux = (con & SPI1_PLL_MASK) >> SPI1_PLL_SHIFT;
588 div = (con & SPI1_DIV_MASK) >> SPI1_DIV_SHIFT;
Simon Glass421358c2015-08-30 16:55:31 -0600589 break;
Simon Glass8d32f4b2016-01-21 19:43:38 -0700590 case SCLK_SPI2:
Simon Glass421358c2015-08-30 16:55:31 -0600591 con = readl(&cru->cru_clksel_con[39]);
Simon Glass303384f2017-05-31 17:57:31 -0600592 mux = (con & SPI2_PLL_MASK) >> SPI2_PLL_SHIFT;
593 div = (con & SPI2_DIV_MASK) >> SPI2_DIV_SHIFT;
Simon Glass421358c2015-08-30 16:55:31 -0600594 break;
595 default:
596 return -EINVAL;
597 }
598 assert(mux == SPI0_PLL_SELECT_GENERAL);
599
Simon Glassafe0cb02016-01-21 19:43:39 -0700600 return DIV_TO_RATE(gclk_rate, div);
Simon Glass421358c2015-08-30 16:55:31 -0600601}
602
Simon Glassafe0cb02016-01-21 19:43:39 -0700603static ulong rockchip_spi_set_clk(struct rk3288_cru *cru, uint gclk_rate,
Simon Glass8d32f4b2016-01-21 19:43:38 -0700604 int periph, uint freq)
Simon Glass421358c2015-08-30 16:55:31 -0600605{
606 int src_clk_div;
607
Simon Glassafe0cb02016-01-21 19:43:39 -0700608 debug("%s: clk_general_rate=%u\n", __func__, gclk_rate);
Kever Yangf20995b2017-07-27 12:54:02 +0800609 src_clk_div = DIV_ROUND_UP(gclk_rate, freq) - 1;
610 assert(src_clk_div < 128);
Simon Glass421358c2015-08-30 16:55:31 -0600611 switch (periph) {
Simon Glass8d32f4b2016-01-21 19:43:38 -0700612 case SCLK_SPI0:
Simon Glass421358c2015-08-30 16:55:31 -0600613 rk_clrsetreg(&cru->cru_clksel_con[25],
Simon Glass303384f2017-05-31 17:57:31 -0600614 SPI0_PLL_MASK | SPI0_DIV_MASK,
Simon Glass421358c2015-08-30 16:55:31 -0600615 SPI0_PLL_SELECT_GENERAL << SPI0_PLL_SHIFT |
616 src_clk_div << SPI0_DIV_SHIFT);
617 break;
Simon Glass8d32f4b2016-01-21 19:43:38 -0700618 case SCLK_SPI1:
Simon Glass421358c2015-08-30 16:55:31 -0600619 rk_clrsetreg(&cru->cru_clksel_con[25],
Simon Glass303384f2017-05-31 17:57:31 -0600620 SPI1_PLL_MASK | SPI1_DIV_MASK,
Simon Glass421358c2015-08-30 16:55:31 -0600621 SPI1_PLL_SELECT_GENERAL << SPI1_PLL_SHIFT |
622 src_clk_div << SPI1_DIV_SHIFT);
623 break;
Simon Glass8d32f4b2016-01-21 19:43:38 -0700624 case SCLK_SPI2:
Simon Glass421358c2015-08-30 16:55:31 -0600625 rk_clrsetreg(&cru->cru_clksel_con[39],
Simon Glass303384f2017-05-31 17:57:31 -0600626 SPI2_PLL_MASK | SPI2_DIV_MASK,
Simon Glass421358c2015-08-30 16:55:31 -0600627 SPI2_PLL_SELECT_GENERAL << SPI2_PLL_SHIFT |
628 src_clk_div << SPI2_DIV_SHIFT);
629 break;
630 default:
631 return -EINVAL;
632 }
633
Simon Glassafe0cb02016-01-21 19:43:39 -0700634 return rockchip_spi_get_clk(cru, gclk_rate, periph);
Simon Glass421358c2015-08-30 16:55:31 -0600635}
636
Stephen Warrena9622432016-06-17 09:44:00 -0600637static ulong rk3288_clk_get_rate(struct clk *clk)
Simon Glass398ced12016-01-21 19:43:40 -0700638{
Stephen Warrena9622432016-06-17 09:44:00 -0600639 struct rk3288_clk_priv *priv = dev_get_priv(clk->dev);
Simon Glass398ced12016-01-21 19:43:40 -0700640 ulong new_rate, gclk_rate;
Simon Glass398ced12016-01-21 19:43:40 -0700641
Stephen Warrena9622432016-06-17 09:44:00 -0600642 gclk_rate = rkclk_pll_get_rate(priv->cru, CLK_GENERAL);
643 switch (clk->id) {
644 case 0 ... 63:
645 new_rate = rkclk_pll_get_rate(priv->cru, clk->id);
646 break;
Simon Glass398ced12016-01-21 19:43:40 -0700647 case HCLK_EMMC:
Simon Glassd4a8a682016-01-21 19:43:45 -0700648 case HCLK_SDMMC:
Simon Glass398ced12016-01-21 19:43:40 -0700649 case HCLK_SDIO0:
Xu Ziyuan42118e42017-04-16 17:44:45 +0800650 case SCLK_EMMC:
651 case SCLK_SDMMC:
652 case SCLK_SDIO0:
Stephen Warrena9622432016-06-17 09:44:00 -0600653 new_rate = rockchip_mmc_get_clk(priv->cru, gclk_rate, clk->id);
Simon Glass398ced12016-01-21 19:43:40 -0700654 break;
655 case SCLK_SPI0:
656 case SCLK_SPI1:
657 case SCLK_SPI2:
Stephen Warrena9622432016-06-17 09:44:00 -0600658 new_rate = rockchip_spi_get_clk(priv->cru, gclk_rate, clk->id);
Simon Glass398ced12016-01-21 19:43:40 -0700659 break;
660 case PCLK_I2C0:
661 case PCLK_I2C1:
662 case PCLK_I2C2:
663 case PCLK_I2C3:
664 case PCLK_I2C4:
665 case PCLK_I2C5:
666 return gclk_rate;
Kever Yang40514622016-08-12 17:57:05 +0800667 case PCLK_PWM:
668 return PD_BUS_PCLK_HZ;
Simon Glass398ced12016-01-21 19:43:40 -0700669 default:
670 return -ENOENT;
671 }
672
673 return new_rate;
674}
675
Stephen Warrena9622432016-06-17 09:44:00 -0600676static ulong rk3288_clk_set_rate(struct clk *clk, ulong rate)
Simon Glass421358c2015-08-30 16:55:31 -0600677{
Stephen Warrena9622432016-06-17 09:44:00 -0600678 struct rk3288_clk_priv *priv = dev_get_priv(clk->dev);
Simon Glass273afb22016-01-21 19:45:02 -0700679 struct rk3288_cru *cru = priv->cru;
Simon Glass8d32f4b2016-01-21 19:43:38 -0700680 ulong new_rate, gclk_rate;
Simon Glass421358c2015-08-30 16:55:31 -0600681
Stephen Warrena9622432016-06-17 09:44:00 -0600682 gclk_rate = rkclk_pll_get_rate(priv->cru, CLK_GENERAL);
683 switch (clk->id) {
Simon Glasscb1c7af2016-11-13 14:22:13 -0700684 case PLL_APLL:
685 /* We only support a fixed rate here */
686 if (rate != 1800000000)
687 return -EINVAL;
688 rk3288_clk_configure_cpu(priv->cru, priv->grf);
689 new_rate = rate;
690 break;
Stephen Warrena9622432016-06-17 09:44:00 -0600691 case CLK_DDR:
692 new_rate = rkclk_configure_ddr(priv->cru, priv->grf, rate);
693 break;
Simon Glass8d32f4b2016-01-21 19:43:38 -0700694 case HCLK_EMMC:
695 case HCLK_SDMMC:
696 case HCLK_SDIO0:
Xu Ziyuan42118e42017-04-16 17:44:45 +0800697 case SCLK_EMMC:
698 case SCLK_SDMMC:
699 case SCLK_SDIO0:
Stephen Warrena9622432016-06-17 09:44:00 -0600700 new_rate = rockchip_mmc_set_clk(cru, gclk_rate, clk->id, rate);
Simon Glass421358c2015-08-30 16:55:31 -0600701 break;
Simon Glass8d32f4b2016-01-21 19:43:38 -0700702 case SCLK_SPI0:
703 case SCLK_SPI1:
704 case SCLK_SPI2:
Stephen Warrena9622432016-06-17 09:44:00 -0600705 new_rate = rockchip_spi_set_clk(cru, gclk_rate, clk->id, rate);
Simon Glass273afb22016-01-21 19:45:02 -0700706 break;
707#ifndef CONFIG_SPL_BUILD
Sjoerd Simons3ce69bf2016-02-28 22:24:59 +0100708 case SCLK_MAC:
Stephen Warrena9622432016-06-17 09:44:00 -0600709 new_rate = rockchip_mac_set_clk(priv->cru, clk->id, rate);
Sjoerd Simons3ce69bf2016-02-28 22:24:59 +0100710 break;
Simon Glass273afb22016-01-21 19:45:02 -0700711 case DCLK_VOP0:
712 case DCLK_VOP1:
Stephen Warrena9622432016-06-17 09:44:00 -0600713 new_rate = rockchip_vop_set_clk(cru, priv->grf, clk->id, rate);
Simon Glass273afb22016-01-21 19:45:02 -0700714 break;
715 case SCLK_EDP_24M:
716 /* clk_edp_24M source: 24M */
717 rk_setreg(&cru->cru_clksel_con[28], 1 << 15);
718
719 /* rst edp */
720 rk_setreg(&cru->cru_clksel_con[6], 1 << 15);
721 udelay(1);
722 rk_clrreg(&cru->cru_clksel_con[6], 1 << 15);
723 new_rate = rate;
724 break;
725 case ACLK_VOP0:
726 case ACLK_VOP1: {
727 u32 div;
728
729 /* vop aclk source clk: cpll */
730 div = CPLL_HZ / rate;
731 assert((div - 1 < 64) && (div * rate == CPLL_HZ));
732
Stephen Warrena9622432016-06-17 09:44:00 -0600733 switch (clk->id) {
Simon Glass273afb22016-01-21 19:45:02 -0700734 case ACLK_VOP0:
735 rk_clrsetreg(&cru->cru_clksel_con[31],
736 3 << 6 | 0x1f << 0,
737 0 << 6 | (div - 1) << 0);
738 break;
739 case ACLK_VOP1:
740 rk_clrsetreg(&cru->cru_clksel_con[31],
741 3 << 14 | 0x1f << 8,
742 0 << 14 | (div - 1) << 8);
743 break;
744 }
745 new_rate = rate;
Simon Glass421358c2015-08-30 16:55:31 -0600746 break;
Simon Glass273afb22016-01-21 19:45:02 -0700747 }
748 case PCLK_HDMI_CTRL:
749 /* enable pclk hdmi ctrl */
750 rk_clrreg(&cru->cru_clkgate_con[16], 1 << 9);
751
752 /* software reset hdmi */
753 rk_setreg(&cru->cru_clkgate_con[7], 1 << 9);
754 udelay(1);
755 rk_clrreg(&cru->cru_clkgate_con[7], 1 << 9);
756 new_rate = rate;
757 break;
758#endif
Simon Glass421358c2015-08-30 16:55:31 -0600759 default:
760 return -ENOENT;
761 }
762
763 return new_rate;
764}
765
766static struct clk_ops rk3288_clk_ops = {
767 .get_rate = rk3288_clk_get_rate,
768 .set_rate = rk3288_clk_set_rate,
Simon Glass421358c2015-08-30 16:55:31 -0600769};
770
Simon Glass994c29d2016-07-04 11:58:28 -0600771static int rk3288_clk_ofdata_to_platdata(struct udevice *dev)
Simon Glass421358c2015-08-30 16:55:31 -0600772{
Simon Glass00c5fd42016-07-04 11:58:29 -0600773#if !CONFIG_IS_ENABLED(OF_PLATDATA)
Simon Glass421358c2015-08-30 16:55:31 -0600774 struct rk3288_clk_priv *priv = dev_get_priv(dev);
775
Simon Glassba1dea42017-05-17 17:18:05 -0600776 priv->cru = (struct rk3288_cru *)devfdt_get_addr(dev);
Simon Glass00c5fd42016-07-04 11:58:29 -0600777#endif
Simon Glass994c29d2016-07-04 11:58:28 -0600778
779 return 0;
780}
781
782static int rk3288_clk_probe(struct udevice *dev)
783{
784 struct rk3288_clk_priv *priv = dev_get_priv(dev);
Simon Glass30ca6a42017-05-31 17:57:32 -0600785 bool init_clocks = false;
Simon Glass994c29d2016-07-04 11:58:28 -0600786
Simon Glass421358c2015-08-30 16:55:31 -0600787 priv->grf = syscon_get_first_range(ROCKCHIP_SYSCON_GRF);
Simon Glass994c29d2016-07-04 11:58:28 -0600788 if (IS_ERR(priv->grf))
789 return PTR_ERR(priv->grf);
Simon Glass421358c2015-08-30 16:55:31 -0600790#ifdef CONFIG_SPL_BUILD
Simon Glass00c5fd42016-07-04 11:58:29 -0600791#if CONFIG_IS_ENABLED(OF_PLATDATA)
792 struct rk3288_clk_plat *plat = dev_get_platdata(dev);
793
794 priv->cru = map_sysmem(plat->dtd.reg[0], plat->dtd.reg[1]);
795#endif
Simon Glass30ca6a42017-05-31 17:57:32 -0600796 init_clocks = true;
Simon Glass421358c2015-08-30 16:55:31 -0600797#endif
Simon Glass30ca6a42017-05-31 17:57:32 -0600798 if (!(gd->flags & GD_FLG_RELOC)) {
799 u32 reg;
800
801 /*
802 * Init clocks in U-Boot proper if the NPLL is runnning. This
803 * indicates that a previous boot loader set up the clocks, so
804 * we need to redo it. U-Boot's SPL does not set this clock.
805 */
806 reg = readl(&priv->cru->cru_mode_con);
807 if (((reg & NPLL_MODE_MASK) >> NPLL_MODE_SHIFT) ==
808 NPLL_MODE_NORMAL)
809 init_clocks = true;
810 }
811
812 if (init_clocks)
813 rkclk_init(priv->cru, priv->grf);
Simon Glass421358c2015-08-30 16:55:31 -0600814
815 return 0;
816}
817
Simon Glass421358c2015-08-30 16:55:31 -0600818static int rk3288_clk_bind(struct udevice *dev)
819{
Stephen Warrena9622432016-06-17 09:44:00 -0600820 int ret;
Simon Glass421358c2015-08-30 16:55:31 -0600821
822 /* The reset driver does not have a device node, so bind it here */
Stephen Warren859f2562016-05-12 12:03:35 -0600823 ret = device_bind_driver(gd->dm_root, "rk3288_sysreset", "reset", &dev);
Simon Glass421358c2015-08-30 16:55:31 -0600824 if (ret)
825 debug("Warning: No RK3288 reset driver: ret=%d\n", ret);
826
827 return 0;
828}
829
830static const struct udevice_id rk3288_clk_ids[] = {
831 { .compatible = "rockchip,rk3288-cru" },
832 { }
833};
834
Simon Glass00c5fd42016-07-04 11:58:29 -0600835U_BOOT_DRIVER(rockchip_rk3288_cru) = {
836 .name = "rockchip_rk3288_cru",
Simon Glass421358c2015-08-30 16:55:31 -0600837 .id = UCLASS_CLK,
838 .of_match = rk3288_clk_ids,
839 .priv_auto_alloc_size = sizeof(struct rk3288_clk_priv),
Simon Glass00c5fd42016-07-04 11:58:29 -0600840 .platdata_auto_alloc_size = sizeof(struct rk3288_clk_plat),
Simon Glass421358c2015-08-30 16:55:31 -0600841 .ops = &rk3288_clk_ops,
842 .bind = rk3288_clk_bind,
Simon Glass994c29d2016-07-04 11:58:28 -0600843 .ofdata_to_platdata = rk3288_clk_ofdata_to_platdata,
Simon Glass421358c2015-08-30 16:55:31 -0600844 .probe = rk3288_clk_probe,
845};