[PATCH] mci: only write blocks when card out of programming mode
Sascha Hauer
sha at pengutronix.de
Wed Dec 14 02:03:29 PST 2022
On Tue, Dec 13, 2022 at 03:12:04PM +0100, Ahmad Fatoum wrote:
> /**
> * @param p Command definition to setup
> * @param cmd Valid SD/MMC command (refer MMC_CMD_* / SD_CMD_*)
> @@ -119,6 +141,69 @@ static int mci_set_blocklen(struct mci *mci, unsigned len)
>
> static void *sector_buf;
>
> +static int mci_send_status(struct mci *mci, unsigned int *status)
> +{
> + struct mci_host *host = mci->host;
> + struct mci_cmd cmd;
> + int ret;
> +
> + cmd.cmdidx = MMC_CMD_SEND_STATUS;
> + cmd.resp_type = MMC_RSP_R1;
> + if (!mmc_host_is_spi(host))
> + cmd.cmdarg = mci->rca << 16;
cmd.cmdarg will be uninitialized for SPI MMC hosts.
> +
> + ret = mci_send_cmd_retry(mci, &cmd, NULL, 4);
> + if (!ret)
> + *status = cmd.response[0];
> +
> + return ret;
> +}
> +
> +static int mci_poll_until_ready(struct mci *mci, int timeout_ms)
> +{
> + unsigned int status;
> + int err, retries = 0;
> +
> + while (1) {
> + err = mci_send_status(mci, &status);
> + if (err)
> + return err;
> +
> + /*
> + * Some cards mishandle the status bits, so make sure to
> + * check both the busy indication and the card state.
> + */
> + if ((status & R1_READY_FOR_DATA) &&
> + R1_CURRENT_STATE(status) != R1_STATE_PRG)
> + break;
> +
> + if (status & R1_STATUS_MASK) {
> + dev_err(&mci->dev, "Status Error: 0x%08x\n", status);
> + return -EIO;
> + }
> +
> + if (retries++ == timeout_ms)
> + break;
> +
> + udelay(1000);
> + }
> +
> + if (retries >= timeout_ms) {
> + dev_err(&mci->dev, "Timeout waiting card ready\n");
> + return -ETIMEDOUT;
> + }
You could move this into the timeout test in the loop above.
> +
> + /*
> + * With small UART FIFOs and slow CPUs, printing in the loop above
> + * may take enough time to render the polling loop above unnecessary
> + * falsifying the debugging info. Thus we report the retries here.
> + */
People reading this code are likely aware of that, I think you can remove
this comment.
> + dev_info(&mci->dev, "Ready polling took %ums\n", retries);
dev_dbg()?
> +
> + return 0;
> +}
> +
> +
> /**
> * Write one or several blocks of data to the card
> * @param mci_dev MCI instance
> @@ -136,6 +221,17 @@ static int mci_block_write(struct mci *mci, const void *src, int blocknum,
> unsigned mmccmd;
> int ret;
>
> + /*
> + * Quoting eMMC Spec v5.1 (JEDEC Standard No. 84-B51):
> + * Due to legacy reasons, a Device may still treat CMD24/25 during
> + * prg-state (while busy is active) as a legal or illegal command.
> + * A host should not send CMD24/25 while the Device is in the prg
> + * state and busy is active.
> + */
> + ret = mci_poll_until_ready(mci, 10 /* ms */);
Is 10ms enough? U-Boot has 1s here.
Sascha
--
Pengutronix e.K. | |
Steuerwalder Str. 21 | http://www.pengutronix.de/ |
31137 Hildesheim, Germany | Phone: +49-5121-206917-0 |
Amtsgericht Hildesheim, HRA 2686 | Fax: +49-5121-206917-5555 |
More information about the barebox
mailing list