hw: Fix qemu_allocate_irqs() leaks
[qemu/ar7.git] / hw / pcmcia / pxa2xx.c
blob55e8a2a62eeb3b5b8c0576d63c6c5bbab0384bfd
1 /*
2 * Intel XScale PXA255/270 PC Card and CompactFlash Interface.
4 * Copyright (c) 2006 Openedhand Ltd.
5 * Written by Andrzej Zaborowski <balrog@zabor.org>
7 * This code is licensed under the GPLv2.
9 * Contributions after 2012-01-13 are licensed under the terms of the
10 * GNU GPL, version 2 or (at your option) any later version.
13 #include "hw/hw.h"
14 #include "hw/sysbus.h"
15 #include "hw/pcmcia.h"
16 #include "hw/arm/pxa.h"
18 #define TYPE_PXA2XX_PCMCIA "pxa2xx-pcmcia"
19 #define PXA2XX_PCMCIA(obj) \
20 OBJECT_CHECK(PXA2xxPCMCIAState, obj, TYPE_PXA2XX_PCMCIA)
22 struct PXA2xxPCMCIAState {
23 SysBusDevice parent_obj;
25 PCMCIASocket slot;
26 MemoryRegion container_mem;
27 MemoryRegion common_iomem;
28 MemoryRegion attr_iomem;
29 MemoryRegion iomem;
31 qemu_irq irq;
32 qemu_irq cd_irq;
34 PCMCIACardState *card;
37 static uint64_t pxa2xx_pcmcia_common_read(void *opaque,
38 hwaddr offset, unsigned size)
40 PXA2xxPCMCIAState *s = (PXA2xxPCMCIAState *) opaque;
41 PCMCIACardClass *pcc;
43 if (s->slot.attached) {
44 pcc = PCMCIA_CARD_GET_CLASS(s->card);
45 return pcc->common_read(s->card, offset);
48 return 0;
51 static void pxa2xx_pcmcia_common_write(void *opaque, hwaddr offset,
52 uint64_t value, unsigned size)
54 PXA2xxPCMCIAState *s = (PXA2xxPCMCIAState *) opaque;
55 PCMCIACardClass *pcc;
57 if (s->slot.attached) {
58 pcc = PCMCIA_CARD_GET_CLASS(s->card);
59 pcc->common_write(s->card, offset, value);
63 static uint64_t pxa2xx_pcmcia_attr_read(void *opaque,
64 hwaddr offset, unsigned size)
66 PXA2xxPCMCIAState *s = (PXA2xxPCMCIAState *) opaque;
67 PCMCIACardClass *pcc;
69 if (s->slot.attached) {
70 pcc = PCMCIA_CARD_GET_CLASS(s->card);
71 return pcc->attr_read(s->card, offset);
74 return 0;
77 static void pxa2xx_pcmcia_attr_write(void *opaque, hwaddr offset,
78 uint64_t value, unsigned size)
80 PXA2xxPCMCIAState *s = (PXA2xxPCMCIAState *) opaque;
81 PCMCIACardClass *pcc;
83 if (s->slot.attached) {
84 pcc = PCMCIA_CARD_GET_CLASS(s->card);
85 pcc->attr_write(s->card, offset, value);
89 static uint64_t pxa2xx_pcmcia_io_read(void *opaque,
90 hwaddr offset, unsigned size)
92 PXA2xxPCMCIAState *s = (PXA2xxPCMCIAState *) opaque;
93 PCMCIACardClass *pcc;
95 if (s->slot.attached) {
96 pcc = PCMCIA_CARD_GET_CLASS(s->card);
97 return pcc->io_read(s->card, offset);
100 return 0;
103 static void pxa2xx_pcmcia_io_write(void *opaque, hwaddr offset,
104 uint64_t value, unsigned size)
106 PXA2xxPCMCIAState *s = (PXA2xxPCMCIAState *) opaque;
107 PCMCIACardClass *pcc;
109 if (s->slot.attached) {
110 pcc = PCMCIA_CARD_GET_CLASS(s->card);
111 pcc->io_write(s->card, offset, value);
115 static const MemoryRegionOps pxa2xx_pcmcia_common_ops = {
116 .read = pxa2xx_pcmcia_common_read,
117 .write = pxa2xx_pcmcia_common_write,
118 .endianness = DEVICE_NATIVE_ENDIAN
121 static const MemoryRegionOps pxa2xx_pcmcia_attr_ops = {
122 .read = pxa2xx_pcmcia_attr_read,
123 .write = pxa2xx_pcmcia_attr_write,
124 .endianness = DEVICE_NATIVE_ENDIAN
127 static const MemoryRegionOps pxa2xx_pcmcia_io_ops = {
128 .read = pxa2xx_pcmcia_io_read,
129 .write = pxa2xx_pcmcia_io_write,
130 .endianness = DEVICE_NATIVE_ENDIAN
133 static void pxa2xx_pcmcia_set_irq(void *opaque, int line, int level)
135 PXA2xxPCMCIAState *s = (PXA2xxPCMCIAState *) opaque;
136 if (!s->irq)
137 return;
139 qemu_set_irq(s->irq, level);
142 PXA2xxPCMCIAState *pxa2xx_pcmcia_init(MemoryRegion *sysmem,
143 hwaddr base)
145 DeviceState *dev;
146 PXA2xxPCMCIAState *s;
148 dev = qdev_create(NULL, TYPE_PXA2XX_PCMCIA);
149 sysbus_mmio_map(SYS_BUS_DEVICE(dev), 0, base);
150 s = PXA2XX_PCMCIA(dev);
152 if (base == 0x30000000) {
153 s->slot.slot_string = "PXA PC Card Socket 1";
154 } else {
155 s->slot.slot_string = "PXA PC Card Socket 0";
158 qdev_init_nofail(dev);
160 return s;
163 static void pxa2xx_pcmcia_realize(DeviceState *dev, Error **errp)
165 PXA2xxPCMCIAState *s = PXA2XX_PCMCIA(dev);
167 pcmcia_socket_register(&s->slot);
170 static void pxa2xx_pcmcia_initfn(Object *obj)
172 SysBusDevice *sbd = SYS_BUS_DEVICE(obj);
173 PXA2xxPCMCIAState *s = PXA2XX_PCMCIA(obj);
175 memory_region_init(&s->container_mem, obj, "container", 0x10000000);
176 sysbus_init_mmio(sbd, &s->container_mem);
178 /* Socket I/O Memory Space */
179 memory_region_init_io(&s->iomem, NULL, &pxa2xx_pcmcia_io_ops, s,
180 "pxa2xx-pcmcia-io", 0x04000000);
181 memory_region_add_subregion(&s->container_mem, 0x00000000,
182 &s->iomem);
184 /* Then next 64 MB is reserved */
186 /* Socket Attribute Memory Space */
187 memory_region_init_io(&s->attr_iomem, NULL, &pxa2xx_pcmcia_attr_ops, s,
188 "pxa2xx-pcmcia-attribute", 0x04000000);
189 memory_region_add_subregion(&s->container_mem, 0x08000000,
190 &s->attr_iomem);
192 /* Socket Common Memory Space */
193 memory_region_init_io(&s->common_iomem, NULL, &pxa2xx_pcmcia_common_ops, s,
194 "pxa2xx-pcmcia-common", 0x04000000);
195 memory_region_add_subregion(&s->container_mem, 0x0c000000,
196 &s->common_iomem);
198 s->slot.irq = qemu_allocate_irq(pxa2xx_pcmcia_set_irq, s, 0);
200 object_property_add_link(obj, "card", TYPE_PCMCIA_CARD,
201 (Object **)&s->card,
202 NULL, /* read-only property */
203 0, NULL);
206 /* Insert a new card into a slot */
207 int pxa2xx_pcmcia_attach(void *opaque, PCMCIACardState *card)
209 PXA2xxPCMCIAState *s = (PXA2xxPCMCIAState *) opaque;
210 PCMCIACardClass *pcc;
212 if (s->slot.attached) {
213 return -EEXIST;
216 if (s->cd_irq) {
217 qemu_irq_raise(s->cd_irq);
220 s->card = card;
221 pcc = PCMCIA_CARD_GET_CLASS(s->card);
223 s->slot.attached = true;
224 s->card->slot = &s->slot;
225 pcc->attach(s->card);
227 return 0;
230 /* Eject card from the slot */
231 int pxa2xx_pcmcia_detach(void *opaque)
233 PXA2xxPCMCIAState *s = (PXA2xxPCMCIAState *) opaque;
234 PCMCIACardClass *pcc;
236 if (!s->slot.attached) {
237 return -ENOENT;
240 pcc = PCMCIA_CARD_GET_CLASS(s->card);
241 pcc->detach(s->card);
242 s->card->slot = NULL;
243 s->card = NULL;
245 s->slot.attached = false;
247 if (s->irq) {
248 qemu_irq_lower(s->irq);
250 if (s->cd_irq) {
251 qemu_irq_lower(s->cd_irq);
254 return 0;
257 /* Who to notify on card events */
258 void pxa2xx_pcmcia_set_irq_cb(void *opaque, qemu_irq irq, qemu_irq cd_irq)
260 PXA2xxPCMCIAState *s = (PXA2xxPCMCIAState *) opaque;
261 s->irq = irq;
262 s->cd_irq = cd_irq;
265 static void pxa2xx_pcmcia_class_init(ObjectClass *oc, void *data)
267 DeviceClass *dc = DEVICE_CLASS(oc);
269 dc->realize = pxa2xx_pcmcia_realize;
272 static const TypeInfo pxa2xx_pcmcia_type_info = {
273 .name = TYPE_PXA2XX_PCMCIA,
274 .parent = TYPE_SYS_BUS_DEVICE,
275 .instance_size = sizeof(PXA2xxPCMCIAState),
276 .instance_init = pxa2xx_pcmcia_initfn,
277 .class_init = pxa2xx_pcmcia_class_init,
280 static void pxa2xx_pcmcia_register_types(void)
282 type_register_static(&pxa2xx_pcmcia_type_info);
285 type_init(pxa2xx_pcmcia_register_types)