Skip to content

Commit

Permalink
[nrf toup] platform: nordic_nrf: Add option to initialize hardware to…
Browse files Browse the repository at this point in the history
… reset on boot

Add an option to the nordic platforms to initialize the hardware state
to the its reset values when the TF-M firmware is booting.
The option is useful when the TF-M firmware image is chain-loaded, for
example, a bootloader, and we need to guarantee that the internal
states of the architecture core blocks are restored to the reset values.

Signed-off-by: Joakim Andersson <joakim.andersson@nordicsemi.no>
(cherry picked from commit 9f3911b)
Signed-off-by: Joakim Andersson <joakim.andersson@nordicsemi.no>
  • Loading branch information
Joakim Andersson committed Jun 9, 2022
1 parent 7153239 commit 6be90b8
Show file tree
Hide file tree
Showing 6 changed files with 130 additions and 0 deletions.
12 changes: 12 additions & 0 deletions platform/ext/target/nordic_nrf/common/core/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,18 @@ target_sources(platform_s
plat_test.c
)

if (NRF_HW_INIT_RESET_ON_BOOT)
target_sources(platform_s
PRIVATE
hw_init.c
)

target_compile_definitions(platform_s
PUBLIC
NRF_HW_INIT_RESET_ON_BOOT
)
endif()

if(TFM_PARTITION_PLATFORM)
target_sources(platform_s
PRIVATE
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 @@ -20,3 +20,5 @@ set(NULL_POINTER_EXCEPTION_DETECTION FALSE CACHE BOOL

set(PLATFORM_SLIH_IRQ_TEST_SUPPORT ON CACHE BOOL "Platform supports SLIH IRQ tests")
set(PLATFORM_FLIH_IRQ_TEST_SUPPORT ON CACHE BOOL "Platform supports FLIH IRQ tests")

set(NRF_HW_INIT_RESET_ON_BOOT CACHE BOOL OFF "Initialize internal architecture state at boot")
53 changes: 53 additions & 0 deletions platform/ext/target/nordic_nrf/common/core/hw_init.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*
* Copyright (c) 2022 Nordic Semiconductor ASA
*
* SPDX-License-Identifier: LicenseRef-Nordic-5-Clause
*/

#include <stdint.h>
#include <nrf.h>
#include "array.h"

/* This routine clears all ARM MPU region configuration. */
static void hw_init_mpu(void)
{
const int num_regions =
((MPU->TYPE & MPU_TYPE_DREGION_Msk) >> MPU_TYPE_DREGION_Pos);

for (int i = 0; i < num_regions; i++) {
ARM_MPU_ClrRegion(i);
}
}

/* This routine resets Cortex-M system control block components and core
* registers.
*/
void hw_init_reset_on_boot(void)
{
/* Disable interrupts */
__disable_irq();

/* Reset exception and interrupt mask state (PRIMASK handled by
* __enable_irq below)
*/
__set_FAULTMASK(0);
__set_BASEPRI(0);

/* Clear MPU region configuration */
hw_init_mpu();

/* Disable NVIC interrupts */
for (int i = 0; i < ARRAY_SIZE(NVIC->ICER); i++) {
NVIC->ICER[i] = 0xFFFFFFFF;
}
/* Clear pending NVIC interrupts */
for (int i = 0; i < ARRAY_SIZE(NVIC->ICPR); i++) {
NVIC->ICPR[i] = 0xFFFFFFFF;
}

/* Restore Interrupts */
__enable_irq();

__DSB();
__ISB();
}
13 changes: 13 additions & 0 deletions platform/ext/target/nordic_nrf/common/core/hw_init.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/*
* Copyright (c) 2022 Nordic Semiconductor ASA
*
* SPDX-License-Identifier: LicenseRef-Nordic-5-Clause
*/

#ifndef HW_INIT_H__
#define HW_INIT_H__

#define _SCS_BASE_ADDR 0xE000E000
#define _SCS_MPU_CTRL (_SCS_BASE_ADDR + 0xd94)

#endif /* HW_INIT_H__*/
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
*/

#include "tfm_plat_config.h"
#include "hw_init.h"

.syntax unified
.arch armv8-m.main
Expand Down Expand Up @@ -128,6 +129,30 @@ __Vectors:
.globl Reset_Handler
.type Reset_Handler, %function
Reset_Handler:

/* If the ARM core status registers and system blocks are not in their initial
* reset values then reset these first before booting.
*/
#ifdef NRF_HW_INIT_RESET_ON_BOOT
movs r0, #0

/* Reset CONTROL register */
msr CONTROL, r0
isb

/* Clear SPLIM registers */
msr MSPLIM, r0
msr PSPLIM, r0

/* Disable MPU */
ldr r1, =_SCS_MPU_CTRL
str r0, [r1]
dsb

/* Initialize core architecture registers and system blocks */
bl hw_init_reset_on_boot
#endif /* NRF_HW_INIT_RESET_ON_BOOT */

/* Firstly it copies data from read only memory to RAM. There are two schemes
* to copy. One can copy more than one sections. Another can only copy
* one section. The former scheme needs more instructions and read-only
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
*/

#include "tfm_plat_config.h"
#include "hw_init.h"

.syntax unified
.arch armv8-m.main
Expand Down Expand Up @@ -124,6 +125,30 @@ __Vectors:
.globl Reset_Handler
.type Reset_Handler, %function
Reset_Handler:

/* If the ARM core status registers and system blocks are not in their initial
* reset values then reset these first before booting.
*/
#ifdef NRF_HW_INIT_RESET_ON_BOOT
movs r0, #0

/* Reset CONTROL register */
msr CONTROL, r0
isb

/* Clear SPLIM registers */
msr MSPLIM, r0
msr PSPLIM, r0

/* Disable MPU */
ldr r1, =_SCS_MPU_CTRL
str r0, [r1]
dsb

/* Initialize core architecture registers and system blocks */
bl hw_init_reset_on_boot
#endif /* NRF_HW_INIT_RESET_ON_BOOT */

/* Firstly it copies data from read only memory to RAM. There are two schemes
* to copy. One can copy more than one sections. Another can only copy
* one section. The former scheme needs more instructions and read-only
Expand Down

0 comments on commit 6be90b8

Please sign in to comment.