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 <asm/arch/s3c24x0_cpu.h>
24 DECLARE_GLOBAL_DATA_PTR;
27 #define UART_NR S3C24X0_UART0
29 #elif defined(CONFIG_SERIAL2)
30 # if defined(CONFIG_TRAB)
31 # error "TRAB supports only CONFIG_SERIAL1"
33 #define UART_NR S3C24X0_UART1
35 #elif defined(CONFIG_SERIAL3)
36 # if defined(CONFIG_TRAB)
37 # error "TRAB supports only CONFIG_SERIAL1"
39 #define UART_NR S3C24X0_UART2
42 #error "Bad: you didn't configure serial ..."
47 #if defined(CONFIG_SERIAL_MULTI)
50 /* Multi serial device functions */
51 #define DECLARE_S3C_SERIAL_FUNCTIONS(port) \
52 int s3serial##port##_init(void) \
54 return serial_init_dev(port); \
56 void s3serial##port##_setbrg(void) \
58 serial_setbrg_dev(port); \
60 int s3serial##port##_getc(void) \
62 return serial_getc_dev(port); \
64 int s3serial##port##_tstc(void) \
66 return serial_tstc_dev(port); \
68 void s3serial##port##_putc(const char c) \
70 serial_putc_dev(port, c); \
72 void s3serial##port##_puts(const char *s) \
74 serial_puts_dev(port, s); \
77 #define INIT_S3C_SERIAL_STRUCTURE(port, name, bus) { \
80 s3serial##port##_init, \
82 s3serial##port##_setbrg, \
83 s3serial##port##_getc, \
84 s3serial##port##_tstc, \
85 s3serial##port##_putc, \
86 s3serial##port##_puts, \
89 #endif /* CONFIG_SERIAL_MULTI */
95 void _serial_setbrg(const int dev_index)
97 struct s3c24x0_uart *uart = s3c24x0_get_base_uart(dev_index);
101 /* value is calculated so : (int)(PCLK/16./baudrate) -1 */
102 reg = get_PCLK() / (16 * gd->baudrate) - 1;
104 writel(reg, &uart->ubrdiv);
105 for (i = 0; i < 100; i++)
109 #if defined(CONFIG_SERIAL_MULTI)
110 static inline void serial_setbrg_dev(unsigned int dev_index)
112 _serial_setbrg(dev_index);
115 void serial_setbrg(void)
117 _serial_setbrg(UART_NR);
122 /* Initialise the serial port. The settings are always 8 data bits, no parity,
123 * 1 stop bit, no start bits.
125 static int serial_init_dev(const int dev_index)
127 struct s3c24x0_uart *uart = s3c24x0_get_base_uart(dev_index);
130 hwflow = 0; /* turned off by default */
133 /* FIFO enable, Tx/Rx FIFO clear */
134 writel(0x07, &uart->ufcon);
135 writel(0x0, &uart->umcon);
137 /* Normal,No parity,1 stop,8 bit */
138 writel(0x3, &uart->ulcon);
140 * tx=level,rx=edge,disable timeout int.,enable rx error int.,
141 * normal,interrupt or polling
143 writel(0x245, &uart->ucon);
146 writel(0x1, &uart->umcon); /* rts up */
149 /* FIXME: This is sooooooooooooooooooo ugly */
150 #if defined(CONFIG_ARCH_GTA02_v1) || defined(CONFIG_ARCH_GTA02_v2)
151 /* we need auto hw flow control on the gsm and gps port */
152 if (dev_index == 0 || dev_index == 1)
153 writel(0x10, &uart->umcon);
155 _serial_setbrg(dev_index);
160 #if !defined(CONFIG_SERIAL_MULTI)
161 /* Initialise the serial port. The settings are always 8 data bits, no parity,
162 * 1 stop bit, no start bits.
164 int serial_init(void)
166 return serial_init_dev(UART_NR);
171 * Read a single byte from the serial port. Returns 1 on success, 0
172 * otherwise. When the function is succesfull, the character read is
173 * written into its argument c.
175 int _serial_getc(const int dev_index)
177 struct s3c24x0_uart *uart = s3c24x0_get_base_uart(dev_index);
179 while (!(readl(&uart->utrstat) & 0x1))
180 /* wait for character to arrive */ ;
182 return readb(&uart->urxh) & 0xff;
185 #if defined(CONFIG_SERIAL_MULTI)
186 static inline int serial_getc_dev(unsigned int dev_index)
188 return _serial_getc(dev_index);
191 int serial_getc(void)
193 return _serial_getc(UART_NR);
198 int hwflow_onoff(int on)
203 break; /* return current */
205 hwflow = 1; /* turn on */
208 hwflow = 0; /* turn off */
215 #ifdef CONFIG_MODEM_SUPPORT
216 static int be_quiet = 0;
217 void disable_putc(void)
222 void enable_putc(void)
230 * Output a single byte to the serial port.
232 void _serial_putc(const char c, const int dev_index)
234 struct s3c24x0_uart *uart = s3c24x0_get_base_uart(dev_index);
235 #ifdef CONFIG_MODEM_SUPPORT
240 while (!(readl(&uart->utrstat) & 0x2))
241 /* wait for room in the tx FIFO */ ;
244 while (hwflow && !(readl(&uart->umstat) & 0x1))
245 /* Wait for CTS up */ ;
248 writeb(c, &uart->utxh);
250 /* If \n, also do \r */
255 #if defined(CONFIG_SERIAL_MULTI)
256 static inline void serial_putc_dev(unsigned int dev_index, const char c)
258 _serial_putc(c, dev_index);
261 void serial_putc(const char c)
263 _serial_putc(c, UART_NR);
269 * Test whether a character is in the RX buffer
271 int _serial_tstc(const int dev_index)
273 struct s3c24x0_uart *uart = s3c24x0_get_base_uart(dev_index);
275 return readl(&uart->utrstat) & 0x1;
278 #if defined(CONFIG_SERIAL_MULTI)
279 static inline int serial_tstc_dev(unsigned int dev_index)
281 return _serial_tstc(dev_index);
284 int serial_tstc(void)
286 return _serial_tstc(UART_NR);
290 void _serial_puts(const char *s, const int dev_index)
293 _serial_putc(*s++, dev_index);
297 #if defined(CONFIG_SERIAL_MULTI)
298 static inline void serial_puts_dev(int dev_index, const char *s)
300 _serial_puts(s, dev_index);
303 void serial_puts(const char *s)
305 _serial_puts(s, UART_NR);
309 #if defined(CONFIG_SERIAL_MULTI)
310 DECLARE_S3C_SERIAL_FUNCTIONS(0);
311 struct serial_device s3c24xx_serial0_device =
312 INIT_S3C_SERIAL_STRUCTURE(0, "s3ser0", "S3UART1");
313 DECLARE_S3C_SERIAL_FUNCTIONS(1);
314 struct serial_device s3c24xx_serial1_device =
315 INIT_S3C_SERIAL_STRUCTURE(1, "s3ser1", "S3UART2");
316 DECLARE_S3C_SERIAL_FUNCTIONS(2);
317 struct serial_device s3c24xx_serial2_device =
318 INIT_S3C_SERIAL_STRUCTURE(2, "s3ser2", "S3UART3");
319 #endif /* CONFIG_SERIAL_MULTI */