mmcsd_core.h 7.4 KB

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