1 #ifndef __SWM320_SDIO_H__
2 #define __SWM320_SDIO_H__
3 
4 
5 #define SD_CMD_GO_IDLE_STATE                       ((uint8_t)0)
6 #define SD_CMD_SEND_OP_COND                        ((uint8_t)1)
7 #define SD_CMD_ALL_SEND_CID                        ((uint8_t)2)
8 #define SD_CMD_SET_REL_ADDR                        ((uint8_t)3)
9 #define SD_CMD_SET_DSR                             ((uint8_t)4)
10 #define SD_CMD_HS_SWITCH                           ((uint8_t)6)
11 #define SD_CMD_SEL_DESEL_CARD                      ((uint8_t)7)
12 #define SD_CMD_SEND_IF_COND                        ((uint8_t)8)
13 #define SD_CMD_SEND_CSD                            ((uint8_t)9)
14 #define SD_CMD_SEND_CID                            ((uint8_t)10)
15 #define SD_CMD_STOP_TRANSMISSION                   ((uint8_t)12)
16 #define SD_CMD_SEND_STATUS                         ((uint8_t)13)
17 #define SD_CMD_SET_BLOCKLEN                        ((uint8_t)16)
18 #define SD_CMD_READ_SINGLE_BLOCK                   ((uint8_t)17)
19 #define SD_CMD_READ_MULT_BLOCK                     ((uint8_t)18)
20 #define SD_CMD_WRITE_SINGLE_BLOCK                  ((uint8_t)24)
21 #define SD_CMD_WRITE_MULT_BLOCK                    ((uint8_t)25)
22 #define SD_CMD_PROG_CID                            ((uint8_t)26)
23 #define SD_CMD_PROG_CSD                            ((uint8_t)27)
24 #define SD_CMD_APP_CMD                             ((uint8_t)55)
25 
26 /*Following commands are SD Card Specific commands.
27   SDIO_APP_CMD should be sent before sending these commands. */
28 #define SD_CMD_APP_SD_SET_BUSWIDTH                 ((uint8_t)6)
29 #define SD_CMD_SD_APP_STAUS                        ((uint8_t)13)
30 #define SD_CMD_SD_APP_SEND_NUM_WRITE_BLOCKS        ((uint8_t)22)
31 #define SD_CMD_SD_APP_OP_COND                      ((uint8_t)41)
32 #define SD_CMD_SD_APP_SET_CLR_CARD_DETECT          ((uint8_t)42)
33 #define SD_CMD_SD_APP_SEND_SCR                     ((uint8_t)51)
34 #define SD_CMD_SDIO_RW_DIRECT                      ((uint8_t)52)
35 #define SD_CMD_SDIO_RW_EXTENDED                    ((uint8_t)53)
36 
37 
38 #define SD_RESP_NO          0   //0 无响应
39 #define SD_RESP_32b         2   //2 32位响应
40 #define SD_RESP_128b        1   //1 128位响应
41 #define SD_RESP_32b_busy    3   //3 32位响应,check Busy after response
42 
43 #define SD_BUSWIDTH_1b      0
44 #define SD_BUSWIDTH_4b      2
45 
46 #define SD_RES_OK           0
47 #define SD_RES_ERR          1
48 #define SD_RES_TIMEOUT      2
49 
50 
51 typedef struct
52 {
53   __IO uint8_t  CSDStruct;            // CSD structure
54   __IO uint8_t  SysSpecVersion;       // System specification version
55   __IO uint8_t  Reserved1;            // Reserved
56   __IO uint8_t  TAAC;                 // Data read access-time 1
57   __IO uint8_t  NSAC;                 // Data read access-time 2 in CLK cycles
58   __IO uint8_t  MaxBusClkFrec;        // Max. bus clock frequency
59   __IO uint16_t CardComdClasses;      //< Card command classes
60   __IO uint8_t  RdBlockLen;           // Max. read data block length
61   __IO uint8_t  PartBlockRead;        // Partial blocks for read allowed
62   __IO uint8_t  WrBlockMisalign;      // Write block misalignment
63   __IO uint8_t  RdBlockMisalign;      // Read block misalignment
64   __IO uint8_t  DSRImpl;              // DSR implemented
65   __IO uint8_t  Reserved2;            // Reserved
66   __IO uint32_t DeviceSize;           // Device Size
67   __IO uint8_t  MaxRdCurrentVDDMin;   // Max. read current @ VDD min
68   __IO uint8_t  MaxRdCurrentVDDMax;   // Max. read current @ VDD max
69   __IO uint8_t  MaxWrCurrentVDDMin;   // Max. write current @ VDD min
70   __IO uint8_t  MaxWrCurrentVDDMax;   // Max. write current @ VDD max
71   __IO uint8_t  DeviceSizeMul;        // Device size multiplier
72   __IO uint8_t  EraseGrSize;          // Erase group size
73   __IO uint8_t  EraseGrMul;           // Erase group size multiplier
74   __IO uint8_t  WrProtectGrSize;      // Write protect group size
75   __IO uint8_t  WrProtectGrEnable;    // Write protect group enable
76   __IO uint8_t  ManDeflECC;           // Manufacturer default ECC
77   __IO uint8_t  WrSpeedFact;          // Write speed factor
78   __IO uint8_t  MaxWrBlockLen;        // Max. write data block length
79   __IO uint8_t  WriteBlockPaPartial;  // Partial blocks for write allowed
80   __IO uint8_t  Reserved3;            // Reserded
81   __IO uint8_t  ContentProtectAppli;  // Content protection application
82   __IO uint8_t  FileFormatGrouop;     // File format group
83   __IO uint8_t  CopyFlag;             // Copy flag (OTP)
84   __IO uint8_t  PermWrProtect;        // Permanent write protection
85   __IO uint8_t  TempWrProtect;        // Temporary write protection
86   __IO uint8_t  FileFormat;           // File Format
87   __IO uint8_t  ECC;                  // ECC code
88 } SD_CSD;
89 
90 typedef struct
91 {
92   __IO uint8_t  ManufacturerID;       // ManufacturerID
93   __IO uint16_t OEM_AppliID;          // OEM/Application ID
94   __IO uint32_t ProdName1;            // Product Name part1
95   __IO uint8_t  ProdName2;            // Product Name part2
96   __IO uint8_t  ProdRev;              // Product Revision
97   __IO uint32_t ProdSN;               // Product Serial Number
98   __IO uint8_t  Reserved1;            // Reserved1
99   __IO uint16_t ManufactDate;         // Manufacturing Date
100 } SD_CID;
101 
102 
103 #define SDIO_STD_CAPACITY_SD_CARD_V1_1             ((uint32_t)0x00000000)
104 #define SDIO_STD_CAPACITY_SD_CARD_V2_0             ((uint32_t)0x00000001)
105 #define SDIO_HIGH_CAPACITY_SD_CARD                 ((uint32_t)0x00000002)
106 #define SDIO_MULTIMEDIA_CARD                       ((uint32_t)0x00000003)
107 #define SDIO_SECURE_DIGITAL_IO_CARD                ((uint32_t)0x00000004)
108 #define SDIO_HIGH_SPEED_MULTIMEDIA_CARD            ((uint32_t)0x00000005)
109 #define SDIO_SECURE_DIGITAL_IO_COMBO_CARD          ((uint32_t)0x00000006)
110 #define SDIO_HIGH_CAPACITY_MMC_CARD                ((uint32_t)0x00000007)
111 
112 
113 typedef struct
114 {
115   SD_CSD SD_csd;
116   SD_CID SD_cid;
117   uint64_t CardCapacity;  // Card Capacity
118   uint32_t CardBlockSize; // Card Block Size
119   uint16_t RCA;
120   uint8_t CardType;
121 } SD_CardInfo;
122 
123 
124 extern SD_CardInfo SD_cardInfo;
125 
126 uint32_t SDIO_Init(uint32_t freq);
127 uint32_t SDIO_BlockWrite(uint32_t block_addr, uint32_t buff[]);
128 uint32_t SDIO_BlockRead(uint32_t block_addr, uint32_t buff[]);
129 
130 uint32_t SDIO_MultiBlockWrite(uint32_t block_addr, uint16_t block_cnt, uint32_t buff[]);
131 uint32_t SDIO_MultiBlockRead(uint32_t block_addr, uint16_t block_cnt, uint32_t buff[]);
132 
133 uint32_t SDIO_DMABlockWrite(uint32_t block_addr, uint16_t block_cnt, uint32_t buff[]);
134 uint32_t SDIO_DMABlockRead(uint32_t block_addr, uint16_t block_cnt, uint32_t buff[]);
135 
136 uint32_t _SDIO_SendCmd(uint32_t cmd, uint32_t arg, uint32_t resp_type, uint32_t *resp_data, uint32_t have_data, uint32_t data_read, uint16_t block_cnt, uint32_t use_dma);
137 
138 #define SDIO_SendCmd(cmd, arg, resp_type, resp_data)                                      _SDIO_SendCmd(cmd, arg, resp_type, resp_data, 0, 0, 0, 0)
139 #define SDIO_SendCmdWithData(cmd, arg, resp_type, resp_data, data_read, block_cnt)        _SDIO_SendCmd(cmd, arg, resp_type, resp_data, 1, data_read, block_cnt, 0)
140 #define SDIO_SendCmdWithDataByDMA(cmd, arg, resp_type, resp_data, data_read, block_cnt)   _SDIO_SendCmd(cmd, arg, resp_type, resp_data, 1, data_read, block_cnt, 1)
141 
142 void parseCID(uint32_t CID_Tab[4]);
143 void parseCSD(uint32_t CID_Tab[4]);
144 
145 uint32_t calcSDCLKDiv(uint32_t freq_sel);
146 
147 #endif //__SWM320_SDIO_H__
148