blob: 5dfcde8774700b4b2fdd8319003f92bcf07394ec [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;
114
Matt Waddeld6ce53e2010-10-07 15:48:46 -0600115 /* First, disable everything */
Rabin Vincentfb3c95f2010-05-05 09:23:07 +0530116 writel(0, &regs->pl011_cr);
Andreas Engel80438612008-09-08 10:17:31 +0200117
118 /*
Matt Waddeld6ce53e2010-10-07 15:48:46 -0600119 * Set baud rate
120 *
121 * IBRD = UART_CLK / (16 * BAUD_RATE)
122 * FBRD = RND((64 * MOD(UART_CLK,(16 * BAUD_RATE))) / (16 * BAUD_RATE))
Andreas Engel80438612008-09-08 10:17:31 +0200123 */
Matt Waddeld6ce53e2010-10-07 15:48:46 -0600124 temp = 16 * baudrate;
Andreas Engel80438612008-09-08 10:17:31 +0200125 divider = CONFIG_PL011_CLOCK / temp;
126 remainder = CONFIG_PL011_CLOCK % temp;
Matt Waddeld6ce53e2010-10-07 15:48:46 -0600127 temp = (8 * remainder) / baudrate;
Andreas Engel80438612008-09-08 10:17:31 +0200128 fraction = (temp >> 1) + (temp & 1);
129
Rabin Vincentfb3c95f2010-05-05 09:23:07 +0530130 writel(divider, &regs->pl011_ibrd);
131 writel(fraction, &regs->pl011_fbrd);
Andreas Engel80438612008-09-08 10:17:31 +0200132
Matt Waddeld6ce53e2010-10-07 15:48:46 -0600133 /* Set the UART to be 8 bits, 1 stop bit, no parity, fifo enabled */
Rabin Vincentfb3c95f2010-05-05 09:23:07 +0530134 writel(UART_PL011_LCRH_WLEN_8 | UART_PL011_LCRH_FEN,
135 &regs->pl011_lcrh);
Andreas Engel80438612008-09-08 10:17:31 +0200136
Matt Waddeld6ce53e2010-10-07 15:48:46 -0600137 /* Finally, enable the UART */
Rabin Vincentfb3c95f2010-05-05 09:23:07 +0530138 writel(UART_PL011_CR_UARTEN | UART_PL011_CR_TXE | UART_PL011_CR_RXE,
139 &regs->pl011_cr);
Andreas Engel80438612008-09-08 10:17:31 +0200140
141 return 0;
142}
143
Andreas Engel0813b122008-09-08 14:30:53 +0200144#endif /* CONFIG_PL011_SERIAL */
Andreas Engel80438612008-09-08 10:17:31 +0200145
wdenkc35ba4e2004-03-14 22:25:36 +0000146void serial_putc (const char c)
wdenk4989f872004-03-14 15:06:13 +0000147{
148 if (c == '\n')
Andreas Engel80438612008-09-08 10:17:31 +0200149 pl01x_putc (CONSOLE_PORT, '\r');
wdenk4989f872004-03-14 15:06:13 +0000150
Andreas Engel80438612008-09-08 10:17:31 +0200151 pl01x_putc (CONSOLE_PORT, c);
wdenk4989f872004-03-14 15:06:13 +0000152}
153
wdenkc35ba4e2004-03-14 22:25:36 +0000154void serial_puts (const char *s)
wdenk4989f872004-03-14 15:06:13 +0000155{
156 while (*s) {
157 serial_putc (*s++);
158 }
159}
160
wdenkc35ba4e2004-03-14 22:25:36 +0000161int serial_getc (void)
wdenk4989f872004-03-14 15:06:13 +0000162{
Andreas Engel80438612008-09-08 10:17:31 +0200163 return pl01x_getc (CONSOLE_PORT);
wdenk4989f872004-03-14 15:06:13 +0000164}
165
wdenkc35ba4e2004-03-14 22:25:36 +0000166int serial_tstc (void)
wdenk4989f872004-03-14 15:06:13 +0000167{
Andreas Engel80438612008-09-08 10:17:31 +0200168 return pl01x_tstc (CONSOLE_PORT);
wdenk4989f872004-03-14 15:06:13 +0000169}
170
wdenkc35ba4e2004-03-14 22:25:36 +0000171void serial_setbrg (void)
wdenk4989f872004-03-14 15:06:13 +0000172{
Matt Waddeld6ce53e2010-10-07 15:48:46 -0600173 baudrate = gd->baudrate;
174 serial_init();
wdenk4989f872004-03-14 15:06:13 +0000175}
176
Andreas Engel80438612008-09-08 10:17:31 +0200177static void pl01x_putc (int portnum, char c)
wdenk4989f872004-03-14 15:06:13 +0000178{
Rabin Vincentfb3c95f2010-05-05 09:23:07 +0530179 struct pl01x_regs *regs = pl01x_get_regs(portnum);
180
wdenkc35ba4e2004-03-14 22:25:36 +0000181 /* Wait until there is space in the FIFO */
Rabin Vincentfb3c95f2010-05-05 09:23:07 +0530182 while (readl(&regs->fr) & UART_PL01x_FR_TXFF)
Stuart Wood26136ef2008-06-02 16:42:19 -0400183 WATCHDOG_RESET();
wdenkc35ba4e2004-03-14 22:25:36 +0000184
185 /* Send the character */
Rabin Vincentfb3c95f2010-05-05 09:23:07 +0530186 writel(c, &regs->dr);
wdenk4989f872004-03-14 15:06:13 +0000187}
188
Andreas Engel80438612008-09-08 10:17:31 +0200189static int pl01x_getc (int portnum)
wdenk4989f872004-03-14 15:06:13 +0000190{
Rabin Vincentfb3c95f2010-05-05 09:23:07 +0530191 struct pl01x_regs *regs = pl01x_get_regs(portnum);
wdenkc35ba4e2004-03-14 22:25:36 +0000192 unsigned int data;
193
194 /* Wait until there is data in the FIFO */
Rabin Vincentfb3c95f2010-05-05 09:23:07 +0530195 while (readl(&regs->fr) & UART_PL01x_FR_RXFE)
Stuart Wood26136ef2008-06-02 16:42:19 -0400196 WATCHDOG_RESET();
wdenk4989f872004-03-14 15:06:13 +0000197
Rabin Vincentfb3c95f2010-05-05 09:23:07 +0530198 data = readl(&regs->dr);
wdenkc35ba4e2004-03-14 22:25:36 +0000199
200 /* Check for an error flag */
201 if (data & 0xFFFFFF00) {
202 /* Clear the error */
Rabin Vincentfb3c95f2010-05-05 09:23:07 +0530203 writel(0xFFFFFFFF, &regs->ecr);
wdenkc35ba4e2004-03-14 22:25:36 +0000204 return -1;
205 }
206
207 return (int) data;
wdenk4989f872004-03-14 15:06:13 +0000208}
209
Andreas Engel80438612008-09-08 10:17:31 +0200210static int pl01x_tstc (int portnum)
wdenk4989f872004-03-14 15:06:13 +0000211{
Rabin Vincentfb3c95f2010-05-05 09:23:07 +0530212 struct pl01x_regs *regs = pl01x_get_regs(portnum);
213
Stuart Wood26136ef2008-06-02 16:42:19 -0400214 WATCHDOG_RESET();
Rabin Vincentfb3c95f2010-05-05 09:23:07 +0530215 return !(readl(&regs->fr) & UART_PL01x_FR_RXFE);
wdenk4989f872004-03-14 15:06:13 +0000216}