blob: 7e6c37487163613107c6f655e23c7d8edb07a1f8 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Peng Fan88057bc2018-01-10 13:20:22 +08002/*
3 * Copyright 2017 NXP
4 *
5 * Peng Fan <peng.fan@nxp.com>
Peng Fan88057bc2018-01-10 13:20:22 +08006 */
7
Simon Glassed38aef2020-05-10 11:40:03 -06008#include <command.h>
Peng Fan88057bc2018-01-10 13:20:22 +08009#include <asm/arch/clock.h>
10#include <asm/arch/imx-regs.h>
11#include <asm/io.h>
12#include <asm/arch/sys_proto.h>
13#include <errno.h>
Simon Glassdbd79542020-05-10 11:40:11 -060014#include <linux/delay.h>
Peng Fan88057bc2018-01-10 13:20:22 +080015#include <linux/iopoll.h>
16
Peng Fan88057bc2018-01-10 13:20:22 +080017static struct anamix_pll *ana_pll = (struct anamix_pll *)ANATOP_BASE_ADDR;
18
Peng Fan72f1bb22020-04-22 11:18:23 +080019static u32 get_root_clk(enum clk_root_index clock_id);
20
Peng Fan88057bc2018-01-10 13:20:22 +080021static u32 decode_frac_pll(enum clk_root_src frac_pll)
22{
23 u32 pll_cfg0, pll_cfg1, pllout;
24 u32 pll_refclk_sel, pll_refclk;
25 u32 divr_val, divq_val, divf_val, divff, divfi;
26 u32 pllout_div_shift, pllout_div_mask, pllout_div;
27
28 switch (frac_pll) {
29 case ARM_PLL_CLK:
30 pll_cfg0 = readl(&ana_pll->arm_pll_cfg0);
31 pll_cfg1 = readl(&ana_pll->arm_pll_cfg1);
32 pllout_div_shift = HW_FRAC_ARM_PLL_DIV_SHIFT;
33 pllout_div_mask = HW_FRAC_ARM_PLL_DIV_MASK;
34 break;
35 default:
36 printf("Frac PLL %d not supporte\n", frac_pll);
37 return 0;
38 }
39
40 pllout_div = readl(&ana_pll->frac_pllout_div_cfg);
41 pllout_div = (pllout_div & pllout_div_mask) >> pllout_div_shift;
42
43 /* Power down */
44 if (pll_cfg0 & FRAC_PLL_PD_MASK)
45 return 0;
46
47 /* output not enabled */
48 if ((pll_cfg0 & FRAC_PLL_CLKE_MASK) == 0)
49 return 0;
50
51 pll_refclk_sel = pll_cfg0 & FRAC_PLL_REFCLK_SEL_MASK;
52
53 if (pll_refclk_sel == FRAC_PLL_REFCLK_SEL_OSC_25M)
54 pll_refclk = 25000000u;
55 else if (pll_refclk_sel == FRAC_PLL_REFCLK_SEL_OSC_27M)
56 pll_refclk = 27000000u;
57 else if (pll_refclk_sel == FRAC_PLL_REFCLK_SEL_HDMI_PHY_27M)
58 pll_refclk = 27000000u;
59 else
60 pll_refclk = 0;
61
62 if (pll_cfg0 & FRAC_PLL_BYPASS_MASK)
63 return pll_refclk;
64
65 divr_val = (pll_cfg0 & FRAC_PLL_REFCLK_DIV_VAL_MASK) >>
66 FRAC_PLL_REFCLK_DIV_VAL_SHIFT;
67 divq_val = pll_cfg0 & FRAC_PLL_OUTPUT_DIV_VAL_MASK;
68
69 divff = (pll_cfg1 & FRAC_PLL_FRAC_DIV_CTL_MASK) >>
70 FRAC_PLL_FRAC_DIV_CTL_SHIFT;
71 divfi = pll_cfg1 & FRAC_PLL_INT_DIV_CTL_MASK;
72
73 divf_val = 1 + divfi + divff / (1 << 24);
74
75 pllout = pll_refclk / (divr_val + 1) * 8 * divf_val /
76 ((divq_val + 1) * 2);
77
78 return pllout / (pllout_div + 1);
79}
80
81static u32 decode_sscg_pll(enum clk_root_src sscg_pll)
82{
83 u32 pll_cfg0, pll_cfg1, pll_cfg2;
84 u32 pll_refclk_sel, pll_refclk;
85 u32 divr1, divr2, divf1, divf2, divq, div;
86 u32 sse;
87 u32 pll_clke;
88 u32 pllout_div_shift, pllout_div_mask, pllout_div;
89 u32 pllout;
90
91 switch (sscg_pll) {
92 case SYSTEM_PLL1_800M_CLK:
93 case SYSTEM_PLL1_400M_CLK:
94 case SYSTEM_PLL1_266M_CLK:
95 case SYSTEM_PLL1_200M_CLK:
96 case SYSTEM_PLL1_160M_CLK:
97 case SYSTEM_PLL1_133M_CLK:
98 case SYSTEM_PLL1_100M_CLK:
99 case SYSTEM_PLL1_80M_CLK:
100 case SYSTEM_PLL1_40M_CLK:
101 pll_cfg0 = readl(&ana_pll->sys_pll1_cfg0);
102 pll_cfg1 = readl(&ana_pll->sys_pll1_cfg1);
103 pll_cfg2 = readl(&ana_pll->sys_pll1_cfg2);
104 pllout_div_shift = HW_SSCG_SYSTEM_PLL1_DIV_SHIFT;
105 pllout_div_mask = HW_SSCG_SYSTEM_PLL1_DIV_MASK;
106 break;
107 case SYSTEM_PLL2_1000M_CLK:
108 case SYSTEM_PLL2_500M_CLK:
109 case SYSTEM_PLL2_333M_CLK:
110 case SYSTEM_PLL2_250M_CLK:
111 case SYSTEM_PLL2_200M_CLK:
112 case SYSTEM_PLL2_166M_CLK:
113 case SYSTEM_PLL2_125M_CLK:
114 case SYSTEM_PLL2_100M_CLK:
115 case SYSTEM_PLL2_50M_CLK:
116 pll_cfg0 = readl(&ana_pll->sys_pll2_cfg0);
117 pll_cfg1 = readl(&ana_pll->sys_pll2_cfg1);
118 pll_cfg2 = readl(&ana_pll->sys_pll2_cfg2);
119 pllout_div_shift = HW_SSCG_SYSTEM_PLL2_DIV_SHIFT;
120 pllout_div_mask = HW_SSCG_SYSTEM_PLL2_DIV_MASK;
121 break;
122 case SYSTEM_PLL3_CLK:
123 pll_cfg0 = readl(&ana_pll->sys_pll3_cfg0);
124 pll_cfg1 = readl(&ana_pll->sys_pll3_cfg1);
125 pll_cfg2 = readl(&ana_pll->sys_pll3_cfg2);
126 pllout_div_shift = HW_SSCG_SYSTEM_PLL3_DIV_SHIFT;
127 pllout_div_mask = HW_SSCG_SYSTEM_PLL3_DIV_MASK;
128 break;
129 case DRAM_PLL1_CLK:
130 pll_cfg0 = readl(&ana_pll->dram_pll_cfg0);
131 pll_cfg1 = readl(&ana_pll->dram_pll_cfg1);
132 pll_cfg2 = readl(&ana_pll->dram_pll_cfg2);
133 pllout_div_shift = HW_SSCG_DRAM_PLL_DIV_SHIFT;
134 pllout_div_mask = HW_SSCG_DRAM_PLL_DIV_MASK;
135 break;
136 default:
137 printf("sscg pll %d not supporte\n", sscg_pll);
138 return 0;
139 }
140
141 switch (sscg_pll) {
142 case DRAM_PLL1_CLK:
143 pll_clke = SSCG_PLL_DRAM_PLL_CLKE_MASK;
144 div = 1;
145 break;
146 case SYSTEM_PLL3_CLK:
147 pll_clke = SSCG_PLL_PLL3_CLKE_MASK;
148 div = 1;
149 break;
150 case SYSTEM_PLL2_1000M_CLK:
151 case SYSTEM_PLL1_800M_CLK:
152 pll_clke = SSCG_PLL_CLKE_MASK;
153 div = 1;
154 break;
155 case SYSTEM_PLL2_500M_CLK:
156 case SYSTEM_PLL1_400M_CLK:
157 pll_clke = SSCG_PLL_DIV2_CLKE_MASK;
158 div = 2;
159 break;
160 case SYSTEM_PLL2_333M_CLK:
161 case SYSTEM_PLL1_266M_CLK:
162 pll_clke = SSCG_PLL_DIV3_CLKE_MASK;
163 div = 3;
164 break;
165 case SYSTEM_PLL2_250M_CLK:
166 case SYSTEM_PLL1_200M_CLK:
167 pll_clke = SSCG_PLL_DIV4_CLKE_MASK;
168 div = 4;
169 break;
170 case SYSTEM_PLL2_200M_CLK:
171 case SYSTEM_PLL1_160M_CLK:
172 pll_clke = SSCG_PLL_DIV5_CLKE_MASK;
173 div = 5;
174 break;
175 case SYSTEM_PLL2_166M_CLK:
176 case SYSTEM_PLL1_133M_CLK:
177 pll_clke = SSCG_PLL_DIV6_CLKE_MASK;
178 div = 6;
179 break;
180 case SYSTEM_PLL2_125M_CLK:
181 case SYSTEM_PLL1_100M_CLK:
182 pll_clke = SSCG_PLL_DIV8_CLKE_MASK;
183 div = 8;
184 break;
185 case SYSTEM_PLL2_100M_CLK:
186 case SYSTEM_PLL1_80M_CLK:
187 pll_clke = SSCG_PLL_DIV10_CLKE_MASK;
188 div = 10;
189 break;
190 case SYSTEM_PLL2_50M_CLK:
191 case SYSTEM_PLL1_40M_CLK:
192 pll_clke = SSCG_PLL_DIV20_CLKE_MASK;
193 div = 20;
194 break;
195 default:
196 printf("sscg pll %d not supporte\n", sscg_pll);
197 return 0;
198 }
199
200 /* Power down */
201 if (pll_cfg0 & SSCG_PLL_PD_MASK)
202 return 0;
203
204 /* output not enabled */
205 if ((pll_cfg0 & pll_clke) == 0)
206 return 0;
207
208 pllout_div = readl(&ana_pll->sscg_pllout_div_cfg);
209 pllout_div = (pllout_div & pllout_div_mask) >> pllout_div_shift;
210
211 pll_refclk_sel = pll_cfg0 & SSCG_PLL_REFCLK_SEL_MASK;
212
213 if (pll_refclk_sel == SSCG_PLL_REFCLK_SEL_OSC_25M)
214 pll_refclk = 25000000u;
215 else if (pll_refclk_sel == SSCG_PLL_REFCLK_SEL_OSC_27M)
216 pll_refclk = 27000000u;
217 else if (pll_refclk_sel == SSCG_PLL_REFCLK_SEL_HDMI_PHY_27M)
218 pll_refclk = 27000000u;
219 else
220 pll_refclk = 0;
221
222 /* We assume bypass1/2 are the same value */
223 if ((pll_cfg0 & SSCG_PLL_BYPASS1_MASK) ||
224 (pll_cfg0 & SSCG_PLL_BYPASS2_MASK))
225 return pll_refclk;
226
227 divr1 = (pll_cfg2 & SSCG_PLL_REF_DIVR1_MASK) >>
228 SSCG_PLL_REF_DIVR1_SHIFT;
229 divr2 = (pll_cfg2 & SSCG_PLL_REF_DIVR2_MASK) >>
230 SSCG_PLL_REF_DIVR2_SHIFT;
231 divf1 = (pll_cfg2 & SSCG_PLL_FEEDBACK_DIV_F1_MASK) >>
232 SSCG_PLL_FEEDBACK_DIV_F1_SHIFT;
233 divf2 = (pll_cfg2 & SSCG_PLL_FEEDBACK_DIV_F2_MASK) >>
234 SSCG_PLL_FEEDBACK_DIV_F2_SHIFT;
235 divq = (pll_cfg2 & SSCG_PLL_OUTPUT_DIV_VAL_MASK) >>
236 SSCG_PLL_OUTPUT_DIV_VAL_SHIFT;
237 sse = pll_cfg1 & SSCG_PLL_SSE_MASK;
238
239 if (sse)
240 sse = 8;
241 else
242 sse = 2;
243
244 pllout = pll_refclk / (divr1 + 1) * sse * (divf1 + 1) /
245 (divr2 + 1) * (divf2 + 1) / (divq + 1);
246
247 return pllout / (pllout_div + 1) / div;
248}
249
250static u32 get_root_src_clk(enum clk_root_src root_src)
251{
252 switch (root_src) {
253 case OSC_25M_CLK:
254 return 25000000;
255 case OSC_27M_CLK:
Fabio Estevam6f162f22018-12-28 16:43:01 -0200256 return 27000000;
Peng Fan88057bc2018-01-10 13:20:22 +0800257 case OSC_32K_CLK:
Fabio Estevam6f162f22018-12-28 16:43:01 -0200258 return 32768;
Peng Fan88057bc2018-01-10 13:20:22 +0800259 case ARM_PLL_CLK:
260 return decode_frac_pll(root_src);
261 case SYSTEM_PLL1_800M_CLK:
262 case SYSTEM_PLL1_400M_CLK:
263 case SYSTEM_PLL1_266M_CLK:
264 case SYSTEM_PLL1_200M_CLK:
265 case SYSTEM_PLL1_160M_CLK:
266 case SYSTEM_PLL1_133M_CLK:
267 case SYSTEM_PLL1_100M_CLK:
268 case SYSTEM_PLL1_80M_CLK:
269 case SYSTEM_PLL1_40M_CLK:
270 case SYSTEM_PLL2_1000M_CLK:
271 case SYSTEM_PLL2_500M_CLK:
272 case SYSTEM_PLL2_333M_CLK:
273 case SYSTEM_PLL2_250M_CLK:
274 case SYSTEM_PLL2_200M_CLK:
275 case SYSTEM_PLL2_166M_CLK:
276 case SYSTEM_PLL2_125M_CLK:
277 case SYSTEM_PLL2_100M_CLK:
278 case SYSTEM_PLL2_50M_CLK:
279 case SYSTEM_PLL3_CLK:
280 return decode_sscg_pll(root_src);
Peng Fan72f1bb22020-04-22 11:18:23 +0800281 case ARM_A53_ALT_CLK:
282 return get_root_clk(ARM_A53_CLK_ROOT);
Peng Fan88057bc2018-01-10 13:20:22 +0800283 default:
284 return 0;
285 }
286
287 return 0;
288}
289
290static u32 get_root_clk(enum clk_root_index clock_id)
291{
292 enum clk_root_src root_src;
293 u32 post_podf, pre_podf, root_src_clk;
294
295 if (clock_root_enabled(clock_id) <= 0)
296 return 0;
297
298 if (clock_get_prediv(clock_id, &pre_podf) < 0)
299 return 0;
300
301 if (clock_get_postdiv(clock_id, &post_podf) < 0)
302 return 0;
303
304 if (clock_get_src(clock_id, &root_src) < 0)
305 return 0;
306
307 root_src_clk = get_root_src_clk(root_src);
308
309 return root_src_clk / (post_podf + 1) / (pre_podf + 1);
310}
311
Ye Liebabd8d2021-03-25 17:30:17 +0800312#ifdef CONFIG_IMX_HAB
313void hab_caam_clock_enable(unsigned char enable)
314{
315 /* The CAAM clock is always on for iMX8M */
316}
317#endif
318
Peng Fan88057bc2018-01-10 13:20:22 +0800319#ifdef CONFIG_MXC_OCOTP
320void enable_ocotp_clk(unsigned char enable)
321{
322 clock_enable(CCGR_OCOTP, !!enable);
323}
324#endif
325
326int enable_i2c_clk(unsigned char enable, unsigned int i2c_num)
327{
328 /* 0 - 3 is valid i2c num */
329 if (i2c_num > 3)
330 return -EINVAL;
331
332 clock_enable(CCGR_I2C1 + i2c_num, !!enable);
333
334 return 0;
335}
336
Peng Fan72f1bb22020-04-22 11:18:23 +0800337u32 get_arm_core_clk(void)
338{
339 enum clk_root_src root_src;
340 u32 root_src_clk;
341
342 if (clock_get_src(CORE_SEL_CFG, &root_src) < 0)
343 return 0;
344
345 root_src_clk = get_root_src_clk(root_src);
346
347 return root_src_clk;
348}
349
Peng Fanc38755a2019-08-27 06:25:48 +0000350unsigned int mxc_get_clock(enum mxc_clock clk)
Peng Fan88057bc2018-01-10 13:20:22 +0800351{
352 u32 val;
353
Peng Fan72f1bb22020-04-22 11:18:23 +0800354 switch (clk) {
Peng Fan728e6b22019-12-11 06:17:12 +0000355 case MXC_ARM_CLK:
Peng Fan72f1bb22020-04-22 11:18:23 +0800356 return get_arm_core_clk();
Peng Fan728e6b22019-12-11 06:17:12 +0000357 case MXC_IPG_CLK:
Peng Fan88057bc2018-01-10 13:20:22 +0800358 clock_get_target_val(IPG_CLK_ROOT, &val);
359 val = val & 0x3;
360 return get_root_clk(AHB_CLK_ROOT) / (val + 1);
Heiko Thiery9b294362021-09-09 14:59:18 +0200361 case MXC_CSPI_CLK:
362 return get_root_clk(ECSPI1_CLK_ROOT);
Peng Fan728e6b22019-12-11 06:17:12 +0000363 case MXC_ESDHC_CLK:
364 return get_root_clk(USDHC1_CLK_ROOT);
365 case MXC_ESDHC2_CLK:
366 return get_root_clk(USDHC2_CLK_ROOT);
Heiko Thiery9b294362021-09-09 14:59:18 +0200367 case MXC_I2C_CLK:
368 return get_root_clk(I2C1_CLK_ROOT);
369 case MXC_UART_CLK:
370 return get_root_clk(UART1_CLK_ROOT);
371 case MXC_QSPI_CLK:
372 return get_root_clk(QSPI_CLK_ROOT);
Peng Fan728e6b22019-12-11 06:17:12 +0000373 default:
374 return get_root_clk(clk);
Peng Fan88057bc2018-01-10 13:20:22 +0800375 }
Peng Fan88057bc2018-01-10 13:20:22 +0800376}
377
378u32 imx_get_uartclk(void)
379{
380 return mxc_get_clock(UART1_CLK_ROOT);
381}
382
383void mxs_set_lcdclk(u32 base_addr, u32 freq)
384{
385 /*
386 * LCDIF_PIXEL_CLK: select 800MHz root clock,
387 * select pre divider 8, output is 100 MHz
388 */
389 clock_set_target_val(LCDIF_PIXEL_CLK_ROOT, CLK_ROOT_ON |
390 CLK_ROOT_SOURCE_SEL(4) |
391 CLK_ROOT_PRE_DIV(CLK_ROOT_PRE_DIV8));
392}
393
394void init_wdog_clk(void)
395{
396 clock_enable(CCGR_WDOG1, 0);
397 clock_enable(CCGR_WDOG2, 0);
398 clock_enable(CCGR_WDOG3, 0);
399 clock_set_target_val(WDOG_CLK_ROOT, CLK_ROOT_ON |
400 CLK_ROOT_SOURCE_SEL(0));
401 clock_set_target_val(WDOG_CLK_ROOT, CLK_ROOT_ON |
402 CLK_ROOT_SOURCE_SEL(0));
403 clock_set_target_val(WDOG_CLK_ROOT, CLK_ROOT_ON |
404 CLK_ROOT_SOURCE_SEL(0));
405 clock_enable(CCGR_WDOG1, 1);
406 clock_enable(CCGR_WDOG2, 1);
407 clock_enable(CCGR_WDOG3, 1);
408}
409
Ye Lia6af9702021-02-21 08:26:23 -0800410void init_usb_clk(void)
411{
412 if (!is_usb_boot()) {
413 clock_enable(CCGR_USB_CTRL1, 0);
414 clock_enable(CCGR_USB_CTRL2, 0);
415 clock_enable(CCGR_USB_PHY1, 0);
416 clock_enable(CCGR_USB_PHY2, 0);
417 /* 500MHz */
418 clock_set_target_val(USB_BUS_CLK_ROOT, CLK_ROOT_ON |
419 CLK_ROOT_SOURCE_SEL(1));
420 /* 100MHz */
421 clock_set_target_val(USB_CORE_REF_CLK_ROOT, CLK_ROOT_ON |
422 CLK_ROOT_SOURCE_SEL(1));
423 /* 100MHz */
424 clock_set_target_val(USB_PHY_REF_CLK_ROOT, CLK_ROOT_ON |
425 CLK_ROOT_SOURCE_SEL(1));
426 clock_enable(CCGR_USB_CTRL1, 1);
427 clock_enable(CCGR_USB_CTRL2, 1);
428 clock_enable(CCGR_USB_PHY1, 1);
429 clock_enable(CCGR_USB_PHY2, 1);
430 }
431}
Peng Fan88057bc2018-01-10 13:20:22 +0800432
Peng Fan6e6cff72019-10-16 10:24:22 +0000433void init_nand_clk(void)
434{
435 clock_enable(CCGR_RAWNAND, 0);
436 clock_set_target_val(NAND_CLK_ROOT,
437 CLK_ROOT_ON | CLK_ROOT_SOURCE_SEL(3) |
438 CLK_ROOT_POST_DIV(CLK_ROOT_POST_DIV4));
439 clock_enable(CCGR_RAWNAND, 1);
440}
441
Peng Fan88057bc2018-01-10 13:20:22 +0800442void init_uart_clk(u32 index)
443{
444 /* Set uart clock root 25M OSC */
445 switch (index) {
446 case 0:
447 clock_enable(CCGR_UART1, 0);
448 clock_set_target_val(UART1_CLK_ROOT, CLK_ROOT_ON |
449 CLK_ROOT_SOURCE_SEL(0));
450 clock_enable(CCGR_UART1, 1);
451 return;
452 case 1:
453 clock_enable(CCGR_UART2, 0);
454 clock_set_target_val(UART2_CLK_ROOT, CLK_ROOT_ON |
455 CLK_ROOT_SOURCE_SEL(0));
456 clock_enable(CCGR_UART2, 1);
457 return;
458 case 2:
459 clock_enable(CCGR_UART3, 0);
460 clock_set_target_val(UART3_CLK_ROOT, CLK_ROOT_ON |
461 CLK_ROOT_SOURCE_SEL(0));
462 clock_enable(CCGR_UART3, 1);
463 return;
464 case 3:
465 clock_enable(CCGR_UART4, 0);
466 clock_set_target_val(UART4_CLK_ROOT, CLK_ROOT_ON |
467 CLK_ROOT_SOURCE_SEL(0));
468 clock_enable(CCGR_UART4, 1);
469 return;
470 default:
471 printf("Invalid uart index\n");
472 return;
473 }
474}
475
476void init_clk_usdhc(u32 index)
477{
478 /*
479 * set usdhc clock root
480 * sys pll1 400M
481 */
482 switch (index) {
483 case 0:
484 clock_enable(CCGR_USDHC1, 0);
485 clock_set_target_val(USDHC1_CLK_ROOT, CLK_ROOT_ON |
Ye Lie5f8f162019-10-25 01:28:48 -0700486 CLK_ROOT_SOURCE_SEL(1));
Peng Fan88057bc2018-01-10 13:20:22 +0800487 clock_enable(CCGR_USDHC1, 1);
488 return;
489 case 1:
490 clock_enable(CCGR_USDHC2, 0);
491 clock_set_target_val(USDHC2_CLK_ROOT, CLK_ROOT_ON |
Ye Lie5f8f162019-10-25 01:28:48 -0700492 CLK_ROOT_SOURCE_SEL(1));
Peng Fan88057bc2018-01-10 13:20:22 +0800493 clock_enable(CCGR_USDHC2, 1);
494 return;
495 default:
496 printf("Invalid usdhc index\n");
497 return;
498 }
499}
500
501int set_clk_qspi(void)
502{
503 /*
504 * set qspi root
505 * sys pll1 100M
506 */
507 clock_enable(CCGR_QSPI, 0);
508 clock_set_target_val(QSPI_CLK_ROOT, CLK_ROOT_ON |
509 CLK_ROOT_SOURCE_SEL(7));
510 clock_enable(CCGR_QSPI, 1);
511
512 return 0;
513}
514
515#ifdef CONFIG_FEC_MXC
516int set_clk_enet(enum enet_freq type)
517{
518 u32 target;
519 u32 enet1_ref;
520
521 switch (type) {
522 case ENET_125MHZ:
523 enet1_ref = ENET1_REF_CLK_ROOT_FROM_PLL_ENET_MAIN_125M_CLK;
524 break;
525 case ENET_50MHZ:
526 enet1_ref = ENET1_REF_CLK_ROOT_FROM_PLL_ENET_MAIN_50M_CLK;
527 break;
528 case ENET_25MHZ:
529 enet1_ref = ENET1_REF_CLK_ROOT_FROM_PLL_ENET_MAIN_25M_CLK;
530 break;
531 default:
532 return -EINVAL;
533 }
534
535 /* disable the clock first */
536 clock_enable(CCGR_ENET1, 0);
537 clock_enable(CCGR_SIM_ENET, 0);
538
539 /* set enet axi clock 266Mhz */
540 target = CLK_ROOT_ON | ENET_AXI_CLK_ROOT_FROM_SYS1_PLL_266M |
541 CLK_ROOT_PRE_DIV(CLK_ROOT_PRE_DIV1) |
542 CLK_ROOT_POST_DIV(CLK_ROOT_POST_DIV1);
543 clock_set_target_val(ENET_AXI_CLK_ROOT, target);
544
545 target = CLK_ROOT_ON | enet1_ref |
546 CLK_ROOT_PRE_DIV(CLK_ROOT_PRE_DIV1) |
547 CLK_ROOT_POST_DIV(CLK_ROOT_POST_DIV1);
548 clock_set_target_val(ENET_REF_CLK_ROOT, target);
549
550 target = CLK_ROOT_ON |
551 ENET1_TIME_CLK_ROOT_FROM_PLL_ENET_MAIN_100M_CLK |
552 CLK_ROOT_PRE_DIV(CLK_ROOT_PRE_DIV1) |
553 CLK_ROOT_POST_DIV(CLK_ROOT_POST_DIV4);
554 clock_set_target_val(ENET_TIMER_CLK_ROOT, target);
555
556 /* enable clock */
557 clock_enable(CCGR_SIM_ENET, 1);
558 clock_enable(CCGR_ENET1, 1);
559
560 return 0;
561}
562#endif
563
564u32 imx_get_fecclk(void)
565{
566 return get_root_clk(ENET_AXI_CLK_ROOT);
567}
568
Peng Fan24b0d252018-11-20 10:19:32 +0000569static struct dram_bypass_clk_setting imx8mq_dram_bypass_tbl[] = {
570 DRAM_BYPASS_ROOT_CONFIG(MHZ(100), 2, CLK_ROOT_PRE_DIV1, 2,
571 CLK_ROOT_PRE_DIV2),
572 DRAM_BYPASS_ROOT_CONFIG(MHZ(250), 3, CLK_ROOT_PRE_DIV2, 2,
573 CLK_ROOT_PRE_DIV2),
574 DRAM_BYPASS_ROOT_CONFIG(MHZ(400), 1, CLK_ROOT_PRE_DIV2, 3,
575 CLK_ROOT_PRE_DIV2),
576};
577
578void dram_enable_bypass(ulong clk_val)
579{
580 int i;
581 struct dram_bypass_clk_setting *config;
582
583 for (i = 0; i < ARRAY_SIZE(imx8mq_dram_bypass_tbl); i++) {
584 if (clk_val == imx8mq_dram_bypass_tbl[i].clk)
585 break;
586 }
587
588 if (i == ARRAY_SIZE(imx8mq_dram_bypass_tbl)) {
589 printf("No matched freq table %lu\n", clk_val);
590 return;
591 }
592
593 config = &imx8mq_dram_bypass_tbl[i];
594
595 clock_set_target_val(DRAM_ALT_CLK_ROOT, CLK_ROOT_ON |
596 CLK_ROOT_SOURCE_SEL(config->alt_root_sel) |
597 CLK_ROOT_PRE_DIV(config->alt_pre_div));
598 clock_set_target_val(DRAM_APB_CLK_ROOT, CLK_ROOT_ON |
599 CLK_ROOT_SOURCE_SEL(config->apb_root_sel) |
600 CLK_ROOT_PRE_DIV(config->apb_pre_div));
601 clock_set_target_val(DRAM_SEL_CFG, CLK_ROOT_ON |
602 CLK_ROOT_SOURCE_SEL(1));
603}
604
605void dram_disable_bypass(void)
606{
607 clock_set_target_val(DRAM_SEL_CFG, CLK_ROOT_ON |
608 CLK_ROOT_SOURCE_SEL(0));
609 clock_set_target_val(DRAM_APB_CLK_ROOT, CLK_ROOT_ON |
610 CLK_ROOT_SOURCE_SEL(4) |
611 CLK_ROOT_PRE_DIV(CLK_ROOT_PRE_DIV5));
612}
613
Peng Fan88057bc2018-01-10 13:20:22 +0800614#ifdef CONFIG_SPL_BUILD
Peng Fan24b0d252018-11-20 10:19:32 +0000615void dram_pll_init(ulong pll_val)
Peng Fan88057bc2018-01-10 13:20:22 +0800616{
Peng Fan88057bc2018-01-10 13:20:22 +0800617 u32 val;
Peng Fan24b0d252018-11-20 10:19:32 +0000618 void __iomem *pll_control_reg = &ana_pll->dram_pll_cfg0;
619 void __iomem *pll_cfg_reg2 = &ana_pll->dram_pll_cfg2;
Peng Fan88057bc2018-01-10 13:20:22 +0800620
Peng Fan24b0d252018-11-20 10:19:32 +0000621 /* Bypass */
622 setbits_le32(pll_control_reg, SSCG_PLL_BYPASS1_MASK);
623 setbits_le32(pll_control_reg, SSCG_PLL_BYPASS2_MASK);
Peng Fan88057bc2018-01-10 13:20:22 +0800624
Peng Fan24b0d252018-11-20 10:19:32 +0000625 switch (pll_val) {
626 case MHZ(800):
627 val = readl(pll_cfg_reg2);
628 val &= ~(SSCG_PLL_OUTPUT_DIV_VAL_MASK |
629 SSCG_PLL_FEEDBACK_DIV_F2_MASK |
630 SSCG_PLL_FEEDBACK_DIV_F1_MASK |
631 SSCG_PLL_REF_DIVR2_MASK);
632 val |= SSCG_PLL_OUTPUT_DIV_VAL(0);
633 val |= SSCG_PLL_FEEDBACK_DIV_F2_VAL(11);
634 val |= SSCG_PLL_FEEDBACK_DIV_F1_VAL(39);
635 val |= SSCG_PLL_REF_DIVR2_VAL(29);
636 writel(val, pll_cfg_reg2);
637 break;
638 case MHZ(600):
639 val = readl(pll_cfg_reg2);
640 val &= ~(SSCG_PLL_OUTPUT_DIV_VAL_MASK |
641 SSCG_PLL_FEEDBACK_DIV_F2_MASK |
642 SSCG_PLL_FEEDBACK_DIV_F1_MASK |
643 SSCG_PLL_REF_DIVR2_MASK);
644 val |= SSCG_PLL_OUTPUT_DIV_VAL(1);
645 val |= SSCG_PLL_FEEDBACK_DIV_F2_VAL(17);
646 val |= SSCG_PLL_FEEDBACK_DIV_F1_VAL(39);
647 val |= SSCG_PLL_REF_DIVR2_VAL(29);
648 writel(val, pll_cfg_reg2);
649 break;
650 case MHZ(400):
651 val = readl(pll_cfg_reg2);
652 val &= ~(SSCG_PLL_OUTPUT_DIV_VAL_MASK |
653 SSCG_PLL_FEEDBACK_DIV_F2_MASK |
654 SSCG_PLL_FEEDBACK_DIV_F1_MASK |
655 SSCG_PLL_REF_DIVR2_MASK);
656 val |= SSCG_PLL_OUTPUT_DIV_VAL(1);
657 val |= SSCG_PLL_FEEDBACK_DIV_F2_VAL(11);
658 val |= SSCG_PLL_FEEDBACK_DIV_F1_VAL(39);
659 val |= SSCG_PLL_REF_DIVR2_VAL(29);
660 writel(val, pll_cfg_reg2);
661 break;
662 case MHZ(167):
663 val = readl(pll_cfg_reg2);
664 val &= ~(SSCG_PLL_OUTPUT_DIV_VAL_MASK |
665 SSCG_PLL_FEEDBACK_DIV_F2_MASK |
666 SSCG_PLL_FEEDBACK_DIV_F1_MASK |
667 SSCG_PLL_REF_DIVR2_MASK);
668 val |= SSCG_PLL_OUTPUT_DIV_VAL(3);
669 val |= SSCG_PLL_FEEDBACK_DIV_F2_VAL(8);
670 val |= SSCG_PLL_FEEDBACK_DIV_F1_VAL(45);
671 val |= SSCG_PLL_REF_DIVR2_VAL(30);
672 writel(val, pll_cfg_reg2);
673 break;
674 default:
675 break;
676 }
Peng Fan88057bc2018-01-10 13:20:22 +0800677
678 /* Clear power down bit */
Peng Fan24b0d252018-11-20 10:19:32 +0000679 clrbits_le32(pll_control_reg, SSCG_PLL_PD_MASK);
Peng Fan88057bc2018-01-10 13:20:22 +0800680 /* Eanble ARM_PLL/SYS_PLL */
Peng Fan24b0d252018-11-20 10:19:32 +0000681 setbits_le32(pll_control_reg, SSCG_PLL_DRAM_PLL_CLKE_MASK);
Peng Fan88057bc2018-01-10 13:20:22 +0800682
683 /* Clear bypass */
Peng Fan24b0d252018-11-20 10:19:32 +0000684 clrbits_le32(pll_control_reg, SSCG_PLL_BYPASS1_MASK);
Peng Fan88057bc2018-01-10 13:20:22 +0800685 __udelay(100);
Peng Fan24b0d252018-11-20 10:19:32 +0000686 clrbits_le32(pll_control_reg, SSCG_PLL_BYPASS2_MASK);
Peng Fan88057bc2018-01-10 13:20:22 +0800687 /* Wait lock */
Peng Fan24b0d252018-11-20 10:19:32 +0000688 while (!(readl(pll_control_reg) & SSCG_PLL_LOCK_MASK))
689 ;
Peng Fan88057bc2018-01-10 13:20:22 +0800690}
691
Pedro Jardim38e96b22020-01-23 10:21:53 -0300692static int frac_pll_init(u32 pll, enum frac_pll_out_val val)
Peng Fan88057bc2018-01-10 13:20:22 +0800693{
694 void __iomem *pll_cfg0, __iomem *pll_cfg1;
Peng Fan72f1bb22020-04-22 11:18:23 +0800695 u32 val_cfg0, val_cfg1, divq;
Peng Fan88057bc2018-01-10 13:20:22 +0800696 int ret;
697
698 switch (pll) {
699 case ANATOP_ARM_PLL:
700 pll_cfg0 = &ana_pll->arm_pll_cfg0;
701 pll_cfg1 = &ana_pll->arm_pll_cfg1;
702
Peng Fan72f1bb22020-04-22 11:18:23 +0800703 if (val == FRAC_PLL_OUT_1000M) {
Peng Fan88057bc2018-01-10 13:20:22 +0800704 val_cfg1 = FRAC_PLL_INT_DIV_CTL_VAL(49);
Peng Fan72f1bb22020-04-22 11:18:23 +0800705 divq = 0;
706 } else {
Peng Fan88057bc2018-01-10 13:20:22 +0800707 val_cfg1 = FRAC_PLL_INT_DIV_CTL_VAL(79);
Peng Fan72f1bb22020-04-22 11:18:23 +0800708 divq = 1;
709 }
Peng Fan88057bc2018-01-10 13:20:22 +0800710 val_cfg0 = FRAC_PLL_CLKE_MASK | FRAC_PLL_REFCLK_SEL_OSC_25M |
711 FRAC_PLL_LOCK_SEL_MASK | FRAC_PLL_NEWDIV_VAL_MASK |
712 FRAC_PLL_REFCLK_DIV_VAL(4) |
Peng Fan72f1bb22020-04-22 11:18:23 +0800713 FRAC_PLL_OUTPUT_DIV_VAL(divq);
Peng Fan88057bc2018-01-10 13:20:22 +0800714 break;
715 default:
716 return -EINVAL;
717 }
718
719 /* bypass the clock */
720 setbits_le32(pll_cfg0, FRAC_PLL_BYPASS_MASK);
721 /* Set the value */
722 writel(val_cfg1, pll_cfg1);
723 writel(val_cfg0 | FRAC_PLL_BYPASS_MASK, pll_cfg0);
724 val_cfg0 = readl(pll_cfg0);
725 /* unbypass the clock */
726 clrbits_le32(pll_cfg0, FRAC_PLL_BYPASS_MASK);
727 ret = readl_poll_timeout(pll_cfg0, val_cfg0,
728 val_cfg0 & FRAC_PLL_LOCK_MASK, 1);
729 if (ret)
730 printf("%s timeout\n", __func__);
731 clrbits_le32(pll_cfg0, FRAC_PLL_NEWDIV_VAL_MASK);
732
733 return 0;
734}
735
Peng Fan88057bc2018-01-10 13:20:22 +0800736
737int clock_init(void)
738{
739 u32 grade;
740
741 clock_set_target_val(ARM_A53_CLK_ROOT, CLK_ROOT_ON |
742 CLK_ROOT_SOURCE_SEL(0));
743
744 /*
745 * 8MQ only supports two grades: consumer and industrial.
746 * We set ARM clock to 1Ghz for consumer, 800Mhz for industrial
747 */
748 grade = get_cpu_temp_grade(NULL, NULL);
Peng Fan72f1bb22020-04-22 11:18:23 +0800749 if (!grade)
Peng Fan88057bc2018-01-10 13:20:22 +0800750 frac_pll_init(ANATOP_ARM_PLL, FRAC_PLL_OUT_1000M);
Peng Fan72f1bb22020-04-22 11:18:23 +0800751 else
752 frac_pll_init(ANATOP_ARM_PLL, FRAC_PLL_OUT_800M);
753
754 /* Bypass CCM A53 ROOT, Switch to ARM PLL -> MUX-> CPU */
755 clock_set_target_val(CORE_SEL_CFG, CLK_ROOT_SOURCE_SEL(1));
756
Peng Fan88057bc2018-01-10 13:20:22 +0800757 /*
758 * According to ANAMIX SPEC
759 * sys pll1 fixed at 800MHz
760 * sys pll2 fixed at 1GHz
761 * Here we only enable the outputs.
762 */
763 setbits_le32(&ana_pll->sys_pll1_cfg0, SSCG_PLL_CLKE_MASK |
764 SSCG_PLL_DIV2_CLKE_MASK | SSCG_PLL_DIV3_CLKE_MASK |
765 SSCG_PLL_DIV4_CLKE_MASK | SSCG_PLL_DIV5_CLKE_MASK |
766 SSCG_PLL_DIV6_CLKE_MASK | SSCG_PLL_DIV8_CLKE_MASK |
767 SSCG_PLL_DIV10_CLKE_MASK | SSCG_PLL_DIV20_CLKE_MASK);
768
769 setbits_le32(&ana_pll->sys_pll2_cfg0, SSCG_PLL_CLKE_MASK |
770 SSCG_PLL_DIV2_CLKE_MASK | SSCG_PLL_DIV3_CLKE_MASK |
771 SSCG_PLL_DIV4_CLKE_MASK | SSCG_PLL_DIV5_CLKE_MASK |
772 SSCG_PLL_DIV6_CLKE_MASK | SSCG_PLL_DIV8_CLKE_MASK |
773 SSCG_PLL_DIV10_CLKE_MASK | SSCG_PLL_DIV20_CLKE_MASK);
774
775 clock_set_target_val(NAND_USDHC_BUS_CLK_ROOT, CLK_ROOT_ON |
776 CLK_ROOT_SOURCE_SEL(1));
777
778 init_wdog_clk();
779 clock_enable(CCGR_TSENSOR, 1);
Peng Fanc23fbdd2019-10-16 10:24:17 +0000780 clock_enable(CCGR_OCOTP, 1);
Peng Fan88057bc2018-01-10 13:20:22 +0800781
Peng Fan1049fc2e2019-10-16 10:24:20 +0000782 /* config GIC ROOT to sys_pll2_200m */
783 clock_enable(CCGR_GIC, 0);
784 clock_set_target_val(GIC_CLK_ROOT,
785 CLK_ROOT_ON | CLK_ROOT_SOURCE_SEL(1));
786 clock_enable(CCGR_GIC, 1);
787
Peng Fan88057bc2018-01-10 13:20:22 +0800788 return 0;
789}
790#endif
791
792/*
793 * Dump some clockes.
794 */
795#ifndef CONFIG_SPL_BUILD
Simon Glassed38aef2020-05-10 11:40:03 -0600796static int do_imx8m_showclocks(struct cmd_tbl *cmdtp, int flag, int argc,
797 char *const argv[])
Peng Fan88057bc2018-01-10 13:20:22 +0800798{
799 u32 freq;
800
801 freq = decode_frac_pll(ARM_PLL_CLK);
802 printf("ARM_PLL %8d MHz\n", freq / 1000000);
Ye Lia94ceee2020-03-23 19:57:55 -0700803 freq = decode_sscg_pll(DRAM_PLL1_CLK);
804 printf("DRAM_PLL %8d MHz\n", freq / 1000000);
Peng Fan88057bc2018-01-10 13:20:22 +0800805 freq = decode_sscg_pll(SYSTEM_PLL1_800M_CLK);
806 printf("SYS_PLL1_800 %8d MHz\n", freq / 1000000);
807 freq = decode_sscg_pll(SYSTEM_PLL1_400M_CLK);
808 printf("SYS_PLL1_400 %8d MHz\n", freq / 1000000);
809 freq = decode_sscg_pll(SYSTEM_PLL1_266M_CLK);
810 printf("SYS_PLL1_266 %8d MHz\n", freq / 1000000);
811 freq = decode_sscg_pll(SYSTEM_PLL1_200M_CLK);
812 printf("SYS_PLL1_200 %8d MHz\n", freq / 1000000);
813 freq = decode_sscg_pll(SYSTEM_PLL1_160M_CLK);
814 printf("SYS_PLL1_160 %8d MHz\n", freq / 1000000);
815 freq = decode_sscg_pll(SYSTEM_PLL1_133M_CLK);
816 printf("SYS_PLL1_133 %8d MHz\n", freq / 1000000);
817 freq = decode_sscg_pll(SYSTEM_PLL1_100M_CLK);
818 printf("SYS_PLL1_100 %8d MHz\n", freq / 1000000);
819 freq = decode_sscg_pll(SYSTEM_PLL1_80M_CLK);
820 printf("SYS_PLL1_80 %8d MHz\n", freq / 1000000);
821 freq = decode_sscg_pll(SYSTEM_PLL1_40M_CLK);
822 printf("SYS_PLL1_40 %8d MHz\n", freq / 1000000);
823 freq = decode_sscg_pll(SYSTEM_PLL2_1000M_CLK);
824 printf("SYS_PLL2_1000 %8d MHz\n", freq / 1000000);
825 freq = decode_sscg_pll(SYSTEM_PLL2_500M_CLK);
826 printf("SYS_PLL2_500 %8d MHz\n", freq / 1000000);
827 freq = decode_sscg_pll(SYSTEM_PLL2_333M_CLK);
828 printf("SYS_PLL2_333 %8d MHz\n", freq / 1000000);
829 freq = decode_sscg_pll(SYSTEM_PLL2_250M_CLK);
830 printf("SYS_PLL2_250 %8d MHz\n", freq / 1000000);
831 freq = decode_sscg_pll(SYSTEM_PLL2_200M_CLK);
832 printf("SYS_PLL2_200 %8d MHz\n", freq / 1000000);
833 freq = decode_sscg_pll(SYSTEM_PLL2_166M_CLK);
834 printf("SYS_PLL2_166 %8d MHz\n", freq / 1000000);
835 freq = decode_sscg_pll(SYSTEM_PLL2_125M_CLK);
836 printf("SYS_PLL2_125 %8d MHz\n", freq / 1000000);
837 freq = decode_sscg_pll(SYSTEM_PLL2_100M_CLK);
838 printf("SYS_PLL2_100 %8d MHz\n", freq / 1000000);
839 freq = decode_sscg_pll(SYSTEM_PLL2_50M_CLK);
840 printf("SYS_PLL2_50 %8d MHz\n", freq / 1000000);
841 freq = decode_sscg_pll(SYSTEM_PLL3_CLK);
842 printf("SYS_PLL3 %8d MHz\n", freq / 1000000);
843 freq = mxc_get_clock(UART1_CLK_ROOT);
844 printf("UART1 %8d MHz\n", freq / 1000000);
845 freq = mxc_get_clock(USDHC1_CLK_ROOT);
846 printf("USDHC1 %8d MHz\n", freq / 1000000);
847 freq = mxc_get_clock(QSPI_CLK_ROOT);
848 printf("QSPI %8d MHz\n", freq / 1000000);
849 return 0;
850}
851
852U_BOOT_CMD(
Peng Fan39945c12018-11-20 10:19:25 +0000853 clocks, CONFIG_SYS_MAXARGS, 1, do_imx8m_showclocks,
Peng Fan88057bc2018-01-10 13:20:22 +0800854 "display clocks",
855 ""
856);
857#endif