1 // SPDX-License-Identifier: GPL-2.0+
12 #include <asm/cache.h>
13 #include <dm/device-internal.h>
15 #include <dm/uclass.h>
19 #include <asm/arch/sci/sci.h>
20 #include <asm/arch/sys_proto.h>
21 #include <asm/arch-imx/cpu.h>
22 #include <asm/armv8/cpu.h>
23 #include <asm/armv8/mmu.h>
24 #include <asm/setup.h>
25 #include <asm/mach-imx/boot_mode.h>
28 DECLARE_GLOBAL_DATA_PTR;
30 #define BT_PASSOVER_TAG 0x504F
31 struct pass_over_info_t *get_pass_over_info(void)
33 struct pass_over_info_t *p =
34 (struct pass_over_info_t *)PASS_OVER_INFO_ADDR;
36 if (p->barker != BT_PASSOVER_TAG ||
37 p->len != sizeof(struct pass_over_info_t))
43 int arch_cpu_init(void)
45 #if defined(CONFIG_SPL_BUILD) && defined(CONFIG_SPL_RECOVER_DATA_SECTION)
46 spl_save_restore_data();
49 #ifdef CONFIG_SPL_BUILD
50 struct pass_over_info_t *pass_over;
52 if (is_soc_rev(CHIP_REV_A)) {
53 pass_over = get_pass_over_info();
54 if (pass_over && pass_over->g_ap_mu == 0) {
56 * When ap_mu is 0, means the U-Boot booted
57 * from first container
59 sc_misc_boot_status(-1, SC_MISC_BOOT_STATUS_SUCCESS);
67 int arch_cpu_init_dm(void)
72 node = fdt_node_offset_by_compatible(gd->fdt_blob, -1, "fsl,imx8-mu");
74 ret = uclass_get_device_by_of_offset(UCLASS_MISC, node, &devp);
76 printf("could not get scu %d\n", ret);
81 ret = sc_pm_set_resource_power_mode(-1, SC_R_SMMU,
90 int print_bootinfo(void)
92 enum boot_device bt_dev = get_boot_device();
127 printf("Unknown device %u\n", bt_dev);
134 enum boot_device get_boot_device(void)
136 enum boot_device boot_dev = SD1_BOOT;
140 sc_misc_get_boot_dev(-1, &dev_rsrc);
144 boot_dev = MMC1_BOOT;
153 boot_dev = NAND_BOOT;
156 boot_dev = FLEXSPI_BOOT;
159 boot_dev = SATA_BOOT;
173 #ifdef CONFIG_SERIAL_TAG
174 #define FUSE_UNIQUE_ID_WORD0 16
175 #define FUSE_UNIQUE_ID_WORD1 17
176 void get_board_serial(struct tag_serialnr *serialnr)
179 u32 val1 = 0, val2 = 0;
185 word1 = FUSE_UNIQUE_ID_WORD0;
186 word2 = FUSE_UNIQUE_ID_WORD1;
188 err = sc_misc_otp_fuse_read(-1, word1, &val1);
189 if (err != SC_ERR_NONE) {
190 printf("%s fuse %d read error: %d\n", __func__, word1, err);
194 err = sc_misc_otp_fuse_read(-1, word2, &val2);
195 if (err != SC_ERR_NONE) {
196 printf("%s fuse %d read error: %d\n", __func__, word2, err);
199 serialnr->low = val1;
200 serialnr->high = val2;
202 #endif /*CONFIG_SERIAL_TAG*/
204 #ifdef CONFIG_ENV_IS_IN_MMC
205 __weak int board_mmc_get_env_dev(int devno)
207 return CONFIG_SYS_MMC_ENV_DEV;
210 int mmc_get_env_dev(void)
215 sc_misc_get_boot_dev(-1, &dev_rsrc);
228 /* If not boot from sd/mmc, use default value */
229 return CONFIG_SYS_MMC_ENV_DEV;
232 return board_mmc_get_env_dev(devno);
236 #define MEMSTART_ALIGNMENT SZ_2M /* Align the memory start with 2MB */
238 static int get_owned_memreg(sc_rm_mr_t mr, sc_faddr_t *addr_start,
239 sc_faddr_t *addr_end)
241 sc_faddr_t start, end;
245 owned = sc_rm_is_memreg_owned(-1, mr);
247 ret = sc_rm_get_memreg_info(-1, mr, &start, &end);
249 printf("Memreg get info failed, %d\n", ret);
252 debug("0x%llx -- 0x%llx\n", start, end);
262 phys_size_t get_effective_memsize(void)
265 sc_faddr_t start, end, end1, start_aligned;
268 end1 = (sc_faddr_t)PHYS_SDRAM_1 + PHYS_SDRAM_1_SIZE;
270 for (mr = 0; mr < 64; mr++) {
271 err = get_owned_memreg(mr, &start, &end);
273 start_aligned = roundup(start, MEMSTART_ALIGNMENT);
274 /* Too small memory region, not use it */
275 if (start_aligned > end)
278 /* Find the memory region runs the U-Boot */
279 if (start >= PHYS_SDRAM_1 && start <= end1 &&
280 (start <= CONFIG_SYS_TEXT_BASE &&
281 end >= CONFIG_SYS_TEXT_BASE)) {
282 if ((end + 1) <= ((sc_faddr_t)PHYS_SDRAM_1 +
284 return (end - PHYS_SDRAM_1 + 1);
286 return PHYS_SDRAM_1_SIZE;
291 return PHYS_SDRAM_1_SIZE;
297 sc_faddr_t start, end, end1, end2;
300 end1 = (sc_faddr_t)PHYS_SDRAM_1 + PHYS_SDRAM_1_SIZE;
301 end2 = (sc_faddr_t)PHYS_SDRAM_2 + PHYS_SDRAM_2_SIZE;
302 for (mr = 0; mr < 64; mr++) {
303 err = get_owned_memreg(mr, &start, &end);
305 start = roundup(start, MEMSTART_ALIGNMENT);
306 /* Too small memory region, not use it */
310 if (start >= PHYS_SDRAM_1 && start <= end1) {
311 if ((end + 1) <= end1)
312 gd->ram_size += end - start + 1;
314 gd->ram_size += end1 - start;
315 } else if (start >= PHYS_SDRAM_2 && start <= end2) {
316 if ((end + 1) <= end2)
317 gd->ram_size += end - start + 1;
319 gd->ram_size += end2 - start;
324 /* If error, set to the default value */
326 gd->ram_size = PHYS_SDRAM_1_SIZE;
327 gd->ram_size += PHYS_SDRAM_2_SIZE;
332 static void dram_bank_sort(int current_bank)
337 while (current_bank > 0) {
338 if (gd->bd->bi_dram[current_bank - 1].start >
339 gd->bd->bi_dram[current_bank].start) {
340 start = gd->bd->bi_dram[current_bank - 1].start;
341 size = gd->bd->bi_dram[current_bank - 1].size;
343 gd->bd->bi_dram[current_bank - 1].start =
344 gd->bd->bi_dram[current_bank].start;
345 gd->bd->bi_dram[current_bank - 1].size =
346 gd->bd->bi_dram[current_bank].size;
348 gd->bd->bi_dram[current_bank].start = start;
349 gd->bd->bi_dram[current_bank].size = size;
355 int dram_init_banksize(void)
358 sc_faddr_t start, end, end1, end2;
362 end1 = (sc_faddr_t)PHYS_SDRAM_1 + PHYS_SDRAM_1_SIZE;
363 end2 = (sc_faddr_t)PHYS_SDRAM_2 + PHYS_SDRAM_2_SIZE;
365 for (mr = 0; mr < 64 && i < CONFIG_NR_DRAM_BANKS; mr++) {
366 err = get_owned_memreg(mr, &start, &end);
368 start = roundup(start, MEMSTART_ALIGNMENT);
369 if (start > end) /* Small memory region, no use it */
372 if (start >= PHYS_SDRAM_1 && start <= end1) {
373 gd->bd->bi_dram[i].start = start;
375 if ((end + 1) <= end1)
376 gd->bd->bi_dram[i].size =
379 gd->bd->bi_dram[i].size = end1 - start;
383 } else if (start >= PHYS_SDRAM_2 && start <= end2) {
384 gd->bd->bi_dram[i].start = start;
386 if ((end + 1) <= end2)
387 gd->bd->bi_dram[i].size =
390 gd->bd->bi_dram[i].size = end2 - start;
398 /* If error, set to the default value */
400 gd->bd->bi_dram[0].start = PHYS_SDRAM_1;
401 gd->bd->bi_dram[0].size = PHYS_SDRAM_1_SIZE;
402 gd->bd->bi_dram[1].start = PHYS_SDRAM_2;
403 gd->bd->bi_dram[1].size = PHYS_SDRAM_2_SIZE;
409 static u64 get_block_attrs(sc_faddr_t addr_start)
411 u64 attr = PTE_BLOCK_MEMTYPE(MT_DEVICE_NGNRNE) | PTE_BLOCK_NON_SHARE |
412 PTE_BLOCK_PXN | PTE_BLOCK_UXN;
414 if ((addr_start >= PHYS_SDRAM_1 &&
415 addr_start <= ((sc_faddr_t)PHYS_SDRAM_1 + PHYS_SDRAM_1_SIZE)) ||
416 (addr_start >= PHYS_SDRAM_2 &&
417 addr_start <= ((sc_faddr_t)PHYS_SDRAM_2 + PHYS_SDRAM_2_SIZE)))
418 return (PTE_BLOCK_MEMTYPE(MT_NORMAL) | PTE_BLOCK_OUTER_SHARE);
423 static u64 get_block_size(sc_faddr_t addr_start, sc_faddr_t addr_end)
425 sc_faddr_t end1, end2;
427 end1 = (sc_faddr_t)PHYS_SDRAM_1 + PHYS_SDRAM_1_SIZE;
428 end2 = (sc_faddr_t)PHYS_SDRAM_2 + PHYS_SDRAM_2_SIZE;
430 if (addr_start >= PHYS_SDRAM_1 && addr_start <= end1) {
431 if ((addr_end + 1) > end1)
432 return end1 - addr_start;
433 } else if (addr_start >= PHYS_SDRAM_2 && addr_start <= end2) {
434 if ((addr_end + 1) > end2)
435 return end2 - addr_start;
438 return (addr_end - addr_start + 1);
441 #define MAX_PTE_ENTRIES 512
442 #define MAX_MEM_MAP_REGIONS 16
444 static struct mm_region imx8_mem_map[MAX_MEM_MAP_REGIONS];
445 struct mm_region *mem_map = imx8_mem_map;
447 void enable_caches(void)
450 sc_faddr_t start, end;
453 /* Create map for registers access from 0x1c000000 to 0x80000000*/
454 imx8_mem_map[0].virt = 0x1c000000UL;
455 imx8_mem_map[0].phys = 0x1c000000UL;
456 imx8_mem_map[0].size = 0x64000000UL;
457 imx8_mem_map[0].attrs = PTE_BLOCK_MEMTYPE(MT_DEVICE_NGNRNE) |
458 PTE_BLOCK_NON_SHARE | PTE_BLOCK_PXN | PTE_BLOCK_UXN;
461 for (mr = 0; mr < 64 && i < MAX_MEM_MAP_REGIONS; mr++) {
462 err = get_owned_memreg(mr, &start, &end);
464 imx8_mem_map[i].virt = start;
465 imx8_mem_map[i].phys = start;
466 imx8_mem_map[i].size = get_block_size(start, end);
467 imx8_mem_map[i].attrs = get_block_attrs(start);
472 if (i < MAX_MEM_MAP_REGIONS) {
473 imx8_mem_map[i].size = 0;
474 imx8_mem_map[i].attrs = 0;
476 puts("Error, need more MEM MAP REGIONS reserved\n");
481 for (i = 0; i < MAX_MEM_MAP_REGIONS; i++) {
482 debug("[%d] vir = 0x%llx phys = 0x%llx size = 0x%llx attrs = 0x%llx\n",
483 i, imx8_mem_map[i].virt, imx8_mem_map[i].phys,
484 imx8_mem_map[i].size, imx8_mem_map[i].attrs);
491 #if !CONFIG_IS_ENABLED(SYS_DCACHE_OFF)
492 u64 get_page_table_size(void)
494 u64 one_pt = MAX_PTE_ENTRIES * sizeof(u64);
498 * For each memory region, the max table size:
499 * 2 level 3 tables + 2 level 2 tables + 1 level 1 table
501 size = (2 + 2 + 1) * one_pt * MAX_MEM_MAP_REGIONS + one_pt;
504 * We need to duplicate our page table once to have an emergency pt to
505 * resort to when splitting page tables later on
510 * We may need to split page tables later on if dcache settings change,
511 * so reserve up to 4 (random pick) page tables for that.
519 #if defined(CONFIG_IMX8QM)
520 #define FUSE_MAC0_WORD0 452
521 #define FUSE_MAC0_WORD1 453
522 #define FUSE_MAC1_WORD0 454
523 #define FUSE_MAC1_WORD1 455
524 #elif defined(CONFIG_IMX8QXP)
525 #define FUSE_MAC0_WORD0 708
526 #define FUSE_MAC0_WORD1 709
527 #define FUSE_MAC1_WORD0 710
528 #define FUSE_MAC1_WORD1 711
531 void imx_get_mac_from_fuse(int dev_id, unsigned char *mac)
533 u32 word[2], val[2] = {};
537 word[0] = FUSE_MAC0_WORD0;
538 word[1] = FUSE_MAC0_WORD1;
540 word[0] = FUSE_MAC1_WORD0;
541 word[1] = FUSE_MAC1_WORD1;
544 for (i = 0; i < 2; i++) {
545 ret = sc_misc_otp_fuse_read(-1, word[i], &val[i]);
551 mac[1] = val[0] >> 8;
552 mac[2] = val[0] >> 16;
553 mac[3] = val[0] >> 24;
555 mac[5] = val[1] >> 8;
557 debug("%s: MAC%d: %02x.%02x.%02x.%02x.%02x.%02x\n",
558 __func__, dev_id, mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
561 printf("%s: fuse %d, err: %d\n", __func__, word[i], ret);
564 u32 get_cpu_rev(void)
569 ret = sc_misc_get_control(-1, SC_R_SYSTEM, SC_C_ID, &id);
573 rev = (id >> 5) & 0xf;
574 id = (id & 0x1f) + MXC_SOC_IMX8; /* Dummy ID for chip */
576 return (id << 12) | rev;
579 void board_boot_order(u32 *spl_boot_list)
581 spl_boot_list[0] = spl_boot_device();
583 if (spl_boot_list[0] == BOOT_DEVICE_SPI) {
584 /* Check whether we own the flexspi0, if not, use NOR boot */
585 if (!sc_rm_is_resource_owned(-1, SC_R_FSPI_0))
586 spl_boot_list[0] = BOOT_DEVICE_NOR;
590 bool m4_parts_booted(void)
592 sc_rm_pt_t m4_parts[2];
595 err = sc_rm_get_resource_owner(-1, SC_R_M4_0_PID0, &m4_parts[0]);
597 printf("%s get resource [%d] owner error: %d\n", __func__,
598 SC_R_M4_0_PID0, err);
602 if (sc_pm_is_partition_started(-1, m4_parts[0]))
606 err = sc_rm_get_resource_owner(-1, SC_R_M4_1_PID0, &m4_parts[1]);
608 printf("%s get resource [%d] owner error: %d\n",
609 __func__, SC_R_M4_1_PID0, err);
613 if (sc_pm_is_partition_started(-1, m4_parts[1]))