hw/sd: Support block write in SPI mode
v3 cover hw/sd: Support block write in SPI mode
These are Bin's SD patches from his v2, rebased on sd-next.
v2:
https://www.mail-archive.com/[email protected]/msg775712.html
Based-on: <[email protected]>
Bin Meng (6):
hw/sd: ssi-sd: Support multiple block read
hw/sd: sd: Remove duplicated codes in single/multiple block read/write hw/sd: sd: Allow single/multiple block write for SPI mode hw/sd: Introduce receive_ready() callback
hw/sd: ssi-sd: Support single block write
hw/sd: ssi-sd: Support multiple block write
include/hw/sd/sd.h | 2 + hw/sd/core.c | 13 +++++ hw/sd/sd.c | 56 +++------------------- hw/sd/ssi-sd.c | 117 ++++++++++++++++++++++++++++++++++++++++----- 4 files changed, 125 insertions(+), 63 deletions(-)
Signature · 1 line
2.26.2
PATCH v3 1/6 hw/sd: ssi-sd: Support multiple block read
From: Bin Meng <[email protected]>
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.
Message-Id: <[email protected]>
--- hw/sd/ssi-sd.c | 42 +++++++++++++++++++++++++++++++++--------- 1 file changed, 33 insertions(+), 9 deletions(-)
hw/sd/ssi-sd.c +33 -9
@@ -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 ;
Signature · 1 line
2.26.2
PATCH v3 2/6 hw/sd: sd: Remove duplicated codes in single/multiple block read/write
In reply to Philippe Mathieu-DaudéFrom: Bin Meng <[email protected]>
The single block read (CMD17) codes are the same as the multiple block read (CMD18). Merge them into one. The same applies to single block write (CMD24) and multiple block write (CMD25).
Message-Id: <[email protected]>
--- hw/sd/sd.c | 47 ----------------------------------------------- 1 file changed, 47 deletions(-)
hw/sd/sd.c +0 -47
@@ -1181,24 +1181,6 @@ static sd_rsp_type_t sd_normal_command(SDState *sd, SDRequest req)break ;case 17 :/* CMD17: READ_SINGLE_BLOCK */ -switch (sd->state) { -case sd_transfer_state: - -if (addr + sd->blk_len > sd->size) { - sd->card_status |=ADDRESS_ERROR ; -return sd_r1; - } - - sd->state = sd_sendingdata_state; - sd->data_start = addr; - sd->data_offset =0 ; -return sd_r1; - -default : -break ; - } -break ; -case 18 :/* CMD18: READ_MULTIPLE_BLOCK */ switch (sd->state) {case sd_transfer_state:
@@ -1245,35 +1227,6 @@ static sd_rsp_type_t sd_normal_command(SDState *sd, SDRequest req)/* Block write commands (Class 4) */ case 24 :/* CMD24: WRITE_SINGLE_BLOCK */ -switch (sd->state) { -case sd_transfer_state: -/* Writing in SPI mode not implemented. */ -if (sd->spi) -break ; - -if (addr + sd->blk_len > sd->size) { - sd->card_status |=ADDRESS_ERROR ; -return sd_r1; - } - - sd->state = sd_receivingdata_state; - sd->data_start = addr; - sd->data_offset =0 ; - sd->blk_written =0 ; - -if (sd_wp_addr (sd, sd->data_start)) { - sd->card_status |=WP_VIOLATION ; - } -if (sd->csd[14 ] &0x30 ) { - sd->card_status |=WP_VIOLATION ; - } -return sd_r1; - -default : -break ; - } -break ; -case 25 :/* CMD25: WRITE_MULTIPLE_BLOCK */ switch (sd->state) {case sd_transfer_state:
Signature · 1 line
2.26.2
PATCH v3 3/6 hw/sd: sd: Allow single/multiple block write for SPI mode
In reply to Philippe Mathieu-DaudéFrom: Bin Meng <[email protected]>
At present the single/multiple block write in SPI mode is blocked by sd_normal_command(). Remove the limitation.
Message-Id: <[email protected]>
--- hw/sd/sd.c | 3 --- 1 file changed, 3 deletions(-)
hw/sd/sd.c +0 -3
@@ -1230,9 +1230,6 @@ static sd_rsp_type_t sd_normal_command(SDState *sd, SDRequest req)case 25 :/* CMD25: WRITE_MULTIPLE_BLOCK */ switch (sd->state) {case sd_transfer_state: -/* Writing in SPI mode not implemented. */ -if (sd->spi) -break ;if (addr + sd->blk_len > sd->size) { sd->card_status |=ADDRESS_ERROR ;
Signature · 1 line
2.26.2
PATCH v3 4/6 hw/sd: Introduce receive_ready() callback
In reply to Philippe Mathieu-DaudéFrom: Bin Meng <[email protected]>
At present there is a data_ready() callback for the SD data read path. Let's add a receive_ready() for the SD data write path.
Message-Id: <[email protected]>
--- include/hw/sd/sd.h | 2 ++ hw/sd/core.c | 13 +++++++++++++ hw/sd/sd.c | 6 ++++++ 3 files changed, 21 insertions(+)
include/hw/sd/sd.h +2 -0
@@ -116,6 +116,7 @@ struct SDCardClass {* Return: byte value read */ uint8_t (*read_byte)(SDState *sd); +bool (*receive_ready)(SDState *sd);bool (*data_ready)(SDState *sd);void (*set_voltage)(SDState *sd,uint16_t millivolts);uint8_t (*get_dat_lines)(SDState *sd);
@@ -187,6 +188,7 @@ void sdbus_write_data(SDBus *sdbus, const void *buf, size_t length);* Read multiple bytes of data on the data lines of a SD bus. */ void sdbus_read_data (SDBus *sdbus,void *buf,size_t length); +bool sdbus_receive_ready (SDBus *sd);bool sdbus_data_ready (SDBus *sd);bool sdbus_get_inserted (SDBus *sd);bool sdbus_get_readonly (SDBus *sd);
hw/sd/core.c +13 -0
@@ -160,6 +160,19 @@ void sdbus_read_data(SDBus *sdbus, void *buf, size_t length) } } +bool sdbus_receive_ready (SDBus *sdbus) +{ +SDState *card =get_card (sdbus); + +if (card) { +SDCardClass *sc =SD_CARD_GET_CLASS (card); + +return sc->receive_ready (card); + } + +return false; +} +bool sdbus_data_ready (SDBus *sdbus) {SDState *card =get_card (sdbus);
hw/sd/sd.c +6 -0
@@ -2037,6 +2037,11 @@ uint8_t sd_read_byte(SDState *sd)return ret; } +static bool sd_receive_ready (SDState *sd) +{ +return sd->state == sd_receivingdata_state; +} +static bool sd_data_ready (SDState *sd) {return sd->state == sd_sendingdata_state;
@@ -2147,6 +2152,7 @@ static void sd_class_init(ObjectClass *klass, void *data) sc->do_command = sd_do_command; sc->write_byte = sd_write_byte; sc->read_byte = sd_read_byte; + sc->receive_ready = sd_receive_ready; sc->data_ready = sd_data_ready; sc->enable = sd_enable; sc->get_inserted = sd_get_inserted;
Signature · 1 line
2.26.2
PATCH v3 5/6 hw/sd: ssi-sd: Support single block write
In reply to Philippe Mathieu-DaudéFrom: Bin Meng <[email protected]>
Add 2 more states for the block write operation. The SPI host needs to send a data start token to start the transfer, and the data block written to the card will be acknowledged by a data response token.
Message-Id: <[email protected]>
--- hw/sd/ssi-sd.c | 44 ++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 40 insertions(+), 4 deletions(-)
hw/sd/ssi-sd.c +40 -4
@@ -43,6 +43,8 @@ typedef enum {SSI_SD_DATA_START ,SSI_SD_DATA_READ ,SSI_SD_DATA_CRC16 , +SSI_SD_DATA_WRITE , +SSI_SD_SKIP_CRC16 , } ssi_sd_mode; struct ssi_sd_state {
@@ -53,6 +55,7 @@ struct ssi_sd_state {uint8_t response[5 ];uint16_t crc16;int32_t read_bytes; +int32_t write_bytes;int32_t arglen;int32_t response_pos;int32_t stopping;
@@ -85,6 +88,9 @@ OBJECT_DECLARE_SIMPLE_TYPE(ssi_sd_state, SSI_SD)/* dummy value - don't care */ #define SSI_DUMMY 0xff +/* data accepted */ +#define DATA_RESPONSE_ACCEPTED 0x05 +static uint32_t ssi_sd_transfer (SSIPeripheral *dev,uint32_t val) {ssi_sd_state *s =SSI_SD (dev);
@@ -113,10 +119,17 @@ static uint32_t ssi_sd_transfer(SSIPeripheral *dev, uint32_t val)switch (s->mode) {case SSI_SD_CMD : -if (val ==SSI_DUMMY ) { +switch (val) { +case SSI_DUMMY :DPRINTF ("NULL command\n" );return SSI_DUMMY ; +break ; +case SSI_TOKEN_SINGLE : +DPRINTF ("Start write block\n" ); + s->mode =SSI_SD_DATA_WRITE ; +return SSI_DUMMY ; } + s->cmd = val &0x3f ; s->mode =SSI_SD_CMDARG ; s->arglen =0 ;
@@ -250,6 +263,27 @@ static uint32_t ssi_sd_transfer(SSIPeripheral *dev, uint32_t val) s->response_pos =0 ; }return val; +case SSI_SD_DATA_WRITE : +sdbus_write_byte (&s->sdbus, val); + s->write_bytes++; +if (!sdbus_receive_ready (&s->sdbus) || s->write_bytes ==512 ) { +DPRINTF ("Data write end\n" ); + s->mode =SSI_SD_SKIP_CRC16 ; + s->response_pos =0 ; + } +return val; +case SSI_SD_SKIP_CRC16 : +/* we don't verify the crc16 */ + s->response_pos++; +if (s->response_pos ==2 ) { +DPRINTF ("CRC16 receive end\n" ); + s->mode =SSI_SD_RESPONSE ; + s->write_bytes =0 ; + s->arglen =1 ; + s->response[0 ] =DATA_RESPONSE_ACCEPTED ; + s->response_pos =0 ; + } +return SSI_DUMMY ; }/* Should never happen. */ return SSI_DUMMY ;
@@ -259,7 +293,7 @@ static int ssi_sd_post_load(void *opaque, int version_id) {ssi_sd_state *s = (ssi_sd_state *)opaque; -if (s->mode >SSI_SD_DATA_CRC16 ) { +if (s->mode >SSI_SD_SKIP_CRC16 ) {return -EINVAL ; } if (s->mode == SSI_SD_CMDARG &&
@@ -277,8 +311,8 @@ static int ssi_sd_post_load(void *opaque, int version_id)static const VMStateDescription vmstate_ssi_sd = { .name ="ssi_sd" , - .version_id =6 , - .minimum_version_id =6 , + .version_id =7 , + .minimum_version_id =7 , .post_load = ssi_sd_post_load, .fields = (VMStateField []) {VMSTATE_UINT32 (mode, ssi_sd_state),
@@ -287,6 +321,7 @@ static const VMStateDescription vmstate_ssi_sd = {VMSTATE_UINT8_ARRAY (response, ssi_sd_state,5 ),VMSTATE_UINT16 (crc16, ssi_sd_state),VMSTATE_INT32 (read_bytes, ssi_sd_state), +VMSTATE_INT32 (write_bytes, ssi_sd_state),VMSTATE_INT32 (arglen, ssi_sd_state),VMSTATE_INT32 (response_pos, ssi_sd_state), VMSTATE_INT32(stopping, ssi_sd_state),
@@ -340,6 +375,7 @@ static void ssi_sd_reset(DeviceState *dev)memset (s->response,0 ,sizeof (s->response)); s->crc16 =0 ; s->read_bytes =0 ; + s->write_bytes =0 ; s->arglen =0 ; s->response_pos =0 ; s->stopping =0 ;
Signature · 1 line
2.26.2
PATCH v3 6/6 hw/sd: ssi-sd: Support multiple block write
In reply to Philippe Mathieu-DaudéFrom: Bin Meng <[email protected]>
For a multiple block write operation, each block begins with a multi write start token. Unlike the SD mode that the multiple block write ends when receiving a STOP_TRAN command (CMD12), a special stop tran token is used to signal the card.
Emulating this by manually sending a CMD12 to the SD card core, to bring it out of the receiving data state.
Message-Id: <[email protected]>
--- hw/sd/ssi-sd.c | 35 +++++++++++++++++++++++++++++++++-- 1 file changed, 33 insertions(+), 2 deletions(-)
hw/sd/ssi-sd.c +33 -2
@@ -4,6 +4,11 @@* Copyright (c) 2007-2009 CodeSourcery. * Written by Paul Brook * +* Copyright (c) 2021 Wind River Systems, Inc. +* Improved by Bin Meng <[email protected]> +* +* Validated with U-Boot v2021.01 and Linux v5.10 mmc_spi driver +* * This code is licensed under the GNU GPL v2. * * Contributions after 2012-01-13 are licensed under the terms of the
@@ -82,6 +87,10 @@ OBJECT_DECLARE_SIMPLE_TYPE(ssi_sd_state, SSI_SD)#define SSI_SDR_ADDRESS_ERROR 0x2000#define SSI_SDR_PARAMETER_ERROR 0x4000 +/* multiple block write */ +#define SSI_TOKEN_MULTI_WRITE 0xfc +/* terminate multiple block write */ +#define SSI_TOKEN_STOP_TRAN 0xfd/* single block read/write, multiple block read */ #define SSI_TOKEN_SINGLE 0xfe
@@ -94,6 +103,8 @@ OBJECT_DECLARE_SIMPLE_TYPE(ssi_sd_state, SSI_SD)static uint32_t ssi_sd_transfer (SSIPeripheral *dev,uint32_t val) {ssi_sd_state *s =SSI_SD (dev); +SDRequest request; +uint8_t longresp[16 ];/* * Special case: allow CMD12 (STOP TRANSMISSION) while reading data.
@@ -125,9 +136,31 @@ static uint32_t ssi_sd_transfer(SSIPeripheral *dev, uint32_t val)return SSI_DUMMY ;break ;case SSI_TOKEN_SINGLE : +case SSI_TOKEN_MULTI_WRITE :DPRINTF ("Start write block\n" ); s->mode =SSI_SD_DATA_WRITE ;return SSI_DUMMY ; +case SSI_TOKEN_STOP_TRAN : +DPRINTF ("Stop multiple write\n" ); + +/* manually issue cmd12 to stop the transfer */ + request.cmd =12 ; + request.arg =0 ; + s->arglen =sdbus_do_command (&s->sdbus, &request, longresp); +if (s->arglen <=0 ) { + s->arglen =1 ; +/* a zero value indicates the card is busy */ + s->response[0 ] =0 ; +DPRINTF ("SD card busy\n" ); + }else { + s->arglen =1 ; +/* a non-zero value indicates the card is ready */ + s->response[0 ] =SSI_DUMMY ; + } + + s->mode =SSI_SD_RESPONSE ; + s->response_pos =0 ; +return SSI_DUMMY ; } s->cmd = val &0x3f ;
@@ -136,8 +169,6 @@ static uint32_t ssi_sd_transfer(SSIPeripheral *dev, uint32_t val)return SSI_DUMMY ;case SSI_SD_CMDARG :if (s->arglen ==4 ) { -SDRequest request; -uint8_t longresp[16 ];/* FIXME: Check CRC. */ request.cmd = s->cmd; request.arg =ldl_be_p (s->cmdarg);
Signature · 1 line
2.26.2
Quoted 2 lines · Philippe Mathieu-Daudé
These are Bin's SD patches from his v2, rebased on sd-next.
Looks good to me. Thanks!
Quoted 14 lines
v2:
https://www.mail-archive.com/[email protected]/msg775712.htmlBased-on: <[email protected]>
Bin Meng (6):
hw/sd: ssi-sd: Support multiple block read
hw/sd: sd: Remove duplicated codes in single/multiple block read/write hw/sd: sd: Allow single/multiple block write for SPI mode hw/sd: Introducereceive_ready()callback
hw/sd: ssi-sd: Support single block write
hw/sd: ssi-sd: Support multiple block write
Quoted 6 lines
include/hw/sd/sd.h | 2 + hw/sd/core.c | 13 +++++ hw/sd/sd.c | 56 +++------------------- hw/sd/ssi-sd.c | 117 ++++++++++++++++++++++++++++++++++++++++----- 4 files changed, 125 insertions(+), 63 deletions(-)
Regards,
Bin
Quoted 15 lines · Philippe Mathieu-Daudé
These are Bin's SD patches from his v2, rebased on sd-next.
v2:
https://www.mail-archive.com/[email protected]/msg775712.htmlBased-on: <[email protected]>
Bin Meng (6):
hw/sd: ssi-sd: Support multiple block read
hw/sd: sd: Remove duplicated codes in single/multiple block read/write hw/sd: sd: Allow single/multiple block write for SPI mode hw/sd: Introducereceive_ready()callback
hw/sd: ssi-sd: Support single block write
hw/sd: ssi-sd: Support multiple block write
Quoted 5 lines
include/hw/sd/sd.h | 2 + hw/sd/core.c | 13 +++++ hw/sd/sd.c | 56 +++------------------- hw/sd/ssi-sd.c | 117 ++++++++++++++++++++++++++++++++++++++++----- 4 files changed, 125 insertions(+), 63 deletions(-)