]> git.sur5r.net Git - u-boot/blob - drivers/serial/serial_s3c24x0.c
ff35ce595567be4f11656ec7cb1243904f551b6b
[u-boot] / drivers / serial / serial_s3c24x0.c
1 /*
2  * (C) Copyright 2002
3  * Gary Jennejohn, DENX Software Engineering, <garyj@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 #include <linux/compiler.h>
23 #include <asm/arch/s3c24x0_cpu.h>
24
25 DECLARE_GLOBAL_DATA_PTR;
26
27 #ifdef CONFIG_SERIAL1
28 #define UART_NR S3C24X0_UART0
29
30 #elif defined(CONFIG_SERIAL2)
31 #define UART_NR S3C24X0_UART1
32
33 #elif defined(CONFIG_SERIAL3)
34 #define UART_NR S3C24X0_UART2
35
36 #else
37 #error "Bad: you didn't configure serial ..."
38 #endif
39
40 #include <asm/io.h>
41
42 #if defined(CONFIG_SERIAL_MULTI)
43 #include <serial.h>
44
45 /* Multi serial device functions */
46 #define DECLARE_S3C_SERIAL_FUNCTIONS(port) \
47         int s3serial##port##_init(void) \
48         { \
49                 return serial_init_dev(port); \
50         } \
51         void s3serial##port##_setbrg(void) \
52         { \
53                 serial_setbrg_dev(port); \
54         } \
55         int s3serial##port##_getc(void) \
56         { \
57                 return serial_getc_dev(port); \
58         } \
59         int s3serial##port##_tstc(void) \
60         { \
61                 return serial_tstc_dev(port); \
62         } \
63         void s3serial##port##_putc(const char c) \
64         { \
65                 serial_putc_dev(port, c); \
66         } \
67         void s3serial##port##_puts(const char *s) \
68         { \
69                 serial_puts_dev(port, s); \
70         }
71
72 #define INIT_S3C_SERIAL_STRUCTURE(port, name, bus) { \
73         name, \
74         bus, \
75         s3serial##port##_init, \
76         NULL,\
77         s3serial##port##_setbrg, \
78         s3serial##port##_getc, \
79         s3serial##port##_tstc, \
80         s3serial##port##_putc, \
81         s3serial##port##_puts, \
82 }
83
84 #endif /* CONFIG_SERIAL_MULTI */
85
86 #ifdef CONFIG_HWFLOW
87 static int hwflow;
88 #endif
89
90 void _serial_setbrg(const int dev_index)
91 {
92         struct s3c24x0_uart *uart = s3c24x0_get_base_uart(dev_index);
93         unsigned int reg = 0;
94         int i;
95
96         /* value is calculated so : (int)(PCLK/16./baudrate) -1 */
97         reg = get_PCLK() / (16 * gd->baudrate) - 1;
98
99         writel(reg, &uart->ubrdiv);
100         for (i = 0; i < 100; i++)
101                 /* Delay */ ;
102 }
103
104 #if defined(CONFIG_SERIAL_MULTI)
105 static inline void serial_setbrg_dev(unsigned int dev_index)
106 {
107         _serial_setbrg(dev_index);
108 }
109 #else
110 void serial_setbrg(void)
111 {
112         _serial_setbrg(UART_NR);
113 }
114 #endif
115
116
117 /* Initialise the serial port. The settings are always 8 data bits, no parity,
118  * 1 stop bit, no start bits.
119  */
120 static int serial_init_dev(const int dev_index)
121 {
122         struct s3c24x0_uart *uart = s3c24x0_get_base_uart(dev_index);
123
124 #ifdef CONFIG_HWFLOW
125         hwflow = 0;     /* turned off by default */
126 #endif
127
128         /* FIFO enable, Tx/Rx FIFO clear */
129         writel(0x07, &uart->ufcon);
130         writel(0x0, &uart->umcon);
131
132         /* Normal,No parity,1 stop,8 bit */
133         writel(0x3, &uart->ulcon);
134         /*
135          * tx=level,rx=edge,disable timeout int.,enable rx error int.,
136          * normal,interrupt or polling
137          */
138         writel(0x245, &uart->ucon);
139
140 #ifdef CONFIG_HWFLOW
141         writel(0x1, &uart->umcon);      /* rts up */
142 #endif
143
144         /* FIXME: This is sooooooooooooooooooo ugly */
145 #if defined(CONFIG_ARCH_GTA02_v1) || defined(CONFIG_ARCH_GTA02_v2)
146         /* we need auto hw flow control on the gsm and gps port */
147         if (dev_index == 0 || dev_index == 1)
148                 writel(0x10, &uart->umcon);
149 #endif
150         _serial_setbrg(dev_index);
151
152         return (0);
153 }
154
155 #if !defined(CONFIG_SERIAL_MULTI)
156 /* Initialise the serial port. The settings are always 8 data bits, no parity,
157  * 1 stop bit, no start bits.
158  */
159 int serial_init(void)
160 {
161         return serial_init_dev(UART_NR);
162 }
163 #endif
164
165 /*
166  * Read a single byte from the serial port. Returns 1 on success, 0
167  * otherwise. When the function is succesfull, the character read is
168  * written into its argument c.
169  */
170 int _serial_getc(const int dev_index)
171 {
172         struct s3c24x0_uart *uart = s3c24x0_get_base_uart(dev_index);
173
174         while (!(readl(&uart->utrstat) & 0x1))
175                 /* wait for character to arrive */ ;
176
177         return readb(&uart->urxh) & 0xff;
178 }
179
180 #if defined(CONFIG_SERIAL_MULTI)
181 static inline int serial_getc_dev(unsigned int dev_index)
182 {
183         return _serial_getc(dev_index);
184 }
185 #else
186 int serial_getc(void)
187 {
188         return _serial_getc(UART_NR);
189 }
190 #endif
191
192 #ifdef CONFIG_HWFLOW
193 int hwflow_onoff(int on)
194 {
195         switch (on) {
196         case 0:
197         default:
198                 break;          /* return current */
199         case 1:
200                 hwflow = 1;     /* turn on */
201                 break;
202         case -1:
203                 hwflow = 0;     /* turn off */
204                 break;
205         }
206         return hwflow;
207 }
208 #endif
209
210 #ifdef CONFIG_MODEM_SUPPORT
211 static int be_quiet = 0;
212 void disable_putc(void)
213 {
214         be_quiet = 1;
215 }
216
217 void enable_putc(void)
218 {
219         be_quiet = 0;
220 }
221 #endif
222
223
224 /*
225  * Output a single byte to the serial port.
226  */
227 void _serial_putc(const char c, const int dev_index)
228 {
229         struct s3c24x0_uart *uart = s3c24x0_get_base_uart(dev_index);
230 #ifdef CONFIG_MODEM_SUPPORT
231         if (be_quiet)
232                 return;
233 #endif
234
235         while (!(readl(&uart->utrstat) & 0x2))
236                 /* wait for room in the tx FIFO */ ;
237
238 #ifdef CONFIG_HWFLOW
239         while (hwflow && !(readl(&uart->umstat) & 0x1))
240                 /* Wait for CTS up */ ;
241 #endif
242
243         writeb(c, &uart->utxh);
244
245         /* If \n, also do \r */
246         if (c == '\n')
247                 serial_putc('\r');
248 }
249
250 #if defined(CONFIG_SERIAL_MULTI)
251 static inline void serial_putc_dev(unsigned int dev_index, const char c)
252 {
253         _serial_putc(c, dev_index);
254 }
255 #else
256 void serial_putc(const char c)
257 {
258         _serial_putc(c, UART_NR);
259 }
260 #endif
261
262
263 /*
264  * Test whether a character is in the RX buffer
265  */
266 int _serial_tstc(const int dev_index)
267 {
268         struct s3c24x0_uart *uart = s3c24x0_get_base_uart(dev_index);
269
270         return readl(&uart->utrstat) & 0x1;
271 }
272
273 #if defined(CONFIG_SERIAL_MULTI)
274 static inline int serial_tstc_dev(unsigned int dev_index)
275 {
276         return _serial_tstc(dev_index);
277 }
278 #else
279 int serial_tstc(void)
280 {
281         return _serial_tstc(UART_NR);
282 }
283 #endif
284
285 void _serial_puts(const char *s, const int dev_index)
286 {
287         while (*s) {
288                 _serial_putc(*s++, dev_index);
289         }
290 }
291
292 #if defined(CONFIG_SERIAL_MULTI)
293 static inline void serial_puts_dev(int dev_index, const char *s)
294 {
295         _serial_puts(s, dev_index);
296 }
297 #else
298 void serial_puts(const char *s)
299 {
300         _serial_puts(s, UART_NR);
301 }
302 #endif
303
304 #if defined(CONFIG_SERIAL_MULTI)
305 DECLARE_S3C_SERIAL_FUNCTIONS(0);
306 struct serial_device s3c24xx_serial0_device =
307 INIT_S3C_SERIAL_STRUCTURE(0, "s3ser0", "S3UART1");
308 DECLARE_S3C_SERIAL_FUNCTIONS(1);
309 struct serial_device s3c24xx_serial1_device =
310 INIT_S3C_SERIAL_STRUCTURE(1, "s3ser1", "S3UART2");
311 DECLARE_S3C_SERIAL_FUNCTIONS(2);
312 struct serial_device s3c24xx_serial2_device =
313 INIT_S3C_SERIAL_STRUCTURE(2, "s3ser2", "S3UART3");
314
315 __weak struct serial_device *default_serial_console(void)
316 {
317 #if defined(CONFIG_SERIAL1)
318         return &s3c24xx_serial0_device;
319 #elif defined(CONFIG_SERIAL2)
320         return &s3c24xx_serial1_device;
321 #elif defined(CONFIG_SERIAL3)
322         return &s3c24xx_serial2_device;
323 #else
324 #error "CONFIG_SERIAL? missing."
325 #endif
326 }
327 #endif /* CONFIG_SERIAL_MULTI */