mail archive of the barebox mailing list
 help / color / mirror / Atom feed
From: Sascha Hauer <sha@pengutronix.de>
To: Ahmad Fatoum <a.fatoum@pengutronix.de>
Cc: barebox@lists.infradead.org
Subject: Re: [PATCH] mci: only write blocks when card out of programming mode
Date: Wed, 14 Dec 2022 11:03:29 +0100	[thread overview]
Message-ID: <20221214100329.GR29728@pengutronix.de> (raw)
In-Reply-To: <20221213141204.1860400-1-a.fatoum@pengutronix.de>

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 |



  parent reply	other threads:[~2022-12-14 10:04 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-12-13 14:12 Ahmad Fatoum
2022-12-14  9:47 ` Ahmad Fatoum
2022-12-14 10:03 ` Sascha Hauer [this message]
2022-12-14 10:55   ` Ahmad Fatoum

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20221214100329.GR29728@pengutronix.de \
    --to=sha@pengutronix.de \
    --cc=a.fatoum@pengutronix.de \
    --cc=barebox@lists.infradead.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox