sdmmcreg.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. /* $OpenBSD: sdmmcreg.h,v 1.6 2014/09/23 12:08:13 rapha Exp $ */
  2. /*
  3. * Copyright (c) 2006 Uwe Stuehler <uwe@openbsd.org>
  4. *
  5. * Permission to use, copy, modify, and distribute this software for any
  6. * purpose with or without fee is hereby granted, provided that the above
  7. * copyright notice and this permission notice appear in all copies.
  8. *
  9. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  10. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  11. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  12. * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  13. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  14. * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  15. * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  16. */
  17. #ifndef _SDMMCREG_H_
  18. #define _SDMMCREG_H_
  19. /* MMC commands */ /* response type */
  20. #define MMC_GO_IDLE_STATE 0 /* R0 */
  21. #define MMC_SEND_OP_COND 1 /* R3 */
  22. #define MMC_ALL_SEND_CID 2 /* R2 */
  23. #define MMC_SET_RELATIVE_ADDR 3 /* R1 */
  24. #define MMC_SWITCH 6 /* R1B */
  25. #define MMC_SELECT_CARD 7 /* R1 */
  26. #define MMC_SEND_EXT_CSD 8 /* R1 */
  27. #define MMC_SEND_CSD 9 /* R2 */
  28. #define MMC_STOP_TRANSMISSION 12 /* R1B */
  29. #define MMC_SEND_STATUS 13 /* R1 */
  30. #define MMC_SET_BLOCKLEN 16 /* R1 */
  31. #define MMC_READ_BLOCK_SINGLE 17 /* R1 */
  32. #define MMC_READ_BLOCK_MULTIPLE 18 /* R1 */
  33. #define MMC_SET_BLOCK_COUNT 23 /* R1 */
  34. #define MMC_WRITE_BLOCK_SINGLE 24 /* R1 */
  35. #define MMC_WRITE_BLOCK_MULTIPLE 25 /* R1 */
  36. #define MMC_APP_CMD 55 /* R1 */
  37. /* SD commands */ /* response type */
  38. #define SD_SEND_RELATIVE_ADDR 3 /* R6 */
  39. #define SD_SEND_IF_COND 8 /* R7 */
  40. /* SD application commands */ /* response type */
  41. #define SD_APP_SET_BUS_WIDTH 6 /* R1 */
  42. #define SD_APP_OP_COND 41 /* R3 */
  43. /* OCR bits */
  44. #define MMC_OCR_MEM_READY (1<<31) /* memory power-up status bit */
  45. #define MMC_OCR_3_5V_3_6V (1<<23)
  46. #define MMC_OCR_3_4V_3_5V (1<<22)
  47. #define MMC_OCR_3_3V_3_4V (1<<21)
  48. #define MMC_OCR_3_2V_3_3V (1<<20)
  49. #define MMC_OCR_3_1V_3_2V (1<<19)
  50. #define MMC_OCR_3_0V_3_1V (1<<18)
  51. #define MMC_OCR_2_9V_3_0V (1<<17)
  52. #define MMC_OCR_2_8V_2_9V (1<<16)
  53. #define MMC_OCR_2_7V_2_8V (1<<15)
  54. #define MMC_OCR_2_6V_2_7V (1<<14)
  55. #define MMC_OCR_2_5V_2_6V (1<<13)
  56. #define MMC_OCR_2_4V_2_5V (1<<12)
  57. #define MMC_OCR_2_3V_2_4V (1<<11)
  58. #define MMC_OCR_2_2V_2_3V (1<<10)
  59. #define MMC_OCR_2_1V_2_2V (1<<9)
  60. #define MMC_OCR_2_0V_2_1V (1<<8)
  61. #define MMC_OCR_1_9V_2_0V (1<<7)
  62. #define MMC_OCR_1_8V_1_9V (1<<6)
  63. #define MMC_OCR_1_7V_1_8V (1<<5)
  64. #define MMC_OCR_1_6V_1_7V (1<<4)
  65. #define SD_OCR_SDHC_CAP (1<<30)
  66. #define SD_OCR_VOL_MASK 0xFF8000 /* bits 23:15 */
  67. /* R1 response type bits */
  68. #define MMC_R1_READY_FOR_DATA (1<<8) /* ready for next transfer */
  69. #define MMC_R1_APP_CMD (1<<5) /* app. commands supported */
  70. /* 48-bit response decoding (32 bits w/o CRC) */
  71. #define MMC_R1(resp) ((resp)[0])
  72. #define MMC_R3(resp) ((resp)[0])
  73. #define SD_R6(resp) ((resp)[0])
  74. /* RCA argument and response */
  75. #define MMC_ARG_RCA(rca) ((rca) << 16)
  76. #define SD_R6_RCA(resp) (SD_R6((resp)) >> 16)
  77. /* bus width argument */
  78. #define SD_ARG_BUS_WIDTH_1 0
  79. #define SD_ARG_BUS_WIDTH_4 2
  80. /* EXT_CSD fields */
  81. #define EXT_CSD_BUS_WIDTH 183 /* WO */
  82. #define EXT_CSD_HS_TIMING 185 /* R/W */
  83. #define EXT_CSD_REV 192 /* RO */
  84. #define EXT_CSD_STRUCTURE 194 /* RO */
  85. #define EXT_CSD_CARD_TYPE 196 /* RO */
  86. #define EXT_CSD_SEC_COUNT 212 /* RO */
  87. /* EXT_CSD field definitions */
  88. #define EXT_CSD_CMD_SET_NORMAL (1U << 0)
  89. #define EXT_CSD_CMD_SET_SECURE (1U << 1)
  90. #define EXT_CSD_CMD_SET_CPSECURE (1U << 2)
  91. /* EXT_CSD_BUS_WIDTH */
  92. #define EXT_CSD_BUS_WIDTH_1 0
  93. #define EXT_CSD_BUS_WIDTH_4 1
  94. #define EXT_CSD_BUS_WIDTH_8 2
  95. /* EXT_CSD_CARD_TYPE */
  96. /* The only currently valid values for this field are 0x01, 0x03, 0x07,
  97. * 0x0B and 0x0F. */
  98. #define EXT_CSD_CARD_TYPE_F_26M (1 << 0)
  99. #define EXT_CSD_CARD_TYPE_F_52M (1 << 1)
  100. #define EXT_CSD_CARD_TYPE_F_52M_1_8V (1 << 2)
  101. #define EXT_CSD_CARD_TYPE_F_52M_1_2V (1 << 3)
  102. #define EXT_CSD_CARD_TYPE_26M 0x01
  103. #define EXT_CSD_CARD_TYPE_52M 0x03
  104. #define EXT_CSD_CARD_TYPE_52M_V18 0x07
  105. #define EXT_CSD_CARD_TYPE_52M_V12 0x0b
  106. #define EXT_CSD_CARD_TYPE_52M_V12_18 0x0f
  107. /* MMC_SWITCH access mode */
  108. #define MMC_SWITCH_MODE_CMD_SET 0x00 /* Change the command set */
  109. #define MMC_SWITCH_MODE_SET_BITS 0x01 /* Set bits in value */
  110. #define MMC_SWITCH_MODE_CLEAR_BITS 0x02 /* Clear bits in value */
  111. #define MMC_SWITCH_MODE_WRITE_BYTE 0x03 /* Set target to value */
  112. /* MMC R2 response (CSD) */
  113. #define MMC_CSD_CSDVER(resp) MMC_RSP_BITS((resp), 126, 2)
  114. #define MMC_CSD_CSDVER_1_0 1
  115. #define MMC_CSD_CSDVER_2_0 2
  116. #define MMC_CSD_CSDVER_EXT_CSD 3
  117. #define MMC_CSD_MMCVER(resp) MMC_RSP_BITS((resp), 122, 4)
  118. #define MMC_CSD_MMCVER_1_0 0 /* MMC 1.0 - 1.2 */
  119. #define MMC_CSD_MMCVER_1_4 1 /* MMC 1.4 */
  120. #define MMC_CSD_MMCVER_2_0 2 /* MMC 2.0 - 2.2 */
  121. #define MMC_CSD_MMCVER_3_1 3 /* MMC 3.1 - 3.3 */
  122. #define MMC_CSD_MMCVER_4_0 4 /* MMC 4 */
  123. #define MMC_CSD_READ_BL_LEN(resp) MMC_RSP_BITS((resp), 80, 4)
  124. #define MMC_CSD_C_SIZE(resp) MMC_RSP_BITS((resp), 62, 12)
  125. #define MMC_CSD_CAPACITY(resp) ((MMC_CSD_C_SIZE((resp))+1) << \
  126. (MMC_CSD_C_SIZE_MULT((resp))+2))
  127. #define MMC_CSD_C_SIZE_MULT(resp) MMC_RSP_BITS((resp), 47, 3)
  128. /* MMC v1 R2 response (CID) */
  129. #define MMC_CID_MID_V1(resp) MMC_RSP_BITS((resp), 104, 24)
  130. #define MMC_CID_PNM_V1_CPY(resp, pnm) \
  131. do { \
  132. (pnm)[0] = MMC_RSP_BITS((resp), 96, 8); \
  133. (pnm)[1] = MMC_RSP_BITS((resp), 88, 8); \
  134. (pnm)[2] = MMC_RSP_BITS((resp), 80, 8); \
  135. (pnm)[3] = MMC_RSP_BITS((resp), 72, 8); \
  136. (pnm)[4] = MMC_RSP_BITS((resp), 64, 8); \
  137. (pnm)[5] = MMC_RSP_BITS((resp), 56, 8); \
  138. (pnm)[6] = MMC_RSP_BITS((resp), 48, 8); \
  139. (pnm)[7] = '\0'; \
  140. } while (0)
  141. #define MMC_CID_REV_V1(resp) MMC_RSP_BITS((resp), 40, 8)
  142. #define MMC_CID_PSN_V1(resp) MMC_RSP_BITS((resp), 16, 24)
  143. #define MMC_CID_MDT_V1(resp) MMC_RSP_BITS((resp), 8, 8)
  144. /* MMC v2 R2 response (CID) */
  145. #define MMC_CID_MID_V2(resp) MMC_RSP_BITS((resp), 120, 8)
  146. #define MMC_CID_OID_V2(resp) MMC_RSP_BITS((resp), 104, 16)
  147. #define MMC_CID_PNM_V2_CPY(resp, pnm) \
  148. do { \
  149. (pnm)[0] = MMC_RSP_BITS((resp), 96, 8); \
  150. (pnm)[1] = MMC_RSP_BITS((resp), 88, 8); \
  151. (pnm)[2] = MMC_RSP_BITS((resp), 80, 8); \
  152. (pnm)[3] = MMC_RSP_BITS((resp), 72, 8); \
  153. (pnm)[4] = MMC_RSP_BITS((resp), 64, 8); \
  154. (pnm)[5] = MMC_RSP_BITS((resp), 56, 8); \
  155. (pnm)[6] = '\0'; \
  156. } while (0)
  157. #define MMC_CID_PSN_V2(resp) MMC_RSP_BITS((resp), 16, 32)
  158. /* SD R2 response (CSD) */
  159. #define SD_CSD_CSDVER(resp) MMC_RSP_BITS((resp), 126, 2)
  160. #define SD_CSD_CSDVER_1_0 0
  161. #define SD_CSD_CSDVER_2_0 1
  162. #define SD_CSD_TAAC(resp) MMC_RSP_BITS((resp), 112, 8)
  163. #define SD_CSD_TAAC_1_5_MSEC 0x26
  164. #define SD_CSD_NSAC(resp) MMC_RSP_BITS((resp), 104, 8)
  165. #define SD_CSD_SPEED(resp) MMC_RSP_BITS((resp), 96, 8)
  166. #define SD_CSD_SPEED_25_MHZ 0x32
  167. #define SD_CSD_SPEED_50_MHZ 0x5a
  168. #define SD_CSD_CCC(resp) MMC_RSP_BITS((resp), 84, 12)
  169. #define SD_CSD_CCC_ALL 0x5f5
  170. #define SD_CSD_READ_BL_LEN(resp) MMC_RSP_BITS((resp), 80, 4)
  171. #define SD_CSD_READ_BL_PARTIAL(resp) MMC_RSP_BITS((resp), 79, 1)
  172. #define SD_CSD_WRITE_BLK_MISALIGN(resp) MMC_RSP_BITS((resp), 78, 1)
  173. #define SD_CSD_READ_BLK_MISALIGN(resp) MMC_RSP_BITS((resp), 77, 1)
  174. #define SD_CSD_DSR_IMP(resp) MMC_RSP_BITS((resp), 76, 1)
  175. #define SD_CSD_C_SIZE(resp) MMC_RSP_BITS((resp), 62, 12)
  176. #define SD_CSD_CAPACITY(resp) ((SD_CSD_C_SIZE((resp))+1) << \
  177. (SD_CSD_C_SIZE_MULT((resp))+2))
  178. #define SD_CSD_V2_C_SIZE(resp) MMC_RSP_BITS((resp), 48, 22)
  179. #define SD_CSD_V2_CAPACITY(resp) ((SD_CSD_V2_C_SIZE((resp))+1) << 10)
  180. #define SD_CSD_V2_BL_LEN 0x9 /* 512 */
  181. #define SD_CSD_VDD_R_CURR_MIN(resp) MMC_RSP_BITS((resp), 59, 3)
  182. #define SD_CSD_VDD_R_CURR_MAX(resp) MMC_RSP_BITS((resp), 56, 3)
  183. #define SD_CSD_VDD_W_CURR_MIN(resp) MMC_RSP_BITS((resp), 53, 3)
  184. #define SD_CSD_VDD_W_CURR_MAX(resp) MMC_RSP_BITS((resp), 50, 3)
  185. #define SD_CSD_VDD_RW_CURR_100mA 0x7
  186. #define SD_CSD_VDD_RW_CURR_80mA 0x6
  187. #define SD_CSD_C_SIZE_MULT(resp) MMC_RSP_BITS((resp), 47, 3)
  188. #define SD_CSD_ERASE_BLK_EN(resp) MMC_RSP_BITS((resp), 46, 1)
  189. #define SD_CSD_SECTOR_SIZE(resp) MMC_RSP_BITS((resp), 39, 7) /* +1 */
  190. #define SD_CSD_WP_GRP_SIZE(resp) MMC_RSP_BITS((resp), 32, 7) /* +1 */
  191. #define SD_CSD_WP_GRP_ENABLE(resp) MMC_RSP_BITS((resp), 31, 1)
  192. #define SD_CSD_R2W_FACTOR(resp) MMC_RSP_BITS((resp), 26, 3)
  193. #define SD_CSD_WRITE_BL_LEN(resp) MMC_RSP_BITS((resp), 22, 4)
  194. #define SD_CSD_RW_BL_LEN_2G 0xa
  195. #define SD_CSD_RW_BL_LEN_1G 0x9
  196. #define SD_CSD_WRITE_BL_PARTIAL(resp) MMC_RSP_BITS((resp), 21, 1)
  197. #define SD_CSD_FILE_FORMAT_GRP(resp) MMC_RSP_BITS((resp), 15, 1)
  198. #define SD_CSD_COPY(resp) MMC_RSP_BITS((resp), 14, 1)
  199. #define SD_CSD_PERM_WRITE_PROTECT(resp) MMC_RSP_BITS((resp), 13, 1)
  200. #define SD_CSD_TMP_WRITE_PROTECT(resp) MMC_RSP_BITS((resp), 12, 1)
  201. #define SD_CSD_FILE_FORMAT(resp) MMC_RSP_BITS((resp), 10, 2)
  202. /* SD R2 response (CID) */
  203. #define SD_CID_MID(resp) MMC_RSP_BITS((resp), 120, 8)
  204. #define SD_CID_OID(resp) MMC_RSP_BITS((resp), 104, 16)
  205. #define SD_CID_PNM_CPY(resp, pnm) \
  206. do { \
  207. (pnm)[0] = MMC_RSP_BITS((resp), 96, 8); \
  208. (pnm)[1] = MMC_RSP_BITS((resp), 88, 8); \
  209. (pnm)[2] = MMC_RSP_BITS((resp), 80, 8); \
  210. (pnm)[3] = MMC_RSP_BITS((resp), 72, 8); \
  211. (pnm)[4] = MMC_RSP_BITS((resp), 64, 8); \
  212. (pnm)[5] = '\0'; \
  213. } while (0)
  214. #define SD_CID_REV(resp) MMC_RSP_BITS((resp), 56, 8)
  215. #define SD_CID_PSN(resp) MMC_RSP_BITS((resp), 24, 32)
  216. #define SD_CID_MDT(resp) MMC_RSP_BITS((resp), 8, 12)
  217. /* Might be slow, but it should work on big and little endian systems. */
  218. #define MMC_RSP_BITS(resp, start, len) __bitfield((resp), (start)-8, (len))
  219. static __inline int
  220. __bitfield(u_int32_t *src, int start, int len)
  221. {
  222. u_int8_t *sp;
  223. u_int32_t dst, mask;
  224. int shift, bs, bc;
  225. if (start < 0 || len < 0 || len > 32)
  226. return 0;
  227. dst = 0;
  228. mask = len % 32 ? UINT_MAX >> (32 - (len % 32)) : UINT_MAX;
  229. shift = 0;
  230. while (len > 0) {
  231. sp = (u_int8_t *)src + start / 8;
  232. bs = start % 8;
  233. bc = 8 - bs;
  234. if (bc > len)
  235. bc = len;
  236. dst |= (*sp++ >> bs) << shift;
  237. shift += bc;
  238. start += bc;
  239. len -= bc;
  240. }
  241. dst &= mask;
  242. return (int)dst;
  243. }
  244. #endif