Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
wdenk | 1249065 | 2004-04-18 21:13:41 +0000 | [diff] [blame] | 2 | /* |
Michal Simek | 7e4372a | 2015-12-01 14:24:20 +0100 | [diff] [blame] | 3 | * (C) Copyright 2008 - 2015 Michal Simek <monstr@monstr.eu> |
Michal Simek | 403d619 | 2008-07-11 10:10:31 +0200 | [diff] [blame] | 4 | * Clean driver and add xilinx constant from header file |
wdenk | 1249065 | 2004-04-18 21:13:41 +0000 | [diff] [blame] | 5 | * |
Michal Simek | 403d619 | 2008-07-11 10:10:31 +0200 | [diff] [blame] | 6 | * (C) Copyright 2004 Atmark Techno, Inc. |
wdenk | 1249065 | 2004-04-18 21:13:41 +0000 | [diff] [blame] | 7 | * Yasushi SHOJI <yashi@atmark-techno.com> |
wdenk | 1249065 | 2004-04-18 21:13:41 +0000 | [diff] [blame] | 8 | */ |
| 9 | |
| 10 | #include <config.h> |
Michal Simek | 7e4372a | 2015-12-01 14:24:20 +0100 | [diff] [blame] | 11 | #include <dm.h> |
Michal Simek | 403d619 | 2008-07-11 10:10:31 +0200 | [diff] [blame] | 12 | #include <asm/io.h> |
Simon Glass | 4dcacfc | 2020-05-10 11:40:13 -0600 | [diff] [blame] | 13 | #include <linux/bitops.h> |
Michal Simek | 75348da | 2011-09-25 21:03:08 +0000 | [diff] [blame] | 14 | #include <linux/compiler.h> |
| 15 | #include <serial.h> |
wdenk | 1249065 | 2004-04-18 21:13:41 +0000 | [diff] [blame] | 16 | |
Michal Simek | 7e4372a | 2015-12-01 14:24:20 +0100 | [diff] [blame] | 17 | #define SR_TX_FIFO_FULL BIT(3) /* transmit FIFO full */ |
| 18 | #define SR_TX_FIFO_EMPTY BIT(2) /* transmit FIFO empty */ |
| 19 | #define SR_RX_FIFO_VALID_DATA BIT(0) /* data in receive FIFO */ |
| 20 | #define SR_RX_FIFO_FULL BIT(1) /* receive FIFO full */ |
wdenk | 1249065 | 2004-04-18 21:13:41 +0000 | [diff] [blame] | 21 | |
Michal Simek | e5b3c1d | 2014-01-21 07:29:47 +0100 | [diff] [blame] | 22 | #define ULITE_CONTROL_RST_TX 0x01 |
| 23 | #define ULITE_CONTROL_RST_RX 0x02 |
| 24 | |
Michal Simek | 8e4e5f0 | 2024-02-14 12:52:32 +0100 | [diff] [blame] | 25 | static bool little_endian __section(".data"); |
T Karthik Reddy | 070ffd2 | 2020-08-14 03:02:15 -0600 | [diff] [blame] | 26 | |
Michal Simek | 75348da | 2011-09-25 21:03:08 +0000 | [diff] [blame] | 27 | struct uartlite { |
| 28 | unsigned int rx_fifo; |
| 29 | unsigned int tx_fifo; |
| 30 | unsigned int status; |
Michal Simek | e5b3c1d | 2014-01-21 07:29:47 +0100 | [diff] [blame] | 31 | unsigned int control; |
Michal Simek | 75348da | 2011-09-25 21:03:08 +0000 | [diff] [blame] | 32 | }; |
| 33 | |
Simon Glass | b75b15b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 34 | struct uartlite_plat { |
Michal Simek | 7e4372a | 2015-12-01 14:24:20 +0100 | [diff] [blame] | 35 | struct uartlite *regs; |
Michal Simek | 75348da | 2011-09-25 21:03:08 +0000 | [diff] [blame] | 36 | }; |
| 37 | |
T Karthik Reddy | 070ffd2 | 2020-08-14 03:02:15 -0600 | [diff] [blame] | 38 | static u32 uart_in32(void __iomem *addr) |
| 39 | { |
| 40 | if (little_endian) |
| 41 | return in_le32(addr); |
| 42 | else |
| 43 | return in_be32(addr); |
| 44 | } |
| 45 | |
| 46 | static void uart_out32(void __iomem *addr, u32 val) |
| 47 | { |
| 48 | if (little_endian) |
| 49 | out_le32(addr, val); |
| 50 | else |
| 51 | out_be32(addr, val); |
| 52 | } |
| 53 | |
Michal Simek | 7e4372a | 2015-12-01 14:24:20 +0100 | [diff] [blame] | 54 | static int uartlite_serial_putc(struct udevice *dev, const char ch) |
Michal Simek | 75348da | 2011-09-25 21:03:08 +0000 | [diff] [blame] | 55 | { |
Simon Glass | b75b15b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 56 | struct uartlite_plat *plat = dev_get_plat(dev); |
Michal Simek | 7e4372a | 2015-12-01 14:24:20 +0100 | [diff] [blame] | 57 | struct uartlite *regs = plat->regs; |
Michal Simek | 75348da | 2011-09-25 21:03:08 +0000 | [diff] [blame] | 58 | |
T Karthik Reddy | 070ffd2 | 2020-08-14 03:02:15 -0600 | [diff] [blame] | 59 | if (uart_in32(®s->status) & SR_TX_FIFO_FULL) |
Michal Simek | 7e4372a | 2015-12-01 14:24:20 +0100 | [diff] [blame] | 60 | return -EAGAIN; |
Michal Simek | 75348da | 2011-09-25 21:03:08 +0000 | [diff] [blame] | 61 | |
T Karthik Reddy | 070ffd2 | 2020-08-14 03:02:15 -0600 | [diff] [blame] | 62 | uart_out32(®s->tx_fifo, ch & 0xff); |
Michal Simek | 75348da | 2011-09-25 21:03:08 +0000 | [diff] [blame] | 63 | |
Michal Simek | 7e4372a | 2015-12-01 14:24:20 +0100 | [diff] [blame] | 64 | return 0; |
Michal Simek | 75348da | 2011-09-25 21:03:08 +0000 | [diff] [blame] | 65 | } |
| 66 | |
Michal Simek | 7e4372a | 2015-12-01 14:24:20 +0100 | [diff] [blame] | 67 | static int uartlite_serial_getc(struct udevice *dev) |
Michal Simek | 75348da | 2011-09-25 21:03:08 +0000 | [diff] [blame] | 68 | { |
Simon Glass | b75b15b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 69 | struct uartlite_plat *plat = dev_get_plat(dev); |
Michal Simek | 7e4372a | 2015-12-01 14:24:20 +0100 | [diff] [blame] | 70 | struct uartlite *regs = plat->regs; |
| 71 | |
T Karthik Reddy | 070ffd2 | 2020-08-14 03:02:15 -0600 | [diff] [blame] | 72 | if (!(uart_in32(®s->status) & SR_RX_FIFO_VALID_DATA)) |
Michal Simek | 7e4372a | 2015-12-01 14:24:20 +0100 | [diff] [blame] | 73 | return -EAGAIN; |
Michal Simek | 75348da | 2011-09-25 21:03:08 +0000 | [diff] [blame] | 74 | |
T Karthik Reddy | 070ffd2 | 2020-08-14 03:02:15 -0600 | [diff] [blame] | 75 | return uart_in32(®s->rx_fifo) & 0xff; |
Michal Simek | 75348da | 2011-09-25 21:03:08 +0000 | [diff] [blame] | 76 | } |
| 77 | |
Michal Simek | 7e4372a | 2015-12-01 14:24:20 +0100 | [diff] [blame] | 78 | static int uartlite_serial_pending(struct udevice *dev, bool input) |
Michal Simek | 75348da | 2011-09-25 21:03:08 +0000 | [diff] [blame] | 79 | { |
Simon Glass | b75b15b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 80 | struct uartlite_plat *plat = dev_get_plat(dev); |
Michal Simek | 7e4372a | 2015-12-01 14:24:20 +0100 | [diff] [blame] | 81 | struct uartlite *regs = plat->regs; |
wdenk | 1249065 | 2004-04-18 21:13:41 +0000 | [diff] [blame] | 82 | |
Michal Simek | 7e4372a | 2015-12-01 14:24:20 +0100 | [diff] [blame] | 83 | if (input) |
T Karthik Reddy | 070ffd2 | 2020-08-14 03:02:15 -0600 | [diff] [blame] | 84 | return uart_in32(®s->status) & SR_RX_FIFO_VALID_DATA; |
Michal Simek | 7e4372a | 2015-12-01 14:24:20 +0100 | [diff] [blame] | 85 | |
T Karthik Reddy | 070ffd2 | 2020-08-14 03:02:15 -0600 | [diff] [blame] | 86 | return !(uart_in32(®s->status) & SR_TX_FIFO_EMPTY); |
Michal Simek | 75348da | 2011-09-25 21:03:08 +0000 | [diff] [blame] | 87 | } |
| 88 | |
Michal Simek | 7e4372a | 2015-12-01 14:24:20 +0100 | [diff] [blame] | 89 | static int uartlite_serial_probe(struct udevice *dev) |
Michal Simek | d40a252 | 2012-07-02 10:32:18 +0200 | [diff] [blame] | 90 | { |
Simon Glass | b75b15b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 91 | struct uartlite_plat *plat = dev_get_plat(dev); |
Michal Simek | 7e4372a | 2015-12-01 14:24:20 +0100 | [diff] [blame] | 92 | struct uartlite *regs = plat->regs; |
T Karthik Reddy | 070ffd2 | 2020-08-14 03:02:15 -0600 | [diff] [blame] | 93 | int ret; |
Michal Simek | e5b3c1d | 2014-01-21 07:29:47 +0100 | [diff] [blame] | 94 | |
T Karthik Reddy | 070ffd2 | 2020-08-14 03:02:15 -0600 | [diff] [blame] | 95 | uart_out32(®s->control, 0); |
| 96 | uart_out32(®s->control, ULITE_CONTROL_RST_RX | ULITE_CONTROL_RST_TX); |
| 97 | ret = uart_in32(®s->status); |
| 98 | /* Endianness detection */ |
| 99 | if ((ret & SR_TX_FIFO_EMPTY) != SR_TX_FIFO_EMPTY) { |
| 100 | little_endian = true; |
| 101 | uart_out32(®s->control, ULITE_CONTROL_RST_RX | |
| 102 | ULITE_CONTROL_RST_TX); |
| 103 | } |
Michal Simek | e5b3c1d | 2014-01-21 07:29:47 +0100 | [diff] [blame] | 104 | |
Michal Simek | 7e4372a | 2015-12-01 14:24:20 +0100 | [diff] [blame] | 105 | return 0; |
Michal Simek | d40a252 | 2012-07-02 10:32:18 +0200 | [diff] [blame] | 106 | } |
| 107 | |
Simon Glass | aad29ae | 2020-12-03 16:55:21 -0700 | [diff] [blame] | 108 | static int uartlite_serial_of_to_plat(struct udevice *dev) |
Michal Simek | 7e4372a | 2015-12-01 14:24:20 +0100 | [diff] [blame] | 109 | { |
Simon Glass | b75b15b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 110 | struct uartlite_plat *plat = dev_get_plat(dev); |
Michal Simek | 7e4372a | 2015-12-01 14:24:20 +0100 | [diff] [blame] | 111 | |
Masahiro Yamada | 1096ae1 | 2020-07-17 14:36:46 +0900 | [diff] [blame] | 112 | plat->regs = dev_read_addr_ptr(dev); |
Michal Simek | 75348da | 2011-09-25 21:03:08 +0000 | [diff] [blame] | 113 | |
Michal Simek | 7e4372a | 2015-12-01 14:24:20 +0100 | [diff] [blame] | 114 | return 0; |
Marek Vasut | 5bcdf24 | 2012-09-09 18:48:28 +0200 | [diff] [blame] | 115 | } |
Michal Simek | 75348da | 2011-09-25 21:03:08 +0000 | [diff] [blame] | 116 | |
Michal Simek | 7e4372a | 2015-12-01 14:24:20 +0100 | [diff] [blame] | 117 | static const struct dm_serial_ops uartlite_serial_ops = { |
| 118 | .putc = uartlite_serial_putc, |
| 119 | .pending = uartlite_serial_pending, |
| 120 | .getc = uartlite_serial_getc, |
| 121 | }; |
Michal Simek | d40a252 | 2012-07-02 10:32:18 +0200 | [diff] [blame] | 122 | |
Michal Simek | 7e4372a | 2015-12-01 14:24:20 +0100 | [diff] [blame] | 123 | static const struct udevice_id uartlite_serial_ids[] = { |
| 124 | { .compatible = "xlnx,opb-uartlite-1.00.b", }, |
| 125 | { .compatible = "xlnx,xps-uartlite-1.00.a" }, |
| 126 | { } |
| 127 | }; |
Marek Vasut | d97fb5c | 2012-09-12 19:45:58 +0200 | [diff] [blame] | 128 | |
Michal Simek | 7e4372a | 2015-12-01 14:24:20 +0100 | [diff] [blame] | 129 | U_BOOT_DRIVER(serial_uartlite) = { |
| 130 | .name = "serial_uartlite", |
| 131 | .id = UCLASS_SERIAL, |
| 132 | .of_match = uartlite_serial_ids, |
Simon Glass | aad29ae | 2020-12-03 16:55:21 -0700 | [diff] [blame] | 133 | .of_to_plat = uartlite_serial_of_to_plat, |
Simon Glass | b75b15b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 134 | .plat_auto = sizeof(struct uartlite_plat), |
Michal Simek | 7e4372a | 2015-12-01 14:24:20 +0100 | [diff] [blame] | 135 | .probe = uartlite_serial_probe, |
| 136 | .ops = &uartlite_serial_ops, |
Michal Simek | 7e4372a | 2015-12-01 14:24:20 +0100 | [diff] [blame] | 137 | }; |
Michal Simek | 8af618b | 2015-12-14 16:55:10 +0100 | [diff] [blame] | 138 | |
| 139 | #ifdef CONFIG_DEBUG_UART_UARTLITE |
| 140 | |
| 141 | #include <debug_uart.h> |
| 142 | |
| 143 | static inline void _debug_uart_init(void) |
| 144 | { |
Pali Rohár | 8864b35 | 2022-05-27 22:15:24 +0200 | [diff] [blame] | 145 | struct uartlite *regs = (struct uartlite *)CONFIG_VAL(DEBUG_UART_BASE); |
T Karthik Reddy | 070ffd2 | 2020-08-14 03:02:15 -0600 | [diff] [blame] | 146 | int ret; |
Michal Simek | 8af618b | 2015-12-14 16:55:10 +0100 | [diff] [blame] | 147 | |
T Karthik Reddy | 070ffd2 | 2020-08-14 03:02:15 -0600 | [diff] [blame] | 148 | uart_out32(®s->control, 0); |
| 149 | uart_out32(®s->control, ULITE_CONTROL_RST_RX | ULITE_CONTROL_RST_TX); |
Ashok Reddy Soma | 5190dec | 2020-12-01 00:34:47 -0700 | [diff] [blame] | 150 | ret = uart_in32(®s->status); |
T Karthik Reddy | 070ffd2 | 2020-08-14 03:02:15 -0600 | [diff] [blame] | 151 | /* Endianness detection */ |
| 152 | if ((ret & SR_TX_FIFO_EMPTY) != SR_TX_FIFO_EMPTY) { |
| 153 | little_endian = true; |
| 154 | uart_out32(®s->control, ULITE_CONTROL_RST_RX | |
| 155 | ULITE_CONTROL_RST_TX); |
| 156 | } |
Michal Simek | 8af618b | 2015-12-14 16:55:10 +0100 | [diff] [blame] | 157 | } |
| 158 | |
| 159 | static inline void _debug_uart_putc(int ch) |
| 160 | { |
Pali Rohár | 8864b35 | 2022-05-27 22:15:24 +0200 | [diff] [blame] | 161 | struct uartlite *regs = (struct uartlite *)CONFIG_VAL(DEBUG_UART_BASE); |
Michal Simek | 8af618b | 2015-12-14 16:55:10 +0100 | [diff] [blame] | 162 | |
T Karthik Reddy | 070ffd2 | 2020-08-14 03:02:15 -0600 | [diff] [blame] | 163 | while (uart_in32(®s->status) & SR_TX_FIFO_FULL) |
Michal Simek | 8af618b | 2015-12-14 16:55:10 +0100 | [diff] [blame] | 164 | ; |
| 165 | |
T Karthik Reddy | 070ffd2 | 2020-08-14 03:02:15 -0600 | [diff] [blame] | 166 | uart_out32(®s->tx_fifo, ch & 0xff); |
Michal Simek | 8af618b | 2015-12-14 16:55:10 +0100 | [diff] [blame] | 167 | } |
| 168 | |
| 169 | DEBUG_UART_FUNCS |
| 170 | #endif |