blob: 4d214c396f706b0269ce630b245a515e4dd3f46d [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>
Mike Frysingerf96c0422011-04-29 18:03:29 +000022#include <linux/compiler.h>
kevin.morfitt@fearnside-systems.co.uke0d81312009-11-17 18:30:34 +090023#include <asm/arch/s3c24x0_cpu.h>
wdenkc6097192002-11-03 00:24:07 +000024
Wolfgang Denk6405a152006-03-31 18:32:53 +020025DECLARE_GLOBAL_DATA_PTR;
26
wdenk7539dea2003-06-19 23:01:32 +000027#ifdef CONFIG_SERIAL1
28#define UART_NR S3C24X0_UART0
29
wdenkc35ba4e2004-03-14 22:25:36 +000030#elif defined(CONFIG_SERIAL2)
wdenk7539dea2003-06-19 23:01:32 +000031#define UART_NR S3C24X0_UART1
32
wdenkc35ba4e2004-03-14 22:25:36 +000033#elif defined(CONFIG_SERIAL3)
wdenk7539dea2003-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.uk1464f4d2009-10-10 13:33:11 +090040#include <asm/io.h>
Harald Welteda412622007-12-19 14:24:40 +010041#include <serial.h>
42
43/* Multi serial device functions */
44#define DECLARE_S3C_SERIAL_FUNCTIONS(port) \
kevin.morfitt@fearnside-systems.co.uk1464f4d2009-10-10 13:33:11 +090045 int s3serial##port##_init(void) \
46 { \
47 return serial_init_dev(port); \
48 } \
49 void s3serial##port##_setbrg(void) \
50 { \
51 serial_setbrg_dev(port); \
52 } \
53 int s3serial##port##_getc(void) \
54 { \
55 return serial_getc_dev(port); \
56 } \
57 int s3serial##port##_tstc(void) \
58 { \
59 return serial_tstc_dev(port); \
60 } \
61 void s3serial##port##_putc(const char c) \
62 { \
63 serial_putc_dev(port, c); \
64 } \
65 void s3serial##port##_puts(const char *s) \
66 { \
67 serial_puts_dev(port, s); \
68 }
Harald Welteda412622007-12-19 14:24:40 +010069
Marek Vasut5bcdf242012-09-09 18:48:28 +020070#define INIT_S3C_SERIAL_STRUCTURE(port, __name) { \
71 .name = __name, \
72 .start = s3serial##port##_init, \
73 .stop = NULL, \
74 .setbrg = s3serial##port##_setbrg, \
75 .getc = s3serial##port##_getc, \
76 .tstc = s3serial##port##_tstc, \
77 .putc = s3serial##port##_putc, \
78 .puts = s3serial##port##_puts, \
kevin.morfitt@fearnside-systems.co.uk1464f4d2009-10-10 13:33:11 +090079}
Harald Welteda412622007-12-19 14:24:40 +010080
kevin.morfitt@fearnside-systems.co.uk1464f4d2009-10-10 13:33:11 +090081#ifdef CONFIG_HWFLOW
82static int hwflow;
83#endif
84
Harald Welteda412622007-12-19 14:24:40 +010085void _serial_setbrg(const int dev_index)
wdenkc6097192002-11-03 00:24:07 +000086{
kevin.morfitt@fearnside-systems.co.uk1464f4d2009-10-10 13:33:11 +090087 struct s3c24x0_uart *uart = s3c24x0_get_base_uart(dev_index);
wdenkc6097192002-11-03 00:24:07 +000088 unsigned int reg = 0;
Harald Welteda412622007-12-19 14:24:40 +010089 int i;
wdenkc6097192002-11-03 00:24:07 +000090
91 /* value is calculated so : (int)(PCLK/16./baudrate) -1 */
92 reg = get_PCLK() / (16 * gd->baudrate) - 1;
93
C Nauman383c43e2010-10-26 23:04:31 +090094 writel(reg, &uart->ubrdiv);
kevin.morfitt@fearnside-systems.co.uk1464f4d2009-10-10 13:33:11 +090095 for (i = 0; i < 100; i++)
96 /* Delay */ ;
Harald Welteda412622007-12-19 14:24:40 +010097}
kevin.morfitt@fearnside-systems.co.uk1464f4d2009-10-10 13:33:11 +090098
kevin.morfitt@fearnside-systems.co.uk1464f4d2009-10-10 13:33:11 +090099static inline void serial_setbrg_dev(unsigned int dev_index)
Harald Welteda412622007-12-19 14:24:40 +0100100{
101 _serial_setbrg(dev_index);
102}
Harald Welteda412622007-12-19 14:24:40 +0100103
104/* Initialise the serial port. The settings are always 8 data bits, no parity,
105 * 1 stop bit, no start bits.
106 */
107static int serial_init_dev(const int dev_index)
108{
kevin.morfitt@fearnside-systems.co.uk1464f4d2009-10-10 13:33:11 +0900109 struct s3c24x0_uart *uart = s3c24x0_get_base_uart(dev_index);
110
111#ifdef CONFIG_HWFLOW
112 hwflow = 0; /* turned off by default */
113#endif
Harald Welteda412622007-12-19 14:24:40 +0100114
wdenkc6097192002-11-03 00:24:07 +0000115 /* FIFO enable, Tx/Rx FIFO clear */
C Nauman383c43e2010-10-26 23:04:31 +0900116 writel(0x07, &uart->ufcon);
117 writel(0x0, &uart->umcon);
Harald Welteda412622007-12-19 14:24:40 +0100118
wdenkc6097192002-11-03 00:24:07 +0000119 /* Normal,No parity,1 stop,8 bit */
C Nauman383c43e2010-10-26 23:04:31 +0900120 writel(0x3, &uart->ulcon);
wdenkc6097192002-11-03 00:24:07 +0000121 /*
122 * tx=level,rx=edge,disable timeout int.,enable rx error int.,
123 * normal,interrupt or polling
124 */
C Nauman383c43e2010-10-26 23:04:31 +0900125 writel(0x245, &uart->ucon);
wdenkc6097192002-11-03 00:24:07 +0000126
127#ifdef CONFIG_HWFLOW
C Nauman383c43e2010-10-26 23:04:31 +0900128 writel(0x1, &uart->umcon); /* rts up */
wdenkc6097192002-11-03 00:24:07 +0000129#endif
Harald Welteda412622007-12-19 14:24:40 +0100130
131 /* FIXME: This is sooooooooooooooooooo ugly */
132#if defined(CONFIG_ARCH_GTA02_v1) || defined(CONFIG_ARCH_GTA02_v2)
133 /* we need auto hw flow control on the gsm and gps port */
134 if (dev_index == 0 || dev_index == 1)
C Nauman383c43e2010-10-26 23:04:31 +0900135 writel(0x10, &uart->umcon);
Harald Welteda412622007-12-19 14:24:40 +0100136#endif
137 _serial_setbrg(dev_index);
138
139 return (0);
wdenkc6097192002-11-03 00:24:07 +0000140}
wdenkc6097192002-11-03 00:24:07 +0000141
142/*
143 * Read a single byte from the serial port. Returns 1 on success, 0
144 * otherwise. When the function is succesfull, the character read is
145 * written into its argument c.
146 */
kevin.morfitt@fearnside-systems.co.uk1464f4d2009-10-10 13:33:11 +0900147int _serial_getc(const int dev_index)
wdenkc6097192002-11-03 00:24:07 +0000148{
kevin.morfitt@fearnside-systems.co.uk1464f4d2009-10-10 13:33:11 +0900149 struct s3c24x0_uart *uart = s3c24x0_get_base_uart(dev_index);
wdenk57b2d802003-06-27 21:31:46 +0000150
C Nauman383c43e2010-10-26 23:04:31 +0900151 while (!(readl(&uart->utrstat) & 0x1))
kevin.morfitt@fearnside-systems.co.uk1464f4d2009-10-10 13:33:11 +0900152 /* wait for character to arrive */ ;
wdenkc6097192002-11-03 00:24:07 +0000153
C Nauman383c43e2010-10-26 23:04:31 +0900154 return readb(&uart->urxh) & 0xff;
wdenkc6097192002-11-03 00:24:07 +0000155}
kevin.morfitt@fearnside-systems.co.uk1464f4d2009-10-10 13:33:11 +0900156
Harald Welteda412622007-12-19 14:24:40 +0100157static inline int serial_getc_dev(unsigned int dev_index)
158{
159 return _serial_getc(dev_index);
160}
wdenkc6097192002-11-03 00:24:07 +0000161
162#ifdef CONFIG_HWFLOW
wdenkc6097192002-11-03 00:24:07 +0000163int hwflow_onoff(int on)
164{
kevin.morfitt@fearnside-systems.co.uk1464f4d2009-10-10 13:33:11 +0900165 switch (on) {
wdenkc6097192002-11-03 00:24:07 +0000166 case 0:
167 default:
kevin.morfitt@fearnside-systems.co.uk1464f4d2009-10-10 13:33:11 +0900168 break; /* return current */
wdenkc6097192002-11-03 00:24:07 +0000169 case 1:
kevin.morfitt@fearnside-systems.co.uk1464f4d2009-10-10 13:33:11 +0900170 hwflow = 1; /* turn on */
wdenkc6097192002-11-03 00:24:07 +0000171 break;
172 case -1:
kevin.morfitt@fearnside-systems.co.uk1464f4d2009-10-10 13:33:11 +0900173 hwflow = 0; /* turn off */
wdenkc6097192002-11-03 00:24:07 +0000174 break;
175 }
176 return hwflow;
177}
178#endif
179
180#ifdef CONFIG_MODEM_SUPPORT
181static int be_quiet = 0;
182void disable_putc(void)
183{
184 be_quiet = 1;
185}
186
187void enable_putc(void)
188{
189 be_quiet = 0;
190}
191#endif
192
193
194/*
195 * Output a single byte to the serial port.
196 */
kevin.morfitt@fearnside-systems.co.uk1464f4d2009-10-10 13:33:11 +0900197void _serial_putc(const char c, const int dev_index)
wdenkc6097192002-11-03 00:24:07 +0000198{
kevin.morfitt@fearnside-systems.co.uk1464f4d2009-10-10 13:33:11 +0900199 struct s3c24x0_uart *uart = s3c24x0_get_base_uart(dev_index);
wdenkc6097192002-11-03 00:24:07 +0000200#ifdef CONFIG_MODEM_SUPPORT
201 if (be_quiet)
202 return;
203#endif
204
C Nauman383c43e2010-10-26 23:04:31 +0900205 while (!(readl(&uart->utrstat) & 0x2))
kevin.morfitt@fearnside-systems.co.uk1464f4d2009-10-10 13:33:11 +0900206 /* wait for room in the tx FIFO */ ;
wdenkc6097192002-11-03 00:24:07 +0000207
208#ifdef CONFIG_HWFLOW
C Nauman383c43e2010-10-26 23:04:31 +0900209 while (hwflow && !(readl(&uart->umstat) & 0x1))
kevin.morfitt@fearnside-systems.co.uk1464f4d2009-10-10 13:33:11 +0900210 /* Wait for CTS up */ ;
wdenkc6097192002-11-03 00:24:07 +0000211#endif
212
C Nauman383c43e2010-10-26 23:04:31 +0900213 writeb(c, &uart->utxh);
wdenkc6097192002-11-03 00:24:07 +0000214
215 /* If \n, also do \r */
216 if (c == '\n')
kevin.morfitt@fearnside-systems.co.uk1464f4d2009-10-10 13:33:11 +0900217 serial_putc('\r');
wdenkc6097192002-11-03 00:24:07 +0000218}
kevin.morfitt@fearnside-systems.co.uk1464f4d2009-10-10 13:33:11 +0900219
Harald Welteda412622007-12-19 14:24:40 +0100220static inline void serial_putc_dev(unsigned int dev_index, const char c)
221{
222 _serial_putc(c, dev_index);
223}
wdenkc6097192002-11-03 00:24:07 +0000224
225/*
226 * Test whether a character is in the RX buffer
227 */
Harald Welteda412622007-12-19 14:24:40 +0100228int _serial_tstc(const int dev_index)
wdenkc6097192002-11-03 00:24:07 +0000229{
kevin.morfitt@fearnside-systems.co.uk1464f4d2009-10-10 13:33:11 +0900230 struct s3c24x0_uart *uart = s3c24x0_get_base_uart(dev_index);
wdenk7539dea2003-06-19 23:01:32 +0000231
C Nauman383c43e2010-10-26 23:04:31 +0900232 return readl(&uart->utrstat) & 0x1;
wdenkc6097192002-11-03 00:24:07 +0000233}
kevin.morfitt@fearnside-systems.co.uk1464f4d2009-10-10 13:33:11 +0900234
kevin.morfitt@fearnside-systems.co.uk1464f4d2009-10-10 13:33:11 +0900235static inline int serial_tstc_dev(unsigned int dev_index)
Harald Welteda412622007-12-19 14:24:40 +0100236{
237 return _serial_tstc(dev_index);
238}
wdenkc6097192002-11-03 00:24:07 +0000239
Harald Welteda412622007-12-19 14:24:40 +0100240void _serial_puts(const char *s, const int dev_index)
wdenkc6097192002-11-03 00:24:07 +0000241{
242 while (*s) {
kevin.morfitt@fearnside-systems.co.uk1464f4d2009-10-10 13:33:11 +0900243 _serial_putc(*s++, dev_index);
wdenkc6097192002-11-03 00:24:07 +0000244 }
245}
kevin.morfitt@fearnside-systems.co.uk1464f4d2009-10-10 13:33:11 +0900246
kevin.morfitt@fearnside-systems.co.uk1464f4d2009-10-10 13:33:11 +0900247static inline void serial_puts_dev(int dev_index, const char *s)
Harald Welteda412622007-12-19 14:24:40 +0100248{
249 _serial_puts(s, dev_index);
250}
Harald Welteda412622007-12-19 14:24:40 +0100251
Harald Welteda412622007-12-19 14:24:40 +0100252DECLARE_S3C_SERIAL_FUNCTIONS(0);
253struct serial_device s3c24xx_serial0_device =
Mike Frysinger1ef72f62011-04-29 18:03:31 +0000254INIT_S3C_SERIAL_STRUCTURE(0, "s3ser0");
Harald Welteda412622007-12-19 14:24:40 +0100255DECLARE_S3C_SERIAL_FUNCTIONS(1);
256struct serial_device s3c24xx_serial1_device =
Mike Frysinger1ef72f62011-04-29 18:03:31 +0000257INIT_S3C_SERIAL_STRUCTURE(1, "s3ser1");
Harald Welteda412622007-12-19 14:24:40 +0100258DECLARE_S3C_SERIAL_FUNCTIONS(2);
259struct serial_device s3c24xx_serial2_device =
Mike Frysinger1ef72f62011-04-29 18:03:31 +0000260INIT_S3C_SERIAL_STRUCTURE(2, "s3ser2");
Mike Frysingerf96c0422011-04-29 18:03:29 +0000261
262__weak struct serial_device *default_serial_console(void)
263{
264#if defined(CONFIG_SERIAL1)
265 return &s3c24xx_serial0_device;
266#elif defined(CONFIG_SERIAL2)
267 return &s3c24xx_serial1_device;
268#elif defined(CONFIG_SERIAL3)
269 return &s3c24xx_serial2_device;
270#else
271#error "CONFIG_SERIAL? missing."
272#endif
273}
Marek Vasutb3e48aa2012-09-12 16:01:16 +0200274
275void s3c24xx_serial_initialize(void)
276{
277 serial_register(&s3c24xx_serial0_device);
278 serial_register(&s3c24xx_serial1_device);
279 serial_register(&s3c24xx_serial2_device);
280}