Yann Gautier | 4b0c72a | 2018-07-16 10:54:09 +0200 | [diff] [blame] | 1 | /* |
Yann Gautier | 3e33475 | 2023-02-01 15:04:30 +0100 | [diff] [blame] | 2 | * Copyright (c) 2015-2024, Arm Limited and Contributors. All rights reserved. |
Yann Gautier | 4b0c72a | 2018-07-16 10:54:09 +0200 | [diff] [blame] | 3 | * |
| 4 | * SPDX-License-Identifier: BSD-3-Clause |
| 5 | */ |
| 6 | |
Yann Gautier | 4b0c72a | 2018-07-16 10:54:09 +0200 | [diff] [blame] | 7 | #include <assert.h> |
Yann Gautier | 658775c | 2021-07-06 10:00:44 +0200 | [diff] [blame] | 8 | #include <errno.h> |
Antonio Nino Diaz | e0f9063 | 2018-12-14 00:18:21 +0000 | [diff] [blame] | 9 | #include <string.h> |
| 10 | |
Antonio Nino Diaz | e0f9063 | 2018-12-14 00:18:21 +0000 | [diff] [blame] | 11 | #include <arch_helpers.h> |
| 12 | #include <common/bl_common.h> |
| 13 | #include <common/debug.h> |
| 14 | #include <common/desc_image_load.h> |
Antonio Nino Diaz | e0f9063 | 2018-12-14 00:18:21 +0000 | [diff] [blame] | 15 | #include <drivers/generic_delay_timer.h> |
Yann Gautier | a3bd8d1 | 2021-06-18 11:33:26 +0200 | [diff] [blame] | 16 | #include <drivers/mmc.h> |
Yann Gautier | 3edc7c3 | 2019-05-20 19:17:08 +0200 | [diff] [blame] | 17 | #include <drivers/st/bsec.h> |
Pascal Paillet | fc7b805 | 2021-01-29 14:48:49 +0100 | [diff] [blame] | 18 | #include <drivers/st/regulator_fixed.h> |
Yann Gautier | 091eab5 | 2019-06-04 18:06:34 +0200 | [diff] [blame] | 19 | #include <drivers/st/stm32_iwdg.h> |
Nicolas Le Bayon | 5c66fab | 2020-12-02 16:23:49 +0100 | [diff] [blame] | 20 | #include <drivers/st/stm32_rng.h> |
Yann Gautier | 3d8497c | 2021-10-18 16:06:22 +0200 | [diff] [blame] | 21 | #include <drivers/st/stm32_uart.h> |
Antonio Nino Diaz | e0f9063 | 2018-12-14 00:18:21 +0000 | [diff] [blame] | 22 | #include <drivers/st/stm32mp1_clk.h> |
Antonio Nino Diaz | e0f9063 | 2018-12-14 00:18:21 +0000 | [diff] [blame] | 23 | #include <drivers/st/stm32mp1_pwr.h> |
| 24 | #include <drivers/st/stm32mp1_ram.h> |
Yann Gautier | 0c81088 | 2021-12-17 09:53:04 +0100 | [diff] [blame] | 25 | #include <drivers/st/stm32mp_pmic.h> |
Yann Gautier | 658775c | 2021-07-06 10:00:44 +0200 | [diff] [blame] | 26 | #include <lib/fconf/fconf.h> |
| 27 | #include <lib/fconf/fconf_dyn_cfg_getter.h> |
Antonio Nino Diaz | e0f9063 | 2018-12-14 00:18:21 +0000 | [diff] [blame] | 28 | #include <lib/mmio.h> |
Yann Gautier | b3386f7 | 2019-04-19 09:41:01 +0200 | [diff] [blame] | 29 | #include <lib/optee_utils.h> |
Antonio Nino Diaz | e0f9063 | 2018-12-14 00:18:21 +0000 | [diff] [blame] | 30 | #include <lib/xlat_tables/xlat_tables_v2.h> |
| 31 | #include <plat/common/platform.h> |
| 32 | |
Yann Gautier | 0c81088 | 2021-12-17 09:53:04 +0100 | [diff] [blame] | 33 | #include <platform_def.h> |
Sughosh Ganu | 03e2f80 | 2021-12-01 15:56:27 +0530 | [diff] [blame] | 34 | #include <stm32mp_common.h> |
Yann Gautier | 091eab5 | 2019-06-04 18:06:34 +0200 | [diff] [blame] | 35 | #include <stm32mp1_dbgmcu.h> |
Yann Gautier | 4b0c72a | 2018-07-16 10:54:09 +0200 | [diff] [blame] | 36 | |
Lionel Debieve | 7192a00 | 2020-01-28 09:02:41 +0100 | [diff] [blame] | 37 | #if DEBUG |
| 38 | static const char debug_msg[] = { |
| 39 | "***************************************************\n" |
| 40 | "** DEBUG ACCESS PORT IS OPEN! **\n" |
| 41 | "** This boot image is only for debugging purpose **\n" |
| 42 | "** and is unsafe for production use. **\n" |
| 43 | "** **\n" |
| 44 | "** If you see this message and you are not **\n" |
| 45 | "** debugging report this immediately to your **\n" |
| 46 | "** vendor! **\n" |
| 47 | "***************************************************\n" |
| 48 | }; |
| 49 | #endif |
| 50 | |
Yann Gautier | f9d40d5 | 2019-01-17 14:41:46 +0100 | [diff] [blame] | 51 | static void print_reset_reason(void) |
| 52 | { |
Yann Gautier | 3d78a2e | 2019-02-14 11:01:20 +0100 | [diff] [blame] | 53 | uint32_t rstsr = mmio_read_32(stm32mp_rcc_base() + RCC_MP_RSTSCLRR); |
Yann Gautier | f9d40d5 | 2019-01-17 14:41:46 +0100 | [diff] [blame] | 54 | |
| 55 | if (rstsr == 0U) { |
| 56 | WARN("Reset reason unknown\n"); |
| 57 | return; |
| 58 | } |
| 59 | |
| 60 | INFO("Reset reason (0x%x):\n", rstsr); |
| 61 | |
| 62 | if ((rstsr & RCC_MP_RSTSCLRR_PADRSTF) == 0U) { |
| 63 | if ((rstsr & RCC_MP_RSTSCLRR_STDBYRSTF) != 0U) { |
| 64 | INFO("System exits from STANDBY\n"); |
| 65 | return; |
| 66 | } |
| 67 | |
| 68 | if ((rstsr & RCC_MP_RSTSCLRR_CSTDBYRSTF) != 0U) { |
| 69 | INFO("MPU exits from CSTANDBY\n"); |
| 70 | return; |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | if ((rstsr & RCC_MP_RSTSCLRR_PORRSTF) != 0U) { |
| 75 | INFO(" Power-on Reset (rst_por)\n"); |
| 76 | return; |
| 77 | } |
| 78 | |
| 79 | if ((rstsr & RCC_MP_RSTSCLRR_BORRSTF) != 0U) { |
| 80 | INFO(" Brownout Reset (rst_bor)\n"); |
| 81 | return; |
| 82 | } |
| 83 | |
Yann Gautier | cc5f89a | 2020-02-12 09:36:23 +0100 | [diff] [blame] | 84 | #if STM32MP15 |
Yann Gautier | f9d40d5 | 2019-01-17 14:41:46 +0100 | [diff] [blame] | 85 | if ((rstsr & RCC_MP_RSTSCLRR_MCSYSRSTF) != 0U) { |
| 86 | if ((rstsr & RCC_MP_RSTSCLRR_PADRSTF) != 0U) { |
| 87 | INFO(" System reset generated by MCU (MCSYSRST)\n"); |
| 88 | } else { |
| 89 | INFO(" Local reset generated by MCU (MCSYSRST)\n"); |
| 90 | } |
| 91 | return; |
| 92 | } |
Yann Gautier | cc5f89a | 2020-02-12 09:36:23 +0100 | [diff] [blame] | 93 | #endif |
Yann Gautier | f9d40d5 | 2019-01-17 14:41:46 +0100 | [diff] [blame] | 94 | |
| 95 | if ((rstsr & RCC_MP_RSTSCLRR_MPSYSRSTF) != 0U) { |
| 96 | INFO(" System reset generated by MPU (MPSYSRST)\n"); |
| 97 | return; |
| 98 | } |
| 99 | |
| 100 | if ((rstsr & RCC_MP_RSTSCLRR_HCSSRSTF) != 0U) { |
| 101 | INFO(" Reset due to a clock failure on HSE\n"); |
| 102 | return; |
| 103 | } |
| 104 | |
| 105 | if ((rstsr & RCC_MP_RSTSCLRR_IWDG1RSTF) != 0U) { |
| 106 | INFO(" IWDG1 Reset (rst_iwdg1)\n"); |
| 107 | return; |
| 108 | } |
| 109 | |
| 110 | if ((rstsr & RCC_MP_RSTSCLRR_IWDG2RSTF) != 0U) { |
| 111 | INFO(" IWDG2 Reset (rst_iwdg2)\n"); |
| 112 | return; |
| 113 | } |
| 114 | |
| 115 | if ((rstsr & RCC_MP_RSTSCLRR_MPUP0RSTF) != 0U) { |
| 116 | INFO(" MPU Processor 0 Reset\n"); |
| 117 | return; |
| 118 | } |
| 119 | |
Yann Gautier | cc5f89a | 2020-02-12 09:36:23 +0100 | [diff] [blame] | 120 | #if STM32MP15 |
Yann Gautier | f9d40d5 | 2019-01-17 14:41:46 +0100 | [diff] [blame] | 121 | if ((rstsr & RCC_MP_RSTSCLRR_MPUP1RSTF) != 0U) { |
| 122 | INFO(" MPU Processor 1 Reset\n"); |
| 123 | return; |
| 124 | } |
Yann Gautier | cc5f89a | 2020-02-12 09:36:23 +0100 | [diff] [blame] | 125 | #endif |
Yann Gautier | f9d40d5 | 2019-01-17 14:41:46 +0100 | [diff] [blame] | 126 | |
| 127 | if ((rstsr & RCC_MP_RSTSCLRR_PADRSTF) != 0U) { |
| 128 | INFO(" Pad Reset from NRST\n"); |
| 129 | return; |
| 130 | } |
| 131 | |
| 132 | if ((rstsr & RCC_MP_RSTSCLRR_VCORERSTF) != 0U) { |
| 133 | INFO(" Reset due to a failure of VDD_CORE\n"); |
| 134 | return; |
| 135 | } |
| 136 | |
| 137 | ERROR(" Unidentified reset reason\n"); |
| 138 | } |
| 139 | |
| 140 | void bl2_el3_early_platform_setup(u_register_t arg0, |
| 141 | u_register_t arg1 __unused, |
| 142 | u_register_t arg2 __unused, |
| 143 | u_register_t arg3 __unused) |
Yann Gautier | 4b0c72a | 2018-07-16 10:54:09 +0200 | [diff] [blame] | 144 | { |
Yann Gautier | d143574 | 2021-10-18 10:55:23 +0200 | [diff] [blame] | 145 | stm32mp_setup_early_console(); |
| 146 | |
Yann Gautier | a2e2a30 | 2019-02-14 11:13:39 +0100 | [diff] [blame] | 147 | stm32mp_save_boot_ctx_address(arg0); |
Yann Gautier | 4b0c72a | 2018-07-16 10:54:09 +0200 | [diff] [blame] | 148 | } |
| 149 | |
| 150 | void bl2_platform_setup(void) |
| 151 | { |
Yann Gautier | caf575b | 2018-07-24 17:18:19 +0200 | [diff] [blame] | 152 | int ret; |
| 153 | |
Yann Gautier | caf575b | 2018-07-24 17:18:19 +0200 | [diff] [blame] | 154 | ret = stm32mp1_ddr_probe(); |
| 155 | if (ret < 0) { |
| 156 | ERROR("Invalid DDR init: error %d\n", ret); |
| 157 | panic(); |
| 158 | } |
| 159 | |
Yann Gautier | f3bd87e | 2020-09-04 15:55:53 +0200 | [diff] [blame] | 160 | /* Map DDR for binary load, now with cacheable attribute */ |
Yann Gautier | a55169b | 2020-01-10 18:18:59 +0100 | [diff] [blame] | 161 | ret = mmap_add_dynamic_region(STM32MP_DDR_BASE, STM32MP_DDR_BASE, |
Yann Gautier | f3bd87e | 2020-09-04 15:55:53 +0200 | [diff] [blame] | 162 | STM32MP_DDR_MAX_SIZE, MT_MEMORY | MT_RW | MT_SECURE); |
| 163 | if (ret < 0) { |
| 164 | ERROR("DDR mapping: error %d\n", ret); |
| 165 | panic(); |
| 166 | } |
Yann Gautier | 4b0c72a | 2018-07-16 10:54:09 +0200 | [diff] [blame] | 167 | } |
| 168 | |
Yann Gautier | cc5f89a | 2020-02-12 09:36:23 +0100 | [diff] [blame] | 169 | #if STM32MP15 |
Yann Gautier | 5c1dab3 | 2019-04-17 15:12:58 +0200 | [diff] [blame] | 170 | static void update_monotonic_counter(void) |
| 171 | { |
| 172 | uint32_t version; |
| 173 | uint32_t otp; |
| 174 | |
| 175 | CASSERT(STM32_TF_VERSION <= MAX_MONOTONIC_VALUE, |
| 176 | assert_stm32mp1_monotonic_counter_reach_max); |
| 177 | |
| 178 | /* Check if monotonic counter needs to be incremented */ |
| 179 | if (stm32_get_otp_index(MONOTONIC_OTP, &otp, NULL) != 0) { |
| 180 | panic(); |
| 181 | } |
| 182 | |
| 183 | if (stm32_get_otp_value_from_idx(otp, &version) != 0) { |
| 184 | panic(); |
| 185 | } |
| 186 | |
| 187 | if ((version + 1U) < BIT(STM32_TF_VERSION)) { |
| 188 | uint32_t result; |
| 189 | |
| 190 | /* Need to increment the monotonic counter. */ |
| 191 | version = BIT(STM32_TF_VERSION) - 1U; |
| 192 | |
| 193 | result = bsec_program_otp(version, otp); |
| 194 | if (result != BSEC_OK) { |
| 195 | ERROR("BSEC: MONOTONIC_OTP program Error %u\n", |
| 196 | result); |
| 197 | panic(); |
| 198 | } |
| 199 | INFO("Monotonic counter has been incremented (value 0x%x)\n", |
| 200 | version); |
| 201 | } |
| 202 | } |
Yann Gautier | cc5f89a | 2020-02-12 09:36:23 +0100 | [diff] [blame] | 203 | #endif |
Yann Gautier | 5c1dab3 | 2019-04-17 15:12:58 +0200 | [diff] [blame] | 204 | |
Yann Gautier | 4b0c72a | 2018-07-16 10:54:09 +0200 | [diff] [blame] | 205 | void bl2_el3_plat_arch_setup(void) |
| 206 | { |
Yann Gautier | 69035a8 | 2018-07-05 16:48:16 +0200 | [diff] [blame] | 207 | const char *board_model; |
Yann Gautier | 4193466 | 2018-07-20 11:36:05 +0200 | [diff] [blame] | 208 | boot_api_context_t *boot_context = |
Yann Gautier | a2e2a30 | 2019-02-14 11:13:39 +0100 | [diff] [blame] | 209 | (boot_api_context_t *)stm32mp_get_boot_ctx_address(); |
Yann Gautier | 3d78a2e | 2019-02-14 11:01:20 +0100 | [diff] [blame] | 210 | uintptr_t pwr_base; |
| 211 | uintptr_t rcc_base; |
Yann Gautier | 4193466 | 2018-07-20 11:36:05 +0200 | [diff] [blame] | 212 | |
Nicolas Le Bayon | 97287cd | 2019-05-20 18:35:02 +0200 | [diff] [blame] | 213 | if (bsec_probe() != 0U) { |
| 214 | panic(); |
| 215 | } |
| 216 | |
Yann Gautier | f9d40d5 | 2019-01-17 14:41:46 +0100 | [diff] [blame] | 217 | mmap_add_region(BL_CODE_BASE, BL_CODE_BASE, |
| 218 | BL_CODE_END - BL_CODE_BASE, |
| 219 | MT_CODE | MT_SECURE); |
| 220 | |
Yann Gautier | f9d40d5 | 2019-01-17 14:41:46 +0100 | [diff] [blame] | 221 | /* Prevent corruption of preloaded Device Tree */ |
| 222 | mmap_add_region(DTB_BASE, DTB_BASE, |
| 223 | DTB_LIMIT - DTB_BASE, |
Yann Gautier | 3d33df6 | 2019-12-17 17:11:10 +0100 | [diff] [blame] | 224 | MT_RO_DATA | MT_SECURE); |
Yann Gautier | f9d40d5 | 2019-01-17 14:41:46 +0100 | [diff] [blame] | 225 | |
| 226 | configure_mmu(); |
| 227 | |
Yann Gautier | 05773eb | 2020-08-24 11:51:50 +0200 | [diff] [blame] | 228 | if (dt_open_and_check(STM32MP_DTB_BASE) < 0) { |
Yann Gautier | f9d40d5 | 2019-01-17 14:41:46 +0100 | [diff] [blame] | 229 | panic(); |
| 230 | } |
| 231 | |
Yann Gautier | 3d78a2e | 2019-02-14 11:01:20 +0100 | [diff] [blame] | 232 | pwr_base = stm32mp_pwr_base(); |
| 233 | rcc_base = stm32mp_rcc_base(); |
| 234 | |
Yann Gautier | 4b0c72a | 2018-07-16 10:54:09 +0200 | [diff] [blame] | 235 | /* |
| 236 | * Disable the backup domain write protection. |
| 237 | * The protection is enable at each reset by hardware |
| 238 | * and must be disabled by software. |
| 239 | */ |
Yann Gautier | 3d78a2e | 2019-02-14 11:01:20 +0100 | [diff] [blame] | 240 | mmio_setbits_32(pwr_base + PWR_CR1, PWR_CR1_DBP); |
Yann Gautier | 4b0c72a | 2018-07-16 10:54:09 +0200 | [diff] [blame] | 241 | |
Yann Gautier | 3d78a2e | 2019-02-14 11:01:20 +0100 | [diff] [blame] | 242 | while ((mmio_read_32(pwr_base + PWR_CR1) & PWR_CR1_DBP) == 0U) { |
Yann Gautier | 4b0c72a | 2018-07-16 10:54:09 +0200 | [diff] [blame] | 243 | ; |
| 244 | } |
| 245 | |
| 246 | /* Reset backup domain on cold boot cases */ |
Yann Gautier | 3d78a2e | 2019-02-14 11:01:20 +0100 | [diff] [blame] | 247 | if ((mmio_read_32(rcc_base + RCC_BDCR) & RCC_BDCR_RTCSRC_MASK) == 0U) { |
| 248 | mmio_setbits_32(rcc_base + RCC_BDCR, RCC_BDCR_VSWRST); |
Yann Gautier | 4b0c72a | 2018-07-16 10:54:09 +0200 | [diff] [blame] | 249 | |
Yann Gautier | 3d78a2e | 2019-02-14 11:01:20 +0100 | [diff] [blame] | 250 | while ((mmio_read_32(rcc_base + RCC_BDCR) & RCC_BDCR_VSWRST) == |
Yann Gautier | 4b0c72a | 2018-07-16 10:54:09 +0200 | [diff] [blame] | 251 | 0U) { |
| 252 | ; |
| 253 | } |
| 254 | |
Yann Gautier | 3d78a2e | 2019-02-14 11:01:20 +0100 | [diff] [blame] | 255 | mmio_clrbits_32(rcc_base + RCC_BDCR, RCC_BDCR_VSWRST); |
Yann Gautier | 4b0c72a | 2018-07-16 10:54:09 +0200 | [diff] [blame] | 256 | } |
| 257 | |
Yann Gautier | cc5f89a | 2020-02-12 09:36:23 +0100 | [diff] [blame] | 258 | #if STM32MP15 |
Yann Gautier | ed34232 | 2019-02-15 17:33:27 +0100 | [diff] [blame] | 259 | /* Disable MCKPROT */ |
| 260 | mmio_clrbits_32(rcc_base + RCC_TZCR, RCC_TZCR_MCKPROT); |
Yann Gautier | cc5f89a | 2020-02-12 09:36:23 +0100 | [diff] [blame] | 261 | #endif |
Yann Gautier | ed34232 | 2019-02-15 17:33:27 +0100 | [diff] [blame] | 262 | |
Yann Gautier | c0882f4 | 2021-04-27 18:19:13 +0200 | [diff] [blame] | 263 | /* |
| 264 | * Set minimum reset pulse duration to 31ms for discrete power |
| 265 | * supplied boards. |
| 266 | */ |
| 267 | if (dt_pmic_status() <= 0) { |
| 268 | mmio_clrsetbits_32(rcc_base + RCC_RDLSICR, |
| 269 | RCC_RDLSICR_MRD_MASK, |
| 270 | 31U << RCC_RDLSICR_MRD_SHIFT); |
| 271 | } |
| 272 | |
Yann Gautier | 4b0c72a | 2018-07-16 10:54:09 +0200 | [diff] [blame] | 273 | generic_delay_timer_init(); |
| 274 | |
Yann Gautier | 3d8497c | 2021-10-18 16:06:22 +0200 | [diff] [blame] | 275 | #if STM32MP_UART_PROGRAMMER |
| 276 | /* Disable programmer UART before changing clock tree */ |
| 277 | if (boot_context->boot_interface_selected == |
| 278 | BOOT_API_CTX_BOOT_INTERFACE_SEL_SERIAL_UART) { |
| 279 | uintptr_t uart_prog_addr = |
| 280 | get_uart_address(boot_context->boot_interface_instance); |
| 281 | |
| 282 | stm32_uart_stop(uart_prog_addr); |
| 283 | } |
| 284 | #endif |
Yann Gautier | 9aea69e | 2018-07-24 17:13:36 +0200 | [diff] [blame] | 285 | if (stm32mp1_clk_probe() < 0) { |
| 286 | panic(); |
| 287 | } |
| 288 | |
| 289 | if (stm32mp1_clk_init() < 0) { |
| 290 | panic(); |
| 291 | } |
| 292 | |
Yann Gautier | 8402c29 | 2022-06-29 17:03:36 +0200 | [diff] [blame] | 293 | stm32_save_boot_info(boot_context); |
Yann Gautier | 6eef525 | 2021-12-10 17:04:40 +0100 | [diff] [blame] | 294 | |
Yann Gautier | cc5f89a | 2020-02-12 09:36:23 +0100 | [diff] [blame] | 295 | #if STM32MP_USB_PROGRAMMER && STM32MP15 |
Yann Gautier | cd16df3 | 2021-06-04 14:04:05 +0200 | [diff] [blame] | 296 | /* Deconfigure all UART RX pins configured by ROM code */ |
| 297 | stm32mp1_deconfigure_uart_pins(); |
| 298 | #endif |
| 299 | |
Yann Gautier | 66baa96 | 2021-10-18 14:01:00 +0200 | [diff] [blame] | 300 | if (stm32mp_uart_console_setup() != 0) { |
Yann Gautier | 69035a8 | 2018-07-05 16:48:16 +0200 | [diff] [blame] | 301 | goto skip_console_init; |
| 302 | } |
| 303 | |
Yann Gautier | c737405 | 2019-06-04 18:02:37 +0200 | [diff] [blame] | 304 | stm32mp_print_cpuinfo(); |
| 305 | |
Yann Gautier | 69035a8 | 2018-07-05 16:48:16 +0200 | [diff] [blame] | 306 | board_model = dt_get_board_model(); |
| 307 | if (board_model != NULL) { |
Yann Gautier | f9d40d5 | 2019-01-17 14:41:46 +0100 | [diff] [blame] | 308 | NOTICE("Model: %s\n", board_model); |
Yann Gautier | 69035a8 | 2018-07-05 16:48:16 +0200 | [diff] [blame] | 309 | } |
| 310 | |
Yann Gautier | 35dc077 | 2019-05-13 18:34:48 +0200 | [diff] [blame] | 311 | stm32mp_print_boardinfo(); |
| 312 | |
Lionel Debieve | 7bd96f4 | 2019-09-03 12:22:23 +0200 | [diff] [blame] | 313 | if (boot_context->auth_status != BOOT_API_CTX_AUTH_NO) { |
| 314 | NOTICE("Bootrom authentication %s\n", |
| 315 | (boot_context->auth_status == BOOT_API_CTX_AUTH_FAILED) ? |
| 316 | "failed" : "succeeded"); |
| 317 | } |
| 318 | |
Yann Gautier | 69035a8 | 2018-07-05 16:48:16 +0200 | [diff] [blame] | 319 | skip_console_init: |
Lionel Debieve | 474ad81 | 2022-10-05 16:52:09 +0200 | [diff] [blame] | 320 | #if !TRUSTED_BOARD_BOOT |
Yann Gautier | 3e33475 | 2023-02-01 15:04:30 +0100 | [diff] [blame] | 321 | if (stm32mp_check_closed_device() == STM32MP_CHIP_SEC_CLOSED) { |
Lionel Debieve | 474ad81 | 2022-10-05 16:52:09 +0200 | [diff] [blame] | 322 | /* Closed chip mandates authentication */ |
| 323 | ERROR("Secure chip: TRUSTED_BOARD_BOOT must be enabled\n"); |
| 324 | panic(); |
| 325 | } |
| 326 | #endif |
| 327 | |
Pascal Paillet | fc7b805 | 2021-01-29 14:48:49 +0100 | [diff] [blame] | 328 | if (fixed_regulator_register() != 0) { |
| 329 | panic(); |
| 330 | } |
| 331 | |
Yann Gautier | 45c1e58 | 2020-09-17 11:54:52 +0200 | [diff] [blame] | 332 | if (dt_pmic_status() > 0) { |
| 333 | initialize_pmic(); |
Yann Gautier | b2ba78e | 2022-01-18 10:39:52 +0100 | [diff] [blame] | 334 | if (pmic_voltages_init() != 0) { |
| 335 | ERROR("PMIC voltages init failed\n"); |
| 336 | panic(); |
| 337 | } |
Nicolas Le Bayon | 0b10b65 | 2019-11-18 13:13:36 +0100 | [diff] [blame] | 338 | print_pmic_info_and_debug(); |
Yann Gautier | 45c1e58 | 2020-09-17 11:54:52 +0200 | [diff] [blame] | 339 | } |
| 340 | |
| 341 | stm32mp1_syscfg_init(); |
| 342 | |
Yann Gautier | 091eab5 | 2019-06-04 18:06:34 +0200 | [diff] [blame] | 343 | if (stm32_iwdg_init() < 0) { |
| 344 | panic(); |
| 345 | } |
| 346 | |
| 347 | stm32_iwdg_refresh(); |
| 348 | |
Lionel Debieve | 7192a00 | 2020-01-28 09:02:41 +0100 | [diff] [blame] | 349 | if (bsec_read_debug_conf() != 0U) { |
Yann Gautier | 3e33475 | 2023-02-01 15:04:30 +0100 | [diff] [blame] | 350 | if (stm32mp_check_closed_device() == STM32MP_CHIP_SEC_CLOSED) { |
Lionel Debieve | 7192a00 | 2020-01-28 09:02:41 +0100 | [diff] [blame] | 351 | #if DEBUG |
| 352 | WARN("\n%s", debug_msg); |
| 353 | #else |
| 354 | ERROR("***Debug opened on closed chip***\n"); |
| 355 | #endif |
| 356 | } |
| 357 | } |
| 358 | |
Nicolas Le Bayon | 5c66fab | 2020-12-02 16:23:49 +0100 | [diff] [blame] | 359 | #if STM32MP13 |
| 360 | if (stm32_rng_init() != 0) { |
| 361 | panic(); |
| 362 | } |
| 363 | #endif |
| 364 | |
Yann Gautier | caf575b | 2018-07-24 17:18:19 +0200 | [diff] [blame] | 365 | stm32mp1_arch_security_setup(); |
| 366 | |
Yann Gautier | f9d40d5 | 2019-01-17 14:41:46 +0100 | [diff] [blame] | 367 | print_reset_reason(); |
| 368 | |
Yann Gautier | cc5f89a | 2020-02-12 09:36:23 +0100 | [diff] [blame] | 369 | #if STM32MP15 |
Robin van der Gracht | 44ad507 | 2024-01-24 09:29:13 +0100 | [diff] [blame] | 370 | if (stm32mp_check_closed_device() == STM32MP_CHIP_SEC_CLOSED) { |
| 371 | update_monotonic_counter(); |
| 372 | } |
Yann Gautier | cc5f89a | 2020-02-12 09:36:23 +0100 | [diff] [blame] | 373 | #endif |
Yann Gautier | 5c1dab3 | 2019-04-17 15:12:58 +0200 | [diff] [blame] | 374 | |
Yann Gautier | b76c61a | 2020-12-16 10:17:35 +0100 | [diff] [blame] | 375 | stm32mp1_syscfg_enable_io_compensation_finish(); |
| 376 | |
Yann Gautier | 29f1f94 | 2021-07-13 18:07:41 +0200 | [diff] [blame] | 377 | fconf_populate("TB_FW", STM32MP_DTB_BASE); |
Yann Gautier | 29f1f94 | 2021-07-13 18:07:41 +0200 | [diff] [blame] | 378 | |
Yann Gautier | a2e2a30 | 2019-02-14 11:13:39 +0100 | [diff] [blame] | 379 | stm32mp_io_setup(); |
Yann Gautier | 4b0c72a | 2018-07-16 10:54:09 +0200 | [diff] [blame] | 380 | } |
Yann Gautier | b3386f7 | 2019-04-19 09:41:01 +0200 | [diff] [blame] | 381 | |
Yann Gautier | b3386f7 | 2019-04-19 09:41:01 +0200 | [diff] [blame] | 382 | /******************************************************************************* |
| 383 | * This function can be used by the platforms to update/use image |
| 384 | * information for given `image_id`. |
| 385 | ******************************************************************************/ |
| 386 | int bl2_plat_handle_post_image_load(unsigned int image_id) |
| 387 | { |
| 388 | int err = 0; |
| 389 | bl_mem_params_node_t *bl_mem_params = get_bl_mem_params_node(image_id); |
| 390 | bl_mem_params_node_t *bl32_mem_params; |
Yann Gautier | 0ed7b2a | 2021-05-19 18:48:16 +0200 | [diff] [blame] | 391 | bl_mem_params_node_t *pager_mem_params __unused; |
| 392 | bl_mem_params_node_t *paged_mem_params __unused; |
Yann Gautier | 658775c | 2021-07-06 10:00:44 +0200 | [diff] [blame] | 393 | const struct dyn_cfg_dtb_info_t *config_info; |
| 394 | bl_mem_params_node_t *tos_fw_mem_params; |
| 395 | unsigned int i; |
Yann Gautier | fd64835 | 2021-12-13 15:24:41 +0100 | [diff] [blame] | 396 | unsigned int idx; |
Yann Gautier | 658775c | 2021-07-06 10:00:44 +0200 | [diff] [blame] | 397 | unsigned long long ddr_top __unused; |
| 398 | const unsigned int image_ids[] = { |
| 399 | BL32_IMAGE_ID, |
| 400 | BL33_IMAGE_ID, |
| 401 | HW_CONFIG_ID, |
| 402 | TOS_FW_CONFIG_ID, |
| 403 | }; |
Yann Gautier | b3386f7 | 2019-04-19 09:41:01 +0200 | [diff] [blame] | 404 | |
| 405 | assert(bl_mem_params != NULL); |
| 406 | |
| 407 | switch (image_id) { |
Yann Gautier | 658775c | 2021-07-06 10:00:44 +0200 | [diff] [blame] | 408 | case FW_CONFIG_ID: |
| 409 | /* Set global DTB info for fixed fw_config information */ |
Manish V Badarkhe | fab7628 | 2022-03-16 13:51:26 +0000 | [diff] [blame] | 410 | set_config_info(STM32MP_FW_CONFIG_BASE, ~0UL, STM32MP_FW_CONFIG_MAX_SIZE, |
| 411 | FW_CONFIG_ID); |
Yann Gautier | 658775c | 2021-07-06 10:00:44 +0200 | [diff] [blame] | 412 | fconf_populate("FW_CONFIG", STM32MP_FW_CONFIG_BASE); |
| 413 | |
Yann Gautier | fd64835 | 2021-12-13 15:24:41 +0100 | [diff] [blame] | 414 | idx = dyn_cfg_dtb_info_get_index(TOS_FW_CONFIG_ID); |
| 415 | |
Yann Gautier | 658775c | 2021-07-06 10:00:44 +0200 | [diff] [blame] | 416 | /* Iterate through all the fw config IDs */ |
| 417 | for (i = 0U; i < ARRAY_SIZE(image_ids); i++) { |
Yann Gautier | fd64835 | 2021-12-13 15:24:41 +0100 | [diff] [blame] | 418 | if ((image_ids[i] == TOS_FW_CONFIG_ID) && (idx == FCONF_INVALID_IDX)) { |
| 419 | continue; |
| 420 | } |
| 421 | |
Yann Gautier | 658775c | 2021-07-06 10:00:44 +0200 | [diff] [blame] | 422 | bl_mem_params = get_bl_mem_params_node(image_ids[i]); |
| 423 | assert(bl_mem_params != NULL); |
| 424 | |
| 425 | config_info = FCONF_GET_PROPERTY(dyn_cfg, dtb, image_ids[i]); |
| 426 | if (config_info == NULL) { |
| 427 | continue; |
| 428 | } |
| 429 | |
| 430 | bl_mem_params->image_info.image_base = config_info->config_addr; |
| 431 | bl_mem_params->image_info.image_max_size = config_info->config_max_size; |
| 432 | |
| 433 | bl_mem_params->image_info.h.attr &= ~IMAGE_ATTRIB_SKIP_LOADING; |
| 434 | |
| 435 | switch (image_ids[i]) { |
| 436 | case BL32_IMAGE_ID: |
| 437 | bl_mem_params->ep_info.pc = config_info->config_addr; |
| 438 | |
| 439 | /* In case of OPTEE, initialize address space with tos_fw addr */ |
| 440 | pager_mem_params = get_bl_mem_params_node(BL32_EXTRA1_IMAGE_ID); |
Yann Gautier | c6f77b0 | 2022-05-06 09:50:43 +0200 | [diff] [blame] | 441 | assert(pager_mem_params != NULL); |
Yann Gautier | 658775c | 2021-07-06 10:00:44 +0200 | [diff] [blame] | 442 | pager_mem_params->image_info.image_base = config_info->config_addr; |
| 443 | pager_mem_params->image_info.image_max_size = |
| 444 | config_info->config_max_size; |
| 445 | |
| 446 | /* Init base and size for pager if exist */ |
| 447 | paged_mem_params = get_bl_mem_params_node(BL32_EXTRA2_IMAGE_ID); |
Yann Gautier | e622a3d | 2022-06-20 11:43:17 +0200 | [diff] [blame] | 448 | if (paged_mem_params != NULL) { |
| 449 | paged_mem_params->image_info.image_base = STM32MP_DDR_BASE + |
| 450 | (dt_get_ddr_size() - STM32MP_DDR_S_SIZE - |
| 451 | STM32MP_DDR_SHMEM_SIZE); |
| 452 | paged_mem_params->image_info.image_max_size = |
| 453 | STM32MP_DDR_S_SIZE; |
| 454 | } |
Yann Gautier | 658775c | 2021-07-06 10:00:44 +0200 | [diff] [blame] | 455 | break; |
| 456 | |
| 457 | case BL33_IMAGE_ID: |
| 458 | bl_mem_params->ep_info.pc = config_info->config_addr; |
| 459 | break; |
| 460 | |
| 461 | case HW_CONFIG_ID: |
| 462 | case TOS_FW_CONFIG_ID: |
| 463 | break; |
| 464 | |
| 465 | default: |
| 466 | return -EINVAL; |
| 467 | } |
| 468 | } |
| 469 | break; |
Yann Gautier | 658775c | 2021-07-06 10:00:44 +0200 | [diff] [blame] | 470 | |
Yann Gautier | b3386f7 | 2019-04-19 09:41:01 +0200 | [diff] [blame] | 471 | case BL32_IMAGE_ID: |
Yann Gautier | 90f84d7 | 2021-07-13 14:44:09 +0200 | [diff] [blame] | 472 | if (optee_header_is_valid(bl_mem_params->image_info.image_base)) { |
Yann Gautier | e622a3d | 2022-06-20 11:43:17 +0200 | [diff] [blame] | 473 | image_info_t *paged_image_info = NULL; |
| 474 | |
Yann Gautier | 90f84d7 | 2021-07-13 14:44:09 +0200 | [diff] [blame] | 475 | /* BL32 is OP-TEE header */ |
| 476 | bl_mem_params->ep_info.pc = bl_mem_params->image_info.image_base; |
| 477 | pager_mem_params = get_bl_mem_params_node(BL32_EXTRA1_IMAGE_ID); |
Yann Gautier | e622a3d | 2022-06-20 11:43:17 +0200 | [diff] [blame] | 478 | assert(pager_mem_params != NULL); |
| 479 | |
Yann Gautier | 90f84d7 | 2021-07-13 14:44:09 +0200 | [diff] [blame] | 480 | paged_mem_params = get_bl_mem_params_node(BL32_EXTRA2_IMAGE_ID); |
Yann Gautier | e622a3d | 2022-06-20 11:43:17 +0200 | [diff] [blame] | 481 | if (paged_mem_params != NULL) { |
| 482 | paged_image_info = &paged_mem_params->image_info; |
| 483 | } |
Yann Gautier | b3386f7 | 2019-04-19 09:41:01 +0200 | [diff] [blame] | 484 | |
Yann Gautier | 90f84d7 | 2021-07-13 14:44:09 +0200 | [diff] [blame] | 485 | err = parse_optee_header(&bl_mem_params->ep_info, |
| 486 | &pager_mem_params->image_info, |
Yann Gautier | e622a3d | 2022-06-20 11:43:17 +0200 | [diff] [blame] | 487 | paged_image_info); |
| 488 | if (err != 0) { |
Yann Gautier | 90f84d7 | 2021-07-13 14:44:09 +0200 | [diff] [blame] | 489 | ERROR("OPTEE header parse error.\n"); |
| 490 | panic(); |
| 491 | } |
Yann Gautier | b3386f7 | 2019-04-19 09:41:01 +0200 | [diff] [blame] | 492 | |
Yann Gautier | 90f84d7 | 2021-07-13 14:44:09 +0200 | [diff] [blame] | 493 | /* Set optee boot info from parsed header data */ |
Yann Gautier | e622a3d | 2022-06-20 11:43:17 +0200 | [diff] [blame] | 494 | if (paged_mem_params != NULL) { |
| 495 | bl_mem_params->ep_info.args.arg0 = |
| 496 | paged_mem_params->image_info.image_base; |
| 497 | } else { |
| 498 | bl_mem_params->ep_info.args.arg0 = 0U; |
| 499 | } |
| 500 | |
| 501 | bl_mem_params->ep_info.args.arg1 = 0U; /* Unused */ |
| 502 | bl_mem_params->ep_info.args.arg2 = 0U; /* No DT supported */ |
Yann Gautier | 0ed7b2a | 2021-05-19 18:48:16 +0200 | [diff] [blame] | 503 | } else { |
Yann Gautier | 0ed7b2a | 2021-05-19 18:48:16 +0200 | [diff] [blame] | 504 | bl_mem_params->ep_info.pc = bl_mem_params->image_info.image_base; |
Yann Gautier | 658775c | 2021-07-06 10:00:44 +0200 | [diff] [blame] | 505 | tos_fw_mem_params = get_bl_mem_params_node(TOS_FW_CONFIG_ID); |
Yann Gautier | c6f77b0 | 2022-05-06 09:50:43 +0200 | [diff] [blame] | 506 | assert(tos_fw_mem_params != NULL); |
Yann Gautier | 658775c | 2021-07-06 10:00:44 +0200 | [diff] [blame] | 507 | bl_mem_params->image_info.image_max_size += |
| 508 | tos_fw_mem_params->image_info.image_max_size; |
Yann Gautier | 0ed7b2a | 2021-05-19 18:48:16 +0200 | [diff] [blame] | 509 | bl_mem_params->ep_info.args.arg0 = 0; |
Yann Gautier | 90f84d7 | 2021-07-13 14:44:09 +0200 | [diff] [blame] | 510 | } |
Yann Gautier | b3386f7 | 2019-04-19 09:41:01 +0200 | [diff] [blame] | 511 | break; |
| 512 | |
| 513 | case BL33_IMAGE_ID: |
| 514 | bl32_mem_params = get_bl_mem_params_node(BL32_IMAGE_ID); |
| 515 | assert(bl32_mem_params != NULL); |
| 516 | bl32_mem_params->ep_info.lr_svc = bl_mem_params->ep_info.pc; |
Yann Gautier | 5d2eb55 | 2022-11-14 14:14:48 +0100 | [diff] [blame] | 517 | #if PSA_FWU_SUPPORT |
Sughosh Ganu | 03e2f80 | 2021-12-01 15:56:27 +0530 | [diff] [blame] | 518 | stm32mp1_fwu_set_boot_idx(); |
Yann Gautier | 5d2eb55 | 2022-11-14 14:14:48 +0100 | [diff] [blame] | 519 | #endif /* PSA_FWU_SUPPORT */ |
Yann Gautier | b3386f7 | 2019-04-19 09:41:01 +0200 | [diff] [blame] | 520 | break; |
| 521 | |
| 522 | default: |
| 523 | /* Do nothing in default case */ |
| 524 | break; |
| 525 | } |
| 526 | |
Yann Gautier | a3bd8d1 | 2021-06-18 11:33:26 +0200 | [diff] [blame] | 527 | #if STM32MP_SDMMC || STM32MP_EMMC |
| 528 | /* |
| 529 | * Invalidate remaining data read from MMC but not flushed by load_image_flush(). |
| 530 | * We take the worst case which is 2 MMC blocks. |
| 531 | */ |
| 532 | if ((image_id != FW_CONFIG_ID) && |
| 533 | ((bl_mem_params->image_info.h.attr & IMAGE_ATTRIB_SKIP_LOADING) == 0U)) { |
| 534 | inv_dcache_range(bl_mem_params->image_info.image_base + |
| 535 | bl_mem_params->image_info.image_size, |
| 536 | 2U * MMC_BLOCK_SIZE); |
| 537 | } |
| 538 | #endif /* STM32MP_SDMMC || STM32MP_EMMC */ |
| 539 | |
Yann Gautier | b3386f7 | 2019-04-19 09:41:01 +0200 | [diff] [blame] | 540 | return err; |
| 541 | } |
Yann Gautier | d2d9b96 | 2021-08-16 11:58:01 +0200 | [diff] [blame] | 542 | |
| 543 | void bl2_el3_plat_prepare_exit(void) |
| 544 | { |
Yann Gautier | de775e8 | 2022-11-25 15:33:09 +0100 | [diff] [blame] | 545 | #if STM32MP_UART_PROGRAMMER || STM32MP_USB_PROGRAMMER |
Patrick Delaunay | 9c5ee78 | 2021-07-06 14:07:56 +0200 | [diff] [blame] | 546 | uint16_t boot_itf = stm32mp_get_boot_itf_selected(); |
| 547 | |
Yann Gautier | de775e8 | 2022-11-25 15:33:09 +0100 | [diff] [blame] | 548 | if ((boot_itf == BOOT_API_CTX_BOOT_INTERFACE_SEL_SERIAL_UART) || |
| 549 | (boot_itf == BOOT_API_CTX_BOOT_INTERFACE_SEL_SERIAL_USB)) { |
Patrick Delaunay | 9c5ee78 | 2021-07-06 14:07:56 +0200 | [diff] [blame] | 550 | /* Invalidate the downloaded buffer used with io_memmap */ |
| 551 | inv_dcache_range(DWL_BUFFER_BASE, DWL_BUFFER_SIZE); |
Patrick Delaunay | 9c5ee78 | 2021-07-06 14:07:56 +0200 | [diff] [blame] | 552 | } |
Yann Gautier | de775e8 | 2022-11-25 15:33:09 +0100 | [diff] [blame] | 553 | #endif /* STM32MP_UART_PROGRAMMER || STM32MP_USB_PROGRAMMER */ |
Patrick Delaunay | 9c5ee78 | 2021-07-06 14:07:56 +0200 | [diff] [blame] | 554 | |
Yann Gautier | d2d9b96 | 2021-08-16 11:58:01 +0200 | [diff] [blame] | 555 | stm32mp1_security_setup(); |
| 556 | } |