blob: cb55c5ab7196a38b783b1ff9934ee22999cceb2b [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>
Simon Glass767e7372014-09-14 16:36:17 -060012#include <dm.h>
13#include <errno.h>
Rajeshwari Shindebd19fa92013-06-24 16:47:22 +053014#include <fdtdec.h>
Mike Frysingerf96c0422011-04-29 18:03:29 +000015#include <linux/compiler.h>
Minkyu Kangfca30842009-10-01 17:20:28 +090016#include <asm/io.h>
Minkyu Kangfca30842009-10-01 17:20:28 +090017#include <asm/arch/clk.h>
Simon Glass405fd142015-07-02 18:15:53 -060018#include <asm/arch/uart.h>
Minkyu Kangfca30842009-10-01 17:20:28 +090019#include <serial.h>
Thomas Abrahamc81fdbe2016-04-23 22:18:11 +053020#include <clk.h>
Minkyu Kangfca30842009-10-01 17:20:28 +090021
John Rigby0d21ed02010-12-20 18:27:51 -070022DECLARE_GLOBAL_DATA_PTR;
23
Simon Glass767e7372014-09-14 16:36:17 -060024#define RX_FIFO_COUNT_SHIFT 0
25#define RX_FIFO_COUNT_MASK (0xff << RX_FIFO_COUNT_SHIFT)
26#define RX_FIFO_FULL (1 << 8)
27#define TX_FIFO_COUNT_SHIFT 16
28#define TX_FIFO_COUNT_MASK (0xff << TX_FIFO_COUNT_SHIFT)
29#define TX_FIFO_FULL (1 << 24)
Akshay Saraswat63f10902013-03-21 20:33:04 +000030
Rajeshwari Shindebd19fa92013-06-24 16:47:22 +053031/* Information about a serial port */
Simon Glass767e7372014-09-14 16:36:17 -060032struct s5p_serial_platdata {
33 struct s5p_uart *reg; /* address of registers in physical memory */
Rajeshwari Shindebd19fa92013-06-24 16:47:22 +053034 u8 port_id; /* uart port number */
Simon Glass767e7372014-09-14 16:36:17 -060035};
Minkyu Kangfca30842009-10-01 17:20:28 +090036
37/*
Minkyu Kangbaa36882010-03-24 16:59:30 +090038 * The coefficient, used to calculate the baudrate on S5P UARTs is
Minkyu Kangfca30842009-10-01 17:20:28 +090039 * calculated as
40 * C = UBRDIV * 16 + number_of_set_bits_in_UDIVSLOT
41 * however, section 31.6.11 of the datasheet doesn't recomment using 1 for 1,
42 * 3 for 2, ... (2^n - 1) for n, instead, they suggest using these constants:
43 */
44static const int udivslot[] = {
45 0,
46 0x0080,
47 0x0808,
48 0x0888,
49 0x2222,
50 0x4924,
51 0x4a52,
52 0x54aa,
53 0x5555,
54 0xd555,
55 0xd5d5,
56 0xddd5,
57 0xdddd,
58 0xdfdd,
59 0xdfdf,
60 0xffdf,
61};
62
Simon Glass405fd142015-07-02 18:15:53 -060063static void __maybe_unused s5p_serial_init(struct s5p_uart *uart)
Minkyu Kangfca30842009-10-01 17:20:28 +090064{
Simon Glass405fd142015-07-02 18:15:53 -060065 /* enable FIFOs, auto clear Rx FIFO */
66 writel(0x3, &uart->ufcon);
67 writel(0, &uart->umcon);
68 /* 8N1 */
69 writel(0x3, &uart->ulcon);
70 /* No interrupts, no DMA, pure polling */
71 writel(0x245, &uart->ucon);
72}
73
74static void __maybe_unused s5p_serial_baud(struct s5p_uart *uart, uint uclk,
75 int baudrate)
76{
Minkyu Kangfca30842009-10-01 17:20:28 +090077 u32 val;
78
Minkyu Kang36f25cf2010-08-24 15:51:55 +090079 val = uclk / baudrate;
Minkyu Kangfca30842009-10-01 17:20:28 +090080
81 writel(val / 16 - 1, &uart->ubrdiv);
Minkyu Kangbfa14242010-09-28 14:35:02 +090082
Minkyu Kangafae8aa2011-01-24 14:43:25 +090083 if (s5p_uart_divslot())
Minkyu Kangbfa14242010-09-28 14:35:02 +090084 writew(udivslot[val % 16], &uart->rest.slot);
85 else
86 writeb(val % 16, &uart->rest.value);
Simon Glass405fd142015-07-02 18:15:53 -060087}
88
Simon Glass8ed4bc12015-07-02 18:15:55 -060089#ifndef CONFIG_SPL_BUILD
Simon Glass405fd142015-07-02 18:15:53 -060090int s5p_serial_setbrg(struct udevice *dev, int baudrate)
91{
92 struct s5p_serial_platdata *plat = dev->platdata;
93 struct s5p_uart *const uart = plat->reg;
Thomas Abrahamc81fdbe2016-04-23 22:18:11 +053094 u32 uclk;
95
96#ifdef CONFIG_CLK_EXYNOS
97 struct udevice *clk_dev;
98 u32 ret;
99
100 ret = clk_get_by_index(dev, 1, &clk_dev);
101 if (ret < 0)
102 return ret;
103 uclk = clk_get_periph_rate(clk_dev, ret);
104#else
105 uclk = get_uart_clk(plat->port_id);
106#endif
Simon Glass405fd142015-07-02 18:15:53 -0600107
108 s5p_serial_baud(uart, uclk, baudrate);
Simon Glass767e7372014-09-14 16:36:17 -0600109
110 return 0;
Minkyu Kangfca30842009-10-01 17:20:28 +0900111}
112
Simon Glass767e7372014-09-14 16:36:17 -0600113static int s5p_serial_probe(struct udevice *dev)
Minkyu Kangfca30842009-10-01 17:20:28 +0900114{
Simon Glass767e7372014-09-14 16:36:17 -0600115 struct s5p_serial_platdata *plat = dev->platdata;
116 struct s5p_uart *const uart = plat->reg;
Minkyu Kangfca30842009-10-01 17:20:28 +0900117
Simon Glass405fd142015-07-02 18:15:53 -0600118 s5p_serial_init(uart);
Minkyu Kangfca30842009-10-01 17:20:28 +0900119
Minkyu Kangfca30842009-10-01 17:20:28 +0900120 return 0;
121}
122
Simon Glass767e7372014-09-14 16:36:17 -0600123static int serial_err_check(const struct s5p_uart *const uart, int op)
Minkyu Kangfca30842009-10-01 17:20:28 +0900124{
Minkyu Kang9455aab2009-11-10 20:23:50 +0900125 unsigned int mask;
Minkyu Kangfca30842009-10-01 17:20:28 +0900126
Minkyu Kang9455aab2009-11-10 20:23:50 +0900127 /*
128 * UERSTAT
129 * Break Detect [3]
130 * Frame Err [2] : receive operation
131 * Parity Err [1] : receive operation
132 * Overrun Err [0] : receive operation
133 */
134 if (op)
135 mask = 0x8;
136 else
137 mask = 0xf;
Minkyu Kangfca30842009-10-01 17:20:28 +0900138
Minkyu Kang9455aab2009-11-10 20:23:50 +0900139 return readl(&uart->uerstat) & mask;
Minkyu Kangfca30842009-10-01 17:20:28 +0900140}
141
Simon Glass767e7372014-09-14 16:36:17 -0600142static int s5p_serial_getc(struct udevice *dev)
Minkyu Kangfca30842009-10-01 17:20:28 +0900143{
Simon Glass767e7372014-09-14 16:36:17 -0600144 struct s5p_serial_platdata *plat = dev->platdata;
145 struct s5p_uart *const uart = plat->reg;
Minkyu Kangfca30842009-10-01 17:20:28 +0900146
Simon Glass767e7372014-09-14 16:36:17 -0600147 if (!(readl(&uart->ufstat) & RX_FIFO_COUNT_MASK))
148 return -EAGAIN;
Rajeshwari Shindebd19fa92013-06-24 16:47:22 +0530149
Simon Glass767e7372014-09-14 16:36:17 -0600150 serial_err_check(uart, 0);
Minkyu Kang76229812010-07-06 20:08:29 +0900151 return (int)(readb(&uart->urxh) & 0xff);
Minkyu Kangfca30842009-10-01 17:20:28 +0900152}
153
Simon Glass767e7372014-09-14 16:36:17 -0600154static int s5p_serial_putc(struct udevice *dev, const char ch)
Minkyu Kangfca30842009-10-01 17:20:28 +0900155{
Simon Glass767e7372014-09-14 16:36:17 -0600156 struct s5p_serial_platdata *plat = dev->platdata;
157 struct s5p_uart *const uart = plat->reg;
Minkyu Kangfca30842009-10-01 17:20:28 +0900158
Simon Glass767e7372014-09-14 16:36:17 -0600159 if (readl(&uart->ufstat) & TX_FIFO_FULL)
160 return -EAGAIN;
Rajeshwari Shindebd19fa92013-06-24 16:47:22 +0530161
Simon Glass767e7372014-09-14 16:36:17 -0600162 writeb(ch, &uart->utxh);
163 serial_err_check(uart, 1);
Minkyu Kangfca30842009-10-01 17:20:28 +0900164
Simon Glass767e7372014-09-14 16:36:17 -0600165 return 0;
Minkyu Kangfca30842009-10-01 17:20:28 +0900166}
167
Simon Glass767e7372014-09-14 16:36:17 -0600168static int s5p_serial_pending(struct udevice *dev, bool input)
Minkyu Kangfca30842009-10-01 17:20:28 +0900169{
Simon Glass767e7372014-09-14 16:36:17 -0600170 struct s5p_serial_platdata *plat = dev->platdata;
171 struct s5p_uart *const uart = plat->reg;
172 uint32_t ufstat = readl(&uart->ufstat);
Minkyu Kangfca30842009-10-01 17:20:28 +0900173
Simon Glass767e7372014-09-14 16:36:17 -0600174 if (input)
175 return (ufstat & RX_FIFO_COUNT_MASK) >> RX_FIFO_COUNT_SHIFT;
176 else
177 return (ufstat & TX_FIFO_COUNT_MASK) >> TX_FIFO_COUNT_SHIFT;
Marek Vasut5bcdf242012-09-09 18:48:28 +0200178}
Minkyu Kangfca30842009-10-01 17:20:28 +0900179
Simon Glass767e7372014-09-14 16:36:17 -0600180static int s5p_serial_ofdata_to_platdata(struct udevice *dev)
Rajeshwari Shindebd19fa92013-06-24 16:47:22 +0530181{
Simon Glass767e7372014-09-14 16:36:17 -0600182 struct s5p_serial_platdata *plat = dev->platdata;
183 fdt_addr_t addr;
Rajeshwari Shindebd19fa92013-06-24 16:47:22 +0530184
Simon Glass09717782015-08-11 08:33:29 -0600185 addr = dev_get_addr(dev);
Simon Glass767e7372014-09-14 16:36:17 -0600186 if (addr == FDT_ADDR_T_NONE)
187 return -EINVAL;
Rajeshwari Shindebd19fa92013-06-24 16:47:22 +0530188
Simon Glass767e7372014-09-14 16:36:17 -0600189 plat->reg = (struct s5p_uart *)addr;
Thomas Abraham214a3952016-04-23 22:18:10 +0530190 plat->port_id = fdtdec_get_int(gd->fdt_blob, dev->of_offset,
191 "id", dev->seq);
Rajeshwari Shindebd19fa92013-06-24 16:47:22 +0530192 return 0;
193}
Rajeshwari Shindebd19fa92013-06-24 16:47:22 +0530194
Simon Glass767e7372014-09-14 16:36:17 -0600195static const struct dm_serial_ops s5p_serial_ops = {
196 .putc = s5p_serial_putc,
197 .pending = s5p_serial_pending,
198 .getc = s5p_serial_getc,
199 .setbrg = s5p_serial_setbrg,
200};
Rajeshwari Shindebd19fa92013-06-24 16:47:22 +0530201
Simon Glass767e7372014-09-14 16:36:17 -0600202static const struct udevice_id s5p_serial_ids[] = {
203 { .compatible = "samsung,exynos4210-uart" },
204 { }
205};
Marek Vasut533e31e2012-09-12 19:39:57 +0200206
Simon Glass767e7372014-09-14 16:36:17 -0600207U_BOOT_DRIVER(serial_s5p) = {
208 .name = "serial_s5p",
209 .id = UCLASS_SERIAL,
210 .of_match = s5p_serial_ids,
211 .ofdata_to_platdata = s5p_serial_ofdata_to_platdata,
212 .platdata_auto_alloc_size = sizeof(struct s5p_serial_platdata),
213 .probe = s5p_serial_probe,
214 .ops = &s5p_serial_ops,
215 .flags = DM_FLAG_PRE_RELOC,
216};
Simon Glass8ed4bc12015-07-02 18:15:55 -0600217#endif
Simon Glass74afb292015-07-02 18:15:54 -0600218
219#ifdef CONFIG_DEBUG_UART_S5P
220
221#include <debug_uart.h>
222
Simon Glass60517d72015-10-18 19:51:23 -0600223static inline void _debug_uart_init(void)
Simon Glass74afb292015-07-02 18:15:54 -0600224{
225 struct s5p_uart *uart = (struct s5p_uart *)CONFIG_DEBUG_UART_BASE;
226
227 s5p_serial_init(uart);
228 s5p_serial_baud(uart, CONFIG_DEBUG_UART_CLOCK, CONFIG_BAUDRATE);
229}
230
231static inline void _debug_uart_putc(int ch)
232{
233 struct s5p_uart *uart = (struct s5p_uart *)CONFIG_DEBUG_UART_BASE;
234
235 while (readl(&uart->ufstat) & TX_FIFO_FULL);
236
237 writeb(ch, &uart->utxh);
238}
239
240DEBUG_UART_FUNCS
241
242#endif