blob: 64ad57e9b39a32df46f7931bbdb3d54a9d676232 [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>
Simon Glass3ba929a2020-10-30 21:38:53 -060012#include <asm/global_data.h>
Peng Fan99878462019-08-27 06:25:51 +000013#include <asm/io.h>
Peng Fan99878462019-08-27 06:25:51 +000014#include <div64.h>
15#include <errno.h>
Simon Glass4dcacfc2020-05-10 11:40:13 -060016#include <linux/bitops.h>
Simon Glassdbd79542020-05-10 11:40:11 -060017#include <linux/delay.h>
Peng Fan99878462019-08-27 06:25:51 +000018
19DECLARE_GLOBAL_DATA_PTR;
20
21static struct anamix_pll *ana_pll = (struct anamix_pll *)ANATOP_BASE_ADDR;
22
Peng Fan4e36e332020-07-09 11:06:24 +080023static u32 get_root_clk(enum clk_root_index clock_id);
Ye Liebabd8d2021-03-25 17:30:17 +080024
25#ifdef CONFIG_IMX_HAB
26void hab_caam_clock_enable(unsigned char enable)
27{
28 /* The CAAM clock is always on for iMX8M */
29}
30#endif
31
Peng Fan99878462019-08-27 06:25:51 +000032void enable_ocotp_clk(unsigned char enable)
33{
Peng Fan60c29bb2019-12-30 16:52:30 +080034 clock_enable(CCGR_OCOTP, !!enable);
Peng Fan99878462019-08-27 06:25:51 +000035}
36
37int enable_i2c_clk(unsigned char enable, unsigned i2c_num)
38{
Martyn Welchc445dc42022-10-25 10:54:59 +010039 u8 i2c_ccgr[6] = {
40 CCGR_I2C1, CCGR_I2C2, CCGR_I2C3, CCGR_I2C4,
41#if (IS_ENABLED(CONFIG_IMX8MP))
42 CCGR_I2C5_8MP, CCGR_I2C6_8MP
43#endif
44 };
45
46 if (i2c_num > ARRAY_SIZE(i2c_ccgr))
Peng Fan60c29bb2019-12-30 16:52:30 +080047 return -EINVAL;
Peng Fan99878462019-08-27 06:25:51 +000048
Martyn Welchc445dc42022-10-25 10:54:59 +010049 clock_enable(i2c_ccgr[i2c_num], !!enable);
Peng Fan99878462019-08-27 06:25:51 +000050
Peng Fan60c29bb2019-12-30 16:52:30 +080051 return 0;
Peng Fan99878462019-08-27 06:25:51 +000052}
53
54#ifdef CONFIG_SPL_BUILD
55static struct imx_int_pll_rate_table imx8mm_fracpll_tbl[] = {
Peng Fand29bf222019-12-27 11:40:55 +080056 PLL_1443X_RATE(1000000000U, 250, 3, 1, 0),
Marek Vasutc5eab902022-02-26 04:37:41 +010057 PLL_1443X_RATE(933000000U, 311, 4, 1, 0),
Peng Fan99878462019-08-27 06:25:51 +000058 PLL_1443X_RATE(800000000U, 300, 9, 0, 0),
59 PLL_1443X_RATE(750000000U, 250, 8, 0, 0),
60 PLL_1443X_RATE(650000000U, 325, 3, 2, 0),
61 PLL_1443X_RATE(600000000U, 300, 3, 2, 0),
62 PLL_1443X_RATE(594000000U, 99, 1, 2, 0),
63 PLL_1443X_RATE(400000000U, 300, 9, 1, 0),
Andrey Zhizhikin09786cd2021-05-03 09:59:17 +020064 PLL_1443X_RATE(266000000U, 400, 9, 2, 0),
Peng Fan99878462019-08-27 06:25:51 +000065 PLL_1443X_RATE(167000000U, 334, 3, 4, 0),
66 PLL_1443X_RATE(100000000U, 300, 9, 3, 0),
67};
68
Alifer Moraes39931102020-01-14 15:54:59 -030069static int fracpll_configure(enum pll_clocks pll, u32 freq)
Peng Fan99878462019-08-27 06:25:51 +000070{
71 int i;
72 u32 tmp, div_val;
73 void *pll_base;
74 struct imx_int_pll_rate_table *rate;
75
76 for (i = 0; i < ARRAY_SIZE(imx8mm_fracpll_tbl); i++) {
77 if (freq == imx8mm_fracpll_tbl[i].rate)
78 break;
79 }
80
81 if (i == ARRAY_SIZE(imx8mm_fracpll_tbl)) {
Andrey Zhizhikin367ca322021-05-03 10:02:10 +020082 printf("%s: No matched freq table %u\n", __func__, freq);
Peng Fan99878462019-08-27 06:25:51 +000083 return -EINVAL;
84 }
85
86 rate = &imx8mm_fracpll_tbl[i];
87
88 switch (pll) {
89 case ANATOP_DRAM_PLL:
90 setbits_le32(GPC_BASE_ADDR + 0xEC, 1 << 7);
91 setbits_le32(GPC_BASE_ADDR + 0xF8, 1 << 5);
92 writel(SRC_DDR1_ENABLE_MASK, SRC_BASE_ADDR + 0x1004);
93
94 pll_base = &ana_pll->dram_pll_gnrl_ctl;
95 break;
96 case ANATOP_VIDEO_PLL:
97 pll_base = &ana_pll->video_pll1_gnrl_ctl;
98 break;
99 default:
100 return 0;
101 }
102 /* Bypass clock and set lock to pll output lock */
103 tmp = readl(pll_base);
104 tmp |= BYPASS_MASK;
105 writel(tmp, pll_base);
106
107 /* Enable RST */
108 tmp &= ~RST_MASK;
109 writel(tmp, pll_base);
110
111 div_val = (rate->mdiv << MDIV_SHIFT) | (rate->pdiv << PDIV_SHIFT) |
112 (rate->sdiv << SDIV_SHIFT);
113 writel(div_val, pll_base + 4);
114 writel(rate->kdiv << KDIV_SHIFT, pll_base + 8);
115
116 __udelay(100);
117
118 /* Disable RST */
119 tmp |= RST_MASK;
120 writel(tmp, pll_base);
121
122 /* Wait Lock*/
123 while (!(readl(pll_base) & LOCK_STATUS))
124 ;
125
126 /* Bypass */
127 tmp &= ~BYPASS_MASK;
128 writel(tmp, pll_base);
129
130 return 0;
131}
132
133void dram_pll_init(ulong pll_val)
134{
135 fracpll_configure(ANATOP_DRAM_PLL, pll_val);
136}
137
138static struct dram_bypass_clk_setting imx8mm_dram_bypass_tbl[] = {
139 DRAM_BYPASS_ROOT_CONFIG(MHZ(100), 2, CLK_ROOT_PRE_DIV1, 2,
140 CLK_ROOT_PRE_DIV2),
141 DRAM_BYPASS_ROOT_CONFIG(MHZ(250), 3, CLK_ROOT_PRE_DIV2, 2,
142 CLK_ROOT_PRE_DIV2),
143 DRAM_BYPASS_ROOT_CONFIG(MHZ(400), 1, CLK_ROOT_PRE_DIV2, 3,
144 CLK_ROOT_PRE_DIV2),
145};
146
147void dram_enable_bypass(ulong clk_val)
148{
149 int i;
150 struct dram_bypass_clk_setting *config;
151
152 for (i = 0; i < ARRAY_SIZE(imx8mm_dram_bypass_tbl); i++) {
153 if (clk_val == imx8mm_dram_bypass_tbl[i].clk)
154 break;
155 }
156
157 if (i == ARRAY_SIZE(imx8mm_dram_bypass_tbl)) {
Andrey Zhizhikin367ca322021-05-03 10:02:10 +0200158 printf("%s: No matched freq table %lu\n", __func__, clk_val);
Peng Fan99878462019-08-27 06:25:51 +0000159 return;
160 }
161
162 config = &imx8mm_dram_bypass_tbl[i];
163
164 clock_set_target_val(DRAM_ALT_CLK_ROOT, CLK_ROOT_ON |
165 CLK_ROOT_SOURCE_SEL(config->alt_root_sel) |
166 CLK_ROOT_PRE_DIV(config->alt_pre_div));
167 clock_set_target_val(DRAM_APB_CLK_ROOT, CLK_ROOT_ON |
168 CLK_ROOT_SOURCE_SEL(config->apb_root_sel) |
169 CLK_ROOT_PRE_DIV(config->apb_pre_div));
170 clock_set_target_val(DRAM_SEL_CFG, CLK_ROOT_ON |
171 CLK_ROOT_SOURCE_SEL(1));
172}
173
174void dram_disable_bypass(void)
175{
176 clock_set_target_val(DRAM_SEL_CFG, CLK_ROOT_ON |
177 CLK_ROOT_SOURCE_SEL(0));
178 clock_set_target_val(DRAM_APB_CLK_ROOT, CLK_ROOT_ON |
179 CLK_ROOT_SOURCE_SEL(4) |
180 CLK_ROOT_PRE_DIV(CLK_ROOT_PRE_DIV5));
181}
182#endif
183
Peng Fan4e36e332020-07-09 11:06:24 +0800184int intpll_configure(enum pll_clocks pll, ulong freq)
185{
186 void __iomem *pll_gnrl_ctl, __iomem *pll_div_ctl;
187 u32 pll_div_ctl_val, pll_clke_masks;
188
189 switch (pll) {
190 case ANATOP_SYSTEM_PLL1:
191 pll_gnrl_ctl = &ana_pll->sys_pll1_gnrl_ctl;
192 pll_div_ctl = &ana_pll->sys_pll1_div_ctl;
193 pll_clke_masks = INTPLL_DIV20_CLKE_MASK |
194 INTPLL_DIV10_CLKE_MASK | INTPLL_DIV8_CLKE_MASK |
195 INTPLL_DIV6_CLKE_MASK | INTPLL_DIV5_CLKE_MASK |
196 INTPLL_DIV4_CLKE_MASK | INTPLL_DIV3_CLKE_MASK |
197 INTPLL_DIV2_CLKE_MASK | INTPLL_CLKE_MASK;
198 break;
199 case ANATOP_SYSTEM_PLL2:
200 pll_gnrl_ctl = &ana_pll->sys_pll2_gnrl_ctl;
201 pll_div_ctl = &ana_pll->sys_pll2_div_ctl;
202 pll_clke_masks = INTPLL_DIV20_CLKE_MASK |
203 INTPLL_DIV10_CLKE_MASK | INTPLL_DIV8_CLKE_MASK |
204 INTPLL_DIV6_CLKE_MASK | INTPLL_DIV5_CLKE_MASK |
205 INTPLL_DIV4_CLKE_MASK | INTPLL_DIV3_CLKE_MASK |
206 INTPLL_DIV2_CLKE_MASK | INTPLL_CLKE_MASK;
207 break;
208 case ANATOP_SYSTEM_PLL3:
209 pll_gnrl_ctl = &ana_pll->sys_pll3_gnrl_ctl;
210 pll_div_ctl = &ana_pll->sys_pll3_div_ctl;
211 pll_clke_masks = INTPLL_CLKE_MASK;
212 break;
213 case ANATOP_ARM_PLL:
214 pll_gnrl_ctl = &ana_pll->arm_pll_gnrl_ctl;
215 pll_div_ctl = &ana_pll->arm_pll_div_ctl;
216 pll_clke_masks = INTPLL_CLKE_MASK;
217 break;
218 case ANATOP_GPU_PLL:
219 pll_gnrl_ctl = &ana_pll->gpu_pll_gnrl_ctl;
220 pll_div_ctl = &ana_pll->gpu_pll_div_ctl;
221 pll_clke_masks = INTPLL_CLKE_MASK;
222 break;
223 case ANATOP_VPU_PLL:
224 pll_gnrl_ctl = &ana_pll->vpu_pll_gnrl_ctl;
225 pll_div_ctl = &ana_pll->vpu_pll_div_ctl;
226 pll_clke_masks = INTPLL_CLKE_MASK;
227 break;
228 default:
229 return -EINVAL;
230 };
231
232 switch (freq) {
233 case MHZ(600):
234 /* 24 * 0x12c / 3 / 2 ^ 2 */
235 pll_div_ctl_val = INTPLL_MAIN_DIV_VAL(0x12c) |
236 INTPLL_PRE_DIV_VAL(3) | INTPLL_POST_DIV_VAL(2);
237 break;
238 case MHZ(750):
239 /* 24 * 0xfa / 2 / 2 ^ 2 */
240 pll_div_ctl_val = INTPLL_MAIN_DIV_VAL(0xfa) |
241 INTPLL_PRE_DIV_VAL(2) | INTPLL_POST_DIV_VAL(2);
242 break;
243 case MHZ(800):
244 /* 24 * 0x190 / 3 / 2 ^ 2 */
245 pll_div_ctl_val = INTPLL_MAIN_DIV_VAL(0x190) |
246 INTPLL_PRE_DIV_VAL(3) | INTPLL_POST_DIV_VAL(2);
247 break;
248 case MHZ(1000):
249 /* 24 * 0xfa / 3 / 2 ^ 1 */
250 pll_div_ctl_val = INTPLL_MAIN_DIV_VAL(0xfa) |
251 INTPLL_PRE_DIV_VAL(3) | INTPLL_POST_DIV_VAL(1);
252 break;
253 case MHZ(1200):
Marek Vasutd2578f22022-01-25 03:48:05 +0100254 /* 24 * 0x12c / 3 / 2 ^ 1 */
255 pll_div_ctl_val = INTPLL_MAIN_DIV_VAL(0x12c) |
256 INTPLL_PRE_DIV_VAL(3) | INTPLL_POST_DIV_VAL(1);
Peng Fan4e36e332020-07-09 11:06:24 +0800257 break;
Marek Vasutd376a502022-01-25 03:48:06 +0100258 case MHZ(1400):
259 /* 24 * 0x15e / 3 / 2 ^ 1 */
260 pll_div_ctl_val = INTPLL_MAIN_DIV_VAL(0x15e) |
261 INTPLL_PRE_DIV_VAL(3) | INTPLL_POST_DIV_VAL(1);
262 break;
263 case MHZ(1500):
264 /* 24 * 0x177 / 3 / 2 ^ 1 */
265 pll_div_ctl_val = INTPLL_MAIN_DIV_VAL(0x177) |
266 INTPLL_PRE_DIV_VAL(3) | INTPLL_POST_DIV_VAL(1);
267 break;
268 case MHZ(1600):
269 /* 24 * 0xc8 / 3 / 2 ^ 0 */
270 pll_div_ctl_val = INTPLL_MAIN_DIV_VAL(0xc8) |
271 INTPLL_PRE_DIV_VAL(3) | INTPLL_POST_DIV_VAL(0);
272 break;
273 case MHZ(1800):
274 /* 24 * 0xe1 / 3 / 2 ^ 0 */
275 pll_div_ctl_val = INTPLL_MAIN_DIV_VAL(0xe1) |
276 INTPLL_PRE_DIV_VAL(3) | INTPLL_POST_DIV_VAL(0);
277 break;
Peng Fan4e36e332020-07-09 11:06:24 +0800278 case MHZ(2000):
279 /* 24 * 0xfa / 3 / 2 ^ 0 */
280 pll_div_ctl_val = INTPLL_MAIN_DIV_VAL(0xfa) |
281 INTPLL_PRE_DIV_VAL(3) | INTPLL_POST_DIV_VAL(0);
282 break;
283 default:
284 return -EINVAL;
285 };
286 /* Bypass clock and set lock to pll output lock */
287 setbits_le32(pll_gnrl_ctl, INTPLL_BYPASS_MASK | INTPLL_LOCK_SEL_MASK);
288 /* Enable reset */
289 clrbits_le32(pll_gnrl_ctl, INTPLL_RST_MASK);
290 /* Configure */
291 writel(pll_div_ctl_val, pll_div_ctl);
292
293 __udelay(100);
294
295 /* Disable reset */
296 setbits_le32(pll_gnrl_ctl, INTPLL_RST_MASK);
297 /* Wait Lock */
298 while (!(readl(pll_gnrl_ctl) & INTPLL_LOCK_MASK))
299 ;
300 /* Clear bypass */
301 clrbits_le32(pll_gnrl_ctl, INTPLL_BYPASS_MASK);
302 setbits_le32(pll_gnrl_ctl, pll_clke_masks);
303
304 return 0;
305}
306
Peng Fan99878462019-08-27 06:25:51 +0000307void init_uart_clk(u32 index)
308{
309 /*
310 * set uart clock root
311 * 24M OSC
312 */
313 switch (index) {
314 case 0:
315 clock_enable(CCGR_UART1, 0);
316 clock_set_target_val(UART1_CLK_ROOT, CLK_ROOT_ON |
317 CLK_ROOT_SOURCE_SEL(0));
318 clock_enable(CCGR_UART1, 1);
319 return;
320 case 1:
321 clock_enable(CCGR_UART2, 0);
322 clock_set_target_val(UART2_CLK_ROOT, CLK_ROOT_ON |
323 CLK_ROOT_SOURCE_SEL(0));
324 clock_enable(CCGR_UART2, 1);
325 return;
326 case 2:
327 clock_enable(CCGR_UART3, 0);
328 clock_set_target_val(UART3_CLK_ROOT, CLK_ROOT_ON |
329 CLK_ROOT_SOURCE_SEL(0));
330 clock_enable(CCGR_UART3, 1);
331 return;
332 case 3:
333 clock_enable(CCGR_UART4, 0);
334 clock_set_target_val(UART4_CLK_ROOT, CLK_ROOT_ON |
335 CLK_ROOT_SOURCE_SEL(0));
336 clock_enable(CCGR_UART4, 1);
337 return;
338 default:
339 printf("Invalid uart index\n");
340 return;
341 }
342}
343
344void init_wdog_clk(void)
345{
346 clock_enable(CCGR_WDOG1, 0);
347 clock_enable(CCGR_WDOG2, 0);
348 clock_enable(CCGR_WDOG3, 0);
349 clock_set_target_val(WDOG_CLK_ROOT, CLK_ROOT_ON |
350 CLK_ROOT_SOURCE_SEL(0));
351 clock_enable(CCGR_WDOG1, 1);
352 clock_enable(CCGR_WDOG2, 1);
353 clock_enable(CCGR_WDOG3, 1);
354}
355
Peng Fanc61ec9b2020-07-09 11:35:15 +0800356void init_clk_usdhc(u32 index)
357{
358 /*
359 * set usdhc clock root
360 * sys pll1 400M
361 */
362 switch (index) {
363 case 0:
364 clock_enable(CCGR_USDHC1, 0);
365 clock_set_target_val(USDHC1_CLK_ROOT, CLK_ROOT_ON |
366 CLK_ROOT_SOURCE_SEL(1));
367 clock_enable(CCGR_USDHC1, 1);
368 return;
369 case 1:
370 clock_enable(CCGR_USDHC2, 0);
371 clock_set_target_val(USDHC2_CLK_ROOT, CLK_ROOT_ON |
372 CLK_ROOT_SOURCE_SEL(1));
373 clock_enable(CCGR_USDHC2, 1);
374 return;
375 case 2:
376 clock_enable(CCGR_USDHC3, 0);
377 clock_set_target_val(USDHC3_CLK_ROOT, CLK_ROOT_ON |
378 CLK_ROOT_SOURCE_SEL(1));
379 clock_enable(CCGR_USDHC3, 1);
380 return;
381 default:
382 printf("Invalid usdhc index\n");
383 return;
384 }
385}
386
387void init_clk_ecspi(u32 index)
388{
389 switch (index) {
390 case 0:
391 clock_enable(CCGR_ECSPI1, 0);
392 clock_set_target_val(ECSPI1_CLK_ROOT, CLK_ROOT_ON | CLK_ROOT_SOURCE_SEL(0));
393 clock_enable(CCGR_ECSPI1, 1);
394 return;
395 case 1:
396 clock_enable(CCGR_ECSPI2, 0);
397 clock_set_target_val(ECSPI2_CLK_ROOT, CLK_ROOT_ON | CLK_ROOT_SOURCE_SEL(0));
398 clock_enable(CCGR_ECSPI2, 1);
Peng Fan016c2512020-09-16 15:17:21 +0800399 return;
Peng Fanc61ec9b2020-07-09 11:35:15 +0800400 case 2:
401 clock_enable(CCGR_ECSPI3, 0);
402 clock_set_target_val(ECSPI3_CLK_ROOT, CLK_ROOT_ON | CLK_ROOT_SOURCE_SEL(0));
403 clock_enable(CCGR_ECSPI3, 1);
404 return;
405 default:
406 printf("Invalid ecspi index\n");
407 return;
408 }
409}
410
411void init_nand_clk(void)
412{
413 /*
414 * set rawnand root
415 * sys pll1 400M
416 */
417 clock_enable(CCGR_RAWNAND, 0);
418 clock_set_target_val(NAND_CLK_ROOT, CLK_ROOT_ON |
419 CLK_ROOT_SOURCE_SEL(3) | CLK_ROOT_POST_DIV(CLK_ROOT_POST_DIV4)); /* 100M */
420 clock_enable(CCGR_RAWNAND, 1);
421}
422
Peng Fan99878462019-08-27 06:25:51 +0000423int clock_init(void)
424{
425 u32 val_cfg0;
426
427 /*
428 * The gate is not exported to clk tree, so configure them here.
429 * According to ANAMIX SPEC
430 * sys pll1 fixed at 800MHz
431 * sys pll2 fixed at 1GHz
432 * Here we only enable the outputs.
433 */
434 val_cfg0 = readl(&ana_pll->sys_pll1_gnrl_ctl);
435 val_cfg0 |= INTPLL_CLKE_MASK | INTPLL_DIV2_CLKE_MASK |
436 INTPLL_DIV3_CLKE_MASK | INTPLL_DIV4_CLKE_MASK |
437 INTPLL_DIV5_CLKE_MASK | INTPLL_DIV6_CLKE_MASK |
438 INTPLL_DIV8_CLKE_MASK | INTPLL_DIV10_CLKE_MASK |
439 INTPLL_DIV20_CLKE_MASK;
440 writel(val_cfg0, &ana_pll->sys_pll1_gnrl_ctl);
441
442 val_cfg0 = readl(&ana_pll->sys_pll2_gnrl_ctl);
443 val_cfg0 |= INTPLL_CLKE_MASK | INTPLL_DIV2_CLKE_MASK |
444 INTPLL_DIV3_CLKE_MASK | INTPLL_DIV4_CLKE_MASK |
445 INTPLL_DIV5_CLKE_MASK | INTPLL_DIV6_CLKE_MASK |
446 INTPLL_DIV8_CLKE_MASK | INTPLL_DIV10_CLKE_MASK |
447 INTPLL_DIV20_CLKE_MASK;
448 writel(val_cfg0, &ana_pll->sys_pll2_gnrl_ctl);
449
Peng Fan4e36e332020-07-09 11:06:24 +0800450 /* Configure ARM at 1.2GHz */
451 clock_set_target_val(ARM_A53_CLK_ROOT, CLK_ROOT_ON |
452 CLK_ROOT_SOURCE_SEL(2));
453
454 intpll_configure(ANATOP_ARM_PLL, MHZ(1200));
455
456 /* Bypass CCM A53 ROOT, Switch to ARM PLL -> MUX-> CPU */
457 clock_set_target_val(CORE_SEL_CFG, CLK_ROOT_SOURCE_SEL(1));
458
Peng Fane5653932020-07-09 11:18:50 +0800459 if (is_imx8mn() || is_imx8mp())
460 intpll_configure(ANATOP_SYSTEM_PLL3, MHZ(600));
461 else
462 intpll_configure(ANATOP_SYSTEM_PLL3, MHZ(750));
463
464#ifdef CONFIG_IMX8MP
465 /* 8MP ROM already set NOC to 800Mhz, only need to configure NOC_IO clk to 600Mhz */
466 /* 8MP ROM already set GIC to 400Mhz, system_pll1_800m with div = 2 */
467 clock_set_target_val(NOC_IO_CLK_ROOT, CLK_ROOT_ON | CLK_ROOT_SOURCE_SEL(2));
468#else
469 clock_set_target_val(NOC_CLK_ROOT, CLK_ROOT_ON | CLK_ROOT_SOURCE_SEL(2));
470
Peng Fan99878462019-08-27 06:25:51 +0000471 /* config GIC to sys_pll2_100m */
472 clock_enable(CCGR_GIC, 0);
473 clock_set_target_val(GIC_CLK_ROOT, CLK_ROOT_ON |
474 CLK_ROOT_SOURCE_SEL(3));
475 clock_enable(CCGR_GIC, 1);
Peng Fane5653932020-07-09 11:18:50 +0800476#endif
Peng Fan99878462019-08-27 06:25:51 +0000477
478 clock_set_target_val(NAND_USDHC_BUS_CLK_ROOT, CLK_ROOT_ON |
479 CLK_ROOT_SOURCE_SEL(1));
480
481 clock_enable(CCGR_DDR1, 0);
482 clock_set_target_val(DRAM_ALT_CLK_ROOT, CLK_ROOT_ON |
483 CLK_ROOT_SOURCE_SEL(1));
484 clock_set_target_val(DRAM_APB_CLK_ROOT, CLK_ROOT_ON |
485 CLK_ROOT_SOURCE_SEL(1));
486 clock_enable(CCGR_DDR1, 1);
487
488 init_wdog_clk();
489
490 clock_enable(CCGR_TEMP_SENSOR, 1);
491
492 clock_enable(CCGR_SEC_DEBUG, 1);
493
494 return 0;
495};
496
497u32 imx_get_uartclk(void)
498{
499 return 24000000U;
500}
501
Alifer Moraes39931102020-01-14 15:54:59 -0300502static u32 decode_intpll(enum clk_root_src intpll)
Peng Fan60c29bb2019-12-30 16:52:30 +0800503{
504 u32 pll_gnrl_ctl, pll_div_ctl, pll_clke_mask;
505 u32 main_div, pre_div, post_div, div;
506 u64 freq;
507
508 switch (intpll) {
509 case ARM_PLL_CLK:
510 pll_gnrl_ctl = readl(&ana_pll->arm_pll_gnrl_ctl);
511 pll_div_ctl = readl(&ana_pll->arm_pll_div_ctl);
512 break;
513 case GPU_PLL_CLK:
514 pll_gnrl_ctl = readl(&ana_pll->gpu_pll_gnrl_ctl);
515 pll_div_ctl = readl(&ana_pll->gpu_pll_div_ctl);
516 break;
517 case VPU_PLL_CLK:
518 pll_gnrl_ctl = readl(&ana_pll->vpu_pll_gnrl_ctl);
519 pll_div_ctl = readl(&ana_pll->vpu_pll_div_ctl);
520 break;
521 case SYSTEM_PLL1_800M_CLK:
522 case SYSTEM_PLL1_400M_CLK:
523 case SYSTEM_PLL1_266M_CLK:
524 case SYSTEM_PLL1_200M_CLK:
525 case SYSTEM_PLL1_160M_CLK:
526 case SYSTEM_PLL1_133M_CLK:
527 case SYSTEM_PLL1_100M_CLK:
528 case SYSTEM_PLL1_80M_CLK:
529 case SYSTEM_PLL1_40M_CLK:
530 pll_gnrl_ctl = readl(&ana_pll->sys_pll1_gnrl_ctl);
531 pll_div_ctl = readl(&ana_pll->sys_pll1_div_ctl);
532 break;
533 case SYSTEM_PLL2_1000M_CLK:
534 case SYSTEM_PLL2_500M_CLK:
535 case SYSTEM_PLL2_333M_CLK:
536 case SYSTEM_PLL2_250M_CLK:
537 case SYSTEM_PLL2_200M_CLK:
538 case SYSTEM_PLL2_166M_CLK:
539 case SYSTEM_PLL2_125M_CLK:
540 case SYSTEM_PLL2_100M_CLK:
541 case SYSTEM_PLL2_50M_CLK:
542 pll_gnrl_ctl = readl(&ana_pll->sys_pll2_gnrl_ctl);
543 pll_div_ctl = readl(&ana_pll->sys_pll2_div_ctl);
544 break;
545 case SYSTEM_PLL3_CLK:
546 pll_gnrl_ctl = readl(&ana_pll->sys_pll3_gnrl_ctl);
547 pll_div_ctl = readl(&ana_pll->sys_pll3_div_ctl);
548 break;
549 default:
550 return -EINVAL;
551 }
552
553 /* Only support SYS_XTAL 24M, PAD_CLK not take into consideration */
554 if ((pll_gnrl_ctl & INTPLL_REF_CLK_SEL_MASK) != 0)
555 return 0;
556
557 if ((pll_gnrl_ctl & INTPLL_RST_MASK) == 0)
558 return 0;
559
560 /*
561 * When BYPASS is equal to 1, PLL enters the bypass mode
562 * regardless of the values of RESETB
563 */
564 if (pll_gnrl_ctl & INTPLL_BYPASS_MASK)
565 return 24000000u;
566
567 if (!(pll_gnrl_ctl & INTPLL_LOCK_MASK)) {
568 puts("pll not locked\n");
569 return 0;
570 }
571
572 switch (intpll) {
573 case ARM_PLL_CLK:
574 case GPU_PLL_CLK:
575 case VPU_PLL_CLK:
576 case SYSTEM_PLL3_CLK:
577 case SYSTEM_PLL1_800M_CLK:
578 case SYSTEM_PLL2_1000M_CLK:
579 pll_clke_mask = INTPLL_CLKE_MASK;
580 div = 1;
581 break;
582
583 case SYSTEM_PLL1_400M_CLK:
584 case SYSTEM_PLL2_500M_CLK:
585 pll_clke_mask = INTPLL_DIV2_CLKE_MASK;
586 div = 2;
587 break;
588
589 case SYSTEM_PLL1_266M_CLK:
590 case SYSTEM_PLL2_333M_CLK:
591 pll_clke_mask = INTPLL_DIV3_CLKE_MASK;
592 div = 3;
593 break;
594
595 case SYSTEM_PLL1_200M_CLK:
596 case SYSTEM_PLL2_250M_CLK:
597 pll_clke_mask = INTPLL_DIV4_CLKE_MASK;
598 div = 4;
599 break;
600
601 case SYSTEM_PLL1_160M_CLK:
602 case SYSTEM_PLL2_200M_CLK:
603 pll_clke_mask = INTPLL_DIV5_CLKE_MASK;
604 div = 5;
605 break;
606
607 case SYSTEM_PLL1_133M_CLK:
608 case SYSTEM_PLL2_166M_CLK:
609 pll_clke_mask = INTPLL_DIV6_CLKE_MASK;
610 div = 6;
611 break;
612
613 case SYSTEM_PLL1_100M_CLK:
614 case SYSTEM_PLL2_125M_CLK:
615 pll_clke_mask = INTPLL_DIV8_CLKE_MASK;
616 div = 8;
617 break;
618
619 case SYSTEM_PLL1_80M_CLK:
620 case SYSTEM_PLL2_100M_CLK:
621 pll_clke_mask = INTPLL_DIV10_CLKE_MASK;
622 div = 10;
623 break;
624
625 case SYSTEM_PLL1_40M_CLK:
626 case SYSTEM_PLL2_50M_CLK:
627 pll_clke_mask = INTPLL_DIV20_CLKE_MASK;
628 div = 20;
629 break;
630 default:
631 return -EINVAL;
632 }
633
634 if ((pll_gnrl_ctl & pll_clke_mask) == 0)
635 return 0;
636
637 main_div = (pll_div_ctl & INTPLL_MAIN_DIV_MASK) >>
638 INTPLL_MAIN_DIV_SHIFT;
639 pre_div = (pll_div_ctl & INTPLL_PRE_DIV_MASK) >>
640 INTPLL_PRE_DIV_SHIFT;
641 post_div = (pll_div_ctl & INTPLL_POST_DIV_MASK) >>
642 INTPLL_POST_DIV_SHIFT;
643
644 /* FFVCO = (m * FFIN) / p, FFOUT = (m * FFIN) / (p * 2^s) */
645 freq = 24000000ULL * main_div;
646 return lldiv(freq, pre_div * (1 << post_div) * div);
647}
648
Alifer Moraes39931102020-01-14 15:54:59 -0300649static u32 decode_fracpll(enum clk_root_src frac_pll)
Peng Fan60c29bb2019-12-30 16:52:30 +0800650{
651 u32 pll_gnrl_ctl, pll_fdiv_ctl0, pll_fdiv_ctl1;
652 u32 main_div, pre_div, post_div, k;
653
654 switch (frac_pll) {
655 case DRAM_PLL1_CLK:
656 pll_gnrl_ctl = readl(&ana_pll->dram_pll_gnrl_ctl);
657 pll_fdiv_ctl0 = readl(&ana_pll->dram_pll_fdiv_ctl0);
658 pll_fdiv_ctl1 = readl(&ana_pll->dram_pll_fdiv_ctl1);
659 break;
660 case AUDIO_PLL1_CLK:
661 pll_gnrl_ctl = readl(&ana_pll->audio_pll1_gnrl_ctl);
662 pll_fdiv_ctl0 = readl(&ana_pll->audio_pll1_fdiv_ctl0);
663 pll_fdiv_ctl1 = readl(&ana_pll->audio_pll1_fdiv_ctl1);
664 break;
665 case AUDIO_PLL2_CLK:
666 pll_gnrl_ctl = readl(&ana_pll->audio_pll2_gnrl_ctl);
667 pll_fdiv_ctl0 = readl(&ana_pll->audio_pll2_fdiv_ctl0);
668 pll_fdiv_ctl1 = readl(&ana_pll->audio_pll2_fdiv_ctl1);
669 break;
670 case VIDEO_PLL_CLK:
671 pll_gnrl_ctl = readl(&ana_pll->video_pll1_gnrl_ctl);
672 pll_fdiv_ctl0 = readl(&ana_pll->video_pll1_fdiv_ctl0);
673 pll_fdiv_ctl1 = readl(&ana_pll->video_pll1_fdiv_ctl1);
674 break;
675 default:
Andrey Zhizhikin367ca322021-05-03 10:02:10 +0200676 printf("Unsupported clk_root_src %d\n", frac_pll);
Peng Fan60c29bb2019-12-30 16:52:30 +0800677 return 0;
678 }
679
680 /* Only support SYS_XTAL 24M, PAD_CLK not take into consideration */
Ye Licc643ea2020-03-23 19:54:29 -0700681 if ((pll_gnrl_ctl & GENMASK(1, 0)) != 0)
Peng Fan60c29bb2019-12-30 16:52:30 +0800682 return 0;
683
Ye Licc643ea2020-03-23 19:54:29 -0700684 if ((pll_gnrl_ctl & RST_MASK) == 0)
Peng Fan60c29bb2019-12-30 16:52:30 +0800685 return 0;
686 /*
687 * When BYPASS is equal to 1, PLL enters the bypass mode
688 * regardless of the values of RESETB
689 */
Ye Licc643ea2020-03-23 19:54:29 -0700690 if (pll_gnrl_ctl & BYPASS_MASK)
Peng Fan60c29bb2019-12-30 16:52:30 +0800691 return 24000000u;
692
Ye Licc643ea2020-03-23 19:54:29 -0700693 if (!(pll_gnrl_ctl & LOCK_STATUS)) {
Peng Fan60c29bb2019-12-30 16:52:30 +0800694 puts("pll not locked\n");
695 return 0;
696 }
697
Ye Licc643ea2020-03-23 19:54:29 -0700698 if (!(pll_gnrl_ctl & CLKE_MASK))
Peng Fan60c29bb2019-12-30 16:52:30 +0800699 return 0;
700
Ye Licc643ea2020-03-23 19:54:29 -0700701 main_div = (pll_fdiv_ctl0 & MDIV_MASK) >>
702 MDIV_SHIFT;
703 pre_div = (pll_fdiv_ctl0 & PDIV_MASK) >>
704 PDIV_SHIFT;
705 post_div = (pll_fdiv_ctl0 & SDIV_MASK) >>
706 SDIV_SHIFT;
Peng Fan60c29bb2019-12-30 16:52:30 +0800707
Ye Licc643ea2020-03-23 19:54:29 -0700708 k = pll_fdiv_ctl1 & KDIV_MASK;
Peng Fan60c29bb2019-12-30 16:52:30 +0800709
710 return lldiv((main_div * 65536 + k) * 24000000ULL,
711 65536 * pre_div * (1 << post_div));
712}
713
Alifer Moraes39931102020-01-14 15:54:59 -0300714static u32 get_root_src_clk(enum clk_root_src root_src)
Peng Fan60c29bb2019-12-30 16:52:30 +0800715{
716 switch (root_src) {
717 case OSC_24M_CLK:
718 return 24000000u;
719 case OSC_HDMI_CLK:
720 return 26000000u;
721 case OSC_32K_CLK:
722 return 32000u;
723 case ARM_PLL_CLK:
724 case GPU_PLL_CLK:
725 case VPU_PLL_CLK:
726 case SYSTEM_PLL1_800M_CLK:
727 case SYSTEM_PLL1_400M_CLK:
728 case SYSTEM_PLL1_266M_CLK:
729 case SYSTEM_PLL1_200M_CLK:
730 case SYSTEM_PLL1_160M_CLK:
731 case SYSTEM_PLL1_133M_CLK:
732 case SYSTEM_PLL1_100M_CLK:
733 case SYSTEM_PLL1_80M_CLK:
734 case SYSTEM_PLL1_40M_CLK:
735 case SYSTEM_PLL2_1000M_CLK:
736 case SYSTEM_PLL2_500M_CLK:
737 case SYSTEM_PLL2_333M_CLK:
738 case SYSTEM_PLL2_250M_CLK:
739 case SYSTEM_PLL2_200M_CLK:
740 case SYSTEM_PLL2_166M_CLK:
741 case SYSTEM_PLL2_125M_CLK:
742 case SYSTEM_PLL2_100M_CLK:
743 case SYSTEM_PLL2_50M_CLK:
744 case SYSTEM_PLL3_CLK:
745 return decode_intpll(root_src);
746 case DRAM_PLL1_CLK:
747 case AUDIO_PLL1_CLK:
748 case AUDIO_PLL2_CLK:
749 case VIDEO_PLL_CLK:
750 return decode_fracpll(root_src);
Peng Fan4e36e332020-07-09 11:06:24 +0800751 case ARM_A53_ALT_CLK:
752 return get_root_clk(ARM_A53_CLK_ROOT);
Peng Fan60c29bb2019-12-30 16:52:30 +0800753 default:
754 return 0;
755 }
756
757 return 0;
758}
759
Alifer Moraes39931102020-01-14 15:54:59 -0300760static u32 get_root_clk(enum clk_root_index clock_id)
Peng Fan60c29bb2019-12-30 16:52:30 +0800761{
762 enum clk_root_src root_src;
763 u32 post_podf, pre_podf, root_src_clk;
764
765 if (clock_root_enabled(clock_id) <= 0)
766 return 0;
767
768 if (clock_get_prediv(clock_id, &pre_podf) < 0)
769 return 0;
770
771 if (clock_get_postdiv(clock_id, &post_podf) < 0)
772 return 0;
773
774 if (clock_get_src(clock_id, &root_src) < 0)
775 return 0;
776
777 root_src_clk = get_root_src_clk(root_src);
778
779 return root_src_clk / (post_podf + 1) / (pre_podf + 1);
780}
781
Peng Fan4e36e332020-07-09 11:06:24 +0800782u32 get_arm_core_clk(void)
783{
784 enum clk_root_src root_src;
785 u32 root_src_clk;
786
787 if (clock_get_src(CORE_SEL_CFG, &root_src) < 0)
788 return 0;
789
790 root_src_clk = get_root_src_clk(root_src);
791
792 return root_src_clk;
793}
794
Peng Fan99878462019-08-27 06:25:51 +0000795u32 mxc_get_clock(enum mxc_clock clk)
796{
Peng Fan60c29bb2019-12-30 16:52:30 +0800797 u32 val;
Peng Fan99878462019-08-27 06:25:51 +0000798
799 switch (clk) {
Peng Fan99878462019-08-27 06:25:51 +0000800 case MXC_ARM_CLK:
Peng Fan4e36e332020-07-09 11:06:24 +0800801 return get_arm_core_clk();
Peng Fan60c29bb2019-12-30 16:52:30 +0800802 case MXC_IPG_CLK:
803 clock_get_target_val(IPG_CLK_ROOT, &val);
804 val = val & 0x3;
805 return get_root_clk(AHB_CLK_ROOT) / 2 / (val + 1);
806 case MXC_CSPI_CLK:
807 return get_root_clk(ECSPI1_CLK_ROOT);
808 case MXC_ESDHC_CLK:
809 return get_root_clk(USDHC1_CLK_ROOT);
810 case MXC_ESDHC2_CLK:
811 return get_root_clk(USDHC2_CLK_ROOT);
812 case MXC_ESDHC3_CLK:
813 return get_root_clk(USDHC3_CLK_ROOT);
814 case MXC_I2C_CLK:
815 return get_root_clk(I2C1_CLK_ROOT);
816 case MXC_UART_CLK:
817 return get_root_clk(UART1_CLK_ROOT);
818 case MXC_QSPI_CLK:
819 return get_root_clk(QSPI_CLK_ROOT);
Peng Fan99878462019-08-27 06:25:51 +0000820 default:
Peng Fan60c29bb2019-12-30 16:52:30 +0800821 printf("Unsupported mxc_clock %d\n", clk);
822 break;
Peng Fan99878462019-08-27 06:25:51 +0000823 }
824
825 return 0;
826}
Marek Vasut363725d2020-04-24 21:37:26 +0200827
Peng Fan1dd259c2020-07-09 13:14:20 +0800828#ifdef CONFIG_DWC_ETH_QOS
829int set_clk_eqos(enum enet_freq type)
830{
831 u32 target;
832 u32 enet1_ref;
833
834 switch (type) {
835 case ENET_125MHZ:
836 enet1_ref = ENET1_REF_CLK_ROOT_FROM_PLL_ENET_MAIN_125M_CLK;
837 break;
838 case ENET_50MHZ:
839 enet1_ref = ENET1_REF_CLK_ROOT_FROM_PLL_ENET_MAIN_50M_CLK;
840 break;
841 case ENET_25MHZ:
842 enet1_ref = ENET1_REF_CLK_ROOT_FROM_PLL_ENET_MAIN_25M_CLK;
843 break;
844 default:
845 return -EINVAL;
846 }
847
848 /* disable the clock first */
849 clock_enable(CCGR_QOS_ETHENET, 0);
850 clock_enable(CCGR_SDMA2, 0);
851
852 /* set enet axi clock 266Mhz */
853 target = CLK_ROOT_ON | ENET_AXI_CLK_ROOT_FROM_SYS1_PLL_266M |
854 CLK_ROOT_PRE_DIV(CLK_ROOT_PRE_DIV1) |
855 CLK_ROOT_POST_DIV(CLK_ROOT_POST_DIV1);
856 clock_set_target_val(ENET_AXI_CLK_ROOT, target);
857
858 target = CLK_ROOT_ON | enet1_ref |
859 CLK_ROOT_PRE_DIV(CLK_ROOT_PRE_DIV1) |
860 CLK_ROOT_POST_DIV(CLK_ROOT_POST_DIV1);
861 clock_set_target_val(ENET_QOS_CLK_ROOT, target);
862
863 target = CLK_ROOT_ON |
864 ENET1_TIME_CLK_ROOT_FROM_PLL_ENET_MAIN_100M_CLK |
865 CLK_ROOT_PRE_DIV(CLK_ROOT_PRE_DIV1) |
866 CLK_ROOT_POST_DIV(CLK_ROOT_POST_DIV4);
867 clock_set_target_val(ENET_QOS_TIMER_CLK_ROOT, target);
868
869 /* enable clock */
870 clock_enable(CCGR_QOS_ETHENET, 1);
871 clock_enable(CCGR_SDMA2, 1);
872
873 return 0;
874}
875
Marek BehĂșn789a99e2021-05-20 13:24:16 +0200876int imx_eqos_txclk_set_rate(ulong rate)
Peng Fan1dd259c2020-07-09 13:14:20 +0800877{
878 u32 val;
879 u32 eqos_post_div;
880
881 /* disable the clock first */
882 clock_enable(CCGR_QOS_ETHENET, 0);
883 clock_enable(CCGR_SDMA2, 0);
884
885 switch (rate) {
886 case 125000000:
887 eqos_post_div = 1;
888 break;
889 case 25000000:
890 eqos_post_div = 125000000 / 25000000;
891 break;
892 case 2500000:
893 eqos_post_div = 125000000 / 2500000;
894 break;
895 default:
896 return -EINVAL;
897 }
898
899 clock_get_target_val(ENET_QOS_CLK_ROOT, &val);
900 val &= ~(CLK_ROOT_PRE_DIV_MASK | CLK_ROOT_POST_DIV_MASK);
901 val |= CLK_ROOT_PRE_DIV(CLK_ROOT_PRE_DIV1) |
902 CLK_ROOT_POST_DIV(eqos_post_div - 1);
903 clock_set_target_val(ENET_QOS_CLK_ROOT, val);
904
905 /* enable clock */
906 clock_enable(CCGR_QOS_ETHENET, 1);
907 clock_enable(CCGR_SDMA2, 1);
908
909 return 0;
910}
911
912u32 imx_get_eqos_csr_clk(void)
913{
914 return get_root_clk(ENET_AXI_CLK_ROOT);
915}
916#endif
917
Marek Vasut363725d2020-04-24 21:37:26 +0200918#ifdef CONFIG_FEC_MXC
919int set_clk_enet(enum enet_freq type)
920{
921 u32 target;
922 u32 enet1_ref;
923
924 switch (type) {
925 case ENET_125MHZ:
926 enet1_ref = ENET1_REF_CLK_ROOT_FROM_PLL_ENET_MAIN_125M_CLK;
927 break;
928 case ENET_50MHZ:
929 enet1_ref = ENET1_REF_CLK_ROOT_FROM_PLL_ENET_MAIN_50M_CLK;
930 break;
931 case ENET_25MHZ:
932 enet1_ref = ENET1_REF_CLK_ROOT_FROM_PLL_ENET_MAIN_25M_CLK;
933 break;
934 default:
935 return -EINVAL;
936 }
937
938 /* disable the clock first */
939 clock_enable(CCGR_ENET1, 0);
940 clock_enable(CCGR_SIM_ENET, 0);
941
942 /* set enet axi clock 266Mhz */
943 target = CLK_ROOT_ON | ENET_AXI_CLK_ROOT_FROM_SYS1_PLL_266M |
944 CLK_ROOT_PRE_DIV(CLK_ROOT_PRE_DIV1) |
945 CLK_ROOT_POST_DIV(CLK_ROOT_POST_DIV1);
946 clock_set_target_val(ENET_AXI_CLK_ROOT, target);
947
948 target = CLK_ROOT_ON | enet1_ref |
949 CLK_ROOT_PRE_DIV(CLK_ROOT_PRE_DIV1) |
950 CLK_ROOT_POST_DIV(CLK_ROOT_POST_DIV1);
951 clock_set_target_val(ENET_REF_CLK_ROOT, target);
952
953 target = CLK_ROOT_ON |
954 ENET1_TIME_CLK_ROOT_FROM_PLL_ENET_MAIN_100M_CLK |
955 CLK_ROOT_PRE_DIV(CLK_ROOT_PRE_DIV1) |
956 CLK_ROOT_POST_DIV(CLK_ROOT_POST_DIV4);
957 clock_set_target_val(ENET_TIMER_CLK_ROOT, target);
958
959 /* enable clock */
960 clock_enable(CCGR_SIM_ENET, 1);
961 clock_enable(CCGR_ENET1, 1);
962
963 return 0;
964}
965#endif