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