Skip to contentSkip to conversation

Allwinner A10 GPIO support

PATCH Superseded Strahinja Jankovic 2 participants 9 messages Aug 29 – Sep 10

Subsystems (2)

v2 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

v2 cover Allwinner A10 GPIO support

This patch series introduces GPIO support for Allwinner A10 and Cubieboard.

The support is focused on input/output and interrupt functionality. The qtest suite is also updated with tests for implemented functionality.

---
Changes in v2:
- allwinner-gpio-test: Added test for interrupt triggered on both rising
and falling edge
- Link to v1: https://lore.kernel.org/qemu-devel/[email protected]

---
Strahinja Jankovic (3):
hw/gpio: Allwinner A10 GPIO emulation
hw/arm: Add GPIO to Allwinner A10 model
tests/qtest: Allwinner GPIO tests

 MAINTAINERS                           |   3 +
 docs/system/arm/cubieboard.rst        |   1 +
 hw/arm/Kconfig                        |   1 +
 hw/arm/allwinner-a10.c                |   8 +
 hw/gpio/Kconfig                       |   3 +
 hw/gpio/allwinner-gpio.c              | 465 +++++++++++++++++++++++++++++
 hw/gpio/meson.build                   |   1 +
 hw/gpio/trace-events                  |   6 +
 include/hw/arm/allwinner-a10.h        |   2 +
 include/hw/gpio/allwinner-gpio-regs.h | 228 ++++++++++++++
 include/hw/gpio/allwinner-gpio.h      |  41 +++
 tests/qtest/allwinner-gpio-test.c     | 538 ++++++++++++++++++++++++++++++++++
 tests/qtest/meson.build               |   3 +-
 13 files changed, 1299 insertions(+), 1 deletion(-)

---
base-commit: fa19879df1658f96ac07365fca8835b7decd6995
change-id: 20260818-allwinner-gpio-6ea84117df94

Best regards,

Signature · 1 line
Strahinja Jankovic <[email protected]>
#1 lore raw

PATCH v2 1/3 hw/gpio: Allwinner A10 GPIO emulation

This patch implements Allwinner A10 GPIO peripheral emulation.

The emulation focuses on input/output and interrupt functionality, whereas the drive and pull up/down functionality is not emulated.

---
 MAINTAINERS                           |   2 +
 hw/gpio/Kconfig                       |   3 +
 hw/gpio/allwinner-gpio.c              | 465 ++++++++++++++++++++++++++++++++++
 hw/gpio/meson.build                   |   1 +
 hw/gpio/trace-events                  |   6 +
 include/hw/gpio/allwinner-gpio-regs.h | 228 +++++++++++++++++
 include/hw/gpio/allwinner-gpio.h      |  41 +++
 7 files changed, 746 insertions(+)
MAINTAINERS +2 -0
index b51f5c3e60..6cefdde70b 100644
@@ -743,6 +743,8 @@ F: include/hw/*/allwinner*
 F: hw/arm/cubieboard.c
 F: docs/system/arm/cubieboard.rst
 F: hw/misc/axp2xx.c
+F: hw/gpio/allwinner-gpio.c
+F: include/hw/gpio/allwinner-gpio*
 F: tests/functional/arm/test_cubieboard.py
 
 Allwinner-h3
hw/gpio/Kconfig +3 -0
index fcc7c70bd5..ea0fb72b40 100644
@@ -33,3 +33,6 @@ config ZAURUS_SCOOP
 
 config CADENCE_GPIO
     bool
+
+config ALLWINNER_GPIO
+    bool
hw/gpio/allwinner-gpio.c new file +465 -0

465 lines not on this page · raw message

hw/gpio/meson.build +1 -0
index 0555f44b6a..8ede5e88ae 100644
@@ -20,3 +20,4 @@ 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_CADENCE_GPIO', if_true: files('cadence_gpio.c'))
+system_ss.add(when: 'CONFIG_ALLWINNER_GPIO', if_true: files('allwinner-gpio.c'))
hw/gpio/trace-events +6 -0
index 80ca783a03..5ef3e0b8fd 100644
@@ -51,3 +51,9 @@ stm32l4x5_gpio_pins(char *gpio, uint16_t disconnected, uint16_t high) "GPIO%s di
 cdns_gpio_read(const char *path, uint64_t offset, uint32_t value) "%s:reg[0x%04" PRIx64 "] -> 0x%" PRIx32
 cdns_gpio_write(const char *path, uint64_t offset, uint64_t value) "%s:reg[0x%04" PRIx64 "] <- 0x%04" PRIx64
 cdns_gpio_set(const char *path, int line, int level) "%s:[%d] <- %d"
+
+# allwinner_gpio.c
+allwinner_gpio_set(const char *port, int line, int level) "[%s:%d] <- %d"
+allwinner_gpio_read(const char *reg, uint32_t value) "[%s] -> 0x%" PRIx32
+allwinner_gpio_write(const char *reg, uint32_t value) "[%s] <- 0x%" PRIx32
+allwinner_gpio_out_pin(const char *reg, uint32_t pin, uint32_t value) "[%s.%d] <- 0x%" PRIx32
include/hw/gpio/allwinner-gpio-regs.h new file +228 -0

228 lines not on this page · raw message

include/hw/gpio/allwinner-gpio.h new file +41 -0

41 lines not on this page · raw message

Signature · 1 line
2.55.0
#2 lore raw

Re: [PATCH v2 1/3] hw/gpio: Allwinner A10 GPIO emulation

Quoted 7 lines · Strahinja Jankovic

This patch implements Allwinner A10 GPIO peripheral emulation.

The emulation focuses on input/output and interrupt functionality, whereas the drive and pull up/down functionality is not emulated.

Thanks for this patch; my comments below are generally minor ones.

hw/gpio/allwinner-gpio.c line 113

Quoted 2 lines
hw/gpio/allwinner-gpio.c new file +2 -0
+static bool gpio_is_input(AWPortMap *port, uint32_t pin);
+static int int_ctl_cfg(AWGPIOState *s, int irq_line);

I think you could just move the definitions of these functions to here rather than having forward declarations.

Quoted 21 lines
hw/gpio/allwinner-gpio.c new file +21 -0
+
+static inline void allwinner_gpio_update_int(AWGPIOState *s)
+{
+    qemu_set_irq(
+        s->irq,
+        !!(s->regs[REG_INDEX(GPIO_INT_CTL)] & s->regs[REG_INDEX(GPIO_INT_STA)])
+    );
+}
+
+static void allwinner_gpio_set_int_line(AWGPIOState *s, int port,
+                                        int line, AWGPIOLevel level)
+{
+    AWPortsOverlay *o = (AWPortsOverlay *)s->regs;
+    int irq_line = irq_nr(port, line);
+    bool raise_irq = false;
+    unsigned input_val;
+    int int_cfg;
+
+    /* Check if pin can actually trigger an interrupt */
+    if (irq_line == -1) {
+        return;

hw/gpio/allwinner-gpio.c line 144

Quoted 10 lines
hw/gpio/allwinner-gpio.c new file +10 -0
+    }
+
+    /* if this signal isn't configured as an input signal, nothing to do */
+    if (!gpio_is_input(&o->ports[port], line)) {
+        return;
+    }
+
+    /* Get current input value */
+    input_val = extract32(o->ports[port].dat, line, 1);
+    int_cfg = int_ctl_cfg(s, irq_line);

I was tempted to suggest that maybe we could work on more than one pin at a time (compare cc57a945feb090), but the way the config is stored in registers for this device makes that too awkward, unfortunately.

Quoted 33 lines
hw/gpio/allwinner-gpio.c new file +33 -0
+
+    switch (int_cfg) {
+    case AW_GPIO_IRQ_CFG_RISING_EDGE:
+        if (!input_val && level) {
+            raise_irq = true;
+        }
+        break;
+    case AW_GPIO_IRQ_CFG_FALLING_EDGE:
+        if (input_val && !level) {
+            raise_irq = true;
+        }
+        break;
+    case AW_GPIO_IRQ_CFG_HIGH_LEVEL:
+        if (level) {
+            raise_irq = true;
+        }
+        break;
+    case AW_GPIO_IRQ_CFG_LOW_LEVEL:
+        if (!level) {
+            raise_irq = true;
+        }
+        break;
+    case AW_GPIO_IRQ_CFG_BOTH_EDGE:
+        if (input_val != level) {
+            raise_irq = true;
+        }
+        break;
+    default:
+        /* Unexpected */
+        break;
+    }
+
+    if (raise_irq) {

hw/gpio/allwinner-gpio.c line 187

Quoted 10 lines
hw/gpio/allwinner-gpio.c new file +10 -0
+        s->regs[REG_INDEX(GPIO_INT_STA)] |= 1u << irq_line;
+    }
+}
+
+static void port_set_all_int_lines(AWGPIOState *s, int port)
+{
+    AWPortsOverlay *o = (AWPortsOverlay *)s->regs;
+    int i;
+
+    for (i = 0; i < AW_PINS_PER_PORT[port]; i++) {

Our coding style these days prefers "for (int i = ... )" where the variable is only used as a loop iterator.

Quoted 20 lines
hw/gpio/allwinner-gpio.c new file +20 -0
+        AWGPIOLevel aw_level = extract32(o->ports[port].dat, i, 1);
+        allwinner_gpio_set_int_line(s, port, i, aw_level);
+    }
+
+    allwinner_gpio_update_int(s);
+}
+
+static void allwinner_set_all_int_lines(AWGPIOState *s)
+{
+    int i;
+
+    for (i = 0; i < AW_GPIO_PORTS_NUM; i++) {
+        port_set_all_int_lines(s, i);
+    }
+}
+
+static void allwinner_gpio_set(void *opaque, int port, int line, int level)
+{
+    AWGPIOState *s = AW_GPIO(opaque);
+    AWPortsOverlay *o = (AWPortsOverlay *)s->regs;

hw/gpio/allwinner-gpio.c line 217

Quoted 10 lines
hw/gpio/allwinner-gpio.c new file +10 -0
+    AWGPIOLevel aw_level = level ? AW_GPIO_LEVEL_HIGH : AW_GPIO_LEVEL_LOW;
+
+    if (gpio_is_input(&o->ports[port], line)) {
+
+        trace_allwinner_gpio_set(portname(port), line, aw_level);
+
+        allwinner_gpio_set_int_line(s, port, line, aw_level);
+
+        o->ports[port].dat = (o->ports[port].dat & ~(1u << line)) |
+                             (aw_level << line);

This is better written with deposit32(), I think.

Quoted 17 lines
hw/gpio/allwinner-gpio.c new file +17 -0
+    }
+    allwinner_gpio_update_int(s);
+}
+
+
+static inline bool gpio_is_output(AWPortMap *port, uint32_t pin)
+{
+    uint32_t cfg_n = pin / CFG_PINS_PER_REG;
+    uint32_t pin_shift = (pin % CFG_PINS_PER_REG) * CFG_PIN_STRIDE;
+    return (extract32(port->cfg[cfg_n], pin_shift, CFG_PIN_STRIDE - 1) ==
+        CFG_OUTPUT_MASK);
+}
+
+static inline bool gpio_is_input(AWPortMap *port, uint32_t pin)
+{
+    uint32_t cfg_n = pin / CFG_PINS_PER_REG;
+    uint32_t pin_shift = (pin % CFG_PINS_PER_REG) * CFG_PIN_STRIDE;

hw/gpio/allwinner-gpio.c line 244

Quoted 10 lines
hw/gpio/allwinner-gpio.c new file +10 -0
+    return (extract32(port->cfg[cfg_n], pin_shift, CFG_PIN_STRIDE - 1) ==
+        CFG_INPUT_MASK);
+}
+
+static inline int int_ctl_cfg(AWGPIOState *s, int irq_line)
+{
+    unsigned int_cfg_n = s->regs[REG_INDEX(GPIO_INT_CFG0) + irq_line / 8];
+    return extract32(int_cfg_n,
+                     (irq_line % INT_CFG_IRQ_PER_REG) * INT_CFG_IRQ_STRIDE,
+                     INT_CFG_IRQ_STRIDE - 1);

The datasheet says these fields are 4 bits, so shouldn't the third argument here be INT_CFG_IRQ_STRIDE, not ... - 1 ? This is different fromthe port config registers, which are documented as 3 bit fields separated by a reserved bit.

Quoted 7 lines
hw/gpio/allwinner-gpio.c new file +7 -0
+}
+
+static inline void port_update_output_lines(AWGPIOState *s, uint32_t port)
+{
+    AWPortsOverlay *o = (AWPortsOverlay *)s->regs;
+    int pin;
+

hw/gpio/allwinner-gpio.c line 261

Quoted 10 lines
hw/gpio/allwinner-gpio.c new file +10 -0
+    for (pin = 0; pin < AW_PINS_PER_PORT[port]; pin++) {
+        if (gpio_is_output(&o->ports[port], pin)) {
+            qemu_set_irq(s->output[port][pin],
+                         !!(o->ports[port].dat & BIT_MASK(pin)));
+            trace_allwinner_gpio_out_pin(
+                portname(port), pin,
+                !!(o->ports[port].dat & BIT_MASK(pin)));
+         }
+        else if (gpio_is_input(&o->ports[port], pin)) {
+            qemu_irq_lower(s->output[port][pin]);

Shouldn't the way we handle the output GPIO line be the same for both "this is configured as an input" and "this is configured as neither an input or an output" ?

Quoted 32 lines
hw/gpio/allwinner-gpio.c new file +32 -0
+        }
+    }
+}
+
+static inline void allwinner_gpio_update_all_output_lines(AWGPIOState *s)
+{
+    for (int port = 0; port < AW_GPIO_PORTS_NUM; port++) {
+        port_update_output_lines(s, port);
+    }
+}
+
+
+static uint64_t allwinner_gpio_read(void *opaque, hwaddr offset, unsigned size)
+{
+    AWGPIOState *s = AW_GPIO(opaque);
+    uint32_t reg_value = 0;
+    g_autofree char *regname = allwinner_gpio_get_regname(offset);
+
+    switch (offset) {
+    case 0 ... GPIO_Pn_PUL1(GPIO_PI):
+    case GPIO_INT_CFG0:
+    case GPIO_INT_CFG1:
+    case GPIO_INT_CFG2:
+    case GPIO_INT_CFG3:
+    case GPIO_INT_CTL:
+    case GPIO_INT_STA:
+    case GPIO_INT_DEB:
+        reg_value = s->regs[REG_INDEX(offset)];
+        break;
+    default:
+        qemu_log_mask(LOG_GUEST_ERROR, "[%s]%s: Bad register at offset 0x%"
+                      HWADDR_PRIx "\n", TYPE_AW_GPIO, __func__, offset);

hw/gpio/allwinner-gpio.c line 303

Quoted 10 lines
hw/gpio/allwinner-gpio.c new file +10 -0
+        break;
+    }
+
+    trace_allwinner_gpio_read(regname, reg_value);
+
+    return reg_value;
+}
+
+/* update output values */
+/* update interrupts */

These comments seem like they're in the wrong place ?

Quoted 55 lines
hw/gpio/allwinner-gpio.c new file +54 -0
+static void allwinner_port_write(AWGPIOState *s, hwaddr offset, uint64_t value)
+{
+    AWPortsOverlay *o = (AWPortsOverlay *)s->regs;
+    uint32_t port = offset / PORT_STRIDE;
+    uint32_t reg = offset % PORT_STRIDE;
+
+    switch (reg) {
+    case CFG0:
+        o->ports[port].cfg[0] =
+            value & GPIO_CFG0_PINS_MASK(AW_PINS_PER_PORT[port]);
+        break;
+    case CFG1:
+        o->ports[port].cfg[1] =
+            value & GPIO_CFG1_PINS_MASK(AW_PINS_PER_PORT[port]);
+        break;
+    case CFG2:
+        o->ports[port].cfg[2] =
+            value & GPIO_CFG2_PINS_MASK(AW_PINS_PER_PORT[port]);
+        break;
+    case CFG3:
+        o->ports[port].cfg[3] =
+            value & GPIO_CFG3_PINS_MASK(AW_PINS_PER_PORT[port]);
+        break;
+    case DAT:
+        o->ports[port].dat =
+            value & GPIO_DAT_PINS_MASK(AW_PINS_PER_PORT[port]);
+        break;
+    case DRV0:
+        o->ports[port].drv[0] =
+            value & GPIO_DRV0_PINS_MASK(AW_PINS_PER_PORT[port]);
+        break;
+    case DRV1:
+        o->ports[port].drv[1] =
+            value & GPIO_DRV1_PINS_MASK(AW_PINS_PER_PORT[port]);
+        break;
+    case PUL0:
+        o->ports[port].pul[0] =
+            value & GPIO_PUL0_PINS_MASK(AW_PINS_PER_PORT[port]);
+        break;
+    case PUL1:
+        o->ports[port].pul[1] =
+            value & GPIO_PUL1_PINS_MASK(AW_PINS_PER_PORT[port]);
+        break;
+    }
+    port_update_output_lines(s, port);
+    port_set_all_int_lines(s, port);
+}
 
+static void allwinner_gpio_reset(DeviceState *dev)
+{
+    AWGPIOState *s = AW_GPIO(dev);
+    AWPortsOverlay *o = (AWPortsOverlay *)s->regs;
+    int port = 0;
+
+    memset(s->regs, 0, AW_GPIO_IOSIZE);

hw/gpio/allwinner-gpio.c line 420

Quoted 10 lines
hw/gpio/allwinner-gpio.c new file +10 -0
+
+    for (port = 0; port < AW_GPIO_PORTS_NUM; port++) {
+        o->ports[port].drv[0] = aw_gpio_port_reset[port].drv[0];
+        o->ports[port].drv[1] = aw_gpio_port_reset[port].drv[1];
+        o->ports[port].pul[0] = aw_gpio_port_reset[port].pul[0];
+        o->ports[port].pul[1] = aw_gpio_port_reset[port].pul[1];
+    }
+
+    allwinner_gpio_update_all_output_lines(s);
+    allwinner_gpio_update_int(s);

Generally we say you shouldn't update gpio lines outbound in a reset function.

include/hw/gpio/allwinner-gpio-regs.h line 176

Quoted 12 lines
include/hw/gpio/allwinner-gpio-regs.h new file +11 -0
+}
 
+typedef enum AWGPIOLevel {
+    AW_GPIO_LEVEL_LOW = 0,
+    AW_GPIO_LEVEL_HIGH = 1,
+} AWGPIOLevel;
+
+typedef enum AWGPIOCfg {
+    AW_GPIO_CFG_IN = 0,
+    AW_GPIO_CFG_OUT = 1,
+    AW_GPIO_CFG_FUN = 2,
+    /* Use only fun for testing */

What does this comment mean ?

Quoted 14 lines
include/hw/gpio/allwinner-gpio-regs.h new file +1 -0
+} AWGPIOCfg;
include/hw/gpio/allwinner-gpio.h new file +6 -0
new file mode 100644
index 0000000000..f6778d1893
@@ -0,0 +1,41 @@
+/*
+ *  Allwinner GPIO registers definition
+ *
+ *  Copyright (C) 2026 Strahinja Jankovic. <[email protected]>
+ *
+ *  SPDX-License-Identifier: GPL-2.0-or-later

include/hw/gpio/allwinner-gpio.h line 16

Quoted 10 lines
include/hw/gpio/allwinner-gpio.h new file +10 -0
+ */
+
+#ifndef ALLWINNER_GPIO_H
+#define ALLWINNER_GPIO_H
+
+#include "hw/core/sysbus.h"
+#include "qom/object.h"
+
+/** Size of register I/O address space used by GPIO device */
+#define AW_GPIO_IOSIZE (0x400)

You don't need brackets around single values.

Quoted 15 lines
include/hw/gpio/allwinner-gpio.h new file +15 -0
+
+/** Total number of known registers */
+#define AW_GPIO_REGS_NUM    (AW_GPIO_IOSIZE / sizeof(uint32_t))
+
+/** Max number of ports */
+#define AW_GPIO_PORTS_NUM   9
+/** Max number of pins per ports */
+#define AW_GPIO_PIN_COUNT 32
+
+#define TYPE_AW_GPIO        "allwinner.gpio"
+OBJECT_DECLARE_SIMPLE_TYPE(AWGPIOState, AW_GPIO)
+
+struct AWGPIOState {
+    /*< private >*/
+    SysBusDevice parent_obj;

include/hw/gpio/allwinner-gpio.h line 41

Quoted 10 lines
include/hw/gpio/allwinner-gpio.h new file +10 -0
+
+    /*< public >*/
+    MemoryRegion iomem;
+    qemu_irq irq;
+    qemu_irq output[AW_GPIO_PORTS_NUM][AW_GPIO_PIN_COUNT];
+
+    uint32_t regs[AW_GPIO_REGS_NUM];
+};
+
+#endif /* ALLWINNER_GPIO_H */

thanks
-- PMM

#3 lore raw

Re: [PATCH v2 1/3] hw/gpio: Allwinner A10 GPIO emulation

Hi Peter,

Thanks for taking time to review these patches.

Quoted 10 lines · Peter Maydell
Quoted 7 lines · Strahinja Jankovic

This patch implements Allwinner A10 GPIO peripheral emulation.

The emulation focuses on input/output and interrupt functionality, whereas the drive and pull up/down functionality is not emulated.

Quoted 9 lines

Thanks for this patch; my comments below are generally minor ones.

hw/gpio/allwinner-gpio.c line 113

Quoted 2 lines
hw/gpio/allwinner-gpio.c new file +2 -0
+static bool gpio_is_input(AWPortMap *port, uint32_t pin);
+static int int_ctl_cfg(AWGPIOState *s, int irq_line);

I think you could just move the definitions of these functions to here rather than having forward declarations.

I will move definitions here.

Quoted 77 lines

hw/gpio/allwinner-gpio.c

Quoted 6 lines
hw/gpio/allwinner-gpio.c new file +6 -0
+
+static inline void allwinner_gpio_update_int(AWGPIOState *s)
+{
+    qemu_set_irq(
+        s->irq,
+        !!(s->regs[REG_INDEX(GPIO_INT_CTL)] &

s->regs[REG_INDEX(GPIO_INT_STA)])

Quoted 8 lines
hw/gpio/allwinner-gpio.c new file +8 -0
+    );
+}
+
+static void allwinner_gpio_set_int_line(AWGPIOState *s, int port,
+                                        int line, AWGPIOLevel level)
+{
+    AWPortsOverlay *o = (AWPortsOverlay *)s->regs;
+    int irq_line = irq_nr(port, line);

hw/gpio/allwinner-gpio.c

Quoted 10 lines
hw/gpio/allwinner-gpio.c new file +10 -0
+    bool raise_irq = false;
+    unsigned input_val;
+    int int_cfg;
+
+    /* Check if pin can actually trigger an interrupt */
+    if (irq_line == -1) {
+        return;
+    }
+
+    /* if this signal isn't configured as an input signal, nothing to

do */

hw/gpio/allwinner-gpio.c line 144

Quoted 7 lines
hw/gpio/allwinner-gpio.c new file +7 -0
+    if (!gpio_is_input(&o->ports[port], line)) {
+        return;
+    }
+
+    /* Get current input value */
+    input_val = extract32(o->ports[port].dat, line, 1);
+    int_cfg = int_ctl_cfg(s, irq_line);

I was tempted to suggest that maybe we could work on more than one pin at a time (compare cc57a945feb090), but the way the config is stored in registers for this device makes that too awkward, unfortunately.

Quoted 38 lines
hw/gpio/allwinner-gpio.c new file +38 -0
+
+    switch (int_cfg) {
+    case AW_GPIO_IRQ_CFG_RISING_EDGE:
+        if (!input_val && level) {
+            raise_irq = true;
+        }
+        break;
+    case AW_GPIO_IRQ_CFG_FALLING_EDGE:
+        if (input_val && !level) {
+            raise_irq = true;
+        }
+        break;
+    case AW_GPIO_IRQ_CFG_HIGH_LEVEL:
+        if (level) {
+            raise_irq = true;
+        }
+        break;
+    case AW_GPIO_IRQ_CFG_LOW_LEVEL:
+        if (!level) {
+            raise_irq = true;
+        }
+        break;
+    case AW_GPIO_IRQ_CFG_BOTH_EDGE:
+        if (input_val != level) {
+            raise_irq = true;
+        }
+        break;
+    default:
+        /* Unexpected */
+        break;
+    }
+
+    if (raise_irq) {
+        s->regs[REG_INDEX(GPIO_INT_STA)] |= 1u << irq_line;
+    }
+}
+
+static void port_set_all_int_lines(AWGPIOState *s, int port)
Quoted 9 lines

hw/gpio/allwinner-gpio.c line 187

Quoted 5 lines
hw/gpio/allwinner-gpio.c new file +5 -0
+{
+    AWPortsOverlay *o = (AWPortsOverlay *)s->regs;
+    int i;
+
+    for (i = 0; i < AW_PINS_PER_PORT[port]; i++) {

Our coding style these days prefers "for (int i = ... )" where the variable is only used as a loop iterator.

Ok!

Quoted 24 lines
Quoted 8 lines
hw/gpio/allwinner-gpio.c new file +7 -0
+        AWGPIOLevel aw_level = extract32(o->ports[port].dat, i, 1);
+        allwinner_gpio_set_int_line(s, port, i, aw_level);
+    }
+
+    allwinner_gpio_update_int(s);
+}
+

hw/gpio/allwinner-gpio.c

Quoted 10 lines
hw/gpio/allwinner-gpio.c new file +10 -0
+static void allwinner_set_all_int_lines(AWGPIOState *s)
+{
+    int i;
+
+    for (i = 0; i < AW_GPIO_PORTS_NUM; i++) {
+        port_set_all_int_lines(s, i);
+    }
+}
+
+static void allwinner_gpio_set(void *opaque, int port, int line, int

level)

hw/gpio/allwinner-gpio.c

Quoted 4 lines
hw/gpio/allwinner-gpio.c new file +4 -0
+{
+    AWGPIOState *s = AW_GPIO(opaque);
+    AWPortsOverlay *o = (AWPortsOverlay *)s->regs;
+    AWGPIOLevel aw_level = level ? AW_GPIO_LEVEL_HIGH :

AW_GPIO_LEVEL_LOW;

Quoted 12 lines

hw/gpio/allwinner-gpio.c line 217

Quoted 9 lines
hw/gpio/allwinner-gpio.c new file +9 -0
+
+    if (gpio_is_input(&o->ports[port], line)) {
+
+        trace_allwinner_gpio_set(portname(port), line, aw_level);
+
+        allwinner_gpio_set_int_line(s, port, line, aw_level);
+
+        o->ports[port].dat = (o->ports[port].dat & ~(1u << line)) |
+                             (aw_level << line);

This is better written with deposit32(), I think.

I will try with deposit32().

Quoted 30 lines

hw/gpio/allwinner-gpio.c

Quoted 11 lines
hw/gpio/allwinner-gpio.c new file +10 -0
+    }
+    allwinner_gpio_update_int(s);
+}
+
+
+static inline bool gpio_is_output(AWPortMap *port, uint32_t pin)
+{
+    uint32_t cfg_n = pin / CFG_PINS_PER_REG;
+    uint32_t pin_shift = (pin % CFG_PINS_PER_REG) * CFG_PIN_STRIDE;
+    return (extract32(port->cfg[cfg_n], pin_shift, CFG_PIN_STRIDE - 1)

==

hw/gpio/allwinner-gpio.c

Quoted 8 lines
hw/gpio/allwinner-gpio.c new file +8 -0
+        CFG_OUTPUT_MASK);
+}
+
+static inline bool gpio_is_input(AWPortMap *port, uint32_t pin)
+{
+    uint32_t cfg_n = pin / CFG_PINS_PER_REG;
+    uint32_t pin_shift = (pin % CFG_PINS_PER_REG) * CFG_PIN_STRIDE;
+    return (extract32(port->cfg[cfg_n], pin_shift, CFG_PIN_STRIDE - 1)

==

hw/gpio/allwinner-gpio.c

Quoted 6 lines
hw/gpio/allwinner-gpio.c new file +6 -0
+        CFG_INPUT_MASK);
+}
+
+static inline int int_ctl_cfg(AWGPIOState *s, int irq_line)
+{
+    unsigned int_cfg_n = s->regs[REG_INDEX(GPIO_INT_CFG0) + irq_line /

8];

Review

Quoted 2 lines
(quoted diff) +2 -0
+    return extract32(int_cfg_n,
+                     (irq_line % INT_CFG_IRQ_PER_REG) *
Quoted 8 lines

INT_CFG_IRQ_STRIDE,

Quoted 1 line

+ INT_CFG_IRQ_STRIDE - 1);

The datasheet says these fields are 4 bits, so shouldn't the third argument here be INT_CFG_IRQ_STRIDE, not ... - 1 ? This is different fromthe port config registers, which are documented as 3 bit fields separated by a reserved bit.

The fields are 4 bits only, but the maximum non-reserved value is 4, so nothing is lost if only 3 bits are used.
However, in order to keep the code more readable, I will change to INT_CFG_IRQ_STRIDE and add a check
when writing that only non-reserved values are written.

Quoted 15 lines

hw/gpio/allwinner-gpio.c

Quoted 4 lines
hw/gpio/allwinner-gpio.c new file +3 -0
+}
+
+static inline void port_update_output_lines(AWGPIOState *s, uint32_t

port)

Quoted 10 lines
hw/gpio/allwinner-gpio.c new file +10 -0
+{
+    AWPortsOverlay *o = (AWPortsOverlay *)s->regs;
+    int pin;
+
+    for (pin = 0; pin < AW_PINS_PER_PORT[port]; pin++) {
+        if (gpio_is_output(&o->ports[port], pin)) {
+            qemu_set_irq(s->output[port][pin],
+                         !!(o->ports[port].dat & BIT_MASK(pin)));
+            trace_allwinner_gpio_out_pin(
+                portname(port), pin,
Quoted 9 lines

hw/gpio/allwinner-gpio.c line 261

Quoted 4 lines
hw/gpio/allwinner-gpio.c new file +4 -0
+                !!(o->ports[port].dat & BIT_MASK(pin)));
+         }
+        else if (gpio_is_input(&o->ports[port], pin)) {
+            qemu_irq_lower(s->output[port][pin]);

Shouldn't the way we handle the output GPIO line be the same for both "this is configured as an input" and "this is configured as neither an input or an output" ?

My idea was that the output pins should not be changed by the GPIO controller if
they are assigned to an alternate function/another peripheral, since they should
change values according to that function. However, since the alternate function
connection to other peripherals is not modeled, then these pins might as well be
fixed to low value if the pin is not configured as output.

I will change this.

Quoted 35 lines

hw/gpio/allwinner-gpio.c

Quoted 6 lines
hw/gpio/allwinner-gpio.c new file +5 -0
+        }
+    }
+}
+
+static inline void allwinner_gpio_update_all_output_lines(AWGPIOState

*s)

hw/gpio/allwinner-gpio.c

Quoted 8 lines
hw/gpio/allwinner-gpio.c new file +8 -0
+{
+    for (int port = 0; port < AW_GPIO_PORTS_NUM; port++) {
+        port_update_output_lines(s, port);
+    }
+}
+
+
+static uint64_t allwinner_gpio_read(void *opaque, hwaddr offset,

unsigned size)

Quoted 8 lines
hw/gpio/allwinner-gpio.c new file +8 -0
+{
+    AWGPIOState *s = AW_GPIO(opaque);
+    uint32_t reg_value = 0;
+    g_autofree char *regname = allwinner_gpio_get_regname(offset);
+
+    switch (offset) {
+    case 0 ... GPIO_Pn_PUL1(GPIO_PI):
+    case GPIO_INT_CFG0:

hw/gpio/allwinner-gpio.c

Quoted 10 lines
hw/gpio/allwinner-gpio.c new file +10 -0
+    case GPIO_INT_CFG1:
+    case GPIO_INT_CFG2:
+    case GPIO_INT_CFG3:
+    case GPIO_INT_CTL:
+    case GPIO_INT_STA:
+    case GPIO_INT_DEB:
+        reg_value = s->regs[REG_INDEX(offset)];
+        break;
+    default:
+        qemu_log_mask(LOG_GUEST_ERROR, "[%s]%s: Bad register at offset

0x%"

Quoted 14 lines

hw/gpio/allwinner-gpio.c line 303

Quoted 11 lines
hw/gpio/allwinner-gpio.c new file +11 -0
+                      HWADDR_PRIx "\n", TYPE_AW_GPIO, __func__, offset);
+        break;
+    }
+
+    trace_allwinner_gpio_read(regname, reg_value);
+
+    return reg_value;
+}
+
+/* update output values */
+/* update interrupts */

These comments seem like they're in the wrong place ?

Correct, I will remove them.

Quoted 62 lines
Quoted 2 lines

+static void allwinner_port_write(AWGPIOState *s, hwaddr offset,

uint64_t value)

Quoted 59 lines
hw/gpio/allwinner-gpio.c new file +58 -0
+{
+    AWPortsOverlay *o = (AWPortsOverlay *)s->regs;
+    uint32_t port = offset / PORT_STRIDE;
+    uint32_t reg = offset % PORT_STRIDE;
+
+    switch (reg) {
+    case CFG0:
+        o->ports[port].cfg[0] =
+            value & GPIO_CFG0_PINS_MASK(AW_PINS_PER_PORT[port]);
+        break;
+    case CFG1:
+        o->ports[port].cfg[1] =
+            value & GPIO_CFG1_PINS_MASK(AW_PINS_PER_PORT[port]);
+        break;
+    case CFG2:
+        o->ports[port].cfg[2] =
+            value & GPIO_CFG2_PINS_MASK(AW_PINS_PER_PORT[port]);
+        break;
+    case CFG3:
+        o->ports[port].cfg[3] =
+            value & GPIO_CFG3_PINS_MASK(AW_PINS_PER_PORT[port]);
+        break;
+    case DAT:
+        o->ports[port].dat =
+            value & GPIO_DAT_PINS_MASK(AW_PINS_PER_PORT[port]);
+        break;
+    case DRV0:
+        o->ports[port].drv[0] =
+            value & GPIO_DRV0_PINS_MASK(AW_PINS_PER_PORT[port]);
+        break;
+    case DRV1:
+        o->ports[port].drv[1] =
+            value & GPIO_DRV1_PINS_MASK(AW_PINS_PER_PORT[port]);
+        break;
+    case PUL0:
+        o->ports[port].pul[0] =
+            value & GPIO_PUL0_PINS_MASK(AW_PINS_PER_PORT[port]);
+        break;
+    case PUL1:
+        o->ports[port].pul[1] =
+            value & GPIO_PUL1_PINS_MASK(AW_PINS_PER_PORT[port]);
+        break;
+    }
+    port_update_output_lines(s, port);
+    port_set_all_int_lines(s, port);
+}
 
+static void allwinner_gpio_reset(DeviceState *dev)
+{
+    AWGPIOState *s = AW_GPIO(dev);
+    AWPortsOverlay *o = (AWPortsOverlay *)s->regs;
+    int port = 0;
+
+    memset(s->regs, 0, AW_GPIO_IOSIZE);
+
+    for (port = 0; port < AW_GPIO_PORTS_NUM; port++) {
+        o->ports[port].drv[0] = aw_gpio_port_reset[port].drv[0];
+        o->ports[port].drv[1] = aw_gpio_port_reset[port].drv[1];
+        o->ports[port].pul[0] = aw_gpio_port_reset[port].pul[0];
Quoted 9 lines

hw/gpio/allwinner-gpio.c line 420

Quoted 5 lines
hw/gpio/allwinner-gpio.c new file +5 -0
+        o->ports[port].pul[1] = aw_gpio_port_reset[port].pul[1];
+    }
+
+    allwinner_gpio_update_all_output_lines(s);
+    allwinner_gpio_update_int(s);

Generally we say you shouldn't update gpio lines outbound in a reset function.

My idea was to make certain that all pins are low and interrupt is not set after reset,
but I can remove the update of outbound gpio lines in the reset function.

Quoted 16 lines

include/hw/gpio/allwinner-gpio-regs.h line 176

Quoted 13 lines
include/hw/gpio/allwinner-gpio-regs.h new file +11 -0
+}
 
+typedef enum AWGPIOLevel {
+    AW_GPIO_LEVEL_LOW = 0,
+    AW_GPIO_LEVEL_HIGH = 1,
+} AWGPIOLevel;
+
+typedef enum AWGPIOCfg {
+    AW_GPIO_CFG_IN = 0,
+    AW_GPIO_CFG_OUT = 1,
+    AW_GPIO_CFG_FUN = 2,
+    /* Use only fun for testing */

What does this comment mean ?

Another note to myself that I forgot to remove; it was an indicator that the FUN setting (alternate function) is not used in the code, and in the end, not in testing.

It will be removed.

Quoted 15 lines

Review

Quoted 4 lines
include/hw/gpio/allwinner-gpio-regs.h new file +1 -0
+} AWGPIOCfg;
(quoted diff) +0 -0

b/include/hw/gpio/allwinner-gpio.h

include/hw/gpio/allwinner-gpio.h line 4

Quoted 9 lines

new file mode 100644
index 0000000000..f6778d1893

include/hw/gpio/allwinner-gpio.h new file +4 -0
@@ -0,0 +1,41 @@
+/*
+ *  Allwinner GPIO registers definition
+ *
+ *  Copyright (C) 2026 Strahinja Jankovic. <

[email protected]>

Quoted 15 lines

include/hw/gpio/allwinner-gpio.h line 16

Quoted 12 lines
include/hw/gpio/allwinner-gpio.h new file +12 -0
+ *
+ *  SPDX-License-Identifier: GPL-2.0-or-later
+ */
+
+#ifndef ALLWINNER_GPIO_H
+#define ALLWINNER_GPIO_H
+
+#include "hw/core/sysbus.h"
+#include "qom/object.h"
+
+/** Size of register I/O address space used by GPIO device */
+#define AW_GPIO_IOSIZE (0x400)

You don't need brackets around single values.

Ok.

Best regards,
Strahinja Jankovic

Quoted 30 lines
Quoted 16 lines
include/hw/gpio/allwinner-gpio.h new file +15 -0
+
+/** Total number of known registers */
+#define AW_GPIO_REGS_NUM    (AW_GPIO_IOSIZE / sizeof(uint32_t))
+
+/** Max number of ports */
+#define AW_GPIO_PORTS_NUM   9
+/** Max number of pins per ports */
+#define AW_GPIO_PIN_COUNT 32
+
+#define TYPE_AW_GPIO        "allwinner.gpio"
+OBJECT_DECLARE_SIMPLE_TYPE(AWGPIOState, AW_GPIO)
+
+struct AWGPIOState {
+    /*< private >*/
+    SysBusDevice parent_obj;

include/hw/gpio/allwinner-gpio.h line 41

Quoted 10 lines
include/hw/gpio/allwinner-gpio.h new file +10 -0
+
+    /*< public >*/
+    MemoryRegion iomem;
+    qemu_irq irq;
+    qemu_irq output[AW_GPIO_PORTS_NUM][AW_GPIO_PIN_COUNT];
+
+    uint32_t regs[AW_GPIO_REGS_NUM];
+};
+
+#endif /* ALLWINNER_GPIO_H */

thanks
-- PMM

#4 lore raw

PATCH v2 2/3 hw/arm: Add GPIO to Allwinner A10 model

In reply to Strahinja Jankovic

The Allwinner A10 GPIO is added to the Allwinner A10 description, so it is available when Cubieboard is emulated.

Update the documentation for Cubieboard to indicate GPIO availability.

---
 docs/system/arm/cubieboard.rst | 1 +
 hw/arm/Kconfig                 | 1 +
 hw/arm/allwinner-a10.c         | 8 ++++++++
 include/hw/arm/allwinner-a10.h | 2 ++
 4 files changed, 12 insertions(+)
docs/system/arm/cubieboard.rst +1 -0
index 90d24c73a1..ee9f489ead 100644
@@ -12,6 +12,7 @@ Emulated devices:
 - RTC
 - EMAC
 - SDHCI
+- GPIO
 - USB controller
 - SATA controller
 - TWI (I2C) controller
hw/arm/Kconfig +1 -0
index 260d2f0751..6d326746c9 100644
@@ -311,6 +311,7 @@ config ALLWINNER_A10
     select ALLWINNER_EMAC
     select ALLWINNER_I2C
     select ALLWINNER_A10_SPI
+    select ALLWINNER_GPIO
     select AXP2XX_PMU
     select SERIAL_MM
     select UNIMP
hw/arm/allwinner-a10.c +8 -0
index 66f30757dd..66fe484ae0 100644
@@ -41,6 +41,7 @@
 #define AW_A10_EHCI_BASE        0x01c14000
 #define AW_A10_OHCI_BASE        0x01c14400
 #define AW_A10_SATA_BASE        0x01c18000
+#define AW_A10_GPIO_BASE        0x01c20800
 #define AW_A10_WDT_BASE         0x01c20c90
 #define AW_A10_RTC_BASE         0x01c20d00
 #define AW_A10_I2C0_BASE        0x01c2ac00
@@ -83,6 +84,8 @@ static void aw_a10_init(Object *obj)
 
     object_initialize_child(obj, "spi0", &s->spi0, TYPE_AW_A10_SPI);
 
+    object_initialize_child(obj, "gpio", &s->gpio, TYPE_AW_GPIO);
+
     for (size_t i = 0; i < AW_A10_NUM_USB; i++) {
         object_initialize_child(obj, "ehci[*]", &s->ehci[i],
                                 TYPE_PLATFORM_EHCI);
@@ -203,6 +206,11 @@ static void aw_a10_realize(DeviceState *dev, Error **errp)
     sysbus_mmio_map(SYS_BUS_DEVICE(&s->spi0), 0, AW_A10_SPI0_BASE);
     sysbus_connect_irq(SYS_BUS_DEVICE(&s->spi0), 0, qdev_get_gpio_in(dev, 10));
 
+    /* GPIO */
+    sysbus_realize(SYS_BUS_DEVICE(&s->gpio), &error_fatal);
+    sysbus_mmio_map(SYS_BUS_DEVICE(&s->gpio), 0, AW_A10_GPIO_BASE);
+    sysbus_connect_irq(SYS_BUS_DEVICE(&s->gpio), 0, qdev_get_gpio_in(dev, 28));
+
     /* WDT */
     sysbus_realize(SYS_BUS_DEVICE(&s->wdt), &error_fatal);
     sysbus_mmio_map_overlap(SYS_BUS_DEVICE(&s->wdt), 0, AW_A10_WDT_BASE, 1);
include/hw/arm/allwinner-a10.h +2 -0
index 445ba1be21..8b7736c7bd 100644
@@ -13,6 +13,7 @@
 #include "hw/misc/allwinner-a10-dramc.h"
 #include "hw/i2c/allwinner-i2c.h"
 #include "hw/ssi/allwinner-a10-spi.h"
+#include "hw/gpio/allwinner-gpio.h"
 #include "hw/watchdog/allwinner-wdt.h"
 #include "system/block-backend.h"
 
@@ -42,6 +43,7 @@ struct AwA10State {
     AwSdHostState mmc0;
     AWI2CState i2c0;
     AWA10SPIState spi0;
+    AWGPIOState gpio;
     AwRtcState rtc;
     AwWdtState wdt;
     MemoryRegion sram_a;
Signature · 1 line
2.55.0
Reviewed-by on v2 2/3
#5 lore raw

Re: [PATCH v2 2/3] hw/arm: Add GPIO to Allwinner A10 model

Quoted 6 lines · Strahinja Jankovic

The Allwinner A10 GPIO is added to the Allwinner A10 description, so it is available when Cubieboard is emulated.

Update the documentation for Cubieboard to indicate GPIO availability.

Quoted 7 lines
---
 docs/system/arm/cubieboard.rst | 1 +
 hw/arm/Kconfig                 | 1 +
 hw/arm/allwinner-a10.c         | 8 ++++++++
 include/hw/arm/allwinner-a10.h | 2 ++
 4 files changed, 12 insertions(+)

thanks
-- PMM

#6 lore raw

PATCH v2 3/3 tests/qtest: Allwinner GPIO tests

In reply to Strahinja Jankovic

Add Allwinner GPIO set of qtests to validate functionality. Tests cover input and output port setting, as well as interrupt functionality.

---
 MAINTAINERS                       |   1 +
 tests/qtest/allwinner-gpio-test.c | 538 ++++++++++++++++++++++++++++++++++++++
 tests/qtest/meson.build           |   3 +-
 3 files changed, 541 insertions(+), 1 deletion(-)
MAINTAINERS +1 -0
index 6cefdde70b..2691796557 100644
@@ -746,6 +746,7 @@ F: hw/misc/axp2xx.c
 F: hw/gpio/allwinner-gpio.c
 F: include/hw/gpio/allwinner-gpio*
 F: tests/functional/arm/test_cubieboard.py
+F: tests/qtest/allwinner-gpio-test.c
 
 Allwinner-h3
 M: Niek Linnenbank <[email protected]>
tests/qtest/allwinner-gpio-test.c new file +538 -0

538 lines not on this page · raw message

tests/qtest/meson.build +2 -1
index f7c7d06620..853f7a9c4c 100644
@@ -257,7 +257,8 @@ qtests_arm = \
   (config_all_devices.has_key('CONFIG_STM32L4X5_SOC') and
    config_all_devices.has_key('CONFIG_DM163')? ['dm163-test'] : []) + \
   ['arm-cpu-features',
-   'boot-serial-test']
+   'boot-serial-test'] + \
+  (config_all_devices.has_key('CONFIG_ALLWINNER_A10')? ['allwinner-gpio-test'] : [])
 
 # TODO: once aarch64 TCG is fixed on ARM 32 bit host, make bios-tables-test unconditional
 qtests_aarch64 = \
Signature · 1 line
2.55.0
#7 lore raw

Re: [PATCH v2 3/3] tests/qtest: Allwinner GPIO tests

Quoted 16 lines · Strahinja Jankovic

Add Allwinner GPIO set of qtests to validate functionality. Tests cover input and output port setting, as well as interrupt functionality.

---

tests/qtest/allwinner-gpio-test.c new file +7 -0
+static void test_irq_pin_rising(const void *data)
+{
+    PortPinTestParam *param = (PortPinTestParam *)data;
+    QTestState *s = qtest_init("-machine cubieboard");
+    const int gpio_port = param->port;
+    const int port_pin = param->pin;
+    g_autofree char *in_name = portname_in(gpio_port);

tests/qtest/allwinner-gpio-test.c line 258

Quoted 10 lines
tests/qtest/allwinner-gpio-test.c new file +10 -0
+    int irq_pin = irq_nr(gpio_port, port_pin);
+    unsigned int irq_mask = 1u << irq_pin;
+
+    qtest_irq_intercept_in(s, "/machine/soc");
+
+    /* Configure as input and set pin low */
+    update_cfg_reg(s, gpio_port, port_pin, AW_GPIO_CFG_IN);
+    assert_cfg_regval(s, gpio_port, port_pin, AW_GPIO_CFG_IN);
+    qtest_set_irq_in(
+        s, "/machine/soc/gpio", in_name, port_pin, AW_GPIO_LEVEL_LOW);

Breaking the line immediately after the opening "(" looks odd; generally put the first parameter there and break the line after that where it makes sense.

Quoted 24 lines
tests/qtest/allwinner-gpio-test.c new file +24 -0
+
+    /* Enable interrupt */
+    update_int_cfg_reg(s, irq_pin, AW_GPIO_IRQ_CFG_RISING_EDGE);
+    assert_int_cfg_regval(s, irq_pin, AW_GPIO_IRQ_CFG_RISING_EDGE);
+    qtest_writel(s, GPIO_INT_CTL_ADDR, irq_mask);
+
+    /* Raise interrupt */
+    qtest_set_irq_in(
+        s, "/machine/soc/gpio", in_name, port_pin, AW_GPIO_LEVEL_HIGH);
+    /* Check that value is written */
+    assert_data_reg(s, gpio_port, port_pin, AW_GPIO_LEVEL_HIGH);
+    /* Check interrupt status */
+    g_assert_cmphex(qtest_readl(s, GPIO_INT_STA_ADDR), ==, irq_mask);
+    g_assert_true(qtest_get_irq(s, AW_A10_IRQ_LINE));
+
+    /* Clear interrupt */
+    qtest_writel(s, GPIO_INT_STA_ADDR, irq_mask);
+
+    /* Check that IRQ line is low */
+    g_assert_cmphex(qtest_readl(s, GPIO_INT_STA_ADDR) & irq_mask, ==, 0);
+    g_assert_false(qtest_get_irq(s, AW_A10_IRQ_LINE));
+
+    qtest_quit(s);
+}

tests/qtest/allwinner-gpio-test.c line 493

Quoted 10 lines
tests/qtest/allwinner-gpio-test.c new file +7 -0
 
 
 
+int main(int argc, char **argv)
+{
+  int r;
+
+  g_test_init(&argc, &argv, NULL);
+
+  for (int i = 0; i < G_N_ELEMENTS(in_out_test_parameters); i++) {

Prefer ARRAY_SIZE() over G_N_ELEMENTS().

tests/qtest/allwinner-gpio-test.c line 499

Quoted 6 lines
tests/qtest/allwinner-gpio-test.c new file +6 -0
+    g_autofree char *out_name = g_strdup_printf(
+    "/allwinner-cubieboard/gpio/set_output_pins/%s%d",
+    portname(in_out_test_parameters[i].port), in_out_test_parameters[i].pin);
+    g_autofree char *in_name = g_strdup_printf(
+    "/allwinner-cubieboard/gpio/set_input_pins/%s%d",
+    portname(in_out_test_parameters[i].port), in_out_test_parameters[i].pin);

This has turned into an unreadable lump especially given the lack of indentation. I suggest factoring out the creation of the test:

static char *add_test(const char *type, const PortPinTestParam *p,
void (*testfn)(const void *))

{
    g_autofree char *name =
         g_strdup_printf("/allwinner-cubieboard/gpio/%s/%s%d",
                         type, portname(p->port), p->pin);

    qtest_add_data_func(name, p, testfn);
}

Then you in your loop you can do:

    PortPinTestParam *p = &in_out_test_parameters[i];

    add_test("set_output_pins", p, test_set_output_pins);
    add_test("set_input_pins", p, test_set_input_pins);
Quoted 6 lines
tests/qtest/allwinner-gpio-test.c new file +6 -0
+    qtest_add_data_func(out_name, &in_out_test_parameters[i],
+                        test_set_output_pins);
+    qtest_add_data_func(in_name, &in_out_test_parameters[i],
+                        test_set_input_pins);
+  }
+  qtest_add_func("/allwinner-cubieboard/gpio/reset_values",

tests/qtest/allwinner-gpio-test.c line 515

Quoted 10 lines
tests/qtest/allwinner-gpio-test.c new file +10 -0
+                      test_reset_values);
+  for (int i = 0; i < G_N_ELEMENTS(irq_test_parameters); i++) {
+    g_autofree char *rising_edge_name = g_strdup_printf(
+    "/allwinner-cubieboard/gpio/irq_pin_rising/%s%d",
+    portname(irq_test_parameters[i].port), irq_test_parameters[i].pin);
+    qtest_add_data_func(
+            rising_edge_name, &irq_test_parameters[i], test_irq_pin_rising);
+    g_autofree char *falling_edge_name = g_strdup_printf(
+    "/allwinner-cubieboard/gpio/irq_pin_falling/%s%d",
+    portname(irq_test_parameters[i].port), irq_test_parameters[i].pin);

add_test() should make this loop more readable too.

Quoted 13 lines
tests/qtest/allwinner-gpio-test.c new file +13 -0
+    qtest_add_data_func(
+            falling_edge_name, &irq_test_parameters[i], test_irq_pin_falling);
+    g_autofree char *high_level_name = g_strdup_printf(
+    "/allwinner-cubieboard/gpio/irq_pin_high_level/%s%d",
+    portname(irq_test_parameters[i].port), irq_test_parameters[i].pin);
+    qtest_add_data_func(
+            high_level_name, &irq_test_parameters[i], test_irq_pin_high_level);
+    g_autofree char *low_level_name = g_strdup_printf(
+    "/allwinner-cubieboard/gpio/irq_pin_low_level/%s%d",
+    portname(irq_test_parameters[i].port), irq_test_parameters[i].pin);
+    qtest_add_data_func(
+            low_level_name, &irq_test_parameters[i], test_irq_pin_low_level);
+    g_autofree char *both_edge_name = g_strdup_printf(

tests/qtest/allwinner-gpio-test.c line 538

Quoted 10 lines
tests/qtest/allwinner-gpio-test.c new file +10 -0
+    "/allwinner-cubieboard/gpio/irq_pin_both_edge/%s%d",
+    portname(irq_test_parameters[i].port), irq_test_parameters[i].pin);
+    qtest_add_data_func(
+            both_edge_name, &irq_test_parameters[i], test_irq_pin_both_edge);
+
+  }
+  r = g_test_run();
+
+  return r;
+}

thanks
-- PMM

#8 lore raw

Re: [PATCH v2 3/3] tests/qtest: Allwinner GPIO tests

Hi Peter,

Quoted 24 lines · Peter Maydell
Quoted 22 lines · Strahinja Jankovic

Add Allwinner GPIO set of qtests to validate functionality. Tests cover input and output port setting, as well as interrupt functionality.

---

tests/qtest/allwinner-gpio-test.c new file +13 -0
+static void test_irq_pin_rising(const void *data)
+{
+    PortPinTestParam *param = (PortPinTestParam *)data;
+    QTestState *s = qtest_init("-machine cubieboard");
+    const int gpio_port = param->port;
+    const int port_pin = param->pin;
+    g_autofree char *in_name = portname_in(gpio_port);
+    int irq_pin = irq_nr(gpio_port, port_pin);
+    unsigned int irq_mask = 1u << irq_pin;
+
+    qtest_irq_intercept_in(s, "/machine/soc");
+
+    /* Configure as input and set pin low */
Quoted 9 lines

tests/qtest/allwinner-gpio-test.c line 258

Quoted 4 lines
tests/qtest/allwinner-gpio-test.c new file +4 -0
+    update_cfg_reg(s, gpio_port, port_pin, AW_GPIO_CFG_IN);
+    assert_cfg_regval(s, gpio_port, port_pin, AW_GPIO_CFG_IN);
+    qtest_set_irq_in(
+        s, "/machine/soc/gpio", in_name, port_pin, AW_GPIO_LEVEL_LOW);

Breaking the line immediately after the opening "(" looks odd; generally put the first parameter there and break the line after that where it makes sense.

I will change this, had a lot of issues with the line width, so I will update all the places where I did a break like this.

Quoted 29 lines
Quoted 11 lines
tests/qtest/allwinner-gpio-test.c new file +10 -0
+
+    /* Enable interrupt */
+    update_int_cfg_reg(s, irq_pin, AW_GPIO_IRQ_CFG_RISING_EDGE);
+    assert_int_cfg_regval(s, irq_pin, AW_GPIO_IRQ_CFG_RISING_EDGE);
+    qtest_writel(s, GPIO_INT_CTL_ADDR, irq_mask);
+
+    /* Raise interrupt */
+    qtest_set_irq_in(
+        s, "/machine/soc/gpio", in_name, port_pin, AW_GPIO_LEVEL_HIGH);
+    /* Check that value is written */

tests/qtest/allwinner-gpio-test.c

Quoted 10 lines
tests/qtest/allwinner-gpio-test.c new file +10 -0
+    assert_data_reg(s, gpio_port, port_pin, AW_GPIO_LEVEL_HIGH);
+    /* Check interrupt status */
+    g_assert_cmphex(qtest_readl(s, GPIO_INT_STA_ADDR), ==, irq_mask);
+    g_assert_true(qtest_get_irq(s, AW_A10_IRQ_LINE));
+
+    /* Clear interrupt */
+    qtest_writel(s, GPIO_INT_STA_ADDR, irq_mask);
+
+    /* Check that IRQ line is low */
+    g_assert_cmphex(qtest_readl(s, GPIO_INT_STA_ADDR) & irq_mask, ==,

0);

Quoted 4 lines
tests/qtest/allwinner-gpio-test.c new file +4 -0
+    g_assert_false(qtest_get_irq(s, AW_A10_IRQ_LINE));
+
+    qtest_quit(s);
+}
Quoted 10 lines

tests/qtest/allwinner-gpio-test.c line 493

Quoted 7 lines
tests/qtest/allwinner-gpio-test.c new file +7 -0
+int main(int argc, char **argv)
+{
+  int r;
+
+  g_test_init(&argc, &argv, NULL);
+
+  for (int i = 0; i < G_N_ELEMENTS(in_out_test_parameters); i++) {

Prefer ARRAY_SIZE() over G_N_ELEMENTS().

Ok!

Quoted 25 lines

tests/qtest/allwinner-gpio-test.c

Quoted 4 lines
tests/qtest/allwinner-gpio-test.c new file +3 -0
+    g_autofree char *out_name = g_strdup_printf(
+    "/allwinner-cubieboard/gpio/set_output_pins/%s%d",
+    portname(in_out_test_parameters[i].port),

in_out_test_parameters[i].pin);

tests/qtest/allwinner-gpio-test.c

Quoted 3 lines
tests/qtest/allwinner-gpio-test.c new file +3 -0
+    g_autofree char *in_name = g_strdup_printf(
+    "/allwinner-cubieboard/gpio/set_input_pins/%s%d",
+    portname(in_out_test_parameters[i].port),

in_out_test_parameters[i].pin);

This has turned into an unreadable lump especially given the lack of indentation. I suggest factoring out the creation of the test:

static char *add_test(const char *type, const PortPinTestParam *p,
void (*testfn)(const void *))

{
    g_autofree char *name =
         g_strdup_printf("/allwinner-cubieboard/gpio/%s/%s%d",
                         type, portname(p->port), p->pin);

    qtest_add_data_func(name, p, testfn);
}
Quoted 8 lines

Then you in your loop you can do:

    PortPinTestParam *p = &in_out_test_parameters[i];

    add_test("set_output_pins", p, test_set_output_pins);
    add_test("set_input_pins", p, test_set_input_pins);

Now that you point it out, it has. I was adding line after line just to add all tests,
but I forgot to take another look to prettify it.
Thanks for the suggestion, I will use it!

Quoted 14 lines
Quoted 4 lines
tests/qtest/allwinner-gpio-test.c new file +3 -0
+    qtest_add_data_func(out_name, &in_out_test_parameters[i],
+                        test_set_output_pins);
+    qtest_add_data_func(in_name, &in_out_test_parameters[i],

tests/qtest/allwinner-gpio-test.c

Quoted 10 lines
tests/qtest/allwinner-gpio-test.c new file +10 -0
+                        test_set_input_pins);
+  }
+  qtest_add_func("/allwinner-cubieboard/gpio/reset_values",
+                      test_reset_values);
+  for (int i = 0; i < G_N_ELEMENTS(irq_test_parameters); i++) {
+    g_autofree char *rising_edge_name = g_strdup_printf(
+    "/allwinner-cubieboard/gpio/irq_pin_rising/%s%d",
+    portname(irq_test_parameters[i].port), irq_test_parameters[i].pin);
+    qtest_add_data_func(
+            rising_edge_name, &irq_test_parameters[i],
Quoted 7 lines

test_irq_pin_rising);

tests/qtest/allwinner-gpio-test.c line 515

Quoted 3 lines
tests/qtest/allwinner-gpio-test.c new file +3 -0
+    g_autofree char *falling_edge_name = g_strdup_printf(
+    "/allwinner-cubieboard/gpio/irq_pin_falling/%s%d",
+    portname(irq_test_parameters[i].port), irq_test_parameters[i].pin);

add_test() should make this loop more readable too.

Ok!

Thank you very much for your thorough review and help, I will make the changes and send a new version.

Best regards,
Strahinja Jankovic

Quoted 32 lines

Review

Quoted 3 lines
(quoted diff) +2 -0
+    qtest_add_data_func(
+            falling_edge_name, &irq_test_parameters[i],

test_irq_pin_falling);

tests/qtest/allwinner-gpio-test.c

Quoted 5 lines
tests/qtest/allwinner-gpio-test.c new file +5 -0
+    g_autofree char *high_level_name = g_strdup_printf(
+    "/allwinner-cubieboard/gpio/irq_pin_high_level/%s%d",
+    portname(irq_test_parameters[i].port), irq_test_parameters[i].pin);
+    qtest_add_data_func(
+            high_level_name, &irq_test_parameters[i],

test_irq_pin_high_level);

tests/qtest/allwinner-gpio-test.c

Quoted 5 lines
tests/qtest/allwinner-gpio-test.c new file +5 -0
+    g_autofree char *low_level_name = g_strdup_printf(
+    "/allwinner-cubieboard/gpio/irq_pin_low_level/%s%d",
+    portname(irq_test_parameters[i].port), irq_test_parameters[i].pin);
+    qtest_add_data_func(
+            low_level_name, &irq_test_parameters[i],

test_irq_pin_low_level);

tests/qtest/allwinner-gpio-test.c

Quoted 5 lines
tests/qtest/allwinner-gpio-test.c new file +5 -0
+    g_autofree char *both_edge_name = g_strdup_printf(
+    "/allwinner-cubieboard/gpio/irq_pin_both_edge/%s%d",
+    portname(irq_test_parameters[i].port), irq_test_parameters[i].pin);
+    qtest_add_data_func(
+            both_edge_name, &irq_test_parameters[i],

test_irq_pin_both_edge);

tests/qtest/allwinner-gpio-test.c line 538

Quoted 6 lines
tests/qtest/allwinner-gpio-test.c new file +6 -0
+
+  }
+  r = g_test_run();
+
+  return r;
+}

thanks
-- PMM