Page 1 of 1

Bug on U-Boot SD Host driver for Raspberry Pi - may fail during write operations

Posted: Tue Jun 11, 2019 2:37 pm
by rbenet
Hi,
I just submitted a patch to U-Boot to fix a bug on the SD Host driver that made it fail under some circumstances.
In my particular case, it was failing when performing 'saveenv' on certain brand/model SD cards after the Pi had been power-cycled abruptly (ie: not gentle reboot). But note that the issue was on the U-Boot, not on the card or EXT4 filesystem. It just so happens that under those circumstances the SD card was taking longer (56ms) to perform a CMD25 on the superblock of the EXT4 partition and the SD Host driver was aborting after 12ms... Ultimately then U-Boot was just hanging.

As I normally look for info related to the Pi here on the Pi forums and this is head-scratching kind of bug, I thought I'd submit a copy of the patch here for other people's benefit.

[PATCH] ARM: bcm2835: (Raspberry Pi) Fix issue on SD Host driver, where wait_transfer_complete was not waiting long enough
Function bcm_2835_wait_transfer_complete() on driver/mmc/bcm2835_sdhost.c
(used on Raspberry Pi platform), was not waiting long enough for transfer to complete.
The previous code was claiming to wait for ~1 seconds, but my measurements
on a RPi 3B+ indicated a maximum wait of 12 ms. Measured using get_tiimer() function.
Some, but not all, of the cards we use, sometimes required wait times of ~56 ms to perform
the command 'saveenv' on an EXT4 partition. (the exact operation being a CMD25
write of the superblock of EXT4).

Re-implemented the loop exit condition to use get_timer() which allows to specify
the wait time in more reliable manner. Set the maximum wait time to the originally
intended 1 second.

Signed-off by: Raul Benet <raul.benet_at_kaptivo.com>

Code: Select all

---
 drivers/mmc/bcm2835_sdhost.c | 11 ++++++-----
 1 file changed, 6 insertions(+), 5 deletions(-)

diff --git a/drivers/mmc/bcm2835_sdhost.c b/drivers/mmc/bcm2835_sdhost.c
index 1ce019a..96e06b8 100644
--- a/drivers/mmc/bcm2835_sdhost.c
+++ b/drivers/mmc/bcm2835_sdhost.c
@@ -234,7 +234,7 @@ static void bcm2835_reset_internal(struct bcm2835_host *host)
 
 static int bcm2835_wait_transfer_complete(struct bcm2835_host *host)
 {
- int timediff = 0;
+        ulong tstart_ms = get_timer(0);
 
  while (1) {
  u32 edm, fsm;
@@ -254,11 +254,12 @@ static int bcm2835_wait_transfer_complete(struct bcm2835_host *host)
  break;
  }
 
- /* Error out after 100000 register reads (~1s) */
- if (timediff++ == 100000) {
+ /* Error out after ~1s */
+                ulong tlapse_ms = get_timer(tstart_ms);
+ if ( tlapse_ms > 1000 /* ms */ ) {
  dev_err(host->dev,
- "wait_transfer_complete - still waiting after %d retries\n",
- timediff);
+ "wait_transfer_complete - still waiting after %lu ms\n",
+ tlapse_ms);
  bcm2835_dumpregs(host);
  return -ETIMEDOUT;
  }