Yann Gautier | ee8f542 | 2019-02-14 11:13:25 +0100 | [diff] [blame] | 1 | /* |
Yann Gautier | 352d863 | 2020-09-17 11:38:09 +0200 | [diff] [blame] | 2 | * Copyright (c) 2015-2022, ARM Limited and Contributors. All rights reserved. |
Yann Gautier | ee8f542 | 2019-02-14 11:13:25 +0100 | [diff] [blame] | 3 | * |
| 4 | * SPDX-License-Identifier: BSD-3-Clause |
| 5 | */ |
| 6 | |
Yann Gautier | e3bf913 | 2019-05-07 18:52:17 +0200 | [diff] [blame] | 7 | #include <assert.h> |
| 8 | |
Yann Gautier | a205a5c | 2021-08-30 15:06:54 +0200 | [diff] [blame] | 9 | #include <drivers/clk.h> |
Yann Gautier | cd16df3 | 2021-06-04 14:04:05 +0200 | [diff] [blame] | 10 | #include <drivers/st/stm32_gpio.h> |
| 11 | #include <drivers/st/stm32_iwdg.h> |
Yann Gautier | 6eef525 | 2021-12-10 17:04:40 +0100 | [diff] [blame] | 12 | #include <lib/mmio.h> |
Yann Gautier | cd16df3 | 2021-06-04 14:04:05 +0200 | [diff] [blame] | 13 | #include <lib/xlat_tables/xlat_tables_v2.h> |
Yann Gautier | 0c81088 | 2021-12-17 09:53:04 +0100 | [diff] [blame] | 14 | #include <libfdt.h> |
Yann Gautier | 35dc077 | 2019-05-13 18:34:48 +0200 | [diff] [blame] | 15 | |
Sughosh Ganu | 03e2f80 | 2021-12-01 15:56:27 +0530 | [diff] [blame] | 16 | #include <plat/common/platform.h> |
Yann Gautier | ee8f542 | 2019-02-14 11:13:25 +0100 | [diff] [blame] | 17 | #include <platform_def.h> |
| 18 | |
Yann Gautier | 35dc077 | 2019-05-13 18:34:48 +0200 | [diff] [blame] | 19 | /* Internal layout of the 32bit OTP word board_id */ |
| 20 | #define BOARD_ID_BOARD_NB_MASK GENMASK(31, 16) |
| 21 | #define BOARD_ID_BOARD_NB_SHIFT 16 |
Patrick Delaunay | 7704f16 | 2020-01-08 10:05:14 +0100 | [diff] [blame] | 22 | #define BOARD_ID_VARCPN_MASK GENMASK(15, 12) |
| 23 | #define BOARD_ID_VARCPN_SHIFT 12 |
Yann Gautier | 35dc077 | 2019-05-13 18:34:48 +0200 | [diff] [blame] | 24 | #define BOARD_ID_REVISION_MASK GENMASK(11, 8) |
| 25 | #define BOARD_ID_REVISION_SHIFT 8 |
Patrick Delaunay | 7704f16 | 2020-01-08 10:05:14 +0100 | [diff] [blame] | 26 | #define BOARD_ID_VARFG_MASK GENMASK(7, 4) |
| 27 | #define BOARD_ID_VARFG_SHIFT 4 |
Yann Gautier | 35dc077 | 2019-05-13 18:34:48 +0200 | [diff] [blame] | 28 | #define BOARD_ID_BOM_MASK GENMASK(3, 0) |
| 29 | |
| 30 | #define BOARD_ID2NB(_id) (((_id) & BOARD_ID_BOARD_NB_MASK) >> \ |
| 31 | BOARD_ID_BOARD_NB_SHIFT) |
Patrick Delaunay | 7704f16 | 2020-01-08 10:05:14 +0100 | [diff] [blame] | 32 | #define BOARD_ID2VARCPN(_id) (((_id) & BOARD_ID_VARCPN_MASK) >> \ |
| 33 | BOARD_ID_VARCPN_SHIFT) |
Yann Gautier | 35dc077 | 2019-05-13 18:34:48 +0200 | [diff] [blame] | 34 | #define BOARD_ID2REV(_id) (((_id) & BOARD_ID_REVISION_MASK) >> \ |
| 35 | BOARD_ID_REVISION_SHIFT) |
Patrick Delaunay | 7704f16 | 2020-01-08 10:05:14 +0100 | [diff] [blame] | 36 | #define BOARD_ID2VARFG(_id) (((_id) & BOARD_ID_VARFG_MASK) >> \ |
| 37 | BOARD_ID_VARFG_SHIFT) |
Yann Gautier | 35dc077 | 2019-05-13 18:34:48 +0200 | [diff] [blame] | 38 | #define BOARD_ID2BOM(_id) ((_id) & BOARD_ID_BOM_MASK) |
| 39 | |
Yann Gautier | 6eef525 | 2021-12-10 17:04:40 +0100 | [diff] [blame] | 40 | #define TAMP_BOOT_MODE_BACKUP_REG_ID U(20) |
| 41 | #define TAMP_BOOT_MODE_ITF_MASK U(0x0000FF00) |
| 42 | #define TAMP_BOOT_MODE_ITF_SHIFT 8 |
| 43 | |
Sughosh Ganu | 03e2f80 | 2021-12-01 15:56:27 +0530 | [diff] [blame] | 44 | #define TAMP_BOOT_COUNTER_REG_ID U(21) |
| 45 | |
Etienne Carriere | 72369b1 | 2019-12-08 08:17:56 +0100 | [diff] [blame] | 46 | #if defined(IMAGE_BL2) |
| 47 | #define MAP_SEC_SYSRAM MAP_REGION_FLAT(STM32MP_SYSRAM_BASE, \ |
Yann Gautier | a2e2a30 | 2019-02-14 11:13:39 +0100 | [diff] [blame] | 48 | STM32MP_SYSRAM_SIZE, \ |
Yann Gautier | ee8f542 | 2019-02-14 11:13:25 +0100 | [diff] [blame] | 49 | MT_MEMORY | \ |
| 50 | MT_RW | \ |
| 51 | MT_SECURE | \ |
| 52 | MT_EXECUTE_NEVER) |
Etienne Carriere | 72369b1 | 2019-12-08 08:17:56 +0100 | [diff] [blame] | 53 | #elif defined(IMAGE_BL32) |
| 54 | #define MAP_SEC_SYSRAM MAP_REGION_FLAT(STM32MP_SEC_SYSRAM_BASE, \ |
| 55 | STM32MP_SEC_SYSRAM_SIZE, \ |
| 56 | MT_MEMORY | \ |
| 57 | MT_RW | \ |
| 58 | MT_SECURE | \ |
| 59 | MT_EXECUTE_NEVER) |
Yann Gautier | ee8f542 | 2019-02-14 11:13:25 +0100 | [diff] [blame] | 60 | |
Etienne Carriere | 72369b1 | 2019-12-08 08:17:56 +0100 | [diff] [blame] | 61 | /* Non-secure SYSRAM is used a uncached memory for SCMI message transfer */ |
| 62 | #define MAP_NS_SYSRAM MAP_REGION_FLAT(STM32MP_NS_SYSRAM_BASE, \ |
| 63 | STM32MP_NS_SYSRAM_SIZE, \ |
| 64 | MT_DEVICE | \ |
| 65 | MT_RW | \ |
| 66 | MT_NS | \ |
| 67 | MT_EXECUTE_NEVER) |
| 68 | #endif |
| 69 | |
Yann Gautier | ee8f542 | 2019-02-14 11:13:25 +0100 | [diff] [blame] | 70 | #define MAP_DEVICE1 MAP_REGION_FLAT(STM32MP1_DEVICE1_BASE, \ |
| 71 | STM32MP1_DEVICE1_SIZE, \ |
| 72 | MT_DEVICE | \ |
| 73 | MT_RW | \ |
| 74 | MT_SECURE | \ |
| 75 | MT_EXECUTE_NEVER) |
| 76 | |
| 77 | #define MAP_DEVICE2 MAP_REGION_FLAT(STM32MP1_DEVICE2_BASE, \ |
| 78 | STM32MP1_DEVICE2_SIZE, \ |
| 79 | MT_DEVICE | \ |
| 80 | MT_RW | \ |
| 81 | MT_SECURE | \ |
| 82 | MT_EXECUTE_NEVER) |
| 83 | |
| 84 | #if defined(IMAGE_BL2) |
| 85 | static const mmap_region_t stm32mp1_mmap[] = { |
Etienne Carriere | 72369b1 | 2019-12-08 08:17:56 +0100 | [diff] [blame] | 86 | MAP_SEC_SYSRAM, |
Yann Gautier | ee8f542 | 2019-02-14 11:13:25 +0100 | [diff] [blame] | 87 | MAP_DEVICE1, |
Yann Gautier | 352d863 | 2020-09-17 11:38:09 +0200 | [diff] [blame] | 88 | #if STM32MP_RAW_NAND |
Yann Gautier | ee8f542 | 2019-02-14 11:13:25 +0100 | [diff] [blame] | 89 | MAP_DEVICE2, |
Yann Gautier | 352d863 | 2020-09-17 11:38:09 +0200 | [diff] [blame] | 90 | #endif |
Yann Gautier | ee8f542 | 2019-02-14 11:13:25 +0100 | [diff] [blame] | 91 | {0} |
| 92 | }; |
| 93 | #endif |
| 94 | #if defined(IMAGE_BL32) |
| 95 | static const mmap_region_t stm32mp1_mmap[] = { |
Etienne Carriere | 72369b1 | 2019-12-08 08:17:56 +0100 | [diff] [blame] | 96 | MAP_SEC_SYSRAM, |
| 97 | MAP_NS_SYSRAM, |
Yann Gautier | ee8f542 | 2019-02-14 11:13:25 +0100 | [diff] [blame] | 98 | MAP_DEVICE1, |
| 99 | MAP_DEVICE2, |
| 100 | {0} |
| 101 | }; |
| 102 | #endif |
| 103 | |
| 104 | void configure_mmu(void) |
| 105 | { |
| 106 | mmap_add(stm32mp1_mmap); |
| 107 | init_xlat_tables(); |
| 108 | |
| 109 | enable_mmu_svc_mon(0); |
| 110 | } |
Yann Gautier | e3bf913 | 2019-05-07 18:52:17 +0200 | [diff] [blame] | 111 | |
Etienne Carriere | 66b0452 | 2019-12-02 10:05:02 +0100 | [diff] [blame] | 112 | uintptr_t stm32_get_gpio_bank_base(unsigned int bank) |
| 113 | { |
| 114 | if (bank == GPIO_BANK_Z) { |
| 115 | return GPIOZ_BASE; |
| 116 | } |
| 117 | |
| 118 | assert(GPIO_BANK_A == 0 && bank <= GPIO_BANK_K); |
| 119 | |
| 120 | return GPIOA_BASE + (bank * GPIO_BANK_OFFSET); |
| 121 | } |
| 122 | |
| 123 | uint32_t stm32_get_gpio_bank_offset(unsigned int bank) |
| 124 | { |
| 125 | if (bank == GPIO_BANK_Z) { |
| 126 | return 0; |
| 127 | } |
| 128 | |
| 129 | assert(GPIO_BANK_A == 0 && bank <= GPIO_BANK_K); |
| 130 | |
| 131 | return bank * GPIO_BANK_OFFSET; |
| 132 | } |
| 133 | |
Yann Gautier | 2b79c37 | 2021-06-11 10:54:56 +0200 | [diff] [blame] | 134 | bool stm32_gpio_is_secure_at_reset(unsigned int bank) |
| 135 | { |
| 136 | if (bank == GPIO_BANK_Z) { |
| 137 | return true; |
| 138 | } |
| 139 | |
| 140 | return false; |
| 141 | } |
| 142 | |
Yann Gautier | e3bf913 | 2019-05-07 18:52:17 +0200 | [diff] [blame] | 143 | unsigned long stm32_get_gpio_bank_clock(unsigned int bank) |
| 144 | { |
| 145 | if (bank == GPIO_BANK_Z) { |
| 146 | return GPIOZ; |
| 147 | } |
| 148 | |
| 149 | assert(GPIO_BANK_A == 0 && bank <= GPIO_BANK_K); |
| 150 | |
| 151 | return GPIOA + (bank - GPIO_BANK_A); |
| 152 | } |
Yann Gautier | 091eab5 | 2019-06-04 18:06:34 +0200 | [diff] [blame] | 153 | |
Etienne Carriere | d81dadf | 2020-04-25 11:14:45 +0200 | [diff] [blame] | 154 | int stm32_get_gpio_bank_pinctrl_node(void *fdt, unsigned int bank) |
| 155 | { |
| 156 | switch (bank) { |
| 157 | case GPIO_BANK_A: |
| 158 | case GPIO_BANK_B: |
| 159 | case GPIO_BANK_C: |
| 160 | case GPIO_BANK_D: |
| 161 | case GPIO_BANK_E: |
| 162 | case GPIO_BANK_F: |
| 163 | case GPIO_BANK_G: |
| 164 | case GPIO_BANK_H: |
| 165 | case GPIO_BANK_I: |
| 166 | case GPIO_BANK_J: |
| 167 | case GPIO_BANK_K: |
| 168 | return fdt_path_offset(fdt, "/soc/pin-controller"); |
| 169 | case GPIO_BANK_Z: |
| 170 | return fdt_path_offset(fdt, "/soc/pin-controller-z"); |
| 171 | default: |
| 172 | panic(); |
| 173 | } |
| 174 | } |
| 175 | |
Yann Gautier | 3d8497c | 2021-10-18 16:06:22 +0200 | [diff] [blame] | 176 | #if STM32MP_UART_PROGRAMMER || !defined(IMAGE_BL2) |
Patrick Delaunay | e50571b | 2021-10-28 13:48:52 +0200 | [diff] [blame] | 177 | /* |
| 178 | * UART Management |
| 179 | */ |
| 180 | static const uintptr_t stm32mp1_uart_addresses[8] = { |
| 181 | USART1_BASE, |
| 182 | USART2_BASE, |
| 183 | USART3_BASE, |
| 184 | UART4_BASE, |
| 185 | UART5_BASE, |
| 186 | USART6_BASE, |
| 187 | UART7_BASE, |
| 188 | UART8_BASE, |
| 189 | }; |
| 190 | |
| 191 | uintptr_t get_uart_address(uint32_t instance_nb) |
| 192 | { |
| 193 | if ((instance_nb == 0U) || |
| 194 | (instance_nb > ARRAY_SIZE(stm32mp1_uart_addresses))) { |
| 195 | return 0U; |
| 196 | } |
| 197 | |
| 198 | return stm32mp1_uart_addresses[instance_nb - 1U]; |
| 199 | } |
| 200 | #endif |
| 201 | |
Yann Gautier | cd16df3 | 2021-06-04 14:04:05 +0200 | [diff] [blame] | 202 | #if STM32MP_USB_PROGRAMMER |
| 203 | struct gpio_bank_pin_list { |
| 204 | uint32_t bank; |
| 205 | uint32_t pin; |
| 206 | }; |
| 207 | |
| 208 | static const struct gpio_bank_pin_list gpio_list[] = { |
| 209 | { /* USART2_RX: GPIOA3 */ |
| 210 | .bank = 0U, |
| 211 | .pin = 3U, |
| 212 | }, |
| 213 | { /* USART3_RX: GPIOB12 */ |
| 214 | .bank = 1U, |
| 215 | .pin = 12U, |
| 216 | }, |
| 217 | { /* UART4_RX: GPIOB2 */ |
| 218 | .bank = 1U, |
| 219 | .pin = 2U, |
| 220 | }, |
| 221 | { /* UART5_RX: GPIOB4 */ |
| 222 | .bank = 1U, |
| 223 | .pin = 5U, |
| 224 | }, |
| 225 | { /* USART6_RX: GPIOC7 */ |
| 226 | .bank = 2U, |
| 227 | .pin = 7U, |
| 228 | }, |
| 229 | { /* UART7_RX: GPIOF6 */ |
| 230 | .bank = 5U, |
| 231 | .pin = 6U, |
| 232 | }, |
| 233 | { /* UART8_RX: GPIOE0 */ |
| 234 | .bank = 4U, |
| 235 | .pin = 0U, |
| 236 | }, |
| 237 | }; |
| 238 | |
| 239 | void stm32mp1_deconfigure_uart_pins(void) |
| 240 | { |
| 241 | size_t i; |
| 242 | |
| 243 | for (i = 0U; i < ARRAY_SIZE(gpio_list); i++) { |
| 244 | set_gpio_reset_cfg(gpio_list[i].bank, gpio_list[i].pin); |
| 245 | } |
| 246 | } |
| 247 | #endif |
| 248 | |
Yann Gautier | a0a6ff6 | 2021-05-10 16:05:18 +0200 | [diff] [blame] | 249 | uint32_t stm32mp_get_chip_version(void) |
Yann Gautier | c737405 | 2019-06-04 18:02:37 +0200 | [diff] [blame] | 250 | { |
Yann Gautier | a0a6ff6 | 2021-05-10 16:05:18 +0200 | [diff] [blame] | 251 | uint32_t version = 0U; |
| 252 | |
| 253 | if (stm32mp1_dbgmcu_get_chip_version(&version) < 0) { |
| 254 | INFO("Cannot get CPU version, debug disabled\n"); |
| 255 | return 0U; |
| 256 | } |
| 257 | |
| 258 | return version; |
| 259 | } |
Yann Gautier | c737405 | 2019-06-04 18:02:37 +0200 | [diff] [blame] | 260 | |
Yann Gautier | a0a6ff6 | 2021-05-10 16:05:18 +0200 | [diff] [blame] | 261 | uint32_t stm32mp_get_chip_dev_id(void) |
| 262 | { |
| 263 | uint32_t dev_id; |
Nicolas Le Bayon | 98f4ea0 | 2019-09-23 11:18:32 +0200 | [diff] [blame] | 264 | |
Yann Gautier | c737405 | 2019-06-04 18:02:37 +0200 | [diff] [blame] | 265 | if (stm32mp1_dbgmcu_get_chip_dev_id(&dev_id) < 0) { |
Yann Gautier | a0a6ff6 | 2021-05-10 16:05:18 +0200 | [diff] [blame] | 266 | INFO("Use default chip ID, debug disabled\n"); |
| 267 | dev_id = STM32MP1_CHIP_ID; |
Yann Gautier | c737405 | 2019-06-04 18:02:37 +0200 | [diff] [blame] | 268 | } |
| 269 | |
Yann Gautier | a0a6ff6 | 2021-05-10 16:05:18 +0200 | [diff] [blame] | 270 | return dev_id; |
| 271 | } |
| 272 | |
| 273 | static uint32_t get_part_number(void) |
| 274 | { |
| 275 | static uint32_t part_number; |
| 276 | |
| 277 | if (part_number != 0U) { |
| 278 | return part_number; |
| 279 | } |
| 280 | |
Yann Gautier | c737405 | 2019-06-04 18:02:37 +0200 | [diff] [blame] | 281 | if (bsec_shadow_read_otp(&part_number, PART_NUMBER_OTP) != BSEC_OK) { |
Yann Gautier | a0a6ff6 | 2021-05-10 16:05:18 +0200 | [diff] [blame] | 282 | panic(); |
Yann Gautier | c737405 | 2019-06-04 18:02:37 +0200 | [diff] [blame] | 283 | } |
| 284 | |
| 285 | part_number = (part_number & PART_NUMBER_OTP_PART_MASK) >> |
| 286 | PART_NUMBER_OTP_PART_SHIFT; |
| 287 | |
Yann Gautier | a0a6ff6 | 2021-05-10 16:05:18 +0200 | [diff] [blame] | 288 | part_number |= stm32mp_get_chip_dev_id() << 16; |
Yann Gautier | c737405 | 2019-06-04 18:02:37 +0200 | [diff] [blame] | 289 | |
Yann Gautier | a0a6ff6 | 2021-05-10 16:05:18 +0200 | [diff] [blame] | 290 | return part_number; |
Yann Gautier | c737405 | 2019-06-04 18:02:37 +0200 | [diff] [blame] | 291 | } |
| 292 | |
Yann Gautier | a0a6ff6 | 2021-05-10 16:05:18 +0200 | [diff] [blame] | 293 | static uint32_t get_cpu_package(void) |
Yann Gautier | c737405 | 2019-06-04 18:02:37 +0200 | [diff] [blame] | 294 | { |
| 295 | uint32_t package; |
| 296 | |
| 297 | if (bsec_shadow_read_otp(&package, PACKAGE_OTP) != BSEC_OK) { |
Yann Gautier | a0a6ff6 | 2021-05-10 16:05:18 +0200 | [diff] [blame] | 298 | panic(); |
Yann Gautier | c737405 | 2019-06-04 18:02:37 +0200 | [diff] [blame] | 299 | } |
| 300 | |
Yann Gautier | a0a6ff6 | 2021-05-10 16:05:18 +0200 | [diff] [blame] | 301 | package = (package & PACKAGE_OTP_PKG_MASK) >> |
Yann Gautier | c737405 | 2019-06-04 18:02:37 +0200 | [diff] [blame] | 302 | PACKAGE_OTP_PKG_SHIFT; |
| 303 | |
Yann Gautier | a0a6ff6 | 2021-05-10 16:05:18 +0200 | [diff] [blame] | 304 | return package; |
Yann Gautier | c737405 | 2019-06-04 18:02:37 +0200 | [diff] [blame] | 305 | } |
| 306 | |
Yann Gautier | a0a6ff6 | 2021-05-10 16:05:18 +0200 | [diff] [blame] | 307 | void stm32mp_get_soc_name(char name[STM32_SOC_NAME_SIZE]) |
Yann Gautier | c737405 | 2019-06-04 18:02:37 +0200 | [diff] [blame] | 308 | { |
Yann Gautier | a0a6ff6 | 2021-05-10 16:05:18 +0200 | [diff] [blame] | 309 | char *cpu_s, *cpu_r, *pkg; |
Yann Gautier | c737405 | 2019-06-04 18:02:37 +0200 | [diff] [blame] | 310 | |
| 311 | /* MPUs Part Numbers */ |
Yann Gautier | a0a6ff6 | 2021-05-10 16:05:18 +0200 | [diff] [blame] | 312 | switch (get_part_number()) { |
Yann Gautier | c737405 | 2019-06-04 18:02:37 +0200 | [diff] [blame] | 313 | case STM32MP157C_PART_NB: |
| 314 | cpu_s = "157C"; |
| 315 | break; |
| 316 | case STM32MP157A_PART_NB: |
| 317 | cpu_s = "157A"; |
| 318 | break; |
| 319 | case STM32MP153C_PART_NB: |
| 320 | cpu_s = "153C"; |
| 321 | break; |
| 322 | case STM32MP153A_PART_NB: |
| 323 | cpu_s = "153A"; |
| 324 | break; |
| 325 | case STM32MP151C_PART_NB: |
| 326 | cpu_s = "151C"; |
| 327 | break; |
| 328 | case STM32MP151A_PART_NB: |
| 329 | cpu_s = "151A"; |
| 330 | break; |
Lionel Debieve | 7b64e3e | 2019-05-17 16:01:18 +0200 | [diff] [blame] | 331 | case STM32MP157F_PART_NB: |
| 332 | cpu_s = "157F"; |
| 333 | break; |
| 334 | case STM32MP157D_PART_NB: |
| 335 | cpu_s = "157D"; |
| 336 | break; |
| 337 | case STM32MP153F_PART_NB: |
| 338 | cpu_s = "153F"; |
| 339 | break; |
| 340 | case STM32MP153D_PART_NB: |
| 341 | cpu_s = "153D"; |
| 342 | break; |
| 343 | case STM32MP151F_PART_NB: |
| 344 | cpu_s = "151F"; |
| 345 | break; |
| 346 | case STM32MP151D_PART_NB: |
| 347 | cpu_s = "151D"; |
| 348 | break; |
Yann Gautier | c737405 | 2019-06-04 18:02:37 +0200 | [diff] [blame] | 349 | default: |
| 350 | cpu_s = "????"; |
| 351 | break; |
| 352 | } |
| 353 | |
| 354 | /* Package */ |
Yann Gautier | a0a6ff6 | 2021-05-10 16:05:18 +0200 | [diff] [blame] | 355 | switch (get_cpu_package()) { |
Yann Gautier | c737405 | 2019-06-04 18:02:37 +0200 | [diff] [blame] | 356 | case PKG_AA_LFBGA448: |
| 357 | pkg = "AA"; |
| 358 | break; |
| 359 | case PKG_AB_LFBGA354: |
| 360 | pkg = "AB"; |
| 361 | break; |
| 362 | case PKG_AC_TFBGA361: |
| 363 | pkg = "AC"; |
| 364 | break; |
| 365 | case PKG_AD_TFBGA257: |
| 366 | pkg = "AD"; |
| 367 | break; |
| 368 | default: |
| 369 | pkg = "??"; |
| 370 | break; |
| 371 | } |
| 372 | |
| 373 | /* REVISION */ |
Yann Gautier | a0a6ff6 | 2021-05-10 16:05:18 +0200 | [diff] [blame] | 374 | switch (stm32mp_get_chip_version()) { |
Yann Gautier | c737405 | 2019-06-04 18:02:37 +0200 | [diff] [blame] | 375 | case STM32MP1_REV_B: |
| 376 | cpu_r = "B"; |
| 377 | break; |
Lionel Debieve | 2d64b53 | 2019-06-25 10:40:37 +0200 | [diff] [blame] | 378 | case STM32MP1_REV_Z: |
| 379 | cpu_r = "Z"; |
| 380 | break; |
Yann Gautier | c737405 | 2019-06-04 18:02:37 +0200 | [diff] [blame] | 381 | default: |
| 382 | cpu_r = "?"; |
| 383 | break; |
| 384 | } |
| 385 | |
Yann Gautier | a0a6ff6 | 2021-05-10 16:05:18 +0200 | [diff] [blame] | 386 | snprintf(name, STM32_SOC_NAME_SIZE, |
| 387 | "STM32MP%s%s Rev.%s", cpu_s, pkg, cpu_r); |
| 388 | } |
| 389 | |
| 390 | void stm32mp_print_cpuinfo(void) |
| 391 | { |
| 392 | char name[STM32_SOC_NAME_SIZE]; |
| 393 | |
| 394 | stm32mp_get_soc_name(name); |
| 395 | NOTICE("CPU: %s\n", name); |
Yann Gautier | c737405 | 2019-06-04 18:02:37 +0200 | [diff] [blame] | 396 | } |
| 397 | |
Yann Gautier | 35dc077 | 2019-05-13 18:34:48 +0200 | [diff] [blame] | 398 | void stm32mp_print_boardinfo(void) |
| 399 | { |
| 400 | uint32_t board_id; |
| 401 | uint32_t board_otp; |
| 402 | int bsec_node, bsec_board_id_node; |
| 403 | void *fdt; |
| 404 | const fdt32_t *cuint; |
| 405 | |
| 406 | if (fdt_get_address(&fdt) == 0) { |
| 407 | panic(); |
| 408 | } |
| 409 | |
| 410 | bsec_node = fdt_node_offset_by_compatible(fdt, -1, DT_BSEC_COMPAT); |
| 411 | if (bsec_node < 0) { |
| 412 | return; |
| 413 | } |
| 414 | |
| 415 | bsec_board_id_node = fdt_subnode_offset(fdt, bsec_node, "board_id"); |
| 416 | if (bsec_board_id_node <= 0) { |
| 417 | return; |
| 418 | } |
| 419 | |
| 420 | cuint = fdt_getprop(fdt, bsec_board_id_node, "reg", NULL); |
| 421 | if (cuint == NULL) { |
| 422 | panic(); |
| 423 | } |
| 424 | |
| 425 | board_otp = fdt32_to_cpu(*cuint) / sizeof(uint32_t); |
| 426 | |
| 427 | if (bsec_shadow_read_otp(&board_id, board_otp) != BSEC_OK) { |
| 428 | ERROR("BSEC: PART_NUMBER_OTP Error\n"); |
| 429 | return; |
| 430 | } |
| 431 | |
| 432 | if (board_id != 0U) { |
| 433 | char rev[2]; |
| 434 | |
| 435 | rev[0] = BOARD_ID2REV(board_id) - 1 + 'A'; |
| 436 | rev[1] = '\0'; |
Yann Gautier | 36e9d38 | 2020-10-13 18:03:31 +0200 | [diff] [blame] | 437 | NOTICE("Board: MB%04x Var%u.%u Rev.%s-%02u\n", |
Yann Gautier | 35dc077 | 2019-05-13 18:34:48 +0200 | [diff] [blame] | 438 | BOARD_ID2NB(board_id), |
Patrick Delaunay | 7704f16 | 2020-01-08 10:05:14 +0100 | [diff] [blame] | 439 | BOARD_ID2VARCPN(board_id), |
| 440 | BOARD_ID2VARFG(board_id), |
Yann Gautier | 35dc077 | 2019-05-13 18:34:48 +0200 | [diff] [blame] | 441 | rev, |
| 442 | BOARD_ID2BOM(board_id)); |
| 443 | } |
| 444 | } |
| 445 | |
Yann Gautier | af19ff9 | 2019-06-04 18:23:10 +0200 | [diff] [blame] | 446 | /* Return true when SoC provides a single Cortex-A7 core, and false otherwise */ |
| 447 | bool stm32mp_is_single_core(void) |
| 448 | { |
Yann Gautier | a0a6ff6 | 2021-05-10 16:05:18 +0200 | [diff] [blame] | 449 | switch (get_part_number()) { |
Yann Gautier | af19ff9 | 2019-06-04 18:23:10 +0200 | [diff] [blame] | 450 | case STM32MP151A_PART_NB: |
| 451 | case STM32MP151C_PART_NB: |
Lionel Debieve | 7b64e3e | 2019-05-17 16:01:18 +0200 | [diff] [blame] | 452 | case STM32MP151D_PART_NB: |
| 453 | case STM32MP151F_PART_NB: |
| 454 | return true; |
Yann Gautier | af19ff9 | 2019-06-04 18:23:10 +0200 | [diff] [blame] | 455 | default: |
Lionel Debieve | 7b64e3e | 2019-05-17 16:01:18 +0200 | [diff] [blame] | 456 | return false; |
Yann Gautier | af19ff9 | 2019-06-04 18:23:10 +0200 | [diff] [blame] | 457 | } |
Yann Gautier | af19ff9 | 2019-06-04 18:23:10 +0200 | [diff] [blame] | 458 | } |
| 459 | |
Lionel Debieve | 0e73d73 | 2019-09-16 12:17:09 +0200 | [diff] [blame] | 460 | /* Return true when device is in closed state */ |
| 461 | bool stm32mp_is_closed_device(void) |
| 462 | { |
| 463 | uint32_t value; |
| 464 | |
| 465 | if ((bsec_shadow_register(DATA0_OTP) != BSEC_OK) || |
| 466 | (bsec_read_otp(&value, DATA0_OTP) != BSEC_OK)) { |
| 467 | return true; |
| 468 | } |
| 469 | |
| 470 | return (value & DATA0_OTP_SECURED) == DATA0_OTP_SECURED; |
| 471 | } |
| 472 | |
Yann Gautier | 091eab5 | 2019-06-04 18:06:34 +0200 | [diff] [blame] | 473 | uint32_t stm32_iwdg_get_instance(uintptr_t base) |
| 474 | { |
| 475 | switch (base) { |
| 476 | case IWDG1_BASE: |
| 477 | return IWDG1_INST; |
| 478 | case IWDG2_BASE: |
| 479 | return IWDG2_INST; |
| 480 | default: |
| 481 | panic(); |
| 482 | } |
| 483 | } |
| 484 | |
| 485 | uint32_t stm32_iwdg_get_otp_config(uint32_t iwdg_inst) |
| 486 | { |
| 487 | uint32_t iwdg_cfg = 0U; |
| 488 | uint32_t otp_value; |
| 489 | |
| 490 | #if defined(IMAGE_BL2) |
| 491 | if (bsec_shadow_register(HW2_OTP) != BSEC_OK) { |
| 492 | panic(); |
| 493 | } |
| 494 | #endif |
| 495 | |
| 496 | if (bsec_read_otp(&otp_value, HW2_OTP) != BSEC_OK) { |
| 497 | panic(); |
| 498 | } |
| 499 | |
| 500 | if ((otp_value & BIT(iwdg_inst + HW2_OTP_IWDG_HW_POS)) != 0U) { |
| 501 | iwdg_cfg |= IWDG_HW_ENABLED; |
| 502 | } |
| 503 | |
| 504 | if ((otp_value & BIT(iwdg_inst + HW2_OTP_IWDG_FZ_STOP_POS)) != 0U) { |
| 505 | iwdg_cfg |= IWDG_DISABLE_ON_STOP; |
| 506 | } |
| 507 | |
| 508 | if ((otp_value & BIT(iwdg_inst + HW2_OTP_IWDG_FZ_STANDBY_POS)) != 0U) { |
| 509 | iwdg_cfg |= IWDG_DISABLE_ON_STANDBY; |
| 510 | } |
| 511 | |
| 512 | return iwdg_cfg; |
| 513 | } |
| 514 | |
| 515 | #if defined(IMAGE_BL2) |
| 516 | uint32_t stm32_iwdg_shadow_update(uint32_t iwdg_inst, uint32_t flags) |
| 517 | { |
| 518 | uint32_t otp; |
| 519 | uint32_t result; |
| 520 | |
| 521 | if (bsec_shadow_read_otp(&otp, HW2_OTP) != BSEC_OK) { |
| 522 | panic(); |
| 523 | } |
| 524 | |
| 525 | if ((flags & IWDG_DISABLE_ON_STOP) != 0U) { |
| 526 | otp |= BIT(iwdg_inst + HW2_OTP_IWDG_FZ_STOP_POS); |
| 527 | } |
| 528 | |
| 529 | if ((flags & IWDG_DISABLE_ON_STANDBY) != 0U) { |
| 530 | otp |= BIT(iwdg_inst + HW2_OTP_IWDG_FZ_STANDBY_POS); |
| 531 | } |
| 532 | |
| 533 | result = bsec_write_otp(otp, HW2_OTP); |
| 534 | if (result != BSEC_OK) { |
| 535 | return result; |
| 536 | } |
| 537 | |
| 538 | /* Sticky lock OTP_IWDG (read and write) */ |
| 539 | if (!bsec_write_sr_lock(HW2_OTP, 1U) || |
| 540 | !bsec_write_sw_lock(HW2_OTP, 1U)) { |
| 541 | return BSEC_LOCK_FAIL; |
| 542 | } |
| 543 | |
| 544 | return BSEC_OK; |
| 545 | } |
| 546 | #endif |
Yann Gautier | 8f268c8 | 2020-02-26 13:39:44 +0100 | [diff] [blame] | 547 | |
Lionel Debieve | 1dc5e2e | 2020-09-27 21:13:53 +0200 | [diff] [blame] | 548 | #if STM32MP_USE_STM32IMAGE |
Yann Gautier | 8f268c8 | 2020-02-26 13:39:44 +0100 | [diff] [blame] | 549 | /* Get the non-secure DDR size */ |
| 550 | uint32_t stm32mp_get_ddr_ns_size(void) |
| 551 | { |
| 552 | static uint32_t ddr_ns_size; |
| 553 | uint32_t ddr_size; |
| 554 | |
| 555 | if (ddr_ns_size != 0U) { |
| 556 | return ddr_ns_size; |
| 557 | } |
| 558 | |
| 559 | ddr_size = dt_get_ddr_size(); |
| 560 | if ((ddr_size <= (STM32MP_DDR_S_SIZE + STM32MP_DDR_SHMEM_SIZE)) || |
| 561 | (ddr_size > STM32MP_DDR_MAX_SIZE)) { |
| 562 | panic(); |
| 563 | } |
| 564 | |
| 565 | ddr_ns_size = ddr_size - (STM32MP_DDR_S_SIZE + STM32MP_DDR_SHMEM_SIZE); |
| 566 | |
| 567 | return ddr_ns_size; |
| 568 | } |
Lionel Debieve | 1dc5e2e | 2020-09-27 21:13:53 +0200 | [diff] [blame] | 569 | #endif /* STM32MP_USE_STM32IMAGE */ |
Yann Gautier | 6eef525 | 2021-12-10 17:04:40 +0100 | [diff] [blame] | 570 | |
| 571 | void stm32_save_boot_interface(uint32_t interface, uint32_t instance) |
| 572 | { |
| 573 | uint32_t bkpr_itf_idx = tamp_bkpr(TAMP_BOOT_MODE_BACKUP_REG_ID); |
| 574 | |
Yann Gautier | a205a5c | 2021-08-30 15:06:54 +0200 | [diff] [blame] | 575 | clk_enable(RTCAPB); |
Yann Gautier | 6eef525 | 2021-12-10 17:04:40 +0100 | [diff] [blame] | 576 | |
| 577 | mmio_clrsetbits_32(bkpr_itf_idx, |
| 578 | TAMP_BOOT_MODE_ITF_MASK, |
| 579 | ((interface << 4) | (instance & 0xFU)) << |
| 580 | TAMP_BOOT_MODE_ITF_SHIFT); |
| 581 | |
Yann Gautier | a205a5c | 2021-08-30 15:06:54 +0200 | [diff] [blame] | 582 | clk_disable(RTCAPB); |
Yann Gautier | 6eef525 | 2021-12-10 17:04:40 +0100 | [diff] [blame] | 583 | } |
Yann Gautier | aaee061 | 2020-12-16 12:04:06 +0100 | [diff] [blame] | 584 | |
| 585 | void stm32_get_boot_interface(uint32_t *interface, uint32_t *instance) |
| 586 | { |
| 587 | static uint32_t itf; |
| 588 | |
| 589 | if (itf == 0U) { |
| 590 | uint32_t bkpr = tamp_bkpr(TAMP_BOOT_MODE_BACKUP_REG_ID); |
| 591 | |
Yann Gautier | a205a5c | 2021-08-30 15:06:54 +0200 | [diff] [blame] | 592 | clk_enable(RTCAPB); |
Yann Gautier | aaee061 | 2020-12-16 12:04:06 +0100 | [diff] [blame] | 593 | |
| 594 | itf = (mmio_read_32(bkpr) & TAMP_BOOT_MODE_ITF_MASK) >> |
| 595 | TAMP_BOOT_MODE_ITF_SHIFT; |
| 596 | |
Yann Gautier | a205a5c | 2021-08-30 15:06:54 +0200 | [diff] [blame] | 597 | clk_disable(RTCAPB); |
Yann Gautier | aaee061 | 2020-12-16 12:04:06 +0100 | [diff] [blame] | 598 | } |
| 599 | |
| 600 | *interface = itf >> 4; |
| 601 | *instance = itf & 0xFU; |
| 602 | } |
Sughosh Ganu | 03e2f80 | 2021-12-01 15:56:27 +0530 | [diff] [blame] | 603 | |
| 604 | #if !STM32MP_USE_STM32IMAGE && PSA_FWU_SUPPORT |
| 605 | void stm32mp1_fwu_set_boot_idx(void) |
| 606 | { |
| 607 | clk_enable(RTCAPB); |
| 608 | mmio_write_32(tamp_bkpr(TAMP_BOOT_COUNTER_REG_ID), |
| 609 | plat_fwu_get_boot_idx()); |
| 610 | clk_disable(RTCAPB); |
| 611 | } |
| 612 | #endif /* !STM32MP_USE_STM32IMAGE && PSA_FWU_SUPPORT */ |