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 2b98fc3)
  • Loading branch information
joerchan authored and mbolivar-nordic committed Feb 20, 2023
1 parent 19403a8 commit b2346e8
Show file tree
Hide file tree
Showing 10 changed files with 70 additions and 5 deletions.
2 changes: 1 addition & 1 deletion config/config_default.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ set(CONFIG_TFM_LAZY_STACKING OFF CACHE BOOL "Enable/disa

set(CONFIG_TFM_DOORBELL_API ON CACHE BOOL "Enable the doorbell APIs")

set(CONFIG_TFM_LOG_SHARE_UART OFF CACHE BOOL "Allow TF-M and the non-secure application to share the UART instance. TF-M will use ut during its boot, after which the non-secure application will use it until an eventual fatal error is handled and logged by TF-M. Logging from TF-M will therefore otherwise be suppressed")
set(CONFIG_TFM_LOG_SHARE_UART OFF CACHE BOOL "Allow TF-M and the non-secure application to share the UART instance. TF-M will use it while it is booting, after which the non-secure application will use it until an eventual fatal error is handled and logged by TF-M. Logging from TF-M will therefore otherwise be suppressed")
############################ Platform ##########################################

set(TFM_MULTI_CORE_TOPOLOGY OFF CACHE BOOL "Whether to build for a dual-cpu architecture")
Expand Down
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 @@ -109,6 +109,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 @@ -27,3 +27,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_PERIPHERALS_ON_BOOT 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 @@ -614,8 +614,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 @@ -711,11 +716,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 @@ -34,7 +34,11 @@

#include "tfm_plat_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 @@ -33,7 +33,11 @@

#include "tfm_plat_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 b2346e8

Please sign in to comment.