sd/milkymist-memcard: Fix format string
[qemu/armbru.git] / hw / pcmcia / pxa2xx.c
blob5f4bf22a90b4fb4c0144a2457c400d66c3f96968
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 "qemu/osdep.h"
14 #include "hw/irq.h"
15 #include "hw/sysbus.h"
16 #include "qapi/error.h"
17 #include "qemu/module.h"
18 #include "hw/pcmcia.h"
19 #include "hw/arm/pxa.h"
21 #define TYPE_PXA2XX_PCMCIA "pxa2xx-pcmcia"
22 #define PXA2XX_PCMCIA(obj) \
23 OBJECT_CHECK(PXA2xxPCMCIAState, obj, TYPE_PXA2XX_PCMCIA)
25 struct PXA2xxPCMCIAState {
26 SysBusDevice parent_obj;
28 PCMCIASocket slot;
29 MemoryRegion container_mem;
30 MemoryRegion common_iomem;
31 MemoryRegion attr_iomem;
32 MemoryRegion iomem;
34 qemu_irq irq;
35 qemu_irq cd_irq;
37 PCMCIACardState *card;
40 static uint64_t pxa2xx_pcmcia_common_read(void *opaque,
41 hwaddr offset, unsigned size)
43 PXA2xxPCMCIAState *s = (PXA2xxPCMCIAState *) opaque;
44 PCMCIACardClass *pcc;
46 if (s->slot.attached) {
47 pcc = PCMCIA_CARD_GET_CLASS(s->card);
48 return pcc->common_read(s->card, offset);
51 return 0;
54 static void pxa2xx_pcmcia_common_write(void *opaque, hwaddr offset,
55 uint64_t value, unsigned size)
57 PXA2xxPCMCIAState *s = (PXA2xxPCMCIAState *) opaque;
58 PCMCIACardClass *pcc;
60 if (s->slot.attached) {
61 pcc = PCMCIA_CARD_GET_CLASS(s->card);
62 pcc->common_write(s->card, offset, value);
66 static uint64_t pxa2xx_pcmcia_attr_read(void *opaque,
67 hwaddr offset, unsigned size)
69 PXA2xxPCMCIAState *s = (PXA2xxPCMCIAState *) opaque;
70 PCMCIACardClass *pcc;
72 if (s->slot.attached) {
73 pcc = PCMCIA_CARD_GET_CLASS(s->card);
74 return pcc->attr_read(s->card, offset);
77 return 0;
80 static void pxa2xx_pcmcia_attr_write(void *opaque, hwaddr offset,
81 uint64_t value, unsigned size)
83 PXA2xxPCMCIAState *s = (PXA2xxPCMCIAState *) opaque;
84 PCMCIACardClass *pcc;
86 if (s->slot.attached) {
87 pcc = PCMCIA_CARD_GET_CLASS(s->card);
88 pcc->attr_write(s->card, offset, value);
92 static uint64_t pxa2xx_pcmcia_io_read(void *opaque,
93 hwaddr offset, unsigned size)
95 PXA2xxPCMCIAState *s = (PXA2xxPCMCIAState *) opaque;
96 PCMCIACardClass *pcc;
98 if (s->slot.attached) {
99 pcc = PCMCIA_CARD_GET_CLASS(s->card);
100 return pcc->io_read(s->card, offset);
103 return 0;
106 static void pxa2xx_pcmcia_io_write(void *opaque, hwaddr offset,
107 uint64_t value, unsigned size)
109 PXA2xxPCMCIAState *s = (PXA2xxPCMCIAState *) opaque;
110 PCMCIACardClass *pcc;
112 if (s->slot.attached) {
113 pcc = PCMCIA_CARD_GET_CLASS(s->card);
114 pcc->io_write(s->card, offset, value);
118 static const MemoryRegionOps pxa2xx_pcmcia_common_ops = {
119 .read = pxa2xx_pcmcia_common_read,
120 .write = pxa2xx_pcmcia_common_write,
121 .endianness = DEVICE_NATIVE_ENDIAN
124 static const MemoryRegionOps pxa2xx_pcmcia_attr_ops = {
125 .read = pxa2xx_pcmcia_attr_read,
126 .write = pxa2xx_pcmcia_attr_write,
127 .endianness = DEVICE_NATIVE_ENDIAN
130 static const MemoryRegionOps pxa2xx_pcmcia_io_ops = {
131 .read = pxa2xx_pcmcia_io_read,
132 .write = pxa2xx_pcmcia_io_write,
133 .endianness = DEVICE_NATIVE_ENDIAN
136 static void pxa2xx_pcmcia_set_irq(void *opaque, int line, int level)
138 PXA2xxPCMCIAState *s = (PXA2xxPCMCIAState *) opaque;
139 if (!s->irq)
140 return;
142 qemu_set_irq(s->irq, level);
145 PXA2xxPCMCIAState *pxa2xx_pcmcia_init(MemoryRegion *sysmem,
146 hwaddr base)
148 DeviceState *dev;
149 PXA2xxPCMCIAState *s;
151 dev = qdev_new(TYPE_PXA2XX_PCMCIA);
152 sysbus_mmio_map(SYS_BUS_DEVICE(dev), 0, base);
153 s = PXA2XX_PCMCIA(dev);
155 sysbus_realize_and_unref(SYS_BUS_DEVICE(dev), &error_fatal);
157 return s;
160 static void pxa2xx_pcmcia_initfn(Object *obj)
162 SysBusDevice *sbd = SYS_BUS_DEVICE(obj);
163 PXA2xxPCMCIAState *s = PXA2XX_PCMCIA(obj);
165 memory_region_init(&s->container_mem, obj, "container", 0x10000000);
166 sysbus_init_mmio(sbd, &s->container_mem);
168 /* Socket I/O Memory Space */
169 memory_region_init_io(&s->iomem, obj, &pxa2xx_pcmcia_io_ops, s,
170 "pxa2xx-pcmcia-io", 0x04000000);
171 memory_region_add_subregion(&s->container_mem, 0x00000000,
172 &s->iomem);
174 /* Then next 64 MB is reserved */
176 /* Socket Attribute Memory Space */
177 memory_region_init_io(&s->attr_iomem, obj, &pxa2xx_pcmcia_attr_ops, s,
178 "pxa2xx-pcmcia-attribute", 0x04000000);
179 memory_region_add_subregion(&s->container_mem, 0x08000000,
180 &s->attr_iomem);
182 /* Socket Common Memory Space */
183 memory_region_init_io(&s->common_iomem, obj, &pxa2xx_pcmcia_common_ops, s,
184 "pxa2xx-pcmcia-common", 0x04000000);
185 memory_region_add_subregion(&s->container_mem, 0x0c000000,
186 &s->common_iomem);
188 s->slot.irq = qemu_allocate_irq(pxa2xx_pcmcia_set_irq, s, 0);
190 object_property_add_link(obj, "card", TYPE_PCMCIA_CARD,
191 (Object **)&s->card,
192 NULL, /* read-only property */
196 /* Insert a new card into a slot */
197 int pxa2xx_pcmcia_attach(void *opaque, PCMCIACardState *card)
199 PXA2xxPCMCIAState *s = (PXA2xxPCMCIAState *) opaque;
200 PCMCIACardClass *pcc;
202 if (s->slot.attached) {
203 return -EEXIST;
206 if (s->cd_irq) {
207 qemu_irq_raise(s->cd_irq);
210 s->card = card;
211 pcc = PCMCIA_CARD_GET_CLASS(s->card);
213 s->slot.attached = true;
214 s->card->slot = &s->slot;
215 pcc->attach(s->card);
217 return 0;
220 /* Eject card from the slot */
221 int pxa2xx_pcmcia_detach(void *opaque)
223 PXA2xxPCMCIAState *s = (PXA2xxPCMCIAState *) opaque;
224 PCMCIACardClass *pcc;
226 if (!s->slot.attached) {
227 return -ENOENT;
230 pcc = PCMCIA_CARD_GET_CLASS(s->card);
231 pcc->detach(s->card);
232 s->card->slot = NULL;
233 s->card = NULL;
235 s->slot.attached = false;
237 if (s->irq) {
238 qemu_irq_lower(s->irq);
240 if (s->cd_irq) {
241 qemu_irq_lower(s->cd_irq);
244 return 0;
247 /* Who to notify on card events */
248 void pxa2xx_pcmcia_set_irq_cb(void *opaque, qemu_irq irq, qemu_irq cd_irq)
250 PXA2xxPCMCIAState *s = (PXA2xxPCMCIAState *) opaque;
251 s->irq = irq;
252 s->cd_irq = cd_irq;
255 static const TypeInfo pxa2xx_pcmcia_type_info = {
256 .name = TYPE_PXA2XX_PCMCIA,
257 .parent = TYPE_SYS_BUS_DEVICE,
258 .instance_size = sizeof(PXA2xxPCMCIAState),
259 .instance_init = pxa2xx_pcmcia_initfn,
262 static void pxa2xx_pcmcia_register_types(void)
264 type_register_static(&pxa2xx_pcmcia_type_info);
267 type_init(pxa2xx_pcmcia_register_types)