1 | /* |
---|
2 | * Copyright (c) 2015 Jarielle Catbagan <jcatbagan93@gmail.com> |
---|
3 | * |
---|
4 | * The license and distribution terms for this file may be |
---|
5 | * found in the file LICENSE in this distribution or at |
---|
6 | * http://www.apache.org/licenses/LICENSE-2.0 |
---|
7 | */ |
---|
8 | |
---|
9 | #include "stddefs.h" |
---|
10 | #include "am335x.h" |
---|
11 | #include "sd.h" |
---|
12 | |
---|
13 | uint16_t sdrca; |
---|
14 | |
---|
15 | int |
---|
16 | sdcmd(uint32_t cmd, uint32_t arg, uint32_t resp[4]) |
---|
17 | { |
---|
18 | /* Clear the SD_STAT register for proper update of status bits after CMD invocation */ |
---|
19 | MMCHS0_REG(SD_STAT) = 0xFFFFFFFF; |
---|
20 | |
---|
21 | MMCHS0_REG(SD_ARG) = arg; |
---|
22 | MMCHS0_REG(SD_CMD) = cmd; |
---|
23 | |
---|
24 | /* CMDx complete? */ |
---|
25 | while(!(MMCHS0_REG(SD_STAT) & (SD_STAT_CC | SD_STAT_ERRI))); |
---|
26 | |
---|
27 | resp[0] = MMCHS0_REG(SD_RSP10); |
---|
28 | resp[1] = MMCHS0_REG(SD_RSP32); |
---|
29 | resp[2] = MMCHS0_REG(SD_RSP54); |
---|
30 | resp[3] = MMCHS0_REG(SD_RSP76); |
---|
31 | |
---|
32 | /* CMDx error? */ |
---|
33 | if(MMCHS0_REG(SD_STAT) & SD_STAT_ERRI) { |
---|
34 | return(-1); |
---|
35 | } else { |
---|
36 | return(0); |
---|
37 | } |
---|
38 | } |
---|
39 | |
---|
40 | int |
---|
41 | sdCardCmd(int interface, int cmd, unsigned long arg, unsigned char *resp) |
---|
42 | { |
---|
43 | return(-1); |
---|
44 | } |
---|
45 | |
---|
46 | int |
---|
47 | sdInit(int interface, int verbosity) |
---|
48 | { |
---|
49 | uint32_t cmd, arg, resp[4]; |
---|
50 | |
---|
51 | /* Reset the MMC/SD controller */ |
---|
52 | MMCHS0_REG(SD_SYSCONFIG) = SD_SYSCONFIG_SOFTRESET; |
---|
53 | while(!(MMCHS0_REG(SD_SYSSTATUS) & SD_SYSSTATUS_RESETDONE)); |
---|
54 | |
---|
55 | /* Reset the command and data lines */ |
---|
56 | MMCHS0_REG(SD_SYSCTL) |= SD_SYSCTL_SRA; |
---|
57 | while(MMCHS0_REG(SD_SYSCTL) & SD_SYSCTL_SRA); |
---|
58 | |
---|
59 | /* Configure the MMC/SD controller capabilities to enable 3.0 V operating voltage */ |
---|
60 | MMCHS0_REG(SD_CAPA) = SD_CAPA_VS30; |
---|
61 | |
---|
62 | /* Configure SD_IE register to update certain status bits in SD_STAT */ |
---|
63 | MMCHS0_REG(SD_IE) = SD_IE_BADA_ENABLE | SD_IE_CERR_ENABLE | SD_IE_ACE_ENABLE | |
---|
64 | SD_IE_DEB_ENABLE | SD_IE_DCRC_ENABLE | SD_IE_DTO_ENABLE | SD_IE_CIE_ENABLE | |
---|
65 | SD_IE_CEB_ENABLE | SD_IE_CCRC_ENABLE | SD_IE_CIRQ_ENABLE | SD_IE_CREM_ENABLE | |
---|
66 | SD_IE_CINS_ENABLE | SD_IE_BRR_ENABLE | SD_IE_BWR_ENABLE | |
---|
67 | SD_IE_TC_ENABLE | SD_IE_CC_ENABLE; |
---|
68 | |
---|
69 | /* Disable open-drain mode (only used for MMC cards) and 8-bit data width */ |
---|
70 | MMCHS0_REG(SD_CON) &= ~(SD_CON_OD | SD_CON_DW8); |
---|
71 | |
---|
72 | /* Configure the operating voltage to 3.0 V */ |
---|
73 | MMCHS0_REG(SD_HCTL) &= ~(SD_HCTL_SDVS); |
---|
74 | MMCHS0_REG(SD_HCTL) |= SD_HCTL_SDVS_VS30; |
---|
75 | |
---|
76 | /* Set the data width to 4-bits */ |
---|
77 | MMCHS0_REG(SD_HCTL) |= SD_HCTL_DTW; |
---|
78 | |
---|
79 | /* Turn on the bus */ |
---|
80 | MMCHS0_REG(SD_HCTL) |= SD_HCTL_SDBP; |
---|
81 | while(!(MMCHS0_REG(SD_HCTL) & SD_HCTL_SDBP)); |
---|
82 | |
---|
83 | /* Enable the internal clock */ |
---|
84 | MMCHS0_REG(SD_SYSCTL) |= SD_SYSCTL_ICE; |
---|
85 | |
---|
86 | /* Configure Clock Frequency Select to 100 KHz */ |
---|
87 | MMCHS0_REG(SD_SYSCTL) = (MMCHS0_REG(SD_SYSCTL) & ~(SD_SYSCTL_CLKD)) | (960 << 6); |
---|
88 | |
---|
89 | /* Wait for clock to stabilize */ |
---|
90 | while(!(MMCHS0_REG(SD_SYSCTL) & SD_SYSCTL_ICS)); |
---|
91 | |
---|
92 | /* Configure SD_SYSCONFIG */ |
---|
93 | MMCHS0_REG(SD_SYSCONFIG) &= ~(SD_SYSCONFIG_CLOCKACTIVITY | SD_SYSCONFIG_SIDLEMODE); |
---|
94 | MMCHS0_REG(SD_SYSCONFIG) |= SD_SYSCONFIG_SIDLEMODE_WKUP | SD_SYSCONFIG_ENAWAKEUP_ENABLE | |
---|
95 | SD_SYSCONFIG_AUTOIDLE_AUTOGATE; |
---|
96 | |
---|
97 | /* Enable the clock to the SD card */ |
---|
98 | MMCHS0_REG(SD_SYSCTL) |= SD_SYSCTL_CEN_ENABLE; |
---|
99 | |
---|
100 | /* Perform the Initialization Stream as specified in the AM335x TRM, Section 18.3.3.2 |
---|
101 | "Card Detection, Identicication, and Selection" */ |
---|
102 | MMCHS0_REG(SD_CON) |= SD_CON_INIT; |
---|
103 | /* Clear the SD_STAT register */ |
---|
104 | MMCHS0_REG(SD_STAT) = 0xFFFFFFFF; |
---|
105 | MMCHS0_REG(SD_ARG) = 0x00000000; |
---|
106 | MMCHS0_REG(SD_CMD) = 0x00000000; |
---|
107 | while(!(MMCHS0_REG(SD_STAT) & SD_STAT_CC)); |
---|
108 | /* Clear CC flag in SD_STAT */ |
---|
109 | MMCHS0_REG(SD_STAT) |= SD_STAT_CC; |
---|
110 | MMCHS0_REG(SD_CON) &= ~SD_CON_INIT; |
---|
111 | |
---|
112 | /* Change the clock frequency to 6 MHz and set the DTO to the maximum value setting */ |
---|
113 | MMCHS0_REG(SD_SYSCTL) &= ~SD_SYSCTL_DTO; |
---|
114 | MMCHS0_REG(SD_SYSCTL) |= SD_SYSCTL_DTO_TCF_2_27; |
---|
115 | MMCHS0_REG(SD_SYSCTL) = (MMCHS0_REG(SD_SYSCTL) & ~SD_SYSCTL_CLKD) | (16 << 6); |
---|
116 | |
---|
117 | /* Wait for clock to stabilize */ |
---|
118 | while((MMCHS0_REG(SD_SYSCTL) & SD_SYSCTL_ICS) != SD_SYSCTL_ICS); |
---|
119 | |
---|
120 | /* Send CMD0/GO_IDLE_STATE to reset the SD card connected to MMC0 interface */ |
---|
121 | arg = 0x00000000; |
---|
122 | cmd = SD_CMD_CMD0_GO_IDLE_STATE | SD_CMD_CMD_TYPE_NORMAL | SD_CMD_DP_NO_DATA_PRESENT | |
---|
123 | SD_CMD_CICE_DISABLE | SD_CMD_CCCE_DISABLE | SD_CMD_RSP_TYPE_NO_RESPONSE; |
---|
124 | if(sdcmd(cmd, arg, resp) == -1) { |
---|
125 | return(-1); |
---|
126 | } |
---|
127 | |
---|
128 | /* Send CMD8/SEND_IF_COND to verify that the SD card satisfies MMC/SD controller |
---|
129 | requirements */ |
---|
130 | arg = 0x00000188; |
---|
131 | cmd = SD_CMD_CMD8_SEND_IF_COND | SD_CMD_CMD_TYPE_NORMAL | SD_CMD_DP_NO_DATA_PRESENT | |
---|
132 | SD_CMD_CICE_ENABLE | SD_CMD_CCCE_ENABLE | SD_CMD_RSP_TYPE_R7; |
---|
133 | if(sdcmd(cmd, arg, resp) == -1) { |
---|
134 | return(-1); |
---|
135 | } |
---|
136 | |
---|
137 | /* Determine what type of SD card is connected, i.e. standard capacity, high capacity, etc. |
---|
138 | * We perform a CMD55/ACMD41 loop until the "Card power up status bit" is set in the OCR |
---|
139 | * register from the SD card to determine when we have a valid response */ |
---|
140 | do { |
---|
141 | arg = 0x00000000; |
---|
142 | cmd = SD_CMD_CMD55_APP_CMD | SD_CMD_CMD_TYPE_NORMAL | SD_CMD_DP_NO_DATA_PRESENT | |
---|
143 | SD_CMD_CICE_ENABLE | SD_CMD_CCCE_ENABLE | SD_CMD_RSP_TYPE_R1; |
---|
144 | if(sdcmd(cmd, arg, resp) == -1) { |
---|
145 | return(-1); |
---|
146 | } |
---|
147 | |
---|
148 | arg = 0x40060000; |
---|
149 | cmd = SD_CMD_ACMD41_SD_SEND_OP_COND | SD_CMD_CMD_TYPE_NORMAL | |
---|
150 | SD_CMD_DP_NO_DATA_PRESENT | SD_CMD_CICE_DISABLE | SD_CMD_CCCE_DISABLE | |
---|
151 | SD_CMD_RSP_TYPE_R3; |
---|
152 | if(sdcmd(cmd, arg, resp) == -1) { |
---|
153 | return(-1); |
---|
154 | } |
---|
155 | } while(!(resp[0] & SD_RSP10_R3_CARD_POWER_UP_STATUS)); |
---|
156 | |
---|
157 | /* Check SD_RSP10 to determine whether the card connected is high capacity or not */ |
---|
158 | if(resp[0] & SD_RSP10_R3_CARD_CAPACITY_STATUS) { |
---|
159 | sdInfoTbl[interface].highcapacity = 1; |
---|
160 | } else { |
---|
161 | sdInfoTbl[interface].highcapacity = 0; |
---|
162 | } |
---|
163 | |
---|
164 | /* Send CMD2 to get SD's CID and to put the card into Identification State */ |
---|
165 | arg = 0x00000000; |
---|
166 | cmd = SD_CMD_CMD2_ALL_SEND_CID | SD_CMD_CMD_TYPE_NORMAL | SD_CMD_DP_NO_DATA_PRESENT | |
---|
167 | SD_CMD_CICE_DISABLE | SD_CMD_CCCE_ENABLE | SD_CMD_RSP_TYPE_R2; |
---|
168 | if(sdcmd(cmd, arg, resp) == -1) { |
---|
169 | return(-1); |
---|
170 | } |
---|
171 | |
---|
172 | /* Send CMD3, i.e. request new relative address (RCA) and to put the card into |
---|
173 | Stand-by State */ |
---|
174 | arg = 0x00000000; |
---|
175 | cmd = SD_CMD_CMD3_SEND_RELATIVE_ADDR | SD_CMD_CMD_TYPE_NORMAL | SD_CMD_DP_NO_DATA_PRESENT | |
---|
176 | SD_CMD_CICE_ENABLE | SD_CMD_CCCE_ENABLE | SD_CMD_RSP_TYPE_R6; |
---|
177 | if(sdcmd(cmd, arg, resp) == -1) { |
---|
178 | return(-1); |
---|
179 | } |
---|
180 | |
---|
181 | /* Save the RCA published from the SD card, this will be used in future CMDx commands */ |
---|
182 | sdrca = (MMCHS0_REG(SD_RSP10) >> 16) & 0xFFFF; |
---|
183 | |
---|
184 | /* Wait for the SD card to enter Stand-by State */ |
---|
185 | do { |
---|
186 | arg = (sdrca << 16) & 0xFFFF0000; |
---|
187 | cmd = SD_CMD_CMD13_SEND_STATUS | SD_CMD_CMD_TYPE_NORMAL | SD_CMD_DP_NO_DATA_PRESENT | |
---|
188 | SD_CMD_CICE_ENABLE | SD_CMD_CCCE_ENABLE | SD_CMD_RSP_TYPE_R1; |
---|
189 | if(sdcmd(cmd, arg, resp) == -1) { |
---|
190 | return(-1); |
---|
191 | } |
---|
192 | } while((resp[0] & SD_RSP10_R1_CURRENT_STATE) != SD_RSP10_R1_CURRENT_STATE_STANDBY); |
---|
193 | |
---|
194 | /* Put the card with the RCA obtained previously into Transfer State via CMD7 */ |
---|
195 | arg = (sdrca << 16) & 0xFFFF0000; |
---|
196 | cmd = SD_CMD_CMD7_SELECT_DESELECT_CARD | SD_CMD_CMD_TYPE_NORMAL | SD_CMD_DP_NO_DATA_PRESENT | |
---|
197 | SD_CMD_CICE_ENABLE | SD_CMD_CCCE_ENABLE | SD_CMD_RSP_TYPE_R1B; |
---|
198 | if(sdcmd(cmd, arg, resp) == -1) { |
---|
199 | return(-1); |
---|
200 | } |
---|
201 | |
---|
202 | /* Wait for the SD card to enter Transfer State */ |
---|
203 | do { |
---|
204 | arg = (sdrca << 16) & 0xFFFF0000; |
---|
205 | cmd = SD_CMD_CMD13_SEND_STATUS | SD_CMD_CMD_TYPE_NORMAL | SD_CMD_DP_NO_DATA_PRESENT | |
---|
206 | SD_CMD_CICE_ENABLE | SD_CMD_CCCE_ENABLE | SD_CMD_RSP_TYPE_R1; |
---|
207 | if(sdcmd(cmd, arg, resp) == -1) { |
---|
208 | return(-1); |
---|
209 | } |
---|
210 | } while((resp[0] & SD_RSP10_R1_CURRENT_STATE) != SD_RSP10_R1_CURRENT_STATE_TRANSFER); |
---|
211 | |
---|
212 | /* Set the bus-width to 4-bits */ |
---|
213 | |
---|
214 | /* Send CMD55 to get ready to configure the bus-width via ACMD6 */ |
---|
215 | arg = (sdrca << 16) & 0xFFFF0000; |
---|
216 | cmd = SD_CMD_CMD55_APP_CMD | SD_CMD_CMD_TYPE_NORMAL | SD_CMD_DP_NO_DATA_PRESENT | |
---|
217 | SD_CMD_CICE_ENABLE | SD_CMD_CCCE_ENABLE | SD_CMD_RSP_TYPE_R1; |
---|
218 | if(sdcmd(cmd, arg, resp) == -1) { |
---|
219 | return(-1); |
---|
220 | } |
---|
221 | |
---|
222 | /* Send ACMD6, SET_BUS_WIDTH to set the bus-width to 4-bits */ |
---|
223 | arg = 0x00000002; |
---|
224 | cmd = SD_CMD_ACMD6_SET_BUS_WIDTH | SD_CMD_CMD_TYPE_NORMAL | SD_CMD_DP_NO_DATA_PRESENT | |
---|
225 | SD_CMD_CICE_ENABLE | SD_CMD_CCCE_ENABLE | SD_CMD_RSP_TYPE_R1; |
---|
226 | if(sdcmd(cmd, arg, resp) == -1) { |
---|
227 | return(-1); |
---|
228 | } |
---|
229 | |
---|
230 | /* Put the SD card into Stand-by State */ |
---|
231 | arg = 0x00000000; |
---|
232 | cmd = SD_CMD_CMD7_SELECT_DESELECT_CARD | SD_CMD_CMD_TYPE_NORMAL | SD_CMD_DP_NO_DATA_PRESENT | |
---|
233 | SD_CMD_CICE_ENABLE | SD_CMD_CCCE_ENABLE | SD_CMD_RSP_TYPE_NO_RESPONSE; |
---|
234 | if(sdcmd(cmd, arg, resp) == -1) { |
---|
235 | return(-1); |
---|
236 | } |
---|
237 | |
---|
238 | /* Wait for the SD card to enter Stand-by State */ |
---|
239 | do { |
---|
240 | arg = (sdrca << 16) & 0xFFFF0000; |
---|
241 | cmd = SD_CMD_CMD13_SEND_STATUS | SD_CMD_CMD_TYPE_NORMAL | SD_CMD_DP_NO_DATA_PRESENT | |
---|
242 | SD_CMD_CICE_ENABLE | SD_CMD_CCCE_ENABLE | SD_CMD_RSP_TYPE_R1; |
---|
243 | if(sdcmd(cmd, arg, resp) == -1) { |
---|
244 | return(-1); |
---|
245 | } |
---|
246 | } while((resp[0] & SD_RSP10_R1_CURRENT_STATE) != SD_RSP10_R1_CURRENT_STATE_STANDBY); |
---|
247 | |
---|
248 | /* This point is reached when SD controller initialization and SD card |
---|
249 | communication is successful */ |
---|
250 | return(0); |
---|
251 | } |
---|
252 | |
---|
253 | int |
---|
254 | sdRead(int interface, char *buf, int blknum, int blkcount) |
---|
255 | { |
---|
256 | uint32_t cmd, arg, resp[4]; |
---|
257 | uint32_t *wordptr = (uint32_t *) buf; |
---|
258 | int byteindex; |
---|
259 | |
---|
260 | /* Get the SD card's status via CMD13 */ |
---|
261 | arg = (sdrca << 16) & 0xFFFF0000; |
---|
262 | cmd = SD_CMD_CMD13_SEND_STATUS | SD_CMD_CMD_TYPE_NORMAL | SD_CMD_DP_NO_DATA_PRESENT | |
---|
263 | SD_CMD_CICE_ENABLE | SD_CMD_CCCE_ENABLE | SD_CMD_RSP_TYPE_R1; |
---|
264 | if(sdcmd(cmd, arg, resp) == -1) { |
---|
265 | return(-1); |
---|
266 | } |
---|
267 | |
---|
268 | /* Ensure that the card is in Transfer State before proceeding */ |
---|
269 | if((resp[0] & SD_RSP10_R1_CURRENT_STATE) != SD_RSP10_R1_CURRENT_STATE_TRANSFER) { |
---|
270 | arg = (sdrca << 16) & 0xFFFF0000; |
---|
271 | cmd = SD_CMD_CMD7_SELECT_DESELECT_CARD | SD_CMD_CMD_TYPE_NORMAL | |
---|
272 | SD_CMD_DP_NO_DATA_PRESENT | SD_CMD_CICE_ENABLE | SD_CMD_CCCE_ENABLE | |
---|
273 | SD_CMD_RSP_TYPE_R1B; |
---|
274 | if(sdcmd(cmd, arg, resp) == -1) { |
---|
275 | return(-1); |
---|
276 | } |
---|
277 | |
---|
278 | /* Wait for the SD card to enter Transfer State */ |
---|
279 | do { |
---|
280 | arg = (sdrca << 16) & 0xFFFF0000; |
---|
281 | cmd = SD_CMD_CMD13_SEND_STATUS | SD_CMD_CMD_TYPE_NORMAL | |
---|
282 | SD_CMD_DP_NO_DATA_PRESENT | SD_CMD_CICE_ENABLE | SD_CMD_CCCE_ENABLE | |
---|
283 | SD_CMD_RSP_TYPE_R1; |
---|
284 | if(sdcmd(cmd, arg, resp) == -1) { |
---|
285 | return(-1); |
---|
286 | } |
---|
287 | } while((resp[0] & SD_RSP10_R1_CURRENT_STATE) != SD_RSP10_R1_CURRENT_STATE_TRANSFER); |
---|
288 | } |
---|
289 | |
---|
290 | /* Set the block length and the number of blocks to read */ |
---|
291 | MMCHS0_REG(SD_BLK) = SD_BLKSIZE | (blkcount << 16); |
---|
292 | /* Send CMD18, i.e. read multiple blocks */ |
---|
293 | arg = blknum; |
---|
294 | cmd = SD_CMD_CMD18_READ_MULTIPLE_BLOCK | SD_CMD_CMD_TYPE_NORMAL | SD_CMD_DP_DATA_PRESENT | |
---|
295 | SD_CMD_CICE_ENABLE | SD_CMD_CCCE_ENABLE | SD_CMD_RSP_TYPE_R1 | SD_CMD_MSBS_MULTIPLE | |
---|
296 | SD_CMD_DDIR_READ | SD_CMD_ACEN_CMD12_ENABLE | SD_CMD_BCE_ENABLE; |
---|
297 | if(sdcmd(cmd, arg, resp) == -1) { |
---|
298 | return(-1); |
---|
299 | } |
---|
300 | |
---|
301 | /* Check the data buffer to see if there is data to be read */ |
---|
302 | do { |
---|
303 | while(!(MMCHS0_REG(SD_STAT) & SD_STAT_BRR)); |
---|
304 | |
---|
305 | /* Clear the BRR status bit in SD_STAT */ |
---|
306 | MMCHS0_REG(SD_STAT) |= SD_STAT_BRR; |
---|
307 | |
---|
308 | for(byteindex = 0; byteindex < (SD_BLKSIZE / 4); byteindex++) { |
---|
309 | *wordptr = (MMCHS0_REG(SD_DATA)); |
---|
310 | wordptr++; |
---|
311 | } |
---|
312 | } while(!(MMCHS0_REG(SD_STAT) & SD_STAT_TC)); |
---|
313 | |
---|
314 | /* Put the SD card into Stand-by State */ |
---|
315 | arg = 0; |
---|
316 | cmd = SD_CMD_CMD7_SELECT_DESELECT_CARD | SD_CMD_CMD_TYPE_NORMAL | SD_CMD_DP_NO_DATA_PRESENT | |
---|
317 | SD_CMD_CICE_ENABLE | SD_CMD_CCCE_ENABLE | SD_CMD_RSP_TYPE_NO_RESPONSE; |
---|
318 | if(sdcmd(cmd, arg, resp) == -1) { |
---|
319 | return(-1); |
---|
320 | } |
---|
321 | |
---|
322 | /* Wait for the SD card to enter Stand-by State */ |
---|
323 | do { |
---|
324 | arg = (sdrca << 16) & 0xFFFF0000; |
---|
325 | cmd = SD_CMD_CMD13_SEND_STATUS | SD_CMD_CMD_TYPE_NORMAL | SD_CMD_DP_NO_DATA_PRESENT | |
---|
326 | SD_CMD_CICE_ENABLE | SD_CMD_CCCE_ENABLE | SD_CMD_RSP_TYPE_R1; |
---|
327 | if(sdcmd(cmd, arg, resp) == -1) { |
---|
328 | return(-1); |
---|
329 | } |
---|
330 | } while((resp[0] & SD_RSP10_R1_CURRENT_STATE) != SD_RSP10_R1_CURRENT_STATE_STANDBY); |
---|
331 | |
---|
332 | return(0); |
---|
333 | } |
---|
334 | |
---|
335 | int |
---|
336 | sdWrite(int interface, char *buf, int blknum, int blkcount) |
---|
337 | { |
---|
338 | uint32_t cmd, arg, resp[4]; |
---|
339 | uint32_t *wordptr = (uint32_t *) buf; |
---|
340 | int byteindex; |
---|
341 | |
---|
342 | /* Get the SD card's status by sending CMD13 */ |
---|
343 | arg = (sdrca << 16) & 0xFFFF0000; |
---|
344 | cmd = SD_CMD_CMD13_SEND_STATUS | SD_CMD_CMD_TYPE_NORMAL | SD_CMD_DP_NO_DATA_PRESENT | |
---|
345 | SD_CMD_CICE_ENABLE | SD_CMD_CCCE_ENABLE | SD_CMD_RSP_TYPE_R1; |
---|
346 | if(sdcmd(cmd, arg, resp) == -1) { |
---|
347 | return(-1); |
---|
348 | } |
---|
349 | |
---|
350 | /* Ensure that the card is in the Transfer State before proceeding */ |
---|
351 | if((resp[0] & SD_RSP10_R1_CURRENT_STATE) != SD_RSP10_R1_CURRENT_STATE_TRANSFER) { |
---|
352 | arg = (sdrca << 16) & 0xFFFF0000; |
---|
353 | cmd = SD_CMD_CMD7_SELECT_DESELECT_CARD | SD_CMD_CMD_TYPE_NORMAL | |
---|
354 | SD_CMD_DP_NO_DATA_PRESENT | SD_CMD_CICE_ENABLE | SD_CMD_CCCE_ENABLE | |
---|
355 | SD_CMD_RSP_TYPE_R1B; |
---|
356 | if(sdcmd(cmd, arg, resp) == -1) { |
---|
357 | return(-1); |
---|
358 | } |
---|
359 | |
---|
360 | /* Wait for SD card to enter Transfer State */ |
---|
361 | do { |
---|
362 | arg = (sdrca << 16) & 0xFFFF0000; |
---|
363 | cmd = SD_CMD_CMD13_SEND_STATUS | SD_CMD_CMD_TYPE_NORMAL | |
---|
364 | SD_CMD_DP_NO_DATA_PRESENT | SD_CMD_CICE_ENABLE | SD_CMD_CCCE_ENABLE | |
---|
365 | SD_CMD_RSP_TYPE_R1; |
---|
366 | if(sdcmd(cmd, arg, resp) == -1) { |
---|
367 | return(-1); |
---|
368 | } |
---|
369 | } while((resp[0] & SD_RSP10_R1_CURRENT_STATE) != SD_RSP10_R1_CURRENT_STATE_TRANSFER); |
---|
370 | } |
---|
371 | |
---|
372 | /* Set the block length in bytes and the number of blocks to write to the SD card */ |
---|
373 | MMCHS0_REG(SD_BLK) = SD_BLKSIZE | (blkcount << 16); |
---|
374 | /* Send CMD25, that is write the number of blocks specified in 'blkcount' from 'buf' to the |
---|
375 | * location that is 512 byte aligned in the SD card specified by the block number 'blknum' |
---|
376 | */ |
---|
377 | arg = blknum; |
---|
378 | cmd = SD_CMD_CMD25_WRITE_MULTIPLE_BLOCK | SD_CMD_CMD_TYPE_NORMAL | SD_CMD_DP_DATA_PRESENT | |
---|
379 | SD_CMD_CICE_ENABLE | SD_CMD_CCCE_ENABLE | SD_CMD_RSP_TYPE_R1 | SD_CMD_MSBS_MULTIPLE | |
---|
380 | SD_CMD_DDIR_WRITE | SD_CMD_ACEN_CMD12_ENABLE | SD_CMD_BCE_ENABLE; |
---|
381 | if(sdcmd(cmd, arg, resp) == -1) { |
---|
382 | return(-1); |
---|
383 | } |
---|
384 | |
---|
385 | /* Write the data */ |
---|
386 | do { |
---|
387 | /* Wait until data is ready to be written */ |
---|
388 | while(!(MMCHS0_REG(SD_STAT) & (SD_STAT_BWR | SD_STAT_TC))); |
---|
389 | |
---|
390 | if(MMCHS0_REG(SD_STAT) & SD_STAT_TC) { |
---|
391 | break; |
---|
392 | } |
---|
393 | |
---|
394 | /* Clear the BWR status bit in SD_STAT */ |
---|
395 | MMCHS0_REG(SD_STAT) |= SD_STAT_BWR; |
---|
396 | |
---|
397 | for(byteindex = 0; byteindex < (SD_BLKSIZE / 4); byteindex++) { |
---|
398 | MMCHS0_REG(SD_DATA) = *wordptr++; |
---|
399 | } |
---|
400 | } while(!(MMCHS0_REG(SD_STAT) & SD_STAT_TC)); |
---|
401 | |
---|
402 | /* Put the SD card into Stand-by State */ |
---|
403 | arg = 0x00000000; |
---|
404 | cmd = SD_CMD_CMD7_SELECT_DESELECT_CARD | SD_CMD_CMD_TYPE_NORMAL | SD_CMD_DP_NO_DATA_PRESENT | |
---|
405 | SD_CMD_CICE_ENABLE | SD_CMD_CCCE_ENABLE | SD_CMD_RSP_TYPE_NO_RESPONSE; |
---|
406 | if(sdcmd(cmd, arg, resp) == -1) { |
---|
407 | return(-1); |
---|
408 | } |
---|
409 | |
---|
410 | /* Wait for SD card to enter Stand-by State */ |
---|
411 | do { |
---|
412 | arg= (sdrca << 16) & 0xFFFF0000; |
---|
413 | cmd = SD_CMD_CMD13_SEND_STATUS | SD_CMD_CMD_TYPE_NORMAL | SD_CMD_DP_NO_DATA_PRESENT | |
---|
414 | SD_CMD_CICE_ENABLE | SD_CMD_CCCE_ENABLE | SD_CMD_RSP_TYPE_R1; |
---|
415 | if(sdcmd(cmd, arg, resp) == -1) { |
---|
416 | return(-1); |
---|
417 | } |
---|
418 | } while((resp[0] & SD_RSP10_R1_CURRENT_STATE) != SD_RSP10_R1_CURRENT_STATE_STANDBY); |
---|
419 | |
---|
420 | return(0); |
---|
421 | } |
---|
422 | |
---|
423 | int |
---|
424 | sdInstalled(int interface) |
---|
425 | { |
---|
426 | if((MMCHS0_REG(SD_CON) & SD_CON_CDP) == SD_CON_CDP_ACTIVE_HIGH) |
---|
427 | if(MMCHS0_REG(SD_PSTATE) & SD_PSTATE_CINS) { |
---|
428 | return(1); |
---|
429 | } else { |
---|
430 | return(0); |
---|
431 | } |
---|
432 | else if(MMCHS0_REG(SD_PSTATE) & SD_PSTATE_CINS) { |
---|
433 | return(0); |
---|
434 | } else { |
---|
435 | return(1); |
---|
436 | } |
---|
437 | } |
---|
438 | |
---|
439 | int |
---|
440 | sdPowerup(int tot) |
---|
441 | { |
---|
442 | return(-1); |
---|
443 | } |
---|
444 | |
---|
445 | int |
---|
446 | sdReadCxD(int interface, unsigned char *buf, int cmd) |
---|
447 | { |
---|
448 | return(-1); |
---|
449 | } |
---|