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.
14 #define PXA2XX_GPIO_BANKS 4
16 typedef struct PXA2xxGPIOInfo PXA2xxGPIOInfo
;
17 struct PXA2xxGPIOInfo
{
19 qemu_irq irq0
, irq1
, irqX
;
24 /* XXX: GNU C vectors are more suitable */
25 uint32_t ilevel
[PXA2XX_GPIO_BANKS
];
26 uint32_t olevel
[PXA2XX_GPIO_BANKS
];
27 uint32_t dir
[PXA2XX_GPIO_BANKS
];
28 uint32_t rising
[PXA2XX_GPIO_BANKS
];
29 uint32_t falling
[PXA2XX_GPIO_BANKS
];
30 uint32_t status
[PXA2XX_GPIO_BANKS
];
31 uint32_t gpsr
[PXA2XX_GPIO_BANKS
];
32 uint32_t gafr
[PXA2XX_GPIO_BANKS
* 2];
34 uint32_t prev_level
[PXA2XX_GPIO_BANKS
];
35 qemu_irq handler
[PXA2XX_GPIO_BANKS
* 32];
53 } pxa2xx_gpio_regs
[0x200] = {
54 [0 ... 0x1ff] = { GPIO_NONE
, 0 },
55 #define PXA2XX_REG(reg, a0, a1, a2, a3) \
56 [a0] = { reg, 0 }, [a1] = { reg, 1 }, [a2] = { reg, 2 }, [a3] = { reg, 3 },
58 PXA2XX_REG(GPLR
, 0x000, 0x004, 0x008, 0x100)
59 PXA2XX_REG(GPSR
, 0x018, 0x01c, 0x020, 0x118)
60 PXA2XX_REG(GPCR
, 0x024, 0x028, 0x02c, 0x124)
61 PXA2XX_REG(GPDR
, 0x00c, 0x010, 0x014, 0x10c)
62 PXA2XX_REG(GRER
, 0x030, 0x034, 0x038, 0x130)
63 PXA2XX_REG(GFER
, 0x03c, 0x040, 0x044, 0x13c)
64 PXA2XX_REG(GEDR
, 0x048, 0x04c, 0x050, 0x148)
65 PXA2XX_REG(GAFR_L
, 0x054, 0x05c, 0x064, 0x06c)
66 PXA2XX_REG(GAFR_U
, 0x058, 0x060, 0x068, 0x070)
69 static void pxa2xx_gpio_irq_update(PXA2xxGPIOInfo
*s
)
71 if (s
->status
[0] & (1 << 0))
72 qemu_irq_raise(s
->irq0
);
74 qemu_irq_lower(s
->irq0
);
76 if (s
->status
[0] & (1 << 1))
77 qemu_irq_raise(s
->irq1
);
79 qemu_irq_lower(s
->irq1
);
81 if ((s
->status
[0] & ~3) | s
->status
[1] | s
->status
[2] | s
->status
[3])
82 qemu_irq_raise(s
->irqX
);
84 qemu_irq_lower(s
->irqX
);
87 /* Bitmap of pins used as standby and sleep wake-up sources. */
88 static const int pxa2xx_gpio_wake
[PXA2XX_GPIO_BANKS
] = {
89 0x8003fe1b, 0x002001fc, 0xec080000, 0x0012007f,
92 static void pxa2xx_gpio_set(void *opaque
, int line
, int level
)
94 PXA2xxGPIOInfo
*s
= (PXA2xxGPIOInfo
*) opaque
;
98 if (line
>= s
->lines
) {
99 printf("%s: No GPIO pin %i\n", __FUNCTION__
, line
);
104 mask
= 1 << (line
& 31);
107 s
->status
[bank
] |= s
->rising
[bank
] & mask
&
108 ~s
->ilevel
[bank
] & ~s
->dir
[bank
];
109 s
->ilevel
[bank
] |= mask
;
111 s
->status
[bank
] |= s
->falling
[bank
] & mask
&
112 s
->ilevel
[bank
] & ~s
->dir
[bank
];
113 s
->ilevel
[bank
] &= ~mask
;
116 if (s
->status
[bank
] & mask
)
117 pxa2xx_gpio_irq_update(s
);
120 if (s
->cpu_env
->halted
&& (mask
& ~s
->dir
[bank
] & pxa2xx_gpio_wake
[bank
]))
121 cpu_interrupt(s
->cpu_env
, CPU_INTERRUPT_EXITTB
);
124 static void pxa2xx_gpio_handler_update(PXA2xxGPIOInfo
*s
) {
125 uint32_t level
, diff
;
127 for (i
= 0; i
< PXA2XX_GPIO_BANKS
; i
++) {
128 level
= s
->olevel
[i
] & s
->dir
[i
];
130 for (diff
= s
->prev_level
[i
] ^ level
; diff
; diff
^= 1 << bit
) {
133 qemu_set_irq(s
->handler
[line
], (level
>> bit
) & 1);
136 s
->prev_level
[i
] = level
;
140 static uint32_t pxa2xx_gpio_read(void *opaque
, target_phys_addr_t offset
)
142 PXA2xxGPIOInfo
*s
= (PXA2xxGPIOInfo
*) opaque
;
148 bank
= pxa2xx_gpio_regs
[offset
].bank
;
149 switch (pxa2xx_gpio_regs
[offset
].reg
) {
150 case GPDR
: /* GPIO Pin-Direction registers */
153 case GPSR
: /* GPIO Pin-Output Set registers */
154 printf("%s: Read from a write-only register " REG_FMT
"\n",
155 __FUNCTION__
, offset
);
156 return s
->gpsr
[bank
]; /* Return last written value. */
158 case GPCR
: /* GPIO Pin-Output Clear registers */
159 printf("%s: Read from a write-only register " REG_FMT
"\n",
160 __FUNCTION__
, offset
);
161 return 31337; /* Specified as unpredictable in the docs. */
163 case GRER
: /* GPIO Rising-Edge Detect Enable registers */
164 return s
->rising
[bank
];
166 case GFER
: /* GPIO Falling-Edge Detect Enable registers */
167 return s
->falling
[bank
];
169 case GAFR_L
: /* GPIO Alternate Function registers */
170 return s
->gafr
[bank
* 2];
172 case GAFR_U
: /* GPIO Alternate Function registers */
173 return s
->gafr
[bank
* 2 + 1];
175 case GPLR
: /* GPIO Pin-Level registers */
176 ret
= (s
->olevel
[bank
] & s
->dir
[bank
]) |
177 (s
->ilevel
[bank
] & ~s
->dir
[bank
]);
178 qemu_irq_raise(s
->read_notify
);
181 case GEDR
: /* GPIO Edge Detect Status registers */
182 return s
->status
[bank
];
185 hw_error("%s: Bad offset " REG_FMT
"\n", __FUNCTION__
, offset
);
191 static void pxa2xx_gpio_write(void *opaque
,
192 target_phys_addr_t offset
, uint32_t value
)
194 PXA2xxGPIOInfo
*s
= (PXA2xxGPIOInfo
*) opaque
;
199 bank
= pxa2xx_gpio_regs
[offset
].bank
;
200 switch (pxa2xx_gpio_regs
[offset
].reg
) {
201 case GPDR
: /* GPIO Pin-Direction registers */
202 s
->dir
[bank
] = value
;
203 pxa2xx_gpio_handler_update(s
);
206 case GPSR
: /* GPIO Pin-Output Set registers */
207 s
->olevel
[bank
] |= value
;
208 pxa2xx_gpio_handler_update(s
);
209 s
->gpsr
[bank
] = value
;
212 case GPCR
: /* GPIO Pin-Output Clear registers */
213 s
->olevel
[bank
] &= ~value
;
214 pxa2xx_gpio_handler_update(s
);
217 case GRER
: /* GPIO Rising-Edge Detect Enable registers */
218 s
->rising
[bank
] = value
;
221 case GFER
: /* GPIO Falling-Edge Detect Enable registers */
222 s
->falling
[bank
] = value
;
225 case GAFR_L
: /* GPIO Alternate Function registers */
226 s
->gafr
[bank
* 2] = value
;
229 case GAFR_U
: /* GPIO Alternate Function registers */
230 s
->gafr
[bank
* 2 + 1] = value
;
233 case GEDR
: /* GPIO Edge Detect Status registers */
234 s
->status
[bank
] &= ~value
;
235 pxa2xx_gpio_irq_update(s
);
239 hw_error("%s: Bad offset " REG_FMT
"\n", __FUNCTION__
, offset
);
243 static CPUReadMemoryFunc
* const pxa2xx_gpio_readfn
[] = {
249 static CPUWriteMemoryFunc
* const pxa2xx_gpio_writefn
[] = {
255 DeviceState
*pxa2xx_gpio_init(target_phys_addr_t base
,
256 CPUState
*env
, DeviceState
*pic
, int lines
)
260 dev
= qdev_create(NULL
, "pxa2xx-gpio");
261 qdev_prop_set_int32(dev
, "lines", lines
);
262 qdev_prop_set_int32(dev
, "ncpu", env
->cpu_index
);
263 qdev_init_nofail(dev
);
265 sysbus_mmio_map(sysbus_from_qdev(dev
), 0, base
);
266 sysbus_connect_irq(sysbus_from_qdev(dev
), 0,
267 qdev_get_gpio_in(pic
, PXA2XX_PIC_GPIO_0
));
268 sysbus_connect_irq(sysbus_from_qdev(dev
), 1,
269 qdev_get_gpio_in(pic
, PXA2XX_PIC_GPIO_1
));
270 sysbus_connect_irq(sysbus_from_qdev(dev
), 2,
271 qdev_get_gpio_in(pic
, PXA2XX_PIC_GPIO_X
));
276 static int pxa2xx_gpio_initfn(SysBusDevice
*dev
)
281 s
= FROM_SYSBUS(PXA2xxGPIOInfo
, dev
);
283 s
->cpu_env
= qemu_get_cpu(s
->ncpu
);
285 qdev_init_gpio_in(&dev
->qdev
, pxa2xx_gpio_set
, s
->lines
);
286 qdev_init_gpio_out(&dev
->qdev
, s
->handler
, s
->lines
);
288 iomemtype
= cpu_register_io_memory(pxa2xx_gpio_readfn
,
289 pxa2xx_gpio_writefn
, s
, DEVICE_NATIVE_ENDIAN
);
291 sysbus_init_mmio(dev
, 0x1000, iomemtype
);
292 sysbus_init_irq(dev
, &s
->irq0
);
293 sysbus_init_irq(dev
, &s
->irq1
);
294 sysbus_init_irq(dev
, &s
->irqX
);
300 * Registers a callback to notify on GPLR reads. This normally
301 * shouldn't be needed but it is used for the hack on Spitz machines.
303 void pxa2xx_gpio_read_notifier(DeviceState
*dev
, qemu_irq handler
)
305 PXA2xxGPIOInfo
*s
= FROM_SYSBUS(PXA2xxGPIOInfo
, sysbus_from_qdev(dev
));
306 s
->read_notify
= handler
;
309 static const VMStateDescription vmstate_pxa2xx_gpio_regs
= {
310 .name
= "pxa2xx-gpio",
312 .minimum_version_id
= 1,
313 .minimum_version_id_old
= 1,
314 .fields
= (VMStateField
[]) {
315 VMSTATE_INT32(lines
, PXA2xxGPIOInfo
),
316 VMSTATE_UINT32_ARRAY(ilevel
, PXA2xxGPIOInfo
, PXA2XX_GPIO_BANKS
),
317 VMSTATE_UINT32_ARRAY(olevel
, PXA2xxGPIOInfo
, PXA2XX_GPIO_BANKS
),
318 VMSTATE_UINT32_ARRAY(dir
, PXA2xxGPIOInfo
, PXA2XX_GPIO_BANKS
),
319 VMSTATE_UINT32_ARRAY(rising
, PXA2xxGPIOInfo
, PXA2XX_GPIO_BANKS
),
320 VMSTATE_UINT32_ARRAY(falling
, PXA2xxGPIOInfo
, PXA2XX_GPIO_BANKS
),
321 VMSTATE_UINT32_ARRAY(status
, PXA2xxGPIOInfo
, PXA2XX_GPIO_BANKS
),
322 VMSTATE_UINT32_ARRAY(gafr
, PXA2xxGPIOInfo
, PXA2XX_GPIO_BANKS
* 2),
323 VMSTATE_END_OF_LIST(),
327 static SysBusDeviceInfo pxa2xx_gpio_info
= {
328 .init
= pxa2xx_gpio_initfn
,
329 .qdev
.name
= "pxa2xx-gpio",
330 .qdev
.desc
= "PXA2xx GPIO controller",
331 .qdev
.size
= sizeof(PXA2xxGPIOInfo
),
332 .qdev
.props
= (Property
[]) {
333 DEFINE_PROP_INT32("lines", PXA2xxGPIOInfo
, lines
, 0),
334 DEFINE_PROP_INT32("ncpu", PXA2xxGPIOInfo
, ncpu
, 0),
335 DEFINE_PROP_END_OF_LIST(),
339 static void pxa2xx_gpio_register(void)
341 sysbus_register_withprop(&pxa2xx_gpio_info
);
343 device_init(pxa2xx_gpio_register
);