From: Chao Liu <[email protected]>

Implement the HPTIMER control register enable bit used during firmware
boot and connect the device to the RK3588 SoC. Reset the enable state in
the device; the board supplies the firmware handoff state.

Signed-off-by: Chao Liu <[email protected]>
Signed-off-by: Yifan Wang <[email protected]>
---
 MAINTAINERS                         |   2 +
 hw/arm/rk3588.c                     |  23 ++++++
 hw/timer/meson.build                |   1 +
 hw/timer/rockchip-hptimer.c         | 115 ++++++++++++++++++++++++++++
 include/hw/arm/rk3588.h             |   2 +
 include/hw/timer/rockchip-hptimer.h |  20 +++++
 6 files changed, 163 insertions(+)
 create mode 100644 hw/timer/rockchip-hptimer.c
 create mode 100644 include/hw/timer/rockchip-hptimer.h

diff --git a/MAINTAINERS b/MAINTAINERS
index e7caabd565..4fc6f8e59d 100644
--- a/MAINTAINERS
+++ b/MAINTAINERS
@@ -1065,7 +1065,9 @@ M: Yifan Wang <[email protected]>
 L: [email protected]
 S: Maintained
 F: hw/arm/rk3588.c
+F: hw/timer/rockchip-hptimer.c
 F: include/hw/arm/rk3588.h
+F: include/hw/timer/rockchip-hptimer.h
 
 SABRELITE / i.MX6
 M: Peter Maydell <[email protected]>
diff --git a/hw/arm/rk3588.c b/hw/arm/rk3588.c
index 1e6f44433c..8255aee7fc 100644
--- a/hw/arm/rk3588.c
+++ b/hw/arm/rk3588.c
@@ -18,6 +18,7 @@
 #include "hw/core/qdev-properties.h"
 #include "hw/core/sysbus.h"
 #include "hw/cpu/cluster.h"
+#include "hw/timer/rockchip-hptimer.h"
 #include "migration/vmstate.h"
 
 /* MPIDR Aff0 values per Aff1: 1 (one CPU per RK3588 cluster). */
@@ -46,6 +47,20 @@ static const struct {
 
 G_STATIC_ASSERT(ARRAY_SIZE(rk3588_cpu_clusters) == RK3588_CLUSTER_COUNT);
 
+static bool rk3588_soc_create_hptimer(RK3588SoCState *s, Error **errp)
+{
+    SysBusDevice *hptimer = SYS_BUS_DEVICE(s->hptimer);
+
+    if (!sysbus_realize(hptimer, errp)) {
+        return false;
+    }
+
+    memory_region_add_subregion(s->memory, RK3588_HPTIMER_BASE,
+                                sysbus_mmio_get_region(hptimer, 0));
+
+    return true;
+}
+
 static void rk3588_soc_realize(DeviceState *dev, Error **errp)
 {
     RK3588SoCState *s = RK3588_SOC(dev);
@@ -82,6 +97,10 @@ static void rk3588_soc_realize(DeviceState *dev, Error 
**errp)
         }
     }
 
+    if (!rk3588_soc_create_hptimer(s, errp)) {
+        return;
+    }
+
     if (!memory_region_init_ram(&s->sram, OBJECT(s), "rk3588.sram",
                                 MiB, errp)) {
         return;
@@ -110,6 +129,10 @@ static void rk3588_soc_instance_init(Object *obj)
             n++;
         }
     }
+
+    s->hptimer = qdev_new(TYPE_ROCKCHIP_HPTIMER);
+    object_property_add_child(obj, "hptimer", OBJECT(s->hptimer));
+    object_unref(OBJECT(s->hptimer));
 }
 
 static const Property rk3588_soc_properties[] = {
diff --git a/hw/timer/meson.build b/hw/timer/meson.build
index e2bcdbdf65..5375f26b47 100644
--- a/hw/timer/meson.build
+++ b/hw/timer/meson.build
@@ -23,6 +23,7 @@ system_ss.add(when: 'CONFIG_NPCM7XX', if_true: 
files('npcm7xx_timer.c'))
 system_ss.add(when: 'CONFIG_NRF51_SOC', if_true: files('nrf51_timer.c'))
 system_ss.add(when: 'CONFIG_PXA2XX_TIMER', if_true: files('pxa2xx_timer.c'))
 system_ss.add(when: 'CONFIG_RASPI', if_true: files('bcm2835_systmr.c'))
+system_ss.add(when: 'CONFIG_RK3588', if_true: files('rockchip-hptimer.c'))
 system_ss.add(when: 'CONFIG_SH_TIMER', if_true: files('sh_timer.c'))
 system_ss.add(when: 'CONFIG_SLAVIO', if_true: files('slavio_timer.c'))
 system_ss.add(when: 'CONFIG_SSE_COUNTER', if_true: files('sse-counter.c'))
diff --git a/hw/timer/rockchip-hptimer.c b/hw/timer/rockchip-hptimer.c
new file mode 100644
index 0000000000..200894036c
--- /dev/null
+++ b/hw/timer/rockchip-hptimer.c
@@ -0,0 +1,115 @@
+/*
+ * Rockchip RK3588 HPTIMER boot control register
+ *
+ * Copyright (C) 2026 Process Mission
+ *
+ * SPDX-License-Identifier: GPL-2.0-or-later
+ */
+
+#include "qemu/osdep.h"
+#include "qemu/log.h"
+#include "qemu/module.h"
+#include "qemu/units.h"
+#include "hw/core/resettable.h"
+#include "hw/core/sysbus.h"
+#include "hw/timer/rockchip-hptimer.h"
+#include "migration/vmstate.h"
+
+/* Model the enable state used at firmware handoff; the CPU provides timing. */
+struct RockchipHPTimerState {
+    SysBusDevice parent_obj;
+    MemoryRegion iomem;
+    uint32_t ctrl;
+};
+
+static uint64_t rockchip_hptimer_read(void *opaque, hwaddr offset,
+                                     unsigned size)
+{
+    RockchipHPTimerState *s = ROCKCHIP_HPTIMER(opaque);
+
+    switch (offset) {
+    case ROCKCHIP_HPTIMER_CTRL:
+        return s->ctrl;
+    default:
+        qemu_log_mask(LOG_UNIMP, "rockchip-hptimer: unimplemented read at "
+                      "0x%" HWADDR_PRIx "\n", offset);
+        return 0;
+    }
+}
+
+static void rockchip_hptimer_write(void *opaque, hwaddr offset,
+                                  uint64_t value, unsigned size)
+{
+    RockchipHPTimerState *s = ROCKCHIP_HPTIMER(opaque);
+
+    switch (offset) {
+    case ROCKCHIP_HPTIMER_CTRL:
+        s->ctrl = value & ROCKCHIP_HPTIMER_ENABLE;
+        break;
+    default:
+        qemu_log_mask(LOG_UNIMP, "rockchip-hptimer: unimplemented write at "
+                      "0x%" HWADDR_PRIx "\n", offset);
+        break;
+    }
+}
+
+static const MemoryRegionOps rockchip_hptimer_ops = {
+    .read = rockchip_hptimer_read,
+    .write = rockchip_hptimer_write,
+    .endianness = DEVICE_LITTLE_ENDIAN,
+    .valid = {
+        .min_access_size = 4,
+        .max_access_size = 4,
+    },
+    .impl = {
+        .min_access_size = 4,
+        .max_access_size = 4,
+    },
+};
+
+static void rockchip_hptimer_reset_enter(Object *obj, ResetType type)
+{
+    RockchipHPTimerState *s = ROCKCHIP_HPTIMER(obj);
+
+    s->ctrl = 0;
+}
+
+static void rockchip_hptimer_init(Object *obj)
+{
+    RockchipHPTimerState *s = ROCKCHIP_HPTIMER(obj);
+
+    memory_region_init_io(&s->iomem, obj, &rockchip_hptimer_ops, s,
+                          TYPE_ROCKCHIP_HPTIMER, 16 * KiB);
+    sysbus_init_mmio(SYS_BUS_DEVICE(obj), &s->iomem);
+}
+
+static const VMStateDescription vmstate_rockchip_hptimer = {
+    .name = TYPE_ROCKCHIP_HPTIMER,
+    .unmigratable = true,
+};
+
+static void rockchip_hptimer_class_init(ObjectClass *oc, const void *data)
+{
+    DeviceClass *dc = DEVICE_CLASS(oc);
+    ResettableClass *rc = RESETTABLE_CLASS(oc);
+
+    rc->phases.enter = rockchip_hptimer_reset_enter;
+    dc->desc = "Rockchip RK3588 HPTIMER boot control";
+    dc->vmsd = &vmstate_rockchip_hptimer;
+    dc->user_creatable = false;
+}
+
+static const TypeInfo rockchip_hptimer_typeinfo = {
+    .name = TYPE_ROCKCHIP_HPTIMER,
+    .parent = TYPE_SYS_BUS_DEVICE,
+    .instance_size = sizeof(RockchipHPTimerState),
+    .instance_init = rockchip_hptimer_init,
+    .class_init = rockchip_hptimer_class_init,
+};
+
+static void rockchip_hptimer_register_types(void)
+{
+    type_register_static(&rockchip_hptimer_typeinfo);
+}
+
+type_init(rockchip_hptimer_register_types)
diff --git a/include/hw/arm/rk3588.h b/include/hw/arm/rk3588.h
index 450af3d282..8539d2e64d 100644
--- a/include/hw/arm/rk3588.h
+++ b/include/hw/arm/rk3588.h
@@ -36,6 +36,7 @@ typedef enum RK3588CpuClusterId {
     RK3588_A76_CPUS_PER_CLUSTER * (RK3588_CLUSTER_COUNT - 1))
 
 #define RK3588_SRAM_BASE             0xff000000
+#define RK3588_HPTIMER_BASE          0xfd8c8000
 
 struct RK3588SoCState {
     /*< private >*/
@@ -46,6 +47,7 @@ struct RK3588SoCState {
     CPUClusterState cluster[RK3588_CLUSTER_COUNT];
     MemoryRegion *memory;
     MemoryRegion sram;
+    DeviceState *hptimer;
 };
 
 #endif /* HW_ARM_RK3588_H */
diff --git a/include/hw/timer/rockchip-hptimer.h 
b/include/hw/timer/rockchip-hptimer.h
new file mode 100644
index 0000000000..2e9f5fd819
--- /dev/null
+++ b/include/hw/timer/rockchip-hptimer.h
@@ -0,0 +1,20 @@
+/*
+ * Rockchip RK3588 HPTIMER
+ *
+ * Copyright (C) 2026 Process Mission
+ *
+ * SPDX-License-Identifier: GPL-2.0-or-later
+ */
+
+#ifndef HW_TIMER_ROCKCHIP_HPTIMER_H
+#define HW_TIMER_ROCKCHIP_HPTIMER_H
+
+#include "qom/object.h"
+
+#define TYPE_ROCKCHIP_HPTIMER "rockchip-hptimer"
+OBJECT_DECLARE_SIMPLE_TYPE(RockchipHPTimerState, ROCKCHIP_HPTIMER)
+
+#define ROCKCHIP_HPTIMER_CTRL   0x04
+#define ROCKCHIP_HPTIMER_ENABLE (1U << 0)
+
+#endif /* HW_TIMER_ROCKCHIP_HPTIMER_H */
-- 
2.53.0


Reply via email to