blob: 1504360866068797cf4793496f2331b6931c3e4e [file] [log] [blame]
Yann Gautier4b0c72a2018-07-16 10:54:09 +02001/*
Yann Gautierb76c61a2020-12-16 10:17:35 +01002 * Copyright (c) 2015-2022, ARM Limited and Contributors. All rights reserved.
Yann Gautier4b0c72a2018-07-16 10:54:09 +02003 *
4 * SPDX-License-Identifier: BSD-3-Clause
5 */
6
Yann Gautier4b0c72a2018-07-16 10:54:09 +02007#include <assert.h>
Yann Gautier658775c2021-07-06 10:00:44 +02008#include <errno.h>
Antonio Nino Diaze0f90632018-12-14 00:18:21 +00009#include <string.h>
10
Antonio Nino Diaze0f90632018-12-14 00:18:21 +000011#include <arch_helpers.h>
12#include <common/bl_common.h>
13#include <common/debug.h>
14#include <common/desc_image_load.h>
Antonio Nino Diaze0f90632018-12-14 00:18:21 +000015#include <drivers/generic_delay_timer.h>
Yann Gautiera3bd8d12021-06-18 11:33:26 +020016#include <drivers/mmc.h>
Yann Gautier3edc7c32019-05-20 19:17:08 +020017#include <drivers/st/bsec.h>
Pascal Pailletfc7b8052021-01-29 14:48:49 +010018#include <drivers/st/regulator_fixed.h>
Yann Gautier091eab52019-06-04 18:06:34 +020019#include <drivers/st/stm32_iwdg.h>
Yann Gautier3d8497c2021-10-18 16:06:22 +020020#include <drivers/st/stm32_uart.h>
Antonio Nino Diaze0f90632018-12-14 00:18:21 +000021#include <drivers/st/stm32mp1_clk.h>
Antonio Nino Diaze0f90632018-12-14 00:18:21 +000022#include <drivers/st/stm32mp1_pwr.h>
23#include <drivers/st/stm32mp1_ram.h>
Yann Gautier0c810882021-12-17 09:53:04 +010024#include <drivers/st/stm32mp_pmic.h>
Yann Gautier658775c2021-07-06 10:00:44 +020025#include <lib/fconf/fconf.h>
26#include <lib/fconf/fconf_dyn_cfg_getter.h>
Antonio Nino Diaze0f90632018-12-14 00:18:21 +000027#include <lib/mmio.h>
Yann Gautierb3386f72019-04-19 09:41:01 +020028#include <lib/optee_utils.h>
Antonio Nino Diaze0f90632018-12-14 00:18:21 +000029#include <lib/xlat_tables/xlat_tables_v2.h>
30#include <plat/common/platform.h>
31
Yann Gautier0c810882021-12-17 09:53:04 +010032#include <platform_def.h>
Sughosh Ganu03e2f802021-12-01 15:56:27 +053033#include <stm32mp_common.h>
Yann Gautier091eab52019-06-04 18:06:34 +020034#include <stm32mp1_dbgmcu.h>
Yann Gautier4b0c72a2018-07-16 10:54:09 +020035
Lionel Debieve7192a002020-01-28 09:02:41 +010036#if DEBUG
37static const char debug_msg[] = {
38 "***************************************************\n"
39 "** DEBUG ACCESS PORT IS OPEN! **\n"
40 "** This boot image is only for debugging purpose **\n"
41 "** and is unsafe for production use. **\n"
42 "** **\n"
43 "** If you see this message and you are not **\n"
44 "** debugging report this immediately to your **\n"
45 "** vendor! **\n"
46 "***************************************************\n"
47};
48#endif
49
Lionel Debieve7bd96f42019-09-03 12:22:23 +020050static struct stm32mp_auth_ops stm32mp1_auth_ops;
Yann Gautier8593e442018-11-14 18:46:15 +010051
Yann Gautierf9d40d52019-01-17 14:41:46 +010052static void print_reset_reason(void)
53{
Yann Gautier3d78a2e2019-02-14 11:01:20 +010054 uint32_t rstsr = mmio_read_32(stm32mp_rcc_base() + RCC_MP_RSTSCLRR);
Yann Gautierf9d40d52019-01-17 14:41:46 +010055
56 if (rstsr == 0U) {
57 WARN("Reset reason unknown\n");
58 return;
59 }
60
61 INFO("Reset reason (0x%x):\n", rstsr);
62
63 if ((rstsr & RCC_MP_RSTSCLRR_PADRSTF) == 0U) {
64 if ((rstsr & RCC_MP_RSTSCLRR_STDBYRSTF) != 0U) {
65 INFO("System exits from STANDBY\n");
66 return;
67 }
68
69 if ((rstsr & RCC_MP_RSTSCLRR_CSTDBYRSTF) != 0U) {
70 INFO("MPU exits from CSTANDBY\n");
71 return;
72 }
73 }
74
75 if ((rstsr & RCC_MP_RSTSCLRR_PORRSTF) != 0U) {
76 INFO(" Power-on Reset (rst_por)\n");
77 return;
78 }
79
80 if ((rstsr & RCC_MP_RSTSCLRR_BORRSTF) != 0U) {
81 INFO(" Brownout Reset (rst_bor)\n");
82 return;
83 }
84
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 }
93
94 if ((rstsr & RCC_MP_RSTSCLRR_MPSYSRSTF) != 0U) {
95 INFO(" System reset generated by MPU (MPSYSRST)\n");
96 return;
97 }
98
99 if ((rstsr & RCC_MP_RSTSCLRR_HCSSRSTF) != 0U) {
100 INFO(" Reset due to a clock failure on HSE\n");
101 return;
102 }
103
104 if ((rstsr & RCC_MP_RSTSCLRR_IWDG1RSTF) != 0U) {
105 INFO(" IWDG1 Reset (rst_iwdg1)\n");
106 return;
107 }
108
109 if ((rstsr & RCC_MP_RSTSCLRR_IWDG2RSTF) != 0U) {
110 INFO(" IWDG2 Reset (rst_iwdg2)\n");
111 return;
112 }
113
114 if ((rstsr & RCC_MP_RSTSCLRR_MPUP0RSTF) != 0U) {
115 INFO(" MPU Processor 0 Reset\n");
116 return;
117 }
118
119 if ((rstsr & RCC_MP_RSTSCLRR_MPUP1RSTF) != 0U) {
120 INFO(" MPU Processor 1 Reset\n");
121 return;
122 }
123
124 if ((rstsr & RCC_MP_RSTSCLRR_PADRSTF) != 0U) {
125 INFO(" Pad Reset from NRST\n");
126 return;
127 }
128
129 if ((rstsr & RCC_MP_RSTSCLRR_VCORERSTF) != 0U) {
130 INFO(" Reset due to a failure of VDD_CORE\n");
131 return;
132 }
133
134 ERROR(" Unidentified reset reason\n");
135}
136
137void bl2_el3_early_platform_setup(u_register_t arg0,
138 u_register_t arg1 __unused,
139 u_register_t arg2 __unused,
140 u_register_t arg3 __unused)
Yann Gautier4b0c72a2018-07-16 10:54:09 +0200141{
Yann Gautiera2e2a302019-02-14 11:13:39 +0100142 stm32mp_save_boot_ctx_address(arg0);
Yann Gautier4b0c72a2018-07-16 10:54:09 +0200143}
144
145void bl2_platform_setup(void)
146{
Yann Gautiercaf575b2018-07-24 17:18:19 +0200147 int ret;
148
Yann Gautiercaf575b2018-07-24 17:18:19 +0200149 ret = stm32mp1_ddr_probe();
150 if (ret < 0) {
151 ERROR("Invalid DDR init: error %d\n", ret);
152 panic();
153 }
154
Yann Gautierf3bd87e2020-09-04 15:55:53 +0200155 /* Map DDR for binary load, now with cacheable attribute */
Yann Gautiera55169b2020-01-10 18:18:59 +0100156 ret = mmap_add_dynamic_region(STM32MP_DDR_BASE, STM32MP_DDR_BASE,
Yann Gautierf3bd87e2020-09-04 15:55:53 +0200157 STM32MP_DDR_MAX_SIZE, MT_MEMORY | MT_RW | MT_SECURE);
158 if (ret < 0) {
159 ERROR("DDR mapping: error %d\n", ret);
160 panic();
161 }
Yann Gautiera55169b2020-01-10 18:18:59 +0100162
Yann Gautier0ed7b2a2021-05-19 18:48:16 +0200163#if STM32MP_USE_STM32IMAGE
Yann Gautierb3386f72019-04-19 09:41:01 +0200164#ifdef AARCH32_SP_OPTEE
165 INFO("BL2 runs OP-TEE setup\n");
Yann Gautierb3386f72019-04-19 09:41:01 +0200166#else
Yann Gautier4b0c72a2018-07-16 10:54:09 +0200167 INFO("BL2 runs SP_MIN setup\n");
Yann Gautierb3386f72019-04-19 09:41:01 +0200168#endif
Yann Gautier0ed7b2a2021-05-19 18:48:16 +0200169#endif /* STM32MP_USE_STM32IMAGE */
Yann Gautier4b0c72a2018-07-16 10:54:09 +0200170}
171
Yann Gautier5c1dab32019-04-17 15:12:58 +0200172static void update_monotonic_counter(void)
173{
174 uint32_t version;
175 uint32_t otp;
176
177 CASSERT(STM32_TF_VERSION <= MAX_MONOTONIC_VALUE,
178 assert_stm32mp1_monotonic_counter_reach_max);
179
180 /* Check if monotonic counter needs to be incremented */
181 if (stm32_get_otp_index(MONOTONIC_OTP, &otp, NULL) != 0) {
182 panic();
183 }
184
185 if (stm32_get_otp_value_from_idx(otp, &version) != 0) {
186 panic();
187 }
188
189 if ((version + 1U) < BIT(STM32_TF_VERSION)) {
190 uint32_t result;
191
192 /* Need to increment the monotonic counter. */
193 version = BIT(STM32_TF_VERSION) - 1U;
194
195 result = bsec_program_otp(version, otp);
196 if (result != BSEC_OK) {
197 ERROR("BSEC: MONOTONIC_OTP program Error %u\n",
198 result);
199 panic();
200 }
201 INFO("Monotonic counter has been incremented (value 0x%x)\n",
202 version);
203 }
204}
205
Yann Gautier4b0c72a2018-07-16 10:54:09 +0200206void bl2_el3_plat_arch_setup(void)
207{
Yann Gautier69035a82018-07-05 16:48:16 +0200208 const char *board_model;
Yann Gautier41934662018-07-20 11:36:05 +0200209 boot_api_context_t *boot_context =
Yann Gautiera2e2a302019-02-14 11:13:39 +0100210 (boot_api_context_t *)stm32mp_get_boot_ctx_address();
Yann Gautier3d78a2e2019-02-14 11:01:20 +0100211 uintptr_t pwr_base;
212 uintptr_t rcc_base;
Yann Gautier41934662018-07-20 11:36:05 +0200213
Nicolas Le Bayon97287cd2019-05-20 18:35:02 +0200214 if (bsec_probe() != 0U) {
215 panic();
216 }
217
Yann Gautierf9d40d52019-01-17 14:41:46 +0100218 mmap_add_region(BL_CODE_BASE, BL_CODE_BASE,
219 BL_CODE_END - BL_CODE_BASE,
220 MT_CODE | MT_SECURE);
221
Yann Gautier0ed7b2a2021-05-19 18:48:16 +0200222#if STM32MP_USE_STM32IMAGE
Yann Gautierb3386f72019-04-19 09:41:01 +0200223#ifdef AARCH32_SP_OPTEE
Yann Gautierb3386f72019-04-19 09:41:01 +0200224 mmap_add_region(STM32MP_OPTEE_BASE, STM32MP_OPTEE_BASE,
225 STM32MP_OPTEE_SIZE,
226 MT_MEMORY | MT_RW | MT_SECURE);
Yann Gautier90f84d72021-07-13 14:44:09 +0200227#else
228 /* Prevent corruption of preloaded BL32 */
229 mmap_add_region(BL32_BASE, BL32_BASE,
230 BL32_LIMIT - BL32_BASE,
231 MT_RO_DATA | MT_SECURE);
Yann Gautierb3386f72019-04-19 09:41:01 +0200232#endif
Yann Gautier0ed7b2a2021-05-19 18:48:16 +0200233#endif /* STM32MP_USE_STM32IMAGE */
234
Yann Gautierf9d40d52019-01-17 14:41:46 +0100235 /* Prevent corruption of preloaded Device Tree */
236 mmap_add_region(DTB_BASE, DTB_BASE,
237 DTB_LIMIT - DTB_BASE,
Yann Gautier3d33df62019-12-17 17:11:10 +0100238 MT_RO_DATA | MT_SECURE);
Yann Gautierf9d40d52019-01-17 14:41:46 +0100239
240 configure_mmu();
241
Yann Gautier05773eb2020-08-24 11:51:50 +0200242 if (dt_open_and_check(STM32MP_DTB_BASE) < 0) {
Yann Gautierf9d40d52019-01-17 14:41:46 +0100243 panic();
244 }
245
Yann Gautier3d78a2e2019-02-14 11:01:20 +0100246 pwr_base = stm32mp_pwr_base();
247 rcc_base = stm32mp_rcc_base();
248
Yann Gautier4b0c72a2018-07-16 10:54:09 +0200249 /*
250 * Disable the backup domain write protection.
251 * The protection is enable at each reset by hardware
252 * and must be disabled by software.
253 */
Yann Gautier3d78a2e2019-02-14 11:01:20 +0100254 mmio_setbits_32(pwr_base + PWR_CR1, PWR_CR1_DBP);
Yann Gautier4b0c72a2018-07-16 10:54:09 +0200255
Yann Gautier3d78a2e2019-02-14 11:01:20 +0100256 while ((mmio_read_32(pwr_base + PWR_CR1) & PWR_CR1_DBP) == 0U) {
Yann Gautier4b0c72a2018-07-16 10:54:09 +0200257 ;
258 }
259
260 /* Reset backup domain on cold boot cases */
Yann Gautier3d78a2e2019-02-14 11:01:20 +0100261 if ((mmio_read_32(rcc_base + RCC_BDCR) & RCC_BDCR_RTCSRC_MASK) == 0U) {
262 mmio_setbits_32(rcc_base + RCC_BDCR, RCC_BDCR_VSWRST);
Yann Gautier4b0c72a2018-07-16 10:54:09 +0200263
Yann Gautier3d78a2e2019-02-14 11:01:20 +0100264 while ((mmio_read_32(rcc_base + RCC_BDCR) & RCC_BDCR_VSWRST) ==
Yann Gautier4b0c72a2018-07-16 10:54:09 +0200265 0U) {
266 ;
267 }
268
Yann Gautier3d78a2e2019-02-14 11:01:20 +0100269 mmio_clrbits_32(rcc_base + RCC_BDCR, RCC_BDCR_VSWRST);
Yann Gautier4b0c72a2018-07-16 10:54:09 +0200270 }
271
Yann Gautiered342322019-02-15 17:33:27 +0100272 /* Disable MCKPROT */
273 mmio_clrbits_32(rcc_base + RCC_TZCR, RCC_TZCR_MCKPROT);
274
Yann Gautierc0882f42021-04-27 18:19:13 +0200275 /*
276 * Set minimum reset pulse duration to 31ms for discrete power
277 * supplied boards.
278 */
279 if (dt_pmic_status() <= 0) {
280 mmio_clrsetbits_32(rcc_base + RCC_RDLSICR,
281 RCC_RDLSICR_MRD_MASK,
282 31U << RCC_RDLSICR_MRD_SHIFT);
283 }
284
Yann Gautier4b0c72a2018-07-16 10:54:09 +0200285 generic_delay_timer_init();
286
Yann Gautier3d8497c2021-10-18 16:06:22 +0200287#if STM32MP_UART_PROGRAMMER
288 /* Disable programmer UART before changing clock tree */
289 if (boot_context->boot_interface_selected ==
290 BOOT_API_CTX_BOOT_INTERFACE_SEL_SERIAL_UART) {
291 uintptr_t uart_prog_addr =
292 get_uart_address(boot_context->boot_interface_instance);
293
294 stm32_uart_stop(uart_prog_addr);
295 }
296#endif
Yann Gautier9aea69e2018-07-24 17:13:36 +0200297 if (stm32mp1_clk_probe() < 0) {
298 panic();
299 }
300
301 if (stm32mp1_clk_init() < 0) {
302 panic();
303 }
304
Yann Gautier6eef5252021-12-10 17:04:40 +0100305 stm32_save_boot_interface(boot_context->boot_interface_selected,
306 boot_context->boot_interface_instance);
307
Yann Gautiercd16df32021-06-04 14:04:05 +0200308#if STM32MP_USB_PROGRAMMER
309 /* Deconfigure all UART RX pins configured by ROM code */
310 stm32mp1_deconfigure_uart_pins();
311#endif
312
Yann Gautier66baa962021-10-18 14:01:00 +0200313 if (stm32mp_uart_console_setup() != 0) {
Yann Gautier69035a82018-07-05 16:48:16 +0200314 goto skip_console_init;
315 }
316
Yann Gautierc7374052019-06-04 18:02:37 +0200317 stm32mp_print_cpuinfo();
318
Yann Gautier69035a82018-07-05 16:48:16 +0200319 board_model = dt_get_board_model();
320 if (board_model != NULL) {
Yann Gautierf9d40d52019-01-17 14:41:46 +0100321 NOTICE("Model: %s\n", board_model);
Yann Gautier69035a82018-07-05 16:48:16 +0200322 }
323
Yann Gautier35dc0772019-05-13 18:34:48 +0200324 stm32mp_print_boardinfo();
325
Lionel Debieve7bd96f42019-09-03 12:22:23 +0200326 if (boot_context->auth_status != BOOT_API_CTX_AUTH_NO) {
327 NOTICE("Bootrom authentication %s\n",
328 (boot_context->auth_status == BOOT_API_CTX_AUTH_FAILED) ?
329 "failed" : "succeeded");
330 }
331
Yann Gautier69035a82018-07-05 16:48:16 +0200332skip_console_init:
Pascal Pailletfc7b8052021-01-29 14:48:49 +0100333 if (fixed_regulator_register() != 0) {
334 panic();
335 }
336
Yann Gautier45c1e582020-09-17 11:54:52 +0200337 if (dt_pmic_status() > 0) {
338 initialize_pmic();
Nicolas Le Bayon0b10b652019-11-18 13:13:36 +0100339 print_pmic_info_and_debug();
Yann Gautier45c1e582020-09-17 11:54:52 +0200340 }
341
342 stm32mp1_syscfg_init();
343
Yann Gautier091eab52019-06-04 18:06:34 +0200344 if (stm32_iwdg_init() < 0) {
345 panic();
346 }
347
348 stm32_iwdg_refresh();
349
Lionel Debieve7192a002020-01-28 09:02:41 +0100350 if (bsec_read_debug_conf() != 0U) {
351 if (stm32mp_is_closed_device()) {
352#if DEBUG
353 WARN("\n%s", debug_msg);
354#else
355 ERROR("***Debug opened on closed chip***\n");
356#endif
357 }
358 }
359
Lionel Debieve06bc62d2019-12-06 12:42:20 +0100360 if (stm32mp_is_auth_supported()) {
361 stm32mp1_auth_ops.check_key =
362 boot_context->bootrom_ecdsa_check_key;
363 stm32mp1_auth_ops.verify_signature =
364 boot_context->bootrom_ecdsa_verify_signature;
Lionel Debieve7bd96f42019-09-03 12:22:23 +0200365
Lionel Debieve06bc62d2019-12-06 12:42:20 +0100366 stm32mp_init_auth(&stm32mp1_auth_ops);
367 }
Lionel Debieve7bd96f42019-09-03 12:22:23 +0200368
Yann Gautiercaf575b2018-07-24 17:18:19 +0200369 stm32mp1_arch_security_setup();
370
Yann Gautierf9d40d52019-01-17 14:41:46 +0100371 print_reset_reason();
372
Yann Gautier5c1dab32019-04-17 15:12:58 +0200373 update_monotonic_counter();
374
Yann Gautierb76c61a2020-12-16 10:17:35 +0100375 stm32mp1_syscfg_enable_io_compensation_finish();
376
Yann Gautier29f1f942021-07-13 18:07:41 +0200377#if !STM32MP_USE_STM32IMAGE
378 fconf_populate("TB_FW", STM32MP_DTB_BASE);
379#endif /* !STM32MP_USE_STM32IMAGE */
380
Yann Gautiera2e2a302019-02-14 11:13:39 +0100381 stm32mp_io_setup();
Yann Gautier4b0c72a2018-07-16 10:54:09 +0200382}
Yann Gautierb3386f72019-04-19 09:41:01 +0200383
Yann Gautierb3386f72019-04-19 09:41:01 +0200384/*******************************************************************************
385 * This function can be used by the platforms to update/use image
386 * information for given `image_id`.
387 ******************************************************************************/
388int bl2_plat_handle_post_image_load(unsigned int image_id)
389{
390 int err = 0;
391 bl_mem_params_node_t *bl_mem_params = get_bl_mem_params_node(image_id);
392 bl_mem_params_node_t *bl32_mem_params;
Yann Gautier0ed7b2a2021-05-19 18:48:16 +0200393 bl_mem_params_node_t *pager_mem_params __unused;
394 bl_mem_params_node_t *paged_mem_params __unused;
Yann Gautier658775c2021-07-06 10:00:44 +0200395#if !STM32MP_USE_STM32IMAGE
396 const struct dyn_cfg_dtb_info_t *config_info;
397 bl_mem_params_node_t *tos_fw_mem_params;
398 unsigned int i;
Yann Gautierfd648352021-12-13 15:24:41 +0100399 unsigned int idx;
Yann Gautier658775c2021-07-06 10:00:44 +0200400 unsigned long long ddr_top __unused;
401 const unsigned int image_ids[] = {
402 BL32_IMAGE_ID,
403 BL33_IMAGE_ID,
404 HW_CONFIG_ID,
405 TOS_FW_CONFIG_ID,
406 };
407#endif /* !STM32MP_USE_STM32IMAGE */
Yann Gautierb3386f72019-04-19 09:41:01 +0200408
409 assert(bl_mem_params != NULL);
410
411 switch (image_id) {
Yann Gautier658775c2021-07-06 10:00:44 +0200412#if !STM32MP_USE_STM32IMAGE
413 case FW_CONFIG_ID:
414 /* Set global DTB info for fixed fw_config information */
415 set_config_info(STM32MP_FW_CONFIG_BASE, STM32MP_FW_CONFIG_MAX_SIZE, FW_CONFIG_ID);
416 fconf_populate("FW_CONFIG", STM32MP_FW_CONFIG_BASE);
417
Yann Gautierfd648352021-12-13 15:24:41 +0100418 idx = dyn_cfg_dtb_info_get_index(TOS_FW_CONFIG_ID);
419
Yann Gautier658775c2021-07-06 10:00:44 +0200420 /* Iterate through all the fw config IDs */
421 for (i = 0U; i < ARRAY_SIZE(image_ids); i++) {
Yann Gautierfd648352021-12-13 15:24:41 +0100422 if ((image_ids[i] == TOS_FW_CONFIG_ID) && (idx == FCONF_INVALID_IDX)) {
423 continue;
424 }
425
Yann Gautier658775c2021-07-06 10:00:44 +0200426 bl_mem_params = get_bl_mem_params_node(image_ids[i]);
427 assert(bl_mem_params != NULL);
428
429 config_info = FCONF_GET_PROPERTY(dyn_cfg, dtb, image_ids[i]);
430 if (config_info == NULL) {
431 continue;
432 }
433
434 bl_mem_params->image_info.image_base = config_info->config_addr;
435 bl_mem_params->image_info.image_max_size = config_info->config_max_size;
436
437 bl_mem_params->image_info.h.attr &= ~IMAGE_ATTRIB_SKIP_LOADING;
438
439 switch (image_ids[i]) {
440 case BL32_IMAGE_ID:
441 bl_mem_params->ep_info.pc = config_info->config_addr;
442
443 /* In case of OPTEE, initialize address space with tos_fw addr */
444 pager_mem_params = get_bl_mem_params_node(BL32_EXTRA1_IMAGE_ID);
445 pager_mem_params->image_info.image_base = config_info->config_addr;
446 pager_mem_params->image_info.image_max_size =
447 config_info->config_max_size;
448
449 /* Init base and size for pager if exist */
450 paged_mem_params = get_bl_mem_params_node(BL32_EXTRA2_IMAGE_ID);
451 paged_mem_params->image_info.image_base = STM32MP_DDR_BASE +
452 (dt_get_ddr_size() - STM32MP_DDR_S_SIZE -
453 STM32MP_DDR_SHMEM_SIZE);
454 paged_mem_params->image_info.image_max_size = STM32MP_DDR_S_SIZE;
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;
470#endif /* !STM32MP_USE_STM32IMAGE */
471
Yann Gautierb3386f72019-04-19 09:41:01 +0200472 case BL32_IMAGE_ID:
Yann Gautier90f84d72021-07-13 14:44:09 +0200473 if (optee_header_is_valid(bl_mem_params->image_info.image_base)) {
474 /* BL32 is OP-TEE header */
475 bl_mem_params->ep_info.pc = bl_mem_params->image_info.image_base;
476 pager_mem_params = get_bl_mem_params_node(BL32_EXTRA1_IMAGE_ID);
477 paged_mem_params = get_bl_mem_params_node(BL32_EXTRA2_IMAGE_ID);
478 assert((pager_mem_params != NULL) && (paged_mem_params != NULL));
Yann Gautierb3386f72019-04-19 09:41:01 +0200479
Yann Gautier0ed7b2a2021-05-19 18:48:16 +0200480#if STM32MP_USE_STM32IMAGE && defined(AARCH32_SP_OPTEE)
Yann Gautier90f84d72021-07-13 14:44:09 +0200481 /* Set OP-TEE extra image load areas at run-time */
482 pager_mem_params->image_info.image_base = STM32MP_OPTEE_BASE;
483 pager_mem_params->image_info.image_max_size = STM32MP_OPTEE_SIZE;
Yann Gautierb3386f72019-04-19 09:41:01 +0200484
Yann Gautier90f84d72021-07-13 14:44:09 +0200485 paged_mem_params->image_info.image_base = STM32MP_DDR_BASE +
486 dt_get_ddr_size() -
487 STM32MP_DDR_S_SIZE -
488 STM32MP_DDR_SHMEM_SIZE;
489 paged_mem_params->image_info.image_max_size = STM32MP_DDR_S_SIZE;
Yann Gautier0ed7b2a2021-05-19 18:48:16 +0200490#endif /* STM32MP_USE_STM32IMAGE && defined(AARCH32_SP_OPTEE) */
Yann Gautierb3386f72019-04-19 09:41:01 +0200491
Yann Gautier90f84d72021-07-13 14:44:09 +0200492 err = parse_optee_header(&bl_mem_params->ep_info,
493 &pager_mem_params->image_info,
494 &paged_mem_params->image_info);
495 if (err) {
496 ERROR("OPTEE header parse error.\n");
497 panic();
498 }
Yann Gautierb3386f72019-04-19 09:41:01 +0200499
Yann Gautier90f84d72021-07-13 14:44:09 +0200500 /* Set optee boot info from parsed header data */
501 bl_mem_params->ep_info.args.arg0 = paged_mem_params->image_info.image_base;
502 bl_mem_params->ep_info.args.arg1 = 0; /* Unused */
503 bl_mem_params->ep_info.args.arg2 = 0; /* No DT supported */
Yann Gautier0ed7b2a2021-05-19 18:48:16 +0200504 } else {
505#if !STM32MP_USE_STM32IMAGE
506 bl_mem_params->ep_info.pc = bl_mem_params->image_info.image_base;
Yann Gautier658775c2021-07-06 10:00:44 +0200507 tos_fw_mem_params = get_bl_mem_params_node(TOS_FW_CONFIG_ID);
508 bl_mem_params->image_info.image_max_size +=
509 tos_fw_mem_params->image_info.image_max_size;
Yann Gautier0ed7b2a2021-05-19 18:48:16 +0200510#endif /* !STM32MP_USE_STM32IMAGE */
511 bl_mem_params->ep_info.args.arg0 = 0;
Yann Gautier90f84d72021-07-13 14:44:09 +0200512 }
Yann Gautierb3386f72019-04-19 09:41:01 +0200513 break;
514
515 case BL33_IMAGE_ID:
516 bl32_mem_params = get_bl_mem_params_node(BL32_IMAGE_ID);
517 assert(bl32_mem_params != NULL);
518 bl32_mem_params->ep_info.lr_svc = bl_mem_params->ep_info.pc;
Sughosh Ganu03e2f802021-12-01 15:56:27 +0530519#if !STM32MP_USE_STM32IMAGE && PSA_FWU_SUPPORT
520 stm32mp1_fwu_set_boot_idx();
521#endif /* !STM32MP_USE_STM32IMAGE && PSA_FWU_SUPPORT */
Yann Gautierb3386f72019-04-19 09:41:01 +0200522 break;
523
524 default:
525 /* Do nothing in default case */
526 break;
527 }
528
Yann Gautiera3bd8d12021-06-18 11:33:26 +0200529#if STM32MP_SDMMC || STM32MP_EMMC
530 /*
531 * Invalidate remaining data read from MMC but not flushed by load_image_flush().
532 * We take the worst case which is 2 MMC blocks.
533 */
534 if ((image_id != FW_CONFIG_ID) &&
535 ((bl_mem_params->image_info.h.attr & IMAGE_ATTRIB_SKIP_LOADING) == 0U)) {
536 inv_dcache_range(bl_mem_params->image_info.image_base +
537 bl_mem_params->image_info.image_size,
538 2U * MMC_BLOCK_SIZE);
539 }
540#endif /* STM32MP_SDMMC || STM32MP_EMMC */
541
Yann Gautierb3386f72019-04-19 09:41:01 +0200542 return err;
543}
Yann Gautierd2d9b962021-08-16 11:58:01 +0200544
545void bl2_el3_plat_prepare_exit(void)
546{
Patrick Delaunay9c5ee782021-07-06 14:07:56 +0200547 uint16_t boot_itf = stm32mp_get_boot_itf_selected();
548
549 switch (boot_itf) {
Patrick Delaunaye50571b2021-10-28 13:48:52 +0200550#if STM32MP_UART_PROGRAMMER || STM32MP_USB_PROGRAMMER
551 case BOOT_API_CTX_BOOT_INTERFACE_SEL_SERIAL_UART:
Patrick Delaunay9c5ee782021-07-06 14:07:56 +0200552 case BOOT_API_CTX_BOOT_INTERFACE_SEL_SERIAL_USB:
553 /* Invalidate the downloaded buffer used with io_memmap */
554 inv_dcache_range(DWL_BUFFER_BASE, DWL_BUFFER_SIZE);
555 break;
Patrick Delaunaye50571b2021-10-28 13:48:52 +0200556#endif /* STM32MP_UART_PROGRAMMER || STM32MP_USB_PROGRAMMER */
Patrick Delaunay9c5ee782021-07-06 14:07:56 +0200557 default:
558 /* Do nothing in default case */
559 break;
560 }
561
Yann Gautierd2d9b962021-08-16 11:58:01 +0200562 stm32mp1_security_setup();
563}