2 * (C) Copyright 2009 SAMSUNG Electronics
3 * Minkyu Kang <mk7.kang@samsung.com>
4 * Heungjun Kim <riverful.kim@samsung.com>
6 * based on drivers/serial/s3c64xx.c
8 * SPDX-License-Identifier: GPL-2.0+
15 #include <linux/compiler.h>
17 #include <asm/arch/uart.h>
18 #include <asm/arch/clk.h>
21 DECLARE_GLOBAL_DATA_PTR;
23 #define RX_FIFO_COUNT_SHIFT 0
24 #define RX_FIFO_COUNT_MASK (0xff << RX_FIFO_COUNT_SHIFT)
25 #define RX_FIFO_FULL (1 << 8)
26 #define TX_FIFO_COUNT_SHIFT 16
27 #define TX_FIFO_COUNT_MASK (0xff << TX_FIFO_COUNT_SHIFT)
28 #define TX_FIFO_FULL (1 << 24)
30 /* Information about a serial port */
31 struct s5p_serial_platdata {
32 struct s5p_uart *reg; /* address of registers in physical memory */
33 u8 port_id; /* uart port number */
37 * The coefficient, used to calculate the baudrate on S5P UARTs is
39 * C = UBRDIV * 16 + number_of_set_bits_in_UDIVSLOT
40 * however, section 31.6.11 of the datasheet doesn't recomment using 1 for 1,
41 * 3 for 2, ... (2^n - 1) for n, instead, they suggest using these constants:
43 static const int udivslot[] = {
62 int s5p_serial_setbrg(struct udevice *dev, int baudrate)
64 struct s5p_serial_platdata *plat = dev->platdata;
65 struct s5p_uart *const uart = plat->reg;
66 u32 uclk = get_uart_clk(plat->port_id);
69 val = uclk / baudrate;
71 writel(val / 16 - 1, &uart->ubrdiv);
73 if (s5p_uart_divslot())
74 writew(udivslot[val % 16], &uart->rest.slot);
76 writeb(val % 16, &uart->rest.value);
81 static int s5p_serial_probe(struct udevice *dev)
83 struct s5p_serial_platdata *plat = dev->platdata;
84 struct s5p_uart *const uart = plat->reg;
86 /* enable FIFOs, auto clear Rx FIFO */
87 writel(0x3, &uart->ufcon);
88 writel(0, &uart->umcon);
90 writel(0x3, &uart->ulcon);
91 /* No interrupts, no DMA, pure polling */
92 writel(0x245, &uart->ucon);
97 static int serial_err_check(const struct s5p_uart *const uart, int op)
104 * Frame Err [2] : receive operation
105 * Parity Err [1] : receive operation
106 * Overrun Err [0] : receive operation
113 return readl(&uart->uerstat) & mask;
116 static int s5p_serial_getc(struct udevice *dev)
118 struct s5p_serial_platdata *plat = dev->platdata;
119 struct s5p_uart *const uart = plat->reg;
121 if (!(readl(&uart->ufstat) & RX_FIFO_COUNT_MASK))
124 serial_err_check(uart, 0);
125 return (int)(readb(&uart->urxh) & 0xff);
128 static int s5p_serial_putc(struct udevice *dev, const char ch)
130 struct s5p_serial_platdata *plat = dev->platdata;
131 struct s5p_uart *const uart = plat->reg;
133 if (readl(&uart->ufstat) & TX_FIFO_FULL)
136 writeb(ch, &uart->utxh);
137 serial_err_check(uart, 1);
142 static int s5p_serial_pending(struct udevice *dev, bool input)
144 struct s5p_serial_platdata *plat = dev->platdata;
145 struct s5p_uart *const uart = plat->reg;
146 uint32_t ufstat = readl(&uart->ufstat);
149 return (ufstat & RX_FIFO_COUNT_MASK) >> RX_FIFO_COUNT_SHIFT;
151 return (ufstat & TX_FIFO_COUNT_MASK) >> TX_FIFO_COUNT_SHIFT;
154 static int s5p_serial_ofdata_to_platdata(struct udevice *dev)
156 struct s5p_serial_platdata *plat = dev->platdata;
159 addr = fdtdec_get_addr(gd->fdt_blob, dev->of_offset, "reg");
160 if (addr == FDT_ADDR_T_NONE)
163 plat->reg = (struct s5p_uart *)addr;
164 plat->port_id = fdtdec_get_int(gd->fdt_blob, dev->of_offset, "id", -1);
169 static const struct dm_serial_ops s5p_serial_ops = {
170 .putc = s5p_serial_putc,
171 .pending = s5p_serial_pending,
172 .getc = s5p_serial_getc,
173 .setbrg = s5p_serial_setbrg,
176 static const struct udevice_id s5p_serial_ids[] = {
177 { .compatible = "samsung,exynos4210-uart" },
181 U_BOOT_DRIVER(serial_s5p) = {
182 .name = "serial_s5p",
184 .of_match = s5p_serial_ids,
185 .ofdata_to_platdata = s5p_serial_ofdata_to_platdata,
186 .platdata_auto_alloc_size = sizeof(struct s5p_serial_platdata),
187 .probe = s5p_serial_probe,
188 .ops = &s5p_serial_ops,
189 .flags = DM_FLAG_PRE_RELOC,