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