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