0d7858a02d4a2e4892d2203c07423002e631fbca
[platform/kernel/u-boot.git] / arch / arm / mach-imx / imx8ulp / soc.c
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Copyright 2021 NXP
4  */
5
6 #include <asm/io.h>
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>
14 #include <event.h>
15 #include <spl.h>
16 #include <asm/arch/rdc.h>
17 #include <asm/mach-imx/s400_api.h>
18 #include <asm/mach-imx/mu_hal.h>
19 #include <cpu_func.h>
20 #include <asm/setup.h>
21 #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>
27 #include <fuse.h>
28 #include <thermal.h>
29 #include <linux/iopoll.h>
30 #include <env.h>
31 #include <env_internal.h>
32
33 DECLARE_GLOBAL_DATA_PTR;
34
35 struct rom_api *g_rom_api = (struct rom_api *)0x1980;
36
37 bool is_usb_boot(void)
38 {
39         return get_boot_device() == USB_BOOT;
40 }
41
42 #ifdef CONFIG_ENV_IS_IN_MMC
43 __weak int board_mmc_get_env_dev(int devno)
44 {
45         return devno;
46 }
47
48 int mmc_get_env_dev(void)
49 {
50         int ret;
51         u32 boot;
52         u16 boot_type;
53         u8 boot_instance;
54
55         ret = rom_api_query_boot_infor(QUERY_BT_DEV, &boot);
56
57         if (ret != ROM_API_OKAY) {
58                 puts("ROMAPI: failure at query_boot_info\n");
59                 return CONFIG_SYS_MMC_ENV_DEV;
60         }
61
62         boot_type = boot >> 16;
63         boot_instance = (boot >> 8) & 0xff;
64
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);
68
69         return board_mmc_get_env_dev(boot_instance);
70 }
71 #endif
72
73 static void set_cpu_info(struct sentinel_get_info_data *info)
74 {
75         gd->arch.soc_rev = info->soc;
76         gd->arch.lifecycle = info->lc;
77         memcpy((void *)&gd->arch.uid, &info->uid, 4 * sizeof(u32));
78 }
79
80 u32 get_cpu_rev(void)
81 {
82         u32 rev = (gd->arch.soc_rev >> 24) - 0xa0;
83
84         return (MXC_CPU_IMX8ULP << 12) | (CHIP_REV_1_0 + rev);
85 }
86
87 enum bt_mode get_boot_mode(void)
88 {
89         u32 bt0_cfg = 0;
90
91         bt0_cfg = readl(SIM_SEC_BASE_ADDR + 0x24);
92         bt0_cfg &= (BT0CFG_LPBOOT_MASK | BT0CFG_DUALBOOT_MASK);
93
94         if (!(bt0_cfg & BT0CFG_LPBOOT_MASK)) {
95                 /* No low power boot */
96                 if (bt0_cfg & BT0CFG_DUALBOOT_MASK)
97                         return DUAL_BOOT;
98                 else
99                         return SINGLE_BOOT;
100         }
101
102         return LOW_POWER_BOOT;
103 }
104
105 bool m33_image_booted(void)
106 {
107         u32 gp6;
108
109         /* DGO_GP6 */
110         gp6 = readl(SIM_SEC_BASE_ADDR + 0x28);
111         if (gp6 & BIT(5))
112                 return true;
113
114         return false;
115 }
116
117 int m33_image_handshake(ulong timeout_ms)
118 {
119         u32 fsr;
120         int ret;
121         ulong timeout_us = timeout_ms * 1000;
122
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 */
125
126         /*
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
129          */
130         ret = readl_poll_sleep_timeout(MU0_B_BASE_ADDR + 0x104, fsr, fsr & BIT(0), 10, timeout_us);
131         if (!ret)
132                 clrbits_le32(MU0_B_BASE_ADDR + 0x100, BIT(0));
133
134         return ret;
135 }
136
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)
151
152 static char *get_reset_cause(char *ret)
153 {
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);
157
158         if (!ret)
159                 return "null";
160
161         srs = readl(reg_srs);
162         cause1 = readl(reg_ssrs);
163
164         cause = srs & (CMC_SRS_POR | CMC_SRS_WUP | CMC_SRS_WARM);
165
166         switch (cause) {
167         case CMC_SRS_POR:
168                 sprintf(ret, "%s", "POR");
169                 break;
170         case CMC_SRS_WUP:
171                 sprintf(ret, "%s", "WUP");
172                 break;
173         case CMC_SRS_WARM:
174                 cause = srs & (CMC_SRS_WDG | CMC_SRS_SW |
175                         CMC_SRS_JTAG_RST);
176                 switch (cause) {
177                 case CMC_SRS_WDG:
178                         sprintf(ret, "%s", "WARM-WDG");
179                         break;
180                 case CMC_SRS_SW:
181                         sprintf(ret, "%s", "WARM-SW");
182                         break;
183                 case CMC_SRS_JTAG_RST:
184                         sprintf(ret, "%s", "WARM-JTAG");
185                         break;
186                 default:
187                         sprintf(ret, "%s", "WARM-UNKN");
188                         break;
189                 }
190                 break;
191         default:
192                 sprintf(ret, "%s-%X", "UNKN", srs);
193                 break;
194         }
195
196         debug("[%X] SRS[%X] %X - ", cause1, srs, srs ^ cause1);
197         return ret;
198 }
199
200 #if defined(CONFIG_DISPLAY_CPUINFO)
201 const char *get_imx_type(u32 imxtype)
202 {
203         return "8ULP";
204 }
205
206 int print_cpuinfo(void)
207 {
208         u32 cpurev;
209         char cause[18];
210
211         cpurev = get_cpu_rev();
212
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);
217
218 #if defined(CONFIG_IMX_PMC_TEMPERATURE)
219         struct udevice *udev;
220         int ret, temp;
221
222         ret = uclass_get_device(UCLASS_THERMAL, 0, &udev);
223         if (!ret) {
224                 ret = thermal_get_temp(udev, &temp);
225                 if (!ret)
226                         printf("CPU current temperature: %d\n", temp);
227                 else
228                         debug(" - failed to get CPU current temperature\n");
229         } else {
230                 debug(" - failed to get CPU current temperature\n");
231         }
232 #endif
233
234         printf("Reset cause: %s\n", get_reset_cause(cause));
235
236         printf("Boot mode: ");
237         switch (get_boot_mode()) {
238         case LOW_POWER_BOOT:
239                 printf("Low power boot\n");
240                 break;
241         case DUAL_BOOT:
242                 printf("Dual boot\n");
243                 break;
244         case SINGLE_BOOT:
245         default:
246                 printf("Single boot\n");
247                 break;
248         }
249
250         return 0;
251 }
252 #endif
253
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 */
258
259 static void disable_wdog(void __iomem *wdog_base)
260 {
261         u32 val_cs = readl(wdog_base + 0x00);
262
263         if (!(val_cs & 0x80))
264                 return;
265
266         dmb();
267         __raw_writel(REFRESH_WORD0, (wdog_base + 0x04)); /* Refresh the CNT */
268         __raw_writel(REFRESH_WORD1, (wdog_base + 0x04));
269         dmb();
270
271         if (!(val_cs & 800)) {
272                 dmb();
273                 __raw_writel(UNLOCK_WORD0, (wdog_base + 0x04));
274                 __raw_writel(UNLOCK_WORD1, (wdog_base + 0x04));
275                 dmb();
276
277                 while (!(readl(wdog_base + 0x00) & 0x800))
278                         ;
279         }
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 */
283
284         while (!(readl(wdog_base + 0x00) & 0x400))
285                 ;
286 }
287
288 void init_wdog(void)
289 {
290         disable_wdog((void __iomem *)WDG3_RBASE);
291 }
292
293 static struct mm_region imx8ulp_arm64_mem_map[] = {
294         {
295                 /* ROM */
296                 .virt = 0x0,
297                 .phys = 0x0,
298                 .size = 0x40000UL,
299                 .attrs = PTE_BLOCK_MEMTYPE(MT_NORMAL) |
300                          PTE_BLOCK_OUTER_SHARE
301         },
302         {
303                 /* FLEXSPI0 */
304                 .virt = 0x04000000,
305                 .phys = 0x04000000,
306                 .size = 0x08000000UL,
307                 .attrs = PTE_BLOCK_MEMTYPE(MT_DEVICE_NGNRNE) |
308                          PTE_BLOCK_NON_SHARE |
309                          PTE_BLOCK_PXN | PTE_BLOCK_UXN
310         },
311         {
312                 /* SSRAM (align with 2M) */
313                 .virt = 0x1FE00000UL,
314                 .phys = 0x1FE00000UL,
315                 .size = 0x400000UL,
316                 .attrs = PTE_BLOCK_MEMTYPE(MT_NORMAL) |
317                          PTE_BLOCK_OUTER_SHARE |
318                          PTE_BLOCK_PXN | PTE_BLOCK_UXN
319         }, {
320                 /* SRAM1 (align with 2M) */
321                 .virt = 0x21000000UL,
322                 .phys = 0x21000000UL,
323                 .size = 0x200000UL,
324                 .attrs = PTE_BLOCK_MEMTYPE(MT_NORMAL) |
325                          PTE_BLOCK_OUTER_SHARE |
326                          PTE_BLOCK_PXN | PTE_BLOCK_UXN
327         }, {
328                 /* SRAM0 (align with 2M) */
329                 .virt = 0x22000000UL,
330                 .phys = 0x22000000UL,
331                 .size = 0x200000UL,
332                 .attrs = PTE_BLOCK_MEMTYPE(MT_NORMAL) |
333                          PTE_BLOCK_OUTER_SHARE |
334                          PTE_BLOCK_PXN | PTE_BLOCK_UXN
335         }, {
336                 /* Peripherals */
337                 .virt = 0x27000000UL,
338                 .phys = 0x27000000UL,
339                 .size = 0x3000000UL,
340                 .attrs = PTE_BLOCK_MEMTYPE(MT_DEVICE_NGNRNE) |
341                          PTE_BLOCK_NON_SHARE |
342                          PTE_BLOCK_PXN | PTE_BLOCK_UXN
343         }, {
344                 /* Peripherals */
345                 .virt = 0x2D000000UL,
346                 .phys = 0x2D000000UL,
347                 .size = 0x1600000UL,
348                 .attrs = PTE_BLOCK_MEMTYPE(MT_DEVICE_NGNRNE) |
349                          PTE_BLOCK_NON_SHARE |
350                          PTE_BLOCK_PXN | PTE_BLOCK_UXN
351         }, {
352                 /* FLEXSPI1-2 */
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
359         }, {
360                 /* DRAM1 */
361                 .virt = 0x80000000UL,
362                 .phys = 0x80000000UL,
363                 .size = PHYS_SDRAM_SIZE,
364                 .attrs = PTE_BLOCK_MEMTYPE(MT_NORMAL) |
365                          PTE_BLOCK_OUTER_SHARE
366         }, {
367                 /*
368                  * empty entrie to split table entry 5
369                  * if needed when TEEs are used
370                  */
371                 0,
372         }, {
373                 /* List terminator */
374                 0,
375         }
376 };
377
378 struct mm_region *mem_map = imx8ulp_arm64_mem_map;
379
380 static unsigned int imx8ulp_find_dram_entry_in_mem_map(void)
381 {
382         int i;
383
384         for (i = 0; i < ARRAY_SIZE(imx8ulp_arm64_mem_map); i++)
385                 if (imx8ulp_arm64_mem_map[i].phys == CFG_SYS_SDRAM_BASE)
386                         return i;
387
388         hang(); /* Entry not found, this must never happen. */
389 }
390
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)
395 {
396         u64 one_pt = MAX_PTE_ENTRIES * sizeof(u64);
397         u64 size = 0;
398
399         /*
400          * For each memory region, the max table size:
401          * 2 level 3 tables + 2 level 2 tables + 1 level 1 table
402          */
403         size = (2 + 2 + 1) * one_pt * MAX_MEM_MAP_REGIONS + one_pt;
404
405         /*
406          * We need to duplicate our page table once to have an emergency pt to
407          * resort to when splitting page tables later on
408          */
409         size *= 2;
410
411         /*
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.
414          */
415         size += one_pt * 4;
416
417         return size;
418 }
419
420 void enable_caches(void)
421 {
422         /* If OPTEE runs, remove OPTEE memory from MMU table to avoid speculative prefetch */
423         if (rom_pointer[1]) {
424                 /*
425                  * TEE are loaded, So the ddr bank structures
426                  * have been modified update mmu table accordingly
427                  */
428                 int i = 0;
429                 int entry = imx8ulp_find_dram_entry_in_mem_map();
430                 u64 attrs = imx8ulp_arm64_mem_map[entry].attrs;
431
432                 while (i < CONFIG_NR_DRAM_BANKS &&
433                        entry < ARRAY_SIZE(imx8ulp_arm64_mem_map)) {
434                         if (gd->bd->bi_dram[i].start == 0)
435                                 break;
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);
442                         i++; entry++;
443                 }
444         }
445
446         icache_enable();
447         dcache_enable();
448 }
449
450 __weak int board_phys_sdram_size(phys_size_t *size)
451 {
452         if (!size)
453                 return -EINVAL;
454
455         *size = PHYS_SDRAM_SIZE;
456         return 0;
457 }
458
459 int dram_init(void)
460 {
461         unsigned int entry = imx8ulp_find_dram_entry_in_mem_map();
462         phys_size_t sdram_size;
463         int ret;
464
465         ret = board_phys_sdram_size(&sdram_size);
466         if (ret)
467                 return ret;
468
469         /* rom_pointer[1] contains the size of TEE occupies */
470         if (rom_pointer[1])
471                 gd->ram_size = sdram_size - rom_pointer[1];
472         else
473                 gd->ram_size = sdram_size;
474
475         /* also update the SDRAM size in the mem_map used externally */
476         imx8ulp_arm64_mem_map[entry].size = sdram_size;
477         return 0;
478 }
479
480 int dram_init_banksize(void)
481 {
482         int bank = 0;
483         int ret;
484         phys_size_t sdram_size;
485
486         ret = board_phys_sdram_size(&sdram_size);
487         if (ret)
488                 return ret;
489
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];
494
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");
499                                 return -1;
500                         }
501
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;
505                 }
506         } else {
507                 gd->bd->bi_dram[bank].size = sdram_size;
508         }
509
510         return 0;
511 }
512
513 phys_size_t get_effective_memsize(void)
514 {
515         /* return the first bank as effective memory */
516         if (rom_pointer[1])
517                 return ((phys_addr_t)rom_pointer[0] - PHYS_SDRAM);
518
519         return gd->ram_size;
520 }
521
522 #ifdef CONFIG_ENV_VARS_UBOOT_RUNTIME_CONFIG
523 void get_board_serial(struct tag_serialnr *serialnr)
524 {
525         u32 uid[4];
526         u32 res;
527         int ret;
528
529         ret = ahab_read_common_fuse(1, uid, 4, &res);
530         if (ret)
531                 printf("ahab read fuse failed %d, 0x%x\n", ret, res);
532         else
533                 printf("UID 0x%x,0x%x,0x%x,0x%x\n", uid[0], uid[1], uid[2], uid[3]);
534
535         serialnr->low = uid[0];
536         serialnr->high = uid[3];
537 }
538 #endif
539
540 static void set_core0_reset_vector(u32 entry)
541 {
542         /* Update SIM1 DGO8 for reset vector base */
543         writel(entry, SIM1_BASE_ADDR + 0x5c);
544
545         /* set update bit */
546         setbits_le32(SIM1_BASE_ADDR + 0x8, 0x1 << 24);
547
548         /* polling the ack */
549         while ((readl(SIM1_BASE_ADDR + 0x8) & (0x1 << 26)) == 0)
550                 ;
551
552         /* clear the update */
553         clrbits_le32(SIM1_BASE_ADDR + 0x8, (0x1 << 24));
554
555         /* clear the ack by set 1 */
556         setbits_le32(SIM1_BASE_ADDR + 0x8, (0x1 << 26));
557 }
558
559 /* Not used now */
560 int trdc_set_access(void)
561 {
562         /*
563          * TRDC mgr + 4 MBC + 2 MRC.
564          */
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);
571
572         /* 0x1fff8000 used for resource table by remoteproc */
573         trdc_mbc_set_access(0, 7, 2, 31, false);
574
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);
578
579         /* PCC1 */
580         trdc_mbc_set_access(2, 7, 1, 17, false);
581         trdc_mbc_set_access(2, 7, 1, 34, false);
582
583         /* Iomuxc0: : PBridge1 slot 33 */
584         trdc_mbc_set_access(2, 7, 1, 33, false);
585
586         /* flexspi0 */
587         trdc_mbc_set_access(2, 7, 0, 57, false);
588         trdc_mrc_region_set_access(0, 7, 0x04000000, 0x0c000000, false);
589
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);
594
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);
602
603         /* Set SAI0 for eDMA 0, NS */
604         trdc_mbc_set_access(2, 0, 1, 28, false);
605
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);
615
616         writel(0x800000a0, 0x28031840);
617
618         return 0;
619 }
620
621 void lpav_configure(bool lpav_to_m33)
622 {
623         if (!lpav_to_m33)
624                 setbits_le32(SIM_SEC_BASE_ADDR + 0x44, BIT(7)); /* LPAV to APD */
625
626         /* PXP/GPU 2D/3D/DCNANO/MIPI_DSI/EPDC/HIFI4 to APD */
627         setbits_le32(SIM_SEC_BASE_ADDR + 0x4c, 0x7F);
628
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);
633 }
634
635 void 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
655 void set_lpav_qos(void)
656 {
657         /* Set read QoS of dcnano on LPAV NIC */
658         writel(0xf, 0x2e447100);
659 }
660
661 int arch_cpu_init(void)
662 {
663         if (IS_ENABLED(CONFIG_SPL_BUILD)) {
664                 u32 val = 0;
665                 int ret;
666                 bool rdc_en = true; /* Default assume DBD_EN is set */
667
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));
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
681                 /* Disable wdog */
682                 init_wdog();
683
684                 /* Read DBD_EN fuse */
685                 ret = fuse_read(8, 1, &val);
686                 if (!ret)
687                         rdc_en = !!(val & 0x4000);
688
689                 if (get_boot_mode() == SINGLE_BOOT)
690                         lpav_configure(false);
691                 else
692                         lpav_configure(true);
693
694                 /* Release xrdc, then allow A35 to write SRAM2 */
695                 if (rdc_en)
696                         release_rdc(RDC_XRDC);
697
698                 xrdc_mrc_region_set_access(2, CONFIG_SPL_TEXT_BASE, 0xE00);
699
700                 clock_init_early();
701         } else {
702                 /* reconfigure core0 reset vector to ROM */
703                 set_core0_reset_vector(0x1000);
704         }
705
706         return 0;
707 }
708
709 int imx8ulp_dm_post_init(void)
710 {
711         struct udevice *devp;
712         int ret;
713         u32 res;
714         struct sentinel_get_info_data *info = (struct sentinel_get_info_data *)SRAM0_BASE;
715
716         ret = uclass_get_device_by_driver(UCLASS_MISC, DM_DRIVER_GET(imx8ulp_mu), &devp);
717         if (ret) {
718                 printf("could not get S400 mu %d\n", ret);
719                 return ret;
720         }
721
722         ret = ahab_get_info(info, &res);
723         if (ret) {
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;
728         }
729
730         set_cpu_info(info);
731
732         return 0;
733 }
734
735 static int imx8ulp_evt_dm_post_init(void *ctx, struct event *event)
736 {
737         return imx8ulp_dm_post_init();
738 }
739 EVENT_SPY(EVT_DM_POST_INIT, imx8ulp_evt_dm_post_init);
740
741 #if defined(CONFIG_SPL_BUILD)
742 __weak void __noreturn jump_to_image_no_args(struct spl_image_info *spl_image)
743 {
744         debug("image entry point: 0x%lx\n", spl_image->entry_point);
745
746         set_core0_reset_vector((u32)spl_image->entry_point);
747
748         /* Enable the 512KB cache */
749         setbits_le32(SIM1_BASE_ADDR + 0x30, (0x1 << 4));
750
751         /* reset core */
752         setbits_le32(SIM1_BASE_ADDR + 0x30, (0x1 << 16));
753
754         while (1)
755                 ;
756 }
757 #endif
758
759 void imx_get_mac_from_fuse(int dev_id, unsigned char *mac)
760 {
761         u32 val[2] = {};
762         int ret;
763
764         ret = fuse_read(5, 3, &val[0]);
765         if (ret)
766                 goto err;
767
768         ret = fuse_read(5, 4, &val[1]);
769         if (ret)
770                 goto err;
771
772         mac[0] = val[0];
773         mac[1] = val[0] >> 8;
774         mac[2] = val[0] >> 16;
775         mac[3] = val[0] >> 24;
776         mac[4] = val[1];
777         mac[5] = val[1] >> 8;
778
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]);
781         return;
782 err:
783         memset(mac, 0, 6);
784         printf("%s: fuse read err: %d\n", __func__, ret);
785 }
786
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)
789 {
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())
793                 image_offset = 0;
794
795         return image_offset;
796 }
797
798 enum env_location env_get_location(enum env_operation op, int prio)
799 {
800         enum boot_device dev = get_boot_device();
801         enum env_location env_loc = ENVL_UNKNOWN;
802
803         if (prio)
804                 return env_loc;
805
806         switch (dev) {
807 #ifdef CONFIG_ENV_IS_IN_SPI_FLASH
808         case QSPI_BOOT:
809                 env_loc = ENVL_SPI_FLASH;
810                 break;
811 #endif
812 #ifdef CONFIG_ENV_IS_IN_MMC
813         case SD1_BOOT:
814         case SD2_BOOT:
815         case SD3_BOOT:
816         case MMC1_BOOT:
817         case MMC2_BOOT:
818         case MMC3_BOOT:
819                 env_loc =  ENVL_MMC;
820                 break;
821 #endif
822         default:
823 #if defined(CONFIG_ENV_IS_NOWHERE)
824                 env_loc = ENVL_NOWHERE;
825 #endif
826                 break;
827         }
828
829         return env_loc;
830 }