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