blob: 35020c9714d39b22c4b4a69dd4395de1f02c95ae [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{
39 volatile gd_t *pgd = gd;
40 int ret;
41 u32 boot;
42 u16 boot_type;
43 u8 boot_instance;
44 enum boot_device boot_dev = SD1_BOOT;
45
46 ret = g_rom_api->query_boot_infor(QUERY_BT_DEV, &boot,
47 ((uintptr_t)&boot) ^ QUERY_BT_DEV);
48 set_gd(pgd);
49
50 if (ret != ROM_API_OKAY) {
51 puts("ROMAPI: failure at query_boot_info\n");
52 return -1;
53 }
54
55 boot_type = boot >> 16;
56 boot_instance = (boot >> 8) & 0xff;
57
58 switch (boot_type) {
59 case BT_DEV_TYPE_SD:
60 boot_dev = boot_instance + SD1_BOOT;
61 break;
62 case BT_DEV_TYPE_MMC:
63 boot_dev = boot_instance + MMC1_BOOT;
64 break;
65 case BT_DEV_TYPE_NAND:
66 boot_dev = NAND_BOOT;
67 break;
68 case BT_DEV_TYPE_FLEXSPINOR:
69 boot_dev = QSPI_BOOT;
70 break;
71 case BT_DEV_TYPE_USB:
72 boot_dev = USB_BOOT;
73 break;
74 default:
75 break;
76 }
77
78 return boot_dev;
79}
80
81bool is_usb_boot(void)
82{
83 return get_boot_device() == USB_BOOT;
84}
85
86#ifdef CONFIG_ENV_IS_IN_MMC
87__weak int board_mmc_get_env_dev(int devno)
88{
89 return devno;
90}
91
92int mmc_get_env_dev(void)
93{
94 volatile gd_t *pgd = gd;
95 int ret;
96 u32 boot;
97 u16 boot_type;
98 u8 boot_instance;
99
100 ret = g_rom_api->query_boot_infor(QUERY_BT_DEV, &boot,
101 ((uintptr_t)&boot) ^ QUERY_BT_DEV);
102 set_gd(pgd);
103
104 if (ret != ROM_API_OKAY) {
105 puts("ROMAPI: failure at query_boot_info\n");
106 return CONFIG_SYS_MMC_ENV_DEV;
107 }
108
109 boot_type = boot >> 16;
110 boot_instance = (boot >> 8) & 0xff;
111
112 /* If not boot from sd/mmc, use default value */
113 if (boot_type != BOOT_TYPE_SD && boot_type != BOOT_TYPE_MMC)
114 return env_get_ulong("mmcdev", 10, CONFIG_SYS_MMC_ENV_DEV);
115
116 return board_mmc_get_env_dev(boot_instance);
117}
118#endif
119
Peng Fan5c2218a2021-08-07 16:00:31 +0800120u32 get_cpu_rev(void)
121{
122 return (MXC_CPU_IMX8ULP << 12) | CHIP_REV_1_0;
123}
Peng Fan72530162021-08-07 16:00:33 +0800124
125enum bt_mode get_boot_mode(void)
126{
127 u32 bt0_cfg = 0;
128
Ye Li0e358052021-08-07 16:01:07 +0800129 bt0_cfg = readl(SIM_SEC_BASE_ADDR + 0x24);
Peng Fan72530162021-08-07 16:00:33 +0800130 bt0_cfg &= (BT0CFG_LPBOOT_MASK | BT0CFG_DUALBOOT_MASK);
131
132 if (!(bt0_cfg & BT0CFG_LPBOOT_MASK)) {
133 /* No low power boot */
134 if (bt0_cfg & BT0CFG_DUALBOOT_MASK)
135 return DUAL_BOOT;
136 else
137 return SINGLE_BOOT;
138 }
139
140 return LOW_POWER_BOOT;
141}
142
Ye Li2e9f15c2022-04-06 14:30:08 +0800143bool m33_image_booted(void)
144{
145 u32 gp6;
146
147 /* DGO_GP6 */
148 gp6 = readl(SIM_SEC_BASE_ADDR + 0x28);
149 if (gp6 & BIT(5))
150 return true;
151
152 return false;
153}
154
155int m33_image_handshake(ulong timeout_ms)
156{
157 u32 fsr;
158 int ret;
159 ulong timeout_us = timeout_ms * 1000;
160
Ye Li2e9f15c2022-04-06 14:30:08 +0800161 /* Notify m33 that it's ready to do init srtm(enable mu receive interrupt and so on) */
162 setbits_le32(MU0_B_BASE_ADDR + 0x100, BIT(0)); /* set FCR F0 flag of MU0_MUB */
163
164 /*
165 * Wait m33 to set FCR F0 flag of MU0_MUA
166 * Clear FCR F0 flag of MU0_MUB after m33 has set FCR F0 flag of MU0_MUA
167 */
168 ret = readl_poll_sleep_timeout(MU0_B_BASE_ADDR + 0x104, fsr, fsr & BIT(0), 10, timeout_us);
169 if (!ret)
170 clrbits_le32(MU0_B_BASE_ADDR + 0x100, BIT(0));
171
172 return ret;
173}
174
Peng Fanaf4f3b32021-08-07 16:00:34 +0800175#define CMC_SRS_TAMPER BIT(31)
176#define CMC_SRS_SECURITY BIT(30)
177#define CMC_SRS_TZWDG BIT(29)
178#define CMC_SRS_JTAG_RST BIT(28)
179#define CMC_SRS_CORE1 BIT(16)
180#define CMC_SRS_LOCKUP BIT(15)
181#define CMC_SRS_SW BIT(14)
182#define CMC_SRS_WDG BIT(13)
183#define CMC_SRS_PIN_RESET BIT(8)
184#define CMC_SRS_WARM BIT(4)
185#define CMC_SRS_HVD BIT(3)
186#define CMC_SRS_LVD BIT(2)
187#define CMC_SRS_POR BIT(1)
188#define CMC_SRS_WUP BIT(0)
189
Peng Fanaf4f3b32021-08-07 16:00:34 +0800190static char *get_reset_cause(char *ret)
191{
192 u32 cause1, cause = 0, srs = 0;
Peng Fanb15705a2021-08-07 16:00:35 +0800193 void __iomem *reg_ssrs = (void __iomem *)(CMC1_BASE_ADDR + 0x88);
194 void __iomem *reg_srs = (void __iomem *)(CMC1_BASE_ADDR + 0x80);
Peng Fanaf4f3b32021-08-07 16:00:34 +0800195
196 if (!ret)
197 return "null";
198
199 srs = readl(reg_srs);
200 cause1 = readl(reg_ssrs);
201
Peng Fan0d720e22021-08-07 16:01:06 +0800202 cause = srs & (CMC_SRS_POR | CMC_SRS_WUP | CMC_SRS_WARM);
Peng Fanaf4f3b32021-08-07 16:00:34 +0800203
204 switch (cause) {
205 case CMC_SRS_POR:
206 sprintf(ret, "%s", "POR");
207 break;
208 case CMC_SRS_WUP:
209 sprintf(ret, "%s", "WUP");
210 break;
211 case CMC_SRS_WARM:
Peng Fan0d720e22021-08-07 16:01:06 +0800212 cause = srs & (CMC_SRS_WDG | CMC_SRS_SW |
Peng Fanaf4f3b32021-08-07 16:00:34 +0800213 CMC_SRS_JTAG_RST);
214 switch (cause) {
215 case CMC_SRS_WDG:
216 sprintf(ret, "%s", "WARM-WDG");
217 break;
218 case CMC_SRS_SW:
219 sprintf(ret, "%s", "WARM-SW");
220 break;
221 case CMC_SRS_JTAG_RST:
222 sprintf(ret, "%s", "WARM-JTAG");
223 break;
224 default:
225 sprintf(ret, "%s", "WARM-UNKN");
226 break;
227 }
228 break;
229 default:
Peng Fan0d720e22021-08-07 16:01:06 +0800230 sprintf(ret, "%s-%X", "UNKN", srs);
Peng Fanaf4f3b32021-08-07 16:00:34 +0800231 break;
232 }
233
234 debug("[%X] SRS[%X] %X - ", cause1, srs, srs ^ cause1);
235 return ret;
236}
237
Peng Fan72530162021-08-07 16:00:33 +0800238#if defined(CONFIG_DISPLAY_CPUINFO)
239const char *get_imx_type(u32 imxtype)
240{
241 return "8ULP";
242}
243
244int print_cpuinfo(void)
245{
246 u32 cpurev;
247 char cause[18];
248
249 cpurev = get_cpu_rev();
250
Ye Lif012ceb2021-10-29 09:46:24 +0800251 printf("CPU: i.MX%s rev%d.%d at %d MHz\n",
Peng Fan72530162021-08-07 16:00:33 +0800252 get_imx_type((cpurev & 0xFF000) >> 12),
253 (cpurev & 0x000F0) >> 4, (cpurev & 0x0000F) >> 0,
254 mxc_get_clock(MXC_ARM_CLK) / 1000000);
255
Alice Guof2c4a392021-10-29 09:46:32 +0800256#if defined(CONFIG_IMX_PMC_TEMPERATURE)
257 struct udevice *udev;
258 int ret, temp;
259
260 ret = uclass_get_device(UCLASS_THERMAL, 0, &udev);
261 if (!ret) {
262 ret = thermal_get_temp(udev, &temp);
263 if (!ret)
264 printf("CPU current temperature: %d\n", temp);
265 else
266 debug(" - failed to get CPU current temperature\n");
267 } else {
268 debug(" - failed to get CPU current temperature\n");
269 }
270#endif
271
Peng Fanaf4f3b32021-08-07 16:00:34 +0800272 printf("Reset cause: %s\n", get_reset_cause(cause));
273
Peng Fan72530162021-08-07 16:00:33 +0800274 printf("Boot mode: ");
275 switch (get_boot_mode()) {
276 case LOW_POWER_BOOT:
277 printf("Low power boot\n");
278 break;
279 case DUAL_BOOT:
280 printf("Dual boot\n");
281 break;
282 case SINGLE_BOOT:
283 default:
284 printf("Single boot\n");
285 break;
286 }
287
288 return 0;
289}
290#endif
Peng Fanb15705a2021-08-07 16:00:35 +0800291
Peng Fanc84bc102021-08-07 16:00:49 +0800292#define UNLOCK_WORD0 0xC520 /* 1st unlock word */
293#define UNLOCK_WORD1 0xD928 /* 2nd unlock word */
294#define REFRESH_WORD0 0xA602 /* 1st refresh word */
295#define REFRESH_WORD1 0xB480 /* 2nd refresh word */
296
297static void disable_wdog(void __iomem *wdog_base)
298{
299 u32 val_cs = readl(wdog_base + 0x00);
300
301 if (!(val_cs & 0x80))
302 return;
303
304 dmb();
305 __raw_writel(REFRESH_WORD0, (wdog_base + 0x04)); /* Refresh the CNT */
306 __raw_writel(REFRESH_WORD1, (wdog_base + 0x04));
307 dmb();
308
309 if (!(val_cs & 800)) {
310 dmb();
311 __raw_writel(UNLOCK_WORD0, (wdog_base + 0x04));
312 __raw_writel(UNLOCK_WORD1, (wdog_base + 0x04));
313 dmb();
314
315 while (!(readl(wdog_base + 0x00) & 0x800))
316 ;
317 }
318 writel(0x0, (wdog_base + 0x0C)); /* Set WIN to 0 */
319 writel(0x400, (wdog_base + 0x08)); /* Set timeout to default 0x400 */
320 writel(0x120, (wdog_base + 0x00)); /* Disable it and set update */
321
322 while (!(readl(wdog_base + 0x00) & 0x400))
323 ;
324}
325
Peng Fanb15705a2021-08-07 16:00:35 +0800326void init_wdog(void)
327{
Peng Fanc84bc102021-08-07 16:00:49 +0800328 disable_wdog((void __iomem *)WDG3_RBASE);
Peng Fanb15705a2021-08-07 16:00:35 +0800329}
330
331static struct mm_region imx8ulp_arm64_mem_map[] = {
332 {
333 /* ROM */
334 .virt = 0x0,
335 .phys = 0x0,
336 .size = 0x40000UL,
337 .attrs = PTE_BLOCK_MEMTYPE(MT_NORMAL) |
338 PTE_BLOCK_OUTER_SHARE
339 },
340 {
341 /* FLEXSPI0 */
342 .virt = 0x04000000,
343 .phys = 0x04000000,
344 .size = 0x08000000UL,
345 .attrs = PTE_BLOCK_MEMTYPE(MT_DEVICE_NGNRNE) |
346 PTE_BLOCK_NON_SHARE |
347 PTE_BLOCK_PXN | PTE_BLOCK_UXN
348 },
349 {
350 /* SSRAM (align with 2M) */
351 .virt = 0x1FE00000UL,
352 .phys = 0x1FE00000UL,
353 .size = 0x400000UL,
354 .attrs = PTE_BLOCK_MEMTYPE(MT_NORMAL) |
355 PTE_BLOCK_OUTER_SHARE |
356 PTE_BLOCK_PXN | PTE_BLOCK_UXN
357 }, {
358 /* SRAM1 (align with 2M) */
359 .virt = 0x21000000UL,
360 .phys = 0x21000000UL,
361 .size = 0x200000UL,
362 .attrs = PTE_BLOCK_MEMTYPE(MT_NORMAL) |
363 PTE_BLOCK_OUTER_SHARE |
364 PTE_BLOCK_PXN | PTE_BLOCK_UXN
365 }, {
366 /* SRAM0 (align with 2M) */
367 .virt = 0x22000000UL,
368 .phys = 0x22000000UL,
369 .size = 0x200000UL,
370 .attrs = PTE_BLOCK_MEMTYPE(MT_NORMAL) |
371 PTE_BLOCK_OUTER_SHARE |
372 PTE_BLOCK_PXN | PTE_BLOCK_UXN
373 }, {
374 /* Peripherals */
375 .virt = 0x27000000UL,
376 .phys = 0x27000000UL,
377 .size = 0x3000000UL,
378 .attrs = PTE_BLOCK_MEMTYPE(MT_DEVICE_NGNRNE) |
379 PTE_BLOCK_NON_SHARE |
380 PTE_BLOCK_PXN | PTE_BLOCK_UXN
381 }, {
382 /* Peripherals */
383 .virt = 0x2D000000UL,
384 .phys = 0x2D000000UL,
385 .size = 0x1600000UL,
386 .attrs = PTE_BLOCK_MEMTYPE(MT_DEVICE_NGNRNE) |
387 PTE_BLOCK_NON_SHARE |
388 PTE_BLOCK_PXN | PTE_BLOCK_UXN
389 }, {
390 /* FLEXSPI1-2 */
391 .virt = 0x40000000UL,
392 .phys = 0x40000000UL,
393 .size = 0x40000000UL,
394 .attrs = PTE_BLOCK_MEMTYPE(MT_DEVICE_NGNRNE) |
395 PTE_BLOCK_NON_SHARE |
396 PTE_BLOCK_PXN | PTE_BLOCK_UXN
397 }, {
398 /* DRAM1 */
399 .virt = 0x80000000UL,
400 .phys = 0x80000000UL,
401 .size = PHYS_SDRAM_SIZE,
402 .attrs = PTE_BLOCK_MEMTYPE(MT_NORMAL) |
403 PTE_BLOCK_OUTER_SHARE
404 }, {
405 /*
406 * empty entrie to split table entry 5
407 * if needed when TEEs are used
408 */
409 0,
410 }, {
411 /* List terminator */
412 0,
413 }
414};
415
416struct mm_region *mem_map = imx8ulp_arm64_mem_map;
417
Ji Luo2fd258c2022-04-06 14:30:28 +0800418static unsigned int imx8ulp_find_dram_entry_in_mem_map(void)
419{
420 int i;
421
422 for (i = 0; i < ARRAY_SIZE(imx8ulp_arm64_mem_map); i++)
423 if (imx8ulp_arm64_mem_map[i].phys == CONFIG_SYS_SDRAM_BASE)
424 return i;
425
426 hang(); /* Entry not found, this must never happen. */
427}
428
Peng Fanb15705a2021-08-07 16:00:35 +0800429/* simplify the page table size to enhance boot speed */
430#define MAX_PTE_ENTRIES 512
431#define MAX_MEM_MAP_REGIONS 16
432u64 get_page_table_size(void)
433{
434 u64 one_pt = MAX_PTE_ENTRIES * sizeof(u64);
435 u64 size = 0;
436
437 /*
438 * For each memory region, the max table size:
439 * 2 level 3 tables + 2 level 2 tables + 1 level 1 table
440 */
441 size = (2 + 2 + 1) * one_pt * MAX_MEM_MAP_REGIONS + one_pt;
442
443 /*
444 * We need to duplicate our page table once to have an emergency pt to
445 * resort to when splitting page tables later on
446 */
447 size *= 2;
448
449 /*
450 * We may need to split page tables later on if dcache settings change,
451 * so reserve up to 4 (random pick) page tables for that.
452 */
453 size += one_pt * 4;
454
455 return size;
456}
457
458void enable_caches(void)
459{
Ji Luo2fd258c2022-04-06 14:30:28 +0800460 /* If OPTEE runs, remove OPTEE memory from MMU table to avoid speculative prefetch */
461 if (rom_pointer[1]) {
462 /*
463 * TEE are loaded, So the ddr bank structures
464 * have been modified update mmu table accordingly
465 */
466 int i = 0;
467 int entry = imx8ulp_find_dram_entry_in_mem_map();
468 u64 attrs = imx8ulp_arm64_mem_map[entry].attrs;
469
470 while (i < CONFIG_NR_DRAM_BANKS &&
471 entry < ARRAY_SIZE(imx8ulp_arm64_mem_map)) {
472 if (gd->bd->bi_dram[i].start == 0)
473 break;
474 imx8ulp_arm64_mem_map[entry].phys = gd->bd->bi_dram[i].start;
475 imx8ulp_arm64_mem_map[entry].virt = gd->bd->bi_dram[i].start;
476 imx8ulp_arm64_mem_map[entry].size = gd->bd->bi_dram[i].size;
477 imx8ulp_arm64_mem_map[entry].attrs = attrs;
478 debug("Added memory mapping (%d): %llx %llx\n", entry,
479 imx8ulp_arm64_mem_map[entry].phys, imx8ulp_arm64_mem_map[entry].size);
480 i++; entry++;
481 }
482 }
Peng Fanb15705a2021-08-07 16:00:35 +0800483
484 icache_enable();
485 dcache_enable();
486}
487
Ji Luo2fd258c2022-04-06 14:30:28 +0800488__weak int board_phys_sdram_size(phys_size_t *size)
489{
490 if (!size)
491 return -EINVAL;
492
493 *size = PHYS_SDRAM_SIZE;
494 return 0;
495}
496
Peng Fanb15705a2021-08-07 16:00:35 +0800497int dram_init(void)
498{
Ji Luo2fd258c2022-04-06 14:30:28 +0800499 unsigned int entry = imx8ulp_find_dram_entry_in_mem_map();
500 phys_size_t sdram_size;
501 int ret;
502
503 ret = board_phys_sdram_size(&sdram_size);
504 if (ret)
505 return ret;
506
507 /* rom_pointer[1] contains the size of TEE occupies */
508 if (rom_pointer[1])
509 gd->ram_size = sdram_size - rom_pointer[1];
510 else
511 gd->ram_size = sdram_size;
512
513 /* also update the SDRAM size in the mem_map used externally */
514 imx8ulp_arm64_mem_map[entry].size = sdram_size;
515 return 0;
516}
517
518int dram_init_banksize(void)
519{
520 int bank = 0;
521 int ret;
522 phys_size_t sdram_size;
523
524 ret = board_phys_sdram_size(&sdram_size);
525 if (ret)
526 return ret;
527
528 gd->bd->bi_dram[bank].start = PHYS_SDRAM;
529 if (rom_pointer[1]) {
530 phys_addr_t optee_start = (phys_addr_t)rom_pointer[0];
531 phys_size_t optee_size = (size_t)rom_pointer[1];
532
533 gd->bd->bi_dram[bank].size = optee_start - gd->bd->bi_dram[bank].start;
534 if ((optee_start + optee_size) < (PHYS_SDRAM + sdram_size)) {
535 if (++bank >= CONFIG_NR_DRAM_BANKS) {
536 puts("CONFIG_NR_DRAM_BANKS is not enough\n");
537 return -1;
538 }
539
540 gd->bd->bi_dram[bank].start = optee_start + optee_size;
541 gd->bd->bi_dram[bank].size = PHYS_SDRAM +
542 sdram_size - gd->bd->bi_dram[bank].start;
543 }
544 } else {
545 gd->bd->bi_dram[bank].size = sdram_size;
546 }
Peng Fanb15705a2021-08-07 16:00:35 +0800547
548 return 0;
549}
550
Ji Luo2fd258c2022-04-06 14:30:28 +0800551phys_size_t get_effective_memsize(void)
552{
553 /* return the first bank as effective memory */
554 if (rom_pointer[1])
555 return ((phys_addr_t)rom_pointer[0] - PHYS_SDRAM);
556
557 return gd->ram_size;
558}
559
Tom Riniae21e7f2021-08-30 09:16:29 -0400560#ifdef CONFIG_ENV_VARS_UBOOT_RUNTIME_CONFIG
Peng Fanb15705a2021-08-07 16:00:35 +0800561void get_board_serial(struct tag_serialnr *serialnr)
562{
Ye Li7bea5b02021-08-07 16:01:00 +0800563 u32 uid[4];
564 u32 res;
565 int ret;
566
567 ret = ahab_read_common_fuse(1, uid, 4, &res);
568 if (ret)
569 printf("ahab read fuse failed %d, 0x%x\n", ret, res);
570 else
571 printf("UID 0x%x,0x%x,0x%x,0x%x\n", uid[0], uid[1], uid[2], uid[3]);
572
573 serialnr->low = uid[0];
574 serialnr->high = uid[3];
Peng Fanb15705a2021-08-07 16:00:35 +0800575}
576#endif
577
Ye Li6ee435eb2021-08-07 16:00:50 +0800578static void set_core0_reset_vector(u32 entry)
Peng Fanb15705a2021-08-07 16:00:35 +0800579{
Ye Li6dd43022021-08-07 16:00:48 +0800580 /* Update SIM1 DGO8 for reset vector base */
Ye Li6ee435eb2021-08-07 16:00:50 +0800581 writel(entry, SIM1_BASE_ADDR + 0x5c);
Ye Li6dd43022021-08-07 16:00:48 +0800582
583 /* set update bit */
584 setbits_le32(SIM1_BASE_ADDR + 0x8, 0x1 << 24);
585
586 /* polling the ack */
587 while ((readl(SIM1_BASE_ADDR + 0x8) & (0x1 << 26)) == 0)
588 ;
589
590 /* clear the update */
591 clrbits_le32(SIM1_BASE_ADDR + 0x8, (0x1 << 24));
592
593 /* clear the ack by set 1 */
594 setbits_le32(SIM1_BASE_ADDR + 0x8, (0x1 << 26));
Ye Li6ee435eb2021-08-07 16:00:50 +0800595}
596
Peng Fan9c87e462021-08-07 16:00:59 +0800597static int trdc_set_access(void)
Peng Fanb5c41b12021-08-07 16:00:58 +0800598{
599 /*
Peng Fan9c87e462021-08-07 16:00:59 +0800600 * TRDC mgr + 4 MBC + 2 MRC.
601 * S400 should already configure when release RDC
602 * A35 only map non-secure region for pbridge0 and 1, set sec_access to false
Peng Fanb5c41b12021-08-07 16:00:58 +0800603 */
Peng Fan9c87e462021-08-07 16:00:59 +0800604 trdc_mbc_set_access(2, 7, 0, 49, false);
605 trdc_mbc_set_access(2, 7, 0, 50, false);
606 trdc_mbc_set_access(2, 7, 0, 51, false);
607 trdc_mbc_set_access(2, 7, 0, 52, false);
608 trdc_mbc_set_access(2, 7, 0, 53, false);
609 trdc_mbc_set_access(2, 7, 0, 54, false);
Peng Fanb5c41b12021-08-07 16:00:58 +0800610
Peng Fan9c87e462021-08-07 16:00:59 +0800611 /* CGC0: PBridge0 slot 47 */
612 trdc_mbc_set_access(2, 7, 0, 47, false);
Peng Fanb5c41b12021-08-07 16:00:58 +0800613
Peng Fan9c87e462021-08-07 16:00:59 +0800614 /* Iomuxc0: : PBridge1 slot 33 */
615 trdc_mbc_set_access(2, 7, 1, 33, false);
Peng Fanb5c41b12021-08-07 16:00:58 +0800616
Ye Lid325d372021-10-29 09:46:20 +0800617 /* flexspi0 */
618 trdc_mrc_region_set_access(0, 7, 0x04000000, 0x0c000000, false);
Ye Li27666ca2021-10-29 09:46:21 +0800619
620 /* tpm0: PBridge1 slot 21 */
621 trdc_mbc_set_access(2, 7, 1, 21, false);
622 /* lpi2c0: PBridge1 slot 24 */
623 trdc_mbc_set_access(2, 7, 1, 24, false);
Peng Fanb5c41b12021-08-07 16:00:58 +0800624 return 0;
625}
626
Ye Liec10a802022-04-06 14:30:17 +0800627void lpav_configure(bool lpav_to_m33)
Ye Li43819eb2021-10-29 09:46:16 +0800628{
Ye Liec10a802022-04-06 14:30:17 +0800629 if (!lpav_to_m33)
630 setbits_le32(SIM_SEC_BASE_ADDR + 0x44, BIT(7)); /* LPAV to APD */
Ye Li43819eb2021-10-29 09:46:16 +0800631
Peng Fanfa609b42021-10-29 09:46:17 +0800632 /* PXP/GPU 2D/3D/DCNANO/MIPI_DSI/EPDC/HIFI4 to APD */
633 setbits_le32(SIM_SEC_BASE_ADDR + 0x4c, 0x7F);
Ye Li43819eb2021-10-29 09:46:16 +0800634
635 /* LPAV slave/dma2 ch allocation and request allocation to APD */
636 writel(0x1f, SIM_SEC_BASE_ADDR + 0x50);
637 writel(0xffffffff, SIM_SEC_BASE_ADDR + 0x54);
638 writel(0x003fffff, SIM_SEC_BASE_ADDR + 0x58);
Ye Li715cfa02021-10-29 09:46:23 +0800639}
Ye Lia0311552021-10-29 09:46:22 +0800640
Ye Li133f8b82021-10-29 09:46:25 +0800641void load_lposc_fuse(void)
642{
643 int ret;
644 u32 val = 0, val2 = 0, reg;
645
646 ret = fuse_read(25, 0, &val);
647 if (ret)
648 return; /* failed */
649
650 ret = fuse_read(25, 1, &val2);
651 if (ret)
652 return; /* failed */
653
654 /* LPOSCCTRL */
655 reg = readl(0x2802f304);
656 reg &= ~0xff;
657 reg |= (val & 0xff);
658 writel(reg, 0x2802f304);
659}
660
Ye Li715cfa02021-10-29 09:46:23 +0800661void set_lpav_qos(void)
662{
Ye Lia0311552021-10-29 09:46:22 +0800663 /* Set read QoS of dcnano on LPAV NIC */
664 writel(0xf, 0x2e447100);
Ye Li43819eb2021-10-29 09:46:16 +0800665}
666
Ye Li6ee435eb2021-08-07 16:00:50 +0800667int arch_cpu_init(void)
668{
669 if (IS_ENABLED(CONFIG_SPL_BUILD)) {
Ye Li72012622021-10-29 09:46:15 +0800670 u32 val = 0;
671 int ret;
672 bool rdc_en = true; /* Default assume DBD_EN is set */
673
Peng Fan21bda432022-04-06 14:30:27 +0800674 /* Enable System Reset Interrupt using WDOG_AD */
675 setbits_le32(CMC1_BASE_ADDR + 0x8C, BIT(13));
676 /* Clear AD_PERIPH Power switch domain out of reset interrupt flag */
677 setbits_le32(CMC1_BASE_ADDR + 0x70, BIT(4));
678
679 if (readl(CMC1_BASE_ADDR + 0x90) & BIT(13)) {
680 /* Clear System Reset Interrupt Flag Register of WDOG_AD */
681 setbits_le32(CMC1_BASE_ADDR + 0x90, BIT(13));
682 /* Reset WDOG to clear reset request */
683 pcc_reset_peripheral(3, WDOG3_PCC3_SLOT, true);
684 pcc_reset_peripheral(3, WDOG3_PCC3_SLOT, false);
685 }
686
Ye Li853cc9d2021-08-07 16:00:55 +0800687 /* Disable wdog */
688 init_wdog();
689
Ye Li72012622021-10-29 09:46:15 +0800690 /* Read DBD_EN fuse */
691 ret = fuse_read(8, 1, &val);
692 if (!ret)
693 rdc_en = !!(val & 0x4000);
694
Peng Fanb5c41b12021-08-07 16:00:58 +0800695 if (get_boot_mode() == SINGLE_BOOT) {
Ye Li72012622021-10-29 09:46:15 +0800696 if (rdc_en)
697 release_rdc(RDC_TRDC);
698
Peng Fanb5c41b12021-08-07 16:00:58 +0800699 trdc_set_access();
Ye Liec10a802022-04-06 14:30:17 +0800700 lpav_configure(false);
701 } else {
702 lpav_configure(true);
Peng Fanb5c41b12021-08-07 16:00:58 +0800703 }
Peng Fanfa55b212021-08-07 16:00:57 +0800704
Ye Li72012622021-10-29 09:46:15 +0800705 /* Release xrdc, then allow A35 to write SRAM2 */
706 if (rdc_en)
707 release_rdc(RDC_XRDC);
708
Ye Li853cc9d2021-08-07 16:00:55 +0800709 xrdc_mrc_region_set_access(2, CONFIG_SPL_TEXT_BASE, 0xE00);
710
Peng Fan4cdb3a32022-04-06 14:30:12 +0800711 clock_init_early();
Ye Li6ee435eb2021-08-07 16:00:50 +0800712 } else {
713 /* reconfigure core0 reset vector to ROM */
714 set_core0_reset_vector(0x1000);
715 }
716
717 return 0;
718}
719
Simon Glassfc557362022-03-04 08:43:05 -0700720static int imx8ulp_check_mu(void *ctx, struct event *event)
Ye Li7bea5b02021-08-07 16:01:00 +0800721{
722 struct udevice *devp;
723 int node, ret;
724
725 node = fdt_node_offset_by_compatible(gd->fdt_blob, -1, "fsl,imx8ulp-mu");
726
727 ret = uclass_get_device_by_of_offset(UCLASS_MISC, node, &devp);
728 if (ret) {
729 printf("could not get S400 mu %d\n", ret);
730 return ret;
731 }
732
733 return 0;
734}
Simon Glassfc557362022-03-04 08:43:05 -0700735EVENT_SPY(EVT_DM_POST_INIT, imx8ulp_check_mu);
Ye Li7bea5b02021-08-07 16:01:00 +0800736
Ye Li6ee435eb2021-08-07 16:00:50 +0800737#if defined(CONFIG_SPL_BUILD)
738__weak void __noreturn jump_to_image_no_args(struct spl_image_info *spl_image)
739{
740 debug("image entry point: 0x%lx\n", spl_image->entry_point);
741
742 set_core0_reset_vector((u32)spl_image->entry_point);
Ye Li6dd43022021-08-07 16:00:48 +0800743
744 /* Enable the 512KB cache */
745 setbits_le32(SIM1_BASE_ADDR + 0x30, (0x1 << 4));
746
747 /* reset core */
748 setbits_le32(SIM1_BASE_ADDR + 0x30, (0x1 << 16));
749
750 while (1)
751 ;
752}
753#endif
Peng Fanfa6ae052021-08-07 16:01:03 +0800754
755void imx_get_mac_from_fuse(int dev_id, unsigned char *mac)
756{
Ye Li992b0ae2021-10-29 09:46:28 +0800757 u32 val[2] = {};
758 int ret;
759
760 ret = fuse_read(5, 3, &val[0]);
761 if (ret)
762 goto err;
763
764 ret = fuse_read(5, 4, &val[1]);
765 if (ret)
766 goto err;
767
768 mac[0] = val[0];
769 mac[1] = val[0] >> 8;
770 mac[2] = val[0] >> 16;
771 mac[3] = val[0] >> 24;
772 mac[4] = val[1];
773 mac[5] = val[1] >> 8;
774
775 debug("%s: MAC%d: %02x.%02x.%02x.%02x.%02x.%02x\n",
776 __func__, dev_id, mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
777 return;
778err:
Peng Fanfa6ae052021-08-07 16:01:03 +0800779 memset(mac, 0, 6);
Ye Li992b0ae2021-10-29 09:46:28 +0800780 printf("%s: fuse read err: %d\n", __func__, ret);
Peng Fanfa6ae052021-08-07 16:01:03 +0800781}
Ye Li479fd4a2021-08-07 16:01:08 +0800782
783int (*card_emmc_is_boot_part_en)(void) = (void *)0x67cc;
784u32 spl_arch_boot_image_offset(u32 image_offset, u32 rom_bt_dev)
785{
786 /* Hard code for eMMC image_offset on 8ULP ROM, need fix by ROM, temp workaround */
787 if (((rom_bt_dev >> 16) & 0xff) == BT_DEV_TYPE_MMC && card_emmc_is_boot_part_en())
788 image_offset = 0;
789
790 return image_offset;
791}
Ye Li48836ae2022-04-06 14:30:30 +0800792
793enum env_location env_get_location(enum env_operation op, int prio)
794{
795 enum boot_device dev = get_boot_device();
796 enum env_location env_loc = ENVL_UNKNOWN;
797
798 if (prio)
799 return env_loc;
800
801 switch (dev) {
802#ifdef CONFIG_ENV_IS_IN_SPI_FLASH
803 case QSPI_BOOT:
804 env_loc = ENVL_SPI_FLASH;
805 break;
806#endif
807#ifdef CONFIG_ENV_IS_IN_MMC
808 case SD1_BOOT:
809 case SD2_BOOT:
810 case SD3_BOOT:
811 case MMC1_BOOT:
812 case MMC2_BOOT:
813 case MMC3_BOOT:
814 env_loc = ENVL_MMC;
815 break;
816#endif
817 default:
818#if defined(CONFIG_ENV_IS_NOWHERE)
819 env_loc = ENVL_NOWHERE;
820#endif
821 break;
822 }
823
824 return env_loc;
825}