Minkyu Kang | fca3084 | 2009-10-01 17:20:28 +0900 | [diff] [blame] | 1 | /* |
| 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 Denk | d79de1d | 2013-07-08 09:37:19 +0200 | [diff] [blame] | 8 | * SPDX-License-Identifier: GPL-2.0+ |
Minkyu Kang | fca3084 | 2009-10-01 17:20:28 +0900 | [diff] [blame] | 9 | */ |
| 10 | |
| 11 | #include <common.h> |
Simon Glass | 767e737 | 2014-09-14 16:36:17 -0600 | [diff] [blame^] | 12 | #include <dm.h> |
| 13 | #include <errno.h> |
Rajeshwari Shinde | bd19fa9 | 2013-06-24 16:47:22 +0530 | [diff] [blame] | 14 | #include <fdtdec.h> |
Mike Frysinger | f96c042 | 2011-04-29 18:03:29 +0000 | [diff] [blame] | 15 | #include <linux/compiler.h> |
Minkyu Kang | fca3084 | 2009-10-01 17:20:28 +0900 | [diff] [blame] | 16 | #include <asm/io.h> |
| 17 | #include <asm/arch/uart.h> |
| 18 | #include <asm/arch/clk.h> |
| 19 | #include <serial.h> |
| 20 | |
John Rigby | 0d21ed0 | 2010-12-20 18:27:51 -0700 | [diff] [blame] | 21 | DECLARE_GLOBAL_DATA_PTR; |
| 22 | |
Simon Glass | 767e737 | 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 | 63f1090 | 2013-03-21 20:33:04 +0000 | [diff] [blame] | 29 | |
Rajeshwari Shinde | bd19fa9 | 2013-06-24 16:47:22 +0530 | [diff] [blame] | 30 | /* Information about a serial port */ |
Simon Glass | 767e737 | 2014-09-14 16:36:17 -0600 | [diff] [blame^] | 31 | struct s5p_serial_platdata { |
| 32 | struct s5p_uart *reg; /* address of registers in physical memory */ |
Rajeshwari Shinde | bd19fa9 | 2013-06-24 16:47:22 +0530 | [diff] [blame] | 33 | u8 port_id; /* uart port number */ |
Simon Glass | 767e737 | 2014-09-14 16:36:17 -0600 | [diff] [blame^] | 34 | }; |
Minkyu Kang | fca3084 | 2009-10-01 17:20:28 +0900 | [diff] [blame] | 35 | |
| 36 | /* |
Minkyu Kang | baa3688 | 2010-03-24 16:59:30 +0900 | [diff] [blame] | 37 | * The coefficient, used to calculate the baudrate on S5P UARTs is |
Minkyu Kang | fca3084 | 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 | 767e737 | 2014-09-14 16:36:17 -0600 | [diff] [blame^] | 62 | int s5p_serial_setbrg(struct udevice *dev, int baudrate) |
Minkyu Kang | fca3084 | 2009-10-01 17:20:28 +0900 | [diff] [blame] | 63 | { |
Simon Glass | 767e737 | 2014-09-14 16:36:17 -0600 | [diff] [blame^] | 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); |
Minkyu Kang | fca3084 | 2009-10-01 17:20:28 +0900 | [diff] [blame] | 67 | u32 val; |
| 68 | |
Minkyu Kang | 36f25cf | 2010-08-24 15:51:55 +0900 | [diff] [blame] | 69 | val = uclk / baudrate; |
Minkyu Kang | fca3084 | 2009-10-01 17:20:28 +0900 | [diff] [blame] | 70 | |
| 71 | writel(val / 16 - 1, &uart->ubrdiv); |
Minkyu Kang | bfa1424 | 2010-09-28 14:35:02 +0900 | [diff] [blame] | 72 | |
Minkyu Kang | afae8aa | 2011-01-24 14:43:25 +0900 | [diff] [blame] | 73 | if (s5p_uart_divslot()) |
Minkyu Kang | bfa1424 | 2010-09-28 14:35:02 +0900 | [diff] [blame] | 74 | writew(udivslot[val % 16], &uart->rest.slot); |
| 75 | else |
| 76 | writeb(val % 16, &uart->rest.value); |
Simon Glass | 767e737 | 2014-09-14 16:36:17 -0600 | [diff] [blame^] | 77 | |
| 78 | return 0; |
Minkyu Kang | fca3084 | 2009-10-01 17:20:28 +0900 | [diff] [blame] | 79 | } |
| 80 | |
Simon Glass | 767e737 | 2014-09-14 16:36:17 -0600 | [diff] [blame^] | 81 | static int s5p_serial_probe(struct udevice *dev) |
Minkyu Kang | fca3084 | 2009-10-01 17:20:28 +0900 | [diff] [blame] | 82 | { |
Simon Glass | 767e737 | 2014-09-14 16:36:17 -0600 | [diff] [blame^] | 83 | struct s5p_serial_platdata *plat = dev->platdata; |
| 84 | struct s5p_uart *const uart = plat->reg; |
Minkyu Kang | fca3084 | 2009-10-01 17:20:28 +0900 | [diff] [blame] | 85 | |
Inha Song | c0de847 | 2014-02-04 14:57:25 +0900 | [diff] [blame] | 86 | /* enable FIFOs, auto clear Rx FIFO */ |
| 87 | writel(0x3, &uart->ufcon); |
Minkyu Kang | fca3084 | 2009-10-01 17:20:28 +0900 | [diff] [blame] | 88 | writel(0, &uart->umcon); |
| 89 | /* 8N1 */ |
| 90 | writel(0x3, &uart->ulcon); |
| 91 | /* No interrupts, no DMA, pure polling */ |
| 92 | writel(0x245, &uart->ucon); |
| 93 | |
Minkyu Kang | fca3084 | 2009-10-01 17:20:28 +0900 | [diff] [blame] | 94 | return 0; |
| 95 | } |
| 96 | |
Simon Glass | 767e737 | 2014-09-14 16:36:17 -0600 | [diff] [blame^] | 97 | static int serial_err_check(const struct s5p_uart *const uart, int op) |
Minkyu Kang | fca3084 | 2009-10-01 17:20:28 +0900 | [diff] [blame] | 98 | { |
Minkyu Kang | 9455aab | 2009-11-10 20:23:50 +0900 | [diff] [blame] | 99 | unsigned int mask; |
Minkyu Kang | fca3084 | 2009-10-01 17:20:28 +0900 | [diff] [blame] | 100 | |
Minkyu Kang | 9455aab | 2009-11-10 20:23:50 +0900 | [diff] [blame] | 101 | /* |
| 102 | * UERSTAT |
| 103 | * Break Detect [3] |
| 104 | * Frame Err [2] : receive operation |
| 105 | * Parity Err [1] : receive operation |
| 106 | * Overrun Err [0] : receive operation |
| 107 | */ |
| 108 | if (op) |
| 109 | mask = 0x8; |
| 110 | else |
| 111 | mask = 0xf; |
Minkyu Kang | fca3084 | 2009-10-01 17:20:28 +0900 | [diff] [blame] | 112 | |
Minkyu Kang | 9455aab | 2009-11-10 20:23:50 +0900 | [diff] [blame] | 113 | return readl(&uart->uerstat) & mask; |
Minkyu Kang | fca3084 | 2009-10-01 17:20:28 +0900 | [diff] [blame] | 114 | } |
| 115 | |
Simon Glass | 767e737 | 2014-09-14 16:36:17 -0600 | [diff] [blame^] | 116 | static int s5p_serial_getc(struct udevice *dev) |
Minkyu Kang | fca3084 | 2009-10-01 17:20:28 +0900 | [diff] [blame] | 117 | { |
Simon Glass | 767e737 | 2014-09-14 16:36:17 -0600 | [diff] [blame^] | 118 | struct s5p_serial_platdata *plat = dev->platdata; |
| 119 | struct s5p_uart *const uart = plat->reg; |
Minkyu Kang | fca3084 | 2009-10-01 17:20:28 +0900 | [diff] [blame] | 120 | |
Simon Glass | 767e737 | 2014-09-14 16:36:17 -0600 | [diff] [blame^] | 121 | if (!(readl(&uart->ufstat) & RX_FIFO_COUNT_MASK)) |
| 122 | return -EAGAIN; |
Rajeshwari Shinde | bd19fa9 | 2013-06-24 16:47:22 +0530 | [diff] [blame] | 123 | |
Simon Glass | 767e737 | 2014-09-14 16:36:17 -0600 | [diff] [blame^] | 124 | serial_err_check(uart, 0); |
Minkyu Kang | 7622981 | 2010-07-06 20:08:29 +0900 | [diff] [blame] | 125 | return (int)(readb(&uart->urxh) & 0xff); |
Minkyu Kang | fca3084 | 2009-10-01 17:20:28 +0900 | [diff] [blame] | 126 | } |
| 127 | |
Simon Glass | 767e737 | 2014-09-14 16:36:17 -0600 | [diff] [blame^] | 128 | static int s5p_serial_putc(struct udevice *dev, const char ch) |
Minkyu Kang | fca3084 | 2009-10-01 17:20:28 +0900 | [diff] [blame] | 129 | { |
Simon Glass | 767e737 | 2014-09-14 16:36:17 -0600 | [diff] [blame^] | 130 | struct s5p_serial_platdata *plat = dev->platdata; |
| 131 | struct s5p_uart *const uart = plat->reg; |
Minkyu Kang | fca3084 | 2009-10-01 17:20:28 +0900 | [diff] [blame] | 132 | |
Simon Glass | 767e737 | 2014-09-14 16:36:17 -0600 | [diff] [blame^] | 133 | if (readl(&uart->ufstat) & TX_FIFO_FULL) |
| 134 | return -EAGAIN; |
Rajeshwari Shinde | bd19fa9 | 2013-06-24 16:47:22 +0530 | [diff] [blame] | 135 | |
Simon Glass | 767e737 | 2014-09-14 16:36:17 -0600 | [diff] [blame^] | 136 | writeb(ch, &uart->utxh); |
| 137 | serial_err_check(uart, 1); |
Minkyu Kang | fca3084 | 2009-10-01 17:20:28 +0900 | [diff] [blame] | 138 | |
Simon Glass | 767e737 | 2014-09-14 16:36:17 -0600 | [diff] [blame^] | 139 | return 0; |
Minkyu Kang | fca3084 | 2009-10-01 17:20:28 +0900 | [diff] [blame] | 140 | } |
| 141 | |
Simon Glass | 767e737 | 2014-09-14 16:36:17 -0600 | [diff] [blame^] | 142 | static int s5p_serial_pending(struct udevice *dev, bool input) |
Minkyu Kang | fca3084 | 2009-10-01 17:20:28 +0900 | [diff] [blame] | 143 | { |
Simon Glass | 767e737 | 2014-09-14 16:36:17 -0600 | [diff] [blame^] | 144 | struct s5p_serial_platdata *plat = dev->platdata; |
| 145 | struct s5p_uart *const uart = plat->reg; |
| 146 | uint32_t ufstat = readl(&uart->ufstat); |
Minkyu Kang | fca3084 | 2009-10-01 17:20:28 +0900 | [diff] [blame] | 147 | |
Simon Glass | 767e737 | 2014-09-14 16:36:17 -0600 | [diff] [blame^] | 148 | if (input) |
| 149 | return (ufstat & RX_FIFO_COUNT_MASK) >> RX_FIFO_COUNT_SHIFT; |
| 150 | else |
| 151 | return (ufstat & TX_FIFO_COUNT_MASK) >> TX_FIFO_COUNT_SHIFT; |
Marek Vasut | 5bcdf24 | 2012-09-09 18:48:28 +0200 | [diff] [blame] | 152 | } |
Minkyu Kang | fca3084 | 2009-10-01 17:20:28 +0900 | [diff] [blame] | 153 | |
Simon Glass | 767e737 | 2014-09-14 16:36:17 -0600 | [diff] [blame^] | 154 | static int s5p_serial_ofdata_to_platdata(struct udevice *dev) |
Rajeshwari Shinde | bd19fa9 | 2013-06-24 16:47:22 +0530 | [diff] [blame] | 155 | { |
Simon Glass | 767e737 | 2014-09-14 16:36:17 -0600 | [diff] [blame^] | 156 | struct s5p_serial_platdata *plat = dev->platdata; |
| 157 | fdt_addr_t addr; |
Rajeshwari Shinde | bd19fa9 | 2013-06-24 16:47:22 +0530 | [diff] [blame] | 158 | |
Simon Glass | 767e737 | 2014-09-14 16:36:17 -0600 | [diff] [blame^] | 159 | addr = fdtdec_get_addr(gd->fdt_blob, dev->of_offset, "reg"); |
| 160 | if (addr == FDT_ADDR_T_NONE) |
| 161 | return -EINVAL; |
Rajeshwari Shinde | bd19fa9 | 2013-06-24 16:47:22 +0530 | [diff] [blame] | 162 | |
Simon Glass | 767e737 | 2014-09-14 16:36:17 -0600 | [diff] [blame^] | 163 | plat->reg = (struct s5p_uart *)addr; |
| 164 | plat->port_id = fdtdec_get_int(gd->fdt_blob, dev->of_offset, "id", -1); |
Rajeshwari Shinde | bd19fa9 | 2013-06-24 16:47:22 +0530 | [diff] [blame] | 165 | |
| 166 | return 0; |
| 167 | } |
Rajeshwari Shinde | bd19fa9 | 2013-06-24 16:47:22 +0530 | [diff] [blame] | 168 | |
Simon Glass | 767e737 | 2014-09-14 16:36:17 -0600 | [diff] [blame^] | 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, |
| 174 | }; |
Rajeshwari Shinde | bd19fa9 | 2013-06-24 16:47:22 +0530 | [diff] [blame] | 175 | |
Simon Glass | 767e737 | 2014-09-14 16:36:17 -0600 | [diff] [blame^] | 176 | static const struct udevice_id s5p_serial_ids[] = { |
| 177 | { .compatible = "samsung,exynos4210-uart" }, |
| 178 | { } |
| 179 | }; |
Marek Vasut | 533e31e | 2012-09-12 19:39:57 +0200 | [diff] [blame] | 180 | |
Simon Glass | 767e737 | 2014-09-14 16:36:17 -0600 | [diff] [blame^] | 181 | U_BOOT_DRIVER(serial_s5p) = { |
| 182 | .name = "serial_s5p", |
| 183 | .id = UCLASS_SERIAL, |
| 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, |
| 190 | }; |