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