Merge branch 'master' of /home/wd/git/u-boot/custodians
[oweals/u-boot.git] / cpu / arm920t / s3c24x0 / serial.c
1 /*
2  * (C) Copyright 2002
3  * Gary Jennejohn, DENX Software Engineering, <gj@denx.de>
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18  *
19  */
20
21 #include <common.h>
22 #if defined(CONFIG_S3C2400) || defined (CONFIG_S3C2410) || defined (CONFIG_TRAB)
23
24 #if defined(CONFIG_S3C2400) || defined(CONFIG_TRAB)
25 #include <s3c2400.h>
26 #elif defined(CONFIG_S3C2410)
27 #include <s3c2410.h>
28 #endif
29
30 DECLARE_GLOBAL_DATA_PTR;
31
32 #ifdef CONFIG_SERIAL1
33 #define UART_NR S3C24X0_UART0
34
35 #elif defined(CONFIG_SERIAL2)
36 # if defined(CONFIG_TRAB)
37 #  error "TRAB supports only CONFIG_SERIAL1"
38 # endif
39 #define UART_NR S3C24X0_UART1
40
41 #elif defined(CONFIG_SERIAL3)
42 # if defined(CONFIG_TRAB)
43 #  #error "TRAB supports only CONFIG_SERIAL1"
44 # endif
45 #define UART_NR S3C24X0_UART2
46
47 #else
48 #error "Bad: you didn't configure serial ..."
49 #endif
50
51 #if defined(CONFIG_SERIAL_MULTI)
52 #include <serial.h>
53
54 /* Multi serial device functions */
55 #define DECLARE_S3C_SERIAL_FUNCTIONS(port) \
56     int  s3serial##port##_init (void) {\
57         return serial_init_dev(port);}\
58     void s3serial##port##_setbrg (void) {\
59         serial_setbrg_dev(port);}\
60     int  s3serial##port##_getc (void) {\
61         return serial_getc_dev(port);}\
62     int  s3serial##port##_tstc (void) {\
63         return serial_tstc_dev(port);}\
64     void s3serial##port##_putc (const char c) {\
65         serial_putc_dev(port, c);}\
66     void s3serial##port##_puts (const char *s) {\
67         serial_puts_dev(port, s);}
68
69 #define INIT_S3C_SERIAL_STRUCTURE(port,name,bus) {\
70         name,\
71         bus,\
72         s3serial##port##_init,\
73         s3serial##port##_setbrg,\
74         s3serial##port##_getc,\
75         s3serial##port##_tstc,\
76         s3serial##port##_putc,\
77         s3serial##port##_puts, }
78
79 #endif /* CONFIG_SERIAL_MULTI */
80
81 void _serial_setbrg(const int dev_index)
82 {
83         S3C24X0_UART * const uart = S3C24X0_GetBase_UART(dev_index);
84         unsigned int reg = 0;
85         int i;
86
87         /* value is calculated so : (int)(PCLK/16./baudrate) -1 */
88         reg = get_PCLK() / (16 * gd->baudrate) - 1;
89
90         uart->UBRDIV = reg;
91         for (i = 0; i < 100; i++);
92 }
93 #if defined(CONFIG_SERIAL_MULTI)
94 static inline void
95 serial_setbrg_dev(unsigned int dev_index)
96 {
97         _serial_setbrg(dev_index);
98 }
99 #else
100 void serial_setbrg(void)
101 {
102         _serial_setbrg(UART_NR);
103 }
104 #endif
105
106
107 /* Initialise the serial port. The settings are always 8 data bits, no parity,
108  * 1 stop bit, no start bits.
109  */
110 static int serial_init_dev(const int dev_index)
111 {
112         S3C24X0_UART * const uart = S3C24X0_GetBase_UART(dev_index);
113         int i;
114
115         /* FIFO enable, Tx/Rx FIFO clear */
116         uart->UFCON = 0x07;
117         uart->UMCON = 0x0;
118
119         /* Normal,No parity,1 stop,8 bit */
120         uart->ULCON = 0x3;
121         /*
122          * tx=level,rx=edge,disable timeout int.,enable rx error int.,
123          * normal,interrupt or polling
124          */
125         uart->UCON = 0x245;
126
127 #ifdef CONFIG_HWFLOW
128         uart->UMCON = 0x1; /* RTS up */
129 #endif
130
131         /* FIXME: This is sooooooooooooooooooo ugly */
132 #if defined(CONFIG_ARCH_GTA02_v1) || defined(CONFIG_ARCH_GTA02_v2)
133         /* we need auto hw flow control on the gsm and gps port */
134         if (dev_index == 0 || dev_index == 1)
135                 uart->UMCON = 0x10;
136 #endif
137         _serial_setbrg(dev_index);
138
139         return (0);
140 }
141
142 #if !defined(CONFIG_SERIAL_MULTI)
143 /* Initialise the serial port. The settings are always 8 data bits, no parity,
144  * 1 stop bit, no start bits.
145  */
146 int serial_init (void)
147 {
148         return serial_init_dev(UART_NR);
149 }
150 #endif
151
152 /*
153  * Read a single byte from the serial port. Returns 1 on success, 0
154  * otherwise. When the function is succesfull, the character read is
155  * written into its argument c.
156  */
157 int _serial_getc (const int dev_index)
158 {
159         S3C24X0_UART * const uart = S3C24X0_GetBase_UART(dev_index);
160
161         /* wait for character to arrive */
162         while (!(uart->UTRSTAT & 0x1));
163
164         return uart->URXH & 0xff;
165 }
166 #if defined(CONFIG_SERIAL_MULTI)
167 static inline int serial_getc_dev(unsigned int dev_index)
168 {
169         return _serial_getc(dev_index);
170 }
171 #else
172 int serial_getc (void)
173 {
174         return _serial_getc(UART_NR);
175 }
176 #endif
177
178 #ifdef CONFIG_HWFLOW
179 static int hwflow = 0; /* turned off by default */
180 int hwflow_onoff(int on)
181 {
182         switch(on) {
183         case 0:
184         default:
185                 break; /* return current */
186         case 1:
187                 hwflow = 1; /* turn on */
188                 break;
189         case -1:
190                 hwflow = 0; /* turn off */
191                 break;
192         }
193         return hwflow;
194 }
195 #endif
196
197 #ifdef CONFIG_MODEM_SUPPORT
198 static int be_quiet = 0;
199 void disable_putc(void)
200 {
201         be_quiet = 1;
202 }
203
204 void enable_putc(void)
205 {
206         be_quiet = 0;
207 }
208 #endif
209
210
211 /*
212  * Output a single byte to the serial port.
213  */
214 void _serial_putc (const char c, const int dev_index)
215 {
216         S3C24X0_UART * const uart = S3C24X0_GetBase_UART(dev_index);
217 #ifdef CONFIG_MODEM_SUPPORT
218         if (be_quiet)
219                 return;
220 #endif
221
222         /* wait for room in the tx FIFO */
223         while (!(uart->UTRSTAT & 0x2));
224
225 #ifdef CONFIG_HWFLOW
226         /* Wait for CTS up */
227         while(hwflow && !(uart->UMSTAT & 0x1))
228                 ;
229 #endif
230
231         uart->UTXH = c;
232
233         /* If \n, also do \r */
234         if (c == '\n')
235                 serial_putc ('\r');
236 }
237 #if defined(CONFIG_SERIAL_MULTI)
238 static inline void serial_putc_dev(unsigned int dev_index, const char c)
239 {
240         _serial_putc(c, dev_index);
241 }
242 #else
243 void serial_putc(const char c)
244 {
245         _serial_putc(c, UART_NR);
246 }
247 #endif
248
249
250 /*
251  * Test whether a character is in the RX buffer
252  */
253 int _serial_tstc(const int dev_index)
254 {
255         S3C24X0_UART * const uart = S3C24X0_GetBase_UART(dev_index);
256
257         return uart->UTRSTAT & 0x1;
258 }
259 #if defined(CONFIG_SERIAL_MULTI)
260 static inline int
261 serial_tstc_dev(unsigned int dev_index)
262 {
263         return _serial_tstc(dev_index);
264 }
265 #else
266 int serial_tstc(void)
267 {
268         return _serial_tstc(UART_NR);
269 }
270 #endif
271
272 void _serial_puts(const char *s, const int dev_index)
273 {
274         while (*s) {
275                 _serial_putc (*s++, dev_index);
276         }
277 }
278 #if defined(CONFIG_SERIAL_MULTI)
279 static inline void
280 serial_puts_dev(int dev_index, const char *s)
281 {
282         _serial_puts(s, dev_index);
283 }
284 #else
285 void
286 serial_puts (const char *s)
287 {
288         _serial_puts(s, UART_NR);
289 }
290 #endif
291
292 #if defined(CONFIG_SERIAL_MULTI)
293 DECLARE_S3C_SERIAL_FUNCTIONS(0);
294 struct serial_device s3c24xx_serial0_device =
295         INIT_S3C_SERIAL_STRUCTURE(0, "s3ser0", "S3UART1");
296 DECLARE_S3C_SERIAL_FUNCTIONS(1);
297 struct serial_device s3c24xx_serial1_device =
298         INIT_S3C_SERIAL_STRUCTURE(1, "s3ser1", "S3UART2");
299 DECLARE_S3C_SERIAL_FUNCTIONS(2);
300 struct serial_device s3c24xx_serial2_device =
301         INIT_S3C_SERIAL_STRUCTURE(2, "s3ser2", "S3UART3");
302
303 #endif /* CONFIG_SERIAL_MULTI */
304
305 #endif /* defined(CONFIG_S3C2400) || defined (CONFIG_S3C2410) || defined (CONFIG_TRAB) */