diff mbox series

[v42,62/98] hw/sd/sdcard: Add sd_cmd_ERASE handler (CMD38)

Message ID 20240628070216.92609-63-philmd@linaro.org (mailing list archive)
State New, archived
Headers show
Series hw/sd/sdcard: Add eMMC support | expand

Commit Message

Philippe Mathieu-Daudé June 28, 2024, 7:01 a.m. UTC
Signed-off-by: Philippe Mathieu-Daudé <philmd@linaro.org>
---
 hw/sd/sd.c | 41 ++++++++++++++++++++---------------------
 1 file changed, 20 insertions(+), 21 deletions(-)

Comments

Cédric Le Goater June 28, 2024, 8:02 a.m. UTC | #1
On 6/28/24 9:01 AM, Philippe Mathieu-Daudé wrote:
> Signed-off-by: Philippe Mathieu-Daudé <philmd@linaro.org>


Reviewed-by: Cédric Le Goater <clg@redhat.com>

Thanks,

C.


> ---
>   hw/sd/sd.c | 41 ++++++++++++++++++++---------------------
>   1 file changed, 20 insertions(+), 21 deletions(-)
> 
> diff --git a/hw/sd/sd.c b/hw/sd/sd.c
> index 4e31dfe18f..17fec612eb 100644
> --- a/hw/sd/sd.c
> +++ b/hw/sd/sd.c
> @@ -244,7 +244,6 @@ static const char *sd_cmd_name(SDState *sd, uint8_t cmd)
>                                               [21]    = "DPS_spec",
>                                               [25]    = "WRITE_MULTIPLE_BLOCK",
>           [26]    = "MANUF_RSVD",
> -        [38]    = "ERASE",
>           [40]    = "DPS_spec",
>           [42]    = "LOCK_UNLOCK",
>           [54]    = "SDIO_RSVD",              [55]    = "APP_CMD",
> @@ -1602,6 +1601,24 @@ static sd_rsp_type_t sd_cmd_ERASE_WR_BLK_END(SDState *sd, SDRequest req)
>       return sd_r1;
>   }
>   
> +/* CMD38 */
> +static sd_rsp_type_t sd_cmd_ERASE(SDState *sd, SDRequest req)
> +{
> +    if (sd->state != sd_transfer_state) {
> +        return sd_invalid_state_for_cmd(sd, req);
> +    }
> +    if (sd->csd[14] & 0x30) {
> +        sd->card_status |= WP_VIOLATION;
> +        return sd_r1b;
> +    }
> +
> +    sd->state = sd_programming_state;
> +    sd_erase(sd);
> +    /* Bzzzzzzztt .... Operation complete.  */
> +    sd->state = sd_transfer_state;
> +    return sd_r1b;
> +}
> +
>   static sd_rsp_type_t sd_normal_command(SDState *sd, SDRequest req)
>   {
>       uint16_t rca;
> @@ -1690,26 +1707,6 @@ static sd_rsp_type_t sd_normal_command(SDState *sd, SDRequest req)
>       case 26:  /* CMD26:  PROGRAM_CID */
>           return sd_cmd_to_receivingdata(sd, req, 0, sizeof(sd->cid));
>   
> -    /* Erase commands (Class 5) */
> -    case 38:  /* CMD38:  ERASE */
> -        switch (sd->state) {
> -        case sd_transfer_state:
> -            if (sd->csd[14] & 0x30) {
> -                sd->card_status |= WP_VIOLATION;
> -                return sd_r1b;
> -            }
> -
> -            sd->state = sd_programming_state;
> -            sd_erase(sd);
> -            /* Bzzzzzzztt .... Operation complete.  */
> -            sd->state = sd_transfer_state;
> -            return sd_r1b;
> -
> -        default:
> -            break;
> -        }
> -        break;
> -
>       /* Lock card commands (Class 7) */
>       case 42:  /* CMD42:  LOCK_UNLOCK */
>           return sd_cmd_to_receivingdata(sd, req, 0, 0);
> @@ -2318,6 +2315,7 @@ static const SDProto sd_proto_spi = {
>           [35] = {10, sd_spi, "WRITE_SEC_CMD", sd_cmd_optional},
>           [36] = {10, sd_spi, "SEND_PSI", sd_cmd_optional},
>           [37] = {10, sd_spi, "CONTROL_ASSD_SYSTEM", sd_cmd_optional},
> +        [38] = {5,  sd_spi, "ERASE", sd_cmd_ERASE},
>           [50] = {10, sd_spi, "DIRECT_SECURE_READ", sd_cmd_optional},
>           [52] = {9,  sd_spi, "IO_RW_DIRECT", sd_cmd_optional},
>           [53] = {9,  sd_spi, "IO_RW_EXTENDED", sd_cmd_optional},
> @@ -2361,6 +2359,7 @@ static const SDProto sd_proto_sd = {
>           [35] = {10, sd_adtc, "WRITE_SEC_CMD", sd_cmd_optional},
>           [36] = {10, sd_adtc, "SEND_PSI", sd_cmd_optional},
>           [37] = {10, sd_ac,   "CONTROL_ASSD_SYSTEM", sd_cmd_optional},
> +        [38] = {5,  sd_ac,   "ERASE", sd_cmd_ERASE},
>           [43] = {1,  sd_ac,   "Q_MANAGEMENT", sd_cmd_optional},
>           [44] = {1,  sd_ac,   "Q_TASK_INFO_A", sd_cmd_optional},
>           [45] = {1,  sd_ac,   "Q_TASK_INFO_B", sd_cmd_optional},
diff mbox series

Patch

diff --git a/hw/sd/sd.c b/hw/sd/sd.c
index 4e31dfe18f..17fec612eb 100644
--- a/hw/sd/sd.c
+++ b/hw/sd/sd.c
@@ -244,7 +244,6 @@  static const char *sd_cmd_name(SDState *sd, uint8_t cmd)
                                             [21]    = "DPS_spec",
                                             [25]    = "WRITE_MULTIPLE_BLOCK",
         [26]    = "MANUF_RSVD",
-        [38]    = "ERASE",
         [40]    = "DPS_spec",
         [42]    = "LOCK_UNLOCK",
         [54]    = "SDIO_RSVD",              [55]    = "APP_CMD",
@@ -1602,6 +1601,24 @@  static sd_rsp_type_t sd_cmd_ERASE_WR_BLK_END(SDState *sd, SDRequest req)
     return sd_r1;
 }
 
+/* CMD38 */
+static sd_rsp_type_t sd_cmd_ERASE(SDState *sd, SDRequest req)
+{
+    if (sd->state != sd_transfer_state) {
+        return sd_invalid_state_for_cmd(sd, req);
+    }
+    if (sd->csd[14] & 0x30) {
+        sd->card_status |= WP_VIOLATION;
+        return sd_r1b;
+    }
+
+    sd->state = sd_programming_state;
+    sd_erase(sd);
+    /* Bzzzzzzztt .... Operation complete.  */
+    sd->state = sd_transfer_state;
+    return sd_r1b;
+}
+
 static sd_rsp_type_t sd_normal_command(SDState *sd, SDRequest req)
 {
     uint16_t rca;
@@ -1690,26 +1707,6 @@  static sd_rsp_type_t sd_normal_command(SDState *sd, SDRequest req)
     case 26:  /* CMD26:  PROGRAM_CID */
         return sd_cmd_to_receivingdata(sd, req, 0, sizeof(sd->cid));
 
-    /* Erase commands (Class 5) */
-    case 38:  /* CMD38:  ERASE */
-        switch (sd->state) {
-        case sd_transfer_state:
-            if (sd->csd[14] & 0x30) {
-                sd->card_status |= WP_VIOLATION;
-                return sd_r1b;
-            }
-
-            sd->state = sd_programming_state;
-            sd_erase(sd);
-            /* Bzzzzzzztt .... Operation complete.  */
-            sd->state = sd_transfer_state;
-            return sd_r1b;
-
-        default:
-            break;
-        }
-        break;
-
     /* Lock card commands (Class 7) */
     case 42:  /* CMD42:  LOCK_UNLOCK */
         return sd_cmd_to_receivingdata(sd, req, 0, 0);
@@ -2318,6 +2315,7 @@  static const SDProto sd_proto_spi = {
         [35] = {10, sd_spi, "WRITE_SEC_CMD", sd_cmd_optional},
         [36] = {10, sd_spi, "SEND_PSI", sd_cmd_optional},
         [37] = {10, sd_spi, "CONTROL_ASSD_SYSTEM", sd_cmd_optional},
+        [38] = {5,  sd_spi, "ERASE", sd_cmd_ERASE},
         [50] = {10, sd_spi, "DIRECT_SECURE_READ", sd_cmd_optional},
         [52] = {9,  sd_spi, "IO_RW_DIRECT", sd_cmd_optional},
         [53] = {9,  sd_spi, "IO_RW_EXTENDED", sd_cmd_optional},
@@ -2361,6 +2359,7 @@  static const SDProto sd_proto_sd = {
         [35] = {10, sd_adtc, "WRITE_SEC_CMD", sd_cmd_optional},
         [36] = {10, sd_adtc, "SEND_PSI", sd_cmd_optional},
         [37] = {10, sd_ac,   "CONTROL_ASSD_SYSTEM", sd_cmd_optional},
+        [38] = {5,  sd_ac,   "ERASE", sd_cmd_ERASE},
         [43] = {1,  sd_ac,   "Q_MANAGEMENT", sd_cmd_optional},
         [44] = {1,  sd_ac,   "Q_TASK_INFO_A", sd_cmd_optional},
         [45] = {1,  sd_ac,   "Q_TASK_INFO_B", sd_cmd_optional},