blob: c0f0df356ead7d007f90132deb6d9daff801c26f [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>
Simon Glassfc557362022-03-04 08:43:05 -070014#include <event.h>
Ye Li6dd43022021-08-07 16:00:48 +080015#include <spl.h>
Peng Fan9c87e462021-08-07 16:00:59 +080016#include <asm/arch/rdc.h>
Ye Li853cc9d2021-08-07 16:00:55 +080017#include <asm/arch/s400_api.h>
18#include <asm/arch/mu_hal.h>
19#include <cpu_func.h>
20#include <asm/setup.h>
Ye Li7bea5b02021-08-07 16:01:00 +080021#include <dm.h>
22#include <dm/device-internal.h>
23#include <dm/lists.h>
24#include <dm/uclass.h>
25#include <dm/device.h>
26#include <dm/uclass-internal.h>
Ye Li72012622021-10-29 09:46:15 +080027#include <fuse.h>
Alice Guof2c4a392021-10-29 09:46:32 +080028#include <thermal.h>
Ye Li2e9f15c2022-04-06 14:30:08 +080029#include <linux/iopoll.h>
Ye Li48836ae2022-04-06 14:30:30 +080030#include <env.h>
31#include <env_internal.h>
Peng Fan5c2218a2021-08-07 16:00:31 +080032
Peng Fanb15705a2021-08-07 16:00:35 +080033DECLARE_GLOBAL_DATA_PTR;
34
Ye Li7a71c612021-08-07 16:00:39 +080035struct rom_api *g_rom_api = (struct rom_api *)0x1980;
36
Ye Li88044c72021-08-07 16:01:01 +080037enum boot_device get_boot_device(void)
38{
Ye Li88044c72021-08-07 16:01:01 +080039 int ret;
40 u32 boot;
41 u16 boot_type;
42 u8 boot_instance;
43 enum boot_device boot_dev = SD1_BOOT;
44
Rasmus Villemoesb06f5a42022-06-20 10:53:22 +020045 ret = rom_api_query_boot_infor(QUERY_BT_DEV, &boot);
Ye Li88044c72021-08-07 16:01:01 +080046
47 if (ret != ROM_API_OKAY) {
48 puts("ROMAPI: failure at query_boot_info\n");
49 return -1;
50 }
51
52 boot_type = boot >> 16;
53 boot_instance = (boot >> 8) & 0xff;
54
55 switch (boot_type) {
56 case BT_DEV_TYPE_SD:
57 boot_dev = boot_instance + SD1_BOOT;
58 break;
59 case BT_DEV_TYPE_MMC:
60 boot_dev = boot_instance + MMC1_BOOT;
61 break;
62 case BT_DEV_TYPE_NAND:
63 boot_dev = NAND_BOOT;
64 break;
65 case BT_DEV_TYPE_FLEXSPINOR:
66 boot_dev = QSPI_BOOT;
67 break;
68 case BT_DEV_TYPE_USB:
69 boot_dev = USB_BOOT;
70 break;
71 default:
72 break;
73 }
74
75 return boot_dev;
76}
77
78bool is_usb_boot(void)
79{
80 return get_boot_device() == USB_BOOT;
81}
82
83#ifdef CONFIG_ENV_IS_IN_MMC
84__weak int board_mmc_get_env_dev(int devno)
85{
86 return devno;
87}
88
89int mmc_get_env_dev(void)
90{
Ye Li88044c72021-08-07 16:01:01 +080091 int ret;
92 u32 boot;
93 u16 boot_type;
94 u8 boot_instance;
95
Rasmus Villemoesb06f5a42022-06-20 10:53:22 +020096 ret = rom_api_query_boot_infor(QUERY_BT_DEV, &boot);
Ye Li88044c72021-08-07 16:01:01 +080097
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 Li0e358052021-08-07 16:01:07 +0800123 bt0_cfg = readl(SIM_SEC_BASE_ADDR + 0x24);
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
Ye Li2e9f15c2022-04-06 14:30:08 +0800137bool m33_image_booted(void)
138{
139 u32 gp6;
140
141 /* DGO_GP6 */
142 gp6 = readl(SIM_SEC_BASE_ADDR + 0x28);
143 if (gp6 & BIT(5))
144 return true;
145
146 return false;
147}
148
149int m33_image_handshake(ulong timeout_ms)
150{
151 u32 fsr;
152 int ret;
153 ulong timeout_us = timeout_ms * 1000;
154
Ye Li2e9f15c2022-04-06 14:30:08 +0800155 /* Notify m33 that it's ready to do init srtm(enable mu receive interrupt and so on) */
156 setbits_le32(MU0_B_BASE_ADDR + 0x100, BIT(0)); /* set FCR F0 flag of MU0_MUB */
157
158 /*
159 * Wait m33 to set FCR F0 flag of MU0_MUA
160 * Clear FCR F0 flag of MU0_MUB after m33 has set FCR F0 flag of MU0_MUA
161 */
162 ret = readl_poll_sleep_timeout(MU0_B_BASE_ADDR + 0x104, fsr, fsr & BIT(0), 10, timeout_us);
163 if (!ret)
164 clrbits_le32(MU0_B_BASE_ADDR + 0x100, BIT(0));
165
166 return ret;
167}
168
Peng Fanaf4f3b32021-08-07 16:00:34 +0800169#define CMC_SRS_TAMPER BIT(31)
170#define CMC_SRS_SECURITY BIT(30)
171#define CMC_SRS_TZWDG BIT(29)
172#define CMC_SRS_JTAG_RST BIT(28)
173#define CMC_SRS_CORE1 BIT(16)
174#define CMC_SRS_LOCKUP BIT(15)
175#define CMC_SRS_SW BIT(14)
176#define CMC_SRS_WDG BIT(13)
177#define CMC_SRS_PIN_RESET BIT(8)
178#define CMC_SRS_WARM BIT(4)
179#define CMC_SRS_HVD BIT(3)
180#define CMC_SRS_LVD BIT(2)
181#define CMC_SRS_POR BIT(1)
182#define CMC_SRS_WUP BIT(0)
183
Peng Fanaf4f3b32021-08-07 16:00:34 +0800184static char *get_reset_cause(char *ret)
185{
186 u32 cause1, cause = 0, srs = 0;
Peng Fanb15705a2021-08-07 16:00:35 +0800187 void __iomem *reg_ssrs = (void __iomem *)(CMC1_BASE_ADDR + 0x88);
188 void __iomem *reg_srs = (void __iomem *)(CMC1_BASE_ADDR + 0x80);
Peng Fanaf4f3b32021-08-07 16:00:34 +0800189
190 if (!ret)
191 return "null";
192
193 srs = readl(reg_srs);
194 cause1 = readl(reg_ssrs);
195
Peng Fan0d720e22021-08-07 16:01:06 +0800196 cause = srs & (CMC_SRS_POR | CMC_SRS_WUP | CMC_SRS_WARM);
Peng Fanaf4f3b32021-08-07 16:00:34 +0800197
198 switch (cause) {
199 case CMC_SRS_POR:
200 sprintf(ret, "%s", "POR");
201 break;
202 case CMC_SRS_WUP:
203 sprintf(ret, "%s", "WUP");
204 break;
205 case CMC_SRS_WARM:
Peng Fan0d720e22021-08-07 16:01:06 +0800206 cause = srs & (CMC_SRS_WDG | CMC_SRS_SW |
Peng Fanaf4f3b32021-08-07 16:00:34 +0800207 CMC_SRS_JTAG_RST);
208 switch (cause) {
209 case CMC_SRS_WDG:
210 sprintf(ret, "%s", "WARM-WDG");
211 break;
212 case CMC_SRS_SW:
213 sprintf(ret, "%s", "WARM-SW");
214 break;
215 case CMC_SRS_JTAG_RST:
216 sprintf(ret, "%s", "WARM-JTAG");
217 break;
218 default:
219 sprintf(ret, "%s", "WARM-UNKN");
220 break;
221 }
222 break;
223 default:
Peng Fan0d720e22021-08-07 16:01:06 +0800224 sprintf(ret, "%s-%X", "UNKN", srs);
Peng Fanaf4f3b32021-08-07 16:00:34 +0800225 break;
226 }
227
228 debug("[%X] SRS[%X] %X - ", cause1, srs, srs ^ cause1);
229 return ret;
230}
231
Peng Fan72530162021-08-07 16:00:33 +0800232#if defined(CONFIG_DISPLAY_CPUINFO)
233const char *get_imx_type(u32 imxtype)
234{
235 return "8ULP";
236}
237
238int print_cpuinfo(void)
239{
240 u32 cpurev;
241 char cause[18];
242
243 cpurev = get_cpu_rev();
244
Ye Lif012ceb2021-10-29 09:46:24 +0800245 printf("CPU: i.MX%s rev%d.%d at %d MHz\n",
Peng Fan72530162021-08-07 16:00:33 +0800246 get_imx_type((cpurev & 0xFF000) >> 12),
247 (cpurev & 0x000F0) >> 4, (cpurev & 0x0000F) >> 0,
248 mxc_get_clock(MXC_ARM_CLK) / 1000000);
249
Alice Guof2c4a392021-10-29 09:46:32 +0800250#if defined(CONFIG_IMX_PMC_TEMPERATURE)
251 struct udevice *udev;
252 int ret, temp;
253
254 ret = uclass_get_device(UCLASS_THERMAL, 0, &udev);
255 if (!ret) {
256 ret = thermal_get_temp(udev, &temp);
257 if (!ret)
258 printf("CPU current temperature: %d\n", temp);
259 else
260 debug(" - failed to get CPU current temperature\n");
261 } else {
262 debug(" - failed to get CPU current temperature\n");
263 }
264#endif
265
Peng Fanaf4f3b32021-08-07 16:00:34 +0800266 printf("Reset cause: %s\n", get_reset_cause(cause));
267
Peng Fan72530162021-08-07 16:00:33 +0800268 printf("Boot mode: ");
269 switch (get_boot_mode()) {
270 case LOW_POWER_BOOT:
271 printf("Low power boot\n");
272 break;
273 case DUAL_BOOT:
274 printf("Dual boot\n");
275 break;
276 case SINGLE_BOOT:
277 default:
278 printf("Single boot\n");
279 break;
280 }
281
282 return 0;
283}
284#endif
Peng Fanb15705a2021-08-07 16:00:35 +0800285
Peng Fanc84bc102021-08-07 16:00:49 +0800286#define UNLOCK_WORD0 0xC520 /* 1st unlock word */
287#define UNLOCK_WORD1 0xD928 /* 2nd unlock word */
288#define REFRESH_WORD0 0xA602 /* 1st refresh word */
289#define REFRESH_WORD1 0xB480 /* 2nd refresh word */
290
291static void disable_wdog(void __iomem *wdog_base)
292{
293 u32 val_cs = readl(wdog_base + 0x00);
294
295 if (!(val_cs & 0x80))
296 return;
297
298 dmb();
299 __raw_writel(REFRESH_WORD0, (wdog_base + 0x04)); /* Refresh the CNT */
300 __raw_writel(REFRESH_WORD1, (wdog_base + 0x04));
301 dmb();
302
303 if (!(val_cs & 800)) {
304 dmb();
305 __raw_writel(UNLOCK_WORD0, (wdog_base + 0x04));
306 __raw_writel(UNLOCK_WORD1, (wdog_base + 0x04));
307 dmb();
308
309 while (!(readl(wdog_base + 0x00) & 0x800))
310 ;
311 }
312 writel(0x0, (wdog_base + 0x0C)); /* Set WIN to 0 */
313 writel(0x400, (wdog_base + 0x08)); /* Set timeout to default 0x400 */
314 writel(0x120, (wdog_base + 0x00)); /* Disable it and set update */
315
316 while (!(readl(wdog_base + 0x00) & 0x400))
317 ;
318}
319
Peng Fanb15705a2021-08-07 16:00:35 +0800320void init_wdog(void)
321{
Peng Fanc84bc102021-08-07 16:00:49 +0800322 disable_wdog((void __iomem *)WDG3_RBASE);
Peng Fanb15705a2021-08-07 16:00:35 +0800323}
324
325static struct mm_region imx8ulp_arm64_mem_map[] = {
326 {
327 /* ROM */
328 .virt = 0x0,
329 .phys = 0x0,
330 .size = 0x40000UL,
331 .attrs = PTE_BLOCK_MEMTYPE(MT_NORMAL) |
332 PTE_BLOCK_OUTER_SHARE
333 },
334 {
335 /* FLEXSPI0 */
336 .virt = 0x04000000,
337 .phys = 0x04000000,
338 .size = 0x08000000UL,
339 .attrs = PTE_BLOCK_MEMTYPE(MT_DEVICE_NGNRNE) |
340 PTE_BLOCK_NON_SHARE |
341 PTE_BLOCK_PXN | PTE_BLOCK_UXN
342 },
343 {
344 /* SSRAM (align with 2M) */
345 .virt = 0x1FE00000UL,
346 .phys = 0x1FE00000UL,
347 .size = 0x400000UL,
348 .attrs = PTE_BLOCK_MEMTYPE(MT_NORMAL) |
349 PTE_BLOCK_OUTER_SHARE |
350 PTE_BLOCK_PXN | PTE_BLOCK_UXN
351 }, {
352 /* SRAM1 (align with 2M) */
353 .virt = 0x21000000UL,
354 .phys = 0x21000000UL,
355 .size = 0x200000UL,
356 .attrs = PTE_BLOCK_MEMTYPE(MT_NORMAL) |
357 PTE_BLOCK_OUTER_SHARE |
358 PTE_BLOCK_PXN | PTE_BLOCK_UXN
359 }, {
360 /* SRAM0 (align with 2M) */
361 .virt = 0x22000000UL,
362 .phys = 0x22000000UL,
363 .size = 0x200000UL,
364 .attrs = PTE_BLOCK_MEMTYPE(MT_NORMAL) |
365 PTE_BLOCK_OUTER_SHARE |
366 PTE_BLOCK_PXN | PTE_BLOCK_UXN
367 }, {
368 /* Peripherals */
369 .virt = 0x27000000UL,
370 .phys = 0x27000000UL,
371 .size = 0x3000000UL,
372 .attrs = PTE_BLOCK_MEMTYPE(MT_DEVICE_NGNRNE) |
373 PTE_BLOCK_NON_SHARE |
374 PTE_BLOCK_PXN | PTE_BLOCK_UXN
375 }, {
376 /* Peripherals */
377 .virt = 0x2D000000UL,
378 .phys = 0x2D000000UL,
379 .size = 0x1600000UL,
380 .attrs = PTE_BLOCK_MEMTYPE(MT_DEVICE_NGNRNE) |
381 PTE_BLOCK_NON_SHARE |
382 PTE_BLOCK_PXN | PTE_BLOCK_UXN
383 }, {
384 /* FLEXSPI1-2 */
385 .virt = 0x40000000UL,
386 .phys = 0x40000000UL,
387 .size = 0x40000000UL,
388 .attrs = PTE_BLOCK_MEMTYPE(MT_DEVICE_NGNRNE) |
389 PTE_BLOCK_NON_SHARE |
390 PTE_BLOCK_PXN | PTE_BLOCK_UXN
391 }, {
392 /* DRAM1 */
393 .virt = 0x80000000UL,
394 .phys = 0x80000000UL,
395 .size = PHYS_SDRAM_SIZE,
396 .attrs = PTE_BLOCK_MEMTYPE(MT_NORMAL) |
397 PTE_BLOCK_OUTER_SHARE
398 }, {
399 /*
400 * empty entrie to split table entry 5
401 * if needed when TEEs are used
402 */
403 0,
404 }, {
405 /* List terminator */
406 0,
407 }
408};
409
410struct mm_region *mem_map = imx8ulp_arm64_mem_map;
411
Ji Luo2fd258c2022-04-06 14:30:28 +0800412static unsigned int imx8ulp_find_dram_entry_in_mem_map(void)
413{
414 int i;
415
416 for (i = 0; i < ARRAY_SIZE(imx8ulp_arm64_mem_map); i++)
417 if (imx8ulp_arm64_mem_map[i].phys == CONFIG_SYS_SDRAM_BASE)
418 return i;
419
420 hang(); /* Entry not found, this must never happen. */
421}
422
Peng Fanb15705a2021-08-07 16:00:35 +0800423/* simplify the page table size to enhance boot speed */
424#define MAX_PTE_ENTRIES 512
425#define MAX_MEM_MAP_REGIONS 16
426u64 get_page_table_size(void)
427{
428 u64 one_pt = MAX_PTE_ENTRIES * sizeof(u64);
429 u64 size = 0;
430
431 /*
432 * For each memory region, the max table size:
433 * 2 level 3 tables + 2 level 2 tables + 1 level 1 table
434 */
435 size = (2 + 2 + 1) * one_pt * MAX_MEM_MAP_REGIONS + one_pt;
436
437 /*
438 * We need to duplicate our page table once to have an emergency pt to
439 * resort to when splitting page tables later on
440 */
441 size *= 2;
442
443 /*
444 * We may need to split page tables later on if dcache settings change,
445 * so reserve up to 4 (random pick) page tables for that.
446 */
447 size += one_pt * 4;
448
449 return size;
450}
451
452void enable_caches(void)
453{
Ji Luo2fd258c2022-04-06 14:30:28 +0800454 /* If OPTEE runs, remove OPTEE memory from MMU table to avoid speculative prefetch */
455 if (rom_pointer[1]) {
456 /*
457 * TEE are loaded, So the ddr bank structures
458 * have been modified update mmu table accordingly
459 */
460 int i = 0;
461 int entry = imx8ulp_find_dram_entry_in_mem_map();
462 u64 attrs = imx8ulp_arm64_mem_map[entry].attrs;
463
464 while (i < CONFIG_NR_DRAM_BANKS &&
465 entry < ARRAY_SIZE(imx8ulp_arm64_mem_map)) {
466 if (gd->bd->bi_dram[i].start == 0)
467 break;
468 imx8ulp_arm64_mem_map[entry].phys = gd->bd->bi_dram[i].start;
469 imx8ulp_arm64_mem_map[entry].virt = gd->bd->bi_dram[i].start;
470 imx8ulp_arm64_mem_map[entry].size = gd->bd->bi_dram[i].size;
471 imx8ulp_arm64_mem_map[entry].attrs = attrs;
472 debug("Added memory mapping (%d): %llx %llx\n", entry,
473 imx8ulp_arm64_mem_map[entry].phys, imx8ulp_arm64_mem_map[entry].size);
474 i++; entry++;
475 }
476 }
Peng Fanb15705a2021-08-07 16:00:35 +0800477
478 icache_enable();
479 dcache_enable();
480}
481
Ji Luo2fd258c2022-04-06 14:30:28 +0800482__weak int board_phys_sdram_size(phys_size_t *size)
483{
484 if (!size)
485 return -EINVAL;
486
487 *size = PHYS_SDRAM_SIZE;
488 return 0;
489}
490
Peng Fanb15705a2021-08-07 16:00:35 +0800491int dram_init(void)
492{
Ji Luo2fd258c2022-04-06 14:30:28 +0800493 unsigned int entry = imx8ulp_find_dram_entry_in_mem_map();
494 phys_size_t sdram_size;
495 int ret;
496
497 ret = board_phys_sdram_size(&sdram_size);
498 if (ret)
499 return ret;
500
501 /* rom_pointer[1] contains the size of TEE occupies */
502 if (rom_pointer[1])
503 gd->ram_size = sdram_size - rom_pointer[1];
504 else
505 gd->ram_size = sdram_size;
506
507 /* also update the SDRAM size in the mem_map used externally */
508 imx8ulp_arm64_mem_map[entry].size = sdram_size;
509 return 0;
510}
511
512int dram_init_banksize(void)
513{
514 int bank = 0;
515 int ret;
516 phys_size_t sdram_size;
517
518 ret = board_phys_sdram_size(&sdram_size);
519 if (ret)
520 return ret;
521
522 gd->bd->bi_dram[bank].start = PHYS_SDRAM;
523 if (rom_pointer[1]) {
524 phys_addr_t optee_start = (phys_addr_t)rom_pointer[0];
525 phys_size_t optee_size = (size_t)rom_pointer[1];
526
527 gd->bd->bi_dram[bank].size = optee_start - gd->bd->bi_dram[bank].start;
528 if ((optee_start + optee_size) < (PHYS_SDRAM + sdram_size)) {
529 if (++bank >= CONFIG_NR_DRAM_BANKS) {
530 puts("CONFIG_NR_DRAM_BANKS is not enough\n");
531 return -1;
532 }
533
534 gd->bd->bi_dram[bank].start = optee_start + optee_size;
535 gd->bd->bi_dram[bank].size = PHYS_SDRAM +
536 sdram_size - gd->bd->bi_dram[bank].start;
537 }
538 } else {
539 gd->bd->bi_dram[bank].size = sdram_size;
540 }
Peng Fanb15705a2021-08-07 16:00:35 +0800541
542 return 0;
543}
544
Ji Luo2fd258c2022-04-06 14:30:28 +0800545phys_size_t get_effective_memsize(void)
546{
547 /* return the first bank as effective memory */
548 if (rom_pointer[1])
549 return ((phys_addr_t)rom_pointer[0] - PHYS_SDRAM);
550
551 return gd->ram_size;
552}
553
Tom Riniae21e7f2021-08-30 09:16:29 -0400554#ifdef CONFIG_ENV_VARS_UBOOT_RUNTIME_CONFIG
Peng Fanb15705a2021-08-07 16:00:35 +0800555void get_board_serial(struct tag_serialnr *serialnr)
556{
Ye Li7bea5b02021-08-07 16:01:00 +0800557 u32 uid[4];
558 u32 res;
559 int ret;
560
561 ret = ahab_read_common_fuse(1, uid, 4, &res);
562 if (ret)
563 printf("ahab read fuse failed %d, 0x%x\n", ret, res);
564 else
565 printf("UID 0x%x,0x%x,0x%x,0x%x\n", uid[0], uid[1], uid[2], uid[3]);
566
567 serialnr->low = uid[0];
568 serialnr->high = uid[3];
Peng Fanb15705a2021-08-07 16:00:35 +0800569}
570#endif
571
Ye Li6ee435eb2021-08-07 16:00:50 +0800572static void set_core0_reset_vector(u32 entry)
Peng Fanb15705a2021-08-07 16:00:35 +0800573{
Ye Li6dd43022021-08-07 16:00:48 +0800574 /* Update SIM1 DGO8 for reset vector base */
Ye Li6ee435eb2021-08-07 16:00:50 +0800575 writel(entry, SIM1_BASE_ADDR + 0x5c);
Ye Li6dd43022021-08-07 16:00:48 +0800576
577 /* set update bit */
578 setbits_le32(SIM1_BASE_ADDR + 0x8, 0x1 << 24);
579
580 /* polling the ack */
581 while ((readl(SIM1_BASE_ADDR + 0x8) & (0x1 << 26)) == 0)
582 ;
583
584 /* clear the update */
585 clrbits_le32(SIM1_BASE_ADDR + 0x8, (0x1 << 24));
586
587 /* clear the ack by set 1 */
588 setbits_le32(SIM1_BASE_ADDR + 0x8, (0x1 << 26));
Ye Li6ee435eb2021-08-07 16:00:50 +0800589}
590
Peng Fan9c87e462021-08-07 16:00:59 +0800591static int trdc_set_access(void)
Peng Fanb5c41b12021-08-07 16:00:58 +0800592{
593 /*
Peng Fan9c87e462021-08-07 16:00:59 +0800594 * TRDC mgr + 4 MBC + 2 MRC.
595 * S400 should already configure when release RDC
596 * A35 only map non-secure region for pbridge0 and 1, set sec_access to false
Peng Fanb5c41b12021-08-07 16:00:58 +0800597 */
Peng Fan9c87e462021-08-07 16:00:59 +0800598 trdc_mbc_set_access(2, 7, 0, 49, false);
599 trdc_mbc_set_access(2, 7, 0, 50, false);
600 trdc_mbc_set_access(2, 7, 0, 51, false);
601 trdc_mbc_set_access(2, 7, 0, 52, false);
602 trdc_mbc_set_access(2, 7, 0, 53, false);
603 trdc_mbc_set_access(2, 7, 0, 54, false);
Peng Fanb5c41b12021-08-07 16:00:58 +0800604
Peng Fan9c87e462021-08-07 16:00:59 +0800605 /* CGC0: PBridge0 slot 47 */
606 trdc_mbc_set_access(2, 7, 0, 47, false);
Peng Fanb5c41b12021-08-07 16:00:58 +0800607
Peng Fan9c87e462021-08-07 16:00:59 +0800608 /* Iomuxc0: : PBridge1 slot 33 */
609 trdc_mbc_set_access(2, 7, 1, 33, false);
Peng Fanb5c41b12021-08-07 16:00:58 +0800610
Ye Lid325d372021-10-29 09:46:20 +0800611 /* flexspi0 */
612 trdc_mrc_region_set_access(0, 7, 0x04000000, 0x0c000000, false);
Ye Li27666ca2021-10-29 09:46:21 +0800613
614 /* tpm0: PBridge1 slot 21 */
615 trdc_mbc_set_access(2, 7, 1, 21, false);
616 /* lpi2c0: PBridge1 slot 24 */
617 trdc_mbc_set_access(2, 7, 1, 24, false);
Peng Fanb5c41b12021-08-07 16:00:58 +0800618 return 0;
619}
620
Ye Liec10a802022-04-06 14:30:17 +0800621void lpav_configure(bool lpav_to_m33)
Ye Li43819eb2021-10-29 09:46:16 +0800622{
Ye Liec10a802022-04-06 14:30:17 +0800623 if (!lpav_to_m33)
624 setbits_le32(SIM_SEC_BASE_ADDR + 0x44, BIT(7)); /* LPAV to APD */
Ye Li43819eb2021-10-29 09:46:16 +0800625
Peng Fanfa609b42021-10-29 09:46:17 +0800626 /* PXP/GPU 2D/3D/DCNANO/MIPI_DSI/EPDC/HIFI4 to APD */
627 setbits_le32(SIM_SEC_BASE_ADDR + 0x4c, 0x7F);
Ye Li43819eb2021-10-29 09:46:16 +0800628
629 /* LPAV slave/dma2 ch allocation and request allocation to APD */
630 writel(0x1f, SIM_SEC_BASE_ADDR + 0x50);
631 writel(0xffffffff, SIM_SEC_BASE_ADDR + 0x54);
632 writel(0x003fffff, SIM_SEC_BASE_ADDR + 0x58);
Ye Li715cfa02021-10-29 09:46:23 +0800633}
Ye Lia0311552021-10-29 09:46:22 +0800634
Ye Li133f8b82021-10-29 09:46:25 +0800635void load_lposc_fuse(void)
636{
637 int ret;
638 u32 val = 0, val2 = 0, reg;
639
640 ret = fuse_read(25, 0, &val);
641 if (ret)
642 return; /* failed */
643
644 ret = fuse_read(25, 1, &val2);
645 if (ret)
646 return; /* failed */
647
648 /* LPOSCCTRL */
649 reg = readl(0x2802f304);
650 reg &= ~0xff;
651 reg |= (val & 0xff);
652 writel(reg, 0x2802f304);
653}
654
Ye Li715cfa02021-10-29 09:46:23 +0800655void set_lpav_qos(void)
656{
Ye Lia0311552021-10-29 09:46:22 +0800657 /* Set read QoS of dcnano on LPAV NIC */
658 writel(0xf, 0x2e447100);
Ye Li43819eb2021-10-29 09:46:16 +0800659}
660
Ye Li6ee435eb2021-08-07 16:00:50 +0800661int arch_cpu_init(void)
662{
663 if (IS_ENABLED(CONFIG_SPL_BUILD)) {
Ye Li72012622021-10-29 09:46:15 +0800664 u32 val = 0;
665 int ret;
666 bool rdc_en = true; /* Default assume DBD_EN is set */
667
Peng Fan21bda432022-04-06 14:30:27 +0800668 /* Enable System Reset Interrupt using WDOG_AD */
669 setbits_le32(CMC1_BASE_ADDR + 0x8C, BIT(13));
670 /* Clear AD_PERIPH Power switch domain out of reset interrupt flag */
671 setbits_le32(CMC1_BASE_ADDR + 0x70, BIT(4));
672
673 if (readl(CMC1_BASE_ADDR + 0x90) & BIT(13)) {
674 /* Clear System Reset Interrupt Flag Register of WDOG_AD */
675 setbits_le32(CMC1_BASE_ADDR + 0x90, BIT(13));
676 /* Reset WDOG to clear reset request */
677 pcc_reset_peripheral(3, WDOG3_PCC3_SLOT, true);
678 pcc_reset_peripheral(3, WDOG3_PCC3_SLOT, false);
679 }
680
Ye Li853cc9d2021-08-07 16:00:55 +0800681 /* Disable wdog */
682 init_wdog();
683
Ye Li72012622021-10-29 09:46:15 +0800684 /* Read DBD_EN fuse */
685 ret = fuse_read(8, 1, &val);
686 if (!ret)
687 rdc_en = !!(val & 0x4000);
688
Peng Fanb5c41b12021-08-07 16:00:58 +0800689 if (get_boot_mode() == SINGLE_BOOT) {
Ye Li72012622021-10-29 09:46:15 +0800690 if (rdc_en)
691 release_rdc(RDC_TRDC);
692
Peng Fanb5c41b12021-08-07 16:00:58 +0800693 trdc_set_access();
Ye Liec10a802022-04-06 14:30:17 +0800694 lpav_configure(false);
695 } else {
696 lpav_configure(true);
Peng Fanb5c41b12021-08-07 16:00:58 +0800697 }
Peng Fanfa55b212021-08-07 16:00:57 +0800698
Ye Li72012622021-10-29 09:46:15 +0800699 /* Release xrdc, then allow A35 to write SRAM2 */
700 if (rdc_en)
701 release_rdc(RDC_XRDC);
702
Ye Li853cc9d2021-08-07 16:00:55 +0800703 xrdc_mrc_region_set_access(2, CONFIG_SPL_TEXT_BASE, 0xE00);
704
Peng Fan4cdb3a32022-04-06 14:30:12 +0800705 clock_init_early();
Ye Li6ee435eb2021-08-07 16:00:50 +0800706 } else {
707 /* reconfigure core0 reset vector to ROM */
708 set_core0_reset_vector(0x1000);
709 }
710
711 return 0;
712}
713
Simon Glassfc557362022-03-04 08:43:05 -0700714static int imx8ulp_check_mu(void *ctx, struct event *event)
Ye Li7bea5b02021-08-07 16:01:00 +0800715{
716 struct udevice *devp;
717 int node, ret;
718
719 node = fdt_node_offset_by_compatible(gd->fdt_blob, -1, "fsl,imx8ulp-mu");
720
721 ret = uclass_get_device_by_of_offset(UCLASS_MISC, node, &devp);
722 if (ret) {
723 printf("could not get S400 mu %d\n", ret);
724 return ret;
725 }
726
727 return 0;
728}
Simon Glassfc557362022-03-04 08:43:05 -0700729EVENT_SPY(EVT_DM_POST_INIT, imx8ulp_check_mu);
Ye Li7bea5b02021-08-07 16:01:00 +0800730
Ye Li6ee435eb2021-08-07 16:00:50 +0800731#if defined(CONFIG_SPL_BUILD)
732__weak void __noreturn jump_to_image_no_args(struct spl_image_info *spl_image)
733{
734 debug("image entry point: 0x%lx\n", spl_image->entry_point);
735
736 set_core0_reset_vector((u32)spl_image->entry_point);
Ye Li6dd43022021-08-07 16:00:48 +0800737
738 /* Enable the 512KB cache */
739 setbits_le32(SIM1_BASE_ADDR + 0x30, (0x1 << 4));
740
741 /* reset core */
742 setbits_le32(SIM1_BASE_ADDR + 0x30, (0x1 << 16));
743
744 while (1)
745 ;
746}
747#endif
Peng Fanfa6ae052021-08-07 16:01:03 +0800748
749void imx_get_mac_from_fuse(int dev_id, unsigned char *mac)
750{
Ye Li992b0ae2021-10-29 09:46:28 +0800751 u32 val[2] = {};
752 int ret;
753
754 ret = fuse_read(5, 3, &val[0]);
755 if (ret)
756 goto err;
757
758 ret = fuse_read(5, 4, &val[1]);
759 if (ret)
760 goto err;
761
762 mac[0] = val[0];
763 mac[1] = val[0] >> 8;
764 mac[2] = val[0] >> 16;
765 mac[3] = val[0] >> 24;
766 mac[4] = val[1];
767 mac[5] = val[1] >> 8;
768
769 debug("%s: MAC%d: %02x.%02x.%02x.%02x.%02x.%02x\n",
770 __func__, dev_id, mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
771 return;
772err:
Peng Fanfa6ae052021-08-07 16:01:03 +0800773 memset(mac, 0, 6);
Ye Li992b0ae2021-10-29 09:46:28 +0800774 printf("%s: fuse read err: %d\n", __func__, ret);
Peng Fanfa6ae052021-08-07 16:01:03 +0800775}
Ye Li479fd4a2021-08-07 16:01:08 +0800776
777int (*card_emmc_is_boot_part_en)(void) = (void *)0x67cc;
778u32 spl_arch_boot_image_offset(u32 image_offset, u32 rom_bt_dev)
779{
780 /* Hard code for eMMC image_offset on 8ULP ROM, need fix by ROM, temp workaround */
781 if (((rom_bt_dev >> 16) & 0xff) == BT_DEV_TYPE_MMC && card_emmc_is_boot_part_en())
782 image_offset = 0;
783
784 return image_offset;
785}
Ye Li48836ae2022-04-06 14:30:30 +0800786
787enum env_location env_get_location(enum env_operation op, int prio)
788{
789 enum boot_device dev = get_boot_device();
790 enum env_location env_loc = ENVL_UNKNOWN;
791
792 if (prio)
793 return env_loc;
794
795 switch (dev) {
796#ifdef CONFIG_ENV_IS_IN_SPI_FLASH
797 case QSPI_BOOT:
798 env_loc = ENVL_SPI_FLASH;
799 break;
800#endif
801#ifdef CONFIG_ENV_IS_IN_MMC
802 case SD1_BOOT:
803 case SD2_BOOT:
804 case SD3_BOOT:
805 case MMC1_BOOT:
806 case MMC2_BOOT:
807 case MMC3_BOOT:
808 env_loc = ENVL_MMC;
809 break;
810#endif
811 default:
812#if defined(CONFIG_ENV_IS_NOWHERE)
813 env_loc = ENVL_NOWHERE;
814#endif
815 break;
816 }
817
818 return env_loc;
819}