blob: c23ddc12b456979aa57c9e6ca5fcaf7bb4296b4e [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Alison Wang035260a2013-05-27 22:55:42 +00002/*
3 * Copyright 2013 Freescale Semiconductor, Inc.
Alison Wang035260a2013-05-27 22:55:42 +00004 */
5
6#include <common.h>
Simon Glass85d65312019-12-28 10:44:58 -07007#include <clock_legacy.h>
Simon Glassed38aef2020-05-10 11:40:03 -06008#include <command.h>
Simon Glass1d91ba72019-11-14 12:57:37 -07009#include <cpu_func.h>
Simon Glass97589732020-05-10 11:40:02 -060010#include <init.h>
Simon Glass274e0b02020-05-10 11:39:56 -060011#include <net.h>
12#include <asm/cache.h>
Simon Glass3ba929a2020-10-30 21:38:53 -060013#include <asm/global_data.h>
Alison Wang035260a2013-05-27 22:55:42 +000014#include <asm/io.h>
15#include <asm/arch/imx-regs.h>
16#include <asm/arch/clock.h>
17#include <asm/arch/crm_regs.h>
Stefano Babic33731bc2017-06-29 10:16:06 +020018#include <asm/mach-imx/sys_proto.h>
Simon Glass5e6201b2019-08-01 09:46:51 -060019#include <env.h>
Alison Wang035260a2013-05-27 22:55:42 +000020#include <netdev.h>
Yangbo Lu73340382019-06-21 11:42:28 +080021#ifdef CONFIG_FSL_ESDHC_IMX
22#include <fsl_esdhc_imx.h>
Alison Wang035260a2013-05-27 22:55:42 +000023#endif
24
Yangbo Lu73340382019-06-21 11:42:28 +080025#ifdef CONFIG_FSL_ESDHC_IMX
Alison Wang035260a2013-05-27 22:55:42 +000026DECLARE_GLOBAL_DATA_PTR;
27#endif
28
Sanchayan Maitycb01fb12015-04-15 16:24:24 +053029static char soc_type[] = "xx0";
30
Alison Wang035260a2013-05-27 22:55:42 +000031#ifdef CONFIG_MXC_OCOTP
32void enable_ocotp_clk(unsigned char enable)
33{
34 struct ccm_reg *ccm = (struct ccm_reg *)CCM_BASE_ADDR;
35 u32 reg;
36
37 reg = readl(&ccm->ccgr6);
38 if (enable)
39 reg |= CCM_CCGR6_OCOTP_CTRL_MASK;
40 else
41 reg &= ~CCM_CCGR6_OCOTP_CTRL_MASK;
42 writel(reg, &ccm->ccgr6);
43}
44#endif
45
46static u32 get_mcu_main_clk(void)
47{
48 struct ccm_reg *ccm = (struct ccm_reg *)CCM_BASE_ADDR;
49 u32 ccm_ccsr, ccm_cacrr, armclk_div;
50 u32 sysclk_sel, pll_pfd_sel = 0;
51 u32 freq = 0;
52
53 ccm_ccsr = readl(&ccm->ccsr);
54 sysclk_sel = ccm_ccsr & CCM_CCSR_SYS_CLK_SEL_MASK;
55 sysclk_sel >>= CCM_CCSR_SYS_CLK_SEL_OFFSET;
56
57 ccm_cacrr = readl(&ccm->cacrr);
58 armclk_div = ccm_cacrr & CCM_CACRR_ARM_CLK_DIV_MASK;
59 armclk_div >>= CCM_CACRR_ARM_CLK_DIV_OFFSET;
60 armclk_div += 1;
61
62 switch (sysclk_sel) {
63 case 0:
64 freq = FASE_CLK_FREQ;
65 break;
66 case 1:
67 freq = SLOW_CLK_FREQ;
68 break;
69 case 2:
70 pll_pfd_sel = ccm_ccsr & CCM_CCSR_PLL2_PFD_CLK_SEL_MASK;
71 pll_pfd_sel >>= CCM_CCSR_PLL2_PFD_CLK_SEL_OFFSET;
72 if (pll_pfd_sel == 0)
73 freq = PLL2_MAIN_FREQ;
74 else if (pll_pfd_sel == 1)
75 freq = PLL2_PFD1_FREQ;
76 else if (pll_pfd_sel == 2)
77 freq = PLL2_PFD2_FREQ;
78 else if (pll_pfd_sel == 3)
79 freq = PLL2_PFD3_FREQ;
80 else if (pll_pfd_sel == 4)
81 freq = PLL2_PFD4_FREQ;
82 break;
83 case 3:
84 freq = PLL2_MAIN_FREQ;
85 break;
86 case 4:
87 pll_pfd_sel = ccm_ccsr & CCM_CCSR_PLL1_PFD_CLK_SEL_MASK;
88 pll_pfd_sel >>= CCM_CCSR_PLL1_PFD_CLK_SEL_OFFSET;
89 if (pll_pfd_sel == 0)
90 freq = PLL1_MAIN_FREQ;
91 else if (pll_pfd_sel == 1)
92 freq = PLL1_PFD1_FREQ;
93 else if (pll_pfd_sel == 2)
94 freq = PLL1_PFD2_FREQ;
95 else if (pll_pfd_sel == 3)
96 freq = PLL1_PFD3_FREQ;
97 else if (pll_pfd_sel == 4)
98 freq = PLL1_PFD4_FREQ;
99 break;
100 case 5:
101 freq = PLL3_MAIN_FREQ;
102 break;
103 default:
104 printf("unsupported system clock select\n");
105 }
106
107 return freq / armclk_div;
108}
109
110static u32 get_bus_clk(void)
111{
112 struct ccm_reg *ccm = (struct ccm_reg *)CCM_BASE_ADDR;
113 u32 ccm_cacrr, busclk_div;
114
115 ccm_cacrr = readl(&ccm->cacrr);
116
117 busclk_div = ccm_cacrr & CCM_CACRR_BUS_CLK_DIV_MASK;
118 busclk_div >>= CCM_CACRR_BUS_CLK_DIV_OFFSET;
119 busclk_div += 1;
120
121 return get_mcu_main_clk() / busclk_div;
122}
123
124static u32 get_ipg_clk(void)
125{
126 struct ccm_reg *ccm = (struct ccm_reg *)CCM_BASE_ADDR;
127 u32 ccm_cacrr, ipgclk_div;
128
129 ccm_cacrr = readl(&ccm->cacrr);
130
131 ipgclk_div = ccm_cacrr & CCM_CACRR_IPG_CLK_DIV_MASK;
132 ipgclk_div >>= CCM_CACRR_IPG_CLK_DIV_OFFSET;
133 ipgclk_div += 1;
134
135 return get_bus_clk() / ipgclk_div;
136}
137
138static u32 get_uart_clk(void)
139{
140 return get_ipg_clk();
141}
142
143static u32 get_sdhc_clk(void)
144{
145 struct ccm_reg *ccm = (struct ccm_reg *)CCM_BASE_ADDR;
146 u32 ccm_cscmr1, ccm_cscdr2, sdhc_clk_sel, sdhc_clk_div;
147 u32 freq = 0;
148
149 ccm_cscmr1 = readl(&ccm->cscmr1);
150 sdhc_clk_sel = ccm_cscmr1 & CCM_CSCMR1_ESDHC1_CLK_SEL_MASK;
151 sdhc_clk_sel >>= CCM_CSCMR1_ESDHC1_CLK_SEL_OFFSET;
152
153 ccm_cscdr2 = readl(&ccm->cscdr2);
154 sdhc_clk_div = ccm_cscdr2 & CCM_CSCDR2_ESDHC1_CLK_DIV_MASK;
155 sdhc_clk_div >>= CCM_CSCDR2_ESDHC1_CLK_DIV_OFFSET;
156 sdhc_clk_div += 1;
157
158 switch (sdhc_clk_sel) {
159 case 0:
160 freq = PLL3_MAIN_FREQ;
161 break;
162 case 1:
163 freq = PLL3_PFD3_FREQ;
164 break;
165 case 2:
166 freq = PLL1_PFD3_FREQ;
167 break;
168 case 3:
169 freq = get_bus_clk();
170 break;
171 }
172
173 return freq / sdhc_clk_div;
174}
175
176u32 get_fec_clk(void)
177{
178 struct ccm_reg *ccm = (struct ccm_reg *)CCM_BASE_ADDR;
179 u32 ccm_cscmr2, rmii_clk_sel;
180 u32 freq = 0;
181
182 ccm_cscmr2 = readl(&ccm->cscmr2);
183 rmii_clk_sel = ccm_cscmr2 & CCM_CSCMR2_RMII_CLK_SEL_MASK;
184 rmii_clk_sel >>= CCM_CSCMR2_RMII_CLK_SEL_OFFSET;
185
186 switch (rmii_clk_sel) {
187 case 0:
188 freq = ENET_EXTERNAL_CLK;
189 break;
190 case 1:
191 freq = AUDIO_EXTERNAL_CLK;
192 break;
193 case 2:
194 freq = PLL5_MAIN_FREQ;
195 break;
196 case 3:
197 freq = PLL5_MAIN_FREQ / 2;
198 break;
199 }
200
201 return freq;
202}
203
Alison Wang86bef202013-06-17 15:30:38 +0800204static u32 get_i2c_clk(void)
205{
206 return get_ipg_clk();
207}
208
Bhuvanchandra DV860c1802015-06-01 18:37:19 +0530209static u32 get_dspi_clk(void)
210{
211 return get_ipg_clk();
212}
213
Peng Fan836a6cc2017-02-22 16:21:51 +0800214u32 get_lpuart_clk(void)
215{
216 return get_uart_clk();
217}
218
Alison Wang035260a2013-05-27 22:55:42 +0000219unsigned int mxc_get_clock(enum mxc_clock clk)
220{
221 switch (clk) {
222 case MXC_ARM_CLK:
223 return get_mcu_main_clk();
224 case MXC_BUS_CLK:
225 return get_bus_clk();
226 case MXC_IPG_CLK:
227 return get_ipg_clk();
228 case MXC_UART_CLK:
229 return get_uart_clk();
230 case MXC_ESDHC_CLK:
231 return get_sdhc_clk();
232 case MXC_FEC_CLK:
233 return get_fec_clk();
Alison Wang86bef202013-06-17 15:30:38 +0800234 case MXC_I2C_CLK:
235 return get_i2c_clk();
Bhuvanchandra DV860c1802015-06-01 18:37:19 +0530236 case MXC_DSPI_CLK:
237 return get_dspi_clk();
Alison Wang035260a2013-05-27 22:55:42 +0000238 default:
239 break;
240 }
241 return -1;
242}
243
244/* Dump some core clocks */
Simon Glassed38aef2020-05-10 11:40:03 -0600245int do_vf610_showclocks(struct cmd_tbl *cmdtp, int flag, int argc,
246 char *const argv[])
Alison Wang035260a2013-05-27 22:55:42 +0000247{
248 printf("\n");
249 printf("cpu clock : %8d MHz\n", mxc_get_clock(MXC_ARM_CLK) / 1000000);
250 printf("bus clock : %8d MHz\n", mxc_get_clock(MXC_BUS_CLK) / 1000000);
251 printf("ipg clock : %8d MHz\n", mxc_get_clock(MXC_IPG_CLK) / 1000000);
252
253 return 0;
254}
255
256U_BOOT_CMD(
257 clocks, CONFIG_SYS_MAXARGS, 1, do_vf610_showclocks,
258 "display clocks",
259 ""
260);
261
262#ifdef CONFIG_FEC_MXC
Lukasz Majewskib78294d2019-02-13 22:46:43 +0100263__weak void imx_get_mac_from_fuse(int dev_id, unsigned char *mac)
Alison Wang035260a2013-05-27 22:55:42 +0000264{
265 struct ocotp_regs *ocotp = (struct ocotp_regs *)OCOTP_BASE_ADDR;
266 struct fuse_bank *bank = &ocotp->bank[4];
267 struct fuse_bank4_regs *fuse =
268 (struct fuse_bank4_regs *)bank->fuse_regs;
269
270 u32 value = readl(&fuse->mac_addr0);
271 mac[0] = (value >> 8);
272 mac[1] = value;
273
274 value = readl(&fuse->mac_addr1);
275 mac[2] = value >> 24;
276 mac[3] = value >> 16;
277 mac[4] = value >> 8;
278 mac[5] = value;
279}
280#endif
281
Peng Fan0bacb932015-09-01 17:15:03 +0800282u32 get_cpu_rev(void)
283{
284 return MXC_CPU_VF610 << 12;
285}
286
Alison Wang035260a2013-05-27 22:55:42 +0000287#if defined(CONFIG_DISPLAY_CPUINFO)
288static char *get_reset_cause(void)
289{
290 u32 cause;
291 struct src *src_regs = (struct src *)SRC_BASE_ADDR;
292
293 cause = readl(&src_regs->srsr);
294 writel(cause, &src_regs->srsr);
Alison Wang035260a2013-05-27 22:55:42 +0000295
Stefan Agner5b4ca3e2014-11-27 23:58:20 +0100296 if (cause & SRC_SRSR_POR_RST)
297 return "POWER ON RESET";
298 else if (cause & SRC_SRSR_WDOG_A5)
299 return "WDOG A5";
300 else if (cause & SRC_SRSR_WDOG_M4)
301 return "WDOG M4";
302 else if (cause & SRC_SRSR_JTAG_RST)
Alison Wang035260a2013-05-27 22:55:42 +0000303 return "JTAG HIGH-Z";
Stefan Agner5b4ca3e2014-11-27 23:58:20 +0100304 else if (cause & SRC_SRSR_SW_RST)
305 return "SW RESET";
306 else if (cause & SRC_SRSR_RESETB)
Alison Wang035260a2013-05-27 22:55:42 +0000307 return "EXTERNAL RESET";
Stefan Agner5b4ca3e2014-11-27 23:58:20 +0100308 else
Alison Wang035260a2013-05-27 22:55:42 +0000309 return "unknown reset";
Alison Wang035260a2013-05-27 22:55:42 +0000310}
311
312int print_cpuinfo(void)
313{
Sanchayan Maitycb01fb12015-04-15 16:24:24 +0530314 printf("CPU: Freescale Vybrid VF%s at %d MHz\n",
315 soc_type, mxc_get_clock(MXC_ARM_CLK) / 1000000);
Alison Wang035260a2013-05-27 22:55:42 +0000316 printf("Reset cause: %s\n", get_reset_cause());
317
318 return 0;
319}
320#endif
321
Sanchayan Maitycb01fb12015-04-15 16:24:24 +0530322int arch_cpu_init(void)
323{
324 struct mscm *mscm = (struct mscm *)MSCM_BASE_ADDR;
325
326 soc_type[0] = mscm->cpxcount ? '6' : '5'; /*Dual Core => VF6x0 */
327 soc_type[1] = mscm->cpxcfg1 ? '1' : '0'; /* L2 Cache => VFx10 */
328
329 return 0;
330}
331
332#ifdef CONFIG_ARCH_MISC_INIT
333int arch_misc_init(void)
334{
335 char soc[6];
336
Stefan Agner71782cf2016-09-28 11:29:28 +0200337 strcpy(soc, "vf");
Sanchayan Maitycb01fb12015-04-15 16:24:24 +0530338 strcat(soc, soc_type);
Simon Glass6a38e412017-08-03 12:22:09 -0600339 env_set("soc", soc);
Sanchayan Maitycb01fb12015-04-15 16:24:24 +0530340
341 return 0;
342}
343#endif
344
Masahiro Yamadaf7ed78b2020-06-26 15:13:33 +0900345int cpu_eth_init(struct bd_info *bis)
Alison Wang035260a2013-05-27 22:55:42 +0000346{
347 int rc = -ENODEV;
348
349#if defined(CONFIG_FEC_MXC)
350 rc = fecmxc_initialize(bis);
351#endif
352
353 return rc;
354}
355
Yangbo Lu73340382019-06-21 11:42:28 +0800356#ifdef CONFIG_FSL_ESDHC_IMX
Masahiro Yamadaf7ed78b2020-06-26 15:13:33 +0900357int cpu_mmc_init(struct bd_info *bis)
Alison Wang035260a2013-05-27 22:55:42 +0000358{
359 return fsl_esdhc_mmc_init(bis);
360}
361#endif
362
363int get_clocks(void)
364{
Yangbo Lu73340382019-06-21 11:42:28 +0800365#ifdef CONFIG_FSL_ESDHC_IMX
Alison Wang035260a2013-05-27 22:55:42 +0000366 gd->arch.sdhc_clk = mxc_get_clock(MXC_ESDHC_CLK);
367#endif
368 return 0;
369}
Stefan Agner208aaa22015-04-15 16:24:25 +0530370
Trevor Woerner43ec7e02019-05-03 09:41:00 -0400371#if !CONFIG_IS_ENABLED(SYS_DCACHE_OFF)
Stefan Agner208aaa22015-04-15 16:24:25 +0530372void enable_caches(void)
373{
374#if defined(CONFIG_SYS_ARM_CACHE_WRITETHROUGH)
375 enum dcache_option option = DCACHE_WRITETHROUGH;
376#else
377 enum dcache_option option = DCACHE_WRITEBACK;
378#endif
379 dcache_enable();
380 icache_enable();
381
382 /* Enable caching on OCRAM */
383 mmu_set_region_dcache_behaviour(IRAM_BASE_ADDR, IRAM_SIZE, option);
384}
385#endif
Lukasz Majewski821df472019-02-13 22:46:41 +0100386
387#ifdef CONFIG_SYS_I2C_MXC
388/* i2c_num can be from 0 - 3 */
389int enable_i2c_clk(unsigned char enable, unsigned int i2c_num)
390{
391 struct ccm_reg *ccm = (struct ccm_reg *)CCM_BASE_ADDR;
392
393 switch (i2c_num) {
394 case 0:
395 clrsetbits_le32(&ccm->ccgr4, CCM_CCGR4_I2C0_CTRL_MASK,
396 CCM_CCGR4_I2C0_CTRL_MASK);
397 case 2:
398 clrsetbits_le32(&ccm->ccgr10, CCM_CCGR10_I2C2_CTRL_MASK,
399 CCM_CCGR10_I2C2_CTRL_MASK);
400 break;
401 default:
402 return -EINVAL;
403 }
404
405 return 0;
406}
407#endif