wdenk | 507bbe3 | 2004-04-18 21:13:41 +0000 | [diff] [blame] | 1 | /* |
Michal Simek | 49a23e4 | 2011-09-25 21:03:08 +0000 | [diff] [blame] | 2 | * (C) Copyright 2008-2011 Michal Simek <monstr@monstr.eu> |
Michal Simek | 53ea981 | 2008-07-11 10:10:31 +0200 | [diff] [blame] | 3 | * Clean driver and add xilinx constant from header file |
wdenk | 507bbe3 | 2004-04-18 21:13:41 +0000 | [diff] [blame] | 4 | * |
Michal Simek | 53ea981 | 2008-07-11 10:10:31 +0200 | [diff] [blame] | 5 | * (C) Copyright 2004 Atmark Techno, Inc. |
wdenk | 507bbe3 | 2004-04-18 21:13:41 +0000 | [diff] [blame] | 6 | * Yasushi SHOJI <yashi@atmark-techno.com> |
| 7 | * |
Wolfgang Denk | 1a45966 | 2013-07-08 09:37:19 +0200 | [diff] [blame] | 8 | * SPDX-License-Identifier: GPL-2.0+ |
wdenk | 507bbe3 | 2004-04-18 21:13:41 +0000 | [diff] [blame] | 9 | */ |
| 10 | |
| 11 | #include <config.h> |
Michal Simek | 49a23e4 | 2011-09-25 21:03:08 +0000 | [diff] [blame] | 12 | #include <common.h> |
Michal Simek | 53ea981 | 2008-07-11 10:10:31 +0200 | [diff] [blame] | 13 | #include <asm/io.h> |
Michal Simek | 49a23e4 | 2011-09-25 21:03:08 +0000 | [diff] [blame] | 14 | #include <linux/compiler.h> |
| 15 | #include <serial.h> |
wdenk | 507bbe3 | 2004-04-18 21:13:41 +0000 | [diff] [blame] | 16 | |
Michal Simek | 53ea981 | 2008-07-11 10:10:31 +0200 | [diff] [blame] | 17 | #define SR_TX_FIFO_FULL 0x08 /* transmit FIFO full */ |
| 18 | #define SR_RX_FIFO_VALID_DATA 0x01 /* data in receive FIFO */ |
| 19 | #define SR_RX_FIFO_FULL 0x02 /* receive FIFO full */ |
wdenk | 507bbe3 | 2004-04-18 21:13:41 +0000 | [diff] [blame] | 20 | |
Michal Simek | 8c3bd6b | 2014-01-21 07:29:47 +0100 | [diff] [blame] | 21 | #define ULITE_CONTROL_RST_TX 0x01 |
| 22 | #define ULITE_CONTROL_RST_RX 0x02 |
| 23 | |
Michal Simek | 49a23e4 | 2011-09-25 21:03:08 +0000 | [diff] [blame] | 24 | struct uartlite { |
| 25 | unsigned int rx_fifo; |
| 26 | unsigned int tx_fifo; |
| 27 | unsigned int status; |
Michal Simek | 8c3bd6b | 2014-01-21 07:29:47 +0100 | [diff] [blame] | 28 | unsigned int control; |
Michal Simek | 49a23e4 | 2011-09-25 21:03:08 +0000 | [diff] [blame] | 29 | }; |
wdenk | 507bbe3 | 2004-04-18 21:13:41 +0000 | [diff] [blame] | 30 | |
Michal Simek | 8cb9b23 | 2011-10-18 00:22:10 +0000 | [diff] [blame] | 31 | static struct uartlite *userial_ports[4] = { |
Michal Simek | 49a23e4 | 2011-09-25 21:03:08 +0000 | [diff] [blame] | 32 | #ifdef XILINX_UARTLITE_BASEADDR |
| 33 | [0] = (struct uartlite *)XILINX_UARTLITE_BASEADDR, |
| 34 | #endif |
| 35 | #ifdef XILINX_UARTLITE_BASEADDR1 |
| 36 | [1] = (struct uartlite *)XILINX_UARTLITE_BASEADDR1, |
| 37 | #endif |
| 38 | #ifdef XILINX_UARTLITE_BASEADDR2 |
| 39 | [2] = (struct uartlite *)XILINX_UARTLITE_BASEADDR2, |
| 40 | #endif |
| 41 | #ifdef XILINX_UARTLITE_BASEADDR3 |
| 42 | [3] = (struct uartlite *)XILINX_UARTLITE_BASEADDR3 |
| 43 | #endif |
| 44 | }; |
| 45 | |
Axel Lin | 212d7da | 2013-10-16 09:45:56 +0800 | [diff] [blame] | 46 | static void uartlite_serial_putc(const char c, const int port) |
Michal Simek | 49a23e4 | 2011-09-25 21:03:08 +0000 | [diff] [blame] | 47 | { |
| 48 | struct uartlite *regs = userial_ports[port]; |
| 49 | |
| 50 | if (c == '\n') |
| 51 | uartlite_serial_putc('\r', port); |
| 52 | |
| 53 | while (in_be32(®s->status) & SR_TX_FIFO_FULL) |
| 54 | ; |
| 55 | out_be32(®s->tx_fifo, c & 0xff); |
| 56 | } |
| 57 | |
Axel Lin | 212d7da | 2013-10-16 09:45:56 +0800 | [diff] [blame] | 58 | static void uartlite_serial_puts(const char *s, const int port) |
Michal Simek | 49a23e4 | 2011-09-25 21:03:08 +0000 | [diff] [blame] | 59 | { |
| 60 | while (*s) |
| 61 | uartlite_serial_putc(*s++, port); |
| 62 | } |
| 63 | |
Axel Lin | 212d7da | 2013-10-16 09:45:56 +0800 | [diff] [blame] | 64 | static int uartlite_serial_getc(const int port) |
Michal Simek | 49a23e4 | 2011-09-25 21:03:08 +0000 | [diff] [blame] | 65 | { |
| 66 | struct uartlite *regs = userial_ports[port]; |
| 67 | |
| 68 | while (!(in_be32(®s->status) & SR_RX_FIFO_VALID_DATA)) |
| 69 | ; |
| 70 | return in_be32(®s->rx_fifo) & 0xff; |
| 71 | } |
| 72 | |
Axel Lin | 212d7da | 2013-10-16 09:45:56 +0800 | [diff] [blame] | 73 | static int uartlite_serial_tstc(const int port) |
Michal Simek | 49a23e4 | 2011-09-25 21:03:08 +0000 | [diff] [blame] | 74 | { |
| 75 | struct uartlite *regs = userial_ports[port]; |
| 76 | |
| 77 | return in_be32(®s->status) & SR_RX_FIFO_VALID_DATA; |
| 78 | } |
| 79 | |
Michal Simek | 25239e1 | 2012-07-02 10:32:18 +0200 | [diff] [blame] | 80 | static int uartlite_serial_init(const int port) |
| 81 | { |
Michal Simek | 8c3bd6b | 2014-01-21 07:29:47 +0100 | [diff] [blame] | 82 | struct uartlite *regs = userial_ports[port]; |
| 83 | |
| 84 | if (regs) { |
| 85 | out_be32(®s->control, 0); |
| 86 | out_be32(®s->control, |
| 87 | ULITE_CONTROL_RST_RX | ULITE_CONTROL_RST_TX); |
| 88 | in_be32(®s->control); |
Michal Simek | 25239e1 | 2012-07-02 10:32:18 +0200 | [diff] [blame] | 89 | return 0; |
Michal Simek | 8c3bd6b | 2014-01-21 07:29:47 +0100 | [diff] [blame] | 90 | } |
| 91 | |
Michal Simek | 25239e1 | 2012-07-02 10:32:18 +0200 | [diff] [blame] | 92 | return -1; |
| 93 | } |
| 94 | |
Michal Simek | 49a23e4 | 2011-09-25 21:03:08 +0000 | [diff] [blame] | 95 | /* Multi serial device functions */ |
| 96 | #define DECLARE_ESERIAL_FUNCTIONS(port) \ |
Axel Lin | 212d7da | 2013-10-16 09:45:56 +0800 | [diff] [blame] | 97 | static int userial##port##_init(void) \ |
Michal Simek | 25239e1 | 2012-07-02 10:32:18 +0200 | [diff] [blame] | 98 | { return uartlite_serial_init(port); } \ |
Axel Lin | 212d7da | 2013-10-16 09:45:56 +0800 | [diff] [blame] | 99 | static void userial##port##_setbrg(void) {} \ |
| 100 | static int userial##port##_getc(void) \ |
Michal Simek | 49a23e4 | 2011-09-25 21:03:08 +0000 | [diff] [blame] | 101 | { return uartlite_serial_getc(port); } \ |
Axel Lin | 212d7da | 2013-10-16 09:45:56 +0800 | [diff] [blame] | 102 | static int userial##port##_tstc(void) \ |
Michal Simek | 49a23e4 | 2011-09-25 21:03:08 +0000 | [diff] [blame] | 103 | { return uartlite_serial_tstc(port); } \ |
Axel Lin | 212d7da | 2013-10-16 09:45:56 +0800 | [diff] [blame] | 104 | static void userial##port##_putc(const char c) \ |
Michal Simek | 49a23e4 | 2011-09-25 21:03:08 +0000 | [diff] [blame] | 105 | { uartlite_serial_putc(c, port); } \ |
Axel Lin | 212d7da | 2013-10-16 09:45:56 +0800 | [diff] [blame] | 106 | static void userial##port##_puts(const char *s) \ |
Michal Simek | 49a23e4 | 2011-09-25 21:03:08 +0000 | [diff] [blame] | 107 | { uartlite_serial_puts(s, port); } |
| 108 | |
| 109 | /* Serial device descriptor */ |
Marek Vasut | 90bad89 | 2012-09-09 18:48:28 +0200 | [diff] [blame] | 110 | #define INIT_ESERIAL_STRUCTURE(port, __name) { \ |
| 111 | .name = __name, \ |
| 112 | .start = userial##port##_init, \ |
| 113 | .stop = NULL, \ |
| 114 | .setbrg = userial##port##_setbrg, \ |
| 115 | .getc = userial##port##_getc, \ |
| 116 | .tstc = userial##port##_tstc, \ |
| 117 | .putc = userial##port##_putc, \ |
| 118 | .puts = userial##port##_puts, \ |
| 119 | } |
Michal Simek | 49a23e4 | 2011-09-25 21:03:08 +0000 | [diff] [blame] | 120 | |
| 121 | DECLARE_ESERIAL_FUNCTIONS(0); |
| 122 | struct serial_device uartlite_serial0_device = |
| 123 | INIT_ESERIAL_STRUCTURE(0, "ttyUL0"); |
| 124 | DECLARE_ESERIAL_FUNCTIONS(1); |
| 125 | struct serial_device uartlite_serial1_device = |
| 126 | INIT_ESERIAL_STRUCTURE(1, "ttyUL1"); |
| 127 | DECLARE_ESERIAL_FUNCTIONS(2); |
| 128 | struct serial_device uartlite_serial2_device = |
| 129 | INIT_ESERIAL_STRUCTURE(2, "ttyUL2"); |
| 130 | DECLARE_ESERIAL_FUNCTIONS(3); |
| 131 | struct serial_device uartlite_serial3_device = |
| 132 | INIT_ESERIAL_STRUCTURE(3, "ttyUL3"); |
| 133 | |
| 134 | __weak struct serial_device *default_serial_console(void) |
| 135 | { |
Michal Simek | 25239e1 | 2012-07-02 10:32:18 +0200 | [diff] [blame] | 136 | if (userial_ports[0]) |
| 137 | return &uartlite_serial0_device; |
| 138 | if (userial_ports[1]) |
| 139 | return &uartlite_serial1_device; |
| 140 | if (userial_ports[2]) |
| 141 | return &uartlite_serial2_device; |
| 142 | if (userial_ports[3]) |
| 143 | return &uartlite_serial3_device; |
| 144 | |
| 145 | return NULL; |
Michal Simek | 49a23e4 | 2011-09-25 21:03:08 +0000 | [diff] [blame] | 146 | } |
Marek Vasut | 87d6922 | 2012-09-12 19:45:58 +0200 | [diff] [blame] | 147 | |
| 148 | void uartlite_serial_initialize(void) |
| 149 | { |
| 150 | #ifdef XILINX_UARTLITE_BASEADDR |
| 151 | serial_register(&uartlite_serial0_device); |
| 152 | #endif /* XILINX_UARTLITE_BASEADDR */ |
| 153 | #ifdef XILINX_UARTLITE_BASEADDR1 |
| 154 | serial_register(&uartlite_serial1_device); |
| 155 | #endif /* XILINX_UARTLITE_BASEADDR1 */ |
| 156 | #ifdef XILINX_UARTLITE_BASEADDR2 |
| 157 | serial_register(&uartlite_serial2_device); |
| 158 | #endif /* XILINX_UARTLITE_BASEADDR2 */ |
| 159 | #ifdef XILINX_UARTLITE_BASEADDR3 |
| 160 | serial_register(&uartlite_serial3_device); |
| 161 | #endif /* XILINX_UARTLITE_BASEADDR3 */ |
| 162 | } |