* [PATCH 01/11] hw/sd/sdcard: Introduce sd_cmd_to_sendingdata and sd_generic_read_byte
2024-06-25 6:10 [PATCH 00/11] hw/sd/sd: Introduce sd_cmd_to_sendingdata() and sd_generic_read_byte() Philippe Mathieu-Daudé
@ 2024-06-25 6:10 ` Philippe Mathieu-Daudé
2024-06-25 6:10 ` [PATCH 02/11] hw/sd/sdcard: Convert SWITCH_FUNCTION to generic_read_byte (CMD6) Philippe Mathieu-Daudé
` (9 subsequent siblings)
10 siblings, 0 replies; 13+ messages in thread
From: Philippe Mathieu-Daudé @ 2024-06-25 6:10 UTC (permalink / raw)
To: qemu-devel
Cc: Cédric Le Goater, qemu-block, Bin Meng,
Philippe Mathieu-Daudé
All commands switching from TRANSFER state to (sending)DATA
do the same: send stream of data on the DAT lines. Instead
of duplicating the same code many times, introduce 2 helpers:
- sd_cmd_to_sendingdata() on the I/O line setup the data to
be transferred,
- sd_generic_read_byte() on the DAT lines to fetch the data.
Signed-off-by: Philippe Mathieu-Daudé <philmd@linaro.org>
---
hw/sd/sd.c | 38 ++++++++++++++++++++++++++++++++++++++
1 file changed, 38 insertions(+)
diff --git a/hw/sd/sd.c b/hw/sd/sd.c
index 6685fba4bb..89006ba1b6 100644
--- a/hw/sd/sd.c
+++ b/hw/sd/sd.c
@@ -142,8 +142,10 @@ struct SDState {
*/
bool expecting_acmd;
uint32_t blk_written;
+
uint64_t data_start;
uint32_t data_offset;
+ size_t data_size;
uint8_t data[512];
uint8_t vendor_data[512];
@@ -1064,6 +1066,28 @@ static sd_rsp_type_t sd_cmd_unimplemented(SDState *sd, SDRequest req)
return sd_illegal;
}
+__attribute__((unused))
+static sd_rsp_type_t sd_cmd_to_sendingdata(SDState *sd, SDRequest req,
+ uint64_t start,
+ const void *data, size_t size)
+{
+ if (sd->state != sd_transfer_state) {
+ sd_invalid_state_for_cmd(sd, req);
+ }
+
+ sd->state = sd_sendingdata_state;
+ sd->data_start = start;
+ sd->data_offset = 0;
+ if (data) {
+ assert(size);
+ memcpy(sd->data, data, size);
+ }
+ if (size) {
+ sd->data_size = size;
+ }
+ return sd_r1;
+}
+
/* CMD0 */
static sd_rsp_type_t sd_cmd_GO_IDLE_STATE(SDState *sd, SDRequest req)
{
@@ -1898,6 +1922,20 @@ send_response:
return rsplen;
}
+__attribute__((unused))
+/* Return true when buffer consumed */
+static bool sd_generic_read_byte(SDState *sd, uint8_t *value)
+{
+ *value = sd->data[sd->data_offset];
+
+ if (++sd->data_offset >= sd->data_size) {
+ sd->state = sd_transfer_state;
+ return true;
+ }
+
+ return false;
+}
+
void sd_write_byte(SDState *sd, uint8_t value)
{
int i;
--
2.41.0
^ permalink raw reply related [flat|nested] 13+ messages in thread
* [PATCH 02/11] hw/sd/sdcard: Convert SWITCH_FUNCTION to generic_read_byte (CMD6)
2024-06-25 6:10 [PATCH 00/11] hw/sd/sd: Introduce sd_cmd_to_sendingdata() and sd_generic_read_byte() Philippe Mathieu-Daudé
2024-06-25 6:10 ` [PATCH 01/11] hw/sd/sdcard: Introduce sd_cmd_to_sendingdata and sd_generic_read_byte Philippe Mathieu-Daudé
@ 2024-06-25 6:10 ` Philippe Mathieu-Daudé
2024-06-25 6:14 ` Philippe Mathieu-Daudé
2024-06-25 6:10 ` [PATCH 03/11] hw/sd/sdcard: Convert SEND_CSD/SEND_CID to generic_read_byte (CMD9 & 10) Philippe Mathieu-Daudé
` (8 subsequent siblings)
10 siblings, 1 reply; 13+ messages in thread
From: Philippe Mathieu-Daudé @ 2024-06-25 6:10 UTC (permalink / raw)
To: qemu-devel
Cc: Cédric Le Goater, qemu-block, Bin Meng,
Philippe Mathieu-Daudé
Signed-off-by: Philippe Mathieu-Daudé <philmd@linaro.org>
---
hw/sd/sd.c | 11 ++---------
1 file changed, 2 insertions(+), 9 deletions(-)
diff --git a/hw/sd/sd.c b/hw/sd/sd.c
index 89006ba1b6..0011aa86da 100644
--- a/hw/sd/sd.c
+++ b/hw/sd/sd.c
@@ -1220,10 +1220,7 @@ static sd_rsp_type_t sd_normal_command(SDState *sd, SDRequest req)
}
sd_function_switch(sd, req.arg);
- sd->state = sd_sendingdata_state;
- sd->data_start = 0;
- sd->data_offset = 0;
- return sd_r1;
+ return sd_cmd_to_sendingdata(sd, req, 0, NULL, 64);
case 7: /* CMD7: SELECT/DESELECT_CARD */
rca = sd_req_get_rca(sd, req);
@@ -1922,7 +1919,6 @@ send_response:
return rsplen;
}
-__attribute__((unused))
/* Return true when buffer consumed */
static bool sd_generic_read_byte(SDState *sd, uint8_t *value)
{
@@ -2112,10 +2108,7 @@ uint8_t sd_read_byte(SDState *sd)
sd->current_cmd, sd->data_offset, io_len);
switch (sd->current_cmd) {
case 6: /* CMD6: SWITCH_FUNCTION */
- ret = sd->data[sd->data_offset ++];
-
- if (sd->data_offset >= 64)
- sd->state = sd_transfer_state;
+ sd_generic_read_byte(sd, &ret);
break;
case 9: /* CMD9: SEND_CSD */
--
2.41.0
^ permalink raw reply related [flat|nested] 13+ messages in thread
* Re: [PATCH 02/11] hw/sd/sdcard: Convert SWITCH_FUNCTION to generic_read_byte (CMD6)
2024-06-25 6:10 ` [PATCH 02/11] hw/sd/sdcard: Convert SWITCH_FUNCTION to generic_read_byte (CMD6) Philippe Mathieu-Daudé
@ 2024-06-25 6:14 ` Philippe Mathieu-Daudé
0 siblings, 0 replies; 13+ messages in thread
From: Philippe Mathieu-Daudé @ 2024-06-25 6:14 UTC (permalink / raw)
To: qemu-devel; +Cc: Cédric Le Goater, qemu-block, Bin Meng
On 25/6/24 08:10, Philippe Mathieu-Daudé wrote:
> Signed-off-by: Philippe Mathieu-Daudé <philmd@linaro.org>
> ---
> hw/sd/sd.c | 11 ++---------
> 1 file changed, 2 insertions(+), 9 deletions(-)
>
> diff --git a/hw/sd/sd.c b/hw/sd/sd.c
> index 89006ba1b6..0011aa86da 100644
> --- a/hw/sd/sd.c
> +++ b/hw/sd/sd.c
> @@ -1220,10 +1220,7 @@ static sd_rsp_type_t sd_normal_command(SDState *sd, SDRequest req)
> }
>
> sd_function_switch(sd, req.arg);
> - sd->state = sd_sendingdata_state;
> - sd->data_start = 0;
> - sd->data_offset = 0;
> - return sd_r1;
> + return sd_cmd_to_sendingdata(sd, req, 0, NULL, 64);
>
> case 7: /* CMD7: SELECT/DESELECT_CARD */
> rca = sd_req_get_rca(sd, req);
Oops, missing:
-- >8 --
@@ -1068,3 +1068,2 @@ static sd_rsp_type_t sd_cmd_unimplemented(SDState
*sd, SDRequest req)
-__attribute__((unused))
static sd_rsp_type_t sd_cmd_to_sendingdata(SDState *sd, SDRequest req,
---
> @@ -1922,7 +1919,6 @@ send_response:
> return rsplen;
> }
>
> -__attribute__((unused))
> /* Return true when buffer consumed */
> static bool sd_generic_read_byte(SDState *sd, uint8_t *value)
> {
> @@ -2112,10 +2108,7 @@ uint8_t sd_read_byte(SDState *sd)
> sd->current_cmd, sd->data_offset, io_len);
> switch (sd->current_cmd) {
> case 6: /* CMD6: SWITCH_FUNCTION */
> - ret = sd->data[sd->data_offset ++];
> -
> - if (sd->data_offset >= 64)
> - sd->state = sd_transfer_state;
> + sd_generic_read_byte(sd, &ret);
> break;
>
> case 9: /* CMD9: SEND_CSD */
^ permalink raw reply [flat|nested] 13+ messages in thread
* [PATCH 03/11] hw/sd/sdcard: Convert SEND_CSD/SEND_CID to generic_read_byte (CMD9 & 10)
2024-06-25 6:10 [PATCH 00/11] hw/sd/sd: Introduce sd_cmd_to_sendingdata() and sd_generic_read_byte() Philippe Mathieu-Daudé
2024-06-25 6:10 ` [PATCH 01/11] hw/sd/sdcard: Introduce sd_cmd_to_sendingdata and sd_generic_read_byte Philippe Mathieu-Daudé
2024-06-25 6:10 ` [PATCH 02/11] hw/sd/sdcard: Convert SWITCH_FUNCTION to generic_read_byte (CMD6) Philippe Mathieu-Daudé
@ 2024-06-25 6:10 ` Philippe Mathieu-Daudé
2024-06-25 6:10 ` [PATCH 04/11] hw/sd/sdcard: Duplicate READ_SINGLE_BLOCK / READ_MULTIPLE_BLOCK cases Philippe Mathieu-Daudé
` (7 subsequent siblings)
10 siblings, 0 replies; 13+ messages in thread
From: Philippe Mathieu-Daudé @ 2024-06-25 6:10 UTC (permalink / raw)
To: qemu-devel
Cc: Cédric Le Goater, qemu-block, Bin Meng,
Philippe Mathieu-Daudé
Signed-off-by: Philippe Mathieu-Daudé <philmd@linaro.org>
---
hw/sd/sd.c | 24 ++++++------------------
1 file changed, 6 insertions(+), 18 deletions(-)
diff --git a/hw/sd/sd.c b/hw/sd/sd.c
index 0011aa86da..346bd657a7 100644
--- a/hw/sd/sd.c
+++ b/hw/sd/sd.c
@@ -1290,11 +1290,8 @@ static sd_rsp_type_t sd_normal_command(SDState *sd, SDRequest req)
if (!sd_is_spi(sd)) {
break;
}
- sd->state = sd_sendingdata_state;
- memcpy(sd->data, sd->csd, 16);
- sd->data_start = sd_req_get_address(sd, req);
- sd->data_offset = 0;
- return sd_r1;
+ return sd_cmd_to_sendingdata(sd, req, sd_req_get_address(sd, req),
+ sd->csd, 16);
default:
break;
@@ -1314,11 +1311,8 @@ static sd_rsp_type_t sd_normal_command(SDState *sd, SDRequest req)
if (!sd_is_spi(sd)) {
break;
}
- sd->state = sd_sendingdata_state;
- memcpy(sd->data, sd->cid, 16);
- sd->data_start = sd_req_get_address(sd, req);
- sd->data_offset = 0;
- return sd_r1;
+ return sd_cmd_to_sendingdata(sd, req, sd_req_get_address(sd, req),
+ sd->cid, 16);
default:
break;
@@ -2108,15 +2102,9 @@ uint8_t sd_read_byte(SDState *sd)
sd->current_cmd, sd->data_offset, io_len);
switch (sd->current_cmd) {
case 6: /* CMD6: SWITCH_FUNCTION */
- sd_generic_read_byte(sd, &ret);
- break;
-
case 9: /* CMD9: SEND_CSD */
- case 10: /* CMD10: SEND_CID */
- ret = sd->data[sd->data_offset ++];
-
- if (sd->data_offset >= 16)
- sd->state = sd_transfer_state;
+ case 10: /* CMD10: SEND_CID */
+ sd_generic_read_byte(sd, &ret);
break;
case 13: /* ACMD13: SD_STATUS */
--
2.41.0
^ permalink raw reply related [flat|nested] 13+ messages in thread
* [PATCH 04/11] hw/sd/sdcard: Duplicate READ_SINGLE_BLOCK / READ_MULTIPLE_BLOCK cases
2024-06-25 6:10 [PATCH 00/11] hw/sd/sd: Introduce sd_cmd_to_sendingdata() and sd_generic_read_byte() Philippe Mathieu-Daudé
` (2 preceding siblings ...)
2024-06-25 6:10 ` [PATCH 03/11] hw/sd/sdcard: Convert SEND_CSD/SEND_CID to generic_read_byte (CMD9 & 10) Philippe Mathieu-Daudé
@ 2024-06-25 6:10 ` Philippe Mathieu-Daudé
2024-06-25 6:10 ` [PATCH 05/11] hw/sd/sdcard: Convert READ_SINGLE_BLOCK to generic_read_byte (CMD17) Philippe Mathieu-Daudé
` (6 subsequent siblings)
10 siblings, 0 replies; 13+ messages in thread
From: Philippe Mathieu-Daudé @ 2024-06-25 6:10 UTC (permalink / raw)
To: qemu-devel
Cc: Cédric Le Goater, qemu-block, Bin Meng,
Philippe Mathieu-Daudé
In order to modify the READ_SINGLE_BLOCK case in the
next commit, duplicate it first.
Signed-off-by: Philippe Mathieu-Daudé <philmd@linaro.org>
---
hw/sd/sd.c | 18 ++++++++++++++++++
1 file changed, 18 insertions(+)
diff --git a/hw/sd/sd.c b/hw/sd/sd.c
index 346bd657a7..9aaa78a334 100644
--- a/hw/sd/sd.c
+++ b/hw/sd/sd.c
@@ -1376,6 +1376,24 @@ static sd_rsp_type_t sd_normal_command(SDState *sd, SDRequest req)
break;
case 17: /* CMD17: READ_SINGLE_BLOCK */
+ addr = sd_req_get_address(sd, req);
+ switch (sd->state) {
+ case sd_transfer_state:
+
+ if (!address_in_range(sd, "READ_SINGLE_BLOCK", addr, sd->blk_len)) {
+ 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 */
addr = sd_req_get_address(sd, req);
switch (sd->state) {
--
2.41.0
^ permalink raw reply related [flat|nested] 13+ messages in thread
* [PATCH 05/11] hw/sd/sdcard: Convert READ_SINGLE_BLOCK to generic_read_byte (CMD17)
2024-06-25 6:10 [PATCH 00/11] hw/sd/sd: Introduce sd_cmd_to_sendingdata() and sd_generic_read_byte() Philippe Mathieu-Daudé
` (3 preceding siblings ...)
2024-06-25 6:10 ` [PATCH 04/11] hw/sd/sdcard: Duplicate READ_SINGLE_BLOCK / READ_MULTIPLE_BLOCK cases Philippe Mathieu-Daudé
@ 2024-06-25 6:10 ` Philippe Mathieu-Daudé
2024-06-25 6:10 ` [PATCH 06/11] hw/sd/sdcard: Convert SEND_TUNING_BLOCK to generic_read_byte (CMD19) Philippe Mathieu-Daudé
` (5 subsequent siblings)
10 siblings, 0 replies; 13+ messages in thread
From: Philippe Mathieu-Daudé @ 2024-06-25 6:10 UTC (permalink / raw)
To: qemu-devel
Cc: Cédric Le Goater, qemu-block, Bin Meng,
Philippe Mathieu-Daudé
Signed-off-by: Philippe Mathieu-Daudé <philmd@linaro.org>
---
hw/sd/sd.c | 18 +++---------------
1 file changed, 3 insertions(+), 15 deletions(-)
diff --git a/hw/sd/sd.c b/hw/sd/sd.c
index 9aaa78a334..6af7b3f034 100644
--- a/hw/sd/sd.c
+++ b/hw/sd/sd.c
@@ -1383,11 +1383,8 @@ static sd_rsp_type_t sd_normal_command(SDState *sd, SDRequest req)
if (!address_in_range(sd, "READ_SINGLE_BLOCK", addr, sd->blk_len)) {
return sd_r1;
}
-
- sd->state = sd_sendingdata_state;
- sd->data_start = addr;
- sd->data_offset = 0;
- return sd_r1;
+ sd_blk_read(sd, addr, sd->blk_len);
+ return sd_cmd_to_sendingdata(sd, req, addr, NULL, sd->blk_len);
default:
break;
@@ -2122,6 +2119,7 @@ uint8_t sd_read_byte(SDState *sd)
case 6: /* CMD6: SWITCH_FUNCTION */
case 9: /* CMD9: SEND_CSD */
case 10: /* CMD10: SEND_CID */
+ case 17: /* CMD17: READ_SINGLE_BLOCK */
sd_generic_read_byte(sd, &ret);
break;
@@ -2132,16 +2130,6 @@ uint8_t sd_read_byte(SDState *sd)
sd->state = sd_transfer_state;
break;
- case 17: /* CMD17: READ_SINGLE_BLOCK */
- if (sd->data_offset == 0) {
- sd_blk_read(sd, sd->data_start, io_len);
- }
- ret = sd->data[sd->data_offset ++];
-
- if (sd->data_offset >= io_len)
- sd->state = sd_transfer_state;
- break;
-
case 18: /* CMD18: READ_MULTIPLE_BLOCK */
if (sd->data_offset == 0) {
if (!address_in_range(sd, "READ_MULTIPLE_BLOCK",
--
2.41.0
^ permalink raw reply related [flat|nested] 13+ messages in thread
* [PATCH 06/11] hw/sd/sdcard: Convert SEND_TUNING_BLOCK to generic_read_byte (CMD19)
2024-06-25 6:10 [PATCH 00/11] hw/sd/sd: Introduce sd_cmd_to_sendingdata() and sd_generic_read_byte() Philippe Mathieu-Daudé
` (4 preceding siblings ...)
2024-06-25 6:10 ` [PATCH 05/11] hw/sd/sdcard: Convert READ_SINGLE_BLOCK to generic_read_byte (CMD17) Philippe Mathieu-Daudé
@ 2024-06-25 6:10 ` Philippe Mathieu-Daudé
2024-06-25 6:10 ` [PATCH 07/11] hw/sd/sdcard: Convert SEND_WRITE_PROT to generic_read_byte (CMD30) Philippe Mathieu-Daudé
` (4 subsequent siblings)
10 siblings, 0 replies; 13+ messages in thread
From: Philippe Mathieu-Daudé @ 2024-06-25 6:10 UTC (permalink / raw)
To: qemu-devel
Cc: Cédric Le Goater, qemu-block, Bin Meng,
Philippe Mathieu-Daudé
Signed-off-by: Philippe Mathieu-Daudé <philmd@linaro.org>
---
hw/sd/sd.c | 48 +++++++++++++++++++-----------------------------
1 file changed, 19 insertions(+), 29 deletions(-)
diff --git a/hw/sd/sd.c b/hw/sd/sd.c
index 6af7b3f034..3d341495e9 100644
--- a/hw/sd/sd.c
+++ b/hw/sd/sd.c
@@ -562,6 +562,21 @@ static void sd_set_sdstatus(SDState *sd)
memset(sd->sd_status, 0, 64);
}
+static const uint8_t sd_tuning_block_pattern4[64] = {
+ /*
+ * See: Physical Layer Simplified Specification Version 3.01,
+ * Table 4-2.
+ */
+ 0xff, 0x0f, 0xff, 0x00, 0x0f, 0xfc, 0xc3, 0xcc,
+ 0xc3, 0x3c, 0xcc, 0xff, 0xfe, 0xff, 0xfe, 0xef,
+ 0xff, 0xdf, 0xff, 0xdd, 0xff, 0xfb, 0xff, 0xfb,
+ 0xbf, 0xff, 0x7f, 0xff, 0x77, 0xf7, 0xbd, 0xef,
+ 0xff, 0xf0, 0xff, 0xf0, 0x0f, 0xfc, 0xcc, 0x3c,
+ 0xcc, 0x33, 0xcc, 0xcf, 0xff, 0xef, 0xff, 0xee,
+ 0xff, 0xfd, 0xff, 0xfd, 0xdf, 0xff, 0xbf, 0xff,
+ 0xbb, 0xff, 0xf7, 0xff, 0xf7, 0x7f, 0x7b, 0xde
+};
+
static int sd_req_crc_validate(SDRequest *req)
{
uint8_t buffer[5];
@@ -1139,14 +1154,9 @@ static sd_rsp_type_t sd_cmd_SEND_TUNING_BLOCK(SDState *sd, SDRequest req)
return sd_cmd_illegal(sd, req);
}
- if (sd->state != sd_transfer_state) {
- return sd_invalid_state_for_cmd(sd, req);
- }
-
- sd->state = sd_sendingdata_state;
- sd->data_offset = 0;
-
- return sd_r1;
+ return sd_cmd_to_sendingdata(sd, req, 0,
+ sd_tuning_block_pattern4,
+ sizeof(sd_tuning_block_pattern4));
}
/* CMD23 */
@@ -2078,20 +2088,6 @@ void sd_write_byte(SDState *sd, uint8_t value)
}
}
-#define SD_TUNING_BLOCK_SIZE 64
-
-static const uint8_t sd_tuning_block_pattern[SD_TUNING_BLOCK_SIZE] = {
- /* See: Physical Layer Simplified Specification Version 3.01, Table 4-2 */
- 0xff, 0x0f, 0xff, 0x00, 0x0f, 0xfc, 0xc3, 0xcc,
- 0xc3, 0x3c, 0xcc, 0xff, 0xfe, 0xff, 0xfe, 0xef,
- 0xff, 0xdf, 0xff, 0xdd, 0xff, 0xfb, 0xff, 0xfb,
- 0xbf, 0xff, 0x7f, 0xff, 0x77, 0xf7, 0xbd, 0xef,
- 0xff, 0xf0, 0xff, 0xf0, 0x0f, 0xfc, 0xcc, 0x3c,
- 0xcc, 0x33, 0xcc, 0xcf, 0xff, 0xef, 0xff, 0xee,
- 0xff, 0xfd, 0xff, 0xfd, 0xdf, 0xff, 0xbf, 0xff,
- 0xbb, 0xff, 0xf7, 0xff, 0xf7, 0x7f, 0x7b, 0xde,
-};
-
uint8_t sd_read_byte(SDState *sd)
{
/* TODO: Append CRCs */
@@ -2120,6 +2116,7 @@ uint8_t sd_read_byte(SDState *sd)
case 9: /* CMD9: SEND_CSD */
case 10: /* CMD10: SEND_CID */
case 17: /* CMD17: READ_SINGLE_BLOCK */
+ case 19: /* CMD19: SEND_TUNING_BLOCK (SD) */
sd_generic_read_byte(sd, &ret);
break;
@@ -2154,13 +2151,6 @@ uint8_t sd_read_byte(SDState *sd)
}
break;
- case 19: /* CMD19: SEND_TUNING_BLOCK (SD) */
- if (sd->data_offset >= SD_TUNING_BLOCK_SIZE - 1) {
- sd->state = sd_transfer_state;
- }
- ret = sd_tuning_block_pattern[sd->data_offset++];
- break;
-
case 22: /* ACMD22: SEND_NUM_WR_BLOCKS */
ret = sd->data[sd->data_offset ++];
--
2.41.0
^ permalink raw reply related [flat|nested] 13+ messages in thread
* [PATCH 07/11] hw/sd/sdcard: Convert SEND_WRITE_PROT to generic_read_byte (CMD30)
2024-06-25 6:10 [PATCH 00/11] hw/sd/sd: Introduce sd_cmd_to_sendingdata() and sd_generic_read_byte() Philippe Mathieu-Daudé
` (5 preceding siblings ...)
2024-06-25 6:10 ` [PATCH 06/11] hw/sd/sdcard: Convert SEND_TUNING_BLOCK to generic_read_byte (CMD19) Philippe Mathieu-Daudé
@ 2024-06-25 6:10 ` Philippe Mathieu-Daudé
2024-06-25 6:10 ` [PATCH 08/11] hw/sd/sdcard: Convert GEN_CMD to generic_read_byte (CMD56) Philippe Mathieu-Daudé
` (3 subsequent siblings)
10 siblings, 0 replies; 13+ messages in thread
From: Philippe Mathieu-Daudé @ 2024-06-25 6:10 UTC (permalink / raw)
To: qemu-devel
Cc: Cédric Le Goater, qemu-block, Bin Meng,
Philippe Mathieu-Daudé
Signed-off-by: Philippe Mathieu-Daudé <philmd@linaro.org>
---
hw/sd/sd.c | 17 ++++-------------
1 file changed, 4 insertions(+), 13 deletions(-)
diff --git a/hw/sd/sd.c b/hw/sd/sd.c
index 3d341495e9..6e6b37bd2d 100644
--- a/hw/sd/sd.c
+++ b/hw/sd/sd.c
@@ -1180,6 +1180,7 @@ static sd_rsp_type_t sd_normal_command(SDState *sd, SDRequest req)
{
uint16_t rca;
uint64_t addr;
+ uint32_t data;
sd->last_cmd_name = sd_cmd_name(req.cmd);
/* CMD55 precedes an ACMD, so we are not interested in tracing it.
@@ -1533,12 +1534,8 @@ static sd_rsp_type_t sd_normal_command(SDState *sd, SDRequest req)
req.arg, sd->blk_len)) {
return sd_r1;
}
-
- sd->state = sd_sendingdata_state;
- stl_be_p(sd->data, sd_wpbits(sd, req.arg));
- sd->data_start = addr;
- sd->data_offset = 0;
- return sd_r1;
+ data = sd_wpbits(sd, req.arg);
+ return sd_cmd_to_sendingdata(sd, req, addr, &data, sizeof(data));
default:
break;
@@ -2117,6 +2114,7 @@ uint8_t sd_read_byte(SDState *sd)
case 10: /* CMD10: SEND_CID */
case 17: /* CMD17: READ_SINGLE_BLOCK */
case 19: /* CMD19: SEND_TUNING_BLOCK (SD) */
+ case 30: /* CMD30: SEND_WRITE_PROT */
sd_generic_read_byte(sd, &ret);
break;
@@ -2158,13 +2156,6 @@ uint8_t sd_read_byte(SDState *sd)
sd->state = sd_transfer_state;
break;
- case 30: /* CMD30: SEND_WRITE_PROT */
- ret = sd->data[sd->data_offset ++];
-
- if (sd->data_offset >= 4)
- sd->state = sd_transfer_state;
- break;
-
case 51: /* ACMD51: SEND_SCR */
ret = sd->scr[sd->data_offset ++];
--
2.41.0
^ permalink raw reply related [flat|nested] 13+ messages in thread
* [PATCH 08/11] hw/sd/sdcard: Convert GEN_CMD to generic_read_byte (CMD56)
2024-06-25 6:10 [PATCH 00/11] hw/sd/sd: Introduce sd_cmd_to_sendingdata() and sd_generic_read_byte() Philippe Mathieu-Daudé
` (6 preceding siblings ...)
2024-06-25 6:10 ` [PATCH 07/11] hw/sd/sdcard: Convert SEND_WRITE_PROT to generic_read_byte (CMD30) Philippe Mathieu-Daudé
@ 2024-06-25 6:10 ` Philippe Mathieu-Daudé
2024-06-25 6:10 ` [PATCH 09/11] hw/sd/sdcard: Convert SD_STATUS to generic_read_byte (ACMD13) Philippe Mathieu-Daudé
` (2 subsequent siblings)
10 siblings, 0 replies; 13+ messages in thread
From: Philippe Mathieu-Daudé @ 2024-06-25 6:10 UTC (permalink / raw)
To: qemu-devel
Cc: Cédric Le Goater, qemu-block, Bin Meng,
Philippe Mathieu-Daudé
Signed-off-by: Philippe Mathieu-Daudé <philmd@linaro.org>
---
hw/sd/sd.c | 19 +++++++------------
1 file changed, 7 insertions(+), 12 deletions(-)
diff --git a/hw/sd/sd.c b/hw/sd/sd.c
index 6e6b37bd2d..bf78f06b5c 100644
--- a/hw/sd/sd.c
+++ b/hw/sd/sd.c
@@ -1626,10 +1626,12 @@ static sd_rsp_type_t sd_normal_command(SDState *sd, SDRequest req)
switch (sd->state) {
case sd_transfer_state:
sd->data_offset = 0;
- if (req.arg & 1)
- sd->state = sd_sendingdata_state;
- else
- sd->state = sd_receivingdata_state;
+ if (req.arg & 1) {
+ return sd_cmd_to_sendingdata(sd, req, 0,
+ sd->vendor_data,
+ sizeof(sd->vendor_data));
+ }
+ sd->state = sd_receivingdata_state;
return sd_r1;
default:
@@ -2115,6 +2117,7 @@ uint8_t sd_read_byte(SDState *sd)
case 17: /* CMD17: READ_SINGLE_BLOCK */
case 19: /* CMD19: SEND_TUNING_BLOCK (SD) */
case 30: /* CMD30: SEND_WRITE_PROT */
+ case 56: /* CMD56: GEN_CMD */
sd_generic_read_byte(sd, &ret);
break;
@@ -2163,14 +2166,6 @@ uint8_t sd_read_byte(SDState *sd)
sd->state = sd_transfer_state;
break;
- case 56: /* CMD56: GEN_CMD */
- ret = sd->vendor_data[sd->data_offset ++];
-
- if (sd->data_offset >= sizeof(sd->vendor_data)) {
- sd->state = sd_transfer_state;
- }
- break;
-
default:
qemu_log_mask(LOG_GUEST_ERROR, "%s: unknown command\n", __func__);
return 0x00;
--
2.41.0
^ permalink raw reply related [flat|nested] 13+ messages in thread
* [PATCH 09/11] hw/sd/sdcard: Convert SD_STATUS to generic_read_byte (ACMD13)
2024-06-25 6:10 [PATCH 00/11] hw/sd/sd: Introduce sd_cmd_to_sendingdata() and sd_generic_read_byte() Philippe Mathieu-Daudé
` (7 preceding siblings ...)
2024-06-25 6:10 ` [PATCH 08/11] hw/sd/sdcard: Convert GEN_CMD to generic_read_byte (CMD56) Philippe Mathieu-Daudé
@ 2024-06-25 6:10 ` Philippe Mathieu-Daudé
2024-06-25 6:10 ` [PATCH 10/11] hw/sd/sdcard: Convert SEND_NUM_WR_BLOCKS to generic_read_byte (ACMD22) Philippe Mathieu-Daudé
2024-06-25 6:10 ` [PATCH 11/11] hw/sd/sdcard: Convert SEND_SCR to generic_read_byte (ACMD51) Philippe Mathieu-Daudé
10 siblings, 0 replies; 13+ messages in thread
From: Philippe Mathieu-Daudé @ 2024-06-25 6:10 UTC (permalink / raw)
To: qemu-devel
Cc: Cédric Le Goater, qemu-block, Bin Meng,
Philippe Mathieu-Daudé
Signed-off-by: Philippe Mathieu-Daudé <philmd@linaro.org>
---
hw/sd/sd.c | 15 ++++-----------
1 file changed, 4 insertions(+), 11 deletions(-)
diff --git a/hw/sd/sd.c b/hw/sd/sd.c
index bf78f06b5c..2f00184bb3 100644
--- a/hw/sd/sd.c
+++ b/hw/sd/sd.c
@@ -1681,10 +1681,9 @@ static sd_rsp_type_t sd_app_command(SDState *sd,
case 13: /* ACMD13: SD_STATUS */
switch (sd->state) {
case sd_transfer_state:
- sd->state = sd_sendingdata_state;
- sd->data_start = 0;
- sd->data_offset = 0;
- return sd_r1;
+ return sd_cmd_to_sendingdata(sd, req, 0,
+ sd->sd_status,
+ sizeof(sd->sd_status));
default:
break;
@@ -2114,6 +2113,7 @@ uint8_t sd_read_byte(SDState *sd)
case 6: /* CMD6: SWITCH_FUNCTION */
case 9: /* CMD9: SEND_CSD */
case 10: /* CMD10: SEND_CID */
+ case 13: /* ACMD13: SD_STATUS */
case 17: /* CMD17: READ_SINGLE_BLOCK */
case 19: /* CMD19: SEND_TUNING_BLOCK (SD) */
case 30: /* CMD30: SEND_WRITE_PROT */
@@ -2121,13 +2121,6 @@ uint8_t sd_read_byte(SDState *sd)
sd_generic_read_byte(sd, &ret);
break;
- case 13: /* ACMD13: SD_STATUS */
- ret = sd->sd_status[sd->data_offset ++];
-
- if (sd->data_offset >= sizeof(sd->sd_status))
- sd->state = sd_transfer_state;
- break;
-
case 18: /* CMD18: READ_MULTIPLE_BLOCK */
if (sd->data_offset == 0) {
if (!address_in_range(sd, "READ_MULTIPLE_BLOCK",
--
2.41.0
^ permalink raw reply related [flat|nested] 13+ messages in thread
* [PATCH 10/11] hw/sd/sdcard: Convert SEND_NUM_WR_BLOCKS to generic_read_byte (ACMD22)
2024-06-25 6:10 [PATCH 00/11] hw/sd/sd: Introduce sd_cmd_to_sendingdata() and sd_generic_read_byte() Philippe Mathieu-Daudé
` (8 preceding siblings ...)
2024-06-25 6:10 ` [PATCH 09/11] hw/sd/sdcard: Convert SD_STATUS to generic_read_byte (ACMD13) Philippe Mathieu-Daudé
@ 2024-06-25 6:10 ` Philippe Mathieu-Daudé
2024-06-25 6:10 ` [PATCH 11/11] hw/sd/sdcard: Convert SEND_SCR to generic_read_byte (ACMD51) Philippe Mathieu-Daudé
10 siblings, 0 replies; 13+ messages in thread
From: Philippe Mathieu-Daudé @ 2024-06-25 6:10 UTC (permalink / raw)
To: qemu-devel
Cc: Cédric Le Goater, qemu-block, Bin Meng,
Philippe Mathieu-Daudé
Signed-off-by: Philippe Mathieu-Daudé <philmd@linaro.org>
---
hw/sd/sd.c | 16 ++++------------
1 file changed, 4 insertions(+), 12 deletions(-)
diff --git a/hw/sd/sd.c b/hw/sd/sd.c
index 2f00184bb3..023fcc63ac 100644
--- a/hw/sd/sd.c
+++ b/hw/sd/sd.c
@@ -1693,11 +1693,9 @@ static sd_rsp_type_t sd_app_command(SDState *sd,
case 22: /* ACMD22: SEND_NUM_WR_BLOCKS */
switch (sd->state) {
case sd_transfer_state:
- stl_be_p(sd->data, sd->blk_written);
- sd->state = sd_sendingdata_state;
- sd->data_start = 0;
- sd->data_offset = 0;
- return sd_r1;
+ return sd_cmd_to_sendingdata(sd, req, 0,
+ &sd->blk_written,
+ sizeof(sd->blk_written));
default:
break;
@@ -2116,6 +2114,7 @@ uint8_t sd_read_byte(SDState *sd)
case 13: /* ACMD13: SD_STATUS */
case 17: /* CMD17: READ_SINGLE_BLOCK */
case 19: /* CMD19: SEND_TUNING_BLOCK (SD) */
+ case 22: /* ACMD22: SEND_NUM_WR_BLOCKS */
case 30: /* CMD30: SEND_WRITE_PROT */
case 56: /* CMD56: GEN_CMD */
sd_generic_read_byte(sd, &ret);
@@ -2145,13 +2144,6 @@ uint8_t sd_read_byte(SDState *sd)
}
break;
- case 22: /* ACMD22: SEND_NUM_WR_BLOCKS */
- ret = sd->data[sd->data_offset ++];
-
- if (sd->data_offset >= 4)
- sd->state = sd_transfer_state;
- break;
-
case 51: /* ACMD51: SEND_SCR */
ret = sd->scr[sd->data_offset ++];
--
2.41.0
^ permalink raw reply related [flat|nested] 13+ messages in thread
* [PATCH 11/11] hw/sd/sdcard: Convert SEND_SCR to generic_read_byte (ACMD51)
2024-06-25 6:10 [PATCH 00/11] hw/sd/sd: Introduce sd_cmd_to_sendingdata() and sd_generic_read_byte() Philippe Mathieu-Daudé
` (9 preceding siblings ...)
2024-06-25 6:10 ` [PATCH 10/11] hw/sd/sdcard: Convert SEND_NUM_WR_BLOCKS to generic_read_byte (ACMD22) Philippe Mathieu-Daudé
@ 2024-06-25 6:10 ` Philippe Mathieu-Daudé
10 siblings, 0 replies; 13+ messages in thread
From: Philippe Mathieu-Daudé @ 2024-06-25 6:10 UTC (permalink / raw)
To: qemu-devel
Cc: Cédric Le Goater, qemu-block, Bin Meng,
Philippe Mathieu-Daudé
Signed-off-by: Philippe Mathieu-Daudé <philmd@linaro.org>
---
hw/sd/sd.c | 13 ++-----------
1 file changed, 2 insertions(+), 11 deletions(-)
diff --git a/hw/sd/sd.c b/hw/sd/sd.c
index 023fcc63ac..af3a46373f 100644
--- a/hw/sd/sd.c
+++ b/hw/sd/sd.c
@@ -1763,10 +1763,7 @@ static sd_rsp_type_t sd_app_command(SDState *sd,
case 51: /* ACMD51: SEND_SCR */
switch (sd->state) {
case sd_transfer_state:
- sd->state = sd_sendingdata_state;
- sd->data_start = 0;
- sd->data_offset = 0;
- return sd_r1;
+ return sd_cmd_to_sendingdata(sd, req, 0, sd->scr, sizeof(sd->scr));
default:
break;
@@ -2116,6 +2113,7 @@ uint8_t sd_read_byte(SDState *sd)
case 19: /* CMD19: SEND_TUNING_BLOCK (SD) */
case 22: /* ACMD22: SEND_NUM_WR_BLOCKS */
case 30: /* CMD30: SEND_WRITE_PROT */
+ case 51: /* ACMD51: SEND_SCR */
case 56: /* CMD56: GEN_CMD */
sd_generic_read_byte(sd, &ret);
break;
@@ -2144,13 +2142,6 @@ uint8_t sd_read_byte(SDState *sd)
}
break;
- case 51: /* ACMD51: SEND_SCR */
- ret = sd->scr[sd->data_offset ++];
-
- if (sd->data_offset >= sizeof(sd->scr))
- sd->state = sd_transfer_state;
- break;
-
default:
qemu_log_mask(LOG_GUEST_ERROR, "%s: unknown command\n", __func__);
return 0x00;
--
2.41.0
^ permalink raw reply related [flat|nested] 13+ messages in thread