From 10c35b8e708eba7a5472fda33a462e9d77c53f1f Mon Sep 17 00:00:00 2001 From: Vebjorn Myklebust Date: Wed, 24 Jul 2024 14:47:47 +0200 Subject: [PATCH] drivers: timer: Add support for cc23x0 systim Add support for systim to cc23x0 SoC. Signed-off-by: Lars Thalian Morstad Signed-off-by: Vebjorn Myklebust Signed-off-by: Stoyan Bogdanov Signed-off-by: Julien Panis --- drivers/timer/CMakeLists.txt | 1 + drivers/timer/Kconfig | 1 + drivers/timer/Kconfig.cc23x0_systim | 13 ++ drivers/timer/cc23x0_systim_timer.c | 154 ++++++++++++++++++++++++ dts/bindings/timer/ti,cc23x0-timer.yaml | 15 +++ 5 files changed, 184 insertions(+) create mode 100644 drivers/timer/Kconfig.cc23x0_systim create mode 100644 drivers/timer/cc23x0_systim_timer.c create mode 100644 dts/bindings/timer/ti,cc23x0-timer.yaml diff --git a/drivers/timer/CMakeLists.txt b/drivers/timer/CMakeLists.txt index 51b0e552f4a..9a6b2697e98 100644 --- a/drivers/timer/CMakeLists.txt +++ b/drivers/timer/CMakeLists.txt @@ -12,6 +12,7 @@ zephyr_library_sources_ifdef(CONFIG_ARCV2_TIMER arcv2_timer0.c) zephyr_library_sources_ifdef(CONFIG_ARM_ARCH_TIMER arm_arch_timer.c) zephyr_library_sources_ifdef(CONFIG_INTEL_ADSP_TIMER intel_adsp_timer.c) zephyr_library_sources_ifdef(CONFIG_CC13XX_CC26XX_RTC_TIMER cc13xx_cc26xx_rtc_timer.c) +zephyr_library_sources_ifdef(CONFIG_CC23X0_SYSTIM_TIMER cc23x0_systim_timer.c) zephyr_library_sources_ifdef(CONFIG_CH32V00X_SYSTICK wch_systick_ch32v00x.c) zephyr_library_sources_ifdef(CONFIG_CORTEX_M_SYSTICK cortex_m_systick.c) zephyr_library_sources_ifdef(CONFIG_ESP32_SYS_TIMER esp32_sys_timer.c) diff --git a/drivers/timer/Kconfig b/drivers/timer/Kconfig index f017589a60e..839dec71fa2 100644 --- a/drivers/timer/Kconfig +++ b/drivers/timer/Kconfig @@ -70,6 +70,7 @@ source "drivers/timer/Kconfig.arcv2" source "drivers/timer/Kconfig.arm_arch" source "drivers/timer/Kconfig.cavs" source "drivers/timer/Kconfig.cc13xx_cc26xx_rtc" +source "drivers/timer/Kconfig.cc23x0_systim" source "drivers/timer/Kconfig.wch_ch32v00x" source "drivers/timer/Kconfig.cortex_m_systick" source "drivers/timer/Kconfig.esp32" diff --git a/drivers/timer/Kconfig.cc23x0_systim b/drivers/timer/Kconfig.cc23x0_systim new file mode 100644 index 00000000000..41db73e6d52 --- /dev/null +++ b/drivers/timer/Kconfig.cc23x0_systim @@ -0,0 +1,13 @@ +# Copyright (c) 2024 Texas Instruments Incorporated +# Copyright (c) 2024 BayLibre, SAS +# +# SPDX-License-Identifier: Apache-2.0 + +config CC23X0_SYSTIM_TIMER + bool "TI SimpleLink CC23X0 system clock timer" + default y + depends on HAS_CC23X0_SDK + select TICKLESS_CAPABLE + help + This module provides the "system clock driver" interfaces + for the TI Simplelink CC23X0 devices. diff --git a/drivers/timer/cc23x0_systim_timer.c b/drivers/timer/cc23x0_systim_timer.c new file mode 100644 index 00000000000..1bc264ba179 --- /dev/null +++ b/drivers/timer/cc23x0_systim_timer.c @@ -0,0 +1,154 @@ +/* + * Copyright (c) 2024 Texas Instruments Incorporated + * Copyright (c) 2024 BayLibre, SAS + * + * SPDX-License-Identifier: Apache-2.0 + */ + +#define DT_DRV_COMPAT ti_cc23x0_systim_timer + +/* + * TI SimpleLink CC23X0 timer driver based on SYSTIM + */ + +#include + +#include +#include +#include +#include +#include +#include +#include + +#include +#include +#include +#include + +/* Kernel tick period in microseconds (same timebase as systim) */ +#define TICK_PERIOD_MICRO_SEC (1000000 / CONFIG_SYS_CLOCK_TICKS_PER_SEC) + +/* + * Max number of systim ticks into the future + * + * Under the hood, the kernel timer uses the SysTimer whose events trigger + * immediately if the compare value is less than 2^22 systimer ticks in the past + * (4.194sec at 1us resolution). Therefore, the max number of SysTimer ticks you + * can schedule into the future is 2^32 - 2^22 - 1 ticks (~= 4290 sec at 1us + * resolution). + */ +#define SYSTIM_TIMEOUT_MAX 0xFFBFFFFFU + +/* Set systim interrupt to lowest priority */ +#define SYSTIM_ISR_PRIORITY 3U + +/* Keep track of systim counter at previous announcement to the kernel */ +static uint32_t last_systim_count; + +static void systim_isr(const void *arg); +static int sys_clock_driver_init(void); + +/* + * Set system clock timeout. + */ +void sys_clock_set_timeout(int32_t ticks, bool idle) +{ + ARG_UNUSED(idle); + + /* If timeout is necessary */ + if (ticks != K_TICKS_FOREVER) { + /* Get current value as early as possible */ + uint32_t now_tick = HWREG(SYSTIM_BASE + SYSTIM_O_TIME1U); + uint32_t timeout = ticks * TICK_PERIOD_MICRO_SEC; + + if (timeout > SYSTIM_TIMEOUT_MAX) { + timeout = SYSTIM_TIMEOUT_MAX; + } + /* This should wrap around */ + HWREG(SYSTIM_BASE + SYSTIM_O_CH0CC) = now_tick + timeout; + } +} + +uint32_t sys_clock_elapsed(void) +{ + /* Get current value as early as possible */ + uint32_t current_systim_count = HWREG(SYSTIM_BASE + SYSTIM_O_TIME1U); + uint32_t elapsed_systim; + + if (current_systim_count >= last_systim_count) { + elapsed_systim = current_systim_count - last_systim_count; + } else { + elapsed_systim = (UINT32_MAX - last_systim_count) + current_systim_count; + } + + int32_t elapsed_ticks = elapsed_systim / TICK_PERIOD_MICRO_SEC; + + return elapsed_ticks; +} + +uint32_t sys_clock_cycle_get_32(void) +{ + return HWREG(SYSTIM_BASE + SYSTIM_O_TIME1U); +} + +void systim_isr(const void *arg) +{ + /* Get current value as early as possible */ + uint32_t current_systim_count = HWREG(SYSTIM_BASE + SYSTIM_O_TIME1U); + uint32_t elapsed_systim; + + if (current_systim_count >= last_systim_count) { + elapsed_systim = current_systim_count - last_systim_count; + } else { + elapsed_systim = (UINT32_MAX - last_systim_count) + current_systim_count; + } + + int32_t elapsed_ticks = elapsed_systim / TICK_PERIOD_MICRO_SEC; + + sys_clock_announce(elapsed_ticks); + + last_systim_count = current_systim_count; + + /* Do not re-arm systim. Zephyr will do so through sys_clock_set_timeout */ +} + +static int sys_clock_driver_init(void) +{ + uint32_t now_tick; + + /* Get current value as early as possible */ + now_tick = HWREG(SYSTIM_BASE + SYSTIM_O_TIME1U); + last_systim_count = now_tick; + + /* Clear any pending interrupts on SysTimer channel 0 */ + HWREG(SYSTIM_BASE + SYSTIM_O_ICLR) = SYSTIM_ICLR_EV0_CLR; + + /* + * Configure SysTimer channel 0 to compare mode with timer + * resolution of 1 us. + */ + HWREG(SYSTIM_BASE + SYSTIM_O_CH0CFG) = 0; + + /* Make SysTimer halt on CPU debug halt */ + HWREG(SYSTIM_BASE + SYSTIM_O_EMU) = SYSTIM_EMU_HALT_STOP; + + HWREG(EVTSVT_BASE + EVTSVT_O_CPUIRQ16SEL) = EVTSVT_CPUIRQ16SEL_PUBID_SYSTIM0; + + /* + * Set IMASK for channel 0. IMASK is used by the power driver to know + * which systimer channels are active. + */ + HWREG(SYSTIM_BASE + SYSTIM_O_IMSET) = SYSTIM_IMSET_EV0_SET; + + /* This should wrap around and set a maximum timeout */ + HWREG(SYSTIM_BASE + SYSTIM_O_CH0CC) = now_tick + SYSTIM_TIMEOUT_MAX; + + /* Take configurable interrupt IRQ16 for systimer */ + IRQ_CONNECT(CPUIRQ16_IRQn, SYSTIM_ISR_PRIORITY, systim_isr, 0, 0); + irq_enable(CPUIRQ16_IRQn); + + return 0; +} + +SYS_INIT(sys_clock_driver_init, PRE_KERNEL_2, CONFIG_SYSTEM_CLOCK_INIT_PRIORITY); diff --git a/dts/bindings/timer/ti,cc23x0-timer.yaml b/dts/bindings/timer/ti,cc23x0-timer.yaml new file mode 100644 index 00000000000..94b70e1a145 --- /dev/null +++ b/dts/bindings/timer/ti,cc23x0-timer.yaml @@ -0,0 +1,15 @@ +# Copyright (c) 2024 Baylibre SAS +# SPDX-License-Identifier: Apache-2.0 + +description: TI SimpleLink CC23x0 Timer Node + +compatible: "ti,cc23x0-systim-timer" + +include: base.yaml + +properties: + reg: + required: true + + interrupts: + required: true