Skip to contentSkip to conversation

i.MX: Add GPIO devices to i.MX SOC

PATCH Superseded Jean-Christophe Dubois 2 participants 8 messages Sep 6, 2015 – Sep 9, 2015 Arm i.MX and Layerscape

v2 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

v2 cover i.MX: Add GPIO devices to i.MX SOC

Add GPIO device to i.MX31 and i.MX25 SOC

Jean-Christophe Dubois (3):
i.MX: Add GPIO device
i.MX: Add GPIO devices to i.MX31 SOC
i.MX: Add GPIO devices to i.MX25 SOC

 hw/arm/fsl-imx25.c         |  29 ++++
 hw/arm/fsl-imx31.c         |  34 +++++
 hw/gpio/Makefile.objs      |   1 +
 hw/gpio/imx_gpio.c         | 347 +++++++++++++++++++++++++++++++++++++++++++++
 include/hw/arm/fsl-imx25.h |  15 ++
 include/hw/arm/fsl-imx31.h |  12 ++
 include/hw/gpio/imx_gpio.h |  63 ++++++++
 7 files changed, 501 insertions(+)
 create mode 100644 hw/gpio/imx_gpio.c
 create mode 100644 include/hw/gpio/imx_gpio.h
Signature · 1 line
2.1.4
#1 lore raw

PATCH v2 1/3 i.MX: Add GPIO device

---

Changes since V1:
* added "has-edge-sel" property
* use extract64() and deposit64() in read/write icr access
* set "number of GPIO pin" as a #define
* reorganize functions in file to group them
* various coding style cleanup
* rename state handler array in output array.

 hw/gpio/Makefile.objs      |   1 +
 hw/gpio/imx_gpio.c         | 347 +++++++++++++++++++++++++++++++++++++++++++++
 include/hw/gpio/imx_gpio.h |  63 ++++++++
 3 files changed, 411 insertions(+)
 create mode 100644 hw/gpio/imx_gpio.c
 create mode 100644 include/hw/gpio/imx_gpio.h
hw/gpio/Makefile.objs +1 -0
index 1abcf17..52233f7 100644
@@ -5,3 +5,4 @@ common-obj-$(CONFIG_ZAURUS) += zaurus.o
 common-obj-$(CONFIG_E500) += mpc8xxx.o
 
 obj-$(CONFIG_OMAP) += omap_gpio.o
+obj-$(CONFIG_IMX) += imx_gpio.o
hw/gpio/imx_gpio.c new file +347 -0

347 lines not on this page · raw message

include/hw/gpio/imx_gpio.h new file +63 -0

63 lines not on this page · raw message

Signature · 1 line
2.1.4
#2 lore raw

Re: [Qemu-devel] [PATCH v2 1/3] i.MX: Add GPIO device

Quoted 44 lines · Jean-Christophe Dubois

---

Changes since V1:
* added "has-edge-sel" property
* use extract64() and deposit64() in read/write icr access
* set "number of GPIO pin" as a #define
* reorganize functions in file to group them
* various coding style cleanup
* rename state handler array in output array.

 hw/gpio/Makefile.objs      |   1 +
 hw/gpio/imx_gpio.c         | 347 +++++++++++++++++++++++++++++++++++++++++++++
 include/hw/gpio/imx_gpio.h |  63 ++++++++
 3 files changed, 411 insertions(+)
 create mode 100644 hw/gpio/imx_gpio.c
 create mode 100644 include/hw/gpio/imx_gpio.h
hw/gpio/Makefile.objs +1 -0
index 1abcf17..52233f7 100644
@@ -5,3 +5,4 @@ common-obj-$(CONFIG_ZAURUS) += zaurus.o
 common-obj-$(CONFIG_E500) += mpc8xxx.o
 
 obj-$(CONFIG_OMAP) += omap_gpio.o
+obj-$(CONFIG_IMX) += imx_gpio.o
hw/gpio/imx_gpio.c new file +11 -0
new file mode 100644
index 0000000..fc43c73
@@ -0,0 +1,347 @@
+/*
+ * i.MX processors GPIO emulation.
+ *
+ * Copyright (C) 2015 Jean-Christophe Dubois <[email protected]>
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License as
+ * published by the Free Software Foundation; either version 2 or
+ * (at your option) version 3 of the License.
+ *
+ * This program is distributed in the hope that it will be useful,

hw/gpio/imx_gpio.c line 21

Quoted 10 lines
hw/gpio/imx_gpio.c new file +10 -0
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include "hw/gpio/imx_gpio.h"
+

Cutting from here [1] ...

hw/gpio/imx_gpio.c line 22

Quoted 1 line
hw/gpio/imx_gpio.c new file +1 -0
+#ifdef DEBUG_GPIO

This needs a less generic name, such as DEBUG_IMX_GPIO. This allows defining at configure as a -D CFLAG without other consequences.

hw/gpio/imx_gpio.c line 24

Quoted 2 lines
hw/gpio/imx_gpio.c new file +2 -0
+#  define DPRINTF(fmt, args...) \
+          do { fprintf(stder, "%s: " fmt , __func__, ##args); } while (0)

stderr.

hw/gpio/imx_gpio.c line 26

Quoted 2 lines
hw/gpio/imx_gpio.c new file +2 -0
+
+static char const *imx_gpio_reg_name(uint32_t reg)

const char *. Just for consistency with code base. A grep of "const char \*" suggests this is the norm. We could add a CODING_STYLE.

Quoted 16 lines
hw/gpio/imx_gpio.c new file +16 -0
+{
+    switch (reg) {
+    case DR_ADDR:
+        return "DR";
+    case GDIR_ADDR:
+        return "GDIR";
+    case PSR_ADDR:
+        return "PSR";
+    case ICR1_ADDR:
+        return "ICR1";
+    case ICR2_ADDR:
+        return "ICR2";
+    case IMR_ADDR:
+        return "IMR";
+    case ISR_ADDR:
+        return "ISR";

hw/gpio/imx_gpio.c line 52

Quoted 10 lines
hw/gpio/imx_gpio.c new file +10 -0
+    case EDGE_SEL_ADDR:
+        /* This register is not present in i.MX31 */
+        return "EDGE_SEL";
+    default:
+        return "[?]";
+    }
+}
+#else /* DEBUG_GPIO */
+#  define DPRINTF(fmt, args...) do {} while (0)
+#endif /* DEBUG_GPIO */

[1] ... to here. I would try something like:

#ifndef DEBUG_GPIO
#define DEBUG_GPIO 1
#endif

#define DPRINTF(fmt, args...) \
          do { fprintf(stder, "%s: " fmt , __func__, ##args); } while (0)

static char const *imx_gpio_reg_name(uint32_t reg)

{
    switch (reg) {
    case DR_ADDR:
        return "DR";
...
        return "EDGE_SEL";
    default:
        return "[?]";
    }
}

No, #else needed. As the DPRINTF bodies are always compiled you also need (and want) always-compilation of gpio_reg_name and without the unused static warn.

Quoted 7 lines
hw/gpio/imx_gpio.c new file +7 -0
+
+typedef enum IMXGPIOLevel {
+    IMX_GPIO_LEVEL_LOW = 0,
+    IMX_GPIO_LEVEL_HIGH = 1,
+} IMXGPIOLevel;
+
+static void imx_gpio_update_int(IMXGPIOState *s)

hw/gpio/imx_gpio.c line 69

Quoted 10 lines
hw/gpio/imx_gpio.c new file +10 -0
+{
+    qemu_set_irq(s->irq, s->isr ? 1 : 0);
+}
+
+static void imx_gpio_set_int_line(IMXGPIOState *s, int line, IMXGPIOLevel level)
+{
+    /* if this signal isn't configured as an interrupt source, nothing to do */
+    if (!extract32(s->imr, line, 1)) {
+        return;
+    }

I had a read up of the TRM and found this:

"
Interrupt Status bits. Bit i of this register is asserted (active high) when the active condition is detected on the GPIO input and is waiting for service. The value of this register is independent of the value in the IMR register. When the active condition has been detected, the corresponding bit remains set until cleared by software.
"

IMR does not affect ISR, IMR only bitwise gates the top level interrupt signalling. ISR is unaffected. I think this short return should be dropped IIUC.

hw/gpio/imx_gpio.c line 71

Quoted 2 lines
hw/gpio/imx_gpio.c new file +2 -0
+
+    /* When set, EDGE_SEL override the ICR config */

"overrides"

Quoted 101 lines
hw/gpio/imx_gpio.c new file +101 -0
+    if (extract32(s->edge_sel, line, 1)) {
+        /* we detect interrupt on rising and falling edge */
+        if (extract32(s->psr, line, 1) != level) {
+            /* level changed */
+            s->isr = deposit32(s->isr, line, 1, 1);
+        }
+    } else if (extract64(s->icr, 2*line + 1, 1)) {
+        /* interrupt is edge sensitive */
+        if (extract32(s->psr, line, 1) != level) {
+            /* level changed */
+            if (extract64(s->icr, 2*line, 1) != level) {
+                s->isr = deposit32(s->isr, line, 1, 1);
+            }
+        }
+    } else {
+        /* interrupt is level sensitive */
+        if (extract64(s->icr, 2*line, 1) == level) {
+            s->isr = deposit32(s->isr, line, 1, 1);
+        }
+    }
+}
+
+static void imx_gpio_set(void *opaque, int line, int level)
+{
+    IMXGPIOState *s = IMX_GPIO(opaque);
+    IMXGPIOLevel imx_level = level ? IMX_GPIO_LEVEL_HIGH : IMX_GPIO_LEVEL_LOW;
+
+    /* if the line is configured as output nothing to do */
+    if (extract32(s->gdir, line, 1)) {
+        /* actually we should not be called */
+        qemu_log_mask(LOG_GUEST_ERROR, "%s[%s]: requesting to set line %d"
+                      " to %d but this is an output line\n",
+                      TYPE_IMX_GPIO, __func__, line, level);
+        return;
+    }
+
+    imx_gpio_set_int_line(s, line, imx_level);
+
+    /* this is an input signal, so set PSR */
+    s->psr = deposit32(s->psr, line, 1, imx_level);
+
+    imx_gpio_update_int(s);
+}
+
+static void imx_gpio_set_all_int_lines(IMXGPIOState *s)
+{
+    int i;
+
+    for (i = 0; i < IMX_GPIO_MAX_PIN_COUNT; i++) {
+        IMXGPIOLevel imx_level = extract32(s->psr, i, 1);
+        imx_gpio_set_int_line(s, i, imx_level);
+    }
+
+    imx_gpio_update_int(s);
+}
+
+static inline void imx_gpio_set_all_output_lines(IMXGPIOState *s)
+{
+    int i;
+
+    for (i = 0; i < IMX_GPIO_MAX_PIN_COUNT; i++) {
+        /*
+         * if the line is set as output, then forward the line
+         * level to its user.
+         */
+        if (extract32(s->gdir, i, 1) && s->output[i]) {
+            qemu_set_irq(s->output[i], extract32(s->dr, i, 1));
+        }
+    }
+}
+
+static uint64_t imx_gpio_read(void *opaque, hwaddr offset, unsigned size)
+{
+    IMXGPIOState *s = IMX_GPIO(opaque);
+    uint32_t reg_value = 0;
+
+    switch (offset) {
+    case DR_ADDR:
+        /*
+         * depending on the "line" configuration, the bit values
+         * are coming either from DR or PSR
+         */
+        reg_value = (s->dr & s->gdir) | (s->psr & ~s->gdir);
+        break;
+
+    case GDIR_ADDR:
+        reg_value = s->gdir;
+        break;
+
+    case PSR_ADDR:
+        reg_value = s->psr;
+        break;
+
+    case ICR1_ADDR:
+        reg_value = extract64(s->icr, 0, 32);
+        break;
+
+    case ICR2_ADDR:
+        reg_value = extract64(s->icr, 32, 32);
+        break;
+

hw/gpio/imx_gpio.c line 182

Quoted 10 lines
hw/gpio/imx_gpio.c new file +10 -0
+    case IMR_ADDR:
+        reg_value = s->imr;
+        break;
+
+    case ISR_ADDR:
+        reg_value = s->isr;
+        break;
+
+    case EDGE_SEL_ADDR:
+        /* This register is not present in i.MX31 */

I would remove all references to i.MX31, as this model is not specific to either 31 or 25. A comment like this could go in the 31 machine model however on the boolean prop setter.

hw/gpio/imx_gpio.c line 187

Quoted 5 lines
hw/gpio/imx_gpio.c new file +5 -0
+        if (s->has_edge_sel) {
+            reg_value = s->edge_sel;
+        } else {
+            qemu_log_mask(LOG_GUEST_ERROR, "%s[%s]: EDGE_SEL register not "
+                          "present on imx31 type\n", TYPE_IMX_GPIO, __func__);

-"imx31"

hw/gpio/imx_gpio.c line 197

Quoted 10 lines
hw/gpio/imx_gpio.c new file +10 -0
+        }
+        break;
+
+    default:
+        qemu_log_mask(LOG_GUEST_ERROR, "%s[%s]: Bad register at offset %d\n",
+                      TYPE_IMX_GPIO, __func__, (int)offset);
+        break;
+    }
+
+    DPRINTF("(%s) = 0x%"PRIx32"\n", imx_gpio_reg_name(offset), reg_value);

I would work __func__ in there like with the LOG_GUEST_ERRORS to make it more identifiable in big logs.

Quoted 12 lines
hw/gpio/imx_gpio.c new file +12 -0
+
+    return reg_value;
+}
+
+static void imx_gpio_write(void *opaque, hwaddr offset, uint64_t value,
+                           unsigned size)
+{
+    IMXGPIOState *s = IMX_GPIO(opaque);
+
+    DPRINTF("(%s, value = 0x%"PRIx32")\n", imx_gpio_reg_name(offset),
+            (uint32_t)value);
+

hw/gpio/imx_gpio.c line 219

Quoted 10 lines
hw/gpio/imx_gpio.c new file +10 -0
+    switch (offset) {
+    case DR_ADDR:
+        s->dr = value;
+        imx_gpio_set_all_output_lines(s);
+        break;
+
+    case GDIR_ADDR:
+        s->gdir = value;
+        imx_gpio_set_all_output_lines(s);
+        imx_gpio_set_all_int_lines(s);

So does this need to bitwise set PSR to the most input values on a transition from output to input? You may need extra state, a uint32_t which captures and saves the input state regardless or gdir. Then set_all_output_lines is renamed to set_psr() or something like, setting psr to input values on transitions (that is set_psr() will always set every pad value). I think the rest of the logic will just work, as forcing psr to the last (and current) input value will non trigger edge interrupts, which should be the case if my interpretation of TRM is correct.

This handles cases where there is circuitry that can change directionality at runtime with the proper clamps (e.g. I2C).

Quoted 5 lines
hw/gpio/imx_gpio.c new file +5 -0
+        break;
+
+    case ICR1_ADDR:
+        s->icr = deposit64(s->icr, 0, 32, value);
+        imx_gpio_set_all_int_lines(s);

hw/gpio/imx_gpio.c line 234

Quoted 10 lines
hw/gpio/imx_gpio.c new file +10 -0
+        break;
+
+    case ICR2_ADDR:
+        s->icr = deposit64(s->icr, 32, 32, value);
+        imx_gpio_set_all_int_lines(s);
+        break;
+
+    case IMR_ADDR:
+        s->imr = value;
+        imx_gpio_set_all_int_lines(s);

Following on from IMR comment, this would be replacable with update_int().

Quoted 14 lines
hw/gpio/imx_gpio.c new file +14 -0
+        break;
+
+    case ISR_ADDR:
+        s->isr |= ~value;
+        imx_gpio_set_all_int_lines(s);
+        break;
+
+    case EDGE_SEL_ADDR:
+        /* This register is not present in i.MX31 */
+        if (s->has_edge_sel) {
+            s->edge_sel = value;
+            imx_gpio_set_all_int_lines(s);
+        } else {
+            qemu_log_mask(LOG_GUEST_ERROR, "%s[%s]: EDGE_SEL register not "

hw/gpio/imx_gpio.c line 258

Quoted 10 lines
hw/gpio/imx_gpio.c new file +10 -0
+                          "present on imx31 type\n", TYPE_IMX_GPIO, __func__);
+        }
+        break;
+
+    default:
+        qemu_log_mask(LOG_GUEST_ERROR, "%s[%s]: Bad register at offset %d\n",
+                      TYPE_IMX_GPIO, __func__, (int)offset);
+        break;
+    }
+

Same comments as for read.

Quoted 14 lines
hw/gpio/imx_gpio.c new file +14 -0
+    return;
+}
+
+static const MemoryRegionOps imx_gpio_ops = {
+    .read = imx_gpio_read,
+    .write = imx_gpio_write,
+    .valid.min_access_size = 4,
+    .valid.max_access_size = 4,
+    .endianness = DEVICE_NATIVE_ENDIAN,
+};
+
+static const VMStateDescription vmstate_imx_gpio = {
+    .name = TYPE_IMX_GPIO,
+    .version_id = 1,

hw/gpio/imx_gpio.c line 282

Quoted 10 lines
hw/gpio/imx_gpio.c new file +10 -0
+    .minimum_version_id = 1,
+    .minimum_version_id_old = 1,
+    .fields = (VMStateField[]) {
+        VMSTATE_UINT32(dr, IMXGPIOState),
+        VMSTATE_UINT32(gdir, IMXGPIOState),
+        VMSTATE_UINT32(psr, IMXGPIOState),
+        VMSTATE_UINT64(icr, IMXGPIOState),
+        VMSTATE_UINT32(imr, IMXGPIOState),
+        VMSTATE_UINT32(isr, IMXGPIOState),
+        /* This register is not present in i.MX31 */

Drop comment.

Quoted 102 lines
hw/gpio/imx_gpio.c new file +65 -0
+        VMSTATE_UINT32(edge_sel, IMXGPIOState),
+        VMSTATE_BOOL(has_edge_sel, IMXGPIOState),
+        VMSTATE_END_OF_LIST()
+    }
+};
+
+static Property imx_gpio_properties[] = {
+    DEFINE_PROP_BOOL("has-edge-sel", IMXGPIOState, has_edge_sel, true),
+    DEFINE_PROP_END_OF_LIST(),
+};
+
+static void imx_gpio_reset(DeviceState *dev)
+{
+    IMXGPIOState *s = IMX_GPIO(dev);
+
+    s->dr       = 0;
+    s->gdir     = 0;
+    s->psr      = 0;
+    s->icr      = 0;
+    s->imr      = 0;
+    s->isr      = 0;
+    s->edge_sel = 0;
+
+    imx_gpio_set_all_output_lines(s);
+    imx_gpio_update_int(s);
+}
+
+static void imx_gpio_realize(DeviceState *dev, Error **errp)
+{
+    IMXGPIOState *s = IMX_GPIO(dev);
+
+    memory_region_init_io(&s->iomem, OBJECT(s), &imx_gpio_ops, s,
+                          TYPE_IMX_GPIO, IMX_GPIO_MEM_SIZE);
+
+    qdev_init_gpio_in(DEVICE(s), imx_gpio_set, IMX_GPIO_MAX_PIN_COUNT);
+    qdev_init_gpio_out(DEVICE(s), s->output, IMX_GPIO_MAX_PIN_COUNT);
+
+    sysbus_init_irq(SYS_BUS_DEVICE(dev), &s->irq);
+    sysbus_init_mmio(SYS_BUS_DEVICE(dev), &s->iomem);
+}
+
+static void imx_gpio_class_init(ObjectClass *klass, void *data)
+{
+    DeviceClass *dc = DEVICE_CLASS(klass);
+
+    dc->realize = imx_gpio_realize;
+    dc->reset = imx_gpio_reset;
+    dc->props = imx_gpio_properties;
+    dc->vmsd = &vmstate_imx_gpio;
+    dc->desc = "i.MX GPIO controller";
+}
+
+static const TypeInfo imx_gpio_info = {
+    .name = TYPE_IMX_GPIO,
+    .parent = TYPE_SYS_BUS_DEVICE,
+    .instance_size = sizeof(IMXGPIOState),
+    .class_init = imx_gpio_class_init,
+};
+
+static void imx_gpio_register_types(void)
+{
+    type_register_static(&imx_gpio_info);
+}
+
+type_init(imx_gpio_register_types)
include/hw/gpio/imx_gpio.h new file +31 -0
new file mode 100644
index 0000000..d91997a
@@ -0,0 +1,63 @@
+/*
+ * i.MX processors GPIO registers definition.
+ *
+ * Copyright (C) 2015 Jean-Christophe Dubois <[email protected]>
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License as
+ * published by the Free Software Foundation; either version 2 or
+ * (at your option) version 3 of the License.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, see <http://www.gnu.org/licenses/>.
+ */
+
+#ifndef __IMX_GPIO_H_
+#define __IMX_GPIO_H_
+
+#include <hw/sysbus.h>
+
+#define TYPE_IMX_GPIO "imx.gpio"
+#define IMX_GPIO(obj) OBJECT_CHECK(IMXGPIOState, (obj), TYPE_IMX_GPIO)
+
+#define IMX_GPIO_MEM_SIZE 0x20
+
+/* i.MX GPIO memory map */
+#define DR_ADDR             0x00 /* DATA REGISTER */

include/hw/gpio/imx_gpio.h line 41

Quoted 10 lines
include/hw/gpio/imx_gpio.h new file +10 -0
+#define GDIR_ADDR           0x04 /* DIRECTION REGISTER */
+#define PSR_ADDR            0x08 /* PAD STATUS REGISTER */
+#define ICR1_ADDR           0x0c /* INTERRUPT CONFIGURATION REGISTER 1 */
+#define ICR2_ADDR           0x10 /* INTERRUPT CONFIGURATION REGISTER 2 */
+#define IMR_ADDR            0x14 /* INTERRUPT MASK REGISTER */
+#define ISR_ADDR            0x18 /* INTERRUPT STATUS REGISTER */
+#define EDGE_SEL_ADDR       0x1c /* EDGE SEL REGISTER */
+
+#define IMX_GPIO_MAX_PIN_COUNT 32
+

MAX indicates it is configurable. Does the 25 and 31 down configure the IP to the max of 32? If so you can probably drop the MAX. If not, it should be converted to a property.

Regards,
Peter

Quoted 25 lines
include/hw/gpio/imx_gpio.h new file +22 -1
+typedef struct IMXGPIOState {
+    /*< private >*/
+    SysBusDevice parent_obj;
+
+    /*< public >*/
+    MemoryRegion iomem;
+
+    uint32_t dr;
+    uint32_t gdir;
+    uint32_t psr;
+    uint64_t icr;
+    uint32_t imr;
+    uint32_t isr;
+    bool has_edge_sel;
+    /* This register is not present in i.MX31 */
+    uint32_t edge_sel;
+
+    qemu_irq irq;
+    qemu_irq output[IMX_GPIO_MAX_PIN_COUNT];
+} IMXGPIOState;
+
+#endif /* __IMX_GPIO_H_ */
--

2.1.4

#3 lore raw

Re: [Qemu-devel] [PATCH v2 1/3] i.MX: Add GPIO device

Quoted 58 lines · Peter Crosthwaite
Quoted 44 lines · Jean-Christophe Dubois

---

Changes since V1:
* added "has-edge-sel" property
* use extract64() and deposit64() in read/write icr access
* set "number of GPIO pin" as a #define
* reorganize functions in file to group them
* various coding style cleanup
* rename state handler array in output array.

  hw/gpio/Makefile.objs      |   1 +
  hw/gpio/imx_gpio.c         | 347 +++++++++++++++++++++++++++++++++++++++++++++
  include/hw/gpio/imx_gpio.h |  63 ++++++++
  3 files changed, 411 insertions(+)

create mode 100644 hw/gpio/imx_gpio.c
create mode 100644 include/hw/gpio/imx_gpio.h

hw/gpio/Makefile.objs +1 -0
index 1abcf17..52233f7 100644
@@ -5,3 +5,4 @@ common-obj-$(CONFIG_ZAURUS) += zaurus.o
  common-obj-$(CONFIG_E500) += mpc8xxx.o
 
  obj-$(CONFIG_OMAP) += omap_gpio.o
+obj-$(CONFIG_IMX) += imx_gpio.o
hw/gpio/imx_gpio.c new file +11 -0
new file mode 100644
index 0000000..fc43c73
@@ -0,0 +1,347 @@
+/*
+ * i.MX processors GPIO emulation.
+ *
+ * Copyright (C) 2015 Jean-Christophe Dubois <[email protected]>
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License as
+ * published by the Free Software Foundation; either version 2 or
+ * (at your option) version 3 of the License.
+ *
+ * This program is distributed in the hope that it will be useful,

hw/gpio/imx_gpio.c line 21

Quoted 10 lines
hw/gpio/imx_gpio.c new file +10 -0
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include "hw/gpio/imx_gpio.h"
+

Cutting from here [1] ...

Quoted 3 lines

hw/gpio/imx_gpio.c line 22

Quoted 1 line
hw/gpio/imx_gpio.c new file +1 -0
+#ifdef DEBUG_GPIO

This needs a less generic name, such as DEBUG_IMX_GPIO. This allows defining at configure as a -D CFLAG without other consequences.

OK

Quoted 4 lines

hw/gpio/imx_gpio.c line 24

Quoted 3 lines
hw/gpio/imx_gpio.c new file +2 -0
+#  define DPRINTF(fmt, args...) \
+          do { fprintf(stder, "%s: " fmt , __func__, ##args); } while (0)

stderr.

OK (it is caught by the "always compile" scheme now)

Quoted 5 lines

hw/gpio/imx_gpio.c line 26

Quoted 3 lines
hw/gpio/imx_gpio.c new file +2 -0
+
+static char const *imx_gpio_reg_name(uint32_t reg)

const char *. Just for consistency with code base. A grep of "const char \*" suggests this is the norm. We could add a CODING_STYLE.

OK

Quoted 48 lines
Quoted 17 lines
hw/gpio/imx_gpio.c new file +16 -0
+{
+    switch (reg) {
+    case DR_ADDR:
+        return "DR";
+    case GDIR_ADDR:
+        return "GDIR";
+    case PSR_ADDR:
+        return "PSR";
+    case ICR1_ADDR:
+        return "ICR1";
+    case ICR2_ADDR:
+        return "ICR2";
+    case IMR_ADDR:
+        return "IMR";
+    case ISR_ADDR:
+        return "ISR";

hw/gpio/imx_gpio.c line 52

Quoted 10 lines
hw/gpio/imx_gpio.c new file +10 -0
+    case EDGE_SEL_ADDR:
+        /* This register is not present in i.MX31 */
+        return "EDGE_SEL";
+    default:
+        return "[?]";
+    }
+}
+#else /* DEBUG_GPIO */
+#  define DPRINTF(fmt, args...) do {} while (0)
+#endif /* DEBUG_GPIO */

[1] ... to here. I would try something like:

#ifndef DEBUG_GPIO
#define DEBUG_GPIO 1
#endif

#define DPRINTF(fmt, args...) \
           do { fprintf(stder, "%s: " fmt , __func__, ##args); } while (0)

static char const *imx_gpio_reg_name(uint32_t reg)

{
     switch (reg) {
     case DR_ADDR:
         return "DR";
...
         return "EDGE_SEL";
     default:
         return "[?]";
     }
}
Quoted 3 lines

No, #else needed. As the DPRINTF bodies are always compiled you also need (and want) always-compilation of gpio_reg_name and without the unused static warn.

OK

Quoted 29 lines
Quoted 8 lines
hw/gpio/imx_gpio.c new file +7 -0
+
+typedef enum IMXGPIOLevel {
+    IMX_GPIO_LEVEL_LOW = 0,
+    IMX_GPIO_LEVEL_HIGH = 1,
+} IMXGPIOLevel;
+
+static void imx_gpio_update_int(IMXGPIOState *s)

hw/gpio/imx_gpio.c line 69

Quoted 10 lines
hw/gpio/imx_gpio.c new file +10 -0
+{
+    qemu_set_irq(s->irq, s->isr ? 1 : 0);
+}
+
+static void imx_gpio_set_int_line(IMXGPIOState *s, int line, IMXGPIOLevel level)
+{
+    /* if this signal isn't configured as an interrupt source, nothing to do */
+    if (!extract32(s->imr, line, 1)) {
+        return;
+    }

I had a read up of the TRM and found this:

"
Interrupt Status bits. Bit i of this register is asserted (active high) when the active condition is detected on the GPIO input and is waiting for service. The value of this register is independent of the value in the IMR register. When the active condition has been detected, the corresponding bit remains set until cleared by software.
"

Quoted 3 lines

IMR does not affect ISR, IMR only bitwise gates the top level interrupt signalling. ISR is unaffected. I think this short return should be dropped IIUC.

OK, then I have to also use IMR when raising interrupts in imx_gpio_update_int()

Quoted 4 lines

hw/gpio/imx_gpio.c line 71

Quoted 3 lines
hw/gpio/imx_gpio.c new file +2 -0
+
+    /* When set, EDGE_SEL override the ICR config */

"overrides"

OK

Quoted 107 lines
Quoted 107 lines
hw/gpio/imx_gpio.c new file +106 -0
+    if (extract32(s->edge_sel, line, 1)) {
+        /* we detect interrupt on rising and falling edge */
+        if (extract32(s->psr, line, 1) != level) {
+            /* level changed */
+            s->isr = deposit32(s->isr, line, 1, 1);
+        }
+    } else if (extract64(s->icr, 2*line + 1, 1)) {
+        /* interrupt is edge sensitive */
+        if (extract32(s->psr, line, 1) != level) {
+            /* level changed */
+            if (extract64(s->icr, 2*line, 1) != level) {
+                s->isr = deposit32(s->isr, line, 1, 1);
+            }
+        }
+    } else {
+        /* interrupt is level sensitive */
+        if (extract64(s->icr, 2*line, 1) == level) {
+            s->isr = deposit32(s->isr, line, 1, 1);
+        }
+    }
+}
+
+static void imx_gpio_set(void *opaque, int line, int level)
+{
+    IMXGPIOState *s = IMX_GPIO(opaque);
+    IMXGPIOLevel imx_level = level ? IMX_GPIO_LEVEL_HIGH : IMX_GPIO_LEVEL_LOW;
+
+    /* if the line is configured as output nothing to do */
+    if (extract32(s->gdir, line, 1)) {
+        /* actually we should not be called */
+        qemu_log_mask(LOG_GUEST_ERROR, "%s[%s]: requesting to set line %d"
+                      " to %d but this is an output line\n",
+                      TYPE_IMX_GPIO, __func__, line, level);
+        return;
+    }
+
+    imx_gpio_set_int_line(s, line, imx_level);
+
+    /* this is an input signal, so set PSR */
+    s->psr = deposit32(s->psr, line, 1, imx_level);
+
+    imx_gpio_update_int(s);
+}
+
+static void imx_gpio_set_all_int_lines(IMXGPIOState *s)
+{
+    int i;
+
+    for (i = 0; i < IMX_GPIO_MAX_PIN_COUNT; i++) {
+        IMXGPIOLevel imx_level = extract32(s->psr, i, 1);
+        imx_gpio_set_int_line(s, i, imx_level);
+    }
+
+    imx_gpio_update_int(s);
+}
+
+static inline void imx_gpio_set_all_output_lines(IMXGPIOState *s)
+{
+    int i;
+
+    for (i = 0; i < IMX_GPIO_MAX_PIN_COUNT; i++) {
+        /*
+         * if the line is set as output, then forward the line
+         * level to its user.
+         */
+        if (extract32(s->gdir, i, 1) && s->output[i]) {
+            qemu_set_irq(s->output[i], extract32(s->dr, i, 1));
+        }
+    }
+}
+
+static uint64_t imx_gpio_read(void *opaque, hwaddr offset, unsigned size)
+{
+    IMXGPIOState *s = IMX_GPIO(opaque);
+    uint32_t reg_value = 0;
+
+    switch (offset) {
+    case DR_ADDR:
+        /*
+         * depending on the "line" configuration, the bit values
+         * are coming either from DR or PSR
+         */
+        reg_value = (s->dr & s->gdir) | (s->psr & ~s->gdir);
+        break;
+
+    case GDIR_ADDR:
+        reg_value = s->gdir;
+        break;
+
+    case PSR_ADDR:
+        reg_value = s->psr;
+        break;
+
+    case ICR1_ADDR:
+        reg_value = extract64(s->icr, 0, 32);
+        break;
+
+    case ICR2_ADDR:
+        reg_value = extract64(s->icr, 32, 32);
+        break;
+
+    case IMR_ADDR:
+        reg_value = s->imr;
+        break;
+
+    case ISR_ADDR:
Quoted 8 lines

hw/gpio/imx_gpio.c line 182

Quoted 5 lines
hw/gpio/imx_gpio.c new file +5 -0
+        reg_value = s->isr;
+        break;
+
+    case EDGE_SEL_ADDR:
+        /* This register is not present in i.MX31 */

I would remove all references to i.MX31, as this model is not specific to either 31 or 25. A comment like this could go in the 31 machine model however on the boolean prop setter.

OK

Quoted 7 lines

hw/gpio/imx_gpio.c line 187

Quoted 6 lines
hw/gpio/imx_gpio.c new file +5 -0
+        if (s->has_edge_sel) {
+            reg_value = s->edge_sel;
+        } else {
+            qemu_log_mask(LOG_GUEST_ERROR, "%s[%s]: EDGE_SEL register not "
+                          "present on imx31 type\n", TYPE_IMX_GPIO, __func__);

-"imx31"

OK

Quoted 13 lines

hw/gpio/imx_gpio.c line 197

Quoted 11 lines
hw/gpio/imx_gpio.c new file +10 -0
+        }
+        break;
+
+    default:
+        qemu_log_mask(LOG_GUEST_ERROR, "%s[%s]: Bad register at offset %d\n",
+                      TYPE_IMX_GPIO, __func__, (int)offset);
+        break;
+    }
+
+    DPRINTF("(%s) = 0x%"PRIx32"\n", imx_gpio_reg_name(offset), reg_value);

I would work __func__ in there like with the LOG_GUEST_ERRORS to make it more identifiable in big logs.

It is taken care of by DPRINTF()

Quoted 33 lines
Quoted 13 lines
hw/gpio/imx_gpio.c new file +12 -0
+
+    return reg_value;
+}
+
+static void imx_gpio_write(void *opaque, hwaddr offset, uint64_t value,
+                           unsigned size)
+{
+    IMXGPIOState *s = IMX_GPIO(opaque);
+
+    DPRINTF("(%s, value = 0x%"PRIx32")\n", imx_gpio_reg_name(offset),
+            (uint32_t)value);
+

hw/gpio/imx_gpio.c line 219

Quoted 10 lines
hw/gpio/imx_gpio.c new file +10 -0
+    switch (offset) {
+    case DR_ADDR:
+        s->dr = value;
+        imx_gpio_set_all_output_lines(s);
+        break;
+
+    case GDIR_ADDR:
+        s->gdir = value;
+        imx_gpio_set_all_output_lines(s);
+        imx_gpio_set_all_int_lines(s);

So does this need to bitwise set PSR to the most input values on a transition from output to input? You may need extra state, a uint32_t which captures and saves the input state regardless or gdir. Then set_all_output_lines is renamed to set_psr() or something like, setting psr to input values on transitions (that is set_psr() will always set every pad value). I think the rest of the logic will just work, as forcing psr to the last (and current) input value will non trigger edge interrupts, which should be the case if my interpretation of TRM is correct.

Quoted 2 lines

This handles cases where there is circuitry that can change directionality at runtime with the proper clamps (e.g. I2C).

So does this mean we should accept (as opposed to the v2 code) the "guest" setting the pin even if it is configured as output (just to be able to remember it has been set at some point)? And so without emitting a log to warm the user of the "strange behavior ...

Now is there so many "boards" using a GPIO both ways? You could if you were doing some kind of bitbanging i2C device for example but usually freescale devices have enough built in I2C devices to avoid this.

But OK, let's do it this way ...

Quoted 17 lines
Quoted 6 lines
hw/gpio/imx_gpio.c new file +5 -0
+        break;
+
+    case ICR1_ADDR:
+        s->icr = deposit64(s->icr, 0, 32, value);
+        imx_gpio_set_all_int_lines(s);

hw/gpio/imx_gpio.c line 234

Quoted 10 lines
hw/gpio/imx_gpio.c new file +10 -0
+        break;
+
+    case ICR2_ADDR:
+        s->icr = deposit64(s->icr, 32, 32, value);
+        imx_gpio_set_all_int_lines(s);
+        break;
+
+    case IMR_ADDR:
+        s->imr = value;
+        imx_gpio_set_all_int_lines(s);

Following on from IMR comment, this would be replacable with update_int().

OK, I changed update_int() to use IMR.

Quoted 18 lines
Quoted 18 lines
hw/gpio/imx_gpio.c new file +17 -0
+        break;
+
+    case ISR_ADDR:
+        s->isr |= ~value;
+        imx_gpio_set_all_int_lines(s);
+        break;
+
+    case EDGE_SEL_ADDR:
+        /* This register is not present in i.MX31 */
+        if (s->has_edge_sel) {
+            s->edge_sel = value;
+            imx_gpio_set_all_int_lines(s);
+        } else {
+            qemu_log_mask(LOG_GUEST_ERROR, "%s[%s]: EDGE_SEL register not "
+                          "present on imx31 type\n", TYPE_IMX_GPIO, __func__);
+        }
+        break;
Quoted 8 lines

hw/gpio/imx_gpio.c line 196

Quoted 7 lines
hw/gpio/imx_gpio.c new file +7 -0
+
+    default:
+        qemu_log_mask(LOG_GUEST_ERROR, "%s[%s]: Bad register at offset %d\n",
+                      TYPE_IMX_GPIO, __func__, (int)offset);
+        break;
+    }
+

Same comments as for read.

OK

Quoted 18 lines
Quoted 18 lines
hw/gpio/imx_gpio.c new file +17 -0
+    return;
+}
+
+static const MemoryRegionOps imx_gpio_ops = {
+    .read = imx_gpio_read,
+    .write = imx_gpio_write,
+    .valid.min_access_size = 4,
+    .valid.max_access_size = 4,
+    .endianness = DEVICE_NATIVE_ENDIAN,
+};
+
+static const VMStateDescription vmstate_imx_gpio = {
+    .name = TYPE_IMX_GPIO,
+    .version_id = 1,
+    .minimum_version_id = 1,
+    .minimum_version_id_old = 1,
+    .fields = (VMStateField[]) {
Quoted 8 lines

hw/gpio/imx_gpio.c line 282

Quoted 7 lines
hw/gpio/imx_gpio.c new file +7 -0
+        VMSTATE_UINT32(dr, IMXGPIOState),
+        VMSTATE_UINT32(gdir, IMXGPIOState),
+        VMSTATE_UINT32(psr, IMXGPIOState),
+        VMSTATE_UINT64(icr, IMXGPIOState),
+        VMSTATE_UINT32(imr, IMXGPIOState),
+        VMSTATE_UINT32(isr, IMXGPIOState),
+        /* This register is not present in i.MX31 */

Drop comment.

OK

Quoted 107 lines
Quoted 107 lines
hw/gpio/imx_gpio.c new file +65 -0
+        VMSTATE_UINT32(edge_sel, IMXGPIOState),
+        VMSTATE_BOOL(has_edge_sel, IMXGPIOState),
+        VMSTATE_END_OF_LIST()
+    }
+};
+
+static Property imx_gpio_properties[] = {
+    DEFINE_PROP_BOOL("has-edge-sel", IMXGPIOState, has_edge_sel, true),
+    DEFINE_PROP_END_OF_LIST(),
+};
+
+static void imx_gpio_reset(DeviceState *dev)
+{
+    IMXGPIOState *s = IMX_GPIO(dev);
+
+    s->dr       = 0;
+    s->gdir     = 0;
+    s->psr      = 0;
+    s->icr      = 0;
+    s->imr      = 0;
+    s->isr      = 0;
+    s->edge_sel = 0;
+
+    imx_gpio_set_all_output_lines(s);
+    imx_gpio_update_int(s);
+}
+
+static void imx_gpio_realize(DeviceState *dev, Error **errp)
+{
+    IMXGPIOState *s = IMX_GPIO(dev);
+
+    memory_region_init_io(&s->iomem, OBJECT(s), &imx_gpio_ops, s,
+                          TYPE_IMX_GPIO, IMX_GPIO_MEM_SIZE);
+
+    qdev_init_gpio_in(DEVICE(s), imx_gpio_set, IMX_GPIO_MAX_PIN_COUNT);
+    qdev_init_gpio_out(DEVICE(s), s->output, IMX_GPIO_MAX_PIN_COUNT);
+
+    sysbus_init_irq(SYS_BUS_DEVICE(dev), &s->irq);
+    sysbus_init_mmio(SYS_BUS_DEVICE(dev), &s->iomem);
+}
+
+static void imx_gpio_class_init(ObjectClass *klass, void *data)
+{
+    DeviceClass *dc = DEVICE_CLASS(klass);
+
+    dc->realize = imx_gpio_realize;
+    dc->reset = imx_gpio_reset;
+    dc->props = imx_gpio_properties;
+    dc->vmsd = &vmstate_imx_gpio;
+    dc->desc = "i.MX GPIO controller";
+}
+
+static const TypeInfo imx_gpio_info = {
+    .name = TYPE_IMX_GPIO,
+    .parent = TYPE_SYS_BUS_DEVICE,
+    .instance_size = sizeof(IMXGPIOState),
+    .class_init = imx_gpio_class_init,
+};
+
+static void imx_gpio_register_types(void)
+{
+    type_register_static(&imx_gpio_info);
+}
+
+type_init(imx_gpio_register_types)
include/hw/gpio/imx_gpio.h new file +35 -0
new file mode 100644
index 0000000..d91997a
@@ -0,0 +1,63 @@
+/*
+ * i.MX processors GPIO registers definition.
+ *
+ * Copyright (C) 2015 Jean-Christophe Dubois <[email protected]>
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License as
+ * published by the Free Software Foundation; either version 2 or
+ * (at your option) version 3 of the License.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, see <http://www.gnu.org/licenses/>.
+ */
+
+#ifndef __IMX_GPIO_H_
+#define __IMX_GPIO_H_
+
+#include <hw/sysbus.h>
+
+#define TYPE_IMX_GPIO "imx.gpio"
+#define IMX_GPIO(obj) OBJECT_CHECK(IMXGPIOState, (obj), TYPE_IMX_GPIO)
+
+#define IMX_GPIO_MEM_SIZE 0x20
+
+/* i.MX GPIO memory map */
+#define DR_ADDR             0x00 /* DATA REGISTER */
+#define GDIR_ADDR           0x04 /* DIRECTION REGISTER */
+#define PSR_ADDR            0x08 /* PAD STATUS REGISTER */
+#define ICR1_ADDR           0x0c /* INTERRUPT CONFIGURATION REGISTER 1 */
+#define ICR2_ADDR           0x10 /* INTERRUPT CONFIGURATION REGISTER 2 */
Quoted 8 lines

include/hw/gpio/imx_gpio.h line 41

Quoted 6 lines
include/hw/gpio/imx_gpio.h new file +6 -0
+#define IMR_ADDR            0x14 /* INTERRUPT MASK REGISTER */
+#define ISR_ADDR            0x18 /* INTERRUPT STATUS REGISTER */
+#define EDGE_SEL_ADDR       0x1c /* EDGE SEL REGISTER */
+
+#define IMX_GPIO_MAX_PIN_COUNT 32
+

MAX indicates it is configurable. Does the 25 and 31 down configure the IP to the max of 32? If so you can probably drop the MAX.

OK

Quoted 2 lines

If not,
it should be converted to a property.

We will revisit this when we add modifications for i.MX6.

Quoted 29 lines

Regards,
Peter

Quoted 25 lines
include/hw/gpio/imx_gpio.h new file +22 -1
+typedef struct IMXGPIOState {
+    /*< private >*/
+    SysBusDevice parent_obj;
+
+    /*< public >*/
+    MemoryRegion iomem;
+
+    uint32_t dr;
+    uint32_t gdir;
+    uint32_t psr;
+    uint64_t icr;
+    uint32_t imr;
+    uint32_t isr;
+    bool has_edge_sel;
+    /* This register is not present in i.MX31 */
+    uint32_t edge_sel;
+
+    qemu_irq irq;
+    qemu_irq output[IMX_GPIO_MAX_PIN_COUNT];
+} IMXGPIOState;
+
+#endif /* __IMX_GPIO_H_ */
--

2.1.4

#4 lore raw

PATCH v2 2/3 i.MX: Add GPIO devices to i.MX31 SOC

In reply to Jean-Christophe Dubois

---

Changes since v1:
* Use "has-edge-sel" property for i.MX31
* various coding style cleanup

 hw/arm/fsl-imx31.c         | 34 ++++++++++++++++++++++++++++++++++
 include/hw/arm/fsl-imx31.h | 12 ++++++++++++
 2 files changed, 46 insertions(+)
hw/arm/fsl-imx31.c +34 -0
index 87548c8..0d31486 100644
@@ -55,6 +55,11 @@ static void fsl_imx31_init(Object *obj)
         object_initialize(&s->i2c[i], sizeof(s->i2c[i]), TYPE_IMX_I2C);
         qdev_set_parent_bus(DEVICE(&s->i2c[i]), sysbus_get_default());
     }
+
+    for (i = 0; i < FSL_IMX31_NUM_GPIOS; i++) {
+        object_initialize(&s->gpio[i], sizeof(s->gpio[i]), TYPE_IMX_GPIO);
+        qdev_set_parent_bus(DEVICE(&s->gpio[i]), sysbus_get_default());
+    }
 }
 
 static void fsl_imx31_realize(DeviceState *dev, Error **errp)
@@ -184,6 +189,35 @@ static void fsl_imx31_realize(DeviceState *dev, Error **errp)
                                             i2c_table[i].irq));
     }
 
+    /* Initialize all GPIOs */
+    for (i = 0; i < FSL_IMX31_NUM_GPIOS; i++) {
+        static const struct {
+            hwaddr addr;
+            unsigned int irq;
+        } gpio_table[FSL_IMX31_NUM_GPIOS] = {
+            { FSL_IMX31_GPIO1_ADDR, FSL_IMX31_GPIO1_IRQ },
+            { FSL_IMX31_GPIO2_ADDR, FSL_IMX31_GPIO2_IRQ },
+            { FSL_IMX31_GPIO3_ADDR, FSL_IMX31_GPIO3_IRQ }
+        };
+
+        object_property_set_bool(OBJECT(&s->gpio[i]), false, "has-edge-sel",
+                                 &err);
+        if (err) {
+            error_propagate(errp, err);
+            return;
+        }
+        object_property_set_bool(OBJECT(&s->gpio[i]), true, "realized", &err);
+        if (err) {
+            error_propagate(errp, err);
+            return;
+        }
+        sysbus_mmio_map(SYS_BUS_DEVICE(&s->gpio[i]), 0, gpio_table[i].addr);
+        /* Connect GPIO IRQ to PIC */
+        sysbus_connect_irq(SYS_BUS_DEVICE(&s->gpio[i]), 0,
+                           qdev_get_gpio_in(DEVICE(&s->avic),
+                                            gpio_table[i].irq));
+    }
+
     /* On a real system, the first 16k is a `secure boot rom' */
     memory_region_init_rom_device(&s->secure_rom, NULL, NULL, NULL,
                                   "imx31.secure_rom",
include/hw/arm/fsl-imx31.h +12 -0
index 891166f..5e8f795 100644
@@ -24,6 +24,7 @@
 #include "hw/timer/imx_gpt.h"
 #include "hw/timer/imx_epit.h"
 #include "hw/i2c/imx_i2c.h"
+#include "hw/gpio/imx_gpio.h"
 #include "exec/memory.h"
 
 #define TYPE_FSL_IMX31 "fsl,imx31"
@@ -32,6 +33,7 @@
 #define FSL_IMX31_NUM_UARTS 2
 #define FSL_IMX31_NUM_EPITS 2
 #define FSL_IMX31_NUM_I2CS 3
+#define FSL_IMX31_NUM_GPIOS 3
 
 typedef struct FslIMX31State {
     /*< private >*/
@@ -45,6 +47,7 @@ typedef struct FslIMX31State {
     IMXGPTState    gpt;
     IMXEPITState   epit[FSL_IMX31_NUM_EPITS];
     IMXI2CState    i2c[FSL_IMX31_NUM_I2CS];
+    IMXGPIOState   gpio[FSL_IMX31_NUM_GPIOS];
     MemoryRegion   secure_rom;
     MemoryRegion   rom;
     MemoryRegion   iram;
@@ -77,6 +80,12 @@ typedef struct FslIMX31State {
 #define FSL_IMX31_EPIT1_SIZE            0x4000
 #define FSL_IMX31_EPIT2_ADDR            0x53F98000
 #define FSL_IMX31_EPIT2_SIZE            0x4000
+#define FSL_IMX31_GPIO3_ADDR            0x53FA4000
+#define FSL_IMX31_GPIO3_SIZE            0x4000
+#define FSL_IMX31_GPIO1_ADDR            0x53FCC000
+#define FSL_IMX31_GPIO1_SIZE            0x4000
+#define FSL_IMX31_GPIO2_ADDR            0x53FD0000
+#define FSL_IMX31_GPIO2_SIZE            0x4000
 #define FSL_IMX31_AVIC_ADDR             0x68000000
 #define FSL_IMX31_AVIC_SIZE             0x100
 #define FSL_IMX31_SDRAM0_ADDR           0x80000000
@@ -106,5 +115,8 @@ typedef struct FslIMX31State {
 #define FSL_IMX31_I2C1_IRQ              10
 #define FSL_IMX31_I2C2_IRQ              4
 #define FSL_IMX31_I2C3_IRQ              3
+#define FSL_IMX31_GPIO1_IRQ             52
+#define FSL_IMX31_GPIO2_IRQ             51
+#define FSL_IMX31_GPIO3_IRQ             56
 
 #endif /* FSL_IMX31_H */
Signature · 1 line
2.1.4
#5 lore raw

Re: [Qemu-devel] [PATCH v2 2/3] i.MX: Add GPIO devices to i.MX31 SOC

Quoted 39 lines · Jean-Christophe Dubois

---

Changes since v1:
* Use "has-edge-sel" property for i.MX31
* various coding style cleanup

 hw/arm/fsl-imx31.c         | 34 ++++++++++++++++++++++++++++++++++
 include/hw/arm/fsl-imx31.h | 12 ++++++++++++
 2 files changed, 46 insertions(+)
hw/arm/fsl-imx31.c +12 -0
index 87548c8..0d31486 100644
@@ -55,6 +55,11 @@ static void fsl_imx31_init(Object *obj)
         object_initialize(&s->i2c[i], sizeof(s->i2c[i]), TYPE_IMX_I2C);
         qdev_set_parent_bus(DEVICE(&s->i2c[i]), sysbus_get_default());
     }
+
+    for (i = 0; i < FSL_IMX31_NUM_GPIOS; i++) {
+        object_initialize(&s->gpio[i], sizeof(s->gpio[i]), TYPE_IMX_GPIO);
+        qdev_set_parent_bus(DEVICE(&s->gpio[i]), sysbus_get_default());
+    }
 }
 
 static void fsl_imx31_realize(DeviceState *dev, Error **errp)
@@ -184,6 +189,35 @@ static void fsl_imx31_realize(DeviceState *dev, Error **errp)
                                             i2c_table[i].irq));
     }
 
+    /* Initialize all GPIOs */
+    for (i = 0; i < FSL_IMX31_NUM_GPIOS; i++) {
+        static const struct {
+            hwaddr addr;
+            unsigned int irq;
+        } gpio_table[FSL_IMX31_NUM_GPIOS] = {
+            { FSL_IMX31_GPIO1_ADDR, FSL_IMX31_GPIO1_IRQ },

hw/arm/fsl-imx31.c static void fsl_imx31_realize(DeviceState *dev, Error **errp) · line 208

Quoted 10 lines
hw/arm/fsl-imx31.c +10 -0
+            { FSL_IMX31_GPIO2_ADDR, FSL_IMX31_GPIO2_IRQ },
+            { FSL_IMX31_GPIO3_ADDR, FSL_IMX31_GPIO3_IRQ }
+        };
+
+        object_property_set_bool(OBJECT(&s->gpio[i]), false, "has-edge-sel",
+                                 &err);
+        if (err) {
+            error_propagate(errp, err);
+            return;
+        }

This one is fatal, so you can change to &error_abort and drop the if-propagate.

Regards,
Peter

Quoted 68 lines
hw/arm/fsl-imx31.c +12 -0
+        object_property_set_bool(OBJECT(&s->gpio[i]), true, "realized", &err);
+        if (err) {
+            error_propagate(errp, err);
+            return;
+        }
+        sysbus_mmio_map(SYS_BUS_DEVICE(&s->gpio[i]), 0, gpio_table[i].addr);
+        /* Connect GPIO IRQ to PIC */
+        sysbus_connect_irq(SYS_BUS_DEVICE(&s->gpio[i]), 0,
+                           qdev_get_gpio_in(DEVICE(&s->avic),
+                                            gpio_table[i].irq));
+    }
+
     /* On a real system, the first 16k is a `secure boot rom' */
     memory_region_init_rom_device(&s->secure_rom, NULL, NULL, NULL,
                                   "imx31.secure_rom",
include/hw/arm/fsl-imx31.h +12 -1
index 891166f..5e8f795 100644
@@ -24,6 +24,7 @@
 #include "hw/timer/imx_gpt.h"
 #include "hw/timer/imx_epit.h"
 #include "hw/i2c/imx_i2c.h"
+#include "hw/gpio/imx_gpio.h"
 #include "exec/memory.h"
 
 #define TYPE_FSL_IMX31 "fsl,imx31"
@@ -32,6 +33,7 @@
 #define FSL_IMX31_NUM_UARTS 2
 #define FSL_IMX31_NUM_EPITS 2
 #define FSL_IMX31_NUM_I2CS 3
+#define FSL_IMX31_NUM_GPIOS 3
 
 typedef struct FslIMX31State {
     /*< private >*/
@@ -45,6 +47,7 @@ typedef struct FslIMX31State {
     IMXGPTState    gpt;
     IMXEPITState   epit[FSL_IMX31_NUM_EPITS];
     IMXI2CState    i2c[FSL_IMX31_NUM_I2CS];
+    IMXGPIOState   gpio[FSL_IMX31_NUM_GPIOS];
     MemoryRegion   secure_rom;
     MemoryRegion   rom;
     MemoryRegion   iram;
@@ -77,6 +80,12 @@ typedef struct FslIMX31State {
 #define FSL_IMX31_EPIT1_SIZE            0x4000
 #define FSL_IMX31_EPIT2_ADDR            0x53F98000
 #define FSL_IMX31_EPIT2_SIZE            0x4000
+#define FSL_IMX31_GPIO3_ADDR            0x53FA4000
+#define FSL_IMX31_GPIO3_SIZE            0x4000
+#define FSL_IMX31_GPIO1_ADDR            0x53FCC000
+#define FSL_IMX31_GPIO1_SIZE            0x4000
+#define FSL_IMX31_GPIO2_ADDR            0x53FD0000
+#define FSL_IMX31_GPIO2_SIZE            0x4000
 #define FSL_IMX31_AVIC_ADDR             0x68000000
 #define FSL_IMX31_AVIC_SIZE             0x100
 #define FSL_IMX31_SDRAM0_ADDR           0x80000000
@@ -106,5 +115,8 @@ typedef struct FslIMX31State {
 #define FSL_IMX31_I2C1_IRQ              10
 #define FSL_IMX31_I2C2_IRQ              4
 #define FSL_IMX31_I2C3_IRQ              3
+#define FSL_IMX31_GPIO1_IRQ             52
+#define FSL_IMX31_GPIO2_IRQ             51
+#define FSL_IMX31_GPIO3_IRQ             56
 
 #endif /* FSL_IMX31_H */
--

2.1.4

#6 lore raw

PATCH v2 3/3 i.MX: Add GPIO devices to i.MX25 SOC

In reply to Jean-Christophe Dubois

---

Changes since v1:
* various coding style cleanup

 hw/arm/fsl-imx25.c         | 29 +++++++++++++++++++++++++++++
 include/hw/arm/fsl-imx25.h | 15 +++++++++++++++
 2 files changed, 44 insertions(+)
hw/arm/fsl-imx25.c +29 -0
index 6d157c9..86fde42 100644
@@ -63,6 +63,11 @@ static void fsl_imx25_init(Object *obj)
         object_initialize(&s->i2c[i], sizeof(s->i2c[i]), TYPE_IMX_I2C);
         qdev_set_parent_bus(DEVICE(&s->i2c[i]), sysbus_get_default());
     }
+
+    for (i = 0; i < FSL_IMX25_NUM_GPIOS; i++) {
+        object_initialize(&s->gpio[i], sizeof(s->gpio[i]), TYPE_IMX_GPIO);
+        qdev_set_parent_bus(DEVICE(&s->gpio[i]), sysbus_get_default());
+    }
 }
 
 static void fsl_imx25_realize(DeviceState *dev, Error **errp)
@@ -214,6 +219,30 @@ static void fsl_imx25_realize(DeviceState *dev, Error **errp)
                                             i2c_table[i].irq));
     }
 
+    /* Initialize all GPIOs */
+    for (i = 0; i < FSL_IMX25_NUM_GPIOS; i++) {
+        static const struct {
+            hwaddr addr;
+            unsigned int irq;
+        } gpio_table[FSL_IMX25_NUM_GPIOS] = {
+            { FSL_IMX25_GPIO1_ADDR, FSL_IMX25_GPIO1_IRQ },
+            { FSL_IMX25_GPIO2_ADDR, FSL_IMX25_GPIO2_IRQ },
+            { FSL_IMX25_GPIO3_ADDR, FSL_IMX25_GPIO3_IRQ },
+            { FSL_IMX25_GPIO4_ADDR, FSL_IMX25_GPIO4_IRQ }
+        };
+
+        object_property_set_bool(OBJECT(&s->gpio[i]), true, "realized", &err);
+        if (err) {
+            error_propagate(errp, err);
+            return;
+        }
+        sysbus_mmio_map(SYS_BUS_DEVICE(&s->gpio[i]), 0, gpio_table[i].addr);
+        /* Connect GPIO IRQ to PIC */
+        sysbus_connect_irq(SYS_BUS_DEVICE(&s->gpio[i]), 0,
+                           qdev_get_gpio_in(DEVICE(&s->avic),
+                                            gpio_table[i].irq));
+    }
+
     /* initialize 2 x 16 KB ROM */
     memory_region_init_rom_device(&s->rom[0], NULL, NULL, NULL,
                                   "imx25.rom0", FSL_IMX25_ROM0_SIZE, &err);
include/hw/arm/fsl-imx25.h +15 -0
index 7f6bb64..73f50c6 100644
@@ -25,6 +25,7 @@
 #include "hw/timer/imx_epit.h"
 #include "hw/net/imx_fec.h"
 #include "hw/i2c/imx_i2c.h"
+#include "hw/gpio/imx_gpio.h"
 #include "exec/memory.h"
 
 #define TYPE_FSL_IMX25 "fsl,imx25"
@@ -34,6 +35,7 @@
 #define FSL_IMX25_NUM_GPTS 4
 #define FSL_IMX25_NUM_EPITS 2
 #define FSL_IMX25_NUM_I2CS 3
+#define FSL_IMX25_NUM_GPIOS 4
 
 typedef struct FslIMX25State {
     /*< private >*/
@@ -48,6 +50,7 @@ typedef struct FslIMX25State {
     IMXEPITState   epit[FSL_IMX25_NUM_EPITS];
     IMXFECState    fec;
     IMXI2CState    i2c[FSL_IMX25_NUM_I2CS];
+    IMXGPIOState   gpio[FSL_IMX25_NUM_GPIOS];
     MemoryRegion   rom[2];
     MemoryRegion   iram;
     MemoryRegion   iram_alias;
@@ -204,6 +207,14 @@ typedef struct FslIMX25State {
 #define FSL_IMX25_EPIT1_SIZE    0x4000
 #define FSL_IMX25_EPIT2_ADDR    0x53F98000
 #define FSL_IMX25_EPIT2_SIZE    0x4000
+#define FSL_IMX25_GPIO4_ADDR    0x53F9C000
+#define FSL_IMX25_GPIO4_SIZE    0x4000
+#define FSL_IMX25_GPIO3_ADDR    0x53FA4000
+#define FSL_IMX25_GPIO3_SIZE    0x4000
+#define FSL_IMX25_GPIO1_ADDR    0x53FCC000
+#define FSL_IMX25_GPIO1_SIZE    0x4000
+#define FSL_IMX25_GPIO2_ADDR    0x53FD0000
+#define FSL_IMX25_GPIO2_SIZE    0x4000
 #define FSL_IMX25_AVIC_ADDR     0x68000000
 #define FSL_IMX25_AVIC_SIZE     0x4000
 #define FSL_IMX25_IRAM_ADDR     0x78000000
@@ -230,5 +241,9 @@ typedef struct FslIMX25State {
 #define FSL_IMX25_I2C1_IRQ      3
 #define FSL_IMX25_I2C2_IRQ      4
 #define FSL_IMX25_I2C3_IRQ      10
+#define FSL_IMX25_GPIO1_IRQ     52
+#define FSL_IMX25_GPIO2_IRQ     51
+#define FSL_IMX25_GPIO3_IRQ     16
+#define FSL_IMX25_GPIO4_IRQ     23
 
 #endif /* FSL_IMX25_H */
Signature · 1 line
2.1.4
#7 lore raw

Re: [Qemu-devel] [PATCH v2 3/3] i.MX: Add GPIO devices to i.MX25 SOC

Quoted 38 lines · Jean-Christophe Dubois

---

Changes since v1:
* various coding style cleanup

 hw/arm/fsl-imx25.c         | 29 +++++++++++++++++++++++++++++
 include/hw/arm/fsl-imx25.h | 15 +++++++++++++++
 2 files changed, 44 insertions(+)
hw/arm/fsl-imx25.c +12 -0
index 6d157c9..86fde42 100644
@@ -63,6 +63,11 @@ static void fsl_imx25_init(Object *obj)
         object_initialize(&s->i2c[i], sizeof(s->i2c[i]), TYPE_IMX_I2C);
         qdev_set_parent_bus(DEVICE(&s->i2c[i]), sysbus_get_default());
     }
+
+    for (i = 0; i < FSL_IMX25_NUM_GPIOS; i++) {
+        object_initialize(&s->gpio[i], sizeof(s->gpio[i]), TYPE_IMX_GPIO);
+        qdev_set_parent_bus(DEVICE(&s->gpio[i]), sysbus_get_default());
+    }
 }
 
 static void fsl_imx25_realize(DeviceState *dev, Error **errp)
@@ -214,6 +219,30 @@ static void fsl_imx25_realize(DeviceState *dev, Error **errp)
                                             i2c_table[i].irq));
     }
 
+    /* Initialize all GPIOs */
+    for (i = 0; i < FSL_IMX25_NUM_GPIOS; i++) {
+        static const struct {
+            hwaddr addr;
+            unsigned int irq;
+        } gpio_table[FSL_IMX25_NUM_GPIOS] = {
+            { FSL_IMX25_GPIO1_ADDR, FSL_IMX25_GPIO1_IRQ },

hw/arm/fsl-imx25.c static void fsl_imx25_realize(DeviceState *dev, Error **errp) · line 238

Quoted 10 lines
hw/arm/fsl-imx25.c +10 -0
+            { FSL_IMX25_GPIO2_ADDR, FSL_IMX25_GPIO2_IRQ },
+            { FSL_IMX25_GPIO3_ADDR, FSL_IMX25_GPIO3_IRQ },
+            { FSL_IMX25_GPIO4_ADDR, FSL_IMX25_GPIO4_IRQ }
+        };
+
+        object_property_set_bool(OBJECT(&s->gpio[i]), true, "realized", &err);
+        if (err) {
+            error_propagate(errp, err);
+            return;
+        }

Same. Feel free to rely on the default value (prop should be false) in one case of the other, but being explicit in both machine models is OK as well.

Regards,
Peter

Quoted 66 lines
hw/arm/fsl-imx25.c +7 -0
+        sysbus_mmio_map(SYS_BUS_DEVICE(&s->gpio[i]), 0, gpio_table[i].addr);
+        /* Connect GPIO IRQ to PIC */
+        sysbus_connect_irq(SYS_BUS_DEVICE(&s->gpio[i]), 0,
+                           qdev_get_gpio_in(DEVICE(&s->avic),
+                                            gpio_table[i].irq));
+    }
+
     /* initialize 2 x 16 KB ROM */
     memory_region_init_rom_device(&s->rom[0], NULL, NULL, NULL,
                                   "imx25.rom0", FSL_IMX25_ROM0_SIZE, &err);
include/hw/arm/fsl-imx25.h +15 -1
index 7f6bb64..73f50c6 100644
@@ -25,6 +25,7 @@
 #include "hw/timer/imx_epit.h"
 #include "hw/net/imx_fec.h"
 #include "hw/i2c/imx_i2c.h"
+#include "hw/gpio/imx_gpio.h"
 #include "exec/memory.h"
 
 #define TYPE_FSL_IMX25 "fsl,imx25"
@@ -34,6 +35,7 @@
 #define FSL_IMX25_NUM_GPTS 4
 #define FSL_IMX25_NUM_EPITS 2
 #define FSL_IMX25_NUM_I2CS 3
+#define FSL_IMX25_NUM_GPIOS 4
 
 typedef struct FslIMX25State {
     /*< private >*/
@@ -48,6 +50,7 @@ typedef struct FslIMX25State {
     IMXEPITState   epit[FSL_IMX25_NUM_EPITS];
     IMXFECState    fec;
     IMXI2CState    i2c[FSL_IMX25_NUM_I2CS];
+    IMXGPIOState   gpio[FSL_IMX25_NUM_GPIOS];
     MemoryRegion   rom[2];
     MemoryRegion   iram;
     MemoryRegion   iram_alias;
@@ -204,6 +207,14 @@ typedef struct FslIMX25State {
 #define FSL_IMX25_EPIT1_SIZE    0x4000
 #define FSL_IMX25_EPIT2_ADDR    0x53F98000
 #define FSL_IMX25_EPIT2_SIZE    0x4000
+#define FSL_IMX25_GPIO4_ADDR    0x53F9C000
+#define FSL_IMX25_GPIO4_SIZE    0x4000
+#define FSL_IMX25_GPIO3_ADDR    0x53FA4000
+#define FSL_IMX25_GPIO3_SIZE    0x4000
+#define FSL_IMX25_GPIO1_ADDR    0x53FCC000
+#define FSL_IMX25_GPIO1_SIZE    0x4000
+#define FSL_IMX25_GPIO2_ADDR    0x53FD0000
+#define FSL_IMX25_GPIO2_SIZE    0x4000
 #define FSL_IMX25_AVIC_ADDR     0x68000000
 #define FSL_IMX25_AVIC_SIZE     0x4000
 #define FSL_IMX25_IRAM_ADDR     0x78000000
@@ -230,5 +241,9 @@ typedef struct FslIMX25State {
 #define FSL_IMX25_I2C1_IRQ      3
 #define FSL_IMX25_I2C2_IRQ      4
 #define FSL_IMX25_I2C3_IRQ      10
+#define FSL_IMX25_GPIO1_IRQ     52
+#define FSL_IMX25_GPIO2_IRQ     51
+#define FSL_IMX25_GPIO3_IRQ     16
+#define FSL_IMX25_GPIO4_IRQ     23
 
 #endif /* FSL_IMX25_H */
--

2.1.4