bl2_plat_setup.c 5.74 KB
Newer Older
1
/*
2
 * Copyright (c) 2015-2019, 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
18

#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>
#include <drivers/st/stm32_console.h>
19
#include <drivers/st/stm32mp_pmic.h>
20
#include <drivers/st/stm32mp_reset.h>
21
22
23
24
25
26
27
#include <drivers/st/stm32mp1_clk.h>
#include <drivers/st/stm32mp1_pwr.h>
#include <drivers/st/stm32mp1_ram.h>
#include <lib/mmio.h>
#include <lib/xlat_tables/xlat_tables_v2.h>
#include <plat/common/platform.h>

Yann Gautier's avatar
Yann Gautier committed
28
#include <stm32mp1_context.h>
29

Yann Gautier's avatar
Yann Gautier committed
30
31
static struct console_stm32 console;

Yann Gautier's avatar
Yann Gautier committed
32
33
static void print_reset_reason(void)
{
34
	uint32_t rstsr = mmio_read_32(stm32mp_rcc_base() + RCC_MP_RSTSCLRR);
Yann Gautier's avatar
Yann Gautier committed
35
36
37
38
39
40
41
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

	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)
121
{
122
	stm32mp_save_boot_ctx_address(arg0);
123
124
125
126
}

void bl2_platform_setup(void)
{
127
128
	int ret;

129
	if (dt_pmic_status() > 0) {
Yann Gautier's avatar
Yann Gautier committed
130
131
132
		initialize_pmic();
	}

133
134
135
136
137
138
	ret = stm32mp1_ddr_probe();
	if (ret < 0) {
		ERROR("Invalid DDR init: error %d\n", ret);
		panic();
	}

139
140
141
142
143
	INFO("BL2 runs SP_MIN setup\n");
}

void bl2_el3_plat_arch_setup(void)
{
Yann Gautier's avatar
Yann Gautier committed
144
	int32_t result;
Yann Gautier's avatar
Yann Gautier committed
145
	struct dt_node_info dt_uart_info;
Yann Gautier's avatar
Yann Gautier committed
146
	const char *board_model;
147
	boot_api_context_t *boot_context =
148
		(boot_api_context_t *)stm32mp_get_boot_ctx_address();
Yann Gautier's avatar
Yann Gautier committed
149
	uint32_t clk_rate;
150
151
	uintptr_t pwr_base;
	uintptr_t rcc_base;
152

Yann Gautier's avatar
Yann Gautier committed
153
154
155
156
157
158
159
160
161
162
	mmap_add_region(BL_CODE_BASE, BL_CODE_BASE,
			BL_CODE_END - BL_CODE_BASE,
			MT_CODE | MT_SECURE);

	/* Prevent corruption of preloaded BL32 */
	mmap_add_region(BL32_BASE, BL32_BASE,
			BL32_LIMIT - BL32_BASE,
			MT_MEMORY | MT_RO | MT_SECURE);

	/* Map non secure DDR for BL33 load and DDR training area restore */
163
164
165
	mmap_add_region(STM32MP_DDR_BASE,
			STM32MP_DDR_BASE,
			STM32MP_DDR_MAX_SIZE,
Yann Gautier's avatar
Yann Gautier committed
166
167
168
169
170
171
172
173
174
175
176
177
178
			MT_MEMORY | MT_RW | MT_NS);

	/* Prevent corruption of preloaded Device Tree */
	mmap_add_region(DTB_BASE, DTB_BASE,
			DTB_LIMIT - DTB_BASE,
			MT_MEMORY | MT_RO | MT_SECURE);

	configure_mmu();

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

179
180
181
	pwr_base = stm32mp_pwr_base();
	rcc_base = stm32mp_rcc_base();

182
183
184
185
186
	/*
	 * Disable the backup domain write protection.
	 * The protection is enable at each reset by hardware
	 * and must be disabled by software.
	 */
187
	mmio_setbits_32(pwr_base + PWR_CR1, PWR_CR1_DBP);
188

189
	while ((mmio_read_32(pwr_base + PWR_CR1) & PWR_CR1_DBP) == 0U) {
190
191
192
193
		;
	}

	/* Reset backup domain on cold boot cases */
194
195
	if ((mmio_read_32(rcc_base + RCC_BDCR) & RCC_BDCR_RTCSRC_MASK) == 0U) {
		mmio_setbits_32(rcc_base + RCC_BDCR, RCC_BDCR_VSWRST);
196

197
		while ((mmio_read_32(rcc_base + RCC_BDCR) & RCC_BDCR_VSWRST) ==
198
199
200
201
		       0U) {
			;
		}

202
		mmio_clrbits_32(rcc_base + RCC_BDCR, RCC_BDCR_VSWRST);
203
204
	}

205
206
207
	/* Disable MCKPROT */
	mmio_clrbits_32(rcc_base + RCC_TZCR, RCC_TZCR_MCKPROT);

208
209
	generic_delay_timer_init();

210
211
212
213
214
215
216
217
	if (stm32mp1_clk_probe() < 0) {
		panic();
	}

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

Yann Gautier's avatar
Yann Gautier committed
218
	result = dt_get_stdout_uart_info(&dt_uart_info);
Yann Gautier's avatar
Yann Gautier committed
219
220

	if ((result <= 0) ||
Yann Gautier's avatar
Yann Gautier committed
221
222
223
	    (dt_uart_info.status == 0U) ||
	    (dt_uart_info.clock < 0) ||
	    (dt_uart_info.reset < 0)) {
Yann Gautier's avatar
Yann Gautier committed
224
225
226
227
228
229
230
		goto skip_console_init;
	}

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

Yann Gautier's avatar
Yann Gautier committed
231
	stm32mp_clk_enable((unsigned long)dt_uart_info.clock);
Yann Gautier's avatar
Yann Gautier committed
232

233
	stm32mp_reset_assert((uint32_t)dt_uart_info.reset);
Yann Gautier's avatar
Yann Gautier committed
234
	udelay(2);
235
	stm32mp_reset_deassert((uint32_t)dt_uart_info.reset);
Yann Gautier's avatar
Yann Gautier committed
236
237
	mdelay(1);

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

Yann Gautier's avatar
Yann Gautier committed
240
	if (console_stm32_register(dt_uart_info.base, clk_rate,
241
				   STM32MP_UART_BAUDRATE, &console) == 0) {
Yann Gautier's avatar
Yann Gautier committed
242
243
244
245
246
		panic();
	}

	board_model = dt_get_board_model();
	if (board_model != NULL) {
Yann Gautier's avatar
Yann Gautier committed
247
		NOTICE("Model: %s\n", board_model);
Yann Gautier's avatar
Yann Gautier committed
248
249
250
251
	}

skip_console_init:

252
253
254
255
256
257
	if (stm32_save_boot_interface(boot_context->boot_interface_selected,
				      boot_context->boot_interface_instance) !=
	    0) {
		ERROR("Cannot save boot interface\n");
	}

258
259
	stm32mp1_arch_security_setup();

Yann Gautier's avatar
Yann Gautier committed
260
261
	print_reset_reason();

262
	stm32mp_io_setup();
263
}