2 * OMAP on-chip MMC/SD host emulation.
4 * Copyright (C) 2006-2007 Andrzej Zaborowski <balrog@zabor.org>
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License as
8 * published by the Free Software Foundation; either version 2 or
9 * (at your option) version 3 of the License.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, see <http://www.gnu.org/licenses/>.
48 uint16_t blen_counter
;
50 uint16_t nblk_counter
;
65 static void omap_mmc_interrupts_update(struct omap_mmc_s
*s
)
67 qemu_set_irq(s
->irq
, !!(s
->status
& s
->mask
));
70 static void omap_mmc_fifolevel_update(struct omap_mmc_s
*host
)
72 if (!host
->transfer
&& !host
->fifo_len
) {
73 host
->status
&= 0xf3ff;
77 if (host
->fifo_len
> host
->af_level
&& host
->ddir
) {
79 host
->status
&= 0xfbff;
80 qemu_irq_raise(host
->dma
[1]);
82 host
->status
|= 0x0400;
84 host
->status
&= 0xfbff;
85 qemu_irq_lower(host
->dma
[1]);
88 if (host
->fifo_len
< host
->ae_level
&& !host
->ddir
) {
90 host
->status
&= 0xf7ff;
91 qemu_irq_raise(host
->dma
[0]);
93 host
->status
|= 0x0800;
95 qemu_irq_lower(host
->dma
[0]);
96 host
->status
&= 0xf7ff;
101 sd_nore
= 0, /* no response */
102 sd_r1
, /* normal response command */
103 sd_r2
, /* CID, CSD registers */
104 sd_r3
, /* OCR register */
105 sd_r6
= 6, /* Published RCA response */
109 static void omap_mmc_command(struct omap_mmc_s
*host
, int cmd
, int dir
,
110 sd_cmd_type_t type
, int busy
, sd_rsp_type_t resptype
, int init
)
112 uint32_t rspstatus
, mask
;
115 uint8_t response
[16];
117 if (init
&& cmd
== 0) {
118 host
->status
|= 0x0001;
122 if (resptype
== sd_r1
&& busy
)
125 if (type
== sd_adtc
) {
126 host
->fifo_start
= 0;
137 request
.arg
= host
->arg
;
138 request
.crc
= 0; /* FIXME */
140 rsplen
= sd_do_command(host
->card
, &request
, response
);
142 /* TODO: validate CRCs */
156 mask
= OUT_OF_RANGE
| ADDRESS_ERROR
| BLOCK_LEN_ERROR
|
157 ERASE_SEQ_ERROR
| ERASE_PARAM
| WP_VIOLATION
|
158 LOCK_UNLOCK_FAILED
| COM_CRC_ERROR
| ILLEGAL_COMMAND
|
159 CARD_ECC_FAILED
| CC_ERROR
| SD_ERROR
|
161 if (host
->sdio
& (1 << 13))
162 mask
|= AKE_SEQ_ERROR
;
163 rspstatus
= (response
[0] << 24) | (response
[1] << 16) |
164 (response
[2] << 8) | (response
[3] << 0);
182 rspstatus
= (response
[0] << 24) | (response
[1] << 16) |
183 (response
[2] << 8) | (response
[3] << 0);
184 if (rspstatus
& 0x80000000)
185 host
->status
&= 0xe000;
187 host
->status
|= 0x1000;
197 mask
= 0xe000 | AKE_SEQ_ERROR
;
198 rspstatus
= (response
[2] << 8) | (response
[3] << 0);
201 if (rspstatus
& mask
)
202 host
->status
|= 0x4000;
204 host
->status
&= 0xb000;
207 for (rsplen
= 0; rsplen
< 8; rsplen
++)
208 host
->rsp
[~rsplen
& 7] = response
[(rsplen
<< 1) | 1] |
209 (response
[(rsplen
<< 1) | 0] << 8);
212 host
->status
|= 0x0080;
214 host
->status
|= 0x0005; /* Makes it more real */
216 host
->status
|= 0x0001;
219 static void omap_mmc_transfer(struct omap_mmc_s
*host
)
228 if (host
->fifo_len
> host
->af_level
)
231 value
= sd_read_data(host
->card
);
232 host
->fifo
[(host
->fifo_start
+ host
->fifo_len
) & 31] = value
;
233 if (-- host
->blen_counter
) {
234 value
= sd_read_data(host
->card
);
235 host
->fifo
[(host
->fifo_start
+ host
->fifo_len
) & 31] |=
237 host
->blen_counter
--;
245 value
= host
->fifo
[host
->fifo_start
] & 0xff;
246 sd_write_data(host
->card
, value
);
247 if (-- host
->blen_counter
) {
248 value
= host
->fifo
[host
->fifo_start
] >> 8;
249 sd_write_data(host
->card
, value
);
250 host
->blen_counter
--;
255 host
->fifo_start
&= 31;
258 if (host
->blen_counter
== 0) {
259 host
->nblk_counter
--;
260 host
->blen_counter
= host
->blen
;
262 if (host
->nblk_counter
== 0) {
263 host
->nblk_counter
= host
->nblk
;
265 host
->status
|= 0x0008;
272 static void omap_mmc_update(void *opaque
)
274 struct omap_mmc_s
*s
= opaque
;
275 omap_mmc_transfer(s
);
276 omap_mmc_fifolevel_update(s
);
277 omap_mmc_interrupts_update(s
);
280 void omap_mmc_reset(struct omap_mmc_s
*host
)
283 memset(host
->rsp
, 0, sizeof(host
->rsp
));
294 host
->blen_counter
= 0;
296 host
->nblk_counter
= 0;
299 host
->ae_level
= 0x00;
300 host
->af_level
= 0x1f;
302 host
->cdet_wakeup
= 0;
303 host
->cdet_enable
= 0;
304 qemu_set_irq(host
->coverswitch
, host
->cdet_state
);
308 static uint32_t omap_mmc_read(void *opaque
, target_phys_addr_t offset
)
311 struct omap_mmc_s
*s
= (struct omap_mmc_s
*) opaque
;
312 offset
&= OMAP_MPUI_REG_MASK
;
315 case 0x00: /* MMC_CMD */
318 case 0x04: /* MMC_ARGL */
319 return s
->arg
& 0x0000ffff;
321 case 0x08: /* MMC_ARGH */
324 case 0x0c: /* MMC_CON */
325 return (s
->dw
<< 15) | (s
->mode
<< 12) | (s
->enable
<< 11) |
326 (s
->be
<< 10) | s
->clkdiv
;
328 case 0x10: /* MMC_STAT */
331 case 0x14: /* MMC_IE */
334 case 0x18: /* MMC_CTO */
337 case 0x1c: /* MMC_DTO */
340 case 0x20: /* MMC_DATA */
341 /* TODO: support 8-bit access */
342 i
= s
->fifo
[s
->fifo_start
];
343 if (s
->fifo_len
== 0) {
344 printf("MMC: FIFO underrun\n");
350 omap_mmc_transfer(s
);
351 omap_mmc_fifolevel_update(s
);
352 omap_mmc_interrupts_update(s
);
355 case 0x24: /* MMC_BLEN */
356 return s
->blen_counter
;
358 case 0x28: /* MMC_NBLK */
359 return s
->nblk_counter
;
361 case 0x2c: /* MMC_BUF */
362 return (s
->rx_dma
<< 15) | (s
->af_level
<< 8) |
363 (s
->tx_dma
<< 7) | s
->ae_level
;
365 case 0x30: /* MMC_SPI */
367 case 0x34: /* MMC_SDIO */
368 return (s
->cdet_wakeup
<< 2) | (s
->cdet_enable
) | s
->sdio
;
369 case 0x38: /* MMC_SYST */
372 case 0x3c: /* MMC_REV */
375 case 0x40: /* MMC_RSP0 */
376 case 0x44: /* MMC_RSP1 */
377 case 0x48: /* MMC_RSP2 */
378 case 0x4c: /* MMC_RSP3 */
379 case 0x50: /* MMC_RSP4 */
380 case 0x54: /* MMC_RSP5 */
381 case 0x58: /* MMC_RSP6 */
382 case 0x5c: /* MMC_RSP7 */
383 return s
->rsp
[(offset
- 0x40) >> 2];
386 case 0x60: /* MMC_IOSR */
387 case 0x64: /* MMC_SYSC */
389 case 0x68: /* MMC_SYSS */
393 OMAP_BAD_REG(offset
);
397 static void omap_mmc_write(void *opaque
, target_phys_addr_t offset
,
401 struct omap_mmc_s
*s
= (struct omap_mmc_s
*) opaque
;
402 offset
&= OMAP_MPUI_REG_MASK
;
405 case 0x00: /* MMC_CMD */
410 for (i
= 0; i
< 8; i
++)
412 omap_mmc_command(s
, value
& 63, (value
>> 15) & 1,
413 (sd_cmd_type_t
) ((value
>> 12) & 3),
415 (sd_rsp_type_t
) ((value
>> 8) & 7),
420 case 0x04: /* MMC_ARGL */
421 s
->arg
&= 0xffff0000;
422 s
->arg
|= 0x0000ffff & value
;
425 case 0x08: /* MMC_ARGH */
426 s
->arg
&= 0x0000ffff;
427 s
->arg
|= value
<< 16;
430 case 0x0c: /* MMC_CON */
431 s
->dw
= (value
>> 15) & 1;
432 s
->mode
= (value
>> 12) & 3;
433 s
->enable
= (value
>> 11) & 1;
434 s
->be
= (value
>> 10) & 1;
435 s
->clkdiv
= (value
>> 0) & (s
->rev
>= 2 ? 0x3ff : 0xff);
437 printf("SD mode %i unimplemented!\n", s
->mode
);
439 printf("SD FIFO byte sex unimplemented!\n");
440 if (s
->dw
!= 0 && s
->lines
< 4)
441 printf("4-bit SD bus enabled\n");
446 case 0x10: /* MMC_STAT */
448 omap_mmc_interrupts_update(s
);
451 case 0x14: /* MMC_IE */
452 s
->mask
= value
& 0x7fff;
453 omap_mmc_interrupts_update(s
);
456 case 0x18: /* MMC_CTO */
457 s
->cto
= value
& 0xff;
458 if (s
->cto
> 0xfd && s
->rev
<= 1)
459 printf("MMC: CTO of 0xff and 0xfe cannot be used!\n");
462 case 0x1c: /* MMC_DTO */
463 s
->dto
= value
& 0xffff;
466 case 0x20: /* MMC_DATA */
467 /* TODO: support 8-bit access */
468 if (s
->fifo_len
== 32)
470 s
->fifo
[(s
->fifo_start
+ s
->fifo_len
) & 31] = value
;
472 omap_mmc_transfer(s
);
473 omap_mmc_fifolevel_update(s
);
474 omap_mmc_interrupts_update(s
);
477 case 0x24: /* MMC_BLEN */
478 s
->blen
= (value
& 0x07ff) + 1;
479 s
->blen_counter
= s
->blen
;
482 case 0x28: /* MMC_NBLK */
483 s
->nblk
= (value
& 0x07ff) + 1;
484 s
->nblk_counter
= s
->nblk
;
485 s
->blen_counter
= s
->blen
;
488 case 0x2c: /* MMC_BUF */
489 s
->rx_dma
= (value
>> 15) & 1;
490 s
->af_level
= (value
>> 8) & 0x1f;
491 s
->tx_dma
= (value
>> 7) & 1;
492 s
->ae_level
= value
& 0x1f;
498 omap_mmc_fifolevel_update(s
);
499 omap_mmc_interrupts_update(s
);
502 /* SPI, SDIO and TEST modes unimplemented */
503 case 0x30: /* MMC_SPI (OMAP1 only) */
505 case 0x34: /* MMC_SDIO */
506 s
->sdio
= value
& (s
->rev
>= 2 ? 0xfbf3 : 0x2020);
507 s
->cdet_wakeup
= (value
>> 9) & 1;
508 s
->cdet_enable
= (value
>> 2) & 1;
510 case 0x38: /* MMC_SYST */
513 case 0x3c: /* MMC_REV */
514 case 0x40: /* MMC_RSP0 */
515 case 0x44: /* MMC_RSP1 */
516 case 0x48: /* MMC_RSP2 */
517 case 0x4c: /* MMC_RSP3 */
518 case 0x50: /* MMC_RSP4 */
519 case 0x54: /* MMC_RSP5 */
520 case 0x58: /* MMC_RSP6 */
521 case 0x5c: /* MMC_RSP7 */
526 case 0x60: /* MMC_IOSR */
528 printf("MMC: SDIO bits used!\n");
530 case 0x64: /* MMC_SYSC */
531 if (value
& (1 << 2)) /* SRTS */
534 case 0x68: /* MMC_SYSS */
539 OMAP_BAD_REG(offset
);
543 static CPUReadMemoryFunc
* const omap_mmc_readfn
[] = {
544 omap_badwidth_read16
,
546 omap_badwidth_read16
,
549 static CPUWriteMemoryFunc
* const omap_mmc_writefn
[] = {
550 omap_badwidth_write16
,
552 omap_badwidth_write16
,
555 static void omap_mmc_cover_cb(void *opaque
, int line
, int level
)
557 struct omap_mmc_s
*host
= (struct omap_mmc_s
*) opaque
;
559 if (!host
->cdet_state
&& level
) {
560 host
->status
|= 0x0002;
561 omap_mmc_interrupts_update(host
);
562 if (host
->cdet_wakeup
)
563 /* TODO: Assert wake-up */;
566 if (host
->cdet_state
!= level
) {
567 qemu_set_irq(host
->coverswitch
, level
);
568 host
->cdet_state
= level
;
572 struct omap_mmc_s
*omap_mmc_init(target_phys_addr_t base
,
573 BlockDriverState
*bd
,
574 qemu_irq irq
, qemu_irq dma
[], omap_clk clk
)
577 struct omap_mmc_s
*s
= (struct omap_mmc_s
*)
578 qemu_mallocz(sizeof(struct omap_mmc_s
));
583 s
->lines
= 1; /* TODO: needs to be settable per-board */
588 iomemtype
= cpu_register_io_memory(omap_mmc_readfn
,
589 omap_mmc_writefn
, s
);
590 cpu_register_physical_memory(base
, 0x800, iomemtype
);
592 /* Instantiate the storage */
593 s
->card
= sd_init(bd
, 0);
598 struct omap_mmc_s
*omap2_mmc_init(struct omap_target_agent_s
*ta
,
599 BlockDriverState
*bd
, qemu_irq irq
, qemu_irq dma
[],
600 omap_clk fclk
, omap_clk iclk
)
603 struct omap_mmc_s
*s
= (struct omap_mmc_s
*)
604 qemu_mallocz(sizeof(struct omap_mmc_s
));
614 iomemtype
= l4_register_io_memory(omap_mmc_readfn
,
615 omap_mmc_writefn
, s
);
616 omap_l4_attach(ta
, 0, iomemtype
);
618 /* Instantiate the storage */
619 s
->card
= sd_init(bd
, 0);
621 s
->cdet
= qemu_allocate_irqs(omap_mmc_cover_cb
, s
, 1)[0];
622 sd_set_cb(s
->card
, NULL
, s
->cdet
);
627 void omap_mmc_handlers(struct omap_mmc_s
*s
, qemu_irq ro
, qemu_irq cover
)
630 sd_set_cb(s
->card
, ro
, s
->cdet
);
631 s
->coverswitch
= cover
;
632 qemu_set_irq(cover
, s
->cdet_state
);
634 sd_set_cb(s
->card
, ro
, cover
);
637 void omap_mmc_enable(struct omap_mmc_s
*s
, int enable
)
639 sd_enable(s
->card
, enable
);