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.
14 #include "hw/arm/pxa.h"
18 struct PXA2xxMMCIState
{
47 uint16_t resp_fifo
[9];
53 #define MMC_STRPCL 0x00 /* MMC Clock Start/Stop register */
54 #define MMC_STAT 0x04 /* MMC Status register */
55 #define MMC_CLKRT 0x08 /* MMC Clock Rate register */
56 #define MMC_SPI 0x0c /* MMC SPI Mode register */
57 #define MMC_CMDAT 0x10 /* MMC Command/Data register */
58 #define MMC_RESTO 0x14 /* MMC Response Time-Out register */
59 #define MMC_RDTO 0x18 /* MMC Read Time-Out register */
60 #define MMC_BLKLEN 0x1c /* MMC Block Length register */
61 #define MMC_NUMBLK 0x20 /* MMC Number of Blocks register */
62 #define MMC_PRTBUF 0x24 /* MMC Buffer Partly Full register */
63 #define MMC_I_MASK 0x28 /* MMC Interrupt Mask register */
64 #define MMC_I_REG 0x2c /* MMC Interrupt Request register */
65 #define MMC_CMD 0x30 /* MMC Command register */
66 #define MMC_ARGH 0x34 /* MMC Argument High register */
67 #define MMC_ARGL 0x38 /* MMC Argument Low register */
68 #define MMC_RES 0x3c /* MMC Response FIFO */
69 #define MMC_RXFIFO 0x40 /* MMC Receive FIFO */
70 #define MMC_TXFIFO 0x44 /* MMC Transmit FIFO */
71 #define MMC_RDWAIT 0x48 /* MMC RD_WAIT register */
72 #define MMC_BLKS_REM 0x4c /* MMC Blocks Remaining register */
75 #define STRPCL_STOP_CLK (1 << 0)
76 #define STRPCL_STRT_CLK (1 << 1)
77 #define STAT_TOUT_RES (1 << 1)
78 #define STAT_CLK_EN (1 << 8)
79 #define STAT_DATA_DONE (1 << 11)
80 #define STAT_PRG_DONE (1 << 12)
81 #define STAT_END_CMDRES (1 << 13)
82 #define SPI_SPI_MODE (1 << 0)
83 #define CMDAT_RES_TYPE (3 << 0)
84 #define CMDAT_DATA_EN (1 << 2)
85 #define CMDAT_WR_RD (1 << 3)
86 #define CMDAT_DMA_EN (1 << 7)
87 #define CMDAT_STOP_TRAN (1 << 10)
88 #define INT_DATA_DONE (1 << 0)
89 #define INT_PRG_DONE (1 << 1)
90 #define INT_END_CMD (1 << 2)
91 #define INT_STOP_CMD (1 << 3)
92 #define INT_CLK_OFF (1 << 4)
93 #define INT_RXFIFO_REQ (1 << 5)
94 #define INT_TXFIFO_REQ (1 << 6)
95 #define INT_TINT (1 << 7)
96 #define INT_DAT_ERR (1 << 8)
97 #define INT_RES_ERR (1 << 9)
98 #define INT_RD_STALLED (1 << 10)
99 #define INT_SDIO_INT (1 << 11)
100 #define INT_SDIO_SACK (1 << 12)
101 #define PRTBUF_PRT_BUF (1 << 0)
103 /* Route internal interrupt lines to the global IC and DMA */
104 static void pxa2xx_mmci_int_update(PXA2xxMMCIState
*s
)
106 uint32_t mask
= s
->intmask
;
107 if (s
->cmdat
& CMDAT_DMA_EN
) {
108 mask
|= INT_RXFIFO_REQ
| INT_TXFIFO_REQ
;
110 qemu_set_irq(s
->rx_dma
, !!(s
->intreq
& INT_RXFIFO_REQ
));
111 qemu_set_irq(s
->tx_dma
, !!(s
->intreq
& INT_TXFIFO_REQ
));
114 qemu_set_irq(s
->irq
, !!(s
->intreq
& ~mask
));
117 static void pxa2xx_mmci_fifo_update(PXA2xxMMCIState
*s
)
122 if (s
->cmdat
& CMDAT_WR_RD
) {
123 while (s
->bytesleft
&& s
->tx_len
) {
124 sd_write_data(s
->card
, s
->tx_fifo
[s
->tx_start
++]);
130 s
->intreq
|= INT_TXFIFO_REQ
;
132 while (s
->bytesleft
&& s
->rx_len
< 32) {
133 s
->rx_fifo
[(s
->rx_start
+ (s
->rx_len
++)) & 0x1f] =
134 sd_read_data(s
->card
);
136 s
->intreq
|= INT_RXFIFO_REQ
;
141 s
->intreq
|= INT_DATA_DONE
;
142 s
->status
|= STAT_DATA_DONE
;
144 if (s
->cmdat
& CMDAT_WR_RD
) {
145 s
->intreq
|= INT_PRG_DONE
;
146 s
->status
|= STAT_PRG_DONE
;
150 pxa2xx_mmci_int_update(s
);
153 static void pxa2xx_mmci_wakequeues(PXA2xxMMCIState
*s
)
157 uint8_t response
[16];
164 request
.cmd
= s
->cmd
;
165 request
.arg
= s
->arg
;
166 request
.crc
= 0; /* FIXME */
168 rsplen
= sd_do_command(s
->card
, &request
, response
);
169 s
->intreq
|= INT_END_CMD
;
171 memset(s
->resp_fifo
, 0, sizeof(s
->resp_fifo
));
172 switch (s
->cmdat
& CMDAT_RES_TYPE
) {
173 #define PXAMMCI_RESP(wd, value0, value1) \
174 s->resp_fifo[(wd) + 0] |= (value0); \
175 s->resp_fifo[(wd) + 1] |= (value1) << 8;
176 case 0: /* No response */
179 case 1: /* R1, R4, R5 or R6 */
195 for (i
= 0; rsplen
> 0; i
++, rsplen
-= 2) {
196 PXAMMCI_RESP(i
, response
[i
* 2], response
[i
* 2 + 1]);
198 s
->status
|= STAT_END_CMDRES
;
200 if (!(s
->cmdat
& CMDAT_DATA_EN
))
203 s
->bytesleft
= s
->numblk
* s
->blklen
;
210 s
->status
|= STAT_TOUT_RES
;
214 pxa2xx_mmci_fifo_update(s
);
217 static uint64_t pxa2xx_mmci_read(void *opaque
, hwaddr offset
, unsigned size
)
219 PXA2xxMMCIState
*s
= (PXA2xxMMCIState
*) opaque
;
248 return s
->cmd
| 0x40;
252 return s
->arg
& 0xffff;
255 return s
->resp_fifo
[s
->resp_len
++];
259 while (size
-- && s
->rx_len
) {
260 ret
|= s
->rx_fifo
[s
->rx_start
++] << (size
<< 3);
264 s
->intreq
&= ~INT_RXFIFO_REQ
;
265 pxa2xx_mmci_fifo_update(s
);
272 hw_error("%s: Bad offset " REG_FMT
"\n", __FUNCTION__
, offset
);
278 static void pxa2xx_mmci_write(void *opaque
,
279 hwaddr offset
, uint64_t value
, unsigned size
)
281 PXA2xxMMCIState
*s
= (PXA2xxMMCIState
*) opaque
;
285 if (value
& STRPCL_STRT_CLK
) {
286 s
->status
|= STAT_CLK_EN
;
287 s
->intreq
&= ~INT_CLK_OFF
;
289 if (s
->cmdreq
&& !(s
->cmdat
& CMDAT_STOP_TRAN
)) {
290 s
->status
&= STAT_CLK_EN
;
291 pxa2xx_mmci_wakequeues(s
);
295 if (value
& STRPCL_STOP_CLK
) {
296 s
->status
&= ~STAT_CLK_EN
;
297 s
->intreq
|= INT_CLK_OFF
;
301 pxa2xx_mmci_int_update(s
);
305 s
->clkrt
= value
& 7;
309 s
->spi
= value
& 0xf;
310 if (value
& SPI_SPI_MODE
)
311 printf("%s: attempted to use card in SPI mode\n", __FUNCTION__
);
315 s
->cmdat
= value
& 0x3dff;
318 if (!(value
& CMDAT_STOP_TRAN
)) {
319 s
->status
&= STAT_CLK_EN
;
321 if (s
->status
& STAT_CLK_EN
)
322 pxa2xx_mmci_wakequeues(s
);
325 pxa2xx_mmci_int_update(s
);
329 s
->resp_tout
= value
& 0x7f;
333 s
->read_tout
= value
& 0xffff;
337 s
->blklen
= value
& 0xfff;
341 s
->numblk
= value
& 0xffff;
345 if (value
& PRTBUF_PRT_BUF
) {
349 pxa2xx_mmci_fifo_update(s
);
353 s
->intmask
= value
& 0x1fff;
354 pxa2xx_mmci_int_update(s
);
358 s
->cmd
= value
& 0x3f;
362 s
->arg
&= 0x0000ffff;
363 s
->arg
|= value
<< 16;
367 s
->arg
&= 0xffff0000;
368 s
->arg
|= value
& 0x0000ffff;
372 while (size
-- && s
->tx_len
< 0x20)
373 s
->tx_fifo
[(s
->tx_start
+ (s
->tx_len
++)) & 0x1f] =
374 (value
>> (size
<< 3)) & 0xff;
375 s
->intreq
&= ~INT_TXFIFO_REQ
;
376 pxa2xx_mmci_fifo_update(s
);
384 hw_error("%s: Bad offset " REG_FMT
"\n", __FUNCTION__
, offset
);
388 static const MemoryRegionOps pxa2xx_mmci_ops
= {
389 .read
= pxa2xx_mmci_read
,
390 .write
= pxa2xx_mmci_write
,
391 .endianness
= DEVICE_NATIVE_ENDIAN
,
394 static void pxa2xx_mmci_save(QEMUFile
*f
, void *opaque
)
396 PXA2xxMMCIState
*s
= (PXA2xxMMCIState
*) opaque
;
399 qemu_put_be32s(f
, &s
->status
);
400 qemu_put_be32s(f
, &s
->clkrt
);
401 qemu_put_be32s(f
, &s
->spi
);
402 qemu_put_be32s(f
, &s
->cmdat
);
403 qemu_put_be32s(f
, &s
->resp_tout
);
404 qemu_put_be32s(f
, &s
->read_tout
);
405 qemu_put_be32(f
, s
->blklen
);
406 qemu_put_be32(f
, s
->numblk
);
407 qemu_put_be32s(f
, &s
->intmask
);
408 qemu_put_be32s(f
, &s
->intreq
);
409 qemu_put_be32(f
, s
->cmd
);
410 qemu_put_be32s(f
, &s
->arg
);
411 qemu_put_be32(f
, s
->cmdreq
);
412 qemu_put_be32(f
, s
->active
);
413 qemu_put_be32(f
, s
->bytesleft
);
415 qemu_put_byte(f
, s
->tx_len
);
416 for (i
= 0; i
< s
->tx_len
; i
++)
417 qemu_put_byte(f
, s
->tx_fifo
[(s
->tx_start
+ i
) & 63]);
419 qemu_put_byte(f
, s
->rx_len
);
420 for (i
= 0; i
< s
->rx_len
; i
++)
421 qemu_put_byte(f
, s
->rx_fifo
[(s
->rx_start
+ i
) & 31]);
423 qemu_put_byte(f
, s
->resp_len
);
424 for (i
= s
->resp_len
; i
< 9; i
++)
425 qemu_put_be16s(f
, &s
->resp_fifo
[i
]);
428 static int pxa2xx_mmci_load(QEMUFile
*f
, void *opaque
, int version_id
)
430 PXA2xxMMCIState
*s
= (PXA2xxMMCIState
*) opaque
;
433 qemu_get_be32s(f
, &s
->status
);
434 qemu_get_be32s(f
, &s
->clkrt
);
435 qemu_get_be32s(f
, &s
->spi
);
436 qemu_get_be32s(f
, &s
->cmdat
);
437 qemu_get_be32s(f
, &s
->resp_tout
);
438 qemu_get_be32s(f
, &s
->read_tout
);
439 s
->blklen
= qemu_get_be32(f
);
440 s
->numblk
= qemu_get_be32(f
);
441 qemu_get_be32s(f
, &s
->intmask
);
442 qemu_get_be32s(f
, &s
->intreq
);
443 s
->cmd
= qemu_get_be32(f
);
444 qemu_get_be32s(f
, &s
->arg
);
445 s
->cmdreq
= qemu_get_be32(f
);
446 s
->active
= qemu_get_be32(f
);
447 s
->bytesleft
= qemu_get_be32(f
);
449 s
->tx_len
= qemu_get_byte(f
);
451 if (s
->tx_len
>= sizeof(s
->tx_fifo
) || s
->tx_len
< 0)
453 for (i
= 0; i
< s
->tx_len
; i
++)
454 s
->tx_fifo
[i
] = qemu_get_byte(f
);
456 s
->rx_len
= qemu_get_byte(f
);
458 if (s
->rx_len
>= sizeof(s
->rx_fifo
) || s
->rx_len
< 0)
460 for (i
= 0; i
< s
->rx_len
; i
++)
461 s
->rx_fifo
[i
] = qemu_get_byte(f
);
463 s
->resp_len
= qemu_get_byte(f
);
464 if (s
->resp_len
> 9 || s
->resp_len
< 0)
466 for (i
= s
->resp_len
; i
< 9; i
++)
467 qemu_get_be16s(f
, &s
->resp_fifo
[i
]);
472 PXA2xxMMCIState
*pxa2xx_mmci_init(MemoryRegion
*sysmem
,
474 BlockBackend
*blk
, qemu_irq irq
,
475 qemu_irq rx_dma
, qemu_irq tx_dma
)
479 s
= (PXA2xxMMCIState
*) g_malloc0(sizeof(PXA2xxMMCIState
));
484 memory_region_init_io(&s
->iomem
, NULL
, &pxa2xx_mmci_ops
, s
,
485 "pxa2xx-mmci", 0x00100000);
486 memory_region_add_subregion(sysmem
, base
, &s
->iomem
);
488 /* Instantiate the actual storage */
489 s
->card
= sd_init(blk
, false);
490 if (s
->card
== NULL
) {
494 register_savevm(NULL
, "pxa2xx_mmci", 0, 0,
495 pxa2xx_mmci_save
, pxa2xx_mmci_load
, s
);
500 void pxa2xx_mmci_handlers(PXA2xxMMCIState
*s
, qemu_irq readonly
,
501 qemu_irq coverswitch
)
503 sd_set_cb(s
->card
, readonly
, coverswitch
);