2 * (C) Copyright 2016 Stephen Warren <swarren@wwwdotorg.org>
4 * Derived from pl01x code:
7 * Rob Taylor, Flying Pig Systems. robt@flyingpig.com.
11 * Philippe Robin, <philippe.robin@arm.com>
13 * SPDX-License-Identifier: GPL-2.0+
16 /* Simple U-Boot driver for the BCM283x mini UART */
24 #include <dm/platform_data/serial_bcm283x_mu.h>
25 #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_setbrg(struct udevice *dev, int baudrate)
55 struct bcm283x_mu_serial_platdata *plat = dev_get_platdata(dev);
56 struct bcm283x_mu_priv *priv = dev_get_priv(dev);
57 struct bcm283x_mu_regs *regs = priv->regs;
63 divider = plat->clock / (baudrate * 8);
65 writel(BCM283X_MU_LCR_DATA_SIZE_8, ®s->lcr);
66 writel(divider - 1, ®s->baud);
71 static int bcm283x_mu_serial_probe(struct udevice *dev)
73 struct bcm283x_mu_serial_platdata *plat = dev_get_platdata(dev);
74 struct bcm283x_mu_priv *priv = dev_get_priv(dev);
79 priv->regs = (struct bcm283x_mu_regs *)plat->base;
84 static int bcm283x_mu_serial_getc(struct udevice *dev)
86 struct bcm283x_mu_priv *priv = dev_get_priv(dev);
87 struct bcm283x_mu_regs *regs = priv->regs;
90 /* Wait until there is data in the FIFO */
91 if (!(readl(®s->lsr) & BCM283X_MU_LSR_RX_READY))
94 data = readl(®s->io);
99 static int bcm283x_mu_serial_putc(struct udevice *dev, const char data)
101 struct bcm283x_mu_priv *priv = dev_get_priv(dev);
102 struct bcm283x_mu_regs *regs = priv->regs;
104 /* Wait until there is space in the FIFO */
105 if (!(readl(®s->lsr) & BCM283X_MU_LSR_TX_EMPTY))
108 /* Send the character */
109 writel(data, ®s->io);
114 static int bcm283x_mu_serial_pending(struct udevice *dev, bool input)
116 struct bcm283x_mu_priv *priv = dev_get_priv(dev);
117 struct bcm283x_mu_regs *regs = priv->regs;
118 unsigned int lsr = readl(®s->lsr);
122 return (lsr & BCM283X_MU_LSR_RX_READY) ? 1 : 0;
124 return (lsr & BCM283X_MU_LSR_TX_IDLE) ? 0 : 1;
128 static const struct dm_serial_ops bcm283x_mu_serial_ops = {
129 .putc = bcm283x_mu_serial_putc,
130 .pending = bcm283x_mu_serial_pending,
131 .getc = bcm283x_mu_serial_getc,
132 .setbrg = bcm283x_mu_serial_setbrg,
135 U_BOOT_DRIVER(serial_bcm283x_mu) = {
136 .name = "serial_bcm283x_mu",
138 .platdata_auto_alloc_size = sizeof(struct bcm283x_mu_serial_platdata),
139 .probe = bcm283x_mu_serial_probe,
140 .ops = &bcm283x_mu_serial_ops,
141 .flags = DM_FLAG_PRE_RELOC,
142 .priv_auto_alloc_size = sizeof(struct bcm283x_mu_priv),