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/module.h"
21 #include "qom/object.h"
23 //#define DEBUG_SSI_SD 1
26 #define DPRINTF(fmt, ...) \
27 do { printf("ssi_sd: " fmt , ## __VA_ARGS__); } while (0)
28 #define BADF(fmt, ...) \
29 do { fprintf(stderr, "ssi_sd: error: " fmt , ## __VA_ARGS__); exit(1);} while (0)
31 #define DPRINTF(fmt, ...) do {} while(0)
32 #define BADF(fmt, ...) \
33 do { fprintf(stderr, "ssi_sd: error: " fmt , ## __VA_ARGS__);} while (0)
56 #define TYPE_SSI_SD "ssi-sd"
57 OBJECT_DECLARE_SIMPLE_TYPE(ssi_sd_state
, SSI_SD
)
59 /* State word bits. */
60 #define SSI_SDR_LOCKED 0x0001
61 #define SSI_SDR_WP_ERASE 0x0002
62 #define SSI_SDR_ERROR 0x0004
63 #define SSI_SDR_CC_ERROR 0x0008
64 #define SSI_SDR_ECC_FAILED 0x0010
65 #define SSI_SDR_WP_VIOLATION 0x0020
66 #define SSI_SDR_ERASE_PARAM 0x0040
67 #define SSI_SDR_OUT_OF_RANGE 0x0080
68 #define SSI_SDR_IDLE 0x0100
69 #define SSI_SDR_ERASE_RESET 0x0200
70 #define SSI_SDR_ILLEGAL_COMMAND 0x0400
71 #define SSI_SDR_COM_CRC_ERROR 0x0800
72 #define SSI_SDR_ERASE_SEQ_ERROR 0x1000
73 #define SSI_SDR_ADDRESS_ERROR 0x2000
74 #define SSI_SDR_PARAMETER_ERROR 0x4000
76 static uint32_t ssi_sd_transfer(SSISlave
*dev
, uint32_t val
)
78 ssi_sd_state
*s
= SSI_SD(dev
);
80 /* Special case: allow CMD12 (STOP TRANSMISSION) while reading data. */
81 if (s
->mode
== SSI_SD_DATA_READ
&& val
== 0x4d) {
83 /* There must be at least one byte delay before the card responds. */
90 DPRINTF("NULL command\n");
94 s
->mode
= SSI_SD_CMDARG
;
100 uint8_t longresp
[16];
101 /* FIXME: Check CRC. */
102 request
.cmd
= s
->cmd
;
103 request
.arg
= ldl_be_p(s
->cmdarg
);
104 DPRINTF("CMD%d arg 0x%08x\n", s
->cmd
, request
.arg
);
105 s
->arglen
= sdbus_do_command(&s
->sdbus
, &request
, longresp
);
106 if (s
->arglen
<= 0) {
109 DPRINTF("SD command failed\n");
110 } else if (s
->cmd
== 58) {
111 /* CMD58 returns R3 response (OCR) */
112 DPRINTF("Returned OCR\n");
115 memcpy(&s
->response
[1], longresp
, 4);
116 } else if (s
->arglen
!= 4) {
117 BADF("Unexpected response to cmd %d\n", s
->cmd
);
118 /* Illegal command is about as near as we can get. */
122 /* All other commands return status. */
125 /* CMD13 returns a 2-byte statuse work. Other commands
126 only return the first byte. */
127 s
->arglen
= (s
->cmd
== 13) ? 2 : 1;
128 cardstatus
= ldl_be_p(longresp
);
130 if (((cardstatus
>> 9) & 0xf) < 4)
131 status
|= SSI_SDR_IDLE
;
132 if (cardstatus
& ERASE_RESET
)
133 status
|= SSI_SDR_ERASE_RESET
;
134 if (cardstatus
& ILLEGAL_COMMAND
)
135 status
|= SSI_SDR_ILLEGAL_COMMAND
;
136 if (cardstatus
& COM_CRC_ERROR
)
137 status
|= SSI_SDR_COM_CRC_ERROR
;
138 if (cardstatus
& ERASE_SEQ_ERROR
)
139 status
|= SSI_SDR_ERASE_SEQ_ERROR
;
140 if (cardstatus
& ADDRESS_ERROR
)
141 status
|= SSI_SDR_ADDRESS_ERROR
;
142 if (cardstatus
& CARD_IS_LOCKED
)
143 status
|= SSI_SDR_LOCKED
;
144 if (cardstatus
& (LOCK_UNLOCK_FAILED
| WP_ERASE_SKIP
))
145 status
|= SSI_SDR_WP_ERASE
;
146 if (cardstatus
& SD_ERROR
)
147 status
|= SSI_SDR_ERROR
;
148 if (cardstatus
& CC_ERROR
)
149 status
|= SSI_SDR_CC_ERROR
;
150 if (cardstatus
& CARD_ECC_FAILED
)
151 status
|= SSI_SDR_ECC_FAILED
;
152 if (cardstatus
& WP_VIOLATION
)
153 status
|= SSI_SDR_WP_VIOLATION
;
154 if (cardstatus
& ERASE_PARAM
)
155 status
|= SSI_SDR_ERASE_PARAM
;
156 if (cardstatus
& (OUT_OF_RANGE
| CID_CSD_OVERWRITE
))
157 status
|= SSI_SDR_OUT_OF_RANGE
;
158 /* ??? Don't know what Parameter Error really means, so
159 assume it's set if the second byte is nonzero. */
161 status
|= SSI_SDR_PARAMETER_ERROR
;
162 s
->response
[0] = status
>> 8;
163 s
->response
[1] = status
;
164 DPRINTF("Card status 0x%02x\n", status
);
166 s
->mode
= SSI_SD_RESPONSE
;
169 s
->cmdarg
[s
->arglen
++] = val
;
172 case SSI_SD_RESPONSE
:
177 if (s
->response_pos
< s
->arglen
) {
178 DPRINTF("Response 0x%02x\n", s
->response
[s
->response_pos
]);
179 return s
->response
[s
->response_pos
++];
181 if (sdbus_data_ready(&s
->sdbus
)) {
182 DPRINTF("Data read\n");
183 s
->mode
= SSI_SD_DATA_START
;
185 DPRINTF("End of command\n");
186 s
->mode
= SSI_SD_CMD
;
189 case SSI_SD_DATA_START
:
190 DPRINTF("Start read block\n");
191 s
->mode
= SSI_SD_DATA_READ
;
193 case SSI_SD_DATA_READ
:
194 val
= sdbus_read_byte(&s
->sdbus
);
195 if (!sdbus_data_ready(&s
->sdbus
)) {
196 DPRINTF("Data read end\n");
197 s
->mode
= SSI_SD_CMD
;
201 /* Should never happen. */
205 static int ssi_sd_post_load(void *opaque
, int version_id
)
207 ssi_sd_state
*s
= (ssi_sd_state
*)opaque
;
209 if (s
->mode
> SSI_SD_DATA_READ
) {
212 if (s
->mode
== SSI_SD_CMDARG
&&
213 (s
->arglen
< 0 || s
->arglen
>= ARRAY_SIZE(s
->cmdarg
))) {
216 if (s
->mode
== SSI_SD_RESPONSE
&&
217 (s
->response_pos
< 0 || s
->response_pos
>= ARRAY_SIZE(s
->response
) ||
218 (!s
->stopping
&& s
->arglen
> ARRAY_SIZE(s
->response
)))) {
225 static const VMStateDescription vmstate_ssi_sd
= {
228 .minimum_version_id
= 2,
229 .post_load
= ssi_sd_post_load
,
230 .fields
= (VMStateField
[]) {
231 VMSTATE_UINT32(mode
, ssi_sd_state
),
232 VMSTATE_INT32(cmd
, ssi_sd_state
),
233 VMSTATE_UINT8_ARRAY(cmdarg
, ssi_sd_state
, 4),
234 VMSTATE_UINT8_ARRAY(response
, ssi_sd_state
, 5),
235 VMSTATE_INT32(arglen
, ssi_sd_state
),
236 VMSTATE_INT32(response_pos
, ssi_sd_state
),
237 VMSTATE_INT32(stopping
, ssi_sd_state
),
238 VMSTATE_SSI_SLAVE(ssidev
, ssi_sd_state
),
239 VMSTATE_END_OF_LIST()
243 static void ssi_sd_realize(SSISlave
*d
, Error
**errp
)
246 ssi_sd_state
*s
= SSI_SD(d
);
247 DeviceState
*carddev
;
250 qbus_create_inplace(&s
->sdbus
, sizeof(s
->sdbus
), TYPE_SD_BUS
,
251 DEVICE(d
), "sd-bus");
253 /* Create and plug in the sd card */
254 /* FIXME use a qdev drive property instead of drive_get_next() */
255 dinfo
= drive_get_next(IF_SD
);
256 carddev
= qdev_new(TYPE_SD_CARD
);
258 if (!qdev_prop_set_drive_err(carddev
, "drive",
259 blk_by_legacy_dinfo(dinfo
), errp
)) {
264 if (!object_property_set_bool(OBJECT(carddev
), "spi", true, errp
)) {
268 if (!qdev_realize_and_unref(carddev
, BUS(&s
->sdbus
), errp
)) {
275 error_prepend(errp
, "failed to init SD card: ");
278 static void ssi_sd_reset(DeviceState
*dev
)
280 ssi_sd_state
*s
= SSI_SD(dev
);
282 s
->mode
= SSI_SD_CMD
;
284 memset(s
->cmdarg
, 0, sizeof(s
->cmdarg
));
285 memset(s
->response
, 0, sizeof(s
->response
));
291 static void ssi_sd_class_init(ObjectClass
*klass
, void *data
)
293 DeviceClass
*dc
= DEVICE_CLASS(klass
);
294 SSISlaveClass
*k
= SSI_SLAVE_CLASS(klass
);
296 k
->realize
= ssi_sd_realize
;
297 k
->transfer
= ssi_sd_transfer
;
298 k
->cs_polarity
= SSI_CS_LOW
;
299 dc
->vmsd
= &vmstate_ssi_sd
;
300 dc
->reset
= ssi_sd_reset
;
301 /* Reason: init() method uses drive_get_next() */
302 dc
->user_creatable
= false;
305 static const TypeInfo ssi_sd_info
= {
307 .parent
= TYPE_SSI_SLAVE
,
308 .instance_size
= sizeof(ssi_sd_state
),
309 .class_init
= ssi_sd_class_init
,
312 static void ssi_sd_register_types(void)
314 type_register_static(&ssi_sd_info
);
317 type_init(ssi_sd_register_types
)