gnne_dev.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. /* Copyright (c) 2023, Canaan Bright Sight Co., Ltd
  2. *
  3. * Redistribution and use in source and binary forms, with or without
  4. * modification, are permitted provided that the following conditions are met:
  5. * 1. Redistributions of source code must retain the above copyright
  6. * notice, this list of conditions and the following disclaimer.
  7. * 2. Redistributions in binary form must reproduce the above copyright
  8. * notice, this list of conditions and the following disclaimer in the
  9. * documentation and/or other materials provided with the distribution.
  10. *
  11. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
  12. * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
  13. * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  14. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  15. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
  16. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  17. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  18. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  19. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  20. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  21. * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  22. * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  23. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  24. */
  25. /*
  26. * Copyright (c) 2006-2025 RT-Thread Development Team
  27. *
  28. * SPDX-License-Identifier: Apache-2.0
  29. */
  30. #include <rtthread.h>
  31. #include <rthw.h>
  32. #include "io.h"
  33. #include <ioremap.h>
  34. #include "board.h"
  35. #include "drv_hardlock.h"
  36. #if defined(RT_USING_POSIX_DEVIO)
  37. #include <dfs_posix.h>
  38. #include <poll.h>
  39. #include <termios.h>
  40. #endif
  41. struct gnne_dev_handle
  42. {
  43. rt_wqueue_t *wait;
  44. rt_bool_t is_lock;
  45. };
  46. #define gnne_log(s...) rt_kprintf(s)
  47. #define gnne_err(s...) do { \
  48. gnne_log("<err>[%s:%d] ", __func__, __LINE__); \
  49. gnne_log(s); \
  50. gnne_log("\r\n"); \
  51. } while (0)
  52. #define GNNE_CMD_LOCK 0
  53. #define GNNE_CMD_TRYLOCK 1
  54. #define GNNE_CMD_UNLOCK 2
  55. static struct rt_device g_gnne_device = {0};
  56. static struct rt_event g_gnne_event = {0};
  57. void *gnne_base_addr = RT_NULL;
  58. static hardlock_type g_kpu_lock = HARDLOCK_MAX;
  59. static int gnne_device_open(struct dfs_file *file)
  60. {
  61. struct gnne_dev_handle *handle;
  62. rt_device_t device;
  63. handle = rt_malloc(sizeof(struct gnne_dev_handle));
  64. if (handle == RT_NULL)
  65. {
  66. gnne_err("malloc failed\n");
  67. return -1;
  68. }
  69. device = (rt_device_t)file->vnode->data;
  70. handle->wait = &device->wait_queue;
  71. handle->is_lock = RT_FALSE;
  72. file->data = (void *)handle;
  73. return RT_EOK;
  74. }
  75. static int gnne_device_close(struct dfs_file *file)
  76. {
  77. struct gnne_dev_handle *handle;
  78. handle = (struct gnne_dev_handle *)file->data;
  79. if (handle == RT_NULL)
  80. {
  81. gnne_err("try to close a invalid handle");
  82. return -RT_EINVAL;
  83. }
  84. if (handle->is_lock)
  85. {
  86. kd_hardlock_unlock(g_kpu_lock);
  87. }
  88. rt_free(handle);
  89. file->data = RT_NULL;
  90. return RT_EOK;
  91. }
  92. static int gnne_device_ioctl(struct dfs_file *file, int cmd, void *args)
  93. {
  94. struct gnne_dev_handle *handle;
  95. int ret = -1;
  96. handle = (struct gnne_dev_handle *)file->data;
  97. if ((g_kpu_lock == HARDLOCK_MAX))
  98. {
  99. return ret;
  100. }
  101. if (cmd == GNNE_CMD_LOCK)
  102. {
  103. if (handle->is_lock == RT_TRUE)
  104. {
  105. return 0;
  106. }
  107. while (kd_hardlock_lock(g_kpu_lock));
  108. handle->is_lock = RT_TRUE;
  109. ret = 0;
  110. }
  111. else if (cmd == GNNE_CMD_UNLOCK)
  112. {
  113. if (handle->is_lock == RT_FALSE)
  114. {
  115. return 0;
  116. }
  117. kd_hardlock_unlock(g_kpu_lock);
  118. handle->is_lock = RT_FALSE;
  119. ret = 0;
  120. }
  121. else if (cmd == GNNE_CMD_TRYLOCK)
  122. {
  123. if (handle->is_lock == RT_TRUE)
  124. {
  125. return 0;
  126. }
  127. if (!kd_hardlock_lock(g_kpu_lock))
  128. {
  129. handle->is_lock = RT_TRUE;
  130. ret = 0;
  131. }
  132. }
  133. return ret;
  134. }
  135. int gnne_device_poll(struct dfs_file *file, struct rt_pollreq *req)
  136. {
  137. struct gnne_dev_handle *handle;
  138. unsigned int flags;
  139. handle = (struct gnne_dev_handle *)file->data;
  140. if (!handle)
  141. {
  142. gnne_err("gnne_dev_handle NULL!");
  143. return -EINVAL;
  144. }
  145. rt_event_recv(&g_gnne_event, 0x01, RT_EVENT_FLAG_OR | RT_EVENT_FLAG_CLEAR, RT_WAITING_FOREVER, NULL);
  146. rt_poll_add(handle->wait, req);
  147. return POLLIN;
  148. }
  149. static const struct dfs_file_ops gnne_input_fops =
  150. {
  151. .open = gnne_device_open,
  152. .close = gnne_device_close,
  153. .ioctl = gnne_device_ioctl,
  154. .poll = gnne_device_poll,
  155. };
  156. static void irq_callback(int irq, void *data)
  157. {
  158. rt_wqueue_t *wait = (rt_wqueue_t *)data;
  159. volatile void *write_addr = (void *)((char *)gnne_base_addr + 0x128);
  160. if (gnne_base_addr == RT_NULL)
  161. {
  162. gnne_err("gnne interrupts while the hardware is not yet initialized\n");
  163. }
  164. /*clear kpu intr*/
  165. __iowmb();
  166. *(rt_uint64_t *)write_addr = 0x400000004;
  167. rt_wqueue_wakeup(wait, (void *)POLLIN);
  168. rt_event_send(&g_gnne_event, 0x1);
  169. }
  170. int gnne_device_init(void)
  171. {
  172. int ret = 0;
  173. rt_isr_handler_t old_handler;
  174. rt_device_t gnne_device = &g_gnne_device;
  175. ret = rt_event_init(&g_gnne_event, "gnne_event", RT_IPC_FLAG_PRIO);
  176. if (ret)
  177. {
  178. gnne_err("event init failed\n");
  179. return -ENOMEM;
  180. }
  181. ret = rt_device_register(gnne_device, "gnne_device", RT_DEVICE_FLAG_RDWR);
  182. if (ret)
  183. {
  184. gnne_err("gnne_device register fail\n");
  185. return ret;
  186. }
  187. /*rt_ioremap maps the size of at least one page*/
  188. gnne_base_addr = rt_ioremap((void *)KPU_BASE_ADDR, (KPU_IO_SIZE + FFT_IO_SIZE + AI2D_IO_SIZE));
  189. if (gnne_base_addr == RT_NULL)
  190. {
  191. gnne_err("gnne ioremap error\n");
  192. return -1;
  193. }
  194. rt_wqueue_init(&gnne_device->wait_queue);
  195. old_handler = rt_hw_interrupt_install(K230_IRQ_GNNE, irq_callback, &gnne_device->wait_queue, "gnne_irq");
  196. if (old_handler == RT_NULL)
  197. {
  198. gnne_err("gnne_device interrupt install fail\n");
  199. return -RT_ERROR;
  200. }
  201. rt_hw_interrupt_umask(K230_IRQ_GNNE);
  202. gnne_device->fops = &gnne_input_fops;
  203. if (kd_request_lock(HARDLOCK_KPU))
  204. {
  205. gnne_err("fail to request hardlock-%d\n", HARDLOCK_KPU);
  206. }
  207. else
  208. {
  209. g_kpu_lock = HARDLOCK_KPU;
  210. }
  211. return RT_EOK;
  212. }