3 * Gary Jennejohn, DENX Software Engineering, <garyj@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
22 #include <linux/compiler.h>
23 #include <asm/arch/s3c24x0_cpu.h>
25 DECLARE_GLOBAL_DATA_PTR;
28 #define UART_NR S3C24X0_UART0
30 #elif defined(CONFIG_SERIAL2)
31 #define UART_NR S3C24X0_UART1
33 #elif defined(CONFIG_SERIAL3)
34 #define UART_NR S3C24X0_UART2
37 #error "Bad: you didn't configure serial ..."
42 #if defined(CONFIG_SERIAL_MULTI)
45 /* Multi serial device functions */
46 #define DECLARE_S3C_SERIAL_FUNCTIONS(port) \
47 int s3serial##port##_init(void) \
49 return serial_init_dev(port); \
51 void s3serial##port##_setbrg(void) \
53 serial_setbrg_dev(port); \
55 int s3serial##port##_getc(void) \
57 return serial_getc_dev(port); \
59 int s3serial##port##_tstc(void) \
61 return serial_tstc_dev(port); \
63 void s3serial##port##_putc(const char c) \
65 serial_putc_dev(port, c); \
67 void s3serial##port##_puts(const char *s) \
69 serial_puts_dev(port, s); \
72 #define INIT_S3C_SERIAL_STRUCTURE(port, name) { \
74 s3serial##port##_init, \
76 s3serial##port##_setbrg, \
77 s3serial##port##_getc, \
78 s3serial##port##_tstc, \
79 s3serial##port##_putc, \
80 s3serial##port##_puts, \
83 #endif /* CONFIG_SERIAL_MULTI */
89 void _serial_setbrg(const int dev_index)
91 struct s3c24x0_uart *uart = s3c24x0_get_base_uart(dev_index);
95 /* value is calculated so : (int)(PCLK/16./baudrate) -1 */
96 reg = get_PCLK() / (16 * gd->baudrate) - 1;
98 writel(reg, &uart->ubrdiv);
99 for (i = 0; i < 100; i++)
103 #if defined(CONFIG_SERIAL_MULTI)
104 static inline void serial_setbrg_dev(unsigned int dev_index)
106 _serial_setbrg(dev_index);
109 void serial_setbrg(void)
111 _serial_setbrg(UART_NR);
116 /* Initialise the serial port. The settings are always 8 data bits, no parity,
117 * 1 stop bit, no start bits.
119 static int serial_init_dev(const int dev_index)
121 struct s3c24x0_uart *uart = s3c24x0_get_base_uart(dev_index);
124 hwflow = 0; /* turned off by default */
127 /* FIFO enable, Tx/Rx FIFO clear */
128 writel(0x07, &uart->ufcon);
129 writel(0x0, &uart->umcon);
131 /* Normal,No parity,1 stop,8 bit */
132 writel(0x3, &uart->ulcon);
134 * tx=level,rx=edge,disable timeout int.,enable rx error int.,
135 * normal,interrupt or polling
137 writel(0x245, &uart->ucon);
140 writel(0x1, &uart->umcon); /* rts up */
143 /* FIXME: This is sooooooooooooooooooo ugly */
144 #if defined(CONFIG_ARCH_GTA02_v1) || defined(CONFIG_ARCH_GTA02_v2)
145 /* we need auto hw flow control on the gsm and gps port */
146 if (dev_index == 0 || dev_index == 1)
147 writel(0x10, &uart->umcon);
149 _serial_setbrg(dev_index);
154 #if !defined(CONFIG_SERIAL_MULTI)
155 /* Initialise the serial port. The settings are always 8 data bits, no parity,
156 * 1 stop bit, no start bits.
158 int serial_init(void)
160 return serial_init_dev(UART_NR);
165 * Read a single byte from the serial port. Returns 1 on success, 0
166 * otherwise. When the function is succesfull, the character read is
167 * written into its argument c.
169 int _serial_getc(const int dev_index)
171 struct s3c24x0_uart *uart = s3c24x0_get_base_uart(dev_index);
173 while (!(readl(&uart->utrstat) & 0x1))
174 /* wait for character to arrive */ ;
176 return readb(&uart->urxh) & 0xff;
179 #if defined(CONFIG_SERIAL_MULTI)
180 static inline int serial_getc_dev(unsigned int dev_index)
182 return _serial_getc(dev_index);
185 int serial_getc(void)
187 return _serial_getc(UART_NR);
192 int hwflow_onoff(int on)
197 break; /* return current */
199 hwflow = 1; /* turn on */
202 hwflow = 0; /* turn off */
209 #ifdef CONFIG_MODEM_SUPPORT
210 static int be_quiet = 0;
211 void disable_putc(void)
216 void enable_putc(void)
224 * Output a single byte to the serial port.
226 void _serial_putc(const char c, const int dev_index)
228 struct s3c24x0_uart *uart = s3c24x0_get_base_uart(dev_index);
229 #ifdef CONFIG_MODEM_SUPPORT
234 while (!(readl(&uart->utrstat) & 0x2))
235 /* wait for room in the tx FIFO */ ;
238 while (hwflow && !(readl(&uart->umstat) & 0x1))
239 /* Wait for CTS up */ ;
242 writeb(c, &uart->utxh);
244 /* If \n, also do \r */
249 #if defined(CONFIG_SERIAL_MULTI)
250 static inline void serial_putc_dev(unsigned int dev_index, const char c)
252 _serial_putc(c, dev_index);
255 void serial_putc(const char c)
257 _serial_putc(c, UART_NR);
263 * Test whether a character is in the RX buffer
265 int _serial_tstc(const int dev_index)
267 struct s3c24x0_uart *uart = s3c24x0_get_base_uart(dev_index);
269 return readl(&uart->utrstat) & 0x1;
272 #if defined(CONFIG_SERIAL_MULTI)
273 static inline int serial_tstc_dev(unsigned int dev_index)
275 return _serial_tstc(dev_index);
278 int serial_tstc(void)
280 return _serial_tstc(UART_NR);
284 void _serial_puts(const char *s, const int dev_index)
287 _serial_putc(*s++, dev_index);
291 #if defined(CONFIG_SERIAL_MULTI)
292 static inline void serial_puts_dev(int dev_index, const char *s)
294 _serial_puts(s, dev_index);
297 void serial_puts(const char *s)
299 _serial_puts(s, UART_NR);
303 #if defined(CONFIG_SERIAL_MULTI)
304 DECLARE_S3C_SERIAL_FUNCTIONS(0);
305 struct serial_device s3c24xx_serial0_device =
306 INIT_S3C_SERIAL_STRUCTURE(0, "s3ser0");
307 DECLARE_S3C_SERIAL_FUNCTIONS(1);
308 struct serial_device s3c24xx_serial1_device =
309 INIT_S3C_SERIAL_STRUCTURE(1, "s3ser1");
310 DECLARE_S3C_SERIAL_FUNCTIONS(2);
311 struct serial_device s3c24xx_serial2_device =
312 INIT_S3C_SERIAL_STRUCTURE(2, "s3ser2");
314 __weak struct serial_device *default_serial_console(void)
316 #if defined(CONFIG_SERIAL1)
317 return &s3c24xx_serial0_device;
318 #elif defined(CONFIG_SERIAL2)
319 return &s3c24xx_serial1_device;
320 #elif defined(CONFIG_SERIAL3)
321 return &s3c24xx_serial2_device;
323 #error "CONFIG_SERIAL? missing."
326 #endif /* CONFIG_SERIAL_MULTI */