mmcsd_core.h 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. /*
  2. * File : mmcsd_core.h
  3. * This file is part of RT-Thread RTOS
  4. * COPYRIGHT (C) 2006, RT-Thread Development Team
  5. *
  6. * The license and distribution terms for this file may be
  7. * found in the file LICENSE in this distribution or at
  8. * http://www.rt-thread.org/license/LICENSE
  9. *
  10. * Change Logs:
  11. * Date Author Notes
  12. * 2011-07-25 weety first version
  13. */
  14. #ifndef __CORE_H__
  15. #define __CORE_H__
  16. #include <rtthread.h>
  17. #include "mmcsd_host.h"
  18. #include "mmcsd_card.h"
  19. #include "mmcsd_cmd.h"
  20. #ifdef __cplusplus
  21. extern "C" {
  22. #endif
  23. #ifdef RT_MMCSD_DBG
  24. #define mmcsd_dbg(fmt, ...) rt_kprintf(fmt, ##__VA_ARGS__)
  25. #else
  26. #define mmcsd_dbg(fmt, ...)
  27. #endif
  28. struct rt_mmcsd_data {
  29. rt_uint32_t blksize;
  30. rt_uint32_t blks;
  31. rt_uint32_t *buf;
  32. rt_int32_t err;
  33. rt_uint32_t flags;
  34. #define DATA_DIR_WRITE (1 << 0)
  35. #define DATA_DIR_READ (1 << 1)
  36. #define DATA_STREAM (1 << 2)
  37. unsigned int bytes_xfered;
  38. struct rt_mmcsd_cmd *stop; /* stop command */
  39. struct rt_mmcsd_req *mrq; /* associated request */
  40. rt_uint32_t timeout_ns;
  41. rt_uint32_t timeout_clks;
  42. };
  43. struct rt_mmcsd_cmd {
  44. rt_uint32_t cmd_code;
  45. rt_uint32_t arg;
  46. rt_uint32_t resp[4];
  47. rt_uint32_t flags;
  48. /*rsponse types
  49. *bits:0~3
  50. */
  51. #define RESP_MASK (0xF)
  52. #define RESP_NONE (0)
  53. #define RESP_R1 (1 << 0)
  54. #define RESP_R1B (2 << 0)
  55. #define RESP_R2 (3 << 0)
  56. #define RESP_R3 (4 << 0)
  57. #define RESP_R4 (5 << 0)
  58. #define RESP_R6 (6 << 0)
  59. #define RESP_R7 (7 << 0)
  60. /*command types
  61. *bits:4~5
  62. */
  63. #define CMD_MASK (3 << 4) /* command type */
  64. #define CMD_AC (0 << 4)
  65. #define CMD_ADTC (1 << 4)
  66. #define CMD_BC (2 << 4)
  67. #define CMD_BCR (3 << 4)
  68. #define resp_type(cmd) ((cmd)->flags & RESP_MASK)
  69. /*spi rsponse types
  70. *bits:6~8
  71. */
  72. #define RESP_SPI_MASK (0x7 << 6)
  73. #define RESP_SPI_R1 (1 << 6)
  74. #define RESP_SPI_R1B (2 << 6)
  75. #define RESP_SPI_R2 (3 << 6)
  76. #define RESP_SPI_R3 (4 << 6)
  77. #define RESP_SPI_R4 (5 << 6)
  78. #define RESP_SPI_R5 (6 << 6)
  79. #define RESP_SPI_R7 (7 << 6)
  80. #define spi_resp_type(cmd) ((cmd)->flags & RESP_SPI_MASK)
  81. /*
  82. * These are the command types.
  83. */
  84. #define cmd_type(cmd) ((cmd)->flags & CMD_MASK)
  85. rt_int32_t retries; /* max number of retries */
  86. rt_int32_t err;
  87. struct rt_mmcsd_data *data;
  88. struct rt_mmcsd_req *mrq; /* associated request */
  89. };
  90. struct rt_mmcsd_req {
  91. struct rt_mmcsd_data *data;
  92. struct rt_mmcsd_cmd *cmd;
  93. struct rt_mmcsd_cmd *stop;
  94. };
  95. /*the following is response bit*/
  96. #define R1_OUT_OF_RANGE (1 << 31) /* er, c */
  97. #define R1_ADDRESS_ERROR (1 << 30) /* erx, c */
  98. #define R1_BLOCK_LEN_ERROR (1 << 29) /* er, c */
  99. #define R1_ERASE_SEQ_ERROR (1 << 28) /* er, c */
  100. #define R1_ERASE_PARAM (1 << 27) /* ex, c */
  101. #define R1_WP_VIOLATION (1 << 26) /* erx, c */
  102. #define R1_CARD_IS_LOCKED (1 << 25) /* sx, a */
  103. #define R1_LOCK_UNLOCK_FAILED (1 << 24) /* erx, c */
  104. #define R1_COM_CRC_ERROR (1 << 23) /* er, b */
  105. #define R1_ILLEGAL_COMMAND (1 << 22) /* er, b */
  106. #define R1_CARD_ECC_FAILED (1 << 21) /* ex, c */
  107. #define R1_CC_ERROR (1 << 20) /* erx, c */
  108. #define R1_ERROR (1 << 19) /* erx, c */
  109. #define R1_UNDERRUN (1 << 18) /* ex, c */
  110. #define R1_OVERRUN (1 << 17) /* ex, c */
  111. #define R1_CID_CSD_OVERWRITE (1 << 16) /* erx, c, CID/CSD overwrite */
  112. #define R1_WP_ERASE_SKIP (1 << 15) /* sx, c */
  113. #define R1_CARD_ECC_DISABLED (1 << 14) /* sx, a */
  114. #define R1_ERASE_RESET (1 << 13) /* sr, c */
  115. #define R1_STATUS(x) (x & 0xFFFFE000)
  116. #define R1_CURRENT_STATE(x) ((x & 0x00001E00) >> 9) /* sx, b (4 bits) */
  117. #define R1_READY_FOR_DATA (1 << 8) /* sx, a */
  118. #define R1_APP_CMD (1 << 5) /* sr, c */
  119. #define R1_SPI_IDLE (1 << 0)
  120. #define R1_SPI_ERASE_RESET (1 << 1)
  121. #define R1_SPI_ILLEGAL_COMMAND (1 << 2)
  122. #define R1_SPI_COM_CRC (1 << 3)
  123. #define R1_SPI_ERASE_SEQ (1 << 4)
  124. #define R1_SPI_ADDRESS (1 << 5)
  125. #define R1_SPI_PARAMETER (1 << 6)
  126. /* R1 bit 7 is always zero */
  127. #define R2_SPI_CARD_LOCKED (1 << 8)
  128. #define R2_SPI_WP_ERASE_SKIP (1 << 9) /* or lock/unlock fail */
  129. #define R2_SPI_LOCK_UNLOCK_FAIL R2_SPI_WP_ERASE_SKIP
  130. #define R2_SPI_ERROR (1 << 10)
  131. #define R2_SPI_CC_ERROR (1 << 11)
  132. #define R2_SPI_CARD_ECC_ERROR (1 << 12)
  133. #define R2_SPI_WP_VIOLATION (1 << 13)
  134. #define R2_SPI_ERASE_PARAM (1 << 14)
  135. #define R2_SPI_OUT_OF_RANGE (1 << 15) /* or CSD overwrite */
  136. #define R2_SPI_CSD_OVERWRITE R2_SPI_OUT_OF_RANGE
  137. #define CARD_BUSY 0x80000000 /* Card Power up status bit */
  138. /**
  139. * fls - find last (most-significant) bit set
  140. * @x: the word to search
  141. *
  142. * This is defined the same way as ffs.
  143. * Note fls(0) = 0, fls(1) = 1, fls(0x80000000) = 32.
  144. */
  145. rt_inline rt_uint32_t fls(rt_uint32_t val)
  146. {
  147. rt_uint32_t bit = 32;
  148. if (!val)
  149. return 0;
  150. if (!(val & 0xffff0000u))
  151. {
  152. val <<= 16;
  153. bit -= 16;
  154. }
  155. if (!(val & 0xff000000u))
  156. {
  157. val <<= 8;
  158. bit -= 8;
  159. }
  160. if (!(val & 0xf0000000u))
  161. {
  162. val <<= 4;
  163. bit -= 4;
  164. }
  165. if (!(val & 0xc0000000u))
  166. {
  167. val <<= 2;
  168. bit -= 2;
  169. }
  170. if (!(val & 0x80000000u))
  171. {
  172. val <<= 1;
  173. bit -= 1;
  174. }
  175. return bit;
  176. }
  177. #if !defined(__GNUC__) || defined(__CC_ARM)
  178. rt_inline rt_uint32_t ffs(rt_uint32_t x)
  179. {
  180. int r = 1;
  181. if (!x)
  182. return 0;
  183. if (!(x & 0xffff)) {
  184. x >>= 16;
  185. r += 16;
  186. }
  187. if (!(x & 0xff)) {
  188. x >>= 8;
  189. r += 8;
  190. }
  191. if (!(x & 0xf)) {
  192. x >>= 4;
  193. r += 4;
  194. }
  195. if (!(x & 3)) {
  196. x >>= 2;
  197. r += 2;
  198. }
  199. if (!(x & 1)) {
  200. x >>= 1;
  201. r += 1;
  202. }
  203. return r;
  204. }
  205. #endif
  206. void mmcsd_host_lock(struct rt_mmcsd_host *host);
  207. void mmcsd_host_unlock(struct rt_mmcsd_host *host);
  208. void mmcsd_req_complete(struct rt_mmcsd_host *host);
  209. void mmcsd_send_request(struct rt_mmcsd_host *host, struct rt_mmcsd_req *req);
  210. rt_int32_t mmcsd_send_cmd(struct rt_mmcsd_host *host, struct rt_mmcsd_cmd *cmd, int retries);
  211. rt_int32_t mmcsd_go_idle(struct rt_mmcsd_host *host);
  212. rt_int32_t mmcsd_spi_read_ocr(struct rt_mmcsd_host *host, rt_int32_t high_capacity, rt_uint32_t *ocr);
  213. rt_int32_t mmcsd_all_get_cid(struct rt_mmcsd_host *host, rt_uint32_t *cid);
  214. rt_int32_t mmcsd_get_cid(struct rt_mmcsd_host *host, rt_uint32_t *cid);
  215. rt_int32_t mmcsd_get_csd(struct rt_mmcsd_card *card, rt_uint32_t *csd);
  216. rt_int32_t mmcsd_select_card(struct rt_mmcsd_card *card);
  217. rt_int32_t mmcsd_deselect_cards(struct rt_mmcsd_card *host);
  218. rt_int32_t mmcsd_spi_use_crc(struct rt_mmcsd_host *host, rt_int32_t use_crc);
  219. void mmcsd_set_chip_select(struct rt_mmcsd_host *host, rt_int32_t mode);
  220. void mmcsd_set_clock(struct rt_mmcsd_host *host, rt_uint32_t clk);
  221. void mmcsd_set_bus_mode(struct rt_mmcsd_host *host, rt_uint32_t mode);
  222. void mmcsd_set_bus_width(struct rt_mmcsd_host *host, rt_uint32_t width);
  223. void mmcsd_set_data_timeout(struct rt_mmcsd_data *data, const struct rt_mmcsd_card *card);
  224. rt_uint32_t mmcsd_select_voltage(struct rt_mmcsd_host *host, rt_uint32_t ocr);
  225. void mmcsd_change(struct rt_mmcsd_host *host);
  226. void mmcsd_detect(void *param);
  227. struct rt_mmcsd_host *mmcsd_alloc_host(void);
  228. void mmcsd_free_host(struct rt_mmcsd_host *host);
  229. void rt_mmcsd_core_init(void);
  230. void rt_mmcsd_blk_init(void);
  231. rt_int32_t rt_mmcsd_blk_probe(struct rt_mmcsd_card *card);
  232. void rt_mmcsd_blk_remove(struct rt_mmcsd_card *card);
  233. #ifdef __cplusplus
  234. }
  235. #endif
  236. #endif