Big white-space cleanup.
[platform/kernel/u-boot.git] / cpu / arm920t / imx / serial.c
1 /*
2  * (c) 2004 Sascha Hauer <sascha@saschahauer.de>
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
17  *
18  */
19
20 #include <common.h>
21 #if defined (CONFIG_IMX)
22
23 #include <asm/arch/imx-regs.h>
24
25 #ifndef CONFIG_IMX_SERIAL_NONE
26
27 #if defined CONFIG_IMX_SERIAL1
28 #define UART_BASE IMX_UART1_BASE
29 #elif defined CONFIG_IMX_SERIAL2
30 #define UART_BASE IMX_UART2_BASE
31 #else
32 #error "define CONFIG_IMX_SERIAL1, CONFIG_IMX_SERIAL2 or CONFIG_IMX_SERIAL_NONE"
33 #endif
34
35 struct imx_serial {
36         volatile uint32_t urxd[16];
37         volatile uint32_t utxd[16];
38         volatile uint32_t ucr1;
39         volatile uint32_t ucr2;
40         volatile uint32_t ucr3;
41         volatile uint32_t ucr4;
42         volatile uint32_t ufcr;
43         volatile uint32_t usr1;
44         volatile uint32_t usr2;
45         volatile uint32_t uesc;
46         volatile uint32_t utim;
47         volatile uint32_t ubir;
48         volatile uint32_t ubmr;
49         volatile uint32_t ubrc;
50         volatile uint32_t bipr[4];
51         volatile uint32_t bmpr[4];
52         volatile uint32_t uts;
53 };
54
55 void serial_setbrg (void)
56 {
57         serial_init();
58 }
59
60 extern void imx_gpio_mode(int gpio_mode);
61
62 /*
63  * Initialise the serial port with the given baudrate. The settings
64  * are always 8 data bits, no parity, 1 stop bit, no start bits.
65  *
66  */
67 int serial_init (void)
68 {
69         volatile struct imx_serial* base = (struct imx_serial *)UART_BASE;
70 #ifdef CONFIG_IMX_SERIAL1
71         imx_gpio_mode(PC11_PF_UART1_TXD);
72         imx_gpio_mode(PC12_PF_UART1_RXD);
73 #else
74         imx_gpio_mode(PB30_PF_UART2_TXD);
75         imx_gpio_mode(PB31_PF_UART2_RXD);
76 #endif
77
78         /* Disable UART */
79         base->ucr1 &= ~UCR1_UARTEN;
80
81         /* Set to default POR state */
82
83         base->ucr1 = 0x00000004;
84         base->ucr2 = 0x00000000;
85         base->ucr3 = 0x00000000;
86         base->ucr4 = 0x00008040;
87         base->uesc = 0x0000002B;
88         base->utim = 0x00000000;
89         base->ubir = 0x00000000;
90         base->ubmr = 0x00000000;
91         base->uts  = 0x00000000;
92         /* Set clocks */
93         base->ucr4 |= UCR4_REF16;
94
95         /* Configure FIFOs */
96         base->ufcr = 0xa81;
97
98         /* Set the numerator value minus one of the BRM ratio */
99         base->ubir = (CONFIG_BAUDRATE / 100) - 1;
100
101         /* Set the denominator value minus one of the BRM ratio */
102         base->ubmr = 10000 - 1;
103
104         /* Set to 8N1 */
105         base->ucr2 &= ~UCR2_PREN;
106         base->ucr2 |= UCR2_WS;
107         base->ucr2 &= ~UCR2_STPB;
108
109         /* Ignore RTS */
110         base->ucr2 |= UCR2_IRTS;
111
112         /* Enable UART */
113         base->ucr1 |= UCR1_UARTEN | UCR1_UARTCLKEN;
114
115         /* Enable FIFOs */
116         base->ucr2 |= UCR2_SRST | UCR2_RXEN | UCR2_TXEN;
117
118         /* Clear status flags */
119         base->usr2 |= USR2_ADET  |
120                   USR2_DTRF  |
121                   USR2_IDLE  |
122                   USR2_IRINT |
123                   USR2_WAKE  |
124                   USR2_RTSF  |
125                   USR2_BRCD  |
126                   USR2_ORE   |
127                   USR2_RDR;
128
129         /* Clear status flags */
130         base->usr1 |= USR1_PARITYERR |
131                   USR1_RTSD      |
132                   USR1_ESCF      |
133                   USR1_FRAMERR   |
134                   USR1_AIRINT    |
135                   USR1_AWAKE;
136         return (0);
137 }
138
139 /*
140  * Read a single byte from the serial port. Returns 1 on success, 0
141  * otherwise. When the function is successful, the character read is
142  * written into its argument c.
143  */
144 int serial_getc (void)
145 {
146         volatile struct imx_serial* base = (struct imx_serial *)UART_BASE;
147         unsigned char ch;
148
149         while(base->uts & UTS_RXEMPTY);
150
151         ch = (char)base->urxd[0];
152
153         return ch;
154 }
155
156 #ifdef CONFIG_HWFLOW
157 static int hwflow = 0; /* turned off by default */
158 int hwflow_onoff(int on)
159 {
160 }
161 #endif
162
163 /*
164  * Output a single byte to the serial port.
165  */
166 void serial_putc (const char c)
167 {
168         volatile struct imx_serial* base = (struct imx_serial *)UART_BASE;
169
170         /* Wait for Tx FIFO not full */
171         while (base->uts & UTS_TXFULL);
172
173         base->utxd[0] = c;
174
175         /* If \n, also do \r */
176         if (c == '\n')
177                 serial_putc ('\r');
178 }
179
180 /*
181  * Test whether a character is in the RX buffer
182  */
183 int serial_tstc (void)
184 {
185         volatile struct imx_serial* base = (struct imx_serial *)UART_BASE;
186
187         /* If receive fifo is empty, return false */
188         if (base->uts & UTS_RXEMPTY)
189                 return 0;
190         return 1;
191 }
192
193 void
194 serial_puts (const char *s)
195 {
196         while (*s) {
197                 serial_putc (*s++);
198         }
199 }
200 #endif /* CONFIG_IMX_SERIAL_NONE */
201 #endif /* defined CONFIG_IMX */