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 | |
Peng Fan | 3a01f72 | 2021-08-07 16:00:49 +0800 | [diff] [blame] | 147 | #define UNLOCK_WORD0 0xC520 /* 1st unlock word */ |
| 148 | #define UNLOCK_WORD1 0xD928 /* 2nd unlock word */ |
| 149 | #define REFRESH_WORD0 0xA602 /* 1st refresh word */ |
| 150 | #define REFRESH_WORD1 0xB480 /* 2nd refresh word */ |
| 151 | |
| 152 | static void disable_wdog(void __iomem *wdog_base) |
| 153 | { |
| 154 | u32 val_cs = readl(wdog_base + 0x00); |
| 155 | |
| 156 | if (!(val_cs & 0x80)) |
| 157 | return; |
| 158 | |
| 159 | dmb(); |
| 160 | __raw_writel(REFRESH_WORD0, (wdog_base + 0x04)); /* Refresh the CNT */ |
| 161 | __raw_writel(REFRESH_WORD1, (wdog_base + 0x04)); |
| 162 | dmb(); |
| 163 | |
| 164 | if (!(val_cs & 800)) { |
| 165 | dmb(); |
| 166 | __raw_writel(UNLOCK_WORD0, (wdog_base + 0x04)); |
| 167 | __raw_writel(UNLOCK_WORD1, (wdog_base + 0x04)); |
| 168 | dmb(); |
| 169 | |
| 170 | while (!(readl(wdog_base + 0x00) & 0x800)) |
| 171 | ; |
| 172 | } |
| 173 | writel(0x0, (wdog_base + 0x0C)); /* Set WIN to 0 */ |
| 174 | writel(0x400, (wdog_base + 0x08)); /* Set timeout to default 0x400 */ |
| 175 | writel(0x120, (wdog_base + 0x00)); /* Disable it and set update */ |
| 176 | |
| 177 | while (!(readl(wdog_base + 0x00) & 0x400)) |
| 178 | ; |
| 179 | } |
| 180 | |
Peng Fan | 9ef89ea | 2021-08-07 16:00:35 +0800 | [diff] [blame] | 181 | void init_wdog(void) |
| 182 | { |
Peng Fan | 3a01f72 | 2021-08-07 16:00:49 +0800 | [diff] [blame] | 183 | disable_wdog((void __iomem *)WDG3_RBASE); |
Peng Fan | 9ef89ea | 2021-08-07 16:00:35 +0800 | [diff] [blame] | 184 | } |
| 185 | |
| 186 | static struct mm_region imx8ulp_arm64_mem_map[] = { |
| 187 | { |
| 188 | /* ROM */ |
| 189 | .virt = 0x0, |
| 190 | .phys = 0x0, |
| 191 | .size = 0x40000UL, |
| 192 | .attrs = PTE_BLOCK_MEMTYPE(MT_NORMAL) | |
| 193 | PTE_BLOCK_OUTER_SHARE |
| 194 | }, |
| 195 | { |
| 196 | /* FLEXSPI0 */ |
| 197 | .virt = 0x04000000, |
| 198 | .phys = 0x04000000, |
| 199 | .size = 0x08000000UL, |
| 200 | .attrs = PTE_BLOCK_MEMTYPE(MT_DEVICE_NGNRNE) | |
| 201 | PTE_BLOCK_NON_SHARE | |
| 202 | PTE_BLOCK_PXN | PTE_BLOCK_UXN |
| 203 | }, |
| 204 | { |
| 205 | /* SSRAM (align with 2M) */ |
| 206 | .virt = 0x1FE00000UL, |
| 207 | .phys = 0x1FE00000UL, |
| 208 | .size = 0x400000UL, |
| 209 | .attrs = PTE_BLOCK_MEMTYPE(MT_NORMAL) | |
| 210 | PTE_BLOCK_OUTER_SHARE | |
| 211 | PTE_BLOCK_PXN | PTE_BLOCK_UXN |
| 212 | }, { |
| 213 | /* SRAM1 (align with 2M) */ |
| 214 | .virt = 0x21000000UL, |
| 215 | .phys = 0x21000000UL, |
| 216 | .size = 0x200000UL, |
| 217 | .attrs = PTE_BLOCK_MEMTYPE(MT_NORMAL) | |
| 218 | PTE_BLOCK_OUTER_SHARE | |
| 219 | PTE_BLOCK_PXN | PTE_BLOCK_UXN |
| 220 | }, { |
| 221 | /* SRAM0 (align with 2M) */ |
| 222 | .virt = 0x22000000UL, |
| 223 | .phys = 0x22000000UL, |
| 224 | .size = 0x200000UL, |
| 225 | .attrs = PTE_BLOCK_MEMTYPE(MT_NORMAL) | |
| 226 | PTE_BLOCK_OUTER_SHARE | |
| 227 | PTE_BLOCK_PXN | PTE_BLOCK_UXN |
| 228 | }, { |
| 229 | /* Peripherals */ |
| 230 | .virt = 0x27000000UL, |
| 231 | .phys = 0x27000000UL, |
| 232 | .size = 0x3000000UL, |
| 233 | .attrs = PTE_BLOCK_MEMTYPE(MT_DEVICE_NGNRNE) | |
| 234 | PTE_BLOCK_NON_SHARE | |
| 235 | PTE_BLOCK_PXN | PTE_BLOCK_UXN |
| 236 | }, { |
| 237 | /* Peripherals */ |
| 238 | .virt = 0x2D000000UL, |
| 239 | .phys = 0x2D000000UL, |
| 240 | .size = 0x1600000UL, |
| 241 | .attrs = PTE_BLOCK_MEMTYPE(MT_DEVICE_NGNRNE) | |
| 242 | PTE_BLOCK_NON_SHARE | |
| 243 | PTE_BLOCK_PXN | PTE_BLOCK_UXN |
| 244 | }, { |
| 245 | /* FLEXSPI1-2 */ |
| 246 | .virt = 0x40000000UL, |
| 247 | .phys = 0x40000000UL, |
| 248 | .size = 0x40000000UL, |
| 249 | .attrs = PTE_BLOCK_MEMTYPE(MT_DEVICE_NGNRNE) | |
| 250 | PTE_BLOCK_NON_SHARE | |
| 251 | PTE_BLOCK_PXN | PTE_BLOCK_UXN |
| 252 | }, { |
| 253 | /* DRAM1 */ |
| 254 | .virt = 0x80000000UL, |
| 255 | .phys = 0x80000000UL, |
| 256 | .size = PHYS_SDRAM_SIZE, |
| 257 | .attrs = PTE_BLOCK_MEMTYPE(MT_NORMAL) | |
| 258 | PTE_BLOCK_OUTER_SHARE |
| 259 | }, { |
| 260 | /* |
| 261 | * empty entrie to split table entry 5 |
| 262 | * if needed when TEEs are used |
| 263 | */ |
| 264 | 0, |
| 265 | }, { |
| 266 | /* List terminator */ |
| 267 | 0, |
| 268 | } |
| 269 | }; |
| 270 | |
| 271 | struct mm_region *mem_map = imx8ulp_arm64_mem_map; |
| 272 | |
| 273 | /* simplify the page table size to enhance boot speed */ |
| 274 | #define MAX_PTE_ENTRIES 512 |
| 275 | #define MAX_MEM_MAP_REGIONS 16 |
| 276 | u64 get_page_table_size(void) |
| 277 | { |
| 278 | u64 one_pt = MAX_PTE_ENTRIES * sizeof(u64); |
| 279 | u64 size = 0; |
| 280 | |
| 281 | /* |
| 282 | * For each memory region, the max table size: |
| 283 | * 2 level 3 tables + 2 level 2 tables + 1 level 1 table |
| 284 | */ |
| 285 | size = (2 + 2 + 1) * one_pt * MAX_MEM_MAP_REGIONS + one_pt; |
| 286 | |
| 287 | /* |
| 288 | * We need to duplicate our page table once to have an emergency pt to |
| 289 | * resort to when splitting page tables later on |
| 290 | */ |
| 291 | size *= 2; |
| 292 | |
| 293 | /* |
| 294 | * We may need to split page tables later on if dcache settings change, |
| 295 | * so reserve up to 4 (random pick) page tables for that. |
| 296 | */ |
| 297 | size += one_pt * 4; |
| 298 | |
| 299 | return size; |
| 300 | } |
| 301 | |
| 302 | void enable_caches(void) |
| 303 | { |
| 304 | /* TODO: add TEE memmap region */ |
| 305 | |
| 306 | icache_enable(); |
| 307 | dcache_enable(); |
| 308 | } |
| 309 | |
| 310 | int dram_init(void) |
| 311 | { |
| 312 | gd->ram_size = PHYS_SDRAM_SIZE; |
| 313 | |
| 314 | return 0; |
| 315 | } |
| 316 | |
| 317 | #ifdef CONFIG_SERIAL_TAG |
| 318 | void get_board_serial(struct tag_serialnr *serialnr) |
| 319 | { |
| 320 | /* TODO */ |
| 321 | } |
| 322 | #endif |
| 323 | |
Ye Li | aadd6ca | 2021-08-07 16:00:50 +0800 | [diff] [blame^] | 324 | static void set_core0_reset_vector(u32 entry) |
Peng Fan | 9ef89ea | 2021-08-07 16:00:35 +0800 | [diff] [blame] | 325 | { |
Ye Li | 610083e | 2021-08-07 16:00:48 +0800 | [diff] [blame] | 326 | /* Update SIM1 DGO8 for reset vector base */ |
Ye Li | aadd6ca | 2021-08-07 16:00:50 +0800 | [diff] [blame^] | 327 | writel(entry, SIM1_BASE_ADDR + 0x5c); |
Ye Li | 610083e | 2021-08-07 16:00:48 +0800 | [diff] [blame] | 328 | |
| 329 | /* set update bit */ |
| 330 | setbits_le32(SIM1_BASE_ADDR + 0x8, 0x1 << 24); |
| 331 | |
| 332 | /* polling the ack */ |
| 333 | while ((readl(SIM1_BASE_ADDR + 0x8) & (0x1 << 26)) == 0) |
| 334 | ; |
| 335 | |
| 336 | /* clear the update */ |
| 337 | clrbits_le32(SIM1_BASE_ADDR + 0x8, (0x1 << 24)); |
| 338 | |
| 339 | /* clear the ack by set 1 */ |
| 340 | setbits_le32(SIM1_BASE_ADDR + 0x8, (0x1 << 26)); |
Ye Li | aadd6ca | 2021-08-07 16:00:50 +0800 | [diff] [blame^] | 341 | } |
| 342 | |
| 343 | int arch_cpu_init(void) |
| 344 | { |
| 345 | if (IS_ENABLED(CONFIG_SPL_BUILD)) { |
| 346 | clock_init(); |
| 347 | } else { |
| 348 | /* reconfigure core0 reset vector to ROM */ |
| 349 | set_core0_reset_vector(0x1000); |
| 350 | } |
| 351 | |
| 352 | return 0; |
| 353 | } |
| 354 | |
| 355 | #if defined(CONFIG_SPL_BUILD) |
| 356 | __weak void __noreturn jump_to_image_no_args(struct spl_image_info *spl_image) |
| 357 | { |
| 358 | debug("image entry point: 0x%lx\n", spl_image->entry_point); |
| 359 | |
| 360 | set_core0_reset_vector((u32)spl_image->entry_point); |
Ye Li | 610083e | 2021-08-07 16:00:48 +0800 | [diff] [blame] | 361 | |
| 362 | /* Enable the 512KB cache */ |
| 363 | setbits_le32(SIM1_BASE_ADDR + 0x30, (0x1 << 4)); |
| 364 | |
| 365 | /* reset core */ |
| 366 | setbits_le32(SIM1_BASE_ADDR + 0x30, (0x1 << 16)); |
| 367 | |
| 368 | while (1) |
| 369 | ; |
| 370 | } |
| 371 | #endif |