1 // SPDX-License-Identifier: GPL-2.0+
3 * (C) Copyright 2016 Stephen Warren <swarren@wwwdotorg.org>
5 * Derived from pl01x code:
8 * Rob Taylor, Flying Pig Systems. robt@flyingpig.com.
12 * Philippe Robin, <philippe.robin@arm.com>
15 /* Simple U-Boot driver for the BCM283x mini UART */
24 #include <dm/platform_data/serial_bcm283x_mu.h>
25 #include <dm/pinctrl.h>
26 #include <linux/compiler.h>
28 struct bcm283x_mu_regs {
42 #define BCM283X_MU_LCR_DATA_SIZE_8 3
44 #define BCM283X_MU_LSR_TX_IDLE BIT(6)
45 /* This actually means not full, but is named not empty in the docs */
46 #define BCM283X_MU_LSR_TX_EMPTY BIT(5)
47 #define BCM283X_MU_LSR_RX_READY BIT(0)
49 struct bcm283x_mu_priv {
50 struct bcm283x_mu_regs *regs;
53 static int bcm283x_mu_serial_getc(struct udevice *dev);
55 static int bcm283x_mu_serial_setbrg(struct udevice *dev, int baudrate)
57 struct bcm283x_mu_serial_platdata *plat = dev_get_platdata(dev);
58 struct bcm283x_mu_priv *priv = dev_get_priv(dev);
59 struct bcm283x_mu_regs *regs = priv->regs;
65 divider = plat->clock / (baudrate * 8);
67 writel(BCM283X_MU_LCR_DATA_SIZE_8, ®s->lcr);
68 writel(divider - 1, ®s->baud);
71 /* Flush the RX queue - all data in there is bogus */
72 while (bcm283x_mu_serial_getc(dev) != -EAGAIN) ;
77 static int bcm283x_mu_serial_probe(struct udevice *dev)
79 struct bcm283x_mu_serial_platdata *plat = dev_get_platdata(dev);
80 struct bcm283x_mu_priv *priv = dev_get_priv(dev);
82 priv->regs = (struct bcm283x_mu_regs *)plat->base;
87 static int bcm283x_mu_serial_getc(struct udevice *dev)
89 struct bcm283x_mu_priv *priv = dev_get_priv(dev);
90 struct bcm283x_mu_regs *regs = priv->regs;
93 /* Wait until there is data in the FIFO */
94 if (!(readl(®s->lsr) & BCM283X_MU_LSR_RX_READY))
97 data = readl(®s->io);
102 static int bcm283x_mu_serial_putc(struct udevice *dev, const char data)
104 struct bcm283x_mu_priv *priv = dev_get_priv(dev);
105 struct bcm283x_mu_regs *regs = priv->regs;
107 /* Wait until there is space in the FIFO */
108 if (!(readl(®s->lsr) & BCM283X_MU_LSR_TX_EMPTY))
111 /* Send the character */
112 writel(data, ®s->io);
117 static int bcm283x_mu_serial_pending(struct udevice *dev, bool input)
119 struct bcm283x_mu_priv *priv = dev_get_priv(dev);
120 struct bcm283x_mu_regs *regs = priv->regs;
123 lsr = readl(®s->lsr);
127 return (lsr & BCM283X_MU_LSR_RX_READY) ? 1 : 0;
129 return (lsr & BCM283X_MU_LSR_TX_IDLE) ? 0 : 1;
133 static const struct dm_serial_ops bcm283x_mu_serial_ops = {
134 .putc = bcm283x_mu_serial_putc,
135 .pending = bcm283x_mu_serial_pending,
136 .getc = bcm283x_mu_serial_getc,
137 .setbrg = bcm283x_mu_serial_setbrg,
140 #if CONFIG_IS_ENABLED(OF_CONTROL)
141 static const struct udevice_id bcm283x_mu_serial_id[] = {
142 {.compatible = "brcm,bcm2835-aux-uart"},
147 * Check if this serial device is muxed
149 * The serial device will only work properly if it has been muxed to the serial
150 * pins by firmware. Check whether that happened here.
152 * @return true if serial device is muxed, false if not
154 static bool bcm283x_is_serial_muxed(void)
156 int serial_gpio = 15;
159 if (uclass_first_device(UCLASS_PINCTRL, &dev) || !dev)
162 if (pinctrl_get_gpio_mux(dev, 0, serial_gpio) != BCM2835_GPIO_ALT5)
168 static int bcm283x_mu_serial_ofdata_to_platdata(struct udevice *dev)
170 struct bcm283x_mu_serial_platdata *plat = dev_get_platdata(dev);
173 /* Don't spawn the device if it's not muxed */
174 if (!bcm283x_is_serial_muxed())
177 addr = devfdt_get_addr(dev);
178 if (addr == FDT_ADDR_T_NONE)
182 plat->clock = dev_read_u32_default(dev, "clock", 1);
185 * TODO: Reinitialization doesn't always work for now, just skip
186 * init always - we know we're already initialized
188 plat->skip_init = true;
194 U_BOOT_DRIVER(serial_bcm283x_mu) = {
195 .name = "serial_bcm283x_mu",
197 .of_match = of_match_ptr(bcm283x_mu_serial_id),
198 .ofdata_to_platdata = of_match_ptr(bcm283x_mu_serial_ofdata_to_platdata),
199 .platdata_auto_alloc_size = sizeof(struct bcm283x_mu_serial_platdata),
200 .probe = bcm283x_mu_serial_probe,
201 .ops = &bcm283x_mu_serial_ops,
202 #if !CONFIG_IS_ENABLED(OF_CONTROL)
203 .flags = DM_FLAG_PRE_RELOC,
205 .priv_auto_alloc_size = sizeof(struct bcm283x_mu_priv),