blob: 27d298e8d5dfdd9054542cab18cfdfa93bfcc70a [file] [log] [blame]
Yann Gautier4b0c72a2018-07-16 10:54:09 +02001/*
Yann Gautiera45433b2019-01-16 18:31:00 +01002 * Copyright (c) 2015-2019, 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 Gautiera45433b2019-01-16 18:31:00 +010020#include <drivers/st/stm32mp_pmic.h>
Yann Gautiera2e2a302019-02-14 11:13:39 +010021#include <drivers/st/stm32mp_reset.h>
Antonio Nino Diaze0f90632018-12-14 00:18:21 +000022#include <drivers/st/stm32mp1_clk.h>
Antonio Nino Diaze0f90632018-12-14 00:18:21 +000023#include <drivers/st/stm32mp1_pwr.h>
24#include <drivers/st/stm32mp1_ram.h>
Antonio Nino Diaze0f90632018-12-14 00:18:21 +000025#include <lib/mmio.h>
Yann Gautierb3386f72019-04-19 09:41:01 +020026#include <lib/optee_utils.h>
Antonio Nino Diaze0f90632018-12-14 00:18:21 +000027#include <lib/xlat_tables/xlat_tables_v2.h>
28#include <plat/common/platform.h>
29
Yann Gautier8593e442018-11-14 18:46:15 +010030#include <stm32mp1_context.h>
Yann Gautier4b0c72a2018-07-16 10:54:09 +020031
Yann Gautier8593e442018-11-14 18:46:15 +010032static struct console_stm32 console;
33
Yann Gautierf9d40d52019-01-17 14:41:46 +010034static void print_reset_reason(void)
35{
Yann Gautier3d78a2e2019-02-14 11:01:20 +010036 uint32_t rstsr = mmio_read_32(stm32mp_rcc_base() + RCC_MP_RSTSCLRR);
Yann Gautierf9d40d52019-01-17 14:41:46 +010037
38 if (rstsr == 0U) {
39 WARN("Reset reason unknown\n");
40 return;
41 }
42
43 INFO("Reset reason (0x%x):\n", rstsr);
44
45 if ((rstsr & RCC_MP_RSTSCLRR_PADRSTF) == 0U) {
46 if ((rstsr & RCC_MP_RSTSCLRR_STDBYRSTF) != 0U) {
47 INFO("System exits from STANDBY\n");
48 return;
49 }
50
51 if ((rstsr & RCC_MP_RSTSCLRR_CSTDBYRSTF) != 0U) {
52 INFO("MPU exits from CSTANDBY\n");
53 return;
54 }
55 }
56
57 if ((rstsr & RCC_MP_RSTSCLRR_PORRSTF) != 0U) {
58 INFO(" Power-on Reset (rst_por)\n");
59 return;
60 }
61
62 if ((rstsr & RCC_MP_RSTSCLRR_BORRSTF) != 0U) {
63 INFO(" Brownout Reset (rst_bor)\n");
64 return;
65 }
66
67 if ((rstsr & RCC_MP_RSTSCLRR_MCSYSRSTF) != 0U) {
68 if ((rstsr & RCC_MP_RSTSCLRR_PADRSTF) != 0U) {
69 INFO(" System reset generated by MCU (MCSYSRST)\n");
70 } else {
71 INFO(" Local reset generated by MCU (MCSYSRST)\n");
72 }
73 return;
74 }
75
76 if ((rstsr & RCC_MP_RSTSCLRR_MPSYSRSTF) != 0U) {
77 INFO(" System reset generated by MPU (MPSYSRST)\n");
78 return;
79 }
80
81 if ((rstsr & RCC_MP_RSTSCLRR_HCSSRSTF) != 0U) {
82 INFO(" Reset due to a clock failure on HSE\n");
83 return;
84 }
85
86 if ((rstsr & RCC_MP_RSTSCLRR_IWDG1RSTF) != 0U) {
87 INFO(" IWDG1 Reset (rst_iwdg1)\n");
88 return;
89 }
90
91 if ((rstsr & RCC_MP_RSTSCLRR_IWDG2RSTF) != 0U) {
92 INFO(" IWDG2 Reset (rst_iwdg2)\n");
93 return;
94 }
95
96 if ((rstsr & RCC_MP_RSTSCLRR_MPUP0RSTF) != 0U) {
97 INFO(" MPU Processor 0 Reset\n");
98 return;
99 }
100
101 if ((rstsr & RCC_MP_RSTSCLRR_MPUP1RSTF) != 0U) {
102 INFO(" MPU Processor 1 Reset\n");
103 return;
104 }
105
106 if ((rstsr & RCC_MP_RSTSCLRR_PADRSTF) != 0U) {
107 INFO(" Pad Reset from NRST\n");
108 return;
109 }
110
111 if ((rstsr & RCC_MP_RSTSCLRR_VCORERSTF) != 0U) {
112 INFO(" Reset due to a failure of VDD_CORE\n");
113 return;
114 }
115
116 ERROR(" Unidentified reset reason\n");
117}
118
119void bl2_el3_early_platform_setup(u_register_t arg0,
120 u_register_t arg1 __unused,
121 u_register_t arg2 __unused,
122 u_register_t arg3 __unused)
Yann Gautier4b0c72a2018-07-16 10:54:09 +0200123{
Yann Gautiera2e2a302019-02-14 11:13:39 +0100124 stm32mp_save_boot_ctx_address(arg0);
Yann Gautier4b0c72a2018-07-16 10:54:09 +0200125}
126
127void bl2_platform_setup(void)
128{
Yann Gautiercaf575b2018-07-24 17:18:19 +0200129 int ret;
130
Yann Gautierf3928f62019-02-14 11:15:03 +0100131 if (dt_pmic_status() > 0) {
Yann Gautierbb836ee2018-07-16 17:55:07 +0200132 initialize_pmic();
133 }
134
Yann Gautiercaf575b2018-07-24 17:18:19 +0200135 ret = stm32mp1_ddr_probe();
136 if (ret < 0) {
137 ERROR("Invalid DDR init: error %d\n", ret);
138 panic();
139 }
140
Yann Gautierb3386f72019-04-19 09:41:01 +0200141#ifdef AARCH32_SP_OPTEE
142 INFO("BL2 runs OP-TEE setup\n");
143 /* Initialize tzc400 after DDR initialization */
144 stm32mp1_security_setup();
145#else
Yann Gautier4b0c72a2018-07-16 10:54:09 +0200146 INFO("BL2 runs SP_MIN setup\n");
Yann Gautierb3386f72019-04-19 09:41:01 +0200147#endif
Yann Gautier4b0c72a2018-07-16 10:54:09 +0200148}
149
150void bl2_el3_plat_arch_setup(void)
151{
Yann Gautier69035a82018-07-05 16:48:16 +0200152 int32_t result;
Yann Gautierf9d40d52019-01-17 14:41:46 +0100153 struct dt_node_info dt_uart_info;
Yann Gautier69035a82018-07-05 16:48:16 +0200154 const char *board_model;
Yann Gautier41934662018-07-20 11:36:05 +0200155 boot_api_context_t *boot_context =
Yann Gautiera2e2a302019-02-14 11:13:39 +0100156 (boot_api_context_t *)stm32mp_get_boot_ctx_address();
Yann Gautier69035a82018-07-05 16:48:16 +0200157 uint32_t clk_rate;
Yann Gautier3d78a2e2019-02-14 11:01:20 +0100158 uintptr_t pwr_base;
159 uintptr_t rcc_base;
Yann Gautier41934662018-07-20 11:36:05 +0200160
Yann Gautierf9d40d52019-01-17 14:41:46 +0100161 mmap_add_region(BL_CODE_BASE, BL_CODE_BASE,
162 BL_CODE_END - BL_CODE_BASE,
163 MT_CODE | MT_SECURE);
164
Yann Gautierb3386f72019-04-19 09:41:01 +0200165#ifdef AARCH32_SP_OPTEE
166 /* OP-TEE image needs post load processing: keep RAM read/write */
167 mmap_add_region(STM32MP_DDR_BASE + dt_get_ddr_size() -
168 STM32MP_DDR_S_SIZE - STM32MP_DDR_SHMEM_SIZE,
169 STM32MP_DDR_BASE + dt_get_ddr_size() -
170 STM32MP_DDR_S_SIZE - STM32MP_DDR_SHMEM_SIZE,
171 STM32MP_DDR_S_SIZE,
172 MT_MEMORY | MT_RW | MT_SECURE);
173
174 mmap_add_region(STM32MP_OPTEE_BASE, STM32MP_OPTEE_BASE,
175 STM32MP_OPTEE_SIZE,
176 MT_MEMORY | MT_RW | MT_SECURE);
177#else
Yann Gautierf9d40d52019-01-17 14:41:46 +0100178 /* Prevent corruption of preloaded BL32 */
179 mmap_add_region(BL32_BASE, BL32_BASE,
180 BL32_LIMIT - BL32_BASE,
181 MT_MEMORY | MT_RO | MT_SECURE);
182
Yann Gautierb3386f72019-04-19 09:41:01 +0200183#endif
Yann Gautierf9d40d52019-01-17 14:41:46 +0100184 /* Map non secure DDR for BL33 load and DDR training area restore */
Yann Gautiera2e2a302019-02-14 11:13:39 +0100185 mmap_add_region(STM32MP_DDR_BASE,
186 STM32MP_DDR_BASE,
187 STM32MP_DDR_MAX_SIZE,
Yann Gautierf9d40d52019-01-17 14:41:46 +0100188 MT_MEMORY | MT_RW | MT_NS);
189
190 /* Prevent corruption of preloaded Device Tree */
191 mmap_add_region(DTB_BASE, DTB_BASE,
192 DTB_LIMIT - DTB_BASE,
193 MT_MEMORY | MT_RO | MT_SECURE);
194
195 configure_mmu();
196
197 if (dt_open_and_check() < 0) {
198 panic();
199 }
200
Yann Gautier3d78a2e2019-02-14 11:01:20 +0100201 pwr_base = stm32mp_pwr_base();
202 rcc_base = stm32mp_rcc_base();
203
Yann Gautier4b0c72a2018-07-16 10:54:09 +0200204 /*
205 * Disable the backup domain write protection.
206 * The protection is enable at each reset by hardware
207 * and must be disabled by software.
208 */
Yann Gautier3d78a2e2019-02-14 11:01:20 +0100209 mmio_setbits_32(pwr_base + PWR_CR1, PWR_CR1_DBP);
Yann Gautier4b0c72a2018-07-16 10:54:09 +0200210
Yann Gautier3d78a2e2019-02-14 11:01:20 +0100211 while ((mmio_read_32(pwr_base + PWR_CR1) & PWR_CR1_DBP) == 0U) {
Yann Gautier4b0c72a2018-07-16 10:54:09 +0200212 ;
213 }
214
Yann Gautier3edc7c32019-05-20 19:17:08 +0200215 if (bsec_probe() != 0) {
216 panic();
217 }
218
Yann Gautier4b0c72a2018-07-16 10:54:09 +0200219 /* Reset backup domain on cold boot cases */
Yann Gautier3d78a2e2019-02-14 11:01:20 +0100220 if ((mmio_read_32(rcc_base + RCC_BDCR) & RCC_BDCR_RTCSRC_MASK) == 0U) {
221 mmio_setbits_32(rcc_base + RCC_BDCR, RCC_BDCR_VSWRST);
Yann Gautier4b0c72a2018-07-16 10:54:09 +0200222
Yann Gautier3d78a2e2019-02-14 11:01:20 +0100223 while ((mmio_read_32(rcc_base + RCC_BDCR) & RCC_BDCR_VSWRST) ==
Yann Gautier4b0c72a2018-07-16 10:54:09 +0200224 0U) {
225 ;
226 }
227
Yann Gautier3d78a2e2019-02-14 11:01:20 +0100228 mmio_clrbits_32(rcc_base + RCC_BDCR, RCC_BDCR_VSWRST);
Yann Gautier4b0c72a2018-07-16 10:54:09 +0200229 }
230
Yann Gautiered342322019-02-15 17:33:27 +0100231 /* Disable MCKPROT */
232 mmio_clrbits_32(rcc_base + RCC_TZCR, RCC_TZCR_MCKPROT);
233
Yann Gautier4b0c72a2018-07-16 10:54:09 +0200234 generic_delay_timer_init();
235
Yann Gautier9aea69e2018-07-24 17:13:36 +0200236 if (stm32mp1_clk_probe() < 0) {
237 panic();
238 }
239
240 if (stm32mp1_clk_init() < 0) {
241 panic();
242 }
243
Yann Gautier3edc7c32019-05-20 19:17:08 +0200244 stm32mp1_syscfg_init();
245
Yann Gautierf9d40d52019-01-17 14:41:46 +0100246 result = dt_get_stdout_uart_info(&dt_uart_info);
Yann Gautier69035a82018-07-05 16:48:16 +0200247
248 if ((result <= 0) ||
Yann Gautierf9d40d52019-01-17 14:41:46 +0100249 (dt_uart_info.status == 0U) ||
250 (dt_uart_info.clock < 0) ||
251 (dt_uart_info.reset < 0)) {
Yann Gautier69035a82018-07-05 16:48:16 +0200252 goto skip_console_init;
253 }
254
255 if (dt_set_stdout_pinctrl() != 0) {
256 goto skip_console_init;
257 }
258
Yann Gautiere4a3c352019-02-14 10:53:33 +0100259 stm32mp_clk_enable((unsigned long)dt_uart_info.clock);
Yann Gautier69035a82018-07-05 16:48:16 +0200260
Yann Gautiera2e2a302019-02-14 11:13:39 +0100261 stm32mp_reset_assert((uint32_t)dt_uart_info.reset);
Yann Gautier69035a82018-07-05 16:48:16 +0200262 udelay(2);
Yann Gautiera2e2a302019-02-14 11:13:39 +0100263 stm32mp_reset_deassert((uint32_t)dt_uart_info.reset);
Yann Gautier69035a82018-07-05 16:48:16 +0200264 mdelay(1);
265
Yann Gautiera2e2a302019-02-14 11:13:39 +0100266 clk_rate = stm32mp_clk_get_rate((unsigned long)dt_uart_info.clock);
Yann Gautier69035a82018-07-05 16:48:16 +0200267
Yann Gautierf9d40d52019-01-17 14:41:46 +0100268 if (console_stm32_register(dt_uart_info.base, clk_rate,
Yann Gautiera2e2a302019-02-14 11:13:39 +0100269 STM32MP_UART_BAUDRATE, &console) == 0) {
Yann Gautier69035a82018-07-05 16:48:16 +0200270 panic();
271 }
272
273 board_model = dt_get_board_model();
274 if (board_model != NULL) {
Yann Gautierf9d40d52019-01-17 14:41:46 +0100275 NOTICE("Model: %s\n", board_model);
Yann Gautier69035a82018-07-05 16:48:16 +0200276 }
277
278skip_console_init:
279
Yann Gautier41934662018-07-20 11:36:05 +0200280 if (stm32_save_boot_interface(boot_context->boot_interface_selected,
281 boot_context->boot_interface_instance) !=
282 0) {
283 ERROR("Cannot save boot interface\n");
284 }
285
Yann Gautiercaf575b2018-07-24 17:18:19 +0200286 stm32mp1_arch_security_setup();
287
Yann Gautierf9d40d52019-01-17 14:41:46 +0100288 print_reset_reason();
289
Yann Gautiera2e2a302019-02-14 11:13:39 +0100290 stm32mp_io_setup();
Yann Gautier4b0c72a2018-07-16 10:54:09 +0200291}
Yann Gautierb3386f72019-04-19 09:41:01 +0200292
293#if defined(AARCH32_SP_OPTEE)
294/*******************************************************************************
295 * This function can be used by the platforms to update/use image
296 * information for given `image_id`.
297 ******************************************************************************/
298int bl2_plat_handle_post_image_load(unsigned int image_id)
299{
300 int err = 0;
301 bl_mem_params_node_t *bl_mem_params = get_bl_mem_params_node(image_id);
302 bl_mem_params_node_t *bl32_mem_params;
303 bl_mem_params_node_t *pager_mem_params;
304 bl_mem_params_node_t *paged_mem_params;
305
306 assert(bl_mem_params != NULL);
307
308 switch (image_id) {
309 case BL32_IMAGE_ID:
310 bl_mem_params->ep_info.pc =
311 bl_mem_params->image_info.image_base;
312
313 pager_mem_params = get_bl_mem_params_node(BL32_EXTRA1_IMAGE_ID);
314 assert(pager_mem_params != NULL);
315 pager_mem_params->image_info.image_base = STM32MP_OPTEE_BASE;
316 pager_mem_params->image_info.image_max_size =
317 STM32MP_OPTEE_SIZE;
318
319 paged_mem_params = get_bl_mem_params_node(BL32_EXTRA2_IMAGE_ID);
320 assert(paged_mem_params != NULL);
321 paged_mem_params->image_info.image_base = STM32MP_DDR_BASE +
322 (dt_get_ddr_size() - STM32MP_DDR_S_SIZE -
323 STM32MP_DDR_SHMEM_SIZE);
324 paged_mem_params->image_info.image_max_size =
325 STM32MP_DDR_S_SIZE;
326
327 err = parse_optee_header(&bl_mem_params->ep_info,
328 &pager_mem_params->image_info,
329 &paged_mem_params->image_info);
330 if (err) {
331 ERROR("OPTEE header parse error.\n");
332 panic();
333 }
334
335 /* Set optee boot info from parsed header data */
336 bl_mem_params->ep_info.pc =
337 pager_mem_params->image_info.image_base;
338 bl_mem_params->ep_info.args.arg0 =
339 paged_mem_params->image_info.image_base;
340 bl_mem_params->ep_info.args.arg1 = 0; /* Unused */
341 bl_mem_params->ep_info.args.arg2 = 0; /* No DT supported */
342 break;
343
344 case BL33_IMAGE_ID:
345 bl32_mem_params = get_bl_mem_params_node(BL32_IMAGE_ID);
346 assert(bl32_mem_params != NULL);
347 bl32_mem_params->ep_info.lr_svc = bl_mem_params->ep_info.pc;
348 break;
349
350 default:
351 /* Do nothing in default case */
352 break;
353 }
354
355 return err;
356}
357#endif