blob: 8a3e30209cfae7857d3ad59925031480f8e4c791 [file] [log] [blame]
wdenk7ac16102004-08-01 22:48:16 +00001/*
2 * (C) Copyright 2002
Detlev Zundelf1b3f2b2009-05-13 10:54:10 +02003 * Gary Jennejohn, DENX Software Engineering, <garyj@denx.de>
wdenk7ac16102004-08-01 22:48:16 +00004 *
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
wdenkc6097192002-11-03 00:24:07 +000016 * 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>
kevin.morfitt@fearnside-systems.co.uke0d81312009-11-17 18:30:34 +090022#include <asm/arch/s3c24x0_cpu.h>
wdenkc6097192002-11-03 00:24:07 +000023
Wolfgang Denk6405a152006-03-31 18:32:53 +020024DECLARE_GLOBAL_DATA_PTR;
25
wdenk7539dea2003-06-19 23:01:32 +000026#ifdef CONFIG_SERIAL1
27#define UART_NR S3C24X0_UART0
28
wdenkc35ba4e2004-03-14 22:25:36 +000029#elif defined(CONFIG_SERIAL2)
wdenk7539dea2003-06-19 23:01:32 +000030# if defined(CONFIG_TRAB)
wdenkf6a6ac12003-09-17 15:10:32 +000031# error "TRAB supports only CONFIG_SERIAL1"
wdenk7539dea2003-06-19 23:01:32 +000032# endif
33#define UART_NR S3C24X0_UART1
34
wdenkc35ba4e2004-03-14 22:25:36 +000035#elif defined(CONFIG_SERIAL3)
wdenk7539dea2003-06-19 23:01:32 +000036# if defined(CONFIG_TRAB)
kevin.morfitt@fearnside-systems.co.uk1464f4d2009-10-10 13:33:11 +090037# error "TRAB supports only CONFIG_SERIAL1"
wdenk7539dea2003-06-19 23:01:32 +000038# endif
39#define UART_NR S3C24X0_UART2
40
41#else
42#error "Bad: you didn't configure serial ..."
43#endif
wdenkc6097192002-11-03 00:24:07 +000044
kevin.morfitt@fearnside-systems.co.uk1464f4d2009-10-10 13:33:11 +090045#include <asm/io.h>
46
Harald Welteda412622007-12-19 14:24:40 +010047#if defined(CONFIG_SERIAL_MULTI)
48#include <serial.h>
49
50/* Multi serial device functions */
51#define DECLARE_S3C_SERIAL_FUNCTIONS(port) \
kevin.morfitt@fearnside-systems.co.uk1464f4d2009-10-10 13:33:11 +090052 int s3serial##port##_init(void) \
53 { \
54 return serial_init_dev(port); \
55 } \
56 void s3serial##port##_setbrg(void) \
57 { \
58 serial_setbrg_dev(port); \
59 } \
60 int s3serial##port##_getc(void) \
61 { \
62 return serial_getc_dev(port); \
63 } \
64 int s3serial##port##_tstc(void) \
65 { \
66 return serial_tstc_dev(port); \
67 } \
68 void s3serial##port##_putc(const char c) \
69 { \
70 serial_putc_dev(port, c); \
71 } \
72 void s3serial##port##_puts(const char *s) \
73 { \
74 serial_puts_dev(port, s); \
75 }
Harald Welteda412622007-12-19 14:24:40 +010076
kevin.morfitt@fearnside-systems.co.uk1464f4d2009-10-10 13:33:11 +090077#define INIT_S3C_SERIAL_STRUCTURE(port, name, bus) { \
78 name, \
79 bus, \
80 s3serial##port##_init, \
Anatolij Gustschin99d25f92010-04-24 19:27:04 +020081 NULL,\
kevin.morfitt@fearnside-systems.co.uk1464f4d2009-10-10 13:33:11 +090082 s3serial##port##_setbrg, \
83 s3serial##port##_getc, \
84 s3serial##port##_tstc, \
85 s3serial##port##_putc, \
86 s3serial##port##_puts, \
87}
Harald Welteda412622007-12-19 14:24:40 +010088
89#endif /* CONFIG_SERIAL_MULTI */
90
kevin.morfitt@fearnside-systems.co.uk1464f4d2009-10-10 13:33:11 +090091#ifdef CONFIG_HWFLOW
92static int hwflow;
93#endif
94
Harald Welteda412622007-12-19 14:24:40 +010095void _serial_setbrg(const int dev_index)
wdenkc6097192002-11-03 00:24:07 +000096{
kevin.morfitt@fearnside-systems.co.uk1464f4d2009-10-10 13:33:11 +090097 struct s3c24x0_uart *uart = s3c24x0_get_base_uart(dev_index);
wdenkc6097192002-11-03 00:24:07 +000098 unsigned int reg = 0;
Harald Welteda412622007-12-19 14:24:40 +010099 int i;
wdenkc6097192002-11-03 00:24:07 +0000100
101 /* value is calculated so : (int)(PCLK/16./baudrate) -1 */
102 reg = get_PCLK() / (16 * gd->baudrate) - 1;
103
kevin.morfitt@fearnside-systems.co.uk1464f4d2009-10-10 13:33:11 +0900104 writel(reg, &uart->UBRDIV);
105 for (i = 0; i < 100; i++)
106 /* Delay */ ;
Harald Welteda412622007-12-19 14:24:40 +0100107}
kevin.morfitt@fearnside-systems.co.uk1464f4d2009-10-10 13:33:11 +0900108
Harald Welteda412622007-12-19 14:24:40 +0100109#if defined(CONFIG_SERIAL_MULTI)
kevin.morfitt@fearnside-systems.co.uk1464f4d2009-10-10 13:33:11 +0900110static inline void serial_setbrg_dev(unsigned int dev_index)
Harald Welteda412622007-12-19 14:24:40 +0100111{
112 _serial_setbrg(dev_index);
113}
114#else
115void serial_setbrg(void)
116{
117 _serial_setbrg(UART_NR);
118}
119#endif
120
121
122/* Initialise the serial port. The settings are always 8 data bits, no parity,
123 * 1 stop bit, no start bits.
124 */
125static int serial_init_dev(const int dev_index)
126{
kevin.morfitt@fearnside-systems.co.uk1464f4d2009-10-10 13:33:11 +0900127 struct s3c24x0_uart *uart = s3c24x0_get_base_uart(dev_index);
128
129#ifdef CONFIG_HWFLOW
130 hwflow = 0; /* turned off by default */
131#endif
Harald Welteda412622007-12-19 14:24:40 +0100132
wdenkc6097192002-11-03 00:24:07 +0000133 /* FIFO enable, Tx/Rx FIFO clear */
kevin.morfitt@fearnside-systems.co.uk1464f4d2009-10-10 13:33:11 +0900134 writel(0x07, &uart->UFCON);
135 writel(0x0, &uart->UMCON);
Harald Welteda412622007-12-19 14:24:40 +0100136
wdenkc6097192002-11-03 00:24:07 +0000137 /* Normal,No parity,1 stop,8 bit */
kevin.morfitt@fearnside-systems.co.uk1464f4d2009-10-10 13:33:11 +0900138 writel(0x3, &uart->ULCON);
wdenkc6097192002-11-03 00:24:07 +0000139 /*
140 * tx=level,rx=edge,disable timeout int.,enable rx error int.,
141 * normal,interrupt or polling
142 */
kevin.morfitt@fearnside-systems.co.uk1464f4d2009-10-10 13:33:11 +0900143 writel(0x245, &uart->UCON);
wdenkc6097192002-11-03 00:24:07 +0000144
145#ifdef CONFIG_HWFLOW
kevin.morfitt@fearnside-systems.co.uk1464f4d2009-10-10 13:33:11 +0900146 writel(0x1, &uart->UMCON); /* RTS up */
wdenkc6097192002-11-03 00:24:07 +0000147#endif
Harald Welteda412622007-12-19 14:24:40 +0100148
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)
kevin.morfitt@fearnside-systems.co.uk1464f4d2009-10-10 13:33:11 +0900153 writel(0x10, &uart->UMCON);
Harald Welteda412622007-12-19 14:24:40 +0100154#endif
155 _serial_setbrg(dev_index);
156
157 return (0);
wdenkc6097192002-11-03 00:24:07 +0000158}
159
Harald Welteda412622007-12-19 14:24:40 +0100160#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.
wdenkc6097192002-11-03 00:24:07 +0000163 */
kevin.morfitt@fearnside-systems.co.uk1464f4d2009-10-10 13:33:11 +0900164int serial_init(void)
wdenkc6097192002-11-03 00:24:07 +0000165{
Harald Welteda412622007-12-19 14:24:40 +0100166 return serial_init_dev(UART_NR);
wdenkc6097192002-11-03 00:24:07 +0000167}
Harald Welteda412622007-12-19 14:24:40 +0100168#endif
wdenkc6097192002-11-03 00:24:07 +0000169
170/*
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.
174 */
kevin.morfitt@fearnside-systems.co.uk1464f4d2009-10-10 13:33:11 +0900175int _serial_getc(const int dev_index)
wdenkc6097192002-11-03 00:24:07 +0000176{
kevin.morfitt@fearnside-systems.co.uk1464f4d2009-10-10 13:33:11 +0900177 struct s3c24x0_uart *uart = s3c24x0_get_base_uart(dev_index);
wdenk57b2d802003-06-27 21:31:46 +0000178
kevin.morfitt@fearnside-systems.co.uk1464f4d2009-10-10 13:33:11 +0900179 while (!(readl(&uart->UTRSTAT) & 0x1))
180 /* wait for character to arrive */ ;
wdenkc6097192002-11-03 00:24:07 +0000181
kevin.morfitt@fearnside-systems.co.uk1464f4d2009-10-10 13:33:11 +0900182 return readb(&uart->URXH) & 0xff;
wdenkc6097192002-11-03 00:24:07 +0000183}
kevin.morfitt@fearnside-systems.co.uk1464f4d2009-10-10 13:33:11 +0900184
Harald Welteda412622007-12-19 14:24:40 +0100185#if defined(CONFIG_SERIAL_MULTI)
186static inline int serial_getc_dev(unsigned int dev_index)
187{
188 return _serial_getc(dev_index);
189}
190#else
kevin.morfitt@fearnside-systems.co.uk1464f4d2009-10-10 13:33:11 +0900191int serial_getc(void)
Harald Welteda412622007-12-19 14:24:40 +0100192{
193 return _serial_getc(UART_NR);
194}
195#endif
wdenkc6097192002-11-03 00:24:07 +0000196
197#ifdef CONFIG_HWFLOW
wdenkc6097192002-11-03 00:24:07 +0000198int hwflow_onoff(int on)
199{
kevin.morfitt@fearnside-systems.co.uk1464f4d2009-10-10 13:33:11 +0900200 switch (on) {
wdenkc6097192002-11-03 00:24:07 +0000201 case 0:
202 default:
kevin.morfitt@fearnside-systems.co.uk1464f4d2009-10-10 13:33:11 +0900203 break; /* return current */
wdenkc6097192002-11-03 00:24:07 +0000204 case 1:
kevin.morfitt@fearnside-systems.co.uk1464f4d2009-10-10 13:33:11 +0900205 hwflow = 1; /* turn on */
wdenkc6097192002-11-03 00:24:07 +0000206 break;
207 case -1:
kevin.morfitt@fearnside-systems.co.uk1464f4d2009-10-10 13:33:11 +0900208 hwflow = 0; /* turn off */
wdenkc6097192002-11-03 00:24:07 +0000209 break;
210 }
211 return hwflow;
212}
213#endif
214
215#ifdef CONFIG_MODEM_SUPPORT
216static int be_quiet = 0;
217void disable_putc(void)
218{
219 be_quiet = 1;
220}
221
222void enable_putc(void)
223{
224 be_quiet = 0;
225}
226#endif
227
228
229/*
230 * Output a single byte to the serial port.
231 */
kevin.morfitt@fearnside-systems.co.uk1464f4d2009-10-10 13:33:11 +0900232void _serial_putc(const char c, const int dev_index)
wdenkc6097192002-11-03 00:24:07 +0000233{
kevin.morfitt@fearnside-systems.co.uk1464f4d2009-10-10 13:33:11 +0900234 struct s3c24x0_uart *uart = s3c24x0_get_base_uart(dev_index);
wdenkc6097192002-11-03 00:24:07 +0000235#ifdef CONFIG_MODEM_SUPPORT
236 if (be_quiet)
237 return;
238#endif
239
kevin.morfitt@fearnside-systems.co.uk1464f4d2009-10-10 13:33:11 +0900240 while (!(readl(&uart->UTRSTAT) & 0x2))
241 /* wait for room in the tx FIFO */ ;
wdenkc6097192002-11-03 00:24:07 +0000242
243#ifdef CONFIG_HWFLOW
kevin.morfitt@fearnside-systems.co.uk1464f4d2009-10-10 13:33:11 +0900244 while (hwflow && !(readl(&uart->UMSTAT) & 0x1))
245 /* Wait for CTS up */ ;
wdenkc6097192002-11-03 00:24:07 +0000246#endif
247
kevin.morfitt@fearnside-systems.co.uk1464f4d2009-10-10 13:33:11 +0900248 writeb(c, &uart->UTXH);
wdenkc6097192002-11-03 00:24:07 +0000249
250 /* If \n, also do \r */
251 if (c == '\n')
kevin.morfitt@fearnside-systems.co.uk1464f4d2009-10-10 13:33:11 +0900252 serial_putc('\r');
wdenkc6097192002-11-03 00:24:07 +0000253}
kevin.morfitt@fearnside-systems.co.uk1464f4d2009-10-10 13:33:11 +0900254
Harald Welteda412622007-12-19 14:24:40 +0100255#if defined(CONFIG_SERIAL_MULTI)
256static inline void serial_putc_dev(unsigned int dev_index, const char c)
257{
258 _serial_putc(c, dev_index);
259}
260#else
261void serial_putc(const char c)
262{
263 _serial_putc(c, UART_NR);
264}
265#endif
266
wdenkc6097192002-11-03 00:24:07 +0000267
268/*
269 * Test whether a character is in the RX buffer
270 */
Harald Welteda412622007-12-19 14:24:40 +0100271int _serial_tstc(const int dev_index)
wdenkc6097192002-11-03 00:24:07 +0000272{
kevin.morfitt@fearnside-systems.co.uk1464f4d2009-10-10 13:33:11 +0900273 struct s3c24x0_uart *uart = s3c24x0_get_base_uart(dev_index);
wdenk7539dea2003-06-19 23:01:32 +0000274
kevin.morfitt@fearnside-systems.co.uk1464f4d2009-10-10 13:33:11 +0900275 return readl(&uart->UTRSTAT) & 0x1;
wdenkc6097192002-11-03 00:24:07 +0000276}
kevin.morfitt@fearnside-systems.co.uk1464f4d2009-10-10 13:33:11 +0900277
Harald Welteda412622007-12-19 14:24:40 +0100278#if defined(CONFIG_SERIAL_MULTI)
kevin.morfitt@fearnside-systems.co.uk1464f4d2009-10-10 13:33:11 +0900279static inline int serial_tstc_dev(unsigned int dev_index)
Harald Welteda412622007-12-19 14:24:40 +0100280{
281 return _serial_tstc(dev_index);
282}
283#else
284int serial_tstc(void)
285{
286 return _serial_tstc(UART_NR);
287}
288#endif
wdenkc6097192002-11-03 00:24:07 +0000289
Harald Welteda412622007-12-19 14:24:40 +0100290void _serial_puts(const char *s, const int dev_index)
wdenkc6097192002-11-03 00:24:07 +0000291{
292 while (*s) {
kevin.morfitt@fearnside-systems.co.uk1464f4d2009-10-10 13:33:11 +0900293 _serial_putc(*s++, dev_index);
wdenkc6097192002-11-03 00:24:07 +0000294 }
295}
kevin.morfitt@fearnside-systems.co.uk1464f4d2009-10-10 13:33:11 +0900296
Harald Welteda412622007-12-19 14:24:40 +0100297#if defined(CONFIG_SERIAL_MULTI)
kevin.morfitt@fearnside-systems.co.uk1464f4d2009-10-10 13:33:11 +0900298static inline void serial_puts_dev(int dev_index, const char *s)
Harald Welteda412622007-12-19 14:24:40 +0100299{
300 _serial_puts(s, dev_index);
301}
302#else
kevin.morfitt@fearnside-systems.co.uk1464f4d2009-10-10 13:33:11 +0900303void serial_puts(const char *s)
Harald Welteda412622007-12-19 14:24:40 +0100304{
305 _serial_puts(s, UART_NR);
306}
307#endif
308
309#if defined(CONFIG_SERIAL_MULTI)
310DECLARE_S3C_SERIAL_FUNCTIONS(0);
311struct serial_device s3c24xx_serial0_device =
kevin.morfitt@fearnside-systems.co.uk1464f4d2009-10-10 13:33:11 +0900312INIT_S3C_SERIAL_STRUCTURE(0, "s3ser0", "S3UART1");
Harald Welteda412622007-12-19 14:24:40 +0100313DECLARE_S3C_SERIAL_FUNCTIONS(1);
314struct serial_device s3c24xx_serial1_device =
kevin.morfitt@fearnside-systems.co.uk1464f4d2009-10-10 13:33:11 +0900315INIT_S3C_SERIAL_STRUCTURE(1, "s3ser1", "S3UART2");
Harald Welteda412622007-12-19 14:24:40 +0100316DECLARE_S3C_SERIAL_FUNCTIONS(2);
317struct serial_device s3c24xx_serial2_device =
kevin.morfitt@fearnside-systems.co.uk1464f4d2009-10-10 13:33:11 +0900318INIT_S3C_SERIAL_STRUCTURE(2, "s3ser2", "S3UART3");
Harald Welteda412622007-12-19 14:24:40 +0100319#endif /* CONFIG_SERIAL_MULTI */