Merge branch 'master' of https://gitlab.denx.de/u-boot/custodians/u-boot-spi
[oweals/u-boot.git] / drivers / serial / serial_bcm283x_mu.c
index 7357bbfb26798ade1fb228140bcfd7f6e25bf052..a6ffc84b963e8468d2691bd5927132f50f68d2b3 100644 (file)
@@ -1,3 +1,4 @@
+// SPDX-License-Identifier: GPL-2.0+
 /*
  * (C) Copyright 2016 Stephen Warren <swarren@wwwdotorg.org>
  *
@@ -9,8 +10,6 @@
  * (C) Copyright 2004
  * ARM Ltd.
  * Philippe Robin, <philippe.robin@arm.com>
- *
- * SPDX-License-Identifier:    GPL-2.0+
  */
 
 /* Simple U-Boot driver for the BCM283x mini UART */
 #include <dm.h>
 #include <errno.h>
 #include <watchdog.h>
+#include <asm/gpio.h>
 #include <asm/io.h>
 #include <serial.h>
 #include <dm/platform_data/serial_bcm283x_mu.h>
+#include <dm/pinctrl.h>
 #include <linux/compiler.h>
-#include <fdtdec.h>
 
 struct bcm283x_mu_regs {
        u32 io;
@@ -50,6 +50,8 @@ struct bcm283x_mu_priv {
        struct bcm283x_mu_regs *regs;
 };
 
+static int bcm283x_mu_serial_getc(struct udevice *dev);
+
 static int bcm283x_mu_serial_setbrg(struct udevice *dev, int baudrate)
 {
        struct bcm283x_mu_serial_platdata *plat = dev_get_platdata(dev);
@@ -58,13 +60,17 @@ static int bcm283x_mu_serial_setbrg(struct udevice *dev, int baudrate)
        u32 divider;
 
        if (plat->skip_init)
-               return 0;
+               goto out;
 
        divider = plat->clock / (baudrate * 8);
 
        writel(BCM283X_MU_LCR_DATA_SIZE_8, &regs->lcr);
        writel(divider - 1, &regs->baud);
 
+out:
+       /* Flush the RX queue - all data in there is bogus */
+       while (bcm283x_mu_serial_getc(dev) != -EAGAIN) ;
+
        return 0;
 }
 
@@ -112,7 +118,9 @@ static int bcm283x_mu_serial_pending(struct udevice *dev, bool input)
 {
        struct bcm283x_mu_priv *priv = dev_get_priv(dev);
        struct bcm283x_mu_regs *regs = priv->regs;
-       unsigned int lsr = readl(&regs->lsr);
+       unsigned int lsr;
+
+       lsr = readl(&regs->lsr);
 
        if (input) {
                WATCHDOG_RESET();
@@ -129,12 +137,70 @@ static const struct dm_serial_ops bcm283x_mu_serial_ops = {
        .setbrg = bcm283x_mu_serial_setbrg,
 };
 
+#if CONFIG_IS_ENABLED(OF_CONTROL)
+static const struct udevice_id bcm283x_mu_serial_id[] = {
+       {.compatible = "brcm,bcm2835-aux-uart"},
+       {}
+};
+
+/*
+ * Check if this serial device is muxed
+ *
+ * The serial device will only work properly if it has been muxed to the serial
+ * pins by firmware. Check whether that happened here.
+ *
+ * @return true if serial device is muxed, false if not
+ */
+static bool bcm283x_is_serial_muxed(void)
+{
+       int serial_gpio = 15;
+       struct udevice *dev;
+
+       if (uclass_first_device(UCLASS_PINCTRL, &dev) || !dev)
+               return false;
+
+       if (pinctrl_get_gpio_mux(dev, 0, serial_gpio) != BCM2835_GPIO_ALT5)
+               return false;
+
+       return true;
+}
+
+static int bcm283x_mu_serial_ofdata_to_platdata(struct udevice *dev)
+{
+       struct bcm283x_mu_serial_platdata *plat = dev_get_platdata(dev);
+       fdt_addr_t addr;
+
+       /* Don't spawn the device if it's not muxed */
+       if (!bcm283x_is_serial_muxed())
+               return -ENODEV;
+
+       addr = devfdt_get_addr(dev);
+       if (addr == FDT_ADDR_T_NONE)
+               return -EINVAL;
+
+       plat->base = addr;
+       plat->clock = dev_read_u32_default(dev, "clock", 1);
+
+       /*
+        * TODO: Reinitialization doesn't always work for now, just skip
+        *       init always - we know we're already initialized
+        */
+       plat->skip_init = true;
+
+       return 0;
+}
+#endif
+
 U_BOOT_DRIVER(serial_bcm283x_mu) = {
        .name = "serial_bcm283x_mu",
        .id = UCLASS_SERIAL,
+       .of_match = of_match_ptr(bcm283x_mu_serial_id),
+       .ofdata_to_platdata = of_match_ptr(bcm283x_mu_serial_ofdata_to_platdata),
        .platdata_auto_alloc_size = sizeof(struct bcm283x_mu_serial_platdata),
        .probe = bcm283x_mu_serial_probe,
        .ops = &bcm283x_mu_serial_ops,
+#if !CONFIG_IS_ENABLED(OF_CONTROL) || CONFIG_IS_ENABLED(OF_BOARD)
        .flags = DM_FLAG_PRE_RELOC,
+#endif
        .priv_auto_alloc_size = sizeof(struct bcm283x_mu_priv),
 };