Tom Rini | 83d290c | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Minkyu Kang | dd2c9e6 | 2009-10-01 17:20:28 +0900 | [diff] [blame] | 2 | /* |
| 3 | * (C) Copyright 2009 SAMSUNG Electronics |
| 4 | * Minkyu Kang <mk7.kang@samsung.com> |
| 5 | * Heungjun Kim <riverful.kim@samsung.com> |
| 6 | * |
| 7 | * based on drivers/serial/s3c64xx.c |
Minkyu Kang | dd2c9e6 | 2009-10-01 17:20:28 +0900 | [diff] [blame] | 8 | */ |
| 9 | |
| 10 | #include <common.h> |
Simon Glass | 73e256c | 2014-09-14 16:36:17 -0600 | [diff] [blame] | 11 | #include <dm.h> |
| 12 | #include <errno.h> |
Rajeshwari Shinde | d4ec8f0 | 2013-06-24 16:47:22 +0530 | [diff] [blame] | 13 | #include <fdtdec.h> |
Mike Frysinger | 6c768ca | 2011-04-29 18:03:29 +0000 | [diff] [blame] | 14 | #include <linux/compiler.h> |
Minkyu Kang | dd2c9e6 | 2009-10-01 17:20:28 +0900 | [diff] [blame] | 15 | #include <asm/io.h> |
Minkyu Kang | dd2c9e6 | 2009-10-01 17:20:28 +0900 | [diff] [blame] | 16 | #include <asm/arch/clk.h> |
Simon Glass | 89ca935 | 2015-07-02 18:15:53 -0600 | [diff] [blame] | 17 | #include <asm/arch/uart.h> |
Minkyu Kang | dd2c9e6 | 2009-10-01 17:20:28 +0900 | [diff] [blame] | 18 | #include <serial.h> |
Thomas Abraham | cf75cdf | 2016-04-23 22:18:11 +0530 | [diff] [blame] | 19 | #include <clk.h> |
Minkyu Kang | dd2c9e6 | 2009-10-01 17:20:28 +0900 | [diff] [blame] | 20 | |
John Rigby | 2956532 | 2010-12-20 18:27:51 -0700 | [diff] [blame] | 21 | DECLARE_GLOBAL_DATA_PTR; |
| 22 | |
Simon Glass | 73e256c | 2014-09-14 16:36:17 -0600 | [diff] [blame] | 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) |
Akshay Saraswat | ffbff1d | 2013-03-21 20:33:04 +0000 | [diff] [blame] | 29 | |
Rajeshwari Shinde | d4ec8f0 | 2013-06-24 16:47:22 +0530 | [diff] [blame] | 30 | /* Information about a serial port */ |
Simon Glass | 8a8d24b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 31 | struct s5p_serial_plat { |
Simon Glass | 73e256c | 2014-09-14 16:36:17 -0600 | [diff] [blame] | 32 | struct s5p_uart *reg; /* address of registers in physical memory */ |
Rajeshwari Shinde | d4ec8f0 | 2013-06-24 16:47:22 +0530 | [diff] [blame] | 33 | u8 port_id; /* uart port number */ |
Simon Glass | 73e256c | 2014-09-14 16:36:17 -0600 | [diff] [blame] | 34 | }; |
Minkyu Kang | dd2c9e6 | 2009-10-01 17:20:28 +0900 | [diff] [blame] | 35 | |
| 36 | /* |
Minkyu Kang | 46a3b5c | 2010-03-24 16:59:30 +0900 | [diff] [blame] | 37 | * The coefficient, used to calculate the baudrate on S5P UARTs is |
Minkyu Kang | dd2c9e6 | 2009-10-01 17:20:28 +0900 | [diff] [blame] | 38 | * calculated as |
| 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: |
| 42 | */ |
| 43 | static const int udivslot[] = { |
| 44 | 0, |
| 45 | 0x0080, |
| 46 | 0x0808, |
| 47 | 0x0888, |
| 48 | 0x2222, |
| 49 | 0x4924, |
| 50 | 0x4a52, |
| 51 | 0x54aa, |
| 52 | 0x5555, |
| 53 | 0xd555, |
| 54 | 0xd5d5, |
| 55 | 0xddd5, |
| 56 | 0xdddd, |
| 57 | 0xdfdd, |
| 58 | 0xdfdf, |
| 59 | 0xffdf, |
| 60 | }; |
| 61 | |
Simon Glass | 89ca935 | 2015-07-02 18:15:53 -0600 | [diff] [blame] | 62 | static void __maybe_unused s5p_serial_init(struct s5p_uart *uart) |
Minkyu Kang | dd2c9e6 | 2009-10-01 17:20:28 +0900 | [diff] [blame] | 63 | { |
Simon Glass | 89ca935 | 2015-07-02 18:15:53 -0600 | [diff] [blame] | 64 | /* enable FIFOs, auto clear Rx FIFO */ |
| 65 | writel(0x3, &uart->ufcon); |
| 66 | writel(0, &uart->umcon); |
| 67 | /* 8N1 */ |
| 68 | writel(0x3, &uart->ulcon); |
| 69 | /* No interrupts, no DMA, pure polling */ |
| 70 | writel(0x245, &uart->ucon); |
| 71 | } |
| 72 | |
| 73 | static void __maybe_unused s5p_serial_baud(struct s5p_uart *uart, uint uclk, |
| 74 | int baudrate) |
| 75 | { |
Minkyu Kang | dd2c9e6 | 2009-10-01 17:20:28 +0900 | [diff] [blame] | 76 | u32 val; |
| 77 | |
Minkyu Kang | f70409a | 2010-08-24 15:51:55 +0900 | [diff] [blame] | 78 | val = uclk / baudrate; |
Minkyu Kang | dd2c9e6 | 2009-10-01 17:20:28 +0900 | [diff] [blame] | 79 | |
| 80 | writel(val / 16 - 1, &uart->ubrdiv); |
Minkyu Kang | 1628cfc | 2010-09-28 14:35:02 +0900 | [diff] [blame] | 81 | |
Minkyu Kang | e0617c6 | 2011-01-24 14:43:25 +0900 | [diff] [blame] | 82 | if (s5p_uart_divslot()) |
Minkyu Kang | 1628cfc | 2010-09-28 14:35:02 +0900 | [diff] [blame] | 83 | writew(udivslot[val % 16], &uart->rest.slot); |
| 84 | else |
| 85 | writeb(val % 16, &uart->rest.value); |
Simon Glass | 89ca935 | 2015-07-02 18:15:53 -0600 | [diff] [blame] | 86 | } |
| 87 | |
Simon Glass | 7fb5739 | 2015-07-02 18:15:55 -0600 | [diff] [blame] | 88 | #ifndef CONFIG_SPL_BUILD |
Simon Glass | 89ca935 | 2015-07-02 18:15:53 -0600 | [diff] [blame] | 89 | int s5p_serial_setbrg(struct udevice *dev, int baudrate) |
| 90 | { |
Simon Glass | 0fd3d91 | 2020-12-22 19:30:28 -0700 | [diff] [blame] | 91 | struct s5p_serial_plat *plat = dev_get_plat(dev); |
Simon Glass | 89ca935 | 2015-07-02 18:15:53 -0600 | [diff] [blame] | 92 | struct s5p_uart *const uart = plat->reg; |
Thomas Abraham | cf75cdf | 2016-04-23 22:18:11 +0530 | [diff] [blame] | 93 | u32 uclk; |
| 94 | |
| 95 | #ifdef CONFIG_CLK_EXYNOS |
Stephen Warren | 135aa95 | 2016-06-17 09:44:00 -0600 | [diff] [blame] | 96 | struct clk clk; |
Thomas Abraham | cf75cdf | 2016-04-23 22:18:11 +0530 | [diff] [blame] | 97 | u32 ret; |
| 98 | |
Stephen Warren | 135aa95 | 2016-06-17 09:44:00 -0600 | [diff] [blame] | 99 | ret = clk_get_by_index(dev, 1, &clk); |
Thomas Abraham | cf75cdf | 2016-04-23 22:18:11 +0530 | [diff] [blame] | 100 | if (ret < 0) |
| 101 | return ret; |
Stephen Warren | 135aa95 | 2016-06-17 09:44:00 -0600 | [diff] [blame] | 102 | uclk = clk_get_rate(&clk); |
Thomas Abraham | cf75cdf | 2016-04-23 22:18:11 +0530 | [diff] [blame] | 103 | #else |
| 104 | uclk = get_uart_clk(plat->port_id); |
| 105 | #endif |
Simon Glass | 89ca935 | 2015-07-02 18:15:53 -0600 | [diff] [blame] | 106 | |
| 107 | s5p_serial_baud(uart, uclk, baudrate); |
Simon Glass | 73e256c | 2014-09-14 16:36:17 -0600 | [diff] [blame] | 108 | |
| 109 | return 0; |
Minkyu Kang | dd2c9e6 | 2009-10-01 17:20:28 +0900 | [diff] [blame] | 110 | } |
| 111 | |
Simon Glass | 73e256c | 2014-09-14 16:36:17 -0600 | [diff] [blame] | 112 | static int s5p_serial_probe(struct udevice *dev) |
Minkyu Kang | dd2c9e6 | 2009-10-01 17:20:28 +0900 | [diff] [blame] | 113 | { |
Simon Glass | 0fd3d91 | 2020-12-22 19:30:28 -0700 | [diff] [blame] | 114 | struct s5p_serial_plat *plat = dev_get_plat(dev); |
Simon Glass | 73e256c | 2014-09-14 16:36:17 -0600 | [diff] [blame] | 115 | struct s5p_uart *const uart = plat->reg; |
Minkyu Kang | dd2c9e6 | 2009-10-01 17:20:28 +0900 | [diff] [blame] | 116 | |
Simon Glass | 89ca935 | 2015-07-02 18:15:53 -0600 | [diff] [blame] | 117 | s5p_serial_init(uart); |
Minkyu Kang | dd2c9e6 | 2009-10-01 17:20:28 +0900 | [diff] [blame] | 118 | |
Minkyu Kang | dd2c9e6 | 2009-10-01 17:20:28 +0900 | [diff] [blame] | 119 | return 0; |
| 120 | } |
| 121 | |
Simon Glass | 73e256c | 2014-09-14 16:36:17 -0600 | [diff] [blame] | 122 | static int serial_err_check(const struct s5p_uart *const uart, int op) |
Minkyu Kang | dd2c9e6 | 2009-10-01 17:20:28 +0900 | [diff] [blame] | 123 | { |
Minkyu Kang | 9400322 | 2009-11-10 20:23:50 +0900 | [diff] [blame] | 124 | unsigned int mask; |
Minkyu Kang | dd2c9e6 | 2009-10-01 17:20:28 +0900 | [diff] [blame] | 125 | |
Minkyu Kang | 9400322 | 2009-11-10 20:23:50 +0900 | [diff] [blame] | 126 | /* |
| 127 | * UERSTAT |
| 128 | * Break Detect [3] |
| 129 | * Frame Err [2] : receive operation |
| 130 | * Parity Err [1] : receive operation |
| 131 | * Overrun Err [0] : receive operation |
| 132 | */ |
| 133 | if (op) |
| 134 | mask = 0x8; |
| 135 | else |
| 136 | mask = 0xf; |
Minkyu Kang | dd2c9e6 | 2009-10-01 17:20:28 +0900 | [diff] [blame] | 137 | |
Minkyu Kang | 9400322 | 2009-11-10 20:23:50 +0900 | [diff] [blame] | 138 | return readl(&uart->uerstat) & mask; |
Minkyu Kang | dd2c9e6 | 2009-10-01 17:20:28 +0900 | [diff] [blame] | 139 | } |
| 140 | |
Simon Glass | 73e256c | 2014-09-14 16:36:17 -0600 | [diff] [blame] | 141 | static int s5p_serial_getc(struct udevice *dev) |
Minkyu Kang | dd2c9e6 | 2009-10-01 17:20:28 +0900 | [diff] [blame] | 142 | { |
Simon Glass | 0fd3d91 | 2020-12-22 19:30:28 -0700 | [diff] [blame] | 143 | struct s5p_serial_plat *plat = dev_get_plat(dev); |
Simon Glass | 73e256c | 2014-09-14 16:36:17 -0600 | [diff] [blame] | 144 | struct s5p_uart *const uart = plat->reg; |
Minkyu Kang | dd2c9e6 | 2009-10-01 17:20:28 +0900 | [diff] [blame] | 145 | |
Simon Glass | 73e256c | 2014-09-14 16:36:17 -0600 | [diff] [blame] | 146 | if (!(readl(&uart->ufstat) & RX_FIFO_COUNT_MASK)) |
| 147 | return -EAGAIN; |
Rajeshwari Shinde | d4ec8f0 | 2013-06-24 16:47:22 +0530 | [diff] [blame] | 148 | |
Simon Glass | 73e256c | 2014-09-14 16:36:17 -0600 | [diff] [blame] | 149 | serial_err_check(uart, 0); |
Minkyu Kang | 1a4106d | 2010-07-06 20:08:29 +0900 | [diff] [blame] | 150 | return (int)(readb(&uart->urxh) & 0xff); |
Minkyu Kang | dd2c9e6 | 2009-10-01 17:20:28 +0900 | [diff] [blame] | 151 | } |
| 152 | |
Simon Glass | 73e256c | 2014-09-14 16:36:17 -0600 | [diff] [blame] | 153 | static int s5p_serial_putc(struct udevice *dev, const char ch) |
Minkyu Kang | dd2c9e6 | 2009-10-01 17:20:28 +0900 | [diff] [blame] | 154 | { |
Simon Glass | 0fd3d91 | 2020-12-22 19:30:28 -0700 | [diff] [blame] | 155 | struct s5p_serial_plat *plat = dev_get_plat(dev); |
Simon Glass | 73e256c | 2014-09-14 16:36:17 -0600 | [diff] [blame] | 156 | struct s5p_uart *const uart = plat->reg; |
Minkyu Kang | dd2c9e6 | 2009-10-01 17:20:28 +0900 | [diff] [blame] | 157 | |
Simon Glass | 73e256c | 2014-09-14 16:36:17 -0600 | [diff] [blame] | 158 | if (readl(&uart->ufstat) & TX_FIFO_FULL) |
| 159 | return -EAGAIN; |
Rajeshwari Shinde | d4ec8f0 | 2013-06-24 16:47:22 +0530 | [diff] [blame] | 160 | |
Simon Glass | 73e256c | 2014-09-14 16:36:17 -0600 | [diff] [blame] | 161 | writeb(ch, &uart->utxh); |
| 162 | serial_err_check(uart, 1); |
Rajeshwari Shinde | d4ec8f0 | 2013-06-24 16:47:22 +0530 | [diff] [blame] | 163 | |
| 164 | return 0; |
| 165 | } |
Rajeshwari Shinde | d4ec8f0 | 2013-06-24 16:47:22 +0530 | [diff] [blame] | 166 | |
Simon Glass | 73e256c | 2014-09-14 16:36:17 -0600 | [diff] [blame] | 167 | static int s5p_serial_pending(struct udevice *dev, bool input) |
Mike Frysinger | 6c768ca | 2011-04-29 18:03:29 +0000 | [diff] [blame] | 168 | { |
Simon Glass | 0fd3d91 | 2020-12-22 19:30:28 -0700 | [diff] [blame] | 169 | struct s5p_serial_plat *plat = dev_get_plat(dev); |
Simon Glass | 73e256c | 2014-09-14 16:36:17 -0600 | [diff] [blame] | 170 | struct s5p_uart *const uart = plat->reg; |
| 171 | uint32_t ufstat = readl(&uart->ufstat); |
Rajeshwari Shinde | d4ec8f0 | 2013-06-24 16:47:22 +0530 | [diff] [blame] | 172 | |
Simon Glass | 73e256c | 2014-09-14 16:36:17 -0600 | [diff] [blame] | 173 | if (input) |
| 174 | return (ufstat & RX_FIFO_COUNT_MASK) >> RX_FIFO_COUNT_SHIFT; |
| 175 | else |
| 176 | return (ufstat & TX_FIFO_COUNT_MASK) >> TX_FIFO_COUNT_SHIFT; |
Mike Frysinger | 6c768ca | 2011-04-29 18:03:29 +0000 | [diff] [blame] | 177 | } |
Marek Vasut | b498051 | 2012-09-12 19:39:57 +0200 | [diff] [blame] | 178 | |
Simon Glass | d1998a9 | 2020-12-03 16:55:21 -0700 | [diff] [blame] | 179 | static int s5p_serial_of_to_plat(struct udevice *dev) |
Marek Vasut | b498051 | 2012-09-12 19:39:57 +0200 | [diff] [blame] | 180 | { |
Simon Glass | 0fd3d91 | 2020-12-22 19:30:28 -0700 | [diff] [blame] | 181 | struct s5p_serial_plat *plat = dev_get_plat(dev); |
Simon Glass | 73e256c | 2014-09-14 16:36:17 -0600 | [diff] [blame] | 182 | fdt_addr_t addr; |
| 183 | |
Masahiro Yamada | 2548493 | 2020-07-17 14:36:48 +0900 | [diff] [blame] | 184 | addr = dev_read_addr(dev); |
Simon Glass | 73e256c | 2014-09-14 16:36:17 -0600 | [diff] [blame] | 185 | if (addr == FDT_ADDR_T_NONE) |
| 186 | return -EINVAL; |
| 187 | |
| 188 | plat->reg = (struct s5p_uart *)addr; |
Simon Glass | e160f7d | 2017-01-17 16:52:55 -0700 | [diff] [blame] | 189 | plat->port_id = fdtdec_get_int(gd->fdt_blob, dev_of_offset(dev), |
Simon Glass | 8b85dfc | 2020-12-16 21:20:07 -0700 | [diff] [blame] | 190 | "id", dev_seq(dev)); |
Simon Glass | 73e256c | 2014-09-14 16:36:17 -0600 | [diff] [blame] | 191 | return 0; |
Marek Vasut | b498051 | 2012-09-12 19:39:57 +0200 | [diff] [blame] | 192 | } |
Simon Glass | 73e256c | 2014-09-14 16:36:17 -0600 | [diff] [blame] | 193 | |
| 194 | static const struct dm_serial_ops s5p_serial_ops = { |
| 195 | .putc = s5p_serial_putc, |
| 196 | .pending = s5p_serial_pending, |
| 197 | .getc = s5p_serial_getc, |
| 198 | .setbrg = s5p_serial_setbrg, |
| 199 | }; |
| 200 | |
| 201 | static const struct udevice_id s5p_serial_ids[] = { |
| 202 | { .compatible = "samsung,exynos4210-uart" }, |
| 203 | { } |
| 204 | }; |
| 205 | |
| 206 | U_BOOT_DRIVER(serial_s5p) = { |
| 207 | .name = "serial_s5p", |
| 208 | .id = UCLASS_SERIAL, |
| 209 | .of_match = s5p_serial_ids, |
Simon Glass | d1998a9 | 2020-12-03 16:55:21 -0700 | [diff] [blame] | 210 | .of_to_plat = s5p_serial_of_to_plat, |
Simon Glass | 8a8d24b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 211 | .plat_auto = sizeof(struct s5p_serial_plat), |
Simon Glass | 73e256c | 2014-09-14 16:36:17 -0600 | [diff] [blame] | 212 | .probe = s5p_serial_probe, |
| 213 | .ops = &s5p_serial_ops, |
Simon Glass | 73e256c | 2014-09-14 16:36:17 -0600 | [diff] [blame] | 214 | }; |
Simon Glass | 7fb5739 | 2015-07-02 18:15:55 -0600 | [diff] [blame] | 215 | #endif |
Simon Glass | bf6e702 | 2015-07-02 18:15:54 -0600 | [diff] [blame] | 216 | |
| 217 | #ifdef CONFIG_DEBUG_UART_S5P |
| 218 | |
| 219 | #include <debug_uart.h> |
| 220 | |
Simon Glass | 97b0597 | 2015-10-18 19:51:23 -0600 | [diff] [blame] | 221 | static inline void _debug_uart_init(void) |
Simon Glass | bf6e702 | 2015-07-02 18:15:54 -0600 | [diff] [blame] | 222 | { |
| 223 | struct s5p_uart *uart = (struct s5p_uart *)CONFIG_DEBUG_UART_BASE; |
| 224 | |
| 225 | s5p_serial_init(uart); |
| 226 | s5p_serial_baud(uart, CONFIG_DEBUG_UART_CLOCK, CONFIG_BAUDRATE); |
| 227 | } |
| 228 | |
| 229 | static inline void _debug_uart_putc(int ch) |
| 230 | { |
| 231 | struct s5p_uart *uart = (struct s5p_uart *)CONFIG_DEBUG_UART_BASE; |
| 232 | |
| 233 | while (readl(&uart->ufstat) & TX_FIFO_FULL); |
| 234 | |
| 235 | writeb(ch, &uart->utxh); |
| 236 | } |
| 237 | |
| 238 | DEBUG_UART_FUNCS |
| 239 | |
| 240 | #endif |