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.
14 struct PXA2xxMMCIState
{
41 uint16_t resp_fifo
[9];
48 #define MMC_STRPCL 0x00 /* MMC Clock Start/Stop register */
49 #define MMC_STAT 0x04 /* MMC Status register */
50 #define MMC_CLKRT 0x08 /* MMC Clock Rate register */
51 #define MMC_SPI 0x0c /* MMC SPI Mode register */
52 #define MMC_CMDAT 0x10 /* MMC Command/Data register */
53 #define MMC_RESTO 0x14 /* MMC Response Time-Out register */
54 #define MMC_RDTO 0x18 /* MMC Read Time-Out register */
55 #define MMC_BLKLEN 0x1c /* MMC Block Length register */
56 #define MMC_NUMBLK 0x20 /* MMC Number of Blocks register */
57 #define MMC_PRTBUF 0x24 /* MMC Buffer Partly Full register */
58 #define MMC_I_MASK 0x28 /* MMC Interrupt Mask register */
59 #define MMC_I_REG 0x2c /* MMC Interrupt Request register */
60 #define MMC_CMD 0x30 /* MMC Command register */
61 #define MMC_ARGH 0x34 /* MMC Argument High register */
62 #define MMC_ARGL 0x38 /* MMC Argument Low register */
63 #define MMC_RES 0x3c /* MMC Response FIFO */
64 #define MMC_RXFIFO 0x40 /* MMC Receive FIFO */
65 #define MMC_TXFIFO 0x44 /* MMC Transmit FIFO */
66 #define MMC_RDWAIT 0x48 /* MMC RD_WAIT register */
67 #define MMC_BLKS_REM 0x4c /* MMC Blocks Remaining register */
70 #define STRPCL_STOP_CLK (1 << 0)
71 #define STRPCL_STRT_CLK (1 << 1)
72 #define STAT_TOUT_RES (1 << 1)
73 #define STAT_CLK_EN (1 << 8)
74 #define STAT_DATA_DONE (1 << 11)
75 #define STAT_PRG_DONE (1 << 12)
76 #define STAT_END_CMDRES (1 << 13)
77 #define SPI_SPI_MODE (1 << 0)
78 #define CMDAT_RES_TYPE (3 << 0)
79 #define CMDAT_DATA_EN (1 << 2)
80 #define CMDAT_WR_RD (1 << 3)
81 #define CMDAT_DMA_EN (1 << 7)
82 #define CMDAT_STOP_TRAN (1 << 10)
83 #define INT_DATA_DONE (1 << 0)
84 #define INT_PRG_DONE (1 << 1)
85 #define INT_END_CMD (1 << 2)
86 #define INT_STOP_CMD (1 << 3)
87 #define INT_CLK_OFF (1 << 4)
88 #define INT_RXFIFO_REQ (1 << 5)
89 #define INT_TXFIFO_REQ (1 << 6)
90 #define INT_TINT (1 << 7)
91 #define INT_DAT_ERR (1 << 8)
92 #define INT_RES_ERR (1 << 9)
93 #define INT_RD_STALLED (1 << 10)
94 #define INT_SDIO_INT (1 << 11)
95 #define INT_SDIO_SACK (1 << 12)
96 #define PRTBUF_PRT_BUF (1 << 0)
98 /* Route internal interrupt lines to the global IC and DMA */
99 static void pxa2xx_mmci_int_update(PXA2xxMMCIState
*s
)
101 uint32_t mask
= s
->intmask
;
102 if (s
->cmdat
& CMDAT_DMA_EN
) {
103 mask
|= INT_RXFIFO_REQ
| INT_TXFIFO_REQ
;
105 pxa2xx_dma_request(s
->dma
,
106 PXA2XX_RX_RQ_MMCI
, !!(s
->intreq
& INT_RXFIFO_REQ
));
107 pxa2xx_dma_request(s
->dma
,
108 PXA2XX_TX_RQ_MMCI
, !!(s
->intreq
& INT_TXFIFO_REQ
));
111 qemu_set_irq(s
->irq
, !!(s
->intreq
& ~mask
));
114 static void pxa2xx_mmci_fifo_update(PXA2xxMMCIState
*s
)
119 if (s
->cmdat
& CMDAT_WR_RD
) {
120 while (s
->bytesleft
&& s
->tx_len
) {
121 sd_write_data(s
->card
, s
->tx_fifo
[s
->tx_start
++]);
127 s
->intreq
|= INT_TXFIFO_REQ
;
129 while (s
->bytesleft
&& s
->rx_len
< 32) {
130 s
->rx_fifo
[(s
->rx_start
+ (s
->rx_len
++)) & 0x1f] =
131 sd_read_data(s
->card
);
133 s
->intreq
|= INT_RXFIFO_REQ
;
138 s
->intreq
|= INT_DATA_DONE
;
139 s
->status
|= STAT_DATA_DONE
;
141 if (s
->cmdat
& CMDAT_WR_RD
) {
142 s
->intreq
|= INT_PRG_DONE
;
143 s
->status
|= STAT_PRG_DONE
;
147 pxa2xx_mmci_int_update(s
);
150 static void pxa2xx_mmci_wakequeues(PXA2xxMMCIState
*s
)
154 uint8_t response
[16];
161 request
.cmd
= s
->cmd
;
162 request
.arg
= s
->arg
;
163 request
.crc
= 0; /* FIXME */
165 rsplen
= sd_do_command(s
->card
, &request
, response
);
166 s
->intreq
|= INT_END_CMD
;
168 memset(s
->resp_fifo
, 0, sizeof(s
->resp_fifo
));
169 switch (s
->cmdat
& CMDAT_RES_TYPE
) {
170 #define PXAMMCI_RESP(wd, value0, value1) \
171 s->resp_fifo[(wd) + 0] |= (value0); \
172 s->resp_fifo[(wd) + 1] |= (value1) << 8;
173 case 0: /* No response */
176 case 1: /* R1, R4, R5 or R6 */
192 for (i
= 0; rsplen
> 0; i
++, rsplen
-= 2) {
193 PXAMMCI_RESP(i
, response
[i
* 2], response
[i
* 2 + 1]);
195 s
->status
|= STAT_END_CMDRES
;
197 if (!(s
->cmdat
& CMDAT_DATA_EN
))
200 s
->bytesleft
= s
->numblk
* s
->blklen
;
207 s
->status
|= STAT_TOUT_RES
;
211 pxa2xx_mmci_fifo_update(s
);
214 static uint32_t pxa2xx_mmci_read(void *opaque
, target_phys_addr_t offset
)
216 PXA2xxMMCIState
*s
= (PXA2xxMMCIState
*) opaque
;
245 return s
->cmd
| 0x40;
249 return s
->arg
& 0xffff;
252 return s
->resp_fifo
[s
->resp_len
++];
256 while (s
->ac_width
-- && s
->rx_len
) {
257 ret
|= s
->rx_fifo
[s
->rx_start
++] << (s
->ac_width
<< 3);
261 s
->intreq
&= ~INT_RXFIFO_REQ
;
262 pxa2xx_mmci_fifo_update(s
);
269 hw_error("%s: Bad offset " REG_FMT
"\n", __FUNCTION__
, offset
);
275 static void pxa2xx_mmci_write(void *opaque
,
276 target_phys_addr_t offset
, uint32_t value
)
278 PXA2xxMMCIState
*s
= (PXA2xxMMCIState
*) opaque
;
282 if (value
& STRPCL_STRT_CLK
) {
283 s
->status
|= STAT_CLK_EN
;
284 s
->intreq
&= ~INT_CLK_OFF
;
286 if (s
->cmdreq
&& !(s
->cmdat
& CMDAT_STOP_TRAN
)) {
287 s
->status
&= STAT_CLK_EN
;
288 pxa2xx_mmci_wakequeues(s
);
292 if (value
& STRPCL_STOP_CLK
) {
293 s
->status
&= ~STAT_CLK_EN
;
294 s
->intreq
|= INT_CLK_OFF
;
298 pxa2xx_mmci_int_update(s
);
302 s
->clkrt
= value
& 7;
306 s
->spi
= value
& 0xf;
307 if (value
& SPI_SPI_MODE
)
308 printf("%s: attempted to use card in SPI mode\n", __FUNCTION__
);
312 s
->cmdat
= value
& 0x3dff;
315 if (!(value
& CMDAT_STOP_TRAN
)) {
316 s
->status
&= STAT_CLK_EN
;
318 if (s
->status
& STAT_CLK_EN
)
319 pxa2xx_mmci_wakequeues(s
);
322 pxa2xx_mmci_int_update(s
);
326 s
->resp_tout
= value
& 0x7f;
330 s
->read_tout
= value
& 0xffff;
334 s
->blklen
= value
& 0xfff;
338 s
->numblk
= value
& 0xffff;
342 if (value
& PRTBUF_PRT_BUF
) {
346 pxa2xx_mmci_fifo_update(s
);
350 s
->intmask
= value
& 0x1fff;
351 pxa2xx_mmci_int_update(s
);
355 s
->cmd
= value
& 0x3f;
359 s
->arg
&= 0x0000ffff;
360 s
->arg
|= value
<< 16;
364 s
->arg
&= 0xffff0000;
365 s
->arg
|= value
& 0x0000ffff;
369 while (s
->ac_width
-- && s
->tx_len
< 0x20)
370 s
->tx_fifo
[(s
->tx_start
+ (s
->tx_len
++)) & 0x1f] =
371 (value
>> (s
->ac_width
<< 3)) & 0xff;
372 s
->intreq
&= ~INT_TXFIFO_REQ
;
373 pxa2xx_mmci_fifo_update(s
);
381 hw_error("%s: Bad offset " REG_FMT
"\n", __FUNCTION__
, offset
);
385 static uint32_t pxa2xx_mmci_readb(void *opaque
, target_phys_addr_t offset
)
387 PXA2xxMMCIState
*s
= (PXA2xxMMCIState
*) opaque
;
389 return pxa2xx_mmci_read(opaque
, offset
);
392 static uint32_t pxa2xx_mmci_readh(void *opaque
, target_phys_addr_t offset
)
394 PXA2xxMMCIState
*s
= (PXA2xxMMCIState
*) opaque
;
396 return pxa2xx_mmci_read(opaque
, offset
);
399 static uint32_t pxa2xx_mmci_readw(void *opaque
, target_phys_addr_t offset
)
401 PXA2xxMMCIState
*s
= (PXA2xxMMCIState
*) opaque
;
403 return pxa2xx_mmci_read(opaque
, offset
);
406 static CPUReadMemoryFunc
* const pxa2xx_mmci_readfn
[] = {
412 static void pxa2xx_mmci_writeb(void *opaque
,
413 target_phys_addr_t offset
, uint32_t value
)
415 PXA2xxMMCIState
*s
= (PXA2xxMMCIState
*) opaque
;
417 pxa2xx_mmci_write(opaque
, offset
, value
);
420 static void pxa2xx_mmci_writeh(void *opaque
,
421 target_phys_addr_t offset
, uint32_t value
)
423 PXA2xxMMCIState
*s
= (PXA2xxMMCIState
*) opaque
;
425 pxa2xx_mmci_write(opaque
, offset
, value
);
428 static void pxa2xx_mmci_writew(void *opaque
,
429 target_phys_addr_t offset
, uint32_t value
)
431 PXA2xxMMCIState
*s
= (PXA2xxMMCIState
*) opaque
;
433 pxa2xx_mmci_write(opaque
, offset
, value
);
436 static CPUWriteMemoryFunc
* const pxa2xx_mmci_writefn
[] = {
442 static void pxa2xx_mmci_save(QEMUFile
*f
, void *opaque
)
444 PXA2xxMMCIState
*s
= (PXA2xxMMCIState
*) opaque
;
447 qemu_put_be32s(f
, &s
->status
);
448 qemu_put_be32s(f
, &s
->clkrt
);
449 qemu_put_be32s(f
, &s
->spi
);
450 qemu_put_be32s(f
, &s
->cmdat
);
451 qemu_put_be32s(f
, &s
->resp_tout
);
452 qemu_put_be32s(f
, &s
->read_tout
);
453 qemu_put_be32(f
, s
->blklen
);
454 qemu_put_be32(f
, s
->numblk
);
455 qemu_put_be32s(f
, &s
->intmask
);
456 qemu_put_be32s(f
, &s
->intreq
);
457 qemu_put_be32(f
, s
->cmd
);
458 qemu_put_be32s(f
, &s
->arg
);
459 qemu_put_be32(f
, s
->cmdreq
);
460 qemu_put_be32(f
, s
->active
);
461 qemu_put_be32(f
, s
->bytesleft
);
463 qemu_put_byte(f
, s
->tx_len
);
464 for (i
= 0; i
< s
->tx_len
; i
++)
465 qemu_put_byte(f
, s
->tx_fifo
[(s
->tx_start
+ i
) & 63]);
467 qemu_put_byte(f
, s
->rx_len
);
468 for (i
= 0; i
< s
->rx_len
; i
++)
469 qemu_put_byte(f
, s
->rx_fifo
[(s
->rx_start
+ i
) & 31]);
471 qemu_put_byte(f
, s
->resp_len
);
472 for (i
= s
->resp_len
; i
< 9; i
++)
473 qemu_put_be16s(f
, &s
->resp_fifo
[i
]);
476 static int pxa2xx_mmci_load(QEMUFile
*f
, void *opaque
, int version_id
)
478 PXA2xxMMCIState
*s
= (PXA2xxMMCIState
*) opaque
;
481 qemu_get_be32s(f
, &s
->status
);
482 qemu_get_be32s(f
, &s
->clkrt
);
483 qemu_get_be32s(f
, &s
->spi
);
484 qemu_get_be32s(f
, &s
->cmdat
);
485 qemu_get_be32s(f
, &s
->resp_tout
);
486 qemu_get_be32s(f
, &s
->read_tout
);
487 s
->blklen
= qemu_get_be32(f
);
488 s
->numblk
= qemu_get_be32(f
);
489 qemu_get_be32s(f
, &s
->intmask
);
490 qemu_get_be32s(f
, &s
->intreq
);
491 s
->cmd
= qemu_get_be32(f
);
492 qemu_get_be32s(f
, &s
->arg
);
493 s
->cmdreq
= qemu_get_be32(f
);
494 s
->active
= qemu_get_be32(f
);
495 s
->bytesleft
= qemu_get_be32(f
);
497 s
->tx_len
= qemu_get_byte(f
);
499 if (s
->tx_len
>= sizeof(s
->tx_fifo
) || s
->tx_len
< 0)
501 for (i
= 0; i
< s
->tx_len
; i
++)
502 s
->tx_fifo
[i
] = qemu_get_byte(f
);
504 s
->rx_len
= qemu_get_byte(f
);
506 if (s
->rx_len
>= sizeof(s
->rx_fifo
) || s
->rx_len
< 0)
508 for (i
= 0; i
< s
->rx_len
; i
++)
509 s
->rx_fifo
[i
] = qemu_get_byte(f
);
511 s
->resp_len
= qemu_get_byte(f
);
512 if (s
->resp_len
> 9 || s
->resp_len
< 0)
514 for (i
= s
->resp_len
; i
< 9; i
++)
515 qemu_get_be16s(f
, &s
->resp_fifo
[i
]);
520 PXA2xxMMCIState
*pxa2xx_mmci_init(target_phys_addr_t base
,
521 BlockDriverState
*bd
, qemu_irq irq
, void *dma
)
526 s
= (PXA2xxMMCIState
*) qemu_mallocz(sizeof(PXA2xxMMCIState
));
530 iomemtype
= cpu_register_io_memory(pxa2xx_mmci_readfn
,
531 pxa2xx_mmci_writefn
, s
);
532 cpu_register_physical_memory(base
, 0x00100000, iomemtype
);
534 /* Instantiate the actual storage */
535 s
->card
= sd_init(bd
, 0);
537 register_savevm(NULL
, "pxa2xx_mmci", 0, 0,
538 pxa2xx_mmci_save
, pxa2xx_mmci_load
, s
);
543 void pxa2xx_mmci_handlers(PXA2xxMMCIState
*s
, qemu_irq readonly
,
544 qemu_irq coverswitch
)
546 sd_set_cb(s
->card
, readonly
, coverswitch
);