Contiki-NG
Loading...
Searching...
No Matches
tz-target-cfg.c
1/*
2 * Copyright (c) 2018-2020 Arm Limited. All rights reserved.
3 * Copyright (c) 2020 Nordic Semiconductor ASA.
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18/* This file has been modified for use in the Contiki-NG operating system. */
19
20#include "contiki.h"
21
22#include "tz-target-cfg.h"
23#include "region_defs.h"
24
25#include <spu.h>
26#include <nrfx.h>
27#include <hal/nrf_gpio.h>
28
29#include "nrf5340_application_bitfields.h"
30
31/******************************************************************************/
32#include "sys/log.h"
33#define LOG_MODULE "TZSecureWorld"
34#define LOG_LEVEL LOG_LEVEL_DBG
35/******************************************************************************/
36
37#define PIN_XL1 0
38#define PIN_XL2 1
39
40/* To write into AIRCR register, 0x5FA value must be write to the VECTKEY field,
41 * otherwise the processor ignores the write.
42 */
43#define SCB_AIRCR_WRITE_MASK ((0x5FAUL << SCB_AIRCR_VECTKEY_Pos))
44/******************************************************************************/
46enable_fault_handlers(void)
47{
48 /* Explicitly set secure fault priority to the highest */
49 NVIC_SetPriority(SecureFault_IRQn, 0);
50
51 /* Enables BUS, MEM, USG and Secure faults */
52 SCB->SHCSR |= SCB_SHCSR_USGFAULTENA_Msk | SCB_SHCSR_BUSFAULTENA_Msk | SCB_SHCSR_MEMFAULTENA_Msk | SCB_SHCSR_SECUREFAULTENA_Msk;
53 return TFM_PLAT_ERR_SUCCESS;
54}
55/******************************************************************************/
57system_reset_cfg(void)
58{
59 uint32_t reg_value = SCB->AIRCR;
60
61 /* Clear SCB_AIRCR_VECTKEY value */
62 reg_value &= ~(uint32_t)(SCB_AIRCR_VECTKEY_Msk);
63
64 /* Enable system reset request only to the secure world */
65 reg_value |= (uint32_t)(SCB_AIRCR_WRITE_MASK | SCB_AIRCR_SYSRESETREQS_Msk);
66
67 SCB->AIRCR = reg_value;
68
69 return TFM_PLAT_ERR_SUCCESS;
70}
71/******************************************************************************/
72/*----------------- NVIC interrupt target state to NS configuration ----------*/
74nvic_interrupt_target_state_cfg(void)
75{
76 /* Target most interrupt to NS; unimplemented interrupts will be
77 Write-Ignored */
78
79 NVIC_SetTargetState(NRFX_IRQ_NUMBER_GET(NRF_TIMER0));
80 NVIC_SetTargetState(NRFX_IRQ_NUMBER_GET(NRF_RTC0));
81
82 for(uint8_t i = 1; i < sizeof(NVIC->ITNS) / sizeof(NVIC->ITNS[0]); i++) {
83 NVIC->ITNS[i] = 0xffffffff;
84 }
85
86 /* Make sure that the SPU is targeted to S state */
87 NVIC_ClearTargetState(NRFX_IRQ_NUMBER_GET(NRF_SPU));
88
89#ifdef SECURE_UART0
90 /* UARTE0 is a secure peripheral, so its IRQ has to target S state */
91 NVIC_ClearTargetState(NRFX_IRQ_NUMBER_GET(NRF_UARTE0));
92#endif
93
94#ifdef SECURE_UART1
95 /* UARTE1 is a secure peripheral, so its IRQ has to target S state */
96 NVIC_ClearTargetState(NRFX_IRQ_NUMBER_GET(NRF_UARTE1));
97#endif
98
99 return TFM_PLAT_ERR_SUCCESS;
100}
101/******************************************************************************/
102/*----------------- NVIC interrupt enabling for S peripherals ----------------*/
104nvic_interrupt_enable(void)
105{
106 /* SPU interrupt enabling */
107 spu_enable_interrupts();
108
109 NVIC_ClearPendingIRQ(NRFX_IRQ_NUMBER_GET(NRF_SPU));
110 NVIC_EnableIRQ(NRFX_IRQ_NUMBER_GET(NRF_SPU));
111
112 return TFM_PLAT_ERR_SUCCESS;
113}
114/******************************************************************************/
115/*------------------- SAU/IDAU configuration functions -----------------------*/
116void
117sau_and_idau_cfg(void)
118{
119 /* IDAU (SPU) is always enabled. SAU is non-existent.
120 * Allow SPU to have precedence over (non-existing) ARMv8-M SAU.
121 */
122 TZ_SAU_Disable();
123 SAU->CTRL |= SAU_CTRL_ALLNS_Msk;
124}
125/******************************************************************************/
127spu_periph_init_cfg(void)
128{
129 /* Peripheral configuration */
130
131 NVIC_DisableIRQ(NRFX_IRQ_NUMBER_GET(NRF_FPU));
132 spu_peripheral_config_non_secure((uint32_t)NRF_FPU, false);
133
134 NVIC_DisableIRQ(NRFX_IRQ_NUMBER_GET(NRF_REGULATORS));
135 spu_peripheral_config_non_secure((uint32_t)NRF_REGULATORS, false);
136
137 NVIC_DisableIRQ(NRFX_IRQ_NUMBER_GET(NRF_CLOCK));
138 spu_peripheral_config_non_secure((uint32_t)NRF_CLOCK, true); /* Necessary */
139
140#ifndef SECURE_UART0
141 /* If UART0 is a secure peripheral, we need to leave Serial-Box 0 as Secure */
142 NVIC_DisableIRQ(NRFX_IRQ_NUMBER_GET(NRF_SPIM0));
143 spu_peripheral_config_non_secure((uint32_t)NRF_SPIM0, false);
144#endif
145
146#ifndef SECURE_UART1
147 /* If UART1 is a secure peripheral, we need to leave Serial-Box 1 as Secure */
148 NVIC_DisableIRQ(NRFX_IRQ_NUMBER_GET(NRF_SPIM1));
149 spu_peripheral_config_non_secure((uint32_t)NRF_SPIM1, false);
150#endif
151
152 NVIC_DisableIRQ(NRFX_IRQ_NUMBER_GET(NRF_SPIM4));
153 spu_peripheral_config_non_secure((uint32_t)NRF_SPIM4, false);
154
155 NVIC_DisableIRQ(NRFX_IRQ_NUMBER_GET(NRF_SPIM2));
156 spu_peripheral_config_non_secure((uint32_t)NRF_SPIM2, false);
157
158 NVIC_DisableIRQ(NRFX_IRQ_NUMBER_GET(NRF_SPIM3));
159 spu_peripheral_config_non_secure((uint32_t)NRF_SPIM3, false);
160
161 NVIC_DisableIRQ(NRFX_IRQ_NUMBER_GET(NRF_SAADC));
162 spu_peripheral_config_non_secure((uint32_t)NRF_SAADC, false);
163
164 NVIC_DisableIRQ(NRFX_IRQ_NUMBER_GET(NRF_TIMER0));
165 spu_peripheral_config_non_secure((uint32_t)NRF_TIMER0, false);
166
167#if 0
168 NVIC_DisableIRQ(NRFX_IRQ_NUMBER_GET(NRF_TIMER1));
169 spu_peripheral_config_non_secure((uint32_t)NRF_TIMER1, false);
170#endif
171
172 NVIC_DisableIRQ(NRFX_IRQ_NUMBER_GET(NRF_TIMER2));
173 spu_peripheral_config_non_secure((uint32_t)NRF_TIMER2, false);
174
175 NVIC_DisableIRQ(NRFX_IRQ_NUMBER_GET(NRF_RTC0));
176 spu_peripheral_config_non_secure((uint32_t)NRF_RTC0, false);
177
178#if 0
179 NVIC_DisableIRQ(NRFX_IRQ_NUMBER_GET(NRF_RTC1));
180 spu_peripheral_config_non_secure((uint32_t)NRF_RTC1, false);
181#endif
182
183 NVIC_DisableIRQ(NRFX_IRQ_NUMBER_GET(NRF_DPPIC));
184 spu_peripheral_config_non_secure((uint32_t)NRF_DPPIC, false);
185
186 NVIC_DisableIRQ(NRFX_IRQ_NUMBER_GET(NRF_WDT0));
187 spu_peripheral_config_non_secure((uint32_t)NRF_WDT0, false);
188
189 NVIC_DisableIRQ(NRFX_IRQ_NUMBER_GET(NRF_WDT1));
190 spu_peripheral_config_non_secure((uint32_t)NRF_WDT1, false);
191
192 NVIC_DisableIRQ(NRFX_IRQ_NUMBER_GET(NRF_COMP));
193 spu_peripheral_config_non_secure((uint32_t)NRF_COMP, false);
194
195 NVIC_DisableIRQ(NRFX_IRQ_NUMBER_GET(NRF_EGU0));
196 spu_peripheral_config_non_secure((uint32_t)NRF_EGU0, false);
197
198 NVIC_DisableIRQ(NRFX_IRQ_NUMBER_GET(NRF_EGU1));
199 spu_peripheral_config_non_secure((uint32_t)NRF_EGU1, false);
200
201 NVIC_DisableIRQ(NRFX_IRQ_NUMBER_GET(NRF_EGU2));
202 spu_peripheral_config_non_secure((uint32_t)NRF_EGU2, false);
203
204 NVIC_DisableIRQ(NRFX_IRQ_NUMBER_GET(NRF_EGU3));
205 spu_peripheral_config_non_secure((uint32_t)NRF_EGU3, false);
206
207 NVIC_DisableIRQ(NRFX_IRQ_NUMBER_GET(NRF_EGU4));
208 spu_peripheral_config_non_secure((uint32_t)NRF_EGU4, false);
209#ifndef PSA_API_TEST_IPC
210 /* EGU5 is used as a secure peripheral in PSA FF tests */
211
212 NVIC_DisableIRQ(NRFX_IRQ_NUMBER_GET(NRF_EGU5));
213 spu_peripheral_config_non_secure((uint32_t)NRF_EGU5, false);
214#endif
215
216 NVIC_DisableIRQ(NRFX_IRQ_NUMBER_GET(NRF_PWM0));
217 spu_peripheral_config_non_secure((uint32_t)NRF_PWM0, false); /* Necessary */
218
219 NVIC_DisableIRQ(NRFX_IRQ_NUMBER_GET(NRF_PWM1));
220 spu_peripheral_config_non_secure((uint32_t)NRF_PWM1, false);
221
222 NVIC_DisableIRQ(NRFX_IRQ_NUMBER_GET(NRF_PWM2));
223 spu_peripheral_config_non_secure((uint32_t)NRF_PWM2, false);
224
225 NVIC_DisableIRQ(NRFX_IRQ_NUMBER_GET(NRF_PWM3));
226 spu_peripheral_config_non_secure((uint32_t)NRF_PWM3, false);
227
228 NVIC_DisableIRQ(NRFX_IRQ_NUMBER_GET(NRF_PDM0));
229 spu_peripheral_config_non_secure((uint32_t)NRF_PDM0, false);
230
231 NVIC_DisableIRQ(NRFX_IRQ_NUMBER_GET(NRF_I2S0));
232 spu_peripheral_config_non_secure((uint32_t)NRF_I2S0, false);
233
234 NVIC_DisableIRQ(NRFX_IRQ_NUMBER_GET(NRF_IPC));
235 spu_peripheral_config_non_secure((uint32_t)NRF_IPC, false);
236
237 NVIC_DisableIRQ(NRFX_IRQ_NUMBER_GET(NRF_QSPI));
238 spu_peripheral_config_non_secure((uint32_t)NRF_QSPI, false);
239
240 NVIC_DisableIRQ(NRFX_IRQ_NUMBER_GET(NRF_NFCT));
241 spu_peripheral_config_non_secure((uint32_t)NRF_NFCT, false);
242
243 NVIC_DisableIRQ(NRFX_IRQ_NUMBER_GET(NRF_GPIOTE1_NS));
244 spu_peripheral_config_non_secure((uint32_t)NRF_GPIOTE1_NS, false);
245
246 NVIC_DisableIRQ(NRFX_IRQ_NUMBER_GET(NRF_MUTEX));
247 spu_peripheral_config_non_secure((uint32_t)NRF_MUTEX, false);
248
249 NVIC_DisableIRQ(NRFX_IRQ_NUMBER_GET(NRF_QDEC0));
250 spu_peripheral_config_non_secure((uint32_t)NRF_QDEC0, false);
251
252 NVIC_DisableIRQ(NRFX_IRQ_NUMBER_GET(NRF_QDEC1));
253 spu_peripheral_config_non_secure((uint32_t)NRF_QDEC1, false);
254
255 NVIC_DisableIRQ(NRFX_IRQ_NUMBER_GET(NRF_USBD));
256 spu_peripheral_config_non_secure((uint32_t)NRF_USBD, false);
257
258 NVIC_DisableIRQ(NRFX_IRQ_NUMBER_GET(NRF_USBREGULATOR));
259 spu_peripheral_config_non_secure((uint32_t)NRF_USBREGULATOR, false);
260
261 NVIC_DisableIRQ(NRFX_IRQ_NUMBER_GET(NRF_NVMC));
262 spu_peripheral_config_non_secure((uint32_t)NRF_NVMC, false); /* Necessary */
263
264 NVIC_DisableIRQ(NRFX_IRQ_NUMBER_GET(NRF_P0));
265 spu_peripheral_config_non_secure((uint32_t)NRF_P0, false); /* Necessary */
266
267 NVIC_DisableIRQ(NRFX_IRQ_NUMBER_GET(NRF_P1));
268 spu_peripheral_config_non_secure((uint32_t)NRF_P1, false);
269
270 NVIC_DisableIRQ(NRFX_IRQ_NUMBER_GET(NRF_VMC));
271 spu_peripheral_config_non_secure((uint32_t)NRF_VMC, false);
272
273#ifndef SECURE_UART1
274 NVIC_DisableIRQ(NRFX_IRQ_NUMBER_GET(NRF_UARTE1));
275 spu_peripheral_config_non_secure((uint32_t)NRF_UARTE1, false);
276#endif /* SECURE_UART1 */
277
278 /* Skip this one because it is secure explicitly. */
279 NVIC_DisableIRQ(NRFX_IRQ_NUMBER_GET(NRF_UARTE2));
280 spu_peripheral_config_non_secure((uint32_t)NRF_UARTE2, false);
281
282 NVIC_DisableIRQ(NRFX_IRQ_NUMBER_GET(NRF_TWIM2));
283 spu_peripheral_config_non_secure((uint32_t)NRF_TWIM2, false);
284
285 NVIC_DisableIRQ(NRFX_IRQ_NUMBER_GET(NRF_IPC_S));
286 spu_peripheral_config_non_secure((uint32_t)NRF_IPC_S, false);
287
288 NVIC_DisableIRQ(NRFX_IRQ_NUMBER_GET(NRF_VMC_S));
289 spu_peripheral_config_non_secure((uint32_t)NRF_VMC_S, false);
290
291 NVIC_DisableIRQ(NRFX_IRQ_NUMBER_GET(NRF_FPU_S));
292 spu_peripheral_config_non_secure((uint32_t)NRF_FPU_S, false);
293
294 NVIC_DisableIRQ(NRFX_IRQ_NUMBER_GET(NRF_EGU1_S));
295 spu_peripheral_config_non_secure((uint32_t)NRF_EGU1_S, false);
296
297 NVIC_DisableIRQ(NRFX_IRQ_NUMBER_GET(NRF_EGU2_S));
298 spu_peripheral_config_non_secure((uint32_t)NRF_EGU2_S, false);
299
300 NVIC_DisableIRQ(NRFX_IRQ_NUMBER_GET(NRF_DPPIC_S));
301 spu_peripheral_config_non_secure((uint32_t)NRF_DPPIC_S, false);
302
303 NVIC_DisableIRQ(NRFX_IRQ_NUMBER_GET(NRF_REGULATORS_S));
304 spu_peripheral_config_non_secure((uint32_t)NRF_REGULATORS_S, false);
305
306 /* DPPI channel configuration */
307 spu_dppi_config_non_secure(false);
308
309 /* GPIO pin configuration (P0 and P1 ports) */
310 spu_gpio_config_non_secure(0, true); /* P0.00 to P0.31 */
311 spu_gpio_config_non_secure(1, true); /* P1.00 to P1.15 */
312
313 /*
314 * Configure properly the XL1 and XL2 pins so that the low-frequency
315 * crystal oscillator (LFXO) can be used. This configuration can be
316 * done only from secure code, as otherwise those register fields
317 * are not accessible. That's why it is placed here.
318 */
319 nrf_gpio_pin_mcu_select(PIN_XL1, NRF_GPIO_PIN_MCUSEL_PERIPHERAL);
320 nrf_gpio_pin_mcu_select(PIN_XL2, NRF_GPIO_PIN_MCUSEL_PERIPHERAL);
321
322 /*
323 * Enable the instruction and data cache (this can be done only from secure
324 * code; that's why it is placed here).
325 */
326 NRF_CACHE->ENABLE = CACHE_ENABLE_ENABLE_Enabled;
327
328 return TFM_PLAT_ERR_SUCCESS;
329}
330/******************************************************************************/
331void
332spu_periph_configure_to_secure(uint32_t periph_num)
333{
334 spu_peripheral_config_secure(periph_num, true);
335}
336/******************************************************************************/
337void
338spu_periph_configure_to_non_secure(uint32_t periph_num)
339{
340 spu_peripheral_config_non_secure(periph_num, true);
341}
342/******************************************************************************/
343void
344spu_periph_config_uarte(void)
345{
346#ifndef SECURE_UART0
347 NVIC_DisableIRQ(NRFX_IRQ_NUMBER_GET(NRF_UARTE0));
348 spu_peripheral_config_non_secure((uint32_t)NRF_UARTE0, false);
349#endif /* SECURE_UART0 */
350}
351/******************************************************************************/
352void
353non_secure_configuration(void)
354{
355 spu_regions_reset_all_secure();
356 /* Hard coded linker script addresses. */
357 spu_regions_flash_config_non_secure((uint32_t)NS_CODE_START,
358 (uint32_t)NS_ROM_LIMIT_ADDR);
359 spu_regions_sram_config_non_secure((uint32_t)NS_DATA_START,
360 (uint32_t)NS_DATA_LIMIT);
361 spu_periph_init_cfg();
362}
363/******************************************************************************/
364void
365configure_nonsecure_vtor_offset(uint32_t vtor_ns)
366{
367 SCB_NS->VTOR = vtor_ns;
368}
369/******************************************************************************/
370void
371configure_nonsecure_msp(uint32_t msp_ns)
372{
373 __TZ_set_MSP_NS(msp_ns);
374}
375/******************************************************************************/
376static void
377configure_nonsecure_psp(uint32_t psp_ns)
378{
379 __TZ_set_PSP_NS(psp_ns);
380}
381/******************************************************************************/
382static void
383configure_nonsecure_control(uint32_t spsel_ns, uint32_t npriv_ns)
384{
385 uint32_t control_ns = __TZ_get_CONTROL_NS();
386
387 /* Only nPRIV and SPSEL bits are banked between security states. */
388 control_ns &= ~(CONTROL_SPSEL_Msk | CONTROL_nPRIV_Msk);
389
390 if(spsel_ns) {
391 control_ns |= CONTROL_SPSEL_Msk;
392 }
393 if(npriv_ns) {
394 control_ns |= CONTROL_nPRIV_Msk;
395 }
396
397 __TZ_set_CONTROL_NS(control_ns);
398}
399/******************************************************************************/
400void
401tz_nonsecure_state_setup(const tz_nonsecure_setup_conf_t *p_ns_conf)
402{
403 configure_nonsecure_vtor_offset(p_ns_conf->vtor_ns);
404 configure_nonsecure_msp(p_ns_conf->msp_ns);
405 configure_nonsecure_psp(p_ns_conf->psp_ns);
406
407 /* Select which stack pointer to use (MSP or PSP) and the privilege
408 level for thread mode. */
409 configure_nonsecure_control(p_ns_conf->control_ns.spsel,
410 p_ns_conf->control_ns.npriv);
411}
412/******************************************************************************/
Header file for the logging system.
A convenient struct to include all required Non-Secure state configuration.
nRF5340 target configuration header
tfm_plat_err_t
TFM error codes.