blob: 432a79291c8a4250b20735e71c7b5f9b7338d72c [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0
Simon Glass421358c2015-08-30 16:55:31 -06002/*
3 * (C) Copyright 2015 Google, Inc
Simon Glass421358c2015-08-30 16:55:31 -06004 */
5
David Wu3c248b22017-09-20 14:28:19 +08006#include <bitfield.h>
Stephen Warrena9622432016-06-17 09:44:00 -06007#include <clk-uclass.h>
Simon Glasscf6741b2018-12-27 20:15:20 -07008#include <div64.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 Glass0f2af882020-05-10 11:40:05 -060012#include <log.h>
Simon Glass9bc15642020-02-03 07:36:16 -070013#include <malloc.h>
Simon Glass00c5fd42016-07-04 11:58:29 -060014#include <mapmem.h>
Simon Glass421358c2015-08-30 16:55:31 -060015#include <syscon.h>
Simon Glass3ba929a2020-10-30 21:38:53 -060016#include <asm/global_data.h>
Kever Yang9fbe17c2019-03-28 11:01:23 +080017#include <asm/arch-rockchip/clock.h>
Jagan Teki783acfd2020-01-09 14:22:17 +053018#include <asm/arch-rockchip/cru.h>
Kever Yang9fbe17c2019-03-28 11:01:23 +080019#include <asm/arch-rockchip/grf_rk3288.h>
20#include <asm/arch-rockchip/hardware.h>
Simon Glass8d32f4b2016-01-21 19:43:38 -070021#include <dt-bindings/clock/rk3288-cru.h>
Simon Glass344f3662016-01-21 19:43:41 -070022#include <dm/device-internal.h>
Simon Glass421358c2015-08-30 16:55:31 -060023#include <dm/lists.h>
Simon Glass344f3662016-01-21 19:43:41 -070024#include <dm/uclass-internal.h>
Simon Glass4dcacfc2020-05-10 11:40:13 -060025#include <linux/bitops.h>
Simon Glassdbd79542020-05-10 11:40:11 -060026#include <linux/delay.h>
Simon Glassd66c5f72020-02-03 07:36:15 -070027#include <linux/err.h>
Heiko Stübner1b7dcc32016-07-22 23:51:06 +020028#include <linux/log2.h>
Simon Glassfb64e362020-05-10 11:40:09 -060029#include <linux/stringify.h>
Simon Glass421358c2015-08-30 16:55:31 -060030
31DECLARE_GLOBAL_DATA_PTR;
32
Simon Glass00c5fd42016-07-04 11:58:29 -060033struct rk3288_clk_plat {
34#if CONFIG_IS_ENABLED(OF_PLATDATA)
35 struct dtd_rockchip_rk3288_cru dtd;
36#endif
37};
38
Simon Glass421358c2015-08-30 16:55:31 -060039struct pll_div {
40 u32 nr;
41 u32 nf;
42 u32 no;
43};
44
45enum {
46 VCO_MAX_HZ = 2200U * 1000000,
47 VCO_MIN_HZ = 440 * 1000000,
48 OUTPUT_MAX_HZ = 2200U * 1000000,
49 OUTPUT_MIN_HZ = 27500000,
50 FREF_MAX_HZ = 2200U * 1000000,
Heiko Stübner7f78c242016-07-16 00:17:17 +020051 FREF_MIN_HZ = 269 * 1000,
Simon Glass421358c2015-08-30 16:55:31 -060052};
53
54enum {
55 /* PLL CON0 */
56 PLL_OD_MASK = 0x0f,
57
58 /* PLL CON1 */
59 PLL_NF_MASK = 0x1fff,
60
61 /* PLL CON2 */
62 PLL_BWADJ_MASK = 0x0fff,
63
64 /* PLL CON3 */
65 PLL_RESET_SHIFT = 5,
66
Simon Glass94906e42016-01-21 19:45:17 -070067 /* CLKSEL0 */
Simon Glass94906e42016-01-21 19:45:17 -070068 CORE_SEL_PLL_SHIFT = 15,
Simon Glass303384f2017-05-31 17:57:31 -060069 CORE_SEL_PLL_MASK = 1 << CORE_SEL_PLL_SHIFT,
Simon Glass94906e42016-01-21 19:45:17 -070070 A17_DIV_SHIFT = 8,
Simon Glass303384f2017-05-31 17:57:31 -060071 A17_DIV_MASK = 0x1f << A17_DIV_SHIFT,
Simon Glass94906e42016-01-21 19:45:17 -070072 MP_DIV_SHIFT = 4,
Simon Glass303384f2017-05-31 17:57:31 -060073 MP_DIV_MASK = 0xf << MP_DIV_SHIFT,
Simon Glass94906e42016-01-21 19:45:17 -070074 M0_DIV_SHIFT = 0,
Simon Glass303384f2017-05-31 17:57:31 -060075 M0_DIV_MASK = 0xf << M0_DIV_SHIFT,
Simon Glass94906e42016-01-21 19:45:17 -070076
Simon Glass421358c2015-08-30 16:55:31 -060077 /* CLKSEL1: pd bus clk pll sel: codec or general */
78 PD_BUS_SEL_PLL_MASK = 15,
79 PD_BUS_SEL_CPLL = 0,
80 PD_BUS_SEL_GPLL,
81
82 /* pd bus pclk div: pclk = pd_bus_aclk /(div + 1) */
83 PD_BUS_PCLK_DIV_SHIFT = 12,
Simon Glass303384f2017-05-31 17:57:31 -060084 PD_BUS_PCLK_DIV_MASK = 7 << PD_BUS_PCLK_DIV_SHIFT,
Simon Glass421358c2015-08-30 16:55:31 -060085
86 /* pd bus hclk div: aclk_bus: hclk_bus = 1:1 or 2:1 or 4:1 */
87 PD_BUS_HCLK_DIV_SHIFT = 8,
Simon Glass303384f2017-05-31 17:57:31 -060088 PD_BUS_HCLK_DIV_MASK = 3 << PD_BUS_HCLK_DIV_SHIFT,
Simon Glass421358c2015-08-30 16:55:31 -060089
90 /* pd bus aclk div: pd_bus_aclk = pd_bus_src_clk /(div0 * div1) */
91 PD_BUS_ACLK_DIV0_SHIFT = 3,
Simon Glass303384f2017-05-31 17:57:31 -060092 PD_BUS_ACLK_DIV0_MASK = 0x1f << PD_BUS_ACLK_DIV0_SHIFT,
Simon Glass421358c2015-08-30 16:55:31 -060093 PD_BUS_ACLK_DIV1_SHIFT = 0,
Simon Glass303384f2017-05-31 17:57:31 -060094 PD_BUS_ACLK_DIV1_MASK = 0x7 << PD_BUS_ACLK_DIV1_SHIFT,
Simon Glass421358c2015-08-30 16:55:31 -060095
96 /*
97 * CLKSEL10
98 * peripheral bus pclk div:
99 * aclk_bus: pclk_bus = 1:1 or 2:1 or 4:1 or 8:1
100 */
Simon Glasse6a682b2016-01-21 19:45:15 -0700101 PERI_SEL_PLL_SHIFT = 15,
Simon Glass303384f2017-05-31 17:57:31 -0600102 PERI_SEL_PLL_MASK = 1 << PERI_SEL_PLL_SHIFT,
Simon Glasse6a682b2016-01-21 19:45:15 -0700103 PERI_SEL_CPLL = 0,
104 PERI_SEL_GPLL,
105
Simon Glass421358c2015-08-30 16:55:31 -0600106 PERI_PCLK_DIV_SHIFT = 12,
Simon Glass303384f2017-05-31 17:57:31 -0600107 PERI_PCLK_DIV_MASK = 3 << PERI_PCLK_DIV_SHIFT,
Simon Glass421358c2015-08-30 16:55:31 -0600108
109 /* peripheral bus hclk div: aclk_bus: hclk_bus = 1:1 or 2:1 or 4:1 */
110 PERI_HCLK_DIV_SHIFT = 8,
Simon Glass303384f2017-05-31 17:57:31 -0600111 PERI_HCLK_DIV_MASK = 3 << PERI_HCLK_DIV_SHIFT,
Simon Glass421358c2015-08-30 16:55:31 -0600112
113 /*
114 * peripheral bus aclk div:
115 * aclk_periph = periph_clk_src / (peri_aclk_div_con + 1)
116 */
117 PERI_ACLK_DIV_SHIFT = 0,
Simon Glass303384f2017-05-31 17:57:31 -0600118 PERI_ACLK_DIV_MASK = 0x1f << PERI_ACLK_DIV_SHIFT,
Simon Glass421358c2015-08-30 16:55:31 -0600119
David Wu3c248b22017-09-20 14:28:19 +0800120 /*
121 * CLKSEL24
122 * saradc_div_con:
123 * clk_saradc=24MHz/(saradc_div_con+1)
124 */
125 CLK_SARADC_DIV_CON_SHIFT = 8,
126 CLK_SARADC_DIV_CON_MASK = GENMASK(15, 8),
127 CLK_SARADC_DIV_CON_WIDTH = 8,
128
Simon Glass421358c2015-08-30 16:55:31 -0600129 SOCSTS_DPLL_LOCK = 1 << 5,
130 SOCSTS_APLL_LOCK = 1 << 6,
131 SOCSTS_CPLL_LOCK = 1 << 7,
132 SOCSTS_GPLL_LOCK = 1 << 8,
133 SOCSTS_NPLL_LOCK = 1 << 9,
134};
135
Simon Glass421358c2015-08-30 16:55:31 -0600136#define DIV_TO_RATE(input_rate, div) ((input_rate) / ((div) + 1))
137
138#define PLL_DIVISORS(hz, _nr, _no) {\
139 .nr = _nr, .nf = (u32)((u64)hz * _nr * _no / OSC_HZ), .no = _no};\
140 _Static_assert(((u64)hz * _nr * _no / OSC_HZ) * OSC_HZ /\
141 (_nr * _no) == hz, #hz "Hz cannot be hit with PLL "\
142 "divisors on line " __stringify(__LINE__));
143
144/* Keep divisors as low as possible to reduce jitter and power usage */
145static const struct pll_div apll_init_cfg = PLL_DIVISORS(APLL_HZ, 1, 1);
146static const struct pll_div gpll_init_cfg = PLL_DIVISORS(GPLL_HZ, 2, 2);
147static const struct pll_div cpll_init_cfg = PLL_DIVISORS(CPLL_HZ, 1, 2);
148
Jagan Teki783acfd2020-01-09 14:22:17 +0530149static int rkclk_set_pll(struct rockchip_cru *cru, enum rk_clk_id clk_id,
Simon Glass421358c2015-08-30 16:55:31 -0600150 const struct pll_div *div)
151{
152 int pll_id = rk_pll_id(clk_id);
153 struct rk3288_pll *pll = &cru->pll[pll_id];
154 /* All PLLs have same VCO and output frequency range restrictions. */
155 uint vco_hz = OSC_HZ / 1000 * div->nf / div->nr * 1000;
156 uint output_hz = vco_hz / div->no;
157
Simon Glasse6a682b2016-01-21 19:45:15 -0700158 debug("PLL at %x: nf=%d, nr=%d, no=%d, vco=%u Hz, output=%u Hz\n",
159 (uint)pll, div->nf, div->nr, div->no, vco_hz, output_hz);
Simon Glass421358c2015-08-30 16:55:31 -0600160 assert(vco_hz >= VCO_MIN_HZ && vco_hz <= VCO_MAX_HZ &&
161 output_hz >= OUTPUT_MIN_HZ && output_hz <= OUTPUT_MAX_HZ &&
162 (div->no == 1 || !(div->no % 2)));
163
Simon Glasse6a682b2016-01-21 19:45:15 -0700164 /* enter reset */
Simon Glass421358c2015-08-30 16:55:31 -0600165 rk_setreg(&pll->con3, 1 << PLL_RESET_SHIFT);
166
Simon Glass303384f2017-05-31 17:57:31 -0600167 rk_clrsetreg(&pll->con0, CLKR_MASK | PLL_OD_MASK,
Simon Glass421358c2015-08-30 16:55:31 -0600168 ((div->nr - 1) << CLKR_SHIFT) | (div->no - 1));
169 rk_clrsetreg(&pll->con1, CLKF_MASK, div->nf - 1);
170 rk_clrsetreg(&pll->con2, PLL_BWADJ_MASK, (div->nf >> 1) - 1);
171
172 udelay(10);
173
Simon Glasse6a682b2016-01-21 19:45:15 -0700174 /* return from reset */
Simon Glass421358c2015-08-30 16:55:31 -0600175 rk_clrreg(&pll->con3, 1 << PLL_RESET_SHIFT);
176
177 return 0;
178}
179
Jagan Teki783acfd2020-01-09 14:22:17 +0530180static int rkclk_configure_ddr(struct rockchip_cru *cru, struct rk3288_grf *grf,
Simon Glass421358c2015-08-30 16:55:31 -0600181 unsigned int hz)
182{
183 static const struct pll_div dpll_cfg[] = {
184 {.nf = 25, .nr = 2, .no = 1},
185 {.nf = 400, .nr = 9, .no = 2},
186 {.nf = 500, .nr = 9, .no = 2},
187 {.nf = 100, .nr = 3, .no = 1},
188 };
189 int cfg;
190
Simon Glass421358c2015-08-30 16:55:31 -0600191 switch (hz) {
192 case 300000000:
193 cfg = 0;
194 break;
195 case 533000000: /* actually 533.3P MHz */
196 cfg = 1;
197 break;
198 case 666000000: /* actually 666.6P MHz */
199 cfg = 2;
200 break;
201 case 800000000:
202 cfg = 3;
203 break;
204 default:
Simon Glasse6a682b2016-01-21 19:45:15 -0700205 debug("Unsupported SDRAM frequency");
Simon Glass421358c2015-08-30 16:55:31 -0600206 return -EINVAL;
207 }
208
209 /* pll enter slow-mode */
Simon Glass303384f2017-05-31 17:57:31 -0600210 rk_clrsetreg(&cru->cru_mode_con, DPLL_MODE_MASK,
Simon Glass421358c2015-08-30 16:55:31 -0600211 DPLL_MODE_SLOW << DPLL_MODE_SHIFT);
212
213 rkclk_set_pll(cru, CLK_DDR, &dpll_cfg[cfg]);
214
215 /* wait for pll lock */
216 while (!(readl(&grf->soc_status[1]) & SOCSTS_DPLL_LOCK))
217 udelay(1);
218
219 /* PLL enter normal-mode */
Simon Glass303384f2017-05-31 17:57:31 -0600220 rk_clrsetreg(&cru->cru_mode_con, DPLL_MODE_MASK,
Simon Glass5562bf12016-01-21 19:45:01 -0700221 DPLL_MODE_NORMAL << DPLL_MODE_SHIFT);
Simon Glass421358c2015-08-30 16:55:31 -0600222
223 return 0;
224}
225
Simon Glass273afb22016-01-21 19:45:02 -0700226#ifndef CONFIG_SPL_BUILD
227#define VCO_MAX_KHZ 2200000
228#define VCO_MIN_KHZ 440000
229#define FREF_MAX_KHZ 2200000
230#define FREF_MIN_KHZ 269
231
232static int pll_para_config(ulong freq_hz, struct pll_div *div, uint *ext_div)
233{
234 uint ref_khz = OSC_HZ / 1000, nr, nf = 0;
235 uint fref_khz;
236 uint diff_khz, best_diff_khz;
237 const uint max_nr = 1 << 6, max_nf = 1 << 12, max_no = 1 << 4;
238 uint vco_khz;
239 uint no = 1;
240 uint freq_khz = freq_hz / 1000;
241
242 if (!freq_hz) {
243 printf("%s: the frequency can not be 0 Hz\n", __func__);
244 return -EINVAL;
245 }
246
247 no = DIV_ROUND_UP(VCO_MIN_KHZ, freq_khz);
248 if (ext_div) {
249 *ext_div = DIV_ROUND_UP(no, max_no);
250 no = DIV_ROUND_UP(no, *ext_div);
251 }
252
253 /* only even divisors (and 1) are supported */
254 if (no > 1)
255 no = DIV_ROUND_UP(no, 2) * 2;
256
257 vco_khz = freq_khz * no;
258 if (ext_div)
259 vco_khz *= *ext_div;
260
261 if (vco_khz < VCO_MIN_KHZ || vco_khz > VCO_MAX_KHZ || no > max_no) {
262 printf("%s: Cannot find out a supported VCO for Frequency (%luHz).\n",
263 __func__, freq_hz);
264 return -1;
265 }
266
267 div->no = no;
268
269 best_diff_khz = vco_khz;
270 for (nr = 1; nr < max_nr && best_diff_khz; nr++) {
271 fref_khz = ref_khz / nr;
272 if (fref_khz < FREF_MIN_KHZ)
273 break;
274 if (fref_khz > FREF_MAX_KHZ)
275 continue;
276
277 nf = vco_khz / fref_khz;
278 if (nf >= max_nf)
279 continue;
280 diff_khz = vco_khz - nf * fref_khz;
281 if (nf + 1 < max_nf && diff_khz > fref_khz / 2) {
282 nf++;
283 diff_khz = fref_khz - diff_khz;
284 }
285
286 if (diff_khz >= best_diff_khz)
287 continue;
288
289 best_diff_khz = diff_khz;
290 div->nr = nr;
291 div->nf = nf;
292 }
293
294 if (best_diff_khz > 4 * 1000) {
295 printf("%s: Failed to match output frequency %lu, difference is %u Hz, exceed 4MHZ\n",
296 __func__, freq_hz, best_diff_khz * 1000);
297 return -EINVAL;
298 }
299
300 return 0;
301}
302
Jagan Teki783acfd2020-01-09 14:22:17 +0530303static int rockchip_mac_set_clk(struct rockchip_cru *cru, uint freq)
Sjoerd Simons3ce69bf2016-02-28 22:24:59 +0100304{
David Wu879d2fb2018-01-13 14:06:33 +0800305 ulong ret;
306
307 /*
308 * The gmac clock can be derived either from an external clock
309 * or can be generated from internally by a divider from SCLK_MAC.
310 */
311 if (readl(&cru->cru_clksel_con[21]) & RMII_EXTCLK_MASK) {
312 /* An external clock will always generate the right rate... */
313 ret = freq;
314 } else {
315 u32 con = readl(&cru->cru_clksel_con[21]);
316 ulong pll_rate;
317 u8 div;
318
319 if (((con >> EMAC_PLL_SHIFT) & EMAC_PLL_MASK) ==
320 EMAC_PLL_SELECT_GENERAL)
321 pll_rate = GPLL_HZ;
322 else if (((con >> EMAC_PLL_SHIFT) & EMAC_PLL_MASK) ==
323 EMAC_PLL_SELECT_CODEC)
324 pll_rate = CPLL_HZ;
325 else
326 pll_rate = NPLL_HZ;
327
328 div = DIV_ROUND_UP(pll_rate, freq) - 1;
329 if (div <= 0x1f)
330 rk_clrsetreg(&cru->cru_clksel_con[21], MAC_DIV_CON_MASK,
331 div << MAC_DIV_CON_SHIFT);
332 else
333 debug("Unsupported div for gmac:%d\n", div);
334
335 return DIV_TO_RATE(pll_rate, div);
336 }
Sjoerd Simons3ce69bf2016-02-28 22:24:59 +0100337
David Wu879d2fb2018-01-13 14:06:33 +0800338 return ret;
Sjoerd Simons3ce69bf2016-02-28 22:24:59 +0100339}
340
Jagan Teki783acfd2020-01-09 14:22:17 +0530341static int rockchip_vop_set_clk(struct rockchip_cru *cru, struct rk3288_grf *grf,
Simon Glass273afb22016-01-21 19:45:02 -0700342 int periph, unsigned int rate_hz)
343{
344 struct pll_div npll_config = {0};
345 u32 lcdc_div;
346 int ret;
347
348 ret = pll_para_config(rate_hz, &npll_config, &lcdc_div);
349 if (ret)
350 return ret;
351
Simon Glass303384f2017-05-31 17:57:31 -0600352 rk_clrsetreg(&cru->cru_mode_con, NPLL_MODE_MASK,
Simon Glass273afb22016-01-21 19:45:02 -0700353 NPLL_MODE_SLOW << NPLL_MODE_SHIFT);
354 rkclk_set_pll(cru, CLK_NEW, &npll_config);
355
356 /* waiting for pll lock */
357 while (1) {
358 if (readl(&grf->soc_status[1]) & SOCSTS_NPLL_LOCK)
359 break;
360 udelay(1);
361 }
362
Simon Glass303384f2017-05-31 17:57:31 -0600363 rk_clrsetreg(&cru->cru_mode_con, NPLL_MODE_MASK,
Simon Glass273afb22016-01-21 19:45:02 -0700364 NPLL_MODE_NORMAL << NPLL_MODE_SHIFT);
365
366 /* vop dclk source clk: npll,dclk_div: 1 */
367 switch (periph) {
368 case DCLK_VOP0:
369 rk_clrsetreg(&cru->cru_clksel_con[27], 0xff << 8 | 3 << 0,
370 (lcdc_div - 1) << 8 | 2 << 0);
371 break;
372 case DCLK_VOP1:
373 rk_clrsetreg(&cru->cru_clksel_con[29], 0xff << 8 | 3 << 6,
374 (lcdc_div - 1) << 8 | 2 << 6);
375 break;
376 }
377
378 return 0;
379}
Simon Glasscf6741b2018-12-27 20:15:20 -0700380
381static u32 rockchip_clk_gcd(u32 a, u32 b)
382{
383 while (b != 0) {
384 int r = b;
385
386 b = a % b;
387 a = r;
388 }
389 return a;
390}
391
Jagan Teki783acfd2020-01-09 14:22:17 +0530392static ulong rockchip_i2s_get_clk(struct rockchip_cru *cru, uint gclk_rate)
Simon Glasscf6741b2018-12-27 20:15:20 -0700393{
394 unsigned long long rate;
395 uint val;
396 int n, d;
397
398 val = readl(&cru->cru_clksel_con[8]);
399 n = (val & I2S0_FRAC_NUMER_MASK) >> I2S0_FRAC_NUMER_SHIFT;
400 d = (val & I2S0_FRAC_DENOM_MASK) >> I2S0_FRAC_DENOM_SHIFT;
401
402 rate = (unsigned long long)gclk_rate * n;
403 do_div(rate, d);
404
405 return (ulong)rate;
406}
407
Jagan Teki783acfd2020-01-09 14:22:17 +0530408static ulong rockchip_i2s_set_clk(struct rockchip_cru *cru, uint gclk_rate,
Simon Glasscf6741b2018-12-27 20:15:20 -0700409 uint freq)
410{
411 int n, d;
412 int v;
413
414 /* set frac divider */
415 v = rockchip_clk_gcd(gclk_rate, freq);
416 n = gclk_rate / v;
417 d = freq / v;
418 assert(freq == gclk_rate / n * d);
419 writel(d << I2S0_FRAC_NUMER_SHIFT | n << I2S0_FRAC_DENOM_SHIFT,
420 &cru->cru_clksel_con[8]);
421
422 return rockchip_i2s_get_clk(cru, gclk_rate);
423}
Simon Glass30ca6a42017-05-31 17:57:32 -0600424#endif /* CONFIG_SPL_BUILD */
Simon Glass273afb22016-01-21 19:45:02 -0700425
Jagan Teki783acfd2020-01-09 14:22:17 +0530426static void rkclk_init(struct rockchip_cru *cru, struct rk3288_grf *grf)
Simon Glass421358c2015-08-30 16:55:31 -0600427{
428 u32 aclk_div;
429 u32 hclk_div;
430 u32 pclk_div;
431
432 /* pll enter slow-mode */
433 rk_clrsetreg(&cru->cru_mode_con,
Simon Glass303384f2017-05-31 17:57:31 -0600434 GPLL_MODE_MASK | CPLL_MODE_MASK,
Simon Glass421358c2015-08-30 16:55:31 -0600435 GPLL_MODE_SLOW << GPLL_MODE_SHIFT |
436 CPLL_MODE_SLOW << CPLL_MODE_SHIFT);
437
438 /* init pll */
439 rkclk_set_pll(cru, CLK_GENERAL, &gpll_init_cfg);
440 rkclk_set_pll(cru, CLK_CODEC, &cpll_init_cfg);
441
442 /* waiting for pll lock */
443 while ((readl(&grf->soc_status[1]) &
444 (SOCSTS_CPLL_LOCK | SOCSTS_GPLL_LOCK)) !=
445 (SOCSTS_CPLL_LOCK | SOCSTS_GPLL_LOCK))
446 udelay(1);
447
448 /*
449 * pd_bus clock pll source selection and
450 * set up dependent divisors for PCLK/HCLK and ACLK clocks.
451 */
452 aclk_div = GPLL_HZ / PD_BUS_ACLK_HZ - 1;
453 assert((aclk_div + 1) * PD_BUS_ACLK_HZ == GPLL_HZ && aclk_div < 0x1f);
454 hclk_div = PD_BUS_ACLK_HZ / PD_BUS_HCLK_HZ - 1;
455 assert((hclk_div + 1) * PD_BUS_HCLK_HZ ==
456 PD_BUS_ACLK_HZ && (hclk_div < 0x4) && (hclk_div != 0x2));
457
458 pclk_div = PD_BUS_ACLK_HZ / PD_BUS_PCLK_HZ - 1;
459 assert((pclk_div + 1) * PD_BUS_PCLK_HZ ==
460 PD_BUS_ACLK_HZ && pclk_div < 0x7);
461
462 rk_clrsetreg(&cru->cru_clksel_con[1],
Simon Glass303384f2017-05-31 17:57:31 -0600463 PD_BUS_PCLK_DIV_MASK | PD_BUS_HCLK_DIV_MASK |
464 PD_BUS_ACLK_DIV0_MASK | PD_BUS_ACLK_DIV1_MASK,
Simon Glass421358c2015-08-30 16:55:31 -0600465 pclk_div << PD_BUS_PCLK_DIV_SHIFT |
466 hclk_div << PD_BUS_HCLK_DIV_SHIFT |
467 aclk_div << PD_BUS_ACLK_DIV0_SHIFT |
468 0 << 0);
469
470 /*
471 * peri clock pll source selection and
472 * set up dependent divisors for PCLK/HCLK and ACLK clocks.
473 */
474 aclk_div = GPLL_HZ / PERI_ACLK_HZ - 1;
475 assert((aclk_div + 1) * PERI_ACLK_HZ == GPLL_HZ && aclk_div < 0x1f);
476
Heiko Stübner1b7dcc32016-07-22 23:51:06 +0200477 hclk_div = ilog2(PERI_ACLK_HZ / PERI_HCLK_HZ);
Simon Glass421358c2015-08-30 16:55:31 -0600478 assert((1 << hclk_div) * PERI_HCLK_HZ ==
479 PERI_ACLK_HZ && (hclk_div < 0x4));
480
Heiko Stübner1b7dcc32016-07-22 23:51:06 +0200481 pclk_div = ilog2(PERI_ACLK_HZ / PERI_PCLK_HZ);
Simon Glass421358c2015-08-30 16:55:31 -0600482 assert((1 << pclk_div) * PERI_PCLK_HZ ==
483 PERI_ACLK_HZ && (pclk_div < 0x4));
484
485 rk_clrsetreg(&cru->cru_clksel_con[10],
Simon Glass303384f2017-05-31 17:57:31 -0600486 PERI_PCLK_DIV_MASK | PERI_HCLK_DIV_MASK |
487 PERI_ACLK_DIV_MASK,
Simon Glasse6a682b2016-01-21 19:45:15 -0700488 PERI_SEL_GPLL << PERI_SEL_PLL_SHIFT |
Simon Glass421358c2015-08-30 16:55:31 -0600489 pclk_div << PERI_PCLK_DIV_SHIFT |
490 hclk_div << PERI_HCLK_DIV_SHIFT |
491 aclk_div << PERI_ACLK_DIV_SHIFT);
492
493 /* PLL enter normal-mode */
494 rk_clrsetreg(&cru->cru_mode_con,
Simon Glass303384f2017-05-31 17:57:31 -0600495 GPLL_MODE_MASK | CPLL_MODE_MASK,
Simon Glass5562bf12016-01-21 19:45:01 -0700496 GPLL_MODE_NORMAL << GPLL_MODE_SHIFT |
497 CPLL_MODE_NORMAL << CPLL_MODE_SHIFT);
Simon Glass421358c2015-08-30 16:55:31 -0600498}
Simon Glass421358c2015-08-30 16:55:31 -0600499
Jagan Teki783acfd2020-01-09 14:22:17 +0530500void rk3288_clk_configure_cpu(struct rockchip_cru *cru, struct rk3288_grf *grf)
Simon Glass94906e42016-01-21 19:45:17 -0700501{
502 /* pll enter slow-mode */
Simon Glass303384f2017-05-31 17:57:31 -0600503 rk_clrsetreg(&cru->cru_mode_con, APLL_MODE_MASK,
Simon Glass94906e42016-01-21 19:45:17 -0700504 APLL_MODE_SLOW << APLL_MODE_SHIFT);
505
506 rkclk_set_pll(cru, CLK_ARM, &apll_init_cfg);
507
508 /* waiting for pll lock */
509 while (!(readl(&grf->soc_status[1]) & SOCSTS_APLL_LOCK))
510 udelay(1);
511
512 /*
513 * core clock pll source selection and
514 * set up dependent divisors for MPAXI/M0AXI and ARM clocks.
515 * core clock select apll, apll clk = 1800MHz
516 * arm clk = 1800MHz, mpclk = 450MHz, m0clk = 900MHz
517 */
518 rk_clrsetreg(&cru->cru_clksel_con[0],
Simon Glass303384f2017-05-31 17:57:31 -0600519 CORE_SEL_PLL_MASK | A17_DIV_MASK | MP_DIV_MASK |
520 M0_DIV_MASK,
Simon Glass94906e42016-01-21 19:45:17 -0700521 0 << A17_DIV_SHIFT |
522 3 << MP_DIV_SHIFT |
523 1 << M0_DIV_SHIFT);
524
525 /*
526 * set up dependent divisors for L2RAM/ATCLK and PCLK clocks.
527 * l2ramclk = 900MHz, atclk = 450MHz, pclk_dbg = 450MHz
528 */
529 rk_clrsetreg(&cru->cru_clksel_con[37],
Simon Glass303384f2017-05-31 17:57:31 -0600530 CLK_L2RAM_DIV_MASK | ATCLK_CORE_DIV_CON_MASK |
531 PCLK_CORE_DBG_DIV_MASK,
Simon Glass94906e42016-01-21 19:45:17 -0700532 1 << CLK_L2RAM_DIV_SHIFT |
533 3 << ATCLK_CORE_DIV_CON_SHIFT |
534 3 << PCLK_CORE_DBG_DIV_SHIFT);
535
536 /* PLL enter normal-mode */
Simon Glass303384f2017-05-31 17:57:31 -0600537 rk_clrsetreg(&cru->cru_mode_con, APLL_MODE_MASK,
Simon Glass94906e42016-01-21 19:45:17 -0700538 APLL_MODE_NORMAL << APLL_MODE_SHIFT);
539}
540
Simon Glass421358c2015-08-30 16:55:31 -0600541/* Get pll rate by id */
Jagan Teki783acfd2020-01-09 14:22:17 +0530542static uint32_t rkclk_pll_get_rate(struct rockchip_cru *cru,
Simon Glass421358c2015-08-30 16:55:31 -0600543 enum rk_clk_id clk_id)
544{
545 uint32_t nr, no, nf;
546 uint32_t con;
547 int pll_id = rk_pll_id(clk_id);
548 struct rk3288_pll *pll = &cru->pll[pll_id];
549 static u8 clk_shift[CLK_COUNT] = {
Simon Glass5562bf12016-01-21 19:45:01 -0700550 0xff, APLL_MODE_SHIFT, DPLL_MODE_SHIFT, CPLL_MODE_SHIFT,
551 GPLL_MODE_SHIFT, NPLL_MODE_SHIFT
Simon Glass421358c2015-08-30 16:55:31 -0600552 };
553 uint shift;
554
555 con = readl(&cru->cru_mode_con);
556 shift = clk_shift[clk_id];
Simon Glass303384f2017-05-31 17:57:31 -0600557 switch ((con >> shift) & CRU_MODE_MASK) {
Simon Glass5562bf12016-01-21 19:45:01 -0700558 case APLL_MODE_SLOW:
Simon Glass421358c2015-08-30 16:55:31 -0600559 return OSC_HZ;
Simon Glass5562bf12016-01-21 19:45:01 -0700560 case APLL_MODE_NORMAL:
Simon Glass421358c2015-08-30 16:55:31 -0600561 /* normal mode */
562 con = readl(&pll->con0);
Simon Glass303384f2017-05-31 17:57:31 -0600563 no = ((con & CLKOD_MASK) >> CLKOD_SHIFT) + 1;
564 nr = ((con & CLKR_MASK) >> CLKR_SHIFT) + 1;
Simon Glass421358c2015-08-30 16:55:31 -0600565 con = readl(&pll->con1);
Simon Glass303384f2017-05-31 17:57:31 -0600566 nf = ((con & CLKF_MASK) >> CLKF_SHIFT) + 1;
Simon Glass421358c2015-08-30 16:55:31 -0600567
568 return (24 * nf / (nr * no)) * 1000000;
Simon Glass5562bf12016-01-21 19:45:01 -0700569 case APLL_MODE_DEEP:
Simon Glass421358c2015-08-30 16:55:31 -0600570 default:
571 return 32768;
572 }
573}
574
Jagan Teki783acfd2020-01-09 14:22:17 +0530575static ulong rockchip_mmc_get_clk(struct rockchip_cru *cru, uint gclk_rate,
Simon Glass8d32f4b2016-01-21 19:43:38 -0700576 int periph)
Simon Glass421358c2015-08-30 16:55:31 -0600577{
578 uint src_rate;
579 uint div, mux;
580 u32 con;
581
582 switch (periph) {
Simon Glass8d32f4b2016-01-21 19:43:38 -0700583 case HCLK_EMMC:
Xu Ziyuan42118e42017-04-16 17:44:45 +0800584 case SCLK_EMMC:
Simon Glass421358c2015-08-30 16:55:31 -0600585 con = readl(&cru->cru_clksel_con[12]);
Simon Glass303384f2017-05-31 17:57:31 -0600586 mux = (con & EMMC_PLL_MASK) >> EMMC_PLL_SHIFT;
587 div = (con & EMMC_DIV_MASK) >> EMMC_DIV_SHIFT;
Simon Glass421358c2015-08-30 16:55:31 -0600588 break;
Simon Glass8d32f4b2016-01-21 19:43:38 -0700589 case HCLK_SDMMC:
Xu Ziyuan42118e42017-04-16 17:44:45 +0800590 case SCLK_SDMMC:
Simon Glass8d32f4b2016-01-21 19:43:38 -0700591 con = readl(&cru->cru_clksel_con[11]);
Simon Glass303384f2017-05-31 17:57:31 -0600592 mux = (con & MMC0_PLL_MASK) >> MMC0_PLL_SHIFT;
593 div = (con & MMC0_DIV_MASK) >> MMC0_DIV_SHIFT;
Simon Glass421358c2015-08-30 16:55:31 -0600594 break;
Simon Glass8d32f4b2016-01-21 19:43:38 -0700595 case HCLK_SDIO0:
Xu Ziyuan42118e42017-04-16 17:44:45 +0800596 case SCLK_SDIO0:
Simon Glass421358c2015-08-30 16:55:31 -0600597 con = readl(&cru->cru_clksel_con[12]);
Simon Glass303384f2017-05-31 17:57:31 -0600598 mux = (con & SDIO0_PLL_MASK) >> SDIO0_PLL_SHIFT;
599 div = (con & SDIO0_DIV_MASK) >> SDIO0_DIV_SHIFT;
Simon Glass421358c2015-08-30 16:55:31 -0600600 break;
601 default:
602 return -EINVAL;
603 }
604
Simon Glassafe0cb02016-01-21 19:43:39 -0700605 src_rate = mux == EMMC_PLL_SELECT_24MHZ ? OSC_HZ : gclk_rate;
Simon Glass421358c2015-08-30 16:55:31 -0600606 return DIV_TO_RATE(src_rate, div);
607}
608
Jagan Teki783acfd2020-01-09 14:22:17 +0530609static ulong rockchip_mmc_set_clk(struct rockchip_cru *cru, uint gclk_rate,
Simon Glass8d32f4b2016-01-21 19:43:38 -0700610 int periph, uint freq)
Simon Glass421358c2015-08-30 16:55:31 -0600611{
612 int src_clk_div;
613 int mux;
614
Simon Glassafe0cb02016-01-21 19:43:39 -0700615 debug("%s: gclk_rate=%u\n", __func__, gclk_rate);
Kever Yang99b546d2017-07-27 12:54:01 +0800616 /* mmc clock default div 2 internal, need provide double in cru */
617 src_clk_div = DIV_ROUND_UP(gclk_rate / 2, freq);
Simon Glass421358c2015-08-30 16:55:31 -0600618
619 if (src_clk_div > 0x3f) {
Kever Yang99b546d2017-07-27 12:54:01 +0800620 src_clk_div = DIV_ROUND_UP(OSC_HZ / 2, freq);
Kever Yangf20995b2017-07-27 12:54:02 +0800621 assert(src_clk_div < 0x40);
Simon Glass421358c2015-08-30 16:55:31 -0600622 mux = EMMC_PLL_SELECT_24MHZ;
623 assert((int)EMMC_PLL_SELECT_24MHZ ==
624 (int)MMC0_PLL_SELECT_24MHZ);
625 } else {
626 mux = EMMC_PLL_SELECT_GENERAL;
627 assert((int)EMMC_PLL_SELECT_GENERAL ==
628 (int)MMC0_PLL_SELECT_GENERAL);
629 }
630 switch (periph) {
Simon Glass8d32f4b2016-01-21 19:43:38 -0700631 case HCLK_EMMC:
Xu Ziyuan42118e42017-04-16 17:44:45 +0800632 case SCLK_EMMC:
Simon Glass421358c2015-08-30 16:55:31 -0600633 rk_clrsetreg(&cru->cru_clksel_con[12],
Simon Glass303384f2017-05-31 17:57:31 -0600634 EMMC_PLL_MASK | EMMC_DIV_MASK,
Simon Glass421358c2015-08-30 16:55:31 -0600635 mux << EMMC_PLL_SHIFT |
636 (src_clk_div - 1) << EMMC_DIV_SHIFT);
637 break;
Simon Glass8d32f4b2016-01-21 19:43:38 -0700638 case HCLK_SDMMC:
Xu Ziyuan42118e42017-04-16 17:44:45 +0800639 case SCLK_SDMMC:
Simon Glass421358c2015-08-30 16:55:31 -0600640 rk_clrsetreg(&cru->cru_clksel_con[11],
Simon Glass303384f2017-05-31 17:57:31 -0600641 MMC0_PLL_MASK | MMC0_DIV_MASK,
Simon Glass421358c2015-08-30 16:55:31 -0600642 mux << MMC0_PLL_SHIFT |
643 (src_clk_div - 1) << MMC0_DIV_SHIFT);
644 break;
Simon Glass8d32f4b2016-01-21 19:43:38 -0700645 case HCLK_SDIO0:
Xu Ziyuan42118e42017-04-16 17:44:45 +0800646 case SCLK_SDIO0:
Simon Glass421358c2015-08-30 16:55:31 -0600647 rk_clrsetreg(&cru->cru_clksel_con[12],
Simon Glass303384f2017-05-31 17:57:31 -0600648 SDIO0_PLL_MASK | SDIO0_DIV_MASK,
Simon Glass421358c2015-08-30 16:55:31 -0600649 mux << SDIO0_PLL_SHIFT |
650 (src_clk_div - 1) << SDIO0_DIV_SHIFT);
651 break;
652 default:
653 return -EINVAL;
654 }
655
Simon Glassafe0cb02016-01-21 19:43:39 -0700656 return rockchip_mmc_get_clk(cru, gclk_rate, periph);
Simon Glass421358c2015-08-30 16:55:31 -0600657}
658
Jagan Teki783acfd2020-01-09 14:22:17 +0530659static ulong rockchip_spi_get_clk(struct rockchip_cru *cru, uint gclk_rate,
Simon Glass8d32f4b2016-01-21 19:43:38 -0700660 int periph)
Simon Glass421358c2015-08-30 16:55:31 -0600661{
662 uint div, mux;
663 u32 con;
664
665 switch (periph) {
Simon Glass8d32f4b2016-01-21 19:43:38 -0700666 case SCLK_SPI0:
Simon Glass421358c2015-08-30 16:55:31 -0600667 con = readl(&cru->cru_clksel_con[25]);
Simon Glass303384f2017-05-31 17:57:31 -0600668 mux = (con & SPI0_PLL_MASK) >> SPI0_PLL_SHIFT;
669 div = (con & SPI0_DIV_MASK) >> SPI0_DIV_SHIFT;
Simon Glass421358c2015-08-30 16:55:31 -0600670 break;
Simon Glass8d32f4b2016-01-21 19:43:38 -0700671 case SCLK_SPI1:
Simon Glass421358c2015-08-30 16:55:31 -0600672 con = readl(&cru->cru_clksel_con[25]);
Simon Glass303384f2017-05-31 17:57:31 -0600673 mux = (con & SPI1_PLL_MASK) >> SPI1_PLL_SHIFT;
674 div = (con & SPI1_DIV_MASK) >> SPI1_DIV_SHIFT;
Simon Glass421358c2015-08-30 16:55:31 -0600675 break;
Simon Glass8d32f4b2016-01-21 19:43:38 -0700676 case SCLK_SPI2:
Simon Glass421358c2015-08-30 16:55:31 -0600677 con = readl(&cru->cru_clksel_con[39]);
Simon Glass303384f2017-05-31 17:57:31 -0600678 mux = (con & SPI2_PLL_MASK) >> SPI2_PLL_SHIFT;
679 div = (con & SPI2_DIV_MASK) >> SPI2_DIV_SHIFT;
Simon Glass421358c2015-08-30 16:55:31 -0600680 break;
681 default:
682 return -EINVAL;
683 }
684 assert(mux == SPI0_PLL_SELECT_GENERAL);
685
Simon Glassafe0cb02016-01-21 19:43:39 -0700686 return DIV_TO_RATE(gclk_rate, div);
Simon Glass421358c2015-08-30 16:55:31 -0600687}
688
Jagan Teki783acfd2020-01-09 14:22:17 +0530689static ulong rockchip_spi_set_clk(struct rockchip_cru *cru, uint gclk_rate,
Simon Glass8d32f4b2016-01-21 19:43:38 -0700690 int periph, uint freq)
Simon Glass421358c2015-08-30 16:55:31 -0600691{
692 int src_clk_div;
693
Simon Glassafe0cb02016-01-21 19:43:39 -0700694 debug("%s: clk_general_rate=%u\n", __func__, gclk_rate);
Kever Yangf20995b2017-07-27 12:54:02 +0800695 src_clk_div = DIV_ROUND_UP(gclk_rate, freq) - 1;
696 assert(src_clk_div < 128);
Simon Glass421358c2015-08-30 16:55:31 -0600697 switch (periph) {
Simon Glass8d32f4b2016-01-21 19:43:38 -0700698 case SCLK_SPI0:
Simon Glass421358c2015-08-30 16:55:31 -0600699 rk_clrsetreg(&cru->cru_clksel_con[25],
Simon Glass303384f2017-05-31 17:57:31 -0600700 SPI0_PLL_MASK | SPI0_DIV_MASK,
Simon Glass421358c2015-08-30 16:55:31 -0600701 SPI0_PLL_SELECT_GENERAL << SPI0_PLL_SHIFT |
702 src_clk_div << SPI0_DIV_SHIFT);
703 break;
Simon Glass8d32f4b2016-01-21 19:43:38 -0700704 case SCLK_SPI1:
Simon Glass421358c2015-08-30 16:55:31 -0600705 rk_clrsetreg(&cru->cru_clksel_con[25],
Simon Glass303384f2017-05-31 17:57:31 -0600706 SPI1_PLL_MASK | SPI1_DIV_MASK,
Simon Glass421358c2015-08-30 16:55:31 -0600707 SPI1_PLL_SELECT_GENERAL << SPI1_PLL_SHIFT |
708 src_clk_div << SPI1_DIV_SHIFT);
709 break;
Simon Glass8d32f4b2016-01-21 19:43:38 -0700710 case SCLK_SPI2:
Simon Glass421358c2015-08-30 16:55:31 -0600711 rk_clrsetreg(&cru->cru_clksel_con[39],
Simon Glass303384f2017-05-31 17:57:31 -0600712 SPI2_PLL_MASK | SPI2_DIV_MASK,
Simon Glass421358c2015-08-30 16:55:31 -0600713 SPI2_PLL_SELECT_GENERAL << SPI2_PLL_SHIFT |
714 src_clk_div << SPI2_DIV_SHIFT);
715 break;
716 default:
717 return -EINVAL;
718 }
719
Simon Glassafe0cb02016-01-21 19:43:39 -0700720 return rockchip_spi_get_clk(cru, gclk_rate, periph);
Simon Glass421358c2015-08-30 16:55:31 -0600721}
722
Jagan Teki783acfd2020-01-09 14:22:17 +0530723static ulong rockchip_saradc_get_clk(struct rockchip_cru *cru)
David Wu3c248b22017-09-20 14:28:19 +0800724{
725 u32 div, val;
726
727 val = readl(&cru->cru_clksel_con[24]);
728 div = bitfield_extract(val, CLK_SARADC_DIV_CON_SHIFT,
729 CLK_SARADC_DIV_CON_WIDTH);
730
731 return DIV_TO_RATE(OSC_HZ, div);
732}
733
Jagan Teki783acfd2020-01-09 14:22:17 +0530734static ulong rockchip_saradc_set_clk(struct rockchip_cru *cru, uint hz)
David Wu3c248b22017-09-20 14:28:19 +0800735{
736 int src_clk_div;
737
738 src_clk_div = DIV_ROUND_UP(OSC_HZ, hz) - 1;
739 assert(src_clk_div < 128);
740
741 rk_clrsetreg(&cru->cru_clksel_con[24],
742 CLK_SARADC_DIV_CON_MASK,
743 src_clk_div << CLK_SARADC_DIV_CON_SHIFT);
744
745 return rockchip_saradc_get_clk(cru);
746}
747
Stephen Warrena9622432016-06-17 09:44:00 -0600748static ulong rk3288_clk_get_rate(struct clk *clk)
Simon Glass398ced12016-01-21 19:43:40 -0700749{
Stephen Warrena9622432016-06-17 09:44:00 -0600750 struct rk3288_clk_priv *priv = dev_get_priv(clk->dev);
Simon Glass398ced12016-01-21 19:43:40 -0700751 ulong new_rate, gclk_rate;
Simon Glass398ced12016-01-21 19:43:40 -0700752
Stephen Warrena9622432016-06-17 09:44:00 -0600753 gclk_rate = rkclk_pll_get_rate(priv->cru, CLK_GENERAL);
754 switch (clk->id) {
755 case 0 ... 63:
756 new_rate = rkclk_pll_get_rate(priv->cru, clk->id);
757 break;
Simon Glass398ced12016-01-21 19:43:40 -0700758 case HCLK_EMMC:
Simon Glassd4a8a682016-01-21 19:43:45 -0700759 case HCLK_SDMMC:
Simon Glass398ced12016-01-21 19:43:40 -0700760 case HCLK_SDIO0:
Xu Ziyuan42118e42017-04-16 17:44:45 +0800761 case SCLK_EMMC:
762 case SCLK_SDMMC:
763 case SCLK_SDIO0:
Stephen Warrena9622432016-06-17 09:44:00 -0600764 new_rate = rockchip_mmc_get_clk(priv->cru, gclk_rate, clk->id);
Simon Glass398ced12016-01-21 19:43:40 -0700765 break;
766 case SCLK_SPI0:
767 case SCLK_SPI1:
768 case SCLK_SPI2:
Stephen Warrena9622432016-06-17 09:44:00 -0600769 new_rate = rockchip_spi_get_clk(priv->cru, gclk_rate, clk->id);
Simon Glass398ced12016-01-21 19:43:40 -0700770 break;
771 case PCLK_I2C0:
772 case PCLK_I2C1:
773 case PCLK_I2C2:
774 case PCLK_I2C3:
775 case PCLK_I2C4:
776 case PCLK_I2C5:
777 return gclk_rate;
Kever Yang40514622016-08-12 17:57:05 +0800778 case PCLK_PWM:
Johan Jonker76452692023-03-15 19:34:13 +0100779 case PCLK_RKPWM:
Kever Yang40514622016-08-12 17:57:05 +0800780 return PD_BUS_PCLK_HZ;
David Wu3c248b22017-09-20 14:28:19 +0800781 case SCLK_SARADC:
782 new_rate = rockchip_saradc_get_clk(priv->cru);
783 break;
Simon Glass398ced12016-01-21 19:43:40 -0700784 default:
785 return -ENOENT;
786 }
787
788 return new_rate;
789}
790
Stephen Warrena9622432016-06-17 09:44:00 -0600791static ulong rk3288_clk_set_rate(struct clk *clk, ulong rate)
Simon Glass421358c2015-08-30 16:55:31 -0600792{
Stephen Warrena9622432016-06-17 09:44:00 -0600793 struct rk3288_clk_priv *priv = dev_get_priv(clk->dev);
Jagan Teki783acfd2020-01-09 14:22:17 +0530794 struct rockchip_cru *cru = priv->cru;
Simon Glass8d32f4b2016-01-21 19:43:38 -0700795 ulong new_rate, gclk_rate;
Simon Glass421358c2015-08-30 16:55:31 -0600796
Stephen Warrena9622432016-06-17 09:44:00 -0600797 gclk_rate = rkclk_pll_get_rate(priv->cru, CLK_GENERAL);
798 switch (clk->id) {
Simon Glasscb1c7af2016-11-13 14:22:13 -0700799 case PLL_APLL:
800 /* We only support a fixed rate here */
801 if (rate != 1800000000)
802 return -EINVAL;
803 rk3288_clk_configure_cpu(priv->cru, priv->grf);
804 new_rate = rate;
805 break;
Stephen Warrena9622432016-06-17 09:44:00 -0600806 case CLK_DDR:
807 new_rate = rkclk_configure_ddr(priv->cru, priv->grf, rate);
808 break;
Simon Glass8d32f4b2016-01-21 19:43:38 -0700809 case HCLK_EMMC:
810 case HCLK_SDMMC:
811 case HCLK_SDIO0:
Xu Ziyuan42118e42017-04-16 17:44:45 +0800812 case SCLK_EMMC:
813 case SCLK_SDMMC:
814 case SCLK_SDIO0:
Stephen Warrena9622432016-06-17 09:44:00 -0600815 new_rate = rockchip_mmc_set_clk(cru, gclk_rate, clk->id, rate);
Simon Glass421358c2015-08-30 16:55:31 -0600816 break;
Simon Glass8d32f4b2016-01-21 19:43:38 -0700817 case SCLK_SPI0:
818 case SCLK_SPI1:
819 case SCLK_SPI2:
Stephen Warrena9622432016-06-17 09:44:00 -0600820 new_rate = rockchip_spi_set_clk(cru, gclk_rate, clk->id, rate);
Simon Glass273afb22016-01-21 19:45:02 -0700821 break;
822#ifndef CONFIG_SPL_BUILD
Simon Glasscf6741b2018-12-27 20:15:20 -0700823 case SCLK_I2S0:
824 new_rate = rockchip_i2s_set_clk(cru, gclk_rate, rate);
825 break;
Sjoerd Simons3ce69bf2016-02-28 22:24:59 +0100826 case SCLK_MAC:
David Wu879d2fb2018-01-13 14:06:33 +0800827 new_rate = rockchip_mac_set_clk(priv->cru, rate);
Sjoerd Simons3ce69bf2016-02-28 22:24:59 +0100828 break;
Simon Glass273afb22016-01-21 19:45:02 -0700829 case DCLK_VOP0:
830 case DCLK_VOP1:
Stephen Warrena9622432016-06-17 09:44:00 -0600831 new_rate = rockchip_vop_set_clk(cru, priv->grf, clk->id, rate);
Simon Glass273afb22016-01-21 19:45:02 -0700832 break;
833 case SCLK_EDP_24M:
834 /* clk_edp_24M source: 24M */
835 rk_setreg(&cru->cru_clksel_con[28], 1 << 15);
836
837 /* rst edp */
838 rk_setreg(&cru->cru_clksel_con[6], 1 << 15);
839 udelay(1);
840 rk_clrreg(&cru->cru_clksel_con[6], 1 << 15);
841 new_rate = rate;
842 break;
843 case ACLK_VOP0:
844 case ACLK_VOP1: {
845 u32 div;
846
847 /* vop aclk source clk: cpll */
848 div = CPLL_HZ / rate;
849 assert((div - 1 < 64) && (div * rate == CPLL_HZ));
850
Stephen Warrena9622432016-06-17 09:44:00 -0600851 switch (clk->id) {
Simon Glass273afb22016-01-21 19:45:02 -0700852 case ACLK_VOP0:
853 rk_clrsetreg(&cru->cru_clksel_con[31],
854 3 << 6 | 0x1f << 0,
855 0 << 6 | (div - 1) << 0);
856 break;
857 case ACLK_VOP1:
858 rk_clrsetreg(&cru->cru_clksel_con[31],
859 3 << 14 | 0x1f << 8,
860 0 << 14 | (div - 1) << 8);
861 break;
862 }
863 new_rate = rate;
Simon Glass421358c2015-08-30 16:55:31 -0600864 break;
Simon Glass273afb22016-01-21 19:45:02 -0700865 }
866 case PCLK_HDMI_CTRL:
867 /* enable pclk hdmi ctrl */
868 rk_clrreg(&cru->cru_clkgate_con[16], 1 << 9);
869
870 /* software reset hdmi */
871 rk_setreg(&cru->cru_clkgate_con[7], 1 << 9);
872 udelay(1);
873 rk_clrreg(&cru->cru_clkgate_con[7], 1 << 9);
874 new_rate = rate;
875 break;
876#endif
David Wu3c248b22017-09-20 14:28:19 +0800877 case SCLK_SARADC:
878 new_rate = rockchip_saradc_set_clk(priv->cru, rate);
879 break;
David Wu879d2fb2018-01-13 14:06:33 +0800880 case PLL_GPLL:
881 case PLL_CPLL:
882 case PLL_NPLL:
883 case ACLK_CPU:
884 case HCLK_CPU:
885 case PCLK_CPU:
886 case ACLK_PERI:
887 case HCLK_PERI:
888 case PCLK_PERI:
889 case SCLK_UART0:
890 return 0;
Simon Glass421358c2015-08-30 16:55:31 -0600891 default:
892 return -ENOENT;
893 }
894
895 return new_rate;
896}
897
Philipp Tomsich6dd2fb42018-01-25 15:27:10 +0100898static int __maybe_unused rk3288_gmac_set_parent(struct clk *clk, struct clk *parent)
David Wu879d2fb2018-01-13 14:06:33 +0800899{
900 struct rk3288_clk_priv *priv = dev_get_priv(clk->dev);
Jagan Teki783acfd2020-01-09 14:22:17 +0530901 struct rockchip_cru *cru = priv->cru;
David Wu879d2fb2018-01-13 14:06:33 +0800902 const char *clock_output_name;
903 int ret;
904
905 /*
906 * If the requested parent is in the same clock-controller and
907 * the id is SCLK_MAC_PLL ("mac_pll_src"), switch to the internal
908 * clock.
909 */
910 if ((parent->dev == clk->dev) && (parent->id == SCLK_MAC_PLL)) {
911 debug("%s: switching GAMC to SCLK_MAC_PLL\n", __func__);
912 rk_clrsetreg(&cru->cru_clksel_con[21], RMII_EXTCLK_MASK, 0);
913 return 0;
914 }
915
916 /*
917 * Otherwise, we need to check the clock-output-names of the
918 * requested parent to see if the requested id is "ext_gmac".
919 */
920 ret = dev_read_string_index(parent->dev, "clock-output-names",
921 parent->id, &clock_output_name);
922 if (ret < 0)
923 return -ENODATA;
924
925 /* If this is "ext_gmac", switch to the external clock input */
926 if (!strcmp(clock_output_name, "ext_gmac")) {
927 debug("%s: switching GMAC to external clock\n", __func__);
928 rk_clrsetreg(&cru->cru_clksel_con[21], RMII_EXTCLK_MASK,
929 RMII_EXTCLK_SELECT_EXT_CLK << RMII_EXTCLK_SHIFT);
930 return 0;
931 }
932
933 return -EINVAL;
934}
935
Philipp Tomsich6dd2fb42018-01-25 15:27:10 +0100936static int __maybe_unused rk3288_clk_set_parent(struct clk *clk, struct clk *parent)
David Wu879d2fb2018-01-13 14:06:33 +0800937{
938 switch (clk->id) {
939 case SCLK_MAC:
940 return rk3288_gmac_set_parent(clk, parent);
941 case SCLK_USBPHY480M_SRC:
942 return 0;
943 }
944
945 debug("%s: unsupported clk %ld\n", __func__, clk->id);
946 return -ENOENT;
947}
948
Simon Glass421358c2015-08-30 16:55:31 -0600949static struct clk_ops rk3288_clk_ops = {
950 .get_rate = rk3288_clk_get_rate,
951 .set_rate = rk3288_clk_set_rate,
Simon Glass3580f6d2021-08-07 07:24:03 -0600952#if CONFIG_IS_ENABLED(OF_REAL)
David Wu879d2fb2018-01-13 14:06:33 +0800953 .set_parent = rk3288_clk_set_parent,
Philipp Tomsich6dd2fb42018-01-25 15:27:10 +0100954#endif
Simon Glass421358c2015-08-30 16:55:31 -0600955};
956
Simon Glassaad29ae2020-12-03 16:55:21 -0700957static int rk3288_clk_of_to_plat(struct udevice *dev)
Simon Glass421358c2015-08-30 16:55:31 -0600958{
Simon Glass6d70ba02021-08-07 07:24:06 -0600959 if (CONFIG_IS_ENABLED(OF_REAL)) {
960 struct rk3288_clk_priv *priv = dev_get_priv(dev);
Simon Glass421358c2015-08-30 16:55:31 -0600961
Simon Glass6d70ba02021-08-07 07:24:06 -0600962 priv->cru = dev_read_addr_ptr(dev);
963 }
Simon Glass994c29d2016-07-04 11:58:28 -0600964
965 return 0;
966}
967
968static int rk3288_clk_probe(struct udevice *dev)
969{
970 struct rk3288_clk_priv *priv = dev_get_priv(dev);
Simon Glass30ca6a42017-05-31 17:57:32 -0600971 bool init_clocks = false;
Simon Glass994c29d2016-07-04 11:58:28 -0600972
Simon Glass421358c2015-08-30 16:55:31 -0600973 priv->grf = syscon_get_first_range(ROCKCHIP_SYSCON_GRF);
Simon Glass994c29d2016-07-04 11:58:28 -0600974 if (IS_ERR(priv->grf))
975 return PTR_ERR(priv->grf);
Simon Glass421358c2015-08-30 16:55:31 -0600976#ifdef CONFIG_SPL_BUILD
Simon Glass00c5fd42016-07-04 11:58:29 -0600977#if CONFIG_IS_ENABLED(OF_PLATDATA)
Simon Glassfa20e932020-12-03 16:55:20 -0700978 struct rk3288_clk_plat *plat = dev_get_plat(dev);
Simon Glass00c5fd42016-07-04 11:58:29 -0600979
980 priv->cru = map_sysmem(plat->dtd.reg[0], plat->dtd.reg[1]);
981#endif
Simon Glass30ca6a42017-05-31 17:57:32 -0600982 init_clocks = true;
Simon Glass421358c2015-08-30 16:55:31 -0600983#endif
Simon Glass30ca6a42017-05-31 17:57:32 -0600984 if (!(gd->flags & GD_FLG_RELOC)) {
985 u32 reg;
986
987 /*
988 * Init clocks in U-Boot proper if the NPLL is runnning. This
989 * indicates that a previous boot loader set up the clocks, so
990 * we need to redo it. U-Boot's SPL does not set this clock.
991 */
992 reg = readl(&priv->cru->cru_mode_con);
993 if (((reg & NPLL_MODE_MASK) >> NPLL_MODE_SHIFT) ==
994 NPLL_MODE_NORMAL)
995 init_clocks = true;
996 }
997
998 if (init_clocks)
999 rkclk_init(priv->cru, priv->grf);
Simon Glass421358c2015-08-30 16:55:31 -06001000
1001 return 0;
1002}
1003
Simon Glass421358c2015-08-30 16:55:31 -06001004static int rk3288_clk_bind(struct udevice *dev)
1005{
Stephen Warrena9622432016-06-17 09:44:00 -06001006 int ret;
Kever Yang4fbb6c22017-11-03 15:16:13 +08001007 struct udevice *sys_child;
1008 struct sysreset_reg *priv;
Simon Glass421358c2015-08-30 16:55:31 -06001009
1010 /* The reset driver does not have a device node, so bind it here */
Kever Yang4fbb6c22017-11-03 15:16:13 +08001011 ret = device_bind_driver(dev, "rockchip_sysreset", "sysreset",
1012 &sys_child);
1013 if (ret) {
1014 debug("Warning: No sysreset driver: ret=%d\n", ret);
1015 } else {
1016 priv = malloc(sizeof(struct sysreset_reg));
Jagan Teki783acfd2020-01-09 14:22:17 +05301017 priv->glb_srst_fst_value = offsetof(struct rockchip_cru,
Kever Yang4fbb6c22017-11-03 15:16:13 +08001018 cru_glb_srst_fst_value);
Jagan Teki783acfd2020-01-09 14:22:17 +05301019 priv->glb_srst_snd_value = offsetof(struct rockchip_cru,
Kever Yang4fbb6c22017-11-03 15:16:13 +08001020 cru_glb_srst_snd_value);
Simon Glass95588622020-12-22 19:30:28 -07001021 dev_set_priv(sys_child, priv);
Kever Yang4fbb6c22017-11-03 15:16:13 +08001022 }
Simon Glass421358c2015-08-30 16:55:31 -06001023
Heiko Stuebner416f8d32019-11-09 00:06:30 +01001024#if CONFIG_IS_ENABLED(RESET_ROCKCHIP)
Jagan Teki783acfd2020-01-09 14:22:17 +05301025 ret = offsetof(struct rockchip_cru, cru_softrst_con[0]);
Elaine Zhang432976f2017-12-19 18:22:38 +08001026 ret = rockchip_reset_bind(dev, ret, 12);
1027 if (ret)
Eugen Hristevf1798262023-04-11 10:17:56 +03001028 debug("Warning: software reset driver bind failed\n");
Elaine Zhang432976f2017-12-19 18:22:38 +08001029#endif
1030
Simon Glass421358c2015-08-30 16:55:31 -06001031 return 0;
1032}
1033
1034static const struct udevice_id rk3288_clk_ids[] = {
1035 { .compatible = "rockchip,rk3288-cru" },
1036 { }
1037};
1038
Simon Glass00c5fd42016-07-04 11:58:29 -06001039U_BOOT_DRIVER(rockchip_rk3288_cru) = {
1040 .name = "rockchip_rk3288_cru",
Simon Glass421358c2015-08-30 16:55:31 -06001041 .id = UCLASS_CLK,
1042 .of_match = rk3288_clk_ids,
Simon Glass8a2b47f2020-12-03 16:55:17 -07001043 .priv_auto = sizeof(struct rk3288_clk_priv),
Simon Glass71fa5b42020-12-03 16:55:18 -07001044 .plat_auto = sizeof(struct rk3288_clk_plat),
Simon Glass421358c2015-08-30 16:55:31 -06001045 .ops = &rk3288_clk_ops,
1046 .bind = rk3288_clk_bind,
Simon Glassaad29ae2020-12-03 16:55:21 -07001047 .of_to_plat = rk3288_clk_of_to_plat,
Simon Glass421358c2015-08-30 16:55:31 -06001048 .probe = rk3288_clk_probe,
1049};