Tom Rini | 4549e78 | 2018-05-06 18:27:01 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ OR BSD-3-Clause |
Patrick Delaunay | 2514c2d | 2018-03-12 10:46:10 +0100 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (C) 2018, STMicroelectronics - All Rights Reserved |
Patrick Delaunay | 2514c2d | 2018-03-12 10:46:10 +0100 | [diff] [blame] | 4 | */ |
| 5 | #include <common.h> |
| 6 | #include <clk.h> |
Patrick Delaunay | 320d266 | 2018-05-17 14:50:46 +0200 | [diff] [blame] | 7 | #include <debug_uart.h> |
Patrick Delaunay | 7f7deb0 | 2018-05-17 15:24:07 +0200 | [diff] [blame] | 8 | #include <environment.h> |
| 9 | #include <misc.h> |
Patrick Delaunay | 2514c2d | 2018-03-12 10:46:10 +0100 | [diff] [blame] | 10 | #include <asm/io.h> |
| 11 | #include <asm/arch/stm32.h> |
Patrick Delaunay | 96583cd | 2018-03-19 19:09:21 +0100 | [diff] [blame] | 12 | #include <asm/arch/sys_proto.h> |
Patrick Delaunay | 7f7deb0 | 2018-05-17 15:24:07 +0200 | [diff] [blame] | 13 | #include <dm/device.h> |
Patrick Delaunay | 08772f6 | 2018-03-20 10:54:53 +0100 | [diff] [blame] | 14 | #include <dm/uclass.h> |
Patrick Delaunay | 2514c2d | 2018-03-12 10:46:10 +0100 | [diff] [blame] | 15 | |
Patrick Delaunay | cda3dcb | 2018-03-19 19:09:20 +0100 | [diff] [blame] | 16 | /* RCC register */ |
| 17 | #define RCC_TZCR (STM32_RCC_BASE + 0x00) |
| 18 | #define RCC_DBGCFGR (STM32_RCC_BASE + 0x080C) |
| 19 | #define RCC_BDCR (STM32_RCC_BASE + 0x0140) |
| 20 | #define RCC_MP_APB5ENSETR (STM32_RCC_BASE + 0x0208) |
| 21 | #define RCC_BDCR_VSWRST BIT(31) |
| 22 | #define RCC_BDCR_RTCSRC GENMASK(17, 16) |
| 23 | #define RCC_DBGCFGR_DBGCKEN BIT(8) |
Patrick Delaunay | 2514c2d | 2018-03-12 10:46:10 +0100 | [diff] [blame] | 24 | |
Patrick Delaunay | cda3dcb | 2018-03-19 19:09:20 +0100 | [diff] [blame] | 25 | /* Security register */ |
Patrick Delaunay | 2514c2d | 2018-03-12 10:46:10 +0100 | [diff] [blame] | 26 | #define ETZPC_TZMA1_SIZE (STM32_ETZPC_BASE + 0x04) |
| 27 | #define ETZPC_DECPROT0 (STM32_ETZPC_BASE + 0x10) |
| 28 | |
| 29 | #define TZC_GATE_KEEPER (STM32_TZC_BASE + 0x008) |
| 30 | #define TZC_REGION_ATTRIBUTE0 (STM32_TZC_BASE + 0x110) |
| 31 | #define TZC_REGION_ID_ACCESS0 (STM32_TZC_BASE + 0x114) |
| 32 | |
| 33 | #define TAMP_CR1 (STM32_TAMP_BASE + 0x00) |
| 34 | |
| 35 | #define PWR_CR1 (STM32_PWR_BASE + 0x00) |
| 36 | #define PWR_CR1_DBP BIT(8) |
| 37 | |
Patrick Delaunay | cda3dcb | 2018-03-19 19:09:20 +0100 | [diff] [blame] | 38 | /* DBGMCU register */ |
Patrick Delaunay | 96583cd | 2018-03-19 19:09:21 +0100 | [diff] [blame] | 39 | #define DBGMCU_IDC (STM32_DBGMCU_BASE + 0x00) |
Patrick Delaunay | cda3dcb | 2018-03-19 19:09:20 +0100 | [diff] [blame] | 40 | #define DBGMCU_APB4FZ1 (STM32_DBGMCU_BASE + 0x2C) |
| 41 | #define DBGMCU_APB4FZ1_IWDG2 BIT(2) |
Patrick Delaunay | 96583cd | 2018-03-19 19:09:21 +0100 | [diff] [blame] | 42 | #define DBGMCU_IDC_DEV_ID_MASK GENMASK(11, 0) |
| 43 | #define DBGMCU_IDC_DEV_ID_SHIFT 0 |
| 44 | #define DBGMCU_IDC_REV_ID_MASK GENMASK(31, 16) |
| 45 | #define DBGMCU_IDC_REV_ID_SHIFT 16 |
Patrick Delaunay | 2514c2d | 2018-03-12 10:46:10 +0100 | [diff] [blame] | 46 | |
Patrick Delaunay | 08772f6 | 2018-03-20 10:54:53 +0100 | [diff] [blame] | 47 | /* boot interface from Bootrom |
| 48 | * - boot instance = bit 31:16 |
| 49 | * - boot device = bit 15:0 |
| 50 | */ |
| 51 | #define BOOTROM_PARAM_ADDR 0x2FFC0078 |
| 52 | #define BOOTROM_MODE_MASK GENMASK(15, 0) |
| 53 | #define BOOTROM_MODE_SHIFT 0 |
| 54 | #define BOOTROM_INSTANCE_MASK GENMASK(31, 16) |
| 55 | #define BOOTROM_INSTANCE_SHIFT 16 |
| 56 | |
Patrick Delaunay | 7f7deb0 | 2018-05-17 15:24:07 +0200 | [diff] [blame] | 57 | /* BSEC OTP index */ |
Patrick Delaunay | 35d568f | 2019-02-27 17:01:13 +0100 | [diff] [blame] | 58 | #define BSEC_OTP_RPN 1 |
Patrick Delaunay | 7f7deb0 | 2018-05-17 15:24:07 +0200 | [diff] [blame] | 59 | #define BSEC_OTP_SERIAL 13 |
Patrick Delaunay | 35d568f | 2019-02-27 17:01:13 +0100 | [diff] [blame] | 60 | #define BSEC_OTP_PKG 16 |
Patrick Delaunay | 7f7deb0 | 2018-05-17 15:24:07 +0200 | [diff] [blame] | 61 | #define BSEC_OTP_MAC 57 |
| 62 | |
Patrick Delaunay | 35d568f | 2019-02-27 17:01:13 +0100 | [diff] [blame] | 63 | /* Device Part Number (RPN) = OTP_DATA1 lower 8 bits */ |
| 64 | #define RPN_SHIFT 0 |
| 65 | #define RPN_MASK GENMASK(7, 0) |
| 66 | |
| 67 | /* Package = bit 27:29 of OTP16 |
| 68 | * - 100: LBGA448 (FFI) => AA = LFBGA 18x18mm 448 balls p. 0.8mm |
| 69 | * - 011: LBGA354 (LCI) => AB = LFBGA 16x16mm 359 balls p. 0.8mm |
| 70 | * - 010: TFBGA361 (FFC) => AC = TFBGA 12x12mm 361 balls p. 0.5mm |
| 71 | * - 001: TFBGA257 (LCC) => AD = TFBGA 10x10mm 257 balls p. 0.5mm |
| 72 | * - others: Reserved |
| 73 | */ |
| 74 | #define PKG_SHIFT 27 |
| 75 | #define PKG_MASK GENMASK(2, 0) |
| 76 | |
| 77 | #define PKG_AA_LBGA448 4 |
| 78 | #define PKG_AB_LBGA354 3 |
| 79 | #define PKG_AC_TFBGA361 2 |
| 80 | #define PKG_AD_TFBGA257 1 |
| 81 | |
Patrick Delaunay | cda3dcb | 2018-03-19 19:09:20 +0100 | [diff] [blame] | 82 | #if !defined(CONFIG_SPL) || defined(CONFIG_SPL_BUILD) |
Patrick Delaunay | abf2678 | 2019-02-12 11:44:39 +0100 | [diff] [blame] | 83 | #ifndef CONFIG_STM32MP1_TRUSTED |
Patrick Delaunay | 2514c2d | 2018-03-12 10:46:10 +0100 | [diff] [blame] | 84 | static void security_init(void) |
| 85 | { |
| 86 | /* Disable the backup domain write protection */ |
| 87 | /* the protection is enable at each reset by hardware */ |
| 88 | /* And must be disable by software */ |
| 89 | setbits_le32(PWR_CR1, PWR_CR1_DBP); |
| 90 | |
| 91 | while (!(readl(PWR_CR1) & PWR_CR1_DBP)) |
| 92 | ; |
| 93 | |
| 94 | /* If RTC clock isn't enable so this is a cold boot then we need |
| 95 | * to reset the backup domain |
| 96 | */ |
| 97 | if (!(readl(RCC_BDCR) & RCC_BDCR_RTCSRC)) { |
| 98 | setbits_le32(RCC_BDCR, RCC_BDCR_VSWRST); |
| 99 | while (!(readl(RCC_BDCR) & RCC_BDCR_VSWRST)) |
| 100 | ; |
| 101 | clrbits_le32(RCC_BDCR, RCC_BDCR_VSWRST); |
| 102 | } |
| 103 | |
| 104 | /* allow non secure access in Write/Read for all peripheral */ |
| 105 | writel(GENMASK(25, 0), ETZPC_DECPROT0); |
| 106 | |
| 107 | /* Open SYSRAM for no secure access */ |
| 108 | writel(0x0, ETZPC_TZMA1_SIZE); |
| 109 | |
| 110 | /* enable TZC1 TZC2 clock */ |
| 111 | writel(BIT(11) | BIT(12), RCC_MP_APB5ENSETR); |
| 112 | |
| 113 | /* Region 0 set to no access by default */ |
| 114 | /* bit 0 / 16 => nsaid0 read/write Enable |
| 115 | * bit 1 / 17 => nsaid1 read/write Enable |
| 116 | * ... |
| 117 | * bit 15 / 31 => nsaid15 read/write Enable |
| 118 | */ |
| 119 | writel(0xFFFFFFFF, TZC_REGION_ID_ACCESS0); |
| 120 | /* bit 30 / 31 => Secure Global Enable : write/read */ |
| 121 | /* bit 0 / 1 => Region Enable for filter 0/1 */ |
| 122 | writel(BIT(0) | BIT(1) | BIT(30) | BIT(31), TZC_REGION_ATTRIBUTE0); |
| 123 | |
| 124 | /* Enable Filter 0 and 1 */ |
| 125 | setbits_le32(TZC_GATE_KEEPER, BIT(0) | BIT(1)); |
| 126 | |
| 127 | /* RCC trust zone deactivated */ |
| 128 | writel(0x0, RCC_TZCR); |
| 129 | |
| 130 | /* TAMP: deactivate the internal tamper |
| 131 | * Bit 23 ITAMP8E: monotonic counter overflow |
| 132 | * Bit 20 ITAMP5E: RTC calendar overflow |
| 133 | * Bit 19 ITAMP4E: HSE monitoring |
| 134 | * Bit 18 ITAMP3E: LSE monitoring |
| 135 | * Bit 16 ITAMP1E: RTC power domain supply monitoring |
| 136 | */ |
| 137 | writel(0x0, TAMP_CR1); |
| 138 | } |
Patrick Delaunay | abf2678 | 2019-02-12 11:44:39 +0100 | [diff] [blame] | 139 | #endif /* CONFIG_STM32MP1_TRUSTED */ |
Patrick Delaunay | 2514c2d | 2018-03-12 10:46:10 +0100 | [diff] [blame] | 140 | |
Patrick Delaunay | cda3dcb | 2018-03-19 19:09:20 +0100 | [diff] [blame] | 141 | /* |
Patrick Delaunay | 2514c2d | 2018-03-12 10:46:10 +0100 | [diff] [blame] | 142 | * Debug init |
Patrick Delaunay | cda3dcb | 2018-03-19 19:09:20 +0100 | [diff] [blame] | 143 | */ |
Patrick Delaunay | 2514c2d | 2018-03-12 10:46:10 +0100 | [diff] [blame] | 144 | static void dbgmcu_init(void) |
| 145 | { |
| 146 | setbits_le32(RCC_DBGCFGR, RCC_DBGCFGR_DBGCKEN); |
| 147 | |
| 148 | /* Freeze IWDG2 if Cortex-A7 is in debug mode */ |
| 149 | setbits_le32(DBGMCU_APB4FZ1, DBGMCU_APB4FZ1_IWDG2); |
| 150 | } |
| 151 | #endif /* !defined(CONFIG_SPL) || defined(CONFIG_SPL_BUILD) */ |
| 152 | |
Patrick Delaunay | abf2678 | 2019-02-12 11:44:39 +0100 | [diff] [blame] | 153 | #if !defined(CONFIG_STM32MP1_TRUSTED) && \ |
| 154 | (!defined(CONFIG_SPL) || defined(CONFIG_SPL_BUILD)) |
Patrick Delaunay | 7f63c1e | 2019-02-27 17:01:12 +0100 | [diff] [blame] | 155 | /* get bootmode from ROM code boot context: saved in TAMP register */ |
| 156 | static void update_bootmode(void) |
| 157 | { |
| 158 | u32 boot_mode; |
Patrick Delaunay | 08772f6 | 2018-03-20 10:54:53 +0100 | [diff] [blame] | 159 | u32 bootrom_itf = readl(BOOTROM_PARAM_ADDR); |
| 160 | u32 bootrom_device, bootrom_instance; |
| 161 | |
Patrick Delaunay | 7f63c1e | 2019-02-27 17:01:12 +0100 | [diff] [blame] | 162 | /* enable TAMP clock = RTCAPBEN */ |
| 163 | writel(BIT(8), RCC_MP_APB5ENSETR); |
| 164 | |
| 165 | /* read bootrom context */ |
Patrick Delaunay | 08772f6 | 2018-03-20 10:54:53 +0100 | [diff] [blame] | 166 | bootrom_device = |
| 167 | (bootrom_itf & BOOTROM_MODE_MASK) >> BOOTROM_MODE_SHIFT; |
| 168 | bootrom_instance = |
| 169 | (bootrom_itf & BOOTROM_INSTANCE_MASK) >> BOOTROM_INSTANCE_SHIFT; |
| 170 | boot_mode = |
| 171 | ((bootrom_device << BOOT_TYPE_SHIFT) & BOOT_TYPE_MASK) | |
| 172 | ((bootrom_instance << BOOT_INSTANCE_SHIFT) & |
| 173 | BOOT_INSTANCE_MASK); |
| 174 | |
| 175 | /* save the boot mode in TAMP backup register */ |
| 176 | clrsetbits_le32(TAMP_BOOT_CONTEXT, |
| 177 | TAMP_BOOT_MODE_MASK, |
| 178 | boot_mode << TAMP_BOOT_MODE_SHIFT); |
Patrick Delaunay | 7f63c1e | 2019-02-27 17:01:12 +0100 | [diff] [blame] | 179 | } |
Patrick Delaunay | 08772f6 | 2018-03-20 10:54:53 +0100 | [diff] [blame] | 180 | #endif |
Patrick Delaunay | 7f63c1e | 2019-02-27 17:01:12 +0100 | [diff] [blame] | 181 | |
| 182 | u32 get_bootmode(void) |
| 183 | { |
| 184 | /* read bootmode from TAMP backup register */ |
| 185 | return (readl(TAMP_BOOT_CONTEXT) & TAMP_BOOT_MODE_MASK) >> |
| 186 | TAMP_BOOT_MODE_SHIFT; |
Patrick Delaunay | 08772f6 | 2018-03-20 10:54:53 +0100 | [diff] [blame] | 187 | } |
| 188 | |
| 189 | /* |
| 190 | * Early system init |
| 191 | */ |
Patrick Delaunay | 2514c2d | 2018-03-12 10:46:10 +0100 | [diff] [blame] | 192 | int arch_cpu_init(void) |
| 193 | { |
Patrick Delaunay | 320d266 | 2018-05-17 14:50:46 +0200 | [diff] [blame] | 194 | u32 boot_mode; |
| 195 | |
Patrick Delaunay | 2514c2d | 2018-03-12 10:46:10 +0100 | [diff] [blame] | 196 | /* early armv7 timer init: needed for polling */ |
| 197 | timer_init(); |
| 198 | |
| 199 | #if !defined(CONFIG_SPL) || defined(CONFIG_SPL_BUILD) |
| 200 | dbgmcu_init(); |
Patrick Delaunay | abf2678 | 2019-02-12 11:44:39 +0100 | [diff] [blame] | 201 | #ifndef CONFIG_STM32MP1_TRUSTED |
Patrick Delaunay | 2514c2d | 2018-03-12 10:46:10 +0100 | [diff] [blame] | 202 | security_init(); |
Patrick Delaunay | 7f63c1e | 2019-02-27 17:01:12 +0100 | [diff] [blame] | 203 | update_bootmode(); |
Patrick Delaunay | 2514c2d | 2018-03-12 10:46:10 +0100 | [diff] [blame] | 204 | #endif |
Patrick Delaunay | abf2678 | 2019-02-12 11:44:39 +0100 | [diff] [blame] | 205 | #endif |
Patrick Delaunay | 320d266 | 2018-05-17 14:50:46 +0200 | [diff] [blame] | 206 | |
Patrick Delaunay | 320d266 | 2018-05-17 14:50:46 +0200 | [diff] [blame] | 207 | boot_mode = get_bootmode(); |
| 208 | |
| 209 | if ((boot_mode & TAMP_BOOT_DEVICE_MASK) == BOOT_SERIAL_UART) |
| 210 | gd->flags |= GD_FLG_SILENT | GD_FLG_DISABLE_CONSOLE; |
| 211 | #if defined(CONFIG_DEBUG_UART) && \ |
Patrick Delaunay | abf2678 | 2019-02-12 11:44:39 +0100 | [diff] [blame] | 212 | !defined(CONFIG_STM32MP1_TRUSTED) && \ |
Patrick Delaunay | 320d266 | 2018-05-17 14:50:46 +0200 | [diff] [blame] | 213 | (!defined(CONFIG_SPL) || defined(CONFIG_SPL_BUILD)) |
| 214 | else |
| 215 | debug_uart_init(); |
| 216 | #endif |
Patrick Delaunay | 2514c2d | 2018-03-12 10:46:10 +0100 | [diff] [blame] | 217 | |
| 218 | return 0; |
| 219 | } |
| 220 | |
Patrick Delaunay | cda3dcb | 2018-03-19 19:09:20 +0100 | [diff] [blame] | 221 | void enable_caches(void) |
| 222 | { |
| 223 | /* Enable D-cache. I-cache is already enabled in start.S */ |
| 224 | dcache_enable(); |
| 225 | } |
| 226 | |
Patrick Delaunay | 96583cd | 2018-03-19 19:09:21 +0100 | [diff] [blame] | 227 | static u32 read_idc(void) |
| 228 | { |
| 229 | setbits_le32(RCC_DBGCFGR, RCC_DBGCFGR_DBGCKEN); |
| 230 | |
| 231 | return readl(DBGMCU_IDC); |
| 232 | } |
| 233 | |
| 234 | u32 get_cpu_rev(void) |
| 235 | { |
| 236 | return (read_idc() & DBGMCU_IDC_REV_ID_MASK) >> DBGMCU_IDC_REV_ID_SHIFT; |
| 237 | } |
| 238 | |
Patrick Delaunay | 35d568f | 2019-02-27 17:01:13 +0100 | [diff] [blame] | 239 | static u32 get_otp(int index, int shift, int mask) |
| 240 | { |
| 241 | int ret; |
| 242 | struct udevice *dev; |
| 243 | u32 otp = 0; |
| 244 | |
| 245 | ret = uclass_get_device_by_driver(UCLASS_MISC, |
| 246 | DM_GET_DRIVER(stm32mp_bsec), |
| 247 | &dev); |
| 248 | |
| 249 | if (!ret) |
| 250 | ret = misc_read(dev, STM32_BSEC_SHADOW(index), |
| 251 | &otp, sizeof(otp)); |
| 252 | |
| 253 | return (otp >> shift) & mask; |
| 254 | } |
| 255 | |
| 256 | /* Get Device Part Number (RPN) from OTP */ |
| 257 | static u32 get_cpu_rpn(void) |
| 258 | { |
| 259 | return get_otp(BSEC_OTP_RPN, RPN_SHIFT, RPN_MASK); |
| 260 | } |
| 261 | |
Patrick Delaunay | 96583cd | 2018-03-19 19:09:21 +0100 | [diff] [blame] | 262 | u32 get_cpu_type(void) |
| 263 | { |
Patrick Delaunay | 35d568f | 2019-02-27 17:01:13 +0100 | [diff] [blame] | 264 | u32 id; |
| 265 | |
| 266 | id = (read_idc() & DBGMCU_IDC_DEV_ID_MASK) >> DBGMCU_IDC_DEV_ID_SHIFT; |
| 267 | |
| 268 | return (id << 16) | get_cpu_rpn(); |
| 269 | } |
| 270 | |
| 271 | /* Get Package options from OTP */ |
| 272 | static u32 get_cpu_package(void) |
| 273 | { |
| 274 | return get_otp(BSEC_OTP_PKG, PKG_SHIFT, PKG_MASK); |
Patrick Delaunay | 96583cd | 2018-03-19 19:09:21 +0100 | [diff] [blame] | 275 | } |
| 276 | |
Patrick Delaunay | 2514c2d | 2018-03-12 10:46:10 +0100 | [diff] [blame] | 277 | #if defined(CONFIG_DISPLAY_CPUINFO) |
| 278 | int print_cpuinfo(void) |
| 279 | { |
Patrick Delaunay | 35d568f | 2019-02-27 17:01:13 +0100 | [diff] [blame] | 280 | char *cpu_s, *cpu_r, *pkg; |
Patrick Delaunay | 96583cd | 2018-03-19 19:09:21 +0100 | [diff] [blame] | 281 | |
Patrick Delaunay | 35d568f | 2019-02-27 17:01:13 +0100 | [diff] [blame] | 282 | /* MPUs Part Numbers */ |
Patrick Delaunay | 96583cd | 2018-03-19 19:09:21 +0100 | [diff] [blame] | 283 | switch (get_cpu_type()) { |
Patrick Delaunay | 35d568f | 2019-02-27 17:01:13 +0100 | [diff] [blame] | 284 | case CPU_STM32MP157Cxx: |
| 285 | cpu_s = "157C"; |
| 286 | break; |
| 287 | case CPU_STM32MP157Axx: |
| 288 | cpu_s = "157A"; |
| 289 | break; |
| 290 | case CPU_STM32MP153Cxx: |
| 291 | cpu_s = "153C"; |
| 292 | break; |
| 293 | case CPU_STM32MP153Axx: |
| 294 | cpu_s = "153A"; |
| 295 | break; |
| 296 | case CPU_STM32MP151Cxx: |
| 297 | cpu_s = "151C"; |
| 298 | break; |
| 299 | case CPU_STM32MP151Axx: |
| 300 | cpu_s = "151A"; |
Patrick Delaunay | 96583cd | 2018-03-19 19:09:21 +0100 | [diff] [blame] | 301 | break; |
| 302 | default: |
Patrick Delaunay | 35d568f | 2019-02-27 17:01:13 +0100 | [diff] [blame] | 303 | cpu_s = "????"; |
Patrick Delaunay | 96583cd | 2018-03-19 19:09:21 +0100 | [diff] [blame] | 304 | break; |
| 305 | } |
| 306 | |
Patrick Delaunay | 35d568f | 2019-02-27 17:01:13 +0100 | [diff] [blame] | 307 | /* Package */ |
| 308 | switch (get_cpu_package()) { |
| 309 | case PKG_AA_LBGA448: |
| 310 | pkg = "AA"; |
| 311 | break; |
| 312 | case PKG_AB_LBGA354: |
| 313 | pkg = "AB"; |
| 314 | break; |
| 315 | case PKG_AC_TFBGA361: |
| 316 | pkg = "AC"; |
| 317 | break; |
| 318 | case PKG_AD_TFBGA257: |
| 319 | pkg = "AD"; |
| 320 | break; |
| 321 | default: |
| 322 | pkg = "??"; |
| 323 | break; |
| 324 | } |
| 325 | |
| 326 | /* REVISION */ |
Patrick Delaunay | 96583cd | 2018-03-19 19:09:21 +0100 | [diff] [blame] | 327 | switch (get_cpu_rev()) { |
| 328 | case CPU_REVA: |
| 329 | cpu_r = "A"; |
| 330 | break; |
| 331 | case CPU_REVB: |
| 332 | cpu_r = "B"; |
| 333 | break; |
| 334 | default: |
| 335 | cpu_r = "?"; |
| 336 | break; |
| 337 | } |
| 338 | |
Patrick Delaunay | 35d568f | 2019-02-27 17:01:13 +0100 | [diff] [blame] | 339 | printf("CPU: STM32MP%s%s Rev.%s\n", cpu_s, pkg, cpu_r); |
Patrick Delaunay | 2514c2d | 2018-03-12 10:46:10 +0100 | [diff] [blame] | 340 | |
| 341 | return 0; |
| 342 | } |
| 343 | #endif /* CONFIG_DISPLAY_CPUINFO */ |
| 344 | |
Patrick Delaunay | 08772f6 | 2018-03-20 10:54:53 +0100 | [diff] [blame] | 345 | static void setup_boot_mode(void) |
| 346 | { |
Patrick Delaunay | 7f63c1e | 2019-02-27 17:01:12 +0100 | [diff] [blame] | 347 | const u32 serial_addr[] = { |
| 348 | STM32_USART1_BASE, |
| 349 | STM32_USART2_BASE, |
| 350 | STM32_USART3_BASE, |
| 351 | STM32_UART4_BASE, |
| 352 | STM32_UART5_BASE, |
| 353 | STM32_USART6_BASE, |
| 354 | STM32_UART7_BASE, |
| 355 | STM32_UART8_BASE |
| 356 | }; |
Patrick Delaunay | 08772f6 | 2018-03-20 10:54:53 +0100 | [diff] [blame] | 357 | char cmd[60]; |
| 358 | u32 boot_ctx = readl(TAMP_BOOT_CONTEXT); |
| 359 | u32 boot_mode = |
| 360 | (boot_ctx & TAMP_BOOT_MODE_MASK) >> TAMP_BOOT_MODE_SHIFT; |
| 361 | int instance = (boot_mode & TAMP_BOOT_INSTANCE_MASK) - 1; |
Patrick Delaunay | 9a2ba28 | 2019-02-27 17:01:20 +0100 | [diff] [blame] | 362 | u32 forced_mode = (boot_ctx & TAMP_BOOT_FORCED_MASK); |
Patrick Delaunay | 7f63c1e | 2019-02-27 17:01:12 +0100 | [diff] [blame] | 363 | struct udevice *dev; |
| 364 | int alias; |
Patrick Delaunay | 08772f6 | 2018-03-20 10:54:53 +0100 | [diff] [blame] | 365 | |
Patrick Delaunay | 9a2ba28 | 2019-02-27 17:01:20 +0100 | [diff] [blame] | 366 | pr_debug("%s: boot_ctx=0x%x => boot_mode=%x, instance=%d forced=%x\n", |
| 367 | __func__, boot_ctx, boot_mode, instance, forced_mode); |
Patrick Delaunay | 08772f6 | 2018-03-20 10:54:53 +0100 | [diff] [blame] | 368 | switch (boot_mode & TAMP_BOOT_DEVICE_MASK) { |
| 369 | case BOOT_SERIAL_UART: |
Patrick Delaunay | 7f63c1e | 2019-02-27 17:01:12 +0100 | [diff] [blame] | 370 | if (instance > ARRAY_SIZE(serial_addr)) |
| 371 | break; |
| 372 | /* serial : search associated alias in devicetree */ |
| 373 | sprintf(cmd, "serial@%x", serial_addr[instance]); |
| 374 | if (uclass_get_device_by_name(UCLASS_SERIAL, cmd, &dev)) |
| 375 | break; |
| 376 | if (fdtdec_get_alias_seq(gd->fdt_blob, "serial", |
| 377 | dev_of_offset(dev), &alias)) |
| 378 | break; |
| 379 | sprintf(cmd, "%d", alias); |
| 380 | env_set("boot_device", "serial"); |
Patrick Delaunay | 08772f6 | 2018-03-20 10:54:53 +0100 | [diff] [blame] | 381 | env_set("boot_instance", cmd); |
Patrick Delaunay | 7f63c1e | 2019-02-27 17:01:12 +0100 | [diff] [blame] | 382 | |
| 383 | /* restore console on uart when not used */ |
| 384 | if (gd->cur_serial_dev != dev) { |
| 385 | gd->flags &= ~(GD_FLG_SILENT | |
| 386 | GD_FLG_DISABLE_CONSOLE); |
| 387 | printf("serial boot with console enabled!\n"); |
| 388 | } |
Patrick Delaunay | 08772f6 | 2018-03-20 10:54:53 +0100 | [diff] [blame] | 389 | break; |
| 390 | case BOOT_SERIAL_USB: |
| 391 | env_set("boot_device", "usb"); |
| 392 | env_set("boot_instance", "0"); |
| 393 | break; |
| 394 | case BOOT_FLASH_SD: |
| 395 | case BOOT_FLASH_EMMC: |
| 396 | sprintf(cmd, "%d", instance); |
| 397 | env_set("boot_device", "mmc"); |
| 398 | env_set("boot_instance", cmd); |
| 399 | break; |
| 400 | case BOOT_FLASH_NAND: |
| 401 | env_set("boot_device", "nand"); |
| 402 | env_set("boot_instance", "0"); |
| 403 | break; |
| 404 | case BOOT_FLASH_NOR: |
| 405 | env_set("boot_device", "nor"); |
| 406 | env_set("boot_instance", "0"); |
| 407 | break; |
| 408 | default: |
| 409 | pr_debug("unexpected boot mode = %x\n", boot_mode); |
| 410 | break; |
| 411 | } |
Patrick Delaunay | 9a2ba28 | 2019-02-27 17:01:20 +0100 | [diff] [blame] | 412 | |
| 413 | switch (forced_mode) { |
| 414 | case BOOT_FASTBOOT: |
| 415 | printf("Enter fastboot!\n"); |
| 416 | env_set("preboot", "env set preboot; fastboot 0"); |
| 417 | break; |
| 418 | case BOOT_STM32PROG: |
| 419 | env_set("boot_device", "usb"); |
| 420 | env_set("boot_instance", "0"); |
| 421 | break; |
| 422 | case BOOT_UMS_MMC0: |
| 423 | case BOOT_UMS_MMC1: |
| 424 | case BOOT_UMS_MMC2: |
| 425 | printf("Enter UMS!\n"); |
| 426 | instance = forced_mode - BOOT_UMS_MMC0; |
| 427 | sprintf(cmd, "env set preboot; ums 0 mmc %d", instance); |
| 428 | env_set("preboot", cmd); |
| 429 | break; |
| 430 | case BOOT_RECOVERY: |
| 431 | env_set("preboot", "env set preboot; run altbootcmd"); |
| 432 | break; |
| 433 | case BOOT_NORMAL: |
| 434 | break; |
| 435 | default: |
| 436 | pr_debug("unexpected forced boot mode = %x\n", forced_mode); |
| 437 | break; |
| 438 | } |
| 439 | |
| 440 | /* clear TAMP for next reboot */ |
| 441 | clrsetbits_le32(TAMP_BOOT_CONTEXT, TAMP_BOOT_FORCED_MASK, BOOT_NORMAL); |
Patrick Delaunay | 08772f6 | 2018-03-20 10:54:53 +0100 | [diff] [blame] | 442 | } |
| 443 | |
Patrick Delaunay | 7f7deb0 | 2018-05-17 15:24:07 +0200 | [diff] [blame] | 444 | /* |
| 445 | * If there is no MAC address in the environment, then it will be initialized |
| 446 | * (silently) from the value in the OTP. |
| 447 | */ |
| 448 | static int setup_mac_address(void) |
| 449 | { |
| 450 | #if defined(CONFIG_NET) |
| 451 | int ret; |
| 452 | int i; |
| 453 | u32 otp[2]; |
| 454 | uchar enetaddr[6]; |
| 455 | struct udevice *dev; |
| 456 | |
| 457 | /* MAC already in environment */ |
| 458 | if (eth_env_get_enetaddr("ethaddr", enetaddr)) |
| 459 | return 0; |
| 460 | |
| 461 | ret = uclass_get_device_by_driver(UCLASS_MISC, |
| 462 | DM_GET_DRIVER(stm32mp_bsec), |
| 463 | &dev); |
| 464 | if (ret) |
| 465 | return ret; |
| 466 | |
| 467 | ret = misc_read(dev, BSEC_OTP_MAC * 4 + STM32_BSEC_OTP_OFFSET, |
| 468 | otp, sizeof(otp)); |
Simon Glass | 8729b1a | 2018-11-06 15:21:39 -0700 | [diff] [blame] | 469 | if (ret < 0) |
Patrick Delaunay | 7f7deb0 | 2018-05-17 15:24:07 +0200 | [diff] [blame] | 470 | return ret; |
| 471 | |
| 472 | for (i = 0; i < 6; i++) |
| 473 | enetaddr[i] = ((uint8_t *)&otp)[i]; |
| 474 | |
| 475 | if (!is_valid_ethaddr(enetaddr)) { |
| 476 | pr_err("invalid MAC address in OTP %pM", enetaddr); |
| 477 | return -EINVAL; |
| 478 | } |
| 479 | pr_debug("OTP MAC address = %pM\n", enetaddr); |
| 480 | ret = !eth_env_set_enetaddr("ethaddr", enetaddr); |
| 481 | if (!ret) |
| 482 | pr_err("Failed to set mac address %pM from OTP: %d\n", |
| 483 | enetaddr, ret); |
| 484 | #endif |
| 485 | |
| 486 | return 0; |
| 487 | } |
| 488 | |
| 489 | static int setup_serial_number(void) |
| 490 | { |
| 491 | char serial_string[25]; |
| 492 | u32 otp[3] = {0, 0, 0 }; |
| 493 | struct udevice *dev; |
| 494 | int ret; |
| 495 | |
| 496 | if (env_get("serial#")) |
| 497 | return 0; |
| 498 | |
| 499 | ret = uclass_get_device_by_driver(UCLASS_MISC, |
| 500 | DM_GET_DRIVER(stm32mp_bsec), |
| 501 | &dev); |
| 502 | if (ret) |
| 503 | return ret; |
| 504 | |
| 505 | ret = misc_read(dev, BSEC_OTP_SERIAL * 4 + STM32_BSEC_OTP_OFFSET, |
| 506 | otp, sizeof(otp)); |
Simon Glass | 8729b1a | 2018-11-06 15:21:39 -0700 | [diff] [blame] | 507 | if (ret < 0) |
Patrick Delaunay | 7f7deb0 | 2018-05-17 15:24:07 +0200 | [diff] [blame] | 508 | return ret; |
| 509 | |
Patrick Delaunay | 8983ba2 | 2019-02-27 17:01:25 +0100 | [diff] [blame^] | 510 | sprintf(serial_string, "%08X%08X%08X", otp[0], otp[1], otp[2]); |
Patrick Delaunay | 7f7deb0 | 2018-05-17 15:24:07 +0200 | [diff] [blame] | 511 | env_set("serial#", serial_string); |
| 512 | |
| 513 | return 0; |
| 514 | } |
| 515 | |
Patrick Delaunay | 08772f6 | 2018-03-20 10:54:53 +0100 | [diff] [blame] | 516 | int arch_misc_init(void) |
| 517 | { |
| 518 | setup_boot_mode(); |
Patrick Delaunay | 7f7deb0 | 2018-05-17 15:24:07 +0200 | [diff] [blame] | 519 | setup_mac_address(); |
| 520 | setup_serial_number(); |
Patrick Delaunay | 08772f6 | 2018-03-20 10:54:53 +0100 | [diff] [blame] | 521 | |
| 522 | return 0; |
| 523 | } |