1*4882a593Smuzhiyun /*
2*4882a593Smuzhiyun * (C) Copyright 2017 Rockchip Electronics Co., Ltd
3*4882a593Smuzhiyun * Author: Andy Yan <andy.yan@rock-chips.com>
4*4882a593Smuzhiyun * (C) Copyright 2017 Theobroma Systems Design und Consulting GmbH
5*4882a593Smuzhiyun * SPDX-License-Identifier: GPL-2.0
6*4882a593Smuzhiyun */
7*4882a593Smuzhiyun
8*4882a593Smuzhiyun #include <common.h>
9*4882a593Smuzhiyun #include <clk-uclass.h>
10*4882a593Smuzhiyun #include <dm.h>
11*4882a593Smuzhiyun #include <dt-structs.h>
12*4882a593Smuzhiyun #include <errno.h>
13*4882a593Smuzhiyun #include <mapmem.h>
14*4882a593Smuzhiyun #include <syscon.h>
15*4882a593Smuzhiyun #include <bitfield.h>
16*4882a593Smuzhiyun #include <asm/arch/clock.h>
17*4882a593Smuzhiyun #include <asm/arch/cru_rk3368.h>
18*4882a593Smuzhiyun #include <asm/arch/hardware.h>
19*4882a593Smuzhiyun #include <asm/io.h>
20*4882a593Smuzhiyun #include <dm/lists.h>
21*4882a593Smuzhiyun #include <dt-bindings/clock/rk3368-cru.h>
22*4882a593Smuzhiyun
23*4882a593Smuzhiyun DECLARE_GLOBAL_DATA_PTR;
24*4882a593Smuzhiyun
25*4882a593Smuzhiyun #if CONFIG_IS_ENABLED(OF_PLATDATA)
26*4882a593Smuzhiyun struct rk3368_clk_plat {
27*4882a593Smuzhiyun struct dtd_rockchip_rk3368_cru dtd;
28*4882a593Smuzhiyun };
29*4882a593Smuzhiyun #endif
30*4882a593Smuzhiyun
31*4882a593Smuzhiyun struct pll_div {
32*4882a593Smuzhiyun ulong rate;
33*4882a593Smuzhiyun u32 nr;
34*4882a593Smuzhiyun u32 nf;
35*4882a593Smuzhiyun u32 no;
36*4882a593Smuzhiyun u32 nb;
37*4882a593Smuzhiyun };
38*4882a593Smuzhiyun
39*4882a593Smuzhiyun #define RK3368_PLL_RATE(_rate, _nr, _nf, _no, _nb) \
40*4882a593Smuzhiyun { \
41*4882a593Smuzhiyun .rate = _rate##U, \
42*4882a593Smuzhiyun .nr = _nr, \
43*4882a593Smuzhiyun .nf = _nf, \
44*4882a593Smuzhiyun .no = _no, \
45*4882a593Smuzhiyun .nb = _nb, \
46*4882a593Smuzhiyun }
47*4882a593Smuzhiyun
48*4882a593Smuzhiyun static struct pll_div rk3368_pll_rates[] = {
49*4882a593Smuzhiyun /* _mhz, _nr, _nf, _no, _nb */
50*4882a593Smuzhiyun RK3368_PLL_RATE(594000000, 1, 99, 4, 16),
51*4882a593Smuzhiyun RK3368_PLL_RATE(424200000, 5, 707, 8, 0),
52*4882a593Smuzhiyun RK3368_PLL_RATE(410000000, 3, 205, 4, 16),
53*4882a593Smuzhiyun };
54*4882a593Smuzhiyun
55*4882a593Smuzhiyun #define OSC_HZ (24 * 1000 * 1000)
56*4882a593Smuzhiyun #define APLL_L_HZ (800 * 1000 * 1000)
57*4882a593Smuzhiyun #define APLL_B_HZ (816 * 1000 * 1000)
58*4882a593Smuzhiyun #define GPLL_HZ (576 * 1000 * 1000)
59*4882a593Smuzhiyun #define CPLL_HZ (400 * 1000 * 1000)
60*4882a593Smuzhiyun #define NPLL_HZ (594 * 1000 * 1000)
61*4882a593Smuzhiyun
62*4882a593Smuzhiyun #define DIV_TO_RATE(input_rate, div) ((input_rate) / ((div) + 1))
63*4882a593Smuzhiyun
64*4882a593Smuzhiyun #if !defined(CONFIG_SPL_BUILD)
65*4882a593Smuzhiyun #define RK3368_CLK_DUMP(_id, _name, _iscru) \
66*4882a593Smuzhiyun { \
67*4882a593Smuzhiyun .id = _id, \
68*4882a593Smuzhiyun .name = _name, \
69*4882a593Smuzhiyun .is_cru = _iscru, \
70*4882a593Smuzhiyun }
71*4882a593Smuzhiyun
72*4882a593Smuzhiyun static const struct rk3368_clk_info clks_dump[] = {
73*4882a593Smuzhiyun RK3368_CLK_DUMP(PLL_APLLB, "apllb", true),
74*4882a593Smuzhiyun RK3368_CLK_DUMP(PLL_APLLL, "aplll", true),
75*4882a593Smuzhiyun RK3368_CLK_DUMP(PLL_DPLL, "dpll", true),
76*4882a593Smuzhiyun RK3368_CLK_DUMP(PLL_CPLL, "cpll", true),
77*4882a593Smuzhiyun RK3368_CLK_DUMP(PLL_GPLL, "gpll", true),
78*4882a593Smuzhiyun RK3368_CLK_DUMP(PLL_NPLL, "npll", true),
79*4882a593Smuzhiyun RK3368_CLK_DUMP(ARMCLKB, "armclkb", true),
80*4882a593Smuzhiyun RK3368_CLK_DUMP(ARMCLKL, "armclkl", true),
81*4882a593Smuzhiyun RK3368_CLK_DUMP(ACLK_BUS, "aclk_bus", true),
82*4882a593Smuzhiyun RK3368_CLK_DUMP(HCLK_BUS, "hclk_bus", true),
83*4882a593Smuzhiyun RK3368_CLK_DUMP(PCLK_BUS, "pclk_Bus", true),
84*4882a593Smuzhiyun RK3368_CLK_DUMP(ACLK_PERI, "aclk_peri", true),
85*4882a593Smuzhiyun RK3368_CLK_DUMP(HCLK_PERI, "hclk_peri", true),
86*4882a593Smuzhiyun RK3368_CLK_DUMP(PCLK_PERI, "pclk_peri", true),
87*4882a593Smuzhiyun };
88*4882a593Smuzhiyun #endif
89*4882a593Smuzhiyun
90*4882a593Smuzhiyun #define RK3368_CPUCLK_RATE(_rate, _aclk_div, _pclk_div) \
91*4882a593Smuzhiyun { \
92*4882a593Smuzhiyun .rate = _rate##U, \
93*4882a593Smuzhiyun .aclk_div = _aclk_div, \
94*4882a593Smuzhiyun .pclk_div = _pclk_div, \
95*4882a593Smuzhiyun }
96*4882a593Smuzhiyun
97*4882a593Smuzhiyun static struct rockchip_cpu_rate_table rk3368_cpu_rates[] = {
98*4882a593Smuzhiyun #if !defined(CONFIG_SPL_BUILD)
99*4882a593Smuzhiyun RK3368_CPUCLK_RATE(1200000000, 1, 5),
100*4882a593Smuzhiyun RK3368_CPUCLK_RATE(1008000000, 1, 5),
101*4882a593Smuzhiyun #endif
102*4882a593Smuzhiyun RK3368_CPUCLK_RATE(816000000, 1, 3),
103*4882a593Smuzhiyun RK3368_CPUCLK_RATE(600000000, 1, 3),
104*4882a593Smuzhiyun };
105*4882a593Smuzhiyun
106*4882a593Smuzhiyun #define PLL_DIVISORS(hz, _nr, _no) { \
107*4882a593Smuzhiyun .nr = _nr, .nf = (u32)((u64)hz * _nr * _no / OSC_HZ), .no = _no}; \
108*4882a593Smuzhiyun _Static_assert(((u64)hz * _nr * _no / OSC_HZ) * OSC_HZ /\
109*4882a593Smuzhiyun (_nr * _no) == hz, #hz "Hz cannot be hit with PLL " \
110*4882a593Smuzhiyun "divisors on line " __stringify(__LINE__));
111*4882a593Smuzhiyun
112*4882a593Smuzhiyun #if IS_ENABLED(CONFIG_SPL_BUILD) || IS_ENABLED(CONFIG_TPL_BUILD)
113*4882a593Smuzhiyun static const struct pll_div apll_l_init_cfg = PLL_DIVISORS(APLL_L_HZ, 12, 2);
114*4882a593Smuzhiyun static const struct pll_div apll_b_init_cfg = PLL_DIVISORS(APLL_B_HZ, 1, 2);
115*4882a593Smuzhiyun #if !defined(CONFIG_TPL_BUILD)
116*4882a593Smuzhiyun static const struct pll_div gpll_init_cfg = PLL_DIVISORS(GPLL_HZ, 1, 2);
117*4882a593Smuzhiyun static const struct pll_div cpll_init_cfg = PLL_DIVISORS(CPLL_HZ, 1, 6);
118*4882a593Smuzhiyun #endif
119*4882a593Smuzhiyun #endif
120*4882a593Smuzhiyun
121*4882a593Smuzhiyun static ulong rk3368_clk_get_rate(struct clk *clk);
122*4882a593Smuzhiyun
123*4882a593Smuzhiyun #define VCO_MAX_KHZ 2200000
124*4882a593Smuzhiyun #define VCO_MIN_KHZ 440000
125*4882a593Smuzhiyun #define FREF_MAX_KHZ 2200000
126*4882a593Smuzhiyun #define FREF_MIN_KHZ 269
127*4882a593Smuzhiyun #define PLL_LIMIT_FREQ 400000000
128*4882a593Smuzhiyun
rkclk_get_pll_config(ulong freq_hz)129*4882a593Smuzhiyun struct pll_div *rkclk_get_pll_config(ulong freq_hz)
130*4882a593Smuzhiyun {
131*4882a593Smuzhiyun unsigned int rate_count = ARRAY_SIZE(rk3368_pll_rates);
132*4882a593Smuzhiyun int i;
133*4882a593Smuzhiyun
134*4882a593Smuzhiyun for (i = 0; i < rate_count; i++) {
135*4882a593Smuzhiyun if (freq_hz == rk3368_pll_rates[i].rate)
136*4882a593Smuzhiyun return &rk3368_pll_rates[i];
137*4882a593Smuzhiyun }
138*4882a593Smuzhiyun return NULL;
139*4882a593Smuzhiyun }
140*4882a593Smuzhiyun
pll_para_config(ulong freq_hz,struct pll_div * div,uint * ext_div)141*4882a593Smuzhiyun static int pll_para_config(ulong freq_hz, struct pll_div *div, uint *ext_div)
142*4882a593Smuzhiyun {
143*4882a593Smuzhiyun struct pll_div *best_div = NULL;
144*4882a593Smuzhiyun uint ref_khz = OSC_HZ / 1000, nr, nf = 0;
145*4882a593Smuzhiyun uint fref_khz;
146*4882a593Smuzhiyun uint diff_khz, best_diff_khz;
147*4882a593Smuzhiyun const uint max_nr = 1 << 6, max_nf = 1 << 12, max_no = 1 << 4;
148*4882a593Smuzhiyun uint vco_khz;
149*4882a593Smuzhiyun uint no = 1;
150*4882a593Smuzhiyun uint freq_khz = freq_hz / 1000;
151*4882a593Smuzhiyun
152*4882a593Smuzhiyun if (!freq_hz) {
153*4882a593Smuzhiyun printf("%s: the frequency can not be 0 Hz\n", __func__);
154*4882a593Smuzhiyun return -EINVAL;
155*4882a593Smuzhiyun }
156*4882a593Smuzhiyun
157*4882a593Smuzhiyun no = DIV_ROUND_UP(VCO_MIN_KHZ, freq_khz);
158*4882a593Smuzhiyun if (ext_div) {
159*4882a593Smuzhiyun *ext_div = DIV_ROUND_UP(PLL_LIMIT_FREQ, freq_hz);
160*4882a593Smuzhiyun no = DIV_ROUND_UP(no, *ext_div);
161*4882a593Smuzhiyun }
162*4882a593Smuzhiyun
163*4882a593Smuzhiyun best_div = rkclk_get_pll_config(freq_hz * (*ext_div));
164*4882a593Smuzhiyun if (best_div) {
165*4882a593Smuzhiyun div->nr = best_div->nr;
166*4882a593Smuzhiyun div->nf = best_div->nf;
167*4882a593Smuzhiyun div->no = best_div->no;
168*4882a593Smuzhiyun div->nb = best_div->nb;
169*4882a593Smuzhiyun return 0;
170*4882a593Smuzhiyun }
171*4882a593Smuzhiyun
172*4882a593Smuzhiyun /* only even divisors (and 1) are supported */
173*4882a593Smuzhiyun if (no > 1)
174*4882a593Smuzhiyun no = DIV_ROUND_UP(no, 2) * 2;
175*4882a593Smuzhiyun
176*4882a593Smuzhiyun vco_khz = freq_khz * no;
177*4882a593Smuzhiyun if (ext_div)
178*4882a593Smuzhiyun vco_khz *= *ext_div;
179*4882a593Smuzhiyun
180*4882a593Smuzhiyun if (vco_khz < VCO_MIN_KHZ || vco_khz > VCO_MAX_KHZ || no > max_no) {
181*4882a593Smuzhiyun printf("%s: Cannot find out VCO for Frequency (%luHz).\n",
182*4882a593Smuzhiyun __func__, freq_hz);
183*4882a593Smuzhiyun return -1;
184*4882a593Smuzhiyun }
185*4882a593Smuzhiyun
186*4882a593Smuzhiyun div->no = no;
187*4882a593Smuzhiyun
188*4882a593Smuzhiyun best_diff_khz = vco_khz;
189*4882a593Smuzhiyun for (nr = 1; nr < max_nr && best_diff_khz; nr++) {
190*4882a593Smuzhiyun fref_khz = ref_khz / nr;
191*4882a593Smuzhiyun if (fref_khz < FREF_MIN_KHZ)
192*4882a593Smuzhiyun break;
193*4882a593Smuzhiyun if (fref_khz > FREF_MAX_KHZ)
194*4882a593Smuzhiyun continue;
195*4882a593Smuzhiyun
196*4882a593Smuzhiyun nf = vco_khz / fref_khz;
197*4882a593Smuzhiyun if (nf >= max_nf)
198*4882a593Smuzhiyun continue;
199*4882a593Smuzhiyun diff_khz = vco_khz - nf * fref_khz;
200*4882a593Smuzhiyun if (nf + 1 < max_nf && diff_khz > fref_khz / 2) {
201*4882a593Smuzhiyun nf++;
202*4882a593Smuzhiyun diff_khz = fref_khz - diff_khz;
203*4882a593Smuzhiyun }
204*4882a593Smuzhiyun
205*4882a593Smuzhiyun if (diff_khz >= best_diff_khz)
206*4882a593Smuzhiyun continue;
207*4882a593Smuzhiyun
208*4882a593Smuzhiyun best_diff_khz = diff_khz;
209*4882a593Smuzhiyun div->nr = nr;
210*4882a593Smuzhiyun div->nf = nf;
211*4882a593Smuzhiyun }
212*4882a593Smuzhiyun
213*4882a593Smuzhiyun if (best_diff_khz > 4 * 1000) {
214*4882a593Smuzhiyun printf("%s:Fail to match output freq %lu,best_is %u Hz\n",
215*4882a593Smuzhiyun __func__, freq_hz, best_diff_khz * 1000);
216*4882a593Smuzhiyun return -EINVAL;
217*4882a593Smuzhiyun }
218*4882a593Smuzhiyun
219*4882a593Smuzhiyun return 0;
220*4882a593Smuzhiyun }
221*4882a593Smuzhiyun
222*4882a593Smuzhiyun /* Get pll rate by id */
rkclk_pll_get_rate(struct rk3368_cru * cru,enum rk3368_pll_id pll_id)223*4882a593Smuzhiyun static uint32_t rkclk_pll_get_rate(struct rk3368_cru *cru,
224*4882a593Smuzhiyun enum rk3368_pll_id pll_id)
225*4882a593Smuzhiyun {
226*4882a593Smuzhiyun uint32_t nr, no, nf;
227*4882a593Smuzhiyun uint32_t con;
228*4882a593Smuzhiyun struct rk3368_pll *pll = &cru->pll[pll_id];
229*4882a593Smuzhiyun
230*4882a593Smuzhiyun con = readl(&pll->con3);
231*4882a593Smuzhiyun
232*4882a593Smuzhiyun switch ((con & PLL_MODE_MASK) >> PLL_MODE_SHIFT) {
233*4882a593Smuzhiyun case PLL_MODE_SLOW:
234*4882a593Smuzhiyun return OSC_HZ;
235*4882a593Smuzhiyun case PLL_MODE_NORMAL:
236*4882a593Smuzhiyun con = readl(&pll->con0);
237*4882a593Smuzhiyun no = ((con & PLL_OD_MASK) >> PLL_OD_SHIFT) + 1;
238*4882a593Smuzhiyun nr = ((con & PLL_NR_MASK) >> PLL_NR_SHIFT) + 1;
239*4882a593Smuzhiyun con = readl(&pll->con1);
240*4882a593Smuzhiyun nf = ((con & PLL_NF_MASK) >> PLL_NF_SHIFT) + 1;
241*4882a593Smuzhiyun
242*4882a593Smuzhiyun return (24 * nf / (nr * no)) * 1000000;
243*4882a593Smuzhiyun case PLL_MODE_DEEP_SLOW:
244*4882a593Smuzhiyun default:
245*4882a593Smuzhiyun return 32768;
246*4882a593Smuzhiyun }
247*4882a593Smuzhiyun }
248*4882a593Smuzhiyun
rkclk_set_pll(struct rk3368_cru * cru,enum rk3368_pll_id pll_id,const struct pll_div * div)249*4882a593Smuzhiyun static int rkclk_set_pll(struct rk3368_cru *cru, enum rk3368_pll_id pll_id,
250*4882a593Smuzhiyun const struct pll_div *div)
251*4882a593Smuzhiyun {
252*4882a593Smuzhiyun struct rk3368_pll *pll = &cru->pll[pll_id];
253*4882a593Smuzhiyun /* All PLLs have same VCO and output frequency range restrictions*/
254*4882a593Smuzhiyun uint vco_hz = OSC_HZ / 1000 * div->nf / div->nr * 1000;
255*4882a593Smuzhiyun uint output_hz = vco_hz / div->no;
256*4882a593Smuzhiyun
257*4882a593Smuzhiyun debug("PLL at %p: nf=%d, nr=%d, no=%d, vco=%u Hz, output=%u Hz\n",
258*4882a593Smuzhiyun pll, div->nf, div->nr, div->no, vco_hz, output_hz);
259*4882a593Smuzhiyun
260*4882a593Smuzhiyun /* enter slow mode and reset pll */
261*4882a593Smuzhiyun rk_clrsetreg(&pll->con3, PLL_MODE_MASK | PLL_RESET_MASK,
262*4882a593Smuzhiyun PLL_RESET << PLL_RESET_SHIFT);
263*4882a593Smuzhiyun
264*4882a593Smuzhiyun rk_clrsetreg(&pll->con0, PLL_NR_MASK | PLL_OD_MASK,
265*4882a593Smuzhiyun ((div->nr - 1) << PLL_NR_SHIFT) |
266*4882a593Smuzhiyun ((div->no - 1) << PLL_OD_SHIFT));
267*4882a593Smuzhiyun writel((div->nf - 1) << PLL_NF_SHIFT, &pll->con1);
268*4882a593Smuzhiyun /*
269*4882a593Smuzhiyun * BWADJ should be set to NF / 2 to ensure the nominal bandwidth.
270*4882a593Smuzhiyun * Compare the RK3368 TRM, section "3.6.4 PLL Bandwidth Adjustment".
271*4882a593Smuzhiyun */
272*4882a593Smuzhiyun if (div->nb)
273*4882a593Smuzhiyun clrsetbits_le32(&pll->con2, PLL_BWADJ_MASK, div->nb - 1);
274*4882a593Smuzhiyun else
275*4882a593Smuzhiyun clrsetbits_le32(&pll->con2, PLL_BWADJ_MASK, (div->nf >> 1) - 1);
276*4882a593Smuzhiyun
277*4882a593Smuzhiyun udelay(10);
278*4882a593Smuzhiyun
279*4882a593Smuzhiyun /* return from reset */
280*4882a593Smuzhiyun rk_clrreg(&pll->con3, PLL_RESET_MASK);
281*4882a593Smuzhiyun
282*4882a593Smuzhiyun /* waiting for pll lock */
283*4882a593Smuzhiyun while (!(readl(&pll->con1) & PLL_LOCK_STA))
284*4882a593Smuzhiyun udelay(1);
285*4882a593Smuzhiyun
286*4882a593Smuzhiyun rk_clrsetreg(&pll->con3, PLL_MODE_MASK,
287*4882a593Smuzhiyun PLL_MODE_NORMAL << PLL_MODE_SHIFT);
288*4882a593Smuzhiyun
289*4882a593Smuzhiyun return 0;
290*4882a593Smuzhiyun }
291*4882a593Smuzhiyun
292*4882a593Smuzhiyun #if !IS_ENABLED(CONFIG_SPL_BUILD) || CONFIG_IS_ENABLED(MMC_SUPPORT)
rk3368_mmc_get_clk(struct rk3368_cru * cru,uint clk_id)293*4882a593Smuzhiyun static ulong rk3368_mmc_get_clk(struct rk3368_cru *cru, uint clk_id)
294*4882a593Smuzhiyun {
295*4882a593Smuzhiyun u32 div, con, con_id, rate;
296*4882a593Smuzhiyun u32 pll_rate;
297*4882a593Smuzhiyun
298*4882a593Smuzhiyun switch (clk_id) {
299*4882a593Smuzhiyun case HCLK_SDMMC:
300*4882a593Smuzhiyun con_id = 50;
301*4882a593Smuzhiyun break;
302*4882a593Smuzhiyun case HCLK_EMMC:
303*4882a593Smuzhiyun con_id = 51;
304*4882a593Smuzhiyun break;
305*4882a593Smuzhiyun case SCLK_SDIO0:
306*4882a593Smuzhiyun con_id = 48;
307*4882a593Smuzhiyun break;
308*4882a593Smuzhiyun default:
309*4882a593Smuzhiyun return -EINVAL;
310*4882a593Smuzhiyun }
311*4882a593Smuzhiyun
312*4882a593Smuzhiyun con = readl(&cru->clksel_con[con_id]);
313*4882a593Smuzhiyun switch (con & MMC_PLL_SEL_MASK) {
314*4882a593Smuzhiyun case MMC_PLL_SEL_GPLL:
315*4882a593Smuzhiyun pll_rate = rkclk_pll_get_rate(cru, GPLL);
316*4882a593Smuzhiyun break;
317*4882a593Smuzhiyun case MMC_PLL_SEL_24M:
318*4882a593Smuzhiyun pll_rate = OSC_HZ;
319*4882a593Smuzhiyun break;
320*4882a593Smuzhiyun case MMC_PLL_SEL_CPLL:
321*4882a593Smuzhiyun pll_rate = rkclk_pll_get_rate(cru, CPLL);
322*4882a593Smuzhiyun break;
323*4882a593Smuzhiyun case MMC_PLL_SEL_USBPHY_480M:
324*4882a593Smuzhiyun default:
325*4882a593Smuzhiyun return -EINVAL;
326*4882a593Smuzhiyun }
327*4882a593Smuzhiyun div = (con & MMC_CLK_DIV_MASK) >> MMC_CLK_DIV_SHIFT;
328*4882a593Smuzhiyun rate = DIV_TO_RATE(pll_rate, div);
329*4882a593Smuzhiyun
330*4882a593Smuzhiyun debug("%s: raw rate %d (post-divide by 2)\n", __func__, rate);
331*4882a593Smuzhiyun return rate >> 1;
332*4882a593Smuzhiyun }
333*4882a593Smuzhiyun
rk3368_mmc_find_best_rate_and_parent(struct clk * clk,ulong rate,u32 * best_mux,u32 * best_div)334*4882a593Smuzhiyun static ulong rk3368_mmc_find_best_rate_and_parent(struct clk *clk,
335*4882a593Smuzhiyun ulong rate,
336*4882a593Smuzhiyun u32 *best_mux,
337*4882a593Smuzhiyun u32 *best_div)
338*4882a593Smuzhiyun {
339*4882a593Smuzhiyun int i;
340*4882a593Smuzhiyun ulong best_rate = 0;
341*4882a593Smuzhiyun const ulong MHz = 1000000;
342*4882a593Smuzhiyun const struct {
343*4882a593Smuzhiyun u32 mux;
344*4882a593Smuzhiyun ulong rate;
345*4882a593Smuzhiyun } parents[] = {
346*4882a593Smuzhiyun { .mux = MMC_PLL_SEL_CPLL, .rate = CPLL_HZ },
347*4882a593Smuzhiyun { .mux = MMC_PLL_SEL_GPLL, .rate = GPLL_HZ },
348*4882a593Smuzhiyun { .mux = MMC_PLL_SEL_24M, .rate = 24 * MHz }
349*4882a593Smuzhiyun };
350*4882a593Smuzhiyun
351*4882a593Smuzhiyun debug("%s: target rate %ld\n", __func__, rate);
352*4882a593Smuzhiyun for (i = 0; i < ARRAY_SIZE(parents); ++i) {
353*4882a593Smuzhiyun /*
354*4882a593Smuzhiyun * Find the largest rate no larger than the target-rate for
355*4882a593Smuzhiyun * the current parent.
356*4882a593Smuzhiyun */
357*4882a593Smuzhiyun ulong parent_rate = parents[i].rate;
358*4882a593Smuzhiyun u32 div = DIV_ROUND_UP(parent_rate, rate);
359*4882a593Smuzhiyun u32 adj_div = div;
360*4882a593Smuzhiyun ulong new_rate = parent_rate / adj_div;
361*4882a593Smuzhiyun
362*4882a593Smuzhiyun debug("%s: rate %ld, parent-mux %d, parent-rate %ld, div %d\n",
363*4882a593Smuzhiyun __func__, rate, parents[i].mux, parents[i].rate, div);
364*4882a593Smuzhiyun
365*4882a593Smuzhiyun /* Skip, if not representable */
366*4882a593Smuzhiyun if ((div - 1) > MMC_CLK_DIV_MASK)
367*4882a593Smuzhiyun continue;
368*4882a593Smuzhiyun
369*4882a593Smuzhiyun /* Skip, if we already have a better (or equal) solution */
370*4882a593Smuzhiyun if (new_rate <= best_rate)
371*4882a593Smuzhiyun continue;
372*4882a593Smuzhiyun
373*4882a593Smuzhiyun /* This is our new best rate. */
374*4882a593Smuzhiyun best_rate = new_rate;
375*4882a593Smuzhiyun *best_mux = parents[i].mux;
376*4882a593Smuzhiyun *best_div = div - 1;
377*4882a593Smuzhiyun }
378*4882a593Smuzhiyun
379*4882a593Smuzhiyun debug("%s: best_mux = %x, best_div = %d, best_rate = %ld\n",
380*4882a593Smuzhiyun __func__, *best_mux, *best_div, best_rate);
381*4882a593Smuzhiyun
382*4882a593Smuzhiyun return best_rate;
383*4882a593Smuzhiyun }
384*4882a593Smuzhiyun
rk3368_mmc_set_clk(struct clk * clk,ulong rate)385*4882a593Smuzhiyun static ulong rk3368_mmc_set_clk(struct clk *clk, ulong rate)
386*4882a593Smuzhiyun {
387*4882a593Smuzhiyun struct rk3368_clk_priv *priv = dev_get_priv(clk->dev);
388*4882a593Smuzhiyun struct rk3368_cru *cru = priv->cru;
389*4882a593Smuzhiyun ulong clk_id = clk->id;
390*4882a593Smuzhiyun u32 con_id, mux = 0, div = 0;
391*4882a593Smuzhiyun
392*4882a593Smuzhiyun /* Find the best parent and rate */
393*4882a593Smuzhiyun rk3368_mmc_find_best_rate_and_parent(clk, rate << 1, &mux, &div);
394*4882a593Smuzhiyun
395*4882a593Smuzhiyun switch (clk_id) {
396*4882a593Smuzhiyun case HCLK_SDMMC:
397*4882a593Smuzhiyun con_id = 50;
398*4882a593Smuzhiyun break;
399*4882a593Smuzhiyun case HCLK_EMMC:
400*4882a593Smuzhiyun con_id = 51;
401*4882a593Smuzhiyun break;
402*4882a593Smuzhiyun case SCLK_SDIO0:
403*4882a593Smuzhiyun con_id = 48;
404*4882a593Smuzhiyun break;
405*4882a593Smuzhiyun default:
406*4882a593Smuzhiyun return -EINVAL;
407*4882a593Smuzhiyun }
408*4882a593Smuzhiyun
409*4882a593Smuzhiyun rk_clrsetreg(&cru->clksel_con[con_id],
410*4882a593Smuzhiyun MMC_PLL_SEL_MASK | MMC_CLK_DIV_MASK,
411*4882a593Smuzhiyun mux | div);
412*4882a593Smuzhiyun
413*4882a593Smuzhiyun return rk3368_mmc_get_clk(cru, clk_id);
414*4882a593Smuzhiyun }
415*4882a593Smuzhiyun #endif
416*4882a593Smuzhiyun
417*4882a593Smuzhiyun #if IS_ENABLED(CONFIG_TPL_BUILD)
rk3368_ddr_set_clk(struct rk3368_cru * cru,ulong set_rate)418*4882a593Smuzhiyun static ulong rk3368_ddr_set_clk(struct rk3368_cru *cru, ulong set_rate)
419*4882a593Smuzhiyun {
420*4882a593Smuzhiyun const struct pll_div *dpll_cfg = NULL;
421*4882a593Smuzhiyun const ulong MHz = 1000000;
422*4882a593Smuzhiyun
423*4882a593Smuzhiyun /* Fout = ((Fin /NR) * NF )/ NO */
424*4882a593Smuzhiyun static const struct pll_div dpll_1200 = PLL_DIVISORS(1200 * MHz, 1, 1);
425*4882a593Smuzhiyun static const struct pll_div dpll_1332 = PLL_DIVISORS(1332 * MHz, 2, 1);
426*4882a593Smuzhiyun static const struct pll_div dpll_1600 = PLL_DIVISORS(1600 * MHz, 3, 2);
427*4882a593Smuzhiyun
428*4882a593Smuzhiyun switch (set_rate) {
429*4882a593Smuzhiyun case 1200*MHz:
430*4882a593Smuzhiyun dpll_cfg = &dpll_1200;
431*4882a593Smuzhiyun break;
432*4882a593Smuzhiyun case 1332*MHz:
433*4882a593Smuzhiyun dpll_cfg = &dpll_1332;
434*4882a593Smuzhiyun break;
435*4882a593Smuzhiyun case 1600*MHz:
436*4882a593Smuzhiyun dpll_cfg = &dpll_1600;
437*4882a593Smuzhiyun break;
438*4882a593Smuzhiyun default:
439*4882a593Smuzhiyun pr_err("Unsupported SDRAM frequency!,%ld\n", set_rate);
440*4882a593Smuzhiyun }
441*4882a593Smuzhiyun rkclk_set_pll(cru, DPLL, dpll_cfg);
442*4882a593Smuzhiyun
443*4882a593Smuzhiyun return set_rate;
444*4882a593Smuzhiyun }
445*4882a593Smuzhiyun #endif
446*4882a593Smuzhiyun
447*4882a593Smuzhiyun #if CONFIG_IS_ENABLED(GMAC_ROCKCHIP)
rk3368_gmac_set_clk(struct rk3368_cru * cru,ulong set_rate)448*4882a593Smuzhiyun static ulong rk3368_gmac_set_clk(struct rk3368_cru *cru, ulong set_rate)
449*4882a593Smuzhiyun {
450*4882a593Smuzhiyun ulong ret;
451*4882a593Smuzhiyun
452*4882a593Smuzhiyun /*
453*4882a593Smuzhiyun * The gmac clock can be derived either from an external clock
454*4882a593Smuzhiyun * or can be generated from internally by a divider from SCLK_MAC.
455*4882a593Smuzhiyun */
456*4882a593Smuzhiyun if (readl(&cru->clksel_con[43]) & GMAC_MUX_SEL_EXTCLK) {
457*4882a593Smuzhiyun /* An external clock will always generate the right rate... */
458*4882a593Smuzhiyun ret = set_rate;
459*4882a593Smuzhiyun } else {
460*4882a593Smuzhiyun u32 con = readl(&cru->clksel_con[43]);
461*4882a593Smuzhiyun ulong pll_rate;
462*4882a593Smuzhiyun u8 div;
463*4882a593Smuzhiyun
464*4882a593Smuzhiyun if (((con >> GMAC_PLL_SHIFT) & GMAC_PLL_MASK) ==
465*4882a593Smuzhiyun GMAC_PLL_SELECT_GENERAL)
466*4882a593Smuzhiyun pll_rate = GPLL_HZ;
467*4882a593Smuzhiyun else if (((con >> GMAC_PLL_SHIFT) & GMAC_PLL_MASK) ==
468*4882a593Smuzhiyun GMAC_PLL_SELECT_CODEC)
469*4882a593Smuzhiyun pll_rate = CPLL_HZ;
470*4882a593Smuzhiyun else
471*4882a593Smuzhiyun /* CPLL is not set */
472*4882a593Smuzhiyun return -EPERM;
473*4882a593Smuzhiyun
474*4882a593Smuzhiyun div = DIV_ROUND_UP(pll_rate, set_rate) - 1;
475*4882a593Smuzhiyun if (div <= 0x1f)
476*4882a593Smuzhiyun rk_clrsetreg(&cru->clksel_con[43], GMAC_DIV_CON_MASK,
477*4882a593Smuzhiyun div << GMAC_DIV_CON_SHIFT);
478*4882a593Smuzhiyun else
479*4882a593Smuzhiyun debug("Unsupported div for gmac:%d\n", div);
480*4882a593Smuzhiyun
481*4882a593Smuzhiyun return DIV_TO_RATE(pll_rate, div);
482*4882a593Smuzhiyun }
483*4882a593Smuzhiyun
484*4882a593Smuzhiyun return ret;
485*4882a593Smuzhiyun }
486*4882a593Smuzhiyun #endif
487*4882a593Smuzhiyun
488*4882a593Smuzhiyun /*
489*4882a593Smuzhiyun * RK3368 SPI clocks have a common divider-width (7 bits) and a single bit
490*4882a593Smuzhiyun * to select either CPLL or GPLL as the clock-parent. The location within
491*4882a593Smuzhiyun * the enclosing CLKSEL_CON (i.e. div_shift and sel_shift) are variable.
492*4882a593Smuzhiyun */
493*4882a593Smuzhiyun
494*4882a593Smuzhiyun struct spi_clkreg {
495*4882a593Smuzhiyun uint8_t reg; /* CLKSEL_CON[reg] register in CRU */
496*4882a593Smuzhiyun uint8_t div_shift;
497*4882a593Smuzhiyun uint8_t sel_shift;
498*4882a593Smuzhiyun };
499*4882a593Smuzhiyun
500*4882a593Smuzhiyun /*
501*4882a593Smuzhiyun * The entries are numbered relative to their offset from SCLK_SPI0.
502*4882a593Smuzhiyun */
503*4882a593Smuzhiyun static const struct spi_clkreg spi_clkregs[] = {
504*4882a593Smuzhiyun [0] = { .reg = 45, .div_shift = 0, .sel_shift = 7, },
505*4882a593Smuzhiyun [1] = { .reg = 45, .div_shift = 8, .sel_shift = 15, },
506*4882a593Smuzhiyun [2] = { .reg = 46, .div_shift = 8, .sel_shift = 15, },
507*4882a593Smuzhiyun };
508*4882a593Smuzhiyun
extract_bits(u32 val,unsigned width,unsigned shift)509*4882a593Smuzhiyun static inline u32 extract_bits(u32 val, unsigned width, unsigned shift)
510*4882a593Smuzhiyun {
511*4882a593Smuzhiyun return (val >> shift) & ((1 << width) - 1);
512*4882a593Smuzhiyun }
513*4882a593Smuzhiyun
rk3368_spi_get_clk(struct rk3368_cru * cru,ulong clk_id)514*4882a593Smuzhiyun static ulong rk3368_spi_get_clk(struct rk3368_cru *cru, ulong clk_id)
515*4882a593Smuzhiyun {
516*4882a593Smuzhiyun const struct spi_clkreg *spiclk = NULL;
517*4882a593Smuzhiyun u32 div, val;
518*4882a593Smuzhiyun
519*4882a593Smuzhiyun switch (clk_id) {
520*4882a593Smuzhiyun case SCLK_SPI0 ... SCLK_SPI2:
521*4882a593Smuzhiyun spiclk = &spi_clkregs[clk_id - SCLK_SPI0];
522*4882a593Smuzhiyun break;
523*4882a593Smuzhiyun
524*4882a593Smuzhiyun default:
525*4882a593Smuzhiyun pr_err("%s: SPI clk-id %ld not supported\n", __func__, clk_id);
526*4882a593Smuzhiyun return -EINVAL;
527*4882a593Smuzhiyun }
528*4882a593Smuzhiyun
529*4882a593Smuzhiyun val = readl(&cru->clksel_con[spiclk->reg]);
530*4882a593Smuzhiyun div = extract_bits(val, 7, spiclk->div_shift);
531*4882a593Smuzhiyun
532*4882a593Smuzhiyun debug("%s: div 0x%x\n", __func__, div);
533*4882a593Smuzhiyun return DIV_TO_RATE(GPLL_HZ, div);
534*4882a593Smuzhiyun }
535*4882a593Smuzhiyun
rk3368_spi_set_clk(struct rk3368_cru * cru,ulong clk_id,uint hz)536*4882a593Smuzhiyun static ulong rk3368_spi_set_clk(struct rk3368_cru *cru, ulong clk_id, uint hz)
537*4882a593Smuzhiyun {
538*4882a593Smuzhiyun const struct spi_clkreg *spiclk = NULL;
539*4882a593Smuzhiyun int src_clk_div;
540*4882a593Smuzhiyun
541*4882a593Smuzhiyun src_clk_div = DIV_ROUND_UP(GPLL_HZ, hz);
542*4882a593Smuzhiyun assert(src_clk_div < 127);
543*4882a593Smuzhiyun
544*4882a593Smuzhiyun switch (clk_id) {
545*4882a593Smuzhiyun case SCLK_SPI0 ... SCLK_SPI2:
546*4882a593Smuzhiyun spiclk = &spi_clkregs[clk_id - SCLK_SPI0];
547*4882a593Smuzhiyun break;
548*4882a593Smuzhiyun
549*4882a593Smuzhiyun default:
550*4882a593Smuzhiyun pr_err("%s: SPI clk-id %ld not supported\n", __func__, clk_id);
551*4882a593Smuzhiyun return -EINVAL;
552*4882a593Smuzhiyun }
553*4882a593Smuzhiyun
554*4882a593Smuzhiyun rk_clrsetreg(&cru->clksel_con[spiclk->reg],
555*4882a593Smuzhiyun ((0x7f << spiclk->div_shift) |
556*4882a593Smuzhiyun (0x1 << spiclk->sel_shift)),
557*4882a593Smuzhiyun ((src_clk_div << spiclk->div_shift) |
558*4882a593Smuzhiyun (1 << spiclk->sel_shift)));
559*4882a593Smuzhiyun
560*4882a593Smuzhiyun return rk3368_spi_get_clk(cru, clk_id);
561*4882a593Smuzhiyun }
562*4882a593Smuzhiyun
rk3368_saradc_get_clk(struct rk3368_cru * cru)563*4882a593Smuzhiyun static ulong rk3368_saradc_get_clk(struct rk3368_cru *cru)
564*4882a593Smuzhiyun {
565*4882a593Smuzhiyun u32 div, val;
566*4882a593Smuzhiyun
567*4882a593Smuzhiyun val = readl(&cru->clksel_con[25]);
568*4882a593Smuzhiyun div = bitfield_extract(val, CLK_SARADC_DIV_CON_SHIFT,
569*4882a593Smuzhiyun CLK_SARADC_DIV_CON_WIDTH);
570*4882a593Smuzhiyun
571*4882a593Smuzhiyun return DIV_TO_RATE(OSC_HZ, div);
572*4882a593Smuzhiyun }
573*4882a593Smuzhiyun
rk3368_saradc_set_clk(struct rk3368_cru * cru,uint hz)574*4882a593Smuzhiyun static ulong rk3368_saradc_set_clk(struct rk3368_cru *cru, uint hz)
575*4882a593Smuzhiyun {
576*4882a593Smuzhiyun int src_clk_div;
577*4882a593Smuzhiyun
578*4882a593Smuzhiyun src_clk_div = DIV_ROUND_UP(OSC_HZ, hz) - 1;
579*4882a593Smuzhiyun assert(src_clk_div < 128);
580*4882a593Smuzhiyun
581*4882a593Smuzhiyun rk_clrsetreg(&cru->clksel_con[25],
582*4882a593Smuzhiyun CLK_SARADC_DIV_CON_MASK,
583*4882a593Smuzhiyun src_clk_div << CLK_SARADC_DIV_CON_SHIFT);
584*4882a593Smuzhiyun
585*4882a593Smuzhiyun return rk3368_saradc_get_clk(cru);
586*4882a593Smuzhiyun }
587*4882a593Smuzhiyun
rk3368_bus_get_clk(struct rk3368_cru * cru,ulong clk_id)588*4882a593Smuzhiyun static ulong rk3368_bus_get_clk(struct rk3368_cru *cru, ulong clk_id)
589*4882a593Smuzhiyun {
590*4882a593Smuzhiyun u32 div, con, parent;
591*4882a593Smuzhiyun
592*4882a593Smuzhiyun switch (clk_id) {
593*4882a593Smuzhiyun case ACLK_BUS:
594*4882a593Smuzhiyun con = readl(&cru->clksel_con[8]);
595*4882a593Smuzhiyun div = (con & ACLK_BUS_DIV_CON_MASK) >> ACLK_BUS_DIV_CON_SHIFT;
596*4882a593Smuzhiyun parent = rkclk_pll_get_rate(cru, GPLL);
597*4882a593Smuzhiyun break;
598*4882a593Smuzhiyun case HCLK_BUS:
599*4882a593Smuzhiyun con = readl(&cru->clksel_con[8]);
600*4882a593Smuzhiyun div = (con & HCLK_BUS_DIV_CON_MASK) >> HCLK_BUS_DIV_CON_SHIFT;
601*4882a593Smuzhiyun parent = rk3368_bus_get_clk(cru, ACLK_BUS);
602*4882a593Smuzhiyun break;
603*4882a593Smuzhiyun case PCLK_BUS:
604*4882a593Smuzhiyun case PCLK_PWM0:
605*4882a593Smuzhiyun case PCLK_PWM1:
606*4882a593Smuzhiyun case PCLK_I2C0:
607*4882a593Smuzhiyun case PCLK_I2C1:
608*4882a593Smuzhiyun con = readl(&cru->clksel_con[8]);
609*4882a593Smuzhiyun div = (con & PCLK_BUS_DIV_CON_MASK) >> PCLK_BUS_DIV_CON_SHIFT;
610*4882a593Smuzhiyun parent = rk3368_bus_get_clk(cru, ACLK_BUS);
611*4882a593Smuzhiyun break;
612*4882a593Smuzhiyun default:
613*4882a593Smuzhiyun return -ENOENT;
614*4882a593Smuzhiyun }
615*4882a593Smuzhiyun
616*4882a593Smuzhiyun return DIV_TO_RATE(parent, div);
617*4882a593Smuzhiyun }
618*4882a593Smuzhiyun
rk3368_bus_set_clk(struct rk3368_cru * cru,ulong clk_id,ulong hz)619*4882a593Smuzhiyun static ulong rk3368_bus_set_clk(struct rk3368_cru *cru,
620*4882a593Smuzhiyun ulong clk_id, ulong hz)
621*4882a593Smuzhiyun {
622*4882a593Smuzhiyun int src_clk_div;
623*4882a593Smuzhiyun
624*4882a593Smuzhiyun /*
625*4882a593Smuzhiyun * select gpll as pd_bus bus clock source and
626*4882a593Smuzhiyun * set up dependent divisors for PCLK/HCLK and ACLK clocks.
627*4882a593Smuzhiyun */
628*4882a593Smuzhiyun switch (clk_id) {
629*4882a593Smuzhiyun case ACLK_BUS:
630*4882a593Smuzhiyun src_clk_div = DIV_ROUND_UP(rkclk_pll_get_rate(cru, GPLL), hz);
631*4882a593Smuzhiyun assert(src_clk_div - 1 < 31);
632*4882a593Smuzhiyun rk_clrsetreg(&cru->clksel_con[8],
633*4882a593Smuzhiyun CLK_BUS_PLL_SEL_MASK | ACLK_BUS_DIV_CON_MASK,
634*4882a593Smuzhiyun CLK_BUS_PLL_SEL_GPLL << CLK_BUS_PLL_SEL_SHIFT |
635*4882a593Smuzhiyun (src_clk_div - 1) << ACLK_BUS_DIV_CON_SHIFT);
636*4882a593Smuzhiyun break;
637*4882a593Smuzhiyun case HCLK_BUS:
638*4882a593Smuzhiyun src_clk_div = DIV_ROUND_UP(rk3368_bus_get_clk(cru,
639*4882a593Smuzhiyun ACLK_BUS),
640*4882a593Smuzhiyun hz);
641*4882a593Smuzhiyun assert(src_clk_div - 1 < 3);
642*4882a593Smuzhiyun rk_clrsetreg(&cru->clksel_con[8],
643*4882a593Smuzhiyun HCLK_BUS_DIV_CON_MASK,
644*4882a593Smuzhiyun (src_clk_div - 1) << HCLK_BUS_DIV_CON_SHIFT);
645*4882a593Smuzhiyun break;
646*4882a593Smuzhiyun case PCLK_BUS:
647*4882a593Smuzhiyun src_clk_div = DIV_ROUND_UP(rk3368_bus_get_clk(cru,
648*4882a593Smuzhiyun ACLK_BUS),
649*4882a593Smuzhiyun hz);
650*4882a593Smuzhiyun assert(src_clk_div - 1 < 3);
651*4882a593Smuzhiyun rk_clrsetreg(&cru->clksel_con[8],
652*4882a593Smuzhiyun PCLK_BUS_DIV_CON_MASK,
653*4882a593Smuzhiyun (src_clk_div - 1) << PCLK_BUS_DIV_CON_SHIFT);
654*4882a593Smuzhiyun break;
655*4882a593Smuzhiyun default:
656*4882a593Smuzhiyun printf("do not support this bus freq\n");
657*4882a593Smuzhiyun return -EINVAL;
658*4882a593Smuzhiyun }
659*4882a593Smuzhiyun return rk3368_bus_get_clk(cru, clk_id);
660*4882a593Smuzhiyun }
661*4882a593Smuzhiyun
rk3368_peri_get_clk(struct rk3368_cru * cru,ulong clk_id)662*4882a593Smuzhiyun static ulong rk3368_peri_get_clk(struct rk3368_cru *cru, ulong clk_id)
663*4882a593Smuzhiyun {
664*4882a593Smuzhiyun u32 div, con, parent;
665*4882a593Smuzhiyun
666*4882a593Smuzhiyun switch (clk_id) {
667*4882a593Smuzhiyun case ACLK_PERI:
668*4882a593Smuzhiyun con = readl(&cru->clksel_con[9]);
669*4882a593Smuzhiyun div = (con & ACLK_PERI_DIV_CON_MASK) >> ACLK_PERI_DIV_CON_SHIFT;
670*4882a593Smuzhiyun parent = rkclk_pll_get_rate(cru, GPLL);
671*4882a593Smuzhiyun break;
672*4882a593Smuzhiyun case HCLK_PERI:
673*4882a593Smuzhiyun con = readl(&cru->clksel_con[9]);
674*4882a593Smuzhiyun div = (con & HCLK_PERI_DIV_CON_MASK) >> HCLK_PERI_DIV_CON_SHIFT;
675*4882a593Smuzhiyun parent = rk3368_peri_get_clk(cru, ACLK_PERI);
676*4882a593Smuzhiyun break;
677*4882a593Smuzhiyun case PCLK_PERI:
678*4882a593Smuzhiyun case PCLK_I2C2:
679*4882a593Smuzhiyun case PCLK_I2C3:
680*4882a593Smuzhiyun case PCLK_I2C4:
681*4882a593Smuzhiyun case PCLK_I2C5:
682*4882a593Smuzhiyun con = readl(&cru->clksel_con[9]);
683*4882a593Smuzhiyun div = (con & PCLK_PERI_DIV_CON_MASK) >> PCLK_PERI_DIV_CON_SHIFT;
684*4882a593Smuzhiyun parent = rk3368_peri_get_clk(cru, ACLK_PERI);
685*4882a593Smuzhiyun break;
686*4882a593Smuzhiyun default:
687*4882a593Smuzhiyun return -ENOENT;
688*4882a593Smuzhiyun }
689*4882a593Smuzhiyun
690*4882a593Smuzhiyun return DIV_TO_RATE(parent, div);
691*4882a593Smuzhiyun }
692*4882a593Smuzhiyun
rk3368_peri_set_clk(struct rk3368_cru * cru,ulong clk_id,ulong hz)693*4882a593Smuzhiyun static ulong rk3368_peri_set_clk(struct rk3368_cru *cru,
694*4882a593Smuzhiyun ulong clk_id, ulong hz)
695*4882a593Smuzhiyun {
696*4882a593Smuzhiyun int src_clk_div;
697*4882a593Smuzhiyun
698*4882a593Smuzhiyun /*
699*4882a593Smuzhiyun * select gpll as pd_bus bus clock source and
700*4882a593Smuzhiyun * set up dependent divisors for PCLK/HCLK and ACLK clocks.
701*4882a593Smuzhiyun */
702*4882a593Smuzhiyun switch (clk_id) {
703*4882a593Smuzhiyun case ACLK_PERI:
704*4882a593Smuzhiyun src_clk_div = DIV_ROUND_UP(rkclk_pll_get_rate(cru, GPLL), hz);
705*4882a593Smuzhiyun assert(src_clk_div - 1 < 31);
706*4882a593Smuzhiyun rk_clrsetreg(&cru->clksel_con[9],
707*4882a593Smuzhiyun CLK_PERI_PLL_SEL_MASK | ACLK_PERI_DIV_CON_MASK,
708*4882a593Smuzhiyun CLK_PERI_PLL_SEL_GPLL << CLK_PERI_PLL_SEL_SHIFT |
709*4882a593Smuzhiyun (src_clk_div - 1) << ACLK_PERI_DIV_CON_SHIFT);
710*4882a593Smuzhiyun break;
711*4882a593Smuzhiyun case HCLK_PERI:
712*4882a593Smuzhiyun src_clk_div = DIV_ROUND_UP(rk3368_peri_get_clk(cru,
713*4882a593Smuzhiyun ACLK_PERI),
714*4882a593Smuzhiyun hz);
715*4882a593Smuzhiyun assert(src_clk_div - 1 < 3);
716*4882a593Smuzhiyun rk_clrsetreg(&cru->clksel_con[9],
717*4882a593Smuzhiyun HCLK_PERI_DIV_CON_MASK,
718*4882a593Smuzhiyun (src_clk_div - 1) << HCLK_PERI_DIV_CON_SHIFT);
719*4882a593Smuzhiyun break;
720*4882a593Smuzhiyun case PCLK_PERI:
721*4882a593Smuzhiyun src_clk_div = DIV_ROUND_UP(rk3368_peri_get_clk(cru,
722*4882a593Smuzhiyun ACLK_PERI),
723*4882a593Smuzhiyun hz);
724*4882a593Smuzhiyun assert(src_clk_div - 1 < 3);
725*4882a593Smuzhiyun rk_clrsetreg(&cru->clksel_con[9],
726*4882a593Smuzhiyun PCLK_PERI_DIV_CON_MASK,
727*4882a593Smuzhiyun (src_clk_div - 1) << PCLK_PERI_DIV_CON_SHIFT);
728*4882a593Smuzhiyun break;
729*4882a593Smuzhiyun default:
730*4882a593Smuzhiyun printf("do not support this bus freq\n");
731*4882a593Smuzhiyun return -EINVAL;
732*4882a593Smuzhiyun }
733*4882a593Smuzhiyun
734*4882a593Smuzhiyun return rk3368_peri_get_clk(cru, clk_id);
735*4882a593Smuzhiyun }
736*4882a593Smuzhiyun
737*4882a593Smuzhiyun #if !defined(CONFIG_SPL_BUILD)
rk3368_vop_get_clk(struct rk3368_cru * cru,int clk_id)738*4882a593Smuzhiyun static ulong rk3368_vop_get_clk(struct rk3368_cru *cru, int clk_id)
739*4882a593Smuzhiyun {
740*4882a593Smuzhiyun u32 div, con, parent, sel;
741*4882a593Smuzhiyun
742*4882a593Smuzhiyun switch (clk_id) {
743*4882a593Smuzhiyun case DCLK_VOP:
744*4882a593Smuzhiyun con = readl(&cru->clksel_con[20]);
745*4882a593Smuzhiyun div = con & DCLK_VOP_DIV_MASK;
746*4882a593Smuzhiyun parent = rkclk_pll_get_rate(cru, NPLL);
747*4882a593Smuzhiyun break;
748*4882a593Smuzhiyun case ACLK_VOP:
749*4882a593Smuzhiyun con = readl(&cru->clksel_con[19]);
750*4882a593Smuzhiyun div = con & ACLK_VOP_DIV_MASK;
751*4882a593Smuzhiyun sel = (con & (ACLK_VOP_PLL_SEL_MASK <<
752*4882a593Smuzhiyun ACLK_VOP_PLL_SEL_SHIFT)) >>
753*4882a593Smuzhiyun ACLK_VOP_PLL_SEL_SHIFT;
754*4882a593Smuzhiyun if (sel == ACLK_VOP_PLL_SEL_CPLL)
755*4882a593Smuzhiyun parent = rkclk_pll_get_rate(cru, CPLL);
756*4882a593Smuzhiyun else if (ACLK_VOP_PLL_SEL_GPLL)
757*4882a593Smuzhiyun parent = rkclk_pll_get_rate(cru, GPLL);
758*4882a593Smuzhiyun else
759*4882a593Smuzhiyun parent = 480000000;
760*4882a593Smuzhiyun break;
761*4882a593Smuzhiyun default:
762*4882a593Smuzhiyun return -EINVAL;
763*4882a593Smuzhiyun }
764*4882a593Smuzhiyun
765*4882a593Smuzhiyun return DIV_TO_RATE(parent, div);
766*4882a593Smuzhiyun }
767*4882a593Smuzhiyun
rk3368_vop_set_clk(struct rk3368_cru * cru,int clk_id,uint hz)768*4882a593Smuzhiyun static ulong rk3368_vop_set_clk(struct rk3368_cru *cru, int clk_id, uint hz)
769*4882a593Smuzhiyun {
770*4882a593Smuzhiyun struct pll_div npll_config = {0};
771*4882a593Smuzhiyun u32 lcdc_div;
772*4882a593Smuzhiyun int ret;
773*4882a593Smuzhiyun
774*4882a593Smuzhiyun switch (clk_id) {
775*4882a593Smuzhiyun case DCLK_VOP:
776*4882a593Smuzhiyun if (!(NPLL_HZ % hz)) {
777*4882a593Smuzhiyun rkclk_set_pll(cru, NPLL, rkclk_get_pll_config(NPLL_HZ));
778*4882a593Smuzhiyun lcdc_div = NPLL_HZ / hz;
779*4882a593Smuzhiyun } else {
780*4882a593Smuzhiyun ret = pll_para_config(hz, &npll_config, &lcdc_div);
781*4882a593Smuzhiyun if (ret)
782*4882a593Smuzhiyun return ret;
783*4882a593Smuzhiyun
784*4882a593Smuzhiyun rkclk_set_pll(cru, NPLL, &npll_config);
785*4882a593Smuzhiyun }
786*4882a593Smuzhiyun /* vop dclk source clk: npll,dclk_div: 1 */
787*4882a593Smuzhiyun rk_clrsetreg(&cru->clksel_con[20],
788*4882a593Smuzhiyun (DCLK_VOP_PLL_SEL_MASK << DCLK_VOP_PLL_SEL_SHIFT) |
789*4882a593Smuzhiyun (DCLK_VOP_DIV_MASK << DCLK_VOP_DIV_SHIFT),
790*4882a593Smuzhiyun (DCLK_VOP_PLL_SEL_NPLL << DCLK_VOP_PLL_SEL_SHIFT) |
791*4882a593Smuzhiyun (lcdc_div - 1) << DCLK_VOP_DIV_SHIFT);
792*4882a593Smuzhiyun break;
793*4882a593Smuzhiyun case ACLK_VOP:
794*4882a593Smuzhiyun if ((rkclk_pll_get_rate(cru, CPLL) % hz) == 0) {
795*4882a593Smuzhiyun lcdc_div = rkclk_pll_get_rate(cru, CPLL) / hz;
796*4882a593Smuzhiyun rk_clrsetreg(&cru->clksel_con[19],
797*4882a593Smuzhiyun (ACLK_VOP_PLL_SEL_MASK <<
798*4882a593Smuzhiyun ACLK_VOP_PLL_SEL_SHIFT) |
799*4882a593Smuzhiyun (ACLK_VOP_DIV_MASK <<
800*4882a593Smuzhiyun ACLK_VOP_DIV_SHIFT),
801*4882a593Smuzhiyun (ACLK_VOP_PLL_SEL_CPLL <<
802*4882a593Smuzhiyun ACLK_VOP_PLL_SEL_SHIFT) |
803*4882a593Smuzhiyun (lcdc_div - 1) <<
804*4882a593Smuzhiyun ACLK_VOP_DIV_SHIFT);
805*4882a593Smuzhiyun } else {
806*4882a593Smuzhiyun lcdc_div = rkclk_pll_get_rate(cru, GPLL) / hz;
807*4882a593Smuzhiyun rk_clrsetreg(&cru->clksel_con[19],
808*4882a593Smuzhiyun (ACLK_VOP_PLL_SEL_MASK <<
809*4882a593Smuzhiyun ACLK_VOP_PLL_SEL_SHIFT) |
810*4882a593Smuzhiyun (ACLK_VOP_DIV_MASK <<
811*4882a593Smuzhiyun ACLK_VOP_DIV_SHIFT),
812*4882a593Smuzhiyun (ACLK_VOP_PLL_SEL_GPLL <<
813*4882a593Smuzhiyun ACLK_VOP_PLL_SEL_SHIFT) |
814*4882a593Smuzhiyun (lcdc_div - 1) <<
815*4882a593Smuzhiyun ACLK_VOP_DIV_SHIFT);
816*4882a593Smuzhiyun }
817*4882a593Smuzhiyun break;
818*4882a593Smuzhiyun default:
819*4882a593Smuzhiyun return -EINVAL;
820*4882a593Smuzhiyun }
821*4882a593Smuzhiyun
822*4882a593Smuzhiyun return 0;
823*4882a593Smuzhiyun }
824*4882a593Smuzhiyun
rk3368_alive_get_clk(struct rk3368_clk_priv * priv)825*4882a593Smuzhiyun static ulong rk3368_alive_get_clk(struct rk3368_clk_priv *priv)
826*4882a593Smuzhiyun {
827*4882a593Smuzhiyun struct rk3368_cru *cru = priv->cru;
828*4882a593Smuzhiyun u32 div, con, parent;
829*4882a593Smuzhiyun
830*4882a593Smuzhiyun con = readl(&cru->clksel_con[10]);
831*4882a593Smuzhiyun div = (con & PCLK_ALIVE_DIV_CON_MASK) >>
832*4882a593Smuzhiyun PCLK_ALIVE_DIV_CON_SHIFT;
833*4882a593Smuzhiyun parent = GPLL_HZ;
834*4882a593Smuzhiyun return DIV_TO_RATE(parent, div);
835*4882a593Smuzhiyun }
836*4882a593Smuzhiyun
rk3368_crypto_get_rate(struct rk3368_clk_priv * priv)837*4882a593Smuzhiyun static ulong rk3368_crypto_get_rate(struct rk3368_clk_priv *priv)
838*4882a593Smuzhiyun {
839*4882a593Smuzhiyun struct rk3368_cru *cru = priv->cru;
840*4882a593Smuzhiyun u32 div, val;
841*4882a593Smuzhiyun
842*4882a593Smuzhiyun val = readl(&cru->clksel_con[10]);
843*4882a593Smuzhiyun div = (val & CLK_CRYPTO_DIV_CON_MASK) >> CLK_CRYPTO_DIV_CON_SHIFT;
844*4882a593Smuzhiyun
845*4882a593Smuzhiyun return DIV_TO_RATE(rk3368_bus_get_clk(priv->cru, ACLK_BUS), div);
846*4882a593Smuzhiyun }
847*4882a593Smuzhiyun
rk3368_crypto_set_rate(struct rk3368_clk_priv * priv,uint hz)848*4882a593Smuzhiyun static ulong rk3368_crypto_set_rate(struct rk3368_clk_priv *priv,
849*4882a593Smuzhiyun uint hz)
850*4882a593Smuzhiyun {
851*4882a593Smuzhiyun struct rk3368_cru *cru = priv->cru;
852*4882a593Smuzhiyun int src_clk_div;
853*4882a593Smuzhiyun uint p_rate;
854*4882a593Smuzhiyun
855*4882a593Smuzhiyun p_rate = rk3368_bus_get_clk(priv->cru, ACLK_BUS);
856*4882a593Smuzhiyun src_clk_div = DIV_ROUND_UP(p_rate, hz) - 1;
857*4882a593Smuzhiyun assert(src_clk_div < 3);
858*4882a593Smuzhiyun
859*4882a593Smuzhiyun rk_clrsetreg(&cru->clksel_con[10],
860*4882a593Smuzhiyun CLK_CRYPTO_DIV_CON_MASK,
861*4882a593Smuzhiyun src_clk_div << CLK_CRYPTO_DIV_CON_SHIFT);
862*4882a593Smuzhiyun
863*4882a593Smuzhiyun return rk3368_crypto_get_rate(priv);
864*4882a593Smuzhiyun }
865*4882a593Smuzhiyun #endif
866*4882a593Smuzhiyun
rk3368_armclk_set_clk(struct rk3368_clk_priv * priv,int clk_id,ulong hz)867*4882a593Smuzhiyun static ulong rk3368_armclk_set_clk(struct rk3368_clk_priv *priv,
868*4882a593Smuzhiyun int clk_id, ulong hz)
869*4882a593Smuzhiyun {
870*4882a593Smuzhiyun struct rk3368_cru *cru = priv->cru;
871*4882a593Smuzhiyun const struct rockchip_cpu_rate_table *rate;
872*4882a593Smuzhiyun struct pll_div pll_config = {0};
873*4882a593Smuzhiyun ulong old_rate;
874*4882a593Smuzhiyun u32 pll_div, pll_id, con_id;
875*4882a593Smuzhiyun int ret;
876*4882a593Smuzhiyun
877*4882a593Smuzhiyun rate = rockchip_get_cpu_settings(rk3368_cpu_rates, hz);
878*4882a593Smuzhiyun if (!rate) {
879*4882a593Smuzhiyun printf("%s unsupported rate\n", __func__);
880*4882a593Smuzhiyun return -EINVAL;
881*4882a593Smuzhiyun }
882*4882a593Smuzhiyun
883*4882a593Smuzhiyun /*
884*4882a593Smuzhiyun * select apll as cpu/core clock pll source and
885*4882a593Smuzhiyun * set up dependent divisors for PERI and ACLK clocks.
886*4882a593Smuzhiyun * core hz : apll = 1:1
887*4882a593Smuzhiyun */
888*4882a593Smuzhiyun
889*4882a593Smuzhiyun ret = pll_para_config(hz, &pll_config, &pll_div);
890*4882a593Smuzhiyun if (ret)
891*4882a593Smuzhiyun return ret;
892*4882a593Smuzhiyun
893*4882a593Smuzhiyun if (clk_id == ARMCLKB) {
894*4882a593Smuzhiyun old_rate = rkclk_pll_get_rate(priv->cru, APLLB);
895*4882a593Smuzhiyun pll_id = APLLB;
896*4882a593Smuzhiyun con_id = 0;
897*4882a593Smuzhiyun } else {
898*4882a593Smuzhiyun old_rate = rkclk_pll_get_rate(priv->cru, APLLL);
899*4882a593Smuzhiyun pll_id = APLLL;
900*4882a593Smuzhiyun con_id = 2;
901*4882a593Smuzhiyun }
902*4882a593Smuzhiyun
903*4882a593Smuzhiyun if (old_rate > hz) {
904*4882a593Smuzhiyun ret = rkclk_set_pll(priv->cru, pll_id, &pll_config);
905*4882a593Smuzhiyun rk_clrsetreg(&cru->clksel_con[con_id],
906*4882a593Smuzhiyun CORE_CLK_PLL_SEL_MASK | CORE_DIV_CON_MASK,
907*4882a593Smuzhiyun CORE_CLK_PLL_SEL_APLL << CORE_CLK_PLL_SEL_SHIFT |
908*4882a593Smuzhiyun 0 << CORE_DIV_CON_SHIFT);
909*4882a593Smuzhiyun rk_clrsetreg(&cru->clksel_con[con_id + 1],
910*4882a593Smuzhiyun CORE_ACLK_DIV_MASK | CORE_DBG_DIV_MASK,
911*4882a593Smuzhiyun rate->aclk_div << CORE_ACLK_DIV_SHIFT |
912*4882a593Smuzhiyun rate->pclk_div << CORE_DBG_DIV_SHIFT);
913*4882a593Smuzhiyun } else if (old_rate < hz) {
914*4882a593Smuzhiyun rk_clrsetreg(&cru->clksel_con[con_id],
915*4882a593Smuzhiyun CORE_CLK_PLL_SEL_MASK | CORE_DIV_CON_MASK,
916*4882a593Smuzhiyun CORE_CLK_PLL_SEL_APLL << CORE_CLK_PLL_SEL_SHIFT |
917*4882a593Smuzhiyun 0 << CORE_DIV_CON_SHIFT);
918*4882a593Smuzhiyun rk_clrsetreg(&cru->clksel_con[con_id + 1],
919*4882a593Smuzhiyun CORE_ACLK_DIV_MASK | CORE_DBG_DIV_MASK,
920*4882a593Smuzhiyun rate->aclk_div << CORE_ACLK_DIV_SHIFT |
921*4882a593Smuzhiyun rate->pclk_div << CORE_DBG_DIV_SHIFT);
922*4882a593Smuzhiyun ret = rkclk_set_pll(priv->cru, pll_id, &pll_config);
923*4882a593Smuzhiyun }
924*4882a593Smuzhiyun
925*4882a593Smuzhiyun return rkclk_pll_get_rate(priv->cru, pll_id);
926*4882a593Smuzhiyun }
927*4882a593Smuzhiyun
rk3368_clk_get_rate(struct clk * clk)928*4882a593Smuzhiyun static ulong rk3368_clk_get_rate(struct clk *clk)
929*4882a593Smuzhiyun {
930*4882a593Smuzhiyun struct rk3368_clk_priv *priv = dev_get_priv(clk->dev);
931*4882a593Smuzhiyun ulong rate = 0;
932*4882a593Smuzhiyun
933*4882a593Smuzhiyun debug("%s: id %ld\n", __func__, clk->id);
934*4882a593Smuzhiyun switch (clk->id) {
935*4882a593Smuzhiyun case PLL_APLLB:
936*4882a593Smuzhiyun case PLL_APLLL:
937*4882a593Smuzhiyun case PLL_DPLL:
938*4882a593Smuzhiyun case PLL_CPLL:
939*4882a593Smuzhiyun case PLL_GPLL:
940*4882a593Smuzhiyun case PLL_NPLL:
941*4882a593Smuzhiyun rate = rkclk_pll_get_rate(priv->cru, clk->id - 1);
942*4882a593Smuzhiyun break;
943*4882a593Smuzhiyun case ARMCLKB:
944*4882a593Smuzhiyun rate = rkclk_pll_get_rate(priv->cru, APLLB);
945*4882a593Smuzhiyun break;
946*4882a593Smuzhiyun case ARMCLKL:
947*4882a593Smuzhiyun rate = rkclk_pll_get_rate(priv->cru, APLLL);
948*4882a593Smuzhiyun break;
949*4882a593Smuzhiyun case SCLK_SPI0 ... SCLK_SPI2:
950*4882a593Smuzhiyun rate = rk3368_spi_get_clk(priv->cru, clk->id);
951*4882a593Smuzhiyun break;
952*4882a593Smuzhiyun case ACLK_BUS:
953*4882a593Smuzhiyun case HCLK_BUS:
954*4882a593Smuzhiyun case PCLK_BUS:
955*4882a593Smuzhiyun case PCLK_PWM0:
956*4882a593Smuzhiyun case PCLK_PWM1:
957*4882a593Smuzhiyun case PCLK_I2C0:
958*4882a593Smuzhiyun case PCLK_I2C1:
959*4882a593Smuzhiyun rate = rk3368_bus_get_clk(priv->cru, clk->id);
960*4882a593Smuzhiyun break;
961*4882a593Smuzhiyun case ACLK_PERI:
962*4882a593Smuzhiyun case HCLK_PERI:
963*4882a593Smuzhiyun case PCLK_PERI:
964*4882a593Smuzhiyun case PCLK_I2C2:
965*4882a593Smuzhiyun case PCLK_I2C3:
966*4882a593Smuzhiyun case PCLK_I2C4:
967*4882a593Smuzhiyun case PCLK_I2C5:
968*4882a593Smuzhiyun rate = rk3368_peri_get_clk(priv->cru, clk->id);
969*4882a593Smuzhiyun break;
970*4882a593Smuzhiyun #if !IS_ENABLED(CONFIG_SPL_BUILD) || CONFIG_IS_ENABLED(MMC_SUPPORT)
971*4882a593Smuzhiyun case HCLK_SDMMC:
972*4882a593Smuzhiyun case HCLK_EMMC:
973*4882a593Smuzhiyun rate = rk3368_mmc_get_clk(priv->cru, clk->id);
974*4882a593Smuzhiyun break;
975*4882a593Smuzhiyun #endif
976*4882a593Smuzhiyun case SCLK_SARADC:
977*4882a593Smuzhiyun rate = rk3368_saradc_get_clk(priv->cru);
978*4882a593Smuzhiyun break;
979*4882a593Smuzhiyun #if !defined(CONFIG_SPL_BUILD)
980*4882a593Smuzhiyun case ACLK_VOP:
981*4882a593Smuzhiyun case DCLK_VOP:
982*4882a593Smuzhiyun rate = rk3368_vop_get_clk(priv->cru, clk->id);
983*4882a593Smuzhiyun break;
984*4882a593Smuzhiyun case PCLK_WDT:
985*4882a593Smuzhiyun rate = rk3368_alive_get_clk(priv);
986*4882a593Smuzhiyun break;
987*4882a593Smuzhiyun case SCLK_CRYPTO:
988*4882a593Smuzhiyun rate = rk3368_crypto_get_rate(priv);
989*4882a593Smuzhiyun break;
990*4882a593Smuzhiyun #endif
991*4882a593Smuzhiyun default:
992*4882a593Smuzhiyun return -ENOENT;
993*4882a593Smuzhiyun }
994*4882a593Smuzhiyun return rate;
995*4882a593Smuzhiyun }
996*4882a593Smuzhiyun
rk3368_clk_set_rate(struct clk * clk,ulong rate)997*4882a593Smuzhiyun static ulong rk3368_clk_set_rate(struct clk *clk, ulong rate)
998*4882a593Smuzhiyun {
999*4882a593Smuzhiyun __maybe_unused struct rk3368_clk_priv *priv = dev_get_priv(clk->dev);
1000*4882a593Smuzhiyun struct pll_div pll_config = {0};
1001*4882a593Smuzhiyun u32 pll_div;
1002*4882a593Smuzhiyun ulong ret = 0;
1003*4882a593Smuzhiyun
1004*4882a593Smuzhiyun switch (clk->id) {
1005*4882a593Smuzhiyun case PLL_APLLB:
1006*4882a593Smuzhiyun case PLL_APLLL:
1007*4882a593Smuzhiyun case PLL_CPLL:
1008*4882a593Smuzhiyun case PLL_GPLL:
1009*4882a593Smuzhiyun case PLL_NPLL:
1010*4882a593Smuzhiyun ret = pll_para_config(rate, &pll_config, &pll_div);
1011*4882a593Smuzhiyun if (ret)
1012*4882a593Smuzhiyun return ret;
1013*4882a593Smuzhiyun
1014*4882a593Smuzhiyun ret = rkclk_set_pll(priv->cru, clk->id - 1, &pll_config);
1015*4882a593Smuzhiyun break;
1016*4882a593Smuzhiyun case ARMCLKB:
1017*4882a593Smuzhiyun if (priv->armbclk_hz)
1018*4882a593Smuzhiyun ret = rk3368_armclk_set_clk(priv, clk->id, rate);
1019*4882a593Smuzhiyun priv->armbclk_hz = rate;
1020*4882a593Smuzhiyun break;
1021*4882a593Smuzhiyun case ARMCLKL:
1022*4882a593Smuzhiyun if (priv->armlclk_hz)
1023*4882a593Smuzhiyun ret = rk3368_armclk_set_clk(priv, clk->id, rate);
1024*4882a593Smuzhiyun priv->armlclk_hz = rate;
1025*4882a593Smuzhiyun break;
1026*4882a593Smuzhiyun case SCLK_SPI0 ... SCLK_SPI2:
1027*4882a593Smuzhiyun ret = rk3368_spi_set_clk(priv->cru, clk->id, rate);
1028*4882a593Smuzhiyun break;
1029*4882a593Smuzhiyun #if IS_ENABLED(CONFIG_TPL_BUILD)
1030*4882a593Smuzhiyun case SCLK_DDRCLK:
1031*4882a593Smuzhiyun ret = rk3368_ddr_set_clk(priv->cru, rate);
1032*4882a593Smuzhiyun break;
1033*4882a593Smuzhiyun #endif
1034*4882a593Smuzhiyun case ACLK_BUS:
1035*4882a593Smuzhiyun case HCLK_BUS:
1036*4882a593Smuzhiyun case PCLK_BUS:
1037*4882a593Smuzhiyun rate = rk3368_bus_set_clk(priv->cru, clk->id, rate);
1038*4882a593Smuzhiyun break;
1039*4882a593Smuzhiyun case ACLK_PERI:
1040*4882a593Smuzhiyun case HCLK_PERI:
1041*4882a593Smuzhiyun case PCLK_PERI:
1042*4882a593Smuzhiyun rate = rk3368_peri_set_clk(priv->cru, clk->id, rate);
1043*4882a593Smuzhiyun break;
1044*4882a593Smuzhiyun #if !IS_ENABLED(CONFIG_SPL_BUILD) || CONFIG_IS_ENABLED(MMC_SUPPORT)
1045*4882a593Smuzhiyun case HCLK_SDMMC:
1046*4882a593Smuzhiyun case HCLK_EMMC:
1047*4882a593Smuzhiyun ret = rk3368_mmc_set_clk(clk, rate);
1048*4882a593Smuzhiyun break;
1049*4882a593Smuzhiyun #endif
1050*4882a593Smuzhiyun #if CONFIG_IS_ENABLED(GMAC_ROCKCHIP)
1051*4882a593Smuzhiyun case SCLK_MAC:
1052*4882a593Smuzhiyun /* select the external clock */
1053*4882a593Smuzhiyun ret = rk3368_gmac_set_clk(priv->cru, rate);
1054*4882a593Smuzhiyun break;
1055*4882a593Smuzhiyun #endif
1056*4882a593Smuzhiyun case SCLK_SARADC:
1057*4882a593Smuzhiyun ret = rk3368_saradc_set_clk(priv->cru, rate);
1058*4882a593Smuzhiyun break;
1059*4882a593Smuzhiyun #if !defined(CONFIG_SPL_BUILD)
1060*4882a593Smuzhiyun case ACLK_VOP:
1061*4882a593Smuzhiyun case DCLK_VOP:
1062*4882a593Smuzhiyun ret = rk3368_vop_set_clk(priv->cru, clk->id, rate);
1063*4882a593Smuzhiyun break;
1064*4882a593Smuzhiyun case ACLK_CCI_PRE:
1065*4882a593Smuzhiyun ret = 0;
1066*4882a593Smuzhiyun break;
1067*4882a593Smuzhiyun case SCLK_CRYPTO:
1068*4882a593Smuzhiyun ret = rk3368_crypto_set_rate(priv, rate);
1069*4882a593Smuzhiyun break;
1070*4882a593Smuzhiyun #endif
1071*4882a593Smuzhiyun default:
1072*4882a593Smuzhiyun return -ENOENT;
1073*4882a593Smuzhiyun }
1074*4882a593Smuzhiyun
1075*4882a593Smuzhiyun return ret;
1076*4882a593Smuzhiyun }
1077*4882a593Smuzhiyun
rk3368_gmac_set_parent(struct clk * clk,struct clk * parent)1078*4882a593Smuzhiyun static int __maybe_unused rk3368_gmac_set_parent(struct clk *clk, struct clk *parent)
1079*4882a593Smuzhiyun {
1080*4882a593Smuzhiyun struct rk3368_clk_priv *priv = dev_get_priv(clk->dev);
1081*4882a593Smuzhiyun struct rk3368_cru *cru = priv->cru;
1082*4882a593Smuzhiyun const char *clock_output_name;
1083*4882a593Smuzhiyun int ret;
1084*4882a593Smuzhiyun
1085*4882a593Smuzhiyun /*
1086*4882a593Smuzhiyun * If the requested parent is in the same clock-controller and
1087*4882a593Smuzhiyun * the id is SCLK_MAC ("sclk_mac"), switch to the internal
1088*4882a593Smuzhiyun * clock.
1089*4882a593Smuzhiyun */
1090*4882a593Smuzhiyun if ((parent->dev == clk->dev) && (parent->id == SCLK_MAC)) {
1091*4882a593Smuzhiyun debug("%s: switching GAMC to SCLK_MAC\n", __func__);
1092*4882a593Smuzhiyun rk_clrreg(&cru->clksel_con[43], GMAC_MUX_SEL_EXTCLK);
1093*4882a593Smuzhiyun return 0;
1094*4882a593Smuzhiyun }
1095*4882a593Smuzhiyun
1096*4882a593Smuzhiyun /*
1097*4882a593Smuzhiyun * Otherwise, we need to check the clock-output-names of the
1098*4882a593Smuzhiyun * requested parent to see if the requested id is "ext_gmac".
1099*4882a593Smuzhiyun */
1100*4882a593Smuzhiyun ret = dev_read_string_index(parent->dev, "clock-output-names",
1101*4882a593Smuzhiyun parent->id, &clock_output_name);
1102*4882a593Smuzhiyun if (ret < 0)
1103*4882a593Smuzhiyun return -ENODATA;
1104*4882a593Smuzhiyun
1105*4882a593Smuzhiyun /* If this is "ext_gmac", switch to the external clock input */
1106*4882a593Smuzhiyun if (!strcmp(clock_output_name, "ext_gmac")) {
1107*4882a593Smuzhiyun debug("%s: switching GMAC to external clock\n", __func__);
1108*4882a593Smuzhiyun rk_setreg(&cru->clksel_con[43], GMAC_MUX_SEL_EXTCLK);
1109*4882a593Smuzhiyun return 0;
1110*4882a593Smuzhiyun }
1111*4882a593Smuzhiyun
1112*4882a593Smuzhiyun return -EINVAL;
1113*4882a593Smuzhiyun }
1114*4882a593Smuzhiyun
rk3368_clk_set_parent(struct clk * clk,struct clk * parent)1115*4882a593Smuzhiyun static int __maybe_unused rk3368_clk_set_parent(struct clk *clk, struct clk *parent)
1116*4882a593Smuzhiyun {
1117*4882a593Smuzhiyun switch (clk->id) {
1118*4882a593Smuzhiyun case SCLK_MAC:
1119*4882a593Smuzhiyun return rk3368_gmac_set_parent(clk, parent);
1120*4882a593Smuzhiyun }
1121*4882a593Smuzhiyun
1122*4882a593Smuzhiyun debug("%s: unsupported clk %ld\n", __func__, clk->id);
1123*4882a593Smuzhiyun return -ENOENT;
1124*4882a593Smuzhiyun }
1125*4882a593Smuzhiyun
1126*4882a593Smuzhiyun #define ROCKCHIP_MMC_DELAY_SEL BIT(10)
1127*4882a593Smuzhiyun #define ROCKCHIP_MMC_DEGREE_MASK 0x3
1128*4882a593Smuzhiyun #define ROCKCHIP_MMC_DELAYNUM_OFFSET 2
1129*4882a593Smuzhiyun #define ROCKCHIP_MMC_DELAYNUM_MASK (0xff << ROCKCHIP_MMC_DELAYNUM_OFFSET)
1130*4882a593Smuzhiyun
1131*4882a593Smuzhiyun #define PSECS_PER_SEC 1000000000000LL
1132*4882a593Smuzhiyun /*
1133*4882a593Smuzhiyun * Each fine delay is between 44ps-77ps. Assume each fine delay is 60ps to
1134*4882a593Smuzhiyun * simplify calculations. So 45degs could be anywhere between 33deg and 57.8deg.
1135*4882a593Smuzhiyun */
1136*4882a593Smuzhiyun #define ROCKCHIP_MMC_DELAY_ELEMENT_PSEC 60
1137*4882a593Smuzhiyun
rk3368_mmc_get_phase(struct clk * clk)1138*4882a593Smuzhiyun int rk3368_mmc_get_phase(struct clk *clk)
1139*4882a593Smuzhiyun {
1140*4882a593Smuzhiyun struct rk3368_clk_priv *priv = dev_get_priv(clk->dev);
1141*4882a593Smuzhiyun struct rk3368_cru *cru = priv->cru;
1142*4882a593Smuzhiyun u32 raw_value, delay_num;
1143*4882a593Smuzhiyun u16 degrees = 0;
1144*4882a593Smuzhiyun ulong rate;
1145*4882a593Smuzhiyun
1146*4882a593Smuzhiyun rate = rk3368_clk_get_rate(clk);
1147*4882a593Smuzhiyun
1148*4882a593Smuzhiyun if (rate < 0)
1149*4882a593Smuzhiyun return rate;
1150*4882a593Smuzhiyun
1151*4882a593Smuzhiyun if (clk->id == SCLK_EMMC_SAMPLE)
1152*4882a593Smuzhiyun raw_value = readl(&cru->emmc_con[1]);
1153*4882a593Smuzhiyun else if (clk->id == SCLK_SDMMC_SAMPLE)
1154*4882a593Smuzhiyun raw_value = readl(&cru->sdmmc_con[1]);
1155*4882a593Smuzhiyun else
1156*4882a593Smuzhiyun raw_value = readl(&cru->sdio0_con[1]);
1157*4882a593Smuzhiyun
1158*4882a593Smuzhiyun raw_value >>= 1;
1159*4882a593Smuzhiyun degrees = (raw_value & ROCKCHIP_MMC_DEGREE_MASK) * 90;
1160*4882a593Smuzhiyun
1161*4882a593Smuzhiyun if (raw_value & ROCKCHIP_MMC_DELAY_SEL) {
1162*4882a593Smuzhiyun /* degrees/delaynum * 10000 */
1163*4882a593Smuzhiyun unsigned long factor = (ROCKCHIP_MMC_DELAY_ELEMENT_PSEC / 10) *
1164*4882a593Smuzhiyun 36 * (rate / 1000000);
1165*4882a593Smuzhiyun
1166*4882a593Smuzhiyun delay_num = (raw_value & ROCKCHIP_MMC_DELAYNUM_MASK);
1167*4882a593Smuzhiyun delay_num >>= ROCKCHIP_MMC_DELAYNUM_OFFSET;
1168*4882a593Smuzhiyun degrees += DIV_ROUND_CLOSEST(delay_num * factor, 10000);
1169*4882a593Smuzhiyun }
1170*4882a593Smuzhiyun
1171*4882a593Smuzhiyun return degrees % 360;
1172*4882a593Smuzhiyun }
1173*4882a593Smuzhiyun
rk3368_mmc_set_phase(struct clk * clk,u32 degrees)1174*4882a593Smuzhiyun int rk3368_mmc_set_phase(struct clk *clk, u32 degrees)
1175*4882a593Smuzhiyun {
1176*4882a593Smuzhiyun struct rk3368_clk_priv *priv = dev_get_priv(clk->dev);
1177*4882a593Smuzhiyun struct rk3368_cru *cru = priv->cru;
1178*4882a593Smuzhiyun u8 nineties, remainder, delay_num;
1179*4882a593Smuzhiyun u32 raw_value, delay;
1180*4882a593Smuzhiyun ulong rate;
1181*4882a593Smuzhiyun
1182*4882a593Smuzhiyun rate = rk3368_clk_get_rate(clk);
1183*4882a593Smuzhiyun
1184*4882a593Smuzhiyun if (rate < 0)
1185*4882a593Smuzhiyun return rate;
1186*4882a593Smuzhiyun
1187*4882a593Smuzhiyun nineties = degrees / 90;
1188*4882a593Smuzhiyun remainder = (degrees % 90);
1189*4882a593Smuzhiyun
1190*4882a593Smuzhiyun /*
1191*4882a593Smuzhiyun * Convert to delay; do a little extra work to make sure we
1192*4882a593Smuzhiyun * don't overflow 32-bit / 64-bit numbers.
1193*4882a593Smuzhiyun */
1194*4882a593Smuzhiyun delay = 10000000; /* PSECS_PER_SEC / 10000 / 10 */
1195*4882a593Smuzhiyun delay *= remainder;
1196*4882a593Smuzhiyun delay = DIV_ROUND_CLOSEST(delay, (rate / 1000) * 36 *
1197*4882a593Smuzhiyun (ROCKCHIP_MMC_DELAY_ELEMENT_PSEC / 10));
1198*4882a593Smuzhiyun
1199*4882a593Smuzhiyun delay_num = (u8)min_t(u32, delay, 255);
1200*4882a593Smuzhiyun
1201*4882a593Smuzhiyun raw_value = delay_num ? ROCKCHIP_MMC_DELAY_SEL : 0;
1202*4882a593Smuzhiyun raw_value |= delay_num << ROCKCHIP_MMC_DELAYNUM_OFFSET;
1203*4882a593Smuzhiyun raw_value |= nineties;
1204*4882a593Smuzhiyun
1205*4882a593Smuzhiyun raw_value <<= 1;
1206*4882a593Smuzhiyun if (clk->id == SCLK_EMMC_SAMPLE)
1207*4882a593Smuzhiyun writel(raw_value | 0xffff0000, &cru->emmc_con[1]);
1208*4882a593Smuzhiyun else if (clk->id == SCLK_SDMMC_SAMPLE)
1209*4882a593Smuzhiyun writel(raw_value | 0xffff0000, &cru->sdmmc_con[1]);
1210*4882a593Smuzhiyun else
1211*4882a593Smuzhiyun writel(raw_value | 0xffff0000, &cru->sdio0_con[1]);
1212*4882a593Smuzhiyun
1213*4882a593Smuzhiyun debug("mmc set_phase(%d) delay_nums=%u reg=%#x actual_degrees=%d\n",
1214*4882a593Smuzhiyun degrees, delay_num, raw_value, rk3368_mmc_get_phase(clk));
1215*4882a593Smuzhiyun
1216*4882a593Smuzhiyun return 0;
1217*4882a593Smuzhiyun }
1218*4882a593Smuzhiyun
rk3368_clk_get_phase(struct clk * clk)1219*4882a593Smuzhiyun static int rk3368_clk_get_phase(struct clk *clk)
1220*4882a593Smuzhiyun {
1221*4882a593Smuzhiyun int ret;
1222*4882a593Smuzhiyun
1223*4882a593Smuzhiyun debug("%s %ld\n", __func__, clk->id);
1224*4882a593Smuzhiyun switch (clk->id) {
1225*4882a593Smuzhiyun case SCLK_EMMC_SAMPLE:
1226*4882a593Smuzhiyun case SCLK_SDMMC_SAMPLE:
1227*4882a593Smuzhiyun case SCLK_SDIO0_SAMPLE:
1228*4882a593Smuzhiyun ret = rk3368_mmc_get_phase(clk);
1229*4882a593Smuzhiyun break;
1230*4882a593Smuzhiyun default:
1231*4882a593Smuzhiyun return -ENOENT;
1232*4882a593Smuzhiyun }
1233*4882a593Smuzhiyun
1234*4882a593Smuzhiyun return ret;
1235*4882a593Smuzhiyun }
1236*4882a593Smuzhiyun
rk3368_clk_set_phase(struct clk * clk,int degrees)1237*4882a593Smuzhiyun static int rk3368_clk_set_phase(struct clk *clk, int degrees)
1238*4882a593Smuzhiyun {
1239*4882a593Smuzhiyun int ret;
1240*4882a593Smuzhiyun
1241*4882a593Smuzhiyun debug("%s %ld\n", __func__, clk->id);
1242*4882a593Smuzhiyun switch (clk->id) {
1243*4882a593Smuzhiyun case SCLK_EMMC_SAMPLE:
1244*4882a593Smuzhiyun case SCLK_SDMMC_SAMPLE:
1245*4882a593Smuzhiyun case SCLK_SDIO0_SAMPLE:
1246*4882a593Smuzhiyun ret = rk3368_mmc_set_phase(clk, degrees);
1247*4882a593Smuzhiyun break;
1248*4882a593Smuzhiyun default:
1249*4882a593Smuzhiyun return -ENOENT;
1250*4882a593Smuzhiyun }
1251*4882a593Smuzhiyun
1252*4882a593Smuzhiyun return ret;
1253*4882a593Smuzhiyun }
1254*4882a593Smuzhiyun
1255*4882a593Smuzhiyun static struct clk_ops rk3368_clk_ops = {
1256*4882a593Smuzhiyun .get_rate = rk3368_clk_get_rate,
1257*4882a593Smuzhiyun .set_rate = rk3368_clk_set_rate,
1258*4882a593Smuzhiyun .get_phase = rk3368_clk_get_phase,
1259*4882a593Smuzhiyun .set_phase = rk3368_clk_set_phase,
1260*4882a593Smuzhiyun #if CONFIG_IS_ENABLED(OF_CONTROL) && !CONFIG_IS_ENABLED(OF_PLATDATA)
1261*4882a593Smuzhiyun .set_parent = rk3368_clk_set_parent,
1262*4882a593Smuzhiyun #endif
1263*4882a593Smuzhiyun };
1264*4882a593Smuzhiyun
1265*4882a593Smuzhiyun #if IS_ENABLED(CONFIG_SPL_BUILD) || IS_ENABLED(CONFIG_TPL_BUILD)
rkclk_init(struct rk3368_cru * cru)1266*4882a593Smuzhiyun static void rkclk_init(struct rk3368_cru *cru)
1267*4882a593Smuzhiyun {
1268*4882a593Smuzhiyun u32 apllb, aplll, dpll, cpll, gpll;
1269*4882a593Smuzhiyun
1270*4882a593Smuzhiyun rkclk_set_pll(cru, APLLB, &apll_b_init_cfg);
1271*4882a593Smuzhiyun rkclk_set_pll(cru, APLLL, &apll_l_init_cfg);
1272*4882a593Smuzhiyun #if !defined(CONFIG_TPL_BUILD)
1273*4882a593Smuzhiyun /*
1274*4882a593Smuzhiyun * If we plan to return to the boot ROM, we can't increase the
1275*4882a593Smuzhiyun * GPLL rate from the SPL stage.
1276*4882a593Smuzhiyun */
1277*4882a593Smuzhiyun rkclk_set_pll(cru, GPLL, &gpll_init_cfg);
1278*4882a593Smuzhiyun rkclk_set_pll(cru, CPLL, &cpll_init_cfg);
1279*4882a593Smuzhiyun #endif
1280*4882a593Smuzhiyun rk_clrsetreg(&cru->clksel_con[37], (1 << 8), 1 << 8);
1281*4882a593Smuzhiyun apllb = rkclk_pll_get_rate(cru, APLLB);
1282*4882a593Smuzhiyun aplll = rkclk_pll_get_rate(cru, APLLL);
1283*4882a593Smuzhiyun dpll = rkclk_pll_get_rate(cru, DPLL);
1284*4882a593Smuzhiyun cpll = rkclk_pll_get_rate(cru, CPLL);
1285*4882a593Smuzhiyun gpll = rkclk_pll_get_rate(cru, GPLL);
1286*4882a593Smuzhiyun
1287*4882a593Smuzhiyun debug("%s apllb(%d) apll(%d) dpll(%d) cpll(%d) gpll(%d)\n",
1288*4882a593Smuzhiyun __func__, apllb, aplll, dpll, cpll, gpll);
1289*4882a593Smuzhiyun }
1290*4882a593Smuzhiyun #endif
1291*4882a593Smuzhiyun
rk3368_clk_probe(struct udevice * dev)1292*4882a593Smuzhiyun static int rk3368_clk_probe(struct udevice *dev)
1293*4882a593Smuzhiyun {
1294*4882a593Smuzhiyun struct rk3368_clk_priv __maybe_unused *priv = dev_get_priv(dev);
1295*4882a593Smuzhiyun int ret;
1296*4882a593Smuzhiyun #if CONFIG_IS_ENABLED(OF_PLATDATA)
1297*4882a593Smuzhiyun struct rk3368_clk_plat *plat = dev_get_platdata(dev);
1298*4882a593Smuzhiyun
1299*4882a593Smuzhiyun priv->cru = map_sysmem(plat->dtd.reg[0], plat->dtd.reg[1]);
1300*4882a593Smuzhiyun #endif
1301*4882a593Smuzhiyun priv->sync_kernel = false;
1302*4882a593Smuzhiyun if (!priv->armlclk_enter_hz)
1303*4882a593Smuzhiyun priv->armlclk_enter_hz = rkclk_pll_get_rate(priv->cru, APLLL);
1304*4882a593Smuzhiyun if (!priv->armbclk_enter_hz)
1305*4882a593Smuzhiyun priv->armbclk_enter_hz = rkclk_pll_get_rate(priv->cru, APLLB);
1306*4882a593Smuzhiyun #if IS_ENABLED(CONFIG_SPL_BUILD) || IS_ENABLED(CONFIG_TPL_BUILD)
1307*4882a593Smuzhiyun rkclk_init(priv->cru);
1308*4882a593Smuzhiyun #endif
1309*4882a593Smuzhiyun rkclk_set_pll(priv->cru, NPLL, rkclk_get_pll_config(NPLL_HZ));
1310*4882a593Smuzhiyun if (!priv->armlclk_init_hz)
1311*4882a593Smuzhiyun priv->armlclk_init_hz = rkclk_pll_get_rate(priv->cru, APLLL);
1312*4882a593Smuzhiyun if (!priv->armbclk_init_hz)
1313*4882a593Smuzhiyun priv->armbclk_init_hz = rkclk_pll_get_rate(priv->cru, APLLB);
1314*4882a593Smuzhiyun /* Process 'assigned-{clocks/clock-parents/clock-rates}' properties */
1315*4882a593Smuzhiyun ret = clk_set_defaults(dev);
1316*4882a593Smuzhiyun if (ret)
1317*4882a593Smuzhiyun debug("%s clk_set_defaults failed %d\n", __func__, ret);
1318*4882a593Smuzhiyun else
1319*4882a593Smuzhiyun priv->sync_kernel = true;
1320*4882a593Smuzhiyun return 0;
1321*4882a593Smuzhiyun }
1322*4882a593Smuzhiyun
rk3368_clk_ofdata_to_platdata(struct udevice * dev)1323*4882a593Smuzhiyun static int rk3368_clk_ofdata_to_platdata(struct udevice *dev)
1324*4882a593Smuzhiyun {
1325*4882a593Smuzhiyun #if !CONFIG_IS_ENABLED(OF_PLATDATA)
1326*4882a593Smuzhiyun struct rk3368_clk_priv *priv = dev_get_priv(dev);
1327*4882a593Smuzhiyun
1328*4882a593Smuzhiyun priv->cru = dev_read_addr_ptr(dev);
1329*4882a593Smuzhiyun #endif
1330*4882a593Smuzhiyun
1331*4882a593Smuzhiyun return 0;
1332*4882a593Smuzhiyun }
1333*4882a593Smuzhiyun
rk3368_clk_bind(struct udevice * dev)1334*4882a593Smuzhiyun static int rk3368_clk_bind(struct udevice *dev)
1335*4882a593Smuzhiyun {
1336*4882a593Smuzhiyun int ret;
1337*4882a593Smuzhiyun struct udevice *sys_child, *sf_child;
1338*4882a593Smuzhiyun struct sysreset_reg *priv;
1339*4882a593Smuzhiyun struct softreset_reg *sf_priv;
1340*4882a593Smuzhiyun
1341*4882a593Smuzhiyun /* The reset driver does not have a device node, so bind it here */
1342*4882a593Smuzhiyun ret = device_bind_driver(dev, "rockchip_sysreset", "sysreset",
1343*4882a593Smuzhiyun &sys_child);
1344*4882a593Smuzhiyun if (ret) {
1345*4882a593Smuzhiyun debug("Warning: No sysreset driver: ret=%d\n", ret);
1346*4882a593Smuzhiyun } else {
1347*4882a593Smuzhiyun priv = malloc(sizeof(struct sysreset_reg));
1348*4882a593Smuzhiyun priv->glb_srst_fst_value = offsetof(struct rk3368_cru,
1349*4882a593Smuzhiyun glb_srst_fst_val);
1350*4882a593Smuzhiyun priv->glb_srst_snd_value = offsetof(struct rk3368_cru,
1351*4882a593Smuzhiyun glb_srst_snd_val);
1352*4882a593Smuzhiyun sys_child->priv = priv;
1353*4882a593Smuzhiyun }
1354*4882a593Smuzhiyun
1355*4882a593Smuzhiyun ret = device_bind_driver_to_node(dev, "rockchip_reset", "reset",
1356*4882a593Smuzhiyun dev_ofnode(dev), &sf_child);
1357*4882a593Smuzhiyun if (ret) {
1358*4882a593Smuzhiyun debug("Warning: No rockchip reset driver: ret=%d\n", ret);
1359*4882a593Smuzhiyun } else {
1360*4882a593Smuzhiyun sf_priv = malloc(sizeof(struct softreset_reg));
1361*4882a593Smuzhiyun sf_priv->sf_reset_offset = offsetof(struct rk3368_cru,
1362*4882a593Smuzhiyun softrst_con[0]);
1363*4882a593Smuzhiyun sf_priv->sf_reset_num = 15;
1364*4882a593Smuzhiyun sf_child->priv = sf_priv;
1365*4882a593Smuzhiyun }
1366*4882a593Smuzhiyun
1367*4882a593Smuzhiyun return 0;
1368*4882a593Smuzhiyun }
1369*4882a593Smuzhiyun
1370*4882a593Smuzhiyun static const struct udevice_id rk3368_clk_ids[] = {
1371*4882a593Smuzhiyun { .compatible = "rockchip,rk3368-cru" },
1372*4882a593Smuzhiyun { }
1373*4882a593Smuzhiyun };
1374*4882a593Smuzhiyun
1375*4882a593Smuzhiyun U_BOOT_DRIVER(rockchip_rk3368_cru) = {
1376*4882a593Smuzhiyun .name = "rockchip_rk3368_cru",
1377*4882a593Smuzhiyun .id = UCLASS_CLK,
1378*4882a593Smuzhiyun .of_match = rk3368_clk_ids,
1379*4882a593Smuzhiyun .priv_auto_alloc_size = sizeof(struct rk3368_clk_priv),
1380*4882a593Smuzhiyun #if CONFIG_IS_ENABLED(OF_PLATDATA)
1381*4882a593Smuzhiyun .platdata_auto_alloc_size = sizeof(struct rk3368_clk_plat),
1382*4882a593Smuzhiyun #endif
1383*4882a593Smuzhiyun .ofdata_to_platdata = rk3368_clk_ofdata_to_platdata,
1384*4882a593Smuzhiyun .ops = &rk3368_clk_ops,
1385*4882a593Smuzhiyun .bind = rk3368_clk_bind,
1386*4882a593Smuzhiyun .probe = rk3368_clk_probe,
1387*4882a593Smuzhiyun };
1388*4882a593Smuzhiyun
1389*4882a593Smuzhiyun #if !defined(CONFIG_SPL_BUILD)
1390*4882a593Smuzhiyun /**
1391*4882a593Smuzhiyun * soc_clk_dump() - Print clock frequencies
1392*4882a593Smuzhiyun * Returns zero on success
1393*4882a593Smuzhiyun *
1394*4882a593Smuzhiyun * Implementation for the clk dump command.
1395*4882a593Smuzhiyun */
soc_clk_dump(void)1396*4882a593Smuzhiyun int soc_clk_dump(void)
1397*4882a593Smuzhiyun {
1398*4882a593Smuzhiyun struct udevice *cru_dev;
1399*4882a593Smuzhiyun struct rk3368_clk_priv *priv;
1400*4882a593Smuzhiyun const struct rk3368_clk_info *clk_dump;
1401*4882a593Smuzhiyun struct clk clk;
1402*4882a593Smuzhiyun unsigned long clk_count = ARRAY_SIZE(clks_dump);
1403*4882a593Smuzhiyun unsigned long rate;
1404*4882a593Smuzhiyun int i, ret;
1405*4882a593Smuzhiyun
1406*4882a593Smuzhiyun ret = uclass_get_device_by_driver(UCLASS_CLK,
1407*4882a593Smuzhiyun DM_GET_DRIVER(rockchip_rk3368_cru),
1408*4882a593Smuzhiyun &cru_dev);
1409*4882a593Smuzhiyun if (ret) {
1410*4882a593Smuzhiyun printf("%s failed to get cru device\n", __func__);
1411*4882a593Smuzhiyun return ret;
1412*4882a593Smuzhiyun }
1413*4882a593Smuzhiyun
1414*4882a593Smuzhiyun priv = dev_get_priv(cru_dev);
1415*4882a593Smuzhiyun printf("CLK: (%s. arml: enter %lu KHz, init %lu KHz, kernel %lu%s)\n",
1416*4882a593Smuzhiyun priv->sync_kernel ? "sync kernel" : "uboot",
1417*4882a593Smuzhiyun priv->armlclk_enter_hz / 1000,
1418*4882a593Smuzhiyun priv->armlclk_init_hz / 1000,
1419*4882a593Smuzhiyun priv->set_armclk_rate ? priv->armlclk_hz / 1000 : 0,
1420*4882a593Smuzhiyun priv->set_armclk_rate ? " KHz" : "N/A");
1421*4882a593Smuzhiyun printf("CLK: (%s. armb: enter %lu KHz, init %lu KHz, kernel %lu%s)\n",
1422*4882a593Smuzhiyun priv->sync_kernel ? "sync kernel" : "uboot",
1423*4882a593Smuzhiyun priv->armbclk_enter_hz / 1000,
1424*4882a593Smuzhiyun priv->armbclk_init_hz / 1000,
1425*4882a593Smuzhiyun priv->set_armclk_rate ? priv->armlclk_hz / 1000 : 0,
1426*4882a593Smuzhiyun priv->set_armclk_rate ? " KHz" : "N/A");
1427*4882a593Smuzhiyun for (i = 0; i < clk_count; i++) {
1428*4882a593Smuzhiyun clk_dump = &clks_dump[i];
1429*4882a593Smuzhiyun if (clk_dump->name) {
1430*4882a593Smuzhiyun clk.id = clk_dump->id;
1431*4882a593Smuzhiyun if (clk_dump->is_cru)
1432*4882a593Smuzhiyun ret = clk_request(cru_dev, &clk);
1433*4882a593Smuzhiyun if (ret < 0)
1434*4882a593Smuzhiyun return ret;
1435*4882a593Smuzhiyun
1436*4882a593Smuzhiyun rate = clk_get_rate(&clk);
1437*4882a593Smuzhiyun clk_free(&clk);
1438*4882a593Smuzhiyun if (i == 0) {
1439*4882a593Smuzhiyun if (rate < 0)
1440*4882a593Smuzhiyun printf(" %s %s\n", clk_dump->name,
1441*4882a593Smuzhiyun "unknown");
1442*4882a593Smuzhiyun else
1443*4882a593Smuzhiyun printf(" %s %lu KHz\n", clk_dump->name,
1444*4882a593Smuzhiyun rate / 1000);
1445*4882a593Smuzhiyun } else {
1446*4882a593Smuzhiyun if (rate < 0)
1447*4882a593Smuzhiyun printf(" %s %s\n", clk_dump->name,
1448*4882a593Smuzhiyun "unknown");
1449*4882a593Smuzhiyun else
1450*4882a593Smuzhiyun printf(" %s %lu KHz\n", clk_dump->name,
1451*4882a593Smuzhiyun rate / 1000);
1452*4882a593Smuzhiyun }
1453*4882a593Smuzhiyun }
1454*4882a593Smuzhiyun }
1455*4882a593Smuzhiyun
1456*4882a593Smuzhiyun return 0;
1457*4882a593Smuzhiyun }
1458*4882a593Smuzhiyun #endif
1459