2 * SD Memory Card emulation as defined in the "SD Memory Card Physical
3 * layer specification, Version 2.00."
5 * Copyright (c) 2006 Andrzej Zaborowski <balrog@zabor.org>
6 * Copyright (c) 2007 CodeSourcery
7 * Copyright (c) 2018 Philippe Mathieu-Daudé <f4bug@amsat.org>
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in
17 * the documentation and/or other materials provided with the
20 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS''
21 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
22 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
23 * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR
24 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
25 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
26 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
27 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
28 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 #include "qemu/osdep.h"
34 #include "qemu/units.h"
37 #include "hw/registerfields.h"
38 #include "sysemu/block-backend.h"
40 #include "qapi/error.h"
41 #include "qemu/bitmap.h"
42 #include "hw/qdev-properties.h"
43 #include "qemu/error-report.h"
44 #include "qemu/timer.h"
46 #include "sdmmc-internal.h"
52 sd_r0
= 0, /* no response */
53 sd_r1
, /* normal response command */
54 sd_r2_i
, /* CID register */
55 sd_r2_s
, /* CSD register */
56 sd_r3
, /* OCR register */
57 sd_r6
= 6, /* Published RCA response */
58 sd_r7
, /* Operating voltage */
65 sd_card_identification_mode
,
66 sd_data_transfer_mode
,
70 sd_inactive_state
= -1,
73 sd_identification_state
,
77 sd_receivingdata_state
,
83 DeviceState parent_obj
;
85 /* SD Memory Card Registers */
92 uint8_t sd_status
[64];
94 /* Configurable properties */
99 uint32_t mode
; /* current card mode, one of SDCardModes */
100 int32_t state
; /* current card state, one of SDCardStates */
103 unsigned long *wp_groups
;
107 uint32_t multi_blk_cnt
;
108 uint32_t erase_start
;
112 uint8_t function_group
[6];
114 /* True if we will handle the next command as an ACMD. Note that this does
115 * *not* track the APP_CMD status bit!
118 uint32_t blk_written
;
120 uint32_t data_offset
;
122 qemu_irq readonly_cb
;
123 qemu_irq inserted_cb
;
124 QEMUTimer
*ocr_power_timer
;
125 const char *proto_name
;
131 static const char *sd_state_name(enum SDCardStates state
)
133 static const char *state_name
[] = {
134 [sd_idle_state
] = "idle",
135 [sd_ready_state
] = "ready",
136 [sd_identification_state
] = "identification",
137 [sd_standby_state
] = "standby",
138 [sd_transfer_state
] = "transfer",
139 [sd_sendingdata_state
] = "sendingdata",
140 [sd_receivingdata_state
] = "receivingdata",
141 [sd_programming_state
] = "programming",
142 [sd_disconnect_state
] = "disconnect",
144 if (state
== sd_inactive_state
) {
147 assert(state
<= ARRAY_SIZE(state_name
));
148 return state_name
[state
];
151 static const char *sd_response_name(sd_rsp_type_t rsp
)
153 static const char *response_name
[] = {
154 [sd_r0
] = "RESP#0 (no response)",
155 [sd_r1
] = "RESP#1 (normal cmd)",
156 [sd_r2_i
] = "RESP#2 (CID reg)",
157 [sd_r2_s
] = "RESP#2 (CSD reg)",
158 [sd_r3
] = "RESP#3 (OCR reg)",
159 [sd_r6
] = "RESP#6 (RCA)",
160 [sd_r7
] = "RESP#7 (operating voltage)",
162 if (rsp
== sd_illegal
) {
163 return "ILLEGAL RESP";
168 assert(rsp
<= ARRAY_SIZE(response_name
));
169 return response_name
[rsp
];
172 static uint8_t sd_get_dat_lines(SDState
*sd
)
174 return sd
->enable
? sd
->dat_lines
: 0;
177 static bool sd_get_cmd_line(SDState
*sd
)
179 return sd
->enable
? sd
->cmd_line
: false;
182 static void sd_set_voltage(SDState
*sd
, uint16_t millivolts
)
184 trace_sdcard_set_voltage(millivolts
);
186 switch (millivolts
) {
187 case 3001 ... 3600: /* SD_VOLTAGE_3_3V */
188 case 2001 ... 3000: /* SD_VOLTAGE_3_0V */
191 qemu_log_mask(LOG_GUEST_ERROR
, "SD card voltage not supported: %.3fV",
192 millivolts
/ 1000.f
);
196 static void sd_set_mode(SDState
*sd
)
199 case sd_inactive_state
:
200 sd
->mode
= sd_inactive
;
205 case sd_identification_state
:
206 sd
->mode
= sd_card_identification_mode
;
209 case sd_standby_state
:
210 case sd_transfer_state
:
211 case sd_sendingdata_state
:
212 case sd_receivingdata_state
:
213 case sd_programming_state
:
214 case sd_disconnect_state
:
215 sd
->mode
= sd_data_transfer_mode
;
220 static const sd_cmd_type_t sd_cmd_type
[SDMMC_CMD_MAX
] = {
221 sd_bc
, sd_none
, sd_bcr
, sd_bcr
, sd_none
, sd_none
, sd_none
, sd_ac
,
222 sd_bcr
, sd_ac
, sd_ac
, sd_adtc
, sd_ac
, sd_ac
, sd_none
, sd_ac
,
224 sd_ac
, sd_adtc
, sd_adtc
, sd_none
, sd_none
, sd_none
, sd_none
, sd_none
,
225 sd_adtc
, sd_adtc
, sd_adtc
, sd_adtc
, sd_ac
, sd_ac
, sd_adtc
, sd_none
,
227 sd_ac
, sd_ac
, sd_none
, sd_none
, sd_none
, sd_none
, sd_ac
, sd_none
,
228 sd_none
, sd_none
, sd_bc
, sd_none
, sd_none
, sd_none
, sd_none
, sd_none
,
230 sd_none
, sd_none
, sd_none
, sd_none
, sd_none
, sd_none
, sd_none
, sd_ac
,
231 sd_adtc
, sd_none
, sd_none
, sd_none
, sd_none
, sd_none
, sd_none
, sd_none
,
234 static const int sd_cmd_class
[SDMMC_CMD_MAX
] = {
235 0, 0, 0, 0, 0, 9, 10, 0, 0, 0, 0, 1, 0, 0, 0, 0,
236 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 6, 6, 6, 6,
237 5, 5, 10, 10, 10, 10, 5, 9, 9, 9, 7, 7, 7, 7, 7, 7,
238 7, 7, 10, 7, 9, 9, 9, 8, 8, 10, 8, 8, 8, 8, 8, 8,
241 static uint8_t sd_crc7(void *message
, size_t width
)
244 uint8_t shift_reg
= 0x00;
245 uint8_t *msg
= (uint8_t *) message
;
247 for (i
= 0; i
< width
; i
++, msg
++)
248 for (bit
= 7; bit
>= 0; bit
--) {
250 if ((shift_reg
>> 7) ^ ((*msg
>> bit
) & 1))
257 static uint16_t sd_crc16(void *message
, size_t width
)
260 uint16_t shift_reg
= 0x0000;
261 uint16_t *msg
= (uint16_t *) message
;
264 for (i
= 0; i
< width
; i
++, msg
++)
265 for (bit
= 15; bit
>= 0; bit
--) {
267 if ((shift_reg
>> 15) ^ ((*msg
>> bit
) & 1))
274 #define OCR_POWER_DELAY_NS 500000 /* 0.5ms */
276 FIELD(OCR
, VDD_VOLTAGE_WINDOW
, 0, 24)
277 FIELD(OCR
, VDD_VOLTAGE_WIN_LO
, 0, 8)
278 FIELD(OCR
, DUAL_VOLTAGE_CARD
, 7, 1)
279 FIELD(OCR
, VDD_VOLTAGE_WIN_HI
, 8, 16)
280 FIELD(OCR
, ACCEPT_SWITCH_1V8
, 24, 1) /* Only UHS-I */
281 FIELD(OCR
, UHS_II_CARD
, 29, 1) /* Only UHS-II */
282 FIELD(OCR
, CARD_CAPACITY
, 30, 1) /* 0:SDSC, 1:SDHC/SDXC */
283 FIELD(OCR
, CARD_POWER_UP
, 31, 1)
285 #define ACMD41_ENQUIRY_MASK 0x00ffffff
286 #define ACMD41_R3_MASK (R_OCR_VDD_VOLTAGE_WIN_HI_MASK \
287 | R_OCR_ACCEPT_SWITCH_1V8_MASK \
288 | R_OCR_UHS_II_CARD_MASK \
289 | R_OCR_CARD_CAPACITY_MASK \
290 | R_OCR_CARD_POWER_UP_MASK)
292 static void sd_set_ocr(SDState
*sd
)
294 /* All voltages OK */
295 sd
->ocr
= R_OCR_VDD_VOLTAGE_WIN_HI_MASK
;
298 static void sd_ocr_powerup(void *opaque
)
300 SDState
*sd
= opaque
;
302 trace_sdcard_powerup();
303 assert(!FIELD_EX32(sd
->ocr
, OCR
, CARD_POWER_UP
));
305 /* card power-up OK */
306 sd
->ocr
= FIELD_DP32(sd
->ocr
, OCR
, CARD_POWER_UP
, 1);
308 if (sd
->size
> 1 * GiB
) {
309 sd
->ocr
= FIELD_DP32(sd
->ocr
, OCR
, CARD_CAPACITY
, 1);
313 static void sd_set_scr(SDState
*sd
)
315 sd
->scr
[0] = 0 << 4; /* SCR structure version 1.0 */
316 if (sd
->spec_version
== SD_PHY_SPECv1_10_VERS
) {
317 sd
->scr
[0] |= 1; /* Spec Version 1.10 */
319 sd
->scr
[0] |= 2; /* Spec Version 2.00 or Version 3.0X */
321 sd
->scr
[1] = (2 << 4) /* SDSC Card (Security Version 1.01) */
322 | 0b0101; /* 1-bit or 4-bit width bus modes */
323 sd
->scr
[2] = 0x00; /* Extended Security is not supported. */
324 if (sd
->spec_version
>= SD_PHY_SPECv3_01_VERS
) {
325 sd
->scr
[2] |= 1 << 7; /* Spec Version 3.0X */
328 /* reserved for manufacturer usage */
342 static void sd_set_cid(SDState
*sd
)
344 sd
->cid
[0] = MID
; /* Fake card manufacturer ID (MID) */
345 sd
->cid
[1] = OID
[0]; /* OEM/Application ID (OID) */
347 sd
->cid
[3] = PNM
[0]; /* Fake product name (PNM) */
352 sd
->cid
[8] = PRV
; /* Fake product revision (PRV) */
353 sd
->cid
[9] = 0xde; /* Fake serial number (PSN) */
357 sd
->cid
[13] = 0x00 | /* Manufacture date (MDT) */
358 ((MDT_YR
- 2000) / 10);
359 sd
->cid
[14] = ((MDT_YR
% 10) << 4) | MDT_MON
;
360 sd
->cid
[15] = (sd_crc7(sd
->cid
, 15) << 1) | 1;
363 #define HWBLOCK_SHIFT 9 /* 512 bytes */
364 #define SECTOR_SHIFT 5 /* 16 kilobytes */
365 #define WPGROUP_SHIFT 7 /* 2 megs */
366 #define CMULT_SHIFT 9 /* 512 times HWBLOCK_SIZE */
367 #define WPGROUP_SIZE (1 << (HWBLOCK_SHIFT + SECTOR_SHIFT + WPGROUP_SHIFT))
369 static const uint8_t sd_csd_rw_mask
[16] = {
370 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
371 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xfc, 0xfe,
374 static void sd_set_csd(SDState
*sd
, uint64_t size
)
376 uint32_t csize
= (size
>> (CMULT_SHIFT
+ HWBLOCK_SHIFT
)) - 1;
377 uint32_t sectsize
= (1 << (SECTOR_SHIFT
+ 1)) - 1;
378 uint32_t wpsize
= (1 << (WPGROUP_SHIFT
+ 1)) - 1;
380 if (size
<= 1 * GiB
) { /* Standard Capacity SD */
381 sd
->csd
[0] = 0x00; /* CSD structure */
382 sd
->csd
[1] = 0x26; /* Data read access-time-1 */
383 sd
->csd
[2] = 0x00; /* Data read access-time-2 */
384 sd
->csd
[3] = 0x32; /* Max. data transfer rate: 25 MHz */
385 sd
->csd
[4] = 0x5f; /* Card Command Classes */
386 sd
->csd
[5] = 0x50 | /* Max. read data block length */
388 sd
->csd
[6] = 0xe0 | /* Partial block for read allowed */
389 ((csize
>> 10) & 0x03);
390 sd
->csd
[7] = 0x00 | /* Device size */
391 ((csize
>> 2) & 0xff);
392 sd
->csd
[8] = 0x3f | /* Max. read current */
393 ((csize
<< 6) & 0xc0);
394 sd
->csd
[9] = 0xfc | /* Max. write current */
395 ((CMULT_SHIFT
- 2) >> 1);
396 sd
->csd
[10] = 0x40 | /* Erase sector size */
397 (((CMULT_SHIFT
- 2) << 7) & 0x80) | (sectsize
>> 1);
398 sd
->csd
[11] = 0x00 | /* Write protect group size */
399 ((sectsize
<< 7) & 0x80) | wpsize
;
400 sd
->csd
[12] = 0x90 | /* Write speed factor */
401 (HWBLOCK_SHIFT
>> 2);
402 sd
->csd
[13] = 0x20 | /* Max. write data block length */
403 ((HWBLOCK_SHIFT
<< 6) & 0xc0);
404 sd
->csd
[14] = 0x00; /* File format group */
415 sd
->csd
[7] = (size
>> 16) & 0xff;
416 sd
->csd
[8] = (size
>> 8) & 0xff;
417 sd
->csd
[9] = (size
& 0xff);
424 sd
->csd
[15] = (sd_crc7(sd
->csd
, 15) << 1) | 1;
427 static void sd_set_rca(SDState
*sd
)
432 FIELD(CSR
, AKE_SEQ_ERROR
, 3, 1)
433 FIELD(CSR
, APP_CMD
, 5, 1)
434 FIELD(CSR
, FX_EVENT
, 6, 1)
435 FIELD(CSR
, READY_FOR_DATA
, 8, 1)
436 FIELD(CSR
, CURRENT_STATE
, 9, 4)
437 FIELD(CSR
, ERASE_RESET
, 13, 1)
438 FIELD(CSR
, CARD_ECC_DISABLED
, 14, 1)
439 FIELD(CSR
, WP_ERASE_SKIP
, 15, 1)
440 FIELD(CSR
, CSD_OVERWRITE
, 16, 1)
441 FIELD(CSR
, DEFERRED_RESPONSE
, 17, 1)
442 FIELD(CSR
, ERROR
, 19, 1)
443 FIELD(CSR
, CC_ERROR
, 20, 1)
444 FIELD(CSR
, CARD_ECC_FAILED
, 21, 1)
445 FIELD(CSR
, ILLEGAL_COMMAND
, 22, 1)
446 FIELD(CSR
, COM_CRC_ERROR
, 23, 1)
447 FIELD(CSR
, LOCK_UNLOCK_FAILED
, 24, 1)
448 FIELD(CSR
, CARD_IS_LOCKED
, 25, 1)
449 FIELD(CSR
, WP_VIOLATION
, 26, 1)
450 FIELD(CSR
, ERASE_PARAM
, 27, 1)
451 FIELD(CSR
, ERASE_SEQ_ERROR
, 28, 1)
452 FIELD(CSR
, BLOCK_LEN_ERROR
, 29, 1)
453 FIELD(CSR
, ADDRESS_ERROR
, 30, 1)
454 FIELD(CSR
, OUT_OF_RANGE
, 31, 1)
456 /* Card status bits, split by clear condition:
457 * A : According to the card current state
458 * B : Always related to the previous command
459 * C : Cleared by read
461 #define CARD_STATUS_A (R_CSR_READY_FOR_DATA_MASK \
462 | R_CSR_CARD_ECC_DISABLED_MASK \
463 | R_CSR_CARD_IS_LOCKED_MASK)
464 #define CARD_STATUS_B (R_CSR_CURRENT_STATE_MASK \
465 | R_CSR_ILLEGAL_COMMAND_MASK \
466 | R_CSR_COM_CRC_ERROR_MASK)
467 #define CARD_STATUS_C (R_CSR_AKE_SEQ_ERROR_MASK \
468 | R_CSR_APP_CMD_MASK \
469 | R_CSR_ERASE_RESET_MASK \
470 | R_CSR_WP_ERASE_SKIP_MASK \
471 | R_CSR_CSD_OVERWRITE_MASK \
473 | R_CSR_CC_ERROR_MASK \
474 | R_CSR_CARD_ECC_FAILED_MASK \
475 | R_CSR_LOCK_UNLOCK_FAILED_MASK \
476 | R_CSR_WP_VIOLATION_MASK \
477 | R_CSR_ERASE_PARAM_MASK \
478 | R_CSR_ERASE_SEQ_ERROR_MASK \
479 | R_CSR_BLOCK_LEN_ERROR_MASK \
480 | R_CSR_ADDRESS_ERROR_MASK \
481 | R_CSR_OUT_OF_RANGE_MASK)
483 static void sd_set_cardstatus(SDState
*sd
)
485 sd
->card_status
= 0x00000100;
488 static void sd_set_sdstatus(SDState
*sd
)
490 memset(sd
->sd_status
, 0, 64);
493 static int sd_req_crc_validate(SDRequest
*req
)
496 buffer
[0] = 0x40 | req
->cmd
;
497 stl_be_p(&buffer
[1], req
->arg
);
499 return sd_crc7(buffer
, 5) != req
->crc
; /* TODO */
502 static void sd_response_r1_make(SDState
*sd
, uint8_t *response
)
504 stl_be_p(response
, sd
->card_status
);
506 /* Clear the "clear on read" status bits */
507 sd
->card_status
&= ~CARD_STATUS_C
;
510 static void sd_response_r3_make(SDState
*sd
, uint8_t *response
)
512 stl_be_p(response
, sd
->ocr
& ACMD41_R3_MASK
);
515 static void sd_response_r6_make(SDState
*sd
, uint8_t *response
)
519 status
= ((sd
->card_status
>> 8) & 0xc000) |
520 ((sd
->card_status
>> 6) & 0x2000) |
521 (sd
->card_status
& 0x1fff);
522 sd
->card_status
&= ~(CARD_STATUS_C
& 0xc81fff);
523 stw_be_p(response
+ 0, sd
->rca
);
524 stw_be_p(response
+ 2, status
);
527 static void sd_response_r7_make(SDState
*sd
, uint8_t *response
)
529 stl_be_p(response
, sd
->vhs
);
532 static inline uint64_t sd_addr_to_wpnum(uint64_t addr
)
534 return addr
>> (HWBLOCK_SHIFT
+ SECTOR_SHIFT
+ WPGROUP_SHIFT
);
537 static void sd_reset(DeviceState
*dev
)
539 SDState
*sd
= SD_CARD(dev
);
543 trace_sdcard_reset();
545 blk_get_geometry(sd
->blk
, §
);
551 sect
= sd_addr_to_wpnum(size
) + 1;
553 sd
->state
= sd_idle_state
;
558 sd_set_csd(sd
, size
);
559 sd_set_cardstatus(sd
);
562 g_free(sd
->wp_groups
);
563 sd
->wp_switch
= sd
->blk
? blk_is_read_only(sd
->blk
) : false;
564 sd
->wpgrps_size
= sect
;
565 sd
->wp_groups
= bitmap_new(sd
->wpgrps_size
);
566 memset(sd
->function_group
, 0, sizeof(sd
->function_group
));
572 sd
->expecting_acmd
= false;
575 sd
->multi_blk_cnt
= 0;
578 static bool sd_get_inserted(SDState
*sd
)
580 return sd
->blk
&& blk_is_inserted(sd
->blk
);
583 static bool sd_get_readonly(SDState
*sd
)
585 return sd
->wp_switch
;
588 static void sd_cardchange(void *opaque
, bool load
, Error
**errp
)
590 SDState
*sd
= opaque
;
591 DeviceState
*dev
= DEVICE(sd
);
592 SDBus
*sdbus
= SD_BUS(qdev_get_parent_bus(dev
));
593 bool inserted
= sd_get_inserted(sd
);
594 bool readonly
= sd_get_readonly(sd
);
597 trace_sdcard_inserted(readonly
);
600 trace_sdcard_ejected();
603 /* The IRQ notification is for legacy non-QOM SD controller devices;
604 * QOMified controllers use the SDBus APIs.
607 sdbus_set_inserted(sdbus
, inserted
);
609 sdbus_set_readonly(sdbus
, readonly
);
612 qemu_set_irq(sd
->inserted_cb
, inserted
);
614 qemu_set_irq(sd
->readonly_cb
, readonly
);
619 static const BlockDevOps sd_block_ops
= {
620 .change_media_cb
= sd_cardchange
,
623 static bool sd_ocr_vmstate_needed(void *opaque
)
625 SDState
*sd
= opaque
;
627 /* Include the OCR state (and timer) if it is not yet powered up */
628 return !FIELD_EX32(sd
->ocr
, OCR
, CARD_POWER_UP
);
631 static const VMStateDescription sd_ocr_vmstate
= {
632 .name
= "sd-card/ocr-state",
634 .minimum_version_id
= 1,
635 .needed
= sd_ocr_vmstate_needed
,
636 .fields
= (VMStateField
[]) {
637 VMSTATE_UINT32(ocr
, SDState
),
638 VMSTATE_TIMER_PTR(ocr_power_timer
, SDState
),
639 VMSTATE_END_OF_LIST()
643 static int sd_vmstate_pre_load(void *opaque
)
645 SDState
*sd
= opaque
;
647 /* If the OCR state is not included (prior versions, or not
648 * needed), then the OCR must be set as powered up. If the OCR state
649 * is included, this will be replaced by the state restore.
656 static const VMStateDescription sd_vmstate
= {
659 .minimum_version_id
= 1,
660 .pre_load
= sd_vmstate_pre_load
,
661 .fields
= (VMStateField
[]) {
662 VMSTATE_UINT32(mode
, SDState
),
663 VMSTATE_INT32(state
, SDState
),
664 VMSTATE_UINT8_ARRAY(cid
, SDState
, 16),
665 VMSTATE_UINT8_ARRAY(csd
, SDState
, 16),
666 VMSTATE_UINT16(rca
, SDState
),
667 VMSTATE_UINT32(card_status
, SDState
),
668 VMSTATE_PARTIAL_BUFFER(sd_status
, SDState
, 1),
669 VMSTATE_UINT32(vhs
, SDState
),
670 VMSTATE_BITMAP(wp_groups
, SDState
, 0, wpgrps_size
),
671 VMSTATE_UINT32(blk_len
, SDState
),
672 VMSTATE_UINT32(multi_blk_cnt
, SDState
),
673 VMSTATE_UINT32(erase_start
, SDState
),
674 VMSTATE_UINT32(erase_end
, SDState
),
675 VMSTATE_UINT8_ARRAY(pwd
, SDState
, 16),
676 VMSTATE_UINT32(pwd_len
, SDState
),
677 VMSTATE_UINT8_ARRAY(function_group
, SDState
, 6),
678 VMSTATE_UINT8(current_cmd
, SDState
),
679 VMSTATE_BOOL(expecting_acmd
, SDState
),
680 VMSTATE_UINT32(blk_written
, SDState
),
681 VMSTATE_UINT64(data_start
, SDState
),
682 VMSTATE_UINT32(data_offset
, SDState
),
683 VMSTATE_UINT8_ARRAY(data
, SDState
, 512),
684 VMSTATE_UNUSED_V(1, 512),
685 VMSTATE_BOOL(enable
, SDState
),
686 VMSTATE_END_OF_LIST()
688 .subsections
= (const VMStateDescription
*[]) {
694 /* Legacy initialization function for use by non-qdevified callers */
695 SDState
*sd_init(BlockBackend
*blk
, bool is_spi
)
701 obj
= object_new(TYPE_SD_CARD
);
703 qdev_prop_set_drive(dev
, "drive", blk
, &err
);
705 error_report("sd_init failed: %s", error_get_pretty(err
));
708 qdev_prop_set_bit(dev
, "spi", is_spi
);
709 object_property_set_bool(obj
, true, "realized", &err
);
711 error_report("sd_init failed: %s", error_get_pretty(err
));
718 void sd_set_cb(SDState
*sd
, qemu_irq readonly
, qemu_irq insert
)
720 sd
->readonly_cb
= readonly
;
721 sd
->inserted_cb
= insert
;
722 qemu_set_irq(readonly
, sd
->blk
? blk_is_read_only(sd
->blk
) : 0);
723 qemu_set_irq(insert
, sd
->blk
? blk_is_inserted(sd
->blk
) : 0);
726 static void sd_erase(SDState
*sd
)
729 uint64_t erase_start
= sd
->erase_start
;
730 uint64_t erase_end
= sd
->erase_end
;
732 trace_sdcard_erase();
733 if (!sd
->erase_start
|| !sd
->erase_end
) {
734 sd
->card_status
|= ERASE_SEQ_ERROR
;
738 if (FIELD_EX32(sd
->ocr
, OCR
, CARD_CAPACITY
)) {
739 /* High capacity memory card: erase units are 512 byte blocks */
744 erase_start
= sd_addr_to_wpnum(erase_start
);
745 erase_end
= sd_addr_to_wpnum(erase_end
);
750 for (i
= erase_start
; i
<= erase_end
; i
++) {
751 if (test_bit(i
, sd
->wp_groups
)) {
752 sd
->card_status
|= WP_ERASE_SKIP
;
757 static uint32_t sd_wpbits(SDState
*sd
, uint64_t addr
)
762 wpnum
= sd_addr_to_wpnum(addr
);
764 for (i
= 0; i
< 32; i
++, wpnum
++, addr
+= WPGROUP_SIZE
) {
765 if (addr
< sd
->size
&& test_bit(wpnum
, sd
->wp_groups
)) {
773 static void sd_function_switch(SDState
*sd
, uint32_t arg
)
775 int i
, mode
, new_func
;
776 mode
= !!(arg
& 0x80000000);
778 sd
->data
[0] = 0x00; /* Maximum current consumption */
780 sd
->data
[2] = 0x80; /* Supported group 6 functions */
782 sd
->data
[4] = 0x80; /* Supported group 5 functions */
784 sd
->data
[6] = 0x80; /* Supported group 4 functions */
786 sd
->data
[8] = 0x80; /* Supported group 3 functions */
788 sd
->data
[10] = 0x80; /* Supported group 2 functions */
790 sd
->data
[12] = 0x80; /* Supported group 1 functions */
792 for (i
= 0; i
< 6; i
++) {
793 new_func
= (arg
>> (i
* 4)) & 0x0f;
794 if (mode
&& new_func
!= 0x0f)
795 sd
->function_group
[i
] = new_func
;
796 sd
->data
[14 + (i
>> 1)] = new_func
<< ((i
* 4) & 4);
798 memset(&sd
->data
[17], 0, 47);
799 stw_be_p(sd
->data
+ 64, sd_crc16(sd
->data
, 64));
802 static inline bool sd_wp_addr(SDState
*sd
, uint64_t addr
)
804 return test_bit(sd_addr_to_wpnum(addr
), sd
->wp_groups
);
807 static void sd_lock_command(SDState
*sd
)
809 int erase
, lock
, clr_pwd
, set_pwd
, pwd_len
;
810 erase
= !!(sd
->data
[0] & 0x08);
811 lock
= sd
->data
[0] & 0x04;
812 clr_pwd
= sd
->data
[0] & 0x02;
813 set_pwd
= sd
->data
[0] & 0x01;
816 pwd_len
= sd
->data
[1];
823 trace_sdcard_unlock();
826 if (!(sd
->card_status
& CARD_IS_LOCKED
) || sd
->blk_len
> 1 ||
827 set_pwd
|| clr_pwd
|| lock
|| sd
->wp_switch
||
828 (sd
->csd
[14] & 0x20)) {
829 sd
->card_status
|= LOCK_UNLOCK_FAILED
;
832 bitmap_zero(sd
->wp_groups
, sd
->wpgrps_size
);
833 sd
->csd
[14] &= ~0x10;
834 sd
->card_status
&= ~CARD_IS_LOCKED
;
836 /* Erasing the entire card here! */
837 fprintf(stderr
, "SD: Card force-erased by CMD42\n");
841 if (sd
->blk_len
< 2 + pwd_len
||
842 pwd_len
<= sd
->pwd_len
||
843 pwd_len
> sd
->pwd_len
+ 16) {
844 sd
->card_status
|= LOCK_UNLOCK_FAILED
;
848 if (sd
->pwd_len
&& memcmp(sd
->pwd
, sd
->data
+ 2, sd
->pwd_len
)) {
849 sd
->card_status
|= LOCK_UNLOCK_FAILED
;
853 pwd_len
-= sd
->pwd_len
;
854 if ((pwd_len
&& !set_pwd
) ||
855 (clr_pwd
&& (set_pwd
|| lock
)) ||
856 (lock
&& !sd
->pwd_len
&& !set_pwd
) ||
857 (!set_pwd
&& !clr_pwd
&&
858 (((sd
->card_status
& CARD_IS_LOCKED
) && lock
) ||
859 (!(sd
->card_status
& CARD_IS_LOCKED
) && !lock
)))) {
860 sd
->card_status
|= LOCK_UNLOCK_FAILED
;
865 memcpy(sd
->pwd
, sd
->data
+ 2 + sd
->pwd_len
, pwd_len
);
866 sd
->pwd_len
= pwd_len
;
874 sd
->card_status
|= CARD_IS_LOCKED
;
876 sd
->card_status
&= ~CARD_IS_LOCKED
;
879 static sd_rsp_type_t
sd_normal_command(SDState
*sd
, SDRequest req
)
881 uint32_t rca
= 0x0000;
882 uint64_t addr
= (sd
->ocr
& (1 << 30)) ? (uint64_t) req
.arg
<< 9 : req
.arg
;
884 /* CMD55 precedes an ACMD, so we are not interested in tracing it.
885 * However there is no ACMD55, so we want to trace this particular case.
887 if (req
.cmd
!= 55 || sd
->expecting_acmd
) {
888 trace_sdcard_normal_command(sd
->proto_name
,
889 sd_cmd_name(req
.cmd
), req
.cmd
,
890 req
.arg
, sd_state_name(sd
->state
));
893 /* Not interpreting this as an app command */
894 sd
->card_status
&= ~APP_CMD
;
896 if (sd_cmd_type
[req
.cmd
] == sd_ac
897 || sd_cmd_type
[req
.cmd
] == sd_adtc
) {
901 /* CMD23 (set block count) must be immediately followed by CMD18 or CMD25
902 * if not, its effects are cancelled */
903 if (sd
->multi_blk_cnt
!= 0 && !(req
.cmd
== 18 || req
.cmd
== 25)) {
904 sd
->multi_blk_cnt
= 0;
908 /* Basic commands (Class 0 and Class 1) */
909 case 0: /* CMD0: GO_IDLE_STATE */
911 case sd_inactive_state
:
912 return sd
->spi
? sd_r1
: sd_r0
;
915 sd
->state
= sd_idle_state
;
916 sd_reset(DEVICE(sd
));
917 return sd
->spi
? sd_r1
: sd_r0
;
921 case 1: /* CMD1: SEND_OP_CMD */
925 sd
->state
= sd_transfer_state
;
928 case 2: /* CMD2: ALL_SEND_CID */
933 sd
->state
= sd_identification_state
;
941 case 3: /* CMD3: SEND_RELATIVE_ADDR */
945 case sd_identification_state
:
946 case sd_standby_state
:
947 sd
->state
= sd_standby_state
;
956 case 4: /* CMD4: SEND_DSR */
960 case sd_standby_state
:
968 case 5: /* CMD5: reserved for SDIO cards */
971 case 6: /* CMD6: SWITCH_FUNCTION */
973 case sd_data_transfer_mode
:
974 sd_function_switch(sd
, req
.arg
);
975 sd
->state
= sd_sendingdata_state
;
985 case 7: /* CMD7: SELECT/DESELECT_CARD */
989 case sd_standby_state
:
993 sd
->state
= sd_transfer_state
;
996 case sd_transfer_state
:
997 case sd_sendingdata_state
:
1001 sd
->state
= sd_standby_state
;
1004 case sd_disconnect_state
:
1008 sd
->state
= sd_programming_state
;
1011 case sd_programming_state
:
1015 sd
->state
= sd_disconnect_state
;
1023 case 8: /* CMD8: SEND_IF_COND */
1024 if (sd
->spec_version
< SD_PHY_SPECv2_00_VERS
) {
1027 if (sd
->state
!= sd_idle_state
) {
1032 /* No response if not exactly one VHS bit is set. */
1033 if (!(req
.arg
>> 8) || (req
.arg
>> (ctz32(req
.arg
& ~0xff) + 1))) {
1034 return sd
->spi
? sd_r7
: sd_r0
;
1041 case 9: /* CMD9: SEND_CSD */
1042 switch (sd
->state
) {
1043 case sd_standby_state
:
1049 case sd_transfer_state
:
1052 sd
->state
= sd_sendingdata_state
;
1053 memcpy(sd
->data
, sd
->csd
, 16);
1054 sd
->data_start
= addr
;
1055 sd
->data_offset
= 0;
1063 case 10: /* CMD10: SEND_CID */
1064 switch (sd
->state
) {
1065 case sd_standby_state
:
1071 case sd_transfer_state
:
1074 sd
->state
= sd_sendingdata_state
;
1075 memcpy(sd
->data
, sd
->cid
, 16);
1076 sd
->data_start
= addr
;
1077 sd
->data_offset
= 0;
1085 case 12: /* CMD12: STOP_TRANSMISSION */
1086 switch (sd
->state
) {
1087 case sd_sendingdata_state
:
1088 sd
->state
= sd_transfer_state
;
1091 case sd_receivingdata_state
:
1092 sd
->state
= sd_programming_state
;
1093 /* Bzzzzzzztt .... Operation complete. */
1094 sd
->state
= sd_transfer_state
;
1102 case 13: /* CMD13: SEND_STATUS */
1104 case sd_data_transfer_mode
:
1115 case 15: /* CMD15: GO_INACTIVE_STATE */
1119 case sd_data_transfer_mode
:
1123 sd
->state
= sd_inactive_state
;
1131 /* Block read commands (Classs 2) */
1132 case 16: /* CMD16: SET_BLOCKLEN */
1133 switch (sd
->state
) {
1134 case sd_transfer_state
:
1135 if (req
.arg
> (1 << HWBLOCK_SHIFT
)) {
1136 sd
->card_status
|= BLOCK_LEN_ERROR
;
1138 trace_sdcard_set_blocklen(req
.arg
);
1139 sd
->blk_len
= req
.arg
;
1149 case 17: /* CMD17: READ_SINGLE_BLOCK */
1150 switch (sd
->state
) {
1151 case sd_transfer_state
:
1152 sd
->state
= sd_sendingdata_state
;
1153 sd
->data_start
= addr
;
1154 sd
->data_offset
= 0;
1156 if (sd
->data_start
+ sd
->blk_len
> sd
->size
)
1157 sd
->card_status
|= ADDRESS_ERROR
;
1165 case 18: /* CMD18: READ_MULTIPLE_BLOCK */
1166 switch (sd
->state
) {
1167 case sd_transfer_state
:
1168 sd
->state
= sd_sendingdata_state
;
1169 sd
->data_start
= addr
;
1170 sd
->data_offset
= 0;
1172 if (sd
->data_start
+ sd
->blk_len
> sd
->size
)
1173 sd
->card_status
|= ADDRESS_ERROR
;
1181 case 19: /* CMD19: SEND_TUNING_BLOCK (SD) */
1182 if (sd
->spec_version
< SD_PHY_SPECv3_01_VERS
) {
1185 if (sd
->state
== sd_transfer_state
) {
1186 sd
->state
= sd_sendingdata_state
;
1187 sd
->data_offset
= 0;
1192 case 23: /* CMD23: SET_BLOCK_COUNT */
1193 if (sd
->spec_version
< SD_PHY_SPECv3_01_VERS
) {
1196 switch (sd
->state
) {
1197 case sd_transfer_state
:
1198 sd
->multi_blk_cnt
= req
.arg
;
1206 /* Block write commands (Class 4) */
1207 case 24: /* CMD24: WRITE_SINGLE_BLOCK */
1208 switch (sd
->state
) {
1209 case sd_transfer_state
:
1210 /* Writing in SPI mode not implemented. */
1213 sd
->state
= sd_receivingdata_state
;
1214 sd
->data_start
= addr
;
1215 sd
->data_offset
= 0;
1216 sd
->blk_written
= 0;
1218 if (sd
->data_start
+ sd
->blk_len
> sd
->size
)
1219 sd
->card_status
|= ADDRESS_ERROR
;
1220 if (sd_wp_addr(sd
, sd
->data_start
))
1221 sd
->card_status
|= WP_VIOLATION
;
1222 if (sd
->csd
[14] & 0x30)
1223 sd
->card_status
|= WP_VIOLATION
;
1231 case 25: /* CMD25: WRITE_MULTIPLE_BLOCK */
1232 switch (sd
->state
) {
1233 case sd_transfer_state
:
1234 /* Writing in SPI mode not implemented. */
1237 sd
->state
= sd_receivingdata_state
;
1238 sd
->data_start
= addr
;
1239 sd
->data_offset
= 0;
1240 sd
->blk_written
= 0;
1242 if (sd
->data_start
+ sd
->blk_len
> sd
->size
)
1243 sd
->card_status
|= ADDRESS_ERROR
;
1244 if (sd_wp_addr(sd
, sd
->data_start
))
1245 sd
->card_status
|= WP_VIOLATION
;
1246 if (sd
->csd
[14] & 0x30)
1247 sd
->card_status
|= WP_VIOLATION
;
1255 case 26: /* CMD26: PROGRAM_CID */
1258 switch (sd
->state
) {
1259 case sd_transfer_state
:
1260 sd
->state
= sd_receivingdata_state
;
1262 sd
->data_offset
= 0;
1270 case 27: /* CMD27: PROGRAM_CSD */
1271 switch (sd
->state
) {
1272 case sd_transfer_state
:
1273 sd
->state
= sd_receivingdata_state
;
1275 sd
->data_offset
= 0;
1283 /* Write protection (Class 6) */
1284 case 28: /* CMD28: SET_WRITE_PROT */
1285 switch (sd
->state
) {
1286 case sd_transfer_state
:
1287 if (addr
>= sd
->size
) {
1288 sd
->card_status
|= ADDRESS_ERROR
;
1292 sd
->state
= sd_programming_state
;
1293 set_bit(sd_addr_to_wpnum(addr
), sd
->wp_groups
);
1294 /* Bzzzzzzztt .... Operation complete. */
1295 sd
->state
= sd_transfer_state
;
1303 case 29: /* CMD29: CLR_WRITE_PROT */
1304 switch (sd
->state
) {
1305 case sd_transfer_state
:
1306 if (addr
>= sd
->size
) {
1307 sd
->card_status
|= ADDRESS_ERROR
;
1311 sd
->state
= sd_programming_state
;
1312 clear_bit(sd_addr_to_wpnum(addr
), sd
->wp_groups
);
1313 /* Bzzzzzzztt .... Operation complete. */
1314 sd
->state
= sd_transfer_state
;
1322 case 30: /* CMD30: SEND_WRITE_PROT */
1323 switch (sd
->state
) {
1324 case sd_transfer_state
:
1325 sd
->state
= sd_sendingdata_state
;
1326 *(uint32_t *) sd
->data
= sd_wpbits(sd
, req
.arg
);
1327 sd
->data_start
= addr
;
1328 sd
->data_offset
= 0;
1336 /* Erase commands (Class 5) */
1337 case 32: /* CMD32: ERASE_WR_BLK_START */
1338 switch (sd
->state
) {
1339 case sd_transfer_state
:
1340 sd
->erase_start
= req
.arg
;
1348 case 33: /* CMD33: ERASE_WR_BLK_END */
1349 switch (sd
->state
) {
1350 case sd_transfer_state
:
1351 sd
->erase_end
= req
.arg
;
1359 case 38: /* CMD38: ERASE */
1360 switch (sd
->state
) {
1361 case sd_transfer_state
:
1362 if (sd
->csd
[14] & 0x30) {
1363 sd
->card_status
|= WP_VIOLATION
;
1367 sd
->state
= sd_programming_state
;
1369 /* Bzzzzzzztt .... Operation complete. */
1370 sd
->state
= sd_transfer_state
;
1378 /* Lock card commands (Class 7) */
1379 case 42: /* CMD42: LOCK_UNLOCK */
1380 switch (sd
->state
) {
1381 case sd_transfer_state
:
1382 sd
->state
= sd_receivingdata_state
;
1384 sd
->data_offset
= 0;
1393 /* CMD52, CMD53, CMD54: reserved for SDIO cards
1394 * (see the SDIO Simplified Specification V2.0)
1395 * Handle as illegal command but do not complain
1396 * on stderr, as some OSes may use these in their
1397 * probing for presence of an SDIO card.
1401 /* Application specific commands (Class 8) */
1402 case 55: /* CMD55: APP_CMD */
1403 switch (sd
->state
) {
1404 case sd_ready_state
:
1405 case sd_identification_state
:
1406 case sd_inactive_state
:
1410 qemu_log_mask(LOG_GUEST_ERROR
,
1411 "SD: illegal RCA 0x%04x for APP_CMD\n", req
.cmd
);
1417 if (sd
->rca
!= rca
) {
1421 sd
->expecting_acmd
= true;
1422 sd
->card_status
|= APP_CMD
;
1425 case 56: /* CMD56: GEN_CMD */
1426 switch (sd
->state
) {
1427 case sd_transfer_state
:
1428 sd
->data_offset
= 0;
1430 sd
->state
= sd_sendingdata_state
;
1432 sd
->state
= sd_receivingdata_state
;
1440 case 58: /* CMD58: READ_OCR (SPI) */
1446 case 59: /* CMD59: CRC_ON_OFF (SPI) */
1450 goto unimplemented_spi_cmd
;
1454 qemu_log_mask(LOG_GUEST_ERROR
, "SD: Unknown CMD%i\n", req
.cmd
);
1457 unimplemented_spi_cmd
:
1458 /* Commands that are recognised but not yet implemented in SPI mode. */
1459 qemu_log_mask(LOG_UNIMP
, "SD: CMD%i not implemented in SPI mode\n",
1464 qemu_log_mask(LOG_GUEST_ERROR
, "SD: CMD%i in a wrong state\n", req
.cmd
);
1468 static sd_rsp_type_t
sd_app_command(SDState
*sd
,
1471 trace_sdcard_app_command(sd
->proto_name
, sd_acmd_name(req
.cmd
),
1472 req
.cmd
, req
.arg
, sd_state_name(sd
->state
));
1473 sd
->card_status
|= APP_CMD
;
1475 case 6: /* ACMD6: SET_BUS_WIDTH */
1477 goto unimplemented_spi_cmd
;
1479 switch (sd
->state
) {
1480 case sd_transfer_state
:
1481 sd
->sd_status
[0] &= 0x3f;
1482 sd
->sd_status
[0] |= (req
.arg
& 0x03) << 6;
1490 case 13: /* ACMD13: SD_STATUS */
1491 switch (sd
->state
) {
1492 case sd_transfer_state
:
1493 sd
->state
= sd_sendingdata_state
;
1495 sd
->data_offset
= 0;
1503 case 22: /* ACMD22: SEND_NUM_WR_BLOCKS */
1504 switch (sd
->state
) {
1505 case sd_transfer_state
:
1506 *(uint32_t *) sd
->data
= sd
->blk_written
;
1508 sd
->state
= sd_sendingdata_state
;
1510 sd
->data_offset
= 0;
1518 case 23: /* ACMD23: SET_WR_BLK_ERASE_COUNT */
1519 switch (sd
->state
) {
1520 case sd_transfer_state
:
1528 case 41: /* ACMD41: SD_APP_OP_COND */
1531 sd
->state
= sd_transfer_state
;
1534 if (sd
->state
!= sd_idle_state
) {
1537 /* If it's the first ACMD41 since reset, we need to decide
1538 * whether to power up. If this is not an enquiry ACMD41,
1539 * we immediately report power on and proceed below to the
1540 * ready state, but if it is, we set a timer to model a
1541 * delay for power up. This works around a bug in EDK2
1542 * UEFI, which sends an initial enquiry ACMD41, but
1543 * assumes that the card is in ready state as soon as it
1544 * sees the power up bit set. */
1545 if (!FIELD_EX32(sd
->ocr
, OCR
, CARD_POWER_UP
)) {
1546 if ((req
.arg
& ACMD41_ENQUIRY_MASK
) != 0) {
1547 timer_del(sd
->ocr_power_timer
);
1550 trace_sdcard_inquiry_cmd41();
1551 if (!timer_pending(sd
->ocr_power_timer
)) {
1552 timer_mod_ns(sd
->ocr_power_timer
,
1553 (qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
)
1554 + OCR_POWER_DELAY_NS
));
1559 if (FIELD_EX32(sd
->ocr
& req
.arg
, OCR
, VDD_VOLTAGE_WINDOW
)) {
1560 /* We accept any voltage. 10000 V is nothing.
1562 * Once we're powered up, we advance straight to ready state
1563 * unless it's an enquiry ACMD41 (bits 23:0 == 0).
1565 sd
->state
= sd_ready_state
;
1570 case 42: /* ACMD42: SET_CLR_CARD_DETECT */
1571 switch (sd
->state
) {
1572 case sd_transfer_state
:
1573 /* Bringing in the 50KOhm pull-up resistor... Done. */
1581 case 51: /* ACMD51: SEND_SCR */
1582 switch (sd
->state
) {
1583 case sd_transfer_state
:
1584 sd
->state
= sd_sendingdata_state
;
1586 sd
->data_offset
= 0;
1594 case 18: /* Reserved for SD security applications */
1599 /* Refer to the "SD Specifications Part3 Security Specification" for
1600 * information about the SD Security Features.
1602 qemu_log_mask(LOG_UNIMP
, "SD: CMD%i Security not implemented\n",
1607 /* Fall back to standard commands. */
1608 return sd_normal_command(sd
, req
);
1610 unimplemented_spi_cmd
:
1611 /* Commands that are recognised but not yet implemented in SPI mode. */
1612 qemu_log_mask(LOG_UNIMP
, "SD: CMD%i not implemented in SPI mode\n",
1617 qemu_log_mask(LOG_GUEST_ERROR
, "SD: ACMD%i in a wrong state\n", req
.cmd
);
1621 static int cmd_valid_while_locked(SDState
*sd
, SDRequest
*req
)
1623 /* Valid commands in locked state:
1625 * lock card class (7)
1627 * implicitly, the ACMD prefix CMD55
1629 * Anything else provokes an "illegal command" response.
1631 if (sd
->expecting_acmd
) {
1632 return req
->cmd
== 41 || req
->cmd
== 42;
1634 if (req
->cmd
== 16 || req
->cmd
== 55) {
1637 return sd_cmd_class
[req
->cmd
] == 0
1638 || sd_cmd_class
[req
->cmd
] == 7;
1641 int sd_do_command(SDState
*sd
, SDRequest
*req
,
1642 uint8_t *response
) {
1644 sd_rsp_type_t rtype
;
1647 if (!sd
->blk
|| !blk_is_inserted(sd
->blk
) || !sd
->enable
) {
1651 if (sd_req_crc_validate(req
)) {
1652 sd
->card_status
|= COM_CRC_ERROR
;
1657 if (req
->cmd
>= SDMMC_CMD_MAX
) {
1658 qemu_log_mask(LOG_GUEST_ERROR
, "SD: incorrect command 0x%02x\n",
1663 if (sd
->card_status
& CARD_IS_LOCKED
) {
1664 if (!cmd_valid_while_locked(sd
, req
)) {
1665 sd
->card_status
|= ILLEGAL_COMMAND
;
1666 sd
->expecting_acmd
= false;
1667 qemu_log_mask(LOG_GUEST_ERROR
, "SD: Card is locked\n");
1673 last_state
= sd
->state
;
1676 if (sd
->expecting_acmd
) {
1677 sd
->expecting_acmd
= false;
1678 rtype
= sd_app_command(sd
, *req
);
1680 rtype
= sd_normal_command(sd
, *req
);
1683 if (rtype
== sd_illegal
) {
1684 sd
->card_status
|= ILLEGAL_COMMAND
;
1686 /* Valid command, we can update the 'state before command' bits.
1687 * (Do this now so they appear in r1 responses.)
1689 sd
->current_cmd
= req
->cmd
;
1690 sd
->card_status
&= ~CURRENT_STATE
;
1691 sd
->card_status
|= (last_state
<< 9);
1698 sd_response_r1_make(sd
, response
);
1703 memcpy(response
, sd
->cid
, sizeof(sd
->cid
));
1708 memcpy(response
, sd
->csd
, sizeof(sd
->csd
));
1713 sd_response_r3_make(sd
, response
);
1718 sd_response_r6_make(sd
, response
);
1723 sd_response_r7_make(sd
, response
);
1732 g_assert_not_reached();
1734 trace_sdcard_response(sd_response_name(rtype
), rsplen
);
1736 if (rtype
!= sd_illegal
) {
1737 /* Clear the "clear on valid command" status bits now we've
1740 sd
->card_status
&= ~CARD_STATUS_B
;
1744 qemu_hexdump((const char *)response
, stderr
, "Response", rsplen
);
1750 static void sd_blk_read(SDState
*sd
, uint64_t addr
, uint32_t len
)
1752 trace_sdcard_read_block(addr
, len
);
1753 if (!sd
->blk
|| blk_pread(sd
->blk
, addr
, sd
->data
, len
) < 0) {
1754 fprintf(stderr
, "sd_blk_read: read error on host side\n");
1758 static void sd_blk_write(SDState
*sd
, uint64_t addr
, uint32_t len
)
1760 trace_sdcard_write_block(addr
, len
);
1761 if (!sd
->blk
|| blk_pwrite(sd
->blk
, addr
, sd
->data
, len
, 0) < 0) {
1762 fprintf(stderr
, "sd_blk_write: write error on host side\n");
1766 #define BLK_READ_BLOCK(a, len) sd_blk_read(sd, a, len)
1767 #define BLK_WRITE_BLOCK(a, len) sd_blk_write(sd, a, len)
1768 #define APP_READ_BLOCK(a, len) memset(sd->data, 0xec, len)
1769 #define APP_WRITE_BLOCK(a, len)
1771 void sd_write_data(SDState
*sd
, uint8_t value
)
1775 if (!sd
->blk
|| !blk_is_inserted(sd
->blk
) || !sd
->enable
)
1778 if (sd
->state
!= sd_receivingdata_state
) {
1779 qemu_log_mask(LOG_GUEST_ERROR
,
1780 "sd_write_data: not in Receiving-Data state\n");
1784 if (sd
->card_status
& (ADDRESS_ERROR
| WP_VIOLATION
))
1787 trace_sdcard_write_data(sd
->proto_name
,
1788 sd_acmd_name(sd
->current_cmd
),
1789 sd
->current_cmd
, value
);
1790 switch (sd
->current_cmd
) {
1791 case 24: /* CMD24: WRITE_SINGLE_BLOCK */
1792 sd
->data
[sd
->data_offset
++] = value
;
1793 if (sd
->data_offset
>= sd
->blk_len
) {
1794 /* TODO: Check CRC before committing */
1795 sd
->state
= sd_programming_state
;
1796 BLK_WRITE_BLOCK(sd
->data_start
, sd
->data_offset
);
1798 sd
->csd
[14] |= 0x40;
1799 /* Bzzzzzzztt .... Operation complete. */
1800 sd
->state
= sd_transfer_state
;
1804 case 25: /* CMD25: WRITE_MULTIPLE_BLOCK */
1805 if (sd
->data_offset
== 0) {
1806 /* Start of the block - let's check the address is valid */
1807 if (sd
->data_start
+ sd
->blk_len
> sd
->size
) {
1808 sd
->card_status
|= ADDRESS_ERROR
;
1811 if (sd_wp_addr(sd
, sd
->data_start
)) {
1812 sd
->card_status
|= WP_VIOLATION
;
1816 sd
->data
[sd
->data_offset
++] = value
;
1817 if (sd
->data_offset
>= sd
->blk_len
) {
1818 /* TODO: Check CRC before committing */
1819 sd
->state
= sd_programming_state
;
1820 BLK_WRITE_BLOCK(sd
->data_start
, sd
->data_offset
);
1822 sd
->data_start
+= sd
->blk_len
;
1823 sd
->data_offset
= 0;
1824 sd
->csd
[14] |= 0x40;
1826 /* Bzzzzzzztt .... Operation complete. */
1827 if (sd
->multi_blk_cnt
!= 0) {
1828 if (--sd
->multi_blk_cnt
== 0) {
1830 sd
->state
= sd_transfer_state
;
1835 sd
->state
= sd_receivingdata_state
;
1839 case 26: /* CMD26: PROGRAM_CID */
1840 sd
->data
[sd
->data_offset
++] = value
;
1841 if (sd
->data_offset
>= sizeof(sd
->cid
)) {
1842 /* TODO: Check CRC before committing */
1843 sd
->state
= sd_programming_state
;
1844 for (i
= 0; i
< sizeof(sd
->cid
); i
++)
1845 if ((sd
->cid
[i
] | 0x00) != sd
->data
[i
])
1846 sd
->card_status
|= CID_CSD_OVERWRITE
;
1848 if (!(sd
->card_status
& CID_CSD_OVERWRITE
))
1849 for (i
= 0; i
< sizeof(sd
->cid
); i
++) {
1851 sd
->cid
[i
] &= sd
->data
[i
];
1853 /* Bzzzzzzztt .... Operation complete. */
1854 sd
->state
= sd_transfer_state
;
1858 case 27: /* CMD27: PROGRAM_CSD */
1859 sd
->data
[sd
->data_offset
++] = value
;
1860 if (sd
->data_offset
>= sizeof(sd
->csd
)) {
1861 /* TODO: Check CRC before committing */
1862 sd
->state
= sd_programming_state
;
1863 for (i
= 0; i
< sizeof(sd
->csd
); i
++)
1864 if ((sd
->csd
[i
] | sd_csd_rw_mask
[i
]) !=
1865 (sd
->data
[i
] | sd_csd_rw_mask
[i
]))
1866 sd
->card_status
|= CID_CSD_OVERWRITE
;
1868 /* Copy flag (OTP) & Permanent write protect */
1869 if (sd
->csd
[14] & ~sd
->data
[14] & 0x60)
1870 sd
->card_status
|= CID_CSD_OVERWRITE
;
1872 if (!(sd
->card_status
& CID_CSD_OVERWRITE
))
1873 for (i
= 0; i
< sizeof(sd
->csd
); i
++) {
1874 sd
->csd
[i
] |= sd_csd_rw_mask
[i
];
1875 sd
->csd
[i
] &= sd
->data
[i
];
1877 /* Bzzzzzzztt .... Operation complete. */
1878 sd
->state
= sd_transfer_state
;
1882 case 42: /* CMD42: LOCK_UNLOCK */
1883 sd
->data
[sd
->data_offset
++] = value
;
1884 if (sd
->data_offset
>= sd
->blk_len
) {
1885 /* TODO: Check CRC before committing */
1886 sd
->state
= sd_programming_state
;
1887 sd_lock_command(sd
);
1888 /* Bzzzzzzztt .... Operation complete. */
1889 sd
->state
= sd_transfer_state
;
1893 case 56: /* CMD56: GEN_CMD */
1894 sd
->data
[sd
->data_offset
++] = value
;
1895 if (sd
->data_offset
>= sd
->blk_len
) {
1896 APP_WRITE_BLOCK(sd
->data_start
, sd
->data_offset
);
1897 sd
->state
= sd_transfer_state
;
1902 qemu_log_mask(LOG_GUEST_ERROR
, "sd_write_data: unknown command\n");
1907 #define SD_TUNING_BLOCK_SIZE 64
1909 static const uint8_t sd_tuning_block_pattern
[SD_TUNING_BLOCK_SIZE
] = {
1910 /* See: Physical Layer Simplified Specification Version 3.01, Table 4-2 */
1911 0xff, 0x0f, 0xff, 0x00, 0x0f, 0xfc, 0xc3, 0xcc,
1912 0xc3, 0x3c, 0xcc, 0xff, 0xfe, 0xff, 0xfe, 0xef,
1913 0xff, 0xdf, 0xff, 0xdd, 0xff, 0xfb, 0xff, 0xfb,
1914 0xbf, 0xff, 0x7f, 0xff, 0x77, 0xf7, 0xbd, 0xef,
1915 0xff, 0xf0, 0xff, 0xf0, 0x0f, 0xfc, 0xcc, 0x3c,
1916 0xcc, 0x33, 0xcc, 0xcf, 0xff, 0xef, 0xff, 0xee,
1917 0xff, 0xfd, 0xff, 0xfd, 0xdf, 0xff, 0xbf, 0xff,
1918 0xbb, 0xff, 0xf7, 0xff, 0xf7, 0x7f, 0x7b, 0xde,
1921 uint8_t sd_read_data(SDState
*sd
)
1923 /* TODO: Append CRCs */
1927 if (!sd
->blk
|| !blk_is_inserted(sd
->blk
) || !sd
->enable
)
1930 if (sd
->state
!= sd_sendingdata_state
) {
1931 qemu_log_mask(LOG_GUEST_ERROR
,
1932 "sd_read_data: not in Sending-Data state\n");
1936 if (sd
->card_status
& (ADDRESS_ERROR
| WP_VIOLATION
))
1939 io_len
= (sd
->ocr
& (1 << 30)) ? 512 : sd
->blk_len
;
1941 trace_sdcard_read_data(sd
->proto_name
,
1942 sd_acmd_name(sd
->current_cmd
),
1943 sd
->current_cmd
, io_len
);
1944 switch (sd
->current_cmd
) {
1945 case 6: /* CMD6: SWITCH_FUNCTION */
1946 ret
= sd
->data
[sd
->data_offset
++];
1948 if (sd
->data_offset
>= 64)
1949 sd
->state
= sd_transfer_state
;
1952 case 9: /* CMD9: SEND_CSD */
1953 case 10: /* CMD10: SEND_CID */
1954 ret
= sd
->data
[sd
->data_offset
++];
1956 if (sd
->data_offset
>= 16)
1957 sd
->state
= sd_transfer_state
;
1960 case 13: /* ACMD13: SD_STATUS */
1961 ret
= sd
->sd_status
[sd
->data_offset
++];
1963 if (sd
->data_offset
>= sizeof(sd
->sd_status
))
1964 sd
->state
= sd_transfer_state
;
1967 case 17: /* CMD17: READ_SINGLE_BLOCK */
1968 if (sd
->data_offset
== 0)
1969 BLK_READ_BLOCK(sd
->data_start
, io_len
);
1970 ret
= sd
->data
[sd
->data_offset
++];
1972 if (sd
->data_offset
>= io_len
)
1973 sd
->state
= sd_transfer_state
;
1976 case 18: /* CMD18: READ_MULTIPLE_BLOCK */
1977 if (sd
->data_offset
== 0) {
1978 if (sd
->data_start
+ io_len
> sd
->size
) {
1979 sd
->card_status
|= ADDRESS_ERROR
;
1982 BLK_READ_BLOCK(sd
->data_start
, io_len
);
1984 ret
= sd
->data
[sd
->data_offset
++];
1986 if (sd
->data_offset
>= io_len
) {
1987 sd
->data_start
+= io_len
;
1988 sd
->data_offset
= 0;
1990 if (sd
->multi_blk_cnt
!= 0) {
1991 if (--sd
->multi_blk_cnt
== 0) {
1993 sd
->state
= sd_transfer_state
;
2000 case 19: /* CMD19: SEND_TUNING_BLOCK (SD) */
2001 if (sd
->data_offset
>= SD_TUNING_BLOCK_SIZE
- 1) {
2002 sd
->state
= sd_transfer_state
;
2004 ret
= sd_tuning_block_pattern
[sd
->data_offset
++];
2007 case 22: /* ACMD22: SEND_NUM_WR_BLOCKS */
2008 ret
= sd
->data
[sd
->data_offset
++];
2010 if (sd
->data_offset
>= 4)
2011 sd
->state
= sd_transfer_state
;
2014 case 30: /* CMD30: SEND_WRITE_PROT */
2015 ret
= sd
->data
[sd
->data_offset
++];
2017 if (sd
->data_offset
>= 4)
2018 sd
->state
= sd_transfer_state
;
2021 case 51: /* ACMD51: SEND_SCR */
2022 ret
= sd
->scr
[sd
->data_offset
++];
2024 if (sd
->data_offset
>= sizeof(sd
->scr
))
2025 sd
->state
= sd_transfer_state
;
2028 case 56: /* CMD56: GEN_CMD */
2029 if (sd
->data_offset
== 0)
2030 APP_READ_BLOCK(sd
->data_start
, sd
->blk_len
);
2031 ret
= sd
->data
[sd
->data_offset
++];
2033 if (sd
->data_offset
>= sd
->blk_len
)
2034 sd
->state
= sd_transfer_state
;
2038 qemu_log_mask(LOG_GUEST_ERROR
, "sd_read_data: unknown command\n");
2045 bool sd_data_ready(SDState
*sd
)
2047 return sd
->state
== sd_sendingdata_state
;
2050 void sd_enable(SDState
*sd
, bool enable
)
2052 sd
->enable
= enable
;
2055 static void sd_instance_init(Object
*obj
)
2057 SDState
*sd
= SD_CARD(obj
);
2060 sd
->ocr_power_timer
= timer_new_ns(QEMU_CLOCK_VIRTUAL
, sd_ocr_powerup
, sd
);
2063 static void sd_instance_finalize(Object
*obj
)
2065 SDState
*sd
= SD_CARD(obj
);
2067 timer_del(sd
->ocr_power_timer
);
2068 timer_free(sd
->ocr_power_timer
);
2071 static void sd_realize(DeviceState
*dev
, Error
**errp
)
2073 SDState
*sd
= SD_CARD(dev
);
2076 sd
->proto_name
= sd
->spi
? "SPI" : "SD";
2078 switch (sd
->spec_version
) {
2079 case SD_PHY_SPECv1_10_VERS
2080 ... SD_PHY_SPECv3_01_VERS
:
2083 error_setg(errp
, "Invalid SD card Spec version: %u", sd
->spec_version
);
2087 if (sd
->blk
&& blk_is_read_only(sd
->blk
)) {
2088 error_setg(errp
, "Cannot use read-only drive as SD card");
2093 ret
= blk_set_perm(sd
->blk
, BLK_PERM_CONSISTENT_READ
| BLK_PERM_WRITE
,
2094 BLK_PERM_ALL
, errp
);
2098 blk_set_dev_ops(sd
->blk
, &sd_block_ops
, sd
);
2102 static Property sd_properties
[] = {
2103 DEFINE_PROP_UINT8("spec_version", SDState
,
2104 spec_version
, SD_PHY_SPECv2_00_VERS
),
2105 DEFINE_PROP_DRIVE("drive", SDState
, blk
),
2106 /* We do not model the chip select pin, so allow the board to select
2107 * whether card should be in SSI or MMC/SD mode. It is also up to the
2108 * board to ensure that ssi transfers only occur when the chip select
2110 DEFINE_PROP_BOOL("spi", SDState
, spi
, false),
2111 DEFINE_PROP_END_OF_LIST()
2114 static void sd_class_init(ObjectClass
*klass
, void *data
)
2116 DeviceClass
*dc
= DEVICE_CLASS(klass
);
2117 SDCardClass
*sc
= SD_CARD_CLASS(klass
);
2119 dc
->realize
= sd_realize
;
2120 dc
->props
= sd_properties
;
2121 dc
->vmsd
= &sd_vmstate
;
2122 dc
->reset
= sd_reset
;
2123 dc
->bus_type
= TYPE_SD_BUS
;
2125 sc
->set_voltage
= sd_set_voltage
;
2126 sc
->get_dat_lines
= sd_get_dat_lines
;
2127 sc
->get_cmd_line
= sd_get_cmd_line
;
2128 sc
->do_command
= sd_do_command
;
2129 sc
->write_data
= sd_write_data
;
2130 sc
->read_data
= sd_read_data
;
2131 sc
->data_ready
= sd_data_ready
;
2132 sc
->enable
= sd_enable
;
2133 sc
->get_inserted
= sd_get_inserted
;
2134 sc
->get_readonly
= sd_get_readonly
;
2137 static const TypeInfo sd_info
= {
2138 .name
= TYPE_SD_CARD
,
2139 .parent
= TYPE_DEVICE
,
2140 .instance_size
= sizeof(SDState
),
2141 .class_size
= sizeof(SDCardClass
),
2142 .class_init
= sd_class_init
,
2143 .instance_init
= sd_instance_init
,
2144 .instance_finalize
= sd_instance_finalize
,
2147 static void sd_register_types(void)
2149 type_register_static(&sd_info
);
2152 type_init(sd_register_types
)