Yann Gautier | a3f4638 | 2023-06-14 10:40:59 +0200 | [diff] [blame] | 1 | /* |
Yann Gautier | 24e94a6 | 2024-01-04 10:58:18 +0100 | [diff] [blame] | 2 | * Copyright (c) 2023-2024, STMicroelectronics - All Rights Reserved |
Yann Gautier | a3f4638 | 2023-06-14 10:40:59 +0200 | [diff] [blame] | 3 | * |
| 4 | * SPDX-License-Identifier: BSD-3-Clause |
| 5 | */ |
| 6 | |
Yann Gautier | 88ca335 | 2024-05-22 16:46:37 +0200 | [diff] [blame] | 7 | #include <assert.h> |
Yann Gautier | a3f4638 | 2023-06-14 10:40:59 +0200 | [diff] [blame] | 8 | #include <cdefs.h> |
Yann Gautier | ece4c25 | 2023-06-13 18:45:03 +0200 | [diff] [blame] | 9 | #include <errno.h> |
Yann Gautier | a3f4638 | 2023-06-14 10:40:59 +0200 | [diff] [blame] | 10 | #include <stdint.h> |
| 11 | |
Yann Gautier | a585d76 | 2024-01-03 14:28:23 +0100 | [diff] [blame] | 12 | #include <common/debug.h> |
Yann Gautier | 88ca335 | 2024-05-22 16:46:37 +0200 | [diff] [blame] | 13 | #include <common/desc_image_load.h> |
Yann Gautier | 8053f2b | 2024-05-21 11:46:59 +0200 | [diff] [blame] | 14 | #include <drivers/clk.h> |
Yann Gautier | 88ca335 | 2024-05-22 16:46:37 +0200 | [diff] [blame] | 15 | #include <drivers/mmc.h> |
Yann Gautier | 3ad65ce | 2024-05-21 12:03:56 +0200 | [diff] [blame] | 16 | #include <drivers/st/regulator_fixed.h> |
Yann Gautier | 40ff138 | 2024-05-21 20:54:04 +0200 | [diff] [blame] | 17 | #include <drivers/st/stm32mp2_ddr_helpers.h> |
Pascal Paillet | 3263aea | 2022-12-16 14:59:34 +0100 | [diff] [blame] | 18 | #include <drivers/st/stm32mp_pmic2.h> |
Maxime Méré | b151f68 | 2024-09-13 17:57:58 +0200 | [diff] [blame] | 19 | #include <drivers/st/stm32mp_risab_regs.h> |
Yann Gautier | 8053f2b | 2024-05-21 11:46:59 +0200 | [diff] [blame] | 20 | #include <lib/fconf/fconf.h> |
| 21 | #include <lib/fconf/fconf_dyn_cfg_getter.h> |
| 22 | #include <lib/mmio.h> |
| 23 | #include <lib/xlat_tables/xlat_tables_v2.h> |
Yann Gautier | 24e94a6 | 2024-01-04 10:58:18 +0100 | [diff] [blame] | 24 | #include <plat/common/platform.h> |
| 25 | |
Yann Gautier | a585d76 | 2024-01-03 14:28:23 +0100 | [diff] [blame] | 26 | #include <platform_def.h> |
Yann Gautier | eb91af5 | 2023-06-14 18:05:47 +0200 | [diff] [blame] | 27 | #include <stm32mp_common.h> |
Yann Gautier | 8053f2b | 2024-05-21 11:46:59 +0200 | [diff] [blame] | 28 | #include <stm32mp_dt.h> |
| 29 | |
| 30 | #define BOOT_CTX_ADDR 0x0e000020UL |
| 31 | |
| 32 | static void print_reset_reason(void) |
| 33 | { |
| 34 | uint32_t rstsr = mmio_read_32(stm32mp_rcc_base() + RCC_C1BOOTRSTSCLRR); |
| 35 | |
| 36 | if (rstsr == 0U) { |
| 37 | WARN("Reset reason unknown\n"); |
| 38 | return; |
| 39 | } |
| 40 | |
| 41 | INFO("Reset reason (0x%x):\n", rstsr); |
| 42 | |
| 43 | if ((rstsr & RCC_C1BOOTRSTSCLRR_PADRSTF) == 0U) { |
| 44 | if ((rstsr & RCC_C1BOOTRSTSCLRR_STBYC1RSTF) != 0U) { |
| 45 | INFO("System exits from Standby for CA35\n"); |
| 46 | return; |
| 47 | } |
| 48 | |
| 49 | if ((rstsr & RCC_C1BOOTRSTSCLRR_D1STBYRSTF) != 0U) { |
| 50 | INFO("D1 domain exits from DStandby\n"); |
| 51 | return; |
| 52 | } |
| 53 | } |
| 54 | |
| 55 | if ((rstsr & RCC_C1BOOTRSTSCLRR_PORRSTF) != 0U) { |
| 56 | INFO(" Power-on Reset (rst_por)\n"); |
| 57 | return; |
| 58 | } |
| 59 | |
| 60 | if ((rstsr & RCC_C1BOOTRSTSCLRR_BORRSTF) != 0U) { |
| 61 | INFO(" Brownout Reset (rst_bor)\n"); |
| 62 | return; |
| 63 | } |
| 64 | |
| 65 | if ((rstsr & RCC_C1BOOTRSTSSETR_SYSC2RSTF) != 0U) { |
| 66 | INFO(" System reset (SYSRST) by M33\n"); |
| 67 | return; |
| 68 | } |
| 69 | |
| 70 | if ((rstsr & RCC_C1BOOTRSTSSETR_SYSC1RSTF) != 0U) { |
| 71 | INFO(" System reset (SYSRST) by A35\n"); |
| 72 | return; |
| 73 | } |
| 74 | |
| 75 | if ((rstsr & RCC_C1BOOTRSTSCLRR_HCSSRSTF) != 0U) { |
| 76 | INFO(" Clock failure on HSE\n"); |
| 77 | return; |
| 78 | } |
| 79 | |
| 80 | if ((rstsr & RCC_C1BOOTRSTSCLRR_IWDG1SYSRSTF) != 0U) { |
| 81 | INFO(" IWDG1 system reset (rst_iwdg1)\n"); |
| 82 | return; |
| 83 | } |
| 84 | |
| 85 | if ((rstsr & RCC_C1BOOTRSTSCLRR_IWDG2SYSRSTF) != 0U) { |
| 86 | INFO(" IWDG2 system reset (rst_iwdg2)\n"); |
| 87 | return; |
| 88 | } |
| 89 | |
| 90 | if ((rstsr & RCC_C1BOOTRSTSCLRR_IWDG3SYSRSTF) != 0U) { |
| 91 | INFO(" IWDG3 system reset (rst_iwdg3)\n"); |
| 92 | return; |
| 93 | } |
| 94 | |
| 95 | if ((rstsr & RCC_C1BOOTRSTSCLRR_IWDG4SYSRSTF) != 0U) { |
| 96 | INFO(" IWDG4 system reset (rst_iwdg4)\n"); |
| 97 | return; |
| 98 | } |
| 99 | |
| 100 | if ((rstsr & RCC_C1BOOTRSTSCLRR_IWDG5SYSRSTF) != 0U) { |
| 101 | INFO(" IWDG5 system reset (rst_iwdg5)\n"); |
| 102 | return; |
| 103 | } |
| 104 | |
| 105 | if ((rstsr & RCC_C1BOOTRSTSCLRR_C1P1RSTF) != 0U) { |
| 106 | INFO(" A35 processor core 1 reset\n"); |
| 107 | return; |
| 108 | } |
| 109 | |
| 110 | if ((rstsr & RCC_C1BOOTRSTSCLRR_PADRSTF) != 0U) { |
| 111 | INFO(" Pad Reset from NRST\n"); |
| 112 | return; |
| 113 | } |
| 114 | |
| 115 | if ((rstsr & RCC_C1BOOTRSTSCLRR_VCORERSTF) != 0U) { |
| 116 | INFO(" Reset due to a failure of VDD_CORE\n"); |
| 117 | return; |
| 118 | } |
| 119 | |
| 120 | if ((rstsr & RCC_C1BOOTRSTSCLRR_C1RSTF) != 0U) { |
| 121 | INFO(" A35 processor reset\n"); |
| 122 | return; |
| 123 | } |
| 124 | |
| 125 | ERROR(" Unidentified reset reason\n"); |
| 126 | } |
Yann Gautier | eb91af5 | 2023-06-14 18:05:47 +0200 | [diff] [blame] | 127 | |
Yann Gautier | a3f4638 | 2023-06-14 10:40:59 +0200 | [diff] [blame] | 128 | void bl2_el3_early_platform_setup(u_register_t arg0 __unused, |
| 129 | u_register_t arg1 __unused, |
| 130 | u_register_t arg2 __unused, |
| 131 | u_register_t arg3 __unused) |
| 132 | { |
Yann Gautier | 8053f2b | 2024-05-21 11:46:59 +0200 | [diff] [blame] | 133 | stm32mp_save_boot_ctx_address(BOOT_CTX_ADDR); |
Yann Gautier | a3f4638 | 2023-06-14 10:40:59 +0200 | [diff] [blame] | 134 | } |
| 135 | |
| 136 | void bl2_platform_setup(void) |
| 137 | { |
| 138 | } |
| 139 | |
Yann Gautier | 8053f2b | 2024-05-21 11:46:59 +0200 | [diff] [blame] | 140 | static void reset_backup_domain(void) |
| 141 | { |
| 142 | uintptr_t pwr_base = stm32mp_pwr_base(); |
| 143 | uintptr_t rcc_base = stm32mp_rcc_base(); |
| 144 | |
| 145 | /* |
| 146 | * Disable the backup domain write protection. |
| 147 | * The protection is enable at each reset by hardware |
| 148 | * and must be disabled by software. |
| 149 | */ |
| 150 | mmio_setbits_32(pwr_base + PWR_BDCR1, PWR_BDCR1_DBD3P); |
| 151 | |
| 152 | while ((mmio_read_32(pwr_base + PWR_BDCR1) & PWR_BDCR1_DBD3P) == 0U) { |
| 153 | ; |
| 154 | } |
| 155 | |
| 156 | /* Reset backup domain on cold boot cases */ |
| 157 | if ((mmio_read_32(rcc_base + RCC_BDCR) & RCC_BDCR_RTCCKEN) == 0U) { |
| 158 | mmio_setbits_32(rcc_base + RCC_BDCR, RCC_BDCR_VSWRST); |
| 159 | |
| 160 | while ((mmio_read_32(rcc_base + RCC_BDCR) & RCC_BDCR_VSWRST) == 0U) { |
| 161 | ; |
| 162 | } |
| 163 | |
| 164 | mmio_clrbits_32(rcc_base + RCC_BDCR, RCC_BDCR_VSWRST); |
| 165 | } |
| 166 | } |
| 167 | |
Yann Gautier | a3f4638 | 2023-06-14 10:40:59 +0200 | [diff] [blame] | 168 | void bl2_el3_plat_arch_setup(void) |
| 169 | { |
Yann Gautier | 8053f2b | 2024-05-21 11:46:59 +0200 | [diff] [blame] | 170 | const char *board_model; |
| 171 | boot_api_context_t *boot_context = |
| 172 | (boot_api_context_t *)stm32mp_get_boot_ctx_address(); |
| 173 | |
Yann Gautier | a585d76 | 2024-01-03 14:28:23 +0100 | [diff] [blame] | 174 | if (stm32_otp_probe() != 0U) { |
Yann Gautier | b619f49 | 2024-01-18 11:39:19 +0100 | [diff] [blame] | 175 | EARLY_ERROR("OTP probe failed\n"); |
Yann Gautier | a585d76 | 2024-01-03 14:28:23 +0100 | [diff] [blame] | 176 | panic(); |
| 177 | } |
Yann Gautier | 8053f2b | 2024-05-21 11:46:59 +0200 | [diff] [blame] | 178 | |
| 179 | mmap_add_region(BL_CODE_BASE, BL_CODE_BASE, |
| 180 | BL_CODE_END - BL_CODE_BASE, |
| 181 | MT_CODE | MT_SECURE); |
| 182 | |
| 183 | configure_mmu(); |
| 184 | |
Yann Gautier | 8053f2b | 2024-05-21 11:46:59 +0200 | [diff] [blame] | 185 | if (dt_open_and_check(STM32MP_DTB_BASE) < 0) { |
| 186 | panic(); |
| 187 | } |
| 188 | |
| 189 | reset_backup_domain(); |
| 190 | |
Yann Gautier | 40ff138 | 2024-05-21 20:54:04 +0200 | [diff] [blame] | 191 | /* |
| 192 | * Initialize DDR sub-system clock. This needs to be done before enabling DDR PLL (PLL2), |
| 193 | * and so before stm32mp2_clk_init(). |
| 194 | */ |
| 195 | ddr_sub_system_clk_init(); |
| 196 | |
Yann Gautier | 8053f2b | 2024-05-21 11:46:59 +0200 | [diff] [blame] | 197 | if (stm32mp2_clk_init() < 0) { |
| 198 | panic(); |
| 199 | } |
| 200 | |
Maxime Méré | b151f68 | 2024-09-13 17:57:58 +0200 | [diff] [blame] | 201 | #if STM32MP_DDR_FIP_IO_STORAGE |
| 202 | /* |
| 203 | * RISAB3 setup (dedicated for SRAM1) |
| 204 | * |
| 205 | * Allow secure read/writes data accesses to non-secure |
| 206 | * blocks or pages, all RISAB registers are writable. |
| 207 | * DDR firmwares are saved there before being loaded in DDRPHY memory. |
| 208 | */ |
| 209 | mmio_write_32(RISAB3_BASE + RISAB_CR, RISAB_CR_SRWIAD); |
| 210 | #endif |
| 211 | |
Yann Gautier | 8053f2b | 2024-05-21 11:46:59 +0200 | [diff] [blame] | 212 | stm32_save_boot_info(boot_context); |
| 213 | |
| 214 | if (stm32mp_uart_console_setup() != 0) { |
| 215 | goto skip_console_init; |
| 216 | } |
| 217 | |
Yann Gautier | 400dcac | 2024-06-21 14:49:47 +0200 | [diff] [blame] | 218 | stm32mp_print_cpuinfo(); |
| 219 | |
Yann Gautier | 8053f2b | 2024-05-21 11:46:59 +0200 | [diff] [blame] | 220 | board_model = dt_get_board_model(); |
| 221 | if (board_model != NULL) { |
| 222 | NOTICE("Model: %s\n", board_model); |
| 223 | } |
| 224 | |
Yann Gautier | a65743b | 2022-04-15 16:15:25 +0200 | [diff] [blame] | 225 | stm32mp_print_boardinfo(); |
| 226 | |
Yann Gautier | 8053f2b | 2024-05-21 11:46:59 +0200 | [diff] [blame] | 227 | print_reset_reason(); |
| 228 | |
| 229 | skip_console_init: |
Yann Gautier | 3ad65ce | 2024-05-21 12:03:56 +0200 | [diff] [blame] | 230 | if (fixed_regulator_register() != 0) { |
| 231 | panic(); |
| 232 | } |
| 233 | |
Pascal Paillet | 3263aea | 2022-12-16 14:59:34 +0100 | [diff] [blame] | 234 | if (dt_pmic_status() > 0) { |
| 235 | initialize_pmic(); |
| 236 | } |
| 237 | |
Yann Gautier | 8053f2b | 2024-05-21 11:46:59 +0200 | [diff] [blame] | 238 | fconf_populate("TB_FW", STM32MP_DTB_BASE); |
| 239 | |
Maxime Méré | 98768bf | 2024-09-19 09:54:28 +0200 | [diff] [blame] | 240 | /* |
| 241 | * RISAB5 setup (dedicated for RETRAM) |
| 242 | * |
| 243 | * Allow secure read/writes data accesses to non-secure |
| 244 | * blocks or pages, all RISAB registers are writable. |
| 245 | * DDR retention registers are saved there and restored |
| 246 | * when exiting standby low power state. |
| 247 | */ |
| 248 | mmio_write_32(RISAB5_BASE + RISAB_CR, RISAB_CR_SRWIAD); |
| 249 | |
Yann Gautier | 8053f2b | 2024-05-21 11:46:59 +0200 | [diff] [blame] | 250 | stm32mp_io_setup(); |
Yann Gautier | a3f4638 | 2023-06-14 10:40:59 +0200 | [diff] [blame] | 251 | } |
Yann Gautier | 88ca335 | 2024-05-22 16:46:37 +0200 | [diff] [blame] | 252 | |
| 253 | /******************************************************************************* |
| 254 | * This function can be used by the platforms to update/use image |
| 255 | * information for given `image_id`. |
| 256 | ******************************************************************************/ |
| 257 | int bl2_plat_handle_post_image_load(unsigned int image_id) |
| 258 | { |
| 259 | int err = 0; |
Yann Gautier | ece4c25 | 2023-06-13 18:45:03 +0200 | [diff] [blame] | 260 | bl_mem_params_node_t *bl_mem_params = get_bl_mem_params_node(image_id); |
| 261 | const struct dyn_cfg_dtb_info_t *config_info; |
| 262 | unsigned int i; |
| 263 | const unsigned int image_ids[] = { |
| 264 | BL31_IMAGE_ID, |
| 265 | }; |
Yann Gautier | 88ca335 | 2024-05-22 16:46:37 +0200 | [diff] [blame] | 266 | |
| 267 | assert(bl_mem_params != NULL); |
| 268 | |
| 269 | #if STM32MP_SDMMC || STM32MP_EMMC |
| 270 | /* |
| 271 | * Invalidate remaining data read from MMC but not flushed by load_image_flush(). |
| 272 | * We take the worst case which is 2 MMC blocks. |
| 273 | */ |
| 274 | if ((image_id != FW_CONFIG_ID) && |
| 275 | ((bl_mem_params->image_info.h.attr & IMAGE_ATTRIB_SKIP_LOADING) == 0U)) { |
| 276 | inv_dcache_range(bl_mem_params->image_info.image_base + |
| 277 | bl_mem_params->image_info.image_size, |
| 278 | 2U * MMC_BLOCK_SIZE); |
| 279 | } |
| 280 | #endif /* STM32MP_SDMMC || STM32MP_EMMC */ |
| 281 | |
| 282 | switch (image_id) { |
| 283 | case FW_CONFIG_ID: |
| 284 | /* Set global DTB info for fixed fw_config information */ |
| 285 | set_config_info(STM32MP_FW_CONFIG_BASE, ~0UL, STM32MP_FW_CONFIG_MAX_SIZE, |
| 286 | FW_CONFIG_ID); |
| 287 | fconf_populate("FW_CONFIG", STM32MP_FW_CONFIG_BASE); |
| 288 | |
Yann Gautier | ece4c25 | 2023-06-13 18:45:03 +0200 | [diff] [blame] | 289 | /* Iterate through all the fw config IDs */ |
| 290 | for (i = 0U; i < ARRAY_SIZE(image_ids); i++) { |
| 291 | bl_mem_params = get_bl_mem_params_node(image_ids[i]); |
| 292 | assert(bl_mem_params != NULL); |
| 293 | |
| 294 | config_info = FCONF_GET_PROPERTY(dyn_cfg, dtb, image_ids[i]); |
| 295 | if (config_info == NULL) { |
| 296 | continue; |
| 297 | } |
| 298 | |
| 299 | bl_mem_params->image_info.image_base = config_info->config_addr; |
| 300 | bl_mem_params->image_info.image_max_size = config_info->config_max_size; |
| 301 | |
| 302 | bl_mem_params->image_info.h.attr &= ~IMAGE_ATTRIB_SKIP_LOADING; |
| 303 | |
| 304 | switch (image_ids[i]) { |
| 305 | case BL31_IMAGE_ID: |
| 306 | bl_mem_params->ep_info.pc = config_info->config_addr; |
| 307 | break; |
| 308 | default: |
| 309 | return -EINVAL; |
| 310 | } |
| 311 | } |
| 312 | |
Yann Gautier | cb84bf6 | 2024-09-02 11:40:43 +0200 | [diff] [blame] | 313 | /* |
| 314 | * After this step, the BL2 device tree area will be overwritten |
| 315 | * with BL31 binary, no other data should be read from BL2 DT. |
| 316 | */ |
Yann Gautier | 88ca335 | 2024-05-22 16:46:37 +0200 | [diff] [blame] | 317 | |
| 318 | break; |
| 319 | |
| 320 | default: |
| 321 | /* Do nothing in default case */ |
| 322 | break; |
| 323 | } |
| 324 | |
| 325 | return err; |
| 326 | } |