wm_hostspi.h 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. /**
  2. * @file wm_hostspi.h
  3. *
  4. * @brief host spi Driver Module
  5. *
  6. * @author dave
  7. *
  8. * Copyright (c) 2015 Winner Microelectronics Co., Ltd.
  9. */
  10. #ifndef WM_HOST_SPI_H
  11. #define WM_HOST_SPI_H
  12. #include "wm_type_def.h"
  13. #include "list.h"
  14. #include "wm_osal.h"
  15. #include "wm_ram_config.h"
  16. #define SPI_USE_DMA
  17. #define SPI_DMA_CMD_MAX_SIZE (0x20)
  18. #define SPI_DMA_BUF_MAX_SIZE (8160)
  19. #define SPI_DMA_MAX_TRANS_SIZE (4092)
  20. /**
  21. * error code.
  22. */
  23. #define TLS_SPI_STATUS_OK (0)
  24. #define TLS_SPI_STATUS_EINVAL (-1)
  25. #define TLS_SPI_STATUS_ENOMEM (-2)
  26. #define TLS_SPI_STATUS_EBUSY (-3)
  27. #define TLS_SPI_STATUS_ESHUTDOWN (-4)
  28. #define TLS_SPI_STATUS_EPERM (-5)
  29. #define TLS_SPI_STATUS_ECLKNOSUPPORT (-6)
  30. #define TLS_SPI_STATUS_EMODENOSUPPORT (-7)
  31. #define SPI_MASTER_FIFO_SIZE (32)
  32. /**
  33. * the SPI master controller's configuration data.
  34. */
  35. /** configuration data. */
  36. #define SPI_CPHA (0x01) /** clock phase. */
  37. #define SPI_CPOL (0x02) /** clock polarity. */
  38. #define TLS_SPI_MODE_0 (0|0) /** motorola mode. */
  39. #define TLS_SPI_MODE_1 (0|SPI_CPHA)
  40. #define TLS_SPI_MODE_2 (SPI_CPOL|0)
  41. #define TLS_SPI_MODE_3 (SPI_CPOL|SPI_CPHA)
  42. #define TLS_SPI_CS_LOW 0x00 /** chipselect active low. */
  43. #define TLS_SPI_CS_HIGH 0x01 /** chipselect active high. */
  44. #define TLS_SPI_FCLK_MIN (1000) /** minimum work clock rate(Hz). */
  45. #define TLS_SPI_FCLK_MAX (APB_CLK/2) /** maximum work clock rate(Hz). */
  46. /** default configuration data. */
  47. #define SPI_DEFAULT_SPEED (2000000) /** default clock rate is 2MHz. */
  48. #define SPI_DEFAULT_MODE (TLS_SPI_MODE_0) /** default mode MODE_0. */
  49. #define SPI_CS_ACTIVE_MODE (TLS_SPI_CS_LOW) /** default chipselect mode is active low. */
  50. #define SPI_CS_INACTIVE_MODE (TLS_SPI_CS_HIGH)
  51. /** SPI transaction message status. */
  52. #define SPI_MESSAGE_STATUS_IDLE (0)
  53. #define SPI_MESSAGE_STATUS_INPROGRESS (1)
  54. #define SPI_MESSAGE_STATUS_DONE (2)
  55. /**slave type*/
  56. #define SPI_SLAVE_FLASH 0 /**flash */
  57. #define SPI_SLAVE_CARD 1 /** SD card */
  58. #define SPI_SLAVE_CONTROL_PIN 0
  59. /**transfer type*/
  60. #define SPI_BYTE_TRANSFER 0 /**byte transfer*/
  61. #define SPI_WORD_TRANSFER 1 /**word transfer*/
  62. #define SPI_DMA_TRANSFER 2 /** DMA transfer */
  63. /**
  64. * a read/write buffer pair
  65. *
  66. * SPI transfers always write the same number of bytes as they read.
  67. * If the transmit buffer is null, zeroes will be shifted out while
  68. * filling rx_buf. If the receive buffer is null, the data shifted in
  69. * will be discarded.
  70. */
  71. struct tls_spi_transfer
  72. {
  73. struct dl_list transfer_list; /**< transfers are sequenced through
  74. tls_spi_message.transfers. */
  75. const void *tx_buf; /**< data to be written, or NULL. */
  76. void *rx_buf; /**< data to be read, or NULL. */
  77. u32 len; /**< size of rx and tx buffers (in bytes). */
  78. u32 delay_usecs; /**< microseconds to delay after this transfer. */
  79. };
  80. /**
  81. * one multi-segment SPI transaction
  82. *
  83. * A struct tls_spi_message is used to execute an atomic sequence of data
  84. * transfers, each represented by a struct tls_spi_transfer. The sequence
  85. * is "atomic" in the sense that no other spi_message may use that SPI bus
  86. * until that sequence completes.
  87. */
  88. struct tls_spi_message
  89. {
  90. struct dl_list queue; /**< transaction messages are sequenced through
  91. tls_spi_port.wait_queue. */
  92. struct dl_list transfers; /**< list of transfer segments in this transaction. */
  93. void (*complete) (void *); /**< called to report transaction completions. */
  94. void *context; /**< the argument to complete() when it's called. */
  95. u32 status; /**< transaction message status. */
  96. };
  97. /**
  98. * driver structure to SPI master controller
  99. *
  100. * This data structure presents the SPI master controller's configuration
  101. * data. The device attached to this SPI master controller share the same
  102. * transfer mode, chipselect mode and clock rate. And this structure maintains
  103. * a queue of tls_spi_message transactions and uses this tls_spi_message transaction
  104. * to access to the SPI device. For each such message it queues, it calls the message's
  105. * completion function when the transaction completes.
  106. */
  107. struct tls_spi_port
  108. {
  109. u32 speed_hz; /**< clock rate to be used. */
  110. u8 cs_active; /**< chipselect mode, maybe active low or active
  111. high. */
  112. u8 mode; /**< SPI transfer mode: mode_0(CPHA=0, CHOL=0),
  113. mode_1(CPHA=0, CHOL=1), mode_2(CPHA=1,
  114. CHOL=0), mode_3(CPHA=1, CHOL=1). */
  115. u8 reconfig;
  116. struct dl_list wait_queue; /**< wait list of transaction messages. */
  117. tls_os_queue_t *lock;
  118. tls_os_queue_t *msg_queue; /**< notify the schedule thread that there's
  119. transaction message queued. */
  120. struct tls_spi_message *current_message; /**< current transaction message
  121. in-progressing. */
  122. u32 current_remaining_transfer; /**< remaining transfer segments count in
  123. current transaction message. */
  124. struct tls_spi_transfer *current_transfer; /**< current transfer segment
  125. in-progressing. */
  126. u32 current_remaining_bytes; /**< remaining data length in current
  127. transfer segment. */
  128. u8 transtype; /**< transfer type */
  129. };
  130. /**
  131. * @defgroup Driver_APIs Driver APIs
  132. * @brief Driver APIs
  133. */
  134. /**
  135. * @addtogroup Driver_APIs
  136. * @{
  137. */
  138. /**
  139. * @defgroup MASTERSPI_Driver_APIs MASTER SPI Driver APIs
  140. * @brief MASTERSPI driver APIs
  141. */
  142. /**
  143. * @addtogroup MASTERSPI_Driver_APIs
  144. * @{
  145. */
  146. /**
  147. * @brief This function is used to initialize the SPI master driver.
  148. *
  149. * @param[in] None
  150. *
  151. * @retval TLS_SPI_STATUS_OK if initialize success
  152. * @retval TLS_SPI_STATUS_EBUSY if SPI is already initialized
  153. * @retval TLS_SPI_STATUS_ENOMEM if malloc SPI memory fail
  154. *
  155. * @note None
  156. */
  157. int tls_spi_init(void);
  158. /**
  159. * @brief This function is used to setup the spi controller.
  160. *
  161. * @param[in] mode is CPOL and CPHA type defined in TLS_SPI_MODE_0 to TLS_SPI_MODE_3
  162. * @param[in] cs_active is cs mode, defined as TLS_SPI_CS_LOW or TLS_SPI_CS_HIGH
  163. * @param[in] fclk is spi clock,the unit is HZ.
  164. *
  165. * @retval TLS_SPI_STATUS_OK if setup success
  166. * @retval TLS_SPI_STATUS_EMODENOSUPPORT if mode is not support
  167. * @retval TLS_SPI_STATUS_EINVAL if cs_active is not support
  168. * @retval TLS_SPI_STATUS_ECLKNOSUPPORT if fclk is not support
  169. *
  170. * @note None
  171. */
  172. int tls_spi_setup(u8 mode, u8 cs_active, u32 fclk);
  173. /**
  174. * @brief This function is used to synchronous write data by SPI.
  175. *
  176. * @param[in] buf data to be sent.
  177. * @param[in] len data length.
  178. *
  179. * @retval TLS_SPI_STATUS_OK if write success.
  180. * @retval TLS_SPI_STATUS_EINVAL if argument is invalid.
  181. * @retval TLS_SPI_STATUS_ENOMEM if there is no enough memory.
  182. * @retval TLS_SPI_STATUS_ESHUTDOWN if SPI driver does not installed.
  183. *
  184. * @note None
  185. */
  186. int tls_spi_write(const u8 * buf, u32 len);
  187. /**
  188. * @brief This function is used to synchronously read data from SPI.
  189. *
  190. * @param[in] buf is the buffer for saving SPI data.
  191. * @param[in] len is the data length.
  192. *
  193. * @retval TLS_SPI_STATUS_OK if write success.
  194. * @retval TLS_SPI_STATUS_EINVAL if argument is invalid.
  195. * @retval TLS_SPI_STATUS_ENOMEM if there is no enough memory.
  196. * @retval TLS_SPI_STATUS_ESHUTDOWN if SPI driver does not installed.
  197. *
  198. * @note None
  199. */
  200. int tls_spi_read(u8 * buf, u32 len);
  201. /**
  202. * @brief This function is used to synchronously write command and then read data from SPI.
  203. *
  204. * @param[in] txbuf is the write data buffer.
  205. * @param[in] n_tx is the write data length.
  206. * @param[in] rxbuf is the read data buffer.
  207. * @param[in] n_rx is the read data length.
  208. *
  209. * @retval TLS_SPI_STATUS_OK if write success.
  210. * @retval TLS_SPI_STATUS_EINVAL if argument is invalid.
  211. * @retval TLS_SPI_STATUS_ENOMEM if there is no enough memory.
  212. * @retval TLS_SPI_STATUS_ESHUTDOWN if SPI driver has not been installed.
  213. *
  214. * @note None
  215. */
  216. int tls_spi_read_with_cmd(const u8 * txbuf, u32 n_tx, u8 * rxbuf, u32 n_rx);
  217. /**
  218. * @brief This function is used to synchronous write 32bit command then write data from SPI.
  219. *
  220. * @param[in] cmd is the command data.
  221. * @param[in] n_cmd is the command len,can not bigger than four
  222. * @param[in] txbuf is the write data buffer.
  223. * @param[in] n_tx is the write data length.
  224. *
  225. * @retval TLS_SPI_STATUS_OK if write success.
  226. * @retval TLS_SPI_STATUS_EINVAL if argument is invalid.
  227. * @retval TLS_SPI_STATUS_ENOMEM if there is no enough memory.
  228. * @retval TLS_SPI_STATUS_ESHUTDOWN if SPI driver does not installed.
  229. *
  230. * @note None
  231. */
  232. int tls_spi_write_with_cmd(const u8 * cmd, u32 n_cmd, const u8 * txbuf, u32 n_tx);
  233. /**
  234. * @brief This function is used to set SPI transfer mode.
  235. *
  236. * @param[in] type is the transfer type.
  237. * SPI_BYTE_TRANSFER ->byte transfer;
  238. * SPI_WORD_TRANSFER ->word transfer;
  239. * SPI_DMA_TRANSFER ->DMA transfer;
  240. *
  241. * @return None
  242. *
  243. * @note None
  244. */
  245. void tls_spi_trans_type(u8 type);
  246. int tls_spi_set_speed(u32 speed);
  247. /**
  248. * @}
  249. */
  250. /**
  251. * @}
  252. */
  253. #endif /* WM_HOST_SPI_H */