[U-Boot] [PATCH 2/3 v2] drivers/serial/serial.c: code maintainability improvments.

Michael Zaidman michael.zaidman at gmail.com
Fri Apr 2 11:10:42 CEST 2010


Signed-off-by: Michael Zaidman <michael.zaidman at gmail.com>
---
 drivers/serial/serial.c |   27 +++++++++++++++++----------
 1 files changed, 17 insertions(+), 10 deletions(-)

diff --git a/drivers/serial/serial.c b/drivers/serial/serial.c
index dd5f332..ba88cb0 100644
--- a/drivers/serial/serial.c
+++ b/drivers/serial/serial.c
@@ -45,24 +45,27 @@ DECLARE_GLOBAL_DATA_PTR;
 #else
 #error	"No console index specified."
 #endif /* CONFIG_SERIAL_MULTI */
-#elif (CONFIG_CONS_INDEX < 1) || (CONFIG_CONS_INDEX > 4)
+#elif CONFIG_CONS_INDEX < 1
 #error	"Invalid console index value."
 #endif
 
-#if CONFIG_CONS_INDEX == 1 && !defined(CONFIG_SYS_NS16550_COM1)
-#error	"Console port 1 defined but not configured."
-#elif CONFIG_CONS_INDEX == 2 && !defined(CONFIG_SYS_NS16550_COM2)
-#error	"Console port 2 defined but not configured."
-#elif CONFIG_CONS_INDEX == 3 && !defined(CONFIG_SYS_NS16550_COM3)
-#error	"Console port 3 defined but not configured."
-#elif CONFIG_CONS_INDEX == 4 && !defined(CONFIG_SYS_NS16550_COM4)
-#error	"Console port 4 defined but not configured."
+#define CONSOLE_PORT_SANITY_CHECK(port) \
+	CONFIG_CONS_INDEX == port && !defined(CONFIG_SYS_NS16550_COM##port)
+
+#if	CONSOLE_PORT_SANITY_CHECK(1)
+#error	Console port 1 defined but not configured.
+#elif	CONSOLE_PORT_SANITY_CHECK(2)
+#error	Console port 2 defined but not configured.
+#elif	CONSOLE_PORT_SANITY_CHECK(3)
+#error	Console port 3 defined but not configured.
+#elif	CONSOLE_PORT_SANITY_CHECK(4)
+#error	Console port 4 defined but not configured.
 #endif
 
 /* Note: The port number specified in the functions is 1 based.
  *	 the array is 0 based.
  */
-static NS16550_t serial_ports[4] = {
+static NS16550_t serial_ports[] = {
 #ifdef CONFIG_SYS_NS16550_COM1
 	(NS16550_t)CONFIG_SYS_NS16550_COM1,
 #else
@@ -85,6 +88,8 @@ static NS16550_t serial_ports[4] = {
 #endif
 };
 
+#define MAX_SER_PORTS (sizeof(serial_ports) / sizeof(NS16550_t))
+
 #define PORT	serial_ports[port-1]
 #if defined(CONFIG_CONS_INDEX)
 #define CONSOLE	(serial_ports[CONFIG_CONS_INDEX-1])
@@ -160,6 +165,8 @@ int serial_init (void)
 {
 	int clock_divisor;
 
+	BUILD_BUG_ON(CONFIG_CONS_INDEX > MAX_SER_PORTS);
+
 #ifdef CONFIG_NS87308
 	initialise_ns87308();
 #endif
-- 
1.6.3.3



More information about the U-Boot mailing list