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 "sysemu/block-backend.h"
14 #include "sysemu/blockdev.h"
18 //#define DEBUG_SSI_SD 1
21 #define DPRINTF(fmt, ...) \
22 do { printf("ssi_sd: " fmt , ## __VA_ARGS__); } while (0)
23 #define BADF(fmt, ...) \
24 do { fprintf(stderr, "ssi_sd: error: " fmt , ## __VA_ARGS__); exit(1);} while (0)
26 #define DPRINTF(fmt, ...) do {} while(0)
27 #define BADF(fmt, ...) \
28 do { fprintf(stderr, "ssi_sd: error: " fmt , ## __VA_ARGS__);} while (0)
51 /* State word bits. */
52 #define SSI_SDR_LOCKED 0x0001
53 #define SSI_SDR_WP_ERASE 0x0002
54 #define SSI_SDR_ERROR 0x0004
55 #define SSI_SDR_CC_ERROR 0x0008
56 #define SSI_SDR_ECC_FAILED 0x0010
57 #define SSI_SDR_WP_VIOLATION 0x0020
58 #define SSI_SDR_ERASE_PARAM 0x0040
59 #define SSI_SDR_OUT_OF_RANGE 0x0080
60 #define SSI_SDR_IDLE 0x0100
61 #define SSI_SDR_ERASE_RESET 0x0200
62 #define SSI_SDR_ILLEGAL_COMMAND 0x0400
63 #define SSI_SDR_COM_CRC_ERROR 0x0800
64 #define SSI_SDR_ERASE_SEQ_ERROR 0x1000
65 #define SSI_SDR_ADDRESS_ERROR 0x2000
66 #define SSI_SDR_PARAMETER_ERROR 0x4000
68 static uint32_t ssi_sd_transfer(SSISlave
*dev
, uint32_t val
)
70 ssi_sd_state
*s
= FROM_SSI_SLAVE(ssi_sd_state
, dev
);
72 /* Special case: allow CMD12 (STOP TRANSMISSION) while reading data. */
73 if (s
->mode
== SSI_SD_DATA_READ
&& val
== 0x4d) {
75 /* There must be at least one byte delay before the card responds. */
82 DPRINTF("NULL command\n");
86 s
->mode
= SSI_SD_CMDARG
;
93 /* FIXME: Check CRC. */
95 request
.arg
= (s
->cmdarg
[0] << 24) | (s
->cmdarg
[1] << 16)
96 | (s
->cmdarg
[2] << 8) | s
->cmdarg
[3];
97 DPRINTF("CMD%d arg 0x%08x\n", s
->cmd
, request
.arg
);
98 s
->arglen
= sd_do_command(s
->sd
, &request
, longresp
);
102 DPRINTF("SD command failed\n");
103 } else if (s
->cmd
== 58) {
104 /* CMD58 returns R3 response (OCR) */
105 DPRINTF("Returned OCR\n");
108 memcpy(&s
->response
[1], longresp
, 4);
109 } else if (s
->arglen
!= 4) {
110 BADF("Unexpected response to cmd %d\n", s
->cmd
);
111 /* Illegal command is about as near as we can get. */
115 /* All other commands return status. */
118 /* CMD13 returns a 2-byte statuse work. Other commands
119 only return the first byte. */
120 s
->arglen
= (s
->cmd
== 13) ? 2 : 1;
121 cardstatus
= (longresp
[0] << 24) | (longresp
[1] << 16)
122 | (longresp
[2] << 8) | longresp
[3];
124 if (((cardstatus
>> 9) & 0xf) < 4)
125 status
|= SSI_SDR_IDLE
;
126 if (cardstatus
& ERASE_RESET
)
127 status
|= SSI_SDR_ERASE_RESET
;
128 if (cardstatus
& ILLEGAL_COMMAND
)
129 status
|= SSI_SDR_ILLEGAL_COMMAND
;
130 if (cardstatus
& COM_CRC_ERROR
)
131 status
|= SSI_SDR_COM_CRC_ERROR
;
132 if (cardstatus
& ERASE_SEQ_ERROR
)
133 status
|= SSI_SDR_ERASE_SEQ_ERROR
;
134 if (cardstatus
& ADDRESS_ERROR
)
135 status
|= SSI_SDR_ADDRESS_ERROR
;
136 if (cardstatus
& CARD_IS_LOCKED
)
137 status
|= SSI_SDR_LOCKED
;
138 if (cardstatus
& (LOCK_UNLOCK_FAILED
| WP_ERASE_SKIP
))
139 status
|= SSI_SDR_WP_ERASE
;
140 if (cardstatus
& SD_ERROR
)
141 status
|= SSI_SDR_ERROR
;
142 if (cardstatus
& CC_ERROR
)
143 status
|= SSI_SDR_CC_ERROR
;
144 if (cardstatus
& CARD_ECC_FAILED
)
145 status
|= SSI_SDR_ECC_FAILED
;
146 if (cardstatus
& WP_VIOLATION
)
147 status
|= SSI_SDR_WP_VIOLATION
;
148 if (cardstatus
& ERASE_PARAM
)
149 status
|= SSI_SDR_ERASE_PARAM
;
150 if (cardstatus
& (OUT_OF_RANGE
| CID_CSD_OVERWRITE
))
151 status
|= SSI_SDR_OUT_OF_RANGE
;
152 /* ??? Don't know what Parameter Error really means, so
153 assume it's set if the second byte is nonzero. */
155 status
|= SSI_SDR_PARAMETER_ERROR
;
156 s
->response
[0] = status
>> 8;
157 s
->response
[1] = status
;
158 DPRINTF("Card status 0x%02x\n", status
);
160 s
->mode
= SSI_SD_RESPONSE
;
163 s
->cmdarg
[s
->arglen
++] = val
;
166 case SSI_SD_RESPONSE
:
171 if (s
->response_pos
< s
->arglen
) {
172 DPRINTF("Response 0x%02x\n", s
->response
[s
->response_pos
]);
173 return s
->response
[s
->response_pos
++];
175 if (sd_data_ready(s
->sd
)) {
176 DPRINTF("Data read\n");
177 s
->mode
= SSI_SD_DATA_START
;
179 DPRINTF("End of command\n");
180 s
->mode
= SSI_SD_CMD
;
183 case SSI_SD_DATA_START
:
184 DPRINTF("Start read block\n");
185 s
->mode
= SSI_SD_DATA_READ
;
187 case SSI_SD_DATA_READ
:
188 val
= sd_read_data(s
->sd
);
189 if (!sd_data_ready(s
->sd
)) {
190 DPRINTF("Data read end\n");
191 s
->mode
= SSI_SD_CMD
;
195 /* Should never happen. */
199 static void ssi_sd_save(QEMUFile
*f
, void *opaque
)
201 SSISlave
*ss
= SSI_SLAVE(opaque
);
202 ssi_sd_state
*s
= (ssi_sd_state
*)opaque
;
205 qemu_put_be32(f
, s
->mode
);
206 qemu_put_be32(f
, s
->cmd
);
207 for (i
= 0; i
< 4; i
++)
208 qemu_put_be32(f
, s
->cmdarg
[i
]);
209 for (i
= 0; i
< 5; i
++)
210 qemu_put_be32(f
, s
->response
[i
]);
211 qemu_put_be32(f
, s
->arglen
);
212 qemu_put_be32(f
, s
->response_pos
);
213 qemu_put_be32(f
, s
->stopping
);
215 qemu_put_be32(f
, ss
->cs
);
218 static int ssi_sd_load(QEMUFile
*f
, void *opaque
, int version_id
)
220 SSISlave
*ss
= SSI_SLAVE(opaque
);
221 ssi_sd_state
*s
= (ssi_sd_state
*)opaque
;
227 s
->mode
= qemu_get_be32(f
);
228 s
->cmd
= qemu_get_be32(f
);
229 for (i
= 0; i
< 4; i
++)
230 s
->cmdarg
[i
] = qemu_get_be32(f
);
231 for (i
= 0; i
< 5; i
++)
232 s
->response
[i
] = qemu_get_be32(f
);
233 s
->arglen
= qemu_get_be32(f
);
234 if (s
->mode
== SSI_SD_CMDARG
&&
235 (s
->arglen
< 0 || s
->arglen
>= ARRAY_SIZE(s
->cmdarg
))) {
238 s
->response_pos
= qemu_get_be32(f
);
239 s
->stopping
= qemu_get_be32(f
);
240 if (s
->mode
== SSI_SD_RESPONSE
&&
241 (s
->response_pos
< 0 || s
->response_pos
>= ARRAY_SIZE(s
->response
) ||
242 (!s
->stopping
&& s
->arglen
> ARRAY_SIZE(s
->response
)))) {
246 ss
->cs
= qemu_get_be32(f
);
251 static int ssi_sd_init(SSISlave
*d
)
253 DeviceState
*dev
= DEVICE(d
);
254 ssi_sd_state
*s
= FROM_SSI_SLAVE(ssi_sd_state
, d
);
257 s
->mode
= SSI_SD_CMD
;
258 /* FIXME use a qdev drive property instead of drive_get_next() */
259 dinfo
= drive_get_next(IF_SD
);
260 s
->sd
= sd_init(dinfo
? blk_by_legacy_dinfo(dinfo
) : NULL
, true);
264 register_savevm(dev
, "ssi_sd", -1, 1, ssi_sd_save
, ssi_sd_load
, s
);
268 static void ssi_sd_class_init(ObjectClass
*klass
, void *data
)
270 SSISlaveClass
*k
= SSI_SLAVE_CLASS(klass
);
272 k
->init
= ssi_sd_init
;
273 k
->transfer
= ssi_sd_transfer
;
274 k
->cs_polarity
= SSI_CS_LOW
;
277 static const TypeInfo ssi_sd_info
= {
279 .parent
= TYPE_SSI_SLAVE
,
280 .instance_size
= sizeof(ssi_sd_state
),
281 .class_init
= ssi_sd_class_init
,
284 static void ssi_sd_register_types(void)
286 type_register_static(&ssi_sd_info
);
289 type_init(ssi_sd_register_types
)