1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
diff --git a/firmware/target/arm/as3525/debug-as3525.c b/firmware/target/arm/as3525/debug-as3525.c
index 71a8a85..9df4565 100644
--- a/firmware/target/arm/as3525/debug-as3525.c
+++ b/firmware/target/arm/as3525/debug-as3525.c
@@ -268,6 +268,30 @@ bool __dbg_hw_info(void)
     {
         while(1)
         {
+        int i;
+        extern int sd_cmds_submitted[NUM_VOLUMES];
+        extern int sd_cmds_errors[NUM_VOLUMES];
+        extern int sd_transfers[NUM_VOLUMES];
+        extern int sd_transfer_errors[NUM_VOLUMES];
+        lcd_clear_display();
+        line = 0;
+        lcd_puts(0, line++, "[SD error stats]");
+        for (i=0; i<NUM_VOLUMES; i++) {
+            lcd_putsf(0, line++, "Drive %d errors:", i);
+            lcd_putsf(0, line++, "cmd %d of %d",
+                       sd_cmds_errors[i], sd_cmds_submitted[i]);
+            lcd_putsf(0, line++, "r/w %d of %d",
+                      sd_transfer_errors[i], sd_transfers[i]);
+        }
+        lcd_update();
+        int btn = button_get_w_tmo(HZ/10);
+        if(btn == (DEBUG_CANCEL|BUTTON_REL))
+            goto end;
+        else if(btn == (BUTTON_DOWN|BUTTON_REL))
+            break;
+        }
+        while(1)
+        {
 #ifdef SANSA_C200V2
         lcd_clear_display();
         line = 0;
diff --git a/firmware/target/arm/as3525/sd-as3525.c b/firmware/target/arm/as3525/sd-as3525.c
index 1fd10bf..f989f6a 100644
--- a/firmware/target/arm/as3525/sd-as3525.c
+++ b/firmware/target/arm/as3525/sd-as3525.c
@@ -51,6 +51,7 @@
 #endif

 #define VERIFY_WRITE 1
+#define VERIFY_READ 1

 /* command flags */
 #define MCI_NO_RESP     (0<<0)
@@ -126,6 +127,11 @@ static struct mutex       sd_mtx;
 static struct event_queue sd_queue;
 bool sd_enabled = false;

+int sd_cmds_submitted[NUM_VOLUMES];
+int sd_cmds_errors[NUM_VOLUMES];
+int sd_transfers[NUM_VOLUMES];
+int sd_transfer_errors[NUM_VOLUMES];
+
 #if defined(HAVE_MULTIDRIVE)
 static bool hs_card = false;
 #define EXT_SD_BITS (1<<2)
@@ -138,6 +144,7 @@ static volatile unsigned int transfer_error[NUM_VOLUMES];
 #define UNALIGNED_NUM_SECTORS 10
 static unsigned char aligned_buffer[UNALIGNED_NUM_SECTORS* SD_BLOCK_SIZE] __attribute__((aligned(32)));   /* align on cache line size */
 static unsigned char *uncached_buffer = AS3525_UNCACHED_ADDR(&aligned_buffer[0]);
+static unsigned char compare_buffer[UNALIGNED_NUM_SECTORS* SD_BLOCK_SIZE] __attribute__((aligned(32)));


 static inline void mci_delay(void) { udelay(1000) ; }
@@ -174,7 +181,7 @@ void sd_gpioa_isr(void)
 {
     static struct timeout sd1_oneshot;
     if (GPIOA_MIS & EXT_SD_BITS)
-        timeout_register(&sd1_oneshot, sd1_oneshot_callback, (3*HZ/10), 0);
+        timeout_register(&sd1_oneshot, sd1_oneshot_callback, (9*HZ/10), 0);
     /* acknowledge interrupt */
     GPIOA_IC = EXT_SD_BITS;
 }
@@ -207,6 +214,8 @@ static bool send_cmd(const int drive, const int cmd, const int arg,
 {
     int status;

+    sd_cmds_submitted[drive]++;
+
     unsigned cmd_retries = 6;
     while(cmd_retries--)
     {
@@ -236,8 +245,10 @@ static bool send_cmd(const int drive, const int cmd, const int arg,
         {
             response[0] = MCI_RESP0(drive); /* Always prepare short response */

-            if(status & MCI_RESPONSE_ERROR) /* timeout or crc failure */
+            if(status & MCI_RESPONSE_ERROR) {/* timeout or crc failure */
+                sd_cmds_errors[drive]++;
                 continue;
+            }

             if(status & MCI_CMD_RESP_END)   /* Response passed CRC check */
             {
@@ -257,6 +268,32 @@ static bool send_cmd(const int drive, const int cmd, const int arg,
     return false;
 }

+static void sd_set_widebus_mode(IF_MD2(int drive,) int on) {
+#ifndef HAVE_MULTIDRIVE
+    const int drive = 0;
+#endif
+    long response = 0;
+    int ret;
+    if((ret = sd_wait_for_tran_state(drive)) < 0) {
+        int ret2 = send_cmd(drive, SD_SEND_STATUS, card_info[drive].rca,
+                            MCI_RESP, &response);
+        panicf("sd_set_widebus(%d): tran: %d %d %lx %lx", on, ret, ret2, response, MCI_STATUS(drive));
+    }
+    /* CMD55 */              /*  Response is requested due to timing issue  */
+    if(!send_cmd(drive, SD_APP_CMD, card_info[drive].rca, MCI_RESP, &response))
+        panicf("sd_set_widebus(%d): app_cmd", on);
+    /* ACMD6  */
+    if(!send_cmd(drive, SD_SET_BUS_WIDTH, on ? 2 : 0, MCI_RESP, &response))
+        panicf("sd_set_widebus(%d): set_bus_width", on);
+    /* Now that card is widebus make controller aware */
+    if (on) {
+        MCI_CLOCK(drive) |= MCI_CLOCK_WIDEBUS;
+    } else {
+        MCI_CLOCK(drive) &= ~MCI_CLOCK_WIDEBUS;
+    }
+}
+
+
 #define MCI_FULLSPEED     (MCI_CLOCK_ENABLE | MCI_CLOCK_BYPASS)     /* MCLK   */
 #define MCI_HALFSPEED     (MCI_CLOCK_ENABLE)                        /* MCLK/2 */
 #define MCI_QUARTERSPEED  (MCI_CLOCK_ENABLE | 1)                    /* MCLK/4 */
@@ -362,23 +399,16 @@ static int sd_init_card(const int drive)
     if(!send_cmd(drive, SD_SELECT_CARD, card_info[drive].rca, MCI_RESP, &response))
         return -10;

-    /*  Switch to to 4 bit widebus mode  */
-    if(sd_wait_for_tran_state(drive) < 0)
-        return -11;
+    /* Disable card detect pullup, we don't need it */
     /* CMD55 */             /*  Response is requested due to timing issue  */
     if(!send_cmd(drive, SD_APP_CMD, card_info[drive].rca, MCI_RESP, &response))
         return -14;
     /* ACMD42  */
     if(!send_cmd(drive, SD_SET_CLR_CARD_DETECT, 0, MCI_RESP, &response))
         return -15;
-    /* CMD55 */              /*  Response is requested due to timing issue  */
-    if(!send_cmd(drive, SD_APP_CMD, card_info[drive].rca, MCI_RESP, &response))
-        return -12;
-    /* ACMD6  */
-    if(!send_cmd(drive, SD_SET_BUS_WIDTH, 2, MCI_RESP, &response))
-        return -13;
-    /* Now that card is widebus make controller aware */
-    MCI_CLOCK(drive) |= MCI_CLOCK_WIDEBUS;
+
+    /*  Switch to to 4 bit widebus mode  */
+    sd_set_widebus_mode(IF_MD2(drive,) 1);

     /*
      * enable bank switching
@@ -596,15 +626,17 @@ static int sd_wait_for_tran_state(const int drive)

     while (1)
     {
-        if(!send_cmd(drive, SD_SEND_STATUS, card_info[drive].rca, MCI_RESP,
-                    &response))
-            return -1;
+        int ret = send_cmd(drive, SD_SEND_STATUS, card_info[drive].rca,
+                           MCI_RESP, &response);

-        if (((response >> 9) & 0xf) == SD_TRAN)
+        if (ret && ((response >> 9) & 0xf) == SD_TRAN)
             return 0;

-        if(TIME_AFTER(current_tick, timeout))
+        if(TIME_AFTER(current_tick, timeout)) {
+            if (!ret)
+                return -1;
             return -2;
+        }

         if (TIME_AFTER(current_tick, next_yield))
         {
@@ -689,7 +721,9 @@ static int sd_transfer_sectors(IF_MD2(int drive,) unsigned long start,
     unsigned long response;
     bool aligned = !((uintptr_t)buf & (CACHEALIGN_SIZE - 1));

-    mutex_lock(&sd_mtx);
+    aligned = 0;
+
+    sd_transfers[drive]++;
     sd_enable(true);
     led(true);

@@ -719,8 +753,7 @@ static int sd_transfer_sectors(IF_MD2(int drive,) unsigned long start,

     dma_retain();

-    if(aligned)
-    {
+    if(aligned) {
         if(write)
             clean_dcache_range(buf, count * SECTOR_SIZE);
         else
@@ -853,9 +886,12 @@ static int sd_transfer_sectors(IF_MD2(int drive,) unsigned long start,
             count -= transfer;
             loops = 0;  /* reset errors counter */
         }
-        else if(loops++ > PL180_MAX_TRANSFER_ERRORS)
+        else {
+            sd_transfer_errors[drive]++;
+            if(loops++ > PL180_MAX_TRANSFER_ERRORS)
                 panicf("SD Xfer %s err:0x%x Disk%d", (write? "write": "read"),
                                                   transfer_error[drive], drive);
+        }
     }

 sd_transfer_error:
@@ -870,19 +906,65 @@ sd_transfer_error_nodma:
     if (ret)    /* error */
         card_info[drive].initialized = 0;

-    mutex_unlock(&sd_mtx);
     return ret;
 }

 int sd_read_sectors(IF_MD2(int drive,) unsigned long start, int count,
                      void* buf)
 {
-    return sd_transfer_sectors(IF_MD2(drive,) start, count, buf, false);
+    mutex_lock(&sd_mtx);
+    int ret;
+
+    ret = sd_transfer_sectors(IF_MD2(drive,) start, count, buf, false);
+
+#ifdef VERIFY_READ
+    if (ret) /* read failed, no point in verifying? */
+    {
+        mutex_unlock(&sd_mtx);
+        return ret;
+    }
+
+    while (count) {
+        int transfer = count;
+        if(transfer > UNALIGNED_NUM_SECTORS)
+            transfer = UNALIGNED_NUM_SECTORS;
+
+        sd_transfer_sectors(IF_MD2(drive,) start, transfer, compare_buffer, false);
+
+        int i;
+        for (i=0; i<transfer*512; i++) {
+            static char panicbuf[500];
+            int n = sizeof(panicbuf);
+            char *s = panicbuf;
+
+            if (compare_buffer[i] == ((char*)buf)[i])
+                continue;
+
+            int j = i;
+            while (n>=5 && j<transfer*512) { /* 4 characters + null */
+                snprintf(s, n, "%02x%02x", ((char*)buf)[j], (char)compare_buffer[j]);
+                s += 4;
+                n -= 4;
+                j++;
+            }
+
+            panicf("sd%d: read verify failed: sec=%ld n=%d %d: %s", drive, start, transfer, i, panicbuf);
+        }
+
+        buf   += transfer * 512;
+        count -= transfer;
+        start += transfer;
+    }
+
+#endif
+    mutex_unlock(&sd_mtx);
+    return ret;
 }

 int sd_write_sectors(IF_MD2(int drive,) unsigned long start, int count,
                      const void* buf)
 {
+    mutex_lock(&sd_mtx);
 #ifdef VERIFY_WRITE
     unsigned long saved_start = start;
     int saved_count = count;
@@ -894,7 +976,10 @@ int sd_write_sectors(IF_MD2(int drive,) unsigned long start, int count,

 #ifdef VERIFY_WRITE
     if (ret) /* write failed, no point in verifying */
+    {
+        mutex_unlock(&sd_mtx);
         return ret;
+    }

     count = saved_count;
     buf = saved_buf;
@@ -904,11 +989,11 @@ int sd_write_sectors(IF_MD2(int drive,) unsigned long start, int count,
         if(transfer > UNALIGNED_NUM_SECTORS)
             transfer = UNALIGNED_NUM_SECTORS;

-        sd_transfer_sectors(IF_MD2(drive,) start, transfer, aligned_buffer, false);
-        if (memcmp(buf, aligned_buffer, transfer * 512) != 0) {
+        sd_transfer_sectors(IF_MD2(drive,) start, transfer, compare_buffer, false);
+        if (memcmp(buf, compare_buffer, transfer * 512) != 0) {
             /* try the write again in the hope to repair the damage */
             sd_transfer_sectors(IF_MD2(drive,) saved_start, saved_count, saved_buf, true);
-            panicf("sd: verify failed: sec=%ld n=%d!", start, transfer);
+            panicf("sd%d: write verify failed: sec=%ld n=%d!", drive, start, transfer);
         }

         buf   += transfer * 512;
@@ -916,6 +1001,7 @@ int sd_write_sectors(IF_MD2(int drive,) unsigned long start, int count,
         start += transfer;
     }
 #endif
+    mutex_unlock(&sd_mtx);
     return ret;
 }