dev_mmcsd_core.h 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. /*
  2. * Copyright (c) 2006-2026 RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2011-07-25 weety first version
  9. */
  10. #ifndef __DEV_MMCSD_CORE_H__
  11. #define __DEV_MMCSD_CORE_H__
  12. #include <rtthread.h>
  13. #include <drivers/mmcsd_host.h>
  14. #include <drivers/mmcsd_card.h>
  15. #include <drivers/mmcsd_cmd.h>
  16. #ifdef __cplusplus
  17. extern "C" {
  18. #endif
  19. #ifdef RT_MMCSD_DBG
  20. #define mmcsd_dbg(fmt, ...) rt_kprintf(fmt, ##__VA_ARGS__)
  21. #else
  22. #define mmcsd_dbg(fmt, ...)
  23. #endif
  24. struct rt_mmcsd_data
  25. {
  26. rt_uint32_t blksize;
  27. rt_uint32_t blks;
  28. rt_uint32_t *buf;
  29. rt_int32_t err;
  30. rt_uint32_t flags;
  31. #define DATA_DIR_WRITE (1 << 0)
  32. #define DATA_DIR_READ (1 << 1)
  33. #define DATA_STREAM (1 << 2)
  34. unsigned int bytes_xfered;
  35. struct rt_mmcsd_cmd *stop; /* stop command */
  36. struct rt_mmcsd_req *mrq; /* associated request */
  37. rt_uint32_t timeout_ns;
  38. rt_uint32_t timeout_clks;
  39. void *sg; /* scatter list */
  40. rt_uint16_t sg_len; /* size of scatter list */
  41. rt_int16_t sg_count; /* mapped sg entries */
  42. rt_ubase_t host_cookie; /* host driver private data */
  43. };
  44. struct rt_mmcsd_cmd
  45. {
  46. rt_uint32_t cmd_code;
  47. rt_uint32_t arg;
  48. rt_uint32_t resp[4];
  49. rt_uint32_t flags;
  50. /*rsponse types
  51. *bits:0~3
  52. */
  53. #define RESP_MASK (0xF)
  54. #define RESP_NONE (0)
  55. #define RESP_R1 (1 << 0)
  56. #define RESP_R1B (2 << 0)
  57. #define RESP_R2 (3 << 0)
  58. #define RESP_R3 (4 << 0)
  59. #define RESP_R4 (5 << 0)
  60. #define RESP_R6 (6 << 0)
  61. #define RESP_R7 (7 << 0)
  62. #define RESP_R5 (8 << 0) /*SDIO command response type*/
  63. /*command types
  64. *bits:4~5
  65. */
  66. #define CMD_MASK (3 << 4) /* command type */
  67. #define CMD_AC (0 << 4)
  68. #define CMD_ADTC (1 << 4)
  69. #define CMD_BC (2 << 4)
  70. #define CMD_BCR (3 << 4)
  71. #define resp_type(cmd) ((cmd)->flags & RESP_MASK)
  72. /*spi rsponse types
  73. *bits:6~8
  74. */
  75. #define RESP_SPI_MASK (0x7 << 6)
  76. #define RESP_SPI_R1 (1 << 6)
  77. #define RESP_SPI_R1B (2 << 6)
  78. #define RESP_SPI_R2 (3 << 6)
  79. #define RESP_SPI_R3 (4 << 6)
  80. #define RESP_SPI_R4 (5 << 6)
  81. #define RESP_SPI_R5 (6 << 6)
  82. #define RESP_SPI_R7 (7 << 6)
  83. #define spi_resp_type(cmd) ((cmd)->flags & RESP_SPI_MASK)
  84. /*
  85. * These are the command types.
  86. */
  87. #define cmd_type(cmd) ((cmd)->flags & CMD_MASK)
  88. rt_int32_t retries; /* max number of retries */
  89. rt_int32_t err;
  90. unsigned int busy_timeout; /* busy detect timeout in ms */
  91. struct rt_mmcsd_data *data;
  92. struct rt_mmcsd_req *mrq; /* associated request */
  93. };
  94. struct rt_mmcsd_req
  95. {
  96. struct rt_mmcsd_data *data;
  97. struct rt_mmcsd_cmd *cmd;
  98. struct rt_mmcsd_cmd *stop;
  99. struct rt_mmcsd_cmd *sbc; /* SET_BLOCK_COUNT for multiblock */
  100. /* Allow other commands during this ongoing data transfer or busy wait */
  101. int cap_cmd_during_tfr;
  102. };
  103. /*the following is response bit*/
  104. #define R1_OUT_OF_RANGE (1 << 31) /* er, c */
  105. #define R1_ADDRESS_ERROR (1 << 30) /* erx, c */
  106. #define R1_BLOCK_LEN_ERROR (1 << 29) /* er, c */
  107. #define R1_ERASE_SEQ_ERROR (1 << 28) /* er, c */
  108. #define R1_ERASE_PARAM (1 << 27) /* ex, c */
  109. #define R1_WP_VIOLATION (1 << 26) /* erx, c */
  110. #define R1_CARD_IS_LOCKED (1 << 25) /* sx, a */
  111. #define R1_LOCK_UNLOCK_FAILED (1 << 24) /* erx, c */
  112. #define R1_COM_CRC_ERROR (1 << 23) /* er, b */
  113. #define R1_ILLEGAL_COMMAND (1 << 22) /* er, b */
  114. #define R1_CARD_ECC_FAILED (1 << 21) /* ex, c */
  115. #define R1_CC_ERROR (1 << 20) /* erx, c */
  116. #define R1_ERROR (1 << 19) /* erx, c */
  117. #define R1_UNDERRUN (1 << 18) /* ex, c */
  118. #define R1_OVERRUN (1 << 17) /* ex, c */
  119. #define R1_CID_CSD_OVERWRITE (1 << 16) /* erx, c, CID/CSD overwrite */
  120. #define R1_WP_ERASE_SKIP (1 << 15) /* sx, c */
  121. #define R1_CARD_ECC_DISABLED (1 << 14) /* sx, a */
  122. #define R1_ERASE_RESET (1 << 13) /* sr, c */
  123. #define R1_STATUS(x) (x & 0xFFFFE000)
  124. #define R1_CURRENT_STATE(x) ((x & 0x00001E00) >> 9) /* sx, b (4 bits) */
  125. #define R1_READY_FOR_DATA (1 << 8) /* sx, a */
  126. #define R1_APP_CMD (1 << 5) /* sr, c */
  127. /*the programing is state*/
  128. #define R1_STATE_PRG 0x07
  129. #define R1_SPI_IDLE (1 << 0)
  130. #define R1_SPI_ERASE_RESET (1 << 1)
  131. #define R1_SPI_ILLEGAL_COMMAND (1 << 2)
  132. #define R1_SPI_COM_CRC (1 << 3)
  133. #define R1_SPI_ERASE_SEQ (1 << 4)
  134. #define R1_SPI_ADDRESS (1 << 5)
  135. #define R1_SPI_PARAMETER (1 << 6)
  136. /* R1 bit 7 is always zero */
  137. #define R2_SPI_CARD_LOCKED (1 << 8)
  138. #define R2_SPI_WP_ERASE_SKIP (1 << 9) /* or lock/unlock fail */
  139. #define R2_SPI_LOCK_UNLOCK_FAIL R2_SPI_WP_ERASE_SKIP
  140. #define R2_SPI_ERROR (1 << 10)
  141. #define R2_SPI_CC_ERROR (1 << 11)
  142. #define R2_SPI_CARD_ECC_ERROR (1 << 12)
  143. #define R2_SPI_WP_VIOLATION (1 << 13)
  144. #define R2_SPI_ERASE_PARAM (1 << 14)
  145. #define R2_SPI_OUT_OF_RANGE (1 << 15) /* or CSD overwrite */
  146. #define R2_SPI_CSD_OVERWRITE R2_SPI_OUT_OF_RANGE
  147. #define CARD_BUSY 0x80000000 /* Card Power up status bit */
  148. /* R5 response bits */
  149. #define R5_COM_CRC_ERROR (1 << 15)
  150. #define R5_ILLEGAL_COMMAND (1 << 14)
  151. #define R5_ERROR (1 << 11)
  152. #define R5_FUNCTION_NUMBER (1 << 9)
  153. #define R5_OUT_OF_RANGE (1 << 8)
  154. #define R5_STATUS(x) (x & 0xCB00)
  155. #define R5_IO_CURRENT_STATE(x) ((x & 0x3000) >> 12)
  156. #define MMCSD_HOST_PLUGED 0
  157. #define MMCSD_HOST_UNPLUGED 1
  158. rt_int32_t mmcsd_excute_tuning(struct rt_mmcsd_card *card);
  159. int mmcsd_wait_cd_changed(rt_int32_t timeout);
  160. void mmcsd_host_lock(struct rt_mmcsd_host *host);
  161. void mmcsd_host_unlock(struct rt_mmcsd_host *host);
  162. void mmcsd_req_complete(struct rt_mmcsd_host *host);
  163. void mmcsd_send_request(struct rt_mmcsd_host *host, struct rt_mmcsd_req *req);
  164. rt_int32_t mmcsd_send_cmd(struct rt_mmcsd_host *host, struct rt_mmcsd_cmd *cmd, int retries);
  165. rt_int32_t mmcsd_go_idle(struct rt_mmcsd_host *host);
  166. rt_int32_t mmcsd_spi_read_ocr(struct rt_mmcsd_host *host, rt_int32_t high_capacity, rt_uint32_t *ocr);
  167. rt_int32_t mmcsd_all_get_cid(struct rt_mmcsd_host *host, rt_uint32_t *cid);
  168. rt_int32_t mmcsd_get_cid(struct rt_mmcsd_host *host, rt_uint32_t *cid);
  169. rt_int32_t mmcsd_get_csd(struct rt_mmcsd_card *card, rt_uint32_t *csd);
  170. rt_int32_t mmcsd_select_card(struct rt_mmcsd_card *card);
  171. rt_int32_t mmcsd_deselect_cards(struct rt_mmcsd_card *host);
  172. rt_int32_t mmcsd_spi_use_crc(struct rt_mmcsd_host *host, rt_int32_t use_crc);
  173. void mmcsd_set_chip_select(struct rt_mmcsd_host *host, rt_int32_t mode);
  174. void mmcsd_set_clock(struct rt_mmcsd_host *host, rt_uint32_t clk);
  175. void mmcsd_set_bus_mode(struct rt_mmcsd_host *host, rt_uint32_t mode);
  176. void mmcsd_set_bus_width(struct rt_mmcsd_host *host, rt_uint32_t width);
  177. void mmcsd_set_timing(struct rt_mmcsd_host *host, rt_uint32_t timing);
  178. void mmcsd_set_data_timeout(struct rt_mmcsd_data *data, const struct rt_mmcsd_card *card);
  179. rt_uint32_t mmcsd_select_voltage(struct rt_mmcsd_host *host, rt_uint32_t ocr);
  180. rt_err_t mmcsd_set_signal_voltage(struct rt_mmcsd_host *host, unsigned char signal_voltage);
  181. void mmcsd_set_initial_signal_voltage(struct rt_mmcsd_host *host);
  182. rt_err_t mmcsd_host_set_uhs_voltage(struct rt_mmcsd_host *host);
  183. rt_err_t mmcsd_set_uhs_voltage(struct rt_mmcsd_host *host, rt_uint32_t ocr);
  184. rt_err_t mmcsd_send_tuning(struct rt_mmcsd_host *host, rt_uint32_t opcode, rt_err_t *cmd_error);
  185. rt_err_t mmcsd_send_abort_tuning(struct rt_mmcsd_host *host, rt_uint32_t opcode);
  186. void mmcsd_change(struct rt_mmcsd_host *host);
  187. void mmcsd_detect(void *param);
  188. void mmcsd_host_init(struct rt_mmcsd_host *host);
  189. struct rt_mmcsd_host *mmcsd_alloc_host(void);
  190. void mmcsd_free_host(struct rt_mmcsd_host *host);
  191. int rt_mmcsd_core_init(void);
  192. rt_int32_t rt_mmcsd_blk_probe(struct rt_mmcsd_card *card);
  193. void rt_mmcsd_blk_remove(struct rt_mmcsd_card *card);
  194. #ifdef __cplusplus
  195. }
  196. #endif
  197. #endif