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"
15 #include "hw/sysbus.h"
16 #include "hw/pcmcia.h"
17 #include "hw/arm/pxa.h"
19 #define TYPE_PXA2XX_PCMCIA "pxa2xx-pcmcia"
20 #define PXA2XX_PCMCIA(obj) \
21 OBJECT_CHECK(PXA2xxPCMCIAState, obj, TYPE_PXA2XX_PCMCIA)
23 struct PXA2xxPCMCIAState
{
24 SysBusDevice parent_obj
;
27 MemoryRegion container_mem
;
28 MemoryRegion common_iomem
;
29 MemoryRegion attr_iomem
;
35 PCMCIACardState
*card
;
38 static uint64_t pxa2xx_pcmcia_common_read(void *opaque
,
39 hwaddr offset
, unsigned size
)
41 PXA2xxPCMCIAState
*s
= (PXA2xxPCMCIAState
*) opaque
;
44 if (s
->slot
.attached
) {
45 pcc
= PCMCIA_CARD_GET_CLASS(s
->card
);
46 return pcc
->common_read(s
->card
, offset
);
52 static void pxa2xx_pcmcia_common_write(void *opaque
, hwaddr offset
,
53 uint64_t value
, unsigned size
)
55 PXA2xxPCMCIAState
*s
= (PXA2xxPCMCIAState
*) opaque
;
58 if (s
->slot
.attached
) {
59 pcc
= PCMCIA_CARD_GET_CLASS(s
->card
);
60 pcc
->common_write(s
->card
, offset
, value
);
64 static uint64_t pxa2xx_pcmcia_attr_read(void *opaque
,
65 hwaddr offset
, unsigned size
)
67 PXA2xxPCMCIAState
*s
= (PXA2xxPCMCIAState
*) opaque
;
70 if (s
->slot
.attached
) {
71 pcc
= PCMCIA_CARD_GET_CLASS(s
->card
);
72 return pcc
->attr_read(s
->card
, offset
);
78 static void pxa2xx_pcmcia_attr_write(void *opaque
, hwaddr offset
,
79 uint64_t value
, unsigned size
)
81 PXA2xxPCMCIAState
*s
= (PXA2xxPCMCIAState
*) opaque
;
84 if (s
->slot
.attached
) {
85 pcc
= PCMCIA_CARD_GET_CLASS(s
->card
);
86 pcc
->attr_write(s
->card
, offset
, value
);
90 static uint64_t pxa2xx_pcmcia_io_read(void *opaque
,
91 hwaddr offset
, unsigned size
)
93 PXA2xxPCMCIAState
*s
= (PXA2xxPCMCIAState
*) opaque
;
96 if (s
->slot
.attached
) {
97 pcc
= PCMCIA_CARD_GET_CLASS(s
->card
);
98 return pcc
->io_read(s
->card
, offset
);
104 static void pxa2xx_pcmcia_io_write(void *opaque
, hwaddr offset
,
105 uint64_t value
, unsigned size
)
107 PXA2xxPCMCIAState
*s
= (PXA2xxPCMCIAState
*) opaque
;
108 PCMCIACardClass
*pcc
;
110 if (s
->slot
.attached
) {
111 pcc
= PCMCIA_CARD_GET_CLASS(s
->card
);
112 pcc
->io_write(s
->card
, offset
, value
);
116 static const MemoryRegionOps pxa2xx_pcmcia_common_ops
= {
117 .read
= pxa2xx_pcmcia_common_read
,
118 .write
= pxa2xx_pcmcia_common_write
,
119 .endianness
= DEVICE_NATIVE_ENDIAN
122 static const MemoryRegionOps pxa2xx_pcmcia_attr_ops
= {
123 .read
= pxa2xx_pcmcia_attr_read
,
124 .write
= pxa2xx_pcmcia_attr_write
,
125 .endianness
= DEVICE_NATIVE_ENDIAN
128 static const MemoryRegionOps pxa2xx_pcmcia_io_ops
= {
129 .read
= pxa2xx_pcmcia_io_read
,
130 .write
= pxa2xx_pcmcia_io_write
,
131 .endianness
= DEVICE_NATIVE_ENDIAN
134 static void pxa2xx_pcmcia_set_irq(void *opaque
, int line
, int level
)
136 PXA2xxPCMCIAState
*s
= (PXA2xxPCMCIAState
*) opaque
;
140 qemu_set_irq(s
->irq
, level
);
143 PXA2xxPCMCIAState
*pxa2xx_pcmcia_init(MemoryRegion
*sysmem
,
147 PXA2xxPCMCIAState
*s
;
149 dev
= qdev_create(NULL
, TYPE_PXA2XX_PCMCIA
);
150 sysbus_mmio_map(SYS_BUS_DEVICE(dev
), 0, base
);
151 s
= PXA2XX_PCMCIA(dev
);
153 qdev_init_nofail(dev
);
158 static void pxa2xx_pcmcia_initfn(Object
*obj
)
160 SysBusDevice
*sbd
= SYS_BUS_DEVICE(obj
);
161 PXA2xxPCMCIAState
*s
= PXA2XX_PCMCIA(obj
);
163 memory_region_init(&s
->container_mem
, obj
, "container", 0x10000000);
164 sysbus_init_mmio(sbd
, &s
->container_mem
);
166 /* Socket I/O Memory Space */
167 memory_region_init_io(&s
->iomem
, obj
, &pxa2xx_pcmcia_io_ops
, s
,
168 "pxa2xx-pcmcia-io", 0x04000000);
169 memory_region_add_subregion(&s
->container_mem
, 0x00000000,
172 /* Then next 64 MB is reserved */
174 /* Socket Attribute Memory Space */
175 memory_region_init_io(&s
->attr_iomem
, obj
, &pxa2xx_pcmcia_attr_ops
, s
,
176 "pxa2xx-pcmcia-attribute", 0x04000000);
177 memory_region_add_subregion(&s
->container_mem
, 0x08000000,
180 /* Socket Common Memory Space */
181 memory_region_init_io(&s
->common_iomem
, obj
, &pxa2xx_pcmcia_common_ops
, s
,
182 "pxa2xx-pcmcia-common", 0x04000000);
183 memory_region_add_subregion(&s
->container_mem
, 0x0c000000,
186 s
->slot
.irq
= qemu_allocate_irq(pxa2xx_pcmcia_set_irq
, s
, 0);
188 object_property_add_link(obj
, "card", TYPE_PCMCIA_CARD
,
190 NULL
, /* read-only property */
194 /* Insert a new card into a slot */
195 int pxa2xx_pcmcia_attach(void *opaque
, PCMCIACardState
*card
)
197 PXA2xxPCMCIAState
*s
= (PXA2xxPCMCIAState
*) opaque
;
198 PCMCIACardClass
*pcc
;
200 if (s
->slot
.attached
) {
205 qemu_irq_raise(s
->cd_irq
);
209 pcc
= PCMCIA_CARD_GET_CLASS(s
->card
);
211 s
->slot
.attached
= true;
212 s
->card
->slot
= &s
->slot
;
213 pcc
->attach(s
->card
);
218 /* Eject card from the slot */
219 int pxa2xx_pcmcia_detach(void *opaque
)
221 PXA2xxPCMCIAState
*s
= (PXA2xxPCMCIAState
*) opaque
;
222 PCMCIACardClass
*pcc
;
224 if (!s
->slot
.attached
) {
228 pcc
= PCMCIA_CARD_GET_CLASS(s
->card
);
229 pcc
->detach(s
->card
);
230 s
->card
->slot
= NULL
;
233 s
->slot
.attached
= false;
236 qemu_irq_lower(s
->irq
);
239 qemu_irq_lower(s
->cd_irq
);
245 /* Who to notify on card events */
246 void pxa2xx_pcmcia_set_irq_cb(void *opaque
, qemu_irq irq
, qemu_irq cd_irq
)
248 PXA2xxPCMCIAState
*s
= (PXA2xxPCMCIAState
*) opaque
;
253 static const TypeInfo pxa2xx_pcmcia_type_info
= {
254 .name
= TYPE_PXA2XX_PCMCIA
,
255 .parent
= TYPE_SYS_BUS_DEVICE
,
256 .instance_size
= sizeof(PXA2xxPCMCIAState
),
257 .instance_init
= pxa2xx_pcmcia_initfn
,
260 static void pxa2xx_pcmcia_register_types(void)
262 type_register_static(&pxa2xx_pcmcia_type_info
);
265 type_init(pxa2xx_pcmcia_register_types
)