Platform: Add SPE platform-specific hw initialization

Adding tfm_spm_hal_post_init() and tfm_spm_hal_post_init_platform()
functions.

Contrarily to SystemInit() intended for a high priority hw
initialization (for example clock and power subsystems), and called
on a very early boot stage from startup code, this function is called
from C code, hence variables and other drivers data are protected from
being cleared up by the C library init.

These functions can be used for initializing platform-specific hw
resources (e.g. IPC, UART) thus freeing up application main() function
from the platform details.

tfm_spm_hal_post_init() function is intended for initialization common
to all platforms, while tfm_spm_hal_post_init_platform() implements
platform-specific initialization.

Signed-off-by: Andrei Narkevitch <ainh@cypress.com>
Change-Id: I7e5c9941c86a1be7fe718d1ebee7427526e8dd83
diff --git a/platform/ext/Mps2AN519.cmake b/platform/ext/Mps2AN519.cmake
index 07f7169..adbb652 100644
--- a/platform/ext/Mps2AN519.cmake
+++ b/platform/ext/Mps2AN519.cmake
@@ -131,6 +131,7 @@
   if (TFM_PARTITION_PLATFORM)
     list(APPEND ALL_SRC_C_S "${PLATFORM_DIR}/target/mps2/an519/services/src/tfm_platform_system.c")
   endif()
+  list(APPEND ALL_SRC_C_S "${PLATFORM_DIR}/common/tfm_platform.c")
   embedded_include_directories(PATH "${PLATFORM_DIR}/common" ABSOLUTE)
 endif()
 
diff --git a/platform/ext/Mps2AN521.cmake b/platform/ext/Mps2AN521.cmake
index abdfc85..62bce16 100644
--- a/platform/ext/Mps2AN521.cmake
+++ b/platform/ext/Mps2AN521.cmake
@@ -132,6 +132,7 @@
   if (TFM_PARTITION_PLATFORM)
     list(APPEND ALL_SRC_C_S "${PLATFORM_DIR}/target/mps2/an521/services/src/tfm_platform_system.c")
   endif()
+  list(APPEND ALL_SRC_C_S "${PLATFORM_DIR}/common/tfm_platform.c")
   embedded_include_directories(PATH "${PLATFORM_DIR}/common" ABSOLUTE)
 endif()
 
diff --git a/platform/ext/Mps2AN539.cmake b/platform/ext/Mps2AN539.cmake
index 325a18a..c2729ea 100644
--- a/platform/ext/Mps2AN539.cmake
+++ b/platform/ext/Mps2AN539.cmake
@@ -128,6 +128,7 @@
   if (TFM_PARTITION_PLATFORM)
     list(APPEND ALL_SRC_C_S "${AN539_DIR}/services/src/tfm_platform_system.c")
   endif()
+  list(APPEND ALL_SRC_C_S "${PLATFORM_DIR}/common/tfm_platform.c")
   embedded_include_directories(PATH "${PLATFORM_DIR}/common" ABSOLUTE)
 endif()
 
diff --git a/platform/ext/Mps3AN524.cmake b/platform/ext/Mps3AN524.cmake
index 103efee..20cd519 100644
--- a/platform/ext/Mps3AN524.cmake
+++ b/platform/ext/Mps3AN524.cmake
@@ -138,6 +138,7 @@
   list(APPEND ALL_SRC_C_S "${AN524_DIR}/attest_hal.c")
   list(APPEND ALL_SRC_C_S "${AN524_DIR}/native_drivers/mpu_armv8m_drv.c")
   list(APPEND ALL_SRC_C_S "${AN524_DIR}/services/src/tfm_platform_system.c")
+  list(APPEND ALL_SRC_C_S "${PLATFORM_DIR}/common/tfm_platform.c")
   embedded_include_directories(PATH "${PLATFORM_DIR}/common" ABSOLUTE)
 endif()
 
diff --git a/platform/ext/common/tfm_platform.c b/platform/ext/common/tfm_platform.c
new file mode 100644
index 0000000..2eeddf5
--- /dev/null
+++ b/platform/ext/common/tfm_platform.c
@@ -0,0 +1,27 @@
+/*
+ * Copyright (c) 2019, Arm Limited. All rights reserved.
+ *
+ * SPDX-License-Identifier: BSD-3-Clause
+ *
+ */
+
+#include "target_cfg.h"
+#include "platform/include/tfm_spm_hal.h"
+
+/* platform-specific hw initialization */
+__WEAK enum tfm_plat_err_t tfm_spm_hal_post_init_platform(void)
+{
+    return TFM_PLAT_ERR_SUCCESS;
+}
+
+enum tfm_plat_err_t tfm_spm_hal_post_init(void)
+{
+    if (tfm_spm_hal_post_init_platform() != TFM_PLAT_ERR_SUCCESS) {
+        return TFM_PLAT_ERR_SYSTEM_ERR;
+    }
+
+    __enable_irq();
+    stdio_init();
+
+    return TFM_PLAT_ERR_SUCCESS;
+}
diff --git a/platform/ext/musca_a.cmake b/platform/ext/musca_a.cmake
index a8f674b..69fdec8 100644
--- a/platform/ext/musca_a.cmake
+++ b/platform/ext/musca_a.cmake
@@ -117,6 +117,7 @@
   list(APPEND ALL_SRC_C_S "${PLATFORM_DIR}/target/musca_a/spm_hal.c")
   list(APPEND ALL_SRC_C_S "${PLATFORM_DIR}/target/musca_a/attest_hal.c")
   list(APPEND ALL_SRC_C_S "${PLATFORM_DIR}/target/musca_a/Native_Driver/mpu_armv8m_drv.c")
+  list(APPEND ALL_SRC_C_S "${PLATFORM_DIR}/common/tfm_platform.c")
   if (TFM_PARTITION_PLATFORM)
     list(APPEND ALL_SRC_C_S "${PLATFORM_DIR}/target/musca_a/services/src/tfm_platform_system.c")
   endif()
diff --git a/platform/ext/musca_b1.cmake b/platform/ext/musca_b1.cmake
index d7b3bda..600c936 100644
--- a/platform/ext/musca_b1.cmake
+++ b/platform/ext/musca_b1.cmake
@@ -139,6 +139,7 @@
         list(APPEND ALL_SRC_C_S "${PLATFORM_DIR}/target/musca_b1/services/src/tfm_platform_system.c")
         list(APPEND ALL_SRC_C_S "${PLATFORM_DIR}/target/musca_b1/services/src/tfm_ioctl_s_api.c")
     endif()
+    list(APPEND ALL_SRC_C_S "${PLATFORM_DIR}/common/tfm_platform.c")
     embedded_include_directories(PATH "${PLATFORM_DIR}/common" ABSOLUTE)
 endif()
 
diff --git a/platform/ext/musca_s1.cmake b/platform/ext/musca_s1.cmake
index a97f39f..abf4185 100644
--- a/platform/ext/musca_s1.cmake
+++ b/platform/ext/musca_s1.cmake
@@ -132,6 +132,7 @@
     if (TFM_PARTITION_PLATFORM)
         list(APPEND ALL_SRC_C_S "${PLATFORM_DIR}/target/musca_s1/services/src/tfm_platform_system.c")
     endif()
+    list(APPEND ALL_SRC_C_S "${PLATFORM_DIR}/common/tfm_platform.c")
     embedded_include_directories(PATH "${PLATFORM_DIR}/common" ABSOLUTE)
 endif()
 
diff --git a/platform/include/tfm_spm_hal.h b/platform/include/tfm_spm_hal.h
index 38f22ff..cd12ae4 100644
--- a/platform/include/tfm_spm_hal.h
+++ b/platform/include/tfm_spm_hal.h
@@ -67,6 +67,32 @@
 #endif
 
 /**
+ * \brief This function initializes peripherals common to all platforms.
+ *
+ * Contrarily to SystemInit() intended for a high-priority hw initialization
+ * (for example clock and power subsystems), and called on a very early boot
+ * stage from startup code, this function is called from C code, hence variables
+ * and other drivers data are protected from being cleared up by the C library
+ * init.
+ * In addition to performing initialization common to all platforms, it also
+ * calls tfm_spm_hal_post_init_platform() function which implements
+ * initialization of platform-specific peripherals and other hw.
+ *
+ * \return Returns values as specified by the \ref tfm_plat_err_t
+ */
+enum tfm_plat_err_t tfm_spm_hal_post_init(void);
+
+/**
+ * \brief This function initializes platform-specific peripherals and hardware.
+ *
+ * Called from tfm_spm_hal_post_init(), this function is intended for
+ * platform-specific portion of hardware initialization.
+ *
+ * \return Returns values as specified by the \ref tfm_plat_err_t
+ */
+enum tfm_plat_err_t tfm_spm_hal_post_init_platform(void);
+
+/**
  * \brief This function initialises the HW used for isolation, and sets the
  *        default configuration for them.
  *