]> git.kernelconcepts.de Git - karo-tx-uboot.git/blobdiff - drivers/serial/serial-uclass.c
Merge git://git.denx.de/u-boot-dm
[karo-tx-uboot.git] / drivers / serial / serial-uclass.c
index 0f019c8f505bf717fefe0b240d7524d09829f537..b09053f1402bdf55aa3c89abea593ab0bd82d2dd 100644 (file)
@@ -6,6 +6,7 @@
 
 #include <common.h>
 #include <dm.h>
+#include <environment.h>
 #include <errno.h>
 #include <fdtdec.h>
 #include <os.h>
 
 DECLARE_GLOBAL_DATA_PTR;
 
+/*
+ * Table with supported baudrates (defined in config_xyz.h)
+ */
+static const unsigned long baudrate_table[] = CONFIG_SYS_BAUDRATE_TABLE;
+
 #ifndef CONFIG_SYS_MALLOC_F_LEN
 #error "Serial is required before relocation - define CONFIG_SYS_MALLOC_F_LEN to make this work"
 #endif
@@ -160,10 +166,12 @@ void serial_stdio_init(void)
 {
 }
 
+#ifdef CONFIG_DM_STDIO
 static void serial_stub_putc(struct stdio_dev *sdev, const char ch)
 {
        _serial_putc(sdev->priv, ch);
 }
+#endif
 
 void serial_stub_puts(struct stdio_dev *sdev, const char *str)
 {
@@ -180,11 +188,74 @@ int serial_stub_tstc(struct stdio_dev *sdev)
        return _serial_tstc(sdev->priv);
 }
 
+/**
+ * on_baudrate() - Update the actual baudrate when the env var changes
+ *
+ * This will check for a valid baudrate and only apply it if valid.
+ */
+static int on_baudrate(const char *name, const char *value, enum env_op op,
+       int flags)
+{
+       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 < ARRAY_SIZE(baudrate_table); ++i) {
+                       if (baudrate == baudrate_table[i])
+                               break;
+               }
+               if (i == ARRAY_SIZE(baudrate_table)) {
+                       if ((flags & H_FORCE) == 0)
+                               printf("## Baudrate %d bps not supported\n",
+                                      baudrate);
+                       return 1;
+               }
+               if ((flags & H_INTERACTIVE) != 0) {
+                       printf("## Switch baudrate to %d bps and press ENTER ...\n",
+                              baudrate);
+                       udelay(50000);
+               }
+
+               gd->baudrate = baudrate;
+
+               serial_setbrg();
+
+               udelay(50000);
+
+               if ((flags & H_INTERACTIVE) != 0)
+                       while (1) {
+                               if (getc() == '\r')
+                                       break;
+                       }
+
+               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);
+
 static int serial_post_probe(struct udevice *dev)
 {
-       struct stdio_dev sdev;
        struct dm_serial_ops *ops = serial_get_ops(dev);
+#ifdef CONFIG_DM_STDIO
        struct serial_dev_priv *upriv = dev->uclass_priv;
+       struct stdio_dev sdev;
+#endif
        int ret;
 
        /* Set the baud rate */
@@ -194,9 +265,9 @@ static int serial_post_probe(struct udevice *dev)
                        return ret;
        }
 
+#ifdef CONFIG_DM_STDIO
        if (!(gd->flags & GD_FLG_RELOC))
                return 0;
-
        memset(&sdev, '\0', sizeof(sdev));
 
        strncpy(sdev.name, dev->name, sizeof(sdev.name));
@@ -207,7 +278,7 @@ static int serial_post_probe(struct udevice *dev)
        sdev.getc = serial_stub_getc;
        sdev.tstc = serial_stub_tstc;
        stdio_register_dev(&sdev, &upriv->sdev);
-
+#endif
        return 0;
 }