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/sysbus.h"
14 #include "hw/arm/pxa.h"
16 #include "qemu/module.h"
18 #define PXA2XX_GPIO_BANKS 4
20 #define TYPE_PXA2XX_GPIO "pxa2xx-gpio"
21 #define PXA2XX_GPIO(obj) \
22 OBJECT_CHECK(PXA2xxGPIOInfo, (obj), TYPE_PXA2XX_GPIO)
24 typedef struct PXA2xxGPIOInfo PXA2xxGPIOInfo
;
25 struct PXA2xxGPIOInfo
{
27 SysBusDevice parent_obj
;
31 qemu_irq irq0
, irq1
, irqX
;
36 /* XXX: GNU C vectors are more suitable */
37 uint32_t ilevel
[PXA2XX_GPIO_BANKS
];
38 uint32_t olevel
[PXA2XX_GPIO_BANKS
];
39 uint32_t dir
[PXA2XX_GPIO_BANKS
];
40 uint32_t rising
[PXA2XX_GPIO_BANKS
];
41 uint32_t falling
[PXA2XX_GPIO_BANKS
];
42 uint32_t status
[PXA2XX_GPIO_BANKS
];
43 uint32_t gafr
[PXA2XX_GPIO_BANKS
* 2];
45 uint32_t prev_level
[PXA2XX_GPIO_BANKS
];
46 qemu_irq handler
[PXA2XX_GPIO_BANKS
* 32];
64 } pxa2xx_gpio_regs
[0x200] = {
65 [0 ... 0x1ff] = { GPIO_NONE
, 0 },
66 #define PXA2XX_REG(reg, a0, a1, a2, a3) \
67 [a0] = { reg, 0 }, [a1] = { reg, 1 }, [a2] = { reg, 2 }, [a3] = { reg, 3 },
69 PXA2XX_REG(GPLR
, 0x000, 0x004, 0x008, 0x100)
70 PXA2XX_REG(GPSR
, 0x018, 0x01c, 0x020, 0x118)
71 PXA2XX_REG(GPCR
, 0x024, 0x028, 0x02c, 0x124)
72 PXA2XX_REG(GPDR
, 0x00c, 0x010, 0x014, 0x10c)
73 PXA2XX_REG(GRER
, 0x030, 0x034, 0x038, 0x130)
74 PXA2XX_REG(GFER
, 0x03c, 0x040, 0x044, 0x13c)
75 PXA2XX_REG(GEDR
, 0x048, 0x04c, 0x050, 0x148)
76 PXA2XX_REG(GAFR_L
, 0x054, 0x05c, 0x064, 0x06c)
77 PXA2XX_REG(GAFR_U
, 0x058, 0x060, 0x068, 0x070)
80 static void pxa2xx_gpio_irq_update(PXA2xxGPIOInfo
*s
)
82 if (s
->status
[0] & (1 << 0))
83 qemu_irq_raise(s
->irq0
);
85 qemu_irq_lower(s
->irq0
);
87 if (s
->status
[0] & (1 << 1))
88 qemu_irq_raise(s
->irq1
);
90 qemu_irq_lower(s
->irq1
);
92 if ((s
->status
[0] & ~3) | s
->status
[1] | s
->status
[2] | s
->status
[3])
93 qemu_irq_raise(s
->irqX
);
95 qemu_irq_lower(s
->irqX
);
98 /* Bitmap of pins used as standby and sleep wake-up sources. */
99 static const int pxa2xx_gpio_wake
[PXA2XX_GPIO_BANKS
] = {
100 0x8003fe1b, 0x002001fc, 0xec080000, 0x0012007f,
103 static void pxa2xx_gpio_set(void *opaque
, int line
, int level
)
105 PXA2xxGPIOInfo
*s
= (PXA2xxGPIOInfo
*) opaque
;
106 CPUState
*cpu
= CPU(s
->cpu
);
110 if (line
>= s
->lines
) {
111 printf("%s: No GPIO pin %i\n", __func__
, line
);
116 mask
= 1U << (line
& 31);
119 s
->status
[bank
] |= s
->rising
[bank
] & mask
&
120 ~s
->ilevel
[bank
] & ~s
->dir
[bank
];
121 s
->ilevel
[bank
] |= mask
;
123 s
->status
[bank
] |= s
->falling
[bank
] & mask
&
124 s
->ilevel
[bank
] & ~s
->dir
[bank
];
125 s
->ilevel
[bank
] &= ~mask
;
128 if (s
->status
[bank
] & mask
)
129 pxa2xx_gpio_irq_update(s
);
132 if (cpu
->halted
&& (mask
& ~s
->dir
[bank
] & pxa2xx_gpio_wake
[bank
])) {
133 cpu_interrupt(cpu
, CPU_INTERRUPT_EXITTB
);
137 static void pxa2xx_gpio_handler_update(PXA2xxGPIOInfo
*s
) {
138 uint32_t level
, diff
;
140 for (i
= 0; i
< PXA2XX_GPIO_BANKS
; i
++) {
141 level
= s
->olevel
[i
] & s
->dir
[i
];
143 for (diff
= s
->prev_level
[i
] ^ level
; diff
; diff
^= 1 << bit
) {
146 qemu_set_irq(s
->handler
[line
], (level
>> bit
) & 1);
149 s
->prev_level
[i
] = level
;
153 static uint64_t pxa2xx_gpio_read(void *opaque
, hwaddr offset
,
156 PXA2xxGPIOInfo
*s
= (PXA2xxGPIOInfo
*) opaque
;
162 bank
= pxa2xx_gpio_regs
[offset
].bank
;
163 switch (pxa2xx_gpio_regs
[offset
].reg
) {
164 case GPDR
: /* GPIO Pin-Direction registers */
167 case GPSR
: /* GPIO Pin-Output Set registers */
168 qemu_log_mask(LOG_GUEST_ERROR
,
169 "pxa2xx GPIO: read from write only register GPSR\n");
172 case GPCR
: /* GPIO Pin-Output Clear registers */
173 qemu_log_mask(LOG_GUEST_ERROR
,
174 "pxa2xx GPIO: read from write only register GPCR\n");
177 case GRER
: /* GPIO Rising-Edge Detect Enable registers */
178 return s
->rising
[bank
];
180 case GFER
: /* GPIO Falling-Edge Detect Enable registers */
181 return s
->falling
[bank
];
183 case GAFR_L
: /* GPIO Alternate Function registers */
184 return s
->gafr
[bank
* 2];
186 case GAFR_U
: /* GPIO Alternate Function registers */
187 return s
->gafr
[bank
* 2 + 1];
189 case GPLR
: /* GPIO Pin-Level registers */
190 ret
= (s
->olevel
[bank
] & s
->dir
[bank
]) |
191 (s
->ilevel
[bank
] & ~s
->dir
[bank
]);
192 qemu_irq_raise(s
->read_notify
);
195 case GEDR
: /* GPIO Edge Detect Status registers */
196 return s
->status
[bank
];
199 hw_error("%s: Bad offset " REG_FMT
"\n", __func__
, offset
);
205 static void pxa2xx_gpio_write(void *opaque
, hwaddr offset
,
206 uint64_t value
, unsigned size
)
208 PXA2xxGPIOInfo
*s
= (PXA2xxGPIOInfo
*) opaque
;
213 bank
= pxa2xx_gpio_regs
[offset
].bank
;
214 switch (pxa2xx_gpio_regs
[offset
].reg
) {
215 case GPDR
: /* GPIO Pin-Direction registers */
216 s
->dir
[bank
] = value
;
217 pxa2xx_gpio_handler_update(s
);
220 case GPSR
: /* GPIO Pin-Output Set registers */
221 s
->olevel
[bank
] |= value
;
222 pxa2xx_gpio_handler_update(s
);
225 case GPCR
: /* GPIO Pin-Output Clear registers */
226 s
->olevel
[bank
] &= ~value
;
227 pxa2xx_gpio_handler_update(s
);
230 case GRER
: /* GPIO Rising-Edge Detect Enable registers */
231 s
->rising
[bank
] = value
;
234 case GFER
: /* GPIO Falling-Edge Detect Enable registers */
235 s
->falling
[bank
] = value
;
238 case GAFR_L
: /* GPIO Alternate Function registers */
239 s
->gafr
[bank
* 2] = value
;
242 case GAFR_U
: /* GPIO Alternate Function registers */
243 s
->gafr
[bank
* 2 + 1] = value
;
246 case GEDR
: /* GPIO Edge Detect Status registers */
247 s
->status
[bank
] &= ~value
;
248 pxa2xx_gpio_irq_update(s
);
252 hw_error("%s: Bad offset " REG_FMT
"\n", __func__
, offset
);
256 static const MemoryRegionOps pxa_gpio_ops
= {
257 .read
= pxa2xx_gpio_read
,
258 .write
= pxa2xx_gpio_write
,
259 .endianness
= DEVICE_NATIVE_ENDIAN
,
262 DeviceState
*pxa2xx_gpio_init(hwaddr base
,
263 ARMCPU
*cpu
, DeviceState
*pic
, int lines
)
265 CPUState
*cs
= CPU(cpu
);
268 dev
= qdev_create(NULL
, TYPE_PXA2XX_GPIO
);
269 qdev_prop_set_int32(dev
, "lines", lines
);
270 qdev_prop_set_int32(dev
, "ncpu", cs
->cpu_index
);
271 qdev_init_nofail(dev
);
273 sysbus_mmio_map(SYS_BUS_DEVICE(dev
), 0, base
);
274 sysbus_connect_irq(SYS_BUS_DEVICE(dev
), 0,
275 qdev_get_gpio_in(pic
, PXA2XX_PIC_GPIO_0
));
276 sysbus_connect_irq(SYS_BUS_DEVICE(dev
), 1,
277 qdev_get_gpio_in(pic
, PXA2XX_PIC_GPIO_1
));
278 sysbus_connect_irq(SYS_BUS_DEVICE(dev
), 2,
279 qdev_get_gpio_in(pic
, PXA2XX_PIC_GPIO_X
));
284 static void pxa2xx_gpio_initfn(Object
*obj
)
286 SysBusDevice
*sbd
= SYS_BUS_DEVICE(obj
);
287 DeviceState
*dev
= DEVICE(sbd
);
288 PXA2xxGPIOInfo
*s
= PXA2XX_GPIO(dev
);
290 memory_region_init_io(&s
->iomem
, obj
, &pxa_gpio_ops
,
291 s
, "pxa2xx-gpio", 0x1000);
292 sysbus_init_mmio(sbd
, &s
->iomem
);
293 sysbus_init_irq(sbd
, &s
->irq0
);
294 sysbus_init_irq(sbd
, &s
->irq1
);
295 sysbus_init_irq(sbd
, &s
->irqX
);
298 static void pxa2xx_gpio_realize(DeviceState
*dev
, Error
**errp
)
300 PXA2xxGPIOInfo
*s
= PXA2XX_GPIO(dev
);
302 s
->cpu
= ARM_CPU(qemu_get_cpu(s
->ncpu
));
304 qdev_init_gpio_in(dev
, pxa2xx_gpio_set
, s
->lines
);
305 qdev_init_gpio_out(dev
, s
->handler
, s
->lines
);
309 * Registers a callback to notify on GPLR reads. This normally
310 * shouldn't be needed but it is used for the hack on Spitz machines.
312 void pxa2xx_gpio_read_notifier(DeviceState
*dev
, qemu_irq handler
)
314 PXA2xxGPIOInfo
*s
= PXA2XX_GPIO(dev
);
316 s
->read_notify
= handler
;
319 static const VMStateDescription vmstate_pxa2xx_gpio_regs
= {
320 .name
= "pxa2xx-gpio",
322 .minimum_version_id
= 1,
323 .fields
= (VMStateField
[]) {
324 VMSTATE_UINT32_ARRAY(ilevel
, PXA2xxGPIOInfo
, PXA2XX_GPIO_BANKS
),
325 VMSTATE_UINT32_ARRAY(olevel
, PXA2xxGPIOInfo
, PXA2XX_GPIO_BANKS
),
326 VMSTATE_UINT32_ARRAY(dir
, PXA2xxGPIOInfo
, PXA2XX_GPIO_BANKS
),
327 VMSTATE_UINT32_ARRAY(rising
, PXA2xxGPIOInfo
, PXA2XX_GPIO_BANKS
),
328 VMSTATE_UINT32_ARRAY(falling
, PXA2xxGPIOInfo
, PXA2XX_GPIO_BANKS
),
329 VMSTATE_UINT32_ARRAY(status
, PXA2xxGPIOInfo
, PXA2XX_GPIO_BANKS
),
330 VMSTATE_UINT32_ARRAY(gafr
, PXA2xxGPIOInfo
, PXA2XX_GPIO_BANKS
* 2),
331 VMSTATE_UINT32_ARRAY(prev_level
, PXA2xxGPIOInfo
, PXA2XX_GPIO_BANKS
),
332 VMSTATE_END_OF_LIST(),
336 static Property pxa2xx_gpio_properties
[] = {
337 DEFINE_PROP_INT32("lines", PXA2xxGPIOInfo
, lines
, 0),
338 DEFINE_PROP_INT32("ncpu", PXA2xxGPIOInfo
, ncpu
, 0),
339 DEFINE_PROP_END_OF_LIST(),
342 static void pxa2xx_gpio_class_init(ObjectClass
*klass
, void *data
)
344 DeviceClass
*dc
= DEVICE_CLASS(klass
);
346 dc
->desc
= "PXA2xx GPIO controller";
347 dc
->props
= pxa2xx_gpio_properties
;
348 dc
->vmsd
= &vmstate_pxa2xx_gpio_regs
;
349 dc
->realize
= pxa2xx_gpio_realize
;
352 static const TypeInfo pxa2xx_gpio_info
= {
353 .name
= TYPE_PXA2XX_GPIO
,
354 .parent
= TYPE_SYS_BUS_DEVICE
,
355 .instance_size
= sizeof(PXA2xxGPIOInfo
),
356 .instance_init
= pxa2xx_gpio_initfn
,
357 .class_init
= pxa2xx_gpio_class_init
,
360 static void pxa2xx_gpio_register_types(void)
362 type_register_static(&pxa2xx_gpio_info
);
365 type_init(pxa2xx_gpio_register_types
)