mmcsd_core.h 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. #ifndef _MMCSD_CORE_H
  2. #define _MMCSD_CORE_H
  3. #include "FreeRTOS.h"
  4. #include "mmcsd_host.h"
  5. #include "mmcsd_card.h"
  6. #include "mmcsd_cmd.h"
  7. #ifdef __cplusplus
  8. extern "C" {
  9. #endif
  10. #ifdef MMCSD_DBG
  11. #define mmcsd_dbg(fmt, ...) printf(fmt, ##__VA_ARGS__)
  12. #else
  13. #define mmcsd_dbg(fmt, ...)
  14. #endif
  15. struct mmcsd_data {
  16. uint32_t blksize;
  17. uint32_t blks;
  18. uint32_t *buf;
  19. int32_t err;
  20. uint32_t flags;
  21. #define DATA_DIR_WRITE (1 << 0)
  22. #define DATA_DIR_READ (1 << 1)
  23. #define DATA_STREAM (1 << 2)
  24. unsigned int bytes_xfered;
  25. struct mmcsd_cmd *stop; /* stop command */
  26. struct mmcsd_req *mrq; /* associated request */
  27. uint32_t timeout_ns;
  28. uint32_t timeout_clks;
  29. };
  30. struct mmcsd_cmd {
  31. uint32_t cmd_code;
  32. uint32_t arg;
  33. uint32_t resp[4];
  34. uint32_t flags;
  35. /*rsponse types
  36. *bits:0~3
  37. */
  38. #define RESP_MASK (0xF)
  39. #define RESP_NONE (0)
  40. #define RESP_R1 (1 << 0)
  41. #define RESP_R1B (2 << 0)
  42. #define RESP_R2 (3 << 0)
  43. #define RESP_R3 (4 << 0)
  44. #define RESP_R4 (5 << 0)
  45. #define RESP_R6 (6 << 0)
  46. #define RESP_R7 (7 << 0)
  47. #define RESP_R5 (8 << 0) /*SDIO command response type*/
  48. /*command types
  49. *bits:4~5
  50. */
  51. #define CMD_MASK (3 << 4) /* command type */
  52. #define CMD_AC (0 << 4)
  53. #define CMD_ADTC (1 << 4)
  54. #define CMD_BC (2 << 4)
  55. #define CMD_BCR (3 << 4)
  56. #define resp_type(cmd) ((cmd)->flags & RESP_MASK)
  57. /*spi rsponse types
  58. *bits:6~8
  59. */
  60. #define RESP_SPI_MASK (0x7 << 6)
  61. #define RESP_SPI_R1 (1 << 6)
  62. #define RESP_SPI_R1B (2 << 6)
  63. #define RESP_SPI_R2 (3 << 6)
  64. #define RESP_SPI_R3 (4 << 6)
  65. #define RESP_SPI_R4 (5 << 6)
  66. #define RESP_SPI_R5 (6 << 6)
  67. #define RESP_SPI_R7 (7 << 6)
  68. #define spi_resp_type(cmd) ((cmd)->flags & RESP_SPI_MASK)
  69. /*
  70. * These are the command types.
  71. */
  72. #define cmd_type(cmd) ((cmd)->flags & CMD_MASK)
  73. int32_t retries; /* max number of retries */
  74. int32_t err;
  75. struct mmcsd_data *data;
  76. struct mmcsd_req *mrq; /* associated request */
  77. };
  78. struct mmcsd_req {
  79. struct mmcsd_data *data;
  80. struct mmcsd_cmd *cmd;
  81. struct mmcsd_cmd *stop;
  82. };
  83. /*the following is response bit*/
  84. #define R1_OUT_OF_RANGE (1 << 31) /* er, c */
  85. #define R1_ADDRESS_ERROR (1 << 30) /* erx, c */
  86. #define R1_BLOCK_LEN_ERROR (1 << 29) /* er, c */
  87. #define R1_ERASE_SEQ_ERROR (1 << 28) /* er, c */
  88. #define R1_ERASE_PARAM (1 << 27) /* ex, c */
  89. #define R1_WP_VIOLATION (1 << 26) /* erx, c */
  90. #define R1_CARD_IS_LOCKED (1 << 25) /* sx, a */
  91. #define R1_LOCK_UNLOCK_FAILED (1 << 24) /* erx, c */
  92. #define R1_COM_CRC_ERROR (1 << 23) /* er, b */
  93. #define R1_ILLEGAL_COMMAND (1 << 22) /* er, b */
  94. #define R1_CARD_ECC_FAILED (1 << 21) /* ex, c */
  95. #define R1_CC_ERROR (1 << 20) /* erx, c */
  96. #define R1_ERROR (1 << 19) /* erx, c */
  97. #define R1_UNDERRUN (1 << 18) /* ex, c */
  98. #define R1_OVERRUN (1 << 17) /* ex, c */
  99. #define R1_CID_CSD_OVERWRITE (1 << 16) /* erx, c, CID/CSD overwrite */
  100. #define R1_WP_ERASE_SKIP (1 << 15) /* sx, c */
  101. #define R1_CARD_ECC_DISABLED (1 << 14) /* sx, a */
  102. #define R1_ERASE_RESET (1 << 13) /* sr, c */
  103. #define R1_STATUS(x) (x & 0xFFFFE000)
  104. #define R1_CURRENT_STATE(x) ((x & 0x00001E00) >> 9) /* sx, b (4 bits) */
  105. #define R1_READY_FOR_DATA (1 << 8) /* sx, a */
  106. #define R1_APP_CMD (1 << 5) /* sr, c */
  107. #define R1_SPI_IDLE (1 << 0)
  108. #define R1_SPI_ERASE_RESET (1 << 1)
  109. #define R1_SPI_ILLEGAL_COMMAND (1 << 2)
  110. #define R1_SPI_COM_CRC (1 << 3)
  111. #define R1_SPI_ERASE_SEQ (1 << 4)
  112. #define R1_SPI_ADDRESS (1 << 5)
  113. #define R1_SPI_PARAMETER (1 << 6)
  114. /* R1 bit 7 is always zero */
  115. #define R2_SPI_CARD_LOCKED (1 << 8)
  116. #define R2_SPI_WP_ERASE_SKIP (1 << 9) /* or lock/unlock fail */
  117. #define R2_SPI_LOCK_UNLOCK_FAIL R2_SPI_WP_ERASE_SKIP
  118. #define R2_SPI_ERROR (1 << 10)
  119. #define R2_SPI_CC_ERROR (1 << 11)
  120. #define R2_SPI_CARD_ECC_ERROR (1 << 12)
  121. #define R2_SPI_WP_VIOLATION (1 << 13)
  122. #define R2_SPI_ERASE_PARAM (1 << 14)
  123. #define R2_SPI_OUT_OF_RANGE (1 << 15) /* or CSD overwrite */
  124. #define R2_SPI_CSD_OVERWRITE R2_SPI_OUT_OF_RANGE
  125. #define CARD_BUSY 0x80000000 /* Card Power up status bit */
  126. /* R5 response bits */
  127. #define R5_COM_CRC_ERROR (1 << 15)
  128. #define R5_ILLEGAL_COMMAND (1 << 14)
  129. #define R5_ERROR (1 << 11)
  130. #define R5_FUNCTION_NUMBER (1 << 9)
  131. #define R5_OUT_OF_RANGE (1 << 8)
  132. #define R5_STATUS(x) (x & 0xCB00)
  133. #define R5_IO_CURRENT_STATE(x) ((x & 0x3000) >> 12)
  134. #define MMCSD_STATUS_PLUGED (1<<0)
  135. #define MMCSD_STATUS_UNPLUG (1<<1)
  136. #define MMCSD_STATUS_READY (1<<4)
  137. #define MMCSD_STATUS_UNREADY (1<<5)
  138. int mmcsd_wait_cd_changed(uint32_t timeout);
  139. int mmcsd_wait_sdio_ready_by_name(const char *name, uint32_t timeout);
  140. int mmcsd_wait_sdio_unready_by_name(const char *name, uint32_t timeout);
  141. int mmcsd_wait_dev_status(int driver_id, uint16_t expect_status, uint32_t timeout);
  142. int mmcsd_wait_dev_status_by_name(const char *name, uint16_t expect_status, uint32_t timeout);
  143. int mmcsd_dev_is_support_hotpluge(int driver_id);
  144. int mmcsd_dev_is_sdio_card(int driver_id);
  145. int mmcsd_wait_sdio_ready(uint32_t timeout);
  146. void mmcsd_host_lock(struct mmcsd_host *host);
  147. void mmcsd_host_unlock(struct mmcsd_host *host);
  148. void mmcsd_req_complete(struct mmcsd_host *host);
  149. void mmcsd_send_request(struct mmcsd_host *host, struct mmcsd_req *req);
  150. int32_t mmcsd_send_cmd(struct mmcsd_host *host, struct mmcsd_cmd *cmd, int retries);
  151. int32_t mmcsd_go_idle(struct mmcsd_host *host);
  152. int32_t mmcsd_spi_read_ocr(struct mmcsd_host *host, int32_t high_capacity, uint32_t *ocr);
  153. int32_t mmcsd_all_get_cid(struct mmcsd_host *host, uint32_t *cid);
  154. int32_t mmcsd_get_cid(struct mmcsd_host *host, uint32_t *cid);
  155. int32_t mmcsd_get_csd(struct mmcsd_card *card, uint32_t *csd);
  156. int32_t mmcsd_select_card(struct mmcsd_card *card);
  157. int32_t mmcsd_deselect_cards(struct mmcsd_card *host);
  158. int32_t mmcsd_spi_use_crc(struct mmcsd_host *host, int32_t use_crc);
  159. void mmcsd_set_chip_select(struct mmcsd_host *host, int32_t mode);
  160. void mmcsd_set_clock(struct mmcsd_host *host, uint32_t clk);
  161. void mmcsd_set_bus_mode(struct mmcsd_host *host, uint32_t mode);
  162. void mmcsd_set_bus_width(struct mmcsd_host *host, uint32_t width);
  163. void mmcsd_set_data_timeout(struct mmcsd_data *data, const struct mmcsd_card *card);
  164. uint32_t mmcsd_select_voltage(struct mmcsd_host *host, uint32_t ocr);
  165. void mmcsd_change(struct mmcsd_host *host);
  166. void mmcsd_change_from_isr(struct mmcsd_host *host);
  167. void mmcsd_detect(void *param);
  168. struct mmcsd_host *mmcsd_alloc_host(void);
  169. void mmcsd_free_host(struct mmcsd_host *host);
  170. int mmcsd_core_init(void);
  171. int mmcsd_blk_init(void);
  172. int32_t mmcsd_num_wr_blocks(struct mmcsd_card *card);
  173. int mmcsd_req_blk(struct mmcsd_card *card, uint32_t sector, void *buf, size_t blks, uint8_t dir);
  174. int32_t mmcsd_set_blksize(struct mmcsd_card *card);
  175. struct mmcsd_card *mmcsd_get_sdmmc_card_info_by_name(const char *pcName);
  176. #if DEVICE_TYPE_SELECT == EMMC_FLASH
  177. int emmc_read(uint32_t offset, size_t size, uint8_t *data);
  178. int emmc_write(uint32_t offset, size_t size, uint8_t *data);
  179. #endif
  180. #ifdef __cplusplus
  181. }
  182. #endif
  183. #endif