Tom Rini | 83d290c | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
wdenk | 507bbe3 | 2004-04-18 21:13:41 +0000 | [diff] [blame] | 2 | /* |
Michal Simek | 9376839 | 2015-12-01 14:24:20 +0100 | [diff] [blame] | 3 | * (C) Copyright 2008 - 2015 Michal Simek <monstr@monstr.eu> |
Michal Simek | 53ea981 | 2008-07-11 10:10:31 +0200 | [diff] [blame] | 4 | * Clean driver and add xilinx constant from header file |
wdenk | 507bbe3 | 2004-04-18 21:13:41 +0000 | [diff] [blame] | 5 | * |
Michal Simek | 53ea981 | 2008-07-11 10:10:31 +0200 | [diff] [blame] | 6 | * (C) Copyright 2004 Atmark Techno, Inc. |
wdenk | 507bbe3 | 2004-04-18 21:13:41 +0000 | [diff] [blame] | 7 | * Yasushi SHOJI <yashi@atmark-techno.com> |
wdenk | 507bbe3 | 2004-04-18 21:13:41 +0000 | [diff] [blame] | 8 | */ |
| 9 | |
| 10 | #include <config.h> |
Michal Simek | 49a23e4 | 2011-09-25 21:03:08 +0000 | [diff] [blame] | 11 | #include <common.h> |
Michal Simek | 9376839 | 2015-12-01 14:24:20 +0100 | [diff] [blame] | 12 | #include <dm.h> |
Michal Simek | 53ea981 | 2008-07-11 10:10:31 +0200 | [diff] [blame] | 13 | #include <asm/io.h> |
Simon Glass | cd93d62 | 2020-05-10 11:40:13 -0600 | [diff] [blame] | 14 | #include <linux/bitops.h> |
Michal Simek | 49a23e4 | 2011-09-25 21:03:08 +0000 | [diff] [blame] | 15 | #include <linux/compiler.h> |
| 16 | #include <serial.h> |
wdenk | 507bbe3 | 2004-04-18 21:13:41 +0000 | [diff] [blame] | 17 | |
Michal Simek | 9376839 | 2015-12-01 14:24:20 +0100 | [diff] [blame] | 18 | #define SR_TX_FIFO_FULL BIT(3) /* transmit FIFO full */ |
| 19 | #define SR_TX_FIFO_EMPTY BIT(2) /* transmit FIFO empty */ |
| 20 | #define SR_RX_FIFO_VALID_DATA BIT(0) /* data in receive FIFO */ |
| 21 | #define SR_RX_FIFO_FULL BIT(1) /* receive FIFO full */ |
wdenk | 507bbe3 | 2004-04-18 21:13:41 +0000 | [diff] [blame] | 22 | |
Michal Simek | 8c3bd6b | 2014-01-21 07:29:47 +0100 | [diff] [blame] | 23 | #define ULITE_CONTROL_RST_TX 0x01 |
| 24 | #define ULITE_CONTROL_RST_RX 0x02 |
| 25 | |
Michal Simek | 49a23e4 | 2011-09-25 21:03:08 +0000 | [diff] [blame] | 26 | struct uartlite { |
| 27 | unsigned int rx_fifo; |
| 28 | unsigned int tx_fifo; |
| 29 | unsigned int status; |
Michal Simek | 8c3bd6b | 2014-01-21 07:29:47 +0100 | [diff] [blame] | 30 | unsigned int control; |
Michal Simek | 49a23e4 | 2011-09-25 21:03:08 +0000 | [diff] [blame] | 31 | }; |
wdenk | 507bbe3 | 2004-04-18 21:13:41 +0000 | [diff] [blame] | 32 | |
Michal Simek | 9376839 | 2015-12-01 14:24:20 +0100 | [diff] [blame] | 33 | struct uartlite_platdata { |
| 34 | struct uartlite *regs; |
Michal Simek | 49a23e4 | 2011-09-25 21:03:08 +0000 | [diff] [blame] | 35 | }; |
| 36 | |
Michal Simek | 9376839 | 2015-12-01 14:24:20 +0100 | [diff] [blame] | 37 | static int uartlite_serial_putc(struct udevice *dev, const char ch) |
Michal Simek | 49a23e4 | 2011-09-25 21:03:08 +0000 | [diff] [blame] | 38 | { |
Michal Simek | 9376839 | 2015-12-01 14:24:20 +0100 | [diff] [blame] | 39 | struct uartlite_platdata *plat = dev_get_platdata(dev); |
| 40 | struct uartlite *regs = plat->regs; |
Michal Simek | 49a23e4 | 2011-09-25 21:03:08 +0000 | [diff] [blame] | 41 | |
Michal Simek | 9376839 | 2015-12-01 14:24:20 +0100 | [diff] [blame] | 42 | if (in_be32(®s->status) & SR_TX_FIFO_FULL) |
| 43 | return -EAGAIN; |
Michal Simek | 49a23e4 | 2011-09-25 21:03:08 +0000 | [diff] [blame] | 44 | |
Michal Simek | 9376839 | 2015-12-01 14:24:20 +0100 | [diff] [blame] | 45 | out_be32(®s->tx_fifo, ch & 0xff); |
| 46 | |
| 47 | return 0; |
Michal Simek | 49a23e4 | 2011-09-25 21:03:08 +0000 | [diff] [blame] | 48 | } |
| 49 | |
Michal Simek | 9376839 | 2015-12-01 14:24:20 +0100 | [diff] [blame] | 50 | static int uartlite_serial_getc(struct udevice *dev) |
Michal Simek | 49a23e4 | 2011-09-25 21:03:08 +0000 | [diff] [blame] | 51 | { |
Michal Simek | 9376839 | 2015-12-01 14:24:20 +0100 | [diff] [blame] | 52 | struct uartlite_platdata *plat = dev_get_platdata(dev); |
| 53 | struct uartlite *regs = plat->regs; |
Michal Simek | 49a23e4 | 2011-09-25 21:03:08 +0000 | [diff] [blame] | 54 | |
Michal Simek | 9376839 | 2015-12-01 14:24:20 +0100 | [diff] [blame] | 55 | if (!(in_be32(®s->status) & SR_RX_FIFO_VALID_DATA)) |
| 56 | return -EAGAIN; |
Michal Simek | 49a23e4 | 2011-09-25 21:03:08 +0000 | [diff] [blame] | 57 | |
Michal Simek | 49a23e4 | 2011-09-25 21:03:08 +0000 | [diff] [blame] | 58 | return in_be32(®s->rx_fifo) & 0xff; |
| 59 | } |
| 60 | |
Michal Simek | 9376839 | 2015-12-01 14:24:20 +0100 | [diff] [blame] | 61 | static int uartlite_serial_pending(struct udevice *dev, bool input) |
Michal Simek | 49a23e4 | 2011-09-25 21:03:08 +0000 | [diff] [blame] | 62 | { |
Michal Simek | 9376839 | 2015-12-01 14:24:20 +0100 | [diff] [blame] | 63 | struct uartlite_platdata *plat = dev_get_platdata(dev); |
| 64 | struct uartlite *regs = plat->regs; |
Michal Simek | 49a23e4 | 2011-09-25 21:03:08 +0000 | [diff] [blame] | 65 | |
Michal Simek | 9376839 | 2015-12-01 14:24:20 +0100 | [diff] [blame] | 66 | if (input) |
| 67 | return in_be32(®s->status) & SR_RX_FIFO_VALID_DATA; |
| 68 | |
| 69 | return !(in_be32(®s->status) & SR_TX_FIFO_EMPTY); |
Michal Simek | 49a23e4 | 2011-09-25 21:03:08 +0000 | [diff] [blame] | 70 | } |
| 71 | |
Michal Simek | 9376839 | 2015-12-01 14:24:20 +0100 | [diff] [blame] | 72 | static int uartlite_serial_probe(struct udevice *dev) |
Michal Simek | 25239e1 | 2012-07-02 10:32:18 +0200 | [diff] [blame] | 73 | { |
Michal Simek | 9376839 | 2015-12-01 14:24:20 +0100 | [diff] [blame] | 74 | struct uartlite_platdata *plat = dev_get_platdata(dev); |
| 75 | struct uartlite *regs = plat->regs; |
Michal Simek | 8c3bd6b | 2014-01-21 07:29:47 +0100 | [diff] [blame] | 76 | |
Michal Simek | 9376839 | 2015-12-01 14:24:20 +0100 | [diff] [blame] | 77 | out_be32(®s->control, 0); |
| 78 | out_be32(®s->control, ULITE_CONTROL_RST_RX | ULITE_CONTROL_RST_TX); |
| 79 | in_be32(®s->control); |
Michal Simek | 8c3bd6b | 2014-01-21 07:29:47 +0100 | [diff] [blame] | 80 | |
Michal Simek | 9376839 | 2015-12-01 14:24:20 +0100 | [diff] [blame] | 81 | return 0; |
Michal Simek | 25239e1 | 2012-07-02 10:32:18 +0200 | [diff] [blame] | 82 | } |
| 83 | |
Michal Simek | 9376839 | 2015-12-01 14:24:20 +0100 | [diff] [blame] | 84 | static int uartlite_serial_ofdata_to_platdata(struct udevice *dev) |
Michal Simek | 49a23e4 | 2011-09-25 21:03:08 +0000 | [diff] [blame] | 85 | { |
Michal Simek | 9376839 | 2015-12-01 14:24:20 +0100 | [diff] [blame] | 86 | struct uartlite_platdata *plat = dev_get_platdata(dev); |
Michal Simek | 25239e1 | 2012-07-02 10:32:18 +0200 | [diff] [blame] | 87 | |
Simon Glass | a821c4a | 2017-05-17 17:18:05 -0600 | [diff] [blame] | 88 | plat->regs = (struct uartlite *)devfdt_get_addr(dev); |
Michal Simek | 9376839 | 2015-12-01 14:24:20 +0100 | [diff] [blame] | 89 | |
| 90 | return 0; |
Michal Simek | 49a23e4 | 2011-09-25 21:03:08 +0000 | [diff] [blame] | 91 | } |
Marek Vasut | 87d6922 | 2012-09-12 19:45:58 +0200 | [diff] [blame] | 92 | |
Michal Simek | 9376839 | 2015-12-01 14:24:20 +0100 | [diff] [blame] | 93 | static const struct dm_serial_ops uartlite_serial_ops = { |
| 94 | .putc = uartlite_serial_putc, |
| 95 | .pending = uartlite_serial_pending, |
| 96 | .getc = uartlite_serial_getc, |
| 97 | }; |
| 98 | |
| 99 | static const struct udevice_id uartlite_serial_ids[] = { |
| 100 | { .compatible = "xlnx,opb-uartlite-1.00.b", }, |
| 101 | { .compatible = "xlnx,xps-uartlite-1.00.a" }, |
| 102 | { } |
| 103 | }; |
| 104 | |
| 105 | U_BOOT_DRIVER(serial_uartlite) = { |
| 106 | .name = "serial_uartlite", |
| 107 | .id = UCLASS_SERIAL, |
| 108 | .of_match = uartlite_serial_ids, |
| 109 | .ofdata_to_platdata = uartlite_serial_ofdata_to_platdata, |
| 110 | .platdata_auto_alloc_size = sizeof(struct uartlite_platdata), |
| 111 | .probe = uartlite_serial_probe, |
| 112 | .ops = &uartlite_serial_ops, |
Michal Simek | 9376839 | 2015-12-01 14:24:20 +0100 | [diff] [blame] | 113 | }; |
Michal Simek | 4166ba3 | 2015-12-14 16:55:10 +0100 | [diff] [blame] | 114 | |
| 115 | #ifdef CONFIG_DEBUG_UART_UARTLITE |
| 116 | |
| 117 | #include <debug_uart.h> |
| 118 | |
| 119 | static inline void _debug_uart_init(void) |
| 120 | { |
| 121 | struct uartlite *regs = (struct uartlite *)CONFIG_DEBUG_UART_BASE; |
| 122 | |
| 123 | out_be32(®s->control, 0); |
| 124 | out_be32(®s->control, ULITE_CONTROL_RST_RX | ULITE_CONTROL_RST_TX); |
| 125 | in_be32(®s->control); |
| 126 | } |
| 127 | |
| 128 | static inline void _debug_uart_putc(int ch) |
| 129 | { |
| 130 | struct uartlite *regs = (struct uartlite *)CONFIG_DEBUG_UART_BASE; |
| 131 | |
| 132 | while (in_be32(®s->status) & SR_TX_FIFO_FULL) |
| 133 | ; |
| 134 | |
| 135 | out_be32(®s->tx_fifo, ch & 0xff); |
| 136 | } |
| 137 | |
| 138 | DEBUG_UART_FUNCS |
| 139 | #endif |