From: Chao Liu <[email protected]>

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 <[email protected]>
Signed-off-by: Yifan Wang <[email protected]>
---
 hw/char/dw8250.c | 43 +++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 43 insertions(+)

diff --git a/hw/char/dw8250.c b/hw/char/dw8250.c
index 50baeae62c..4f9a8ea989 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,62 @@
 
 #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 */
+
+/* FIFO Control Register bits */
+#define DW_UART_FCR_FIFO_EN BIT(0) /* FIFO enable */
+
+/* Line Status Register bits */
+#define DW_UART_LSR_DR   BIT(0) /* Receiver data ready */
+#define DW_UART_LSR_THRE BIT(5) /* Transmitter holding register empty */
+#define DW_UART_LSR_TEMT BIT(6) /* Transmitter empty */
+
 #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 & DW_UART_FCR_FIFO_EN;
+        bool thr_empty = serial->lsr & DW_UART_LSR_THRE;
+        bool tx_idle = serial->lsr & DW_UART_LSR_TEMT;
+        bool rx_ready = serial->lsr & DW_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:
-- 
2.53.0


Reply via email to