3 * Rob Taylor, Flying Pig Systems. robt@flyingpig.com.
7 * Philippe Robin, <philippe.robin@arm.com>
9 * See file CREDITS for list of people who contributed to this
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.
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.
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,
28 /* Simple U-Boot driver for the PrimeCell PL010/PL011 UARTs */
33 #include "serial_pl01x.h"
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.
40 #define CONSOLE_PORT CONFIG_CONS_INDEX
41 static volatile unsigned char *const port[] = CONFIG_PL01x_PORTS;
42 #define NUM_PORTS (sizeof(port)/sizeof(port[0]))
44 static void pl01x_putc (int portnum, char c);
45 static int pl01x_getc (int portnum);
46 static int pl01x_tstc (int portnum);
47 unsigned int baudrate = CONFIG_BAUDRATE;
48 DECLARE_GLOBAL_DATA_PTR;
50 #ifdef CONFIG_PL010_SERIAL
52 int serial_init (void)
56 /* First, disable everything */
57 writel(0x0, port[CONSOLE_PORT] + UART_PL010_CR);
62 divisor = UART_PL010_BAUD_9600;
66 divisor = UART_PL010_BAUD_9600;
70 divisor = UART_PL010_BAUD_38400;
74 divisor = UART_PL010_BAUD_57600;
78 divisor = UART_PL010_BAUD_115200;
82 divisor = UART_PL010_BAUD_38400;
85 writel(((divisor & 0xf00) >> 8), port[CONSOLE_PORT] + UART_PL010_LCRM);
86 writel((divisor & 0xff), port[CONSOLE_PORT] + UART_PL010_LCRL);
88 /* Set the UART to be 8 bits, 1 stop bit, no parity, fifo enabled */
89 writel((UART_PL010_LCRH_WLEN_8 | UART_PL010_LCRH_FEN),
90 port[CONSOLE_PORT] + UART_PL010_LCRH);
92 /* Finally, enable the UART */
93 writel((UART_PL010_CR_UARTEN), port[CONSOLE_PORT] + UART_PL010_CR);
98 #endif /* CONFIG_PL010_SERIAL */
100 #ifdef CONFIG_PL011_SERIAL
102 int serial_init (void)
105 unsigned int divider;
106 unsigned int remainder;
107 unsigned int fraction;
109 /* First, disable everything */
110 writel(0x0, port[CONSOLE_PORT] + UART_PL011_CR);
115 * IBRD = UART_CLK / (16 * BAUD_RATE)
116 * FBRD = RND((64 * MOD(UART_CLK,(16 * BAUD_RATE))) / (16 * BAUD_RATE))
118 temp = 16 * baudrate;
119 divider = CONFIG_PL011_CLOCK / temp;
120 remainder = CONFIG_PL011_CLOCK % temp;
121 temp = (8 * remainder) / baudrate;
122 fraction = (temp >> 1) + (temp & 1);
124 writel(divider, port[CONSOLE_PORT] + UART_PL011_IBRD);
125 writel(fraction, port[CONSOLE_PORT] + UART_PL011_FBRD);
127 /* Set the UART to be 8 bits, 1 stop bit, no parity, fifo enabled */
128 writel((UART_PL011_LCRH_WLEN_8 | UART_PL011_LCRH_FEN),
129 port[CONSOLE_PORT] + UART_PL011_LCRH);
131 /* Finally, enable the UART */
132 writel((UART_PL011_CR_UARTEN | UART_PL011_CR_TXE | UART_PL011_CR_RXE),
133 port[CONSOLE_PORT] + UART_PL011_CR);
138 #endif /* CONFIG_PL011_SERIAL */
140 void serial_putc (const char c)
143 pl01x_putc (CONSOLE_PORT, '\r');
145 pl01x_putc (CONSOLE_PORT, c);
148 void serial_puts (const char *s)
155 int serial_getc (void)
157 return pl01x_getc (CONSOLE_PORT);
160 int serial_tstc (void)
162 return pl01x_tstc (CONSOLE_PORT);
165 void serial_setbrg (void)
167 baudrate = gd->baudrate;
171 static void pl01x_putc (int portnum, char c)
173 /* Wait until there is space in the FIFO */
174 while (readl(port[portnum] + UART_PL01x_FR) & UART_PL01x_FR_TXFF)
177 /* Send the character */
178 writel(c, port[portnum] + UART_PL01x_DR);
181 static int pl01x_getc (int portnum)
185 /* Wait until there is data in the FIFO */
186 while (readl(port[portnum] + UART_PL01x_FR) & UART_PL01x_FR_RXFE)
189 data = readl(port[portnum] + UART_PL01x_DR);
191 /* Check for an error flag */
192 if (data & 0xFFFFFF00) {
193 /* Clear the error */
194 writel(0xFFFFFFFF, port[portnum] + UART_PL01x_ECR);
201 static int pl01x_tstc (int portnum)
204 return !(readl(port[portnum] + UART_PL01x_FR) &