blob: 45f12827703ad0db222827dbc2da358603409072 [file] [log] [blame]
wdenk3d3befa2004-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 Denk1a459662013-07-08 09:37:19 +02009 * SPDX-License-Identifier: GPL-2.0+
wdenk3d3befa2004-03-14 15:06:13 +000010 */
11
Andreas Engel48d01922008-09-08 14:30:53 +020012/* Simple U-Boot driver for the PrimeCell PL010/PL011 UARTs */
wdenk3d3befa2004-03-14 15:06:13 +000013
14#include <common.h>
Simon Glass8a9cd5a2014-09-22 17:30:58 -060015#include <dm.h>
Simon Glassaed2fbe2014-09-22 17:30:57 -060016#include <errno.h>
Stuart Wood8b616ed2008-06-02 16:42:19 -040017#include <watchdog.h>
Matt Waddel249d5212010-10-07 15:48:46 -060018#include <asm/io.h>
Marek Vasut39f61472012-09-14 22:38:46 +020019#include <serial.h>
Masahiro Yamada86256b72014-10-24 12:41:19 +090020#include <dm/platform_data/serial_pl01x.h>
Marek Vasut39f61472012-09-14 22:38:46 +020021#include <linux/compiler.h>
Simon Glassaed2fbe2014-09-22 17:30:57 -060022#include "serial_pl01x_internal.h"
Vikas Manocha69751722015-05-06 11:46:29 -070023
24DECLARE_GLOBAL_DATA_PTR;
wdenk3d3befa2004-03-14 15:06:13 +000025
Simon Glass8a9cd5a2014-09-22 17:30:58 -060026#ifndef CONFIG_DM_SERIAL
27
wdenk6705d812004-08-02 23:22:59 +000028static volatile unsigned char *const port[] = CONFIG_PL01x_PORTS;
Simon Glassaed2fbe2014-09-22 17:30:57 -060029static enum pl01x_type pl01x_type __attribute__ ((section(".data")));
30static struct pl01x_regs *base_regs __attribute__ ((section(".data")));
wdenk6705d812004-08-02 23:22:59 +000031#define NUM_PORTS (sizeof(port)/sizeof(port[0]))
wdenk3d3befa2004-03-14 15:06:13 +000032
Simon Glass8a9cd5a2014-09-22 17:30:58 -060033#endif
wdenk3d3befa2004-03-14 15:06:13 +000034
Simon Glassaed2fbe2014-09-22 17:30:57 -060035static int pl01x_putc(struct pl01x_regs *regs, char c)
Rabin Vincent72d5e442010-05-05 09:23:07 +053036{
wdenk42dfe7a2004-03-14 22:25:36 +000037 /* Wait until there is space in the FIFO */
Simon Glassaed2fbe2014-09-22 17:30:57 -060038 if (readl(&regs->fr) & UART_PL01x_FR_TXFF)
39 return -EAGAIN;
wdenk42dfe7a2004-03-14 22:25:36 +000040
41 /* Send the character */
Rabin Vincent72d5e442010-05-05 09:23:07 +053042 writel(c, &regs->dr);
Simon Glassaed2fbe2014-09-22 17:30:57 -060043
44 return 0;
wdenk3d3befa2004-03-14 15:06:13 +000045}
46
Simon Glassaed2fbe2014-09-22 17:30:57 -060047static int pl01x_getc(struct pl01x_regs *regs)
wdenk3d3befa2004-03-14 15:06:13 +000048{
wdenk42dfe7a2004-03-14 22:25:36 +000049 unsigned int data;
wdenk3d3befa2004-03-14 15:06:13 +000050
wdenk42dfe7a2004-03-14 22:25:36 +000051 /* Wait until there is data in the FIFO */
Simon Glassaed2fbe2014-09-22 17:30:57 -060052 if (readl(&regs->fr) & UART_PL01x_FR_RXFE)
53 return -EAGAIN;
wdenk42dfe7a2004-03-14 22:25:36 +000054
Rabin Vincent72d5e442010-05-05 09:23:07 +053055 data = readl(&regs->dr);
wdenk42dfe7a2004-03-14 22:25:36 +000056
57 /* Check for an error flag */
58 if (data & 0xFFFFFF00) {
59 /* Clear the error */
Rabin Vincent72d5e442010-05-05 09:23:07 +053060 writel(0xFFFFFFFF, &regs->ecr);
wdenk42dfe7a2004-03-14 22:25:36 +000061 return -1;
62 }
63
64 return (int) data;
wdenk3d3befa2004-03-14 15:06:13 +000065}
66
Simon Glassaed2fbe2014-09-22 17:30:57 -060067static int pl01x_tstc(struct pl01x_regs *regs)
wdenk3d3befa2004-03-14 15:06:13 +000068{
Stuart Wood8b616ed2008-06-02 16:42:19 -040069 WATCHDOG_RESET();
Rabin Vincent72d5e442010-05-05 09:23:07 +053070 return !(readl(&regs->fr) & UART_PL01x_FR_RXFE);
wdenk3d3befa2004-03-14 15:06:13 +000071}
Marek Vasut39f61472012-09-14 22:38:46 +020072
Simon Glassaed2fbe2014-09-22 17:30:57 -060073static int pl01x_generic_serial_init(struct pl01x_regs *regs,
74 enum pl01x_type type)
75{
Simon Glassaed2fbe2014-09-22 17:30:57 -060076 switch (type) {
77 case TYPE_PL010:
Vikas Manochaf7e517b2014-11-21 10:34:22 -080078 /* disable everything */
79 writel(0, &regs->pl010_cr);
Simon Glassaed2fbe2014-09-22 17:30:57 -060080 break;
Vikas Manochad2ca9fd2014-11-21 10:34:21 -080081 case TYPE_PL011:
Vikas Manochaf7e517b2014-11-21 10:34:22 -080082 /* disable everything */
83 writel(0, &regs->pl011_cr);
Vikas Manochad2ca9fd2014-11-21 10:34:21 -080084 break;
85 default:
86 return -EINVAL;
87 }
88
89 return 0;
90}
91
Linus Walleijd77447f2015-04-21 15:10:06 +020092static int pl011_set_line_control(struct pl01x_regs *regs)
Vikas Manochad2ca9fd2014-11-21 10:34:21 -080093{
94 unsigned int lcr;
95 /*
96 * Internal update of baud rate register require line
97 * control register write
98 */
99 lcr = UART_PL011_LCRH_WLEN_8 | UART_PL011_LCRH_FEN;
Vikas Manochad2ca9fd2014-11-21 10:34:21 -0800100 writel(lcr, &regs->pl011_lcrh);
Simon Glassaed2fbe2014-09-22 17:30:57 -0600101 return 0;
102}
103
104static int pl01x_generic_setbrg(struct pl01x_regs *regs, enum pl01x_type type,
105 int clock, int baudrate)
106{
107 switch (type) {
108 case TYPE_PL010: {
109 unsigned int divisor;
110
Linus Walleijd77447f2015-04-21 15:10:06 +0200111 /* disable everything */
112 writel(0, &regs->pl010_cr);
113
Simon Glassaed2fbe2014-09-22 17:30:57 -0600114 switch (baudrate) {
115 case 9600:
116 divisor = UART_PL010_BAUD_9600;
117 break;
118 case 19200:
Alyssa Rosenzweigb2aa8892017-04-07 09:48:22 -0700119 divisor = UART_PL010_BAUD_19200;
Simon Glassaed2fbe2014-09-22 17:30:57 -0600120 break;
121 case 38400:
122 divisor = UART_PL010_BAUD_38400;
123 break;
124 case 57600:
125 divisor = UART_PL010_BAUD_57600;
126 break;
127 case 115200:
128 divisor = UART_PL010_BAUD_115200;
129 break;
130 default:
131 divisor = UART_PL010_BAUD_38400;
132 }
133
134 writel((divisor & 0xf00) >> 8, &regs->pl010_lcrm);
135 writel(divisor & 0xff, &regs->pl010_lcrl);
136
Linus Walleijd77447f2015-04-21 15:10:06 +0200137 /*
138 * Set line control for the PL010 to be 8 bits, 1 stop bit,
139 * no parity, fifo enabled
140 */
141 writel(UART_PL010_LCRH_WLEN_8 | UART_PL010_LCRH_FEN,
142 &regs->pl010_lcrh);
Simon Glassaed2fbe2014-09-22 17:30:57 -0600143 /* Finally, enable the UART */
144 writel(UART_PL010_CR_UARTEN, &regs->pl010_cr);
145 break;
146 }
147 case TYPE_PL011: {
148 unsigned int temp;
149 unsigned int divider;
150 unsigned int remainder;
151 unsigned int fraction;
152
153 /*
154 * Set baud rate
155 *
156 * IBRD = UART_CLK / (16 * BAUD_RATE)
157 * FBRD = RND((64 * MOD(UART_CLK,(16 * BAUD_RATE)))
158 * / (16 * BAUD_RATE))
159 */
160 temp = 16 * baudrate;
161 divider = clock / temp;
162 remainder = clock % temp;
163 temp = (8 * remainder) / baudrate;
164 fraction = (temp >> 1) + (temp & 1);
165
166 writel(divider, &regs->pl011_ibrd);
167 writel(fraction, &regs->pl011_fbrd);
168
Linus Walleijd77447f2015-04-21 15:10:06 +0200169 pl011_set_line_control(regs);
Simon Glassaed2fbe2014-09-22 17:30:57 -0600170 /* Finally, enable the UART */
171 writel(UART_PL011_CR_UARTEN | UART_PL011_CR_TXE |
172 UART_PL011_CR_RXE | UART_PL011_CR_RTS, &regs->pl011_cr);
173 break;
174 }
175 default:
176 return -EINVAL;
177 }
178
179 return 0;
180}
181
182#ifndef CONFIG_DM_SERIAL
183static void pl01x_serial_init_baud(int baudrate)
184{
185 int clock = 0;
186
187#if defined(CONFIG_PL010_SERIAL)
188 pl01x_type = TYPE_PL010;
189#elif defined(CONFIG_PL011_SERIAL)
190 pl01x_type = TYPE_PL011;
191 clock = CONFIG_PL011_CLOCK;
192#endif
193 base_regs = (struct pl01x_regs *)port[CONFIG_CONS_INDEX];
194
195 pl01x_generic_serial_init(base_regs, pl01x_type);
Vikas Manochaa7deea62014-11-21 10:34:19 -0800196 pl01x_generic_setbrg(base_regs, pl01x_type, clock, baudrate);
Simon Glassaed2fbe2014-09-22 17:30:57 -0600197}
198
199/*
200 * Integrator AP has two UARTs, we use the first one, at 38400-8-N-1
201 * Integrator CP has two UARTs, use the first one, at 38400-8-N-1
202 * Versatile PB has four UARTs.
203 */
204int pl01x_serial_init(void)
205{
206 pl01x_serial_init_baud(CONFIG_BAUDRATE);
207
208 return 0;
209}
210
211static void pl01x_serial_putc(const char c)
212{
213 if (c == '\n')
214 while (pl01x_putc(base_regs, '\r') == -EAGAIN);
215
216 while (pl01x_putc(base_regs, c) == -EAGAIN);
217}
218
219static int pl01x_serial_getc(void)
220{
221 while (1) {
222 int ch = pl01x_getc(base_regs);
223
224 if (ch == -EAGAIN) {
225 WATCHDOG_RESET();
226 continue;
227 }
228
229 return ch;
230 }
231}
232
233static int pl01x_serial_tstc(void)
234{
235 return pl01x_tstc(base_regs);
236}
237
238static void pl01x_serial_setbrg(void)
239{
240 /*
241 * Flush FIFO and wait for non-busy before changing baudrate to avoid
242 * crap in console
243 */
244 while (!(readl(&base_regs->fr) & UART_PL01x_FR_TXFE))
245 WATCHDOG_RESET();
246 while (readl(&base_regs->fr) & UART_PL01x_FR_BUSY)
247 WATCHDOG_RESET();
248 pl01x_serial_init_baud(gd->baudrate);
249}
250
Marek Vasut39f61472012-09-14 22:38:46 +0200251static struct serial_device pl01x_serial_drv = {
252 .name = "pl01x_serial",
253 .start = pl01x_serial_init,
254 .stop = NULL,
255 .setbrg = pl01x_serial_setbrg,
256 .putc = pl01x_serial_putc,
Marek Vasutec3fd682012-10-06 14:07:02 +0000257 .puts = default_serial_puts,
Marek Vasut39f61472012-09-14 22:38:46 +0200258 .getc = pl01x_serial_getc,
259 .tstc = pl01x_serial_tstc,
260};
261
262void pl01x_serial_initialize(void)
263{
264 serial_register(&pl01x_serial_drv);
265}
266
267__weak struct serial_device *default_serial_console(void)
268{
269 return &pl01x_serial_drv;
270}
Simon Glassaed2fbe2014-09-22 17:30:57 -0600271
272#endif /* nCONFIG_DM_SERIAL */
Simon Glass8a9cd5a2014-09-22 17:30:58 -0600273
274#ifdef CONFIG_DM_SERIAL
275
Alexander Grafc9bf43d2018-03-07 22:08:25 +0100276int pl01x_serial_setbrg(struct udevice *dev, int baudrate)
Simon Glass8a9cd5a2014-09-22 17:30:58 -0600277{
278 struct pl01x_serial_platdata *plat = dev_get_platdata(dev);
279 struct pl01x_priv *priv = dev_get_priv(dev);
280
Eric Anholtcd0fa5b2016-03-13 18:16:54 -0700281 if (!plat->skip_init) {
282 pl01x_generic_setbrg(priv->regs, priv->type, plat->clock,
283 baudrate);
284 }
Simon Glass8a9cd5a2014-09-22 17:30:58 -0600285
286 return 0;
287}
288
Alexander Graf60019852018-01-25 12:05:55 +0100289int pl01x_serial_probe(struct udevice *dev)
Simon Glass8a9cd5a2014-09-22 17:30:58 -0600290{
291 struct pl01x_serial_platdata *plat = dev_get_platdata(dev);
292 struct pl01x_priv *priv = dev_get_priv(dev);
293
294 priv->regs = (struct pl01x_regs *)plat->base;
295 priv->type = plat->type;
Eric Anholtcd0fa5b2016-03-13 18:16:54 -0700296 if (!plat->skip_init)
297 return pl01x_generic_serial_init(priv->regs, priv->type);
298 else
299 return 0;
Simon Glass8a9cd5a2014-09-22 17:30:58 -0600300}
301
Alexander Grafc9bf43d2018-03-07 22:08:25 +0100302int pl01x_serial_getc(struct udevice *dev)
Simon Glass8a9cd5a2014-09-22 17:30:58 -0600303{
304 struct pl01x_priv *priv = dev_get_priv(dev);
305
306 return pl01x_getc(priv->regs);
307}
308
Alexander Grafc9bf43d2018-03-07 22:08:25 +0100309int pl01x_serial_putc(struct udevice *dev, const char ch)
Simon Glass8a9cd5a2014-09-22 17:30:58 -0600310{
311 struct pl01x_priv *priv = dev_get_priv(dev);
312
313 return pl01x_putc(priv->regs, ch);
314}
315
Alexander Grafc9bf43d2018-03-07 22:08:25 +0100316int pl01x_serial_pending(struct udevice *dev, bool input)
Simon Glass8a9cd5a2014-09-22 17:30:58 -0600317{
318 struct pl01x_priv *priv = dev_get_priv(dev);
319 unsigned int fr = readl(&priv->regs->fr);
320
321 if (input)
322 return pl01x_tstc(priv->regs);
323 else
324 return fr & UART_PL01x_FR_TXFF ? 0 : 1;
325}
326
Alexander Grafc9bf43d2018-03-07 22:08:25 +0100327static const struct dm_serial_ops pl01x_serial_ops = {
Simon Glass8a9cd5a2014-09-22 17:30:58 -0600328 .putc = pl01x_serial_putc,
329 .pending = pl01x_serial_pending,
330 .getc = pl01x_serial_getc,
331 .setbrg = pl01x_serial_setbrg,
332};
333
Masahiro Yamada0f925822015-08-12 07:31:55 +0900334#if CONFIG_IS_ENABLED(OF_CONTROL)
Vikas Manocha69751722015-05-06 11:46:29 -0700335static const struct udevice_id pl01x_serial_id[] ={
336 {.compatible = "arm,pl011", .data = TYPE_PL011},
337 {.compatible = "arm,pl010", .data = TYPE_PL010},
338 {}
339};
340
Alexander Graf60019852018-01-25 12:05:55 +0100341int pl01x_serial_ofdata_to_platdata(struct udevice *dev)
Vikas Manocha69751722015-05-06 11:46:29 -0700342{
343 struct pl01x_serial_platdata *plat = dev_get_platdata(dev);
344 fdt_addr_t addr;
345
Simon Glassa821c4a2017-05-17 17:18:05 -0600346 addr = devfdt_get_addr(dev);
Vikas Manocha69751722015-05-06 11:46:29 -0700347 if (addr == FDT_ADDR_T_NONE)
348 return -EINVAL;
349
350 plat->base = addr;
Alexander Grafb3111632018-01-25 12:05:49 +0100351 plat->clock = dev_read_u32_default(dev, "clock", 1);
Vikas Manocha69751722015-05-06 11:46:29 -0700352 plat->type = dev_get_driver_data(dev);
Alexander Grafb3111632018-01-25 12:05:49 +0100353 plat->skip_init = dev_read_bool(dev, "skip-init");
354
Vikas Manocha69751722015-05-06 11:46:29 -0700355 return 0;
356}
357#endif
358
Simon Glass8a9cd5a2014-09-22 17:30:58 -0600359U_BOOT_DRIVER(serial_pl01x) = {
360 .name = "serial_pl01x",
361 .id = UCLASS_SERIAL,
Vikas Manocha69751722015-05-06 11:46:29 -0700362 .of_match = of_match_ptr(pl01x_serial_id),
363 .ofdata_to_platdata = of_match_ptr(pl01x_serial_ofdata_to_platdata),
364 .platdata_auto_alloc_size = sizeof(struct pl01x_serial_platdata),
Simon Glass8a9cd5a2014-09-22 17:30:58 -0600365 .probe = pl01x_serial_probe,
366 .ops = &pl01x_serial_ops,
367 .flags = DM_FLAG_PRE_RELOC,
Simon Glass59c73d72014-11-24 21:36:35 -0700368 .priv_auto_alloc_size = sizeof(struct pl01x_priv),
Simon Glass8a9cd5a2014-09-22 17:30:58 -0600369};
370
371#endif
Sergey Temerkhanovb81406d2015-10-14 09:54:23 -0700372
373#if defined(CONFIG_DEBUG_UART_PL010) || defined(CONFIG_DEBUG_UART_PL011)
374
375#include <debug_uart.h>
376
377static void _debug_uart_init(void)
378{
379#ifndef CONFIG_DEBUG_UART_SKIP_INIT
380 struct pl01x_regs *regs = (struct pl01x_regs *)CONFIG_DEBUG_UART_BASE;
381 enum pl01x_type type = CONFIG_IS_ENABLED(DEBUG_UART_PL011) ?
382 TYPE_PL011 : TYPE_PL010;
383
384 pl01x_generic_serial_init(regs, type);
385 pl01x_generic_setbrg(regs, type,
386 CONFIG_DEBUG_UART_CLOCK, CONFIG_BAUDRATE);
387#endif
388}
389
390static inline void _debug_uart_putc(int ch)
391{
392 struct pl01x_regs *regs = (struct pl01x_regs *)CONFIG_DEBUG_UART_BASE;
393
394 pl01x_putc(regs, ch);
395}
396
397DEBUG_UART_FUNCS
398
399#endif