blob: 4dacc0327d29a039737927b793efa8071dfcb2d3 [file] [log] [blame]
Kristofer Jonsson431d70c2021-06-18 17:00:32 +02001/*
2 * Copyright (c) 2021 Arm Limited. All rights reserved.
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 *
6 * Licensed under the Apache License, Version 2.0 (the License); you may
7 * not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
9 *
10 * www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an AS IS BASIS, WITHOUT
14 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 */
18#include "ethosu_driver.h"
19#include <device.h>
20#include <devicetree.h>
21#include <init.h>
22#include <kernel.h>
23#include <stdio.h>
24#include <stdlib.h>
25#include <sys/printk.h>
26#include <sys/util.h>
27
28#define DT_DRV_COMPAT arm_ethos_u
29
30/*******************************************************************************
31 * Re-implementation/Overrides __((weak)) symbol functions from ethosu_driver.c
32 * To handle mutex and semaphores
33 *******************************************************************************/
34void *ethosu_mutex_create(void)
35{
36 int status = 0;
37 struct k_mutex *mutex = k_malloc(sizeof(*mutex));
38 status = k_mutex_init(mutex);
39 if (status != 0)
40 {
41 printk("Failed to create mutex with error - %d\n", status);
42 }
43 return (void *)mutex;
44}
45
46void ethosu_mutex_lock(void *mutex)
47{
48 int status = 0;
49 status = k_mutex_lock((struct k_mutex *)mutex, K_FOREVER);
50 if (status != 0)
51 {
52 printk("Failed to lock mutex with error - %d\n", status);
53 }
54}
55
56void ethosu_mutex_unlock(void *mutex)
57{
58 k_mutex_unlock((struct k_mutex *)mutex);
59}
60
61void *ethosu_semaphore_create(void)
62{
63 int status = 0;
64 struct k_sem *sem = k_malloc(sizeof(*sem));
65 status = k_sem_init(sem, 1, 100);
66 if (status != 0)
67 {
68 printk("Failed to create semaphore with error - %d\n", status);
69 }
70 return (void *)sem;
71}
72
73void ethosu_semaphore_take(void *sem)
74{
75 int status = 0;
76 status = k_sem_take((struct k_sem *)sem, K_FOREVER);
77 if (status != 0)
78 {
79 printk("Failed to take semaphore with error - %d\n", status);
80 }
81}
82
83void ethosu_semaphore_give(void *sem)
84{
85 k_sem_give((struct k_sem *)sem);
86}
87
88struct ethosu_dts_info
89{
90 uint32_t base_addr;
91 uint32_t secure_enable;
92 uint32_t privilege_enable;
93 uint32_t irq;
94 uint32_t irq_priority;
95 uint32_t inst;
96};
97
98struct ethosu_data
99{
100 struct ethosu_driver drv;
101 void (*irq_config)(void);
102};
103
104static int ethosu_zephyr_init(const struct device *dev)
105{
106 const struct ethosu_dts_info *config = dev->config;
107 const struct ethosu_data *data = dev->data;
108 struct ethosu_driver *drv = (struct ethosu_driver *)&data->drv;
109 struct ethosu_driver_version version;
110 printk("Ethos-U DTS info. base_address=0x%x, inst=%u, secure_enable=%u, privilege_enable=%u, irq=%u, "
111 "irq_priority=%u\n",
112 config->base_addr,
113 config->inst,
114 config->secure_enable,
115 config->privilege_enable,
116 config->irq,
117 config->irq_priority);
118
119 ethosu_get_driver_version(&version);
120 printk("Version. major=%u, minor=%u, patch=%u\n", version.major, version.minor, version.patch);
121 if (ethosu_init(drv, (void *)(config->base_addr), NULL, 0, config->secure_enable, config->privilege_enable))
122 {
123 printk("Failed to initialize NPU with ethosu_init().\n");
124 return -EINVAL;
125 }
126
127 data->irq_config();
128
129 return 0;
130}
131
132#define ETHOSU_DEVICE_INIT(n) \
133 static void ethosu_zephyr_irq_config_##n(void); \
134 \
135 static struct ethosu_data ethosu_data_##n = {.irq_config = &ethosu_zephyr_irq_config_##n}; \
136 \
137 static void ethosu_zephyr_irq_handler_##n(void) \
138 { \
139 struct ethosu_driver *drv = &ethosu_data_##n.drv; \
140 ethosu_irq_handler(drv); \
141 } \
142 \
143 static void ethosu_zephyr_irq_config_##n(void) \
144 { \
145 IRQ_DIRECT_CONNECT(DT_INST_IRQN(n), DT_INST_IRQ(n, priority), ethosu_zephyr_irq_handler_##n, 0); \
146 irq_enable(DT_INST_IRQN(n)); \
147 } \
148 \
149 static const struct ethosu_dts_info ethosu_dts_info_##n = { \
150 .base_addr = DT_INST_REG_ADDR(n), \
151 .secure_enable = DT_INST_PROP(n, secure_enable), \
152 .privilege_enable = DT_INST_PROP(n, privilege_enable), \
153 .irq = DT_INST_IRQN(n), \
154 .irq_priority = DT_INST_IRQ(n, priority), \
155 .inst = n, \
156 }; \
157 \
158 DEVICE_DT_INST_DEFINE(n, \
159 ethosu_zephyr_init, \
160 NULL, \
161 &ethosu_data_##n, \
162 &ethosu_dts_info_##n, \
163 POST_KERNEL, \
164 CONFIG_KERNEL_INIT_PRIORITY_DEFAULT, \
165 NULL);
166
167DT_INST_FOREACH_STATUS_OKAY(ETHOSU_DEVICE_INIT);