blob: e5985c4cf1f7776ba4370960ae3d7fc1831725b5 [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 Li88044c72021-08-07 16:01:01 +080012#include <asm/global_data.h>
Ye Li6dd43022021-08-07 16:00:48 +080013#include <efi_loader.h>
14#include <spl.h>
Peng Fan9c87e462021-08-07 16:00:59 +080015#include <asm/arch/rdc.h>
Ye Li853cc9d2021-08-07 16:00:55 +080016#include <asm/arch/s400_api.h>
17#include <asm/arch/mu_hal.h>
18#include <cpu_func.h>
19#include <asm/setup.h>
Ye Li7bea5b02021-08-07 16:01:00 +080020#include <dm.h>
21#include <dm/device-internal.h>
22#include <dm/lists.h>
23#include <dm/uclass.h>
24#include <dm/device.h>
25#include <dm/uclass-internal.h>
Peng Fan5c2218a2021-08-07 16:00:31 +080026
Peng Fanb15705a2021-08-07 16:00:35 +080027DECLARE_GLOBAL_DATA_PTR;
28
Ye Li7a71c612021-08-07 16:00:39 +080029struct rom_api *g_rom_api = (struct rom_api *)0x1980;
30
Ye Li88044c72021-08-07 16:01:01 +080031enum boot_device get_boot_device(void)
32{
33 volatile gd_t *pgd = gd;
34 int ret;
35 u32 boot;
36 u16 boot_type;
37 u8 boot_instance;
38 enum boot_device boot_dev = SD1_BOOT;
39
40 ret = g_rom_api->query_boot_infor(QUERY_BT_DEV, &boot,
41 ((uintptr_t)&boot) ^ QUERY_BT_DEV);
42 set_gd(pgd);
43
44 if (ret != ROM_API_OKAY) {
45 puts("ROMAPI: failure at query_boot_info\n");
46 return -1;
47 }
48
49 boot_type = boot >> 16;
50 boot_instance = (boot >> 8) & 0xff;
51
52 switch (boot_type) {
53 case BT_DEV_TYPE_SD:
54 boot_dev = boot_instance + SD1_BOOT;
55 break;
56 case BT_DEV_TYPE_MMC:
57 boot_dev = boot_instance + MMC1_BOOT;
58 break;
59 case BT_DEV_TYPE_NAND:
60 boot_dev = NAND_BOOT;
61 break;
62 case BT_DEV_TYPE_FLEXSPINOR:
63 boot_dev = QSPI_BOOT;
64 break;
65 case BT_DEV_TYPE_USB:
66 boot_dev = USB_BOOT;
67 break;
68 default:
69 break;
70 }
71
72 return boot_dev;
73}
74
75bool is_usb_boot(void)
76{
77 return get_boot_device() == USB_BOOT;
78}
79
80#ifdef CONFIG_ENV_IS_IN_MMC
81__weak int board_mmc_get_env_dev(int devno)
82{
83 return devno;
84}
85
86int mmc_get_env_dev(void)
87{
88 volatile gd_t *pgd = gd;
89 int ret;
90 u32 boot;
91 u16 boot_type;
92 u8 boot_instance;
93
94 ret = g_rom_api->query_boot_infor(QUERY_BT_DEV, &boot,
95 ((uintptr_t)&boot) ^ QUERY_BT_DEV);
96 set_gd(pgd);
97
98 if (ret != ROM_API_OKAY) {
99 puts("ROMAPI: failure at query_boot_info\n");
100 return CONFIG_SYS_MMC_ENV_DEV;
101 }
102
103 boot_type = boot >> 16;
104 boot_instance = (boot >> 8) & 0xff;
105
106 /* If not boot from sd/mmc, use default value */
107 if (boot_type != BOOT_TYPE_SD && boot_type != BOOT_TYPE_MMC)
108 return env_get_ulong("mmcdev", 10, CONFIG_SYS_MMC_ENV_DEV);
109
110 return board_mmc_get_env_dev(boot_instance);
111}
112#endif
113
Peng Fan5c2218a2021-08-07 16:00:31 +0800114u32 get_cpu_rev(void)
115{
116 return (MXC_CPU_IMX8ULP << 12) | CHIP_REV_1_0;
117}
Peng Fan72530162021-08-07 16:00:33 +0800118
119enum bt_mode get_boot_mode(void)
120{
121 u32 bt0_cfg = 0;
122
Ye Li0be116e2021-08-07 16:00:47 +0800123 bt0_cfg = readl(CMC1_BASE_ADDR + 0xa0);
Peng Fan72530162021-08-07 16:00:33 +0800124 bt0_cfg &= (BT0CFG_LPBOOT_MASK | BT0CFG_DUALBOOT_MASK);
125
126 if (!(bt0_cfg & BT0CFG_LPBOOT_MASK)) {
127 /* No low power boot */
128 if (bt0_cfg & BT0CFG_DUALBOOT_MASK)
129 return DUAL_BOOT;
130 else
131 return SINGLE_BOOT;
132 }
133
134 return LOW_POWER_BOOT;
135}
136
Peng Fanaf4f3b32021-08-07 16:00:34 +0800137#define CMC_SRS_TAMPER BIT(31)
138#define CMC_SRS_SECURITY BIT(30)
139#define CMC_SRS_TZWDG BIT(29)
140#define CMC_SRS_JTAG_RST BIT(28)
141#define CMC_SRS_CORE1 BIT(16)
142#define CMC_SRS_LOCKUP BIT(15)
143#define CMC_SRS_SW BIT(14)
144#define CMC_SRS_WDG BIT(13)
145#define CMC_SRS_PIN_RESET BIT(8)
146#define CMC_SRS_WARM BIT(4)
147#define CMC_SRS_HVD BIT(3)
148#define CMC_SRS_LVD BIT(2)
149#define CMC_SRS_POR BIT(1)
150#define CMC_SRS_WUP BIT(0)
151
152static u32 reset_cause = -1;
153
154static char *get_reset_cause(char *ret)
155{
156 u32 cause1, cause = 0, srs = 0;
Peng Fanb15705a2021-08-07 16:00:35 +0800157 void __iomem *reg_ssrs = (void __iomem *)(CMC1_BASE_ADDR + 0x88);
158 void __iomem *reg_srs = (void __iomem *)(CMC1_BASE_ADDR + 0x80);
Peng Fanaf4f3b32021-08-07 16:00:34 +0800159
160 if (!ret)
161 return "null";
162
163 srs = readl(reg_srs);
164 cause1 = readl(reg_ssrs);
165
166 reset_cause = cause1;
167
168 cause = cause1 & (CMC_SRS_POR | CMC_SRS_WUP | CMC_SRS_WARM);
169
170 switch (cause) {
171 case CMC_SRS_POR:
172 sprintf(ret, "%s", "POR");
173 break;
174 case CMC_SRS_WUP:
175 sprintf(ret, "%s", "WUP");
176 break;
177 case CMC_SRS_WARM:
178 cause = cause1 & (CMC_SRS_WDG | CMC_SRS_SW |
179 CMC_SRS_JTAG_RST);
180 switch (cause) {
181 case CMC_SRS_WDG:
182 sprintf(ret, "%s", "WARM-WDG");
183 break;
184 case CMC_SRS_SW:
185 sprintf(ret, "%s", "WARM-SW");
186 break;
187 case CMC_SRS_JTAG_RST:
188 sprintf(ret, "%s", "WARM-JTAG");
189 break;
190 default:
191 sprintf(ret, "%s", "WARM-UNKN");
192 break;
193 }
194 break;
195 default:
196 sprintf(ret, "%s-%X", "UNKN", cause1);
197 break;
198 }
199
200 debug("[%X] SRS[%X] %X - ", cause1, srs, srs ^ cause1);
201 return ret;
202}
203
Peng Fan72530162021-08-07 16:00:33 +0800204#if defined(CONFIG_DISPLAY_CPUINFO)
205const char *get_imx_type(u32 imxtype)
206{
207 return "8ULP";
208}
209
210int print_cpuinfo(void)
211{
212 u32 cpurev;
213 char cause[18];
214
215 cpurev = get_cpu_rev();
216
217 printf("CPU: Freescale i.MX%s rev%d.%d at %d MHz\n",
218 get_imx_type((cpurev & 0xFF000) >> 12),
219 (cpurev & 0x000F0) >> 4, (cpurev & 0x0000F) >> 0,
220 mxc_get_clock(MXC_ARM_CLK) / 1000000);
221
Peng Fanaf4f3b32021-08-07 16:00:34 +0800222 printf("Reset cause: %s\n", get_reset_cause(cause));
223
Peng Fan72530162021-08-07 16:00:33 +0800224 printf("Boot mode: ");
225 switch (get_boot_mode()) {
226 case LOW_POWER_BOOT:
227 printf("Low power boot\n");
228 break;
229 case DUAL_BOOT:
230 printf("Dual boot\n");
231 break;
232 case SINGLE_BOOT:
233 default:
234 printf("Single boot\n");
235 break;
236 }
237
238 return 0;
239}
240#endif
Peng Fanb15705a2021-08-07 16:00:35 +0800241
Peng Fanc84bc102021-08-07 16:00:49 +0800242#define UNLOCK_WORD0 0xC520 /* 1st unlock word */
243#define UNLOCK_WORD1 0xD928 /* 2nd unlock word */
244#define REFRESH_WORD0 0xA602 /* 1st refresh word */
245#define REFRESH_WORD1 0xB480 /* 2nd refresh word */
246
247static void disable_wdog(void __iomem *wdog_base)
248{
249 u32 val_cs = readl(wdog_base + 0x00);
250
251 if (!(val_cs & 0x80))
252 return;
253
254 dmb();
255 __raw_writel(REFRESH_WORD0, (wdog_base + 0x04)); /* Refresh the CNT */
256 __raw_writel(REFRESH_WORD1, (wdog_base + 0x04));
257 dmb();
258
259 if (!(val_cs & 800)) {
260 dmb();
261 __raw_writel(UNLOCK_WORD0, (wdog_base + 0x04));
262 __raw_writel(UNLOCK_WORD1, (wdog_base + 0x04));
263 dmb();
264
265 while (!(readl(wdog_base + 0x00) & 0x800))
266 ;
267 }
268 writel(0x0, (wdog_base + 0x0C)); /* Set WIN to 0 */
269 writel(0x400, (wdog_base + 0x08)); /* Set timeout to default 0x400 */
270 writel(0x120, (wdog_base + 0x00)); /* Disable it and set update */
271
272 while (!(readl(wdog_base + 0x00) & 0x400))
273 ;
274}
275
Peng Fanb15705a2021-08-07 16:00:35 +0800276void init_wdog(void)
277{
Peng Fanc84bc102021-08-07 16:00:49 +0800278 disable_wdog((void __iomem *)WDG3_RBASE);
Peng Fanb15705a2021-08-07 16:00:35 +0800279}
280
281static struct mm_region imx8ulp_arm64_mem_map[] = {
282 {
283 /* ROM */
284 .virt = 0x0,
285 .phys = 0x0,
286 .size = 0x40000UL,
287 .attrs = PTE_BLOCK_MEMTYPE(MT_NORMAL) |
288 PTE_BLOCK_OUTER_SHARE
289 },
290 {
291 /* FLEXSPI0 */
292 .virt = 0x04000000,
293 .phys = 0x04000000,
294 .size = 0x08000000UL,
295 .attrs = PTE_BLOCK_MEMTYPE(MT_DEVICE_NGNRNE) |
296 PTE_BLOCK_NON_SHARE |
297 PTE_BLOCK_PXN | PTE_BLOCK_UXN
298 },
299 {
300 /* SSRAM (align with 2M) */
301 .virt = 0x1FE00000UL,
302 .phys = 0x1FE00000UL,
303 .size = 0x400000UL,
304 .attrs = PTE_BLOCK_MEMTYPE(MT_NORMAL) |
305 PTE_BLOCK_OUTER_SHARE |
306 PTE_BLOCK_PXN | PTE_BLOCK_UXN
307 }, {
308 /* SRAM1 (align with 2M) */
309 .virt = 0x21000000UL,
310 .phys = 0x21000000UL,
311 .size = 0x200000UL,
312 .attrs = PTE_BLOCK_MEMTYPE(MT_NORMAL) |
313 PTE_BLOCK_OUTER_SHARE |
314 PTE_BLOCK_PXN | PTE_BLOCK_UXN
315 }, {
316 /* SRAM0 (align with 2M) */
317 .virt = 0x22000000UL,
318 .phys = 0x22000000UL,
319 .size = 0x200000UL,
320 .attrs = PTE_BLOCK_MEMTYPE(MT_NORMAL) |
321 PTE_BLOCK_OUTER_SHARE |
322 PTE_BLOCK_PXN | PTE_BLOCK_UXN
323 }, {
324 /* Peripherals */
325 .virt = 0x27000000UL,
326 .phys = 0x27000000UL,
327 .size = 0x3000000UL,
328 .attrs = PTE_BLOCK_MEMTYPE(MT_DEVICE_NGNRNE) |
329 PTE_BLOCK_NON_SHARE |
330 PTE_BLOCK_PXN | PTE_BLOCK_UXN
331 }, {
332 /* Peripherals */
333 .virt = 0x2D000000UL,
334 .phys = 0x2D000000UL,
335 .size = 0x1600000UL,
336 .attrs = PTE_BLOCK_MEMTYPE(MT_DEVICE_NGNRNE) |
337 PTE_BLOCK_NON_SHARE |
338 PTE_BLOCK_PXN | PTE_BLOCK_UXN
339 }, {
340 /* FLEXSPI1-2 */
341 .virt = 0x40000000UL,
342 .phys = 0x40000000UL,
343 .size = 0x40000000UL,
344 .attrs = PTE_BLOCK_MEMTYPE(MT_DEVICE_NGNRNE) |
345 PTE_BLOCK_NON_SHARE |
346 PTE_BLOCK_PXN | PTE_BLOCK_UXN
347 }, {
348 /* DRAM1 */
349 .virt = 0x80000000UL,
350 .phys = 0x80000000UL,
351 .size = PHYS_SDRAM_SIZE,
352 .attrs = PTE_BLOCK_MEMTYPE(MT_NORMAL) |
353 PTE_BLOCK_OUTER_SHARE
354 }, {
355 /*
356 * empty entrie to split table entry 5
357 * if needed when TEEs are used
358 */
359 0,
360 }, {
361 /* List terminator */
362 0,
363 }
364};
365
366struct mm_region *mem_map = imx8ulp_arm64_mem_map;
367
368/* simplify the page table size to enhance boot speed */
369#define MAX_PTE_ENTRIES 512
370#define MAX_MEM_MAP_REGIONS 16
371u64 get_page_table_size(void)
372{
373 u64 one_pt = MAX_PTE_ENTRIES * sizeof(u64);
374 u64 size = 0;
375
376 /*
377 * For each memory region, the max table size:
378 * 2 level 3 tables + 2 level 2 tables + 1 level 1 table
379 */
380 size = (2 + 2 + 1) * one_pt * MAX_MEM_MAP_REGIONS + one_pt;
381
382 /*
383 * We need to duplicate our page table once to have an emergency pt to
384 * resort to when splitting page tables later on
385 */
386 size *= 2;
387
388 /*
389 * We may need to split page tables later on if dcache settings change,
390 * so reserve up to 4 (random pick) page tables for that.
391 */
392 size += one_pt * 4;
393
394 return size;
395}
396
397void enable_caches(void)
398{
399 /* TODO: add TEE memmap region */
400
401 icache_enable();
402 dcache_enable();
403}
404
405int dram_init(void)
406{
407 gd->ram_size = PHYS_SDRAM_SIZE;
408
409 return 0;
410}
411
412#ifdef CONFIG_SERIAL_TAG
413void get_board_serial(struct tag_serialnr *serialnr)
414{
Ye Li7bea5b02021-08-07 16:01:00 +0800415 u32 uid[4];
416 u32 res;
417 int ret;
418
419 ret = ahab_read_common_fuse(1, uid, 4, &res);
420 if (ret)
421 printf("ahab read fuse failed %d, 0x%x\n", ret, res);
422 else
423 printf("UID 0x%x,0x%x,0x%x,0x%x\n", uid[0], uid[1], uid[2], uid[3]);
424
425 serialnr->low = uid[0];
426 serialnr->high = uid[3];
Peng Fanb15705a2021-08-07 16:00:35 +0800427}
428#endif
429
Ye Li6ee435eb2021-08-07 16:00:50 +0800430static void set_core0_reset_vector(u32 entry)
Peng Fanb15705a2021-08-07 16:00:35 +0800431{
Ye Li6dd43022021-08-07 16:00:48 +0800432 /* Update SIM1 DGO8 for reset vector base */
Ye Li6ee435eb2021-08-07 16:00:50 +0800433 writel(entry, SIM1_BASE_ADDR + 0x5c);
Ye Li6dd43022021-08-07 16:00:48 +0800434
435 /* set update bit */
436 setbits_le32(SIM1_BASE_ADDR + 0x8, 0x1 << 24);
437
438 /* polling the ack */
439 while ((readl(SIM1_BASE_ADDR + 0x8) & (0x1 << 26)) == 0)
440 ;
441
442 /* clear the update */
443 clrbits_le32(SIM1_BASE_ADDR + 0x8, (0x1 << 24));
444
445 /* clear the ack by set 1 */
446 setbits_le32(SIM1_BASE_ADDR + 0x8, (0x1 << 26));
Ye Li6ee435eb2021-08-07 16:00:50 +0800447}
448
Peng Fan9c87e462021-08-07 16:00:59 +0800449static int trdc_set_access(void)
Peng Fanb5c41b12021-08-07 16:00:58 +0800450{
451 /*
Peng Fan9c87e462021-08-07 16:00:59 +0800452 * TRDC mgr + 4 MBC + 2 MRC.
453 * S400 should already configure when release RDC
454 * A35 only map non-secure region for pbridge0 and 1, set sec_access to false
Peng Fanb5c41b12021-08-07 16:00:58 +0800455 */
Peng Fan9c87e462021-08-07 16:00:59 +0800456 trdc_mbc_set_access(2, 7, 0, 49, false);
457 trdc_mbc_set_access(2, 7, 0, 50, false);
458 trdc_mbc_set_access(2, 7, 0, 51, false);
459 trdc_mbc_set_access(2, 7, 0, 52, false);
460 trdc_mbc_set_access(2, 7, 0, 53, false);
461 trdc_mbc_set_access(2, 7, 0, 54, false);
Peng Fanb5c41b12021-08-07 16:00:58 +0800462
Peng Fan9c87e462021-08-07 16:00:59 +0800463 /* CGC0: PBridge0 slot 47 */
464 trdc_mbc_set_access(2, 7, 0, 47, false);
Peng Fanb5c41b12021-08-07 16:00:58 +0800465
Peng Fan9c87e462021-08-07 16:00:59 +0800466 /* Iomuxc0: : PBridge1 slot 33 */
467 trdc_mbc_set_access(2, 7, 1, 33, false);
Peng Fanb5c41b12021-08-07 16:00:58 +0800468
469 return 0;
470}
471
Ye Li6ee435eb2021-08-07 16:00:50 +0800472int arch_cpu_init(void)
473{
474 if (IS_ENABLED(CONFIG_SPL_BUILD)) {
Ye Li853cc9d2021-08-07 16:00:55 +0800475 /* Disable wdog */
476 init_wdog();
477
Peng Fanb5c41b12021-08-07 16:00:58 +0800478 if (get_boot_mode() == SINGLE_BOOT) {
Peng Fanfa55b212021-08-07 16:00:57 +0800479 release_rdc(RDC_TRDC);
Peng Fanb5c41b12021-08-07 16:00:58 +0800480 trdc_set_access();
481 /* LPAV to APD */
482 setbits_le32(0x2802B044, BIT(7));
483 /* GPU 2D/3D to APD */
484 setbits_le32(0x2802B04C, BIT(1) | BIT(2));
Ye Li6739e632021-08-07 16:01:02 +0800485 /* DCNANO and MIPI_DSI to APD */
486 setbits_le32(0x2802B04C, BIT(1) | BIT(2) | BIT(3) | BIT(4));
Peng Fanb5c41b12021-08-07 16:00:58 +0800487 }
Peng Fanfa55b212021-08-07 16:00:57 +0800488
Ye Li853cc9d2021-08-07 16:00:55 +0800489 /* release xrdc, then allow A35 to write SRAM2 */
Peng Fanfa55b212021-08-07 16:00:57 +0800490 release_rdc(RDC_XRDC);
Ye Li853cc9d2021-08-07 16:00:55 +0800491 xrdc_mrc_region_set_access(2, CONFIG_SPL_TEXT_BASE, 0xE00);
492
Ye Li6ee435eb2021-08-07 16:00:50 +0800493 clock_init();
494 } else {
495 /* reconfigure core0 reset vector to ROM */
496 set_core0_reset_vector(0x1000);
497 }
498
499 return 0;
500}
501
Ye Li7bea5b02021-08-07 16:01:00 +0800502int arch_cpu_init_dm(void)
503{
504 struct udevice *devp;
505 int node, ret;
506
507 node = fdt_node_offset_by_compatible(gd->fdt_blob, -1, "fsl,imx8ulp-mu");
508
509 ret = uclass_get_device_by_of_offset(UCLASS_MISC, node, &devp);
510 if (ret) {
511 printf("could not get S400 mu %d\n", ret);
512 return ret;
513 }
514
515 return 0;
516}
517
Ye Li6ee435eb2021-08-07 16:00:50 +0800518#if defined(CONFIG_SPL_BUILD)
519__weak void __noreturn jump_to_image_no_args(struct spl_image_info *spl_image)
520{
521 debug("image entry point: 0x%lx\n", spl_image->entry_point);
522
523 set_core0_reset_vector((u32)spl_image->entry_point);
Ye Li6dd43022021-08-07 16:00:48 +0800524
525 /* Enable the 512KB cache */
526 setbits_le32(SIM1_BASE_ADDR + 0x30, (0x1 << 4));
527
528 /* reset core */
529 setbits_le32(SIM1_BASE_ADDR + 0x30, (0x1 << 16));
530
531 while (1)
532 ;
533}
534#endif