Peng Fan | 331d40d | 2021-08-07 16:00:31 +0800 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
| 2 | /* |
| 3 | * Copyright 2021 NXP |
| 4 | */ |
| 5 | |
Peng Fan | 77c3b9c | 2021-08-07 16:00:33 +0800 | [diff] [blame] | 6 | #include <asm/io.h> |
| 7 | #include <asm/arch/clock.h> |
| 8 | #include <asm/arch/imx-regs.h> |
Peng Fan | 331d40d | 2021-08-07 16:00:31 +0800 | [diff] [blame] | 9 | #include <asm/arch/sys_proto.h> |
Peng Fan | 9ef89ea | 2021-08-07 16:00:35 +0800 | [diff] [blame] | 10 | #include <asm/armv8/mmu.h> |
Peng Fan | 77c3b9c | 2021-08-07 16:00:33 +0800 | [diff] [blame] | 11 | #include <asm/mach-imx/boot_mode.h> |
Ye Li | 610083e | 2021-08-07 16:00:48 +0800 | [diff] [blame^] | 12 | #include <efi_loader.h> |
| 13 | #include <spl.h> |
Peng Fan | 331d40d | 2021-08-07 16:00:31 +0800 | [diff] [blame] | 14 | |
Peng Fan | 9ef89ea | 2021-08-07 16:00:35 +0800 | [diff] [blame] | 15 | DECLARE_GLOBAL_DATA_PTR; |
| 16 | |
Ye Li | 6f3858d | 2021-08-07 16:00:39 +0800 | [diff] [blame] | 17 | struct rom_api *g_rom_api = (struct rom_api *)0x1980; |
| 18 | |
Peng Fan | 331d40d | 2021-08-07 16:00:31 +0800 | [diff] [blame] | 19 | u32 get_cpu_rev(void) |
| 20 | { |
| 21 | return (MXC_CPU_IMX8ULP << 12) | CHIP_REV_1_0; |
| 22 | } |
Peng Fan | 77c3b9c | 2021-08-07 16:00:33 +0800 | [diff] [blame] | 23 | |
| 24 | enum bt_mode get_boot_mode(void) |
| 25 | { |
| 26 | u32 bt0_cfg = 0; |
| 27 | |
Ye Li | 981f040 | 2021-08-07 16:00:47 +0800 | [diff] [blame] | 28 | bt0_cfg = readl(CMC1_BASE_ADDR + 0xa0); |
Peng Fan | 77c3b9c | 2021-08-07 16:00:33 +0800 | [diff] [blame] | 29 | bt0_cfg &= (BT0CFG_LPBOOT_MASK | BT0CFG_DUALBOOT_MASK); |
| 30 | |
| 31 | if (!(bt0_cfg & BT0CFG_LPBOOT_MASK)) { |
| 32 | /* No low power boot */ |
| 33 | if (bt0_cfg & BT0CFG_DUALBOOT_MASK) |
| 34 | return DUAL_BOOT; |
| 35 | else |
| 36 | return SINGLE_BOOT; |
| 37 | } |
| 38 | |
| 39 | return LOW_POWER_BOOT; |
| 40 | } |
| 41 | |
Peng Fan | c17f593 | 2021-08-07 16:00:34 +0800 | [diff] [blame] | 42 | #define CMC_SRS_TAMPER BIT(31) |
| 43 | #define CMC_SRS_SECURITY BIT(30) |
| 44 | #define CMC_SRS_TZWDG BIT(29) |
| 45 | #define CMC_SRS_JTAG_RST BIT(28) |
| 46 | #define CMC_SRS_CORE1 BIT(16) |
| 47 | #define CMC_SRS_LOCKUP BIT(15) |
| 48 | #define CMC_SRS_SW BIT(14) |
| 49 | #define CMC_SRS_WDG BIT(13) |
| 50 | #define CMC_SRS_PIN_RESET BIT(8) |
| 51 | #define CMC_SRS_WARM BIT(4) |
| 52 | #define CMC_SRS_HVD BIT(3) |
| 53 | #define CMC_SRS_LVD BIT(2) |
| 54 | #define CMC_SRS_POR BIT(1) |
| 55 | #define CMC_SRS_WUP BIT(0) |
| 56 | |
| 57 | static u32 reset_cause = -1; |
| 58 | |
| 59 | static char *get_reset_cause(char *ret) |
| 60 | { |
| 61 | u32 cause1, cause = 0, srs = 0; |
Peng Fan | 9ef89ea | 2021-08-07 16:00:35 +0800 | [diff] [blame] | 62 | void __iomem *reg_ssrs = (void __iomem *)(CMC1_BASE_ADDR + 0x88); |
| 63 | void __iomem *reg_srs = (void __iomem *)(CMC1_BASE_ADDR + 0x80); |
Peng Fan | c17f593 | 2021-08-07 16:00:34 +0800 | [diff] [blame] | 64 | |
| 65 | if (!ret) |
| 66 | return "null"; |
| 67 | |
| 68 | srs = readl(reg_srs); |
| 69 | cause1 = readl(reg_ssrs); |
| 70 | |
| 71 | reset_cause = cause1; |
| 72 | |
| 73 | cause = cause1 & (CMC_SRS_POR | CMC_SRS_WUP | CMC_SRS_WARM); |
| 74 | |
| 75 | switch (cause) { |
| 76 | case CMC_SRS_POR: |
| 77 | sprintf(ret, "%s", "POR"); |
| 78 | break; |
| 79 | case CMC_SRS_WUP: |
| 80 | sprintf(ret, "%s", "WUP"); |
| 81 | break; |
| 82 | case CMC_SRS_WARM: |
| 83 | cause = cause1 & (CMC_SRS_WDG | CMC_SRS_SW | |
| 84 | CMC_SRS_JTAG_RST); |
| 85 | switch (cause) { |
| 86 | case CMC_SRS_WDG: |
| 87 | sprintf(ret, "%s", "WARM-WDG"); |
| 88 | break; |
| 89 | case CMC_SRS_SW: |
| 90 | sprintf(ret, "%s", "WARM-SW"); |
| 91 | break; |
| 92 | case CMC_SRS_JTAG_RST: |
| 93 | sprintf(ret, "%s", "WARM-JTAG"); |
| 94 | break; |
| 95 | default: |
| 96 | sprintf(ret, "%s", "WARM-UNKN"); |
| 97 | break; |
| 98 | } |
| 99 | break; |
| 100 | default: |
| 101 | sprintf(ret, "%s-%X", "UNKN", cause1); |
| 102 | break; |
| 103 | } |
| 104 | |
| 105 | debug("[%X] SRS[%X] %X - ", cause1, srs, srs ^ cause1); |
| 106 | return ret; |
| 107 | } |
| 108 | |
Peng Fan | 77c3b9c | 2021-08-07 16:00:33 +0800 | [diff] [blame] | 109 | #if defined(CONFIG_DISPLAY_CPUINFO) |
| 110 | const char *get_imx_type(u32 imxtype) |
| 111 | { |
| 112 | return "8ULP"; |
| 113 | } |
| 114 | |
| 115 | int print_cpuinfo(void) |
| 116 | { |
| 117 | u32 cpurev; |
| 118 | char cause[18]; |
| 119 | |
| 120 | cpurev = get_cpu_rev(); |
| 121 | |
| 122 | printf("CPU: Freescale i.MX%s rev%d.%d at %d MHz\n", |
| 123 | get_imx_type((cpurev & 0xFF000) >> 12), |
| 124 | (cpurev & 0x000F0) >> 4, (cpurev & 0x0000F) >> 0, |
| 125 | mxc_get_clock(MXC_ARM_CLK) / 1000000); |
| 126 | |
Peng Fan | c17f593 | 2021-08-07 16:00:34 +0800 | [diff] [blame] | 127 | printf("Reset cause: %s\n", get_reset_cause(cause)); |
| 128 | |
Peng Fan | 77c3b9c | 2021-08-07 16:00:33 +0800 | [diff] [blame] | 129 | printf("Boot mode: "); |
| 130 | switch (get_boot_mode()) { |
| 131 | case LOW_POWER_BOOT: |
| 132 | printf("Low power boot\n"); |
| 133 | break; |
| 134 | case DUAL_BOOT: |
| 135 | printf("Dual boot\n"); |
| 136 | break; |
| 137 | case SINGLE_BOOT: |
| 138 | default: |
| 139 | printf("Single boot\n"); |
| 140 | break; |
| 141 | } |
| 142 | |
| 143 | return 0; |
| 144 | } |
| 145 | #endif |
Peng Fan | 9ef89ea | 2021-08-07 16:00:35 +0800 | [diff] [blame] | 146 | |
| 147 | void init_wdog(void) |
| 148 | { |
| 149 | /* TODO */ |
| 150 | } |
| 151 | |
| 152 | static struct mm_region imx8ulp_arm64_mem_map[] = { |
| 153 | { |
| 154 | /* ROM */ |
| 155 | .virt = 0x0, |
| 156 | .phys = 0x0, |
| 157 | .size = 0x40000UL, |
| 158 | .attrs = PTE_BLOCK_MEMTYPE(MT_NORMAL) | |
| 159 | PTE_BLOCK_OUTER_SHARE |
| 160 | }, |
| 161 | { |
| 162 | /* FLEXSPI0 */ |
| 163 | .virt = 0x04000000, |
| 164 | .phys = 0x04000000, |
| 165 | .size = 0x08000000UL, |
| 166 | .attrs = PTE_BLOCK_MEMTYPE(MT_DEVICE_NGNRNE) | |
| 167 | PTE_BLOCK_NON_SHARE | |
| 168 | PTE_BLOCK_PXN | PTE_BLOCK_UXN |
| 169 | }, |
| 170 | { |
| 171 | /* SSRAM (align with 2M) */ |
| 172 | .virt = 0x1FE00000UL, |
| 173 | .phys = 0x1FE00000UL, |
| 174 | .size = 0x400000UL, |
| 175 | .attrs = PTE_BLOCK_MEMTYPE(MT_NORMAL) | |
| 176 | PTE_BLOCK_OUTER_SHARE | |
| 177 | PTE_BLOCK_PXN | PTE_BLOCK_UXN |
| 178 | }, { |
| 179 | /* SRAM1 (align with 2M) */ |
| 180 | .virt = 0x21000000UL, |
| 181 | .phys = 0x21000000UL, |
| 182 | .size = 0x200000UL, |
| 183 | .attrs = PTE_BLOCK_MEMTYPE(MT_NORMAL) | |
| 184 | PTE_BLOCK_OUTER_SHARE | |
| 185 | PTE_BLOCK_PXN | PTE_BLOCK_UXN |
| 186 | }, { |
| 187 | /* SRAM0 (align with 2M) */ |
| 188 | .virt = 0x22000000UL, |
| 189 | .phys = 0x22000000UL, |
| 190 | .size = 0x200000UL, |
| 191 | .attrs = PTE_BLOCK_MEMTYPE(MT_NORMAL) | |
| 192 | PTE_BLOCK_OUTER_SHARE | |
| 193 | PTE_BLOCK_PXN | PTE_BLOCK_UXN |
| 194 | }, { |
| 195 | /* Peripherals */ |
| 196 | .virt = 0x27000000UL, |
| 197 | .phys = 0x27000000UL, |
| 198 | .size = 0x3000000UL, |
| 199 | .attrs = PTE_BLOCK_MEMTYPE(MT_DEVICE_NGNRNE) | |
| 200 | PTE_BLOCK_NON_SHARE | |
| 201 | PTE_BLOCK_PXN | PTE_BLOCK_UXN |
| 202 | }, { |
| 203 | /* Peripherals */ |
| 204 | .virt = 0x2D000000UL, |
| 205 | .phys = 0x2D000000UL, |
| 206 | .size = 0x1600000UL, |
| 207 | .attrs = PTE_BLOCK_MEMTYPE(MT_DEVICE_NGNRNE) | |
| 208 | PTE_BLOCK_NON_SHARE | |
| 209 | PTE_BLOCK_PXN | PTE_BLOCK_UXN |
| 210 | }, { |
| 211 | /* FLEXSPI1-2 */ |
| 212 | .virt = 0x40000000UL, |
| 213 | .phys = 0x40000000UL, |
| 214 | .size = 0x40000000UL, |
| 215 | .attrs = PTE_BLOCK_MEMTYPE(MT_DEVICE_NGNRNE) | |
| 216 | PTE_BLOCK_NON_SHARE | |
| 217 | PTE_BLOCK_PXN | PTE_BLOCK_UXN |
| 218 | }, { |
| 219 | /* DRAM1 */ |
| 220 | .virt = 0x80000000UL, |
| 221 | .phys = 0x80000000UL, |
| 222 | .size = PHYS_SDRAM_SIZE, |
| 223 | .attrs = PTE_BLOCK_MEMTYPE(MT_NORMAL) | |
| 224 | PTE_BLOCK_OUTER_SHARE |
| 225 | }, { |
| 226 | /* |
| 227 | * empty entrie to split table entry 5 |
| 228 | * if needed when TEEs are used |
| 229 | */ |
| 230 | 0, |
| 231 | }, { |
| 232 | /* List terminator */ |
| 233 | 0, |
| 234 | } |
| 235 | }; |
| 236 | |
| 237 | struct mm_region *mem_map = imx8ulp_arm64_mem_map; |
| 238 | |
| 239 | /* simplify the page table size to enhance boot speed */ |
| 240 | #define MAX_PTE_ENTRIES 512 |
| 241 | #define MAX_MEM_MAP_REGIONS 16 |
| 242 | u64 get_page_table_size(void) |
| 243 | { |
| 244 | u64 one_pt = MAX_PTE_ENTRIES * sizeof(u64); |
| 245 | u64 size = 0; |
| 246 | |
| 247 | /* |
| 248 | * For each memory region, the max table size: |
| 249 | * 2 level 3 tables + 2 level 2 tables + 1 level 1 table |
| 250 | */ |
| 251 | size = (2 + 2 + 1) * one_pt * MAX_MEM_MAP_REGIONS + one_pt; |
| 252 | |
| 253 | /* |
| 254 | * We need to duplicate our page table once to have an emergency pt to |
| 255 | * resort to when splitting page tables later on |
| 256 | */ |
| 257 | size *= 2; |
| 258 | |
| 259 | /* |
| 260 | * We may need to split page tables later on if dcache settings change, |
| 261 | * so reserve up to 4 (random pick) page tables for that. |
| 262 | */ |
| 263 | size += one_pt * 4; |
| 264 | |
| 265 | return size; |
| 266 | } |
| 267 | |
| 268 | void enable_caches(void) |
| 269 | { |
| 270 | /* TODO: add TEE memmap region */ |
| 271 | |
| 272 | icache_enable(); |
| 273 | dcache_enable(); |
| 274 | } |
| 275 | |
| 276 | int dram_init(void) |
| 277 | { |
| 278 | gd->ram_size = PHYS_SDRAM_SIZE; |
| 279 | |
| 280 | return 0; |
| 281 | } |
| 282 | |
| 283 | #ifdef CONFIG_SERIAL_TAG |
| 284 | void get_board_serial(struct tag_serialnr *serialnr) |
| 285 | { |
| 286 | /* TODO */ |
| 287 | } |
| 288 | #endif |
| 289 | |
| 290 | int arch_cpu_init(void) |
| 291 | { |
Peng Fan | a84dab4 | 2021-08-07 16:00:45 +0800 | [diff] [blame] | 292 | if (IS_ENABLED(CONFIG_SPL_BUILD)) |
| 293 | clock_init(); |
| 294 | |
Peng Fan | 9ef89ea | 2021-08-07 16:00:35 +0800 | [diff] [blame] | 295 | return 0; |
| 296 | } |
Ye Li | 610083e | 2021-08-07 16:00:48 +0800 | [diff] [blame^] | 297 | |
| 298 | #if defined(CONFIG_SPL_BUILD) |
| 299 | __weak void __noreturn jump_to_image_no_args(struct spl_image_info *spl_image) |
| 300 | { |
| 301 | debug("image entry point: 0x%lx\n", spl_image->entry_point); |
| 302 | |
| 303 | /* Update SIM1 DGO8 for reset vector base */ |
| 304 | writel((u32)spl_image->entry_point, SIM1_BASE_ADDR + 0x5c); |
| 305 | |
| 306 | /* set update bit */ |
| 307 | setbits_le32(SIM1_BASE_ADDR + 0x8, 0x1 << 24); |
| 308 | |
| 309 | /* polling the ack */ |
| 310 | while ((readl(SIM1_BASE_ADDR + 0x8) & (0x1 << 26)) == 0) |
| 311 | ; |
| 312 | |
| 313 | /* clear the update */ |
| 314 | clrbits_le32(SIM1_BASE_ADDR + 0x8, (0x1 << 24)); |
| 315 | |
| 316 | /* clear the ack by set 1 */ |
| 317 | setbits_le32(SIM1_BASE_ADDR + 0x8, (0x1 << 26)); |
| 318 | |
| 319 | /* Enable the 512KB cache */ |
| 320 | setbits_le32(SIM1_BASE_ADDR + 0x30, (0x1 << 4)); |
| 321 | |
| 322 | /* reset core */ |
| 323 | setbits_le32(SIM1_BASE_ADDR + 0x30, (0x1 << 16)); |
| 324 | |
| 325 | while (1) |
| 326 | ; |
| 327 | } |
| 328 | #endif |