blob: 0b3e3b206411dfa0e1cb6d7fba99c55a8cd66cc8 [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
7#include <common.h>
Simon Glass370382c2019-11-14 12:57:35 -07008#include <cpu_func.h>
Simon Glass97589732020-05-10 11:40:02 -06009#include <init.h>
Simon Glass274e0b02020-05-10 11:39:56 -060010#include <net.h>
Simon Glassf5c208d2019-11-14 12:57:20 -070011#include <vsprintf.h>
Wang Huan8ce6bec2014-09-05 13:52:34 +080012#include <asm/arch/clock.h>
Simon Glass3ba929a2020-10-30 21:38:53 -060013#include <asm/global_data.h>
Wang Huan8ce6bec2014-09-05 13:52:34 +080014#include <asm/io.h>
15#include <asm/arch/immap_ls102xa.h>
Minghuan Lian6c9afed2015-01-21 17:29:17 +080016#include <asm/cache.h>
17#include <asm/system.h>
Wang Huan8ce6bec2014-09-05 13:52:34 +080018#include <tsec.h>
19#include <netdev.h>
20#include <fsl_esdhc.h>
Fabio Estevam0af008c2015-10-03 14:21:00 -030021#include <config.h>
22#include <fsl_wdog.h>
Simon Glassdbd79542020-05-10 11:40:11 -060023#include <linux/delay.h>
Gaurav Jain476c6392022-03-24 11:50:35 +053024#include <dm.h>
Wang Huan8ce6bec2014-09-05 13:52:34 +080025
chenhui zhao0c789872014-10-22 18:20:22 +080026#include "fsl_epu.h"
27
chenhui zhao9a378cb2015-01-23 15:53:53 +080028#define DCSR_RCPM2_BLOCK_OFFSET 0x223000
29#define DCSR_RCPM2_CPMFSMCR0 0x400
30#define DCSR_RCPM2_CPMFSMSR0 0x404
31#define DCSR_RCPM2_CPMFSMCR1 0x414
32#define DCSR_RCPM2_CPMFSMSR1 0x418
33#define CPMFSMSR_FSM_STATE_MASK 0x7f
34
Wang Huan8ce6bec2014-09-05 13:52:34 +080035DECLARE_GLOBAL_DATA_PTR;
36
Trevor Woerner43ec7e02019-05-03 09:41:00 -040037#if !CONFIG_IS_ENABLED(SYS_DCACHE_OFF)
Minghuan Lian6c9afed2015-01-21 17:29:17 +080038
39/*
40 * Bit[1] of the descriptor indicates the descriptor type,
41 * and bit[0] indicates whether the descriptor is valid.
42 */
43#define PMD_TYPE_TABLE 0x3
44#define PMD_TYPE_SECT 0x1
45
46/* AttrIndx[2:0] */
47#define PMD_ATTRINDX(t) ((t) << 2)
48
49/* Section */
50#define PMD_SECT_AF (1 << 10)
51
52#define BLOCK_SIZE_L1 (1UL << 30)
53#define BLOCK_SIZE_L2 (1UL << 21)
54
55/* TTBCR flags */
56#define TTBCR_EAE (1 << 31)
57#define TTBCR_T0SZ(x) ((x) << 0)
58#define TTBCR_T1SZ(x) ((x) << 16)
59#define TTBCR_USING_TTBR0 (TTBCR_T0SZ(0) | TTBCR_T1SZ(0))
60#define TTBCR_IRGN0_NC (0 << 8)
61#define TTBCR_IRGN0_WBWA (1 << 8)
62#define TTBCR_IRGN0_WT (2 << 8)
63#define TTBCR_IRGN0_WBNWA (3 << 8)
64#define TTBCR_IRGN0_MASK (3 << 8)
65#define TTBCR_ORGN0_NC (0 << 10)
66#define TTBCR_ORGN0_WBWA (1 << 10)
67#define TTBCR_ORGN0_WT (2 << 10)
68#define TTBCR_ORGN0_WBNWA (3 << 10)
69#define TTBCR_ORGN0_MASK (3 << 10)
70#define TTBCR_SHARED_NON (0 << 12)
71#define TTBCR_SHARED_OUTER (2 << 12)
72#define TTBCR_SHARED_INNER (3 << 12)
73#define TTBCR_EPD0 (0 << 7)
74#define TTBCR (TTBCR_SHARED_NON | \
75 TTBCR_ORGN0_NC | \
76 TTBCR_IRGN0_NC | \
77 TTBCR_USING_TTBR0 | \
78 TTBCR_EAE)
79
80/*
81 * Memory region attributes for LPAE (defined in pgtable):
82 *
83 * n = AttrIndx[2:0]
84 *
85 * n MAIR
86 * UNCACHED 000 00000000
87 * BUFFERABLE 001 01000100
88 * DEV_WC 001 01000100
89 * WRITETHROUGH 010 10101010
90 * WRITEBACK 011 11101110
91 * DEV_CACHED 011 11101110
92 * DEV_SHARED 100 00000100
93 * DEV_NONSHARED 100 00000100
94 * unused 101
95 * unused 110
96 * WRITEALLOC 111 11111111
97 */
98#define MT_MAIR0 0xeeaa4400
99#define MT_MAIR1 0xff000004
100#define MT_STRONLY_ORDER 0
101#define MT_NORMAL_NC 1
102#define MT_DEVICE_MEM 4
103#define MT_NORMAL 7
104
105/* The phy_addr must be aligned to 4KB */
106static inline void set_pgtable(u32 *page_table, u32 index, u32 phy_addr)
107{
108 u32 value = phy_addr | PMD_TYPE_TABLE;
109
110 page_table[2 * index] = value;
111 page_table[2 * index + 1] = 0;
112}
113
114/* The phy_addr must be aligned to 4KB */
115static inline void set_pgsection(u32 *page_table, u32 index, u64 phy_addr,
116 u32 memory_type)
117{
118 u64 value;
119
120 value = phy_addr | PMD_TYPE_SECT | PMD_SECT_AF;
121 value |= PMD_ATTRINDX(memory_type);
122 page_table[2 * index] = value & 0xFFFFFFFF;
123 page_table[2 * index + 1] = (value >> 32) & 0xFFFFFFFF;
124}
125
126/*
127 * Start MMU after DDR is available, we create MMU table in DRAM.
128 * The base address of TTLB is gd->arch.tlb_addr. We use two
129 * levels of translation tables here to cover 40-bit address space.
130 *
131 * The TTLBs are located at PHY 2G~4G.
132 *
133 * VA mapping:
134 *
135 * ------- <---- 0GB
136 * | |
137 * | |
138 * |-------| <---- 0x24000000
139 * |///////| ===> 192MB VA map for PCIe1 with offset 0x40_0000_0000
140 * |-------| <---- 0x300000000
141 * | |
142 * |-------| <---- 0x34000000
143 * |///////| ===> 192MB VA map for PCIe2 with offset 0x48_0000_0000
144 * |-------| <---- 0x40000000
145 * | |
146 * |-------| <---- 0x80000000 DDR0 space start
147 * |\\\\\\\|
148 *.|\\\\\\\| ===> 2GB VA map for 2GB DDR0 Memory space
149 * |\\\\\\\|
150 * ------- <---- 4GB DDR0 space end
151 */
152static void mmu_setup(void)
153{
154 u32 *level0_table = (u32 *)gd->arch.tlb_addr;
155 u32 *level1_table = (u32 *)(gd->arch.tlb_addr + 0x1000);
156 u64 va_start = 0;
157 u32 reg;
158 int i;
159
160 /* Level 0 Table 2-3 are used to map DDR */
161 set_pgsection(level0_table, 3, 3 * BLOCK_SIZE_L1, MT_NORMAL);
162 set_pgsection(level0_table, 2, 2 * BLOCK_SIZE_L1, MT_NORMAL);
163 /* Level 0 Table 1 is used to map device */
164 set_pgsection(level0_table, 1, 1 * BLOCK_SIZE_L1, MT_DEVICE_MEM);
165 /* Level 0 Table 0 is used to map device including PCIe MEM */
166 set_pgtable(level0_table, 0, (u32)level1_table);
167
168 /* Level 1 has 512 entries */
169 for (i = 0; i < 512; i++) {
170 /* Mapping for PCIe 1 */
171 if (va_start >= CONFIG_SYS_PCIE1_VIRT_ADDR &&
172 va_start < (CONFIG_SYS_PCIE1_VIRT_ADDR +
173 CONFIG_SYS_PCIE_MMAP_SIZE))
174 set_pgsection(level1_table, i,
175 CONFIG_SYS_PCIE1_PHYS_BASE + va_start,
176 MT_DEVICE_MEM);
177 /* Mapping for PCIe 2 */
178 else if (va_start >= CONFIG_SYS_PCIE2_VIRT_ADDR &&
179 va_start < (CONFIG_SYS_PCIE2_VIRT_ADDR +
180 CONFIG_SYS_PCIE_MMAP_SIZE))
181 set_pgsection(level1_table, i,
182 CONFIG_SYS_PCIE2_PHYS_BASE + va_start,
183 MT_DEVICE_MEM);
184 else
185 set_pgsection(level1_table, i,
186 va_start,
187 MT_DEVICE_MEM);
188 va_start += BLOCK_SIZE_L2;
189 }
190
191 asm volatile("dsb sy;isb");
192 asm volatile("mcr p15, 0, %0, c2, c0, 2" /* Write RT to TTBCR */
193 : : "r" (TTBCR) : "memory");
194 asm volatile("mcrr p15, 0, %0, %1, c2" /* TTBR 0 */
195 : : "r" ((u32)level0_table), "r" (0) : "memory");
196 asm volatile("mcr p15, 0, %0, c10, c2, 0" /* write MAIR 0 */
197 : : "r" (MT_MAIR0) : "memory");
198 asm volatile("mcr p15, 0, %0, c10, c2, 1" /* write MAIR 1 */
199 : : "r" (MT_MAIR1) : "memory");
200
201 /* Set the access control to all-supervisor */
202 asm volatile("mcr p15, 0, %0, c3, c0, 0"
203 : : "r" (~0));
204
205 /* Enable the mmu */
206 reg = get_cr();
207 set_cr(reg | CR_M);
208}
209
210/*
211 * This function is called from lib/board.c. It recreates MMU
212 * table in main memory. MMU and i/d-cache are enabled here.
213 */
214void enable_caches(void)
215{
216 /* Invalidate all TLB */
217 mmu_page_table_flush(gd->arch.tlb_addr,
218 gd->arch.tlb_addr + gd->arch.tlb_size);
219 /* Set up and enable mmu */
220 mmu_setup();
221
222 /* Invalidate & Enable d-cache */
223 invalidate_dcache_all();
224 set_cr(get_cr() | CR_C);
225}
Trevor Woerner43ec7e02019-05-03 09:41:00 -0400226#endif /* #if !CONFIG_IS_ENABLED(SYS_DCACHE_OFF) */
Minghuan Lian6c9afed2015-01-21 17:29:17 +0800227
Shengzhou Liubf5aee92015-11-20 15:52:02 +0800228
229uint get_svr(void)
230{
231 struct ccsr_gur __iomem *gur = (void *)(CONFIG_SYS_FSL_GUTS_ADDR);
232
233 return in_be32(&gur->svr);
234}
235
Wang Huan8ce6bec2014-09-05 13:52:34 +0800236#if defined(CONFIG_DISPLAY_CPUINFO)
237int print_cpuinfo(void)
238{
239 char buf1[32], buf2[32];
240 struct ccsr_gur __iomem *gur = (void *)(CONFIG_SYS_FSL_GUTS_ADDR);
241 unsigned int svr, major, minor, ver, i;
242
243 svr = in_be32(&gur->svr);
244 major = SVR_MAJ(svr);
245 minor = SVR_MIN(svr);
246
247 puts("CPU: Freescale LayerScape ");
248
249 ver = SVR_SOC_VER(svr);
250 switch (ver) {
251 case SOC_VER_SLS1020:
252 puts("SLS1020");
253 break;
254 case SOC_VER_LS1020:
255 puts("LS1020");
256 break;
257 case SOC_VER_LS1021:
258 puts("LS1021");
259 break;
260 case SOC_VER_LS1022:
261 puts("LS1022");
262 break;
263 default:
264 puts("Unknown");
265 break;
266 }
267
268 if (IS_E_PROCESSOR(svr) && (ver != SOC_VER_SLS1020))
269 puts("E");
270
271 printf(", Version: %d.%d, (0x%08x)\n", major, minor, svr);
272
273 puts("Clock Configuration:");
274
275 printf("\n CPU0(ARMV7):%-4s MHz, ", strmhz(buf1, gd->cpu_clk));
276 printf("\n Bus:%-4s MHz, ", strmhz(buf1, gd->bus_clk));
277 printf("DDR:%-4s MHz (%s MT/s data rate), ",
278 strmhz(buf1, gd->mem_clk/2), strmhz(buf2, gd->mem_clk));
279 puts("\n");
280
281 /* Display the RCW, so that no one gets confused as to what RCW
282 * we're actually using for this boot.
283 */
284 puts("Reset Configuration Word (RCW):");
285 for (i = 0; i < ARRAY_SIZE(gur->rcwsr); i++) {
286 u32 rcw = in_be32(&gur->rcwsr[i]);
287
288 if ((i % 4) == 0)
289 printf("\n %08x:", i * 4);
290 printf(" %08x", rcw);
291 }
292 puts("\n");
293
294 return 0;
295}
296#endif
297
Wang Huan8ce6bec2014-09-05 13:52:34 +0800298#ifdef CONFIG_FSL_ESDHC
Masahiro Yamadaf7ed78b2020-06-26 15:13:33 +0900299int cpu_mmc_init(struct bd_info *bis)
Wang Huan8ce6bec2014-09-05 13:52:34 +0800300{
301 return fsl_esdhc_mmc_init(bis);
302}
303#endif
304
Masahiro Yamadaf7ed78b2020-06-26 15:13:33 +0900305int cpu_eth_init(struct bd_info *bis)
Wang Huan8ce6bec2014-09-05 13:52:34 +0800306{
Bin Meng19c04602019-07-19 00:29:59 +0300307#if defined(CONFIG_TSEC_ENET) && !defined(CONFIG_DM_ETH)
Wang Huan8ce6bec2014-09-05 13:52:34 +0800308 tsec_standard_init(bis);
309#endif
310
311 return 0;
312}
chenhui zhao0c789872014-10-22 18:20:22 +0800313
314int arch_cpu_init(void)
315{
316 void *epu_base = (void *)(CONFIG_SYS_DCSRBAR + EPU_BLOCK_OFFSET);
chenhui zhao9a378cb2015-01-23 15:53:53 +0800317 void *rcpm2_base =
318 (void *)(CONFIG_SYS_DCSRBAR + DCSR_RCPM2_BLOCK_OFFSET);
horia.geanta@freescale.comcc0619c2015-10-15 14:21:31 +0300319 struct ccsr_scfg *scfg = (void *)CONFIG_SYS_FSL_SCFG_ADDR;
chenhui zhao9a378cb2015-01-23 15:53:53 +0800320 u32 state;
321
Hou Zhiqiang49713762021-02-05 15:45:12 +0800322 icache_enable();
323
chenhui zhao9a378cb2015-01-23 15:53:53 +0800324 /*
325 * The RCPM FSM state may not be reset after power-on.
326 * So, reset them.
327 */
328 state = in_be32(rcpm2_base + DCSR_RCPM2_CPMFSMSR0) &
329 CPMFSMSR_FSM_STATE_MASK;
330 if (state != 0) {
331 out_be32(rcpm2_base + DCSR_RCPM2_CPMFSMCR0, 0x80);
332 out_be32(rcpm2_base + DCSR_RCPM2_CPMFSMCR0, 0x0);
333 }
334
335 state = in_be32(rcpm2_base + DCSR_RCPM2_CPMFSMSR1) &
336 CPMFSMSR_FSM_STATE_MASK;
337 if (state != 0) {
338 out_be32(rcpm2_base + DCSR_RCPM2_CPMFSMCR1, 0x80);
339 out_be32(rcpm2_base + DCSR_RCPM2_CPMFSMCR1, 0x0);
340 }
chenhui zhao0c789872014-10-22 18:20:22 +0800341
342 /*
343 * After wakeup from deep sleep, Clear EPU registers
344 * as early as possible to prevent from possible issue.
345 * It's also safe to clear at normal boot.
346 */
347 fsl_epu_clean(epu_base);
348
horia.geanta@freescale.comcc0619c2015-10-15 14:21:31 +0300349 setbits_be32(&scfg->snpcnfgcr, SCFG_SNPCNFGCR_SEC_RD_WR);
350
chenhui zhao0c789872014-10-22 18:20:22 +0800351 return 0;
352}
Xiubo Lib73446d2014-11-21 17:40:56 +0800353
Jan Kiszkaac31b5a2015-04-21 07:18:24 +0200354#ifdef CONFIG_ARMV7_NONSEC
Xiubo Lib73446d2014-11-21 17:40:56 +0800355/* Set the address at which the secondary core starts from.*/
356void smp_set_core_boot_addr(unsigned long addr, int corenr)
357{
358 struct ccsr_gur __iomem *gur = (void *)(CONFIG_SYS_FSL_GUTS_ADDR);
359
360 out_be32(&gur->scratchrw[0], addr);
361}
362
363/* Release the secondary core from holdoff state and kick it */
364void smp_kick_all_cpus(void)
365{
366 struct ccsr_gur __iomem *gur = (void *)(CONFIG_SYS_FSL_GUTS_ADDR);
367
368 out_be32(&gur->brrl, 0x2);
Wang Dongsheng0b5034c2015-06-18 18:32:58 +0800369
370 /*
371 * LS1 STANDBYWFE is not captured outside the ARM module in the soc.
372 * So add a delay to wait bootrom execute WFE.
373 */
374 udelay(1);
375
376 asm volatile("sev");
Xiubo Lib73446d2014-11-21 17:40:56 +0800377}
378#endif
Fabio Estevam0af008c2015-10-03 14:21:00 -0300379
Harald Seiler6f14d5f2020-12-15 16:47:52 +0100380void reset_cpu(void)
Fabio Estevam0af008c2015-10-03 14:21:00 -0300381{
382 struct watchdog_regs *wdog = (struct watchdog_regs *)WDOG1_BASE_ADDR;
383
384 clrbits_be16(&wdog->wcr, WCR_SRS);
385
386 while (1) {
387 /*
388 * Let the watchdog trigger
389 */
390 }
391}
Alison Wangd285ef72015-08-04 09:55:37 +0800392
393void arch_preboot_os(void)
394{
395 unsigned long ctrl;
396
397 /* Disable PL1 Physical Timer */
398 asm("mrc p15, 0, %0, c14, c2, 1" : "=r" (ctrl));
399 ctrl &= ~ARCH_TIMER_CTRL_ENABLE;
400 asm("mcr p15, 0, %0, c14, c2, 1" : : "r" (ctrl));
401}
Gaurav Jain476c6392022-03-24 11:50:35 +0530402
403#ifdef CONFIG_ARCH_MISC_INIT
404int arch_misc_init(void)
405{
406 if (IS_ENABLED(CONFIG_FSL_CAAM)) {
407 struct udevice *dev;
408 int ret;
409
410 ret = uclass_get_device_by_driver(UCLASS_MISC, DM_DRIVER_GET(caam_jr), &dev);
411 if (ret)
Ye Liec346892022-05-11 13:56:20 +0530412 printf("Failed to initialize caam_jr: %d\n", ret);
Gaurav Jain476c6392022-03-24 11:50:35 +0530413 }
414
415 return 0;
416}
417#endif