blob: 89f5d68dd0897ce0886b9eaf00f0f5e92f45c8ac [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 *
Wolfgang Denkd79de1d2013-07-08 09:37:19 +02008 * SPDX-License-Identifier: GPL-2.0+
Minkyu Kangfca30842009-10-01 17:20:28 +09009 */
10
11#include <common.h>
Rajeshwari Shindebd19fa92013-06-24 16:47:22 +053012#include <fdtdec.h>
Mike Frysingerf96c0422011-04-29 18:03:29 +000013#include <linux/compiler.h>
Minkyu Kangfca30842009-10-01 17:20:28 +090014#include <asm/io.h>
15#include <asm/arch/uart.h>
16#include <asm/arch/clk.h>
17#include <serial.h>
18
John Rigby0d21ed02010-12-20 18:27:51 -070019DECLARE_GLOBAL_DATA_PTR;
20
Akshay Saraswat63f10902013-03-21 20:33:04 +000021#define RX_FIFO_COUNT_MASK 0xff
22#define RX_FIFO_FULL_MASK (1 << 8)
23#define TX_FIFO_FULL_MASK (1 << 24)
24
Rajeshwari Shindebd19fa92013-06-24 16:47:22 +053025/* Information about a serial port */
26struct fdt_serial {
27 u32 base_addr; /* address of registers in physical memory */
28 u8 port_id; /* uart port number */
29 u8 enabled; /* 1 if enabled, 0 if disabled */
30} config __attribute__ ((section(".data")));
31
Minkyu Kangbaa36882010-03-24 16:59:30 +090032static inline struct s5p_uart *s5p_get_base_uart(int dev_index)
Minkyu Kangfca30842009-10-01 17:20:28 +090033{
Rajeshwari Shindebd19fa92013-06-24 16:47:22 +053034#ifdef CONFIG_OF_CONTROL
35 return (struct s5p_uart *)(config.base_addr);
36#else
Minkyu Kangbaa36882010-03-24 16:59:30 +090037 u32 offset = dev_index * sizeof(struct s5p_uart);
Minkyu Kangc8189842010-08-13 16:07:35 +090038 return (struct s5p_uart *)(samsung_get_base_uart() + offset);
Rajeshwari Shindebd19fa92013-06-24 16:47:22 +053039#endif
Minkyu Kangfca30842009-10-01 17:20:28 +090040}
41
42/*
Minkyu Kangbaa36882010-03-24 16:59:30 +090043 * The coefficient, used to calculate the baudrate on S5P UARTs is
Minkyu Kangfca30842009-10-01 17:20:28 +090044 * calculated as
45 * C = UBRDIV * 16 + number_of_set_bits_in_UDIVSLOT
46 * however, section 31.6.11 of the datasheet doesn't recomment using 1 for 1,
47 * 3 for 2, ... (2^n - 1) for n, instead, they suggest using these constants:
48 */
49static const int udivslot[] = {
50 0,
51 0x0080,
52 0x0808,
53 0x0888,
54 0x2222,
55 0x4924,
56 0x4a52,
57 0x54aa,
58 0x5555,
59 0xd555,
60 0xd5d5,
61 0xddd5,
62 0xdddd,
63 0xdfdd,
64 0xdfdf,
65 0xffdf,
66};
67
Axel Lina21e3792013-10-16 09:47:13 +080068static void serial_setbrg_dev(const int dev_index)
Minkyu Kangfca30842009-10-01 17:20:28 +090069{
Minkyu Kangbaa36882010-03-24 16:59:30 +090070 struct s5p_uart *const uart = s5p_get_base_uart(dev_index);
Minkyu Kang36f25cf2010-08-24 15:51:55 +090071 u32 uclk = get_uart_clk(dev_index);
Minkyu Kangfca30842009-10-01 17:20:28 +090072 u32 baudrate = gd->baudrate;
73 u32 val;
74
Rajeshwari Shindebd19fa92013-06-24 16:47:22 +053075#if defined(CONFIG_SILENT_CONSOLE) && \
76 defined(CONFIG_OF_CONTROL) && \
77 !defined(CONFIG_SPL_BUILD)
78 if (fdtdec_get_config_int(gd->fdt_blob, "silent_console", 0))
79 gd->flags |= GD_FLG_SILENT;
80#endif
81
82 if (!config.enabled)
83 return;
84
Minkyu Kang36f25cf2010-08-24 15:51:55 +090085 val = uclk / baudrate;
Minkyu Kangfca30842009-10-01 17:20:28 +090086
87 writel(val / 16 - 1, &uart->ubrdiv);
Minkyu Kangbfa14242010-09-28 14:35:02 +090088
Minkyu Kangafae8aa2011-01-24 14:43:25 +090089 if (s5p_uart_divslot())
Minkyu Kangbfa14242010-09-28 14:35:02 +090090 writew(udivslot[val % 16], &uart->rest.slot);
91 else
92 writeb(val % 16, &uart->rest.value);
Minkyu Kangfca30842009-10-01 17:20:28 +090093}
94
95/*
96 * Initialise the serial port with the given baudrate. The settings
97 * are always 8 data bits, no parity, 1 stop bit, no start bits.
98 */
Axel Lina21e3792013-10-16 09:47:13 +080099static int serial_init_dev(const int dev_index)
Minkyu Kangfca30842009-10-01 17:20:28 +0900100{
Minkyu Kangbaa36882010-03-24 16:59:30 +0900101 struct s5p_uart *const uart = s5p_get_base_uart(dev_index);
Minkyu Kangfca30842009-10-01 17:20:28 +0900102
Akshay Saraswat63f10902013-03-21 20:33:04 +0000103 /* enable FIFOs */
104 writel(0x1, &uart->ufcon);
Minkyu Kangfca30842009-10-01 17:20:28 +0900105 writel(0, &uart->umcon);
106 /* 8N1 */
107 writel(0x3, &uart->ulcon);
108 /* No interrupts, no DMA, pure polling */
109 writel(0x245, &uart->ucon);
110
111 serial_setbrg_dev(dev_index);
112
113 return 0;
114}
115
Minkyu Kang9455aab2009-11-10 20:23:50 +0900116static int serial_err_check(const int dev_index, int op)
Minkyu Kangfca30842009-10-01 17:20:28 +0900117{
Minkyu Kangbaa36882010-03-24 16:59:30 +0900118 struct s5p_uart *const uart = s5p_get_base_uart(dev_index);
Minkyu Kang9455aab2009-11-10 20:23:50 +0900119 unsigned int mask;
Minkyu Kangfca30842009-10-01 17:20:28 +0900120
Minkyu Kang9455aab2009-11-10 20:23:50 +0900121 /*
122 * UERSTAT
123 * Break Detect [3]
124 * Frame Err [2] : receive operation
125 * Parity Err [1] : receive operation
126 * Overrun Err [0] : receive operation
127 */
128 if (op)
129 mask = 0x8;
130 else
131 mask = 0xf;
Minkyu Kangfca30842009-10-01 17:20:28 +0900132
Minkyu Kang9455aab2009-11-10 20:23:50 +0900133 return readl(&uart->uerstat) & mask;
Minkyu Kangfca30842009-10-01 17:20:28 +0900134}
135
136/*
137 * Read a single byte from the serial port. Returns 1 on success, 0
138 * otherwise. When the function is succesfull, the character read is
139 * written into its argument c.
140 */
Axel Lina21e3792013-10-16 09:47:13 +0800141static int serial_getc_dev(const int dev_index)
Minkyu Kangfca30842009-10-01 17:20:28 +0900142{
Minkyu Kangbaa36882010-03-24 16:59:30 +0900143 struct s5p_uart *const uart = s5p_get_base_uart(dev_index);
Minkyu Kangfca30842009-10-01 17:20:28 +0900144
Rajeshwari Shindebd19fa92013-06-24 16:47:22 +0530145 if (!config.enabled)
146 return 0;
147
Minkyu Kangfca30842009-10-01 17:20:28 +0900148 /* wait for character to arrive */
Akshay Saraswat63f10902013-03-21 20:33:04 +0000149 while (!(readl(&uart->ufstat) & (RX_FIFO_COUNT_MASK |
150 RX_FIFO_FULL_MASK))) {
Minkyu Kang9455aab2009-11-10 20:23:50 +0900151 if (serial_err_check(dev_index, 0))
Minkyu Kangfca30842009-10-01 17:20:28 +0900152 return 0;
153 }
154
Minkyu Kang76229812010-07-06 20:08:29 +0900155 return (int)(readb(&uart->urxh) & 0xff);
Minkyu Kangfca30842009-10-01 17:20:28 +0900156}
157
158/*
159 * Output a single byte to the serial port.
160 */
Axel Lina21e3792013-10-16 09:47:13 +0800161static void serial_putc_dev(const char c, const int dev_index)
Minkyu Kangfca30842009-10-01 17:20:28 +0900162{
Minkyu Kangbaa36882010-03-24 16:59:30 +0900163 struct s5p_uart *const uart = s5p_get_base_uart(dev_index);
Minkyu Kangfca30842009-10-01 17:20:28 +0900164
Rajeshwari Shindebd19fa92013-06-24 16:47:22 +0530165 if (!config.enabled)
166 return;
167
Minkyu Kangfca30842009-10-01 17:20:28 +0900168 /* wait for room in the tx FIFO */
Akshay Saraswat63f10902013-03-21 20:33:04 +0000169 while ((readl(&uart->ufstat) & TX_FIFO_FULL_MASK)) {
Minkyu Kang9455aab2009-11-10 20:23:50 +0900170 if (serial_err_check(dev_index, 1))
Minkyu Kangfca30842009-10-01 17:20:28 +0900171 return;
172 }
173
Minkyu Kang76229812010-07-06 20:08:29 +0900174 writeb(c, &uart->utxh);
Minkyu Kangfca30842009-10-01 17:20:28 +0900175
176 /* If \n, also do \r */
177 if (c == '\n')
178 serial_putc('\r');
179}
180
181/*
182 * Test whether a character is in the RX buffer
183 */
Axel Lina21e3792013-10-16 09:47:13 +0800184static int serial_tstc_dev(const int dev_index)
Minkyu Kangfca30842009-10-01 17:20:28 +0900185{
Minkyu Kangbaa36882010-03-24 16:59:30 +0900186 struct s5p_uart *const uart = s5p_get_base_uart(dev_index);
Minkyu Kangfca30842009-10-01 17:20:28 +0900187
Rajeshwari Shindebd19fa92013-06-24 16:47:22 +0530188 if (!config.enabled)
189 return 0;
190
Minkyu Kangfca30842009-10-01 17:20:28 +0900191 return (int)(readl(&uart->utrstat) & 0x1);
192}
193
Axel Lina21e3792013-10-16 09:47:13 +0800194static void serial_puts_dev(const char *s, const int dev_index)
Minkyu Kangfca30842009-10-01 17:20:28 +0900195{
196 while (*s)
197 serial_putc_dev(*s++, dev_index);
198}
199
200/* Multi serial device functions */
201#define DECLARE_S5P_SERIAL_FUNCTIONS(port) \
Axel Lina21e3792013-10-16 09:47:13 +0800202static int s5p_serial##port##_init(void) { return serial_init_dev(port); } \
203static void s5p_serial##port##_setbrg(void) { serial_setbrg_dev(port); } \
204static int s5p_serial##port##_getc(void) { return serial_getc_dev(port); } \
205static int s5p_serial##port##_tstc(void) { return serial_tstc_dev(port); } \
206static void s5p_serial##port##_putc(const char c) { serial_putc_dev(c, port); } \
207static void s5p_serial##port##_puts(const char *s) { serial_puts_dev(s, port); }
Minkyu Kangfca30842009-10-01 17:20:28 +0900208
Marek Vasut5bcdf242012-09-09 18:48:28 +0200209#define INIT_S5P_SERIAL_STRUCTURE(port, __name) { \
210 .name = __name, \
211 .start = s5p_serial##port##_init, \
212 .stop = NULL, \
213 .setbrg = s5p_serial##port##_setbrg, \
214 .getc = s5p_serial##port##_getc, \
215 .tstc = s5p_serial##port##_tstc, \
216 .putc = s5p_serial##port##_putc, \
217 .puts = s5p_serial##port##_puts, \
218}
Minkyu Kangfca30842009-10-01 17:20:28 +0900219
220DECLARE_S5P_SERIAL_FUNCTIONS(0);
Minkyu Kangbaa36882010-03-24 16:59:30 +0900221struct serial_device s5p_serial0_device =
Mike Frysinger1ef72f62011-04-29 18:03:31 +0000222 INIT_S5P_SERIAL_STRUCTURE(0, "s5pser0");
Minkyu Kangfca30842009-10-01 17:20:28 +0900223DECLARE_S5P_SERIAL_FUNCTIONS(1);
Minkyu Kangbaa36882010-03-24 16:59:30 +0900224struct serial_device s5p_serial1_device =
Mike Frysinger1ef72f62011-04-29 18:03:31 +0000225 INIT_S5P_SERIAL_STRUCTURE(1, "s5pser1");
Minkyu Kangfca30842009-10-01 17:20:28 +0900226DECLARE_S5P_SERIAL_FUNCTIONS(2);
Minkyu Kangbaa36882010-03-24 16:59:30 +0900227struct serial_device s5p_serial2_device =
Mike Frysinger1ef72f62011-04-29 18:03:31 +0000228 INIT_S5P_SERIAL_STRUCTURE(2, "s5pser2");
Minkyu Kangfca30842009-10-01 17:20:28 +0900229DECLARE_S5P_SERIAL_FUNCTIONS(3);
Minkyu Kangbaa36882010-03-24 16:59:30 +0900230struct serial_device s5p_serial3_device =
Mike Frysinger1ef72f62011-04-29 18:03:31 +0000231 INIT_S5P_SERIAL_STRUCTURE(3, "s5pser3");
Mike Frysingerf96c0422011-04-29 18:03:29 +0000232
Rajeshwari Shindebd19fa92013-06-24 16:47:22 +0530233#ifdef CONFIG_OF_CONTROL
234int fdtdec_decode_console(int *index, struct fdt_serial *uart)
235{
236 const void *blob = gd->fdt_blob;
237 int node;
238
239 node = fdt_path_offset(blob, "console");
240 if (node < 0)
241 return node;
242
243 uart->base_addr = fdtdec_get_addr(blob, node, "reg");
244 if (uart->base_addr == FDT_ADDR_T_NONE)
245 return -FDT_ERR_NOTFOUND;
246
247 uart->port_id = fdtdec_get_int(blob, node, "id", -1);
248 uart->enabled = fdtdec_get_is_enabled(blob, node);
249
250 return 0;
251}
252#endif
253
Mike Frysingerf96c0422011-04-29 18:03:29 +0000254__weak struct serial_device *default_serial_console(void)
255{
Rajeshwari Shindebd19fa92013-06-24 16:47:22 +0530256#ifdef CONFIG_OF_CONTROL
257 int index = 0;
258
259 if ((!config.base_addr) && (fdtdec_decode_console(&index, &config))) {
260 debug("Cannot decode default console node\n");
261 return NULL;
262 }
263
264 switch (config.port_id) {
265 case 0:
266 return &s5p_serial0_device;
267 case 1:
268 return &s5p_serial1_device;
269 case 2:
270 return &s5p_serial2_device;
271 case 3:
272 return &s5p_serial3_device;
273 default:
274 debug("Unknown config.port_id: %d", config.port_id);
275 break;
276 }
277
278 return NULL;
279#else
280 config.enabled = 1;
Mike Frysingerf96c0422011-04-29 18:03:29 +0000281#if defined(CONFIG_SERIAL0)
282 return &s5p_serial0_device;
283#elif defined(CONFIG_SERIAL1)
284 return &s5p_serial1_device;
285#elif defined(CONFIG_SERIAL2)
286 return &s5p_serial2_device;
287#elif defined(CONFIG_SERIAL3)
288 return &s5p_serial3_device;
289#else
290#error "CONFIG_SERIAL? missing."
291#endif
Rajeshwari Shindebd19fa92013-06-24 16:47:22 +0530292#endif
Mike Frysingerf96c0422011-04-29 18:03:29 +0000293}
Marek Vasut533e31e2012-09-12 19:39:57 +0200294
295void s5p_serial_initialize(void)
296{
297 serial_register(&s5p_serial0_device);
298 serial_register(&s5p_serial1_device);
299 serial_register(&s5p_serial2_device);
300 serial_register(&s5p_serial3_device);
301}