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"
35 #include "qemu/cutils.h"
37 #include "hw/registerfields.h"
38 #include "sysemu/block-backend.h"
40 #include "migration/vmstate.h"
41 #include "qapi/error.h"
42 #include "qemu/bitmap.h"
43 #include "hw/qdev-properties.h"
44 #include "qemu/error-report.h"
45 #include "qemu/timer.h"
47 #include "qemu/module.h"
48 #include "sdmmc-internal.h"
54 sd_r0
= 0, /* no response */
55 sd_r1
, /* normal response command */
56 sd_r2_i
, /* CID register */
57 sd_r2_s
, /* CSD register */
58 sd_r3
, /* OCR register */
59 sd_r6
= 6, /* Published RCA response */
60 sd_r7
, /* Operating voltage */
67 sd_card_identification_mode
,
68 sd_data_transfer_mode
,
72 sd_inactive_state
= -1,
75 sd_identification_state
,
79 sd_receivingdata_state
,
85 DeviceState parent_obj
;
87 /* If true, created by sd_init() for a non-qdevified caller */
88 /* TODO purge them with fire */
89 bool me_no_qdev_me_kill_mammoth_with_rocks
;
91 /* SD Memory Card Registers */
98 uint8_t sd_status
[64];
100 /* Configurable properties */
101 uint8_t spec_version
;
105 uint32_t mode
; /* current card mode, one of SDCardModes */
106 int32_t state
; /* current card state, one of SDCardStates */
109 unsigned long *wp_groups
;
113 uint32_t multi_blk_cnt
;
114 uint32_t erase_start
;
118 uint8_t function_group
[6];
120 /* True if we will handle the next command as an ACMD. Note that this does
121 * *not* track the APP_CMD status bit!
124 uint32_t blk_written
;
126 uint32_t data_offset
;
128 qemu_irq readonly_cb
;
129 qemu_irq inserted_cb
;
130 QEMUTimer
*ocr_power_timer
;
131 const char *proto_name
;
137 static void sd_realize(DeviceState
*dev
, Error
**errp
);
139 static const char *sd_state_name(enum SDCardStates state
)
141 static const char *state_name
[] = {
142 [sd_idle_state
] = "idle",
143 [sd_ready_state
] = "ready",
144 [sd_identification_state
] = "identification",
145 [sd_standby_state
] = "standby",
146 [sd_transfer_state
] = "transfer",
147 [sd_sendingdata_state
] = "sendingdata",
148 [sd_receivingdata_state
] = "receivingdata",
149 [sd_programming_state
] = "programming",
150 [sd_disconnect_state
] = "disconnect",
152 if (state
== sd_inactive_state
) {
155 assert(state
< ARRAY_SIZE(state_name
));
156 return state_name
[state
];
159 static const char *sd_response_name(sd_rsp_type_t rsp
)
161 static const char *response_name
[] = {
162 [sd_r0
] = "RESP#0 (no response)",
163 [sd_r1
] = "RESP#1 (normal cmd)",
164 [sd_r2_i
] = "RESP#2 (CID reg)",
165 [sd_r2_s
] = "RESP#2 (CSD reg)",
166 [sd_r3
] = "RESP#3 (OCR reg)",
167 [sd_r6
] = "RESP#6 (RCA)",
168 [sd_r7
] = "RESP#7 (operating voltage)",
170 if (rsp
== sd_illegal
) {
171 return "ILLEGAL RESP";
176 assert(rsp
< ARRAY_SIZE(response_name
));
177 return response_name
[rsp
];
180 static uint8_t sd_get_dat_lines(SDState
*sd
)
182 return sd
->enable
? sd
->dat_lines
: 0;
185 static bool sd_get_cmd_line(SDState
*sd
)
187 return sd
->enable
? sd
->cmd_line
: false;
190 static void sd_set_voltage(SDState
*sd
, uint16_t millivolts
)
192 trace_sdcard_set_voltage(millivolts
);
194 switch (millivolts
) {
195 case 3001 ... 3600: /* SD_VOLTAGE_3_3V */
196 case 2001 ... 3000: /* SD_VOLTAGE_3_0V */
199 qemu_log_mask(LOG_GUEST_ERROR
, "SD card voltage not supported: %.3fV",
200 millivolts
/ 1000.f
);
204 static void sd_set_mode(SDState
*sd
)
207 case sd_inactive_state
:
208 sd
->mode
= sd_inactive
;
213 case sd_identification_state
:
214 sd
->mode
= sd_card_identification_mode
;
217 case sd_standby_state
:
218 case sd_transfer_state
:
219 case sd_sendingdata_state
:
220 case sd_receivingdata_state
:
221 case sd_programming_state
:
222 case sd_disconnect_state
:
223 sd
->mode
= sd_data_transfer_mode
;
228 static const sd_cmd_type_t sd_cmd_type
[SDMMC_CMD_MAX
] = {
229 sd_bc
, sd_none
, sd_bcr
, sd_bcr
, sd_none
, sd_none
, sd_none
, sd_ac
,
230 sd_bcr
, sd_ac
, sd_ac
, sd_adtc
, sd_ac
, sd_ac
, sd_none
, sd_ac
,
232 sd_ac
, sd_adtc
, sd_adtc
, sd_none
, sd_none
, sd_none
, sd_none
, sd_none
,
233 sd_adtc
, sd_adtc
, sd_adtc
, sd_adtc
, sd_ac
, sd_ac
, sd_adtc
, sd_none
,
235 sd_ac
, sd_ac
, sd_none
, sd_none
, sd_none
, sd_none
, sd_ac
, sd_none
,
236 sd_none
, sd_none
, sd_bc
, sd_none
, sd_none
, sd_none
, sd_none
, sd_none
,
238 sd_none
, sd_none
, sd_none
, sd_none
, sd_none
, sd_none
, sd_none
, sd_ac
,
239 sd_adtc
, sd_none
, sd_none
, sd_none
, sd_none
, sd_none
, sd_none
, sd_none
,
242 static const int sd_cmd_class
[SDMMC_CMD_MAX
] = {
243 0, 0, 0, 0, 0, 9, 10, 0, 0, 0, 0, 1, 0, 0, 0, 0,
244 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 6, 6, 6, 6,
245 5, 5, 10, 10, 10, 10, 5, 9, 9, 9, 7, 7, 7, 7, 7, 7,
246 7, 7, 10, 7, 9, 9, 9, 8, 8, 10, 8, 8, 8, 8, 8, 8,
249 static uint8_t sd_crc7(void *message
, size_t width
)
252 uint8_t shift_reg
= 0x00;
253 uint8_t *msg
= (uint8_t *) message
;
255 for (i
= 0; i
< width
; i
++, msg
++)
256 for (bit
= 7; bit
>= 0; bit
--) {
258 if ((shift_reg
>> 7) ^ ((*msg
>> bit
) & 1))
265 static uint16_t sd_crc16(void *message
, size_t width
)
268 uint16_t shift_reg
= 0x0000;
269 uint16_t *msg
= (uint16_t *) message
;
272 for (i
= 0; i
< width
; i
++, msg
++)
273 for (bit
= 15; bit
>= 0; bit
--) {
275 if ((shift_reg
>> 15) ^ ((*msg
>> bit
) & 1))
282 #define OCR_POWER_DELAY_NS 500000 /* 0.5ms */
284 FIELD(OCR
, VDD_VOLTAGE_WINDOW
, 0, 24)
285 FIELD(OCR
, VDD_VOLTAGE_WIN_LO
, 0, 8)
286 FIELD(OCR
, DUAL_VOLTAGE_CARD
, 7, 1)
287 FIELD(OCR
, VDD_VOLTAGE_WIN_HI
, 8, 16)
288 FIELD(OCR
, ACCEPT_SWITCH_1V8
, 24, 1) /* Only UHS-I */
289 FIELD(OCR
, UHS_II_CARD
, 29, 1) /* Only UHS-II */
290 FIELD(OCR
, CARD_CAPACITY
, 30, 1) /* 0:SDSC, 1:SDHC/SDXC */
291 FIELD(OCR
, CARD_POWER_UP
, 31, 1)
293 #define ACMD41_ENQUIRY_MASK 0x00ffffff
294 #define ACMD41_R3_MASK (R_OCR_VDD_VOLTAGE_WIN_HI_MASK \
295 | R_OCR_ACCEPT_SWITCH_1V8_MASK \
296 | R_OCR_UHS_II_CARD_MASK \
297 | R_OCR_CARD_CAPACITY_MASK \
298 | R_OCR_CARD_POWER_UP_MASK)
300 static void sd_set_ocr(SDState
*sd
)
302 /* All voltages OK */
303 sd
->ocr
= R_OCR_VDD_VOLTAGE_WIN_HI_MASK
;
306 static void sd_ocr_powerup(void *opaque
)
308 SDState
*sd
= opaque
;
310 trace_sdcard_powerup();
311 assert(!FIELD_EX32(sd
->ocr
, OCR
, CARD_POWER_UP
));
313 /* card power-up OK */
314 sd
->ocr
= FIELD_DP32(sd
->ocr
, OCR
, CARD_POWER_UP
, 1);
316 if (sd
->size
> 1 * GiB
) {
317 sd
->ocr
= FIELD_DP32(sd
->ocr
, OCR
, CARD_CAPACITY
, 1);
321 static void sd_set_scr(SDState
*sd
)
323 sd
->scr
[0] = 0 << 4; /* SCR structure version 1.0 */
324 if (sd
->spec_version
== SD_PHY_SPECv1_10_VERS
) {
325 sd
->scr
[0] |= 1; /* Spec Version 1.10 */
327 sd
->scr
[0] |= 2; /* Spec Version 2.00 or Version 3.0X */
329 sd
->scr
[1] = (2 << 4) /* SDSC Card (Security Version 1.01) */
330 | 0b0101; /* 1-bit or 4-bit width bus modes */
331 sd
->scr
[2] = 0x00; /* Extended Security is not supported. */
332 if (sd
->spec_version
>= SD_PHY_SPECv3_01_VERS
) {
333 sd
->scr
[2] |= 1 << 7; /* Spec Version 3.0X */
336 /* reserved for manufacturer usage */
350 static void sd_set_cid(SDState
*sd
)
352 sd
->cid
[0] = MID
; /* Fake card manufacturer ID (MID) */
353 sd
->cid
[1] = OID
[0]; /* OEM/Application ID (OID) */
355 sd
->cid
[3] = PNM
[0]; /* Fake product name (PNM) */
360 sd
->cid
[8] = PRV
; /* Fake product revision (PRV) */
361 sd
->cid
[9] = 0xde; /* Fake serial number (PSN) */
365 sd
->cid
[13] = 0x00 | /* Manufacture date (MDT) */
366 ((MDT_YR
- 2000) / 10);
367 sd
->cid
[14] = ((MDT_YR
% 10) << 4) | MDT_MON
;
368 sd
->cid
[15] = (sd_crc7(sd
->cid
, 15) << 1) | 1;
371 #define HWBLOCK_SHIFT 9 /* 512 bytes */
372 #define SECTOR_SHIFT 5 /* 16 kilobytes */
373 #define WPGROUP_SHIFT 7 /* 2 megs */
374 #define CMULT_SHIFT 9 /* 512 times HWBLOCK_SIZE */
375 #define WPGROUP_SIZE (1 << (HWBLOCK_SHIFT + SECTOR_SHIFT + WPGROUP_SHIFT))
377 static const uint8_t sd_csd_rw_mask
[16] = {
378 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
379 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xfc, 0xfe,
382 static void sd_set_csd(SDState
*sd
, uint64_t size
)
384 uint32_t csize
= (size
>> (CMULT_SHIFT
+ HWBLOCK_SHIFT
)) - 1;
385 uint32_t sectsize
= (1 << (SECTOR_SHIFT
+ 1)) - 1;
386 uint32_t wpsize
= (1 << (WPGROUP_SHIFT
+ 1)) - 1;
388 if (size
<= 1 * GiB
) { /* Standard Capacity SD */
389 sd
->csd
[0] = 0x00; /* CSD structure */
390 sd
->csd
[1] = 0x26; /* Data read access-time-1 */
391 sd
->csd
[2] = 0x00; /* Data read access-time-2 */
392 sd
->csd
[3] = 0x32; /* Max. data transfer rate: 25 MHz */
393 sd
->csd
[4] = 0x5f; /* Card Command Classes */
394 sd
->csd
[5] = 0x50 | /* Max. read data block length */
396 sd
->csd
[6] = 0xe0 | /* Partial block for read allowed */
397 ((csize
>> 10) & 0x03);
398 sd
->csd
[7] = 0x00 | /* Device size */
399 ((csize
>> 2) & 0xff);
400 sd
->csd
[8] = 0x3f | /* Max. read current */
401 ((csize
<< 6) & 0xc0);
402 sd
->csd
[9] = 0xfc | /* Max. write current */
403 ((CMULT_SHIFT
- 2) >> 1);
404 sd
->csd
[10] = 0x40 | /* Erase sector size */
405 (((CMULT_SHIFT
- 2) << 7) & 0x80) | (sectsize
>> 1);
406 sd
->csd
[11] = 0x00 | /* Write protect group size */
407 ((sectsize
<< 7) & 0x80) | wpsize
;
408 sd
->csd
[12] = 0x90 | /* Write speed factor */
409 (HWBLOCK_SHIFT
>> 2);
410 sd
->csd
[13] = 0x20 | /* Max. write data block length */
411 ((HWBLOCK_SHIFT
<< 6) & 0xc0);
412 sd
->csd
[14] = 0x00; /* File format group */
423 sd
->csd
[7] = (size
>> 16) & 0xff;
424 sd
->csd
[8] = (size
>> 8) & 0xff;
425 sd
->csd
[9] = (size
& 0xff);
432 sd
->csd
[15] = (sd_crc7(sd
->csd
, 15) << 1) | 1;
435 static void sd_set_rca(SDState
*sd
)
440 FIELD(CSR
, AKE_SEQ_ERROR
, 3, 1)
441 FIELD(CSR
, APP_CMD
, 5, 1)
442 FIELD(CSR
, FX_EVENT
, 6, 1)
443 FIELD(CSR
, READY_FOR_DATA
, 8, 1)
444 FIELD(CSR
, CURRENT_STATE
, 9, 4)
445 FIELD(CSR
, ERASE_RESET
, 13, 1)
446 FIELD(CSR
, CARD_ECC_DISABLED
, 14, 1)
447 FIELD(CSR
, WP_ERASE_SKIP
, 15, 1)
448 FIELD(CSR
, CSD_OVERWRITE
, 16, 1)
449 FIELD(CSR
, DEFERRED_RESPONSE
, 17, 1)
450 FIELD(CSR
, ERROR
, 19, 1)
451 FIELD(CSR
, CC_ERROR
, 20, 1)
452 FIELD(CSR
, CARD_ECC_FAILED
, 21, 1)
453 FIELD(CSR
, ILLEGAL_COMMAND
, 22, 1)
454 FIELD(CSR
, COM_CRC_ERROR
, 23, 1)
455 FIELD(CSR
, LOCK_UNLOCK_FAILED
, 24, 1)
456 FIELD(CSR
, CARD_IS_LOCKED
, 25, 1)
457 FIELD(CSR
, WP_VIOLATION
, 26, 1)
458 FIELD(CSR
, ERASE_PARAM
, 27, 1)
459 FIELD(CSR
, ERASE_SEQ_ERROR
, 28, 1)
460 FIELD(CSR
, BLOCK_LEN_ERROR
, 29, 1)
461 FIELD(CSR
, ADDRESS_ERROR
, 30, 1)
462 FIELD(CSR
, OUT_OF_RANGE
, 31, 1)
464 /* Card status bits, split by clear condition:
465 * A : According to the card current state
466 * B : Always related to the previous command
467 * C : Cleared by read
469 #define CARD_STATUS_A (R_CSR_READY_FOR_DATA_MASK \
470 | R_CSR_CARD_ECC_DISABLED_MASK \
471 | R_CSR_CARD_IS_LOCKED_MASK)
472 #define CARD_STATUS_B (R_CSR_CURRENT_STATE_MASK \
473 | R_CSR_ILLEGAL_COMMAND_MASK \
474 | R_CSR_COM_CRC_ERROR_MASK)
475 #define CARD_STATUS_C (R_CSR_AKE_SEQ_ERROR_MASK \
476 | R_CSR_APP_CMD_MASK \
477 | R_CSR_ERASE_RESET_MASK \
478 | R_CSR_WP_ERASE_SKIP_MASK \
479 | R_CSR_CSD_OVERWRITE_MASK \
481 | R_CSR_CC_ERROR_MASK \
482 | R_CSR_CARD_ECC_FAILED_MASK \
483 | R_CSR_LOCK_UNLOCK_FAILED_MASK \
484 | R_CSR_WP_VIOLATION_MASK \
485 | R_CSR_ERASE_PARAM_MASK \
486 | R_CSR_ERASE_SEQ_ERROR_MASK \
487 | R_CSR_BLOCK_LEN_ERROR_MASK \
488 | R_CSR_ADDRESS_ERROR_MASK \
489 | R_CSR_OUT_OF_RANGE_MASK)
491 static void sd_set_cardstatus(SDState
*sd
)
493 sd
->card_status
= 0x00000100;
496 static void sd_set_sdstatus(SDState
*sd
)
498 memset(sd
->sd_status
, 0, 64);
501 static int sd_req_crc_validate(SDRequest
*req
)
504 buffer
[0] = 0x40 | req
->cmd
;
505 stl_be_p(&buffer
[1], req
->arg
);
507 return sd_crc7(buffer
, 5) != req
->crc
; /* TODO */
510 static void sd_response_r1_make(SDState
*sd
, uint8_t *response
)
512 stl_be_p(response
, sd
->card_status
);
514 /* Clear the "clear on read" status bits */
515 sd
->card_status
&= ~CARD_STATUS_C
;
518 static void sd_response_r3_make(SDState
*sd
, uint8_t *response
)
520 stl_be_p(response
, sd
->ocr
& ACMD41_R3_MASK
);
523 static void sd_response_r6_make(SDState
*sd
, uint8_t *response
)
527 status
= ((sd
->card_status
>> 8) & 0xc000) |
528 ((sd
->card_status
>> 6) & 0x2000) |
529 (sd
->card_status
& 0x1fff);
530 sd
->card_status
&= ~(CARD_STATUS_C
& 0xc81fff);
531 stw_be_p(response
+ 0, sd
->rca
);
532 stw_be_p(response
+ 2, status
);
535 static void sd_response_r7_make(SDState
*sd
, uint8_t *response
)
537 stl_be_p(response
, sd
->vhs
);
540 static inline uint64_t sd_addr_to_wpnum(uint64_t addr
)
542 return addr
>> (HWBLOCK_SHIFT
+ SECTOR_SHIFT
+ WPGROUP_SHIFT
);
545 static void sd_reset(DeviceState
*dev
)
547 SDState
*sd
= SD_CARD(dev
);
551 trace_sdcard_reset();
553 blk_get_geometry(sd
->blk
, §
);
559 sect
= sd_addr_to_wpnum(size
) + 1;
561 sd
->state
= sd_idle_state
;
566 sd_set_csd(sd
, size
);
567 sd_set_cardstatus(sd
);
570 g_free(sd
->wp_groups
);
571 sd
->wp_switch
= sd
->blk
? blk_is_read_only(sd
->blk
) : false;
572 sd
->wpgrps_size
= sect
;
573 sd
->wp_groups
= bitmap_new(sd
->wpgrps_size
);
574 memset(sd
->function_group
, 0, sizeof(sd
->function_group
));
580 sd
->expecting_acmd
= false;
583 sd
->multi_blk_cnt
= 0;
586 static bool sd_get_inserted(SDState
*sd
)
588 return sd
->blk
&& blk_is_inserted(sd
->blk
);
591 static bool sd_get_readonly(SDState
*sd
)
593 return sd
->wp_switch
;
596 static void sd_cardchange(void *opaque
, bool load
, Error
**errp
)
598 SDState
*sd
= opaque
;
599 DeviceState
*dev
= DEVICE(sd
);
601 bool inserted
= sd_get_inserted(sd
);
602 bool readonly
= sd_get_readonly(sd
);
605 trace_sdcard_inserted(readonly
);
608 trace_sdcard_ejected();
611 if (sd
->me_no_qdev_me_kill_mammoth_with_rocks
) {
612 qemu_set_irq(sd
->inserted_cb
, inserted
);
614 qemu_set_irq(sd
->readonly_cb
, readonly
);
617 sdbus
= SD_BUS(qdev_get_parent_bus(dev
));
618 sdbus_set_inserted(sdbus
, inserted
);
620 sdbus_set_readonly(sdbus
, readonly
);
625 static const BlockDevOps sd_block_ops
= {
626 .change_media_cb
= sd_cardchange
,
629 static bool sd_ocr_vmstate_needed(void *opaque
)
631 SDState
*sd
= opaque
;
633 /* Include the OCR state (and timer) if it is not yet powered up */
634 return !FIELD_EX32(sd
->ocr
, OCR
, CARD_POWER_UP
);
637 static const VMStateDescription sd_ocr_vmstate
= {
638 .name
= "sd-card/ocr-state",
640 .minimum_version_id
= 1,
641 .needed
= sd_ocr_vmstate_needed
,
642 .fields
= (VMStateField
[]) {
643 VMSTATE_UINT32(ocr
, SDState
),
644 VMSTATE_TIMER_PTR(ocr_power_timer
, SDState
),
645 VMSTATE_END_OF_LIST()
649 static int sd_vmstate_pre_load(void *opaque
)
651 SDState
*sd
= opaque
;
653 /* If the OCR state is not included (prior versions, or not
654 * needed), then the OCR must be set as powered up. If the OCR state
655 * is included, this will be replaced by the state restore.
662 static const VMStateDescription sd_vmstate
= {
665 .minimum_version_id
= 1,
666 .pre_load
= sd_vmstate_pre_load
,
667 .fields
= (VMStateField
[]) {
668 VMSTATE_UINT32(mode
, SDState
),
669 VMSTATE_INT32(state
, SDState
),
670 VMSTATE_UINT8_ARRAY(cid
, SDState
, 16),
671 VMSTATE_UINT8_ARRAY(csd
, SDState
, 16),
672 VMSTATE_UINT16(rca
, SDState
),
673 VMSTATE_UINT32(card_status
, SDState
),
674 VMSTATE_PARTIAL_BUFFER(sd_status
, SDState
, 1),
675 VMSTATE_UINT32(vhs
, SDState
),
676 VMSTATE_BITMAP(wp_groups
, SDState
, 0, wpgrps_size
),
677 VMSTATE_UINT32(blk_len
, SDState
),
678 VMSTATE_UINT32(multi_blk_cnt
, SDState
),
679 VMSTATE_UINT32(erase_start
, SDState
),
680 VMSTATE_UINT32(erase_end
, SDState
),
681 VMSTATE_UINT8_ARRAY(pwd
, SDState
, 16),
682 VMSTATE_UINT32(pwd_len
, SDState
),
683 VMSTATE_UINT8_ARRAY(function_group
, SDState
, 6),
684 VMSTATE_UINT8(current_cmd
, SDState
),
685 VMSTATE_BOOL(expecting_acmd
, SDState
),
686 VMSTATE_UINT32(blk_written
, SDState
),
687 VMSTATE_UINT64(data_start
, SDState
),
688 VMSTATE_UINT32(data_offset
, SDState
),
689 VMSTATE_UINT8_ARRAY(data
, SDState
, 512),
690 VMSTATE_UNUSED_V(1, 512),
691 VMSTATE_BOOL(enable
, SDState
),
692 VMSTATE_END_OF_LIST()
694 .subsections
= (const VMStateDescription
*[]) {
700 /* Legacy initialization function for use by non-qdevified callers */
701 SDState
*sd_init(BlockBackend
*blk
, bool is_spi
)
708 obj
= object_new(TYPE_SD_CARD
);
710 if (!qdev_prop_set_drive_err(dev
, "drive", blk
, &err
)) {
711 error_reportf_err(err
, "sd_init failed: ");
714 qdev_prop_set_bit(dev
, "spi", is_spi
);
717 * Realizing the device properly would put it into the QOM
718 * composition tree even though it is not plugged into an
719 * appropriate bus. That's a no-no. Hide the device from
720 * QOM/qdev, and call its qdev realize callback directly.
723 object_unparent(obj
);
724 sd_realize(dev
, &err
);
726 error_reportf_err(err
, "sd_init failed: ");
731 sd
->me_no_qdev_me_kill_mammoth_with_rocks
= true;
735 void sd_set_cb(SDState
*sd
, qemu_irq readonly
, qemu_irq insert
)
737 sd
->readonly_cb
= readonly
;
738 sd
->inserted_cb
= insert
;
739 qemu_set_irq(readonly
, sd
->blk
? blk_is_read_only(sd
->blk
) : 0);
740 qemu_set_irq(insert
, sd
->blk
? blk_is_inserted(sd
->blk
) : 0);
743 static void sd_erase(SDState
*sd
)
746 uint64_t erase_start
= sd
->erase_start
;
747 uint64_t erase_end
= sd
->erase_end
;
749 trace_sdcard_erase();
750 if (!sd
->erase_start
|| !sd
->erase_end
) {
751 sd
->card_status
|= ERASE_SEQ_ERROR
;
755 if (FIELD_EX32(sd
->ocr
, OCR
, CARD_CAPACITY
)) {
756 /* High capacity memory card: erase units are 512 byte blocks */
761 erase_start
= sd_addr_to_wpnum(erase_start
);
762 erase_end
= sd_addr_to_wpnum(erase_end
);
767 for (i
= erase_start
; i
<= erase_end
; i
++) {
768 if (test_bit(i
, sd
->wp_groups
)) {
769 sd
->card_status
|= WP_ERASE_SKIP
;
774 static uint32_t sd_wpbits(SDState
*sd
, uint64_t addr
)
779 wpnum
= sd_addr_to_wpnum(addr
);
781 for (i
= 0; i
< 32; i
++, wpnum
++, addr
+= WPGROUP_SIZE
) {
782 if (addr
< sd
->size
&& test_bit(wpnum
, sd
->wp_groups
)) {
790 static void sd_function_switch(SDState
*sd
, uint32_t arg
)
792 int i
, mode
, new_func
;
793 mode
= !!(arg
& 0x80000000);
795 sd
->data
[0] = 0x00; /* Maximum current consumption */
797 sd
->data
[2] = 0x80; /* Supported group 6 functions */
799 sd
->data
[4] = 0x80; /* Supported group 5 functions */
801 sd
->data
[6] = 0x80; /* Supported group 4 functions */
803 sd
->data
[8] = 0x80; /* Supported group 3 functions */
805 sd
->data
[10] = 0x80; /* Supported group 2 functions */
807 sd
->data
[12] = 0x80; /* Supported group 1 functions */
809 for (i
= 0; i
< 6; i
++) {
810 new_func
= (arg
>> (i
* 4)) & 0x0f;
811 if (mode
&& new_func
!= 0x0f)
812 sd
->function_group
[i
] = new_func
;
813 sd
->data
[14 + (i
>> 1)] = new_func
<< ((i
* 4) & 4);
815 memset(&sd
->data
[17], 0, 47);
816 stw_be_p(sd
->data
+ 64, sd_crc16(sd
->data
, 64));
819 static inline bool sd_wp_addr(SDState
*sd
, uint64_t addr
)
821 return test_bit(sd_addr_to_wpnum(addr
), sd
->wp_groups
);
824 static void sd_lock_command(SDState
*sd
)
826 int erase
, lock
, clr_pwd
, set_pwd
, pwd_len
;
827 erase
= !!(sd
->data
[0] & 0x08);
828 lock
= sd
->data
[0] & 0x04;
829 clr_pwd
= sd
->data
[0] & 0x02;
830 set_pwd
= sd
->data
[0] & 0x01;
833 pwd_len
= sd
->data
[1];
840 trace_sdcard_unlock();
843 if (!(sd
->card_status
& CARD_IS_LOCKED
) || sd
->blk_len
> 1 ||
844 set_pwd
|| clr_pwd
|| lock
|| sd
->wp_switch
||
845 (sd
->csd
[14] & 0x20)) {
846 sd
->card_status
|= LOCK_UNLOCK_FAILED
;
849 bitmap_zero(sd
->wp_groups
, sd
->wpgrps_size
);
850 sd
->csd
[14] &= ~0x10;
851 sd
->card_status
&= ~CARD_IS_LOCKED
;
853 /* Erasing the entire card here! */
854 fprintf(stderr
, "SD: Card force-erased by CMD42\n");
858 if (sd
->blk_len
< 2 + pwd_len
||
859 pwd_len
<= sd
->pwd_len
||
860 pwd_len
> sd
->pwd_len
+ 16) {
861 sd
->card_status
|= LOCK_UNLOCK_FAILED
;
865 if (sd
->pwd_len
&& memcmp(sd
->pwd
, sd
->data
+ 2, sd
->pwd_len
)) {
866 sd
->card_status
|= LOCK_UNLOCK_FAILED
;
870 pwd_len
-= sd
->pwd_len
;
871 if ((pwd_len
&& !set_pwd
) ||
872 (clr_pwd
&& (set_pwd
|| lock
)) ||
873 (lock
&& !sd
->pwd_len
&& !set_pwd
) ||
874 (!set_pwd
&& !clr_pwd
&&
875 (((sd
->card_status
& CARD_IS_LOCKED
) && lock
) ||
876 (!(sd
->card_status
& CARD_IS_LOCKED
) && !lock
)))) {
877 sd
->card_status
|= LOCK_UNLOCK_FAILED
;
882 memcpy(sd
->pwd
, sd
->data
+ 2 + sd
->pwd_len
, pwd_len
);
883 sd
->pwd_len
= pwd_len
;
891 sd
->card_status
|= CARD_IS_LOCKED
;
893 sd
->card_status
&= ~CARD_IS_LOCKED
;
896 static sd_rsp_type_t
sd_normal_command(SDState
*sd
, SDRequest req
)
898 uint32_t rca
= 0x0000;
899 uint64_t addr
= (sd
->ocr
& (1 << 30)) ? (uint64_t) req
.arg
<< 9 : req
.arg
;
901 /* CMD55 precedes an ACMD, so we are not interested in tracing it.
902 * However there is no ACMD55, so we want to trace this particular case.
904 if (req
.cmd
!= 55 || sd
->expecting_acmd
) {
905 trace_sdcard_normal_command(sd
->proto_name
,
906 sd_cmd_name(req
.cmd
), req
.cmd
,
907 req
.arg
, sd_state_name(sd
->state
));
910 /* Not interpreting this as an app command */
911 sd
->card_status
&= ~APP_CMD
;
913 if (sd_cmd_type
[req
.cmd
] == sd_ac
914 || sd_cmd_type
[req
.cmd
] == sd_adtc
) {
918 /* CMD23 (set block count) must be immediately followed by CMD18 or CMD25
919 * if not, its effects are cancelled */
920 if (sd
->multi_blk_cnt
!= 0 && !(req
.cmd
== 18 || req
.cmd
== 25)) {
921 sd
->multi_blk_cnt
= 0;
924 if (sd_cmd_class
[req
.cmd
] == 6 && FIELD_EX32(sd
->ocr
, OCR
, CARD_CAPACITY
)) {
925 /* Only Standard Capacity cards support class 6 commands */
930 /* Basic commands (Class 0 and Class 1) */
931 case 0: /* CMD0: GO_IDLE_STATE */
933 case sd_inactive_state
:
934 return sd
->spi
? sd_r1
: sd_r0
;
937 sd
->state
= sd_idle_state
;
938 sd_reset(DEVICE(sd
));
939 return sd
->spi
? sd_r1
: sd_r0
;
943 case 1: /* CMD1: SEND_OP_CMD */
947 sd
->state
= sd_transfer_state
;
950 case 2: /* CMD2: ALL_SEND_CID */
955 sd
->state
= sd_identification_state
;
963 case 3: /* CMD3: SEND_RELATIVE_ADDR */
967 case sd_identification_state
:
968 case sd_standby_state
:
969 sd
->state
= sd_standby_state
;
978 case 4: /* CMD4: SEND_DSR */
982 case sd_standby_state
:
990 case 5: /* CMD5: reserved for SDIO cards */
993 case 6: /* CMD6: SWITCH_FUNCTION */
995 case sd_data_transfer_mode
:
996 sd_function_switch(sd
, req
.arg
);
997 sd
->state
= sd_sendingdata_state
;
1007 case 7: /* CMD7: SELECT/DESELECT_CARD */
1010 switch (sd
->state
) {
1011 case sd_standby_state
:
1015 sd
->state
= sd_transfer_state
;
1018 case sd_transfer_state
:
1019 case sd_sendingdata_state
:
1023 sd
->state
= sd_standby_state
;
1026 case sd_disconnect_state
:
1030 sd
->state
= sd_programming_state
;
1033 case sd_programming_state
:
1037 sd
->state
= sd_disconnect_state
;
1045 case 8: /* CMD8: SEND_IF_COND */
1046 if (sd
->spec_version
< SD_PHY_SPECv2_00_VERS
) {
1049 if (sd
->state
!= sd_idle_state
) {
1054 /* No response if not exactly one VHS bit is set. */
1055 if (!(req
.arg
>> 8) || (req
.arg
>> (ctz32(req
.arg
& ~0xff) + 1))) {
1056 return sd
->spi
? sd_r7
: sd_r0
;
1063 case 9: /* CMD9: SEND_CSD */
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
->csd
, 16);
1076 sd
->data_start
= addr
;
1077 sd
->data_offset
= 0;
1085 case 10: /* CMD10: SEND_CID */
1086 switch (sd
->state
) {
1087 case sd_standby_state
:
1093 case sd_transfer_state
:
1096 sd
->state
= sd_sendingdata_state
;
1097 memcpy(sd
->data
, sd
->cid
, 16);
1098 sd
->data_start
= addr
;
1099 sd
->data_offset
= 0;
1107 case 12: /* CMD12: STOP_TRANSMISSION */
1108 switch (sd
->state
) {
1109 case sd_sendingdata_state
:
1110 sd
->state
= sd_transfer_state
;
1113 case sd_receivingdata_state
:
1114 sd
->state
= sd_programming_state
;
1115 /* Bzzzzzzztt .... Operation complete. */
1116 sd
->state
= sd_transfer_state
;
1124 case 13: /* CMD13: SEND_STATUS */
1126 case sd_data_transfer_mode
:
1137 case 15: /* CMD15: GO_INACTIVE_STATE */
1141 case sd_data_transfer_mode
:
1145 sd
->state
= sd_inactive_state
;
1153 /* Block read commands (Classs 2) */
1154 case 16: /* CMD16: SET_BLOCKLEN */
1155 switch (sd
->state
) {
1156 case sd_transfer_state
:
1157 if (req
.arg
> (1 << HWBLOCK_SHIFT
)) {
1158 sd
->card_status
|= BLOCK_LEN_ERROR
;
1160 trace_sdcard_set_blocklen(req
.arg
);
1161 sd
->blk_len
= req
.arg
;
1171 case 17: /* CMD17: READ_SINGLE_BLOCK */
1172 switch (sd
->state
) {
1173 case sd_transfer_state
:
1175 if (addr
+ sd
->blk_len
> sd
->size
) {
1176 sd
->card_status
|= ADDRESS_ERROR
;
1180 sd
->state
= sd_sendingdata_state
;
1181 sd
->data_start
= addr
;
1182 sd
->data_offset
= 0;
1190 case 18: /* CMD18: READ_MULTIPLE_BLOCK */
1191 switch (sd
->state
) {
1192 case sd_transfer_state
:
1194 if (addr
+ sd
->blk_len
> sd
->size
) {
1195 sd
->card_status
|= ADDRESS_ERROR
;
1199 sd
->state
= sd_sendingdata_state
;
1200 sd
->data_start
= addr
;
1201 sd
->data_offset
= 0;
1209 case 19: /* CMD19: SEND_TUNING_BLOCK (SD) */
1210 if (sd
->spec_version
< SD_PHY_SPECv3_01_VERS
) {
1213 if (sd
->state
== sd_transfer_state
) {
1214 sd
->state
= sd_sendingdata_state
;
1215 sd
->data_offset
= 0;
1220 case 23: /* CMD23: SET_BLOCK_COUNT */
1221 if (sd
->spec_version
< SD_PHY_SPECv3_01_VERS
) {
1224 switch (sd
->state
) {
1225 case sd_transfer_state
:
1226 sd
->multi_blk_cnt
= req
.arg
;
1234 /* Block write commands (Class 4) */
1235 case 24: /* CMD24: WRITE_SINGLE_BLOCK */
1236 switch (sd
->state
) {
1237 case sd_transfer_state
:
1238 /* Writing in SPI mode not implemented. */
1242 if (addr
+ sd
->blk_len
> sd
->size
) {
1243 sd
->card_status
|= ADDRESS_ERROR
;
1247 sd
->state
= sd_receivingdata_state
;
1248 sd
->data_start
= addr
;
1249 sd
->data_offset
= 0;
1250 sd
->blk_written
= 0;
1252 if (sd_wp_addr(sd
, sd
->data_start
)) {
1253 sd
->card_status
|= WP_VIOLATION
;
1255 if (sd
->csd
[14] & 0x30) {
1256 sd
->card_status
|= WP_VIOLATION
;
1265 case 25: /* CMD25: WRITE_MULTIPLE_BLOCK */
1266 switch (sd
->state
) {
1267 case sd_transfer_state
:
1268 /* Writing in SPI mode not implemented. */
1272 if (addr
+ sd
->blk_len
> sd
->size
) {
1273 sd
->card_status
|= ADDRESS_ERROR
;
1277 sd
->state
= sd_receivingdata_state
;
1278 sd
->data_start
= addr
;
1279 sd
->data_offset
= 0;
1280 sd
->blk_written
= 0;
1282 if (sd_wp_addr(sd
, sd
->data_start
)) {
1283 sd
->card_status
|= WP_VIOLATION
;
1285 if (sd
->csd
[14] & 0x30) {
1286 sd
->card_status
|= WP_VIOLATION
;
1295 case 26: /* CMD26: PROGRAM_CID */
1298 switch (sd
->state
) {
1299 case sd_transfer_state
:
1300 sd
->state
= sd_receivingdata_state
;
1302 sd
->data_offset
= 0;
1310 case 27: /* CMD27: PROGRAM_CSD */
1311 switch (sd
->state
) {
1312 case sd_transfer_state
:
1313 sd
->state
= sd_receivingdata_state
;
1315 sd
->data_offset
= 0;
1323 /* Write protection (Class 6) */
1324 case 28: /* CMD28: SET_WRITE_PROT */
1325 switch (sd
->state
) {
1326 case sd_transfer_state
:
1327 if (addr
>= sd
->size
) {
1328 sd
->card_status
|= ADDRESS_ERROR
;
1332 sd
->state
= sd_programming_state
;
1333 set_bit(sd_addr_to_wpnum(addr
), sd
->wp_groups
);
1334 /* Bzzzzzzztt .... Operation complete. */
1335 sd
->state
= sd_transfer_state
;
1343 case 29: /* CMD29: CLR_WRITE_PROT */
1344 switch (sd
->state
) {
1345 case sd_transfer_state
:
1346 if (addr
>= sd
->size
) {
1347 sd
->card_status
|= ADDRESS_ERROR
;
1351 sd
->state
= sd_programming_state
;
1352 clear_bit(sd_addr_to_wpnum(addr
), sd
->wp_groups
);
1353 /* Bzzzzzzztt .... Operation complete. */
1354 sd
->state
= sd_transfer_state
;
1362 case 30: /* CMD30: SEND_WRITE_PROT */
1363 switch (sd
->state
) {
1364 case sd_transfer_state
:
1365 sd
->state
= sd_sendingdata_state
;
1366 *(uint32_t *) sd
->data
= sd_wpbits(sd
, req
.arg
);
1367 sd
->data_start
= addr
;
1368 sd
->data_offset
= 0;
1376 /* Erase commands (Class 5) */
1377 case 32: /* CMD32: ERASE_WR_BLK_START */
1378 switch (sd
->state
) {
1379 case sd_transfer_state
:
1380 sd
->erase_start
= req
.arg
;
1388 case 33: /* CMD33: ERASE_WR_BLK_END */
1389 switch (sd
->state
) {
1390 case sd_transfer_state
:
1391 sd
->erase_end
= req
.arg
;
1399 case 38: /* CMD38: ERASE */
1400 switch (sd
->state
) {
1401 case sd_transfer_state
:
1402 if (sd
->csd
[14] & 0x30) {
1403 sd
->card_status
|= WP_VIOLATION
;
1407 sd
->state
= sd_programming_state
;
1409 /* Bzzzzzzztt .... Operation complete. */
1410 sd
->state
= sd_transfer_state
;
1418 /* Lock card commands (Class 7) */
1419 case 42: /* CMD42: LOCK_UNLOCK */
1420 switch (sd
->state
) {
1421 case sd_transfer_state
:
1422 sd
->state
= sd_receivingdata_state
;
1424 sd
->data_offset
= 0;
1433 /* CMD52, CMD53, CMD54: reserved for SDIO cards
1434 * (see the SDIO Simplified Specification V2.0)
1435 * Handle as illegal command but do not complain
1436 * on stderr, as some OSes may use these in their
1437 * probing for presence of an SDIO card.
1441 /* Application specific commands (Class 8) */
1442 case 55: /* CMD55: APP_CMD */
1443 switch (sd
->state
) {
1444 case sd_ready_state
:
1445 case sd_identification_state
:
1446 case sd_inactive_state
:
1450 qemu_log_mask(LOG_GUEST_ERROR
,
1451 "SD: illegal RCA 0x%04x for APP_CMD\n", req
.cmd
);
1457 if (sd
->rca
!= rca
) {
1461 sd
->expecting_acmd
= true;
1462 sd
->card_status
|= APP_CMD
;
1465 case 56: /* CMD56: GEN_CMD */
1466 switch (sd
->state
) {
1467 case sd_transfer_state
:
1468 sd
->data_offset
= 0;
1470 sd
->state
= sd_sendingdata_state
;
1472 sd
->state
= sd_receivingdata_state
;
1480 case 58: /* CMD58: READ_OCR (SPI) */
1486 case 59: /* CMD59: CRC_ON_OFF (SPI) */
1490 goto unimplemented_spi_cmd
;
1494 qemu_log_mask(LOG_GUEST_ERROR
, "SD: Unknown CMD%i\n", req
.cmd
);
1497 unimplemented_spi_cmd
:
1498 /* Commands that are recognised but not yet implemented in SPI mode. */
1499 qemu_log_mask(LOG_UNIMP
, "SD: CMD%i not implemented in SPI mode\n",
1504 qemu_log_mask(LOG_GUEST_ERROR
, "SD: CMD%i in a wrong state\n", req
.cmd
);
1508 static sd_rsp_type_t
sd_app_command(SDState
*sd
,
1511 trace_sdcard_app_command(sd
->proto_name
, sd_acmd_name(req
.cmd
),
1512 req
.cmd
, req
.arg
, sd_state_name(sd
->state
));
1513 sd
->card_status
|= APP_CMD
;
1515 case 6: /* ACMD6: SET_BUS_WIDTH */
1517 goto unimplemented_spi_cmd
;
1519 switch (sd
->state
) {
1520 case sd_transfer_state
:
1521 sd
->sd_status
[0] &= 0x3f;
1522 sd
->sd_status
[0] |= (req
.arg
& 0x03) << 6;
1530 case 13: /* ACMD13: SD_STATUS */
1531 switch (sd
->state
) {
1532 case sd_transfer_state
:
1533 sd
->state
= sd_sendingdata_state
;
1535 sd
->data_offset
= 0;
1543 case 22: /* ACMD22: SEND_NUM_WR_BLOCKS */
1544 switch (sd
->state
) {
1545 case sd_transfer_state
:
1546 *(uint32_t *) sd
->data
= sd
->blk_written
;
1548 sd
->state
= sd_sendingdata_state
;
1550 sd
->data_offset
= 0;
1558 case 23: /* ACMD23: SET_WR_BLK_ERASE_COUNT */
1559 switch (sd
->state
) {
1560 case sd_transfer_state
:
1568 case 41: /* ACMD41: SD_APP_OP_COND */
1571 sd
->state
= sd_transfer_state
;
1574 if (sd
->state
!= sd_idle_state
) {
1577 /* If it's the first ACMD41 since reset, we need to decide
1578 * whether to power up. If this is not an enquiry ACMD41,
1579 * we immediately report power on and proceed below to the
1580 * ready state, but if it is, we set a timer to model a
1581 * delay for power up. This works around a bug in EDK2
1582 * UEFI, which sends an initial enquiry ACMD41, but
1583 * assumes that the card is in ready state as soon as it
1584 * sees the power up bit set. */
1585 if (!FIELD_EX32(sd
->ocr
, OCR
, CARD_POWER_UP
)) {
1586 if ((req
.arg
& ACMD41_ENQUIRY_MASK
) != 0) {
1587 timer_del(sd
->ocr_power_timer
);
1590 trace_sdcard_inquiry_cmd41();
1591 if (!timer_pending(sd
->ocr_power_timer
)) {
1592 timer_mod_ns(sd
->ocr_power_timer
,
1593 (qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
)
1594 + OCR_POWER_DELAY_NS
));
1599 if (FIELD_EX32(sd
->ocr
& req
.arg
, OCR
, VDD_VOLTAGE_WINDOW
)) {
1600 /* We accept any voltage. 10000 V is nothing.
1602 * Once we're powered up, we advance straight to ready state
1603 * unless it's an enquiry ACMD41 (bits 23:0 == 0).
1605 sd
->state
= sd_ready_state
;
1610 case 42: /* ACMD42: SET_CLR_CARD_DETECT */
1611 switch (sd
->state
) {
1612 case sd_transfer_state
:
1613 /* Bringing in the 50KOhm pull-up resistor... Done. */
1621 case 51: /* ACMD51: SEND_SCR */
1622 switch (sd
->state
) {
1623 case sd_transfer_state
:
1624 sd
->state
= sd_sendingdata_state
;
1626 sd
->data_offset
= 0;
1634 case 18: /* Reserved for SD security applications */
1639 /* Refer to the "SD Specifications Part3 Security Specification" for
1640 * information about the SD Security Features.
1642 qemu_log_mask(LOG_UNIMP
, "SD: CMD%i Security not implemented\n",
1647 /* Fall back to standard commands. */
1648 return sd_normal_command(sd
, req
);
1650 unimplemented_spi_cmd
:
1651 /* Commands that are recognised but not yet implemented in SPI mode. */
1652 qemu_log_mask(LOG_UNIMP
, "SD: CMD%i not implemented in SPI mode\n",
1657 qemu_log_mask(LOG_GUEST_ERROR
, "SD: ACMD%i in a wrong state\n", req
.cmd
);
1661 static int cmd_valid_while_locked(SDState
*sd
, SDRequest
*req
)
1663 /* Valid commands in locked state:
1665 * lock card class (7)
1667 * implicitly, the ACMD prefix CMD55
1669 * Anything else provokes an "illegal command" response.
1671 if (sd
->expecting_acmd
) {
1672 return req
->cmd
== 41 || req
->cmd
== 42;
1674 if (req
->cmd
== 16 || req
->cmd
== 55) {
1677 return sd_cmd_class
[req
->cmd
] == 0
1678 || sd_cmd_class
[req
->cmd
] == 7;
1681 int sd_do_command(SDState
*sd
, SDRequest
*req
,
1682 uint8_t *response
) {
1684 sd_rsp_type_t rtype
;
1687 if (!sd
->blk
|| !blk_is_inserted(sd
->blk
) || !sd
->enable
) {
1691 if (sd_req_crc_validate(req
)) {
1692 sd
->card_status
|= COM_CRC_ERROR
;
1697 if (req
->cmd
>= SDMMC_CMD_MAX
) {
1698 qemu_log_mask(LOG_GUEST_ERROR
, "SD: incorrect command 0x%02x\n",
1703 if (sd
->card_status
& CARD_IS_LOCKED
) {
1704 if (!cmd_valid_while_locked(sd
, req
)) {
1705 sd
->card_status
|= ILLEGAL_COMMAND
;
1706 sd
->expecting_acmd
= false;
1707 qemu_log_mask(LOG_GUEST_ERROR
, "SD: Card is locked\n");
1713 last_state
= sd
->state
;
1716 if (sd
->expecting_acmd
) {
1717 sd
->expecting_acmd
= false;
1718 rtype
= sd_app_command(sd
, *req
);
1720 rtype
= sd_normal_command(sd
, *req
);
1723 if (rtype
== sd_illegal
) {
1724 sd
->card_status
|= ILLEGAL_COMMAND
;
1726 /* Valid command, we can update the 'state before command' bits.
1727 * (Do this now so they appear in r1 responses.)
1729 sd
->current_cmd
= req
->cmd
;
1730 sd
->card_status
&= ~CURRENT_STATE
;
1731 sd
->card_status
|= (last_state
<< 9);
1738 sd_response_r1_make(sd
, response
);
1743 memcpy(response
, sd
->cid
, sizeof(sd
->cid
));
1748 memcpy(response
, sd
->csd
, sizeof(sd
->csd
));
1753 sd_response_r3_make(sd
, response
);
1758 sd_response_r6_make(sd
, response
);
1763 sd_response_r7_make(sd
, response
);
1772 g_assert_not_reached();
1774 trace_sdcard_response(sd_response_name(rtype
), rsplen
);
1776 if (rtype
!= sd_illegal
) {
1777 /* Clear the "clear on valid command" status bits now we've
1780 sd
->card_status
&= ~CARD_STATUS_B
;
1784 qemu_hexdump((const char *)response
, stderr
, "Response", rsplen
);
1790 static void sd_blk_read(SDState
*sd
, uint64_t addr
, uint32_t len
)
1792 trace_sdcard_read_block(addr
, len
);
1793 if (!sd
->blk
|| blk_pread(sd
->blk
, addr
, sd
->data
, len
) < 0) {
1794 fprintf(stderr
, "sd_blk_read: read error on host side\n");
1798 static void sd_blk_write(SDState
*sd
, uint64_t addr
, uint32_t len
)
1800 trace_sdcard_write_block(addr
, len
);
1801 if (!sd
->blk
|| blk_pwrite(sd
->blk
, addr
, sd
->data
, len
, 0) < 0) {
1802 fprintf(stderr
, "sd_blk_write: write error on host side\n");
1806 #define BLK_READ_BLOCK(a, len) sd_blk_read(sd, a, len)
1807 #define BLK_WRITE_BLOCK(a, len) sd_blk_write(sd, a, len)
1808 #define APP_READ_BLOCK(a, len) memset(sd->data, 0xec, len)
1809 #define APP_WRITE_BLOCK(a, len)
1811 void sd_write_data(SDState
*sd
, uint8_t value
)
1815 if (!sd
->blk
|| !blk_is_inserted(sd
->blk
) || !sd
->enable
)
1818 if (sd
->state
!= sd_receivingdata_state
) {
1819 qemu_log_mask(LOG_GUEST_ERROR
,
1820 "sd_write_data: not in Receiving-Data state\n");
1824 if (sd
->card_status
& (ADDRESS_ERROR
| WP_VIOLATION
))
1827 trace_sdcard_write_data(sd
->proto_name
,
1828 sd_acmd_name(sd
->current_cmd
),
1829 sd
->current_cmd
, value
);
1830 switch (sd
->current_cmd
) {
1831 case 24: /* CMD24: WRITE_SINGLE_BLOCK */
1832 sd
->data
[sd
->data_offset
++] = value
;
1833 if (sd
->data_offset
>= sd
->blk_len
) {
1834 /* TODO: Check CRC before committing */
1835 sd
->state
= sd_programming_state
;
1836 BLK_WRITE_BLOCK(sd
->data_start
, sd
->data_offset
);
1838 sd
->csd
[14] |= 0x40;
1839 /* Bzzzzzzztt .... Operation complete. */
1840 sd
->state
= sd_transfer_state
;
1844 case 25: /* CMD25: WRITE_MULTIPLE_BLOCK */
1845 if (sd
->data_offset
== 0) {
1846 /* Start of the block - let's check the address is valid */
1847 if (sd
->data_start
+ sd
->blk_len
> sd
->size
) {
1848 sd
->card_status
|= ADDRESS_ERROR
;
1851 if (sd_wp_addr(sd
, sd
->data_start
)) {
1852 sd
->card_status
|= WP_VIOLATION
;
1856 sd
->data
[sd
->data_offset
++] = value
;
1857 if (sd
->data_offset
>= sd
->blk_len
) {
1858 /* TODO: Check CRC before committing */
1859 sd
->state
= sd_programming_state
;
1860 BLK_WRITE_BLOCK(sd
->data_start
, sd
->data_offset
);
1862 sd
->data_start
+= sd
->blk_len
;
1863 sd
->data_offset
= 0;
1864 sd
->csd
[14] |= 0x40;
1866 /* Bzzzzzzztt .... Operation complete. */
1867 if (sd
->multi_blk_cnt
!= 0) {
1868 if (--sd
->multi_blk_cnt
== 0) {
1870 sd
->state
= sd_transfer_state
;
1875 sd
->state
= sd_receivingdata_state
;
1879 case 26: /* CMD26: PROGRAM_CID */
1880 sd
->data
[sd
->data_offset
++] = value
;
1881 if (sd
->data_offset
>= sizeof(sd
->cid
)) {
1882 /* TODO: Check CRC before committing */
1883 sd
->state
= sd_programming_state
;
1884 for (i
= 0; i
< sizeof(sd
->cid
); i
++)
1885 if ((sd
->cid
[i
] | 0x00) != sd
->data
[i
])
1886 sd
->card_status
|= CID_CSD_OVERWRITE
;
1888 if (!(sd
->card_status
& CID_CSD_OVERWRITE
))
1889 for (i
= 0; i
< sizeof(sd
->cid
); i
++) {
1891 sd
->cid
[i
] &= sd
->data
[i
];
1893 /* Bzzzzzzztt .... Operation complete. */
1894 sd
->state
= sd_transfer_state
;
1898 case 27: /* CMD27: PROGRAM_CSD */
1899 sd
->data
[sd
->data_offset
++] = value
;
1900 if (sd
->data_offset
>= sizeof(sd
->csd
)) {
1901 /* TODO: Check CRC before committing */
1902 sd
->state
= sd_programming_state
;
1903 for (i
= 0; i
< sizeof(sd
->csd
); i
++)
1904 if ((sd
->csd
[i
] | sd_csd_rw_mask
[i
]) !=
1905 (sd
->data
[i
] | sd_csd_rw_mask
[i
]))
1906 sd
->card_status
|= CID_CSD_OVERWRITE
;
1908 /* Copy flag (OTP) & Permanent write protect */
1909 if (sd
->csd
[14] & ~sd
->data
[14] & 0x60)
1910 sd
->card_status
|= CID_CSD_OVERWRITE
;
1912 if (!(sd
->card_status
& CID_CSD_OVERWRITE
))
1913 for (i
= 0; i
< sizeof(sd
->csd
); i
++) {
1914 sd
->csd
[i
] |= sd_csd_rw_mask
[i
];
1915 sd
->csd
[i
] &= sd
->data
[i
];
1917 /* Bzzzzzzztt .... Operation complete. */
1918 sd
->state
= sd_transfer_state
;
1922 case 42: /* CMD42: LOCK_UNLOCK */
1923 sd
->data
[sd
->data_offset
++] = value
;
1924 if (sd
->data_offset
>= sd
->blk_len
) {
1925 /* TODO: Check CRC before committing */
1926 sd
->state
= sd_programming_state
;
1927 sd_lock_command(sd
);
1928 /* Bzzzzzzztt .... Operation complete. */
1929 sd
->state
= sd_transfer_state
;
1933 case 56: /* CMD56: GEN_CMD */
1934 sd
->data
[sd
->data_offset
++] = value
;
1935 if (sd
->data_offset
>= sd
->blk_len
) {
1936 APP_WRITE_BLOCK(sd
->data_start
, sd
->data_offset
);
1937 sd
->state
= sd_transfer_state
;
1942 qemu_log_mask(LOG_GUEST_ERROR
, "sd_write_data: unknown command\n");
1947 #define SD_TUNING_BLOCK_SIZE 64
1949 static const uint8_t sd_tuning_block_pattern
[SD_TUNING_BLOCK_SIZE
] = {
1950 /* See: Physical Layer Simplified Specification Version 3.01, Table 4-2 */
1951 0xff, 0x0f, 0xff, 0x00, 0x0f, 0xfc, 0xc3, 0xcc,
1952 0xc3, 0x3c, 0xcc, 0xff, 0xfe, 0xff, 0xfe, 0xef,
1953 0xff, 0xdf, 0xff, 0xdd, 0xff, 0xfb, 0xff, 0xfb,
1954 0xbf, 0xff, 0x7f, 0xff, 0x77, 0xf7, 0xbd, 0xef,
1955 0xff, 0xf0, 0xff, 0xf0, 0x0f, 0xfc, 0xcc, 0x3c,
1956 0xcc, 0x33, 0xcc, 0xcf, 0xff, 0xef, 0xff, 0xee,
1957 0xff, 0xfd, 0xff, 0xfd, 0xdf, 0xff, 0xbf, 0xff,
1958 0xbb, 0xff, 0xf7, 0xff, 0xf7, 0x7f, 0x7b, 0xde,
1961 uint8_t sd_read_data(SDState
*sd
)
1963 /* TODO: Append CRCs */
1967 if (!sd
->blk
|| !blk_is_inserted(sd
->blk
) || !sd
->enable
)
1970 if (sd
->state
!= sd_sendingdata_state
) {
1971 qemu_log_mask(LOG_GUEST_ERROR
,
1972 "sd_read_data: not in Sending-Data state\n");
1976 if (sd
->card_status
& (ADDRESS_ERROR
| WP_VIOLATION
))
1979 io_len
= (sd
->ocr
& (1 << 30)) ? 512 : sd
->blk_len
;
1981 trace_sdcard_read_data(sd
->proto_name
,
1982 sd_acmd_name(sd
->current_cmd
),
1983 sd
->current_cmd
, io_len
);
1984 switch (sd
->current_cmd
) {
1985 case 6: /* CMD6: SWITCH_FUNCTION */
1986 ret
= sd
->data
[sd
->data_offset
++];
1988 if (sd
->data_offset
>= 64)
1989 sd
->state
= sd_transfer_state
;
1992 case 9: /* CMD9: SEND_CSD */
1993 case 10: /* CMD10: SEND_CID */
1994 ret
= sd
->data
[sd
->data_offset
++];
1996 if (sd
->data_offset
>= 16)
1997 sd
->state
= sd_transfer_state
;
2000 case 13: /* ACMD13: SD_STATUS */
2001 ret
= sd
->sd_status
[sd
->data_offset
++];
2003 if (sd
->data_offset
>= sizeof(sd
->sd_status
))
2004 sd
->state
= sd_transfer_state
;
2007 case 17: /* CMD17: READ_SINGLE_BLOCK */
2008 if (sd
->data_offset
== 0)
2009 BLK_READ_BLOCK(sd
->data_start
, io_len
);
2010 ret
= sd
->data
[sd
->data_offset
++];
2012 if (sd
->data_offset
>= io_len
)
2013 sd
->state
= sd_transfer_state
;
2016 case 18: /* CMD18: READ_MULTIPLE_BLOCK */
2017 if (sd
->data_offset
== 0) {
2018 if (sd
->data_start
+ io_len
> sd
->size
) {
2019 sd
->card_status
|= ADDRESS_ERROR
;
2022 BLK_READ_BLOCK(sd
->data_start
, io_len
);
2024 ret
= sd
->data
[sd
->data_offset
++];
2026 if (sd
->data_offset
>= io_len
) {
2027 sd
->data_start
+= io_len
;
2028 sd
->data_offset
= 0;
2030 if (sd
->multi_blk_cnt
!= 0) {
2031 if (--sd
->multi_blk_cnt
== 0) {
2033 sd
->state
= sd_transfer_state
;
2040 case 19: /* CMD19: SEND_TUNING_BLOCK (SD) */
2041 if (sd
->data_offset
>= SD_TUNING_BLOCK_SIZE
- 1) {
2042 sd
->state
= sd_transfer_state
;
2044 ret
= sd_tuning_block_pattern
[sd
->data_offset
++];
2047 case 22: /* ACMD22: SEND_NUM_WR_BLOCKS */
2048 ret
= sd
->data
[sd
->data_offset
++];
2050 if (sd
->data_offset
>= 4)
2051 sd
->state
= sd_transfer_state
;
2054 case 30: /* CMD30: SEND_WRITE_PROT */
2055 ret
= sd
->data
[sd
->data_offset
++];
2057 if (sd
->data_offset
>= 4)
2058 sd
->state
= sd_transfer_state
;
2061 case 51: /* ACMD51: SEND_SCR */
2062 ret
= sd
->scr
[sd
->data_offset
++];
2064 if (sd
->data_offset
>= sizeof(sd
->scr
))
2065 sd
->state
= sd_transfer_state
;
2068 case 56: /* CMD56: GEN_CMD */
2069 if (sd
->data_offset
== 0)
2070 APP_READ_BLOCK(sd
->data_start
, sd
->blk_len
);
2071 ret
= sd
->data
[sd
->data_offset
++];
2073 if (sd
->data_offset
>= sd
->blk_len
)
2074 sd
->state
= sd_transfer_state
;
2078 qemu_log_mask(LOG_GUEST_ERROR
, "sd_read_data: unknown command\n");
2085 bool sd_data_ready(SDState
*sd
)
2087 return sd
->state
== sd_sendingdata_state
;
2090 void sd_enable(SDState
*sd
, bool enable
)
2092 sd
->enable
= enable
;
2095 static void sd_instance_init(Object
*obj
)
2097 SDState
*sd
= SD_CARD(obj
);
2100 sd
->ocr_power_timer
= timer_new_ns(QEMU_CLOCK_VIRTUAL
, sd_ocr_powerup
, sd
);
2103 static void sd_instance_finalize(Object
*obj
)
2105 SDState
*sd
= SD_CARD(obj
);
2107 timer_del(sd
->ocr_power_timer
);
2108 timer_free(sd
->ocr_power_timer
);
2111 static void sd_realize(DeviceState
*dev
, Error
**errp
)
2113 SDState
*sd
= SD_CARD(dev
);
2116 sd
->proto_name
= sd
->spi
? "SPI" : "SD";
2118 switch (sd
->spec_version
) {
2119 case SD_PHY_SPECv1_10_VERS
2120 ... SD_PHY_SPECv3_01_VERS
:
2123 error_setg(errp
, "Invalid SD card Spec version: %u", sd
->spec_version
);
2130 if (blk_is_read_only(sd
->blk
)) {
2131 error_setg(errp
, "Cannot use read-only drive as SD card");
2135 blk_size
= blk_getlength(sd
->blk
);
2136 if (blk_size
> 0 && !is_power_of_2(blk_size
)) {
2137 int64_t blk_size_aligned
= pow2ceil(blk_size
);
2140 blk_size_str
= size_to_str(blk_size
);
2141 error_setg(errp
, "Invalid SD card size: %s", blk_size_str
);
2142 g_free(blk_size_str
);
2144 blk_size_str
= size_to_str(blk_size_aligned
);
2145 error_append_hint(errp
,
2146 "SD card size has to be a power of 2, e.g. %s.\n"
2147 "You can resize disk images with"
2148 " 'qemu-img resize <imagefile> <new-size>'\n"
2149 "(note that this will lose data if you make the"
2150 " image smaller than it currently is).\n",
2152 g_free(blk_size_str
);
2157 ret
= blk_set_perm(sd
->blk
, BLK_PERM_CONSISTENT_READ
| BLK_PERM_WRITE
,
2158 BLK_PERM_ALL
, errp
);
2162 blk_set_dev_ops(sd
->blk
, &sd_block_ops
, sd
);
2166 static Property sd_properties
[] = {
2167 DEFINE_PROP_UINT8("spec_version", SDState
,
2168 spec_version
, SD_PHY_SPECv2_00_VERS
),
2169 DEFINE_PROP_DRIVE("drive", SDState
, blk
),
2170 /* We do not model the chip select pin, so allow the board to select
2171 * whether card should be in SSI or MMC/SD mode. It is also up to the
2172 * board to ensure that ssi transfers only occur when the chip select
2174 DEFINE_PROP_BOOL("spi", SDState
, spi
, false),
2175 DEFINE_PROP_END_OF_LIST()
2178 static void sd_class_init(ObjectClass
*klass
, void *data
)
2180 DeviceClass
*dc
= DEVICE_CLASS(klass
);
2181 SDCardClass
*sc
= SD_CARD_CLASS(klass
);
2183 dc
->realize
= sd_realize
;
2184 device_class_set_props(dc
, sd_properties
);
2185 dc
->vmsd
= &sd_vmstate
;
2186 dc
->reset
= sd_reset
;
2187 dc
->bus_type
= TYPE_SD_BUS
;
2188 set_bit(DEVICE_CATEGORY_STORAGE
, dc
->categories
);
2190 sc
->set_voltage
= sd_set_voltage
;
2191 sc
->get_dat_lines
= sd_get_dat_lines
;
2192 sc
->get_cmd_line
= sd_get_cmd_line
;
2193 sc
->do_command
= sd_do_command
;
2194 sc
->write_data
= sd_write_data
;
2195 sc
->read_data
= sd_read_data
;
2196 sc
->data_ready
= sd_data_ready
;
2197 sc
->enable
= sd_enable
;
2198 sc
->get_inserted
= sd_get_inserted
;
2199 sc
->get_readonly
= sd_get_readonly
;
2202 static const TypeInfo sd_info
= {
2203 .name
= TYPE_SD_CARD
,
2204 .parent
= TYPE_DEVICE
,
2205 .instance_size
= sizeof(SDState
),
2206 .class_size
= sizeof(SDCardClass
),
2207 .class_init
= sd_class_init
,
2208 .instance_init
= sd_instance_init
,
2209 .instance_finalize
= sd_instance_finalize
,
2212 static void sd_register_types(void)
2214 type_register_static(&sd_info
);
2217 type_init(sd_register_types
)