drv_spi.c 8.3 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-03-28 qiujingbao first version
  9. * 2024/06/08 flyingcys fix transmission failure
  10. */
  11. #include <rtthread.h>
  12. #include <rthw.h>
  13. #include <rtdevice.h>
  14. #include "board.h"
  15. #include "drv_spi.h"
  16. #include "drv_pinmux.h"
  17. #define DBG_LEVEL DBG_LOG
  18. #include <rtdbg.h>
  19. #define LOG_TAG "drv.spi"
  20. struct _device_spi
  21. {
  22. struct rt_spi_bus spi_bus;
  23. struct dw_spi dws;
  24. char *device_name;
  25. };
  26. static struct _device_spi _spi_obj[] =
  27. {
  28. #ifdef BSP_USING_SPI0
  29. {
  30. .dws.regs = (void *)DW_SPI0_BASE,
  31. .dws.irq = DW_SPI0_IRQn,
  32. .dws.index = 0,
  33. .device_name = "spi0",
  34. },
  35. #endif /* BSP_USING_SPI0 */
  36. #ifdef BSP_USING_SPI1
  37. {
  38. .dws.regs = (void *)DW_SPI1_BASE,
  39. .dws.irq = DW_SPI1_IRQn,
  40. .dws.index = 0,
  41. .device_name = "spi1",
  42. },
  43. #endif /* BSP_USING_SPI1 */
  44. #ifdef BSP_USING_SPI2
  45. {
  46. .dws.regs = (void *)DW_SPI2_BASE,
  47. .dws.irq = DW_SPI2_IRQn,
  48. .dws.index = 0,
  49. .device_name = "spi2",
  50. },
  51. #endif /* BSP_USING_SPI2 */
  52. #ifdef BSP_USING_SPI3
  53. {
  54. .dws.regs = (void *)DW_SPI3_BASE,
  55. .dws.irq = DW_SPI3_IRQn,
  56. .dws.index = 0,
  57. .device_name = "spi3",
  58. },
  59. #endif /* BSP_USING_SPI3 */
  60. };
  61. static rt_err_t spi_configure(struct rt_spi_device *device, struct rt_spi_configuration *cfg)
  62. {
  63. RT_ASSERT(device != RT_NULL);
  64. RT_ASSERT(device->bus != RT_NULL);
  65. RT_ASSERT(device->bus->parent.user_data != RT_NULL);
  66. RT_ASSERT(cfg != RT_NULL);
  67. rt_err_t ret = RT_EOK;
  68. struct _device_spi *spi = (struct _device_spi *)device->bus->parent.user_data;
  69. struct dw_spi *dws = &spi->dws;
  70. rt_uint8_t mode;
  71. LOG_D("spi_configure input");
  72. /* set cs low when spi idle */
  73. writel(0, (void *)0x030001d0);
  74. if (cfg->mode & RT_SPI_SLAVE)
  75. {
  76. LOG_E("invalid mode: %d", cfg->mode);
  77. return -RT_EINVAL;
  78. }
  79. spi_reset_chip(dws);
  80. spi_hw_init(dws);
  81. spi_enable_chip(dws, 0);
  82. LOG_D("cfg->max_hz: %d", cfg->max_hz);
  83. dw_spi_set_clock(dws, SPI_REF_CLK, cfg->max_hz);
  84. LOG_D("cfg->data_width: %d", cfg->data_width);
  85. if (dw_spi_set_data_frame_len(dws, (uint32_t)cfg->data_width) < 0)
  86. {
  87. LOG_E("dw_spi_set_data_frame_len failed...\n");
  88. return -RT_ERROR;
  89. }
  90. LOG_D("cfg->mode: %08x", cfg->mode);
  91. switch (cfg->mode & RT_SPI_MODE_3)
  92. {
  93. case RT_SPI_MODE_0:
  94. mode = SPI_FORMAT_CPOL0_CPHA0;
  95. break;
  96. case RT_SPI_MODE_1:
  97. mode = SPI_FORMAT_CPOL0_CPHA1;
  98. break;
  99. case RT_SPI_MODE_2:
  100. mode = SPI_FORMAT_CPOL1_CPHA0;
  101. break;
  102. case RT_SPI_MODE_3:
  103. mode = SPI_FORMAT_CPOL1_CPHA1;
  104. break;
  105. default:
  106. LOG_E("spi configure mode error %x\n", cfg->mode);
  107. break;
  108. }
  109. dw_spi_set_polarity_and_phase(dws, mode);
  110. dw_spi_set_cs(dws, 1, 0);
  111. spi_enable_chip(dws, 1);
  112. return RT_EOK;
  113. }
  114. static rt_err_t dw_spi_transfer_one(struct dw_spi *dws, const void *tx_buf, void *rx_buf, uint32_t len, enum transfer_type tran_type)
  115. {
  116. uint8_t imask = 0;
  117. uint16_t txlevel = 0;
  118. dws->tx = NULL;
  119. dws->tx_end = NULL;
  120. dws->rx = NULL;
  121. dws->rx_end = NULL;
  122. if (tx_buf != NULL) {
  123. dws->tx = tx_buf;
  124. dws->tx_end = dws->tx + len;
  125. }
  126. if (rx_buf != NULL) {
  127. dws->rx = rx_buf;
  128. dws->rx_end = dws->rx + len;
  129. }
  130. dws->rx_len = len / dws->n_bytes;
  131. dws->tx_len = len / dws->n_bytes;
  132. spi_enable_chip(dws, 0);
  133. /* For poll mode just disable all interrupts */
  134. spi_mask_intr(dws, 0xff);
  135. /* set tran mode */
  136. set_tran_mode(dws);
  137. /* cs0 */
  138. dw_spi_set_cs(dws, true, 0);
  139. /* enable spi */
  140. spi_enable_chip(dws, 1);
  141. rt_hw_us_delay(10);
  142. if (tran_type == POLL_TRAN)
  143. {
  144. if (poll_transfer(dws) < 0)
  145. return -RT_ERROR;
  146. }
  147. else
  148. {
  149. return -RT_ENOSYS;
  150. }
  151. return RT_EOK;
  152. }
  153. static rt_ssize_t spi_xfer(struct rt_spi_device *device, struct rt_spi_message *message)
  154. {
  155. RT_ASSERT(device != RT_NULL);
  156. RT_ASSERT(device->bus != RT_NULL);
  157. RT_ASSERT(device->bus->parent.user_data != RT_NULL);
  158. RT_ASSERT(message != RT_NULL);
  159. struct _device_spi *spi = (struct _device_spi *)device->bus->parent.user_data;
  160. struct dw_spi *dws = &spi->dws;
  161. int32_t ret = 0;
  162. if (message->send_buf && message->recv_buf)
  163. {
  164. ret = dw_spi_transfer_one(dws, message->send_buf, message->recv_buf, message->length, POLL_TRAN);
  165. }
  166. else if (message->send_buf)
  167. {
  168. ret = dw_spi_transfer_one(dws, message->send_buf, RT_NULL, message->length, POLL_TRAN);
  169. }
  170. else if (message->recv_buf)
  171. {
  172. ret = dw_spi_transfer_one(dws, RT_NULL, message->recv_buf, message->length, POLL_TRAN);
  173. }
  174. return message->length;
  175. }
  176. static const struct rt_spi_ops _spi_ops =
  177. {
  178. .configure = spi_configure,
  179. .xfer = spi_xfer,
  180. };
  181. #if defined(BOARD_TYPE_MILKV_DUO) || defined(BOARD_TYPE_MILKV_DUO_SPINOR) || defined(BOARD_TYPE_MILKV_DUO256M) || defined(BOARD_TYPE_MILKV_DUO256M_SPINOR)
  182. // For Duo / Duo 256m, only SPI2 are exported on board.
  183. #ifdef BSP_USING_SPI0
  184. static const char *pinname_whitelist_spi0_sck[] = {
  185. NULL,
  186. };
  187. static const char *pinname_whitelist_spi0_sdo[] = {
  188. NULL,
  189. };
  190. static const char *pinname_whitelist_spi0_sdi[] = {
  191. NULL,
  192. };
  193. static const char *pinname_whitelist_spi0_cs[] = {
  194. NULL,
  195. };
  196. #endif
  197. #ifdef BSP_USING_SPI1
  198. static const char *pinname_whitelist_spi1_sck[] = {
  199. NULL,
  200. };
  201. static const char *pinname_whitelist_spi1_sdo[] = {
  202. NULL,
  203. };
  204. static const char *pinname_whitelist_spi1_sdi[] = {
  205. NULL,
  206. };
  207. static const char *pinname_whitelist_spi1_cs[] = {
  208. NULL,
  209. };
  210. #endif
  211. #ifdef BSP_USING_SPI2
  212. static const char *pinname_whitelist_spi2_sck[] = {
  213. "SD1_CLK",
  214. NULL,
  215. };
  216. static const char *pinname_whitelist_spi2_sdo[] = {
  217. "SD1_CMD",
  218. NULL,
  219. };
  220. static const char *pinname_whitelist_spi2_sdi[] = {
  221. "SD1_D0",
  222. NULL,
  223. };
  224. static const char *pinname_whitelist_spi2_cs[] = {
  225. "SD1_D3",
  226. NULL,
  227. };
  228. #endif
  229. #ifdef BSP_USING_SPI3
  230. static const char *pinname_whitelist_spi3_sck[] = {
  231. NULL,
  232. };
  233. static const char *pinname_whitelist_spi3_sdo[] = {
  234. NULL,
  235. };
  236. static const char *pinname_whitelist_spi3_sdi[] = {
  237. NULL,
  238. };
  239. static const char *pinname_whitelist_spi3_cs[] = {
  240. NULL,
  241. };
  242. #endif
  243. #else
  244. #error "Unsupported board type!"
  245. #endif
  246. static void rt_hw_spi_pinmux_config()
  247. {
  248. #ifdef BSP_USING_SPI0
  249. pinmux_config(BSP_SPI0_SCK_PINNAME, SPI0_SCK, pinname_whitelist_spi0_sck);
  250. pinmux_config(BSP_SPI0_SDO_PINNAME, SPI0_SDO, pinname_whitelist_spi0_sdo);
  251. pinmux_config(BSP_SPI0_SDI_PINNAME, SPI0_SDI, pinname_whitelist_spi0_sdi);
  252. pinmux_config(BSP_SPI0_CS_PINNAME, SPI0_CS_X, pinname_whitelist_spi0_cs);
  253. #endif /* BSP_USING_SPI0 */
  254. #ifdef BSP_USING_SPI1
  255. pinmux_config(BSP_SPI1_SCK_PINNAME, SPI1_SCK, pinname_whitelist_spi1_sck);
  256. pinmux_config(BSP_SPI1_SDO_PINNAME, SPI1_SDO, pinname_whitelist_spi1_sdo);
  257. pinmux_config(BSP_SPI1_SDI_PINNAME, SPI1_SDI, pinname_whitelist_spi1_sdi);
  258. pinmux_config(BSP_SPI1_CS_PINNAME, SPI1_CS_X, pinname_whitelist_spi1_cs);
  259. #endif /* BSP_USING_SPI1 */
  260. #ifdef BSP_USING_SPI2
  261. pinmux_config(BSP_SPI2_SCK_PINNAME, SPI2_SCK, pinname_whitelist_spi2_sck);
  262. pinmux_config(BSP_SPI2_SDO_PINNAME, SPI2_SDO, pinname_whitelist_spi2_sdo);
  263. pinmux_config(BSP_SPI2_SDI_PINNAME, SPI2_SDI, pinname_whitelist_spi2_sdi);
  264. pinmux_config(BSP_SPI2_CS_PINNAME, SPI2_CS_X, pinname_whitelist_spi2_cs);
  265. #endif /* BSP_USING_SPI2 */
  266. #ifdef BSP_USING_SPI3
  267. pinmux_config(BSP_SPI3_SCK_PINNAME, SPI3_SCK, pinname_whitelist_spi3_sck);
  268. pinmux_config(BSP_SPI3_SDO_PINNAME, SPI3_SDO, pinname_whitelist_spi3_sdo);
  269. pinmux_config(BSP_SPI3_SDI_PINNAME, SPI3_SDI, pinname_whitelist_spi3_sdi);
  270. pinmux_config(BSP_SPI3_CS_PINNAME, SPI3_CS_X, pinname_whitelist_spi3_cs);
  271. #endif /* BSP_USING_SPI3 */
  272. }
  273. int rt_hw_spi_init(void)
  274. {
  275. rt_err_t ret = RT_EOK;
  276. struct dw_spi *dws;
  277. rt_hw_spi_pinmux_config();
  278. for (rt_size_t i = 0; i < sizeof(_spi_obj) / sizeof(struct _device_spi); i++)
  279. {
  280. _spi_obj[i].spi_bus.parent.user_data = (void *)&_spi_obj[i];
  281. ret = rt_spi_bus_register(&_spi_obj[i].spi_bus, _spi_obj[i].device_name, &_spi_ops);
  282. }
  283. RT_ASSERT(ret == RT_EOK);
  284. return ret;
  285. }
  286. INIT_DEVICE_EXPORT(rt_hw_spi_init);