blob: ff35ce595567be4f11656ec7cb1243904f551b6b [file] [log] [blame]
wdenk281e00a2004-08-01 22:48:16 +00001/*
2 * (C) Copyright 2002
Detlev Zundel792a09e2009-05-13 10:54:10 +02003 * Gary Jennejohn, DENX Software Engineering, <garyj@denx.de>
wdenk281e00a2004-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>
Mike Frysinger6c768ca2011-04-29 18:03:29 +000022#include <linux/compiler.h>
kevin.morfitt@fearnside-systems.co.ukac678042009-11-17 18:30:34 +090023#include <asm/arch/s3c24x0_cpu.h>
wdenkc6097192002-11-03 00:24:07 +000024
Wolfgang Denkd87080b2006-03-31 18:32:53 +020025DECLARE_GLOBAL_DATA_PTR;
26
wdenk48b42612003-06-19 23:01:32 +000027#ifdef CONFIG_SERIAL1
28#define UART_NR S3C24X0_UART0
29
wdenk42dfe7a2004-03-14 22:25:36 +000030#elif defined(CONFIG_SERIAL2)
wdenk48b42612003-06-19 23:01:32 +000031#define UART_NR S3C24X0_UART1
32
wdenk42dfe7a2004-03-14 22:25:36 +000033#elif defined(CONFIG_SERIAL3)
wdenk48b42612003-06-19 23:01:32 +000034#define UART_NR S3C24X0_UART2
35
36#else
37#error "Bad: you didn't configure serial ..."
38#endif
wdenkc6097192002-11-03 00:24:07 +000039
kevin.morfitt@fearnside-systems.co.ukeb0ae7f2009-10-10 13:33:11 +090040#include <asm/io.h>
41
Harald Weltea7c185e2007-12-19 14:24:40 +010042#if defined(CONFIG_SERIAL_MULTI)
43#include <serial.h>
44
45/* Multi serial device functions */
46#define DECLARE_S3C_SERIAL_FUNCTIONS(port) \
kevin.morfitt@fearnside-systems.co.ukeb0ae7f2009-10-10 13:33:11 +090047 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 }
Harald Weltea7c185e2007-12-19 14:24:40 +010071
kevin.morfitt@fearnside-systems.co.ukeb0ae7f2009-10-10 13:33:11 +090072#define INIT_S3C_SERIAL_STRUCTURE(port, name, bus) { \
73 name, \
74 bus, \
75 s3serial##port##_init, \
Anatolij Gustschinfbb00302010-04-24 19:27:04 +020076 NULL,\
kevin.morfitt@fearnside-systems.co.ukeb0ae7f2009-10-10 13:33:11 +090077 s3serial##port##_setbrg, \
78 s3serial##port##_getc, \
79 s3serial##port##_tstc, \
80 s3serial##port##_putc, \
81 s3serial##port##_puts, \
82}
Harald Weltea7c185e2007-12-19 14:24:40 +010083
84#endif /* CONFIG_SERIAL_MULTI */
85
kevin.morfitt@fearnside-systems.co.ukeb0ae7f2009-10-10 13:33:11 +090086#ifdef CONFIG_HWFLOW
87static int hwflow;
88#endif
89
Harald Weltea7c185e2007-12-19 14:24:40 +010090void _serial_setbrg(const int dev_index)
wdenkc6097192002-11-03 00:24:07 +000091{
kevin.morfitt@fearnside-systems.co.ukeb0ae7f2009-10-10 13:33:11 +090092 struct s3c24x0_uart *uart = s3c24x0_get_base_uart(dev_index);
wdenkc6097192002-11-03 00:24:07 +000093 unsigned int reg = 0;
Harald Weltea7c185e2007-12-19 14:24:40 +010094 int i;
wdenkc6097192002-11-03 00:24:07 +000095
96 /* value is calculated so : (int)(PCLK/16./baudrate) -1 */
97 reg = get_PCLK() / (16 * gd->baudrate) - 1;
98
C Naumand9abba82010-10-26 23:04:31 +090099 writel(reg, &uart->ubrdiv);
kevin.morfitt@fearnside-systems.co.ukeb0ae7f2009-10-10 13:33:11 +0900100 for (i = 0; i < 100; i++)
101 /* Delay */ ;
Harald Weltea7c185e2007-12-19 14:24:40 +0100102}
kevin.morfitt@fearnside-systems.co.ukeb0ae7f2009-10-10 13:33:11 +0900103
Harald Weltea7c185e2007-12-19 14:24:40 +0100104#if defined(CONFIG_SERIAL_MULTI)
kevin.morfitt@fearnside-systems.co.ukeb0ae7f2009-10-10 13:33:11 +0900105static inline void serial_setbrg_dev(unsigned int dev_index)
Harald Weltea7c185e2007-12-19 14:24:40 +0100106{
107 _serial_setbrg(dev_index);
108}
109#else
110void 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 */
120static int serial_init_dev(const int dev_index)
121{
kevin.morfitt@fearnside-systems.co.ukeb0ae7f2009-10-10 13:33:11 +0900122 struct s3c24x0_uart *uart = s3c24x0_get_base_uart(dev_index);
123
124#ifdef CONFIG_HWFLOW
125 hwflow = 0; /* turned off by default */
126#endif
Harald Weltea7c185e2007-12-19 14:24:40 +0100127
wdenkc6097192002-11-03 00:24:07 +0000128 /* FIFO enable, Tx/Rx FIFO clear */
C Naumand9abba82010-10-26 23:04:31 +0900129 writel(0x07, &uart->ufcon);
130 writel(0x0, &uart->umcon);
Harald Weltea7c185e2007-12-19 14:24:40 +0100131
wdenkc6097192002-11-03 00:24:07 +0000132 /* Normal,No parity,1 stop,8 bit */
C Naumand9abba82010-10-26 23:04:31 +0900133 writel(0x3, &uart->ulcon);
wdenkc6097192002-11-03 00:24:07 +0000134 /*
135 * tx=level,rx=edge,disable timeout int.,enable rx error int.,
136 * normal,interrupt or polling
137 */
C Naumand9abba82010-10-26 23:04:31 +0900138 writel(0x245, &uart->ucon);
wdenkc6097192002-11-03 00:24:07 +0000139
140#ifdef CONFIG_HWFLOW
C Naumand9abba82010-10-26 23:04:31 +0900141 writel(0x1, &uart->umcon); /* rts up */
wdenkc6097192002-11-03 00:24:07 +0000142#endif
wdenkc6097192002-11-03 00:24:07 +0000143
Harald Weltea7c185e2007-12-19 14:24:40 +0100144 /* 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)
C Naumand9abba82010-10-26 23:04:31 +0900148 writel(0x10, &uart->umcon);
Harald Weltea7c185e2007-12-19 14:24:40 +0100149#endif
150 _serial_setbrg(dev_index);
wdenkc6097192002-11-03 00:24:07 +0000151
152 return (0);
153}
154
Harald Weltea7c185e2007-12-19 14:24:40 +0100155#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 */
kevin.morfitt@fearnside-systems.co.ukeb0ae7f2009-10-10 13:33:11 +0900159int serial_init(void)
Harald Weltea7c185e2007-12-19 14:24:40 +0100160{
161 return serial_init_dev(UART_NR);
162}
163#endif
164
wdenkc6097192002-11-03 00:24:07 +0000165/*
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 */
kevin.morfitt@fearnside-systems.co.ukeb0ae7f2009-10-10 13:33:11 +0900170int _serial_getc(const int dev_index)
wdenkc6097192002-11-03 00:24:07 +0000171{
kevin.morfitt@fearnside-systems.co.ukeb0ae7f2009-10-10 13:33:11 +0900172 struct s3c24x0_uart *uart = s3c24x0_get_base_uart(dev_index);
wdenk8bde7f72003-06-27 21:31:46 +0000173
C Naumand9abba82010-10-26 23:04:31 +0900174 while (!(readl(&uart->utrstat) & 0x1))
kevin.morfitt@fearnside-systems.co.ukeb0ae7f2009-10-10 13:33:11 +0900175 /* wait for character to arrive */ ;
wdenkc6097192002-11-03 00:24:07 +0000176
C Naumand9abba82010-10-26 23:04:31 +0900177 return readb(&uart->urxh) & 0xff;
wdenkc6097192002-11-03 00:24:07 +0000178}
kevin.morfitt@fearnside-systems.co.ukeb0ae7f2009-10-10 13:33:11 +0900179
Harald Weltea7c185e2007-12-19 14:24:40 +0100180#if defined(CONFIG_SERIAL_MULTI)
181static inline int serial_getc_dev(unsigned int dev_index)
182{
183 return _serial_getc(dev_index);
184}
185#else
kevin.morfitt@fearnside-systems.co.ukeb0ae7f2009-10-10 13:33:11 +0900186int serial_getc(void)
Harald Weltea7c185e2007-12-19 14:24:40 +0100187{
188 return _serial_getc(UART_NR);
189}
190#endif
wdenkc6097192002-11-03 00:24:07 +0000191
192#ifdef CONFIG_HWFLOW
wdenkc6097192002-11-03 00:24:07 +0000193int hwflow_onoff(int on)
194{
kevin.morfitt@fearnside-systems.co.ukeb0ae7f2009-10-10 13:33:11 +0900195 switch (on) {
wdenkc6097192002-11-03 00:24:07 +0000196 case 0:
197 default:
kevin.morfitt@fearnside-systems.co.ukeb0ae7f2009-10-10 13:33:11 +0900198 break; /* return current */
wdenkc6097192002-11-03 00:24:07 +0000199 case 1:
kevin.morfitt@fearnside-systems.co.ukeb0ae7f2009-10-10 13:33:11 +0900200 hwflow = 1; /* turn on */
wdenkc6097192002-11-03 00:24:07 +0000201 break;
202 case -1:
kevin.morfitt@fearnside-systems.co.ukeb0ae7f2009-10-10 13:33:11 +0900203 hwflow = 0; /* turn off */
wdenkc6097192002-11-03 00:24:07 +0000204 break;
205 }
206 return hwflow;
207}
208#endif
209
210#ifdef CONFIG_MODEM_SUPPORT
211static int be_quiet = 0;
212void disable_putc(void)
213{
214 be_quiet = 1;
215}
216
217void enable_putc(void)
218{
219 be_quiet = 0;
220}
221#endif
222
223
224/*
225 * Output a single byte to the serial port.
226 */
kevin.morfitt@fearnside-systems.co.ukeb0ae7f2009-10-10 13:33:11 +0900227void _serial_putc(const char c, const int dev_index)
wdenkc6097192002-11-03 00:24:07 +0000228{
kevin.morfitt@fearnside-systems.co.ukeb0ae7f2009-10-10 13:33:11 +0900229 struct s3c24x0_uart *uart = s3c24x0_get_base_uart(dev_index);
wdenkc6097192002-11-03 00:24:07 +0000230#ifdef CONFIG_MODEM_SUPPORT
231 if (be_quiet)
232 return;
233#endif
234
C Naumand9abba82010-10-26 23:04:31 +0900235 while (!(readl(&uart->utrstat) & 0x2))
kevin.morfitt@fearnside-systems.co.ukeb0ae7f2009-10-10 13:33:11 +0900236 /* wait for room in the tx FIFO */ ;
wdenkc6097192002-11-03 00:24:07 +0000237
238#ifdef CONFIG_HWFLOW
C Naumand9abba82010-10-26 23:04:31 +0900239 while (hwflow && !(readl(&uart->umstat) & 0x1))
kevin.morfitt@fearnside-systems.co.ukeb0ae7f2009-10-10 13:33:11 +0900240 /* Wait for CTS up */ ;
wdenkc6097192002-11-03 00:24:07 +0000241#endif
242
C Naumand9abba82010-10-26 23:04:31 +0900243 writeb(c, &uart->utxh);
wdenkc6097192002-11-03 00:24:07 +0000244
245 /* If \n, also do \r */
246 if (c == '\n')
kevin.morfitt@fearnside-systems.co.ukeb0ae7f2009-10-10 13:33:11 +0900247 serial_putc('\r');
wdenkc6097192002-11-03 00:24:07 +0000248}
kevin.morfitt@fearnside-systems.co.ukeb0ae7f2009-10-10 13:33:11 +0900249
Harald Weltea7c185e2007-12-19 14:24:40 +0100250#if defined(CONFIG_SERIAL_MULTI)
251static inline void serial_putc_dev(unsigned int dev_index, const char c)
252{
253 _serial_putc(c, dev_index);
254}
255#else
256void serial_putc(const char c)
257{
258 _serial_putc(c, UART_NR);
259}
260#endif
261
wdenkc6097192002-11-03 00:24:07 +0000262
263/*
264 * Test whether a character is in the RX buffer
265 */
Harald Weltea7c185e2007-12-19 14:24:40 +0100266int _serial_tstc(const int dev_index)
wdenkc6097192002-11-03 00:24:07 +0000267{
kevin.morfitt@fearnside-systems.co.ukeb0ae7f2009-10-10 13:33:11 +0900268 struct s3c24x0_uart *uart = s3c24x0_get_base_uart(dev_index);
wdenk48b42612003-06-19 23:01:32 +0000269
C Naumand9abba82010-10-26 23:04:31 +0900270 return readl(&uart->utrstat) & 0x1;
wdenkc6097192002-11-03 00:24:07 +0000271}
kevin.morfitt@fearnside-systems.co.ukeb0ae7f2009-10-10 13:33:11 +0900272
Harald Weltea7c185e2007-12-19 14:24:40 +0100273#if defined(CONFIG_SERIAL_MULTI)
kevin.morfitt@fearnside-systems.co.ukeb0ae7f2009-10-10 13:33:11 +0900274static inline int serial_tstc_dev(unsigned int dev_index)
Harald Weltea7c185e2007-12-19 14:24:40 +0100275{
276 return _serial_tstc(dev_index);
277}
278#else
279int serial_tstc(void)
280{
281 return _serial_tstc(UART_NR);
282}
283#endif
wdenkc6097192002-11-03 00:24:07 +0000284
Harald Weltea7c185e2007-12-19 14:24:40 +0100285void _serial_puts(const char *s, const int dev_index)
286{
287 while (*s) {
kevin.morfitt@fearnside-systems.co.ukeb0ae7f2009-10-10 13:33:11 +0900288 _serial_putc(*s++, dev_index);
Harald Weltea7c185e2007-12-19 14:24:40 +0100289 }
290}
kevin.morfitt@fearnside-systems.co.ukeb0ae7f2009-10-10 13:33:11 +0900291
Harald Weltea7c185e2007-12-19 14:24:40 +0100292#if defined(CONFIG_SERIAL_MULTI)
kevin.morfitt@fearnside-systems.co.ukeb0ae7f2009-10-10 13:33:11 +0900293static inline void serial_puts_dev(int dev_index, const char *s)
Harald Weltea7c185e2007-12-19 14:24:40 +0100294{
295 _serial_puts(s, dev_index);
296}
297#else
kevin.morfitt@fearnside-systems.co.ukeb0ae7f2009-10-10 13:33:11 +0900298void serial_puts(const char *s)
wdenkc6097192002-11-03 00:24:07 +0000299{
Harald Weltea7c185e2007-12-19 14:24:40 +0100300 _serial_puts(s, UART_NR);
wdenkc6097192002-11-03 00:24:07 +0000301}
Harald Weltea7c185e2007-12-19 14:24:40 +0100302#endif
303
304#if defined(CONFIG_SERIAL_MULTI)
305DECLARE_S3C_SERIAL_FUNCTIONS(0);
306struct serial_device s3c24xx_serial0_device =
kevin.morfitt@fearnside-systems.co.ukeb0ae7f2009-10-10 13:33:11 +0900307INIT_S3C_SERIAL_STRUCTURE(0, "s3ser0", "S3UART1");
Harald Weltea7c185e2007-12-19 14:24:40 +0100308DECLARE_S3C_SERIAL_FUNCTIONS(1);
309struct serial_device s3c24xx_serial1_device =
kevin.morfitt@fearnside-systems.co.ukeb0ae7f2009-10-10 13:33:11 +0900310INIT_S3C_SERIAL_STRUCTURE(1, "s3ser1", "S3UART2");
Harald Weltea7c185e2007-12-19 14:24:40 +0100311DECLARE_S3C_SERIAL_FUNCTIONS(2);
312struct serial_device s3c24xx_serial2_device =
kevin.morfitt@fearnside-systems.co.ukeb0ae7f2009-10-10 13:33:11 +0900313INIT_S3C_SERIAL_STRUCTURE(2, "s3ser2", "S3UART3");
Mike Frysinger6c768ca2011-04-29 18:03:29 +0000314
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}
Harald Weltea7c185e2007-12-19 14:24:40 +0100327#endif /* CONFIG_SERIAL_MULTI */