2 * Intel XScale PXA255/270 GPIO controller emulation.
4 * Copyright (c) 2006 Openedhand Ltd.
5 * Written by Andrzej Zaborowski <balrog@zabor.org>
7 * This code is licensed under the GPL.
10 #include "qemu/osdep.h"
13 #include "hw/qdev-properties.h"
14 #include "hw/sysbus.h"
15 #include "migration/vmstate.h"
16 #include "hw/arm/pxa.h"
17 #include "qapi/error.h"
19 #include "qemu/module.h"
21 #define PXA2XX_GPIO_BANKS 4
23 #define TYPE_PXA2XX_GPIO "pxa2xx-gpio"
24 #define PXA2XX_GPIO(obj) \
25 OBJECT_CHECK(PXA2xxGPIOInfo, (obj), TYPE_PXA2XX_GPIO)
27 typedef struct PXA2xxGPIOInfo PXA2xxGPIOInfo
;
28 struct PXA2xxGPIOInfo
{
30 SysBusDevice parent_obj
;
34 qemu_irq irq0
, irq1
, irqX
;
39 /* XXX: GNU C vectors are more suitable */
40 uint32_t ilevel
[PXA2XX_GPIO_BANKS
];
41 uint32_t olevel
[PXA2XX_GPIO_BANKS
];
42 uint32_t dir
[PXA2XX_GPIO_BANKS
];
43 uint32_t rising
[PXA2XX_GPIO_BANKS
];
44 uint32_t falling
[PXA2XX_GPIO_BANKS
];
45 uint32_t status
[PXA2XX_GPIO_BANKS
];
46 uint32_t gafr
[PXA2XX_GPIO_BANKS
* 2];
48 uint32_t prev_level
[PXA2XX_GPIO_BANKS
];
49 qemu_irq handler
[PXA2XX_GPIO_BANKS
* 32];
67 } pxa2xx_gpio_regs
[0x200] = {
68 [0 ... 0x1ff] = { GPIO_NONE
, 0 },
69 #define PXA2XX_REG(reg, a0, a1, a2, a3) \
70 [a0] = { reg, 0 }, [a1] = { reg, 1 }, [a2] = { reg, 2 }, [a3] = { reg, 3 },
72 PXA2XX_REG(GPLR
, 0x000, 0x004, 0x008, 0x100)
73 PXA2XX_REG(GPSR
, 0x018, 0x01c, 0x020, 0x118)
74 PXA2XX_REG(GPCR
, 0x024, 0x028, 0x02c, 0x124)
75 PXA2XX_REG(GPDR
, 0x00c, 0x010, 0x014, 0x10c)
76 PXA2XX_REG(GRER
, 0x030, 0x034, 0x038, 0x130)
77 PXA2XX_REG(GFER
, 0x03c, 0x040, 0x044, 0x13c)
78 PXA2XX_REG(GEDR
, 0x048, 0x04c, 0x050, 0x148)
79 PXA2XX_REG(GAFR_L
, 0x054, 0x05c, 0x064, 0x06c)
80 PXA2XX_REG(GAFR_U
, 0x058, 0x060, 0x068, 0x070)
83 static void pxa2xx_gpio_irq_update(PXA2xxGPIOInfo
*s
)
85 if (s
->status
[0] & (1 << 0))
86 qemu_irq_raise(s
->irq0
);
88 qemu_irq_lower(s
->irq0
);
90 if (s
->status
[0] & (1 << 1))
91 qemu_irq_raise(s
->irq1
);
93 qemu_irq_lower(s
->irq1
);
95 if ((s
->status
[0] & ~3) | s
->status
[1] | s
->status
[2] | s
->status
[3])
96 qemu_irq_raise(s
->irqX
);
98 qemu_irq_lower(s
->irqX
);
101 /* Bitmap of pins used as standby and sleep wake-up sources. */
102 static const int pxa2xx_gpio_wake
[PXA2XX_GPIO_BANKS
] = {
103 0x8003fe1b, 0x002001fc, 0xec080000, 0x0012007f,
106 static void pxa2xx_gpio_set(void *opaque
, int line
, int level
)
108 PXA2xxGPIOInfo
*s
= (PXA2xxGPIOInfo
*) opaque
;
109 CPUState
*cpu
= CPU(s
->cpu
);
113 if (line
>= s
->lines
) {
114 printf("%s: No GPIO pin %i\n", __func__
, line
);
119 mask
= 1U << (line
& 31);
122 s
->status
[bank
] |= s
->rising
[bank
] & mask
&
123 ~s
->ilevel
[bank
] & ~s
->dir
[bank
];
124 s
->ilevel
[bank
] |= mask
;
126 s
->status
[bank
] |= s
->falling
[bank
] & mask
&
127 s
->ilevel
[bank
] & ~s
->dir
[bank
];
128 s
->ilevel
[bank
] &= ~mask
;
131 if (s
->status
[bank
] & mask
)
132 pxa2xx_gpio_irq_update(s
);
135 if (cpu
->halted
&& (mask
& ~s
->dir
[bank
] & pxa2xx_gpio_wake
[bank
])) {
136 cpu_interrupt(cpu
, CPU_INTERRUPT_EXITTB
);
140 static void pxa2xx_gpio_handler_update(PXA2xxGPIOInfo
*s
) {
141 uint32_t level
, diff
;
143 for (i
= 0; i
< PXA2XX_GPIO_BANKS
; i
++) {
144 level
= s
->olevel
[i
] & s
->dir
[i
];
146 for (diff
= s
->prev_level
[i
] ^ level
; diff
; diff
^= 1 << bit
) {
149 qemu_set_irq(s
->handler
[line
], (level
>> bit
) & 1);
152 s
->prev_level
[i
] = level
;
156 static uint64_t pxa2xx_gpio_read(void *opaque
, hwaddr offset
,
159 PXA2xxGPIOInfo
*s
= (PXA2xxGPIOInfo
*) opaque
;
165 bank
= pxa2xx_gpio_regs
[offset
].bank
;
166 switch (pxa2xx_gpio_regs
[offset
].reg
) {
167 case GPDR
: /* GPIO Pin-Direction registers */
170 case GPSR
: /* GPIO Pin-Output Set registers */
171 qemu_log_mask(LOG_GUEST_ERROR
,
172 "pxa2xx GPIO: read from write only register GPSR\n");
175 case GPCR
: /* GPIO Pin-Output Clear registers */
176 qemu_log_mask(LOG_GUEST_ERROR
,
177 "pxa2xx GPIO: read from write only register GPCR\n");
180 case GRER
: /* GPIO Rising-Edge Detect Enable registers */
181 return s
->rising
[bank
];
183 case GFER
: /* GPIO Falling-Edge Detect Enable registers */
184 return s
->falling
[bank
];
186 case GAFR_L
: /* GPIO Alternate Function registers */
187 return s
->gafr
[bank
* 2];
189 case GAFR_U
: /* GPIO Alternate Function registers */
190 return s
->gafr
[bank
* 2 + 1];
192 case GPLR
: /* GPIO Pin-Level registers */
193 ret
= (s
->olevel
[bank
] & s
->dir
[bank
]) |
194 (s
->ilevel
[bank
] & ~s
->dir
[bank
]);
195 qemu_irq_raise(s
->read_notify
);
198 case GEDR
: /* GPIO Edge Detect Status registers */
199 return s
->status
[bank
];
202 qemu_log_mask(LOG_GUEST_ERROR
, "%s: Bad offset 0x%" HWADDR_PRIX
"\n",
209 static void pxa2xx_gpio_write(void *opaque
, hwaddr offset
,
210 uint64_t value
, unsigned size
)
212 PXA2xxGPIOInfo
*s
= (PXA2xxGPIOInfo
*) opaque
;
217 bank
= pxa2xx_gpio_regs
[offset
].bank
;
218 switch (pxa2xx_gpio_regs
[offset
].reg
) {
219 case GPDR
: /* GPIO Pin-Direction registers */
220 s
->dir
[bank
] = value
;
221 pxa2xx_gpio_handler_update(s
);
224 case GPSR
: /* GPIO Pin-Output Set registers */
225 s
->olevel
[bank
] |= value
;
226 pxa2xx_gpio_handler_update(s
);
229 case GPCR
: /* GPIO Pin-Output Clear registers */
230 s
->olevel
[bank
] &= ~value
;
231 pxa2xx_gpio_handler_update(s
);
234 case GRER
: /* GPIO Rising-Edge Detect Enable registers */
235 s
->rising
[bank
] = value
;
238 case GFER
: /* GPIO Falling-Edge Detect Enable registers */
239 s
->falling
[bank
] = value
;
242 case GAFR_L
: /* GPIO Alternate Function registers */
243 s
->gafr
[bank
* 2] = value
;
246 case GAFR_U
: /* GPIO Alternate Function registers */
247 s
->gafr
[bank
* 2 + 1] = value
;
250 case GEDR
: /* GPIO Edge Detect Status registers */
251 s
->status
[bank
] &= ~value
;
252 pxa2xx_gpio_irq_update(s
);
256 qemu_log_mask(LOG_GUEST_ERROR
, "%s: Bad offset 0x%" HWADDR_PRIX
"\n",
261 static const MemoryRegionOps pxa_gpio_ops
= {
262 .read
= pxa2xx_gpio_read
,
263 .write
= pxa2xx_gpio_write
,
264 .endianness
= DEVICE_NATIVE_ENDIAN
,
267 DeviceState
*pxa2xx_gpio_init(hwaddr base
,
268 ARMCPU
*cpu
, DeviceState
*pic
, int lines
)
270 CPUState
*cs
= CPU(cpu
);
273 dev
= qdev_new(TYPE_PXA2XX_GPIO
);
274 qdev_prop_set_int32(dev
, "lines", lines
);
275 qdev_prop_set_int32(dev
, "ncpu", cs
->cpu_index
);
276 sysbus_realize_and_unref(SYS_BUS_DEVICE(dev
), &error_fatal
);
278 sysbus_mmio_map(SYS_BUS_DEVICE(dev
), 0, base
);
279 sysbus_connect_irq(SYS_BUS_DEVICE(dev
), 0,
280 qdev_get_gpio_in(pic
, PXA2XX_PIC_GPIO_0
));
281 sysbus_connect_irq(SYS_BUS_DEVICE(dev
), 1,
282 qdev_get_gpio_in(pic
, PXA2XX_PIC_GPIO_1
));
283 sysbus_connect_irq(SYS_BUS_DEVICE(dev
), 2,
284 qdev_get_gpio_in(pic
, PXA2XX_PIC_GPIO_X
));
289 static void pxa2xx_gpio_initfn(Object
*obj
)
291 SysBusDevice
*sbd
= SYS_BUS_DEVICE(obj
);
292 DeviceState
*dev
= DEVICE(sbd
);
293 PXA2xxGPIOInfo
*s
= PXA2XX_GPIO(dev
);
295 memory_region_init_io(&s
->iomem
, obj
, &pxa_gpio_ops
,
296 s
, "pxa2xx-gpio", 0x1000);
297 sysbus_init_mmio(sbd
, &s
->iomem
);
298 sysbus_init_irq(sbd
, &s
->irq0
);
299 sysbus_init_irq(sbd
, &s
->irq1
);
300 sysbus_init_irq(sbd
, &s
->irqX
);
303 static void pxa2xx_gpio_realize(DeviceState
*dev
, Error
**errp
)
305 PXA2xxGPIOInfo
*s
= PXA2XX_GPIO(dev
);
307 s
->cpu
= ARM_CPU(qemu_get_cpu(s
->ncpu
));
309 qdev_init_gpio_in(dev
, pxa2xx_gpio_set
, s
->lines
);
310 qdev_init_gpio_out(dev
, s
->handler
, s
->lines
);
314 * Registers a callback to notify on GPLR reads. This normally
315 * shouldn't be needed but it is used for the hack on Spitz machines.
317 void pxa2xx_gpio_read_notifier(DeviceState
*dev
, qemu_irq handler
)
319 PXA2xxGPIOInfo
*s
= PXA2XX_GPIO(dev
);
321 s
->read_notify
= handler
;
324 static const VMStateDescription vmstate_pxa2xx_gpio_regs
= {
325 .name
= "pxa2xx-gpio",
327 .minimum_version_id
= 1,
328 .fields
= (VMStateField
[]) {
329 VMSTATE_UINT32_ARRAY(ilevel
, PXA2xxGPIOInfo
, PXA2XX_GPIO_BANKS
),
330 VMSTATE_UINT32_ARRAY(olevel
, PXA2xxGPIOInfo
, PXA2XX_GPIO_BANKS
),
331 VMSTATE_UINT32_ARRAY(dir
, PXA2xxGPIOInfo
, PXA2XX_GPIO_BANKS
),
332 VMSTATE_UINT32_ARRAY(rising
, PXA2xxGPIOInfo
, PXA2XX_GPIO_BANKS
),
333 VMSTATE_UINT32_ARRAY(falling
, PXA2xxGPIOInfo
, PXA2XX_GPIO_BANKS
),
334 VMSTATE_UINT32_ARRAY(status
, PXA2xxGPIOInfo
, PXA2XX_GPIO_BANKS
),
335 VMSTATE_UINT32_ARRAY(gafr
, PXA2xxGPIOInfo
, PXA2XX_GPIO_BANKS
* 2),
336 VMSTATE_UINT32_ARRAY(prev_level
, PXA2xxGPIOInfo
, PXA2XX_GPIO_BANKS
),
337 VMSTATE_END_OF_LIST(),
341 static Property pxa2xx_gpio_properties
[] = {
342 DEFINE_PROP_INT32("lines", PXA2xxGPIOInfo
, lines
, 0),
343 DEFINE_PROP_INT32("ncpu", PXA2xxGPIOInfo
, ncpu
, 0),
344 DEFINE_PROP_END_OF_LIST(),
347 static void pxa2xx_gpio_class_init(ObjectClass
*klass
, void *data
)
349 DeviceClass
*dc
= DEVICE_CLASS(klass
);
351 dc
->desc
= "PXA2xx GPIO controller";
352 device_class_set_props(dc
, pxa2xx_gpio_properties
);
353 dc
->vmsd
= &vmstate_pxa2xx_gpio_regs
;
354 dc
->realize
= pxa2xx_gpio_realize
;
357 static const TypeInfo pxa2xx_gpio_info
= {
358 .name
= TYPE_PXA2XX_GPIO
,
359 .parent
= TYPE_SYS_BUS_DEVICE
,
360 .instance_size
= sizeof(PXA2xxGPIOInfo
),
361 .instance_init
= pxa2xx_gpio_initfn
,
362 .class_init
= pxa2xx_gpio_class_init
,
365 static void pxa2xx_gpio_register_types(void)
367 type_register_static(&pxa2xx_gpio_info
);
370 type_init(pxa2xx_gpio_register_types
)