1 // SPDX-License-Identifier: GPL-2.0+
3 * (C) Copyright 2009 SAMSUNG Electronics
4 * Minkyu Kang <mk7.kang@samsung.com>
5 * Heungjun Kim <riverful.kim@samsung.com>
7 * based on drivers/serial/s3c64xx.c
14 #include <asm/global_data.h>
15 #include <linux/compiler.h>
17 #if !CONFIG_IS_ENABLED(ARCH_APPLE)
18 #include <asm/arch/clk.h>
20 #include <asm/arch/uart.h>
24 DECLARE_GLOBAL_DATA_PTR;
31 #define S5L_RX_FIFO_COUNT_SHIFT 0
32 #define S5L_RX_FIFO_COUNT_MASK (0xf << S5L_RX_FIFO_COUNT_SHIFT)
33 #define S5L_RX_FIFO_FULL (1 << 8)
34 #define S5L_TX_FIFO_COUNT_SHIFT 4
35 #define S5L_TX_FIFO_COUNT_MASK (0xf << S5L_TX_FIFO_COUNT_SHIFT)
36 #define S5L_TX_FIFO_FULL (1 << 9)
38 #define S5P_RX_FIFO_COUNT_SHIFT 0
39 #define S5P_RX_FIFO_COUNT_MASK (0xff << S5P_RX_FIFO_COUNT_SHIFT)
40 #define S5P_RX_FIFO_FULL (1 << 8)
41 #define S5P_TX_FIFO_COUNT_SHIFT 16
42 #define S5P_TX_FIFO_COUNT_MASK (0xff << S5P_TX_FIFO_COUNT_SHIFT)
43 #define S5P_TX_FIFO_FULL (1 << 24)
45 /* Information about a serial port */
46 struct s5p_serial_plat {
47 struct s5p_uart *reg; /* address of registers in physical memory */
48 u8 reg_width; /* register width */
49 u8 port_id; /* uart port number */
50 u8 rx_fifo_count_shift;
51 u8 tx_fifo_count_shift;
52 u32 rx_fifo_count_mask;
53 u32 tx_fifo_count_mask;
59 * The coefficient, used to calculate the baudrate on S5P UARTs is
61 * C = UBRDIV * 16 + number_of_set_bits_in_UDIVSLOT
62 * however, section 31.6.11 of the datasheet doesn't recomment using 1 for 1,
63 * 3 for 2, ... (2^n - 1) for n, instead, they suggest using these constants:
65 static const int udivslot[] = {
84 static void __maybe_unused s5p_serial_init(struct s5p_uart *uart)
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);
95 static void __maybe_unused s5p_serial_baud(struct s5p_uart *uart, u8 reg_width,
96 uint uclk, int baudrate)
100 val = uclk / baudrate;
102 writel(val / 16 - 1, &uart->ubrdiv);
104 if (s5p_uart_divslot())
105 writew(udivslot[val % 16], &uart->rest.slot);
106 else if (reg_width == 4)
107 writel(val % 16, &uart->rest.value);
109 writeb(val % 16, &uart->rest.value);
112 #ifndef CONFIG_SPL_BUILD
113 int s5p_serial_setbrg(struct udevice *dev, int baudrate)
115 struct s5p_serial_plat *plat = dev_get_plat(dev);
116 struct s5p_uart *const uart = plat->reg;
119 #if CONFIG_IS_ENABLED(CLK_EXYNOS) || CONFIG_IS_ENABLED(ARCH_APPLE)
123 ret = clk_get_by_index(dev, 1, &clk);
126 uclk = clk_get_rate(&clk);
128 uclk = get_uart_clk(plat->port_id);
131 s5p_serial_baud(uart, plat->reg_width, uclk, baudrate);
136 static int s5p_serial_probe(struct udevice *dev)
138 struct s5p_serial_plat *plat = dev_get_plat(dev);
139 struct s5p_uart *const uart = plat->reg;
141 s5p_serial_init(uart);
146 static int serial_err_check(const struct s5p_uart *const uart, int op)
153 * Frame Err [2] : receive operation
154 * Parity Err [1] : receive operation
155 * Overrun Err [0] : receive operation
162 return readl(&uart->uerstat) & mask;
165 static int s5p_serial_getc(struct udevice *dev)
167 struct s5p_serial_plat *plat = dev_get_plat(dev);
168 struct s5p_uart *const uart = plat->reg;
170 if (!(readl(&uart->ufstat) & plat->rx_fifo_count_mask))
173 serial_err_check(uart, 0);
174 if (plat->reg_width == 4)
175 return (int)(readl(&uart->urxh) & 0xff);
177 return (int)(readb(&uart->urxh) & 0xff);
180 static int s5p_serial_putc(struct udevice *dev, const char ch)
182 struct s5p_serial_plat *plat = dev_get_plat(dev);
183 struct s5p_uart *const uart = plat->reg;
185 if (readl(&uart->ufstat) & plat->tx_fifo_full)
188 if (plat->reg_width == 4)
189 writel(ch, &uart->utxh);
191 writeb(ch, &uart->utxh);
192 serial_err_check(uart, 1);
197 static int s5p_serial_pending(struct udevice *dev, bool input)
199 struct s5p_serial_plat *plat = dev_get_plat(dev);
200 struct s5p_uart *const uart = plat->reg;
201 uint32_t ufstat = readl(&uart->ufstat);
204 return (ufstat & plat->rx_fifo_count_mask) >>
205 plat->rx_fifo_count_shift;
207 return (ufstat & plat->tx_fifo_count_mask) >>
208 plat->tx_fifo_count_shift;
212 static int s5p_serial_of_to_plat(struct udevice *dev)
214 struct s5p_serial_plat *plat = dev_get_plat(dev);
215 const ulong port_type = dev_get_driver_data(dev);
218 addr = dev_read_addr(dev);
219 if (addr == FDT_ADDR_T_NONE)
222 plat->reg = (struct s5p_uart *)addr;
223 plat->reg_width = dev_read_u32_default(dev, "reg-io-width", 1);
224 plat->port_id = fdtdec_get_int(gd->fdt_blob, dev_of_offset(dev),
227 if (port_type == PORT_S5L) {
228 plat->rx_fifo_count_shift = S5L_RX_FIFO_COUNT_SHIFT;
229 plat->rx_fifo_count_mask = S5L_RX_FIFO_COUNT_MASK;
230 plat->rx_fifo_full = S5L_RX_FIFO_FULL;
231 plat->tx_fifo_count_shift = S5L_TX_FIFO_COUNT_SHIFT;
232 plat->tx_fifo_count_mask = S5L_TX_FIFO_COUNT_MASK;
233 plat->tx_fifo_full = S5L_TX_FIFO_FULL;
235 plat->rx_fifo_count_shift = S5P_RX_FIFO_COUNT_SHIFT;
236 plat->rx_fifo_count_mask = S5P_RX_FIFO_COUNT_MASK;
237 plat->rx_fifo_full = S5P_RX_FIFO_FULL;
238 plat->tx_fifo_count_shift = S5P_TX_FIFO_COUNT_SHIFT;
239 plat->tx_fifo_count_mask = S5P_TX_FIFO_COUNT_MASK;
240 plat->tx_fifo_full = S5P_TX_FIFO_FULL;
246 static const struct dm_serial_ops s5p_serial_ops = {
247 .putc = s5p_serial_putc,
248 .pending = s5p_serial_pending,
249 .getc = s5p_serial_getc,
250 .setbrg = s5p_serial_setbrg,
253 static const struct udevice_id s5p_serial_ids[] = {
254 { .compatible = "samsung,exynos4210-uart", .data = PORT_S5P },
255 { .compatible = "apple,s5l-uart", .data = PORT_S5L },
259 U_BOOT_DRIVER(serial_s5p) = {
260 .name = "serial_s5p",
262 .of_match = s5p_serial_ids,
263 .of_to_plat = s5p_serial_of_to_plat,
264 .plat_auto = sizeof(struct s5p_serial_plat),
265 .probe = s5p_serial_probe,
266 .ops = &s5p_serial_ops,
270 #ifdef CONFIG_DEBUG_UART_S5P
272 #include <debug_uart.h>
274 static inline void _debug_uart_init(void)
276 if (IS_ENABLED(CONFIG_DEBUG_UART_SKIP_INIT))
279 struct s5p_uart *uart = (struct s5p_uart *)CONFIG_DEBUG_UART_BASE;
281 s5p_serial_init(uart);
282 #if CONFIG_IS_ENABLED(ARCH_APPLE)
283 s5p_serial_baud(uart, 4, CONFIG_DEBUG_UART_CLOCK, CONFIG_BAUDRATE);
285 s5p_serial_baud(uart, 1, CONFIG_DEBUG_UART_CLOCK, CONFIG_BAUDRATE);
289 static inline void _debug_uart_putc(int ch)
291 struct s5p_uart *uart = (struct s5p_uart *)CONFIG_DEBUG_UART_BASE;
293 #if CONFIG_IS_ENABLED(ARCH_APPLE)
294 while (readl(&uart->ufstat) & S5L_TX_FIFO_FULL);
295 writel(ch, &uart->utxh);
297 while (readl(&uart->ufstat) & S5P_TX_FIFO_FULL);
298 writeb(ch, &uart->utxh);