Remove the hard-coded baudrate handler and use a callback instead

Signed-off-by: Joe Hershberger <joe.hershber...@ni.com>
---
 common/Makefile        |  2 +-
 common/cmd_nvedit.c    | 35 ------------------------------
 common/serial.c        | 58 ++++++++++++++++++++++++++++++++++++++++++++++++++
 include/env_callback.h |  2 +-
 4 files changed, 60 insertions(+), 37 deletions(-)

diff --git a/common/Makefile b/common/Makefile
index f61c9a1..1187960 100644
--- a/common/Makefile
+++ b/common/Makefile
@@ -32,7 +32,7 @@ COBJS-y += command.o
 COBJS-y += exports.o
 COBJS-$(CONFIG_SYS_HUSH_PARSER) += hush.o
 COBJS-y += s_record.o
-COBJS-$(CONFIG_SERIAL_MULTI) += serial.o
+COBJS-y += serial.o
 COBJS-y += xyzModem.o
 
 # core command
diff --git a/common/cmd_nvedit.c b/common/cmd_nvedit.c
index ac15a3c..652e8ce 100644
--- a/common/cmd_nvedit.c
+++ b/common/cmd_nvedit.c
@@ -47,7 +47,6 @@
 #include <errno.h>
 #include <malloc.h>
 #include <watchdog.h>
-#include <serial.h>
 #include <linux/stddef.h>
 #include <asm/byteorder.h>
 
@@ -77,12 +76,6 @@ SPI_FLASH|NVRAM|MMC|FAT|REMOTE} or CONFIG_ENV_IS_NOWHERE
 #define        MAX_ENV_SIZE    (1 << 20)       /* 1 MiB */
 
 /*
- * Table with supported baudrates (defined in config_xyz.h)
- */
-static const unsigned long baudrate_table[] = CONFIG_SYS_BAUDRATE_TABLE;
-#define        N_BAUDRATES (sizeof(baudrate_table) / sizeof(baudrate_table[0]))
-
-/*
  * This variable is incremented on each do_env_set(), so it can
  * be used via get_env_id() as an indication, if the environment
  * has changed or not. So it is possible to reread an environment
@@ -263,34 +256,6 @@ int _do_env_set(int flag, int argc, char * const argv[])
                        return 1;
                }
 #endif
-               /*
-                * Switch to new baudrate if new baudrate is supported
-                */
-               if (strcmp(name, "baudrate") == 0) {
-                       int baudrate = simple_strtoul(argv[2], NULL, 10);
-                       int i;
-                       for (i = 0; i < N_BAUDRATES; ++i) {
-                               if (baudrate == baudrate_table[i])
-                                       break;
-                       }
-                       if (i == N_BAUDRATES) {
-                               printf("## Baudrate %d bps not supported\n",
-                                       baudrate);
-                               return 1;
-                       }
-                       printf("## Switch baudrate to %d bps and"
-                              "press ENTER ...\n", baudrate);
-                       udelay(50000);
-                       gd->baudrate = baudrate;
-#if defined(CONFIG_PPC) || defined(CONFIG_MCF52x2)
-                       gd->bd->bi_baudrate = baudrate;
-#endif
-
-                       serial_setbrg();
-                       udelay(50000);
-                       while (getc() != '\r')
-                               ;
-               }
        }
 
        /* Delete only ? */
diff --git a/common/serial.c b/common/serial.c
index 75cc1bb..4ff1463 100644
--- a/common/serial.c
+++ b/common/serial.c
@@ -22,6 +22,7 @@
  */
 
 #include <common.h>
+#include <environment.h>
 #include <serial.h>
 #include <stdio_dev.h>
 #include <post.h>
@@ -29,6 +30,62 @@
 
 DECLARE_GLOBAL_DATA_PTR;
 
+/*
+ * Table with supported baudrates (defined in config_xyz.h)
+ */
+static const unsigned long baudrate_table[] = CONFIG_SYS_BAUDRATE_TABLE;
+#define        N_BAUDRATES (sizeof(baudrate_table) / sizeof(baudrate_table[0]))
+
+static int on_baudrate(const char *name, const char *value, enum env_op op)
+{
+       int i;
+       int baudrate;
+
+       switch (op) {
+       case env_op_create:
+       case env_op_overwrite:
+               /*
+                * Switch to new baudrate if new baudrate is supported
+                */
+               baudrate = simple_strtoul(value, NULL, 10);
+
+               /* Not actually changing */
+               if (gd->baudrate == baudrate)
+                       return 0;
+
+               for (i = 0; i < N_BAUDRATES; ++i) {
+                       if (baudrate == baudrate_table[i])
+                               break;
+               }
+               if (i == N_BAUDRATES) {
+                       printf("## Baudrate %d bps not supported\n",
+                               baudrate);
+                       return 1;
+               }
+               printf("## Switch baudrate to %d"
+                      " bps and press ENTER ...\n", baudrate);
+               udelay(50000);
+               gd->baudrate = baudrate;
+#if defined(CONFIG_PPC) || defined(CONFIG_MCF52x2)
+               gd->bd->bi_baudrate = baudrate;
+#endif
+
+               serial_setbrg();
+               udelay(50000);
+               while (getc() != '\r')
+                       ;
+
+               return 0;
+       case env_op_delete:
+               printf("## Baudrate may not be deleted\n");
+               return 1;
+       default:
+               return 0;
+       }
+}
+U_BOOT_ENV_CALLBACK(baudrate, on_baudrate);
+
+#ifdef CONFIG_SERIAL_MULTI
 static struct serial_device *serial_devices;
 static struct serial_device *serial_current;
 
@@ -303,3 +360,4 @@ int uart_post_test(int flags)
        return ret;
 }
 #endif
+#endif /* CONFIG_SERIAL_MULTI */
diff --git a/include/env_callback.h b/include/env_callback.h
index 42c3d91..3f39996 100644
--- a/include/env_callback.h
+++ b/include/env_callback.h
@@ -31,7 +31,7 @@
 #endif
 
 #define ENV_CALLBACK_LIST_STATIC ENV_CALLBACK_VAR ":callbacks," \
-       "loadaddr:loadaddr,bootfile:bootfile," \
+       "loadaddr:loadaddr,bootfile:bootfile,baudrate:baudrate," \
        CONFIG_ENV_CALLBACK_LIST_STATIC
 
 enum env_op {
-- 
1.7.11.5

_______________________________________________
U-Boot mailing list
U-Boot@lists.denx.de
http://lists.denx.de/mailman/listinfo/u-boot

Reply via email to