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 pxa2xx_mmci_s
{
15 target_phys_addr_t base
;
42 uint16_t resp_fifo
[9];
49 #define MMC_STRPCL 0x00 /* MMC Clock Start/Stop register */
50 #define MMC_STAT 0x04 /* MMC Status register */
51 #define MMC_CLKRT 0x08 /* MMC Clock Rate register */
52 #define MMC_SPI 0x0c /* MMC SPI Mode register */
53 #define MMC_CMDAT 0x10 /* MMC Command/Data register */
54 #define MMC_RESTO 0x14 /* MMC Response Time-Out register */
55 #define MMC_RDTO 0x18 /* MMC Read Time-Out register */
56 #define MMC_BLKLEN 0x1c /* MMC Block Length register */
57 #define MMC_NUMBLK 0x20 /* MMC Number of Blocks register */
58 #define MMC_PRTBUF 0x24 /* MMC Buffer Partly Full register */
59 #define MMC_I_MASK 0x28 /* MMC Interrupt Mask register */
60 #define MMC_I_REG 0x2c /* MMC Interrupt Request register */
61 #define MMC_CMD 0x30 /* MMC Command register */
62 #define MMC_ARGH 0x34 /* MMC Argument High register */
63 #define MMC_ARGL 0x38 /* MMC Argument Low register */
64 #define MMC_RES 0x3c /* MMC Response FIFO */
65 #define MMC_RXFIFO 0x40 /* MMC Receive FIFO */
66 #define MMC_TXFIFO 0x44 /* MMC Transmit FIFO */
67 #define MMC_RDWAIT 0x48 /* MMC RD_WAIT register */
68 #define MMC_BLKS_REM 0x4c /* MMC Blocks Remaining register */
71 #define STRPCL_STOP_CLK (1 << 0)
72 #define STRPCL_STRT_CLK (1 << 1)
73 #define STAT_TOUT_RES (1 << 1)
74 #define STAT_CLK_EN (1 << 8)
75 #define STAT_DATA_DONE (1 << 11)
76 #define STAT_PRG_DONE (1 << 12)
77 #define STAT_END_CMDRES (1 << 13)
78 #define SPI_SPI_MODE (1 << 0)
79 #define CMDAT_RES_TYPE (3 << 0)
80 #define CMDAT_DATA_EN (1 << 2)
81 #define CMDAT_WR_RD (1 << 3)
82 #define CMDAT_DMA_EN (1 << 7)
83 #define CMDAT_STOP_TRAN (1 << 10)
84 #define INT_DATA_DONE (1 << 0)
85 #define INT_PRG_DONE (1 << 1)
86 #define INT_END_CMD (1 << 2)
87 #define INT_STOP_CMD (1 << 3)
88 #define INT_CLK_OFF (1 << 4)
89 #define INT_RXFIFO_REQ (1 << 5)
90 #define INT_TXFIFO_REQ (1 << 6)
91 #define INT_TINT (1 << 7)
92 #define INT_DAT_ERR (1 << 8)
93 #define INT_RES_ERR (1 << 9)
94 #define INT_RD_STALLED (1 << 10)
95 #define INT_SDIO_INT (1 << 11)
96 #define INT_SDIO_SACK (1 << 12)
97 #define PRTBUF_PRT_BUF (1 << 0)
99 /* Route internal interrupt lines to the global IC and DMA */
100 static void pxa2xx_mmci_int_update(struct pxa2xx_mmci_s
*s
)
102 uint32_t mask
= s
->intmask
;
103 if (s
->cmdat
& CMDAT_DMA_EN
) {
104 mask
|= INT_RXFIFO_REQ
| INT_TXFIFO_REQ
;
106 pxa2xx_dma_request((struct pxa2xx_dma_state_s
*) s
->dma
,
107 PXA2XX_RX_RQ_MMCI
, !!(s
->intreq
& INT_RXFIFO_REQ
));
108 pxa2xx_dma_request((struct pxa2xx_dma_state_s
*) s
->dma
,
109 PXA2XX_TX_RQ_MMCI
, !!(s
->intreq
& INT_TXFIFO_REQ
));
112 qemu_set_irq(s
->irq
, !!(s
->intreq
& ~mask
));
115 static void pxa2xx_mmci_fifo_update(struct pxa2xx_mmci_s
*s
)
120 if (s
->cmdat
& CMDAT_WR_RD
) {
121 while (s
->bytesleft
&& s
->tx_len
) {
122 sd_write_data(s
->card
, s
->tx_fifo
[s
->tx_start
++]);
128 s
->intreq
|= INT_TXFIFO_REQ
;
130 while (s
->bytesleft
&& s
->rx_len
< 32) {
131 s
->rx_fifo
[(s
->rx_start
+ (s
->rx_len
++)) & 0x1f] =
132 sd_read_data(s
->card
);
134 s
->intreq
|= INT_RXFIFO_REQ
;
139 s
->intreq
|= INT_DATA_DONE
;
140 s
->status
|= STAT_DATA_DONE
;
142 if (s
->cmdat
& CMDAT_WR_RD
) {
143 s
->intreq
|= INT_PRG_DONE
;
144 s
->status
|= STAT_PRG_DONE
;
148 pxa2xx_mmci_int_update(s
);
151 static void pxa2xx_mmci_wakequeues(struct pxa2xx_mmci_s
*s
)
154 struct sd_request_s request
;
155 uint8_t response
[16];
162 request
.cmd
= s
->cmd
;
163 request
.arg
= s
->arg
;
164 request
.crc
= 0; /* FIXME */
166 rsplen
= sd_do_command(s
->card
, &request
, response
);
167 s
->intreq
|= INT_END_CMD
;
169 memset(s
->resp_fifo
, 0, sizeof(s
->resp_fifo
));
170 switch (s
->cmdat
& CMDAT_RES_TYPE
) {
171 #define PXAMMCI_RESP(wd, value0, value1) \
172 s->resp_fifo[(wd) + 0] |= (value0); \
173 s->resp_fifo[(wd) + 1] |= (value1) << 8;
174 case 0: /* No response */
177 case 1: /* R1, R4, R5 or R6 */
193 for (i
= 0; rsplen
> 0; i
++, rsplen
-= 2) {
194 PXAMMCI_RESP(i
, response
[i
* 2], response
[i
* 2 + 1]);
196 s
->status
|= STAT_END_CMDRES
;
198 if (!(s
->cmdat
& CMDAT_DATA_EN
))
201 s
->bytesleft
= s
->numblk
* s
->blklen
;
208 s
->status
|= STAT_TOUT_RES
;
212 pxa2xx_mmci_fifo_update(s
);
215 static uint32_t pxa2xx_mmci_read(void *opaque
, target_phys_addr_t offset
)
217 struct pxa2xx_mmci_s
*s
= (struct pxa2xx_mmci_s
*) opaque
;
247 return s
->cmd
| 0x40;
251 return s
->arg
& 0xffff;
254 return s
->resp_fifo
[s
->resp_len
++];
258 while (s
->ac_width
-- && s
->rx_len
) {
259 ret
|= s
->rx_fifo
[s
->rx_start
++] << (s
->ac_width
<< 3);
263 s
->intreq
&= ~INT_RXFIFO_REQ
;
264 pxa2xx_mmci_fifo_update(s
);
271 cpu_abort(cpu_single_env
, "%s: Bad offset " REG_FMT
"\n",
272 __FUNCTION__
, offset
);
278 static void pxa2xx_mmci_write(void *opaque
,
279 target_phys_addr_t offset
, uint32_t value
)
281 struct pxa2xx_mmci_s
*s
= (struct pxa2xx_mmci_s
*) opaque
;
286 if (value
& STRPCL_STRT_CLK
) {
287 s
->status
|= STAT_CLK_EN
;
288 s
->intreq
&= ~INT_CLK_OFF
;
290 if (s
->cmdreq
&& !(s
->cmdat
& CMDAT_STOP_TRAN
)) {
291 s
->status
&= STAT_CLK_EN
;
292 pxa2xx_mmci_wakequeues(s
);
296 if (value
& STRPCL_STOP_CLK
) {
297 s
->status
&= ~STAT_CLK_EN
;
298 s
->intreq
|= INT_CLK_OFF
;
302 pxa2xx_mmci_int_update(s
);
306 s
->clkrt
= value
& 7;
310 s
->spi
= value
& 0xf;
311 if (value
& SPI_SPI_MODE
)
312 printf("%s: attempted to use card in SPI mode\n", __FUNCTION__
);
316 s
->cmdat
= value
& 0x3dff;
319 if (!(value
& CMDAT_STOP_TRAN
)) {
320 s
->status
&= STAT_CLK_EN
;
322 if (s
->status
& STAT_CLK_EN
)
323 pxa2xx_mmci_wakequeues(s
);
326 pxa2xx_mmci_int_update(s
);
330 s
->resp_tout
= value
& 0x7f;
334 s
->read_tout
= value
& 0xffff;
338 s
->blklen
= value
& 0xfff;
342 s
->numblk
= value
& 0xffff;
346 if (value
& PRTBUF_PRT_BUF
) {
350 pxa2xx_mmci_fifo_update(s
);
354 s
->intmask
= value
& 0x1fff;
355 pxa2xx_mmci_int_update(s
);
359 s
->cmd
= value
& 0x3f;
363 s
->arg
&= 0x0000ffff;
364 s
->arg
|= value
<< 16;
368 s
->arg
&= 0xffff0000;
369 s
->arg
|= value
& 0x0000ffff;
373 while (s
->ac_width
-- && s
->tx_len
< 0x20)
374 s
->tx_fifo
[(s
->tx_start
+ (s
->tx_len
++)) & 0x1f] =
375 (value
>> (s
->ac_width
<< 3)) & 0xff;
376 s
->intreq
&= ~INT_TXFIFO_REQ
;
377 pxa2xx_mmci_fifo_update(s
);
385 cpu_abort(cpu_single_env
, "%s: Bad offset " REG_FMT
"\n",
386 __FUNCTION__
, offset
);
390 static uint32_t pxa2xx_mmci_readb(void *opaque
, target_phys_addr_t offset
)
392 struct pxa2xx_mmci_s
*s
= (struct pxa2xx_mmci_s
*) opaque
;
394 return pxa2xx_mmci_read(opaque
, offset
);
397 static uint32_t pxa2xx_mmci_readh(void *opaque
, target_phys_addr_t offset
)
399 struct pxa2xx_mmci_s
*s
= (struct pxa2xx_mmci_s
*) opaque
;
401 return pxa2xx_mmci_read(opaque
, offset
);
404 static uint32_t pxa2xx_mmci_readw(void *opaque
, target_phys_addr_t offset
)
406 struct pxa2xx_mmci_s
*s
= (struct pxa2xx_mmci_s
*) opaque
;
408 return pxa2xx_mmci_read(opaque
, offset
);
411 static CPUReadMemoryFunc
*pxa2xx_mmci_readfn
[] = {
417 static void pxa2xx_mmci_writeb(void *opaque
,
418 target_phys_addr_t offset
, uint32_t value
)
420 struct pxa2xx_mmci_s
*s
= (struct pxa2xx_mmci_s
*) opaque
;
422 pxa2xx_mmci_write(opaque
, offset
, value
);
425 static void pxa2xx_mmci_writeh(void *opaque
,
426 target_phys_addr_t offset
, uint32_t value
)
428 struct pxa2xx_mmci_s
*s
= (struct pxa2xx_mmci_s
*) opaque
;
430 pxa2xx_mmci_write(opaque
, offset
, value
);
433 static void pxa2xx_mmci_writew(void *opaque
,
434 target_phys_addr_t offset
, uint32_t value
)
436 struct pxa2xx_mmci_s
*s
= (struct pxa2xx_mmci_s
*) opaque
;
438 pxa2xx_mmci_write(opaque
, offset
, value
);
441 static CPUWriteMemoryFunc
*pxa2xx_mmci_writefn
[] = {
447 static void pxa2xx_mmci_save(QEMUFile
*f
, void *opaque
)
449 struct pxa2xx_mmci_s
*s
= (struct pxa2xx_mmci_s
*) opaque
;
452 qemu_put_be32s(f
, &s
->status
);
453 qemu_put_be32s(f
, &s
->clkrt
);
454 qemu_put_be32s(f
, &s
->spi
);
455 qemu_put_be32s(f
, &s
->cmdat
);
456 qemu_put_be32s(f
, &s
->resp_tout
);
457 qemu_put_be32s(f
, &s
->read_tout
);
458 qemu_put_be32(f
, s
->blklen
);
459 qemu_put_be32(f
, s
->numblk
);
460 qemu_put_be32s(f
, &s
->intmask
);
461 qemu_put_be32s(f
, &s
->intreq
);
462 qemu_put_be32(f
, s
->cmd
);
463 qemu_put_be32s(f
, &s
->arg
);
464 qemu_put_be32(f
, s
->cmdreq
);
465 qemu_put_be32(f
, s
->active
);
466 qemu_put_be32(f
, s
->bytesleft
);
468 qemu_put_byte(f
, s
->tx_len
);
469 for (i
= 0; i
< s
->tx_len
; i
++)
470 qemu_put_byte(f
, s
->tx_fifo
[(s
->tx_start
+ i
) & 63]);
472 qemu_put_byte(f
, s
->rx_len
);
473 for (i
= 0; i
< s
->rx_len
; i
++)
474 qemu_put_byte(f
, s
->rx_fifo
[(s
->rx_start
+ i
) & 31]);
476 qemu_put_byte(f
, s
->resp_len
);
477 for (i
= s
->resp_len
; i
< 9; i
++)
478 qemu_put_be16s(f
, &s
->resp_fifo
[i
]);
481 static int pxa2xx_mmci_load(QEMUFile
*f
, void *opaque
, int version_id
)
483 struct pxa2xx_mmci_s
*s
= (struct pxa2xx_mmci_s
*) opaque
;
486 qemu_get_be32s(f
, &s
->status
);
487 qemu_get_be32s(f
, &s
->clkrt
);
488 qemu_get_be32s(f
, &s
->spi
);
489 qemu_get_be32s(f
, &s
->cmdat
);
490 qemu_get_be32s(f
, &s
->resp_tout
);
491 qemu_get_be32s(f
, &s
->read_tout
);
492 s
->blklen
= qemu_get_be32(f
);
493 s
->numblk
= qemu_get_be32(f
);
494 qemu_get_be32s(f
, &s
->intmask
);
495 qemu_get_be32s(f
, &s
->intreq
);
496 s
->cmd
= qemu_get_be32(f
);
497 qemu_get_be32s(f
, &s
->arg
);
498 s
->cmdreq
= qemu_get_be32(f
);
499 s
->active
= qemu_get_be32(f
);
500 s
->bytesleft
= qemu_get_be32(f
);
502 s
->tx_len
= qemu_get_byte(f
);
504 if (s
->tx_len
>= sizeof(s
->tx_fifo
) || s
->tx_len
< 0)
506 for (i
= 0; i
< s
->tx_len
; i
++)
507 s
->tx_fifo
[i
] = qemu_get_byte(f
);
509 s
->rx_len
= qemu_get_byte(f
);
511 if (s
->rx_len
>= sizeof(s
->rx_fifo
) || s
->rx_len
< 0)
513 for (i
= 0; i
< s
->rx_len
; i
++)
514 s
->rx_fifo
[i
] = qemu_get_byte(f
);
516 s
->resp_len
= qemu_get_byte(f
);
517 if (s
->resp_len
> 9 || s
->resp_len
< 0)
519 for (i
= s
->resp_len
; i
< 9; i
++)
520 qemu_get_be16s(f
, &s
->resp_fifo
[i
]);
525 struct pxa2xx_mmci_s
*pxa2xx_mmci_init(target_phys_addr_t base
,
526 BlockDriverState
*bd
, qemu_irq irq
, void *dma
)
529 struct pxa2xx_mmci_s
*s
;
531 s
= (struct pxa2xx_mmci_s
*) qemu_mallocz(sizeof(struct pxa2xx_mmci_s
));
536 iomemtype
= cpu_register_io_memory(0, pxa2xx_mmci_readfn
,
537 pxa2xx_mmci_writefn
, s
);
538 cpu_register_physical_memory(base
, 0x00100000, iomemtype
);
540 /* Instantiate the actual storage */
541 s
->card
= sd_init(bd
, 0);
543 register_savevm("pxa2xx_mmci", 0, 0,
544 pxa2xx_mmci_save
, pxa2xx_mmci_load
, s
);
549 void pxa2xx_mmci_handlers(struct pxa2xx_mmci_s
*s
, qemu_irq readonly
,
550 qemu_irq coverswitch
)
552 sd_set_cb(s
->card
, readonly
, coverswitch
);