Subversion Repositories svnkaklik

Rev

Details | Last modification | View Log

Rev Author Line No. Line
507 kaklik 1
/*-----------------------------------------------------------------------*/
2
/* MMC/SDSC/SDHC (in SPI mode) control module  (C)ChaN, 2007             */
3
/*-----------------------------------------------------------------------*/
4
/* Only rcvr_spi(), xmit_spi(), disk_timerproc() and some macros         */
5
/* are platform dependent.                                               */
6
/*-----------------------------------------------------------------------*/
7
 
8
 
9
#include <avr/io.h>
10
#include "diskio.h"
11
 
12
 
13
/* Definitions for MMC/SDC command */
14
#define CMD0	(0x40+0)	/* GO_IDLE_STATE */
15
#define CMD1	(0x40+1)	/* SEND_OP_COND (MMC) */
16
#define	ACMD41	(0xC0+41)	/* SEND_OP_COND (SDC) */
17
#define CMD8	(0x40+8)	/* SEND_IF_COND */
18
#define CMD9	(0x40+9)	/* SEND_CSD */
19
#define CMD10	(0x40+10)	/* SEND_CID */
20
#define CMD12	(0x40+12)	/* STOP_TRANSMISSION */
21
#define ACMD13	(0xC0+13)	/* SD_STATUS (SDC) */
22
#define CMD16	(0x40+16)	/* SET_BLOCKLEN */
23
#define CMD17	(0x40+17)	/* READ_SINGLE_BLOCK */
24
#define CMD18	(0x40+18)	/* READ_MULTIPLE_BLOCK */
25
#define CMD23	(0x40+23)	/* SET_BLOCK_COUNT (MMC) */
26
#define	ACMD23	(0xC0+23)	/* SET_WR_BLK_ERASE_COUNT (SDC) */
27
#define CMD24	(0x40+24)	/* WRITE_BLOCK */
28
#define CMD25	(0x40+25)	/* WRITE_MULTIPLE_BLOCK */
29
#define CMD55	(0x40+55)	/* APP_CMD */
30
#define CMD58	(0x40+58)	/* READ_OCR */
31
 
32
 
33
/* Port Controls  (Platform dependent) */
34
#define SELECT()	PORTB &= ~1		/* MMC CS = L */
35
#define	DESELECT()	PORTB |= 1		/* MMC CS = H */
36
 
37
#define SOCKPORT	PINB			/* Socket contact port */
38
#define SOCKWP		0x20			/* Write protect switch (PB5) */
39
#define SOCKINS		0x10			/* Card detect switch (PB4) */
40
 
41
 
42
 
43
/*--------------------------------------------------------------------------
44
 
45
   Module Private Functions
46
 
47
---------------------------------------------------------------------------*/
48
 
49
static volatile
50
DSTATUS Stat = STA_NOINIT;	/* Disk status */
51
 
52
static volatile
53
BYTE Timer1, Timer2;	/* 100Hz decrement timer */
54
 
55
static
56
BYTE CardType;			/* b0:MMC, b1:SDv1, b2:SDv2, b3:Block addressing */
57
 
58
 
59
 
60
/*-----------------------------------------------------------------------*/
61
/* Transmit a byte to MMC via SPI  (Platform dependent)                  */
62
/*-----------------------------------------------------------------------*/
63
 
64
#define xmit_spi(dat) 	SPDR=(dat); loop_until_bit_is_set(SPSR,SPIF)
65
 
66
 
67
 
68
/*-----------------------------------------------------------------------*/
69
/* Receive a byte from MMC via SPI  (Platform dependent)                 */
70
/*-----------------------------------------------------------------------*/
71
 
72
static
73
BYTE rcvr_spi (void)
74
{
75
	SPDR = 0xFF;
76
	loop_until_bit_is_set(SPSR, SPIF);
77
	return SPDR;
78
}
79
 
80
/* Alternative macro to receive data fast */
81
#define rcvr_spi_m(dst)	SPDR=0xFF; loop_until_bit_is_set(SPSR,SPIF); *(dst)=SPDR
82
 
83
 
84
 
85
/*-----------------------------------------------------------------------*/
86
/* Wait for card ready                                                   */
87
/*-----------------------------------------------------------------------*/
88
 
89
static
90
BYTE wait_ready (void)
91
{
92
	BYTE res;
93
 
94
 
95
	Timer2 = 50;	/* Wait for ready in timeout of 500ms */
96
	rcvr_spi();
97
	do
98
		res = rcvr_spi();
99
	while ((res != 0xFF) && Timer2);
100
 
101
	return res;
102
}
103
 
104
 
105
 
106
/*-----------------------------------------------------------------------*/
107
/* Deselect the card and release SPI bus                                 */
108
/*-----------------------------------------------------------------------*/
109
 
110
static
111
void release_spi (void)
112
{
113
	DESELECT();
114
	rcvr_spi();
115
}
116
 
117
 
118
 
119
/*-----------------------------------------------------------------------*/
120
/* Power Control  (Platform dependent)                                   */
121
/*-----------------------------------------------------------------------*/
122
/* When the target system does not support socket power control, there   */
123
/* is nothing to do in these functions and chk_power always returns 1.   */
124
 
125
static
126
void power_on (void)
127
{
128
	PORTE &= ~0x80;				/* Socket power ON */
129
	for (Timer1 = 3; Timer1; );	/* Wait for 30ms */
130
	PORTB = 0b10110101;			/* Enable drivers */
131
	DDRB  = 0b11000111;
132
	SPCR = 0b01010000;			/* Initialize SPI port (Mode 0) */
133
	SPSR = 0b00000001;
134
}
135
 
136
static
137
void power_off (void)
138
{
139
	SELECT();				/* Wait for card ready */
140
	wait_ready();
141
	release_spi();
142
 
143
	SPCR = 0;				/* Disable SPI function */
144
	DDRB  = 0b11000000;		/* Disable drivers */
145
	PORTB = 0b10110000;
146
	PORTE |=  0x80;			/* Socket power OFF */
147
	Stat |= STA_NOINIT;		/* Set STA_NOINIT */
148
}
149
 
150
static
151
int chk_power(void)		/* Socket power state: 0=off, 1=on */
152
{
153
	return (PORTE & 0x80) ? 0 : 1;
154
}
155
 
156
 
157
 
158
/*-----------------------------------------------------------------------*/
159
/* Receive a data packet from MMC                                        */
160
/*-----------------------------------------------------------------------*/
161
 
162
static
163
BOOL rcvr_datablock (
164
	BYTE *buff,			/* Data buffer to store received data */
165
	UINT btr			/* Byte count (must be multiple of 4) */
166
)
167
{
168
	BYTE token;
169
 
170
 
171
	Timer1 = 10;
172
	do {							/* Wait for data packet in timeout of 100ms */
173
		token = rcvr_spi();
174
	} while ((token == 0xFF) && Timer1);
175
	if(token != 0xFE) return FALSE;	/* If not valid data token, retutn with error */
176
 
177
	do {							/* Receive the data block into buffer */
178
		rcvr_spi_m(buff++);
179
		rcvr_spi_m(buff++);
180
		rcvr_spi_m(buff++);
181
		rcvr_spi_m(buff++);
182
	} while (btr -= 4);
183
	rcvr_spi();						/* Discard CRC */
184
	rcvr_spi();
185
 
186
	return TRUE;					/* Return with success */
187
}
188
 
189
 
190
 
191
/*-----------------------------------------------------------------------*/
192
/* Send a data packet to MMC                                             */
193
/*-----------------------------------------------------------------------*/
194
 
195
#if _READONLY == 0
196
static
197
BOOL xmit_datablock (
198
	const BYTE *buff,	/* 512 byte data block to be transmitted */
199
	BYTE token			/* Data/Stop token */
200
)
201
{
202
	BYTE resp, wc;
203
 
204
 
205
	if (wait_ready() != 0xFF) return FALSE;
206
 
207
	xmit_spi(token);					/* Xmit data token */
208
	if (token != 0xFD) {	/* Is data token */
209
		wc = 0;
210
		do {							/* Xmit the 512 byte data block to MMC */
211
			xmit_spi(*buff++);
212
			xmit_spi(*buff++);
213
		} while (--wc);
214
		xmit_spi(0xFF);					/* CRC (Dummy) */
215
		xmit_spi(0xFF);
216
		resp = rcvr_spi();				/* Reveive data response */
217
		if ((resp & 0x1F) != 0x05)		/* If not accepted, return with error */
218
			return FALSE;
219
	}
220
 
221
	return TRUE;
222
}
223
#endif /* _READONLY */
224
 
225
 
226
 
227
/*-----------------------------------------------------------------------*/
228
/* Send a command packet to MMC                                          */
229
/*-----------------------------------------------------------------------*/
230
 
231
static
232
BYTE send_cmd (
233
	BYTE cmd,		/* Command byte */
234
	DWORD arg		/* Argument */
235
)
236
{
237
	BYTE n, res;
238
 
239
 
240
	if (cmd & 0x80) {	/* ACMD<n> is the command sequense of CMD55-CMD<n> */
241
		cmd &= 0x7F;
242
		res = send_cmd(CMD55, 0);
243
		if (res > 1) return res;
244
	}
245
 
246
	/* Select the card and wait for ready */
247
	DESELECT();
248
	SELECT();
249
	if (wait_ready() != 0xFF) return 0xFF;
250
 
251
	/* Send command packet */
252
	xmit_spi(cmd);						/* Start + Command index */
253
	xmit_spi((BYTE)(arg >> 24));		/* Argument[31..24] */
254
	xmit_spi((BYTE)(arg >> 16));		/* Argument[23..16] */
255
	xmit_spi((BYTE)(arg >> 8));			/* Argument[15..8] */
256
	xmit_spi((BYTE)arg);				/* Argument[7..0] */
257
	n = 0x01;							/* Dummy CRC + Stop */
258
	if (cmd == CMD0) n = 0x95;			/* Valid CRC for CMD0(0) */
259
	if (cmd == CMD8) n = 0x87;			/* Valid CRC for CMD8(0x1AA) */
260
	xmit_spi(n);
261
 
262
	/* Receive command response */
263
	if (cmd == CMD12) rcvr_spi();		/* Skip a stuff byte when stop reading */
264
	n = 10;								/* Wait for a valid response in timeout of 10 attempts */
265
	do
266
		res = rcvr_spi();
267
	while ((res & 0x80) && --n);
268
 
269
	return res;			/* Return with the response value */
270
}
271
 
272
 
273
 
274
/*--------------------------------------------------------------------------
275
 
276
   Public Functions
277
 
278
---------------------------------------------------------------------------*/
279
 
280
 
281
/*-----------------------------------------------------------------------*/
282
/* Initialize Disk Drive                                                 */
283
/*-----------------------------------------------------------------------*/
284
 
285
DSTATUS disk_initialize (
286
	BYTE drv		/* Physical drive nmuber (0) */
287
)
288
{
289
	BYTE n, cmd, ty, ocr[4];
290
 
291
 
292
	if (drv) return STA_NOINIT;			/* Supports only single drive */
293
	if (Stat & STA_NODISK) return Stat;	/* No card in the socket */
294
 
295
	power_on();							/* Force socket power on */
296
	for (n = 10; n; n--) rcvr_spi();	/* 80 dummy clocks */
297
 
298
	ty = 0;
299
	if (send_cmd(CMD0, 0) == 1) {			/* Enter Idle state */
300
		Timer1 = 100;						/* Initialization timeout of 1000 msec */
301
		if (send_cmd(CMD8, 0x1AA) == 1) {	/* SDHC */
302
			for (n = 0; n < 4; n++) ocr[n] = rcvr_spi();		/* Get trailing return value of R7 resp */
303
			if (ocr[2] == 0x01 && ocr[3] == 0xAA) {				/* The card can work at vdd range of 2.7-3.6V */
304
				while (Timer1 && send_cmd(ACMD41, 1UL << 30));	/* Wait for leaving idle state (ACMD41 with HCS bit) */
305
				if (Timer1 && send_cmd(CMD58, 0) == 0) {		/* Check CCS bit in the OCR */
306
					for (n = 0; n < 4; n++) ocr[n] = rcvr_spi();
307
					ty = (ocr[0] & 0x40) ? 12 : 4;
308
				}
309
			}
310
		} else {							/* SDSC or MMC */
311
			if (send_cmd(ACMD41, 0) <= 1) 	{
312
				ty = 2; cmd = ACMD41;	/* SDSC */
313
			} else {
314
				ty = 1; cmd = CMD1;		/* MMC */
315
			}
316
			while (Timer1 && send_cmd(cmd, 0));			/* Wait for leaving idle state */
317
			if (!Timer1 || send_cmd(CMD16, 512) != 0)	/* Set R/W block length to 512 */
318
				ty = 0;
319
		}
320
	}
321
	CardType = ty;
322
	release_spi();
323
 
324
	if (ty) {			/* Initialization succeded */
325
		Stat &= ~STA_NOINIT;		/* Clear STA_NOINIT */
326
	} else {			/* Initialization failed */
327
		power_off();
328
	}
329
 
330
	return Stat;
331
}
332
 
333
 
334
 
335
/*-----------------------------------------------------------------------*/
336
/* Get Disk Status                                                       */
337
/*-----------------------------------------------------------------------*/
338
 
339
DSTATUS disk_status (
340
	BYTE drv		/* Physical drive nmuber (0) */
341
)
342
{
343
	if (drv) return STA_NOINIT;		/* Supports only single drive */
344
	return Stat;
345
}
346
 
347
 
348
 
349
/*-----------------------------------------------------------------------*/
350
/* Read Sector(s)                                                        */
351
/*-----------------------------------------------------------------------*/
352
 
353
DRESULT disk_read (
354
	BYTE drv,			/* Physical drive nmuber (0) */
355
	BYTE *buff,			/* Pointer to the data buffer to store read data */
356
	DWORD sector,		/* Start sector number (LBA) */
357
	BYTE count			/* Sector count (1..255) */
358
)
359
{
360
	if (drv || !count) return RES_PARERR;
361
	if (Stat & STA_NOINIT) return RES_NOTRDY;
362
 
363
	if (!(CardType & 8)) sector *= 512;	/* Convert to byte address if needed */
364
 
365
	if (count == 1) {	/* Single block read */
366
		if ((send_cmd(CMD17, sector) == 0)	/* READ_SINGLE_BLOCK */
367
			&& rcvr_datablock(buff, 512))
368
			count = 0;
369
	}
370
	else {				/* Multiple block read */
371
		if (send_cmd(CMD18, sector) == 0) {	/* READ_MULTIPLE_BLOCK */
372
			do {
373
				if (!rcvr_datablock(buff, 512)) break;
374
				buff += 512;
375
			} while (--count);
376
			send_cmd(CMD12, 0);				/* STOP_TRANSMISSION */
377
		}
378
	}
379
	release_spi();
380
 
381
	return count ? RES_ERROR : RES_OK;
382
}
383
 
384
 
385
 
386
/*-----------------------------------------------------------------------*/
387
/* Write Sector(s)                                                       */
388
/*-----------------------------------------------------------------------*/
389
 
390
#if _READONLY == 0
391
DRESULT disk_write (
392
	BYTE drv,			/* Physical drive nmuber (0) */
393
	const BYTE *buff,	/* Pointer to the data to be written */
394
	DWORD sector,		/* Start sector number (LBA) */
395
	BYTE count			/* Sector count (1..255) */
396
)
397
{
398
	if (drv || !count) return RES_PARERR;
399
	if (Stat & STA_NOINIT) return RES_NOTRDY;
400
	if (Stat & STA_PROTECT) return RES_WRPRT;
401
 
402
	if (!(CardType & 8)) sector *= 512;	/* Convert to byte address if needed */
403
 
404
	if (count == 1) {	/* Single block write */
405
		if ((send_cmd(CMD24, sector) == 0)	/* WRITE_BLOCK */
406
			&& xmit_datablock(buff, 0xFE))
407
			count = 0;
408
	}
409
	else {				/* Multiple block write */
410
		if (CardType & 6) send_cmd(ACMD23, count);
411
		if (send_cmd(CMD25, sector) == 0) {	/* WRITE_MULTIPLE_BLOCK */
412
			do {
413
				if (!xmit_datablock(buff, 0xFC)) break;
414
				buff += 512;
415
			} while (--count);
416
			if (!xmit_datablock(0, 0xFD))	/* STOP_TRAN token */
417
				count = 1;
418
		}
419
	}
420
	release_spi();
421
 
422
	return count ? RES_ERROR : RES_OK;
423
}
424
#endif /* _READONLY == 0 */
425
 
426
 
427
 
428
/*-----------------------------------------------------------------------*/
429
/* Miscellaneous Functions                                               */
430
/*-----------------------------------------------------------------------*/
431
 
432
#if _USE_IOCTL != 0
433
DRESULT disk_ioctl (
434
	BYTE drv,		/* Physical drive nmuber (0) */
435
	BYTE ctrl,		/* Control code */
436
	void *buff		/* Buffer to send/receive control data */
437
)
438
{
439
	DRESULT res;
440
	BYTE n, csd[16], *ptr = buff;
441
	WORD csize;
442
 
443
 
444
	if (drv) return RES_PARERR;
445
 
446
	res = RES_ERROR;
447
 
448
	if (ctrl == CTRL_POWER) {
449
		switch (*ptr) {
450
		case 0:		/* Sub control code == 0 (POWER_OFF) */
451
			if (chk_power())
452
				power_off();		/* Power off */
453
			res = RES_OK;
454
			break;
455
		case 1:		/* Sub control code == 1 (POWER_ON) */
456
			power_on();				/* Power on */
457
			res = RES_OK;
458
			break;
459
		case 2:		/* Sub control code == 2 (POWER_GET) */
460
			*(ptr+1) = (BYTE)chk_power();
461
			res = RES_OK;
462
			break;
463
		default :
464
			res = RES_PARERR;
465
		}
466
	}
467
	else {
468
		if (Stat & STA_NOINIT) return RES_NOTRDY;
469
 
470
		switch (ctrl) {
471
		case CTRL_SYNC :		/* Make sure that no pending write process */
472
			SELECT();
473
			if (wait_ready() == 0xFF)
474
				res = RES_OK;
475
			break;
476
 
477
		case GET_SECTOR_COUNT :	/* Get number of sectors on the disk (DWORD) */
478
			if ((send_cmd(CMD9, 0) == 0) && rcvr_datablock(csd, 16)) {
479
				if ((csd[0] >> 6) == 1) {	/* SDC ver 2.00 */
480
					csize = csd[9] + ((WORD)csd[8] << 8) + 1;
481
					*(DWORD*)buff = (DWORD)csize << 10;
482
				} else {					/* SDC ver 1.XX or MMC*/
483
					n = (csd[5] & 15) + ((csd[10] & 128) >> 7) + ((csd[9] & 3) << 1) + 2;
484
					csize = (csd[8] >> 6) + ((WORD)csd[7] << 2) + ((WORD)(csd[6] & 3) << 10) + 1;
485
					*(DWORD*)buff = (DWORD)csize << (n - 9);
486
				}
487
				res = RES_OK;
488
			}
489
			break;
490
 
491
		case GET_SECTOR_SIZE :	/* Get R/W sector size (WORD) */
492
			*(WORD*)buff = 512;
493
			res = RES_OK;
494
			break;
495
 
496
		case GET_BLOCK_SIZE :	/* Get erase block size in unit of sector (DWORD) */
497
			if (CardType & 4) {			/* SDC ver 2.00 */
498
				if (send_cmd(ACMD13, 0) == 0) {		/* Read SD status */
499
					rcvr_spi();
500
					if (rcvr_datablock(csd, 16)) {				/* Read partial block */
501
						for (n = 64 - 16; n; n--) rcvr_spi();	/* Purge trailing data */
502
						*(DWORD*)buff = 16UL << (csd[10] >> 4);
503
						res = RES_OK;
504
					}
505
				}
506
			} else {					/* SDC ver 1.XX or MMC */
507
				if ((send_cmd(CMD9, 0) == 0) && rcvr_datablock(csd, 16)) {	/* Read CSD */
508
					if (CardType & 2) {			/* SDC ver 1.XX */
509
						*(DWORD*)buff = (((csd[10] & 63) << 1) + ((WORD)(csd[11] & 128) >> 7) + 1) << ((csd[13] >> 6) - 1);
510
					} else {					/* MMC */
511
						*(DWORD*)buff = ((WORD)((csd[10] & 124) >> 2) + 1) * (((csd[11] & 3) << 3) + ((csd[11] & 224) >> 5) + 1);
512
					}
513
					res = RES_OK;
514
				}
515
			}
516
			break;
517
 
518
		case MMC_GET_TYPE :		/* Get card type flags (1 byte) */
519
			*ptr = CardType;
520
			res = RES_OK;
521
			break;
522
 
523
		case MMC_GET_CSD :		/* Receive CSD as a data block (16 bytes) */
524
			if (send_cmd(CMD9, 0) == 0		/* READ_CSD */
525
				&& rcvr_datablock(ptr, 16))
526
				res = RES_OK;
527
			break;
528
 
529
		case MMC_GET_CID :		/* Receive CID as a data block (16 bytes) */
530
			if (send_cmd(CMD10, 0) == 0		/* READ_CID */
531
				&& rcvr_datablock(ptr, 16))
532
				res = RES_OK;
533
			break;
534
 
535
		case MMC_GET_OCR :		/* Receive OCR as an R3 resp (4 bytes) */
536
			if (send_cmd(CMD58, 0) == 0) {	/* READ_OCR */
537
				for (n = 4; n; n--) *ptr++ = rcvr_spi();
538
				res = RES_OK;
539
			}
540
			break;
541
 
542
		case MMC_GET_SDSTAT :	/* Receive SD statsu as a data block (64 bytes) */
543
			if (send_cmd(ACMD13, 0) == 0) {	/* SD_STATUS */
544
				rcvr_spi();
545
				if (rcvr_datablock(ptr, 64))
546
					res = RES_OK;
547
			}
548
			break;
549
 
550
		default:
551
			res = RES_PARERR;
552
		}
553
 
554
		release_spi();
555
	}
556
 
557
	return res;
558
}
559
#endif /* _USE_IOCTL != 0 */
560
 
561
 
562
/*-----------------------------------------------------------------------*/
563
/* Device Timer Interrupt Procedure  (Platform dependent)                */
564
/*-----------------------------------------------------------------------*/
565
/* This function must be called in period of 10ms                        */
566
 
567
void disk_timerproc (void)
568
{
569
	static BYTE pv;
570
	BYTE n, s;
571
 
572
 
573
	n = Timer1;						/* 100Hz decrement timer */
574
	if (n) Timer1 = --n;
575
	n = Timer2;
576
	if (n) Timer2 = --n;
577
 
578
	n = pv;
579
	pv = SOCKPORT & (SOCKWP | SOCKINS);	/* Sample socket switch */
580
 
581
	if (n == pv) {					/* Have contacts stabled? */
582
		s = Stat;
583
 
584
		if (pv & SOCKWP)			/* WP is H (write protected) */
585
			s |= STA_PROTECT;
586
		else						/* WP is L (write enabled) */
587
			s &= ~STA_PROTECT;
588
 
589
		if (pv & SOCKINS)			/* INS = H (Socket empty) */
590
			s |= (STA_NODISK | STA_NOINIT);
591
		else						/* INS = L (Card inserted) */
592
			s &= ~STA_NODISK;
593
 
594
		Stat = s;
595
	}
596
}
597