ai2d_dev.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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 "drv_hardlock.h"
  33. #include "board.h"
  34. #if defined(RT_USING_POSIX_DEVIO)
  35. #include <dfs_posix.h>
  36. #include <poll.h>
  37. #include <termios.h>
  38. #endif
  39. #define AI2D_CMD_LOCK 0
  40. #define AI2D_CMD_TRYLOCK 1
  41. #define AI2D_CMD_UNLOCK 2
  42. static hardlock_type g_ai2d_lock = HARDLOCK_MAX;
  43. struct ai_2d_dev_handle
  44. {
  45. rt_wqueue_t *wait;
  46. rt_bool_t is_lock;
  47. };
  48. #define ai_2d_log(s...) rt_kprintf(s)
  49. #define ai_2d_err(s...) do { \
  50. ai_2d_log("<err>[%s:%d] ", __func__, __LINE__); \
  51. ai_2d_log(s); \
  52. ai_2d_log("\r\n"); \
  53. } while (0)
  54. static struct rt_device g_ai_2d_device = {0};
  55. static struct rt_event g_ai_2d_event = {0};
  56. extern void *gnne_base_addr;
  57. static int ai_2d_device_open(struct dfs_file *file)
  58. {
  59. struct ai_2d_dev_handle *handle;
  60. rt_device_t device;
  61. handle = rt_malloc(sizeof(struct ai_2d_dev_handle));
  62. if (handle == RT_NULL)
  63. {
  64. ai_2d_err("malloc failed\n");
  65. return -1;
  66. }
  67. device = (rt_device_t)file->vnode->data;
  68. handle->wait = &device->wait_queue;
  69. handle->is_lock = RT_FALSE;
  70. file->data = (void *)handle;
  71. return RT_EOK;
  72. }
  73. static int ai_2d_device_close(struct dfs_file *file)
  74. {
  75. struct ai_2d_dev_handle *handle;
  76. handle = (struct ai_2d_dev_handle *)file->data;
  77. if (handle == RT_NULL)
  78. {
  79. ai_2d_err("try to close a invalid handle");
  80. return -RT_EINVAL;
  81. }
  82. if (handle->is_lock)
  83. {
  84. kd_hardlock_unlock(g_ai2d_lock);
  85. }
  86. rt_free(handle);
  87. file->data = RT_NULL;
  88. return RT_EOK;
  89. }
  90. static int ai_2d_device_ioctl(struct dfs_file *file, int cmd, void *args)
  91. {
  92. struct ai_2d_dev_handle *handle;
  93. int ret = -1;
  94. handle = (struct ai_2d_dev_handle *)file->data;
  95. if (g_ai2d_lock == HARDLOCK_MAX)
  96. return ret;
  97. if (cmd == AI2D_CMD_LOCK)
  98. {
  99. if (handle->is_lock == RT_TRUE)
  100. {
  101. return 0;
  102. }
  103. while (kd_hardlock_lock(g_ai2d_lock));
  104. handle->is_lock = RT_TRUE;
  105. ret = 0;
  106. }
  107. else if (cmd == AI2D_CMD_UNLOCK)
  108. {
  109. if (handle->is_lock == RT_FALSE)
  110. {
  111. return 0;
  112. }
  113. kd_hardlock_unlock(g_ai2d_lock);
  114. handle->is_lock = RT_FALSE;
  115. ret = 0;
  116. }
  117. else if (cmd == AI2D_CMD_TRYLOCK)
  118. {
  119. if (handle->is_lock == RT_TRUE)
  120. {
  121. return 0;
  122. }
  123. if (!kd_hardlock_lock(g_ai2d_lock))
  124. {
  125. handle->is_lock = RT_TRUE;
  126. ret = 0;
  127. }
  128. }
  129. return ret;
  130. }
  131. int ai_2d_device_poll(struct dfs_file *file, struct rt_pollreq *req)
  132. {
  133. struct ai_2d_dev_handle *handle;
  134. unsigned int flags;
  135. handle = (struct ai_2d_dev_handle *)file->data;
  136. if (!handle)
  137. {
  138. ai_2d_err("ai_2d_dev_handle NULL!");
  139. return -EINVAL;
  140. }
  141. rt_event_recv(&g_ai_2d_event, 0x01, RT_EVENT_FLAG_OR | RT_EVENT_FLAG_CLEAR, RT_WAITING_FOREVER, NULL);
  142. rt_poll_add(handle->wait, req);
  143. return POLLIN;
  144. }
  145. static const struct dfs_file_ops ai_2d_input_fops =
  146. {
  147. .open = ai_2d_device_open,
  148. .close = ai_2d_device_close,
  149. .ioctl = ai_2d_device_ioctl,
  150. .poll = ai_2d_device_poll,
  151. };
  152. static void irq_callback(int irq, void *data)
  153. {
  154. rt_wqueue_t *wait = (rt_wqueue_t *)data;
  155. volatile rt_uint32_t *write_addr = (rt_uint32_t *)((char *)gnne_base_addr + 0xca0);
  156. if (gnne_base_addr == RT_NULL)
  157. {
  158. ai_2d_err("ai2d interrupts while the hardware is not yet initialized\n");
  159. }
  160. write_addr[0] = 1;
  161. write_addr[1] = 0;
  162. write_addr[2] = 0;
  163. write_addr[3] = 0;
  164. rt_wqueue_wakeup(wait, (void *)POLLIN);
  165. rt_event_send(&g_ai_2d_event, 0x1);
  166. }
  167. int ai_2d_device_init(void)
  168. {
  169. int ret = 0;
  170. rt_isr_handler_t old_handler;
  171. rt_device_t ai_2d_device = &g_ai_2d_device;
  172. ret = rt_event_init(&g_ai_2d_event, "ai_2d_event", RT_IPC_FLAG_PRIO);
  173. if (ret)
  174. {
  175. ai_2d_err("event init failed\n");
  176. return -ENOMEM;
  177. }
  178. ret = rt_device_register(ai_2d_device, "ai_2d_device", RT_DEVICE_FLAG_RDWR);
  179. if (ret)
  180. {
  181. ai_2d_err("ai_2d_device register fail\n");
  182. return ret;
  183. }
  184. rt_wqueue_init(&ai_2d_device->wait_queue);
  185. old_handler = rt_hw_interrupt_install(K230_IRQ_AI_2D, irq_callback, &ai_2d_device->wait_queue, "ai_2d_irq");
  186. if (old_handler == RT_NULL)
  187. {
  188. ai_2d_err("ai_2d_device interrupt install fail\n");
  189. return -RT_ERROR;
  190. }
  191. rt_hw_interrupt_umask(K230_IRQ_AI_2D);
  192. ai_2d_device->fops = &ai_2d_input_fops;
  193. if (kd_request_lock(HARDLOCK_AI2D))
  194. {
  195. ai_2d_err("fail to request hardlock-%d\n", HARDLOCK_AI2D);
  196. }
  197. else
  198. {
  199. g_ai2d_lock = HARDLOCK_AI2D;
  200. }
  201. return RT_EOK;
  202. }