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
diff --git a/firmware/target/arm/as3525/sd-as3525.c b/firmware/target/arm/as3525/sd-as3525.c
index fb1ec95..1e5acde 100644
--- a/firmware/target/arm/as3525/sd-as3525.c
+++ b/firmware/target/arm/as3525/sd-as3525.c
@@ -208,6 +208,9 @@ static bool send_cmd(const int drive, const int cmd, const int arg,
 {
     int status;

+    unsigned cmd_retries = 6;
+    while(cmd_retries--)
+    {
     /* Clear old status flags */
     MCI_CLEAR(drive) = 0x7ff;

@@ -235,7 +238,7 @@ 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 */
-            return false;
+                continue;

         if(status & MCI_CMD_RESP_END)      /* Response passed CRC check */
         {
@@ -250,6 +253,7 @@ static bool send_cmd(const int drive, const int cmd, const int arg,
     }
     else if(status & MCI_CMD_SENT)          /* CMD sent, no response required */
         return true;
+    }

     return false;
 }
@@ -573,7 +577,7 @@ bool sd_present(IF_MD_NONVOID(int drive))
 static int sd_wait_for_tran_state(const int drive)
 {
     unsigned long response = 0;
-    unsigned int timeout = current_tick + HZ;
+    unsigned int timeout = current_tick + 5 * HZ;

     while (1)
     {
@@ -815,7 +819,10 @@ static int sd_transfer_sectors(IF_MD2(int drive,) unsigned long start,
                                                   transfer_error[drive], drive);
     }

-    ret = 0;    /* success */
+    /* be sure the card has finished programming */
+    ret = sd_wait_for_tran_state(drive);
+    if (ret < 0)
+        ret -= 5*20;

 sd_transfer_error:

@@ -912,10 +919,6 @@ void sd_enable(bool on)
         }
 #endif  /* defined(HAVE_HOTSWAP) && defined (HAVE_ADJUSTABLE_CPU_VOLTAGE) */

-        /* not sure why we have to wait, but without this, test_disk freezes
-         * when closing the 300MB file which was just written to */
-        udelay(100);
-
         sd_enabled = false;

 #ifdef HAVE_MULTIDRIVE
diff --git a/firmware/target/arm/as3525/sd-as3525v2.c b/firmware/target/arm/as3525/sd-as3525v2.c
index 8ad2196..1826978 100644
--- a/firmware/target/arm/as3525/sd-as3525v2.c
+++ b/firmware/target/arm/as3525/sd-as3525v2.c
@@ -397,15 +397,8 @@ static inline bool card_detect_target(void)
 static bool send_cmd(const int drive, const int cmd, const int arg, const int flags,
         unsigned long *response)
 {
-#if defined(HAVE_MULTIDRIVE)
-    if(sd_present(SD_SLOT_AS3525))
-        GPIOB_PIN(5) = (1-drive) << 5;
-#endif
-
-    MCI_ARGUMENT = arg;
-
     /* Construct MCI_COMMAND  */
-    MCI_COMMAND =
+    unsigned long command =
       /*b5:0*/    cmd
       /*b6  */  | ((flags & MCI_RESP)                ? CMD_RESP_EXP_BIT:      0)
       /*b7  */  | ((flags & MCI_LONG_RESP)           ? CMD_RESP_LENGTH_BIT:   0)
@@ -423,6 +416,17 @@ static bool send_cmd(const int drive, const int cmd, const int arg, const int fl
       /*b23     | CMD_CCS_EXPECTED        unused  */
       /*b31 */  |                                      CMD_DONE_BIT;

+    unsigned cmd_retries = 6;
+    while(cmd_retries--)
+    {
+#if defined(HAVE_MULTIDRIVE)
+        if(sd_present(SD_SLOT_AS3525))
+            GPIOB_PIN(5) = (1-drive) << 5;
+#endif
+
+        MCI_ARGUMENT = arg;
+        MCI_COMMAND = command;
+
 #ifdef SANSA_FUZEV2
     extern int buttonlight_is_on;
     if(buttonlight_is_on)
@@ -436,7 +440,7 @@ static bool send_cmd(const int drive, const int cmd, const int arg, const int fl
     if(flags & MCI_RESP)
     {
         if(cmd_error & (MCI_INT_RCRC | MCI_INT_RTO))
-            return false;
+                continue;

         if(flags & MCI_LONG_RESP)
         {
@@ -448,9 +452,13 @@ static bool send_cmd(const int drive, const int cmd, const int arg, const int fl
         else
             response[0] = MCI_RESP0;
     }
+
     return true;
 }

+    return false;
+}
+
 static int sd_init_card(const int drive)
 {
     unsigned long response;
@@ -759,8 +767,7 @@ int sd_init(void)
 static int sd_wait_for_tran_state(const int drive)
 {
     unsigned long response;
-    unsigned int timeout = 100; /* ticks */
-    long t = current_tick;
+    long timeout = current_tick + 5*HZ; /* 5 seconds should be large enough */

     while (1)
     {
@@ -771,7 +778,7 @@ static int sd_wait_for_tran_state(const int drive)
         if (((response >> 9) & 0xf) == SD_TRAN)
             return 0;

-        if(TIME_AFTER(current_tick, t + timeout))
+        if(TIME_AFTER(current_tick, timeout))
             return -10 * ((response >> 9) & 0xf);

         if (TIME_AFTER((tick = current_tick), next_yield))
@@ -846,6 +853,20 @@ static int sd_transfer_sectors(IF_MD2(int drive,) unsigned long start,

         MCI_BYTCNT = transfer * SD_BLOCK_SIZE;

+        int arg = start;
+        if(!(card_info[drive].ocr & (1<<30))) /* not SDHC */
+            arg *= SD_BLOCK_SIZE;
+
+        if (write)
+            dma_enable_channel(0, dma_buf, MCI_FIFO, DMA_PERI_SD,
+                DMAC_FLOWCTRL_PERI_MEM_TO_PERI, true, false, 0, DMA_S8, NULL);
+        else
+            dma_enable_channel(0, MCI_FIFO, dma_buf, DMA_PERI_SD,
+                DMAC_FLOWCTRL_PERI_PERI_TO_MEM, false, true, 0, DMA_S8, NULL);
+
+        while (MCI_STATUS & DATA_BUSY)
+            yield();
+
         ret = sd_wait_for_tran_state(drive);
         if (ret < 0)
         {
@@ -860,17 +881,6 @@ static int sd_transfer_sectors(IF_MD2(int drive,) unsigned long start,
             goto sd_transfer_error;
         }

-        int arg = start;
-        if(!(card_info[drive].ocr & (1<<30))) /* not SDHC */
-            arg *= SD_BLOCK_SIZE;
-
-        if(write)
-            dma_enable_channel(0, dma_buf, MCI_FIFO, DMA_PERI_SD,
-                DMAC_FLOWCTRL_PERI_MEM_TO_PERI, true, false, 0, DMA_S8, NULL);
-        else
-            dma_enable_channel(0, MCI_FIFO, dma_buf, DMA_PERI_SD,
-                DMAC_FLOWCTRL_PERI_PERI_TO_MEM, false, true, 0, DMA_S8, NULL);
-
         unsigned long dummy; /* if we don't ask for a response, writing fails */
         if(!send_cmd(drive, cmd, arg, MCI_RESP, &dummy))
             panicf("%s multiple blocks failed", write ? "write" : "read");