1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3 * (C) Copyright 2016 Stephen Warren <swarren@wwwdotorg.org>
4 *
5 * Derived from pl01x code:
6 *
7 * (C) Copyright 2000
8 * Rob Taylor, Flying Pig Systems. robt@flyingpig.com.
9 *
10 * (C) Copyright 2004
11 * ARM Ltd.
12 * Philippe Robin, <philippe.robin@arm.com>
13 */
14
15 /* Simple U-Boot driver for the BCM283x mini UART */
16
17 #include <common.h>
18 #include <dm.h>
19 #include <errno.h>
20 #include <watchdog.h>
21 #include <asm/gpio.h>
22 #include <asm/io.h>
23 #include <serial.h>
24 #include <dm/platform_data/serial_bcm283x_mu.h>
25 #include <dm/pinctrl.h>
26 #include <linux/compiler.h>
27
28 struct bcm283x_mu_regs {
29 u32 io;
30 u32 iir;
31 u32 ier;
32 u32 lcr;
33 u32 mcr;
34 u32 lsr;
35 u32 msr;
36 u32 scratch;
37 u32 cntl;
38 u32 stat;
39 u32 baud;
40 };
41
42 #define BCM283X_MU_LCR_DATA_SIZE_8 3
43
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)
48
49 struct bcm283x_mu_priv {
50 struct bcm283x_mu_regs *regs;
51 };
52
53 static int bcm283x_mu_serial_getc(struct udevice *dev);
54
bcm283x_mu_serial_setbrg(struct udevice * dev,int baudrate)55 static int bcm283x_mu_serial_setbrg(struct udevice *dev, int baudrate)
56 {
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;
60 u32 divider;
61
62 if (plat->skip_init)
63 goto out;
64
65 divider = plat->clock / (baudrate * 8);
66
67 writel(BCM283X_MU_LCR_DATA_SIZE_8, ®s->lcr);
68 writel(divider - 1, ®s->baud);
69
70 out:
71 /* Flush the RX queue - all data in there is bogus */
72 while (bcm283x_mu_serial_getc(dev) != -EAGAIN) ;
73
74 return 0;
75 }
76
bcm283x_mu_serial_probe(struct udevice * dev)77 static int bcm283x_mu_serial_probe(struct udevice *dev)
78 {
79 struct bcm283x_mu_serial_platdata *plat = dev_get_platdata(dev);
80 struct bcm283x_mu_priv *priv = dev_get_priv(dev);
81
82 priv->regs = (struct bcm283x_mu_regs *)plat->base;
83
84 return 0;
85 }
86
bcm283x_mu_serial_getc(struct udevice * dev)87 static int bcm283x_mu_serial_getc(struct udevice *dev)
88 {
89 struct bcm283x_mu_priv *priv = dev_get_priv(dev);
90 struct bcm283x_mu_regs *regs = priv->regs;
91 u32 data;
92
93 /* Wait until there is data in the FIFO */
94 if (!(readl(®s->lsr) & BCM283X_MU_LSR_RX_READY))
95 return -EAGAIN;
96
97 data = readl(®s->io);
98
99 return (int)data;
100 }
101
bcm283x_mu_serial_putc(struct udevice * dev,const char data)102 static int bcm283x_mu_serial_putc(struct udevice *dev, const char data)
103 {
104 struct bcm283x_mu_priv *priv = dev_get_priv(dev);
105 struct bcm283x_mu_regs *regs = priv->regs;
106
107 /* Wait until there is space in the FIFO */
108 if (!(readl(®s->lsr) & BCM283X_MU_LSR_TX_EMPTY))
109 return -EAGAIN;
110
111 /* Send the character */
112 writel(data, ®s->io);
113
114 return 0;
115 }
116
bcm283x_mu_serial_pending(struct udevice * dev,bool input)117 static int bcm283x_mu_serial_pending(struct udevice *dev, bool input)
118 {
119 struct bcm283x_mu_priv *priv = dev_get_priv(dev);
120 struct bcm283x_mu_regs *regs = priv->regs;
121 unsigned int lsr;
122
123 lsr = readl(®s->lsr);
124
125 if (input) {
126 WATCHDOG_RESET();
127 return (lsr & BCM283X_MU_LSR_RX_READY) ? 1 : 0;
128 } else {
129 return (lsr & BCM283X_MU_LSR_TX_IDLE) ? 0 : 1;
130 }
131 }
132
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,
138 };
139
140 #if CONFIG_IS_ENABLED(OF_CONTROL)
141 static const struct udevice_id bcm283x_mu_serial_id[] = {
142 {.compatible = "brcm,bcm2835-aux-uart"},
143 {}
144 };
145
146 /*
147 * Check if this serial device is muxed
148 *
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.
151 *
152 * @return true if serial device is muxed, false if not
153 */
bcm283x_is_serial_muxed(void)154 static bool bcm283x_is_serial_muxed(void)
155 {
156 int serial_gpio = 15;
157 struct udevice *dev;
158
159 if (uclass_first_device(UCLASS_PINCTRL, &dev) || !dev)
160 return false;
161
162 if (pinctrl_get_gpio_mux(dev, 0, serial_gpio) != BCM2835_GPIO_ALT5)
163 return false;
164
165 return true;
166 }
167
bcm283x_mu_serial_ofdata_to_platdata(struct udevice * dev)168 static int bcm283x_mu_serial_ofdata_to_platdata(struct udevice *dev)
169 {
170 struct bcm283x_mu_serial_platdata *plat = dev_get_platdata(dev);
171 fdt_addr_t addr;
172
173 /* Don't spawn the device if it's not muxed */
174 if (!bcm283x_is_serial_muxed())
175 return -ENODEV;
176
177 addr = devfdt_get_addr(dev);
178 if (addr == FDT_ADDR_T_NONE)
179 return -EINVAL;
180
181 plat->base = addr;
182 plat->clock = dev_read_u32_default(dev, "clock", 1);
183
184 /*
185 * TODO: Reinitialization doesn't always work for now, just skip
186 * init always - we know we're already initialized
187 */
188 plat->skip_init = true;
189
190 return 0;
191 }
192 #endif
193
194 U_BOOT_DRIVER(serial_bcm283x_mu) = {
195 .name = "serial_bcm283x_mu",
196 .id = UCLASS_SERIAL,
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 .flags = DM_FLAG_PRE_RELOC,
203 .priv_auto_alloc_size = sizeof(struct bcm283x_mu_priv),
204 };
205