sd_protocol_defs.h 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537
  1. /*
  2. * SPDX-FileCopyrightText: 2006 Uwe Stuehler <uwe@openbsd.org>
  3. *
  4. * SPDX-License-Identifier: ISC
  5. *
  6. * SPDX-FileContributor: 2016-2023 Espressif Systems (Shanghai) CO LTD
  7. */
  8. /*
  9. * Copyright (c) 2006 Uwe Stuehler <uwe@openbsd.org>
  10. *
  11. * Permission to use, copy, modify, and distribute this software for any
  12. * purpose with or without fee is hereby granted, provided that the above
  13. * copyright notice and this permission notice appear in all copies.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  16. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  17. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  18. * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  19. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  20. * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  21. * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  22. */
  23. #pragma once
  24. #include <stdint.h>
  25. #include <limits.h>
  26. #ifdef __cplusplus
  27. extern "C" {
  28. #endif
  29. /* MMC commands */ /* response type */
  30. #define MMC_GO_IDLE_STATE 0 /* R0 */
  31. #define MMC_SEND_OP_COND 1 /* R3 */
  32. #define MMC_ALL_SEND_CID 2 /* R2 */
  33. #define MMC_SET_RELATIVE_ADDR 3 /* R1 */
  34. #define MMC_SWITCH 6 /* R1B */
  35. #define MMC_SELECT_CARD 7 /* R1 */
  36. #define MMC_SEND_EXT_CSD 8 /* R1 */
  37. #define MMC_SEND_CSD 9 /* R2 */
  38. #define MMC_SEND_CID 10 /* R1 */
  39. #define MMC_READ_DAT_UNTIL_STOP 11 /* R1 */
  40. #define MMC_STOP_TRANSMISSION 12 /* R1B */
  41. #define MMC_SEND_STATUS 13 /* R1 */
  42. #define MMC_SET_BLOCKLEN 16 /* R1 */
  43. #define MMC_READ_BLOCK_SINGLE 17 /* R1 */
  44. #define MMC_READ_BLOCK_MULTIPLE 18 /* R1 */
  45. #define MMC_WRITE_DAT_UNTIL_STOP 20 /* R1 */
  46. #define MMC_SET_BLOCK_COUNT 23 /* R1 */
  47. #define MMC_WRITE_BLOCK_SINGLE 24 /* R1 */
  48. #define MMC_WRITE_BLOCK_MULTIPLE 25 /* R1 */
  49. #define MMC_ERASE_GROUP_START 35 /* R1 */
  50. #define MMC_ERASE_GROUP_END 36 /* R1 */
  51. #define MMC_ERASE 38 /* R1B */
  52. #define MMC_APP_CMD 55 /* R1 */
  53. /* SD commands */ /* response type */
  54. #define SD_SEND_RELATIVE_ADDR 3 /* R6 */
  55. #define SD_SEND_SWITCH_FUNC 6 /* R1 */
  56. #define SD_SEND_IF_COND 8 /* R7 */
  57. #define SD_ERASE_GROUP_START 32 /* R1 */
  58. #define SD_ERASE_GROUP_END 33 /* R1 */
  59. #define SD_READ_OCR 58 /* R3 */
  60. #define SD_CRC_ON_OFF 59 /* R1 */
  61. /* SD application commands */ /* response type */
  62. #define SD_APP_SET_BUS_WIDTH 6 /* R1 */
  63. #define SD_APP_SD_STATUS 13 /* R2 */
  64. #define SD_APP_OP_COND 41 /* R3 */
  65. #define SD_APP_SEND_SCR 51 /* R1 */
  66. /* SD IO commands */
  67. #define SD_IO_SEND_OP_COND 5 /* R4 */
  68. #define SD_IO_RW_DIRECT 52 /* R5 */
  69. #define SD_IO_RW_EXTENDED 53 /* R5 */
  70. /* OCR bits */
  71. #define MMC_OCR_MEM_READY (1<<31) /* memory power-up status bit */
  72. #define MMC_OCR_ACCESS_MODE_MASK 0x60000000 /* bits 30:29 */
  73. #define MMC_OCR_SECTOR_MODE (1<<30)
  74. #define MMC_OCR_BYTE_MODE (1<<29)
  75. #define MMC_OCR_3_5V_3_6V (1<<23)
  76. #define MMC_OCR_3_4V_3_5V (1<<22)
  77. #define MMC_OCR_3_3V_3_4V (1<<21)
  78. #define MMC_OCR_3_2V_3_3V (1<<20)
  79. #define MMC_OCR_3_1V_3_2V (1<<19)
  80. #define MMC_OCR_3_0V_3_1V (1<<18)
  81. #define MMC_OCR_2_9V_3_0V (1<<17)
  82. #define MMC_OCR_2_8V_2_9V (1<<16)
  83. #define MMC_OCR_2_7V_2_8V (1<<15)
  84. #define MMC_OCR_2_6V_2_7V (1<<14)
  85. #define MMC_OCR_2_5V_2_6V (1<<13)
  86. #define MMC_OCR_2_4V_2_5V (1<<12)
  87. #define MMC_OCR_2_3V_2_4V (1<<11)
  88. #define MMC_OCR_2_2V_2_3V (1<<10)
  89. #define MMC_OCR_2_1V_2_2V (1<<9)
  90. #define MMC_OCR_2_0V_2_1V (1<<8)
  91. #define MMC_OCR_1_65V_1_95V (1<<7)
  92. #define SD_OCR_SDHC_CAP (1<<30)
  93. #define SD_OCR_VOL_MASK 0xFF8000 /* bits 23:15 */
  94. /* SD mode R1 response type bits */
  95. #define MMC_R1_READY_FOR_DATA (1<<8) /* ready for next transfer */
  96. #define MMC_R1_APP_CMD (1<<5) /* app. commands supported */
  97. #define MMC_R1_SWITCH_ERROR (1<<7) /* switch command did not succeed */
  98. #define MMC_R1_CURRENT_STATE_POS (9)
  99. #define MMC_R1_CURRENT_STATE_MASK (0x1E00)/* card current state */
  100. #define MMC_R1_CURRENT_STATE_TRAN (4)
  101. /* SPI mode R1 response type bits */
  102. #define SD_SPI_R1_IDLE_STATE (1<<0)
  103. #define SD_SPI_R1_ERASE_RST (1<<1)
  104. #define SD_SPI_R1_ILLEGAL_CMD (1<<2)
  105. #define SD_SPI_R1_CMD_CRC_ERR (1<<3)
  106. #define SD_SPI_R1_ERASE_SEQ_ERR (1<<4)
  107. #define SD_SPI_R1_ADDR_ERR (1<<5)
  108. #define SD_SPI_R1_PARAM_ERR (1<<6)
  109. #define SD_SPI_R1_NO_RESPONSE (1<<7)
  110. #define SDIO_R1_FUNC_NUM_ERR (1<<4)
  111. /* SPI mode R2 response type bits.
  112. * The first byte is the same as for R1.
  113. * The bits below belong to the second byte.
  114. * Bits 10, 11, 12, 15 can also be reported in data error token of a read command response.
  115. */
  116. #define SD_SPI_R2_CARD_LOCKED (1<<8) /* Set when the card is locked by the user */
  117. #define SD_SPI_R2_UNLOCK_FAILED (1<<9) /* Host attempts to erase a write-protected sector or makes an error during card lock/unlock operation */
  118. #define SD_SPI_R2_ERROR (1<<10) /* A general or an unknown error occurred during the operation */
  119. #define SD_SPI_R2_CC_ERROR (1<<11) /* Internal card controller error */
  120. #define SD_SPI_R2_ECC_FAILED (1<<12) /* Card internal ECC was applied but failed to correct the data */
  121. #define SD_SPI_R2_WP_VIOLATION (1<<13) /* The command tried to write a write-protected block */
  122. #define SD_SPI_R2_ERASE_PARAM (1<<14) /* An invalid selection for erase, sectors or groups */
  123. #define SD_SPI_R2_OUT_OF_RANGE (1<<15) /* The command argument was out of the allowed range for this card */
  124. /* 48-bit response decoding (32 bits w/o CRC) */
  125. #define MMC_R1(resp) ((resp)[0])
  126. #define MMC_R3(resp) ((resp)[0])
  127. #define MMC_R4(resp) ((resp)[0])
  128. #define MMC_R5(resp) ((resp)[0])
  129. #define SD_R6(resp) ((resp)[0])
  130. #define MMC_R1_CURRENT_STATE(resp) (((resp)[0] >> 9) & 0xf)
  131. /* SPI mode response decoding */
  132. #define SD_SPI_R1(resp) ((resp)[0] & 0xff)
  133. #define SD_SPI_R2(resp) ((resp)[0] & 0xffff)
  134. #define SD_SPI_R3(resp) ((resp)[0])
  135. #define SD_SPI_R7(resp) ((resp)[0])
  136. /* SPI mode data response decoding */
  137. #define SD_SPI_DATA_RSP_VALID(resp_byte) (((resp_byte)&0x11)==0x1)
  138. #define SD_SPI_DATA_RSP(resp_byte) (((resp_byte)>>1)&0x7)
  139. #define SD_SPI_DATA_ACCEPTED 0x2
  140. #define SD_SPI_DATA_CRC_ERROR 0x5
  141. #define SD_SPI_DATA_WR_ERROR 0x6
  142. /* RCA argument and response */
  143. #define MMC_ARG_RCA(rca) ((rca) << 16)
  144. #define SD_R6_RCA(resp) (SD_R6((resp)) >> 16)
  145. /* bus width argument */
  146. #define SD_ARG_BUS_WIDTH_1 0
  147. #define SD_ARG_BUS_WIDTH_4 2
  148. /* EXT_CSD fields */
  149. #define EXT_CSD_SANITIZE_START 165 /* WO */
  150. #define EXT_CSD_ERASED_MEM_CONT 181 /* RO */
  151. #define EXT_CSD_BUS_WIDTH 183 /* WO */
  152. #define EXT_CSD_HS_TIMING 185 /* R/W */
  153. #define EXT_CSD_POWER_CLASS 187 /* R/W */
  154. #define EXT_CSD_CMD_SET 191 /* R/W */
  155. #define EXT_CSD_REV 192 /* RO */
  156. #define EXT_CSD_STRUCTURE 194 /* RO */
  157. #define EXT_CSD_CARD_TYPE 196 /* RO */
  158. #define EXT_CSD_PWR_CL_52_195 200 /* RO */
  159. #define EXT_CSD_PWR_CL_26_195 201 /* RO */
  160. #define EXT_CSD_PWR_CL_52_360 202 /* RO */
  161. #define EXT_CSD_PWR_CL_26_360 203 /* RO */
  162. #define EXT_CSD_SEC_COUNT 212 /* RO */
  163. #define EXT_CSD_SEC_FEATURE_SUPPORT 231 /* RO */
  164. #define EXT_CSD_S_CMD_SET 504 /* RO */
  165. /* EXT_CSD field definitions */
  166. #define EXT_CSD_REV_1_6 6 /* Revision 1.6 (for MMC v4.5, v4.51) */
  167. #define EXT_CSD_CMD_SET_NORMAL (1U << 0)
  168. #define EXT_CSD_CMD_SET_SECURE (1U << 1)
  169. #define EXT_CSD_CMD_SET_CPSECURE (1U << 2)
  170. /* EXT_CSD_HS_TIMING */
  171. #define EXT_CSD_HS_TIMING_BC 0
  172. #define EXT_CSD_HS_TIMING_HS 1
  173. #define EXT_CSD_HS_TIMING_HS200 2
  174. #define EXT_CSD_HS_TIMING_HS400 3
  175. /* EXT_CSD_BUS_WIDTH */
  176. #define EXT_CSD_BUS_WIDTH_1 0
  177. #define EXT_CSD_BUS_WIDTH_4 1
  178. #define EXT_CSD_BUS_WIDTH_8 2
  179. #define EXT_CSD_BUS_WIDTH_4_DDR 5
  180. #define EXT_CSD_BUS_WIDTH_8_DDR 6
  181. /* EXT_CSD_CARD_TYPE */
  182. /* The only currently valid values for this field are 0x01, 0x03, 0x07,
  183. * 0x0B and 0x0F. */
  184. #define EXT_CSD_CARD_TYPE_F_26M (1 << 0) /* SDR at "rated voltages */
  185. #define EXT_CSD_CARD_TYPE_F_52M (1 << 1) /* SDR at "rated voltages */
  186. #define EXT_CSD_CARD_TYPE_F_52M_1_8V (1 << 2) /* DDR, 1.8V or 3.3V I/O */
  187. #define EXT_CSD_CARD_TYPE_F_52M_1_2V (1 << 3) /* DDR, 1.2V I/O */
  188. #define EXT_CSD_CARD_TYPE_26M 0x01
  189. #define EXT_CSD_CARD_TYPE_52M 0x03
  190. #define EXT_CSD_CARD_TYPE_52M_V18 0x07
  191. #define EXT_CSD_CARD_TYPE_52M_V12 0x0b
  192. #define EXT_CSD_CARD_TYPE_52M_V12_18 0x0f
  193. /* EXT_CSD_SEC_FEATURE_SUPPORT */
  194. #define EXT_CSD_SECURE_ER_EN (uint8_t)(1 << 0)
  195. #define EXT_CSD_SEC_BD_BLK_EN (uint8_t)(1 << 2)
  196. #define EXT_CSD_SEC_GB_CL_EN (uint8_t)(1 << 4)
  197. #define EXT_CSD_SEC_SANITIZE (uint8_t)(1 << 6)
  198. /* EXT_CSD MMC */
  199. #define EXT_CSD_MMC_SIZE 512
  200. /* MMC_SWITCH access mode */
  201. #define MMC_SWITCH_MODE_CMD_SET 0x00 /* Change the command set */
  202. #define MMC_SWITCH_MODE_SET_BITS 0x01 /* Set bits in value */
  203. #define MMC_SWITCH_MODE_CLEAR_BITS 0x02 /* Clear bits in value */
  204. #define MMC_SWITCH_MODE_WRITE_BYTE 0x03 /* Set target to value */
  205. /* MMC R2 response (CSD) */
  206. #define MMC_CSD_CSDVER(resp) MMC_RSP_BITS((resp), 126, 2)
  207. #define MMC_CSD_CSDVER_1_0 1
  208. #define MMC_CSD_CSDVER_2_0 2
  209. #define MMC_CSD_CSDVER_EXT_CSD 3
  210. #define MMC_CSD_MMCVER(resp) MMC_RSP_BITS((resp), 122, 4)
  211. #define MMC_CSD_MMCVER_1_0 0 /* MMC 1.0 - 1.2 */
  212. #define MMC_CSD_MMCVER_1_4 1 /* MMC 1.4 */
  213. #define MMC_CSD_MMCVER_2_0 2 /* MMC 2.0 - 2.2 */
  214. #define MMC_CSD_MMCVER_3_1 3 /* MMC 3.1 - 3.3 */
  215. #define MMC_CSD_MMCVER_4_0 4 /* MMC 4 */
  216. #define MMC_CSD_READ_BL_LEN(resp) MMC_RSP_BITS((resp), 80, 4)
  217. #define MMC_CSD_C_SIZE(resp) MMC_RSP_BITS((resp), 62, 12)
  218. #define MMC_CSD_CAPACITY(resp) ((MMC_CSD_C_SIZE((resp))+1) << \
  219. (MMC_CSD_C_SIZE_MULT((resp))+2))
  220. #define MMC_CSD_C_SIZE_MULT(resp) MMC_RSP_BITS((resp), 47, 3)
  221. /* MMC v1 R2 response (CID) */
  222. #define MMC_CID_MID_V1(resp) MMC_RSP_BITS((resp), 104, 24)
  223. #define MMC_CID_PNM_V1_CPY(resp, pnm) \
  224. do { \
  225. (pnm)[0] = MMC_RSP_BITS((resp), 96, 8); \
  226. (pnm)[1] = MMC_RSP_BITS((resp), 88, 8); \
  227. (pnm)[2] = MMC_RSP_BITS((resp), 80, 8); \
  228. (pnm)[3] = MMC_RSP_BITS((resp), 72, 8); \
  229. (pnm)[4] = MMC_RSP_BITS((resp), 64, 8); \
  230. (pnm)[5] = MMC_RSP_BITS((resp), 56, 8); \
  231. (pnm)[6] = MMC_RSP_BITS((resp), 48, 8); \
  232. (pnm)[7] = '\0'; \
  233. } while (0)
  234. #define MMC_CID_REV_V1(resp) MMC_RSP_BITS((resp), 40, 8)
  235. #define MMC_CID_PSN_V1(resp) MMC_RSP_BITS((resp), 16, 24)
  236. #define MMC_CID_MDT_V1(resp) MMC_RSP_BITS((resp), 8, 8)
  237. /* MMC v2 R2 response (CID) */
  238. #define MMC_CID_MID_V2(resp) MMC_RSP_BITS((resp), 120, 8)
  239. #define MMC_CID_OID_V2(resp) MMC_RSP_BITS((resp), 104, 16)
  240. #define MMC_CID_PNM_V2_CPY(resp, pnm) \
  241. do { \
  242. (pnm)[0] = MMC_RSP_BITS((resp), 96, 8); \
  243. (pnm)[1] = MMC_RSP_BITS((resp), 88, 8); \
  244. (pnm)[2] = MMC_RSP_BITS((resp), 80, 8); \
  245. (pnm)[3] = MMC_RSP_BITS((resp), 72, 8); \
  246. (pnm)[4] = MMC_RSP_BITS((resp), 64, 8); \
  247. (pnm)[5] = MMC_RSP_BITS((resp), 56, 8); \
  248. (pnm)[6] = '\0'; \
  249. } while (0)
  250. #define MMC_CID_PSN_V2(resp) MMC_RSP_BITS((resp), 16, 32)
  251. /* SD R2 response (CSD) */
  252. #define SD_CSD_CSDVER(resp) MMC_RSP_BITS((resp), 126, 2)
  253. #define SD_CSD_CSDVER_1_0 0
  254. #define SD_CSD_CSDVER_2_0 1
  255. #define SD_CSD_TAAC(resp) MMC_RSP_BITS((resp), 112, 8)
  256. #define SD_CSD_TAAC_1_5_MSEC 0x26
  257. #define SD_CSD_NSAC(resp) MMC_RSP_BITS((resp), 104, 8)
  258. #define SD_CSD_SPEED(resp) MMC_RSP_BITS((resp), 96, 8)
  259. #define SD_CSD_SPEED_25_MHZ 0x32
  260. #define SD_CSD_SPEED_50_MHZ 0x5a
  261. #define SD_CSD_CCC(resp) MMC_RSP_BITS((resp), 84, 12)
  262. #define SD_CSD_CCC_BASIC (1 << 0) /* basic */
  263. #define SD_CSD_CCC_BR (1 << 2) /* block read */
  264. #define SD_CSD_CCC_BW (1 << 4) /* block write */
  265. #define SD_CSD_CCC_ERASE (1 << 5) /* erase */
  266. #define SD_CSD_CCC_WP (1 << 6) /* write protection */
  267. #define SD_CSD_CCC_LC (1 << 7) /* lock card */
  268. #define SD_CSD_CCC_AS (1 << 8) /*application specific*/
  269. #define SD_CSD_CCC_IOM (1 << 9) /* I/O mode */
  270. #define SD_CSD_CCC_SWITCH (1 << 10) /* switch */
  271. #define SD_CSD_READ_BL_LEN(resp) MMC_RSP_BITS((resp), 80, 4)
  272. #define SD_CSD_READ_BL_PARTIAL(resp) MMC_RSP_BITS((resp), 79, 1)
  273. #define SD_CSD_WRITE_BLK_MISALIGN(resp) MMC_RSP_BITS((resp), 78, 1)
  274. #define SD_CSD_READ_BLK_MISALIGN(resp) MMC_RSP_BITS((resp), 77, 1)
  275. #define SD_CSD_DSR_IMP(resp) MMC_RSP_BITS((resp), 76, 1)
  276. #define SD_CSD_C_SIZE(resp) MMC_RSP_BITS((resp), 62, 12)
  277. #define SD_CSD_CAPACITY(resp) ((SD_CSD_C_SIZE((resp))+1) << \
  278. (SD_CSD_C_SIZE_MULT((resp))+2))
  279. #define SD_CSD_V2_C_SIZE(resp) MMC_RSP_BITS((resp), 48, 22)
  280. #define SD_CSD_V2_CAPACITY(resp) ((SD_CSD_V2_C_SIZE((resp))+1) << 10)
  281. #define SD_CSD_V2_BL_LEN 0x9 /* 512 */
  282. #define SD_CSD_VDD_R_CURR_MIN(resp) MMC_RSP_BITS((resp), 59, 3)
  283. #define SD_CSD_VDD_R_CURR_MAX(resp) MMC_RSP_BITS((resp), 56, 3)
  284. #define SD_CSD_VDD_W_CURR_MIN(resp) MMC_RSP_BITS((resp), 53, 3)
  285. #define SD_CSD_VDD_W_CURR_MAX(resp) MMC_RSP_BITS((resp), 50, 3)
  286. #define SD_CSD_VDD_RW_CURR_100mA 0x7
  287. #define SD_CSD_VDD_RW_CURR_80mA 0x6
  288. #define SD_CSD_C_SIZE_MULT(resp) MMC_RSP_BITS((resp), 47, 3)
  289. #define SD_CSD_ERASE_BLK_EN(resp) MMC_RSP_BITS((resp), 46, 1)
  290. #define SD_CSD_SECTOR_SIZE(resp) MMC_RSP_BITS((resp), 39, 7) /* +1 */
  291. #define SD_CSD_WP_GRP_SIZE(resp) MMC_RSP_BITS((resp), 32, 7) /* +1 */
  292. #define SD_CSD_WP_GRP_ENABLE(resp) MMC_RSP_BITS((resp), 31, 1)
  293. #define SD_CSD_R2W_FACTOR(resp) MMC_RSP_BITS((resp), 26, 3)
  294. #define SD_CSD_WRITE_BL_LEN(resp) MMC_RSP_BITS((resp), 22, 4)
  295. #define SD_CSD_RW_BL_LEN_2G 0xa
  296. #define SD_CSD_RW_BL_LEN_1G 0x9
  297. #define SD_CSD_WRITE_BL_PARTIAL(resp) MMC_RSP_BITS((resp), 21, 1)
  298. #define SD_CSD_FILE_FORMAT_GRP(resp) MMC_RSP_BITS((resp), 15, 1)
  299. #define SD_CSD_COPY(resp) MMC_RSP_BITS((resp), 14, 1)
  300. #define SD_CSD_PERM_WRITE_PROTECT(resp) MMC_RSP_BITS((resp), 13, 1)
  301. #define SD_CSD_TMP_WRITE_PROTECT(resp) MMC_RSP_BITS((resp), 12, 1)
  302. #define SD_CSD_FILE_FORMAT(resp) MMC_RSP_BITS((resp), 10, 2)
  303. /* SD R2 response (CID) */
  304. #define SD_CID_MID(resp) MMC_RSP_BITS((resp), 120, 8)
  305. #define SD_CID_OID(resp) MMC_RSP_BITS((resp), 104, 16)
  306. #define SD_CID_PNM_CPY(resp, pnm) \
  307. do { \
  308. (pnm)[0] = MMC_RSP_BITS((resp), 96, 8); \
  309. (pnm)[1] = MMC_RSP_BITS((resp), 88, 8); \
  310. (pnm)[2] = MMC_RSP_BITS((resp), 80, 8); \
  311. (pnm)[3] = MMC_RSP_BITS((resp), 72, 8); \
  312. (pnm)[4] = MMC_RSP_BITS((resp), 64, 8); \
  313. (pnm)[5] = '\0'; \
  314. } while (0)
  315. #define SD_CID_REV(resp) MMC_RSP_BITS((resp), 56, 8)
  316. #define SD_CID_PSN(resp) MMC_RSP_BITS((resp), 24, 32)
  317. #define SD_CID_MDT(resp) MMC_RSP_BITS((resp), 8, 12)
  318. /* SCR (SD Configuration Register) */
  319. #define SCR_STRUCTURE(scr) MMC_RSP_BITS((scr), 60, 4)
  320. #define SCR_STRUCTURE_VER_1_0 0 /* Version 1.0 */
  321. #define SCR_SD_SPEC(scr) MMC_RSP_BITS((scr), 56, 4)
  322. #define SCR_SD_SPEC_VER_1_0 0 /* Version 1.0 and 1.01 */
  323. #define SCR_SD_SPEC_VER_1_10 1 /* Version 1.10 */
  324. #define SCR_SD_SPEC_VER_2 2 /* Version 2.00 or Version 3.0X */
  325. #define SCR_DATA_STAT_AFTER_ERASE(scr) MMC_RSP_BITS((scr), 55, 1)
  326. #define SCR_SD_SECURITY(scr) MMC_RSP_BITS((scr), 52, 3)
  327. #define SCR_SD_SECURITY_NONE 0 /* no security */
  328. #define SCR_SD_SECURITY_1_0 1 /* security protocol 1.0 */
  329. #define SCR_SD_SECURITY_1_0_2 2 /* security protocol 1.0 */
  330. #define SCR_SD_BUS_WIDTHS(scr) MMC_RSP_BITS((scr), 48, 4)
  331. #define SCR_SD_BUS_WIDTHS_1BIT (1 << 0) /* 1bit (DAT0) */
  332. #define SCR_SD_BUS_WIDTHS_4BIT (1 << 2) /* 4bit (DAT0-3) */
  333. #define SCR_SD_SPEC3(scr) MMC_RSP_BITS((scr), 47, 1)
  334. #define SCR_EX_SECURITY(scr) MMC_RSP_BITS((scr), 43, 4)
  335. #define SCR_SD_SPEC4(scr) MMC_RSP_BITS((scr), 42, 1)
  336. #define SCR_RESERVED(scr) MMC_RSP_BITS((scr), 34, 8)
  337. #define SCR_CMD_SUPPORT_CMD23(scr) MMC_RSP_BITS((scr), 33, 1)
  338. #define SCR_CMD_SUPPORT_CMD20(scr) MMC_RSP_BITS((scr), 32, 1)
  339. #define SCR_RESERVED2(scr) MMC_RSP_BITS((scr), 0, 32)
  340. /* SSR (SD Status Register) */
  341. #define SSR_DAT_BUS_WIDTH(ssr) MMC_RSP_BITS((ssr), 510, 2)
  342. #define SSR_AU_SIZE(ssr) MMC_RSP_BITS((ssr), 428, 4)
  343. #define SSR_ERASE_SIZE(ssr) MMC_RSP_BITS((ssr), 408, 16)
  344. #define SSR_ERASE_TIMEOUT(ssr) MMC_RSP_BITS((ssr), 402, 6)
  345. #define SSR_ERASE_OFFSET(ssr) MMC_RSP_BITS((ssr), 400, 2)
  346. #define SSR_DISCARD_SUPPORT(ssr) MMC_RSP_BITS((ssr), 313, 1)
  347. #define SSR_FULE_SUPPORT(ssr) MMC_RSP_BITS((ssr), 312, 1)
  348. /* Max supply current in SWITCH_FUNC response (in mA) */
  349. #define SD_SFUNC_I_MAX(status) (MMC_RSP_BITS((uint32_t *)(status), 496, 16))
  350. /* Supported flags in SWITCH_FUNC response */
  351. #define SD_SFUNC_SUPPORTED(status, group) \
  352. (MMC_RSP_BITS((uint32_t *)(status), 400 + (group - 1) * 16, 16))
  353. /* Selected function in SWITCH_FUNC response */
  354. #define SD_SFUNC_SELECTED(status, group) \
  355. (MMC_RSP_BITS((uint32_t *)(status), 376 + (group - 1) * 4, 4))
  356. /* Busy flags in SWITCH_FUNC response */
  357. #define SD_SFUNC_BUSY(status, group) \
  358. (MMC_RSP_BITS((uint32_t *)(status), 272 + (group - 1) * 16, 16))
  359. /* Version of SWITCH_FUNC response */
  360. #define SD_SFUNC_VER(status) (MMC_RSP_BITS((uint32_t *)(status), 368, 8))
  361. #define SD_SFUNC_GROUP_MAX 6
  362. #define SD_SFUNC_FUNC_MAX 15
  363. #define SD_ACCESS_MODE 1 /* Function group 1, Access Mode */
  364. #define SD_ACCESS_MODE_SDR12 0 /* 25 MHz clock */
  365. #define SD_ACCESS_MODE_SDR25 1 /* 50 MHz clock */
  366. #define SD_ACCESS_MODE_SDR50 2 /* UHS-I, 100 MHz clock */
  367. #define SD_ACCESS_MODE_SDR104 3 /* UHS-I, 208 MHz clock */
  368. #define SD_ACCESS_MODE_DDR50 4 /* UHS-I, 50 MHz clock, DDR */
  369. #define SD_SSR_SIZE 64 /* SD status register */
  370. /**
  371. * @brief Extract up to 32 sequential bits from an array of 32-bit words
  372. *
  373. * Bits within the word are numbered in the increasing order from LSB to MSB.
  374. *
  375. * As an example, consider 2 32-bit words:
  376. *
  377. * 0x01234567 0x89abcdef
  378. *
  379. * On a little-endian system, the bytes are stored in memory as follows:
  380. *
  381. * 67 45 23 01 ef cd ab 89
  382. *
  383. * MMC_RSP_BITS will extact bits as follows:
  384. *
  385. * start=0 len=4 -> result=0x00000007
  386. * start=0 len=12 -> result=0x00000567
  387. * start=28 len=8 -> result=0x000000f0
  388. * start=59 len=5 -> result=0x00000011
  389. *
  390. * @param src array of words to extract bits from
  391. * @param start index of the first bit to extract
  392. * @param len number of bits to extract, 1 to 32
  393. * @return 32-bit word where requested bits start from LSB
  394. */
  395. static inline uint32_t MMC_RSP_BITS(uint32_t *src, int start, int len)
  396. {
  397. uint32_t mask = (len % 32 == 0) ? UINT_MAX : UINT_MAX >> (32 - (len % 32));
  398. size_t word = start / 32;
  399. size_t shift = start % 32;
  400. uint32_t right = src[word] >> shift;
  401. uint32_t left = (len + shift <= 32) ? 0 : src[word + 1] << ((32 - shift) % 32);
  402. return (left | right) & mask;
  403. }
  404. /* SD R4 response (IO OCR) */
  405. #define SD_IO_OCR_MEM_READY (1<<31)
  406. #define SD_IO_OCR_NUM_FUNCTIONS(ocr) (((ocr) >> 28) & 0x7)
  407. #define SD_IO_OCR_MEM_PRESENT (1<<27)
  408. #define SD_IO_OCR_MASK 0x00fffff0
  409. /* CMD52 arguments */
  410. #define SD_ARG_CMD52_READ (0<<31)
  411. #define SD_ARG_CMD52_WRITE (1<<31)
  412. #define SD_ARG_CMD52_FUNC_SHIFT 28
  413. #define SD_ARG_CMD52_FUNC_MASK 0x7
  414. #define SD_ARG_CMD52_EXCHANGE (1<<27)
  415. #define SD_ARG_CMD52_REG_SHIFT 9
  416. #define SD_ARG_CMD52_REG_MASK 0x1ffff
  417. #define SD_ARG_CMD52_DATA_SHIFT 0
  418. #define SD_ARG_CMD52_DATA_MASK 0xff
  419. #define SD_R5_DATA(resp) ((resp)[0] & 0xff)
  420. /* CMD53 arguments */
  421. #define SD_ARG_CMD53_READ (0<<31)
  422. #define SD_ARG_CMD53_WRITE (1<<31)
  423. #define SD_ARG_CMD53_FUNC_SHIFT 28
  424. #define SD_ARG_CMD53_FUNC_MASK 0x7
  425. #define SD_ARG_CMD53_BLOCK_MODE (1<<27)
  426. #define SD_ARG_CMD53_INCREMENT (1<<26)
  427. #define SD_ARG_CMD53_REG_SHIFT 9
  428. #define SD_ARG_CMD53_REG_MASK 0x1ffff
  429. #define SD_ARG_CMD53_LENGTH_SHIFT 0
  430. #define SD_ARG_CMD53_LENGTH_MASK 0x1ff
  431. #define SD_ARG_CMD53_LENGTH_MAX 512
  432. /* Card Common Control Registers (CCCR) */
  433. #define SD_IO_CCCR_START 0x00000
  434. #define SD_IO_CCCR_SIZE 0x100
  435. #define SD_IO_CCCR_FN_ENABLE 0x02
  436. #define SD_IO_CCCR_FN_READY 0x03
  437. #define SD_IO_CCCR_INT_ENABLE 0x04
  438. #define SD_IO_CCCR_INT_PENDING 0x05
  439. #define SD_IO_CCCR_CTL 0x06
  440. #define CCCR_CTL_RES (1<<3)
  441. #define SD_IO_CCCR_BUS_WIDTH 0x07
  442. #define CCCR_BUS_WIDTH_1 (0<<0)
  443. #define CCCR_BUS_WIDTH_4 (2<<0)
  444. #define CCCR_BUS_WIDTH_8 (3<<0)
  445. #define CCCR_BUS_WIDTH_ECSI (1<<5)
  446. #define SD_IO_CCCR_CARD_CAP 0x08
  447. #define CCCR_CARD_CAP_LSC BIT(6)
  448. #define CCCR_CARD_CAP_4BLS BIT(7)
  449. #define SD_IO_CCCR_CISPTR 0x09
  450. #define SD_IO_CCCR_BLKSIZEL 0x10
  451. #define SD_IO_CCCR_BLKSIZEH 0x11
  452. #define SD_IO_CCCR_HIGHSPEED 0x13
  453. #define CCCR_HIGHSPEED_SUPPORT BIT(0)
  454. #define CCCR_HIGHSPEED_ENABLE BIT(1)
  455. /* Function Basic Registers (FBR) */
  456. #define SD_IO_FBR_START 0x00100
  457. #define SD_IO_FBR_SIZE 0x00700
  458. /* Card Information Structure (CIS) */
  459. #define SD_IO_CIS_START 0x01000
  460. #define SD_IO_CIS_SIZE 0x17000
  461. /* CIS tuple codes (based on PC Card 16) */
  462. #define CISTPL_CODE_NULL 0x00
  463. #define CISTPL_CODE_DEVICE 0x01
  464. #define CISTPL_CODE_CHKSUM 0x10
  465. #define CISTPL_CODE_VERS1 0x15
  466. #define CISTPL_CODE_ALTSTR 0x16
  467. #define CISTPL_CODE_CONFIG 0x1A
  468. #define CISTPL_CODE_CFTABLE_ENTRY 0x1B
  469. #define CISTPL_CODE_MANFID 0x20
  470. #define CISTPL_CODE_FUNCID 0x21
  471. #define TPLFID_FUNCTION_SDIO 0x0c
  472. #define CISTPL_CODE_FUNCE 0x22
  473. #define CISTPL_CODE_VENDER_BEGIN 0x80
  474. #define CISTPL_CODE_VENDER_END 0x8F
  475. #define CISTPL_CODE_SDIO_STD 0x91
  476. #define CISTPL_CODE_SDIO_EXT 0x92
  477. #define CISTPL_CODE_END 0xFF
  478. /* Timing */
  479. #define SDMMC_TIMING_LEGACY 0
  480. #define SDMMC_TIMING_HIGHSPEED 1
  481. #define SDMMC_TIMING_MMC_DDR52 2
  482. #ifdef __cplusplus
  483. }
  484. #endif