blob: c37e8a53a26899a833551d0ea563fbcfa8a85962 [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>
Simon Glass3ba929a2020-10-30 21:38:53 -060021#include <asm/global_data.h>
Simon Glass95588622020-12-22 19:30:28 -070022#include <dm/device-internal.h>
Kever Yangca19eac2016-07-29 10:35:25 +080023#include <dm/lists.h>
24#include <dt-bindings/clock/rk3399-cru.h>
Simon Glass4dcacfc2020-05-10 11:40:13 -060025#include <linux/bitops.h>
Simon Glassdbd79542020-05-10 11:40:11 -060026#include <linux/delay.h>
Simon Glassbdd5f812023-09-14 18:21:46 -060027#include <linux/printk.h>
Kever Yangca19eac2016-07-29 10:35:25 +080028
Alper Nebi Yasak66327ce2020-10-28 00:15:10 +030029DECLARE_GLOBAL_DATA_PTR;
30
Kever Yange1980532017-02-13 17:38:56 +080031#if CONFIG_IS_ENABLED(OF_PLATDATA)
32struct rk3399_clk_plat {
33 struct dtd_rockchip_rk3399_cru dtd;
Kever Yange54d26a2016-08-12 17:47:15 +080034};
35
Kever Yange1980532017-02-13 17:38:56 +080036struct rk3399_pmuclk_plat {
37 struct dtd_rockchip_rk3399_pmucru dtd;
38};
39#endif
40
Kever Yangca19eac2016-07-29 10:35:25 +080041struct pll_div {
42 u32 refdiv;
43 u32 fbdiv;
44 u32 postdiv1;
45 u32 postdiv2;
46 u32 frac;
47};
48
49#define RATE_TO_DIV(input_rate, output_rate) \
Jagan Tekibef02a32019-07-15 23:51:10 +053050 ((input_rate) / (output_rate) - 1)
51#define DIV_TO_RATE(input_rate, div) ((input_rate) / ((div) + 1))
Kever Yangca19eac2016-07-29 10:35:25 +080052
53#define PLL_DIVISORS(hz, _refdiv, _postdiv1, _postdiv2) {\
54 .refdiv = _refdiv,\
55 .fbdiv = (u32)((u64)hz * _refdiv * _postdiv1 * _postdiv2 / OSC_HZ),\
56 .postdiv1 = _postdiv1, .postdiv2 = _postdiv2};
57
58static const struct pll_div gpll_init_cfg = PLL_DIVISORS(GPLL_HZ, 2, 2, 1);
59static const struct pll_div cpll_init_cfg = PLL_DIVISORS(CPLL_HZ, 1, 2, 2);
Alper Nebi Yasak66327ce2020-10-28 00:15:10 +030060#if !defined(CONFIG_SPL_BUILD)
Kever Yangca19eac2016-07-29 10:35:25 +080061static const struct pll_div ppll_init_cfg = PLL_DIVISORS(PPLL_HZ, 2, 2, 1);
Philipp Tomsichcf0a4ba2017-03-24 19:24:24 +010062#endif
Kever Yangca19eac2016-07-29 10:35:25 +080063
Jagan Tekibef02a32019-07-15 23:51:10 +053064static const struct pll_div apll_l_1600_cfg = PLL_DIVISORS(1600 * MHz, 3, 1, 1);
65static const struct pll_div apll_l_600_cfg = PLL_DIVISORS(600 * MHz, 1, 2, 1);
Kever Yangca19eac2016-07-29 10:35:25 +080066
67static const struct pll_div *apll_l_cfgs[] = {
68 [APLL_L_1600_MHZ] = &apll_l_1600_cfg,
69 [APLL_L_600_MHZ] = &apll_l_600_cfg,
70};
71
Jagan Tekibef02a32019-07-15 23:51:10 +053072static const struct pll_div apll_b_600_cfg = PLL_DIVISORS(600 * MHz, 1, 2, 1);
Christoph Muellner25c7ba92018-11-30 20:32:48 +010073static const struct pll_div *apll_b_cfgs[] = {
74 [APLL_B_600_MHZ] = &apll_b_600_cfg,
75};
76
Kever Yangca19eac2016-07-29 10:35:25 +080077enum {
78 /* PLL_CON0 */
79 PLL_FBDIV_MASK = 0xfff,
80 PLL_FBDIV_SHIFT = 0,
81
82 /* PLL_CON1 */
83 PLL_POSTDIV2_SHIFT = 12,
84 PLL_POSTDIV2_MASK = 0x7 << PLL_POSTDIV2_SHIFT,
85 PLL_POSTDIV1_SHIFT = 8,
86 PLL_POSTDIV1_MASK = 0x7 << PLL_POSTDIV1_SHIFT,
87 PLL_REFDIV_MASK = 0x3f,
88 PLL_REFDIV_SHIFT = 0,
89
90 /* PLL_CON2 */
91 PLL_LOCK_STATUS_SHIFT = 31,
92 PLL_LOCK_STATUS_MASK = 1 << PLL_LOCK_STATUS_SHIFT,
93 PLL_FRACDIV_MASK = 0xffffff,
94 PLL_FRACDIV_SHIFT = 0,
95
96 /* PLL_CON3 */
97 PLL_MODE_SHIFT = 8,
98 PLL_MODE_MASK = 3 << PLL_MODE_SHIFT,
99 PLL_MODE_SLOW = 0,
100 PLL_MODE_NORM,
101 PLL_MODE_DEEP,
102 PLL_DSMPD_SHIFT = 3,
103 PLL_DSMPD_MASK = 1 << PLL_DSMPD_SHIFT,
104 PLL_INTEGER_MODE = 1,
105
106 /* PMUCRU_CLKSEL_CON0 */
107 PMU_PCLK_DIV_CON_MASK = 0x1f,
108 PMU_PCLK_DIV_CON_SHIFT = 0,
109
110 /* PMUCRU_CLKSEL_CON1 */
111 SPI3_PLL_SEL_SHIFT = 7,
112 SPI3_PLL_SEL_MASK = 1 << SPI3_PLL_SEL_SHIFT,
113 SPI3_PLL_SEL_24M = 0,
114 SPI3_PLL_SEL_PPLL = 1,
115 SPI3_DIV_CON_SHIFT = 0x0,
116 SPI3_DIV_CON_MASK = 0x7f,
117
118 /* PMUCRU_CLKSEL_CON2 */
119 I2C_DIV_CON_MASK = 0x7f,
Kever Yange54d26a2016-08-12 17:47:15 +0800120 CLK_I2C8_DIV_CON_SHIFT = 8,
121 CLK_I2C0_DIV_CON_SHIFT = 0,
Kever Yangca19eac2016-07-29 10:35:25 +0800122
123 /* PMUCRU_CLKSEL_CON3 */
Kever Yange54d26a2016-08-12 17:47:15 +0800124 CLK_I2C4_DIV_CON_SHIFT = 0,
Kever Yangca19eac2016-07-29 10:35:25 +0800125
126 /* CLKSEL_CON0 */
127 ACLKM_CORE_L_DIV_CON_SHIFT = 8,
128 ACLKM_CORE_L_DIV_CON_MASK = 0x1f << ACLKM_CORE_L_DIV_CON_SHIFT,
129 CLK_CORE_L_PLL_SEL_SHIFT = 6,
130 CLK_CORE_L_PLL_SEL_MASK = 3 << CLK_CORE_L_PLL_SEL_SHIFT,
131 CLK_CORE_L_PLL_SEL_ALPLL = 0x0,
132 CLK_CORE_L_PLL_SEL_ABPLL = 0x1,
133 CLK_CORE_L_PLL_SEL_DPLL = 0x10,
134 CLK_CORE_L_PLL_SEL_GPLL = 0x11,
135 CLK_CORE_L_DIV_MASK = 0x1f,
136 CLK_CORE_L_DIV_SHIFT = 0,
137
138 /* CLKSEL_CON1 */
139 PCLK_DBG_L_DIV_SHIFT = 0x8,
140 PCLK_DBG_L_DIV_MASK = 0x1f << PCLK_DBG_L_DIV_SHIFT,
141 ATCLK_CORE_L_DIV_SHIFT = 0,
142 ATCLK_CORE_L_DIV_MASK = 0x1f << ATCLK_CORE_L_DIV_SHIFT,
143
Christoph Muellner25c7ba92018-11-30 20:32:48 +0100144 /* CLKSEL_CON2 */
145 ACLKM_CORE_B_DIV_CON_SHIFT = 8,
146 ACLKM_CORE_B_DIV_CON_MASK = 0x1f << ACLKM_CORE_B_DIV_CON_SHIFT,
147 CLK_CORE_B_PLL_SEL_SHIFT = 6,
148 CLK_CORE_B_PLL_SEL_MASK = 3 << CLK_CORE_B_PLL_SEL_SHIFT,
149 CLK_CORE_B_PLL_SEL_ALPLL = 0x0,
150 CLK_CORE_B_PLL_SEL_ABPLL = 0x1,
151 CLK_CORE_B_PLL_SEL_DPLL = 0x10,
152 CLK_CORE_B_PLL_SEL_GPLL = 0x11,
153 CLK_CORE_B_DIV_MASK = 0x1f,
154 CLK_CORE_B_DIV_SHIFT = 0,
155
156 /* CLKSEL_CON3 */
157 PCLK_DBG_B_DIV_SHIFT = 0x8,
158 PCLK_DBG_B_DIV_MASK = 0x1f << PCLK_DBG_B_DIV_SHIFT,
159 ATCLK_CORE_B_DIV_SHIFT = 0,
160 ATCLK_CORE_B_DIV_MASK = 0x1f << ATCLK_CORE_B_DIV_SHIFT,
161
Kever Yangca19eac2016-07-29 10:35:25 +0800162 /* CLKSEL_CON14 */
163 PCLK_PERIHP_DIV_CON_SHIFT = 12,
164 PCLK_PERIHP_DIV_CON_MASK = 0x7 << PCLK_PERIHP_DIV_CON_SHIFT,
165 HCLK_PERIHP_DIV_CON_SHIFT = 8,
166 HCLK_PERIHP_DIV_CON_MASK = 3 << HCLK_PERIHP_DIV_CON_SHIFT,
167 ACLK_PERIHP_PLL_SEL_SHIFT = 7,
168 ACLK_PERIHP_PLL_SEL_MASK = 1 << ACLK_PERIHP_PLL_SEL_SHIFT,
169 ACLK_PERIHP_PLL_SEL_CPLL = 0,
170 ACLK_PERIHP_PLL_SEL_GPLL = 1,
171 ACLK_PERIHP_DIV_CON_SHIFT = 0,
172 ACLK_PERIHP_DIV_CON_MASK = 0x1f,
173
174 /* CLKSEL_CON21 */
175 ACLK_EMMC_PLL_SEL_SHIFT = 7,
176 ACLK_EMMC_PLL_SEL_MASK = 0x1 << ACLK_EMMC_PLL_SEL_SHIFT,
177 ACLK_EMMC_PLL_SEL_GPLL = 0x1,
178 ACLK_EMMC_DIV_CON_SHIFT = 0,
179 ACLK_EMMC_DIV_CON_MASK = 0x1f,
180
181 /* CLKSEL_CON22 */
182 CLK_EMMC_PLL_SHIFT = 8,
183 CLK_EMMC_PLL_MASK = 0x7 << CLK_EMMC_PLL_SHIFT,
184 CLK_EMMC_PLL_SEL_GPLL = 0x1,
Kever Yangdc850de2016-08-04 11:44:58 +0800185 CLK_EMMC_PLL_SEL_24M = 0x5,
Kever Yangca19eac2016-07-29 10:35:25 +0800186 CLK_EMMC_DIV_CON_SHIFT = 0,
187 CLK_EMMC_DIV_CON_MASK = 0x7f << CLK_EMMC_DIV_CON_SHIFT,
188
189 /* CLKSEL_CON23 */
190 PCLK_PERILP0_DIV_CON_SHIFT = 12,
191 PCLK_PERILP0_DIV_CON_MASK = 0x7 << PCLK_PERILP0_DIV_CON_SHIFT,
192 HCLK_PERILP0_DIV_CON_SHIFT = 8,
193 HCLK_PERILP0_DIV_CON_MASK = 3 << HCLK_PERILP0_DIV_CON_SHIFT,
194 ACLK_PERILP0_PLL_SEL_SHIFT = 7,
195 ACLK_PERILP0_PLL_SEL_MASK = 1 << ACLK_PERILP0_PLL_SEL_SHIFT,
196 ACLK_PERILP0_PLL_SEL_CPLL = 0,
197 ACLK_PERILP0_PLL_SEL_GPLL = 1,
198 ACLK_PERILP0_DIV_CON_SHIFT = 0,
199 ACLK_PERILP0_DIV_CON_MASK = 0x1f,
200
201 /* CLKSEL_CON25 */
202 PCLK_PERILP1_DIV_CON_SHIFT = 8,
203 PCLK_PERILP1_DIV_CON_MASK = 0x7 << PCLK_PERILP1_DIV_CON_SHIFT,
204 HCLK_PERILP1_PLL_SEL_SHIFT = 7,
205 HCLK_PERILP1_PLL_SEL_MASK = 1 << HCLK_PERILP1_PLL_SEL_SHIFT,
206 HCLK_PERILP1_PLL_SEL_CPLL = 0,
207 HCLK_PERILP1_PLL_SEL_GPLL = 1,
208 HCLK_PERILP1_DIV_CON_SHIFT = 0,
209 HCLK_PERILP1_DIV_CON_MASK = 0x1f,
210
211 /* CLKSEL_CON26 */
212 CLK_SARADC_DIV_CON_SHIFT = 8,
David Wuf91b9b42017-09-20 14:38:58 +0800213 CLK_SARADC_DIV_CON_MASK = GENMASK(15, 8),
214 CLK_SARADC_DIV_CON_WIDTH = 8,
Kever Yangca19eac2016-07-29 10:35:25 +0800215
216 /* CLKSEL_CON27 */
217 CLK_TSADC_SEL_X24M = 0x0,
218 CLK_TSADC_SEL_SHIFT = 15,
219 CLK_TSADC_SEL_MASK = 1 << CLK_TSADC_SEL_SHIFT,
220 CLK_TSADC_DIV_CON_SHIFT = 0,
221 CLK_TSADC_DIV_CON_MASK = 0x3ff,
222
223 /* CLKSEL_CON47 & CLKSEL_CON48 */
224 ACLK_VOP_PLL_SEL_SHIFT = 6,
225 ACLK_VOP_PLL_SEL_MASK = 0x3 << ACLK_VOP_PLL_SEL_SHIFT,
226 ACLK_VOP_PLL_SEL_CPLL = 0x1,
227 ACLK_VOP_DIV_CON_SHIFT = 0,
228 ACLK_VOP_DIV_CON_MASK = 0x1f << ACLK_VOP_DIV_CON_SHIFT,
229
230 /* CLKSEL_CON49 & CLKSEL_CON50 */
231 DCLK_VOP_DCLK_SEL_SHIFT = 11,
232 DCLK_VOP_DCLK_SEL_MASK = 1 << DCLK_VOP_DCLK_SEL_SHIFT,
233 DCLK_VOP_DCLK_SEL_DIVOUT = 0,
234 DCLK_VOP_PLL_SEL_SHIFT = 8,
235 DCLK_VOP_PLL_SEL_MASK = 3 << DCLK_VOP_PLL_SEL_SHIFT,
236 DCLK_VOP_PLL_SEL_VPLL = 0,
237 DCLK_VOP_DIV_CON_MASK = 0xff,
238 DCLK_VOP_DIV_CON_SHIFT = 0,
239
Jack Mitchell4ef38ce2020-09-17 10:42:06 +0100240 /* CLKSEL_CON57 */
241 PCLK_ALIVE_DIV_CON_SHIFT = 0,
242 PCLK_ALIVE_DIV_CON_MASK = 0x1f << PCLK_ALIVE_DIV_CON_SHIFT,
243
Kever Yangca19eac2016-07-29 10:35:25 +0800244 /* CLKSEL_CON58 */
Philipp Tomsichc31ee922017-04-20 22:05:49 +0200245 CLK_SPI_PLL_SEL_WIDTH = 1,
246 CLK_SPI_PLL_SEL_MASK = ((1 < CLK_SPI_PLL_SEL_WIDTH) - 1),
247 CLK_SPI_PLL_SEL_CPLL = 0,
248 CLK_SPI_PLL_SEL_GPLL = 1,
249 CLK_SPI_PLL_DIV_CON_WIDTH = 7,
250 CLK_SPI_PLL_DIV_CON_MASK = ((1 << CLK_SPI_PLL_DIV_CON_WIDTH) - 1),
251
252 CLK_SPI5_PLL_DIV_CON_SHIFT = 8,
253 CLK_SPI5_PLL_SEL_SHIFT = 15,
Kever Yangca19eac2016-07-29 10:35:25 +0800254
255 /* CLKSEL_CON59 */
256 CLK_SPI1_PLL_SEL_SHIFT = 15,
257 CLK_SPI1_PLL_DIV_CON_SHIFT = 8,
258 CLK_SPI0_PLL_SEL_SHIFT = 7,
259 CLK_SPI0_PLL_DIV_CON_SHIFT = 0,
260
261 /* CLKSEL_CON60 */
262 CLK_SPI4_PLL_SEL_SHIFT = 15,
263 CLK_SPI4_PLL_DIV_CON_SHIFT = 8,
264 CLK_SPI2_PLL_SEL_SHIFT = 7,
265 CLK_SPI2_PLL_DIV_CON_SHIFT = 0,
266
267 /* CLKSEL_CON61 */
268 CLK_I2C_PLL_SEL_MASK = 1,
269 CLK_I2C_PLL_SEL_CPLL = 0,
270 CLK_I2C_PLL_SEL_GPLL = 1,
271 CLK_I2C5_PLL_SEL_SHIFT = 15,
272 CLK_I2C5_DIV_CON_SHIFT = 8,
273 CLK_I2C1_PLL_SEL_SHIFT = 7,
274 CLK_I2C1_DIV_CON_SHIFT = 0,
275
276 /* CLKSEL_CON62 */
277 CLK_I2C6_PLL_SEL_SHIFT = 15,
278 CLK_I2C6_DIV_CON_SHIFT = 8,
279 CLK_I2C2_PLL_SEL_SHIFT = 7,
280 CLK_I2C2_DIV_CON_SHIFT = 0,
281
282 /* CLKSEL_CON63 */
283 CLK_I2C7_PLL_SEL_SHIFT = 15,
284 CLK_I2C7_DIV_CON_SHIFT = 8,
285 CLK_I2C3_PLL_SEL_SHIFT = 7,
286 CLK_I2C3_DIV_CON_SHIFT = 0,
287
288 /* CRU_SOFTRST_CON4 */
289 RESETN_DDR0_REQ_SHIFT = 8,
290 RESETN_DDR0_REQ_MASK = 1 << RESETN_DDR0_REQ_SHIFT,
291 RESETN_DDRPHY0_REQ_SHIFT = 9,
292 RESETN_DDRPHY0_REQ_MASK = 1 << RESETN_DDRPHY0_REQ_SHIFT,
293 RESETN_DDR1_REQ_SHIFT = 12,
294 RESETN_DDR1_REQ_MASK = 1 << RESETN_DDR1_REQ_SHIFT,
295 RESETN_DDRPHY1_REQ_SHIFT = 13,
296 RESETN_DDRPHY1_REQ_MASK = 1 << RESETN_DDRPHY1_REQ_SHIFT,
297};
298
299#define VCO_MAX_KHZ (3200 * (MHz / KHz))
300#define VCO_MIN_KHZ (800 * (MHz / KHz))
301#define OUTPUT_MAX_KHZ (3200 * (MHz / KHz))
302#define OUTPUT_MIN_KHZ (16 * (MHz / KHz))
303
304/*
305 * the div restructions of pll in integer mode, these are defined in
306 * * CRU_*PLL_CON0 or PMUCRU_*PLL_CON0
307 */
308#define PLL_DIV_MIN 16
309#define PLL_DIV_MAX 3200
310
311/*
312 * How to calculate the PLL(from TRM V0.3 Part 1 Page 63):
313 * Formulas also embedded within the Fractional PLL Verilog model:
314 * If DSMPD = 1 (DSM is disabled, "integer mode")
315 * FOUTVCO = FREF / REFDIV * FBDIV
316 * FOUTPOSTDIV = FOUTVCO / POSTDIV1 / POSTDIV2
317 * Where:
318 * FOUTVCO = Fractional PLL non-divided output frequency
319 * FOUTPOSTDIV = Fractional PLL divided output frequency
320 * (output of second post divider)
321 * FREF = Fractional PLL input reference frequency, (the OSC_HZ 24MHz input)
322 * REFDIV = Fractional PLL input reference clock divider
323 * FBDIV = Integer value programmed into feedback divide
324 *
325 */
326static void rkclk_set_pll(u32 *pll_con, const struct pll_div *div)
327{
328 /* All 8 PLLs have same VCO and output frequency range restrictions. */
329 u32 vco_khz = OSC_HZ / 1000 * div->fbdiv / div->refdiv;
330 u32 output_khz = vco_khz / div->postdiv1 / div->postdiv2;
331
332 debug("PLL at %p: fbdiv=%d, refdiv=%d, postdiv1=%d, "
333 "postdiv2=%d, vco=%u khz, output=%u khz\n",
334 pll_con, div->fbdiv, div->refdiv, div->postdiv1,
335 div->postdiv2, vco_khz, output_khz);
336 assert(vco_khz >= VCO_MIN_KHZ && vco_khz <= VCO_MAX_KHZ &&
337 output_khz >= OUTPUT_MIN_KHZ && output_khz <= OUTPUT_MAX_KHZ &&
338 div->fbdiv >= PLL_DIV_MIN && div->fbdiv <= PLL_DIV_MAX);
339
340 /*
341 * When power on or changing PLL setting,
342 * we must force PLL into slow mode to ensure output stable clock.
343 */
344 rk_clrsetreg(&pll_con[3], PLL_MODE_MASK,
345 PLL_MODE_SLOW << PLL_MODE_SHIFT);
346
347 /* use integer mode */
348 rk_clrsetreg(&pll_con[3], PLL_DSMPD_MASK,
349 PLL_INTEGER_MODE << PLL_DSMPD_SHIFT);
350
351 rk_clrsetreg(&pll_con[0], PLL_FBDIV_MASK,
352 div->fbdiv << PLL_FBDIV_SHIFT);
353 rk_clrsetreg(&pll_con[1],
354 PLL_POSTDIV2_MASK | PLL_POSTDIV1_MASK |
355 PLL_REFDIV_MASK | PLL_REFDIV_SHIFT,
356 (div->postdiv2 << PLL_POSTDIV2_SHIFT) |
357 (div->postdiv1 << PLL_POSTDIV1_SHIFT) |
358 (div->refdiv << PLL_REFDIV_SHIFT));
359
360 /* waiting for pll lock */
361 while (!(readl(&pll_con[2]) & (1 << PLL_LOCK_STATUS_SHIFT)))
362 udelay(1);
363
364 /* pll enter normal mode */
365 rk_clrsetreg(&pll_con[3], PLL_MODE_MASK,
366 PLL_MODE_NORM << PLL_MODE_SHIFT);
367}
368
369static int pll_para_config(u32 freq_hz, struct pll_div *div)
370{
371 u32 ref_khz = OSC_HZ / KHz, refdiv, fbdiv = 0;
372 u32 postdiv1, postdiv2 = 1;
373 u32 fref_khz;
374 u32 diff_khz, best_diff_khz;
375 const u32 max_refdiv = 63, max_fbdiv = 3200, min_fbdiv = 16;
376 const u32 max_postdiv1 = 7, max_postdiv2 = 7;
377 u32 vco_khz;
378 u32 freq_khz = freq_hz / KHz;
379
380 if (!freq_hz) {
381 printf("%s: the frequency can't be 0 Hz\n", __func__);
382 return -1;
383 }
384
385 postdiv1 = DIV_ROUND_UP(VCO_MIN_KHZ, freq_khz);
386 if (postdiv1 > max_postdiv1) {
387 postdiv2 = DIV_ROUND_UP(postdiv1, max_postdiv1);
388 postdiv1 = DIV_ROUND_UP(postdiv1, postdiv2);
389 }
390
391 vco_khz = freq_khz * postdiv1 * postdiv2;
392
393 if (vco_khz < VCO_MIN_KHZ || vco_khz > VCO_MAX_KHZ ||
394 postdiv2 > max_postdiv2) {
395 printf("%s: Cannot find out a supported VCO"
396 " for Frequency (%uHz).\n", __func__, freq_hz);
397 return -1;
398 }
399
400 div->postdiv1 = postdiv1;
401 div->postdiv2 = postdiv2;
402
403 best_diff_khz = vco_khz;
404 for (refdiv = 1; refdiv < max_refdiv && best_diff_khz; refdiv++) {
405 fref_khz = ref_khz / refdiv;
406
407 fbdiv = vco_khz / fref_khz;
Jagan Tekibef02a32019-07-15 23:51:10 +0530408 if (fbdiv >= max_fbdiv || fbdiv <= min_fbdiv)
Kever Yangca19eac2016-07-29 10:35:25 +0800409 continue;
410 diff_khz = vco_khz - fbdiv * fref_khz;
411 if (fbdiv + 1 < max_fbdiv && diff_khz > fref_khz / 2) {
412 fbdiv++;
413 diff_khz = fref_khz - diff_khz;
414 }
415
416 if (diff_khz >= best_diff_khz)
417 continue;
418
419 best_diff_khz = diff_khz;
420 div->refdiv = refdiv;
421 div->fbdiv = fbdiv;
422 }
423
Jagan Tekibef02a32019-07-15 23:51:10 +0530424 if (best_diff_khz > 4 * (MHz / KHz)) {
Kever Yangca19eac2016-07-29 10:35:25 +0800425 printf("%s: Failed to match output frequency %u, "
426 "difference is %u Hz,exceed 4MHZ\n", __func__, freq_hz,
427 best_diff_khz * KHz);
428 return -1;
429 }
430 return 0;
431}
432
Jagan Teki783acfd2020-01-09 14:22:17 +0530433void rk3399_configure_cpu_l(struct rockchip_cru *cru,
Christoph Muellner25c7ba92018-11-30 20:32:48 +0100434 enum apll_l_frequencies apll_l_freq)
Kever Yangca19eac2016-07-29 10:35:25 +0800435{
436 u32 aclkm_div;
437 u32 pclk_dbg_div;
438 u32 atclk_div;
439
Christoph Muellner25c7ba92018-11-30 20:32:48 +0100440 /* Setup cluster L */
Kever Yangca19eac2016-07-29 10:35:25 +0800441 rkclk_set_pll(&cru->apll_l_con[0], apll_l_cfgs[apll_l_freq]);
442
Christoph Muellner25c7ba92018-11-30 20:32:48 +0100443 aclkm_div = LPLL_HZ / ACLKM_CORE_L_HZ - 1;
444 assert((aclkm_div + 1) * ACLKM_CORE_L_HZ == LPLL_HZ &&
Kever Yangca19eac2016-07-29 10:35:25 +0800445 aclkm_div < 0x1f);
446
Christoph Muellner25c7ba92018-11-30 20:32:48 +0100447 pclk_dbg_div = LPLL_HZ / PCLK_DBG_L_HZ - 1;
448 assert((pclk_dbg_div + 1) * PCLK_DBG_L_HZ == LPLL_HZ &&
Kever Yangca19eac2016-07-29 10:35:25 +0800449 pclk_dbg_div < 0x1f);
450
Christoph Muellner25c7ba92018-11-30 20:32:48 +0100451 atclk_div = LPLL_HZ / ATCLK_CORE_L_HZ - 1;
452 assert((atclk_div + 1) * ATCLK_CORE_L_HZ == LPLL_HZ &&
Kever Yangca19eac2016-07-29 10:35:25 +0800453 atclk_div < 0x1f);
454
455 rk_clrsetreg(&cru->clksel_con[0],
456 ACLKM_CORE_L_DIV_CON_MASK | CLK_CORE_L_PLL_SEL_MASK |
457 CLK_CORE_L_DIV_MASK,
458 aclkm_div << ACLKM_CORE_L_DIV_CON_SHIFT |
459 CLK_CORE_L_PLL_SEL_ALPLL << CLK_CORE_L_PLL_SEL_SHIFT |
460 0 << CLK_CORE_L_DIV_SHIFT);
461
462 rk_clrsetreg(&cru->clksel_con[1],
463 PCLK_DBG_L_DIV_MASK | ATCLK_CORE_L_DIV_MASK,
464 pclk_dbg_div << PCLK_DBG_L_DIV_SHIFT |
465 atclk_div << ATCLK_CORE_L_DIV_SHIFT);
466}
Christoph Muellner25c7ba92018-11-30 20:32:48 +0100467
Jagan Teki783acfd2020-01-09 14:22:17 +0530468void rk3399_configure_cpu_b(struct rockchip_cru *cru,
Christoph Muellner25c7ba92018-11-30 20:32:48 +0100469 enum apll_b_frequencies apll_b_freq)
470{
471 u32 aclkm_div;
472 u32 pclk_dbg_div;
473 u32 atclk_div;
474
475 /* Setup cluster B */
476 rkclk_set_pll(&cru->apll_b_con[0], apll_b_cfgs[apll_b_freq]);
477
478 aclkm_div = BPLL_HZ / ACLKM_CORE_B_HZ - 1;
479 assert((aclkm_div + 1) * ACLKM_CORE_B_HZ == BPLL_HZ &&
480 aclkm_div < 0x1f);
481
482 pclk_dbg_div = BPLL_HZ / PCLK_DBG_B_HZ - 1;
483 assert((pclk_dbg_div + 1) * PCLK_DBG_B_HZ == BPLL_HZ &&
484 pclk_dbg_div < 0x1f);
485
486 atclk_div = BPLL_HZ / ATCLK_CORE_B_HZ - 1;
487 assert((atclk_div + 1) * ATCLK_CORE_B_HZ == BPLL_HZ &&
488 atclk_div < 0x1f);
489
490 rk_clrsetreg(&cru->clksel_con[2],
491 ACLKM_CORE_B_DIV_CON_MASK | CLK_CORE_B_PLL_SEL_MASK |
492 CLK_CORE_B_DIV_MASK,
493 aclkm_div << ACLKM_CORE_B_DIV_CON_SHIFT |
494 CLK_CORE_B_PLL_SEL_ABPLL << CLK_CORE_B_PLL_SEL_SHIFT |
495 0 << CLK_CORE_B_DIV_SHIFT);
496
497 rk_clrsetreg(&cru->clksel_con[3],
498 PCLK_DBG_B_DIV_MASK | ATCLK_CORE_B_DIV_MASK,
499 pclk_dbg_div << PCLK_DBG_B_DIV_SHIFT |
500 atclk_div << ATCLK_CORE_B_DIV_SHIFT);
501}
502
Kever Yangca19eac2016-07-29 10:35:25 +0800503#define I2C_CLK_REG_MASK(bus) \
Jagan Tekibef02a32019-07-15 23:51:10 +0530504 (I2C_DIV_CON_MASK << CLK_I2C ##bus## _DIV_CON_SHIFT | \
505 CLK_I2C_PLL_SEL_MASK << CLK_I2C ##bus## _PLL_SEL_SHIFT)
Kever Yangca19eac2016-07-29 10:35:25 +0800506
507#define I2C_CLK_REG_VALUE(bus, clk_div) \
Jagan Tekibef02a32019-07-15 23:51:10 +0530508 ((clk_div - 1) << CLK_I2C ##bus## _DIV_CON_SHIFT | \
509 CLK_I2C_PLL_SEL_GPLL << CLK_I2C ##bus## _PLL_SEL_SHIFT)
Kever Yangca19eac2016-07-29 10:35:25 +0800510
511#define I2C_CLK_DIV_VALUE(con, bus) \
Jagan Tekibef02a32019-07-15 23:51:10 +0530512 ((con >> CLK_I2C ##bus## _DIV_CON_SHIFT) & I2C_DIV_CON_MASK)
Kever Yangca19eac2016-07-29 10:35:25 +0800513
Kever Yange54d26a2016-08-12 17:47:15 +0800514#define I2C_PMUCLK_REG_MASK(bus) \
Jagan Tekibef02a32019-07-15 23:51:10 +0530515 (I2C_DIV_CON_MASK << CLK_I2C ##bus## _DIV_CON_SHIFT)
Kever Yange54d26a2016-08-12 17:47:15 +0800516
517#define I2C_PMUCLK_REG_VALUE(bus, clk_div) \
Jagan Tekibef02a32019-07-15 23:51:10 +0530518 ((clk_div - 1) << CLK_I2C ##bus## _DIV_CON_SHIFT)
Kever Yange54d26a2016-08-12 17:47:15 +0800519
Jagan Teki783acfd2020-01-09 14:22:17 +0530520static ulong rk3399_i2c_get_clk(struct rockchip_cru *cru, ulong clk_id)
Kever Yangca19eac2016-07-29 10:35:25 +0800521{
522 u32 div, con;
523
524 switch (clk_id) {
525 case SCLK_I2C1:
526 con = readl(&cru->clksel_con[61]);
527 div = I2C_CLK_DIV_VALUE(con, 1);
528 break;
529 case SCLK_I2C2:
530 con = readl(&cru->clksel_con[62]);
531 div = I2C_CLK_DIV_VALUE(con, 2);
532 break;
533 case SCLK_I2C3:
534 con = readl(&cru->clksel_con[63]);
535 div = I2C_CLK_DIV_VALUE(con, 3);
536 break;
537 case SCLK_I2C5:
538 con = readl(&cru->clksel_con[61]);
539 div = I2C_CLK_DIV_VALUE(con, 5);
540 break;
541 case SCLK_I2C6:
542 con = readl(&cru->clksel_con[62]);
543 div = I2C_CLK_DIV_VALUE(con, 6);
544 break;
545 case SCLK_I2C7:
546 con = readl(&cru->clksel_con[63]);
547 div = I2C_CLK_DIV_VALUE(con, 7);
548 break;
549 default:
550 printf("do not support this i2c bus\n");
551 return -EINVAL;
552 }
553
554 return DIV_TO_RATE(GPLL_HZ, div);
555}
556
Jagan Teki783acfd2020-01-09 14:22:17 +0530557static ulong rk3399_i2c_set_clk(struct rockchip_cru *cru, ulong clk_id, uint hz)
Kever Yangca19eac2016-07-29 10:35:25 +0800558{
559 int src_clk_div;
560
561 /* i2c0,4,8 src clock from ppll, i2c1,2,3,5,6,7 src clock from gpll*/
562 src_clk_div = GPLL_HZ / hz;
563 assert(src_clk_div - 1 < 127);
564
565 switch (clk_id) {
566 case SCLK_I2C1:
567 rk_clrsetreg(&cru->clksel_con[61], I2C_CLK_REG_MASK(1),
568 I2C_CLK_REG_VALUE(1, src_clk_div));
569 break;
570 case SCLK_I2C2:
571 rk_clrsetreg(&cru->clksel_con[62], I2C_CLK_REG_MASK(2),
572 I2C_CLK_REG_VALUE(2, src_clk_div));
573 break;
574 case SCLK_I2C3:
575 rk_clrsetreg(&cru->clksel_con[63], I2C_CLK_REG_MASK(3),
576 I2C_CLK_REG_VALUE(3, src_clk_div));
577 break;
578 case SCLK_I2C5:
579 rk_clrsetreg(&cru->clksel_con[61], I2C_CLK_REG_MASK(5),
580 I2C_CLK_REG_VALUE(5, src_clk_div));
581 break;
582 case SCLK_I2C6:
583 rk_clrsetreg(&cru->clksel_con[62], I2C_CLK_REG_MASK(6),
584 I2C_CLK_REG_VALUE(6, src_clk_div));
585 break;
586 case SCLK_I2C7:
587 rk_clrsetreg(&cru->clksel_con[63], I2C_CLK_REG_MASK(7),
588 I2C_CLK_REG_VALUE(7, src_clk_div));
589 break;
590 default:
591 printf("do not support this i2c bus\n");
592 return -EINVAL;
593 }
594
Philipp Tomsich30d7c152017-04-20 22:05:50 +0200595 return rk3399_i2c_get_clk(cru, clk_id);
Kever Yangca19eac2016-07-29 10:35:25 +0800596}
597
Philipp Tomsichc31ee922017-04-20 22:05:49 +0200598/*
599 * RK3399 SPI clocks have a common divider-width (7 bits) and a single bit
600 * to select either CPLL or GPLL as the clock-parent. The location within
601 * the enclosing CLKSEL_CON (i.e. div_shift and sel_shift) are variable.
602 */
603
604struct spi_clkreg {
Jagan Tekibef02a32019-07-15 23:51:10 +0530605 u8 reg; /* CLKSEL_CON[reg] register in CRU */
606 u8 div_shift;
607 u8 sel_shift;
Philipp Tomsichc31ee922017-04-20 22:05:49 +0200608};
609
610/*
611 * The entries are numbered relative to their offset from SCLK_SPI0.
612 *
613 * Note that SCLK_SPI3 (which is configured via PMUCRU and requires different
614 * logic is not supported).
615 */
616static const struct spi_clkreg spi_clkregs[] = {
617 [0] = { .reg = 59,
618 .div_shift = CLK_SPI0_PLL_DIV_CON_SHIFT,
619 .sel_shift = CLK_SPI0_PLL_SEL_SHIFT, },
620 [1] = { .reg = 59,
621 .div_shift = CLK_SPI1_PLL_DIV_CON_SHIFT,
622 .sel_shift = CLK_SPI1_PLL_SEL_SHIFT, },
623 [2] = { .reg = 60,
624 .div_shift = CLK_SPI2_PLL_DIV_CON_SHIFT,
625 .sel_shift = CLK_SPI2_PLL_SEL_SHIFT, },
626 [3] = { .reg = 60,
627 .div_shift = CLK_SPI4_PLL_DIV_CON_SHIFT,
628 .sel_shift = CLK_SPI4_PLL_SEL_SHIFT, },
629 [4] = { .reg = 58,
630 .div_shift = CLK_SPI5_PLL_DIV_CON_SHIFT,
631 .sel_shift = CLK_SPI5_PLL_SEL_SHIFT, },
632};
633
Jagan Teki783acfd2020-01-09 14:22:17 +0530634static ulong rk3399_spi_get_clk(struct rockchip_cru *cru, ulong clk_id)
Philipp Tomsichc31ee922017-04-20 22:05:49 +0200635{
636 const struct spi_clkreg *spiclk = NULL;
637 u32 div, val;
638
639 switch (clk_id) {
640 case SCLK_SPI0 ... SCLK_SPI5:
641 spiclk = &spi_clkregs[clk_id - SCLK_SPI0];
642 break;
643
644 default:
Masahiro Yamada81e10422017-09-16 14:10:41 +0900645 pr_err("%s: SPI clk-id %ld not supported\n", __func__, clk_id);
Philipp Tomsichc31ee922017-04-20 22:05:49 +0200646 return -EINVAL;
647 }
648
649 val = readl(&cru->clksel_con[spiclk->reg]);
Philipp Tomsich8a4868f2017-11-22 19:45:04 +0100650 div = bitfield_extract(val, spiclk->div_shift,
651 CLK_SPI_PLL_DIV_CON_WIDTH);
Philipp Tomsichc31ee922017-04-20 22:05:49 +0200652
653 return DIV_TO_RATE(GPLL_HZ, div);
654}
655
Jagan Teki783acfd2020-01-09 14:22:17 +0530656static ulong rk3399_spi_set_clk(struct rockchip_cru *cru, ulong clk_id, uint hz)
Philipp Tomsichc31ee922017-04-20 22:05:49 +0200657{
658 const struct spi_clkreg *spiclk = NULL;
659 int src_clk_div;
660
Kever Yangf20995b2017-07-27 12:54:02 +0800661 src_clk_div = DIV_ROUND_UP(GPLL_HZ, hz) - 1;
662 assert(src_clk_div < 128);
Philipp Tomsichc31ee922017-04-20 22:05:49 +0200663
664 switch (clk_id) {
665 case SCLK_SPI1 ... SCLK_SPI5:
666 spiclk = &spi_clkregs[clk_id - SCLK_SPI0];
667 break;
668
669 default:
Masahiro Yamada81e10422017-09-16 14:10:41 +0900670 pr_err("%s: SPI clk-id %ld not supported\n", __func__, clk_id);
Philipp Tomsichc31ee922017-04-20 22:05:49 +0200671 return -EINVAL;
672 }
673
674 rk_clrsetreg(&cru->clksel_con[spiclk->reg],
675 ((CLK_SPI_PLL_DIV_CON_MASK << spiclk->div_shift) |
676 (CLK_SPI_PLL_SEL_GPLL << spiclk->sel_shift)),
677 ((src_clk_div << spiclk->div_shift) |
678 (CLK_SPI_PLL_SEL_GPLL << spiclk->sel_shift)));
679
Philipp Tomsich30d7c152017-04-20 22:05:50 +0200680 return rk3399_spi_get_clk(cru, clk_id);
Philipp Tomsichc31ee922017-04-20 22:05:49 +0200681}
682
Jagan Teki783acfd2020-01-09 14:22:17 +0530683static ulong rk3399_vop_set_clk(struct rockchip_cru *cru, ulong clk_id, u32 hz)
Kever Yangca19eac2016-07-29 10:35:25 +0800684{
685 struct pll_div vpll_config = {0};
Jagan Tekibef02a32019-07-15 23:51:10 +0530686 int aclk_vop = 198 * MHz;
Kever Yangca19eac2016-07-29 10:35:25 +0800687 void *aclkreg_addr, *dclkreg_addr;
688 u32 div;
689
690 switch (clk_id) {
691 case DCLK_VOP0:
692 aclkreg_addr = &cru->clksel_con[47];
693 dclkreg_addr = &cru->clksel_con[49];
694 break;
695 case DCLK_VOP1:
696 aclkreg_addr = &cru->clksel_con[48];
697 dclkreg_addr = &cru->clksel_con[50];
698 break;
699 default:
700 return -EINVAL;
701 }
702 /* vop aclk source clk: cpll */
703 div = CPLL_HZ / aclk_vop;
704 assert(div - 1 < 32);
705
706 rk_clrsetreg(aclkreg_addr,
707 ACLK_VOP_PLL_SEL_MASK | ACLK_VOP_DIV_CON_MASK,
708 ACLK_VOP_PLL_SEL_CPLL << ACLK_VOP_PLL_SEL_SHIFT |
709 (div - 1) << ACLK_VOP_DIV_CON_SHIFT);
710
711 /* vop dclk source from vpll, and equals to vpll(means div == 1) */
712 if (pll_para_config(hz, &vpll_config))
713 return -1;
714
715 rkclk_set_pll(&cru->vpll_con[0], &vpll_config);
716
717 rk_clrsetreg(dclkreg_addr,
Jagan Tekibef02a32019-07-15 23:51:10 +0530718 DCLK_VOP_DCLK_SEL_MASK | DCLK_VOP_PLL_SEL_MASK |
Kever Yangca19eac2016-07-29 10:35:25 +0800719 DCLK_VOP_DIV_CON_MASK,
720 DCLK_VOP_DCLK_SEL_DIVOUT << DCLK_VOP_DCLK_SEL_SHIFT |
721 DCLK_VOP_PLL_SEL_VPLL << DCLK_VOP_PLL_SEL_SHIFT |
722 (1 - 1) << DCLK_VOP_DIV_CON_SHIFT);
723
724 return hz;
725}
726
Jagan Teki783acfd2020-01-09 14:22:17 +0530727static ulong rk3399_mmc_get_clk(struct rockchip_cru *cru, uint clk_id)
Kever Yangca19eac2016-07-29 10:35:25 +0800728{
729 u32 div, con;
730
731 switch (clk_id) {
Michal Suchanek84789442022-08-21 09:17:24 +0200732 case HCLK_SDIO:
733 case SCLK_SDIO:
734 con = readl(&cru->clksel_con[15]);
735 /* dwmmc controller have internal div 2 */
736 div = 2;
737 break;
Philipp Tomsich78a73142017-04-25 09:52:06 +0200738 case HCLK_SDMMC:
Kever Yangca19eac2016-07-29 10:35:25 +0800739 case SCLK_SDMMC:
740 con = readl(&cru->clksel_con[16]);
Kever Yang99b546d2017-07-27 12:54:01 +0800741 /* dwmmc controller have internal div 2 */
742 div = 2;
Kever Yangca19eac2016-07-29 10:35:25 +0800743 break;
744 case SCLK_EMMC:
Jagan Tekiad386002020-05-24 22:13:15 +0530745 con = readl(&cru->clksel_con[22]);
Kever Yang99b546d2017-07-27 12:54:01 +0800746 div = 1;
Kever Yangca19eac2016-07-29 10:35:25 +0800747 break;
748 default:
749 return -EINVAL;
750 }
Kever Yangca19eac2016-07-29 10:35:25 +0800751
Kever Yang99b546d2017-07-27 12:54:01 +0800752 div *= (con & CLK_EMMC_DIV_CON_MASK) >> CLK_EMMC_DIV_CON_SHIFT;
Kever Yangdc850de2016-08-04 11:44:58 +0800753 if ((con & CLK_EMMC_PLL_MASK) >> CLK_EMMC_PLL_SHIFT
754 == CLK_EMMC_PLL_SEL_24M)
Kever Yang99b546d2017-07-27 12:54:01 +0800755 return DIV_TO_RATE(OSC_HZ, div);
Kever Yangdc850de2016-08-04 11:44:58 +0800756 else
757 return DIV_TO_RATE(GPLL_HZ, div);
Kever Yangca19eac2016-07-29 10:35:25 +0800758}
759
Michal Suchanek84789442022-08-21 09:17:24 +0200760static void rk3399_dwmmc_set_clk(struct rockchip_cru *cru,
761 unsigned int con, ulong set_rate)
762{
763 /* Select clk_sdmmc source from GPLL by default */
764 /* mmc clock defaulg div 2 internal, provide double in cru */
765 int src_clk_div = DIV_ROUND_UP(GPLL_HZ / 2, set_rate);
766
767 if (src_clk_div > 128) {
768 /* use 24MHz source for 400KHz clock */
769 src_clk_div = DIV_ROUND_UP(OSC_HZ / 2, set_rate);
770 assert(src_clk_div - 1 < 128);
771 rk_clrsetreg(&cru->clksel_con[con],
772 CLK_EMMC_PLL_MASK | CLK_EMMC_DIV_CON_MASK,
773 CLK_EMMC_PLL_SEL_24M << CLK_EMMC_PLL_SHIFT |
774 (src_clk_div - 1) << CLK_EMMC_DIV_CON_SHIFT);
775 } else {
776 rk_clrsetreg(&cru->clksel_con[con],
777 CLK_EMMC_PLL_MASK | CLK_EMMC_DIV_CON_MASK,
778 CLK_EMMC_PLL_SEL_GPLL << CLK_EMMC_PLL_SHIFT |
779 (src_clk_div - 1) << CLK_EMMC_DIV_CON_SHIFT);
780 }
781}
782
Jagan Teki783acfd2020-01-09 14:22:17 +0530783static ulong rk3399_mmc_set_clk(struct rockchip_cru *cru,
Kever Yangca19eac2016-07-29 10:35:25 +0800784 ulong clk_id, ulong set_rate)
785{
Kever Yangca19eac2016-07-29 10:35:25 +0800786 switch (clk_id) {
Michal Suchanek84789442022-08-21 09:17:24 +0200787 case HCLK_SDIO:
788 case SCLK_SDIO:
789 rk3399_dwmmc_set_clk(cru, 15, set_rate);
790 break;
Philipp Tomsich78a73142017-04-25 09:52:06 +0200791 case HCLK_SDMMC:
Kever Yangca19eac2016-07-29 10:35:25 +0800792 case SCLK_SDMMC:
Michal Suchanek84789442022-08-21 09:17:24 +0200793 rk3399_dwmmc_set_clk(cru, 16, set_rate);
Kever Yangca19eac2016-07-29 10:35:25 +0800794 break;
Michal Suchanek84789442022-08-21 09:17:24 +0200795 case SCLK_EMMC: {
796 int aclk_emmc = 198 * MHz;
Kever Yangca19eac2016-07-29 10:35:25 +0800797 /* Select aclk_emmc source from GPLL */
Michal Suchanek84789442022-08-21 09:17:24 +0200798 int src_clk_div = DIV_ROUND_UP(GPLL_HZ, aclk_emmc);
799
Kever Yangf20995b2017-07-27 12:54:02 +0800800 assert(src_clk_div - 1 < 32);
Kever Yangca19eac2016-07-29 10:35:25 +0800801
802 rk_clrsetreg(&cru->clksel_con[21],
803 ACLK_EMMC_PLL_SEL_MASK | ACLK_EMMC_DIV_CON_MASK,
804 ACLK_EMMC_PLL_SEL_GPLL << ACLK_EMMC_PLL_SEL_SHIFT |
805 (src_clk_div - 1) << ACLK_EMMC_DIV_CON_SHIFT);
806
807 /* Select clk_emmc source from GPLL too */
Kever Yangf20995b2017-07-27 12:54:02 +0800808 src_clk_div = DIV_ROUND_UP(GPLL_HZ, set_rate);
809 assert(src_clk_div - 1 < 128);
Kever Yangca19eac2016-07-29 10:35:25 +0800810
811 rk_clrsetreg(&cru->clksel_con[22],
812 CLK_EMMC_PLL_MASK | CLK_EMMC_DIV_CON_MASK,
813 CLK_EMMC_PLL_SEL_GPLL << CLK_EMMC_PLL_SHIFT |
814 (src_clk_div - 1) << CLK_EMMC_DIV_CON_SHIFT);
815 break;
Michal Suchanek84789442022-08-21 09:17:24 +0200816 }
Kever Yangca19eac2016-07-29 10:35:25 +0800817 default:
818 return -EINVAL;
819 }
820 return rk3399_mmc_get_clk(cru, clk_id);
821}
822
Jagan Teki783acfd2020-01-09 14:22:17 +0530823static ulong rk3399_gmac_set_clk(struct rockchip_cru *cru, ulong rate)
Philipp Tomsichf4ba6ed2018-01-08 13:11:01 +0100824{
825 ulong ret;
826
827 /*
828 * The RGMII CLK can be derived either from an external "clkin"
829 * or can be generated from internally by a divider from SCLK_MAC.
830 */
831 if (readl(&cru->clksel_con[19]) & BIT(4)) {
832 /* An external clock will always generate the right rate... */
833 ret = rate;
834 } else {
835 /*
836 * No platform uses an internal clock to date.
837 * Implement this once it becomes necessary and print an error
838 * if someone tries to use it (while it remains unimplemented).
839 */
840 pr_err("%s: internal clock is UNIMPLEMENTED\n", __func__);
841 ret = 0;
842 }
843
844 return ret;
845}
846
Kever Yange1980532017-02-13 17:38:56 +0800847#define PMUSGRF_DDR_RGN_CON16 0xff330040
Jagan Teki783acfd2020-01-09 14:22:17 +0530848static ulong rk3399_ddr_set_clk(struct rockchip_cru *cru,
Kever Yange1980532017-02-13 17:38:56 +0800849 ulong set_rate)
850{
851 struct pll_div dpll_cfg;
852
853 /* IC ECO bug, need to set this register */
854 writel(0xc000c000, PMUSGRF_DDR_RGN_CON16);
855
856 /* clk_ddrc == DPLL = 24MHz / refdiv * fbdiv / postdiv1 / postdiv2 */
857 switch (set_rate) {
Jagan Teki4833f322019-07-16 17:27:35 +0530858 case 50 * MHz:
859 dpll_cfg = (struct pll_div)
Xavier Drudis Ferran7d20cf92022-07-16 12:31:45 +0200860 {.refdiv = 2, .fbdiv = 75, .postdiv1 = 3, .postdiv2 = 6};
Jagan Teki4833f322019-07-16 17:27:35 +0530861 break;
Jagan Tekibef02a32019-07-15 23:51:10 +0530862 case 200 * MHz:
Kever Yange1980532017-02-13 17:38:56 +0800863 dpll_cfg = (struct pll_div)
864 {.refdiv = 1, .fbdiv = 50, .postdiv1 = 6, .postdiv2 = 1};
865 break;
Jagan Tekibef02a32019-07-15 23:51:10 +0530866 case 300 * MHz:
Kever Yange1980532017-02-13 17:38:56 +0800867 dpll_cfg = (struct pll_div)
868 {.refdiv = 2, .fbdiv = 100, .postdiv1 = 4, .postdiv2 = 1};
869 break;
Jagan Tekif0b06312019-07-16 17:27:36 +0530870 case 400 * MHz:
871 dpll_cfg = (struct pll_div)
872 {.refdiv = 1, .fbdiv = 50, .postdiv1 = 3, .postdiv2 = 1};
873 break;
Jagan Tekibef02a32019-07-15 23:51:10 +0530874 case 666 * MHz:
Kever Yange1980532017-02-13 17:38:56 +0800875 dpll_cfg = (struct pll_div)
876 {.refdiv = 2, .fbdiv = 111, .postdiv1 = 2, .postdiv2 = 1};
877 break;
Jagan Tekibef02a32019-07-15 23:51:10 +0530878 case 800 * MHz:
Kever Yange1980532017-02-13 17:38:56 +0800879 dpll_cfg = (struct pll_div)
880 {.refdiv = 1, .fbdiv = 100, .postdiv1 = 3, .postdiv2 = 1};
881 break;
Jagan Tekibef02a32019-07-15 23:51:10 +0530882 case 933 * MHz:
Kever Yange1980532017-02-13 17:38:56 +0800883 dpll_cfg = (struct pll_div)
884 {.refdiv = 1, .fbdiv = 116, .postdiv1 = 3, .postdiv2 = 1};
885 break;
886 default:
Masahiro Yamada81e10422017-09-16 14:10:41 +0900887 pr_err("Unsupported SDRAM frequency!,%ld\n", set_rate);
Kever Yange1980532017-02-13 17:38:56 +0800888 }
889 rkclk_set_pll(&cru->dpll_con[0], &dpll_cfg);
890
891 return set_rate;
892}
David Wuf91b9b42017-09-20 14:38:58 +0800893
Jack Mitchell4ef38ce2020-09-17 10:42:06 +0100894static ulong rk3399_alive_get_clk(struct rockchip_cru *cru)
895{
896 u32 div, val;
897
898 val = readl(&cru->clksel_con[57]);
899 div = (val & PCLK_ALIVE_DIV_CON_MASK) >>
900 PCLK_ALIVE_DIV_CON_SHIFT;
901
902 return DIV_TO_RATE(GPLL_HZ, div);
903}
904
Jagan Teki783acfd2020-01-09 14:22:17 +0530905static ulong rk3399_saradc_get_clk(struct rockchip_cru *cru)
David Wuf91b9b42017-09-20 14:38:58 +0800906{
907 u32 div, val;
908
909 val = readl(&cru->clksel_con[26]);
910 div = bitfield_extract(val, CLK_SARADC_DIV_CON_SHIFT,
911 CLK_SARADC_DIV_CON_WIDTH);
912
913 return DIV_TO_RATE(OSC_HZ, div);
914}
915
Jagan Teki783acfd2020-01-09 14:22:17 +0530916static ulong rk3399_saradc_set_clk(struct rockchip_cru *cru, uint hz)
David Wuf91b9b42017-09-20 14:38:58 +0800917{
918 int src_clk_div;
919
920 src_clk_div = DIV_ROUND_UP(OSC_HZ, hz) - 1;
921 assert(src_clk_div < 128);
922
923 rk_clrsetreg(&cru->clksel_con[26],
924 CLK_SARADC_DIV_CON_MASK,
925 src_clk_div << CLK_SARADC_DIV_CON_SHIFT);
926
927 return rk3399_saradc_get_clk(cru);
928}
929
Kever Yangca19eac2016-07-29 10:35:25 +0800930static ulong rk3399_clk_get_rate(struct clk *clk)
931{
932 struct rk3399_clk_priv *priv = dev_get_priv(clk->dev);
933 ulong rate = 0;
934
935 switch (clk->id) {
936 case 0 ... 63:
937 return 0;
Michal Suchanek84789442022-08-21 09:17:24 +0200938 case HCLK_SDIO:
939 case SCLK_SDIO:
Philipp Tomsich78a73142017-04-25 09:52:06 +0200940 case HCLK_SDMMC:
Kever Yangca19eac2016-07-29 10:35:25 +0800941 case SCLK_SDMMC:
942 case SCLK_EMMC:
943 rate = rk3399_mmc_get_clk(priv->cru, clk->id);
944 break;
945 case SCLK_I2C1:
946 case SCLK_I2C2:
947 case SCLK_I2C3:
948 case SCLK_I2C5:
949 case SCLK_I2C6:
950 case SCLK_I2C7:
951 rate = rk3399_i2c_get_clk(priv->cru, clk->id);
952 break;
Philipp Tomsichc31ee922017-04-20 22:05:49 +0200953 case SCLK_SPI0...SCLK_SPI5:
954 rate = rk3399_spi_get_clk(priv->cru, clk->id);
955 break;
956 case SCLK_UART0:
Christoph Muellnere5607a02019-05-07 10:58:44 +0200957 case SCLK_UART1:
Philipp Tomsichc31ee922017-04-20 22:05:49 +0200958 case SCLK_UART2:
Christoph Muellnere5607a02019-05-07 10:58:44 +0200959 case SCLK_UART3:
Philipp Tomsichc31ee922017-04-20 22:05:49 +0200960 return 24000000;
Philipp Tomsich10b594b2017-04-28 18:33:57 +0200961 case PCLK_HDMI_CTRL:
962 break;
Kever Yangca19eac2016-07-29 10:35:25 +0800963 case DCLK_VOP0:
964 case DCLK_VOP1:
965 break;
Philipp Tomsichd10b45e2017-04-28 17:11:55 +0200966 case PCLK_EFUSE1024NS:
967 break;
David Wuf91b9b42017-09-20 14:38:58 +0800968 case SCLK_SARADC:
969 rate = rk3399_saradc_get_clk(priv->cru);
970 break;
Simon Glassd27b3172019-01-21 14:53:30 -0700971 case ACLK_VIO:
972 case ACLK_HDCP:
973 case ACLK_GIC_PRE:
974 case PCLK_DDR:
975 break;
Jack Mitchell4ef38ce2020-09-17 10:42:06 +0100976 case PCLK_ALIVE:
977 case PCLK_WDT:
978 rate = rk3399_alive_get_clk(priv->cru);
979 break;
Kever Yangca19eac2016-07-29 10:35:25 +0800980 default:
Simon Glassd27b3172019-01-21 14:53:30 -0700981 log_debug("Unknown clock %lu\n", clk->id);
Kever Yangca19eac2016-07-29 10:35:25 +0800982 return -ENOENT;
983 }
984
985 return rate;
986}
987
988static ulong rk3399_clk_set_rate(struct clk *clk, ulong rate)
989{
990 struct rk3399_clk_priv *priv = dev_get_priv(clk->dev);
991 ulong ret = 0;
992
993 switch (clk->id) {
994 case 0 ... 63:
995 return 0;
Philipp Tomsich2d20a632018-01-08 14:00:27 +0100996
997 case ACLK_PERIHP:
998 case HCLK_PERIHP:
999 case PCLK_PERIHP:
1000 return 0;
1001
1002 case ACLK_PERILP0:
1003 case HCLK_PERILP0:
1004 case PCLK_PERILP0:
1005 return 0;
1006
1007 case ACLK_CCI:
1008 return 0;
1009
1010 case HCLK_PERILP1:
1011 case PCLK_PERILP1:
1012 return 0;
1013
Michal Suchanek84789442022-08-21 09:17:24 +02001014 case HCLK_SDIO:
1015 case SCLK_SDIO:
Philipp Tomsich78a73142017-04-25 09:52:06 +02001016 case HCLK_SDMMC:
Kever Yangca19eac2016-07-29 10:35:25 +08001017 case SCLK_SDMMC:
1018 case SCLK_EMMC:
1019 ret = rk3399_mmc_set_clk(priv->cru, clk->id, rate);
1020 break;
Philipp Tomsichbfa896c2017-03-24 19:24:25 +01001021 case SCLK_MAC:
Philipp Tomsichf4ba6ed2018-01-08 13:11:01 +01001022 ret = rk3399_gmac_set_clk(priv->cru, rate);
Philipp Tomsichbfa896c2017-03-24 19:24:25 +01001023 break;
Kever Yangca19eac2016-07-29 10:35:25 +08001024 case SCLK_I2C1:
1025 case SCLK_I2C2:
1026 case SCLK_I2C3:
1027 case SCLK_I2C5:
1028 case SCLK_I2C6:
1029 case SCLK_I2C7:
1030 ret = rk3399_i2c_set_clk(priv->cru, clk->id, rate);
1031 break;
Philipp Tomsichc31ee922017-04-20 22:05:49 +02001032 case SCLK_SPI0...SCLK_SPI5:
1033 ret = rk3399_spi_set_clk(priv->cru, clk->id, rate);
1034 break;
Philipp Tomsich10b594b2017-04-28 18:33:57 +02001035 case PCLK_HDMI_CTRL:
1036 case PCLK_VIO_GRF:
1037 /* the PCLK gates for video are enabled by default */
1038 break;
Kever Yangca19eac2016-07-29 10:35:25 +08001039 case DCLK_VOP0:
1040 case DCLK_VOP1:
Kever Yange54d26a2016-08-12 17:47:15 +08001041 ret = rk3399_vop_set_clk(priv->cru, clk->id, rate);
Kever Yangca19eac2016-07-29 10:35:25 +08001042 break;
Jagan Teki99f0f822020-04-02 17:11:21 +05301043 case ACLK_VOP1:
1044 case HCLK_VOP1:
Jagan Teki3f26bce2020-04-28 15:30:16 +05301045 case HCLK_SD:
Jagan Teki4fb2c6d2020-05-26 11:32:06 +08001046 case SCLK_UPHY0_TCPDCORE:
1047 case SCLK_UPHY1_TCPDCORE:
Jagan Teki99f0f822020-04-02 17:11:21 +05301048 /**
1049 * assigned-clocks handling won't require for vopl, so
1050 * return 0 to satisfy clk_set_defaults during device probe.
1051 */
1052 return 0;
Kever Yange1980532017-02-13 17:38:56 +08001053 case SCLK_DDRCLK:
1054 ret = rk3399_ddr_set_clk(priv->cru, rate);
1055 break;
Philipp Tomsichd10b45e2017-04-28 17:11:55 +02001056 case PCLK_EFUSE1024NS:
1057 break;
David Wuf91b9b42017-09-20 14:38:58 +08001058 case SCLK_SARADC:
1059 ret = rk3399_saradc_set_clk(priv->cru, rate);
1060 break;
Simon Glassd27b3172019-01-21 14:53:30 -07001061 case ACLK_VIO:
1062 case ACLK_HDCP:
1063 case ACLK_GIC_PRE:
1064 case PCLK_DDR:
1065 return 0;
Kever Yangca19eac2016-07-29 10:35:25 +08001066 default:
Simon Glassd27b3172019-01-21 14:53:30 -07001067 log_debug("Unknown clock %lu\n", clk->id);
Kever Yangca19eac2016-07-29 10:35:25 +08001068 return -ENOENT;
1069 }
1070
1071 return ret;
1072}
1073
Jagan Tekibef02a32019-07-15 23:51:10 +05301074static int __maybe_unused rk3399_gmac_set_parent(struct clk *clk,
1075 struct clk *parent)
Philipp Tomsichf4ba6ed2018-01-08 13:11:01 +01001076{
1077 struct rk3399_clk_priv *priv = dev_get_priv(clk->dev);
1078 const char *clock_output_name;
1079 int ret;
1080
1081 /*
1082 * If the requested parent is in the same clock-controller and
1083 * the id is SCLK_MAC ("clk_gmac"), switch to the internal clock.
1084 */
Jagan Tekibef02a32019-07-15 23:51:10 +05301085 if (parent->dev == clk->dev && parent->id == SCLK_MAC) {
Philipp Tomsichf4ba6ed2018-01-08 13:11:01 +01001086 debug("%s: switching RGMII to SCLK_MAC\n", __func__);
1087 rk_clrreg(&priv->cru->clksel_con[19], BIT(4));
1088 return 0;
1089 }
1090
1091 /*
1092 * Otherwise, we need to check the clock-output-names of the
1093 * requested parent to see if the requested id is "clkin_gmac".
1094 */
1095 ret = dev_read_string_index(parent->dev, "clock-output-names",
1096 parent->id, &clock_output_name);
1097 if (ret < 0)
1098 return -ENODATA;
1099
1100 /* If this is "clkin_gmac", switch to the external clock input */
1101 if (!strcmp(clock_output_name, "clkin_gmac")) {
1102 debug("%s: switching RGMII to CLKIN\n", __func__);
1103 rk_setreg(&priv->cru->clksel_con[19], BIT(4));
1104 return 0;
1105 }
1106
1107 return -EINVAL;
1108}
1109
Jagan Tekibef02a32019-07-15 23:51:10 +05301110static int __maybe_unused rk3399_clk_set_parent(struct clk *clk,
1111 struct clk *parent)
Philipp Tomsichf4ba6ed2018-01-08 13:11:01 +01001112{
1113 switch (clk->id) {
1114 case SCLK_RMII_SRC:
1115 return rk3399_gmac_set_parent(clk, parent);
1116 }
1117
1118 debug("%s: unsupported clk %ld\n", __func__, clk->id);
1119 return -ENOENT;
1120}
1121
Jagan Teki6c7f14a2020-05-09 22:26:19 +05301122static int rk3399_clk_enable(struct clk *clk)
1123{
1124 struct rk3399_clk_priv *priv = dev_get_priv(clk->dev);
1125
1126 switch (clk->id) {
1127 case SCLK_MAC:
1128 rk_clrreg(&priv->cru->clkgate_con[5], BIT(5));
1129 break;
1130 case SCLK_MAC_RX:
1131 rk_clrreg(&priv->cru->clkgate_con[5], BIT(8));
1132 break;
1133 case SCLK_MAC_TX:
1134 rk_clrreg(&priv->cru->clkgate_con[5], BIT(9));
1135 break;
1136 case SCLK_MACREF:
1137 rk_clrreg(&priv->cru->clkgate_con[5], BIT(7));
1138 break;
1139 case SCLK_MACREF_OUT:
1140 rk_clrreg(&priv->cru->clkgate_con[5], BIT(6));
1141 break;
Jagan Tekia5915372020-05-26 11:32:05 +08001142 case SCLK_USB2PHY0_REF:
1143 rk_clrreg(&priv->cru->clkgate_con[6], BIT(5));
1144 break;
1145 case SCLK_USB2PHY1_REF:
1146 rk_clrreg(&priv->cru->clkgate_con[6], BIT(6));
1147 break;
Jagan Teki6c7f14a2020-05-09 22:26:19 +05301148 case ACLK_GMAC:
1149 rk_clrreg(&priv->cru->clkgate_con[32], BIT(0));
1150 break;
1151 case PCLK_GMAC:
1152 rk_clrreg(&priv->cru->clkgate_con[32], BIT(2));
1153 break;
1154 case SCLK_USB3OTG0_REF:
1155 rk_clrreg(&priv->cru->clkgate_con[12], BIT(1));
1156 break;
1157 case SCLK_USB3OTG1_REF:
1158 rk_clrreg(&priv->cru->clkgate_con[12], BIT(2));
1159 break;
1160 case SCLK_USB3OTG0_SUSPEND:
1161 rk_clrreg(&priv->cru->clkgate_con[12], BIT(3));
1162 break;
1163 case SCLK_USB3OTG1_SUSPEND:
1164 rk_clrreg(&priv->cru->clkgate_con[12], BIT(4));
1165 break;
1166 case ACLK_USB3OTG0:
1167 rk_clrreg(&priv->cru->clkgate_con[30], BIT(1));
1168 break;
1169 case ACLK_USB3OTG1:
1170 rk_clrreg(&priv->cru->clkgate_con[30], BIT(2));
1171 break;
1172 case ACLK_USB3_RKSOC_AXI_PERF:
1173 rk_clrreg(&priv->cru->clkgate_con[30], BIT(3));
1174 break;
1175 case ACLK_USB3:
1176 rk_clrreg(&priv->cru->clkgate_con[12], BIT(0));
1177 break;
1178 case ACLK_USB3_GRF:
1179 rk_clrreg(&priv->cru->clkgate_con[30], BIT(4));
1180 break;
1181 case HCLK_HOST0:
1182 rk_clrreg(&priv->cru->clksel_con[20], BIT(5));
1183 break;
1184 case HCLK_HOST0_ARB:
1185 rk_clrreg(&priv->cru->clksel_con[20], BIT(6));
1186 break;
1187 case HCLK_HOST1:
1188 rk_clrreg(&priv->cru->clksel_con[20], BIT(7));
1189 break;
1190 case HCLK_HOST1_ARB:
1191 rk_clrreg(&priv->cru->clksel_con[20], BIT(8));
1192 break;
Jagan Teki055c2182020-05-26 11:32:07 +08001193 case SCLK_UPHY0_TCPDPHY_REF:
1194 rk_clrreg(&priv->cru->clkgate_con[13], BIT(4));
1195 break;
1196 case SCLK_UPHY0_TCPDCORE:
1197 rk_clrreg(&priv->cru->clkgate_con[13], BIT(5));
1198 break;
1199 case SCLK_UPHY1_TCPDPHY_REF:
1200 rk_clrreg(&priv->cru->clkgate_con[13], BIT(6));
1201 break;
1202 case SCLK_UPHY1_TCPDCORE:
1203 rk_clrreg(&priv->cru->clkgate_con[13], BIT(7));
1204 break;
Jagan Teki70c54ee2020-05-09 22:26:20 +05301205 case SCLK_PCIEPHY_REF:
1206 rk_clrreg(&priv->cru->clksel_con[18], BIT(10));
1207 break;
Jagan Teki6c7f14a2020-05-09 22:26:19 +05301208 default:
1209 debug("%s: unsupported clk %ld\n", __func__, clk->id);
1210 return -ENOENT;
1211 }
1212
1213 return 0;
1214}
1215
1216static int rk3399_clk_disable(struct clk *clk)
1217{
1218 struct rk3399_clk_priv *priv = dev_get_priv(clk->dev);
1219
1220 switch (clk->id) {
1221 case SCLK_MAC:
1222 rk_setreg(&priv->cru->clkgate_con[5], BIT(5));
1223 break;
1224 case SCLK_MAC_RX:
1225 rk_setreg(&priv->cru->clkgate_con[5], BIT(8));
1226 break;
1227 case SCLK_MAC_TX:
1228 rk_setreg(&priv->cru->clkgate_con[5], BIT(9));
1229 break;
1230 case SCLK_MACREF:
1231 rk_setreg(&priv->cru->clkgate_con[5], BIT(7));
1232 break;
1233 case SCLK_MACREF_OUT:
1234 rk_setreg(&priv->cru->clkgate_con[5], BIT(6));
1235 break;
Jagan Tekia5915372020-05-26 11:32:05 +08001236 case SCLK_USB2PHY0_REF:
1237 rk_setreg(&priv->cru->clkgate_con[6], BIT(5));
1238 break;
1239 case SCLK_USB2PHY1_REF:
1240 rk_setreg(&priv->cru->clkgate_con[6], BIT(6));
1241 break;
Jagan Teki6c7f14a2020-05-09 22:26:19 +05301242 case ACLK_GMAC:
1243 rk_setreg(&priv->cru->clkgate_con[32], BIT(0));
1244 break;
1245 case PCLK_GMAC:
1246 rk_setreg(&priv->cru->clkgate_con[32], BIT(2));
1247 break;
1248 case SCLK_USB3OTG0_REF:
1249 rk_setreg(&priv->cru->clkgate_con[12], BIT(1));
1250 break;
1251 case SCLK_USB3OTG1_REF:
1252 rk_setreg(&priv->cru->clkgate_con[12], BIT(2));
1253 break;
1254 case SCLK_USB3OTG0_SUSPEND:
1255 rk_setreg(&priv->cru->clkgate_con[12], BIT(3));
1256 break;
1257 case SCLK_USB3OTG1_SUSPEND:
1258 rk_setreg(&priv->cru->clkgate_con[12], BIT(4));
1259 break;
1260 case ACLK_USB3OTG0:
1261 rk_setreg(&priv->cru->clkgate_con[30], BIT(1));
1262 break;
1263 case ACLK_USB3OTG1:
1264 rk_setreg(&priv->cru->clkgate_con[30], BIT(2));
1265 break;
1266 case ACLK_USB3_RKSOC_AXI_PERF:
1267 rk_setreg(&priv->cru->clkgate_con[30], BIT(3));
1268 break;
1269 case ACLK_USB3:
1270 rk_setreg(&priv->cru->clkgate_con[12], BIT(0));
1271 break;
1272 case ACLK_USB3_GRF:
1273 rk_setreg(&priv->cru->clkgate_con[30], BIT(4));
1274 break;
1275 case HCLK_HOST0:
1276 rk_setreg(&priv->cru->clksel_con[20], BIT(5));
1277 break;
1278 case HCLK_HOST0_ARB:
1279 rk_setreg(&priv->cru->clksel_con[20], BIT(6));
1280 break;
1281 case HCLK_HOST1:
1282 rk_setreg(&priv->cru->clksel_con[20], BIT(7));
1283 break;
1284 case HCLK_HOST1_ARB:
1285 rk_setreg(&priv->cru->clksel_con[20], BIT(8));
1286 break;
Jagan Teki055c2182020-05-26 11:32:07 +08001287 case SCLK_UPHY0_TCPDPHY_REF:
1288 rk_setreg(&priv->cru->clkgate_con[13], BIT(4));
1289 break;
1290 case SCLK_UPHY0_TCPDCORE:
1291 rk_setreg(&priv->cru->clkgate_con[13], BIT(5));
1292 break;
1293 case SCLK_UPHY1_TCPDPHY_REF:
1294 rk_setreg(&priv->cru->clkgate_con[13], BIT(6));
1295 break;
1296 case SCLK_UPHY1_TCPDCORE:
1297 rk_setreg(&priv->cru->clkgate_con[13], BIT(7));
1298 break;
Jagan Teki70c54ee2020-05-09 22:26:20 +05301299 case SCLK_PCIEPHY_REF:
1300 rk_clrreg(&priv->cru->clksel_con[18], BIT(10));
1301 break;
Jagan Teki6c7f14a2020-05-09 22:26:19 +05301302 default:
1303 debug("%s: unsupported clk %ld\n", __func__, clk->id);
1304 return -ENOENT;
1305 }
1306
1307 return 0;
1308}
1309
Kever Yangca19eac2016-07-29 10:35:25 +08001310static struct clk_ops rk3399_clk_ops = {
1311 .get_rate = rk3399_clk_get_rate,
1312 .set_rate = rk3399_clk_set_rate,
Simon Glass3580f6d2021-08-07 07:24:03 -06001313#if CONFIG_IS_ENABLED(OF_REAL)
Philipp Tomsichf4ba6ed2018-01-08 13:11:01 +01001314 .set_parent = rk3399_clk_set_parent,
Philipp Tomsich6dd2fb42018-01-25 15:27:10 +01001315#endif
Jagan Teki6c7f14a2020-05-09 22:26:19 +05301316 .enable = rk3399_clk_enable,
1317 .disable = rk3399_clk_disable,
Kever Yangca19eac2016-07-29 10:35:25 +08001318};
1319
Jagan Teki783acfd2020-01-09 14:22:17 +05301320static void rkclk_init(struct rockchip_cru *cru)
Kever Yang05a14b02017-10-12 15:27:29 +08001321{
1322 u32 aclk_div;
1323 u32 hclk_div;
1324 u32 pclk_div;
1325
Christoph Muellner25c7ba92018-11-30 20:32:48 +01001326 rk3399_configure_cpu_l(cru, APLL_L_600_MHZ);
1327 rk3399_configure_cpu_b(cru, APLL_B_600_MHZ);
Kever Yang05a14b02017-10-12 15:27:29 +08001328 /*
1329 * some cru registers changed by bootrom, we'd better reset them to
1330 * reset/default values described in TRM to avoid confusion in kernel.
1331 * Please consider these three lines as a fix of bootrom bug.
1332 */
1333 rk_clrsetreg(&cru->clksel_con[12], 0xffff, 0x4101);
1334 rk_clrsetreg(&cru->clksel_con[19], 0xffff, 0x033f);
1335 rk_clrsetreg(&cru->clksel_con[56], 0x0003, 0x0003);
1336
1337 /* configure gpll cpll */
1338 rkclk_set_pll(&cru->gpll_con[0], &gpll_init_cfg);
1339 rkclk_set_pll(&cru->cpll_con[0], &cpll_init_cfg);
1340
1341 /* configure perihp aclk, hclk, pclk */
1342 aclk_div = GPLL_HZ / PERIHP_ACLK_HZ - 1;
1343 assert((aclk_div + 1) * PERIHP_ACLK_HZ == GPLL_HZ && aclk_div < 0x1f);
1344
1345 hclk_div = PERIHP_ACLK_HZ / PERIHP_HCLK_HZ - 1;
1346 assert((hclk_div + 1) * PERIHP_HCLK_HZ ==
1347 PERIHP_ACLK_HZ && (hclk_div < 0x4));
1348
1349 pclk_div = PERIHP_ACLK_HZ / PERIHP_PCLK_HZ - 1;
1350 assert((pclk_div + 1) * PERIHP_PCLK_HZ ==
1351 PERIHP_ACLK_HZ && (pclk_div < 0x7));
1352
1353 rk_clrsetreg(&cru->clksel_con[14],
1354 PCLK_PERIHP_DIV_CON_MASK | HCLK_PERIHP_DIV_CON_MASK |
1355 ACLK_PERIHP_PLL_SEL_MASK | ACLK_PERIHP_DIV_CON_MASK,
1356 pclk_div << PCLK_PERIHP_DIV_CON_SHIFT |
1357 hclk_div << HCLK_PERIHP_DIV_CON_SHIFT |
1358 ACLK_PERIHP_PLL_SEL_GPLL << ACLK_PERIHP_PLL_SEL_SHIFT |
1359 aclk_div << ACLK_PERIHP_DIV_CON_SHIFT);
1360
1361 /* configure perilp0 aclk, hclk, pclk */
1362 aclk_div = GPLL_HZ / PERILP0_ACLK_HZ - 1;
1363 assert((aclk_div + 1) * PERILP0_ACLK_HZ == GPLL_HZ && aclk_div < 0x1f);
1364
1365 hclk_div = PERILP0_ACLK_HZ / PERILP0_HCLK_HZ - 1;
1366 assert((hclk_div + 1) * PERILP0_HCLK_HZ ==
1367 PERILP0_ACLK_HZ && (hclk_div < 0x4));
1368
1369 pclk_div = PERILP0_ACLK_HZ / PERILP0_PCLK_HZ - 1;
1370 assert((pclk_div + 1) * PERILP0_PCLK_HZ ==
1371 PERILP0_ACLK_HZ && (pclk_div < 0x7));
1372
1373 rk_clrsetreg(&cru->clksel_con[23],
1374 PCLK_PERILP0_DIV_CON_MASK | HCLK_PERILP0_DIV_CON_MASK |
1375 ACLK_PERILP0_PLL_SEL_MASK | ACLK_PERILP0_DIV_CON_MASK,
1376 pclk_div << PCLK_PERILP0_DIV_CON_SHIFT |
1377 hclk_div << HCLK_PERILP0_DIV_CON_SHIFT |
1378 ACLK_PERILP0_PLL_SEL_GPLL << ACLK_PERILP0_PLL_SEL_SHIFT |
1379 aclk_div << ACLK_PERILP0_DIV_CON_SHIFT);
1380
1381 /* perilp1 hclk select gpll as source */
1382 hclk_div = GPLL_HZ / PERILP1_HCLK_HZ - 1;
1383 assert((hclk_div + 1) * PERILP1_HCLK_HZ ==
1384 GPLL_HZ && (hclk_div < 0x1f));
1385
1386 pclk_div = PERILP1_HCLK_HZ / PERILP1_HCLK_HZ - 1;
1387 assert((pclk_div + 1) * PERILP1_HCLK_HZ ==
1388 PERILP1_HCLK_HZ && (hclk_div < 0x7));
1389
1390 rk_clrsetreg(&cru->clksel_con[25],
1391 PCLK_PERILP1_DIV_CON_MASK | HCLK_PERILP1_DIV_CON_MASK |
1392 HCLK_PERILP1_PLL_SEL_MASK,
1393 pclk_div << PCLK_PERILP1_DIV_CON_SHIFT |
1394 hclk_div << HCLK_PERILP1_DIV_CON_SHIFT |
1395 HCLK_PERILP1_PLL_SEL_GPLL << HCLK_PERILP1_PLL_SEL_SHIFT);
1396}
Kever Yang05a14b02017-10-12 15:27:29 +08001397
Kever Yangca19eac2016-07-29 10:35:25 +08001398static int rk3399_clk_probe(struct udevice *dev)
1399{
1400 struct rk3399_clk_priv *priv = dev_get_priv(dev);
Alper Nebi Yasak66327ce2020-10-28 00:15:10 +03001401 bool init_clocks = false;
Kever Yangca19eac2016-07-29 10:35:25 +08001402
Kever Yange1980532017-02-13 17:38:56 +08001403#if CONFIG_IS_ENABLED(OF_PLATDATA)
Simon Glassfa20e932020-12-03 16:55:20 -07001404 struct rk3399_clk_plat *plat = dev_get_plat(dev);
Kever Yangca19eac2016-07-29 10:35:25 +08001405
Simon Glass1b1fe412017-08-29 14:15:50 -06001406 priv->cru = map_sysmem(plat->dtd.reg[0], plat->dtd.reg[1]);
Kever Yange1980532017-02-13 17:38:56 +08001407#endif
Alper Nebi Yasak66327ce2020-10-28 00:15:10 +03001408
1409#if defined(CONFIG_SPL_BUILD)
1410 init_clocks = true;
1411#elif CONFIG_IS_ENABLED(HANDOFF)
1412 if (!(gd->flags & GD_FLG_RELOC)) {
1413 if (!(gd->spl_handoff))
1414 init_clocks = true;
1415 }
Kever Yange1980532017-02-13 17:38:56 +08001416#endif
Alper Nebi Yasak66327ce2020-10-28 00:15:10 +03001417
1418 if (init_clocks)
1419 rkclk_init(priv->cru);
1420
Kever Yangca19eac2016-07-29 10:35:25 +08001421 return 0;
1422}
1423
Simon Glassaad29ae2020-12-03 16:55:21 -07001424static int rk3399_clk_of_to_plat(struct udevice *dev)
Kever Yangca19eac2016-07-29 10:35:25 +08001425{
Simon Glass6d70ba02021-08-07 07:24:06 -06001426 if (CONFIG_IS_ENABLED(OF_REAL)) {
1427 struct rk3399_clk_priv *priv = dev_get_priv(dev);
Kever Yangca19eac2016-07-29 10:35:25 +08001428
Simon Glass6d70ba02021-08-07 07:24:06 -06001429 priv->cru = dev_read_addr_ptr(dev);
1430 }
1431
Kever Yangca19eac2016-07-29 10:35:25 +08001432 return 0;
1433}
1434
1435static int rk3399_clk_bind(struct udevice *dev)
1436{
1437 int ret;
Kever Yang4fbb6c22017-11-03 15:16:13 +08001438 struct udevice *sys_child;
1439 struct sysreset_reg *priv;
Kever Yangca19eac2016-07-29 10:35:25 +08001440
1441 /* The reset driver does not have a device node, so bind it here */
Kever Yang4fbb6c22017-11-03 15:16:13 +08001442 ret = device_bind_driver(dev, "rockchip_sysreset", "sysreset",
1443 &sys_child);
1444 if (ret) {
1445 debug("Warning: No sysreset driver: ret=%d\n", ret);
1446 } else {
1447 priv = malloc(sizeof(struct sysreset_reg));
Jagan Teki783acfd2020-01-09 14:22:17 +05301448 priv->glb_srst_fst_value = offsetof(struct rockchip_cru,
Kever Yang4fbb6c22017-11-03 15:16:13 +08001449 glb_srst_fst_value);
Jagan Teki783acfd2020-01-09 14:22:17 +05301450 priv->glb_srst_snd_value = offsetof(struct rockchip_cru,
Kever Yang4fbb6c22017-11-03 15:16:13 +08001451 glb_srst_snd_value);
Simon Glass95588622020-12-22 19:30:28 -07001452 dev_set_priv(sys_child, priv);
Kever Yang4fbb6c22017-11-03 15:16:13 +08001453 }
Kever Yangca19eac2016-07-29 10:35:25 +08001454
Heiko Stuebner416f8d32019-11-09 00:06:30 +01001455#if CONFIG_IS_ENABLED(RESET_ROCKCHIP)
Jagan Teki783acfd2020-01-09 14:22:17 +05301456 ret = offsetof(struct rockchip_cru, softrst_con[0]);
Elaine Zhang432976f2017-12-19 18:22:38 +08001457 ret = rockchip_reset_bind(dev, ret, 21);
1458 if (ret)
Eugen Hristevf1798262023-04-11 10:17:56 +03001459 debug("Warning: software reset driver bind failed\n");
Elaine Zhang432976f2017-12-19 18:22:38 +08001460#endif
1461
Kever Yangca19eac2016-07-29 10:35:25 +08001462 return 0;
1463}
1464
1465static const struct udevice_id rk3399_clk_ids[] = {
1466 { .compatible = "rockchip,rk3399-cru" },
1467 { }
1468};
1469
1470U_BOOT_DRIVER(clk_rk3399) = {
Kever Yange1980532017-02-13 17:38:56 +08001471 .name = "rockchip_rk3399_cru",
Kever Yangca19eac2016-07-29 10:35:25 +08001472 .id = UCLASS_CLK,
1473 .of_match = rk3399_clk_ids,
Simon Glass8a2b47f2020-12-03 16:55:17 -07001474 .priv_auto = sizeof(struct rk3399_clk_priv),
Simon Glassaad29ae2020-12-03 16:55:21 -07001475 .of_to_plat = rk3399_clk_of_to_plat,
Kever Yangca19eac2016-07-29 10:35:25 +08001476 .ops = &rk3399_clk_ops,
1477 .bind = rk3399_clk_bind,
1478 .probe = rk3399_clk_probe,
Kever Yange1980532017-02-13 17:38:56 +08001479#if CONFIG_IS_ENABLED(OF_PLATDATA)
Simon Glass71fa5b42020-12-03 16:55:18 -07001480 .plat_auto = sizeof(struct rk3399_clk_plat),
Kever Yange1980532017-02-13 17:38:56 +08001481#endif
Kever Yangca19eac2016-07-29 10:35:25 +08001482};
Kever Yange54d26a2016-08-12 17:47:15 +08001483
1484static ulong rk3399_i2c_get_pmuclk(struct rk3399_pmucru *pmucru, ulong clk_id)
1485{
1486 u32 div, con;
1487
1488 switch (clk_id) {
1489 case SCLK_I2C0_PMU:
1490 con = readl(&pmucru->pmucru_clksel[2]);
1491 div = I2C_CLK_DIV_VALUE(con, 0);
1492 break;
1493 case SCLK_I2C4_PMU:
1494 con = readl(&pmucru->pmucru_clksel[3]);
1495 div = I2C_CLK_DIV_VALUE(con, 4);
1496 break;
1497 case SCLK_I2C8_PMU:
1498 con = readl(&pmucru->pmucru_clksel[2]);
1499 div = I2C_CLK_DIV_VALUE(con, 8);
1500 break;
1501 default:
1502 printf("do not support this i2c bus\n");
1503 return -EINVAL;
1504 }
1505
1506 return DIV_TO_RATE(PPLL_HZ, div);
1507}
1508
1509static ulong rk3399_i2c_set_pmuclk(struct rk3399_pmucru *pmucru, ulong clk_id,
1510 uint hz)
1511{
1512 int src_clk_div;
1513
1514 src_clk_div = PPLL_HZ / hz;
1515 assert(src_clk_div - 1 < 127);
1516
1517 switch (clk_id) {
1518 case SCLK_I2C0_PMU:
1519 rk_clrsetreg(&pmucru->pmucru_clksel[2], I2C_PMUCLK_REG_MASK(0),
1520 I2C_PMUCLK_REG_VALUE(0, src_clk_div));
1521 break;
1522 case SCLK_I2C4_PMU:
1523 rk_clrsetreg(&pmucru->pmucru_clksel[3], I2C_PMUCLK_REG_MASK(4),
1524 I2C_PMUCLK_REG_VALUE(4, src_clk_div));
1525 break;
1526 case SCLK_I2C8_PMU:
1527 rk_clrsetreg(&pmucru->pmucru_clksel[2], I2C_PMUCLK_REG_MASK(8),
1528 I2C_PMUCLK_REG_VALUE(8, src_clk_div));
1529 break;
1530 default:
1531 printf("do not support this i2c bus\n");
1532 return -EINVAL;
1533 }
1534
1535 return DIV_TO_RATE(PPLL_HZ, src_clk_div);
1536}
1537
1538static ulong rk3399_pwm_get_clk(struct rk3399_pmucru *pmucru)
1539{
1540 u32 div, con;
1541
1542 /* PWM closk rate is same as pclk_pmu */
1543 con = readl(&pmucru->pmucru_clksel[0]);
1544 div = con & PMU_PCLK_DIV_CON_MASK;
1545
1546 return DIV_TO_RATE(PPLL_HZ, div);
1547}
1548
1549static ulong rk3399_pmuclk_get_rate(struct clk *clk)
1550{
1551 struct rk3399_pmuclk_priv *priv = dev_get_priv(clk->dev);
1552 ulong rate = 0;
1553
1554 switch (clk->id) {
Philipp Tomsich932908c2018-02-23 17:36:41 +01001555 case PLL_PPLL:
1556 return PPLL_HZ;
Kever Yange54d26a2016-08-12 17:47:15 +08001557 case PCLK_RKPWM_PMU:
Jack Mitchell4ef38ce2020-09-17 10:42:06 +01001558 case PCLK_WDT_M0_PMU:
Kever Yange54d26a2016-08-12 17:47:15 +08001559 rate = rk3399_pwm_get_clk(priv->pmucru);
1560 break;
1561 case SCLK_I2C0_PMU:
1562 case SCLK_I2C4_PMU:
1563 case SCLK_I2C8_PMU:
1564 rate = rk3399_i2c_get_pmuclk(priv->pmucru, clk->id);
1565 break;
1566 default:
1567 return -ENOENT;
1568 }
1569
1570 return rate;
1571}
1572
1573static ulong rk3399_pmuclk_set_rate(struct clk *clk, ulong rate)
1574{
1575 struct rk3399_pmuclk_priv *priv = dev_get_priv(clk->dev);
1576 ulong ret = 0;
1577
1578 switch (clk->id) {
Philipp Tomsich932908c2018-02-23 17:36:41 +01001579 case PLL_PPLL:
1580 /*
1581 * This has already been set up and we don't want/need
1582 * to change it here. Accept the request though, as the
1583 * device-tree has this in an 'assigned-clocks' list.
1584 */
1585 return PPLL_HZ;
Kever Yange54d26a2016-08-12 17:47:15 +08001586 case SCLK_I2C0_PMU:
1587 case SCLK_I2C4_PMU:
1588 case SCLK_I2C8_PMU:
1589 ret = rk3399_i2c_set_pmuclk(priv->pmucru, clk->id, rate);
1590 break;
1591 default:
1592 return -ENOENT;
1593 }
1594
1595 return ret;
1596}
1597
1598static struct clk_ops rk3399_pmuclk_ops = {
1599 .get_rate = rk3399_pmuclk_get_rate,
1600 .set_rate = rk3399_pmuclk_set_rate,
1601};
1602
Kever Yange1980532017-02-13 17:38:56 +08001603#ifndef CONFIG_SPL_BUILD
Kever Yange54d26a2016-08-12 17:47:15 +08001604static void pmuclk_init(struct rk3399_pmucru *pmucru)
1605{
1606 u32 pclk_div;
1607
1608 /* configure pmu pll(ppll) */
1609 rkclk_set_pll(&pmucru->ppll_con[0], &ppll_init_cfg);
1610
1611 /* configure pmu pclk */
1612 pclk_div = PPLL_HZ / PMU_PCLK_HZ - 1;
Kever Yange54d26a2016-08-12 17:47:15 +08001613 rk_clrsetreg(&pmucru->pmucru_clksel[0],
1614 PMU_PCLK_DIV_CON_MASK,
1615 pclk_div << PMU_PCLK_DIV_CON_SHIFT);
1616}
Kever Yange1980532017-02-13 17:38:56 +08001617#endif
Kever Yange54d26a2016-08-12 17:47:15 +08001618
1619static int rk3399_pmuclk_probe(struct udevice *dev)
1620{
Philipp Tomsichcf0a4ba2017-03-24 19:24:24 +01001621#if CONFIG_IS_ENABLED(OF_PLATDATA) || !defined(CONFIG_SPL_BUILD)
Kever Yange54d26a2016-08-12 17:47:15 +08001622 struct rk3399_pmuclk_priv *priv = dev_get_priv(dev);
Philipp Tomsichcf0a4ba2017-03-24 19:24:24 +01001623#endif
Kever Yange54d26a2016-08-12 17:47:15 +08001624
Kever Yange1980532017-02-13 17:38:56 +08001625#if CONFIG_IS_ENABLED(OF_PLATDATA)
Simon Glassfa20e932020-12-03 16:55:20 -07001626 struct rk3399_pmuclk_plat *plat = dev_get_plat(dev);
Kever Yange54d26a2016-08-12 17:47:15 +08001627
Simon Glass1b1fe412017-08-29 14:15:50 -06001628 priv->pmucru = map_sysmem(plat->dtd.reg[0], plat->dtd.reg[1]);
Kever Yange1980532017-02-13 17:38:56 +08001629#endif
1630
1631#ifndef CONFIG_SPL_BUILD
1632 pmuclk_init(priv->pmucru);
1633#endif
Kever Yange54d26a2016-08-12 17:47:15 +08001634 return 0;
1635}
1636
Simon Glassaad29ae2020-12-03 16:55:21 -07001637static int rk3399_pmuclk_of_to_plat(struct udevice *dev)
Kever Yange54d26a2016-08-12 17:47:15 +08001638{
Simon Glass6d70ba02021-08-07 07:24:06 -06001639 if (CONFIG_IS_ENABLED(OF_REAL)) {
1640 struct rk3399_pmuclk_priv *priv = dev_get_priv(dev);
Kever Yange54d26a2016-08-12 17:47:15 +08001641
Simon Glass6d70ba02021-08-07 07:24:06 -06001642 priv->pmucru = dev_read_addr_ptr(dev);
1643 }
1644
Kever Yange54d26a2016-08-12 17:47:15 +08001645 return 0;
1646}
1647
Elaine Zhang432976f2017-12-19 18:22:38 +08001648static int rk3399_pmuclk_bind(struct udevice *dev)
1649{
Alper Nebi Yasak5de2f332020-10-05 09:57:29 +03001650#if CONFIG_IS_ENABLED(RESET_ROCKCHIP)
Elaine Zhang432976f2017-12-19 18:22:38 +08001651 int ret;
1652
1653 ret = offsetof(struct rk3399_pmucru, pmucru_softrst_con[0]);
1654 ret = rockchip_reset_bind(dev, ret, 2);
1655 if (ret)
Eugen Hristevf1798262023-04-11 10:17:56 +03001656 debug("Warning: software reset driver bind failed\n");
Elaine Zhang432976f2017-12-19 18:22:38 +08001657#endif
1658 return 0;
1659}
1660
Kever Yange54d26a2016-08-12 17:47:15 +08001661static const struct udevice_id rk3399_pmuclk_ids[] = {
1662 { .compatible = "rockchip,rk3399-pmucru" },
1663 { }
1664};
1665
Simon Glassd1dfea72016-10-01 20:04:51 -06001666U_BOOT_DRIVER(rockchip_rk3399_pmuclk) = {
Kever Yange1980532017-02-13 17:38:56 +08001667 .name = "rockchip_rk3399_pmucru",
Kever Yange54d26a2016-08-12 17:47:15 +08001668 .id = UCLASS_CLK,
1669 .of_match = rk3399_pmuclk_ids,
Simon Glass8a2b47f2020-12-03 16:55:17 -07001670 .priv_auto = sizeof(struct rk3399_pmuclk_priv),
Simon Glassaad29ae2020-12-03 16:55:21 -07001671 .of_to_plat = rk3399_pmuclk_of_to_plat,
Kever Yange54d26a2016-08-12 17:47:15 +08001672 .ops = &rk3399_pmuclk_ops,
1673 .probe = rk3399_pmuclk_probe,
Elaine Zhang432976f2017-12-19 18:22:38 +08001674 .bind = rk3399_pmuclk_bind,
Kever Yange1980532017-02-13 17:38:56 +08001675#if CONFIG_IS_ENABLED(OF_PLATDATA)
Simon Glass71fa5b42020-12-03 16:55:18 -07001676 .plat_auto = sizeof(struct rk3399_pmuclk_plat),
Kever Yange1980532017-02-13 17:38:56 +08001677#endif
Kever Yange54d26a2016-08-12 17:47:15 +08001678};