sdio-dw.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  1. /*
  2. * Copyright (c) 2006-2022, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2022-12-06 GuEe-GUI first version
  9. */
  10. #ifndef __SDIO_DW_H__
  11. #define __SDIO_DW_H__
  12. #include "../dev_sdio_dm.h"
  13. #define SDIO_DW_CTRL 0x000
  14. #define SDIO_DW_PWREN 0x004
  15. #define SDIO_DW_CLKDIV 0x008
  16. #define SDIO_DW_CLKSRC 0x00c
  17. #define SDIO_DW_CLKENA 0x010
  18. #define SDIO_DW_TMOUT 0x014
  19. #define SDIO_DW_CTYPE 0x018
  20. #define SDIO_DW_BLKSIZ 0x01c
  21. #define SDIO_DW_BYTCNT 0x020
  22. #define SDIO_DW_INTMASK 0x024
  23. #define SDIO_DW_CMDARG 0x028
  24. #define SDIO_DW_CMD 0x02c
  25. #define SDIO_DW_RESP0 0x030
  26. #define SDIO_DW_RESP1 0x034
  27. #define SDIO_DW_RESP2 0x038
  28. #define SDIO_DW_RESP3 0x03c
  29. #define SDIO_DW_MINTSTS 0x040
  30. #define SDIO_DW_RINTSTS 0x044
  31. #define SDIO_DW_STATUS 0x048
  32. #define SDIO_DW_FIFOTH 0x04c
  33. #define SDIO_DW_CDETECT 0x050
  34. #define SDIO_DW_WRTPRT 0x054
  35. #define SDIO_DW_GPIO 0x058
  36. #define SDIO_DW_TCBCNT 0x05c
  37. #define SDIO_DW_TBBCNT 0x060
  38. #define SDIO_DW_DEBNCE 0x064
  39. #define SDIO_DW_USRID 0x068
  40. #define SDIO_DW_VERID 0x06c
  41. #define SDIO_DW_HCON 0x070
  42. #define SDIO_DW_UHS_REG 0x074
  43. #define SDIO_DW_RST_N 0x078
  44. #define SDIO_DW_BMOD 0x080
  45. #define SDIO_DW_PLDMND 0x084
  46. #define SDIO_DW_DBADDR 0x088
  47. #define SDIO_DW_IDSTS 0x08c
  48. #define SDIO_DW_IDINTEN 0x090
  49. #define SDIO_DW_DSCADDR 0x094
  50. #define SDIO_DW_BUFADDR 0x098
  51. #define SDIO_DW_CDTHRCTL 0x100
  52. #define SDIO_DW_UHS_REG_EXT 0x108
  53. #define SDIO_DW_DDR_REG 0x10c
  54. #define SDIO_DW_ENABLE_SHIFT 0x110
  55. #define SDIO_DW_DATA(x) (x)
  56. /*
  57. * Registers to support idmac 64-bit address mode
  58. */
  59. #define SDIO_DW_DBADDRL 0x088
  60. #define SDIO_DW_DBADDRU 0x08c
  61. #define SDIO_DW_IDSTS64 0x090
  62. #define SDIO_DW_IDINTEN64 0x094
  63. #define SDIO_DW_DSCADDRL 0x098
  64. #define SDIO_DW_DSCADDRU 0x09c
  65. #define SDIO_DW_BUFADDRL 0x0a0
  66. #define SDIO_DW_BUFADDRU 0x0a4
  67. /* Support for longer data read timeout */
  68. #define SDIO_DW_QUIRK_EXTENDED_TMOUT RT_BIT(0)
  69. #define SDIO_DW_240A 0x240a
  70. #define SDIO_DW_280A 0x280a
  71. /*
  72. * Data offset is difference according to Version
  73. * Lower than 2.40a : data register offest is 0x100
  74. */
  75. #define DATA_OFFSET 0x100
  76. #define DATA_240A_OFFSET 0x200
  77. /* Control register defines */
  78. #define SDIO_DW_CTRL_USE_IDMAC RT_BIT(25)
  79. #define SDIO_DW_CTRL_CEATA_INT_EN RT_BIT(11)
  80. #define SDIO_DW_CTRL_SEND_AS_CCSD RT_BIT(10)
  81. #define SDIO_DW_CTRL_SEND_CCSD RT_BIT(9)
  82. #define SDIO_DW_CTRL_ABRT_READ_DATA RT_BIT(8)
  83. #define SDIO_DW_CTRL_SEND_IRQ_RESP RT_BIT(7)
  84. #define SDIO_DW_CTRL_READ_WAIT RT_BIT(6)
  85. #define SDIO_DW_CTRL_DMA_ENABLE RT_BIT(5)
  86. #define SDIO_DW_CTRL_INT_ENABLE RT_BIT(4)
  87. #define SDIO_DW_CTRL_DMA_RESET RT_BIT(2)
  88. #define SDIO_DW_CTRL_FIFO_RESET RT_BIT(1)
  89. #define SDIO_DW_CTRL_RESET RT_BIT(0)
  90. /* Clock Enable register defines */
  91. #define SDIO_DW_CLKEN_LOW_PWR RT_BIT(16)
  92. #define SDIO_DW_CLKEN_ENABLE RT_BIT(0)
  93. /* Time-out register defines */
  94. #define SDIO_DW_TMOUT_DATA(n) ((n) << 8)
  95. #define SDIO_DW_TMOUT_DATA_MSK 0xffffff00
  96. #define SDIO_DW_TMOUT_RESP(n) ((n) & 0xff)
  97. #define SDIO_DW_TMOUT_RESP_MSK 0xff
  98. /* Card-type register defines */
  99. #define SDIO_DW_CTYPE_8BIT RT_BIT(16)
  100. #define SDIO_DW_CTYPE_4BIT RT_BIT(0)
  101. #define SDIO_DW_CTYPE_1BIT 0
  102. /* Interrupt status & mask register defines */
  103. #define SDIO_DW_INT_SDIO(n) RT_BIT(16 + (n))
  104. #define SDIO_DW_INT_RAW_SDIO RT_BIT(24)
  105. #define SDIO_DW_INT_EBE RT_BIT(15)
  106. #define SDIO_DW_INT_ACD RT_BIT(14)
  107. #define SDIO_DW_INT_SBE RT_BIT(13)
  108. #define SDIO_DW_INT_HLE RT_BIT(12)
  109. #define SDIO_DW_INT_FRUN RT_BIT(11)
  110. #define SDIO_DW_INT_HTO RT_BIT(10)
  111. #define SDIO_DW_INT_VOLT_SWITCH RT_BIT(10)
  112. #define SDIO_DW_INT_DRTO RT_BIT(9)
  113. #define SDIO_DW_INT_RTO RT_BIT(8)
  114. #define SDIO_DW_INT_DCRC RT_BIT(7)
  115. #define SDIO_DW_INT_RCRC RT_BIT(6)
  116. #define SDIO_DW_INT_RXDR RT_BIT(5)
  117. #define SDIO_DW_INT_TXDR RT_BIT(4)
  118. #define SDIO_DW_INT_DATA_OVER RT_BIT(3)
  119. #define SDIO_DW_INT_CMD_DONE RT_BIT(2)
  120. #define SDIO_DW_INT_RESP_ERR RT_BIT(1)
  121. #define SDIO_DW_INT_CD RT_BIT(0)
  122. #define SDIO_DW_INT_ERROR 0xbfc2
  123. /* Command register defines */
  124. #define SDIO_DW_CMD_START RT_BIT(31)
  125. #define SDIO_DW_CMD_USE_HOLD_REG RT_BIT(29)
  126. #define SDIO_DW_CMD_VOLT_SWITCH RT_BIT(28)
  127. #define SDIO_DW_CMD_CCS_EXP RT_BIT(23)
  128. #define SDIO_DW_CMD_CEATA_RD RT_BIT(22)
  129. #define SDIO_DW_CMD_UPD_CLK RT_BIT(21)
  130. #define SDIO_DW_CMD_INIT RT_BIT(15)
  131. #define SDIO_DW_CMD_STOP RT_BIT(14)
  132. #define SDIO_DW_CMD_PRV_DAT_WAIT RT_BIT(13)
  133. #define SDIO_DW_CMD_SEND_STOP RT_BIT(12)
  134. #define SDIO_DW_CMD_STRM_MODE RT_BIT(11)
  135. #define SDIO_DW_CMD_DAT_WR RT_BIT(10)
  136. #define SDIO_DW_CMD_DAT_EXP RT_BIT(9)
  137. #define SDIO_DW_CMD_RESP_CRC RT_BIT(8)
  138. #define SDIO_DW_CMD_RESP_LONG RT_BIT(7)
  139. #define SDIO_DW_CMD_RESP_EXP RT_BIT(6)
  140. #define SDIO_DW_CMD_INDX(n) ((n) & 0x1f)
  141. /* Status register defines */
  142. #define SDIO_DW_GET_FCNT(x) (((x) >> 17) & 0x1fff)
  143. #define SDIO_DW_STATUS_DMA_REQ RT_BIT(31)
  144. #define SDIO_DW_STATUS_BUSY RT_BIT(9)
  145. /* FIFOTH register defines */
  146. #define SDIO_DW_SET_FIFOTH(m, r, t) (((m) & 0x7) << 28 | ((r) & 0xfff) << 16 | ((t) & 0xfff))
  147. /* HCON register defines */
  148. #define DMA_INTERFACE_IDMA (0x0)
  149. #define DMA_INTERFACE_DWDMA (0x1)
  150. #define DMA_INTERFACE_GDMA (0x2)
  151. #define DMA_INTERFACE_NODMA (0x3)
  152. #define SDIO_DW_GET_TRANS_MODE(x) (((x) >> 16) & 0x3)
  153. #define SDIO_DW_GET_SLOT_NUM(x) ((((x) >> 1) & 0x1f) + 1)
  154. #define SDIO_DW_GET_HDATA_WIDTH(x) (((x) >> 7) & 0x7)
  155. #define SDIO_DW_GET_ADDR_CONFIG(x) (((x) >> 27) & 0x1)
  156. /* Internal DMAC interrupt defines */
  157. #define SDIO_DW_IDMAC_INT_AI RT_BIT(9)
  158. #define SDIO_DW_IDMAC_INT_NI RT_BIT(8)
  159. #define SDIO_DW_IDMAC_INT_CES RT_BIT(5)
  160. #define SDIO_DW_IDMAC_INT_DU RT_BIT(4)
  161. #define SDIO_DW_IDMAC_INT_FBE RT_BIT(2)
  162. #define SDIO_DW_IDMAC_INT_RI RT_BIT(1)
  163. #define SDIO_DW_IDMAC_INT_TI RT_BIT(0)
  164. /* Internal DMAC bus mode bits */
  165. #define SDIO_DW_IDMAC_ENABLE RT_BIT(7)
  166. #define SDIO_DW_IDMAC_FB RT_BIT(1)
  167. #define SDIO_DW_IDMAC_SWRESET RT_BIT(0)
  168. /* H/W reset */
  169. #define SDIO_DW_RST_HWACTIVE 0x1
  170. /* Version ID register define */
  171. #define SDIO_DW_GET_VERID(x) ((x) & 0xffff)
  172. /* Card read threshold */
  173. #define SDIO_DW_SET_THLD(v, x) (((v) & 0xfff) << 16 | (x))
  174. #define SDIO_DW_CARD_WR_THR_EN RT_BIT(2)
  175. #define SDIO_DW_CARD_RD_THR_EN RT_BIT(0)
  176. /* UHS-1 register defines */
  177. #define SDIO_DW_UHS_DDR RT_BIT(16)
  178. #define SDIO_DW_UHS_18V RT_BIT(0)
  179. /* DDR register defines */
  180. #define SDIO_DW_DDR_HS400 RT_BIT(31)
  181. /* Enable shift register defines */
  182. #define SDIO_DW_ENABLE_PHASE RT_BIT(0)
  183. /* All ctrl reset bits */
  184. #define SDIO_DW_CTRL_ALL_RESET_FLAGS (SDIO_DW_CTRL_RESET | SDIO_DW_CTRL_FIFO_RESET | SDIO_DW_CTRL_DMA_RESET)
  185. struct rt_dma_chan;
  186. struct sdio_dw
  187. {
  188. struct rt_device parent;
  189. struct rt_device *bus_dev;
  190. struct rt_workqueue *state_wq;
  191. struct rt_work state_work;
  192. void *base;
  193. void *fifo_base;
  194. rt_ubase_t base_phy;
  195. rt_uint32_t data_addr_override;
  196. rt_bool_t wm_aligned;
  197. int irq;
  198. int sdio_id0;
  199. rt_uint32_t verid;
  200. rt_uint32_t quirks;
  201. rt_uint32_t bus_hz;
  202. rt_bool_t fifo_mode;
  203. rt_uint32_t fifo_depth;
  204. rt_uint32_t fifoth_val;
  205. rt_uint32_t detect_delay_ms;
  206. rt_uint32_t current_speed;
  207. rt_uint32_t minimum_speed;
  208. void *priv;
  209. rt_uint32_t vqmmc_enabled;
  210. rt_uint32_t cmd_status;
  211. rt_uint32_t data_status;
  212. rt_uint32_t stop_cmdr;
  213. rt_uint32_t dir_status;
  214. #define STATE_IDLE 0
  215. #define STATE_SENDING_CMD 1
  216. #define STATE_SENDING_DATA 2
  217. #define STATE_DATA_BUSY 3
  218. #define STATE_SENDING_STOP 4
  219. #define STATE_DATA_ERROR 5
  220. #define STATE_SENDING_CMD11 6
  221. #define STATE_WAITING_CMD11_DONE 7
  222. rt_uint32_t state;
  223. #define EVENT_CMD_COMPLETE 0
  224. #define EVENT_XFER_COMPLETE 1
  225. #define EVENT_DATA_COMPLETE 2
  226. #define EVENT_DATA_ERROR 3
  227. rt_bitmap_t pending_events;
  228. struct rt_mmcsd_req *req;
  229. struct rt_mmcsd_data *data;
  230. struct rt_mmcsd_cmd *cmd;
  231. struct rt_mmcsd_cmd stop_abort;
  232. rt_uint32_t prev_blksz;
  233. rt_uint8_t timing;
  234. struct rt_clk *biu_clk;
  235. struct rt_clk *ciu_clk;
  236. void *last_buf;
  237. rt_uint32_t last_remain;
  238. int data_shift;
  239. rt_uint8_t part_buf_start;
  240. rt_uint8_t part_buf_count;
  241. union
  242. {
  243. rt_uint64_t part_buf;
  244. rt_uint16_t part_buf16;
  245. rt_uint32_t part_buf32;
  246. rt_uint64_t part_buf64;
  247. };
  248. void (*push_data)(struct sdio_dw *sd, void *buf, int cnt);
  249. void (*pull_data)(struct sdio_dw *sd, void *buf, int cnt);
  250. /* DMA interface members */
  251. #define TRANS_MODE_PIO 0
  252. #define TRANS_MODE_IDMAC 1
  253. #define TRANS_MODE_EDMAC 2
  254. rt_bool_t use_dma;
  255. rt_bool_t using_dma;
  256. rt_bool_t dma_64bit_address;
  257. rt_size_t ring_size;
  258. void *dma_buf;
  259. rt_ubase_t dma_buf_phy;
  260. struct rt_dma_chan *edma_chan;
  261. const struct sdio_dw_dma_ops *dma_ops;
  262. struct rt_timer cmd11_timer;
  263. struct rt_timer cto_timer;
  264. struct rt_timer dto_timer;
  265. struct rt_reset_control *rstc;
  266. struct sdio_dw_slot *slot;
  267. const struct sdio_dw_drv_data *drv_data;
  268. struct rt_spinlock lock, irq_lock;
  269. };
  270. /* DMA ops for Internal/External DMAC interface */
  271. struct sdio_dw_dma_ops
  272. {
  273. rt_err_t (*init)(struct sdio_dw *sd);
  274. rt_err_t (*start)(struct sdio_dw *sd);
  275. rt_err_t (*complete)(struct sdio_dw *sd);
  276. rt_err_t (*stop)(struct sdio_dw *sd);
  277. rt_err_t (*cleanup)(struct sdio_dw *sd);
  278. rt_err_t (*exit)(struct sdio_dw *sd);
  279. };
  280. struct sdio_dw_slot
  281. {
  282. struct rt_mmcsd_host *host;
  283. struct sdio_dw *sd;
  284. rt_uint32_t ctype;
  285. struct rt_mmcsd_req *req;
  286. rt_uint32_t clock;
  287. rt_uint32_t clk_old;
  288. #define DW_MMC_CARD_PRESENT 0
  289. #define DW_MMC_CARD_NEED_INIT 1
  290. #define DW_MMC_CARD_NO_LOW_PWR 2
  291. #define DW_MMC_CARD_NO_USE_HOLD 3
  292. #define DW_MMC_CARD_NEEDS_POLL 4
  293. rt_bitmap_t flags;
  294. int id;
  295. int sdio_id;
  296. };
  297. struct sdio_dw_drv_data
  298. {
  299. rt_ubase_t *caps;
  300. rt_uint32_t num_caps;
  301. rt_uint32_t common_caps;
  302. rt_err_t (*init)(struct sdio_dw *sd);
  303. rt_err_t (*set_iocfg)(struct sdio_dw *sd, struct rt_mmcsd_io_cfg *ios);
  304. rt_err_t (*parse_ofw)(struct sdio_dw *sd);
  305. rt_err_t (*execute_tuning)(struct sdio_dw_slot *slot, rt_uint32_t opcode);
  306. rt_err_t (*prepare_hs400_tuning)(struct sdio_dw *sd, struct rt_mmcsd_io_cfg *ios);
  307. rt_err_t (*switch_voltage)(struct rt_mmcsd_host *host, struct rt_mmcsd_io_cfg *ios);
  308. rt_err_t (*set_data_timeout)(struct sdio_dw *sd, rt_uint32_t timeout_ns);
  309. rt_uint32_t (*get_drto_clks)(struct sdio_dw *sd);
  310. };
  311. #define sdio_dw_writel(sd, reg, val) HWREG32((sd)->base + SDIO_DW_##reg) = (val)
  312. #define sdio_dw_writew(sd, reg, val) HWREG16((sd)->base + SDIO_DW_##reg) = (val)
  313. #define sdio_dw_writeb(sd, reg, val) HWREG8((sd)->base + SDIO_DW_##reg) = (val)
  314. #define sdio_dw_readl(sd, reg) HWREG32((sd)->base + SDIO_DW_##reg)
  315. #define sdio_dw_readw(sd, reg) HWREG16((sd)->base + SDIO_DW_##reg)
  316. #define sdio_dw_readb(sd, reg) HWREG8((sd)->base + SDIO_DW_##reg)
  317. #define sdio_dw_fifo_writew(sd, val) HWREG16((sd)->fifo_base) = (val)
  318. #define sdio_dw_fifo_writel(sd, val) HWREG32((sd)->fifo_base) = (val)
  319. #define sdio_dw_fifo_writeq(sd, val) HWREG64((sd)->fifo_base) = (val)
  320. #define sdio_dw_fifo_readw(sd) HWREG16((sd)->fifo_base)
  321. #define sdio_dw_fifo_readl(sd) HWREG32((sd)->fifo_base)
  322. #define sdio_dw_fifo_readq(sd) HWREG64((sd)->fifo_base)
  323. rt_err_t sdio_dw_probe(struct sdio_dw *sd);
  324. rt_err_t sdio_dw_remove(struct sdio_dw *sd);
  325. #endif /* __SDIO_DW_H__ */