blob: 7a064ffb24dfa169095e68c209bbbd286290989f [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 *
9 * See file CREDITS for list of people who contributed to this
10 * project.
11 *
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License as
14 * published by the Free Software Foundation; either version 2 of
15 * the License, or (at your option) any later version.
16 *
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
25 * MA 02111-1307 USA
26 */
27
Andreas Engel0813b122008-09-08 14:30:53 +020028/* Simple U-Boot driver for the PrimeCell PL010/PL011 UARTs */
wdenk4989f872004-03-14 15:06:13 +000029
30#include <common.h>
Stuart Wood26136ef2008-06-02 16:42:19 -040031#include <watchdog.h>
Matt Waddeld6ce53e2010-10-07 15:48:46 -060032#include <asm/io.h>
Andreas Engel80438612008-09-08 10:17:31 +020033#include "serial_pl01x.h"
wdenk4989f872004-03-14 15:06:13 +000034
Andreas Engel80438612008-09-08 10:17:31 +020035/*
36 * Integrator AP has two UARTs, we use the first one, at 38400-8-N-1
37 * Integrator CP has two UARTs, use the first one, at 38400-8-N-1
38 * Versatile PB has four UARTs.
39 */
wdenk4989f872004-03-14 15:06:13 +000040#define CONSOLE_PORT CONFIG_CONS_INDEX
wdenkda04a8b2004-08-02 23:22:59 +000041static volatile unsigned char *const port[] = CONFIG_PL01x_PORTS;
42#define NUM_PORTS (sizeof(port)/sizeof(port[0]))
wdenk4989f872004-03-14 15:06:13 +000043
Andreas Engel80438612008-09-08 10:17:31 +020044static void pl01x_putc (int portnum, char c);
45static int pl01x_getc (int portnum);
46static int pl01x_tstc (int portnum);
Matt Waddeld6ce53e2010-10-07 15:48:46 -060047unsigned int baudrate = CONFIG_BAUDRATE;
48DECLARE_GLOBAL_DATA_PTR;
wdenk4989f872004-03-14 15:06:13 +000049
Rabin Vincentfb3c95f2010-05-05 09:23:07 +053050static struct pl01x_regs *pl01x_get_regs(int portnum)
51{
52 return (struct pl01x_regs *) port[portnum];
53}
54
Andreas Engel0813b122008-09-08 14:30:53 +020055#ifdef CONFIG_PL010_SERIAL
wdenk4989f872004-03-14 15:06:13 +000056
57int serial_init (void)
58{
Rabin Vincentfb3c95f2010-05-05 09:23:07 +053059 struct pl01x_regs *regs = pl01x_get_regs(CONSOLE_PORT);
wdenkc35ba4e2004-03-14 22:25:36 +000060 unsigned int divisor;
wdenk4989f872004-03-14 15:06:13 +000061
Matt Waddeld6ce53e2010-10-07 15:48:46 -060062 /* First, disable everything */
Rabin Vincentfb3c95f2010-05-05 09:23:07 +053063 writel(0, &regs->pl010_cr);
wdenk4989f872004-03-14 15:06:13 +000064
Matt Waddeld6ce53e2010-10-07 15:48:46 -060065 /* Set baud rate */
66 switch (baudrate) {
wdenkc35ba4e2004-03-14 22:25:36 +000067 case 9600:
68 divisor = UART_PL010_BAUD_9600;
69 break;
wdenk4989f872004-03-14 15:06:13 +000070
wdenkc35ba4e2004-03-14 22:25:36 +000071 case 19200:
72 divisor = UART_PL010_BAUD_9600;
73 break;
wdenk4989f872004-03-14 15:06:13 +000074
wdenkc35ba4e2004-03-14 22:25:36 +000075 case 38400:
76 divisor = UART_PL010_BAUD_38400;
77 break;
wdenk4989f872004-03-14 15:06:13 +000078
wdenkc35ba4e2004-03-14 22:25:36 +000079 case 57600:
80 divisor = UART_PL010_BAUD_57600;
81 break;
wdenk4989f872004-03-14 15:06:13 +000082
wdenkc35ba4e2004-03-14 22:25:36 +000083 case 115200:
84 divisor = UART_PL010_BAUD_115200;
85 break;
wdenk4989f872004-03-14 15:06:13 +000086
wdenkc35ba4e2004-03-14 22:25:36 +000087 default:
88 divisor = UART_PL010_BAUD_38400;
89 }
90
Rabin Vincentfb3c95f2010-05-05 09:23:07 +053091 writel((divisor & 0xf00) >> 8, &regs->pl010_lcrm);
92 writel(divisor & 0xff, &regs->pl010_lcrl);
wdenk4989f872004-03-14 15:06:13 +000093
Matt Waddeld6ce53e2010-10-07 15:48:46 -060094 /* Set the UART to be 8 bits, 1 stop bit, no parity, fifo enabled */
Rabin Vincentfb3c95f2010-05-05 09:23:07 +053095 writel(UART_PL010_LCRH_WLEN_8 | UART_PL010_LCRH_FEN, &regs->pl010_lcrh);
wdenk4989f872004-03-14 15:06:13 +000096
Matt Waddeld6ce53e2010-10-07 15:48:46 -060097 /* Finally, enable the UART */
Rabin Vincentfb3c95f2010-05-05 09:23:07 +053098 writel(UART_PL010_CR_UARTEN, &regs->pl010_cr);
wdenk4989f872004-03-14 15:06:13 +000099
Andreas Engel80438612008-09-08 10:17:31 +0200100 return 0;
wdenk4989f872004-03-14 15:06:13 +0000101}
102
Andreas Engel0813b122008-09-08 14:30:53 +0200103#endif /* CONFIG_PL010_SERIAL */
Andreas Engel80438612008-09-08 10:17:31 +0200104
Andreas Engel0813b122008-09-08 14:30:53 +0200105#ifdef CONFIG_PL011_SERIAL
Andreas Engel80438612008-09-08 10:17:31 +0200106
107int serial_init (void)
108{
Rabin Vincentfb3c95f2010-05-05 09:23:07 +0530109 struct pl01x_regs *regs = pl01x_get_regs(CONSOLE_PORT);
Andreas Engel80438612008-09-08 10:17:31 +0200110 unsigned int temp;
111 unsigned int divider;
112 unsigned int remainder;
113 unsigned int fraction;
John Rigby34e21ee2011-04-19 10:42:39 +0000114 unsigned int lcr;
115
116#ifdef CONFIG_PL011_SERIAL_FLUSH_ON_INIT
117 /* Empty RX fifo if necessary */
118 if (readl(&regs->pl011_cr) & UART_PL011_CR_UARTEN) {
119 while (!(readl(&regs->fr) & UART_PL01x_FR_RXFE))
120 readl(&regs->dr);
121 }
122#endif
Andreas Engel80438612008-09-08 10:17:31 +0200123
Matt Waddeld6ce53e2010-10-07 15:48:46 -0600124 /* First, disable everything */
Rabin Vincentfb3c95f2010-05-05 09:23:07 +0530125 writel(0, &regs->pl011_cr);
Andreas Engel80438612008-09-08 10:17:31 +0200126
127 /*
Matt Waddeld6ce53e2010-10-07 15:48:46 -0600128 * Set baud rate
129 *
130 * IBRD = UART_CLK / (16 * BAUD_RATE)
131 * FBRD = RND((64 * MOD(UART_CLK,(16 * BAUD_RATE))) / (16 * BAUD_RATE))
Andreas Engel80438612008-09-08 10:17:31 +0200132 */
Matt Waddeld6ce53e2010-10-07 15:48:46 -0600133 temp = 16 * baudrate;
Andreas Engel80438612008-09-08 10:17:31 +0200134 divider = CONFIG_PL011_CLOCK / temp;
135 remainder = CONFIG_PL011_CLOCK % temp;
Matt Waddeld6ce53e2010-10-07 15:48:46 -0600136 temp = (8 * remainder) / baudrate;
Andreas Engel80438612008-09-08 10:17:31 +0200137 fraction = (temp >> 1) + (temp & 1);
138
Rabin Vincentfb3c95f2010-05-05 09:23:07 +0530139 writel(divider, &regs->pl011_ibrd);
140 writel(fraction, &regs->pl011_fbrd);
Andreas Engel80438612008-09-08 10:17:31 +0200141
Matt Waddeld6ce53e2010-10-07 15:48:46 -0600142 /* Set the UART to be 8 bits, 1 stop bit, no parity, fifo enabled */
John Rigby34e21ee2011-04-19 10:42:39 +0000143 lcr = UART_PL011_LCRH_WLEN_8 | UART_PL011_LCRH_FEN;
144 writel(lcr, &regs->pl011_lcrh);
145
146#ifdef CONFIG_PL011_SERIAL_RLCR
147 {
148 int i;
149
150 /*
151 * Program receive line control register after waiting
152 * 10 bus cycles. Delay be writing to readonly register
153 * 10 times
154 */
155 for (i = 0; i < 10; i++)
156 writel(lcr, &regs->fr);
Andreas Engel80438612008-09-08 10:17:31 +0200157
John Rigby34e21ee2011-04-19 10:42:39 +0000158 writel(lcr, &regs->pl011_rlcr);
159 }
160#endif
Matt Waddeld6ce53e2010-10-07 15:48:46 -0600161 /* Finally, enable the UART */
Rabin Vincentfb3c95f2010-05-05 09:23:07 +0530162 writel(UART_PL011_CR_UARTEN | UART_PL011_CR_TXE | UART_PL011_CR_RXE,
163 &regs->pl011_cr);
Andreas Engel80438612008-09-08 10:17:31 +0200164
165 return 0;
166}
167
Andreas Engel0813b122008-09-08 14:30:53 +0200168#endif /* CONFIG_PL011_SERIAL */
Andreas Engel80438612008-09-08 10:17:31 +0200169
wdenkc35ba4e2004-03-14 22:25:36 +0000170void serial_putc (const char c)
wdenk4989f872004-03-14 15:06:13 +0000171{
172 if (c == '\n')
Andreas Engel80438612008-09-08 10:17:31 +0200173 pl01x_putc (CONSOLE_PORT, '\r');
wdenk4989f872004-03-14 15:06:13 +0000174
Andreas Engel80438612008-09-08 10:17:31 +0200175 pl01x_putc (CONSOLE_PORT, c);
wdenk4989f872004-03-14 15:06:13 +0000176}
177
wdenkc35ba4e2004-03-14 22:25:36 +0000178void serial_puts (const char *s)
wdenk4989f872004-03-14 15:06:13 +0000179{
180 while (*s) {
181 serial_putc (*s++);
182 }
183}
184
wdenkc35ba4e2004-03-14 22:25:36 +0000185int serial_getc (void)
wdenk4989f872004-03-14 15:06:13 +0000186{
Andreas Engel80438612008-09-08 10:17:31 +0200187 return pl01x_getc (CONSOLE_PORT);
wdenk4989f872004-03-14 15:06:13 +0000188}
189
wdenkc35ba4e2004-03-14 22:25:36 +0000190int serial_tstc (void)
wdenk4989f872004-03-14 15:06:13 +0000191{
Andreas Engel80438612008-09-08 10:17:31 +0200192 return pl01x_tstc (CONSOLE_PORT);
wdenk4989f872004-03-14 15:06:13 +0000193}
194
wdenkc35ba4e2004-03-14 22:25:36 +0000195void serial_setbrg (void)
wdenk4989f872004-03-14 15:06:13 +0000196{
Matt Waddeld6ce53e2010-10-07 15:48:46 -0600197 baudrate = gd->baudrate;
198 serial_init();
wdenk4989f872004-03-14 15:06:13 +0000199}
200
Andreas Engel80438612008-09-08 10:17:31 +0200201static void pl01x_putc (int portnum, char c)
wdenk4989f872004-03-14 15:06:13 +0000202{
Rabin Vincentfb3c95f2010-05-05 09:23:07 +0530203 struct pl01x_regs *regs = pl01x_get_regs(portnum);
204
wdenkc35ba4e2004-03-14 22:25:36 +0000205 /* Wait until there is space in the FIFO */
Rabin Vincentfb3c95f2010-05-05 09:23:07 +0530206 while (readl(&regs->fr) & UART_PL01x_FR_TXFF)
Stuart Wood26136ef2008-06-02 16:42:19 -0400207 WATCHDOG_RESET();
wdenkc35ba4e2004-03-14 22:25:36 +0000208
209 /* Send the character */
Rabin Vincentfb3c95f2010-05-05 09:23:07 +0530210 writel(c, &regs->dr);
wdenk4989f872004-03-14 15:06:13 +0000211}
212
Andreas Engel80438612008-09-08 10:17:31 +0200213static int pl01x_getc (int portnum)
wdenk4989f872004-03-14 15:06:13 +0000214{
Rabin Vincentfb3c95f2010-05-05 09:23:07 +0530215 struct pl01x_regs *regs = pl01x_get_regs(portnum);
wdenkc35ba4e2004-03-14 22:25:36 +0000216 unsigned int data;
217
218 /* Wait until there is data in the FIFO */
Rabin Vincentfb3c95f2010-05-05 09:23:07 +0530219 while (readl(&regs->fr) & UART_PL01x_FR_RXFE)
Stuart Wood26136ef2008-06-02 16:42:19 -0400220 WATCHDOG_RESET();
wdenk4989f872004-03-14 15:06:13 +0000221
Rabin Vincentfb3c95f2010-05-05 09:23:07 +0530222 data = readl(&regs->dr);
wdenkc35ba4e2004-03-14 22:25:36 +0000223
224 /* Check for an error flag */
225 if (data & 0xFFFFFF00) {
226 /* Clear the error */
Rabin Vincentfb3c95f2010-05-05 09:23:07 +0530227 writel(0xFFFFFFFF, &regs->ecr);
wdenkc35ba4e2004-03-14 22:25:36 +0000228 return -1;
229 }
230
231 return (int) data;
wdenk4989f872004-03-14 15:06:13 +0000232}
233
Andreas Engel80438612008-09-08 10:17:31 +0200234static int pl01x_tstc (int portnum)
wdenk4989f872004-03-14 15:06:13 +0000235{
Rabin Vincentfb3c95f2010-05-05 09:23:07 +0530236 struct pl01x_regs *regs = pl01x_get_regs(portnum);
237
Stuart Wood26136ef2008-06-02 16:42:19 -0400238 WATCHDOG_RESET();
Rabin Vincentfb3c95f2010-05-05 09:23:07 +0530239 return !(readl(&regs->fr) & UART_PL01x_FR_RXFE);
wdenk4989f872004-03-14 15:06:13 +0000240}