34 #define ACMD41 (0xC0+41) 37 #define CMD10 (0x40+10) 38 #define CMD12 (0x40+12) 39 #define ACMD13 (0xC0+13) 40 #define CMD16 (0x40+16) 41 #define CMD17 (0x40+17) 42 #define CMD18 (0x40+18) 43 #define CMD23 (0x40+23) 44 #define ACMD23 (0xC0+23) 45 #define CMD24 (0x40+24) 46 #define CMD25 (0x40+25) 47 #define CMD41 (0x40+41) 48 #define CMD55 (0x40+55) 49 #define CMD58 (0x40+58) 54 static void SELECT(
void)
59 static BYTE rcvr_spi(
void);
61 static void DESELECT(
void)
75 DSTATUS Stat = STA_NOINIT;
87 void xmit_spi (BYTE dat)
103 DKPRINTF(0x01,
"rcvr_spi %02X\n", rd);
117 BYTE wait_ready (
void)
127 SYSERR_PRINT(
"MMC wait ready timeout(%02X)\n", res);
139 void release_spi (
void)
159 DKPRINTF(0x01,
"MMC On\n");
164 void power_off (
void)
172 DKPRINTF(0x01,
"MMC Off\n");
192 unsigned long now_time = Timer1;
194 DKPRINTF(0x01,
"MMC read block buf=%08X size=%d\n", (
int)buff, (
int)btr);
198 }
while ((token == 0xFF) && (Timer1 > (now_time =
get_kernel_time())));
200 SYSERR_PRINT(
"data read error(token = %02X)\n", token);
201 SYSERR_PRINT(
"time = %ld\n", now_time - (Timer1 - PKT_TOUT));
205 #ifdef NOTUSE_BLOCKREAD 207 *buff++ = rcvr_spi();
208 *buff++ = rcvr_spi();
235 #ifdef NOTUSE_BLOCKREAD 239 if (wait_ready() != 0xFF)
return 0;
243 #ifdef NOTUSE_BLOCKREAD 255 if ((resp & 0x1F) != 0x05) {
256 SYSERR_PRINT(
"xmit_datablock() error(%02X)\n", resp);
279 DKPRINTF(0x01,
"MMC_cmd(%02X,%02X)\n", (
int)cmd, (
int)arg);
283 res = send_cmd(CMD55, 0);
284 if (res > 1)
return res;
290 if (wait_ready() != 0xFF)
return 0xFF;
294 xmit_spi((BYTE)(arg >> 24));
295 xmit_spi((BYTE)(arg >> 16));
296 xmit_spi((BYTE)(arg >> 8));
299 if (cmd == CMD0) n = 0x95;
300 if (cmd == CMD8) n = 0x87;
304 if (cmd == CMD12) rcvr_spi();
308 while ((res & 0x80) && --n);
326 static DSTATUS mmc_disk_initialize (
330 BYTE n, ty, cmd, ocr[4];
332 if (drv)
return STA_NOINIT;
333 if (Stat & STA_NODISK)
return Stat;
338 for (n = 10; n; n--) rcvr_spi();
341 if (send_cmd(CMD0, 0) == 1) {
343 if (send_cmd(CMD8, 0x1AA) == 1) {
344 for (n = 0; n < 4; n++) ocr[n] = rcvr_spi();
345 if (ocr[2] == 0x01 && ocr[3] == 0xAA) {
348 for (n = 0; n < 4; n++) ocr[n] = rcvr_spi();
349 ty = (ocr[0] & 0x40) ? 12 : 4;
353 if (send_cmd(ACMD41, 0) <= 1) {
354 ty = 2; cmd = ACMD41;
386 static DSTATUS mmc_disk_status (
390 return (drv) ? STA_NODISK : Stat;
399 static DRESULT mmc_disk_read (
406 if (drv || !count)
return RES_PARERR;
407 if (Stat & STA_NOINIT)
return RES_NOTRDY;
409 if (!(CardType & 8)) sector *= 512;
414 if (send_cmd(CMD17, sector) == 0) {
415 if(rcvr_datablock(buff, 512) != 0) {
418 SYSERR_PRINT(
"MMC single block read error\n");
421 SYSERR_PRINT(
"MMC CMD17 error\n");
425 if (send_cmd(CMD18, sector) == 0) {
427 if (!rcvr_datablock(buff, 512)) {
428 SYSERR_PRINT(
"MMC multiple block read error\n");
438 return count ? RES_ERROR : RES_OK;
448 static DRESULT mmc_disk_write (
455 if (drv || !count)
return RES_PARERR;
456 if (Stat & STA_NOINIT)
return RES_NOTRDY;
457 if (Stat & STA_PROTECT)
return RES_WRPRT;
459 if (!(CardType & 8)) sector *= 512;
464 if ((send_cmd(CMD24, sector) == 0)
465 && xmit_datablock(buff, 0xFE))
469 if (CardType & 6) send_cmd(ACMD23, count);
470 if (send_cmd(CMD25, sector) == 0) {
472 if (!xmit_datablock(buff, 0xFC))
break;
475 if (!xmit_datablock(0, 0xFD))
481 return count ? RES_ERROR : RES_OK;
491 static DRESULT mmc_disk_ioctl (
498 BYTE n, csd[16], *ptr = buff;
502 if (drv)
return RES_PARERR;
503 if (Stat & STA_NOINIT)
return RES_NOTRDY;
512 if (wait_ready() == 0xFF)
516 case GET_SECTOR_COUNT :
517 if ((send_cmd(CMD9, 0) == 0) && rcvr_datablock(csd, 16)) {
518 if ((csd[0] >> 6) == 1) {
519 csize = csd[9] + ((WORD)csd[8] << 8) + 1;
520 *(DWORD*)buff = (DWORD)csize << 10;
522 n = (csd[5] & 15) + ((csd[10] & 128) >> 7) + ((csd[9] & 3) << 1) + 2;
523 csize = (csd[8] >> 6) + ((WORD)csd[7] << 2) + ((WORD)(csd[6] & 3) << 10) + 1;
524 *(DWORD*)buff = (DWORD)csize << (n - 9);
530 case GET_SECTOR_SIZE :
535 case GET_BLOCK_SIZE :
537 if (send_cmd(ACMD13, 0) == 0) {
539 if (rcvr_datablock(csd, 16)) {
540 for (n = 64 - 16; n; n--) rcvr_spi();
541 *(DWORD*)buff = 16UL << (csd[10] >> 4);
546 if ((send_cmd(CMD9, 0) == 0) && rcvr_datablock(csd, 16)) {
548 *(DWORD*)buff = (((csd[10] & 63) << 1) + ((WORD)(csd[11] & 128) >> 7) + 1) << ((csd[13] >> 6) - 1);
550 *(DWORD*)buff = ((WORD)((csd[10] & 124) >> 2) + 1) * (((csd[11] & 3) << 3) + ((csd[11] & 224) >> 5) + 1);
563 if (send_cmd(CMD9, 0) == 0
564 && rcvr_datablock(ptr, 16))
569 if (send_cmd(CMD10, 0) == 0
570 && rcvr_datablock(ptr, 16))
575 if (send_cmd(CMD58, 0) == 0) {
576 for (n = 4; n; n--) *ptr++ = rcvr_spi();
581 case MMC_GET_SDSTAT :
582 if (send_cmd(ACMD13, 0) == 0) {
584 if (rcvr_datablock(ptr, 64))
602 static int mmc_register(
struct st_device *dev,
char *param)
606 SYSERR_PRINT(
"cannot open device \"%s\"\n", param);
618 static int mmc_unregister(
struct st_device *dev)
623 static int mmc_open(
struct st_device *dev)
627 stat = mmc_disk_initialize(0);
637 static int mmc_close(
struct st_device *dev)
648 static int mmc_block_read(
struct st_device *dev,
void *data,
unsigned int sector,
unsigned int count)
650 if(mmc_disk_read(0, data, sector, count) == RES_OK) {
653 SYSERR_PRINT(
"MMC read error(data=%p,size=%d)\n", data, count);
658 static int mmc_block_write(
struct st_device *dev,
const void *data,
unsigned int sector,
unsigned int count)
660 if(mmc_disk_write(0, data, sector, count) == RES_OK) {
663 SYSERR_PRINT(
"MMC write error(data=%p,size=%d)\n", data, count);
668 static int mmc_ioctl(
struct st_device *dev,
unsigned int com,
unsigned int arg,
void *param)
675 unsigned int sectorcount = 0;
676 mmc_disk_ioctl(0, GET_SECTOR_COUNT, (
void *)§orcount);
677 rt = (int)sectorcount;
684 mmc_disk_ioctl(0, GET_SECTOR_SIZE, (
void *)§orsize);
685 rt = (int)sectorsize;
691 unsigned int blocksize = 0;
692 mmc_disk_ioctl(0, GET_BLOCK_SIZE, (
void *)&blocksize);
698 SYSERR_PRINT(
"Unknow command %08lX arg %08lX\n", com, arg);
705 static int mmc_sync(
struct st_device *dev)
707 mmc_disk_ioctl(0, CTRL_SYNC, 0);
712 static int mmc_suspend(
struct st_device *dev)
719 static int mmc_resume(
struct st_device *dev)
721 return mmc_open(dev);
726 .explan =
"MMC/SD SPI mode Strage",
727 .register_dev = mmc_register,
728 .unregister_dev = mmc_unregister,
731 .block_read = mmc_block_read,
732 .block_write = mmc_block_write,
735 .suspend = mmc_suspend,
736 .resume = mmc_resume,
#define IOCMD_SD_GET_SECTOR_COUNT
セクタ数を取得する
#define IOCMD_SPI_CS0NEGATE
CS0をネゲートする
#define IOCMD_SD_GET_BLOCK_SIZE
消去ブロックサイズを取得する
struct st_device * open_device(char *name)
デバイスをオープンする
int write_device(struct st_device *dev, const void *buf, unsigned int count)
デバイスにデータを書き込む
int read_device(struct st_device *dev, void *buf, unsigned int count)
デバイスよりデータを読み出す
#define IOCMD_SPI_SPEED
com : 転送速度を設定する, arg : 転送速度(bps)
#define DEF_DEV_NAME_SD
標準ストレージデバイス名(MMC,SD等)
#define IOCMD_SD_GET_SECTOR_SIZE
1セクタサイズを取得する
int getc_device(struct st_device *dev, unsigned char *data)
デバイスよりデータを1バイト読み出す
#define IOCMD_SPI_CS0ASSERT
CS0をアサートしたままにする
int putc_device(struct st_device *dev, unsigned char data)
デバイスにデータを1バイト書き込む
void wait_time(unsigned int time)
指定時間待つ
ストレージデバイスドライバ ioctl 用マクロ定義
unsigned long long get_kernel_time(void)
カーネル時間を取得する
int ioctl_device(struct st_device *dev, unsigned int com, unsigned int arg, void *param)
デバイスを制御する
char name[MAX_DEVNAMELRN]
デバイス名文字列