blob: 74a2dcbc116a909a2816a90155ab1764b5132093 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Wang Huan8ce6bec2014-09-05 13:52:34 +08002/*
3 * Copyright 2014 Freescale Semiconductor, Inc.
Gaurav Jain476c6392022-03-24 11:50:35 +05304 * Copyright 2021 NXP
Wang Huan8ce6bec2014-09-05 13:52:34 +08005 */
6
Simon Glass370382c2019-11-14 12:57:35 -07007#include <cpu_func.h>
Simon Glass97589732020-05-10 11:40:02 -06008#include <init.h>
Simon Glass274e0b02020-05-10 11:39:56 -06009#include <net.h>
Simon Glassf5c208d2019-11-14 12:57:20 -070010#include <vsprintf.h>
Wang Huan8ce6bec2014-09-05 13:52:34 +080011#include <asm/arch/clock.h>
Simon Glass3ba929a2020-10-30 21:38:53 -060012#include <asm/global_data.h>
Wang Huan8ce6bec2014-09-05 13:52:34 +080013#include <asm/io.h>
14#include <asm/arch/immap_ls102xa.h>
Minghuan Lian6c9afed2015-01-21 17:29:17 +080015#include <asm/cache.h>
16#include <asm/system.h>
Wang Huan8ce6bec2014-09-05 13:52:34 +080017#include <tsec.h>
18#include <netdev.h>
19#include <fsl_esdhc.h>
Fabio Estevam0af008c2015-10-03 14:21:00 -030020#include <config.h>
21#include <fsl_wdog.h>
Simon Glassdbd79542020-05-10 11:40:11 -060022#include <linux/delay.h>
Gaurav Jain476c6392022-03-24 11:50:35 +053023#include <dm.h>
Wang Huan8ce6bec2014-09-05 13:52:34 +080024
chenhui zhao0c789872014-10-22 18:20:22 +080025#include "fsl_epu.h"
26
chenhui zhao9a378cb2015-01-23 15:53:53 +080027#define DCSR_RCPM2_BLOCK_OFFSET 0x223000
28#define DCSR_RCPM2_CPMFSMCR0 0x400
29#define DCSR_RCPM2_CPMFSMSR0 0x404
30#define DCSR_RCPM2_CPMFSMCR1 0x414
31#define DCSR_RCPM2_CPMFSMSR1 0x418
32#define CPMFSMSR_FSM_STATE_MASK 0x7f
33
Wang Huan8ce6bec2014-09-05 13:52:34 +080034DECLARE_GLOBAL_DATA_PTR;
35
Trevor Woerner43ec7e02019-05-03 09:41:00 -040036#if !CONFIG_IS_ENABLED(SYS_DCACHE_OFF)
Minghuan Lian6c9afed2015-01-21 17:29:17 +080037
38/*
39 * Bit[1] of the descriptor indicates the descriptor type,
40 * and bit[0] indicates whether the descriptor is valid.
41 */
42#define PMD_TYPE_TABLE 0x3
43#define PMD_TYPE_SECT 0x1
44
45/* AttrIndx[2:0] */
46#define PMD_ATTRINDX(t) ((t) << 2)
47
48/* Section */
49#define PMD_SECT_AF (1 << 10)
50
51#define BLOCK_SIZE_L1 (1UL << 30)
52#define BLOCK_SIZE_L2 (1UL << 21)
53
54/* TTBCR flags */
55#define TTBCR_EAE (1 << 31)
56#define TTBCR_T0SZ(x) ((x) << 0)
57#define TTBCR_T1SZ(x) ((x) << 16)
58#define TTBCR_USING_TTBR0 (TTBCR_T0SZ(0) | TTBCR_T1SZ(0))
59#define TTBCR_IRGN0_NC (0 << 8)
60#define TTBCR_IRGN0_WBWA (1 << 8)
61#define TTBCR_IRGN0_WT (2 << 8)
62#define TTBCR_IRGN0_WBNWA (3 << 8)
63#define TTBCR_IRGN0_MASK (3 << 8)
64#define TTBCR_ORGN0_NC (0 << 10)
65#define TTBCR_ORGN0_WBWA (1 << 10)
66#define TTBCR_ORGN0_WT (2 << 10)
67#define TTBCR_ORGN0_WBNWA (3 << 10)
68#define TTBCR_ORGN0_MASK (3 << 10)
69#define TTBCR_SHARED_NON (0 << 12)
70#define TTBCR_SHARED_OUTER (2 << 12)
71#define TTBCR_SHARED_INNER (3 << 12)
72#define TTBCR_EPD0 (0 << 7)
73#define TTBCR (TTBCR_SHARED_NON | \
74 TTBCR_ORGN0_NC | \
75 TTBCR_IRGN0_NC | \
76 TTBCR_USING_TTBR0 | \
77 TTBCR_EAE)
78
79/*
80 * Memory region attributes for LPAE (defined in pgtable):
81 *
82 * n = AttrIndx[2:0]
83 *
84 * n MAIR
85 * UNCACHED 000 00000000
86 * BUFFERABLE 001 01000100
87 * DEV_WC 001 01000100
88 * WRITETHROUGH 010 10101010
89 * WRITEBACK 011 11101110
90 * DEV_CACHED 011 11101110
91 * DEV_SHARED 100 00000100
92 * DEV_NONSHARED 100 00000100
93 * unused 101
94 * unused 110
95 * WRITEALLOC 111 11111111
96 */
97#define MT_MAIR0 0xeeaa4400
98#define MT_MAIR1 0xff000004
99#define MT_STRONLY_ORDER 0
100#define MT_NORMAL_NC 1
101#define MT_DEVICE_MEM 4
102#define MT_NORMAL 7
103
104/* The phy_addr must be aligned to 4KB */
105static inline void set_pgtable(u32 *page_table, u32 index, u32 phy_addr)
106{
107 u32 value = phy_addr | PMD_TYPE_TABLE;
108
109 page_table[2 * index] = value;
110 page_table[2 * index + 1] = 0;
111}
112
113/* The phy_addr must be aligned to 4KB */
114static inline void set_pgsection(u32 *page_table, u32 index, u64 phy_addr,
115 u32 memory_type)
116{
117 u64 value;
118
119 value = phy_addr | PMD_TYPE_SECT | PMD_SECT_AF;
120 value |= PMD_ATTRINDX(memory_type);
121 page_table[2 * index] = value & 0xFFFFFFFF;
122 page_table[2 * index + 1] = (value >> 32) & 0xFFFFFFFF;
123}
124
125/*
126 * Start MMU after DDR is available, we create MMU table in DRAM.
127 * The base address of TTLB is gd->arch.tlb_addr. We use two
128 * levels of translation tables here to cover 40-bit address space.
129 *
130 * The TTLBs are located at PHY 2G~4G.
131 *
132 * VA mapping:
133 *
134 * ------- <---- 0GB
135 * | |
136 * | |
137 * |-------| <---- 0x24000000
138 * |///////| ===> 192MB VA map for PCIe1 with offset 0x40_0000_0000
139 * |-------| <---- 0x300000000
140 * | |
141 * |-------| <---- 0x34000000
142 * |///////| ===> 192MB VA map for PCIe2 with offset 0x48_0000_0000
143 * |-------| <---- 0x40000000
144 * | |
145 * |-------| <---- 0x80000000 DDR0 space start
146 * |\\\\\\\|
147 *.|\\\\\\\| ===> 2GB VA map for 2GB DDR0 Memory space
148 * |\\\\\\\|
149 * ------- <---- 4GB DDR0 space end
150 */
151static void mmu_setup(void)
152{
153 u32 *level0_table = (u32 *)gd->arch.tlb_addr;
154 u32 *level1_table = (u32 *)(gd->arch.tlb_addr + 0x1000);
155 u64 va_start = 0;
156 u32 reg;
157 int i;
158
159 /* Level 0 Table 2-3 are used to map DDR */
160 set_pgsection(level0_table, 3, 3 * BLOCK_SIZE_L1, MT_NORMAL);
161 set_pgsection(level0_table, 2, 2 * BLOCK_SIZE_L1, MT_NORMAL);
162 /* Level 0 Table 1 is used to map device */
163 set_pgsection(level0_table, 1, 1 * BLOCK_SIZE_L1, MT_DEVICE_MEM);
164 /* Level 0 Table 0 is used to map device including PCIe MEM */
165 set_pgtable(level0_table, 0, (u32)level1_table);
166
167 /* Level 1 has 512 entries */
168 for (i = 0; i < 512; i++) {
169 /* Mapping for PCIe 1 */
Tom Rini56af6592022-11-16 13:10:33 -0500170 if (va_start >= CFG_SYS_PCIE1_VIRT_ADDR &&
171 va_start < (CFG_SYS_PCIE1_VIRT_ADDR +
172 CFG_SYS_PCIE_MMAP_SIZE))
Minghuan Lian6c9afed2015-01-21 17:29:17 +0800173 set_pgsection(level1_table, i,
Tom Rini56af6592022-11-16 13:10:33 -0500174 CFG_SYS_PCIE1_PHYS_BASE + va_start,
Minghuan Lian6c9afed2015-01-21 17:29:17 +0800175 MT_DEVICE_MEM);
176 /* Mapping for PCIe 2 */
Tom Rini56af6592022-11-16 13:10:33 -0500177 else if (va_start >= CFG_SYS_PCIE2_VIRT_ADDR &&
178 va_start < (CFG_SYS_PCIE2_VIRT_ADDR +
179 CFG_SYS_PCIE_MMAP_SIZE))
Minghuan Lian6c9afed2015-01-21 17:29:17 +0800180 set_pgsection(level1_table, i,
Tom Rini56af6592022-11-16 13:10:33 -0500181 CFG_SYS_PCIE2_PHYS_BASE + va_start,
Minghuan Lian6c9afed2015-01-21 17:29:17 +0800182 MT_DEVICE_MEM);
183 else
184 set_pgsection(level1_table, i,
185 va_start,
186 MT_DEVICE_MEM);
187 va_start += BLOCK_SIZE_L2;
188 }
189
190 asm volatile("dsb sy;isb");
191 asm volatile("mcr p15, 0, %0, c2, c0, 2" /* Write RT to TTBCR */
192 : : "r" (TTBCR) : "memory");
193 asm volatile("mcrr p15, 0, %0, %1, c2" /* TTBR 0 */
194 : : "r" ((u32)level0_table), "r" (0) : "memory");
195 asm volatile("mcr p15, 0, %0, c10, c2, 0" /* write MAIR 0 */
196 : : "r" (MT_MAIR0) : "memory");
197 asm volatile("mcr p15, 0, %0, c10, c2, 1" /* write MAIR 1 */
198 : : "r" (MT_MAIR1) : "memory");
199
200 /* Set the access control to all-supervisor */
201 asm volatile("mcr p15, 0, %0, c3, c0, 0"
202 : : "r" (~0));
203
204 /* Enable the mmu */
205 reg = get_cr();
206 set_cr(reg | CR_M);
207}
208
209/*
210 * This function is called from lib/board.c. It recreates MMU
211 * table in main memory. MMU and i/d-cache are enabled here.
212 */
213void enable_caches(void)
214{
215 /* Invalidate all TLB */
216 mmu_page_table_flush(gd->arch.tlb_addr,
217 gd->arch.tlb_addr + gd->arch.tlb_size);
218 /* Set up and enable mmu */
219 mmu_setup();
220
221 /* Invalidate & Enable d-cache */
222 invalidate_dcache_all();
223 set_cr(get_cr() | CR_C);
224}
Trevor Woerner43ec7e02019-05-03 09:41:00 -0400225#endif /* #if !CONFIG_IS_ENABLED(SYS_DCACHE_OFF) */
Minghuan Lian6c9afed2015-01-21 17:29:17 +0800226
Shengzhou Liubf5aee92015-11-20 15:52:02 +0800227
228uint get_svr(void)
229{
Tom Rini376b88a2022-10-28 20:27:13 -0400230 struct ccsr_gur __iomem *gur = (void *)(CFG_SYS_FSL_GUTS_ADDR);
Shengzhou Liubf5aee92015-11-20 15:52:02 +0800231
232 return in_be32(&gur->svr);
233}
234
Wang Huan8ce6bec2014-09-05 13:52:34 +0800235#if defined(CONFIG_DISPLAY_CPUINFO)
236int print_cpuinfo(void)
237{
238 char buf1[32], buf2[32];
Tom Rini376b88a2022-10-28 20:27:13 -0400239 struct ccsr_gur __iomem *gur = (void *)(CFG_SYS_FSL_GUTS_ADDR);
Wang Huan8ce6bec2014-09-05 13:52:34 +0800240 unsigned int svr, major, minor, ver, i;
241
242 svr = in_be32(&gur->svr);
243 major = SVR_MAJ(svr);
244 minor = SVR_MIN(svr);
245
246 puts("CPU: Freescale LayerScape ");
247
248 ver = SVR_SOC_VER(svr);
249 switch (ver) {
250 case SOC_VER_SLS1020:
251 puts("SLS1020");
252 break;
253 case SOC_VER_LS1020:
254 puts("LS1020");
255 break;
256 case SOC_VER_LS1021:
257 puts("LS1021");
258 break;
259 case SOC_VER_LS1022:
260 puts("LS1022");
261 break;
262 default:
263 puts("Unknown");
264 break;
265 }
266
267 if (IS_E_PROCESSOR(svr) && (ver != SOC_VER_SLS1020))
268 puts("E");
269
270 printf(", Version: %d.%d, (0x%08x)\n", major, minor, svr);
271
272 puts("Clock Configuration:");
273
274 printf("\n CPU0(ARMV7):%-4s MHz, ", strmhz(buf1, gd->cpu_clk));
275 printf("\n Bus:%-4s MHz, ", strmhz(buf1, gd->bus_clk));
276 printf("DDR:%-4s MHz (%s MT/s data rate), ",
277 strmhz(buf1, gd->mem_clk/2), strmhz(buf2, gd->mem_clk));
278 puts("\n");
279
280 /* Display the RCW, so that no one gets confused as to what RCW
281 * we're actually using for this boot.
282 */
283 puts("Reset Configuration Word (RCW):");
284 for (i = 0; i < ARRAY_SIZE(gur->rcwsr); i++) {
285 u32 rcw = in_be32(&gur->rcwsr[i]);
286
287 if ((i % 4) == 0)
288 printf("\n %08x:", i * 4);
289 printf(" %08x", rcw);
290 }
291 puts("\n");
292
293 return 0;
294}
295#endif
296
Wang Huan8ce6bec2014-09-05 13:52:34 +0800297#ifdef CONFIG_FSL_ESDHC
Masahiro Yamadaf7ed78b2020-06-26 15:13:33 +0900298int cpu_mmc_init(struct bd_info *bis)
Wang Huan8ce6bec2014-09-05 13:52:34 +0800299{
300 return fsl_esdhc_mmc_init(bis);
301}
302#endif
303
chenhui zhao0c789872014-10-22 18:20:22 +0800304int arch_cpu_init(void)
305{
Tom Rini6a5dccc2022-11-16 13:10:41 -0500306 void *epu_base = (void *)(CFG_SYS_DCSRBAR + EPU_BLOCK_OFFSET);
chenhui zhao9a378cb2015-01-23 15:53:53 +0800307 void *rcpm2_base =
Tom Rini6a5dccc2022-11-16 13:10:41 -0500308 (void *)(CFG_SYS_DCSRBAR + DCSR_RCPM2_BLOCK_OFFSET);
Tom Rini376b88a2022-10-28 20:27:13 -0400309 struct ccsr_scfg *scfg = (void *)CFG_SYS_FSL_SCFG_ADDR;
chenhui zhao9a378cb2015-01-23 15:53:53 +0800310 u32 state;
311
Hou Zhiqiang49713762021-02-05 15:45:12 +0800312 icache_enable();
313
chenhui zhao9a378cb2015-01-23 15:53:53 +0800314 /*
315 * The RCPM FSM state may not be reset after power-on.
316 * So, reset them.
317 */
318 state = in_be32(rcpm2_base + DCSR_RCPM2_CPMFSMSR0) &
319 CPMFSMSR_FSM_STATE_MASK;
320 if (state != 0) {
321 out_be32(rcpm2_base + DCSR_RCPM2_CPMFSMCR0, 0x80);
322 out_be32(rcpm2_base + DCSR_RCPM2_CPMFSMCR0, 0x0);
323 }
324
325 state = in_be32(rcpm2_base + DCSR_RCPM2_CPMFSMSR1) &
326 CPMFSMSR_FSM_STATE_MASK;
327 if (state != 0) {
328 out_be32(rcpm2_base + DCSR_RCPM2_CPMFSMCR1, 0x80);
329 out_be32(rcpm2_base + DCSR_RCPM2_CPMFSMCR1, 0x0);
330 }
chenhui zhao0c789872014-10-22 18:20:22 +0800331
332 /*
333 * After wakeup from deep sleep, Clear EPU registers
334 * as early as possible to prevent from possible issue.
335 * It's also safe to clear at normal boot.
336 */
337 fsl_epu_clean(epu_base);
338
horia.geanta@freescale.comcc0619c2015-10-15 14:21:31 +0300339 setbits_be32(&scfg->snpcnfgcr, SCFG_SNPCNFGCR_SEC_RD_WR);
340
chenhui zhao0c789872014-10-22 18:20:22 +0800341 return 0;
342}
Xiubo Lib73446d2014-11-21 17:40:56 +0800343
Jan Kiszkaac31b5a2015-04-21 07:18:24 +0200344#ifdef CONFIG_ARMV7_NONSEC
Xiubo Lib73446d2014-11-21 17:40:56 +0800345/* Set the address at which the secondary core starts from.*/
346void smp_set_core_boot_addr(unsigned long addr, int corenr)
347{
Tom Rini376b88a2022-10-28 20:27:13 -0400348 struct ccsr_gur __iomem *gur = (void *)(CFG_SYS_FSL_GUTS_ADDR);
Xiubo Lib73446d2014-11-21 17:40:56 +0800349
350 out_be32(&gur->scratchrw[0], addr);
351}
352
353/* Release the secondary core from holdoff state and kick it */
354void smp_kick_all_cpus(void)
355{
Tom Rini376b88a2022-10-28 20:27:13 -0400356 struct ccsr_gur __iomem *gur = (void *)(CFG_SYS_FSL_GUTS_ADDR);
Xiubo Lib73446d2014-11-21 17:40:56 +0800357
358 out_be32(&gur->brrl, 0x2);
Wang Dongsheng0b5034c2015-06-18 18:32:58 +0800359
360 /*
361 * LS1 STANDBYWFE is not captured outside the ARM module in the soc.
362 * So add a delay to wait bootrom execute WFE.
363 */
364 udelay(1);
365
366 asm volatile("sev");
Xiubo Lib73446d2014-11-21 17:40:56 +0800367}
368#endif
Fabio Estevam0af008c2015-10-03 14:21:00 -0300369
Harald Seiler6f14d5f2020-12-15 16:47:52 +0100370void reset_cpu(void)
Fabio Estevam0af008c2015-10-03 14:21:00 -0300371{
372 struct watchdog_regs *wdog = (struct watchdog_regs *)WDOG1_BASE_ADDR;
373
374 clrbits_be16(&wdog->wcr, WCR_SRS);
375
376 while (1) {
377 /*
378 * Let the watchdog trigger
379 */
380 }
381}
Alison Wangd285ef72015-08-04 09:55:37 +0800382
383void arch_preboot_os(void)
384{
385 unsigned long ctrl;
386
387 /* Disable PL1 Physical Timer */
388 asm("mrc p15, 0, %0, c14, c2, 1" : "=r" (ctrl));
389 ctrl &= ~ARCH_TIMER_CTRL_ENABLE;
390 asm("mcr p15, 0, %0, c14, c2, 1" : : "r" (ctrl));
391}
Gaurav Jain476c6392022-03-24 11:50:35 +0530392
393#ifdef CONFIG_ARCH_MISC_INIT
394int arch_misc_init(void)
395{
396 if (IS_ENABLED(CONFIG_FSL_CAAM)) {
397 struct udevice *dev;
398 int ret;
399
400 ret = uclass_get_device_by_driver(UCLASS_MISC, DM_DRIVER_GET(caam_jr), &dev);
401 if (ret)
Ye Liec346892022-05-11 13:56:20 +0530402 printf("Failed to initialize caam_jr: %d\n", ret);
Gaurav Jain476c6392022-03-24 11:50:35 +0530403 }
404
405 return 0;
406}
407#endif