[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[PULL 01/18] hw/sd: ssi-sd: Support multiple block read
From: |
Philippe Mathieu-Daudé |
Subject: |
[PULL 01/18] hw/sd: ssi-sd: Support multiple block read |
Date: |
Sat, 20 Feb 2021 01:12:52 +0100 |
From: Bin Meng <bin.meng@windriver.com>
In the case of a multiple block read operation every transferred
block has its suffix of CRC16. Update the state machine logic to
handle multiple block read.
Signed-off-by: Bin Meng <bin.meng@windriver.com>
[PMD: Change VMState version id 5 -> 6]
Signed-off-by: Philippe Mathieu-Daudé <f4bug@amsat.org>
Tested-by: Philippe Mathieu-Daudé <f4bug@amsat.org>
Acked-by: Alistair Francis <alistair.francis@wdc.com>
Message-Id: <20210128063035.15674-2-bmeng.cn@gmail.com>
Signed-off-by: Philippe Mathieu-Daudé <f4bug@amsat.org>
---
hw/sd/ssi-sd.c | 42 +++++++++++++++++++++++++++++++++---------
1 file changed, 33 insertions(+), 9 deletions(-)
diff --git a/hw/sd/ssi-sd.c b/hw/sd/ssi-sd.c
index be1bb101645..6d20a240c69 100644
--- a/hw/sd/ssi-sd.c
+++ b/hw/sd/ssi-sd.c
@@ -52,6 +52,7 @@ struct ssi_sd_state {
uint8_t cmdarg[4];
uint8_t response[5];
uint16_t crc16;
+ int32_t read_bytes;
int32_t arglen;
int32_t response_pos;
int32_t stopping;
@@ -88,11 +89,26 @@ static uint32_t ssi_sd_transfer(SSIPeripheral *dev,
uint32_t val)
{
ssi_sd_state *s = SSI_SD(dev);
- /* Special case: allow CMD12 (STOP TRANSMISSION) while reading data. */
- if (s->mode == SSI_SD_DATA_READ && val == 0x4c) {
- s->mode = SSI_SD_CMD;
- /* There must be at least one byte delay before the card responds. */
- s->stopping = 1;
+ /*
+ * Special case: allow CMD12 (STOP TRANSMISSION) while reading data.
+ *
+ * See "Physical Layer Specification Version 8.00" chapter 7.5.2.2,
+ * to avoid conflict between CMD12 response and next data block,
+ * timing of CMD12 should be controlled as follows:
+ *
+ * - CMD12 issued at the timing that end bit of CMD12 and end bit of
+ * data block is overlapped
+ * - CMD12 issued after one clock cycle after host receives a token
+ * (either Start Block token or Data Error token)
+ *
+ * We need to catch CMD12 in all of the data read states.
+ */
+ if (s->mode >= SSI_SD_PREP_DATA && s->mode <= SSI_SD_DATA_CRC16) {
+ if (val == 0x4c) {
+ s->mode = SSI_SD_CMD;
+ /* There must be at least one byte delay before the card responds
*/
+ s->stopping = 1;
+ }
}
switch (s->mode) {
@@ -212,8 +228,9 @@ static uint32_t ssi_sd_transfer(SSIPeripheral *dev,
uint32_t val)
return SSI_TOKEN_SINGLE;
case SSI_SD_DATA_READ:
val = sdbus_read_byte(&s->sdbus);
+ s->read_bytes++;
s->crc16 = crc_ccitt_false(s->crc16, (uint8_t *)&val, 1);
- if (!sdbus_data_ready(&s->sdbus)) {
+ if (!sdbus_data_ready(&s->sdbus) || s->read_bytes == 512) {
DPRINTF("Data read end\n");
s->mode = SSI_SD_DATA_CRC16;
}
@@ -224,7 +241,12 @@ static uint32_t ssi_sd_transfer(SSIPeripheral *dev,
uint32_t val)
s->response_pos++;
if (s->response_pos == 2) {
DPRINTF("CRC16 read end\n");
- s->mode = SSI_SD_CMD;
+ if (s->read_bytes == 512 && s->cmd != 17) {
+ s->mode = SSI_SD_PREP_DATA;
+ } else {
+ s->mode = SSI_SD_CMD;
+ }
+ s->read_bytes = 0;
s->response_pos = 0;
}
return val;
@@ -255,8 +277,8 @@ static int ssi_sd_post_load(void *opaque, int version_id)
static const VMStateDescription vmstate_ssi_sd = {
.name = "ssi_sd",
- .version_id = 5,
- .minimum_version_id = 5,
+ .version_id = 6,
+ .minimum_version_id = 6,
.post_load = ssi_sd_post_load,
.fields = (VMStateField []) {
VMSTATE_UINT32(mode, ssi_sd_state),
@@ -264,6 +286,7 @@ static const VMStateDescription vmstate_ssi_sd = {
VMSTATE_UINT8_ARRAY(cmdarg, ssi_sd_state, 4),
VMSTATE_UINT8_ARRAY(response, ssi_sd_state, 5),
VMSTATE_UINT16(crc16, ssi_sd_state),
+ VMSTATE_INT32(read_bytes, ssi_sd_state),
VMSTATE_INT32(arglen, ssi_sd_state),
VMSTATE_INT32(response_pos, ssi_sd_state),
VMSTATE_INT32(stopping, ssi_sd_state),
@@ -316,6 +339,7 @@ static void ssi_sd_reset(DeviceState *dev)
memset(s->cmdarg, 0, sizeof(s->cmdarg));
memset(s->response, 0, sizeof(s->response));
s->crc16 = 0;
+ s->read_bytes = 0;
s->arglen = 0;
s->response_pos = 0;
s->stopping = 0;
--
2.26.2
- [PULL 00/18] SD/MMC patches for 2021-02-20, Philippe Mathieu-Daudé, 2021/02/19
- [PULL 01/18] hw/sd: ssi-sd: Support multiple block read,
Philippe Mathieu-Daudé <=
- [PULL 02/18] hw/sd: sd: Remove duplicated codes in single/multiple block read/write, Philippe Mathieu-Daudé, 2021/02/19
- [PULL 03/18] hw/sd: sd: Allow single/multiple block write for SPI mode, Philippe Mathieu-Daudé, 2021/02/19
- [PULL 04/18] hw/sd: Introduce receive_ready() callback, Philippe Mathieu-Daudé, 2021/02/19
- [PULL 05/18] hw/sd: ssi-sd: Support single block write, Philippe Mathieu-Daudé, 2021/02/19
- [PULL 06/18] hw/sd: ssi-sd: Support multiple block write, Philippe Mathieu-Daudé, 2021/02/19
- [PULL 07/18] hw/sd: ssi-sd: Fix SEND_IF_COND (CMD8) response, Philippe Mathieu-Daudé, 2021/02/19
- [PULL 08/18] hw/sd: ssi-sd: Fix STOP_TRANSMISSION (CMD12) response, Philippe Mathieu-Daudé, 2021/02/19
- [PULL 09/18] hw/sd: ssi-sd: Handle the rest commands with R1b response type, Philippe Mathieu-Daudé, 2021/02/19
- [PULL 10/18] hw/sd: sd: Fix address check in sd_erase(), Philippe Mathieu-Daudé, 2021/02/19
- [PULL 11/18] hw/sd: sd: Only SDSC cards support CMD28/29/30, Philippe Mathieu-Daudé, 2021/02/19