[rulkc] [PATCH] hw/watchdog: Add BE-U1000 watchdog timer model
Viktor Kutuzov
vk63200257 at gmail.com
Sun Nov 22 13:05:45 MSK 2026
Add a QEMU model of the BE-U1000 watchdog timer and wire both WDT0 and WDT1 into the be-u1000 SoC. On timeout it performs a system reset; interrupt mode is not yet modelled.
Signed-off-by: Viktor Kutuzov <vk63200257 at gmail.com>
---
hw/riscv/Kconfig | 1 +
hw/riscv/be-u1000.c | 21 ++-
hw/watchdog/Kconfig | 4 +
hw/watchdog/be_u1000_wdt.c | 239 +++++++++++++++++++++++++++++
hw/watchdog/meson.build | 1 +
hw/watchdog/trace-events | 8 +
include/hw/riscv/be-u1000.h | 2 +
include/hw/watchdog/be_u1000_wdt.h | 88 +++++++++++
8 files changed, 362 insertions(+), 2 deletions(-)
create mode 100644 hw/watchdog/be_u1000_wdt.c
create mode 100644 include/hw/watchdog/be_u1000_wdt.h
diff --git a/hw/riscv/Kconfig b/hw/riscv/Kconfig
index 71b88e7de1..3df27c4135 100644
--- a/hw/riscv/Kconfig
+++ b/hw/riscv/Kconfig
@@ -176,6 +176,7 @@ config BE_U1000
select RISCV_CLIC
select SPLIT_IRQ
select BE_U1000_UART
+ select BE_U1000_WDT
config EVU_BA
bool
diff --git a/hw/riscv/be-u1000.c b/hw/riscv/be-u1000.c
index 8c4fb0dd7f..f29ea3976e 100644
--- a/hw/riscv/be-u1000.c
+++ b/hw/riscv/be-u1000.c
@@ -293,6 +293,8 @@ static void be_u1000_soc_initfn(Object *obj)
TYPE_RISCV_CPU_CLOUDBEAR_BM310);
/* CRU (also owns Reset Controller duties: SYSCR0/1/2, see be-1000u-cru.h) */
object_initialize_child(obj, "cru", &s->cru, TYPE_BE1000U_CRU);
+ object_initialize_child(obj, "wdt0", &s->wdt[0], TYPE_BEU1000_WDT);
+ object_initialize_child(obj, "wdt1", &s->wdt[1], TYPE_BEU1000_WDT);
/* TODO: Init CLIC */
for (int i = 0; i < BEU1000_NUM_UART; i++) {
@@ -528,6 +530,23 @@ static void be_u1000_soc_realize(DeviceState *dev_soc, Error **errp)
qdev_get_gpio_in(DEVICE(&s->uart_irq_split[i]), 0));
}
+ /*
+ * WDT0 / WDT1: standalone watchdog timers. WDT response is a whole-chip
+ * (system) reset via watchdog_perform_action(), so no interrupt line is
+ * wired out to the CLICs.
+ */
+ sysbus_realize(SYS_BUS_DEVICE(&s->wdt[0]), errp);
+ memory_region_add_subregion(system_memory,
+ be_u1000_config[BE_U1000_IDX_WDT0].mm.base,
+ sysbus_mmio_get_region(
+ SYS_BUS_DEVICE(&s->wdt[0]), 0));
+
+ sysbus_realize(SYS_BUS_DEVICE(&s->wdt[1]), errp);
+ memory_region_add_subregion(system_memory,
+ be_u1000_config[BE_U1000_IDX_WDT1].mm.base,
+ sysbus_mmio_get_region(
+ SYS_BUS_DEVICE(&s->wdt[1]), 0));
+
UNIMPLEMENTED_DEV(BE_U1000_IDX_QSPI0);
UNIMPLEMENTED_DEV(BE_U1000_IDX_CORE2_DEBUG);
UNIMPLEMENTED_DEV(BE_U1000_IDX_CORE2_TRACE);
@@ -535,7 +554,6 @@ static void be_u1000_soc_realize(DeviceState *dev_soc, Error **errp)
UNIMPLEMENTED_DEV(BE_U1000_IDX_I2C1);
UNIMPLEMENTED_DEV(BE_U1000_IDX_I2S0);
UNIMPLEMENTED_DEV(BE_U1000_IDX_TIM0);
- UNIMPLEMENTED_DEV(BE_U1000_IDX_WDT0);
UNIMPLEMENTED_DEV(BE_U1000_IDX_GPIO0);
UNIMPLEMENTED_DEV(BE_U1000_IDX_PWMG0);
UNIMPLEMENTED_DEV(BE_U1000_IDX_SPI0);
@@ -546,7 +564,6 @@ static void be_u1000_soc_realize(DeviceState *dev_soc, Error **errp)
UNIMPLEMENTED_DEV(BE_U1000_IDX_I2C3);
UNIMPLEMENTED_DEV(BE_U1000_IDX_I2S1);
UNIMPLEMENTED_DEV(BE_U1000_IDX_TIM1);
- UNIMPLEMENTED_DEV(BE_U1000_IDX_WDT1);
UNIMPLEMENTED_DEV(BE_U1000_IDX_GPIO1);
UNIMPLEMENTED_DEV(BE_U1000_IDX_PWMG1);
UNIMPLEMENTED_DEV(BE_U1000_IDX_SPI2);
diff --git a/hw/watchdog/Kconfig b/hw/watchdog/Kconfig
index 55f77c5c84..6d8cd99742 100644
--- a/hw/watchdog/Kconfig
+++ b/hw/watchdog/Kconfig
@@ -22,6 +22,10 @@ config K230_WDT
bool
select PTIMER
+config BE_U1000_WDT
+ bool
+ select PTIMER
+
config WDT_SBSA
bool
diff --git a/hw/watchdog/be_u1000_wdt.c b/hw/watchdog/be_u1000_wdt.c
new file mode 100644
index 0000000000..a0bb7b4acb
--- /dev/null
+++ b/hw/watchdog/be_u1000_wdt.c
@@ -0,0 +1,239 @@
+/* SPDX-License-Identifier: GPL-2.0-or-later */
+/*
+ * QEMU model of the WDT on the BE-1000U series SOCs.
+ */
+
+#include "qemu/osdep.h"
+#include "qemu/bitops.h"
+#include "qemu/module.h"
+#include "system/watchdog.h"
+#include "migration/vmstate.h"
+#include "hw/watchdog/be_u1000_wdt.h"
+#include "trace.h"
+
+static uint32_t be_u1000_wdt_calculate_timeout(uint32_t top_value);
+
+static void be_u1000_wdt_timeout(void *opaque)
+{
+ BEU1000WdtState *s = BEU1000_WDT(opaque);
+
+ trace_be_u1000_wdt_timeout();
+
+ /* WDT response is always system reset. */
+ trace_be_u1000_wdt_reset();
+ watchdog_perform_action();
+
+ /* Restart counter */
+ s->current_count = s->timeout_value;
+ ptimer_set_count(s->timer, s->current_count);
+ ptimer_run(s->timer, 1);
+}
+
+static void be_u1000_wdt_reset(DeviceState *dev)
+{
+ BEU1000WdtState *s = BEU1000_WDT(dev);
+
+ trace_be_u1000_wdt_reset_device();
+
+ ptimer_transaction_begin(s->timer);
+ ptimer_stop(s->timer);
+ ptimer_transaction_commit(s->timer);
+
+ /* Reset registers to their datasheet power-on default values */
+ s->cr = BEU1000_WDT_RESET_CR;
+ s->torr = BEU1000_WDT_RESET_TORR;
+ s->stat = BEU1000_WDT_RESET_STAT;
+
+ s->enabled = false;
+ s->timeout_value = be_u1000_wdt_calculate_timeout(s->torr);
+ s->current_count = s->timeout_value;
+}
+
+static uint64_t be_u1000_wdt_read(void *opaque, hwaddr addr, unsigned int size)
+{
+ BEU1000WdtState *s = BEU1000_WDT(opaque);
+ uint32_t value = 0;
+
+ switch (addr) {
+ case BEU1000_WDT_CR:
+ value = s->cr;
+ break;
+ case BEU1000_WDT_TORR:
+ value = s->torr;
+ break;
+ case BEU1000_WDT_CCVR:
+ if (s->enabled) {
+ value = ptimer_get_count(s->timer);
+ } else {
+ value = s->current_count;
+ }
+ break;
+ case BEU1000_WDT_STAT:
+ value = s->stat;
+ break;
+ case BEU1000_WDT_EOI:
+ s->stat &= ~BEU1000_WDT_STAT_INT;
+ value = 0;
+ break;
+ case BEU1000_WDT_COMP_PARAM_5:
+ value = BEU1000_WDT_COMP_PARAM_5_VAL;
+ break;
+ case BEU1000_WDT_COMP_PARAM_4:
+ value = BEU1000_WDT_COMP_PARAM_4_VAL;
+ break;
+ case BEU1000_WDT_COMP_PARAM_3:
+ value = BEU1000_WDT_COMP_PARAM_3_VAL;
+ break;
+ case BEU1000_WDT_COMP_PARAM_2:
+ value = BEU1000_WDT_COMP_PARAM_2_VAL;
+ break;
+ case BEU1000_WDT_COMP_PARAM_1:
+ value = BEU1000_WDT_COMP_PARAM_1_VAL;
+ break;
+ default:
+ break;
+ }
+
+ trace_be_u1000_wdt_read(addr, value);
+ return value;
+}
+
+static void be_u1000_wdt_update_timer(BEU1000WdtState *s)
+{
+ ptimer_transaction_begin(s->timer);
+
+ if (s->enabled && s->timeout_value > 0) {
+ ptimer_set_count(s->timer, s->current_count);
+ ptimer_run(s->timer, 1);
+ } else {
+ ptimer_stop(s->timer);
+ }
+
+ ptimer_transaction_commit(s->timer);
+}
+
+static uint32_t be_u1000_wdt_calculate_timeout(uint32_t top_value)
+{
+ /*
+ * BE-U1000 timeout is two-segment:
+ * TOP 0-3 -> 2^(8 + TOP) - 1 cycles
+ * TOP 4-15 -> 2^(12 + TOP) - 1 cycles
+ */
+ uint32_t shift = (top_value < 4) ? (8u + top_value) : (12u + top_value);
+ return (1u << shift) - 1;
+}
+
+static void be_u1000_wdt_write(void *opaque, hwaddr addr,
+ uint64_t value, unsigned int size)
+{
+ BEU1000WdtState *s = BEU1000_WDT(opaque);
+
+ trace_be_u1000_wdt_write(addr, value);
+
+ switch (addr) {
+ case BEU1000_WDT_CR:
+ s->cr = value & (BEU1000_WDT_CR_RPL_MASK << BEU1000_WDT_CR_RPL_SHIFT |
+ BEU1000_WDT_CR_RMOD | BEU1000_WDT_CR_WDT_EN);
+
+ s->enabled = (s->cr & BEU1000_WDT_CR_WDT_EN) != 0;
+
+ be_u1000_wdt_update_timer(s);
+ break;
+
+ case BEU1000_WDT_TORR:
+ s->torr = value & BEU1000_WDT_TORR_TOP_MASK;
+
+ s->timeout_value = be_u1000_wdt_calculate_timeout(s->torr);
+ s->current_count = s->timeout_value;
+
+ if (s->enabled) {
+ be_u1000_wdt_update_timer(s);
+ }
+ break;
+
+ case BEU1000_WDT_CRR:
+ /* Restart counter only with magic value 0x76 */
+ if ((value & 0xFF) == BEU1000_WDT_CRR_RESTART) {
+ trace_be_u1000_wdt_restart();
+ s->current_count = s->timeout_value;
+
+ be_u1000_wdt_update_timer(s);
+ }
+ break;
+
+ default:
+ /* Read-only registers, ignore writes */
+ break;
+ }
+}
+
+static const MemoryRegionOps be_u1000_wdt_ops = {
+ .read = be_u1000_wdt_read,
+ .write = be_u1000_wdt_write,
+ .endianness = DEVICE_LITTLE_ENDIAN,
+ .impl = {
+ .min_access_size = 4,
+ .max_access_size = 4,
+ .unaligned = false,
+ },
+};
+
+static const VMStateDescription vmstate_be_u1000_wdt = {
+ .name = "be-u1000.wdt",
+ .fields = (const VMStateField[]) {
+ VMSTATE_PTIMER(timer, BEU1000WdtState),
+ VMSTATE_UINT32(cr, BEU1000WdtState),
+ VMSTATE_UINT32(torr, BEU1000WdtState),
+ VMSTATE_UINT32(stat, BEU1000WdtState),
+ VMSTATE_BOOL(enabled, BEU1000WdtState),
+ VMSTATE_UINT32(timeout_value, BEU1000WdtState),
+ VMSTATE_UINT32(current_count, BEU1000WdtState),
+ VMSTATE_END_OF_LIST()
+ }
+};
+
+static void be_u1000_wdt_realize(DeviceState *dev, Error **errp)
+{
+ BEU1000WdtState *s = BEU1000_WDT(dev);
+ SysBusDevice *sbd = SYS_BUS_DEVICE(dev);
+
+ memory_region_init_io(&s->mmio, OBJECT(dev),
+ &be_u1000_wdt_ops, s,
+ TYPE_BEU1000_WDT,
+ BEU1000_WDT_MMIO_SIZE);
+ sysbus_init_mmio(sbd, &s->mmio);
+
+ s->timer = ptimer_init(be_u1000_wdt_timeout, s,
+ PTIMER_POLICY_NO_IMMEDIATE_TRIGGER |
+ PTIMER_POLICY_NO_IMMEDIATE_RELOAD |
+ PTIMER_POLICY_NO_COUNTER_ROUND_DOWN);
+
+ ptimer_transaction_begin(s->timer);
+ ptimer_set_freq(s->timer, BEU1000_WDT_DEFAULT_FREQ);
+ ptimer_transaction_commit(s->timer);
+}
+
+static void be_u1000_wdt_class_init(ObjectClass *klass, const void *data)
+{
+ DeviceClass *dc = DEVICE_CLASS(klass);
+
+ dc->realize = be_u1000_wdt_realize;
+ device_class_set_legacy_reset(dc, be_u1000_wdt_reset);
+ dc->vmsd = &vmstate_be_u1000_wdt;
+ dc->desc = "BE-U1000 watchdog timer";
+ set_bit(DEVICE_CATEGORY_WATCHDOG, dc->categories);
+}
+
+static const TypeInfo be_u1000_wdt_info = {
+ .name = TYPE_BEU1000_WDT,
+ .parent = TYPE_SYS_BUS_DEVICE,
+ .instance_size = sizeof(BEU1000WdtState),
+ .class_init = be_u1000_wdt_class_init,
+};
+
+static void be_u1000_wdt_register_type(void)
+{
+ type_register_static(&be_u1000_wdt_info);
+}
+
+type_init(be_u1000_wdt_register_type)
diff --git a/hw/watchdog/meson.build b/hw/watchdog/meson.build
index 5edae65a36..57fc89d4f6 100644
--- a/hw/watchdog/meson.build
+++ b/hw/watchdog/meson.build
@@ -7,5 +7,6 @@ system_ss.add(when: 'CONFIG_WDT_DIAG288', if_true: files('wdt_diag288.c'))
system_ss.add(when: 'CONFIG_ASPEED_SOC', if_true: files('wdt_aspeed.c'))
system_ss.add(when: 'CONFIG_WDT_IMX2', if_true: files('wdt_imx2.c'))
system_ss.add(when: 'CONFIG_K230_WDT', if_true: files('k230_wdt.c'))
+system_ss.add(when: 'CONFIG_BE_U1000_WDT', if_true: files('be_u1000_wdt.c'))
system_ss.add(when: 'CONFIG_WDT_SBSA', if_true: files('sbsa_gwdt.c'))
specific_ss.add(when: 'CONFIG_PSERIES', if_true: files('spapr_watchdog.c'))
diff --git a/hw/watchdog/trace-events b/hw/watchdog/trace-events
index b388f7b250..f319b400b7 100644
--- a/hw/watchdog/trace-events
+++ b/hw/watchdog/trace-events
@@ -43,6 +43,14 @@ k230_wdt_reset(void) "K230 WDT system reset"
k230_wdt_restart(void) "K230 WDT restart"
k230_wdt_reset_device(void) "K230 WDT device reset"
+# be_u1000_wdt.c
+be_u1000_wdt_read(uint64_t addr, uint32_t data) "BE-U1000 WDT read: [0x%" PRIx64 "] -> 0x%" PRIx32
+be_u1000_wdt_write(uint64_t addr, uint64_t data) "BE-U1000 WDT write: [0x%" PRIx64 "] <- 0x%" PRIx64
+be_u1000_wdt_timeout(void) "BE-U1000 WDT timeout"
+be_u1000_wdt_reset(void) "BE-U1000 WDT whole-chip reset"
+be_u1000_wdt_restart(void) "BE-U1000 WDT restart"
+be_u1000_wdt_reset_device(void) "BE-U1000 WDT device reset"
+
#sbsa_gwdt.c
sbsa_gwdt_refresh_read(uint64_t addr, uint32_t value) "[0x%" PRIx64 "] -> 0x%" PRIx32
sbsa_gwdt_refresh_write(uint64_t addr, uint64_t value) "[0x%" PRIx64 "] <- 0x%" PRIx64
diff --git a/include/hw/riscv/be-u1000.h b/include/hw/riscv/be-u1000.h
index c4a7108ae1..fceb669fbf 100644
--- a/include/hw/riscv/be-u1000.h
+++ b/include/hw/riscv/be-u1000.h
@@ -18,6 +18,7 @@
#include "hw/char/be-u1000-uart.h"
#include "hw/intc/riscv_clic.h"
#include "hw/core/split-irq.h"
+#include "hw/watchdog/be_u1000_wdt.h"
#define TYPE_BEU1000_SOC "be-u1000-soc"
@@ -39,6 +40,7 @@ typedef struct BEU1000SocState {
SplitIRQ uart_irq_split[BEU1000_NUM_UART]; /* fans each UART's irq out
* to clic0+clic1
*/
+ BEU1000WdtState wdt[2]; /* WDT0..WDT1 */
char *console_uart; /* which uart[] slot backs serial_hd(0), e.g. "uart2" */
MemoryRegion cpu0_memory; /* cpu0's private root address space */
diff --git a/include/hw/watchdog/be_u1000_wdt.h b/include/hw/watchdog/be_u1000_wdt.h
new file mode 100644
index 0000000000..2e8e42dbb8
--- /dev/null
+++ b/include/hw/watchdog/be_u1000_wdt.h
@@ -0,0 +1,88 @@
+/* SPDX-License-Identifier: GPL-2.0-or-later */
+/*
+ * QEMU model of the WDT on the BE-1000U series SOCs.
+ *
+ * Currently the model implements only the reset response: on timeout it
+ * always triggers system reset via watchdog_perform_action().
+ * The interrupt mode is not modelled yet.
+ */
+
+#ifndef BE_U1000_WDT_H
+#define BE_U1000_WDT_H
+
+#include "qemu/bitops.h"
+#include "hw/core/sysbus.h"
+#include "hw/core/ptimer.h"
+#include "qom/object.h"
+
+#define TYPE_BEU1000_WDT "riscv.be-u1000.wdt"
+OBJECT_DECLARE_SIMPLE_TYPE(BEU1000WdtState, BEU1000_WDT)
+
+/* WDT counter clocks from PCLK0 */
+#define BEU1000_WDT_DEFAULT_FREQ (100000000)
+
+/* BE-U1000 Watchdog Register Map */
+enum BEU1000WdtRegisters {
+ BEU1000_WDT_CR = 0x00, /* Control Register */
+ BEU1000_WDT_TORR = 0x04, /* Timeout Range Register */
+ BEU1000_WDT_CCVR = 0x08, /* Current Counter Value Register */
+ BEU1000_WDT_CRR = 0x0c, /* Counter Restart Register */
+ BEU1000_WDT_STAT = 0x10, /* Interrupt Status Register */
+ BEU1000_WDT_EOI = 0x14, /* Interrupt Clear Register */
+ BEU1000_WDT_COMP_PARAM_5 = 0xE4, /* Component Parameters Register 5 */
+ BEU1000_WDT_COMP_PARAM_4 = 0xE8, /* Component Parameters Register 4 */
+ BEU1000_WDT_COMP_PARAM_3 = 0xEC, /* Component Parameters Register 3 */
+ BEU1000_WDT_COMP_PARAM_2 = 0xF0, /* Component Parameters Register 2 */
+ BEU1000_WDT_COMP_PARAM_1 = 0xF4, /* Component Parameters Register 1 */
+};
+
+#define BEU1000_WDT_MMIO_SIZE 0x1000
+
+/* Control Register (WDT_CR) definitions */
+#define BEU1000_WDT_CR_RPL_MASK 0x7 /* Reset Pulse Length */
+#define BEU1000_WDT_CR_RPL_SHIFT 2
+#define BEU1000_WDT_CR_RMOD BIT(1) /* Response Mode (ignored) */
+#define BEU1000_WDT_CR_WDT_EN BIT(0) /* Watchdog Enable */
+
+/* Timeout Range Register (WDT_TORR) definitions */
+#define BEU1000_WDT_TORR_TOP_MASK 0xf /* Timeout Period */
+
+/* Interrupt Status Register (WDT_STAT) definitions */
+#define BEU1000_WDT_STAT_INT BIT(0) /* Interrupt Status */
+
+/* Counter Restart Register (WDT_CRR) magic value */
+#define BEU1000_WDT_CRR_RESTART 0x76 /* Restart command */
+
+/* Hardware reset (power-on default) register values */
+#define BEU1000_WDT_RESET_CR 0x10
+#define BEU1000_WDT_RESET_TORR 0x4
+#define BEU1000_WDT_RESET_STAT 0x0
+
+/* Read-only component parameter register contents (WDT_COMP_PARAM_1..5) */
+#define BEU1000_WDT_COMP_PARAM_5_VAL 0x7FFFFFF /* CP_WDT_USER_TOP_MAX */
+#define BEU1000_WDT_COMP_PARAM_4_VAL 0x0 /* CP_WDT_USER_TOP_INIT_MAX */
+#define BEU1000_WDT_COMP_PARAM_3_VAL 0x4 /* CD_WDT_TOP_RST */
+#define BEU1000_WDT_COMP_PARAM_2_VAL 0xFFFF /* CP_WDT_CNT_RST */
+#define BEU1000_WDT_COMP_PARAM_1_VAL 0x10041280 /* packed IP parameters */
+
+struct BEU1000WdtState {
+ /* <private> */
+ SysBusDevice parent_obj;
+
+ /*< public >*/
+ MemoryRegion mmio;
+
+ struct ptimer_state *timer;
+
+ /* Register state */
+ uint32_t cr; /* Control Register */
+ uint32_t torr; /* Timeout Range Register */
+ uint32_t stat; /* Interrupt Status (STAT/EOI, reset only) */
+
+ /* Internal state */
+ bool enabled;
+ uint32_t timeout_value;
+ uint32_t current_count;
+};
+
+#endif /* BE_U1000_WDT_H */
--
2.43.0
More information about the rulkc
mailing list