[U-Boot] [PATCH v2] Small refactor to remove duplicate serial code

Simon Glass sjg at chromium.org
Wed Sep 14 00:32:41 CEST 2011


This tidies up duplicate code, and checks that default_serial_console() does
in fact produce a device.

Acked-by: Mike Frysinger <vapier at gentoo.org>

Signed-off-by: Simon Glass <sjg at chromium.org>
---
Changes in v2:
- Rebase to master

 common/serial.c |   61 +++++++++++++++++-------------------------------------
 1 files changed, 19 insertions(+), 42 deletions(-)

diff --git a/common/serial.c b/common/serial.c
index 995d268..2bd8976 100644
--- a/common/serial.c
+++ b/common/serial.c
@@ -158,73 +158,50 @@ void serial_reinit_all (void)
 	}
 }
 
-int serial_init (void)
+static struct serial_device *get_current(void)
 {
+	struct serial_device *dev;
+
 	if (!(gd->flags & GD_FLG_RELOC) || !serial_current) {
-		struct serial_device *dev = default_serial_console ();
+		dev = default_serial_console();
 
-		return dev->init ();
+		/* We must have a console device */
+		if (!dev)
+			panic("Cannot find console");
+	} else {
+		dev = serial_current;
 	}
+	return dev;
+}
 
-	return serial_current->init ();
+int serial_init(void)
+{
+	return get_current()->init();
 }
 
 void serial_setbrg (void)
 {
-	if (!(gd->flags & GD_FLG_RELOC) || !serial_current) {
-		struct serial_device *dev = default_serial_console ();
-
-		dev->setbrg ();
-		return;
-	}
-
-	serial_current->setbrg ();
+	get_current()->setbrg();
 }
 
 int serial_getc (void)
 {
-	if (!(gd->flags & GD_FLG_RELOC) || !serial_current) {
-		struct serial_device *dev = default_serial_console ();
-
-		return dev->getc ();
-	}
-
-	return serial_current->getc ();
+	return get_current()->getc();
 }
 
 int serial_tstc (void)
 {
-	if (!(gd->flags & GD_FLG_RELOC) || !serial_current) {
-		struct serial_device *dev = default_serial_console ();
-
-		return dev->tstc ();
-	}
-
-	return serial_current->tstc ();
+	return get_current()->tstc();
 }
 
 void serial_putc (const char c)
 {
-	if (!(gd->flags & GD_FLG_RELOC) || !serial_current) {
-		struct serial_device *dev = default_serial_console ();
-
-		dev->putc (c);
-		return;
-	}
-
-	serial_current->putc (c);
+	get_current()->putc(c);
 }
 
 void serial_puts (const char *s)
 {
-	if (!(gd->flags & GD_FLG_RELOC) || !serial_current) {
-		struct serial_device *dev = default_serial_console ();
-
-		dev->puts (s);
-		return;
-	}
-
-	serial_current->puts (s);
+	get_current()->puts(s);
 }
 
 #if CONFIG_POST & CONFIG_SYS_POST_UART
-- 
1.7.3.1



More information about the U-Boot mailing list