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/blockdev.h"
17
//#define DEBUG_SSI_SD 1
20
#define DPRINTF(fmt, ...) \
21
do { printf("ssi_sd: " fmt , ## __VA_ARGS__); } while (0)
22
#define BADF(fmt, ...) \
23
do { fprintf(stderr, "ssi_sd: error: " fmt , ## __VA_ARGS__); exit(1);} while (0)
25
#define DPRINTF(fmt, ...) do {} while(0)
26
#define BADF(fmt, ...) \
27
do { fprintf(stderr, "ssi_sd: error: " fmt , ## __VA_ARGS__);} while (0)
50
/* State word bits. */
51
#define SSI_SDR_LOCKED 0x0001
52
#define SSI_SDR_WP_ERASE 0x0002
53
#define SSI_SDR_ERROR 0x0004
54
#define SSI_SDR_CC_ERROR 0x0008
55
#define SSI_SDR_ECC_FAILED 0x0010
56
#define SSI_SDR_WP_VIOLATION 0x0020
57
#define SSI_SDR_ERASE_PARAM 0x0040
58
#define SSI_SDR_OUT_OF_RANGE 0x0080
59
#define SSI_SDR_IDLE 0x0100
60
#define SSI_SDR_ERASE_RESET 0x0200
61
#define SSI_SDR_ILLEGAL_COMMAND 0x0400
62
#define SSI_SDR_COM_CRC_ERROR 0x0800
63
#define SSI_SDR_ERASE_SEQ_ERROR 0x1000
64
#define SSI_SDR_ADDRESS_ERROR 0x2000
65
#define SSI_SDR_PARAMETER_ERROR 0x4000
67
static uint32_t ssi_sd_transfer(SSISlave *dev, uint32_t val)
69
ssi_sd_state *s = FROM_SSI_SLAVE(ssi_sd_state, dev);
71
/* Special case: allow CMD12 (STOP TRANSMISSION) while reading data. */
72
if (s->mode == SSI_SD_DATA_READ && val == 0x4d) {
74
/* There must be at least one byte delay before the card responds. */
81
DPRINTF("NULL command\n");
85
s->mode = SSI_SD_CMDARG;
92
/* FIXME: Check CRC. */
94
request.arg = (s->cmdarg[0] << 24) | (s->cmdarg[1] << 16)
95
| (s->cmdarg[2] << 8) | s->cmdarg[3];
96
DPRINTF("CMD%d arg 0x%08x\n", s->cmd, request.arg);
97
s->arglen = sd_do_command(s->sd, &request, longresp);
101
DPRINTF("SD command failed\n");
102
} else if (s->cmd == 58) {
103
/* CMD58 returns R3 response (OCR) */
104
DPRINTF("Returned OCR\n");
107
memcpy(&s->response[1], longresp, 4);
108
} else if (s->arglen != 4) {
109
BADF("Unexpected response to cmd %d\n", s->cmd);
110
/* Illegal command is about as near as we can get. */
114
/* All other commands return status. */
117
/* CMD13 returns a 2-byte statuse work. Other commands
118
only return the first byte. */
119
s->arglen = (s->cmd == 13) ? 2 : 1;
120
cardstatus = (longresp[0] << 24) | (longresp[1] << 16)
121
| (longresp[2] << 8) | longresp[3];
123
if (((cardstatus >> 9) & 0xf) < 4)
124
status |= SSI_SDR_IDLE;
125
if (cardstatus & ERASE_RESET)
126
status |= SSI_SDR_ERASE_RESET;
127
if (cardstatus & ILLEGAL_COMMAND)
128
status |= SSI_SDR_ILLEGAL_COMMAND;
129
if (cardstatus & COM_CRC_ERROR)
130
status |= SSI_SDR_COM_CRC_ERROR;
131
if (cardstatus & ERASE_SEQ_ERROR)
132
status |= SSI_SDR_ERASE_SEQ_ERROR;
133
if (cardstatus & ADDRESS_ERROR)
134
status |= SSI_SDR_ADDRESS_ERROR;
135
if (cardstatus & CARD_IS_LOCKED)
136
status |= SSI_SDR_LOCKED;
137
if (cardstatus & (LOCK_UNLOCK_FAILED | WP_ERASE_SKIP))
138
status |= SSI_SDR_WP_ERASE;
139
if (cardstatus & SD_ERROR)
140
status |= SSI_SDR_ERROR;
141
if (cardstatus & CC_ERROR)
142
status |= SSI_SDR_CC_ERROR;
143
if (cardstatus & CARD_ECC_FAILED)
144
status |= SSI_SDR_ECC_FAILED;
145
if (cardstatus & WP_VIOLATION)
146
status |= SSI_SDR_WP_VIOLATION;
147
if (cardstatus & ERASE_PARAM)
148
status |= SSI_SDR_ERASE_PARAM;
149
if (cardstatus & (OUT_OF_RANGE | CID_CSD_OVERWRITE))
150
status |= SSI_SDR_OUT_OF_RANGE;
151
/* ??? Don't know what Parameter Error really means, so
152
assume it's set if the second byte is nonzero. */
154
status |= SSI_SDR_PARAMETER_ERROR;
155
s->response[0] = status >> 8;
156
s->response[1] = status;
157
DPRINTF("Card status 0x%02x\n", status);
159
s->mode = SSI_SD_RESPONSE;
162
s->cmdarg[s->arglen++] = val;
165
case SSI_SD_RESPONSE:
170
if (s->response_pos < s->arglen) {
171
DPRINTF("Response 0x%02x\n", s->response[s->response_pos]);
172
return s->response[s->response_pos++];
174
if (sd_data_ready(s->sd)) {
175
DPRINTF("Data read\n");
176
s->mode = SSI_SD_DATA_START;
178
DPRINTF("End of command\n");
179
s->mode = SSI_SD_CMD;
182
case SSI_SD_DATA_START:
183
DPRINTF("Start read block\n");
184
s->mode = SSI_SD_DATA_READ;
186
case SSI_SD_DATA_READ:
187
val = sd_read_data(s->sd);
188
if (!sd_data_ready(s->sd)) {
189
DPRINTF("Data read end\n");
190
s->mode = SSI_SD_CMD;
194
/* Should never happen. */
198
static void ssi_sd_save(QEMUFile *f, void *opaque)
200
SSISlave *ss = SSI_SLAVE(opaque);
201
ssi_sd_state *s = (ssi_sd_state *)opaque;
204
qemu_put_be32(f, s->mode);
205
qemu_put_be32(f, s->cmd);
206
for (i = 0; i < 4; i++)
207
qemu_put_be32(f, s->cmdarg[i]);
208
for (i = 0; i < 5; i++)
209
qemu_put_be32(f, s->response[i]);
210
qemu_put_be32(f, s->arglen);
211
qemu_put_be32(f, s->response_pos);
212
qemu_put_be32(f, s->stopping);
214
qemu_put_be32(f, ss->cs);
217
static int ssi_sd_load(QEMUFile *f, void *opaque, int version_id)
219
SSISlave *ss = SSI_SLAVE(opaque);
220
ssi_sd_state *s = (ssi_sd_state *)opaque;
226
s->mode = qemu_get_be32(f);
227
s->cmd = qemu_get_be32(f);
228
for (i = 0; i < 4; i++)
229
s->cmdarg[i] = qemu_get_be32(f);
230
for (i = 0; i < 5; i++)
231
s->response[i] = qemu_get_be32(f);
232
s->arglen = qemu_get_be32(f);
233
s->response_pos = qemu_get_be32(f);
234
s->stopping = qemu_get_be32(f);
236
ss->cs = qemu_get_be32(f);
241
static int ssi_sd_init(SSISlave *dev)
243
ssi_sd_state *s = FROM_SSI_SLAVE(ssi_sd_state, dev);
246
s->mode = SSI_SD_CMD;
247
dinfo = drive_get_next(IF_SD);
248
s->sd = sd_init(dinfo ? dinfo->bdrv : NULL, true);
252
register_savevm(&dev->qdev, "ssi_sd", -1, 1, ssi_sd_save, ssi_sd_load, s);
256
static void ssi_sd_class_init(ObjectClass *klass, void *data)
258
SSISlaveClass *k = SSI_SLAVE_CLASS(klass);
260
k->init = ssi_sd_init;
261
k->transfer = ssi_sd_transfer;
262
k->cs_polarity = SSI_CS_LOW;
265
static const TypeInfo ssi_sd_info = {
267
.parent = TYPE_SSI_SLAVE,
268
.instance_size = sizeof(ssi_sd_state),
269
.class_init = ssi_sd_class_init,
272
static void ssi_sd_register_types(void)
274
type_register_static(&ssi_sd_info);
277
type_init(ssi_sd_register_types)