1 // SPDX-License-Identifier: GPL-2.0+
9 #include <dm/device-internal.h>
11 #include <dm/uclass.h>
13 #include <asm/arch/sci/sci.h>
14 #include <asm/arch/sys_proto.h>
15 #include <asm/arch-imx/cpu.h>
16 #include <asm/armv8/cpu.h>
17 #include <asm/armv8/mmu.h>
18 #include <asm/mach-imx/boot_mode.h>
20 DECLARE_GLOBAL_DATA_PTR;
27 ret = sc_misc_get_control(-1, SC_R_SYSTEM, SC_C_ID, &id);
31 rev = (id >> 5) & 0xf;
32 id = (id & 0x1f) + MXC_SOC_IMX8; /* Dummy ID for chip */
34 return (id << 12) | rev;
37 #ifdef CONFIG_DISPLAY_CPUINFO
38 const char *get_imx8_type(u32 imxtype)
48 const char *get_imx8_rev(u32 rev)
60 const char *get_core_name(void)
68 int print_cpuinfo(void)
74 ret = uclass_get_device(UCLASS_CPU, 0, &dev);
78 ret = clk_get_by_index(dev, 0, &cpu_clk);
80 dev_err(dev, "failed to clk\n");
86 cpurev = get_cpu_rev();
88 printf("CPU: Freescale i.MX%s rev%s %s at %ld MHz\n",
89 get_imx8_type((cpurev & 0xFF000) >> 12),
90 get_imx8_rev((cpurev & 0xFFF)),
92 clk_get_rate(&cpu_clk) / 1000000);
98 int print_bootinfo(void)
100 enum boot_device bt_dev = get_boot_device();
135 printf("Unknown device %u\n", bt_dev);
142 enum boot_device get_boot_device(void)
144 enum boot_device boot_dev = SD1_BOOT;
148 sc_misc_get_boot_dev(-1, &dev_rsrc);
152 boot_dev = MMC1_BOOT;
161 boot_dev = NAND_BOOT;
164 boot_dev = FLEXSPI_BOOT;
167 boot_dev = SATA_BOOT;
181 #ifdef CONFIG_ENV_IS_IN_MMC
182 __weak int board_mmc_get_env_dev(int devno)
184 return CONFIG_SYS_MMC_ENV_DEV;
187 int mmc_get_env_dev(void)
192 sc_misc_get_boot_dev(-1, &dev_rsrc);
205 /* If not boot from sd/mmc, use default value */
206 return CONFIG_SYS_MMC_ENV_DEV;
209 return board_mmc_get_env_dev(devno);
213 #define MEMSTART_ALIGNMENT SZ_2M /* Align the memory start with 2MB */
215 static int get_owned_memreg(sc_rm_mr_t mr, sc_faddr_t *addr_start,
216 sc_faddr_t *addr_end)
218 sc_faddr_t start, end;
222 owned = sc_rm_is_memreg_owned(-1, mr);
224 ret = sc_rm_get_memreg_info(-1, mr, &start, &end);
226 printf("Memreg get info failed, %d\n", ret);
229 debug("0x%llx -- 0x%llx\n", start, end);
239 phys_size_t get_effective_memsize(void)
242 sc_faddr_t start, end, end1;
245 end1 = (sc_faddr_t)PHYS_SDRAM_1 + PHYS_SDRAM_1_SIZE;
247 for (mr = 0; mr < 64; mr++) {
248 err = get_owned_memreg(mr, &start, &end);
250 start = roundup(start, MEMSTART_ALIGNMENT);
251 /* Too small memory region, not use it */
255 /* Find the memory region runs the u-boot */
256 if (start >= PHYS_SDRAM_1 && start <= end1 &&
257 (start <= CONFIG_SYS_TEXT_BASE &&
258 end >= CONFIG_SYS_TEXT_BASE)) {
259 if ((end + 1) <= ((sc_faddr_t)PHYS_SDRAM_1 +
261 return (end - PHYS_SDRAM_1 + 1);
263 return PHYS_SDRAM_1_SIZE;
268 return PHYS_SDRAM_1_SIZE;
274 sc_faddr_t start, end, end1, end2;
277 end1 = (sc_faddr_t)PHYS_SDRAM_1 + PHYS_SDRAM_1_SIZE;
278 end2 = (sc_faddr_t)PHYS_SDRAM_2 + PHYS_SDRAM_2_SIZE;
279 for (mr = 0; mr < 64; mr++) {
280 err = get_owned_memreg(mr, &start, &end);
282 start = roundup(start, MEMSTART_ALIGNMENT);
283 /* Too small memory region, not use it */
287 if (start >= PHYS_SDRAM_1 && start <= end1) {
288 if ((end + 1) <= end1)
289 gd->ram_size += end - start + 1;
291 gd->ram_size += end1 - start;
292 } else if (start >= PHYS_SDRAM_2 && start <= end2) {
293 if ((end + 1) <= end2)
294 gd->ram_size += end - start + 1;
296 gd->ram_size += end2 - start;
301 /* If error, set to the default value */
303 gd->ram_size = PHYS_SDRAM_1_SIZE;
304 gd->ram_size += PHYS_SDRAM_2_SIZE;
309 static void dram_bank_sort(int current_bank)
314 while (current_bank > 0) {
315 if (gd->bd->bi_dram[current_bank - 1].start >
316 gd->bd->bi_dram[current_bank].start) {
317 start = gd->bd->bi_dram[current_bank - 1].start;
318 size = gd->bd->bi_dram[current_bank - 1].size;
320 gd->bd->bi_dram[current_bank - 1].start =
321 gd->bd->bi_dram[current_bank].start;
322 gd->bd->bi_dram[current_bank - 1].size =
323 gd->bd->bi_dram[current_bank].size;
325 gd->bd->bi_dram[current_bank].start = start;
326 gd->bd->bi_dram[current_bank].size = size;
332 int dram_init_banksize(void)
335 sc_faddr_t start, end, end1, end2;
339 end1 = (sc_faddr_t)PHYS_SDRAM_1 + PHYS_SDRAM_1_SIZE;
340 end2 = (sc_faddr_t)PHYS_SDRAM_2 + PHYS_SDRAM_2_SIZE;
342 for (mr = 0; mr < 64 && i < CONFIG_NR_DRAM_BANKS; mr++) {
343 err = get_owned_memreg(mr, &start, &end);
345 start = roundup(start, MEMSTART_ALIGNMENT);
346 if (start > end) /* Small memory region, no use it */
349 if (start >= PHYS_SDRAM_1 && start <= end1) {
350 gd->bd->bi_dram[i].start = start;
352 if ((end + 1) <= end1)
353 gd->bd->bi_dram[i].size =
356 gd->bd->bi_dram[i].size = end1 - start;
360 } else if (start >= PHYS_SDRAM_2 && start <= end2) {
361 gd->bd->bi_dram[i].start = start;
363 if ((end + 1) <= end2)
364 gd->bd->bi_dram[i].size =
367 gd->bd->bi_dram[i].size = end2 - start;
375 /* If error, set to the default value */
377 gd->bd->bi_dram[0].start = PHYS_SDRAM_1;
378 gd->bd->bi_dram[0].size = PHYS_SDRAM_1_SIZE;
379 gd->bd->bi_dram[1].start = PHYS_SDRAM_2;
380 gd->bd->bi_dram[1].size = PHYS_SDRAM_2_SIZE;
386 static u64 get_block_attrs(sc_faddr_t addr_start)
388 u64 attr = PTE_BLOCK_MEMTYPE(MT_DEVICE_NGNRNE) | PTE_BLOCK_NON_SHARE |
389 PTE_BLOCK_PXN | PTE_BLOCK_UXN;
391 if ((addr_start >= PHYS_SDRAM_1 &&
392 addr_start <= ((sc_faddr_t)PHYS_SDRAM_1 + PHYS_SDRAM_1_SIZE)) ||
393 (addr_start >= PHYS_SDRAM_2 &&
394 addr_start <= ((sc_faddr_t)PHYS_SDRAM_2 + PHYS_SDRAM_2_SIZE)))
395 return (PTE_BLOCK_MEMTYPE(MT_NORMAL) | PTE_BLOCK_OUTER_SHARE);
400 static u64 get_block_size(sc_faddr_t addr_start, sc_faddr_t addr_end)
402 sc_faddr_t end1, end2;
404 end1 = (sc_faddr_t)PHYS_SDRAM_1 + PHYS_SDRAM_1_SIZE;
405 end2 = (sc_faddr_t)PHYS_SDRAM_2 + PHYS_SDRAM_2_SIZE;
407 if (addr_start >= PHYS_SDRAM_1 && addr_start <= end1) {
408 if ((addr_end + 1) > end1)
409 return end1 - addr_start;
410 } else if (addr_start >= PHYS_SDRAM_2 && addr_start <= end2) {
411 if ((addr_end + 1) > end2)
412 return end2 - addr_start;
415 return (addr_end - addr_start + 1);
418 #define MAX_PTE_ENTRIES 512
419 #define MAX_MEM_MAP_REGIONS 16
421 static struct mm_region imx8_mem_map[MAX_MEM_MAP_REGIONS];
422 struct mm_region *mem_map = imx8_mem_map;
424 void enable_caches(void)
427 sc_faddr_t start, end;
430 /* Create map for registers access from 0x1c000000 to 0x80000000*/
431 imx8_mem_map[0].virt = 0x1c000000UL;
432 imx8_mem_map[0].phys = 0x1c000000UL;
433 imx8_mem_map[0].size = 0x64000000UL;
434 imx8_mem_map[0].attrs = PTE_BLOCK_MEMTYPE(MT_DEVICE_NGNRNE) |
435 PTE_BLOCK_NON_SHARE | PTE_BLOCK_PXN | PTE_BLOCK_UXN;
438 for (mr = 0; mr < 64 && i < MAX_MEM_MAP_REGIONS; mr++) {
439 err = get_owned_memreg(mr, &start, &end);
441 imx8_mem_map[i].virt = start;
442 imx8_mem_map[i].phys = start;
443 imx8_mem_map[i].size = get_block_size(start, end);
444 imx8_mem_map[i].attrs = get_block_attrs(start);
449 if (i < MAX_MEM_MAP_REGIONS) {
450 imx8_mem_map[i].size = 0;
451 imx8_mem_map[i].attrs = 0;
453 puts("Error, need more MEM MAP REGIONS reserved\n");
458 for (i = 0; i < MAX_MEM_MAP_REGIONS; i++) {
459 debug("[%d] vir = 0x%llx phys = 0x%llx size = 0x%llx attrs = 0x%llx\n",
460 i, imx8_mem_map[i].virt, imx8_mem_map[i].phys,
461 imx8_mem_map[i].size, imx8_mem_map[i].attrs);
468 #ifndef CONFIG_SYS_DCACHE_OFF
469 u64 get_page_table_size(void)
471 u64 one_pt = MAX_PTE_ENTRIES * sizeof(u64);
475 * For each memory region, the max table size:
476 * 2 level 3 tables + 2 level 2 tables + 1 level 1 table
478 size = (2 + 2 + 1) * one_pt * MAX_MEM_MAP_REGIONS + one_pt;
481 * We need to duplicate our page table once to have an emergency pt to
482 * resort to when splitting page tables later on
487 * We may need to split page tables later on if dcache settings change,
488 * so reserve up to 4 (random pick) page tables for that.