blob: dfb610e1a9d553429d1462335a6550f6d31afe04 [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>
Stuart Wood26136ef2008-06-02 16:42:19 -040015#include <watchdog.h>
Matt Waddeld6ce53e2010-10-07 15:48:46 -060016#include <asm/io.h>
Marek Vasut46e4d5f2012-09-14 22:38:46 +020017#include <serial.h>
18#include <linux/compiler.h>
Andreas Engel80438612008-09-08 10:17:31 +020019#include "serial_pl01x.h"
wdenk4989f872004-03-14 15:06:13 +000020
Andreas Engel80438612008-09-08 10:17:31 +020021/*
22 * Integrator AP has two UARTs, we use the first one, at 38400-8-N-1
23 * Integrator CP has two UARTs, use the first one, at 38400-8-N-1
24 * Versatile PB has four UARTs.
25 */
wdenk4989f872004-03-14 15:06:13 +000026#define CONSOLE_PORT CONFIG_CONS_INDEX
wdenkda04a8b2004-08-02 23:22:59 +000027static volatile unsigned char *const port[] = CONFIG_PL01x_PORTS;
28#define NUM_PORTS (sizeof(port)/sizeof(port[0]))
wdenk4989f872004-03-14 15:06:13 +000029
Andreas Engel80438612008-09-08 10:17:31 +020030static void pl01x_putc (int portnum, char c);
31static int pl01x_getc (int portnum);
32static int pl01x_tstc (int portnum);
Matt Waddeld6ce53e2010-10-07 15:48:46 -060033unsigned int baudrate = CONFIG_BAUDRATE;
34DECLARE_GLOBAL_DATA_PTR;
wdenk4989f872004-03-14 15:06:13 +000035
Rabin Vincentfb3c95f2010-05-05 09:23:07 +053036static struct pl01x_regs *pl01x_get_regs(int portnum)
37{
38 return (struct pl01x_regs *) port[portnum];
39}
40
Andreas Engel0813b122008-09-08 14:30:53 +020041#ifdef CONFIG_PL010_SERIAL
wdenk4989f872004-03-14 15:06:13 +000042
Marek Vasut46e4d5f2012-09-14 22:38:46 +020043static int pl01x_serial_init(void)
wdenk4989f872004-03-14 15:06:13 +000044{
Rabin Vincentfb3c95f2010-05-05 09:23:07 +053045 struct pl01x_regs *regs = pl01x_get_regs(CONSOLE_PORT);
wdenkc35ba4e2004-03-14 22:25:36 +000046 unsigned int divisor;
wdenk4989f872004-03-14 15:06:13 +000047
Matt Waddeld6ce53e2010-10-07 15:48:46 -060048 /* First, disable everything */
Rabin Vincentfb3c95f2010-05-05 09:23:07 +053049 writel(0, &regs->pl010_cr);
wdenk4989f872004-03-14 15:06:13 +000050
Matt Waddeld6ce53e2010-10-07 15:48:46 -060051 /* Set baud rate */
52 switch (baudrate) {
wdenkc35ba4e2004-03-14 22:25:36 +000053 case 9600:
54 divisor = UART_PL010_BAUD_9600;
55 break;
wdenk4989f872004-03-14 15:06:13 +000056
wdenkc35ba4e2004-03-14 22:25:36 +000057 case 19200:
58 divisor = UART_PL010_BAUD_9600;
59 break;
wdenk4989f872004-03-14 15:06:13 +000060
wdenkc35ba4e2004-03-14 22:25:36 +000061 case 38400:
62 divisor = UART_PL010_BAUD_38400;
63 break;
wdenk4989f872004-03-14 15:06:13 +000064
wdenkc35ba4e2004-03-14 22:25:36 +000065 case 57600:
66 divisor = UART_PL010_BAUD_57600;
67 break;
wdenk4989f872004-03-14 15:06:13 +000068
wdenkc35ba4e2004-03-14 22:25:36 +000069 case 115200:
70 divisor = UART_PL010_BAUD_115200;
71 break;
wdenk4989f872004-03-14 15:06:13 +000072
wdenkc35ba4e2004-03-14 22:25:36 +000073 default:
74 divisor = UART_PL010_BAUD_38400;
75 }
76
Rabin Vincentfb3c95f2010-05-05 09:23:07 +053077 writel((divisor & 0xf00) >> 8, &regs->pl010_lcrm);
78 writel(divisor & 0xff, &regs->pl010_lcrl);
wdenk4989f872004-03-14 15:06:13 +000079
Matt Waddeld6ce53e2010-10-07 15:48:46 -060080 /* Set the UART to be 8 bits, 1 stop bit, no parity, fifo enabled */
Rabin Vincentfb3c95f2010-05-05 09:23:07 +053081 writel(UART_PL010_LCRH_WLEN_8 | UART_PL010_LCRH_FEN, &regs->pl010_lcrh);
wdenk4989f872004-03-14 15:06:13 +000082
Matt Waddeld6ce53e2010-10-07 15:48:46 -060083 /* Finally, enable the UART */
Rabin Vincentfb3c95f2010-05-05 09:23:07 +053084 writel(UART_PL010_CR_UARTEN, &regs->pl010_cr);
wdenk4989f872004-03-14 15:06:13 +000085
Andreas Engel80438612008-09-08 10:17:31 +020086 return 0;
wdenk4989f872004-03-14 15:06:13 +000087}
88
Andreas Engel0813b122008-09-08 14:30:53 +020089#endif /* CONFIG_PL010_SERIAL */
Andreas Engel80438612008-09-08 10:17:31 +020090
Andreas Engel0813b122008-09-08 14:30:53 +020091#ifdef CONFIG_PL011_SERIAL
Andreas Engel80438612008-09-08 10:17:31 +020092
Marek Vasut46e4d5f2012-09-14 22:38:46 +020093static int pl01x_serial_init(void)
Andreas Engel80438612008-09-08 10:17:31 +020094{
Rabin Vincentfb3c95f2010-05-05 09:23:07 +053095 struct pl01x_regs *regs = pl01x_get_regs(CONSOLE_PORT);
Andreas Engel80438612008-09-08 10:17:31 +020096 unsigned int temp;
97 unsigned int divider;
98 unsigned int remainder;
99 unsigned int fraction;
John Rigby34e21ee2011-04-19 10:42:39 +0000100 unsigned int lcr;
101
102#ifdef CONFIG_PL011_SERIAL_FLUSH_ON_INIT
103 /* Empty RX fifo if necessary */
104 if (readl(&regs->pl011_cr) & UART_PL011_CR_UARTEN) {
105 while (!(readl(&regs->fr) & UART_PL01x_FR_RXFE))
106 readl(&regs->dr);
107 }
108#endif
Andreas Engel80438612008-09-08 10:17:31 +0200109
Matt Waddeld6ce53e2010-10-07 15:48:46 -0600110 /* First, disable everything */
Rabin Vincentfb3c95f2010-05-05 09:23:07 +0530111 writel(0, &regs->pl011_cr);
Andreas Engel80438612008-09-08 10:17:31 +0200112
113 /*
Matt Waddeld6ce53e2010-10-07 15:48:46 -0600114 * Set baud rate
115 *
116 * IBRD = UART_CLK / (16 * BAUD_RATE)
117 * FBRD = RND((64 * MOD(UART_CLK,(16 * BAUD_RATE))) / (16 * BAUD_RATE))
Andreas Engel80438612008-09-08 10:17:31 +0200118 */
Matt Waddeld6ce53e2010-10-07 15:48:46 -0600119 temp = 16 * baudrate;
Andreas Engel80438612008-09-08 10:17:31 +0200120 divider = CONFIG_PL011_CLOCK / temp;
121 remainder = CONFIG_PL011_CLOCK % temp;
Matt Waddeld6ce53e2010-10-07 15:48:46 -0600122 temp = (8 * remainder) / baudrate;
Andreas Engel80438612008-09-08 10:17:31 +0200123 fraction = (temp >> 1) + (temp & 1);
124
Rabin Vincentfb3c95f2010-05-05 09:23:07 +0530125 writel(divider, &regs->pl011_ibrd);
126 writel(fraction, &regs->pl011_fbrd);
Andreas Engel80438612008-09-08 10:17:31 +0200127
Matt Waddeld6ce53e2010-10-07 15:48:46 -0600128 /* Set the UART to be 8 bits, 1 stop bit, no parity, fifo enabled */
John Rigby34e21ee2011-04-19 10:42:39 +0000129 lcr = UART_PL011_LCRH_WLEN_8 | UART_PL011_LCRH_FEN;
130 writel(lcr, &regs->pl011_lcrh);
131
132#ifdef CONFIG_PL011_SERIAL_RLCR
133 {
134 int i;
135
136 /*
137 * Program receive line control register after waiting
138 * 10 bus cycles. Delay be writing to readonly register
139 * 10 times
140 */
141 for (i = 0; i < 10; i++)
142 writel(lcr, &regs->fr);
Andreas Engel80438612008-09-08 10:17:31 +0200143
John Rigby34e21ee2011-04-19 10:42:39 +0000144 writel(lcr, &regs->pl011_rlcr);
Mathieu J. Poirier4a036fe2012-08-03 11:05:12 +0000145 /* lcrh needs to be set again for change to be effective */
146 writel(lcr, &regs->pl011_lcrh);
John Rigby34e21ee2011-04-19 10:42:39 +0000147 }
148#endif
Matt Waddeld6ce53e2010-10-07 15:48:46 -0600149 /* Finally, enable the UART */
Joshua Houshf1d17fb2012-12-02 17:09:26 +0000150 writel(UART_PL011_CR_UARTEN | UART_PL011_CR_TXE | UART_PL011_CR_RXE |
151 UART_PL011_CR_RTS, &regs->pl011_cr);
Andreas Engel80438612008-09-08 10:17:31 +0200152
153 return 0;
154}
155
Andreas Engel0813b122008-09-08 14:30:53 +0200156#endif /* CONFIG_PL011_SERIAL */
Andreas Engel80438612008-09-08 10:17:31 +0200157
Marek Vasut46e4d5f2012-09-14 22:38:46 +0200158static void pl01x_serial_putc(const char c)
wdenk4989f872004-03-14 15:06:13 +0000159{
160 if (c == '\n')
Andreas Engel80438612008-09-08 10:17:31 +0200161 pl01x_putc (CONSOLE_PORT, '\r');
wdenk4989f872004-03-14 15:06:13 +0000162
Andreas Engel80438612008-09-08 10:17:31 +0200163 pl01x_putc (CONSOLE_PORT, c);
wdenk4989f872004-03-14 15:06:13 +0000164}
165
Marek Vasut46e4d5f2012-09-14 22:38:46 +0200166static int pl01x_serial_getc(void)
wdenk4989f872004-03-14 15:06:13 +0000167{
Andreas Engel80438612008-09-08 10:17:31 +0200168 return pl01x_getc (CONSOLE_PORT);
wdenk4989f872004-03-14 15:06:13 +0000169}
170
Marek Vasut46e4d5f2012-09-14 22:38:46 +0200171static int pl01x_serial_tstc(void)
wdenk4989f872004-03-14 15:06:13 +0000172{
Andreas Engel80438612008-09-08 10:17:31 +0200173 return pl01x_tstc (CONSOLE_PORT);
wdenk4989f872004-03-14 15:06:13 +0000174}
175
Marek Vasut46e4d5f2012-09-14 22:38:46 +0200176static void pl01x_serial_setbrg(void)
wdenk4989f872004-03-14 15:06:13 +0000177{
Linus Walleijb8058e82011-10-02 11:52:52 +0000178 struct pl01x_regs *regs = pl01x_get_regs(CONSOLE_PORT);
179
Matt Waddeld6ce53e2010-10-07 15:48:46 -0600180 baudrate = gd->baudrate;
Linus Walleijb8058e82011-10-02 11:52:52 +0000181 /*
182 * Flush FIFO and wait for non-busy before changing baudrate to avoid
183 * crap in console
184 */
185 while (!(readl(&regs->fr) & UART_PL01x_FR_TXFE))
186 WATCHDOG_RESET();
187 while (readl(&regs->fr) & UART_PL01x_FR_BUSY)
188 WATCHDOG_RESET();
Matt Waddeld6ce53e2010-10-07 15:48:46 -0600189 serial_init();
wdenk4989f872004-03-14 15:06:13 +0000190}
191
Andreas Engel80438612008-09-08 10:17:31 +0200192static void pl01x_putc (int portnum, char c)
wdenk4989f872004-03-14 15:06:13 +0000193{
Rabin Vincentfb3c95f2010-05-05 09:23:07 +0530194 struct pl01x_regs *regs = pl01x_get_regs(portnum);
195
wdenkc35ba4e2004-03-14 22:25:36 +0000196 /* Wait until there is space in the FIFO */
Rabin Vincentfb3c95f2010-05-05 09:23:07 +0530197 while (readl(&regs->fr) & UART_PL01x_FR_TXFF)
Stuart Wood26136ef2008-06-02 16:42:19 -0400198 WATCHDOG_RESET();
wdenkc35ba4e2004-03-14 22:25:36 +0000199
200 /* Send the character */
Rabin Vincentfb3c95f2010-05-05 09:23:07 +0530201 writel(c, &regs->dr);
wdenk4989f872004-03-14 15:06:13 +0000202}
203
Andreas Engel80438612008-09-08 10:17:31 +0200204static int pl01x_getc (int portnum)
wdenk4989f872004-03-14 15:06:13 +0000205{
Rabin Vincentfb3c95f2010-05-05 09:23:07 +0530206 struct pl01x_regs *regs = pl01x_get_regs(portnum);
wdenkc35ba4e2004-03-14 22:25:36 +0000207 unsigned int data;
208
209 /* Wait until there is data in the FIFO */
Rabin Vincentfb3c95f2010-05-05 09:23:07 +0530210 while (readl(&regs->fr) & UART_PL01x_FR_RXFE)
Stuart Wood26136ef2008-06-02 16:42:19 -0400211 WATCHDOG_RESET();
wdenk4989f872004-03-14 15:06:13 +0000212
Rabin Vincentfb3c95f2010-05-05 09:23:07 +0530213 data = readl(&regs->dr);
wdenkc35ba4e2004-03-14 22:25:36 +0000214
215 /* Check for an error flag */
216 if (data & 0xFFFFFF00) {
217 /* Clear the error */
Rabin Vincentfb3c95f2010-05-05 09:23:07 +0530218 writel(0xFFFFFFFF, &regs->ecr);
wdenkc35ba4e2004-03-14 22:25:36 +0000219 return -1;
220 }
221
222 return (int) data;
wdenk4989f872004-03-14 15:06:13 +0000223}
224
Andreas Engel80438612008-09-08 10:17:31 +0200225static int pl01x_tstc (int portnum)
wdenk4989f872004-03-14 15:06:13 +0000226{
Rabin Vincentfb3c95f2010-05-05 09:23:07 +0530227 struct pl01x_regs *regs = pl01x_get_regs(portnum);
228
Stuart Wood26136ef2008-06-02 16:42:19 -0400229 WATCHDOG_RESET();
Rabin Vincentfb3c95f2010-05-05 09:23:07 +0530230 return !(readl(&regs->fr) & UART_PL01x_FR_RXFE);
wdenk4989f872004-03-14 15:06:13 +0000231}
Marek Vasut46e4d5f2012-09-14 22:38:46 +0200232
Marek Vasut46e4d5f2012-09-14 22:38:46 +0200233static struct serial_device pl01x_serial_drv = {
234 .name = "pl01x_serial",
235 .start = pl01x_serial_init,
236 .stop = NULL,
237 .setbrg = pl01x_serial_setbrg,
238 .putc = pl01x_serial_putc,
Marek Vasutd9c64492012-10-06 14:07:02 +0000239 .puts = default_serial_puts,
Marek Vasut46e4d5f2012-09-14 22:38:46 +0200240 .getc = pl01x_serial_getc,
241 .tstc = pl01x_serial_tstc,
242};
243
244void pl01x_serial_initialize(void)
245{
246 serial_register(&pl01x_serial_drv);
247}
248
249__weak struct serial_device *default_serial_console(void)
250{
251 return &pl01x_serial_drv;
252}