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 */
23 #include <dm/platform_data/serial_bcm283x_mu.h>
24 #include <dm/pinctrl.h>
25 #include <linux/bitops.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_plat *plat = dev_get_plat(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_getc(struct udevice *dev)
79 struct bcm283x_mu_priv *priv = dev_get_priv(dev);
80 struct bcm283x_mu_regs *regs = priv->regs;
83 /* Wait until there is data in the FIFO */
84 if (!(readl(®s->lsr) & BCM283X_MU_LSR_RX_READY))
87 data = readl(®s->io);
92 static int bcm283x_mu_serial_putc(struct udevice *dev, const char data)
94 struct bcm283x_mu_priv *priv = dev_get_priv(dev);
95 struct bcm283x_mu_regs *regs = priv->regs;
97 /* Wait until there is space in the FIFO */
98 if (!(readl(®s->lsr) & BCM283X_MU_LSR_TX_EMPTY))
101 /* Send the character */
102 writel(data, ®s->io);
107 static int bcm283x_mu_serial_pending(struct udevice *dev, bool input)
109 struct bcm283x_mu_priv *priv = dev_get_priv(dev);
110 struct bcm283x_mu_regs *regs = priv->regs;
113 lsr = readl(®s->lsr);
117 return (lsr & BCM283X_MU_LSR_RX_READY) ? 1 : 0;
119 return (lsr & BCM283X_MU_LSR_TX_IDLE) ? 0 : 1;
123 static const struct dm_serial_ops bcm283x_mu_serial_ops = {
124 .putc = bcm283x_mu_serial_putc,
125 .pending = bcm283x_mu_serial_pending,
126 .getc = bcm283x_mu_serial_getc,
127 .setbrg = bcm283x_mu_serial_setbrg,
130 #if CONFIG_IS_ENABLED(OF_CONTROL)
131 static const struct udevice_id bcm283x_mu_serial_id[] = {
132 {.compatible = "brcm,bcm2835-aux-uart"},
137 * Check if this serial device is muxed
139 * The serial device will only work properly if it has been muxed to the serial
140 * pins by firmware. Check whether that happened here.
142 * Return: true if serial device is muxed, false if not
144 static bool bcm283x_is_serial_muxed(void)
146 int serial_gpio = 15;
149 if (uclass_first_device_err(UCLASS_PINCTRL, &dev))
152 if (pinctrl_get_gpio_mux(dev, 0, serial_gpio) != BCM2835_GPIO_ALT5)
158 static int bcm283x_mu_serial_probe(struct udevice *dev)
160 struct bcm283x_mu_serial_plat *plat = dev_get_plat(dev);
161 struct bcm283x_mu_priv *priv = dev_get_priv(dev);
164 /* Don't spawn the device if it's not muxed */
165 if (!bcm283x_is_serial_muxed())
169 * Read the ofdata here rather than in an of_to_plat() method
170 * since we need the soc simple-bus to be probed so that the 'ranges'
173 addr = dev_read_addr(dev);
174 if (addr == FDT_ADDR_T_NONE)
178 plat->clock = dev_read_u32_default(dev, "clock", 1);
181 * TODO: Reinitialization doesn't always work for now, just skip
182 * init always - we know we're already initialized
184 plat->skip_init = true;
186 priv->regs = (struct bcm283x_mu_regs *)plat->base;
192 U_BOOT_DRIVER(serial_bcm283x_mu) = {
193 .name = "serial_bcm283x_mu",
195 .of_match = of_match_ptr(bcm283x_mu_serial_id),
196 .plat_auto = sizeof(struct bcm283x_mu_serial_plat),
197 .probe = bcm283x_mu_serial_probe,
198 .ops = &bcm283x_mu_serial_ops,
199 #if !CONFIG_IS_ENABLED(OF_CONTROL) || IS_ENABLED(CONFIG_OF_BOARD)
200 .flags = DM_FLAG_PRE_RELOC,
202 .priv_auto = sizeof(struct bcm283x_mu_priv),