sd_mem.h
上传用户:hrbsz2007
上传日期:2008-05-11
资源大小:1222k
文件大小:16k
开发平台:

C/C++

  1. /* Copyright 2002, ESS Technology, Inc. */
  2. /* SCCSID @(#)sd_mem.h 1.8 11/01/03 */
  3. #ifndef _SD_MEM_H_
  4. #define _SD_MEM_H_
  5. /******************************************************************/
  6. /*                                                                */
  7. /*      SD IO defined at "mem_dev.h"                              */
  8. /*                                                                */
  9. /******************************************************************/
  10. /**************************************************************************
  11.  * SD related constants and macros
  12.  **************************************************************************/
  13. #define SD_ACCEPTABLE_OCR (0x007c<<16) /* 3.0 < Vdd < 3.5 */
  14. #define SD_MAX_RETRIES 2
  15. /* SD communication states */
  16. #define SD_IDLE 0x0
  17. #define HOST2SD_8CYCLES 0x1
  18. #define HOST2SD_START 0x2
  19. #define HOST2SD_CMD 0x3
  20. #define HOST2SD_CRC7 0x4
  21. #define HOST2SD_WAIT 0x5
  22. #define HOST2SD_DATA1 0x6
  23. #define HOST2SD_DATA4 0x7
  24. #define HOST2SD_CRC16_DATA 0x8
  25. #define HOST2SD_CHECK_BUSY 0x9
  26. #define SD2HOST_NODATA 0xa
  27. #define SD2HOST_DATA1 0xb
  28. #define SD2HOST_DATA4 0xc
  29. #define SD2HOST_CRC7_NODATA 0xd
  30. #define SD2HOST_CRC16_DATA 0xe
  31. /* SD Card internal states */
  32. #define SD_STATE_IDLE 0x0
  33. #define SD_STATE_READY 0x1
  34. #define SD_STATE_IDENT 0x2
  35. #define SD_STATE_STBY 0x3
  36. #define SD_STATE_TRAN 0x4
  37. #define SD_STATE_DATA 0x5
  38. #define SD_STATE_RCV 0x6
  39. #define SD_STATE_PRG 0x7
  40. #define SD_STATE_DIS 0x8
  41. /* SD command responses */
  42. #define SD_NR 0x0 /* No response */
  43. #define SD_R1 0x1
  44. #define SD_R2 0x2
  45. #define SD_R3 0x3
  46. #define SD_R1b 0x4
  47. #define SD_R6 0x6
  48. /* SD responses for commands that drive data  */
  49. #define SD_USE_DAT_RD 0x20 /* data line(s) for read */
  50. #define SD_NR_RD (SD_NR|SD_USE_DAT_RD)
  51. #define SD_R1_RD (SD_R1|SD_USE_DAT_RD)
  52. #define SD_R1b_RD (SD_R1b|SD_USE_DAT_RD)
  53. #define SD_R2_RD (SD_R2|SD_USE_DAT_RD)
  54. #define SD_R3_RD (SD_R3|SD_USE_DAT_RD)
  55. #define SD_R6_RD (SD_R6|SD_USE_DAT_RD)
  56. #define SD_USE_DAT_WR 0x10 /* data line(s) for write */
  57. #define SD_NR_WR (SD_NR|SD_USE_DAT_WR)
  58. #define SD_R1_WR (SD_R1|SD_USE_DAT_WR)
  59. #define SD_R1b_WR (SD_R1b|SD_USE_DAT_WR)
  60. #define SD_R2_WR (SD_R2|SD_USE_DAT_WR)
  61. #define SD_R3_WR (SD_R3|SD_USE_DAT_WR)
  62. #define SD_R6_WR (SD_R6|SD_USE_DAT_WR)
  63. /* SD command classes */
  64. #define SD_CL0 0 /* basic */
  65. #define SD_CL1 1 /* reserved */
  66. #define SD_CL2 2 /* block read */
  67. #define SD_CL3 3 /* reserved */
  68. #define SD_CL4 4 /* block write */
  69. #define SD_CL5 5 /* erase */
  70. #define SD_CL6 6 /* write protection */
  71. #define SD_CL7 7 /* lock card */
  72. #define SD_CL8 8 /* application specific */
  73. #define SD_CL9 9 /* reserved */
  74. #define SD_CL10 10 /* reserved */
  75. #define SD_CL11 11 /* reserved */
  76. #define SD_CLX 12 /* No class defined */
  77. /* Command format: 
  78.  * [start[47]|xmit[46]|index[45:40]|arg[39:8]|crc[7:1]|end[0]]
  79.  */
  80. #define SD_CMD_LENGTH 48 /* bits */
  81. #define SD_CMD_INDEX_LENGTH 6 /* bits */
  82. #define SD_CMD_ARG_LENGTH 32 /* bits */
  83. #define SD_CMD_CRC_LENGTH 7 /* bits */
  84. #define SD_CMD_START_BIT 0x0
  85. #define SD_CMD_END_BIT 0x1
  86. #define SD_CMD_HOST_XMIT_BIT 0x1
  87. /* CMD INDEX */
  88. #define SD_CMD_GO_IDLE_STATE 0 /* class 0 */
  89. #define SD_CMD_MMC_SEND_OP_COND 1 /* class 0 */
  90. #define SD_CMD_ALL_SEND_CID 2 /* class 0-R2 */
  91. #define SD_CMD_SEND_REL_ADDR 3 /* class 0-R6 */
  92. #define SD_CMD_SET_DSR 4 /* class 0 */
  93. #define SD_CMD_RESERVED5 5 /* class 0 */
  94. #define SD_CMD_RESERVED6 6 /* class 0 */
  95. #define SD_CMD_SELECT_CARD 7 /* class 0-R1b */
  96. #define SD_CMD_RESERVED8 8 /* class 0 */
  97. #define SD_CMD_SEND_CSD 9 /* class 0-R2 */
  98. #define SD_CMD_SEND_CID 10 /* class 0-R2 */
  99. #define SD_CMD_RESERVED11 11 /* class 0 */
  100. #define MMC_CMD_READ_UNTIL_STOP         11      /* class 1-R1 */
  101. #define SD_CMD_STOP_TRANSMIT 12 /* class 0-R1b */
  102. #define SD_CMD_SEND_STATUS 13 /* class 0-R1 */
  103. #define SD_CMD_RESERVED14 14 /* class 0 */
  104. #define SD_CMD_GO_INACTIVE_STATE 15 /* class 0 */
  105. #define SD_CMD_SET_BLOCKLEN 16 /* class 2-R1 */
  106. #define SD_CMD_READ_SINGLE_BLOCK 17 /* class 2-R1 */
  107. #define SD_CMD_READ_MULTI_BLOCK 18 /* class 2-R1 */
  108. #define SD_CMD_RESERVED19 19 /* class 2 */
  109. #define SD_CMD_RESERVED20 20 /* class 2 */
  110. #define SD_CMD_RESERVED21 21 /* class 2 */
  111. #define SD_CMD_RESERVED22 22 /* class 2 */
  112. #define SD_CMD_RESERVED23 23 /* class 2 */
  113. #define MMC_CMD_SET_BLOCK_COUNT         23      /* class 2- R1 */
  114. #define SD_CMD_WRITE_BLOCK 24 /* class 4-R1 */
  115. #define SD_CMD_WRITE_MULTI_BLOCK 25 /* class 4-R1 */
  116. #define SD_CMD_RESERVED26 26 /* class 4 */
  117. #define SD_CMD_PROGRAM_CSD 27 /* class 4-R1 */
  118. #define SD_CMD_SET_WRITE_PROT 28 /* class 6-R1b */
  119. #define SD_CMD_CLR_WRITE_PROT 29 /* class 6-R1b */
  120. #define SD_CMD_SEND_WRITE_PROT 30 /* class 6-R1 */
  121. #define SD_CMD_RESERVED31 31 /* class 6 */
  122. #define SD_CMD_ERASE_WR_BLK_START 32 /* class 5-R1 */
  123. #define SD_CMD_ERASE_WR_BLK_END 33 /* class 5-R1 */
  124. #define SD_CMD_RESERVED34 34 /* class 5 */
  125. #define SD_CMD_RESERVED35 35 /* class 5 */
  126. #define SD_CMD_RESERVED36 36 /* class 5 */
  127. #define SD_CMD_RESERVED37 37 /* class 5 */
  128. #define SD_CMD_ERASE 38 /* class 5-R1b */
  129. #define SD_CMD_RESERVED39 39 /* class 5 */
  130. #define SD_CMD_INVALID 40 /* class 5 */
  131. #define SD_CMD_RESERVED41 41 /* class 5 */
  132. #define SD_CMD_LOCK_UNLOCK 42 /* class 7-R1 */
  133. #define SD_CMD_RESERVED43 43 /* class 7 */
  134. #define SD_CMD_RESERVED44 44 /* class 7 */
  135. #define SD_CMD_RESERVED45 45 /* class 7 */
  136. #define SD_CMD_RESERVED46 46 /* class 7 */
  137. #define SD_CMD_RESERVED47 47 /* class 7 */
  138. #define SD_CMD_RESERVED48 48 /* class 7 */
  139. #define SD_CMD_RESERVED49 49 /* class 7 */
  140. #define SD_CMD_RESERVED50 50 /* class 7 */
  141. #define SD_CMD_RESERVED51 51 /* class 7 */
  142. #define SD_CMD_RESERVED52 52 /* class 7 */
  143. #define SD_CMD_RESERVED53 53 /* class 7 */
  144. #define SD_CMD_RESERVED54 54 /* class 7 */
  145. #define SD_CMD_APP_CMD 55 /* class 8-R1 */
  146. #define SD_CMD_GEN_CMD 56 /* class 8-R1 */
  147. #define SD_CMD_RESERVED57 57 /* class 8 */
  148. #define SD_CMD_RESERVED58 58 /* class 8 */
  149. #define SD_CMD_RESERVED59 59 /* class 8 */
  150. #define SD_CMD_RESERVED60 60 /* class 8 */
  151. #define SD_CMD_RESERVED61 61 /* class 8 */
  152. #define SD_CMD_RESERVED62 62 /* class 8 */
  153. #define SD_CMD_RESERVED63 63 /* class 8 */
  154. #define SD_CMD_EMPTY SD_CMD_RESERVED63
  155. /* ACMD INDEX (application specific..
  156.  * must be preceded by SD_CMD_APP_CMD) 
  157.  */
  158. #define ACMD_MARKER 0x80
  159. #define SD_ACMD_SET_BUS_WIDTH (6|ACMD_MARKER) /* R1 */
  160. #define SD_ACMD_SD_STATUS (13|ACMD_MARKER) /* R1 */
  161. #define SD_ACMD_RESERVED17 (17|ACMD_MARKER)
  162. #define SD_ACMD_RESERVED18 (18|ACMD_MARKER)
  163. #define SD_ACMD_RESERVED19 (19|ACMD_MARKER)
  164. #define SD_ACMD_RESERVED20 (20|ACMD_MARKER)
  165. #define SD_ACMD_RESERVED21 (21|ACMD_MARKER)
  166. #define SD_ACMD_SEND_NUM_WR_BLOCKS (22|ACMD_MARKER) /* R1 */
  167. #define SD_ACMD_SET_WR_BLK_ERASE_CNT (23|ACMD_MARKER) /* R1 */
  168. #define SD_ACMD_RESERVED24 (24|ACMD_MARKER)
  169. #define SD_ACMD_RESERVED25 (25|ACMD_MARKER)
  170. #define SD_ACMD_RESERVED26 (26|ACMD_MARKER)
  171. #define SD_ACMD_RESERVED38 (38|ACMD_MARKER)
  172. #define SD_ACMD_RESERVED39 (39|ACMD_MARKER)
  173. #define SD_ACMD_RESERVED40 (40|ACMD_MARKER)
  174. #define SD_ACMD_SD_SEND_OP_COND (41|ACMD_MARKER) /* R3 */
  175. #define SD_ACMD_SET_CLR_CARD_DETECT (42|ACMD_MARKER) /* R1 */
  176. #define SD_ACMD_RESERVED43 (43|ACMD_MARKER)
  177. #define SD_ACMD_RESERVED49 (49|ACMD_MARKER)
  178. #define SD_ACMD_SEND_SCR (51|ACMD_MARKER) /* R1 */
  179. /* Command/Response delay info */
  180. #define SD_NCR_MIN 2 /* clock cycles*/
  181. #define SD_NCR_MAX 64 /* clock cycles*/
  182. #define SD_NID_MIN 5 /* clock cycles*/
  183. #define SD_NID_MAX 15 /* clock cycles*/
  184. #define SD_NAC_MIN 2 /* clock cycles*/
  185. #define SD_NRC_MIN 8 /* clock cycles*/
  186. #define SD_NCC_MIN 8 /* clock cycles*/
  187. #define SD_NWR_MIN 2 /* clock cycles*/
  188. /* SD_card_info.type..*/
  189. #define SD_IO 3 /* not yet */
  190. #define SD_MEM 2
  191. #define SD_MMC 1
  192. #define SD_UNKNOWN 0
  193. /* Response, R1 format (normal)..
  194.  *     [start[47]|xmit[46]|index[45:40]|stat[39:8]|crc[7:1]|end[0]]
  195.  */
  196. #define SD_R1_LENGTH 48 /* bits */
  197. #define SD_R1_INDEX_LENGTH 6 /* bits */
  198. #define SD_R1_STATUS_LENGTH 32 /* bits */
  199. #define SD_R1_CRC_LENGTH 7 /* bits */
  200. #define SD_R1_START_BIT 0x0
  201. #define SD_R1_END_BIT 0x1
  202. #define SD_R1_CARD_XMIT_BIT 0x0
  203. /* Response, R2 format (CID, CSD register)..
  204.  *     [start[135]|xmit[134]|res[133:128]|reg[127:1]|end[0]]
  205.  */
  206. #define SD_R2_LENGTH 136 /* bits */
  207. #define SD_R2_RESERVED_LENGTH 6 /* bits */
  208. #define SD_R2_REG_LENGTH 127 /* bits */
  209. #define SD_R2_START_BIT 0x0
  210. #define SD_R2_END_BIT 0x1
  211. #define SD_R2_CARD_XMIT_BIT 0x0
  212. /* Response, R3 format (OCR register)..
  213.  *     [start[47]|xmit[46]|res1[45:40]|reg[39:8]|res2[7:1]|end[0]]
  214.  */
  215. #define SD_R3_LENGTH 48 /* bits */
  216. #define SD_R3_RESERVED_LENGTH1 6 /* bits */
  217. #define SD_R3_REG_LENGTH 32 /* bits */
  218. #define SD_R3_RESERVED_LENGTH2 7 /* bits */
  219. #define SD_R3_START_BIT 0x0
  220. #define SD_R3_END_BIT 0x1
  221. #define SD_R3_CARD_XMIT_BIT 0x0
  222. /* Response, R6 format (RCA register)..
  223.  * [start[47]|xmit[46]|index[45:40]|RCA[39:24]|stat[23:8]|crc[7:1]|end[0]]
  224.  */
  225. #define SD_R6_LENGTH 48 /* bits */
  226. #define SD_R6_INDEX_LENGTH 6 /* bits */
  227. #define SD_R6_RCA_LENGTH 16 /* bits */
  228. #define SD_R6_STATUS_LENGTH 16 /* bits */
  229. #define SD_R6_CRC_LENGTH 7 /* bits */
  230. #define SD_R6_START_BIT 0x0
  231. #define SD_R6_END_BIT 0x1
  232. #define SD_R6_CARD_XMIT_BIT 0x0
  233. /**************************************************************************
  234.  * SD structures 
  235.  **************************************************************************/
  236. typedef struct { /* ref. p.49-52..SD Physical Layer */
  237.     unsigned out_of_range :1;
  238.     unsigned addr_error :1;
  239.     unsigned block_len_error :1;
  240.     unsigned erase_seq_error :1;
  241.     unsigned erase_param :1;
  242.     unsigned wp_violation :1;
  243.     unsigned card_locked :1;
  244.     unsigned lock_failed :1;
  245.     unsigned com_crc_error :1;
  246.     unsigned illegal_cmd :1;
  247.     unsigned ecc_failed :1;
  248.     unsigned cc_error :1;
  249.     unsigned error :1;
  250.     unsigned reserved_18_17 :2;
  251.     unsigned cid_csd_overwr :1;
  252.     unsigned wp_erase_skip :1;
  253.     unsigned ecc_disable :1;
  254.     unsigned erase_reset :1;
  255.     unsigned current_state :4;
  256.     unsigned ready_for_data :1;
  257.     unsigned reserved_7_6 :2;
  258.     unsigned app_cmd :1;
  259.     unsigned reserved_4 :1;
  260.     unsigned ake_seq_error :1;
  261.     unsigned reserved_2_0 :3;
  262. } SD_CARD_STATUS;
  263. typedef struct { /* ref. p.52-53..SD Physical Layer */
  264.     unsigned dat_bus_width :2;    
  265.     unsigned secured_mode :1;
  266.     unsigned reserved_508_496 :13;
  267.     unsigned sd_card_type :16;
  268.     unsigned protected_size :32;
  269. #if 0 /* only 64 of 512 bits used */
  270.     unsigned reserved_447_312 :136; /* illustrative..not for compile! */
  271.     unsigned reserved_311_0 :312; /* illustrative..not for compile! */
  272. #endif
  273. } SD_STATUS;
  274. typedef struct { /* ref. p.61..SD Physical Layer */
  275.     unsigned card_ready :1;
  276.     unsigned reserved_30_24 :7;
  277.     unsigned vdd35_36 :1;
  278.     unsigned vdd34_35 :1;
  279.     unsigned vdd33_34 :1;
  280.     unsigned vdd32_33 :1;
  281.     unsigned vdd31_32 :1;
  282.     unsigned vdd30_31 :1;
  283.     unsigned vdd29_30 :1;
  284.     unsigned vdd28_29 :1;
  285.     unsigned vdd27_28 :1;
  286.     unsigned vdd26_27 :1;
  287.     unsigned vdd25_26 :1;
  288.     unsigned vdd24_25 :1;
  289.     unsigned vdd23_24 :1;
  290.     unsigned vdd22_23 :1;
  291.     unsigned vdd21_22 :1;
  292.     unsigned vdd20_21 :1;
  293.     unsigned vdd19_20 :1;
  294.     unsigned vdd18_19 :1;
  295.     unsigned vdd17_18 :1;
  296.     unsigned vdd16_17 :1;
  297.     unsigned reserved_3_0 :4;
  298. } SD_OCR;
  299. typedef struct { /* ref. p.62-63..SD Physical Layer */
  300.     unsigned mid :8;
  301.     unsigned oid_15_8 :8; /* oid[1] ascii..2B */
  302.     unsigned oid_7_0 :8; /* oid[0] ascii */
  303.     unsigned pnm_39_32 :8; /* pnm[4] ascii..5B */
  304.     unsigned pnm_31_24 :8; /* pnm[3] ascii */ 
  305.     unsigned pnm_23_16 :8; /* pnm[2] ascii */  
  306.     unsigned pnm_15_8 :8; /* pnm[1] ascii */  
  307.     unsigned pnm_7_0 :8; /* pnm[0] ascii */  
  308.     unsigned prv :8; /* BCD */
  309.     unsigned psn_31_8 :24; /* psn crossed DW boundary */
  310.     unsigned psn_7_0 :8; /* psn crossed DW boundary */
  311.     unsigned reserved_23_20 :4;
  312.     unsigned mdt :12;/* [y[11:4] | m[3:0]]..y: 0->2000; m: 1->January */
  313.     unsigned crc :7;
  314.     unsigned reserved_0 :1;    
  315. } SD_CID;
  316. typedef struct { /* ref. p.63-71..SD Physical Layer */
  317.     unsigned version :2; 
  318.     unsigned reserved_125_119 :7; 
  319.     unsigned taac_value :4; /* ref. p.65 */ 
  320.     unsigned taac_unit :3; /* ref. p.65 */ 
  321.     unsigned nsac :8; /* N*100 clock cycles */
  322.     unsigned reserved_103 :1; 
  323.     unsigned tran_spd_value :4; /* ref. p.65 */ 
  324.     unsigned tran_spd_unit :3; /* ref. p.65 */ 
  325.     unsigned ccc :12; /* supported command classes */
  326.     unsigned rd_blk_len :4;
  327.     unsigned rd_blk_len_partial :1;
  328.     unsigned wr_blk_misalign :1;
  329.     unsigned rd_blk_misalign :1;
  330.     unsigned dsr_imp :1;
  331.     unsigned reserved_75_74 :2;
  332.     unsigned c_size_11_2 :10; /* c_size crossed DW boundary */
  333.     unsigned c_size_1_0 :2; /* c_size crossed DW boundary */
  334.     unsigned vdd_r_curr_min :3;
  335.     unsigned vdd_r_curr_max :3;
  336.     unsigned vdd_w_curr_min :3;
  337.     unsigned vdd_w_curr_max :3;
  338.     unsigned c_size_mult :3;
  339.     unsigned erase_blk_en :1;
  340.     unsigned sector_size :7;
  341.     unsigned wp_grp_size :7;
  342.     unsigned wp_grp_enable :1;
  343.     unsigned reserved_30_29 :2;
  344.     unsigned r2w_factor :3;
  345.     unsigned wr_blk_len :4;
  346.     unsigned wr_blk_partial :1;
  347.     unsigned reserved_20_16 :5;
  348.     unsigned file_format_grp :1;
  349.     unsigned copy :1;
  350.     unsigned perm_wr_protect :1;
  351.     unsigned tmp_wr_protect :1;
  352.     unsigned file_format :2;
  353.     unsigned reserved_9_8 :2;
  354.     unsigned crc :7;
  355.     unsigned reserved_0 :1;
  356. } SD_CSD;
  357. typedef struct { /* ref. p.72-73..SD Physical Layer */
  358.     unsigned scr_struct :4;
  359.     unsigned sd_spec :4;
  360.     unsigned dat_stat_aft_erase :1;
  361.     unsigned sd_security :3;
  362.     unsigned sd_bus_widths :4;
  363.     unsigned reserved_47_32 :16;
  364. #if 0 /* use 32 of 64 bits */
  365.     unsigned reserved_31_0 :32; 
  366. #endif
  367. } SD_SCR;
  368. typedef struct { 
  369.     unsigned com_state :4; /* value <= 0xf */
  370.     unsigned response_recv :1;
  371.     unsigned response_expected :3;
  372.     unsigned cmd :8;
  373.     unsigned cur_blk_size :4; /* size = 2^value (bytes) */ 
  374.     unsigned data_width :1; /* 0:1bit, 1:4bits*/
  375.     unsigned use_data :2; /* 1:write, 2:read, 0:no data */
  376.     unsigned cmd_sent  :1; /* 1:cmd completed */ 
  377.     unsigned retry_cnt :8;
  378.     uint wait_nclk;
  379.     uint arg;
  380.     uint data_cnt;
  381.     uint data_len;             /* length to be xmited in bytes */
  382.     uint *rd_dest_ptr;
  383.     int  *wr_src_ptr;
  384.     int result;               /* 0: success, -1: failed */
  385. #if SD_CHECK_RECV_CRC
  386.     uint crc7_err_cnt;
  387.     uint crc16_err_cnt;
  388. #endif
  389. } SD_XMIT_STATUS;
  390. typedef struct { 
  391.     uchar cmd;
  392.     uint  arg;
  393. } SD_CMD_Q;
  394. typedef struct { 
  395.     uchar type;
  396.     uchar inserted;
  397.     uint n_wr_max; /* min: 2 clk cycles */
  398.     uint n_ac_max; /* min: 2 clk cycles */
  399.     uint capacity; /* in Bytes */
  400. } SD_CINFO;
  401. /**************************************************************************
  402.  * Public SD variables
  403.  **************************************************************************/
  404. EXPORT SD_CINFO SD_card_info;
  405. EXPORT SD_XMIT_STATUS SD_xmit_info;
  406. /**************************************************************************
  407.  * Public SD functions
  408.  **************************************************************************/
  409. EXPORT void SD_service(void);
  410. EXPORT int SD_identify_device(void);
  411. EXPORT void SD_init(void);
  412. EXPORT void SD_enable_io(int);
  413. EXPORT void SD_reset(void);
  414. EXPORT int SD_set_bus_width(int);
  415. EXPORT int SD_read_blk(int, int, uint *, int);
  416. EXPORT int SD_read_nblk(int, int, uint *, int);
  417. EXPORT int SD_send_cmd(int, uint, int);
  418. EXPORT int SD_send_acmd(int, uint, int);
  419. EXPORT int SD_clk_restricted_cmd(int, uint);
  420. EXPORT int SD_get_next_cmd(void);
  421. EXPORT unsigned int SD_boot_setup(void);
  422. EXPORT int SD_read_sector_lba(short *, int, int, int);
  423. #endif /*  _SD_MEM_H_ */