]> Git Repo - J-u-boot.git/blob - arch/arm/mach-imx/imx8ulp/soc.c
f64a8fb9fc5ed76452a98262d239e6f8fc6b25fa
[J-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 <spl.h>
15 #include <asm/arch/rdc.h>
16 #include <asm/arch/s400_api.h>
17 #include <asm/arch/mu_hal.h>
18 #include <cpu_func.h>
19 #include <asm/setup.h>
20 #include <dm.h>
21 #include <dm/device-internal.h>
22 #include <dm/lists.h>
23 #include <dm/uclass.h>
24 #include <dm/device.h>
25 #include <dm/uclass-internal.h>
26 #include <fuse.h>
27
28 DECLARE_GLOBAL_DATA_PTR;
29
30 struct rom_api *g_rom_api = (struct rom_api *)0x1980;
31
32 enum boot_device get_boot_device(void)
33 {
34         volatile gd_t *pgd = gd;
35         int ret;
36         u32 boot;
37         u16 boot_type;
38         u8 boot_instance;
39         enum boot_device boot_dev = SD1_BOOT;
40
41         ret = g_rom_api->query_boot_infor(QUERY_BT_DEV, &boot,
42                                           ((uintptr_t)&boot) ^ QUERY_BT_DEV);
43         set_gd(pgd);
44
45         if (ret != ROM_API_OKAY) {
46                 puts("ROMAPI: failure at query_boot_info\n");
47                 return -1;
48         }
49
50         boot_type = boot >> 16;
51         boot_instance = (boot >> 8) & 0xff;
52
53         switch (boot_type) {
54         case BT_DEV_TYPE_SD:
55                 boot_dev = boot_instance + SD1_BOOT;
56                 break;
57         case BT_DEV_TYPE_MMC:
58                 boot_dev = boot_instance + MMC1_BOOT;
59                 break;
60         case BT_DEV_TYPE_NAND:
61                 boot_dev = NAND_BOOT;
62                 break;
63         case BT_DEV_TYPE_FLEXSPINOR:
64                 boot_dev = QSPI_BOOT;
65                 break;
66         case BT_DEV_TYPE_USB:
67                 boot_dev = USB_BOOT;
68                 break;
69         default:
70                 break;
71         }
72
73         return boot_dev;
74 }
75
76 bool is_usb_boot(void)
77 {
78         return get_boot_device() == USB_BOOT;
79 }
80
81 #ifdef CONFIG_ENV_IS_IN_MMC
82 __weak int board_mmc_get_env_dev(int devno)
83 {
84         return devno;
85 }
86
87 int mmc_get_env_dev(void)
88 {
89         volatile gd_t *pgd = gd;
90         int ret;
91         u32 boot;
92         u16 boot_type;
93         u8 boot_instance;
94
95         ret = g_rom_api->query_boot_infor(QUERY_BT_DEV, &boot,
96                                           ((uintptr_t)&boot) ^ QUERY_BT_DEV);
97         set_gd(pgd);
98
99         if (ret != ROM_API_OKAY) {
100                 puts("ROMAPI: failure at query_boot_info\n");
101                 return CONFIG_SYS_MMC_ENV_DEV;
102         }
103
104         boot_type = boot >> 16;
105         boot_instance = (boot >> 8) & 0xff;
106
107         /* If not boot from sd/mmc, use default value */
108         if (boot_type != BOOT_TYPE_SD && boot_type != BOOT_TYPE_MMC)
109                 return env_get_ulong("mmcdev", 10, CONFIG_SYS_MMC_ENV_DEV);
110
111         return board_mmc_get_env_dev(boot_instance);
112 }
113 #endif
114
115 u32 get_cpu_rev(void)
116 {
117         return (MXC_CPU_IMX8ULP << 12) | CHIP_REV_1_0;
118 }
119
120 enum bt_mode get_boot_mode(void)
121 {
122         u32 bt0_cfg = 0;
123
124         bt0_cfg = readl(SIM_SEC_BASE_ADDR + 0x24);
125         bt0_cfg &= (BT0CFG_LPBOOT_MASK | BT0CFG_DUALBOOT_MASK);
126
127         if (!(bt0_cfg & BT0CFG_LPBOOT_MASK)) {
128                 /* No low power boot */
129                 if (bt0_cfg & BT0CFG_DUALBOOT_MASK)
130                         return DUAL_BOOT;
131                 else
132                         return SINGLE_BOOT;
133         }
134
135         return LOW_POWER_BOOT;
136 }
137
138 #define CMC_SRS_TAMPER                    BIT(31)
139 #define CMC_SRS_SECURITY                  BIT(30)
140 #define CMC_SRS_TZWDG                     BIT(29)
141 #define CMC_SRS_JTAG_RST                  BIT(28)
142 #define CMC_SRS_CORE1                     BIT(16)
143 #define CMC_SRS_LOCKUP                    BIT(15)
144 #define CMC_SRS_SW                        BIT(14)
145 #define CMC_SRS_WDG                       BIT(13)
146 #define CMC_SRS_PIN_RESET                 BIT(8)
147 #define CMC_SRS_WARM                      BIT(4)
148 #define CMC_SRS_HVD                       BIT(3)
149 #define CMC_SRS_LVD                       BIT(2)
150 #define CMC_SRS_POR                       BIT(1)
151 #define CMC_SRS_WUP                       BIT(0)
152
153 static char *get_reset_cause(char *ret)
154 {
155         u32 cause1, cause = 0, srs = 0;
156         void __iomem *reg_ssrs = (void __iomem *)(CMC1_BASE_ADDR + 0x88);
157         void __iomem *reg_srs = (void __iomem *)(CMC1_BASE_ADDR + 0x80);
158
159         if (!ret)
160                 return "null";
161
162         srs = readl(reg_srs);
163         cause1 = readl(reg_ssrs);
164
165         cause = srs & (CMC_SRS_POR | CMC_SRS_WUP | CMC_SRS_WARM);
166
167         switch (cause) {
168         case CMC_SRS_POR:
169                 sprintf(ret, "%s", "POR");
170                 break;
171         case CMC_SRS_WUP:
172                 sprintf(ret, "%s", "WUP");
173                 break;
174         case CMC_SRS_WARM:
175                 cause = srs & (CMC_SRS_WDG | CMC_SRS_SW |
176                         CMC_SRS_JTAG_RST);
177                 switch (cause) {
178                 case CMC_SRS_WDG:
179                         sprintf(ret, "%s", "WARM-WDG");
180                         break;
181                 case CMC_SRS_SW:
182                         sprintf(ret, "%s", "WARM-SW");
183                         break;
184                 case CMC_SRS_JTAG_RST:
185                         sprintf(ret, "%s", "WARM-JTAG");
186                         break;
187                 default:
188                         sprintf(ret, "%s", "WARM-UNKN");
189                         break;
190                 }
191                 break;
192         default:
193                 sprintf(ret, "%s-%X", "UNKN", srs);
194                 break;
195         }
196
197         debug("[%X] SRS[%X] %X - ", cause1, srs, srs ^ cause1);
198         return ret;
199 }
200
201 #if defined(CONFIG_DISPLAY_CPUINFO)
202 const char *get_imx_type(u32 imxtype)
203 {
204         return "8ULP";
205 }
206
207 int print_cpuinfo(void)
208 {
209         u32 cpurev;
210         char cause[18];
211
212         cpurev = get_cpu_rev();
213
214         printf("CPU:   Freescale i.MX%s rev%d.%d at %d MHz\n",
215                get_imx_type((cpurev & 0xFF000) >> 12),
216                (cpurev & 0x000F0) >> 4, (cpurev & 0x0000F) >> 0,
217                mxc_get_clock(MXC_ARM_CLK) / 1000000);
218
219         printf("Reset cause: %s\n", get_reset_cause(cause));
220
221         printf("Boot mode: ");
222         switch (get_boot_mode()) {
223         case LOW_POWER_BOOT:
224                 printf("Low power boot\n");
225                 break;
226         case DUAL_BOOT:
227                 printf("Dual boot\n");
228                 break;
229         case SINGLE_BOOT:
230         default:
231                 printf("Single boot\n");
232                 break;
233         }
234
235         return 0;
236 }
237 #endif
238
239 #define UNLOCK_WORD0 0xC520 /* 1st unlock word */
240 #define UNLOCK_WORD1 0xD928 /* 2nd unlock word */
241 #define REFRESH_WORD0 0xA602 /* 1st refresh word */
242 #define REFRESH_WORD1 0xB480 /* 2nd refresh word */
243
244 static void disable_wdog(void __iomem *wdog_base)
245 {
246         u32 val_cs = readl(wdog_base + 0x00);
247
248         if (!(val_cs & 0x80))
249                 return;
250
251         dmb();
252         __raw_writel(REFRESH_WORD0, (wdog_base + 0x04)); /* Refresh the CNT */
253         __raw_writel(REFRESH_WORD1, (wdog_base + 0x04));
254         dmb();
255
256         if (!(val_cs & 800)) {
257                 dmb();
258                 __raw_writel(UNLOCK_WORD0, (wdog_base + 0x04));
259                 __raw_writel(UNLOCK_WORD1, (wdog_base + 0x04));
260                 dmb();
261
262                 while (!(readl(wdog_base + 0x00) & 0x800))
263                         ;
264         }
265         writel(0x0, (wdog_base + 0x0C)); /* Set WIN to 0 */
266         writel(0x400, (wdog_base + 0x08)); /* Set timeout to default 0x400 */
267         writel(0x120, (wdog_base + 0x00)); /* Disable it and set update */
268
269         while (!(readl(wdog_base + 0x00) & 0x400))
270                 ;
271 }
272
273 void init_wdog(void)
274 {
275         disable_wdog((void __iomem *)WDG3_RBASE);
276 }
277
278 static struct mm_region imx8ulp_arm64_mem_map[] = {
279         {
280                 /* ROM */
281                 .virt = 0x0,
282                 .phys = 0x0,
283                 .size = 0x40000UL,
284                 .attrs = PTE_BLOCK_MEMTYPE(MT_NORMAL) |
285                          PTE_BLOCK_OUTER_SHARE
286         },
287         {
288                 /* FLEXSPI0 */
289                 .virt = 0x04000000,
290                 .phys = 0x04000000,
291                 .size = 0x08000000UL,
292                 .attrs = PTE_BLOCK_MEMTYPE(MT_DEVICE_NGNRNE) |
293                          PTE_BLOCK_NON_SHARE |
294                          PTE_BLOCK_PXN | PTE_BLOCK_UXN
295         },
296         {
297                 /* SSRAM (align with 2M) */
298                 .virt = 0x1FE00000UL,
299                 .phys = 0x1FE00000UL,
300                 .size = 0x400000UL,
301                 .attrs = PTE_BLOCK_MEMTYPE(MT_NORMAL) |
302                          PTE_BLOCK_OUTER_SHARE |
303                          PTE_BLOCK_PXN | PTE_BLOCK_UXN
304         }, {
305                 /* SRAM1 (align with 2M) */
306                 .virt = 0x21000000UL,
307                 .phys = 0x21000000UL,
308                 .size = 0x200000UL,
309                 .attrs = PTE_BLOCK_MEMTYPE(MT_NORMAL) |
310                          PTE_BLOCK_OUTER_SHARE |
311                          PTE_BLOCK_PXN | PTE_BLOCK_UXN
312         }, {
313                 /* SRAM0 (align with 2M) */
314                 .virt = 0x22000000UL,
315                 .phys = 0x22000000UL,
316                 .size = 0x200000UL,
317                 .attrs = PTE_BLOCK_MEMTYPE(MT_NORMAL) |
318                          PTE_BLOCK_OUTER_SHARE |
319                          PTE_BLOCK_PXN | PTE_BLOCK_UXN
320         }, {
321                 /* Peripherals */
322                 .virt = 0x27000000UL,
323                 .phys = 0x27000000UL,
324                 .size = 0x3000000UL,
325                 .attrs = PTE_BLOCK_MEMTYPE(MT_DEVICE_NGNRNE) |
326                          PTE_BLOCK_NON_SHARE |
327                          PTE_BLOCK_PXN | PTE_BLOCK_UXN
328         }, {
329                 /* Peripherals */
330                 .virt = 0x2D000000UL,
331                 .phys = 0x2D000000UL,
332                 .size = 0x1600000UL,
333                 .attrs = PTE_BLOCK_MEMTYPE(MT_DEVICE_NGNRNE) |
334                          PTE_BLOCK_NON_SHARE |
335                          PTE_BLOCK_PXN | PTE_BLOCK_UXN
336         }, {
337                 /* FLEXSPI1-2 */
338                 .virt = 0x40000000UL,
339                 .phys = 0x40000000UL,
340                 .size = 0x40000000UL,
341                 .attrs = PTE_BLOCK_MEMTYPE(MT_DEVICE_NGNRNE) |
342                          PTE_BLOCK_NON_SHARE |
343                          PTE_BLOCK_PXN | PTE_BLOCK_UXN
344         }, {
345                 /* DRAM1 */
346                 .virt = 0x80000000UL,
347                 .phys = 0x80000000UL,
348                 .size = PHYS_SDRAM_SIZE,
349                 .attrs = PTE_BLOCK_MEMTYPE(MT_NORMAL) |
350                          PTE_BLOCK_OUTER_SHARE
351         }, {
352                 /*
353                  * empty entrie to split table entry 5
354                  * if needed when TEEs are used
355                  */
356                 0,
357         }, {
358                 /* List terminator */
359                 0,
360         }
361 };
362
363 struct mm_region *mem_map = imx8ulp_arm64_mem_map;
364
365 /* simplify the page table size to enhance boot speed */
366 #define MAX_PTE_ENTRIES         512
367 #define MAX_MEM_MAP_REGIONS     16
368 u64 get_page_table_size(void)
369 {
370         u64 one_pt = MAX_PTE_ENTRIES * sizeof(u64);
371         u64 size = 0;
372
373         /*
374          * For each memory region, the max table size:
375          * 2 level 3 tables + 2 level 2 tables + 1 level 1 table
376          */
377         size = (2 + 2 + 1) * one_pt * MAX_MEM_MAP_REGIONS + one_pt;
378
379         /*
380          * We need to duplicate our page table once to have an emergency pt to
381          * resort to when splitting page tables later on
382          */
383         size *= 2;
384
385         /*
386          * We may need to split page tables later on if dcache settings change,
387          * so reserve up to 4 (random pick) page tables for that.
388          */
389         size += one_pt * 4;
390
391         return size;
392 }
393
394 void enable_caches(void)
395 {
396         /* TODO: add TEE memmap region */
397
398         icache_enable();
399         dcache_enable();
400 }
401
402 int dram_init(void)
403 {
404         gd->ram_size = PHYS_SDRAM_SIZE;
405
406         return 0;
407 }
408
409 #ifdef CONFIG_ENV_VARS_UBOOT_RUNTIME_CONFIG
410 void get_board_serial(struct tag_serialnr *serialnr)
411 {
412         u32 uid[4];
413         u32 res;
414         int ret;
415
416         ret = ahab_read_common_fuse(1, uid, 4, &res);
417         if (ret)
418                 printf("ahab read fuse failed %d, 0x%x\n", ret, res);
419         else
420                 printf("UID 0x%x,0x%x,0x%x,0x%x\n", uid[0], uid[1], uid[2], uid[3]);
421
422         serialnr->low = uid[0];
423         serialnr->high = uid[3];
424 }
425 #endif
426
427 static void set_core0_reset_vector(u32 entry)
428 {
429         /* Update SIM1 DGO8 for reset vector base */
430         writel(entry, SIM1_BASE_ADDR + 0x5c);
431
432         /* set update bit */
433         setbits_le32(SIM1_BASE_ADDR + 0x8, 0x1 << 24);
434
435         /* polling the ack */
436         while ((readl(SIM1_BASE_ADDR + 0x8) & (0x1 << 26)) == 0)
437                 ;
438
439         /* clear the update */
440         clrbits_le32(SIM1_BASE_ADDR + 0x8, (0x1 << 24));
441
442         /* clear the ack by set 1 */
443         setbits_le32(SIM1_BASE_ADDR + 0x8, (0x1 << 26));
444 }
445
446 static int trdc_set_access(void)
447 {
448         /*
449          * TRDC mgr + 4 MBC + 2 MRC.
450          * S400 should already configure when release RDC
451          * A35 only map non-secure region for pbridge0 and 1, set sec_access to false
452          */
453         trdc_mbc_set_access(2, 7, 0, 49, false);
454         trdc_mbc_set_access(2, 7, 0, 50, false);
455         trdc_mbc_set_access(2, 7, 0, 51, false);
456         trdc_mbc_set_access(2, 7, 0, 52, false);
457         trdc_mbc_set_access(2, 7, 0, 53, false);
458         trdc_mbc_set_access(2, 7, 0, 54, false);
459
460         /* CGC0: PBridge0 slot 47 */
461         trdc_mbc_set_access(2, 7, 0, 47, false);
462
463         /* Iomuxc0: : PBridge1 slot 33 */
464         trdc_mbc_set_access(2, 7, 1, 33, false);
465
466         /* flexspi0 */
467         trdc_mrc_region_set_access(0, 7, 0x04000000, 0x0c000000, false);
468         return 0;
469 }
470
471 void lpav_configure(void)
472 {
473         /* LPAV to APD */
474         setbits_le32(SIM_SEC_BASE_ADDR + 0x44, BIT(7));
475
476         /* PXP/GPU 2D/3D/DCNANO/MIPI_DSI/EPDC/HIFI4 to APD */
477         setbits_le32(SIM_SEC_BASE_ADDR + 0x4c, 0x7F);
478
479         /* LPAV slave/dma2 ch allocation and request allocation to APD */
480         writel(0x1f, SIM_SEC_BASE_ADDR + 0x50);
481         writel(0xffffffff, SIM_SEC_BASE_ADDR + 0x54);
482         writel(0x003fffff, SIM_SEC_BASE_ADDR + 0x58);
483 }
484
485 int arch_cpu_init(void)
486 {
487         if (IS_ENABLED(CONFIG_SPL_BUILD)) {
488                 u32 val = 0;
489                 int ret;
490                 bool rdc_en = true; /* Default assume DBD_EN is set */
491
492                 /* Disable wdog */
493                 init_wdog();
494
495                 /* Read DBD_EN fuse */
496                 ret = fuse_read(8, 1, &val);
497                 if (!ret)
498                         rdc_en = !!(val & 0x4000);
499
500                 if (get_boot_mode() == SINGLE_BOOT) {
501                         if (rdc_en)
502                                 release_rdc(RDC_TRDC);
503
504                         trdc_set_access();
505
506                         lpav_configure();
507                 }
508
509                 /* Release xrdc, then allow A35 to write SRAM2 */
510                 if (rdc_en)
511                         release_rdc(RDC_XRDC);
512
513                 xrdc_mrc_region_set_access(2, CONFIG_SPL_TEXT_BASE, 0xE00);
514
515                 clock_init();
516         } else {
517                 /* reconfigure core0 reset vector to ROM */
518                 set_core0_reset_vector(0x1000);
519         }
520
521         return 0;
522 }
523
524 int arch_cpu_init_dm(void)
525 {
526         struct udevice *devp;
527         int node, ret;
528
529         node = fdt_node_offset_by_compatible(gd->fdt_blob, -1, "fsl,imx8ulp-mu");
530
531         ret = uclass_get_device_by_of_offset(UCLASS_MISC, node, &devp);
532         if (ret) {
533                 printf("could not get S400 mu %d\n", ret);
534                 return ret;
535         }
536
537         return 0;
538 }
539
540 #if defined(CONFIG_SPL_BUILD)
541 __weak void __noreturn jump_to_image_no_args(struct spl_image_info *spl_image)
542 {
543         debug("image entry point: 0x%lx\n", spl_image->entry_point);
544
545         set_core0_reset_vector((u32)spl_image->entry_point);
546
547         /* Enable the 512KB cache */
548         setbits_le32(SIM1_BASE_ADDR + 0x30, (0x1 << 4));
549
550         /* reset core */
551         setbits_le32(SIM1_BASE_ADDR + 0x30, (0x1 << 16));
552
553         while (1)
554                 ;
555 }
556 #endif
557
558 void imx_get_mac_from_fuse(int dev_id, unsigned char *mac)
559 {
560         memset(mac, 0, 6);
561 }
562
563 int (*card_emmc_is_boot_part_en)(void) = (void *)0x67cc;
564 u32 spl_arch_boot_image_offset(u32 image_offset, u32 rom_bt_dev)
565 {
566         /* Hard code for eMMC image_offset on 8ULP ROM, need fix by ROM, temp workaround */
567         if (((rom_bt_dev >> 16) & 0xff) == BT_DEV_TYPE_MMC && card_emmc_is_boot_part_en())
568                 image_offset = 0;
569
570         return image_offset;
571 }
This page took 0.050325 seconds and 2 git commands to generate.