sdhci_host.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  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 __RT_SDHCI_MMC_H__
  11. #define __RT_SDHCI_MMC_H__
  12. #include <drivers/dev_mmcsd_core.h>
  13. #include <rtthread.h>
  14. #include <drivers/mmcsd_cmd.h>
  15. #include <drivers/dev_mmcsd_core.h>
  16. #include <drivers/mmcsd_host.h>
  17. #define 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 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,
  95. struct rt_mmcsd_io_cfg* ios);
  96. void (*hw_reset)(struct rt_mmc_host* host);
  97. void (*card_event)(struct rt_mmc_host* host);
  98. };
  99. struct regulator;
  100. struct mmc_pwrseq;
  101. struct mmc_supply
  102. {
  103. struct regulator *vmmc; /* Card power supply */
  104. struct regulator *vqmmc; /* Optional Vccq supply */
  105. };
  106. struct mmc_ctx
  107. {
  108. struct task_struct *task;
  109. };
  110. /* VDD voltage 3.3 ~ 3.4 */
  111. #define MMC_VDD_34_35 0x00400000 /* VDD voltage 3.4 ~ 3.5 */
  112. #define MMC_VDD_35_36 0x00800000 /* VDD voltage 3.5 ~ 3.6 */
  113. #define MMC_CAP2_HS200_1_8V_SDR MMCSD_SUP_HS200_1V8
  114. #define MMC_CAP_4_BIT_DATA MMCSD_BUSWIDTH_4
  115. #define MMC_CAP_8_BIT_DATA MMCSD_BUSWIDTH_8
  116. #define MMC_CAP2_HS200 MMCSD_SUP_HS200
  117. #define MMC_CAP_MMC_HIGHSPEED MMCSD_SUP_HIGHSPEED
  118. #define MMC_CAP_SD_HIGHSPEED MMCSD_SUP_HIGHSPEED
  119. #define MMC_CAP_1_8V_DDR MMCSD_SUP_DDR_1V8
  120. #define MMC_CAP_3_3V_DDR MMCSD_SUP_DDR_3V3
  121. #define MMC_CAP_1_2V_DDR MMCSD_SUP_DDR_1V2
  122. #define MMC_CAP_NONREMOVABLE MMCSD_SUP_NONREMOVABLE
  123. #define MMC_CAP_UHS_DDR50 0
  124. #define MMC_CAP2_HS400 0
  125. #define MMC_CAP_UHS_SDR50 0
  126. #define MMC_CAP_UHS_SDR25 0
  127. #define MMC_CAP_UHS_SDR12 0
  128. #define MMC_CAP_UHS_SDR104 0
  129. #define MMC_CAP_UHS 0
  130. #define MMC_CAP2_HSX00_1_8V 0
  131. #define MMC_CAP2_HS400_ES 0
  132. #define MMC_CAP_NEEDS_POLL 0
  133. #define MMC_CAP2_HSX00_1_2V 0
  134. #define MMC_CAP2_HS400_1_8V 0
  135. #define MMC_CAP_DRIVER_TYPE_D 0
  136. #define MMC_CAP_DRIVER_TYPE_C 0
  137. #define MMC_SET_DRIVER_TYPE_B 0
  138. #define MMC_CAP_DRIVER_TYPE_A 0
  139. #define MMC_CAP2_SDIO_IRQ_NOTHREAD 0
  140. #define MMC_CAP_CMD23 0
  141. #define MMC_CAP_SDIO_IRQ 0
  142. #define MMC_CAP2_NO_SDIO (1 << 19)
  143. #define MMC_CAP2_NO_SD (1 << 21)
  144. #define MMC_CAP2_NO_MMC (1 << 22)
  145. #define MMC_CAP2_CQE (1 << 23)
  146. #define MMC_VDD_165_195 VDD_165_195
  147. #define MMC_VDD_20_21 VDD_20_21
  148. #define MMC_VDD_29_30 VDD_29_30
  149. #define MMC_VDD_30_31 VDD_30_31
  150. #define MMC_VDD_32_33 VDD_32_33
  151. #define MMC_VDD_33_34 VDD_33_34
  152. struct rt_mmc_host
  153. {
  154. struct rt_mmcsd_host rthost;
  155. struct rt_device *parent;
  156. int index;
  157. const struct mmc_host_ops *ops;
  158. unsigned int f_min;
  159. unsigned int f_max;
  160. unsigned int f_init;
  161. rt_uint32_t ocr_avail;
  162. rt_uint32_t ocr_avail_sdio; /* SDIO-specific OCR */
  163. rt_uint32_t ocr_avail_sd; /* SD-specific OCR */
  164. rt_uint32_t ocr_avail_mmc; /* MMC-specific OCR */
  165. struct wakeup_source *ws; /* Enable consume of uevents */
  166. rt_uint32_t max_current_330;
  167. rt_uint32_t max_current_300;
  168. rt_uint32_t max_current_180;
  169. rt_uint32_t caps; /* Host capabilities */
  170. rt_uint32_t caps2; /* More host capabilities */
  171. /* host specific block data */
  172. unsigned int max_seg_size; /* see blk_queue_max_segment_size */
  173. unsigned short max_segs; /* see blk_queue_max_segments */
  174. unsigned short unused;
  175. unsigned int max_req_size; /* maximum number of bytes in one req */
  176. unsigned int max_blk_size; /* maximum size of one mmc block */
  177. unsigned int max_blk_count; /* maximum number of blocks in one req */
  178. unsigned int max_busy_timeout; /* max busy timeout in ms */
  179. struct rt_mmcsd_io_cfg ios; /* current io bus settings */
  180. unsigned int retune_period;
  181. /* group bitfields together to minimize padding */
  182. unsigned int use_spi_crc : 1;
  183. unsigned int claimed : 1; /* host exclusively claimed */
  184. unsigned int doing_init_tune : 1; /* initial tuning in progress */
  185. unsigned int can_retune : 1; /* re-tuning can be used */
  186. unsigned int doing_retune : 1; /* re-tuning in progress */
  187. unsigned int retune_now : 1; /* do re-tuning at next req */
  188. unsigned int retune_paused : 1; /* re-tuning is temporarily disabled */
  189. unsigned int retune_crc_disable : 1; /* don't trigger retune upon crc */
  190. unsigned int can_dma_map_merge : 1; /* merging can be used */
  191. unsigned int vqmmc_enabled : 1; /* vqmmc regulator is enabled */
  192. int need_retune; /* re-tuning is needed */
  193. int hold_retune; /* hold off re-tuning */
  194. rt_bool_t trigger_card_event; /* card_event necessary */
  195. unsigned int sdio_irqs;
  196. rt_bool_t sdio_irq_pending;
  197. struct led_trigger *led; /* activity led */
  198. struct mmc_supply supply;
  199. /* Ongoing data transfer that allows commands during transfer */
  200. struct rt_mmcsd_req *ongoing_mrq;
  201. unsigned int actual_clock; /* Actual HC clock rate */
  202. rt_uint32_t pm_caps;
  203. unsigned long private[];
  204. };
  205. static inline int mmc_card_is_removable(struct rt_mmc_host *host)
  206. {
  207. return !(host->caps & MMC_CAP_NONREMOVABLE);
  208. }
  209. struct device_node;
  210. struct rt_mmc_host *rt_mmc_alloc_host(int extra, struct rt_device *);
  211. int rt_mmc_add_host(struct rt_mmc_host *);
  212. void rt_mmc_remove_host(struct rt_mmc_host *);
  213. void rt_mmc_free_host(struct rt_mmc_host *);
  214. int rt_mmc_of_parse(struct rt_mmc_host *host);
  215. int rt_mmc_of_parse_voltage(struct rt_mmc_host *host, rt_uint32_t *mask);
  216. static inline void *mmc_priv(struct rt_mmc_host *host)
  217. {
  218. return (void *)host->private;
  219. }
  220. #define mmc_host_is_spi(host) ((host)->caps & MMC_CAP_SPI)
  221. #define mmc_dev(x) ((x)->parent)
  222. #define mmc_classdev(x) (&(x)->class_dev)
  223. #define mmc_hostname(x) (x->parent->parent.name)
  224. void rt_mmc_detect_change(struct rt_mmc_host *, unsigned long delay);
  225. void rt_mmc_request_done(struct rt_mmc_host *, struct rt_mmcsd_req *);
  226. void mmc_command_done(struct rt_mmc_host *host, struct rt_mmcsd_req *mrq);
  227. void mmc_cqe_request_done(struct rt_mmc_host *host, struct rt_mmcsd_req *mrq);
  228. static inline rt_bool_t sdio_irq_claimed(struct rt_mmc_host *host)
  229. {
  230. return host->sdio_irqs > 0;
  231. }
  232. static inline int mmc_regulator_set_ocr(struct rt_mmc_host *mmc,
  233. struct regulator *supply,
  234. unsigned short vdd_bit)
  235. {
  236. return 0;
  237. }
  238. int mmc_regulator_get_supply(struct rt_mmc_host *mmc);
  239. int mmc_regulator_enable_vqmmc(struct rt_mmc_host *mmc);
  240. void mmc_regulator_disable_vqmmc(struct rt_mmc_host *mmc);
  241. void mmc_retune_timer_stop(struct rt_mmc_host* host);
  242. enum dma_data_direction
  243. {
  244. DMA_BIDIRECTIONAL = 0,
  245. DMA_TO_DEVICE = 1,
  246. DMA_FROM_DEVICE = 2,
  247. DMA_NONE = 3,
  248. };
  249. #define DMA_BIT_MASK(n) (((n) == 64) ? ~0ULL : ((1ULL << (n)) - 1))
  250. static inline void mmc_retune_needed(struct rt_mmc_host *host)
  251. {
  252. if (host->can_retune)
  253. host->need_retune = 1;
  254. }
  255. static inline rt_bool_t mmc_can_retune(struct rt_mmc_host *host)
  256. {
  257. return host->can_retune == 1;
  258. }
  259. static inline rt_bool_t mmc_doing_retune(struct rt_mmc_host *host)
  260. {
  261. return host->doing_retune == 1;
  262. }
  263. static inline rt_bool_t mmc_doing_tune(struct rt_mmc_host *host)
  264. {
  265. return host->doing_retune == 1 || host->doing_init_tune == 1;
  266. }
  267. static inline int mmc_get_dma_dir(struct rt_mmcsd_data *data)
  268. {
  269. return data->flags & DATA_DIR_WRITE ? DMA_TO_DEVICE : DMA_FROM_DEVICE;
  270. }
  271. static inline rt_bool_t mmc_op_multi(rt_uint32_t opcode)
  272. {
  273. return opcode == MMC_WRITE_MULTIPLE_BLOCK || opcode == MMC_READ_MULTIPLE_BLOCK;
  274. }
  275. static inline rt_bool_t mmc_op_tuning(rt_uint32_t opcode)
  276. {
  277. return opcode == MMC_SEND_TUNING_BLOCK || opcode == MMC_SEND_TUNING_BLOCK_HS200;
  278. }
  279. int rt_mmc_gpio_get_cd(struct rt_mmc_host *host);
  280. void rt_mmc_detect_change(struct rt_mmc_host *host, unsigned long delay);
  281. int rt_mmc_regulator_set_vqmmc(struct rt_mmc_host *mmc, struct rt_mmcsd_io_cfg *ios);
  282. rt_bool_t rt_mmc_can_gpio_ro(struct rt_mmc_host *host);
  283. int rt_mmc_gpio_get_ro(struct rt_mmc_host *host);
  284. int rt_mmc_send_abort_tuning(struct rt_mmc_host *host, rt_uint32_t opcode);
  285. int rt_mmc_of_parse(struct rt_mmc_host *host);
  286. #endif