drv_sdio.h 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. /*
  2. * Copyright (c) 2006-2018, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2018-12-13 BalanceTWK first version
  9. */
  10. #ifndef _DRV_SDIO_H
  11. #define _DRV_SDIO_H
  12. #include <rtthread.h>
  13. #include "rtdevice.h"
  14. #include <rthw.h>
  15. #include <drv_common.h>
  16. #include "drv_dma.h"
  17. #include <string.h>
  18. #include <drivers/mmcsd_core.h>
  19. #include <drivers/sdio.h>
  20. #define SDIO_BUFF_SIZE 4096
  21. #define SDIO_MAX_FREQ 2000000
  22. #define SDIO_ALIGN_LEN 32
  23. #ifndef SDIO_BASE_ADDRESS
  24. #define SDIO_BASE_ADDRESS (0x40012800U)
  25. #endif
  26. #ifndef SDIO_CLOCK_FREQ
  27. #define SDIO_CLOCK_FREQ (48U * 1000 * 1000)
  28. #endif
  29. #ifndef SDIO_BUFF_SIZE
  30. #define SDIO_BUFF_SIZE (4096)
  31. #endif
  32. #ifndef SDIO_ALIGN_LEN
  33. #define SDIO_ALIGN_LEN (32)
  34. #endif
  35. #ifndef SDIO_MAX_FREQ
  36. #define SDIO_MAX_FREQ (24 * 1000 * 1000)
  37. #endif
  38. #define HW_SDIO_IT_CCRCFAIL (0x01U << 0)
  39. #define HW_SDIO_IT_DCRCFAIL (0x01U << 1)
  40. #define HW_SDIO_IT_CTIMEOUT (0x01U << 2)
  41. #define HW_SDIO_IT_DTIMEOUT (0x01U << 3)
  42. #define HW_SDIO_IT_TXUNDERR (0x01U << 4)
  43. #define HW_SDIO_IT_RXOVERR (0x01U << 5)
  44. #define HW_SDIO_IT_CMDREND (0x01U << 6)
  45. #define HW_SDIO_IT_CMDSENT (0x01U << 7)
  46. #define HW_SDIO_IT_DATAEND (0x01U << 8)
  47. #define HW_SDIO_IT_STBITERR (0x01U << 9)
  48. #define HW_SDIO_IT_DBCKEND (0x01U << 10)
  49. #define HW_SDIO_IT_CMDACT (0x01U << 11)
  50. #define HW_SDIO_IT_TXACT (0x01U << 12)
  51. #define HW_SDIO_IT_RXACT (0x01U << 13)
  52. #define HW_SDIO_IT_TXFIFOHE (0x01U << 14)
  53. #define HW_SDIO_IT_RXFIFOHF (0x01U << 15)
  54. #define HW_SDIO_IT_TXFIFOF (0x01U << 16)
  55. #define HW_SDIO_IT_RXFIFOF (0x01U << 17)
  56. #define HW_SDIO_IT_TXFIFOE (0x01U << 18)
  57. #define HW_SDIO_IT_RXFIFOE (0x01U << 19)
  58. #define HW_SDIO_IT_TXDAVL (0x01U << 20)
  59. #define HW_SDIO_IT_RXDAVL (0x01U << 21)
  60. #define HW_SDIO_IT_SDIOIT (0x01U << 22)
  61. #define HW_SDIO_ERRORS \
  62. (HW_SDIO_IT_CCRCFAIL | HW_SDIO_IT_CTIMEOUT | \
  63. HW_SDIO_IT_DCRCFAIL | HW_SDIO_IT_DTIMEOUT | \
  64. HW_SDIO_IT_RXOVERR | HW_SDIO_IT_TXUNDERR)
  65. #define HW_SDIO_POWER_OFF (0x00U)
  66. #define HW_SDIO_POWER_UP (0x02U)
  67. #define HW_SDIO_POWER_ON (0x03U)
  68. #define HW_SDIO_FLOW_ENABLE (0x01U << 14)
  69. #define HW_SDIO_BUSWIDE_1B (0x00U << 11)
  70. #define HW_SDIO_BUSWIDE_4B (0x01U << 11)
  71. #define HW_SDIO_BUSWIDE_8B (0x02U << 11)
  72. #define HW_SDIO_BYPASS_ENABLE (0x01U << 10)
  73. #define HW_SDIO_IDLE_ENABLE (0x01U << 9)
  74. #define HW_SDIO_CLK_ENABLE (0x01U << 8)
  75. #define HW_SDIO_SUSPEND_CMD (0x01U << 11)
  76. #define HW_SDIO_CPSM_ENABLE (0x01U << 10)
  77. #define HW_SDIO_WAIT_END (0x01U << 9)
  78. #define HW_SDIO_WAIT_INT (0x01U << 8)
  79. #define HW_SDIO_RESPONSE_NO (0x00U << 6)
  80. #define HW_SDIO_RESPONSE_SHORT (0x01U << 6)
  81. #define HW_SDIO_RESPONSE_LONG (0x03U << 6)
  82. #define HW_SDIO_DATA_LEN_MASK (0x01FFFFFFU)
  83. #define HW_SDIO_IO_ENABLE (0x01U << 11)
  84. #define HW_SDIO_RWMOD_CK (0x01U << 10)
  85. #define HW_SDIO_RWSTOP_ENABLE (0x01U << 9)
  86. #define HW_SDIO_RWSTART_ENABLE (0x01U << 8)
  87. #define HW_SDIO_DBLOCKSIZE_1 (0x00U << 4)
  88. #define HW_SDIO_DBLOCKSIZE_2 (0x01U << 4)
  89. #define HW_SDIO_DBLOCKSIZE_4 (0x02U << 4)
  90. #define HW_SDIO_DBLOCKSIZE_8 (0x03U << 4)
  91. #define HW_SDIO_DBLOCKSIZE_16 (0x04U << 4)
  92. #define HW_SDIO_DBLOCKSIZE_32 (0x05U << 4)
  93. #define HW_SDIO_DBLOCKSIZE_64 (0x06U << 4)
  94. #define HW_SDIO_DBLOCKSIZE_128 (0x07U << 4)
  95. #define HW_SDIO_DBLOCKSIZE_256 (0x08U << 4)
  96. #define HW_SDIO_DBLOCKSIZE_512 (0x09U << 4)
  97. #define HW_SDIO_DBLOCKSIZE_1024 (0x0AU << 4)
  98. #define HW_SDIO_DBLOCKSIZE_2048 (0x0BU << 4)
  99. #define HW_SDIO_DBLOCKSIZE_4096 (0x0CU << 4)
  100. #define HW_SDIO_DBLOCKSIZE_8192 (0x0DU << 4)
  101. #define HW_SDIO_DBLOCKSIZE_16384 (0x0EU << 4)
  102. #define HW_SDIO_DMA_ENABLE (0x01U << 3)
  103. #define HW_SDIO_STREAM_ENABLE (0x01U << 2)
  104. #define HW_SDIO_TO_HOST (0x01U << 1)
  105. #define HW_SDIO_DPSM_ENABLE (0x01U << 0)
  106. #define HW_SDIO_DATATIMEOUT (0xF0000000U)
  107. struct stm32_sdio
  108. {
  109. volatile rt_uint32_t power;
  110. volatile rt_uint32_t clkcr;
  111. volatile rt_uint32_t arg;
  112. volatile rt_uint32_t cmd;
  113. volatile rt_uint32_t respcmd;
  114. volatile rt_uint32_t resp1;
  115. volatile rt_uint32_t resp2;
  116. volatile rt_uint32_t resp3;
  117. volatile rt_uint32_t resp4;
  118. volatile rt_uint32_t dtimer;
  119. volatile rt_uint32_t dlen;
  120. volatile rt_uint32_t dctrl;
  121. volatile rt_uint32_t dcount;
  122. volatile rt_uint32_t sta;
  123. volatile rt_uint32_t icr;
  124. volatile rt_uint32_t mask;
  125. volatile rt_uint32_t reserved0[2];
  126. volatile rt_uint32_t fifocnt;
  127. volatile rt_uint32_t reserved1[13];
  128. volatile rt_uint32_t fifo;
  129. };
  130. typedef rt_err_t (*dma_txconfig)(rt_uint32_t *src, rt_uint32_t *dst, int size);
  131. typedef rt_err_t (*dma_rxconfig)(rt_uint32_t *src, rt_uint32_t *dst, int size);
  132. typedef rt_uint32_t (*sdio_clk_get)(struct stm32_sdio *hw_sdio);
  133. struct stm32_sdio_des
  134. {
  135. struct stm32_sdio *hw_sdio;
  136. dma_txconfig txconfig;
  137. dma_rxconfig rxconfig;
  138. sdio_clk_get clk_get;
  139. };
  140. struct stm32_sdio_config
  141. {
  142. SDIO_TypeDef *Instance;
  143. struct dma_config dma_rx, dma_tx;
  144. };
  145. /* stm32 sdio dirver class */
  146. struct stm32_sdio_class
  147. {
  148. struct stm32_sdio_des *des;
  149. const struct stm32_sdio_config *cfg;
  150. struct rt_mmcsd_host host;
  151. struct
  152. {
  153. DMA_HandleTypeDef handle_rx;
  154. DMA_HandleTypeDef handle_tx;
  155. } dma;
  156. };
  157. #endif