Tom Rini | 83d290c | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Peng Fan | 1b40982 | 2017-02-22 16:21:43 +0800 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (C) 2016 Freescale Semiconductor, Inc. |
Peng Fan | 1b40982 | 2017-02-22 16:21:43 +0800 | [diff] [blame] | 4 | */ |
Simon Glass | c3dc39a | 2020-05-10 11:39:55 -0600 | [diff] [blame] | 5 | |
| 6 | #include <common.h> |
Simon Glass | 9a3b4ce | 2019-12-28 10:45:01 -0700 | [diff] [blame] | 7 | #include <cpu_func.h> |
Simon Glass | 5255932 | 2019-11-14 12:57:46 -0700 | [diff] [blame] | 8 | #include <init.h> |
Peng Fan | 1b40982 | 2017-02-22 16:21:43 +0800 | [diff] [blame] | 9 | #include <asm/io.h> |
| 10 | #include <asm/arch/clock.h> |
| 11 | #include <asm/arch/imx-regs.h> |
| 12 | #include <asm/arch/sys_proto.h> |
Peng Fan | e92fca6 | 2019-07-22 01:24:37 +0000 | [diff] [blame] | 13 | #include <asm/mach-imx/boot_mode.h> |
Stefano Babic | 552a848 | 2017-06-29 10:16:06 +0200 | [diff] [blame] | 14 | #include <asm/mach-imx/hab.h> |
Peng Fan | 1b40982 | 2017-02-22 16:21:43 +0800 | [diff] [blame] | 15 | |
Fabio Estevam | b8cabb0 | 2019-11-05 09:47:51 -0300 | [diff] [blame] | 16 | #define PMC0_BASE_ADDR 0x410a1000 |
| 17 | #define PMC0_CTRL 0x28 |
| 18 | #define PMC0_CTRL_LDOEN BIT(31) |
| 19 | #define PMC0_CTRL_LDOOKDIS BIT(30) |
| 20 | #define PMC0_CTRL_PMC1ON BIT(24) |
| 21 | #define PMC1_BASE_ADDR 0x40400000 |
| 22 | #define PMC1_RUN 0x8 |
| 23 | #define PMC1_STOP 0x10 |
| 24 | #define PMC1_VLPS 0x14 |
Fabio Estevam | 0619af0 | 2019-11-05 09:47:52 -0300 | [diff] [blame] | 25 | #define PMC1_LDOVL_SHIFT 16 |
| 26 | #define PMC1_LDOVL_MASK (0x3f << PMC1_LDOVL_SHIFT) |
| 27 | #define PMC1_LDOVL_900 0x1e |
| 28 | #define PMC1_LDOVL_950 0x23 |
Fabio Estevam | b8cabb0 | 2019-11-05 09:47:51 -0300 | [diff] [blame] | 29 | #define PMC1_STATUS 0x20 |
| 30 | #define PMC1_STATUS_LDOVLF BIT(8) |
| 31 | |
Peng Fan | 1b40982 | 2017-02-22 16:21:43 +0800 | [diff] [blame] | 32 | static char *get_reset_cause(char *); |
| 33 | |
Stefano Babic | d714a75 | 2019-09-20 08:47:53 +0200 | [diff] [blame] | 34 | #if defined(CONFIG_IMX_HAB) |
Peng Fan | 27117b2 | 2017-02-22 16:21:53 +0800 | [diff] [blame] | 35 | struct imx_sec_config_fuse_t const imx_sec_config_fuse = { |
| 36 | .bank = 29, |
| 37 | .word = 6, |
| 38 | }; |
| 39 | #endif |
| 40 | |
Peng Fan | e25dc29 | 2019-07-22 01:25:05 +0000 | [diff] [blame] | 41 | #define ROM_VERSION_ADDR 0x80 |
Peng Fan | 1b40982 | 2017-02-22 16:21:43 +0800 | [diff] [blame] | 42 | u32 get_cpu_rev(void) |
| 43 | { |
Peng Fan | e25dc29 | 2019-07-22 01:25:05 +0000 | [diff] [blame] | 44 | /* Check the ROM version for cpu revision */ |
| 45 | u32 rom_version = readl((void __iomem *)ROM_VERSION_ADDR); |
| 46 | |
| 47 | return (MXC_CPU_MX7ULP << 12) | (rom_version & 0xFF); |
Peng Fan | 1b40982 | 2017-02-22 16:21:43 +0800 | [diff] [blame] | 48 | } |
| 49 | |
| 50 | #ifdef CONFIG_REVISION_TAG |
| 51 | u32 __weak get_board_rev(void) |
| 52 | { |
| 53 | return get_cpu_rev(); |
| 54 | } |
| 55 | #endif |
| 56 | |
| 57 | enum bt_mode get_boot_mode(void) |
| 58 | { |
| 59 | u32 bt0_cfg = 0; |
| 60 | |
| 61 | bt0_cfg = readl(CMC0_RBASE + 0x40); |
| 62 | bt0_cfg &= (BT0CFG_LPBOOT_MASK | BT0CFG_DUALBOOT_MASK); |
| 63 | |
| 64 | if (!(bt0_cfg & BT0CFG_LPBOOT_MASK)) { |
| 65 | /* No low power boot */ |
| 66 | if (bt0_cfg & BT0CFG_DUALBOOT_MASK) |
| 67 | return DUAL_BOOT; |
| 68 | else |
| 69 | return SINGLE_BOOT; |
| 70 | } |
| 71 | |
| 72 | return LOW_POWER_BOOT; |
| 73 | } |
| 74 | |
| 75 | int arch_cpu_init(void) |
| 76 | { |
| 77 | return 0; |
| 78 | } |
| 79 | |
| 80 | #ifdef CONFIG_BOARD_POSTCLK_INIT |
| 81 | int board_postclk_init(void) |
| 82 | { |
| 83 | return 0; |
| 84 | } |
| 85 | #endif |
| 86 | |
| 87 | #define UNLOCK_WORD0 0xC520 /* 1st unlock word */ |
| 88 | #define UNLOCK_WORD1 0xD928 /* 2nd unlock word */ |
| 89 | #define REFRESH_WORD0 0xA602 /* 1st refresh word */ |
| 90 | #define REFRESH_WORD1 0xB480 /* 2nd refresh word */ |
| 91 | |
| 92 | static void disable_wdog(u32 wdog_base) |
| 93 | { |
| 94 | writel(UNLOCK_WORD0, (wdog_base + 0x04)); |
| 95 | writel(UNLOCK_WORD1, (wdog_base + 0x04)); |
| 96 | writel(0x0, (wdog_base + 0x0C)); /* Set WIN to 0 */ |
| 97 | writel(0x400, (wdog_base + 0x08)); /* Set timeout to default 0x400 */ |
| 98 | writel(0x120, (wdog_base + 0x00)); /* Disable it and set update */ |
| 99 | |
| 100 | writel(REFRESH_WORD0, (wdog_base + 0x04)); /* Refresh the CNT */ |
| 101 | writel(REFRESH_WORD1, (wdog_base + 0x04)); |
| 102 | } |
| 103 | |
| 104 | void init_wdog(void) |
| 105 | { |
| 106 | /* |
| 107 | * ROM will configure WDOG1, disable it or enable it |
| 108 | * depending on FUSE. The update bit is set for reconfigurable. |
| 109 | * We have to use unlock sequence to reconfigure it. |
| 110 | * WDOG2 is not touched by ROM, so it will have default value |
| 111 | * which is enabled. We can directly configure it. |
| 112 | * To simplify the codes, we still use same reconfigure |
| 113 | * process as WDOG1. Because the update bit is not set for |
| 114 | * WDOG2, the unlock sequence won't take effect really. |
| 115 | * It actually directly configure the wdog. |
| 116 | * In this function, we will disable both WDOG1 and WDOG2, |
| 117 | * and set update bit for both. So that kernel can reconfigure them. |
| 118 | */ |
| 119 | disable_wdog(WDG1_RBASE); |
| 120 | disable_wdog(WDG2_RBASE); |
| 121 | } |
| 122 | |
Fabio Estevam | cbc81b7 | 2020-02-03 09:01:09 -0300 | [diff] [blame] | 123 | static bool ldo_mode_is_enabled(void) |
| 124 | { |
| 125 | unsigned int reg; |
| 126 | |
| 127 | reg = readl(PMC0_BASE_ADDR + PMC0_CTRL); |
| 128 | if (reg & PMC0_CTRL_LDOEN) |
| 129 | return true; |
| 130 | else |
| 131 | return false; |
| 132 | } |
| 133 | |
Jorge Ramirez-Ortiz | 30b8eb5 | 2020-01-17 10:50:25 +0100 | [diff] [blame] | 134 | #if !defined(CONFIG_SPL) || (defined(CONFIG_SPL) && defined(CONFIG_SPL_BUILD)) |
Fabio Estevam | b8cabb0 | 2019-11-05 09:47:51 -0300 | [diff] [blame] | 135 | #if defined(CONFIG_LDO_ENABLED_MODE) |
| 136 | static void init_ldo_mode(void) |
| 137 | { |
| 138 | unsigned int reg; |
| 139 | |
Fabio Estevam | cbc81b7 | 2020-02-03 09:01:09 -0300 | [diff] [blame] | 140 | if (ldo_mode_is_enabled()) |
| 141 | return; |
| 142 | |
Fabio Estevam | b8cabb0 | 2019-11-05 09:47:51 -0300 | [diff] [blame] | 143 | /* Set LDOOKDIS */ |
| 144 | setbits_le32(PMC0_BASE_ADDR + PMC0_CTRL, PMC0_CTRL_LDOOKDIS); |
| 145 | |
| 146 | /* Set LDOVL to 0.95V in PMC1_RUN */ |
| 147 | reg = readl(PMC1_BASE_ADDR + PMC1_RUN); |
Fabio Estevam | 0619af0 | 2019-11-05 09:47:52 -0300 | [diff] [blame] | 148 | reg &= ~PMC1_LDOVL_MASK; |
| 149 | reg |= (PMC1_LDOVL_950 << PMC1_LDOVL_SHIFT); |
Fabio Estevam | b8cabb0 | 2019-11-05 09:47:51 -0300 | [diff] [blame] | 150 | writel(PMC1_BASE_ADDR + PMC1_RUN, reg); |
| 151 | |
| 152 | /* Wait for LDOVLF to be cleared */ |
| 153 | reg = readl(PMC1_BASE_ADDR + PMC1_STATUS); |
| 154 | while (reg & PMC1_STATUS_LDOVLF) |
| 155 | ; |
| 156 | |
| 157 | /* Set LDOVL to 0.95V in PMC1_STOP */ |
| 158 | reg = readl(PMC1_BASE_ADDR + PMC1_STOP); |
Fabio Estevam | 0619af0 | 2019-11-05 09:47:52 -0300 | [diff] [blame] | 159 | reg &= ~PMC1_LDOVL_MASK; |
| 160 | reg |= (PMC1_LDOVL_950 << PMC1_LDOVL_SHIFT); |
Fabio Estevam | b8cabb0 | 2019-11-05 09:47:51 -0300 | [diff] [blame] | 161 | writel(PMC1_BASE_ADDR + PMC1_STOP, reg); |
| 162 | |
| 163 | /* Set LDOVL to 0.90V in PMC1_VLPS */ |
| 164 | reg = readl(PMC1_BASE_ADDR + PMC1_VLPS); |
Fabio Estevam | 0619af0 | 2019-11-05 09:47:52 -0300 | [diff] [blame] | 165 | reg &= ~PMC1_LDOVL_MASK; |
| 166 | reg |= (PMC1_LDOVL_900 << PMC1_LDOVL_SHIFT); |
Fabio Estevam | b8cabb0 | 2019-11-05 09:47:51 -0300 | [diff] [blame] | 167 | writel(PMC1_BASE_ADDR + PMC1_VLPS, reg); |
| 168 | |
| 169 | /* Set LDOEN bit */ |
| 170 | setbits_le32(PMC0_BASE_ADDR + PMC0_CTRL, PMC0_CTRL_LDOEN); |
| 171 | |
| 172 | /* Set the PMC1ON bit */ |
| 173 | setbits_le32(PMC0_BASE_ADDR + PMC0_CTRL, PMC0_CTRL_PMC1ON); |
| 174 | } |
| 175 | #endif |
Peng Fan | 1b40982 | 2017-02-22 16:21:43 +0800 | [diff] [blame] | 176 | |
| 177 | void s_init(void) |
| 178 | { |
| 179 | /* Disable wdog */ |
| 180 | init_wdog(); |
| 181 | |
| 182 | /* clock configuration. */ |
| 183 | clock_init(); |
| 184 | |
Bai Ping | 7777406 | 2019-07-22 01:24:42 +0000 | [diff] [blame] | 185 | if (soc_rev() < CHIP_REV_2_0) { |
| 186 | /* enable dumb pmic */ |
| 187 | writel((readl(SNVS_LP_LPCR) | SNVS_LPCR_DPEN), SNVS_LP_LPCR); |
| 188 | } |
Fabio Estevam | b8cabb0 | 2019-11-05 09:47:51 -0300 | [diff] [blame] | 189 | |
| 190 | #if defined(CONFIG_LDO_ENABLED_MODE) |
| 191 | init_ldo_mode(); |
| 192 | #endif |
Peng Fan | 1b40982 | 2017-02-22 16:21:43 +0800 | [diff] [blame] | 193 | return; |
| 194 | } |
Jorge Ramirez-Ortiz | 30b8eb5 | 2020-01-17 10:50:25 +0100 | [diff] [blame] | 195 | #endif |
Peng Fan | 1b40982 | 2017-02-22 16:21:43 +0800 | [diff] [blame] | 196 | |
| 197 | #ifndef CONFIG_ULP_WATCHDOG |
| 198 | void reset_cpu(ulong addr) |
| 199 | { |
| 200 | setbits_le32(SIM0_RBASE, SIM_SOPT1_A7_SW_RESET); |
| 201 | while (1) |
| 202 | ; |
| 203 | } |
| 204 | #endif |
| 205 | |
| 206 | #if defined(CONFIG_DISPLAY_CPUINFO) |
| 207 | const char *get_imx_type(u32 imxtype) |
| 208 | { |
| 209 | return "7ULP"; |
| 210 | } |
| 211 | |
| 212 | int print_cpuinfo(void) |
| 213 | { |
| 214 | u32 cpurev; |
| 215 | char cause[18]; |
| 216 | |
| 217 | cpurev = get_cpu_rev(); |
| 218 | |
| 219 | printf("CPU: Freescale i.MX%s rev%d.%d at %d MHz\n", |
| 220 | get_imx_type((cpurev & 0xFF000) >> 12), |
| 221 | (cpurev & 0x000F0) >> 4, (cpurev & 0x0000F) >> 0, |
| 222 | mxc_get_clock(MXC_ARM_CLK) / 1000000); |
| 223 | |
| 224 | printf("Reset cause: %s\n", get_reset_cause(cause)); |
| 225 | |
| 226 | printf("Boot mode: "); |
| 227 | switch (get_boot_mode()) { |
| 228 | case LOW_POWER_BOOT: |
| 229 | printf("Low power boot\n"); |
| 230 | break; |
| 231 | case DUAL_BOOT: |
| 232 | printf("Dual boot\n"); |
| 233 | break; |
| 234 | case SINGLE_BOOT: |
| 235 | default: |
| 236 | printf("Single boot\n"); |
| 237 | break; |
| 238 | } |
| 239 | |
Fabio Estevam | 72a093a | 2019-11-05 09:47:50 -0300 | [diff] [blame] | 240 | if (ldo_mode_is_enabled()) |
| 241 | printf("PMC1: LDO enabled mode\n"); |
| 242 | else |
| 243 | printf("PMC1: LDO bypass mode\n"); |
| 244 | |
Peng Fan | 1b40982 | 2017-02-22 16:21:43 +0800 | [diff] [blame] | 245 | return 0; |
| 246 | } |
| 247 | #endif |
| 248 | |
| 249 | #define CMC_SRS_TAMPER (1 << 31) |
| 250 | #define CMC_SRS_SECURITY (1 << 30) |
| 251 | #define CMC_SRS_TZWDG (1 << 29) |
| 252 | #define CMC_SRS_JTAG_RST (1 << 28) |
| 253 | #define CMC_SRS_CORE1 (1 << 16) |
| 254 | #define CMC_SRS_LOCKUP (1 << 15) |
| 255 | #define CMC_SRS_SW (1 << 14) |
| 256 | #define CMC_SRS_WDG (1 << 13) |
| 257 | #define CMC_SRS_PIN_RESET (1 << 8) |
| 258 | #define CMC_SRS_WARM (1 << 4) |
| 259 | #define CMC_SRS_HVD (1 << 3) |
| 260 | #define CMC_SRS_LVD (1 << 2) |
| 261 | #define CMC_SRS_POR (1 << 1) |
| 262 | #define CMC_SRS_WUP (1 << 0) |
| 263 | |
| 264 | static u32 reset_cause = -1; |
| 265 | |
| 266 | static char *get_reset_cause(char *ret) |
| 267 | { |
| 268 | u32 cause1, cause = 0, srs = 0; |
| 269 | u32 *reg_ssrs = (u32 *)(SRC_BASE_ADDR + 0x28); |
| 270 | u32 *reg_srs = (u32 *)(SRC_BASE_ADDR + 0x20); |
| 271 | |
| 272 | if (!ret) |
| 273 | return "null"; |
| 274 | |
| 275 | srs = readl(reg_srs); |
| 276 | cause1 = readl(reg_ssrs); |
| 277 | writel(cause1, reg_ssrs); |
| 278 | |
| 279 | reset_cause = cause1; |
| 280 | |
| 281 | cause = cause1 & (CMC_SRS_POR | CMC_SRS_WUP | CMC_SRS_WARM); |
| 282 | |
| 283 | switch (cause) { |
| 284 | case CMC_SRS_POR: |
| 285 | sprintf(ret, "%s", "POR"); |
| 286 | break; |
| 287 | case CMC_SRS_WUP: |
| 288 | sprintf(ret, "%s", "WUP"); |
| 289 | break; |
| 290 | case CMC_SRS_WARM: |
| 291 | cause = cause1 & (CMC_SRS_WDG | CMC_SRS_SW | |
| 292 | CMC_SRS_JTAG_RST); |
| 293 | switch (cause) { |
| 294 | case CMC_SRS_WDG: |
| 295 | sprintf(ret, "%s", "WARM-WDG"); |
| 296 | break; |
| 297 | case CMC_SRS_SW: |
| 298 | sprintf(ret, "%s", "WARM-SW"); |
| 299 | break; |
| 300 | case CMC_SRS_JTAG_RST: |
| 301 | sprintf(ret, "%s", "WARM-JTAG"); |
| 302 | break; |
| 303 | default: |
| 304 | sprintf(ret, "%s", "WARM-UNKN"); |
| 305 | break; |
| 306 | } |
| 307 | break; |
| 308 | default: |
| 309 | sprintf(ret, "%s-%X", "UNKN", cause1); |
| 310 | break; |
| 311 | } |
| 312 | |
| 313 | debug("[%X] SRS[%X] %X - ", cause1, srs, srs^cause1); |
| 314 | return ret; |
| 315 | } |
| 316 | |
| 317 | #ifdef CONFIG_ENV_IS_IN_MMC |
| 318 | __weak int board_mmc_get_env_dev(int devno) |
| 319 | { |
| 320 | return CONFIG_SYS_MMC_ENV_DEV; |
| 321 | } |
| 322 | |
| 323 | int mmc_get_env_dev(void) |
| 324 | { |
| 325 | int devno = 0; |
| 326 | u32 bt1_cfg = 0; |
| 327 | |
| 328 | /* If not boot from sd/mmc, use default value */ |
| 329 | if (get_boot_mode() == LOW_POWER_BOOT) |
| 330 | return CONFIG_SYS_MMC_ENV_DEV; |
| 331 | |
| 332 | bt1_cfg = readl(CMC1_RBASE + 0x40); |
| 333 | devno = (bt1_cfg >> 9) & 0x7; |
| 334 | |
| 335 | return board_mmc_get_env_dev(devno); |
| 336 | } |
| 337 | #endif |
Peng Fan | e92fca6 | 2019-07-22 01:24:37 +0000 | [diff] [blame] | 338 | |
| 339 | enum boot_device get_boot_device(void) |
| 340 | { |
| 341 | struct bootrom_sw_info **p = |
| 342 | (struct bootrom_sw_info **)ROM_SW_INFO_ADDR; |
| 343 | |
| 344 | enum boot_device boot_dev = SD1_BOOT; |
| 345 | u8 boot_type = (*p)->boot_dev_type; |
| 346 | u8 boot_instance = (*p)->boot_dev_instance; |
| 347 | |
| 348 | switch (boot_type) { |
| 349 | case BOOT_TYPE_SD: |
| 350 | boot_dev = boot_instance + SD1_BOOT; |
| 351 | break; |
| 352 | case BOOT_TYPE_MMC: |
| 353 | boot_dev = boot_instance + MMC1_BOOT; |
| 354 | break; |
| 355 | case BOOT_TYPE_USB: |
| 356 | boot_dev = USB_BOOT; |
| 357 | break; |
| 358 | default: |
| 359 | break; |
| 360 | } |
| 361 | |
| 362 | return boot_dev; |
| 363 | } |