qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
* [PATCH v2 00/11] hw/sd/sd: Introduce sd_cmd_to_sendingdata() and sd_generic_read_byte()
@ 2024-06-27 16:27 Philippe Mathieu-Daudé
  2024-06-27 16:27 ` [PATCH v2 01/11] hw/sd/sdcard: Introduce sd_cmd_to_sendingdata and sd_generic_read_byte Philippe Mathieu-Daudé
                   ` (10 more replies)
  0 siblings, 11 replies; 12+ messages in thread
From: Philippe Mathieu-Daudé @ 2024-06-27 16:27 UTC (permalink / raw)
  To: qemu-devel
  Cc: qemu-block, Cédric Le Goater, Francisco Iglesias, Bin Meng,
	Sai Pavan Boddu, Joel Stanley, Philippe Mathieu-Daudé,
	Luc Michel

Consolitate reading bytes on the DAT lines by introducing
a pair of helpers to reuse in all commands sending data.

Full series for testing:
https://gitlab.com/philmd/qemu/-/tags/emmc-v4

Based-on: <20240627162232.80428-1-philmd@linaro.org>

Philippe Mathieu-Daudé (11):
  hw/sd/sdcard: Introduce sd_cmd_to_sendingdata and sd_generic_read_byte
  hw/sd/sdcard: Convert SWITCH_FUNCTION to generic_read_byte (CMD6)
  hw/sd/sdcard: Convert SEND_CSD/SEND_CID to generic_read_byte (CMD9 &
    10)
  hw/sd/sdcard: Duplicate READ_SINGLE_BLOCK / READ_MULTIPLE_BLOCK cases
  hw/sd/sdcard: Convert READ_SINGLE_BLOCK to generic_read_byte (CMD17)
  hw/sd/sdcard: Convert SEND_TUNING_BLOCK to generic_read_byte (CMD19)
  hw/sd/sdcard: Convert SEND_WRITE_PROT to generic_read_byte (CMD30)
  hw/sd/sdcard: Convert GEN_CMD to generic_read_byte (CMD56)
  hw/sd/sdcard: Convert SD_STATUS to generic_read_byte (ACMD13)
  hw/sd/sdcard: Convert SEND_NUM_WR_BLOCKS to generic_read_byte (ACMD22)
  hw/sd/sdcard: Convert SEND_SCR to generic_read_byte (ACMD51)

 hw/sd/sd.c | 223 ++++++++++++++++++++++++-----------------------------
 1 file changed, 100 insertions(+), 123 deletions(-)

-- 
2.41.0



^ permalink raw reply	[flat|nested] 12+ messages in thread

* [PATCH v2 01/11] hw/sd/sdcard: Introduce sd_cmd_to_sendingdata and sd_generic_read_byte
  2024-06-27 16:27 [PATCH v2 00/11] hw/sd/sd: Introduce sd_cmd_to_sendingdata() and sd_generic_read_byte() Philippe Mathieu-Daudé
@ 2024-06-27 16:27 ` Philippe Mathieu-Daudé
  2024-06-27 16:27 ` [PATCH v2 02/11] hw/sd/sdcard: Convert SWITCH_FUNCTION to generic_read_byte (CMD6) Philippe Mathieu-Daudé
                   ` (9 subsequent siblings)
  10 siblings, 0 replies; 12+ messages in thread
From: Philippe Mathieu-Daudé @ 2024-06-27 16:27 UTC (permalink / raw)
  To: qemu-devel
  Cc: qemu-block, Cédric Le Goater, Francisco Iglesias, Bin Meng,
	Sai Pavan Boddu, Joel Stanley, Philippe Mathieu-Daudé,
	Luc Michel

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 | 39 +++++++++++++++++++++++++++++++++++++++
 1 file changed, 39 insertions(+)

diff --git a/hw/sd/sd.c b/hw/sd/sd.c
index d85b2906f4..1a8d06804d 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];
 
@@ -1083,6 +1085,29 @@ static sd_rsp_type_t sd_cmd_unimplemented(SDState *sd, SDRequest req)
     return sd_illegal;
 }
 
+/* Configure fields for following sd_generic_read_byte() calls */
+__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)
 {
@@ -1920,6 +1945,20 @@ send_response:
     return rsplen;
 }
 
+/* Return true when buffer is consumed. Configured by sd_cmd_to_sendingdata() */
+__attribute__((unused))
+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] 12+ messages in thread

* [PATCH v2 02/11] hw/sd/sdcard: Convert SWITCH_FUNCTION to generic_read_byte (CMD6)
  2024-06-27 16:27 [PATCH v2 00/11] hw/sd/sd: Introduce sd_cmd_to_sendingdata() and sd_generic_read_byte() Philippe Mathieu-Daudé
  2024-06-27 16:27 ` [PATCH v2 01/11] hw/sd/sdcard: Introduce sd_cmd_to_sendingdata and sd_generic_read_byte Philippe Mathieu-Daudé
@ 2024-06-27 16:27 ` Philippe Mathieu-Daudé
  2024-06-27 16:27 ` [PATCH v2 03/11] hw/sd/sdcard: Convert SEND_CSD/SEND_CID to generic_read_byte (CMD9 & 10) Philippe Mathieu-Daudé
                   ` (8 subsequent siblings)
  10 siblings, 0 replies; 12+ messages in thread
From: Philippe Mathieu-Daudé @ 2024-06-27 16:27 UTC (permalink / raw)
  To: qemu-devel
  Cc: qemu-block, Cédric Le Goater, Francisco Iglesias, Bin Meng,
	Sai Pavan Boddu, Joel Stanley, Philippe Mathieu-Daudé,
	Luc Michel

Signed-off-by: Philippe Mathieu-Daudé <philmd@linaro.org>
---
 hw/sd/sd.c | 12 ++----------
 1 file changed, 2 insertions(+), 10 deletions(-)

diff --git a/hw/sd/sd.c b/hw/sd/sd.c
index 1a8d06804d..f7735c39a8 100644
--- a/hw/sd/sd.c
+++ b/hw/sd/sd.c
@@ -1086,7 +1086,6 @@ static sd_rsp_type_t sd_cmd_unimplemented(SDState *sd, SDRequest req)
 }
 
 /* Configure fields for following sd_generic_read_byte() calls */
-__attribute__((unused))
 static sd_rsp_type_t sd_cmd_to_sendingdata(SDState *sd, SDRequest req,
                                            uint64_t start,
                                            const void *data, size_t size)
@@ -1243,10 +1242,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);
@@ -1946,7 +1942,6 @@ send_response:
 }
 
 /* Return true when buffer is consumed. Configured by sd_cmd_to_sendingdata() */
-__attribute__((unused))
 static bool sd_generic_read_byte(SDState *sd, uint8_t *value)
 {
     *value = sd->data[sd->data_offset];
@@ -2135,10 +2130,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] 12+ messages in thread

* [PATCH v2 03/11] hw/sd/sdcard: Convert SEND_CSD/SEND_CID to generic_read_byte (CMD9 & 10)
  2024-06-27 16:27 [PATCH v2 00/11] hw/sd/sd: Introduce sd_cmd_to_sendingdata() and sd_generic_read_byte() Philippe Mathieu-Daudé
  2024-06-27 16:27 ` [PATCH v2 01/11] hw/sd/sdcard: Introduce sd_cmd_to_sendingdata and sd_generic_read_byte Philippe Mathieu-Daudé
  2024-06-27 16:27 ` [PATCH v2 02/11] hw/sd/sdcard: Convert SWITCH_FUNCTION to generic_read_byte (CMD6) Philippe Mathieu-Daudé
@ 2024-06-27 16:27 ` Philippe Mathieu-Daudé
  2024-06-27 16:27 ` [PATCH v2 04/11] hw/sd/sdcard: Duplicate READ_SINGLE_BLOCK / READ_MULTIPLE_BLOCK cases Philippe Mathieu-Daudé
                   ` (7 subsequent siblings)
  10 siblings, 0 replies; 12+ messages in thread
From: Philippe Mathieu-Daudé @ 2024-06-27 16:27 UTC (permalink / raw)
  To: qemu-devel
  Cc: qemu-block, Cédric Le Goater, Francisco Iglesias, Bin Meng,
	Sai Pavan Boddu, Joel Stanley, Philippe Mathieu-Daudé,
	Luc Michel

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 f7735c39a8..8201f3245c 100644
--- a/hw/sd/sd.c
+++ b/hw/sd/sd.c
@@ -1312,11 +1312,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;
@@ -1336,11 +1333,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;
@@ -2130,15 +2124,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] 12+ messages in thread

* [PATCH v2 04/11] hw/sd/sdcard: Duplicate READ_SINGLE_BLOCK / READ_MULTIPLE_BLOCK cases
  2024-06-27 16:27 [PATCH v2 00/11] hw/sd/sd: Introduce sd_cmd_to_sendingdata() and sd_generic_read_byte() Philippe Mathieu-Daudé
                   ` (2 preceding siblings ...)
  2024-06-27 16:27 ` [PATCH v2 03/11] hw/sd/sdcard: Convert SEND_CSD/SEND_CID to generic_read_byte (CMD9 & 10) Philippe Mathieu-Daudé
@ 2024-06-27 16:27 ` Philippe Mathieu-Daudé
  2024-06-27 16:27 ` [PATCH v2 05/11] hw/sd/sdcard: Convert READ_SINGLE_BLOCK to generic_read_byte (CMD17) Philippe Mathieu-Daudé
                   ` (6 subsequent siblings)
  10 siblings, 0 replies; 12+ messages in thread
From: Philippe Mathieu-Daudé @ 2024-06-27 16:27 UTC (permalink / raw)
  To: qemu-devel
  Cc: qemu-block, Cédric Le Goater, Francisco Iglesias, Bin Meng,
	Sai Pavan Boddu, Joel Stanley, Philippe Mathieu-Daudé,
	Luc Michel

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 8201f3245c..dfcb213aa9 100644
--- a/hw/sd/sd.c
+++ b/hw/sd/sd.c
@@ -1398,6 +1398,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] 12+ messages in thread

* [PATCH v2 05/11] hw/sd/sdcard: Convert READ_SINGLE_BLOCK to generic_read_byte (CMD17)
  2024-06-27 16:27 [PATCH v2 00/11] hw/sd/sd: Introduce sd_cmd_to_sendingdata() and sd_generic_read_byte() Philippe Mathieu-Daudé
                   ` (3 preceding siblings ...)
  2024-06-27 16:27 ` [PATCH v2 04/11] hw/sd/sdcard: Duplicate READ_SINGLE_BLOCK / READ_MULTIPLE_BLOCK cases Philippe Mathieu-Daudé
@ 2024-06-27 16:27 ` Philippe Mathieu-Daudé
  2024-06-27 16:27 ` [PATCH v2 06/11] hw/sd/sdcard: Convert SEND_TUNING_BLOCK to generic_read_byte (CMD19) Philippe Mathieu-Daudé
                   ` (5 subsequent siblings)
  10 siblings, 0 replies; 12+ messages in thread
From: Philippe Mathieu-Daudé @ 2024-06-27 16:27 UTC (permalink / raw)
  To: qemu-devel
  Cc: qemu-block, Cédric Le Goater, Francisco Iglesias, Bin Meng,
	Sai Pavan Boddu, Joel Stanley, Philippe Mathieu-Daudé,
	Luc Michel

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 dfcb213aa9..605269163d 100644
--- a/hw/sd/sd.c
+++ b/hw/sd/sd.c
@@ -1405,11 +1405,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;
@@ -2144,6 +2141,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;
 
@@ -2154,16 +2152,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] 12+ messages in thread

* [PATCH v2 06/11] hw/sd/sdcard: Convert SEND_TUNING_BLOCK to generic_read_byte (CMD19)
  2024-06-27 16:27 [PATCH v2 00/11] hw/sd/sd: Introduce sd_cmd_to_sendingdata() and sd_generic_read_byte() Philippe Mathieu-Daudé
                   ` (4 preceding siblings ...)
  2024-06-27 16:27 ` [PATCH v2 05/11] hw/sd/sdcard: Convert READ_SINGLE_BLOCK to generic_read_byte (CMD17) Philippe Mathieu-Daudé
@ 2024-06-27 16:27 ` Philippe Mathieu-Daudé
  2024-06-27 16:27 ` [PATCH v2 07/11] hw/sd/sdcard: Convert SEND_WRITE_PROT to generic_read_byte (CMD30) Philippe Mathieu-Daudé
                   ` (4 subsequent siblings)
  10 siblings, 0 replies; 12+ messages in thread
From: Philippe Mathieu-Daudé @ 2024-06-27 16:27 UTC (permalink / raw)
  To: qemu-devel
  Cc: qemu-block, Cédric Le Goater, Francisco Iglesias, Bin Meng,
	Sai Pavan Boddu, Joel Stanley, Philippe Mathieu-Daudé,
	Luc Michel

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 605269163d..eece33194a 100644
--- a/hw/sd/sd.c
+++ b/hw/sd/sd.c
@@ -568,6 +568,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];
@@ -1161,14 +1176,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 */
@@ -2100,20 +2110,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 */
@@ -2142,6 +2138,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;
 
@@ -2176,13 +2173,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] 12+ messages in thread

* [PATCH v2 07/11] hw/sd/sdcard: Convert SEND_WRITE_PROT to generic_read_byte (CMD30)
  2024-06-27 16:27 [PATCH v2 00/11] hw/sd/sd: Introduce sd_cmd_to_sendingdata() and sd_generic_read_byte() Philippe Mathieu-Daudé
                   ` (5 preceding siblings ...)
  2024-06-27 16:27 ` [PATCH v2 06/11] hw/sd/sdcard: Convert SEND_TUNING_BLOCK to generic_read_byte (CMD19) Philippe Mathieu-Daudé
@ 2024-06-27 16:27 ` Philippe Mathieu-Daudé
  2024-06-27 16:27 ` [PATCH v2 08/11] hw/sd/sdcard: Convert GEN_CMD to generic_read_byte (CMD56) Philippe Mathieu-Daudé
                   ` (3 subsequent siblings)
  10 siblings, 0 replies; 12+ messages in thread
From: Philippe Mathieu-Daudé @ 2024-06-27 16:27 UTC (permalink / raw)
  To: qemu-devel
  Cc: qemu-block, Cédric Le Goater, Francisco Iglesias, Bin Meng,
	Sai Pavan Boddu, Joel Stanley, Philippe Mathieu-Daudé,
	Luc Michel

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 eece33194a..bf922da2cc 100644
--- a/hw/sd/sd.c
+++ b/hw/sd/sd.c
@@ -1202,6 +1202,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.
@@ -1555,12 +1556,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;
@@ -2139,6 +2136,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;
 
@@ -2180,13 +2178,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] 12+ messages in thread

* [PATCH v2 08/11] hw/sd/sdcard: Convert GEN_CMD to generic_read_byte (CMD56)
  2024-06-27 16:27 [PATCH v2 00/11] hw/sd/sd: Introduce sd_cmd_to_sendingdata() and sd_generic_read_byte() Philippe Mathieu-Daudé
                   ` (6 preceding siblings ...)
  2024-06-27 16:27 ` [PATCH v2 07/11] hw/sd/sdcard: Convert SEND_WRITE_PROT to generic_read_byte (CMD30) Philippe Mathieu-Daudé
@ 2024-06-27 16:27 ` Philippe Mathieu-Daudé
  2024-06-27 16:27 ` [PATCH v2 09/11] hw/sd/sdcard: Convert SD_STATUS to generic_read_byte (ACMD13) Philippe Mathieu-Daudé
                   ` (2 subsequent siblings)
  10 siblings, 0 replies; 12+ messages in thread
From: Philippe Mathieu-Daudé @ 2024-06-27 16:27 UTC (permalink / raw)
  To: qemu-devel
  Cc: qemu-block, Cédric Le Goater, Francisco Iglesias, Bin Meng,
	Sai Pavan Boddu, Joel Stanley, Philippe Mathieu-Daudé,
	Luc Michel

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 bf922da2cc..ccf81b9e59 100644
--- a/hw/sd/sd.c
+++ b/hw/sd/sd.c
@@ -1648,10 +1648,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:
@@ -2137,6 +2139,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;
 
@@ -2185,14 +2188,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] 12+ messages in thread

* [PATCH v2 09/11] hw/sd/sdcard: Convert SD_STATUS to generic_read_byte (ACMD13)
  2024-06-27 16:27 [PATCH v2 00/11] hw/sd/sd: Introduce sd_cmd_to_sendingdata() and sd_generic_read_byte() Philippe Mathieu-Daudé
                   ` (7 preceding siblings ...)
  2024-06-27 16:27 ` [PATCH v2 08/11] hw/sd/sdcard: Convert GEN_CMD to generic_read_byte (CMD56) Philippe Mathieu-Daudé
@ 2024-06-27 16:27 ` Philippe Mathieu-Daudé
  2024-06-27 16:27 ` [PATCH v2 10/11] hw/sd/sdcard: Convert SEND_NUM_WR_BLOCKS to generic_read_byte (ACMD22) Philippe Mathieu-Daudé
  2024-06-27 16:27 ` [PATCH v2 11/11] hw/sd/sdcard: Convert SEND_SCR to generic_read_byte (ACMD51) Philippe Mathieu-Daudé
  10 siblings, 0 replies; 12+ messages in thread
From: Philippe Mathieu-Daudé @ 2024-06-27 16:27 UTC (permalink / raw)
  To: qemu-devel
  Cc: qemu-block, Cédric Le Goater, Francisco Iglesias, Bin Meng,
	Sai Pavan Boddu, Joel Stanley, Philippe Mathieu-Daudé,
	Luc Michel

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 ccf81b9e59..1c4811f410 100644
--- a/hw/sd/sd.c
+++ b/hw/sd/sd.c
@@ -1703,10 +1703,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;
@@ -2136,6 +2135,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 */
@@ -2143,13 +2143,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] 12+ messages in thread

* [PATCH v2 10/11] hw/sd/sdcard: Convert SEND_NUM_WR_BLOCKS to generic_read_byte (ACMD22)
  2024-06-27 16:27 [PATCH v2 00/11] hw/sd/sd: Introduce sd_cmd_to_sendingdata() and sd_generic_read_byte() Philippe Mathieu-Daudé
                   ` (8 preceding siblings ...)
  2024-06-27 16:27 ` [PATCH v2 09/11] hw/sd/sdcard: Convert SD_STATUS to generic_read_byte (ACMD13) Philippe Mathieu-Daudé
@ 2024-06-27 16:27 ` Philippe Mathieu-Daudé
  2024-06-27 16:27 ` [PATCH v2 11/11] hw/sd/sdcard: Convert SEND_SCR to generic_read_byte (ACMD51) Philippe Mathieu-Daudé
  10 siblings, 0 replies; 12+ messages in thread
From: Philippe Mathieu-Daudé @ 2024-06-27 16:27 UTC (permalink / raw)
  To: qemu-devel
  Cc: qemu-block, Cédric Le Goater, Francisco Iglesias, Bin Meng,
	Sai Pavan Boddu, Joel Stanley, Philippe Mathieu-Daudé,
	Luc Michel

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 1c4811f410..8d02cd9a26 100644
--- a/hw/sd/sd.c
+++ b/hw/sd/sd.c
@@ -1715,11 +1715,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;
@@ -2138,6 +2136,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);
@@ -2167,13 +2166,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] 12+ messages in thread

* [PATCH v2 11/11] hw/sd/sdcard: Convert SEND_SCR to generic_read_byte (ACMD51)
  2024-06-27 16:27 [PATCH v2 00/11] hw/sd/sd: Introduce sd_cmd_to_sendingdata() and sd_generic_read_byte() Philippe Mathieu-Daudé
                   ` (9 preceding siblings ...)
  2024-06-27 16:27 ` [PATCH v2 10/11] hw/sd/sdcard: Convert SEND_NUM_WR_BLOCKS to generic_read_byte (ACMD22) Philippe Mathieu-Daudé
@ 2024-06-27 16:27 ` Philippe Mathieu-Daudé
  10 siblings, 0 replies; 12+ messages in thread
From: Philippe Mathieu-Daudé @ 2024-06-27 16:27 UTC (permalink / raw)
  To: qemu-devel
  Cc: qemu-block, Cédric Le Goater, Francisco Iglesias, Bin Meng,
	Sai Pavan Boddu, Joel Stanley, Philippe Mathieu-Daudé,
	Luc Michel

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 8d02cd9a26..cd308e9a89 100644
--- a/hw/sd/sd.c
+++ b/hw/sd/sd.c
@@ -1785,10 +1785,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;
@@ -2138,6 +2135,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;
@@ -2166,13 +2164,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] 12+ messages in thread

end of thread, other threads:[~2024-06-27 16:29 UTC | newest]

Thread overview: 12+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2024-06-27 16:27 [PATCH v2 00/11] hw/sd/sd: Introduce sd_cmd_to_sendingdata() and sd_generic_read_byte() Philippe Mathieu-Daudé
2024-06-27 16:27 ` [PATCH v2 01/11] hw/sd/sdcard: Introduce sd_cmd_to_sendingdata and sd_generic_read_byte Philippe Mathieu-Daudé
2024-06-27 16:27 ` [PATCH v2 02/11] hw/sd/sdcard: Convert SWITCH_FUNCTION to generic_read_byte (CMD6) Philippe Mathieu-Daudé
2024-06-27 16:27 ` [PATCH v2 03/11] hw/sd/sdcard: Convert SEND_CSD/SEND_CID to generic_read_byte (CMD9 & 10) Philippe Mathieu-Daudé
2024-06-27 16:27 ` [PATCH v2 04/11] hw/sd/sdcard: Duplicate READ_SINGLE_BLOCK / READ_MULTIPLE_BLOCK cases Philippe Mathieu-Daudé
2024-06-27 16:27 ` [PATCH v2 05/11] hw/sd/sdcard: Convert READ_SINGLE_BLOCK to generic_read_byte (CMD17) Philippe Mathieu-Daudé
2024-06-27 16:27 ` [PATCH v2 06/11] hw/sd/sdcard: Convert SEND_TUNING_BLOCK to generic_read_byte (CMD19) Philippe Mathieu-Daudé
2024-06-27 16:27 ` [PATCH v2 07/11] hw/sd/sdcard: Convert SEND_WRITE_PROT to generic_read_byte (CMD30) Philippe Mathieu-Daudé
2024-06-27 16:27 ` [PATCH v2 08/11] hw/sd/sdcard: Convert GEN_CMD to generic_read_byte (CMD56) Philippe Mathieu-Daudé
2024-06-27 16:27 ` [PATCH v2 09/11] hw/sd/sdcard: Convert SD_STATUS to generic_read_byte (ACMD13) Philippe Mathieu-Daudé
2024-06-27 16:27 ` [PATCH v2 10/11] hw/sd/sdcard: Convert SEND_NUM_WR_BLOCKS to generic_read_byte (ACMD22) Philippe Mathieu-Daudé
2024-06-27 16:27 ` [PATCH v2 11/11] hw/sd/sdcard: Convert SEND_SCR to generic_read_byte (ACMD51) Philippe Mathieu-Daudé

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).