On 6/28/24 9:02 AM, Philippe Mathieu-Daudé wrote:
> From: Luc Michel <luc.michel@amd.com>
> 
> The JEDEC standards specifies a sleep state where the eMMC won't answer
> any command appart from RESET and WAKEUP and go to low power state.
> Implement this state and the corresponding command number 5.
> 
> Signed-off-by: Luc Michel <luc.michel@amd.com>
> Signed-off-by: Francisco Iglesias <francisco.iglesias@amd.com>
> Signed-off-by: Philippe Mathieu-Daudé <philmd@linaro.org>
Reviewed-by: Cédric Le Goater <clg@redhat.com>
Thanks,
C.
> ---
>   hw/sd/sd.c | 47 +++++++++++++++++++++++++++++++++++++++++++++--
>   1 file changed, 45 insertions(+), 2 deletions(-)
> 
> diff --git a/hw/sd/sd.c b/hw/sd/sd.c
> index 0f9bab105e..bd77853419 100644
> --- a/hw/sd/sd.c
> +++ b/hw/sd/sd.c
> @@ -1182,8 +1182,19 @@ static sd_rsp_type_t sd_cmd_to_sendingdata(SDState *sd, SDRequest req,
>   /* CMD0 */
>   static sd_rsp_type_t sd_cmd_GO_IDLE_STATE(SDState *sd, SDRequest req)
>   {
> -    sd->state = sd_idle_state;
> -    sd_reset(DEVICE(sd));
> +    if (sd->state == sd_sleep_state) {
> +        switch (req.arg) {
> +        case 0x00000000:
> +        case 0xf0f0f0f0:
> +            break;
> +        default:
> +            return sd_r0;
> +        }
> +    }
> +    if (sd->state != sd_inactive_state) {
> +        sd->state = sd_idle_state;
> +        sd_reset(DEVICE(sd));
> +    }
>   
>       return sd_is_spi(sd) ? sd_r1 : sd_r0;
>   }
> @@ -1246,6 +1257,30 @@ static sd_rsp_type_t emmc_cmd_SET_RELATIVE_ADDR(SDState *sd, SDRequest req)
>       }
>   }
>   
> +/* CMD5 */
> +static sd_rsp_type_t emmc_cmd_sleep_awake(SDState *sd, SDRequest req)
> +{
> +    bool do_sleep = extract32(req.arg, 15, 1);
> +
> +    switch (sd->state) {
> +    case sd_sleep_state:
> +        if (!do_sleep) {
> +            /* Awake */
> +            sd->state = sd_standby_state;
> +        }
> +        return sd_r1b;
> +
> +    case sd_standby_state:
> +        if (do_sleep) {
> +            sd->state = sd_sleep_state;
> +        }
> +        return sd_r1b;
> +
> +    default:
> +        return sd_invalid_state_for_cmd(sd, req);
> +    }
> +}
> +
>   /* CMD6 */
>   static sd_rsp_type_t sd_cmd_SWITCH_FUNCTION(SDState *sd, SDRequest req)
>   {
> @@ -1648,6 +1683,7 @@ static sd_rsp_type_t sd_cmd_APP_CMD(SDState *sd, SDRequest req)
>       case sd_ready_state:
>       case sd_identification_state:
>       case sd_inactive_state:
> +    case sd_sleep_state:
>           return sd_invalid_state_for_cmd(sd, req);
>       case sd_idle_state:
>           if (!sd_is_spi(sd) && sd_req_get_rca(sd, req) != 0x0000) {
> @@ -1969,6 +2005,12 @@ int sd_do_command(SDState *sd, SDRequest *req,
>           req->cmd &= 0x3f;
>       }
>   
> +    if (sd->state == sd_sleep_state && req->cmd) {
> +        qemu_log_mask(LOG_GUEST_ERROR, "SD: Card is sleeping\n");
> +        rtype = sd_r0;
> +        goto send_response;
> +    }
> +
>       if (sd->card_status & CARD_IS_LOCKED) {
>           if (!cmd_valid_while_locked(sd, req->cmd)) {
>               sd->card_status |= ILLEGAL_COMMAND;
> @@ -2420,6 +2462,7 @@ static const SDProto sd_proto_emmc = {
>           [2]  = {0,  sd_bcr,  "ALL_SEND_CID", sd_cmd_ALL_SEND_CID},
>           [3]  = {0,  sd_ac,   "SET_RELATIVE_ADDR", emmc_cmd_SET_RELATIVE_ADDR},
>           [4]  = {0,  sd_bc,   "SEND_DSR", sd_cmd_unimplemented},
> +        [5]  = {0,  sd_ac,   "SLEEP/AWAKE", emmc_cmd_sleep_awake},
>           [7]  = {0,  sd_ac,   "(DE)SELECT_CARD", sd_cmd_DE_SELECT_CARD},
>           [9]  = {0,  sd_ac,   "SEND_CSD", sd_cmd_SEND_CSD},
>           [10] = {0,  sd_ac,   "SEND_CID", sd_cmd_SEND_CID},