[PATCH 2/2] mci: wait for card to leave programming state after block writes

Sascha Hauer s.hauer at pengutronix.de
Mon Oct 19 02:52:44 PDT 2026


Commit d8c3bab30542 ("mci: only write blocks when card out of programming
mode") polls the card status before a block write, so that CMD24/25 is
not sent while the card is still programming the previous write. Reads
are not covered: a read following a write can still find the card in
prg state. CMD17/18 are illegal there and the card does not answer
them, so the host sees a response timeout.

On a KSZ9477-EVB (SAMA5D3) with a freshly flashed SD card, the first
saveenv fails with:

  ERROR: atmel_mci f0000000.mmc at f0000000.of: command/data timeout
  ERROR: atmel_mci f0000000.mmc at f0000000.of: command/data timeout

With more verbose error reporting, the failing commands turn out to be
a multi-block read and the stop command sent after it:

  CMD18: response timeout (SR=0x04100005)
  CMD12: response timeout (SR=0x04100005)

The same happens without any filesystem involved when writing larger
amounts of data to the raw device:

  memcpy -s /dev/ram0 -d /dev/mmc0.root-A 0 0 2M

Also poll the card status after each block write, like U-Boot does, so
that any command following a write finds the card back in transfer
state.

Assisted-by: Claude:claude-opus-5-5
Signed-off-by: Sascha Hauer <s.hauer at pengutronix.de>
---
 drivers/mci/mci-core.c | 11 ++++++++++-
 1 file changed, 10 insertions(+), 1 deletion(-)

diff --git a/drivers/mci/mci-core.c b/drivers/mci/mci-core.c
index 874ee40cb3..1954de8053 100644
--- a/drivers/mci/mci-core.c
+++ b/drivers/mci/mci-core.c
@@ -266,7 +266,7 @@ static int mci_block_write(struct mci *mci, const void *src, int blocknum,
 {
 	struct mci_cmd cmd = {};
 	unsigned mmccmd;
-	int ret;
+	int ret, err;
 
 	/*
 	 * Quoting eMMC Spec v5.1 (JEDEC Standard No. 84-B51):
@@ -304,6 +304,15 @@ static int mci_block_write(struct mci *mci, const void *src, int blocknum,
 		mci_send_cmd(mci, &cmd);
         }
 
+	/*
+	 * The card stays in prg state for a while after the write. Polling
+	 * before the next write is not enough: a read sent in prg state is
+	 * an illegal command, which the card does not answer at all.
+	 */
+	err = mci_poll_until_ready(mci, 1000 /* ms */);
+	if (!ret && err != -ENOSYS)
+		ret = err;
+
 	return ret;
 }
 

-- 
2.47.3




More information about the barebox mailing list