blob: e65125ccd7422dda5dd5a5e11956701c0c43c2ae [file] [log] [blame]
Minkyu Kangfca30842009-10-01 17:20:28 +09001/*
2 * (C) Copyright 2009 SAMSUNG Electronics
3 * Minkyu Kang <mk7.kang@samsung.com>
4 * Heungjun Kim <riverful.kim@samsung.com>
5 *
6 * based on drivers/serial/s3c64xx.c
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 *
22 */
23
24#include <common.h>
Mike Frysingerf96c0422011-04-29 18:03:29 +000025#include <linux/compiler.h>
Minkyu Kangfca30842009-10-01 17:20:28 +090026#include <asm/io.h>
27#include <asm/arch/uart.h>
28#include <asm/arch/clk.h>
29#include <serial.h>
30
John Rigby0d21ed02010-12-20 18:27:51 -070031DECLARE_GLOBAL_DATA_PTR;
32
Akshay Saraswat63f10902013-03-21 20:33:04 +000033#define RX_FIFO_COUNT_MASK 0xff
34#define RX_FIFO_FULL_MASK (1 << 8)
35#define TX_FIFO_FULL_MASK (1 << 24)
36
Minkyu Kangbaa36882010-03-24 16:59:30 +090037static inline struct s5p_uart *s5p_get_base_uart(int dev_index)
Minkyu Kangfca30842009-10-01 17:20:28 +090038{
Minkyu Kangbaa36882010-03-24 16:59:30 +090039 u32 offset = dev_index * sizeof(struct s5p_uart);
Minkyu Kangc8189842010-08-13 16:07:35 +090040 return (struct s5p_uart *)(samsung_get_base_uart() + offset);
Minkyu Kangfca30842009-10-01 17:20:28 +090041}
42
43/*
Minkyu Kangbaa36882010-03-24 16:59:30 +090044 * The coefficient, used to calculate the baudrate on S5P UARTs is
Minkyu Kangfca30842009-10-01 17:20:28 +090045 * calculated as
46 * C = UBRDIV * 16 + number_of_set_bits_in_UDIVSLOT
47 * however, section 31.6.11 of the datasheet doesn't recomment using 1 for 1,
48 * 3 for 2, ... (2^n - 1) for n, instead, they suggest using these constants:
49 */
50static const int udivslot[] = {
51 0,
52 0x0080,
53 0x0808,
54 0x0888,
55 0x2222,
56 0x4924,
57 0x4a52,
58 0x54aa,
59 0x5555,
60 0xd555,
61 0xd5d5,
62 0xddd5,
63 0xdddd,
64 0xdfdd,
65 0xdfdf,
66 0xffdf,
67};
68
69void serial_setbrg_dev(const int dev_index)
70{
Minkyu Kangbaa36882010-03-24 16:59:30 +090071 struct s5p_uart *const uart = s5p_get_base_uart(dev_index);
Minkyu Kang36f25cf2010-08-24 15:51:55 +090072 u32 uclk = get_uart_clk(dev_index);
Minkyu Kangfca30842009-10-01 17:20:28 +090073 u32 baudrate = gd->baudrate;
74 u32 val;
75
Minkyu Kang36f25cf2010-08-24 15:51:55 +090076 val = uclk / baudrate;
Minkyu Kangfca30842009-10-01 17:20:28 +090077
78 writel(val / 16 - 1, &uart->ubrdiv);
Minkyu Kangbfa14242010-09-28 14:35:02 +090079
Minkyu Kangafae8aa2011-01-24 14:43:25 +090080 if (s5p_uart_divslot())
Minkyu Kangbfa14242010-09-28 14:35:02 +090081 writew(udivslot[val % 16], &uart->rest.slot);
82 else
83 writeb(val % 16, &uart->rest.value);
Minkyu Kangfca30842009-10-01 17:20:28 +090084}
85
86/*
87 * Initialise the serial port with the given baudrate. The settings
88 * are always 8 data bits, no parity, 1 stop bit, no start bits.
89 */
90int serial_init_dev(const int dev_index)
91{
Minkyu Kangbaa36882010-03-24 16:59:30 +090092 struct s5p_uart *const uart = s5p_get_base_uart(dev_index);
Minkyu Kangfca30842009-10-01 17:20:28 +090093
Akshay Saraswat63f10902013-03-21 20:33:04 +000094 /* enable FIFOs */
95 writel(0x1, &uart->ufcon);
Minkyu Kangfca30842009-10-01 17:20:28 +090096 writel(0, &uart->umcon);
97 /* 8N1 */
98 writel(0x3, &uart->ulcon);
99 /* No interrupts, no DMA, pure polling */
100 writel(0x245, &uart->ucon);
101
102 serial_setbrg_dev(dev_index);
103
104 return 0;
105}
106
Minkyu Kang9455aab2009-11-10 20:23:50 +0900107static int serial_err_check(const int dev_index, int op)
Minkyu Kangfca30842009-10-01 17:20:28 +0900108{
Minkyu Kangbaa36882010-03-24 16:59:30 +0900109 struct s5p_uart *const uart = s5p_get_base_uart(dev_index);
Minkyu Kang9455aab2009-11-10 20:23:50 +0900110 unsigned int mask;
Minkyu Kangfca30842009-10-01 17:20:28 +0900111
Minkyu Kang9455aab2009-11-10 20:23:50 +0900112 /*
113 * UERSTAT
114 * Break Detect [3]
115 * Frame Err [2] : receive operation
116 * Parity Err [1] : receive operation
117 * Overrun Err [0] : receive operation
118 */
119 if (op)
120 mask = 0x8;
121 else
122 mask = 0xf;
Minkyu Kangfca30842009-10-01 17:20:28 +0900123
Minkyu Kang9455aab2009-11-10 20:23:50 +0900124 return readl(&uart->uerstat) & mask;
Minkyu Kangfca30842009-10-01 17:20:28 +0900125}
126
127/*
128 * Read a single byte from the serial port. Returns 1 on success, 0
129 * otherwise. When the function is succesfull, the character read is
130 * written into its argument c.
131 */
132int serial_getc_dev(const int dev_index)
133{
Minkyu Kangbaa36882010-03-24 16:59:30 +0900134 struct s5p_uart *const uart = s5p_get_base_uart(dev_index);
Minkyu Kangfca30842009-10-01 17:20:28 +0900135
136 /* wait for character to arrive */
Akshay Saraswat63f10902013-03-21 20:33:04 +0000137 while (!(readl(&uart->ufstat) & (RX_FIFO_COUNT_MASK |
138 RX_FIFO_FULL_MASK))) {
Minkyu Kang9455aab2009-11-10 20:23:50 +0900139 if (serial_err_check(dev_index, 0))
Minkyu Kangfca30842009-10-01 17:20:28 +0900140 return 0;
141 }
142
Minkyu Kang76229812010-07-06 20:08:29 +0900143 return (int)(readb(&uart->urxh) & 0xff);
Minkyu Kangfca30842009-10-01 17:20:28 +0900144}
145
146/*
147 * Output a single byte to the serial port.
148 */
149void serial_putc_dev(const char c, const int dev_index)
150{
Minkyu Kangbaa36882010-03-24 16:59:30 +0900151 struct s5p_uart *const uart = s5p_get_base_uart(dev_index);
Minkyu Kangfca30842009-10-01 17:20:28 +0900152
153 /* wait for room in the tx FIFO */
Akshay Saraswat63f10902013-03-21 20:33:04 +0000154 while ((readl(&uart->ufstat) & TX_FIFO_FULL_MASK)) {
Minkyu Kang9455aab2009-11-10 20:23:50 +0900155 if (serial_err_check(dev_index, 1))
Minkyu Kangfca30842009-10-01 17:20:28 +0900156 return;
157 }
158
Minkyu Kang76229812010-07-06 20:08:29 +0900159 writeb(c, &uart->utxh);
Minkyu Kangfca30842009-10-01 17:20:28 +0900160
161 /* If \n, also do \r */
162 if (c == '\n')
163 serial_putc('\r');
164}
165
166/*
167 * Test whether a character is in the RX buffer
168 */
169int serial_tstc_dev(const int dev_index)
170{
Minkyu Kangbaa36882010-03-24 16:59:30 +0900171 struct s5p_uart *const uart = s5p_get_base_uart(dev_index);
Minkyu Kangfca30842009-10-01 17:20:28 +0900172
173 return (int)(readl(&uart->utrstat) & 0x1);
174}
175
176void serial_puts_dev(const char *s, const int dev_index)
177{
178 while (*s)
179 serial_putc_dev(*s++, dev_index);
180}
181
182/* Multi serial device functions */
183#define DECLARE_S5P_SERIAL_FUNCTIONS(port) \
184int s5p_serial##port##_init(void) { return serial_init_dev(port); } \
185void s5p_serial##port##_setbrg(void) { serial_setbrg_dev(port); } \
186int s5p_serial##port##_getc(void) { return serial_getc_dev(port); } \
187int s5p_serial##port##_tstc(void) { return serial_tstc_dev(port); } \
188void s5p_serial##port##_putc(const char c) { serial_putc_dev(c, port); } \
189void s5p_serial##port##_puts(const char *s) { serial_puts_dev(s, port); }
190
Marek Vasut5bcdf242012-09-09 18:48:28 +0200191#define INIT_S5P_SERIAL_STRUCTURE(port, __name) { \
192 .name = __name, \
193 .start = s5p_serial##port##_init, \
194 .stop = NULL, \
195 .setbrg = s5p_serial##port##_setbrg, \
196 .getc = s5p_serial##port##_getc, \
197 .tstc = s5p_serial##port##_tstc, \
198 .putc = s5p_serial##port##_putc, \
199 .puts = s5p_serial##port##_puts, \
200}
Minkyu Kangfca30842009-10-01 17:20:28 +0900201
202DECLARE_S5P_SERIAL_FUNCTIONS(0);
Minkyu Kangbaa36882010-03-24 16:59:30 +0900203struct serial_device s5p_serial0_device =
Mike Frysinger1ef72f62011-04-29 18:03:31 +0000204 INIT_S5P_SERIAL_STRUCTURE(0, "s5pser0");
Minkyu Kangfca30842009-10-01 17:20:28 +0900205DECLARE_S5P_SERIAL_FUNCTIONS(1);
Minkyu Kangbaa36882010-03-24 16:59:30 +0900206struct serial_device s5p_serial1_device =
Mike Frysinger1ef72f62011-04-29 18:03:31 +0000207 INIT_S5P_SERIAL_STRUCTURE(1, "s5pser1");
Minkyu Kangfca30842009-10-01 17:20:28 +0900208DECLARE_S5P_SERIAL_FUNCTIONS(2);
Minkyu Kangbaa36882010-03-24 16:59:30 +0900209struct serial_device s5p_serial2_device =
Mike Frysinger1ef72f62011-04-29 18:03:31 +0000210 INIT_S5P_SERIAL_STRUCTURE(2, "s5pser2");
Minkyu Kangfca30842009-10-01 17:20:28 +0900211DECLARE_S5P_SERIAL_FUNCTIONS(3);
Minkyu Kangbaa36882010-03-24 16:59:30 +0900212struct serial_device s5p_serial3_device =
Mike Frysinger1ef72f62011-04-29 18:03:31 +0000213 INIT_S5P_SERIAL_STRUCTURE(3, "s5pser3");
Mike Frysingerf96c0422011-04-29 18:03:29 +0000214
215__weak struct serial_device *default_serial_console(void)
216{
217#if defined(CONFIG_SERIAL0)
218 return &s5p_serial0_device;
219#elif defined(CONFIG_SERIAL1)
220 return &s5p_serial1_device;
221#elif defined(CONFIG_SERIAL2)
222 return &s5p_serial2_device;
223#elif defined(CONFIG_SERIAL3)
224 return &s5p_serial3_device;
225#else
226#error "CONFIG_SERIAL? missing."
227#endif
228}
Marek Vasut533e31e2012-09-12 19:39:57 +0200229
230void s5p_serial_initialize(void)
231{
232 serial_register(&s5p_serial0_device);
233 serial_register(&s5p_serial1_device);
234 serial_register(&s5p_serial2_device);
235 serial_register(&s5p_serial3_device);
236}