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
diff --git a/firmware/target/arm/as3525/ata_sd_as3525.c b/firmware/target/arm/as3525/ata_sd_as3525.c
index 802a649..f09a7dd 100644
--- a/firmware/target/arm/as3525/ata_sd_as3525.c
+++ b/firmware/target/arm/as3525/ata_sd_as3525.c
@@ -244,7 +244,7 @@ static int sd_init_card(const int drive)
 {
     unsigned long response;
     long init_timeout;
-    bool sdhc;
+    bool sdv2 = false;
     unsigned long temp_reg[4];
     int i;

@@ -253,10 +253,9 @@ static int sd_init_card(const int drive)

     mci_delay();

-    sdhc = false;
     if(send_cmd(drive, SD_SEND_IF_COND, 0x1AA, MCI_RESP|MCI_ARG, &response))
         if((response & 0xFFF) == 0x1AA)
-            sdhc = true;
+            sdv2 = true;

     /* timeout for initialization is 1sec, from SD Specification 2.00 */
     init_timeout = current_tick + HZ;
@@ -274,7 +273,7 @@ static int sd_init_card(const int drive)
         }

         /* acmd41 */
-        if(!send_cmd(drive, SD_APP_OP_COND, (sdhc ? 0x40FF8000 : (1<<23)),
+        if(!send_cmd(drive, SD_APP_OP_COND, 0x00FF8000 | (sdv2 ? (1<<30) : 0),
                         MCI_RESP|MCI_ARG, &card_info[drive].ocr))
         {
             return -4;
@@ -323,12 +322,17 @@ static int sd_init_card(const int drive)
     MCI_CLOCK(drive) |= MCI_CLOCK_BYPASS; /* full speed for controller clock */
     mci_delay();

+    int x = sd_get_status(drive);
+    if(x)
+        panicf("fff %d", x);
+
     /*
      * enable bank switching 
      * without issuing this command, we only have access to 1/4 of the blocks
      * of the first bank (0x1E9E00 blocks, which is the size reported in the
      * CSD register)
      */
+
     if(drive == INTERNAL_AS3525)
     {
         const int ret = sd_select_bank(-1);
@@ -555,6 +559,86 @@ static int sd_wait_for_state(const int drive, unsigned int state)
     }
 }

+int sd_status[2][512/4];
+int sd_get_status(int drive)
+{
+    int ret;
+    unsigned long response;
+    unsigned loops = 0;
+
+    mutex_lock(&sd_mtx);
+
+    do {
+        if(loops++ > PL180_MAX_TRANSFER_ERRORS)
+            panicf("send_status error : 0x%x", transfer_error[drive]);
+
+        ret = sd_wait_for_state(drive, SD_TRAN);
+        if (ret < 0)
+        {
+            ret -= 2;
+            goto error;
+        }
+
+        if( !send_cmd(drive, SD_APP_CMD, card_info[drive].rca, MCI_RESP|MCI_ARG, &response) )
+        {
+            ret = -66;
+            goto error;
+        }
+
+        if(!(response & (1<<5)) )
+        {
+            ret = -67;
+            goto error;
+        }
+
+        mci_delay();
+
+        if(!send_cmd(drive, SD_SEND_STATUS, 0, MCI_NO_FLAGS, NULL))
+        {
+            ret = -2;
+            goto error;
+        }
+
+        mci_delay();
+
+        dma_retain();
+        /* we don't use the uncached buffer here, because we need the
+         * physical memory address for DMA transfers */
+        dma_enable_channel(0, MCI_FIFO(drive), aligned_buffer,
+            (drive == 0) ? DMA_PERI_SD : DMA_PERI_SD_SLOT, 
+            DMAC_FLOWCTRL_PERI_PERI_TO_MEM, false, true, 0, DMA_S8,
+            NULL);
+
+        MCI_DATA_TIMER(drive) = SD_MAX_WRITE_TIMEOUT;
+        MCI_DATA_LENGTH(drive) = 512;
+        MCI_DATA_CTRL(drive) =  (1<<0) /* enable */   |
+                                (1<<1) /* transfer direction */ |
+                                (1<<3) /* DMA */                |
+                                (9<<4) /* 2^9 = 512 */ ;
+
+        wakeup_wait(&transfer_completion_signal, TIMEOUT_BLOCK);
+
+        dma_release();
+
+        mci_delay();
+
+        ret = sd_wait_for_state(drive, SD_TRAN);
+        if (ret < 0)
+        {
+            ret -= 4;
+            goto error;
+        }
+    } while(transfer_error[drive]);
+
+    memcpy(sd_status[drive], uncached_buffer, 512);
+
+    ret = 0;
+
+error:
+    mutex_lock(&sd_mtx);
+    return ret;
+}
+
 static int sd_select_bank(signed char bank)
 {
     int ret;
diff --git a/firmware/target/arm/as3525/debug-as3525.c b/firmware/target/arm/as3525/debug-as3525.c
index b0ee8ef..b74a263 100644
--- a/firmware/target/arm/as3525/debug-as3525.c
+++ b/firmware/target/arm/as3525/debug-as3525.c
@@ -348,6 +348,33 @@ bool __dbg_ports(void)
     lcd_clear_display();
     lcd_setfont(FONT_SYSFIXED);

+    extern int** sd_status;
+    int fd;
+
+/*
+    int ret;
+    ret = sd_get_status(0);
+    if(ret)
+        panicf("c 0 %d", ret);
+*/
+    fd = creat("/status.0");
+    if(fd >= 0)
+    {
+        write(fd, sd_status[0], 512);
+        close(fd);
+    }
+/*
+    ret = sd_get_status(1);
+    if(ret)
+        panicf("c 1 %d", ret);
+*/
+    fd = creat("/status.1");
+    if(fd >= 0)
+    {
+        write(fd, sd_status[1], 512);
+        close(fd);
+    }
+
     while(1)
     {
         line = 0;