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
Index: firmware/target/arm/as3525/ata_sd_as3525.c
===================================================================
--- firmware/target/arm/as3525/ata_sd_as3525.c	(Revision 19773)
+++ firmware/target/arm/as3525/ata_sd_as3525.c	(Arbeitskopie)
@@ -110,7 +110,7 @@
 #endif

 static struct wakeup transfer_completion_signal;
-bool retry;
+static volatile bool retry;

 static inline void mci_delay(void) { int i = 0xffff; while(i--) ; }

@@ -270,7 +270,10 @@
             sdhc = true;

     do {
+        /* some MicroSD cards seems to need more delays, so play safe */
         mci_delay();
+        mci_delay();
+        mci_delay();

         /* app_cmd */
         if( !send_cmd(drive, SD_APP_CMD, 0, MCI_RESP|MCI_ARG, &response) ||
@@ -286,7 +289,7 @@

     } while(!(card_info[drive].ocr & (1<<31)) && max_tries--);

-    if(!max_tries)
+    if(max_tries < 0)
         return -4;

     /* send CID */
@@ -371,19 +374,20 @@

             /* We now have exclusive control of fat cache and ata */

-            disk_unmount(1);     /* release "by force", ensure file
+            disk_unmount(SD_SLOT_AS3525);     /* release "by force", ensure file
                                     descriptors aren't leaked and any busy
                                     ones are invalid if mounting */

             /* Force card init for new card, re-init for re-inserted one or
              * clear if the last attempt to init failed with an error. */
-            card_info[1].initialized = 0;
+            card_info[SD_SLOT_AS3525].initialized = 0;

             if (ev.id == SYS_HOTSWAP_INSERTED)
             {
                 sd_enable(true);
                 init_pl180_controller(SD_SLOT_AS3525);
-                disk_mount(1);
+                sd_init_card(SD_SLOT_AS3525);
+                disk_mount(SD_SLOT_AS3525);
             }

             queue_broadcast(SYS_FS_CHANGED, 0);
@@ -391,6 +395,7 @@
             /* Access is now safe */
             mutex_unlock(&sd_mtx);
             fat_unlock();
+            sd_enable(false);
             break;
 #endif
         case SYS_TIMEOUT:
@@ -445,6 +450,7 @@
     GPIOA_IS &= ~(1<<2);
     /* detect both raising and falling edges */
     GPIOA_IBE |= (1<<2);
+
 #endif

 #else
@@ -470,43 +476,40 @@
 int sd_init(void)
 {
     int ret;
-
-    CGU_IDE =   (1<<7)  /* AHB interface enable */  |
+    CGU_IDE = (1<<7)  /* AHB interface enable */   |
                 (1<<6)  /* interface enable */      |
                 ((CLK_DIV(AS3525_PLLA_FREQ, AS3525_IDE_FREQ) - 1) << 2) |
                 1       /* clock source = PLLA */;

+
     CGU_PERI |= CGU_NAF_CLOCK_ENABLE;
 #ifdef HAVE_MULTIVOLUME
     CGU_PERI |= CGU_MCI_CLOCK_ENABLE;
+    CCU_IO &= ~(1<<3);           /* bits 3:2 = 01, xpd is SD interface */
+    CCU_IO |= (1<<2);
 #endif
-
+    
     wakeup_init(&transfer_completion_signal);

     init_pl180_controller(INTERNAL_AS3525);
     ret = sd_init_card(INTERNAL_AS3525);
     if(ret < 0)
         return ret;
-
 #ifdef HAVE_MULTIVOLUME
-    CCU_IO &= ~8;           /* bits 3:2 = 01, xpd is SD interface */
-    CCU_IO |= 4;
-
     init_pl180_controller(SD_SLOT_AS3525);
-    sd_init_card(SD_SLOT_AS3525);
 #endif
-    /* init mutex */

-#ifndef BOOTLOADER
-    sd_enable(false);
-#endif
-
+    /* init mutex */
     mutex_init(&sd_mtx);

     queue_init(&sd_queue, true);
     create_thread(sd_thread, sd_stack, sizeof(sd_stack), 0,
             sd_thread_name IF_PRIO(, PRIORITY_USER_INTERFACE) IF_COP(, CPU));

+#ifndef BOOTLOADER
+    sd_enabled = true;
+    sd_enable(false);
+#endif
     return 0;
 }

@@ -592,21 +595,12 @@
     sd_enable(true);
 #endif

-#ifdef HAVE_MULTIVOLUME
-    if (drive != 0 && !card_detect_target())
-    {
-        /* no external sd-card inserted */
-        ret = -88;
-        goto sd_transfer_error;
-    }
-#endif
-
     if (card_info[drive].initialized <= 0)
     {
-        sd_init_card(drive);
+        ret = sd_init_card(drive);
         if (!(card_info[drive].initialized))
         {
-            panicf("card not initialised");
+            panicf("card not initialised (%d)", ret);
             goto sd_transfer_error;
         }
     }
@@ -623,6 +617,8 @@

     while(count)
     {
+        /* Interrupt handler might set this to true during transfer */
+        retry = false;
         /* 128 * 512 = 2^16, and doesn't fit in the 16 bits of DATA_LENGTH
          * register, so we have to transfer maximum 127 sectors at a time. */
         unsigned int transfer = (count >= 128) ? 127 : count; /* sectors */
@@ -636,10 +632,7 @@
                     MCI_ARG, NULL);

         if (ret < 0)
-        {
-            panicf("transfer multiple blocks failed");
-            goto sd_transfer_error;
-        }
+            panicf("transfer multiple blocks failed (%d)", ret);

         if(write)
             dma_enable_channel(0, buf, MCI_FIFO(drive),
@@ -657,7 +650,7 @@
                                 (1<<3) /* DMA */                        |
                                 (9<<4) /* 2^9 = 512 */ ;

-        retry = false;
+
         wakeup_wait(&transfer_completion_signal, TIMEOUT_BLOCK);
         if(!retry)
         {
@@ -678,26 +671,23 @@
         ret = sd_wait_for_state(drive, SD_TRAN);
         if (ret < 0)
         {
-            panicf(" wait for state TRAN failed");
+            panicf(" wait for state TRAN failed (%d)", ret);
             goto sd_transfer_error;
         }
     }

-    while (1)
-    {
-        dma_release();
+    dma_release();

 #ifndef BOOTLOADER
-        sd_enable(false);
+    sd_enable(false);
 #endif
-        mutex_unlock(&sd_mtx);
+    mutex_unlock(&sd_mtx);
+    return 0;

-        return ret;
-
 sd_transfer_error:
-        panicf("transfer error : %d",ret);
-        card_info[drive].initialized = 0;
-    }
+    panicf("transfer error : %d",ret);
+    card_info[drive].initialized = 0;
+    return ret;
 }

 int sd_read_sectors(IF_MV2(int drive,) unsigned long start, int count,