liblitesdcard/sdcard: adjust card-ready timeout
authorGabriel Somlo <gsomlo@gmail.com>
Mon, 6 Jul 2020 21:00:24 +0000 (17:00 -0400)
committerGabriel Somlo <gsomlo@gmail.com>
Mon, 6 Jul 2020 21:38:07 +0000 (17:38 -0400)
Testing on nexys4ddr and rocket, approximately 12 iterations of the
timeout loop (using `busy_wait(1)`) are needed to receive a "ready"
response from the SDcard, assuming a "warm" reset where the card has
already been previously initialized.

If the SDcard is ejected and re-inserted, or if the board is "cold-reset"
(e.g., reprogrammed via openocd vs. a simple push of the reset button),
it takes approximately 450 iterations before the SDCard responds with a
"ready" message.

In either case, a timeout of 10 is insufficient. This patch increases
the busy-wait to 10, and the timeout loop counter to 128, which should
cover most cases.

Additionally, make a few minor cosmetic improvements.

Signed-off-by: Gabriel Somlo <gsomlo@gmail.com>
litex/soc/software/liblitesdcard/sdcard.c

index 440c8f61ce16fbd84ff0cd8a93529b025541fbf7..4f0d401e7bcd93a96c7c92ee78de1b13efc6662b 100644 (file)
@@ -448,7 +448,7 @@ void sdcard_decode_csd(void) {
 
 int sdcard_init(void) {
        unsigned short rca;
-    uint16_t timeout;
+       uint16_t timeout;
 
        /* initialize freq */
        sdcard_set_clk_freq(16000000);
@@ -462,15 +462,13 @@ int sdcard_init(void) {
        busy_wait(1);
        sdcard_send_ext_csd();
        /* wait for card to be ready */
-       timeout = 10;
-       while (timeout) {
+       for (timeout = 128; timeout > 0; timeout--) {
                sdcard_app_cmd(0);
                sdcard_app_send_op_cond(1, 0);
                if (sdcard_response[3] & 0x80000000) {
                        break;
                }
-               busy_wait(1);
-               timeout--;
+               busy_wait(10);
        }
        if (timeout == 0)
                return 0;