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