blob: 9b12d3d1ad2946dbd92db607155a9e87a5d5da61 [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 Lic408ed32022-07-26 16:40:49 +080017#include <asm/mach-imx/s400_api.h>
18#include <asm/mach-imx/mu_hal.h>
Ye Li853cc9d2021-08-07 16:00:55 +080019#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 +080037bool 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
48int mmc_get_env_dev(void)
49{
Ye Li88044c72021-08-07 16:01:01 +080050 int ret;
51 u32 boot;
52 u16 boot_type;
53 u8 boot_instance;
54
Rasmus Villemoesb06f5a42022-06-20 10:53:22 +020055 ret = rom_api_query_boot_infor(QUERY_BT_DEV, &boot);
Ye Li88044c72021-08-07 16:01:01 +080056
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
Ye Lid5ffe552023-01-31 16:42:13 +080073static 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
Peng Fan5c2218a2021-08-07 16:00:31 +080080u32 get_cpu_rev(void)
81{
Ye Lid5ffe552023-01-31 16:42:13 +080082 u32 rev = (gd->arch.soc_rev >> 24) - 0xa0;
83
84 return (MXC_CPU_IMX8ULP << 12) | (CHIP_REV_1_0 + rev);
Peng Fan5c2218a2021-08-07 16:00:31 +080085}
Peng Fan72530162021-08-07 16:00:33 +080086
87enum bt_mode get_boot_mode(void)
88{
89 u32 bt0_cfg = 0;
90
Ye Li0e358052021-08-07 16:01:07 +080091 bt0_cfg = readl(SIM_SEC_BASE_ADDR + 0x24);
Peng Fan72530162021-08-07 16:00:33 +080092 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
Ye Li2e9f15c2022-04-06 14:30:08 +0800105bool 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
117int m33_image_handshake(ulong timeout_ms)
118{
119 u32 fsr;
120 int ret;
121 ulong timeout_us = timeout_ms * 1000;
122
Ye Li2e9f15c2022-04-06 14:30:08 +0800123 /* 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
Peng Fanaf4f3b32021-08-07 16:00:34 +0800137#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
Peng Fanaf4f3b32021-08-07 16:00:34 +0800152static char *get_reset_cause(char *ret)
153{
154 u32 cause1, cause = 0, srs = 0;
Peng Fanb15705a2021-08-07 16:00:35 +0800155 void __iomem *reg_ssrs = (void __iomem *)(CMC1_BASE_ADDR + 0x88);
156 void __iomem *reg_srs = (void __iomem *)(CMC1_BASE_ADDR + 0x80);
Peng Fanaf4f3b32021-08-07 16:00:34 +0800157
158 if (!ret)
159 return "null";
160
161 srs = readl(reg_srs);
162 cause1 = readl(reg_ssrs);
163
Peng Fan0d720e22021-08-07 16:01:06 +0800164 cause = srs & (CMC_SRS_POR | CMC_SRS_WUP | CMC_SRS_WARM);
Peng Fanaf4f3b32021-08-07 16:00:34 +0800165
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:
Peng Fan0d720e22021-08-07 16:01:06 +0800174 cause = srs & (CMC_SRS_WDG | CMC_SRS_SW |
Peng Fanaf4f3b32021-08-07 16:00:34 +0800175 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:
Peng Fan0d720e22021-08-07 16:01:06 +0800192 sprintf(ret, "%s-%X", "UNKN", srs);
Peng Fanaf4f3b32021-08-07 16:00:34 +0800193 break;
194 }
195
196 debug("[%X] SRS[%X] %X - ", cause1, srs, srs ^ cause1);
197 return ret;
198}
199
Peng Fan72530162021-08-07 16:00:33 +0800200#if defined(CONFIG_DISPLAY_CPUINFO)
201const char *get_imx_type(u32 imxtype)
202{
203 return "8ULP";
204}
205
206int print_cpuinfo(void)
207{
208 u32 cpurev;
209 char cause[18];
210
211 cpurev = get_cpu_rev();
212
Ye Lif012ceb2021-10-29 09:46:24 +0800213 printf("CPU: i.MX%s rev%d.%d at %d MHz\n",
Peng Fan72530162021-08-07 16:00:33 +0800214 get_imx_type((cpurev & 0xFF000) >> 12),
215 (cpurev & 0x000F0) >> 4, (cpurev & 0x0000F) >> 0,
216 mxc_get_clock(MXC_ARM_CLK) / 1000000);
217
Alice Guof2c4a392021-10-29 09:46:32 +0800218#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
Peng Fanaf4f3b32021-08-07 16:00:34 +0800234 printf("Reset cause: %s\n", get_reset_cause(cause));
235
Peng Fan72530162021-08-07 16:00:33 +0800236 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
Peng Fanb15705a2021-08-07 16:00:35 +0800253
Peng Fanc84bc102021-08-07 16:00:49 +0800254#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
259static 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
Peng Fanb15705a2021-08-07 16:00:35 +0800288void init_wdog(void)
289{
Peng Fanc84bc102021-08-07 16:00:49 +0800290 disable_wdog((void __iomem *)WDG3_RBASE);
Peng Fanb15705a2021-08-07 16:00:35 +0800291}
292
293static 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
378struct mm_region *mem_map = imx8ulp_arm64_mem_map;
379
Ji Luo2fd258c2022-04-06 14:30:28 +0800380static 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++)
Tom Rinibb4dd962022-11-16 13:10:37 -0500385 if (imx8ulp_arm64_mem_map[i].phys == CFG_SYS_SDRAM_BASE)
Ji Luo2fd258c2022-04-06 14:30:28 +0800386 return i;
387
388 hang(); /* Entry not found, this must never happen. */
389}
390
Peng Fanb15705a2021-08-07 16:00:35 +0800391/* simplify the page table size to enhance boot speed */
392#define MAX_PTE_ENTRIES 512
393#define MAX_MEM_MAP_REGIONS 16
394u64 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
420void enable_caches(void)
421{
Ji Luo2fd258c2022-04-06 14:30:28 +0800422 /* 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 }
Peng Fanb15705a2021-08-07 16:00:35 +0800445
446 icache_enable();
447 dcache_enable();
448}
449
Ji Luo2fd258c2022-04-06 14:30:28 +0800450__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
Peng Fanb15705a2021-08-07 16:00:35 +0800459int dram_init(void)
460{
Ji Luo2fd258c2022-04-06 14:30:28 +0800461 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
480int 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 }
Peng Fanb15705a2021-08-07 16:00:35 +0800509
510 return 0;
511}
512
Ji Luo2fd258c2022-04-06 14:30:28 +0800513phys_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
Tom Riniae21e7f2021-08-30 09:16:29 -0400522#ifdef CONFIG_ENV_VARS_UBOOT_RUNTIME_CONFIG
Peng Fanb15705a2021-08-07 16:00:35 +0800523void get_board_serial(struct tag_serialnr *serialnr)
524{
Ye Li7bea5b02021-08-07 16:01:00 +0800525 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];
Peng Fanb15705a2021-08-07 16:00:35 +0800537}
538#endif
539
Ye Li6ee435eb2021-08-07 16:00:50 +0800540static void set_core0_reset_vector(u32 entry)
Peng Fanb15705a2021-08-07 16:00:35 +0800541{
Ye Li6dd43022021-08-07 16:00:48 +0800542 /* Update SIM1 DGO8 for reset vector base */
Ye Li6ee435eb2021-08-07 16:00:50 +0800543 writel(entry, SIM1_BASE_ADDR + 0x5c);
Ye Li6dd43022021-08-07 16:00:48 +0800544
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));
Ye Li6ee435eb2021-08-07 16:00:50 +0800557}
558
Peng Fan9c87e462021-08-07 16:00:59 +0800559static int trdc_set_access(void)
Peng Fanb5c41b12021-08-07 16:00:58 +0800560{
561 /*
Peng Fan9c87e462021-08-07 16:00:59 +0800562 * TRDC mgr + 4 MBC + 2 MRC.
563 * S400 should already configure when release RDC
564 * A35 only map non-secure region for pbridge0 and 1, set sec_access to false
Peng Fanb5c41b12021-08-07 16:00:58 +0800565 */
Peng Fan9c87e462021-08-07 16:00:59 +0800566 trdc_mbc_set_access(2, 7, 0, 49, false);
567 trdc_mbc_set_access(2, 7, 0, 50, false);
568 trdc_mbc_set_access(2, 7, 0, 51, false);
569 trdc_mbc_set_access(2, 7, 0, 52, false);
570 trdc_mbc_set_access(2, 7, 0, 53, false);
571 trdc_mbc_set_access(2, 7, 0, 54, false);
Peng Fanb5c41b12021-08-07 16:00:58 +0800572
Peng Fan9c87e462021-08-07 16:00:59 +0800573 /* CGC0: PBridge0 slot 47 */
574 trdc_mbc_set_access(2, 7, 0, 47, false);
Peng Fanb5c41b12021-08-07 16:00:58 +0800575
Peng Fan9c87e462021-08-07 16:00:59 +0800576 /* Iomuxc0: : PBridge1 slot 33 */
577 trdc_mbc_set_access(2, 7, 1, 33, false);
Peng Fanb5c41b12021-08-07 16:00:58 +0800578
Ye Lid325d372021-10-29 09:46:20 +0800579 /* flexspi0 */
580 trdc_mrc_region_set_access(0, 7, 0x04000000, 0x0c000000, false);
Ye Li27666ca2021-10-29 09:46:21 +0800581
582 /* tpm0: PBridge1 slot 21 */
583 trdc_mbc_set_access(2, 7, 1, 21, false);
584 /* lpi2c0: PBridge1 slot 24 */
585 trdc_mbc_set_access(2, 7, 1, 24, false);
Peng Fanb5c41b12021-08-07 16:00:58 +0800586 return 0;
587}
588
Ye Liec10a802022-04-06 14:30:17 +0800589void lpav_configure(bool lpav_to_m33)
Ye Li43819eb2021-10-29 09:46:16 +0800590{
Ye Liec10a802022-04-06 14:30:17 +0800591 if (!lpav_to_m33)
592 setbits_le32(SIM_SEC_BASE_ADDR + 0x44, BIT(7)); /* LPAV to APD */
Ye Li43819eb2021-10-29 09:46:16 +0800593
Peng Fanfa609b42021-10-29 09:46:17 +0800594 /* PXP/GPU 2D/3D/DCNANO/MIPI_DSI/EPDC/HIFI4 to APD */
595 setbits_le32(SIM_SEC_BASE_ADDR + 0x4c, 0x7F);
Ye Li43819eb2021-10-29 09:46:16 +0800596
597 /* LPAV slave/dma2 ch allocation and request allocation to APD */
598 writel(0x1f, SIM_SEC_BASE_ADDR + 0x50);
599 writel(0xffffffff, SIM_SEC_BASE_ADDR + 0x54);
600 writel(0x003fffff, SIM_SEC_BASE_ADDR + 0x58);
Ye Li715cfa02021-10-29 09:46:23 +0800601}
Ye Lia0311552021-10-29 09:46:22 +0800602
Ye Li133f8b82021-10-29 09:46:25 +0800603void load_lposc_fuse(void)
604{
605 int ret;
606 u32 val = 0, val2 = 0, reg;
607
608 ret = fuse_read(25, 0, &val);
609 if (ret)
610 return; /* failed */
611
612 ret = fuse_read(25, 1, &val2);
613 if (ret)
614 return; /* failed */
615
616 /* LPOSCCTRL */
617 reg = readl(0x2802f304);
618 reg &= ~0xff;
619 reg |= (val & 0xff);
620 writel(reg, 0x2802f304);
621}
622
Ye Li715cfa02021-10-29 09:46:23 +0800623void set_lpav_qos(void)
624{
Ye Lia0311552021-10-29 09:46:22 +0800625 /* Set read QoS of dcnano on LPAV NIC */
626 writel(0xf, 0x2e447100);
Ye Li43819eb2021-10-29 09:46:16 +0800627}
628
Ye Li6ee435eb2021-08-07 16:00:50 +0800629int arch_cpu_init(void)
630{
631 if (IS_ENABLED(CONFIG_SPL_BUILD)) {
Ye Li72012622021-10-29 09:46:15 +0800632 u32 val = 0;
633 int ret;
634 bool rdc_en = true; /* Default assume DBD_EN is set */
635
Peng Fan21bda432022-04-06 14:30:27 +0800636 /* Enable System Reset Interrupt using WDOG_AD */
637 setbits_le32(CMC1_BASE_ADDR + 0x8C, BIT(13));
638 /* Clear AD_PERIPH Power switch domain out of reset interrupt flag */
639 setbits_le32(CMC1_BASE_ADDR + 0x70, BIT(4));
640
641 if (readl(CMC1_BASE_ADDR + 0x90) & BIT(13)) {
642 /* Clear System Reset Interrupt Flag Register of WDOG_AD */
643 setbits_le32(CMC1_BASE_ADDR + 0x90, BIT(13));
644 /* Reset WDOG to clear reset request */
645 pcc_reset_peripheral(3, WDOG3_PCC3_SLOT, true);
646 pcc_reset_peripheral(3, WDOG3_PCC3_SLOT, false);
647 }
648
Ye Li853cc9d2021-08-07 16:00:55 +0800649 /* Disable wdog */
650 init_wdog();
651
Ye Li72012622021-10-29 09:46:15 +0800652 /* Read DBD_EN fuse */
653 ret = fuse_read(8, 1, &val);
654 if (!ret)
655 rdc_en = !!(val & 0x4000);
656
Peng Fanb5c41b12021-08-07 16:00:58 +0800657 if (get_boot_mode() == SINGLE_BOOT) {
Ye Li72012622021-10-29 09:46:15 +0800658 if (rdc_en)
659 release_rdc(RDC_TRDC);
660
Peng Fanb5c41b12021-08-07 16:00:58 +0800661 trdc_set_access();
Ye Liec10a802022-04-06 14:30:17 +0800662 lpav_configure(false);
663 } else {
664 lpav_configure(true);
Peng Fanb5c41b12021-08-07 16:00:58 +0800665 }
Peng Fanfa55b212021-08-07 16:00:57 +0800666
Ye Li72012622021-10-29 09:46:15 +0800667 /* Release xrdc, then allow A35 to write SRAM2 */
668 if (rdc_en)
669 release_rdc(RDC_XRDC);
670
Ye Li853cc9d2021-08-07 16:00:55 +0800671 xrdc_mrc_region_set_access(2, CONFIG_SPL_TEXT_BASE, 0xE00);
672
Peng Fan4cdb3a32022-04-06 14:30:12 +0800673 clock_init_early();
Ye Li6ee435eb2021-08-07 16:00:50 +0800674 } else {
675 /* reconfigure core0 reset vector to ROM */
676 set_core0_reset_vector(0x1000);
677 }
678
679 return 0;
680}
681
Ye Lid5ffe552023-01-31 16:42:13 +0800682int imx8ulp_dm_post_init(void)
Ye Li7bea5b02021-08-07 16:01:00 +0800683{
684 struct udevice *devp;
Ye Li2ec75c52023-01-31 16:42:12 +0800685 int ret;
Ye Lid5ffe552023-01-31 16:42:13 +0800686 u32 res;
687 struct sentinel_get_info_data *info = (struct sentinel_get_info_data *)SRAM0_BASE;
Ye Li7bea5b02021-08-07 16:01:00 +0800688
Ye Li2ec75c52023-01-31 16:42:12 +0800689 ret = uclass_get_device_by_driver(UCLASS_MISC, DM_DRIVER_GET(imx8ulp_mu), &devp);
Ye Li7bea5b02021-08-07 16:01:00 +0800690 if (ret) {
691 printf("could not get S400 mu %d\n", ret);
692 return ret;
693 }
694
Ye Lid5ffe552023-01-31 16:42:13 +0800695 ret = ahab_get_info(info, &res);
696 if (ret) {
697 printf("ahab_get_info failed %d\n", ret);
698 /* fallback to A0.1 revision */
699 memset((void *)info, 0, sizeof(struct sentinel_get_info_data));
700 info->soc = 0xa000084d;
701 }
702
703 set_cpu_info(info);
704
Ye Li7bea5b02021-08-07 16:01:00 +0800705 return 0;
706}
Ye Lid5ffe552023-01-31 16:42:13 +0800707
708static int imx8ulp_evt_dm_post_init(void *ctx, struct event *event)
709{
710 return imx8ulp_dm_post_init();
711}
712EVENT_SPY(EVT_DM_POST_INIT, imx8ulp_evt_dm_post_init);
Ye Li7bea5b02021-08-07 16:01:00 +0800713
Ye Li6ee435eb2021-08-07 16:00:50 +0800714#if defined(CONFIG_SPL_BUILD)
715__weak void __noreturn jump_to_image_no_args(struct spl_image_info *spl_image)
716{
717 debug("image entry point: 0x%lx\n", spl_image->entry_point);
718
719 set_core0_reset_vector((u32)spl_image->entry_point);
Ye Li6dd43022021-08-07 16:00:48 +0800720
721 /* Enable the 512KB cache */
722 setbits_le32(SIM1_BASE_ADDR + 0x30, (0x1 << 4));
723
724 /* reset core */
725 setbits_le32(SIM1_BASE_ADDR + 0x30, (0x1 << 16));
726
727 while (1)
728 ;
729}
730#endif
Peng Fanfa6ae052021-08-07 16:01:03 +0800731
732void imx_get_mac_from_fuse(int dev_id, unsigned char *mac)
733{
Ye Li992b0ae2021-10-29 09:46:28 +0800734 u32 val[2] = {};
735 int ret;
736
737 ret = fuse_read(5, 3, &val[0]);
738 if (ret)
739 goto err;
740
741 ret = fuse_read(5, 4, &val[1]);
742 if (ret)
743 goto err;
744
745 mac[0] = val[0];
746 mac[1] = val[0] >> 8;
747 mac[2] = val[0] >> 16;
748 mac[3] = val[0] >> 24;
749 mac[4] = val[1];
750 mac[5] = val[1] >> 8;
751
752 debug("%s: MAC%d: %02x.%02x.%02x.%02x.%02x.%02x\n",
753 __func__, dev_id, mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
754 return;
755err:
Peng Fanfa6ae052021-08-07 16:01:03 +0800756 memset(mac, 0, 6);
Ye Li992b0ae2021-10-29 09:46:28 +0800757 printf("%s: fuse read err: %d\n", __func__, ret);
Peng Fanfa6ae052021-08-07 16:01:03 +0800758}
Ye Li479fd4a2021-08-07 16:01:08 +0800759
760int (*card_emmc_is_boot_part_en)(void) = (void *)0x67cc;
761u32 spl_arch_boot_image_offset(u32 image_offset, u32 rom_bt_dev)
762{
763 /* Hard code for eMMC image_offset on 8ULP ROM, need fix by ROM, temp workaround */
764 if (((rom_bt_dev >> 16) & 0xff) == BT_DEV_TYPE_MMC && card_emmc_is_boot_part_en())
765 image_offset = 0;
766
767 return image_offset;
768}
Ye Li48836ae2022-04-06 14:30:30 +0800769
770enum env_location env_get_location(enum env_operation op, int prio)
771{
772 enum boot_device dev = get_boot_device();
773 enum env_location env_loc = ENVL_UNKNOWN;
774
775 if (prio)
776 return env_loc;
777
778 switch (dev) {
779#ifdef CONFIG_ENV_IS_IN_SPI_FLASH
780 case QSPI_BOOT:
781 env_loc = ENVL_SPI_FLASH;
782 break;
783#endif
784#ifdef CONFIG_ENV_IS_IN_MMC
785 case SD1_BOOT:
786 case SD2_BOOT:
787 case SD3_BOOT:
788 case MMC1_BOOT:
789 case MMC2_BOOT:
790 case MMC3_BOOT:
791 env_loc = ENVL_MMC;
792 break;
793#endif
794 default:
795#if defined(CONFIG_ENV_IS_NOWHERE)
796 env_loc = ENVL_NOWHERE;
797#endif
798 break;
799 }
800
801 return env_loc;
802}