3 * Rob Taylor, Flying Pig Systems. robt@flyingpig.com.
7 * Philippe Robin, <philippe.robin@arm.com>
9 * SPDX-License-Identifier: GPL-2.0+
12 /* Simple U-Boot driver for the PrimeCell PL010/PL011 UARTs */
20 #include <dm/platform_data/serial_pl01x.h>
21 #include <linux/compiler.h>
22 #include "serial_pl01x_internal.h"
24 #ifndef CONFIG_DM_SERIAL
26 static volatile unsigned char *const port[] = CONFIG_PL01x_PORTS;
27 static enum pl01x_type pl01x_type __attribute__ ((section(".data")));
28 static struct pl01x_regs *base_regs __attribute__ ((section(".data")));
29 #define NUM_PORTS (sizeof(port)/sizeof(port[0]))
31 DECLARE_GLOBAL_DATA_PTR;
34 static int pl01x_putc(struct pl01x_regs *regs, char c)
36 /* Wait until there is space in the FIFO */
37 if (readl(®s->fr) & UART_PL01x_FR_TXFF)
40 /* Send the character */
46 static int pl01x_getc(struct pl01x_regs *regs)
50 /* Wait until there is data in the FIFO */
51 if (readl(®s->fr) & UART_PL01x_FR_RXFE)
54 data = readl(®s->dr);
56 /* Check for an error flag */
57 if (data & 0xFFFFFF00) {
59 writel(0xFFFFFFFF, ®s->ecr);
66 static int pl01x_tstc(struct pl01x_regs *regs)
69 return !(readl(®s->fr) & UART_PL01x_FR_RXFE);
72 static int pl01x_generic_serial_init(struct pl01x_regs *regs,
77 #ifdef CONFIG_PL011_SERIAL_FLUSH_ON_INIT
78 if (type == TYPE_PL011) {
79 /* Empty RX fifo if necessary */
80 if (readl(®s->pl011_cr) & UART_PL011_CR_UARTEN) {
81 while (!(readl(®s->fr) & UART_PL01x_FR_RXFE))
87 /* First, disable everything */
88 writel(0, ®s->pl010_cr);
90 /* Set the UART to be 8 bits, 1 stop bit, no parity, fifo enabled */
91 lcr = UART_PL011_LCRH_WLEN_8 | UART_PL011_LCRH_FEN;
92 writel(lcr, ®s->pl011_lcrh);
98 #ifdef CONFIG_PL011_SERIAL_RLCR
102 * Program receive line control register after waiting
103 * 10 bus cycles. Delay be writing to readonly register
106 for (i = 0; i < 10; i++)
107 writel(lcr, ®s->fr);
109 writel(lcr, ®s->pl011_rlcr);
110 /* lcrh needs to be set again for change to be effective */
111 writel(lcr, ®s->pl011_lcrh);
122 static int pl01x_generic_setbrg(struct pl01x_regs *regs, enum pl01x_type type,
123 int clock, int baudrate)
128 unsigned int divisor;
132 divisor = UART_PL010_BAUD_9600;
135 divisor = UART_PL010_BAUD_9600;
138 divisor = UART_PL010_BAUD_38400;
141 divisor = UART_PL010_BAUD_57600;
144 divisor = UART_PL010_BAUD_115200;
147 divisor = UART_PL010_BAUD_38400;
150 writel((divisor & 0xf00) >> 8, ®s->pl010_lcrm);
151 writel(divisor & 0xff, ®s->pl010_lcrl);
153 /* Finally, enable the UART */
154 writel(UART_PL010_CR_UARTEN, ®s->pl010_cr);
159 unsigned int divider;
160 unsigned int remainder;
161 unsigned int fraction;
166 * IBRD = UART_CLK / (16 * BAUD_RATE)
167 * FBRD = RND((64 * MOD(UART_CLK,(16 * BAUD_RATE)))
168 * / (16 * BAUD_RATE))
170 temp = 16 * baudrate;
171 divider = clock / temp;
172 remainder = clock % temp;
173 temp = (8 * remainder) / baudrate;
174 fraction = (temp >> 1) + (temp & 1);
176 writel(divider, ®s->pl011_ibrd);
177 writel(fraction, ®s->pl011_fbrd);
180 * Internal update of baud rate register require line
181 * control register write
183 lcr = UART_PL011_LCRH_WLEN_8 | UART_PL011_LCRH_FEN;
184 writel(lcr, ®s->pl011_lcrh);
186 /* Finally, enable the UART */
187 writel(UART_PL011_CR_UARTEN | UART_PL011_CR_TXE |
188 UART_PL011_CR_RXE | UART_PL011_CR_RTS, ®s->pl011_cr);
198 #ifndef CONFIG_DM_SERIAL
199 static void pl01x_serial_init_baud(int baudrate)
203 #if defined(CONFIG_PL010_SERIAL)
204 pl01x_type = TYPE_PL010;
205 #elif defined(CONFIG_PL011_SERIAL)
206 pl01x_type = TYPE_PL011;
207 clock = CONFIG_PL011_CLOCK;
209 base_regs = (struct pl01x_regs *)port[CONFIG_CONS_INDEX];
211 pl01x_generic_serial_init(base_regs, pl01x_type);
212 pl01x_generic_setbrg(base_regs, pl01x_type, clock, baudrate);
216 * Integrator AP has two UARTs, we use the first one, at 38400-8-N-1
217 * Integrator CP has two UARTs, use the first one, at 38400-8-N-1
218 * Versatile PB has four UARTs.
220 int pl01x_serial_init(void)
222 pl01x_serial_init_baud(CONFIG_BAUDRATE);
227 static void pl01x_serial_putc(const char c)
230 while (pl01x_putc(base_regs, '\r') == -EAGAIN);
232 while (pl01x_putc(base_regs, c) == -EAGAIN);
235 static int pl01x_serial_getc(void)
238 int ch = pl01x_getc(base_regs);
249 static int pl01x_serial_tstc(void)
251 return pl01x_tstc(base_regs);
254 static void pl01x_serial_setbrg(void)
257 * Flush FIFO and wait for non-busy before changing baudrate to avoid
260 while (!(readl(&base_regs->fr) & UART_PL01x_FR_TXFE))
262 while (readl(&base_regs->fr) & UART_PL01x_FR_BUSY)
264 pl01x_serial_init_baud(gd->baudrate);
267 static struct serial_device pl01x_serial_drv = {
268 .name = "pl01x_serial",
269 .start = pl01x_serial_init,
271 .setbrg = pl01x_serial_setbrg,
272 .putc = pl01x_serial_putc,
273 .puts = default_serial_puts,
274 .getc = pl01x_serial_getc,
275 .tstc = pl01x_serial_tstc,
278 void pl01x_serial_initialize(void)
280 serial_register(&pl01x_serial_drv);
283 __weak struct serial_device *default_serial_console(void)
285 return &pl01x_serial_drv;
288 #endif /* nCONFIG_DM_SERIAL */
290 #ifdef CONFIG_DM_SERIAL
293 struct pl01x_regs *regs;
294 enum pl01x_type type;
297 static int pl01x_serial_setbrg(struct udevice *dev, int baudrate)
299 struct pl01x_serial_platdata *plat = dev_get_platdata(dev);
300 struct pl01x_priv *priv = dev_get_priv(dev);
302 pl01x_generic_setbrg(priv->regs, priv->type, plat->clock, baudrate);
307 static int pl01x_serial_probe(struct udevice *dev)
309 struct pl01x_serial_platdata *plat = dev_get_platdata(dev);
310 struct pl01x_priv *priv = dev_get_priv(dev);
312 priv->regs = (struct pl01x_regs *)plat->base;
313 priv->type = plat->type;
314 return pl01x_generic_serial_init(priv->regs, priv->type);
317 static int pl01x_serial_getc(struct udevice *dev)
319 struct pl01x_priv *priv = dev_get_priv(dev);
321 return pl01x_getc(priv->regs);
324 static int pl01x_serial_putc(struct udevice *dev, const char ch)
326 struct pl01x_priv *priv = dev_get_priv(dev);
328 return pl01x_putc(priv->regs, ch);
331 static int pl01x_serial_pending(struct udevice *dev, bool input)
333 struct pl01x_priv *priv = dev_get_priv(dev);
334 unsigned int fr = readl(&priv->regs->fr);
337 return pl01x_tstc(priv->regs);
339 return fr & UART_PL01x_FR_TXFF ? 0 : 1;
342 static const struct dm_serial_ops pl01x_serial_ops = {
343 .putc = pl01x_serial_putc,
344 .pending = pl01x_serial_pending,
345 .getc = pl01x_serial_getc,
346 .setbrg = pl01x_serial_setbrg,
349 U_BOOT_DRIVER(serial_pl01x) = {
350 .name = "serial_pl01x",
352 .probe = pl01x_serial_probe,
353 .ops = &pl01x_serial_ops,
354 .flags = DM_FLAG_PRE_RELOC,