blob: 625c6b7d0c579706450a26a0e8fa650df4ab5905 [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
Marek Vasut90bad892012-09-09 18:48:28 +020072#define INIT_S3C_SERIAL_STRUCTURE(port, __name) { \
73 .name = __name, \
74 .start = s3serial##port##_init, \
75 .stop = NULL, \
76 .setbrg = s3serial##port##_setbrg, \
77 .getc = s3serial##port##_getc, \
78 .tstc = s3serial##port##_tstc, \
79 .putc = s3serial##port##_putc, \
80 .puts = s3serial##port##_puts, \
kevin.morfitt@fearnside-systems.co.ukeb0ae7f2009-10-10 13:33:11 +090081}
Harald Weltea7c185e2007-12-19 14:24:40 +010082
83#endif /* CONFIG_SERIAL_MULTI */
84
kevin.morfitt@fearnside-systems.co.ukeb0ae7f2009-10-10 13:33:11 +090085#ifdef CONFIG_HWFLOW
86static int hwflow;
87#endif
88
Harald Weltea7c185e2007-12-19 14:24:40 +010089void _serial_setbrg(const int dev_index)
wdenkc6097192002-11-03 00:24:07 +000090{
kevin.morfitt@fearnside-systems.co.ukeb0ae7f2009-10-10 13:33:11 +090091 struct s3c24x0_uart *uart = s3c24x0_get_base_uart(dev_index);
wdenkc6097192002-11-03 00:24:07 +000092 unsigned int reg = 0;
Harald Weltea7c185e2007-12-19 14:24:40 +010093 int i;
wdenkc6097192002-11-03 00:24:07 +000094
95 /* value is calculated so : (int)(PCLK/16./baudrate) -1 */
96 reg = get_PCLK() / (16 * gd->baudrate) - 1;
97
C Naumand9abba82010-10-26 23:04:31 +090098 writel(reg, &uart->ubrdiv);
kevin.morfitt@fearnside-systems.co.ukeb0ae7f2009-10-10 13:33:11 +090099 for (i = 0; i < 100; i++)
100 /* Delay */ ;
Harald Weltea7c185e2007-12-19 14:24:40 +0100101}
kevin.morfitt@fearnside-systems.co.ukeb0ae7f2009-10-10 13:33:11 +0900102
Harald Weltea7c185e2007-12-19 14:24:40 +0100103#if defined(CONFIG_SERIAL_MULTI)
kevin.morfitt@fearnside-systems.co.ukeb0ae7f2009-10-10 13:33:11 +0900104static inline void serial_setbrg_dev(unsigned int dev_index)
Harald Weltea7c185e2007-12-19 14:24:40 +0100105{
106 _serial_setbrg(dev_index);
107}
108#else
109void serial_setbrg(void)
110{
111 _serial_setbrg(UART_NR);
112}
113#endif
114
115
116/* Initialise the serial port. The settings are always 8 data bits, no parity,
117 * 1 stop bit, no start bits.
118 */
119static int serial_init_dev(const int dev_index)
120{
kevin.morfitt@fearnside-systems.co.ukeb0ae7f2009-10-10 13:33:11 +0900121 struct s3c24x0_uart *uart = s3c24x0_get_base_uart(dev_index);
122
123#ifdef CONFIG_HWFLOW
124 hwflow = 0; /* turned off by default */
125#endif
Harald Weltea7c185e2007-12-19 14:24:40 +0100126
wdenkc6097192002-11-03 00:24:07 +0000127 /* FIFO enable, Tx/Rx FIFO clear */
C Naumand9abba82010-10-26 23:04:31 +0900128 writel(0x07, &uart->ufcon);
129 writel(0x0, &uart->umcon);
Harald Weltea7c185e2007-12-19 14:24:40 +0100130
wdenkc6097192002-11-03 00:24:07 +0000131 /* Normal,No parity,1 stop,8 bit */
C Naumand9abba82010-10-26 23:04:31 +0900132 writel(0x3, &uart->ulcon);
wdenkc6097192002-11-03 00:24:07 +0000133 /*
134 * tx=level,rx=edge,disable timeout int.,enable rx error int.,
135 * normal,interrupt or polling
136 */
C Naumand9abba82010-10-26 23:04:31 +0900137 writel(0x245, &uart->ucon);
wdenkc6097192002-11-03 00:24:07 +0000138
139#ifdef CONFIG_HWFLOW
C Naumand9abba82010-10-26 23:04:31 +0900140 writel(0x1, &uart->umcon); /* rts up */
wdenkc6097192002-11-03 00:24:07 +0000141#endif
wdenkc6097192002-11-03 00:24:07 +0000142
Harald Weltea7c185e2007-12-19 14:24:40 +0100143 /* 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)
C Naumand9abba82010-10-26 23:04:31 +0900147 writel(0x10, &uart->umcon);
Harald Weltea7c185e2007-12-19 14:24:40 +0100148#endif
149 _serial_setbrg(dev_index);
wdenkc6097192002-11-03 00:24:07 +0000150
151 return (0);
152}
153
Harald Weltea7c185e2007-12-19 14:24:40 +0100154#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.
157 */
kevin.morfitt@fearnside-systems.co.ukeb0ae7f2009-10-10 13:33:11 +0900158int serial_init(void)
Harald Weltea7c185e2007-12-19 14:24:40 +0100159{
160 return serial_init_dev(UART_NR);
161}
162#endif
163
wdenkc6097192002-11-03 00:24:07 +0000164/*
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.
168 */
kevin.morfitt@fearnside-systems.co.ukeb0ae7f2009-10-10 13:33:11 +0900169int _serial_getc(const int dev_index)
wdenkc6097192002-11-03 00:24:07 +0000170{
kevin.morfitt@fearnside-systems.co.ukeb0ae7f2009-10-10 13:33:11 +0900171 struct s3c24x0_uart *uart = s3c24x0_get_base_uart(dev_index);
wdenk8bde7f72003-06-27 21:31:46 +0000172
C Naumand9abba82010-10-26 23:04:31 +0900173 while (!(readl(&uart->utrstat) & 0x1))
kevin.morfitt@fearnside-systems.co.ukeb0ae7f2009-10-10 13:33:11 +0900174 /* wait for character to arrive */ ;
wdenkc6097192002-11-03 00:24:07 +0000175
C Naumand9abba82010-10-26 23:04:31 +0900176 return readb(&uart->urxh) & 0xff;
wdenkc6097192002-11-03 00:24:07 +0000177}
kevin.morfitt@fearnside-systems.co.ukeb0ae7f2009-10-10 13:33:11 +0900178
Harald Weltea7c185e2007-12-19 14:24:40 +0100179#if defined(CONFIG_SERIAL_MULTI)
180static inline int serial_getc_dev(unsigned int dev_index)
181{
182 return _serial_getc(dev_index);
183}
184#else
kevin.morfitt@fearnside-systems.co.ukeb0ae7f2009-10-10 13:33:11 +0900185int serial_getc(void)
Harald Weltea7c185e2007-12-19 14:24:40 +0100186{
187 return _serial_getc(UART_NR);
188}
189#endif
wdenkc6097192002-11-03 00:24:07 +0000190
191#ifdef CONFIG_HWFLOW
wdenkc6097192002-11-03 00:24:07 +0000192int hwflow_onoff(int on)
193{
kevin.morfitt@fearnside-systems.co.ukeb0ae7f2009-10-10 13:33:11 +0900194 switch (on) {
wdenkc6097192002-11-03 00:24:07 +0000195 case 0:
196 default:
kevin.morfitt@fearnside-systems.co.ukeb0ae7f2009-10-10 13:33:11 +0900197 break; /* return current */
wdenkc6097192002-11-03 00:24:07 +0000198 case 1:
kevin.morfitt@fearnside-systems.co.ukeb0ae7f2009-10-10 13:33:11 +0900199 hwflow = 1; /* turn on */
wdenkc6097192002-11-03 00:24:07 +0000200 break;
201 case -1:
kevin.morfitt@fearnside-systems.co.ukeb0ae7f2009-10-10 13:33:11 +0900202 hwflow = 0; /* turn off */
wdenkc6097192002-11-03 00:24:07 +0000203 break;
204 }
205 return hwflow;
206}
207#endif
208
209#ifdef CONFIG_MODEM_SUPPORT
210static int be_quiet = 0;
211void disable_putc(void)
212{
213 be_quiet = 1;
214}
215
216void enable_putc(void)
217{
218 be_quiet = 0;
219}
220#endif
221
222
223/*
224 * Output a single byte to the serial port.
225 */
kevin.morfitt@fearnside-systems.co.ukeb0ae7f2009-10-10 13:33:11 +0900226void _serial_putc(const char c, const int dev_index)
wdenkc6097192002-11-03 00:24:07 +0000227{
kevin.morfitt@fearnside-systems.co.ukeb0ae7f2009-10-10 13:33:11 +0900228 struct s3c24x0_uart *uart = s3c24x0_get_base_uart(dev_index);
wdenkc6097192002-11-03 00:24:07 +0000229#ifdef CONFIG_MODEM_SUPPORT
230 if (be_quiet)
231 return;
232#endif
233
C Naumand9abba82010-10-26 23:04:31 +0900234 while (!(readl(&uart->utrstat) & 0x2))
kevin.morfitt@fearnside-systems.co.ukeb0ae7f2009-10-10 13:33:11 +0900235 /* wait for room in the tx FIFO */ ;
wdenkc6097192002-11-03 00:24:07 +0000236
237#ifdef CONFIG_HWFLOW
C Naumand9abba82010-10-26 23:04:31 +0900238 while (hwflow && !(readl(&uart->umstat) & 0x1))
kevin.morfitt@fearnside-systems.co.ukeb0ae7f2009-10-10 13:33:11 +0900239 /* Wait for CTS up */ ;
wdenkc6097192002-11-03 00:24:07 +0000240#endif
241
C Naumand9abba82010-10-26 23:04:31 +0900242 writeb(c, &uart->utxh);
wdenkc6097192002-11-03 00:24:07 +0000243
244 /* If \n, also do \r */
245 if (c == '\n')
kevin.morfitt@fearnside-systems.co.ukeb0ae7f2009-10-10 13:33:11 +0900246 serial_putc('\r');
wdenkc6097192002-11-03 00:24:07 +0000247}
kevin.morfitt@fearnside-systems.co.ukeb0ae7f2009-10-10 13:33:11 +0900248
Harald Weltea7c185e2007-12-19 14:24:40 +0100249#if defined(CONFIG_SERIAL_MULTI)
250static inline void serial_putc_dev(unsigned int dev_index, const char c)
251{
252 _serial_putc(c, dev_index);
253}
254#else
255void serial_putc(const char c)
256{
257 _serial_putc(c, UART_NR);
258}
259#endif
260
wdenkc6097192002-11-03 00:24:07 +0000261
262/*
263 * Test whether a character is in the RX buffer
264 */
Harald Weltea7c185e2007-12-19 14:24:40 +0100265int _serial_tstc(const int dev_index)
wdenkc6097192002-11-03 00:24:07 +0000266{
kevin.morfitt@fearnside-systems.co.ukeb0ae7f2009-10-10 13:33:11 +0900267 struct s3c24x0_uart *uart = s3c24x0_get_base_uart(dev_index);
wdenk48b42612003-06-19 23:01:32 +0000268
C Naumand9abba82010-10-26 23:04:31 +0900269 return readl(&uart->utrstat) & 0x1;
wdenkc6097192002-11-03 00:24:07 +0000270}
kevin.morfitt@fearnside-systems.co.ukeb0ae7f2009-10-10 13:33:11 +0900271
Harald Weltea7c185e2007-12-19 14:24:40 +0100272#if defined(CONFIG_SERIAL_MULTI)
kevin.morfitt@fearnside-systems.co.ukeb0ae7f2009-10-10 13:33:11 +0900273static inline int serial_tstc_dev(unsigned int dev_index)
Harald Weltea7c185e2007-12-19 14:24:40 +0100274{
275 return _serial_tstc(dev_index);
276}
277#else
278int serial_tstc(void)
279{
280 return _serial_tstc(UART_NR);
281}
282#endif
wdenkc6097192002-11-03 00:24:07 +0000283
Harald Weltea7c185e2007-12-19 14:24:40 +0100284void _serial_puts(const char *s, const int dev_index)
285{
286 while (*s) {
kevin.morfitt@fearnside-systems.co.ukeb0ae7f2009-10-10 13:33:11 +0900287 _serial_putc(*s++, dev_index);
Harald Weltea7c185e2007-12-19 14:24:40 +0100288 }
289}
kevin.morfitt@fearnside-systems.co.ukeb0ae7f2009-10-10 13:33:11 +0900290
Harald Weltea7c185e2007-12-19 14:24:40 +0100291#if defined(CONFIG_SERIAL_MULTI)
kevin.morfitt@fearnside-systems.co.ukeb0ae7f2009-10-10 13:33:11 +0900292static inline void serial_puts_dev(int dev_index, const char *s)
Harald Weltea7c185e2007-12-19 14:24:40 +0100293{
294 _serial_puts(s, dev_index);
295}
296#else
kevin.morfitt@fearnside-systems.co.ukeb0ae7f2009-10-10 13:33:11 +0900297void serial_puts(const char *s)
wdenkc6097192002-11-03 00:24:07 +0000298{
Harald Weltea7c185e2007-12-19 14:24:40 +0100299 _serial_puts(s, UART_NR);
wdenkc6097192002-11-03 00:24:07 +0000300}
Harald Weltea7c185e2007-12-19 14:24:40 +0100301#endif
302
303#if defined(CONFIG_SERIAL_MULTI)
304DECLARE_S3C_SERIAL_FUNCTIONS(0);
305struct serial_device s3c24xx_serial0_device =
Mike Frysinger1c9a5602011-04-29 18:03:31 +0000306INIT_S3C_SERIAL_STRUCTURE(0, "s3ser0");
Harald Weltea7c185e2007-12-19 14:24:40 +0100307DECLARE_S3C_SERIAL_FUNCTIONS(1);
308struct serial_device s3c24xx_serial1_device =
Mike Frysinger1c9a5602011-04-29 18:03:31 +0000309INIT_S3C_SERIAL_STRUCTURE(1, "s3ser1");
Harald Weltea7c185e2007-12-19 14:24:40 +0100310DECLARE_S3C_SERIAL_FUNCTIONS(2);
311struct serial_device s3c24xx_serial2_device =
Mike Frysinger1c9a5602011-04-29 18:03:31 +0000312INIT_S3C_SERIAL_STRUCTURE(2, "s3ser2");
Mike Frysinger6c768ca2011-04-29 18:03:29 +0000313
314__weak struct serial_device *default_serial_console(void)
315{
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;
322#else
323#error "CONFIG_SERIAL? missing."
324#endif
325}
Marek Vasut28af6382012-09-12 16:01:16 +0200326
327void s3c24xx_serial_initialize(void)
328{
329 serial_register(&s3c24xx_serial0_device);
330 serial_register(&s3c24xx_serial1_device);
331 serial_register(&s3c24xx_serial2_device);
332}
Harald Weltea7c185e2007-12-19 14:24:40 +0100333#endif /* CONFIG_SERIAL_MULTI */