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
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--) ; }

@@ -383,9 +383,10 @@
             {
                 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);

             /* Access is now safe */
@@ -427,6 +428,11 @@

 static void init_pl180_controller(const int drive)
 {
+#ifdef HAVE_MULTIVOLUME
+    if (drive == SD_SLOT_AS3525)
+    {
+    }
+#endif
     MCI_COMMAND(drive) = MCI_DATA_CTRL(drive) = 0;
     MCI_CLEAR(drive) = 0x7ff;

@@ -445,6 +451,7 @@
     GPIOA_IS &= ~(1<<2);
     /* detect both raising and falling edges */
     GPIOA_IBE |= (1<<2);
+
 #endif

 #else
@@ -470,37 +477,19 @@
 int sd_init(void)
 {
     int ret;
-
-    CGU_IDE =   (1<<7)  /* AHB interface enable */  |
-                (1<<6)  /* interface enable */      |
-                ((CLK_DIV(AS3525_PLLA_FREQ, AS3525_IDE_FREQ) - 1) << 2) |
+    sd_enable(true);
+    CGU_IDE |= ((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;
-#endif
-
     wakeup_init(&transfer_completion_signal);

     init_pl180_controller(INTERNAL_AS3525);
+    init_pl180_controller(SD_SLOT_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
-
     mutex_init(&sd_mtx);

     queue_init(&sd_queue, true);
@@ -603,10 +592,10 @@

     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 +612,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 +627,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 +645,7 @@
                                 (1<<3) /* DMA */                        |
                                 (9<<4) /* 2^9 = 512 */ ;

-        retry = false;
+
         wakeup_wait(&transfer_completion_signal, TIMEOUT_BLOCK);
         if(!retry)
         {
@@ -678,26 +666,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,
@@ -748,6 +733,8 @@
         return; /* nothing to do */
     if(on)
     {
+        CCU_IO &= ~(1<<3);           /* bits 3:2 = 01, xpd is SD interface */
+        CCU_IO |= (1<<2);
         CGU_PERI |= CGU_NAF_CLOCK_ENABLE;
 #ifdef HAVE_MULTIVOLUME
         CGU_PERI |= CGU_MCI_CLOCK_ENABLE;
@@ -758,6 +745,7 @@
     }
     else
     {
+        CCU_IO &= ~(1<<2|1<<3);
         CGU_PERI &= ~CGU_NAF_CLOCK_ENABLE;
 #ifdef HAVE_MULTIVOLUME
         CGU_PERI &= ~CGU_MCI_CLOCK_ENABLE;