sd_protocol.h 46 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352
  1. #ifndef _H_SD_PROTOCOL
  2. #define _H_SD_PROTOCOL
  3. #include <linux/slab.h>
  4. #include <linux/cardreader/cardreader.h>
  5. #include <linux/cardreader/card_block.h>
  6. #include <linux/cardreader/sdio_hw.h>
  7. #include <mach/am_regs.h>
  8. #include <mach/irqs.h>
  9. #include <mach/card_io.h>
  10. typedef enum _SDIO_CMD_ERR {
  11. SDIO_NONE_ERR,
  12. SDIO_PACK_CRC_ERR,
  13. SDIO_PACK_TIMEOUT_ERR,
  14. SDIO_RES_CRC_ERR,
  15. SDIO_RES_TIMEOUT_ERR
  16. } SDIO_CMD_ERR;
  17. //Never change any sequence of following data variables
  18. #pragma pack(1)
  19. /**********************
  20. SDXC regs (9)
  21. ***********************/
  22. #define SDHC_BASE 0xC1108E00
  23. #define SDHC_ARGU 0x2380
  24. #define SDHC_SEND 0x2381
  25. #define SDHC_CTRL 0x2382
  26. #define SDHC_STAT 0x2383
  27. #define SDHC_CLKC 0x2384
  28. #define SDHC_ADDR 0x2385
  29. #define SDHC_PDMA 0x2386
  30. #define SDHC_MISC 0x2387
  31. #define SDHC_DATA 0x2388
  32. #define SDHC_ICTL 0x2389
  33. #define SDHC_ISTA 0x238A
  34. #define SDHC_SRST 0x238B
  35. extern int using_sdxc_controller;
  36. // 0 : SDHC_ARGU
  37. typedef struct SDXC_Arg_Reg {
  38. unsigned long command;
  39. } SDXC_Arg_Reg_t;
  40. // 1 : SDHC_SEND
  41. typedef struct SDXC_Send_Reg {
  42. unsigned command_index : 6;
  43. unsigned command_has_resp : 1;
  44. unsigned command_has_data : 1;
  45. unsigned response_length : 1;
  46. unsigned response_no_crc : 1;
  47. unsigned data_direction : 1;
  48. unsigned data_stop : 1;
  49. unsigned total_pack : 20;
  50. } SDXC_Send_Reg_t;
  51. /*
  52. //SDHC_CTRL
  53. typedef struct SDXC_Ctrl_Reg {
  54. unsigned dat_width : 2; //dat_type
  55. unsigned ddr_mode : 1;
  56. unsigned sdxc_soft_reset : 1;
  57. unsigned pack_len : 9; //0:512, 1:1, ..., 511:511
  58. unsigned rx_timeout : 7;
  59. ////unsigned sdxc_irq_en : 12; //detail
  60. unsigned dma_done_int_en : 1;
  61. unsigned sdio_dat1_int_en : 1; //@@
  62. unsigned rx_fifo_int_en : 1;
  63. unsigned tx_fifo_int_en : 1;
  64. unsigned data_complete_int_en : 1; //@@
  65. unsigned pack_crc_err_int_en : 1;
  66. unsigned pack_timeout_int_en : 1;
  67. unsigned pack_complete_int_en : 1;
  68. unsigned dat0_ready_int_en : 1;
  69. unsigned res_crc_err_int_en : 1;
  70. unsigned res_timeout_int_en : 1;
  71. unsigned res_ok_int_en : 1; //@@
  72. } SDXC_Ctrl_Reg_t;
  73. */
  74. // 2 : SDHC_CTRL
  75. typedef struct SDXC_Ctrl_Reg {
  76. unsigned dat_width : 2; //dat_type
  77. unsigned ddr_mode : 1;
  78. unsigned reserved1 : 1;
  79. unsigned pack_len : 9; //0:512, 1:1, ..., 511:511
  80. unsigned rx_timeout : 7;
  81. unsigned rc_period : 4;
  82. unsigned endian : 2;
  83. unsigned reserved2 : 6;
  84. } SDXC_Ctrl_Reg_t;
  85. /*
  86. //SDHC_STAT
  87. typedef struct SDXC_Status_Reg {
  88. unsigned busy : 1;
  89. unsigned dat_state : 4;
  90. unsigned cmd_state : 1;
  91. unsigned request_rx : 1; //??? set when rx_count >= rx_threshold
  92. unsigned request_tx : 1; //??? set when tx_count <= tx_threshold
  93. unsigned tx_count : 6;
  94. unsigned rx_count : 6;
  95. ////unsigned sdxc_irq_state : 12; //detail
  96. unsigned dma_done_int : 1;
  97. unsigned sdio_dat1_int : 1; //@@
  98. unsigned rx_fifo_int : 1;
  99. unsigned tx_fifo_int : 1;
  100. unsigned data_complete_int : 1; //@@
  101. unsigned pack_crc_err_int : 1;
  102. unsigned pack_timeout_int : 1;
  103. unsigned pack_complete_int : 1;
  104. unsigned dat0_ready_int : 1;
  105. unsigned res_crc_err_int : 1;
  106. unsigned res_timeout_int : 1;
  107. unsigned res_ok_int : 1; //@@
  108. } SDXC_Status_Reg_t;
  109. */
  110. // 3 : SDHC_STAT
  111. typedef struct SDXC_Status_Reg {
  112. unsigned busy : 1;
  113. unsigned dat_3_0 : 4;
  114. unsigned cmd_state : 1;
  115. unsigned reserved1 : 1;
  116. unsigned reserved2 : 1;
  117. unsigned rx_count : 6;
  118. unsigned tx_count : 6;
  119. unsigned dat_7_4 : 4;
  120. unsigned reserved3 : 8;
  121. } SDXC_Status_Reg_t;
  122. /*
  123. //SDHC_CLKC
  124. typedef struct SDXC_Clk_Reg {
  125. unsigned clk_div : 16;
  126. unsigned clk_in_sel : 3;
  127. unsigned clk_en : 1;
  128. unsigned phase_sel : 2;
  129. unsigned reserved1 : 1;
  130. unsigned clk_ctl_en : 1; //??? write 0, change, write 1
  131. unsigned sdio_dat1_irq2_en : 1;
  132. unsigned sdio_dat1_irq2_delay : 2;
  133. unsigned reserved2 : 5;
  134. } SDXC_Clk_Reg_t;
  135. */
  136. // 4 : SDHC_CLKC
  137. typedef struct SDXC_Clk_Reg {
  138. unsigned clk_div : 16;
  139. unsigned clk_in_sel : 3;
  140. unsigned clk_en : 1;
  141. unsigned rx_clk_phase_sel : 2;
  142. unsigned rx_clk_feedback_en : 1;
  143. unsigned clk_ctl_en : 1; //??? write 0, change, write 1
  144. unsigned clk_jic_control : 1; //???
  145. unsigned reserved1 : 7;
  146. } SDXC_Clk_Reg_t;
  147. // 5 : SDHC_ADDR
  148. typedef struct SDXC_Addr_Reg {
  149. unsigned long dma_addr;
  150. } SDXC_Addr_Reg_t;
  151. // 6 : SDHC_PDMA
  152. typedef struct SDXC_PDMA_Reg {
  153. unsigned dma_mode : 1; //0:PIO, 1:DMA
  154. unsigned pio_rd_resp : 3; //0:[39:8], 1:1st long, 2:2nd long, ..., 6 or 7:cmd arg
  155. unsigned dma_urgent : 1;
  156. unsigned wr_burst : 5;
  157. unsigned rd_burst : 5;
  158. unsigned rx_threshold : 6;
  159. unsigned tx_threshold : 6;
  160. unsigned rx_manual_flush : 1; //???
  161. unsigned reserved1 : 4;
  162. } SDXC_PDMA_Reg_t;
  163. /*
  164. //SDHC_MISC
  165. typedef struct SDXC_Misc_Reg {
  166. unsigned cmd_line_delay : 2; //for tuning
  167. unsigned dat_line_delay : 2; //for tuning
  168. unsigned manual_stop : 1;
  169. unsigned ext_ctrl : 1;
  170. unsigned burst_num : 6;
  171. unsigned thread_id : 6;
  172. unsigned pio_rd_flag : 1;
  173. unsigned pio_rd_mode : 1; //0:one time mode, 1:burst mode
  174. unsigned rx_limit : 6;
  175. unsigned tx_limit : 6;
  176. } SDXC_Misc_Reg_t;
  177. */
  178. // 7 : SDHC_MISC
  179. typedef struct SDXC_Misc_Reg {
  180. unsigned cmd_line_delay : 2; //for tuning
  181. unsigned dat_line_delay : 2; //for tuning
  182. unsigned rx_full_threshold : 6; //default : 30
  183. unsigned tx_empty_threshold : 6; //default : 0
  184. unsigned burst_num : 6;
  185. unsigned thread_id : 6;
  186. unsigned manual_stop : 1;
  187. unsigned reserved1 : 3;
  188. } SDXC_Misc_Reg_t;
  189. // 8 : SDHC_DATA
  190. typedef struct SDXC_Data_Reg {
  191. unsigned long value;
  192. } SDXC_Data_Reg_t;
  193. // 9 : SDHC_ICTL
  194. typedef struct SDXC_Ictl_Reg { //interrupt control
  195. unsigned res_ok_int_en : 1; //@@
  196. unsigned res_timeout_int_en : 1;
  197. unsigned res_crc_err_int_en : 1;
  198. unsigned dat0_ready_int_en : 1;
  199. unsigned pack_complete_int_en : 1;
  200. unsigned pack_timeout_int_en : 1;
  201. unsigned pack_crc_err_int_en : 1;
  202. unsigned data_complete_int_en : 1; //@@
  203. unsigned rx_fifo_int_en : 1;
  204. unsigned tx_fifo_int_en : 1;
  205. unsigned sdio_dat1_int_en : 1; //@@
  206. unsigned dma_done_int_en : 1;
  207. unsigned rx_fifo_full_int_en : 1;
  208. unsigned tx_fifo_empty_int_en : 1;
  209. unsigned additional_sdio_dat1_int_en : 1; //???
  210. unsigned reserved1 : 1;
  211. unsigned sdio_dat1_mask_delay : 2;
  212. unsigned reserved2 : 14;
  213. } SDXC_Ictl_Reg_t;
  214. // A : SDHC_ISTA
  215. typedef struct SDXC_Ista_Reg { //interrupt status
  216. unsigned res_ok_int : 1; //@@
  217. unsigned res_timeout_int : 1;
  218. unsigned res_crc_err_int : 1;
  219. unsigned dat0_ready_int : 1;
  220. unsigned pack_complete_int : 1;
  221. unsigned pack_timeout_int : 1;
  222. unsigned pack_crc_err_int : 1;
  223. unsigned data_complete_int : 1; //@@
  224. unsigned rx_fifo_int : 1;
  225. unsigned tx_fifo_int : 1;
  226. unsigned sdio_dat1_int : 1; //@@
  227. unsigned dma_done_int : 1;
  228. unsigned rx_fifo_full_int : 1;
  229. unsigned tx_fifo_empty_int : 1;
  230. unsigned additional_sdio_dat1_int : 1; //???
  231. unsigned reserved1 : 17;
  232. } SDXC_Ista_Reg_t;
  233. // B : SDHC_SRST
  234. typedef struct SDXC_Srst_Reg { //soft reset
  235. unsigned main_ctrl_srst : 1;
  236. unsigned rx_fifo_srst : 1;
  237. unsigned tx_fifo_srst : 1;
  238. unsigned rx_dphy_srst : 1; //manual clear
  239. unsigned tx_dphy_srst : 1; //manual clear
  240. unsigned dma_srst : 1;
  241. unsigned reserved1 : 26;
  242. } SDXC_Srst_Reg_t;
  243. typedef struct _SD_REG_SSR {
  244. unsigned Reserved1_1 : 5;
  245. unsigned SECURE_MODE : 1;
  246. unsigned DAT_BUS_WIDTH : 2;
  247. unsigned Reserved2 : 6;
  248. unsigned Reserved1_2 : 2;
  249. unsigned short SD_CARD_TYPE; // 16
  250. unsigned int SIZE_OF_PROTECTED_AREA; // 32
  251. unsigned char SPEED_CLASS; //8
  252. unsigned char PERPORMANCE_MOVE; //8
  253. unsigned Reserved3 : 4;
  254. unsigned AU_SIZE : 4;
  255. unsigned short ERASE_SIZE; // 16
  256. unsigned ERASE_OFFSET : 2;
  257. unsigned ERASE_TIMEOUT : 6;
  258. unsigned char Reserved4[11]; // 88
  259. unsigned char Reserved5[39]; // 312
  260. } SD_REG_SSR_t;
  261. //MSB->LSB, structure for Operation Conditions Register
  262. typedef struct _SD_REG_OCR {
  263. //unsigned Reserved0:6;
  264. unsigned S18A : 1;
  265. unsigned Reserved0 : 5;
  266. unsigned Card_Capacity_Status:1; //Card_High_capacity
  267. unsigned Card_Busy:1; //Card power up status bit (busy)
  268. unsigned VDD_28_29:1; /* VDD voltage 2.8 ~ 2.9 */
  269. unsigned VDD_29_30:1; /* VDD voltage 2.9 ~ 3.0 */
  270. unsigned VDD_30_31:1; /* VDD voltage 3.0 ~ 3.1 */
  271. unsigned VDD_31_32:1; /* VDD voltage 3.1 ~ 3.2 */
  272. unsigned VDD_32_33:1; /* VDD voltage 3.2 ~ 3.3 */
  273. unsigned VDD_33_34:1; /* VDD voltage 3.3 ~ 3.4 */
  274. unsigned VDD_34_35:1; /* VDD voltage 3.4 ~ 3.5 */
  275. unsigned VDD_35_36:1; /* VDD voltage 3.5 ~ 3.6 */
  276. unsigned VDD_20_21:1; /* VDD voltage 2.0 ~ 2.1 */
  277. unsigned VDD_21_22:1; /* VDD voltage 2.1 ~ 2.2 */
  278. unsigned VDD_22_23:1; /* VDD voltage 2.2 ~ 2.3 */
  279. unsigned VDD_23_24:1; /* VDD voltage 2.3 ~ 2.4 */
  280. unsigned VDD_24_25:1; /* VDD voltage 2.4 ~ 2.5 */
  281. unsigned VDD_25_26:1; /* VDD voltage 2.5 ~ 2.6 */
  282. unsigned VDD_26_27:1; /* VDD voltage 2.6 ~ 2.7 */
  283. unsigned VDD_27_28:1; /* VDD voltage 2.7 ~ 2.8 */
  284. unsigned Reserved4:1; /* MMC VDD voltage 1.45 ~ 1.50 */
  285. unsigned Reserved3:1; /* MMC VDD voltage 1.50 ~ 1.55 */
  286. unsigned Reserved2:1; /* MMC VDD voltage 1.55 ~ 1.60 */
  287. unsigned Reserved1:1; /* MMC VDD voltage 1.60 ~ 1.65 */
  288. unsigned VDD_16_17:1; /* VDD voltage 1.6 ~ 1.7 */
  289. unsigned VDD_17_18:1; /* VDD voltage 1.7 ~ 1.8 */
  290. unsigned VDD_18_19:1; /* VDD voltage 1.8 ~ 1.9 */
  291. unsigned VDD_19_20:1; /* VDD voltage 1.9 ~ 2.0 */
  292. } SD_REG_OCR_t;
  293. typedef struct _SDIO_REG_OCR {
  294. unsigned VDD_28_29:1; /* VDD voltage 2.8 ~ 2.9 */
  295. unsigned VDD_29_30:1; /* VDD voltage 2.9 ~ 3.0 */
  296. unsigned VDD_30_31:1; /* VDD voltage 3.0 ~ 3.1 */
  297. unsigned VDD_31_32:1; /* VDD voltage 3.1 ~ 3.2 */
  298. unsigned VDD_32_33:1; /* VDD voltage 3.2 ~ 3.3 */
  299. unsigned VDD_33_34:1; /* VDD voltage 3.3 ~ 3.4 */
  300. unsigned VDD_34_35:1; /* VDD voltage 3.4 ~ 3.5 */
  301. unsigned VDD_35_36:1; /* VDD voltage 3.5 ~ 3.6 */
  302. unsigned VDD_20_21:1; /* VDD voltage 2.0 ~ 2.1 */
  303. unsigned VDD_21_22:1; /* VDD voltage 2.1 ~ 2.2 */
  304. unsigned VDD_22_23:1; /* VDD voltage 2.2 ~ 2.3 */
  305. unsigned VDD_23_24:1; /* VDD voltage 2.3 ~ 2.4 */
  306. unsigned VDD_24_25:1; /* VDD voltage 2.4 ~ 2.5 */
  307. unsigned VDD_25_26:1; /* VDD voltage 2.5 ~ 2.6 */
  308. unsigned VDD_26_27:1; /* VDD voltage 2.6 ~ 2.7 */
  309. unsigned VDD_27_28:1; /* VDD voltage 2.7 ~ 2.8 */
  310. unsigned Reserved:8; /* reserved */
  311. } SDIO_REG_OCR_t;
  312. //MSB->LSB, structure for Card_Identification Register
  313. typedef struct _SD_REG_CID {
  314. unsigned char MID; //Manufacturer ID
  315. char OID[2]; //OEM/Application ID
  316. char PNM[5]; //Product Name
  317. unsigned char PRV; //Product Revision
  318. unsigned long PSN; //Serial Number
  319. unsigned MDT_high:4; //Manufacture Date Code
  320. unsigned Reserved:4;
  321. unsigned MDT_low:8; // MDT = (MDT_high << 8) | MDT_low
  322. unsigned NotUsed:1;
  323. unsigned CRC:7; //CRC7 checksum
  324. } SD_REG_CID_t;
  325. //MSB->LSB, structure for Card-Specific Data Register
  326. typedef struct _SD_REG_CSD {
  327. unsigned Reserved1:2;
  328. unsigned MMC_SPEC_VERS:4; //MMC Spec_vers
  329. unsigned CSD_STRUCTURE:2; //CSD structure
  330. unsigned TAAC:8; //data read access-time-1
  331. unsigned NSAC:8; //data read access-time-2 in CLK cycles(NSAC*100)
  332. unsigned TRAN_SPEED:8; //max. data transfer rate
  333. unsigned CCC_high:8; //card command classes
  334. unsigned READ_BL_LEN:4; //max. read data block length
  335. unsigned CCC_low:4; // CCC = (CCC_high << 4) | CCC_low
  336. unsigned C_SIZE_high:2; //device size
  337. unsigned Reserved2:2;
  338. unsigned DSR_IMP:1; //DSR implemented
  339. unsigned READ_BLK_MISALIGN:1; //read block misalignment
  340. unsigned WRITE_BLK_MISALIGN:1; //write block misalignment
  341. unsigned READ_BL_PARTIAL:1; //partial blocks for read allowed
  342. unsigned C_SIZE_mid:8;
  343. unsigned VDD_R_CURR_MAX:3; //max. read current @VDD max
  344. unsigned VDD_R_CURR_MIN:3; //max. read current @VDD min
  345. unsigned C_SIZE_low:2; // C_SIZE = (C_SIZE_high << 10) | (C_SIZE_mid << 2) | C_SIZE_low
  346. unsigned C_SIZE_MULT_high:2;
  347. unsigned VDD_W_CURR_MAX:3; //max. write current @VDD max
  348. unsigned VDD_W_CURR_MIN:3; //max. write current @VDD min
  349. unsigned SECTOR_SIZE_high:6; //erase sector size
  350. unsigned ERASE_BLK_EN:1; //erase single block enable
  351. unsigned C_SIZE_MULT_low:1; // C_SIZE_MULT = (C_SIZE_MULT_high << 1) | C_SIZE_MULT_low
  352. unsigned WP_GRP_SIZE:7; //write protect group size
  353. unsigned SECTOR_SIZE_low:1; // SECTOR_SIZE = (SECTOR_SIZE_high << 1) | SECTOR_SIZE_low
  354. unsigned WRITE_BL_LEN_high:2; //max. write data block length
  355. unsigned R2W_FACTOR:3; //write speed factor
  356. unsigned Reserved3:2;
  357. unsigned WP_GRP_ENABLE:1; //write protect group enable
  358. unsigned Reserved4:5;
  359. unsigned WRITE_BL_PARTIAL:1; //partial blocks for write allowed
  360. unsigned WRITE_BL_LEN_low:2; //WRITE_BL_LEN = (WRITE_BL_LEN_high << 2) | WRITE_BL_LEN_low
  361. unsigned Reserved5:2;
  362. unsigned FILE_FORMAT:2; //File format
  363. unsigned TMP_WRITE_PROTECT:1; //temporary write protection
  364. unsigned PERM_WRITE_PROTECT:1; //permanent write protection
  365. unsigned COPY:1; //copy flag (OTP)
  366. unsigned FILE_FORMAT_GRP:1; //File format group
  367. unsigned NotUsed:1;
  368. unsigned CRC:7; //CRC checksum
  369. } SD_REG_CSD_t;
  370. typedef struct _SDHC_REG_CSD {
  371. unsigned Reserved1:6;
  372. unsigned CSD_STRUCTURE:2; //CSD structure
  373. unsigned TAAC:8; //data read access-time-1
  374. unsigned NSAC:8; //data read access-time-2 in CLK cycles(NSAC*100)
  375. unsigned TRAN_SPEED:8; //max. data transfer rate
  376. unsigned CCC_high:8; //card command classes
  377. unsigned READ_BL_LEN:4; //max. read data block length
  378. unsigned CCC_low:4; // CCC = (CCC_high << 4) | CCC_low
  379. unsigned Reserved2:4;
  380. unsigned DSR_IMP:1; //DSR implemented
  381. unsigned READ_BLK_MISALIGN:1; //read block misalignment
  382. unsigned WRITE_BLK_MISALIGN:1; //write block misalignment
  383. unsigned READ_BL_PARTIAL:1; //partial blocks for read allowed
  384. unsigned C_SIZE_high:6; //device size
  385. unsigned Reserved3:2;
  386. unsigned C_SIZE_mid:8;
  387. unsigned C_SIZE_low:8;
  388. unsigned SECTOR_SIZE_high:6; //erase sector size
  389. unsigned ERASE_BLK_EN:1; //erase single block enable
  390. unsigned Reserved4:1;
  391. unsigned WP_GRP_SIZE:7; //write protect group size
  392. unsigned SECTOR_SIZE_low:1; // SECTOR_SIZE = (SECTOR_SIZE_high << 1) | SECTOR_SIZE_low
  393. unsigned WRITE_BL_LEN_high:2; //max. write data block length
  394. unsigned R2W_FACTOR:3; //write speed factor
  395. unsigned Reserved5:2;
  396. unsigned WP_GRP_ENABLE:1; //write protect group enable
  397. unsigned Reserved6:5;
  398. unsigned WRITE_BL_PARTIAL:1; //partial blocks for write allowed
  399. unsigned WRITE_BL_LEN_low:2; //WRITE_BL_LEN = (WRITE_BL_LEN_high << 2) | WRITE_BL_LEN_low
  400. unsigned Reserved7:2;
  401. unsigned FILE_FORMAT:2; //File format
  402. unsigned TMP_WRITE_PROTECT:1; //temporary write protection
  403. unsigned PERM_WRITE_PROTECT:1; //permanent write protection
  404. unsigned COPY:1; //copy flag (OTP)
  405. unsigned FILE_FORMAT_GRP:1; //File format group
  406. unsigned NotUsed:1;
  407. unsigned CRC:7; //CRC checksum
  408. } SDHC_REG_CSD_t;
  409. /*typedef struct _MMC_REG_EXT_CSD
  410. {
  411. unsigned char Reserved1[7];
  412. unsigned char S_CMD_SET;
  413. unsigned char Reserved2[300];
  414. unsigned char PWR_CL_26_360;
  415. unsigned char PWR_CL_52_360;
  416. unsigned char PWR_CL_26_195;
  417. unsigned char PWR_CL_52_195;
  418. unsigned char Reserved3[3];
  419. unsigned char CARD_TYPE;
  420. unsigned char Reserved4;
  421. unsigned char CSD_STRUCTURE;
  422. unsigned char Reserved5;
  423. unsigned char EXT_CSD_REV;
  424. unsigned char CMD_SET;
  425. unsigned char Reserved6;
  426. unsigned char CMD_SET_REV;
  427. unsigned char Reserved7;
  428. unsigned char POWER_CLASS;
  429. unsigned char Reserved8;
  430. unsigned char HS_TIMING;
  431. unsigned char Reserved9;
  432. unsigned char BUS_WIDTH;
  433. unsigned char Reserved10[183];
  434. } MMC_REG_EXT_CSD_t;*/// reserved for future use
  435. typedef struct _MMC_REG_EXT_CSD
  436. {
  437. unsigned char Reserved26[134]; //133
  438. unsigned char SEC_BAD_BLK_MGMNT; //134
  439. unsigned char Reserved25; //135
  440. unsigned char ENH_START_ADDR[4]; //136
  441. unsigned char ENH_SIZE_MULT[3]; //140
  442. unsigned char GP_SIZE_MULT[12]; //143
  443. unsigned char PARTITION_SETTING_COMPLETED; //155
  444. unsigned char PARTITIONS_ATTRIBUTE; //156
  445. unsigned char MAX_ENH_SIZE_MULT[3]; //157
  446. unsigned char PARTITIONING_SUPPORT; //160
  447. unsigned char HPI_MGMT; //161
  448. unsigned char RST_n_FUNCTION; //162
  449. unsigned char BKOPS_EN; //163
  450. unsigned char BKOPS_START; //164
  451. unsigned char Reserved24; //165
  452. unsigned char WR_REL_PARAM; //166
  453. unsigned char WR_REL_SET; //167
  454. unsigned char RPMB_SIZE_MULT; //168
  455. unsigned char FW_CONFIG; //169
  456. unsigned char Reserved23; //170
  457. unsigned char USER_WP; //171
  458. unsigned char Reserved22; //172
  459. unsigned char BOOT_WP; //173
  460. unsigned char Reserved21; //174
  461. unsigned char ERASE_GROUP_DEF; //175
  462. unsigned char Reserved20; //176
  463. unsigned char BOOT_BUS_WIDTH; //177
  464. unsigned char BOOT_CONFIG_PROT; //178
  465. unsigned char PARTITION_CONFIG; //179
  466. unsigned char Reserved19; //180
  467. unsigned char ERASED_MEM_CONT; //181
  468. unsigned char Reserved18; //182
  469. unsigned char BUS_WIDTH; //183
  470. unsigned char Reserved17; //184
  471. unsigned char HS_TIMING; //185
  472. unsigned char Reserved16; //186
  473. unsigned char POWER_CLASS; //187
  474. unsigned char Reserved15; //188
  475. unsigned char CMD_SET_REV; //189
  476. unsigned char Reserved14; //190
  477. unsigned char CMD_SET; //191
  478. unsigned char EXT_CSD_REV; //192
  479. unsigned char Reserved13; //193
  480. unsigned char CSD_STRUCTURE; //194
  481. unsigned char Reserved12; //195
  482. unsigned char CARD_TYPE; //196
  483. unsigned char Reserved11; //197
  484. unsigned char OUT_OF_INTERRUPT_TIME; //198
  485. unsigned char PARTITION_SWITCH_TIME; //199
  486. unsigned char PWR_CL_52_195; //200
  487. unsigned char PWR_CL_26_195; //201
  488. unsigned char PWR_CL_52_360; //202
  489. unsigned char PWR_CL_26_360; //203
  490. unsigned char Reserved10; //204
  491. unsigned char MIN_PERF_R_4_26; //205
  492. unsigned char MIN_PERF_W_4_26; //206
  493. unsigned char MIN_PERF_R_8_26_4_52; //207
  494. unsigned char MIN_PERF_W_8_26_4_52; //208
  495. unsigned char MIN_PERF_R_8_52; //209
  496. unsigned char MIN_PERF_W_8_52; //210
  497. unsigned char Reserved9; //211
  498. unsigned char SEC_COUNT[4]; //212
  499. unsigned char Reserved8; //216
  500. unsigned char S_A_TIMEOUT; //217
  501. unsigned char Reserved7; //218
  502. unsigned char S_C_VCCQ; //219
  503. unsigned char S_C_VCC; //220
  504. unsigned char HC_WP_GRP_SIZE; //221
  505. unsigned char REL_WR_SEC_C; //222
  506. unsigned char ERASE_TIMEOUT_MULT; //223
  507. unsigned char HC_ERASE_GRP_SIZE; //224
  508. unsigned char ACC_SIZE; //225
  509. unsigned char BOOT_SIZE_MULTI; //226
  510. unsigned char Reserved6; //227
  511. unsigned char BOOT_INFO; //228
  512. unsigned char SEC_TRIM_MULT; //229
  513. unsigned char SEC_ERASE_MULT; //230
  514. unsigned char SEC_FEATURE_SUPPORT; //231
  515. unsigned char TRIM_MULT; //232
  516. unsigned char Reserved5; //233
  517. unsigned char MIN_PERF_DDR_R_8_52; //234
  518. unsigned char MIN_PERF_DDR_W_8_52; //235
  519. unsigned char Reserved4[2]; //236
  520. unsigned char PWR_CL_DDR_52_195; //238
  521. unsigned char PWR_CL_DDR_52_360; //239
  522. unsigned char Reserved3; //240
  523. unsigned char INI_TIMEOUT_AP; //241
  524. unsigned char CORRECTLY_PRG_SECTORS_NUM[4]; //242
  525. unsigned char BKOPS_STATUS; //246
  526. unsigned char Reserved2[255]; //247
  527. unsigned char BKOPS_SUPPORT; //502
  528. unsigned char HPI_FEATURES; //503
  529. unsigned char S_CMD_SET; //504
  530. unsigned char Reserved1[7]; //505
  531. } MMC_REG_EXT_CSD_t;
  532. //MSB->LSB, structure for SD CARD Configuration Register
  533. typedef struct _SD_REG_SCR {
  534. unsigned SD_SPEC:4; //SD Card¡ªSpec. Version
  535. unsigned SCR_STRUCTURE:4; //SCR Structure
  536. unsigned SD_BUS_WIDTHS:4; //DAT Bus widths supported
  537. unsigned SD_SECURITY:3; //SD Security Support
  538. unsigned DATA_STAT_AFTER_ERASE:1; //data_status_after erases
  539. //unsigned Reserved1:16; //for alignment
  540. unsigned CMD_SUPPORT : 2;
  541. unsigned Reserved1 : 13;
  542. unsigned SD_SPEC3 : 1;
  543. unsigned long Reserved2;
  544. } SD_REG_SCR_t;
  545. typedef struct _SDIO_REG_CCCR {
  546. unsigned char CCCR_SDIO_SPEC; //cccr and sdio spec verion low four bits(cccr) high four bits(sdio)
  547. unsigned char SD_SPEC; //SD Card¡ªSpec. Version low four bits
  548. unsigned char IO_ENABLE; //
  549. unsigned char IO_READY; //
  550. unsigned char INT_ENABLE; //
  551. unsigned char INT_PENDING;
  552. unsigned char INT_ABORT;
  553. unsigned char BUS_Interface_Control;
  554. unsigned char Card_Capability;
  555. unsigned short Common_CIS_Pointer1;
  556. unsigned char Common_CIS_Pointer2;
  557. unsigned char BUS_Suspend;
  558. unsigned char Function_Select;
  559. unsigned char Exec_Flags;
  560. unsigned short FN0_Block_Size;
  561. unsigned char Power_Control;
  562. unsigned char High_Speed;
  563. unsigned char RFU[220]; //
  564. unsigned char Reserved[16];
  565. } SDIO_REG_CCCR_t;
  566. typedef struct _SD_REG_DSR {
  567. } SD_REG_DSR_t;
  568. //MSB->LSB, structrue for SD Card Status
  569. typedef struct _SD_Card_Status {
  570. unsigned LOCK_UNLOCK_FAILED:1; //Set when a sequence or password error has been detected in lock/ unlock card command or if there was an attempt to access a locked card
  571. unsigned CARD_IS_LOCKED:1; //When set, signals that the card is locked by the host
  572. unsigned WP_VIOLATION:1; //Attempt to program a write-protected block.
  573. unsigned ERASE_PARAM:1; //An invalid selection of write-blocks for erase occurred.
  574. unsigned ERASE_SEQ_ERROR:1; //An error in the sequence of erase commands occurred.
  575. unsigned BLOCK_LEN_ERROR:1; //The transferred block length is not allowed for this card, or the number of transferred bytes does not match the block length.
  576. unsigned ADDRESS_ERROR:1; //A misaligned address that did not match the block length was used in the command.
  577. unsigned OUT_OF_RANGE:1; //The command¡¯s argument was out of the allowed range for this card.
  578. unsigned CID_CSD_OVERWRITE:1; //Can be either one of the following errors:
  579. unsigned Reserved1:1;
  580. unsigned Reserved2:1;
  581. unsigned ERROR:1; //A general or an unknown error occurred during the operation.
  582. unsigned CC_ERROR:1; //Internal card controller error
  583. unsigned CARD_ECC_FAILED:1; //Card internal ECC was applied but failed to correct the data.
  584. unsigned ILLEGAL_COMMAND:1; //Command not legal for the card state
  585. unsigned COM_CRC_ERROR:1; //The CRC check of the previous command failed.
  586. unsigned READY_FOR_DATA:1; //Corresponds to buffer empty signalling on the bus.
  587. unsigned CURRENT_STATE:4; //The state of the card when receiving the command.
  588. unsigned ERASE_RESET:1; //An erase sequence was cleared beforem executing because an out of erase sequence command was received.
  589. unsigned CARD_ECC_DISABLED:1; //The command has been executed without using the internal ECC.
  590. unsigned WP_ERASE_SKIP:1; //Only partial address space was erased due to existing write protected blocks.
  591. unsigned Reserved3:2;
  592. unsigned Reserved4:1;
  593. unsigned AKE_SEQ_ERROR:1; //Error in the sequence of authentication process.
  594. unsigned Reserved5:1;
  595. unsigned APP_CMD:1; //The card will expect ACMD, or indication that the command has been interpreted as ACMD.
  596. unsigned NotUsed:2;
  597. } SD_Card_Status_t;
  598. //MSB->LSB, structure for SD SD_Status
  599. typedef struct _SD_SD_Status {
  600. unsigned Reserved1:5;
  601. unsigned SECURED_MODE:1; //Card is in Secured Mode of operation (refer to the SD Security Specifications document).
  602. unsigned DAT_BUS_WIDTH:2; //Shows the currently defined data bus width that was defined by the SET_sd_info.bus_width command.
  603. unsigned Reserved2:8;
  604. unsigned SIZE_OF_PROTECTED_AREA:2; //Shows the size of the protected area. The actual area = (SIZE_OF_PROTECTED_AREA) * MULT * BLOCK_LEN.
  605. unsigned SD_CARD_TYPE:6; //In the future, the 8 LSBs will be used to define different variations of an SD Card (each bit will define different SD types).
  606. unsigned Reserved3:8; //just for bit structure alignment
  607. unsigned char Reserved4[16];
  608. unsigned char Reserved5[39];
  609. } SD_SD_Status_t;
  610. typedef struct _SD_Switch_Function__Status {
  611. unsigned short Max_Current_Consumption;
  612. unsigned short Function_Group[6];
  613. unsigned Function_Group_Status5:4;
  614. unsigned Function_Group_Status6:4;
  615. unsigned Function_Group_Status3:4;
  616. unsigned Function_Group_Status4:4;
  617. unsigned Function_Group_Status1:4;
  618. unsigned Function_Group_Status2:4;
  619. unsigned char Data_Struction_Verion;
  620. unsigned short Function_Status_In_Group[6];
  621. unsigned char Reserved[34];
  622. } SD_Switch_Function_Status_t;
  623. //structure for response
  624. typedef struct _SD_Response_R1 {
  625. unsigned char command; //command index = bit 6:0
  626. SD_Card_Status_t card_status; //card status
  627. unsigned end_bit:1; //end bit = bit 0
  628. unsigned crc7:7; //CRC7 = bit 7:1
  629. } SD_Response_R1_t;
  630. typedef struct _SD_Response_R2_CID {
  631. unsigned char reserved; //should be 0x3F
  632. SD_REG_CID_t cid; //response CID
  633. unsigned end_bit:1; //end bit = bit 0
  634. unsigned crc7:7; //CRC7 = bit 7:1
  635. } SD_Response_R2_CID_t;
  636. typedef struct _SD_Response_R2_CSD {
  637. unsigned char reserved; //should be 0x3F
  638. SD_REG_CSD_t csd; //response CSD
  639. unsigned end_bit:1; //end bit = bit 0
  640. unsigned crc7:7; //CRC7 = bit 7:1
  641. } SD_Response_R2_CSD_t;
  642. typedef struct _SDHC_Response_R2_CSD {
  643. unsigned char reserved; //should be 0x3F
  644. SDHC_REG_CSD_t csd; //response CSD
  645. unsigned end_bit:1; //end bit = bit 0
  646. unsigned crc7:7; //CRC7 = bit 7:1
  647. } SDHC_Response_R2_CSD_t;
  648. typedef struct _SD_Response_R3 {
  649. unsigned char reserved1; //should be 0x3F
  650. SD_REG_OCR_t ocr; //OCR register
  651. unsigned end_bit:1; //end bit = bit 0
  652. unsigned reserved2:7; //should be 0x7F
  653. } SD_Response_R3_t;
  654. typedef struct _SDIO_Response_R4 {
  655. unsigned char reserved1; //should be 0x3F
  656. unsigned Stuff_bits:3;
  657. unsigned Memory_Present:1;
  658. unsigned IO_Function_No:3;
  659. unsigned Card_Ready:1;
  660. SDIO_REG_OCR_t ocr; //OCR register
  661. unsigned end_bit:1; //end bit = bit 0
  662. unsigned reserved2:7; //should be 0x7F
  663. } SDIO_Response_R4_t;
  664. typedef struct _SDIO_RW_CMD_Response_R5 {
  665. unsigned command:6; //command index = bit 6:0
  666. unsigned direct_bit:1;
  667. unsigned start_bit:1;
  668. unsigned short stuff; //not used
  669. unsigned Out_Of_Range:1; //status of sdio card
  670. unsigned Function_Number:1;
  671. unsigned RFU:1;
  672. unsigned Error:1;
  673. unsigned IO_Current_State:2;
  674. unsigned Illegal_CMD:1;
  675. unsigned CMD_CRC_Error:1;
  676. unsigned char read_or_write_data; //read back data
  677. unsigned end_bit:1; //end bit = bit 0
  678. unsigned crc7:7; //CRC7 = bit 7:1
  679. } SDIO_RW_CMD_Response_R5_t;
  680. typedef struct _SD_Response_R6 {
  681. unsigned char command; //command index = bit 6:0
  682. unsigned char rca_high; //New published RCA [31:16] of the card
  683. unsigned char rca_low;
  684. unsigned short part_card_status; //[15:0] card status bits: 23,22,19,12:0
  685. unsigned end_bit:1; //end bit = bit 0
  686. unsigned crc7:7; //CRC7 = bit 7:1
  687. } SD_Response_R6_t;
  688. typedef struct _SD_Response_R7 {
  689. unsigned char command; //command index = bit 6:0
  690. unsigned reserved1:4;
  691. unsigned cmd_version:4; //0:voltage check
  692. unsigned char reserved;
  693. unsigned voltage_accept:4; //0001b:2.7v-3.6v 0010b:1.65v-1.95v
  694. unsigned reserved2:4;
  695. unsigned char check_pattern;
  696. unsigned end_bit:1; //end bit = bit 0
  697. unsigned crc7:7; //CRC7 = bit 7:1
  698. } SD_Response_R7_t;
  699. typedef struct _SDIO_IO_RW_CMD_ARG {
  700. unsigned char write_data_bytes; //write data bytes count
  701. unsigned stuff1:1; //not used
  702. unsigned Register_Address:17; //byte address of select fuction to read
  703. unsigned stuff2:1; //not used
  704. unsigned RAW_Flag:1; //read after write flag
  705. unsigned Function_No:3; //function number of wish to read or write
  706. unsigned R_W_Flag:1; //the direction of I/O operation
  707. } SDIO_IO_RW_CMD_ARG_t;
  708. typedef struct _SDIO_IO_RW_EXTENDED_ARG {
  709. unsigned Byte_Block_Count:9; //bytes or block count
  710. unsigned Register_Address:17; //start address of I/O register
  711. unsigned OP_Code:1; //define the read write operation
  712. unsigned Block_Mode:1; //Block or byte mode of read and write
  713. unsigned Function_No:3; //function number of wish to read or write
  714. unsigned R_W_Flag:1; //the direction of I/O operation
  715. } SDIO_IO_RW_EXTENDED_ARG;
  716. #pragma pack()
  717. typedef enum _SD_Card_State {
  718. STATE_UNKNOWN = -1,
  719. STATE_INACTIVE = 0,
  720. STATE_IDLE,
  721. STATE_READY,
  722. STATE_IDENTIFICATION,
  723. STATE_STAND_BY,
  724. STATE_TRANSFER,
  725. STATE_SENDING_DATA,
  726. STATE_RECEIVE_DATA,
  727. STATE_PROGRAMMING,
  728. STATE_DISCONNECT
  729. } SD_Card_State_t;
  730. typedef enum _SD_Response_Type {
  731. RESPONSE_NONE = -1,
  732. RESPONSE_R1 = 0,
  733. RESPONSE_R1B,
  734. RESPONSE_R2_CID,
  735. RESPONSE_R2_CSD,
  736. RESPONSE_R3,
  737. RESPONSE_R4, //SD, responses are not supported.
  738. RESPONSE_R5, //SD, responses are not supported.
  739. RESPONSE_R6,
  740. RESPONSE_R7
  741. } SD_Response_Type_t;
  742. /* Error codes */
  743. typedef enum _SD_Error_Status_t {
  744. SD_MMC_NO_ERROR = 0,
  745. SD_MMC_ERROR_OUT_OF_RANGE, //Bit 31
  746. SD_MMC_ERROR_ADDRESS, //Bit 30
  747. SD_MMC_ERROR_BLOCK_LEN, //Bit 29
  748. SD_MMC_ERROR_ERASE_SEQ, //Bit 28
  749. SD_MMC_ERROR_ERASE_PARAM, //Bit 27
  750. SD_MMC_ERROR_WP_VIOLATION, //Bit 26
  751. SD_ERROR_CARD_IS_LOCKED, //Bit 25
  752. SD_ERROR_LOCK_UNLOCK_FAILED, //Bit 24
  753. SD_MMC_ERROR_COM_CRC, //Bit 23
  754. SD_MMC_ERROR_ILLEGAL_COMMAND, //Bit 22
  755. SD_ERROR_CARD_ECC_FAILED, //Bit 21
  756. SD_ERROR_CC, //Bit 20
  757. SD_MMC_ERROR_GENERAL, //Bit 19
  758. SD_ERROR_Reserved1, //Bit 18
  759. SD_ERROR_Reserved2, //Bit 17
  760. SD_MMC_ERROR_CID_CSD_OVERWRITE, //Bit 16
  761. SD_ERROR_AKE_SEQ, //Bit 03
  762. SD_MMC_ERROR_STATE_MISMATCH,
  763. SD_MMC_ERROR_HEADER_MISMATCH,
  764. SD_MMC_ERROR_DATA_CRC,
  765. SD_MMC_ERROR_TIMEOUT,
  766. SD_MMC_ERROR_DRIVER_FAILURE,
  767. SD_MMC_ERROR_WRITE_PROTECTED,
  768. SD_MMC_ERROR_NO_MEMORY,
  769. SD_ERROR_SWITCH_FUNCTION_COMUNICATION,
  770. SD_ERROR_NO_FUNCTION_SWITCH,
  771. SD_MMC_ERROR_NO_CARD_INS,
  772. SD_MMC_ERROR_READ_DATA_FAILED,
  773. SD_SDIO_ERROR_NO_FUNCTION
  774. } SD_Error_Status_t;
  775. typedef enum _SD_Operation_Mode {
  776. CARD_INDENTIFICATION_MODE = 0, //fod = 100 ~ 400 Khz, OHz stops the clock. The given minimum frequency range is for cases where a continuous clock is required.
  777. DATA_TRANSFER_MODE = 1 //fpp = 0 ~ 25 Mhz
  778. } SD_Operation_Mode_t;
  779. typedef enum _SD_Bus_Width {
  780. SD_BUS_SINGLE = 1, //only DAT0
  781. SD_BUS_WIDE = 4 //use DAT0-4
  782. } SD_Bus_Width_t;
  783. typedef enum SD_Card_Type {
  784. CARD_TYPE_NONE = 0,
  785. CARD_TYPE_SD,
  786. CARD_TYPE_SDHC,
  787. CARD_TYPE_MMC,
  788. CARD_TYPE_EMMC,
  789. CARD_TYPE_SDIO
  790. } SD_Card_Type_t;
  791. typedef enum SDIO_Card_Type {
  792. CARD_TYPE_NONE_SDIO =0,
  793. CARD_TYPE_SDIO_STD_UART,
  794. CARD_TYPE_SDIO_BT_TYPEA,
  795. CARD_TYPE_SDIO_BT_TYPEB,
  796. CARD_TYPE_SDIO_GPS,
  797. CARD_TYPE_SDIO_CAMERA,
  798. CARD_TYPE_SDIO_PHS,
  799. CARD_TYPE_SDIO_WLAN,
  800. CARD_TYPE_SDIO_OTHER_IF
  801. } SDIO_Card_Type_t;
  802. typedef enum SD_SPEC_VERSION {
  803. SPEC_VERSION_10_101,
  804. SPEC_VERSION_110,
  805. SPEC_VERSION_20,
  806. SPEC_VERSION_30
  807. } SD_SPEC_VERSION_t;
  808. typedef enum MMC_SPEC_VERSION {
  809. SPEC_VERSION_10_12,
  810. SPEC_VERSION_14,
  811. SPEC_VERSION_20_22,
  812. SPEC_VERSION_30_33,
  813. SPEC_VERSION_40_41
  814. } MMC_SPEC_VERSION_t;
  815. typedef enum SD_SPEED_CLASS {
  816. NORMAL_SPEED,
  817. HIGH_SPEED
  818. } SD_SPEED_CLASS_t;
  819. //function group 4
  820. typedef enum SD_CURRENT_LIMIT {
  821. CURRENT_200mA = 0x01,
  822. CURRENT_400mA = 0x02,
  823. CURRENT_600mA = 0x04,
  824. CURRENT_800mA = 0x08
  825. } SD_CURRENT_LIMIT_t;
  826. //function group 3
  827. typedef enum SD_DRIVER_STRENGTH {
  828. TYPE_B = 0x01,
  829. TYPE_A = 0x02,
  830. TYPE_C = 0x04,
  831. TYPE_D = 0x08
  832. } SD_DRIVER_STRENGTH_t;
  833. //function group 1
  834. typedef enum SD_UHS_I_MODE {
  835. SDR12 = 0x01,
  836. SDR25 = 0x02,
  837. SDR50 = 0x04,
  838. SDR104 = 0x08,
  839. DDR50 = 0x10
  840. } SD_UHS_I_MODE_t;
  841. typedef struct SD_MMC_Card_Info {
  842. SD_Card_Type_t card_type;
  843. SDIO_Card_Type_t sdio_card_type[8];
  844. SD_Operation_Mode_t operation_mode;
  845. SD_Bus_Width_t bus_width;
  846. SD_SPEC_VERSION_t spec_version;
  847. MMC_SPEC_VERSION_t mmc_spec_version;
  848. SD_SPEED_CLASS_t speed_class;
  849. SD_UHS_I_MODE_t uhs_mode;
  850. SD_REG_CID_t raw_cid;
  851. SDIO_Pad_Type_t io_pad_type; /* hw io pin pad */
  852. unsigned short card_rca;
  853. unsigned char sdio_function_nums;
  854. unsigned sdio_clk_unit;
  855. unsigned sdio_clk;
  856. unsigned blk_len;
  857. unsigned sdio_blk_len[8];
  858. unsigned sdio_cis_addr[8];
  859. unsigned blk_nums;
  860. unsigned clks_nac;
  861. int write_protected_flag;
  862. int inited_flag;
  863. int removed_flag;
  864. int init_retry;
  865. int single_blk_failed;
  866. int sdio_init_flag;
  867. unsigned emmc_boot_support;
  868. unsigned emmc_boot_partition_size[2];
  869. unsigned sd_save_hw_io_flag;
  870. unsigned sd_save_hw_io_config;
  871. unsigned sd_save_hw_io_mult_config;
  872. unsigned read_multi_block_failed;
  873. unsigned write_multi_block_failed;
  874. unsigned sdio_read_crc_close;
  875. unsigned sd_mmc_power_delay ;
  876. unsigned disable_high_speed;
  877. unsigned disable_wide_bus;
  878. unsigned max_blk_count;
  879. unsigned sdxc_save_hw_io_flag;
  880. unsigned sdxc_save_hw_io_ctrl;
  881. unsigned sdxc_save_hw_io_clk;
  882. unsigned support_uhs_mode;
  883. unsigned char *sd_mmc_buf;
  884. unsigned char *sd_mmc_phy_buf;
  885. void (*sd_mmc_power) (int power_on);
  886. int (*sd_mmc_get_ins) (void);
  887. int (*sd_get_wp) (void);
  888. void (*sd_mmc_io_release) (void);
  889. } SD_MMC_Card_Info_t;
  890. #define EMMC_BOOT_SIZE_UNIT (128*1024) // for emmc boot size
  891. //SDIO_REG_DEFINE
  892. #define CCCR_SDIO_SPEC_REG 0x00
  893. #define SD_SPEC_REG 0x01
  894. #define IO_ENABLE_REG 0x02
  895. #define IO_READY_REG 0x03
  896. #define INT_ENABLE_REG 0x04
  897. #define INT_PENDING_REG 0x05
  898. #define IO_ABORT_REG 0x06
  899. #define BUS_Interface_Control_REG 0x07
  900. #define Card_Capability_REG 0x08
  901. #define Common_CIS_Pointer1_REG 0x09
  902. #define Common_CIS_Pointer2_REG 0x0a
  903. #define Common_CIS_Pointer3_REG 0x0b
  904. #define BUS_Suspend_REG 0x0c
  905. #define Function_Select_REG 0x0d
  906. #define Exec_Flags_REG 0x0e
  907. #define Ready_Flags_REG 0x0f
  908. #define FN0_Block_Size_Low_REG 0x10
  909. #define FN0_Block_Size_High_REG 0x11
  910. #define Power_Control_REG 0x12
  911. #define High_Speed_REG 0x13
  912. #define FN1_Block_Size_Low_REG 0x110
  913. #define FN1_Block_Size_High_REG 0x111
  914. #define SDIO_Read_Data 0
  915. #define SDIO_Write_Data 1
  916. #define SDIO_DONT_Read_After_Write 0
  917. #ifdef CONFIG_SDIO_MARVELL_NH387_WIFI
  918. #define SDIO_Read_After_Write 0 //MARVELL_NH387 can't support verify check
  919. #else
  920. #define SDIO_Read_After_Write 1
  921. #endif
  922. #define SDIO_Block_MODE 1
  923. #define SDIO_Byte_MODE 0
  924. #define SDIO_Wide_bus_Bit 0x02
  925. #define SDIO_Single_bus_Bit 0x00
  926. #define SDIO_Support_High_Speed 0x01
  927. #define SDIO_Enable_High_Speed 0x02
  928. #define SDIO_Support_Multi_Block 0x02
  929. #define SDIO_INT_EN_MASK 0x01
  930. #define SDIO_E4MI_EN_MASK 0x20
  931. #define SDIO_RES_bit 0x08
  932. #define SDIO_BLOCK_SIZE 512
  933. //SD/MMC Card bus commands CMD type argument response
  934. //Broadcast Commands (bc), no response
  935. //Broadcast Commands with Response (bcr)
  936. //Addressed (point-to-point) Commands (ac)¡ªno data transfer on DAT
  937. //Addressed (point-to-point) Data Transfer Commands (adtc)¡ªdata transfer on DAT.
  938. #define APP_SPECIFIC 0x0
  939. /* Class 0 and 1, Basic Commands */
  940. #define SD_MMC_GO_IDLE_STATE 0 //--- [31:0] don¡¯t care --------
  941. #define MMC_SEND_OP_COND 1 //bcr [31:0] OCR w/out busy R3
  942. #define SD_MMC_ALL_SEND_CID 2 //bcr [31:0] don¡¯t care R2
  943. #define SD_MMC_SEND_RELATIVE_ADDR 3 //bcr [31:0] don¡¯t care R6 for SD and R1 for MMC
  944. // Reserved 4 ----- ---------- --------
  945. #define IO_SEND_OP_COND 5 //bcr [23:0] OCR w/out busy R4
  946. #define SD_SET_BUS_WIDTHS (6 | APP_SPECIFIC) //ac [31:2]stuff,[1:0]B/W R1 ,Application Specific Commands Used
  947. #define MMC_SWITCH_FUNTION 6 //MMC_c mmc switch power clock bus_width cmd
  948. #define SD_SWITCH_FUNCTION 46 //46 //bcr [31:23]mode,[22:8]default bit,[7:0]function
  949. #define SD_MMC_SELECT_DESELECT_CARD 7 //ac [31:16] RCA R1
  950. #define SD_SEND_IF_COND 8 // [11:8]supply voltage R7
  951. #define MMC_SEND_EXT_CSD 48 //ac [31:0]stuff R1
  952. #define SD_MMC_SEND_CSD 9 //ac [31:16] RCA R2
  953. #define SD_MMC_SEND_CID 10 //ac [31:16] RCA R2
  954. ////#define SD_READ_DAT_UNTIL_STOP 11 //adtc [31:0] data address R1
  955. #define VOLTAGE_SWITCH 11
  956. #define SD_MMC_STOP_TRANSMISSION 12 //ac [31:0] don¡¯t care R1b
  957. #define SD_MMC_SEND_STATUS (13 | APP_SPECIFIC) //ac [31:16] RCA R1, Application Specific Commands Used
  958. // Reserved 14 ----- ---------- --------
  959. #define SD_MMC_GO_INACTIVE_STATE 15 //ac [31:16] RCA --------
  960. /* Class 2, Block Read Commands */
  961. #define SD_MMC_SET_BLOCKLEN 16 //ac [31:0] block length R1
  962. #define SD_MMC_READ_SINGLE_BLOCK 17 //adtc [31:0] data address R1
  963. #define SD_MMC_READ_MULTIPLE_BLOCK 18 //adtc [31:0] data address R1
  964. #define SD_SEND_TUNNING_PATTERN 19 //adtc [31:0] data address R1
  965. // Reserved 20 ----- ---------- --------
  966. // Reserved 21 ----- ---------- --------
  967. #define SD_SEND_NUM_WR_BLOCKS (22 | APP_SPECIFIC)//adtc [31:0] stuff bits R1 ,Application Specific Commands Used
  968. #define SD_SET_WR_BLK_ERASE_COUNT (23 | APP_SPECIFIC)//ac [31:23]stuff,[22:0]B/N R1 ,Application Specific Commands Used
  969. /* Class 4, Block Write Commands */
  970. #define SD_MMC_WRITE_BLOCK 24 //adtc [31:0] data address R1
  971. #define SD_MMC_WRITE_MULTIPLE_BLOCK 25 //adtc [31:0] data address R1
  972. // Reserved 26 ----- ---------- --------
  973. #define SD_MMC_PROGRAM_CSD 27 //adtc [31:0] don¡¯t care* R1
  974. /* Class 6, Write Protection */
  975. #define SD_MMC_SET_WRITE_PROT 28 //ac [31:0] data address R1b
  976. #define SD_MMC_CLR_WRITE_PROT 29 //ac [31:0] data address R1b
  977. #define SD_MMC_SEND_WRITE_PROT 30 //adtc [31:0] WP data address R1
  978. // Reserved 31 ----- ---------- --------
  979. /* Class 5, Erase Commands */
  980. #define SD_ERASE_WR_BLK_START 32 //ac [31:0] data address R1
  981. #define MMC_TAG_SECTOR_START 32 //ac [31:0] data address R1
  982. #define SD_ERASE_WR_BLK_END 33 //ac [31:0] data address R1
  983. #define MMC_TAG_SECTOR_END 33 //ac [31:0] data address R1
  984. #define MMC_UNTAG_SECTOR 34 //ac [31:0] data address R1
  985. #define MMC_TAG_ERASE_GROUP_START 35 //ac [31:0] data address R1
  986. #define MMC_TAG_ERASE_GROUP_END 36 //ac [31:0] data address R1
  987. #define MMC_UNTAG_ERASE_GROUP 37 //ac [31:0] data address R1
  988. #define SD_MMC_ERASE 38 //ac [31:0] don¡¯t care R1b
  989. // Reserved 39 ----- ---------- --------
  990. // Reserved 40 ----- ---------- --------
  991. #define SD_APP_OP_COND (41 | APP_SPECIFIC) //bcr [31:0]OCR without busy R3 ,Application Specific Commands Used
  992. /* Class 7, Lock Card Commands */
  993. #define SD_SET_CLR_CARD_DETECT (42 | APP_SPECIFIC) //ac [31:1]stuff,[0]set_cd R1, Application Specific Commands Used
  994. #define MMC_LOCK_UNLOCK 42 //adtc [31:0] stuff bits R1b
  995. // SDA Optional Commands 43 ----- ---------- --------
  996. // SDA Optional Commands 44 ----- ---------- --------
  997. // SDA Optional Commands 45 ----- ---------- --------
  998. // SDA Optional Commands 46 ----- ---------- --------
  999. // SDA Optional Commands 47 ----- ---------- --------
  1000. // SDA Optional Commands 48 ----- ---------- --------
  1001. // SDA Optional Commands 49 ----- ---------- --------
  1002. // SDA Optional Commands 50 ----- ---------- --------
  1003. #define SD_SEND_SCR (51 | APP_SPECIFIC) //adtc [31:0] staff bits R1 ,Application Specific Commands Used
  1004. #define IO_RW_DIRECT 52 //R5
  1005. #define IO_RW_EXTENDED 53 //R5----- ---------- --------
  1006. // SDA Optional Commands 54 ----- ---------- --------
  1007. /* Class 8, Application Specific Commands */
  1008. #define SD_APP_CMD 55 //ac [31:16]RCA,[15:0]stuff R1
  1009. #define SD_GEN_CMD 56 //adtc [31:1]stuff,[0]RD/WR R1
  1010. // Reserved 57 ----- ---------- --------
  1011. // Reserved 58 ----- ---------- --------
  1012. // Reserved 59 ----- ---------- --------
  1013. // Rsserved for Manufacturer 60 ----- ---------- --------
  1014. // Rsserved for Manufacturer 61 ----- ---------- --------
  1015. // Rsserved for Manufacturer 62 ----- ---------- --------
  1016. // Rsserved for Manufacturer 63 ----- ---------- --------
  1017. //All timing values definition for NAND MMC and SD-based Products
  1018. #define SD_MMC_TIME_NCR_MIN 2 /* min. of Number of cycles
  1019. between command and response */
  1020. #define SD_MMC_TIME_NCR_MAX (128*10) /* max. of Number of cycles
  1021. between command and response */
  1022. #define SD_MMC_TIME_NID 5 /* Number of cycles
  1023. between card identification or
  1024. card operation conditions command
  1025. and the corresponding response */
  1026. #define SD_MMC_TIME_NAC_MIN 2 /* min. of Number of cycles
  1027. between command and
  1028. the start of a related data block */
  1029. #define SD_MMC_TIME_NRC_MIN 8 /* min. of Number of cycles
  1030. between the last reponse and
  1031. a new command */
  1032. #define SD_MMC_TIME_NCC_MIN 8 /* min. of Number of cycles
  1033. between two commands, if no reponse
  1034. will be send after the first command
  1035. (e.g. broadcast) */
  1036. #define SD_MMC_TIME_NWR_MIN 2 /* min. of Number of cycles
  1037. between a write command and
  1038. the start of a related data block */
  1039. #define SD_MMC_TIME_NRC_NCC 16 /* actual NRC/NCC time used in source code */
  1040. #define SD_MMC_TIME_NWR 8 /* actual Nwr time used in source code */
  1041. #define SD_MMC_Z_CMD_TO_RES 2 /* number of Z cycles
  1042. allowing time for direction switching on the bus) */
  1043. #define SD_MMC_TIME_NAC_DEFAULT 25500
  1044. //Misc definitions
  1045. #define MAX_RESPONSE_BYTES 20
  1046. #define RESPONSE_R1_R3_R4_R5_R6_R7_LENGTH 6
  1047. #define RESPONSE_R2_CID_CSD_LENGTH 17
  1048. #define RESPONSE_NONE_LENGTH 0
  1049. #define SD_MMC_IDENTIFY_TIMEOUT 1500 // ms, SD=1000, MMC=500
  1050. #define MAX_CHECK_INSERT_RETRY 3
  1051. #define SD_IDENTIFICATION_TIMEOUT (250*TIMER_1MS) // ms
  1052. #define SD_PROGRAMMING_TIMEOUT (1500*TIMER_1MS) // ms
  1053. #define SDIO_FUNCTION_TIMEOUT 1000
  1054. #define SD_MMC_INIT_RETRY 3
  1055. #define SD_MMC_IDENTIFY_CLK 300 //K HZ
  1056. #define SD_MMC_TRANSFER_SLOWER_CLK 12 //M HZ
  1057. #define SD_MMC_TRANSFER_CLK 18 //M HZ
  1058. #define SD_MMC_TRANSFER_HIGHSPEED_CLK 25 //M HZ
  1059. //Following functions are the API used for outside routine
  1060. //SD Initialization...
  1061. int sd_mmc_init(SD_MMC_Card_Info_t *sd_mmc_info);
  1062. void sd_mmc_exit(SD_MMC_Card_Info_t *sd_mmc_info);
  1063. void sd_mmc_prepare_init(SD_MMC_Card_Info_t *sd_mmc_info);
  1064. //get sd_mmc card information
  1065. //void sd_mmc_get_info(blkdev_stat_t *info);
  1066. //Check if any card is connected to adapter
  1067. SD_Card_Type_t sd_mmc_check_present(SD_MMC_Card_Info_t *sd_mmc_info);
  1068. //Check if any card is inserted according to pull up resistor
  1069. int sd_mmc_check_insert(SD_MMC_Card_Info_t *sd_mmc_info);
  1070. //Read data from SD/MMC card
  1071. int sd_mmc_read_data(SD_MMC_Card_Info_t *sd_mmc_info, unsigned long lba, unsigned long byte_cnt,
  1072. unsigned char *data_buf);
  1073. //Write data to SD/MMC card
  1074. int sd_mmc_write_data(SD_MMC_Card_Info_t *sd_mmc_info, unsigned long lba, unsigned long byte_cnt,
  1075. unsigned char *data_buf);
  1076. int sdio_read_data_block_hw(SD_MMC_Card_Info_t *sd_mmc_info, int function_no, int buf_or_fifo,
  1077. unsigned long sdio_addr,
  1078. unsigned long block_count,
  1079. unsigned char *data_buf);
  1080. int sdio_read_data_byte_hw(SD_MMC_Card_Info_t *sd_mmc_info, int function_no, int buf_or_fifo,
  1081. unsigned long sdio_addr, unsigned long byte_count,
  1082. unsigned char *data_buf);
  1083. int sdio_write_data_block_hw(SD_MMC_Card_Info_t *sd_mmc_info, int function_no, int buf_or_fifo,
  1084. unsigned long sdio_addr,
  1085. unsigned long block_count,
  1086. unsigned char *data_buf);
  1087. int sdio_write_data_byte_hw(SD_MMC_Card_Info_t *sd_mmc_info, int function_no, int buf_or_fifo,
  1088. unsigned long sdio_addr, unsigned long byte_count,
  1089. unsigned char *data_buf);
  1090. int sdio_read_reg(SD_MMC_Card_Info_t *sd_mmc_info, int function_no, unsigned long sdio_register,
  1091. unsigned char *reg_data);
  1092. int sdio_write_reg(SD_MMC_Card_Info_t *sd_mmc_info, int function_no, unsigned int sdio_register,
  1093. unsigned char *reg_data, unsigned read_after_write_flag);
  1094. int sdio_read_data(SD_MMC_Card_Info_t *sd_mmc_info, int function_no, int buf_or_fifo, unsigned long sdio_addr,
  1095. unsigned long byte_count, unsigned char *data_buf);
  1096. int sdio_write_data(SD_MMC_Card_Info_t *sd_mmc_info, int function_no, int buf_or_fifo, unsigned long sdio_addr,
  1097. unsigned long byte_count, unsigned char *data_buf);
  1098. int sdio_close_target_interrupt(SD_MMC_Card_Info_t *sd_mmc_info, int function_no);
  1099. int sdio_open_target_interrupt(SD_MMC_Card_Info_t *sd_mmc_info, int function_no);
  1100. //SD Power on/off
  1101. void sd_mmc_power_on(SD_MMC_Card_Info_t *sd_mmc_info);
  1102. void sd_mmc_power_off(SD_MMC_Card_Info_t *sd_mmc_info);
  1103. void sd_mmc_prepare_init(SD_MMC_Card_Info_t *sd_mmc_info);
  1104. #endif //_H_SD_PROTOCOL