Skip to content

Commit

Permalink
[nrf toup] platform: nordic_nrf: Add support shared UART and using UA…
Browse files Browse the repository at this point in the history
…RT0 instance

Add support for selecting which UART instance to use as the secure UART
instance. The supported options are UART0 and UART1.

Add support for the secure UART instance being shared with the non-secure
application.
The UART instance is configured as non-secure after it has been
uninitialized, and configured as secure when it is initialized again
on a fatal error.

NCSDK-18595

Signed-off-by: Joakim Andersson <joakim.andersson@nordicsemi.no>
(cherry picked from commit b2346e8)
Signed-off-by: Joakim Andersson <joakim.andersson@nordicsemi.no>
  • Loading branch information
joerchan committed Feb 22, 2023
1 parent 54af7a2 commit 97224b0
Show file tree
Hide file tree
Showing 9 changed files with 69 additions and 4 deletions.
5 changes: 5 additions & 0 deletions platform/ext/target/nordic_nrf/common/core/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,11 @@ if(TFM_SPM_LOG_RAW_ENABLED)
cmsis_drivers/Driver_USART.c
${HAL_NORDIC_PATH}/nrfx/drivers/src/nrfx_uarte.c
)

target_compile_definitions(platform_s
PUBLIC
NRF_SECURE_UART_INSTANCE=${NRF_SECURE_UART_INSTANCE}
)
endif()

target_compile_options(platform_s
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@
#include <nrfx_uarte.h>
#include <string.h>

#if !(DOMAIN_NS == 1U) && defined(CONFIG_TFM_LOG_SHARE_UART)
#define SPU_CONFIGURE_UART
#include <spu.h>
#endif

#ifndef ARG_UNUSED
#define ARG_UNUSED(arg) (void)arg
#endif
Expand Down Expand Up @@ -64,6 +69,11 @@ static int32_t ARM_USARTx_Initialize(ARM_USART_SignalEvent_t cb_event,
{
ARG_UNUSED(cb_event);

#ifdef SPU_CONFIGURE_UART
spu_peripheral_config_secure((uint32_t)uart_resources->uarte.p_reg, false);
NVIC_ClearTargetState(NRFX_IRQ_NUMBER_GET((uint32_t)uart_resources->uarte.p_reg));
#endif

nrfx_err_t err_code = nrfx_uarte_init(&uart_resources->uarte,
uart_resources->initial_config,
NULL);
Expand All @@ -85,6 +95,12 @@ static int32_t ARM_USARTx_Uninitialize(UARTx_Resources *uart_resources)
nrfx_uarte_uninit(&uart_resources->uarte);

uart_resources->initialized = false;

#ifdef SPU_CONFIGURE_UART
spu_peripheral_config_non_secure((uint32_t)uart_resources->uarte.p_reg, false);
NVIC_SetTargetState(NRFX_IRQ_NUMBER_GET((uint32_t)uart_resources->uarte.p_reg));
#endif

return ARM_DRIVER_OK;
}

Expand Down
2 changes: 2 additions & 0 deletions platform/ext/target/nordic_nrf/common/core/config.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,5 @@ set(NRF_HW_INIT_NRF_PERIPHERALS OFF CACHE BOOL "Initialize nRF peripherals at bo
if (NRF_HW_INIT_NRF_PERIPHERALS AND NOT NRF_HW_INIT_RESET_ON_BOOT)
message(FATAL_ERROR "NRF_HW_INIT_NRF_PERIPHERALS depends on NRF_HW_INIT_RESET_ON_BOOT")
endif()

set(NRF_SECURE_UART_INSTANCE 1 CACHE STRING "The UART instance number to use for secure UART")
17 changes: 15 additions & 2 deletions platform/ext/target/nordic_nrf/common/nrf5340/target_cfg.c
Original file line number Diff line number Diff line change
Expand Up @@ -615,8 +615,13 @@ enum tfm_plat_err_t nvic_interrupt_target_state_cfg(void)
NVIC_ClearTargetState(NRFX_IRQ_NUMBER_GET(NRF_SPU));

#ifdef SECURE_UART1
#if NRF_SECURE_UART_INSTANCE == 0
/* UARTE0 is a secure peripheral, so its IRQ has to target S state */
NVIC_ClearTargetState(NRFX_IRQ_NUMBER_GET(NRF_UARTE0));
#elif NRF_SECURE_UART_INSTANCE == 1
/* UARTE1 is a secure peripheral, so its IRQ has to target S state */
NVIC_ClearTargetState(NRFX_IRQ_NUMBER_GET(NRF_UARTE1));
#endif
#endif

return TFM_PLAT_ERR_SUCCESS;
Expand Down Expand Up @@ -712,11 +717,19 @@ enum tfm_plat_err_t spu_periph_init_cfg(void)
* - TWISx
* - UARTEx
*/

/* When UART0 is a secure peripheral we need to leave Serial-Box 0 as Secure.
* The UART Driver will configure it as non-secure when it uninitializes.
*/
#if !(defined(SECURE_UART1) && NRF_SECURE_UART_INSTANCE == 0)
spu_peripheral_config_non_secure((uint32_t)NRF_SPIM0, false);
#ifndef SECURE_UART1
/* UART1 is a secure peripheral, so we need to leave Serial-Box 1 as Secure */
#endif

/* When UART1 is a secure peripheral we need to leave Serial-Box 1 as Secure */
#if !(defined(SECURE_UART1) && NRF_SECURE_UART_INSTANCE == 1)
spu_peripheral_config_non_secure((uint32_t)NRF_SPIM1, false);
#endif

spu_peripheral_config_non_secure((uint32_t)NRF_SPIM4, false);
spu_peripheral_config_non_secure((uint32_t)NRF_SPIM2, false);
spu_peripheral_config_non_secure((uint32_t)NRF_SPIM3, false);
Expand Down
4 changes: 4 additions & 0 deletions platform/ext/target/nordic_nrf/common/nrf5340/target_cfg.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,11 @@
#include "tfm_plat_defs.h"
#include "region_defs.h"

#if NRF_SECURE_UART_INSTANCE == 0
#define TFM_DRIVER_STDIO Driver_USART0
#elif NRF_SECURE_UART_INSTANCE == 1
#define TFM_DRIVER_STDIO Driver_USART1
#endif
#define NS_DRIVER_STDIO Driver_USART0

/**
Expand Down
17 changes: 15 additions & 2 deletions platform/ext/target/nordic_nrf/common/nrf9160/target_cfg.c
Original file line number Diff line number Diff line change
Expand Up @@ -489,8 +489,13 @@ enum tfm_plat_err_t nvic_interrupt_target_state_cfg(void)
NVIC_ClearTargetState(NRFX_IRQ_NUMBER_GET(NRF_SPU));

#ifdef SECURE_UART1
#if NRF_SECURE_UART_INSTANCE == 0
/* UARTE0 is a secure peripheral, so its IRQ has to target S state */
NVIC_ClearTargetState(NRFX_IRQ_NUMBER_GET(NRF_UARTE0));
#elif NRF_SECURE_UART_INSTANCE == 1
/* UARTE1 is a secure peripheral, so its IRQ has to target S state */
NVIC_ClearTargetState(NRFX_IRQ_NUMBER_GET(NRF_UARTE1));
#endif
#endif

return TFM_PLAT_ERR_SUCCESS;
Expand Down Expand Up @@ -580,11 +585,19 @@ enum tfm_plat_err_t spu_periph_init_cfg(void)
* - TWISx
* - UARTEx
*/

/* When UART0 is a secure peripheral we need to leave Serial-Box 0 as Secure.
* The UART Driver will configure it as non-secure when it uninitializes.
*/
#if !(defined(SECURE_UART1) && NRF_SECURE_UART_INSTANCE == 0)
spu_peripheral_config_non_secure((uint32_t)NRF_SPIM0, false);
#ifndef SECURE_UART1
/* UART1 is a secure peripheral, so we need to leave Serial-Box 1 as Secure */
#endif

/* When UART0 is a secure peripheral we need to leave Serial-Box 1 as Secure. */
#if !(defined(SECURE_UART1) && NRF_SECURE_UART_INSTANCE == 1)
spu_peripheral_config_non_secure((uint32_t)NRF_SPIM1, false);
#endif

spu_peripheral_config_non_secure((uint32_t)NRF_SPIM2, false);
spu_peripheral_config_non_secure((uint32_t)NRF_SPIM3, false);
spu_peripheral_config_non_secure((uint32_t)NRF_SAADC, false);
Expand Down
4 changes: 4 additions & 0 deletions platform/ext/target/nordic_nrf/common/nrf9160/target_cfg.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,11 @@
#include "tfm_plat_defs.h"
#include "region_defs.h"

#if NRF_SECURE_UART_INSTANCE == 0
#define TFM_DRIVER_STDIO Driver_USART0
#elif NRF_SECURE_UART_INSTANCE == 1
#define TFM_DRIVER_STDIO Driver_USART1
#endif
#define NS_DRIVER_STDIO Driver_USART0

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,12 @@ extern "C" {
#endif

#ifdef SECURE_UART1
#if NRF_SECURE_UART_INSTANCE == 0
#define TFM_PERIPHERAL_UARTE0_SECURE 1
#elif NRF_SECURE_UART_INSTANCE == 1
#define TFM_PERIPHERAL_UARTE1_SECURE 1
#endif
#endif

#if TEST_NS_SLIH_IRQ || TEST_NS_FLIH_IRQ
#define TFM_PERIPHERAL_TIMER0_SECURE 1
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,12 @@ extern "C" {
#endif

#ifdef SECURE_UART1
#if NRF_SECURE_UART_INSTANCE == 0
#define TFM_PERIPHERAL_UARTE0_SECURE 1
#elif NRF_SECURE_UART_INSTANCE == 1
#define TFM_PERIPHERAL_UARTE1_SECURE 1
#endif
#endif

#if TEST_NS_SLIH_IRQ || TEST_NS_FLIH_IRQ
#define TFM_PERIPHERAL_TIMER0_SECURE 1
Expand Down

0 comments on commit 97224b0

Please sign in to comment.