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
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
 firmware/target/arm/as3525/sd-as3525v2.c |  248 +++++++++++++++---------------
 1 files changed, 125 insertions(+), 123 deletions(-)

diff --git a/firmware/target/arm/as3525/sd-as3525v2.c b/firmware/target/arm/as3525/sd-as3525v2.c
index 70c0477..417f2d0 100644
--- a/firmware/target/arm/as3525/sd-as3525v2.c
+++ b/firmware/target/arm/as3525/sd-as3525v2.c
@@ -168,20 +168,40 @@ static void printf(const char *format, ...)
 /* FIXME */
 #define MCI_POWER
 #define MCI_CLOCK
+
+#define UNKNOWN_REG_00  (*(volatile unsigned long *) (SD_BASE+0x00))
+#define UNKNOWN_REG_04  (*(volatile unsigned long *) (SD_BASE+0x04))
+#define UNKNOWN_REG_08  (*(volatile unsigned long *) (SD_BASE+0x08))
+#define UNKNOWN_REG_0C  (*(volatile unsigned long *) (SD_BASE+0x0C))
+#define UNKNOWN_REG_10  (*(volatile unsigned long *) (SD_BASE+0x10))
+#define UNKNOWN_REG_14  (*(volatile unsigned long *) (SD_BASE+0x14))
+#define UNKNOWN_REG_18  (*(volatile unsigned long *) (SD_BASE+0x18))
+#define UNKNOWN_REG_1C  (*(volatile unsigned long *) (SD_BASE+0x1C))
+#define UNKNOWN_REG_20  (*(volatile unsigned long *) (SD_BASE+0x20))
+#define MCI_MASK        (*(volatile unsigned long *) (SD_BASE+0x24))
 #define MCI_ARGUMENT    (*(volatile unsigned long *) (SD_BASE+0x28))
 #define MCI_COMMAND     (*(volatile unsigned long *) (SD_BASE+0x2C))
-#define MCI_RESPCMD
 #define MCI_RESP0       (*(volatile unsigned long *) (SD_BASE+0x30))
 #define MCI_RESP1       (*(volatile unsigned long *) (SD_BASE+0x34))
 #define MCI_RESP2       (*(volatile unsigned long *) (SD_BASE+0x38))
 #define MCI_RESP3       (*(volatile unsigned long *) (SD_BASE+0x3C))
+#define MCI_STATUS      (*(volatile unsigned long *) (SD_BASE+0x40))
+#define MCI_CLEAR       (*(volatile unsigned long *) (SD_BASE+0x44))
+#define UNKNOWN_REG_48  (*(volatile unsigned long *) (SD_BASE+0x48))
+#define UNKNOWN_REG_4C  (*(volatile unsigned long *) (SD_BASE+0x4C))
+#define UNKNOWN_REG_64  (*(volatile unsigned long *) (SD_BASE+0x64))
+#define UNKNOWN_REG_70  (*(volatile unsigned long *) (SD_BASE+0x70))
+
+/*  Registers we use with pl180 but have not figured out here yet  */
+#define MCI_POWER
+#define MCI_CLOCK
+#define MCI_RESPCMD
 #define MCI_DATA_TIMER
 #define MCI_DATA_LENGTH
 #define MCI_DATA_CTRL
-#define MCI_STATUS      (*(volatile unsigned long *) (SD_BASE+0x40))
-#define MCI_CLEAR       (*(volatile unsigned long *) (SD_BASE+0x44))
-#define MCI_MASK        (*(volatile unsigned long *) (SD_BASE+0x24))
+#define MCI_DATA_CNT
 #define MCI_SELECT
+#define MCI_FIFO_CNT

 #define MCI_ERROR 0     /* FIXME */

@@ -220,7 +240,7 @@ static inline void mci_delay(void) { int i = 0xffff; while(i--) ; }

 void INT_NAND(void)
 {
-    (*(volatile unsigned long *) (SD_BASE+0x0)) &= ~0x10;   // ?
+    UNKNOWN_REG_00 &= ~0x10;   // ?
     const int status = MCI_STATUS;

 #if 0
@@ -262,47 +282,33 @@ void INT_NAND(void)
      *  read resp (6, 7, 12, 42) : while bit 9 is unset ;
      *
      */
-    printf("%x %x", status, (*(volatile unsigned long *) (SD_BASE+0x48)));
+    printf("%x %x", status, UNKNOWN_REG_48);
     //while(!button_read_device());
     //while(button_read_device());

-    (*(volatile unsigned long *) (SD_BASE+0x0)) |= 0x10;    // ?
+    UNKNOWN_REG_00 |= 0x10;    // ?
 }

 static bool send_cmd(const int cmd, const int arg, const int flags,
         unsigned long *response)
 {
-    int val;
-    val = cmd | MCI_COMMAND_ENABLE;
-    if(flags & MCI_RESP)
-    {
-        val |= MCI_COMMAND_RESPONSE;
-        if(flags & MCI_LONG_RESP)
-            val |= MCI_COMMAND_LONG_RESPONSE;
-    }
-
-    if(cmd == 18)       /* r */
-        val |= 0x2200;
-    else if(cmd == 25)  /* w */
-        val |= 0x2700;
-
-    int tmp = (*(volatile unsigned long *) (SD_BASE+0x10));
-    (*(volatile unsigned long *) (SD_BASE+0x10)) = 0;
+    int tmp = UNKNOWN_REG_10;
+    UNKNOWN_REG_10 = 0;

     MCI_COMMAND = 0x80202000;
     MCI_ARGUMENT = 0;
     int max = 10;
     while(max-- && MCI_COMMAND & MCI_COMMAND_ACTIVE);

-    (*(volatile unsigned long *) (SD_BASE+0x08)) &= ~0xff;
-    (*(volatile unsigned long *) (SD_BASE+0x08)) |= 0;
+    UNKNOWN_REG_08 &= ~0xff;
+    UNKNOWN_REG_08 |= 0;

     MCI_COMMAND = 0x80202000;
     MCI_ARGUMENT = 0;
     max = 10;
     while(max-- && MCI_COMMAND & MCI_COMMAND_ACTIVE);

-    (*(volatile unsigned long *) (SD_BASE+0x10)) = tmp;
+    UNKNOWN_REG_10 = tmp;

     MCI_COMMAND = 0x80202000;
     MCI_ARGUMENT = 0;
@@ -312,9 +318,22 @@ static bool send_cmd(const int cmd, const int arg, const int flags,
     mci_delay();

     MCI_ARGUMENT = arg;
-    MCI_COMMAND = val;

-    (*(volatile unsigned long *) (SD_BASE+0x00)) |= 0x10;
+    /* Construct MCI_COMMAND & enable CPSM */
+    MCI_COMMAND =
+    /*b0:5*/  cmd
+    /* b6 */| ((flags & (MCI_RESP|MCI_LONG_RESP)) ? MCI_COMMAND_RESPONSE : 0)
+    /* b7 */| ((flags & MCI_LONG_RESP) ? MCI_COMMAND_LONG_RESPONSE : 0)
+    /* b8   | bit 8 not determined yet */
+    /* b9 */| ((cmd==18 || cmd==25)? (1<<9): 0)
+    /* b10*/| ((cmd==25)? (1<<10): 0)
+    /* b11*/| ((cmd==25)? (1<<11): 0)
+    /* b12  | bit 12 not determined yet */
+    /* b13*/| ((cmd==18 || cmd==25)? (1<<13): 0)
+    /* b14:30   not determined yet */
+    /* b31*/| MCI_COMMAND_ENABLE;     /* Enables CPSM */
+
+    UNKNOWN_REG_00 |= 0x10;

     max = 1000;
     while(max-- && MCI_COMMAND & MCI_COMMAND_ACTIVE); /* wait for cmd completion */
@@ -340,87 +359,74 @@ static bool send_cmd(const int cmd, const int arg, const int flags,
 static int sd_init_card(void)
 {
     unsigned long response;
+    long init_timeout;
     unsigned long temp_reg[4];
-    int max_tries = 100; /* max acmd41 attemps */
-    bool sdhc;
+    bool sdv2 = false;
     int i;

+
+    /* FIXME:  Need to set MCICLK speed to 400 kHz for Ident mode  */
+
+    /* 100 - 400kHz clock required for Identification Mode  */
+    /*  Start of Card Identification Mode ************************************/
+
     if(!send_cmd(SD_GO_IDLE_STATE, 0, MCI_NO_RESP, NULL))
         return -1;

     mci_delay();

-    sdhc = false;
     if(send_cmd(SD_SEND_IF_COND, 0x1AA, MCI_RESP, &response))
         if((response & 0xFFF) == 0x1AA)
-            sdhc = true;
+            sdv2 = true;
+
+        /* timeout for initialization is 1sec, from SD Specification 2.00 */
+        init_timeout = current_tick + HZ;

     do {
-        /* some MicroSD cards seems to need more delays, so play safe */
-        mci_delay();
-        mci_delay();
-        mci_delay();
-
-        /* app_cmd */
-        if( !send_cmd(SD_APP_CMD, 0, MCI_RESP, &response) /*||
-            !(response & (1<<5))*/ )
-        {
-            return -2;
-        }
+      /* this timeout is the only valid error for this loop*/
+      if(TIME_AFTER(current_tick, init_timeout))
+        return -2;

-        /* acmd41 */
-        if(!send_cmd(SD_APP_OP_COND, (sdhc ? 0x40FF8000 : (1<<23)),
-                        MCI_RESP, &card_info.ocr))
-            return -3;
-    } while(!(card_info.ocr & (1<<31)) && max_tries--);
+      /* app_cmd */
+      send_cmd(SD_APP_CMD, 0, MCI_RESP, &response);

-    if(max_tries < 0)
-        return -4;
+      /* ACMD41 For v2 cards set HCS bit[30] & send host voltage range to all */
+      send_cmd(SD_APP_OP_COND, (0x00FF8000 | (sdv2 ? 1<<30 : 0)),
+               MCI_RESP, &card_info.ocr);

-    mci_delay();
-    mci_delay();
-    mci_delay();
+    } while(!(card_info.ocr & (1<<31)));
+
+    /* CMD2 send CID */
+    if(!send_cmd(SD_ALL_SEND_CID, 0, MCI_RESP|MCI_LONG_RESP,
+      temp_reg))
+      return -3;
+
+    for(i=0; i<4; i++)
+      card_info.cid[3-i] = temp_reg[i];

-    /* send CID */
-    if(!send_cmd(SD_ALL_SEND_CID, 0, MCI_RESP|MCI_LONG_RESP, card_info.cid))
-        return -5;
+    /* CMD3 send RCA */
+    if(!send_cmd(SD_SEND_RELATIVE_ADDR, 0, MCI_RESP,
+      &card_info.rca))
+      return -4;

-    /* send RCA */
-    if(!send_cmd(SD_SEND_RELATIVE_ADDR, 0, MCI_RESP, &card_info.rca))
-        return -6;
+    /*  End of Card Identification Mode   ************************************/

-    /* send CSD */
+    /* CMD9 send CSD */
     if(!send_cmd(SD_SEND_CSD, card_info.rca,
-                 MCI_RESP|MCI_LONG_RESP, temp_reg))
-        return -7;
+      MCI_RESP|MCI_LONG_RESP, temp_reg))
+      return -9;

     for(i=0; i<4; i++)
-        card_info.csd[3-i] = temp_reg[i];
+      card_info.csd[3-i] = temp_reg[i];

     sd_parse_csd(&card_info);

-    if(!send_cmd(SD_APP_CMD, 0, MCI_RESP, &response) ||
-       !send_cmd(42, 0, MCI_NO_RESP, NULL)) /* disconnect the 50 KOhm pull-up
-                                               resistor on CD/DAT3 */
-        return -13;
-
-    if(!send_cmd(SD_APP_CMD, card_info.rca, MCI_NO_RESP, NULL))
-        return -10;
-
-    if(!send_cmd(SD_SET_BUS_WIDTH, card_info.rca | 2, MCI_NO_RESP, NULL))
-        return -11;
-
-    (*(volatile unsigned long *) (SD_BASE+0x18)) &= ~(0x10001);
-    (*(volatile unsigned long *) (SD_BASE+0x18)) |= 0x1;
+    UNKNOWN_REG_18 &= ~(0x10001);
+    UNKNOWN_REG_18 |= 0x1;

     if(!send_cmd(SD_SELECT_CARD, card_info.rca, MCI_NO_RESP, NULL))
         return -9;

-    /* not sent in init_card() by OF */
-    if(!send_cmd(SD_SET_BLOCKLEN, card_info.blocksize, MCI_NO_RESP,
-                 NULL))
-        return -12;
-
     card_info.initialized = 1;

     return 0;
@@ -472,36 +478,36 @@ static void sd_thread(void)

 static void init_controller(void)
 {
-    int tmp = (*(volatile unsigned long *) (SD_BASE+0x70));
+    int tmp = UNKNOWN_REG_70;
     int shift = 1 + ((tmp << 26) >> 27);

-    (*(volatile unsigned long *) (SD_BASE+0x04)) &= ~((1 << shift) -1);
-    (*(volatile unsigned long *) (SD_BASE+0x04)) = (1 << shift) -1;
+    UNKNOWN_REG_04 &= ~((1 << shift) -1);
+    UNKNOWN_REG_04 = (1 << shift) -1;

     mci_delay();

-    (*(volatile unsigned long *) (SD_BASE+0x00)) |= 1;
+    UNKNOWN_REG_00 |= 1;
     int max = 1000;
-    while(max-- && !(*(volatile unsigned long *) (SD_BASE+0x00)) & 1)
+    while(max-- && !UNKNOWN_REG_00 & 1)
         ;

     MCI_CLEAR = 0xffffffff;
-    MCI_MASK = 0xffffbffe;
+    MCI_MASK = 0xffffbffe;        /* (1<<0 | 1<<14)  */

-    (*(volatile unsigned long *) (SD_BASE+0x00)) |= 0x10;
-    (*(volatile unsigned long *) (SD_BASE+0x14)) = 0xffffffff;
+    UNKNOWN_REG_00 |= 0x10;
+    UNKNOWN_REG_14 = 0xffffffff;

-    (*(volatile unsigned long *) (SD_BASE+0x10)) = (1<<shift) - 1;
+    UNKNOWN_REG_10 = (1<<shift) - 1;

     MCI_ARGUMENT = 0;
-    MCI_COMMAND = 0x80202000;
+    MCI_COMMAND = 0x80202000;    /* (1<<31|1<<21|1<<13) */
     max = 10;
     while(max-- && (MCI_COMMAND & (1<<31))) ;

-    (*(volatile unsigned long *) (SD_BASE+0x64)) = 0xfffff;
+    UNKNOWN_REG_64 = 0xfffff;

-    (*(volatile unsigned long *) (SD_BASE+0x4c)) = ~0x7fff0fff; // interrupt mask ?
-    (*(volatile unsigned long *) (SD_BASE+0x4c)) |= 0x503f0080;
+    UNKNOWN_REG_4C = ~0x7fff0fff; // interrupt mask ?
+    UNKNOWN_REG_4C |= 0x503f0080;

     MCI_MASK = 0xffffbffe;
 }
@@ -520,7 +526,7 @@ int sd_init(void)
         (CLK_DIV(AS3525_PLLA_FREQ, AS3525_MS_FREQ) -1) | 1;

     /* ?? */
-    *(volatile int*)0xC80F003C = (1<<7) |
+    *(volatile int*)(CGU_BASE + 0x3C) = (1<<7) |
         (CLK_DIV(AS3525_PLLA_FREQ, 24000000) -1) | 1;

     wakeup_init(&transfer_completion_signal);
@@ -559,30 +565,26 @@ void sd_get_info(struct storage_info *info)

 static int sd_wait_for_state(unsigned int state)
 {
-    unsigned long response;
-    unsigned int timeout = 100; /* ticks */
-    long t = current_tick;
+    unsigned long response = 0;
+    unsigned int timeout = current_tick + 100; /* 100 ticks timeout */

     while (1)
     {
-        long tick;
-
-        if(!send_cmd(SD_SEND_STATUS, card_info.rca,
-                    MCI_RESP, &response))
-            return -1;
+      if(!send_cmd(SD_SEND_STATUS, card_info.rca,
+          MCI_RESP, &response))
+          return -1;

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

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

-        if (TIME_AFTER((tick = current_tick), next_yield))
-        {
-            yield();
-            timeout += current_tick - tick;
-            next_yield = tick + MIN_YIELD_PERIOD;
-        }
+      if (TIME_AFTER(current_tick, next_yield))
+      {
+          yield();
+          next_yield = current_tick + MIN_YIELD_PERIOD;
+      }
     }
 }

@@ -639,27 +641,27 @@ static int sd_transfer_sectors(unsigned long start, int count, void* buf, bool w
         const int cmd =
             write ? SD_WRITE_MULTIPLE_BLOCK : SD_READ_MULTIPLE_BLOCK;

-    (*(volatile unsigned long *) (SD_BASE+0x00)) |= 2;
-    while(( *(volatile unsigned long *) (SD_BASE+0x00)) & 2) ;
+    UNKNOWN_REG_00 |= 2;
+    while(UNKNOWN_REG_00 & 2) ;

-    //(*(volatile unsigned long *) (SD_BASE+0x1c)) = 512;
-    (*(volatile unsigned long *) (SD_BASE+0x20)) = transfer * 512;
+    //UNKNOWN_REG_1C = 512;
+    UNKNOWN_REG_20 = transfer * 512;

-    (*(volatile unsigned long *) (SD_BASE+0x00)) |= 2;
-    while(( *(volatile unsigned long *) (SD_BASE+0x00)) & 2) ;
+    UNKNOWN_REG_00 |= 2;
+    while(UNKNOWN_REG_00 & 2) ;

-    (*(volatile unsigned long *) (SD_BASE+0x4c)) &= ~0x7fff0fff;
+    UNKNOWN_REG_4C &= ~0x7fff0fff;

     if(0)
     {
-        (*(volatile unsigned long *) (SD_BASE+0x00)) |= 0x20;
+        UNKNOWN_REG_00 |= 0x20;
         MCI_MASK = 0xBE8C;
-        (*(volatile unsigned long *) (SD_BASE+0x4c)) |= 0x503f0080;
+        UNKNOWN_REG_4C |= 0x503f0080;
     }
     else
     {
         MCI_MASK = 0xBEB8;
-        (*(volatile unsigned long *) (SD_BASE+0x4c)) |= 0x3f0030;
+        UNKNOWN_REG_4C |= 0x3f0030;
     }

         if(card_info.ocr & (1<<30) ) /* SDHC */