blob: 62c02a6223e17907af29eb5481b0310c8dcc7792 [file] [log] [blame]
Peng Fan5c2218a2021-08-07 16:00:31 +08001// SPDX-License-Identifier: GPL-2.0+
2/*
3 * Copyright 2021 NXP
4 */
5
Peng Fan72530162021-08-07 16:00:33 +08006#include <asm/io.h>
7#include <asm/arch/clock.h>
8#include <asm/arch/imx-regs.h>
Peng Fan5c2218a2021-08-07 16:00:31 +08009#include <asm/arch/sys_proto.h>
Peng Fanb15705a2021-08-07 16:00:35 +080010#include <asm/armv8/mmu.h>
Peng Fan72530162021-08-07 16:00:33 +080011#include <asm/mach-imx/boot_mode.h>
Ye Li6dd43022021-08-07 16:00:48 +080012#include <efi_loader.h>
13#include <spl.h>
Ye Li853cc9d2021-08-07 16:00:55 +080014#include <asm/arch/s400_api.h>
15#include <asm/arch/mu_hal.h>
16#include <cpu_func.h>
17#include <asm/setup.h>
Peng Fan5c2218a2021-08-07 16:00:31 +080018
Peng Fanb15705a2021-08-07 16:00:35 +080019DECLARE_GLOBAL_DATA_PTR;
20
Ye Li7a71c612021-08-07 16:00:39 +080021struct rom_api *g_rom_api = (struct rom_api *)0x1980;
22
Peng Fan5c2218a2021-08-07 16:00:31 +080023u32 get_cpu_rev(void)
24{
25 return (MXC_CPU_IMX8ULP << 12) | CHIP_REV_1_0;
26}
Peng Fan72530162021-08-07 16:00:33 +080027
28enum bt_mode get_boot_mode(void)
29{
30 u32 bt0_cfg = 0;
31
Ye Li0be116e2021-08-07 16:00:47 +080032 bt0_cfg = readl(CMC1_BASE_ADDR + 0xa0);
Peng Fan72530162021-08-07 16:00:33 +080033 bt0_cfg &= (BT0CFG_LPBOOT_MASK | BT0CFG_DUALBOOT_MASK);
34
35 if (!(bt0_cfg & BT0CFG_LPBOOT_MASK)) {
36 /* No low power boot */
37 if (bt0_cfg & BT0CFG_DUALBOOT_MASK)
38 return DUAL_BOOT;
39 else
40 return SINGLE_BOOT;
41 }
42
43 return LOW_POWER_BOOT;
44}
45
Peng Fanaf4f3b32021-08-07 16:00:34 +080046#define CMC_SRS_TAMPER BIT(31)
47#define CMC_SRS_SECURITY BIT(30)
48#define CMC_SRS_TZWDG BIT(29)
49#define CMC_SRS_JTAG_RST BIT(28)
50#define CMC_SRS_CORE1 BIT(16)
51#define CMC_SRS_LOCKUP BIT(15)
52#define CMC_SRS_SW BIT(14)
53#define CMC_SRS_WDG BIT(13)
54#define CMC_SRS_PIN_RESET BIT(8)
55#define CMC_SRS_WARM BIT(4)
56#define CMC_SRS_HVD BIT(3)
57#define CMC_SRS_LVD BIT(2)
58#define CMC_SRS_POR BIT(1)
59#define CMC_SRS_WUP BIT(0)
60
61static u32 reset_cause = -1;
62
63static char *get_reset_cause(char *ret)
64{
65 u32 cause1, cause = 0, srs = 0;
Peng Fanb15705a2021-08-07 16:00:35 +080066 void __iomem *reg_ssrs = (void __iomem *)(CMC1_BASE_ADDR + 0x88);
67 void __iomem *reg_srs = (void __iomem *)(CMC1_BASE_ADDR + 0x80);
Peng Fanaf4f3b32021-08-07 16:00:34 +080068
69 if (!ret)
70 return "null";
71
72 srs = readl(reg_srs);
73 cause1 = readl(reg_ssrs);
74
75 reset_cause = cause1;
76
77 cause = cause1 & (CMC_SRS_POR | CMC_SRS_WUP | CMC_SRS_WARM);
78
79 switch (cause) {
80 case CMC_SRS_POR:
81 sprintf(ret, "%s", "POR");
82 break;
83 case CMC_SRS_WUP:
84 sprintf(ret, "%s", "WUP");
85 break;
86 case CMC_SRS_WARM:
87 cause = cause1 & (CMC_SRS_WDG | CMC_SRS_SW |
88 CMC_SRS_JTAG_RST);
89 switch (cause) {
90 case CMC_SRS_WDG:
91 sprintf(ret, "%s", "WARM-WDG");
92 break;
93 case CMC_SRS_SW:
94 sprintf(ret, "%s", "WARM-SW");
95 break;
96 case CMC_SRS_JTAG_RST:
97 sprintf(ret, "%s", "WARM-JTAG");
98 break;
99 default:
100 sprintf(ret, "%s", "WARM-UNKN");
101 break;
102 }
103 break;
104 default:
105 sprintf(ret, "%s-%X", "UNKN", cause1);
106 break;
107 }
108
109 debug("[%X] SRS[%X] %X - ", cause1, srs, srs ^ cause1);
110 return ret;
111}
112
Peng Fan72530162021-08-07 16:00:33 +0800113#if defined(CONFIG_DISPLAY_CPUINFO)
114const char *get_imx_type(u32 imxtype)
115{
116 return "8ULP";
117}
118
119int print_cpuinfo(void)
120{
121 u32 cpurev;
122 char cause[18];
123
124 cpurev = get_cpu_rev();
125
126 printf("CPU: Freescale i.MX%s rev%d.%d at %d MHz\n",
127 get_imx_type((cpurev & 0xFF000) >> 12),
128 (cpurev & 0x000F0) >> 4, (cpurev & 0x0000F) >> 0,
129 mxc_get_clock(MXC_ARM_CLK) / 1000000);
130
Peng Fanaf4f3b32021-08-07 16:00:34 +0800131 printf("Reset cause: %s\n", get_reset_cause(cause));
132
Peng Fan72530162021-08-07 16:00:33 +0800133 printf("Boot mode: ");
134 switch (get_boot_mode()) {
135 case LOW_POWER_BOOT:
136 printf("Low power boot\n");
137 break;
138 case DUAL_BOOT:
139 printf("Dual boot\n");
140 break;
141 case SINGLE_BOOT:
142 default:
143 printf("Single boot\n");
144 break;
145 }
146
147 return 0;
148}
149#endif
Peng Fanb15705a2021-08-07 16:00:35 +0800150
Peng Fanc84bc102021-08-07 16:00:49 +0800151#define UNLOCK_WORD0 0xC520 /* 1st unlock word */
152#define UNLOCK_WORD1 0xD928 /* 2nd unlock word */
153#define REFRESH_WORD0 0xA602 /* 1st refresh word */
154#define REFRESH_WORD1 0xB480 /* 2nd refresh word */
155
156static void disable_wdog(void __iomem *wdog_base)
157{
158 u32 val_cs = readl(wdog_base + 0x00);
159
160 if (!(val_cs & 0x80))
161 return;
162
163 dmb();
164 __raw_writel(REFRESH_WORD0, (wdog_base + 0x04)); /* Refresh the CNT */
165 __raw_writel(REFRESH_WORD1, (wdog_base + 0x04));
166 dmb();
167
168 if (!(val_cs & 800)) {
169 dmb();
170 __raw_writel(UNLOCK_WORD0, (wdog_base + 0x04));
171 __raw_writel(UNLOCK_WORD1, (wdog_base + 0x04));
172 dmb();
173
174 while (!(readl(wdog_base + 0x00) & 0x800))
175 ;
176 }
177 writel(0x0, (wdog_base + 0x0C)); /* Set WIN to 0 */
178 writel(0x400, (wdog_base + 0x08)); /* Set timeout to default 0x400 */
179 writel(0x120, (wdog_base + 0x00)); /* Disable it and set update */
180
181 while (!(readl(wdog_base + 0x00) & 0x400))
182 ;
183}
184
Peng Fanb15705a2021-08-07 16:00:35 +0800185void init_wdog(void)
186{
Peng Fanc84bc102021-08-07 16:00:49 +0800187 disable_wdog((void __iomem *)WDG3_RBASE);
Peng Fanb15705a2021-08-07 16:00:35 +0800188}
189
190static struct mm_region imx8ulp_arm64_mem_map[] = {
191 {
192 /* ROM */
193 .virt = 0x0,
194 .phys = 0x0,
195 .size = 0x40000UL,
196 .attrs = PTE_BLOCK_MEMTYPE(MT_NORMAL) |
197 PTE_BLOCK_OUTER_SHARE
198 },
199 {
200 /* FLEXSPI0 */
201 .virt = 0x04000000,
202 .phys = 0x04000000,
203 .size = 0x08000000UL,
204 .attrs = PTE_BLOCK_MEMTYPE(MT_DEVICE_NGNRNE) |
205 PTE_BLOCK_NON_SHARE |
206 PTE_BLOCK_PXN | PTE_BLOCK_UXN
207 },
208 {
209 /* SSRAM (align with 2M) */
210 .virt = 0x1FE00000UL,
211 .phys = 0x1FE00000UL,
212 .size = 0x400000UL,
213 .attrs = PTE_BLOCK_MEMTYPE(MT_NORMAL) |
214 PTE_BLOCK_OUTER_SHARE |
215 PTE_BLOCK_PXN | PTE_BLOCK_UXN
216 }, {
217 /* SRAM1 (align with 2M) */
218 .virt = 0x21000000UL,
219 .phys = 0x21000000UL,
220 .size = 0x200000UL,
221 .attrs = PTE_BLOCK_MEMTYPE(MT_NORMAL) |
222 PTE_BLOCK_OUTER_SHARE |
223 PTE_BLOCK_PXN | PTE_BLOCK_UXN
224 }, {
225 /* SRAM0 (align with 2M) */
226 .virt = 0x22000000UL,
227 .phys = 0x22000000UL,
228 .size = 0x200000UL,
229 .attrs = PTE_BLOCK_MEMTYPE(MT_NORMAL) |
230 PTE_BLOCK_OUTER_SHARE |
231 PTE_BLOCK_PXN | PTE_BLOCK_UXN
232 }, {
233 /* Peripherals */
234 .virt = 0x27000000UL,
235 .phys = 0x27000000UL,
236 .size = 0x3000000UL,
237 .attrs = PTE_BLOCK_MEMTYPE(MT_DEVICE_NGNRNE) |
238 PTE_BLOCK_NON_SHARE |
239 PTE_BLOCK_PXN | PTE_BLOCK_UXN
240 }, {
241 /* Peripherals */
242 .virt = 0x2D000000UL,
243 .phys = 0x2D000000UL,
244 .size = 0x1600000UL,
245 .attrs = PTE_BLOCK_MEMTYPE(MT_DEVICE_NGNRNE) |
246 PTE_BLOCK_NON_SHARE |
247 PTE_BLOCK_PXN | PTE_BLOCK_UXN
248 }, {
249 /* FLEXSPI1-2 */
250 .virt = 0x40000000UL,
251 .phys = 0x40000000UL,
252 .size = 0x40000000UL,
253 .attrs = PTE_BLOCK_MEMTYPE(MT_DEVICE_NGNRNE) |
254 PTE_BLOCK_NON_SHARE |
255 PTE_BLOCK_PXN | PTE_BLOCK_UXN
256 }, {
257 /* DRAM1 */
258 .virt = 0x80000000UL,
259 .phys = 0x80000000UL,
260 .size = PHYS_SDRAM_SIZE,
261 .attrs = PTE_BLOCK_MEMTYPE(MT_NORMAL) |
262 PTE_BLOCK_OUTER_SHARE
263 }, {
264 /*
265 * empty entrie to split table entry 5
266 * if needed when TEEs are used
267 */
268 0,
269 }, {
270 /* List terminator */
271 0,
272 }
273};
274
275struct mm_region *mem_map = imx8ulp_arm64_mem_map;
276
277/* simplify the page table size to enhance boot speed */
278#define MAX_PTE_ENTRIES 512
279#define MAX_MEM_MAP_REGIONS 16
280u64 get_page_table_size(void)
281{
282 u64 one_pt = MAX_PTE_ENTRIES * sizeof(u64);
283 u64 size = 0;
284
285 /*
286 * For each memory region, the max table size:
287 * 2 level 3 tables + 2 level 2 tables + 1 level 1 table
288 */
289 size = (2 + 2 + 1) * one_pt * MAX_MEM_MAP_REGIONS + one_pt;
290
291 /*
292 * We need to duplicate our page table once to have an emergency pt to
293 * resort to when splitting page tables later on
294 */
295 size *= 2;
296
297 /*
298 * We may need to split page tables later on if dcache settings change,
299 * so reserve up to 4 (random pick) page tables for that.
300 */
301 size += one_pt * 4;
302
303 return size;
304}
305
306void enable_caches(void)
307{
308 /* TODO: add TEE memmap region */
309
310 icache_enable();
311 dcache_enable();
312}
313
314int dram_init(void)
315{
316 gd->ram_size = PHYS_SDRAM_SIZE;
317
318 return 0;
319}
320
321#ifdef CONFIG_SERIAL_TAG
322void get_board_serial(struct tag_serialnr *serialnr)
323{
324 /* TODO */
325}
326#endif
327
Ye Li6ee435eb2021-08-07 16:00:50 +0800328static void set_core0_reset_vector(u32 entry)
Peng Fanb15705a2021-08-07 16:00:35 +0800329{
Ye Li6dd43022021-08-07 16:00:48 +0800330 /* Update SIM1 DGO8 for reset vector base */
Ye Li6ee435eb2021-08-07 16:00:50 +0800331 writel(entry, SIM1_BASE_ADDR + 0x5c);
Ye Li6dd43022021-08-07 16:00:48 +0800332
333 /* set update bit */
334 setbits_le32(SIM1_BASE_ADDR + 0x8, 0x1 << 24);
335
336 /* polling the ack */
337 while ((readl(SIM1_BASE_ADDR + 0x8) & (0x1 << 26)) == 0)
338 ;
339
340 /* clear the update */
341 clrbits_le32(SIM1_BASE_ADDR + 0x8, (0x1 << 24));
342
343 /* clear the ack by set 1 */
344 setbits_le32(SIM1_BASE_ADDR + 0x8, (0x1 << 26));
Ye Li6ee435eb2021-08-07 16:00:50 +0800345}
346
Ye Li853cc9d2021-08-07 16:00:55 +0800347static int release_xrdc(void)
348{
349 ulong s_mu_base = 0x27020000UL;
350 struct imx8ulp_s400_msg msg;
351 int ret;
352
353 msg.version = AHAB_VERSION;
354 msg.tag = AHAB_CMD_TAG;
355 msg.size = 2;
356 msg.command = AHAB_RELEASE_RDC_REQ_CID;
357 msg.data[0] = (0x78 << 8) | 0x2; /* A35 XRDC */
358
359 mu_hal_init(s_mu_base);
360 mu_hal_sendmsg(s_mu_base, 0, *((u32 *)&msg));
361 mu_hal_sendmsg(s_mu_base, 1, msg.data[0]);
362
363 ret = mu_hal_receivemsg(s_mu_base, 0, (u32 *)&msg);
364 if (!ret) {
365 ret = mu_hal_receivemsg(s_mu_base, 1, &msg.data[0]);
366 if (!ret)
367 return ret;
368
369 if ((msg.data[0] & 0xff) == 0)
370 return 0;
371 else
372 return -EIO;
373 }
374
375 return ret;
376}
377
378static void xrdc_mrc_region_set_access(int mrc_index, u32 addr, u32 access)
379{
380 ulong xrdc_base = 0x292f0000, off;
381 u32 mrgd[5];
382 u8 mrcfg, j, region_num;
383 u8 dsel;
384
385 mrcfg = readb(xrdc_base + 0x140 + mrc_index);
386 region_num = mrcfg & 0x1f;
387
388 for (j = 0; j < region_num; j++) {
389 off = 0x2000 + mrc_index * 0x200 + j * 0x20;
390
391 mrgd[0] = readl(xrdc_base + off);
392 mrgd[1] = readl(xrdc_base + off + 4);
393 mrgd[2] = readl(xrdc_base + off + 8);
394 mrgd[3] = readl(xrdc_base + off + 0xc);
395 mrgd[4] = readl(xrdc_base + off + 0x10);
396
397 debug("MRC [%u][%u]\n", mrc_index, j);
398 debug("0x%x, 0x%x, 0x%x, 0x%x, 0x%x\n",
399 mrgd[0], mrgd[1], mrgd[2], mrgd[3], mrgd[4]);
400
401 /* hit */
402 if (addr >= mrgd[0] && addr <= mrgd[1]) {
403 /* find domain 7 DSEL */
404 dsel = (mrgd[2] >> 21) & 0x7;
405 if (dsel == 1) {
406 mrgd[4] &= ~0xFFF;
407 mrgd[4] |= (access & 0xFFF);
408 } else if (dsel == 2) {
409 mrgd[4] &= ~0xFFF0000;
410 mrgd[4] |= ((access & 0xFFF) << 16);
411 }
412
413 /* not handle other cases, since S400 only set ACCESS1 and 2 */
414 writel(mrgd[4], xrdc_base + off + 0x10);
415 return;
416 }
417 }
418}
419
Ye Li6ee435eb2021-08-07 16:00:50 +0800420int arch_cpu_init(void)
421{
422 if (IS_ENABLED(CONFIG_SPL_BUILD)) {
Ye Li853cc9d2021-08-07 16:00:55 +0800423 /* Disable wdog */
424 init_wdog();
425
426 /* release xrdc, then allow A35 to write SRAM2 */
427 release_xrdc();
428 xrdc_mrc_region_set_access(2, CONFIG_SPL_TEXT_BASE, 0xE00);
429
Ye Li6ee435eb2021-08-07 16:00:50 +0800430 clock_init();
431 } else {
432 /* reconfigure core0 reset vector to ROM */
433 set_core0_reset_vector(0x1000);
434 }
435
436 return 0;
437}
438
439#if defined(CONFIG_SPL_BUILD)
440__weak void __noreturn jump_to_image_no_args(struct spl_image_info *spl_image)
441{
442 debug("image entry point: 0x%lx\n", spl_image->entry_point);
443
444 set_core0_reset_vector((u32)spl_image->entry_point);
Ye Li6dd43022021-08-07 16:00:48 +0800445
446 /* Enable the 512KB cache */
447 setbits_le32(SIM1_BASE_ADDR + 0x30, (0x1 << 4));
448
449 /* reset core */
450 setbits_le32(SIM1_BASE_ADDR + 0x30, (0x1 << 16));
451
452 while (1)
453 ;
454}
455#endif