| 1 | /* SPDX-License-Identifier: GPL-2.0-or-later */ |
| 2 | /** |
| 3 | ****************************************************************************** |
| 4 | * @file uart.h |
| 5 | * @author StarFive Technology |
| 6 | * @version V1.0 |
| 7 | * @date 07/24/2020 |
| 8 | * @brief |
| 9 | ****************************************************************************** |
| 10 | * @copy |
| 11 | * |
| 12 | * THE PRESENT SOFTWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS |
| 13 | * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE |
| 14 | * TIME. AS A RESULT, STARFIVE SHALL NOT BE HELD LIABLE FOR ANY |
| 15 | * DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING |
| 16 | * FROM THE CONTENT OF SUCH SOFTWARE AND/OR THE USE MADE BY CUSTOMERS OF THE |
| 17 | * CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS. |
| 18 | * |
| 19 | * COPYRIGHT 2020 Shanghai StarFive Technology Co., Ltd. |
| 20 | */ |
| 21 | |
| 22 | |
| 23 | #ifndef _UART_H |
| 24 | #define _UART_H |
| 25 | #include <sys.h> |
| 26 | |
| 27 | #define UART_REG_ADDR_INTERVAL 4 |
| 28 | #define REG_THR 0x00 /* Transmitter holding reg. */ |
| 29 | #define REG_RDR 0x00 /* Receiver data reg. */ |
| 30 | #define REG_BRDL 0x00 /* Baud rate divisor (LSB) */ |
| 31 | #define REG_BRDH 0x01 /* Baud rate divisor (MSB) */ |
| 32 | #define REG_IER 0x01 /* Interrupt enable reg. */ |
| 33 | #define REG_IIR 0x02 /* Interrupt ID reg. */ |
| 34 | #define REG_FCR 0x02 /* FIFO control reg. */ |
| 35 | #define REG_LCR 0x03 /* Line control reg. */ |
| 36 | #define REG_MDC 0x04 /* Modem control reg. */ |
| 37 | #define REG_LSR 0x05 /* Line status reg. */ |
| 38 | #define REG_MSR 0x06 /* Modem status reg. */ |
| 39 | #define REG_DLF 0xC0 /* Divisor Latch Fraction */ |
| 40 | |
| 41 | |
| 42 | |
| 43 | #define UART_USR 31 |
| 44 | #define UART_USR_BUSY (1 << 0) /* UART is busy (1) */ |
| 45 | #define UART_USR_Tx_FIFO_NFUL (1 << 1) /* Tx FIFO is not full (1) */ |
| 46 | #define UART_USR_Tx_FIFO_NEMP (1 << 2) /* Tx FIFO is empty (1) */ |
| 47 | #define UART_USR_Rx_FIFO_NHFL (1 << 3) /* Rx FIFO is not empty (1) */ |
| 48 | #define UART_USR_Rx_FIFO_NFUL (1 << 4) /* Rx FIFO is full (1) */ |
| 49 | |
| 50 | |
| 51 | /* equates for interrupt enable register */ |
| 52 | |
| 53 | #define IER_RXRDY 0x01 /* receiver data ready */ |
| 54 | #define IER_TBE 0x02 /* transmit bit enable */ |
| 55 | #define IER_LSR 0x04 /* line status interrupts */ |
| 56 | #define IER_MSI 0x08 /* modem status interrupts */ |
| 57 | |
| 58 | |
| 59 | /* constants for line control register */ |
| 60 | |
| 61 | #define LCR_CS5 0x00 /* 5 bits data size */ |
| 62 | #define LCR_CS6 0x01 /* 6 bits data size */ |
| 63 | #define LCR_CS7 0x02 /* 7 bits data size */ |
| 64 | #define LCR_CS8 0x03 /* 8 bits data size */ |
| 65 | #define LCR_2_STB 0x04 /* 2 stop bits */ |
| 66 | #define LCR_1_STB 0x00 /* 1 stop bit */ |
| 67 | #define LCR_PEN 0x08 /* parity enable */ |
| 68 | #define LCR_PDIS 0x00 /* parity disable */ |
| 69 | #define LCR_EPS 0x10 /* even parity select */ |
| 70 | #define LCR_SP 0x20 /* stick parity select */ |
| 71 | #define LCR_SBRK 0x40 /* break control bit */ |
| 72 | #define LCR_DLAB 0x80 /* divisor latch access enable */ |
| 73 | |
| 74 | /* constants for line status register */ |
| 75 | |
| 76 | #define LSR_RXRDY 0x01 /* receiver data available */ |
| 77 | #define LSR_OE 0x02 /* overrun error */ |
| 78 | #define LSR_PE 0x04 /* parity error */ |
| 79 | #define LSR_FE 0x08 /* framing error */ |
| 80 | #define LSR_BI 0x10 /* break interrupt */ |
| 81 | #define LSR_EOB_MASK 0x1E /* Error or Break mask */ |
| 82 | #define LSR_THRE 0x20 /* transmit holding register empty */ |
| 83 | #define LSR_TEMT 0x40 /* transmitter empty */ |
| 84 | |
| 85 | /* equates for FIFO control register */ |
| 86 | |
| 87 | #define FCR_FIFO 0x01 /* enable XMIT and RCVR FIFO */ |
| 88 | #define FCR_RCVRCLR 0x02 /* clear RCVR FIFO */ |
| 89 | #define FCR_XMITCLR 0x04 /* clear XMIT FIFO */ |
| 90 | |
| 91 | /* |
| 92 | * Per PC16550D (Literature Number: SNLS378B): |
| 93 | * |
| 94 | * RXRDY, Mode 0: When in the 16450 Mode (FCR0 = 0) or in |
| 95 | * the FIFO Mode (FCR0 = 1, FCR3 = 0) and there is at least 1 |
| 96 | * character in the RCVR FIFO or RCVR holding register, the |
| 97 | * RXRDY pin (29) will be low active. Once it is activated the |
| 98 | * RXRDY pin will go inactive when there are no more charac- |
| 99 | * ters in the FIFO or holding register. |
| 100 | * |
| 101 | * RXRDY, Mode 1: In the FIFO Mode (FCR0 = 1) when the |
| 102 | * FCR3 = 1 and the trigger level or the timeout has been |
| 103 | * reached, the RXRDY pin will go low active. Once it is acti- |
| 104 | * vated it will go inactive when there are no more characters |
| 105 | * in the FIFO or holding register. |
| 106 | * |
| 107 | * TXRDY, Mode 0: In the 16450 Mode (FCR0 = 0) or in the |
| 108 | * FIFO Mode (FCR0 = 1, FCR3 = 0) and there are no charac- |
| 109 | * ters in the XMIT FIFO or XMIT holding register, the TXRDY |
| 110 | * pin (24) will be low active. Once it is activated the TXRDY |
| 111 | * pin will go inactive after the first character is loaded into the |
| 112 | * XMIT FIFO or holding register. |
| 113 | * |
| 114 | * TXRDY, Mode 1: In the FIFO Mode (FCR0 = 1) when |
| 115 | * FCR3 = 1 and there are no characters in the XMIT FIFO, the |
| 116 | * TXRDY pin will go low active. This pin will become inactive |
| 117 | * when the XMIT FIFO is completely full. |
| 118 | */ |
| 119 | #define FCR_MODE0 0x00 /* set receiver in mode 0 */ |
| 120 | #define FCR_MODE1 0x08 /* set receiver in mode 1 */ |
| 121 | |
| 122 | /* RCVR FIFO interrupt levels: trigger interrupt with this bytes in FIFO */ |
| 123 | #define FCR_FIFO_1 0x00 /* 1 byte in RCVR FIFO */ |
| 124 | #define FCR_FIFO_4 0x40 /* 4 bytes in RCVR FIFO */ |
| 125 | #define FCR_FIFO_8 0x80 /* 8 bytes in RCVR FIFO */ |
| 126 | #define FCR_FIFO_14 0xC0 /* 14 bytes in RCVR FIFO */ |
| 127 | |
| 128 | /* |
| 129 | * UART NS16750 supports 64 bytes FIFO, which can be enabled |
| 130 | * via the FCR register |
| 131 | */ |
| 132 | #define FCR_FIFO_64 0x20 /* Enable 64 bytes FIFO */ |
| 133 | |
| 134 | /* constants for line control register */ |
| 135 | |
| 136 | #define LCR_CS5 0x00 /* 5 bits data size */ |
| 137 | #define LCR_CS6 0x01 /* 6 bits data size */ |
| 138 | #define LCR_CS7 0x02 /* 7 bits data size */ |
| 139 | #define LCR_CS8 0x03 /* 8 bits data size */ |
| 140 | #define LCR_2_STB 0x04 /* 2 stop bits */ |
| 141 | #define LCR_1_STB 0x00 /* 1 stop bit */ |
| 142 | #define LCR_PEN 0x08 /* parity enable */ |
| 143 | #define LCR_PDIS 0x00 /* parity disable */ |
| 144 | #define LCR_EPS 0x10 /* even parity select */ |
| 145 | #define LCR_SP 0x20 /* stick parity select */ |
| 146 | #define LCR_SBRK 0x40 /* break control bit */ |
| 147 | #define LCR_DLAB 0x80 /* divisor latch access enable */ |
| 148 | |
| 149 | /* constants for the modem control register */ |
| 150 | |
| 151 | #define MCR_DTR 0x01 /* dtr output */ |
| 152 | #define MCR_RTS 0x02 /* rts output */ |
| 153 | #define MCR_OUT1 0x04 /* output #1 */ |
| 154 | #define MCR_OUT2 0x08 /* output #2 */ |
| 155 | #define MCR_LOOP 0x10 /* loop back */ |
| 156 | #define MCR_AFCE 0x20 /* auto flow control enable */ |
| 157 | |
| 158 | /* constants for line status register */ |
| 159 | |
| 160 | #define LSR_RXRDY 0x01 /* receiver data available */ |
| 161 | #define LSR_OE 0x02 /* overrun error */ |
| 162 | #define LSR_PE 0x04 /* parity error */ |
| 163 | #define LSR_FE 0x08 /* framing error */ |
| 164 | #define LSR_BI 0x10 /* break interrupt */ |
| 165 | #define LSR_EOB_MASK 0x1E /* Error or Break mask */ |
| 166 | #define LSR_THRE 0x20 /* transmit holding register empty */ |
| 167 | #define LSR_TEMT 0x40 /* transmitter empty */ |
| 168 | |
| 169 | /* constants for modem status register */ |
| 170 | |
| 171 | #define MSR_DCTS 0x01 /* cts change */ |
| 172 | #define MSR_DDSR 0x02 /* dsr change */ |
| 173 | #define MSR_DRI 0x04 /* ring change */ |
| 174 | #define MSR_DDCD 0x08 /* data carrier change */ |
| 175 | #define MSR_CTS 0x10 /* complement of cts */ |
| 176 | #define MSR_DSR 0x20 /* complement of dsr */ |
| 177 | #define MSR_RI 0x40 /* complement of ring signal */ |
| 178 | #define MSR_DCD 0x80 /* complement of dcd */ |
| 179 | |
| 180 | #define UART_THR_REG (REG_THR * UART_REG_ADDR_INTERVAL) |
| 181 | #define UART_RDR_REG (REG_RDR * UART_REG_ADDR_INTERVAL) |
| 182 | #define UART_BRDL_REG (REG_BRDL * UART_REG_ADDR_INTERVAL) |
| 183 | #define UART_BRDH_REG (REG_BRDH * UART_REG_ADDR_INTERVAL) |
| 184 | #define UART_IER_REG (REG_IER * UART_REG_ADDR_INTERVAL) |
| 185 | #define UART_IIR_REG (REG_IIR * UART_REG_ADDR_INTERVAL) |
| 186 | #define UART_FCR_REG (REG_FCR * UART_REG_ADDR_INTERVAL) |
| 187 | #define UART_LCR_REG (REG_LCR * UART_REG_ADDR_INTERVAL) |
| 188 | #define UART_MDC_REG (REG_MDC * UART_REG_ADDR_INTERVAL) |
| 189 | #define UART_LSR_REG (REG_LSR * UART_REG_ADDR_INTERVAL) |
| 190 | #define UART_MSR_REG (REG_MSR * UART_REG_ADDR_INTERVAL) |
| 191 | |
| 192 | |
| 193 | extern void uart_init(); |
| 194 | extern int _putc(char c); |
| 195 | |
| 196 | extern void rlSendString(char *s); |
| 197 | extern int serial_getc(); |
| 198 | extern int CtrlBreak( void ); |
| 199 | extern int serial_nowait_getc(); |
| 200 | extern void print_ubyte_hex(unsigned char bval); |
| 201 | |
| 202 | #endif // _UART_H |