drv_spi.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. /*
  2. * Copyright (c) 2006-2023, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Email: opensource_embedded@phytium.com.cn
  7. *
  8. * Change Logs:
  9. * Date Author Notes
  10. * 2022-11-10 liqiaozhong first commit
  11. * 2023-03-08 liqiaozhong support 4 spis and qspi working together
  12. *
  13. */
  14. #include"rtconfig.h"
  15. #include <rtthread.h>
  16. #include <rtdevice.h>
  17. #include "interrupt.h"
  18. #define LOG_TAG "spi_drv"
  19. #include "drv_log.h"
  20. #include <string.h>
  21. #include "fparameters.h"
  22. #include "fcpu_info.h"
  23. #include "fkernel.h"
  24. #include "ftypes.h"
  25. #ifdef RT_USING_SMART
  26. #include <ioremap.h>
  27. #endif
  28. #include <dfs_file.h>
  29. #include "fspim.h"
  30. #include "fspim_hw.h" /* include low-level header file for internal probe */
  31. #include "drv_spi.h"
  32. /************************** Constant Definitions *****************************/
  33. /**************************** Type Definitions *******************************/
  34. /************************** Variable Definitions *****************************/
  35. typedef struct
  36. {
  37. struct rt_spi_bus spi_bus;
  38. FSpim spim_instance;
  39. const char *name;
  40. } phytium_spi_bus;
  41. static struct rt_event rx_done_event;
  42. /***************** Macros (Inline Functions) Definitions *********************/
  43. #define EVENT_RX_DONE (1 << 1)
  44. /*******************************Api Functions*********************************/
  45. static rt_err_t spim_configure(struct rt_spi_device *device, struct rt_spi_configuration *configuration);
  46. static rt_uint32_t spim_xfer(struct rt_spi_device *device, struct rt_spi_message *message);
  47. static FError FSpimSetupInterrupt(FSpim *instance_p)
  48. {
  49. FASSERT(instance_p);
  50. FSpimConfig *config_p = &instance_p->config;
  51. uintptr base_addr = config_p->base_addr;
  52. rt_uint32_t cpu_id = rt_hw_cpu_id();
  53. LOG_D("cpu_id is %d, irq_num is %d\n", cpu_id, config_p->irq_num);
  54. config_p->irq_prority = 0xd0;
  55. rt_hw_interrupt_set_target_cpus(config_p->irq_num, cpu_id);
  56. rt_hw_interrupt_set_priority(config_p->irq_num, config_p->irq_prority);
  57. /* register intr callback */
  58. rt_hw_interrupt_install(config_p->irq_num,
  59. FSpimInterruptHandler,
  60. instance_p,
  61. NULL);
  62. /* enable tx fifo overflow / rx overflow / rx full */
  63. FSpimMaskIrq(base_addr, FSPIM_IMR_ALL_BITS);
  64. /* enable irq */
  65. rt_hw_interrupt_umask(config_p->irq_num);
  66. return FSPIM_SUCCESS;
  67. }
  68. static void rt_ft_send_event_done(void *instance_p, void *param)
  69. {
  70. FASSERT(instance_p);
  71. rt_event_send(&rx_done_event, EVENT_RX_DONE);
  72. return;
  73. }
  74. static const struct rt_spi_ops spim_ops =
  75. {
  76. .configure = spim_configure,
  77. .xfer = spim_xfer
  78. };
  79. static rt_err_t spim_configure(struct rt_spi_device *device,
  80. struct rt_spi_configuration *configuration)
  81. {
  82. FError ret = FSPIM_SUCCESS;
  83. RT_ASSERT(device != RT_NULL);
  84. RT_ASSERT(configuration != RT_NULL);
  85. phytium_spi_bus *user_data_cfg = device->parent.user_data;
  86. FSpimConfig input_cfg = *FSpimLookupConfig(user_data_cfg->spim_instance.config.instance_id);
  87. #ifdef RT_USING_SMART
  88. input_cfg.base_addr = (uintptr)rt_ioremap((void *)input_cfg.base_addr, 0x1000);
  89. #endif
  90. FSpimConfig *set_input_cfg = &input_cfg;
  91. /* set fspim device according to configuration */
  92. if (configuration->mode & RT_SPI_CPOL)
  93. {
  94. set_input_cfg->cpol = FSPIM_CPOL_HIGH;
  95. }
  96. else
  97. {
  98. set_input_cfg->cpol = FSPIM_CPOL_LOW;
  99. }
  100. if (configuration->mode & RT_SPI_CPHA)
  101. {
  102. set_input_cfg->cpha = FSPIM_CPHA_2_EDGE;
  103. }
  104. else
  105. {
  106. set_input_cfg->cpha = FSPIM_CPHA_1_EDGE;
  107. }
  108. if (configuration->data_width == 8)
  109. {
  110. set_input_cfg->n_bytes = FSPIM_1_BYTE;
  111. }
  112. else if (configuration->data_width == 16)
  113. {
  114. set_input_cfg->n_bytes = FSPIM_2_BYTE;
  115. }
  116. /* send spi_cfg to RT-Thread sys */
  117. ret = FSpimCfgInitialize(&user_data_cfg->spim_instance, &input_cfg);
  118. if (FSPIM_SUCCESS != ret)
  119. {
  120. return -RT_ERROR;
  121. }
  122. /* irq setting */
  123. ret = FSpimSetupInterrupt(&user_data_cfg->spim_instance);
  124. if (FSPIM_SUCCESS != ret)
  125. {
  126. return -RT_ERROR;
  127. }
  128. FSpimRegisterIntrruptHandler(&user_data_cfg->spim_instance, FSPIM_INTR_EVT_RX_DONE, rt_ft_send_event_done, NULL);
  129. return ret;
  130. }
  131. static rt_uint32_t spim_xfer(struct rt_spi_device *device, struct rt_spi_message *message)
  132. {
  133. RT_ASSERT(device != RT_NULL);
  134. RT_ASSERT(device->parent.user_data != RT_NULL);
  135. RT_ASSERT(message != RT_NULL);
  136. rt_size_t message_length;
  137. rt_uint8_t *recv_buf;
  138. const rt_uint8_t *send_buf;
  139. /* recv spi_cfg from RT-Thread sys */
  140. phytium_spi_bus *user_data_xfer = device->parent.user_data;
  141. FSpim *xfer_spim_instance = &user_data_xfer->spim_instance;
  142. FError tx_rx_result = FSPIM_SUCCESS;
  143. message_length = message->length;
  144. recv_buf = message->recv_buf;
  145. send_buf = message->send_buf;
  146. if (message->cs_take)
  147. {
  148. FSpimSetChipSelection(xfer_spim_instance, TRUE);
  149. }
  150. if (message_length > 0)
  151. {
  152. if (send_buf == RT_NULL && recv_buf != RT_NULL)
  153. {
  154. /* receive message */
  155. tx_rx_result = FSpimTransferByInterrupt(xfer_spim_instance, RT_NULL, recv_buf, message_length);
  156. }
  157. else if (send_buf != RT_NULL && recv_buf == RT_NULL)
  158. {
  159. /* send message */
  160. tx_rx_result = FSpimTransferByInterrupt(xfer_spim_instance, send_buf, RT_NULL, message_length);
  161. }
  162. else if (send_buf != RT_NULL && recv_buf != RT_NULL)
  163. {
  164. /* not supported yet */
  165. rt_kprintf("Do not support the situation that send_buf and recv_buf both not equal to 0.");
  166. }
  167. }
  168. if (FSPIM_SUCCESS != tx_rx_result)
  169. {
  170. rt_kprintf("FSpimTransferByInterrupt() fail!!!");
  171. message_length = 0;
  172. }
  173. if (rt_event_recv(&rx_done_event, (EVENT_RX_DONE),
  174. (RT_EVENT_FLAG_OR | RT_EVENT_FLAG_CLEAR),
  175. RT_WAITING_FOREVER, RT_NULL) != RT_EOK)
  176. {
  177. rt_kprintf("Wait rx timeout!!!\n");
  178. message_length = 0;
  179. }
  180. if (message->cs_release)
  181. {
  182. FSpimSetChipSelection(xfer_spim_instance, FALSE);
  183. }
  184. return message_length;
  185. }
  186. static int spi_init(phytium_spi_bus *phytium_spi)
  187. {
  188. rt_spi_bus_register(&phytium_spi->spi_bus, phytium_spi->name, &spim_ops);
  189. RT_ASSERT((struct rt_spi_device *)rt_device_find(phytium_spi->name));
  190. return 0;
  191. }
  192. #ifdef RT_USING_SPIM0
  193. static phytium_spi_bus spi0_bus;
  194. #endif
  195. #ifdef RT_USING_SPIM1
  196. static phytium_spi_bus spi1_bus;
  197. #endif
  198. #ifdef RT_USING_SPIM2
  199. static phytium_spi_bus spi2_bus;
  200. #endif
  201. #ifdef RT_USING_SPIM3
  202. static phytium_spi_bus spi3_bus;
  203. #endif
  204. int rt_hw_spi_init(void)
  205. {
  206. /* event creat */
  207. if (RT_EOK != rt_event_init(&rx_done_event, "rx_done_event", RT_IPC_FLAG_FIFO))
  208. {
  209. rt_kprintf("Create event failed.\n");
  210. return -RT_ERROR;
  211. }
  212. #ifdef RT_USING_SPIM0
  213. spi0_bus.name = "SPI0";
  214. spi0_bus.spim_instance.config.instance_id = FSPI0_ID;
  215. spi_init(&spi0_bus);
  216. #endif
  217. #ifdef RT_USING_SPIM1
  218. spi1_bus.name = "SPI1";
  219. spi1_bus.spim_instance.config.instance_id = FSPI1_ID;
  220. spi_init(&spi1_bus);
  221. #endif
  222. #ifdef RT_USING_SPIM2
  223. spi2_bus.name = "SPI2";
  224. spi2_bus.spim_instance.config.instance_id = FSPI2_ID;
  225. spi_init(&spi2_bus);
  226. #endif
  227. #ifdef RT_USING_SPIM3
  228. spi3_bus.name = "SPI3";
  229. spi3_bus.spim_instance.config.instance_id = FSPI3_ID;
  230. spi_init(&spi3_bus);
  231. #endif
  232. return 0;
  233. }
  234. INIT_DEVICE_EXPORT(rt_hw_spi_init);