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"
14 #include "qapi/error.h"
16 #include "hw/sysbus.h"
17 #include "hw/arm/pxa.h"
20 #include "hw/qdev-properties.h"
21 #include "qemu/error-report.h"
23 #include "qemu/module.h"
26 #define TYPE_PXA2XX_MMCI "pxa2xx-mmci"
27 #define PXA2XX_MMCI(obj) OBJECT_CHECK(PXA2xxMMCIState, (obj), TYPE_PXA2XX_MMCI)
29 #define TYPE_PXA2XX_MMCI_BUS "pxa2xx-mmci-bus"
30 #define PXA2XX_MMCI_BUS(obj) OBJECT_CHECK(SDBus, (obj), TYPE_PXA2XX_MMCI_BUS)
32 struct PXA2xxMMCIState
{
33 SysBusDevice parent_obj
;
66 uint16_t resp_fifo
[9];
72 static bool pxa2xx_mmci_vmstate_validate(void *opaque
, int version_id
)
74 PXA2xxMMCIState
*s
= opaque
;
76 return s
->tx_start
< ARRAY_SIZE(s
->tx_fifo
)
77 && s
->rx_start
< ARRAY_SIZE(s
->rx_fifo
)
78 && s
->tx_len
<= ARRAY_SIZE(s
->tx_fifo
)
79 && s
->rx_len
<= ARRAY_SIZE(s
->rx_fifo
)
80 && s
->resp_len
<= ARRAY_SIZE(s
->resp_fifo
);
84 static const VMStateDescription vmstate_pxa2xx_mmci
= {
85 .name
= "pxa2xx-mmci",
87 .minimum_version_id
= 2,
88 .fields
= (VMStateField
[]) {
89 VMSTATE_UINT32(status
, PXA2xxMMCIState
),
90 VMSTATE_UINT32(clkrt
, PXA2xxMMCIState
),
91 VMSTATE_UINT32(spi
, PXA2xxMMCIState
),
92 VMSTATE_UINT32(cmdat
, PXA2xxMMCIState
),
93 VMSTATE_UINT32(resp_tout
, PXA2xxMMCIState
),
94 VMSTATE_UINT32(read_tout
, PXA2xxMMCIState
),
95 VMSTATE_INT32(blklen
, PXA2xxMMCIState
),
96 VMSTATE_INT32(numblk
, PXA2xxMMCIState
),
97 VMSTATE_UINT32(intmask
, PXA2xxMMCIState
),
98 VMSTATE_UINT32(intreq
, PXA2xxMMCIState
),
99 VMSTATE_INT32(cmd
, PXA2xxMMCIState
),
100 VMSTATE_UINT32(arg
, PXA2xxMMCIState
),
101 VMSTATE_INT32(cmdreq
, PXA2xxMMCIState
),
102 VMSTATE_INT32(active
, PXA2xxMMCIState
),
103 VMSTATE_INT32(bytesleft
, PXA2xxMMCIState
),
104 VMSTATE_UINT32(tx_start
, PXA2xxMMCIState
),
105 VMSTATE_UINT32(tx_len
, PXA2xxMMCIState
),
106 VMSTATE_UINT32(rx_start
, PXA2xxMMCIState
),
107 VMSTATE_UINT32(rx_len
, PXA2xxMMCIState
),
108 VMSTATE_UINT32(resp_len
, PXA2xxMMCIState
),
109 VMSTATE_VALIDATE("fifo size incorrect", pxa2xx_mmci_vmstate_validate
),
110 VMSTATE_UINT8_ARRAY(tx_fifo
, PXA2xxMMCIState
, 64),
111 VMSTATE_UINT8_ARRAY(rx_fifo
, PXA2xxMMCIState
, 32),
112 VMSTATE_UINT16_ARRAY(resp_fifo
, PXA2xxMMCIState
, 9),
113 VMSTATE_END_OF_LIST()
117 #define MMC_STRPCL 0x00 /* MMC Clock Start/Stop register */
118 #define MMC_STAT 0x04 /* MMC Status register */
119 #define MMC_CLKRT 0x08 /* MMC Clock Rate register */
120 #define MMC_SPI 0x0c /* MMC SPI Mode register */
121 #define MMC_CMDAT 0x10 /* MMC Command/Data register */
122 #define MMC_RESTO 0x14 /* MMC Response Time-Out register */
123 #define MMC_RDTO 0x18 /* MMC Read Time-Out register */
124 #define MMC_BLKLEN 0x1c /* MMC Block Length register */
125 #define MMC_NUMBLK 0x20 /* MMC Number of Blocks register */
126 #define MMC_PRTBUF 0x24 /* MMC Buffer Partly Full register */
127 #define MMC_I_MASK 0x28 /* MMC Interrupt Mask register */
128 #define MMC_I_REG 0x2c /* MMC Interrupt Request register */
129 #define MMC_CMD 0x30 /* MMC Command register */
130 #define MMC_ARGH 0x34 /* MMC Argument High register */
131 #define MMC_ARGL 0x38 /* MMC Argument Low register */
132 #define MMC_RES 0x3c /* MMC Response FIFO */
133 #define MMC_RXFIFO 0x40 /* MMC Receive FIFO */
134 #define MMC_TXFIFO 0x44 /* MMC Transmit FIFO */
135 #define MMC_RDWAIT 0x48 /* MMC RD_WAIT register */
136 #define MMC_BLKS_REM 0x4c /* MMC Blocks Remaining register */
139 #define STRPCL_STOP_CLK (1 << 0)
140 #define STRPCL_STRT_CLK (1 << 1)
141 #define STAT_TOUT_RES (1 << 1)
142 #define STAT_CLK_EN (1 << 8)
143 #define STAT_DATA_DONE (1 << 11)
144 #define STAT_PRG_DONE (1 << 12)
145 #define STAT_END_CMDRES (1 << 13)
146 #define SPI_SPI_MODE (1 << 0)
147 #define CMDAT_RES_TYPE (3 << 0)
148 #define CMDAT_DATA_EN (1 << 2)
149 #define CMDAT_WR_RD (1 << 3)
150 #define CMDAT_DMA_EN (1 << 7)
151 #define CMDAT_STOP_TRAN (1 << 10)
152 #define INT_DATA_DONE (1 << 0)
153 #define INT_PRG_DONE (1 << 1)
154 #define INT_END_CMD (1 << 2)
155 #define INT_STOP_CMD (1 << 3)
156 #define INT_CLK_OFF (1 << 4)
157 #define INT_RXFIFO_REQ (1 << 5)
158 #define INT_TXFIFO_REQ (1 << 6)
159 #define INT_TINT (1 << 7)
160 #define INT_DAT_ERR (1 << 8)
161 #define INT_RES_ERR (1 << 9)
162 #define INT_RD_STALLED (1 << 10)
163 #define INT_SDIO_INT (1 << 11)
164 #define INT_SDIO_SACK (1 << 12)
165 #define PRTBUF_PRT_BUF (1 << 0)
167 /* Route internal interrupt lines to the global IC and DMA */
168 static void pxa2xx_mmci_int_update(PXA2xxMMCIState
*s
)
170 uint32_t mask
= s
->intmask
;
171 if (s
->cmdat
& CMDAT_DMA_EN
) {
172 mask
|= INT_RXFIFO_REQ
| INT_TXFIFO_REQ
;
174 qemu_set_irq(s
->rx_dma
, !!(s
->intreq
& INT_RXFIFO_REQ
));
175 qemu_set_irq(s
->tx_dma
, !!(s
->intreq
& INT_TXFIFO_REQ
));
178 qemu_set_irq(s
->irq
, !!(s
->intreq
& ~mask
));
181 static void pxa2xx_mmci_fifo_update(PXA2xxMMCIState
*s
)
186 if (s
->cmdat
& CMDAT_WR_RD
) {
187 while (s
->bytesleft
&& s
->tx_len
) {
188 sdbus_write_data(&s
->sdbus
, s
->tx_fifo
[s
->tx_start
++]);
194 s
->intreq
|= INT_TXFIFO_REQ
;
196 while (s
->bytesleft
&& s
->rx_len
< 32) {
197 s
->rx_fifo
[(s
->rx_start
+ (s
->rx_len
++)) & 0x1f] =
198 sdbus_read_data(&s
->sdbus
);
200 s
->intreq
|= INT_RXFIFO_REQ
;
205 s
->intreq
|= INT_DATA_DONE
;
206 s
->status
|= STAT_DATA_DONE
;
208 if (s
->cmdat
& CMDAT_WR_RD
) {
209 s
->intreq
|= INT_PRG_DONE
;
210 s
->status
|= STAT_PRG_DONE
;
214 pxa2xx_mmci_int_update(s
);
217 static void pxa2xx_mmci_wakequeues(PXA2xxMMCIState
*s
)
221 uint8_t response
[16];
228 request
.cmd
= s
->cmd
;
229 request
.arg
= s
->arg
;
230 request
.crc
= 0; /* FIXME */
232 rsplen
= sdbus_do_command(&s
->sdbus
, &request
, response
);
233 s
->intreq
|= INT_END_CMD
;
235 memset(s
->resp_fifo
, 0, sizeof(s
->resp_fifo
));
236 switch (s
->cmdat
& CMDAT_RES_TYPE
) {
237 #define PXAMMCI_RESP(wd, value0, value1) \
238 s->resp_fifo[(wd) + 0] |= (value0); \
239 s->resp_fifo[(wd) + 1] |= (value1) << 8;
240 case 0: /* No response */
243 case 1: /* R1, R4, R5 or R6 */
259 for (i
= 0; rsplen
> 0; i
++, rsplen
-= 2) {
260 PXAMMCI_RESP(i
, response
[i
* 2], response
[i
* 2 + 1]);
262 s
->status
|= STAT_END_CMDRES
;
264 if (!(s
->cmdat
& CMDAT_DATA_EN
))
267 s
->bytesleft
= s
->numblk
* s
->blklen
;
274 s
->status
|= STAT_TOUT_RES
;
278 pxa2xx_mmci_fifo_update(s
);
281 static uint64_t pxa2xx_mmci_read(void *opaque
, hwaddr offset
, unsigned size
)
283 PXA2xxMMCIState
*s
= (PXA2xxMMCIState
*) opaque
;
328 ret
= s
->arg
& 0xffff;
331 ret
= (s
->resp_len
< 9) ? s
->resp_fifo
[s
->resp_len
++] : 0;
334 while (size
-- && s
->rx_len
) {
335 ret
|= s
->rx_fifo
[s
->rx_start
++] << (size
<< 3);
339 s
->intreq
&= ~INT_RXFIFO_REQ
;
340 pxa2xx_mmci_fifo_update(s
);
348 qemu_log_mask(LOG_GUEST_ERROR
,
349 "%s: incorrect register 0x%02" HWADDR_PRIx
"\n",
352 trace_pxa2xx_mmci_read(size
, offset
, ret
);
357 static void pxa2xx_mmci_write(void *opaque
,
358 hwaddr offset
, uint64_t value
, unsigned size
)
360 PXA2xxMMCIState
*s
= (PXA2xxMMCIState
*) opaque
;
362 trace_pxa2xx_mmci_write(size
, offset
, value
);
365 if (value
& STRPCL_STRT_CLK
) {
366 s
->status
|= STAT_CLK_EN
;
367 s
->intreq
&= ~INT_CLK_OFF
;
369 if (s
->cmdreq
&& !(s
->cmdat
& CMDAT_STOP_TRAN
)) {
370 s
->status
&= STAT_CLK_EN
;
371 pxa2xx_mmci_wakequeues(s
);
375 if (value
& STRPCL_STOP_CLK
) {
376 s
->status
&= ~STAT_CLK_EN
;
377 s
->intreq
|= INT_CLK_OFF
;
381 pxa2xx_mmci_int_update(s
);
385 s
->clkrt
= value
& 7;
389 s
->spi
= value
& 0xf;
390 if (value
& SPI_SPI_MODE
) {
391 qemu_log_mask(LOG_GUEST_ERROR
,
392 "%s: attempted to use card in SPI mode\n", __func__
);
397 s
->cmdat
= value
& 0x3dff;
400 if (!(value
& CMDAT_STOP_TRAN
)) {
401 s
->status
&= STAT_CLK_EN
;
403 if (s
->status
& STAT_CLK_EN
)
404 pxa2xx_mmci_wakequeues(s
);
407 pxa2xx_mmci_int_update(s
);
411 s
->resp_tout
= value
& 0x7f;
415 s
->read_tout
= value
& 0xffff;
419 s
->blklen
= value
& 0xfff;
423 s
->numblk
= value
& 0xffff;
427 if (value
& PRTBUF_PRT_BUF
) {
431 pxa2xx_mmci_fifo_update(s
);
435 s
->intmask
= value
& 0x1fff;
436 pxa2xx_mmci_int_update(s
);
440 s
->cmd
= value
& 0x3f;
444 s
->arg
&= 0x0000ffff;
445 s
->arg
|= value
<< 16;
449 s
->arg
&= 0xffff0000;
450 s
->arg
|= value
& 0x0000ffff;
454 while (size
-- && s
->tx_len
< 0x20)
455 s
->tx_fifo
[(s
->tx_start
+ (s
->tx_len
++)) & 0x1f] =
456 (value
>> (size
<< 3)) & 0xff;
457 s
->intreq
&= ~INT_TXFIFO_REQ
;
458 pxa2xx_mmci_fifo_update(s
);
466 qemu_log_mask(LOG_GUEST_ERROR
,
467 "%s: incorrect reg 0x%02" HWADDR_PRIx
" "
468 "(value 0x%08" PRIx64
")\n", __func__
, offset
, value
);
472 static const MemoryRegionOps pxa2xx_mmci_ops
= {
473 .read
= pxa2xx_mmci_read
,
474 .write
= pxa2xx_mmci_write
,
475 .endianness
= DEVICE_NATIVE_ENDIAN
,
478 PXA2xxMMCIState
*pxa2xx_mmci_init(MemoryRegion
*sysmem
,
480 BlockBackend
*blk
, qemu_irq irq
,
481 qemu_irq rx_dma
, qemu_irq tx_dma
)
483 DeviceState
*dev
, *carddev
;
488 dev
= qdev_create(NULL
, TYPE_PXA2XX_MMCI
);
489 s
= PXA2XX_MMCI(dev
);
490 sbd
= SYS_BUS_DEVICE(dev
);
491 sysbus_mmio_map(sbd
, 0, base
);
492 sysbus_connect_irq(sbd
, 0, irq
);
493 qdev_connect_gpio_out_named(dev
, "rx-dma", 0, rx_dma
);
494 qdev_connect_gpio_out_named(dev
, "tx-dma", 0, tx_dma
);
496 /* Create and plug in the sd card */
497 carddev
= qdev_create(qdev_get_child_bus(dev
, "sd-bus"), TYPE_SD_CARD
);
498 qdev_prop_set_drive(carddev
, "drive", blk
, &err
);
500 error_report("failed to init SD card: %s", error_get_pretty(err
));
503 object_property_set_bool(OBJECT(carddev
), true, "realized", &err
);
505 error_report("failed to init SD card: %s", error_get_pretty(err
));
512 static void pxa2xx_mmci_set_inserted(DeviceState
*dev
, bool inserted
)
514 PXA2xxMMCIState
*s
= PXA2XX_MMCI(dev
);
516 qemu_set_irq(s
->inserted
, inserted
);
519 static void pxa2xx_mmci_set_readonly(DeviceState
*dev
, bool readonly
)
521 PXA2xxMMCIState
*s
= PXA2XX_MMCI(dev
);
523 qemu_set_irq(s
->readonly
, readonly
);
526 void pxa2xx_mmci_handlers(PXA2xxMMCIState
*s
, qemu_irq readonly
,
527 qemu_irq coverswitch
)
529 DeviceState
*dev
= DEVICE(s
);
531 s
->readonly
= readonly
;
532 s
->inserted
= coverswitch
;
534 pxa2xx_mmci_set_inserted(dev
, sdbus_get_inserted(&s
->sdbus
));
535 pxa2xx_mmci_set_readonly(dev
, sdbus_get_readonly(&s
->sdbus
));
538 static void pxa2xx_mmci_reset(DeviceState
*d
)
540 PXA2xxMMCIState
*s
= PXA2XX_MMCI(d
);
562 memset(s
->tx_fifo
, 0, sizeof(s
->tx_fifo
));
563 memset(s
->rx_fifo
, 0, sizeof(s
->rx_fifo
));
564 memset(s
->resp_fifo
, 0, sizeof(s
->resp_fifo
));
567 static void pxa2xx_mmci_instance_init(Object
*obj
)
569 PXA2xxMMCIState
*s
= PXA2XX_MMCI(obj
);
570 SysBusDevice
*sbd
= SYS_BUS_DEVICE(obj
);
571 DeviceState
*dev
= DEVICE(obj
);
573 memory_region_init_io(&s
->iomem
, obj
, &pxa2xx_mmci_ops
, s
,
574 "pxa2xx-mmci", 0x00100000);
575 sysbus_init_mmio(sbd
, &s
->iomem
);
576 sysbus_init_irq(sbd
, &s
->irq
);
577 qdev_init_gpio_out_named(dev
, &s
->rx_dma
, "rx-dma", 1);
578 qdev_init_gpio_out_named(dev
, &s
->tx_dma
, "tx-dma", 1);
580 qbus_create_inplace(&s
->sdbus
, sizeof(s
->sdbus
),
581 TYPE_PXA2XX_MMCI_BUS
, DEVICE(obj
), "sd-bus");
584 static void pxa2xx_mmci_class_init(ObjectClass
*klass
, void *data
)
586 DeviceClass
*dc
= DEVICE_CLASS(klass
);
588 dc
->vmsd
= &vmstate_pxa2xx_mmci
;
589 dc
->reset
= pxa2xx_mmci_reset
;
592 static void pxa2xx_mmci_bus_class_init(ObjectClass
*klass
, void *data
)
594 SDBusClass
*sbc
= SD_BUS_CLASS(klass
);
596 sbc
->set_inserted
= pxa2xx_mmci_set_inserted
;
597 sbc
->set_readonly
= pxa2xx_mmci_set_readonly
;
600 static const TypeInfo pxa2xx_mmci_info
= {
601 .name
= TYPE_PXA2XX_MMCI
,
602 .parent
= TYPE_SYS_BUS_DEVICE
,
603 .instance_size
= sizeof(PXA2xxMMCIState
),
604 .instance_init
= pxa2xx_mmci_instance_init
,
605 .class_init
= pxa2xx_mmci_class_init
,
608 static const TypeInfo pxa2xx_mmci_bus_info
= {
609 .name
= TYPE_PXA2XX_MMCI_BUS
,
610 .parent
= TYPE_SD_BUS
,
611 .instance_size
= sizeof(SDBus
),
612 .class_init
= pxa2xx_mmci_bus_class_init
,
615 static void pxa2xx_mmci_register_types(void)
617 type_register_static(&pxa2xx_mmci_info
);
618 type_register_static(&pxa2xx_mmci_bus_info
);
621 type_init(pxa2xx_mmci_register_types
)