blob: edada72c910b47c852dd6bd72693b51536f47c7e [file] [log] [blame]
Yann Gautiera3f46382023-06-14 10:40:59 +02001/*
Yann Gautier24e94a62024-01-04 10:58:18 +01002 * Copyright (c) 2023-2024, STMicroelectronics - All Rights Reserved
Yann Gautiera3f46382023-06-14 10:40:59 +02003 *
4 * SPDX-License-Identifier: BSD-3-Clause
5 */
6
Yann Gautier88ca3352024-05-22 16:46:37 +02007#include <assert.h>
Yann Gautiera3f46382023-06-14 10:40:59 +02008#include <cdefs.h>
9#include <stdint.h>
10
Yann Gautiera585d762024-01-03 14:28:23 +010011#include <common/debug.h>
Yann Gautier88ca3352024-05-22 16:46:37 +020012#include <common/desc_image_load.h>
Yann Gautier8053f2b2024-05-21 11:46:59 +020013#include <drivers/clk.h>
Yann Gautier88ca3352024-05-22 16:46:37 +020014#include <drivers/mmc.h>
Yann Gautier3ad65ce2024-05-21 12:03:56 +020015#include <drivers/st/regulator_fixed.h>
Yann Gautier40ff1382024-05-21 20:54:04 +020016#include <drivers/st/stm32mp2_ddr_helpers.h>
Yann Gautier8053f2b2024-05-21 11:46:59 +020017#include <lib/fconf/fconf.h>
18#include <lib/fconf/fconf_dyn_cfg_getter.h>
19#include <lib/mmio.h>
20#include <lib/xlat_tables/xlat_tables_v2.h>
Yann Gautier24e94a62024-01-04 10:58:18 +010021#include <plat/common/platform.h>
22
Yann Gautiera585d762024-01-03 14:28:23 +010023#include <platform_def.h>
Yann Gautiereb91af52023-06-14 18:05:47 +020024#include <stm32mp_common.h>
Yann Gautier8053f2b2024-05-21 11:46:59 +020025#include <stm32mp_dt.h>
26
27#define BOOT_CTX_ADDR 0x0e000020UL
28
29static void print_reset_reason(void)
30{
31 uint32_t rstsr = mmio_read_32(stm32mp_rcc_base() + RCC_C1BOOTRSTSCLRR);
32
33 if (rstsr == 0U) {
34 WARN("Reset reason unknown\n");
35 return;
36 }
37
38 INFO("Reset reason (0x%x):\n", rstsr);
39
40 if ((rstsr & RCC_C1BOOTRSTSCLRR_PADRSTF) == 0U) {
41 if ((rstsr & RCC_C1BOOTRSTSCLRR_STBYC1RSTF) != 0U) {
42 INFO("System exits from Standby for CA35\n");
43 return;
44 }
45
46 if ((rstsr & RCC_C1BOOTRSTSCLRR_D1STBYRSTF) != 0U) {
47 INFO("D1 domain exits from DStandby\n");
48 return;
49 }
50 }
51
52 if ((rstsr & RCC_C1BOOTRSTSCLRR_PORRSTF) != 0U) {
53 INFO(" Power-on Reset (rst_por)\n");
54 return;
55 }
56
57 if ((rstsr & RCC_C1BOOTRSTSCLRR_BORRSTF) != 0U) {
58 INFO(" Brownout Reset (rst_bor)\n");
59 return;
60 }
61
62 if ((rstsr & RCC_C1BOOTRSTSSETR_SYSC2RSTF) != 0U) {
63 INFO(" System reset (SYSRST) by M33\n");
64 return;
65 }
66
67 if ((rstsr & RCC_C1BOOTRSTSSETR_SYSC1RSTF) != 0U) {
68 INFO(" System reset (SYSRST) by A35\n");
69 return;
70 }
71
72 if ((rstsr & RCC_C1BOOTRSTSCLRR_HCSSRSTF) != 0U) {
73 INFO(" Clock failure on HSE\n");
74 return;
75 }
76
77 if ((rstsr & RCC_C1BOOTRSTSCLRR_IWDG1SYSRSTF) != 0U) {
78 INFO(" IWDG1 system reset (rst_iwdg1)\n");
79 return;
80 }
81
82 if ((rstsr & RCC_C1BOOTRSTSCLRR_IWDG2SYSRSTF) != 0U) {
83 INFO(" IWDG2 system reset (rst_iwdg2)\n");
84 return;
85 }
86
87 if ((rstsr & RCC_C1BOOTRSTSCLRR_IWDG3SYSRSTF) != 0U) {
88 INFO(" IWDG3 system reset (rst_iwdg3)\n");
89 return;
90 }
91
92 if ((rstsr & RCC_C1BOOTRSTSCLRR_IWDG4SYSRSTF) != 0U) {
93 INFO(" IWDG4 system reset (rst_iwdg4)\n");
94 return;
95 }
96
97 if ((rstsr & RCC_C1BOOTRSTSCLRR_IWDG5SYSRSTF) != 0U) {
98 INFO(" IWDG5 system reset (rst_iwdg5)\n");
99 return;
100 }
101
102 if ((rstsr & RCC_C1BOOTRSTSCLRR_C1P1RSTF) != 0U) {
103 INFO(" A35 processor core 1 reset\n");
104 return;
105 }
106
107 if ((rstsr & RCC_C1BOOTRSTSCLRR_PADRSTF) != 0U) {
108 INFO(" Pad Reset from NRST\n");
109 return;
110 }
111
112 if ((rstsr & RCC_C1BOOTRSTSCLRR_VCORERSTF) != 0U) {
113 INFO(" Reset due to a failure of VDD_CORE\n");
114 return;
115 }
116
117 if ((rstsr & RCC_C1BOOTRSTSCLRR_C1RSTF) != 0U) {
118 INFO(" A35 processor reset\n");
119 return;
120 }
121
122 ERROR(" Unidentified reset reason\n");
123}
Yann Gautiereb91af52023-06-14 18:05:47 +0200124
Yann Gautiera3f46382023-06-14 10:40:59 +0200125void bl2_el3_early_platform_setup(u_register_t arg0 __unused,
126 u_register_t arg1 __unused,
127 u_register_t arg2 __unused,
128 u_register_t arg3 __unused)
129{
Yann Gautier8053f2b2024-05-21 11:46:59 +0200130 stm32mp_save_boot_ctx_address(BOOT_CTX_ADDR);
Yann Gautiera3f46382023-06-14 10:40:59 +0200131}
132
133void bl2_platform_setup(void)
134{
135}
136
Yann Gautier8053f2b2024-05-21 11:46:59 +0200137static void reset_backup_domain(void)
138{
139 uintptr_t pwr_base = stm32mp_pwr_base();
140 uintptr_t rcc_base = stm32mp_rcc_base();
141
142 /*
143 * Disable the backup domain write protection.
144 * The protection is enable at each reset by hardware
145 * and must be disabled by software.
146 */
147 mmio_setbits_32(pwr_base + PWR_BDCR1, PWR_BDCR1_DBD3P);
148
149 while ((mmio_read_32(pwr_base + PWR_BDCR1) & PWR_BDCR1_DBD3P) == 0U) {
150 ;
151 }
152
153 /* Reset backup domain on cold boot cases */
154 if ((mmio_read_32(rcc_base + RCC_BDCR) & RCC_BDCR_RTCCKEN) == 0U) {
155 mmio_setbits_32(rcc_base + RCC_BDCR, RCC_BDCR_VSWRST);
156
157 while ((mmio_read_32(rcc_base + RCC_BDCR) & RCC_BDCR_VSWRST) == 0U) {
158 ;
159 }
160
161 mmio_clrbits_32(rcc_base + RCC_BDCR, RCC_BDCR_VSWRST);
162 }
163}
164
Yann Gautiera3f46382023-06-14 10:40:59 +0200165void bl2_el3_plat_arch_setup(void)
166{
Yann Gautier8053f2b2024-05-21 11:46:59 +0200167 const char *board_model;
168 boot_api_context_t *boot_context =
169 (boot_api_context_t *)stm32mp_get_boot_ctx_address();
170
Yann Gautiera585d762024-01-03 14:28:23 +0100171 if (stm32_otp_probe() != 0U) {
Yann Gautierb619f492024-01-18 11:39:19 +0100172 EARLY_ERROR("OTP probe failed\n");
Yann Gautiera585d762024-01-03 14:28:23 +0100173 panic();
174 }
Yann Gautier8053f2b2024-05-21 11:46:59 +0200175
176 mmap_add_region(BL_CODE_BASE, BL_CODE_BASE,
177 BL_CODE_END - BL_CODE_BASE,
178 MT_CODE | MT_SECURE);
179
180 configure_mmu();
181
Yann Gautier8053f2b2024-05-21 11:46:59 +0200182 if (dt_open_and_check(STM32MP_DTB_BASE) < 0) {
183 panic();
184 }
185
186 reset_backup_domain();
187
Yann Gautier40ff1382024-05-21 20:54:04 +0200188 /*
189 * Initialize DDR sub-system clock. This needs to be done before enabling DDR PLL (PLL2),
190 * and so before stm32mp2_clk_init().
191 */
192 ddr_sub_system_clk_init();
193
Yann Gautier8053f2b2024-05-21 11:46:59 +0200194 if (stm32mp2_clk_init() < 0) {
195 panic();
196 }
197
198 stm32_save_boot_info(boot_context);
199
200 if (stm32mp_uart_console_setup() != 0) {
201 goto skip_console_init;
202 }
203
Yann Gautier400dcac2024-06-21 14:49:47 +0200204 stm32mp_print_cpuinfo();
205
Yann Gautier8053f2b2024-05-21 11:46:59 +0200206 board_model = dt_get_board_model();
207 if (board_model != NULL) {
208 NOTICE("Model: %s\n", board_model);
209 }
210
Yann Gautiera65743b2022-04-15 16:15:25 +0200211 stm32mp_print_boardinfo();
212
Yann Gautier8053f2b2024-05-21 11:46:59 +0200213 print_reset_reason();
214
215skip_console_init:
Yann Gautier3ad65ce2024-05-21 12:03:56 +0200216 if (fixed_regulator_register() != 0) {
217 panic();
218 }
219
Yann Gautier8053f2b2024-05-21 11:46:59 +0200220 fconf_populate("TB_FW", STM32MP_DTB_BASE);
221
222 stm32mp_io_setup();
Yann Gautiera3f46382023-06-14 10:40:59 +0200223}
Yann Gautier88ca3352024-05-22 16:46:37 +0200224
225/*******************************************************************************
226 * This function can be used by the platforms to update/use image
227 * information for given `image_id`.
228 ******************************************************************************/
229int bl2_plat_handle_post_image_load(unsigned int image_id)
230{
231 int err = 0;
232 bl_mem_params_node_t *bl_mem_params __maybe_unused = get_bl_mem_params_node(image_id);
233
234 assert(bl_mem_params != NULL);
235
236#if STM32MP_SDMMC || STM32MP_EMMC
237 /*
238 * Invalidate remaining data read from MMC but not flushed by load_image_flush().
239 * We take the worst case which is 2 MMC blocks.
240 */
241 if ((image_id != FW_CONFIG_ID) &&
242 ((bl_mem_params->image_info.h.attr & IMAGE_ATTRIB_SKIP_LOADING) == 0U)) {
243 inv_dcache_range(bl_mem_params->image_info.image_base +
244 bl_mem_params->image_info.image_size,
245 2U * MMC_BLOCK_SIZE);
246 }
247#endif /* STM32MP_SDMMC || STM32MP_EMMC */
248
249 switch (image_id) {
250 case FW_CONFIG_ID:
251 /* Set global DTB info for fixed fw_config information */
252 set_config_info(STM32MP_FW_CONFIG_BASE, ~0UL, STM32MP_FW_CONFIG_MAX_SIZE,
253 FW_CONFIG_ID);
254 fconf_populate("FW_CONFIG", STM32MP_FW_CONFIG_BASE);
255
Yann Gautiercb84bf62024-09-02 11:40:43 +0200256 /*
257 * After this step, the BL2 device tree area will be overwritten
258 * with BL31 binary, no other data should be read from BL2 DT.
259 */
Yann Gautier88ca3352024-05-22 16:46:37 +0200260
261 break;
262
263 default:
264 /* Do nothing in default case */
265 break;
266 }
267
268 return err;
269}