Skip to contentSkip to conversation

Update opentitan uart (part of supporting opentitan version 1)

PATCH In review ~lexbaileylowrisc 3 participants 19 messages Apr 30 – Jun 1

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

v4 cover Update opentitan uart (part of supporting opentitan version 1)

This is the fourth revision of the patch set that updates the OpenTitan uart to align with Earlgrey version 1.0.0.

For a specific git commit from OpenTitan that we are aiming to support:
https://github.com/lowRISC/opentitan/tree/Earlgrey-PROD-M6
(git tag Earlgrey-PROD-M6, which is commit a78922f)

Once again, for completeness, the details of the version 1 of the UART are found here:
https://opentitan.org/book/hw/ip/uart/index.html
(and this URL is still added to the file header in one of these commits, as before)

Fixes in this revision:
- removed accidental add/delete of unused #define.
- added documentation about break condition in commit message and also in a documentation file (is it in a suitable place?) this is copied from the documentation on our (lowRISC's) fork.
- removed the commit that removes the regular clock pulse to transmit data. Following the discussion, I think it's better to merge this without that change for now, and if we decide we need it in the future we will test it with tock and propose the change again.
- one review comment I couldn't fix: the one that suggested adding a return statement to avoid an else block. There is logic after the else block that runs in both cases, so I can't remove that.

Thanks!

Lex.

Lex Bailey (9):
Rename ibex_uart to ot_uart
ot_uart: move to new reset API
ot_uart: update register defs, switch to Fifo8 for tx/rx buffers
ot_uart: replace individual IRQ fields with array, add missing IRQs
ot_uart: gather similar behaviours togeter in register read and write
ot_uart: implement RX fifo and loopback
ot_uart: handle break condition
ot_uart: update file headers with new authorship and documentation URL
ot_uart: add tracing

 MAINTAINERS                          |   1 +
 docs/system/riscv/opentitan-uart.rst |  43 ++
 hw/char/Kconfig                      |   3 +
 hw/char/ibex_uart.c                  | 569 --------------------
 hw/char/meson.build                  |   2 +-
 hw/char/ot_uart.c                    | 778 +++++++++++++++++++++++++++
 hw/char/trace-events                 |   8 +
 hw/riscv/Kconfig                     |   1 +
 hw/riscv/opentitan.c                 |   3 +-
 include/hw/char/ibex_uart.h          |  73 ---
 include/hw/char/ot_uart.h            |  70 +++
 include/hw/riscv/opentitan.h         |   4 +-
 12 files changed, 909 insertions(+), 646 deletions(-)
 create mode 100644 docs/system/riscv/opentitan-uart.rst
 delete mode 100644 hw/char/ibex_uart.c
 create mode 100644 hw/char/ot_uart.c
 delete mode 100644 include/hw/char/ibex_uart.h
 create mode 100644 include/hw/char/ot_uart.h
Signature · 1 line
2.49.1
#1 lore raw

PATCH v4 1/9 Rename ibex_uart to ot_uart

From: Lex Bailey <[email protected]>

This UART device is not specifically tied to ibex. Currently it is only used in the opentitan machine, which does use an ibex core, but an opentitan machine with a different core would still use this UART, hence it should be considered a part of opentitan more than a part of ibex.

---
 MAINTAINERS                                |   1 +
 hw/char/Kconfig                            |   3 +
 hw/char/meson.build                        |   2 +-
 hw/char/{ibex_uart.c => ot_uart.c}         | 177 ++++++++++-----------
 hw/riscv/Kconfig                           |   1 +
 hw/riscv/opentitan.c                       |   2 +-
 include/hw/char/{ibex_uart.h => ot_uart.h} |  20 +--
 include/hw/riscv/opentitan.h               |   4 +-
 8 files changed, 107 insertions(+), 103 deletions(-)
 rename hw/char/{ibex_uart.c => ot_uart.c} (75%)
 rename include/hw/char/{ibex_uart.h => ot_uart.h} (84%)
MAINTAINERS +1 -0
index e41f0eb92c..42abaa1ee3 100644
@@ -1727,6 +1727,7 @@ L: [email protected]
 S: Supported
 F: hw/riscv/opentitan.c
 F: hw/*/ibex_*.c
+F: hw/*/ot_*.c
 F: include/hw/riscv/opentitan.h
 F: include/hw/*/ibex_*.h
 F: target/riscv/insn_trans/trans_xthead.c.inc
hw/char/Kconfig +3 -0
index 020c0a84bb..23ddab6b78 100644
@@ -95,3 +95,6 @@ config IP_OCTAL_232
     bool
     default y
     depends on IPACK
+
+config OT_UART
+    bool
hw/char/meson.build +1 -1
index fc3d7ee506..056153085f 100644
@@ -2,7 +2,7 @@ system_ss.add(when: 'CONFIG_CADENCE', if_true: files('cadence_uart.c'))
 system_ss.add(when: 'CONFIG_CMSDK_APB_UART', if_true: files('cmsdk-apb-uart.c'))
 system_ss.add(when: 'CONFIG_ESCC', if_true: files('escc.c'))
 system_ss.add(when: 'CONFIG_GRLIB', if_true: files('grlib_apbuart.c'))
-system_ss.add(when: 'CONFIG_IBEX', if_true: files('ibex_uart.c'))
+system_ss.add(when: 'CONFIG_OT_UART', if_true: files('ot_uart.c'))
 system_ss.add(when: 'CONFIG_IMX', if_true: files('imx_serial.c'))
 system_ss.add(when: 'CONFIG_IP_OCTAL_232', if_true: files('ipoctal232.c'))
 system_ss.add(when: 'CONFIG_ISA_BUS', if_true: files('parallel-isa.c'))
hw/char/ot_uart.c renamed from hw/char/ibex_uart.c +88 -89

372 lines not on this page · raw message

hw/riscv/Kconfig +1 -0
index 0222c93f87..cb4bb5f442 100644
@@ -38,6 +38,7 @@ config OPENTITAN
     default y
     depends on RISCV32
     select IBEX
+    select OT_UART
     select SIFIVE_PLIC
     select UNIMP
 
hw/riscv/opentitan.c +1 -1
index 309125e854..97c33d1b53 100644
@@ -132,7 +132,7 @@ static void lowrisc_ibex_soc_init(Object *obj)
 
     object_initialize_child(obj, "plic", &s->plic, TYPE_SIFIVE_PLIC);
 
-    object_initialize_child(obj, "uart", &s->uart, TYPE_IBEX_UART);
+    object_initialize_child(obj, "uart", &s->uart, TYPE_OT_UART);
 
     object_initialize_child(obj, "timer", &s->timer, TYPE_IBEX_TIMER);
 
include/hw/char/ot_uart.h renamed from include/hw/char/ibex_uart.h +10 -10
similarity index 84%
rename from include/hw/char/ibex_uart.h
rename to include/hw/char/ot_uart.h
index 882796e0c6..517324b65d 100644
@@ -1,5 +1,5 @@
 /*
- * QEMU lowRISC Ibex UART device
+ * QEMU OpenTitan UART device
  *
  * Copyright (c) 2020 Western Digital
  *
@@ -22,28 +22,28 @@
  * THE SOFTWARE.
  */
 
-#ifndef HW_IBEX_UART_H
-#define HW_IBEX_UART_H
+#ifndef HW_OT_UART_H
+#define HW_OT_UART_H
 
 #include "hw/core/sysbus.h"
 #include "chardev/char-fe.h"
 #include "qemu/timer.h"
 #include "qom/object.h"
 
-#define IBEX_UART_TX_FIFO_SIZE 16
-#define IBEX_UART_CLOCK 50000000 /* 50MHz clock */
+#define OT_UART_TX_FIFO_SIZE 16
+#define OT_UART_CLOCK 50000000 /* 50MHz clock */
 
-#define TYPE_IBEX_UART "ibex-uart"
-OBJECT_DECLARE_SIMPLE_TYPE(IbexUartState, IBEX_UART)
+#define TYPE_OT_UART "ot-uart"
+OBJECT_DECLARE_SIMPLE_TYPE(OtUARTState, OT_UART)
 
-struct IbexUartState {
+struct OtUARTState {
     /* <private> */
     SysBusDevice parent_obj;
 
     /* <public> */
     MemoryRegion mmio;
 
-    uint8_t tx_fifo[IBEX_UART_TX_FIFO_SIZE];
+    uint8_t tx_fifo[OT_UART_TX_FIFO_SIZE];
     uint32_t tx_level;
 
     uint32_t rx_level;
@@ -70,4 +70,4 @@ struct IbexUartState {
     qemu_irq tx_empty;
     qemu_irq rx_overflow;
 };
-#endif /* HW_IBEX_UART_H */
+#endif /* HW_OT_UART_H */
include/hw/riscv/opentitan.h +2 -2
index 5b9016e1d8..f2b4bf3814 100644
@@ -21,7 +21,7 @@
 
 #include "hw/riscv/riscv_hart.h"
 #include "hw/intc/sifive_plic.h"
-#include "hw/char/ibex_uart.h"
+#include "hw/char/ot_uart.h"
 #include "hw/timer/ibex_timer.h"
 #include "hw/ssi/ibex_spi_host.h"
 #include "hw/core/boards.h"
@@ -43,7 +43,7 @@ struct LowRISCIbexSoCState {
     /*< public >*/
     RISCVHartArrayState cpus;
     SiFivePLICState plic;
-    IbexUartState uart;
+    OtUARTState uart;
     IbexTimerState timer;
     IbexSPIHostState spi_host[OPENTITAN_NUM_SPI_HOSTS];
 
Signature · 1 line
2.49.1
#2 lore raw

PATCH v4 2/9 ot_uart: move to new reset API

In reply to ~lexbaileylowrisc

From: Lex Bailey <[email protected]>

switch from legacy_reset to new ResettableClass for OpenTitan UART device

---
 hw/char/ot_uart.c         | 16 +++++++++++++---
 include/hw/char/ot_uart.h |  8 +++++++-
 2 files changed, 20 insertions(+), 4 deletions(-)
hw/char/ot_uart.c +13 -3
index b3eb619641..9478af40f9 100644
@@ -227,9 +227,14 @@ static void uart_write_tx_fifo(OtUARTState *s, const uint8_t *buf,
               (s->char_tx_time * 4));
 }
 
-static void ot_uart_reset(DeviceState *dev)
+static void ot_uart_reset_enter(Object *obj, ResetType type)
 {
-    OtUARTState *s = OT_UART(dev);
+    OtUARTClass *c = OT_UART_GET_CLASS(obj);
+    OtUARTState *s = OT_UART(obj);
+
+    if (c->parent_phases.enter) {
+        c->parent_phases.enter(obj, type);
+    }
 
     s->uart_intr_state = 0x00000000;
     s->uart_intr_state = 0x00000000;
@@ -544,12 +549,16 @@ static void ot_uart_realize(DeviceState *dev, Error **errp)
 static void ot_uart_class_init(ObjectClass *klass, const void *data)
 {
     DeviceClass *dc = DEVICE_CLASS(klass);
+    OtUARTClass *uc = OT_UART_CLASS(klass);
+    ResettableClass *rc = RESETTABLE_CLASS(klass);
 
-    device_class_set_legacy_reset(dc, ot_uart_reset);
     dc->realize = ot_uart_realize;
     dc->vmsd = &vmstate_ot_uart;
     device_class_set_props(dc, ot_uart_properties);
     set_bit(DEVICE_CATEGORY_INPUT, dc->categories);
+
+    resettable_class_set_parent_phases(rc, &ot_uart_reset_enter, NULL, NULL,
+                                       &uc->parent_phases);
 }
 
 static const TypeInfo ot_uart_info = {
@@ -557,6 +566,7 @@ static const TypeInfo ot_uart_info = {
     .parent        = TYPE_SYS_BUS_DEVICE,
     .instance_size = sizeof(OtUARTState),
     .instance_init = ot_uart_init,
+    .class_size    = sizeof(OtUARTClass),
     .class_init    = ot_uart_class_init,
 };
 
include/hw/char/ot_uart.h +7 -1
index 517324b65d..fee2128f90 100644
@@ -34,7 +34,7 @@
 #define OT_UART_CLOCK 50000000 /* 50MHz clock */
 
 #define TYPE_OT_UART "ot-uart"
-OBJECT_DECLARE_SIMPLE_TYPE(OtUARTState, OT_UART)
+OBJECT_DECLARE_TYPE(OtUARTState, OtUARTClass, OT_UART)
 
 struct OtUARTState {
     /* <private> */
@@ -70,4 +70,10 @@ struct OtUARTState {
     qemu_irq tx_empty;
     qemu_irq rx_overflow;
 };
+
+struct OtUARTClass {
+    SysBusDeviceClass parent_class;
+    ResettablePhases parent_phases;
+};
+
 #endif /* HW_OT_UART_H */
Signature · 1 line
2.49.1
Reviewed-by on v4 2/9
#3 lore raw

Re: [PATCH qemu v4 2/9] ot_uart: move to new reset API

Quoted 6 lines · ~lexbaileylowrisc

From: Lex Bailey <[email protected]>

switch from legacy_reset to new ResettableClass for OpenTitan UART device

Alistair

Quoted 80 lines
---
 hw/char/ot_uart.c         | 16 +++++++++++++---
 include/hw/char/ot_uart.h |  8 +++++++-
 2 files changed, 20 insertions(+), 4 deletions(-)
hw/char/ot_uart.c +13 -3
index b3eb619641..9478af40f9 100644
@@ -227,9 +227,14 @@ static void uart_write_tx_fifo(OtUARTState *s, const uint8_t *buf,
               (s->char_tx_time * 4));
 }
 
-static void ot_uart_reset(DeviceState *dev)
+static void ot_uart_reset_enter(Object *obj, ResetType type)
 {
-    OtUARTState *s = OT_UART(dev);
+    OtUARTClass *c = OT_UART_GET_CLASS(obj);
+    OtUARTState *s = OT_UART(obj);
+
+    if (c->parent_phases.enter) {
+        c->parent_phases.enter(obj, type);
+    }
 
     s->uart_intr_state = 0x00000000;
     s->uart_intr_state = 0x00000000;
@@ -544,12 +549,16 @@ static void ot_uart_realize(DeviceState *dev, Error **errp)
 static void ot_uart_class_init(ObjectClass *klass, const void *data)
 {
     DeviceClass *dc = DEVICE_CLASS(klass);
+    OtUARTClass *uc = OT_UART_CLASS(klass);
+    ResettableClass *rc = RESETTABLE_CLASS(klass);
 
-    device_class_set_legacy_reset(dc, ot_uart_reset);
     dc->realize = ot_uart_realize;
     dc->vmsd = &vmstate_ot_uart;
     device_class_set_props(dc, ot_uart_properties);
     set_bit(DEVICE_CATEGORY_INPUT, dc->categories);
+
+    resettable_class_set_parent_phases(rc, &ot_uart_reset_enter, NULL, NULL,
+                                       &uc->parent_phases);
 }
 
 static const TypeInfo ot_uart_info = {
@@ -557,6 +566,7 @@ static const TypeInfo ot_uart_info = {
     .parent        = TYPE_SYS_BUS_DEVICE,
     .instance_size = sizeof(OtUARTState),
     .instance_init = ot_uart_init,
+    .class_size    = sizeof(OtUARTClass),
     .class_init    = ot_uart_class_init,
 };
 
include/hw/char/ot_uart.h +7 -2
index 517324b65d..fee2128f90 100644
@@ -34,7 +34,7 @@
 #define OT_UART_CLOCK 50000000 /* 50MHz clock */
 
 #define TYPE_OT_UART "ot-uart"
-OBJECT_DECLARE_SIMPLE_TYPE(OtUARTState, OT_UART)
+OBJECT_DECLARE_TYPE(OtUARTState, OtUARTClass, OT_UART)
 
 struct OtUARTState {
     /* <private> */
@@ -70,4 +70,10 @@ struct OtUARTState {
     qemu_irq tx_empty;
     qemu_irq rx_overflow;
 };
+
+struct OtUARTClass {
+    SysBusDeviceClass parent_class;
+    ResettablePhases parent_phases;
+};
+
 #endif /* HW_OT_UART_H */
--

2.49.1

#4 lore raw

Re: [PATCH qemu v4 2/9] ot_uart: move to new reset API

Hi Alistair,

thanks for reviewing this. I just want to check I haven't missed any messages from you, did you have a chance to look again at the other commits? are there any other changes you need? or is this ready to merge?

Thanks,

Lex.

Quoted 91 lines · Alistair Francis
Quoted 5 lines · ~lexbaileylowrisc

From: Lex Bailey<[email protected]>

switch from legacy_reset to new ResettableClass for OpenTitan UART device

Alistair

Quoted 80 lines
---
  hw/char/ot_uart.c         | 16 +++++++++++++---
  include/hw/char/ot_uart.h |  8 +++++++-
  2 files changed, 20 insertions(+), 4 deletions(-)
hw/char/ot_uart.c +13 -3
index b3eb619641..9478af40f9 100644
@@ -227,9 +227,14 @@ static void uart_write_tx_fifo(OtUARTState *s, const uint8_t *buf,
                (s->char_tx_time * 4));
  }
 
-static void ot_uart_reset(DeviceState *dev)
+static void ot_uart_reset_enter(Object *obj, ResetType type)
  {
-    OtUARTState *s = OT_UART(dev);
+    OtUARTClass *c = OT_UART_GET_CLASS(obj);
+    OtUARTState *s = OT_UART(obj);
+
+    if (c->parent_phases.enter) {
+        c->parent_phases.enter(obj, type);
+    }
 
      s->uart_intr_state = 0x00000000;
      s->uart_intr_state = 0x00000000;
@@ -544,12 +549,16 @@ static void ot_uart_realize(DeviceState *dev, Error **errp)
  static void ot_uart_class_init(ObjectClass *klass, const void *data)
  {
      DeviceClass *dc = DEVICE_CLASS(klass);
+    OtUARTClass *uc = OT_UART_CLASS(klass);
+    ResettableClass *rc = RESETTABLE_CLASS(klass);
 
-    device_class_set_legacy_reset(dc, ot_uart_reset);
      dc->realize = ot_uart_realize;
      dc->vmsd = &vmstate_ot_uart;
      device_class_set_props(dc, ot_uart_properties);
      set_bit(DEVICE_CATEGORY_INPUT, dc->categories);
+
+    resettable_class_set_parent_phases(rc, &ot_uart_reset_enter, NULL, NULL,
+                                       &uc->parent_phases);
  }
 
  static const TypeInfo ot_uart_info = {
@@ -557,6 +566,7 @@ static const TypeInfo ot_uart_info = {
      .parent        = TYPE_SYS_BUS_DEVICE,
      .instance_size = sizeof(OtUARTState),
      .instance_init = ot_uart_init,
+    .class_size    = sizeof(OtUARTClass),
      .class_init    = ot_uart_class_init,
  };
 
include/hw/char/ot_uart.h +7 -2
index 517324b65d..fee2128f90 100644
@@ -34,7 +34,7 @@
  #define OT_UART_CLOCK 50000000 /* 50MHz clock */
 
  #define TYPE_OT_UART "ot-uart"
-OBJECT_DECLARE_SIMPLE_TYPE(OtUARTState, OT_UART)
+OBJECT_DECLARE_TYPE(OtUARTState, OtUARTClass, OT_UART)
 
  struct OtUARTState {
      /* <private> */
@@ -70,4 +70,10 @@ struct OtUARTState {
      qemu_irq tx_empty;
      qemu_irq rx_overflow;
  };
+
+struct OtUARTClass {
+    SysBusDeviceClass parent_class;
+    ResettablePhases parent_phases;
+};
+
  #endif /* HW_OT_UART_H */
--

2.49.1

Signature · 2 lines
Lex Bailey (they/them)
lowRISC C.I.C. 7 Hills Road, Cambridge, CB2 1GE, UK. Registered in England & Wales 09272283 [email protected]
#5 lore raw

PATCH v4 3/9 ot_uart: update register defs, switch to Fifo8 for tx/rx buffers

In reply to ~lexbaileylowrisc

From: Lex Bailey <[email protected]>

The register definitions for the UART device need to be updated to match the documentation at https://opentitan.org/book/hw/ip/uart/doc/registers.html

This commit does that, and also switches to using Fifo8 for managing the transmit and receive buffers.

---
 hw/char/ot_uart.c         | 338 ++++++++++++++++++++++----------------
 include/hw/char/ot_uart.h |  18 +-
 2 files changed, 201 insertions(+), 155 deletions(-)
hw/char/ot_uart.c +195 -143
index 9478af40f9..3fe7c5efe5 100644
@@ -27,6 +27,7 @@
 
 #include "qemu/osdep.h"
 #include "hw/char/ot_uart.h"
+#include "qemu/fifo8.h"
 #include "hw/core/irq.h"
 #include "hw/core/qdev-clock.h"
 #include "hw/core/qdev-properties.h"
@@ -37,16 +38,22 @@
 #include "qemu/module.h"
 
 REG32(INTR_STATE, 0x00)
-    FIELD(INTR_STATE, TX_WATERMARK, 0, 1)
-    FIELD(INTR_STATE, RX_WATERMARK, 1, 1)
-    FIELD(INTR_STATE, TX_EMPTY, 2, 1)
-    FIELD(INTR_STATE, RX_OVERFLOW, 3, 1)
+    SHARED_FIELD(INTR_TX_WATERMARK, 0, 1)
+    SHARED_FIELD(INTR_RX_WATERMARK, 1, 1)
+    SHARED_FIELD(INTR_TX_DONE, 2, 1)
+    SHARED_FIELD(INTR_RX_OVERFLOW, 3, 1)
+    SHARED_FIELD(INTR_RX_FRAME_ERR, 4, 1)
+    SHARED_FIELD(INTR_RX_BREAK_ERR, 5, 1)
+    SHARED_FIELD(INTR_RX_TIMEOUT, 6, 1)
+    SHARED_FIELD(INTR_RX_PARITY_ERR, 7, 1)
+    SHARED_FIELD(INTR_TX_EMPTY, 8, 1)
 REG32(INTR_ENABLE, 0x04)
 REG32(INTR_TEST, 0x08)
 REG32(ALERT_TEST, 0x0C)
+    FIELD(ALERT_TEST, FATAL_FAULT, 0, 1)
 REG32(CTRL, 0x10)
-    FIELD(CTRL, TX_ENABLE, 0, 1)
-    FIELD(CTRL, RX_ENABLE, 1, 1)
+    FIELD(CTRL, TX, 0, 1)
+    FIELD(CTRL, RX, 1, 1)
     FIELD(CTRL, NF, 2, 1)
     FIELD(CTRL, SLPBK, 4, 1)
     FIELD(CTRL, LLPBK, 5, 1)
@@ -58,43 +65,74 @@ REG32(STATUS, 0x14)
     FIELD(STATUS, TXFULL, 0, 1)
     FIELD(STATUS, RXFULL, 1, 1)
     FIELD(STATUS, TXEMPTY, 2, 1)
+    FIELD(STATUS, TXIDLE, 3, 1)
     FIELD(STATUS, RXIDLE, 4, 1)
     FIELD(STATUS, RXEMPTY, 5, 1)
 REG32(RDATA, 0x18)
+    FIELD(RDATA, RDATA, 0, 8)
 REG32(WDATA, 0x1C)
+    FIELD(WDATA, WDATA, 0, 8)
 REG32(FIFO_CTRL, 0x20)
     FIELD(FIFO_CTRL, RXRST, 0, 1)
     FIELD(FIFO_CTRL, TXRST, 1, 1)
     FIELD(FIFO_CTRL, RXILVL, 2, 3)
-    FIELD(FIFO_CTRL, TXILVL, 5, 2)
+    FIELD(FIFO_CTRL, TXILVL, 5, 3)
 REG32(FIFO_STATUS, 0x24)
-    FIELD(FIFO_STATUS, TXLVL, 0, 5)
-    FIELD(FIFO_STATUS, RXLVL, 16, 5)
+    FIELD(FIFO_STATUS, TXLVL, 0, 8)
+    FIELD(FIFO_STATUS, RXLVL, 16, 8)
 REG32(OVRD, 0x28)
+    FIELD(OVRD, TXEN, 0, 1)
+    FIELD(OVRD, TXVAL, 1, 1)
 REG32(VAL, 0x2C)
+    FIELD(VAL, RX, 0, 16)
 REG32(TIMEOUT_CTRL, 0x30)
+    FIELD(TIMEOUT_CTRL, VAL, 0, 24)
+    FIELD(TIMEOUT_CTRL, EN, 31, 1)
+
+#define INTR_MASK \
+    (INTR_TX_WATERMARK_MASK | INTR_RX_WATERMARK_MASK | INTR_TX_DONE_MASK | \
+     INTR_RX_OVERFLOW_MASK | INTR_RX_FRAME_ERR_MASK | INTR_RX_BREAK_ERR_MASK | \
+     INTR_RX_TIMEOUT_MASK | INTR_RX_PARITY_ERR_MASK | INTR_TX_EMPTY_MASK)
+
+#define CTRL_MASK \
+    (R_CTRL_TX_MASK | R_CTRL_RX_MASK | R_CTRL_NF_MASK | R_CTRL_SLPBK_MASK | \
+     R_CTRL_LLPBK_MASK | R_CTRL_PARITY_EN_MASK | R_CTRL_PARITY_ODD_MASK | \
+     R_CTRL_RXBLVL_MASK | R_CTRL_NCO_MASK)
+
+#define OT_UART_NCO_BITS     16
+#define OT_UART_TX_FIFO_SIZE 128
+#define OT_UART_RX_FIFO_SIZE 128
+
+#define R32_OFF(_r_) ((_r_) / sizeof(uint32_t))
+
+#define R_LAST_REG (R_TIMEOUT_CTRL)
+#define REGS_COUNT (R_LAST_REG + 1u)
+#define REGS_SIZE  (REGS_COUNT * sizeof(uint32_t))
 
 static void ot_uart_update_irqs(OtUARTState *s)
 {
-    if (s->uart_intr_state & s->uart_intr_enable & R_INTR_STATE_TX_WATERMARK_MASK) {
+    if (s->regs[R_INTR_STATE] & s->regs[R_INTR_ENABLE]
+        & INTR_TX_WATERMARK_MASK) {
         qemu_set_irq(s->tx_watermark, 1);
     } else {
         qemu_set_irq(s->tx_watermark, 0);
     }
 
-    if (s->uart_intr_state & s->uart_intr_enable & R_INTR_STATE_RX_WATERMARK_MASK) {
+    if (s->regs[R_INTR_STATE] & s->regs[R_INTR_ENABLE]
+        & INTR_RX_WATERMARK_MASK) {
         qemu_set_irq(s->rx_watermark, 1);
     } else {
         qemu_set_irq(s->rx_watermark, 0);
     }
 
-    if (s->uart_intr_state & s->uart_intr_enable & R_INTR_STATE_TX_EMPTY_MASK) {
+    if (s->regs[R_INTR_STATE] & s->regs[R_INTR_ENABLE] & INTR_TX_EMPTY_MASK) {
         qemu_set_irq(s->tx_empty, 1);
     } else {
         qemu_set_irq(s->tx_empty, 0);
     }
 
-    if (s->uart_intr_state & s->uart_intr_enable & R_INTR_STATE_RX_OVERFLOW_MASK) {
+    if (s->regs[R_INTR_STATE] & s->regs[R_INTR_ENABLE]
+        & INTR_RX_OVERFLOW_MASK) {
         qemu_set_irq(s->rx_overflow, 1);
     } else {
         qemu_set_irq(s->rx_overflow, 0);
@@ -105,122 +143,133 @@ static int ot_uart_can_receive(void *opaque)
 {
     OtUARTState *s = opaque;
 
-    if ((s->uart_ctrl & R_CTRL_RX_ENABLE_MASK)
-           && !(s->uart_status & R_STATUS_RXFULL_MASK)) {
-        return 1;
+    if (s->regs[R_CTRL] & R_CTRL_RX_MASK) {
+        return (int)fifo8_num_free(&s->rx_fifo);
     }
 
     return 0;
 }
 
+static uint32_t ot_uart_get_rx_watermark_level(const OtUARTState *s)
+{
+    uint32_t rx_ilvl = FIELD_EX32(s->regs[R_FIFO_CTRL], FIFO_CTRL, RXILVL);
+
+    return rx_ilvl < 7 ? (1 << rx_ilvl) : 126;
+}
+
 static void ot_uart_receive(void *opaque, const uint8_t *buf, int size)
 {
     OtUARTState *s = opaque;
-    uint8_t rx_fifo_level = (s->uart_fifo_ctrl & R_FIFO_CTRL_RXILVL_MASK)
-                            >> R_FIFO_CTRL_RXILVL_SHIFT;
-
-    s->uart_rdata = *buf;
+    uint32_t rx_watermark_level;
+    size_t count = MIN(fifo8_num_free(&s->rx_fifo), (size_t)size);
 
-    s->uart_status &= ~R_STATUS_RXIDLE_MASK;
-    s->uart_status &= ~R_STATUS_RXEMPTY_MASK;
-    /* The RXFULL is set after receiving a single byte
-     * as the FIFO buffers are not yet implemented.
-     */
-    s->uart_status |= R_STATUS_RXFULL_MASK;
-    s->rx_level += 1;
+    for (int index = 0; index < size; index++) {
+        fifo8_push(&s->rx_fifo, buf[index]);
+    }
 
-    if (size > rx_fifo_level) {
-        s->uart_intr_state |= R_INTR_STATE_RX_WATERMARK_MASK;
+    /* update INTR_STATE */
+    if (count != size) {
+        s->regs[R_INTR_STATE] |= INTR_RX_OVERFLOW_MASK;
+    }
+    rx_watermark_level = ot_uart_get_rx_watermark_level(s);
+    if (rx_watermark_level && size >= rx_watermark_level) {
+        s->regs[R_INTR_STATE] |= INTR_RX_WATERMARK_MASK;
     }
 
     ot_uart_update_irqs(s);
 }
 
-static gboolean ot_uart_xmit(void *do_not_use, GIOCondition cond,
-                             void *opaque)
+static void ot_uart_reset_tx_fifo(OtUARTState *s)
 {
-    OtUARTState *s = opaque;
-    uint8_t tx_fifo_level = (s->uart_fifo_ctrl & R_FIFO_CTRL_TXILVL_MASK)
-                            >> R_FIFO_CTRL_TXILVL_SHIFT;
-    int ret;
-
-    /* instant drain the fifo when there's no back-end */
-    if (!qemu_chr_fe_backend_connected(&s->chr)) {
-        s->tx_level = 0;
-        return G_SOURCE_REMOVE;
+    fifo8_reset(&s->tx_fifo);
+    s->regs[R_INTR_STATE] |= INTR_TX_EMPTY_MASK;
+    s->regs[R_INTR_STATE] |= INTR_TX_DONE_MASK;
+    if (s->tx_watermark_level) {
+        s->regs[R_INTR_STATE] |= INTR_TX_WATERMARK_MASK;
+        s->tx_watermark_level = 0;
     }
+}
 
-    if (!s->tx_level) {
-        s->uart_status &= ~R_STATUS_TXFULL_MASK;
-        s->uart_status |= R_STATUS_TXEMPTY_MASK;
-        s->uart_intr_state |= R_INTR_STATE_TX_EMPTY_MASK;
-        s->uart_intr_state &= ~R_INTR_STATE_TX_WATERMARK_MASK;
-        ot_uart_update_irqs(s);
-        return G_SOURCE_REMOVE;
+static void ot_uart_reset_rx_fifo(OtUARTState *s)
+{
+    fifo8_reset(&s->rx_fifo);
+    s->regs[R_INTR_STATE] &= ~INTR_RX_WATERMARK_MASK;
+    s->regs[R_INTR_STATE] &= ~INTR_RX_OVERFLOW_MASK;
+    if (FIELD_EX32(s->regs[R_CTRL], CTRL, RX)) {
+        qemu_chr_fe_accept_input(&s->chr);
     }
+}
 
-    ret = qemu_chr_fe_write(&s->chr, s->tx_fifo, s->tx_level);
+static uint32_t ot_uart_get_tx_watermark_level(const OtUARTState *s)
+{
+    uint32_t tx_ilvl = FIELD_EX32(s->regs[R_FIFO_CTRL], FIFO_CTRL, TXILVL);
 
-    if (ret >= 0) {
-        s->tx_level -= ret;
-        memmove(s->tx_fifo, s->tx_fifo + ret, s->tx_level);
-    }
+    return tx_ilvl < 7 ? (1 << tx_ilvl) : 64;
+}
 
-    if (s->tx_level) {
-        guint r = qemu_chr_fe_add_watch(&s->chr, G_IO_OUT | G_IO_HUP,
-                                        ot_uart_xmit, s);
-        if (!r) {
-            s->tx_level = 0;
-            return G_SOURCE_REMOVE;
-        }
+static void ot_uart_xmit(OtUARTState *s)
+{
+    const uint8_t *buf;
+    uint32_t size;
+    int ret;
+
+    if (fifo8_is_empty(&s->tx_fifo)) {
+        return;
     }
 
-    /* Clear the TX Full bit */
-    if (s->tx_level != OT_UART_TX_FIFO_SIZE) {
-        s->uart_status &= ~R_STATUS_TXFULL_MASK;
+    /* instant drain the fifo when there's no back-end */
+    if (!qemu_chr_fe_backend_connected(&s->chr)) {
+        ot_uart_reset_tx_fifo(s);
+        ot_uart_update_irqs(s);
+        return;
     }
 
-    /* Disable the TX_WATERMARK IRQ */
-    if (s->tx_level < tx_fifo_level) {
-        s->uart_intr_state &= ~R_INTR_STATE_TX_WATERMARK_MASK;
+    /* get a continuous buffer from the FIFO */
+    buf =
+        fifo8_peek_bufptr(&s->tx_fifo, fifo8_num_used(&s->tx_fifo), &size);
+    /* send as much as possible */
+    ret = qemu_chr_fe_write(&s->chr, buf, (int)size);
+    /* if some characters where sent, remove them from the FIFO */
+    if (ret >= 0) {
+        fifo8_drop(&s->tx_fifo, ret);
     }
 
-    /* Set TX empty */
-    if (s->tx_level == 0) {
-        s->uart_status |= R_STATUS_TXEMPTY_MASK;
-        s->uart_intr_state |= R_INTR_STATE_TX_EMPTY_MASK;
+    /* update INTR_STATE */
+    if (fifo8_is_empty(&s->tx_fifo)) {
+        s->regs[R_INTR_STATE] |= INTR_TX_EMPTY_MASK;
+        s->regs[R_INTR_STATE] |= INTR_TX_DONE_MASK;
+    }
+    if (s->tx_watermark_level &&
+        fifo8_num_used(&s->tx_fifo) < s->tx_watermark_level) {
+        s->regs[R_INTR_STATE] |= INTR_TX_WATERMARK_MASK;
+        s->tx_watermark_level = 0;
     }
 
     ot_uart_update_irqs(s);
-    return G_SOURCE_REMOVE;
 }
 
-static void uart_write_tx_fifo(OtUARTState *s, const uint8_t *buf,
-                               int size)
+static void uart_write_tx_fifo(OtUARTState *s, uint8_t val)
 {
     uint64_t current_time = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
-    uint8_t tx_fifo_level = (s->uart_fifo_ctrl & R_FIFO_CTRL_TXILVL_MASK)
-                            >> R_FIFO_CTRL_TXILVL_SHIFT;
-
-    if (size > OT_UART_TX_FIFO_SIZE - s->tx_level) {
-        size = OT_UART_TX_FIFO_SIZE - s->tx_level;
+    if (fifo8_is_full(&s->tx_fifo)) {
         qemu_log_mask(LOG_GUEST_ERROR, "ot_uart: TX FIFO overflow");
+        return;
     }
 
-    memcpy(s->tx_fifo + s->tx_level, buf, size);
-    s->tx_level += size;
+    fifo8_push(&s->tx_fifo, val);
 
-    if (s->tx_level > 0) {
-        s->uart_status &= ~R_STATUS_TXEMPTY_MASK;
+    s->tx_watermark_level = ot_uart_get_tx_watermark_level(s);
+    if (fifo8_num_used(&s->tx_fifo) < s->tx_watermark_level) {
+        /*
+         * TX watermark interrupt is raised when FIFO depth goes from above
+         * watermark to below. If we haven't reached watermark, reset cached
+         * watermark level
+         */
+        s->tx_watermark_level = 0;
     }
 
-    if (s->tx_level >= tx_fifo_level) {
-        s->uart_intr_state |= R_INTR_STATE_TX_WATERMARK_MASK;
-        ot_uart_update_irqs(s);
-    }
-
-    if (s->tx_level == OT_UART_TX_FIFO_SIZE) {
-        s->uart_status |= R_STATUS_TXFULL_MASK;
+    if (FIELD_EX32(s->regs[R_CTRL], CTRL, TX)) {
+        ot_uart_xmit(s);
     }
 
     timer_mod(s->fifo_trigger_handle, current_time +
@@ -236,17 +285,13 @@ static void ot_uart_reset_enter(Object *obj, ResetType type)
         c->parent_phases.enter(obj, type);
     }
 
-    s->uart_intr_state = 0x00000000;
-    s->uart_intr_state = 0x00000000;
-    s->uart_intr_enable = 0x00000000;
-    s->uart_ctrl = 0x00000000;
-    s->uart_status = 0x0000003c;
-    s->uart_rdata = 0x00000000;
-    s->uart_fifo_ctrl = 0x00000000;
-    s->uart_fifo_status = 0x00000000;
-    s->uart_ovrd = 0x00000000;
-    s->uart_val = 0x00000000;
-    s->uart_timeout_ctrl = 0x00000000;
+    memset(&s->regs[0], 0, sizeof(s->regs));
+
+    s->regs[R_STATUS] = 0x0000003c;
+
+    s->tx_watermark_level = 0;
+    ot_uart_reset_tx_fifo(s);
+    ot_uart_reset_rx_fifo(s);
 
     s->tx_level = 0;
     s->rx_level = 0;
@@ -260,7 +305,7 @@ static uint64_t ot_uart_get_baud(OtUARTState *s)
 {
     uint64_t baud;
 
-    baud = ((s->uart_ctrl & R_CTRL_NCO_MASK) >> 16);
+    baud = ((s->regs[R_CTRL] & R_CTRL_NCO_MASK) >> 16);
     baud *= clock_get_hz(s->f_clk);
     baud >>= 20;
 
@@ -274,10 +319,10 @@ static uint64_t ot_uart_read(void *opaque, hwaddr addr, unsigned int size)
 
     switch (addr >> 2) {
     case R_INTR_STATE:
-        retvalue = s->uart_intr_state;
+        retvalue = s->regs[R_INTR_STATE];
         break;
     case R_INTR_ENABLE:
-        retvalue = s->uart_intr_enable;
+        retvalue = s->regs[R_INTR_ENABLE];
         break;
     case R_INTR_TEST:
         qemu_log_mask(LOG_GUEST_ERROR,
@@ -285,22 +330,22 @@ static uint64_t ot_uart_read(void *opaque, hwaddr addr, unsigned int size)
         break;
 
     case R_CTRL:
-        retvalue = s->uart_ctrl;
+        retvalue = s->regs[R_CTRL];
         break;
     case R_STATUS:
-        retvalue = s->uart_status;
+        retvalue = s->regs[R_STATUS];
         break;
 
     case R_RDATA:
-        retvalue = s->uart_rdata;
-        if ((s->uart_ctrl & R_CTRL_RX_ENABLE_MASK) && (s->rx_level > 0)) {
+        retvalue = s->regs[R_RDATA];
+        if ((s->regs[R_CTRL] & R_CTRL_RX_MASK) && (s->rx_level > 0)) {
             qemu_chr_fe_accept_input(&s->chr);
 
             s->rx_level -= 1;
-            s->uart_status &= ~R_STATUS_RXFULL_MASK;
+            s->regs[R_STATUS] &= ~R_STATUS_RXFULL_MASK;
             if (s->rx_level == 0) {
-                s->uart_status |= R_STATUS_RXIDLE_MASK;
-                s->uart_status |= R_STATUS_RXEMPTY_MASK;
+                s->regs[R_STATUS] |= R_STATUS_RXIDLE_MASK;
+                s->regs[R_STATUS] |= R_STATUS_RXEMPTY_MASK;
             }
         }
         break;
@@ -310,10 +355,10 @@ static uint64_t ot_uart_read(void *opaque, hwaddr addr, unsigned int size)
         break;
 
     case R_FIFO_CTRL:
-        retvalue = s->uart_fifo_ctrl;
+        retvalue = s->regs[R_FIFO_CTRL];
         break;
     case R_FIFO_STATUS:
-        retvalue = s->uart_fifo_status;
+        retvalue = s->regs[R_FIFO_STATUS];
 
         retvalue |= (s->rx_level & 0x1F) << R_FIFO_STATUS_RXLVL_SHIFT;
         retvalue |= (s->tx_level & 0x1F) << R_FIFO_STATUS_TXLVL_SHIFT;
@@ -323,17 +368,17 @@ static uint64_t ot_uart_read(void *opaque, hwaddr addr, unsigned int size)
         break;
 
     case R_OVRD:
-        retvalue = s->uart_ovrd;
+        retvalue = s->regs[R_OVRD];
         qemu_log_mask(LOG_UNIMP,
                       "%s: ovrd is not supported\n", __func__);
         break;
     case R_VAL:
-        retvalue = s->uart_val;
+        retvalue = s->regs[R_VAL];
         qemu_log_mask(LOG_UNIMP,
                       "%s: val is not supported\n", __func__);
         break;
     case R_TIMEOUT_CTRL:
-        retvalue = s->uart_timeout_ctrl;
+        retvalue = s->regs[R_TIMEOUT_CTRL];
         qemu_log_mask(LOG_UNIMP,
                       "%s: timeout_ctrl is not supported\n", __func__);
         break;
@@ -355,20 +400,23 @@ static void ot_uart_write(void *opaque, hwaddr addr, uint64_t val64,
     switch (addr >> 2) {
     case R_INTR_STATE:
         /* Write 1 clear */
-        s->uart_intr_state &= ~value;
+        value &= INTR_MASK;
+        s->regs[R_INTR_STATE] &= ~value;
         ot_uart_update_irqs(s);
         break;
     case R_INTR_ENABLE:
-        s->uart_intr_enable = value;
+        value &= INTR_MASK;
+        s->regs[R_INTR_ENABLE] = value;
         ot_uart_update_irqs(s);
         break;
     case R_INTR_TEST:
-        s->uart_intr_state |= value;
+        value &= INTR_MASK;
+        s->regs[R_INTR_STATE] |= value;
         ot_uart_update_irqs(s);
         break;
 
     case R_CTRL:
-        s->uart_ctrl = value;
+        s->regs[R_CTRL] = value;
 
         if (value & R_CTRL_NF_MASK) {
             qemu_log_mask(LOG_UNIMP,
@@ -412,11 +460,11 @@ static void ot_uart_write(void *opaque, hwaddr addr, uint64_t val64,
                       "%s: rdata is read only\n", __func__);
         break;
     case R_WDATA:
-        uart_write_tx_fifo(s, (uint8_t *) &value, 1);
+        uart_write_tx_fifo(s, value);
         break;
 
     case R_FIFO_CTRL:
-        s->uart_fifo_ctrl = value;
+        s->regs[R_FIFO_CTRL] = value;
 
         if (value & R_FIFO_CTRL_RXRST_MASK) {
             s->rx_level = 0;
@@ -431,18 +479,20 @@ static void ot_uart_write(void *opaque, hwaddr addr, uint64_t val64,
         qemu_log_mask(LOG_GUEST_ERROR,
                       "%s: fifo_status is read only\n", __func__);
         break;
-
     case R_OVRD:
-        s->uart_ovrd = value;
-        qemu_log_mask(LOG_UNIMP,
-                      "%s: ovrd is not supported\n", __func__);
+        if (value & R_OVRD_TXEN_MASK) {
+            qemu_log_mask(LOG_UNIMP, "%s: OVRD.TXEN is not supported\n",
+                          __func__);
+        }
+        s->regs[R_OVRD] = value & R_OVRD_TXVAL_MASK;
         break;
     case R_VAL:
         qemu_log_mask(LOG_GUEST_ERROR,
                       "%s: val is read only\n", __func__);
         break;
     case R_TIMEOUT_CTRL:
-        s->uart_timeout_ctrl = value;
+        s->regs[R_TIMEOUT_CTRL] =
+            value & (R_TIMEOUT_CTRL_EN_MASK | R_TIMEOUT_CTRL_VAL_MASK);
         qemu_log_mask(LOG_UNIMP,
                       "%s: timeout_ctrl is not supported\n", __func__);
         break;
@@ -466,8 +516,8 @@ static void fifo_trigger_update(void *opaque)
 {
     OtUARTState *s = opaque;
 
-    if (s->uart_ctrl & R_CTRL_TX_ENABLE_MASK) {
-        ot_uart_xmit(NULL, G_IO_OUT, s);
+    if (s->regs[R_CTRL] & R_CTRL_TX_MASK) {
+        ot_uart_xmit(s);
     }
 }
 
@@ -489,25 +539,17 @@ static int ot_uart_post_load(void *opaque, int version_id)
 
 static const VMStateDescription vmstate_ot_uart = {
     .name = TYPE_OT_UART,
-    .version_id = 1,
-    .minimum_version_id = 1,
+    .version_id = 2,
+    .minimum_version_id = 2,
     .post_load = ot_uart_post_load,
     .fields = (const VMStateField[]) {
-        VMSTATE_UINT8_ARRAY(tx_fifo, OtUARTState,
-                            OT_UART_TX_FIFO_SIZE),
+        VMSTATE_STRUCT(tx_fifo, OtUARTState, 1, vmstate_fifo8, Fifo8),
+        VMSTATE_STRUCT(rx_fifo, OtUARTState, 1, vmstate_fifo8, Fifo8),
         VMSTATE_UINT32(tx_level, OtUARTState),
         VMSTATE_UINT64(char_tx_time, OtUARTState),
         VMSTATE_TIMER_PTR(fifo_trigger_handle, OtUARTState),
-        VMSTATE_UINT32(uart_intr_state, OtUARTState),
-        VMSTATE_UINT32(uart_intr_enable, OtUARTState),
-        VMSTATE_UINT32(uart_ctrl, OtUARTState),
-        VMSTATE_UINT32(uart_status, OtUARTState),
-        VMSTATE_UINT32(uart_rdata, OtUARTState),
-        VMSTATE_UINT32(uart_fifo_ctrl, OtUARTState),
-        VMSTATE_UINT32(uart_fifo_status, OtUARTState),
-        VMSTATE_UINT32(uart_ovrd, OtUARTState),
-        VMSTATE_UINT32(uart_val, OtUARTState),
-        VMSTATE_UINT32(uart_timeout_ctrl, OtUARTState),
+        VMSTATE_ARRAY(regs, OtUARTState, REGS_COUNT, 1, vmstate_info_uint32,
+                      uint32_t),
         VMSTATE_END_OF_LIST()
     }
 };
@@ -529,9 +571,16 @@ static void ot_uart_init(Object *obj)
     sysbus_init_irq(SYS_BUS_DEVICE(obj), &s->tx_empty);
     sysbus_init_irq(SYS_BUS_DEVICE(obj), &s->rx_overflow);
 
-    memory_region_init_io(&s->mmio, obj, &ot_uart_ops, s,
-                          TYPE_OT_UART, 0x400);
+    memory_region_init_io(&s->mmio, obj, &ot_uart_ops, s, TYPE_OT_UART,
+                          REGS_SIZE);
     sysbus_init_mmio(SYS_BUS_DEVICE(obj), &s->mmio);
+
+    /*
+     * This array has a fixed size in the header. This assertion is used to
+     * check that it is consistent with the definition in this file. This is
+     * ostensibly a runtime check, but may be optimised away by the compiler.
+     */
+    assert(REGS_SIZE == sizeof(s->regs));
 }
 
 static void ot_uart_realize(DeviceState *dev, Error **errp)
@@ -541,6 +590,9 @@ static void ot_uart_realize(DeviceState *dev, Error **errp)
     s->fifo_trigger_handle = timer_new_ns(QEMU_CLOCK_VIRTUAL,
                                           fifo_trigger_update, s);
 
+    fifo8_create(&s->tx_fifo, OT_UART_TX_FIFO_SIZE);
+    fifo8_create(&s->rx_fifo, OT_UART_RX_FIFO_SIZE);
+
     qemu_chr_fe_set_handlers(&s->chr, ot_uart_can_receive,
                              ot_uart_receive, NULL, NULL,
                              s, NULL, true);
include/hw/char/ot_uart.h +6 -12
index fee2128f90..f489612700 100644
@@ -26,11 +26,11 @@
 #define HW_OT_UART_H
 
 #include "hw/core/sysbus.h"
+#include "qemu/fifo8.h"
 #include "chardev/char-fe.h"
 #include "qemu/timer.h"
 #include "qom/object.h"
 
-#define OT_UART_TX_FIFO_SIZE 16
 #define OT_UART_CLOCK 50000000 /* 50MHz clock */
 
 #define TYPE_OT_UART "ot-uart"
@@ -43,7 +43,6 @@ struct OtUARTState {
     /* <public> */
     MemoryRegion mmio;
 
-    uint8_t tx_fifo[OT_UART_TX_FIFO_SIZE];
     uint32_t tx_level;
 
     uint32_t rx_level;
@@ -51,16 +50,11 @@ struct OtUARTState {
     QEMUTimer *fifo_trigger_handle;
     uint64_t char_tx_time;
 
-    uint32_t uart_intr_state;
-    uint32_t uart_intr_enable;
-    uint32_t uart_ctrl;
-    uint32_t uart_status;
-    uint32_t uart_rdata;
-    uint32_t uart_fifo_ctrl;
-    uint32_t uart_fifo_status;
-    uint32_t uart_ovrd;
-    uint32_t uart_val;
-    uint32_t uart_timeout_ctrl;
+    uint32_t regs[13]; /* Length must be updated if regs added or removed */
+
+    Fifo8 tx_fifo;
+    Fifo8 rx_fifo;
+    uint32_t tx_watermark_level;
 
     Clock *f_clk;
 
Signature · 1 line
2.49.1
#6 lore raw

Re: [PATCH qemu v4 3/9] ot_uart: update register defs, switch to Fifo8 for tx/rx buffers

Quoted 324 lines · ~lexbaileylowrisc

From: Lex Bailey <[email protected]>

The register definitions for the UART device need to be updated to match the documentation at https://opentitan.org/book/hw/ip/uart/doc/registers.html

This commit does that, and also switches to using Fifo8 for managing the transmit and receive buffers.

---
 hw/char/ot_uart.c         | 338 ++++++++++++++++++++++----------------
 include/hw/char/ot_uart.h |  18 +-
 2 files changed, 201 insertions(+), 155 deletions(-)
hw/char/ot_uart.c +127 -80
index 9478af40f9..3fe7c5efe5 100644
@@ -27,6 +27,7 @@
 
 #include "qemu/osdep.h"
 #include "hw/char/ot_uart.h"
+#include "qemu/fifo8.h"
 #include "hw/core/irq.h"
 #include "hw/core/qdev-clock.h"
 #include "hw/core/qdev-properties.h"
@@ -37,16 +38,22 @@
 #include "qemu/module.h"
 
 REG32(INTR_STATE, 0x00)
-    FIELD(INTR_STATE, TX_WATERMARK, 0, 1)
-    FIELD(INTR_STATE, RX_WATERMARK, 1, 1)
-    FIELD(INTR_STATE, TX_EMPTY, 2, 1)
-    FIELD(INTR_STATE, RX_OVERFLOW, 3, 1)
+    SHARED_FIELD(INTR_TX_WATERMARK, 0, 1)
+    SHARED_FIELD(INTR_RX_WATERMARK, 1, 1)
+    SHARED_FIELD(INTR_TX_DONE, 2, 1)
+    SHARED_FIELD(INTR_RX_OVERFLOW, 3, 1)
+    SHARED_FIELD(INTR_RX_FRAME_ERR, 4, 1)
+    SHARED_FIELD(INTR_RX_BREAK_ERR, 5, 1)
+    SHARED_FIELD(INTR_RX_TIMEOUT, 6, 1)
+    SHARED_FIELD(INTR_RX_PARITY_ERR, 7, 1)
+    SHARED_FIELD(INTR_TX_EMPTY, 8, 1)
 REG32(INTR_ENABLE, 0x04)
 REG32(INTR_TEST, 0x08)
 REG32(ALERT_TEST, 0x0C)
+    FIELD(ALERT_TEST, FATAL_FAULT, 0, 1)
 REG32(CTRL, 0x10)
-    FIELD(CTRL, TX_ENABLE, 0, 1)
-    FIELD(CTRL, RX_ENABLE, 1, 1)
+    FIELD(CTRL, TX, 0, 1)
+    FIELD(CTRL, RX, 1, 1)
     FIELD(CTRL, NF, 2, 1)
     FIELD(CTRL, SLPBK, 4, 1)
     FIELD(CTRL, LLPBK, 5, 1)
@@ -58,43 +65,74 @@ REG32(STATUS, 0x14)
     FIELD(STATUS, TXFULL, 0, 1)
     FIELD(STATUS, RXFULL, 1, 1)
     FIELD(STATUS, TXEMPTY, 2, 1)
+    FIELD(STATUS, TXIDLE, 3, 1)
     FIELD(STATUS, RXIDLE, 4, 1)
     FIELD(STATUS, RXEMPTY, 5, 1)
 REG32(RDATA, 0x18)
+    FIELD(RDATA, RDATA, 0, 8)
 REG32(WDATA, 0x1C)
+    FIELD(WDATA, WDATA, 0, 8)
 REG32(FIFO_CTRL, 0x20)
     FIELD(FIFO_CTRL, RXRST, 0, 1)
     FIELD(FIFO_CTRL, TXRST, 1, 1)
     FIELD(FIFO_CTRL, RXILVL, 2, 3)
-    FIELD(FIFO_CTRL, TXILVL, 5, 2)
+    FIELD(FIFO_CTRL, TXILVL, 5, 3)
 REG32(FIFO_STATUS, 0x24)
-    FIELD(FIFO_STATUS, TXLVL, 0, 5)
-    FIELD(FIFO_STATUS, RXLVL, 16, 5)
+    FIELD(FIFO_STATUS, TXLVL, 0, 8)
+    FIELD(FIFO_STATUS, RXLVL, 16, 8)
 REG32(OVRD, 0x28)
+    FIELD(OVRD, TXEN, 0, 1)
+    FIELD(OVRD, TXVAL, 1, 1)
 REG32(VAL, 0x2C)
+    FIELD(VAL, RX, 0, 16)
 REG32(TIMEOUT_CTRL, 0x30)
+    FIELD(TIMEOUT_CTRL, VAL, 0, 24)
+    FIELD(TIMEOUT_CTRL, EN, 31, 1)
+
+#define INTR_MASK \
+    (INTR_TX_WATERMARK_MASK | INTR_RX_WATERMARK_MASK | INTR_TX_DONE_MASK | \
+     INTR_RX_OVERFLOW_MASK | INTR_RX_FRAME_ERR_MASK | INTR_RX_BREAK_ERR_MASK | \
+     INTR_RX_TIMEOUT_MASK | INTR_RX_PARITY_ERR_MASK | INTR_TX_EMPTY_MASK)
+
+#define CTRL_MASK \
+    (R_CTRL_TX_MASK | R_CTRL_RX_MASK | R_CTRL_NF_MASK | R_CTRL_SLPBK_MASK | \
+     R_CTRL_LLPBK_MASK | R_CTRL_PARITY_EN_MASK | R_CTRL_PARITY_ODD_MASK | \
+     R_CTRL_RXBLVL_MASK | R_CTRL_NCO_MASK)
+
+#define OT_UART_NCO_BITS     16
+#define OT_UART_TX_FIFO_SIZE 128
+#define OT_UART_RX_FIFO_SIZE 128
+
+#define R32_OFF(_r_) ((_r_) / sizeof(uint32_t))
+
+#define R_LAST_REG (R_TIMEOUT_CTRL)
+#define REGS_COUNT (R_LAST_REG + 1u)
+#define REGS_SIZE  (REGS_COUNT * sizeof(uint32_t))
 
 static void ot_uart_update_irqs(OtUARTState *s)
 {
-    if (s->uart_intr_state & s->uart_intr_enable & R_INTR_STATE_TX_WATERMARK_MASK) {
+    if (s->regs[R_INTR_STATE] & s->regs[R_INTR_ENABLE]
+        & INTR_TX_WATERMARK_MASK) {
         qemu_set_irq(s->tx_watermark, 1);
     } else {
         qemu_set_irq(s->tx_watermark, 0);
     }
 
-    if (s->uart_intr_state & s->uart_intr_enable & R_INTR_STATE_RX_WATERMARK_MASK) {
+    if (s->regs[R_INTR_STATE] & s->regs[R_INTR_ENABLE]
+        & INTR_RX_WATERMARK_MASK) {
         qemu_set_irq(s->rx_watermark, 1);
     } else {
         qemu_set_irq(s->rx_watermark, 0);
     }
 
-    if (s->uart_intr_state & s->uart_intr_enable & R_INTR_STATE_TX_EMPTY_MASK) {
+    if (s->regs[R_INTR_STATE] & s->regs[R_INTR_ENABLE] & INTR_TX_EMPTY_MASK) {
         qemu_set_irq(s->tx_empty, 1);
     } else {
         qemu_set_irq(s->tx_empty, 0);
     }
 
-    if (s->uart_intr_state & s->uart_intr_enable & R_INTR_STATE_RX_OVERFLOW_MASK) {
+    if (s->regs[R_INTR_STATE] & s->regs[R_INTR_ENABLE]
+        & INTR_RX_OVERFLOW_MASK) {
         qemu_set_irq(s->rx_overflow, 1);
     } else {
         qemu_set_irq(s->rx_overflow, 0);
@@ -105,122 +143,133 @@ static int ot_uart_can_receive(void *opaque)
 {
     OtUARTState *s = opaque;
 
-    if ((s->uart_ctrl & R_CTRL_RX_ENABLE_MASK)
-           && !(s->uart_status & R_STATUS_RXFULL_MASK)) {
-        return 1;
+    if (s->regs[R_CTRL] & R_CTRL_RX_MASK) {
+        return (int)fifo8_num_free(&s->rx_fifo);
     }
 
     return 0;
 }
 
+static uint32_t ot_uart_get_rx_watermark_level(const OtUARTState *s)
+{
+    uint32_t rx_ilvl = FIELD_EX32(s->regs[R_FIFO_CTRL], FIFO_CTRL, RXILVL);
+
+    return rx_ilvl < 7 ? (1 << rx_ilvl) : 126;
+}
+
 static void ot_uart_receive(void *opaque, const uint8_t *buf, int size)
 {
     OtUARTState *s = opaque;
-    uint8_t rx_fifo_level = (s->uart_fifo_ctrl & R_FIFO_CTRL_RXILVL_MASK)
-                            >> R_FIFO_CTRL_RXILVL_SHIFT;
-
-    s->uart_rdata = *buf;
+    uint32_t rx_watermark_level;
+    size_t count = MIN(fifo8_num_free(&s->rx_fifo), (size_t)size);
 
-    s->uart_status &= ~R_STATUS_RXIDLE_MASK;
-    s->uart_status &= ~R_STATUS_RXEMPTY_MASK;
-    /* The RXFULL is set after receiving a single byte
-     * as the FIFO buffers are not yet implemented.
-     */
-    s->uart_status |= R_STATUS_RXFULL_MASK;
-    s->rx_level += 1;
+    for (int index = 0; index < size; index++) {
+        fifo8_push(&s->rx_fifo, buf[index]);
+    }
 
-    if (size > rx_fifo_level) {
-        s->uart_intr_state |= R_INTR_STATE_RX_WATERMARK_MASK;
+    /* update INTR_STATE */
+    if (count != size) {
+        s->regs[R_INTR_STATE] |= INTR_RX_OVERFLOW_MASK;
+    }
+    rx_watermark_level = ot_uart_get_rx_watermark_level(s);
+    if (rx_watermark_level && size >= rx_watermark_level) {
+        s->regs[R_INTR_STATE] |= INTR_RX_WATERMARK_MASK;
     }
 
     ot_uart_update_irqs(s);
 }
 
-static gboolean ot_uart_xmit(void *do_not_use, GIOCondition cond,
-                             void *opaque)
+static void ot_uart_reset_tx_fifo(OtUARTState *s)
 {
-    OtUARTState *s = opaque;
-    uint8_t tx_fifo_level = (s->uart_fifo_ctrl & R_FIFO_CTRL_TXILVL_MASK)
-                            >> R_FIFO_CTRL_TXILVL_SHIFT;
-    int ret;
-
-    /* instant drain the fifo when there's no back-end */
-    if (!qemu_chr_fe_backend_connected(&s->chr)) {
-        s->tx_level = 0;
-        return G_SOURCE_REMOVE;
+    fifo8_reset(&s->tx_fifo);
+    s->regs[R_INTR_STATE] |= INTR_TX_EMPTY_MASK;
+    s->regs[R_INTR_STATE] |= INTR_TX_DONE_MASK;
+    if (s->tx_watermark_level) {
+        s->regs[R_INTR_STATE] |= INTR_TX_WATERMARK_MASK;
+        s->tx_watermark_level = 0;
     }
+}
 
-    if (!s->tx_level) {
-        s->uart_status &= ~R_STATUS_TXFULL_MASK;
-        s->uart_status |= R_STATUS_TXEMPTY_MASK;
-        s->uart_intr_state |= R_INTR_STATE_TX_EMPTY_MASK;
-        s->uart_intr_state &= ~R_INTR_STATE_TX_WATERMARK_MASK;
-        ot_uart_update_irqs(s);
-        return G_SOURCE_REMOVE;
+static void ot_uart_reset_rx_fifo(OtUARTState *s)
+{
+    fifo8_reset(&s->rx_fifo);
+    s->regs[R_INTR_STATE] &= ~INTR_RX_WATERMARK_MASK;
+    s->regs[R_INTR_STATE] &= ~INTR_RX_OVERFLOW_MASK;
+    if (FIELD_EX32(s->regs[R_CTRL], CTRL, RX)) {
+        qemu_chr_fe_accept_input(&s->chr);
     }
+}
 
-    ret = qemu_chr_fe_write(&s->chr, s->tx_fifo, s->tx_level);
+static uint32_t ot_uart_get_tx_watermark_level(const OtUARTState *s)
+{
+    uint32_t tx_ilvl = FIELD_EX32(s->regs[R_FIFO_CTRL], FIFO_CTRL, TXILVL);
 
-    if (ret >= 0) {
-        s->tx_level -= ret;
-        memmove(s->tx_fifo, s->tx_fifo + ret, s->tx_level);
-    }
+    return tx_ilvl < 7 ? (1 << tx_ilvl) : 64;
+}
 
-    if (s->tx_level) {
-        guint r = qemu_chr_fe_add_watch(&s->chr, G_IO_OUT | G_IO_HUP,
-                                        ot_uart_xmit, s);
-        if (!r) {
-            s->tx_level = 0;
-            return G_SOURCE_REMOVE;
-        }
+static void ot_uart_xmit(OtUARTState *s)
+{
+    const uint8_t *buf;
+    uint32_t size;
+    int ret;
+
+    if (fifo8_is_empty(&s->tx_fifo)) {
+        return;
     }
 
-    /* Clear the TX Full bit */
-    if (s->tx_level != OT_UART_TX_FIFO_SIZE) {
-        s->uart_status &= ~R_STATUS_TXFULL_MASK;
+    /* instant drain the fifo when there's no back-end */
+    if (!qemu_chr_fe_backend_connected(&s->chr)) {
+        ot_uart_reset_tx_fifo(s);
+        ot_uart_update_irqs(s);
+        return;
     }
 
-    /* Disable the TX_WATERMARK IRQ */
-    if (s->tx_level < tx_fifo_level) {
-        s->uart_intr_state &= ~R_INTR_STATE_TX_WATERMARK_MASK;
+    /* get a continuous buffer from the FIFO */
+    buf =
+        fifo8_peek_bufptr(&s->tx_fifo, fifo8_num_used(&s->tx_fifo), &size);
+    /* send as much as possible */
+    ret = qemu_chr_fe_write(&s->chr, buf, (int)size);
+    /* if some characters where sent, remove them from the FIFO */
+    if (ret >= 0) {
+        fifo8_drop(&s->tx_fifo, ret);
     }
 
-    /* Set TX empty */
-    if (s->tx_level == 0) {
-        s->uart_status |= R_STATUS_TXEMPTY_MASK;
-        s->uart_intr_state |= R_INTR_STATE_TX_EMPTY_MASK;
+    /* update INTR_STATE */
+    if (fifo8_is_empty(&s->tx_fifo)) {
+        s->regs[R_INTR_STATE] |= INTR_TX_EMPTY_MASK;
+        s->regs[R_INTR_STATE] |= INTR_TX_DONE_MASK;
+    }
+    if (s->tx_watermark_level &&
+        fifo8_num_used(&s->tx_fifo) < s->tx_watermark_level) {
+        s->regs[R_INTR_STATE] |= INTR_TX_WATERMARK_MASK;
+        s->tx_watermark_level = 0;
     }
 
     ot_uart_update_irqs(s);
-    return G_SOURCE_REMOVE;
 }
 
-static void uart_write_tx_fifo(OtUARTState *s, const uint8_t *buf,
-                               int size)
+static void uart_write_tx_fifo(OtUARTState *s, uint8_t val)
 {
     uint64_t current_time = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
-    uint8_t tx_fifo_level = (s->uart_fifo_ctrl & R_FIFO_CTRL_TXILVL_MASK)
-                            >> R_FIFO_CTRL_TXILVL_SHIFT;
-
-    if (size > OT_UART_TX_FIFO_SIZE - s->tx_level) {
-        size = OT_UART_TX_FIFO_SIZE - s->tx_level;
+    if (fifo8_is_full(&s->tx_fifo)) {
         qemu_log_mask(LOG_GUEST_ERROR, "ot_uart: TX FIFO overflow");
+        return;
     }
 
-    memcpy(s->tx_fifo + s->tx_level, buf, size);
-    s->tx_level += size;
+    fifo8_push(&s->tx_fifo, val);
 
-    if (s->tx_level > 0) {

hw/char/ot_uart.c static int ot_uart_can_receive(void *opaque) · line 269

Quoted 10 lines
hw/char/ot_uart.c +8 -1
-        s->uart_status &= ~R_STATUS_TXEMPTY_MASK;
+    s->tx_watermark_level = ot_uart_get_tx_watermark_level(s);
+    if (fifo8_num_used(&s->tx_fifo) < s->tx_watermark_level) {
+        /*
+         * TX watermark interrupt is raised when FIFO depth goes from above
+         * watermark to below. If we haven't reached watermark, reset cached
+         * watermark level
+         */
+        s->tx_watermark_level = 0;
     }

Don't we need to update the interrupts at this point?

Alistair

#7 lore raw

Re: [PATCH qemu v4 3/9] ot_uart: update register defs, switch to Fifo8 for tx/rx buffers

Quoted 324 lines · Alistair Francis
Quoted 322 lines · ~lexbaileylowrisc

From: Lex Bailey<[email protected]>

The register definitions for the UART device need to be updated to match the documentation athttps://opentitan.org/book/hw/ip/uart/doc/registers.html

This commit does that, and also switches to using Fifo8 for managing the transmit and receive buffers.

---
  hw/char/ot_uart.c         | 338 ++++++++++++++++++++++----------------
  include/hw/char/ot_uart.h |  18 +-
  2 files changed, 201 insertions(+), 155 deletions(-)
hw/char/ot_uart.c +127 -79
index 9478af40f9..3fe7c5efe5 100644
@@ -27,6 +27,7 @@
 
  #include "qemu/osdep.h"
  #include "hw/char/ot_uart.h"
+#include "qemu/fifo8.h"
  #include "hw/core/irq.h"
  #include "hw/core/qdev-clock.h"
  #include "hw/core/qdev-properties.h"
@@ -37,16 +38,22 @@
  #include "qemu/module.h"
 
  REG32(INTR_STATE, 0x00)
-    FIELD(INTR_STATE, TX_WATERMARK, 0, 1)
-    FIELD(INTR_STATE, RX_WATERMARK, 1, 1)
-    FIELD(INTR_STATE, TX_EMPTY, 2, 1)
-    FIELD(INTR_STATE, RX_OVERFLOW, 3, 1)
+    SHARED_FIELD(INTR_TX_WATERMARK, 0, 1)
+    SHARED_FIELD(INTR_RX_WATERMARK, 1, 1)
+    SHARED_FIELD(INTR_TX_DONE, 2, 1)
+    SHARED_FIELD(INTR_RX_OVERFLOW, 3, 1)
+    SHARED_FIELD(INTR_RX_FRAME_ERR, 4, 1)
+    SHARED_FIELD(INTR_RX_BREAK_ERR, 5, 1)
+    SHARED_FIELD(INTR_RX_TIMEOUT, 6, 1)
+    SHARED_FIELD(INTR_RX_PARITY_ERR, 7, 1)
+    SHARED_FIELD(INTR_TX_EMPTY, 8, 1)
  REG32(INTR_ENABLE, 0x04)
  REG32(INTR_TEST, 0x08)
  REG32(ALERT_TEST, 0x0C)
+    FIELD(ALERT_TEST, FATAL_FAULT, 0, 1)
  REG32(CTRL, 0x10)
-    FIELD(CTRL, TX_ENABLE, 0, 1)
-    FIELD(CTRL, RX_ENABLE, 1, 1)
+    FIELD(CTRL, TX, 0, 1)
+    FIELD(CTRL, RX, 1, 1)
      FIELD(CTRL, NF, 2, 1)
      FIELD(CTRL, SLPBK, 4, 1)
      FIELD(CTRL, LLPBK, 5, 1)
@@ -58,43 +65,74 @@ REG32(STATUS, 0x14)
      FIELD(STATUS, TXFULL, 0, 1)
      FIELD(STATUS, RXFULL, 1, 1)
      FIELD(STATUS, TXEMPTY, 2, 1)
+    FIELD(STATUS, TXIDLE, 3, 1)
      FIELD(STATUS, RXIDLE, 4, 1)
      FIELD(STATUS, RXEMPTY, 5, 1)
  REG32(RDATA, 0x18)
+    FIELD(RDATA, RDATA, 0, 8)
  REG32(WDATA, 0x1C)
+    FIELD(WDATA, WDATA, 0, 8)
  REG32(FIFO_CTRL, 0x20)
      FIELD(FIFO_CTRL, RXRST, 0, 1)
      FIELD(FIFO_CTRL, TXRST, 1, 1)
      FIELD(FIFO_CTRL, RXILVL, 2, 3)
-    FIELD(FIFO_CTRL, TXILVL, 5, 2)
+    FIELD(FIFO_CTRL, TXILVL, 5, 3)
  REG32(FIFO_STATUS, 0x24)
-    FIELD(FIFO_STATUS, TXLVL, 0, 5)
-    FIELD(FIFO_STATUS, RXLVL, 16, 5)
+    FIELD(FIFO_STATUS, TXLVL, 0, 8)
+    FIELD(FIFO_STATUS, RXLVL, 16, 8)
  REG32(OVRD, 0x28)
+    FIELD(OVRD, TXEN, 0, 1)
+    FIELD(OVRD, TXVAL, 1, 1)
  REG32(VAL, 0x2C)
+    FIELD(VAL, RX, 0, 16)
  REG32(TIMEOUT_CTRL, 0x30)
+    FIELD(TIMEOUT_CTRL, VAL, 0, 24)
+    FIELD(TIMEOUT_CTRL, EN, 31, 1)
+
+#define INTR_MASK \
+    (INTR_TX_WATERMARK_MASK | INTR_RX_WATERMARK_MASK | INTR_TX_DONE_MASK | \
+     INTR_RX_OVERFLOW_MASK | INTR_RX_FRAME_ERR_MASK | INTR_RX_BREAK_ERR_MASK | \
+     INTR_RX_TIMEOUT_MASK | INTR_RX_PARITY_ERR_MASK | INTR_TX_EMPTY_MASK)
+
+#define CTRL_MASK \
+    (R_CTRL_TX_MASK | R_CTRL_RX_MASK | R_CTRL_NF_MASK | R_CTRL_SLPBK_MASK | \
+     R_CTRL_LLPBK_MASK | R_CTRL_PARITY_EN_MASK | R_CTRL_PARITY_ODD_MASK | \
+     R_CTRL_RXBLVL_MASK | R_CTRL_NCO_MASK)
+
+#define OT_UART_NCO_BITS     16
+#define OT_UART_TX_FIFO_SIZE 128
+#define OT_UART_RX_FIFO_SIZE 128
+
+#define R32_OFF(_r_) ((_r_) / sizeof(uint32_t))
+
+#define R_LAST_REG (R_TIMEOUT_CTRL)
+#define REGS_COUNT (R_LAST_REG + 1u)
+#define REGS_SIZE  (REGS_COUNT * sizeof(uint32_t))
 
  static void ot_uart_update_irqs(OtUARTState *s)
  {
-    if (s->uart_intr_state & s->uart_intr_enable & R_INTR_STATE_TX_WATERMARK_MASK) {
+    if (s->regs[R_INTR_STATE] & s->regs[R_INTR_ENABLE]
+        & INTR_TX_WATERMARK_MASK) {
          qemu_set_irq(s->tx_watermark, 1);
      } else {
          qemu_set_irq(s->tx_watermark, 0);
      }
 
-    if (s->uart_intr_state & s->uart_intr_enable & R_INTR_STATE_RX_WATERMARK_MASK) {
+    if (s->regs[R_INTR_STATE] & s->regs[R_INTR_ENABLE]
+        & INTR_RX_WATERMARK_MASK) {
          qemu_set_irq(s->rx_watermark, 1);
      } else {
          qemu_set_irq(s->rx_watermark, 0);
      }
 
-    if (s->uart_intr_state & s->uart_intr_enable & R_INTR_STATE_TX_EMPTY_MASK) {
+    if (s->regs[R_INTR_STATE] & s->regs[R_INTR_ENABLE] & INTR_TX_EMPTY_MASK) {
          qemu_set_irq(s->tx_empty, 1);
      } else {
          qemu_set_irq(s->tx_empty, 0);
      }
 
-    if (s->uart_intr_state & s->uart_intr_enable & R_INTR_STATE_RX_OVERFLOW_MASK) {
+    if (s->regs[R_INTR_STATE] & s->regs[R_INTR_ENABLE]
+        & INTR_RX_OVERFLOW_MASK) {
          qemu_set_irq(s->rx_overflow, 1);
      } else {
          qemu_set_irq(s->rx_overflow, 0);
@@ -105,122 +143,133 @@ static int ot_uart_can_receive(void *opaque)
  {
      OtUARTState *s = opaque;
 
-    if ((s->uart_ctrl & R_CTRL_RX_ENABLE_MASK)
-           && !(s->uart_status & R_STATUS_RXFULL_MASK)) {
-        return 1;
+    if (s->regs[R_CTRL] & R_CTRL_RX_MASK) {
+        return (int)fifo8_num_free(&s->rx_fifo);
      }
 
      return 0;
  }
 
+static uint32_t ot_uart_get_rx_watermark_level(const OtUARTState *s)
+{
+    uint32_t rx_ilvl = FIELD_EX32(s->regs[R_FIFO_CTRL], FIFO_CTRL, RXILVL);
+
+    return rx_ilvl < 7 ? (1 << rx_ilvl) : 126;
+}
+
  static void ot_uart_receive(void *opaque, const uint8_t *buf, int size)
  {
      OtUARTState *s = opaque;
-    uint8_t rx_fifo_level = (s->uart_fifo_ctrl & R_FIFO_CTRL_RXILVL_MASK)
-                            >> R_FIFO_CTRL_RXILVL_SHIFT;
-
-    s->uart_rdata = *buf;
+    uint32_t rx_watermark_level;
+    size_t count = MIN(fifo8_num_free(&s->rx_fifo), (size_t)size);
 
-    s->uart_status &= ~R_STATUS_RXIDLE_MASK;
-    s->uart_status &= ~R_STATUS_RXEMPTY_MASK;
-    /* The RXFULL is set after receiving a single byte
-     * as the FIFO buffers are not yet implemented.
-     */
-    s->uart_status |= R_STATUS_RXFULL_MASK;
-    s->rx_level += 1;
+    for (int index = 0; index < size; index++) {
+        fifo8_push(&s->rx_fifo, buf[index]);
+    }
 
-    if (size > rx_fifo_level) {
-        s->uart_intr_state |= R_INTR_STATE_RX_WATERMARK_MASK;
+    /* update INTR_STATE */
+    if (count != size) {
+        s->regs[R_INTR_STATE] |= INTR_RX_OVERFLOW_MASK;
+    }
+    rx_watermark_level = ot_uart_get_rx_watermark_level(s);
+    if (rx_watermark_level && size >= rx_watermark_level) {
+        s->regs[R_INTR_STATE] |= INTR_RX_WATERMARK_MASK;
      }
 
      ot_uart_update_irqs(s);
  }
 
-static gboolean ot_uart_xmit(void *do_not_use, GIOCondition cond,
-                             void *opaque)
+static void ot_uart_reset_tx_fifo(OtUARTState *s)
  {
-    OtUARTState *s = opaque;
-    uint8_t tx_fifo_level = (s->uart_fifo_ctrl & R_FIFO_CTRL_TXILVL_MASK)
-                            >> R_FIFO_CTRL_TXILVL_SHIFT;
-    int ret;
-
-    /* instant drain the fifo when there's no back-end */
-    if (!qemu_chr_fe_backend_connected(&s->chr)) {
-        s->tx_level = 0;
-        return G_SOURCE_REMOVE;
+    fifo8_reset(&s->tx_fifo);
+    s->regs[R_INTR_STATE] |= INTR_TX_EMPTY_MASK;
+    s->regs[R_INTR_STATE] |= INTR_TX_DONE_MASK;
+    if (s->tx_watermark_level) {
+        s->regs[R_INTR_STATE] |= INTR_TX_WATERMARK_MASK;
+        s->tx_watermark_level = 0;
      }
+}
 
-    if (!s->tx_level) {
-        s->uart_status &= ~R_STATUS_TXFULL_MASK;
-        s->uart_status |= R_STATUS_TXEMPTY_MASK;
-        s->uart_intr_state |= R_INTR_STATE_TX_EMPTY_MASK;
-        s->uart_intr_state &= ~R_INTR_STATE_TX_WATERMARK_MASK;
-        ot_uart_update_irqs(s);
-        return G_SOURCE_REMOVE;
+static void ot_uart_reset_rx_fifo(OtUARTState *s)
+{
+    fifo8_reset(&s->rx_fifo);
+    s->regs[R_INTR_STATE] &= ~INTR_RX_WATERMARK_MASK;
+    s->regs[R_INTR_STATE] &= ~INTR_RX_OVERFLOW_MASK;
+    if (FIELD_EX32(s->regs[R_CTRL], CTRL, RX)) {
+        qemu_chr_fe_accept_input(&s->chr);
      }
+}
 
-    ret = qemu_chr_fe_write(&s->chr, s->tx_fifo, s->tx_level);
+static uint32_t ot_uart_get_tx_watermark_level(const OtUARTState *s)
+{
+    uint32_t tx_ilvl = FIELD_EX32(s->regs[R_FIFO_CTRL], FIFO_CTRL, TXILVL);
 
-    if (ret >= 0) {
-        s->tx_level -= ret;
-        memmove(s->tx_fifo, s->tx_fifo + ret, s->tx_level);
-    }
+    return tx_ilvl < 7 ? (1 << tx_ilvl) : 64;
+}
 
-    if (s->tx_level) {
-        guint r = qemu_chr_fe_add_watch(&s->chr, G_IO_OUT | G_IO_HUP,
-                                        ot_uart_xmit, s);
-        if (!r) {
-            s->tx_level = 0;
-            return G_SOURCE_REMOVE;
-        }
+static void ot_uart_xmit(OtUARTState *s)
+{
+    const uint8_t *buf;
+    uint32_t size;
+    int ret;
+
+    if (fifo8_is_empty(&s->tx_fifo)) {
+        return;
      }
 
-    /* Clear the TX Full bit */
-    if (s->tx_level != OT_UART_TX_FIFO_SIZE) {
-        s->uart_status &= ~R_STATUS_TXFULL_MASK;
+    /* instant drain the fifo when there's no back-end */
+    if (!qemu_chr_fe_backend_connected(&s->chr)) {
+        ot_uart_reset_tx_fifo(s);
+        ot_uart_update_irqs(s);
+        return;
      }
 
-    /* Disable the TX_WATERMARK IRQ */
-    if (s->tx_level < tx_fifo_level) {
-        s->uart_intr_state &= ~R_INTR_STATE_TX_WATERMARK_MASK;
+    /* get a continuous buffer from the FIFO */
+    buf =
+        fifo8_peek_bufptr(&s->tx_fifo, fifo8_num_used(&s->tx_fifo), &size);
+    /* send as much as possible */
+    ret = qemu_chr_fe_write(&s->chr, buf, (int)size);
+    /* if some characters where sent, remove them from the FIFO */
+    if (ret >= 0) {
+        fifo8_drop(&s->tx_fifo, ret);
      }
 
-    /* Set TX empty */
-    if (s->tx_level == 0) {
-        s->uart_status |= R_STATUS_TXEMPTY_MASK;
-        s->uart_intr_state |= R_INTR_STATE_TX_EMPTY_MASK;
+    /* update INTR_STATE */
+    if (fifo8_is_empty(&s->tx_fifo)) {
+        s->regs[R_INTR_STATE] |= INTR_TX_EMPTY_MASK;
+        s->regs[R_INTR_STATE] |= INTR_TX_DONE_MASK;
+    }
+    if (s->tx_watermark_level &&
+        fifo8_num_used(&s->tx_fifo) < s->tx_watermark_level) {
+        s->regs[R_INTR_STATE] |= INTR_TX_WATERMARK_MASK;
+        s->tx_watermark_level = 0;
      }
 
      ot_uart_update_irqs(s);
-    return G_SOURCE_REMOVE;
  }
 
-static void uart_write_tx_fifo(OtUARTState *s, const uint8_t *buf,
-                               int size)
+static void uart_write_tx_fifo(OtUARTState *s, uint8_t val)
  {
      uint64_t current_time = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
-    uint8_t tx_fifo_level = (s->uart_fifo_ctrl & R_FIFO_CTRL_TXILVL_MASK)
-                            >> R_FIFO_CTRL_TXILVL_SHIFT;
-
-    if (size > OT_UART_TX_FIFO_SIZE - s->tx_level) {
-        size = OT_UART_TX_FIFO_SIZE - s->tx_level;
+    if (fifo8_is_full(&s->tx_fifo)) {
          qemu_log_mask(LOG_GUEST_ERROR, "ot_uart: TX FIFO overflow");
+        return;
      }
 
-    memcpy(s->tx_fifo + s->tx_level, buf, size);
-    s->tx_level += size;
+    fifo8_push(&s->tx_fifo, val);
 
Quoted 12 lines

hw/char/ot_uart.c static int ot_uart_can_receive(void *opaque) · line 269

Quoted 11 lines
hw/char/ot_uart.c +8 -2
-    if (s->tx_level > 0) {
-        s->uart_status &= ~R_STATUS_TXEMPTY_MASK;
+    s->tx_watermark_level = ot_uart_get_tx_watermark_level(s);
+    if (fifo8_num_used(&s->tx_fifo) < s->tx_watermark_level) {
+        /*
+         * TX watermark interrupt is raised when FIFO depth goes from above
+         * watermark to below. If we haven't reached watermark, reset cached
+         * watermark level
+         */
+        s->tx_watermark_level = 0;
      }

Don't we need to update the interrupts at this point?

Can you clarify what you mean by this? Are you wanting me to squash the next commit in to this one so that the IRQ change happens simultaneously with this one?

Quoted 2 lines

Alistair

Signature · 2 lines
Lex Bailey (they/them)
lowRISC C.I.C. 7 Hills Road, Cambridge, CB2 1GE, UK. Registered in England & Wales 09272283 [email protected]
#8 lore raw

Re: [PATCH qemu v4 3/9] ot_uart: update register defs, switch to Fifo8 for tx/rx buffers

Quoted 343 lines · Lex Bailey

On 26/05/2026 04:58, Alistair Francis wrote:

On Fri, May 1, 2026 at 2:05 AM ~lexbaileylowrisc
<[email protected]> wrote:

From: Lex Bailey <[email protected]>

The register definitions for the UART device need to be updated to match the documentation at https://opentitan.org/book/hw/ip/uart/doc/registers.html

This commit does that, and also switches to using Fifo8 for managing the transmit and receive buffers.

---
 hw/char/ot_uart.c         | 338 ++++++++++++++++++++++----------------
 include/hw/char/ot_uart.h |  18 +-
 2 files changed, 201 insertions(+), 155 deletions(-)
hw/char/ot_uart.c +135 -81
index 9478af40f9..3fe7c5efe5 100644
@@ -27,6 +27,7 @@
 
 #include "qemu/osdep.h"
 #include "hw/char/ot_uart.h"
+#include "qemu/fifo8.h"
 #include "hw/core/irq.h"
 #include "hw/core/qdev-clock.h"
 #include "hw/core/qdev-properties.h"
@@ -37,16 +38,22 @@
 #include "qemu/module.h"
 
 REG32(INTR_STATE, 0x00)
-    FIELD(INTR_STATE, TX_WATERMARK, 0, 1)
-    FIELD(INTR_STATE, RX_WATERMARK, 1, 1)
-    FIELD(INTR_STATE, TX_EMPTY, 2, 1)
-    FIELD(INTR_STATE, RX_OVERFLOW, 3, 1)
+    SHARED_FIELD(INTR_TX_WATERMARK, 0, 1)
+    SHARED_FIELD(INTR_RX_WATERMARK, 1, 1)
+    SHARED_FIELD(INTR_TX_DONE, 2, 1)
+    SHARED_FIELD(INTR_RX_OVERFLOW, 3, 1)
+    SHARED_FIELD(INTR_RX_FRAME_ERR, 4, 1)
+    SHARED_FIELD(INTR_RX_BREAK_ERR, 5, 1)
+    SHARED_FIELD(INTR_RX_TIMEOUT, 6, 1)
+    SHARED_FIELD(INTR_RX_PARITY_ERR, 7, 1)
+    SHARED_FIELD(INTR_TX_EMPTY, 8, 1)
 REG32(INTR_ENABLE, 0x04)
 REG32(INTR_TEST, 0x08)
 REG32(ALERT_TEST, 0x0C)
+    FIELD(ALERT_TEST, FATAL_FAULT, 0, 1)
 REG32(CTRL, 0x10)
-    FIELD(CTRL, TX_ENABLE, 0, 1)
-    FIELD(CTRL, RX_ENABLE, 1, 1)
+    FIELD(CTRL, TX, 0, 1)
+    FIELD(CTRL, RX, 1, 1)
     FIELD(CTRL, NF, 2, 1)
     FIELD(CTRL, SLPBK, 4, 1)
     FIELD(CTRL, LLPBK, 5, 1)
@@ -58,43 +65,74 @@ REG32(STATUS, 0x14)
     FIELD(STATUS, TXFULL, 0, 1)
     FIELD(STATUS, RXFULL, 1, 1)
     FIELD(STATUS, TXEMPTY, 2, 1)
+    FIELD(STATUS, TXIDLE, 3, 1)
     FIELD(STATUS, RXIDLE, 4, 1)
     FIELD(STATUS, RXEMPTY, 5, 1)
 REG32(RDATA, 0x18)
+    FIELD(RDATA, RDATA, 0, 8)
 REG32(WDATA, 0x1C)
+    FIELD(WDATA, WDATA, 0, 8)
 REG32(FIFO_CTRL, 0x20)
     FIELD(FIFO_CTRL, RXRST, 0, 1)
     FIELD(FIFO_CTRL, TXRST, 1, 1)
     FIELD(FIFO_CTRL, RXILVL, 2, 3)
-    FIELD(FIFO_CTRL, TXILVL, 5, 2)
+    FIELD(FIFO_CTRL, TXILVL, 5, 3)
 REG32(FIFO_STATUS, 0x24)
-    FIELD(FIFO_STATUS, TXLVL, 0, 5)
-    FIELD(FIFO_STATUS, RXLVL, 16, 5)
+    FIELD(FIFO_STATUS, TXLVL, 0, 8)
+    FIELD(FIFO_STATUS, RXLVL, 16, 8)
 REG32(OVRD, 0x28)
+    FIELD(OVRD, TXEN, 0, 1)
+    FIELD(OVRD, TXVAL, 1, 1)
 REG32(VAL, 0x2C)
+    FIELD(VAL, RX, 0, 16)
 REG32(TIMEOUT_CTRL, 0x30)
+    FIELD(TIMEOUT_CTRL, VAL, 0, 24)
+    FIELD(TIMEOUT_CTRL, EN, 31, 1)
+
+#define INTR_MASK \
+    (INTR_TX_WATERMARK_MASK | INTR_RX_WATERMARK_MASK | INTR_TX_DONE_MASK | \
+     INTR_RX_OVERFLOW_MASK | INTR_RX_FRAME_ERR_MASK | INTR_RX_BREAK_ERR_MASK | \
+     INTR_RX_TIMEOUT_MASK | INTR_RX_PARITY_ERR_MASK | INTR_TX_EMPTY_MASK)
+
+#define CTRL_MASK \
+    (R_CTRL_TX_MASK | R_CTRL_RX_MASK | R_CTRL_NF_MASK | R_CTRL_SLPBK_MASK | \
+     R_CTRL_LLPBK_MASK | R_CTRL_PARITY_EN_MASK | R_CTRL_PARITY_ODD_MASK | \
+     R_CTRL_RXBLVL_MASK | R_CTRL_NCO_MASK)
+
+#define OT_UART_NCO_BITS     16
+#define OT_UART_TX_FIFO_SIZE 128
+#define OT_UART_RX_FIFO_SIZE 128
+
+#define R32_OFF(_r_) ((_r_) / sizeof(uint32_t))
+
+#define R_LAST_REG (R_TIMEOUT_CTRL)
+#define REGS_COUNT (R_LAST_REG + 1u)
+#define REGS_SIZE  (REGS_COUNT * sizeof(uint32_t))
 
 static void ot_uart_update_irqs(OtUARTState *s)
 {
-    if (s->uart_intr_state & s->uart_intr_enable & R_INTR_STATE_TX_WATERMARK_MASK) {
+    if (s->regs[R_INTR_STATE] & s->regs[R_INTR_ENABLE]
+        & INTR_TX_WATERMARK_MASK) {
         qemu_set_irq(s->tx_watermark, 1);
     } else {
         qemu_set_irq(s->tx_watermark, 0);
     }
 
-    if (s->uart_intr_state & s->uart_intr_enable & R_INTR_STATE_RX_WATERMARK_MASK) {
+    if (s->regs[R_INTR_STATE] & s->regs[R_INTR_ENABLE]
+        & INTR_RX_WATERMARK_MASK) {
         qemu_set_irq(s->rx_watermark, 1);
     } else {
         qemu_set_irq(s->rx_watermark, 0);
     }
 
-    if (s->uart_intr_state & s->uart_intr_enable & R_INTR_STATE_TX_EMPTY_MASK) {
+    if (s->regs[R_INTR_STATE] & s->regs[R_INTR_ENABLE] & INTR_TX_EMPTY_MASK) {
         qemu_set_irq(s->tx_empty, 1);
     } else {
         qemu_set_irq(s->tx_empty, 0);
     }
 
-    if (s->uart_intr_state & s->uart_intr_enable & R_INTR_STATE_RX_OVERFLOW_MASK) {
+    if (s->regs[R_INTR_STATE] & s->regs[R_INTR_ENABLE]
+        & INTR_RX_OVERFLOW_MASK) {
         qemu_set_irq(s->rx_overflow, 1);
     } else {
         qemu_set_irq(s->rx_overflow, 0);
@@ -105,122 +143,133 @@ static int ot_uart_can_receive(void *opaque)
 {
     OtUARTState *s = opaque;
 
-    if ((s->uart_ctrl & R_CTRL_RX_ENABLE_MASK)
-           && !(s->uart_status & R_STATUS_RXFULL_MASK)) {
-        return 1;
+    if (s->regs[R_CTRL] & R_CTRL_RX_MASK) {
+        return (int)fifo8_num_free(&s->rx_fifo);
     }
 
     return 0;
 }
 
+static uint32_t ot_uart_get_rx_watermark_level(const OtUARTState *s)
+{
+    uint32_t rx_ilvl = FIELD_EX32(s->regs[R_FIFO_CTRL], FIFO_CTRL, RXILVL);
+
+    return rx_ilvl < 7 ? (1 << rx_ilvl) : 126;
+}
+
 static void ot_uart_receive(void *opaque, const uint8_t *buf, int size)
 {
     OtUARTState *s = opaque;
-    uint8_t rx_fifo_level = (s->uart_fifo_ctrl & R_FIFO_CTRL_RXILVL_MASK)
-                            >> R_FIFO_CTRL_RXILVL_SHIFT;
-
-    s->uart_rdata = *buf;
+    uint32_t rx_watermark_level;
+    size_t count = MIN(fifo8_num_free(&s->rx_fifo), (size_t)size);
 
-    s->uart_status &= ~R_STATUS_RXIDLE_MASK;
-    s->uart_status &= ~R_STATUS_RXEMPTY_MASK;
-    /* The RXFULL is set after receiving a single byte
-     * as the FIFO buffers are not yet implemented.
-     */
-    s->uart_status |= R_STATUS_RXFULL_MASK;
-    s->rx_level += 1;
+    for (int index = 0; index < size; index++) {
+        fifo8_push(&s->rx_fifo, buf[index]);
+    }
 
-    if (size > rx_fifo_level) {
-        s->uart_intr_state |= R_INTR_STATE_RX_WATERMARK_MASK;
+    /* update INTR_STATE */
+    if (count != size) {
+        s->regs[R_INTR_STATE] |= INTR_RX_OVERFLOW_MASK;
+    }
+    rx_watermark_level = ot_uart_get_rx_watermark_level(s);
+    if (rx_watermark_level && size >= rx_watermark_level) {
+        s->regs[R_INTR_STATE] |= INTR_RX_WATERMARK_MASK;
     }
 
     ot_uart_update_irqs(s);
 }
 
-static gboolean ot_uart_xmit(void *do_not_use, GIOCondition cond,
-                             void *opaque)
+static void ot_uart_reset_tx_fifo(OtUARTState *s)
 {
-    OtUARTState *s = opaque;
-    uint8_t tx_fifo_level = (s->uart_fifo_ctrl & R_FIFO_CTRL_TXILVL_MASK)
-                            >> R_FIFO_CTRL_TXILVL_SHIFT;
-    int ret;
-
-    /* instant drain the fifo when there's no back-end */
-    if (!qemu_chr_fe_backend_connected(&s->chr)) {
-        s->tx_level = 0;
-        return G_SOURCE_REMOVE;
+    fifo8_reset(&s->tx_fifo);
+    s->regs[R_INTR_STATE] |= INTR_TX_EMPTY_MASK;
+    s->regs[R_INTR_STATE] |= INTR_TX_DONE_MASK;
+    if (s->tx_watermark_level) {
+        s->regs[R_INTR_STATE] |= INTR_TX_WATERMARK_MASK;
+        s->tx_watermark_level = 0;
     }
+}
 
-    if (!s->tx_level) {
-        s->uart_status &= ~R_STATUS_TXFULL_MASK;
-        s->uart_status |= R_STATUS_TXEMPTY_MASK;
-        s->uart_intr_state |= R_INTR_STATE_TX_EMPTY_MASK;
-        s->uart_intr_state &= ~R_INTR_STATE_TX_WATERMARK_MASK;
-        ot_uart_update_irqs(s);
-        return G_SOURCE_REMOVE;
+static void ot_uart_reset_rx_fifo(OtUARTState *s)
+{
+    fifo8_reset(&s->rx_fifo);
+    s->regs[R_INTR_STATE] &= ~INTR_RX_WATERMARK_MASK;
+    s->regs[R_INTR_STATE] &= ~INTR_RX_OVERFLOW_MASK;
+    if (FIELD_EX32(s->regs[R_CTRL], CTRL, RX)) {
+        qemu_chr_fe_accept_input(&s->chr);
     }
+}
 
-    ret = qemu_chr_fe_write(&s->chr, s->tx_fifo, s->tx_level);
+static uint32_t ot_uart_get_tx_watermark_level(const OtUARTState *s)
+{
+    uint32_t tx_ilvl = FIELD_EX32(s->regs[R_FIFO_CTRL], FIFO_CTRL, TXILVL);
 
-    if (ret >= 0) {
-        s->tx_level -= ret;
-        memmove(s->tx_fifo, s->tx_fifo + ret, s->tx_level);
-    }
+    return tx_ilvl < 7 ? (1 << tx_ilvl) : 64;
+}
 
-    if (s->tx_level) {
-        guint r = qemu_chr_fe_add_watch(&s->chr, G_IO_OUT | G_IO_HUP,
-                                        ot_uart_xmit, s);
-        if (!r) {
-            s->tx_level = 0;
-            return G_SOURCE_REMOVE;
-        }
+static void ot_uart_xmit(OtUARTState *s)
+{
+    const uint8_t *buf;
+    uint32_t size;
+    int ret;
+
+    if (fifo8_is_empty(&s->tx_fifo)) {
+        return;
     }
 
-    /* Clear the TX Full bit */
-    if (s->tx_level != OT_UART_TX_FIFO_SIZE) {
-        s->uart_status &= ~R_STATUS_TXFULL_MASK;
+    /* instant drain the fifo when there's no back-end */
+    if (!qemu_chr_fe_backend_connected(&s->chr)) {
+        ot_uart_reset_tx_fifo(s);
+        ot_uart_update_irqs(s);
+        return;
     }
 
-    /* Disable the TX_WATERMARK IRQ */
-    if (s->tx_level < tx_fifo_level) {
-        s->uart_intr_state &= ~R_INTR_STATE_TX_WATERMARK_MASK;
+    /* get a continuous buffer from the FIFO */
+    buf =
+        fifo8_peek_bufptr(&s->tx_fifo, fifo8_num_used(&s->tx_fifo), &size);
+    /* send as much as possible */
+    ret = qemu_chr_fe_write(&s->chr, buf, (int)size);
+    /* if some characters where sent, remove them from the FIFO */
+    if (ret >= 0) {
+        fifo8_drop(&s->tx_fifo, ret);
     }
 
-    /* Set TX empty */
-    if (s->tx_level == 0) {
-        s->uart_status |= R_STATUS_TXEMPTY_MASK;
-        s->uart_intr_state |= R_INTR_STATE_TX_EMPTY_MASK;
+    /* update INTR_STATE */
+    if (fifo8_is_empty(&s->tx_fifo)) {
+        s->regs[R_INTR_STATE] |= INTR_TX_EMPTY_MASK;
+        s->regs[R_INTR_STATE] |= INTR_TX_DONE_MASK;
+    }
+    if (s->tx_watermark_level &&
+        fifo8_num_used(&s->tx_fifo) < s->tx_watermark_level) {
+        s->regs[R_INTR_STATE] |= INTR_TX_WATERMARK_MASK;
+        s->tx_watermark_level = 0;
     }
 
     ot_uart_update_irqs(s);
-    return G_SOURCE_REMOVE;
 }
 
-static void uart_write_tx_fifo(OtUARTState *s, const uint8_t *buf,
-                               int size)
+static void uart_write_tx_fifo(OtUARTState *s, uint8_t val)
 {
     uint64_t current_time = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
-    uint8_t tx_fifo_level = (s->uart_fifo_ctrl & R_FIFO_CTRL_TXILVL_MASK)
-                            >> R_FIFO_CTRL_TXILVL_SHIFT;
-
-    if (size > OT_UART_TX_FIFO_SIZE - s->tx_level) {
-        size = OT_UART_TX_FIFO_SIZE - s->tx_level;
+    if (fifo8_is_full(&s->tx_fifo)) {
         qemu_log_mask(LOG_GUEST_ERROR, "ot_uart: TX FIFO overflow");
+        return;
     }
 
-    memcpy(s->tx_fifo + s->tx_level, buf, size);
-    s->tx_level += size;
+    fifo8_push(&s->tx_fifo, val);
 
-    if (s->tx_level > 0) {
-        s->uart_status &= ~R_STATUS_TXEMPTY_MASK;
+    s->tx_watermark_level = ot_uart_get_tx_watermark_level(s);
+    if (fifo8_num_used(&s->tx_fifo) < s->tx_watermark_level) {
+        /*
+         * TX watermark interrupt is raised when FIFO depth goes from above
+         * watermark to below. If we haven't reached watermark, reset cached
+         * watermark level
+         */
+        s->tx_watermark_level = 0;
     }
 

Don't we need to update the interrupts at this point?

Quoted 1 line

Can you clarify what you mean by this? Are you wanting me to squash the next commit in to this one so that the IRQ change happens simultaneously with this one?

Basically you have updated a bunch of state that could cause an interrupt, but aren't checking if you should generate an interrupt. It looks like you will miss an interrupt here.

I'm not sure about squashing, but each commit should not break existing code, and it looks like this will break interrupts.

Alistair

#9 lore raw

PATCH v4 4/9 ot_uart: replace individual IRQ fields with array, add missing IRQs

In reply to ~lexbaileylowrisc

From: Lex Bailey <[email protected]>

There are 9 interrupts in the OpenTitan UART device. These are documented here:
https://opentitan.org/book/hw/ip/uart/doc/theory_of_operation.html#interrupts

This commit removes the individually named interrupts (of which there was only four) and replaces them with an array of 9 interrupts.

---
 hw/char/ot_uart.c         | 44 +++++++++++++--------------------------
 include/hw/char/ot_uart.h |  5 +----
 2 files changed, 15 insertions(+), 34 deletions(-)
hw/char/ot_uart.c +14 -30
index 3fe7c5efe5..62c182cbed 100644
@@ -102,6 +102,7 @@ REG32(TIMEOUT_CTRL, 0x30)
 #define OT_UART_NCO_BITS     16
 #define OT_UART_TX_FIFO_SIZE 128
 #define OT_UART_RX_FIFO_SIZE 128
+#define OT_UART_IRQ_NUM      9
 
 #define R32_OFF(_r_) ((_r_) / sizeof(uint32_t))
 
@@ -111,31 +112,11 @@ REG32(TIMEOUT_CTRL, 0x30)
 
 static void ot_uart_update_irqs(OtUARTState *s)
 {
-    if (s->regs[R_INTR_STATE] & s->regs[R_INTR_ENABLE]
-        & INTR_TX_WATERMARK_MASK) {
-        qemu_set_irq(s->tx_watermark, 1);
-    } else {
-        qemu_set_irq(s->tx_watermark, 0);
-    }
-
-    if (s->regs[R_INTR_STATE] & s->regs[R_INTR_ENABLE]
-        & INTR_RX_WATERMARK_MASK) {
-        qemu_set_irq(s->rx_watermark, 1);
-    } else {
-        qemu_set_irq(s->rx_watermark, 0);
-    }
+    uint32_t state_masked = s->regs[R_INTR_STATE] & s->regs[R_INTR_ENABLE];
 
-    if (s->regs[R_INTR_STATE] & s->regs[R_INTR_ENABLE] & INTR_TX_EMPTY_MASK) {
-        qemu_set_irq(s->tx_empty, 1);
-    } else {
-        qemu_set_irq(s->tx_empty, 0);
-    }
-
-    if (s->regs[R_INTR_STATE] & s->regs[R_INTR_ENABLE]
-        & INTR_RX_OVERFLOW_MASK) {
-        qemu_set_irq(s->rx_overflow, 1);
-    } else {
-        qemu_set_irq(s->rx_overflow, 0);
+    for (int index = 0; index < OT_UART_IRQ_NUM; index++) {
+        bool level = (state_masked & (1U << index)) != 0;
+        qemu_set_irq(s->irqs[index], level);
     }
 }
 
@@ -290,6 +271,9 @@ static void ot_uart_reset_enter(Object *obj, ResetType type)
     s->regs[R_STATUS] = 0x0000003c;
 
     s->tx_watermark_level = 0;
+    for (unsigned index = 0; index < ARRAY_SIZE(s->irqs); index++) {
+        qemu_set_irq(s->irqs[index], 0);
+    }
     ot_uart_reset_tx_fifo(s);
     ot_uart_reset_rx_fifo(s);
 
@@ -566,21 +550,21 @@ static void ot_uart_init(Object *obj)
                                   ot_uart_clk_update, s, ClockUpdate);
     clock_set_hz(s->f_clk, OT_UART_CLOCK);
 
-    sysbus_init_irq(SYS_BUS_DEVICE(obj), &s->tx_watermark);
-    sysbus_init_irq(SYS_BUS_DEVICE(obj), &s->rx_watermark);
-    sysbus_init_irq(SYS_BUS_DEVICE(obj), &s->tx_empty);
-    sysbus_init_irq(SYS_BUS_DEVICE(obj), &s->rx_overflow);
+    for (unsigned index = 0; index < OT_UART_IRQ_NUM; index++) {
+        sysbus_init_irq(SYS_BUS_DEVICE(obj), &s->irqs[index]);
+    }
 
     memory_region_init_io(&s->mmio, obj, &ot_uart_ops, s, TYPE_OT_UART,
                           REGS_SIZE);
     sysbus_init_mmio(SYS_BUS_DEVICE(obj), &s->mmio);
 
     /*
-     * This array has a fixed size in the header. This assertion is used to
-     * check that it is consistent with the definition in this file. This is
+     * These arrays have fixed sizes in the header. These assertions are used to
+     * check that they are consistent with the definitions in this file. This is
      * ostensibly a runtime check, but may be optimised away by the compiler.
      */
     assert(REGS_SIZE == sizeof(s->regs));
+    assert(OT_UART_IRQ_NUM * sizeof(qemu_irq) == sizeof(s->irqs));
 }
 
 static void ot_uart_realize(DeviceState *dev, Error **errp)
include/hw/char/ot_uart.h +1 -4
index f489612700..a2c5ff8b33 100644
@@ -42,6 +42,7 @@ struct OtUARTState {
 
     /* <public> */
     MemoryRegion mmio;
+    qemu_irq irqs[9];
 
     uint32_t tx_level;
 
@@ -59,10 +60,6 @@ struct OtUARTState {
     Clock *f_clk;
 
     CharFrontend chr;
-    qemu_irq tx_watermark;
-    qemu_irq rx_watermark;
-    qemu_irq tx_empty;
-    qemu_irq rx_overflow;
 };
 
 struct OtUARTClass {
Signature · 1 line
2.49.1
#10 lore raw

PATCH v4 5/9 ot_uart: gather similar behaviours togeter in register read and write

In reply to ~lexbaileylowrisc

From: Lex Bailey <[email protected]>

the register read and write functions are each a large switch statement with many similar behaviours in, this is mostly for enabling the use of specific register names inside of log messages. this commit adds a lookup table for register names and uses that instead, allowing much of the code in the register read and write functions to be deduplicated.

the write function was also missing a switch case for R_ALERT_TEST, so I added that in this commit since it has a very simple implementation for now

---
 hw/char/ot_uart.c | 103 ++++++++++++++++++++++++----------------------
 1 file changed, 53 insertions(+), 50 deletions(-)
hw/char/ot_uart.c +53 -50
index 62c182cbed..2247db7110 100644
@@ -109,6 +109,26 @@ REG32(TIMEOUT_CTRL, 0x30)
 #define R_LAST_REG (R_TIMEOUT_CTRL)
 #define REGS_COUNT (R_LAST_REG + 1u)
 #define REGS_SIZE  (REGS_COUNT * sizeof(uint32_t))
+#define REG_NAME(_reg_) \
+    ((((_reg_) < REGS_COUNT) && REG_NAMES[_reg_]) ? REG_NAMES[_reg_] : "?")
+
+#define REG_NAME_ENTRY(_reg_) [R_##_reg_] = stringify(_reg_)
+static const char *REG_NAMES[REGS_COUNT] = {
+    REG_NAME_ENTRY(INTR_STATE),
+    REG_NAME_ENTRY(INTR_ENABLE),
+    REG_NAME_ENTRY(INTR_TEST),
+    REG_NAME_ENTRY(ALERT_TEST),
+    REG_NAME_ENTRY(CTRL),
+    REG_NAME_ENTRY(STATUS),
+    REG_NAME_ENTRY(RDATA),
+    REG_NAME_ENTRY(WDATA),
+    REG_NAME_ENTRY(FIFO_CTRL),
+    REG_NAME_ENTRY(FIFO_STATUS),
+    REG_NAME_ENTRY(OVRD),
+    REG_NAME_ENTRY(VAL),
+    REG_NAME_ENTRY(TIMEOUT_CTRL),
+};
+#undef REG_NAME_ENTRY
 
 static void ot_uart_update_irqs(OtUARTState *s)
 {
@@ -301,23 +321,14 @@ static uint64_t ot_uart_read(void *opaque, hwaddr addr, unsigned int size)
     OtUARTState *s = opaque;
     uint64_t retvalue = 0;
 
-    switch (addr >> 2) {
+    hwaddr reg = R32_OFF(addr);
+    switch (reg) {
     case R_INTR_STATE:
-        retvalue = s->regs[R_INTR_STATE];
-        break;
     case R_INTR_ENABLE:
-        retvalue = s->regs[R_INTR_ENABLE];
-        break;
-    case R_INTR_TEST:
-        qemu_log_mask(LOG_GUEST_ERROR,
-                      "%s: wdata is write only\n", __func__);
-        break;
-
     case R_CTRL:
-        retvalue = s->regs[R_CTRL];
-        break;
+    case R_FIFO_CTRL:
     case R_STATUS:
-        retvalue = s->regs[R_STATUS];
+        retvalue = s->regs[reg];
         break;
 
     case R_RDATA:
@@ -333,14 +344,7 @@ static uint64_t ot_uart_read(void *opaque, hwaddr addr, unsigned int size)
             }
         }
         break;
-    case R_WDATA:
-        qemu_log_mask(LOG_GUEST_ERROR,
-                      "%s: wdata is write only\n", __func__);
-        break;
 
-    case R_FIFO_CTRL:
-        retvalue = s->regs[R_FIFO_CTRL];
-        break;
     case R_FIFO_STATUS:
         retvalue = s->regs[R_FIFO_STATUS];
 
@@ -351,21 +355,21 @@ static uint64_t ot_uart_read(void *opaque, hwaddr addr, unsigned int size)
                       "%s: RX fifos are not supported\n", __func__);
         break;
 
-    case R_OVRD:
-        retvalue = s->regs[R_OVRD];
-        qemu_log_mask(LOG_UNIMP,
-                      "%s: ovrd is not supported\n", __func__);
-        break;
     case R_VAL:
-        retvalue = s->regs[R_VAL];
-        qemu_log_mask(LOG_UNIMP,
-                      "%s: val is not supported\n", __func__);
-        break;
+    case R_OVRD:
     case R_TIMEOUT_CTRL:
-        retvalue = s->regs[R_TIMEOUT_CTRL];
+        retvalue = s->regs[reg];
         qemu_log_mask(LOG_UNIMP,
-                      "%s: timeout_ctrl is not supported\n", __func__);
+                      "%s: %s is not supported\n", __func__, REG_NAME(reg));
+        break;
+
+    case R_ALERT_TEST:
+    case R_INTR_TEST:
+    case R_WDATA:
+        qemu_log_mask(LOG_GUEST_ERROR,
+                      "%s: %s is write only\n", __func__, REG_NAME(reg));
         break;
+
     default:
         qemu_log_mask(LOG_GUEST_ERROR,
                       "%s: Bad offset 0x%"HWADDR_PRIx"\n", __func__, addr);
@@ -381,7 +385,9 @@ static void ot_uart_write(void *opaque, hwaddr addr, uint64_t val64,
     OtUARTState *s = opaque;
     uint32_t value = val64;
 
-    switch (addr >> 2) {
+    hwaddr reg = R32_OFF(addr);
+
+    switch (reg) {
     case R_INTR_STATE:
         /* Write 1 clear */
         value &= INTR_MASK;
@@ -398,7 +404,11 @@ static void ot_uart_write(void *opaque, hwaddr addr, uint64_t val64,
         s->regs[R_INTR_STATE] |= value;
         ot_uart_update_irqs(s);
         break;
-
+    case R_ALERT_TEST:
+        value &= R_ALERT_TEST_FATAL_FAULT_MASK;
+        s->regs[reg] = value;
+        /* This will also set an IRQ once the alert handler is added */
+        break;
     case R_CTRL:
         s->regs[R_CTRL] = value;
 
@@ -434,15 +444,6 @@ static void ot_uart_write(void *opaque, hwaddr addr, uint64_t val64,
             s->char_tx_time = (NANOSECONDS_PER_SECOND / baud) * 10;
         }
         break;
-    case R_STATUS:
-        qemu_log_mask(LOG_GUEST_ERROR,
-                      "%s: status is read only\n", __func__);
-        break;
-
-    case R_RDATA:
-        qemu_log_mask(LOG_GUEST_ERROR,
-                      "%s: rdata is read only\n", __func__);
-        break;
     case R_WDATA:
         uart_write_tx_fifo(s, value);
         break;
@@ -459,10 +460,6 @@ static void ot_uart_write(void *opaque, hwaddr addr, uint64_t val64,
             s->tx_level = 0;
         }
         break;
-    case R_FIFO_STATUS:
-        qemu_log_mask(LOG_GUEST_ERROR,
-                      "%s: fifo_status is read only\n", __func__);
-        break;
     case R_OVRD:
         if (value & R_OVRD_TXEN_MASK) {
             qemu_log_mask(LOG_UNIMP, "%s: OVRD.TXEN is not supported\n",
@@ -470,16 +467,22 @@ static void ot_uart_write(void *opaque, hwaddr addr, uint64_t val64,
         }
         s->regs[R_OVRD] = value & R_OVRD_TXVAL_MASK;
         break;
-    case R_VAL:
-        qemu_log_mask(LOG_GUEST_ERROR,
-                      "%s: val is read only\n", __func__);
-        break;
+
     case R_TIMEOUT_CTRL:
         s->regs[R_TIMEOUT_CTRL] =
             value & (R_TIMEOUT_CTRL_EN_MASK | R_TIMEOUT_CTRL_VAL_MASK);
         qemu_log_mask(LOG_UNIMP,
                       "%s: timeout_ctrl is not supported\n", __func__);
         break;
+
+    case R_STATUS:
+    case R_RDATA:
+    case R_FIFO_STATUS:
+    case R_VAL:
+        qemu_log_mask(LOG_GUEST_ERROR,
+                      "%s: %s is read only\n", __func__, REG_NAME(reg));
+        break;
+
     default:
         qemu_log_mask(LOG_GUEST_ERROR,
                       "%s: Bad offset 0x%"HWADDR_PRIx"\n", __func__, addr);
Signature · 1 line
2.49.1
Reviewed-by on v4 5/9
#11 lore raw

Re: [PATCH qemu v4 5/9] ot_uart: gather similar behaviours togeter in register read and write

Quoted 9 lines · ~lexbaileylowrisc

From: Lex Bailey <[email protected]>

the register read and write functions are each a large switch statement with many similar behaviours in, this is mostly for enabling the use of specific register names inside of log messages. this commit adds a lookup table for register names and uses that instead, allowing much of the code in the register read and write functions to be deduplicated.

Quoted 4 lines

the write function was also missing a switch case for R_ALERT_TEST, so I added that in this commit since it has a very simple implementation for now

Alistair

Quoted 193 lines
---
 hw/char/ot_uart.c | 103 ++++++++++++++++++++++++----------------------
 1 file changed, 53 insertions(+), 50 deletions(-)
hw/char/ot_uart.c +53 -51
index 62c182cbed..2247db7110 100644
@@ -109,6 +109,26 @@ REG32(TIMEOUT_CTRL, 0x30)
 #define R_LAST_REG (R_TIMEOUT_CTRL)
 #define REGS_COUNT (R_LAST_REG + 1u)
 #define REGS_SIZE  (REGS_COUNT * sizeof(uint32_t))
+#define REG_NAME(_reg_) \
+    ((((_reg_) < REGS_COUNT) && REG_NAMES[_reg_]) ? REG_NAMES[_reg_] : "?")
+
+#define REG_NAME_ENTRY(_reg_) [R_##_reg_] = stringify(_reg_)
+static const char *REG_NAMES[REGS_COUNT] = {
+    REG_NAME_ENTRY(INTR_STATE),
+    REG_NAME_ENTRY(INTR_ENABLE),
+    REG_NAME_ENTRY(INTR_TEST),
+    REG_NAME_ENTRY(ALERT_TEST),
+    REG_NAME_ENTRY(CTRL),
+    REG_NAME_ENTRY(STATUS),
+    REG_NAME_ENTRY(RDATA),
+    REG_NAME_ENTRY(WDATA),
+    REG_NAME_ENTRY(FIFO_CTRL),
+    REG_NAME_ENTRY(FIFO_STATUS),
+    REG_NAME_ENTRY(OVRD),
+    REG_NAME_ENTRY(VAL),
+    REG_NAME_ENTRY(TIMEOUT_CTRL),
+};
+#undef REG_NAME_ENTRY
 
 static void ot_uart_update_irqs(OtUARTState *s)
 {
@@ -301,23 +321,14 @@ static uint64_t ot_uart_read(void *opaque, hwaddr addr, unsigned int size)
     OtUARTState *s = opaque;
     uint64_t retvalue = 0;
 
-    switch (addr >> 2) {
+    hwaddr reg = R32_OFF(addr);
+    switch (reg) {
     case R_INTR_STATE:
-        retvalue = s->regs[R_INTR_STATE];
-        break;
     case R_INTR_ENABLE:
-        retvalue = s->regs[R_INTR_ENABLE];
-        break;
-    case R_INTR_TEST:
-        qemu_log_mask(LOG_GUEST_ERROR,
-                      "%s: wdata is write only\n", __func__);
-        break;
-
     case R_CTRL:
-        retvalue = s->regs[R_CTRL];
-        break;
+    case R_FIFO_CTRL:
     case R_STATUS:
-        retvalue = s->regs[R_STATUS];
+        retvalue = s->regs[reg];
         break;
 
     case R_RDATA:
@@ -333,14 +344,7 @@ static uint64_t ot_uart_read(void *opaque, hwaddr addr, unsigned int size)
             }
         }
         break;
-    case R_WDATA:
-        qemu_log_mask(LOG_GUEST_ERROR,
-                      "%s: wdata is write only\n", __func__);
-        break;
 
-    case R_FIFO_CTRL:
-        retvalue = s->regs[R_FIFO_CTRL];
-        break;
     case R_FIFO_STATUS:
         retvalue = s->regs[R_FIFO_STATUS];
 
@@ -351,21 +355,21 @@ static uint64_t ot_uart_read(void *opaque, hwaddr addr, unsigned int size)
                       "%s: RX fifos are not supported\n", __func__);
         break;
 
-    case R_OVRD:
-        retvalue = s->regs[R_OVRD];
-        qemu_log_mask(LOG_UNIMP,
-                      "%s: ovrd is not supported\n", __func__);
-        break;
     case R_VAL:
-        retvalue = s->regs[R_VAL];
-        qemu_log_mask(LOG_UNIMP,
-                      "%s: val is not supported\n", __func__);
-        break;
+    case R_OVRD:
     case R_TIMEOUT_CTRL:
-        retvalue = s->regs[R_TIMEOUT_CTRL];
+        retvalue = s->regs[reg];
         qemu_log_mask(LOG_UNIMP,
-                      "%s: timeout_ctrl is not supported\n", __func__);
+                      "%s: %s is not supported\n", __func__, REG_NAME(reg));
+        break;
+
+    case R_ALERT_TEST:
+    case R_INTR_TEST:
+    case R_WDATA:
+        qemu_log_mask(LOG_GUEST_ERROR,
+                      "%s: %s is write only\n", __func__, REG_NAME(reg));
         break;
+
     default:
         qemu_log_mask(LOG_GUEST_ERROR,
                       "%s: Bad offset 0x%"HWADDR_PRIx"\n", __func__, addr);
@@ -381,7 +385,9 @@ static void ot_uart_write(void *opaque, hwaddr addr, uint64_t val64,
     OtUARTState *s = opaque;
     uint32_t value = val64;
 
-    switch (addr >> 2) {
+    hwaddr reg = R32_OFF(addr);
+
+    switch (reg) {
     case R_INTR_STATE:
         /* Write 1 clear */
         value &= INTR_MASK;
@@ -398,7 +404,11 @@ static void ot_uart_write(void *opaque, hwaddr addr, uint64_t val64,
         s->regs[R_INTR_STATE] |= value;
         ot_uart_update_irqs(s);
         break;
-
+    case R_ALERT_TEST:
+        value &= R_ALERT_TEST_FATAL_FAULT_MASK;
+        s->regs[reg] = value;
+        /* This will also set an IRQ once the alert handler is added */
+        break;
     case R_CTRL:
         s->regs[R_CTRL] = value;
 
@@ -434,15 +444,6 @@ static void ot_uart_write(void *opaque, hwaddr addr, uint64_t val64,
             s->char_tx_time = (NANOSECONDS_PER_SECOND / baud) * 10;
         }
         break;
-    case R_STATUS:
-        qemu_log_mask(LOG_GUEST_ERROR,
-                      "%s: status is read only\n", __func__);
-        break;
-
-    case R_RDATA:
-        qemu_log_mask(LOG_GUEST_ERROR,
-                      "%s: rdata is read only\n", __func__);
-        break;
     case R_WDATA:
         uart_write_tx_fifo(s, value);
         break;
@@ -459,10 +460,6 @@ static void ot_uart_write(void *opaque, hwaddr addr, uint64_t val64,
             s->tx_level = 0;
         }
         break;
-    case R_FIFO_STATUS:
-        qemu_log_mask(LOG_GUEST_ERROR,
-                      "%s: fifo_status is read only\n", __func__);
-        break;
     case R_OVRD:
         if (value & R_OVRD_TXEN_MASK) {
             qemu_log_mask(LOG_UNIMP, "%s: OVRD.TXEN is not supported\n",
@@ -470,16 +467,22 @@ static void ot_uart_write(void *opaque, hwaddr addr, uint64_t val64,
         }
         s->regs[R_OVRD] = value & R_OVRD_TXVAL_MASK;
         break;
-    case R_VAL:
-        qemu_log_mask(LOG_GUEST_ERROR,
-                      "%s: val is read only\n", __func__);
-        break;
+
     case R_TIMEOUT_CTRL:
         s->regs[R_TIMEOUT_CTRL] =
             value & (R_TIMEOUT_CTRL_EN_MASK | R_TIMEOUT_CTRL_VAL_MASK);
         qemu_log_mask(LOG_UNIMP,
                       "%s: timeout_ctrl is not supported\n", __func__);
         break;
+
+    case R_STATUS:
+    case R_RDATA:
+    case R_FIFO_STATUS:
+    case R_VAL:
+        qemu_log_mask(LOG_GUEST_ERROR,
+                      "%s: %s is read only\n", __func__, REG_NAME(reg));
+        break;
+
     default:
         qemu_log_mask(LOG_GUEST_ERROR,
                       "%s: Bad offset 0x%"HWADDR_PRIx"\n", __func__, addr);
--

2.49.1

#12 lore raw

PATCH v4 6/9 ot_uart: implement RX fifo and loopback

In reply to ~lexbaileylowrisc

From: Lex Bailey <[email protected]>

The STATUS register value is now generated on every read instead of being tracked in memory since it has such a dynamic value. the receive fifo is now implemented, and the system loopback mode. (line loopback is not supported)

---
 hw/char/ot_uart.c | 160 ++++++++++++++++++++++++++++++++--------------
 1 file changed, 112 insertions(+), 48 deletions(-)
hw/char/ot_uart.c +112 -48
index 2247db7110..3511a42fbe 100644
@@ -140,6 +140,21 @@ static void ot_uart_update_irqs(OtUARTState *s)
     }
 }
 
+static bool ot_uart_is_sys_loopack_enabled(const OtUARTState *s)
+{
+    return FIELD_EX32(s->regs[R_CTRL], CTRL, SLPBK);
+}
+
+static bool ot_uart_is_tx_enabled(const OtUARTState *s)
+{
+    return FIELD_EX32(s->regs[R_CTRL], CTRL, TX);
+}
+
+static bool ot_uart_is_rx_enabled(const OtUARTState *s)
+{
+    return FIELD_EX32(s->regs[R_CTRL], CTRL, RX);
+}
+
 static int ot_uart_can_receive(void *opaque)
 {
     OtUARTState *s = opaque;
@@ -196,7 +211,7 @@ static void ot_uart_reset_rx_fifo(OtUARTState *s)
     fifo8_reset(&s->rx_fifo);
     s->regs[R_INTR_STATE] &= ~INTR_RX_WATERMARK_MASK;
     s->regs[R_INTR_STATE] &= ~INTR_RX_OVERFLOW_MASK;
-    if (FIELD_EX32(s->regs[R_CTRL], CTRL, RX)) {
+    if (ot_uart_is_rx_enabled(s)) {
         qemu_chr_fe_accept_input(&s->chr);
     }
 }
@@ -218,21 +233,37 @@ static void ot_uart_xmit(OtUARTState *s)
         return;
     }
 
-    /* instant drain the fifo when there's no back-end */
-    if (!qemu_chr_fe_backend_connected(&s->chr)) {
-        ot_uart_reset_tx_fifo(s);
-        ot_uart_update_irqs(s);
-        return;
-    }
+    if (ot_uart_is_sys_loopack_enabled(s)) {
+        /* system loopback mode, just forward to RX FIFO */
+        uint32_t count = fifo8_num_used(&s->tx_fifo);
+        buf = fifo8_pop_bufptr(&s->tx_fifo, count, &size);
+        ot_uart_receive(s, buf, (int)size);
+        count -= size;
+        /*
+         * there may be more data to send if data wraps around the end of TX
+         * FIFO
+         */
+        if (count) {
+            buf = fifo8_pop_bufptr(&s->tx_fifo, count, &size);
+            ot_uart_receive(s, buf, (int)size);
+        }
+    } else {
+        /* instant drain the fifo when there's no back-end */
+        if (!qemu_chr_fe_backend_connected(&s->chr)) {
+            ot_uart_reset_tx_fifo(s);
+            ot_uart_update_irqs(s);
+            return;
+        }
 
-    /* get a continuous buffer from the FIFO */
-    buf =
-        fifo8_peek_bufptr(&s->tx_fifo, fifo8_num_used(&s->tx_fifo), &size);
-    /* send as much as possible */
-    ret = qemu_chr_fe_write(&s->chr, buf, (int)size);
-    /* if some characters where sent, remove them from the FIFO */
-    if (ret >= 0) {
-        fifo8_drop(&s->tx_fifo, ret);
+        /* get a continuous buffer from the FIFO */
+        buf =
+            fifo8_peek_bufptr(&s->tx_fifo, fifo8_num_used(&s->tx_fifo), &size);
+        /* send as much as possible */
+        ret = qemu_chr_fe_write(&s->chr, buf, (int)size);
+        /* if some characters where sent, remove them from the FIFO */
+        if (ret >= 0) {
+            fifo8_drop(&s->tx_fifo, ret);
+        }
     }
 
     /* update INTR_STATE */
@@ -269,7 +300,7 @@ static void uart_write_tx_fifo(OtUARTState *s, uint8_t val)
         s->tx_watermark_level = 0;
     }
 
-    if (FIELD_EX32(s->regs[R_CTRL], CTRL, TX)) {
+    if (ot_uart_is_tx_enabled(s)) {
         ot_uart_xmit(s);
     }
 
@@ -288,8 +319,6 @@ static void ot_uart_reset_enter(Object *obj, ResetType type)
 
     memset(&s->regs[0], 0, sizeof(s->regs));
 
-    s->regs[R_STATUS] = 0x0000003c;
-
     s->tx_watermark_level = 0;
     for (unsigned index = 0; index < ARRAY_SIZE(s->irqs); index++) {
         qemu_set_irq(s->irqs[index], 0);
@@ -305,6 +334,27 @@ static void ot_uart_reset_enter(Object *obj, ResetType type)
     ot_uart_update_irqs(s);
 }
 
+static uint8_t ot_uart_read_rx_fifo(OtUARTState *s)
+{
+    uint8_t val;
+
+    if (!(s->regs[R_CTRL] & R_CTRL_RX_MASK)) {
+        return 0;
+    }
+
+    if (fifo8_is_empty(&s->rx_fifo)) {
+        return 0;
+    }
+
+    val = fifo8_pop(&s->rx_fifo);
+
+    if (ot_uart_is_rx_enabled(s) && !ot_uart_is_sys_loopack_enabled(s)) {
+        qemu_chr_fe_accept_input(&s->chr);
+    }
+
+    return val;
+}
+
 static uint64_t ot_uart_get_baud(OtUARTState *s)
 {
     uint64_t baud;
@@ -327,32 +377,50 @@ static uint64_t ot_uart_read(void *opaque, hwaddr addr, unsigned int size)
     case R_INTR_ENABLE:
     case R_CTRL:
     case R_FIFO_CTRL:
-    case R_STATUS:
         retvalue = s->regs[reg];
         break;
+    case R_STATUS:
+        /* assume that UART always report RXIDLE */
+        retvalue = R_STATUS_RXIDLE_MASK;
+        /* report RXEMPTY or RXFULL */
+        switch (fifo8_num_used(&s->rx_fifo)) {
+        case 0:
+            retvalue |= R_STATUS_RXEMPTY_MASK;
+            break;
+        case OT_UART_RX_FIFO_SIZE:
+            retvalue |= R_STATUS_RXFULL_MASK;
+            break;
+        default:
+            break;
+        }
+        /* report TXEMPTY+TXIDLE or TXFULL */
+        switch (fifo8_num_used(&s->tx_fifo)) {
+        case 0:
+            retvalue |= R_STATUS_TXEMPTY_MASK | R_STATUS_TXIDLE_MASK;
+            break;
+        case OT_UART_TX_FIFO_SIZE:
+            retvalue |= R_STATUS_TXFULL_MASK;
+            break;
+        default:
+            break;
+        }
+        if (!ot_uart_is_tx_enabled(s)) {
+            retvalue |= R_STATUS_TXIDLE_MASK;
+        }
+        if (!ot_uart_is_rx_enabled(s)) {
+            retvalue |= R_STATUS_RXIDLE_MASK;
+        }
+        break;
 
     case R_RDATA:
-        retvalue = s->regs[R_RDATA];
-        if ((s->regs[R_CTRL] & R_CTRL_RX_MASK) && (s->rx_level > 0)) {
-            qemu_chr_fe_accept_input(&s->chr);
-
-            s->rx_level -= 1;
-            s->regs[R_STATUS] &= ~R_STATUS_RXFULL_MASK;
-            if (s->rx_level == 0) {
-                s->regs[R_STATUS] |= R_STATUS_RXIDLE_MASK;
-                s->regs[R_STATUS] |= R_STATUS_RXEMPTY_MASK;
-            }
-        }
+        retvalue = (uint32_t)ot_uart_read_rx_fifo(s);
         break;
 
     case R_FIFO_STATUS:
-        retvalue = s->regs[R_FIFO_STATUS];
-
-        retvalue |= (s->rx_level & 0x1F) << R_FIFO_STATUS_RXLVL_SHIFT;
-        retvalue |= (s->tx_level & 0x1F) << R_FIFO_STATUS_TXLVL_SHIFT;
-
-        qemu_log_mask(LOG_UNIMP,
-                      "%s: RX fifos are not supported\n", __func__);
+        retvalue =
+            (fifo8_num_used(&s->rx_fifo) & 0xffu) << R_FIFO_STATUS_RXLVL_SHIFT;
+        retvalue |=
+            (fifo8_num_used(&s->tx_fifo) & 0xffu) << R_FIFO_STATUS_TXLVL_SHIFT;
         break;
 
     case R_VAL:
@@ -416,10 +484,6 @@ static void ot_uart_write(void *opaque, hwaddr addr, uint64_t val64,
             qemu_log_mask(LOG_UNIMP,
                           "%s: UART_CTRL_NF is not supported\n", __func__);
         }
-        if (value & R_CTRL_SLPBK_MASK) {
-            qemu_log_mask(LOG_UNIMP,
-                          "%s: UART_CTRL_SLPBK is not supported\n", __func__);
-        }
         if (value & R_CTRL_LLPBK_MASK) {
             qemu_log_mask(LOG_UNIMP,
                           "%s: UART_CTRL_LLPBK is not supported\n", __func__);
@@ -445,19 +509,19 @@ static void ot_uart_write(void *opaque, hwaddr addr, uint64_t val64,
         }
         break;
     case R_WDATA:
-        uart_write_tx_fifo(s, value);
+        uart_write_tx_fifo(s, (uint8_t)(value & R_WDATA_WDATA_MASK));
         break;
 
     case R_FIFO_CTRL:
-        s->regs[R_FIFO_CTRL] = value;
-
+        s->regs[R_FIFO_CTRL] =
+            value & (R_FIFO_CTRL_RXILVL_MASK | R_FIFO_CTRL_TXILVL_MASK);
         if (value & R_FIFO_CTRL_RXRST_MASK) {
-            s->rx_level = 0;
-            qemu_log_mask(LOG_UNIMP,
-                          "%s: RX fifos are not supported\n", __func__);
+            ot_uart_reset_rx_fifo(s);
+            ot_uart_update_irqs(s);
         }
         if (value & R_FIFO_CTRL_TXRST_MASK) {
-            s->tx_level = 0;
+            ot_uart_reset_tx_fifo(s);
+            ot_uart_update_irqs(s);
         }
         break;
     case R_OVRD:
Signature · 1 line
2.49.1
Reviewed-by on v4 6/9
#13 lore raw

Re: [PATCH qemu v4 6/9] ot_uart: implement RX fifo and loopback

Quoted 8 lines · ~lexbaileylowrisc

From: Lex Bailey <[email protected]>

The STATUS register value is now generated on every read instead of being tracked in memory since it has such a dynamic value. the receive fifo is now implemented, and the system loopback mode. (line loopback is not supported)

Alistair

Quoted 249 lines
---
 hw/char/ot_uart.c | 160 ++++++++++++++++++++++++++++++++--------------
 1 file changed, 112 insertions(+), 48 deletions(-)
hw/char/ot_uart.c +112 -49
index 2247db7110..3511a42fbe 100644
@@ -140,6 +140,21 @@ static void ot_uart_update_irqs(OtUARTState *s)
     }
 }
 
+static bool ot_uart_is_sys_loopack_enabled(const OtUARTState *s)
+{
+    return FIELD_EX32(s->regs[R_CTRL], CTRL, SLPBK);
+}
+
+static bool ot_uart_is_tx_enabled(const OtUARTState *s)
+{
+    return FIELD_EX32(s->regs[R_CTRL], CTRL, TX);
+}
+
+static bool ot_uart_is_rx_enabled(const OtUARTState *s)
+{
+    return FIELD_EX32(s->regs[R_CTRL], CTRL, RX);
+}
+
 static int ot_uart_can_receive(void *opaque)
 {
     OtUARTState *s = opaque;
@@ -196,7 +211,7 @@ static void ot_uart_reset_rx_fifo(OtUARTState *s)
     fifo8_reset(&s->rx_fifo);
     s->regs[R_INTR_STATE] &= ~INTR_RX_WATERMARK_MASK;
     s->regs[R_INTR_STATE] &= ~INTR_RX_OVERFLOW_MASK;
-    if (FIELD_EX32(s->regs[R_CTRL], CTRL, RX)) {
+    if (ot_uart_is_rx_enabled(s)) {
         qemu_chr_fe_accept_input(&s->chr);
     }
 }
@@ -218,21 +233,37 @@ static void ot_uart_xmit(OtUARTState *s)
         return;
     }
 
-    /* instant drain the fifo when there's no back-end */
-    if (!qemu_chr_fe_backend_connected(&s->chr)) {
-        ot_uart_reset_tx_fifo(s);
-        ot_uart_update_irqs(s);
-        return;
-    }
+    if (ot_uart_is_sys_loopack_enabled(s)) {
+        /* system loopback mode, just forward to RX FIFO */
+        uint32_t count = fifo8_num_used(&s->tx_fifo);
+        buf = fifo8_pop_bufptr(&s->tx_fifo, count, &size);
+        ot_uart_receive(s, buf, (int)size);
+        count -= size;
+        /*
+         * there may be more data to send if data wraps around the end of TX
+         * FIFO
+         */
+        if (count) {
+            buf = fifo8_pop_bufptr(&s->tx_fifo, count, &size);
+            ot_uart_receive(s, buf, (int)size);
+        }
+    } else {
+        /* instant drain the fifo when there's no back-end */
+        if (!qemu_chr_fe_backend_connected(&s->chr)) {
+            ot_uart_reset_tx_fifo(s);
+            ot_uart_update_irqs(s);
+            return;
+        }
 
-    /* get a continuous buffer from the FIFO */
-    buf =
-        fifo8_peek_bufptr(&s->tx_fifo, fifo8_num_used(&s->tx_fifo), &size);
-    /* send as much as possible */
-    ret = qemu_chr_fe_write(&s->chr, buf, (int)size);
-    /* if some characters where sent, remove them from the FIFO */
-    if (ret >= 0) {
-        fifo8_drop(&s->tx_fifo, ret);
+        /* get a continuous buffer from the FIFO */
+        buf =
+            fifo8_peek_bufptr(&s->tx_fifo, fifo8_num_used(&s->tx_fifo), &size);
+        /* send as much as possible */
+        ret = qemu_chr_fe_write(&s->chr, buf, (int)size);
+        /* if some characters where sent, remove them from the FIFO */
+        if (ret >= 0) {
+            fifo8_drop(&s->tx_fifo, ret);
+        }
     }
 
     /* update INTR_STATE */
@@ -269,7 +300,7 @@ static void uart_write_tx_fifo(OtUARTState *s, uint8_t val)
         s->tx_watermark_level = 0;
     }
 
-    if (FIELD_EX32(s->regs[R_CTRL], CTRL, TX)) {
+    if (ot_uart_is_tx_enabled(s)) {
         ot_uart_xmit(s);
     }
 
@@ -288,8 +319,6 @@ static void ot_uart_reset_enter(Object *obj, ResetType type)
 
     memset(&s->regs[0], 0, sizeof(s->regs));
 
-    s->regs[R_STATUS] = 0x0000003c;
-
     s->tx_watermark_level = 0;
     for (unsigned index = 0; index < ARRAY_SIZE(s->irqs); index++) {
         qemu_set_irq(s->irqs[index], 0);
@@ -305,6 +334,27 @@ static void ot_uart_reset_enter(Object *obj, ResetType type)
     ot_uart_update_irqs(s);
 }
 
+static uint8_t ot_uart_read_rx_fifo(OtUARTState *s)
+{
+    uint8_t val;
+
+    if (!(s->regs[R_CTRL] & R_CTRL_RX_MASK)) {
+        return 0;
+    }
+
+    if (fifo8_is_empty(&s->rx_fifo)) {
+        return 0;
+    }
+
+    val = fifo8_pop(&s->rx_fifo);
+
+    if (ot_uart_is_rx_enabled(s) && !ot_uart_is_sys_loopack_enabled(s)) {
+        qemu_chr_fe_accept_input(&s->chr);
+    }
+
+    return val;
+}
+
 static uint64_t ot_uart_get_baud(OtUARTState *s)
 {
     uint64_t baud;
@@ -327,32 +377,50 @@ static uint64_t ot_uart_read(void *opaque, hwaddr addr, unsigned int size)
     case R_INTR_ENABLE:
     case R_CTRL:
     case R_FIFO_CTRL:
-    case R_STATUS:
         retvalue = s->regs[reg];
         break;
+    case R_STATUS:
+        /* assume that UART always report RXIDLE */
+        retvalue = R_STATUS_RXIDLE_MASK;
+        /* report RXEMPTY or RXFULL */
+        switch (fifo8_num_used(&s->rx_fifo)) {
+        case 0:
+            retvalue |= R_STATUS_RXEMPTY_MASK;
+            break;
+        case OT_UART_RX_FIFO_SIZE:
+            retvalue |= R_STATUS_RXFULL_MASK;
+            break;
+        default:
+            break;
+        }
+        /* report TXEMPTY+TXIDLE or TXFULL */
+        switch (fifo8_num_used(&s->tx_fifo)) {
+        case 0:
+            retvalue |= R_STATUS_TXEMPTY_MASK | R_STATUS_TXIDLE_MASK;
+            break;
+        case OT_UART_TX_FIFO_SIZE:
+            retvalue |= R_STATUS_TXFULL_MASK;
+            break;
+        default:
+            break;
+        }
+        if (!ot_uart_is_tx_enabled(s)) {
+            retvalue |= R_STATUS_TXIDLE_MASK;
+        }
+        if (!ot_uart_is_rx_enabled(s)) {
+            retvalue |= R_STATUS_RXIDLE_MASK;
+        }
+        break;
 
     case R_RDATA:
-        retvalue = s->regs[R_RDATA];
-        if ((s->regs[R_CTRL] & R_CTRL_RX_MASK) && (s->rx_level > 0)) {
-            qemu_chr_fe_accept_input(&s->chr);
-
-            s->rx_level -= 1;
-            s->regs[R_STATUS] &= ~R_STATUS_RXFULL_MASK;
-            if (s->rx_level == 0) {
-                s->regs[R_STATUS] |= R_STATUS_RXIDLE_MASK;
-                s->regs[R_STATUS] |= R_STATUS_RXEMPTY_MASK;
-            }
-        }
+        retvalue = (uint32_t)ot_uart_read_rx_fifo(s);
         break;
 
     case R_FIFO_STATUS:
-        retvalue = s->regs[R_FIFO_STATUS];
-
-        retvalue |= (s->rx_level & 0x1F) << R_FIFO_STATUS_RXLVL_SHIFT;
-        retvalue |= (s->tx_level & 0x1F) << R_FIFO_STATUS_TXLVL_SHIFT;
-
-        qemu_log_mask(LOG_UNIMP,
-                      "%s: RX fifos are not supported\n", __func__);
+        retvalue =
+            (fifo8_num_used(&s->rx_fifo) & 0xffu) << R_FIFO_STATUS_RXLVL_SHIFT;
+        retvalue |=
+            (fifo8_num_used(&s->tx_fifo) & 0xffu) << R_FIFO_STATUS_TXLVL_SHIFT;
         break;
 
     case R_VAL:
@@ -416,10 +484,6 @@ static void ot_uart_write(void *opaque, hwaddr addr, uint64_t val64,
             qemu_log_mask(LOG_UNIMP,
                           "%s: UART_CTRL_NF is not supported\n", __func__);
         }
-        if (value & R_CTRL_SLPBK_MASK) {
-            qemu_log_mask(LOG_UNIMP,
-                          "%s: UART_CTRL_SLPBK is not supported\n", __func__);
-        }
         if (value & R_CTRL_LLPBK_MASK) {
             qemu_log_mask(LOG_UNIMP,
                           "%s: UART_CTRL_LLPBK is not supported\n", __func__);
@@ -445,19 +509,19 @@ static void ot_uart_write(void *opaque, hwaddr addr, uint64_t val64,
         }
         break;
     case R_WDATA:
-        uart_write_tx_fifo(s, value);
+        uart_write_tx_fifo(s, (uint8_t)(value & R_WDATA_WDATA_MASK));
         break;
 
     case R_FIFO_CTRL:
-        s->regs[R_FIFO_CTRL] = value;
-
+        s->regs[R_FIFO_CTRL] =
+            value & (R_FIFO_CTRL_RXILVL_MASK | R_FIFO_CTRL_TXILVL_MASK);
         if (value & R_FIFO_CTRL_RXRST_MASK) {
-            s->rx_level = 0;
-            qemu_log_mask(LOG_UNIMP,
-                          "%s: RX fifos are not supported\n", __func__);
+            ot_uart_reset_rx_fifo(s);
+            ot_uart_update_irqs(s);
         }
         if (value & R_FIFO_CTRL_TXRST_MASK) {
-            s->tx_level = 0;
+            ot_uart_reset_tx_fifo(s);
+            ot_uart_update_irqs(s);
         }
         break;
     case R_OVRD:
--

2.49.1

#14 lore raw

PATCH v4 7/9 ot_uart: handle break condition

In reply to ~lexbaileylowrisc

From: Lex Bailey <[email protected]>

What is a break event:
Normally, a UART wire is held high by the transmitting device any time the communication channel is idle (nothing to transmit). If a receiver does not see that its receive line is high, this means one of two things:
1. there is a transmission happening now
2. there is no transmitter connected, and the line is floating or pulled low, or the transmitting device is in a bad state or reset state and so it has not yet asserted its TX output.

In case 1, the line will go high again very soon.
In case 2, it is sometimes useful to detect that this is the case and signal to the software that this has happened (normally by means of an interrupt)

The ot_uart device does exactly this in case 2. It detects that this is happening by detecting when the RX pin stays low for an extended period. This period can be configured to be 2, 4, 8, or 16 times a character period. The character period itself depends on the configuration of the device (baud rate, and parity bit)

for more details about how this works, there is documentation about the UART break detection on this page:
https://opentitan.org/book/hw/ip/uart/doc/theory_of_operation.html#rx_break_err

This commit adds a handler for the UART break event, and logic to emulate a break when oversampling is enabled and there is a break event.

---
 docs/system/riscv/opentitan-uart.rst | 43 +++++++++++++
 hw/char/ot_uart.c                    | 92 +++++++++++++++++++++++++++-
 include/hw/char/ot_uart.h            |  4 ++
 3 files changed, 136 insertions(+), 3 deletions(-)
 create mode 100644 docs/system/riscv/opentitan-uart.rst
docs/system/riscv/opentitan-uart.rst new file +43 -0
new file mode 100644
index 0000000000..7ca596712d
@@ -0,0 +1,43 @@
+# OpenTitan UART Support
+
+## Connecting to the UART
+
+* `-serial mon:stdio`, used as the first `-serial` option, redirects the virtual UART0 to the
+  current console/shell.
+
+* `-chardev socket,id=serial1,host=localhost,port=8001,server=on,wait=off` and
+  `-serial chardev:serial1` can be used to redirect UART1 (in this example) to a TCP socket. These
+  options are not specific to OpenTitan emulation, but are useful to communicate over a UART.
+  Note that QEMU offers many `chardev` backends, please check QEMU documentation for details.
+
+## Sending Break Conditions
+
+Break conditions can be sent to the UART on select supported CharDev backends (telnet, mux)
+or by sending the `chardev-send-break` command with the CharDev ID via the QEMU Monitor.
+Break conditions are treated as transient events and the length of time of a break condition
+is not considered.
+
+## Oversampling
+
+OpenTitan's UART has a `VAL` register which oversamples the RX pin 16 times per bit.
+This cannot be emulated by QEMU which uses a CharDev backend and does not have a notion of
+accurate sampling times.
+
+If software wishes to poll the `VAL` register to determine break conditions, there are
+some properties available to help with emulating this use case:
+
+* `-global ot-uart.oversample-break=true` is used to enable UART break oversampling.
+  This will attempt to display 16 samples of the last bit received in the `VAL` register,
+  which will be 16 high bits after any UART frame is transmitted (as these end with a stop
+  bit, which is high), or 16 low bits if the UART previously received a break condition
+  and has not received any frames since. That is, enabling this property assumes that
+  transmitted break conditions are "held" until the next UART transfer in terms of what
+  is being shown in the oversampled `VAL` register.
+
+* `-global ot-uart.toggle-break=true` is used to provide more control over "holding"
+  the UART RX break condition like a GPIO strap, and changes the behavior of a UART
+  such that received break condition events now *toggle* the break condition state
+  rather than keeping it asserted until the next transfer. This allows any device talking
+  to OpenTitan via UART to have more precise control over when the UART VAL register
+  displays idle and when it displays a break condition, as it can precisely toggle the
+  break condition on or off like a GPIO strapping being held down.
hw/char/ot_uart.c +89 -3

133 lines not on this page · raw message

include/hw/char/ot_uart.h +4 -0
index a2c5ff8b33..997bf2f367 100644
@@ -56,10 +56,14 @@ struct OtUARTState {
     Fifo8 tx_fifo;
     Fifo8 rx_fifo;
     uint32_t tx_watermark_level;
+    bool in_break;
+    guint watch_tag;
 
     Clock *f_clk;
 
     CharFrontend chr;
+    bool oversample_break; /* Should mock break in the oversampled VAL reg? */
+    bool toggle_break; /* Are incoming breaks temporary or toggled? */
 };
 
 struct OtUARTClass {
Signature · 1 line
2.49.1
#15 lore raw

Re: [PATCH qemu v4 7/9] ot_uart: handle break condition

docs/system/riscv/opentitan-uart.rst

Quoted 42 lines · ~lexbaileylowrisc

From: Lex Bailey <[email protected]>

What is a break event:
Normally, a UART wire is held high by the transmitting device any time the communication channel is idle (nothing to transmit). If a receiver does not see that its receive line is high, this means one of two things:
1. there is a transmission happening now
2. there is no transmitter connected, and the line is floating or pulled low, or the transmitting device is in a bad state or reset state and so it has not yet asserted its TX output.

In case 1, the line will go high again very soon.
In case 2, it is sometimes useful to detect that this is the case and signal to the software that this has happened (normally by means of an interrupt)

The ot_uart device does exactly this in case 2. It detects that this is happening by detecting when the RX pin stays low for an extended period. This period can be configured to be 2, 4, 8, or 16 times a character period. The character period itself depends on the configuration of the device (baud rate, and parity bit)

for more details about how this works, there is documentation about the UART break detection on this page:
https://opentitan.org/book/hw/ip/uart/doc/theory_of_operation.html#rx_break_err

This commit adds a handler for the UART break event, and logic to emulate a break when oversampling is enabled and there is a break event.

---
 docs/system/riscv/opentitan-uart.rst | 43 +++++++++++++
 hw/char/ot_uart.c                    | 92 +++++++++++++++++++++++++++-
 include/hw/char/ot_uart.h            |  4 ++
 3 files changed, 136 insertions(+), 3 deletions(-)
 create mode 100644 docs/system/riscv/opentitan-uart.rst
docs/system/riscv/opentitan-uart.rst new file +0 -0
new file mode 100644
index 0000000000..7ca596712d

We should just add an OpenTitan Machine documentation here and include the UART there.

Quoted 147 lines
docs/system/riscv/opentitan-uart.rst new file +43 -0
@@ -0,0 +1,43 @@
+# OpenTitan UART Support
+
+## Connecting to the UART
+
+* `-serial mon:stdio`, used as the first `-serial` option, redirects the virtual UART0 to the
+  current console/shell.
+
+* `-chardev socket,id=serial1,host=localhost,port=8001,server=on,wait=off` and
+  `-serial chardev:serial1` can be used to redirect UART1 (in this example) to a TCP socket. These
+  options are not specific to OpenTitan emulation, but are useful to communicate over a UART.
+  Note that QEMU offers many `chardev` backends, please check QEMU documentation for details.
+
+## Sending Break Conditions
+
+Break conditions can be sent to the UART on select supported CharDev backends (telnet, mux)
+or by sending the `chardev-send-break` command with the CharDev ID via the QEMU Monitor.
+Break conditions are treated as transient events and the length of time of a break condition
+is not considered.
+
+## Oversampling
+
+OpenTitan's UART has a `VAL` register which oversamples the RX pin 16 times per bit.
+This cannot be emulated by QEMU which uses a CharDev backend and does not have a notion of
+accurate sampling times.
+
+If software wishes to poll the `VAL` register to determine break conditions, there are
+some properties available to help with emulating this use case:
+
+* `-global ot-uart.oversample-break=true` is used to enable UART break oversampling.
+  This will attempt to display 16 samples of the last bit received in the `VAL` register,
+  which will be 16 high bits after any UART frame is transmitted (as these end with a stop
+  bit, which is high), or 16 low bits if the UART previously received a break condition
+  and has not received any frames since. That is, enabling this property assumes that
+  transmitted break conditions are "held" until the next UART transfer in terms of what
+  is being shown in the oversampled `VAL` register.
+
+* `-global ot-uart.toggle-break=true` is used to provide more control over "holding"
+  the UART RX break condition like a GPIO strap, and changes the behavior of a UART
+  such that received break condition events now *toggle* the break condition state
+  rather than keeping it asserted until the next transfer. This allows any device talking
+  to OpenTitan via UART to have more precise control over when the UART VAL register
+  displays idle and when it displays a break condition, as it can precisely toggle the
+  break condition on or off like a GPIO strapping being held down.
hw/char/ot_uart.c +67 -0
index 3511a42fbe..cdf02da62b 100644
@@ -179,6 +179,11 @@ static void ot_uart_receive(void *opaque, const uint8_t *buf, int size)
     uint32_t rx_watermark_level;
     size_t count = MIN(fifo8_num_free(&s->rx_fifo), (size_t)size);
 
+    if (size && !s->toggle_break) {
+        /* no longer breaking, so emulate idle in oversampled VAL register */
+        s->in_break = false;
+    }
+
     for (int index = 0; index < size; index++) {
         fifo8_push(&s->rx_fifo, buf[index]);
     }
@@ -331,9 +336,40 @@ static void ot_uart_reset_enter(Object *obj, ResetType type)
 
     s->char_tx_time = (NANOSECONDS_PER_SECOND / 230400) * 10;
 
+    /*
+     * do not reset `s->in_break`, as that tracks whether we are currently
+     * receiving a break condition over UART RX from some device talking
+     * to OpenTitan, which should survive resets. The QEMU CharDev only
+     * supports transient break events and not the notion of holding the
+     * UART in break, so remembering breaks like this is required to
+     * support mocking of break conditions in the oversampled `VAL` reg.
+     */
+    if (s->in_break) {
+        /* ignore CTRL.RXBLVL as we have no notion of break "time" */
+        s->regs[R_INTR_STATE] |= INTR_RX_BREAK_ERR_MASK;
+    }
+
     ot_uart_update_irqs(s);
 }
 
+static void ot_uart_event_handler(void *opaque, QEMUChrEvent event)
+{
+    OtUARTState *s = opaque;
+
+    if (event == CHR_EVENT_BREAK) {
+        if (!s->in_break || !s->oversample_break) {
+            /* ignore CTRL.RXBLVL as we have no notion of break "time" */
+            s->regs[R_INTR_STATE] |= INTR_RX_BREAK_ERR_MASK;
+            ot_uart_update_irqs(s);
+            /* emulate break in the oversampled VAL register */
+            s->in_break = true;
+        } else if (s->toggle_break) {
+            /* emulate toggling break off in the oversampled VAL register */
+            s->in_break = false;
+        }
+    }
+}
+
 static uint8_t ot_uart_read_rx_fifo(OtUARTState *s)
 {
     uint8_t val;
@@ -355,6 +391,17 @@ static uint8_t ot_uart_read_rx_fifo(OtUARTState *s)
     return val;
 }
 
+static gboolean ot_uart_watch_cb(void *do_not_use, GIOCondition cond,
+                                 void *opaque)
+{
+    OtUARTState *s = opaque;
+
+    s->watch_tag = 0;
+    ot_uart_xmit(s);
+
+    return FALSE;
+}
+
 static uint64_t ot_uart_get_baud(OtUARTState *s)
 {
     uint64_t baud;
@@ -424,6 +471,26 @@ static uint64_t ot_uart_read(void *opaque, hwaddr addr, unsigned int size)
         break;
 
     case R_VAL:
+        /*
+         * This is not trivially implemented due to the QEMU UART
+         * interface. There is no way to reliably sample or oversample
+         * given our emulated interface, but some software might poll the
+         * value of this register to determine break conditions.
+         *
+         * As such, default to reporting 16 of the last sample received
+         * instead. This defaults to 16 idle high samples (as a stop bit is
+         * always the last received), except for when the `oversample-break`
+         * property is set and a break condition is received over UART RX,
+         * where we then show 16 low samples until the next valid UART
+         * transmission is received (or break is toggled off with the
+         * `toggle-break` property enabled). This will not be accurate, but
+         * should be sufficient to support basic software flows that
+         * essentially use UART break as a strapping mechanism.
+         */
+        retvalue = (s->in_break && s->oversample_break) ? 0u : UINT16_MAX;
+        qemu_log_mask(LOG_UNIMP, "%s: VAL only shows idle%s\n", __func__,
+                      (s->oversample_break ? "/break" : ""));
+        break;
     case R_OVRD:
     case R_TIMEOUT_CTRL:
         retvalue = s->regs[reg];

hw/char/ot_uart.c static const VMStateDescription vmstate_ot_uart = { · line 678

Quoted 5 lines
hw/char/ot_uart.c +2 -0
@@ -607,8 +674,27 @@ static const VMStateDescription vmstate_ot_uart = {
 
 static const Property ot_uart_properties[] = {
     DEFINE_PROP_CHR("chardev", OtUARTState, chr),
+    DEFINE_PROP_BOOL("oversample-break", OtUARTState, oversample_break, false),
+    DEFINE_PROP_BOOL("toggle-break", OtUARTState, toggle_break, false),

Why have these disabled by default?

Alistair

#16 lore raw

PATCH v4 8/9 ot_uart: update file headers with new authorship and documentation URL

In reply to ~lexbaileylowrisc

From: Lex Bailey <[email protected]>

The documentation has moved, URL updated.
This file is now a combination of code from various people/organisations. Took the opportunity while editing this to switch to just using the SPDX ID.

---
 hw/char/ot_uart.c         | 24 ++++++++----------------
 include/hw/char/ot_uart.h | 25 ++++++++++---------------
 2 files changed, 18 insertions(+), 31 deletions(-)
hw/char/ot_uart.c +8 -16
index cdf02da62b..d00f7bee66 100644
@@ -2,27 +2,19 @@
  * QEMU OpenTitan UART device
  *
  * Copyright (c) 2020 Western Digital
+ * Copyright (c) 2022-2025 Rivos, Inc.
+ * Copyright (c) 2025-2026 lowRISC contributors.
  *
  * For details check the documentation here:
- *    https://docs.opentitan.org/hw/ip/uart/doc/
+ *    https://opentitan.org/book/hw/ip/uart/doc/
  *
- * Permission is hereby granted, free of charge, to any person obtaining a copy
- * of this software and associated documentation files (the "Software"), to deal
- * in the Software without restriction, including without limitation the rights
- * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
- * copies of the Software, and to permit persons to whom the Software is
- * furnished to do so, subject to the following conditions:
+ * Author(s):
+ *  Alistair Francis <[email protected]>
+ *  Loïc Lefort <[email protected]>
+ *  Lex Bailey <[email protected]>
  *
- * The above copyright notice and this permission notice shall be included in
- * all copies or substantial portions of the Software.
+ * SPDX-License-Identifier: MIT
  *
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
- * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
- * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
- * THE SOFTWARE.
  */
 
 #include "qemu/osdep.h"
include/hw/char/ot_uart.h +10 -15
index 997bf2f367..4dc51142f7 100644
@@ -2,24 +2,19 @@
  * QEMU OpenTitan UART device
  *
  * Copyright (c) 2020 Western Digital
+ * Copyright (c) 2022-2025 Rivos, Inc.
+ * Copyright (c) 2025-2026 lowRISC contributors.
  *
- * Permission is hereby granted, free of charge, to any person obtaining a copy
- * of this software and associated documentation files (the "Software"), to deal
- * in the Software without restriction, including without limitation the rights
- * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
- * copies of the Software, and to permit persons to whom the Software is
- * furnished to do so, subject to the following conditions:
+ * For details check the documentation here:
+ *    https://opentitan.org/book/hw/ip/uart/doc/
  *
- * The above copyright notice and this permission notice shall be included in
- * all copies or substantial portions of the Software.
+ * Author(s):
+ *  Alistair Francis <[email protected]>
+ *  Loïc Lefort <[email protected]>
+ *  Lex Bailey <[email protected]>
+ *
+ * SPDX-License-Identifier: MIT
  *
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
- * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
- * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
- * THE SOFTWARE.
  */
 
 #ifndef HW_OT_UART_H
Signature · 1 line
2.49.1
#17 lore raw

PATCH v4 9/9 ot_uart: add tracing

In reply to ~lexbaileylowrisc

From: Lex Bailey <[email protected]>

Added some tracing to the OpenTitan UART for transparency when debugging

---
 hw/char/ot_uart.c         | 19 +++++++++++++++++++
 hw/char/trace-events      |  8 ++++++++
 hw/riscv/opentitan.c      |  1 +
 include/hw/char/ot_uart.h |  1 +
 4 files changed, 29 insertions(+)
hw/char/ot_uart.c +19 -0
index d00f7bee66..910bef00f2 100644
@@ -20,6 +20,7 @@
 #include "qemu/osdep.h"
 #include "hw/char/ot_uart.h"
 #include "qemu/fifo8.h"
+#include "hw/core/cpu.h"
 #include "hw/core/irq.h"
 #include "hw/core/qdev-clock.h"
 #include "hw/core/qdev-properties.h"
@@ -28,6 +29,7 @@
 #include "migration/vmstate.h"
 #include "qemu/log.h"
 #include "qemu/module.h"
+#include "trace.h"
 
 REG32(INTR_STATE, 0x00)
     SHARED_FIELD(INTR_TX_WATERMARK, 0, 1)
@@ -126,6 +128,9 @@ static void ot_uart_update_irqs(OtUARTState *s)
 {
     uint32_t state_masked = s->regs[R_INTR_STATE] & s->regs[R_INTR_ENABLE];
 
+    trace_ot_uart_irqs(s->ot_id, s->regs[R_INTR_STATE], s->regs[R_INTR_ENABLE],
+                       state_masked);
+
     for (int index = 0; index < OT_UART_IRQ_NUM; index++) {
         bool level = (state_masked & (1U << index)) != 0;
         qemu_set_irq(s->irqs[index], level);
@@ -402,6 +407,10 @@ static uint64_t ot_uart_get_baud(OtUARTState *s)
     baud *= clock_get_hz(s->f_clk);
     baud >>= 20;
 
+    if (baud) {
+        trace_ot_uart_check_baudrate(s->ot_id, baud);
+    }
+
     return baud;
 }
 
@@ -503,6 +512,10 @@ static uint64_t ot_uart_read(void *opaque, hwaddr addr, unsigned int size)
         return 0;
     }
 
+    uint32_t pc = current_cpu->cc->get_pc(current_cpu);
+    trace_ot_uart_io_read_out(s->ot_id, (uint32_t)addr, REG_NAME(reg),
+                              retvalue, pc);
+
     return retvalue;
 }
 
@@ -514,6 +527,9 @@ static void ot_uart_write(void *opaque, hwaddr addr, uint64_t val64,
 
     hwaddr reg = R32_OFF(addr);
 
+    uint32_t pc = current_cpu->cc->get_pc(current_cpu);
+    trace_ot_uart_io_write(s->ot_id, (uint32_t)addr, REG_NAME(reg), value, pc);
+
     switch (reg) {
     case R_INTR_STATE:
         /* Write 1 clear */
@@ -665,6 +681,7 @@ static const VMStateDescription vmstate_ot_uart = {
 };
 
 static const Property ot_uart_properties[] = {
+    DEFINE_PROP_STRING("ot-id", OtUARTState, ot_id),
     DEFINE_PROP_CHR("chardev", OtUARTState, chr),
     DEFINE_PROP_BOOL("oversample-break", OtUARTState, oversample_break, false),
     DEFINE_PROP_BOOL("toggle-break", OtUARTState, toggle_break, false),
@@ -716,6 +733,8 @@ static void ot_uart_realize(DeviceState *dev, Error **errp)
 {
     OtUARTState *s = OT_UART(dev);
 
+    g_assert(s->ot_id);
+
     s->fifo_trigger_handle = timer_new_ns(QEMU_CLOCK_VIRTUAL,
                                           fifo_trigger_update, s);
 
hw/char/trace-events +8 -0
index a3fcc77287..17cc9ad2d5 100644
@@ -141,3 +141,11 @@ stm32f2xx_usart_receive(char *id, uint8_t chr) " %s receiving '%c'"
 # riscv_htif.c
 htif_uart_write_to_host(uint8_t device, uint8_t cmd, uint64_t payload) "device: %u cmd: %02u payload: %016" PRIx64
 htif_uart_unknown_device_command(uint8_t device, uint8_t cmd, uint64_t payload) "device: %u cmd: %02u payload: %016" PRIx64
+
+# ot_uart.c
+ot_uart_check_baudrate(const char *id, unsigned baud) "%s: %u bps"
+ot_uart_connect_input_clock(const char *id, const char * srcname) "%s: %s"
+ot_uart_debug(const char *id, const char *msg) "%s: %s"
+ot_uart_io_read_out(const char *id, uint32_t addr, const char *regname, uint32_t val, uint32_t pc) "%s: addr=0x%02x (%s), val=0x%x, pc=0x%x"
+ot_uart_io_write(const char *id, uint32_t addr, const char *regname, uint32_t val, uint32_t pc) "%s: addr=0x%02x (%s), val=0x%x, pc=0x%x"
+ot_uart_irqs(const char *id, uint32_t active, uint32_t mask, uint32_t eff) "%s: act:0x%08x msk:0x%08x eff:0x%08x"
hw/riscv/opentitan.c +1 -0
index 97c33d1b53..163d3ac3d3 100644
@@ -133,6 +133,7 @@ static void lowrisc_ibex_soc_init(Object *obj)
     object_initialize_child(obj, "plic", &s->plic, TYPE_SIFIVE_PLIC);
 
     object_initialize_child(obj, "uart", &s->uart, TYPE_OT_UART);
+    object_property_set_str(OBJECT(&s->uart), "ot-id", "uart0", &error_fatal);
 
     object_initialize_child(obj, "timer", &s->timer, TYPE_IBEX_TIMER);
 
include/hw/char/ot_uart.h +1 -0
index 4dc51142f7..2f538e1f6f 100644
@@ -54,6 +54,7 @@ struct OtUARTState {
     bool in_break;
     guint watch_tag;
 
+    char *ot_id;
     Clock *f_clk;
 
     CharFrontend chr;
Signature · 1 line
2.49.1
#18 lore raw

Re: [PATCH qemu v4 9/9] ot_uart: add tracing

Quoted 76 lines · ~lexbaileylowrisc

From: Lex Bailey <[email protected]>

Added some tracing to the OpenTitan UART for transparency when debugging

---
 hw/char/ot_uart.c         | 19 +++++++++++++++++++
 hw/char/trace-events      |  8 ++++++++
 hw/riscv/opentitan.c      |  1 +
 include/hw/char/ot_uart.h |  1 +
 4 files changed, 29 insertions(+)
hw/char/ot_uart.c +16 -0
index d00f7bee66..910bef00f2 100644
@@ -20,6 +20,7 @@
 #include "qemu/osdep.h"
 #include "hw/char/ot_uart.h"
 #include "qemu/fifo8.h"
+#include "hw/core/cpu.h"
 #include "hw/core/irq.h"
 #include "hw/core/qdev-clock.h"
 #include "hw/core/qdev-properties.h"
@@ -28,6 +29,7 @@
 #include "migration/vmstate.h"
 #include "qemu/log.h"
 #include "qemu/module.h"
+#include "trace.h"
 
 REG32(INTR_STATE, 0x00)
     SHARED_FIELD(INTR_TX_WATERMARK, 0, 1)
@@ -126,6 +128,9 @@ static void ot_uart_update_irqs(OtUARTState *s)
 {
     uint32_t state_masked = s->regs[R_INTR_STATE] & s->regs[R_INTR_ENABLE];
 
+    trace_ot_uart_irqs(s->ot_id, s->regs[R_INTR_STATE], s->regs[R_INTR_ENABLE],
+                       state_masked);
+
     for (int index = 0; index < OT_UART_IRQ_NUM; index++) {
         bool level = (state_masked & (1U << index)) != 0;
         qemu_set_irq(s->irqs[index], level);
@@ -402,6 +407,10 @@ static uint64_t ot_uart_get_baud(OtUARTState *s)
     baud *= clock_get_hz(s->f_clk);
     baud >>= 20;
 
+    if (baud) {
+        trace_ot_uart_check_baudrate(s->ot_id, baud);
+    }
+
     return baud;
 }
 
@@ -503,6 +512,10 @@ static uint64_t ot_uart_read(void *opaque, hwaddr addr, unsigned int size)
         return 0;
     }
 
+    uint32_t pc = current_cpu->cc->get_pc(current_cpu);
+    trace_ot_uart_io_read_out(s->ot_id, (uint32_t)addr, REG_NAME(reg),
+                              retvalue, pc);
+
     return retvalue;
 }
 
@@ -514,6 +527,9 @@ static void ot_uart_write(void *opaque, hwaddr addr, uint64_t val64,
 
     hwaddr reg = R32_OFF(addr);
 
+    uint32_t pc = current_cpu->cc->get_pc(current_cpu);
+    trace_ot_uart_io_write(s->ot_id, (uint32_t)addr, REG_NAME(reg), value, pc);
+
     switch (reg) {
     case R_INTR_STATE:
         /* Write 1 clear */

hw/char/ot_uart.c static const VMStateDescription vmstate_ot_uart = { · line 684

Quoted 4 lines
hw/char/ot_uart.c +1 -0
@@ -665,6 +681,7 @@ static const VMStateDescription vmstate_ot_uart = {
 };
 
 static const Property ot_uart_properties[] = {
+    DEFINE_PROP_STRING("ot-id", OtUARTState, ot_id),

I'm not sure we need this

Alistair

Quoted 55 lines
hw/char/ot_uart.c +2 -0
     DEFINE_PROP_CHR("chardev", OtUARTState, chr),
     DEFINE_PROP_BOOL("oversample-break", OtUARTState, oversample_break, false),
     DEFINE_PROP_BOOL("toggle-break", OtUARTState, toggle_break, false),
@@ -716,6 +733,8 @@ static void ot_uart_realize(DeviceState *dev, Error **errp)
 {
     OtUARTState *s = OT_UART(dev);
 
+    g_assert(s->ot_id);
+
     s->fifo_trigger_handle = timer_new_ns(QEMU_CLOCK_VIRTUAL,
                                           fifo_trigger_update, s);
 
hw/char/trace-events +8 -0
index a3fcc77287..17cc9ad2d5 100644
@@ -141,3 +141,11 @@ stm32f2xx_usart_receive(char *id, uint8_t chr) " %s receiving '%c'"
 # riscv_htif.c
 htif_uart_write_to_host(uint8_t device, uint8_t cmd, uint64_t payload) "device: %u cmd: %02u payload: %016" PRIx64
 htif_uart_unknown_device_command(uint8_t device, uint8_t cmd, uint64_t payload) "device: %u cmd: %02u payload: %016" PRIx64
+
+# ot_uart.c
+ot_uart_check_baudrate(const char *id, unsigned baud) "%s: %u bps"
+ot_uart_connect_input_clock(const char *id, const char * srcname) "%s: %s"
+ot_uart_debug(const char *id, const char *msg) "%s: %s"
+ot_uart_io_read_out(const char *id, uint32_t addr, const char *regname, uint32_t val, uint32_t pc) "%s: addr=0x%02x (%s), val=0x%x, pc=0x%x"
+ot_uart_io_write(const char *id, uint32_t addr, const char *regname, uint32_t val, uint32_t pc) "%s: addr=0x%02x (%s), val=0x%x, pc=0x%x"
+ot_uart_irqs(const char *id, uint32_t active, uint32_t mask, uint32_t eff) "%s: act:0x%08x msk:0x%08x eff:0x%08x"
hw/riscv/opentitan.c +1 -0
index 97c33d1b53..163d3ac3d3 100644
@@ -133,6 +133,7 @@ static void lowrisc_ibex_soc_init(Object *obj)
     object_initialize_child(obj, "plic", &s->plic, TYPE_SIFIVE_PLIC);
 
     object_initialize_child(obj, "uart", &s->uart, TYPE_OT_UART);
+    object_property_set_str(OBJECT(&s->uart), "ot-id", "uart0", &error_fatal);
 
     object_initialize_child(obj, "timer", &s->timer, TYPE_IBEX_TIMER);
 
include/hw/char/ot_uart.h +1 -1
index 4dc51142f7..2f538e1f6f 100644
@@ -54,6 +54,7 @@ struct OtUARTState {
     bool in_break;
     guint watch_tag;
 
+    char *ot_id;
     Clock *f_clk;
 
     CharFrontend chr;
--

2.49.1