dm: serial: ns16550: Convert to livetree
[oweals/u-boot.git] / drivers / serial / serial-uclass.c
index 55011cc4b9a7e6a36dd5039e5a501c6afc85d86e..200f4b9fd720a780b98f6af3d7fc1319a6a11784 100644 (file)
@@ -29,14 +29,40 @@ static const unsigned long baudrate_table[] = CONFIG_SYS_BAUDRATE_TABLE;
 
 static void serial_find_console_or_panic(void)
 {
+       const void *blob = gd->fdt_blob;
        struct udevice *dev;
        int node;
 
-       if (CONFIG_IS_ENABLED(OF_CONTROL) && gd->fdt_blob) {
+       if (CONFIG_IS_ENABLED(OF_PLATDATA)) {
+               uclass_first_device(UCLASS_SERIAL, &dev);
+               if (dev) {
+                       gd->cur_serial_dev = dev;
+                       return;
+               }
+       } else if (CONFIG_IS_ENABLED(OF_CONTROL) && blob) {
                /* Check for a chosen console */
-               node = fdtdec_get_chosen_node(gd->fdt_blob, "stdout-path");
+               node = fdtdec_get_chosen_node(blob, "stdout-path");
+               if (node < 0) {
+                       const char *str, *p, *name;
+
+                       /*
+                        * Deal with things like
+                        *      stdout-path = "serial0:115200n8";
+                        *
+                        * We need to look up the alias and then follow it to
+                        * the correct node.
+                        */
+                       str = fdtdec_get_chosen_prop(blob, "stdout-path");
+                       if (str) {
+                               p = strchr(str, ':');
+                               name = fdt_get_alias_namelen(blob, str,
+                                               p ? p - str : strlen(str));
+                               if (name)
+                                       node = fdt_path_offset(blob, name);
+                       }
+               }
                if (node < 0)
-                       node = fdt_path_offset(gd->fdt_blob, "console");
+                       node = fdt_path_offset(blob, "console");
                if (!uclass_get_device_by_of_offset(UCLASS_SERIAL, node,
                                                    &dev)) {
                        gd->cur_serial_dev = dev;
@@ -44,26 +70,27 @@ static void serial_find_console_or_panic(void)
                }
 
                /*
-               * If the console is not marked to be bound before relocation,
-               * bind it anyway.
-               */
+                * If the console is not marked to be bound before relocation,
+                * bind it anyway.
+                */
                if (node > 0 &&
-                   !lists_bind_fdt(gd->dm_root, gd->fdt_blob, node, &dev)) {
+                   !lists_bind_fdt(gd->dm_root, offset_to_ofnode(node),
+                                   &dev)) {
                        if (!device_probe(dev)) {
                                gd->cur_serial_dev = dev;
                                return;
                        }
                }
        }
-       if (!SPL_BUILD || !CONFIG_IS_ENABLED(OF_CONTROL) || !gd->fdt_blob) {
+       if (!SPL_BUILD || !CONFIG_IS_ENABLED(OF_CONTROL) || !blob) {
                /*
-               * Try to use CONFIG_CONS_INDEX if available (it is numbered
-               * from 1!).
-               *
-               * Failing that, get the device with sequence number 0, or in
-               * extremis just the first serial device we can find. But we
-               * insist on having a console (even if it is silent).
-               */
+                * Try to use CONFIG_CONS_INDEX if available (it is numbered
+                * from 1!).
+                *
+                * Failing that, get the device with sequence number 0, or in
+                * extremis just the first serial device we can find. But we
+                * insist on having a console (even if it is silent).
+                */
 #ifdef CONFIG_CONS_INDEX
 #define INDEX (CONFIG_CONS_INDEX - 1)
 #else
@@ -95,7 +122,7 @@ int serial_init(void)
 /* Called after relocation */
 void serial_initialize(void)
 {
-       serial_find_console_or_panic();
+       serial_init();
 }
 
 static void _serial_putc(struct udevice *dev, char ch)
@@ -103,11 +130,12 @@ static void _serial_putc(struct udevice *dev, char ch)
        struct dm_serial_ops *ops = serial_get_ops(dev);
        int err;
 
+       if (ch == '\n')
+               _serial_putc(dev, '\r');
+
        do {
                err = ops->putc(dev, ch);
        } while (err == -EAGAIN);
-       if (ch == '\n')
-               _serial_putc(dev, '\r');
 }
 
 static void _serial_puts(struct udevice *dev, const char *str)
@@ -184,27 +212,30 @@ void serial_stdio_init(void)
 {
 }
 
-#ifdef CONFIG_DM_STDIO
+#if defined(CONFIG_DM_STDIO)
+
+#if CONFIG_IS_ENABLED(SERIAL_PRESENT)
 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)
+static void serial_stub_puts(struct stdio_dev *sdev, const char *str)
 {
        _serial_puts(sdev->priv, str);
 }
 
-int serial_stub_getc(struct stdio_dev *sdev)
+static int serial_stub_getc(struct stdio_dev *sdev)
 {
        return _serial_getc(sdev->priv);
 }
 
-int serial_stub_tstc(struct stdio_dev *sdev)
+static int serial_stub_tstc(struct stdio_dev *sdev)
 {
        return _serial_tstc(sdev->priv);
 }
+#endif
 
 /**
  * on_baudrate() - Update the actual baudrate when the env var changes
@@ -267,6 +298,7 @@ static int on_baudrate(const char *name, const char *value, enum env_op op,
 }
 U_BOOT_ENV_CALLBACK(baudrate, on_baudrate);
 
+#if CONFIG_IS_ENABLED(SERIAL_PRESENT)
 static int serial_post_probe(struct udevice *dev)
 {
        struct dm_serial_ops *ops = serial_get_ops(dev);
@@ -318,10 +350,10 @@ static int serial_post_probe(struct udevice *dev)
 
 static int serial_pre_remove(struct udevice *dev)
 {
-#ifdef CONFIG_SYS_STDIO_DEREGISTER
+#if CONFIG_IS_ENABLED(SYS_STDIO_DEREGISTER)
        struct serial_dev_priv *upriv = dev_get_uclass_priv(dev);
 
-       if (stdio_deregister_dev(upriv->sdev, 0))
+       if (stdio_deregister_dev(upriv->sdev, true))
                return -EPERM;
 #endif
 
@@ -336,3 +368,4 @@ UCLASS_DRIVER(serial) = {
        .pre_remove     = serial_pre_remove,
        .per_device_auto_alloc_size = sizeof(struct serial_dev_priv),
 };
+#endif