Skip to contentSkip to conversation

hw/gpio: add K230 GPIO controller model

PATCH Superseded [email protected] 1 participant 2 messages Aug 3 1 message not in the archive

Subsystems (2)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

A message that is not in the archive (sent off-list or not yet received): [email protected]

#1 lore raw

PATCH 1/2 hw/gpio: add K230 GPIO controller model

From: guochun wang <[email protected]>

Implement a K230 SoC APB GPIO controller model for QEMU, derived from the Synopsys DesignWare APB GPIO (the Linux gpio-k230 driver is based on gpio-dwapb.c and shares the same register layout), with a Canaan-specific compatible ("canaan,k230-apb-gpio") and hardlock integration. The model is capable of running the Linux gpio-k230 driver and driving external input/output lines. Behaviour follows the K230 Technical Reference Manual v0.3.1.

Implemented:
- Port A registers: SWPORTA_DR/DDR/CTL, EXT_PORTA, INTEN, INTMASK, INTTYPE_LEVEL, INT_POLARITY, INTSTATUS, RAW_INTSTATUS, DEBOUNCE, LS_SYNC, INT_BOTHEDGE, PORTA_EOI, ID_CODE, VER_ID_CODE, CONFIG_REG1/2
- Software control mode: DR drives output pad, DDR selects direction, EXT_PORTA multiplexes external input vs. DR by DDR
- Per-pin IRQ output lines (single interrupt scheme), 32 per group
- Edge detection: rising / falling / both-edge via INT_BOTHEDGE
- Level-sensitive interrupts: active-high / active-low
- PORTA_EOI clears edge interrupts only; level interrupts cleared by source removal or INTMASK
- INTMASK masking and INTEN gating
- DDR/CTL mode switching: no new interrupts while output/hardware mode, pending interrupts preserved; on switch back to input/software mode, level interrupts re-evaluated against current ext level, edge left untouched, per TRM v0.3.1
- Three-phase Resettable API (enter/hold); VMState migration

Not implemented (no observable effect under QEMU):
- Hardware control mode (aux_porta_out/en/in signals, single-ctl params)
- Debounce logic (dbclk, glitch filtering, both-edge debounce timing)
- Combined interrupt output (gpio_intr_flag OR of all lines)
- Clock-domain synchronization (pclk_intr, ls_sync metastability registers, gpio_intrclk_en output)
- Config-time parameters GPIO_PA_SYNC_EXT_DATA / GPIO_PA_SYNC_INTERRUPTS / GPIO_INT_BOTH_EDGE / GPIO_PORTX_SINGLE_CTL

Two controllers are instantiated in the K230 SoC (gpio0 at 0x9140B000, gpio1 at 0x9140C000), replacing the previous unimplemented-device placeholders. Each controller exposes 32 per-pin IRQ lines connected to the PLIC:
- GPIO0: PLIC sources 32..63
- GPIO1: PLIC sources 64..95

---
 MAINTAINERS                 |   2 +
 hw/gpio/Kconfig             |   3 +
 hw/gpio/k230_gpio.c         | 362 ++++++++++++++++++++++++++++++++++++
 hw/gpio/meson.build         |   1 +
 hw/riscv/Kconfig            |   1 +
 hw/riscv/k230.c             |  27 ++-
 include/hw/gpio/k230_gpio.h |  73 ++++++++
 include/hw/riscv/k230.h     |   5 +
 8 files changed, 468 insertions(+), 6 deletions(-)
 create mode 100644 hw/gpio/k230_gpio.c
 create mode 100644 include/hw/gpio/k230_gpio.h
MAINTAINERS +2 -0
index 902db77218..4301d29f7e 100644
@@ -1827,8 +1827,10 @@ M: Chao Liu <[email protected]>
 L: [email protected]
 S: Maintained
 F: docs/system/riscv/k230.rst
+F: hw/gpio/k230_gpio.c
 F: hw/riscv/k230.c
 F: hw/watchdog/k230_wdt.c
+F: include/hw/gpio/k230_gpio.h
 F: include/hw/riscv/k230.h
 F: include/hw/watchdog/k230_wdt.h
 F: tests/functional/riscv64/test_k230.py
hw/gpio/Kconfig +3 -0
index a209294c20..2d37a8bb6f 100644
@@ -16,6 +16,9 @@ config SIFIVE_GPIO
 config STM32L4X5_GPIO
     bool
 
+config K230_GPIO
+    bool
+
 config PCA9552
     bool
     depends on I2C
hw/gpio/k230_gpio.c new file +362 -0

362 lines not on this page · raw message

hw/gpio/meson.build +1 -0
index 6a67ee958f..94b5d9e2fa 100644
@@ -19,3 +19,4 @@ system_ss.add(when: 'CONFIG_ASPEED_SOC', if_true: files('aspeed_gpio.c'))
 system_ss.add(when: 'CONFIG_ASPEED_SOC', if_true: files('aspeed_sgpio.c'))
 system_ss.add(when: 'CONFIG_SIFIVE_GPIO', if_true: files('sifive_gpio.c'))
 system_ss.add(when: 'CONFIG_PCF8574', if_true: files('pcf8574.c'))
+system_ss.add(when: 'CONFIG_K230_GPIO', if_true: files('k230_gpio.c'))
hw/riscv/Kconfig +1 -0
index de37c08cae..410877425c 100644
@@ -162,3 +162,4 @@ config K230
     select SERIAL_MM
     select UNIMP
     select K230_WDT
+    select K230_GPIO
hw/riscv/k230.c +21 -6
index 656f28190c..62967a67d0 100644
@@ -110,6 +110,8 @@ 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-gpio0", &s->gpio[0], TYPE_K230_GPIO);
+    object_initialize_child(obj, "k230-gpio1", &s->gpio[1], TYPE_K230_GPIO);
 
     qdev_prop_set_uint32(DEVICE(cpu0), "hartid-base", 0);
     qdev_prop_set_string(DEVICE(cpu0), "cpu-type", TYPE_RISCV_CPU_THEAD_C908);
@@ -206,6 +208,25 @@ static void k230_soc_realize(DeviceState *dev, Error **errp)
     sysbus_connect_irq(SYS_BUS_DEVICE(&s->wdt[1]), 0,
                        qdev_get_gpio_in(DEVICE(s->c908_plic), K230_WDT1_IRQ));
 
+    /* GPIO */
+    sysbus_realize(SYS_BUS_DEVICE(&s->gpio[0]), &error_fatal);
+    sysbus_mmio_map(SYS_BUS_DEVICE(&s->gpio[0]), 0,
+                    memmap[K230_DEV_GPIO0].base);
+    for (int i = 0; i < K230_GPIO_PINS_PER_GROUP; i++) {
+        sysbus_connect_irq(SYS_BUS_DEVICE(&s->gpio[0]), i,
+                           qdev_get_gpio_in(DEVICE(s->c908_plic),
+                                            K230_GPIO0_IRQ_BASE + i));
+    }
+
+    sysbus_realize(SYS_BUS_DEVICE(&s->gpio[1]), &error_fatal);
+    sysbus_mmio_map(SYS_BUS_DEVICE(&s->gpio[1]), 0,
+                    memmap[K230_DEV_GPIO1].base);
+    for (int i = 0; i < K230_GPIO_PINS_PER_GROUP; i++) {
+        sysbus_connect_irq(SYS_BUS_DEVICE(&s->gpio[1]), i,
+                           qdev_get_gpio_in(DEVICE(s->c908_plic),
+                                            K230_GPIO1_IRQ_BASE + i));
+    }
+
     /* unimplemented devices */
     create_unimplemented_device("kpu.l2-cache",
                                 memmap[K230_DEV_KPU_L2_CACHE].base,
@@ -322,12 +343,6 @@ static void k230_soc_realize(DeviceState *dev, Error **errp)
     create_unimplemented_device("pwm", memmap[K230_DEV_PWM].base,
                                 memmap[K230_DEV_PWM].size);
 
-    create_unimplemented_device("gpio0", memmap[K230_DEV_GPIO0].base,
-                                memmap[K230_DEV_GPIO0].size);
-
-    create_unimplemented_device("gpio1", memmap[K230_DEV_GPIO1].base,
-                                memmap[K230_DEV_GPIO1].size);
-
     create_unimplemented_device("adc", memmap[K230_DEV_ADC].base,
                                 memmap[K230_DEV_ADC].size);
 
include/hw/gpio/k230_gpio.h new file +73 -0

73 lines not on this page · raw message

include/hw/riscv/k230.h +5 -0
index 592e1c26bf..5fccea7fd5 100644
@@ -18,6 +18,7 @@
 #include "hw/core/boards.h"
 #include "hw/riscv/riscv_hart.h"
 #include "hw/watchdog/k230_wdt.h"
+#include "hw/gpio/k230_gpio.h"
 
 #define C908_CPU_HARTID   (0)
 
@@ -33,6 +34,7 @@ typedef struct K230SoCState {
     RISCVHartArrayState c908_cpu; /* Small core */
 
     K230WdtState wdt[2];
+    K230GPIOState gpio[2];
     MemoryRegion sram;
     MemoryRegion bootrom;
 
@@ -127,8 +129,11 @@ enum {
     K230_UART2_IRQ  = 18,
     K230_UART3_IRQ  = 19,
     K230_UART4_IRQ  = 20,
+    K230_GPIO0_IRQ_BASE  = 32,
+    K230_GPIO1_IRQ_BASE  = 64,
     K230_WDT0_IRQ   = 107,
     K230_WDT1_IRQ   = 108,
+
 };
 
 #define K230_UART_COUNT 5
Signature · 1 line
2.43.0
#2 lore raw

PATCH 2/2 tests/qtest: add K230 GPIO controller test

In reply to a message not in the archive

From: guochun wang <[email protected]>

Add qtest cases verifying the K230 GPIO model against TRM v0.3.1 behaviour and Linux gpio-k230 driver requirements.

Tests cover:
- Reset values: all registers cleared after machine init
- Edge detection: rising / falling / both-edge, with EOI clearing
- Level-sensitive: active-high / active-low, cleared by source removal
- EOI: clears edge interrupts only, raw status verified
- INTEN gating and INTMASK masking (raw status still recorded)
- No new interrupts while DDR=output or CTL=hardware mode
- PLIC integration: per-pin IRQ, claim/complete flow
- Multiple simultaneous interrupts on independent lines
- GPIO1 controller (second instance at 0x9140C000)
- DDR/CTL mode switching: pending interrupts preserved; on switch back to input/software mode, level interrupts re-evaluated against current ext level, edge interrupts left untouched (TRM v0.3.1)

Update MAINTAINERS with the new test file.

---
 MAINTAINERS                  |   1 +
 tests/qtest/k230-gpio-test.c | 521 +++++++++++++++++++++++++++++++++++
 tests/qtest/meson.build      |   2 +-
 3 files changed, 523 insertions(+), 1 deletion(-)
 create mode 100644 tests/qtest/k230-gpio-test.c
MAINTAINERS +1 -0
index 4301d29f7e..bb4f585426 100644
@@ -1834,6 +1834,7 @@ F: include/hw/gpio/k230_gpio.h
 F: include/hw/riscv/k230.h
 F: include/hw/watchdog/k230_wdt.h
 F: tests/functional/riscv64/test_k230.py
+F: tests/qtest/k230-gpio-test.c
 F: tests/qtest/k230-wdt-test.c
 
 RX Machines
tests/qtest/k230-gpio-test.c new file +521 -0

521 lines not on this page · raw message

tests/qtest/meson.build +1 -1
index 56ff860e21..cec2f818ae 100644
@@ -297,7 +297,7 @@ 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-gpio-test'] : [])
 
 qtests_hexagon = ['boot-serial-test']
 
Signature · 1 line
2.43.0