Skip to contentSkip to conversation

hw/misc: add K230 PMU device model

PATCH New JunWei Li 1 participant 4 messages Aug 29

Subsystems (2)
lore
Share & tools
lore

JunWei Li

author
#0 lore raw

cover hw/misc: add K230 PMU device model

This series adds a device model for the K230 power management unit (PMU) and wires it into the K230 SoC.

The PMU (K230 TRM V0.3.1 chapter 14) controls the core power-on/off path through two PMIC output pads and reports six PAD inputs plus RTC interrupts to the CPU. This minimal model covers the APB register bank with TRM reset values and write masks, interrupt detection state (PAD64..PAD69 inputs setting PMU_INT_STATE, the interrupt line following PMU_INT_STATE & PMU_INT0_TO_CPU, W1C clear) and the PAD70/PAD71 outputs driven by PMU_SYSCTRL_REG. Long/short press timing, edge counting and debounce are intentionally left for a follow-up.

Patch 1 adds the device model, patch 2 wires it into the K230 SoC (MMIO 0x91000000, small-core PLIC source 81) and patch 3 adds qtest coverage. The k230 qtests pass under qtest-riscv64.

JunWei Li (3):
hw/misc: add K230 PMU device
hw/riscv: wire up K230 PMU in the SoC
tests/qtest: add k230-pmu-test

 MAINTAINERS                 |   3 +
 hw/misc/Kconfig             |   3 +
 hw/misc/k230_pmu.c          | 242 ++++++++++++++++++++++++++++++++++++
 hw/misc/meson.build         |   1 +
 hw/riscv/Kconfig            |   1 +
 hw/riscv/k230.c             |  12 +-
 include/hw/misc/k230_pmu.h  |  71 +++++++++++
 include/hw/riscv/k230.h     |   3 +
 tests/qtest/k230-pmu-test.c | 136 ++++++++++++++++++++
 tests/qtest/meson.build     |   3 +-
 10 files changed, 471 insertions(+), 4 deletions(-)
 create mode 100644 hw/misc/k230_pmu.c
 create mode 100644 include/hw/misc/k230_pmu.h
 create mode 100644 tests/qtest/k230-pmu-test.c
Signature · 1 line
2.20.1
author
#1 lore raw

PATCH 1/3 hw/misc: add K230 PMU device

Add a model of the K230 power management unit with the APB register bank, interrupt detection state and the two PMIC output pads, based on the K230 Technical Reference Manual V0.3.1 chapter 14.

The PMU registers keep their reset values and write masks from the TRM. PAD64..PAD69 inputs set the matching PMU_INT_STATE bit when the corresponding detection is enabled, and the interrupt line follows PMU_INT_STATE & PMU_INT0_TO_CPU. Writing PMU_SYSCTRL_REG drives the PAD70/PAD71 outputs. Long/short press timing, edge counting and debounce are not modelled yet.

---
 MAINTAINERS                |   3 +
 hw/misc/Kconfig            |   3 +
 hw/misc/k230_pmu.c         | 242 +++++++++++++++++++++++++++++++++++++
 hw/misc/meson.build        |   1 +
 include/hw/misc/k230_pmu.h |  71 +++++++++++
 5 files changed, 320 insertions(+)
 create mode 100644 hw/misc/k230_pmu.c
 create mode 100644 include/hw/misc/k230_pmu.h
MAINTAINERS +3 -0
index 6171cc7494..49c165f71e 100644
@@ -1824,10 +1824,13 @@ M: Chao Liu <[email protected]>
 L: [email protected]
 S: Maintained
 F: docs/system/riscv/k230.rst
+F: hw/misc/k230_pmu.c
 F: hw/riscv/k230.c
 F: hw/watchdog/k230_wdt.c
+F: include/hw/misc/k230_pmu.h
 F: include/hw/riscv/k230.h
 F: include/hw/watchdog/k230_wdt.h
+F: tests/qtest/k230-pmu-test.c
 F: tests/qtest/k230-wdt-test.c
 
 RX Machines
hw/misc/Kconfig +3 -0
index 1543ee6653..98a2ab3709 100644
@@ -135,6 +135,9 @@ config RISCV_MIPS_CPC
 config RISCV_MIPS_CPS
     bool
 
+config K230_PMU
+    bool
+
 config MPS2_FPGAIO
     bool
     select LED
hw/misc/k230_pmu.c new file +242 -0

242 lines not on this page · raw message

hw/misc/meson.build +1 -0
index 23265f6035..7eaff69081 100644
@@ -28,6 +28,7 @@ system_ss.add(when: 'CONFIG_IOSB', if_true: files('iosb.c'))
 system_ss.add(when: 'CONFIG_VIRT_CTRL', if_true: files('virt_ctrl.c'))
 
 # RISC-V devices
+system_ss.add(when: 'CONFIG_K230_PMU', if_true: files('k230_pmu.c'))
 system_ss.add(when: 'CONFIG_MCHP_PFSOC_DMC', if_true: files('mchp_pfsoc_dmc.c'))
 system_ss.add(when: 'CONFIG_MCHP_PFSOC_IOSCB', if_true: files('mchp_pfsoc_ioscb.c'))
 system_ss.add(when: 'CONFIG_MCHP_PFSOC_SYSREG', if_true: files('mchp_pfsoc_sysreg.c'))
include/hw/misc/k230_pmu.h new file +71 -0

71 lines not on this page · raw message

Signature · 1 line
2.20.1
author
#2 lore raw

PATCH 2/3 hw/riscv: wire up K230 PMU in the SoC

In reply to JunWei Li

Instantiate the PMU device, map its MMIO region at 0x91000000 and connect its interrupt line to the small-core PLIC source 81 (TRM 2.4 interrupt bit 65 + 16). Replace the previous unimplemented PMU window.

---
 hw/riscv/Kconfig        |  1 +
 hw/riscv/k230.c         | 12 +++++++++---
 include/hw/riscv/k230.h |  3 +++
 3 files changed, 13 insertions(+), 3 deletions(-)
hw/riscv/Kconfig +1 -0
index de37c08cae..0e61e6be3d 100644
@@ -162,3 +162,4 @@ config K230
     select SERIAL_MM
     select UNIMP
     select K230_WDT
+    select K230_PMU
hw/riscv/k230.c +9 -3
index 656f28190c..906a5c1240 100644
@@ -110,6 +110,7 @@ static void k230_soc_init(Object *obj)
     object_initialize_child(obj, "c908-cpu", cpu0, TYPE_RISCV_HART_ARRAY);
     object_initialize_child(obj, "k230-wdt0", &s->wdt[0], TYPE_K230_WDT);
     object_initialize_child(obj, "k230-wdt1", &s->wdt[1], TYPE_K230_WDT);
+    object_initialize_child(obj, "k230-pmu", &s->pmu, TYPE_K230_PMU);
 
     qdev_prop_set_uint32(DEVICE(cpu0), "hartid-base", 0);
     qdev_prop_set_string(DEVICE(cpu0), "cpu-type", TYPE_RISCV_CPU_THEAD_C908);
@@ -191,6 +192,14 @@ static void k230_soc_realize(DeviceState *dev, Error **errp)
         k230_create_uart(sys_mem, DEVICE(s->c908_plic), i);
     }
 
+    /* PMU */
+    if (!sysbus_realize(SYS_BUS_DEVICE(&s->pmu), errp)) {
+        return;
+    }
+    sysbus_mmio_map(SYS_BUS_DEVICE(&s->pmu), 0, memmap[K230_DEV_PMU].base);
+    sysbus_connect_irq(SYS_BUS_DEVICE(&s->pmu), 0,
+                       qdev_get_gpio_in(DEVICE(s->c908_plic), K230_PMU_IRQ));
+
     /* Watchdog */
     for (int i = 0; i < 2; i++) {
         if (!sysbus_realize(SYS_BUS_DEVICE(&s->wdt[i]), errp)) {
@@ -259,9 +268,6 @@ static void k230_soc_realize(DeviceState *dev, Error **errp)
     create_unimplemented_device("3d-engine", memmap[K230_DEV_3D_ENGINE].base,
                                 memmap[K230_DEV_3D_ENGINE].size);
 
-    create_unimplemented_device("pmu", memmap[K230_DEV_PMU].base,
-                                memmap[K230_DEV_PMU].size);
-
     create_unimplemented_device("rtc", memmap[K230_DEV_RTC].base,
                                 memmap[K230_DEV_RTC].size);
 
include/hw/riscv/k230.h +3 -0
index 592e1c26bf..5e4845a274 100644
@@ -16,6 +16,7 @@
 #define HW_K230_H
 
 #include "hw/core/boards.h"
+#include "hw/misc/k230_pmu.h"
 #include "hw/riscv/riscv_hart.h"
 #include "hw/watchdog/k230_wdt.h"
 
@@ -37,6 +38,7 @@ typedef struct K230SoCState {
     MemoryRegion bootrom;
 
     DeviceState *c908_plic;
+    K230PmuState pmu;
 } K230SoCState;
 
 #define TYPE_RISCV_K230_MACHINE MACHINE_TYPE_NAME("k230")
@@ -127,6 +129,7 @@ enum {
     K230_UART2_IRQ  = 18,
     K230_UART3_IRQ  = 19,
     K230_UART4_IRQ  = 20,
+    K230_PMU_IRQ    = 81,
     K230_WDT0_IRQ   = 107,
     K230_WDT1_IRQ   = 108,
 };
Signature · 1 line
2.20.1
author
#3 lore raw

PATCH 3/3 tests/qtest: add k230-pmu-test

In reply to JunWei Li

Add qtest coverage for the K230 PMU: register reset values, write masks, interrupt triggering and W1C clearing through the input pads, and PAD70/PAD71 output behaviour.

---
 tests/qtest/k230-pmu-test.c | 136 ++++++++++++++++++++++++++++++++++++
 tests/qtest/meson.build     |   3 +-
 2 files changed, 138 insertions(+), 1 deletion(-)
 create mode 100644 tests/qtest/k230-pmu-test.c
tests/qtest/k230-pmu-test.c new file +136 -0
new file mode 100644
index 0000000000..9f24b18f19
@@ -0,0 +1,136 @@
+/*
+ * QTest for the K230 PMU Unit.
+ *
+ * Copyright (c) 2026 LiJunWei <[email protected]>
+ * SPDX-License-Identifier: GPL-2.0-or-later
+ *
+ * K230 Technical Reference Manual V0.3.1 (2024-11-18):
+ * https://github.com/revyos/external-docs/blob/master/K230/en-us/K230_Technical_Reference_Manual_V0.3.1_20241118.pdf
+ */
+
+#include "qemu/osdep.h"
+#include "libqtest.h"
+#include "hw/misc/k230_pmu.h"
+
+#define PMU_BASE 0x91000000
+
+static void test_reset_values(void)
+{
+    QTestState *qts = qtest_init("-machine k230");
+
+    g_assert_cmphex(qtest_readl(qts, PMU_BASE + R_INT0_TO_CTL * 4), ==,
+                    0xfff);
+    g_assert_cmphex(qtest_readl(qts, PMU_BASE + R_INT1_TO_CTL * 4), ==,
+                    0xfff);
+    g_assert_cmphex(qtest_readl(qts, PMU_BASE + R_INT0_TO_CPU * 4), ==, 0);
+    g_assert_cmphex(qtest_readl(qts, PMU_BASE + R_INT_DETECT_EN * 4), ==,
+                    0x810);
+    g_assert_cmphex(qtest_readl(qts, PMU_BASE + R_INT_DETECT_TYP * 4), ==, 0);
+    g_assert_cmphex(qtest_readl(qts, PMU_BASE + R_INT_DETECT_CLR * 4), ==, 0);
+    g_assert_cmphex(qtest_readl(qts, PMU_BASE + R_LONG_PRESS_TRIGGER * 4),
+                    ==, 0x17700);
+    g_assert_cmphex(qtest_readl(qts, PMU_BASE + R_LONG_PRESS_SHUTDOWN * 4),
+                    ==, 0x4e200);
+    g_assert_cmphex(qtest_readl(qts, PMU_BASE + R_SHORT_PRESS_TRIGGER * 4),
+                    ==, 0x3e800);
+    g_assert_cmphex(qtest_readl(qts, PMU_BASE + R_LEVEL_DEBOUNCE * 4), ==,
+                    0x100);
+    g_assert_cmphex(qtest_readl(qts, PMU_BASE + R_EDGE_NUMBER * 4), ==, 0x1);
+    g_assert_cmphex(qtest_readl(qts, PMU_BASE + R_SYSCTRL * 4), ==, 0x1);
+    g_assert_cmphex(qtest_readl(qts, PMU_BASE + R_OUT_DELAY_SET * 4), ==,
+                    0x1100640);
+    g_assert_cmphex(qtest_readl(qts, PMU_BASE + R_IO_CFG0 * 4), ==, 0x1124);
+    g_assert_cmphex(qtest_readl(qts, PMU_BASE + R_IO_CFG6 * 4), ==, 0x10a4);
+    g_assert_cmphex(qtest_readl(qts, PMU_BASE + R_IO_CFG7 * 4), ==, 0x1084);
+    g_assert_cmphex(qtest_readl(qts, PMU_BASE + R_NORMAL_TIMER_VAL * 4), ==,
+                    0x780);
+    g_assert_cmphex(qtest_readl(qts, PMU_BASE + R_OUT_EVENT_CTRL * 4), ==,
+                    0x5c);
+    g_assert_cmphex(qtest_readl(qts, PMU_BASE + R_OUT_LOGIC_CTRL * 4), ==,
+                    0x2);
+    g_assert_cmphex(qtest_readl(qts, PMU_BASE + R_INT_STATE * 4), ==, 0);
+
+    qtest_quit(qts);
+}
+
+static void test_write_masks(void)
+{
+    QTestState *qts = qtest_init("-machine k230");
+
+    /* Reserved bits are write-ignored and read as zero. */
+    qtest_writel(qts, PMU_BASE + R_INT_DETECT_EN * 4, 0xffffffff);
+    g_assert_cmphex(qtest_readl(qts, PMU_BASE + R_INT_DETECT_EN * 4), ==,
+                    0x1fff);
+    qtest_writel(qts, PMU_BASE + R_INT_DETECT_TYP * 4, 0xffffffff);
+    g_assert_cmphex(qtest_readl(qts, PMU_BASE + R_INT_DETECT_TYP * 4), ==,
+                    0x1fffff);
+    qtest_writel(qts, PMU_BASE + R_IO_CFG0 * 4, 0xffffffff);
+    g_assert_cmphex(qtest_readl(qts, PMU_BASE + R_IO_CFG0 * 4), ==, 0x3def);
+    qtest_writel(qts, PMU_BASE + R_SYSCTRL * 4, 0xffffffff);
+    g_assert_cmphex(qtest_readl(qts, PMU_BASE + R_SYSCTRL * 4), ==, 0x3);
+
+    /* PMU_INT_STATE_REG is read-only. */
+    qtest_writel(qts, PMU_BASE + R_INT_STATE * 4, 0xffffffff);
+    g_assert_cmphex(qtest_readl(qts, PMU_BASE + R_INT_STATE * 4), ==, 0);
+
+    qtest_quit(qts);
+}
+
+static void test_interrupt_clear(void)
+{
+    QTestState *qts = qtest_init("-machine k230");
+
+    qtest_irq_intercept_out_named(qts, "/machine/soc/k230-pmu",
+                                  "sysbus-irq");
+
+    /*
+     * PAD68 (input 4) edge/level detection is enabled by default
+     * (PMU_INT_DETECT_EN bit 4).  Raise the input and gate the
+     * corresponding interrupt through PMU_INT0_TO_CPU.
+     */
+    qtest_writel(qts, PMU_BASE + R_INT0_TO_CPU * 4, BIT(4));
+    qtest_set_irq_in(qts, "/machine/soc/k230-pmu", NULL, 4, 1);
+
+    g_assert_cmphex(qtest_readl(qts, PMU_BASE + R_INT_STATE * 4) & BIT(4),
+                    ==, BIT(4));
+    g_assert_cmpuint(qtest_get_irq(qts, 0), ==, 1);
+
+    /* CLR bit 1 clears the int4 edge/level state (W1C). */
+    qtest_writel(qts, PMU_BASE + R_INT_DETECT_CLR * 4, BIT(1));
+    g_assert_cmphex(qtest_readl(qts, PMU_BASE + R_INT_STATE * 4) & BIT(4),
+                    ==, 0);
+    g_assert_cmpuint(qtest_get_irq(qts, 0), ==, 0);
+
+    qtest_quit(qts);
+}
+
+static void test_pad_outputs(void)
+{
+    QTestState *qts = qtest_init("-machine k230");
+
+    qtest_irq_intercept_out_named(qts, "/machine/soc/k230-pmu",
+                                  "sysbus-irq");
+
+    /* PAD70/PAD71 follow the PMU_SYSCTRL_REG to_ctrl bits. */
+    qtest_writel(qts, PMU_BASE + R_SYSCTRL * 4, 0x0);
+    g_assert_cmpuint(qtest_get_irq(qts, 1), ==, 0);
+    g_assert_cmpuint(qtest_get_irq(qts, 2), ==, 0);
+
+    qtest_writel(qts, PMU_BASE + R_SYSCTRL * 4, 0x3);
+    g_assert_cmpuint(qtest_get_irq(qts, 1), ==, 1);
+    g_assert_cmpuint(qtest_get_irq(qts, 2), ==, 1);
+
+    qtest_quit(qts);
+}
+
+int main(int argc, char **argv)
+{
+    g_test_init(&argc, &argv, NULL);
+
+    qtest_add_func("/k230/pmu/reset-values", test_reset_values);
+    qtest_add_func("/k230/pmu/write-masks", test_write_masks);
+    qtest_add_func("/k230/pmu/interrupt-clear", test_interrupt_clear);
+    qtest_add_func("/k230/pmu/pad-outputs", test_pad_outputs);
+
+    return g_test_run();
+}
tests/qtest/meson.build +2 -1
index 56ff860e21..daa5b6a284 100644
@@ -297,7 +297,8 @@ qtests_riscv64 = ['riscv-csr-test'] + \
   (config_all_devices.has_key('CONFIG_IOMMU_TESTDEV') and
    config_all_devices.has_key('CONFIG_RISCV_IOMMU') ?
    ['iommu-riscv-test'] : []) + \
-  (config_all_devices.has_key('CONFIG_K230') ? ['k230-wdt-test'] : [])
+  (config_all_devices.has_key('CONFIG_K230') ?
+   ['k230-wdt-test', 'k230-pmu-test'] : [])
 
 qtests_hexagon = ['boot-serial-test']
 
Signature · 1 line
2.20.1