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 "hw/sd/sdcard_legacy.h"
41 #include "migration/vmstate.h"
42 #include "qapi/error.h"
43 #include "qemu/bitmap.h"
44 #include "hw/qdev-properties.h"
45 #include "hw/qdev-properties-system.h"
46 #include "qemu/error-report.h"
47 #include "qemu/timer.h"
49 #include "qemu/module.h"
50 #include "sdmmc-internal.h"
55 #define SDSC_MAX_CAPACITY (2 * GiB)
57 #define INVALID_ADDRESS UINT32_MAX
60 sd_r0
= 0, /* no response */
61 sd_r1
, /* normal response command */
62 sd_r2_i
, /* CID register */
63 sd_r2_s
, /* CSD register */
64 sd_r3
, /* OCR register */
65 sd_r6
= 6, /* Published RCA response */
66 sd_r7
, /* Operating voltage */
73 sd_card_identification_mode
,
74 sd_data_transfer_mode
,
78 sd_inactive_state
= -1,
81 sd_identification_state
= 2,
83 sd_transfer_state
= 4,
84 sd_sendingdata_state
= 5,
85 sd_receivingdata_state
= 6,
86 sd_programming_state
= 7,
87 sd_disconnect_state
= 8,
90 typedef sd_rsp_type_t (*sd_cmd_handler
)(SDState
*sd
, SDRequest req
);
92 typedef struct SDProto
{
94 sd_cmd_handler cmd
[SDMMC_CMD_MAX
];
95 sd_cmd_handler acmd
[SDMMC_CMD_MAX
];
99 DeviceState parent_obj
;
101 /* If true, created by sd_init() for a non-qdevified caller */
102 /* TODO purge them with fire */
103 bool me_no_qdev_me_kill_mammoth_with_rocks
;
105 /* SD Memory Card Registers */
111 uint32_t card_status
;
112 uint8_t sd_status
[64];
114 /* Static properties */
116 uint8_t spec_version
;
119 const SDProto
*proto
;
121 /* Runtime changeables */
123 uint32_t mode
; /* current card mode, one of SDCardModes */
124 int32_t state
; /* current card state, one of SDCardStates */
127 unsigned long *wp_group_bmap
;
128 int32_t wp_group_bits
;
131 uint32_t multi_blk_cnt
;
132 uint32_t erase_start
;
136 uint8_t function_group
[6];
138 const char *last_cmd_name
;
139 /* True if we will handle the next command as an ACMD. Note that this does
140 * *not* track the APP_CMD status bit!
143 uint32_t blk_written
;
146 uint32_t data_offset
;
149 qemu_irq readonly_cb
;
150 qemu_irq inserted_cb
;
151 QEMUTimer
*ocr_power_timer
;
157 static void sd_realize(DeviceState
*dev
, Error
**errp
);
159 static const SDProto sd_proto_spi
;
161 static bool sd_is_spi(SDState
*sd
)
163 return sd
->proto
== &sd_proto_spi
;
166 static const char *sd_version_str(enum SDPhySpecificationVersion version
)
168 static const char *sdphy_version
[] = {
169 [SD_PHY_SPECv1_10_VERS
] = "v1.10",
170 [SD_PHY_SPECv2_00_VERS
] = "v2.00",
171 [SD_PHY_SPECv3_01_VERS
] = "v3.01",
173 if (version
>= ARRAY_SIZE(sdphy_version
)) {
174 return "unsupported version";
176 return sdphy_version
[version
];
179 static const char *sd_mode_name(enum SDCardModes mode
)
181 static const char *mode_name
[] = {
182 [sd_inactive
] = "inactive",
183 [sd_card_identification_mode
] = "identification",
184 [sd_data_transfer_mode
] = "transfer",
186 assert(mode
< ARRAY_SIZE(mode_name
));
187 return mode_name
[mode
];
190 static const char *sd_state_name(enum SDCardStates state
)
192 static const char *state_name
[] = {
193 [sd_idle_state
] = "idle",
194 [sd_ready_state
] = "ready",
195 [sd_identification_state
] = "identification",
196 [sd_standby_state
] = "standby",
197 [sd_transfer_state
] = "transfer",
198 [sd_sendingdata_state
] = "sendingdata",
199 [sd_receivingdata_state
] = "receivingdata",
200 [sd_programming_state
] = "programming",
201 [sd_disconnect_state
] = "disconnect",
203 if (state
== sd_inactive_state
) {
206 assert(state
< ARRAY_SIZE(state_name
));
207 return state_name
[state
];
210 static const char *sd_response_name(sd_rsp_type_t rsp
)
212 static const char *response_name
[] = {
213 [sd_r0
] = "RESP#0 (no response)",
214 [sd_r1
] = "RESP#1 (normal cmd)",
215 [sd_r2_i
] = "RESP#2 (CID reg)",
216 [sd_r2_s
] = "RESP#2 (CSD reg)",
217 [sd_r3
] = "RESP#3 (OCR reg)",
218 [sd_r6
] = "RESP#6 (RCA)",
219 [sd_r7
] = "RESP#7 (operating voltage)",
221 if (rsp
== sd_illegal
) {
222 return "ILLEGAL RESP";
227 assert(rsp
< ARRAY_SIZE(response_name
));
228 return response_name
[rsp
];
231 static uint8_t sd_get_dat_lines(SDState
*sd
)
233 return sd
->enable
? sd
->dat_lines
: 0;
236 static bool sd_get_cmd_line(SDState
*sd
)
238 return sd
->enable
? sd
->cmd_line
: false;
241 static void sd_set_voltage(SDState
*sd
, uint16_t millivolts
)
243 trace_sdcard_set_voltage(millivolts
);
245 switch (millivolts
) {
246 case 3001 ... 3600: /* SD_VOLTAGE_3_3V */
247 case 2001 ... 3000: /* SD_VOLTAGE_3_0V */
250 qemu_log_mask(LOG_GUEST_ERROR
, "SD card voltage not supported: %.3fV",
251 millivolts
/ 1000.f
);
255 static void sd_set_mode(SDState
*sd
)
258 case sd_inactive_state
:
259 sd
->mode
= sd_inactive
;
264 case sd_identification_state
:
265 sd
->mode
= sd_card_identification_mode
;
268 case sd_standby_state
:
269 case sd_transfer_state
:
270 case sd_sendingdata_state
:
271 case sd_receivingdata_state
:
272 case sd_programming_state
:
273 case sd_disconnect_state
:
274 sd
->mode
= sd_data_transfer_mode
;
279 static const sd_cmd_type_t sd_cmd_type
[SDMMC_CMD_MAX
] = {
280 sd_bc
, sd_none
, sd_bcr
, sd_bcr
, sd_none
, sd_none
, sd_none
, sd_ac
,
281 sd_bcr
, sd_ac
, sd_ac
, sd_adtc
, sd_ac
, sd_ac
, sd_none
, sd_ac
,
283 sd_ac
, sd_adtc
, sd_adtc
, sd_none
, sd_none
, sd_none
, sd_none
, sd_none
,
284 sd_adtc
, sd_adtc
, sd_adtc
, sd_adtc
, sd_ac
, sd_ac
, sd_adtc
, sd_none
,
286 sd_ac
, sd_ac
, sd_none
, sd_none
, sd_none
, sd_none
, sd_ac
, sd_none
,
287 sd_none
, sd_none
, sd_bc
, sd_none
, sd_none
, sd_none
, sd_none
, sd_none
,
289 sd_none
, sd_none
, sd_none
, sd_none
, sd_none
, sd_none
, sd_none
, sd_ac
,
290 sd_adtc
, sd_none
, sd_none
, sd_none
, sd_none
, sd_none
, sd_none
, sd_none
,
293 static const int sd_cmd_class
[SDMMC_CMD_MAX
] = {
294 0, 0, 0, 0, 0, 9, 10, 0, 0, 0, 0, 1, 0, 0, 0, 0,
295 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 6, 6, 6, 6,
296 5, 5, 10, 10, 10, 10, 5, 9, 9, 9, 7, 7, 7, 7, 7, 7,
297 7, 7, 10, 7, 9, 9, 9, 8, 8, 10, 8, 8, 8, 8, 8, 8,
300 static uint8_t sd_crc7(const void *message
, size_t width
)
303 uint8_t shift_reg
= 0x00;
304 const uint8_t *msg
= (const uint8_t *)message
;
306 for (i
= 0; i
< width
; i
++, msg
++)
307 for (bit
= 7; bit
>= 0; bit
--) {
309 if ((shift_reg
>> 7) ^ ((*msg
>> bit
) & 1))
316 /* Operation Conditions register */
318 #define OCR_POWER_DELAY_NS 500000 /* 0.5ms */
320 FIELD(OCR
, VDD_VOLTAGE_WINDOW
, 0, 24)
321 FIELD(OCR
, VDD_VOLTAGE_WIN_LO
, 0, 8)
322 FIELD(OCR
, DUAL_VOLTAGE_CARD
, 7, 1)
323 FIELD(OCR
, VDD_VOLTAGE_WIN_HI
, 8, 16)
324 FIELD(OCR
, ACCEPT_SWITCH_1V8
, 24, 1) /* Only UHS-I */
325 FIELD(OCR
, UHS_II_CARD
, 29, 1) /* Only UHS-II */
326 FIELD(OCR
, CARD_CAPACITY
, 30, 1) /* 0:SDSC, 1:SDHC/SDXC */
327 FIELD(OCR
, CARD_POWER_UP
, 31, 1)
329 #define ACMD41_ENQUIRY_MASK 0x00ffffff
330 #define ACMD41_R3_MASK (R_OCR_VDD_VOLTAGE_WIN_HI_MASK \
331 | R_OCR_ACCEPT_SWITCH_1V8_MASK \
332 | R_OCR_UHS_II_CARD_MASK \
333 | R_OCR_CARD_CAPACITY_MASK \
334 | R_OCR_CARD_POWER_UP_MASK)
336 static void sd_ocr_powerup(void *opaque
)
338 SDState
*sd
= opaque
;
340 trace_sdcard_powerup();
341 assert(!FIELD_EX32(sd
->ocr
, OCR
, CARD_POWER_UP
));
343 /* card power-up OK */
344 sd
->ocr
= FIELD_DP32(sd
->ocr
, OCR
, CARD_POWER_UP
, 1);
346 if (sd
->size
> SDSC_MAX_CAPACITY
) {
347 sd
->ocr
= FIELD_DP32(sd
->ocr
, OCR
, CARD_CAPACITY
, 1);
351 static void sd_set_ocr(SDState
*sd
)
353 /* All voltages OK */
354 sd
->ocr
= R_OCR_VDD_VOLTAGE_WIN_HI_MASK
;
358 * We don't need to emulate power up sequence in SPI-mode.
359 * Thus, the card's power up status bit should be set to 1 when reset.
360 * The card's capacity status bit should also be set if SD card size
361 * is larger than 2GB for SDHC support.
367 /* SD Configuration register */
369 static void sd_set_scr(SDState
*sd
)
371 sd
->scr
[0] = 0 << 4; /* SCR structure version 1.0 */
372 if (sd
->spec_version
== SD_PHY_SPECv1_10_VERS
) {
373 sd
->scr
[0] |= 1; /* Spec Version 1.10 */
375 sd
->scr
[0] |= 2; /* Spec Version 2.00 or Version 3.0X */
377 sd
->scr
[1] = (2 << 4) /* SDSC Card (Security Version 1.01) */
378 | 0b0101; /* 1-bit or 4-bit width bus modes */
379 sd
->scr
[2] = 0x00; /* Extended Security is not supported. */
380 if (sd
->spec_version
>= SD_PHY_SPECv3_01_VERS
) {
381 sd
->scr
[2] |= 1 << 7; /* Spec Version 3.0X */
384 /* reserved for manufacturer usage */
391 /* Card IDentification register */
400 static void sd_set_cid(SDState
*sd
)
402 sd
->cid
[0] = MID
; /* Fake card manufacturer ID (MID) */
403 sd
->cid
[1] = OID
[0]; /* OEM/Application ID (OID) */
405 sd
->cid
[3] = PNM
[0]; /* Fake product name (PNM) */
410 sd
->cid
[8] = PRV
; /* Fake product revision (PRV) */
411 stl_be_p(&sd
->cid
[9], 0xdeadbeef); /* Fake serial number (PSN) */
412 sd
->cid
[13] = 0x00 | /* Manufacture date (MDT) */
413 ((MDT_YR
- 2000) / 10);
414 sd
->cid
[14] = ((MDT_YR
% 10) << 4) | MDT_MON
;
415 sd
->cid
[15] = (sd_crc7(sd
->cid
, 15) << 1) | 1;
418 /* Card-Specific Data register */
420 #define HWBLOCK_SHIFT 9 /* 512 bytes */
421 #define SECTOR_SHIFT 5 /* 16 kilobytes */
422 #define WPGROUP_SHIFT 7 /* 2 megs */
423 #define CMULT_SHIFT 9 /* 512 times HWBLOCK_SIZE */
424 #define WPGROUP_SIZE (1 << (HWBLOCK_SHIFT + SECTOR_SHIFT + WPGROUP_SHIFT))
426 static const uint8_t sd_csd_rw_mask
[16] = {
427 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
428 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xfc, 0xfe,
431 static void sd_set_csd(SDState
*sd
, uint64_t size
)
433 int hwblock_shift
= HWBLOCK_SHIFT
;
435 uint32_t sectsize
= (1 << (SECTOR_SHIFT
+ 1)) - 1;
436 uint32_t wpsize
= (1 << (WPGROUP_SHIFT
+ 1)) - 1;
438 /* To indicate 2 GiB card, BLOCK_LEN shall be 1024 bytes */
439 if (size
== SDSC_MAX_CAPACITY
) {
442 csize
= (size
>> (CMULT_SHIFT
+ hwblock_shift
)) - 1;
444 if (size
<= SDSC_MAX_CAPACITY
) { /* Standard Capacity SD */
445 sd
->csd
[0] = 0x00; /* CSD structure */
446 sd
->csd
[1] = 0x26; /* Data read access-time-1 */
447 sd
->csd
[2] = 0x00; /* Data read access-time-2 */
448 sd
->csd
[3] = 0x32; /* Max. data transfer rate: 25 MHz */
449 sd
->csd
[4] = 0x5f; /* Card Command Classes */
450 sd
->csd
[5] = 0x50 | /* Max. read data block length */
452 sd
->csd
[6] = 0xe0 | /* Partial block for read allowed */
453 ((csize
>> 10) & 0x03);
454 sd
->csd
[7] = 0x00 | /* Device size */
455 ((csize
>> 2) & 0xff);
456 sd
->csd
[8] = 0x3f | /* Max. read current */
457 ((csize
<< 6) & 0xc0);
458 sd
->csd
[9] = 0xfc | /* Max. write current */
459 ((CMULT_SHIFT
- 2) >> 1);
460 sd
->csd
[10] = 0x40 | /* Erase sector size */
461 (((CMULT_SHIFT
- 2) << 7) & 0x80) | (sectsize
>> 1);
462 sd
->csd
[11] = 0x00 | /* Write protect group size */
463 ((sectsize
<< 7) & 0x80) | wpsize
;
464 sd
->csd
[12] = 0x90 | /* Write speed factor */
465 (hwblock_shift
>> 2);
466 sd
->csd
[13] = 0x20 | /* Max. write data block length */
467 ((hwblock_shift
<< 6) & 0xc0);
468 sd
->csd
[14] = 0x00; /* File format group */
479 st24_be_p(&sd
->csd
[7], size
);
486 sd
->csd
[15] = (sd_crc7(sd
->csd
, 15) << 1) | 1;
489 /* Relative Card Address register */
491 static void sd_set_rca(SDState
*sd
)
496 static uint16_t sd_req_get_rca(SDState
*s
, SDRequest req
)
498 if (sd_cmd_type
[req
.cmd
] == sd_ac
|| sd_cmd_type
[req
.cmd
] == sd_adtc
) {
499 return req
.arg
>> 16;
504 /* Card Status register */
506 FIELD(CSR
, AKE_SEQ_ERROR
, 3, 1)
507 FIELD(CSR
, APP_CMD
, 5, 1)
508 FIELD(CSR
, FX_EVENT
, 6, 1)
509 FIELD(CSR
, READY_FOR_DATA
, 8, 1)
510 FIELD(CSR
, CURRENT_STATE
, 9, 4)
511 FIELD(CSR
, ERASE_RESET
, 13, 1)
512 FIELD(CSR
, CARD_ECC_DISABLED
, 14, 1)
513 FIELD(CSR
, WP_ERASE_SKIP
, 15, 1)
514 FIELD(CSR
, CSD_OVERWRITE
, 16, 1)
515 FIELD(CSR
, DEFERRED_RESPONSE
, 17, 1)
516 FIELD(CSR
, ERROR
, 19, 1)
517 FIELD(CSR
, CC_ERROR
, 20, 1)
518 FIELD(CSR
, CARD_ECC_FAILED
, 21, 1)
519 FIELD(CSR
, ILLEGAL_COMMAND
, 22, 1)
520 FIELD(CSR
, COM_CRC_ERROR
, 23, 1)
521 FIELD(CSR
, LOCK_UNLOCK_FAILED
, 24, 1)
522 FIELD(CSR
, CARD_IS_LOCKED
, 25, 1)
523 FIELD(CSR
, WP_VIOLATION
, 26, 1)
524 FIELD(CSR
, ERASE_PARAM
, 27, 1)
525 FIELD(CSR
, ERASE_SEQ_ERROR
, 28, 1)
526 FIELD(CSR
, BLOCK_LEN_ERROR
, 29, 1)
527 FIELD(CSR
, ADDRESS_ERROR
, 30, 1)
528 FIELD(CSR
, OUT_OF_RANGE
, 31, 1)
530 /* Card status bits, split by clear condition:
531 * A : According to the card current state
532 * B : Always related to the previous command
533 * C : Cleared by read
535 #define CARD_STATUS_A (R_CSR_READY_FOR_DATA_MASK \
536 | R_CSR_CARD_ECC_DISABLED_MASK \
537 | R_CSR_CARD_IS_LOCKED_MASK)
538 #define CARD_STATUS_B (R_CSR_CURRENT_STATE_MASK \
539 | R_CSR_ILLEGAL_COMMAND_MASK \
540 | R_CSR_COM_CRC_ERROR_MASK)
541 #define CARD_STATUS_C (R_CSR_AKE_SEQ_ERROR_MASK \
542 | R_CSR_APP_CMD_MASK \
543 | R_CSR_ERASE_RESET_MASK \
544 | R_CSR_WP_ERASE_SKIP_MASK \
545 | R_CSR_CSD_OVERWRITE_MASK \
547 | R_CSR_CC_ERROR_MASK \
548 | R_CSR_CARD_ECC_FAILED_MASK \
549 | R_CSR_LOCK_UNLOCK_FAILED_MASK \
550 | R_CSR_WP_VIOLATION_MASK \
551 | R_CSR_ERASE_PARAM_MASK \
552 | R_CSR_ERASE_SEQ_ERROR_MASK \
553 | R_CSR_BLOCK_LEN_ERROR_MASK \
554 | R_CSR_ADDRESS_ERROR_MASK \
555 | R_CSR_OUT_OF_RANGE_MASK)
557 static void sd_set_cardstatus(SDState
*sd
)
559 sd
->card_status
= READY_FOR_DATA
;
562 static void sd_set_sdstatus(SDState
*sd
)
564 memset(sd
->sd_status
, 0, 64);
567 static const uint8_t sd_tuning_block_pattern4
[64] = {
569 * See: Physical Layer Simplified Specification Version 3.01,
572 0xff, 0x0f, 0xff, 0x00, 0x0f, 0xfc, 0xc3, 0xcc,
573 0xc3, 0x3c, 0xcc, 0xff, 0xfe, 0xff, 0xfe, 0xef,
574 0xff, 0xdf, 0xff, 0xdd, 0xff, 0xfb, 0xff, 0xfb,
575 0xbf, 0xff, 0x7f, 0xff, 0x77, 0xf7, 0xbd, 0xef,
576 0xff, 0xf0, 0xff, 0xf0, 0x0f, 0xfc, 0xcc, 0x3c,
577 0xcc, 0x33, 0xcc, 0xcf, 0xff, 0xef, 0xff, 0xee,
578 0xff, 0xfd, 0xff, 0xfd, 0xdf, 0xff, 0xbf, 0xff,
579 0xbb, 0xff, 0xf7, 0xff, 0xf7, 0x7f, 0x7b, 0xde
582 static int sd_req_crc_validate(SDRequest
*req
)
585 buffer
[0] = 0x40 | req
->cmd
;
586 stl_be_p(&buffer
[1], req
->arg
);
588 return sd_crc7(buffer
, 5) != req
->crc
; /* TODO */
591 static void sd_response_r1_make(SDState
*sd
, uint8_t *response
)
593 stl_be_p(response
, sd
->card_status
);
595 /* Clear the "clear on read" status bits */
596 sd
->card_status
&= ~CARD_STATUS_C
;
599 static void sd_response_r3_make(SDState
*sd
, uint8_t *response
)
601 stl_be_p(response
, sd
->ocr
& ACMD41_R3_MASK
);
604 static void sd_response_r6_make(SDState
*sd
, uint8_t *response
)
608 status
= ((sd
->card_status
>> 8) & 0xc000) |
609 ((sd
->card_status
>> 6) & 0x2000) |
610 (sd
->card_status
& 0x1fff);
611 sd
->card_status
&= ~(CARD_STATUS_C
& 0xc81fff);
612 stw_be_p(response
+ 0, sd
->rca
);
613 stw_be_p(response
+ 2, status
);
616 static void sd_response_r7_make(SDState
*sd
, uint8_t *response
)
618 stl_be_p(response
, sd
->vhs
);
621 static uint32_t sd_blk_len(SDState
*sd
)
623 if (FIELD_EX32(sd
->ocr
, OCR
, CARD_CAPACITY
)) {
624 return 1 << HWBLOCK_SHIFT
;
629 static uint64_t sd_req_get_address(SDState
*sd
, SDRequest req
)
633 if (FIELD_EX32(sd
->ocr
, OCR
, CARD_CAPACITY
)) {
634 addr
= (uint64_t) req
.arg
<< HWBLOCK_SHIFT
;
638 trace_sdcard_req_addr(req
.arg
, addr
);
642 static inline uint64_t sd_addr_to_wpnum(uint64_t addr
)
644 return addr
>> (HWBLOCK_SHIFT
+ SECTOR_SHIFT
+ WPGROUP_SHIFT
);
647 static void sd_reset(DeviceState
*dev
)
649 SDState
*sd
= SD_CARD(dev
);
653 trace_sdcard_reset();
655 blk_get_geometry(sd
->blk
, §
);
659 size
= sect
<< HWBLOCK_SHIFT
;
661 sect
= sd_addr_to_wpnum(size
) + 1;
663 sd
->state
= sd_idle_state
;
671 sd_set_csd(sd
, size
);
672 sd_set_cardstatus(sd
);
675 g_free(sd
->wp_group_bmap
);
676 sd
->wp_switch
= sd
->blk
? !blk_is_writable(sd
->blk
) : false;
677 sd
->wp_group_bits
= sect
;
678 sd
->wp_group_bmap
= bitmap_new(sd
->wp_group_bits
);
679 memset(sd
->function_group
, 0, sizeof(sd
->function_group
));
680 sd
->erase_start
= INVALID_ADDRESS
;
681 sd
->erase_end
= INVALID_ADDRESS
;
684 sd
->expecting_acmd
= false;
687 sd
->multi_blk_cnt
= 0;
690 static bool sd_get_inserted(SDState
*sd
)
692 return sd
->blk
&& blk_is_inserted(sd
->blk
);
695 static bool sd_get_readonly(SDState
*sd
)
697 return sd
->wp_switch
;
700 static void sd_cardchange(void *opaque
, bool load
, Error
**errp
)
702 SDState
*sd
= opaque
;
703 DeviceState
*dev
= DEVICE(sd
);
705 bool inserted
= sd_get_inserted(sd
);
706 bool readonly
= sd_get_readonly(sd
);
709 trace_sdcard_inserted(readonly
);
712 trace_sdcard_ejected();
715 if (sd
->me_no_qdev_me_kill_mammoth_with_rocks
) {
716 qemu_set_irq(sd
->inserted_cb
, inserted
);
718 qemu_set_irq(sd
->readonly_cb
, readonly
);
721 sdbus
= SD_BUS(qdev_get_parent_bus(dev
));
722 sdbus_set_inserted(sdbus
, inserted
);
724 sdbus_set_readonly(sdbus
, readonly
);
729 static const BlockDevOps sd_block_ops
= {
730 .change_media_cb
= sd_cardchange
,
733 static bool sd_ocr_vmstate_needed(void *opaque
)
735 SDState
*sd
= opaque
;
737 /* Include the OCR state (and timer) if it is not yet powered up */
738 return !FIELD_EX32(sd
->ocr
, OCR
, CARD_POWER_UP
);
741 static const VMStateDescription sd_ocr_vmstate
= {
742 .name
= "sd-card/ocr-state",
744 .minimum_version_id
= 1,
745 .needed
= sd_ocr_vmstate_needed
,
746 .fields
= (const VMStateField
[]) {
747 VMSTATE_UINT32(ocr
, SDState
),
748 VMSTATE_TIMER_PTR(ocr_power_timer
, SDState
),
749 VMSTATE_END_OF_LIST()
753 static int sd_vmstate_pre_load(void *opaque
)
755 SDState
*sd
= opaque
;
757 /* If the OCR state is not included (prior versions, or not
758 * needed), then the OCR must be set as powered up. If the OCR state
759 * is included, this will be replaced by the state restore.
766 static const VMStateDescription sd_vmstate
= {
769 .minimum_version_id
= 2,
770 .pre_load
= sd_vmstate_pre_load
,
771 .fields
= (const VMStateField
[]) {
772 VMSTATE_UINT32(mode
, SDState
),
773 VMSTATE_INT32(state
, SDState
),
774 VMSTATE_UINT8_ARRAY(cid
, SDState
, 16),
775 VMSTATE_UINT8_ARRAY(csd
, SDState
, 16),
776 VMSTATE_UINT16(rca
, SDState
),
777 VMSTATE_UINT32(card_status
, SDState
),
778 VMSTATE_PARTIAL_BUFFER(sd_status
, SDState
, 1),
779 VMSTATE_UINT32(vhs
, SDState
),
780 VMSTATE_BITMAP(wp_group_bmap
, SDState
, 0, wp_group_bits
),
781 VMSTATE_UINT32(blk_len
, SDState
),
782 VMSTATE_UINT32(multi_blk_cnt
, SDState
),
783 VMSTATE_UINT32(erase_start
, SDState
),
784 VMSTATE_UINT32(erase_end
, SDState
),
785 VMSTATE_UINT8_ARRAY(pwd
, SDState
, 16),
786 VMSTATE_UINT32(pwd_len
, SDState
),
787 VMSTATE_UINT8_ARRAY(function_group
, SDState
, 6),
788 VMSTATE_UINT8(current_cmd
, SDState
),
789 VMSTATE_BOOL(expecting_acmd
, SDState
),
790 VMSTATE_UINT32(blk_written
, SDState
),
791 VMSTATE_UINT64(data_start
, SDState
),
792 VMSTATE_UINT32(data_offset
, SDState
),
793 VMSTATE_UINT8_ARRAY(data
, SDState
, 512),
794 VMSTATE_UNUSED_V(1, 512),
795 VMSTATE_BOOL(enable
, SDState
),
796 VMSTATE_END_OF_LIST()
798 .subsections
= (const VMStateDescription
* const []) {
804 /* Legacy initialization function for use by non-qdevified callers */
805 SDState
*sd_init(BlockBackend
*blk
, bool is_spi
)
812 obj
= object_new(is_spi
? TYPE_SD_CARD_SPI
: TYPE_SD_CARD
);
814 if (!qdev_prop_set_drive_err(dev
, "drive", blk
, &err
)) {
815 error_reportf_err(err
, "sd_init failed: ");
820 * Realizing the device properly would put it into the QOM
821 * composition tree even though it is not plugged into an
822 * appropriate bus. That's a no-no. Hide the device from
823 * QOM/qdev, and call its qdev realize callback directly.
826 object_unparent(obj
);
827 sd_realize(dev
, &err
);
829 error_reportf_err(err
, "sd_init failed: ");
834 sd
->me_no_qdev_me_kill_mammoth_with_rocks
= true;
838 void sd_set_cb(SDState
*sd
, qemu_irq readonly
, qemu_irq insert
)
840 sd
->readonly_cb
= readonly
;
841 sd
->inserted_cb
= insert
;
842 qemu_set_irq(readonly
, sd
->blk
? !blk_is_writable(sd
->blk
) : 0);
843 qemu_set_irq(insert
, sd
->blk
? blk_is_inserted(sd
->blk
) : 0);
846 static void sd_blk_read(SDState
*sd
, uint64_t addr
, uint32_t len
)
848 trace_sdcard_read_block(addr
, len
);
849 if (!sd
->blk
|| blk_pread(sd
->blk
, addr
, len
, sd
->data
, 0) < 0) {
850 fprintf(stderr
, "sd_blk_read: read error on host side\n");
854 static void sd_blk_write(SDState
*sd
, uint64_t addr
, uint32_t len
)
856 trace_sdcard_write_block(addr
, len
);
857 if (!sd
->blk
|| blk_pwrite(sd
->blk
, addr
, len
, sd
->data
, 0) < 0) {
858 fprintf(stderr
, "sd_blk_write: write error on host side\n");
862 #define APP_READ_BLOCK(a, len) memset(sd->data, 0xec, len)
863 #define APP_WRITE_BLOCK(a, len)
865 static void sd_erase(SDState
*sd
)
867 uint64_t erase_start
= sd
->erase_start
;
868 uint64_t erase_end
= sd
->erase_end
;
872 int erase_len
= 1 << HWBLOCK_SHIFT
;
874 trace_sdcard_erase(sd
->erase_start
, sd
->erase_end
);
875 if (sd
->erase_start
== INVALID_ADDRESS
876 || sd
->erase_end
== INVALID_ADDRESS
) {
877 sd
->card_status
|= ERASE_SEQ_ERROR
;
878 sd
->erase_start
= INVALID_ADDRESS
;
879 sd
->erase_end
= INVALID_ADDRESS
;
883 if (FIELD_EX32(sd
->ocr
, OCR
, CARD_CAPACITY
)) {
884 /* High capacity memory card: erase units are 512 byte blocks */
885 erase_start
<<= HWBLOCK_SHIFT
;
886 erase_end
<<= HWBLOCK_SHIFT
;
890 if (erase_start
> sd
->size
|| erase_end
> sd
->size
) {
891 sd
->card_status
|= OUT_OF_RANGE
;
892 sd
->erase_start
= INVALID_ADDRESS
;
893 sd
->erase_end
= INVALID_ADDRESS
;
897 sd
->erase_start
= INVALID_ADDRESS
;
898 sd
->erase_end
= INVALID_ADDRESS
;
901 memset(sd
->data
, 0xff, erase_len
);
902 for (erase_addr
= erase_start
; erase_addr
<= erase_end
;
903 erase_addr
+= erase_len
) {
905 /* Only SDSC cards support write protect groups */
906 wpnum
= sd_addr_to_wpnum(erase_addr
);
907 assert(wpnum
< sd
->wp_group_bits
);
908 if (test_bit(wpnum
, sd
->wp_group_bmap
)) {
909 sd
->card_status
|= WP_ERASE_SKIP
;
913 sd_blk_write(sd
, erase_addr
, erase_len
);
917 static uint32_t sd_wpbits(SDState
*sd
, uint64_t addr
)
922 wpnum
= sd_addr_to_wpnum(addr
);
924 for (i
= 0; i
< 32; i
++, wpnum
++, addr
+= WPGROUP_SIZE
) {
925 if (addr
>= sd
->size
) {
927 * If the addresses of the last groups are outside the valid range,
928 * then the corresponding write protection bits shall be set to 0.
932 assert(wpnum
< sd
->wp_group_bits
);
933 if (test_bit(wpnum
, sd
->wp_group_bmap
)) {
941 static void sd_function_switch(SDState
*sd
, uint32_t arg
)
943 int i
, mode
, new_func
;
944 mode
= !!(arg
& 0x80000000);
946 sd
->data
[0] = 0x00; /* Maximum current consumption */
948 sd
->data
[2] = 0x80; /* Supported group 6 functions */
950 sd
->data
[4] = 0x80; /* Supported group 5 functions */
952 sd
->data
[6] = 0x80; /* Supported group 4 functions */
954 sd
->data
[8] = 0x80; /* Supported group 3 functions */
956 sd
->data
[10] = 0x80; /* Supported group 2 functions */
958 sd
->data
[12] = 0x80; /* Supported group 1 functions */
961 memset(&sd
->data
[14], 0, 3);
962 for (i
= 0; i
< 6; i
++) {
963 new_func
= (arg
>> (i
* 4)) & 0x0f;
964 if (mode
&& new_func
!= 0x0f)
965 sd
->function_group
[i
] = new_func
;
966 sd
->data
[16 - (i
>> 1)] |= new_func
<< ((i
% 2) * 4);
968 memset(&sd
->data
[17], 0, 47);
971 static inline bool sd_wp_addr(SDState
*sd
, uint64_t addr
)
973 return test_bit(sd_addr_to_wpnum(addr
), sd
->wp_group_bmap
);
976 static void sd_lock_command(SDState
*sd
)
978 int erase
, lock
, clr_pwd
, set_pwd
, pwd_len
;
979 erase
= !!(sd
->data
[0] & 0x08);
980 lock
= sd
->data
[0] & 0x04;
981 clr_pwd
= sd
->data
[0] & 0x02;
982 set_pwd
= sd
->data
[0] & 0x01;
985 pwd_len
= sd
->data
[1];
992 trace_sdcard_unlock();
995 if (!(sd
->card_status
& CARD_IS_LOCKED
) || sd
->blk_len
> 1 ||
996 set_pwd
|| clr_pwd
|| lock
|| sd
->wp_switch
||
997 (sd
->csd
[14] & 0x20)) {
998 sd
->card_status
|= LOCK_UNLOCK_FAILED
;
1001 bitmap_zero(sd
->wp_group_bmap
, sd
->wp_group_bits
);
1002 sd
->csd
[14] &= ~0x10;
1003 sd
->card_status
&= ~CARD_IS_LOCKED
;
1005 /* Erasing the entire card here! */
1006 fprintf(stderr
, "SD: Card force-erased by CMD42\n");
1010 if (sd
->blk_len
< 2 + pwd_len
||
1011 pwd_len
<= sd
->pwd_len
||
1012 pwd_len
> sd
->pwd_len
+ 16) {
1013 sd
->card_status
|= LOCK_UNLOCK_FAILED
;
1017 if (sd
->pwd_len
&& memcmp(sd
->pwd
, sd
->data
+ 2, sd
->pwd_len
)) {
1018 sd
->card_status
|= LOCK_UNLOCK_FAILED
;
1022 pwd_len
-= sd
->pwd_len
;
1023 if ((pwd_len
&& !set_pwd
) ||
1024 (clr_pwd
&& (set_pwd
|| lock
)) ||
1025 (lock
&& !sd
->pwd_len
&& !set_pwd
) ||
1026 (!set_pwd
&& !clr_pwd
&&
1027 (((sd
->card_status
& CARD_IS_LOCKED
) && lock
) ||
1028 (!(sd
->card_status
& CARD_IS_LOCKED
) && !lock
)))) {
1029 sd
->card_status
|= LOCK_UNLOCK_FAILED
;
1034 memcpy(sd
->pwd
, sd
->data
+ 2 + sd
->pwd_len
, pwd_len
);
1035 sd
->pwd_len
= pwd_len
;
1043 sd
->card_status
|= CARD_IS_LOCKED
;
1045 sd
->card_status
&= ~CARD_IS_LOCKED
;
1048 static bool address_in_range(SDState
*sd
, const char *desc
,
1049 uint64_t addr
, uint32_t length
)
1051 if (addr
+ length
> sd
->size
) {
1052 qemu_log_mask(LOG_GUEST_ERROR
,
1053 "%s offset %"PRIu64
" > card %"PRIu64
" [%%%u]\n",
1054 desc
, addr
, sd
->size
, length
);
1055 sd
->card_status
|= ADDRESS_ERROR
;
1061 static sd_rsp_type_t
sd_invalid_state_for_cmd(SDState
*sd
, SDRequest req
)
1063 qemu_log_mask(LOG_GUEST_ERROR
, "%s: CMD%i in a wrong state: %s (spec %s)\n",
1064 sd
->proto
->name
, req
.cmd
, sd_state_name(sd
->state
),
1065 sd_version_str(sd
->spec_version
));
1070 static sd_rsp_type_t
sd_invalid_mode_for_cmd(SDState
*sd
, SDRequest req
)
1072 qemu_log_mask(LOG_GUEST_ERROR
, "%s: CMD%i in a wrong mode: %s (spec %s)\n",
1073 sd
->proto
->name
, req
.cmd
, sd_mode_name(sd
->mode
),
1074 sd_version_str(sd
->spec_version
));
1079 static sd_rsp_type_t
sd_cmd_illegal(SDState
*sd
, SDRequest req
)
1081 qemu_log_mask(LOG_GUEST_ERROR
, "%s: Unknown CMD%i for spec %s\n",
1082 sd
->proto
->name
, req
.cmd
,
1083 sd_version_str(sd
->spec_version
));
1088 /* Commands that are recognised but not yet implemented. */
1089 __attribute__((unused
))
1090 static sd_rsp_type_t
sd_cmd_unimplemented(SDState
*sd
, SDRequest req
)
1092 qemu_log_mask(LOG_UNIMP
, "%s: CMD%i not implemented\n",
1093 sd
->proto
->name
, req
.cmd
);
1098 /* Configure fields for following sd_generic_read_byte() calls */
1099 static sd_rsp_type_t
sd_cmd_to_sendingdata(SDState
*sd
, SDRequest req
,
1101 const void *data
, size_t size
)
1103 if (sd
->state
!= sd_transfer_state
) {
1104 sd_invalid_state_for_cmd(sd
, req
);
1107 sd
->state
= sd_sendingdata_state
;
1108 sd
->data_start
= start
;
1109 sd
->data_offset
= 0;
1111 assert(size
> 0 && size
<= sizeof(sd
->data
));
1112 memcpy(sd
->data
, data
, size
);
1115 sd
->data_size
= size
;
1121 static sd_rsp_type_t
sd_cmd_GO_IDLE_STATE(SDState
*sd
, SDRequest req
)
1123 sd
->state
= sd_idle_state
;
1124 sd_reset(DEVICE(sd
));
1126 return sd_is_spi(sd
) ? sd_r1
: sd_r0
;
1130 static sd_rsp_type_t
spi_cmd_SEND_OP_COND(SDState
*sd
, SDRequest req
)
1132 sd
->state
= sd_transfer_state
;
1138 static sd_rsp_type_t
sd_cmd_ALL_SEND_CID(SDState
*sd
, SDRequest req
)
1140 switch (sd
->state
) {
1141 case sd_ready_state
:
1142 sd
->state
= sd_identification_state
;
1145 return sd_invalid_state_for_cmd(sd
, req
);
1150 static sd_rsp_type_t
sd_cmd_SEND_RELATIVE_ADDR(SDState
*sd
, SDRequest req
)
1152 switch (sd
->state
) {
1153 case sd_identification_state
:
1154 case sd_standby_state
:
1155 sd
->state
= sd_standby_state
;
1160 return sd_invalid_state_for_cmd(sd
, req
);
1165 static sd_rsp_type_t
sd_cmd_SEND_TUNING_BLOCK(SDState
*sd
, SDRequest req
)
1167 if (sd
->spec_version
< SD_PHY_SPECv3_01_VERS
) {
1168 return sd_cmd_illegal(sd
, req
);
1171 return sd_cmd_to_sendingdata(sd
, req
, 0,
1172 sd_tuning_block_pattern4
,
1173 sizeof(sd_tuning_block_pattern4
));
1177 static sd_rsp_type_t
sd_cmd_SET_BLOCK_COUNT(SDState
*sd
, SDRequest req
)
1179 if (sd
->spec_version
< SD_PHY_SPECv3_01_VERS
) {
1180 return sd_cmd_illegal(sd
, req
);
1183 if (sd
->state
!= sd_transfer_state
) {
1184 return sd_invalid_state_for_cmd(sd
, req
);
1187 sd
->multi_blk_cnt
= req
.arg
;
1188 trace_sdcard_set_block_count(sd
->multi_blk_cnt
);
1193 static sd_rsp_type_t
sd_normal_command(SDState
*sd
, SDRequest req
)
1199 sd
->last_cmd_name
= sd_cmd_name(req
.cmd
);
1200 /* CMD55 precedes an ACMD, so we are not interested in tracing it.
1201 * However there is no ACMD55, so we want to trace this particular case.
1203 if (req
.cmd
!= 55 || sd
->expecting_acmd
) {
1204 trace_sdcard_normal_command(sd
->proto
->name
,
1205 sd
->last_cmd_name
, req
.cmd
,
1206 req
.arg
, sd_state_name(sd
->state
));
1209 /* Not interpreting this as an app command */
1210 sd
->card_status
&= ~APP_CMD
;
1212 /* CMD23 (set block count) must be immediately followed by CMD18 or CMD25
1213 * if not, its effects are cancelled */
1214 if (sd
->multi_blk_cnt
!= 0 && !(req
.cmd
== 18 || req
.cmd
== 25)) {
1215 sd
->multi_blk_cnt
= 0;
1218 if (sd_cmd_class
[req
.cmd
] == 6 && FIELD_EX32(sd
->ocr
, OCR
, CARD_CAPACITY
)) {
1219 /* Only Standard Capacity cards support class 6 commands */
1223 if (sd
->proto
->cmd
[req
.cmd
]) {
1224 return sd
->proto
->cmd
[req
.cmd
](sd
, req
);
1228 /* Basic commands (Class 0 and Class 1) */
1229 case 4: /* CMD4: SEND_DSR */
1230 switch (sd
->state
) {
1231 case sd_standby_state
:
1239 case 6: /* CMD6: SWITCH_FUNCTION */
1240 if (sd
->mode
!= sd_data_transfer_mode
) {
1241 return sd_invalid_mode_for_cmd(sd
, req
);
1243 if (sd
->state
!= sd_transfer_state
) {
1244 return sd_invalid_state_for_cmd(sd
, req
);
1247 sd_function_switch(sd
, req
.arg
);
1248 return sd_cmd_to_sendingdata(sd
, req
, 0, NULL
, 64);
1250 case 7: /* CMD7: SELECT/DESELECT_CARD */
1251 rca
= sd_req_get_rca(sd
, req
);
1252 switch (sd
->state
) {
1253 case sd_standby_state
:
1257 sd
->state
= sd_transfer_state
;
1260 case sd_transfer_state
:
1261 case sd_sendingdata_state
:
1265 sd
->state
= sd_standby_state
;
1268 case sd_disconnect_state
:
1272 sd
->state
= sd_programming_state
;
1275 case sd_programming_state
:
1279 sd
->state
= sd_disconnect_state
;
1287 case 8: /* CMD8: SEND_IF_COND */
1288 if (sd
->spec_version
< SD_PHY_SPECv2_00_VERS
) {
1291 if (sd
->state
!= sd_idle_state
) {
1296 /* No response if not exactly one VHS bit is set. */
1297 if (!(req
.arg
>> 8) || (req
.arg
>> (ctz32(req
.arg
& ~0xff) + 1))) {
1298 return sd_is_spi(sd
) ? sd_r7
: sd_r0
;
1305 case 9: /* CMD9: SEND_CSD */
1306 rca
= sd_req_get_rca(sd
, req
);
1307 switch (sd
->state
) {
1308 case sd_standby_state
:
1314 case sd_transfer_state
:
1315 if (!sd_is_spi(sd
)) {
1318 return sd_cmd_to_sendingdata(sd
, req
, sd_req_get_address(sd
, req
),
1326 case 10: /* CMD10: SEND_CID */
1327 rca
= sd_req_get_rca(sd
, req
);
1328 switch (sd
->state
) {
1329 case sd_standby_state
:
1335 case sd_transfer_state
:
1336 if (!sd_is_spi(sd
)) {
1339 return sd_cmd_to_sendingdata(sd
, req
, sd_req_get_address(sd
, req
),
1347 case 12: /* CMD12: STOP_TRANSMISSION */
1348 switch (sd
->state
) {
1349 case sd_sendingdata_state
:
1350 sd
->state
= sd_transfer_state
;
1353 case sd_receivingdata_state
:
1354 sd
->state
= sd_programming_state
;
1355 /* Bzzzzzzztt .... Operation complete. */
1356 sd
->state
= sd_transfer_state
;
1364 case 13: /* CMD13: SEND_STATUS */
1365 rca
= sd_req_get_rca(sd
, req
);
1366 if (sd
->mode
!= sd_data_transfer_mode
) {
1367 return sd_invalid_mode_for_cmd(sd
, req
);
1369 if (!sd_is_spi(sd
) && sd
->rca
!= rca
) {
1375 case 15: /* CMD15: GO_INACTIVE_STATE */
1376 if (sd
->mode
!= sd_data_transfer_mode
) {
1377 return sd_invalid_mode_for_cmd(sd
, req
);
1379 rca
= sd_req_get_rca(sd
, req
);
1380 if (sd
->rca
== rca
) {
1381 sd
->state
= sd_inactive_state
;
1385 /* Block read commands (Class 2) */
1386 case 16: /* CMD16: SET_BLOCKLEN */
1387 switch (sd
->state
) {
1388 case sd_transfer_state
:
1389 if (req
.arg
> (1 << HWBLOCK_SHIFT
)) {
1390 sd
->card_status
|= BLOCK_LEN_ERROR
;
1392 trace_sdcard_set_blocklen(req
.arg
);
1393 sd
->blk_len
= req
.arg
;
1403 case 17: /* CMD17: READ_SINGLE_BLOCK */
1404 addr
= sd_req_get_address(sd
, req
);
1405 switch (sd
->state
) {
1406 case sd_transfer_state
:
1408 if (!address_in_range(sd
, "READ_SINGLE_BLOCK", addr
, sd
->blk_len
)) {
1411 sd_blk_read(sd
, addr
, sd
->blk_len
);
1412 return sd_cmd_to_sendingdata(sd
, req
, addr
, NULL
, sd
->blk_len
);
1419 case 18: /* CMD18: READ_MULTIPLE_BLOCK */
1420 addr
= sd_req_get_address(sd
, req
);
1421 switch (sd
->state
) {
1422 case sd_transfer_state
:
1424 if (!address_in_range(sd
, "READ_BLOCK", addr
, sd
->blk_len
)) {
1428 sd
->state
= sd_sendingdata_state
;
1429 sd
->data_start
= addr
;
1430 sd
->data_offset
= 0;
1438 /* Block write commands (Class 4) */
1439 case 24: /* CMD24: WRITE_SINGLE_BLOCK */
1440 case 25: /* CMD25: WRITE_MULTIPLE_BLOCK */
1441 addr
= sd_req_get_address(sd
, req
);
1442 switch (sd
->state
) {
1443 case sd_transfer_state
:
1445 if (!address_in_range(sd
, "WRITE_BLOCK", addr
, sd
->blk_len
)) {
1449 sd
->state
= sd_receivingdata_state
;
1450 sd
->data_start
= addr
;
1451 sd
->data_offset
= 0;
1452 sd
->blk_written
= 0;
1454 if (sd
->size
<= SDSC_MAX_CAPACITY
) {
1455 if (sd_wp_addr(sd
, sd
->data_start
)) {
1456 sd
->card_status
|= WP_VIOLATION
;
1459 if (sd
->csd
[14] & 0x30) {
1460 sd
->card_status
|= WP_VIOLATION
;
1469 case 26: /* CMD26: PROGRAM_CID */
1470 switch (sd
->state
) {
1471 case sd_transfer_state
:
1472 sd
->state
= sd_receivingdata_state
;
1474 sd
->data_offset
= 0;
1482 case 27: /* CMD27: PROGRAM_CSD */
1483 switch (sd
->state
) {
1484 case sd_transfer_state
:
1485 sd
->state
= sd_receivingdata_state
;
1487 sd
->data_offset
= 0;
1495 /* Write protection (Class 6) */
1496 case 28: /* CMD28: SET_WRITE_PROT */
1497 if (sd
->size
> SDSC_MAX_CAPACITY
) {
1500 addr
= sd_req_get_address(sd
, req
);
1501 switch (sd
->state
) {
1502 case sd_transfer_state
:
1503 if (!address_in_range(sd
, "SET_WRITE_PROT", addr
, 1)) {
1507 sd
->state
= sd_programming_state
;
1508 set_bit(sd_addr_to_wpnum(addr
), sd
->wp_group_bmap
);
1509 /* Bzzzzzzztt .... Operation complete. */
1510 sd
->state
= sd_transfer_state
;
1518 case 29: /* CMD29: CLR_WRITE_PROT */
1519 if (sd
->size
> SDSC_MAX_CAPACITY
) {
1522 addr
= sd_req_get_address(sd
, req
);
1523 switch (sd
->state
) {
1524 case sd_transfer_state
:
1525 if (!address_in_range(sd
, "CLR_WRITE_PROT", addr
, 1)) {
1529 sd
->state
= sd_programming_state
;
1530 clear_bit(sd_addr_to_wpnum(addr
), sd
->wp_group_bmap
);
1531 /* Bzzzzzzztt .... Operation complete. */
1532 sd
->state
= sd_transfer_state
;
1540 case 30: /* CMD30: SEND_WRITE_PROT */
1541 if (sd
->size
> SDSC_MAX_CAPACITY
) {
1544 addr
= sd_req_get_address(sd
, req
);
1545 switch (sd
->state
) {
1546 case sd_transfer_state
:
1547 if (!address_in_range(sd
, "SEND_WRITE_PROT",
1548 req
.arg
, sd
->blk_len
)) {
1551 data
= sd_wpbits(sd
, req
.arg
);
1552 return sd_cmd_to_sendingdata(sd
, req
, addr
, &data
, sizeof(data
));
1559 /* Erase commands (Class 5) */
1560 case 32: /* CMD32: ERASE_WR_BLK_START */
1561 switch (sd
->state
) {
1562 case sd_transfer_state
:
1563 sd
->erase_start
= req
.arg
;
1571 case 33: /* CMD33: ERASE_WR_BLK_END */
1572 switch (sd
->state
) {
1573 case sd_transfer_state
:
1574 sd
->erase_end
= req
.arg
;
1582 case 38: /* CMD38: ERASE */
1583 switch (sd
->state
) {
1584 case sd_transfer_state
:
1585 if (sd
->csd
[14] & 0x30) {
1586 sd
->card_status
|= WP_VIOLATION
;
1590 sd
->state
= sd_programming_state
;
1592 /* Bzzzzzzztt .... Operation complete. */
1593 sd
->state
= sd_transfer_state
;
1601 /* Lock card commands (Class 7) */
1602 case 42: /* CMD42: LOCK_UNLOCK */
1603 switch (sd
->state
) {
1604 case sd_transfer_state
:
1605 sd
->state
= sd_receivingdata_state
;
1607 sd
->data_offset
= 0;
1615 /* Application specific commands (Class 8) */
1616 case 55: /* CMD55: APP_CMD */
1617 rca
= sd_req_get_rca(sd
, req
);
1618 switch (sd
->state
) {
1619 case sd_ready_state
:
1620 case sd_identification_state
:
1624 qemu_log_mask(LOG_GUEST_ERROR
,
1625 "SD: illegal RCA 0x%04x for APP_CMD\n", req
.cmd
);
1630 if (!sd_is_spi(sd
)) {
1631 if (sd
->rca
!= rca
) {
1635 sd
->expecting_acmd
= true;
1636 sd
->card_status
|= APP_CMD
;
1639 case 56: /* CMD56: GEN_CMD */
1640 switch (sd
->state
) {
1641 case sd_transfer_state
:
1642 sd
->data_offset
= 0;
1644 sd
->state
= sd_sendingdata_state
;
1646 sd
->state
= sd_receivingdata_state
;
1654 case 58: /* CMD58: READ_OCR (SPI) */
1657 case 59: /* CMD59: CRC_ON_OFF (SPI) */
1661 qemu_log_mask(LOG_GUEST_ERROR
, "SD: Unknown CMD%i\n", req
.cmd
);
1665 return sd_invalid_state_for_cmd(sd
, req
);
1668 static sd_rsp_type_t
sd_app_command(SDState
*sd
,
1671 sd
->last_cmd_name
= sd_acmd_name(req
.cmd
);
1672 trace_sdcard_app_command(sd
->proto
->name
, sd
->last_cmd_name
,
1673 req
.cmd
, req
.arg
, sd_state_name(sd
->state
));
1674 sd
->card_status
|= APP_CMD
;
1676 if (sd
->proto
->acmd
[req
.cmd
]) {
1677 return sd
->proto
->acmd
[req
.cmd
](sd
, req
);
1681 case 6: /* ACMD6: SET_BUS_WIDTH */
1682 switch (sd
->state
) {
1683 case sd_transfer_state
:
1684 sd
->sd_status
[0] &= 0x3f;
1685 sd
->sd_status
[0] |= (req
.arg
& 0x03) << 6;
1693 case 13: /* ACMD13: SD_STATUS */
1694 switch (sd
->state
) {
1695 case sd_transfer_state
:
1696 return sd_cmd_to_sendingdata(sd
, req
, 0,
1698 sizeof(sd
->sd_status
));
1705 case 22: /* ACMD22: SEND_NUM_WR_BLOCKS */
1706 switch (sd
->state
) {
1707 case sd_transfer_state
:
1708 stl_be_p(sd
->data
, sd
->blk_written
);
1709 sd
->state
= sd_sendingdata_state
;
1711 sd
->data_offset
= 0;
1719 case 23: /* ACMD23: SET_WR_BLK_ERASE_COUNT */
1720 switch (sd
->state
) {
1721 case sd_transfer_state
:
1729 case 41: /* ACMD41: SD_APP_OP_COND */
1730 if (sd
->state
!= sd_idle_state
) {
1733 /* If it's the first ACMD41 since reset, we need to decide
1734 * whether to power up. If this is not an enquiry ACMD41,
1735 * we immediately report power on and proceed below to the
1736 * ready state, but if it is, we set a timer to model a
1737 * delay for power up. This works around a bug in EDK2
1738 * UEFI, which sends an initial enquiry ACMD41, but
1739 * assumes that the card is in ready state as soon as it
1740 * sees the power up bit set. */
1741 if (!FIELD_EX32(sd
->ocr
, OCR
, CARD_POWER_UP
)) {
1742 if ((req
.arg
& ACMD41_ENQUIRY_MASK
) != 0) {
1743 timer_del(sd
->ocr_power_timer
);
1746 trace_sdcard_inquiry_cmd41();
1747 if (!timer_pending(sd
->ocr_power_timer
)) {
1748 timer_mod_ns(sd
->ocr_power_timer
,
1749 (qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
)
1750 + OCR_POWER_DELAY_NS
));
1755 if (FIELD_EX32(sd
->ocr
& req
.arg
, OCR
, VDD_VOLTAGE_WINDOW
)) {
1756 /* We accept any voltage. 10000 V is nothing.
1758 * Once we're powered up, we advance straight to ready state
1759 * unless it's an enquiry ACMD41 (bits 23:0 == 0).
1761 sd
->state
= sd_ready_state
;
1766 case 42: /* ACMD42: SET_CLR_CARD_DETECT */
1767 switch (sd
->state
) {
1768 case sd_transfer_state
:
1769 /* Bringing in the 50KOhm pull-up resistor... Done. */
1777 case 51: /* ACMD51: SEND_SCR */
1778 switch (sd
->state
) {
1779 case sd_transfer_state
:
1780 sd
->state
= sd_sendingdata_state
;
1782 sd
->data_offset
= 0;
1790 case 18: /* Reserved for SD security applications */
1795 /* Refer to the "SD Specifications Part3 Security Specification" for
1796 * information about the SD Security Features.
1798 qemu_log_mask(LOG_UNIMP
, "SD: CMD%i Security not implemented\n",
1803 /* Fall back to standard commands. */
1804 return sd_normal_command(sd
, req
);
1807 qemu_log_mask(LOG_GUEST_ERROR
, "SD: ACMD%i in a wrong state\n", req
.cmd
);
1811 static bool cmd_valid_while_locked(SDState
*sd
, unsigned cmd
)
1813 /* Valid commands in locked state:
1815 * lock card class (7)
1817 * implicitly, the ACMD prefix CMD55
1819 * Anything else provokes an "illegal command" response.
1821 if (sd
->expecting_acmd
) {
1822 return cmd
== 41 || cmd
== 42;
1824 if (cmd
== 16 || cmd
== 55) {
1827 return sd_cmd_class
[cmd
] == 0 || sd_cmd_class
[cmd
] == 7;
1830 int sd_do_command(SDState
*sd
, SDRequest
*req
,
1831 uint8_t *response
) {
1833 sd_rsp_type_t rtype
;
1836 if (!sd
->blk
|| !blk_is_inserted(sd
->blk
) || !sd
->enable
) {
1840 if (sd
->state
== sd_inactive_state
) {
1845 if (sd_req_crc_validate(req
)) {
1846 sd
->card_status
|= COM_CRC_ERROR
;
1851 if (req
->cmd
>= SDMMC_CMD_MAX
) {
1852 qemu_log_mask(LOG_GUEST_ERROR
, "SD: incorrect command 0x%02x\n",
1857 if (sd
->card_status
& CARD_IS_LOCKED
) {
1858 if (!cmd_valid_while_locked(sd
, req
->cmd
)) {
1859 sd
->card_status
|= ILLEGAL_COMMAND
;
1860 sd
->expecting_acmd
= false;
1861 qemu_log_mask(LOG_GUEST_ERROR
, "SD: Card is locked\n");
1867 last_state
= sd
->state
;
1870 if (sd
->expecting_acmd
) {
1871 sd
->expecting_acmd
= false;
1872 rtype
= sd_app_command(sd
, *req
);
1874 rtype
= sd_normal_command(sd
, *req
);
1877 if (rtype
== sd_illegal
) {
1878 sd
->card_status
|= ILLEGAL_COMMAND
;
1880 /* Valid command, we can update the 'state before command' bits.
1881 * (Do this now so they appear in r1 responses.)
1883 sd
->current_cmd
= req
->cmd
;
1884 sd
->card_status
= FIELD_DP32(sd
->card_status
, CSR
,
1885 CURRENT_STATE
, last_state
);
1892 sd_response_r1_make(sd
, response
);
1897 memcpy(response
, sd
->cid
, sizeof(sd
->cid
));
1902 memcpy(response
, sd
->csd
, sizeof(sd
->csd
));
1907 sd_response_r3_make(sd
, response
);
1912 sd_response_r6_make(sd
, response
);
1917 sd_response_r7_make(sd
, response
);
1923 * Invalid state transition, reset implementation
1924 * fields to avoid OOB abuse.
1927 sd
->data_offset
= 0;
1933 g_assert_not_reached();
1935 trace_sdcard_response(sd_response_name(rtype
), rsplen
);
1937 if (rtype
!= sd_illegal
) {
1938 /* Clear the "clear on valid command" status bits now we've
1941 sd
->card_status
&= ~CARD_STATUS_B
;
1945 qemu_hexdump(stderr
, "Response", response
, rsplen
);
1951 /* Return true when buffer is consumed. Configured by sd_cmd_to_sendingdata() */
1952 static bool sd_generic_read_byte(SDState
*sd
, uint8_t *value
)
1954 *value
= sd
->data
[sd
->data_offset
];
1956 if (++sd
->data_offset
>= sd
->data_size
) {
1957 sd
->state
= sd_transfer_state
;
1964 void sd_write_byte(SDState
*sd
, uint8_t value
)
1968 if (!sd
->blk
|| !blk_is_inserted(sd
->blk
) || !sd
->enable
)
1971 if (sd
->state
!= sd_receivingdata_state
) {
1972 qemu_log_mask(LOG_GUEST_ERROR
,
1973 "%s: not in Receiving-Data state\n", __func__
);
1977 if (sd
->card_status
& (ADDRESS_ERROR
| WP_VIOLATION
))
1980 trace_sdcard_write_data(sd
->proto
->name
,
1982 sd
->current_cmd
, sd
->data_offset
, value
);
1983 switch (sd
->current_cmd
) {
1984 case 24: /* CMD24: WRITE_SINGLE_BLOCK */
1985 sd
->data
[sd
->data_offset
++] = value
;
1986 if (sd
->data_offset
>= sd
->blk_len
) {
1987 /* TODO: Check CRC before committing */
1988 sd
->state
= sd_programming_state
;
1989 sd_blk_write(sd
, sd
->data_start
, sd
->data_offset
);
1991 sd
->csd
[14] |= 0x40;
1992 /* Bzzzzzzztt .... Operation complete. */
1993 sd
->state
= sd_transfer_state
;
1997 case 25: /* CMD25: WRITE_MULTIPLE_BLOCK */
1998 if (sd
->data_offset
== 0) {
1999 /* Start of the block - let's check the address is valid */
2000 if (!address_in_range(sd
, "WRITE_MULTIPLE_BLOCK",
2001 sd
->data_start
, sd
->blk_len
)) {
2004 if (sd
->size
<= SDSC_MAX_CAPACITY
) {
2005 if (sd_wp_addr(sd
, sd
->data_start
)) {
2006 sd
->card_status
|= WP_VIOLATION
;
2011 sd
->data
[sd
->data_offset
++] = value
;
2012 if (sd
->data_offset
>= sd
->blk_len
) {
2013 /* TODO: Check CRC before committing */
2014 sd
->state
= sd_programming_state
;
2015 sd_blk_write(sd
, sd
->data_start
, sd
->data_offset
);
2017 sd
->data_start
+= sd
->blk_len
;
2018 sd
->data_offset
= 0;
2019 sd
->csd
[14] |= 0x40;
2021 /* Bzzzzzzztt .... Operation complete. */
2022 if (sd
->multi_blk_cnt
!= 0) {
2023 if (--sd
->multi_blk_cnt
== 0) {
2025 sd
->state
= sd_transfer_state
;
2030 sd
->state
= sd_receivingdata_state
;
2034 case 26: /* CMD26: PROGRAM_CID */
2035 sd
->data
[sd
->data_offset
++] = value
;
2036 if (sd
->data_offset
>= sizeof(sd
->cid
)) {
2037 /* TODO: Check CRC before committing */
2038 sd
->state
= sd_programming_state
;
2039 for (i
= 0; i
< sizeof(sd
->cid
); i
++)
2040 if ((sd
->cid
[i
] | 0x00) != sd
->data
[i
])
2041 sd
->card_status
|= CID_CSD_OVERWRITE
;
2043 if (!(sd
->card_status
& CID_CSD_OVERWRITE
))
2044 for (i
= 0; i
< sizeof(sd
->cid
); i
++) {
2046 sd
->cid
[i
] &= sd
->data
[i
];
2048 /* Bzzzzzzztt .... Operation complete. */
2049 sd
->state
= sd_transfer_state
;
2053 case 27: /* CMD27: PROGRAM_CSD */
2054 sd
->data
[sd
->data_offset
++] = value
;
2055 if (sd
->data_offset
>= sizeof(sd
->csd
)) {
2056 /* TODO: Check CRC before committing */
2057 sd
->state
= sd_programming_state
;
2058 for (i
= 0; i
< sizeof(sd
->csd
); i
++)
2059 if ((sd
->csd
[i
] | sd_csd_rw_mask
[i
]) !=
2060 (sd
->data
[i
] | sd_csd_rw_mask
[i
]))
2061 sd
->card_status
|= CID_CSD_OVERWRITE
;
2063 /* Copy flag (OTP) & Permanent write protect */
2064 if (sd
->csd
[14] & ~sd
->data
[14] & 0x60)
2065 sd
->card_status
|= CID_CSD_OVERWRITE
;
2067 if (!(sd
->card_status
& CID_CSD_OVERWRITE
))
2068 for (i
= 0; i
< sizeof(sd
->csd
); i
++) {
2069 sd
->csd
[i
] |= sd_csd_rw_mask
[i
];
2070 sd
->csd
[i
] &= sd
->data
[i
];
2072 /* Bzzzzzzztt .... Operation complete. */
2073 sd
->state
= sd_transfer_state
;
2077 case 42: /* CMD42: LOCK_UNLOCK */
2078 sd
->data
[sd
->data_offset
++] = value
;
2079 if (sd
->data_offset
>= sd
->blk_len
) {
2080 /* TODO: Check CRC before committing */
2081 sd
->state
= sd_programming_state
;
2082 sd_lock_command(sd
);
2083 /* Bzzzzzzztt .... Operation complete. */
2084 sd
->state
= sd_transfer_state
;
2088 case 56: /* CMD56: GEN_CMD */
2089 sd
->data
[sd
->data_offset
++] = value
;
2090 if (sd
->data_offset
>= sd
->blk_len
) {
2091 APP_WRITE_BLOCK(sd
->data_start
, sd
->data_offset
);
2092 sd
->state
= sd_transfer_state
;
2097 qemu_log_mask(LOG_GUEST_ERROR
, "%s: unknown command\n", __func__
);
2102 uint8_t sd_read_byte(SDState
*sd
)
2104 /* TODO: Append CRCs */
2108 if (!sd
->blk
|| !blk_is_inserted(sd
->blk
) || !sd
->enable
)
2111 if (sd
->state
!= sd_sendingdata_state
) {
2112 qemu_log_mask(LOG_GUEST_ERROR
,
2113 "%s: not in Sending-Data state\n", __func__
);
2117 if (sd
->card_status
& (ADDRESS_ERROR
| WP_VIOLATION
))
2120 io_len
= sd_blk_len(sd
);
2122 trace_sdcard_read_data(sd
->proto
->name
,
2124 sd
->current_cmd
, sd
->data_offset
, io_len
);
2125 switch (sd
->current_cmd
) {
2126 case 6: /* CMD6: SWITCH_FUNCTION */
2127 case 9: /* CMD9: SEND_CSD */
2128 case 10: /* CMD10: SEND_CID */
2129 case 13: /* ACMD13: SD_STATUS */
2130 case 17: /* CMD17: READ_SINGLE_BLOCK */
2131 case 19: /* CMD19: SEND_TUNING_BLOCK (SD) */
2132 case 30: /* CMD30: SEND_WRITE_PROT */
2133 sd_generic_read_byte(sd
, &ret
);
2136 case 18: /* CMD18: READ_MULTIPLE_BLOCK */
2137 if (sd
->data_offset
== 0) {
2138 if (!address_in_range(sd
, "READ_MULTIPLE_BLOCK",
2139 sd
->data_start
, io_len
)) {
2142 sd_blk_read(sd
, sd
->data_start
, io_len
);
2144 ret
= sd
->data
[sd
->data_offset
++];
2146 if (sd
->data_offset
>= io_len
) {
2147 sd
->data_start
+= io_len
;
2148 sd
->data_offset
= 0;
2150 if (sd
->multi_blk_cnt
!= 0) {
2151 if (--sd
->multi_blk_cnt
== 0) {
2153 sd
->state
= sd_transfer_state
;
2160 case 22: /* ACMD22: SEND_NUM_WR_BLOCKS */
2161 ret
= sd
->data
[sd
->data_offset
++];
2163 if (sd
->data_offset
>= 4)
2164 sd
->state
= sd_transfer_state
;
2167 case 51: /* ACMD51: SEND_SCR */
2168 ret
= sd
->scr
[sd
->data_offset
++];
2170 if (sd
->data_offset
>= sizeof(sd
->scr
))
2171 sd
->state
= sd_transfer_state
;
2174 case 56: /* CMD56: GEN_CMD */
2175 if (sd
->data_offset
== 0)
2176 APP_READ_BLOCK(sd
->data_start
, sd
->blk_len
);
2177 ret
= sd
->data
[sd
->data_offset
++];
2179 if (sd
->data_offset
>= sd
->blk_len
)
2180 sd
->state
= sd_transfer_state
;
2184 qemu_log_mask(LOG_GUEST_ERROR
, "%s: unknown command\n", __func__
);
2191 static bool sd_receive_ready(SDState
*sd
)
2193 return sd
->state
== sd_receivingdata_state
;
2196 static bool sd_data_ready(SDState
*sd
)
2198 return sd
->state
== sd_sendingdata_state
;
2201 void sd_enable(SDState
*sd
, bool enable
)
2203 sd
->enable
= enable
;
2206 static const SDProto sd_proto_spi
= {
2209 [0] = sd_cmd_GO_IDLE_STATE
,
2210 [1] = spi_cmd_SEND_OP_COND
,
2213 [41] = spi_cmd_SEND_OP_COND
,
2217 static const SDProto sd_proto_sd
= {
2220 [0] = sd_cmd_GO_IDLE_STATE
,
2221 [2] = sd_cmd_ALL_SEND_CID
,
2222 [3] = sd_cmd_SEND_RELATIVE_ADDR
,
2223 [19] = sd_cmd_SEND_TUNING_BLOCK
,
2224 [23] = sd_cmd_SET_BLOCK_COUNT
,
2228 static void sd_instance_init(Object
*obj
)
2230 SDState
*sd
= SD_CARD(obj
);
2231 SDCardClass
*sc
= SD_CARD_GET_CLASS(sd
);
2233 sd
->proto
= sc
->proto
;
2234 sd
->last_cmd_name
= "UNSET";
2236 sd
->ocr_power_timer
= timer_new_ns(QEMU_CLOCK_VIRTUAL
, sd_ocr_powerup
, sd
);
2239 static void sd_instance_finalize(Object
*obj
)
2241 SDState
*sd
= SD_CARD(obj
);
2243 timer_free(sd
->ocr_power_timer
);
2246 static void sd_realize(DeviceState
*dev
, Error
**errp
)
2248 SDState
*sd
= SD_CARD(dev
);
2251 switch (sd
->spec_version
) {
2252 case SD_PHY_SPECv1_10_VERS
2253 ... SD_PHY_SPECv3_01_VERS
:
2256 error_setg(errp
, "Invalid SD card Spec version: %u", sd
->spec_version
);
2263 if (!blk_supports_write_perm(sd
->blk
)) {
2264 error_setg(errp
, "Cannot use read-only drive as SD card");
2268 blk_size
= blk_getlength(sd
->blk
);
2269 if (blk_size
> 0 && !is_power_of_2(blk_size
)) {
2270 int64_t blk_size_aligned
= pow2ceil(blk_size
);
2273 blk_size_str
= size_to_str(blk_size
);
2274 error_setg(errp
, "Invalid SD card size: %s", blk_size_str
);
2275 g_free(blk_size_str
);
2277 blk_size_str
= size_to_str(blk_size_aligned
);
2278 error_append_hint(errp
,
2279 "SD card size has to be a power of 2, e.g. %s.\n"
2280 "You can resize disk images with"
2281 " 'qemu-img resize <imagefile> <new-size>'\n"
2282 "(note that this will lose data if you make the"
2283 " image smaller than it currently is).\n",
2285 g_free(blk_size_str
);
2290 ret
= blk_set_perm(sd
->blk
, BLK_PERM_CONSISTENT_READ
| BLK_PERM_WRITE
,
2291 BLK_PERM_ALL
, errp
);
2295 blk_set_dev_ops(sd
->blk
, &sd_block_ops
, sd
);
2299 static Property sd_properties
[] = {
2300 DEFINE_PROP_UINT8("spec_version", SDState
,
2301 spec_version
, SD_PHY_SPECv2_00_VERS
),
2302 DEFINE_PROP_DRIVE("drive", SDState
, blk
),
2303 /* We do not model the chip select pin, so allow the board to select
2304 * whether card should be in SSI or MMC/SD mode. It is also up to the
2305 * board to ensure that ssi transfers only occur when the chip select
2307 DEFINE_PROP_END_OF_LIST()
2310 static void sd_class_init(ObjectClass
*klass
, void *data
)
2312 DeviceClass
*dc
= DEVICE_CLASS(klass
);
2313 SDCardClass
*sc
= SD_CARD_CLASS(klass
);
2315 dc
->realize
= sd_realize
;
2316 device_class_set_props(dc
, sd_properties
);
2317 dc
->vmsd
= &sd_vmstate
;
2318 dc
->reset
= sd_reset
;
2319 dc
->bus_type
= TYPE_SD_BUS
;
2320 set_bit(DEVICE_CATEGORY_STORAGE
, dc
->categories
);
2322 sc
->set_voltage
= sd_set_voltage
;
2323 sc
->get_dat_lines
= sd_get_dat_lines
;
2324 sc
->get_cmd_line
= sd_get_cmd_line
;
2325 sc
->do_command
= sd_do_command
;
2326 sc
->write_byte
= sd_write_byte
;
2327 sc
->read_byte
= sd_read_byte
;
2328 sc
->receive_ready
= sd_receive_ready
;
2329 sc
->data_ready
= sd_data_ready
;
2330 sc
->enable
= sd_enable
;
2331 sc
->get_inserted
= sd_get_inserted
;
2332 sc
->get_readonly
= sd_get_readonly
;
2333 sc
->proto
= &sd_proto_sd
;
2337 * We do not model the chip select pin, so allow the board to select
2338 * whether card should be in SSI or MMC/SD mode. It is also up to the
2339 * board to ensure that ssi transfers only occur when the chip select
2342 static void sd_spi_class_init(ObjectClass
*klass
, void *data
)
2344 DeviceClass
*dc
= DEVICE_CLASS(klass
);
2345 SDCardClass
*sc
= SD_CARD_CLASS(klass
);
2347 dc
->desc
= "SD SPI";
2348 sc
->proto
= &sd_proto_spi
;
2351 static const TypeInfo sd_types
[] = {
2353 .name
= TYPE_SD_CARD
,
2354 .parent
= TYPE_DEVICE
,
2355 .instance_size
= sizeof(SDState
),
2356 .class_size
= sizeof(SDCardClass
),
2357 .class_init
= sd_class_init
,
2358 .instance_init
= sd_instance_init
,
2359 .instance_finalize
= sd_instance_finalize
,
2362 .name
= TYPE_SD_CARD_SPI
,
2363 .parent
= TYPE_SD_CARD
,
2364 .class_init
= sd_spi_class_init
,
2368 DEFINE_TYPES(sd_types
)