2 * SD Memory Card emulation as defined in the "SD Memory Card Physical
3 * layer specification, Version 1.10."
5 * Copyright (c) 2006 Andrzej Zaborowski <balrog@zabor.org>
6 * Copyright (c) 2007 CodeSourcery
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in
16 * the documentation and/or other materials provided with the
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS''
20 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
21 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
22 * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR
23 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
24 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
25 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
26 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
27 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
40 #define DPRINTF(fmt, ...) \
41 do { fprintf(stderr, "SD: " fmt , ## __VA_ARGS__); } while (0)
43 #define DPRINTF(fmt, ...) do {} while(0)
47 sd_r0
= 0, /* no response */
48 sd_r1
, /* normal response command */
49 sd_r2_i
, /* CID register */
50 sd_r2_s
, /* CSD register */
51 sd_r3
, /* OCR register */
52 sd_r6
= 6, /* Published RCA response */
53 sd_r7
, /* Operating voltage */
61 sd_card_identification_mode
,
62 sd_data_transfer_mode
,
65 sd_inactive_state
= -1,
68 sd_identification_state
,
72 sd_receivingdata_state
,
82 uint8_t sd_status
[64];
85 unsigned long *wp_groups
;
92 int function_group
[6];
96 /* True if we will handle the next command as an ACMD. Note that this does
97 * *not* track the APP_CMD status bit!
102 uint32_t data_offset
;
104 qemu_irq readonly_cb
;
105 qemu_irq inserted_cb
;
106 BlockDriverState
*bdrv
;
112 static void sd_set_mode(SDState
*sd
)
115 case sd_inactive_state
:
116 sd
->mode
= sd_inactive
;
121 case sd_identification_state
:
122 sd
->mode
= sd_card_identification_mode
;
125 case sd_standby_state
:
126 case sd_transfer_state
:
127 case sd_sendingdata_state
:
128 case sd_receivingdata_state
:
129 case sd_programming_state
:
130 case sd_disconnect_state
:
131 sd
->mode
= sd_data_transfer_mode
;
136 static const sd_cmd_type_t sd_cmd_type
[64] = {
137 sd_bc
, sd_none
, sd_bcr
, sd_bcr
, sd_none
, sd_none
, sd_none
, sd_ac
,
138 sd_bcr
, sd_ac
, sd_ac
, sd_adtc
, sd_ac
, sd_ac
, sd_none
, sd_ac
,
139 sd_ac
, sd_adtc
, sd_adtc
, sd_none
, sd_none
, sd_none
, sd_none
, sd_none
,
140 sd_adtc
, sd_adtc
, sd_adtc
, sd_adtc
, sd_ac
, sd_ac
, sd_adtc
, sd_none
,
141 sd_ac
, sd_ac
, sd_none
, sd_none
, sd_none
, sd_none
, sd_ac
, sd_none
,
142 sd_none
, sd_none
, sd_bc
, sd_none
, sd_none
, sd_none
, sd_none
, sd_none
,
143 sd_none
, sd_none
, sd_none
, sd_none
, sd_none
, sd_none
, sd_none
, sd_ac
,
144 sd_adtc
, sd_none
, sd_none
, sd_none
, sd_none
, sd_none
, sd_none
, sd_none
,
147 static const sd_cmd_type_t sd_acmd_type
[64] = {
148 sd_none
, sd_none
, sd_none
, sd_none
, sd_none
, sd_none
, sd_ac
, sd_none
,
149 sd_none
, sd_none
, sd_none
, sd_none
, sd_none
, sd_adtc
, sd_none
, sd_none
,
150 sd_none
, sd_none
, sd_none
, sd_none
, sd_none
, sd_none
, sd_adtc
, sd_ac
,
151 sd_none
, sd_none
, sd_none
, sd_none
, sd_none
, sd_none
, sd_none
, sd_none
,
152 sd_none
, sd_none
, sd_none
, sd_none
, sd_none
, sd_none
, sd_none
, sd_none
,
153 sd_none
, sd_bcr
, sd_ac
, sd_none
, sd_none
, sd_none
, sd_none
, sd_none
,
154 sd_none
, sd_none
, sd_none
, sd_adtc
, sd_none
, sd_none
, sd_none
, sd_none
,
155 sd_none
, sd_none
, sd_none
, sd_none
, sd_none
, sd_none
, sd_none
, sd_none
,
158 static const int sd_cmd_class
[64] = {
159 0, 0, 0, 0, 0, 9, 10, 0, 0, 0, 0, 1, 0, 0, 0, 0,
160 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 6, 6, 6, 6,
161 5, 5, 10, 10, 10, 10, 5, 9, 9, 9, 7, 7, 7, 7, 7, 7,
162 7, 7, 10, 7, 9, 9, 9, 8, 8, 10, 8, 8, 8, 8, 8, 8,
165 static uint8_t sd_crc7(void *message
, size_t width
)
168 uint8_t shift_reg
= 0x00;
169 uint8_t *msg
= (uint8_t *) message
;
171 for (i
= 0; i
< width
; i
++, msg
++)
172 for (bit
= 7; bit
>= 0; bit
--) {
174 if ((shift_reg
>> 7) ^ ((*msg
>> bit
) & 1))
181 static uint16_t sd_crc16(void *message
, size_t width
)
184 uint16_t shift_reg
= 0x0000;
185 uint16_t *msg
= (uint16_t *) message
;
188 for (i
= 0; i
< width
; i
++, msg
++)
189 for (bit
= 15; bit
>= 0; bit
--) {
191 if ((shift_reg
>> 15) ^ ((*msg
>> bit
) & 1))
198 static void sd_set_ocr(SDState
*sd
)
200 /* All voltages OK, card power-up OK, Standard Capacity SD Memory Card */
201 sd
->ocr
= 0x80ffff00;
204 static void sd_set_scr(SDState
*sd
)
206 sd
->scr
[0] = 0x00; /* SCR Structure */
207 sd
->scr
[1] = 0x2f; /* SD Security Support */
223 static void sd_set_cid(SDState
*sd
)
225 sd
->cid
[0] = MID
; /* Fake card manufacturer ID (MID) */
226 sd
->cid
[1] = OID
[0]; /* OEM/Application ID (OID) */
228 sd
->cid
[3] = PNM
[0]; /* Fake product name (PNM) */
233 sd
->cid
[8] = PRV
; /* Fake product revision (PRV) */
234 sd
->cid
[9] = 0xde; /* Fake serial number (PSN) */
238 sd
->cid
[13] = 0x00 | /* Manufacture date (MDT) */
239 ((MDT_YR
- 2000) / 10);
240 sd
->cid
[14] = ((MDT_YR
% 10) << 4) | MDT_MON
;
241 sd
->cid
[15] = (sd_crc7(sd
->cid
, 15) << 1) | 1;
244 #define HWBLOCK_SHIFT 9 /* 512 bytes */
245 #define SECTOR_SHIFT 5 /* 16 kilobytes */
246 #define WPGROUP_SHIFT 7 /* 2 megs */
247 #define CMULT_SHIFT 9 /* 512 times HWBLOCK_SIZE */
248 #define WPGROUP_SIZE (1 << (HWBLOCK_SHIFT + SECTOR_SHIFT + WPGROUP_SHIFT))
250 static const uint8_t sd_csd_rw_mask
[16] = {
251 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
252 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xfc, 0xfe,
255 static void sd_set_csd(SDState
*sd
, uint64_t size
)
257 uint32_t csize
= (size
>> (CMULT_SHIFT
+ HWBLOCK_SHIFT
)) - 1;
258 uint32_t sectsize
= (1 << (SECTOR_SHIFT
+ 1)) - 1;
259 uint32_t wpsize
= (1 << (WPGROUP_SHIFT
+ 1)) - 1;
261 if (size
<= 0x40000000) { /* Standard Capacity SD */
262 sd
->csd
[0] = 0x00; /* CSD structure */
263 sd
->csd
[1] = 0x26; /* Data read access-time-1 */
264 sd
->csd
[2] = 0x00; /* Data read access-time-2 */
265 sd
->csd
[3] = 0x5a; /* Max. data transfer rate */
266 sd
->csd
[4] = 0x5f; /* Card Command Classes */
267 sd
->csd
[5] = 0x50 | /* Max. read data block length */
269 sd
->csd
[6] = 0xe0 | /* Partial block for read allowed */
270 ((csize
>> 10) & 0x03);
271 sd
->csd
[7] = 0x00 | /* Device size */
272 ((csize
>> 2) & 0xff);
273 sd
->csd
[8] = 0x3f | /* Max. read current */
274 ((csize
<< 6) & 0xc0);
275 sd
->csd
[9] = 0xfc | /* Max. write current */
276 ((CMULT_SHIFT
- 2) >> 1);
277 sd
->csd
[10] = 0x40 | /* Erase sector size */
278 (((CMULT_SHIFT
- 2) << 7) & 0x80) | (sectsize
>> 1);
279 sd
->csd
[11] = 0x00 | /* Write protect group size */
280 ((sectsize
<< 7) & 0x80) | wpsize
;
281 sd
->csd
[12] = 0x90 | /* Write speed factor */
282 (HWBLOCK_SHIFT
>> 2);
283 sd
->csd
[13] = 0x20 | /* Max. write data block length */
284 ((HWBLOCK_SHIFT
<< 6) & 0xc0);
285 sd
->csd
[14] = 0x00; /* File format group */
286 sd
->csd
[15] = (sd_crc7(sd
->csd
, 15) << 1) | 1;
297 sd
->csd
[7] = (size
>> 16) & 0xff;
298 sd
->csd
[8] = (size
>> 8) & 0xff;
299 sd
->csd
[9] = (size
& 0xff);
306 sd
->ocr
|= 1 << 30; /* High Capacity SD Memort Card */
310 static void sd_set_rca(SDState
*sd
)
315 /* Card status bits, split by clear condition:
316 * A : According to the card current state
317 * B : Always related to the previous command
318 * C : Cleared by read
320 #define CARD_STATUS_A 0x02004100
321 #define CARD_STATUS_B 0x00c01e00
322 #define CARD_STATUS_C 0xfd39a028
324 static void sd_set_cardstatus(SDState
*sd
)
326 sd
->card_status
= 0x00000100;
329 static void sd_set_sdstatus(SDState
*sd
)
331 memset(sd
->sd_status
, 0, 64);
334 static int sd_req_crc_validate(SDRequest
*req
)
337 buffer
[0] = 0x40 | req
->cmd
;
338 buffer
[1] = (req
->arg
>> 24) & 0xff;
339 buffer
[2] = (req
->arg
>> 16) & 0xff;
340 buffer
[3] = (req
->arg
>> 8) & 0xff;
341 buffer
[4] = (req
->arg
>> 0) & 0xff;
343 return sd_crc7(buffer
, 5) != req
->crc
; /* TODO */
346 static void sd_response_r1_make(SDState
*sd
, uint8_t *response
)
348 uint32_t status
= sd
->card_status
;
349 /* Clear the "clear on read" status bits */
350 sd
->card_status
&= ~CARD_STATUS_C
;
352 response
[0] = (status
>> 24) & 0xff;
353 response
[1] = (status
>> 16) & 0xff;
354 response
[2] = (status
>> 8) & 0xff;
355 response
[3] = (status
>> 0) & 0xff;
358 static void sd_response_r3_make(SDState
*sd
, uint8_t *response
)
360 response
[0] = (sd
->ocr
>> 24) & 0xff;
361 response
[1] = (sd
->ocr
>> 16) & 0xff;
362 response
[2] = (sd
->ocr
>> 8) & 0xff;
363 response
[3] = (sd
->ocr
>> 0) & 0xff;
366 static void sd_response_r6_make(SDState
*sd
, uint8_t *response
)
372 status
= ((sd
->card_status
>> 8) & 0xc000) |
373 ((sd
->card_status
>> 6) & 0x2000) |
374 (sd
->card_status
& 0x1fff);
375 sd
->card_status
&= ~(CARD_STATUS_C
& 0xc81fff);
377 response
[0] = (arg
>> 8) & 0xff;
378 response
[1] = arg
& 0xff;
379 response
[2] = (status
>> 8) & 0xff;
380 response
[3] = status
& 0xff;
383 static void sd_response_r7_make(SDState
*sd
, uint8_t *response
)
385 response
[0] = (sd
->vhs
>> 24) & 0xff;
386 response
[1] = (sd
->vhs
>> 16) & 0xff;
387 response
[2] = (sd
->vhs
>> 8) & 0xff;
388 response
[3] = (sd
->vhs
>> 0) & 0xff;
391 static inline uint64_t sd_addr_to_wpnum(uint64_t addr
)
393 return addr
>> (HWBLOCK_SHIFT
+ SECTOR_SHIFT
+ WPGROUP_SHIFT
);
396 static void sd_reset(SDState
*sd
, BlockDriverState
*bdrv
)
402 bdrv_get_geometry(bdrv
, §
);
408 sect
= sd_addr_to_wpnum(size
) + 1;
410 sd
->state
= sd_idle_state
;
415 sd_set_csd(sd
, size
);
416 sd_set_cardstatus(sd
);
422 g_free(sd
->wp_groups
);
423 sd
->wp_switch
= bdrv
? bdrv_is_read_only(bdrv
) : false;
424 sd
->wp_groups
= bitmap_new(sect
);
425 memset(sd
->function_group
, 0, sizeof(int) * 6);
431 sd
->expecting_acmd
= false;
434 static void sd_cardchange(void *opaque
, bool load
)
436 SDState
*sd
= opaque
;
438 qemu_set_irq(sd
->inserted_cb
, bdrv_is_inserted(sd
->bdrv
));
439 if (bdrv_is_inserted(sd
->bdrv
)) {
440 sd_reset(sd
, sd
->bdrv
);
441 qemu_set_irq(sd
->readonly_cb
, sd
->wp_switch
);
445 static const BlockDevOps sd_block_ops
= {
446 .change_media_cb
= sd_cardchange
,
449 /* We do not model the chip select pin, so allow the board to select
450 whether card should be in SSI or MMC/SD mode. It is also up to the
451 board to ensure that ssi transfers only occur when the chip select
453 SDState
*sd_init(BlockDriverState
*bs
, bool is_spi
)
457 sd
= (SDState
*) g_malloc0(sizeof(SDState
));
458 sd
->buf
= qemu_blockalign(bs
, 512);
463 bdrv_attach_dev_nofail(sd
->bdrv
, sd
);
464 bdrv_set_dev_ops(sd
->bdrv
, &sd_block_ops
, sd
);
469 void sd_set_cb(SDState
*sd
, qemu_irq readonly
, qemu_irq insert
)
471 sd
->readonly_cb
= readonly
;
472 sd
->inserted_cb
= insert
;
473 qemu_set_irq(readonly
, sd
->bdrv
? bdrv_is_read_only(sd
->bdrv
) : 0);
474 qemu_set_irq(insert
, sd
->bdrv
? bdrv_is_inserted(sd
->bdrv
) : 0);
477 static void sd_erase(SDState
*sd
)
480 if (!sd
->erase_start
|| !sd
->erase_end
) {
481 sd
->card_status
|= ERASE_SEQ_ERROR
;
485 start
= sd_addr_to_wpnum(sd
->erase_start
);
486 end
= sd_addr_to_wpnum(sd
->erase_end
);
491 for (i
= start
; i
<= end
; i
++) {
492 if (test_bit(i
, sd
->wp_groups
)) {
493 sd
->card_status
|= WP_ERASE_SKIP
;
498 static uint32_t sd_wpbits(SDState
*sd
, uint64_t addr
)
503 wpnum
= sd_addr_to_wpnum(addr
);
505 for (i
= 0; i
< 32; i
++, wpnum
++, addr
+= WPGROUP_SIZE
) {
506 if (addr
< sd
->size
&& test_bit(wpnum
, sd
->wp_groups
)) {
514 static void sd_function_switch(SDState
*sd
, uint32_t arg
)
516 int i
, mode
, new_func
, crc
;
517 mode
= !!(arg
& 0x80000000);
519 sd
->data
[0] = 0x00; /* Maximum current consumption */
521 sd
->data
[2] = 0x80; /* Supported group 6 functions */
523 sd
->data
[4] = 0x80; /* Supported group 5 functions */
525 sd
->data
[6] = 0x80; /* Supported group 4 functions */
527 sd
->data
[8] = 0x80; /* Supported group 3 functions */
529 sd
->data
[10] = 0x80; /* Supported group 2 functions */
531 sd
->data
[12] = 0x80; /* Supported group 1 functions */
533 for (i
= 0; i
< 6; i
++) {
534 new_func
= (arg
>> (i
* 4)) & 0x0f;
535 if (mode
&& new_func
!= 0x0f)
536 sd
->function_group
[i
] = new_func
;
537 sd
->data
[14 + (i
>> 1)] = new_func
<< ((i
* 4) & 4);
539 memset(&sd
->data
[17], 0, 47);
540 crc
= sd_crc16(sd
->data
, 64);
541 sd
->data
[65] = crc
>> 8;
542 sd
->data
[66] = crc
& 0xff;
545 static inline bool sd_wp_addr(SDState
*sd
, uint64_t addr
)
547 return test_bit(sd_addr_to_wpnum(addr
), sd
->wp_groups
);
550 static void sd_lock_command(SDState
*sd
)
552 int erase
, lock
, clr_pwd
, set_pwd
, pwd_len
;
553 erase
= !!(sd
->data
[0] & 0x08);
554 lock
= sd
->data
[0] & 0x04;
555 clr_pwd
= sd
->data
[0] & 0x02;
556 set_pwd
= sd
->data
[0] & 0x01;
559 pwd_len
= sd
->data
[1];
564 if (!(sd
->card_status
& CARD_IS_LOCKED
) || sd
->blk_len
> 1 ||
565 set_pwd
|| clr_pwd
|| lock
|| sd
->wp_switch
||
566 (sd
->csd
[14] & 0x20)) {
567 sd
->card_status
|= LOCK_UNLOCK_FAILED
;
570 bitmap_zero(sd
->wp_groups
, sd_addr_to_wpnum(sd
->size
) + 1);
571 sd
->csd
[14] &= ~0x10;
572 sd
->card_status
&= ~CARD_IS_LOCKED
;
574 /* Erasing the entire card here! */
575 fprintf(stderr
, "SD: Card force-erased by CMD42\n");
579 if (sd
->blk_len
< 2 + pwd_len
||
580 pwd_len
<= sd
->pwd_len
||
581 pwd_len
> sd
->pwd_len
+ 16) {
582 sd
->card_status
|= LOCK_UNLOCK_FAILED
;
586 if (sd
->pwd_len
&& memcmp(sd
->pwd
, sd
->data
+ 2, sd
->pwd_len
)) {
587 sd
->card_status
|= LOCK_UNLOCK_FAILED
;
591 pwd_len
-= sd
->pwd_len
;
592 if ((pwd_len
&& !set_pwd
) ||
593 (clr_pwd
&& (set_pwd
|| lock
)) ||
594 (lock
&& !sd
->pwd_len
&& !set_pwd
) ||
595 (!set_pwd
&& !clr_pwd
&&
596 (((sd
->card_status
& CARD_IS_LOCKED
) && lock
) ||
597 (!(sd
->card_status
& CARD_IS_LOCKED
) && !lock
)))) {
598 sd
->card_status
|= LOCK_UNLOCK_FAILED
;
603 memcpy(sd
->pwd
, sd
->data
+ 2 + sd
->pwd_len
, pwd_len
);
604 sd
->pwd_len
= pwd_len
;
612 sd
->card_status
|= CARD_IS_LOCKED
;
614 sd
->card_status
&= ~CARD_IS_LOCKED
;
617 static sd_rsp_type_t
sd_normal_command(SDState
*sd
,
620 uint32_t rca
= 0x0000;
621 uint64_t addr
= (sd
->ocr
& (1 << 30)) ? (uint64_t) req
.arg
<< 9 : req
.arg
;
623 /* Not interpreting this as an app command */
624 sd
->card_status
&= ~APP_CMD
;
626 if (sd_cmd_type
[req
.cmd
] == sd_ac
|| sd_cmd_type
[req
.cmd
] == sd_adtc
)
629 DPRINTF("CMD%d 0x%08x state %d\n", req
.cmd
, req
.arg
, sd
->state
);
631 /* Basic commands (Class 0 and Class 1) */
632 case 0: /* CMD0: GO_IDLE_STATE */
634 case sd_inactive_state
:
635 return sd
->spi
? sd_r1
: sd_r0
;
638 sd
->state
= sd_idle_state
;
639 sd_reset(sd
, sd
->bdrv
);
640 return sd
->spi
? sd_r1
: sd_r0
;
644 case 1: /* CMD1: SEND_OP_CMD */
648 sd
->state
= sd_transfer_state
;
651 case 2: /* CMD2: ALL_SEND_CID */
656 sd
->state
= sd_identification_state
;
664 case 3: /* CMD3: SEND_RELATIVE_ADDR */
668 case sd_identification_state
:
669 case sd_standby_state
:
670 sd
->state
= sd_standby_state
;
679 case 4: /* CMD4: SEND_DSR */
683 case sd_standby_state
:
691 case 5: /* CMD5: reserved for SDIO cards */
694 case 6: /* CMD6: SWITCH_FUNCTION */
698 case sd_data_transfer_mode
:
699 sd_function_switch(sd
, req
.arg
);
700 sd
->state
= sd_sendingdata_state
;
710 case 7: /* CMD7: SELECT/DESELECT_CARD */
714 case sd_standby_state
:
718 sd
->state
= sd_transfer_state
;
721 case sd_transfer_state
:
722 case sd_sendingdata_state
:
726 sd
->state
= sd_standby_state
;
729 case sd_disconnect_state
:
733 sd
->state
= sd_programming_state
;
736 case sd_programming_state
:
740 sd
->state
= sd_disconnect_state
;
748 case 8: /* CMD8: SEND_IF_COND */
749 /* Physical Layer Specification Version 2.00 command */
754 /* No response if not exactly one VHS bit is set. */
755 if (!(req
.arg
>> 8) || (req
.arg
>> ffs(req
.arg
& ~0xff)))
756 return sd
->spi
? sd_r7
: sd_r0
;
767 case 9: /* CMD9: SEND_CSD */
769 case sd_standby_state
:
775 case sd_transfer_state
:
778 sd
->state
= sd_sendingdata_state
;
779 memcpy(sd
->data
, sd
->csd
, 16);
780 sd
->data_start
= addr
;
789 case 10: /* CMD10: SEND_CID */
791 case sd_standby_state
:
797 case sd_transfer_state
:
800 sd
->state
= sd_sendingdata_state
;
801 memcpy(sd
->data
, sd
->cid
, 16);
802 sd
->data_start
= addr
;
811 case 11: /* CMD11: READ_DAT_UNTIL_STOP */
815 case sd_transfer_state
:
816 sd
->state
= sd_sendingdata_state
;
817 sd
->data_start
= req
.arg
;
820 if (sd
->data_start
+ sd
->blk_len
> sd
->size
)
821 sd
->card_status
|= ADDRESS_ERROR
;
829 case 12: /* CMD12: STOP_TRANSMISSION */
831 case sd_sendingdata_state
:
832 sd
->state
= sd_transfer_state
;
835 case sd_receivingdata_state
:
836 sd
->state
= sd_programming_state
;
837 /* Bzzzzzzztt .... Operation complete. */
838 sd
->state
= sd_transfer_state
;
846 case 13: /* CMD13: SEND_STATUS */
848 case sd_data_transfer_mode
:
859 case 15: /* CMD15: GO_INACTIVE_STATE */
863 case sd_data_transfer_mode
:
867 sd
->state
= sd_inactive_state
;
875 /* Block read commands (Classs 2) */
876 case 16: /* CMD16: SET_BLOCKLEN */
878 case sd_transfer_state
:
879 if (req
.arg
> (1 << HWBLOCK_SHIFT
))
880 sd
->card_status
|= BLOCK_LEN_ERROR
;
882 sd
->blk_len
= req
.arg
;
891 case 17: /* CMD17: READ_SINGLE_BLOCK */
893 case sd_transfer_state
:
894 sd
->state
= sd_sendingdata_state
;
895 sd
->data_start
= addr
;
898 if (sd
->data_start
+ sd
->blk_len
> sd
->size
)
899 sd
->card_status
|= ADDRESS_ERROR
;
907 case 18: /* CMD18: READ_MULTIPLE_BLOCK */
909 case sd_transfer_state
:
910 sd
->state
= sd_sendingdata_state
;
911 sd
->data_start
= addr
;
914 if (sd
->data_start
+ sd
->blk_len
> sd
->size
)
915 sd
->card_status
|= ADDRESS_ERROR
;
923 /* Block write commands (Class 4) */
924 case 24: /* CMD24: WRITE_SINGLE_BLOCK */
926 goto unimplemented_cmd
;
928 case sd_transfer_state
:
929 /* Writing in SPI mode not implemented. */
932 sd
->state
= sd_receivingdata_state
;
933 sd
->data_start
= addr
;
937 if (sd
->data_start
+ sd
->blk_len
> sd
->size
)
938 sd
->card_status
|= ADDRESS_ERROR
;
939 if (sd_wp_addr(sd
, sd
->data_start
))
940 sd
->card_status
|= WP_VIOLATION
;
941 if (sd
->csd
[14] & 0x30)
942 sd
->card_status
|= WP_VIOLATION
;
950 case 25: /* CMD25: WRITE_MULTIPLE_BLOCK */
952 goto unimplemented_cmd
;
954 case sd_transfer_state
:
955 /* Writing in SPI mode not implemented. */
958 sd
->state
= sd_receivingdata_state
;
959 sd
->data_start
= addr
;
963 if (sd
->data_start
+ sd
->blk_len
> sd
->size
)
964 sd
->card_status
|= ADDRESS_ERROR
;
965 if (sd_wp_addr(sd
, sd
->data_start
))
966 sd
->card_status
|= WP_VIOLATION
;
967 if (sd
->csd
[14] & 0x30)
968 sd
->card_status
|= WP_VIOLATION
;
976 case 26: /* CMD26: PROGRAM_CID */
980 case sd_transfer_state
:
981 sd
->state
= sd_receivingdata_state
;
991 case 27: /* CMD27: PROGRAM_CSD */
993 goto unimplemented_cmd
;
995 case sd_transfer_state
:
996 sd
->state
= sd_receivingdata_state
;
1006 /* Write protection (Class 6) */
1007 case 28: /* CMD28: SET_WRITE_PROT */
1008 switch (sd
->state
) {
1009 case sd_transfer_state
:
1010 if (addr
>= sd
->size
) {
1011 sd
->card_status
|= ADDRESS_ERROR
;
1015 sd
->state
= sd_programming_state
;
1016 set_bit(sd_addr_to_wpnum(addr
), sd
->wp_groups
);
1017 /* Bzzzzzzztt .... Operation complete. */
1018 sd
->state
= sd_transfer_state
;
1026 case 29: /* CMD29: CLR_WRITE_PROT */
1027 switch (sd
->state
) {
1028 case sd_transfer_state
:
1029 if (addr
>= sd
->size
) {
1030 sd
->card_status
|= ADDRESS_ERROR
;
1034 sd
->state
= sd_programming_state
;
1035 clear_bit(sd_addr_to_wpnum(addr
), sd
->wp_groups
);
1036 /* Bzzzzzzztt .... Operation complete. */
1037 sd
->state
= sd_transfer_state
;
1045 case 30: /* CMD30: SEND_WRITE_PROT */
1046 switch (sd
->state
) {
1047 case sd_transfer_state
:
1048 sd
->state
= sd_sendingdata_state
;
1049 *(uint32_t *) sd
->data
= sd_wpbits(sd
, req
.arg
);
1050 sd
->data_start
= addr
;
1051 sd
->data_offset
= 0;
1059 /* Erase commands (Class 5) */
1060 case 32: /* CMD32: ERASE_WR_BLK_START */
1061 switch (sd
->state
) {
1062 case sd_transfer_state
:
1063 sd
->erase_start
= req
.arg
;
1071 case 33: /* CMD33: ERASE_WR_BLK_END */
1072 switch (sd
->state
) {
1073 case sd_transfer_state
:
1074 sd
->erase_end
= req
.arg
;
1082 case 38: /* CMD38: ERASE */
1083 switch (sd
->state
) {
1084 case sd_transfer_state
:
1085 if (sd
->csd
[14] & 0x30) {
1086 sd
->card_status
|= WP_VIOLATION
;
1090 sd
->state
= sd_programming_state
;
1092 /* Bzzzzzzztt .... Operation complete. */
1093 sd
->state
= sd_transfer_state
;
1101 /* Lock card commands (Class 7) */
1102 case 42: /* CMD42: LOCK_UNLOCK */
1104 goto unimplemented_cmd
;
1105 switch (sd
->state
) {
1106 case sd_transfer_state
:
1107 sd
->state
= sd_receivingdata_state
;
1109 sd
->data_offset
= 0;
1119 /* CMD52, CMD53: reserved for SDIO cards
1120 * (see the SDIO Simplified Specification V2.0)
1121 * Handle as illegal command but do not complain
1122 * on stderr, as some OSes may use these in their
1123 * probing for presence of an SDIO card.
1127 /* Application specific commands (Class 8) */
1128 case 55: /* CMD55: APP_CMD */
1132 sd
->expecting_acmd
= true;
1133 sd
->card_status
|= APP_CMD
;
1136 case 56: /* CMD56: GEN_CMD */
1137 fprintf(stderr
, "SD: GEN_CMD 0x%08x\n", req
.arg
);
1139 switch (sd
->state
) {
1140 case sd_transfer_state
:
1141 sd
->data_offset
= 0;
1143 sd
->state
= sd_sendingdata_state
;
1145 sd
->state
= sd_receivingdata_state
;
1155 fprintf(stderr
, "SD: Unknown CMD%i\n", req
.cmd
);
1159 /* Commands that are recognised but not yet implemented in SPI mode. */
1160 fprintf(stderr
, "SD: CMD%i not implemented in SPI mode\n", req
.cmd
);
1164 fprintf(stderr
, "SD: CMD%i in a wrong state\n", req
.cmd
);
1168 static sd_rsp_type_t
sd_app_command(SDState
*sd
,
1171 DPRINTF("ACMD%d 0x%08x\n", req
.cmd
, req
.arg
);
1172 sd
->card_status
|= APP_CMD
;
1174 case 6: /* ACMD6: SET_BUS_WIDTH */
1175 switch (sd
->state
) {
1176 case sd_transfer_state
:
1177 sd
->sd_status
[0] &= 0x3f;
1178 sd
->sd_status
[0] |= (req
.arg
& 0x03) << 6;
1186 case 13: /* ACMD13: SD_STATUS */
1187 switch (sd
->state
) {
1188 case sd_transfer_state
:
1189 sd
->state
= sd_sendingdata_state
;
1191 sd
->data_offset
= 0;
1199 case 22: /* ACMD22: SEND_NUM_WR_BLOCKS */
1200 switch (sd
->state
) {
1201 case sd_transfer_state
:
1202 *(uint32_t *) sd
->data
= sd
->blk_written
;
1204 sd
->state
= sd_sendingdata_state
;
1206 sd
->data_offset
= 0;
1214 case 23: /* ACMD23: SET_WR_BLK_ERASE_COUNT */
1215 switch (sd
->state
) {
1216 case sd_transfer_state
:
1224 case 41: /* ACMD41: SD_APP_OP_COND */
1227 sd
->state
= sd_transfer_state
;
1230 switch (sd
->state
) {
1232 /* We accept any voltage. 10000 V is nothing. */
1234 sd
->state
= sd_ready_state
;
1243 case 42: /* ACMD42: SET_CLR_CARD_DETECT */
1244 switch (sd
->state
) {
1245 case sd_transfer_state
:
1246 /* Bringing in the 50KOhm pull-up resistor... Done. */
1254 case 51: /* ACMD51: SEND_SCR */
1255 switch (sd
->state
) {
1256 case sd_transfer_state
:
1257 sd
->state
= sd_sendingdata_state
;
1259 sd
->data_offset
= 0;
1268 /* Fall back to standard commands. */
1269 return sd_normal_command(sd
, req
);
1272 fprintf(stderr
, "SD: ACMD%i in a wrong state\n", req
.cmd
);
1276 static int cmd_valid_while_locked(SDState
*sd
, SDRequest
*req
)
1278 /* Valid commands in locked state:
1280 * lock card class (7)
1282 * implicitly, the ACMD prefix CMD55
1284 * Anything else provokes an "illegal command" response.
1286 if (sd
->expecting_acmd
) {
1287 return req
->cmd
== 41 || req
->cmd
== 42;
1289 if (req
->cmd
== 16 || req
->cmd
== 55) {
1292 return sd_cmd_class
[req
->cmd
] == 0 || sd_cmd_class
[req
->cmd
] == 7;
1295 int sd_do_command(SDState
*sd
, SDRequest
*req
,
1296 uint8_t *response
) {
1298 sd_rsp_type_t rtype
;
1301 if (!sd
->bdrv
|| !bdrv_is_inserted(sd
->bdrv
) || !sd
->enable
) {
1305 if (sd_req_crc_validate(req
)) {
1306 sd
->card_status
|= COM_CRC_ERROR
;
1311 if (sd
->card_status
& CARD_IS_LOCKED
) {
1312 if (!cmd_valid_while_locked(sd
, req
)) {
1313 sd
->card_status
|= ILLEGAL_COMMAND
;
1314 sd
->expecting_acmd
= false;
1315 fprintf(stderr
, "SD: Card is locked\n");
1321 last_state
= sd
->state
;
1324 if (sd
->expecting_acmd
) {
1325 sd
->expecting_acmd
= false;
1326 rtype
= sd_app_command(sd
, *req
);
1328 rtype
= sd_normal_command(sd
, *req
);
1331 if (rtype
== sd_illegal
) {
1332 sd
->card_status
|= ILLEGAL_COMMAND
;
1334 /* Valid command, we can update the 'state before command' bits.
1335 * (Do this now so they appear in r1 responses.)
1337 sd
->current_cmd
= req
->cmd
;
1338 sd
->card_status
&= ~CURRENT_STATE
;
1339 sd
->card_status
|= (last_state
<< 9);
1346 sd_response_r1_make(sd
, response
);
1351 memcpy(response
, sd
->cid
, sizeof(sd
->cid
));
1356 memcpy(response
, sd
->csd
, sizeof(sd
->csd
));
1361 sd_response_r3_make(sd
, response
);
1366 sd_response_r6_make(sd
, response
);
1371 sd_response_r7_make(sd
, response
);
1382 if (rtype
!= sd_illegal
) {
1383 /* Clear the "clear on valid command" status bits now we've
1386 sd
->card_status
&= ~CARD_STATUS_B
;
1392 DPRINTF("Response:");
1393 for (i
= 0; i
< rsplen
; i
++)
1394 printf(" %02x", response
[i
]);
1395 printf(" state %d\n", sd
->state
);
1397 DPRINTF("No response %d\n", sd
->state
);
1404 static void sd_blk_read(SDState
*sd
, uint64_t addr
, uint32_t len
)
1406 uint64_t end
= addr
+ len
;
1408 DPRINTF("sd_blk_read: addr = 0x%08llx, len = %d\n",
1409 (unsigned long long) addr
, len
);
1410 if (!sd
->bdrv
|| bdrv_read(sd
->bdrv
, addr
>> 9, sd
->buf
, 1) < 0) {
1411 fprintf(stderr
, "sd_blk_read: read error on host side\n");
1415 if (end
> (addr
& ~511) + 512) {
1416 memcpy(sd
->data
, sd
->buf
+ (addr
& 511), 512 - (addr
& 511));
1418 if (bdrv_read(sd
->bdrv
, end
>> 9, sd
->buf
, 1) < 0) {
1419 fprintf(stderr
, "sd_blk_read: read error on host side\n");
1422 memcpy(sd
->data
+ 512 - (addr
& 511), sd
->buf
, end
& 511);
1424 memcpy(sd
->data
, sd
->buf
+ (addr
& 511), len
);
1427 static void sd_blk_write(SDState
*sd
, uint64_t addr
, uint32_t len
)
1429 uint64_t end
= addr
+ len
;
1431 if ((addr
& 511) || len
< 512)
1432 if (!sd
->bdrv
|| bdrv_read(sd
->bdrv
, addr
>> 9, sd
->buf
, 1) < 0) {
1433 fprintf(stderr
, "sd_blk_write: read error on host side\n");
1437 if (end
> (addr
& ~511) + 512) {
1438 memcpy(sd
->buf
+ (addr
& 511), sd
->data
, 512 - (addr
& 511));
1439 if (bdrv_write(sd
->bdrv
, addr
>> 9, sd
->buf
, 1) < 0) {
1440 fprintf(stderr
, "sd_blk_write: write error on host side\n");
1444 if (bdrv_read(sd
->bdrv
, end
>> 9, sd
->buf
, 1) < 0) {
1445 fprintf(stderr
, "sd_blk_write: read error on host side\n");
1448 memcpy(sd
->buf
, sd
->data
+ 512 - (addr
& 511), end
& 511);
1449 if (bdrv_write(sd
->bdrv
, end
>> 9, sd
->buf
, 1) < 0) {
1450 fprintf(stderr
, "sd_blk_write: write error on host side\n");
1453 memcpy(sd
->buf
+ (addr
& 511), sd
->data
, len
);
1454 if (!sd
->bdrv
|| bdrv_write(sd
->bdrv
, addr
>> 9, sd
->buf
, 1) < 0) {
1455 fprintf(stderr
, "sd_blk_write: write error on host side\n");
1460 #define BLK_READ_BLOCK(a, len) sd_blk_read(sd, a, len)
1461 #define BLK_WRITE_BLOCK(a, len) sd_blk_write(sd, a, len)
1462 #define APP_READ_BLOCK(a, len) memset(sd->data, 0xec, len)
1463 #define APP_WRITE_BLOCK(a, len)
1465 void sd_write_data(SDState
*sd
, uint8_t value
)
1469 if (!sd
->bdrv
|| !bdrv_is_inserted(sd
->bdrv
) || !sd
->enable
)
1472 if (sd
->state
!= sd_receivingdata_state
) {
1473 fprintf(stderr
, "sd_write_data: not in Receiving-Data state\n");
1477 if (sd
->card_status
& (ADDRESS_ERROR
| WP_VIOLATION
))
1480 switch (sd
->current_cmd
) {
1481 case 24: /* CMD24: WRITE_SINGLE_BLOCK */
1482 sd
->data
[sd
->data_offset
++] = value
;
1483 if (sd
->data_offset
>= sd
->blk_len
) {
1484 /* TODO: Check CRC before committing */
1485 sd
->state
= sd_programming_state
;
1486 BLK_WRITE_BLOCK(sd
->data_start
, sd
->data_offset
);
1488 sd
->csd
[14] |= 0x40;
1489 /* Bzzzzzzztt .... Operation complete. */
1490 sd
->state
= sd_transfer_state
;
1494 case 25: /* CMD25: WRITE_MULTIPLE_BLOCK */
1495 if (sd
->data_offset
== 0) {
1496 /* Start of the block - lets check the address is valid */
1497 if (sd
->data_start
+ sd
->blk_len
> sd
->size
) {
1498 sd
->card_status
|= ADDRESS_ERROR
;
1501 if (sd_wp_addr(sd
, sd
->data_start
)) {
1502 sd
->card_status
|= WP_VIOLATION
;
1506 sd
->data
[sd
->data_offset
++] = value
;
1507 if (sd
->data_offset
>= sd
->blk_len
) {
1508 /* TODO: Check CRC before committing */
1509 sd
->state
= sd_programming_state
;
1510 BLK_WRITE_BLOCK(sd
->data_start
, sd
->data_offset
);
1512 sd
->data_start
+= sd
->blk_len
;
1513 sd
->data_offset
= 0;
1514 sd
->csd
[14] |= 0x40;
1516 /* Bzzzzzzztt .... Operation complete. */
1517 sd
->state
= sd_receivingdata_state
;
1521 case 26: /* CMD26: PROGRAM_CID */
1522 sd
->data
[sd
->data_offset
++] = value
;
1523 if (sd
->data_offset
>= sizeof(sd
->cid
)) {
1524 /* TODO: Check CRC before committing */
1525 sd
->state
= sd_programming_state
;
1526 for (i
= 0; i
< sizeof(sd
->cid
); i
++)
1527 if ((sd
->cid
[i
] | 0x00) != sd
->data
[i
])
1528 sd
->card_status
|= CID_CSD_OVERWRITE
;
1530 if (!(sd
->card_status
& CID_CSD_OVERWRITE
))
1531 for (i
= 0; i
< sizeof(sd
->cid
); i
++) {
1533 sd
->cid
[i
] &= sd
->data
[i
];
1535 /* Bzzzzzzztt .... Operation complete. */
1536 sd
->state
= sd_transfer_state
;
1540 case 27: /* CMD27: PROGRAM_CSD */
1541 sd
->data
[sd
->data_offset
++] = value
;
1542 if (sd
->data_offset
>= sizeof(sd
->csd
)) {
1543 /* TODO: Check CRC before committing */
1544 sd
->state
= sd_programming_state
;
1545 for (i
= 0; i
< sizeof(sd
->csd
); i
++)
1546 if ((sd
->csd
[i
] | sd_csd_rw_mask
[i
]) !=
1547 (sd
->data
[i
] | sd_csd_rw_mask
[i
]))
1548 sd
->card_status
|= CID_CSD_OVERWRITE
;
1550 /* Copy flag (OTP) & Permanent write protect */
1551 if (sd
->csd
[14] & ~sd
->data
[14] & 0x60)
1552 sd
->card_status
|= CID_CSD_OVERWRITE
;
1554 if (!(sd
->card_status
& CID_CSD_OVERWRITE
))
1555 for (i
= 0; i
< sizeof(sd
->csd
); i
++) {
1556 sd
->csd
[i
] |= sd_csd_rw_mask
[i
];
1557 sd
->csd
[i
] &= sd
->data
[i
];
1559 /* Bzzzzzzztt .... Operation complete. */
1560 sd
->state
= sd_transfer_state
;
1564 case 42: /* CMD42: LOCK_UNLOCK */
1565 sd
->data
[sd
->data_offset
++] = value
;
1566 if (sd
->data_offset
>= sd
->blk_len
) {
1567 /* TODO: Check CRC before committing */
1568 sd
->state
= sd_programming_state
;
1569 sd_lock_command(sd
);
1570 /* Bzzzzzzztt .... Operation complete. */
1571 sd
->state
= sd_transfer_state
;
1575 case 56: /* CMD56: GEN_CMD */
1576 sd
->data
[sd
->data_offset
++] = value
;
1577 if (sd
->data_offset
>= sd
->blk_len
) {
1578 APP_WRITE_BLOCK(sd
->data_start
, sd
->data_offset
);
1579 sd
->state
= sd_transfer_state
;
1584 fprintf(stderr
, "sd_write_data: unknown command\n");
1589 uint8_t sd_read_data(SDState
*sd
)
1591 /* TODO: Append CRCs */
1595 if (!sd
->bdrv
|| !bdrv_is_inserted(sd
->bdrv
) || !sd
->enable
)
1598 if (sd
->state
!= sd_sendingdata_state
) {
1599 fprintf(stderr
, "sd_read_data: not in Sending-Data state\n");
1603 if (sd
->card_status
& (ADDRESS_ERROR
| WP_VIOLATION
))
1606 io_len
= (sd
->ocr
& (1 << 30)) ? 512 : sd
->blk_len
;
1608 switch (sd
->current_cmd
) {
1609 case 6: /* CMD6: SWITCH_FUNCTION */
1610 ret
= sd
->data
[sd
->data_offset
++];
1612 if (sd
->data_offset
>= 64)
1613 sd
->state
= sd_transfer_state
;
1616 case 9: /* CMD9: SEND_CSD */
1617 case 10: /* CMD10: SEND_CID */
1618 ret
= sd
->data
[sd
->data_offset
++];
1620 if (sd
->data_offset
>= 16)
1621 sd
->state
= sd_transfer_state
;
1624 case 11: /* CMD11: READ_DAT_UNTIL_STOP */
1625 if (sd
->data_offset
== 0)
1626 BLK_READ_BLOCK(sd
->data_start
, io_len
);
1627 ret
= sd
->data
[sd
->data_offset
++];
1629 if (sd
->data_offset
>= io_len
) {
1630 sd
->data_start
+= io_len
;
1631 sd
->data_offset
= 0;
1632 if (sd
->data_start
+ io_len
> sd
->size
) {
1633 sd
->card_status
|= ADDRESS_ERROR
;
1639 case 13: /* ACMD13: SD_STATUS */
1640 ret
= sd
->sd_status
[sd
->data_offset
++];
1642 if (sd
->data_offset
>= sizeof(sd
->sd_status
))
1643 sd
->state
= sd_transfer_state
;
1646 case 17: /* CMD17: READ_SINGLE_BLOCK */
1647 if (sd
->data_offset
== 0)
1648 BLK_READ_BLOCK(sd
->data_start
, io_len
);
1649 ret
= sd
->data
[sd
->data_offset
++];
1651 if (sd
->data_offset
>= io_len
)
1652 sd
->state
= sd_transfer_state
;
1655 case 18: /* CMD18: READ_MULTIPLE_BLOCK */
1656 if (sd
->data_offset
== 0)
1657 BLK_READ_BLOCK(sd
->data_start
, io_len
);
1658 ret
= sd
->data
[sd
->data_offset
++];
1660 if (sd
->data_offset
>= io_len
) {
1661 sd
->data_start
+= io_len
;
1662 sd
->data_offset
= 0;
1663 if (sd
->data_start
+ io_len
> sd
->size
) {
1664 sd
->card_status
|= ADDRESS_ERROR
;
1670 case 22: /* ACMD22: SEND_NUM_WR_BLOCKS */
1671 ret
= sd
->data
[sd
->data_offset
++];
1673 if (sd
->data_offset
>= 4)
1674 sd
->state
= sd_transfer_state
;
1677 case 30: /* CMD30: SEND_WRITE_PROT */
1678 ret
= sd
->data
[sd
->data_offset
++];
1680 if (sd
->data_offset
>= 4)
1681 sd
->state
= sd_transfer_state
;
1684 case 51: /* ACMD51: SEND_SCR */
1685 ret
= sd
->scr
[sd
->data_offset
++];
1687 if (sd
->data_offset
>= sizeof(sd
->scr
))
1688 sd
->state
= sd_transfer_state
;
1691 case 56: /* CMD56: GEN_CMD */
1692 if (sd
->data_offset
== 0)
1693 APP_READ_BLOCK(sd
->data_start
, sd
->blk_len
);
1694 ret
= sd
->data
[sd
->data_offset
++];
1696 if (sd
->data_offset
>= sd
->blk_len
)
1697 sd
->state
= sd_transfer_state
;
1701 fprintf(stderr
, "sd_read_data: unknown command\n");
1708 bool sd_data_ready(SDState
*sd
)
1710 return sd
->state
== sd_sendingdata_state
;
1713 void sd_enable(SDState
*sd
, bool enable
)
1715 sd
->enable
= enable
;