Enable hardware flow control for GSM/GPS
[u-boot-openmoko/mini2440.git] / cpu / arm920t / s3c24x0 / serial.c
blob032390e63d8a648bef3190074440c34124d44259
1 /*
2 * (C) Copyright 2002
3 * Gary Jennejohn, DENX Software Engineering, <gj@denx.de>
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.
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.
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
21 #include <common.h>
22 #if defined(CONFIG_S3C2400) || defined (CONFIG_S3C2410) || \
23 defined(CONFIG_S3C2440) || defined (CONFIG_S3C2442) || \
24 defined (CONFIG_TRAB)
26 #if defined(CONFIG_S3C2400) || defined(CONFIG_TRAB)
27 #include <s3c2400.h>
28 #elif defined(CONFIG_S3C2410)
29 #include <s3c2410.h>
30 #elif defined(CONFIG_S3C2440) || defined(CONFIG_S3C2442)
31 #include <s3c2440.h>
32 #endif
34 DECLARE_GLOBAL_DATA_PTR;
36 #ifdef CONFIG_SERIAL1
37 #define UART_NR S3C24X0_UART0
39 #elif defined(CONFIG_SERIAL2)
40 # if defined(CONFIG_TRAB)
41 # error "TRAB supports only CONFIG_SERIAL1"
42 # endif
43 #define UART_NR S3C24X0_UART1
45 #elif defined(CONFIG_SERIAL3)
46 # if defined(CONFIG_TRAB)
47 # #error "TRAB supports only CONFIG_SERIAL1"
48 # endif
49 #define UART_NR S3C24X0_UART2
51 #else
52 #error "Bad: you didn't configure serial ..."
53 #endif
55 #if defined(CONFIG_SERIAL_MULTI)
56 #include <serial.h>
58 /* Multi serial device functions */
59 #define DECLARE_S3C_SERIAL_FUNCTIONS(port) \
60 int s3serial##port##_init (void) {\
61 return serial_init_dev(port);}\
62 void s3serial##port##_setbrg (void) {\
63 serial_setbrg_dev(port);}\
64 int s3serial##port##_getc (void) {\
65 return serial_getc_dev(port);}\
66 int s3serial##port##_tstc (void) {\
67 return serial_tstc_dev(port);}\
68 void s3serial##port##_putc (const char c) {\
69 serial_putc_dev(port, c);}\
70 void s3serial##port##_puts (const char *s) {\
71 serial_puts_dev(port, s);}
73 #define INIT_S3C_SERIAL_STRUCTURE(port,name,bus) {\
74 name,\
75 bus,\
76 s3serial##port##_init,\
77 s3serial##port##_setbrg,\
78 s3serial##port##_getc,\
79 s3serial##port##_tstc,\
80 s3serial##port##_putc,\
81 s3serial##port##_puts, }
83 #endif /* CONFIG_SERIAL_MULTI */
85 void _serial_setbrg(const int dev_index)
87 S3C24X0_UART * const uart = S3C24X0_GetBase_UART(dev_index);
88 unsigned int reg = 0;
89 int i;
91 /* value is calculated so : (int)(PCLK/16./baudrate) -1 */
92 reg = get_PCLK() / (16 * gd->baudrate) - 1;
94 uart->UBRDIV = reg;
95 for (i = 0; i < 100; i++);
97 #if defined(CONFIG_SERIAL_MULTI)
98 static inline void
99 serial_setbrg_dev(unsigned int dev_index)
101 _serial_setbrg(dev_index);
103 #else
104 void serial_setbrg(void)
106 _serial_setbrg(UART_NR);
108 #endif
111 /* Initialise the serial port. The settings are always 8 data bits, no parity,
112 * 1 stop bit, no start bits.
114 static int serial_init_dev(const int dev_index)
116 S3C24X0_UART * const uart = S3C24X0_GetBase_UART(dev_index);
118 /* FIFO enable, Tx/Rx FIFO clear */
119 uart->UFCON = 0x07;
120 uart->UMCON = 0x0;
122 /* Normal,No parity,1 stop,8 bit */
123 uart->ULCON = 0x3;
125 * tx=level,rx=edge,disable timeout int.,enable rx error int.,
126 * normal,interrupt or polling
128 uart->UCON = 0x245;
130 #ifdef CONFIG_HWFLOW
131 uart->UMCON = 0x1; /* RTS up */
132 #endif
134 /* FIXME: This is sooooooooooooooooooo ugly */
135 #include <config.h>
136 #if defined(CONFIG_GTA02_REVISION)
137 /* we need auto hw flow control on the gsm and gps port */
138 if (dev_index == 0 || dev_index == 1)
139 uart->UMCON = 0x10;
140 #endif
141 _serial_setbrg(dev_index);
143 return (0);
146 #if !defined(CONFIG_SERIAL_MULTI)
147 /* Initialise the serial port. The settings are always 8 data bits, no parity,
148 * 1 stop bit, no start bits.
150 int serial_init (void)
152 return serial_init_dev(UART_NR);
154 #endif
157 * Read a single byte from the serial port. Returns 1 on success, 0
158 * otherwise. When the function is succesfull, the character read is
159 * written into its argument c.
161 int _serial_getc (const int dev_index)
163 S3C24X0_UART * const uart = S3C24X0_GetBase_UART(dev_index);
165 /* wait for character to arrive */
166 while (!(uart->UTRSTAT & 0x1));
168 return uart->URXH & 0xff;
170 #if defined(CONFIG_SERIAL_MULTI)
171 static inline int serial_getc_dev(unsigned int dev_index)
173 return _serial_getc(dev_index);
175 #else
176 int serial_getc (void)
178 return _serial_getc(UART_NR);
180 #endif
182 #ifdef CONFIG_HWFLOW
183 static int hwflow = 0; /* turned off by default */
184 int hwflow_onoff(int on)
186 switch(on) {
187 case 0:
188 default:
189 break; /* return current */
190 case 1:
191 hwflow = 1; /* turn on */
192 break;
193 case -1:
194 hwflow = 0; /* turn off */
195 break;
197 return hwflow;
199 #endif
201 #ifdef CONFIG_MODEM_SUPPORT
202 static int be_quiet = 0;
203 void disable_putc(void)
205 be_quiet = 1;
208 void enable_putc(void)
210 be_quiet = 0;
212 #endif
216 * Output a single byte to the serial port.
218 void _serial_putc (const char c, const int dev_index)
220 S3C24X0_UART * const uart = S3C24X0_GetBase_UART(dev_index);
221 #ifdef CONFIG_MODEM_SUPPORT
222 if (be_quiet)
223 return;
224 #endif
226 /* wait for room in the tx FIFO */
227 while (!(uart->UTRSTAT & 0x2));
229 #ifdef CONFIG_HWFLOW
230 /* Wait for CTS up */
231 while(hwflow && !(uart->UMSTAT & 0x1))
233 #endif
235 uart->UTXH = c;
237 /* If \n, also do \r */
238 if (c == '\n')
239 serial_putc ('\r');
241 #if defined(CONFIG_SERIAL_MULTI)
242 static inline void serial_putc_dev(unsigned int dev_index, const char c)
244 _serial_putc(c, dev_index);
246 #else
247 void serial_putc(const char c)
249 _serial_putc(c, UART_NR);
251 #endif
255 * Test whether a character is in the RX buffer
257 int _serial_tstc(const int dev_index)
259 S3C24X0_UART * const uart = S3C24X0_GetBase_UART(dev_index);
261 return uart->UTRSTAT & 0x1;
263 #if defined(CONFIG_SERIAL_MULTI)
264 static inline int
265 serial_tstc_dev(unsigned int dev_index)
267 return _serial_tstc(dev_index);
269 #else
270 int serial_tstc(void)
272 return _serial_tstc(UART_NR);
274 #endif
276 void _serial_puts(const char *s, const int dev_index)
278 while (*s) {
279 _serial_putc (*s++, dev_index);
282 #if defined(CONFIG_SERIAL_MULTI)
283 static inline void
284 serial_puts_dev(int dev_index, const char *s)
286 _serial_puts(s, dev_index);
288 #else
289 void
290 serial_puts (const char *s)
292 _serial_puts(s, UART_NR);
294 #endif
296 #if defined(CONFIG_SERIAL_MULTI)
297 DECLARE_S3C_SERIAL_FUNCTIONS(0);
298 struct serial_device s3c24xx_serial0_device =
299 INIT_S3C_SERIAL_STRUCTURE(0, "s3ser0", "S3UART1");
300 DECLARE_S3C_SERIAL_FUNCTIONS(1);
301 struct serial_device s3c24xx_serial1_device =
302 INIT_S3C_SERIAL_STRUCTURE(1, "s3ser1", "S3UART2");
303 DECLARE_S3C_SERIAL_FUNCTIONS(2);
304 struct serial_device s3c24xx_serial2_device =
305 INIT_S3C_SERIAL_STRUCTURE(2, "s3ser2", "S3UART3");
307 #endif /* CONFIG_SERIAL_MULTI */
309 #endif /* defined(CONFIG_S3C2400) || defined (CONFIG_S3C2410) ||
310 defined(CONFIG_S3C2440) || defined (CONFIG_S3C2442) ||
311 defined (CONFIG_TRAB) */