drv_spi.c 8.4 KB

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