xref: /openbmc/u-boot/arch/arm/cpu/armv7/vf610/generic.c (revision 4519668b)
1 /*
2  * Copyright 2013 Freescale Semiconductor, Inc.
3  *
4  * SPDX-License-Identifier:	GPL-2.0+
5  */
6 
7 #include <common.h>
8 #include <asm/io.h>
9 #include <asm/arch/imx-regs.h>
10 #include <asm/arch/clock.h>
11 #include <asm/arch/crm_regs.h>
12 #include <netdev.h>
13 #ifdef CONFIG_FSL_ESDHC
14 #include <fsl_esdhc.h>
15 #endif
16 
17 #ifdef CONFIG_FSL_ESDHC
18 DECLARE_GLOBAL_DATA_PTR;
19 #endif
20 
21 static char soc_type[] = "xx0";
22 
23 #ifdef CONFIG_MXC_OCOTP
24 void enable_ocotp_clk(unsigned char enable)
25 {
26 	struct ccm_reg *ccm = (struct ccm_reg *)CCM_BASE_ADDR;
27 	u32 reg;
28 
29 	reg = readl(&ccm->ccgr6);
30 	if (enable)
31 		reg |= CCM_CCGR6_OCOTP_CTRL_MASK;
32 	else
33 		reg &= ~CCM_CCGR6_OCOTP_CTRL_MASK;
34 	writel(reg, &ccm->ccgr6);
35 }
36 #endif
37 
38 static u32 get_mcu_main_clk(void)
39 {
40 	struct ccm_reg *ccm = (struct ccm_reg *)CCM_BASE_ADDR;
41 	u32 ccm_ccsr, ccm_cacrr, armclk_div;
42 	u32 sysclk_sel, pll_pfd_sel = 0;
43 	u32 freq = 0;
44 
45 	ccm_ccsr = readl(&ccm->ccsr);
46 	sysclk_sel = ccm_ccsr & CCM_CCSR_SYS_CLK_SEL_MASK;
47 	sysclk_sel >>= CCM_CCSR_SYS_CLK_SEL_OFFSET;
48 
49 	ccm_cacrr = readl(&ccm->cacrr);
50 	armclk_div = ccm_cacrr & CCM_CACRR_ARM_CLK_DIV_MASK;
51 	armclk_div >>= CCM_CACRR_ARM_CLK_DIV_OFFSET;
52 	armclk_div += 1;
53 
54 	switch (sysclk_sel) {
55 	case 0:
56 		freq = FASE_CLK_FREQ;
57 		break;
58 	case 1:
59 		freq = SLOW_CLK_FREQ;
60 		break;
61 	case 2:
62 		pll_pfd_sel = ccm_ccsr & CCM_CCSR_PLL2_PFD_CLK_SEL_MASK;
63 		pll_pfd_sel >>= CCM_CCSR_PLL2_PFD_CLK_SEL_OFFSET;
64 		if (pll_pfd_sel == 0)
65 			freq = PLL2_MAIN_FREQ;
66 		else if (pll_pfd_sel == 1)
67 			freq = PLL2_PFD1_FREQ;
68 		else if (pll_pfd_sel == 2)
69 			freq = PLL2_PFD2_FREQ;
70 		else if (pll_pfd_sel == 3)
71 			freq = PLL2_PFD3_FREQ;
72 		else if (pll_pfd_sel == 4)
73 			freq = PLL2_PFD4_FREQ;
74 		break;
75 	case 3:
76 		freq = PLL2_MAIN_FREQ;
77 		break;
78 	case 4:
79 		pll_pfd_sel = ccm_ccsr & CCM_CCSR_PLL1_PFD_CLK_SEL_MASK;
80 		pll_pfd_sel >>= CCM_CCSR_PLL1_PFD_CLK_SEL_OFFSET;
81 		if (pll_pfd_sel == 0)
82 			freq = PLL1_MAIN_FREQ;
83 		else if (pll_pfd_sel == 1)
84 			freq = PLL1_PFD1_FREQ;
85 		else if (pll_pfd_sel == 2)
86 			freq = PLL1_PFD2_FREQ;
87 		else if (pll_pfd_sel == 3)
88 			freq = PLL1_PFD3_FREQ;
89 		else if (pll_pfd_sel == 4)
90 			freq = PLL1_PFD4_FREQ;
91 		break;
92 	case 5:
93 		freq = PLL3_MAIN_FREQ;
94 		break;
95 	default:
96 		printf("unsupported system clock select\n");
97 	}
98 
99 	return freq / armclk_div;
100 }
101 
102 static u32 get_bus_clk(void)
103 {
104 	struct ccm_reg *ccm = (struct ccm_reg *)CCM_BASE_ADDR;
105 	u32 ccm_cacrr, busclk_div;
106 
107 	ccm_cacrr = readl(&ccm->cacrr);
108 
109 	busclk_div = ccm_cacrr & CCM_CACRR_BUS_CLK_DIV_MASK;
110 	busclk_div >>= CCM_CACRR_BUS_CLK_DIV_OFFSET;
111 	busclk_div += 1;
112 
113 	return get_mcu_main_clk() / busclk_div;
114 }
115 
116 static u32 get_ipg_clk(void)
117 {
118 	struct ccm_reg *ccm = (struct ccm_reg *)CCM_BASE_ADDR;
119 	u32 ccm_cacrr, ipgclk_div;
120 
121 	ccm_cacrr = readl(&ccm->cacrr);
122 
123 	ipgclk_div = ccm_cacrr & CCM_CACRR_IPG_CLK_DIV_MASK;
124 	ipgclk_div >>= CCM_CACRR_IPG_CLK_DIV_OFFSET;
125 	ipgclk_div += 1;
126 
127 	return get_bus_clk() / ipgclk_div;
128 }
129 
130 static u32 get_uart_clk(void)
131 {
132 	return get_ipg_clk();
133 }
134 
135 static u32 get_sdhc_clk(void)
136 {
137 	struct ccm_reg *ccm = (struct ccm_reg *)CCM_BASE_ADDR;
138 	u32 ccm_cscmr1, ccm_cscdr2, sdhc_clk_sel, sdhc_clk_div;
139 	u32 freq = 0;
140 
141 	ccm_cscmr1 = readl(&ccm->cscmr1);
142 	sdhc_clk_sel = ccm_cscmr1 & CCM_CSCMR1_ESDHC1_CLK_SEL_MASK;
143 	sdhc_clk_sel >>= CCM_CSCMR1_ESDHC1_CLK_SEL_OFFSET;
144 
145 	ccm_cscdr2 = readl(&ccm->cscdr2);
146 	sdhc_clk_div = ccm_cscdr2 & CCM_CSCDR2_ESDHC1_CLK_DIV_MASK;
147 	sdhc_clk_div >>= CCM_CSCDR2_ESDHC1_CLK_DIV_OFFSET;
148 	sdhc_clk_div += 1;
149 
150 	switch (sdhc_clk_sel) {
151 	case 0:
152 		freq = PLL3_MAIN_FREQ;
153 		break;
154 	case 1:
155 		freq = PLL3_PFD3_FREQ;
156 		break;
157 	case 2:
158 		freq = PLL1_PFD3_FREQ;
159 		break;
160 	case 3:
161 		freq = get_bus_clk();
162 		break;
163 	}
164 
165 	return freq / sdhc_clk_div;
166 }
167 
168 u32 get_fec_clk(void)
169 {
170 	struct ccm_reg *ccm = (struct ccm_reg *)CCM_BASE_ADDR;
171 	u32 ccm_cscmr2, rmii_clk_sel;
172 	u32 freq = 0;
173 
174 	ccm_cscmr2 = readl(&ccm->cscmr2);
175 	rmii_clk_sel = ccm_cscmr2 & CCM_CSCMR2_RMII_CLK_SEL_MASK;
176 	rmii_clk_sel >>= CCM_CSCMR2_RMII_CLK_SEL_OFFSET;
177 
178 	switch (rmii_clk_sel) {
179 	case 0:
180 		freq = ENET_EXTERNAL_CLK;
181 		break;
182 	case 1:
183 		freq = AUDIO_EXTERNAL_CLK;
184 		break;
185 	case 2:
186 		freq = PLL5_MAIN_FREQ;
187 		break;
188 	case 3:
189 		freq = PLL5_MAIN_FREQ / 2;
190 		break;
191 	}
192 
193 	return freq;
194 }
195 
196 static u32 get_i2c_clk(void)
197 {
198 	return get_ipg_clk();
199 }
200 
201 static u32 get_dspi_clk(void)
202 {
203 	return get_ipg_clk();
204 }
205 
206 unsigned int mxc_get_clock(enum mxc_clock clk)
207 {
208 	switch (clk) {
209 	case MXC_ARM_CLK:
210 		return get_mcu_main_clk();
211 	case MXC_BUS_CLK:
212 		return get_bus_clk();
213 	case MXC_IPG_CLK:
214 		return get_ipg_clk();
215 	case MXC_UART_CLK:
216 		return get_uart_clk();
217 	case MXC_ESDHC_CLK:
218 		return get_sdhc_clk();
219 	case MXC_FEC_CLK:
220 		return get_fec_clk();
221 	case MXC_I2C_CLK:
222 		return get_i2c_clk();
223 	case MXC_DSPI_CLK:
224 		return get_dspi_clk();
225 	default:
226 		break;
227 	}
228 	return -1;
229 }
230 
231 /* Dump some core clocks */
232 int do_vf610_showclocks(cmd_tbl_t *cmdtp, int flag, int argc,
233 			 char * const argv[])
234 {
235 	printf("\n");
236 	printf("cpu clock : %8d MHz\n", mxc_get_clock(MXC_ARM_CLK) / 1000000);
237 	printf("bus clock : %8d MHz\n", mxc_get_clock(MXC_BUS_CLK) / 1000000);
238 	printf("ipg clock : %8d MHz\n", mxc_get_clock(MXC_IPG_CLK) / 1000000);
239 
240 	return 0;
241 }
242 
243 U_BOOT_CMD(
244 	clocks, CONFIG_SYS_MAXARGS, 1, do_vf610_showclocks,
245 	"display clocks",
246 	""
247 );
248 
249 #ifdef CONFIG_FEC_MXC
250 void imx_get_mac_from_fuse(int dev_id, unsigned char *mac)
251 {
252 	struct ocotp_regs *ocotp = (struct ocotp_regs *)OCOTP_BASE_ADDR;
253 	struct fuse_bank *bank = &ocotp->bank[4];
254 	struct fuse_bank4_regs *fuse =
255 		(struct fuse_bank4_regs *)bank->fuse_regs;
256 
257 	u32 value = readl(&fuse->mac_addr0);
258 	mac[0] = (value >> 8);
259 	mac[1] = value;
260 
261 	value = readl(&fuse->mac_addr1);
262 	mac[2] = value >> 24;
263 	mac[3] = value >> 16;
264 	mac[4] = value >> 8;
265 	mac[5] = value;
266 }
267 #endif
268 
269 #if defined(CONFIG_DISPLAY_CPUINFO)
270 static char *get_reset_cause(void)
271 {
272 	u32 cause;
273 	struct src *src_regs = (struct src *)SRC_BASE_ADDR;
274 
275 	cause = readl(&src_regs->srsr);
276 	writel(cause, &src_regs->srsr);
277 
278 	if (cause & SRC_SRSR_POR_RST)
279 		return "POWER ON RESET";
280 	else if (cause & SRC_SRSR_WDOG_A5)
281 		return "WDOG A5";
282 	else if (cause & SRC_SRSR_WDOG_M4)
283 		return "WDOG M4";
284 	else if (cause & SRC_SRSR_JTAG_RST)
285 		return "JTAG HIGH-Z";
286 	else if (cause & SRC_SRSR_SW_RST)
287 		return "SW RESET";
288 	else if (cause & SRC_SRSR_RESETB)
289 		return "EXTERNAL RESET";
290 	else
291 		return "unknown reset";
292 }
293 
294 int print_cpuinfo(void)
295 {
296 	printf("CPU: Freescale Vybrid VF%s at %d MHz\n",
297 	       soc_type, mxc_get_clock(MXC_ARM_CLK) / 1000000);
298 	printf("Reset cause: %s\n", get_reset_cause());
299 
300 	return 0;
301 }
302 #endif
303 
304 int arch_cpu_init(void)
305 {
306 	struct mscm *mscm = (struct mscm *)MSCM_BASE_ADDR;
307 
308 	soc_type[0] = mscm->cpxcount ? '6' : '5'; /*Dual Core => VF6x0 */
309 	soc_type[1] = mscm->cpxcfg1 ? '1' : '0'; /* L2 Cache => VFx10 */
310 
311 	return 0;
312 }
313 
314 #ifdef CONFIG_ARCH_MISC_INIT
315 int arch_misc_init(void)
316 {
317 	char soc[6];
318 
319 	strcat(soc, "vf");
320 	strcat(soc, soc_type);
321 	setenv("soc", soc);
322 
323 	return 0;
324 }
325 #endif
326 
327 int cpu_eth_init(bd_t *bis)
328 {
329 	int rc = -ENODEV;
330 
331 #if defined(CONFIG_FEC_MXC)
332 	rc = fecmxc_initialize(bis);
333 #endif
334 
335 	return rc;
336 }
337 
338 #ifdef CONFIG_FSL_ESDHC
339 int cpu_mmc_init(bd_t *bis)
340 {
341 	return fsl_esdhc_mmc_init(bis);
342 }
343 #endif
344 
345 int get_clocks(void)
346 {
347 #ifdef CONFIG_FSL_ESDHC
348 	gd->arch.sdhc_clk = mxc_get_clock(MXC_ESDHC_CLK);
349 #endif
350 	return 0;
351 }
352 
353 #ifndef CONFIG_SYS_DCACHE_OFF
354 void enable_caches(void)
355 {
356 #if defined(CONFIG_SYS_ARM_CACHE_WRITETHROUGH)
357 	enum dcache_option option = DCACHE_WRITETHROUGH;
358 #else
359 	enum dcache_option option = DCACHE_WRITEBACK;
360 #endif
361 	dcache_enable();
362 	icache_enable();
363 
364     /* Enable caching on OCRAM */
365 	mmu_set_region_dcache_behaviour(IRAM_BASE_ADDR, IRAM_SIZE, option);
366 }
367 #endif
368