Skip to content

Commit edb1840

Browse files
khoa-nguyen-18kartben
authored andcommitted
drivers: misc: ethos_u: Create the ethos_u_common for every vendor
Seperate the ``ethos_u_common`` for every vendor and ``ethos_u_arm`` for sepcific Arm's boards. Enable vendors to self-configure the init flow and IRQ handler. Signed-off-by: Khoa Nguyen <[email protected]>
1 parent c99f456 commit edb1840

File tree

7 files changed

+156
-107
lines changed

7 files changed

+156
-107
lines changed

drivers/misc/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# SPDX-License-Identifier: Apache-2.0
22

3-
add_subdirectory_ifdef(CONFIG_ARM_ETHOS_U ethos_u)
3+
add_subdirectory_ifdef(CONFIG_ETHOS_U ethos_u)
44
add_subdirectory_ifdef(CONFIG_FT800 ft8xx)
55
add_subdirectory_ifdef(CONFIG_GROVE_LCD_RGB grove_lcd_rgb)
66
add_subdirectory_ifdef(CONFIG_PIO_RPI_PICO pio_rpi_pico)

drivers/misc/Kconfig

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
menu "Miscellaneous Drivers"
77

8+
source "drivers/misc/ethos_u/Kconfig"
89
source "drivers/misc/ft8xx/Kconfig"
910
source "drivers/misc/grove_lcd_rgb/Kconfig"
1011
source "drivers/misc/pio_rpi_pico/Kconfig"

drivers/misc/ethos_u/CMakeLists.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,5 @@
33
# SPDX-License-Identifier: Apache-2.0
44

55
zephyr_library()
6-
zephyr_library_sources(ethos_u.c)
6+
zephyr_library_sources(ethos_u_common.c)
7+
zephyr_library_sources_ifdef(CONFIG_ETHOS_U_ARM ethos_u_arm.c)

drivers/misc/ethos_u/Kconfig

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# Copyright (c) 2025 Renesas Electronics Corporation
2+
# SPDX-License-Identifier: Apache-2.0
3+
4+
choice
5+
prompt "Select vendor Ethos-U NPU driver"
6+
depends on ETHOS_U
7+
default ETHOS_U_ARM if DT_HAS_ARM_ETHOS_U_ENABLED
8+
9+
config ETHOS_U_ARM
10+
bool "Arm Ethos-U NPU driver"
11+
help
12+
Enables Arm Ethos-U NPU driver.
13+
14+
endchoice

drivers/misc/ethos_u/ethos_u.c renamed to drivers/misc/ethos_u/ethos_u_arm.c

Lines changed: 5 additions & 105 deletions
Original file line numberDiff line numberDiff line change
@@ -4,117 +4,17 @@
44
* SPDX-License-Identifier: Apache-2.0
55
*/
66

7-
#include "zephyr/sys_clock.h"
7+
#include <zephyr/irq.h>
8+
#include <zephyr/init.h>
89
#include <zephyr/device.h>
910
#include <zephyr/devicetree.h>
10-
#include <zephyr/init.h>
11-
#include <zephyr/kernel.h>
12-
#include <zephyr/irq.h>
13-
#include <zephyr/sys/util.h>
14-
11+
#include <zephyr/logging/log.h>
1512
#include <ethosu_driver.h>
1613

17-
#include <zephyr/logging/log.h>
18-
LOG_MODULE_REGISTER(ethos_u, CONFIG_ARM_ETHOS_U_LOG_LEVEL);
14+
#include "ethos_u_common.h"
1915

2016
#define DT_DRV_COMPAT arm_ethos_u
21-
22-
/*******************************************************************************
23-
* Re-implementation/Overrides __((weak)) symbol functions from ethosu_driver.c
24-
* To handle mutex and semaphores
25-
*******************************************************************************/
26-
27-
void *ethosu_mutex_create(void)
28-
{
29-
struct k_mutex *mutex;
30-
31-
mutex = k_malloc(sizeof(*mutex));
32-
if (mutex == NULL) {
33-
LOG_ERR("Failed allocate mutex");
34-
return NULL;
35-
}
36-
37-
k_mutex_init(mutex);
38-
39-
return (void *)mutex;
40-
}
41-
42-
int ethosu_mutex_lock(void *mutex)
43-
{
44-
int status;
45-
46-
status = k_mutex_lock((struct k_mutex *)mutex, K_FOREVER);
47-
if (status != 0) {
48-
LOG_ERR("Failed to lock mutex with error - %d", status);
49-
return -1;
50-
}
51-
52-
return 0;
53-
}
54-
55-
int ethosu_mutex_unlock(void *mutex)
56-
{
57-
k_mutex_unlock((struct k_mutex *)mutex);
58-
return 0;
59-
}
60-
61-
void *ethosu_semaphore_create(void)
62-
{
63-
struct k_sem *sem;
64-
65-
sem = k_malloc(sizeof(*sem));
66-
if (sem == NULL) {
67-
LOG_ERR("Failed to allocate semaphore");
68-
return NULL;
69-
}
70-
71-
k_sem_init(sem, 0, 100);
72-
73-
return (void *)sem;
74-
}
75-
76-
int ethosu_semaphore_take(void *sem, uint64_t timeout)
77-
{
78-
int status;
79-
80-
status = k_sem_take((struct k_sem *)sem, (timeout == ETHOSU_SEMAPHORE_WAIT_FOREVER)
81-
? K_FOREVER
82-
: Z_TIMEOUT_TICKS(timeout));
83-
84-
if (status != 0) {
85-
/* The Ethos-U driver expects the semaphore implementation to never fail except for
86-
* when a timeout occurs, and the current ethosu_semaphore_take implementation makes
87-
* no distinction, in terms of return codes, between a timeout and other semaphore
88-
* take failures. Also, note that a timeout is virtually indistinguishable from
89-
* other failures if the driver logging is disabled. Handling errors other than a
90-
* timeout is therefore not covered here and is deferred to the application
91-
* developer if necessary.
92-
*/
93-
if (status != -EAGAIN) {
94-
LOG_ERR("Failed to take semaphore with error - %d", status);
95-
}
96-
return -1;
97-
}
98-
99-
return 0;
100-
}
101-
102-
int ethosu_semaphore_give(void *sem)
103-
{
104-
k_sem_give((struct k_sem *)sem);
105-
return 0;
106-
}
107-
108-
struct ethosu_dts_info {
109-
void *base_addr;
110-
bool secure_enable;
111-
bool privilege_enable;
112-
void (*irq_config)(void);
113-
};
114-
115-
struct ethosu_data {
116-
struct ethosu_driver drv;
117-
};
17+
LOG_MODULE_REGISTER(arm_ethos_u, CONFIG_ETHOS_U_LOG_LEVEL);
11818

11919
void ethosu_zephyr_irq_handler(const struct device *dev)
12020
{

drivers/misc/ethos_u/ethos_u_common.c

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
/*
2+
* SPDX-FileCopyrightText: <text>Copyright 2021-2022, 2024 Arm Limited and/or its
3+
* affiliates <[email protected]></text>
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
#include "zephyr/sys_clock.h"
8+
#include <zephyr/device.h>
9+
#include <zephyr/devicetree.h>
10+
#include <zephyr/kernel.h>
11+
#include <zephyr/sys/util.h>
12+
13+
#include <ethosu_driver.h>
14+
15+
#include <zephyr/logging/log.h>
16+
LOG_MODULE_REGISTER(ethos_u, CONFIG_ETHOS_U_LOG_LEVEL);
17+
18+
/*******************************************************************************
19+
* Re-implementation/Overrides __((weak)) symbol functions from ethosu_driver.c
20+
* To handle mutex and semaphores
21+
*******************************************************************************/
22+
23+
void *ethosu_mutex_create(void)
24+
{
25+
struct k_mutex *mutex;
26+
27+
mutex = k_malloc(sizeof(*mutex));
28+
if (mutex == NULL) {
29+
LOG_ERR("Failed allocate mutex");
30+
return NULL;
31+
}
32+
33+
k_mutex_init(mutex);
34+
35+
return (void *)mutex;
36+
}
37+
38+
int ethosu_mutex_lock(void *mutex)
39+
{
40+
int status;
41+
42+
status = k_mutex_lock((struct k_mutex *)mutex, K_FOREVER);
43+
if (status != 0) {
44+
LOG_ERR("Failed to lock mutex with error - %d", status);
45+
return -1;
46+
}
47+
48+
return 0;
49+
}
50+
51+
int ethosu_mutex_unlock(void *mutex)
52+
{
53+
k_mutex_unlock((struct k_mutex *)mutex);
54+
return 0;
55+
}
56+
57+
void *ethosu_semaphore_create(void)
58+
{
59+
struct k_sem *sem;
60+
61+
sem = k_malloc(sizeof(*sem));
62+
if (sem == NULL) {
63+
LOG_ERR("Failed to allocate semaphore");
64+
return NULL;
65+
}
66+
67+
k_sem_init(sem, 0, 100);
68+
69+
return (void *)sem;
70+
}
71+
72+
int ethosu_semaphore_take(void *sem, uint64_t timeout)
73+
{
74+
int status;
75+
76+
status = k_sem_take((struct k_sem *)sem, (timeout == ETHOSU_SEMAPHORE_WAIT_FOREVER)
77+
? K_FOREVER
78+
: Z_TIMEOUT_TICKS(timeout));
79+
80+
if (status != 0) {
81+
/* The Ethos-U driver expects the semaphore implementation to never fail except for
82+
* when a timeout occurs, and the current ethosu_semaphore_take implementation makes
83+
* no distinction, in terms of return codes, between a timeout and other semaphore
84+
* take failures. Also, note that a timeout is virtually indistinguishable from
85+
* other failures if the driver logging is disabled. Handling errors other than a
86+
* timeout is therefore not covered here and is deferred to the application
87+
* developer if necessary.
88+
*/
89+
if (status != -EAGAIN) {
90+
LOG_ERR("Failed to take semaphore with error - %d", status);
91+
}
92+
return -1;
93+
}
94+
95+
return 0;
96+
}
97+
98+
int ethosu_semaphore_give(void *sem)
99+
{
100+
k_sem_give((struct k_sem *)sem);
101+
return 0;
102+
}

drivers/misc/ethos_u/ethos_u_common.h

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/*
2+
* SPDX-FileCopyrightText: <text>Copyright 2021-2022, 2024 Arm Limited and/or its
3+
* affiliates <[email protected]></text>
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
#ifndef ZEPHYR_DRIVERS_MISC_ETHOS_U_ETHOS_U_COMMON_H_
8+
#define ZEPHYR_DRIVERS_MISC_ETHOS_U_ETHOS_U_COMMON_H_
9+
10+
#include <ethosu_driver.h>
11+
12+
#ifdef __cplusplus
13+
extern "C" {
14+
#endif
15+
16+
struct ethosu_dts_info {
17+
void *base_addr;
18+
bool secure_enable;
19+
bool privilege_enable;
20+
void (*irq_config)(void);
21+
};
22+
23+
struct ethosu_data {
24+
struct ethosu_driver drv;
25+
};
26+
27+
#ifdef __cplusplus
28+
}
29+
#endif
30+
31+
#endif /* ZEPHYR_DRIVERS_MISC_ETHOS_U_ETHOS_U_COMMON_H_ */

0 commit comments

Comments
 (0)