* Patch by Stephan Linz, 09 Mar 2004
[pandora-u-boot.git] / drivers / serial_pl011.c
1 /*
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
28 /* Simple U-Boot driver for the PrimeCell PL011 UARTs on the IntegratorCP */
29 /* Should be fairly simple to make it work with the PL010 as well */
30
31 #include <common.h>
32
33 #ifdef CFG_PL011_SERIAL
34
35 #include "serial_pl011.h"
36
37 #define IO_WRITE(addr, val) (*(volatile unsigned int *)(addr) = (val))
38 #define IO_READ(addr) (*(volatile unsigned int *)(addr))
39
40 /*
41  * IntegratorCP has two UARTs, use the first one, at 38400-8-N-1 
42  * Versatile PB has four UARTs.
43  */
44 #define NUM_PORTS 2
45 #define CONSOLE_PORT CONFIG_CONS_INDEX
46 #define baudRate CONFIG_BAUDRATE
47 static volatile unsigned char * const port[NUM_PORTS] = {(void*)(CFG_SERIAL0),
48                                                          (void*)(CFG_SERIAL1)};
49
50
51 static void pl011_putc(int portnum, char c);
52 static int pl011_getc(int portnum);
53 static int pl011_tstc(int portnum);
54
55
56 int serial_init (void)
57 {
58     unsigned int temp;
59     unsigned int divider;
60     unsigned int remainder;
61     unsigned int fraction;
62
63     /*
64     ** First, disable everything.
65     */
66     IO_WRITE(port[CONSOLE_PORT] + UART_PL011_CR, 0x0);
67
68     /*
69     ** Set baud rate
70     **
71     ** IBRD = UART_CLK / (16 * BAUD_RATE)
72     ** FBRD = ROUND((64 * MOD(UART_CLK,(16 * BAUD_RATE))) / (16 * BAUD_RATE))
73     */
74 #ifdef CONFIG_VERSATILE
75     temp      = 16 * baudRate;
76     divider   = 24000000 / temp;
77     remainder = 24000000 % temp;
78     temp      = (8 * remainder) / baudRate;
79     fraction  = (temp >> 1) + (temp & 1);
80 #endif
81 #ifdef CONFIG_INTEGRATOR
82     temp      = 16 * baudRate;
83     divider   = 14745600 / temp;
84     remainder = 14745600 % temp;
85     temp      = (8 * remainder) / baudRate;
86     fraction  = (temp >> 1) + (temp & 1);
87 #endif
88  
89     IO_WRITE(port[CONSOLE_PORT] + UART_PL011_IBRD, divider);
90     IO_WRITE(port[CONSOLE_PORT] + UART_PL011_FBRD, fraction);
91
92     /*
93     ** Set the UART to be 8 bits, 1 stop bit, no parity, fifo enabled.
94     */
95     IO_WRITE(port[CONSOLE_PORT] + UART_PL011_LCRH,
96         (UART_PL011_LCRH_WLEN_8 | UART_PL011_LCRH_FEN));
97
98     /*
99     ** Finally, enable the UART
100     */
101     IO_WRITE(port[CONSOLE_PORT] + UART_PL011_CR,
102         (UART_PL011_CR_UARTEN | UART_PL011_CR_TXE | UART_PL011_CR_RXE));
103
104     return (0);
105 }
106
107 void
108 serial_putc(const char c)
109 {
110         if (c == '\n')
111                 pl011_putc(CONSOLE_PORT, '\r');
112
113         pl011_putc(CONSOLE_PORT, c);
114 }
115
116 void
117 serial_puts (const char *s)
118 {
119         while (*s) {
120                 serial_putc (*s++);
121         }
122 }
123
124 int
125 serial_getc(void)
126 {
127         return pl011_getc(CONSOLE_PORT);
128 }
129
130 int
131 serial_tstc(void)
132 {
133         return pl011_tstc(CONSOLE_PORT);
134 }
135
136 void
137 serial_setbrg (void)
138 {
139 }
140
141 static void pl011_putc(int portnum, char c)
142 {
143     /* Wait until there is space in the FIFO */
144     while (IO_READ(port[portnum] + UART_PL01x_FR) & UART_PL01x_FR_TXFF);
145     
146     /* Send the character */
147     IO_WRITE(port[portnum] + UART_PL01x_DR, c);
148 }
149
150 static int pl011_getc(int portnum)
151 {
152     unsigned int data;
153
154     /* Wait until there is data in the FIFO */
155     while (IO_READ(port[portnum] + UART_PL01x_FR) & UART_PL01x_FR_RXFE);
156     
157     data = IO_READ(port[portnum] + UART_PL01x_DR);
158     
159     /* Check for an error flag */
160     if (data & 0xFFFFFF00)
161     {
162         /* Clear the error */
163         IO_WRITE(port[portnum] + UART_PL01x_ECR, 0xFFFFFFFF);
164         return -1;
165     }
166     
167     return (int)data;
168 }
169
170 static int pl011_tstc(int portnum)
171 {
172     return !(IO_READ(port[portnum] + UART_PL01x_FR) & UART_PL01x_FR_RXFE);
173 }
174
175 #endif