sdd_sd.h 45 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927
  1. /*****************************************************************************
  2. *
  3. * Copyright Andes Technology Corporation 2007-2008
  4. * All Rights Reserved.
  5. *
  6. * Revision History:
  7. *
  8. * Sep.26.2007 Created.
  9. ****************************************************************************/
  10. /*****************************************************************************
  11. *
  12. * FILE NAME VERSION
  13. *
  14. * sdd_sd.h
  15. *
  16. * DESCRIPTION
  17. *
  18. * Secure digital card specification 2.0 definition.
  19. *
  20. * Currently only Secure Digital Memory standards are well-defined.
  21. * Remaining spec mostly are left for future developers.
  22. *
  23. * DATA STRUCTURES
  24. *
  25. * None
  26. *
  27. * DEPENDENCIES
  28. *
  29. * None
  30. *
  31. ****************************************************************************/
  32. #ifndef __SDD_SD_H__
  33. #define __SDD_SD_H__
  34. #include <hal.h>
  35. /*****************************************************************************
  36. * Secure Digital Memory Card Register Definitions
  37. *
  38. * Summary:
  39. * read program
  40. * ---- -------- ---------------------------------------------- ---------------------
  41. * OCR 32 bits CMD8_SEND_IF_COND/R7 ACMD41_SD_SEND_OP_COND/R3
  42. * CID 128 bits CMD2_ALL_SEND_CIS/R2 CMD10_SEND_CID/R2
  43. * CSD 128 bits CMD9_SEND_CSD/R2 CMD27_PROGRAM_CSD/R1
  44. * SCR 64 bits ACMD51_SEND_SCR/R1
  45. * RCA 16 bits CMD3_SEND_RELATIVE_ADDR/R6
  46. * DSR 16 bits CMD4_SET_DSR
  47. * CSR 32 bits CMD13_SEND_STATUS/R1
  48. * SSR 512 bits ACMD13_SD_STATUS/R1
  49. *
  50. * Responses:
  51. * R1 48 bits (cmd) (normal response reg)
  52. * [45:40] cmd_idx [39:08] CSR [7:1] CRC
  53. * R1b 48 bits (cmd) busy signal (dat)
  54. * [45:40] cmd_idx [39:08] CSR [7:1] CRC
  55. * R2 136 bits (cmd) (CID, CSD reg)
  56. * [127:1] CID/CSD (CRC included)
  57. * R3 48 bits (cmd) (OCR reg))
  58. * [39:8] OCR
  59. * R6 48 bits (cmd) (RCA reg)
  60. * [45:40] cmd_idx (0x03 CMD3) [39:24] New published RCA [23:8] CSR 23,22,19,12-0
  61. * R7 48 bits (cmd) (card interface condition reg)
  62. * [45:40] cmd_idx [19:16] voltage (0x01: 2.7~3.6V) [15:8] check-echo [7:1] CRC
  63. *
  64. ****************************************************************************/
  65. /* General Form 32-bit Response */
  66. typedef struct _SD_R32 {
  67. /*
  68. * Note: The bitfields definition was not applied due to that the
  69. * core architecture may be switched between big-endian
  70. * and little endian. We don't want to define two structures
  71. * to switch between the two endain architectures. Users
  72. * could use following macros to extract the target member.
  73. */
  74. union {
  75. uint32_t r[1];
  76. uint8_t b[4];
  77. };
  78. } SD_R32;
  79. /* General Form 128-bit Response */
  80. typedef struct _SD_R128 {
  81. /*
  82. * Note: The bitfields definition was not applied due to that the
  83. * core architecture may be switched between big-endian
  84. * and little endian. We don't want to define two structures
  85. * to switch between the two endain architectures. Users
  86. * could use following macros to extract the target member.
  87. */
  88. union {
  89. uint32_t r[4];
  90. uint8_t b[16];
  91. };
  92. } SD_R128;
  93. /* R6 Response Fields (Width: 32 bits) */
  94. #define SD_R6_GET_RCA(r32) ((uint32_t)((r32).r[0]) & 0xffff0000)
  95. #define SD_R6_GET_CSR(r32) ((uint32_t)((r32).r[0]) & 0x0000ffff)
  96. #define SD_R6_GET_CSR_ERR(r32) ((uint32_t)((r32).r[0]) & 0x0000e000)
  97. /* R7 Response Fields (Width: 32 bits) */
  98. #define SD_R7_GET_VHS(r32) (((uint32_t)((r32).r[0]) >> 8) & 0x0f)
  99. #define SD_R7_GET_PTN(r32) ((uint32_t)((r32).r[0]) & 0xff)
  100. /* OCR - Operation Condition Register (Width: 32 bits */
  101. /* Readback: ACMD41_SD_SEND_OP_COND/R3 */
  102. typedef SD_R32 SD_OCR;
  103. #define SD_OCR_VDD_MASK 0x00ffffff /* VDD voltage window */
  104. #define SD_OCR_VDD_SHIFT 0
  105. #define SD_OCR_VDD_2_7 0x00008000 /* VDD 2.7 ~ 2.8 */
  106. #define SD_OCR_VDD_2_8 0x00010000 /* VDD 2.8 ~ 2.9 */
  107. #define SD_OCR_VDD_2_9 0x00020000 /* VDD 2.9 ~ 3.0 */
  108. #define SD_OCR_VDD_3_0 0x00040000 /* VDD 3.0 ~ 3.1 */
  109. #define SD_OCR_VDD_3_1 0x00080000 /* VDD 3.1 ~ 3.2 */
  110. #define SD_OCR_VDD_3_2 0x00100000 /* VDD 3.2 ~ 3.3 */
  111. #define SD_OCR_VDD_3_3 0x00200000 /* VDD 3.3 ~ 3.4 */
  112. #define SD_OCR_VDD_3_4 0x00400000 /* VDD 3.4 ~ 3.5 */
  113. #define SD_OCR_VDD_3_5 0x00800000 /* VDD 3.5 ~ 3.6 */
  114. #define SD_OCR_VDD_2_7V_3_6V (SD_OCR_VDD_2_7 | SD_OCR_VDD_2_8 | SD_OCR_VDD_2_9 | SD_OCR_VDD_3_0 | \
  115. SD_OCR_VDD_3_1 | SD_OCR_VDD_3_2 | SD_OCR_VDD_3_3 | SD_OCR_VDD_3_4 | SD_OCR_VDD_3_5)
  116. #define SD_OCR_CCS_MASK 0x40000000 /* Card capacity status */
  117. #define SD_OCR_CCS_SHIFT 30
  118. #define SD_CCS_SD 0
  119. #define SD_CCS_SDHC 1
  120. #define SD_OCR_BUSY_MASK 0x80000000 /* Card power up status bit */
  121. #define SD_OCR_BUSY_SHIFT 31
  122. #define SD_BUSY 0
  123. #define SD_POWERUP 1
  124. #define SD_OCR_GET_VDD(ocr) (((uint32_t)((ocr).r[0]) & SD_OCR_VDD_MASK) >> SD_OCR_VDD_SHIFT)
  125. #define SD_OCR_GET_CCS(ocr) (((uint32_t)((ocr).r[0]) & SD_OCR_CCS_MASK) >> SD_OCR_CCS_SHIFT)
  126. #define SD_OCR_GET_BUSY(ocr) (((uint32_t)((ocr).r[0]) & SD_OCR_BUSY_MASK) >> SD_OCR_BUSY_SHIFT)
  127. /* CID - Card Identification Register (Width: 128 bits) */
  128. /* Readback: CMD10_SEND_CID/R2 */
  129. typedef SD_R128 SD_CID;
  130. /* Manufacture ID */
  131. #define SD_CID_GET_MID(cid) ((uint32_t)((cid).r[3]) >> 24)
  132. #define SD_CID_GET_MID_FROM_R3(r) ((uint32_t)(r) >> 24)
  133. /* OEM/Application ID */
  134. #define SD_CID_GET_OID_PTR(cid) ((uint8_t*)&((cid).b[13]))
  135. /* Product name */
  136. #define SD_CID_GET_PNM_PTR(cid) ((uint8_t*)&((cid).b[8]))
  137. /* Product revision (BCD coding) */
  138. #define SD_CID_GET_PRV(cid) ((uint32_t)((cid).r[1]) >> 24)
  139. #define SD_CID_GET_PRV_FROM_R1(r) ((uint32_t)(r) >> 24)
  140. /* Product serial number */
  141. #define SD_CID_GET_PSN(cid) (((uint32_t)((cid).r[1]) << 8) | ((uint32_t)((cid).r[0]) >> 24))
  142. #define SD_CID_GET_PSN_FROM_R0R1(r0, r1) (((uint32_t)(r1) << 8) | ((uint32_t)(r0) >> 24))
  143. /* Manufacturing date (0x0yym, year = 2000 + yy, month = m) */
  144. #define SD_CID_GET_MDT(cid) (((uint32_t)((cid).r[0]) >> 8) & 0x0fff)
  145. #define SD_CID_GET_MDT_FROM_R0(r) (((uint32_t)(r) >> 8) & 0x0fff)
  146. /* CRC7 checksum */
  147. #define SD_CID_GET_CRC(cid) (((uint32_t)((cid).r[0]) >> 1) & 0x007f)
  148. #define SD_CID_GET_CRC_FROM_R0(r) (((uint32_t)(r) >> 1) & 0x007f)
  149. /*
  150. * CSD - Card-Specific Data Register (Width: 128 bits)
  151. * Readback: CMD9_SEND_CSD/R2
  152. * Program : CMD27_PROGRAM_CSD/R1
  153. */
  154. typedef SD_R128 SD_CSD;
  155. /* -------------------------- */
  156. /* CSD v1.0 definitions */
  157. /* CRC (R/W) */
  158. #define SD_CSD_GET_CRC(csd) (((uint32_t)((csd).r[0]) >> 1) & 0x0000007f)
  159. #define SD_CSD_GET_CRC_FROM_R0(r) (((uint32_t)(r) >> 1) & 0x0000007f)
  160. #define SD_CSD_SET_CRC(csd,v) ((uint32_t)((csd).r[0]) |= (((uint32_t)(v) & 0x7f) << 1))
  161. #define SD_CSD_SET_CRC_TO_R0(r,v) ((uint32_t)(r) |= (((uint32_t)(v) & 0x7f) << 1))
  162. /* File format (R/W1, fixed to 0x00 in 2.0) */
  163. #define SD_CSD_GET_FILE_FORMAT(csd) (((uint32_t)((csd).r[0]) >> 10) & 0x00000003)
  164. #define SD_CSD_GET_FILE_FORMAT_FROM_R0(r) (((uint32_t)(r) >> 10) & 0x00000003)
  165. #define SD_CSD_SET_FILE_FORMAT(csd,v) ((uint32_t)((csd).r[0]) |= (((uint32_t)(v) & 0x03) << 10))
  166. #define SD_CSD_SET_FILE_FORMAT_TO_R0(r,v) ((uint32_t)(r) |= (((uint32_t)(v) & 0x03) << 10))
  167. #define SD_FILE_FORMAT_HDD 0 /* Hard disk like file system with partition table */
  168. #define SD_FILE_FORMAT_FLOPPY 1 /* DOS FAT (floppy like) with boot sector only (no partition table) */
  169. #define SD_FILE_FORMAT_UNIVERSAL 2 /* Universal file format */
  170. #define SD_FILE_FORMAT_OTHERS 3 /* Others/Unknown */
  171. /* Temporary write protection (R/W) */
  172. #define SD_CSD_GET_TMP_WRITE_PROTECT(csd) (((uint32_t)((csd).r[0]) >> 12) & 0x00000001)
  173. #define SD_CSD_GET_TMP_WRITE_PROTECT_FROM_R0(r) (((uint32_t)(r) >> 12) & 0x00000001)
  174. #define SD_CSD_SET_TMP_WRITE_PROTECT(csd,v) ((uint32_t)((csd).r[0]) |= (((uint32_t)(v) & 0x01) << 12))
  175. #define SD_CSD_SET_TMP_WRITE_PROTECT_TO_R0(r,v) ((uint32_t)(r) |= (((uint32_t)(v) & 0x01) << 12))
  176. /* Permanent write protection (R/W1) */
  177. #define SD_CSD_GET_PERM_WRITE_PROTECT(csd) (((uint32_t)((csd).r[0]) >> 13) & 0x00000001)
  178. #define SD_CSD_GET_PERM_WRITE_PROTECT_FROM_R0(r) (((uint32_t)(r) >> 13) & 0x00000001)
  179. #define SD_CSD_SET_PERM_WRITE_PROTECT(csd,v) ((uint32_t)((csd).r[0]) |= (((uint32_t)(v) & 0x01) << 13))
  180. #define SD_CSD_SET_PERM_WRITE_PROTECT_TO_R0(r,v) ((uint32_t)(r) |= (((uint32_t)(v) & 0x01) << 13))
  181. /* Copy flag (R/W1) */
  182. #define SD_CSD_GET_COPY(csd) (((uint32_t)((csd).r[0]) >> 14) & 0x00000001)
  183. #define SD_CSD_GET_COPY_FROM_R0(r) (((uint32_t)(r) >> 14) & 0x00000001)
  184. #define SD_CSD_SET_COPY(csd,v) ((uint32_t)((csd).r[0]) |= (((uint32_t)(v) & 0x01) << 14))
  185. #define SD_CSD_SET_COPY_TO_R0(r,v) ((uint32_t)(r) |= (((uint32_t)(v) & 0x01) << 14))
  186. /* File format group (R/W1, fixed to 0x00 in 2.0) */
  187. #define SD_CSD_GET_FILE_FORMAT_GRP(csd) (((uint32_t)((csd).r[0]) >> 15) & 0x00000001)
  188. #define SD_CSD_GET_FILE_FORMAT_GRP_FROM_R0(r) (((uint32_t)(r) >> 15) & 0x00000001)
  189. #define SD_CSD_SET_FILE_FORMAT_GRP(csd,v) ((uint32_t)((csd).r[0]) |= (((uint32_t)(v) & 0x01) << 15))
  190. #define SD_CSD_SET_FILE_FORMAT_GRP_TO_R0(r,v) ((uint32_t)(r) |= (((uint32_t)(v) & 0x01) << 15))
  191. /* Partial blocks for write allowed (R, fixed to 0x00 in 2.0) */
  192. #define SD_CSD_GET_WRITE_BL_PARTIAL(csd) (((uint32_t)((csd).r[0]) >> 21) & 0x00000001)
  193. #define SD_CSD_GET_WRITE_BL_PARTIAL_FROM_R0(r) (((uint32_t)(r) >> 21) & 0x00000001)
  194. /* Max write data block length (R, fixed to 9 in 2.0) */
  195. #define SD_CSD_GET_WRITE_BL_LEN(csd) (((uint32_t)((csd).r[0]) >> 22) & 0x0000000f)
  196. #define SD_CSD_GET_WRITE_BL_LEN_FROM_R0(r) (((uint32_t)(r) >> 22) & 0x0000000f)
  197. /* Write speed factor (R, fixed to 0x02 in 2.0) */
  198. #define SD_CSD_GET_R2W_FACTOR(csd) (((uint32_t)((csd).r[0]) >> 26) & 0x00000007)
  199. #define SD_CSD_GET_R2W_FACTOR_FROM_R0(r) (((uint32_t)(r) >> 26) & 0x00000007)
  200. /* Write protect group enable (R, fixed to 0x00 in 2.0) */
  201. #define SD_CSD_GET_WP_GRP_ENABLE(csd) ((uint32_t)((csd).r[0]) >> 31)
  202. #define SD_CSD_GET_WP_GRP_ENABLE_FROM_R0(r) ((uint32_t)(r) >> 31)
  203. /* Write protect group size (R, fixed to 0x00 in 2.0) */
  204. #define SD_CSD_GET_WP_GRP_SIZE(csd) (((uint32_t)((csd).r[1]) >> 0) & 0x0000007f)
  205. #define SD_CSD_GET_WP_GRP_SIZE_FROM_R1(r) (((uint32_t)(r) >> 0) & 0x0000007f)
  206. /* Erase sector size (R, fixed to 0x7f in 2.0) */
  207. #define SD_CSD_GET_SECTOR_SIZE(csd) (((uint32_t)((csd).r[1]) >> 7) & 0x0000007f)
  208. #define SD_CSD_GET_SECTOR_SIZE_FROM_R1(r) (((uint32_t)(r) >> 7) & 0x0000007f)
  209. /* Erase single block enable (R, fixed to 0x01 in 2.0) */
  210. #define SD_CSD_GET_ERASE_BLK_EN(csd) (((uint32_t)((csd).r[1]) >> 14) & 0x00000001)
  211. #define SD_CSD_GET_ERASE_BLK_EN_FROM_R1(r) (((uint32_t)(r) >> 14) & 0x00000001)
  212. /* Device size multiplier (R, obsolete in 2.0) */
  213. #define SD_CSD1_GET_C_SIZE_MULT(csd) (((uint32_t)((csd).r[1]) >> 15) & 0x00000007)
  214. #define SD_CSD1_GET_C_SIZE_MULT_FROM_R1(r) (((uint32_t)(r) >> 15) & 0x00000007)
  215. /* Max write current at VDD max (R, obsolete in 2.0) */
  216. #define SD_CSD1_GET_VDD_W_CURR_MAX(csd) (((uint32_t)((csd).r[1]) >> 18) & 0x00000007)
  217. #define SD_CSD1_GET_VDD_W_CURR_MAX_FROM_R1(r) (((uint32_t)(r) >> 18) & 0x00000007)
  218. /* Max write current at VDD min (R, obsolete in 2.0) */
  219. #define SD_CSD1_GET_VDD_W_CURR_MIN(csd) (((uint32_t)((csd).r[1]) >> 21) & 0x00000007)
  220. #define SD_CSD1_GET_VDD_W_CURR_MIN_FROM_R1(r) (((uint32_t)(r) >> 21) & 0x00000007)
  221. /* Max read current at VDD max (R, obsolete in 2.0) */
  222. #define SD_CSD1_GET_VDD_R_CURR_MAX(csd) (((uint32_t)((csd).r[1]) >> 24) & 0x00000007)
  223. #define SD_CSD1_GET_VDD_R_CURR_MAX_FROM_R1(r) (((uint32_t)(r) >> 24) & 0x00000007)
  224. /* Max read current at VDD min (R, obsolete in 2.0) */
  225. #define SD_CSD1_GET_VDD_R_CURR_MIN(csd) (((uint32_t)((csd).r[1]) >> 27) & 0x00000007)
  226. #define SD_CSD1_GET_VDD_R_CURR_MIN_FROM_R1(r) (((uint32_t)(r) >> 27) & 0x00000007)
  227. #define SD_VDD_CURR_MIN_0P5MA 0
  228. #define SD_VDD_CURR_MIN_1MA 1
  229. #define SD_VDD_CURR_MIN_5MA 2
  230. #define SD_VDD_CURR_MIN_10MA 3
  231. #define SD_VDD_CURR_MIN_25MA 4
  232. #define SD_VDD_CURR_MIN_35MA 5
  233. #define SD_VDD_CURR_MIN_60MA 6
  234. #define SD_VDD_CURR_MIN_100MA 7
  235. #define SD_VDD_CURR_MAX_1MA 0
  236. #define SD_VDD_CURR_MAX_5MA 1
  237. #define SD_VDD_CURR_MAX_10MA 2
  238. #define SD_VDD_CURR_MAX_25MA 3
  239. #define SD_VDD_CURR_MAX_35MA 4
  240. #define SD_VDD_CURR_MAX_45MA 5
  241. #define SD_VDD_CURR_MAX_80MA 6
  242. #define SD_VDD_CURR_MAX_200MA 7
  243. /* Device size (R) */
  244. #define SD_CSD1_GET_C_SIZE(csd) ((((uint32_t)((csd).r[2]) << 2) & 0x00000fff) | (((uint32_t)((csd).r[1]) >> 30) & 0x00000003))
  245. #define SD_CSD1_GET_C_SIZE_FROM_R1R2(r1, r2) ((((uint32_t)(r2) << 2) & 0x00000fff) | (((uint32_t)(r1) >> 30) & 0x00000003))
  246. /* DSR implemented (R) */
  247. #define SD_CSD_GET_DSR_IMP(csd) (((uint32_t)((csd).r[2]) >> 12) & 0x00000001)
  248. #define SD_CSD_GET_DSR_IMP_FROM_R2(r) (((uint32_t)(r) >> 12) & 0x00000001)
  249. /* Read block misaligned (R, fixed to 0x00 in 2.0) */
  250. #define SD_CSD_GET_READ_BLK_MISALIGN(csd) (((uint32_t)((csd).r[2]) >> 13) & 0x00000001)
  251. #define SD_CSD_GET_READ_BLK_MISALIGN_FROM_R2(r) (((uint32_t)(r) >> 13) & 0x00000001)
  252. /* Write block misaligned (R, fixed to 0x00 in 2.0) */
  253. #define SD_CSD_GET_WRITE_BLK_MISALIGN(csd) (((uint32_t)((csd).r[2]) >> 14) & 0x00000001)
  254. #define SD_CSD_GET_WRITE_BLK_MISALIGN_FROM_R2(r) (((uint32_t)(r) >> 14) & 0x00000001)
  255. /* Partial blocks for read allowed (R, fixed to 0x00 in 2.0) */
  256. #define SD_CSD_GET_READ_BL_PARTIAL(csd) (((uint32_t)((csd).r[2]) >> 15) & 0x00000001)
  257. #define SD_CSD_GET_READ_BL_PARTIAL_FROM_R2(r) (((uint32_t)(r) >> 15) & 0x00000001)
  258. /* Max read data block length (R, fixed to 9 in 2.0) */
  259. #define SD_CSD_GET_READ_BL_LEN(csd) (((uint32_t)((csd).r[2]) >> 16) & 0x0000000f)
  260. #define SD_CSD_GET_READ_BL_LEN_FROM_R2(r) (((uint32_t)(r) >> 16) & 0x0000000f)
  261. #define SD_BL_LEN_512 9 /* 2^9 */
  262. #define SD_BL_LEN_1024 10 /* 2^10 */
  263. #define SD_BL_LEN_2048 11 /* 2^11 */
  264. /* Card command classes (R) */
  265. #define SD_CSD_GET_CCC(csd) (((uint32_t)((csd).r[2]) >> 20) & 0x00000fff)
  266. #define SD_CSD_GET_CCC_FROM_R2(r) (((uint32_t)(r) >> 20) & 0x00000fff)
  267. #define SD_CCC_CLASS0 0
  268. #define SD_CCC_CLASS1 1
  269. #define SD_CCC_CLASS2 2
  270. #define SD_CCC_CLASS3 3
  271. #define SD_CCC_CLASS4 4
  272. #define SD_CCC_CLASS5 5
  273. #define SD_CCC_CLASS6 6
  274. #define SD_CCC_CLASS7 7
  275. #define SD_CCC_CLASS8 8
  276. #define SD_CCC_CLASS9 9
  277. #define SD_CCC_CLASS10 10
  278. #define SD_CCC_CLASS11 11
  279. /* Max data transfer rate (R, fixed to 0x32 or 0x5a in 2.0) */
  280. #define SD_CSD_GET_TRAN_SPEED(csd) ((uint32_t)((csd).r[3]) & 0x000000ff)
  281. #define SD_CSD_GET_TRAN_SPEED_FROM_R3(r) ((uint32_t)(r) & 0x000000ff)
  282. #define SD_TRAN_SPEED_TU(ts) ((uint32_t)(ts) & 0x07)
  283. #define SD_TRAN_SPEED_TV(ts) (((uint32_t)(ts) >> 3) & 0x0f)
  284. #define SD_TS_TU_100KBPS 0
  285. #define SD_TS_TU_1MBPS 1
  286. #define SD_TS_TU_10MBPS 2
  287. #define SD_TS_TU_100MBPS 3
  288. #define SD_TS_TV_1P0 1
  289. #define SD_TS_TV_1P2 2
  290. #define SD_TS_TV_1P3 3
  291. #define SD_TS_TV_1P5 4
  292. #define SD_TS_TV_2P0 5
  293. #define SD_TS_TV_2P5 6
  294. #define SD_TS_TV_3P0 7
  295. #define SD_TS_TV_3P5 8
  296. #define SD_TS_TV_4P0 9
  297. #define SD_TS_TV_4P5 10
  298. #define SD_TS_TV_5P0 11
  299. #define SD_TS_TV_5P5 12
  300. #define SD_TS_TV_6P0 13
  301. #define SD_TS_TV_7P0 14
  302. #define SD_TS_TV_8P0 15
  303. /* Data read access-time-2 in CLK cycles (NSAC * 100) (R, fixed to 0x00 in 2.0) */
  304. #define SD_CSD_GET_NSAC(csd) (((uint32_t)((csd).r[3]) >> 8) & 0x000000ff)
  305. #define SD_CSD_GET_NSAC_FROM_R3(r) (((uint32_t)(r) >> 8) & 0x000000ff)
  306. /* Data read access-time-1 (R, fixed to 0x0e in 2.0) */
  307. #define SD_CSD_GET_TAAC(csd) (((uint32_t)((csd).r[3]) >> 16) & 0x0000ffff)
  308. #define SD_CSD_GET_TAAC_FROM_R3(r) (((uint32_t)(r) >> 16) & 0x0000ffff)
  309. #define SD_TAAC_TU(ac) ((uint32_t)(ac) & 0x07)
  310. #define SD_TAAC_TV(ac) (((uint32_t)(ac) >> 3) & 0x0f)
  311. #define SD_TAAC_TU_1NS 0
  312. #define SD_TAAC_TU_10NS 1
  313. #define SD_TAAC_TU_100NS 2
  314. #define SD_TAAC_TU_1US 3
  315. #define SD_TAAC_TU_10US 4
  316. #define SD_TAAC_TU_100US 5
  317. #define SD_TAAC_TU_1MS 6
  318. #define SD_TAAC_TU_10MS 7
  319. #define SD_TAAC_TV_1P0 1
  320. #define SD_TAAC_TV_1P2 2
  321. #define SD_TAAC_TV_1P3 3
  322. #define SD_TAAC_TV_1P5 4
  323. #define SD_TAAC_TV_2P0 5
  324. #define SD_TAAC_TV_2P5 6
  325. #define SD_TAAC_TV_3P0 7
  326. #define SD_TAAC_TV_3P5 8
  327. #define SD_TAAC_TV_4P0 9
  328. #define SD_TAAC_TV_4P5 10
  329. #define SD_TAAC_TV_5P0 11
  330. #define SD_TAAC_TV_5P5 12
  331. #define SD_TAAC_TV_6P0 13
  332. #define SD_TAAC_TV_7P0 14
  333. #define SD_TAAC_TV_8P0 15
  334. /* CSD structure (R) */
  335. #define SD_CSD_GET_CSD_STRUCTURE(csd) ((uint32_t)((csd).r[3]) >> 30)
  336. #define SD_CSD_GET_CSD_STRUCTURE_FROM_R3(r) ((uint32_t)(r) >> 30)
  337. /* Get user data size (card memory capacity, not include security protected area) */
  338. #if 0
  339. #define SD_CSD1_GET_CAPACITY(csd) (SD_CSD1_GET_C_SIZE(csd) + 1) * \
  340. (1 << (SD_CSD1_GET_C_SIZE_MULT(csd) + 2)) * \
  341. (1 << SD_CSD_GET_READ_BL_LEN(csd))
  342. #define SD_CSD1_GET_CAPACITY_R1R2(r1,r2) (SD_CSD1_GET_C_SIZE_FROM_R1R2(r1,r2) + 1) * \
  343. (1 << (SD_CSD1_GET_C_SIZE_MULT_FROM_R1(r1) + 2)) * \
  344. (1 << SD_CSD_GET_READ_BL_LEN_FROM_R2(r2))
  345. #endif
  346. /* -------------------------- */
  347. /* CSD v2.0 definitions */
  348. /* */
  349. /* Device size (R) */
  350. #define SD_CSD2_GET_C_SIZE(csd) ((((uint32_t)((csd).r[2]) << 16) & 0x003f0000) | ((uint32_t)((csd).r[1]) >> 16))
  351. #define SD_CSD2_GET_C_SIZE_FROM_R1R2(r1,r2) ((((uint32_t)(r2) << 16) & 0x003f0000) | ((uint32_t)(r1) >> 16))
  352. /* Get user data size (card memory capacity, not include security protected area) */
  353. // #define SD_CSD2_GET_CAPACITY_KB(csd) ((SD_CSD2_GET_C_SIZE(csd) + 1) * 512)
  354. // #define SD_CSD2_GET_CAPACITY_KB_R1R2(r1,r2) ((SD_CSD2_GET_C_SIZE_FROM_R1R2(csd) + 1) * 512)
  355. /*
  356. * SCR - SD Card Configuration Register
  357. * Width: 64 bits
  358. * Readback: ACMD51_SEND_SCR/R1
  359. */
  360. typedef struct _SD_SCR {
  361. /* Note: The bitfields definition was not applied due to that the */
  362. /* core architecture may be switched between big-endian */
  363. /* and little endian. We don't want to define two structures */
  364. /* to switch between the two endain architectures. Users */
  365. /* could use following macros to extract the target member. */
  366. union {
  367. uint32_t r[2];
  368. uint8_t b[8];
  369. };
  370. } SD_SCR;
  371. /* DAT bus widths supported */
  372. #define SD_SCR_GET_SD_BUS_WIDTHS(scr) (((uint32_t)((scr).r[1]) >> 16) & 0x0000000f)
  373. #define SD_SCR_GET_SD_BUS_WIDTHS_FROM_R1(r) (((uint32_t)(r) >> 16) & 0x0000000f)
  374. #define SD_BUS_WIDTH_1BIT 1 /* DAT0 */
  375. #define SD_BUS_WIDTH_4BIT 4 /* DAT0 ~ DAT3 */
  376. /* SD security support */
  377. #define SD_SCR_GET_SD_SECURITY(scr) (((uint32_t)((scr).r[1]) >> 20) & 0x00000007)
  378. #define SD_SCR_GET_SD_SECURITY_FROM_R1(r) (((uint32_t)(r) >> 20) & 0x00000007)
  379. #define SD_SECURITY_NONE 0 /* no security */
  380. #define SD_SECURITY_1_01 2 /* version 1.01 */
  381. #define SD_SECURITY_2_0 3 /* version 2.00 */
  382. /* Data status after erases */
  383. #define SD_SCR_GET_DATA_STAT_AFTER_ERASE(scr) (((uint32_t)((scr).r[1]) >> 23) & 0x00000001)
  384. #define SD_SCR_GET_DATA_STAT_AFTER_ERASE_FROM_R1(r) (((uint32_t)(r) >> 23) & 0x00000001)
  385. /* SD memory card spec version */
  386. #define SD_SCR_GET_SD_SPEC(scr) (((uint32_t)((scr).r[1]) >> 24) & 0x0000000f)
  387. #define SD_SCR_GET_SD_SPEC_FROM_R1(r) (((uint32_t)(r) >> 24) & 0x0000000f)
  388. #define SD_SPEC_1_0 0 /* version 1.0 ~ 1.01 */
  389. #define SD_SPEC_1_1 1 /* version 1.10 */
  390. #define SD_SPEC_2_0 2 /* version 2.00 */
  391. /* SCR structure */
  392. #define SD_SCR_GET_SCR_STRUCTURE(scr) (((uint32_t)((scr).r[1]) >> 28) & 0x0000000f)
  393. #define SD_SCR_GET_SCR_STRUCTURE_FROM_R1(r) (((uint32_t)(r) >> 28) & 0x0000000f)
  394. #define SD_SCR_VERSION_1_0 0
  395. /*
  396. * CSR - Card Status Register
  397. * Width: 32 bits
  398. * Readback: CMD13_SEND_STATUS/R1
  399. */
  400. typedef SD_R32 SD_CSR;
  401. /* Bit field defines of CSR */
  402. #define SD_CSR_AKE_SEQ_ERROR 0x00000008 /* error in the sequence of authentication */
  403. #define SD_CSR_APP_CMD 0x00000020 /* card expects ACMD or the command has been interpreted as ACMD */
  404. #define SD_CSR_READY_FOR_DATA 0x00000100 /* buffer empty signaling on the bus */
  405. #define SD_CSR_CURRENT_STATE_MASK 0x00001e00 /* the state of the card when receiving the command */
  406. #define SD_CSR_CURRENT_STATE_SHIFT 9
  407. #define SD_CSR_ERASE_RESET 0x00002000 /* a erase sequence was cleared before execution due to out of erase sequence commands */
  408. #define SD_CSR_CARD_ECC_DISABLE 0x00004000 /* command was executed without using internal ECC */
  409. #define SD_CSR_WP_ERASE_SKIP 0x00008000 /* attempt to partial erase of write protected blocks */
  410. #define SD_CSR_CSD_OVERWRITE 0x00010000 /* attempt to 1) CSD ro fields mismatch, 2) reverse copy or perm wp bits */
  411. #define SD_CSR_ERROR 0x00080000 /* general error */
  412. #define SD_CSR_CC_ERROR 0x00100000 /* internal card controller error */
  413. #define SD_CSR_CARD_ECC_FAILED 0x00200000 /* card internal ECC was applied but failed to correct the data */
  414. #define SD_CSR_ILLEGAL_COMMAND 0x00400000 /* command illegal for the card state */
  415. #define SD_CSR_COM_CRC_ERROR 0x00800000 /* command crc check failed */
  416. #define SD_CSR_LOCK_UNLOCK_FAILED 0x01000000 /* password error in lock/unlock command */
  417. #define SD_CSR_CARD_IS_LOCKED 0x02000000 /* card is locked by the host */
  418. #define SD_CSR_WP_VIOLATION 0x04000000 /* attempts to write a protected block */
  419. #define SD_CSR_ERASE_PARAM 0x08000000 /* invlaid selection of write blocks for erase */
  420. #define SD_CSR_ERASE_SEQ_ERROR 0x10000000 /* error in erase command sequence */
  421. #define SD_CSR_BLOCK_LEN_ERROR 0x20000000 /* transferred block length is not allowed or transferred bytes mismatch the block length */
  422. #define SD_CSR_ADDRESS_ERROR 0x40000000 /* misaligned address which did not match the block length */
  423. #define SD_CSR_OUT_OF_RANGE 0x80000000 /* command argument was out of allowed range */
  424. /* Error in the sequence of the authentication process */
  425. #define SD_CSR_GET_AKE_SEQ_ERROR(csr) (((uint32_t)((csr).r[0]) >> 3) & 0x00000001)
  426. #define SD_CSR_GET_AKE_SEQ_ERROR_FROM_R(r) (((uint32_t)(r) >> 3) & 0x00000001)
  427. /* Card is now expect ACMD, or the command has been interpreted as ACMD */
  428. #define SD_CSR_GET_APP_CMD(csr) (((uint32_t)((csr).r[0]) >> 5) & 0x00000001)
  429. #define SD_CSR_GET_APP_CMD_FROM_R(r) (((uint32_t)(r) >> 5) & 0x00000001)
  430. /* Corresponds to buffer empty signaling on the bus */
  431. #define SD_CSR_GET_READY_FOR_DATA(csr) (((uint32_t)((csr).r[0]) >> 8) & 0x00000001)
  432. #define SD_CSR_GET_READY_FOR_DATA_FROM_R(r) (((uint32_t)(r) >> 8) & 0x00000001)
  433. /*
  434. * The state of the card when receiving the the command
  435. * (visible in the response of the next command)
  436. */
  437. #define SD_CSR_GET_CURRENT_STATE(csr) (((uint32_t)((csr).r[0]) >> 9) & 0x0000000f)
  438. #define SD_CSR_GET_CURRENT_STATE_FROM_R(r) (((uint32_t)(r) >> 9) & 0x0000000f)
  439. #define SD_STATE_IDLE 0
  440. #define SD_STATE_READY 1
  441. #define SD_STATE_IDENT 2
  442. #define SD_STATE_STBY 3
  443. #define SD_STATE_TRAN 4
  444. #define SD_STATE_DATA 5
  445. #define SD_STATE_RCV 6
  446. #define SD_STATE_PRG 7
  447. #define SD_STATE_DIS 8
  448. /* An erase sequence was cleared due to out of erase sequence command */
  449. #define SD_CSR_GET_ERASE_RESET(csr) (((uint32_t)((csr).r[0]) >> 13) & 0x00000001)
  450. #define SD_CSR_GET_ERASE_RESET_FROM_R(r) (((uint32_t)(r) >> 13) & 0x00000001)
  451. /* The command has been executed without using internal ECC */
  452. #define SD_CSR_GET_CARD_ECC_DISABLED(csr) (((uint32_t)((csr).r[0]) >> 14) & 0x00000001)
  453. #define SD_CSR_GET_CARD_ECC_DISABLED_FROM_R(r) (((uint32_t)(r) >> 14) & 0x00000001)
  454. /* Set only when partial address erase was erased due to write protected (tmp/perm) blocks */
  455. #define SD_CSR_GET_WP_ERASE_SKIP(csr) (((uint32_t)((csr).r[0]) >> 15) & 0x00000001)
  456. #define SD_CSR_GET_WP_ERASE_SKIP_FROM_R(r) (((uint32_t)(r) >> 15) & 0x00000001)
  457. /* Set when 1) read-only section of the CSD mismatch, 2) attempt to reverse copy or permanent WP bits */
  458. #define SD_CSR_GET_CSD_OVERWRITE(csr) (((uint32_t)((csr).r[0]) >> 16) & 0x00000001)
  459. #define SD_CSR_GET_CSD_OVERWRITE_FROM_R(r) (((uint32_t)(r) >> 16) & 0x00000001)
  460. /* General error or unknown error */
  461. #define SD_CSR_GET_ERROR(csr) (((uint32_t)((csr).r[0]) >> 19) & 0x00000001)
  462. #define SD_CSR_GET_ERROR_FROM_R(r) (((uint32_t)(r) >> 19) & 0x00000001)
  463. /* Internal card controller error */
  464. #define SD_CSR_GET_CC_ERROR(csr) (((uint32_t)((csr).r[0]) >> 20) & 0x00000001)
  465. #define SD_CSR_GET_CC_ERROR_FROM_R(r) (((uint32_t)(r) >> 20) & 0x00000001)
  466. /* Card internal ECC was applied but failed to correct the data */
  467. #define SD_CSR_GET_CARD_ECC_FAILED(csr) (((uint32_t)((csr).r[0]) >> 21) & 0x00000001)
  468. #define SD_CSR_GET_CARD_ECC_FAILED_FROM_R(r) (((uint32_t)(r) >> 21) & 0x00000001)
  469. /* Command not legalfor the card state */
  470. #define SD_CSR_GET_ILLEGAL_COMMAND(csr) (((uint32_t)((csr).r[0]) >> 22) & 0x00000001)
  471. #define SD_CSR_GET_ILLEGAL_COMMAND_FROM_R(r) (((uint32_t)(r) >> 22) & 0x00000001)
  472. /* The CRC check of previous command failed */
  473. #define SD_CSR_GET_COM_CRC_ERROR(csr) (((uint32_t)((csr).r[0]) >> 23) & 0x00000001)
  474. #define SD_CSR_GET_COM_CRC_ERROR_FROM_R(r) (((uint32_t)(r) >> 23) & 0x00000001)
  475. /* Set when a sequece or password error has been detected in lock/unlock card command */
  476. #define SD_CSR_GET_LOCK_UNLOCK_FAILED(csr) (((uint32_t)((csr).r[0]) >> 24) & 0x00000001)
  477. #define SD_CSR_GET_LOCK_UNLOCK_FAILED_FROM_R(r) (((uint32_t)(r) >> 24) & 0x00000001)
  478. /* Signals that the card is locked by the host */
  479. #define SD_CSR_GET_CARD_IS_LOCKED(csr) (((uint32_t)((csr).r[0]) >> 25) & 0x00000001)
  480. #define SD_CSR_GET_CARD_IS_LOCKED_FROM_R(r) (((uint32_t)(r) >> 25) & 0x00000001)
  481. /* Set when the host attempts to write protected block or tmp/perm protected card */
  482. #define SD_CSR_GET_WP_VIOLATION(csr) (((uint32_t)((csr).r[0]) >> 26) & 0x00000001)
  483. #define SD_CSR_GET_WP_VIOLATION_FROM_R(r) (((uint32_t)(r) >> 26) & 0x00000001)
  484. /* An invalid selection of write-blocks for erase occured */
  485. #define SD_CSR_GET_ERASE_PARAM(csr) (((uint32_t)((csr).r[0]) >> 27) & 0x00000001)
  486. #define SD_CSR_GET_ERASE_PARAM_FROM_R(r) (((uint32_t)(r) >> 27) & 0x00000001)
  487. /* An error in the sequence of erase commands occured */
  488. #define SD_CSR_GET_ERASE_SEQ_ERROR(csr) (((uint32_t)((csr).r[0]) >> 28) & 0x00000001)
  489. #define SD_CSR_GET_ERASE_SEQ_ERROR_FROM_R(r) (((uint32_t)(r) >> 28) & 0x00000001)
  490. /* Transferred block length is not allowed or transferred length does not match block length */
  491. #define SD_CSR_GET_BLOCK_LEN_ERROR(csr) (((uint32_t)((csr).r[0]) >> 29) & 0x00000001)
  492. #define SD_CSR_GET_BLOCK_LEN_ERROR_FROM_R(r) (((uint32_t)(r) >> 29) & 0x00000001)
  493. /* Misaligned address which did not match the block length was used in the command */
  494. #define SD_CSR_GET_ADDRESS_ERROR(csr) (((uint32_t)((csr).r[0]) >> 30) & 0x00000001)
  495. #define SD_CSR_GET_ADDRESS_ERROR_FROM_R(r) (((uint32_t)(r) >> 30) & 0x00000001)
  496. /* Command argument out of range or this card */
  497. #define SD_CSR_GET_OUT_OF_RANGE(csr) (((uint32_t)((csr).r[0]) >> 31) & 0x00000001)
  498. #define SD_CSR_GET_OUT_OF_RANGE_FROM_R(r) (((uint32_t)(r) >> 31) & 0x00000001)
  499. /* Check all data error bits at once */
  500. #define SD_CSR_CHECK_ERROR_BITS(csr) ((uint32_t)((csr).r[0]) & (SD_CSR_AKE_SEQ_ERROR | SD_CSR_ERASE_RESET | \
  501. SD_CSR_WP_ERASE_SKIP | SD_CSR_CSD_OVERWRITE | \
  502. SD_CSR_ERROR | SD_CSR_CC_ERROR | SD_CSR_CARD_ECC_FAILED | \
  503. /*SD_CSR_ILLEGAL_COMMAND |*/ SD_CSR_COM_CRC_ERROR | SD_CSR_LOCK_UNLOCK_FAILED | \
  504. SD_CSR_CARD_IS_LOCKED | SD_CSR_WP_VIOLATION | SD_CSR_ERASE_PARAM | \
  505. SD_CSR_ERASE_SEQ_ERROR | SD_CSR_BLOCK_LEN_ERROR | SD_CSR_ADDRESS_ERROR | \
  506. SD_CSR_OUT_OF_RANGE))
  507. /*
  508. * SSR - SD Status Register
  509. * Width: 512 bits
  510. * Readback: ACMD13_SD_STATUS/DAT0
  511. */
  512. #if 0
  513. typedef struct _SD_SSR {
  514. /*
  515. * Note: The bitfields definition was not applied due to that the
  516. * core architecture may be switched between big-endian
  517. * and little endian. We don't want to define two structures
  518. * to switch between the two endain architectures. Users
  519. * could use following macros to extract the target member.
  520. */
  521. union {
  522. uint32_t r[16];
  523. uint8_t b[64];
  524. };
  525. } SD_SSR;
  526. #endif
  527. /* Fixed offset value added to erase time */
  528. #define SD_SSR_GET_ERASE_OFFSET(ssr) (((uint32_t)((csr).b[50]) >> 0) & 0x00000003)
  529. /* Timeout value for erasing areas specified by UINT_OF_ERASE_AU */
  530. #define SD_SSR_GET_ERASE_TIMEOUT(ssr) (((uint32_t)((csr).b[50]) >> 2) & 0x0000003f)
  531. /* Number of AUs to be erased at a time */
  532. #define SD_SSR_GET_ERASE_SIZE(ssr) (((uint32_t)((csr).b[52]) << 8) | (uint32_t)((csr).b[51]))
  533. /* Size of AU */
  534. #define SD_SSR_GET_AU_SIZE(ssr) ((uint32_t)((csr).b[53]) >> 4)
  535. #define SD_AU_SIZE_NONE 0x00
  536. #define SD_AU_SIZE_16KB 0x01
  537. #define SD_AU_SIZE_32KB 0x02
  538. #define SD_AU_SIZE_64KB 0x03
  539. #define SD_AU_SIZE_128KB 0x04
  540. #define SD_AU_SIZE_256KB 0x05
  541. #define SD_AU_SIZE_512KB 0x06
  542. #define SD_AU_SIZE_1MB 0x07
  543. #define SD_AU_SIZE_2MB 0x08
  544. #define SD_AU_SIZE_4MB 0x09
  545. /* Performance of move indicated by 1 MB/s step */
  546. #define SD_SSR_GET_PERFORMANCE_MOVE(ssr) ((uint32_t)((csr).b[54]))
  547. /* Speed class of the card */
  548. #define SD_SSR_GET_SPEED_CLASS(ssr) ((uint32_t)((csr).b[55]))
  549. #define SD_SPEED_CLASS_0 0x00
  550. #define SD_SPEED_CLASS_2 0x01
  551. #define SD_SPEED_CLASS_4 0x02
  552. #define SD_SPEED_CLASS_6 0x03
  553. /* Size of protected area */
  554. #define SD_SSR_GET_SIZE_OF_PROTECTED_AREA(ssr) ((uint32_t)((csr).r[14]))
  555. /* SD memory card type */
  556. #define SD_SSR_GET_SD_CARD_TYPE(ssr) (((uint32_t)((csr).r[15]) >> 0) & 0x0000ffff)
  557. #define SD_CARD_TYPE_REGULAR 0x0000
  558. #define SD_CARD_TYPE_ROM 0x0001
  559. /* Card in secured mode operation */
  560. #define SD_SSR_GET_SECURED_MODE(ssr) (((uint32_t)((csr).b[63]) >> 5) & 0x00000001)
  561. /* Shows the current defined data bus width defined by ACMD6_SET_BUS_WIDTH */
  562. #define SD_SSR_GET_DAT_BUS_WIDTH(ssr) (((uint32_t)((csr).b[63]) >> 6) & 0x00000003)
  563. /*****************************************************************************
  564. * Secure Digital Memory Card Command Definitions
  565. *
  566. ****************************************************************************/
  567. /*
  568. * Class 0 - Basic Commands
  569. * Responses:
  570. * R1-R3, R6
  571. * Registers:
  572. * CID, CSD, DSR
  573. */
  574. #define SD_CMD0_GO_IDLE_STATE 0 /* Resets all cards to idle state */
  575. #define SD_CMD2_ALL_SEND_CID 2 /* Ask any card to send the CID numbers */
  576. #define SD_CMD3_SEND_RELATIVE_ADDR 3 /* Ask the card to publish a new relative address */
  577. #define SD_CMD4_SET_DSR 4 /* Programs the DSR of of all cards */
  578. #define SD_CMD7_SELECT_DESELECT_CARD 7 /* Selected (its own relative address), deselected (other address) */
  579. #define SD_CMD8_SEND_IF_COND 8 /* Sends SD Memory Card interface condition */
  580. #define SD_CMD8_MAKE_ARG(vhs, ptn) ((((uint32_t)(vhs) & 0x0f) << 8) | ((uint32_t)(ptn) & 0xff))
  581. #define SD_VHS_2_7V_3_6V 0x01
  582. #define SD_VHS_LOW_VOLTAGE 0x02
  583. #define SD_CMD8_DEFAULT_PTN 0xaa /* spec 20 suggested test pattern */
  584. #define SD_CMD9_SEND_CSD 9 /* Addressed card sends its card-specific data (CSD) on the cmd line */
  585. #define SD_CMD10_SEND_CID 10 /* Addressed card send its card-identification (CID) on the cmd line */
  586. #define SD_CMD12_STOP_TRANSMISSION 12 /* Forces the card to stop transmission */
  587. #define SD_CMD13_SEND_STATUS 13 /* Addressed card sends its status register */
  588. #define SD_CMD15_GO_INACTIVE_STATE 15 /* Sends an addressed card into the inactive state */
  589. /*
  590. * Class 2 - Block-Oriented Read Commands
  591. * Responses:
  592. * R1
  593. */
  594. #define SD_CMD16_SET_BLOCKLEN 16 /* Sets the block length for all following block commands (std cap.: read/write/lock, high cap.: lock) */
  595. #define SD_CMD17_READ_SINGLE_BLOCK 17 /* Reads a block of size (std: SET_BLOCKLEN, high: 512) */
  596. #define SD_CMD18_READ_MULTIPLE_BLOCK 18 /* Continuously transfers data from card to host until interrupted by a STOP_TRANSMISSION command */
  597. /*
  598. * Class 4 - Block-Oriented Write Commands
  599. * Responses:
  600. * R1
  601. * Registers:
  602. * CSD
  603. */
  604. #define SD_CMD24_WRITE_BLOCK 24 /* Writes a block of the size (srd: SET_BLOCKLEN, high: 512) */
  605. #define SD_CMD25_WRITE_MULTIPLE_BLOCK 25 /* Continuously writes block of data until interrupted by a STOP_TRANSMISSION command */
  606. #define SD_CMD27_PROGRAM_CSD 27 /* Programming of the programmable bits of the CSD */
  607. /*
  608. * Class 6 - Block-Oriented Write Protection Commands
  609. * Responses:
  610. * R1
  611. */
  612. #define SD_CMD28_SET_WRITE_PROT 28 /* Std: Sets the write protection bit of the addressed group, high: not supported */
  613. #define SD_CMD29_CLR_WRITE_PROT 29 /* Std: clears the write protection bit of the addressed group, high: not supported */
  614. #define SD_CMD30_SEND_WRITE_PROT 30 /* Std: ask the card to send the status of the write protection bits, high: not supported */
  615. /*
  616. * Class 5 - Erase Commands
  617. * Responses:
  618. * R1
  619. */
  620. #define SD_CMD32_ERASE_WR_BLK_START 32 /* Sets the address of the first write block to be erased */
  621. #define SD_CMD33_ERASE_WR_BLK_END 33 /* Sets the address of the last write block of the continuous range to be erased */
  622. #define SD_CMD38_ERASE 38 /* Erase all previously selected write blocks */
  623. /*
  624. * Class 7 - Lock Card Commands
  625. * Responses:
  626. * R1
  627. */
  628. #define SD_CMD42_LOCK_UNLOCK 42 /* To set/reset the password or lock/unlock the card (SET_BLOCKLEN) */
  629. /*
  630. * Class 8 - Application Specific Commands
  631. * Responses:
  632. * R1
  633. * Registers:
  634. * SCR
  635. */
  636. #define SD_CMD55_APP_CMD 55 /* Indicates to the card that the next command is an application specific command */
  637. #define SD_CMD56_GEN_CMD 56 /* Used to transfer/get a data block to/from the card for general purpose/application specific commands (SET_BLOCKLEN) */
  638. #define SD_ACMD6_SET_BUS_WIDTH 6 /* Defines the data bus width to be used for data thransfer (allowed: SCR) */
  639. #define SD_BUS_WIDTH_ARG_1BIT 0
  640. #define SD_BUS_WIDTH_ARG_4BIT 2
  641. #define SD_ACMD6_MAKE_ARG(bw) ((uint32_t)(bw) & 0x00000003)
  642. #define SD_ACMD13_SD_STATUS 13 /* Send the SD status (table 4-37) */
  643. #define SD_ACMD22_SEND_NUM_WR_BLOCKS 22 /* Send the number of the written write blocks */
  644. #define SD_ACMD23_SET_WR_BLK_ERASE_COUNT 23 /* Set the number of write blocks to be preerased before writing */
  645. #define SD_ACMD41_SD_SEND_OP_COND 41 /* Sends host capacity support information (HCS) and asks operating condition register (OCR) */
  646. #define SD_ACMD41_MAKE_ARG(hcs) ((((uint32_t)(hcs) << SD_OCR_CCS_SHIFT) & SD_OCR_CCS_MASK) | SD_OCR_VDD_2_7V_3_6V)
  647. #define SD_HCS_SD 0 /* (Host Capacity Support) Denotes not supporting SDHC */
  648. #define SD_HCS_SDHC 1 /* (Host Capacity Support) Denotes SDHC support */
  649. #define SD_ACMD42_SET_CLR_CARD_DETECT 42 /* Connect/Disconnect the 50KOhm pull-up resister on CD/DAT3 of the card */
  650. #define SD_ACMD51_SEND_SCR 51 /* Reads the SD configuration register (SCR) */
  651. /*
  652. * Class 10 - Application Specific Commands
  653. * Responses:
  654. * R1
  655. */
  656. #define SD_CMD6_SWITCH_FUNC 6 /* Checks switchable function (mode 0) and switch card function (mode 1) */
  657. /*****************************************************************************
  658. * SD Internal Programming Interfaces
  659. *
  660. ****************************************************************************/
  661. /* PIO SD command retry counts */
  662. #define SD_CMD_MAX_RETRY_COUNT 0x10000
  663. #define SD_ACMD41_MAX_RETRY_COUNT 0x100
  664. #define SD_READ_MAX_RETRY_COUNT 0x1000
  665. #define SD_WRITE_MAX_RETRY_COUNT 0x2000
  666. #define SD_STATE_MAX_RETRY_COUNT 0x1000
  667. #define SD_TRANSFER_MAX_RETRY_COUNT 0x1000
  668. /* SD command predefined to be sent to the SDC */
  669. /* Class 0 - Basic Commands */
  670. #define SDC_CMD0_GO_IDLE_STATE \
  671. ((SD_CMD0_GO_IDLE_STATE & SDC_CMD_IDX_MASK) | SDC_CMD_EN_MASK)
  672. #define SDC_CMD2_ALL_SEND_CID \
  673. ((SD_CMD2_ALL_SEND_CID & SDC_CMD_IDX_MASK) | SDC_CMD_EN_MASK | SDC_NEED_RSP_MASK | SDC_LONG_RSP_MASK)
  674. #define SDC_CMD3_SEND_RELATIVE_ADDR \
  675. ((SD_CMD3_SEND_RELATIVE_ADDR & SDC_CMD_IDX_MASK) | SDC_CMD_EN_MASK | SDC_NEED_RSP_MASK)
  676. #define SDC_CMD4_SET_DSR \
  677. ((SD_CMD4_SET_DSR & SDC_CMD_IDX_MASK) | SDC_CMD_EN_MASK)
  678. #define SDC_CMD7_SELECT_DESELECT_CARD \
  679. ((SD_CMD7_SELECT_DESELECT_CARD & SDC_CMD_IDX_MASK) | SDC_CMD_EN_MASK | SDC_NEED_RSP_MASK)
  680. #define SDC_CMD8_SEND_IF_COND \
  681. ((SD_CMD8_SEND_IF_COND & SDC_CMD_IDX_MASK) | SDC_CMD_EN_MASK | SDC_NEED_RSP_MASK)
  682. #define SDC_CMD9_SEND_CSD \
  683. ((SD_CMD9_SEND_CSD & SDC_CMD_IDX_MASK) | SDC_CMD_EN_MASK | SDC_NEED_RSP_MASK | SDC_LONG_RSP_MASK)
  684. #define SDC_CMD10_SEND_CID \
  685. ((SD_CMD10_SEND_CID & SDC_CMD_IDX_MASK) | SDC_CMD_EN_MASK | SDC_NEED_RSP_MASK | SDC_LONG_RSP_MASK)
  686. #define SDC_CMD12_STOP_TRANSMISSION \
  687. ((SD_CMD12_STOP_TRANSMISSION & SDC_CMD_IDX_MASK) | SDC_CMD_EN_MASK | SDC_NEED_RSP_MASK)
  688. #define SDC_CMD13_SEND_STATUS \
  689. ((SD_CMD13_SEND_STATUS & SDC_CMD_IDX_MASK) | SDC_CMD_EN_MASK | SDC_NEED_RSP_MASK)
  690. #define SDC_CMD15_GO_INACTIVE_STATE \
  691. ((SD_CMD15_GO_INACTIVE_STATE & SDC_CMD_IDX_MASK) | SDC_CMD_EN_MASK)
  692. /* Class 2 - Block-Oriented Read Commands */
  693. #define SDC_CMD16_SET_BLOCKLEN \
  694. ((SD_CMD16_SET_BLOCKLEN & SDC_CMD_IDX_MASK) | SDC_CMD_EN_MASK | SDC_NEED_RSP_MASK)
  695. #define SDC_CMD17_READ_SINGLE_BLOCK \
  696. ((SD_CMD17_READ_SINGLE_BLOCK & SDC_CMD_IDX_MASK) | SDC_CMD_EN_MASK | SDC_NEED_RSP_MASK)
  697. #define SDC_CMD18_READ_MULTIPLE_BLOCK \
  698. ((SD_CMD18_READ_MULTIPLE_BLOCK & SDC_CMD_IDX_MASK) | SDC_CMD_EN_MASK | SDC_NEED_RSP_MASK)
  699. /* Class 4 - Block-Oriented Write Commands */
  700. #define SDC_CMD24_WRITE_BLOCK \
  701. ((SD_CMD24_WRITE_BLOCK & SDC_CMD_IDX_MASK) | SDC_CMD_EN_MASK | SDC_NEED_RSP_MASK)
  702. #define SDC_CMD25_WRITE_MULTIPLE_BLOCK \
  703. ((SD_CMD25_WRITE_MULTIPLE_BLOCK & SDC_CMD_IDX_MASK) | SDC_CMD_EN_MASK | SDC_NEED_RSP_MASK)
  704. #define SDC_CMD27_PROGRAM_CSD \
  705. ((SD_CMD27_PROGRAM_CSD & SDC_CMD_IDX_MASK) | SDC_CMD_EN_MASK | SDC_NEED_RSP_MASK)
  706. /* Class 6 - Block-Oriented Write Protection Commands */
  707. #define SDC_CMD28_SET_WRITE_PROT \
  708. ((SD_CMD28_SET_WRITE_PROT & SDC_CMD_IDX_MASK) | SDC_CMD_EN_MASK | SDC_NEED_RSP_MASK)
  709. #define SDC_CMD29_CLR_WRITE_PROT \
  710. ((SD_CMD29_CLR_WRITE_PROT & SDC_CMD_IDX_MASK) | SDC_CMD_EN_MASK | SDC_NEED_RSP_MASK)
  711. #define SDC_CMD30_SEND_WRITE_PROT \
  712. ((SD_CMD30_SEND_WRITE_PROT & SDC_CMD_IDX_MASK) | SDC_CMD_EN_MASK | SDC_NEED_RSP_MASK)
  713. /* Class 5 - Erase Commands */
  714. #define SDC_CMD32_ERASE_WR_BLK_START \
  715. ((SD_CMD32_ERASE_WR_BLK_START & SDC_CMD_IDX_MASK) | SDC_CMD_EN_MASK | SDC_NEED_RSP_MASK)
  716. #define SDC_CMD33_ERASE_WR_BLK_END \
  717. ((SD_CMD33_ERASE_WR_BLK_END & SDC_CMD_IDX_MASK) | SDC_CMD_EN_MASK | SDC_NEED_RSP_MASK)
  718. #define SDC_CMD38_ERASE \
  719. ((SD_CMD38_ERASE & SDC_CMD_IDX_MASK) | SDC_CMD_EN_MASK | SDC_NEED_RSP_MASK)
  720. /* Class 7 - Lock Card Commands */
  721. #define SDC_CMD42_LOCK_UNLOCK \
  722. ((SD_CMD42_LOCK_UNLOCK & SDC_CMD_IDX_MASK) | SDC_CMD_EN_MASK | SDC_NEED_RSP_MASK)
  723. /* Class 8 - Application Specific Commands */
  724. #define SDC_CMD55_APP_CMD \
  725. ((SD_CMD55_APP_CMD & SDC_CMD_IDX_MASK) | SDC_CMD_EN_MASK | SDC_NEED_RSP_MASK)
  726. #define SDC_CMD56_GEN_CMD \
  727. ((SD_CMD56_GEN_CMD & SDC_CMD_IDX_MASK) | SDC_CMD_EN_MASK | SDC_NEED_RSP_MASK)
  728. #define SDC_ACMD6_SET_BUS_WIDTH \
  729. ((SD_ACMD6_SET_BUS_WIDTH & SDC_CMD_IDX_MASK) | SDC_CMD_EN_MASK | SDC_NEED_RSP_MASK | SDC_APP_CMD_MASK)
  730. #define SDC_ACMD13_SD_STATUS \
  731. ((SD_ACMD13_SD_STATUS & SDC_CMD_IDX_MASK) | SDC_CMD_EN_MASK | SDC_NEED_RSP_MASK | SDC_APP_CMD_MASK)
  732. #define SDC_ACMD22_SEND_NUM_WR_BLOCKS \
  733. ((SD_ACMD22_SEND_NUM_WR_BLOCKS & SDC_CMD_IDX_MASK) | SDC_CMD_EN_MASK | SDC_NEED_RSP_MASK | SDC_APP_CMD_MASK)
  734. #define SDC_ACMD23_SET_WR_BLK_ERASE_COUNT \
  735. ((SD_ACMD23_SET_WR_BLK_ERASE_COUNT & SDC_CMD_IDX_MASK) | SDC_CMD_EN_MASK | SDC_NEED_RSP_MASK | SDC_APP_CMD_MASK)
  736. #define SDC_ACMD41_SD_SEND_OP_COND \
  737. ((SD_ACMD41_SD_SEND_OP_COND & SDC_CMD_IDX_MASK) | SDC_CMD_EN_MASK | SDC_NEED_RSP_MASK | SDC_APP_CMD_MASK)
  738. #define SDC_ACMD42_SET_CLR_CARD_DETECT \
  739. ((SD_ACMD42_SET_CLR_CARD_DETECT & SDC_CMD_IDX_MASK) | SDC_CMD_EN_MASK | SDC_NEED_RSP_MASK | SDC_APP_CMD_MASK)
  740. #define SDC_ACMD51_SEND_SCR \
  741. ((SD_ACMD51_SEND_SCR & SDC_CMD_IDX_MASK) | SDC_CMD_EN_MASK | SDC_NEED_RSP_MASK | SDC_APP_CMD_MASK)
  742. /* Class 10 - Application Specific Commands */
  743. #define SDC_CMD6_SWITCH_FUNC \
  744. ((SD_CMD6_SWITCH_FUNC & SDC_CMD_IDX_MASK) | SDC_CMD_EN_MASK | SDC_NEED_RSP_MASK)
  745. /* SD command interfaces */
  746. #if (SDD_SMALL_SD_FOOTPRINT == 0)
  747. extern uint32_t _sd_cmd0(void);
  748. extern uint32_t _sd_cmd4(uint32_t dsr);
  749. extern uint32_t _sd_cmd10(uint32_t rca, SD_CID * cid);
  750. extern uint32_t _sd_cmd15(uint32_t rca);
  751. extern uint32_t _sd_cmd17(uint32_t addr, SD_R32 * csr);
  752. extern uint32_t _sd_cmd24(uint32_t addr, SD_R32 * csr);
  753. extern uint32_t _sd_cmd27(SD_R32 * csr);
  754. extern uint32_t _sd_cmd28(uint32_t addr, SD_R32 * csr);
  755. extern uint32_t _sd_cmd29(uint32_t addr, SD_R32 * csr);
  756. extern uint32_t _sd_cmd30(uint32_t addr, SD_R32 * csr);
  757. extern uint32_t _sd_cmd32(uint32_t addr, SD_R32 * csr);
  758. extern uint32_t _sd_cmd33(uint32_t addr, SD_R32 * csr);
  759. extern uint32_t _sd_cmd38(SD_R32 * csr);
  760. extern uint32_t _sd_cmd42(SD_R32 * csr);
  761. extern uint32_t _sd_cmd56(uint32_t rdwr, SD_R32 * csr);
  762. extern uint32_t _sd_acmd22(SD_R32 * csr);
  763. extern uint32_t _sd_acmd23(uint32_t blocks, SD_R32 * csr);
  764. extern uint32_t _sd_acmd42(uint32_t conn, SD_R32 * csr);
  765. extern uint32_t _sd_cmd6(uint32_t mode, SD_R32 * csr);
  766. #endif /* (SDD_SMALL_SD_FOOTPRINT == 0) */
  767. extern uint32_t _sd_cmd2(SD_CID * cid);
  768. extern uint32_t _sd_cmd3(SD_R32 * rca);
  769. extern uint32_t _sd_cmd7(uint32_t rca, SD_R32 * csr);
  770. extern uint32_t _sd_cmd8(uint32_t vhs_arg, SD_R32 * vhs_rsp);
  771. extern uint32_t _sd_cmd9(uint32_t rca, SD_CSD * csd);
  772. extern uint32_t _sd_cmd12(SD_R32 * csr);
  773. extern uint32_t _sd_cmd13(uint32_t rca, SD_R32 * csr);
  774. extern uint32_t _sd_cmd16(uint32_t blk_len, SD_R32 * csr);
  775. extern uint32_t _sd_cmd18(uint32_t addr, SD_R32 * csr);
  776. extern uint32_t _sd_cmd25(uint32_t addr, SD_R32 * csr);
  777. extern uint32_t _sd_cmd55(uint32_t rca, SD_R32 * csr);
  778. extern uint32_t _sd_acmd6(uint32_t bw, SD_R32 * csr);
  779. extern uint32_t _sd_acmd13(SD_R32 * csr);
  780. extern uint32_t _sd_acmd41(uint32_t hcs, SD_OCR * ocr);
  781. extern uint32_t _sd_acmd51(SD_R32 * csr);
  782. /* Data transfer mode state transition routines */
  783. // extern uint32_t _sd_wait_sending_state(uint32_t rca);
  784. // extern uint32_t _sd_wait_receiving_state(uint32_t rca);
  785. extern uint32_t _sd_wait_programmed(uint32_t rca);
  786. extern uint32_t _sd_wait_transferred(uint32_t rca);
  787. extern uint32_t _sd_disconnect(uint32_t rca);
  788. extern uint32_t _sd_connect(uint32_t rca);
  789. extern uint32_t _sd_deselect_card(uint32_t rca);
  790. extern uint32_t _sd_select_card(uint32_t rca);
  791. extern uint32_t _sd_stop_transmission(uint32_t rca);
  792. #endif /* __SDD_SD_H__ */