2 * i.MX processors GPIO emulation.
4 * Copyright (C) 2015 Jean-Christophe Dubois <jcd@tribudubois.net>
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License as
8 * published by the Free Software Foundation; either version 2 or
9 * (at your option) version 3 of the License.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, see <http://www.gnu.org/licenses/>.
20 #include "qemu/osdep.h"
21 #include "hw/gpio/imx_gpio.h"
23 #include "qemu/module.h"
25 #ifndef DEBUG_IMX_GPIO
26 #define DEBUG_IMX_GPIO 0
29 typedef enum IMXGPIOLevel
{
30 IMX_GPIO_LEVEL_LOW
= 0,
31 IMX_GPIO_LEVEL_HIGH
= 1,
34 #define DPRINTF(fmt, args...) \
36 if (DEBUG_IMX_GPIO) { \
37 fprintf(stderr, "[%s]%s: " fmt , TYPE_IMX_GPIO, \
42 static const char *imx_gpio_reg_name(uint32_t reg
)
66 static void imx_gpio_update_int(IMXGPIOState
*s
)
68 if (s
->has_upper_pin_irq
) {
69 qemu_set_irq(s
->irq
[0], (s
->isr
& s
->imr
& 0x0000FFFF) ? 1 : 0);
70 qemu_set_irq(s
->irq
[1], (s
->isr
& s
->imr
& 0xFFFF0000) ? 1 : 0);
72 qemu_set_irq(s
->irq
[0], (s
->isr
& s
->imr
) ? 1 : 0);
76 static void imx_gpio_set_int_line(IMXGPIOState
*s
, int line
, IMXGPIOLevel level
)
78 /* if this signal isn't configured as an input signal, nothing to do */
79 if (!extract32(s
->gdir
, line
, 1)) {
83 /* When set, EDGE_SEL overrides the ICR config */
84 if (extract32(s
->edge_sel
, line
, 1)) {
85 /* we detect interrupt on rising and falling edge */
86 if (extract32(s
->psr
, line
, 1) != level
) {
88 s
->isr
= deposit32(s
->isr
, line
, 1, 1);
90 } else if (extract64(s
->icr
, 2*line
+ 1, 1)) {
91 /* interrupt is edge sensitive */
92 if (extract32(s
->psr
, line
, 1) != level
) {
94 if (extract64(s
->icr
, 2*line
, 1) != level
) {
95 s
->isr
= deposit32(s
->isr
, line
, 1, 1);
99 /* interrupt is level sensitive */
100 if (extract64(s
->icr
, 2*line
, 1) == level
) {
101 s
->isr
= deposit32(s
->isr
, line
, 1, 1);
106 static void imx_gpio_set(void *opaque
, int line
, int level
)
108 IMXGPIOState
*s
= IMX_GPIO(opaque
);
109 IMXGPIOLevel imx_level
= level
? IMX_GPIO_LEVEL_HIGH
: IMX_GPIO_LEVEL_LOW
;
111 imx_gpio_set_int_line(s
, line
, imx_level
);
113 /* this is an input signal, so set PSR */
114 s
->psr
= deposit32(s
->psr
, line
, 1, imx_level
);
116 imx_gpio_update_int(s
);
119 static void imx_gpio_set_all_int_lines(IMXGPIOState
*s
)
123 for (i
= 0; i
< IMX_GPIO_PIN_COUNT
; i
++) {
124 IMXGPIOLevel imx_level
= extract32(s
->psr
, i
, 1);
125 imx_gpio_set_int_line(s
, i
, imx_level
);
128 imx_gpio_update_int(s
);
131 static inline void imx_gpio_set_all_output_lines(IMXGPIOState
*s
)
135 for (i
= 0; i
< IMX_GPIO_PIN_COUNT
; i
++) {
137 * if the line is set as output, then forward the line
140 if (extract32(s
->gdir
, i
, 1) && s
->output
[i
]) {
141 qemu_set_irq(s
->output
[i
], extract32(s
->dr
, i
, 1));
146 static uint64_t imx_gpio_read(void *opaque
, hwaddr offset
, unsigned size
)
148 IMXGPIOState
*s
= IMX_GPIO(opaque
);
149 uint32_t reg_value
= 0;
154 * depending on the "line" configuration, the bit values
155 * are coming either from DR or PSR
157 reg_value
= (s
->dr
& s
->gdir
) | (s
->psr
& ~s
->gdir
);
165 reg_value
= s
->psr
& ~s
->gdir
;
169 reg_value
= extract64(s
->icr
, 0, 32);
173 reg_value
= extract64(s
->icr
, 32, 32);
185 if (s
->has_edge_sel
) {
186 reg_value
= s
->edge_sel
;
188 qemu_log_mask(LOG_GUEST_ERROR
, "[%s]%s: EDGE_SEL register not "
189 "present on this version of GPIO device\n",
190 TYPE_IMX_GPIO
, __func__
);
195 qemu_log_mask(LOG_GUEST_ERROR
, "[%s]%s: Bad register at offset 0x%"
196 HWADDR_PRIx
"\n", TYPE_IMX_GPIO
, __func__
, offset
);
200 DPRINTF("(%s) = 0x%" PRIx32
"\n", imx_gpio_reg_name(offset
), reg_value
);
205 static void imx_gpio_write(void *opaque
, hwaddr offset
, uint64_t value
,
208 IMXGPIOState
*s
= IMX_GPIO(opaque
);
210 DPRINTF("(%s, value = 0x%" PRIx32
")\n", imx_gpio_reg_name(offset
),
216 imx_gpio_set_all_output_lines(s
);
221 imx_gpio_set_all_output_lines(s
);
222 imx_gpio_set_all_int_lines(s
);
226 s
->icr
= deposit64(s
->icr
, 0, 32, value
);
227 imx_gpio_set_all_int_lines(s
);
231 s
->icr
= deposit64(s
->icr
, 32, 32, value
);
232 imx_gpio_set_all_int_lines(s
);
237 imx_gpio_update_int(s
);
242 imx_gpio_set_all_int_lines(s
);
246 if (s
->has_edge_sel
) {
248 imx_gpio_set_all_int_lines(s
);
250 qemu_log_mask(LOG_GUEST_ERROR
, "[%s]%s: EDGE_SEL register not "
251 "present on this version of GPIO device\n",
252 TYPE_IMX_GPIO
, __func__
);
257 qemu_log_mask(LOG_GUEST_ERROR
, "[%s]%s: Bad register at offset 0x%"
258 HWADDR_PRIx
"\n", TYPE_IMX_GPIO
, __func__
, offset
);
265 static const MemoryRegionOps imx_gpio_ops
= {
266 .read
= imx_gpio_read
,
267 .write
= imx_gpio_write
,
268 .valid
.min_access_size
= 4,
269 .valid
.max_access_size
= 4,
270 .endianness
= DEVICE_NATIVE_ENDIAN
,
273 static const VMStateDescription vmstate_imx_gpio
= {
274 .name
= TYPE_IMX_GPIO
,
276 .minimum_version_id
= 1,
277 .minimum_version_id_old
= 1,
278 .fields
= (VMStateField
[]) {
279 VMSTATE_UINT32(dr
, IMXGPIOState
),
280 VMSTATE_UINT32(gdir
, IMXGPIOState
),
281 VMSTATE_UINT32(psr
, IMXGPIOState
),
282 VMSTATE_UINT64(icr
, IMXGPIOState
),
283 VMSTATE_UINT32(imr
, IMXGPIOState
),
284 VMSTATE_UINT32(isr
, IMXGPIOState
),
285 VMSTATE_BOOL(has_edge_sel
, IMXGPIOState
),
286 VMSTATE_UINT32(edge_sel
, IMXGPIOState
),
287 VMSTATE_END_OF_LIST()
291 static Property imx_gpio_properties
[] = {
292 DEFINE_PROP_BOOL("has-edge-sel", IMXGPIOState
, has_edge_sel
, true),
293 DEFINE_PROP_BOOL("has-upper-pin-irq", IMXGPIOState
, has_upper_pin_irq
,
295 DEFINE_PROP_END_OF_LIST(),
298 static void imx_gpio_reset(DeviceState
*dev
)
300 IMXGPIOState
*s
= IMX_GPIO(dev
);
310 imx_gpio_set_all_output_lines(s
);
311 imx_gpio_update_int(s
);
314 static void imx_gpio_realize(DeviceState
*dev
, Error
**errp
)
316 IMXGPIOState
*s
= IMX_GPIO(dev
);
318 memory_region_init_io(&s
->iomem
, OBJECT(s
), &imx_gpio_ops
, s
,
319 TYPE_IMX_GPIO
, IMX_GPIO_MEM_SIZE
);
321 qdev_init_gpio_in(DEVICE(s
), imx_gpio_set
, IMX_GPIO_PIN_COUNT
);
322 qdev_init_gpio_out(DEVICE(s
), s
->output
, IMX_GPIO_PIN_COUNT
);
324 sysbus_init_irq(SYS_BUS_DEVICE(dev
), &s
->irq
[0]);
325 sysbus_init_irq(SYS_BUS_DEVICE(dev
), &s
->irq
[1]);
326 sysbus_init_mmio(SYS_BUS_DEVICE(dev
), &s
->iomem
);
329 static void imx_gpio_class_init(ObjectClass
*klass
, void *data
)
331 DeviceClass
*dc
= DEVICE_CLASS(klass
);
333 dc
->realize
= imx_gpio_realize
;
334 dc
->reset
= imx_gpio_reset
;
335 dc
->props
= imx_gpio_properties
;
336 dc
->vmsd
= &vmstate_imx_gpio
;
337 dc
->desc
= "i.MX GPIO controller";
340 static const TypeInfo imx_gpio_info
= {
341 .name
= TYPE_IMX_GPIO
,
342 .parent
= TYPE_SYS_BUS_DEVICE
,
343 .instance_size
= sizeof(IMXGPIOState
),
344 .class_init
= imx_gpio_class_init
,
347 static void imx_gpio_register_types(void)
349 type_register_static(&imx_gpio_info
);
352 type_init(imx_gpio_register_types
)