2 * SSI to SD card adapter.
4 * Copyright (c) 2007-2009 CodeSourcery.
5 * Written by Paul Brook
7 * This code is licenced under the GNU GPL v2.
13 //#define DEBUG_SSI_SD 1
16 #define DPRINTF(fmt, ...) \
17 do { printf("ssi_sd: " fmt , ## __VA_ARGS__); } while (0)
18 #define BADF(fmt, ...) \
19 do { fprintf(stderr, "ssi_sd: error: " fmt , ## __VA_ARGS__); exit(1);} while (0)
21 #define DPRINTF(fmt, ...) do {} while(0)
22 #define BADF(fmt, ...) \
23 do { fprintf(stderr, "ssi_sd: error: " fmt , ## __VA_ARGS__);} while (0)
46 /* State word bits. */
47 #define SSI_SDR_LOCKED 0x0001
48 #define SSI_SDR_WP_ERASE 0x0002
49 #define SSI_SDR_ERROR 0x0004
50 #define SSI_SDR_CC_ERROR 0x0008
51 #define SSI_SDR_ECC_FAILED 0x0010
52 #define SSI_SDR_WP_VIOLATION 0x0020
53 #define SSI_SDR_ERASE_PARAM 0x0040
54 #define SSI_SDR_OUT_OF_RANGE 0x0080
55 #define SSI_SDR_IDLE 0x0100
56 #define SSI_SDR_ERASE_RESET 0x0200
57 #define SSI_SDR_ILLEGAL_COMMAND 0x0400
58 #define SSI_SDR_COM_CRC_ERROR 0x0800
59 #define SSI_SDR_ERASE_SEQ_ERROR 0x1000
60 #define SSI_SDR_ADDRESS_ERROR 0x2000
61 #define SSI_SDR_PARAMETER_ERROR 0x4000
63 static uint32_t ssi_sd_transfer(SSISlave
*dev
, uint32_t val
)
65 ssi_sd_state
*s
= FROM_SSI_SLAVE(ssi_sd_state
, dev
);
67 /* Special case: allow CMD12 (STOP TRANSMISSION) while reading data. */
68 if (s
->mode
== SSI_SD_DATA_READ
&& val
== 0x4d) {
70 /* There must be at least one byte delay before the card responds. */
77 DPRINTF("NULL command\n");
81 s
->mode
= SSI_SD_CMDARG
;
88 /* FIXME: Check CRC. */
90 request
.arg
= (s
->cmdarg
[0] << 24) | (s
->cmdarg
[1] << 16)
91 | (s
->cmdarg
[2] << 8) | s
->cmdarg
[3];
92 DPRINTF("CMD%d arg 0x%08x\n", s
->cmd
, request
.arg
);
93 s
->arglen
= sd_do_command(s
->sd
, &request
, longresp
);
97 DPRINTF("SD command failed\n");
98 } else if (s
->cmd
== 58) {
99 /* CMD58 returns R3 response (OCR) */
100 DPRINTF("Returned OCR\n");
103 memcpy(&s
->response
[1], longresp
, 4);
104 } else if (s
->arglen
!= 4) {
105 BADF("Unexpected response to cmd %d\n", s
->cmd
);
106 /* Illegal command is about as near as we can get. */
110 /* All other commands return status. */
113 /* CMD13 returns a 2-byte statuse work. Other commands
114 only return the first byte. */
115 s
->arglen
= (s
->cmd
== 13) ? 2 : 1;
116 cardstatus
= (longresp
[0] << 24) | (longresp
[1] << 16)
117 | (longresp
[2] << 8) | longresp
[3];
119 if (((cardstatus
>> 9) & 0xf) < 4)
120 status
|= SSI_SDR_IDLE
;
121 if (cardstatus
& ERASE_RESET
)
122 status
|= SSI_SDR_ERASE_RESET
;
123 if (cardstatus
& ILLEGAL_COMMAND
)
124 status
|= SSI_SDR_ILLEGAL_COMMAND
;
125 if (cardstatus
& COM_CRC_ERROR
)
126 status
|= SSI_SDR_COM_CRC_ERROR
;
127 if (cardstatus
& ERASE_SEQ_ERROR
)
128 status
|= SSI_SDR_ERASE_SEQ_ERROR
;
129 if (cardstatus
& ADDRESS_ERROR
)
130 status
|= SSI_SDR_ADDRESS_ERROR
;
131 if (cardstatus
& CARD_IS_LOCKED
)
132 status
|= SSI_SDR_LOCKED
;
133 if (cardstatus
& (LOCK_UNLOCK_FAILED
| WP_ERASE_SKIP
))
134 status
|= SSI_SDR_WP_ERASE
;
135 if (cardstatus
& SD_ERROR
)
136 status
|= SSI_SDR_ERROR
;
137 if (cardstatus
& CC_ERROR
)
138 status
|= SSI_SDR_CC_ERROR
;
139 if (cardstatus
& CARD_ECC_FAILED
)
140 status
|= SSI_SDR_ECC_FAILED
;
141 if (cardstatus
& WP_VIOLATION
)
142 status
|= SSI_SDR_WP_VIOLATION
;
143 if (cardstatus
& ERASE_PARAM
)
144 status
|= SSI_SDR_ERASE_PARAM
;
145 if (cardstatus
& (OUT_OF_RANGE
| CID_CSD_OVERWRITE
))
146 status
|= SSI_SDR_OUT_OF_RANGE
;
147 /* ??? Don't know what Parameter Error really means, so
148 assume it's set if the second byte is nonzero. */
150 status
|= SSI_SDR_PARAMETER_ERROR
;
151 s
->response
[0] = status
>> 8;
152 s
->response
[1] = status
;
153 DPRINTF("Card status 0x%02x\n", status
);
155 s
->mode
= SSI_SD_RESPONSE
;
158 s
->cmdarg
[s
->arglen
++] = val
;
161 case SSI_SD_RESPONSE
:
166 if (s
->response_pos
< s
->arglen
) {
167 DPRINTF("Response 0x%02x\n", s
->response
[s
->response_pos
]);
168 return s
->response
[s
->response_pos
++];
170 if (sd_data_ready(s
->sd
)) {
171 DPRINTF("Data read\n");
172 s
->mode
= SSI_SD_DATA_START
;
174 DPRINTF("End of command\n");
175 s
->mode
= SSI_SD_CMD
;
178 case SSI_SD_DATA_START
:
179 DPRINTF("Start read block\n");
180 s
->mode
= SSI_SD_DATA_READ
;
182 case SSI_SD_DATA_READ
:
183 val
= sd_read_data(s
->sd
);
184 if (!sd_data_ready(s
->sd
)) {
185 DPRINTF("Data read end\n");
186 s
->mode
= SSI_SD_CMD
;
190 /* Should never happen. */
194 static void ssi_sd_save(QEMUFile
*f
, void *opaque
)
196 ssi_sd_state
*s
= (ssi_sd_state
*)opaque
;
199 qemu_put_be32(f
, s
->mode
);
200 qemu_put_be32(f
, s
->cmd
);
201 for (i
= 0; i
< 4; i
++)
202 qemu_put_be32(f
, s
->cmdarg
[i
]);
203 for (i
= 0; i
< 5; i
++)
204 qemu_put_be32(f
, s
->response
[i
]);
205 qemu_put_be32(f
, s
->arglen
);
206 qemu_put_be32(f
, s
->response_pos
);
207 qemu_put_be32(f
, s
->stopping
);
210 static int ssi_sd_load(QEMUFile
*f
, void *opaque
, int version_id
)
212 ssi_sd_state
*s
= (ssi_sd_state
*)opaque
;
218 s
->mode
= qemu_get_be32(f
);
219 s
->cmd
= qemu_get_be32(f
);
220 for (i
= 0; i
< 4; i
++)
221 s
->cmdarg
[i
] = qemu_get_be32(f
);
222 for (i
= 0; i
< 5; i
++)
223 s
->response
[i
] = qemu_get_be32(f
);
224 s
->arglen
= qemu_get_be32(f
);
225 s
->response_pos
= qemu_get_be32(f
);
226 s
->stopping
= qemu_get_be32(f
);
231 static int ssi_sd_init(SSISlave
*dev
)
233 ssi_sd_state
*s
= FROM_SSI_SLAVE(ssi_sd_state
, dev
);
234 BlockDriverState
*bs
;
236 s
->mode
= SSI_SD_CMD
;
237 bs
= qdev_init_bdrv(&dev
->qdev
, IF_SD
);
238 s
->sd
= sd_init(bs
, 1);
239 register_savevm(&dev
->qdev
, "ssi_sd", -1, 1, ssi_sd_save
, ssi_sd_load
, s
);
243 static SSISlaveInfo ssi_sd_info
= {
244 .qdev
.name
= "ssi-sd",
245 .qdev
.size
= sizeof(ssi_sd_state
),
247 .transfer
= ssi_sd_transfer
250 static void ssi_sd_register_devices(void)
252 ssi_register_slave(&ssi_sd_info
);
255 device_init(ssi_sd_register_devices
)