Platform: Unify NS and secure UART STDOUT implementation

The uart_stdout implementations are actually identical for
Non-secure and Secure. The only difference is the actual
underlying stdio driver.
This patch unifies the implementations to one file, using
Macro to distinguish different drivers.

The patch also removes the unused enum uart_channel.

Change-Id: Ice89127cc98bd185947ce8de7af5190bd5bd69dc
Signed-off-by: Kevin Peng <kevin.peng@arm.com>
diff --git a/app/CMakeLists.txt b/app/CMakeLists.txt
index d8fe183..eadbb54 100644
--- a/app/CMakeLists.txt
+++ b/app/CMakeLists.txt
@@ -167,7 +167,7 @@
 set(BUILD_TARGET_HARDWARE_KEYS Off)
 set(BUILD_TARGET_NV_COUNTERS Off)
 set(BUILD_CMSIS_DRIVERS On)
-set(BUILD_UART_STDOUT Off)
+set(BUILD_UART_STDOUT On)
 set(BUILD_FLASH Off)
 if(CORE_TEST_POSITIVE)
 	set(BUILD_PLAT_TEST On)
diff --git a/app/main_ns.c b/app/main_ns.c
index 0c95a38..65a5242 100644
--- a/app/main_ns.c
+++ b/app/main_ns.c
@@ -26,6 +26,7 @@
 #endif
 #include "log/tfm_assert.h"
 #include "log/tfm_log.h"
+#include "uart_stdout.h"
 
 /**
  * \brief Modified table template for user defined SVC functions
@@ -106,24 +107,6 @@
 }
 #endif
 
-/* For UART the CMSIS driver is used */
-extern ARM_DRIVER_USART NS_DRIVER_STDIO;
-
-int stdio_output_string(const unsigned char *str, uint32_t len)
-{
-    int32_t ret;
-
-    ret = NS_DRIVER_STDIO.Send(str, len);
-    if (ret != ARM_DRIVER_OK) {
-        return 0;
-    }
-    /* Add a busy wait after sending. */
-    while (NS_DRIVER_STDIO.GetStatus().tx_busy)
-        ;
-
-    return NS_DRIVER_STDIO.GetTxCount();
-}
-
 /**
  * \brief Platform peripherals and devices initialization.
  *        Can be overridden for platform specific initialization.
@@ -132,19 +115,7 @@
  */
 __WEAK int32_t tfm_ns_platform_init(void)
 {
-    int32_t ret;
-
-    ret = NS_DRIVER_STDIO.Initialize(NULL);
-    TFM_ASSERT(ret == ARM_DRIVER_OK);
-
-    ret = NS_DRIVER_STDIO.PowerControl(ARM_POWER_FULL);
-    TFM_ASSERT(ret == ARM_DRIVER_OK);
-
-    ret = NS_DRIVER_STDIO.Control(ARM_USART_MODE_ASYNCHRONOUS,
-                                  DEFAULT_UART_BAUDRATE);
-    TFM_ASSERT(ret == ARM_DRIVER_OK);
-
-    (void)NS_DRIVER_STDIO.Control(ARM_USART_CONTROL_TX, 1);
+    stdio_init();
 
     return ARM_DRIVER_OK;
 }
@@ -157,12 +128,7 @@
  */
 __WEAK int32_t tfm_ns_platform_uninit(void)
 {
-    int32_t ret;
-
-    (void)NS_DRIVER_STDIO.PowerControl(ARM_POWER_OFF);
-
-    ret = NS_DRIVER_STDIO.Uninitialize();
-    TFM_ASSERT(ret == ARM_DRIVER_OK);
+    stdio_uninit();
 
     return ARM_DRIVER_OK;
 }
diff --git a/app/uart_stdout.h b/app/uart_stdout.h
deleted file mode 100644
index 7c69c71..0000000
--- a/app/uart_stdout.h
+++ /dev/null
@@ -1,18 +0,0 @@
-/*
- * Copyright (c) 2019, Arm Limited. All rights reserved.
- *
- * SPDX-License-Identifier: BSD-3-Clause
- *
- */
-
-#ifndef __UART_STDOUT_H__
-#define __UART_STDOUT_H__
-
-#include <stdint.h>
-
-/**
- * \brief Output buffer by STDIO.
- */
-int stdio_output_string(const unsigned char *str, uint32_t len);
-
-#endif /* __UART_STDOUT_H__ */
diff --git a/platform/ext/common/uart_stdout.c b/platform/ext/common/uart_stdout.c
index ea892d6..075a2b7 100644
--- a/platform/ext/common/uart_stdout.c
+++ b/platform/ext/common/uart_stdout.c
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2017-2019 ARM Limited
+ * Copyright (c) 2017-2020 ARM Limited
  *
  * Licensed under the Apace License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -18,7 +18,6 @@
 
 #include <assert.h>
 #include <stdio.h>
-#include <stdint.h>
 #include "Driver_USART.h"
 #include "target_cfg.h"
 #include "device_cfg.h"
@@ -26,26 +25,32 @@
 #define ASSERT_HIGH(X)  assert(X == ARM_DRIVER_OK)
 
 /* Imports USART driver */
+#if DOMAIN_NS == 1U
+extern ARM_DRIVER_USART NS_DRIVER_STDIO;
+#define STDIO_DRIVER    NS_DRIVER_STDIO
+#else
 extern ARM_DRIVER_USART TFM_DRIVER_STDIO;
+#define STDIO_DRIVER    TFM_DRIVER_STDIO
+#endif
 
 int stdio_output_string(const unsigned char *str, uint32_t len)
 {
     int32_t ret;
 
-    ret = TFM_DRIVER_STDIO.Send(str, len);
+    ret = STDIO_DRIVER.Send(str, len);
     if (ret != ARM_DRIVER_OK) {
         return 0;
     }
     /* Add a busy wait after sending. */
-    while (TFM_DRIVER_STDIO.GetStatus().tx_busy);
+    while (STDIO_DRIVER.GetStatus().tx_busy);
 
-    return TFM_DRIVER_STDIO.GetTxCount();
+    return STDIO_DRIVER.GetTxCount();
 }
 
-/* Redirects printf to TFM_DRIVER_STDIO in case of ARMCLANG*/
+/* Redirects printf to STDIO_DRIVER in case of ARMCLANG*/
 #if defined(__ARMCC_VERSION)
 /* Struct FILE is implemented in stdio.h. Used to redirect printf to
- * TFM_DRIVER_STDIO
+ * STDIO_DRIVER
  */
 FILE __stdout;
 /* __ARMCC_VERSION is only defined starting from Arm compiler version 6 */
@@ -60,7 +65,7 @@
     return ch;
 }
 #elif defined(__GNUC__)
-/* Redirects printf to TFM_DRIVER_STDIO in case of GNUARM */
+/* Redirects printf to STDIO_DRIVER in case of GNUARM */
 int _write(int fd, char *str, int len)
 {
     (void)fd;
@@ -82,25 +87,25 @@
 void stdio_init(void)
 {
     int32_t ret;
-    ret = TFM_DRIVER_STDIO.Initialize(NULL);
+    ret = STDIO_DRIVER.Initialize(NULL);
     ASSERT_HIGH(ret);
 
-    ret = TFM_DRIVER_STDIO.PowerControl(ARM_POWER_FULL);
+    ret = STDIO_DRIVER.PowerControl(ARM_POWER_FULL);
     ASSERT_HIGH(ret);
 
-    ret = TFM_DRIVER_STDIO.Control(ARM_USART_MODE_ASYNCHRONOUS,
-                                   DEFAULT_UART_BAUDRATE);
+    ret = STDIO_DRIVER.Control(ARM_USART_MODE_ASYNCHRONOUS,
+                               DEFAULT_UART_BAUDRATE);
     ASSERT_HIGH(ret);
 
-    (void)TFM_DRIVER_STDIO.Control(ARM_USART_CONTROL_TX, 1);
+    (void)STDIO_DRIVER.Control(ARM_USART_CONTROL_TX, 1);
 }
 
 void stdio_uninit(void)
 {
     int32_t ret;
 
-    (void)TFM_DRIVER_STDIO.PowerControl(ARM_POWER_OFF);
+    (void)STDIO_DRIVER.PowerControl(ARM_POWER_OFF);
 
-    ret = TFM_DRIVER_STDIO.Uninitialize();
+    ret = STDIO_DRIVER.Uninitialize();
     ASSERT_HIGH(ret);
 }
diff --git a/platform/ext/common/uart_stdout.h b/platform/ext/common/uart_stdout.h
index d967c7c..a725ee4 100644
--- a/platform/ext/common/uart_stdout.h
+++ b/platform/ext/common/uart_stdout.h
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2017-2019 ARM Limited
+ * Copyright (c) 2017-2020 ARM Limited
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -20,16 +20,6 @@
 #include <stdint.h>
 
 /**
- * \brief UART channels that
- *        can be used from TFM
- */
-enum uart_channel {
-    UART0_CHANNEL = 0,
-    UART1_CHANNEL,
-    UART_INVALID
-};
-
-/**
  * \brief Initializes the STDIO.
  *
  */