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

7
#include <assert.h>
8

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

#include <arch_helpers.h>
#include <common/bl_common.h>
#include <common/debug.h>
#include <bl31/interrupt_mgmt.h>
#include <drivers/console.h>
16
#include <drivers/rpi3/gpio/rpi3_gpio.h>
17
#include <drivers/ti/uart/uart_16550.h>
18
#include <drivers/arm/pl011.h>
19
#include <lib/xlat_tables/xlat_tables_v2.h>
20

21
#include <rpi_hw.h>
22
#include <rpi_shared.h>
23
24
25
26
27

#define MAP_DEVICE0	MAP_REGION_FLAT(DEVICE0_BASE,			\
					DEVICE0_SIZE,			\
					MT_DEVICE | MT_RW | MT_SECURE)

28
#ifdef SHARED_RAM_BASE
29
30
#define MAP_SHARED_RAM	MAP_REGION_FLAT(SHARED_RAM_BASE,		\
					SHARED_RAM_SIZE,		\
31
					MT_DEVICE | MT_RW | MT_SECURE)
32
#endif
33
34
35
36
37

#ifdef RPI3_PRELOADED_DTB_BASE
#define MAP_NS_DTB	MAP_REGION_FLAT(RPI3_PRELOADED_DTB_BASE, 0x10000, \
					MT_MEMORY | MT_RW | MT_NS)
#endif
38
39
40
41
42
43
44
45
46
47
48

#define MAP_NS_DRAM0	MAP_REGION_FLAT(NS_DRAM0_BASE, NS_DRAM0_SIZE,	\
					MT_MEMORY | MT_RW | MT_NS)

#define MAP_FIP		MAP_REGION_FLAT(PLAT_RPI3_FIP_BASE,		\
					PLAT_RPI3_FIP_MAX_SIZE,		\
					MT_MEMORY | MT_RO | MT_NS)

#define MAP_BL32_MEM	MAP_REGION_FLAT(BL32_MEM_BASE, BL32_MEM_SIZE,	\
					MT_MEMORY | MT_RW | MT_SECURE)

49
50
51
52
53
54
55
#ifdef SPD_opteed
#define MAP_OPTEE_PAGEABLE	MAP_REGION_FLAT(		\
				RPI3_OPTEE_PAGEABLE_LOAD_BASE,	\
				RPI3_OPTEE_PAGEABLE_LOAD_SIZE,	\
				MT_MEMORY | MT_RW | MT_SECURE)
#endif

56
57
58
59
60
/*
 * Table of regions for various BL stages to map using the MMU.
 */
#ifdef IMAGE_BL1
static const mmap_region_t plat_rpi3_mmap[] = {
61
#ifdef MAP_SHARED_RAM
62
	MAP_SHARED_RAM,
63
#endif
64
65
	MAP_DEVICE0,
	MAP_FIP,
66
67
68
#ifdef SPD_opteed
	MAP_OPTEE_PAGEABLE,
#endif
69
70
71
72
73
74
	{0}
};
#endif

#ifdef IMAGE_BL2
static const mmap_region_t plat_rpi3_mmap[] = {
75
#ifdef MAP_SHARED_RAM
76
	MAP_SHARED_RAM,
77
#endif
78
79
80
81
82
83
84
85
86
87
88
89
	MAP_DEVICE0,
	MAP_FIP,
	MAP_NS_DRAM0,
#ifdef BL32_BASE
	MAP_BL32_MEM,
#endif
	{0}
};
#endif

#ifdef IMAGE_BL31
static const mmap_region_t plat_rpi3_mmap[] = {
90
#ifdef MAP_SHARED_RAM
91
	MAP_SHARED_RAM,
92
#endif
93
	MAP_DEVICE0,
94
95
96
#ifdef RPI3_PRELOADED_DTB_BASE
	MAP_NS_DTB,
#endif
97
98
99
100
101
102
103
#ifdef BL32_BASE
	MAP_BL32_MEM,
#endif
	{0}
};
#endif

Antonio Nino Diaz's avatar
Antonio Nino Diaz committed
104
105
106
/*******************************************************************************
 * Function that sets up the console
 ******************************************************************************/
107
static console_t rpi3_console;
Antonio Nino Diaz's avatar
Antonio Nino Diaz committed
108

109

110
111
static bool rpi3_use_mini_uart(void)
{
112
	return rpi3_gpio_get_select(14) == RPI3_GPIO_FUNC_ALT5;
113
114
}

115
void rpi3_console_init(void)
Antonio Nino Diaz's avatar
Antonio Nino Diaz committed
116
{
117
	int console_scope = CONSOLE_FLAG_BOOT;
118
119
120
121
122
	int rc;

	if (RPI3_RUNTIME_UART != -1)
		console_scope |= CONSOLE_FLAG_RUNTIME;

123
124
	rpi3_gpio_init();

125
126
127
128
129
130
131
132
133
134
	if (rpi3_use_mini_uart())
		rc = console_16550_register(PLAT_RPI_MINI_UART_BASE,
					    0,
					    PLAT_RPI_UART_BAUDRATE,
					    &rpi3_console);
	else
		rc = console_pl011_register(PLAT_RPI_PL011_UART_BASE,
					    PLAT_RPI_PL011_UART_CLOCK,
					    PLAT_RPI_UART_BAUDRATE,
					    &rpi3_console);
135

Antonio Nino Diaz's avatar
Antonio Nino Diaz committed
136
137
138
139
140
141
142
143
144
	if (rc == 0) {
		/*
		 * The crash console doesn't use the multi console API, it uses
		 * the core console functions directly. It is safe to call panic
		 * and let it print debug information.
		 */
		panic();
	}

145
	console_set_scope(&rpi3_console, console_scope);
Antonio Nino Diaz's avatar
Antonio Nino Diaz committed
146
147
}

148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
/*******************************************************************************
 * Function that sets up the translation tables.
 ******************************************************************************/
void rpi3_setup_page_tables(uintptr_t total_base, size_t total_size,
			    uintptr_t code_start, uintptr_t code_limit,
			    uintptr_t rodata_start, uintptr_t rodata_limit
#if USE_COHERENT_MEM
			    , uintptr_t coh_start, uintptr_t coh_limit
#endif
			    )
{
	/*
	 * Map the Trusted SRAM with appropriate memory attributes.
	 * Subsequent mappings will adjust the attributes for specific regions.
	 */
	VERBOSE("Trusted SRAM seen by this BL image: %p - %p\n",
		(void *) total_base, (void *) (total_base + total_size));
	mmap_add_region(total_base, total_base,
			total_size,
			MT_MEMORY | MT_RW | MT_SECURE);

	/* Re-map the code section */
	VERBOSE("Code region: %p - %p\n",
		(void *) code_start, (void *) code_limit);
	mmap_add_region(code_start, code_start,
			code_limit - code_start,
			MT_CODE | MT_SECURE);

	/* Re-map the read-only data section */
	VERBOSE("Read-only data region: %p - %p\n",
		(void *) rodata_start, (void *) rodata_limit);
	mmap_add_region(rodata_start, rodata_start,
			rodata_limit - rodata_start,
			MT_RO_DATA | MT_SECURE);

#if USE_COHERENT_MEM
	/* Re-map the coherent memory region */
	VERBOSE("Coherent region: %p - %p\n",
		(void *) coh_start, (void *) coh_limit);
	mmap_add_region(coh_start, coh_start,
			coh_limit - coh_start,
			MT_DEVICE | MT_RW | MT_SECURE);
#endif

	mmap_add(plat_rpi3_mmap);

	init_xlat_tables();
}

/*******************************************************************************
 * Gets SPSR for BL32 entry
 ******************************************************************************/
uint32_t rpi3_get_spsr_for_bl32_entry(void)
{
	/*
	 * The Secure Payload Dispatcher service is responsible for
	 * setting the SPSR prior to entry into the BL32 image.
	 */
	return 0;
}

/*******************************************************************************
 * Gets SPSR for BL33 entry
 ******************************************************************************/
uint32_t rpi3_get_spsr_for_bl33_entry(void)
{
#if RPI3_BL33_IN_AARCH32
	INFO("BL33 will boot in Non-secure AArch32 Hypervisor mode\n");
	return SPSR_MODE32(MODE32_hyp, SPSR_T_ARM, SPSR_E_LITTLE,
			   DISABLE_ALL_EXCEPTIONS);
#else
	return SPSR_64(MODE_EL2, MODE_SP_ELX, DISABLE_ALL_EXCEPTIONS);
#endif
}

unsigned int plat_get_syscnt_freq2(void)
{
	return SYS_COUNTER_FREQ_IN_TICKS;
}

uint32_t plat_ic_get_pending_interrupt_type(void)
{
230
	ERROR("rpi3: Interrupt routed to EL3.\n");
231
232
	return INTR_TYPE_INVAL;
}
233

234
uint32_t plat_interrupt_type_to_line(uint32_t type, uint32_t security_state)
235
{
236
237
238
239
240
241
242
243
244
245
246
	assert((type == INTR_TYPE_S_EL1) || (type == INTR_TYPE_EL3) ||
	       (type == INTR_TYPE_NS));

	assert(sec_state_is_valid(security_state));

	/* Non-secure interrupts are signalled on the IRQ line always. */
	if (type == INTR_TYPE_NS)
		return __builtin_ctz(SCR_IRQ_BIT);

	/* Secure interrupts are signalled on the FIQ line always. */
	return  __builtin_ctz(SCR_FIQ_BIT);
247
}