1 // SPDX-License-Identifier: GPL-2.0+
7 #include <asm/arch/clock.h>
8 #include <asm/arch/imx-regs.h>
9 #include <asm/arch/sys_proto.h>
10 #include <asm/armv8/mmu.h>
11 #include <asm/mach-imx/boot_mode.h>
12 #include <asm/global_data.h>
13 #include <efi_loader.h>
16 #include <asm/arch/rdc.h>
17 #include <asm/mach-imx/s400_api.h>
18 #include <asm/mach-imx/mu_hal.h>
20 #include <asm/setup.h>
22 #include <dm/device-internal.h>
24 #include <dm/uclass.h>
25 #include <dm/device.h>
26 #include <dm/uclass-internal.h>
29 #include <linux/iopoll.h>
31 #include <env_internal.h>
33 DECLARE_GLOBAL_DATA_PTR;
35 struct rom_api *g_rom_api = (struct rom_api *)0x1980;
37 bool is_usb_boot(void)
39 return get_boot_device() == USB_BOOT;
42 #ifdef CONFIG_ENV_IS_IN_MMC
43 __weak int board_mmc_get_env_dev(int devno)
48 int mmc_get_env_dev(void)
55 ret = rom_api_query_boot_infor(QUERY_BT_DEV, &boot);
57 if (ret != ROM_API_OKAY) {
58 puts("ROMAPI: failure at query_boot_info\n");
59 return CONFIG_SYS_MMC_ENV_DEV;
62 boot_type = boot >> 16;
63 boot_instance = (boot >> 8) & 0xff;
65 /* If not boot from sd/mmc, use default value */
66 if (boot_type != BOOT_TYPE_SD && boot_type != BOOT_TYPE_MMC)
67 return env_get_ulong("mmcdev", 10, CONFIG_SYS_MMC_ENV_DEV);
69 return board_mmc_get_env_dev(boot_instance);
73 static void set_cpu_info(struct sentinel_get_info_data *info)
75 gd->arch.soc_rev = info->soc;
76 gd->arch.lifecycle = info->lc;
77 memcpy((void *)&gd->arch.uid, &info->uid, 4 * sizeof(u32));
82 u32 rev = (gd->arch.soc_rev >> 24) - 0xa0;
84 return (MXC_CPU_IMX8ULP << 12) | (CHIP_REV_1_0 + rev);
87 enum bt_mode get_boot_mode(void)
91 bt0_cfg = readl(SIM_SEC_BASE_ADDR + 0x24);
92 bt0_cfg &= (BT0CFG_LPBOOT_MASK | BT0CFG_DUALBOOT_MASK);
94 if (!(bt0_cfg & BT0CFG_LPBOOT_MASK)) {
95 /* No low power boot */
96 if (bt0_cfg & BT0CFG_DUALBOOT_MASK)
102 return LOW_POWER_BOOT;
105 bool m33_image_booted(void)
110 gp6 = readl(SIM_SEC_BASE_ADDR + 0x28);
117 int m33_image_handshake(ulong timeout_ms)
121 ulong timeout_us = timeout_ms * 1000;
123 /* Notify m33 that it's ready to do init srtm(enable mu receive interrupt and so on) */
124 setbits_le32(MU0_B_BASE_ADDR + 0x100, BIT(0)); /* set FCR F0 flag of MU0_MUB */
127 * Wait m33 to set FCR F0 flag of MU0_MUA
128 * Clear FCR F0 flag of MU0_MUB after m33 has set FCR F0 flag of MU0_MUA
130 ret = readl_poll_sleep_timeout(MU0_B_BASE_ADDR + 0x104, fsr, fsr & BIT(0), 10, timeout_us);
132 clrbits_le32(MU0_B_BASE_ADDR + 0x100, BIT(0));
137 #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)
152 static char *get_reset_cause(char *ret)
154 u32 cause1, cause = 0, srs = 0;
155 void __iomem *reg_ssrs = (void __iomem *)(CMC1_BASE_ADDR + 0x88);
156 void __iomem *reg_srs = (void __iomem *)(CMC1_BASE_ADDR + 0x80);
161 srs = readl(reg_srs);
162 cause1 = readl(reg_ssrs);
164 cause = srs & (CMC_SRS_POR | CMC_SRS_WUP | CMC_SRS_WARM);
168 sprintf(ret, "%s", "POR");
171 sprintf(ret, "%s", "WUP");
174 cause = srs & (CMC_SRS_WDG | CMC_SRS_SW |
178 sprintf(ret, "%s", "WARM-WDG");
181 sprintf(ret, "%s", "WARM-SW");
183 case CMC_SRS_JTAG_RST:
184 sprintf(ret, "%s", "WARM-JTAG");
187 sprintf(ret, "%s", "WARM-UNKN");
192 sprintf(ret, "%s-%X", "UNKN", srs);
196 debug("[%X] SRS[%X] %X - ", cause1, srs, srs ^ cause1);
200 #if defined(CONFIG_DISPLAY_CPUINFO)
201 const char *get_imx_type(u32 imxtype)
206 int print_cpuinfo(void)
211 cpurev = get_cpu_rev();
213 printf("CPU: i.MX%s rev%d.%d at %d MHz\n",
214 get_imx_type((cpurev & 0xFF000) >> 12),
215 (cpurev & 0x000F0) >> 4, (cpurev & 0x0000F) >> 0,
216 mxc_get_clock(MXC_ARM_CLK) / 1000000);
218 #if defined(CONFIG_IMX_PMC_TEMPERATURE)
219 struct udevice *udev;
222 ret = uclass_get_device(UCLASS_THERMAL, 0, &udev);
224 ret = thermal_get_temp(udev, &temp);
226 printf("CPU current temperature: %d\n", temp);
228 debug(" - failed to get CPU current temperature\n");
230 debug(" - failed to get CPU current temperature\n");
234 printf("Reset cause: %s\n", get_reset_cause(cause));
236 printf("Boot mode: ");
237 switch (get_boot_mode()) {
239 printf("Low power boot\n");
242 printf("Dual boot\n");
246 printf("Single boot\n");
254 #define UNLOCK_WORD0 0xC520 /* 1st unlock word */
255 #define UNLOCK_WORD1 0xD928 /* 2nd unlock word */
256 #define REFRESH_WORD0 0xA602 /* 1st refresh word */
257 #define REFRESH_WORD1 0xB480 /* 2nd refresh word */
259 static void disable_wdog(void __iomem *wdog_base)
261 u32 val_cs = readl(wdog_base + 0x00);
263 if (!(val_cs & 0x80))
267 __raw_writel(REFRESH_WORD0, (wdog_base + 0x04)); /* Refresh the CNT */
268 __raw_writel(REFRESH_WORD1, (wdog_base + 0x04));
271 if (!(val_cs & 800)) {
273 __raw_writel(UNLOCK_WORD0, (wdog_base + 0x04));
274 __raw_writel(UNLOCK_WORD1, (wdog_base + 0x04));
277 while (!(readl(wdog_base + 0x00) & 0x800))
280 writel(0x0, (wdog_base + 0x0C)); /* Set WIN to 0 */
281 writel(0x400, (wdog_base + 0x08)); /* Set timeout to default 0x400 */
282 writel(0x120, (wdog_base + 0x00)); /* Disable it and set update */
284 while (!(readl(wdog_base + 0x00) & 0x400))
290 disable_wdog((void __iomem *)WDG3_RBASE);
293 static struct mm_region imx8ulp_arm64_mem_map[] = {
299 .attrs = PTE_BLOCK_MEMTYPE(MT_NORMAL) |
300 PTE_BLOCK_OUTER_SHARE
306 .size = 0x08000000UL,
307 .attrs = PTE_BLOCK_MEMTYPE(MT_DEVICE_NGNRNE) |
308 PTE_BLOCK_NON_SHARE |
309 PTE_BLOCK_PXN | PTE_BLOCK_UXN
312 /* SSRAM (align with 2M) */
313 .virt = 0x1FE00000UL,
314 .phys = 0x1FE00000UL,
316 .attrs = PTE_BLOCK_MEMTYPE(MT_NORMAL) |
317 PTE_BLOCK_OUTER_SHARE |
318 PTE_BLOCK_PXN | PTE_BLOCK_UXN
320 /* SRAM1 (align with 2M) */
321 .virt = 0x21000000UL,
322 .phys = 0x21000000UL,
324 .attrs = PTE_BLOCK_MEMTYPE(MT_NORMAL) |
325 PTE_BLOCK_OUTER_SHARE |
326 PTE_BLOCK_PXN | PTE_BLOCK_UXN
328 /* SRAM0 (align with 2M) */
329 .virt = 0x22000000UL,
330 .phys = 0x22000000UL,
332 .attrs = PTE_BLOCK_MEMTYPE(MT_NORMAL) |
333 PTE_BLOCK_OUTER_SHARE |
334 PTE_BLOCK_PXN | PTE_BLOCK_UXN
337 .virt = 0x27000000UL,
338 .phys = 0x27000000UL,
340 .attrs = PTE_BLOCK_MEMTYPE(MT_DEVICE_NGNRNE) |
341 PTE_BLOCK_NON_SHARE |
342 PTE_BLOCK_PXN | PTE_BLOCK_UXN
345 .virt = 0x2D000000UL,
346 .phys = 0x2D000000UL,
348 .attrs = PTE_BLOCK_MEMTYPE(MT_DEVICE_NGNRNE) |
349 PTE_BLOCK_NON_SHARE |
350 PTE_BLOCK_PXN | PTE_BLOCK_UXN
353 .virt = 0x40000000UL,
354 .phys = 0x40000000UL,
355 .size = 0x40000000UL,
356 .attrs = PTE_BLOCK_MEMTYPE(MT_DEVICE_NGNRNE) |
357 PTE_BLOCK_NON_SHARE |
358 PTE_BLOCK_PXN | PTE_BLOCK_UXN
361 .virt = 0x80000000UL,
362 .phys = 0x80000000UL,
363 .size = PHYS_SDRAM_SIZE,
364 .attrs = PTE_BLOCK_MEMTYPE(MT_NORMAL) |
365 PTE_BLOCK_OUTER_SHARE
368 * empty entrie to split table entry 5
369 * if needed when TEEs are used
373 /* List terminator */
378 struct mm_region *mem_map = imx8ulp_arm64_mem_map;
380 static unsigned int imx8ulp_find_dram_entry_in_mem_map(void)
384 for (i = 0; i < ARRAY_SIZE(imx8ulp_arm64_mem_map); i++)
385 if (imx8ulp_arm64_mem_map[i].phys == CFG_SYS_SDRAM_BASE)
388 hang(); /* Entry not found, this must never happen. */
391 /* simplify the page table size to enhance boot speed */
392 #define MAX_PTE_ENTRIES 512
393 #define MAX_MEM_MAP_REGIONS 16
394 u64 get_page_table_size(void)
396 u64 one_pt = MAX_PTE_ENTRIES * sizeof(u64);
400 * For each memory region, the max table size:
401 * 2 level 3 tables + 2 level 2 tables + 1 level 1 table
403 size = (2 + 2 + 1) * one_pt * MAX_MEM_MAP_REGIONS + one_pt;
406 * We need to duplicate our page table once to have an emergency pt to
407 * resort to when splitting page tables later on
412 * We may need to split page tables later on if dcache settings change,
413 * so reserve up to 4 (random pick) page tables for that.
420 void enable_caches(void)
422 /* If OPTEE runs, remove OPTEE memory from MMU table to avoid speculative prefetch */
423 if (rom_pointer[1]) {
425 * TEE are loaded, So the ddr bank structures
426 * have been modified update mmu table accordingly
429 int entry = imx8ulp_find_dram_entry_in_mem_map();
430 u64 attrs = imx8ulp_arm64_mem_map[entry].attrs;
432 while (i < CONFIG_NR_DRAM_BANKS &&
433 entry < ARRAY_SIZE(imx8ulp_arm64_mem_map)) {
434 if (gd->bd->bi_dram[i].start == 0)
436 imx8ulp_arm64_mem_map[entry].phys = gd->bd->bi_dram[i].start;
437 imx8ulp_arm64_mem_map[entry].virt = gd->bd->bi_dram[i].start;
438 imx8ulp_arm64_mem_map[entry].size = gd->bd->bi_dram[i].size;
439 imx8ulp_arm64_mem_map[entry].attrs = attrs;
440 debug("Added memory mapping (%d): %llx %llx\n", entry,
441 imx8ulp_arm64_mem_map[entry].phys, imx8ulp_arm64_mem_map[entry].size);
450 __weak int board_phys_sdram_size(phys_size_t *size)
455 *size = PHYS_SDRAM_SIZE;
461 unsigned int entry = imx8ulp_find_dram_entry_in_mem_map();
462 phys_size_t sdram_size;
465 ret = board_phys_sdram_size(&sdram_size);
469 /* rom_pointer[1] contains the size of TEE occupies */
471 gd->ram_size = sdram_size - rom_pointer[1];
473 gd->ram_size = sdram_size;
475 /* also update the SDRAM size in the mem_map used externally */
476 imx8ulp_arm64_mem_map[entry].size = sdram_size;
480 int dram_init_banksize(void)
484 phys_size_t sdram_size;
486 ret = board_phys_sdram_size(&sdram_size);
490 gd->bd->bi_dram[bank].start = PHYS_SDRAM;
491 if (rom_pointer[1]) {
492 phys_addr_t optee_start = (phys_addr_t)rom_pointer[0];
493 phys_size_t optee_size = (size_t)rom_pointer[1];
495 gd->bd->bi_dram[bank].size = optee_start - gd->bd->bi_dram[bank].start;
496 if ((optee_start + optee_size) < (PHYS_SDRAM + sdram_size)) {
497 if (++bank >= CONFIG_NR_DRAM_BANKS) {
498 puts("CONFIG_NR_DRAM_BANKS is not enough\n");
502 gd->bd->bi_dram[bank].start = optee_start + optee_size;
503 gd->bd->bi_dram[bank].size = PHYS_SDRAM +
504 sdram_size - gd->bd->bi_dram[bank].start;
507 gd->bd->bi_dram[bank].size = sdram_size;
513 phys_size_t get_effective_memsize(void)
515 /* return the first bank as effective memory */
517 return ((phys_addr_t)rom_pointer[0] - PHYS_SDRAM);
522 #ifdef CONFIG_ENV_VARS_UBOOT_RUNTIME_CONFIG
523 void get_board_serial(struct tag_serialnr *serialnr)
529 ret = ahab_read_common_fuse(1, uid, 4, &res);
531 printf("ahab read fuse failed %d, 0x%x\n", ret, res);
533 printf("UID 0x%x,0x%x,0x%x,0x%x\n", uid[0], uid[1], uid[2], uid[3]);
535 serialnr->low = uid[0];
536 serialnr->high = uid[3];
540 static void set_core0_reset_vector(u32 entry)
542 /* Update SIM1 DGO8 for reset vector base */
543 writel(entry, SIM1_BASE_ADDR + 0x5c);
546 setbits_le32(SIM1_BASE_ADDR + 0x8, 0x1 << 24);
548 /* polling the ack */
549 while ((readl(SIM1_BASE_ADDR + 0x8) & (0x1 << 26)) == 0)
552 /* clear the update */
553 clrbits_le32(SIM1_BASE_ADDR + 0x8, (0x1 << 24));
555 /* clear the ack by set 1 */
556 setbits_le32(SIM1_BASE_ADDR + 0x8, (0x1 << 26));
560 int trdc_set_access(void)
563 * TRDC mgr + 4 MBC + 2 MRC.
565 trdc_mbc_set_access(2, 7, 0, 49, true);
566 trdc_mbc_set_access(2, 7, 0, 50, true);
567 trdc_mbc_set_access(2, 7, 0, 51, true);
568 trdc_mbc_set_access(2, 7, 0, 52, true);
569 trdc_mbc_set_access(2, 7, 0, 53, true);
570 trdc_mbc_set_access(2, 7, 0, 54, true);
572 /* 0x1fff8000 used for resource table by remoteproc */
573 trdc_mbc_set_access(0, 7, 2, 31, false);
575 /* CGC0: PBridge0 slot 47 and PCC0 slot 48 */
576 trdc_mbc_set_access(2, 7, 0, 47, false);
577 trdc_mbc_set_access(2, 7, 0, 48, false);
580 trdc_mbc_set_access(2, 7, 1, 17, false);
581 trdc_mbc_set_access(2, 7, 1, 34, false);
583 /* Iomuxc0: : PBridge1 slot 33 */
584 trdc_mbc_set_access(2, 7, 1, 33, false);
587 trdc_mbc_set_access(2, 7, 0, 57, false);
588 trdc_mrc_region_set_access(0, 7, 0x04000000, 0x0c000000, false);
590 /* tpm0: PBridge1 slot 21 */
591 trdc_mbc_set_access(2, 7, 1, 21, false);
592 /* lpi2c0: PBridge1 slot 24 */
593 trdc_mbc_set_access(2, 7, 1, 24, false);
595 /* Allow M33 to access TRDC MGR */
596 trdc_mbc_set_access(2, 6, 0, 49, true);
597 trdc_mbc_set_access(2, 6, 0, 50, true);
598 trdc_mbc_set_access(2, 6, 0, 51, true);
599 trdc_mbc_set_access(2, 6, 0, 52, true);
600 trdc_mbc_set_access(2, 6, 0, 53, true);
601 trdc_mbc_set_access(2, 6, 0, 54, true);
603 /* Set SAI0 for eDMA 0, NS */
604 trdc_mbc_set_access(2, 0, 1, 28, false);
606 /* Set SSRAM for eDMA0 access */
607 trdc_mbc_set_access(0, 0, 2, 0, false);
608 trdc_mbc_set_access(0, 0, 2, 1, false);
609 trdc_mbc_set_access(0, 0, 2, 2, false);
610 trdc_mbc_set_access(0, 0, 2, 3, false);
611 trdc_mbc_set_access(0, 0, 2, 4, false);
612 trdc_mbc_set_access(0, 0, 2, 5, false);
613 trdc_mbc_set_access(0, 0, 2, 6, false);
614 trdc_mbc_set_access(0, 0, 2, 7, false);
616 writel(0x800000a0, 0x28031840);
621 void lpav_configure(bool lpav_to_m33)
624 setbits_le32(SIM_SEC_BASE_ADDR + 0x44, BIT(7)); /* LPAV to APD */
626 /* PXP/GPU 2D/3D/DCNANO/MIPI_DSI/EPDC/HIFI4 to APD */
627 setbits_le32(SIM_SEC_BASE_ADDR + 0x4c, 0x7F);
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);
635 void load_lposc_fuse(void)
638 u32 val = 0, val2 = 0, reg;
640 ret = fuse_read(25, 0, &val);
644 ret = fuse_read(25, 1, &val2);
649 reg = readl(0x2802f304);
652 writel(reg, 0x2802f304);
655 void set_lpav_qos(void)
657 /* Set read QoS of dcnano on LPAV NIC */
658 writel(0xf, 0x2e447100);
661 int arch_cpu_init(void)
663 if (IS_ENABLED(CONFIG_SPL_BUILD)) {
666 bool rdc_en = true; /* Default assume DBD_EN is set */
668 /* 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));
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);
684 /* Read DBD_EN fuse */
685 ret = fuse_read(8, 1, &val);
687 rdc_en = !!(val & 0x4000);
689 if (get_boot_mode() == SINGLE_BOOT)
690 lpav_configure(false);
692 lpav_configure(true);
694 /* Release xrdc, then allow A35 to write SRAM2 */
696 release_rdc(RDC_XRDC);
698 xrdc_mrc_region_set_access(2, CONFIG_SPL_TEXT_BASE, 0xE00);
702 /* reconfigure core0 reset vector to ROM */
703 set_core0_reset_vector(0x1000);
709 int imx8ulp_dm_post_init(void)
711 struct udevice *devp;
714 struct sentinel_get_info_data *info = (struct sentinel_get_info_data *)SRAM0_BASE;
716 ret = uclass_get_device_by_driver(UCLASS_MISC, DM_DRIVER_GET(imx8ulp_mu), &devp);
718 printf("could not get S400 mu %d\n", ret);
722 ret = ahab_get_info(info, &res);
724 printf("ahab_get_info failed %d\n", ret);
725 /* fallback to A0.1 revision */
726 memset((void *)info, 0, sizeof(struct sentinel_get_info_data));
727 info->soc = 0xa000084d;
735 static int imx8ulp_evt_dm_post_init(void *ctx, struct event *event)
737 return imx8ulp_dm_post_init();
739 EVENT_SPY(EVT_DM_POST_INIT, imx8ulp_evt_dm_post_init);
741 #if defined(CONFIG_SPL_BUILD)
742 __weak void __noreturn jump_to_image_no_args(struct spl_image_info *spl_image)
744 debug("image entry point: 0x%lx\n", spl_image->entry_point);
746 set_core0_reset_vector((u32)spl_image->entry_point);
748 /* Enable the 512KB cache */
749 setbits_le32(SIM1_BASE_ADDR + 0x30, (0x1 << 4));
752 setbits_le32(SIM1_BASE_ADDR + 0x30, (0x1 << 16));
759 void imx_get_mac_from_fuse(int dev_id, unsigned char *mac)
764 ret = fuse_read(5, 3, &val[0]);
768 ret = fuse_read(5, 4, &val[1]);
773 mac[1] = val[0] >> 8;
774 mac[2] = val[0] >> 16;
775 mac[3] = val[0] >> 24;
777 mac[5] = val[1] >> 8;
779 debug("%s: MAC%d: %02x.%02x.%02x.%02x.%02x.%02x\n",
780 __func__, dev_id, mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
784 printf("%s: fuse read err: %d\n", __func__, ret);
787 int (*card_emmc_is_boot_part_en)(void) = (void *)0x67cc;
788 u32 spl_arch_boot_image_offset(u32 image_offset, u32 rom_bt_dev)
790 /* Hard code for eMMC image_offset on 8ULP ROM, need fix by ROM, temp workaround */
791 if (is_soc_rev(CHIP_REV_1_0) && ((rom_bt_dev >> 16) & 0xff) == BT_DEV_TYPE_MMC &&
792 card_emmc_is_boot_part_en())
798 enum env_location env_get_location(enum env_operation op, int prio)
800 enum boot_device dev = get_boot_device();
801 enum env_location env_loc = ENVL_UNKNOWN;
807 #ifdef CONFIG_ENV_IS_IN_SPI_FLASH
809 env_loc = ENVL_SPI_FLASH;
812 #ifdef CONFIG_ENV_IS_IN_MMC
823 #if defined(CONFIG_ENV_IS_NOWHERE)
824 env_loc = ENVL_NOWHERE;