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