From edb1840f649016e4aa8a7896796816f380774ce1 Mon Sep 17 00:00:00 2001 From: Khoa Nguyen Date: Thu, 29 May 2025 14:57:13 +0700 Subject: [PATCH] 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 --- drivers/misc/CMakeLists.txt | 2 +- drivers/misc/Kconfig | 1 + drivers/misc/ethos_u/CMakeLists.txt | 3 +- drivers/misc/ethos_u/Kconfig | 14 +++ .../misc/ethos_u/{ethos_u.c => ethos_u_arm.c} | 110 +----------------- drivers/misc/ethos_u/ethos_u_common.c | 102 ++++++++++++++++ drivers/misc/ethos_u/ethos_u_common.h | 31 +++++ 7 files changed, 156 insertions(+), 107 deletions(-) create mode 100644 drivers/misc/ethos_u/Kconfig rename drivers/misc/ethos_u/{ethos_u.c => ethos_u_arm.c} (56%) create mode 100644 drivers/misc/ethos_u/ethos_u_common.c create mode 100644 drivers/misc/ethos_u/ethos_u_common.h diff --git a/drivers/misc/CMakeLists.txt b/drivers/misc/CMakeLists.txt index 5d72bdc116e..051a285785f 100644 --- a/drivers/misc/CMakeLists.txt +++ b/drivers/misc/CMakeLists.txt @@ -1,6 +1,6 @@ # SPDX-License-Identifier: Apache-2.0 -add_subdirectory_ifdef(CONFIG_ARM_ETHOS_U ethos_u) +add_subdirectory_ifdef(CONFIG_ETHOS_U ethos_u) add_subdirectory_ifdef(CONFIG_FT800 ft8xx) add_subdirectory_ifdef(CONFIG_GROVE_LCD_RGB grove_lcd_rgb) add_subdirectory_ifdef(CONFIG_PIO_RPI_PICO pio_rpi_pico) diff --git a/drivers/misc/Kconfig b/drivers/misc/Kconfig index 49c464f0fa1..715bcb68e61 100644 --- a/drivers/misc/Kconfig +++ b/drivers/misc/Kconfig @@ -5,6 +5,7 @@ menu "Miscellaneous Drivers" +source "drivers/misc/ethos_u/Kconfig" source "drivers/misc/ft8xx/Kconfig" source "drivers/misc/grove_lcd_rgb/Kconfig" source "drivers/misc/pio_rpi_pico/Kconfig" diff --git a/drivers/misc/ethos_u/CMakeLists.txt b/drivers/misc/ethos_u/CMakeLists.txt index c251488eb3c..024c09eb329 100644 --- a/drivers/misc/ethos_u/CMakeLists.txt +++ b/drivers/misc/ethos_u/CMakeLists.txt @@ -3,4 +3,5 @@ # SPDX-License-Identifier: Apache-2.0 zephyr_library() -zephyr_library_sources(ethos_u.c) +zephyr_library_sources(ethos_u_common.c) +zephyr_library_sources_ifdef(CONFIG_ETHOS_U_ARM ethos_u_arm.c) diff --git a/drivers/misc/ethos_u/Kconfig b/drivers/misc/ethos_u/Kconfig new file mode 100644 index 00000000000..66d38ae6f5e --- /dev/null +++ b/drivers/misc/ethos_u/Kconfig @@ -0,0 +1,14 @@ +# Copyright (c) 2025 Renesas Electronics Corporation +# SPDX-License-Identifier: Apache-2.0 + +choice + prompt "Select vendor Ethos-U NPU driver" + depends on ETHOS_U + default ETHOS_U_ARM if DT_HAS_ARM_ETHOS_U_ENABLED + +config ETHOS_U_ARM + bool "Arm Ethos-U NPU driver" + help + Enables Arm Ethos-U NPU driver. + +endchoice diff --git a/drivers/misc/ethos_u/ethos_u.c b/drivers/misc/ethos_u/ethos_u_arm.c similarity index 56% rename from drivers/misc/ethos_u/ethos_u.c rename to drivers/misc/ethos_u/ethos_u_arm.c index 308e689fe0f..21278a82a06 100644 --- a/drivers/misc/ethos_u/ethos_u.c +++ b/drivers/misc/ethos_u/ethos_u_arm.c @@ -4,117 +4,17 @@ * SPDX-License-Identifier: Apache-2.0 */ -#include "zephyr/sys_clock.h" +#include +#include #include #include -#include -#include -#include -#include - +#include #include -#include -LOG_MODULE_REGISTER(ethos_u, CONFIG_ARM_ETHOS_U_LOG_LEVEL); +#include "ethos_u_common.h" #define DT_DRV_COMPAT arm_ethos_u - -/******************************************************************************* - * Re-implementation/Overrides __((weak)) symbol functions from ethosu_driver.c - * To handle mutex and semaphores - *******************************************************************************/ - -void *ethosu_mutex_create(void) -{ - struct k_mutex *mutex; - - mutex = k_malloc(sizeof(*mutex)); - if (mutex == NULL) { - LOG_ERR("Failed allocate mutex"); - return NULL; - } - - k_mutex_init(mutex); - - return (void *)mutex; -} - -int ethosu_mutex_lock(void *mutex) -{ - int status; - - status = k_mutex_lock((struct k_mutex *)mutex, K_FOREVER); - if (status != 0) { - LOG_ERR("Failed to lock mutex with error - %d", status); - return -1; - } - - return 0; -} - -int ethosu_mutex_unlock(void *mutex) -{ - k_mutex_unlock((struct k_mutex *)mutex); - return 0; -} - -void *ethosu_semaphore_create(void) -{ - struct k_sem *sem; - - sem = k_malloc(sizeof(*sem)); - if (sem == NULL) { - LOG_ERR("Failed to allocate semaphore"); - return NULL; - } - - k_sem_init(sem, 0, 100); - - return (void *)sem; -} - -int ethosu_semaphore_take(void *sem, uint64_t timeout) -{ - int status; - - status = k_sem_take((struct k_sem *)sem, (timeout == ETHOSU_SEMAPHORE_WAIT_FOREVER) - ? K_FOREVER - : Z_TIMEOUT_TICKS(timeout)); - - if (status != 0) { - /* The Ethos-U driver expects the semaphore implementation to never fail except for - * when a timeout occurs, and the current ethosu_semaphore_take implementation makes - * no distinction, in terms of return codes, between a timeout and other semaphore - * take failures. Also, note that a timeout is virtually indistinguishable from - * other failures if the driver logging is disabled. Handling errors other than a - * timeout is therefore not covered here and is deferred to the application - * developer if necessary. - */ - if (status != -EAGAIN) { - LOG_ERR("Failed to take semaphore with error - %d", status); - } - return -1; - } - - return 0; -} - -int ethosu_semaphore_give(void *sem) -{ - k_sem_give((struct k_sem *)sem); - return 0; -} - -struct ethosu_dts_info { - void *base_addr; - bool secure_enable; - bool privilege_enable; - void (*irq_config)(void); -}; - -struct ethosu_data { - struct ethosu_driver drv; -}; +LOG_MODULE_REGISTER(arm_ethos_u, CONFIG_ETHOS_U_LOG_LEVEL); void ethosu_zephyr_irq_handler(const struct device *dev) { diff --git a/drivers/misc/ethos_u/ethos_u_common.c b/drivers/misc/ethos_u/ethos_u_common.c new file mode 100644 index 00000000000..e71f236b757 --- /dev/null +++ b/drivers/misc/ethos_u/ethos_u_common.c @@ -0,0 +1,102 @@ +/* + * SPDX-FileCopyrightText: Copyright 2021-2022, 2024 Arm Limited and/or its + * affiliates + * SPDX-License-Identifier: Apache-2.0 + */ + +#include "zephyr/sys_clock.h" +#include +#include +#include +#include + +#include + +#include +LOG_MODULE_REGISTER(ethos_u, CONFIG_ETHOS_U_LOG_LEVEL); + +/******************************************************************************* + * Re-implementation/Overrides __((weak)) symbol functions from ethosu_driver.c + * To handle mutex and semaphores + *******************************************************************************/ + +void *ethosu_mutex_create(void) +{ + struct k_mutex *mutex; + + mutex = k_malloc(sizeof(*mutex)); + if (mutex == NULL) { + LOG_ERR("Failed allocate mutex"); + return NULL; + } + + k_mutex_init(mutex); + + return (void *)mutex; +} + +int ethosu_mutex_lock(void *mutex) +{ + int status; + + status = k_mutex_lock((struct k_mutex *)mutex, K_FOREVER); + if (status != 0) { + LOG_ERR("Failed to lock mutex with error - %d", status); + return -1; + } + + return 0; +} + +int ethosu_mutex_unlock(void *mutex) +{ + k_mutex_unlock((struct k_mutex *)mutex); + return 0; +} + +void *ethosu_semaphore_create(void) +{ + struct k_sem *sem; + + sem = k_malloc(sizeof(*sem)); + if (sem == NULL) { + LOG_ERR("Failed to allocate semaphore"); + return NULL; + } + + k_sem_init(sem, 0, 100); + + return (void *)sem; +} + +int ethosu_semaphore_take(void *sem, uint64_t timeout) +{ + int status; + + status = k_sem_take((struct k_sem *)sem, (timeout == ETHOSU_SEMAPHORE_WAIT_FOREVER) + ? K_FOREVER + : Z_TIMEOUT_TICKS(timeout)); + + if (status != 0) { + /* The Ethos-U driver expects the semaphore implementation to never fail except for + * when a timeout occurs, and the current ethosu_semaphore_take implementation makes + * no distinction, in terms of return codes, between a timeout and other semaphore + * take failures. Also, note that a timeout is virtually indistinguishable from + * other failures if the driver logging is disabled. Handling errors other than a + * timeout is therefore not covered here and is deferred to the application + * developer if necessary. + */ + if (status != -EAGAIN) { + LOG_ERR("Failed to take semaphore with error - %d", status); + } + return -1; + } + + return 0; +} + +int ethosu_semaphore_give(void *sem) +{ + k_sem_give((struct k_sem *)sem); + return 0; +} diff --git a/drivers/misc/ethos_u/ethos_u_common.h b/drivers/misc/ethos_u/ethos_u_common.h new file mode 100644 index 00000000000..b27cb132aeb --- /dev/null +++ b/drivers/misc/ethos_u/ethos_u_common.h @@ -0,0 +1,31 @@ +/* + * SPDX-FileCopyrightText: Copyright 2021-2022, 2024 Arm Limited and/or its + * affiliates + * SPDX-License-Identifier: Apache-2.0 + */ + +#ifndef ZEPHYR_DRIVERS_MISC_ETHOS_U_ETHOS_U_COMMON_H_ +#define ZEPHYR_DRIVERS_MISC_ETHOS_U_ETHOS_U_COMMON_H_ + +#include + +#ifdef __cplusplus +extern "C" { +#endif + +struct ethosu_dts_info { + void *base_addr; + bool secure_enable; + bool privilege_enable; + void (*irq_config)(void); +}; + +struct ethosu_data { + struct ethosu_driver drv; +}; + +#ifdef __cplusplus +} +#endif + +#endif /* ZEPHYR_DRIVERS_MISC_ETHOS_U_ETHOS_U_COMMON_H_ */