HAL: Expand the scope of tfm_hal_platform_init API

Several legacy HAL APIs are doing the platform initializations:
 - tfm_spm_hal_enable_fault_handlers
 - tfm_spm_hal_system_reset_cfg
 - tfm_spm_hal_init_debug
 - tfm_spm_hal_nvic_interrupt_target_state_cfg
 - tfm_spm_hal_nvic_interrupt_enable

This patch moves all platform initializations that should be done
*before* SPM initialization into the tfm_hal_platform_init().

Note:
Not all platforms have initializations for the above APIs.

Change-Id: Id0e7221b1eb0e648b25ffa99cf3847beefac93c3
Signed-off-by: Kevin Peng <kevin.peng@arm.com>
diff --git a/docs/technical_references/design_docs/hardware_abstraction_layer.rst b/docs/technical_references/design_docs/hardware_abstraction_layer.rst
index cdff8cc..f843e70 100644
--- a/docs/technical_references/design_docs/hardware_abstraction_layer.rst
+++ b/docs/technical_references/design_docs/hardware_abstraction_layer.rst
@@ -202,10 +202,14 @@
 
 **Description**
 
-This API performs the platform-specific initialization.
+This API performs the platform initializations **before** the :term:`SPM`
+initialization.
 
-This API is called after architecture and platform common initialization has
-finished during system early startup.
+The initializations could include but not limited to:
+- Fault handlers
+- Reset configurations
+- Debug init
+- NVIC init
 
 **Parameter**
 
diff --git a/platform/ext/common/tfm_platform.c b/platform/ext/common/tfm_platform.c
index fd13dc2..3b5860e 100644
--- a/platform/ext/common/tfm_platform.c
+++ b/platform/ext/common/tfm_platform.c
@@ -1,23 +1,13 @@
 /*
- * Copyright (c) 2019-2020, Arm Limited. All rights reserved.
+ * Copyright (c) 2019-2021, Arm Limited. All rights reserved.
  *
  * SPDX-License-Identifier: BSD-3-Clause
  *
  */
 
-#include "target_cfg.h"
-#include "tfm_spm_hal.h"
-#include "uart_stdout.h"
+#include "cmsis.h"
 #include "tfm_hal_platform.h"
 
-__WEAK enum tfm_hal_status_t tfm_hal_platform_init(void)
-{
-    __enable_irq();
-    stdio_init();
-
-    return TFM_HAL_SUCCESS;
-}
-
 __WEAK void tfm_hal_system_reset(void)
 {
     NVIC_SystemReset();
diff --git a/platform/ext/target/arm/mps2/an519/tfm_hal_platform.c b/platform/ext/target/arm/mps2/an519/tfm_hal_platform.c
index 71472ea..4dd7691 100644
--- a/platform/ext/target/arm/mps2/an519/tfm_hal_platform.c
+++ b/platform/ext/target/arm/mps2/an519/tfm_hal_platform.c
@@ -6,13 +6,42 @@
  */
 
 #include "cmsis.h"
+#include "target_cfg.h"
 #include "tfm_hal_platform.h"
+#include "tfm_plat_defs.h"
 #include "uart_stdout.h"
 
 enum tfm_hal_status_t tfm_hal_platform_init(void)
 {
+    enum tfm_plat_err_t plat_err = TFM_PLAT_ERR_SYSTEM_ERR;
+
+    plat_err = enable_fault_handlers();
+    if (plat_err != TFM_PLAT_ERR_SUCCESS) {
+        return TFM_HAL_ERROR_GENERIC;
+    }
+
+    plat_err = system_reset_cfg();
+    if (plat_err != TFM_PLAT_ERR_SUCCESS) {
+        return TFM_HAL_ERROR_GENERIC;
+    }
+
+    plat_err = init_debug();
+    if (plat_err != TFM_PLAT_ERR_SUCCESS) {
+        return TFM_HAL_ERROR_GENERIC;
+    }
+
     __enable_irq();
     stdio_init();
 
+    plat_err = nvic_interrupt_target_state_cfg();
+    if (plat_err != TFM_PLAT_ERR_SUCCESS) {
+        return TFM_HAL_ERROR_GENERIC;
+    }
+
+    plat_err = nvic_interrupt_enable();
+    if (plat_err != TFM_PLAT_ERR_SUCCESS) {
+        return TFM_HAL_ERROR_GENERIC;
+    }
+
     return TFM_HAL_SUCCESS;
 }
diff --git a/platform/ext/target/arm/mps2/an521/target_cfg.h b/platform/ext/target/arm/mps2/an521/target_cfg.h
index 32f9721..1a2feb4 100644
--- a/platform/ext/target/arm/mps2/an521/target_cfg.h
+++ b/platform/ext/target/arm/mps2/an521/target_cfg.h
@@ -135,7 +135,7 @@
 /**
  * \brief Configures the system debug properties.
  *
- * \return Returns values as specified by the \ref fih_int
+ * \return Returns values as specified by the \ref tfm_plat_err_t
  */
 enum tfm_plat_err_t init_debug(void);
 
diff --git a/platform/ext/target/arm/mps2/an521/tfm_hal_platform.c b/platform/ext/target/arm/mps2/an521/tfm_hal_platform.c
index 71472ea..4dd7691 100644
--- a/platform/ext/target/arm/mps2/an521/tfm_hal_platform.c
+++ b/platform/ext/target/arm/mps2/an521/tfm_hal_platform.c
@@ -6,13 +6,42 @@
  */
 
 #include "cmsis.h"
+#include "target_cfg.h"
 #include "tfm_hal_platform.h"
+#include "tfm_plat_defs.h"
 #include "uart_stdout.h"
 
 enum tfm_hal_status_t tfm_hal_platform_init(void)
 {
+    enum tfm_plat_err_t plat_err = TFM_PLAT_ERR_SYSTEM_ERR;
+
+    plat_err = enable_fault_handlers();
+    if (plat_err != TFM_PLAT_ERR_SUCCESS) {
+        return TFM_HAL_ERROR_GENERIC;
+    }
+
+    plat_err = system_reset_cfg();
+    if (plat_err != TFM_PLAT_ERR_SUCCESS) {
+        return TFM_HAL_ERROR_GENERIC;
+    }
+
+    plat_err = init_debug();
+    if (plat_err != TFM_PLAT_ERR_SUCCESS) {
+        return TFM_HAL_ERROR_GENERIC;
+    }
+
     __enable_irq();
     stdio_init();
 
+    plat_err = nvic_interrupt_target_state_cfg();
+    if (plat_err != TFM_PLAT_ERR_SUCCESS) {
+        return TFM_HAL_ERROR_GENERIC;
+    }
+
+    plat_err = nvic_interrupt_enable();
+    if (plat_err != TFM_PLAT_ERR_SUCCESS) {
+        return TFM_HAL_ERROR_GENERIC;
+    }
+
     return TFM_HAL_SUCCESS;
 }
diff --git a/platform/ext/target/arm/mps3/an524/target_cfg.c b/platform/ext/target/arm/mps3/an524/target_cfg.c
index 0648984..2f29dde 100644
--- a/platform/ext/target/arm/mps3/an524/target_cfg.c
+++ b/platform/ext/target/arm/mps3/an524/target_cfg.c
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2019-2020 Arm Limited. All rights reserved.
+ * Copyright (c) 2019-2021 Arm Limited. All rights reserved.
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -24,6 +24,19 @@
 #include "tfm_plat_defs.h"
 #include "region.h"
 
+/* Debug configuration flags */
+#define SPNIDEN_SEL_STATUS (0x01u << 7)
+#define SPNIDEN_STATUS     (0x01u << 6)
+#define SPIDEN_SEL_STATUS  (0x01u << 5)
+#define SPIDEN_STATUS      (0x01u << 4)
+#define NIDEN_SEL_STATUS   (0x01u << 3)
+#define NIDEN_STATUS       (0x01u << 2)
+#define DBGEN_SEL_STATUS   (0x01u << 1)
+#define DBGEN_STATUS       (0x01u << 0)
+
+#define All_SEL_STATUS (SPNIDEN_SEL_STATUS | SPIDEN_SEL_STATUS | \
+                        NIDEN_SEL_STATUS | DBGEN_SEL_STATUS)
+
 /* The section names come from the scatter file */
 REGION_DECLARE(Load$$LR$$, LR_NS_PARTITION, $$Base);
 REGION_DECLARE(Load$$LR$$, LR_VENEER, $$Base);
@@ -158,6 +171,45 @@
     return TFM_PLAT_ERR_SUCCESS;
 }
 
+enum tfm_plat_err_t init_debug(void)
+{
+    volatile struct sysctrl_t *sys_ctrl =
+                                     (struct sysctrl_t *)CMSDK_SYSCTRL_BASE_S;
+
+#if defined(DAUTH_NONE)
+    /* Set all the debug enable selector bits to 1 */
+    sys_ctrl->secdbgset = All_SEL_STATUS;
+    /* Set all the debug enable bits to 0 */
+    sys_ctrl->secdbgclr =
+                   DBGEN_STATUS | NIDEN_STATUS | SPIDEN_STATUS | SPNIDEN_STATUS;
+#elif defined(DAUTH_NS_ONLY)
+    /* Set all the debug enable selector bits to 1 */
+    sys_ctrl->secdbgset = All_SEL_STATUS;
+    /* Set the debug enable bits to 1 for NS, and 0 for S mode */
+    sys_ctrl->secdbgset = DBGEN_STATUS | NIDEN_STATUS;
+    sys_ctrl->secdbgclr = SPIDEN_STATUS | SPNIDEN_STATUS;
+#elif defined(DAUTH_FULL)
+    /* Set all the debug enable selector bits to 1 */
+    sys_ctrl->secdbgset = All_SEL_STATUS;
+    /* Set all the debug enable bits to 1 */
+    sys_ctrl->secdbgset =
+                   DBGEN_STATUS | NIDEN_STATUS | SPIDEN_STATUS | SPNIDEN_STATUS;
+#else
+
+#if !defined(DAUTH_CHIP_DEFAULT)
+#error "No debug authentication setting is provided."
+#endif
+
+    /* Set all the debug enable selector bits to 0 */
+    sys_ctrl->secdbgclr = All_SEL_STATUS;
+
+    /* No need to set any enable bits because the value depends on
+     * input signals.
+     */
+#endif
+    return TFM_PLAT_ERR_SUCCESS;
+}
+
 /*--------------------- NVIC interrupt NS/S configuration --------------------*/
 enum tfm_plat_err_t nvic_interrupt_target_state_cfg(void)
 {
diff --git a/platform/ext/target/arm/mps3/an524/target_cfg.h b/platform/ext/target/arm/mps3/an524/target_cfg.h
index 8246b7a..e43b61b 100644
--- a/platform/ext/target/arm/mps3/an524/target_cfg.h
+++ b/platform/ext/target/arm/mps3/an524/target_cfg.h
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2019-2020 Arm Limited. All rights reserved.
+ * Copyright (c) 2019-2021 Arm Limited. All rights reserved.
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -154,6 +154,13 @@
 enum tfm_plat_err_t system_reset_cfg(void);
 
 /**
+ * \brief Configures the system debug properties.
+ *
+ * \return Returns values as specified by the \ref tfm_plat_err_t
+ */
+enum tfm_plat_err_t init_debug(void);
+
+/**
  * \brief Configures all external interrupts to target the
  *        NS state, apart for the ones associated to secure
  *        peripherals (plus MPC and PPC)
diff --git a/platform/ext/target/arm/mps3/an524/tfm_hal_platform.c b/platform/ext/target/arm/mps3/an524/tfm_hal_platform.c
index 71472ea..4dd7691 100644
--- a/platform/ext/target/arm/mps3/an524/tfm_hal_platform.c
+++ b/platform/ext/target/arm/mps3/an524/tfm_hal_platform.c
@@ -6,13 +6,42 @@
  */
 
 #include "cmsis.h"
+#include "target_cfg.h"
 #include "tfm_hal_platform.h"
+#include "tfm_plat_defs.h"
 #include "uart_stdout.h"
 
 enum tfm_hal_status_t tfm_hal_platform_init(void)
 {
+    enum tfm_plat_err_t plat_err = TFM_PLAT_ERR_SYSTEM_ERR;
+
+    plat_err = enable_fault_handlers();
+    if (plat_err != TFM_PLAT_ERR_SUCCESS) {
+        return TFM_HAL_ERROR_GENERIC;
+    }
+
+    plat_err = system_reset_cfg();
+    if (plat_err != TFM_PLAT_ERR_SUCCESS) {
+        return TFM_HAL_ERROR_GENERIC;
+    }
+
+    plat_err = init_debug();
+    if (plat_err != TFM_PLAT_ERR_SUCCESS) {
+        return TFM_HAL_ERROR_GENERIC;
+    }
+
     __enable_irq();
     stdio_init();
 
+    plat_err = nvic_interrupt_target_state_cfg();
+    if (plat_err != TFM_PLAT_ERR_SUCCESS) {
+        return TFM_HAL_ERROR_GENERIC;
+    }
+
+    plat_err = nvic_interrupt_enable();
+    if (plat_err != TFM_PLAT_ERR_SUCCESS) {
+        return TFM_HAL_ERROR_GENERIC;
+    }
+
     return TFM_HAL_SUCCESS;
 }
diff --git a/platform/ext/target/arm/mps3/an547/tfm_hal_platform.c b/platform/ext/target/arm/mps3/an547/tfm_hal_platform.c
index 71472ea..820acc4 100644
--- a/platform/ext/target/arm/mps3/an547/tfm_hal_platform.c
+++ b/platform/ext/target/arm/mps3/an547/tfm_hal_platform.c
@@ -6,13 +6,42 @@
  */
 
 #include "cmsis.h"
+#include "target_cfg.h"
 #include "tfm_hal_platform.h"
+#include "tfm_peripherals_def.h"
 #include "uart_stdout.h"
 
 enum tfm_hal_status_t tfm_hal_platform_init(void)
 {
+    enum tfm_plat_err_t plat_err = TFM_PLAT_ERR_SYSTEM_ERR;
+
+    plat_err = enable_fault_handlers();
+    if (plat_err != TFM_PLAT_ERR_SUCCESS) {
+        return TFM_HAL_ERROR_GENERIC;
+    }
+
+    plat_err = system_reset_cfg();
+    if (plat_err != TFM_PLAT_ERR_SUCCESS) {
+        return TFM_HAL_ERROR_GENERIC;
+    }
+
+    plat_err = init_debug();
+    if (plat_err != TFM_PLAT_ERR_SUCCESS) {
+        return TFM_HAL_ERROR_GENERIC;
+    }
+
     __enable_irq();
     stdio_init();
 
+    plat_err = nvic_interrupt_target_state_cfg();
+    if (plat_err != TFM_PLAT_ERR_SUCCESS) {
+        return TFM_HAL_ERROR_GENERIC;
+    }
+
+    plat_err = nvic_interrupt_enable();
+    if (plat_err != TFM_PLAT_ERR_SUCCESS) {
+        return TFM_HAL_ERROR_GENERIC;
+    }
+
     return TFM_HAL_SUCCESS;
 }
diff --git a/platform/ext/target/arm/musca_b1/sse_200/spm_hal.c b/platform/ext/target/arm/musca_b1/sse_200/spm_hal.c
index b757402..f88da66 100644
--- a/platform/ext/target/arm/musca_b1/sse_200/spm_hal.c
+++ b/platform/ext/target/arm/musca_b1/sse_200/spm_hal.c
@@ -132,23 +132,6 @@
     return system_reset_cfg();
 }
 
-void tfm_hal_system_reset(void)
-{
-    __disable_irq();
-    mpc_revert_non_secure_to_secure_cfg();
-
-    NVIC->ICPR[0] = UINT32_MAX;         /* Clear all pending interrupts */
-    NVIC->ICPR[1] = UINT32_MAX;         /* Clear all pending interrupts */
-    NVIC->ICPR[2] = UINT32_MAX;         /* Clear all pending interrupts */
-    NVIC->ICPR[3] = UINT32_MAX;         /* Clear all pending interrupts */
-    NVIC->ICPR[4] = UINT32_MAX;         /* Clear all pending interrupts */
-    NVIC->ICPR[5] = UINT32_MAX;         /* Clear all pending interrupts */
-    NVIC->ICPR[6] = UINT32_MAX;         /* Clear all pending interrupts */
-    NVIC->ICPR[7] = UINT32_MAX;         /* Clear all pending interrupts */
-
-    NVIC_SystemReset();
-}
-
 enum tfm_plat_err_t tfm_spm_hal_init_debug(void)
 {
     return init_debug();
diff --git a/platform/ext/target/arm/musca_b1/sse_200/tfm_hal_platform.c b/platform/ext/target/arm/musca_b1/sse_200/tfm_hal_platform.c
index 71472ea..2a6ec34 100644
--- a/platform/ext/target/arm/musca_b1/sse_200/tfm_hal_platform.c
+++ b/platform/ext/target/arm/musca_b1/sse_200/tfm_hal_platform.c
@@ -5,14 +5,62 @@
  *
  */
 
+#include <stdint.h>
+
 #include "cmsis.h"
+#include "target_cfg.h"
 #include "tfm_hal_platform.h"
+#include "tfm_plat_defs.h"
 #include "uart_stdout.h"
 
 enum tfm_hal_status_t tfm_hal_platform_init(void)
 {
+    enum tfm_plat_err_t plat_err = TFM_PLAT_ERR_SYSTEM_ERR;
+
+    plat_err = enable_fault_handlers();
+    if (plat_err != TFM_PLAT_ERR_SUCCESS) {
+        return TFM_HAL_ERROR_GENERIC;
+    }
+
+    plat_err = system_reset_cfg();
+    if (plat_err != TFM_PLAT_ERR_SUCCESS) {
+        return TFM_HAL_ERROR_GENERIC;
+    }
+
+    plat_err = init_debug();
+    if (plat_err != TFM_PLAT_ERR_SUCCESS) {
+        return TFM_HAL_ERROR_GENERIC;
+    }
+
     __enable_irq();
     stdio_init();
 
+    plat_err = nvic_interrupt_target_state_cfg();
+    if (plat_err != TFM_PLAT_ERR_SUCCESS) {
+        return TFM_HAL_ERROR_GENERIC;
+    }
+
+    plat_err = nvic_interrupt_enable();
+    if (plat_err != TFM_PLAT_ERR_SUCCESS) {
+        return TFM_HAL_ERROR_GENERIC;
+    }
+
     return TFM_HAL_SUCCESS;
 }
+
+void tfm_hal_system_reset(void)
+{
+    __disable_irq();
+    mpc_revert_non_secure_to_secure_cfg();
+
+    NVIC->ICPR[0] = UINT32_MAX;         /* Clear all pending interrupts */
+    NVIC->ICPR[1] = UINT32_MAX;         /* Clear all pending interrupts */
+    NVIC->ICPR[2] = UINT32_MAX;         /* Clear all pending interrupts */
+    NVIC->ICPR[3] = UINT32_MAX;         /* Clear all pending interrupts */
+    NVIC->ICPR[4] = UINT32_MAX;         /* Clear all pending interrupts */
+    NVIC->ICPR[5] = UINT32_MAX;         /* Clear all pending interrupts */
+    NVIC->ICPR[6] = UINT32_MAX;         /* Clear all pending interrupts */
+    NVIC->ICPR[7] = UINT32_MAX;         /* Clear all pending interrupts */
+
+    NVIC_SystemReset();
+}
diff --git a/platform/ext/target/arm/musca_s1/target_cfg.c b/platform/ext/target/arm/musca_s1/target_cfg.c
index 0499e8e..4927960 100644
--- a/platform/ext/target/arm/musca_s1/target_cfg.c
+++ b/platform/ext/target/arm/musca_s1/target_cfg.c
@@ -20,7 +20,6 @@
 #include "platform_description.h"
 #include "device_definition.h"
 #include "region_defs.h"
-#include "tfm_hal_platform.h"
 #include "tfm_plat_defs.h"
 #include "region.h"
 #include "cmsis_driver_config.h"
diff --git a/platform/ext/target/arm/musca_s1/tfm_hal_platform.c b/platform/ext/target/arm/musca_s1/tfm_hal_platform.c
index 74425d9..61cf462 100644
--- a/platform/ext/target/arm/musca_s1/tfm_hal_platform.c
+++ b/platform/ext/target/arm/musca_s1/tfm_hal_platform.c
@@ -9,17 +9,46 @@
 #include "cmsis.h"
 #include "cmsis_driver_config.h"
 #include "musca_s1_scc_drv.h"
+#include "target_cfg.h"
 #include "tfm_hal_platform.h"
+#include "tfm_plat_defs.h"
 #include "uart_stdout.h"
 
 enum tfm_hal_status_t tfm_hal_platform_init(void)
 {
+    enum tfm_plat_err_t plat_err = TFM_PLAT_ERR_SYSTEM_ERR;
+
     musca_s1_scc_mram_fast_read_enable(&MUSCA_S1_SCC_DEV);
 
     arm_cache_enable_blocking(&SSE_200_CACHE_DEV);
 
+    plat_err = enable_fault_handlers();
+    if (plat_err != TFM_PLAT_ERR_SUCCESS) {
+        return TFM_HAL_ERROR_GENERIC;
+    }
+
+    plat_err = system_reset_cfg();
+    if (plat_err != TFM_PLAT_ERR_SUCCESS) {
+        return TFM_HAL_ERROR_GENERIC;
+    }
+
+    plat_err = init_debug();
+    if (plat_err != TFM_PLAT_ERR_SUCCESS) {
+        return TFM_HAL_ERROR_GENERIC;
+    }
+
     __enable_irq();
     stdio_init();
 
+    plat_err = nvic_interrupt_target_state_cfg();
+    if (plat_err != TFM_PLAT_ERR_SUCCESS) {
+        return TFM_HAL_ERROR_GENERIC;
+    }
+
+    plat_err = nvic_interrupt_enable();
+    if (plat_err != TFM_PLAT_ERR_SUCCESS) {
+        return TFM_HAL_ERROR_GENERIC;
+    }
+
     return TFM_HAL_SUCCESS;
 }
diff --git a/platform/ext/target/cypress/psoc64/tfm_hal_platform.c b/platform/ext/target/cypress/psoc64/tfm_hal_platform.c
index 432b60d..704dfe9 100644
--- a/platform/ext/target/cypress/psoc64/tfm_hal_platform.c
+++ b/platform/ext/target/cypress/psoc64/tfm_hal_platform.c
@@ -14,6 +14,7 @@
 #include "region_defs.h"
 #include "target_cfg.h"
 #include "tfm_hal_platform.h"
+#include "tfm_plat_defs.h"
 #include "uart_stdout.h"
 
 /* FIXME:
@@ -47,6 +48,8 @@
 
 enum tfm_hal_status_t tfm_hal_platform_init(void)
 {
+    enum tfm_plat_err_t plat_err = TFM_PLAT_ERR_SYSTEM_ERR;
+
     platform_init();
 
 #if defined(CY_DEVICE_SECURE)
@@ -60,5 +63,15 @@
     __enable_irq();
     stdio_init();
 
+    plat_err = nvic_interrupt_target_state_cfg();
+    if (plat_err != TFM_PLAT_ERR_SUCCESS) {
+        return TFM_HAL_ERROR_GENERIC;
+    }
+
+    plat_err = nvic_interrupt_enable();
+    if (plat_err != TFM_PLAT_ERR_SUCCESS) {
+        return TFM_HAL_ERROR_GENERIC;
+    }
+
     return TFM_HAL_SUCCESS;
 }
diff --git a/platform/ext/target/nordic_nrf/common/core/tfm_hal_platform.c b/platform/ext/target/nordic_nrf/common/core/tfm_hal_platform.c
index 71472ea..4dd7691 100644
--- a/platform/ext/target/nordic_nrf/common/core/tfm_hal_platform.c
+++ b/platform/ext/target/nordic_nrf/common/core/tfm_hal_platform.c
@@ -6,13 +6,42 @@
  */
 
 #include "cmsis.h"
+#include "target_cfg.h"
 #include "tfm_hal_platform.h"
+#include "tfm_plat_defs.h"
 #include "uart_stdout.h"
 
 enum tfm_hal_status_t tfm_hal_platform_init(void)
 {
+    enum tfm_plat_err_t plat_err = TFM_PLAT_ERR_SYSTEM_ERR;
+
+    plat_err = enable_fault_handlers();
+    if (plat_err != TFM_PLAT_ERR_SUCCESS) {
+        return TFM_HAL_ERROR_GENERIC;
+    }
+
+    plat_err = system_reset_cfg();
+    if (plat_err != TFM_PLAT_ERR_SUCCESS) {
+        return TFM_HAL_ERROR_GENERIC;
+    }
+
+    plat_err = init_debug();
+    if (plat_err != TFM_PLAT_ERR_SUCCESS) {
+        return TFM_HAL_ERROR_GENERIC;
+    }
+
     __enable_irq();
     stdio_init();
 
+    plat_err = nvic_interrupt_target_state_cfg();
+    if (plat_err != TFM_PLAT_ERR_SUCCESS) {
+        return TFM_HAL_ERROR_GENERIC;
+    }
+
+    plat_err = nvic_interrupt_enable();
+    if (plat_err != TFM_PLAT_ERR_SUCCESS) {
+        return TFM_HAL_ERROR_GENERIC;
+    }
+
     return TFM_HAL_SUCCESS;
 }
diff --git a/platform/ext/target/nuvoton/common/tfm_hal_platform.c b/platform/ext/target/nuvoton/common/tfm_hal_platform.c
index 71472ea..4dd7691 100644
--- a/platform/ext/target/nuvoton/common/tfm_hal_platform.c
+++ b/platform/ext/target/nuvoton/common/tfm_hal_platform.c
@@ -6,13 +6,42 @@
  */
 
 #include "cmsis.h"
+#include "target_cfg.h"
 #include "tfm_hal_platform.h"
+#include "tfm_plat_defs.h"
 #include "uart_stdout.h"
 
 enum tfm_hal_status_t tfm_hal_platform_init(void)
 {
+    enum tfm_plat_err_t plat_err = TFM_PLAT_ERR_SYSTEM_ERR;
+
+    plat_err = enable_fault_handlers();
+    if (plat_err != TFM_PLAT_ERR_SUCCESS) {
+        return TFM_HAL_ERROR_GENERIC;
+    }
+
+    plat_err = system_reset_cfg();
+    if (plat_err != TFM_PLAT_ERR_SUCCESS) {
+        return TFM_HAL_ERROR_GENERIC;
+    }
+
+    plat_err = init_debug();
+    if (plat_err != TFM_PLAT_ERR_SUCCESS) {
+        return TFM_HAL_ERROR_GENERIC;
+    }
+
     __enable_irq();
     stdio_init();
 
+    plat_err = nvic_interrupt_target_state_cfg();
+    if (plat_err != TFM_PLAT_ERR_SUCCESS) {
+        return TFM_HAL_ERROR_GENERIC;
+    }
+
+    plat_err = nvic_interrupt_enable();
+    if (plat_err != TFM_PLAT_ERR_SUCCESS) {
+        return TFM_HAL_ERROR_GENERIC;
+    }
+
     return TFM_HAL_SUCCESS;
 }
diff --git a/platform/ext/target/nxp/common/tfm_hal_platform.c b/platform/ext/target/nxp/common/tfm_hal_platform.c
index 71472ea..4dd7691 100644
--- a/platform/ext/target/nxp/common/tfm_hal_platform.c
+++ b/platform/ext/target/nxp/common/tfm_hal_platform.c
@@ -6,13 +6,42 @@
  */
 
 #include "cmsis.h"
+#include "target_cfg.h"
 #include "tfm_hal_platform.h"
+#include "tfm_plat_defs.h"
 #include "uart_stdout.h"
 
 enum tfm_hal_status_t tfm_hal_platform_init(void)
 {
+    enum tfm_plat_err_t plat_err = TFM_PLAT_ERR_SYSTEM_ERR;
+
+    plat_err = enable_fault_handlers();
+    if (plat_err != TFM_PLAT_ERR_SUCCESS) {
+        return TFM_HAL_ERROR_GENERIC;
+    }
+
+    plat_err = system_reset_cfg();
+    if (plat_err != TFM_PLAT_ERR_SUCCESS) {
+        return TFM_HAL_ERROR_GENERIC;
+    }
+
+    plat_err = init_debug();
+    if (plat_err != TFM_PLAT_ERR_SUCCESS) {
+        return TFM_HAL_ERROR_GENERIC;
+    }
+
     __enable_irq();
     stdio_init();
 
+    plat_err = nvic_interrupt_target_state_cfg();
+    if (plat_err != TFM_PLAT_ERR_SUCCESS) {
+        return TFM_HAL_ERROR_GENERIC;
+    }
+
+    plat_err = nvic_interrupt_enable();
+    if (plat_err != TFM_PLAT_ERR_SUCCESS) {
+        return TFM_HAL_ERROR_GENERIC;
+    }
+
     return TFM_HAL_SUCCESS;
 }
diff --git a/platform/ext/target/stm/common/stm32l5xx/secure/tfm_hal_platform.c b/platform/ext/target/stm/common/stm32l5xx/secure/tfm_hal_platform.c
index 71472ea..47e9252 100644
--- a/platform/ext/target/stm/common/stm32l5xx/secure/tfm_hal_platform.c
+++ b/platform/ext/target/stm/common/stm32l5xx/secure/tfm_hal_platform.c
@@ -6,13 +6,22 @@
  */
 
 #include "cmsis.h"
+#include "target_cfg.h"
 #include "tfm_hal_platform.h"
+#include "tfm_plat_defs.h"
 #include "uart_stdout.h"
 
 enum tfm_hal_status_t tfm_hal_platform_init(void)
 {
+    enum tfm_plat_err_t plat_err = TFM_PLAT_ERR_SYSTEM_ERR;
+
     __enable_irq();
     stdio_init();
 
+    plat_err = nvic_interrupt_target_state_cfg();
+    if (plat_err != TFM_PLAT_ERR_SUCCESS) {
+        return TFM_HAL_ERROR_GENERIC;
+    }
+
     return TFM_HAL_SUCCESS;
 }
diff --git a/platform/ext/target/stm/common/stm32u5xx/secure/tfm_hal_platform.c b/platform/ext/target/stm/common/stm32u5xx/secure/tfm_hal_platform.c
index 71472ea..47e9252 100644
--- a/platform/ext/target/stm/common/stm32u5xx/secure/tfm_hal_platform.c
+++ b/platform/ext/target/stm/common/stm32u5xx/secure/tfm_hal_platform.c
@@ -6,13 +6,22 @@
  */
 
 #include "cmsis.h"
+#include "target_cfg.h"
 #include "tfm_hal_platform.h"
+#include "tfm_plat_defs.h"
 #include "uart_stdout.h"
 
 enum tfm_hal_status_t tfm_hal_platform_init(void)
 {
+    enum tfm_plat_err_t plat_err = TFM_PLAT_ERR_SYSTEM_ERR;
+
     __enable_irq();
     stdio_init();
 
+    plat_err = nvic_interrupt_target_state_cfg();
+    if (plat_err != TFM_PLAT_ERR_SUCCESS) {
+        return TFM_HAL_ERROR_GENERIC;
+    }
+
     return TFM_HAL_SUCCESS;
 }
diff --git a/secure_fw/spm/cmsis_func/main.c b/secure_fw/spm/cmsis_func/main.c
index d8dadbc..a50767f 100644
--- a/secure_fw/spm/cmsis_func/main.c
+++ b/secure_fw/spm/cmsis_func/main.c
@@ -49,31 +49,6 @@
     fih_int fih_rc = FIH_FAILURE;
 #endif
 
-    /* Enables fault handlers */
-    plat_err = tfm_spm_hal_enable_fault_handlers();
-    if (plat_err != TFM_PLAT_ERR_SUCCESS) {
-        FIH_RET(fih_int_encode(TFM_ERROR_GENERIC));
-    }
-
-    /* Configures the system reset request properties */
-    plat_err = tfm_spm_hal_system_reset_cfg();
-    if (plat_err != TFM_PLAT_ERR_SUCCESS) {
-        FIH_RET(fih_int_encode(TFM_ERROR_GENERIC));
-    }
-
-    /* Configures debug authentication */
-#ifdef TFM_FIH_PROFILE_ON
-    FIH_CALL(tfm_spm_hal_init_debug, fih_rc);
-    if (fih_not_eq(fih_rc, fih_int_encode(TFM_PLAT_ERR_SUCCESS))) {
-        FIH_RET(fih_int_encode(TFM_ERROR_GENERIC));
-    }
-#else /* TFM_FIH_PROFILE_ON */
-    plat_err = tfm_spm_hal_init_debug();
-    if (plat_err != TFM_PLAT_ERR_SUCCESS) {
-        return TFM_ERROR_GENERIC;
-    }
-#endif /* TFM_FIH_PROFILE_ON */
-
     /*
      * Access to any peripheral should be performed after programming
      * the necessary security components such as PPC/SAU.
@@ -90,7 +65,6 @@
     }
 #endif /* TFM_FIH_PROFILE_ON */
 
-    /* Performs platform specific initialization */
     hal_status = tfm_hal_platform_init();
     if (hal_status != TFM_HAL_SUCCESS) {
         FIH_RET(fih_int_encode(TFM_ERROR_GENERIC));
@@ -122,14 +96,6 @@
 
     configure_ns_code();
 
-    /* Configures all interrupts to retarget NS state, except for
-     * secure peripherals
-     */
-    plat_err = tfm_spm_hal_nvic_interrupt_target_state_cfg();
-    if (plat_err != TFM_PLAT_ERR_SUCCESS) {
-        FIH_RET(fih_int_encode(TFM_ERROR_GENERIC));
-    }
-
     for (i = 0; i < tfm_core_irq_signals_count; ++i) {
         plat_err = tfm_spm_hal_set_secure_irq_priority(
                                           tfm_core_irq_signals[i].irq_line);
@@ -144,12 +110,6 @@
         }
     }
 
-    /* Enable secure peripherals interrupts */
-    plat_err = tfm_spm_hal_nvic_interrupt_enable();
-    if (plat_err != TFM_PLAT_ERR_SUCCESS) {
-        FIH_RET(fih_int_encode(TFM_ERROR_GENERIC));
-    }
-
     FIH_RET(fih_int_encode(TFM_SUCCESS));
 }
 
diff --git a/secure_fw/spm/cmsis_psa/main.c b/secure_fw/spm/cmsis_psa/main.c
index b8e4ccc..f2eb4d2 100644
--- a/secure_fw/spm/cmsis_psa/main.c
+++ b/secure_fw/spm/cmsis_psa/main.c
@@ -43,31 +43,6 @@
     fih_int fih_rc = FIH_FAILURE;
 #endif
 
-    /* Enables fault handlers */
-    plat_err = tfm_spm_hal_enable_fault_handlers();
-    if (plat_err != TFM_PLAT_ERR_SUCCESS) {
-        FIH_RET(fih_int_encode(TFM_ERROR_GENERIC));
-    }
-
-    /* Configures the system reset request properties */
-    plat_err = tfm_spm_hal_system_reset_cfg();
-    if (plat_err != TFM_PLAT_ERR_SUCCESS) {
-        FIH_RET(fih_int_encode(TFM_ERROR_GENERIC));
-    }
-
-    /* Configures debug authentication */
-#ifdef TFM_FIH_PROFILE_ON
-    FIH_CALL(tfm_spm_hal_init_debug, fih_rc);
-    if (fih_not_eq(fih_rc, fih_int_encode(TFM_PLAT_ERR_SUCCESS))) {
-        FIH_RET(fih_int_encode(TFM_ERROR_GENERIC));
-    }
-#else /* TFM_FIH_PROFILE_ON */
-    plat_err = tfm_spm_hal_init_debug();
-    if (plat_err != TFM_PLAT_ERR_SUCCESS) {
-        return TFM_ERROR_GENERIC;
-    }
-#endif /* TFM_FIH_PROFILE_ON */
-
     /*
      * Access to any peripheral should be performed after programming
      * the necessary security components such as PPC/SAU.
@@ -91,7 +66,6 @@
     }
 #endif
 
-    /* Performs platform specific initialization */
     hal_status = tfm_hal_platform_init();
     if (hal_status != TFM_HAL_SUCCESS) {
         FIH_RET(fih_int_encode(TFM_ERROR_GENERIC));
@@ -123,20 +97,6 @@
 
     configure_ns_code();
 
-    /* Configures all interrupts to retarget NS state, except for
-     * secure peripherals
-     */
-    plat_err = tfm_spm_hal_nvic_interrupt_target_state_cfg();
-    if (plat_err != TFM_PLAT_ERR_SUCCESS) {
-        FIH_RET(fih_int_encode(TFM_ERROR_GENERIC));
-    }
-
-    /* Enable secure peripherals interrupts */
-    plat_err = tfm_spm_hal_nvic_interrupt_enable();
-    if (plat_err != TFM_PLAT_ERR_SUCCESS) {
-        FIH_RET(fih_int_encode(TFM_ERROR_GENERIC));
-    }
-
     FIH_RET(fih_int_encode(TFM_SUCCESS));
 }