1 | @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ |
---|
2 | @ io_scsd_s.s |
---|
3 | @ |
---|
4 | @ Hardware Routines for reading a Secure Digital card |
---|
5 | @ using the SC SD |
---|
6 | @ |
---|
7 | @ Based on code supplied by Romman |
---|
8 | @ |
---|
9 | @ Copyright (c) 2006 Michael "Chishm" Chisholm |
---|
10 | @ |
---|
11 | @ Redistribution and use in source and binary forms, with or without modification, |
---|
12 | @ are permitted provided that the following conditions are met: |
---|
13 | @ |
---|
14 | @ 1. Redistributions of source code must retain the above copyright notice, |
---|
15 | @ this list of conditions and the following disclaimer. |
---|
16 | @ 2. Redistributions in binary form must reproduce the above copyright notice, |
---|
17 | @ this list of conditions and the following disclaimer in the documentation and/or |
---|
18 | @ other materials provided with the distribution. |
---|
19 | @ 3. The name of the author may not be used to endorse or promote products derived |
---|
20 | @ from this software without specific prior written permission. |
---|
21 | @ |
---|
22 | @ THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED |
---|
23 | @ WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY |
---|
24 | @ AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE |
---|
25 | @ LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
---|
26 | @ DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
---|
27 | @ LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
---|
28 | @ THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
---|
29 | @ (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, |
---|
30 | @ EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
---|
31 | @ |
---|
32 | @ 2006-07-22 - Chishm |
---|
33 | @ * First release of stable code |
---|
34 | @ |
---|
35 | @ 2006-08-19 - Chishm |
---|
36 | @ * Added SuperCard Lite support |
---|
37 | @ |
---|
38 | @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ |
---|
39 | |
---|
40 | .align 4 |
---|
41 | .arm |
---|
42 | |
---|
43 | .equ REG_SCSD_DATAWRITE, 0x09000000 |
---|
44 | .equ BYTES_PER_READ, 0x200 |
---|
45 | .equ SCSD_STS_BUSY, 0x100 |
---|
46 | .equ BUSY_WAIT_TIMEOUT, 0x10000 |
---|
47 | .equ FALSE, 0 |
---|
48 | .equ TRUE, 1 |
---|
49 | |
---|
50 | @ bool _SCSD_writeData_s (u8 *data, u16* crc) |
---|
51 | |
---|
52 | .global _SCSD_writeData_s |
---|
53 | |
---|
54 | _SCSD_writeData_s: |
---|
55 | stmfd r13!, {r4-r5} |
---|
56 | mov r5, #BYTES_PER_READ |
---|
57 | mov r2, #REG_SCSD_DATAWRITE |
---|
58 | |
---|
59 | @ Wait for a free data buffer on the SD card |
---|
60 | mov r4, #BUSY_WAIT_TIMEOUT |
---|
61 | _SCSD_writeData_busy_wait: |
---|
62 | @ Test for timeout |
---|
63 | subs r4, r4, #1 |
---|
64 | moveq r0, #FALSE @ return false on failure |
---|
65 | beq _SCSD_writeData_return |
---|
66 | @ Check the busy bit of the status register |
---|
67 | ldrh r3, [r2] |
---|
68 | tst r3, #SCSD_STS_BUSY |
---|
69 | beq _SCSD_writeData_busy_wait |
---|
70 | |
---|
71 | ldrh r3, [r2] @ extra clock |
---|
72 | |
---|
73 | mov r3, #0 @ start bit |
---|
74 | strh r3,[r2] |
---|
75 | |
---|
76 | @ Check if the data buffer is aligned on a halfword boundary |
---|
77 | tst r0, #1 |
---|
78 | beq _SCSD_writeData_data_loop |
---|
79 | |
---|
80 | @ Used when the source data is unaligned |
---|
81 | _SCSD_writeData_data_loop_unaligned: |
---|
82 | ldrb r3, [r0], #1 |
---|
83 | ldrb r4, [r0], #1 |
---|
84 | orr r3, r3, r4, lsl #8 |
---|
85 | stmia r2, {r3-r4} |
---|
86 | subs r5, r5, #2 |
---|
87 | bne _SCSD_writeData_data_loop_unaligned |
---|
88 | b _SCSD_writeData_crc |
---|
89 | |
---|
90 | @ Write the data to the card |
---|
91 | @ 4 halfwords are transmitted to the Supercard at once, for timing purposes |
---|
92 | @ Only the first halfword needs to contain data for standard SuperCards |
---|
93 | @ For the SuperCard Lite, the data is split into 4 nibbles, one per halfword |
---|
94 | _SCSD_writeData_data_loop: |
---|
95 | ldrh r3, [r0], #2 |
---|
96 | |
---|
97 | @ This bit added for SCLite. Notice that the shift is not the same as in |
---|
98 | @ the original (buggy) code supplied by Romman |
---|
99 | add r3, r3, r3, lsl #20 |
---|
100 | mov r4, r3, lsr #8 |
---|
101 | |
---|
102 | stmia r2, {r3-r4} |
---|
103 | |
---|
104 | subs r5, r5, #2 |
---|
105 | bne _SCSD_writeData_data_loop |
---|
106 | |
---|
107 | @ Send the data CRC |
---|
108 | _SCSD_writeData_crc: |
---|
109 | cmp r1, #0 |
---|
110 | movne r0, r1 |
---|
111 | movne r1, #0 |
---|
112 | movne r5, #8 |
---|
113 | bne _SCSD_writeData_data_loop |
---|
114 | |
---|
115 | mov r3, #0xff @ end bit |
---|
116 | strh r3, [r2] |
---|
117 | |
---|
118 | @ Wait for the SD card to signal that it is finished recieving |
---|
119 | mov r4, #BUSY_WAIT_TIMEOUT |
---|
120 | _SCSD_writeData_finished_wait: |
---|
121 | @ Test for timeout |
---|
122 | subs r4, r4, #1 |
---|
123 | moveq r0, #FALSE @ return false on failure |
---|
124 | beq _SCSD_writeData_return |
---|
125 | @ Check the busy bit of the status register |
---|
126 | ldrh r3, [r2] |
---|
127 | tst r3, #0x100 |
---|
128 | bne _SCSD_writeData_finished_wait |
---|
129 | |
---|
130 | @ Send 8 more clocks, as required by the SD card |
---|
131 | ldmia r2, {r3-r4} |
---|
132 | |
---|
133 | @ return true for success |
---|
134 | mov r0, #TRUE |
---|
135 | |
---|
136 | _SCSD_writeData_return: |
---|
137 | ldmfd r13!,{r4-r5} |
---|
138 | bx r14 |
---|
139 | |
---|