blob: 5e7bf57a623046c05a69c20269cfa106c8aad0d9 [file] [log] [blame]
Peng Fan331d40d2021-08-07 16:00:31 +08001// SPDX-License-Identifier: GPL-2.0+
2/*
3 * Copyright 2021 NXP
4 */
5
Peng Fan77c3b9c2021-08-07 16:00:33 +08006#include <asm/io.h>
7#include <asm/arch/clock.h>
8#include <asm/arch/imx-regs.h>
Peng Fan331d40d2021-08-07 16:00:31 +08009#include <asm/arch/sys_proto.h>
Peng Fan9ef89ea2021-08-07 16:00:35 +080010#include <asm/armv8/mmu.h>
Peng Fan77c3b9c2021-08-07 16:00:33 +080011#include <asm/mach-imx/boot_mode.h>
Ye Li610083e2021-08-07 16:00:48 +080012#include <efi_loader.h>
13#include <spl.h>
Peng Fan331d40d2021-08-07 16:00:31 +080014
Peng Fan9ef89ea2021-08-07 16:00:35 +080015DECLARE_GLOBAL_DATA_PTR;
16
Ye Li6f3858d2021-08-07 16:00:39 +080017struct rom_api *g_rom_api = (struct rom_api *)0x1980;
18
Peng Fan331d40d2021-08-07 16:00:31 +080019u32 get_cpu_rev(void)
20{
21 return (MXC_CPU_IMX8ULP << 12) | CHIP_REV_1_0;
22}
Peng Fan77c3b9c2021-08-07 16:00:33 +080023
24enum bt_mode get_boot_mode(void)
25{
26 u32 bt0_cfg = 0;
27
Ye Li981f0402021-08-07 16:00:47 +080028 bt0_cfg = readl(CMC1_BASE_ADDR + 0xa0);
Peng Fan77c3b9c2021-08-07 16:00:33 +080029 bt0_cfg &= (BT0CFG_LPBOOT_MASK | BT0CFG_DUALBOOT_MASK);
30
31 if (!(bt0_cfg & BT0CFG_LPBOOT_MASK)) {
32 /* No low power boot */
33 if (bt0_cfg & BT0CFG_DUALBOOT_MASK)
34 return DUAL_BOOT;
35 else
36 return SINGLE_BOOT;
37 }
38
39 return LOW_POWER_BOOT;
40}
41
Peng Fanc17f5932021-08-07 16:00:34 +080042#define CMC_SRS_TAMPER BIT(31)
43#define CMC_SRS_SECURITY BIT(30)
44#define CMC_SRS_TZWDG BIT(29)
45#define CMC_SRS_JTAG_RST BIT(28)
46#define CMC_SRS_CORE1 BIT(16)
47#define CMC_SRS_LOCKUP BIT(15)
48#define CMC_SRS_SW BIT(14)
49#define CMC_SRS_WDG BIT(13)
50#define CMC_SRS_PIN_RESET BIT(8)
51#define CMC_SRS_WARM BIT(4)
52#define CMC_SRS_HVD BIT(3)
53#define CMC_SRS_LVD BIT(2)
54#define CMC_SRS_POR BIT(1)
55#define CMC_SRS_WUP BIT(0)
56
57static u32 reset_cause = -1;
58
59static char *get_reset_cause(char *ret)
60{
61 u32 cause1, cause = 0, srs = 0;
Peng Fan9ef89ea2021-08-07 16:00:35 +080062 void __iomem *reg_ssrs = (void __iomem *)(CMC1_BASE_ADDR + 0x88);
63 void __iomem *reg_srs = (void __iomem *)(CMC1_BASE_ADDR + 0x80);
Peng Fanc17f5932021-08-07 16:00:34 +080064
65 if (!ret)
66 return "null";
67
68 srs = readl(reg_srs);
69 cause1 = readl(reg_ssrs);
70
71 reset_cause = cause1;
72
73 cause = cause1 & (CMC_SRS_POR | CMC_SRS_WUP | CMC_SRS_WARM);
74
75 switch (cause) {
76 case CMC_SRS_POR:
77 sprintf(ret, "%s", "POR");
78 break;
79 case CMC_SRS_WUP:
80 sprintf(ret, "%s", "WUP");
81 break;
82 case CMC_SRS_WARM:
83 cause = cause1 & (CMC_SRS_WDG | CMC_SRS_SW |
84 CMC_SRS_JTAG_RST);
85 switch (cause) {
86 case CMC_SRS_WDG:
87 sprintf(ret, "%s", "WARM-WDG");
88 break;
89 case CMC_SRS_SW:
90 sprintf(ret, "%s", "WARM-SW");
91 break;
92 case CMC_SRS_JTAG_RST:
93 sprintf(ret, "%s", "WARM-JTAG");
94 break;
95 default:
96 sprintf(ret, "%s", "WARM-UNKN");
97 break;
98 }
99 break;
100 default:
101 sprintf(ret, "%s-%X", "UNKN", cause1);
102 break;
103 }
104
105 debug("[%X] SRS[%X] %X - ", cause1, srs, srs ^ cause1);
106 return ret;
107}
108
Peng Fan77c3b9c2021-08-07 16:00:33 +0800109#if defined(CONFIG_DISPLAY_CPUINFO)
110const char *get_imx_type(u32 imxtype)
111{
112 return "8ULP";
113}
114
115int print_cpuinfo(void)
116{
117 u32 cpurev;
118 char cause[18];
119
120 cpurev = get_cpu_rev();
121
122 printf("CPU: Freescale i.MX%s rev%d.%d at %d MHz\n",
123 get_imx_type((cpurev & 0xFF000) >> 12),
124 (cpurev & 0x000F0) >> 4, (cpurev & 0x0000F) >> 0,
125 mxc_get_clock(MXC_ARM_CLK) / 1000000);
126
Peng Fanc17f5932021-08-07 16:00:34 +0800127 printf("Reset cause: %s\n", get_reset_cause(cause));
128
Peng Fan77c3b9c2021-08-07 16:00:33 +0800129 printf("Boot mode: ");
130 switch (get_boot_mode()) {
131 case LOW_POWER_BOOT:
132 printf("Low power boot\n");
133 break;
134 case DUAL_BOOT:
135 printf("Dual boot\n");
136 break;
137 case SINGLE_BOOT:
138 default:
139 printf("Single boot\n");
140 break;
141 }
142
143 return 0;
144}
145#endif
Peng Fan9ef89ea2021-08-07 16:00:35 +0800146
Peng Fan3a01f722021-08-07 16:00:49 +0800147#define UNLOCK_WORD0 0xC520 /* 1st unlock word */
148#define UNLOCK_WORD1 0xD928 /* 2nd unlock word */
149#define REFRESH_WORD0 0xA602 /* 1st refresh word */
150#define REFRESH_WORD1 0xB480 /* 2nd refresh word */
151
152static void disable_wdog(void __iomem *wdog_base)
153{
154 u32 val_cs = readl(wdog_base + 0x00);
155
156 if (!(val_cs & 0x80))
157 return;
158
159 dmb();
160 __raw_writel(REFRESH_WORD0, (wdog_base + 0x04)); /* Refresh the CNT */
161 __raw_writel(REFRESH_WORD1, (wdog_base + 0x04));
162 dmb();
163
164 if (!(val_cs & 800)) {
165 dmb();
166 __raw_writel(UNLOCK_WORD0, (wdog_base + 0x04));
167 __raw_writel(UNLOCK_WORD1, (wdog_base + 0x04));
168 dmb();
169
170 while (!(readl(wdog_base + 0x00) & 0x800))
171 ;
172 }
173 writel(0x0, (wdog_base + 0x0C)); /* Set WIN to 0 */
174 writel(0x400, (wdog_base + 0x08)); /* Set timeout to default 0x400 */
175 writel(0x120, (wdog_base + 0x00)); /* Disable it and set update */
176
177 while (!(readl(wdog_base + 0x00) & 0x400))
178 ;
179}
180
Peng Fan9ef89ea2021-08-07 16:00:35 +0800181void init_wdog(void)
182{
Peng Fan3a01f722021-08-07 16:00:49 +0800183 disable_wdog((void __iomem *)WDG3_RBASE);
Peng Fan9ef89ea2021-08-07 16:00:35 +0800184}
185
186static struct mm_region imx8ulp_arm64_mem_map[] = {
187 {
188 /* ROM */
189 .virt = 0x0,
190 .phys = 0x0,
191 .size = 0x40000UL,
192 .attrs = PTE_BLOCK_MEMTYPE(MT_NORMAL) |
193 PTE_BLOCK_OUTER_SHARE
194 },
195 {
196 /* FLEXSPI0 */
197 .virt = 0x04000000,
198 .phys = 0x04000000,
199 .size = 0x08000000UL,
200 .attrs = PTE_BLOCK_MEMTYPE(MT_DEVICE_NGNRNE) |
201 PTE_BLOCK_NON_SHARE |
202 PTE_BLOCK_PXN | PTE_BLOCK_UXN
203 },
204 {
205 /* SSRAM (align with 2M) */
206 .virt = 0x1FE00000UL,
207 .phys = 0x1FE00000UL,
208 .size = 0x400000UL,
209 .attrs = PTE_BLOCK_MEMTYPE(MT_NORMAL) |
210 PTE_BLOCK_OUTER_SHARE |
211 PTE_BLOCK_PXN | PTE_BLOCK_UXN
212 }, {
213 /* SRAM1 (align with 2M) */
214 .virt = 0x21000000UL,
215 .phys = 0x21000000UL,
216 .size = 0x200000UL,
217 .attrs = PTE_BLOCK_MEMTYPE(MT_NORMAL) |
218 PTE_BLOCK_OUTER_SHARE |
219 PTE_BLOCK_PXN | PTE_BLOCK_UXN
220 }, {
221 /* SRAM0 (align with 2M) */
222 .virt = 0x22000000UL,
223 .phys = 0x22000000UL,
224 .size = 0x200000UL,
225 .attrs = PTE_BLOCK_MEMTYPE(MT_NORMAL) |
226 PTE_BLOCK_OUTER_SHARE |
227 PTE_BLOCK_PXN | PTE_BLOCK_UXN
228 }, {
229 /* Peripherals */
230 .virt = 0x27000000UL,
231 .phys = 0x27000000UL,
232 .size = 0x3000000UL,
233 .attrs = PTE_BLOCK_MEMTYPE(MT_DEVICE_NGNRNE) |
234 PTE_BLOCK_NON_SHARE |
235 PTE_BLOCK_PXN | PTE_BLOCK_UXN
236 }, {
237 /* Peripherals */
238 .virt = 0x2D000000UL,
239 .phys = 0x2D000000UL,
240 .size = 0x1600000UL,
241 .attrs = PTE_BLOCK_MEMTYPE(MT_DEVICE_NGNRNE) |
242 PTE_BLOCK_NON_SHARE |
243 PTE_BLOCK_PXN | PTE_BLOCK_UXN
244 }, {
245 /* FLEXSPI1-2 */
246 .virt = 0x40000000UL,
247 .phys = 0x40000000UL,
248 .size = 0x40000000UL,
249 .attrs = PTE_BLOCK_MEMTYPE(MT_DEVICE_NGNRNE) |
250 PTE_BLOCK_NON_SHARE |
251 PTE_BLOCK_PXN | PTE_BLOCK_UXN
252 }, {
253 /* DRAM1 */
254 .virt = 0x80000000UL,
255 .phys = 0x80000000UL,
256 .size = PHYS_SDRAM_SIZE,
257 .attrs = PTE_BLOCK_MEMTYPE(MT_NORMAL) |
258 PTE_BLOCK_OUTER_SHARE
259 }, {
260 /*
261 * empty entrie to split table entry 5
262 * if needed when TEEs are used
263 */
264 0,
265 }, {
266 /* List terminator */
267 0,
268 }
269};
270
271struct mm_region *mem_map = imx8ulp_arm64_mem_map;
272
273/* simplify the page table size to enhance boot speed */
274#define MAX_PTE_ENTRIES 512
275#define MAX_MEM_MAP_REGIONS 16
276u64 get_page_table_size(void)
277{
278 u64 one_pt = MAX_PTE_ENTRIES * sizeof(u64);
279 u64 size = 0;
280
281 /*
282 * For each memory region, the max table size:
283 * 2 level 3 tables + 2 level 2 tables + 1 level 1 table
284 */
285 size = (2 + 2 + 1) * one_pt * MAX_MEM_MAP_REGIONS + one_pt;
286
287 /*
288 * We need to duplicate our page table once to have an emergency pt to
289 * resort to when splitting page tables later on
290 */
291 size *= 2;
292
293 /*
294 * We may need to split page tables later on if dcache settings change,
295 * so reserve up to 4 (random pick) page tables for that.
296 */
297 size += one_pt * 4;
298
299 return size;
300}
301
302void enable_caches(void)
303{
304 /* TODO: add TEE memmap region */
305
306 icache_enable();
307 dcache_enable();
308}
309
310int dram_init(void)
311{
312 gd->ram_size = PHYS_SDRAM_SIZE;
313
314 return 0;
315}
316
317#ifdef CONFIG_SERIAL_TAG
318void get_board_serial(struct tag_serialnr *serialnr)
319{
320 /* TODO */
321}
322#endif
323
Ye Liaadd6ca2021-08-07 16:00:50 +0800324static void set_core0_reset_vector(u32 entry)
Peng Fan9ef89ea2021-08-07 16:00:35 +0800325{
Ye Li610083e2021-08-07 16:00:48 +0800326 /* Update SIM1 DGO8 for reset vector base */
Ye Liaadd6ca2021-08-07 16:00:50 +0800327 writel(entry, SIM1_BASE_ADDR + 0x5c);
Ye Li610083e2021-08-07 16:00:48 +0800328
329 /* set update bit */
330 setbits_le32(SIM1_BASE_ADDR + 0x8, 0x1 << 24);
331
332 /* polling the ack */
333 while ((readl(SIM1_BASE_ADDR + 0x8) & (0x1 << 26)) == 0)
334 ;
335
336 /* clear the update */
337 clrbits_le32(SIM1_BASE_ADDR + 0x8, (0x1 << 24));
338
339 /* clear the ack by set 1 */
340 setbits_le32(SIM1_BASE_ADDR + 0x8, (0x1 << 26));
Ye Liaadd6ca2021-08-07 16:00:50 +0800341}
342
343int arch_cpu_init(void)
344{
345 if (IS_ENABLED(CONFIG_SPL_BUILD)) {
346 clock_init();
347 } else {
348 /* reconfigure core0 reset vector to ROM */
349 set_core0_reset_vector(0x1000);
350 }
351
352 return 0;
353}
354
355#if defined(CONFIG_SPL_BUILD)
356__weak void __noreturn jump_to_image_no_args(struct spl_image_info *spl_image)
357{
358 debug("image entry point: 0x%lx\n", spl_image->entry_point);
359
360 set_core0_reset_vector((u32)spl_image->entry_point);
Ye Li610083e2021-08-07 16:00:48 +0800361
362 /* Enable the 512KB cache */
363 setbits_le32(SIM1_BASE_ADDR + 0x30, (0x1 << 4));
364
365 /* reset core */
366 setbits_le32(SIM1_BASE_ADDR + 0x30, (0x1 << 16));
367
368 while (1)
369 ;
370}
371#endif