blob: 6ab75f0e2c4d7a33fb49976498931ef41295f1c6 [file] [log] [blame]
Peng Fan99878462019-08-27 06:25:51 +00001// SPDX-License-Identifier: GPL-2.0+
2/*
3 * Copyright 2018-2019 NXP
4 *
5 * Peng Fan <peng.fan@nxp.com>
6 */
7
8#include <common.h>
9#include <asm/arch/clock.h>
10#include <asm/arch/imx-regs.h>
11#include <asm/arch/sys_proto.h>
12#include <asm/io.h>
Peng Fan99878462019-08-27 06:25:51 +000013#include <div64.h>
14#include <errno.h>
Simon Glass4dcacfc2020-05-10 11:40:13 -060015#include <linux/bitops.h>
Simon Glassdbd79542020-05-10 11:40:11 -060016#include <linux/delay.h>
Peng Fan99878462019-08-27 06:25:51 +000017
18DECLARE_GLOBAL_DATA_PTR;
19
20static struct anamix_pll *ana_pll = (struct anamix_pll *)ANATOP_BASE_ADDR;
21
Peng Fan4e36e332020-07-09 11:06:24 +080022static u32 get_root_clk(enum clk_root_index clock_id);
Peng Fan99878462019-08-27 06:25:51 +000023void enable_ocotp_clk(unsigned char enable)
24{
Peng Fan60c29bb2019-12-30 16:52:30 +080025 clock_enable(CCGR_OCOTP, !!enable);
Peng Fan99878462019-08-27 06:25:51 +000026}
27
28int enable_i2c_clk(unsigned char enable, unsigned i2c_num)
29{
Peng Fan60c29bb2019-12-30 16:52:30 +080030 /* 0 - 3 is valid i2c num */
31 if (i2c_num > 3)
32 return -EINVAL;
Peng Fan99878462019-08-27 06:25:51 +000033
Peng Fan60c29bb2019-12-30 16:52:30 +080034 clock_enable(CCGR_I2C1 + i2c_num, !!enable);
Peng Fan99878462019-08-27 06:25:51 +000035
Peng Fan60c29bb2019-12-30 16:52:30 +080036 return 0;
Peng Fan99878462019-08-27 06:25:51 +000037}
38
39#ifdef CONFIG_SPL_BUILD
40static struct imx_int_pll_rate_table imx8mm_fracpll_tbl[] = {
Peng Fand29bf222019-12-27 11:40:55 +080041 PLL_1443X_RATE(1000000000U, 250, 3, 1, 0),
Peng Fan99878462019-08-27 06:25:51 +000042 PLL_1443X_RATE(800000000U, 300, 9, 0, 0),
43 PLL_1443X_RATE(750000000U, 250, 8, 0, 0),
44 PLL_1443X_RATE(650000000U, 325, 3, 2, 0),
45 PLL_1443X_RATE(600000000U, 300, 3, 2, 0),
46 PLL_1443X_RATE(594000000U, 99, 1, 2, 0),
47 PLL_1443X_RATE(400000000U, 300, 9, 1, 0),
48 PLL_1443X_RATE(266666667U, 400, 9, 2, 0),
49 PLL_1443X_RATE(167000000U, 334, 3, 4, 0),
50 PLL_1443X_RATE(100000000U, 300, 9, 3, 0),
51};
52
Alifer Moraes39931102020-01-14 15:54:59 -030053static int fracpll_configure(enum pll_clocks pll, u32 freq)
Peng Fan99878462019-08-27 06:25:51 +000054{
55 int i;
56 u32 tmp, div_val;
57 void *pll_base;
58 struct imx_int_pll_rate_table *rate;
59
60 for (i = 0; i < ARRAY_SIZE(imx8mm_fracpll_tbl); i++) {
61 if (freq == imx8mm_fracpll_tbl[i].rate)
62 break;
63 }
64
65 if (i == ARRAY_SIZE(imx8mm_fracpll_tbl)) {
66 printf("No matched freq table %u\n", freq);
67 return -EINVAL;
68 }
69
70 rate = &imx8mm_fracpll_tbl[i];
71
72 switch (pll) {
73 case ANATOP_DRAM_PLL:
74 setbits_le32(GPC_BASE_ADDR + 0xEC, 1 << 7);
75 setbits_le32(GPC_BASE_ADDR + 0xF8, 1 << 5);
76 writel(SRC_DDR1_ENABLE_MASK, SRC_BASE_ADDR + 0x1004);
77
78 pll_base = &ana_pll->dram_pll_gnrl_ctl;
79 break;
80 case ANATOP_VIDEO_PLL:
81 pll_base = &ana_pll->video_pll1_gnrl_ctl;
82 break;
83 default:
84 return 0;
85 }
86 /* Bypass clock and set lock to pll output lock */
87 tmp = readl(pll_base);
88 tmp |= BYPASS_MASK;
89 writel(tmp, pll_base);
90
91 /* Enable RST */
92 tmp &= ~RST_MASK;
93 writel(tmp, pll_base);
94
95 div_val = (rate->mdiv << MDIV_SHIFT) | (rate->pdiv << PDIV_SHIFT) |
96 (rate->sdiv << SDIV_SHIFT);
97 writel(div_val, pll_base + 4);
98 writel(rate->kdiv << KDIV_SHIFT, pll_base + 8);
99
100 __udelay(100);
101
102 /* Disable RST */
103 tmp |= RST_MASK;
104 writel(tmp, pll_base);
105
106 /* Wait Lock*/
107 while (!(readl(pll_base) & LOCK_STATUS))
108 ;
109
110 /* Bypass */
111 tmp &= ~BYPASS_MASK;
112 writel(tmp, pll_base);
113
114 return 0;
115}
116
117void dram_pll_init(ulong pll_val)
118{
119 fracpll_configure(ANATOP_DRAM_PLL, pll_val);
120}
121
122static struct dram_bypass_clk_setting imx8mm_dram_bypass_tbl[] = {
123 DRAM_BYPASS_ROOT_CONFIG(MHZ(100), 2, CLK_ROOT_PRE_DIV1, 2,
124 CLK_ROOT_PRE_DIV2),
125 DRAM_BYPASS_ROOT_CONFIG(MHZ(250), 3, CLK_ROOT_PRE_DIV2, 2,
126 CLK_ROOT_PRE_DIV2),
127 DRAM_BYPASS_ROOT_CONFIG(MHZ(400), 1, CLK_ROOT_PRE_DIV2, 3,
128 CLK_ROOT_PRE_DIV2),
129};
130
131void dram_enable_bypass(ulong clk_val)
132{
133 int i;
134 struct dram_bypass_clk_setting *config;
135
136 for (i = 0; i < ARRAY_SIZE(imx8mm_dram_bypass_tbl); i++) {
137 if (clk_val == imx8mm_dram_bypass_tbl[i].clk)
138 break;
139 }
140
141 if (i == ARRAY_SIZE(imx8mm_dram_bypass_tbl)) {
142 printf("No matched freq table %lu\n", clk_val);
143 return;
144 }
145
146 config = &imx8mm_dram_bypass_tbl[i];
147
148 clock_set_target_val(DRAM_ALT_CLK_ROOT, CLK_ROOT_ON |
149 CLK_ROOT_SOURCE_SEL(config->alt_root_sel) |
150 CLK_ROOT_PRE_DIV(config->alt_pre_div));
151 clock_set_target_val(DRAM_APB_CLK_ROOT, CLK_ROOT_ON |
152 CLK_ROOT_SOURCE_SEL(config->apb_root_sel) |
153 CLK_ROOT_PRE_DIV(config->apb_pre_div));
154 clock_set_target_val(DRAM_SEL_CFG, CLK_ROOT_ON |
155 CLK_ROOT_SOURCE_SEL(1));
156}
157
158void dram_disable_bypass(void)
159{
160 clock_set_target_val(DRAM_SEL_CFG, CLK_ROOT_ON |
161 CLK_ROOT_SOURCE_SEL(0));
162 clock_set_target_val(DRAM_APB_CLK_ROOT, CLK_ROOT_ON |
163 CLK_ROOT_SOURCE_SEL(4) |
164 CLK_ROOT_PRE_DIV(CLK_ROOT_PRE_DIV5));
165}
166#endif
167
Peng Fan4e36e332020-07-09 11:06:24 +0800168int intpll_configure(enum pll_clocks pll, ulong freq)
169{
170 void __iomem *pll_gnrl_ctl, __iomem *pll_div_ctl;
171 u32 pll_div_ctl_val, pll_clke_masks;
172
173 switch (pll) {
174 case ANATOP_SYSTEM_PLL1:
175 pll_gnrl_ctl = &ana_pll->sys_pll1_gnrl_ctl;
176 pll_div_ctl = &ana_pll->sys_pll1_div_ctl;
177 pll_clke_masks = INTPLL_DIV20_CLKE_MASK |
178 INTPLL_DIV10_CLKE_MASK | INTPLL_DIV8_CLKE_MASK |
179 INTPLL_DIV6_CLKE_MASK | INTPLL_DIV5_CLKE_MASK |
180 INTPLL_DIV4_CLKE_MASK | INTPLL_DIV3_CLKE_MASK |
181 INTPLL_DIV2_CLKE_MASK | INTPLL_CLKE_MASK;
182 break;
183 case ANATOP_SYSTEM_PLL2:
184 pll_gnrl_ctl = &ana_pll->sys_pll2_gnrl_ctl;
185 pll_div_ctl = &ana_pll->sys_pll2_div_ctl;
186 pll_clke_masks = INTPLL_DIV20_CLKE_MASK |
187 INTPLL_DIV10_CLKE_MASK | INTPLL_DIV8_CLKE_MASK |
188 INTPLL_DIV6_CLKE_MASK | INTPLL_DIV5_CLKE_MASK |
189 INTPLL_DIV4_CLKE_MASK | INTPLL_DIV3_CLKE_MASK |
190 INTPLL_DIV2_CLKE_MASK | INTPLL_CLKE_MASK;
191 break;
192 case ANATOP_SYSTEM_PLL3:
193 pll_gnrl_ctl = &ana_pll->sys_pll3_gnrl_ctl;
194 pll_div_ctl = &ana_pll->sys_pll3_div_ctl;
195 pll_clke_masks = INTPLL_CLKE_MASK;
196 break;
197 case ANATOP_ARM_PLL:
198 pll_gnrl_ctl = &ana_pll->arm_pll_gnrl_ctl;
199 pll_div_ctl = &ana_pll->arm_pll_div_ctl;
200 pll_clke_masks = INTPLL_CLKE_MASK;
201 break;
202 case ANATOP_GPU_PLL:
203 pll_gnrl_ctl = &ana_pll->gpu_pll_gnrl_ctl;
204 pll_div_ctl = &ana_pll->gpu_pll_div_ctl;
205 pll_clke_masks = INTPLL_CLKE_MASK;
206 break;
207 case ANATOP_VPU_PLL:
208 pll_gnrl_ctl = &ana_pll->vpu_pll_gnrl_ctl;
209 pll_div_ctl = &ana_pll->vpu_pll_div_ctl;
210 pll_clke_masks = INTPLL_CLKE_MASK;
211 break;
212 default:
213 return -EINVAL;
214 };
215
216 switch (freq) {
217 case MHZ(600):
218 /* 24 * 0x12c / 3 / 2 ^ 2 */
219 pll_div_ctl_val = INTPLL_MAIN_DIV_VAL(0x12c) |
220 INTPLL_PRE_DIV_VAL(3) | INTPLL_POST_DIV_VAL(2);
221 break;
222 case MHZ(750):
223 /* 24 * 0xfa / 2 / 2 ^ 2 */
224 pll_div_ctl_val = INTPLL_MAIN_DIV_VAL(0xfa) |
225 INTPLL_PRE_DIV_VAL(2) | INTPLL_POST_DIV_VAL(2);
226 break;
227 case MHZ(800):
228 /* 24 * 0x190 / 3 / 2 ^ 2 */
229 pll_div_ctl_val = INTPLL_MAIN_DIV_VAL(0x190) |
230 INTPLL_PRE_DIV_VAL(3) | INTPLL_POST_DIV_VAL(2);
231 break;
232 case MHZ(1000):
233 /* 24 * 0xfa / 3 / 2 ^ 1 */
234 pll_div_ctl_val = INTPLL_MAIN_DIV_VAL(0xfa) |
235 INTPLL_PRE_DIV_VAL(3) | INTPLL_POST_DIV_VAL(1);
236 break;
237 case MHZ(1200):
238 /* 24 * 0xc8 / 2 / 2 ^ 1 */
239 pll_div_ctl_val = INTPLL_MAIN_DIV_VAL(0xc8) |
240 INTPLL_PRE_DIV_VAL(2) | INTPLL_POST_DIV_VAL(1);
241 break;
242 case MHZ(2000):
243 /* 24 * 0xfa / 3 / 2 ^ 0 */
244 pll_div_ctl_val = INTPLL_MAIN_DIV_VAL(0xfa) |
245 INTPLL_PRE_DIV_VAL(3) | INTPLL_POST_DIV_VAL(0);
246 break;
247 default:
248 return -EINVAL;
249 };
250 /* Bypass clock and set lock to pll output lock */
251 setbits_le32(pll_gnrl_ctl, INTPLL_BYPASS_MASK | INTPLL_LOCK_SEL_MASK);
252 /* Enable reset */
253 clrbits_le32(pll_gnrl_ctl, INTPLL_RST_MASK);
254 /* Configure */
255 writel(pll_div_ctl_val, pll_div_ctl);
256
257 __udelay(100);
258
259 /* Disable reset */
260 setbits_le32(pll_gnrl_ctl, INTPLL_RST_MASK);
261 /* Wait Lock */
262 while (!(readl(pll_gnrl_ctl) & INTPLL_LOCK_MASK))
263 ;
264 /* Clear bypass */
265 clrbits_le32(pll_gnrl_ctl, INTPLL_BYPASS_MASK);
266 setbits_le32(pll_gnrl_ctl, pll_clke_masks);
267
268 return 0;
269}
270
Peng Fan99878462019-08-27 06:25:51 +0000271void init_uart_clk(u32 index)
272{
273 /*
274 * set uart clock root
275 * 24M OSC
276 */
277 switch (index) {
278 case 0:
279 clock_enable(CCGR_UART1, 0);
280 clock_set_target_val(UART1_CLK_ROOT, CLK_ROOT_ON |
281 CLK_ROOT_SOURCE_SEL(0));
282 clock_enable(CCGR_UART1, 1);
283 return;
284 case 1:
285 clock_enable(CCGR_UART2, 0);
286 clock_set_target_val(UART2_CLK_ROOT, CLK_ROOT_ON |
287 CLK_ROOT_SOURCE_SEL(0));
288 clock_enable(CCGR_UART2, 1);
289 return;
290 case 2:
291 clock_enable(CCGR_UART3, 0);
292 clock_set_target_val(UART3_CLK_ROOT, CLK_ROOT_ON |
293 CLK_ROOT_SOURCE_SEL(0));
294 clock_enable(CCGR_UART3, 1);
295 return;
296 case 3:
297 clock_enable(CCGR_UART4, 0);
298 clock_set_target_val(UART4_CLK_ROOT, CLK_ROOT_ON |
299 CLK_ROOT_SOURCE_SEL(0));
300 clock_enable(CCGR_UART4, 1);
301 return;
302 default:
303 printf("Invalid uart index\n");
304 return;
305 }
306}
307
308void init_wdog_clk(void)
309{
310 clock_enable(CCGR_WDOG1, 0);
311 clock_enable(CCGR_WDOG2, 0);
312 clock_enable(CCGR_WDOG3, 0);
313 clock_set_target_val(WDOG_CLK_ROOT, CLK_ROOT_ON |
314 CLK_ROOT_SOURCE_SEL(0));
315 clock_enable(CCGR_WDOG1, 1);
316 clock_enable(CCGR_WDOG2, 1);
317 clock_enable(CCGR_WDOG3, 1);
318}
319
320int clock_init(void)
321{
322 u32 val_cfg0;
323
324 /*
325 * The gate is not exported to clk tree, so configure them here.
326 * According to ANAMIX SPEC
327 * sys pll1 fixed at 800MHz
328 * sys pll2 fixed at 1GHz
329 * Here we only enable the outputs.
330 */
331 val_cfg0 = readl(&ana_pll->sys_pll1_gnrl_ctl);
332 val_cfg0 |= INTPLL_CLKE_MASK | INTPLL_DIV2_CLKE_MASK |
333 INTPLL_DIV3_CLKE_MASK | INTPLL_DIV4_CLKE_MASK |
334 INTPLL_DIV5_CLKE_MASK | INTPLL_DIV6_CLKE_MASK |
335 INTPLL_DIV8_CLKE_MASK | INTPLL_DIV10_CLKE_MASK |
336 INTPLL_DIV20_CLKE_MASK;
337 writel(val_cfg0, &ana_pll->sys_pll1_gnrl_ctl);
338
339 val_cfg0 = readl(&ana_pll->sys_pll2_gnrl_ctl);
340 val_cfg0 |= INTPLL_CLKE_MASK | INTPLL_DIV2_CLKE_MASK |
341 INTPLL_DIV3_CLKE_MASK | INTPLL_DIV4_CLKE_MASK |
342 INTPLL_DIV5_CLKE_MASK | INTPLL_DIV6_CLKE_MASK |
343 INTPLL_DIV8_CLKE_MASK | INTPLL_DIV10_CLKE_MASK |
344 INTPLL_DIV20_CLKE_MASK;
345 writel(val_cfg0, &ana_pll->sys_pll2_gnrl_ctl);
346
Peng Fan4e36e332020-07-09 11:06:24 +0800347 /* Configure ARM at 1.2GHz */
348 clock_set_target_val(ARM_A53_CLK_ROOT, CLK_ROOT_ON |
349 CLK_ROOT_SOURCE_SEL(2));
350
351 intpll_configure(ANATOP_ARM_PLL, MHZ(1200));
352
353 /* Bypass CCM A53 ROOT, Switch to ARM PLL -> MUX-> CPU */
354 clock_set_target_val(CORE_SEL_CFG, CLK_ROOT_SOURCE_SEL(1));
355
Peng Fan99878462019-08-27 06:25:51 +0000356 /* config GIC to sys_pll2_100m */
357 clock_enable(CCGR_GIC, 0);
358 clock_set_target_val(GIC_CLK_ROOT, CLK_ROOT_ON |
359 CLK_ROOT_SOURCE_SEL(3));
360 clock_enable(CCGR_GIC, 1);
361
362 clock_set_target_val(NAND_USDHC_BUS_CLK_ROOT, CLK_ROOT_ON |
363 CLK_ROOT_SOURCE_SEL(1));
364
365 clock_enable(CCGR_DDR1, 0);
366 clock_set_target_val(DRAM_ALT_CLK_ROOT, CLK_ROOT_ON |
367 CLK_ROOT_SOURCE_SEL(1));
368 clock_set_target_val(DRAM_APB_CLK_ROOT, CLK_ROOT_ON |
369 CLK_ROOT_SOURCE_SEL(1));
370 clock_enable(CCGR_DDR1, 1);
371
372 init_wdog_clk();
373
374 clock_enable(CCGR_TEMP_SENSOR, 1);
375
376 clock_enable(CCGR_SEC_DEBUG, 1);
377
378 return 0;
379};
380
381u32 imx_get_uartclk(void)
382{
383 return 24000000U;
384}
385
Alifer Moraes39931102020-01-14 15:54:59 -0300386static u32 decode_intpll(enum clk_root_src intpll)
Peng Fan60c29bb2019-12-30 16:52:30 +0800387{
388 u32 pll_gnrl_ctl, pll_div_ctl, pll_clke_mask;
389 u32 main_div, pre_div, post_div, div;
390 u64 freq;
391
392 switch (intpll) {
393 case ARM_PLL_CLK:
394 pll_gnrl_ctl = readl(&ana_pll->arm_pll_gnrl_ctl);
395 pll_div_ctl = readl(&ana_pll->arm_pll_div_ctl);
396 break;
397 case GPU_PLL_CLK:
398 pll_gnrl_ctl = readl(&ana_pll->gpu_pll_gnrl_ctl);
399 pll_div_ctl = readl(&ana_pll->gpu_pll_div_ctl);
400 break;
401 case VPU_PLL_CLK:
402 pll_gnrl_ctl = readl(&ana_pll->vpu_pll_gnrl_ctl);
403 pll_div_ctl = readl(&ana_pll->vpu_pll_div_ctl);
404 break;
405 case SYSTEM_PLL1_800M_CLK:
406 case SYSTEM_PLL1_400M_CLK:
407 case SYSTEM_PLL1_266M_CLK:
408 case SYSTEM_PLL1_200M_CLK:
409 case SYSTEM_PLL1_160M_CLK:
410 case SYSTEM_PLL1_133M_CLK:
411 case SYSTEM_PLL1_100M_CLK:
412 case SYSTEM_PLL1_80M_CLK:
413 case SYSTEM_PLL1_40M_CLK:
414 pll_gnrl_ctl = readl(&ana_pll->sys_pll1_gnrl_ctl);
415 pll_div_ctl = readl(&ana_pll->sys_pll1_div_ctl);
416 break;
417 case SYSTEM_PLL2_1000M_CLK:
418 case SYSTEM_PLL2_500M_CLK:
419 case SYSTEM_PLL2_333M_CLK:
420 case SYSTEM_PLL2_250M_CLK:
421 case SYSTEM_PLL2_200M_CLK:
422 case SYSTEM_PLL2_166M_CLK:
423 case SYSTEM_PLL2_125M_CLK:
424 case SYSTEM_PLL2_100M_CLK:
425 case SYSTEM_PLL2_50M_CLK:
426 pll_gnrl_ctl = readl(&ana_pll->sys_pll2_gnrl_ctl);
427 pll_div_ctl = readl(&ana_pll->sys_pll2_div_ctl);
428 break;
429 case SYSTEM_PLL3_CLK:
430 pll_gnrl_ctl = readl(&ana_pll->sys_pll3_gnrl_ctl);
431 pll_div_ctl = readl(&ana_pll->sys_pll3_div_ctl);
432 break;
433 default:
434 return -EINVAL;
435 }
436
437 /* Only support SYS_XTAL 24M, PAD_CLK not take into consideration */
438 if ((pll_gnrl_ctl & INTPLL_REF_CLK_SEL_MASK) != 0)
439 return 0;
440
441 if ((pll_gnrl_ctl & INTPLL_RST_MASK) == 0)
442 return 0;
443
444 /*
445 * When BYPASS is equal to 1, PLL enters the bypass mode
446 * regardless of the values of RESETB
447 */
448 if (pll_gnrl_ctl & INTPLL_BYPASS_MASK)
449 return 24000000u;
450
451 if (!(pll_gnrl_ctl & INTPLL_LOCK_MASK)) {
452 puts("pll not locked\n");
453 return 0;
454 }
455
456 switch (intpll) {
457 case ARM_PLL_CLK:
458 case GPU_PLL_CLK:
459 case VPU_PLL_CLK:
460 case SYSTEM_PLL3_CLK:
461 case SYSTEM_PLL1_800M_CLK:
462 case SYSTEM_PLL2_1000M_CLK:
463 pll_clke_mask = INTPLL_CLKE_MASK;
464 div = 1;
465 break;
466
467 case SYSTEM_PLL1_400M_CLK:
468 case SYSTEM_PLL2_500M_CLK:
469 pll_clke_mask = INTPLL_DIV2_CLKE_MASK;
470 div = 2;
471 break;
472
473 case SYSTEM_PLL1_266M_CLK:
474 case SYSTEM_PLL2_333M_CLK:
475 pll_clke_mask = INTPLL_DIV3_CLKE_MASK;
476 div = 3;
477 break;
478
479 case SYSTEM_PLL1_200M_CLK:
480 case SYSTEM_PLL2_250M_CLK:
481 pll_clke_mask = INTPLL_DIV4_CLKE_MASK;
482 div = 4;
483 break;
484
485 case SYSTEM_PLL1_160M_CLK:
486 case SYSTEM_PLL2_200M_CLK:
487 pll_clke_mask = INTPLL_DIV5_CLKE_MASK;
488 div = 5;
489 break;
490
491 case SYSTEM_PLL1_133M_CLK:
492 case SYSTEM_PLL2_166M_CLK:
493 pll_clke_mask = INTPLL_DIV6_CLKE_MASK;
494 div = 6;
495 break;
496
497 case SYSTEM_PLL1_100M_CLK:
498 case SYSTEM_PLL2_125M_CLK:
499 pll_clke_mask = INTPLL_DIV8_CLKE_MASK;
500 div = 8;
501 break;
502
503 case SYSTEM_PLL1_80M_CLK:
504 case SYSTEM_PLL2_100M_CLK:
505 pll_clke_mask = INTPLL_DIV10_CLKE_MASK;
506 div = 10;
507 break;
508
509 case SYSTEM_PLL1_40M_CLK:
510 case SYSTEM_PLL2_50M_CLK:
511 pll_clke_mask = INTPLL_DIV20_CLKE_MASK;
512 div = 20;
513 break;
514 default:
515 return -EINVAL;
516 }
517
518 if ((pll_gnrl_ctl & pll_clke_mask) == 0)
519 return 0;
520
521 main_div = (pll_div_ctl & INTPLL_MAIN_DIV_MASK) >>
522 INTPLL_MAIN_DIV_SHIFT;
523 pre_div = (pll_div_ctl & INTPLL_PRE_DIV_MASK) >>
524 INTPLL_PRE_DIV_SHIFT;
525 post_div = (pll_div_ctl & INTPLL_POST_DIV_MASK) >>
526 INTPLL_POST_DIV_SHIFT;
527
528 /* FFVCO = (m * FFIN) / p, FFOUT = (m * FFIN) / (p * 2^s) */
529 freq = 24000000ULL * main_div;
530 return lldiv(freq, pre_div * (1 << post_div) * div);
531}
532
Alifer Moraes39931102020-01-14 15:54:59 -0300533static u32 decode_fracpll(enum clk_root_src frac_pll)
Peng Fan60c29bb2019-12-30 16:52:30 +0800534{
535 u32 pll_gnrl_ctl, pll_fdiv_ctl0, pll_fdiv_ctl1;
536 u32 main_div, pre_div, post_div, k;
537
538 switch (frac_pll) {
539 case DRAM_PLL1_CLK:
540 pll_gnrl_ctl = readl(&ana_pll->dram_pll_gnrl_ctl);
541 pll_fdiv_ctl0 = readl(&ana_pll->dram_pll_fdiv_ctl0);
542 pll_fdiv_ctl1 = readl(&ana_pll->dram_pll_fdiv_ctl1);
543 break;
544 case AUDIO_PLL1_CLK:
545 pll_gnrl_ctl = readl(&ana_pll->audio_pll1_gnrl_ctl);
546 pll_fdiv_ctl0 = readl(&ana_pll->audio_pll1_fdiv_ctl0);
547 pll_fdiv_ctl1 = readl(&ana_pll->audio_pll1_fdiv_ctl1);
548 break;
549 case AUDIO_PLL2_CLK:
550 pll_gnrl_ctl = readl(&ana_pll->audio_pll2_gnrl_ctl);
551 pll_fdiv_ctl0 = readl(&ana_pll->audio_pll2_fdiv_ctl0);
552 pll_fdiv_ctl1 = readl(&ana_pll->audio_pll2_fdiv_ctl1);
553 break;
554 case VIDEO_PLL_CLK:
555 pll_gnrl_ctl = readl(&ana_pll->video_pll1_gnrl_ctl);
556 pll_fdiv_ctl0 = readl(&ana_pll->video_pll1_fdiv_ctl0);
557 pll_fdiv_ctl1 = readl(&ana_pll->video_pll1_fdiv_ctl1);
558 break;
559 default:
560 printf("Not supported\n");
561 return 0;
562 }
563
564 /* Only support SYS_XTAL 24M, PAD_CLK not take into consideration */
Ye Licc643ea2020-03-23 19:54:29 -0700565 if ((pll_gnrl_ctl & GENMASK(1, 0)) != 0)
Peng Fan60c29bb2019-12-30 16:52:30 +0800566 return 0;
567
Ye Licc643ea2020-03-23 19:54:29 -0700568 if ((pll_gnrl_ctl & RST_MASK) == 0)
Peng Fan60c29bb2019-12-30 16:52:30 +0800569 return 0;
570 /*
571 * When BYPASS is equal to 1, PLL enters the bypass mode
572 * regardless of the values of RESETB
573 */
Ye Licc643ea2020-03-23 19:54:29 -0700574 if (pll_gnrl_ctl & BYPASS_MASK)
Peng Fan60c29bb2019-12-30 16:52:30 +0800575 return 24000000u;
576
Ye Licc643ea2020-03-23 19:54:29 -0700577 if (!(pll_gnrl_ctl & LOCK_STATUS)) {
Peng Fan60c29bb2019-12-30 16:52:30 +0800578 puts("pll not locked\n");
579 return 0;
580 }
581
Ye Licc643ea2020-03-23 19:54:29 -0700582 if (!(pll_gnrl_ctl & CLKE_MASK))
Peng Fan60c29bb2019-12-30 16:52:30 +0800583 return 0;
584
Ye Licc643ea2020-03-23 19:54:29 -0700585 main_div = (pll_fdiv_ctl0 & MDIV_MASK) >>
586 MDIV_SHIFT;
587 pre_div = (pll_fdiv_ctl0 & PDIV_MASK) >>
588 PDIV_SHIFT;
589 post_div = (pll_fdiv_ctl0 & SDIV_MASK) >>
590 SDIV_SHIFT;
Peng Fan60c29bb2019-12-30 16:52:30 +0800591
Ye Licc643ea2020-03-23 19:54:29 -0700592 k = pll_fdiv_ctl1 & KDIV_MASK;
Peng Fan60c29bb2019-12-30 16:52:30 +0800593
594 return lldiv((main_div * 65536 + k) * 24000000ULL,
595 65536 * pre_div * (1 << post_div));
596}
597
Alifer Moraes39931102020-01-14 15:54:59 -0300598static u32 get_root_src_clk(enum clk_root_src root_src)
Peng Fan60c29bb2019-12-30 16:52:30 +0800599{
600 switch (root_src) {
601 case OSC_24M_CLK:
602 return 24000000u;
603 case OSC_HDMI_CLK:
604 return 26000000u;
605 case OSC_32K_CLK:
606 return 32000u;
607 case ARM_PLL_CLK:
608 case GPU_PLL_CLK:
609 case VPU_PLL_CLK:
610 case SYSTEM_PLL1_800M_CLK:
611 case SYSTEM_PLL1_400M_CLK:
612 case SYSTEM_PLL1_266M_CLK:
613 case SYSTEM_PLL1_200M_CLK:
614 case SYSTEM_PLL1_160M_CLK:
615 case SYSTEM_PLL1_133M_CLK:
616 case SYSTEM_PLL1_100M_CLK:
617 case SYSTEM_PLL1_80M_CLK:
618 case SYSTEM_PLL1_40M_CLK:
619 case SYSTEM_PLL2_1000M_CLK:
620 case SYSTEM_PLL2_500M_CLK:
621 case SYSTEM_PLL2_333M_CLK:
622 case SYSTEM_PLL2_250M_CLK:
623 case SYSTEM_PLL2_200M_CLK:
624 case SYSTEM_PLL2_166M_CLK:
625 case SYSTEM_PLL2_125M_CLK:
626 case SYSTEM_PLL2_100M_CLK:
627 case SYSTEM_PLL2_50M_CLK:
628 case SYSTEM_PLL3_CLK:
629 return decode_intpll(root_src);
630 case DRAM_PLL1_CLK:
631 case AUDIO_PLL1_CLK:
632 case AUDIO_PLL2_CLK:
633 case VIDEO_PLL_CLK:
634 return decode_fracpll(root_src);
Peng Fan4e36e332020-07-09 11:06:24 +0800635 case ARM_A53_ALT_CLK:
636 return get_root_clk(ARM_A53_CLK_ROOT);
Peng Fan60c29bb2019-12-30 16:52:30 +0800637 default:
638 return 0;
639 }
640
641 return 0;
642}
643
Alifer Moraes39931102020-01-14 15:54:59 -0300644static u32 get_root_clk(enum clk_root_index clock_id)
Peng Fan60c29bb2019-12-30 16:52:30 +0800645{
646 enum clk_root_src root_src;
647 u32 post_podf, pre_podf, root_src_clk;
648
649 if (clock_root_enabled(clock_id) <= 0)
650 return 0;
651
652 if (clock_get_prediv(clock_id, &pre_podf) < 0)
653 return 0;
654
655 if (clock_get_postdiv(clock_id, &post_podf) < 0)
656 return 0;
657
658 if (clock_get_src(clock_id, &root_src) < 0)
659 return 0;
660
661 root_src_clk = get_root_src_clk(root_src);
662
663 return root_src_clk / (post_podf + 1) / (pre_podf + 1);
664}
665
Peng Fan4e36e332020-07-09 11:06:24 +0800666u32 get_arm_core_clk(void)
667{
668 enum clk_root_src root_src;
669 u32 root_src_clk;
670
671 if (clock_get_src(CORE_SEL_CFG, &root_src) < 0)
672 return 0;
673
674 root_src_clk = get_root_src_clk(root_src);
675
676 return root_src_clk;
677}
678
Peng Fan99878462019-08-27 06:25:51 +0000679u32 mxc_get_clock(enum mxc_clock clk)
680{
Peng Fan60c29bb2019-12-30 16:52:30 +0800681 u32 val;
Peng Fan99878462019-08-27 06:25:51 +0000682
683 switch (clk) {
Peng Fan99878462019-08-27 06:25:51 +0000684 case MXC_ARM_CLK:
Peng Fan4e36e332020-07-09 11:06:24 +0800685 return get_arm_core_clk();
Peng Fan60c29bb2019-12-30 16:52:30 +0800686 case MXC_IPG_CLK:
687 clock_get_target_val(IPG_CLK_ROOT, &val);
688 val = val & 0x3;
689 return get_root_clk(AHB_CLK_ROOT) / 2 / (val + 1);
690 case MXC_CSPI_CLK:
691 return get_root_clk(ECSPI1_CLK_ROOT);
692 case MXC_ESDHC_CLK:
693 return get_root_clk(USDHC1_CLK_ROOT);
694 case MXC_ESDHC2_CLK:
695 return get_root_clk(USDHC2_CLK_ROOT);
696 case MXC_ESDHC3_CLK:
697 return get_root_clk(USDHC3_CLK_ROOT);
698 case MXC_I2C_CLK:
699 return get_root_clk(I2C1_CLK_ROOT);
700 case MXC_UART_CLK:
701 return get_root_clk(UART1_CLK_ROOT);
702 case MXC_QSPI_CLK:
703 return get_root_clk(QSPI_CLK_ROOT);
Peng Fan99878462019-08-27 06:25:51 +0000704 default:
Peng Fan60c29bb2019-12-30 16:52:30 +0800705 printf("Unsupported mxc_clock %d\n", clk);
706 break;
Peng Fan99878462019-08-27 06:25:51 +0000707 }
708
709 return 0;
710}
Marek Vasut363725d2020-04-24 21:37:26 +0200711
712#ifdef CONFIG_FEC_MXC
713int set_clk_enet(enum enet_freq type)
714{
715 u32 target;
716 u32 enet1_ref;
717
718 switch (type) {
719 case ENET_125MHZ:
720 enet1_ref = ENET1_REF_CLK_ROOT_FROM_PLL_ENET_MAIN_125M_CLK;
721 break;
722 case ENET_50MHZ:
723 enet1_ref = ENET1_REF_CLK_ROOT_FROM_PLL_ENET_MAIN_50M_CLK;
724 break;
725 case ENET_25MHZ:
726 enet1_ref = ENET1_REF_CLK_ROOT_FROM_PLL_ENET_MAIN_25M_CLK;
727 break;
728 default:
729 return -EINVAL;
730 }
731
732 /* disable the clock first */
733 clock_enable(CCGR_ENET1, 0);
734 clock_enable(CCGR_SIM_ENET, 0);
735
736 /* set enet axi clock 266Mhz */
737 target = CLK_ROOT_ON | ENET_AXI_CLK_ROOT_FROM_SYS1_PLL_266M |
738 CLK_ROOT_PRE_DIV(CLK_ROOT_PRE_DIV1) |
739 CLK_ROOT_POST_DIV(CLK_ROOT_POST_DIV1);
740 clock_set_target_val(ENET_AXI_CLK_ROOT, target);
741
742 target = CLK_ROOT_ON | enet1_ref |
743 CLK_ROOT_PRE_DIV(CLK_ROOT_PRE_DIV1) |
744 CLK_ROOT_POST_DIV(CLK_ROOT_POST_DIV1);
745 clock_set_target_val(ENET_REF_CLK_ROOT, target);
746
747 target = CLK_ROOT_ON |
748 ENET1_TIME_CLK_ROOT_FROM_PLL_ENET_MAIN_100M_CLK |
749 CLK_ROOT_PRE_DIV(CLK_ROOT_PRE_DIV1) |
750 CLK_ROOT_POST_DIV(CLK_ROOT_POST_DIV4);
751 clock_set_target_val(ENET_TIMER_CLK_ROOT, target);
752
753 /* enable clock */
754 clock_enable(CCGR_SIM_ENET, 1);
755 clock_enable(CCGR_ENET1, 1);
756
757 return 0;
758}
759#endif