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,
75 #ifdef CONFIG_PL011_SERIAL_FLUSH_ON_INIT
76 if (type == TYPE_PL011) {
77 /* Empty RX fifo if necessary */
78 if (readl(®s->pl011_cr) & UART_PL011_CR_UARTEN) {
79 while (!(readl(®s->fr) & UART_PL01x_FR_RXFE))
85 /* First, disable everything */
86 writel(0, ®s->pl010_cr);
100 static int set_line_control(struct pl01x_regs *regs)
104 * Internal update of baud rate register require line
105 * control register write
107 lcr = UART_PL011_LCRH_WLEN_8 | UART_PL011_LCRH_FEN;
108 #ifdef CONFIG_PL011_SERIAL_RLCR
113 * Program receive line control register after waiting
114 * 10 bus cycles. Delay be writing to readonly register
117 for (i = 0; i < 10; i++)
118 writel(lcr, ®s->fr);
120 writel(lcr, ®s->pl011_rlcr);
123 writel(lcr, ®s->pl011_lcrh);
127 static int pl01x_generic_setbrg(struct pl01x_regs *regs, enum pl01x_type type,
128 int clock, int baudrate)
132 unsigned int divisor;
136 divisor = UART_PL010_BAUD_9600;
139 divisor = UART_PL010_BAUD_9600;
142 divisor = UART_PL010_BAUD_38400;
145 divisor = UART_PL010_BAUD_57600;
148 divisor = UART_PL010_BAUD_115200;
151 divisor = UART_PL010_BAUD_38400;
154 writel((divisor & 0xf00) >> 8, ®s->pl010_lcrm);
155 writel(divisor & 0xff, ®s->pl010_lcrl);
157 /* Finally, enable the UART */
158 writel(UART_PL010_CR_UARTEN, ®s->pl010_cr);
163 unsigned int divider;
164 unsigned int remainder;
165 unsigned int fraction;
170 * IBRD = UART_CLK / (16 * BAUD_RATE)
171 * FBRD = RND((64 * MOD(UART_CLK,(16 * BAUD_RATE)))
172 * / (16 * BAUD_RATE))
174 temp = 16 * baudrate;
175 divider = clock / temp;
176 remainder = clock % temp;
177 temp = (8 * remainder) / baudrate;
178 fraction = (temp >> 1) + (temp & 1);
180 writel(divider, ®s->pl011_ibrd);
181 writel(fraction, ®s->pl011_fbrd);
183 set_line_control(regs);
184 /* Finally, enable the UART */
185 writel(UART_PL011_CR_UARTEN | UART_PL011_CR_TXE |
186 UART_PL011_CR_RXE | UART_PL011_CR_RTS, ®s->pl011_cr);
196 #ifndef CONFIG_DM_SERIAL
197 static void pl01x_serial_init_baud(int baudrate)
201 #if defined(CONFIG_PL010_SERIAL)
202 pl01x_type = TYPE_PL010;
203 #elif defined(CONFIG_PL011_SERIAL)
204 pl01x_type = TYPE_PL011;
205 clock = CONFIG_PL011_CLOCK;
207 base_regs = (struct pl01x_regs *)port[CONFIG_CONS_INDEX];
209 pl01x_generic_serial_init(base_regs, pl01x_type);
210 pl01x_generic_setbrg(base_regs, pl01x_type, clock, baudrate);
214 * Integrator AP has two UARTs, we use the first one, at 38400-8-N-1
215 * Integrator CP has two UARTs, use the first one, at 38400-8-N-1
216 * Versatile PB has four UARTs.
218 int pl01x_serial_init(void)
220 pl01x_serial_init_baud(CONFIG_BAUDRATE);
225 static void pl01x_serial_putc(const char c)
228 while (pl01x_putc(base_regs, '\r') == -EAGAIN);
230 while (pl01x_putc(base_regs, c) == -EAGAIN);
233 static int pl01x_serial_getc(void)
236 int ch = pl01x_getc(base_regs);
247 static int pl01x_serial_tstc(void)
249 return pl01x_tstc(base_regs);
252 static void pl01x_serial_setbrg(void)
255 * Flush FIFO and wait for non-busy before changing baudrate to avoid
258 while (!(readl(&base_regs->fr) & UART_PL01x_FR_TXFE))
260 while (readl(&base_regs->fr) & UART_PL01x_FR_BUSY)
262 pl01x_serial_init_baud(gd->baudrate);
265 static struct serial_device pl01x_serial_drv = {
266 .name = "pl01x_serial",
267 .start = pl01x_serial_init,
269 .setbrg = pl01x_serial_setbrg,
270 .putc = pl01x_serial_putc,
271 .puts = default_serial_puts,
272 .getc = pl01x_serial_getc,
273 .tstc = pl01x_serial_tstc,
276 void pl01x_serial_initialize(void)
278 serial_register(&pl01x_serial_drv);
281 __weak struct serial_device *default_serial_console(void)
283 return &pl01x_serial_drv;
286 #endif /* nCONFIG_DM_SERIAL */
288 #ifdef CONFIG_DM_SERIAL
291 struct pl01x_regs *regs;
292 enum pl01x_type type;
295 static int pl01x_serial_setbrg(struct udevice *dev, int baudrate)
297 struct pl01x_serial_platdata *plat = dev_get_platdata(dev);
298 struct pl01x_priv *priv = dev_get_priv(dev);
300 pl01x_generic_setbrg(priv->regs, priv->type, plat->clock, baudrate);
305 static int pl01x_serial_probe(struct udevice *dev)
307 struct pl01x_serial_platdata *plat = dev_get_platdata(dev);
308 struct pl01x_priv *priv = dev_get_priv(dev);
310 priv->regs = (struct pl01x_regs *)plat->base;
311 priv->type = plat->type;
312 return pl01x_generic_serial_init(priv->regs, priv->type);
315 static int pl01x_serial_getc(struct udevice *dev)
317 struct pl01x_priv *priv = dev_get_priv(dev);
319 return pl01x_getc(priv->regs);
322 static int pl01x_serial_putc(struct udevice *dev, const char ch)
324 struct pl01x_priv *priv = dev_get_priv(dev);
326 return pl01x_putc(priv->regs, ch);
329 static int pl01x_serial_pending(struct udevice *dev, bool input)
331 struct pl01x_priv *priv = dev_get_priv(dev);
332 unsigned int fr = readl(&priv->regs->fr);
335 return pl01x_tstc(priv->regs);
337 return fr & UART_PL01x_FR_TXFF ? 0 : 1;
340 static const struct dm_serial_ops pl01x_serial_ops = {
341 .putc = pl01x_serial_putc,
342 .pending = pl01x_serial_pending,
343 .getc = pl01x_serial_getc,
344 .setbrg = pl01x_serial_setbrg,
347 U_BOOT_DRIVER(serial_pl01x) = {
348 .name = "serial_pl01x",
350 .probe = pl01x_serial_probe,
351 .ops = &pl01x_serial_ops,
352 .flags = DM_FLAG_PRE_RELOC,