2 * Intel XScale PXA255/270 MultiMediaCard/SD/SDIO Controller emulation.
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/arm/pxa.h"
19 #include "hw/qdev-properties.h"
20 #include "qemu/error-report.h"
22 #define TYPE_PXA2XX_MMCI "pxa2xx-mmci"
23 #define PXA2XX_MMCI(obj) OBJECT_CHECK(PXA2xxMMCIState, (obj), TYPE_PXA2XX_MMCI)
25 #define TYPE_PXA2XX_MMCI_BUS "pxa2xx-mmci-bus"
26 #define PXA2XX_MMCI_BUS(obj) OBJECT_CHECK(SDBus, (obj), TYPE_PXA2XX_MMCI_BUS)
28 struct PXA2xxMMCIState
{
29 SysBusDevice parent_obj
;
62 uint16_t resp_fifo
[9];
68 static bool pxa2xx_mmci_vmstate_validate(void *opaque
, int version_id
)
70 PXA2xxMMCIState
*s
= opaque
;
72 return s
->tx_start
< ARRAY_SIZE(s
->tx_fifo
)
73 && s
->rx_start
< ARRAY_SIZE(s
->rx_fifo
)
74 && s
->tx_len
<= ARRAY_SIZE(s
->tx_fifo
)
75 && s
->rx_len
<= ARRAY_SIZE(s
->rx_fifo
)
76 && s
->resp_len
<= ARRAY_SIZE(s
->resp_fifo
);
80 static const VMStateDescription vmstate_pxa2xx_mmci
= {
81 .name
= "pxa2xx-mmci",
83 .minimum_version_id
= 2,
84 .fields
= (VMStateField
[]) {
85 VMSTATE_UINT32(status
, PXA2xxMMCIState
),
86 VMSTATE_UINT32(clkrt
, PXA2xxMMCIState
),
87 VMSTATE_UINT32(spi
, PXA2xxMMCIState
),
88 VMSTATE_UINT32(cmdat
, PXA2xxMMCIState
),
89 VMSTATE_UINT32(resp_tout
, PXA2xxMMCIState
),
90 VMSTATE_UINT32(read_tout
, PXA2xxMMCIState
),
91 VMSTATE_INT32(blklen
, PXA2xxMMCIState
),
92 VMSTATE_INT32(numblk
, PXA2xxMMCIState
),
93 VMSTATE_UINT32(intmask
, PXA2xxMMCIState
),
94 VMSTATE_UINT32(intreq
, PXA2xxMMCIState
),
95 VMSTATE_INT32(cmd
, PXA2xxMMCIState
),
96 VMSTATE_UINT32(arg
, PXA2xxMMCIState
),
97 VMSTATE_INT32(cmdreq
, PXA2xxMMCIState
),
98 VMSTATE_INT32(active
, PXA2xxMMCIState
),
99 VMSTATE_INT32(bytesleft
, PXA2xxMMCIState
),
100 VMSTATE_UINT32(tx_start
, PXA2xxMMCIState
),
101 VMSTATE_UINT32(tx_len
, PXA2xxMMCIState
),
102 VMSTATE_UINT32(rx_start
, PXA2xxMMCIState
),
103 VMSTATE_UINT32(rx_len
, PXA2xxMMCIState
),
104 VMSTATE_UINT32(resp_len
, PXA2xxMMCIState
),
105 VMSTATE_VALIDATE("fifo size incorrect", pxa2xx_mmci_vmstate_validate
),
106 VMSTATE_UINT8_ARRAY(tx_fifo
, PXA2xxMMCIState
, 64),
107 VMSTATE_UINT8_ARRAY(rx_fifo
, PXA2xxMMCIState
, 32),
108 VMSTATE_UINT16_ARRAY(resp_fifo
, PXA2xxMMCIState
, 9),
109 VMSTATE_END_OF_LIST()
113 #define MMC_STRPCL 0x00 /* MMC Clock Start/Stop register */
114 #define MMC_STAT 0x04 /* MMC Status register */
115 #define MMC_CLKRT 0x08 /* MMC Clock Rate register */
116 #define MMC_SPI 0x0c /* MMC SPI Mode register */
117 #define MMC_CMDAT 0x10 /* MMC Command/Data register */
118 #define MMC_RESTO 0x14 /* MMC Response Time-Out register */
119 #define MMC_RDTO 0x18 /* MMC Read Time-Out register */
120 #define MMC_BLKLEN 0x1c /* MMC Block Length register */
121 #define MMC_NUMBLK 0x20 /* MMC Number of Blocks register */
122 #define MMC_PRTBUF 0x24 /* MMC Buffer Partly Full register */
123 #define MMC_I_MASK 0x28 /* MMC Interrupt Mask register */
124 #define MMC_I_REG 0x2c /* MMC Interrupt Request register */
125 #define MMC_CMD 0x30 /* MMC Command register */
126 #define MMC_ARGH 0x34 /* MMC Argument High register */
127 #define MMC_ARGL 0x38 /* MMC Argument Low register */
128 #define MMC_RES 0x3c /* MMC Response FIFO */
129 #define MMC_RXFIFO 0x40 /* MMC Receive FIFO */
130 #define MMC_TXFIFO 0x44 /* MMC Transmit FIFO */
131 #define MMC_RDWAIT 0x48 /* MMC RD_WAIT register */
132 #define MMC_BLKS_REM 0x4c /* MMC Blocks Remaining register */
135 #define STRPCL_STOP_CLK (1 << 0)
136 #define STRPCL_STRT_CLK (1 << 1)
137 #define STAT_TOUT_RES (1 << 1)
138 #define STAT_CLK_EN (1 << 8)
139 #define STAT_DATA_DONE (1 << 11)
140 #define STAT_PRG_DONE (1 << 12)
141 #define STAT_END_CMDRES (1 << 13)
142 #define SPI_SPI_MODE (1 << 0)
143 #define CMDAT_RES_TYPE (3 << 0)
144 #define CMDAT_DATA_EN (1 << 2)
145 #define CMDAT_WR_RD (1 << 3)
146 #define CMDAT_DMA_EN (1 << 7)
147 #define CMDAT_STOP_TRAN (1 << 10)
148 #define INT_DATA_DONE (1 << 0)
149 #define INT_PRG_DONE (1 << 1)
150 #define INT_END_CMD (1 << 2)
151 #define INT_STOP_CMD (1 << 3)
152 #define INT_CLK_OFF (1 << 4)
153 #define INT_RXFIFO_REQ (1 << 5)
154 #define INT_TXFIFO_REQ (1 << 6)
155 #define INT_TINT (1 << 7)
156 #define INT_DAT_ERR (1 << 8)
157 #define INT_RES_ERR (1 << 9)
158 #define INT_RD_STALLED (1 << 10)
159 #define INT_SDIO_INT (1 << 11)
160 #define INT_SDIO_SACK (1 << 12)
161 #define PRTBUF_PRT_BUF (1 << 0)
163 /* Route internal interrupt lines to the global IC and DMA */
164 static void pxa2xx_mmci_int_update(PXA2xxMMCIState
*s
)
166 uint32_t mask
= s
->intmask
;
167 if (s
->cmdat
& CMDAT_DMA_EN
) {
168 mask
|= INT_RXFIFO_REQ
| INT_TXFIFO_REQ
;
170 qemu_set_irq(s
->rx_dma
, !!(s
->intreq
& INT_RXFIFO_REQ
));
171 qemu_set_irq(s
->tx_dma
, !!(s
->intreq
& INT_TXFIFO_REQ
));
174 qemu_set_irq(s
->irq
, !!(s
->intreq
& ~mask
));
177 static void pxa2xx_mmci_fifo_update(PXA2xxMMCIState
*s
)
182 if (s
->cmdat
& CMDAT_WR_RD
) {
183 while (s
->bytesleft
&& s
->tx_len
) {
184 sdbus_write_data(&s
->sdbus
, s
->tx_fifo
[s
->tx_start
++]);
190 s
->intreq
|= INT_TXFIFO_REQ
;
192 while (s
->bytesleft
&& s
->rx_len
< 32) {
193 s
->rx_fifo
[(s
->rx_start
+ (s
->rx_len
++)) & 0x1f] =
194 sdbus_read_data(&s
->sdbus
);
196 s
->intreq
|= INT_RXFIFO_REQ
;
201 s
->intreq
|= INT_DATA_DONE
;
202 s
->status
|= STAT_DATA_DONE
;
204 if (s
->cmdat
& CMDAT_WR_RD
) {
205 s
->intreq
|= INT_PRG_DONE
;
206 s
->status
|= STAT_PRG_DONE
;
210 pxa2xx_mmci_int_update(s
);
213 static void pxa2xx_mmci_wakequeues(PXA2xxMMCIState
*s
)
217 uint8_t response
[16];
224 request
.cmd
= s
->cmd
;
225 request
.arg
= s
->arg
;
226 request
.crc
= 0; /* FIXME */
228 rsplen
= sdbus_do_command(&s
->sdbus
, &request
, response
);
229 s
->intreq
|= INT_END_CMD
;
231 memset(s
->resp_fifo
, 0, sizeof(s
->resp_fifo
));
232 switch (s
->cmdat
& CMDAT_RES_TYPE
) {
233 #define PXAMMCI_RESP(wd, value0, value1) \
234 s->resp_fifo[(wd) + 0] |= (value0); \
235 s->resp_fifo[(wd) + 1] |= (value1) << 8;
236 case 0: /* No response */
239 case 1: /* R1, R4, R5 or R6 */
255 for (i
= 0; rsplen
> 0; i
++, rsplen
-= 2) {
256 PXAMMCI_RESP(i
, response
[i
* 2], response
[i
* 2 + 1]);
258 s
->status
|= STAT_END_CMDRES
;
260 if (!(s
->cmdat
& CMDAT_DATA_EN
))
263 s
->bytesleft
= s
->numblk
* s
->blklen
;
270 s
->status
|= STAT_TOUT_RES
;
274 pxa2xx_mmci_fifo_update(s
);
277 static uint64_t pxa2xx_mmci_read(void *opaque
, hwaddr offset
, unsigned size
)
279 PXA2xxMMCIState
*s
= (PXA2xxMMCIState
*) opaque
;
308 return s
->cmd
| 0x40;
312 return s
->arg
& 0xffff;
315 return s
->resp_fifo
[s
->resp_len
++];
319 while (size
-- && s
->rx_len
) {
320 ret
|= s
->rx_fifo
[s
->rx_start
++] << (size
<< 3);
324 s
->intreq
&= ~INT_RXFIFO_REQ
;
325 pxa2xx_mmci_fifo_update(s
);
332 hw_error("%s: Bad offset " REG_FMT
"\n", __FUNCTION__
, offset
);
338 static void pxa2xx_mmci_write(void *opaque
,
339 hwaddr offset
, uint64_t value
, unsigned size
)
341 PXA2xxMMCIState
*s
= (PXA2xxMMCIState
*) opaque
;
345 if (value
& STRPCL_STRT_CLK
) {
346 s
->status
|= STAT_CLK_EN
;
347 s
->intreq
&= ~INT_CLK_OFF
;
349 if (s
->cmdreq
&& !(s
->cmdat
& CMDAT_STOP_TRAN
)) {
350 s
->status
&= STAT_CLK_EN
;
351 pxa2xx_mmci_wakequeues(s
);
355 if (value
& STRPCL_STOP_CLK
) {
356 s
->status
&= ~STAT_CLK_EN
;
357 s
->intreq
|= INT_CLK_OFF
;
361 pxa2xx_mmci_int_update(s
);
365 s
->clkrt
= value
& 7;
369 s
->spi
= value
& 0xf;
370 if (value
& SPI_SPI_MODE
)
371 printf("%s: attempted to use card in SPI mode\n", __FUNCTION__
);
375 s
->cmdat
= value
& 0x3dff;
378 if (!(value
& CMDAT_STOP_TRAN
)) {
379 s
->status
&= STAT_CLK_EN
;
381 if (s
->status
& STAT_CLK_EN
)
382 pxa2xx_mmci_wakequeues(s
);
385 pxa2xx_mmci_int_update(s
);
389 s
->resp_tout
= value
& 0x7f;
393 s
->read_tout
= value
& 0xffff;
397 s
->blklen
= value
& 0xfff;
401 s
->numblk
= value
& 0xffff;
405 if (value
& PRTBUF_PRT_BUF
) {
409 pxa2xx_mmci_fifo_update(s
);
413 s
->intmask
= value
& 0x1fff;
414 pxa2xx_mmci_int_update(s
);
418 s
->cmd
= value
& 0x3f;
422 s
->arg
&= 0x0000ffff;
423 s
->arg
|= value
<< 16;
427 s
->arg
&= 0xffff0000;
428 s
->arg
|= value
& 0x0000ffff;
432 while (size
-- && s
->tx_len
< 0x20)
433 s
->tx_fifo
[(s
->tx_start
+ (s
->tx_len
++)) & 0x1f] =
434 (value
>> (size
<< 3)) & 0xff;
435 s
->intreq
&= ~INT_TXFIFO_REQ
;
436 pxa2xx_mmci_fifo_update(s
);
444 hw_error("%s: Bad offset " REG_FMT
"\n", __FUNCTION__
, offset
);
448 static const MemoryRegionOps pxa2xx_mmci_ops
= {
449 .read
= pxa2xx_mmci_read
,
450 .write
= pxa2xx_mmci_write
,
451 .endianness
= DEVICE_NATIVE_ENDIAN
,
454 PXA2xxMMCIState
*pxa2xx_mmci_init(MemoryRegion
*sysmem
,
456 BlockBackend
*blk
, qemu_irq irq
,
457 qemu_irq rx_dma
, qemu_irq tx_dma
)
459 DeviceState
*dev
, *carddev
;
464 dev
= qdev_create(NULL
, TYPE_PXA2XX_MMCI
);
465 s
= PXA2XX_MMCI(dev
);
466 sbd
= SYS_BUS_DEVICE(dev
);
467 sysbus_mmio_map(sbd
, 0, base
);
468 sysbus_connect_irq(sbd
, 0, irq
);
469 qdev_connect_gpio_out_named(dev
, "rx-dma", 0, rx_dma
);
470 qdev_connect_gpio_out_named(dev
, "tx-dma", 0, tx_dma
);
472 /* Create and plug in the sd card */
473 carddev
= qdev_create(qdev_get_child_bus(dev
, "sd-bus"), TYPE_SD_CARD
);
474 qdev_prop_set_drive(carddev
, "drive", blk
, &err
);
476 error_report("failed to init SD card: %s", error_get_pretty(err
));
479 object_property_set_bool(OBJECT(carddev
), true, "realized", &err
);
481 error_report("failed to init SD card: %s", error_get_pretty(err
));
488 static void pxa2xx_mmci_set_inserted(DeviceState
*dev
, bool inserted
)
490 PXA2xxMMCIState
*s
= PXA2XX_MMCI(dev
);
492 qemu_set_irq(s
->inserted
, inserted
);
495 static void pxa2xx_mmci_set_readonly(DeviceState
*dev
, bool readonly
)
497 PXA2xxMMCIState
*s
= PXA2XX_MMCI(dev
);
499 qemu_set_irq(s
->readonly
, readonly
);
502 void pxa2xx_mmci_handlers(PXA2xxMMCIState
*s
, qemu_irq readonly
,
503 qemu_irq coverswitch
)
505 DeviceState
*dev
= DEVICE(s
);
507 s
->readonly
= readonly
;
508 s
->inserted
= coverswitch
;
510 pxa2xx_mmci_set_inserted(dev
, sdbus_get_inserted(&s
->sdbus
));
511 pxa2xx_mmci_set_readonly(dev
, sdbus_get_readonly(&s
->sdbus
));
514 static void pxa2xx_mmci_reset(DeviceState
*d
)
516 PXA2xxMMCIState
*s
= PXA2XX_MMCI(d
);
538 memset(s
->tx_fifo
, 0, sizeof(s
->tx_fifo
));
539 memset(s
->rx_fifo
, 0, sizeof(s
->rx_fifo
));
540 memset(s
->resp_fifo
, 0, sizeof(s
->resp_fifo
));
543 static void pxa2xx_mmci_instance_init(Object
*obj
)
545 PXA2xxMMCIState
*s
= PXA2XX_MMCI(obj
);
546 SysBusDevice
*sbd
= SYS_BUS_DEVICE(obj
);
547 DeviceState
*dev
= DEVICE(obj
);
549 memory_region_init_io(&s
->iomem
, obj
, &pxa2xx_mmci_ops
, s
,
550 "pxa2xx-mmci", 0x00100000);
551 sysbus_init_mmio(sbd
, &s
->iomem
);
552 sysbus_init_irq(sbd
, &s
->irq
);
553 qdev_init_gpio_out_named(dev
, &s
->rx_dma
, "rx-dma", 1);
554 qdev_init_gpio_out_named(dev
, &s
->tx_dma
, "tx-dma", 1);
556 qbus_create_inplace(&s
->sdbus
, sizeof(s
->sdbus
),
557 TYPE_PXA2XX_MMCI_BUS
, DEVICE(obj
), "sd-bus");
560 static void pxa2xx_mmci_class_init(ObjectClass
*klass
, void *data
)
562 DeviceClass
*dc
= DEVICE_CLASS(klass
);
564 dc
->vmsd
= &vmstate_pxa2xx_mmci
;
565 dc
->reset
= pxa2xx_mmci_reset
;
568 static void pxa2xx_mmci_bus_class_init(ObjectClass
*klass
, void *data
)
570 SDBusClass
*sbc
= SD_BUS_CLASS(klass
);
572 sbc
->set_inserted
= pxa2xx_mmci_set_inserted
;
573 sbc
->set_readonly
= pxa2xx_mmci_set_readonly
;
576 static const TypeInfo pxa2xx_mmci_info
= {
577 .name
= TYPE_PXA2XX_MMCI
,
578 .parent
= TYPE_SYS_BUS_DEVICE
,
579 .instance_size
= sizeof(PXA2xxMMCIState
),
580 .instance_init
= pxa2xx_mmci_instance_init
,
581 .class_init
= pxa2xx_mmci_class_init
,
584 static const TypeInfo pxa2xx_mmci_bus_info
= {
585 .name
= TYPE_PXA2XX_MMCI_BUS
,
586 .parent
= TYPE_SD_BUS
,
587 .instance_size
= sizeof(SDBus
),
588 .class_init
= pxa2xx_mmci_bus_class_init
,
591 static void pxa2xx_mmci_register_types(void)
593 type_register_static(&pxa2xx_mmci_info
);
594 type_register_static(&pxa2xx_mmci_bus_info
);
597 type_init(pxa2xx_mmci_register_types
)