bl2_plat_setup.c 9.44 KB
Newer Older
1
/*
2
 * Copyright (c) 2015-2020, ARM Limited and Contributors. All rights reserved.
3
4
5
6
7
 *
 * SPDX-License-Identifier: BSD-3-Clause
 */

#include <assert.h>
8
9
#include <string.h>

10
#include <platform_def.h>
11
12
13
14
15
16
17

#include <arch_helpers.h>
#include <common/bl_common.h>
#include <common/debug.h>
#include <common/desc_image_load.h>
#include <drivers/delay_timer.h>
#include <drivers/generic_delay_timer.h>
18
#include <drivers/st/bsec.h>
19
#include <drivers/st/stm32_console.h>
Yann Gautier's avatar
Yann Gautier committed
20
#include <drivers/st/stm32_iwdg.h>
21
#include <drivers/st/stm32mp_pmic.h>
22
#include <drivers/st/stm32mp_reset.h>
23
24
25
26
#include <drivers/st/stm32mp1_clk.h>
#include <drivers/st/stm32mp1_pwr.h>
#include <drivers/st/stm32mp1_ram.h>
#include <lib/mmio.h>
Yann Gautier's avatar
Yann Gautier committed
27
#include <lib/optee_utils.h>
28
29
30
#include <lib/xlat_tables/xlat_tables_v2.h>
#include <plat/common/platform.h>

Yann Gautier's avatar
Yann Gautier committed
31
#include <stm32mp1_context.h>
Yann Gautier's avatar
Yann Gautier committed
32
#include <stm32mp1_dbgmcu.h>
33

34
35
#define RESET_TIMEOUT_US_1MS		1000U

36
static console_t console;
37
static struct stm32mp_auth_ops stm32mp1_auth_ops;
Yann Gautier's avatar
Yann Gautier committed
38

Yann Gautier's avatar
Yann Gautier committed
39
40
static void print_reset_reason(void)
{
41
	uint32_t rstsr = mmio_read_32(stm32mp_rcc_base() + RCC_MP_RSTSCLRR);
Yann Gautier's avatar
Yann Gautier committed
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127

	if (rstsr == 0U) {
		WARN("Reset reason unknown\n");
		return;
	}

	INFO("Reset reason (0x%x):\n", rstsr);

	if ((rstsr & RCC_MP_RSTSCLRR_PADRSTF) == 0U) {
		if ((rstsr & RCC_MP_RSTSCLRR_STDBYRSTF) != 0U) {
			INFO("System exits from STANDBY\n");
			return;
		}

		if ((rstsr & RCC_MP_RSTSCLRR_CSTDBYRSTF) != 0U) {
			INFO("MPU exits from CSTANDBY\n");
			return;
		}
	}

	if ((rstsr & RCC_MP_RSTSCLRR_PORRSTF) != 0U) {
		INFO("  Power-on Reset (rst_por)\n");
		return;
	}

	if ((rstsr & RCC_MP_RSTSCLRR_BORRSTF) != 0U) {
		INFO("  Brownout Reset (rst_bor)\n");
		return;
	}

	if ((rstsr & RCC_MP_RSTSCLRR_MCSYSRSTF) != 0U) {
		if ((rstsr & RCC_MP_RSTSCLRR_PADRSTF) != 0U) {
			INFO("  System reset generated by MCU (MCSYSRST)\n");
		} else {
			INFO("  Local reset generated by MCU (MCSYSRST)\n");
		}
		return;
	}

	if ((rstsr & RCC_MP_RSTSCLRR_MPSYSRSTF) != 0U) {
		INFO("  System reset generated by MPU (MPSYSRST)\n");
		return;
	}

	if ((rstsr & RCC_MP_RSTSCLRR_HCSSRSTF) != 0U) {
		INFO("  Reset due to a clock failure on HSE\n");
		return;
	}

	if ((rstsr & RCC_MP_RSTSCLRR_IWDG1RSTF) != 0U) {
		INFO("  IWDG1 Reset (rst_iwdg1)\n");
		return;
	}

	if ((rstsr & RCC_MP_RSTSCLRR_IWDG2RSTF) != 0U) {
		INFO("  IWDG2 Reset (rst_iwdg2)\n");
		return;
	}

	if ((rstsr & RCC_MP_RSTSCLRR_MPUP0RSTF) != 0U) {
		INFO("  MPU Processor 0 Reset\n");
		return;
	}

	if ((rstsr & RCC_MP_RSTSCLRR_MPUP1RSTF) != 0U) {
		INFO("  MPU Processor 1 Reset\n");
		return;
	}

	if ((rstsr & RCC_MP_RSTSCLRR_PADRSTF) != 0U) {
		INFO("  Pad Reset from NRST\n");
		return;
	}

	if ((rstsr & RCC_MP_RSTSCLRR_VCORERSTF) != 0U) {
		INFO("  Reset due to a failure of VDD_CORE\n");
		return;
	}

	ERROR("  Unidentified reset reason\n");
}

void bl2_el3_early_platform_setup(u_register_t arg0,
				  u_register_t arg1 __unused,
				  u_register_t arg2 __unused,
				  u_register_t arg3 __unused)
128
{
129
	stm32mp_save_boot_ctx_address(arg0);
130
131
132
133
}

void bl2_platform_setup(void)
{
134
	int ret;
135
	uint32_t ddr_ns_size;
136

137
	if (dt_pmic_status() > 0) {
Yann Gautier's avatar
Yann Gautier committed
138
139
140
		initialize_pmic();
	}

141
142
143
144
145
146
	ret = stm32mp1_ddr_probe();
	if (ret < 0) {
		ERROR("Invalid DDR init: error %d\n", ret);
		panic();
	}

147
148
149
150
151
152
153
154
	ddr_ns_size = stm32mp_get_ddr_ns_size();
	assert(ddr_ns_size > 0U);

	/* Map non secure DDR for BL33 load, now with cacheable attribute */
	ret = mmap_add_dynamic_region(STM32MP_DDR_BASE, STM32MP_DDR_BASE,
				      ddr_ns_size, MT_MEMORY | MT_RW | MT_NS);
	assert(ret == 0);

Yann Gautier's avatar
Yann Gautier committed
155
156
#ifdef AARCH32_SP_OPTEE
	INFO("BL2 runs OP-TEE setup\n");
157
158
159
160
161
162
163
164

	/* Map secure DDR for OP-TEE paged area */
	ret = mmap_add_dynamic_region(STM32MP_DDR_BASE + ddr_ns_size,
				      STM32MP_DDR_BASE + ddr_ns_size,
				      STM32MP_DDR_S_SIZE,
				      MT_MEMORY | MT_RW | MT_SECURE);
	assert(ret == 0);

Yann Gautier's avatar
Yann Gautier committed
165
166
167
	/* Initialize tzc400 after DDR initialization */
	stm32mp1_security_setup();
#else
168
	INFO("BL2 runs SP_MIN setup\n");
Yann Gautier's avatar
Yann Gautier committed
169
#endif
170
171
172
173
}

void bl2_el3_plat_arch_setup(void)
{
Yann Gautier's avatar
Yann Gautier committed
174
	int32_t result;
Yann Gautier's avatar
Yann Gautier committed
175
	struct dt_node_info dt_uart_info;
Yann Gautier's avatar
Yann Gautier committed
176
	const char *board_model;
177
	boot_api_context_t *boot_context =
178
		(boot_api_context_t *)stm32mp_get_boot_ctx_address();
Yann Gautier's avatar
Yann Gautier committed
179
	uint32_t clk_rate;
180
181
	uintptr_t pwr_base;
	uintptr_t rcc_base;
182

Yann Gautier's avatar
Yann Gautier committed
183
184
185
186
	mmap_add_region(BL_CODE_BASE, BL_CODE_BASE,
			BL_CODE_END - BL_CODE_BASE,
			MT_CODE | MT_SECURE);

Yann Gautier's avatar
Yann Gautier committed
187
188
189
190
191
#ifdef AARCH32_SP_OPTEE
	mmap_add_region(STM32MP_OPTEE_BASE, STM32MP_OPTEE_BASE,
			STM32MP_OPTEE_SIZE,
			MT_MEMORY | MT_RW | MT_SECURE);
#else
Yann Gautier's avatar
Yann Gautier committed
192
193
194
	/* Prevent corruption of preloaded BL32 */
	mmap_add_region(BL32_BASE, BL32_BASE,
			BL32_LIMIT - BL32_BASE,
195
			MT_RO_DATA | MT_SECURE);
Yann Gautier's avatar
Yann Gautier committed
196
#endif
Yann Gautier's avatar
Yann Gautier committed
197
198
199
	/* Prevent corruption of preloaded Device Tree */
	mmap_add_region(DTB_BASE, DTB_BASE,
			DTB_LIMIT - DTB_BASE,
200
			MT_RO_DATA | MT_SECURE);
Yann Gautier's avatar
Yann Gautier committed
201
202
203
204
205
206
207

	configure_mmu();

	if (dt_open_and_check() < 0) {
		panic();
	}

208
209
210
	pwr_base = stm32mp_pwr_base();
	rcc_base = stm32mp_rcc_base();

211
212
213
214
215
	/*
	 * Disable the backup domain write protection.
	 * The protection is enable at each reset by hardware
	 * and must be disabled by software.
	 */
216
	mmio_setbits_32(pwr_base + PWR_CR1, PWR_CR1_DBP);
217

218
	while ((mmio_read_32(pwr_base + PWR_CR1) & PWR_CR1_DBP) == 0U) {
219
220
221
		;
	}

222
223
224
225
	if (bsec_probe() != 0) {
		panic();
	}

226
	/* Reset backup domain on cold boot cases */
227
228
	if ((mmio_read_32(rcc_base + RCC_BDCR) & RCC_BDCR_RTCSRC_MASK) == 0U) {
		mmio_setbits_32(rcc_base + RCC_BDCR, RCC_BDCR_VSWRST);
229

230
		while ((mmio_read_32(rcc_base + RCC_BDCR) & RCC_BDCR_VSWRST) ==
231
232
233
234
		       0U) {
			;
		}

235
		mmio_clrbits_32(rcc_base + RCC_BDCR, RCC_BDCR_VSWRST);
236
237
	}

238
239
240
	/* Disable MCKPROT */
	mmio_clrbits_32(rcc_base + RCC_TZCR, RCC_TZCR_MCKPROT);

241
242
	generic_delay_timer_init();

243
244
245
246
247
248
249
250
	if (stm32mp1_clk_probe() < 0) {
		panic();
	}

	if (stm32mp1_clk_init() < 0) {
		panic();
	}

251
252
	stm32mp1_syscfg_init();

Yann Gautier's avatar
Yann Gautier committed
253
	result = dt_get_stdout_uart_info(&dt_uart_info);
Yann Gautier's avatar
Yann Gautier committed
254
255

	if ((result <= 0) ||
Yann Gautier's avatar
Yann Gautier committed
256
257
258
	    (dt_uart_info.status == 0U) ||
	    (dt_uart_info.clock < 0) ||
	    (dt_uart_info.reset < 0)) {
Yann Gautier's avatar
Yann Gautier committed
259
260
261
262
263
264
265
		goto skip_console_init;
	}

	if (dt_set_stdout_pinctrl() != 0) {
		goto skip_console_init;
	}

Yann Gautier's avatar
Yann Gautier committed
266
	stm32mp_clk_enable((unsigned long)dt_uart_info.clock);
Yann Gautier's avatar
Yann Gautier committed
267

268
269
270
271
272
	if (stm32mp_reset_assert((uint32_t)dt_uart_info.reset,
				 RESET_TIMEOUT_US_1MS) != 0) {
		panic();
	}

Yann Gautier's avatar
Yann Gautier committed
273
	udelay(2);
274
275
276
277
278
279

	if (stm32mp_reset_deassert((uint32_t)dt_uart_info.reset,
				   RESET_TIMEOUT_US_1MS) != 0) {
		panic();
	}

Yann Gautier's avatar
Yann Gautier committed
280
281
	mdelay(1);

282
	clk_rate = stm32mp_clk_get_rate((unsigned long)dt_uart_info.clock);
Yann Gautier's avatar
Yann Gautier committed
283

Yann Gautier's avatar
Yann Gautier committed
284
	if (console_stm32_register(dt_uart_info.base, clk_rate,
285
				   STM32MP_UART_BAUDRATE, &console) == 0) {
Yann Gautier's avatar
Yann Gautier committed
286
287
288
		panic();
	}

289
	console_set_scope(&console, CONSOLE_FLAG_BOOT |
290
291
			  CONSOLE_FLAG_CRASH | CONSOLE_FLAG_TRANSLATE_CRLF);

292
293
	stm32mp_print_cpuinfo();

Yann Gautier's avatar
Yann Gautier committed
294
295
	board_model = dt_get_board_model();
	if (board_model != NULL) {
Yann Gautier's avatar
Yann Gautier committed
296
		NOTICE("Model: %s\n", board_model);
Yann Gautier's avatar
Yann Gautier committed
297
298
	}

299
300
	stm32mp_print_boardinfo();

301
302
303
304
305
306
	if (boot_context->auth_status != BOOT_API_CTX_AUTH_NO) {
		NOTICE("Bootrom authentication %s\n",
		       (boot_context->auth_status == BOOT_API_CTX_AUTH_FAILED) ?
		       "failed" : "succeeded");
	}

Yann Gautier's avatar
Yann Gautier committed
307
skip_console_init:
Yann Gautier's avatar
Yann Gautier committed
308
309
310
311
312
313
314
315
316
317
	if (stm32_iwdg_init() < 0) {
		panic();
	}

	stm32_iwdg_refresh();

	result = stm32mp1_dbgmcu_freeze_iwdg2();
	if (result != 0) {
		INFO("IWDG2 freeze error : %i\n", result);
	}
Yann Gautier's avatar
Yann Gautier committed
318

319
320
321
322
323
324
	if (stm32_save_boot_interface(boot_context->boot_interface_selected,
				      boot_context->boot_interface_instance) !=
	    0) {
		ERROR("Cannot save boot interface\n");
	}

325
326
327
328
329
330
	stm32mp1_auth_ops.check_key = boot_context->bootrom_ecdsa_check_key;
	stm32mp1_auth_ops.verify_signature =
		boot_context->bootrom_ecdsa_verify_signature;

	stm32mp_init_auth(&stm32mp1_auth_ops);

331
332
	stm32mp1_arch_security_setup();

Yann Gautier's avatar
Yann Gautier committed
333
334
	print_reset_reason();

335
	stm32mp_io_setup();
336
}
Yann Gautier's avatar
Yann Gautier committed
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366

#if defined(AARCH32_SP_OPTEE)
/*******************************************************************************
 * This function can be used by the platforms to update/use image
 * information for given `image_id`.
 ******************************************************************************/
int bl2_plat_handle_post_image_load(unsigned int image_id)
{
	int err = 0;
	bl_mem_params_node_t *bl_mem_params = get_bl_mem_params_node(image_id);
	bl_mem_params_node_t *bl32_mem_params;
	bl_mem_params_node_t *pager_mem_params;
	bl_mem_params_node_t *paged_mem_params;

	assert(bl_mem_params != NULL);

	switch (image_id) {
	case BL32_IMAGE_ID:
		bl_mem_params->ep_info.pc =
					bl_mem_params->image_info.image_base;

		pager_mem_params = get_bl_mem_params_node(BL32_EXTRA1_IMAGE_ID);
		assert(pager_mem_params != NULL);
		pager_mem_params->image_info.image_base = STM32MP_OPTEE_BASE;
		pager_mem_params->image_info.image_max_size =
			STM32MP_OPTEE_SIZE;

		paged_mem_params = get_bl_mem_params_node(BL32_EXTRA2_IMAGE_ID);
		assert(paged_mem_params != NULL);
		paged_mem_params->image_info.image_base = STM32MP_DDR_BASE +
367
			stm32mp_get_ddr_ns_size();
Yann Gautier's avatar
Yann Gautier committed
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
		paged_mem_params->image_info.image_max_size =
			STM32MP_DDR_S_SIZE;

		err = parse_optee_header(&bl_mem_params->ep_info,
					 &pager_mem_params->image_info,
					 &paged_mem_params->image_info);
		if (err) {
			ERROR("OPTEE header parse error.\n");
			panic();
		}

		/* Set optee boot info from parsed header data */
		bl_mem_params->ep_info.pc =
				pager_mem_params->image_info.image_base;
		bl_mem_params->ep_info.args.arg0 =
				paged_mem_params->image_info.image_base;
		bl_mem_params->ep_info.args.arg1 = 0; /* Unused */
		bl_mem_params->ep_info.args.arg2 = 0; /* No DT supported */
		break;

	case BL33_IMAGE_ID:
		bl32_mem_params = get_bl_mem_params_node(BL32_IMAGE_ID);
		assert(bl32_mem_params != NULL);
		bl32_mem_params->ep_info.lr_svc = bl_mem_params->ep_info.pc;
		break;

	default:
		/* Do nothing in default case */
		break;
	}

	return err;
}
#endif