plat: add NSPE platform-specific hw initialization

 Adding tfm_ns_platform_init() function.

 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.

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

 Implemented as a "weak" functions, it can be overwritten by a platform
 specific implementation.

Signed-off-by: Andrei Narkevitch <ainh@cypress.com>
Change-Id: I16091d35a77ba3e246692cff9df7f31ea082ab27
diff --git a/app/main_ns.c b/app/main_ns.c
index d94ef4e..5168ef2 100644
--- a/app/main_ns.c
+++ b/app/main_ns.c
@@ -21,6 +21,7 @@
 #include "psa_api_test.h"
 #endif
 #include "target_cfg.h"
+#include "tfm_plat_ns.h"
 #include "Driver_USART.h"
 #include "device_cfg.h"
 #ifdef TFM_MULTI_CORE_TOPOLOGY
@@ -141,6 +142,24 @@
 #endif
 
 /**
+ * \brief Platform peripherals and devices initialization.
+ *        Can be overridden for platform specific initialization.
+ *
+ * \return  ARM_DRIVER_OK if the initialization succeeds
+*/
+__WEAK int32_t tfm_ns_platform_init(void)
+{
+    int32_t status;
+
+    status = NS_DRIVER_STDIO.Initialize(NULL);
+    if (status == ARM_DRIVER_OK) {
+        status = NS_DRIVER_STDIO.Control(ARM_USART_MODE_ASYNCHRONOUS,
+                                         DEFAULT_UART_BAUDRATE);
+    }
+    return status;
+}
+
+/**
  * \brief main() function
  */
 #ifndef __GNUC__
@@ -148,9 +167,11 @@
 #endif
 int main(void)
 {
-    (void)NS_DRIVER_STDIO.Initialize(NULL);
-    NS_DRIVER_STDIO.Control(ARM_USART_MODE_ASYNCHRONOUS,
-                            DEFAULT_UART_BAUDRATE);
+
+    if (tfm_ns_platform_init() != ARM_DRIVER_OK) {
+        /* Avoid undefined behavior if platform init failed */
+        while(1);
+    }
 
 #ifdef TFM_MULTI_CORE_TOPOLOGY
     tfm_ns_multi_core_boot();
diff --git a/platform/include/tfm_plat_ns.h b/platform/include/tfm_plat_ns.h
new file mode 100644
index 0000000..3b689b4
--- /dev/null
+++ b/platform/include/tfm_plat_ns.h
@@ -0,0 +1,19 @@
+/*
+ * Copyright (c) 2018-2019, Arm Limited. All rights reserved.
+ *
+ * SPDX-License-Identifier: BSD-3-Clause
+ *
+ */
+
+#ifndef __TFM_PLAT_NS_H__
+#define __TFM_PLAT_NS_H__
+
+/**
+ * \brief Platform peripherals and devices initialization.
+ *
+ * \return  ARM_DRIVER_OK if the initialization succeeds
+*/
+
+int32_t tfm_ns_platform_init(void);
+
+#endif /* __TFM_PLAT_NS_H__ */