blob: 773d2176e674a72374c6a82a40913defda1ccf26 [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0
Kever Yangb0b3c862016-07-29 10:35:25 +08002/*
3 * (C) Copyright 2015 Google, Inc
Philipp Tomsich8fa69792017-04-20 22:05:49 +02004 * (C) 2017 Theobroma Systems Design und Consulting GmbH
Kever Yangb0b3c862016-07-29 10:35:25 +08005 */
6
7#include <common.h>
8#include <clk-uclass.h>
9#include <dm.h>
Kever Yang5ae2fd92017-02-13 17:38:56 +080010#include <dt-structs.h>
Kever Yangb0b3c862016-07-29 10:35:25 +080011#include <errno.h>
Simon Glassf7ae49f2020-05-10 11:40:05 -060012#include <log.h>
Simon Glass336d4612020-02-03 07:36:16 -070013#include <malloc.h>
Kever Yang5ae2fd92017-02-13 17:38:56 +080014#include <mapmem.h>
Kever Yangb0b3c862016-07-29 10:35:25 +080015#include <syscon.h>
David Wu364fc732017-09-20 14:38:58 +080016#include <bitfield.h>
Kever Yangb0b3c862016-07-29 10:35:25 +080017#include <asm/io.h>
Kever Yang15f09a12019-03-28 11:01:23 +080018#include <asm/arch-rockchip/clock.h>
Jagan Tekib52a1992020-01-09 14:22:17 +053019#include <asm/arch-rockchip/cru.h>
Kever Yang15f09a12019-03-28 11:01:23 +080020#include <asm/arch-rockchip/hardware.h>
Kever Yangb0b3c862016-07-29 10:35:25 +080021#include <dm/lists.h>
22#include <dt-bindings/clock/rk3399-cru.h>
Simon Glasscd93d622020-05-10 11:40:13 -060023#include <linux/bitops.h>
Simon Glassc05ed002020-05-10 11:40:11 -060024#include <linux/delay.h>
Kever Yangb0b3c862016-07-29 10:35:25 +080025
Kever Yang5ae2fd92017-02-13 17:38:56 +080026#if CONFIG_IS_ENABLED(OF_PLATDATA)
27struct rk3399_clk_plat {
28 struct dtd_rockchip_rk3399_cru dtd;
Kever Yang5e79f442016-08-12 17:47:15 +080029};
30
Kever Yang5ae2fd92017-02-13 17:38:56 +080031struct rk3399_pmuclk_plat {
32 struct dtd_rockchip_rk3399_pmucru dtd;
33};
34#endif
35
Kever Yangb0b3c862016-07-29 10:35:25 +080036struct pll_div {
37 u32 refdiv;
38 u32 fbdiv;
39 u32 postdiv1;
40 u32 postdiv2;
41 u32 frac;
42};
43
44#define RATE_TO_DIV(input_rate, output_rate) \
Jagan Tekidd7dfa22019-07-15 23:51:10 +053045 ((input_rate) / (output_rate) - 1)
46#define DIV_TO_RATE(input_rate, div) ((input_rate) / ((div) + 1))
Kever Yangb0b3c862016-07-29 10:35:25 +080047
48#define PLL_DIVISORS(hz, _refdiv, _postdiv1, _postdiv2) {\
49 .refdiv = _refdiv,\
50 .fbdiv = (u32)((u64)hz * _refdiv * _postdiv1 * _postdiv2 / OSC_HZ),\
51 .postdiv1 = _postdiv1, .postdiv2 = _postdiv2};
52
Philipp Tomsich61dff332017-03-24 19:24:24 +010053#if defined(CONFIG_SPL_BUILD)
Kever Yangb0b3c862016-07-29 10:35:25 +080054static const struct pll_div gpll_init_cfg = PLL_DIVISORS(GPLL_HZ, 2, 2, 1);
55static const struct pll_div cpll_init_cfg = PLL_DIVISORS(CPLL_HZ, 1, 2, 2);
Philipp Tomsich61dff332017-03-24 19:24:24 +010056#else
Kever Yangb0b3c862016-07-29 10:35:25 +080057static const struct pll_div ppll_init_cfg = PLL_DIVISORS(PPLL_HZ, 2, 2, 1);
Philipp Tomsich61dff332017-03-24 19:24:24 +010058#endif
Kever Yangb0b3c862016-07-29 10:35:25 +080059
Jagan Tekidd7dfa22019-07-15 23:51:10 +053060static const struct pll_div apll_l_1600_cfg = PLL_DIVISORS(1600 * MHz, 3, 1, 1);
61static const struct pll_div apll_l_600_cfg = PLL_DIVISORS(600 * MHz, 1, 2, 1);
Kever Yangb0b3c862016-07-29 10:35:25 +080062
63static const struct pll_div *apll_l_cfgs[] = {
64 [APLL_L_1600_MHZ] = &apll_l_1600_cfg,
65 [APLL_L_600_MHZ] = &apll_l_600_cfg,
66};
67
Jagan Tekidd7dfa22019-07-15 23:51:10 +053068static const struct pll_div apll_b_600_cfg = PLL_DIVISORS(600 * MHz, 1, 2, 1);
Christoph Muellneraf765a42018-11-30 20:32:48 +010069static const struct pll_div *apll_b_cfgs[] = {
70 [APLL_B_600_MHZ] = &apll_b_600_cfg,
71};
72
Kever Yangb0b3c862016-07-29 10:35:25 +080073enum {
74 /* PLL_CON0 */
75 PLL_FBDIV_MASK = 0xfff,
76 PLL_FBDIV_SHIFT = 0,
77
78 /* PLL_CON1 */
79 PLL_POSTDIV2_SHIFT = 12,
80 PLL_POSTDIV2_MASK = 0x7 << PLL_POSTDIV2_SHIFT,
81 PLL_POSTDIV1_SHIFT = 8,
82 PLL_POSTDIV1_MASK = 0x7 << PLL_POSTDIV1_SHIFT,
83 PLL_REFDIV_MASK = 0x3f,
84 PLL_REFDIV_SHIFT = 0,
85
86 /* PLL_CON2 */
87 PLL_LOCK_STATUS_SHIFT = 31,
88 PLL_LOCK_STATUS_MASK = 1 << PLL_LOCK_STATUS_SHIFT,
89 PLL_FRACDIV_MASK = 0xffffff,
90 PLL_FRACDIV_SHIFT = 0,
91
92 /* PLL_CON3 */
93 PLL_MODE_SHIFT = 8,
94 PLL_MODE_MASK = 3 << PLL_MODE_SHIFT,
95 PLL_MODE_SLOW = 0,
96 PLL_MODE_NORM,
97 PLL_MODE_DEEP,
98 PLL_DSMPD_SHIFT = 3,
99 PLL_DSMPD_MASK = 1 << PLL_DSMPD_SHIFT,
100 PLL_INTEGER_MODE = 1,
101
102 /* PMUCRU_CLKSEL_CON0 */
103 PMU_PCLK_DIV_CON_MASK = 0x1f,
104 PMU_PCLK_DIV_CON_SHIFT = 0,
105
106 /* PMUCRU_CLKSEL_CON1 */
107 SPI3_PLL_SEL_SHIFT = 7,
108 SPI3_PLL_SEL_MASK = 1 << SPI3_PLL_SEL_SHIFT,
109 SPI3_PLL_SEL_24M = 0,
110 SPI3_PLL_SEL_PPLL = 1,
111 SPI3_DIV_CON_SHIFT = 0x0,
112 SPI3_DIV_CON_MASK = 0x7f,
113
114 /* PMUCRU_CLKSEL_CON2 */
115 I2C_DIV_CON_MASK = 0x7f,
Kever Yang5e79f442016-08-12 17:47:15 +0800116 CLK_I2C8_DIV_CON_SHIFT = 8,
117 CLK_I2C0_DIV_CON_SHIFT = 0,
Kever Yangb0b3c862016-07-29 10:35:25 +0800118
119 /* PMUCRU_CLKSEL_CON3 */
Kever Yang5e79f442016-08-12 17:47:15 +0800120 CLK_I2C4_DIV_CON_SHIFT = 0,
Kever Yangb0b3c862016-07-29 10:35:25 +0800121
122 /* CLKSEL_CON0 */
123 ACLKM_CORE_L_DIV_CON_SHIFT = 8,
124 ACLKM_CORE_L_DIV_CON_MASK = 0x1f << ACLKM_CORE_L_DIV_CON_SHIFT,
125 CLK_CORE_L_PLL_SEL_SHIFT = 6,
126 CLK_CORE_L_PLL_SEL_MASK = 3 << CLK_CORE_L_PLL_SEL_SHIFT,
127 CLK_CORE_L_PLL_SEL_ALPLL = 0x0,
128 CLK_CORE_L_PLL_SEL_ABPLL = 0x1,
129 CLK_CORE_L_PLL_SEL_DPLL = 0x10,
130 CLK_CORE_L_PLL_SEL_GPLL = 0x11,
131 CLK_CORE_L_DIV_MASK = 0x1f,
132 CLK_CORE_L_DIV_SHIFT = 0,
133
134 /* CLKSEL_CON1 */
135 PCLK_DBG_L_DIV_SHIFT = 0x8,
136 PCLK_DBG_L_DIV_MASK = 0x1f << PCLK_DBG_L_DIV_SHIFT,
137 ATCLK_CORE_L_DIV_SHIFT = 0,
138 ATCLK_CORE_L_DIV_MASK = 0x1f << ATCLK_CORE_L_DIV_SHIFT,
139
Christoph Muellneraf765a42018-11-30 20:32:48 +0100140 /* CLKSEL_CON2 */
141 ACLKM_CORE_B_DIV_CON_SHIFT = 8,
142 ACLKM_CORE_B_DIV_CON_MASK = 0x1f << ACLKM_CORE_B_DIV_CON_SHIFT,
143 CLK_CORE_B_PLL_SEL_SHIFT = 6,
144 CLK_CORE_B_PLL_SEL_MASK = 3 << CLK_CORE_B_PLL_SEL_SHIFT,
145 CLK_CORE_B_PLL_SEL_ALPLL = 0x0,
146 CLK_CORE_B_PLL_SEL_ABPLL = 0x1,
147 CLK_CORE_B_PLL_SEL_DPLL = 0x10,
148 CLK_CORE_B_PLL_SEL_GPLL = 0x11,
149 CLK_CORE_B_DIV_MASK = 0x1f,
150 CLK_CORE_B_DIV_SHIFT = 0,
151
152 /* CLKSEL_CON3 */
153 PCLK_DBG_B_DIV_SHIFT = 0x8,
154 PCLK_DBG_B_DIV_MASK = 0x1f << PCLK_DBG_B_DIV_SHIFT,
155 ATCLK_CORE_B_DIV_SHIFT = 0,
156 ATCLK_CORE_B_DIV_MASK = 0x1f << ATCLK_CORE_B_DIV_SHIFT,
157
Kever Yangb0b3c862016-07-29 10:35:25 +0800158 /* CLKSEL_CON14 */
159 PCLK_PERIHP_DIV_CON_SHIFT = 12,
160 PCLK_PERIHP_DIV_CON_MASK = 0x7 << PCLK_PERIHP_DIV_CON_SHIFT,
161 HCLK_PERIHP_DIV_CON_SHIFT = 8,
162 HCLK_PERIHP_DIV_CON_MASK = 3 << HCLK_PERIHP_DIV_CON_SHIFT,
163 ACLK_PERIHP_PLL_SEL_SHIFT = 7,
164 ACLK_PERIHP_PLL_SEL_MASK = 1 << ACLK_PERIHP_PLL_SEL_SHIFT,
165 ACLK_PERIHP_PLL_SEL_CPLL = 0,
166 ACLK_PERIHP_PLL_SEL_GPLL = 1,
167 ACLK_PERIHP_DIV_CON_SHIFT = 0,
168 ACLK_PERIHP_DIV_CON_MASK = 0x1f,
169
170 /* CLKSEL_CON21 */
171 ACLK_EMMC_PLL_SEL_SHIFT = 7,
172 ACLK_EMMC_PLL_SEL_MASK = 0x1 << ACLK_EMMC_PLL_SEL_SHIFT,
173 ACLK_EMMC_PLL_SEL_GPLL = 0x1,
174 ACLK_EMMC_DIV_CON_SHIFT = 0,
175 ACLK_EMMC_DIV_CON_MASK = 0x1f,
176
177 /* CLKSEL_CON22 */
178 CLK_EMMC_PLL_SHIFT = 8,
179 CLK_EMMC_PLL_MASK = 0x7 << CLK_EMMC_PLL_SHIFT,
180 CLK_EMMC_PLL_SEL_GPLL = 0x1,
Kever Yangfd4b2dc2016-08-04 11:44:58 +0800181 CLK_EMMC_PLL_SEL_24M = 0x5,
Kever Yangb0b3c862016-07-29 10:35:25 +0800182 CLK_EMMC_DIV_CON_SHIFT = 0,
183 CLK_EMMC_DIV_CON_MASK = 0x7f << CLK_EMMC_DIV_CON_SHIFT,
184
185 /* CLKSEL_CON23 */
186 PCLK_PERILP0_DIV_CON_SHIFT = 12,
187 PCLK_PERILP0_DIV_CON_MASK = 0x7 << PCLK_PERILP0_DIV_CON_SHIFT,
188 HCLK_PERILP0_DIV_CON_SHIFT = 8,
189 HCLK_PERILP0_DIV_CON_MASK = 3 << HCLK_PERILP0_DIV_CON_SHIFT,
190 ACLK_PERILP0_PLL_SEL_SHIFT = 7,
191 ACLK_PERILP0_PLL_SEL_MASK = 1 << ACLK_PERILP0_PLL_SEL_SHIFT,
192 ACLK_PERILP0_PLL_SEL_CPLL = 0,
193 ACLK_PERILP0_PLL_SEL_GPLL = 1,
194 ACLK_PERILP0_DIV_CON_SHIFT = 0,
195 ACLK_PERILP0_DIV_CON_MASK = 0x1f,
196
197 /* CLKSEL_CON25 */
198 PCLK_PERILP1_DIV_CON_SHIFT = 8,
199 PCLK_PERILP1_DIV_CON_MASK = 0x7 << PCLK_PERILP1_DIV_CON_SHIFT,
200 HCLK_PERILP1_PLL_SEL_SHIFT = 7,
201 HCLK_PERILP1_PLL_SEL_MASK = 1 << HCLK_PERILP1_PLL_SEL_SHIFT,
202 HCLK_PERILP1_PLL_SEL_CPLL = 0,
203 HCLK_PERILP1_PLL_SEL_GPLL = 1,
204 HCLK_PERILP1_DIV_CON_SHIFT = 0,
205 HCLK_PERILP1_DIV_CON_MASK = 0x1f,
206
207 /* CLKSEL_CON26 */
208 CLK_SARADC_DIV_CON_SHIFT = 8,
David Wu364fc732017-09-20 14:38:58 +0800209 CLK_SARADC_DIV_CON_MASK = GENMASK(15, 8),
210 CLK_SARADC_DIV_CON_WIDTH = 8,
Kever Yangb0b3c862016-07-29 10:35:25 +0800211
212 /* CLKSEL_CON27 */
213 CLK_TSADC_SEL_X24M = 0x0,
214 CLK_TSADC_SEL_SHIFT = 15,
215 CLK_TSADC_SEL_MASK = 1 << CLK_TSADC_SEL_SHIFT,
216 CLK_TSADC_DIV_CON_SHIFT = 0,
217 CLK_TSADC_DIV_CON_MASK = 0x3ff,
218
219 /* CLKSEL_CON47 & CLKSEL_CON48 */
220 ACLK_VOP_PLL_SEL_SHIFT = 6,
221 ACLK_VOP_PLL_SEL_MASK = 0x3 << ACLK_VOP_PLL_SEL_SHIFT,
222 ACLK_VOP_PLL_SEL_CPLL = 0x1,
223 ACLK_VOP_DIV_CON_SHIFT = 0,
224 ACLK_VOP_DIV_CON_MASK = 0x1f << ACLK_VOP_DIV_CON_SHIFT,
225
226 /* CLKSEL_CON49 & CLKSEL_CON50 */
227 DCLK_VOP_DCLK_SEL_SHIFT = 11,
228 DCLK_VOP_DCLK_SEL_MASK = 1 << DCLK_VOP_DCLK_SEL_SHIFT,
229 DCLK_VOP_DCLK_SEL_DIVOUT = 0,
230 DCLK_VOP_PLL_SEL_SHIFT = 8,
231 DCLK_VOP_PLL_SEL_MASK = 3 << DCLK_VOP_PLL_SEL_SHIFT,
232 DCLK_VOP_PLL_SEL_VPLL = 0,
233 DCLK_VOP_DIV_CON_MASK = 0xff,
234 DCLK_VOP_DIV_CON_SHIFT = 0,
235
236 /* CLKSEL_CON58 */
Philipp Tomsich8fa69792017-04-20 22:05:49 +0200237 CLK_SPI_PLL_SEL_WIDTH = 1,
238 CLK_SPI_PLL_SEL_MASK = ((1 < CLK_SPI_PLL_SEL_WIDTH) - 1),
239 CLK_SPI_PLL_SEL_CPLL = 0,
240 CLK_SPI_PLL_SEL_GPLL = 1,
241 CLK_SPI_PLL_DIV_CON_WIDTH = 7,
242 CLK_SPI_PLL_DIV_CON_MASK = ((1 << CLK_SPI_PLL_DIV_CON_WIDTH) - 1),
243
244 CLK_SPI5_PLL_DIV_CON_SHIFT = 8,
245 CLK_SPI5_PLL_SEL_SHIFT = 15,
Kever Yangb0b3c862016-07-29 10:35:25 +0800246
247 /* CLKSEL_CON59 */
248 CLK_SPI1_PLL_SEL_SHIFT = 15,
249 CLK_SPI1_PLL_DIV_CON_SHIFT = 8,
250 CLK_SPI0_PLL_SEL_SHIFT = 7,
251 CLK_SPI0_PLL_DIV_CON_SHIFT = 0,
252
253 /* CLKSEL_CON60 */
254 CLK_SPI4_PLL_SEL_SHIFT = 15,
255 CLK_SPI4_PLL_DIV_CON_SHIFT = 8,
256 CLK_SPI2_PLL_SEL_SHIFT = 7,
257 CLK_SPI2_PLL_DIV_CON_SHIFT = 0,
258
259 /* CLKSEL_CON61 */
260 CLK_I2C_PLL_SEL_MASK = 1,
261 CLK_I2C_PLL_SEL_CPLL = 0,
262 CLK_I2C_PLL_SEL_GPLL = 1,
263 CLK_I2C5_PLL_SEL_SHIFT = 15,
264 CLK_I2C5_DIV_CON_SHIFT = 8,
265 CLK_I2C1_PLL_SEL_SHIFT = 7,
266 CLK_I2C1_DIV_CON_SHIFT = 0,
267
268 /* CLKSEL_CON62 */
269 CLK_I2C6_PLL_SEL_SHIFT = 15,
270 CLK_I2C6_DIV_CON_SHIFT = 8,
271 CLK_I2C2_PLL_SEL_SHIFT = 7,
272 CLK_I2C2_DIV_CON_SHIFT = 0,
273
274 /* CLKSEL_CON63 */
275 CLK_I2C7_PLL_SEL_SHIFT = 15,
276 CLK_I2C7_DIV_CON_SHIFT = 8,
277 CLK_I2C3_PLL_SEL_SHIFT = 7,
278 CLK_I2C3_DIV_CON_SHIFT = 0,
279
280 /* CRU_SOFTRST_CON4 */
281 RESETN_DDR0_REQ_SHIFT = 8,
282 RESETN_DDR0_REQ_MASK = 1 << RESETN_DDR0_REQ_SHIFT,
283 RESETN_DDRPHY0_REQ_SHIFT = 9,
284 RESETN_DDRPHY0_REQ_MASK = 1 << RESETN_DDRPHY0_REQ_SHIFT,
285 RESETN_DDR1_REQ_SHIFT = 12,
286 RESETN_DDR1_REQ_MASK = 1 << RESETN_DDR1_REQ_SHIFT,
287 RESETN_DDRPHY1_REQ_SHIFT = 13,
288 RESETN_DDRPHY1_REQ_MASK = 1 << RESETN_DDRPHY1_REQ_SHIFT,
289};
290
291#define VCO_MAX_KHZ (3200 * (MHz / KHz))
292#define VCO_MIN_KHZ (800 * (MHz / KHz))
293#define OUTPUT_MAX_KHZ (3200 * (MHz / KHz))
294#define OUTPUT_MIN_KHZ (16 * (MHz / KHz))
295
296/*
297 * the div restructions of pll in integer mode, these are defined in
298 * * CRU_*PLL_CON0 or PMUCRU_*PLL_CON0
299 */
300#define PLL_DIV_MIN 16
301#define PLL_DIV_MAX 3200
302
303/*
304 * How to calculate the PLL(from TRM V0.3 Part 1 Page 63):
305 * Formulas also embedded within the Fractional PLL Verilog model:
306 * If DSMPD = 1 (DSM is disabled, "integer mode")
307 * FOUTVCO = FREF / REFDIV * FBDIV
308 * FOUTPOSTDIV = FOUTVCO / POSTDIV1 / POSTDIV2
309 * Where:
310 * FOUTVCO = Fractional PLL non-divided output frequency
311 * FOUTPOSTDIV = Fractional PLL divided output frequency
312 * (output of second post divider)
313 * FREF = Fractional PLL input reference frequency, (the OSC_HZ 24MHz input)
314 * REFDIV = Fractional PLL input reference clock divider
315 * FBDIV = Integer value programmed into feedback divide
316 *
317 */
318static void rkclk_set_pll(u32 *pll_con, const struct pll_div *div)
319{
320 /* All 8 PLLs have same VCO and output frequency range restrictions. */
321 u32 vco_khz = OSC_HZ / 1000 * div->fbdiv / div->refdiv;
322 u32 output_khz = vco_khz / div->postdiv1 / div->postdiv2;
323
324 debug("PLL at %p: fbdiv=%d, refdiv=%d, postdiv1=%d, "
325 "postdiv2=%d, vco=%u khz, output=%u khz\n",
326 pll_con, div->fbdiv, div->refdiv, div->postdiv1,
327 div->postdiv2, vco_khz, output_khz);
328 assert(vco_khz >= VCO_MIN_KHZ && vco_khz <= VCO_MAX_KHZ &&
329 output_khz >= OUTPUT_MIN_KHZ && output_khz <= OUTPUT_MAX_KHZ &&
330 div->fbdiv >= PLL_DIV_MIN && div->fbdiv <= PLL_DIV_MAX);
331
332 /*
333 * When power on or changing PLL setting,
334 * we must force PLL into slow mode to ensure output stable clock.
335 */
336 rk_clrsetreg(&pll_con[3], PLL_MODE_MASK,
337 PLL_MODE_SLOW << PLL_MODE_SHIFT);
338
339 /* use integer mode */
340 rk_clrsetreg(&pll_con[3], PLL_DSMPD_MASK,
341 PLL_INTEGER_MODE << PLL_DSMPD_SHIFT);
342
343 rk_clrsetreg(&pll_con[0], PLL_FBDIV_MASK,
344 div->fbdiv << PLL_FBDIV_SHIFT);
345 rk_clrsetreg(&pll_con[1],
346 PLL_POSTDIV2_MASK | PLL_POSTDIV1_MASK |
347 PLL_REFDIV_MASK | PLL_REFDIV_SHIFT,
348 (div->postdiv2 << PLL_POSTDIV2_SHIFT) |
349 (div->postdiv1 << PLL_POSTDIV1_SHIFT) |
350 (div->refdiv << PLL_REFDIV_SHIFT));
351
352 /* waiting for pll lock */
353 while (!(readl(&pll_con[2]) & (1 << PLL_LOCK_STATUS_SHIFT)))
354 udelay(1);
355
356 /* pll enter normal mode */
357 rk_clrsetreg(&pll_con[3], PLL_MODE_MASK,
358 PLL_MODE_NORM << PLL_MODE_SHIFT);
359}
360
361static int pll_para_config(u32 freq_hz, struct pll_div *div)
362{
363 u32 ref_khz = OSC_HZ / KHz, refdiv, fbdiv = 0;
364 u32 postdiv1, postdiv2 = 1;
365 u32 fref_khz;
366 u32 diff_khz, best_diff_khz;
367 const u32 max_refdiv = 63, max_fbdiv = 3200, min_fbdiv = 16;
368 const u32 max_postdiv1 = 7, max_postdiv2 = 7;
369 u32 vco_khz;
370 u32 freq_khz = freq_hz / KHz;
371
372 if (!freq_hz) {
373 printf("%s: the frequency can't be 0 Hz\n", __func__);
374 return -1;
375 }
376
377 postdiv1 = DIV_ROUND_UP(VCO_MIN_KHZ, freq_khz);
378 if (postdiv1 > max_postdiv1) {
379 postdiv2 = DIV_ROUND_UP(postdiv1, max_postdiv1);
380 postdiv1 = DIV_ROUND_UP(postdiv1, postdiv2);
381 }
382
383 vco_khz = freq_khz * postdiv1 * postdiv2;
384
385 if (vco_khz < VCO_MIN_KHZ || vco_khz > VCO_MAX_KHZ ||
386 postdiv2 > max_postdiv2) {
387 printf("%s: Cannot find out a supported VCO"
388 " for Frequency (%uHz).\n", __func__, freq_hz);
389 return -1;
390 }
391
392 div->postdiv1 = postdiv1;
393 div->postdiv2 = postdiv2;
394
395 best_diff_khz = vco_khz;
396 for (refdiv = 1; refdiv < max_refdiv && best_diff_khz; refdiv++) {
397 fref_khz = ref_khz / refdiv;
398
399 fbdiv = vco_khz / fref_khz;
Jagan Tekidd7dfa22019-07-15 23:51:10 +0530400 if (fbdiv >= max_fbdiv || fbdiv <= min_fbdiv)
Kever Yangb0b3c862016-07-29 10:35:25 +0800401 continue;
402 diff_khz = vco_khz - fbdiv * fref_khz;
403 if (fbdiv + 1 < max_fbdiv && diff_khz > fref_khz / 2) {
404 fbdiv++;
405 diff_khz = fref_khz - diff_khz;
406 }
407
408 if (diff_khz >= best_diff_khz)
409 continue;
410
411 best_diff_khz = diff_khz;
412 div->refdiv = refdiv;
413 div->fbdiv = fbdiv;
414 }
415
Jagan Tekidd7dfa22019-07-15 23:51:10 +0530416 if (best_diff_khz > 4 * (MHz / KHz)) {
Kever Yangb0b3c862016-07-29 10:35:25 +0800417 printf("%s: Failed to match output frequency %u, "
418 "difference is %u Hz,exceed 4MHZ\n", __func__, freq_hz,
419 best_diff_khz * KHz);
420 return -1;
421 }
422 return 0;
423}
424
Jagan Tekib52a1992020-01-09 14:22:17 +0530425void rk3399_configure_cpu_l(struct rockchip_cru *cru,
Christoph Muellneraf765a42018-11-30 20:32:48 +0100426 enum apll_l_frequencies apll_l_freq)
Kever Yangb0b3c862016-07-29 10:35:25 +0800427{
428 u32 aclkm_div;
429 u32 pclk_dbg_div;
430 u32 atclk_div;
431
Christoph Muellneraf765a42018-11-30 20:32:48 +0100432 /* Setup cluster L */
Kever Yangb0b3c862016-07-29 10:35:25 +0800433 rkclk_set_pll(&cru->apll_l_con[0], apll_l_cfgs[apll_l_freq]);
434
Christoph Muellneraf765a42018-11-30 20:32:48 +0100435 aclkm_div = LPLL_HZ / ACLKM_CORE_L_HZ - 1;
436 assert((aclkm_div + 1) * ACLKM_CORE_L_HZ == LPLL_HZ &&
Kever Yangb0b3c862016-07-29 10:35:25 +0800437 aclkm_div < 0x1f);
438
Christoph Muellneraf765a42018-11-30 20:32:48 +0100439 pclk_dbg_div = LPLL_HZ / PCLK_DBG_L_HZ - 1;
440 assert((pclk_dbg_div + 1) * PCLK_DBG_L_HZ == LPLL_HZ &&
Kever Yangb0b3c862016-07-29 10:35:25 +0800441 pclk_dbg_div < 0x1f);
442
Christoph Muellneraf765a42018-11-30 20:32:48 +0100443 atclk_div = LPLL_HZ / ATCLK_CORE_L_HZ - 1;
444 assert((atclk_div + 1) * ATCLK_CORE_L_HZ == LPLL_HZ &&
Kever Yangb0b3c862016-07-29 10:35:25 +0800445 atclk_div < 0x1f);
446
447 rk_clrsetreg(&cru->clksel_con[0],
448 ACLKM_CORE_L_DIV_CON_MASK | CLK_CORE_L_PLL_SEL_MASK |
449 CLK_CORE_L_DIV_MASK,
450 aclkm_div << ACLKM_CORE_L_DIV_CON_SHIFT |
451 CLK_CORE_L_PLL_SEL_ALPLL << CLK_CORE_L_PLL_SEL_SHIFT |
452 0 << CLK_CORE_L_DIV_SHIFT);
453
454 rk_clrsetreg(&cru->clksel_con[1],
455 PCLK_DBG_L_DIV_MASK | ATCLK_CORE_L_DIV_MASK,
456 pclk_dbg_div << PCLK_DBG_L_DIV_SHIFT |
457 atclk_div << ATCLK_CORE_L_DIV_SHIFT);
458}
Christoph Muellneraf765a42018-11-30 20:32:48 +0100459
Jagan Tekib52a1992020-01-09 14:22:17 +0530460void rk3399_configure_cpu_b(struct rockchip_cru *cru,
Christoph Muellneraf765a42018-11-30 20:32:48 +0100461 enum apll_b_frequencies apll_b_freq)
462{
463 u32 aclkm_div;
464 u32 pclk_dbg_div;
465 u32 atclk_div;
466
467 /* Setup cluster B */
468 rkclk_set_pll(&cru->apll_b_con[0], apll_b_cfgs[apll_b_freq]);
469
470 aclkm_div = BPLL_HZ / ACLKM_CORE_B_HZ - 1;
471 assert((aclkm_div + 1) * ACLKM_CORE_B_HZ == BPLL_HZ &&
472 aclkm_div < 0x1f);
473
474 pclk_dbg_div = BPLL_HZ / PCLK_DBG_B_HZ - 1;
475 assert((pclk_dbg_div + 1) * PCLK_DBG_B_HZ == BPLL_HZ &&
476 pclk_dbg_div < 0x1f);
477
478 atclk_div = BPLL_HZ / ATCLK_CORE_B_HZ - 1;
479 assert((atclk_div + 1) * ATCLK_CORE_B_HZ == BPLL_HZ &&
480 atclk_div < 0x1f);
481
482 rk_clrsetreg(&cru->clksel_con[2],
483 ACLKM_CORE_B_DIV_CON_MASK | CLK_CORE_B_PLL_SEL_MASK |
484 CLK_CORE_B_DIV_MASK,
485 aclkm_div << ACLKM_CORE_B_DIV_CON_SHIFT |
486 CLK_CORE_B_PLL_SEL_ABPLL << CLK_CORE_B_PLL_SEL_SHIFT |
487 0 << CLK_CORE_B_DIV_SHIFT);
488
489 rk_clrsetreg(&cru->clksel_con[3],
490 PCLK_DBG_B_DIV_MASK | ATCLK_CORE_B_DIV_MASK,
491 pclk_dbg_div << PCLK_DBG_B_DIV_SHIFT |
492 atclk_div << ATCLK_CORE_B_DIV_SHIFT);
493}
494
Kever Yangb0b3c862016-07-29 10:35:25 +0800495#define I2C_CLK_REG_MASK(bus) \
Jagan Tekidd7dfa22019-07-15 23:51:10 +0530496 (I2C_DIV_CON_MASK << CLK_I2C ##bus## _DIV_CON_SHIFT | \
497 CLK_I2C_PLL_SEL_MASK << CLK_I2C ##bus## _PLL_SEL_SHIFT)
Kever Yangb0b3c862016-07-29 10:35:25 +0800498
499#define I2C_CLK_REG_VALUE(bus, clk_div) \
Jagan Tekidd7dfa22019-07-15 23:51:10 +0530500 ((clk_div - 1) << CLK_I2C ##bus## _DIV_CON_SHIFT | \
501 CLK_I2C_PLL_SEL_GPLL << CLK_I2C ##bus## _PLL_SEL_SHIFT)
Kever Yangb0b3c862016-07-29 10:35:25 +0800502
503#define I2C_CLK_DIV_VALUE(con, bus) \
Jagan Tekidd7dfa22019-07-15 23:51:10 +0530504 ((con >> CLK_I2C ##bus## _DIV_CON_SHIFT) & I2C_DIV_CON_MASK)
Kever Yangb0b3c862016-07-29 10:35:25 +0800505
Kever Yang5e79f442016-08-12 17:47:15 +0800506#define I2C_PMUCLK_REG_MASK(bus) \
Jagan Tekidd7dfa22019-07-15 23:51:10 +0530507 (I2C_DIV_CON_MASK << CLK_I2C ##bus## _DIV_CON_SHIFT)
Kever Yang5e79f442016-08-12 17:47:15 +0800508
509#define I2C_PMUCLK_REG_VALUE(bus, clk_div) \
Jagan Tekidd7dfa22019-07-15 23:51:10 +0530510 ((clk_div - 1) << CLK_I2C ##bus## _DIV_CON_SHIFT)
Kever Yang5e79f442016-08-12 17:47:15 +0800511
Jagan Tekib52a1992020-01-09 14:22:17 +0530512static ulong rk3399_i2c_get_clk(struct rockchip_cru *cru, ulong clk_id)
Kever Yangb0b3c862016-07-29 10:35:25 +0800513{
514 u32 div, con;
515
516 switch (clk_id) {
517 case SCLK_I2C1:
518 con = readl(&cru->clksel_con[61]);
519 div = I2C_CLK_DIV_VALUE(con, 1);
520 break;
521 case SCLK_I2C2:
522 con = readl(&cru->clksel_con[62]);
523 div = I2C_CLK_DIV_VALUE(con, 2);
524 break;
525 case SCLK_I2C3:
526 con = readl(&cru->clksel_con[63]);
527 div = I2C_CLK_DIV_VALUE(con, 3);
528 break;
529 case SCLK_I2C5:
530 con = readl(&cru->clksel_con[61]);
531 div = I2C_CLK_DIV_VALUE(con, 5);
532 break;
533 case SCLK_I2C6:
534 con = readl(&cru->clksel_con[62]);
535 div = I2C_CLK_DIV_VALUE(con, 6);
536 break;
537 case SCLK_I2C7:
538 con = readl(&cru->clksel_con[63]);
539 div = I2C_CLK_DIV_VALUE(con, 7);
540 break;
541 default:
542 printf("do not support this i2c bus\n");
543 return -EINVAL;
544 }
545
546 return DIV_TO_RATE(GPLL_HZ, div);
547}
548
Jagan Tekib52a1992020-01-09 14:22:17 +0530549static ulong rk3399_i2c_set_clk(struct rockchip_cru *cru, ulong clk_id, uint hz)
Kever Yangb0b3c862016-07-29 10:35:25 +0800550{
551 int src_clk_div;
552
553 /* i2c0,4,8 src clock from ppll, i2c1,2,3,5,6,7 src clock from gpll*/
554 src_clk_div = GPLL_HZ / hz;
555 assert(src_clk_div - 1 < 127);
556
557 switch (clk_id) {
558 case SCLK_I2C1:
559 rk_clrsetreg(&cru->clksel_con[61], I2C_CLK_REG_MASK(1),
560 I2C_CLK_REG_VALUE(1, src_clk_div));
561 break;
562 case SCLK_I2C2:
563 rk_clrsetreg(&cru->clksel_con[62], I2C_CLK_REG_MASK(2),
564 I2C_CLK_REG_VALUE(2, src_clk_div));
565 break;
566 case SCLK_I2C3:
567 rk_clrsetreg(&cru->clksel_con[63], I2C_CLK_REG_MASK(3),
568 I2C_CLK_REG_VALUE(3, src_clk_div));
569 break;
570 case SCLK_I2C5:
571 rk_clrsetreg(&cru->clksel_con[61], I2C_CLK_REG_MASK(5),
572 I2C_CLK_REG_VALUE(5, src_clk_div));
573 break;
574 case SCLK_I2C6:
575 rk_clrsetreg(&cru->clksel_con[62], I2C_CLK_REG_MASK(6),
576 I2C_CLK_REG_VALUE(6, src_clk_div));
577 break;
578 case SCLK_I2C7:
579 rk_clrsetreg(&cru->clksel_con[63], I2C_CLK_REG_MASK(7),
580 I2C_CLK_REG_VALUE(7, src_clk_div));
581 break;
582 default:
583 printf("do not support this i2c bus\n");
584 return -EINVAL;
585 }
586
Philipp Tomsichbeb90a52017-04-20 22:05:50 +0200587 return rk3399_i2c_get_clk(cru, clk_id);
Kever Yangb0b3c862016-07-29 10:35:25 +0800588}
589
Philipp Tomsich8fa69792017-04-20 22:05:49 +0200590/*
591 * RK3399 SPI clocks have a common divider-width (7 bits) and a single bit
592 * to select either CPLL or GPLL as the clock-parent. The location within
593 * the enclosing CLKSEL_CON (i.e. div_shift and sel_shift) are variable.
594 */
595
596struct spi_clkreg {
Jagan Tekidd7dfa22019-07-15 23:51:10 +0530597 u8 reg; /* CLKSEL_CON[reg] register in CRU */
598 u8 div_shift;
599 u8 sel_shift;
Philipp Tomsich8fa69792017-04-20 22:05:49 +0200600};
601
602/*
603 * The entries are numbered relative to their offset from SCLK_SPI0.
604 *
605 * Note that SCLK_SPI3 (which is configured via PMUCRU and requires different
606 * logic is not supported).
607 */
608static const struct spi_clkreg spi_clkregs[] = {
609 [0] = { .reg = 59,
610 .div_shift = CLK_SPI0_PLL_DIV_CON_SHIFT,
611 .sel_shift = CLK_SPI0_PLL_SEL_SHIFT, },
612 [1] = { .reg = 59,
613 .div_shift = CLK_SPI1_PLL_DIV_CON_SHIFT,
614 .sel_shift = CLK_SPI1_PLL_SEL_SHIFT, },
615 [2] = { .reg = 60,
616 .div_shift = CLK_SPI2_PLL_DIV_CON_SHIFT,
617 .sel_shift = CLK_SPI2_PLL_SEL_SHIFT, },
618 [3] = { .reg = 60,
619 .div_shift = CLK_SPI4_PLL_DIV_CON_SHIFT,
620 .sel_shift = CLK_SPI4_PLL_SEL_SHIFT, },
621 [4] = { .reg = 58,
622 .div_shift = CLK_SPI5_PLL_DIV_CON_SHIFT,
623 .sel_shift = CLK_SPI5_PLL_SEL_SHIFT, },
624};
625
Jagan Tekib52a1992020-01-09 14:22:17 +0530626static ulong rk3399_spi_get_clk(struct rockchip_cru *cru, ulong clk_id)
Philipp Tomsich8fa69792017-04-20 22:05:49 +0200627{
628 const struct spi_clkreg *spiclk = NULL;
629 u32 div, val;
630
631 switch (clk_id) {
632 case SCLK_SPI0 ... SCLK_SPI5:
633 spiclk = &spi_clkregs[clk_id - SCLK_SPI0];
634 break;
635
636 default:
Masahiro Yamada9b643e32017-09-16 14:10:41 +0900637 pr_err("%s: SPI clk-id %ld not supported\n", __func__, clk_id);
Philipp Tomsich8fa69792017-04-20 22:05:49 +0200638 return -EINVAL;
639 }
640
641 val = readl(&cru->clksel_con[spiclk->reg]);
Philipp Tomsicha8ee98d2017-11-22 19:45:04 +0100642 div = bitfield_extract(val, spiclk->div_shift,
643 CLK_SPI_PLL_DIV_CON_WIDTH);
Philipp Tomsich8fa69792017-04-20 22:05:49 +0200644
645 return DIV_TO_RATE(GPLL_HZ, div);
646}
647
Jagan Tekib52a1992020-01-09 14:22:17 +0530648static ulong rk3399_spi_set_clk(struct rockchip_cru *cru, ulong clk_id, uint hz)
Philipp Tomsich8fa69792017-04-20 22:05:49 +0200649{
650 const struct spi_clkreg *spiclk = NULL;
651 int src_clk_div;
652
Kever Yang217273c2017-07-27 12:54:02 +0800653 src_clk_div = DIV_ROUND_UP(GPLL_HZ, hz) - 1;
654 assert(src_clk_div < 128);
Philipp Tomsich8fa69792017-04-20 22:05:49 +0200655
656 switch (clk_id) {
657 case SCLK_SPI1 ... SCLK_SPI5:
658 spiclk = &spi_clkregs[clk_id - SCLK_SPI0];
659 break;
660
661 default:
Masahiro Yamada9b643e32017-09-16 14:10:41 +0900662 pr_err("%s: SPI clk-id %ld not supported\n", __func__, clk_id);
Philipp Tomsich8fa69792017-04-20 22:05:49 +0200663 return -EINVAL;
664 }
665
666 rk_clrsetreg(&cru->clksel_con[spiclk->reg],
667 ((CLK_SPI_PLL_DIV_CON_MASK << spiclk->div_shift) |
668 (CLK_SPI_PLL_SEL_GPLL << spiclk->sel_shift)),
669 ((src_clk_div << spiclk->div_shift) |
670 (CLK_SPI_PLL_SEL_GPLL << spiclk->sel_shift)));
671
Philipp Tomsichbeb90a52017-04-20 22:05:50 +0200672 return rk3399_spi_get_clk(cru, clk_id);
Philipp Tomsich8fa69792017-04-20 22:05:49 +0200673}
674
Jagan Tekib52a1992020-01-09 14:22:17 +0530675static ulong rk3399_vop_set_clk(struct rockchip_cru *cru, ulong clk_id, u32 hz)
Kever Yangb0b3c862016-07-29 10:35:25 +0800676{
677 struct pll_div vpll_config = {0};
Jagan Tekidd7dfa22019-07-15 23:51:10 +0530678 int aclk_vop = 198 * MHz;
Kever Yangb0b3c862016-07-29 10:35:25 +0800679 void *aclkreg_addr, *dclkreg_addr;
680 u32 div;
681
682 switch (clk_id) {
683 case DCLK_VOP0:
684 aclkreg_addr = &cru->clksel_con[47];
685 dclkreg_addr = &cru->clksel_con[49];
686 break;
687 case DCLK_VOP1:
688 aclkreg_addr = &cru->clksel_con[48];
689 dclkreg_addr = &cru->clksel_con[50];
690 break;
691 default:
692 return -EINVAL;
693 }
694 /* vop aclk source clk: cpll */
695 div = CPLL_HZ / aclk_vop;
696 assert(div - 1 < 32);
697
698 rk_clrsetreg(aclkreg_addr,
699 ACLK_VOP_PLL_SEL_MASK | ACLK_VOP_DIV_CON_MASK,
700 ACLK_VOP_PLL_SEL_CPLL << ACLK_VOP_PLL_SEL_SHIFT |
701 (div - 1) << ACLK_VOP_DIV_CON_SHIFT);
702
703 /* vop dclk source from vpll, and equals to vpll(means div == 1) */
704 if (pll_para_config(hz, &vpll_config))
705 return -1;
706
707 rkclk_set_pll(&cru->vpll_con[0], &vpll_config);
708
709 rk_clrsetreg(dclkreg_addr,
Jagan Tekidd7dfa22019-07-15 23:51:10 +0530710 DCLK_VOP_DCLK_SEL_MASK | DCLK_VOP_PLL_SEL_MASK |
Kever Yangb0b3c862016-07-29 10:35:25 +0800711 DCLK_VOP_DIV_CON_MASK,
712 DCLK_VOP_DCLK_SEL_DIVOUT << DCLK_VOP_DCLK_SEL_SHIFT |
713 DCLK_VOP_PLL_SEL_VPLL << DCLK_VOP_PLL_SEL_SHIFT |
714 (1 - 1) << DCLK_VOP_DIV_CON_SHIFT);
715
716 return hz;
717}
718
Jagan Tekib52a1992020-01-09 14:22:17 +0530719static ulong rk3399_mmc_get_clk(struct rockchip_cru *cru, uint clk_id)
Kever Yangb0b3c862016-07-29 10:35:25 +0800720{
721 u32 div, con;
722
723 switch (clk_id) {
Philipp Tomsich998c61a2017-04-25 09:52:06 +0200724 case HCLK_SDMMC:
Kever Yangb0b3c862016-07-29 10:35:25 +0800725 case SCLK_SDMMC:
726 con = readl(&cru->clksel_con[16]);
Kever Yang3a94d752017-07-27 12:54:01 +0800727 /* dwmmc controller have internal div 2 */
728 div = 2;
Kever Yangb0b3c862016-07-29 10:35:25 +0800729 break;
730 case SCLK_EMMC:
Jagan Teki46481082020-05-24 22:13:15 +0530731 con = readl(&cru->clksel_con[22]);
Kever Yang3a94d752017-07-27 12:54:01 +0800732 div = 1;
Kever Yangb0b3c862016-07-29 10:35:25 +0800733 break;
734 default:
735 return -EINVAL;
736 }
Kever Yangb0b3c862016-07-29 10:35:25 +0800737
Kever Yang3a94d752017-07-27 12:54:01 +0800738 div *= (con & CLK_EMMC_DIV_CON_MASK) >> CLK_EMMC_DIV_CON_SHIFT;
Kever Yangfd4b2dc2016-08-04 11:44:58 +0800739 if ((con & CLK_EMMC_PLL_MASK) >> CLK_EMMC_PLL_SHIFT
740 == CLK_EMMC_PLL_SEL_24M)
Kever Yang3a94d752017-07-27 12:54:01 +0800741 return DIV_TO_RATE(OSC_HZ, div);
Kever Yangfd4b2dc2016-08-04 11:44:58 +0800742 else
743 return DIV_TO_RATE(GPLL_HZ, div);
Kever Yangb0b3c862016-07-29 10:35:25 +0800744}
745
Jagan Tekib52a1992020-01-09 14:22:17 +0530746static ulong rk3399_mmc_set_clk(struct rockchip_cru *cru,
Kever Yangb0b3c862016-07-29 10:35:25 +0800747 ulong clk_id, ulong set_rate)
748{
749 int src_clk_div;
Jagan Tekidd7dfa22019-07-15 23:51:10 +0530750 int aclk_emmc = 198 * MHz;
Kever Yangb0b3c862016-07-29 10:35:25 +0800751
752 switch (clk_id) {
Philipp Tomsich998c61a2017-04-25 09:52:06 +0200753 case HCLK_SDMMC:
Kever Yangb0b3c862016-07-29 10:35:25 +0800754 case SCLK_SDMMC:
Kever Yangfd4b2dc2016-08-04 11:44:58 +0800755 /* Select clk_sdmmc source from GPLL by default */
Kever Yang3a94d752017-07-27 12:54:01 +0800756 /* mmc clock defaulg div 2 internal, provide double in cru */
757 src_clk_div = DIV_ROUND_UP(GPLL_HZ / 2, set_rate);
Kever Yangb0b3c862016-07-29 10:35:25 +0800758
Kever Yang217273c2017-07-27 12:54:02 +0800759 if (src_clk_div > 128) {
Kever Yangfd4b2dc2016-08-04 11:44:58 +0800760 /* use 24MHz source for 400KHz clock */
Kever Yang3a94d752017-07-27 12:54:01 +0800761 src_clk_div = DIV_ROUND_UP(OSC_HZ / 2, set_rate);
Kever Yang217273c2017-07-27 12:54:02 +0800762 assert(src_clk_div - 1 < 128);
Kever Yangfd4b2dc2016-08-04 11:44:58 +0800763 rk_clrsetreg(&cru->clksel_con[16],
764 CLK_EMMC_PLL_MASK | CLK_EMMC_DIV_CON_MASK,
765 CLK_EMMC_PLL_SEL_24M << CLK_EMMC_PLL_SHIFT |
766 (src_clk_div - 1) << CLK_EMMC_DIV_CON_SHIFT);
767 } else {
768 rk_clrsetreg(&cru->clksel_con[16],
769 CLK_EMMC_PLL_MASK | CLK_EMMC_DIV_CON_MASK,
770 CLK_EMMC_PLL_SEL_GPLL << CLK_EMMC_PLL_SHIFT |
771 (src_clk_div - 1) << CLK_EMMC_DIV_CON_SHIFT);
772 }
Kever Yangb0b3c862016-07-29 10:35:25 +0800773 break;
774 case SCLK_EMMC:
775 /* Select aclk_emmc source from GPLL */
Jagan Tekidd7dfa22019-07-15 23:51:10 +0530776 src_clk_div = DIV_ROUND_UP(GPLL_HZ, aclk_emmc);
Kever Yang217273c2017-07-27 12:54:02 +0800777 assert(src_clk_div - 1 < 32);
Kever Yangb0b3c862016-07-29 10:35:25 +0800778
779 rk_clrsetreg(&cru->clksel_con[21],
780 ACLK_EMMC_PLL_SEL_MASK | ACLK_EMMC_DIV_CON_MASK,
781 ACLK_EMMC_PLL_SEL_GPLL << ACLK_EMMC_PLL_SEL_SHIFT |
782 (src_clk_div - 1) << ACLK_EMMC_DIV_CON_SHIFT);
783
784 /* Select clk_emmc source from GPLL too */
Kever Yang217273c2017-07-27 12:54:02 +0800785 src_clk_div = DIV_ROUND_UP(GPLL_HZ, set_rate);
786 assert(src_clk_div - 1 < 128);
Kever Yangb0b3c862016-07-29 10:35:25 +0800787
788 rk_clrsetreg(&cru->clksel_con[22],
789 CLK_EMMC_PLL_MASK | CLK_EMMC_DIV_CON_MASK,
790 CLK_EMMC_PLL_SEL_GPLL << CLK_EMMC_PLL_SHIFT |
791 (src_clk_div - 1) << CLK_EMMC_DIV_CON_SHIFT);
792 break;
793 default:
794 return -EINVAL;
795 }
796 return rk3399_mmc_get_clk(cru, clk_id);
797}
798
Jagan Tekib52a1992020-01-09 14:22:17 +0530799static ulong rk3399_gmac_set_clk(struct rockchip_cru *cru, ulong rate)
Philipp Tomsicha45f17e2018-01-08 13:11:01 +0100800{
801 ulong ret;
802
803 /*
804 * The RGMII CLK can be derived either from an external "clkin"
805 * or can be generated from internally by a divider from SCLK_MAC.
806 */
807 if (readl(&cru->clksel_con[19]) & BIT(4)) {
808 /* An external clock will always generate the right rate... */
809 ret = rate;
810 } else {
811 /*
812 * No platform uses an internal clock to date.
813 * Implement this once it becomes necessary and print an error
814 * if someone tries to use it (while it remains unimplemented).
815 */
816 pr_err("%s: internal clock is UNIMPLEMENTED\n", __func__);
817 ret = 0;
818 }
819
820 return ret;
821}
822
Kever Yang5ae2fd92017-02-13 17:38:56 +0800823#define PMUSGRF_DDR_RGN_CON16 0xff330040
Jagan Tekib52a1992020-01-09 14:22:17 +0530824static ulong rk3399_ddr_set_clk(struct rockchip_cru *cru,
Kever Yang5ae2fd92017-02-13 17:38:56 +0800825 ulong set_rate)
826{
827 struct pll_div dpll_cfg;
828
829 /* IC ECO bug, need to set this register */
830 writel(0xc000c000, PMUSGRF_DDR_RGN_CON16);
831
832 /* clk_ddrc == DPLL = 24MHz / refdiv * fbdiv / postdiv1 / postdiv2 */
833 switch (set_rate) {
Jagan Teki09565682019-07-16 17:27:35 +0530834 case 50 * MHz:
835 dpll_cfg = (struct pll_div)
836 {.refdiv = 1, .fbdiv = 12, .postdiv1 = 3, .postdiv2 = 2};
837 break;
Jagan Tekidd7dfa22019-07-15 23:51:10 +0530838 case 200 * MHz:
Kever Yang5ae2fd92017-02-13 17:38:56 +0800839 dpll_cfg = (struct pll_div)
840 {.refdiv = 1, .fbdiv = 50, .postdiv1 = 6, .postdiv2 = 1};
841 break;
Jagan Tekidd7dfa22019-07-15 23:51:10 +0530842 case 300 * MHz:
Kever Yang5ae2fd92017-02-13 17:38:56 +0800843 dpll_cfg = (struct pll_div)
844 {.refdiv = 2, .fbdiv = 100, .postdiv1 = 4, .postdiv2 = 1};
845 break;
Jagan Tekif556d752019-07-16 17:27:36 +0530846 case 400 * MHz:
847 dpll_cfg = (struct pll_div)
848 {.refdiv = 1, .fbdiv = 50, .postdiv1 = 3, .postdiv2 = 1};
849 break;
Jagan Tekidd7dfa22019-07-15 23:51:10 +0530850 case 666 * MHz:
Kever Yang5ae2fd92017-02-13 17:38:56 +0800851 dpll_cfg = (struct pll_div)
852 {.refdiv = 2, .fbdiv = 111, .postdiv1 = 2, .postdiv2 = 1};
853 break;
Jagan Tekidd7dfa22019-07-15 23:51:10 +0530854 case 800 * MHz:
Kever Yang5ae2fd92017-02-13 17:38:56 +0800855 dpll_cfg = (struct pll_div)
856 {.refdiv = 1, .fbdiv = 100, .postdiv1 = 3, .postdiv2 = 1};
857 break;
Jagan Tekidd7dfa22019-07-15 23:51:10 +0530858 case 933 * MHz:
Kever Yang5ae2fd92017-02-13 17:38:56 +0800859 dpll_cfg = (struct pll_div)
860 {.refdiv = 1, .fbdiv = 116, .postdiv1 = 3, .postdiv2 = 1};
861 break;
862 default:
Masahiro Yamada9b643e32017-09-16 14:10:41 +0900863 pr_err("Unsupported SDRAM frequency!,%ld\n", set_rate);
Kever Yang5ae2fd92017-02-13 17:38:56 +0800864 }
865 rkclk_set_pll(&cru->dpll_con[0], &dpll_cfg);
866
867 return set_rate;
868}
David Wu364fc732017-09-20 14:38:58 +0800869
Jagan Tekib52a1992020-01-09 14:22:17 +0530870static ulong rk3399_saradc_get_clk(struct rockchip_cru *cru)
David Wu364fc732017-09-20 14:38:58 +0800871{
872 u32 div, val;
873
874 val = readl(&cru->clksel_con[26]);
875 div = bitfield_extract(val, CLK_SARADC_DIV_CON_SHIFT,
876 CLK_SARADC_DIV_CON_WIDTH);
877
878 return DIV_TO_RATE(OSC_HZ, div);
879}
880
Jagan Tekib52a1992020-01-09 14:22:17 +0530881static ulong rk3399_saradc_set_clk(struct rockchip_cru *cru, uint hz)
David Wu364fc732017-09-20 14:38:58 +0800882{
883 int src_clk_div;
884
885 src_clk_div = DIV_ROUND_UP(OSC_HZ, hz) - 1;
886 assert(src_clk_div < 128);
887
888 rk_clrsetreg(&cru->clksel_con[26],
889 CLK_SARADC_DIV_CON_MASK,
890 src_clk_div << CLK_SARADC_DIV_CON_SHIFT);
891
892 return rk3399_saradc_get_clk(cru);
893}
894
Kever Yangb0b3c862016-07-29 10:35:25 +0800895static ulong rk3399_clk_get_rate(struct clk *clk)
896{
897 struct rk3399_clk_priv *priv = dev_get_priv(clk->dev);
898 ulong rate = 0;
899
900 switch (clk->id) {
901 case 0 ... 63:
902 return 0;
Philipp Tomsich998c61a2017-04-25 09:52:06 +0200903 case HCLK_SDMMC:
Kever Yangb0b3c862016-07-29 10:35:25 +0800904 case SCLK_SDMMC:
905 case SCLK_EMMC:
906 rate = rk3399_mmc_get_clk(priv->cru, clk->id);
907 break;
908 case SCLK_I2C1:
909 case SCLK_I2C2:
910 case SCLK_I2C3:
911 case SCLK_I2C5:
912 case SCLK_I2C6:
913 case SCLK_I2C7:
914 rate = rk3399_i2c_get_clk(priv->cru, clk->id);
915 break;
Philipp Tomsich8fa69792017-04-20 22:05:49 +0200916 case SCLK_SPI0...SCLK_SPI5:
917 rate = rk3399_spi_get_clk(priv->cru, clk->id);
918 break;
919 case SCLK_UART0:
Christoph Muellner24615432019-05-07 10:58:44 +0200920 case SCLK_UART1:
Philipp Tomsich8fa69792017-04-20 22:05:49 +0200921 case SCLK_UART2:
Christoph Muellner24615432019-05-07 10:58:44 +0200922 case SCLK_UART3:
Philipp Tomsich8fa69792017-04-20 22:05:49 +0200923 return 24000000;
Philipp Tomsichffc1fac2017-04-28 18:33:57 +0200924 case PCLK_HDMI_CTRL:
925 break;
Kever Yangb0b3c862016-07-29 10:35:25 +0800926 case DCLK_VOP0:
927 case DCLK_VOP1:
928 break;
Philipp Tomsicha70feb42017-04-28 17:11:55 +0200929 case PCLK_EFUSE1024NS:
930 break;
David Wu364fc732017-09-20 14:38:58 +0800931 case SCLK_SARADC:
932 rate = rk3399_saradc_get_clk(priv->cru);
933 break;
Simon Glass5328af12019-01-21 14:53:30 -0700934 case ACLK_VIO:
935 case ACLK_HDCP:
936 case ACLK_GIC_PRE:
937 case PCLK_DDR:
938 break;
Kever Yangb0b3c862016-07-29 10:35:25 +0800939 default:
Simon Glass5328af12019-01-21 14:53:30 -0700940 log_debug("Unknown clock %lu\n", clk->id);
Kever Yangb0b3c862016-07-29 10:35:25 +0800941 return -ENOENT;
942 }
943
944 return rate;
945}
946
947static ulong rk3399_clk_set_rate(struct clk *clk, ulong rate)
948{
949 struct rk3399_clk_priv *priv = dev_get_priv(clk->dev);
950 ulong ret = 0;
951
952 switch (clk->id) {
953 case 0 ... 63:
954 return 0;
Philipp Tomsichd2f1f1a2018-01-08 14:00:27 +0100955
956 case ACLK_PERIHP:
957 case HCLK_PERIHP:
958 case PCLK_PERIHP:
959 return 0;
960
961 case ACLK_PERILP0:
962 case HCLK_PERILP0:
963 case PCLK_PERILP0:
964 return 0;
965
966 case ACLK_CCI:
967 return 0;
968
969 case HCLK_PERILP1:
970 case PCLK_PERILP1:
971 return 0;
972
Philipp Tomsich998c61a2017-04-25 09:52:06 +0200973 case HCLK_SDMMC:
Kever Yangb0b3c862016-07-29 10:35:25 +0800974 case SCLK_SDMMC:
975 case SCLK_EMMC:
976 ret = rk3399_mmc_set_clk(priv->cru, clk->id, rate);
977 break;
Philipp Tomsich65d83302017-03-24 19:24:25 +0100978 case SCLK_MAC:
Philipp Tomsicha45f17e2018-01-08 13:11:01 +0100979 ret = rk3399_gmac_set_clk(priv->cru, rate);
Philipp Tomsich65d83302017-03-24 19:24:25 +0100980 break;
Kever Yangb0b3c862016-07-29 10:35:25 +0800981 case SCLK_I2C1:
982 case SCLK_I2C2:
983 case SCLK_I2C3:
984 case SCLK_I2C5:
985 case SCLK_I2C6:
986 case SCLK_I2C7:
987 ret = rk3399_i2c_set_clk(priv->cru, clk->id, rate);
988 break;
Philipp Tomsich8fa69792017-04-20 22:05:49 +0200989 case SCLK_SPI0...SCLK_SPI5:
990 ret = rk3399_spi_set_clk(priv->cru, clk->id, rate);
991 break;
Philipp Tomsichffc1fac2017-04-28 18:33:57 +0200992 case PCLK_HDMI_CTRL:
993 case PCLK_VIO_GRF:
994 /* the PCLK gates for video are enabled by default */
995 break;
Kever Yangb0b3c862016-07-29 10:35:25 +0800996 case DCLK_VOP0:
997 case DCLK_VOP1:
Kever Yang5e79f442016-08-12 17:47:15 +0800998 ret = rk3399_vop_set_clk(priv->cru, clk->id, rate);
Kever Yangb0b3c862016-07-29 10:35:25 +0800999 break;
Jagan Tekib1bcd612020-04-02 17:11:21 +05301000 case ACLK_VOP1:
1001 case HCLK_VOP1:
Jagan Teki96993d72020-04-28 15:30:16 +05301002 case HCLK_SD:
Jagan Tekib1bcd612020-04-02 17:11:21 +05301003 /**
1004 * assigned-clocks handling won't require for vopl, so
1005 * return 0 to satisfy clk_set_defaults during device probe.
1006 */
1007 return 0;
Kever Yang5ae2fd92017-02-13 17:38:56 +08001008 case SCLK_DDRCLK:
1009 ret = rk3399_ddr_set_clk(priv->cru, rate);
1010 break;
Philipp Tomsicha70feb42017-04-28 17:11:55 +02001011 case PCLK_EFUSE1024NS:
1012 break;
David Wu364fc732017-09-20 14:38:58 +08001013 case SCLK_SARADC:
1014 ret = rk3399_saradc_set_clk(priv->cru, rate);
1015 break;
Simon Glass5328af12019-01-21 14:53:30 -07001016 case ACLK_VIO:
1017 case ACLK_HDCP:
1018 case ACLK_GIC_PRE:
1019 case PCLK_DDR:
1020 return 0;
Kever Yangb0b3c862016-07-29 10:35:25 +08001021 default:
Simon Glass5328af12019-01-21 14:53:30 -07001022 log_debug("Unknown clock %lu\n", clk->id);
Kever Yangb0b3c862016-07-29 10:35:25 +08001023 return -ENOENT;
1024 }
1025
1026 return ret;
1027}
1028
Jagan Tekidd7dfa22019-07-15 23:51:10 +05301029static int __maybe_unused rk3399_gmac_set_parent(struct clk *clk,
1030 struct clk *parent)
Philipp Tomsicha45f17e2018-01-08 13:11:01 +01001031{
1032 struct rk3399_clk_priv *priv = dev_get_priv(clk->dev);
1033 const char *clock_output_name;
1034 int ret;
1035
1036 /*
1037 * If the requested parent is in the same clock-controller and
1038 * the id is SCLK_MAC ("clk_gmac"), switch to the internal clock.
1039 */
Jagan Tekidd7dfa22019-07-15 23:51:10 +05301040 if (parent->dev == clk->dev && parent->id == SCLK_MAC) {
Philipp Tomsicha45f17e2018-01-08 13:11:01 +01001041 debug("%s: switching RGMII to SCLK_MAC\n", __func__);
1042 rk_clrreg(&priv->cru->clksel_con[19], BIT(4));
1043 return 0;
1044 }
1045
1046 /*
1047 * Otherwise, we need to check the clock-output-names of the
1048 * requested parent to see if the requested id is "clkin_gmac".
1049 */
1050 ret = dev_read_string_index(parent->dev, "clock-output-names",
1051 parent->id, &clock_output_name);
1052 if (ret < 0)
1053 return -ENODATA;
1054
1055 /* If this is "clkin_gmac", switch to the external clock input */
1056 if (!strcmp(clock_output_name, "clkin_gmac")) {
1057 debug("%s: switching RGMII to CLKIN\n", __func__);
1058 rk_setreg(&priv->cru->clksel_con[19], BIT(4));
1059 return 0;
1060 }
1061
1062 return -EINVAL;
1063}
1064
Jagan Tekidd7dfa22019-07-15 23:51:10 +05301065static int __maybe_unused rk3399_clk_set_parent(struct clk *clk,
1066 struct clk *parent)
Philipp Tomsicha45f17e2018-01-08 13:11:01 +01001067{
1068 switch (clk->id) {
1069 case SCLK_RMII_SRC:
1070 return rk3399_gmac_set_parent(clk, parent);
1071 }
1072
1073 debug("%s: unsupported clk %ld\n", __func__, clk->id);
1074 return -ENOENT;
1075}
1076
Jagan Teki30d09a22020-05-09 22:26:19 +05301077static int rk3399_clk_enable(struct clk *clk)
1078{
1079 struct rk3399_clk_priv *priv = dev_get_priv(clk->dev);
1080
1081 switch (clk->id) {
1082 case SCLK_MAC:
1083 rk_clrreg(&priv->cru->clkgate_con[5], BIT(5));
1084 break;
1085 case SCLK_MAC_RX:
1086 rk_clrreg(&priv->cru->clkgate_con[5], BIT(8));
1087 break;
1088 case SCLK_MAC_TX:
1089 rk_clrreg(&priv->cru->clkgate_con[5], BIT(9));
1090 break;
1091 case SCLK_MACREF:
1092 rk_clrreg(&priv->cru->clkgate_con[5], BIT(7));
1093 break;
1094 case SCLK_MACREF_OUT:
1095 rk_clrreg(&priv->cru->clkgate_con[5], BIT(6));
1096 break;
Jagan Tekif7dd12a2020-05-26 11:32:05 +08001097 case SCLK_USB2PHY0_REF:
1098 rk_clrreg(&priv->cru->clkgate_con[6], BIT(5));
1099 break;
1100 case SCLK_USB2PHY1_REF:
1101 rk_clrreg(&priv->cru->clkgate_con[6], BIT(6));
1102 break;
Jagan Teki30d09a22020-05-09 22:26:19 +05301103 case ACLK_GMAC:
1104 rk_clrreg(&priv->cru->clkgate_con[32], BIT(0));
1105 break;
1106 case PCLK_GMAC:
1107 rk_clrreg(&priv->cru->clkgate_con[32], BIT(2));
1108 break;
1109 case SCLK_USB3OTG0_REF:
1110 rk_clrreg(&priv->cru->clkgate_con[12], BIT(1));
1111 break;
1112 case SCLK_USB3OTG1_REF:
1113 rk_clrreg(&priv->cru->clkgate_con[12], BIT(2));
1114 break;
1115 case SCLK_USB3OTG0_SUSPEND:
1116 rk_clrreg(&priv->cru->clkgate_con[12], BIT(3));
1117 break;
1118 case SCLK_USB3OTG1_SUSPEND:
1119 rk_clrreg(&priv->cru->clkgate_con[12], BIT(4));
1120 break;
1121 case ACLK_USB3OTG0:
1122 rk_clrreg(&priv->cru->clkgate_con[30], BIT(1));
1123 break;
1124 case ACLK_USB3OTG1:
1125 rk_clrreg(&priv->cru->clkgate_con[30], BIT(2));
1126 break;
1127 case ACLK_USB3_RKSOC_AXI_PERF:
1128 rk_clrreg(&priv->cru->clkgate_con[30], BIT(3));
1129 break;
1130 case ACLK_USB3:
1131 rk_clrreg(&priv->cru->clkgate_con[12], BIT(0));
1132 break;
1133 case ACLK_USB3_GRF:
1134 rk_clrreg(&priv->cru->clkgate_con[30], BIT(4));
1135 break;
1136 case HCLK_HOST0:
1137 rk_clrreg(&priv->cru->clksel_con[20], BIT(5));
1138 break;
1139 case HCLK_HOST0_ARB:
1140 rk_clrreg(&priv->cru->clksel_con[20], BIT(6));
1141 break;
1142 case HCLK_HOST1:
1143 rk_clrreg(&priv->cru->clksel_con[20], BIT(7));
1144 break;
1145 case HCLK_HOST1_ARB:
1146 rk_clrreg(&priv->cru->clksel_con[20], BIT(8));
1147 break;
Jagan Teki912f6332020-05-09 22:26:20 +05301148 case SCLK_PCIEPHY_REF:
1149 rk_clrreg(&priv->cru->clksel_con[18], BIT(10));
1150 break;
Jagan Teki30d09a22020-05-09 22:26:19 +05301151 default:
1152 debug("%s: unsupported clk %ld\n", __func__, clk->id);
1153 return -ENOENT;
1154 }
1155
1156 return 0;
1157}
1158
1159static int rk3399_clk_disable(struct clk *clk)
1160{
1161 struct rk3399_clk_priv *priv = dev_get_priv(clk->dev);
1162
1163 switch (clk->id) {
1164 case SCLK_MAC:
1165 rk_setreg(&priv->cru->clkgate_con[5], BIT(5));
1166 break;
1167 case SCLK_MAC_RX:
1168 rk_setreg(&priv->cru->clkgate_con[5], BIT(8));
1169 break;
1170 case SCLK_MAC_TX:
1171 rk_setreg(&priv->cru->clkgate_con[5], BIT(9));
1172 break;
1173 case SCLK_MACREF:
1174 rk_setreg(&priv->cru->clkgate_con[5], BIT(7));
1175 break;
1176 case SCLK_MACREF_OUT:
1177 rk_setreg(&priv->cru->clkgate_con[5], BIT(6));
1178 break;
Jagan Tekif7dd12a2020-05-26 11:32:05 +08001179 case SCLK_USB2PHY0_REF:
1180 rk_setreg(&priv->cru->clkgate_con[6], BIT(5));
1181 break;
1182 case SCLK_USB2PHY1_REF:
1183 rk_setreg(&priv->cru->clkgate_con[6], BIT(6));
1184 break;
Jagan Teki30d09a22020-05-09 22:26:19 +05301185 case ACLK_GMAC:
1186 rk_setreg(&priv->cru->clkgate_con[32], BIT(0));
1187 break;
1188 case PCLK_GMAC:
1189 rk_setreg(&priv->cru->clkgate_con[32], BIT(2));
1190 break;
1191 case SCLK_USB3OTG0_REF:
1192 rk_setreg(&priv->cru->clkgate_con[12], BIT(1));
1193 break;
1194 case SCLK_USB3OTG1_REF:
1195 rk_setreg(&priv->cru->clkgate_con[12], BIT(2));
1196 break;
1197 case SCLK_USB3OTG0_SUSPEND:
1198 rk_setreg(&priv->cru->clkgate_con[12], BIT(3));
1199 break;
1200 case SCLK_USB3OTG1_SUSPEND:
1201 rk_setreg(&priv->cru->clkgate_con[12], BIT(4));
1202 break;
1203 case ACLK_USB3OTG0:
1204 rk_setreg(&priv->cru->clkgate_con[30], BIT(1));
1205 break;
1206 case ACLK_USB3OTG1:
1207 rk_setreg(&priv->cru->clkgate_con[30], BIT(2));
1208 break;
1209 case ACLK_USB3_RKSOC_AXI_PERF:
1210 rk_setreg(&priv->cru->clkgate_con[30], BIT(3));
1211 break;
1212 case ACLK_USB3:
1213 rk_setreg(&priv->cru->clkgate_con[12], BIT(0));
1214 break;
1215 case ACLK_USB3_GRF:
1216 rk_setreg(&priv->cru->clkgate_con[30], BIT(4));
1217 break;
1218 case HCLK_HOST0:
1219 rk_setreg(&priv->cru->clksel_con[20], BIT(5));
1220 break;
1221 case HCLK_HOST0_ARB:
1222 rk_setreg(&priv->cru->clksel_con[20], BIT(6));
1223 break;
1224 case HCLK_HOST1:
1225 rk_setreg(&priv->cru->clksel_con[20], BIT(7));
1226 break;
1227 case HCLK_HOST1_ARB:
1228 rk_setreg(&priv->cru->clksel_con[20], BIT(8));
1229 break;
Jagan Teki912f6332020-05-09 22:26:20 +05301230 case SCLK_PCIEPHY_REF:
1231 rk_clrreg(&priv->cru->clksel_con[18], BIT(10));
1232 break;
Jagan Teki30d09a22020-05-09 22:26:19 +05301233 default:
1234 debug("%s: unsupported clk %ld\n", __func__, clk->id);
1235 return -ENOENT;
1236 }
1237
1238 return 0;
1239}
1240
Kever Yangb0b3c862016-07-29 10:35:25 +08001241static struct clk_ops rk3399_clk_ops = {
1242 .get_rate = rk3399_clk_get_rate,
1243 .set_rate = rk3399_clk_set_rate,
Philipp Tomsich75b381a2018-01-25 15:27:10 +01001244#if CONFIG_IS_ENABLED(OF_CONTROL) && !CONFIG_IS_ENABLED(OF_PLATDATA)
Philipp Tomsicha45f17e2018-01-08 13:11:01 +01001245 .set_parent = rk3399_clk_set_parent,
Philipp Tomsich75b381a2018-01-25 15:27:10 +01001246#endif
Jagan Teki30d09a22020-05-09 22:26:19 +05301247 .enable = rk3399_clk_enable,
1248 .disable = rk3399_clk_disable,
Kever Yangb0b3c862016-07-29 10:35:25 +08001249};
1250
Kever Yang9f636a22017-10-12 15:27:29 +08001251#ifdef CONFIG_SPL_BUILD
Jagan Tekib52a1992020-01-09 14:22:17 +05301252static void rkclk_init(struct rockchip_cru *cru)
Kever Yang9f636a22017-10-12 15:27:29 +08001253{
1254 u32 aclk_div;
1255 u32 hclk_div;
1256 u32 pclk_div;
1257
Christoph Muellneraf765a42018-11-30 20:32:48 +01001258 rk3399_configure_cpu_l(cru, APLL_L_600_MHZ);
1259 rk3399_configure_cpu_b(cru, APLL_B_600_MHZ);
Kever Yang9f636a22017-10-12 15:27:29 +08001260 /*
1261 * some cru registers changed by bootrom, we'd better reset them to
1262 * reset/default values described in TRM to avoid confusion in kernel.
1263 * Please consider these three lines as a fix of bootrom bug.
1264 */
1265 rk_clrsetreg(&cru->clksel_con[12], 0xffff, 0x4101);
1266 rk_clrsetreg(&cru->clksel_con[19], 0xffff, 0x033f);
1267 rk_clrsetreg(&cru->clksel_con[56], 0x0003, 0x0003);
1268
1269 /* configure gpll cpll */
1270 rkclk_set_pll(&cru->gpll_con[0], &gpll_init_cfg);
1271 rkclk_set_pll(&cru->cpll_con[0], &cpll_init_cfg);
1272
1273 /* configure perihp aclk, hclk, pclk */
1274 aclk_div = GPLL_HZ / PERIHP_ACLK_HZ - 1;
1275 assert((aclk_div + 1) * PERIHP_ACLK_HZ == GPLL_HZ && aclk_div < 0x1f);
1276
1277 hclk_div = PERIHP_ACLK_HZ / PERIHP_HCLK_HZ - 1;
1278 assert((hclk_div + 1) * PERIHP_HCLK_HZ ==
1279 PERIHP_ACLK_HZ && (hclk_div < 0x4));
1280
1281 pclk_div = PERIHP_ACLK_HZ / PERIHP_PCLK_HZ - 1;
1282 assert((pclk_div + 1) * PERIHP_PCLK_HZ ==
1283 PERIHP_ACLK_HZ && (pclk_div < 0x7));
1284
1285 rk_clrsetreg(&cru->clksel_con[14],
1286 PCLK_PERIHP_DIV_CON_MASK | HCLK_PERIHP_DIV_CON_MASK |
1287 ACLK_PERIHP_PLL_SEL_MASK | ACLK_PERIHP_DIV_CON_MASK,
1288 pclk_div << PCLK_PERIHP_DIV_CON_SHIFT |
1289 hclk_div << HCLK_PERIHP_DIV_CON_SHIFT |
1290 ACLK_PERIHP_PLL_SEL_GPLL << ACLK_PERIHP_PLL_SEL_SHIFT |
1291 aclk_div << ACLK_PERIHP_DIV_CON_SHIFT);
1292
1293 /* configure perilp0 aclk, hclk, pclk */
1294 aclk_div = GPLL_HZ / PERILP0_ACLK_HZ - 1;
1295 assert((aclk_div + 1) * PERILP0_ACLK_HZ == GPLL_HZ && aclk_div < 0x1f);
1296
1297 hclk_div = PERILP0_ACLK_HZ / PERILP0_HCLK_HZ - 1;
1298 assert((hclk_div + 1) * PERILP0_HCLK_HZ ==
1299 PERILP0_ACLK_HZ && (hclk_div < 0x4));
1300
1301 pclk_div = PERILP0_ACLK_HZ / PERILP0_PCLK_HZ - 1;
1302 assert((pclk_div + 1) * PERILP0_PCLK_HZ ==
1303 PERILP0_ACLK_HZ && (pclk_div < 0x7));
1304
1305 rk_clrsetreg(&cru->clksel_con[23],
1306 PCLK_PERILP0_DIV_CON_MASK | HCLK_PERILP0_DIV_CON_MASK |
1307 ACLK_PERILP0_PLL_SEL_MASK | ACLK_PERILP0_DIV_CON_MASK,
1308 pclk_div << PCLK_PERILP0_DIV_CON_SHIFT |
1309 hclk_div << HCLK_PERILP0_DIV_CON_SHIFT |
1310 ACLK_PERILP0_PLL_SEL_GPLL << ACLK_PERILP0_PLL_SEL_SHIFT |
1311 aclk_div << ACLK_PERILP0_DIV_CON_SHIFT);
1312
1313 /* perilp1 hclk select gpll as source */
1314 hclk_div = GPLL_HZ / PERILP1_HCLK_HZ - 1;
1315 assert((hclk_div + 1) * PERILP1_HCLK_HZ ==
1316 GPLL_HZ && (hclk_div < 0x1f));
1317
1318 pclk_div = PERILP1_HCLK_HZ / PERILP1_HCLK_HZ - 1;
1319 assert((pclk_div + 1) * PERILP1_HCLK_HZ ==
1320 PERILP1_HCLK_HZ && (hclk_div < 0x7));
1321
1322 rk_clrsetreg(&cru->clksel_con[25],
1323 PCLK_PERILP1_DIV_CON_MASK | HCLK_PERILP1_DIV_CON_MASK |
1324 HCLK_PERILP1_PLL_SEL_MASK,
1325 pclk_div << PCLK_PERILP1_DIV_CON_SHIFT |
1326 hclk_div << HCLK_PERILP1_DIV_CON_SHIFT |
1327 HCLK_PERILP1_PLL_SEL_GPLL << HCLK_PERILP1_PLL_SEL_SHIFT);
1328}
1329#endif
1330
Kever Yangb0b3c862016-07-29 10:35:25 +08001331static int rk3399_clk_probe(struct udevice *dev)
1332{
Kever Yang5ae2fd92017-02-13 17:38:56 +08001333#ifdef CONFIG_SPL_BUILD
Kever Yangb0b3c862016-07-29 10:35:25 +08001334 struct rk3399_clk_priv *priv = dev_get_priv(dev);
1335
Kever Yang5ae2fd92017-02-13 17:38:56 +08001336#if CONFIG_IS_ENABLED(OF_PLATDATA)
1337 struct rk3399_clk_plat *plat = dev_get_platdata(dev);
Kever Yangb0b3c862016-07-29 10:35:25 +08001338
Simon Glassc20ee0e2017-08-29 14:15:50 -06001339 priv->cru = map_sysmem(plat->dtd.reg[0], plat->dtd.reg[1]);
Kever Yang5ae2fd92017-02-13 17:38:56 +08001340#endif
1341 rkclk_init(priv->cru);
1342#endif
Kever Yangb0b3c862016-07-29 10:35:25 +08001343 return 0;
1344}
1345
1346static int rk3399_clk_ofdata_to_platdata(struct udevice *dev)
1347{
Kever Yang5ae2fd92017-02-13 17:38:56 +08001348#if !CONFIG_IS_ENABLED(OF_PLATDATA)
Kever Yangb0b3c862016-07-29 10:35:25 +08001349 struct rk3399_clk_priv *priv = dev_get_priv(dev);
1350
Philipp Tomsich75c78592017-09-12 17:32:24 +02001351 priv->cru = dev_read_addr_ptr(dev);
Kever Yang5ae2fd92017-02-13 17:38:56 +08001352#endif
Kever Yangb0b3c862016-07-29 10:35:25 +08001353 return 0;
1354}
1355
1356static int rk3399_clk_bind(struct udevice *dev)
1357{
1358 int ret;
Kever Yangf24e36d2017-11-03 15:16:13 +08001359 struct udevice *sys_child;
1360 struct sysreset_reg *priv;
Kever Yangb0b3c862016-07-29 10:35:25 +08001361
1362 /* The reset driver does not have a device node, so bind it here */
Kever Yangf24e36d2017-11-03 15:16:13 +08001363 ret = device_bind_driver(dev, "rockchip_sysreset", "sysreset",
1364 &sys_child);
1365 if (ret) {
1366 debug("Warning: No sysreset driver: ret=%d\n", ret);
1367 } else {
1368 priv = malloc(sizeof(struct sysreset_reg));
Jagan Tekib52a1992020-01-09 14:22:17 +05301369 priv->glb_srst_fst_value = offsetof(struct rockchip_cru,
Kever Yangf24e36d2017-11-03 15:16:13 +08001370 glb_srst_fst_value);
Jagan Tekib52a1992020-01-09 14:22:17 +05301371 priv->glb_srst_snd_value = offsetof(struct rockchip_cru,
Kever Yangf24e36d2017-11-03 15:16:13 +08001372 glb_srst_snd_value);
1373 sys_child->priv = priv;
1374 }
Kever Yangb0b3c862016-07-29 10:35:25 +08001375
Heiko Stuebnera5ada252019-11-09 00:06:30 +01001376#if CONFIG_IS_ENABLED(RESET_ROCKCHIP)
Jagan Tekib52a1992020-01-09 14:22:17 +05301377 ret = offsetof(struct rockchip_cru, softrst_con[0]);
Elaine Zhang538f67c2017-12-19 18:22:38 +08001378 ret = rockchip_reset_bind(dev, ret, 21);
1379 if (ret)
1380 debug("Warning: software reset driver bind faile\n");
1381#endif
1382
Kever Yangb0b3c862016-07-29 10:35:25 +08001383 return 0;
1384}
1385
1386static const struct udevice_id rk3399_clk_ids[] = {
1387 { .compatible = "rockchip,rk3399-cru" },
1388 { }
1389};
1390
1391U_BOOT_DRIVER(clk_rk3399) = {
Kever Yang5ae2fd92017-02-13 17:38:56 +08001392 .name = "rockchip_rk3399_cru",
Kever Yangb0b3c862016-07-29 10:35:25 +08001393 .id = UCLASS_CLK,
1394 .of_match = rk3399_clk_ids,
1395 .priv_auto_alloc_size = sizeof(struct rk3399_clk_priv),
1396 .ofdata_to_platdata = rk3399_clk_ofdata_to_platdata,
1397 .ops = &rk3399_clk_ops,
1398 .bind = rk3399_clk_bind,
1399 .probe = rk3399_clk_probe,
Kever Yang5ae2fd92017-02-13 17:38:56 +08001400#if CONFIG_IS_ENABLED(OF_PLATDATA)
1401 .platdata_auto_alloc_size = sizeof(struct rk3399_clk_plat),
1402#endif
Kever Yangb0b3c862016-07-29 10:35:25 +08001403};
Kever Yang5e79f442016-08-12 17:47:15 +08001404
1405static ulong rk3399_i2c_get_pmuclk(struct rk3399_pmucru *pmucru, ulong clk_id)
1406{
1407 u32 div, con;
1408
1409 switch (clk_id) {
1410 case SCLK_I2C0_PMU:
1411 con = readl(&pmucru->pmucru_clksel[2]);
1412 div = I2C_CLK_DIV_VALUE(con, 0);
1413 break;
1414 case SCLK_I2C4_PMU:
1415 con = readl(&pmucru->pmucru_clksel[3]);
1416 div = I2C_CLK_DIV_VALUE(con, 4);
1417 break;
1418 case SCLK_I2C8_PMU:
1419 con = readl(&pmucru->pmucru_clksel[2]);
1420 div = I2C_CLK_DIV_VALUE(con, 8);
1421 break;
1422 default:
1423 printf("do not support this i2c bus\n");
1424 return -EINVAL;
1425 }
1426
1427 return DIV_TO_RATE(PPLL_HZ, div);
1428}
1429
1430static ulong rk3399_i2c_set_pmuclk(struct rk3399_pmucru *pmucru, ulong clk_id,
1431 uint hz)
1432{
1433 int src_clk_div;
1434
1435 src_clk_div = PPLL_HZ / hz;
1436 assert(src_clk_div - 1 < 127);
1437
1438 switch (clk_id) {
1439 case SCLK_I2C0_PMU:
1440 rk_clrsetreg(&pmucru->pmucru_clksel[2], I2C_PMUCLK_REG_MASK(0),
1441 I2C_PMUCLK_REG_VALUE(0, src_clk_div));
1442 break;
1443 case SCLK_I2C4_PMU:
1444 rk_clrsetreg(&pmucru->pmucru_clksel[3], I2C_PMUCLK_REG_MASK(4),
1445 I2C_PMUCLK_REG_VALUE(4, src_clk_div));
1446 break;
1447 case SCLK_I2C8_PMU:
1448 rk_clrsetreg(&pmucru->pmucru_clksel[2], I2C_PMUCLK_REG_MASK(8),
1449 I2C_PMUCLK_REG_VALUE(8, src_clk_div));
1450 break;
1451 default:
1452 printf("do not support this i2c bus\n");
1453 return -EINVAL;
1454 }
1455
1456 return DIV_TO_RATE(PPLL_HZ, src_clk_div);
1457}
1458
1459static ulong rk3399_pwm_get_clk(struct rk3399_pmucru *pmucru)
1460{
1461 u32 div, con;
1462
1463 /* PWM closk rate is same as pclk_pmu */
1464 con = readl(&pmucru->pmucru_clksel[0]);
1465 div = con & PMU_PCLK_DIV_CON_MASK;
1466
1467 return DIV_TO_RATE(PPLL_HZ, div);
1468}
1469
1470static ulong rk3399_pmuclk_get_rate(struct clk *clk)
1471{
1472 struct rk3399_pmuclk_priv *priv = dev_get_priv(clk->dev);
1473 ulong rate = 0;
1474
1475 switch (clk->id) {
Philipp Tomsich434d5a02018-02-23 17:36:41 +01001476 case PLL_PPLL:
1477 return PPLL_HZ;
Kever Yang5e79f442016-08-12 17:47:15 +08001478 case PCLK_RKPWM_PMU:
1479 rate = rk3399_pwm_get_clk(priv->pmucru);
1480 break;
1481 case SCLK_I2C0_PMU:
1482 case SCLK_I2C4_PMU:
1483 case SCLK_I2C8_PMU:
1484 rate = rk3399_i2c_get_pmuclk(priv->pmucru, clk->id);
1485 break;
1486 default:
1487 return -ENOENT;
1488 }
1489
1490 return rate;
1491}
1492
1493static ulong rk3399_pmuclk_set_rate(struct clk *clk, ulong rate)
1494{
1495 struct rk3399_pmuclk_priv *priv = dev_get_priv(clk->dev);
1496 ulong ret = 0;
1497
1498 switch (clk->id) {
Philipp Tomsich434d5a02018-02-23 17:36:41 +01001499 case PLL_PPLL:
1500 /*
1501 * This has already been set up and we don't want/need
1502 * to change it here. Accept the request though, as the
1503 * device-tree has this in an 'assigned-clocks' list.
1504 */
1505 return PPLL_HZ;
Kever Yang5e79f442016-08-12 17:47:15 +08001506 case SCLK_I2C0_PMU:
1507 case SCLK_I2C4_PMU:
1508 case SCLK_I2C8_PMU:
1509 ret = rk3399_i2c_set_pmuclk(priv->pmucru, clk->id, rate);
1510 break;
1511 default:
1512 return -ENOENT;
1513 }
1514
1515 return ret;
1516}
1517
1518static struct clk_ops rk3399_pmuclk_ops = {
1519 .get_rate = rk3399_pmuclk_get_rate,
1520 .set_rate = rk3399_pmuclk_set_rate,
1521};
1522
Kever Yang5ae2fd92017-02-13 17:38:56 +08001523#ifndef CONFIG_SPL_BUILD
Kever Yang5e79f442016-08-12 17:47:15 +08001524static void pmuclk_init(struct rk3399_pmucru *pmucru)
1525{
1526 u32 pclk_div;
1527
1528 /* configure pmu pll(ppll) */
1529 rkclk_set_pll(&pmucru->ppll_con[0], &ppll_init_cfg);
1530
1531 /* configure pmu pclk */
1532 pclk_div = PPLL_HZ / PMU_PCLK_HZ - 1;
Kever Yang5e79f442016-08-12 17:47:15 +08001533 rk_clrsetreg(&pmucru->pmucru_clksel[0],
1534 PMU_PCLK_DIV_CON_MASK,
1535 pclk_div << PMU_PCLK_DIV_CON_SHIFT);
1536}
Kever Yang5ae2fd92017-02-13 17:38:56 +08001537#endif
Kever Yang5e79f442016-08-12 17:47:15 +08001538
1539static int rk3399_pmuclk_probe(struct udevice *dev)
1540{
Philipp Tomsich61dff332017-03-24 19:24:24 +01001541#if CONFIG_IS_ENABLED(OF_PLATDATA) || !defined(CONFIG_SPL_BUILD)
Kever Yang5e79f442016-08-12 17:47:15 +08001542 struct rk3399_pmuclk_priv *priv = dev_get_priv(dev);
Philipp Tomsich61dff332017-03-24 19:24:24 +01001543#endif
Kever Yang5e79f442016-08-12 17:47:15 +08001544
Kever Yang5ae2fd92017-02-13 17:38:56 +08001545#if CONFIG_IS_ENABLED(OF_PLATDATA)
1546 struct rk3399_pmuclk_plat *plat = dev_get_platdata(dev);
Kever Yang5e79f442016-08-12 17:47:15 +08001547
Simon Glassc20ee0e2017-08-29 14:15:50 -06001548 priv->pmucru = map_sysmem(plat->dtd.reg[0], plat->dtd.reg[1]);
Kever Yang5ae2fd92017-02-13 17:38:56 +08001549#endif
1550
1551#ifndef CONFIG_SPL_BUILD
1552 pmuclk_init(priv->pmucru);
1553#endif
Kever Yang5e79f442016-08-12 17:47:15 +08001554 return 0;
1555}
1556
1557static int rk3399_pmuclk_ofdata_to_platdata(struct udevice *dev)
1558{
Kever Yang5ae2fd92017-02-13 17:38:56 +08001559#if !CONFIG_IS_ENABLED(OF_PLATDATA)
Kever Yang5e79f442016-08-12 17:47:15 +08001560 struct rk3399_pmuclk_priv *priv = dev_get_priv(dev);
1561
Philipp Tomsich75c78592017-09-12 17:32:24 +02001562 priv->pmucru = dev_read_addr_ptr(dev);
Kever Yang5ae2fd92017-02-13 17:38:56 +08001563#endif
Kever Yang5e79f442016-08-12 17:47:15 +08001564 return 0;
1565}
1566
Elaine Zhang538f67c2017-12-19 18:22:38 +08001567static int rk3399_pmuclk_bind(struct udevice *dev)
1568{
1569#if CONFIG_IS_ENABLED(CONFIG_RESET_ROCKCHIP)
1570 int ret;
1571
1572 ret = offsetof(struct rk3399_pmucru, pmucru_softrst_con[0]);
1573 ret = rockchip_reset_bind(dev, ret, 2);
1574 if (ret)
1575 debug("Warning: software reset driver bind faile\n");
1576#endif
1577 return 0;
1578}
1579
Kever Yang5e79f442016-08-12 17:47:15 +08001580static const struct udevice_id rk3399_pmuclk_ids[] = {
1581 { .compatible = "rockchip,rk3399-pmucru" },
1582 { }
1583};
1584
Simon Glassc8a6bc92016-10-01 20:04:51 -06001585U_BOOT_DRIVER(rockchip_rk3399_pmuclk) = {
Kever Yang5ae2fd92017-02-13 17:38:56 +08001586 .name = "rockchip_rk3399_pmucru",
Kever Yang5e79f442016-08-12 17:47:15 +08001587 .id = UCLASS_CLK,
1588 .of_match = rk3399_pmuclk_ids,
1589 .priv_auto_alloc_size = sizeof(struct rk3399_pmuclk_priv),
1590 .ofdata_to_platdata = rk3399_pmuclk_ofdata_to_platdata,
1591 .ops = &rk3399_pmuclk_ops,
1592 .probe = rk3399_pmuclk_probe,
Elaine Zhang538f67c2017-12-19 18:22:38 +08001593 .bind = rk3399_pmuclk_bind,
Kever Yang5ae2fd92017-02-13 17:38:56 +08001594#if CONFIG_IS_ENABLED(OF_PLATDATA)
1595 .platdata_auto_alloc_size = sizeof(struct rk3399_pmuclk_plat),
1596#endif
Kever Yang5e79f442016-08-12 17:47:15 +08001597};