blob: 1ea41f3c5b2e1f8774d07a6a4117cc27ad5dcf1a [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0
Kever Yangca19eac2016-07-29 10:35:25 +08002/*
3 * (C) Copyright 2015 Google, Inc
Philipp Tomsichc31ee922017-04-20 22:05:49 +02004 * (C) 2017 Theobroma Systems Design und Consulting GmbH
Kever Yangca19eac2016-07-29 10:35:25 +08005 */
6
7#include <common.h>
8#include <clk-uclass.h>
9#include <dm.h>
Kever Yange1980532017-02-13 17:38:56 +080010#include <dt-structs.h>
Kever Yangca19eac2016-07-29 10:35:25 +080011#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>
Kever Yange1980532017-02-13 17:38:56 +080014#include <mapmem.h>
Kever Yangca19eac2016-07-29 10:35:25 +080015#include <syscon.h>
David Wuf91b9b42017-09-20 14:38:58 +080016#include <bitfield.h>
Kever Yangca19eac2016-07-29 10:35:25 +080017#include <asm/io.h>
Kever Yang9fbe17c2019-03-28 11:01:23 +080018#include <asm/arch-rockchip/clock.h>
Jagan Teki783acfd2020-01-09 14:22:17 +053019#include <asm/arch-rockchip/cru.h>
Kever Yang9fbe17c2019-03-28 11:01:23 +080020#include <asm/arch-rockchip/hardware.h>
Kever Yangca19eac2016-07-29 10:35:25 +080021#include <dm/lists.h>
22#include <dt-bindings/clock/rk3399-cru.h>
Simon Glass4dcacfc2020-05-10 11:40:13 -060023#include <linux/bitops.h>
Simon Glassdbd79542020-05-10 11:40:11 -060024#include <linux/delay.h>
Kever Yangca19eac2016-07-29 10:35:25 +080025
Kever Yange1980532017-02-13 17:38:56 +080026#if CONFIG_IS_ENABLED(OF_PLATDATA)
27struct rk3399_clk_plat {
28 struct dtd_rockchip_rk3399_cru dtd;
Kever Yange54d26a2016-08-12 17:47:15 +080029};
30
Kever Yange1980532017-02-13 17:38:56 +080031struct rk3399_pmuclk_plat {
32 struct dtd_rockchip_rk3399_pmucru dtd;
33};
34#endif
35
Kever Yangca19eac2016-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 Tekibef02a32019-07-15 23:51:10 +053045 ((input_rate) / (output_rate) - 1)
46#define DIV_TO_RATE(input_rate, div) ((input_rate) / ((div) + 1))
Kever Yangca19eac2016-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 Tomsichcf0a4ba2017-03-24 19:24:24 +010053#if defined(CONFIG_SPL_BUILD)
Kever Yangca19eac2016-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 Tomsichcf0a4ba2017-03-24 19:24:24 +010056#else
Kever Yangca19eac2016-07-29 10:35:25 +080057static const struct pll_div ppll_init_cfg = PLL_DIVISORS(PPLL_HZ, 2, 2, 1);
Philipp Tomsichcf0a4ba2017-03-24 19:24:24 +010058#endif
Kever Yangca19eac2016-07-29 10:35:25 +080059
Jagan Tekibef02a32019-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 Yangca19eac2016-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 Tekibef02a32019-07-15 23:51:10 +053068static const struct pll_div apll_b_600_cfg = PLL_DIVISORS(600 * MHz, 1, 2, 1);
Christoph Muellner25c7ba92018-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 Yangca19eac2016-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 Yange54d26a2016-08-12 17:47:15 +0800116 CLK_I2C8_DIV_CON_SHIFT = 8,
117 CLK_I2C0_DIV_CON_SHIFT = 0,
Kever Yangca19eac2016-07-29 10:35:25 +0800118
119 /* PMUCRU_CLKSEL_CON3 */
Kever Yange54d26a2016-08-12 17:47:15 +0800120 CLK_I2C4_DIV_CON_SHIFT = 0,
Kever Yangca19eac2016-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 Muellner25c7ba92018-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 Yangca19eac2016-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 Yangdc850de2016-08-04 11:44:58 +0800181 CLK_EMMC_PLL_SEL_24M = 0x5,
Kever Yangca19eac2016-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 Wuf91b9b42017-09-20 14:38:58 +0800209 CLK_SARADC_DIV_CON_MASK = GENMASK(15, 8),
210 CLK_SARADC_DIV_CON_WIDTH = 8,
Kever Yangca19eac2016-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 Tomsichc31ee922017-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 Yangca19eac2016-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 Tekibef02a32019-07-15 23:51:10 +0530400 if (fbdiv >= max_fbdiv || fbdiv <= min_fbdiv)
Kever Yangca19eac2016-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 Tekibef02a32019-07-15 23:51:10 +0530416 if (best_diff_khz > 4 * (MHz / KHz)) {
Kever Yangca19eac2016-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 Teki783acfd2020-01-09 14:22:17 +0530425void rk3399_configure_cpu_l(struct rockchip_cru *cru,
Christoph Muellner25c7ba92018-11-30 20:32:48 +0100426 enum apll_l_frequencies apll_l_freq)
Kever Yangca19eac2016-07-29 10:35:25 +0800427{
428 u32 aclkm_div;
429 u32 pclk_dbg_div;
430 u32 atclk_div;
431
Christoph Muellner25c7ba92018-11-30 20:32:48 +0100432 /* Setup cluster L */
Kever Yangca19eac2016-07-29 10:35:25 +0800433 rkclk_set_pll(&cru->apll_l_con[0], apll_l_cfgs[apll_l_freq]);
434
Christoph Muellner25c7ba92018-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 Yangca19eac2016-07-29 10:35:25 +0800437 aclkm_div < 0x1f);
438
Christoph Muellner25c7ba92018-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 Yangca19eac2016-07-29 10:35:25 +0800441 pclk_dbg_div < 0x1f);
442
Christoph Muellner25c7ba92018-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 Yangca19eac2016-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 Muellner25c7ba92018-11-30 20:32:48 +0100459
Jagan Teki783acfd2020-01-09 14:22:17 +0530460void rk3399_configure_cpu_b(struct rockchip_cru *cru,
Christoph Muellner25c7ba92018-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 Yangca19eac2016-07-29 10:35:25 +0800495#define I2C_CLK_REG_MASK(bus) \
Jagan Tekibef02a32019-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 Yangca19eac2016-07-29 10:35:25 +0800498
499#define I2C_CLK_REG_VALUE(bus, clk_div) \
Jagan Tekibef02a32019-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 Yangca19eac2016-07-29 10:35:25 +0800502
503#define I2C_CLK_DIV_VALUE(con, bus) \
Jagan Tekibef02a32019-07-15 23:51:10 +0530504 ((con >> CLK_I2C ##bus## _DIV_CON_SHIFT) & I2C_DIV_CON_MASK)
Kever Yangca19eac2016-07-29 10:35:25 +0800505
Kever Yange54d26a2016-08-12 17:47:15 +0800506#define I2C_PMUCLK_REG_MASK(bus) \
Jagan Tekibef02a32019-07-15 23:51:10 +0530507 (I2C_DIV_CON_MASK << CLK_I2C ##bus## _DIV_CON_SHIFT)
Kever Yange54d26a2016-08-12 17:47:15 +0800508
509#define I2C_PMUCLK_REG_VALUE(bus, clk_div) \
Jagan Tekibef02a32019-07-15 23:51:10 +0530510 ((clk_div - 1) << CLK_I2C ##bus## _DIV_CON_SHIFT)
Kever Yange54d26a2016-08-12 17:47:15 +0800511
Jagan Teki783acfd2020-01-09 14:22:17 +0530512static ulong rk3399_i2c_get_clk(struct rockchip_cru *cru, ulong clk_id)
Kever Yangca19eac2016-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 Teki783acfd2020-01-09 14:22:17 +0530549static ulong rk3399_i2c_set_clk(struct rockchip_cru *cru, ulong clk_id, uint hz)
Kever Yangca19eac2016-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 Tomsich30d7c152017-04-20 22:05:50 +0200587 return rk3399_i2c_get_clk(cru, clk_id);
Kever Yangca19eac2016-07-29 10:35:25 +0800588}
589
Philipp Tomsichc31ee922017-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 Tekibef02a32019-07-15 23:51:10 +0530597 u8 reg; /* CLKSEL_CON[reg] register in CRU */
598 u8 div_shift;
599 u8 sel_shift;
Philipp Tomsichc31ee922017-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 Teki783acfd2020-01-09 14:22:17 +0530626static ulong rk3399_spi_get_clk(struct rockchip_cru *cru, ulong clk_id)
Philipp Tomsichc31ee922017-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 Yamada81e10422017-09-16 14:10:41 +0900637 pr_err("%s: SPI clk-id %ld not supported\n", __func__, clk_id);
Philipp Tomsichc31ee922017-04-20 22:05:49 +0200638 return -EINVAL;
639 }
640
641 val = readl(&cru->clksel_con[spiclk->reg]);
Philipp Tomsich8a4868f2017-11-22 19:45:04 +0100642 div = bitfield_extract(val, spiclk->div_shift,
643 CLK_SPI_PLL_DIV_CON_WIDTH);
Philipp Tomsichc31ee922017-04-20 22:05:49 +0200644
645 return DIV_TO_RATE(GPLL_HZ, div);
646}
647
Jagan Teki783acfd2020-01-09 14:22:17 +0530648static ulong rk3399_spi_set_clk(struct rockchip_cru *cru, ulong clk_id, uint hz)
Philipp Tomsichc31ee922017-04-20 22:05:49 +0200649{
650 const struct spi_clkreg *spiclk = NULL;
651 int src_clk_div;
652
Kever Yangf20995b2017-07-27 12:54:02 +0800653 src_clk_div = DIV_ROUND_UP(GPLL_HZ, hz) - 1;
654 assert(src_clk_div < 128);
Philipp Tomsichc31ee922017-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 Yamada81e10422017-09-16 14:10:41 +0900662 pr_err("%s: SPI clk-id %ld not supported\n", __func__, clk_id);
Philipp Tomsichc31ee922017-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 Tomsich30d7c152017-04-20 22:05:50 +0200672 return rk3399_spi_get_clk(cru, clk_id);
Philipp Tomsichc31ee922017-04-20 22:05:49 +0200673}
674
Jagan Teki783acfd2020-01-09 14:22:17 +0530675static ulong rk3399_vop_set_clk(struct rockchip_cru *cru, ulong clk_id, u32 hz)
Kever Yangca19eac2016-07-29 10:35:25 +0800676{
677 struct pll_div vpll_config = {0};
Jagan Tekibef02a32019-07-15 23:51:10 +0530678 int aclk_vop = 198 * MHz;
Kever Yangca19eac2016-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 Tekibef02a32019-07-15 23:51:10 +0530710 DCLK_VOP_DCLK_SEL_MASK | DCLK_VOP_PLL_SEL_MASK |
Kever Yangca19eac2016-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 Teki783acfd2020-01-09 14:22:17 +0530719static ulong rk3399_mmc_get_clk(struct rockchip_cru *cru, uint clk_id)
Kever Yangca19eac2016-07-29 10:35:25 +0800720{
721 u32 div, con;
722
723 switch (clk_id) {
Philipp Tomsich78a73142017-04-25 09:52:06 +0200724 case HCLK_SDMMC:
Kever Yangca19eac2016-07-29 10:35:25 +0800725 case SCLK_SDMMC:
726 con = readl(&cru->clksel_con[16]);
Kever Yang99b546d2017-07-27 12:54:01 +0800727 /* dwmmc controller have internal div 2 */
728 div = 2;
Kever Yangca19eac2016-07-29 10:35:25 +0800729 break;
730 case SCLK_EMMC:
Jagan Tekiad386002020-05-24 22:13:15 +0530731 con = readl(&cru->clksel_con[22]);
Kever Yang99b546d2017-07-27 12:54:01 +0800732 div = 1;
Kever Yangca19eac2016-07-29 10:35:25 +0800733 break;
734 default:
735 return -EINVAL;
736 }
Kever Yangca19eac2016-07-29 10:35:25 +0800737
Kever Yang99b546d2017-07-27 12:54:01 +0800738 div *= (con & CLK_EMMC_DIV_CON_MASK) >> CLK_EMMC_DIV_CON_SHIFT;
Kever Yangdc850de2016-08-04 11:44:58 +0800739 if ((con & CLK_EMMC_PLL_MASK) >> CLK_EMMC_PLL_SHIFT
740 == CLK_EMMC_PLL_SEL_24M)
Kever Yang99b546d2017-07-27 12:54:01 +0800741 return DIV_TO_RATE(OSC_HZ, div);
Kever Yangdc850de2016-08-04 11:44:58 +0800742 else
743 return DIV_TO_RATE(GPLL_HZ, div);
Kever Yangca19eac2016-07-29 10:35:25 +0800744}
745
Jagan Teki783acfd2020-01-09 14:22:17 +0530746static ulong rk3399_mmc_set_clk(struct rockchip_cru *cru,
Kever Yangca19eac2016-07-29 10:35:25 +0800747 ulong clk_id, ulong set_rate)
748{
749 int src_clk_div;
Jagan Tekibef02a32019-07-15 23:51:10 +0530750 int aclk_emmc = 198 * MHz;
Kever Yangca19eac2016-07-29 10:35:25 +0800751
752 switch (clk_id) {
Philipp Tomsich78a73142017-04-25 09:52:06 +0200753 case HCLK_SDMMC:
Kever Yangca19eac2016-07-29 10:35:25 +0800754 case SCLK_SDMMC:
Kever Yangdc850de2016-08-04 11:44:58 +0800755 /* Select clk_sdmmc source from GPLL by default */
Kever Yang99b546d2017-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 Yangca19eac2016-07-29 10:35:25 +0800758
Kever Yangf20995b2017-07-27 12:54:02 +0800759 if (src_clk_div > 128) {
Kever Yangdc850de2016-08-04 11:44:58 +0800760 /* use 24MHz source for 400KHz clock */
Kever Yang99b546d2017-07-27 12:54:01 +0800761 src_clk_div = DIV_ROUND_UP(OSC_HZ / 2, set_rate);
Kever Yangf20995b2017-07-27 12:54:02 +0800762 assert(src_clk_div - 1 < 128);
Kever Yangdc850de2016-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 Yangca19eac2016-07-29 10:35:25 +0800773 break;
774 case SCLK_EMMC:
775 /* Select aclk_emmc source from GPLL */
Jagan Tekibef02a32019-07-15 23:51:10 +0530776 src_clk_div = DIV_ROUND_UP(GPLL_HZ, aclk_emmc);
Kever Yangf20995b2017-07-27 12:54:02 +0800777 assert(src_clk_div - 1 < 32);
Kever Yangca19eac2016-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 Yangf20995b2017-07-27 12:54:02 +0800785 src_clk_div = DIV_ROUND_UP(GPLL_HZ, set_rate);
786 assert(src_clk_div - 1 < 128);
Kever Yangca19eac2016-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 Teki783acfd2020-01-09 14:22:17 +0530799static ulong rk3399_gmac_set_clk(struct rockchip_cru *cru, ulong rate)
Philipp Tomsichf4ba6ed2018-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 Yange1980532017-02-13 17:38:56 +0800823#define PMUSGRF_DDR_RGN_CON16 0xff330040
Jagan Teki783acfd2020-01-09 14:22:17 +0530824static ulong rk3399_ddr_set_clk(struct rockchip_cru *cru,
Kever Yange1980532017-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 Teki4833f322019-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 Tekibef02a32019-07-15 23:51:10 +0530838 case 200 * MHz:
Kever Yange1980532017-02-13 17:38:56 +0800839 dpll_cfg = (struct pll_div)
840 {.refdiv = 1, .fbdiv = 50, .postdiv1 = 6, .postdiv2 = 1};
841 break;
Jagan Tekibef02a32019-07-15 23:51:10 +0530842 case 300 * MHz:
Kever Yange1980532017-02-13 17:38:56 +0800843 dpll_cfg = (struct pll_div)
844 {.refdiv = 2, .fbdiv = 100, .postdiv1 = 4, .postdiv2 = 1};
845 break;
Jagan Tekif0b06312019-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 Tekibef02a32019-07-15 23:51:10 +0530850 case 666 * MHz:
Kever Yange1980532017-02-13 17:38:56 +0800851 dpll_cfg = (struct pll_div)
852 {.refdiv = 2, .fbdiv = 111, .postdiv1 = 2, .postdiv2 = 1};
853 break;
Jagan Tekibef02a32019-07-15 23:51:10 +0530854 case 800 * MHz:
Kever Yange1980532017-02-13 17:38:56 +0800855 dpll_cfg = (struct pll_div)
856 {.refdiv = 1, .fbdiv = 100, .postdiv1 = 3, .postdiv2 = 1};
857 break;
Jagan Tekibef02a32019-07-15 23:51:10 +0530858 case 933 * MHz:
Kever Yange1980532017-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 Yamada81e10422017-09-16 14:10:41 +0900863 pr_err("Unsupported SDRAM frequency!,%ld\n", set_rate);
Kever Yange1980532017-02-13 17:38:56 +0800864 }
865 rkclk_set_pll(&cru->dpll_con[0], &dpll_cfg);
866
867 return set_rate;
868}
David Wuf91b9b42017-09-20 14:38:58 +0800869
Jagan Teki783acfd2020-01-09 14:22:17 +0530870static ulong rk3399_saradc_get_clk(struct rockchip_cru *cru)
David Wuf91b9b42017-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 Teki783acfd2020-01-09 14:22:17 +0530881static ulong rk3399_saradc_set_clk(struct rockchip_cru *cru, uint hz)
David Wuf91b9b42017-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 Yangca19eac2016-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 Tomsich78a73142017-04-25 09:52:06 +0200903 case HCLK_SDMMC:
Kever Yangca19eac2016-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 Tomsichc31ee922017-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 Muellnere5607a02019-05-07 10:58:44 +0200920 case SCLK_UART1:
Philipp Tomsichc31ee922017-04-20 22:05:49 +0200921 case SCLK_UART2:
Christoph Muellnere5607a02019-05-07 10:58:44 +0200922 case SCLK_UART3:
Philipp Tomsichc31ee922017-04-20 22:05:49 +0200923 return 24000000;
Philipp Tomsich10b594b2017-04-28 18:33:57 +0200924 case PCLK_HDMI_CTRL:
925 break;
Kever Yangca19eac2016-07-29 10:35:25 +0800926 case DCLK_VOP0:
927 case DCLK_VOP1:
928 break;
Philipp Tomsichd10b45e2017-04-28 17:11:55 +0200929 case PCLK_EFUSE1024NS:
930 break;
David Wuf91b9b42017-09-20 14:38:58 +0800931 case SCLK_SARADC:
932 rate = rk3399_saradc_get_clk(priv->cru);
933 break;
Simon Glassd27b3172019-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 Yangca19eac2016-07-29 10:35:25 +0800939 default:
Simon Glassd27b3172019-01-21 14:53:30 -0700940 log_debug("Unknown clock %lu\n", clk->id);
Kever Yangca19eac2016-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 Tomsich2d20a632018-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 Tomsich78a73142017-04-25 09:52:06 +0200973 case HCLK_SDMMC:
Kever Yangca19eac2016-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 Tomsichbfa896c2017-03-24 19:24:25 +0100978 case SCLK_MAC:
Philipp Tomsichf4ba6ed2018-01-08 13:11:01 +0100979 ret = rk3399_gmac_set_clk(priv->cru, rate);
Philipp Tomsichbfa896c2017-03-24 19:24:25 +0100980 break;
Kever Yangca19eac2016-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 Tomsichc31ee922017-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 Tomsich10b594b2017-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 Yangca19eac2016-07-29 10:35:25 +0800996 case DCLK_VOP0:
997 case DCLK_VOP1:
Kever Yange54d26a2016-08-12 17:47:15 +0800998 ret = rk3399_vop_set_clk(priv->cru, clk->id, rate);
Kever Yangca19eac2016-07-29 10:35:25 +0800999 break;
Jagan Teki99f0f822020-04-02 17:11:21 +05301000 case ACLK_VOP1:
1001 case HCLK_VOP1:
Jagan Teki3f26bce2020-04-28 15:30:16 +05301002 case HCLK_SD:
Jagan Teki4fb2c6d2020-05-26 11:32:06 +08001003 case SCLK_UPHY0_TCPDCORE:
1004 case SCLK_UPHY1_TCPDCORE:
Jagan Teki99f0f822020-04-02 17:11:21 +05301005 /**
1006 * assigned-clocks handling won't require for vopl, so
1007 * return 0 to satisfy clk_set_defaults during device probe.
1008 */
1009 return 0;
Kever Yange1980532017-02-13 17:38:56 +08001010 case SCLK_DDRCLK:
1011 ret = rk3399_ddr_set_clk(priv->cru, rate);
1012 break;
Philipp Tomsichd10b45e2017-04-28 17:11:55 +02001013 case PCLK_EFUSE1024NS:
1014 break;
David Wuf91b9b42017-09-20 14:38:58 +08001015 case SCLK_SARADC:
1016 ret = rk3399_saradc_set_clk(priv->cru, rate);
1017 break;
Simon Glassd27b3172019-01-21 14:53:30 -07001018 case ACLK_VIO:
1019 case ACLK_HDCP:
1020 case ACLK_GIC_PRE:
1021 case PCLK_DDR:
1022 return 0;
Kever Yangca19eac2016-07-29 10:35:25 +08001023 default:
Simon Glassd27b3172019-01-21 14:53:30 -07001024 log_debug("Unknown clock %lu\n", clk->id);
Kever Yangca19eac2016-07-29 10:35:25 +08001025 return -ENOENT;
1026 }
1027
1028 return ret;
1029}
1030
Jagan Tekibef02a32019-07-15 23:51:10 +05301031static int __maybe_unused rk3399_gmac_set_parent(struct clk *clk,
1032 struct clk *parent)
Philipp Tomsichf4ba6ed2018-01-08 13:11:01 +01001033{
1034 struct rk3399_clk_priv *priv = dev_get_priv(clk->dev);
1035 const char *clock_output_name;
1036 int ret;
1037
1038 /*
1039 * If the requested parent is in the same clock-controller and
1040 * the id is SCLK_MAC ("clk_gmac"), switch to the internal clock.
1041 */
Jagan Tekibef02a32019-07-15 23:51:10 +05301042 if (parent->dev == clk->dev && parent->id == SCLK_MAC) {
Philipp Tomsichf4ba6ed2018-01-08 13:11:01 +01001043 debug("%s: switching RGMII to SCLK_MAC\n", __func__);
1044 rk_clrreg(&priv->cru->clksel_con[19], BIT(4));
1045 return 0;
1046 }
1047
1048 /*
1049 * Otherwise, we need to check the clock-output-names of the
1050 * requested parent to see if the requested id is "clkin_gmac".
1051 */
1052 ret = dev_read_string_index(parent->dev, "clock-output-names",
1053 parent->id, &clock_output_name);
1054 if (ret < 0)
1055 return -ENODATA;
1056
1057 /* If this is "clkin_gmac", switch to the external clock input */
1058 if (!strcmp(clock_output_name, "clkin_gmac")) {
1059 debug("%s: switching RGMII to CLKIN\n", __func__);
1060 rk_setreg(&priv->cru->clksel_con[19], BIT(4));
1061 return 0;
1062 }
1063
1064 return -EINVAL;
1065}
1066
Jagan Tekibef02a32019-07-15 23:51:10 +05301067static int __maybe_unused rk3399_clk_set_parent(struct clk *clk,
1068 struct clk *parent)
Philipp Tomsichf4ba6ed2018-01-08 13:11:01 +01001069{
1070 switch (clk->id) {
1071 case SCLK_RMII_SRC:
1072 return rk3399_gmac_set_parent(clk, parent);
1073 }
1074
1075 debug("%s: unsupported clk %ld\n", __func__, clk->id);
1076 return -ENOENT;
1077}
1078
Jagan Teki6c7f14a2020-05-09 22:26:19 +05301079static int rk3399_clk_enable(struct clk *clk)
1080{
1081 struct rk3399_clk_priv *priv = dev_get_priv(clk->dev);
1082
1083 switch (clk->id) {
1084 case SCLK_MAC:
1085 rk_clrreg(&priv->cru->clkgate_con[5], BIT(5));
1086 break;
1087 case SCLK_MAC_RX:
1088 rk_clrreg(&priv->cru->clkgate_con[5], BIT(8));
1089 break;
1090 case SCLK_MAC_TX:
1091 rk_clrreg(&priv->cru->clkgate_con[5], BIT(9));
1092 break;
1093 case SCLK_MACREF:
1094 rk_clrreg(&priv->cru->clkgate_con[5], BIT(7));
1095 break;
1096 case SCLK_MACREF_OUT:
1097 rk_clrreg(&priv->cru->clkgate_con[5], BIT(6));
1098 break;
Jagan Tekia5915372020-05-26 11:32:05 +08001099 case SCLK_USB2PHY0_REF:
1100 rk_clrreg(&priv->cru->clkgate_con[6], BIT(5));
1101 break;
1102 case SCLK_USB2PHY1_REF:
1103 rk_clrreg(&priv->cru->clkgate_con[6], BIT(6));
1104 break;
Jagan Teki6c7f14a2020-05-09 22:26:19 +05301105 case ACLK_GMAC:
1106 rk_clrreg(&priv->cru->clkgate_con[32], BIT(0));
1107 break;
1108 case PCLK_GMAC:
1109 rk_clrreg(&priv->cru->clkgate_con[32], BIT(2));
1110 break;
1111 case SCLK_USB3OTG0_REF:
1112 rk_clrreg(&priv->cru->clkgate_con[12], BIT(1));
1113 break;
1114 case SCLK_USB3OTG1_REF:
1115 rk_clrreg(&priv->cru->clkgate_con[12], BIT(2));
1116 break;
1117 case SCLK_USB3OTG0_SUSPEND:
1118 rk_clrreg(&priv->cru->clkgate_con[12], BIT(3));
1119 break;
1120 case SCLK_USB3OTG1_SUSPEND:
1121 rk_clrreg(&priv->cru->clkgate_con[12], BIT(4));
1122 break;
1123 case ACLK_USB3OTG0:
1124 rk_clrreg(&priv->cru->clkgate_con[30], BIT(1));
1125 break;
1126 case ACLK_USB3OTG1:
1127 rk_clrreg(&priv->cru->clkgate_con[30], BIT(2));
1128 break;
1129 case ACLK_USB3_RKSOC_AXI_PERF:
1130 rk_clrreg(&priv->cru->clkgate_con[30], BIT(3));
1131 break;
1132 case ACLK_USB3:
1133 rk_clrreg(&priv->cru->clkgate_con[12], BIT(0));
1134 break;
1135 case ACLK_USB3_GRF:
1136 rk_clrreg(&priv->cru->clkgate_con[30], BIT(4));
1137 break;
1138 case HCLK_HOST0:
1139 rk_clrreg(&priv->cru->clksel_con[20], BIT(5));
1140 break;
1141 case HCLK_HOST0_ARB:
1142 rk_clrreg(&priv->cru->clksel_con[20], BIT(6));
1143 break;
1144 case HCLK_HOST1:
1145 rk_clrreg(&priv->cru->clksel_con[20], BIT(7));
1146 break;
1147 case HCLK_HOST1_ARB:
1148 rk_clrreg(&priv->cru->clksel_con[20], BIT(8));
1149 break;
Jagan Teki055c2182020-05-26 11:32:07 +08001150 case SCLK_UPHY0_TCPDPHY_REF:
1151 rk_clrreg(&priv->cru->clkgate_con[13], BIT(4));
1152 break;
1153 case SCLK_UPHY0_TCPDCORE:
1154 rk_clrreg(&priv->cru->clkgate_con[13], BIT(5));
1155 break;
1156 case SCLK_UPHY1_TCPDPHY_REF:
1157 rk_clrreg(&priv->cru->clkgate_con[13], BIT(6));
1158 break;
1159 case SCLK_UPHY1_TCPDCORE:
1160 rk_clrreg(&priv->cru->clkgate_con[13], BIT(7));
1161 break;
Jagan Teki70c54ee2020-05-09 22:26:20 +05301162 case SCLK_PCIEPHY_REF:
1163 rk_clrreg(&priv->cru->clksel_con[18], BIT(10));
1164 break;
Jagan Teki6c7f14a2020-05-09 22:26:19 +05301165 default:
1166 debug("%s: unsupported clk %ld\n", __func__, clk->id);
1167 return -ENOENT;
1168 }
1169
1170 return 0;
1171}
1172
1173static int rk3399_clk_disable(struct clk *clk)
1174{
1175 struct rk3399_clk_priv *priv = dev_get_priv(clk->dev);
1176
1177 switch (clk->id) {
1178 case SCLK_MAC:
1179 rk_setreg(&priv->cru->clkgate_con[5], BIT(5));
1180 break;
1181 case SCLK_MAC_RX:
1182 rk_setreg(&priv->cru->clkgate_con[5], BIT(8));
1183 break;
1184 case SCLK_MAC_TX:
1185 rk_setreg(&priv->cru->clkgate_con[5], BIT(9));
1186 break;
1187 case SCLK_MACREF:
1188 rk_setreg(&priv->cru->clkgate_con[5], BIT(7));
1189 break;
1190 case SCLK_MACREF_OUT:
1191 rk_setreg(&priv->cru->clkgate_con[5], BIT(6));
1192 break;
Jagan Tekia5915372020-05-26 11:32:05 +08001193 case SCLK_USB2PHY0_REF:
1194 rk_setreg(&priv->cru->clkgate_con[6], BIT(5));
1195 break;
1196 case SCLK_USB2PHY1_REF:
1197 rk_setreg(&priv->cru->clkgate_con[6], BIT(6));
1198 break;
Jagan Teki6c7f14a2020-05-09 22:26:19 +05301199 case ACLK_GMAC:
1200 rk_setreg(&priv->cru->clkgate_con[32], BIT(0));
1201 break;
1202 case PCLK_GMAC:
1203 rk_setreg(&priv->cru->clkgate_con[32], BIT(2));
1204 break;
1205 case SCLK_USB3OTG0_REF:
1206 rk_setreg(&priv->cru->clkgate_con[12], BIT(1));
1207 break;
1208 case SCLK_USB3OTG1_REF:
1209 rk_setreg(&priv->cru->clkgate_con[12], BIT(2));
1210 break;
1211 case SCLK_USB3OTG0_SUSPEND:
1212 rk_setreg(&priv->cru->clkgate_con[12], BIT(3));
1213 break;
1214 case SCLK_USB3OTG1_SUSPEND:
1215 rk_setreg(&priv->cru->clkgate_con[12], BIT(4));
1216 break;
1217 case ACLK_USB3OTG0:
1218 rk_setreg(&priv->cru->clkgate_con[30], BIT(1));
1219 break;
1220 case ACLK_USB3OTG1:
1221 rk_setreg(&priv->cru->clkgate_con[30], BIT(2));
1222 break;
1223 case ACLK_USB3_RKSOC_AXI_PERF:
1224 rk_setreg(&priv->cru->clkgate_con[30], BIT(3));
1225 break;
1226 case ACLK_USB3:
1227 rk_setreg(&priv->cru->clkgate_con[12], BIT(0));
1228 break;
1229 case ACLK_USB3_GRF:
1230 rk_setreg(&priv->cru->clkgate_con[30], BIT(4));
1231 break;
1232 case HCLK_HOST0:
1233 rk_setreg(&priv->cru->clksel_con[20], BIT(5));
1234 break;
1235 case HCLK_HOST0_ARB:
1236 rk_setreg(&priv->cru->clksel_con[20], BIT(6));
1237 break;
1238 case HCLK_HOST1:
1239 rk_setreg(&priv->cru->clksel_con[20], BIT(7));
1240 break;
1241 case HCLK_HOST1_ARB:
1242 rk_setreg(&priv->cru->clksel_con[20], BIT(8));
1243 break;
Jagan Teki055c2182020-05-26 11:32:07 +08001244 case SCLK_UPHY0_TCPDPHY_REF:
1245 rk_setreg(&priv->cru->clkgate_con[13], BIT(4));
1246 break;
1247 case SCLK_UPHY0_TCPDCORE:
1248 rk_setreg(&priv->cru->clkgate_con[13], BIT(5));
1249 break;
1250 case SCLK_UPHY1_TCPDPHY_REF:
1251 rk_setreg(&priv->cru->clkgate_con[13], BIT(6));
1252 break;
1253 case SCLK_UPHY1_TCPDCORE:
1254 rk_setreg(&priv->cru->clkgate_con[13], BIT(7));
1255 break;
Jagan Teki70c54ee2020-05-09 22:26:20 +05301256 case SCLK_PCIEPHY_REF:
1257 rk_clrreg(&priv->cru->clksel_con[18], BIT(10));
1258 break;
Jagan Teki6c7f14a2020-05-09 22:26:19 +05301259 default:
1260 debug("%s: unsupported clk %ld\n", __func__, clk->id);
1261 return -ENOENT;
1262 }
1263
1264 return 0;
1265}
1266
Kever Yangca19eac2016-07-29 10:35:25 +08001267static struct clk_ops rk3399_clk_ops = {
1268 .get_rate = rk3399_clk_get_rate,
1269 .set_rate = rk3399_clk_set_rate,
Philipp Tomsich6dd2fb42018-01-25 15:27:10 +01001270#if CONFIG_IS_ENABLED(OF_CONTROL) && !CONFIG_IS_ENABLED(OF_PLATDATA)
Philipp Tomsichf4ba6ed2018-01-08 13:11:01 +01001271 .set_parent = rk3399_clk_set_parent,
Philipp Tomsich6dd2fb42018-01-25 15:27:10 +01001272#endif
Jagan Teki6c7f14a2020-05-09 22:26:19 +05301273 .enable = rk3399_clk_enable,
1274 .disable = rk3399_clk_disable,
Kever Yangca19eac2016-07-29 10:35:25 +08001275};
1276
Kever Yang05a14b02017-10-12 15:27:29 +08001277#ifdef CONFIG_SPL_BUILD
Jagan Teki783acfd2020-01-09 14:22:17 +05301278static void rkclk_init(struct rockchip_cru *cru)
Kever Yang05a14b02017-10-12 15:27:29 +08001279{
1280 u32 aclk_div;
1281 u32 hclk_div;
1282 u32 pclk_div;
1283
Christoph Muellner25c7ba92018-11-30 20:32:48 +01001284 rk3399_configure_cpu_l(cru, APLL_L_600_MHZ);
1285 rk3399_configure_cpu_b(cru, APLL_B_600_MHZ);
Kever Yang05a14b02017-10-12 15:27:29 +08001286 /*
1287 * some cru registers changed by bootrom, we'd better reset them to
1288 * reset/default values described in TRM to avoid confusion in kernel.
1289 * Please consider these three lines as a fix of bootrom bug.
1290 */
1291 rk_clrsetreg(&cru->clksel_con[12], 0xffff, 0x4101);
1292 rk_clrsetreg(&cru->clksel_con[19], 0xffff, 0x033f);
1293 rk_clrsetreg(&cru->clksel_con[56], 0x0003, 0x0003);
1294
1295 /* configure gpll cpll */
1296 rkclk_set_pll(&cru->gpll_con[0], &gpll_init_cfg);
1297 rkclk_set_pll(&cru->cpll_con[0], &cpll_init_cfg);
1298
1299 /* configure perihp aclk, hclk, pclk */
1300 aclk_div = GPLL_HZ / PERIHP_ACLK_HZ - 1;
1301 assert((aclk_div + 1) * PERIHP_ACLK_HZ == GPLL_HZ && aclk_div < 0x1f);
1302
1303 hclk_div = PERIHP_ACLK_HZ / PERIHP_HCLK_HZ - 1;
1304 assert((hclk_div + 1) * PERIHP_HCLK_HZ ==
1305 PERIHP_ACLK_HZ && (hclk_div < 0x4));
1306
1307 pclk_div = PERIHP_ACLK_HZ / PERIHP_PCLK_HZ - 1;
1308 assert((pclk_div + 1) * PERIHP_PCLK_HZ ==
1309 PERIHP_ACLK_HZ && (pclk_div < 0x7));
1310
1311 rk_clrsetreg(&cru->clksel_con[14],
1312 PCLK_PERIHP_DIV_CON_MASK | HCLK_PERIHP_DIV_CON_MASK |
1313 ACLK_PERIHP_PLL_SEL_MASK | ACLK_PERIHP_DIV_CON_MASK,
1314 pclk_div << PCLK_PERIHP_DIV_CON_SHIFT |
1315 hclk_div << HCLK_PERIHP_DIV_CON_SHIFT |
1316 ACLK_PERIHP_PLL_SEL_GPLL << ACLK_PERIHP_PLL_SEL_SHIFT |
1317 aclk_div << ACLK_PERIHP_DIV_CON_SHIFT);
1318
1319 /* configure perilp0 aclk, hclk, pclk */
1320 aclk_div = GPLL_HZ / PERILP0_ACLK_HZ - 1;
1321 assert((aclk_div + 1) * PERILP0_ACLK_HZ == GPLL_HZ && aclk_div < 0x1f);
1322
1323 hclk_div = PERILP0_ACLK_HZ / PERILP0_HCLK_HZ - 1;
1324 assert((hclk_div + 1) * PERILP0_HCLK_HZ ==
1325 PERILP0_ACLK_HZ && (hclk_div < 0x4));
1326
1327 pclk_div = PERILP0_ACLK_HZ / PERILP0_PCLK_HZ - 1;
1328 assert((pclk_div + 1) * PERILP0_PCLK_HZ ==
1329 PERILP0_ACLK_HZ && (pclk_div < 0x7));
1330
1331 rk_clrsetreg(&cru->clksel_con[23],
1332 PCLK_PERILP0_DIV_CON_MASK | HCLK_PERILP0_DIV_CON_MASK |
1333 ACLK_PERILP0_PLL_SEL_MASK | ACLK_PERILP0_DIV_CON_MASK,
1334 pclk_div << PCLK_PERILP0_DIV_CON_SHIFT |
1335 hclk_div << HCLK_PERILP0_DIV_CON_SHIFT |
1336 ACLK_PERILP0_PLL_SEL_GPLL << ACLK_PERILP0_PLL_SEL_SHIFT |
1337 aclk_div << ACLK_PERILP0_DIV_CON_SHIFT);
1338
1339 /* perilp1 hclk select gpll as source */
1340 hclk_div = GPLL_HZ / PERILP1_HCLK_HZ - 1;
1341 assert((hclk_div + 1) * PERILP1_HCLK_HZ ==
1342 GPLL_HZ && (hclk_div < 0x1f));
1343
1344 pclk_div = PERILP1_HCLK_HZ / PERILP1_HCLK_HZ - 1;
1345 assert((pclk_div + 1) * PERILP1_HCLK_HZ ==
1346 PERILP1_HCLK_HZ && (hclk_div < 0x7));
1347
1348 rk_clrsetreg(&cru->clksel_con[25],
1349 PCLK_PERILP1_DIV_CON_MASK | HCLK_PERILP1_DIV_CON_MASK |
1350 HCLK_PERILP1_PLL_SEL_MASK,
1351 pclk_div << PCLK_PERILP1_DIV_CON_SHIFT |
1352 hclk_div << HCLK_PERILP1_DIV_CON_SHIFT |
1353 HCLK_PERILP1_PLL_SEL_GPLL << HCLK_PERILP1_PLL_SEL_SHIFT);
1354}
1355#endif
1356
Kever Yangca19eac2016-07-29 10:35:25 +08001357static int rk3399_clk_probe(struct udevice *dev)
1358{
Kever Yange1980532017-02-13 17:38:56 +08001359#ifdef CONFIG_SPL_BUILD
Kever Yangca19eac2016-07-29 10:35:25 +08001360 struct rk3399_clk_priv *priv = dev_get_priv(dev);
1361
Kever Yange1980532017-02-13 17:38:56 +08001362#if CONFIG_IS_ENABLED(OF_PLATDATA)
1363 struct rk3399_clk_plat *plat = dev_get_platdata(dev);
Kever Yangca19eac2016-07-29 10:35:25 +08001364
Simon Glass1b1fe412017-08-29 14:15:50 -06001365 priv->cru = map_sysmem(plat->dtd.reg[0], plat->dtd.reg[1]);
Kever Yange1980532017-02-13 17:38:56 +08001366#endif
1367 rkclk_init(priv->cru);
1368#endif
Kever Yangca19eac2016-07-29 10:35:25 +08001369 return 0;
1370}
1371
1372static int rk3399_clk_ofdata_to_platdata(struct udevice *dev)
1373{
Kever Yange1980532017-02-13 17:38:56 +08001374#if !CONFIG_IS_ENABLED(OF_PLATDATA)
Kever Yangca19eac2016-07-29 10:35:25 +08001375 struct rk3399_clk_priv *priv = dev_get_priv(dev);
1376
Philipp Tomsich44d76842017-09-12 17:32:24 +02001377 priv->cru = dev_read_addr_ptr(dev);
Kever Yange1980532017-02-13 17:38:56 +08001378#endif
Kever Yangca19eac2016-07-29 10:35:25 +08001379 return 0;
1380}
1381
1382static int rk3399_clk_bind(struct udevice *dev)
1383{
1384 int ret;
Kever Yang4fbb6c22017-11-03 15:16:13 +08001385 struct udevice *sys_child;
1386 struct sysreset_reg *priv;
Kever Yangca19eac2016-07-29 10:35:25 +08001387
1388 /* The reset driver does not have a device node, so bind it here */
Kever Yang4fbb6c22017-11-03 15:16:13 +08001389 ret = device_bind_driver(dev, "rockchip_sysreset", "sysreset",
1390 &sys_child);
1391 if (ret) {
1392 debug("Warning: No sysreset driver: ret=%d\n", ret);
1393 } else {
1394 priv = malloc(sizeof(struct sysreset_reg));
Jagan Teki783acfd2020-01-09 14:22:17 +05301395 priv->glb_srst_fst_value = offsetof(struct rockchip_cru,
Kever Yang4fbb6c22017-11-03 15:16:13 +08001396 glb_srst_fst_value);
Jagan Teki783acfd2020-01-09 14:22:17 +05301397 priv->glb_srst_snd_value = offsetof(struct rockchip_cru,
Kever Yang4fbb6c22017-11-03 15:16:13 +08001398 glb_srst_snd_value);
1399 sys_child->priv = priv;
1400 }
Kever Yangca19eac2016-07-29 10:35:25 +08001401
Heiko Stuebner416f8d32019-11-09 00:06:30 +01001402#if CONFIG_IS_ENABLED(RESET_ROCKCHIP)
Jagan Teki783acfd2020-01-09 14:22:17 +05301403 ret = offsetof(struct rockchip_cru, softrst_con[0]);
Elaine Zhang432976f2017-12-19 18:22:38 +08001404 ret = rockchip_reset_bind(dev, ret, 21);
1405 if (ret)
1406 debug("Warning: software reset driver bind faile\n");
1407#endif
1408
Kever Yangca19eac2016-07-29 10:35:25 +08001409 return 0;
1410}
1411
1412static const struct udevice_id rk3399_clk_ids[] = {
1413 { .compatible = "rockchip,rk3399-cru" },
1414 { }
1415};
1416
1417U_BOOT_DRIVER(clk_rk3399) = {
Kever Yange1980532017-02-13 17:38:56 +08001418 .name = "rockchip_rk3399_cru",
Kever Yangca19eac2016-07-29 10:35:25 +08001419 .id = UCLASS_CLK,
1420 .of_match = rk3399_clk_ids,
1421 .priv_auto_alloc_size = sizeof(struct rk3399_clk_priv),
1422 .ofdata_to_platdata = rk3399_clk_ofdata_to_platdata,
1423 .ops = &rk3399_clk_ops,
1424 .bind = rk3399_clk_bind,
1425 .probe = rk3399_clk_probe,
Kever Yange1980532017-02-13 17:38:56 +08001426#if CONFIG_IS_ENABLED(OF_PLATDATA)
1427 .platdata_auto_alloc_size = sizeof(struct rk3399_clk_plat),
1428#endif
Kever Yangca19eac2016-07-29 10:35:25 +08001429};
Kever Yange54d26a2016-08-12 17:47:15 +08001430
1431static ulong rk3399_i2c_get_pmuclk(struct rk3399_pmucru *pmucru, ulong clk_id)
1432{
1433 u32 div, con;
1434
1435 switch (clk_id) {
1436 case SCLK_I2C0_PMU:
1437 con = readl(&pmucru->pmucru_clksel[2]);
1438 div = I2C_CLK_DIV_VALUE(con, 0);
1439 break;
1440 case SCLK_I2C4_PMU:
1441 con = readl(&pmucru->pmucru_clksel[3]);
1442 div = I2C_CLK_DIV_VALUE(con, 4);
1443 break;
1444 case SCLK_I2C8_PMU:
1445 con = readl(&pmucru->pmucru_clksel[2]);
1446 div = I2C_CLK_DIV_VALUE(con, 8);
1447 break;
1448 default:
1449 printf("do not support this i2c bus\n");
1450 return -EINVAL;
1451 }
1452
1453 return DIV_TO_RATE(PPLL_HZ, div);
1454}
1455
1456static ulong rk3399_i2c_set_pmuclk(struct rk3399_pmucru *pmucru, ulong clk_id,
1457 uint hz)
1458{
1459 int src_clk_div;
1460
1461 src_clk_div = PPLL_HZ / hz;
1462 assert(src_clk_div - 1 < 127);
1463
1464 switch (clk_id) {
1465 case SCLK_I2C0_PMU:
1466 rk_clrsetreg(&pmucru->pmucru_clksel[2], I2C_PMUCLK_REG_MASK(0),
1467 I2C_PMUCLK_REG_VALUE(0, src_clk_div));
1468 break;
1469 case SCLK_I2C4_PMU:
1470 rk_clrsetreg(&pmucru->pmucru_clksel[3], I2C_PMUCLK_REG_MASK(4),
1471 I2C_PMUCLK_REG_VALUE(4, src_clk_div));
1472 break;
1473 case SCLK_I2C8_PMU:
1474 rk_clrsetreg(&pmucru->pmucru_clksel[2], I2C_PMUCLK_REG_MASK(8),
1475 I2C_PMUCLK_REG_VALUE(8, src_clk_div));
1476 break;
1477 default:
1478 printf("do not support this i2c bus\n");
1479 return -EINVAL;
1480 }
1481
1482 return DIV_TO_RATE(PPLL_HZ, src_clk_div);
1483}
1484
1485static ulong rk3399_pwm_get_clk(struct rk3399_pmucru *pmucru)
1486{
1487 u32 div, con;
1488
1489 /* PWM closk rate is same as pclk_pmu */
1490 con = readl(&pmucru->pmucru_clksel[0]);
1491 div = con & PMU_PCLK_DIV_CON_MASK;
1492
1493 return DIV_TO_RATE(PPLL_HZ, div);
1494}
1495
1496static ulong rk3399_pmuclk_get_rate(struct clk *clk)
1497{
1498 struct rk3399_pmuclk_priv *priv = dev_get_priv(clk->dev);
1499 ulong rate = 0;
1500
1501 switch (clk->id) {
Philipp Tomsich932908c2018-02-23 17:36:41 +01001502 case PLL_PPLL:
1503 return PPLL_HZ;
Kever Yange54d26a2016-08-12 17:47:15 +08001504 case PCLK_RKPWM_PMU:
1505 rate = rk3399_pwm_get_clk(priv->pmucru);
1506 break;
1507 case SCLK_I2C0_PMU:
1508 case SCLK_I2C4_PMU:
1509 case SCLK_I2C8_PMU:
1510 rate = rk3399_i2c_get_pmuclk(priv->pmucru, clk->id);
1511 break;
1512 default:
1513 return -ENOENT;
1514 }
1515
1516 return rate;
1517}
1518
1519static ulong rk3399_pmuclk_set_rate(struct clk *clk, ulong rate)
1520{
1521 struct rk3399_pmuclk_priv *priv = dev_get_priv(clk->dev);
1522 ulong ret = 0;
1523
1524 switch (clk->id) {
Philipp Tomsich932908c2018-02-23 17:36:41 +01001525 case PLL_PPLL:
1526 /*
1527 * This has already been set up and we don't want/need
1528 * to change it here. Accept the request though, as the
1529 * device-tree has this in an 'assigned-clocks' list.
1530 */
1531 return PPLL_HZ;
Kever Yange54d26a2016-08-12 17:47:15 +08001532 case SCLK_I2C0_PMU:
1533 case SCLK_I2C4_PMU:
1534 case SCLK_I2C8_PMU:
1535 ret = rk3399_i2c_set_pmuclk(priv->pmucru, clk->id, rate);
1536 break;
1537 default:
1538 return -ENOENT;
1539 }
1540
1541 return ret;
1542}
1543
1544static struct clk_ops rk3399_pmuclk_ops = {
1545 .get_rate = rk3399_pmuclk_get_rate,
1546 .set_rate = rk3399_pmuclk_set_rate,
1547};
1548
Kever Yange1980532017-02-13 17:38:56 +08001549#ifndef CONFIG_SPL_BUILD
Kever Yange54d26a2016-08-12 17:47:15 +08001550static void pmuclk_init(struct rk3399_pmucru *pmucru)
1551{
1552 u32 pclk_div;
1553
1554 /* configure pmu pll(ppll) */
1555 rkclk_set_pll(&pmucru->ppll_con[0], &ppll_init_cfg);
1556
1557 /* configure pmu pclk */
1558 pclk_div = PPLL_HZ / PMU_PCLK_HZ - 1;
Kever Yange54d26a2016-08-12 17:47:15 +08001559 rk_clrsetreg(&pmucru->pmucru_clksel[0],
1560 PMU_PCLK_DIV_CON_MASK,
1561 pclk_div << PMU_PCLK_DIV_CON_SHIFT);
1562}
Kever Yange1980532017-02-13 17:38:56 +08001563#endif
Kever Yange54d26a2016-08-12 17:47:15 +08001564
1565static int rk3399_pmuclk_probe(struct udevice *dev)
1566{
Philipp Tomsichcf0a4ba2017-03-24 19:24:24 +01001567#if CONFIG_IS_ENABLED(OF_PLATDATA) || !defined(CONFIG_SPL_BUILD)
Kever Yange54d26a2016-08-12 17:47:15 +08001568 struct rk3399_pmuclk_priv *priv = dev_get_priv(dev);
Philipp Tomsichcf0a4ba2017-03-24 19:24:24 +01001569#endif
Kever Yange54d26a2016-08-12 17:47:15 +08001570
Kever Yange1980532017-02-13 17:38:56 +08001571#if CONFIG_IS_ENABLED(OF_PLATDATA)
1572 struct rk3399_pmuclk_plat *plat = dev_get_platdata(dev);
Kever Yange54d26a2016-08-12 17:47:15 +08001573
Simon Glass1b1fe412017-08-29 14:15:50 -06001574 priv->pmucru = map_sysmem(plat->dtd.reg[0], plat->dtd.reg[1]);
Kever Yange1980532017-02-13 17:38:56 +08001575#endif
1576
1577#ifndef CONFIG_SPL_BUILD
1578 pmuclk_init(priv->pmucru);
1579#endif
Kever Yange54d26a2016-08-12 17:47:15 +08001580 return 0;
1581}
1582
1583static int rk3399_pmuclk_ofdata_to_platdata(struct udevice *dev)
1584{
Kever Yange1980532017-02-13 17:38:56 +08001585#if !CONFIG_IS_ENABLED(OF_PLATDATA)
Kever Yange54d26a2016-08-12 17:47:15 +08001586 struct rk3399_pmuclk_priv *priv = dev_get_priv(dev);
1587
Philipp Tomsich44d76842017-09-12 17:32:24 +02001588 priv->pmucru = dev_read_addr_ptr(dev);
Kever Yange1980532017-02-13 17:38:56 +08001589#endif
Kever Yange54d26a2016-08-12 17:47:15 +08001590 return 0;
1591}
1592
Elaine Zhang432976f2017-12-19 18:22:38 +08001593static int rk3399_pmuclk_bind(struct udevice *dev)
1594{
Alper Nebi Yasak5de2f332020-10-05 09:57:29 +03001595#if CONFIG_IS_ENABLED(RESET_ROCKCHIP)
Elaine Zhang432976f2017-12-19 18:22:38 +08001596 int ret;
1597
1598 ret = offsetof(struct rk3399_pmucru, pmucru_softrst_con[0]);
1599 ret = rockchip_reset_bind(dev, ret, 2);
1600 if (ret)
1601 debug("Warning: software reset driver bind faile\n");
1602#endif
1603 return 0;
1604}
1605
Kever Yange54d26a2016-08-12 17:47:15 +08001606static const struct udevice_id rk3399_pmuclk_ids[] = {
1607 { .compatible = "rockchip,rk3399-pmucru" },
1608 { }
1609};
1610
Simon Glassd1dfea72016-10-01 20:04:51 -06001611U_BOOT_DRIVER(rockchip_rk3399_pmuclk) = {
Kever Yange1980532017-02-13 17:38:56 +08001612 .name = "rockchip_rk3399_pmucru",
Kever Yange54d26a2016-08-12 17:47:15 +08001613 .id = UCLASS_CLK,
1614 .of_match = rk3399_pmuclk_ids,
1615 .priv_auto_alloc_size = sizeof(struct rk3399_pmuclk_priv),
1616 .ofdata_to_platdata = rk3399_pmuclk_ofdata_to_platdata,
1617 .ops = &rk3399_pmuclk_ops,
1618 .probe = rk3399_pmuclk_probe,
Elaine Zhang432976f2017-12-19 18:22:38 +08001619 .bind = rk3399_pmuclk_bind,
Kever Yange1980532017-02-13 17:38:56 +08001620#if CONFIG_IS_ENABLED(OF_PLATDATA)
1621 .platdata_auto_alloc_size = sizeof(struct rk3399_pmuclk_plat),
1622#endif
Kever Yange54d26a2016-08-12 17:47:15 +08001623};