dev_sdhci_host.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  1. /*
  2. * Copyright (c) 2006-2024 RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2024-08-16 zhujiale first version
  9. */
  10. #ifndef __DEV_SDHCI_HOST_H__
  11. #define __DEV_SDHCI_HOST_H__
  12. #include <rtthread.h>
  13. #include <drivers/dma.h>
  14. #include <drivers/mmcsd_cmd.h>
  15. #include <drivers/mmcsd_host.h>
  16. #include <drivers/dev_mmcsd_core.h>
  17. #define rt_mmc_dev(x) ((x)->parent)
  18. #define MMC_SEND_TUNING_BLOCK_HS200 SEND_TUNING_BLOCK_HS200
  19. #define MMC_SEND_TUNING_BLOCK SEND_TUNING_BLOCK
  20. #define MMC_STOP_TRANSMISSION STOP_TRANSMISSION
  21. #define MMC_BUS_TEST_R 14 /* adtc R1 */
  22. #define MMC_WRITE_MULTIPLE_BLOCK WRITE_MULTIPLE_BLOCK
  23. #define MMC_READ_MULTIPLE_BLOCK READ_MULTIPLE_BLOCK
  24. #define MMC_TIMING_UHS_DDR50 MMCSD_TIMING_UHS_DDR50
  25. #define MMC_TIMING_UHS_SDR50 MMCSD_TIMING_UHS_SDR50
  26. #define MMC_TIMING_MMC_HS200 MMCSD_TIMING_MMC_HS200
  27. #define MMC_TIMING_MMC_HS400 MMCSD_TIMING_MMC_HS400
  28. #define MMC_TIMING_UHS_SDR104 MMCSD_TIMING_UHS_SDR104
  29. #define MMC_TIMING_UHS_SDR25 MMCSD_TIMING_UHS_SDR25
  30. #define MMC_TIMING_MMC_DDR52 MMCSD_TIMING_MMC_DDR52
  31. #define MMC_TIMING_UHS_SDR12 MMCSD_TIMING_UHS_SDR12
  32. #define MMC_TIMING_SD_HS MMCSD_TIMING_SD_HS
  33. #define MMC_TIMING_MMC_HS MMCSD_TIMING_MMC_HS
  34. #define MMC_POWER_OFF MMCSD_POWER_OFF
  35. #define MMC_POWER_UP MMCSD_POWER_UP
  36. #define MMC_POWER_ON MMCSD_POWER_ON
  37. #define MMC_POWER_UNDEFINED 3
  38. #define MMC_SET_DRIVER_TYPE_B 0
  39. #define MMC_SET_DRIVER_TYPE_A 1
  40. #define MMC_SET_DRIVER_TYPE_C 2
  41. #define MMC_SET_DRIVER_TYPE_D 3
  42. #define MMC_SIGNAL_VOLTAGE_330 0
  43. #define MMC_SIGNAL_VOLTAGE_180 1
  44. #define MMC_SIGNAL_VOLTAGE_120 2
  45. #define MMC_RSP_PRESENT (1 << 16)
  46. #define MMC_RSP_136 (1 << 17) /* 136 bit response */
  47. #define MMC_RSP_CRC (1 << 18) /* expect valid crc */
  48. #define MMC_RSP_BUSY (1 << 19) /* card may send busy */
  49. #define MMC_RSP_OPCODE (1 << 20) /* response contains opcode */
  50. #define MMC_RSP_NONE (0)
  51. #define MMC_RSP_R1 (MMC_RSP_PRESENT | MMC_RSP_CRC | MMC_RSP_OPCODE)
  52. #define MMC_RSP_R1B (MMC_RSP_PRESENT | MMC_RSP_CRC | MMC_RSP_OPCODE | MMC_RSP_BUSY)
  53. #define MMC_RSP_R2 (MMC_RSP_PRESENT | MMC_RSP_136 | MMC_RSP_CRC)
  54. #define MMC_RSP_R3 (MMC_RSP_PRESENT)
  55. #define MMC_RSP_R4 (MMC_RSP_PRESENT)
  56. #define MMC_RSP_R5 (MMC_RSP_PRESENT | MMC_RSP_CRC | MMC_RSP_OPCODE)
  57. #define MMC_RSP_R6 (MMC_RSP_PRESENT | MMC_RSP_CRC | MMC_RSP_OPCODE)
  58. #define MMC_RSP_R7 (MMC_RSP_PRESENT | MMC_RSP_CRC | MMC_RSP_OPCODE)
  59. #define MMC_CMD_ADTC CMD_ADTC
  60. #define MMC_BUS_WIDTH_8 MMCSD_BUS_WIDTH_8
  61. #define MMC_BUS_WIDTH_4 MMCSD_BUS_WIDTH_4
  62. #define MMC_BUS_WIDTH_1 MMCSD_BUS_WIDTH_1
  63. #define MMC_PM_KEEP_POWER (1 << 0) /* preserve card power during suspend */
  64. #define MMC_PM_WAKE_SDIO_IRQ (1 << 1) /* wake up host system on SDIO IRQ assertion */
  65. enum mmc_blk_status
  66. {
  67. MMC_BLK_SUCCESS = 0,
  68. MMC_BLK_PARTIAL,
  69. MMC_BLK_CMD_ERR,
  70. MMC_BLK_RETRY,
  71. MMC_BLK_ABORT,
  72. MMC_BLK_DATA_ERR,
  73. MMC_BLK_ECC_ERR,
  74. MMC_BLK_NOMEDIUM,
  75. MMC_BLK_NEW_REQUEST,
  76. };
  77. #define MMC_NUM_CLK_PHASES (MMC_TIMING_MMC_HS400 + 1)
  78. struct rt_mmc_host;
  79. struct rt_mmc_host_ops
  80. {
  81. void (*request)(struct rt_mmc_host *host, struct rt_mmcsd_req *req);
  82. void (*set_ios)(struct rt_mmc_host *host, struct rt_mmcsd_io_cfg *ios);
  83. int (*get_ro)(struct rt_mmc_host *host);
  84. int (*get_cd)(struct rt_mmc_host *host);
  85. void (*enable_sdio_irq)(struct rt_mmc_host *host, int enable);
  86. void (*ack_sdio_irq)(struct rt_mmc_host *host);
  87. int (*start_signal_voltage_switch)(struct rt_mmc_host *host, struct rt_mmcsd_io_cfg *ios);
  88. int (*card_busy)(struct rt_mmc_host *host);
  89. int (*execute_tuning)(struct rt_mmc_host *host, unsigned opcode);
  90. int (*prepare_hs400_tuning)(struct rt_mmc_host *host, struct rt_mmcsd_io_cfg *ios);
  91. int (*hs400_prepare_ddr)(struct rt_mmc_host *host);
  92. void (*hs400_downgrade)(struct rt_mmc_host *host);
  93. void (*hs400_complete)(struct rt_mmc_host *host);
  94. void (*hs400_enhanced_strobe)(struct rt_mmc_host *host, struct rt_mmcsd_io_cfg *ios);
  95. void (*hw_reset)(struct rt_mmc_host *host);
  96. void (*card_event)(struct rt_mmc_host *host);
  97. };
  98. /* VDD voltage 3.3 ~ 3.4 */
  99. #define MMC_VDD_34_35 0x00400000 /* VDD voltage 3.4 ~ 3.5 */
  100. #define MMC_VDD_35_36 0x00800000 /* VDD voltage 3.5 ~ 3.6 */
  101. #define MMC_CAP2_HS200_1_8V_SDR MMCSD_SUP_HS200_1V8
  102. #define MMC_CAP2_HS200_1_2V_SDR MMCSD_SUP_HS200_1V2
  103. #define MMC_CAP_4_BIT_DATA MMCSD_BUSWIDTH_4
  104. #define MMC_CAP_8_BIT_DATA MMCSD_BUSWIDTH_8
  105. #define MMC_CAP2_HS200 MMCSD_SUP_HS200
  106. #define MMC_CAP_MMC_HIGHSPEED MMCSD_SUP_HIGHSPEED
  107. #define MMC_CAP_SD_HIGHSPEED MMCSD_SUP_HIGHSPEED
  108. #define MMC_CAP_1_8V_DDR MMCSD_SUP_DDR_1V8
  109. #define MMC_CAP_3_3V_DDR MMCSD_SUP_DDR_3V3
  110. #define MMC_CAP_1_2V_DDR MMCSD_SUP_DDR_1V2
  111. #define MMC_CAP_NONREMOVABLE MMCSD_SUP_NONREMOVABLE
  112. #define MMC_CAP_UHS_DDR50 0
  113. #define MMC_CAP2_HS400 0
  114. #define MMC_CAP_UHS_SDR50 0
  115. #define MMC_CAP_UHS_SDR25 0
  116. #define MMC_CAP_UHS_SDR12 0
  117. #define MMC_CAP_UHS_SDR104 0
  118. #define MMC_CAP_UHS 0
  119. #define MMC_CAP2_HSX00_1_8V 0
  120. #define MMC_CAP2_HS400_ES 0
  121. #define MMC_CAP_NEEDS_POLL 0
  122. #define MMC_CAP2_HSX00_1_2V 0
  123. #define MMC_CAP2_HS400_1_2V 0
  124. #define MMC_CAP2_HS400_1_8V 0
  125. #define MMC_CAP_DRIVER_TYPE_D 0
  126. #define MMC_CAP_DRIVER_TYPE_C 0
  127. #define MMC_SET_DRIVER_TYPE_B 0
  128. #define MMC_CAP_DRIVER_TYPE_A 0
  129. #define MMC_CAP2_SDIO_IRQ_NOTHREAD 0
  130. #define MMC_CAP_CMD23 0
  131. #define MMC_CAP_SDIO_IRQ 0
  132. #define MMC_CAP2_NO_SDIO (1 << 19)
  133. #define MMC_CAP2_NO_SD (1 << 21)
  134. #define MMC_CAP2_NO_MMC (1 << 22)
  135. #define MMC_CAP2_CQE (1 << 23)
  136. #define MMC_VDD_165_195 VDD_165_195
  137. #define MMC_VDD_20_21 VDD_20_21
  138. #define MMC_VDD_29_30 VDD_29_30
  139. #define MMC_VDD_30_31 VDD_30_31
  140. #define MMC_VDD_32_33 VDD_32_33
  141. #define MMC_VDD_33_34 VDD_33_34
  142. struct rt_mmc_host
  143. {
  144. struct rt_mmcsd_host rthost;
  145. struct rt_device *parent;
  146. int index;
  147. const struct rt_mmc_host_ops *ops;
  148. unsigned int f_min;
  149. unsigned int f_max;
  150. unsigned int f_init;
  151. rt_uint32_t ocr_avail;
  152. rt_uint32_t ocr_avail_sdio; /* SDIO-specific OCR */
  153. rt_uint32_t ocr_avail_sd; /* SD-specific OCR */
  154. rt_uint32_t ocr_avail_mmc; /* MMC-specific OCR */
  155. struct wakeup_source *ws; /* Enable consume of uevents */
  156. rt_uint32_t max_current_330;
  157. rt_uint32_t max_current_300;
  158. rt_uint32_t max_current_180;
  159. rt_uint32_t caps; /* Host capabilities */
  160. rt_uint32_t caps2; /* More host capabilities */
  161. /* host specific block data */
  162. unsigned int max_seg_size; /* see blk_queue_max_segment_size */
  163. unsigned short max_segs; /* see blk_queue_max_segments */
  164. unsigned short unused;
  165. unsigned int max_req_size; /* maximum number of bytes in one req */
  166. unsigned int max_blk_size; /* maximum size of one mmc block */
  167. unsigned int max_blk_count; /* maximum number of blocks in one req */
  168. unsigned int max_busy_timeout; /* max busy timeout in ms */
  169. struct rt_mmcsd_io_cfg ios; /* current io bus settings */
  170. unsigned int retune_period;
  171. /* group bitfields together to minimize padding */
  172. unsigned int use_spi_crc : 1;
  173. unsigned int claimed : 1; /* host exclusively claimed */
  174. unsigned int doing_init_tune : 1; /* initial tuning in progress */
  175. unsigned int can_retune : 1; /* re-tuning can be used */
  176. unsigned int doing_retune : 1; /* re-tuning in progress */
  177. unsigned int retune_now : 1; /* do re-tuning at next req */
  178. unsigned int retune_paused : 1; /* re-tuning is temporarily disabled */
  179. unsigned int retune_crc_disable : 1; /* don't trigger retune upon crc */
  180. unsigned int can_dma_map_merge : 1; /* merging can be used */
  181. unsigned int vqmmc_enabled : 1; /* vqmmc regulator is enabled */
  182. int need_retune; /* re-tuning is needed */
  183. int hold_retune; /* hold off re-tuning */
  184. rt_bool_t trigger_card_event; /* card_event necessary */
  185. unsigned int sdio_irqs;
  186. rt_bool_t sdio_irq_pending;
  187. /* Ongoing data transfer that allows commands during transfer */
  188. struct rt_mmcsd_req *ongoing_mrq;
  189. rt_uint32_t actual_clock; /* Actual HC clock rate */
  190. rt_uint32_t pm_caps;
  191. rt_ubase_t private[];
  192. };
  193. rt_inline int mmc_card_is_removable(struct rt_mmc_host *host)
  194. {
  195. return !(host->caps & MMC_CAP_NONREMOVABLE);
  196. }
  197. struct rt_mmc_host *rt_mmc_alloc_host(int extra, struct rt_device *);
  198. rt_err_t rt_mmc_add_host(struct rt_mmc_host *);
  199. void rt_mmc_remove_host(struct rt_mmc_host *);
  200. void rt_mmc_free_host(struct rt_mmc_host *);
  201. rt_err_t rt_mmc_of_parse(struct rt_mmc_host *host);
  202. rt_inline void *rt_mmc_priv(struct rt_mmc_host *host)
  203. {
  204. return (void *)host->private;
  205. }
  206. #define mmc_host_is_spi(host) ((host)->caps & MMC_CAP_SPI)
  207. #define mmc_dev(x) ((x)->parent)
  208. #define mmc_classdev(x) (&(x)->class_dev)
  209. #define mmc_hostname(x) (x->parent->parent.name)
  210. void rt_mmc_detect_change(struct rt_mmc_host *host, rt_ubase_t delay);
  211. void rt_mmc_request_done(struct rt_mmc_host *host, struct rt_mmcsd_req *req);
  212. rt_inline rt_bool_t sdio_irq_claimed(struct rt_mmc_host *host)
  213. {
  214. return host->sdio_irqs > 0;
  215. }
  216. void mmc_retune_timer_stop(struct rt_mmc_host* host);
  217. enum dma_data_direction
  218. {
  219. DMA_BIDIRECTIONAL = 0,
  220. DMA_TO_DEVICE = 1,
  221. DMA_FROM_DEVICE = 2,
  222. DMA_NONE = 3,
  223. };
  224. rt_inline void mmc_retune_needed(struct rt_mmc_host *host)
  225. {
  226. if (host->can_retune)
  227. {
  228. host->need_retune = 1;
  229. }
  230. }
  231. rt_inline rt_bool_t mmc_can_retune(struct rt_mmc_host *host)
  232. {
  233. return host->can_retune == 1;
  234. }
  235. rt_inline rt_bool_t mmc_doing_retune(struct rt_mmc_host *host)
  236. {
  237. return host->doing_retune == 1;
  238. }
  239. rt_inline rt_bool_t mmc_doing_tune(struct rt_mmc_host *host)
  240. {
  241. return host->doing_retune == 1 || host->doing_init_tune == 1;
  242. }
  243. rt_inline int mmc_get_dma_dir(struct rt_mmcsd_data *data)
  244. {
  245. return data->flags & DATA_DIR_WRITE ? DMA_TO_DEVICE : DMA_FROM_DEVICE;
  246. }
  247. rt_inline rt_bool_t mmc_op_multi(rt_uint32_t opcode)
  248. {
  249. return opcode == MMC_WRITE_MULTIPLE_BLOCK ||
  250. opcode == MMC_READ_MULTIPLE_BLOCK;
  251. }
  252. rt_inline rt_bool_t mmc_op_tuning(rt_uint32_t opcode)
  253. {
  254. return opcode == MMC_SEND_TUNING_BLOCK ||
  255. opcode == MMC_SEND_TUNING_BLOCK_HS200;
  256. }
  257. rt_err_t rt_mmc_gpio_get_cd(struct rt_mmc_host *host);
  258. void rt_mmc_detect_change(struct rt_mmc_host *host, rt_ubase_t delay);
  259. rt_bool_t rt_mmc_can_gpio_ro(struct rt_mmc_host *host);
  260. rt_err_t rt_mmc_gpio_get_ro(struct rt_mmc_host *host);
  261. rt_err_t rt_mmc_send_abort_tuning(struct rt_mmc_host *host, rt_uint32_t opcode);
  262. #endif /* __DEV_SDHCI_HOST_H__ */