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/mach-imx/boot_mode.h>
24 DECLARE_GLOBAL_DATA_PTR;
26 #define BT_PASSOVER_TAG 0x504F
27 struct pass_over_info_t *get_pass_over_info(void)
29 struct pass_over_info_t *p =
30 (struct pass_over_info_t *)PASS_OVER_INFO_ADDR;
32 if (p->barker != BT_PASSOVER_TAG ||
33 p->len != sizeof(struct pass_over_info_t))
39 int arch_cpu_init(void)
41 #ifdef CONFIG_SPL_BUILD
42 struct pass_over_info_t *pass_over;
44 if (is_soc_rev(CHIP_REV_A)) {
45 pass_over = get_pass_over_info();
46 if (pass_over && pass_over->g_ap_mu == 0) {
48 * When ap_mu is 0, means the U-Boot booted
49 * from first container
51 sc_misc_boot_status(-1, SC_MISC_BOOT_STATUS_SUCCESS);
59 int arch_cpu_init_dm(void)
64 node = fdt_node_offset_by_compatible(gd->fdt_blob, -1, "fsl,imx8-mu");
66 ret = uclass_get_device_by_of_offset(UCLASS_MISC, node, &devp);
68 printf("could not get scu %d\n", ret);
73 ret = sc_pm_set_resource_power_mode(-1, SC_R_SMMU,
82 int print_bootinfo(void)
84 enum boot_device bt_dev = get_boot_device();
119 printf("Unknown device %u\n", bt_dev);
126 enum boot_device get_boot_device(void)
128 enum boot_device boot_dev = SD1_BOOT;
132 sc_misc_get_boot_dev(-1, &dev_rsrc);
136 boot_dev = MMC1_BOOT;
145 boot_dev = NAND_BOOT;
148 boot_dev = FLEXSPI_BOOT;
151 boot_dev = SATA_BOOT;
165 #ifdef CONFIG_ENV_IS_IN_MMC
166 __weak int board_mmc_get_env_dev(int devno)
168 return CONFIG_SYS_MMC_ENV_DEV;
171 int mmc_get_env_dev(void)
176 sc_misc_get_boot_dev(-1, &dev_rsrc);
189 /* If not boot from sd/mmc, use default value */
190 return CONFIG_SYS_MMC_ENV_DEV;
193 return board_mmc_get_env_dev(devno);
197 #define MEMSTART_ALIGNMENT SZ_2M /* Align the memory start with 2MB */
199 static int get_owned_memreg(sc_rm_mr_t mr, sc_faddr_t *addr_start,
200 sc_faddr_t *addr_end)
202 sc_faddr_t start, end;
206 owned = sc_rm_is_memreg_owned(-1, mr);
208 ret = sc_rm_get_memreg_info(-1, mr, &start, &end);
210 printf("Memreg get info failed, %d\n", ret);
213 debug("0x%llx -- 0x%llx\n", start, end);
223 phys_size_t get_effective_memsize(void)
226 sc_faddr_t start, end, end1;
229 end1 = (sc_faddr_t)PHYS_SDRAM_1 + PHYS_SDRAM_1_SIZE;
231 for (mr = 0; mr < 64; mr++) {
232 err = get_owned_memreg(mr, &start, &end);
234 start = roundup(start, MEMSTART_ALIGNMENT);
235 /* Too small memory region, not use it */
239 /* Find the memory region runs the U-Boot */
240 if (start >= PHYS_SDRAM_1 && start <= end1 &&
241 (start <= CONFIG_SYS_TEXT_BASE &&
242 end >= CONFIG_SYS_TEXT_BASE)) {
243 if ((end + 1) <= ((sc_faddr_t)PHYS_SDRAM_1 +
245 return (end - PHYS_SDRAM_1 + 1);
247 return PHYS_SDRAM_1_SIZE;
252 return PHYS_SDRAM_1_SIZE;
258 sc_faddr_t start, end, end1, end2;
261 end1 = (sc_faddr_t)PHYS_SDRAM_1 + PHYS_SDRAM_1_SIZE;
262 end2 = (sc_faddr_t)PHYS_SDRAM_2 + PHYS_SDRAM_2_SIZE;
263 for (mr = 0; mr < 64; mr++) {
264 err = get_owned_memreg(mr, &start, &end);
266 start = roundup(start, MEMSTART_ALIGNMENT);
267 /* Too small memory region, not use it */
271 if (start >= PHYS_SDRAM_1 && start <= end1) {
272 if ((end + 1) <= end1)
273 gd->ram_size += end - start + 1;
275 gd->ram_size += end1 - start;
276 } else if (start >= PHYS_SDRAM_2 && start <= end2) {
277 if ((end + 1) <= end2)
278 gd->ram_size += end - start + 1;
280 gd->ram_size += end2 - start;
285 /* If error, set to the default value */
287 gd->ram_size = PHYS_SDRAM_1_SIZE;
288 gd->ram_size += PHYS_SDRAM_2_SIZE;
293 static void dram_bank_sort(int current_bank)
298 while (current_bank > 0) {
299 if (gd->bd->bi_dram[current_bank - 1].start >
300 gd->bd->bi_dram[current_bank].start) {
301 start = gd->bd->bi_dram[current_bank - 1].start;
302 size = gd->bd->bi_dram[current_bank - 1].size;
304 gd->bd->bi_dram[current_bank - 1].start =
305 gd->bd->bi_dram[current_bank].start;
306 gd->bd->bi_dram[current_bank - 1].size =
307 gd->bd->bi_dram[current_bank].size;
309 gd->bd->bi_dram[current_bank].start = start;
310 gd->bd->bi_dram[current_bank].size = size;
316 int dram_init_banksize(void)
319 sc_faddr_t start, end, end1, end2;
323 end1 = (sc_faddr_t)PHYS_SDRAM_1 + PHYS_SDRAM_1_SIZE;
324 end2 = (sc_faddr_t)PHYS_SDRAM_2 + PHYS_SDRAM_2_SIZE;
326 for (mr = 0; mr < 64 && i < CONFIG_NR_DRAM_BANKS; mr++) {
327 err = get_owned_memreg(mr, &start, &end);
329 start = roundup(start, MEMSTART_ALIGNMENT);
330 if (start > end) /* Small memory region, no use it */
333 if (start >= PHYS_SDRAM_1 && start <= end1) {
334 gd->bd->bi_dram[i].start = start;
336 if ((end + 1) <= end1)
337 gd->bd->bi_dram[i].size =
340 gd->bd->bi_dram[i].size = end1 - start;
344 } else if (start >= PHYS_SDRAM_2 && start <= end2) {
345 gd->bd->bi_dram[i].start = start;
347 if ((end + 1) <= end2)
348 gd->bd->bi_dram[i].size =
351 gd->bd->bi_dram[i].size = end2 - start;
359 /* If error, set to the default value */
361 gd->bd->bi_dram[0].start = PHYS_SDRAM_1;
362 gd->bd->bi_dram[0].size = PHYS_SDRAM_1_SIZE;
363 gd->bd->bi_dram[1].start = PHYS_SDRAM_2;
364 gd->bd->bi_dram[1].size = PHYS_SDRAM_2_SIZE;
370 static u64 get_block_attrs(sc_faddr_t addr_start)
372 u64 attr = PTE_BLOCK_MEMTYPE(MT_DEVICE_NGNRNE) | PTE_BLOCK_NON_SHARE |
373 PTE_BLOCK_PXN | PTE_BLOCK_UXN;
375 if ((addr_start >= PHYS_SDRAM_1 &&
376 addr_start <= ((sc_faddr_t)PHYS_SDRAM_1 + PHYS_SDRAM_1_SIZE)) ||
377 (addr_start >= PHYS_SDRAM_2 &&
378 addr_start <= ((sc_faddr_t)PHYS_SDRAM_2 + PHYS_SDRAM_2_SIZE)))
379 return (PTE_BLOCK_MEMTYPE(MT_NORMAL) | PTE_BLOCK_OUTER_SHARE);
384 static u64 get_block_size(sc_faddr_t addr_start, sc_faddr_t addr_end)
386 sc_faddr_t end1, end2;
388 end1 = (sc_faddr_t)PHYS_SDRAM_1 + PHYS_SDRAM_1_SIZE;
389 end2 = (sc_faddr_t)PHYS_SDRAM_2 + PHYS_SDRAM_2_SIZE;
391 if (addr_start >= PHYS_SDRAM_1 && addr_start <= end1) {
392 if ((addr_end + 1) > end1)
393 return end1 - addr_start;
394 } else if (addr_start >= PHYS_SDRAM_2 && addr_start <= end2) {
395 if ((addr_end + 1) > end2)
396 return end2 - addr_start;
399 return (addr_end - addr_start + 1);
402 #define MAX_PTE_ENTRIES 512
403 #define MAX_MEM_MAP_REGIONS 16
405 static struct mm_region imx8_mem_map[MAX_MEM_MAP_REGIONS];
406 struct mm_region *mem_map = imx8_mem_map;
408 void enable_caches(void)
411 sc_faddr_t start, end;
414 /* Create map for registers access from 0x1c000000 to 0x80000000*/
415 imx8_mem_map[0].virt = 0x1c000000UL;
416 imx8_mem_map[0].phys = 0x1c000000UL;
417 imx8_mem_map[0].size = 0x64000000UL;
418 imx8_mem_map[0].attrs = PTE_BLOCK_MEMTYPE(MT_DEVICE_NGNRNE) |
419 PTE_BLOCK_NON_SHARE | PTE_BLOCK_PXN | PTE_BLOCK_UXN;
422 for (mr = 0; mr < 64 && i < MAX_MEM_MAP_REGIONS; mr++) {
423 err = get_owned_memreg(mr, &start, &end);
425 imx8_mem_map[i].virt = start;
426 imx8_mem_map[i].phys = start;
427 imx8_mem_map[i].size = get_block_size(start, end);
428 imx8_mem_map[i].attrs = get_block_attrs(start);
433 if (i < MAX_MEM_MAP_REGIONS) {
434 imx8_mem_map[i].size = 0;
435 imx8_mem_map[i].attrs = 0;
437 puts("Error, need more MEM MAP REGIONS reserved\n");
442 for (i = 0; i < MAX_MEM_MAP_REGIONS; i++) {
443 debug("[%d] vir = 0x%llx phys = 0x%llx size = 0x%llx attrs = 0x%llx\n",
444 i, imx8_mem_map[i].virt, imx8_mem_map[i].phys,
445 imx8_mem_map[i].size, imx8_mem_map[i].attrs);
452 #if !CONFIG_IS_ENABLED(SYS_DCACHE_OFF)
453 u64 get_page_table_size(void)
455 u64 one_pt = MAX_PTE_ENTRIES * sizeof(u64);
459 * For each memory region, the max table size:
460 * 2 level 3 tables + 2 level 2 tables + 1 level 1 table
462 size = (2 + 2 + 1) * one_pt * MAX_MEM_MAP_REGIONS + one_pt;
465 * We need to duplicate our page table once to have an emergency pt to
466 * resort to when splitting page tables later on
471 * We may need to split page tables later on if dcache settings change,
472 * so reserve up to 4 (random pick) page tables for that.
480 #if defined(CONFIG_IMX8QM)
481 #define FUSE_MAC0_WORD0 452
482 #define FUSE_MAC0_WORD1 453
483 #define FUSE_MAC1_WORD0 454
484 #define FUSE_MAC1_WORD1 455
485 #elif defined(CONFIG_IMX8QXP)
486 #define FUSE_MAC0_WORD0 708
487 #define FUSE_MAC0_WORD1 709
488 #define FUSE_MAC1_WORD0 710
489 #define FUSE_MAC1_WORD1 711
492 void imx_get_mac_from_fuse(int dev_id, unsigned char *mac)
494 u32 word[2], val[2] = {};
498 word[0] = FUSE_MAC0_WORD0;
499 word[1] = FUSE_MAC0_WORD1;
501 word[0] = FUSE_MAC1_WORD0;
502 word[1] = FUSE_MAC1_WORD1;
505 for (i = 0; i < 2; i++) {
506 ret = sc_misc_otp_fuse_read(-1, word[i], &val[i]);
512 mac[1] = val[0] >> 8;
513 mac[2] = val[0] >> 16;
514 mac[3] = val[0] >> 24;
516 mac[5] = val[1] >> 8;
518 debug("%s: MAC%d: %02x.%02x.%02x.%02x.%02x.%02x\n",
519 __func__, dev_id, mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
522 printf("%s: fuse %d, err: %d\n", __func__, word[i], ret);
525 u32 get_cpu_rev(void)
530 ret = sc_misc_get_control(-1, SC_R_SYSTEM, SC_C_ID, &id);
534 rev = (id >> 5) & 0xf;
535 id = (id & 0x1f) + MXC_SOC_IMX8; /* Dummy ID for chip */
537 return (id << 12) | rev;