From: Chao Liu The Rock 5B machine runs a vendor U-Boot and Linux that read the UART status register to check the transmitter and FIFO state. Implement the register. Signed-off-by: Chao Liu Signed-off-by: Yifan Wang --- hw/char/dw8250.c | 35 +++++++++++++++++++++++++++++++++++ hw/char/serial.c | 21 --------------------- include/hw/char/serial.h | 25 +++++++++++++++++++++++++ 3 files changed, 60 insertions(+), 21 deletions(-) diff --git a/hw/char/dw8250.c b/hw/char/dw8250.c index 50baeae62c..80ace7f7a7 100644 --- a/hw/char/dw8250.c +++ b/hw/char/dw8250.c @@ -6,6 +6,7 @@ */ #include "qemu/osdep.h" +#include "qemu/bitops.h" #include "qemu/log.h" #include "qapi/error.h" #include "hw/char/dw8250.h" @@ -14,20 +15,54 @@ #define DW_UART_REGION_SIZE 0x100 +#define DW_UART_USR (0x7C >> 2) /* UART Status Register */ #define DW_UART_RE_EN (0xB4 >> 2) /* Receiver Output Enable Register */ #define DW_UART_DLF (0xC0 >> 2) /* Divisor Latch Fraction Register */ #define DW_UART_CPR (0xF4 >> 2) /* Component Parameter Register */ #define DW_UART_UCV (0xF8 >> 2) /* UART Component Version */ #define DW_UART_CTR (0xFC >> 2) /* Component Type Register */ +/* UART Status Register bits */ +#define DW_UART_USR_BUSY BIT(0) /* UART busy */ +#define DW_UART_USR_TFNF BIT(1) /* Transmit FIFO not full */ +#define DW_UART_USR_TFE BIT(2) /* Transmit FIFO empty */ +#define DW_UART_USR_RFNE BIT(3) /* Receive FIFO not empty */ +#define DW_UART_USR_RFF BIT(4) /* Receive FIFO full */ + #define DW_UART_UCV_VALUE 0x3332332A /* "323*" -> v3.23a */ #define DW_UART_CTR_VALUE 0x44570110 /* "DW" */ static uint64_t dw8250_ext_read(void *opaque, hwaddr addr, unsigned int size) { DW8250State *s = opaque; + SerialState *serial = &s->serial_mm.serial; switch (addr >> s->regshift) { + case DW_UART_USR: { + bool fifo_enabled = serial->fcr & UART_FCR_FE; + bool thr_empty = serial->lsr & UART_LSR_THRE; + bool tx_idle = serial->lsr & UART_LSR_TEMT; + bool rx_ready = serial->lsr & UART_LSR_DR; + bool tx_empty; + bool tx_full; + bool rx_full; + + if (fifo_enabled) { + tx_empty = fifo8_is_empty(&serial->xmit_fifo); + tx_full = fifo8_is_full(&serial->xmit_fifo); + rx_full = fifo8_is_full(&serial->recv_fifo); + } else { + tx_empty = thr_empty; + tx_full = !thr_empty; + rx_full = rx_ready; + } + + return (!tx_idle ? DW_UART_USR_BUSY : 0) | + (!tx_full ? DW_UART_USR_TFNF : 0) | + (tx_empty ? DW_UART_USR_TFE : 0) | + (rx_ready ? DW_UART_USR_RFNE : 0) | + (rx_full ? DW_UART_USR_RFF : 0); + } case DW_UART_UCV: return DW_UART_UCV_VALUE; case DW_UART_CPR: diff --git a/hw/char/serial.c b/hw/char/serial.c index fefd2cf9fc..0a9821945c 100644 --- a/hw/char/serial.c +++ b/hw/char/serial.c @@ -84,27 +84,6 @@ #define UART_MSR_DCTS 0x01 /* Delta CTS */ #define UART_MSR_ANY_DELTA 0x0F /* Any of the delta bits! */ -#define UART_LSR_TEMT 0x40 /* Transmitter empty */ -#define UART_LSR_THRE 0x20 /* Transmit-hold-register empty */ -#define UART_LSR_BI 0x10 /* Break interrupt indicator */ -#define UART_LSR_FE 0x08 /* Frame error indicator */ -#define UART_LSR_PE 0x04 /* Parity error indicator */ -#define UART_LSR_OE 0x02 /* Overrun error indicator */ -#define UART_LSR_DR 0x01 /* Receiver data ready */ -#define UART_LSR_INT_ANY 0x1E /* Any of the lsr-interrupt-triggering status bits */ - -/* Interrupt trigger levels. The byte-counts are for 16550A - in newer UARTs the byte-count for each ITL is higher. */ - -#define UART_FCR_ITL_1 0x00 /* 1 byte ITL */ -#define UART_FCR_ITL_2 0x40 /* 4 bytes ITL */ -#define UART_FCR_ITL_3 0x80 /* 8 bytes ITL */ -#define UART_FCR_ITL_4 0xC0 /* 14 bytes ITL */ - -#define UART_FCR_DMS 0x08 /* DMA Mode Select */ -#define UART_FCR_XFR 0x04 /* XMIT Fifo Reset */ -#define UART_FCR_RFR 0x02 /* RCVR Fifo Reset */ -#define UART_FCR_FE 0x01 /* FIFO Enable */ - #define MAX_XMIT_RETRY 4 static void serial_receive1(void *opaque, const uint8_t *buf, int size); diff --git a/include/hw/char/serial.h b/include/hw/char/serial.h index ea82ffac47..6bb6dad980 100644 --- a/include/hw/char/serial.h +++ b/include/hw/char/serial.h @@ -34,6 +34,31 @@ #define UART_FIFO_LENGTH 16 /* 16550A Fifo Length */ +#define UART_LSR_TEMT 0x40 /* Transmitter empty */ +#define UART_LSR_THRE 0x20 /* Transmit-hold-register empty */ +#define UART_LSR_BI 0x10 /* Break interrupt indicator */ +#define UART_LSR_FE 0x08 /* Frame error indicator */ +#define UART_LSR_PE 0x04 /* Parity error indicator */ +#define UART_LSR_OE 0x02 /* Overrun error indicator */ +#define UART_LSR_DR 0x01 /* Receiver data ready */ +/* Any of the lsr-interrupt-triggering status bits */ +#define UART_LSR_INT_ANY 0x1E + +/* + * Interrupt trigger levels. The byte-counts are for 16550A - in newer UARTs + * the byte-count for each ITL is higher. + */ + +#define UART_FCR_ITL_1 0x00 /* 1 byte ITL */ +#define UART_FCR_ITL_2 0x40 /* 4 bytes ITL */ +#define UART_FCR_ITL_3 0x80 /* 8 bytes ITL */ +#define UART_FCR_ITL_4 0xC0 /* 14 bytes ITL */ + +#define UART_FCR_DMS 0x08 /* DMA Mode Select */ +#define UART_FCR_XFR 0x04 /* XMIT Fifo Reset */ +#define UART_FCR_RFR 0x02 /* RCVR Fifo Reset */ +#define UART_FCR_FE 0x01 /* FIFO Enable */ + struct SerialState { DeviceState parent; -- 2.53.0