1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright 2018-2019 NXP
9 #include <asm/arch/clock.h>
10 #include <asm/arch/imx-regs.h>
11 #include <asm/arch/sys_proto.h>
16 DECLARE_GLOBAL_DATA_PTR;
18 static struct anamix_pll *ana_pll = (struct anamix_pll *)ANATOP_BASE_ADDR;
20 void enable_ocotp_clk(unsigned char enable)
22 clock_enable(CCGR_OCOTP, !!enable);
25 int enable_i2c_clk(unsigned char enable, unsigned i2c_num)
27 /* 0 - 3 is valid i2c num */
31 clock_enable(CCGR_I2C1 + i2c_num, !!enable);
36 #ifdef CONFIG_SPL_BUILD
37 static struct imx_int_pll_rate_table imx8mm_fracpll_tbl[] = {
38 PLL_1443X_RATE(1000000000U, 250, 3, 1, 0),
39 PLL_1443X_RATE(800000000U, 300, 9, 0, 0),
40 PLL_1443X_RATE(750000000U, 250, 8, 0, 0),
41 PLL_1443X_RATE(650000000U, 325, 3, 2, 0),
42 PLL_1443X_RATE(600000000U, 300, 3, 2, 0),
43 PLL_1443X_RATE(594000000U, 99, 1, 2, 0),
44 PLL_1443X_RATE(400000000U, 300, 9, 1, 0),
45 PLL_1443X_RATE(266666667U, 400, 9, 2, 0),
46 PLL_1443X_RATE(167000000U, 334, 3, 4, 0),
47 PLL_1443X_RATE(100000000U, 300, 9, 3, 0),
50 static int fracpll_configure(enum pll_clocks pll, u32 freq)
55 struct imx_int_pll_rate_table *rate;
57 for (i = 0; i < ARRAY_SIZE(imx8mm_fracpll_tbl); i++) {
58 if (freq == imx8mm_fracpll_tbl[i].rate)
62 if (i == ARRAY_SIZE(imx8mm_fracpll_tbl)) {
63 printf("No matched freq table %u\n", freq);
67 rate = &imx8mm_fracpll_tbl[i];
71 setbits_le32(GPC_BASE_ADDR + 0xEC, 1 << 7);
72 setbits_le32(GPC_BASE_ADDR + 0xF8, 1 << 5);
73 writel(SRC_DDR1_ENABLE_MASK, SRC_BASE_ADDR + 0x1004);
75 pll_base = &ana_pll->dram_pll_gnrl_ctl;
77 case ANATOP_VIDEO_PLL:
78 pll_base = &ana_pll->video_pll1_gnrl_ctl;
83 /* Bypass clock and set lock to pll output lock */
84 tmp = readl(pll_base);
86 writel(tmp, pll_base);
90 writel(tmp, pll_base);
92 div_val = (rate->mdiv << MDIV_SHIFT) | (rate->pdiv << PDIV_SHIFT) |
93 (rate->sdiv << SDIV_SHIFT);
94 writel(div_val, pll_base + 4);
95 writel(rate->kdiv << KDIV_SHIFT, pll_base + 8);
101 writel(tmp, pll_base);
104 while (!(readl(pll_base) & LOCK_STATUS))
109 writel(tmp, pll_base);
114 void dram_pll_init(ulong pll_val)
116 fracpll_configure(ANATOP_DRAM_PLL, pll_val);
119 static struct dram_bypass_clk_setting imx8mm_dram_bypass_tbl[] = {
120 DRAM_BYPASS_ROOT_CONFIG(MHZ(100), 2, CLK_ROOT_PRE_DIV1, 2,
122 DRAM_BYPASS_ROOT_CONFIG(MHZ(250), 3, CLK_ROOT_PRE_DIV2, 2,
124 DRAM_BYPASS_ROOT_CONFIG(MHZ(400), 1, CLK_ROOT_PRE_DIV2, 3,
128 void dram_enable_bypass(ulong clk_val)
131 struct dram_bypass_clk_setting *config;
133 for (i = 0; i < ARRAY_SIZE(imx8mm_dram_bypass_tbl); i++) {
134 if (clk_val == imx8mm_dram_bypass_tbl[i].clk)
138 if (i == ARRAY_SIZE(imx8mm_dram_bypass_tbl)) {
139 printf("No matched freq table %lu\n", clk_val);
143 config = &imx8mm_dram_bypass_tbl[i];
145 clock_set_target_val(DRAM_ALT_CLK_ROOT, CLK_ROOT_ON |
146 CLK_ROOT_SOURCE_SEL(config->alt_root_sel) |
147 CLK_ROOT_PRE_DIV(config->alt_pre_div));
148 clock_set_target_val(DRAM_APB_CLK_ROOT, CLK_ROOT_ON |
149 CLK_ROOT_SOURCE_SEL(config->apb_root_sel) |
150 CLK_ROOT_PRE_DIV(config->apb_pre_div));
151 clock_set_target_val(DRAM_SEL_CFG, CLK_ROOT_ON |
152 CLK_ROOT_SOURCE_SEL(1));
155 void dram_disable_bypass(void)
157 clock_set_target_val(DRAM_SEL_CFG, CLK_ROOT_ON |
158 CLK_ROOT_SOURCE_SEL(0));
159 clock_set_target_val(DRAM_APB_CLK_ROOT, CLK_ROOT_ON |
160 CLK_ROOT_SOURCE_SEL(4) |
161 CLK_ROOT_PRE_DIV(CLK_ROOT_PRE_DIV5));
165 void init_uart_clk(u32 index)
168 * set uart clock root
173 clock_enable(CCGR_UART1, 0);
174 clock_set_target_val(UART1_CLK_ROOT, CLK_ROOT_ON |
175 CLK_ROOT_SOURCE_SEL(0));
176 clock_enable(CCGR_UART1, 1);
179 clock_enable(CCGR_UART2, 0);
180 clock_set_target_val(UART2_CLK_ROOT, CLK_ROOT_ON |
181 CLK_ROOT_SOURCE_SEL(0));
182 clock_enable(CCGR_UART2, 1);
185 clock_enable(CCGR_UART3, 0);
186 clock_set_target_val(UART3_CLK_ROOT, CLK_ROOT_ON |
187 CLK_ROOT_SOURCE_SEL(0));
188 clock_enable(CCGR_UART3, 1);
191 clock_enable(CCGR_UART4, 0);
192 clock_set_target_val(UART4_CLK_ROOT, CLK_ROOT_ON |
193 CLK_ROOT_SOURCE_SEL(0));
194 clock_enable(CCGR_UART4, 1);
197 printf("Invalid uart index\n");
202 void init_wdog_clk(void)
204 clock_enable(CCGR_WDOG1, 0);
205 clock_enable(CCGR_WDOG2, 0);
206 clock_enable(CCGR_WDOG3, 0);
207 clock_set_target_val(WDOG_CLK_ROOT, CLK_ROOT_ON |
208 CLK_ROOT_SOURCE_SEL(0));
209 clock_enable(CCGR_WDOG1, 1);
210 clock_enable(CCGR_WDOG2, 1);
211 clock_enable(CCGR_WDOG3, 1);
219 * The gate is not exported to clk tree, so configure them here.
220 * According to ANAMIX SPEC
221 * sys pll1 fixed at 800MHz
222 * sys pll2 fixed at 1GHz
223 * Here we only enable the outputs.
225 val_cfg0 = readl(&ana_pll->sys_pll1_gnrl_ctl);
226 val_cfg0 |= INTPLL_CLKE_MASK | INTPLL_DIV2_CLKE_MASK |
227 INTPLL_DIV3_CLKE_MASK | INTPLL_DIV4_CLKE_MASK |
228 INTPLL_DIV5_CLKE_MASK | INTPLL_DIV6_CLKE_MASK |
229 INTPLL_DIV8_CLKE_MASK | INTPLL_DIV10_CLKE_MASK |
230 INTPLL_DIV20_CLKE_MASK;
231 writel(val_cfg0, &ana_pll->sys_pll1_gnrl_ctl);
233 val_cfg0 = readl(&ana_pll->sys_pll2_gnrl_ctl);
234 val_cfg0 |= INTPLL_CLKE_MASK | INTPLL_DIV2_CLKE_MASK |
235 INTPLL_DIV3_CLKE_MASK | INTPLL_DIV4_CLKE_MASK |
236 INTPLL_DIV5_CLKE_MASK | INTPLL_DIV6_CLKE_MASK |
237 INTPLL_DIV8_CLKE_MASK | INTPLL_DIV10_CLKE_MASK |
238 INTPLL_DIV20_CLKE_MASK;
239 writel(val_cfg0, &ana_pll->sys_pll2_gnrl_ctl);
241 /* config GIC to sys_pll2_100m */
242 clock_enable(CCGR_GIC, 0);
243 clock_set_target_val(GIC_CLK_ROOT, CLK_ROOT_ON |
244 CLK_ROOT_SOURCE_SEL(3));
245 clock_enable(CCGR_GIC, 1);
247 clock_set_target_val(NAND_USDHC_BUS_CLK_ROOT, CLK_ROOT_ON |
248 CLK_ROOT_SOURCE_SEL(1));
250 clock_enable(CCGR_DDR1, 0);
251 clock_set_target_val(DRAM_ALT_CLK_ROOT, CLK_ROOT_ON |
252 CLK_ROOT_SOURCE_SEL(1));
253 clock_set_target_val(DRAM_APB_CLK_ROOT, CLK_ROOT_ON |
254 CLK_ROOT_SOURCE_SEL(1));
255 clock_enable(CCGR_DDR1, 1);
259 clock_enable(CCGR_TEMP_SENSOR, 1);
261 clock_enable(CCGR_SEC_DEBUG, 1);
266 u32 imx_get_uartclk(void)
271 static u32 decode_intpll(enum clk_root_src intpll)
273 u32 pll_gnrl_ctl, pll_div_ctl, pll_clke_mask;
274 u32 main_div, pre_div, post_div, div;
279 pll_gnrl_ctl = readl(&ana_pll->arm_pll_gnrl_ctl);
280 pll_div_ctl = readl(&ana_pll->arm_pll_div_ctl);
283 pll_gnrl_ctl = readl(&ana_pll->gpu_pll_gnrl_ctl);
284 pll_div_ctl = readl(&ana_pll->gpu_pll_div_ctl);
287 pll_gnrl_ctl = readl(&ana_pll->vpu_pll_gnrl_ctl);
288 pll_div_ctl = readl(&ana_pll->vpu_pll_div_ctl);
290 case SYSTEM_PLL1_800M_CLK:
291 case SYSTEM_PLL1_400M_CLK:
292 case SYSTEM_PLL1_266M_CLK:
293 case SYSTEM_PLL1_200M_CLK:
294 case SYSTEM_PLL1_160M_CLK:
295 case SYSTEM_PLL1_133M_CLK:
296 case SYSTEM_PLL1_100M_CLK:
297 case SYSTEM_PLL1_80M_CLK:
298 case SYSTEM_PLL1_40M_CLK:
299 pll_gnrl_ctl = readl(&ana_pll->sys_pll1_gnrl_ctl);
300 pll_div_ctl = readl(&ana_pll->sys_pll1_div_ctl);
302 case SYSTEM_PLL2_1000M_CLK:
303 case SYSTEM_PLL2_500M_CLK:
304 case SYSTEM_PLL2_333M_CLK:
305 case SYSTEM_PLL2_250M_CLK:
306 case SYSTEM_PLL2_200M_CLK:
307 case SYSTEM_PLL2_166M_CLK:
308 case SYSTEM_PLL2_125M_CLK:
309 case SYSTEM_PLL2_100M_CLK:
310 case SYSTEM_PLL2_50M_CLK:
311 pll_gnrl_ctl = readl(&ana_pll->sys_pll2_gnrl_ctl);
312 pll_div_ctl = readl(&ana_pll->sys_pll2_div_ctl);
314 case SYSTEM_PLL3_CLK:
315 pll_gnrl_ctl = readl(&ana_pll->sys_pll3_gnrl_ctl);
316 pll_div_ctl = readl(&ana_pll->sys_pll3_div_ctl);
322 /* Only support SYS_XTAL 24M, PAD_CLK not take into consideration */
323 if ((pll_gnrl_ctl & INTPLL_REF_CLK_SEL_MASK) != 0)
326 if ((pll_gnrl_ctl & INTPLL_RST_MASK) == 0)
330 * When BYPASS is equal to 1, PLL enters the bypass mode
331 * regardless of the values of RESETB
333 if (pll_gnrl_ctl & INTPLL_BYPASS_MASK)
336 if (!(pll_gnrl_ctl & INTPLL_LOCK_MASK)) {
337 puts("pll not locked\n");
345 case SYSTEM_PLL3_CLK:
346 case SYSTEM_PLL1_800M_CLK:
347 case SYSTEM_PLL2_1000M_CLK:
348 pll_clke_mask = INTPLL_CLKE_MASK;
352 case SYSTEM_PLL1_400M_CLK:
353 case SYSTEM_PLL2_500M_CLK:
354 pll_clke_mask = INTPLL_DIV2_CLKE_MASK;
358 case SYSTEM_PLL1_266M_CLK:
359 case SYSTEM_PLL2_333M_CLK:
360 pll_clke_mask = INTPLL_DIV3_CLKE_MASK;
364 case SYSTEM_PLL1_200M_CLK:
365 case SYSTEM_PLL2_250M_CLK:
366 pll_clke_mask = INTPLL_DIV4_CLKE_MASK;
370 case SYSTEM_PLL1_160M_CLK:
371 case SYSTEM_PLL2_200M_CLK:
372 pll_clke_mask = INTPLL_DIV5_CLKE_MASK;
376 case SYSTEM_PLL1_133M_CLK:
377 case SYSTEM_PLL2_166M_CLK:
378 pll_clke_mask = INTPLL_DIV6_CLKE_MASK;
382 case SYSTEM_PLL1_100M_CLK:
383 case SYSTEM_PLL2_125M_CLK:
384 pll_clke_mask = INTPLL_DIV8_CLKE_MASK;
388 case SYSTEM_PLL1_80M_CLK:
389 case SYSTEM_PLL2_100M_CLK:
390 pll_clke_mask = INTPLL_DIV10_CLKE_MASK;
394 case SYSTEM_PLL1_40M_CLK:
395 case SYSTEM_PLL2_50M_CLK:
396 pll_clke_mask = INTPLL_DIV20_CLKE_MASK;
403 if ((pll_gnrl_ctl & pll_clke_mask) == 0)
406 main_div = (pll_div_ctl & INTPLL_MAIN_DIV_MASK) >>
407 INTPLL_MAIN_DIV_SHIFT;
408 pre_div = (pll_div_ctl & INTPLL_PRE_DIV_MASK) >>
409 INTPLL_PRE_DIV_SHIFT;
410 post_div = (pll_div_ctl & INTPLL_POST_DIV_MASK) >>
411 INTPLL_POST_DIV_SHIFT;
413 /* FFVCO = (m * FFIN) / p, FFOUT = (m * FFIN) / (p * 2^s) */
414 freq = 24000000ULL * main_div;
415 return lldiv(freq, pre_div * (1 << post_div) * div);
418 static u32 decode_fracpll(enum clk_root_src frac_pll)
420 u32 pll_gnrl_ctl, pll_fdiv_ctl0, pll_fdiv_ctl1;
421 u32 main_div, pre_div, post_div, k;
425 pll_gnrl_ctl = readl(&ana_pll->dram_pll_gnrl_ctl);
426 pll_fdiv_ctl0 = readl(&ana_pll->dram_pll_fdiv_ctl0);
427 pll_fdiv_ctl1 = readl(&ana_pll->dram_pll_fdiv_ctl1);
430 pll_gnrl_ctl = readl(&ana_pll->audio_pll1_gnrl_ctl);
431 pll_fdiv_ctl0 = readl(&ana_pll->audio_pll1_fdiv_ctl0);
432 pll_fdiv_ctl1 = readl(&ana_pll->audio_pll1_fdiv_ctl1);
435 pll_gnrl_ctl = readl(&ana_pll->audio_pll2_gnrl_ctl);
436 pll_fdiv_ctl0 = readl(&ana_pll->audio_pll2_fdiv_ctl0);
437 pll_fdiv_ctl1 = readl(&ana_pll->audio_pll2_fdiv_ctl1);
440 pll_gnrl_ctl = readl(&ana_pll->video_pll1_gnrl_ctl);
441 pll_fdiv_ctl0 = readl(&ana_pll->video_pll1_fdiv_ctl0);
442 pll_fdiv_ctl1 = readl(&ana_pll->video_pll1_fdiv_ctl1);
445 printf("Not supported\n");
449 /* Only support SYS_XTAL 24M, PAD_CLK not take into consideration */
450 if ((pll_gnrl_ctl & INTPLL_REF_CLK_SEL_MASK) != 0)
453 if ((pll_gnrl_ctl & INTPLL_RST_MASK) == 0)
456 * When BYPASS is equal to 1, PLL enters the bypass mode
457 * regardless of the values of RESETB
459 if (pll_gnrl_ctl & INTPLL_BYPASS_MASK)
462 if (!(pll_gnrl_ctl & INTPLL_LOCK_MASK)) {
463 puts("pll not locked\n");
467 if (!(pll_gnrl_ctl & INTPLL_CLKE_MASK))
470 main_div = (pll_fdiv_ctl0 & INTPLL_MAIN_DIV_MASK) >>
471 INTPLL_MAIN_DIV_SHIFT;
472 pre_div = (pll_fdiv_ctl0 & INTPLL_PRE_DIV_MASK) >>
473 INTPLL_PRE_DIV_SHIFT;
474 post_div = (pll_fdiv_ctl0 & INTPLL_POST_DIV_MASK) >>
475 INTPLL_POST_DIV_SHIFT;
477 k = pll_fdiv_ctl1 & GENMASK(15, 0);
479 return lldiv((main_div * 65536 + k) * 24000000ULL,
480 65536 * pre_div * (1 << post_div));
483 static u32 get_root_src_clk(enum clk_root_src root_src)
495 case SYSTEM_PLL1_800M_CLK:
496 case SYSTEM_PLL1_400M_CLK:
497 case SYSTEM_PLL1_266M_CLK:
498 case SYSTEM_PLL1_200M_CLK:
499 case SYSTEM_PLL1_160M_CLK:
500 case SYSTEM_PLL1_133M_CLK:
501 case SYSTEM_PLL1_100M_CLK:
502 case SYSTEM_PLL1_80M_CLK:
503 case SYSTEM_PLL1_40M_CLK:
504 case SYSTEM_PLL2_1000M_CLK:
505 case SYSTEM_PLL2_500M_CLK:
506 case SYSTEM_PLL2_333M_CLK:
507 case SYSTEM_PLL2_250M_CLK:
508 case SYSTEM_PLL2_200M_CLK:
509 case SYSTEM_PLL2_166M_CLK:
510 case SYSTEM_PLL2_125M_CLK:
511 case SYSTEM_PLL2_100M_CLK:
512 case SYSTEM_PLL2_50M_CLK:
513 case SYSTEM_PLL3_CLK:
514 return decode_intpll(root_src);
519 return decode_fracpll(root_src);
527 static u32 get_root_clk(enum clk_root_index clock_id)
529 enum clk_root_src root_src;
530 u32 post_podf, pre_podf, root_src_clk;
532 if (clock_root_enabled(clock_id) <= 0)
535 if (clock_get_prediv(clock_id, &pre_podf) < 0)
538 if (clock_get_postdiv(clock_id, &post_podf) < 0)
541 if (clock_get_src(clock_id, &root_src) < 0)
544 root_src_clk = get_root_src_clk(root_src);
546 return root_src_clk / (post_podf + 1) / (pre_podf + 1);
549 u32 mxc_get_clock(enum mxc_clock clk)
555 return get_root_clk(ARM_A53_CLK_ROOT);
557 clock_get_target_val(IPG_CLK_ROOT, &val);
559 return get_root_clk(AHB_CLK_ROOT) / 2 / (val + 1);
561 return get_root_clk(ECSPI1_CLK_ROOT);
563 return get_root_clk(USDHC1_CLK_ROOT);
565 return get_root_clk(USDHC2_CLK_ROOT);
567 return get_root_clk(USDHC3_CLK_ROOT);
569 return get_root_clk(I2C1_CLK_ROOT);
571 return get_root_clk(UART1_CLK_ROOT);
573 return get_root_clk(QSPI_CLK_ROOT);
575 printf("Unsupported mxc_clock %d\n", clk);