2 * SSI to SD card adapter.
4 * Copyright (c) 2007-2009 CodeSourcery.
5 * Written by Paul Brook
7 * This code is licensed under the GNU GPL v2.
9 * Contributions after 2012-01-13 are licensed under the terms of the
10 * GNU GPL, version 2 or (at your option) any later version.
13 #include "qemu/osdep.h"
14 #include "sysemu/blockdev.h"
15 #include "hw/ssi/ssi.h"
16 #include "migration/vmstate.h"
17 #include "hw/qdev-properties.h"
19 #include "qapi/error.h"
20 #include "qemu/crc-ccitt.h"
21 #include "qemu/module.h"
22 #include "qom/object.h"
24 //#define DEBUG_SSI_SD 1
27 #define DPRINTF(fmt, ...) \
28 do { printf("ssi_sd: " fmt , ## __VA_ARGS__); } while (0)
29 #define BADF(fmt, ...) \
30 do { fprintf(stderr, "ssi_sd: error: " fmt , ## __VA_ARGS__); exit(1);} while (0)
32 #define DPRINTF(fmt, ...) do {} while(0)
33 #define BADF(fmt, ...) \
34 do { fprintf(stderr, "ssi_sd: error: " fmt , ## __VA_ARGS__);} while (0)
61 #define TYPE_SSI_SD "ssi-sd"
62 OBJECT_DECLARE_SIMPLE_TYPE(ssi_sd_state
, SSI_SD
)
64 /* State word bits. */
65 #define SSI_SDR_LOCKED 0x0001
66 #define SSI_SDR_WP_ERASE 0x0002
67 #define SSI_SDR_ERROR 0x0004
68 #define SSI_SDR_CC_ERROR 0x0008
69 #define SSI_SDR_ECC_FAILED 0x0010
70 #define SSI_SDR_WP_VIOLATION 0x0020
71 #define SSI_SDR_ERASE_PARAM 0x0040
72 #define SSI_SDR_OUT_OF_RANGE 0x0080
73 #define SSI_SDR_IDLE 0x0100
74 #define SSI_SDR_ERASE_RESET 0x0200
75 #define SSI_SDR_ILLEGAL_COMMAND 0x0400
76 #define SSI_SDR_COM_CRC_ERROR 0x0800
77 #define SSI_SDR_ERASE_SEQ_ERROR 0x1000
78 #define SSI_SDR_ADDRESS_ERROR 0x2000
79 #define SSI_SDR_PARAMETER_ERROR 0x4000
81 /* single block read/write, multiple block read */
82 #define SSI_TOKEN_SINGLE 0xfe
84 /* dummy value - don't care */
85 #define SSI_DUMMY 0xff
87 static uint32_t ssi_sd_transfer(SSIPeripheral
*dev
, uint32_t val
)
89 ssi_sd_state
*s
= SSI_SD(dev
);
91 /* Special case: allow CMD12 (STOP TRANSMISSION) while reading data. */
92 if (s
->mode
== SSI_SD_DATA_READ
&& val
== 0x4c) {
94 /* There must be at least one byte delay before the card responds. */
100 if (val
== SSI_DUMMY
) {
101 DPRINTF("NULL command\n");
105 s
->mode
= SSI_SD_CMDARG
;
109 if (s
->arglen
== 4) {
111 uint8_t longresp
[16];
112 /* FIXME: Check CRC. */
113 request
.cmd
= s
->cmd
;
114 request
.arg
= ldl_be_p(s
->cmdarg
);
115 DPRINTF("CMD%d arg 0x%08x\n", s
->cmd
, request
.arg
);
116 s
->arglen
= sdbus_do_command(&s
->sdbus
, &request
, longresp
);
117 if (s
->arglen
<= 0) {
120 DPRINTF("SD command failed\n");
121 } else if (s
->cmd
== 58) {
122 /* CMD58 returns R3 response (OCR) */
123 DPRINTF("Returned OCR\n");
126 memcpy(&s
->response
[1], longresp
, 4);
127 } else if (s
->arglen
!= 4) {
128 BADF("Unexpected response to cmd %d\n", s
->cmd
);
129 /* Illegal command is about as near as we can get. */
133 /* All other commands return status. */
136 /* CMD13 returns a 2-byte statuse work. Other commands
137 only return the first byte. */
138 s
->arglen
= (s
->cmd
== 13) ? 2 : 1;
139 cardstatus
= ldl_be_p(longresp
);
141 if (((cardstatus
>> 9) & 0xf) < 4)
142 status
|= SSI_SDR_IDLE
;
143 if (cardstatus
& ERASE_RESET
)
144 status
|= SSI_SDR_ERASE_RESET
;
145 if (cardstatus
& ILLEGAL_COMMAND
)
146 status
|= SSI_SDR_ILLEGAL_COMMAND
;
147 if (cardstatus
& COM_CRC_ERROR
)
148 status
|= SSI_SDR_COM_CRC_ERROR
;
149 if (cardstatus
& ERASE_SEQ_ERROR
)
150 status
|= SSI_SDR_ERASE_SEQ_ERROR
;
151 if (cardstatus
& ADDRESS_ERROR
)
152 status
|= SSI_SDR_ADDRESS_ERROR
;
153 if (cardstatus
& CARD_IS_LOCKED
)
154 status
|= SSI_SDR_LOCKED
;
155 if (cardstatus
& (LOCK_UNLOCK_FAILED
| WP_ERASE_SKIP
))
156 status
|= SSI_SDR_WP_ERASE
;
157 if (cardstatus
& SD_ERROR
)
158 status
|= SSI_SDR_ERROR
;
159 if (cardstatus
& CC_ERROR
)
160 status
|= SSI_SDR_CC_ERROR
;
161 if (cardstatus
& CARD_ECC_FAILED
)
162 status
|= SSI_SDR_ECC_FAILED
;
163 if (cardstatus
& WP_VIOLATION
)
164 status
|= SSI_SDR_WP_VIOLATION
;
165 if (cardstatus
& ERASE_PARAM
)
166 status
|= SSI_SDR_ERASE_PARAM
;
167 if (cardstatus
& (OUT_OF_RANGE
| CID_CSD_OVERWRITE
))
168 status
|= SSI_SDR_OUT_OF_RANGE
;
169 /* ??? Don't know what Parameter Error really means, so
170 assume it's set if the second byte is nonzero. */
172 status
|= SSI_SDR_PARAMETER_ERROR
;
173 s
->response
[0] = status
>> 8;
174 s
->response
[1] = status
;
175 DPRINTF("Card status 0x%02x\n", status
);
177 s
->mode
= SSI_SD_PREP_RESP
;
180 s
->cmdarg
[s
->arglen
++] = val
;
183 case SSI_SD_PREP_RESP
:
184 DPRINTF("Prepare card response (Ncr)\n");
185 s
->mode
= SSI_SD_RESPONSE
;
187 case SSI_SD_RESPONSE
:
192 if (s
->response_pos
< s
->arglen
) {
193 DPRINTF("Response 0x%02x\n", s
->response
[s
->response_pos
]);
194 return s
->response
[s
->response_pos
++];
196 if (sdbus_data_ready(&s
->sdbus
)) {
197 DPRINTF("Data read\n");
198 s
->mode
= SSI_SD_DATA_START
;
200 DPRINTF("End of command\n");
201 s
->mode
= SSI_SD_CMD
;
204 case SSI_SD_PREP_DATA
:
205 DPRINTF("Prepare data block (Nac)\n");
206 s
->mode
= SSI_SD_DATA_START
;
208 case SSI_SD_DATA_START
:
209 DPRINTF("Start read block\n");
210 s
->mode
= SSI_SD_DATA_READ
;
212 return SSI_TOKEN_SINGLE
;
213 case SSI_SD_DATA_READ
:
214 val
= sdbus_read_byte(&s
->sdbus
);
215 s
->crc16
= crc_ccitt_false(s
->crc16
, (uint8_t *)&val
, 1);
216 if (!sdbus_data_ready(&s
->sdbus
)) {
217 DPRINTF("Data read end\n");
218 s
->mode
= SSI_SD_DATA_CRC16
;
221 case SSI_SD_DATA_CRC16
:
222 val
= (s
->crc16
& 0xff00) >> 8;
225 if (s
->response_pos
== 2) {
226 DPRINTF("CRC16 read end\n");
227 s
->mode
= SSI_SD_CMD
;
232 /* Should never happen. */
236 static int ssi_sd_post_load(void *opaque
, int version_id
)
238 ssi_sd_state
*s
= (ssi_sd_state
*)opaque
;
240 if (s
->mode
> SSI_SD_DATA_CRC16
) {
243 if (s
->mode
== SSI_SD_CMDARG
&&
244 (s
->arglen
< 0 || s
->arglen
>= ARRAY_SIZE(s
->cmdarg
))) {
247 if (s
->mode
== SSI_SD_RESPONSE
&&
248 (s
->response_pos
< 0 || s
->response_pos
>= ARRAY_SIZE(s
->response
) ||
249 (!s
->stopping
&& s
->arglen
> ARRAY_SIZE(s
->response
)))) {
256 static const VMStateDescription vmstate_ssi_sd
= {
259 .minimum_version_id
= 5,
260 .post_load
= ssi_sd_post_load
,
261 .fields
= (VMStateField
[]) {
262 VMSTATE_UINT32(mode
, ssi_sd_state
),
263 VMSTATE_INT32(cmd
, ssi_sd_state
),
264 VMSTATE_UINT8_ARRAY(cmdarg
, ssi_sd_state
, 4),
265 VMSTATE_UINT8_ARRAY(response
, ssi_sd_state
, 5),
266 VMSTATE_UINT16(crc16
, ssi_sd_state
),
267 VMSTATE_INT32(arglen
, ssi_sd_state
),
268 VMSTATE_INT32(response_pos
, ssi_sd_state
),
269 VMSTATE_INT32(stopping
, ssi_sd_state
),
270 VMSTATE_SSI_PERIPHERAL(ssidev
, ssi_sd_state
),
271 VMSTATE_END_OF_LIST()
275 static void ssi_sd_realize(SSIPeripheral
*d
, Error
**errp
)
278 ssi_sd_state
*s
= SSI_SD(d
);
279 DeviceState
*carddev
;
282 qbus_create_inplace(&s
->sdbus
, sizeof(s
->sdbus
), TYPE_SD_BUS
,
283 DEVICE(d
), "sd-bus");
285 /* Create and plug in the sd card */
286 /* FIXME use a qdev drive property instead of drive_get_next() */
287 dinfo
= drive_get_next(IF_SD
);
288 carddev
= qdev_new(TYPE_SD_CARD
);
290 if (!qdev_prop_set_drive_err(carddev
, "drive",
291 blk_by_legacy_dinfo(dinfo
), errp
)) {
296 if (!object_property_set_bool(OBJECT(carddev
), "spi", true, errp
)) {
300 if (!qdev_realize_and_unref(carddev
, BUS(&s
->sdbus
), errp
)) {
307 error_prepend(errp
, "failed to init SD card: ");
310 static void ssi_sd_reset(DeviceState
*dev
)
312 ssi_sd_state
*s
= SSI_SD(dev
);
314 s
->mode
= SSI_SD_CMD
;
316 memset(s
->cmdarg
, 0, sizeof(s
->cmdarg
));
317 memset(s
->response
, 0, sizeof(s
->response
));
324 static void ssi_sd_class_init(ObjectClass
*klass
, void *data
)
326 DeviceClass
*dc
= DEVICE_CLASS(klass
);
327 SSIPeripheralClass
*k
= SSI_PERIPHERAL_CLASS(klass
);
329 k
->realize
= ssi_sd_realize
;
330 k
->transfer
= ssi_sd_transfer
;
331 k
->cs_polarity
= SSI_CS_LOW
;
332 dc
->vmsd
= &vmstate_ssi_sd
;
333 dc
->reset
= ssi_sd_reset
;
334 /* Reason: init() method uses drive_get_next() */
335 dc
->user_creatable
= false;
338 static const TypeInfo ssi_sd_info
= {
340 .parent
= TYPE_SSI_PERIPHERAL
,
341 .instance_size
= sizeof(ssi_sd_state
),
342 .class_init
= ssi_sd_class_init
,
345 static void ssi_sd_register_types(void)
347 type_register_static(&ssi_sd_info
);
350 type_init(ssi_sd_register_types
)