1 // SPDX-License-Identifier: GPL-2.0+
13 #include <asm/cache.h>
14 #include <dm/device-internal.h>
16 #include <dm/uclass.h>
20 #include <asm/arch/sci/sci.h>
21 #include <asm/arch/sys_proto.h>
22 #include <asm/arch-imx/cpu.h>
23 #include <asm/armv8/cpu.h>
24 #include <asm/armv8/mmu.h>
25 #include <asm/setup.h>
26 #include <asm/mach-imx/boot_mode.h>
29 DECLARE_GLOBAL_DATA_PTR;
31 #define BT_PASSOVER_TAG 0x504F
32 struct pass_over_info_t *get_pass_over_info(void)
34 struct pass_over_info_t *p =
35 (struct pass_over_info_t *)PASS_OVER_INFO_ADDR;
37 if (p->barker != BT_PASSOVER_TAG ||
38 p->len != sizeof(struct pass_over_info_t))
44 int arch_cpu_init(void)
46 #if defined(CONFIG_SPL_BUILD) && defined(CONFIG_SPL_RECOVER_DATA_SECTION)
47 spl_save_restore_data();
50 #ifdef CONFIG_SPL_BUILD
51 struct pass_over_info_t *pass_over;
53 if (is_soc_rev(CHIP_REV_A)) {
54 pass_over = get_pass_over_info();
55 if (pass_over && pass_over->g_ap_mu == 0) {
57 * When ap_mu is 0, means the U-Boot booted
58 * from first container
60 sc_misc_boot_status(-1, SC_MISC_BOOT_STATUS_SUCCESS);
68 int arch_cpu_init_dm(void)
73 node = fdt_node_offset_by_compatible(gd->fdt_blob, -1, "fsl,imx8-mu");
75 ret = uclass_get_device_by_of_offset(UCLASS_MISC, node, &devp);
77 printf("could not get scu %d\n", ret);
82 ret = sc_pm_set_resource_power_mode(-1, SC_R_SMMU,
91 int print_bootinfo(void)
93 enum boot_device bt_dev = get_boot_device();
128 printf("Unknown device %u\n", bt_dev);
135 enum boot_device get_boot_device(void)
137 enum boot_device boot_dev = SD1_BOOT;
141 sc_misc_get_boot_dev(-1, &dev_rsrc);
145 boot_dev = MMC1_BOOT;
154 boot_dev = NAND_BOOT;
157 boot_dev = FLEXSPI_BOOT;
160 boot_dev = SATA_BOOT;
174 #ifdef CONFIG_SERIAL_TAG
175 #define FUSE_UNIQUE_ID_WORD0 16
176 #define FUSE_UNIQUE_ID_WORD1 17
177 void get_board_serial(struct tag_serialnr *serialnr)
180 u32 val1 = 0, val2 = 0;
186 word1 = FUSE_UNIQUE_ID_WORD0;
187 word2 = FUSE_UNIQUE_ID_WORD1;
189 err = sc_misc_otp_fuse_read(-1, word1, &val1);
190 if (err != SC_ERR_NONE) {
191 printf("%s fuse %d read error: %d\n", __func__, word1, err);
195 err = sc_misc_otp_fuse_read(-1, word2, &val2);
196 if (err != SC_ERR_NONE) {
197 printf("%s fuse %d read error: %d\n", __func__, word2, err);
200 serialnr->low = val1;
201 serialnr->high = val2;
203 #endif /*CONFIG_SERIAL_TAG*/
205 #ifdef CONFIG_ENV_IS_IN_MMC
206 __weak int board_mmc_get_env_dev(int devno)
208 return CONFIG_SYS_MMC_ENV_DEV;
211 int mmc_get_env_dev(void)
216 sc_misc_get_boot_dev(-1, &dev_rsrc);
229 /* If not boot from sd/mmc, use default value */
230 return CONFIG_SYS_MMC_ENV_DEV;
233 return board_mmc_get_env_dev(devno);
237 #define MEMSTART_ALIGNMENT SZ_2M /* Align the memory start with 2MB */
239 static int get_owned_memreg(sc_rm_mr_t mr, sc_faddr_t *addr_start,
240 sc_faddr_t *addr_end)
242 sc_faddr_t start, end;
246 owned = sc_rm_is_memreg_owned(-1, mr);
248 ret = sc_rm_get_memreg_info(-1, mr, &start, &end);
250 printf("Memreg get info failed, %d\n", ret);
253 debug("0x%llx -- 0x%llx\n", start, end);
263 phys_size_t get_effective_memsize(void)
266 sc_faddr_t start, end, end1, start_aligned;
269 end1 = (sc_faddr_t)PHYS_SDRAM_1 + PHYS_SDRAM_1_SIZE;
271 for (mr = 0; mr < 64; mr++) {
272 err = get_owned_memreg(mr, &start, &end);
274 start_aligned = roundup(start, MEMSTART_ALIGNMENT);
275 /* Too small memory region, not use it */
276 if (start_aligned > end)
279 /* Find the memory region runs the U-Boot */
280 if (start >= PHYS_SDRAM_1 && start <= end1 &&
281 (start <= CONFIG_SYS_TEXT_BASE &&
282 end >= CONFIG_SYS_TEXT_BASE)) {
283 if ((end + 1) <= ((sc_faddr_t)PHYS_SDRAM_1 +
285 return (end - PHYS_SDRAM_1 + 1);
287 return PHYS_SDRAM_1_SIZE;
292 return PHYS_SDRAM_1_SIZE;
298 sc_faddr_t start, end, end1, end2;
301 end1 = (sc_faddr_t)PHYS_SDRAM_1 + PHYS_SDRAM_1_SIZE;
302 end2 = (sc_faddr_t)PHYS_SDRAM_2 + PHYS_SDRAM_2_SIZE;
303 for (mr = 0; mr < 64; mr++) {
304 err = get_owned_memreg(mr, &start, &end);
306 start = roundup(start, MEMSTART_ALIGNMENT);
307 /* Too small memory region, not use it */
311 if (start >= PHYS_SDRAM_1 && start <= end1) {
312 if ((end + 1) <= end1)
313 gd->ram_size += end - start + 1;
315 gd->ram_size += end1 - start;
316 } else if (start >= PHYS_SDRAM_2 && start <= end2) {
317 if ((end + 1) <= end2)
318 gd->ram_size += end - start + 1;
320 gd->ram_size += end2 - start;
325 /* If error, set to the default value */
327 gd->ram_size = PHYS_SDRAM_1_SIZE;
328 gd->ram_size += PHYS_SDRAM_2_SIZE;
333 static void dram_bank_sort(int current_bank)
338 while (current_bank > 0) {
339 if (gd->bd->bi_dram[current_bank - 1].start >
340 gd->bd->bi_dram[current_bank].start) {
341 start = gd->bd->bi_dram[current_bank - 1].start;
342 size = gd->bd->bi_dram[current_bank - 1].size;
344 gd->bd->bi_dram[current_bank - 1].start =
345 gd->bd->bi_dram[current_bank].start;
346 gd->bd->bi_dram[current_bank - 1].size =
347 gd->bd->bi_dram[current_bank].size;
349 gd->bd->bi_dram[current_bank].start = start;
350 gd->bd->bi_dram[current_bank].size = size;
356 int dram_init_banksize(void)
359 sc_faddr_t start, end, end1, end2;
363 end1 = (sc_faddr_t)PHYS_SDRAM_1 + PHYS_SDRAM_1_SIZE;
364 end2 = (sc_faddr_t)PHYS_SDRAM_2 + PHYS_SDRAM_2_SIZE;
366 for (mr = 0; mr < 64 && i < CONFIG_NR_DRAM_BANKS; mr++) {
367 err = get_owned_memreg(mr, &start, &end);
369 start = roundup(start, MEMSTART_ALIGNMENT);
370 if (start > end) /* Small memory region, no use it */
373 if (start >= PHYS_SDRAM_1 && start <= end1) {
374 gd->bd->bi_dram[i].start = start;
376 if ((end + 1) <= end1)
377 gd->bd->bi_dram[i].size =
380 gd->bd->bi_dram[i].size = end1 - start;
384 } else if (start >= PHYS_SDRAM_2 && start <= end2) {
385 gd->bd->bi_dram[i].start = start;
387 if ((end + 1) <= end2)
388 gd->bd->bi_dram[i].size =
391 gd->bd->bi_dram[i].size = end2 - start;
399 /* If error, set to the default value */
401 gd->bd->bi_dram[0].start = PHYS_SDRAM_1;
402 gd->bd->bi_dram[0].size = PHYS_SDRAM_1_SIZE;
403 gd->bd->bi_dram[1].start = PHYS_SDRAM_2;
404 gd->bd->bi_dram[1].size = PHYS_SDRAM_2_SIZE;
410 static u64 get_block_attrs(sc_faddr_t addr_start)
412 u64 attr = PTE_BLOCK_MEMTYPE(MT_DEVICE_NGNRNE) | PTE_BLOCK_NON_SHARE |
413 PTE_BLOCK_PXN | PTE_BLOCK_UXN;
415 if ((addr_start >= PHYS_SDRAM_1 &&
416 addr_start <= ((sc_faddr_t)PHYS_SDRAM_1 + PHYS_SDRAM_1_SIZE)) ||
417 (addr_start >= PHYS_SDRAM_2 &&
418 addr_start <= ((sc_faddr_t)PHYS_SDRAM_2 + PHYS_SDRAM_2_SIZE)))
419 return (PTE_BLOCK_MEMTYPE(MT_NORMAL) | PTE_BLOCK_OUTER_SHARE);
424 static u64 get_block_size(sc_faddr_t addr_start, sc_faddr_t addr_end)
426 sc_faddr_t end1, end2;
428 end1 = (sc_faddr_t)PHYS_SDRAM_1 + PHYS_SDRAM_1_SIZE;
429 end2 = (sc_faddr_t)PHYS_SDRAM_2 + PHYS_SDRAM_2_SIZE;
431 if (addr_start >= PHYS_SDRAM_1 && addr_start <= end1) {
432 if ((addr_end + 1) > end1)
433 return end1 - addr_start;
434 } else if (addr_start >= PHYS_SDRAM_2 && addr_start <= end2) {
435 if ((addr_end + 1) > end2)
436 return end2 - addr_start;
439 return (addr_end - addr_start + 1);
442 #define MAX_PTE_ENTRIES 512
443 #define MAX_MEM_MAP_REGIONS 16
445 static struct mm_region imx8_mem_map[MAX_MEM_MAP_REGIONS];
446 struct mm_region *mem_map = imx8_mem_map;
448 void enable_caches(void)
451 sc_faddr_t start, end;
454 /* Create map for registers access from 0x1c000000 to 0x80000000*/
455 imx8_mem_map[0].virt = 0x1c000000UL;
456 imx8_mem_map[0].phys = 0x1c000000UL;
457 imx8_mem_map[0].size = 0x64000000UL;
458 imx8_mem_map[0].attrs = PTE_BLOCK_MEMTYPE(MT_DEVICE_NGNRNE) |
459 PTE_BLOCK_NON_SHARE | PTE_BLOCK_PXN | PTE_BLOCK_UXN;
462 for (mr = 0; mr < 64 && i < MAX_MEM_MAP_REGIONS; mr++) {
463 err = get_owned_memreg(mr, &start, &end);
465 imx8_mem_map[i].virt = start;
466 imx8_mem_map[i].phys = start;
467 imx8_mem_map[i].size = get_block_size(start, end);
468 imx8_mem_map[i].attrs = get_block_attrs(start);
473 if (i < MAX_MEM_MAP_REGIONS) {
474 imx8_mem_map[i].size = 0;
475 imx8_mem_map[i].attrs = 0;
477 puts("Error, need more MEM MAP REGIONS reserved\n");
482 for (i = 0; i < MAX_MEM_MAP_REGIONS; i++) {
483 debug("[%d] vir = 0x%llx phys = 0x%llx size = 0x%llx attrs = 0x%llx\n",
484 i, imx8_mem_map[i].virt, imx8_mem_map[i].phys,
485 imx8_mem_map[i].size, imx8_mem_map[i].attrs);
492 #if !CONFIG_IS_ENABLED(SYS_DCACHE_OFF)
493 u64 get_page_table_size(void)
495 u64 one_pt = MAX_PTE_ENTRIES * sizeof(u64);
499 * For each memory region, the max table size:
500 * 2 level 3 tables + 2 level 2 tables + 1 level 1 table
502 size = (2 + 2 + 1) * one_pt * MAX_MEM_MAP_REGIONS + one_pt;
505 * We need to duplicate our page table once to have an emergency pt to
506 * resort to when splitting page tables later on
511 * We may need to split page tables later on if dcache settings change,
512 * so reserve up to 4 (random pick) page tables for that.
520 #if defined(CONFIG_IMX8QM)
521 #define FUSE_MAC0_WORD0 452
522 #define FUSE_MAC0_WORD1 453
523 #define FUSE_MAC1_WORD0 454
524 #define FUSE_MAC1_WORD1 455
525 #elif defined(CONFIG_IMX8QXP)
526 #define FUSE_MAC0_WORD0 708
527 #define FUSE_MAC0_WORD1 709
528 #define FUSE_MAC1_WORD0 710
529 #define FUSE_MAC1_WORD1 711
532 void imx_get_mac_from_fuse(int dev_id, unsigned char *mac)
534 u32 word[2], val[2] = {};
538 word[0] = FUSE_MAC0_WORD0;
539 word[1] = FUSE_MAC0_WORD1;
541 word[0] = FUSE_MAC1_WORD0;
542 word[1] = FUSE_MAC1_WORD1;
545 for (i = 0; i < 2; i++) {
546 ret = sc_misc_otp_fuse_read(-1, word[i], &val[i]);
552 mac[1] = val[0] >> 8;
553 mac[2] = val[0] >> 16;
554 mac[3] = val[0] >> 24;
556 mac[5] = val[1] >> 8;
558 debug("%s: MAC%d: %02x.%02x.%02x.%02x.%02x.%02x\n",
559 __func__, dev_id, mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
562 printf("%s: fuse %d, err: %d\n", __func__, word[i], ret);
565 u32 get_cpu_rev(void)
570 ret = sc_misc_get_control(-1, SC_R_SYSTEM, SC_C_ID, &id);
574 rev = (id >> 5) & 0xf;
575 id = (id & 0x1f) + MXC_SOC_IMX8; /* Dummy ID for chip */
577 return (id << 12) | rev;
580 void board_boot_order(u32 *spl_boot_list)
582 spl_boot_list[0] = spl_boot_device();
584 if (spl_boot_list[0] == BOOT_DEVICE_SPI) {
585 /* Check whether we own the flexspi0, if not, use NOR boot */
586 if (!sc_rm_is_resource_owned(-1, SC_R_FSPI_0))
587 spl_boot_list[0] = BOOT_DEVICE_NOR;
591 bool m4_parts_booted(void)
593 sc_rm_pt_t m4_parts[2];
596 err = sc_rm_get_resource_owner(-1, SC_R_M4_0_PID0, &m4_parts[0]);
598 printf("%s get resource [%d] owner error: %d\n", __func__,
599 SC_R_M4_0_PID0, err);
603 if (sc_pm_is_partition_started(-1, m4_parts[0]))
607 err = sc_rm_get_resource_owner(-1, SC_R_M4_1_PID0, &m4_parts[1]);
609 printf("%s get resource [%d] owner error: %d\n",
610 __func__, SC_R_M4_1_PID0, err);
614 if (sc_pm_is_partition_started(-1, m4_parts[1]))