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 | * |
| 8 | * See file CREDITS for list of people who contributed to this |
| 9 | * project. |
| 10 | * |
| 11 | * This program is free software; you can redistribute it and/or |
| 12 | * modify it under the terms of the GNU General Public License as |
| 13 | * published by the Free Software Foundation; either version 2 of |
| 14 | * the License, or (at your option) any later version. |
| 15 | * |
| 16 | * This program is distributed in the hope that it will be useful, |
| 17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
Michal Simek | 53ea981 | 2008-07-11 10:10:31 +0200 | [diff] [blame] | 18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
wdenk | 507bbe3 | 2004-04-18 21:13:41 +0000 | [diff] [blame] | 19 | * GNU General Public License for more details. |
| 20 | * |
| 21 | * You should have received a copy of the GNU General Public License |
| 22 | * along with this program; if not, write to the Free Software |
| 23 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, |
| 24 | * MA 02111-1307 USA |
| 25 | */ |
| 26 | |
| 27 | #include <config.h> |
Michal Simek | 49a23e4 | 2011-09-25 21:03:08 +0000 | [diff] [blame] | 28 | #include <common.h> |
Michal Simek | 53ea981 | 2008-07-11 10:10:31 +0200 | [diff] [blame] | 29 | #include <asm/io.h> |
Michal Simek | 49a23e4 | 2011-09-25 21:03:08 +0000 | [diff] [blame] | 30 | #include <linux/compiler.h> |
| 31 | #include <serial.h> |
wdenk | 507bbe3 | 2004-04-18 21:13:41 +0000 | [diff] [blame] | 32 | |
Michal Simek | 53ea981 | 2008-07-11 10:10:31 +0200 | [diff] [blame] | 33 | #define SR_TX_FIFO_FULL 0x08 /* transmit FIFO full */ |
| 34 | #define SR_RX_FIFO_VALID_DATA 0x01 /* data in receive FIFO */ |
| 35 | #define SR_RX_FIFO_FULL 0x02 /* receive FIFO full */ |
wdenk | 507bbe3 | 2004-04-18 21:13:41 +0000 | [diff] [blame] | 36 | |
Michal Simek | 49a23e4 | 2011-09-25 21:03:08 +0000 | [diff] [blame] | 37 | struct uartlite { |
| 38 | unsigned int rx_fifo; |
| 39 | unsigned int tx_fifo; |
| 40 | unsigned int status; |
| 41 | }; |
wdenk | 507bbe3 | 2004-04-18 21:13:41 +0000 | [diff] [blame] | 42 | |
Michal Simek | 8cb9b23 | 2011-10-18 00:22:10 +0000 | [diff] [blame] | 43 | static struct uartlite *userial_ports[4] = { |
Michal Simek | 49a23e4 | 2011-09-25 21:03:08 +0000 | [diff] [blame] | 44 | #ifdef XILINX_UARTLITE_BASEADDR |
| 45 | [0] = (struct uartlite *)XILINX_UARTLITE_BASEADDR, |
| 46 | #endif |
| 47 | #ifdef XILINX_UARTLITE_BASEADDR1 |
| 48 | [1] = (struct uartlite *)XILINX_UARTLITE_BASEADDR1, |
| 49 | #endif |
| 50 | #ifdef XILINX_UARTLITE_BASEADDR2 |
| 51 | [2] = (struct uartlite *)XILINX_UARTLITE_BASEADDR2, |
| 52 | #endif |
| 53 | #ifdef XILINX_UARTLITE_BASEADDR3 |
| 54 | [3] = (struct uartlite *)XILINX_UARTLITE_BASEADDR3 |
| 55 | #endif |
| 56 | }; |
| 57 | |
| 58 | void uartlite_serial_putc(const char c, const int port) |
| 59 | { |
| 60 | struct uartlite *regs = userial_ports[port]; |
| 61 | |
| 62 | if (c == '\n') |
| 63 | uartlite_serial_putc('\r', port); |
| 64 | |
| 65 | while (in_be32(®s->status) & SR_TX_FIFO_FULL) |
| 66 | ; |
| 67 | out_be32(®s->tx_fifo, c & 0xff); |
| 68 | } |
| 69 | |
| 70 | void uartlite_serial_puts(const char *s, const int port) |
| 71 | { |
| 72 | while (*s) |
| 73 | uartlite_serial_putc(*s++, port); |
| 74 | } |
| 75 | |
| 76 | int uartlite_serial_getc(const int port) |
| 77 | { |
| 78 | struct uartlite *regs = userial_ports[port]; |
| 79 | |
| 80 | while (!(in_be32(®s->status) & SR_RX_FIFO_VALID_DATA)) |
| 81 | ; |
| 82 | return in_be32(®s->rx_fifo) & 0xff; |
| 83 | } |
| 84 | |
| 85 | int uartlite_serial_tstc(const int port) |
| 86 | { |
| 87 | struct uartlite *regs = userial_ports[port]; |
| 88 | |
| 89 | return in_be32(®s->status) & SR_RX_FIFO_VALID_DATA; |
| 90 | } |
| 91 | |
Michal Simek | 25239e1 | 2012-07-02 10:32:18 +0200 | [diff] [blame] | 92 | static int uartlite_serial_init(const int port) |
| 93 | { |
| 94 | if (userial_ports[port]) |
| 95 | return 0; |
| 96 | return -1; |
| 97 | } |
| 98 | |
Michal Simek | 49a23e4 | 2011-09-25 21:03:08 +0000 | [diff] [blame] | 99 | #if !defined(CONFIG_SERIAL_MULTI) |
wdenk | 507bbe3 | 2004-04-18 21:13:41 +0000 | [diff] [blame] | 100 | int serial_init(void) |
| 101 | { |
Michal Simek | 25239e1 | 2012-07-02 10:32:18 +0200 | [diff] [blame] | 102 | return uartlite_serial_init(0); |
wdenk | 507bbe3 | 2004-04-18 21:13:41 +0000 | [diff] [blame] | 103 | } |
| 104 | |
| 105 | void serial_setbrg(void) |
| 106 | { |
| 107 | /* FIXME: what's this for? */ |
| 108 | } |
| 109 | |
| 110 | void serial_putc(const char c) |
| 111 | { |
Michal Simek | 49a23e4 | 2011-09-25 21:03:08 +0000 | [diff] [blame] | 112 | uartlite_serial_putc(c, 0); |
wdenk | 507bbe3 | 2004-04-18 21:13:41 +0000 | [diff] [blame] | 113 | } |
| 114 | |
Michal Simek | 49a23e4 | 2011-09-25 21:03:08 +0000 | [diff] [blame] | 115 | void serial_puts(const char *s) |
wdenk | 507bbe3 | 2004-04-18 21:13:41 +0000 | [diff] [blame] | 116 | { |
Michal Simek | 49a23e4 | 2011-09-25 21:03:08 +0000 | [diff] [blame] | 117 | uartlite_serial_puts(s, 0); |
wdenk | 507bbe3 | 2004-04-18 21:13:41 +0000 | [diff] [blame] | 118 | } |
| 119 | |
| 120 | int serial_getc(void) |
| 121 | { |
Michal Simek | 49a23e4 | 2011-09-25 21:03:08 +0000 | [diff] [blame] | 122 | return uartlite_serial_getc(0); |
wdenk | 507bbe3 | 2004-04-18 21:13:41 +0000 | [diff] [blame] | 123 | } |
| 124 | |
| 125 | int serial_tstc(void) |
| 126 | { |
Michal Simek | 49a23e4 | 2011-09-25 21:03:08 +0000 | [diff] [blame] | 127 | return uartlite_serial_tstc(0); |
wdenk | 507bbe3 | 2004-04-18 21:13:41 +0000 | [diff] [blame] | 128 | } |
Michal Simek | 49a23e4 | 2011-09-25 21:03:08 +0000 | [diff] [blame] | 129 | #endif |
| 130 | |
| 131 | #if defined(CONFIG_SERIAL_MULTI) |
| 132 | /* Multi serial device functions */ |
| 133 | #define DECLARE_ESERIAL_FUNCTIONS(port) \ |
| 134 | int userial##port##_init(void) \ |
Michal Simek | 25239e1 | 2012-07-02 10:32:18 +0200 | [diff] [blame] | 135 | { return uartlite_serial_init(port); } \ |
Michal Simek | 49a23e4 | 2011-09-25 21:03:08 +0000 | [diff] [blame] | 136 | void userial##port##_setbrg(void) {} \ |
| 137 | int userial##port##_getc(void) \ |
| 138 | { return uartlite_serial_getc(port); } \ |
| 139 | int userial##port##_tstc(void) \ |
| 140 | { return uartlite_serial_tstc(port); } \ |
| 141 | void userial##port##_putc(const char c) \ |
| 142 | { uartlite_serial_putc(c, port); } \ |
| 143 | void userial##port##_puts(const char *s) \ |
| 144 | { uartlite_serial_puts(s, port); } |
| 145 | |
| 146 | /* Serial device descriptor */ |
Marek Vasut | 90bad89 | 2012-09-09 18:48:28 +0200 | [diff] [blame] | 147 | #define INIT_ESERIAL_STRUCTURE(port, __name) { \ |
| 148 | .name = __name, \ |
| 149 | .start = userial##port##_init, \ |
| 150 | .stop = NULL, \ |
| 151 | .setbrg = userial##port##_setbrg, \ |
| 152 | .getc = userial##port##_getc, \ |
| 153 | .tstc = userial##port##_tstc, \ |
| 154 | .putc = userial##port##_putc, \ |
| 155 | .puts = userial##port##_puts, \ |
| 156 | } |
Michal Simek | 49a23e4 | 2011-09-25 21:03:08 +0000 | [diff] [blame] | 157 | |
| 158 | DECLARE_ESERIAL_FUNCTIONS(0); |
| 159 | struct serial_device uartlite_serial0_device = |
| 160 | INIT_ESERIAL_STRUCTURE(0, "ttyUL0"); |
| 161 | DECLARE_ESERIAL_FUNCTIONS(1); |
| 162 | struct serial_device uartlite_serial1_device = |
| 163 | INIT_ESERIAL_STRUCTURE(1, "ttyUL1"); |
| 164 | DECLARE_ESERIAL_FUNCTIONS(2); |
| 165 | struct serial_device uartlite_serial2_device = |
| 166 | INIT_ESERIAL_STRUCTURE(2, "ttyUL2"); |
| 167 | DECLARE_ESERIAL_FUNCTIONS(3); |
| 168 | struct serial_device uartlite_serial3_device = |
| 169 | INIT_ESERIAL_STRUCTURE(3, "ttyUL3"); |
| 170 | |
| 171 | __weak struct serial_device *default_serial_console(void) |
| 172 | { |
Michal Simek | 25239e1 | 2012-07-02 10:32:18 +0200 | [diff] [blame] | 173 | if (userial_ports[0]) |
| 174 | return &uartlite_serial0_device; |
| 175 | if (userial_ports[1]) |
| 176 | return &uartlite_serial1_device; |
| 177 | if (userial_ports[2]) |
| 178 | return &uartlite_serial2_device; |
| 179 | if (userial_ports[3]) |
| 180 | return &uartlite_serial3_device; |
| 181 | |
| 182 | return NULL; |
Michal Simek | 49a23e4 | 2011-09-25 21:03:08 +0000 | [diff] [blame] | 183 | } |
| 184 | #endif /* CONFIG_SERIAL_MULTI */ |