blob: c5e20408c67f794c384417b6266f18a6efac5c18 [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 Fan3912d4b2021-08-07 16:00:59 +080014#include <asm/arch/rdc.h>
Ye Liba472a22021-08-07 16:00:55 +080015#include <asm/arch/s400_api.h>
16#include <asm/arch/mu_hal.h>
17#include <cpu_func.h>
18#include <asm/setup.h>
Peng Fan331d40d2021-08-07 16:00:31 +080019
Peng Fan9ef89ea2021-08-07 16:00:35 +080020DECLARE_GLOBAL_DATA_PTR;
21
Ye Li6f3858d2021-08-07 16:00:39 +080022struct rom_api *g_rom_api = (struct rom_api *)0x1980;
23
Peng Fan331d40d2021-08-07 16:00:31 +080024u32 get_cpu_rev(void)
25{
26 return (MXC_CPU_IMX8ULP << 12) | CHIP_REV_1_0;
27}
Peng Fan77c3b9c2021-08-07 16:00:33 +080028
29enum bt_mode get_boot_mode(void)
30{
31 u32 bt0_cfg = 0;
32
Ye Li981f0402021-08-07 16:00:47 +080033 bt0_cfg = readl(CMC1_BASE_ADDR + 0xa0);
Peng Fan77c3b9c2021-08-07 16:00:33 +080034 bt0_cfg &= (BT0CFG_LPBOOT_MASK | BT0CFG_DUALBOOT_MASK);
35
36 if (!(bt0_cfg & BT0CFG_LPBOOT_MASK)) {
37 /* No low power boot */
38 if (bt0_cfg & BT0CFG_DUALBOOT_MASK)
39 return DUAL_BOOT;
40 else
41 return SINGLE_BOOT;
42 }
43
44 return LOW_POWER_BOOT;
45}
46
Peng Fanc17f5932021-08-07 16:00:34 +080047#define CMC_SRS_TAMPER BIT(31)
48#define CMC_SRS_SECURITY BIT(30)
49#define CMC_SRS_TZWDG BIT(29)
50#define CMC_SRS_JTAG_RST BIT(28)
51#define CMC_SRS_CORE1 BIT(16)
52#define CMC_SRS_LOCKUP BIT(15)
53#define CMC_SRS_SW BIT(14)
54#define CMC_SRS_WDG BIT(13)
55#define CMC_SRS_PIN_RESET BIT(8)
56#define CMC_SRS_WARM BIT(4)
57#define CMC_SRS_HVD BIT(3)
58#define CMC_SRS_LVD BIT(2)
59#define CMC_SRS_POR BIT(1)
60#define CMC_SRS_WUP BIT(0)
61
62static u32 reset_cause = -1;
63
64static char *get_reset_cause(char *ret)
65{
66 u32 cause1, cause = 0, srs = 0;
Peng Fan9ef89ea2021-08-07 16:00:35 +080067 void __iomem *reg_ssrs = (void __iomem *)(CMC1_BASE_ADDR + 0x88);
68 void __iomem *reg_srs = (void __iomem *)(CMC1_BASE_ADDR + 0x80);
Peng Fanc17f5932021-08-07 16:00:34 +080069
70 if (!ret)
71 return "null";
72
73 srs = readl(reg_srs);
74 cause1 = readl(reg_ssrs);
75
76 reset_cause = cause1;
77
78 cause = cause1 & (CMC_SRS_POR | CMC_SRS_WUP | CMC_SRS_WARM);
79
80 switch (cause) {
81 case CMC_SRS_POR:
82 sprintf(ret, "%s", "POR");
83 break;
84 case CMC_SRS_WUP:
85 sprintf(ret, "%s", "WUP");
86 break;
87 case CMC_SRS_WARM:
88 cause = cause1 & (CMC_SRS_WDG | CMC_SRS_SW |
89 CMC_SRS_JTAG_RST);
90 switch (cause) {
91 case CMC_SRS_WDG:
92 sprintf(ret, "%s", "WARM-WDG");
93 break;
94 case CMC_SRS_SW:
95 sprintf(ret, "%s", "WARM-SW");
96 break;
97 case CMC_SRS_JTAG_RST:
98 sprintf(ret, "%s", "WARM-JTAG");
99 break;
100 default:
101 sprintf(ret, "%s", "WARM-UNKN");
102 break;
103 }
104 break;
105 default:
106 sprintf(ret, "%s-%X", "UNKN", cause1);
107 break;
108 }
109
110 debug("[%X] SRS[%X] %X - ", cause1, srs, srs ^ cause1);
111 return ret;
112}
113
Peng Fan77c3b9c2021-08-07 16:00:33 +0800114#if defined(CONFIG_DISPLAY_CPUINFO)
115const char *get_imx_type(u32 imxtype)
116{
117 return "8ULP";
118}
119
120int print_cpuinfo(void)
121{
122 u32 cpurev;
123 char cause[18];
124
125 cpurev = get_cpu_rev();
126
127 printf("CPU: Freescale i.MX%s rev%d.%d at %d MHz\n",
128 get_imx_type((cpurev & 0xFF000) >> 12),
129 (cpurev & 0x000F0) >> 4, (cpurev & 0x0000F) >> 0,
130 mxc_get_clock(MXC_ARM_CLK) / 1000000);
131
Peng Fanc17f5932021-08-07 16:00:34 +0800132 printf("Reset cause: %s\n", get_reset_cause(cause));
133
Peng Fan77c3b9c2021-08-07 16:00:33 +0800134 printf("Boot mode: ");
135 switch (get_boot_mode()) {
136 case LOW_POWER_BOOT:
137 printf("Low power boot\n");
138 break;
139 case DUAL_BOOT:
140 printf("Dual boot\n");
141 break;
142 case SINGLE_BOOT:
143 default:
144 printf("Single boot\n");
145 break;
146 }
147
148 return 0;
149}
150#endif
Peng Fan9ef89ea2021-08-07 16:00:35 +0800151
Peng Fan3a01f722021-08-07 16:00:49 +0800152#define UNLOCK_WORD0 0xC520 /* 1st unlock word */
153#define UNLOCK_WORD1 0xD928 /* 2nd unlock word */
154#define REFRESH_WORD0 0xA602 /* 1st refresh word */
155#define REFRESH_WORD1 0xB480 /* 2nd refresh word */
156
157static void disable_wdog(void __iomem *wdog_base)
158{
159 u32 val_cs = readl(wdog_base + 0x00);
160
161 if (!(val_cs & 0x80))
162 return;
163
164 dmb();
165 __raw_writel(REFRESH_WORD0, (wdog_base + 0x04)); /* Refresh the CNT */
166 __raw_writel(REFRESH_WORD1, (wdog_base + 0x04));
167 dmb();
168
169 if (!(val_cs & 800)) {
170 dmb();
171 __raw_writel(UNLOCK_WORD0, (wdog_base + 0x04));
172 __raw_writel(UNLOCK_WORD1, (wdog_base + 0x04));
173 dmb();
174
175 while (!(readl(wdog_base + 0x00) & 0x800))
176 ;
177 }
178 writel(0x0, (wdog_base + 0x0C)); /* Set WIN to 0 */
179 writel(0x400, (wdog_base + 0x08)); /* Set timeout to default 0x400 */
180 writel(0x120, (wdog_base + 0x00)); /* Disable it and set update */
181
182 while (!(readl(wdog_base + 0x00) & 0x400))
183 ;
184}
185
Peng Fan9ef89ea2021-08-07 16:00:35 +0800186void init_wdog(void)
187{
Peng Fan3a01f722021-08-07 16:00:49 +0800188 disable_wdog((void __iomem *)WDG3_RBASE);
Peng Fan9ef89ea2021-08-07 16:00:35 +0800189}
190
191static struct mm_region imx8ulp_arm64_mem_map[] = {
192 {
193 /* ROM */
194 .virt = 0x0,
195 .phys = 0x0,
196 .size = 0x40000UL,
197 .attrs = PTE_BLOCK_MEMTYPE(MT_NORMAL) |
198 PTE_BLOCK_OUTER_SHARE
199 },
200 {
201 /* FLEXSPI0 */
202 .virt = 0x04000000,
203 .phys = 0x04000000,
204 .size = 0x08000000UL,
205 .attrs = PTE_BLOCK_MEMTYPE(MT_DEVICE_NGNRNE) |
206 PTE_BLOCK_NON_SHARE |
207 PTE_BLOCK_PXN | PTE_BLOCK_UXN
208 },
209 {
210 /* SSRAM (align with 2M) */
211 .virt = 0x1FE00000UL,
212 .phys = 0x1FE00000UL,
213 .size = 0x400000UL,
214 .attrs = PTE_BLOCK_MEMTYPE(MT_NORMAL) |
215 PTE_BLOCK_OUTER_SHARE |
216 PTE_BLOCK_PXN | PTE_BLOCK_UXN
217 }, {
218 /* SRAM1 (align with 2M) */
219 .virt = 0x21000000UL,
220 .phys = 0x21000000UL,
221 .size = 0x200000UL,
222 .attrs = PTE_BLOCK_MEMTYPE(MT_NORMAL) |
223 PTE_BLOCK_OUTER_SHARE |
224 PTE_BLOCK_PXN | PTE_BLOCK_UXN
225 }, {
226 /* SRAM0 (align with 2M) */
227 .virt = 0x22000000UL,
228 .phys = 0x22000000UL,
229 .size = 0x200000UL,
230 .attrs = PTE_BLOCK_MEMTYPE(MT_NORMAL) |
231 PTE_BLOCK_OUTER_SHARE |
232 PTE_BLOCK_PXN | PTE_BLOCK_UXN
233 }, {
234 /* Peripherals */
235 .virt = 0x27000000UL,
236 .phys = 0x27000000UL,
237 .size = 0x3000000UL,
238 .attrs = PTE_BLOCK_MEMTYPE(MT_DEVICE_NGNRNE) |
239 PTE_BLOCK_NON_SHARE |
240 PTE_BLOCK_PXN | PTE_BLOCK_UXN
241 }, {
242 /* Peripherals */
243 .virt = 0x2D000000UL,
244 .phys = 0x2D000000UL,
245 .size = 0x1600000UL,
246 .attrs = PTE_BLOCK_MEMTYPE(MT_DEVICE_NGNRNE) |
247 PTE_BLOCK_NON_SHARE |
248 PTE_BLOCK_PXN | PTE_BLOCK_UXN
249 }, {
250 /* FLEXSPI1-2 */
251 .virt = 0x40000000UL,
252 .phys = 0x40000000UL,
253 .size = 0x40000000UL,
254 .attrs = PTE_BLOCK_MEMTYPE(MT_DEVICE_NGNRNE) |
255 PTE_BLOCK_NON_SHARE |
256 PTE_BLOCK_PXN | PTE_BLOCK_UXN
257 }, {
258 /* DRAM1 */
259 .virt = 0x80000000UL,
260 .phys = 0x80000000UL,
261 .size = PHYS_SDRAM_SIZE,
262 .attrs = PTE_BLOCK_MEMTYPE(MT_NORMAL) |
263 PTE_BLOCK_OUTER_SHARE
264 }, {
265 /*
266 * empty entrie to split table entry 5
267 * if needed when TEEs are used
268 */
269 0,
270 }, {
271 /* List terminator */
272 0,
273 }
274};
275
276struct mm_region *mem_map = imx8ulp_arm64_mem_map;
277
278/* simplify the page table size to enhance boot speed */
279#define MAX_PTE_ENTRIES 512
280#define MAX_MEM_MAP_REGIONS 16
281u64 get_page_table_size(void)
282{
283 u64 one_pt = MAX_PTE_ENTRIES * sizeof(u64);
284 u64 size = 0;
285
286 /*
287 * For each memory region, the max table size:
288 * 2 level 3 tables + 2 level 2 tables + 1 level 1 table
289 */
290 size = (2 + 2 + 1) * one_pt * MAX_MEM_MAP_REGIONS + one_pt;
291
292 /*
293 * We need to duplicate our page table once to have an emergency pt to
294 * resort to when splitting page tables later on
295 */
296 size *= 2;
297
298 /*
299 * We may need to split page tables later on if dcache settings change,
300 * so reserve up to 4 (random pick) page tables for that.
301 */
302 size += one_pt * 4;
303
304 return size;
305}
306
307void enable_caches(void)
308{
309 /* TODO: add TEE memmap region */
310
311 icache_enable();
312 dcache_enable();
313}
314
315int dram_init(void)
316{
317 gd->ram_size = PHYS_SDRAM_SIZE;
318
319 return 0;
320}
321
322#ifdef CONFIG_SERIAL_TAG
323void get_board_serial(struct tag_serialnr *serialnr)
324{
325 /* TODO */
326}
327#endif
328
Ye Liaadd6ca2021-08-07 16:00:50 +0800329static void set_core0_reset_vector(u32 entry)
Peng Fan9ef89ea2021-08-07 16:00:35 +0800330{
Ye Li610083e2021-08-07 16:00:48 +0800331 /* Update SIM1 DGO8 for reset vector base */
Ye Liaadd6ca2021-08-07 16:00:50 +0800332 writel(entry, SIM1_BASE_ADDR + 0x5c);
Ye Li610083e2021-08-07 16:00:48 +0800333
334 /* set update bit */
335 setbits_le32(SIM1_BASE_ADDR + 0x8, 0x1 << 24);
336
337 /* polling the ack */
338 while ((readl(SIM1_BASE_ADDR + 0x8) & (0x1 << 26)) == 0)
339 ;
340
341 /* clear the update */
342 clrbits_le32(SIM1_BASE_ADDR + 0x8, (0x1 << 24));
343
344 /* clear the ack by set 1 */
345 setbits_le32(SIM1_BASE_ADDR + 0x8, (0x1 << 26));
Ye Liaadd6ca2021-08-07 16:00:50 +0800346}
347
Peng Fan3912d4b2021-08-07 16:00:59 +0800348static int trdc_set_access(void)
Peng Fana443ec22021-08-07 16:00:58 +0800349{
350 /*
Peng Fan3912d4b2021-08-07 16:00:59 +0800351 * TRDC mgr + 4 MBC + 2 MRC.
352 * S400 should already configure when release RDC
353 * A35 only map non-secure region for pbridge0 and 1, set sec_access to false
Peng Fana443ec22021-08-07 16:00:58 +0800354 */
Peng Fan3912d4b2021-08-07 16:00:59 +0800355 trdc_mbc_set_access(2, 7, 0, 49, false);
356 trdc_mbc_set_access(2, 7, 0, 50, false);
357 trdc_mbc_set_access(2, 7, 0, 51, false);
358 trdc_mbc_set_access(2, 7, 0, 52, false);
359 trdc_mbc_set_access(2, 7, 0, 53, false);
360 trdc_mbc_set_access(2, 7, 0, 54, false);
Peng Fana443ec22021-08-07 16:00:58 +0800361
Peng Fan3912d4b2021-08-07 16:00:59 +0800362 /* CGC0: PBridge0 slot 47 */
363 trdc_mbc_set_access(2, 7, 0, 47, false);
Peng Fana443ec22021-08-07 16:00:58 +0800364
Peng Fan3912d4b2021-08-07 16:00:59 +0800365 /* Iomuxc0: : PBridge1 slot 33 */
366 trdc_mbc_set_access(2, 7, 1, 33, false);
Peng Fana443ec22021-08-07 16:00:58 +0800367
368 return 0;
369}
370
Ye Liaadd6ca2021-08-07 16:00:50 +0800371int arch_cpu_init(void)
372{
373 if (IS_ENABLED(CONFIG_SPL_BUILD)) {
Ye Liba472a22021-08-07 16:00:55 +0800374 /* Disable wdog */
375 init_wdog();
376
Peng Fana443ec22021-08-07 16:00:58 +0800377 if (get_boot_mode() == SINGLE_BOOT) {
Peng Fan3df56492021-08-07 16:00:57 +0800378 release_rdc(RDC_TRDC);
Peng Fana443ec22021-08-07 16:00:58 +0800379 trdc_set_access();
380 /* LPAV to APD */
381 setbits_le32(0x2802B044, BIT(7));
382 /* GPU 2D/3D to APD */
383 setbits_le32(0x2802B04C, BIT(1) | BIT(2));
384 }
Peng Fan3df56492021-08-07 16:00:57 +0800385
Ye Liba472a22021-08-07 16:00:55 +0800386 /* release xrdc, then allow A35 to write SRAM2 */
Peng Fan3df56492021-08-07 16:00:57 +0800387 release_rdc(RDC_XRDC);
Ye Liba472a22021-08-07 16:00:55 +0800388 xrdc_mrc_region_set_access(2, CONFIG_SPL_TEXT_BASE, 0xE00);
389
Ye Liaadd6ca2021-08-07 16:00:50 +0800390 clock_init();
391 } else {
392 /* reconfigure core0 reset vector to ROM */
393 set_core0_reset_vector(0x1000);
394 }
395
396 return 0;
397}
398
399#if defined(CONFIG_SPL_BUILD)
400__weak void __noreturn jump_to_image_no_args(struct spl_image_info *spl_image)
401{
402 debug("image entry point: 0x%lx\n", spl_image->entry_point);
403
404 set_core0_reset_vector((u32)spl_image->entry_point);
Ye Li610083e2021-08-07 16:00:48 +0800405
406 /* Enable the 512KB cache */
407 setbits_le32(SIM1_BASE_ADDR + 0x30, (0x1 << 4));
408
409 /* reset core */
410 setbits_le32(SIM1_BASE_ADDR + 0x30, (0x1 << 16));
411
412 while (1)
413 ;
414}
415#endif