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