/
mmc.cpp
executable file
·393 lines (312 loc) · 10.5 KB
/
mmc.cpp
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
/*-----------------------------------------------------------------------*/
/* MMC/SDSC/SDHC (in SPI mode) control module (C)ChaN, 2007 */
/*-----------------------------------------------------------------------*/
/* Only rcvr_spi(), xmit_spi(), disk_timerproc() and some macros are */
/* platform dependent. */
/*-----------------------------------------------------------------------*/
#include "mmc.h"
#include "io.h"
#include "unistd.h"
#include "gpio.h"
#include "iolib.h"
#include <stdlib.h>
#include <stdio.h>
#include "assem.h"
/* Results of Disk Functions */
typedef enum {
RES_OK = 0, /* 0: Successful */
RES_ERROR, /* 1: R/W Error */
RES_WRPRT, /* 2: Write Protected */
RES_NOTRDY, /* 3: Not Ready */
RES_PARERR /* 4: Invalid Parameter */
} DRESULT;
/* Disk Status Bits (int) */
#define STA_NOINIT 0x01 /* Drive not initialized */
#define STA_NODISK 0x02 /* No medium in the drive */
#define STA_PROTECT 0x04 /* Write protected */
/* Command code for disk_ioctrl() */
/* Generic command */
#define CTRL_SYNC 0 /* Mandatory for write functions */
/* MMC/SDC command */
#define MMC_GET_TYPE 10
#define MMC_GET_CSD 11
#define MMC_GET_CID 12
#define MMC_GET_OCR 13
#define MMC_GET_SDSTAT 14
/* ATA/CF command */
#define ATA_GET_REV 20
#define ATA_GET_MODEL 21
#define ATA_GET_SN 22
/* MMC/SD command (in SPI) */
#define CMD0 (0x40+0) /* GO_IDLE_STATE */
#define CMD1 (0x40+1) /* SEND_OP_COND */
#define ACMD41 (0xC0+41) /* SEND_OP_COND (SDC) */
#define CMD8 (0x40+8) /* SEND_IF_COND */
#define CMD9 (0x40+9) /* SEND_CSD */
#define CMD10 (0x40+10) /* SEND_CID */
#define CMD12 (0x40+12) /* STOP_TRANSMISSION */
#define ACMD13 (0xC0+13) /* SD_STATUS (SDC) */
#define CMD16 (0x40+16) /* SET_BLOCKLEN */
#define CMD17 (0x40+17) /* READ_SINGLE_BLOCK */
#define CMD18 (0x40+18) /* READ_MULTIPLE_BLOCK */
#define CMD23 (0x40+23) /* SET_BLOCK_COUNT */
#define ACMD23 (0xC0+23) /* SET_WR_BLK_ERASE_COUNT (SDC) */
#define CMD24 (0x40+24) /* WRITE_BLOCK */
#define CMD25 (0x40+25) /* WRITE_MULTIPLE_BLOCK */
#define CMD41 (0x40+41) /* SEND_OP_COND (ACMD) */
#define CMD55 (0x40+55) /* APP_CMD */
#define CMD58 (0x40+58) /* READ_OCR */
//GPIO_CLK = 1 << 11 # pin 2
//GPIO_MISO = 1 << 13 # pin 3
//GPIO_CS = 1 << 9 # pin 4
//GPIO_MOSI = 1 << 12 # pin 5
#define RDC_CONTROL 0x80003848
#define RDC_DATA 0x8000384c
static unsigned long last_gpio;
inline void fast_gpio_set_value(unsigned long mask, int value)
{
if (value) {
last_gpio |= mask;
fast_outl(last_gpio);
} else {
last_gpio &= ~mask;
fast_outl(last_gpio);
}
}
// raise and lower clock
inline void CLOCK()
{
fast_clock(last_gpio);
}
inline void SELECT() { fast_gpio_set_value(GPIO_JTAG4, 0); }
inline void DESELECT() { fast_gpio_set_value(GPIO_JTAG4, 1); }
// MOSI
inline void MMDI(int val)
{
fast_gpio_set_value(GPIO_JTAG5,val);
}
// MISO
inline int MMDO() {
return (fast_inl() & GPIO_JTAG3);
}
/*--------------------------------------------------------------------------
Module Private Functions
---------------------------------------------------------------------------*/
static volatile
int Stat = STA_NOINIT; /* Disk status */
static
u8 CardType; /* b0:MMC, b1:SDv1, b2:SDv2, b3:Block addressing */
/*-----------------------------------------------------------------------*/
/* Transmit a byte to MMC via SPI (Platform dependent) */
/*-----------------------------------------------------------------------*/
inline void xmit_spi (u8 dat)
{
MMDI( (dat & 0x80) ? 1 : 0 );
CLOCK();
MMDI( (dat & 0x40) ? 1 : 0 );
CLOCK();
MMDI( (dat & 0x20) ? 1 : 0 );
CLOCK();
MMDI( (dat & 0x10) ? 1 : 0 );
CLOCK();
MMDI( (dat & 0x08) ? 1 : 0 );
CLOCK();
MMDI( (dat & 0x04) ? 1 : 0 );
CLOCK();
MMDI( (dat & 0x02) ? 1 : 0 );
CLOCK();
MMDI( (dat & 0x01) ? 1 : 0 );
CLOCK();
}
/*-----------------------------------------------------------------------*/
/* Receive a byte from MMC via SPI (Platform dependent) */
/*-----------------------------------------------------------------------*/
inline u8 rcvr_spi (void)
{
MMDI(1);
return fast_rcvr_spi(last_gpio);
}
/*-----------------------------------------------------------------------*/
/* Wait for card ready */
/*-----------------------------------------------------------------------*/
static
u8 wait_ready (void)
{
u8 res;
rcvr_spi();
do
res = rcvr_spi();
while (res != 0xFF);
return res;
}
/*-----------------------------------------------------------------------*/
/* Deselect the card and release SPI bus */
/*-----------------------------------------------------------------------*/
static
void release_spi (void)
{
DESELECT();
rcvr_spi();
}
static
void power_on(void)
{
SELECT();
}
static
void power_off(void)
{
DESELECT();
}
/*-----------------------------------------------------------------------*/
/* Receive a data packet from MMC */
/*-----------------------------------------------------------------------*/
static
int rcvr_datablock (
u8 *buff, /* Data buffer to store received data */
u32 btr /* Byte count (must be even number) */
)
{
u8 token;
do { /* Wait for data packet in timeout of 200ms */
token = rcvr_spi();
} while (token == 0xFF);
if(token != 0xFE) return 0; /* If not valid data token, retutn with error */
do { /* Receive the data block into buffer */
*buff++ = rcvr_spi();
*buff++ = rcvr_spi();
} while (btr -= 2);
rcvr_spi(); /* Discard CRC */
rcvr_spi();
return 1; /* Return with success */
}
/*-----------------------------------------------------------------------*/
/* Send a command packet to MMC */
/*-----------------------------------------------------------------------*/
static
u8 send_cmd (
u8 cmd, /* Command byte */
u32 arg /* Argument */
)
{
u8 n, res;
if (cmd & 0x80) { /* ACMD<n> is the command sequense of CMD55-CMD<n> */
cmd &= 0x7F;
res = send_cmd(CMD55, 0);
if (res > 1) return res;
}
/* Select the card and wait for ready */
DESELECT();
SELECT();
if (wait_ready() != 0xFF) return 0xFF;
/* Send command packet */
xmit_spi(cmd); /* Start + Command index */
xmit_spi((u8)(arg >> 24)); /* Argument[31..24] */
xmit_spi((u8)(arg >> 16)); /* Argument[23..16] */
xmit_spi((u8)(arg >> 8)); /* Argument[15..8] */
xmit_spi((u8)arg); /* Argument[7..0] */
n = 0x01; /* Dummy CRC + Stop */
if (cmd == CMD0) n = 0x95; /* Valid CRC for CMD0(0) */
if (cmd == CMD8) n = 0x87; /* Valid CRC for CMD8(0x1AA) */
xmit_spi(n);
/* Receive command response */
if (cmd == CMD12) rcvr_spi(); /* Skip a stuff byte when stop reading */
n = 10; /* Wait for a valid response in timeout of 10 attempts */
do
res = rcvr_spi();
while ((res & 0x80) && --n);
return res; /* Return with the response value */
}
/*--------------------------------------------------------------------------
Public Functions
---------------------------------------------------------------------------*/
static int disk_initialize ()
{
u8 n, ty, cmd, ocr[4];
// Extra init with enable high
rcvr_spi();
for (n = 10; n; n--) rcvr_spi(); /* 80 dummy clocks */
power_on(); /* Enable */
ty = 0;
if (send_cmd(CMD0, 0) == 1) { /* Enter Idle state */
// Timer1 = 100; /* Initialization timeout of 1000 msec */
if (send_cmd(CMD8, 0x1AA) == 1) { /* SDHC */
printf("SDHC");
for (n = 0; n < 4; n++) ocr[n] = rcvr_spi(); /* Get trailing return value of R7 resp */
if (ocr[2] == 0x01 && ocr[3] == 0xAA) { /* The card can work at vdd range of 2.7-3.6V */
while (send_cmd(ACMD41, 1UL << 30)); /* Wait for leaving idle state (ACMD41 with HCS bit) */
if (send_cmd(CMD58, 0) == 0) { /* Check CCS bit in the OCR */
for (n = 0; n < 4; n++) ocr[n] = rcvr_spi();
ty = (ocr[0] & 0x40) ? 12 : 4;
}
}
} else { /* SDSC or MMC */
if (send_cmd(ACMD41, 0) <= 1) {
printf("SDSC");
ty = 2; cmd = ACMD41; /* SDSC */
} else {
printf("MMC");
ty = 1; cmd = CMD1; /* MMC */
}
while (send_cmd(cmd, 0)); /* Wait for leaving idle state */
if (send_cmd(CMD16, 512) != 0) /* Set R/W block length to 512 */
ty = 0;
}
}
CardType = ty;
release_spi();
if (ty) { /* Initialization succeded */
printf(" initialised.\n");
Stat &= ~STA_NOINIT; /* Clear STA_NOINIT */
} else { /* Initialization failed */
power_off();
}
return Stat;
}
/*-----------------------------------------------------------------------*/
/* Get Disk Status */
/*-----------------------------------------------------------------------*/
int disk_status (
)
{
return Stat;
}
/*-----------------------------------------------------------------------*/
/* Read Sector(s) */
/*-----------------------------------------------------------------------*/
int mmc_read (
unsigned char *buff, /* Pointer to the data buffer to store read data */
unsigned long sector, /* Start sector number (LBA) */
unsigned char count /* Sector count (1..255) */
)
{
if (!count) return RES_PARERR;
if (Stat & STA_NOINIT) return RES_NOTRDY;
if (!(CardType & 8)) sector *= 512; /* Convert to byte address if needed */
if (count == 1) { /* Single block read */
if ((send_cmd(CMD17, sector) == 0) /* READ_SINGLE_BLOCK */
&& rcvr_datablock(buff, 512))
count = 0;
}
else { /* Multiple block read */
if (send_cmd(CMD18, sector) == 0) { /* READ_MULTIPLE_BLOCK */
do {
if (!rcvr_datablock(buff, 512)) break;
buff += 512;
} while (--count);
send_cmd(CMD12, 0); /* STOP_TRANSMISSION */
}
}
release_spi();
return count ? RES_ERROR : RES_OK;
}
int mmc_init(void)
{
gpio_output(GPIO_JTAG2,0); // CLK
gpio_input(GPIO_JTAG3); // MISO
gpio_output(GPIO_JTAG4,1); // CS
gpio_output(GPIO_JTAG5,1); // MOSI
outl(RDC_DATA, 0xcf8); // Select data register.
last_gpio = fast_inl();
return disk_initialize();
}