Skip to contentSkip to conversation

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

PATCH Superseded Jean-Christophe Dubois 2 participants 12 messages Sep 5, 2015 – Sep 7, 2015 Arm i.MX and Layerscape

v1 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

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 device to i.MX31 SOC
i.MX: Add GPIO device to i.MX25 SOC

 hw/arm/fsl-imx25.c         |  31 ++++
 hw/arm/fsl-imx31.c         |  30 ++++
 hw/gpio/Makefile.objs      |   1 +
 hw/gpio/imx_gpio.c         | 358 +++++++++++++++++++++++++++++++++++++++++++++
 include/hw/arm/fsl-imx25.h |  15 ++
 include/hw/arm/fsl-imx31.h |  12 ++
 include/hw/gpio/imx_gpio.h |  60 ++++++++
 7 files changed, 507 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 1/3 i.MX: Add GPIO device

---
 hw/gpio/Makefile.objs      |   1 +
 hw/gpio/imx_gpio.c         | 358 +++++++++++++++++++++++++++++++++++++++++++++
 include/hw/gpio/imx_gpio.h |  60 ++++++++
 3 files changed, 419 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 +358 -0

358 lines not on this page · raw message

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

60 lines not on this page · raw message

Signature · 1 line
2.1.4
#2 lore raw

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

Quoted 43 lines · Jean-Christophe Dubois
---
 hw/gpio/Makefile.objs      |   1 +
 hw/gpio/imx_gpio.c         | 358 +++++++++++++++++++++++++++++++++++++++++++++
 include/hw/gpio/imx_gpio.h |  60 ++++++++
 3 files changed, 419 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 +19 -0
new file mode 100644
index 0000000..8ec1d4c
@@ -0,0 +1,358 @@
+/*
+ * 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,
+ * 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/>.
+ */
+

hw/gpio/imx_gpio.c line 29

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

Use a regular if for debug conditional. This is so the debug code is always compiled.

Quoted 13 lines
hw/gpio/imx_gpio.c new file +13 -0
+static char const *imx_gpio_reg_name(uint32_t reg)
+{
+    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";

hw/gpio/imx_gpio.c line 52

Quoted 10 lines
hw/gpio/imx_gpio.c new file +10 -0
+    case IMR_ADDR:
+        return "IMR";
+    case ISR_ADDR:
+        return "ISR";
+    case EDGE_SEL_ADDR:
+        /* This register is not present in i.MX31 */
+        return "EDGE_SEL";
+    default:
+        return "[?]";
+    }

But I'm guessing this will be an issue for the non debug case. Perhaps return "" from here in non-debug or just leave this always compiled (optimisiser should be smart enough to trim it).

hw/gpio/imx_gpio.c line 64

Quoted 12 lines
hw/gpio/imx_gpio.c new file +12 -0
+}
+#else
+#  define DPRINTF(fmt, args...) do {} while (0)
+#endif
+
+static void imx_gpio_update_int(IMXGPIOState *s)
+{
+    if (s->isr) {
+        qemu_irq_raise(s->irq);
+    } else {
+        qemu_irq_lower(s->irq);
+    }

qemu_set_irq.

hw/gpio/imx_gpio.c line 70

Quoted 6 lines
hw/gpio/imx_gpio.c new file +6 -0
+}
+
+static void imx_gpio_set_int_line(IMXGPIOState *s, int line, int level)
+{
+    /* is this signal configured as an interrupt source */
+    if (extract32(s->imr, line, 1)) {

Short return on this condition (negated) rather than indent entire logic.

hw/gpio/imx_gpio.c line 74

Quoted 4 lines
hw/gpio/imx_gpio.c new file +4 -0
+        /* When set EDGE_SEL override the ICR config */
+        if (extract32(s->edge_sel, line, 1)) {
+            /* we detect interrupt on rising and falling edge */
+            if (extract32(s->psr, line, 1) != level) {

This is dangerous, as level is boolean and this will promote to integer for comparison. I think last time people talked about this on list, we decided that all users or GPIO setters should use 0 vs 1 but it may not be the in-tree case. !!level will resolve.

hw/gpio/imx_gpio.c line 84

Quoted 10 lines
hw/gpio/imx_gpio.c new file +10 -0
+                /* 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 */
+                int falling = extract64(s->icr, 2*line, 1);
+
+                if ((falling && !level) || (!falling && level)) {

falling == !level

hw/gpio/imx_gpio.c line 92

Quoted 8 lines
hw/gpio/imx_gpio.c new file +8 -0
+                    s->isr = deposit32(s->isr, line, 1, 1);
+                }
+            }
+        } else {
+            /* interrupt is level sensitive */
+            int high = extract64(s->icr, 2*line, 1);
+
+            if ((high && level) || (!high && !level)) {

==

Quoted 6 lines
hw/gpio/imx_gpio.c new file +6 -0
+                s->isr = deposit32(s->isr, line, 1, 1);
+            }
+        }
+    }
+}
+

hw/gpio/imx_gpio.c line 108

Quoted 10 lines
hw/gpio/imx_gpio.c new file +10 -0
+static void imx_gpio_set(void *opaque, int line, int level)
+{
+    IMXGPIOState *s = IMX_GPIO(opaque);
+
+    /* 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);

Short return.

Quoted 5 lines
hw/gpio/imx_gpio.c new file +5 -0
+    } else {
+        imx_gpio_set_int_line(s, line, level);
+
+        /* this is an input signal, so set PSR */
+        s->psr = deposit32(s->psr, line, 1, level);

hw/gpio/imx_gpio.c line 123

Quoted 10 lines
hw/gpio/imx_gpio.c new file +10 -0
+
+        imx_gpio_update_int(s);
+    }
+}
+
+static void imx_gpio_set_all_int_lines(IMXGPIOState *s)
+{
+    int i;
+
+    for (i = 0; i < 32; i++) {

32 should be macroified.

Quoted 10 lines
hw/gpio/imx_gpio.c new file +10 -0
+        imx_gpio_set_int_line(s, i, extract32(s->psr, i, 1));
+    }
+
+    imx_gpio_update_int(s);
+}
+
+static uint64_t imx_gpio_read(void *opaque, hwaddr offset, unsigned size)
+{
+    IMXGPIOState *s = IMX_GPIO(opaque);
+    uint32_t reg_value = 0;

hw/gpio/imx_gpio.c line 143

Quoted 10 lines
hw/gpio/imx_gpio.c new file +10 -0
+    int i;
+
+    switch (offset) {
+    case DR_ADDR: /* DATA REGISTER */
+        for (i = 0; i < 32; i++) {
+            uint32_t ref_value;
+
+            /*
+             * depending on the "line" configuration, the bit values
+             * are comming either from DR ou PSR

"coming" "or"

hw/gpio/imx_gpio.c line 152

Quoted 9 lines
hw/gpio/imx_gpio.c new file +9 -0
+             */
+            if (extract32(s->gdir, i, 1)) {
+                ref_value = s->dr;
+            } else {
+                ref_value = s->psr;
+            }
+
+            reg_value = deposit32(reg_value, i, 1,
+                                  extract32(ref_value, i, 1));

This can be one-shot with some bitwise logicals to avoid the loop.

reg_value = s->dr & s->gdir | s->psr & ~s->gdir

I think.

hw/gpio/imx_gpio.c line 156

Quoted 4 lines
hw/gpio/imx_gpio.c new file +4 -0
+        }
+        break;
+
+    case GDIR_ADDR: /* DIRECTION REGISTER */

These trailing comments are duped between read and write handlers. I would move to header as a more global single reference.

Quoted 25 lines
hw/gpio/imx_gpio.c new file +25 -0
+        reg_value = s->gdir;
+        break;
+
+    case PSR_ADDR: /* PAD STATUS REGISTER */
+        reg_value = s->psr;
+        break;
+
+    case ICR1_ADDR: /* INTERRUPT CONFIGURATION REGISTER 1 */
+        reg_value = (uint32_t) (s->icr & 0xffffffff);
+        break;
+
+    case ICR2_ADDR: /* INTERRUPT CONFIGURATION REGISTER 2 */
+        reg_value = (uint32_t) (s->icr >> 32);
+        break;
+
+    case IMR_ADDR: /* INTERRUPT MASK REGISTER */
+        reg_value = s->imr;
+        break;
+
+    case ISR_ADDR: /* INTERRUPT STATUS REGISTER */
+        reg_value = s->isr;
+        break;
+
+    case EDGE_SEL_ADDR: /* EDGE SEL REGISTER */
+        /* This register is not present in i.MX31 */

hw/gpio/imx_gpio.c line 191

Quoted 10 lines
hw/gpio/imx_gpio.c new file +10 -0
+        reg_value = s->edge_sel;
+        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%08x\n", imx_gpio_reg_name(offset), reg_value);

PRIx32.

hw/gpio/imx_gpio.c line 196

Quoted 5 lines
hw/gpio/imx_gpio.c new file +5 -0
+
+    return reg_value;
+}
+
+static inline void imx_gpio_set_output(IMXGPIOState *s)

set_all_outputs to be consistent with set_all_int_lines. Group the two functions and any similars together (either here or above with int_lines).

Quoted 11 lines
hw/gpio/imx_gpio.c new file +11 -0
+{
+    int i;
+
+    for (i = 0; i < 32; i++) {
+        /*
+         * if the line is set as output, then forward the line
+         * level to its user.
+         */
+        if (extract32(s->gdir, i, 1) && s->handler[i]) {
+            qemu_set_irq(s->handler[i], extract32(s->dr, i, 1));
+        }

hw/gpio/imx_gpio.c line 217

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

PRIx32.

hw/gpio/imx_gpio.c line 221

Quoted 4 lines
hw/gpio/imx_gpio.c new file +4 -0
+
+    switch (offset) {
+    case DR_ADDR: /* DATA REGISTER */
+        s->dr = (uint32_t)value;

cast un-needed.

hw/gpio/imx_gpio.c line 228

Quoted 7 lines
hw/gpio/imx_gpio.c new file +7 -0
+
+        imx_gpio_set_output(s);
+
+        break;
+
+    case GDIR_ADDR: /* DIRECTION REGISTER */
+        s->gdir = (uint32_t)value;

cast un-needed (theres a few more below).

hw/gpio/imx_gpio.c line 236

Quoted 8 lines
hw/gpio/imx_gpio.c new file +8 -0
+
+        imx_gpio_set_output(s);
+        imx_gpio_set_all_int_lines(s);
+
+        break;
+
+    case ICR1_ADDR: /* INTERRUPT CONFIGURATION REGISTER 1 */
+        s->icr = (s->icr | 0xffffffff) & (uint32_t)value;

deposit64.

hw/gpio/imx_gpio.c line 243

Quoted 7 lines
hw/gpio/imx_gpio.c new file +7 -0
+
+        imx_gpio_set_all_int_lines(s);
+
+        break;
+
+    case ICR2_ADDR: /* INTERRUPT CONFIGURATION REGISTER 2 */
+        s->icr = (s->icr | 0xffffffff00000000) & (value << 32);

deposit64.

Quoted 17 lines
hw/gpio/imx_gpio.c new file +17 -0
+
+        imx_gpio_set_all_int_lines(s);
+
+        break;
+
+    case IMR_ADDR: /* INTERRUPT MASK REGISTER */
+        s->imr = (uint32_t)value;
+
+        imx_gpio_set_all_int_lines(s);
+
+        break;
+
+    case ISR_ADDR: /* INTERRUPT STATUS REGISTER */
+        s->isr |= ~(uint32_t)value;
+
+        imx_gpio_set_all_int_lines(s);
+

hw/gpio/imx_gpio.c line 270

Quoted 10 lines
hw/gpio/imx_gpio.c new file +10 -0
+        break;
+
+    case EDGE_SEL_ADDR: /* EDGE SEL REGISTER */
+        /* This register is not present in i.MX31 */
+        s->edge_sel = (uint32_t)value;
+
+        imx_gpio_set_all_int_lines(s);
+
+        break;
+

I don't think you need the extra white. For short side effects it is usual to just format as:

case ADDR:

    register = value;
    side_effects();
    break;
Quoted 37 lines
hw/gpio/imx_gpio.c new file +37 -0
+    default:
+        qemu_log_mask(LOG_GUEST_ERROR, "%s[%s]: Bad register at offset %d\n",
+                      TYPE_IMX_GPIO, __func__, (int)offset);
+        break;
+    }
+
+    return;
+}
+
+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_output(s);
+    imx_gpio_update_int(s);
+}
+
+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,

hw/gpio/imx_gpio.c line 317

Quoted 10 lines
hw/gpio/imx_gpio.c new file +10 -0
+    .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 */
+        VMSTATE_UINT32(edge_sel, IMXGPIOState),

You can make a boolean property of the device and turn its presence on and off to get the functionality.

Quoted 16 lines
hw/gpio/imx_gpio.c new file +16 -0
+        VMSTATE_END_OF_LIST()
+    }
+};
+
+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, 32);
+    qdev_init_gpio_out(DEVICE(s), s->handler, 32);
+
+    sysbus_init_irq(SYS_BUS_DEVICE(dev), &s->irq);
+    sysbus_init_mmio(SYS_BUS_DEVICE(dev), &s->iomem);

hw/gpio/imx_gpio.c line 343

Quoted 10 lines
hw/gpio/imx_gpio.c new file +10 -0
+}
+
+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->vmsd = &vmstate_imx_gpio;
+    dc->desc = "i.MX GPIO controller";

dc->props = ...

for the "has-edge-sel" property.

Quoted 68 lines
hw/gpio/imx_gpio.c new file +15 -0
+}
+
+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 +47 -0
new file mode 100644
index 0000000..6d3f149
@@ -0,0 +1,60 @@
+/*
+ * 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
+#define GDIR_ADDR           0x04
+#define PSR_ADDR            0x08
+#define ICR1_ADDR           0x0c
+#define ICR2_ADDR           0x10
+#define IMR_ADDR            0x14
+#define ISR_ADDR            0x18
+#define EDGE_SEL_ADDR       0x1c
+
+typedef struct IMXGPIOState {
+    /*< private >*/
+    SysBusDevice parent_obj;
+
+    /*< public >*/
+    MemoryRegion iomem;
+
+    uint32_t dr;

include/hw/gpio/imx_gpio.h line 57

Quoted 10 lines
include/hw/gpio/imx_gpio.h new file +10 -0
+    uint32_t gdir;
+    uint32_t psr;
+    uint64_t icr;
+    uint32_t imr;
+    uint32_t isr;
+    /* This register is not present in i.MX31 */
+    uint32_t edge_sel;
+
+    qemu_irq irq;
+    qemu_irq handler[32];

Should this just be "outputs"? Im guessing this is a bidirectional pin which QEMU has trouble modelleing and this is the output mode variant of that (hence it probably has no name in the TRM to use).

Regards,
Peter

Quoted 7 lines
include/hw/gpio/imx_gpio.h new file +3 -1
+} IMXGPIOState;
+
+#endif /* __IMX_GPIO_H_ */
--

2.1.4

#3 lore raw

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

Quoted 49 lines · Peter Crosthwaite
Quoted 47 lines · Jean-Christophe Dubois
---
  hw/gpio/Makefile.objs      |   1 +
  hw/gpio/imx_gpio.c         | 358 +++++++++++++++++++++++++++++++++++++++++++++
  include/hw/gpio/imx_gpio.h |  60 ++++++++
  3 files changed, 419 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 +23 -0
new file mode 100644
index 0000000..8ec1d4c
@@ -0,0 +1,358 @@
+/*
+ * 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,
+ * 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"
+
+#ifndef IMX_GPIO_DEBUG
+#define IMX_GPIO_DEBUG                 0
Quoted 8 lines

hw/gpio/imx_gpio.c line 29

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

Use a regular if for debug conditional. This is so the debug code is always compiled.

Not sure what you mean here ... Most files in qemu have DEBUG functions compiled out in the normal case ...

Is there a change in "politic" on this topic?

Could you point to an implementation doing things right?

Quoted 28 lines
Quoted 14 lines
hw/gpio/imx_gpio.c new file +13 -0
+static char const *imx_gpio_reg_name(uint32_t reg)
+{
+    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";

hw/gpio/imx_gpio.c line 52

Quoted 10 lines
hw/gpio/imx_gpio.c new file +10 -0
+    case IMR_ADDR:
+        return "IMR";
+    case ISR_ADDR:
+        return "ISR";
+    case EDGE_SEL_ADDR:
+        /* This register is not present in i.MX31 */
+        return "EDGE_SEL";
+    default:
+        return "[?]";
+    }

But I'm guessing this will be an issue for the non debug case. Perhaps return "" from here in non-debug or just leave this always compiled (optimisiser should be smart enough to trim it).

Quoted 13 lines

hw/gpio/imx_gpio.c line 64

Quoted 12 lines
hw/gpio/imx_gpio.c new file +12 -0
+}
+#else
+#  define DPRINTF(fmt, args...) do {} while (0)
+#endif
+
+static void imx_gpio_update_int(IMXGPIOState *s)
+{
+    if (s->isr) {
+        qemu_irq_raise(s->irq);
+    } else {
+        qemu_irq_lower(s->irq);
+    }

qemu_set_irq.

OK

Quoted 8 lines

hw/gpio/imx_gpio.c line 70

Quoted 7 lines
hw/gpio/imx_gpio.c new file +6 -0
+}
+
+static void imx_gpio_set_int_line(IMXGPIOState *s, int line, int level)
+{
+    /* is this signal configured as an interrupt source */
+    if (extract32(s->imr, line, 1)) {

Short return on this condition (negated) rather than indent entire logic.

OK

Quoted 9 lines

hw/gpio/imx_gpio.c line 74

Quoted 5 lines
hw/gpio/imx_gpio.c new file +4 -0
+        /* When set EDGE_SEL override the ICR config */
+        if (extract32(s->edge_sel, line, 1)) {
+            /* we detect interrupt on rising and falling edge */
+            if (extract32(s->psr, line, 1) != level) {

This is dangerous, as level is boolean and this will promote to integer for comparison. I think last time people talked about this on list, we decided that all users or GPIO setters should use 0 vs 1 but it may not be the in-tree case. !!level will resolve.

OK, I'll work out something with an enum to be explicit.

Quoted 11 lines

hw/gpio/imx_gpio.c line 84

Quoted 11 lines
hw/gpio/imx_gpio.c new file +10 -0
+                /* 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 */
+                int falling = extract64(s->icr, 2*line, 1);
+
+                if ((falling && !level) || (!falling && level)) {
Quoted 1 line

falling == !level

OK

Quoted 10 lines

hw/gpio/imx_gpio.c line 92

Quoted 9 lines
hw/gpio/imx_gpio.c new file +8 -0
+                    s->isr = deposit32(s->isr, line, 1, 1);
+                }
+            }
+        } else {
+            /* interrupt is level sensitive */
+            int high = extract64(s->icr, 2*line, 1);
+
+            if ((high && level) || (!high && !level)) {

==

OK

Quoted 10 lines
Quoted 10 lines
hw/gpio/imx_gpio.c new file +9 -0
+                s->isr = deposit32(s->isr, line, 1, 1);
+            }
+        }
+    }
+}
+
+static void imx_gpio_set(void *opaque, int line, int level)
+{
+    IMXGPIOState *s = IMX_GPIO(opaque);
Quoted 8 lines

hw/gpio/imx_gpio.c line 108

Quoted 7 lines
hw/gpio/imx_gpio.c new file +7 -0
+
+    /* 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);

Short return.

OK

Quoted 17 lines
Quoted 6 lines
hw/gpio/imx_gpio.c new file +5 -0
+    } else {
+        imx_gpio_set_int_line(s, line, level);
+
+        /* this is an input signal, so set PSR */
+        s->psr = deposit32(s->psr, line, 1, level);

hw/gpio/imx_gpio.c line 123

Quoted 10 lines
hw/gpio/imx_gpio.c new file +10 -0
+
+        imx_gpio_update_int(s);
+    }
+}
+
+static void imx_gpio_set_all_int_lines(IMXGPIOState *s)
+{
+    int i;
+
+    for (i = 0; i < 32; i++) {

32 should be macroified.

OK

Quoted 14 lines
Quoted 14 lines
hw/gpio/imx_gpio.c new file +13 -0
+        imx_gpio_set_int_line(s, i, extract32(s->psr, i, 1));
+    }
+
+    imx_gpio_update_int(s);
+}
+
+static uint64_t imx_gpio_read(void *opaque, hwaddr offset, unsigned size)
+{
+    IMXGPIOState *s = IMX_GPIO(opaque);
+    uint32_t reg_value = 0;
+    int i;
+
+    switch (offset) {
Quoted 8 lines

hw/gpio/imx_gpio.c line 143

Quoted 7 lines
hw/gpio/imx_gpio.c new file +7 -0
+    case DR_ADDR: /* DATA REGISTER */
+        for (i = 0; i < 32; i++) {
+            uint32_t ref_value;
+
+            /*
+             * depending on the "line" configuration, the bit values
+             * are comming either from DR ou PSR

"coming" "or"

OK

Quoted 10 lines

hw/gpio/imx_gpio.c line 152

Quoted 10 lines
hw/gpio/imx_gpio.c new file +9 -0
+             */
+            if (extract32(s->gdir, i, 1)) {
+                ref_value = s->dr;
+            } else {
+                ref_value = s->psr;
+            }
+
+            reg_value = deposit32(reg_value, i, 1,
+                                  extract32(ref_value, i, 1));
Quoted 5 lines

This can be one-shot with some bitwise logicals to avoid the loop.

reg_value = s->dr & s->gdir | s->psr & ~s->gdir

I think.

OK

Quoted 7 lines

hw/gpio/imx_gpio.c line 156

Quoted 5 lines
hw/gpio/imx_gpio.c new file +4 -0
+        }
+        break;
+
+    case GDIR_ADDR: /* DIRECTION REGISTER */

These trailing comments are duped between read and write handlers. I would move to header as a more global single reference.

I was thinking it was more helpful here to help understand the code without having to refer to a header file. The point is that the abreviated spec name is not always very helpful.
But, OK, I'll move them to the header file ...

Quoted 29 lines
Quoted 29 lines
hw/gpio/imx_gpio.c new file +28 -0
+        reg_value = s->gdir;
+        break;
+
+    case PSR_ADDR: /* PAD STATUS REGISTER */
+        reg_value = s->psr;
+        break;
+
+    case ICR1_ADDR: /* INTERRUPT CONFIGURATION REGISTER 1 */
+        reg_value = (uint32_t) (s->icr & 0xffffffff);
+        break;
+
+    case ICR2_ADDR: /* INTERRUPT CONFIGURATION REGISTER 2 */
+        reg_value = (uint32_t) (s->icr >> 32);
+        break;
+
+    case IMR_ADDR: /* INTERRUPT MASK REGISTER */
+        reg_value = s->imr;
+        break;
+
+    case ISR_ADDR: /* INTERRUPT STATUS REGISTER */
+        reg_value = s->isr;
+        break;
+
+    case EDGE_SEL_ADDR: /* EDGE SEL REGISTER */
+        /* This register is not present in i.MX31 */
+        reg_value = s->edge_sel;
+        break;
+
Quoted 8 lines

hw/gpio/imx_gpio.c line 191

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;
+    }
+
+    DPRINTF("(%s) = 0x%08x\n", imx_gpio_reg_name(offset), reg_value);

PRIx32.

OK

Quoted 9 lines

hw/gpio/imx_gpio.c line 196

Quoted 6 lines
hw/gpio/imx_gpio.c new file +5 -0
+
+    return reg_value;
+}
+
+static inline void imx_gpio_set_output(IMXGPIOState *s)

set_all_outputs to be consistent with set_all_int_lines. Group the two functions and any similars together (either here or above with int_lines).

OK

Quoted 15 lines
Quoted 15 lines
hw/gpio/imx_gpio.c new file +14 -0
+{
+    int i;
+
+    for (i = 0; i < 32; i++) {
+        /*
+         * if the line is set as output, then forward the line
+         * level to its user.
+         */
+        if (extract32(s->gdir, i, 1) && s->handler[i]) {
+            qemu_set_irq(s->handler[i], extract32(s->dr, i, 1));
+        }
+    }
+}
+
Quoted 8 lines

hw/gpio/imx_gpio.c line 217

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

PRIx32.

OK

Quoted 6 lines

hw/gpio/imx_gpio.c line 221

Quoted 5 lines
hw/gpio/imx_gpio.c new file +4 -0
+
+    switch (offset) {
+    case DR_ADDR: /* DATA REGISTER */
+        s->dr = (uint32_t)value;

cast un-needed.

OK

Quoted 9 lines

hw/gpio/imx_gpio.c line 228

Quoted 8 lines
hw/gpio/imx_gpio.c new file +7 -0
+
+        imx_gpio_set_output(s);
+
+        break;
+
+    case GDIR_ADDR: /* DIRECTION REGISTER */
+        s->gdir = (uint32_t)value;

cast un-needed (theres a few more below).

OK

Quoted 10 lines

hw/gpio/imx_gpio.c line 236

Quoted 9 lines
hw/gpio/imx_gpio.c new file +8 -0
+
+        imx_gpio_set_output(s);
+        imx_gpio_set_all_int_lines(s);
+
+        break;
+
+    case ICR1_ADDR: /* INTERRUPT CONFIGURATION REGISTER 1 */
+        s->icr = (s->icr | 0xffffffff) & (uint32_t)value;

deposit64.

OK

Quoted 9 lines

hw/gpio/imx_gpio.c line 243

Quoted 8 lines
hw/gpio/imx_gpio.c new file +7 -0
+
+        imx_gpio_set_all_int_lines(s);
+
+        break;
+
+    case ICR2_ADDR: /* INTERRUPT CONFIGURATION REGISTER 2 */
+        s->icr = (s->icr | 0xffffffff00000000) & (value << 32);

deposit64.

OK

Quoted 31 lines
Quoted 18 lines
hw/gpio/imx_gpio.c new file +17 -0
+
+        imx_gpio_set_all_int_lines(s);
+
+        break;
+
+    case IMR_ADDR: /* INTERRUPT MASK REGISTER */
+        s->imr = (uint32_t)value;
+
+        imx_gpio_set_all_int_lines(s);
+
+        break;
+
+    case ISR_ADDR: /* INTERRUPT STATUS REGISTER */
+        s->isr |= ~(uint32_t)value;
+
+        imx_gpio_set_all_int_lines(s);
+

hw/gpio/imx_gpio.c line 270

Quoted 10 lines
hw/gpio/imx_gpio.c new file +10 -0
+        break;
+
+    case EDGE_SEL_ADDR: /* EDGE SEL REGISTER */
+        /* This register is not present in i.MX31 */
+        s->edge_sel = (uint32_t)value;
+
+        imx_gpio_set_all_int_lines(s);
+
+        break;
+

I don't think you need the extra white. For short side effects it is usual to just format as:

Quoted 4 lines

case ADDR:

     register = value;
     side_effects();
     break;

OK

Quoted 42 lines
Quoted 42 lines
hw/gpio/imx_gpio.c new file +41 -0
+    default:
+        qemu_log_mask(LOG_GUEST_ERROR, "%s[%s]: Bad register at offset %d\n",
+                      TYPE_IMX_GPIO, __func__, (int)offset);
+        break;
+    }
+
+    return;
+}
+
+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_output(s);
+    imx_gpio_update_int(s);
+}
+
+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[]) {
+        VMSTATE_UINT32(dr, IMXGPIOState),
+        VMSTATE_UINT32(gdir, IMXGPIOState),
Quoted 8 lines

hw/gpio/imx_gpio.c line 317

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

You can make a boolean property of the device and turn its presence on and off to get the functionality.

OK

Quoted 29 lines
Quoted 17 lines
hw/gpio/imx_gpio.c new file +16 -0
+        VMSTATE_END_OF_LIST()
+    }
+};
+
+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, 32);
+    qdev_init_gpio_out(DEVICE(s), s->handler, 32);
+
+    sysbus_init_irq(SYS_BUS_DEVICE(dev), &s->irq);
+    sysbus_init_mmio(SYS_BUS_DEVICE(dev), &s->iomem);

hw/gpio/imx_gpio.c line 343

Quoted 10 lines
hw/gpio/imx_gpio.c new file +10 -0
+}
+
+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->vmsd = &vmstate_imx_gpio;
+    dc->desc = "i.MX GPIO controller";

dc->props = ...

Quoted 1 line

for the "has-edge-sel" property.

OK

Quoted 74 lines
Quoted 74 lines
hw/gpio/imx_gpio.c new file +15 -0
+}
+
+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 +52 -0
new file mode 100644
index 0000000..6d3f149
@@ -0,0 +1,60 @@
+/*
+ * 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
+#define GDIR_ADDR           0x04
+#define PSR_ADDR            0x08
+#define ICR1_ADDR           0x0c
+#define ICR2_ADDR           0x10
+#define IMR_ADDR            0x14
+#define ISR_ADDR            0x18
+#define EDGE_SEL_ADDR       0x1c
+
+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;
Quoted 8 lines

include/hw/gpio/imx_gpio.h line 57

Quoted 5 lines
include/hw/gpio/imx_gpio.h new file +5 -0
+    /* This register is not present in i.MX31 */
+    uint32_t edge_sel;
+
+    qemu_irq irq;
+    qemu_irq handler[32];

Should this just be "outputs"? Im guessing this is a bidirectional pin which QEMU has trouble modelleing and this is the output mode variant of that (hence it probably has no name in the TRM to use).

OK

Quoted 11 lines

Regards,
Peter

Quoted 7 lines
include/hw/gpio/imx_gpio.h new file +3 -1
+} IMXGPIOState;
+
+#endif /* __IMX_GPIO_H_ */
--

2.1.4

#4 lore raw

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

Quoted 65 lines · Jean-Christophe DUBOIS
Quoted 55 lines · Peter Crosthwaite
Quoted 52 lines · Jean-Christophe Dubois
---
  hw/gpio/Makefile.objs      |   1 +
  hw/gpio/imx_gpio.c         | 358
+++++++++++++++++++++++++++++++++++++++++++++
  include/hw/gpio/imx_gpio.h |  60 ++++++++
  3 files changed, 419 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 +16 -0
new file mode 100644
index 0000000..8ec1d4c
@@ -0,0 +1,358 @@
+/*
+ * 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,
+ * 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/>.
+ */

hw/gpio/imx_gpio.c new file +7 -0
+
+#include "hw/gpio/imx_gpio.h"
+
+#ifndef IMX_GPIO_DEBUG
+#define IMX_GPIO_DEBUG                 0
+#endif
+
Quoted 8 lines
Quoted 5 lines
hw/gpio/imx_gpio.c new file +3 -0
+#if IMX_GPIO_DEBUG
+#  define DPRINTF(fmt, args...) \
+          do { fprintf(stder, "%s: " fmt , __func__, ##args); } while

(0)
+

Use a regular if for debug conditional. This is so the debug code is always compiled.

Quoted 3 lines

Not sure what you mean here ... Most files in qemu have DEBUG functions compiled out in the normal case ...

We are trying to slowly change that.

Quoted 2 lines

Is there a change in "politic" on this topic?

No this has been the policy for a while now.

Quoted 2 lines

Could you point to an implementation doing things right?

hw/dma/pl330.c

Regards,
Peter

Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
hw/gpio/imx_gpio.c new file +6 -0
+static char const *imx_gpio_reg_name(uint32_t reg)
+{
+    switch (reg) {
+    case DR_ADDR:
+        return "DR";
+    case GDIR_ADDR:
#5 lore raw

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

Quoted 68 lines · Peter Crosthwaite
Quoted 61 lines · Jean-Christophe DUBOIS
Quoted 52 lines · Peter Crosthwaite
Quoted 50 lines · Jean-Christophe Dubois
---
   hw/gpio/Makefile.objs      |   1 +
   hw/gpio/imx_gpio.c         | 358
+++++++++++++++++++++++++++++++++++++++++++++
   include/hw/gpio/imx_gpio.h |  60 ++++++++
   3 files changed, 419 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 +16 -0
new file mode 100644
index 0000000..8ec1d4c
@@ -0,0 +1,358 @@
+/*
+ * 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,
+ * 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/>.
+ */

hw/gpio/imx_gpio.c new file +6 -0
+
+#include "hw/gpio/imx_gpio.h"
+
+#ifndef IMX_GPIO_DEBUG
+#define IMX_GPIO_DEBUG                 0
+#endif
Quoted 8 lines
Quoted 6 lines
hw/gpio/imx_gpio.c new file +4 -0
+
+#if IMX_GPIO_DEBUG
+#  define DPRINTF(fmt, args...) \
+          do { fprintf(stder, "%s: " fmt , __func__, ##args); } while

(0)
+

Use a regular if for debug conditional. This is so the debug code is always compiled.

Quoted 3 lines

Not sure what you mean here ... Most files in qemu have DEBUG functions compiled out in the normal case ...

We are trying to slowly change that.

Quoted 7 lines
Quoted 2 lines

Is there a change in "politic" on this topic?

No this has been the policy for a while now.

Quoted 2 lines

Could you point to an implementation doing things right?

hw/dma/pl330.c

Not sure, I understand. It is still compiled out as PL330_ERR_DEBUG is 0 in the "normal" case.

JC

Quoted 10 lines

Regards,
Peter

Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
hw/gpio/imx_gpio.c new file +6 -0
+static char const *imx_gpio_reg_name(uint32_t reg)
+{
+    switch (reg) {
+    case DR_ADDR:
+        return "DR";
+    case GDIR_ADDR:
#6 lore raw

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

Quoted 79 lines · Jean-Christophe DUBOIS
Quoted 78 lines · Peter Crosthwaite
Quoted 66 lines · Jean-Christophe DUBOIS
Quoted 55 lines · Peter Crosthwaite
Quoted 52 lines · Jean-Christophe Dubois
---
   hw/gpio/Makefile.objs      |   1 +
   hw/gpio/imx_gpio.c         | 358
+++++++++++++++++++++++++++++++++++++++++++++
   include/hw/gpio/imx_gpio.h |  60 ++++++++
   3 files changed, 419 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 +16 -0
new file mode 100644
index 0000000..8ec1d4c
@@ -0,0 +1,358 @@
+/*
+ * 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,
+ * 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/>.
+ */

hw/gpio/imx_gpio.c new file +7 -0
+
+#include "hw/gpio/imx_gpio.h"
+
+#ifndef IMX_GPIO_DEBUG
+#define IMX_GPIO_DEBUG                 0
+#endif
+
Quoted 8 lines
Quoted 5 lines
hw/gpio/imx_gpio.c new file +3 -0
+#if IMX_GPIO_DEBUG
+#  define DPRINTF(fmt, args...) \
+          do { fprintf(stder, "%s: " fmt , __func__, ##args); } while

(0)
+

Use a regular if for debug conditional. This is so the debug code is always compiled.

Quoted 3 lines

Not sure what you mean here ... Most files in qemu have DEBUG functions compiled out in the normal case ...

We are trying to slowly change that.

Quoted 2 lines

Is there a change in "politic" on this topic?

No this has been the policy for a while now.

Quoted 8 lines
Quoted 3 lines
Quoted 2 lines

Could you point to an implementation doing things right?

hw/dma/pl330.c

Not sure, I understand. It is still compiled out as PL330_ERR_DEBUG is 0 in the "normal" case.

It is not compiled out textually, it is "if (0) {}"''d.

#ifndef PL330_ERR_DEBUG
#define PL330_ERR_DEBUG 0
#endif

#define DB_PRINT_L(lvl, fmt, args...) do {\
    if (PL330_ERR_DEBUG >= lvl) {\
        fprintf(stderr, "PL330: %s:" fmt, __func__, ## args);\
    } \
} while (0);

Note that #define DB_PRINT_L has no conditioning #ifdef which is what we are going for. The idea is, even if the code is never run, it must always still be compiled. This catches issues at compile time, when major changes happen to core headers etc.

Regards,
Peter

Quoted 15 lines

JC

Quoted 10 lines

Regards,
Peter

Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
hw/gpio/imx_gpio.c new file +6 -0
+static char const *imx_gpio_reg_name(uint32_t reg)
+{
+    switch (reg) {
+    case DR_ADDR:
+        return "DR";
+    case GDIR_ADDR:
#7 lore raw

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

In reply to Jean-Christophe Dubois
---
 hw/arm/fsl-imx31.c         | 30 ++++++++++++++++++++++++++++++
 include/hw/arm/fsl-imx31.h | 12 ++++++++++++
 2 files changed, 42 insertions(+)
hw/arm/fsl-imx31.c +30 -0
index 87548c8..aa938ed 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,31 @@ static void fsl_imx31_realize(DeviceState *dev, Error **errp)
                                             i2c_table[i].irq));
     }
 
+    /* Initialize all GPIO */
+    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 }
+        };
+
+        /* Initialize the GPIO */
+        object_property_set_bool(OBJECT(&s->gpio[i]), true, "realized", &err);
+        if (err) {
+            error_propagate(errp, err);
+            return;
+        }
+        /* Map GPIO memory */
+        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
Reviewed-by on v1 2/3
#8 lore raw

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

"devices" in subject.

Quoted 23 lines · Jean-Christophe Dubois
---
 hw/arm/fsl-imx31.c         | 30 ++++++++++++++++++++++++++++++
 include/hw/arm/fsl-imx31.h | 12 ++++++++++++
 2 files changed, 42 insertions(+)
hw/arm/fsl-imx31.c +5 -0
index 87548c8..aa938ed 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)

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

Quoted 4 lines
hw/arm/fsl-imx31.c +1 -0
@@ -184,6 +189,31 @@ static void fsl_imx31_realize(DeviceState *dev, Error **errp)
                                             i2c_table[i].irq));
     }
 
+    /* Initialize all GPIO */

"GPIOs"

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

Quoted 11 lines
hw/arm/fsl-imx31.c +11 -0
+    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 }
+        };
+
+        /* Initialize the GPIO */

Self docced, comment not needed.

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

Quoted 6 lines
hw/arm/fsl-imx31.c +6 -0
+        object_property_set_bool(OBJECT(&s->gpio[i]), true, "realized", &err);
+        if (err) {
+            error_propagate(errp, err);
+            return;
+        }
+        /* Map GPIO memory */

Same.

Otherwise,

Regards,
Peter

Quoted 64 lines
hw/arm/fsl-imx31.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));
+    }
+
     /* 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

#9 lore raw

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

Quoted 25 lines · Peter Crosthwaite

"devices" in subject.

Quoted 21 lines · Jean-Christophe Dubois
---
  hw/arm/fsl-imx31.c         | 30 ++++++++++++++++++++++++++++++
  include/hw/arm/fsl-imx31.h | 12 ++++++++++++
  2 files changed, 42 insertions(+)
hw/arm/fsl-imx31.c +5 -0
index 87548c8..aa938ed 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());
+    }
  }
 
Quoted 7 lines

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

Quoted 6 lines
hw/arm/fsl-imx31.c +1 -0
  static void fsl_imx31_realize(DeviceState *dev, Error **errp)
@@ -184,6 +189,31 @@ static void fsl_imx31_realize(DeviceState *dev, Error **errp)
                                              i2c_table[i].irq));
      }
 
+    /* Initialize all GPIO */

"GPIOs"

OK

Quoted 12 lines

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

Quoted 12 lines
hw/arm/fsl-imx31.c +11 -0
+    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 }
+        };
+
+        /* Initialize the GPIO */
Quoted 1 line

Self docced, comment not needed.

OK

Quoted 8 lines

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

Quoted 7 lines
hw/arm/fsl-imx31.c +6 -0
+        object_property_set_bool(OBJECT(&s->gpio[i]), true, "realized", &err);
+        if (err) {
+            error_propagate(errp, err);
+            return;
+        }
+        /* Map GPIO memory */

Same.

OK

Quoted 72 lines

Otherwise,

Regards,
Peter

Quoted 64 lines
hw/arm/fsl-imx31.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));
+    }
+
      /* 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

#10 lore raw

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

In reply to Jean-Christophe Dubois
---
 hw/arm/fsl-imx25.c         | 31 +++++++++++++++++++++++++++++++
 include/hw/arm/fsl-imx25.h | 15 +++++++++++++++
 2 files changed, 46 insertions(+)
hw/arm/fsl-imx25.c +31 -0
index 6d157c9..035b7a8 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,32 @@ static void fsl_imx25_realize(DeviceState *dev, Error **errp)
                                             i2c_table[i].irq));
     }
 
+    /* Initialize all GPIO */
+    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 }
+        };
+
+        /* Initialize the GPIO */
+        object_property_set_bool(OBJECT(&s->gpio[i]), true, "realized", &err);
+        if (err) {
+            error_propagate(errp, err);
+            return;
+        }
+        /* Map GPIO memory */
+        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
Reviewed-by on v1 3/3
#11 lore raw

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

Quoted 1 line · Jean-Christophe Dubois

Same comments as for P2. Otherwise,

Regards,
Peter

Quoted 111 lines
---
 hw/arm/fsl-imx25.c         | 31 +++++++++++++++++++++++++++++++
 include/hw/arm/fsl-imx25.h | 15 +++++++++++++++
 2 files changed, 46 insertions(+)
hw/arm/fsl-imx25.c +31 -0
index 6d157c9..035b7a8 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,32 @@ static void fsl_imx25_realize(DeviceState *dev, Error **errp)
                                             i2c_table[i].irq));
     }
 
+    /* Initialize all GPIO */
+    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 }
+        };
+
+        /* Initialize the GPIO */
+        object_property_set_bool(OBJECT(&s->gpio[i]), true, "realized", &err);
+        if (err) {
+            error_propagate(errp, err);
+            return;
+        }
+        /* Map GPIO memory */
+        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