blob: 24cefebd1b2a6eabb462ff51726e9ce7c0dda32c [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
Kever Yangca19eac2016-07-29 10:35:25 +08007#include <clk-uclass.h>
8#include <dm.h>
Kever Yange1980532017-02-13 17:38:56 +08009#include <dt-structs.h>
Kever Yangca19eac2016-07-29 10:35:25 +080010#include <errno.h>
Simon Glass0f2af882020-05-10 11:40:05 -060011#include <log.h>
Simon Glass9bc15642020-02-03 07:36:16 -070012#include <malloc.h>
Kever Yange1980532017-02-13 17:38:56 +080013#include <mapmem.h>
Kever Yangca19eac2016-07-29 10:35:25 +080014#include <syscon.h>
David Wuf91b9b42017-09-20 14:38:58 +080015#include <bitfield.h>
Kever Yang9fbe17c2019-03-28 11:01:23 +080016#include <asm/arch-rockchip/clock.h>
Jagan Teki783acfd2020-01-09 14:22:17 +053017#include <asm/arch-rockchip/cru.h>
Kever Yang9fbe17c2019-03-28 11:01:23 +080018#include <asm/arch-rockchip/hardware.h>
Simon Glass3ba929a2020-10-30 21:38:53 -060019#include <asm/global_data.h>
Simon Glass95588622020-12-22 19:30:28 -070020#include <dm/device-internal.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>
Simon Glassbdd5f812023-09-14 18:21:46 -060025#include <linux/printk.h>
Kever Yangca19eac2016-07-29 10:35:25 +080026
Alper Nebi Yasak66327ce2020-10-28 00:15:10 +030027DECLARE_GLOBAL_DATA_PTR;
28
Kever Yange1980532017-02-13 17:38:56 +080029#if CONFIG_IS_ENABLED(OF_PLATDATA)
30struct rk3399_clk_plat {
31 struct dtd_rockchip_rk3399_cru dtd;
Kever Yange54d26a2016-08-12 17:47:15 +080032};
33
Kever Yange1980532017-02-13 17:38:56 +080034struct rk3399_pmuclk_plat {
35 struct dtd_rockchip_rk3399_pmucru dtd;
36};
37#endif
38
Kever Yangca19eac2016-07-29 10:35:25 +080039struct pll_div {
40 u32 refdiv;
41 u32 fbdiv;
42 u32 postdiv1;
43 u32 postdiv2;
44 u32 frac;
45};
46
47#define RATE_TO_DIV(input_rate, output_rate) \
Jagan Tekibef02a32019-07-15 23:51:10 +053048 ((input_rate) / (output_rate) - 1)
49#define DIV_TO_RATE(input_rate, div) ((input_rate) / ((div) + 1))
Kever Yangca19eac2016-07-29 10:35:25 +080050
51#define PLL_DIVISORS(hz, _refdiv, _postdiv1, _postdiv2) {\
52 .refdiv = _refdiv,\
53 .fbdiv = (u32)((u64)hz * _refdiv * _postdiv1 * _postdiv2 / OSC_HZ),\
54 .postdiv1 = _postdiv1, .postdiv2 = _postdiv2};
55
56static const struct pll_div gpll_init_cfg = PLL_DIVISORS(GPLL_HZ, 2, 2, 1);
57static const struct pll_div cpll_init_cfg = PLL_DIVISORS(CPLL_HZ, 1, 2, 2);
Alper Nebi Yasak66327ce2020-10-28 00:15:10 +030058#if !defined(CONFIG_SPL_BUILD)
Kever Yangca19eac2016-07-29 10:35:25 +080059static const struct pll_div ppll_init_cfg = PLL_DIVISORS(PPLL_HZ, 2, 2, 1);
Philipp Tomsichcf0a4ba2017-03-24 19:24:24 +010060#endif
Kever Yangca19eac2016-07-29 10:35:25 +080061
Jagan Tekibef02a32019-07-15 23:51:10 +053062static const struct pll_div apll_l_1600_cfg = PLL_DIVISORS(1600 * MHz, 3, 1, 1);
63static const struct pll_div apll_l_600_cfg = PLL_DIVISORS(600 * MHz, 1, 2, 1);
Kever Yangca19eac2016-07-29 10:35:25 +080064
65static const struct pll_div *apll_l_cfgs[] = {
66 [APLL_L_1600_MHZ] = &apll_l_1600_cfg,
67 [APLL_L_600_MHZ] = &apll_l_600_cfg,
68};
69
Jagan Tekibef02a32019-07-15 23:51:10 +053070static const struct pll_div apll_b_600_cfg = PLL_DIVISORS(600 * MHz, 1, 2, 1);
Christoph Muellner25c7ba92018-11-30 20:32:48 +010071static const struct pll_div *apll_b_cfgs[] = {
72 [APLL_B_600_MHZ] = &apll_b_600_cfg,
73};
74
Kever Yangca19eac2016-07-29 10:35:25 +080075enum {
76 /* PLL_CON0 */
77 PLL_FBDIV_MASK = 0xfff,
78 PLL_FBDIV_SHIFT = 0,
79
80 /* PLL_CON1 */
81 PLL_POSTDIV2_SHIFT = 12,
82 PLL_POSTDIV2_MASK = 0x7 << PLL_POSTDIV2_SHIFT,
83 PLL_POSTDIV1_SHIFT = 8,
84 PLL_POSTDIV1_MASK = 0x7 << PLL_POSTDIV1_SHIFT,
85 PLL_REFDIV_MASK = 0x3f,
86 PLL_REFDIV_SHIFT = 0,
87
88 /* PLL_CON2 */
89 PLL_LOCK_STATUS_SHIFT = 31,
90 PLL_LOCK_STATUS_MASK = 1 << PLL_LOCK_STATUS_SHIFT,
91 PLL_FRACDIV_MASK = 0xffffff,
92 PLL_FRACDIV_SHIFT = 0,
93
94 /* PLL_CON3 */
95 PLL_MODE_SHIFT = 8,
96 PLL_MODE_MASK = 3 << PLL_MODE_SHIFT,
97 PLL_MODE_SLOW = 0,
98 PLL_MODE_NORM,
99 PLL_MODE_DEEP,
100 PLL_DSMPD_SHIFT = 3,
101 PLL_DSMPD_MASK = 1 << PLL_DSMPD_SHIFT,
102 PLL_INTEGER_MODE = 1,
103
104 /* PMUCRU_CLKSEL_CON0 */
105 PMU_PCLK_DIV_CON_MASK = 0x1f,
106 PMU_PCLK_DIV_CON_SHIFT = 0,
107
108 /* PMUCRU_CLKSEL_CON1 */
109 SPI3_PLL_SEL_SHIFT = 7,
110 SPI3_PLL_SEL_MASK = 1 << SPI3_PLL_SEL_SHIFT,
111 SPI3_PLL_SEL_24M = 0,
112 SPI3_PLL_SEL_PPLL = 1,
113 SPI3_DIV_CON_SHIFT = 0x0,
114 SPI3_DIV_CON_MASK = 0x7f,
115
116 /* PMUCRU_CLKSEL_CON2 */
117 I2C_DIV_CON_MASK = 0x7f,
Kever Yange54d26a2016-08-12 17:47:15 +0800118 CLK_I2C8_DIV_CON_SHIFT = 8,
119 CLK_I2C0_DIV_CON_SHIFT = 0,
Kever Yangca19eac2016-07-29 10:35:25 +0800120
121 /* PMUCRU_CLKSEL_CON3 */
Kever Yange54d26a2016-08-12 17:47:15 +0800122 CLK_I2C4_DIV_CON_SHIFT = 0,
Kever Yangca19eac2016-07-29 10:35:25 +0800123
124 /* CLKSEL_CON0 */
125 ACLKM_CORE_L_DIV_CON_SHIFT = 8,
126 ACLKM_CORE_L_DIV_CON_MASK = 0x1f << ACLKM_CORE_L_DIV_CON_SHIFT,
127 CLK_CORE_L_PLL_SEL_SHIFT = 6,
128 CLK_CORE_L_PLL_SEL_MASK = 3 << CLK_CORE_L_PLL_SEL_SHIFT,
129 CLK_CORE_L_PLL_SEL_ALPLL = 0x0,
130 CLK_CORE_L_PLL_SEL_ABPLL = 0x1,
131 CLK_CORE_L_PLL_SEL_DPLL = 0x10,
132 CLK_CORE_L_PLL_SEL_GPLL = 0x11,
133 CLK_CORE_L_DIV_MASK = 0x1f,
134 CLK_CORE_L_DIV_SHIFT = 0,
135
136 /* CLKSEL_CON1 */
137 PCLK_DBG_L_DIV_SHIFT = 0x8,
138 PCLK_DBG_L_DIV_MASK = 0x1f << PCLK_DBG_L_DIV_SHIFT,
139 ATCLK_CORE_L_DIV_SHIFT = 0,
140 ATCLK_CORE_L_DIV_MASK = 0x1f << ATCLK_CORE_L_DIV_SHIFT,
141
Christoph Muellner25c7ba92018-11-30 20:32:48 +0100142 /* CLKSEL_CON2 */
143 ACLKM_CORE_B_DIV_CON_SHIFT = 8,
144 ACLKM_CORE_B_DIV_CON_MASK = 0x1f << ACLKM_CORE_B_DIV_CON_SHIFT,
145 CLK_CORE_B_PLL_SEL_SHIFT = 6,
146 CLK_CORE_B_PLL_SEL_MASK = 3 << CLK_CORE_B_PLL_SEL_SHIFT,
147 CLK_CORE_B_PLL_SEL_ALPLL = 0x0,
148 CLK_CORE_B_PLL_SEL_ABPLL = 0x1,
149 CLK_CORE_B_PLL_SEL_DPLL = 0x10,
150 CLK_CORE_B_PLL_SEL_GPLL = 0x11,
151 CLK_CORE_B_DIV_MASK = 0x1f,
152 CLK_CORE_B_DIV_SHIFT = 0,
153
154 /* CLKSEL_CON3 */
155 PCLK_DBG_B_DIV_SHIFT = 0x8,
156 PCLK_DBG_B_DIV_MASK = 0x1f << PCLK_DBG_B_DIV_SHIFT,
157 ATCLK_CORE_B_DIV_SHIFT = 0,
158 ATCLK_CORE_B_DIV_MASK = 0x1f << ATCLK_CORE_B_DIV_SHIFT,
159
Kever Yangca19eac2016-07-29 10:35:25 +0800160 /* CLKSEL_CON14 */
161 PCLK_PERIHP_DIV_CON_SHIFT = 12,
162 PCLK_PERIHP_DIV_CON_MASK = 0x7 << PCLK_PERIHP_DIV_CON_SHIFT,
163 HCLK_PERIHP_DIV_CON_SHIFT = 8,
164 HCLK_PERIHP_DIV_CON_MASK = 3 << HCLK_PERIHP_DIV_CON_SHIFT,
165 ACLK_PERIHP_PLL_SEL_SHIFT = 7,
166 ACLK_PERIHP_PLL_SEL_MASK = 1 << ACLK_PERIHP_PLL_SEL_SHIFT,
167 ACLK_PERIHP_PLL_SEL_CPLL = 0,
168 ACLK_PERIHP_PLL_SEL_GPLL = 1,
169 ACLK_PERIHP_DIV_CON_SHIFT = 0,
170 ACLK_PERIHP_DIV_CON_MASK = 0x1f,
171
172 /* CLKSEL_CON21 */
173 ACLK_EMMC_PLL_SEL_SHIFT = 7,
174 ACLK_EMMC_PLL_SEL_MASK = 0x1 << ACLK_EMMC_PLL_SEL_SHIFT,
175 ACLK_EMMC_PLL_SEL_GPLL = 0x1,
176 ACLK_EMMC_DIV_CON_SHIFT = 0,
177 ACLK_EMMC_DIV_CON_MASK = 0x1f,
178
179 /* CLKSEL_CON22 */
180 CLK_EMMC_PLL_SHIFT = 8,
181 CLK_EMMC_PLL_MASK = 0x7 << CLK_EMMC_PLL_SHIFT,
182 CLK_EMMC_PLL_SEL_GPLL = 0x1,
Kever Yangdc850de2016-08-04 11:44:58 +0800183 CLK_EMMC_PLL_SEL_24M = 0x5,
Kever Yangca19eac2016-07-29 10:35:25 +0800184 CLK_EMMC_DIV_CON_SHIFT = 0,
185 CLK_EMMC_DIV_CON_MASK = 0x7f << CLK_EMMC_DIV_CON_SHIFT,
186
187 /* CLKSEL_CON23 */
188 PCLK_PERILP0_DIV_CON_SHIFT = 12,
189 PCLK_PERILP0_DIV_CON_MASK = 0x7 << PCLK_PERILP0_DIV_CON_SHIFT,
190 HCLK_PERILP0_DIV_CON_SHIFT = 8,
191 HCLK_PERILP0_DIV_CON_MASK = 3 << HCLK_PERILP0_DIV_CON_SHIFT,
192 ACLK_PERILP0_PLL_SEL_SHIFT = 7,
193 ACLK_PERILP0_PLL_SEL_MASK = 1 << ACLK_PERILP0_PLL_SEL_SHIFT,
194 ACLK_PERILP0_PLL_SEL_CPLL = 0,
195 ACLK_PERILP0_PLL_SEL_GPLL = 1,
196 ACLK_PERILP0_DIV_CON_SHIFT = 0,
197 ACLK_PERILP0_DIV_CON_MASK = 0x1f,
198
199 /* CLKSEL_CON25 */
200 PCLK_PERILP1_DIV_CON_SHIFT = 8,
201 PCLK_PERILP1_DIV_CON_MASK = 0x7 << PCLK_PERILP1_DIV_CON_SHIFT,
202 HCLK_PERILP1_PLL_SEL_SHIFT = 7,
203 HCLK_PERILP1_PLL_SEL_MASK = 1 << HCLK_PERILP1_PLL_SEL_SHIFT,
204 HCLK_PERILP1_PLL_SEL_CPLL = 0,
205 HCLK_PERILP1_PLL_SEL_GPLL = 1,
206 HCLK_PERILP1_DIV_CON_SHIFT = 0,
207 HCLK_PERILP1_DIV_CON_MASK = 0x1f,
208
209 /* CLKSEL_CON26 */
210 CLK_SARADC_DIV_CON_SHIFT = 8,
David Wuf91b9b42017-09-20 14:38:58 +0800211 CLK_SARADC_DIV_CON_MASK = GENMASK(15, 8),
212 CLK_SARADC_DIV_CON_WIDTH = 8,
Kever Yangca19eac2016-07-29 10:35:25 +0800213
214 /* CLKSEL_CON27 */
215 CLK_TSADC_SEL_X24M = 0x0,
216 CLK_TSADC_SEL_SHIFT = 15,
217 CLK_TSADC_SEL_MASK = 1 << CLK_TSADC_SEL_SHIFT,
218 CLK_TSADC_DIV_CON_SHIFT = 0,
219 CLK_TSADC_DIV_CON_MASK = 0x3ff,
220
221 /* CLKSEL_CON47 & CLKSEL_CON48 */
222 ACLK_VOP_PLL_SEL_SHIFT = 6,
223 ACLK_VOP_PLL_SEL_MASK = 0x3 << ACLK_VOP_PLL_SEL_SHIFT,
224 ACLK_VOP_PLL_SEL_CPLL = 0x1,
225 ACLK_VOP_DIV_CON_SHIFT = 0,
226 ACLK_VOP_DIV_CON_MASK = 0x1f << ACLK_VOP_DIV_CON_SHIFT,
227
228 /* CLKSEL_CON49 & CLKSEL_CON50 */
229 DCLK_VOP_DCLK_SEL_SHIFT = 11,
230 DCLK_VOP_DCLK_SEL_MASK = 1 << DCLK_VOP_DCLK_SEL_SHIFT,
231 DCLK_VOP_DCLK_SEL_DIVOUT = 0,
232 DCLK_VOP_PLL_SEL_SHIFT = 8,
233 DCLK_VOP_PLL_SEL_MASK = 3 << DCLK_VOP_PLL_SEL_SHIFT,
234 DCLK_VOP_PLL_SEL_VPLL = 0,
235 DCLK_VOP_DIV_CON_MASK = 0xff,
236 DCLK_VOP_DIV_CON_SHIFT = 0,
237
Jack Mitchell4ef38ce2020-09-17 10:42:06 +0100238 /* CLKSEL_CON57 */
239 PCLK_ALIVE_DIV_CON_SHIFT = 0,
240 PCLK_ALIVE_DIV_CON_MASK = 0x1f << PCLK_ALIVE_DIV_CON_SHIFT,
241
Kever Yangca19eac2016-07-29 10:35:25 +0800242 /* CLKSEL_CON58 */
Philipp Tomsichc31ee922017-04-20 22:05:49 +0200243 CLK_SPI_PLL_SEL_WIDTH = 1,
244 CLK_SPI_PLL_SEL_MASK = ((1 < CLK_SPI_PLL_SEL_WIDTH) - 1),
245 CLK_SPI_PLL_SEL_CPLL = 0,
246 CLK_SPI_PLL_SEL_GPLL = 1,
247 CLK_SPI_PLL_DIV_CON_WIDTH = 7,
248 CLK_SPI_PLL_DIV_CON_MASK = ((1 << CLK_SPI_PLL_DIV_CON_WIDTH) - 1),
249
250 CLK_SPI5_PLL_DIV_CON_SHIFT = 8,
251 CLK_SPI5_PLL_SEL_SHIFT = 15,
Kever Yangca19eac2016-07-29 10:35:25 +0800252
253 /* CLKSEL_CON59 */
254 CLK_SPI1_PLL_SEL_SHIFT = 15,
255 CLK_SPI1_PLL_DIV_CON_SHIFT = 8,
256 CLK_SPI0_PLL_SEL_SHIFT = 7,
257 CLK_SPI0_PLL_DIV_CON_SHIFT = 0,
258
259 /* CLKSEL_CON60 */
260 CLK_SPI4_PLL_SEL_SHIFT = 15,
261 CLK_SPI4_PLL_DIV_CON_SHIFT = 8,
262 CLK_SPI2_PLL_SEL_SHIFT = 7,
263 CLK_SPI2_PLL_DIV_CON_SHIFT = 0,
264
265 /* CLKSEL_CON61 */
266 CLK_I2C_PLL_SEL_MASK = 1,
267 CLK_I2C_PLL_SEL_CPLL = 0,
268 CLK_I2C_PLL_SEL_GPLL = 1,
269 CLK_I2C5_PLL_SEL_SHIFT = 15,
270 CLK_I2C5_DIV_CON_SHIFT = 8,
271 CLK_I2C1_PLL_SEL_SHIFT = 7,
272 CLK_I2C1_DIV_CON_SHIFT = 0,
273
274 /* CLKSEL_CON62 */
275 CLK_I2C6_PLL_SEL_SHIFT = 15,
276 CLK_I2C6_DIV_CON_SHIFT = 8,
277 CLK_I2C2_PLL_SEL_SHIFT = 7,
278 CLK_I2C2_DIV_CON_SHIFT = 0,
279
280 /* CLKSEL_CON63 */
281 CLK_I2C7_PLL_SEL_SHIFT = 15,
282 CLK_I2C7_DIV_CON_SHIFT = 8,
283 CLK_I2C3_PLL_SEL_SHIFT = 7,
284 CLK_I2C3_DIV_CON_SHIFT = 0,
285
286 /* CRU_SOFTRST_CON4 */
287 RESETN_DDR0_REQ_SHIFT = 8,
288 RESETN_DDR0_REQ_MASK = 1 << RESETN_DDR0_REQ_SHIFT,
289 RESETN_DDRPHY0_REQ_SHIFT = 9,
290 RESETN_DDRPHY0_REQ_MASK = 1 << RESETN_DDRPHY0_REQ_SHIFT,
291 RESETN_DDR1_REQ_SHIFT = 12,
292 RESETN_DDR1_REQ_MASK = 1 << RESETN_DDR1_REQ_SHIFT,
293 RESETN_DDRPHY1_REQ_SHIFT = 13,
294 RESETN_DDRPHY1_REQ_MASK = 1 << RESETN_DDRPHY1_REQ_SHIFT,
295};
296
297#define VCO_MAX_KHZ (3200 * (MHz / KHz))
298#define VCO_MIN_KHZ (800 * (MHz / KHz))
299#define OUTPUT_MAX_KHZ (3200 * (MHz / KHz))
300#define OUTPUT_MIN_KHZ (16 * (MHz / KHz))
301
302/*
303 * the div restructions of pll in integer mode, these are defined in
304 * * CRU_*PLL_CON0 or PMUCRU_*PLL_CON0
305 */
306#define PLL_DIV_MIN 16
307#define PLL_DIV_MAX 3200
308
309/*
310 * How to calculate the PLL(from TRM V0.3 Part 1 Page 63):
311 * Formulas also embedded within the Fractional PLL Verilog model:
312 * If DSMPD = 1 (DSM is disabled, "integer mode")
313 * FOUTVCO = FREF / REFDIV * FBDIV
314 * FOUTPOSTDIV = FOUTVCO / POSTDIV1 / POSTDIV2
315 * Where:
316 * FOUTVCO = Fractional PLL non-divided output frequency
317 * FOUTPOSTDIV = Fractional PLL divided output frequency
318 * (output of second post divider)
319 * FREF = Fractional PLL input reference frequency, (the OSC_HZ 24MHz input)
320 * REFDIV = Fractional PLL input reference clock divider
321 * FBDIV = Integer value programmed into feedback divide
322 *
323 */
324static void rkclk_set_pll(u32 *pll_con, const struct pll_div *div)
325{
326 /* All 8 PLLs have same VCO and output frequency range restrictions. */
327 u32 vco_khz = OSC_HZ / 1000 * div->fbdiv / div->refdiv;
328 u32 output_khz = vco_khz / div->postdiv1 / div->postdiv2;
329
330 debug("PLL at %p: fbdiv=%d, refdiv=%d, postdiv1=%d, "
331 "postdiv2=%d, vco=%u khz, output=%u khz\n",
332 pll_con, div->fbdiv, div->refdiv, div->postdiv1,
333 div->postdiv2, vco_khz, output_khz);
334 assert(vco_khz >= VCO_MIN_KHZ && vco_khz <= VCO_MAX_KHZ &&
335 output_khz >= OUTPUT_MIN_KHZ && output_khz <= OUTPUT_MAX_KHZ &&
336 div->fbdiv >= PLL_DIV_MIN && div->fbdiv <= PLL_DIV_MAX);
337
338 /*
339 * When power on or changing PLL setting,
340 * we must force PLL into slow mode to ensure output stable clock.
341 */
342 rk_clrsetreg(&pll_con[3], PLL_MODE_MASK,
343 PLL_MODE_SLOW << PLL_MODE_SHIFT);
344
345 /* use integer mode */
346 rk_clrsetreg(&pll_con[3], PLL_DSMPD_MASK,
347 PLL_INTEGER_MODE << PLL_DSMPD_SHIFT);
348
349 rk_clrsetreg(&pll_con[0], PLL_FBDIV_MASK,
350 div->fbdiv << PLL_FBDIV_SHIFT);
351 rk_clrsetreg(&pll_con[1],
352 PLL_POSTDIV2_MASK | PLL_POSTDIV1_MASK |
353 PLL_REFDIV_MASK | PLL_REFDIV_SHIFT,
354 (div->postdiv2 << PLL_POSTDIV2_SHIFT) |
355 (div->postdiv1 << PLL_POSTDIV1_SHIFT) |
356 (div->refdiv << PLL_REFDIV_SHIFT));
357
358 /* waiting for pll lock */
359 while (!(readl(&pll_con[2]) & (1 << PLL_LOCK_STATUS_SHIFT)))
360 udelay(1);
361
362 /* pll enter normal mode */
363 rk_clrsetreg(&pll_con[3], PLL_MODE_MASK,
364 PLL_MODE_NORM << PLL_MODE_SHIFT);
365}
366
367static int pll_para_config(u32 freq_hz, struct pll_div *div)
368{
369 u32 ref_khz = OSC_HZ / KHz, refdiv, fbdiv = 0;
370 u32 postdiv1, postdiv2 = 1;
371 u32 fref_khz;
372 u32 diff_khz, best_diff_khz;
373 const u32 max_refdiv = 63, max_fbdiv = 3200, min_fbdiv = 16;
374 const u32 max_postdiv1 = 7, max_postdiv2 = 7;
375 u32 vco_khz;
376 u32 freq_khz = freq_hz / KHz;
377
378 if (!freq_hz) {
379 printf("%s: the frequency can't be 0 Hz\n", __func__);
380 return -1;
381 }
382
383 postdiv1 = DIV_ROUND_UP(VCO_MIN_KHZ, freq_khz);
384 if (postdiv1 > max_postdiv1) {
385 postdiv2 = DIV_ROUND_UP(postdiv1, max_postdiv1);
386 postdiv1 = DIV_ROUND_UP(postdiv1, postdiv2);
387 }
388
389 vco_khz = freq_khz * postdiv1 * postdiv2;
390
391 if (vco_khz < VCO_MIN_KHZ || vco_khz > VCO_MAX_KHZ ||
392 postdiv2 > max_postdiv2) {
393 printf("%s: Cannot find out a supported VCO"
394 " for Frequency (%uHz).\n", __func__, freq_hz);
395 return -1;
396 }
397
398 div->postdiv1 = postdiv1;
399 div->postdiv2 = postdiv2;
400
401 best_diff_khz = vco_khz;
402 for (refdiv = 1; refdiv < max_refdiv && best_diff_khz; refdiv++) {
403 fref_khz = ref_khz / refdiv;
404
405 fbdiv = vco_khz / fref_khz;
Jagan Tekibef02a32019-07-15 23:51:10 +0530406 if (fbdiv >= max_fbdiv || fbdiv <= min_fbdiv)
Kever Yangca19eac2016-07-29 10:35:25 +0800407 continue;
408 diff_khz = vco_khz - fbdiv * fref_khz;
409 if (fbdiv + 1 < max_fbdiv && diff_khz > fref_khz / 2) {
410 fbdiv++;
411 diff_khz = fref_khz - diff_khz;
412 }
413
414 if (diff_khz >= best_diff_khz)
415 continue;
416
417 best_diff_khz = diff_khz;
418 div->refdiv = refdiv;
419 div->fbdiv = fbdiv;
420 }
421
Jagan Tekibef02a32019-07-15 23:51:10 +0530422 if (best_diff_khz > 4 * (MHz / KHz)) {
Kever Yangca19eac2016-07-29 10:35:25 +0800423 printf("%s: Failed to match output frequency %u, "
424 "difference is %u Hz,exceed 4MHZ\n", __func__, freq_hz,
425 best_diff_khz * KHz);
426 return -1;
427 }
428 return 0;
429}
430
Jagan Teki783acfd2020-01-09 14:22:17 +0530431void rk3399_configure_cpu_l(struct rockchip_cru *cru,
Christoph Muellner25c7ba92018-11-30 20:32:48 +0100432 enum apll_l_frequencies apll_l_freq)
Kever Yangca19eac2016-07-29 10:35:25 +0800433{
434 u32 aclkm_div;
435 u32 pclk_dbg_div;
436 u32 atclk_div;
437
Christoph Muellner25c7ba92018-11-30 20:32:48 +0100438 /* Setup cluster L */
Kever Yangca19eac2016-07-29 10:35:25 +0800439 rkclk_set_pll(&cru->apll_l_con[0], apll_l_cfgs[apll_l_freq]);
440
Christoph Muellner25c7ba92018-11-30 20:32:48 +0100441 aclkm_div = LPLL_HZ / ACLKM_CORE_L_HZ - 1;
442 assert((aclkm_div + 1) * ACLKM_CORE_L_HZ == LPLL_HZ &&
Kever Yangca19eac2016-07-29 10:35:25 +0800443 aclkm_div < 0x1f);
444
Christoph Muellner25c7ba92018-11-30 20:32:48 +0100445 pclk_dbg_div = LPLL_HZ / PCLK_DBG_L_HZ - 1;
446 assert((pclk_dbg_div + 1) * PCLK_DBG_L_HZ == LPLL_HZ &&
Kever Yangca19eac2016-07-29 10:35:25 +0800447 pclk_dbg_div < 0x1f);
448
Christoph Muellner25c7ba92018-11-30 20:32:48 +0100449 atclk_div = LPLL_HZ / ATCLK_CORE_L_HZ - 1;
450 assert((atclk_div + 1) * ATCLK_CORE_L_HZ == LPLL_HZ &&
Kever Yangca19eac2016-07-29 10:35:25 +0800451 atclk_div < 0x1f);
452
453 rk_clrsetreg(&cru->clksel_con[0],
454 ACLKM_CORE_L_DIV_CON_MASK | CLK_CORE_L_PLL_SEL_MASK |
455 CLK_CORE_L_DIV_MASK,
456 aclkm_div << ACLKM_CORE_L_DIV_CON_SHIFT |
457 CLK_CORE_L_PLL_SEL_ALPLL << CLK_CORE_L_PLL_SEL_SHIFT |
458 0 << CLK_CORE_L_DIV_SHIFT);
459
460 rk_clrsetreg(&cru->clksel_con[1],
461 PCLK_DBG_L_DIV_MASK | ATCLK_CORE_L_DIV_MASK,
462 pclk_dbg_div << PCLK_DBG_L_DIV_SHIFT |
463 atclk_div << ATCLK_CORE_L_DIV_SHIFT);
464}
Christoph Muellner25c7ba92018-11-30 20:32:48 +0100465
Jagan Teki783acfd2020-01-09 14:22:17 +0530466void rk3399_configure_cpu_b(struct rockchip_cru *cru,
Christoph Muellner25c7ba92018-11-30 20:32:48 +0100467 enum apll_b_frequencies apll_b_freq)
468{
469 u32 aclkm_div;
470 u32 pclk_dbg_div;
471 u32 atclk_div;
472
473 /* Setup cluster B */
474 rkclk_set_pll(&cru->apll_b_con[0], apll_b_cfgs[apll_b_freq]);
475
476 aclkm_div = BPLL_HZ / ACLKM_CORE_B_HZ - 1;
477 assert((aclkm_div + 1) * ACLKM_CORE_B_HZ == BPLL_HZ &&
478 aclkm_div < 0x1f);
479
480 pclk_dbg_div = BPLL_HZ / PCLK_DBG_B_HZ - 1;
481 assert((pclk_dbg_div + 1) * PCLK_DBG_B_HZ == BPLL_HZ &&
482 pclk_dbg_div < 0x1f);
483
484 atclk_div = BPLL_HZ / ATCLK_CORE_B_HZ - 1;
485 assert((atclk_div + 1) * ATCLK_CORE_B_HZ == BPLL_HZ &&
486 atclk_div < 0x1f);
487
488 rk_clrsetreg(&cru->clksel_con[2],
489 ACLKM_CORE_B_DIV_CON_MASK | CLK_CORE_B_PLL_SEL_MASK |
490 CLK_CORE_B_DIV_MASK,
491 aclkm_div << ACLKM_CORE_B_DIV_CON_SHIFT |
492 CLK_CORE_B_PLL_SEL_ABPLL << CLK_CORE_B_PLL_SEL_SHIFT |
493 0 << CLK_CORE_B_DIV_SHIFT);
494
495 rk_clrsetreg(&cru->clksel_con[3],
496 PCLK_DBG_B_DIV_MASK | ATCLK_CORE_B_DIV_MASK,
497 pclk_dbg_div << PCLK_DBG_B_DIV_SHIFT |
498 atclk_div << ATCLK_CORE_B_DIV_SHIFT);
499}
500
Kever Yangca19eac2016-07-29 10:35:25 +0800501#define I2C_CLK_REG_MASK(bus) \
Jagan Tekibef02a32019-07-15 23:51:10 +0530502 (I2C_DIV_CON_MASK << CLK_I2C ##bus## _DIV_CON_SHIFT | \
503 CLK_I2C_PLL_SEL_MASK << CLK_I2C ##bus## _PLL_SEL_SHIFT)
Kever Yangca19eac2016-07-29 10:35:25 +0800504
505#define I2C_CLK_REG_VALUE(bus, clk_div) \
Jagan Tekibef02a32019-07-15 23:51:10 +0530506 ((clk_div - 1) << CLK_I2C ##bus## _DIV_CON_SHIFT | \
507 CLK_I2C_PLL_SEL_GPLL << CLK_I2C ##bus## _PLL_SEL_SHIFT)
Kever Yangca19eac2016-07-29 10:35:25 +0800508
509#define I2C_CLK_DIV_VALUE(con, bus) \
Jagan Tekibef02a32019-07-15 23:51:10 +0530510 ((con >> CLK_I2C ##bus## _DIV_CON_SHIFT) & I2C_DIV_CON_MASK)
Kever Yangca19eac2016-07-29 10:35:25 +0800511
Kever Yange54d26a2016-08-12 17:47:15 +0800512#define I2C_PMUCLK_REG_MASK(bus) \
Jagan Tekibef02a32019-07-15 23:51:10 +0530513 (I2C_DIV_CON_MASK << CLK_I2C ##bus## _DIV_CON_SHIFT)
Kever Yange54d26a2016-08-12 17:47:15 +0800514
515#define I2C_PMUCLK_REG_VALUE(bus, clk_div) \
Jagan Tekibef02a32019-07-15 23:51:10 +0530516 ((clk_div - 1) << CLK_I2C ##bus## _DIV_CON_SHIFT)
Kever Yange54d26a2016-08-12 17:47:15 +0800517
Jagan Teki783acfd2020-01-09 14:22:17 +0530518static ulong rk3399_i2c_get_clk(struct rockchip_cru *cru, ulong clk_id)
Kever Yangca19eac2016-07-29 10:35:25 +0800519{
520 u32 div, con;
521
522 switch (clk_id) {
523 case SCLK_I2C1:
524 con = readl(&cru->clksel_con[61]);
525 div = I2C_CLK_DIV_VALUE(con, 1);
526 break;
527 case SCLK_I2C2:
528 con = readl(&cru->clksel_con[62]);
529 div = I2C_CLK_DIV_VALUE(con, 2);
530 break;
531 case SCLK_I2C3:
532 con = readl(&cru->clksel_con[63]);
533 div = I2C_CLK_DIV_VALUE(con, 3);
534 break;
535 case SCLK_I2C5:
536 con = readl(&cru->clksel_con[61]);
537 div = I2C_CLK_DIV_VALUE(con, 5);
538 break;
539 case SCLK_I2C6:
540 con = readl(&cru->clksel_con[62]);
541 div = I2C_CLK_DIV_VALUE(con, 6);
542 break;
543 case SCLK_I2C7:
544 con = readl(&cru->clksel_con[63]);
545 div = I2C_CLK_DIV_VALUE(con, 7);
546 break;
547 default:
548 printf("do not support this i2c bus\n");
549 return -EINVAL;
550 }
551
552 return DIV_TO_RATE(GPLL_HZ, div);
553}
554
Jagan Teki783acfd2020-01-09 14:22:17 +0530555static ulong rk3399_i2c_set_clk(struct rockchip_cru *cru, ulong clk_id, uint hz)
Kever Yangca19eac2016-07-29 10:35:25 +0800556{
557 int src_clk_div;
558
559 /* i2c0,4,8 src clock from ppll, i2c1,2,3,5,6,7 src clock from gpll*/
560 src_clk_div = GPLL_HZ / hz;
561 assert(src_clk_div - 1 < 127);
562
563 switch (clk_id) {
564 case SCLK_I2C1:
565 rk_clrsetreg(&cru->clksel_con[61], I2C_CLK_REG_MASK(1),
566 I2C_CLK_REG_VALUE(1, src_clk_div));
567 break;
568 case SCLK_I2C2:
569 rk_clrsetreg(&cru->clksel_con[62], I2C_CLK_REG_MASK(2),
570 I2C_CLK_REG_VALUE(2, src_clk_div));
571 break;
572 case SCLK_I2C3:
573 rk_clrsetreg(&cru->clksel_con[63], I2C_CLK_REG_MASK(3),
574 I2C_CLK_REG_VALUE(3, src_clk_div));
575 break;
576 case SCLK_I2C5:
577 rk_clrsetreg(&cru->clksel_con[61], I2C_CLK_REG_MASK(5),
578 I2C_CLK_REG_VALUE(5, src_clk_div));
579 break;
580 case SCLK_I2C6:
581 rk_clrsetreg(&cru->clksel_con[62], I2C_CLK_REG_MASK(6),
582 I2C_CLK_REG_VALUE(6, src_clk_div));
583 break;
584 case SCLK_I2C7:
585 rk_clrsetreg(&cru->clksel_con[63], I2C_CLK_REG_MASK(7),
586 I2C_CLK_REG_VALUE(7, src_clk_div));
587 break;
588 default:
589 printf("do not support this i2c bus\n");
590 return -EINVAL;
591 }
592
Philipp Tomsich30d7c152017-04-20 22:05:50 +0200593 return rk3399_i2c_get_clk(cru, clk_id);
Kever Yangca19eac2016-07-29 10:35:25 +0800594}
595
Philipp Tomsichc31ee922017-04-20 22:05:49 +0200596/*
597 * RK3399 SPI clocks have a common divider-width (7 bits) and a single bit
598 * to select either CPLL or GPLL as the clock-parent. The location within
599 * the enclosing CLKSEL_CON (i.e. div_shift and sel_shift) are variable.
600 */
601
602struct spi_clkreg {
Jagan Tekibef02a32019-07-15 23:51:10 +0530603 u8 reg; /* CLKSEL_CON[reg] register in CRU */
604 u8 div_shift;
605 u8 sel_shift;
Philipp Tomsichc31ee922017-04-20 22:05:49 +0200606};
607
608/*
609 * The entries are numbered relative to their offset from SCLK_SPI0.
610 *
611 * Note that SCLK_SPI3 (which is configured via PMUCRU and requires different
612 * logic is not supported).
613 */
614static const struct spi_clkreg spi_clkregs[] = {
615 [0] = { .reg = 59,
616 .div_shift = CLK_SPI0_PLL_DIV_CON_SHIFT,
617 .sel_shift = CLK_SPI0_PLL_SEL_SHIFT, },
618 [1] = { .reg = 59,
619 .div_shift = CLK_SPI1_PLL_DIV_CON_SHIFT,
620 .sel_shift = CLK_SPI1_PLL_SEL_SHIFT, },
621 [2] = { .reg = 60,
622 .div_shift = CLK_SPI2_PLL_DIV_CON_SHIFT,
623 .sel_shift = CLK_SPI2_PLL_SEL_SHIFT, },
624 [3] = { .reg = 60,
625 .div_shift = CLK_SPI4_PLL_DIV_CON_SHIFT,
626 .sel_shift = CLK_SPI4_PLL_SEL_SHIFT, },
627 [4] = { .reg = 58,
628 .div_shift = CLK_SPI5_PLL_DIV_CON_SHIFT,
629 .sel_shift = CLK_SPI5_PLL_SEL_SHIFT, },
630};
631
Jagan Teki783acfd2020-01-09 14:22:17 +0530632static ulong rk3399_spi_get_clk(struct rockchip_cru *cru, ulong clk_id)
Philipp Tomsichc31ee922017-04-20 22:05:49 +0200633{
634 const struct spi_clkreg *spiclk = NULL;
635 u32 div, val;
636
637 switch (clk_id) {
638 case SCLK_SPI0 ... SCLK_SPI5:
639 spiclk = &spi_clkregs[clk_id - SCLK_SPI0];
640 break;
641
642 default:
Masahiro Yamada81e10422017-09-16 14:10:41 +0900643 pr_err("%s: SPI clk-id %ld not supported\n", __func__, clk_id);
Philipp Tomsichc31ee922017-04-20 22:05:49 +0200644 return -EINVAL;
645 }
646
647 val = readl(&cru->clksel_con[spiclk->reg]);
Philipp Tomsich8a4868f2017-11-22 19:45:04 +0100648 div = bitfield_extract(val, spiclk->div_shift,
649 CLK_SPI_PLL_DIV_CON_WIDTH);
Philipp Tomsichc31ee922017-04-20 22:05:49 +0200650
651 return DIV_TO_RATE(GPLL_HZ, div);
652}
653
Jagan Teki783acfd2020-01-09 14:22:17 +0530654static ulong rk3399_spi_set_clk(struct rockchip_cru *cru, ulong clk_id, uint hz)
Philipp Tomsichc31ee922017-04-20 22:05:49 +0200655{
656 const struct spi_clkreg *spiclk = NULL;
657 int src_clk_div;
658
Kever Yangf20995b2017-07-27 12:54:02 +0800659 src_clk_div = DIV_ROUND_UP(GPLL_HZ, hz) - 1;
660 assert(src_clk_div < 128);
Philipp Tomsichc31ee922017-04-20 22:05:49 +0200661
662 switch (clk_id) {
663 case SCLK_SPI1 ... SCLK_SPI5:
664 spiclk = &spi_clkregs[clk_id - SCLK_SPI0];
665 break;
666
667 default:
Masahiro Yamada81e10422017-09-16 14:10:41 +0900668 pr_err("%s: SPI clk-id %ld not supported\n", __func__, clk_id);
Philipp Tomsichc31ee922017-04-20 22:05:49 +0200669 return -EINVAL;
670 }
671
672 rk_clrsetreg(&cru->clksel_con[spiclk->reg],
673 ((CLK_SPI_PLL_DIV_CON_MASK << spiclk->div_shift) |
674 (CLK_SPI_PLL_SEL_GPLL << spiclk->sel_shift)),
675 ((src_clk_div << spiclk->div_shift) |
676 (CLK_SPI_PLL_SEL_GPLL << spiclk->sel_shift)));
677
Philipp Tomsich30d7c152017-04-20 22:05:50 +0200678 return rk3399_spi_get_clk(cru, clk_id);
Philipp Tomsichc31ee922017-04-20 22:05:49 +0200679}
680
Jagan Teki783acfd2020-01-09 14:22:17 +0530681static ulong rk3399_vop_set_clk(struct rockchip_cru *cru, ulong clk_id, u32 hz)
Kever Yangca19eac2016-07-29 10:35:25 +0800682{
683 struct pll_div vpll_config = {0};
Jagan Tekibef02a32019-07-15 23:51:10 +0530684 int aclk_vop = 198 * MHz;
Kever Yangca19eac2016-07-29 10:35:25 +0800685 void *aclkreg_addr, *dclkreg_addr;
686 u32 div;
687
688 switch (clk_id) {
689 case DCLK_VOP0:
690 aclkreg_addr = &cru->clksel_con[47];
691 dclkreg_addr = &cru->clksel_con[49];
692 break;
693 case DCLK_VOP1:
694 aclkreg_addr = &cru->clksel_con[48];
695 dclkreg_addr = &cru->clksel_con[50];
696 break;
697 default:
698 return -EINVAL;
699 }
700 /* vop aclk source clk: cpll */
701 div = CPLL_HZ / aclk_vop;
702 assert(div - 1 < 32);
703
704 rk_clrsetreg(aclkreg_addr,
705 ACLK_VOP_PLL_SEL_MASK | ACLK_VOP_DIV_CON_MASK,
706 ACLK_VOP_PLL_SEL_CPLL << ACLK_VOP_PLL_SEL_SHIFT |
707 (div - 1) << ACLK_VOP_DIV_CON_SHIFT);
708
709 /* vop dclk source from vpll, and equals to vpll(means div == 1) */
710 if (pll_para_config(hz, &vpll_config))
711 return -1;
712
713 rkclk_set_pll(&cru->vpll_con[0], &vpll_config);
714
715 rk_clrsetreg(dclkreg_addr,
Jagan Tekibef02a32019-07-15 23:51:10 +0530716 DCLK_VOP_DCLK_SEL_MASK | DCLK_VOP_PLL_SEL_MASK |
Kever Yangca19eac2016-07-29 10:35:25 +0800717 DCLK_VOP_DIV_CON_MASK,
718 DCLK_VOP_DCLK_SEL_DIVOUT << DCLK_VOP_DCLK_SEL_SHIFT |
719 DCLK_VOP_PLL_SEL_VPLL << DCLK_VOP_PLL_SEL_SHIFT |
720 (1 - 1) << DCLK_VOP_DIV_CON_SHIFT);
721
722 return hz;
723}
724
Jagan Teki783acfd2020-01-09 14:22:17 +0530725static ulong rk3399_mmc_get_clk(struct rockchip_cru *cru, uint clk_id)
Kever Yangca19eac2016-07-29 10:35:25 +0800726{
727 u32 div, con;
728
729 switch (clk_id) {
Michal Suchanek84789442022-08-21 09:17:24 +0200730 case HCLK_SDIO:
731 case SCLK_SDIO:
732 con = readl(&cru->clksel_con[15]);
733 /* dwmmc controller have internal div 2 */
734 div = 2;
735 break;
Philipp Tomsich78a73142017-04-25 09:52:06 +0200736 case HCLK_SDMMC:
Kever Yangca19eac2016-07-29 10:35:25 +0800737 case SCLK_SDMMC:
738 con = readl(&cru->clksel_con[16]);
Kever Yang99b546d2017-07-27 12:54:01 +0800739 /* dwmmc controller have internal div 2 */
740 div = 2;
Kever Yangca19eac2016-07-29 10:35:25 +0800741 break;
742 case SCLK_EMMC:
Jagan Tekiad386002020-05-24 22:13:15 +0530743 con = readl(&cru->clksel_con[22]);
Kever Yang99b546d2017-07-27 12:54:01 +0800744 div = 1;
Kever Yangca19eac2016-07-29 10:35:25 +0800745 break;
746 default:
747 return -EINVAL;
748 }
Kever Yangca19eac2016-07-29 10:35:25 +0800749
Kever Yang99b546d2017-07-27 12:54:01 +0800750 div *= (con & CLK_EMMC_DIV_CON_MASK) >> CLK_EMMC_DIV_CON_SHIFT;
Kever Yangdc850de2016-08-04 11:44:58 +0800751 if ((con & CLK_EMMC_PLL_MASK) >> CLK_EMMC_PLL_SHIFT
752 == CLK_EMMC_PLL_SEL_24M)
Kever Yang99b546d2017-07-27 12:54:01 +0800753 return DIV_TO_RATE(OSC_HZ, div);
Kever Yangdc850de2016-08-04 11:44:58 +0800754 else
755 return DIV_TO_RATE(GPLL_HZ, div);
Kever Yangca19eac2016-07-29 10:35:25 +0800756}
757
Michal Suchanek84789442022-08-21 09:17:24 +0200758static void rk3399_dwmmc_set_clk(struct rockchip_cru *cru,
759 unsigned int con, ulong set_rate)
760{
761 /* Select clk_sdmmc source from GPLL by default */
762 /* mmc clock defaulg div 2 internal, provide double in cru */
763 int src_clk_div = DIV_ROUND_UP(GPLL_HZ / 2, set_rate);
764
765 if (src_clk_div > 128) {
766 /* use 24MHz source for 400KHz clock */
767 src_clk_div = DIV_ROUND_UP(OSC_HZ / 2, set_rate);
768 assert(src_clk_div - 1 < 128);
769 rk_clrsetreg(&cru->clksel_con[con],
770 CLK_EMMC_PLL_MASK | CLK_EMMC_DIV_CON_MASK,
771 CLK_EMMC_PLL_SEL_24M << CLK_EMMC_PLL_SHIFT |
772 (src_clk_div - 1) << CLK_EMMC_DIV_CON_SHIFT);
773 } else {
774 rk_clrsetreg(&cru->clksel_con[con],
775 CLK_EMMC_PLL_MASK | CLK_EMMC_DIV_CON_MASK,
776 CLK_EMMC_PLL_SEL_GPLL << CLK_EMMC_PLL_SHIFT |
777 (src_clk_div - 1) << CLK_EMMC_DIV_CON_SHIFT);
778 }
779}
780
Jagan Teki783acfd2020-01-09 14:22:17 +0530781static ulong rk3399_mmc_set_clk(struct rockchip_cru *cru,
Kever Yangca19eac2016-07-29 10:35:25 +0800782 ulong clk_id, ulong set_rate)
783{
Kever Yangca19eac2016-07-29 10:35:25 +0800784 switch (clk_id) {
Michal Suchanek84789442022-08-21 09:17:24 +0200785 case HCLK_SDIO:
786 case SCLK_SDIO:
787 rk3399_dwmmc_set_clk(cru, 15, set_rate);
788 break;
Philipp Tomsich78a73142017-04-25 09:52:06 +0200789 case HCLK_SDMMC:
Kever Yangca19eac2016-07-29 10:35:25 +0800790 case SCLK_SDMMC:
Michal Suchanek84789442022-08-21 09:17:24 +0200791 rk3399_dwmmc_set_clk(cru, 16, set_rate);
Kever Yangca19eac2016-07-29 10:35:25 +0800792 break;
Michal Suchanek84789442022-08-21 09:17:24 +0200793 case SCLK_EMMC: {
794 int aclk_emmc = 198 * MHz;
Kever Yangca19eac2016-07-29 10:35:25 +0800795 /* Select aclk_emmc source from GPLL */
Michal Suchanek84789442022-08-21 09:17:24 +0200796 int src_clk_div = DIV_ROUND_UP(GPLL_HZ, aclk_emmc);
797
Kever Yangf20995b2017-07-27 12:54:02 +0800798 assert(src_clk_div - 1 < 32);
Kever Yangca19eac2016-07-29 10:35:25 +0800799
800 rk_clrsetreg(&cru->clksel_con[21],
801 ACLK_EMMC_PLL_SEL_MASK | ACLK_EMMC_DIV_CON_MASK,
802 ACLK_EMMC_PLL_SEL_GPLL << ACLK_EMMC_PLL_SEL_SHIFT |
803 (src_clk_div - 1) << ACLK_EMMC_DIV_CON_SHIFT);
804
805 /* Select clk_emmc source from GPLL too */
Kever Yangf20995b2017-07-27 12:54:02 +0800806 src_clk_div = DIV_ROUND_UP(GPLL_HZ, set_rate);
807 assert(src_clk_div - 1 < 128);
Kever Yangca19eac2016-07-29 10:35:25 +0800808
809 rk_clrsetreg(&cru->clksel_con[22],
810 CLK_EMMC_PLL_MASK | CLK_EMMC_DIV_CON_MASK,
811 CLK_EMMC_PLL_SEL_GPLL << CLK_EMMC_PLL_SHIFT |
812 (src_clk_div - 1) << CLK_EMMC_DIV_CON_SHIFT);
813 break;
Michal Suchanek84789442022-08-21 09:17:24 +0200814 }
Kever Yangca19eac2016-07-29 10:35:25 +0800815 default:
816 return -EINVAL;
817 }
818 return rk3399_mmc_get_clk(cru, clk_id);
819}
820
Jagan Teki783acfd2020-01-09 14:22:17 +0530821static ulong rk3399_gmac_set_clk(struct rockchip_cru *cru, ulong rate)
Philipp Tomsichf4ba6ed2018-01-08 13:11:01 +0100822{
823 ulong ret;
824
825 /*
826 * The RGMII CLK can be derived either from an external "clkin"
827 * or can be generated from internally by a divider from SCLK_MAC.
828 */
829 if (readl(&cru->clksel_con[19]) & BIT(4)) {
830 /* An external clock will always generate the right rate... */
831 ret = rate;
832 } else {
833 /*
834 * No platform uses an internal clock to date.
835 * Implement this once it becomes necessary and print an error
836 * if someone tries to use it (while it remains unimplemented).
837 */
838 pr_err("%s: internal clock is UNIMPLEMENTED\n", __func__);
839 ret = 0;
840 }
841
842 return ret;
843}
844
Kever Yange1980532017-02-13 17:38:56 +0800845#define PMUSGRF_DDR_RGN_CON16 0xff330040
Jagan Teki783acfd2020-01-09 14:22:17 +0530846static ulong rk3399_ddr_set_clk(struct rockchip_cru *cru,
Kever Yange1980532017-02-13 17:38:56 +0800847 ulong set_rate)
848{
849 struct pll_div dpll_cfg;
850
851 /* IC ECO bug, need to set this register */
852 writel(0xc000c000, PMUSGRF_DDR_RGN_CON16);
853
854 /* clk_ddrc == DPLL = 24MHz / refdiv * fbdiv / postdiv1 / postdiv2 */
855 switch (set_rate) {
Jagan Teki4833f322019-07-16 17:27:35 +0530856 case 50 * MHz:
857 dpll_cfg = (struct pll_div)
Xavier Drudis Ferran7d20cf92022-07-16 12:31:45 +0200858 {.refdiv = 2, .fbdiv = 75, .postdiv1 = 3, .postdiv2 = 6};
Jagan Teki4833f322019-07-16 17:27:35 +0530859 break;
Jagan Tekibef02a32019-07-15 23:51:10 +0530860 case 200 * MHz:
Kever Yange1980532017-02-13 17:38:56 +0800861 dpll_cfg = (struct pll_div)
862 {.refdiv = 1, .fbdiv = 50, .postdiv1 = 6, .postdiv2 = 1};
863 break;
Jagan Tekibef02a32019-07-15 23:51:10 +0530864 case 300 * MHz:
Kever Yange1980532017-02-13 17:38:56 +0800865 dpll_cfg = (struct pll_div)
866 {.refdiv = 2, .fbdiv = 100, .postdiv1 = 4, .postdiv2 = 1};
867 break;
Jagan Tekif0b06312019-07-16 17:27:36 +0530868 case 400 * MHz:
869 dpll_cfg = (struct pll_div)
870 {.refdiv = 1, .fbdiv = 50, .postdiv1 = 3, .postdiv2 = 1};
871 break;
Jagan Tekibef02a32019-07-15 23:51:10 +0530872 case 666 * MHz:
Kever Yange1980532017-02-13 17:38:56 +0800873 dpll_cfg = (struct pll_div)
874 {.refdiv = 2, .fbdiv = 111, .postdiv1 = 2, .postdiv2 = 1};
875 break;
Jagan Tekibef02a32019-07-15 23:51:10 +0530876 case 800 * MHz:
Kever Yange1980532017-02-13 17:38:56 +0800877 dpll_cfg = (struct pll_div)
878 {.refdiv = 1, .fbdiv = 100, .postdiv1 = 3, .postdiv2 = 1};
879 break;
Jagan Tekibef02a32019-07-15 23:51:10 +0530880 case 933 * MHz:
Kever Yange1980532017-02-13 17:38:56 +0800881 dpll_cfg = (struct pll_div)
882 {.refdiv = 1, .fbdiv = 116, .postdiv1 = 3, .postdiv2 = 1};
883 break;
884 default:
Masahiro Yamada81e10422017-09-16 14:10:41 +0900885 pr_err("Unsupported SDRAM frequency!,%ld\n", set_rate);
Kever Yange1980532017-02-13 17:38:56 +0800886 }
887 rkclk_set_pll(&cru->dpll_con[0], &dpll_cfg);
888
889 return set_rate;
890}
David Wuf91b9b42017-09-20 14:38:58 +0800891
Jack Mitchell4ef38ce2020-09-17 10:42:06 +0100892static ulong rk3399_alive_get_clk(struct rockchip_cru *cru)
893{
894 u32 div, val;
895
896 val = readl(&cru->clksel_con[57]);
897 div = (val & PCLK_ALIVE_DIV_CON_MASK) >>
898 PCLK_ALIVE_DIV_CON_SHIFT;
899
900 return DIV_TO_RATE(GPLL_HZ, div);
901}
902
Jagan Teki783acfd2020-01-09 14:22:17 +0530903static ulong rk3399_saradc_get_clk(struct rockchip_cru *cru)
David Wuf91b9b42017-09-20 14:38:58 +0800904{
905 u32 div, val;
906
907 val = readl(&cru->clksel_con[26]);
908 div = bitfield_extract(val, CLK_SARADC_DIV_CON_SHIFT,
909 CLK_SARADC_DIV_CON_WIDTH);
910
911 return DIV_TO_RATE(OSC_HZ, div);
912}
913
Jagan Teki783acfd2020-01-09 14:22:17 +0530914static ulong rk3399_saradc_set_clk(struct rockchip_cru *cru, uint hz)
David Wuf91b9b42017-09-20 14:38:58 +0800915{
916 int src_clk_div;
917
918 src_clk_div = DIV_ROUND_UP(OSC_HZ, hz) - 1;
919 assert(src_clk_div < 128);
920
921 rk_clrsetreg(&cru->clksel_con[26],
922 CLK_SARADC_DIV_CON_MASK,
923 src_clk_div << CLK_SARADC_DIV_CON_SHIFT);
924
925 return rk3399_saradc_get_clk(cru);
926}
927
Jonas Karlman1f662832024-05-01 16:22:20 +0000928static ulong rk3399_pciephy_get_clk(struct rockchip_cru *cru)
929{
930 if (readl(&cru->clksel_con[18]) & BIT(10))
931 return 100 * MHz;
932 else
933 return OSC_HZ;
934}
935
936static ulong rk3399_pciephy_set_clk(struct rockchip_cru *cru, uint hz)
937{
938 if (hz == 100 * MHz)
939 rk_setreg(&cru->clksel_con[18], BIT(10));
940 else if (hz == OSC_HZ)
941 rk_clrreg(&cru->clksel_con[18], BIT(10));
942 else
943 return -EINVAL;
944
945 return rk3399_pciephy_get_clk(cru);
946}
947
Kever Yangca19eac2016-07-29 10:35:25 +0800948static ulong rk3399_clk_get_rate(struct clk *clk)
949{
950 struct rk3399_clk_priv *priv = dev_get_priv(clk->dev);
951 ulong rate = 0;
952
953 switch (clk->id) {
954 case 0 ... 63:
955 return 0;
Michal Suchanek84789442022-08-21 09:17:24 +0200956 case HCLK_SDIO:
957 case SCLK_SDIO:
Philipp Tomsich78a73142017-04-25 09:52:06 +0200958 case HCLK_SDMMC:
Kever Yangca19eac2016-07-29 10:35:25 +0800959 case SCLK_SDMMC:
960 case SCLK_EMMC:
961 rate = rk3399_mmc_get_clk(priv->cru, clk->id);
962 break;
963 case SCLK_I2C1:
964 case SCLK_I2C2:
965 case SCLK_I2C3:
966 case SCLK_I2C5:
967 case SCLK_I2C6:
968 case SCLK_I2C7:
969 rate = rk3399_i2c_get_clk(priv->cru, clk->id);
970 break;
Philipp Tomsichc31ee922017-04-20 22:05:49 +0200971 case SCLK_SPI0...SCLK_SPI5:
972 rate = rk3399_spi_get_clk(priv->cru, clk->id);
973 break;
974 case SCLK_UART0:
Christoph Muellnere5607a02019-05-07 10:58:44 +0200975 case SCLK_UART1:
Philipp Tomsichc31ee922017-04-20 22:05:49 +0200976 case SCLK_UART2:
Christoph Muellnere5607a02019-05-07 10:58:44 +0200977 case SCLK_UART3:
Jonas Karlman696ab872024-05-01 16:22:21 +0000978 case SCLK_USB3OTG0_REF:
979 case SCLK_USB3OTG1_REF:
980 return OSC_HZ;
Philipp Tomsich10b594b2017-04-28 18:33:57 +0200981 case PCLK_HDMI_CTRL:
982 break;
Kever Yangca19eac2016-07-29 10:35:25 +0800983 case DCLK_VOP0:
984 case DCLK_VOP1:
985 break;
Philipp Tomsichd10b45e2017-04-28 17:11:55 +0200986 case PCLK_EFUSE1024NS:
987 break;
David Wuf91b9b42017-09-20 14:38:58 +0800988 case SCLK_SARADC:
989 rate = rk3399_saradc_get_clk(priv->cru);
990 break;
Jonas Karlman1f662832024-05-01 16:22:20 +0000991 case SCLK_PCIEPHY_REF:
992 rate = rk3399_pciephy_get_clk(priv->cru);
993 break;
Simon Glassd27b3172019-01-21 14:53:30 -0700994 case ACLK_VIO:
995 case ACLK_HDCP:
996 case ACLK_GIC_PRE:
997 case PCLK_DDR:
Jonas Karlman4d106982024-05-01 16:22:19 +0000998 case ACLK_VDU:
Simon Glassd27b3172019-01-21 14:53:30 -0700999 break;
Jack Mitchell4ef38ce2020-09-17 10:42:06 +01001000 case PCLK_ALIVE:
1001 case PCLK_WDT:
1002 rate = rk3399_alive_get_clk(priv->cru);
1003 break;
Kever Yangca19eac2016-07-29 10:35:25 +08001004 default:
Simon Glassd27b3172019-01-21 14:53:30 -07001005 log_debug("Unknown clock %lu\n", clk->id);
Kever Yangca19eac2016-07-29 10:35:25 +08001006 return -ENOENT;
1007 }
1008
1009 return rate;
1010}
1011
1012static ulong rk3399_clk_set_rate(struct clk *clk, ulong rate)
1013{
1014 struct rk3399_clk_priv *priv = dev_get_priv(clk->dev);
1015 ulong ret = 0;
1016
1017 switch (clk->id) {
1018 case 0 ... 63:
1019 return 0;
Philipp Tomsich2d20a632018-01-08 14:00:27 +01001020
1021 case ACLK_PERIHP:
1022 case HCLK_PERIHP:
1023 case PCLK_PERIHP:
1024 return 0;
1025
1026 case ACLK_PERILP0:
1027 case HCLK_PERILP0:
1028 case PCLK_PERILP0:
1029 return 0;
1030
1031 case ACLK_CCI:
1032 return 0;
1033
1034 case HCLK_PERILP1:
1035 case PCLK_PERILP1:
1036 return 0;
1037
Michal Suchanek84789442022-08-21 09:17:24 +02001038 case HCLK_SDIO:
1039 case SCLK_SDIO:
Philipp Tomsich78a73142017-04-25 09:52:06 +02001040 case HCLK_SDMMC:
Kever Yangca19eac2016-07-29 10:35:25 +08001041 case SCLK_SDMMC:
1042 case SCLK_EMMC:
1043 ret = rk3399_mmc_set_clk(priv->cru, clk->id, rate);
1044 break;
Philipp Tomsichbfa896c2017-03-24 19:24:25 +01001045 case SCLK_MAC:
Philipp Tomsichf4ba6ed2018-01-08 13:11:01 +01001046 ret = rk3399_gmac_set_clk(priv->cru, rate);
Philipp Tomsichbfa896c2017-03-24 19:24:25 +01001047 break;
Kever Yangca19eac2016-07-29 10:35:25 +08001048 case SCLK_I2C1:
1049 case SCLK_I2C2:
1050 case SCLK_I2C3:
1051 case SCLK_I2C5:
1052 case SCLK_I2C6:
1053 case SCLK_I2C7:
1054 ret = rk3399_i2c_set_clk(priv->cru, clk->id, rate);
1055 break;
Philipp Tomsichc31ee922017-04-20 22:05:49 +02001056 case SCLK_SPI0...SCLK_SPI5:
1057 ret = rk3399_spi_set_clk(priv->cru, clk->id, rate);
1058 break;
Philipp Tomsich10b594b2017-04-28 18:33:57 +02001059 case PCLK_HDMI_CTRL:
1060 case PCLK_VIO_GRF:
1061 /* the PCLK gates for video are enabled by default */
1062 break;
Kever Yangca19eac2016-07-29 10:35:25 +08001063 case DCLK_VOP0:
1064 case DCLK_VOP1:
Kever Yange54d26a2016-08-12 17:47:15 +08001065 ret = rk3399_vop_set_clk(priv->cru, clk->id, rate);
Kever Yangca19eac2016-07-29 10:35:25 +08001066 break;
Jagan Teki99f0f822020-04-02 17:11:21 +05301067 case ACLK_VOP1:
1068 case HCLK_VOP1:
Jagan Teki3f26bce2020-04-28 15:30:16 +05301069 case HCLK_SD:
Jagan Teki4fb2c6d2020-05-26 11:32:06 +08001070 case SCLK_UPHY0_TCPDCORE:
1071 case SCLK_UPHY1_TCPDCORE:
Jagan Teki99f0f822020-04-02 17:11:21 +05301072 /**
1073 * assigned-clocks handling won't require for vopl, so
1074 * return 0 to satisfy clk_set_defaults during device probe.
1075 */
1076 return 0;
Jonas Karlman91e4c152024-05-01 16:22:18 +00001077 case SCLK_DDRC:
Kever Yange1980532017-02-13 17:38:56 +08001078 ret = rk3399_ddr_set_clk(priv->cru, rate);
1079 break;
Philipp Tomsichd10b45e2017-04-28 17:11:55 +02001080 case PCLK_EFUSE1024NS:
1081 break;
David Wuf91b9b42017-09-20 14:38:58 +08001082 case SCLK_SARADC:
1083 ret = rk3399_saradc_set_clk(priv->cru, rate);
1084 break;
Jonas Karlman1f662832024-05-01 16:22:20 +00001085 case SCLK_PCIEPHY_REF:
1086 ret = rk3399_pciephy_set_clk(priv->cru, rate);
1087 break;
Simon Glassd27b3172019-01-21 14:53:30 -07001088 case ACLK_VIO:
1089 case ACLK_HDCP:
1090 case ACLK_GIC_PRE:
1091 case PCLK_DDR:
Jonas Karlman4d106982024-05-01 16:22:19 +00001092 case ACLK_VDU:
Simon Glassd27b3172019-01-21 14:53:30 -07001093 return 0;
Kever Yangca19eac2016-07-29 10:35:25 +08001094 default:
Simon Glassd27b3172019-01-21 14:53:30 -07001095 log_debug("Unknown clock %lu\n", clk->id);
Kever Yangca19eac2016-07-29 10:35:25 +08001096 return -ENOENT;
1097 }
1098
1099 return ret;
1100}
1101
Jagan Tekibef02a32019-07-15 23:51:10 +05301102static int __maybe_unused rk3399_gmac_set_parent(struct clk *clk,
1103 struct clk *parent)
Philipp Tomsichf4ba6ed2018-01-08 13:11:01 +01001104{
1105 struct rk3399_clk_priv *priv = dev_get_priv(clk->dev);
1106 const char *clock_output_name;
1107 int ret;
1108
1109 /*
1110 * If the requested parent is in the same clock-controller and
1111 * the id is SCLK_MAC ("clk_gmac"), switch to the internal clock.
1112 */
Jagan Tekibef02a32019-07-15 23:51:10 +05301113 if (parent->dev == clk->dev && parent->id == SCLK_MAC) {
Philipp Tomsichf4ba6ed2018-01-08 13:11:01 +01001114 debug("%s: switching RGMII to SCLK_MAC\n", __func__);
1115 rk_clrreg(&priv->cru->clksel_con[19], BIT(4));
1116 return 0;
1117 }
1118
1119 /*
1120 * Otherwise, we need to check the clock-output-names of the
1121 * requested parent to see if the requested id is "clkin_gmac".
1122 */
1123 ret = dev_read_string_index(parent->dev, "clock-output-names",
1124 parent->id, &clock_output_name);
1125 if (ret < 0)
1126 return -ENODATA;
1127
1128 /* If this is "clkin_gmac", switch to the external clock input */
1129 if (!strcmp(clock_output_name, "clkin_gmac")) {
1130 debug("%s: switching RGMII to CLKIN\n", __func__);
1131 rk_setreg(&priv->cru->clksel_con[19], BIT(4));
1132 return 0;
1133 }
1134
Jonas Karlman1f662832024-05-01 16:22:20 +00001135 return -EINVAL;
1136}
1137
1138static int __maybe_unused rk3399_pciephy_set_parent(struct clk *clk,
1139 struct clk *parent)
1140{
1141 struct rk3399_clk_priv *priv = dev_get_priv(clk->dev);
1142 const char *clock_output_name;
1143 int ret;
1144
1145 if (parent->dev == clk->dev && parent->id == SCLK_PCIEPHY_REF100M) {
1146 rk_setreg(&priv->cru->clksel_con[18], BIT(10));
1147 return 0;
1148 }
1149
1150 ret = dev_read_string_index(parent->dev, "clock-output-names",
1151 parent->id, &clock_output_name);
1152 if (ret < 0)
1153 return -ENODATA;
1154
1155 if (!strcmp(clock_output_name, "xin24m")) {
1156 rk_clrreg(&priv->cru->clksel_con[18], BIT(10));
1157 return 0;
1158 }
1159
Philipp Tomsichf4ba6ed2018-01-08 13:11:01 +01001160 return -EINVAL;
1161}
1162
Jagan Tekibef02a32019-07-15 23:51:10 +05301163static int __maybe_unused rk3399_clk_set_parent(struct clk *clk,
1164 struct clk *parent)
Philipp Tomsichf4ba6ed2018-01-08 13:11:01 +01001165{
1166 switch (clk->id) {
1167 case SCLK_RMII_SRC:
1168 return rk3399_gmac_set_parent(clk, parent);
Jonas Karlman1f662832024-05-01 16:22:20 +00001169 case SCLK_PCIEPHY_REF:
1170 return rk3399_pciephy_set_parent(clk, parent);
Philipp Tomsichf4ba6ed2018-01-08 13:11:01 +01001171 }
1172
1173 debug("%s: unsupported clk %ld\n", __func__, clk->id);
1174 return -ENOENT;
1175}
1176
Jagan Teki6c7f14a2020-05-09 22:26:19 +05301177static int rk3399_clk_enable(struct clk *clk)
1178{
1179 struct rk3399_clk_priv *priv = dev_get_priv(clk->dev);
1180
1181 switch (clk->id) {
1182 case SCLK_MAC:
1183 rk_clrreg(&priv->cru->clkgate_con[5], BIT(5));
1184 break;
1185 case SCLK_MAC_RX:
1186 rk_clrreg(&priv->cru->clkgate_con[5], BIT(8));
1187 break;
1188 case SCLK_MAC_TX:
1189 rk_clrreg(&priv->cru->clkgate_con[5], BIT(9));
1190 break;
1191 case SCLK_MACREF:
1192 rk_clrreg(&priv->cru->clkgate_con[5], BIT(7));
1193 break;
1194 case SCLK_MACREF_OUT:
1195 rk_clrreg(&priv->cru->clkgate_con[5], BIT(6));
1196 break;
Jagan Tekia5915372020-05-26 11:32:05 +08001197 case SCLK_USB2PHY0_REF:
1198 rk_clrreg(&priv->cru->clkgate_con[6], BIT(5));
1199 break;
1200 case SCLK_USB2PHY1_REF:
1201 rk_clrreg(&priv->cru->clkgate_con[6], BIT(6));
1202 break;
Jagan Teki6c7f14a2020-05-09 22:26:19 +05301203 case ACLK_GMAC:
1204 rk_clrreg(&priv->cru->clkgate_con[32], BIT(0));
1205 break;
1206 case PCLK_GMAC:
1207 rk_clrreg(&priv->cru->clkgate_con[32], BIT(2));
1208 break;
1209 case SCLK_USB3OTG0_REF:
1210 rk_clrreg(&priv->cru->clkgate_con[12], BIT(1));
1211 break;
1212 case SCLK_USB3OTG1_REF:
1213 rk_clrreg(&priv->cru->clkgate_con[12], BIT(2));
1214 break;
1215 case SCLK_USB3OTG0_SUSPEND:
1216 rk_clrreg(&priv->cru->clkgate_con[12], BIT(3));
1217 break;
1218 case SCLK_USB3OTG1_SUSPEND:
1219 rk_clrreg(&priv->cru->clkgate_con[12], BIT(4));
1220 break;
1221 case ACLK_USB3OTG0:
1222 rk_clrreg(&priv->cru->clkgate_con[30], BIT(1));
1223 break;
1224 case ACLK_USB3OTG1:
1225 rk_clrreg(&priv->cru->clkgate_con[30], BIT(2));
1226 break;
1227 case ACLK_USB3_RKSOC_AXI_PERF:
1228 rk_clrreg(&priv->cru->clkgate_con[30], BIT(3));
1229 break;
1230 case ACLK_USB3:
1231 rk_clrreg(&priv->cru->clkgate_con[12], BIT(0));
1232 break;
1233 case ACLK_USB3_GRF:
1234 rk_clrreg(&priv->cru->clkgate_con[30], BIT(4));
1235 break;
1236 case HCLK_HOST0:
1237 rk_clrreg(&priv->cru->clksel_con[20], BIT(5));
1238 break;
1239 case HCLK_HOST0_ARB:
1240 rk_clrreg(&priv->cru->clksel_con[20], BIT(6));
1241 break;
1242 case HCLK_HOST1:
1243 rk_clrreg(&priv->cru->clksel_con[20], BIT(7));
1244 break;
1245 case HCLK_HOST1_ARB:
1246 rk_clrreg(&priv->cru->clksel_con[20], BIT(8));
1247 break;
Jagan Teki055c2182020-05-26 11:32:07 +08001248 case SCLK_UPHY0_TCPDPHY_REF:
1249 rk_clrreg(&priv->cru->clkgate_con[13], BIT(4));
1250 break;
1251 case SCLK_UPHY0_TCPDCORE:
1252 rk_clrreg(&priv->cru->clkgate_con[13], BIT(5));
1253 break;
1254 case SCLK_UPHY1_TCPDPHY_REF:
1255 rk_clrreg(&priv->cru->clkgate_con[13], BIT(6));
1256 break;
1257 case SCLK_UPHY1_TCPDCORE:
1258 rk_clrreg(&priv->cru->clkgate_con[13], BIT(7));
1259 break;
Jagan Teki70c54ee2020-05-09 22:26:20 +05301260 case SCLK_PCIEPHY_REF:
Jonas Karlman1f662832024-05-01 16:22:20 +00001261 if (readl(&priv->cru->clksel_con[18]) & BIT(10))
1262 rk_clrreg(&priv->cru->clkgate_con[12], BIT(6));
Jagan Teki70c54ee2020-05-09 22:26:20 +05301263 break;
Jagan Teki6c7f14a2020-05-09 22:26:19 +05301264 default:
1265 debug("%s: unsupported clk %ld\n", __func__, clk->id);
1266 return -ENOENT;
1267 }
1268
1269 return 0;
1270}
1271
1272static int rk3399_clk_disable(struct clk *clk)
1273{
1274 struct rk3399_clk_priv *priv = dev_get_priv(clk->dev);
1275
1276 switch (clk->id) {
1277 case SCLK_MAC:
1278 rk_setreg(&priv->cru->clkgate_con[5], BIT(5));
1279 break;
1280 case SCLK_MAC_RX:
1281 rk_setreg(&priv->cru->clkgate_con[5], BIT(8));
1282 break;
1283 case SCLK_MAC_TX:
1284 rk_setreg(&priv->cru->clkgate_con[5], BIT(9));
1285 break;
1286 case SCLK_MACREF:
1287 rk_setreg(&priv->cru->clkgate_con[5], BIT(7));
1288 break;
1289 case SCLK_MACREF_OUT:
1290 rk_setreg(&priv->cru->clkgate_con[5], BIT(6));
1291 break;
Jagan Tekia5915372020-05-26 11:32:05 +08001292 case SCLK_USB2PHY0_REF:
1293 rk_setreg(&priv->cru->clkgate_con[6], BIT(5));
1294 break;
1295 case SCLK_USB2PHY1_REF:
1296 rk_setreg(&priv->cru->clkgate_con[6], BIT(6));
1297 break;
Jagan Teki6c7f14a2020-05-09 22:26:19 +05301298 case ACLK_GMAC:
1299 rk_setreg(&priv->cru->clkgate_con[32], BIT(0));
1300 break;
1301 case PCLK_GMAC:
1302 rk_setreg(&priv->cru->clkgate_con[32], BIT(2));
1303 break;
1304 case SCLK_USB3OTG0_REF:
1305 rk_setreg(&priv->cru->clkgate_con[12], BIT(1));
1306 break;
1307 case SCLK_USB3OTG1_REF:
1308 rk_setreg(&priv->cru->clkgate_con[12], BIT(2));
1309 break;
1310 case SCLK_USB3OTG0_SUSPEND:
1311 rk_setreg(&priv->cru->clkgate_con[12], BIT(3));
1312 break;
1313 case SCLK_USB3OTG1_SUSPEND:
1314 rk_setreg(&priv->cru->clkgate_con[12], BIT(4));
1315 break;
1316 case ACLK_USB3OTG0:
1317 rk_setreg(&priv->cru->clkgate_con[30], BIT(1));
1318 break;
1319 case ACLK_USB3OTG1:
1320 rk_setreg(&priv->cru->clkgate_con[30], BIT(2));
1321 break;
1322 case ACLK_USB3_RKSOC_AXI_PERF:
1323 rk_setreg(&priv->cru->clkgate_con[30], BIT(3));
1324 break;
1325 case ACLK_USB3:
1326 rk_setreg(&priv->cru->clkgate_con[12], BIT(0));
1327 break;
1328 case ACLK_USB3_GRF:
1329 rk_setreg(&priv->cru->clkgate_con[30], BIT(4));
1330 break;
1331 case HCLK_HOST0:
1332 rk_setreg(&priv->cru->clksel_con[20], BIT(5));
1333 break;
1334 case HCLK_HOST0_ARB:
1335 rk_setreg(&priv->cru->clksel_con[20], BIT(6));
1336 break;
1337 case HCLK_HOST1:
1338 rk_setreg(&priv->cru->clksel_con[20], BIT(7));
1339 break;
1340 case HCLK_HOST1_ARB:
1341 rk_setreg(&priv->cru->clksel_con[20], BIT(8));
1342 break;
Jagan Teki055c2182020-05-26 11:32:07 +08001343 case SCLK_UPHY0_TCPDPHY_REF:
1344 rk_setreg(&priv->cru->clkgate_con[13], BIT(4));
1345 break;
1346 case SCLK_UPHY0_TCPDCORE:
1347 rk_setreg(&priv->cru->clkgate_con[13], BIT(5));
1348 break;
1349 case SCLK_UPHY1_TCPDPHY_REF:
1350 rk_setreg(&priv->cru->clkgate_con[13], BIT(6));
1351 break;
1352 case SCLK_UPHY1_TCPDCORE:
1353 rk_setreg(&priv->cru->clkgate_con[13], BIT(7));
1354 break;
Jagan Teki70c54ee2020-05-09 22:26:20 +05301355 case SCLK_PCIEPHY_REF:
Jonas Karlman1f662832024-05-01 16:22:20 +00001356 if (readl(&priv->cru->clksel_con[18]) & BIT(10))
1357 rk_setreg(&priv->cru->clkgate_con[12], BIT(6));
Jagan Teki70c54ee2020-05-09 22:26:20 +05301358 break;
Jagan Teki6c7f14a2020-05-09 22:26:19 +05301359 default:
1360 debug("%s: unsupported clk %ld\n", __func__, clk->id);
1361 return -ENOENT;
1362 }
1363
1364 return 0;
1365}
1366
Kever Yangca19eac2016-07-29 10:35:25 +08001367static struct clk_ops rk3399_clk_ops = {
1368 .get_rate = rk3399_clk_get_rate,
1369 .set_rate = rk3399_clk_set_rate,
Simon Glass3580f6d2021-08-07 07:24:03 -06001370#if CONFIG_IS_ENABLED(OF_REAL)
Philipp Tomsichf4ba6ed2018-01-08 13:11:01 +01001371 .set_parent = rk3399_clk_set_parent,
Philipp Tomsich6dd2fb42018-01-25 15:27:10 +01001372#endif
Jagan Teki6c7f14a2020-05-09 22:26:19 +05301373 .enable = rk3399_clk_enable,
1374 .disable = rk3399_clk_disable,
Kever Yangca19eac2016-07-29 10:35:25 +08001375};
1376
Jagan Teki783acfd2020-01-09 14:22:17 +05301377static void rkclk_init(struct rockchip_cru *cru)
Kever Yang05a14b02017-10-12 15:27:29 +08001378{
1379 u32 aclk_div;
1380 u32 hclk_div;
1381 u32 pclk_div;
1382
Christoph Muellner25c7ba92018-11-30 20:32:48 +01001383 rk3399_configure_cpu_l(cru, APLL_L_600_MHZ);
1384 rk3399_configure_cpu_b(cru, APLL_B_600_MHZ);
Kever Yang05a14b02017-10-12 15:27:29 +08001385 /*
1386 * some cru registers changed by bootrom, we'd better reset them to
1387 * reset/default values described in TRM to avoid confusion in kernel.
1388 * Please consider these three lines as a fix of bootrom bug.
1389 */
1390 rk_clrsetreg(&cru->clksel_con[12], 0xffff, 0x4101);
1391 rk_clrsetreg(&cru->clksel_con[19], 0xffff, 0x033f);
1392 rk_clrsetreg(&cru->clksel_con[56], 0x0003, 0x0003);
1393
1394 /* configure gpll cpll */
1395 rkclk_set_pll(&cru->gpll_con[0], &gpll_init_cfg);
1396 rkclk_set_pll(&cru->cpll_con[0], &cpll_init_cfg);
1397
1398 /* configure perihp aclk, hclk, pclk */
1399 aclk_div = GPLL_HZ / PERIHP_ACLK_HZ - 1;
1400 assert((aclk_div + 1) * PERIHP_ACLK_HZ == GPLL_HZ && aclk_div < 0x1f);
1401
1402 hclk_div = PERIHP_ACLK_HZ / PERIHP_HCLK_HZ - 1;
1403 assert((hclk_div + 1) * PERIHP_HCLK_HZ ==
1404 PERIHP_ACLK_HZ && (hclk_div < 0x4));
1405
1406 pclk_div = PERIHP_ACLK_HZ / PERIHP_PCLK_HZ - 1;
1407 assert((pclk_div + 1) * PERIHP_PCLK_HZ ==
1408 PERIHP_ACLK_HZ && (pclk_div < 0x7));
1409
1410 rk_clrsetreg(&cru->clksel_con[14],
1411 PCLK_PERIHP_DIV_CON_MASK | HCLK_PERIHP_DIV_CON_MASK |
1412 ACLK_PERIHP_PLL_SEL_MASK | ACLK_PERIHP_DIV_CON_MASK,
1413 pclk_div << PCLK_PERIHP_DIV_CON_SHIFT |
1414 hclk_div << HCLK_PERIHP_DIV_CON_SHIFT |
1415 ACLK_PERIHP_PLL_SEL_GPLL << ACLK_PERIHP_PLL_SEL_SHIFT |
1416 aclk_div << ACLK_PERIHP_DIV_CON_SHIFT);
1417
1418 /* configure perilp0 aclk, hclk, pclk */
1419 aclk_div = GPLL_HZ / PERILP0_ACLK_HZ - 1;
1420 assert((aclk_div + 1) * PERILP0_ACLK_HZ == GPLL_HZ && aclk_div < 0x1f);
1421
1422 hclk_div = PERILP0_ACLK_HZ / PERILP0_HCLK_HZ - 1;
1423 assert((hclk_div + 1) * PERILP0_HCLK_HZ ==
1424 PERILP0_ACLK_HZ && (hclk_div < 0x4));
1425
1426 pclk_div = PERILP0_ACLK_HZ / PERILP0_PCLK_HZ - 1;
1427 assert((pclk_div + 1) * PERILP0_PCLK_HZ ==
1428 PERILP0_ACLK_HZ && (pclk_div < 0x7));
1429
1430 rk_clrsetreg(&cru->clksel_con[23],
1431 PCLK_PERILP0_DIV_CON_MASK | HCLK_PERILP0_DIV_CON_MASK |
1432 ACLK_PERILP0_PLL_SEL_MASK | ACLK_PERILP0_DIV_CON_MASK,
1433 pclk_div << PCLK_PERILP0_DIV_CON_SHIFT |
1434 hclk_div << HCLK_PERILP0_DIV_CON_SHIFT |
1435 ACLK_PERILP0_PLL_SEL_GPLL << ACLK_PERILP0_PLL_SEL_SHIFT |
1436 aclk_div << ACLK_PERILP0_DIV_CON_SHIFT);
1437
1438 /* perilp1 hclk select gpll as source */
1439 hclk_div = GPLL_HZ / PERILP1_HCLK_HZ - 1;
1440 assert((hclk_div + 1) * PERILP1_HCLK_HZ ==
1441 GPLL_HZ && (hclk_div < 0x1f));
1442
1443 pclk_div = PERILP1_HCLK_HZ / PERILP1_HCLK_HZ - 1;
1444 assert((pclk_div + 1) * PERILP1_HCLK_HZ ==
1445 PERILP1_HCLK_HZ && (hclk_div < 0x7));
1446
1447 rk_clrsetreg(&cru->clksel_con[25],
1448 PCLK_PERILP1_DIV_CON_MASK | HCLK_PERILP1_DIV_CON_MASK |
1449 HCLK_PERILP1_PLL_SEL_MASK,
1450 pclk_div << PCLK_PERILP1_DIV_CON_SHIFT |
1451 hclk_div << HCLK_PERILP1_DIV_CON_SHIFT |
1452 HCLK_PERILP1_PLL_SEL_GPLL << HCLK_PERILP1_PLL_SEL_SHIFT);
1453}
Kever Yang05a14b02017-10-12 15:27:29 +08001454
Kever Yangca19eac2016-07-29 10:35:25 +08001455static int rk3399_clk_probe(struct udevice *dev)
1456{
1457 struct rk3399_clk_priv *priv = dev_get_priv(dev);
Alper Nebi Yasak66327ce2020-10-28 00:15:10 +03001458 bool init_clocks = false;
Kever Yangca19eac2016-07-29 10:35:25 +08001459
Kever Yange1980532017-02-13 17:38:56 +08001460#if CONFIG_IS_ENABLED(OF_PLATDATA)
Simon Glassfa20e932020-12-03 16:55:20 -07001461 struct rk3399_clk_plat *plat = dev_get_plat(dev);
Kever Yangca19eac2016-07-29 10:35:25 +08001462
Simon Glass1b1fe412017-08-29 14:15:50 -06001463 priv->cru = map_sysmem(plat->dtd.reg[0], plat->dtd.reg[1]);
Kever Yange1980532017-02-13 17:38:56 +08001464#endif
Alper Nebi Yasak66327ce2020-10-28 00:15:10 +03001465
1466#if defined(CONFIG_SPL_BUILD)
1467 init_clocks = true;
1468#elif CONFIG_IS_ENABLED(HANDOFF)
1469 if (!(gd->flags & GD_FLG_RELOC)) {
1470 if (!(gd->spl_handoff))
1471 init_clocks = true;
1472 }
Kever Yange1980532017-02-13 17:38:56 +08001473#endif
Alper Nebi Yasak66327ce2020-10-28 00:15:10 +03001474
1475 if (init_clocks)
1476 rkclk_init(priv->cru);
1477
Kever Yangca19eac2016-07-29 10:35:25 +08001478 return 0;
1479}
1480
Simon Glassaad29ae2020-12-03 16:55:21 -07001481static int rk3399_clk_of_to_plat(struct udevice *dev)
Kever Yangca19eac2016-07-29 10:35:25 +08001482{
Simon Glass6d70ba02021-08-07 07:24:06 -06001483 if (CONFIG_IS_ENABLED(OF_REAL)) {
1484 struct rk3399_clk_priv *priv = dev_get_priv(dev);
Kever Yangca19eac2016-07-29 10:35:25 +08001485
Simon Glass6d70ba02021-08-07 07:24:06 -06001486 priv->cru = dev_read_addr_ptr(dev);
1487 }
1488
Kever Yangca19eac2016-07-29 10:35:25 +08001489 return 0;
1490}
1491
1492static int rk3399_clk_bind(struct udevice *dev)
1493{
1494 int ret;
Kever Yang4fbb6c22017-11-03 15:16:13 +08001495 struct udevice *sys_child;
1496 struct sysreset_reg *priv;
Kever Yangca19eac2016-07-29 10:35:25 +08001497
1498 /* The reset driver does not have a device node, so bind it here */
Kever Yang4fbb6c22017-11-03 15:16:13 +08001499 ret = device_bind_driver(dev, "rockchip_sysreset", "sysreset",
1500 &sys_child);
1501 if (ret) {
1502 debug("Warning: No sysreset driver: ret=%d\n", ret);
1503 } else {
1504 priv = malloc(sizeof(struct sysreset_reg));
Jagan Teki783acfd2020-01-09 14:22:17 +05301505 priv->glb_srst_fst_value = offsetof(struct rockchip_cru,
Kever Yang4fbb6c22017-11-03 15:16:13 +08001506 glb_srst_fst_value);
Jagan Teki783acfd2020-01-09 14:22:17 +05301507 priv->glb_srst_snd_value = offsetof(struct rockchip_cru,
Kever Yang4fbb6c22017-11-03 15:16:13 +08001508 glb_srst_snd_value);
Simon Glass95588622020-12-22 19:30:28 -07001509 dev_set_priv(sys_child, priv);
Kever Yang4fbb6c22017-11-03 15:16:13 +08001510 }
Kever Yangca19eac2016-07-29 10:35:25 +08001511
Heiko Stuebner416f8d32019-11-09 00:06:30 +01001512#if CONFIG_IS_ENABLED(RESET_ROCKCHIP)
Jagan Teki783acfd2020-01-09 14:22:17 +05301513 ret = offsetof(struct rockchip_cru, softrst_con[0]);
Elaine Zhang432976f2017-12-19 18:22:38 +08001514 ret = rockchip_reset_bind(dev, ret, 21);
1515 if (ret)
Eugen Hristevf1798262023-04-11 10:17:56 +03001516 debug("Warning: software reset driver bind failed\n");
Elaine Zhang432976f2017-12-19 18:22:38 +08001517#endif
1518
Kever Yangca19eac2016-07-29 10:35:25 +08001519 return 0;
1520}
1521
1522static const struct udevice_id rk3399_clk_ids[] = {
1523 { .compatible = "rockchip,rk3399-cru" },
1524 { }
1525};
1526
1527U_BOOT_DRIVER(clk_rk3399) = {
Kever Yange1980532017-02-13 17:38:56 +08001528 .name = "rockchip_rk3399_cru",
Kever Yangca19eac2016-07-29 10:35:25 +08001529 .id = UCLASS_CLK,
1530 .of_match = rk3399_clk_ids,
Simon Glass8a2b47f2020-12-03 16:55:17 -07001531 .priv_auto = sizeof(struct rk3399_clk_priv),
Simon Glassaad29ae2020-12-03 16:55:21 -07001532 .of_to_plat = rk3399_clk_of_to_plat,
Kever Yangca19eac2016-07-29 10:35:25 +08001533 .ops = &rk3399_clk_ops,
1534 .bind = rk3399_clk_bind,
1535 .probe = rk3399_clk_probe,
Kever Yange1980532017-02-13 17:38:56 +08001536#if CONFIG_IS_ENABLED(OF_PLATDATA)
Simon Glass71fa5b42020-12-03 16:55:18 -07001537 .plat_auto = sizeof(struct rk3399_clk_plat),
Kever Yange1980532017-02-13 17:38:56 +08001538#endif
Kever Yangca19eac2016-07-29 10:35:25 +08001539};
Kever Yange54d26a2016-08-12 17:47:15 +08001540
1541static ulong rk3399_i2c_get_pmuclk(struct rk3399_pmucru *pmucru, ulong clk_id)
1542{
1543 u32 div, con;
1544
1545 switch (clk_id) {
1546 case SCLK_I2C0_PMU:
1547 con = readl(&pmucru->pmucru_clksel[2]);
1548 div = I2C_CLK_DIV_VALUE(con, 0);
1549 break;
1550 case SCLK_I2C4_PMU:
1551 con = readl(&pmucru->pmucru_clksel[3]);
1552 div = I2C_CLK_DIV_VALUE(con, 4);
1553 break;
1554 case SCLK_I2C8_PMU:
1555 con = readl(&pmucru->pmucru_clksel[2]);
1556 div = I2C_CLK_DIV_VALUE(con, 8);
1557 break;
1558 default:
1559 printf("do not support this i2c bus\n");
1560 return -EINVAL;
1561 }
1562
1563 return DIV_TO_RATE(PPLL_HZ, div);
1564}
1565
1566static ulong rk3399_i2c_set_pmuclk(struct rk3399_pmucru *pmucru, ulong clk_id,
1567 uint hz)
1568{
1569 int src_clk_div;
1570
1571 src_clk_div = PPLL_HZ / hz;
1572 assert(src_clk_div - 1 < 127);
1573
1574 switch (clk_id) {
1575 case SCLK_I2C0_PMU:
1576 rk_clrsetreg(&pmucru->pmucru_clksel[2], I2C_PMUCLK_REG_MASK(0),
1577 I2C_PMUCLK_REG_VALUE(0, src_clk_div));
1578 break;
1579 case SCLK_I2C4_PMU:
1580 rk_clrsetreg(&pmucru->pmucru_clksel[3], I2C_PMUCLK_REG_MASK(4),
1581 I2C_PMUCLK_REG_VALUE(4, src_clk_div));
1582 break;
1583 case SCLK_I2C8_PMU:
1584 rk_clrsetreg(&pmucru->pmucru_clksel[2], I2C_PMUCLK_REG_MASK(8),
1585 I2C_PMUCLK_REG_VALUE(8, src_clk_div));
1586 break;
1587 default:
1588 printf("do not support this i2c bus\n");
1589 return -EINVAL;
1590 }
1591
1592 return DIV_TO_RATE(PPLL_HZ, src_clk_div);
1593}
1594
1595static ulong rk3399_pwm_get_clk(struct rk3399_pmucru *pmucru)
1596{
1597 u32 div, con;
1598
1599 /* PWM closk rate is same as pclk_pmu */
1600 con = readl(&pmucru->pmucru_clksel[0]);
1601 div = con & PMU_PCLK_DIV_CON_MASK;
1602
1603 return DIV_TO_RATE(PPLL_HZ, div);
1604}
1605
1606static ulong rk3399_pmuclk_get_rate(struct clk *clk)
1607{
1608 struct rk3399_pmuclk_priv *priv = dev_get_priv(clk->dev);
1609 ulong rate = 0;
1610
1611 switch (clk->id) {
Philipp Tomsich932908c2018-02-23 17:36:41 +01001612 case PLL_PPLL:
1613 return PPLL_HZ;
Kever Yange54d26a2016-08-12 17:47:15 +08001614 case PCLK_RKPWM_PMU:
Jack Mitchell4ef38ce2020-09-17 10:42:06 +01001615 case PCLK_WDT_M0_PMU:
Kever Yange54d26a2016-08-12 17:47:15 +08001616 rate = rk3399_pwm_get_clk(priv->pmucru);
1617 break;
1618 case SCLK_I2C0_PMU:
1619 case SCLK_I2C4_PMU:
1620 case SCLK_I2C8_PMU:
1621 rate = rk3399_i2c_get_pmuclk(priv->pmucru, clk->id);
1622 break;
1623 default:
1624 return -ENOENT;
1625 }
1626
1627 return rate;
1628}
1629
1630static ulong rk3399_pmuclk_set_rate(struct clk *clk, ulong rate)
1631{
1632 struct rk3399_pmuclk_priv *priv = dev_get_priv(clk->dev);
1633 ulong ret = 0;
1634
1635 switch (clk->id) {
Philipp Tomsich932908c2018-02-23 17:36:41 +01001636 case PLL_PPLL:
1637 /*
1638 * This has already been set up and we don't want/need
1639 * to change it here. Accept the request though, as the
1640 * device-tree has this in an 'assigned-clocks' list.
1641 */
1642 return PPLL_HZ;
Kever Yange54d26a2016-08-12 17:47:15 +08001643 case SCLK_I2C0_PMU:
1644 case SCLK_I2C4_PMU:
1645 case SCLK_I2C8_PMU:
1646 ret = rk3399_i2c_set_pmuclk(priv->pmucru, clk->id, rate);
1647 break;
1648 default:
1649 return -ENOENT;
1650 }
1651
1652 return ret;
1653}
1654
1655static struct clk_ops rk3399_pmuclk_ops = {
1656 .get_rate = rk3399_pmuclk_get_rate,
1657 .set_rate = rk3399_pmuclk_set_rate,
1658};
1659
Kever Yange1980532017-02-13 17:38:56 +08001660#ifndef CONFIG_SPL_BUILD
Kever Yange54d26a2016-08-12 17:47:15 +08001661static void pmuclk_init(struct rk3399_pmucru *pmucru)
1662{
1663 u32 pclk_div;
1664
1665 /* configure pmu pll(ppll) */
1666 rkclk_set_pll(&pmucru->ppll_con[0], &ppll_init_cfg);
1667
1668 /* configure pmu pclk */
1669 pclk_div = PPLL_HZ / PMU_PCLK_HZ - 1;
Kever Yange54d26a2016-08-12 17:47:15 +08001670 rk_clrsetreg(&pmucru->pmucru_clksel[0],
1671 PMU_PCLK_DIV_CON_MASK,
1672 pclk_div << PMU_PCLK_DIV_CON_SHIFT);
1673}
Kever Yange1980532017-02-13 17:38:56 +08001674#endif
Kever Yange54d26a2016-08-12 17:47:15 +08001675
1676static int rk3399_pmuclk_probe(struct udevice *dev)
1677{
Philipp Tomsichcf0a4ba2017-03-24 19:24:24 +01001678#if CONFIG_IS_ENABLED(OF_PLATDATA) || !defined(CONFIG_SPL_BUILD)
Kever Yange54d26a2016-08-12 17:47:15 +08001679 struct rk3399_pmuclk_priv *priv = dev_get_priv(dev);
Philipp Tomsichcf0a4ba2017-03-24 19:24:24 +01001680#endif
Kever Yange54d26a2016-08-12 17:47:15 +08001681
Kever Yange1980532017-02-13 17:38:56 +08001682#if CONFIG_IS_ENABLED(OF_PLATDATA)
Simon Glassfa20e932020-12-03 16:55:20 -07001683 struct rk3399_pmuclk_plat *plat = dev_get_plat(dev);
Kever Yange54d26a2016-08-12 17:47:15 +08001684
Simon Glass1b1fe412017-08-29 14:15:50 -06001685 priv->pmucru = map_sysmem(plat->dtd.reg[0], plat->dtd.reg[1]);
Kever Yange1980532017-02-13 17:38:56 +08001686#endif
1687
1688#ifndef CONFIG_SPL_BUILD
1689 pmuclk_init(priv->pmucru);
1690#endif
Kever Yange54d26a2016-08-12 17:47:15 +08001691 return 0;
1692}
1693
Simon Glassaad29ae2020-12-03 16:55:21 -07001694static int rk3399_pmuclk_of_to_plat(struct udevice *dev)
Kever Yange54d26a2016-08-12 17:47:15 +08001695{
Simon Glass6d70ba02021-08-07 07:24:06 -06001696 if (CONFIG_IS_ENABLED(OF_REAL)) {
1697 struct rk3399_pmuclk_priv *priv = dev_get_priv(dev);
Kever Yange54d26a2016-08-12 17:47:15 +08001698
Simon Glass6d70ba02021-08-07 07:24:06 -06001699 priv->pmucru = dev_read_addr_ptr(dev);
1700 }
1701
Kever Yange54d26a2016-08-12 17:47:15 +08001702 return 0;
1703}
1704
Elaine Zhang432976f2017-12-19 18:22:38 +08001705static int rk3399_pmuclk_bind(struct udevice *dev)
1706{
Alper Nebi Yasak5de2f332020-10-05 09:57:29 +03001707#if CONFIG_IS_ENABLED(RESET_ROCKCHIP)
Elaine Zhang432976f2017-12-19 18:22:38 +08001708 int ret;
1709
1710 ret = offsetof(struct rk3399_pmucru, pmucru_softrst_con[0]);
1711 ret = rockchip_reset_bind(dev, ret, 2);
1712 if (ret)
Eugen Hristevf1798262023-04-11 10:17:56 +03001713 debug("Warning: software reset driver bind failed\n");
Elaine Zhang432976f2017-12-19 18:22:38 +08001714#endif
1715 return 0;
1716}
1717
Kever Yange54d26a2016-08-12 17:47:15 +08001718static const struct udevice_id rk3399_pmuclk_ids[] = {
1719 { .compatible = "rockchip,rk3399-pmucru" },
1720 { }
1721};
1722
Simon Glassd1dfea72016-10-01 20:04:51 -06001723U_BOOT_DRIVER(rockchip_rk3399_pmuclk) = {
Kever Yange1980532017-02-13 17:38:56 +08001724 .name = "rockchip_rk3399_pmucru",
Kever Yange54d26a2016-08-12 17:47:15 +08001725 .id = UCLASS_CLK,
1726 .of_match = rk3399_pmuclk_ids,
Simon Glass8a2b47f2020-12-03 16:55:17 -07001727 .priv_auto = sizeof(struct rk3399_pmuclk_priv),
Simon Glassaad29ae2020-12-03 16:55:21 -07001728 .of_to_plat = rk3399_pmuclk_of_to_plat,
Kever Yange54d26a2016-08-12 17:47:15 +08001729 .ops = &rk3399_pmuclk_ops,
1730 .probe = rk3399_pmuclk_probe,
Elaine Zhang432976f2017-12-19 18:22:38 +08001731 .bind = rk3399_pmuclk_bind,
Kever Yange1980532017-02-13 17:38:56 +08001732#if CONFIG_IS_ENABLED(OF_PLATDATA)
Simon Glass71fa5b42020-12-03 16:55:18 -07001733 .plat_auto = sizeof(struct rk3399_pmuclk_plat),
Kever Yange1980532017-02-13 17:38:56 +08001734#endif
Kever Yange54d26a2016-08-12 17:47:15 +08001735};