blob: 2124161734c0a40f4baf1a86de933be307e4e467 [file] [log] [blame]
wdenk4989f872004-03-14 15:06:13 +00001/*
2 * (C) Copyright 2000
3 * Rob Taylor, Flying Pig Systems. robt@flyingpig.com.
4 *
5 * (C) Copyright 2004
6 * ARM Ltd.
7 * Philippe Robin, <philippe.robin@arm.com>
8 *
Wolfgang Denkd79de1d2013-07-08 09:37:19 +02009 * SPDX-License-Identifier: GPL-2.0+
wdenk4989f872004-03-14 15:06:13 +000010 */
11
Andreas Engel0813b122008-09-08 14:30:53 +020012/* Simple U-Boot driver for the PrimeCell PL010/PL011 UARTs */
wdenk4989f872004-03-14 15:06:13 +000013
14#include <common.h>
Simon Glass3ad93fe2014-09-22 17:30:58 -060015#include <dm.h>
Simon Glassf35484d2014-09-22 17:30:57 -060016#include <errno.h>
Stuart Wood26136ef2008-06-02 16:42:19 -040017#include <watchdog.h>
Matt Waddeld6ce53e2010-10-07 15:48:46 -060018#include <asm/io.h>
Marek Vasut46e4d5f2012-09-14 22:38:46 +020019#include <serial.h>
Masahiro Yamada22c97de2014-10-24 12:41:19 +090020#include <dm/platform_data/serial_pl01x.h>
Marek Vasut46e4d5f2012-09-14 22:38:46 +020021#include <linux/compiler.h>
Simon Glassf35484d2014-09-22 17:30:57 -060022#include "serial_pl01x_internal.h"
wdenk4989f872004-03-14 15:06:13 +000023
Simon Glass3ad93fe2014-09-22 17:30:58 -060024#ifndef CONFIG_DM_SERIAL
25
wdenkda04a8b2004-08-02 23:22:59 +000026static volatile unsigned char *const port[] = CONFIG_PL01x_PORTS;
Simon Glassf35484d2014-09-22 17:30:57 -060027static enum pl01x_type pl01x_type __attribute__ ((section(".data")));
28static struct pl01x_regs *base_regs __attribute__ ((section(".data")));
wdenkda04a8b2004-08-02 23:22:59 +000029#define NUM_PORTS (sizeof(port)/sizeof(port[0]))
wdenk4989f872004-03-14 15:06:13 +000030
Matt Waddeld6ce53e2010-10-07 15:48:46 -060031DECLARE_GLOBAL_DATA_PTR;
Simon Glass3ad93fe2014-09-22 17:30:58 -060032#endif
wdenk4989f872004-03-14 15:06:13 +000033
Simon Glassf35484d2014-09-22 17:30:57 -060034static int pl01x_putc(struct pl01x_regs *regs, char c)
wdenk4989f872004-03-14 15:06:13 +000035{
Simon Glassf35484d2014-09-22 17:30:57 -060036 /* Wait until there is space in the FIFO */
37 if (readl(&regs->fr) & UART_PL01x_FR_TXFF)
38 return -EAGAIN;
wdenk4989f872004-03-14 15:06:13 +000039
Simon Glassf35484d2014-09-22 17:30:57 -060040 /* Send the character */
41 writel(c, &regs->dr);
wdenk4989f872004-03-14 15:06:13 +000042
Simon Glassf35484d2014-09-22 17:30:57 -060043 return 0;
44}
wdenk4989f872004-03-14 15:06:13 +000045
Simon Glassf35484d2014-09-22 17:30:57 -060046static int pl01x_getc(struct pl01x_regs *regs)
47{
48 unsigned int data;
wdenk4989f872004-03-14 15:06:13 +000049
Simon Glassf35484d2014-09-22 17:30:57 -060050 /* Wait until there is data in the FIFO */
51 if (readl(&regs->fr) & UART_PL01x_FR_RXFE)
52 return -EAGAIN;
wdenk4989f872004-03-14 15:06:13 +000053
Simon Glassf35484d2014-09-22 17:30:57 -060054 data = readl(&regs->dr);
wdenk4989f872004-03-14 15:06:13 +000055
Simon Glassf35484d2014-09-22 17:30:57 -060056 /* Check for an error flag */
57 if (data & 0xFFFFFF00) {
58 /* Clear the error */
59 writel(0xFFFFFFFF, &regs->ecr);
60 return -1;
wdenkc35ba4e2004-03-14 22:25:36 +000061 }
62
Simon Glassf35484d2014-09-22 17:30:57 -060063 return (int) data;
wdenk4989f872004-03-14 15:06:13 +000064}
65
Simon Glassf35484d2014-09-22 17:30:57 -060066static int pl01x_tstc(struct pl01x_regs *regs)
67{
68 WATCHDOG_RESET();
69 return !(readl(&regs->fr) & UART_PL01x_FR_RXFE);
70}
Andreas Engel80438612008-09-08 10:17:31 +020071
Simon Glassf35484d2014-09-22 17:30:57 -060072static int pl01x_generic_serial_init(struct pl01x_regs *regs,
73 enum pl01x_type type)
Andreas Engel80438612008-09-08 10:17:31 +020074{
Vikas Manochabe14f152014-11-21 10:34:23 -080075 switch (type) {
76 case TYPE_PL010:
77 /* disable everything */
78 writel(0, &regs->pl010_cr);
79 break;
80 case TYPE_PL011:
John Rigby34e21ee2011-04-19 10:42:39 +000081#ifdef CONFIG_PL011_SERIAL_FLUSH_ON_INIT
Simon Glassf35484d2014-09-22 17:30:57 -060082 /* Empty RX fifo if necessary */
83 if (readl(&regs->pl011_cr) & UART_PL011_CR_UARTEN) {
84 while (!(readl(&regs->fr) & UART_PL01x_FR_RXFE))
85 readl(&regs->dr);
86 }
John Rigby34e21ee2011-04-19 10:42:39 +000087#endif
Vikas Manochaee038e22014-11-21 10:34:22 -080088 /* disable everything */
89 writel(0, &regs->pl011_cr);
Vikas Manochafe96bbd2014-11-21 10:34:21 -080090 break;
91 default:
92 return -EINVAL;
93 }
94
95 return 0;
96}
97
Linus Walleij70864f62015-04-21 15:10:06 +020098static int pl011_set_line_control(struct pl01x_regs *regs)
Vikas Manochafe96bbd2014-11-21 10:34:21 -080099{
100 unsigned int lcr;
101 /*
102 * Internal update of baud rate register require line
103 * control register write
104 */
105 lcr = UART_PL011_LCRH_WLEN_8 | UART_PL011_LCRH_FEN;
John Rigby34e21ee2011-04-19 10:42:39 +0000106#ifdef CONFIG_PL011_SERIAL_RLCR
Vikas Manochafe96bbd2014-11-21 10:34:21 -0800107 {
John Rigby34e21ee2011-04-19 10:42:39 +0000108 int i;
109
110 /*
111 * Program receive line control register after waiting
112 * 10 bus cycles. Delay be writing to readonly register
113 * 10 times
114 */
115 for (i = 0; i < 10; i++)
116 writel(lcr, &regs->fr);
Andreas Engel80438612008-09-08 10:17:31 +0200117
John Rigby34e21ee2011-04-19 10:42:39 +0000118 writel(lcr, &regs->pl011_rlcr);
Simon Glassf35484d2014-09-22 17:30:57 -0600119 }
Vikas Manochafe96bbd2014-11-21 10:34:21 -0800120#endif
121 writel(lcr, &regs->pl011_lcrh);
Andreas Engel80438612008-09-08 10:17:31 +0200122 return 0;
123}
124
Simon Glassf35484d2014-09-22 17:30:57 -0600125static int pl01x_generic_setbrg(struct pl01x_regs *regs, enum pl01x_type type,
126 int clock, int baudrate)
wdenk4989f872004-03-14 15:06:13 +0000127{
Simon Glassf35484d2014-09-22 17:30:57 -0600128 switch (type) {
129 case TYPE_PL010: {
130 unsigned int divisor;
wdenk4989f872004-03-14 15:06:13 +0000131
Linus Walleij70864f62015-04-21 15:10:06 +0200132 /* disable everything */
133 writel(0, &regs->pl010_cr);
134
Simon Glassf35484d2014-09-22 17:30:57 -0600135 switch (baudrate) {
136 case 9600:
137 divisor = UART_PL010_BAUD_9600;
138 break;
139 case 19200:
140 divisor = UART_PL010_BAUD_9600;
141 break;
142 case 38400:
143 divisor = UART_PL010_BAUD_38400;
144 break;
145 case 57600:
146 divisor = UART_PL010_BAUD_57600;
147 break;
148 case 115200:
149 divisor = UART_PL010_BAUD_115200;
150 break;
151 default:
152 divisor = UART_PL010_BAUD_38400;
153 }
wdenk4989f872004-03-14 15:06:13 +0000154
Simon Glassf35484d2014-09-22 17:30:57 -0600155 writel((divisor & 0xf00) >> 8, &regs->pl010_lcrm);
156 writel(divisor & 0xff, &regs->pl010_lcrl);
157
Linus Walleij70864f62015-04-21 15:10:06 +0200158 /*
159 * Set line control for the PL010 to be 8 bits, 1 stop bit,
160 * no parity, fifo enabled
161 */
162 writel(UART_PL010_LCRH_WLEN_8 | UART_PL010_LCRH_FEN,
163 &regs->pl010_lcrh);
Simon Glassf35484d2014-09-22 17:30:57 -0600164 /* Finally, enable the UART */
165 writel(UART_PL010_CR_UARTEN, &regs->pl010_cr);
166 break;
167 }
168 case TYPE_PL011: {
169 unsigned int temp;
170 unsigned int divider;
171 unsigned int remainder;
172 unsigned int fraction;
173
174 /*
175 * Set baud rate
176 *
177 * IBRD = UART_CLK / (16 * BAUD_RATE)
178 * FBRD = RND((64 * MOD(UART_CLK,(16 * BAUD_RATE)))
179 * / (16 * BAUD_RATE))
180 */
181 temp = 16 * baudrate;
182 divider = clock / temp;
183 remainder = clock % temp;
184 temp = (8 * remainder) / baudrate;
185 fraction = (temp >> 1) + (temp & 1);
186
187 writel(divider, &regs->pl011_ibrd);
188 writel(fraction, &regs->pl011_fbrd);
189
Linus Walleij70864f62015-04-21 15:10:06 +0200190 pl011_set_line_control(regs);
Simon Glassf35484d2014-09-22 17:30:57 -0600191 /* Finally, enable the UART */
192 writel(UART_PL011_CR_UARTEN | UART_PL011_CR_TXE |
193 UART_PL011_CR_RXE | UART_PL011_CR_RTS, &regs->pl011_cr);
194 break;
195 }
196 default:
197 return -EINVAL;
198 }
199
200 return 0;
wdenk4989f872004-03-14 15:06:13 +0000201}
202
Simon Glassf35484d2014-09-22 17:30:57 -0600203#ifndef CONFIG_DM_SERIAL
204static void pl01x_serial_init_baud(int baudrate)
wdenk4989f872004-03-14 15:06:13 +0000205{
Simon Glassf35484d2014-09-22 17:30:57 -0600206 int clock = 0;
207
208#if defined(CONFIG_PL010_SERIAL)
209 pl01x_type = TYPE_PL010;
210#elif defined(CONFIG_PL011_SERIAL)
211 pl01x_type = TYPE_PL011;
212 clock = CONFIG_PL011_CLOCK;
213#endif
214 base_regs = (struct pl01x_regs *)port[CONFIG_CONS_INDEX];
215
216 pl01x_generic_serial_init(base_regs, pl01x_type);
Vikas Manochaaac23962014-11-21 10:34:19 -0800217 pl01x_generic_setbrg(base_regs, pl01x_type, clock, baudrate);
wdenk4989f872004-03-14 15:06:13 +0000218}
219
Simon Glassf35484d2014-09-22 17:30:57 -0600220/*
221 * Integrator AP has two UARTs, we use the first one, at 38400-8-N-1
222 * Integrator CP has two UARTs, use the first one, at 38400-8-N-1
223 * Versatile PB has four UARTs.
224 */
225int pl01x_serial_init(void)
wdenk4989f872004-03-14 15:06:13 +0000226{
Simon Glassf35484d2014-09-22 17:30:57 -0600227 pl01x_serial_init_baud(CONFIG_BAUDRATE);
Linus Walleijb8058e82011-10-02 11:52:52 +0000228
Simon Glassf35484d2014-09-22 17:30:57 -0600229 return 0;
wdenk4989f872004-03-14 15:06:13 +0000230}
231
Simon Glassf35484d2014-09-22 17:30:57 -0600232static void pl01x_serial_putc(const char c)
wdenk4989f872004-03-14 15:06:13 +0000233{
Simon Glassf35484d2014-09-22 17:30:57 -0600234 if (c == '\n')
235 while (pl01x_putc(base_regs, '\r') == -EAGAIN);
wdenkc35ba4e2004-03-14 22:25:36 +0000236
Simon Glassf35484d2014-09-22 17:30:57 -0600237 while (pl01x_putc(base_regs, c) == -EAGAIN);
wdenk4989f872004-03-14 15:06:13 +0000238}
239
Simon Glassf35484d2014-09-22 17:30:57 -0600240static int pl01x_serial_getc(void)
wdenk4989f872004-03-14 15:06:13 +0000241{
Simon Glassf35484d2014-09-22 17:30:57 -0600242 while (1) {
243 int ch = pl01x_getc(base_regs);
wdenkc35ba4e2004-03-14 22:25:36 +0000244
Simon Glassf35484d2014-09-22 17:30:57 -0600245 if (ch == -EAGAIN) {
246 WATCHDOG_RESET();
247 continue;
248 }
wdenk4989f872004-03-14 15:06:13 +0000249
Simon Glassf35484d2014-09-22 17:30:57 -0600250 return ch;
wdenkc35ba4e2004-03-14 22:25:36 +0000251 }
wdenk4989f872004-03-14 15:06:13 +0000252}
253
Simon Glassf35484d2014-09-22 17:30:57 -0600254static int pl01x_serial_tstc(void)
wdenk4989f872004-03-14 15:06:13 +0000255{
Simon Glassf35484d2014-09-22 17:30:57 -0600256 return pl01x_tstc(base_regs);
257}
Rabin Vincentfb3c95f2010-05-05 09:23:07 +0530258
Simon Glassf35484d2014-09-22 17:30:57 -0600259static void pl01x_serial_setbrg(void)
260{
261 /*
262 * Flush FIFO and wait for non-busy before changing baudrate to avoid
263 * crap in console
264 */
265 while (!(readl(&base_regs->fr) & UART_PL01x_FR_TXFE))
266 WATCHDOG_RESET();
267 while (readl(&base_regs->fr) & UART_PL01x_FR_BUSY)
268 WATCHDOG_RESET();
269 pl01x_serial_init_baud(gd->baudrate);
wdenk4989f872004-03-14 15:06:13 +0000270}
Marek Vasut46e4d5f2012-09-14 22:38:46 +0200271
Marek Vasut46e4d5f2012-09-14 22:38:46 +0200272static struct serial_device pl01x_serial_drv = {
273 .name = "pl01x_serial",
274 .start = pl01x_serial_init,
275 .stop = NULL,
276 .setbrg = pl01x_serial_setbrg,
277 .putc = pl01x_serial_putc,
Marek Vasutd9c64492012-10-06 14:07:02 +0000278 .puts = default_serial_puts,
Marek Vasut46e4d5f2012-09-14 22:38:46 +0200279 .getc = pl01x_serial_getc,
280 .tstc = pl01x_serial_tstc,
281};
282
283void pl01x_serial_initialize(void)
284{
285 serial_register(&pl01x_serial_drv);
286}
287
288__weak struct serial_device *default_serial_console(void)
289{
290 return &pl01x_serial_drv;
291}
Simon Glassf35484d2014-09-22 17:30:57 -0600292
293#endif /* nCONFIG_DM_SERIAL */
Simon Glass3ad93fe2014-09-22 17:30:58 -0600294
295#ifdef CONFIG_DM_SERIAL
296
297struct pl01x_priv {
298 struct pl01x_regs *regs;
299 enum pl01x_type type;
300};
301
302static int pl01x_serial_setbrg(struct udevice *dev, int baudrate)
303{
304 struct pl01x_serial_platdata *plat = dev_get_platdata(dev);
305 struct pl01x_priv *priv = dev_get_priv(dev);
306
307 pl01x_generic_setbrg(priv->regs, priv->type, plat->clock, baudrate);
308
309 return 0;
310}
311
312static int pl01x_serial_probe(struct udevice *dev)
313{
314 struct pl01x_serial_platdata *plat = dev_get_platdata(dev);
315 struct pl01x_priv *priv = dev_get_priv(dev);
316
317 priv->regs = (struct pl01x_regs *)plat->base;
318 priv->type = plat->type;
319 return pl01x_generic_serial_init(priv->regs, priv->type);
320}
321
322static int pl01x_serial_getc(struct udevice *dev)
323{
324 struct pl01x_priv *priv = dev_get_priv(dev);
325
326 return pl01x_getc(priv->regs);
327}
328
329static int pl01x_serial_putc(struct udevice *dev, const char ch)
330{
331 struct pl01x_priv *priv = dev_get_priv(dev);
332
333 return pl01x_putc(priv->regs, ch);
334}
335
336static int pl01x_serial_pending(struct udevice *dev, bool input)
337{
338 struct pl01x_priv *priv = dev_get_priv(dev);
339 unsigned int fr = readl(&priv->regs->fr);
340
341 if (input)
342 return pl01x_tstc(priv->regs);
343 else
344 return fr & UART_PL01x_FR_TXFF ? 0 : 1;
345}
346
347static const struct dm_serial_ops pl01x_serial_ops = {
348 .putc = pl01x_serial_putc,
349 .pending = pl01x_serial_pending,
350 .getc = pl01x_serial_getc,
351 .setbrg = pl01x_serial_setbrg,
352};
353
354U_BOOT_DRIVER(serial_pl01x) = {
355 .name = "serial_pl01x",
356 .id = UCLASS_SERIAL,
357 .probe = pl01x_serial_probe,
358 .ops = &pl01x_serial_ops,
359 .flags = DM_FLAG_PRE_RELOC,
Simon Glass900de912014-11-24 21:36:35 -0700360 .priv_auto_alloc_size = sizeof(struct pl01x_priv),
Simon Glass3ad93fe2014-09-22 17:30:58 -0600361};
362
363#endif