[U-Boot] [PATCH 1/1] Consolidate strmhz() and init_baudrate() implementation

Jean-Christophe PLAGNIOL-VILLARD plagnioj at jcrosoft.com
Wed Aug 20 00:14:23 CEST 2008


From: Haavard Skinnemoen <haavard.skinnemoen at atmel.com>

ARM, i386, m68k and ppc all have identical implementations of strmhz().
Other architectures don't provide this function at all.

ARM, avr32, blackfin, i386, m68k, mips and sparc all have identical
implementations of init_baudrate().
Other architectures don't provide this function at all.

This patch moves strmhz() and init_baudrate() into lib_generic,
reducing code duplication and providing a more unified API across
architectures.

Signed-off-by: Haavard Skinnemoen <haavard.skinnemoen at atmel.com>
Signed-off-by: Jean-Christophe PLAGNIOL-VILLARD <plagnioj at jcrosoft.com>
---
 include/common.h     |    5 +++-
 lib_arm/board.c      |   25 -----------------------
 lib_avr32/board.c    |   15 -------------
 lib_blackfin/board.c |   10 ---------
 lib_generic/Makefile |    1 +
 lib_generic/board.c  |   54 ++++++++++++++++++++++++++++++++++++++++++++++++++
 lib_i386/board.c     |   25 -----------------------
 lib_m68k/board.c     |   30 ---------------------------
 lib_mips/board.c     |   13 ------------
 lib_ppc/board.c      |   14 -------------
 lib_sparc/board.c    |   13 ------------
 11 files changed, 59 insertions(+), 146 deletions(-)
 create mode 100644 lib_generic/board.c

diff --git a/include/common.h b/include/common.h
index 06ed278..3698788 100644
--- a/include/common.h
+++ b/include/common.h
@@ -224,7 +224,6 @@ void	board_init_r  (gd_t *, ulong) __attribute__ ((noreturn));
 int	checkboard    (void);
 int	checkflash    (void);
 int	checkdram     (void);
-char *	strmhz(char *buf, long hz);
 int	last_stage_init(void);
 extern ulong monitor_flash_len;
 int mac_read_from_eeprom(void);
@@ -615,6 +614,10 @@ int	sprintf(char * buf, const char *fmt, ...)
 		__attribute__ ((format (__printf__, 2, 3)));
 int	vsprintf(char *buf, const char *fmt, va_list args);
 
+/* lib_generic/strmhz.c */
+char *	strmhz(char *buf, long hz);
+int init_baudrate(void);
+
 /* lib_generic/crc32.c */
 uint32_t crc32 (uint32_t, const unsigned char *, uint);
 uint32_t crc32_wd (uint32_t, const unsigned char *, uint, uint);
diff --git a/lib_arm/board.c b/lib_arm/board.c
index a093860..4b48736 100644
--- a/lib_arm/board.c
+++ b/lib_arm/board.c
@@ -115,20 +115,6 @@ void *sbrk (ptrdiff_t increment)
 	return ((void *) old);
 }
 
-char *strmhz(char *buf, long hz)
-{
-	long l, n;
-	long m;
-
-	n = hz / 1000000L;
-	l = sprintf (buf, "%ld", n);
-	m = (hz % 1000000L) / 1000L;
-	if (m != 0)
-		sprintf (buf + l, ".%03ld", m);
-	return (buf);
-}
-
-
 /************************************************************************
  * Coloured LED functionality
  ************************************************************************
@@ -157,17 +143,6 @@ void inline yellow_LED_off(void)__attribute__((weak, alias("__yellow_LED_off")))
  * but let's get it working (again) first...
  */
 
-static int init_baudrate (void)
-{
-	char tmp[64];	/* long enough for environment variables */
-	int i = getenv_r ("baudrate", tmp, sizeof (tmp));
-	gd->bd->bi_baudrate = gd->baudrate = (i > 0)
-			? (int) simple_strtoul (tmp, NULL, 10)
-			: CONFIG_BAUDRATE;
-
-	return (0);
-}
-
 static int display_banner (void)
 {
 	printf ("\n\n%s\n\n", version_string);
diff --git a/lib_avr32/board.c b/lib_avr32/board.c
index f3d0c52..709b9d9 100644
--- a/lib_avr32/board.c
+++ b/lib_avr32/board.c
@@ -120,21 +120,6 @@ static inline void dma_alloc_init(void)
 }
 #endif
 
-static int init_baudrate(void)
-{
-	char tmp[64];
-	int i;
-
-	i = getenv_r("baudrate", tmp, sizeof(tmp));
-	if (i > 0) {
-		gd->baudrate = simple_strtoul(tmp, NULL, 10);
-	} else {
-		gd->baudrate = CONFIG_BAUDRATE;
-	}
-	return 0;
-}
-
-
 static int display_banner (void)
 {
 	printf ("\n\n%s\n\n", version_string);
diff --git a/lib_blackfin/board.c b/lib_blackfin/board.c
index 0b8e664..c51d746 100644
--- a/lib_blackfin/board.c
+++ b/lib_blackfin/board.c
@@ -117,16 +117,6 @@ static int display_banner(void)
 	return 0;
 }
 
-static int init_baudrate(void)
-{
-	char baudrate[15];
-	int i = getenv_r("baudrate", baudrate, sizeof(baudrate));
-	gd->bd->bi_baudrate = gd->baudrate = (i > 0)
-	    ? simple_strtoul(baudrate, NULL, 10)
-	    : CONFIG_BAUDRATE;
-	return 0;
-}
-
 static void display_global_data(void)
 {
 #ifdef CONFIG_DEBUG_EARLY_SERIAL
diff --git a/lib_generic/Makefile b/lib_generic/Makefile
index 4f6ce73..a38550b 100644
--- a/lib_generic/Makefile
+++ b/lib_generic/Makefile
@@ -25,6 +25,7 @@ include $(TOPDIR)/config.mk
 
 LIB	= $(obj)libgeneric.a
 
+COBJS-y	+= board.o
 COBJS-y += bzlib.o
 COBJS-y += bzlib_crctable.o
 COBJS-y += bzlib_decompress.o
diff --git a/lib_generic/board.c b/lib_generic/board.c
new file mode 100644
index 0000000..1970bdb
--- /dev/null
+++ b/lib_generic/board.c
@@ -0,0 +1,54 @@
+/*
+ * (C) Copyright 2002-2006
+ * Wolfgang Denk, DENX Software Engineering, wd at denx.de.
+ *
+ * See file CREDITS for list of people who contributed to this
+ * project.
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License as
+ * published by the Free Software Foundation; either version 2 of
+ * the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
+ * MA 02111-1307 USA
+ */
+#include <common.h>
+
+DECLARE_GLOBAL_DATA_PTR;
+
+char *strmhz (char *buf, long hz)
+{
+	long l, n;
+	long m;
+
+	n = hz / 1000000L;
+	l = sprintf (buf, "%ld", n);
+	m = (hz % 1000000L) / 1000L;
+	if (m != 0)
+		sprintf (buf + l, ".%03ld", m);
+	return (buf);
+}
+
+int init_baudrate(void)
+{
+	char tmp[64];
+	int i;
+
+	i = getenv_r("baudrate", tmp, sizeof(tmp));
+
+	if (i > 0)
+		gd->baudrate = simple_strtoul(tmp, NULL, 10);
+	else
+		gd->baudrate = CONFIG_BAUDRATE;
+
+	return 0;
+}
+
diff --git a/lib_i386/board.c b/lib_i386/board.c
index 22191e6..b145521 100644
--- a/lib_i386/board.c
+++ b/lib_i386/board.c
@@ -108,19 +108,6 @@ void *sbrk (ptrdiff_t increment)
 	return ((void *) old);
 }
 
-char *strmhz (char *buf, long hz)
-{
-	long l, n;
-	long m;
-
-	n = hz / 1000000L;
-	l = sprintf (buf, "%ld", n);
-	m = (hz % 1000000L) / 1000L;
-	if (m != 0)
-		sprintf (buf + l, ".%03ld", m);
-	return (buf);
-}
-
 /************************************************************************
  * Init Utilities							*
  ************************************************************************
@@ -128,18 +115,6 @@ char *strmhz (char *buf, long hz)
  * or dropped completely,
  * but let's get it working (again) first...
  */
-static int init_baudrate (void)
-{
-	char tmp[64];	/* long enough for environment variables */
-	int i = getenv_r("baudrate", tmp, 64);
-
-	gd->baudrate = (i != 0)
-			? (int) simple_strtoul (tmp, NULL, 10)
-			: CONFIG_BAUDRATE;
-
-	return (0);
-}
-
 static int display_banner (void)
 {
 
diff --git a/lib_m68k/board.c b/lib_m68k/board.c
index fe3180e..f4fb4d3 100644
--- a/lib_m68k/board.c
+++ b/lib_m68k/board.c
@@ -140,23 +140,6 @@ void *sbrk (ptrdiff_t increment)
 	return ((void *)old);
 }
 
-char *strmhz(char *buf, long hz)
-{
-	long l, n;
-	long m;
-
-	n = hz / 1000000L;
-
-	l = sprintf (buf, "%ld", n);
-
-	m = (hz % 1000000L) / 1000L;
-
-	if (m != 0)
-		sprintf (buf+l, ".%03ld", m);
-
-	return (buf);
-}
-
 /*
  * All attempts to come up with a "common" initialization sequence
  * that works for all boards and architectures failed: some of the
@@ -178,19 +161,6 @@ typedef int (init_fnc_t) (void);
  * but let's get it working (again) first...
  */
 
-static int init_baudrate (void)
-{
-	char tmp[64];	/* long enough for environment variables */
-	int i = getenv_r ("baudrate", tmp, sizeof (tmp));
-
-	gd->baudrate = (i > 0)
-			? (int) simple_strtoul (tmp, NULL, 10)
-			: CONFIG_BAUDRATE;
-	return (0);
-}
-
-/***********************************************************************/
-
 static int init_func_ram (void)
 {
 	int board_type = 0;	/* use dummy arg */
diff --git a/lib_mips/board.c b/lib_mips/board.c
index fb31e33..a8b21e8 100644
--- a/lib_mips/board.c
+++ b/lib_mips/board.c
@@ -131,19 +131,6 @@ static void display_flash_config(ulong size)
 }
 #endif
 
-static int init_baudrate (void)
-{
-	char tmp[64];	/* long enough for environment variables */
-	int i = getenv_r ("baudrate", tmp, sizeof (tmp));
-
-	gd->baudrate = (i > 0)
-			? (int) simple_strtoul (tmp, NULL, 10)
-			: CONFIG_BAUDRATE;
-
-	return (0);
-}
-
-
 /*
  * Breath some life into the board...
  *
diff --git a/lib_ppc/board.c b/lib_ppc/board.c
index 5816ebd..2aad921 100644
--- a/lib_ppc/board.c
+++ b/lib_ppc/board.c
@@ -167,20 +167,6 @@ void *sbrk (ptrdiff_t increment)
 	mem_malloc_brk = new;
 	return ((void *) old);
 }
-
-char *strmhz (char *buf, long hz)
-{
-	long l, n;
-	long m;
-
-	n = hz / 1000000L;
-	l = sprintf (buf, "%ld", n);
-	m = (hz % 1000000L) / 1000L;
-	if (m != 0)
-		sprintf (buf + l, ".%03ld", m);
-	return (buf);
-}
-
 /*
  * All attempts to come up with a "common" initialization sequence
  * that works for all boards and architectures failed: some of the
diff --git a/lib_sparc/board.c b/lib_sparc/board.c
index 85e3f1f..10ad7bf 100644
--- a/lib_sparc/board.c
+++ b/lib_sparc/board.c
@@ -118,19 +118,6 @@ void *sbrk(ptrdiff_t increment)
  * but let's get it working (again) first...
  */
 
-static int init_baudrate(void)
-{
-	char tmp[64];		/* long enough for environment variables */
-	int i = getenv_r("baudrate", tmp, sizeof(tmp));
-
-	gd->baudrate = (i > 0)
-	    ? (int)simple_strtoul(tmp, NULL, 10)
-	    : CONFIG_BAUDRATE;
-	return (0);
-}
-
-/***********************************************************************/
-
 /*
  * All attempts to come up with a "common" initialization sequence
  * that works for all boards and architectures failed: some of the
-- 
1.5.6.3



More information about the U-Boot mailing list