Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Peng Fan | eae4de2 | 2018-01-10 13:20:37 +0800 | [diff] [blame] | 2 | /* |
| 3 | * Copyright 2017 NXP |
| 4 | * |
| 5 | * Peng Fan <peng.fan@nxp.com> |
Peng Fan | eae4de2 | 2018-01-10 13:20:37 +0800 | [diff] [blame] | 6 | */ |
| 7 | |
| 8 | #include <common.h> |
Simon Glass | 1d91ba7 | 2019-11-14 12:57:37 -0700 | [diff] [blame] | 9 | #include <cpu_func.h> |
Peng Fan | eae4de2 | 2018-01-10 13:20:37 +0800 | [diff] [blame] | 10 | #include <asm/arch/imx-regs.h> |
| 11 | #include <asm/io.h> |
| 12 | #include <asm/arch/clock.h> |
| 13 | #include <asm/arch/sys_proto.h> |
| 14 | #include <asm/mach-imx/hab.h> |
| 15 | #include <asm/mach-imx/boot_mode.h> |
| 16 | #include <asm/mach-imx/syscounter.h> |
| 17 | #include <asm/armv8/mmu.h> |
Peng Fan | c98e032 | 2019-08-27 06:25:58 +0000 | [diff] [blame] | 18 | #include <dm/uclass.h> |
Peng Fan | eae4de2 | 2018-01-10 13:20:37 +0800 | [diff] [blame] | 19 | #include <errno.h> |
| 20 | #include <fdt_support.h> |
| 21 | #include <fsl_wdog.h> |
| 22 | #include <imx_sip.h> |
| 23 | |
| 24 | DECLARE_GLOBAL_DATA_PTR; |
| 25 | |
Stefano Babic | f8b509b | 2019-09-20 08:47:53 +0200 | [diff] [blame] | 26 | #if defined(CONFIG_IMX_HAB) |
Peng Fan | eae4de2 | 2018-01-10 13:20:37 +0800 | [diff] [blame] | 27 | struct imx_sec_config_fuse_t const imx_sec_config_fuse = { |
| 28 | .bank = 1, |
| 29 | .word = 3, |
| 30 | }; |
| 31 | #endif |
| 32 | |
| 33 | int timer_init(void) |
| 34 | { |
| 35 | #ifdef CONFIG_SPL_BUILD |
| 36 | struct sctr_regs *sctr = (struct sctr_regs *)SYSCNT_CTRL_BASE_ADDR; |
| 37 | unsigned long freq = readl(&sctr->cntfid0); |
| 38 | |
| 39 | /* Update with accurate clock frequency */ |
| 40 | asm volatile("msr cntfrq_el0, %0" : : "r" (freq) : "memory"); |
| 41 | |
| 42 | clrsetbits_le32(&sctr->cntcr, SC_CNTCR_FREQ0 | SC_CNTCR_FREQ1, |
| 43 | SC_CNTCR_FREQ0 | SC_CNTCR_ENABLE | SC_CNTCR_HDBG); |
| 44 | #endif |
| 45 | |
| 46 | gd->arch.tbl = 0; |
| 47 | gd->arch.tbu = 0; |
| 48 | |
| 49 | return 0; |
| 50 | } |
| 51 | |
| 52 | void enable_tzc380(void) |
| 53 | { |
| 54 | struct iomuxc_gpr_base_regs *gpr = |
| 55 | (struct iomuxc_gpr_base_regs *)IOMUXC_GPR_BASE_ADDR; |
| 56 | |
| 57 | /* Enable TZASC and lock setting */ |
| 58 | setbits_le32(&gpr->gpr[10], GPR_TZASC_EN); |
| 59 | setbits_le32(&gpr->gpr[10], GPR_TZASC_EN_LOCK); |
Peng Fan | c0e47fc | 2019-12-27 10:19:42 +0800 | [diff] [blame^] | 60 | if (is_imx8mm() || is_imx8mn() || is_imx8mp()) |
Peng Fan | 99047fc | 2019-08-27 06:25:30 +0000 | [diff] [blame] | 61 | setbits_le32(&gpr->gpr[10], BIT(1)); |
Ye Li | 4c97c46 | 2019-08-27 06:25:34 +0000 | [diff] [blame] | 62 | /* |
| 63 | * set Region 0 attribute to allow secure and non-secure |
| 64 | * read/write permission. Found some masters like usb dwc3 |
| 65 | * controllers can't work with secure memory. |
| 66 | */ |
| 67 | writel(0xf0000000, TZASC_BASE_ADDR + 0x108); |
Peng Fan | eae4de2 | 2018-01-10 13:20:37 +0800 | [diff] [blame] | 68 | } |
| 69 | |
| 70 | void set_wdog_reset(struct wdog_regs *wdog) |
| 71 | { |
| 72 | /* |
| 73 | * Output WDOG_B signal to reset external pmic or POR_B decided by |
| 74 | * the board design. Without external reset, the peripherals/DDR/ |
| 75 | * PMIC are not reset, that may cause system working abnormal. |
| 76 | * WDZST bit is write-once only bit. Align this bit in kernel, |
| 77 | * otherwise kernel code will have no chance to set this bit. |
| 78 | */ |
| 79 | setbits_le16(&wdog->wcr, WDOG_WDT_MASK | WDOG_WDZST_MASK); |
| 80 | } |
| 81 | |
| 82 | static struct mm_region imx8m_mem_map[] = { |
| 83 | { |
| 84 | /* ROM */ |
| 85 | .virt = 0x0UL, |
| 86 | .phys = 0x0UL, |
| 87 | .size = 0x100000UL, |
| 88 | .attrs = PTE_BLOCK_MEMTYPE(MT_NORMAL) | |
| 89 | PTE_BLOCK_OUTER_SHARE |
| 90 | }, { |
Gary Bisson | 5c72a45 | 2018-11-14 17:55:28 +0100 | [diff] [blame] | 91 | /* CAAM */ |
| 92 | .virt = 0x100000UL, |
| 93 | .phys = 0x100000UL, |
| 94 | .size = 0x8000UL, |
| 95 | .attrs = PTE_BLOCK_MEMTYPE(MT_DEVICE_NGNRNE) | |
| 96 | PTE_BLOCK_NON_SHARE | |
| 97 | PTE_BLOCK_PXN | PTE_BLOCK_UXN |
| 98 | }, { |
| 99 | /* TCM */ |
| 100 | .virt = 0x7C0000UL, |
| 101 | .phys = 0x7C0000UL, |
| 102 | .size = 0x80000UL, |
| 103 | .attrs = PTE_BLOCK_MEMTYPE(MT_DEVICE_NGNRNE) | |
| 104 | PTE_BLOCK_NON_SHARE | |
| 105 | PTE_BLOCK_PXN | PTE_BLOCK_UXN |
| 106 | }, { |
Peng Fan | eae4de2 | 2018-01-10 13:20:37 +0800 | [diff] [blame] | 107 | /* OCRAM */ |
| 108 | .virt = 0x900000UL, |
| 109 | .phys = 0x900000UL, |
| 110 | .size = 0x200000UL, |
| 111 | .attrs = PTE_BLOCK_MEMTYPE(MT_NORMAL) | |
| 112 | PTE_BLOCK_OUTER_SHARE |
| 113 | }, { |
| 114 | /* AIPS */ |
| 115 | .virt = 0xB00000UL, |
| 116 | .phys = 0xB00000UL, |
| 117 | .size = 0x3f500000UL, |
| 118 | .attrs = PTE_BLOCK_MEMTYPE(MT_DEVICE_NGNRNE) | |
| 119 | PTE_BLOCK_NON_SHARE | |
| 120 | PTE_BLOCK_PXN | PTE_BLOCK_UXN |
| 121 | }, { |
| 122 | /* DRAM1 */ |
| 123 | .virt = 0x40000000UL, |
| 124 | .phys = 0x40000000UL, |
Peng Fan | b749b5e | 2019-08-27 06:25:27 +0000 | [diff] [blame] | 125 | .size = PHYS_SDRAM_SIZE, |
Peng Fan | eae4de2 | 2018-01-10 13:20:37 +0800 | [diff] [blame] | 126 | .attrs = PTE_BLOCK_MEMTYPE(MT_NORMAL) | |
| 127 | PTE_BLOCK_OUTER_SHARE |
Peng Fan | b749b5e | 2019-08-27 06:25:27 +0000 | [diff] [blame] | 128 | #ifdef PHYS_SDRAM_2_SIZE |
Peng Fan | eae4de2 | 2018-01-10 13:20:37 +0800 | [diff] [blame] | 129 | }, { |
| 130 | /* DRAM2 */ |
| 131 | .virt = 0x100000000UL, |
| 132 | .phys = 0x100000000UL, |
Peng Fan | b749b5e | 2019-08-27 06:25:27 +0000 | [diff] [blame] | 133 | .size = PHYS_SDRAM_2_SIZE, |
Peng Fan | eae4de2 | 2018-01-10 13:20:37 +0800 | [diff] [blame] | 134 | .attrs = PTE_BLOCK_MEMTYPE(MT_NORMAL) | |
| 135 | PTE_BLOCK_OUTER_SHARE |
Peng Fan | b749b5e | 2019-08-27 06:25:27 +0000 | [diff] [blame] | 136 | #endif |
Peng Fan | eae4de2 | 2018-01-10 13:20:37 +0800 | [diff] [blame] | 137 | }, { |
| 138 | /* List terminator */ |
| 139 | 0, |
| 140 | } |
| 141 | }; |
| 142 | |
| 143 | struct mm_region *mem_map = imx8m_mem_map; |
| 144 | |
Peng Fan | b749b5e | 2019-08-27 06:25:27 +0000 | [diff] [blame] | 145 | void enable_caches(void) |
| 146 | { |
| 147 | /* |
| 148 | * If OPTEE runs, remove OPTEE memory from MMU table to |
| 149 | * avoid speculative prefetch. OPTEE runs at the top of |
| 150 | * the first memory bank |
| 151 | */ |
| 152 | if (rom_pointer[1]) |
| 153 | imx8m_mem_map[5].size -= rom_pointer[1]; |
| 154 | |
| 155 | icache_enable(); |
| 156 | dcache_enable(); |
| 157 | } |
| 158 | |
Peng Fan | 1caffdf | 2019-08-27 06:25:17 +0000 | [diff] [blame] | 159 | static u32 get_cpu_variant_type(u32 type) |
| 160 | { |
| 161 | struct ocotp_regs *ocotp = (struct ocotp_regs *)OCOTP_BASE_ADDR; |
| 162 | struct fuse_bank *bank = &ocotp->bank[1]; |
| 163 | struct fuse_bank1_regs *fuse = |
| 164 | (struct fuse_bank1_regs *)bank->fuse_regs; |
| 165 | |
| 166 | u32 value = readl(&fuse->tester4); |
| 167 | |
| 168 | if (type == MXC_CPU_IMX8MM) { |
| 169 | switch (value & 0x3) { |
| 170 | case 2: |
| 171 | if (value & 0x1c0000) |
| 172 | return MXC_CPU_IMX8MMDL; |
| 173 | else |
| 174 | return MXC_CPU_IMX8MMD; |
| 175 | case 3: |
| 176 | if (value & 0x1c0000) |
| 177 | return MXC_CPU_IMX8MMSL; |
| 178 | else |
| 179 | return MXC_CPU_IMX8MMS; |
| 180 | default: |
| 181 | if (value & 0x1c0000) |
| 182 | return MXC_CPU_IMX8MML; |
| 183 | break; |
| 184 | } |
| 185 | } |
| 186 | |
| 187 | return type; |
| 188 | } |
| 189 | |
Peng Fan | eae4de2 | 2018-01-10 13:20:37 +0800 | [diff] [blame] | 190 | u32 get_cpu_rev(void) |
| 191 | { |
| 192 | struct anamix_pll *ana_pll = (struct anamix_pll *)ANATOP_BASE_ADDR; |
| 193 | u32 reg = readl(&ana_pll->digprog); |
| 194 | u32 type = (reg >> 16) & 0xff; |
Peng Fan | 1caffdf | 2019-08-27 06:25:17 +0000 | [diff] [blame] | 195 | u32 major_low = (reg >> 8) & 0xff; |
Peng Fan | eae4de2 | 2018-01-10 13:20:37 +0800 | [diff] [blame] | 196 | u32 rom_version; |
| 197 | |
| 198 | reg &= 0xff; |
| 199 | |
Peng Fan | 69cec07 | 2019-12-27 10:14:02 +0800 | [diff] [blame] | 200 | /* iMX8MP */ |
| 201 | if (major_low == 0x43) { |
| 202 | return (MXC_CPU_IMX8MP << 12) | reg; |
| 203 | } else if (major_low == 0x42) { |
| 204 | /* iMX8MN */ |
Peng Fan | 5d2f206 | 2019-06-27 17:23:49 +0800 | [diff] [blame] | 205 | return (MXC_CPU_IMX8MN << 12) | reg; |
| 206 | } else if (major_low == 0x41) { |
Peng Fan | 1caffdf | 2019-08-27 06:25:17 +0000 | [diff] [blame] | 207 | type = get_cpu_variant_type(MXC_CPU_IMX8MM); |
| 208 | } else { |
| 209 | if (reg == CHIP_REV_1_0) { |
| 210 | /* |
Peng Fan | c23fbdd | 2019-10-16 10:24:17 +0000 | [diff] [blame] | 211 | * For B0 chip, the DIGPROG is not updated, |
| 212 | * it is still TO1.0. we have to check ROM |
| 213 | * version or OCOTP_READ_FUSE_DATA. |
| 214 | * 0xff0055aa is magic number for B1. |
Peng Fan | 1caffdf | 2019-08-27 06:25:17 +0000 | [diff] [blame] | 215 | */ |
Peng Fan | c23fbdd | 2019-10-16 10:24:17 +0000 | [diff] [blame] | 216 | if (readl((void __iomem *)(OCOTP_BASE_ADDR + 0x40)) == 0xff0055aa) { |
| 217 | reg = CHIP_REV_2_1; |
| 218 | } else { |
| 219 | rom_version = |
| 220 | readl((void __iomem *)ROM_VERSION_A0); |
| 221 | if (rom_version != CHIP_REV_1_0) { |
| 222 | rom_version = readl((void __iomem *)ROM_VERSION_B0); |
Patrick Wildt | d4a78b9 | 2019-11-19 09:42:06 +0100 | [diff] [blame] | 223 | rom_version &= 0xff; |
Peng Fan | c23fbdd | 2019-10-16 10:24:17 +0000 | [diff] [blame] | 224 | if (rom_version == CHIP_REV_2_0) |
| 225 | reg = CHIP_REV_2_0; |
| 226 | } |
Peng Fan | 1caffdf | 2019-08-27 06:25:17 +0000 | [diff] [blame] | 227 | } |
Peng Fan | eae4de2 | 2018-01-10 13:20:37 +0800 | [diff] [blame] | 228 | } |
| 229 | } |
| 230 | |
| 231 | return (type << 12) | reg; |
| 232 | } |
| 233 | |
| 234 | static void imx_set_wdog_powerdown(bool enable) |
| 235 | { |
| 236 | struct wdog_regs *wdog1 = (struct wdog_regs *)WDOG1_BASE_ADDR; |
| 237 | struct wdog_regs *wdog2 = (struct wdog_regs *)WDOG2_BASE_ADDR; |
| 238 | struct wdog_regs *wdog3 = (struct wdog_regs *)WDOG3_BASE_ADDR; |
| 239 | |
| 240 | /* Write to the PDE (Power Down Enable) bit */ |
| 241 | writew(enable, &wdog1->wmcr); |
| 242 | writew(enable, &wdog2->wmcr); |
| 243 | writew(enable, &wdog3->wmcr); |
| 244 | } |
| 245 | |
Peng Fan | c98e032 | 2019-08-27 06:25:58 +0000 | [diff] [blame] | 246 | int arch_cpu_init_dm(void) |
| 247 | { |
| 248 | struct udevice *dev; |
| 249 | int ret; |
| 250 | |
Peng Fan | 3c07334 | 2019-10-16 03:01:51 +0000 | [diff] [blame] | 251 | if (CONFIG_IS_ENABLED(CLK)) { |
| 252 | ret = uclass_get_device_by_name(UCLASS_CLK, |
| 253 | "clock-controller@30380000", |
| 254 | &dev); |
| 255 | if (ret < 0) { |
| 256 | printf("Failed to find clock node. Check device tree\n"); |
| 257 | return ret; |
| 258 | } |
Peng Fan | c98e032 | 2019-08-27 06:25:58 +0000 | [diff] [blame] | 259 | } |
| 260 | |
| 261 | return 0; |
| 262 | } |
| 263 | |
Peng Fan | eae4de2 | 2018-01-10 13:20:37 +0800 | [diff] [blame] | 264 | int arch_cpu_init(void) |
| 265 | { |
Peng Fan | c0b30d7 | 2019-04-17 09:41:16 +0000 | [diff] [blame] | 266 | struct ocotp_regs *ocotp = (struct ocotp_regs *)OCOTP_BASE_ADDR; |
Peng Fan | eae4de2 | 2018-01-10 13:20:37 +0800 | [diff] [blame] | 267 | /* |
Peng Fan | d0ca289 | 2019-08-27 06:25:37 +0000 | [diff] [blame] | 268 | * ROM might disable clock for SCTR, |
| 269 | * enable the clock before timer_init. |
| 270 | */ |
| 271 | if (IS_ENABLED(CONFIG_SPL_BUILD)) |
| 272 | clock_enable(CCGR_SCTR, 1); |
| 273 | /* |
Peng Fan | eae4de2 | 2018-01-10 13:20:37 +0800 | [diff] [blame] | 274 | * Init timer at very early state, because sscg pll setting |
| 275 | * will use it |
| 276 | */ |
| 277 | timer_init(); |
| 278 | |
| 279 | if (IS_ENABLED(CONFIG_SPL_BUILD)) { |
| 280 | clock_init(); |
| 281 | imx_set_wdog_powerdown(false); |
| 282 | } |
| 283 | |
Peng Fan | c0b30d7 | 2019-04-17 09:41:16 +0000 | [diff] [blame] | 284 | if (is_imx8mq()) { |
| 285 | clock_enable(CCGR_OCOTP, 1); |
| 286 | if (readl(&ocotp->ctrl) & 0x200) |
| 287 | writel(0x200, &ocotp->ctrl_clr); |
| 288 | } |
| 289 | |
Peng Fan | eae4de2 | 2018-01-10 13:20:37 +0800 | [diff] [blame] | 290 | return 0; |
| 291 | } |
| 292 | |
Peng Fan | c9823b0 | 2019-09-16 03:09:36 +0000 | [diff] [blame] | 293 | #if defined(CONFIG_IMX8MN) || defined(CONFIG_IMX8MP) |
| 294 | struct rom_api *g_rom_api = (struct rom_api *)0x980; |
| 295 | |
| 296 | enum boot_device get_boot_device(void) |
| 297 | { |
| 298 | volatile gd_t *pgd = gd; |
| 299 | int ret; |
| 300 | u32 boot; |
| 301 | u16 boot_type; |
| 302 | u8 boot_instance; |
| 303 | enum boot_device boot_dev = SD1_BOOT; |
| 304 | |
| 305 | ret = g_rom_api->query_boot_infor(QUERY_BT_DEV, &boot, |
| 306 | ((uintptr_t)&boot) ^ QUERY_BT_DEV); |
| 307 | gd = pgd; |
| 308 | |
| 309 | if (ret != ROM_API_OKAY) { |
| 310 | puts("ROMAPI: failure at query_boot_info\n"); |
| 311 | return -1; |
| 312 | } |
| 313 | |
| 314 | boot_type = boot >> 16; |
| 315 | boot_instance = (boot >> 8) & 0xff; |
| 316 | |
| 317 | switch (boot_type) { |
| 318 | case BT_DEV_TYPE_SD: |
| 319 | boot_dev = boot_instance + SD1_BOOT; |
| 320 | break; |
| 321 | case BT_DEV_TYPE_MMC: |
| 322 | boot_dev = boot_instance + MMC1_BOOT; |
| 323 | break; |
| 324 | case BT_DEV_TYPE_NAND: |
| 325 | boot_dev = NAND_BOOT; |
| 326 | break; |
| 327 | case BT_DEV_TYPE_FLEXSPINOR: |
| 328 | boot_dev = QSPI_BOOT; |
| 329 | break; |
| 330 | case BT_DEV_TYPE_USB: |
| 331 | boot_dev = USB_BOOT; |
| 332 | break; |
| 333 | default: |
| 334 | break; |
| 335 | } |
| 336 | |
| 337 | return boot_dev; |
| 338 | } |
| 339 | #endif |
| 340 | |
Peng Fan | eae4de2 | 2018-01-10 13:20:37 +0800 | [diff] [blame] | 341 | bool is_usb_boot(void) |
| 342 | { |
| 343 | return get_boot_device() == USB_BOOT; |
| 344 | } |
| 345 | |
| 346 | #ifdef CONFIG_OF_SYSTEM_SETUP |
| 347 | int ft_system_setup(void *blob, bd_t *bd) |
| 348 | { |
| 349 | int i = 0; |
| 350 | int rc; |
| 351 | int nodeoff; |
| 352 | |
| 353 | /* Disable the CPU idle for A0 chip since the HW does not support it */ |
| 354 | if (is_soc_rev(CHIP_REV_1_0)) { |
| 355 | static const char * const nodes_path[] = { |
| 356 | "/cpus/cpu@0", |
| 357 | "/cpus/cpu@1", |
| 358 | "/cpus/cpu@2", |
| 359 | "/cpus/cpu@3", |
| 360 | }; |
| 361 | |
| 362 | for (i = 0; i < ARRAY_SIZE(nodes_path); i++) { |
| 363 | nodeoff = fdt_path_offset(blob, nodes_path[i]); |
| 364 | if (nodeoff < 0) |
| 365 | continue; /* Not found, skip it */ |
| 366 | |
| 367 | printf("Found %s node\n", nodes_path[i]); |
| 368 | |
| 369 | rc = fdt_delprop(blob, nodeoff, "cpu-idle-states"); |
| 370 | if (rc) { |
| 371 | printf("Unable to update property %s:%s, err=%s\n", |
| 372 | nodes_path[i], "status", fdt_strerror(rc)); |
| 373 | return rc; |
| 374 | } |
| 375 | |
| 376 | printf("Remove %s:%s\n", nodes_path[i], |
| 377 | "cpu-idle-states"); |
| 378 | } |
| 379 | } |
| 380 | |
| 381 | return 0; |
| 382 | } |
| 383 | #endif |
| 384 | |
Peng Fan | 24290d9 | 2019-08-27 06:25:41 +0000 | [diff] [blame] | 385 | #if defined(CONFIG_SPL_BUILD) || !defined(CONFIG_SYSRESET) |
Peng Fan | eae4de2 | 2018-01-10 13:20:37 +0800 | [diff] [blame] | 386 | void reset_cpu(ulong addr) |
| 387 | { |
Peng Fan | 24290d9 | 2019-08-27 06:25:41 +0000 | [diff] [blame] | 388 | struct watchdog_regs *wdog = (struct watchdog_regs *)addr; |
Peng Fan | eae4de2 | 2018-01-10 13:20:37 +0800 | [diff] [blame] | 389 | |
Peng Fan | 24290d9 | 2019-08-27 06:25:41 +0000 | [diff] [blame] | 390 | if (!addr) |
| 391 | wdog = (struct watchdog_regs *)WDOG1_BASE_ADDR; |
Peng Fan | eae4de2 | 2018-01-10 13:20:37 +0800 | [diff] [blame] | 392 | |
Peng Fan | 24290d9 | 2019-08-27 06:25:41 +0000 | [diff] [blame] | 393 | /* Clear WDA to trigger WDOG_B immediately */ |
| 394 | writew((WCR_WDE | WCR_SRS), &wdog->wcr); |
| 395 | |
| 396 | while (1) { |
| 397 | /* |
| 398 | * spin for .5 seconds before reset |
| 399 | */ |
| 400 | } |
Peng Fan | eae4de2 | 2018-01-10 13:20:37 +0800 | [diff] [blame] | 401 | } |
Peng Fan | 24290d9 | 2019-08-27 06:25:41 +0000 | [diff] [blame] | 402 | #endif |