1 // SPDX-License-Identifier: GPL-2.0+
12 #include <dm/device-internal.h>
14 #include <dm/uclass.h>
17 #include <asm/arch/sci/sci.h>
18 #include <asm/arch/sys_proto.h>
19 #include <asm/arch-imx/cpu.h>
20 #include <asm/armv8/cpu.h>
21 #include <asm/armv8/mmu.h>
22 #include <asm/setup.h>
23 #include <asm/mach-imx/boot_mode.h>
25 DECLARE_GLOBAL_DATA_PTR;
27 #define BT_PASSOVER_TAG 0x504F
28 struct pass_over_info_t *get_pass_over_info(void)
30 struct pass_over_info_t *p =
31 (struct pass_over_info_t *)PASS_OVER_INFO_ADDR;
33 if (p->barker != BT_PASSOVER_TAG ||
34 p->len != sizeof(struct pass_over_info_t))
40 int arch_cpu_init(void)
42 #ifdef CONFIG_SPL_BUILD
43 struct pass_over_info_t *pass_over;
45 if (is_soc_rev(CHIP_REV_A)) {
46 pass_over = get_pass_over_info();
47 if (pass_over && pass_over->g_ap_mu == 0) {
49 * When ap_mu is 0, means the U-Boot booted
50 * from first container
52 sc_misc_boot_status(-1, SC_MISC_BOOT_STATUS_SUCCESS);
60 int arch_cpu_init_dm(void)
65 node = fdt_node_offset_by_compatible(gd->fdt_blob, -1, "fsl,imx8-mu");
67 ret = uclass_get_device_by_of_offset(UCLASS_MISC, node, &devp);
69 printf("could not get scu %d\n", ret);
74 ret = sc_pm_set_resource_power_mode(-1, SC_R_SMMU,
83 int print_bootinfo(void)
85 enum boot_device bt_dev = get_boot_device();
120 printf("Unknown device %u\n", bt_dev);
127 enum boot_device get_boot_device(void)
129 enum boot_device boot_dev = SD1_BOOT;
133 sc_misc_get_boot_dev(-1, &dev_rsrc);
137 boot_dev = MMC1_BOOT;
146 boot_dev = NAND_BOOT;
149 boot_dev = FLEXSPI_BOOT;
152 boot_dev = SATA_BOOT;
166 #ifdef CONFIG_SERIAL_TAG
167 #define FUSE_UNIQUE_ID_WORD0 16
168 #define FUSE_UNIQUE_ID_WORD1 17
169 void get_board_serial(struct tag_serialnr *serialnr)
172 u32 val1 = 0, val2 = 0;
178 word1 = FUSE_UNIQUE_ID_WORD0;
179 word2 = FUSE_UNIQUE_ID_WORD1;
181 err = sc_misc_otp_fuse_read(-1, word1, &val1);
182 if (err != SC_ERR_NONE) {
183 printf("%s fuse %d read error: %d\n", __func__, word1, err);
187 err = sc_misc_otp_fuse_read(-1, word2, &val2);
188 if (err != SC_ERR_NONE) {
189 printf("%s fuse %d read error: %d\n", __func__, word2, err);
192 serialnr->low = val1;
193 serialnr->high = val2;
195 #endif /*CONFIG_SERIAL_TAG*/
197 #ifdef CONFIG_ENV_IS_IN_MMC
198 __weak int board_mmc_get_env_dev(int devno)
200 return CONFIG_SYS_MMC_ENV_DEV;
203 int mmc_get_env_dev(void)
208 sc_misc_get_boot_dev(-1, &dev_rsrc);
221 /* If not boot from sd/mmc, use default value */
222 return CONFIG_SYS_MMC_ENV_DEV;
225 return board_mmc_get_env_dev(devno);
229 #define MEMSTART_ALIGNMENT SZ_2M /* Align the memory start with 2MB */
231 static int get_owned_memreg(sc_rm_mr_t mr, sc_faddr_t *addr_start,
232 sc_faddr_t *addr_end)
234 sc_faddr_t start, end;
238 owned = sc_rm_is_memreg_owned(-1, mr);
240 ret = sc_rm_get_memreg_info(-1, mr, &start, &end);
242 printf("Memreg get info failed, %d\n", ret);
245 debug("0x%llx -- 0x%llx\n", start, end);
255 phys_size_t get_effective_memsize(void)
258 sc_faddr_t start, end, end1, start_aligned;
261 end1 = (sc_faddr_t)PHYS_SDRAM_1 + PHYS_SDRAM_1_SIZE;
263 for (mr = 0; mr < 64; mr++) {
264 err = get_owned_memreg(mr, &start, &end);
266 start_aligned = roundup(start, MEMSTART_ALIGNMENT);
267 /* Too small memory region, not use it */
268 if (start_aligned > end)
271 /* Find the memory region runs the U-Boot */
272 if (start >= PHYS_SDRAM_1 && start <= end1 &&
273 (start <= CONFIG_SYS_TEXT_BASE &&
274 end >= CONFIG_SYS_TEXT_BASE)) {
275 if ((end + 1) <= ((sc_faddr_t)PHYS_SDRAM_1 +
277 return (end - PHYS_SDRAM_1 + 1);
279 return PHYS_SDRAM_1_SIZE;
284 return PHYS_SDRAM_1_SIZE;
290 sc_faddr_t start, end, end1, end2;
293 end1 = (sc_faddr_t)PHYS_SDRAM_1 + PHYS_SDRAM_1_SIZE;
294 end2 = (sc_faddr_t)PHYS_SDRAM_2 + PHYS_SDRAM_2_SIZE;
295 for (mr = 0; mr < 64; mr++) {
296 err = get_owned_memreg(mr, &start, &end);
298 start = roundup(start, MEMSTART_ALIGNMENT);
299 /* Too small memory region, not use it */
303 if (start >= PHYS_SDRAM_1 && start <= end1) {
304 if ((end + 1) <= end1)
305 gd->ram_size += end - start + 1;
307 gd->ram_size += end1 - start;
308 } else if (start >= PHYS_SDRAM_2 && start <= end2) {
309 if ((end + 1) <= end2)
310 gd->ram_size += end - start + 1;
312 gd->ram_size += end2 - start;
317 /* If error, set to the default value */
319 gd->ram_size = PHYS_SDRAM_1_SIZE;
320 gd->ram_size += PHYS_SDRAM_2_SIZE;
325 static void dram_bank_sort(int current_bank)
330 while (current_bank > 0) {
331 if (gd->bd->bi_dram[current_bank - 1].start >
332 gd->bd->bi_dram[current_bank].start) {
333 start = gd->bd->bi_dram[current_bank - 1].start;
334 size = gd->bd->bi_dram[current_bank - 1].size;
336 gd->bd->bi_dram[current_bank - 1].start =
337 gd->bd->bi_dram[current_bank].start;
338 gd->bd->bi_dram[current_bank - 1].size =
339 gd->bd->bi_dram[current_bank].size;
341 gd->bd->bi_dram[current_bank].start = start;
342 gd->bd->bi_dram[current_bank].size = size;
348 int dram_init_banksize(void)
351 sc_faddr_t start, end, end1, end2;
355 end1 = (sc_faddr_t)PHYS_SDRAM_1 + PHYS_SDRAM_1_SIZE;
356 end2 = (sc_faddr_t)PHYS_SDRAM_2 + PHYS_SDRAM_2_SIZE;
358 for (mr = 0; mr < 64 && i < CONFIG_NR_DRAM_BANKS; mr++) {
359 err = get_owned_memreg(mr, &start, &end);
361 start = roundup(start, MEMSTART_ALIGNMENT);
362 if (start > end) /* Small memory region, no use it */
365 if (start >= PHYS_SDRAM_1 && start <= end1) {
366 gd->bd->bi_dram[i].start = start;
368 if ((end + 1) <= end1)
369 gd->bd->bi_dram[i].size =
372 gd->bd->bi_dram[i].size = end1 - start;
376 } else if (start >= PHYS_SDRAM_2 && start <= end2) {
377 gd->bd->bi_dram[i].start = start;
379 if ((end + 1) <= end2)
380 gd->bd->bi_dram[i].size =
383 gd->bd->bi_dram[i].size = end2 - start;
391 /* If error, set to the default value */
393 gd->bd->bi_dram[0].start = PHYS_SDRAM_1;
394 gd->bd->bi_dram[0].size = PHYS_SDRAM_1_SIZE;
395 gd->bd->bi_dram[1].start = PHYS_SDRAM_2;
396 gd->bd->bi_dram[1].size = PHYS_SDRAM_2_SIZE;
402 static u64 get_block_attrs(sc_faddr_t addr_start)
404 u64 attr = PTE_BLOCK_MEMTYPE(MT_DEVICE_NGNRNE) | PTE_BLOCK_NON_SHARE |
405 PTE_BLOCK_PXN | PTE_BLOCK_UXN;
407 if ((addr_start >= PHYS_SDRAM_1 &&
408 addr_start <= ((sc_faddr_t)PHYS_SDRAM_1 + PHYS_SDRAM_1_SIZE)) ||
409 (addr_start >= PHYS_SDRAM_2 &&
410 addr_start <= ((sc_faddr_t)PHYS_SDRAM_2 + PHYS_SDRAM_2_SIZE)))
411 return (PTE_BLOCK_MEMTYPE(MT_NORMAL) | PTE_BLOCK_OUTER_SHARE);
416 static u64 get_block_size(sc_faddr_t addr_start, sc_faddr_t addr_end)
418 sc_faddr_t end1, end2;
420 end1 = (sc_faddr_t)PHYS_SDRAM_1 + PHYS_SDRAM_1_SIZE;
421 end2 = (sc_faddr_t)PHYS_SDRAM_2 + PHYS_SDRAM_2_SIZE;
423 if (addr_start >= PHYS_SDRAM_1 && addr_start <= end1) {
424 if ((addr_end + 1) > end1)
425 return end1 - addr_start;
426 } else if (addr_start >= PHYS_SDRAM_2 && addr_start <= end2) {
427 if ((addr_end + 1) > end2)
428 return end2 - addr_start;
431 return (addr_end - addr_start + 1);
434 #define MAX_PTE_ENTRIES 512
435 #define MAX_MEM_MAP_REGIONS 16
437 static struct mm_region imx8_mem_map[MAX_MEM_MAP_REGIONS];
438 struct mm_region *mem_map = imx8_mem_map;
440 void enable_caches(void)
443 sc_faddr_t start, end;
446 /* Create map for registers access from 0x1c000000 to 0x80000000*/
447 imx8_mem_map[0].virt = 0x1c000000UL;
448 imx8_mem_map[0].phys = 0x1c000000UL;
449 imx8_mem_map[0].size = 0x64000000UL;
450 imx8_mem_map[0].attrs = PTE_BLOCK_MEMTYPE(MT_DEVICE_NGNRNE) |
451 PTE_BLOCK_NON_SHARE | PTE_BLOCK_PXN | PTE_BLOCK_UXN;
454 for (mr = 0; mr < 64 && i < MAX_MEM_MAP_REGIONS; mr++) {
455 err = get_owned_memreg(mr, &start, &end);
457 imx8_mem_map[i].virt = start;
458 imx8_mem_map[i].phys = start;
459 imx8_mem_map[i].size = get_block_size(start, end);
460 imx8_mem_map[i].attrs = get_block_attrs(start);
465 if (i < MAX_MEM_MAP_REGIONS) {
466 imx8_mem_map[i].size = 0;
467 imx8_mem_map[i].attrs = 0;
469 puts("Error, need more MEM MAP REGIONS reserved\n");
474 for (i = 0; i < MAX_MEM_MAP_REGIONS; i++) {
475 debug("[%d] vir = 0x%llx phys = 0x%llx size = 0x%llx attrs = 0x%llx\n",
476 i, imx8_mem_map[i].virt, imx8_mem_map[i].phys,
477 imx8_mem_map[i].size, imx8_mem_map[i].attrs);
484 #if !CONFIG_IS_ENABLED(SYS_DCACHE_OFF)
485 u64 get_page_table_size(void)
487 u64 one_pt = MAX_PTE_ENTRIES * sizeof(u64);
491 * For each memory region, the max table size:
492 * 2 level 3 tables + 2 level 2 tables + 1 level 1 table
494 size = (2 + 2 + 1) * one_pt * MAX_MEM_MAP_REGIONS + one_pt;
497 * We need to duplicate our page table once to have an emergency pt to
498 * resort to when splitting page tables later on
503 * We may need to split page tables later on if dcache settings change,
504 * so reserve up to 4 (random pick) page tables for that.
512 #if defined(CONFIG_IMX8QM)
513 #define FUSE_MAC0_WORD0 452
514 #define FUSE_MAC0_WORD1 453
515 #define FUSE_MAC1_WORD0 454
516 #define FUSE_MAC1_WORD1 455
517 #elif defined(CONFIG_IMX8QXP)
518 #define FUSE_MAC0_WORD0 708
519 #define FUSE_MAC0_WORD1 709
520 #define FUSE_MAC1_WORD0 710
521 #define FUSE_MAC1_WORD1 711
524 void imx_get_mac_from_fuse(int dev_id, unsigned char *mac)
526 u32 word[2], val[2] = {};
530 word[0] = FUSE_MAC0_WORD0;
531 word[1] = FUSE_MAC0_WORD1;
533 word[0] = FUSE_MAC1_WORD0;
534 word[1] = FUSE_MAC1_WORD1;
537 for (i = 0; i < 2; i++) {
538 ret = sc_misc_otp_fuse_read(-1, word[i], &val[i]);
544 mac[1] = val[0] >> 8;
545 mac[2] = val[0] >> 16;
546 mac[3] = val[0] >> 24;
548 mac[5] = val[1] >> 8;
550 debug("%s: MAC%d: %02x.%02x.%02x.%02x.%02x.%02x\n",
551 __func__, dev_id, mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
554 printf("%s: fuse %d, err: %d\n", __func__, word[i], ret);
557 u32 get_cpu_rev(void)
562 ret = sc_misc_get_control(-1, SC_R_SYSTEM, SC_C_ID, &id);
566 rev = (id >> 5) & 0xf;
567 id = (id & 0x1f) + MXC_SOC_IMX8; /* Dummy ID for chip */
569 return (id << 12) | rev;