-
Notifications
You must be signed in to change notification settings - Fork 6
/
iogmtf.c
257 lines (207 loc) · 4.23 KB
/
iogmtf.c
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
#include "u.h"
#include "io.h"
#include "mem.h"
#include "arm7/card.h"
/*
* storage support for the Datel Game'n'Music card
* Copyright (c) 2007 Michael "Chishm" Chisholm
*/
#define BYTES_PER_SECTOR 512
#define SD_COMMAND_TIMEOUT 0xFFF
#define SD_WRITE_TIMEOUT 0xFFFF
#define CARD_CR2_SETTINGS 0xA0586000
#define READ_SINGLE_BLOCK 17
#define WRITE_SINGLE_BLOCK 24
#define SD_WRITE_OK 0x05
static void
openSpi(void)
{
volatile ulong temp;
CARD_CR1 = Spiena|Spiirqena;
CARD_COMMAND[0] = 0xF2;
CARD_COMMAND[1] = 0x00;
CARD_COMMAND[2] = 0x00;
CARD_COMMAND[3] = 0x00;
CARD_COMMAND[4] = 0x00;
CARD_COMMAND[5] = 0xCC; // 0xCC == enable microSD ?
CARD_COMMAND[6] = 0x00;
CARD_COMMAND[7] = 0x00;
CARD_CR2 = CARD_CR2_SETTINGS;
while (CARD_CR2 & CARD_BUSY) {
temp = CARD_DATA_RD;
}
CARD_CR1 = Spiena|Spiselect|Spihold;
}
void
closeSpi(void)
{
volatile ulong temp;
CARD_CR1 = Spiena|Spiirqena;
CARD_COMMAND[0] = 0xF2;
CARD_COMMAND[1] = 0x00;
CARD_COMMAND[2] = 0x00;
CARD_COMMAND[3] = 0x00;
CARD_COMMAND[4] = 0x00;
CARD_COMMAND[5] = 0xC8; // 0xCC == disable microSD ?
CARD_COMMAND[6] = 0x00;
CARD_COMMAND[7] = 0x00;
CARD_CR2 = CARD_CR2_SETTINGS;
while (CARD_CR2 & Spibusy) {
temp = CARD_DATA_RD;
}
CARD_CR1 = Spiena|Spiselect|Spihold;
CARD_EEPDATA = 0xFF;
while (CARD_CR1 & Spibusy);
}
static uchar
transferSpiByte(uchar send)
{
CARD_EEPDATA = send;
while (CARD_CR1 & Spibusy);
return CARD_EEPDATA;
}
static uchar
getSpiByte(void)
{
CARD_EEPDATA = 0xFF;
while (CARD_CR1 & Spibusy);
return CARD_EEPDATA;
}
static uchar
sendCommand(uchar command, ulong argument)
{
uchar commandData[6];
int timeout;
uchar spiByte;
int i;
commandData[0] = command | 0x40;
commandData[1] = (uchar)(argument >> 24);
commandData[2] = (uchar)(argument >> 16);
commandData[3] = (uchar)(argument >> 8);
commandData[4] = (uchar)(argument >> 0);
commandData[5] = 0x95; // Fake CRC, 0x95 is only important for SD CMD 0
// Send read sector command
for (i = 0; i < 6; i++) {
CARD_EEPDATA = commandData[i];
while (CARD_CR1 & Spibusy);
}
// Wait for a response
timeout = SD_COMMAND_TIMEOUT;
do {
spiByte = getSpiByte();
} while (spiByte == 0xFF && --timeout > 0);
return spiByte;
}
static int
sdRead(ulong sector, uchar* dest)
{
uchar spiByte;
int timeout;
int i;
volatile ulong temp;
openSpi ();
if (sendCommand (READ_SINGLE_BLOCK, sector * BYTES_PER_SECTOR) != 0x00) {
closeSpi ();
return 0;
}
// Wait for data start token
timeout = SD_COMMAND_TIMEOUT;
do {
spiByte = getSpiByte ();
} while (spiByte == 0xFF && --timeout > 0);
if (spiByte != 0xFE) {
closeSpi ();
return 0;
}
for (i = BYTES_PER_SECTOR; i > 0; i--) {
*dest++ = getSpiByte();
}
// Clean up CRC
temp = getSpiByte();
temp = getSpiByte();
closeSpi ();
return 1;
}
static int
sdWrite(ulong sector, uchar* src)
{
int i;
int timeout;
openSpi ();
if (sendCommand (WRITE_SINGLE_BLOCK, sector * BYTES_PER_SECTOR) != 0) {
closeSpi ();
return 0;
}
// Send start token
transferSpiByte (0xFE);
// Send data
for (i = BYTES_PER_SECTOR; i > 0; i--) {
CARD_EEPDATA = *src++;
while (CARD_CR1 & Spibusy);
}
// Send fake CRC
transferSpiByte (0xFF);
transferSpiByte (0xFF);
// Get data response
if ((getSpiByte() & 0x0F) != SD_WRITE_OK) {
closeSpi ();
return 0;
}
// Wait for card to write data
timeout = SD_WRITE_TIMEOUT;
while (getSpiByte() == 0 && --timeout > 0);
closeSpi();
if (timeout == 0) {
return 0;
}
return 1;
}
static int
gmtf_init(void){
return 1;
}
static int
gmtf_clrstat(void){
return 1;
}
static int
gmtf_read(ulong sector, ulong numSectors, void* buffer) {
uchar* data = (uchar*)buffer;
while (numSectors > 0) {
if (!sdRead (sector, data)) {
return 0;
}
sector ++;
data += BYTES_PER_SECTOR;
numSectors --;
}
return 1;
}
static int
gmtf_write(ulong sector, ulong numSectors, void* buffer) {
uchar* data = (uchar*)buffer;
while (numSectors > 0) {
if (!sdWrite (sector, data)) {
return 0;
}
sector ++;
data += BYTES_PER_SECTOR;
numSectors --;
}
return 1;
}
static Ioifc io_gmtf = {
"GMTF",
Cread|Cwrite|Cslotnds,
gmtf_init,
gmtf_init,
(void*)gmtf_read,
(void*)gmtf_write,
gmtf_clrstat,
gmtf_clrstat,
};
void
iogmtflink(void)
{
addioifc(&io_gmtf);
}