blob: e09ce63c21b79ff801ed1bbe450861674f7916bd [file] [log] [blame]
Yann Gautier4b0c72a2018-07-16 10:54:09 +02001/*
Yann Gautiera55169b2020-01-10 18:18:59 +01002 * Copyright (c) 2015-2020, 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>
Antonio Nino Diaze0f90632018-12-14 00:18:21 +00008#include <string.h>
9
Yann Gautier4b0c72a2018-07-16 10:54:09 +020010#include <platform_def.h>
Antonio Nino Diaze0f90632018-12-14 00:18:21 +000011
12#include <arch_helpers.h>
13#include <common/bl_common.h>
14#include <common/debug.h>
15#include <common/desc_image_load.h>
16#include <drivers/delay_timer.h>
17#include <drivers/generic_delay_timer.h>
Yann Gautier3edc7c32019-05-20 19:17:08 +020018#include <drivers/st/bsec.h>
Antonio Nino Diaze0f90632018-12-14 00:18:21 +000019#include <drivers/st/stm32_console.h>
Yann Gautier091eab52019-06-04 18:06:34 +020020#include <drivers/st/stm32_iwdg.h>
Yann Gautiera45433b2019-01-16 18:31:00 +010021#include <drivers/st/stm32mp_pmic.h>
Yann Gautiera2e2a302019-02-14 11:13:39 +010022#include <drivers/st/stm32mp_reset.h>
Antonio Nino Diaze0f90632018-12-14 00:18:21 +000023#include <drivers/st/stm32mp1_clk.h>
Antonio Nino Diaze0f90632018-12-14 00:18:21 +000024#include <drivers/st/stm32mp1_pwr.h>
25#include <drivers/st/stm32mp1_ram.h>
Antonio Nino Diaze0f90632018-12-14 00:18:21 +000026#include <lib/mmio.h>
Yann Gautierb3386f72019-04-19 09:41:01 +020027#include <lib/optee_utils.h>
Antonio Nino Diaze0f90632018-12-14 00:18:21 +000028#include <lib/xlat_tables/xlat_tables_v2.h>
29#include <plat/common/platform.h>
30
Yann Gautier8593e442018-11-14 18:46:15 +010031#include <stm32mp1_context.h>
Yann Gautier091eab52019-06-04 18:06:34 +020032#include <stm32mp1_dbgmcu.h>
Yann Gautier4b0c72a2018-07-16 10:54:09 +020033
Etienne Carrieref02647a2019-12-08 08:14:40 +010034#define RESET_TIMEOUT_US_1MS 1000U
35
Andre Przywara678c6fa2020-01-25 00:58:35 +000036static console_t console;
Lionel Debieve7bd96f42019-09-03 12:22:23 +020037static struct stm32mp_auth_ops stm32mp1_auth_ops;
Yann Gautier8593e442018-11-14 18:46:15 +010038
Yann Gautierf9d40d52019-01-17 14:41:46 +010039static void print_reset_reason(void)
40{
Yann Gautier3d78a2e2019-02-14 11:01:20 +010041 uint32_t rstsr = mmio_read_32(stm32mp_rcc_base() + RCC_MP_RSTSCLRR);
Yann Gautierf9d40d52019-01-17 14:41:46 +010042
43 if (rstsr == 0U) {
44 WARN("Reset reason unknown\n");
45 return;
46 }
47
48 INFO("Reset reason (0x%x):\n", rstsr);
49
50 if ((rstsr & RCC_MP_RSTSCLRR_PADRSTF) == 0U) {
51 if ((rstsr & RCC_MP_RSTSCLRR_STDBYRSTF) != 0U) {
52 INFO("System exits from STANDBY\n");
53 return;
54 }
55
56 if ((rstsr & RCC_MP_RSTSCLRR_CSTDBYRSTF) != 0U) {
57 INFO("MPU exits from CSTANDBY\n");
58 return;
59 }
60 }
61
62 if ((rstsr & RCC_MP_RSTSCLRR_PORRSTF) != 0U) {
63 INFO(" Power-on Reset (rst_por)\n");
64 return;
65 }
66
67 if ((rstsr & RCC_MP_RSTSCLRR_BORRSTF) != 0U) {
68 INFO(" Brownout Reset (rst_bor)\n");
69 return;
70 }
71
72 if ((rstsr & RCC_MP_RSTSCLRR_MCSYSRSTF) != 0U) {
73 if ((rstsr & RCC_MP_RSTSCLRR_PADRSTF) != 0U) {
74 INFO(" System reset generated by MCU (MCSYSRST)\n");
75 } else {
76 INFO(" Local reset generated by MCU (MCSYSRST)\n");
77 }
78 return;
79 }
80
81 if ((rstsr & RCC_MP_RSTSCLRR_MPSYSRSTF) != 0U) {
82 INFO(" System reset generated by MPU (MPSYSRST)\n");
83 return;
84 }
85
86 if ((rstsr & RCC_MP_RSTSCLRR_HCSSRSTF) != 0U) {
87 INFO(" Reset due to a clock failure on HSE\n");
88 return;
89 }
90
91 if ((rstsr & RCC_MP_RSTSCLRR_IWDG1RSTF) != 0U) {
92 INFO(" IWDG1 Reset (rst_iwdg1)\n");
93 return;
94 }
95
96 if ((rstsr & RCC_MP_RSTSCLRR_IWDG2RSTF) != 0U) {
97 INFO(" IWDG2 Reset (rst_iwdg2)\n");
98 return;
99 }
100
101 if ((rstsr & RCC_MP_RSTSCLRR_MPUP0RSTF) != 0U) {
102 INFO(" MPU Processor 0 Reset\n");
103 return;
104 }
105
106 if ((rstsr & RCC_MP_RSTSCLRR_MPUP1RSTF) != 0U) {
107 INFO(" MPU Processor 1 Reset\n");
108 return;
109 }
110
111 if ((rstsr & RCC_MP_RSTSCLRR_PADRSTF) != 0U) {
112 INFO(" Pad Reset from NRST\n");
113 return;
114 }
115
116 if ((rstsr & RCC_MP_RSTSCLRR_VCORERSTF) != 0U) {
117 INFO(" Reset due to a failure of VDD_CORE\n");
118 return;
119 }
120
121 ERROR(" Unidentified reset reason\n");
122}
123
124void bl2_el3_early_platform_setup(u_register_t arg0,
125 u_register_t arg1 __unused,
126 u_register_t arg2 __unused,
127 u_register_t arg3 __unused)
Yann Gautier4b0c72a2018-07-16 10:54:09 +0200128{
Yann Gautiera2e2a302019-02-14 11:13:39 +0100129 stm32mp_save_boot_ctx_address(arg0);
Yann Gautier4b0c72a2018-07-16 10:54:09 +0200130}
131
132void bl2_platform_setup(void)
133{
Yann Gautiercaf575b2018-07-24 17:18:19 +0200134 int ret;
Yann Gautiera55169b2020-01-10 18:18:59 +0100135 uint32_t ddr_ns_size;
Yann Gautiercaf575b2018-07-24 17:18:19 +0200136
Yann Gautierf3928f62019-02-14 11:15:03 +0100137 if (dt_pmic_status() > 0) {
Yann Gautierbb836ee2018-07-16 17:55:07 +0200138 initialize_pmic();
139 }
140
Yann Gautiercaf575b2018-07-24 17:18:19 +0200141 ret = stm32mp1_ddr_probe();
142 if (ret < 0) {
143 ERROR("Invalid DDR init: error %d\n", ret);
144 panic();
145 }
146
Yann Gautiera55169b2020-01-10 18:18:59 +0100147 ddr_ns_size = stm32mp_get_ddr_ns_size();
148 assert(ddr_ns_size > 0U);
149
150 /* Map non secure DDR for BL33 load, now with cacheable attribute */
151 ret = mmap_add_dynamic_region(STM32MP_DDR_BASE, STM32MP_DDR_BASE,
152 ddr_ns_size, MT_MEMORY | MT_RW | MT_NS);
153 assert(ret == 0);
154
Yann Gautierb3386f72019-04-19 09:41:01 +0200155#ifdef AARCH32_SP_OPTEE
156 INFO("BL2 runs OP-TEE setup\n");
Yann Gautiera55169b2020-01-10 18:18:59 +0100157
158 /* Map secure DDR for OP-TEE paged area */
159 ret = mmap_add_dynamic_region(STM32MP_DDR_BASE + ddr_ns_size,
160 STM32MP_DDR_BASE + ddr_ns_size,
161 STM32MP_DDR_S_SIZE,
162 MT_MEMORY | MT_RW | MT_SECURE);
163 assert(ret == 0);
164
Yann Gautierb3386f72019-04-19 09:41:01 +0200165 /* Initialize tzc400 after DDR initialization */
166 stm32mp1_security_setup();
167#else
Yann Gautier4b0c72a2018-07-16 10:54:09 +0200168 INFO("BL2 runs SP_MIN setup\n");
Yann Gautierb3386f72019-04-19 09:41:01 +0200169#endif
Yann Gautier4b0c72a2018-07-16 10:54:09 +0200170}
171
172void bl2_el3_plat_arch_setup(void)
173{
Yann Gautier69035a82018-07-05 16:48:16 +0200174 int32_t result;
Yann Gautierf9d40d52019-01-17 14:41:46 +0100175 struct dt_node_info dt_uart_info;
Yann Gautier69035a82018-07-05 16:48:16 +0200176 const char *board_model;
Yann Gautier41934662018-07-20 11:36:05 +0200177 boot_api_context_t *boot_context =
Yann Gautiera2e2a302019-02-14 11:13:39 +0100178 (boot_api_context_t *)stm32mp_get_boot_ctx_address();
Yann Gautier69035a82018-07-05 16:48:16 +0200179 uint32_t clk_rate;
Yann Gautier3d78a2e2019-02-14 11:01:20 +0100180 uintptr_t pwr_base;
181 uintptr_t rcc_base;
Yann Gautier41934662018-07-20 11:36:05 +0200182
Yann Gautierf9d40d52019-01-17 14:41:46 +0100183 mmap_add_region(BL_CODE_BASE, BL_CODE_BASE,
184 BL_CODE_END - BL_CODE_BASE,
185 MT_CODE | MT_SECURE);
186
Yann Gautierb3386f72019-04-19 09:41:01 +0200187#ifdef AARCH32_SP_OPTEE
Yann Gautierb3386f72019-04-19 09:41:01 +0200188 mmap_add_region(STM32MP_OPTEE_BASE, STM32MP_OPTEE_BASE,
189 STM32MP_OPTEE_SIZE,
190 MT_MEMORY | MT_RW | MT_SECURE);
191#else
Yann Gautierf9d40d52019-01-17 14:41:46 +0100192 /* Prevent corruption of preloaded BL32 */
193 mmap_add_region(BL32_BASE, BL32_BASE,
194 BL32_LIMIT - BL32_BASE,
Yann Gautier3d33df62019-12-17 17:11:10 +0100195 MT_RO_DATA | MT_SECURE);
Yann Gautierb3386f72019-04-19 09:41:01 +0200196#endif
Yann Gautierf9d40d52019-01-17 14:41:46 +0100197 /* Prevent corruption of preloaded Device Tree */
198 mmap_add_region(DTB_BASE, DTB_BASE,
199 DTB_LIMIT - DTB_BASE,
Yann Gautier3d33df62019-12-17 17:11:10 +0100200 MT_RO_DATA | MT_SECURE);
Yann Gautierf9d40d52019-01-17 14:41:46 +0100201
202 configure_mmu();
203
204 if (dt_open_and_check() < 0) {
205 panic();
206 }
207
Yann Gautier3d78a2e2019-02-14 11:01:20 +0100208 pwr_base = stm32mp_pwr_base();
209 rcc_base = stm32mp_rcc_base();
210
Yann Gautier4b0c72a2018-07-16 10:54:09 +0200211 /*
212 * Disable the backup domain write protection.
213 * The protection is enable at each reset by hardware
214 * and must be disabled by software.
215 */
Yann Gautier3d78a2e2019-02-14 11:01:20 +0100216 mmio_setbits_32(pwr_base + PWR_CR1, PWR_CR1_DBP);
Yann Gautier4b0c72a2018-07-16 10:54:09 +0200217
Yann Gautier3d78a2e2019-02-14 11:01:20 +0100218 while ((mmio_read_32(pwr_base + PWR_CR1) & PWR_CR1_DBP) == 0U) {
Yann Gautier4b0c72a2018-07-16 10:54:09 +0200219 ;
220 }
221
Yann Gautier3edc7c32019-05-20 19:17:08 +0200222 if (bsec_probe() != 0) {
223 panic();
224 }
225
Yann Gautier4b0c72a2018-07-16 10:54:09 +0200226 /* Reset backup domain on cold boot cases */
Yann Gautier3d78a2e2019-02-14 11:01:20 +0100227 if ((mmio_read_32(rcc_base + RCC_BDCR) & RCC_BDCR_RTCSRC_MASK) == 0U) {
228 mmio_setbits_32(rcc_base + RCC_BDCR, RCC_BDCR_VSWRST);
Yann Gautier4b0c72a2018-07-16 10:54:09 +0200229
Yann Gautier3d78a2e2019-02-14 11:01:20 +0100230 while ((mmio_read_32(rcc_base + RCC_BDCR) & RCC_BDCR_VSWRST) ==
Yann Gautier4b0c72a2018-07-16 10:54:09 +0200231 0U) {
232 ;
233 }
234
Yann Gautier3d78a2e2019-02-14 11:01:20 +0100235 mmio_clrbits_32(rcc_base + RCC_BDCR, RCC_BDCR_VSWRST);
Yann Gautier4b0c72a2018-07-16 10:54:09 +0200236 }
237
Yann Gautiered342322019-02-15 17:33:27 +0100238 /* Disable MCKPROT */
239 mmio_clrbits_32(rcc_base + RCC_TZCR, RCC_TZCR_MCKPROT);
240
Yann Gautier4b0c72a2018-07-16 10:54:09 +0200241 generic_delay_timer_init();
242
Yann Gautier9aea69e2018-07-24 17:13:36 +0200243 if (stm32mp1_clk_probe() < 0) {
244 panic();
245 }
246
247 if (stm32mp1_clk_init() < 0) {
248 panic();
249 }
250
Yann Gautier3edc7c32019-05-20 19:17:08 +0200251 stm32mp1_syscfg_init();
252
Yann Gautierf9d40d52019-01-17 14:41:46 +0100253 result = dt_get_stdout_uart_info(&dt_uart_info);
Yann Gautier69035a82018-07-05 16:48:16 +0200254
255 if ((result <= 0) ||
Yann Gautierf9d40d52019-01-17 14:41:46 +0100256 (dt_uart_info.status == 0U) ||
257 (dt_uart_info.clock < 0) ||
258 (dt_uart_info.reset < 0)) {
Yann Gautier69035a82018-07-05 16:48:16 +0200259 goto skip_console_init;
260 }
261
262 if (dt_set_stdout_pinctrl() != 0) {
263 goto skip_console_init;
264 }
265
Yann Gautiere4a3c352019-02-14 10:53:33 +0100266 stm32mp_clk_enable((unsigned long)dt_uart_info.clock);
Yann Gautier69035a82018-07-05 16:48:16 +0200267
Etienne Carrieref02647a2019-12-08 08:14:40 +0100268 if (stm32mp_reset_assert((uint32_t)dt_uart_info.reset,
269 RESET_TIMEOUT_US_1MS) != 0) {
270 panic();
271 }
272
Yann Gautier69035a82018-07-05 16:48:16 +0200273 udelay(2);
Etienne Carrieref02647a2019-12-08 08:14:40 +0100274
275 if (stm32mp_reset_deassert((uint32_t)dt_uart_info.reset,
276 RESET_TIMEOUT_US_1MS) != 0) {
277 panic();
278 }
279
Yann Gautier69035a82018-07-05 16:48:16 +0200280 mdelay(1);
281
Yann Gautiera2e2a302019-02-14 11:13:39 +0100282 clk_rate = stm32mp_clk_get_rate((unsigned long)dt_uart_info.clock);
Yann Gautier69035a82018-07-05 16:48:16 +0200283
Yann Gautierf9d40d52019-01-17 14:41:46 +0100284 if (console_stm32_register(dt_uart_info.base, clk_rate,
Yann Gautiera2e2a302019-02-14 11:13:39 +0100285 STM32MP_UART_BAUDRATE, &console) == 0) {
Yann Gautier69035a82018-07-05 16:48:16 +0200286 panic();
287 }
288
Andre Przywara678c6fa2020-01-25 00:58:35 +0000289 console_set_scope(&console, CONSOLE_FLAG_BOOT |
Yann Gautiera30e5f72019-09-04 11:55:10 +0200290 CONSOLE_FLAG_CRASH | CONSOLE_FLAG_TRANSLATE_CRLF);
291
Yann Gautierc7374052019-06-04 18:02:37 +0200292 stm32mp_print_cpuinfo();
293
Yann Gautier69035a82018-07-05 16:48:16 +0200294 board_model = dt_get_board_model();
295 if (board_model != NULL) {
Yann Gautierf9d40d52019-01-17 14:41:46 +0100296 NOTICE("Model: %s\n", board_model);
Yann Gautier69035a82018-07-05 16:48:16 +0200297 }
298
Yann Gautier35dc0772019-05-13 18:34:48 +0200299 stm32mp_print_boardinfo();
300
Lionel Debieve7bd96f42019-09-03 12:22:23 +0200301 if (boot_context->auth_status != BOOT_API_CTX_AUTH_NO) {
302 NOTICE("Bootrom authentication %s\n",
303 (boot_context->auth_status == BOOT_API_CTX_AUTH_FAILED) ?
304 "failed" : "succeeded");
305 }
306
Yann Gautier69035a82018-07-05 16:48:16 +0200307skip_console_init:
Yann Gautier091eab52019-06-04 18:06:34 +0200308 if (stm32_iwdg_init() < 0) {
309 panic();
310 }
311
312 stm32_iwdg_refresh();
313
314 result = stm32mp1_dbgmcu_freeze_iwdg2();
315 if (result != 0) {
316 INFO("IWDG2 freeze error : %i\n", result);
317 }
Yann Gautier69035a82018-07-05 16:48:16 +0200318
Yann Gautier41934662018-07-20 11:36:05 +0200319 if (stm32_save_boot_interface(boot_context->boot_interface_selected,
320 boot_context->boot_interface_instance) !=
321 0) {
322 ERROR("Cannot save boot interface\n");
323 }
324
Lionel Debieve7bd96f42019-09-03 12:22:23 +0200325 stm32mp1_auth_ops.check_key = boot_context->bootrom_ecdsa_check_key;
326 stm32mp1_auth_ops.verify_signature =
327 boot_context->bootrom_ecdsa_verify_signature;
328
329 stm32mp_init_auth(&stm32mp1_auth_ops);
330
Yann Gautiercaf575b2018-07-24 17:18:19 +0200331 stm32mp1_arch_security_setup();
332
Yann Gautierf9d40d52019-01-17 14:41:46 +0100333 print_reset_reason();
334
Yann Gautiera2e2a302019-02-14 11:13:39 +0100335 stm32mp_io_setup();
Yann Gautier4b0c72a2018-07-16 10:54:09 +0200336}
Yann Gautierb3386f72019-04-19 09:41:01 +0200337
338#if defined(AARCH32_SP_OPTEE)
339/*******************************************************************************
340 * This function can be used by the platforms to update/use image
341 * information for given `image_id`.
342 ******************************************************************************/
343int bl2_plat_handle_post_image_load(unsigned int image_id)
344{
345 int err = 0;
346 bl_mem_params_node_t *bl_mem_params = get_bl_mem_params_node(image_id);
347 bl_mem_params_node_t *bl32_mem_params;
348 bl_mem_params_node_t *pager_mem_params;
349 bl_mem_params_node_t *paged_mem_params;
350
351 assert(bl_mem_params != NULL);
352
353 switch (image_id) {
354 case BL32_IMAGE_ID:
355 bl_mem_params->ep_info.pc =
356 bl_mem_params->image_info.image_base;
357
358 pager_mem_params = get_bl_mem_params_node(BL32_EXTRA1_IMAGE_ID);
359 assert(pager_mem_params != NULL);
360 pager_mem_params->image_info.image_base = STM32MP_OPTEE_BASE;
361 pager_mem_params->image_info.image_max_size =
362 STM32MP_OPTEE_SIZE;
363
364 paged_mem_params = get_bl_mem_params_node(BL32_EXTRA2_IMAGE_ID);
365 assert(paged_mem_params != NULL);
366 paged_mem_params->image_info.image_base = STM32MP_DDR_BASE +
Yann Gautiercd40f322020-02-26 13:36:07 +0100367 stm32mp_get_ddr_ns_size();
Yann Gautierb3386f72019-04-19 09:41:01 +0200368 paged_mem_params->image_info.image_max_size =
369 STM32MP_DDR_S_SIZE;
370
371 err = parse_optee_header(&bl_mem_params->ep_info,
372 &pager_mem_params->image_info,
373 &paged_mem_params->image_info);
374 if (err) {
375 ERROR("OPTEE header parse error.\n");
376 panic();
377 }
378
379 /* Set optee boot info from parsed header data */
380 bl_mem_params->ep_info.pc =
381 pager_mem_params->image_info.image_base;
382 bl_mem_params->ep_info.args.arg0 =
383 paged_mem_params->image_info.image_base;
384 bl_mem_params->ep_info.args.arg1 = 0; /* Unused */
385 bl_mem_params->ep_info.args.arg2 = 0; /* No DT supported */
386 break;
387
388 case BL33_IMAGE_ID:
389 bl32_mem_params = get_bl_mem_params_node(BL32_IMAGE_ID);
390 assert(bl32_mem_params != NULL);
391 bl32_mem_params->ep_info.lr_svc = bl_mem_params->ep_info.pc;
392 break;
393
394 default:
395 /* Do nothing in default case */
396 break;
397 }
398
399 return err;
400}
401#endif