Skip to content

Commit 10c35b8

Browse files
vmyklebustkartben
authored andcommitted
drivers: timer: Add support for cc23x0 systim
Add support for systim to cc23x0 SoC. Signed-off-by: Lars Thalian Morstad <l-morstad@ti.com> Signed-off-by: Vebjorn Myklebust <v.myklebust@ti.com> Signed-off-by: Stoyan Bogdanov <sbogdanov@baylibre.com> Signed-off-by: Julien Panis <jpanis@baylibre.com>
1 parent 496c815 commit 10c35b8

File tree

5 files changed

+184
-0
lines changed

5 files changed

+184
-0
lines changed

drivers/timer/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ zephyr_library_sources_ifdef(CONFIG_ARCV2_TIMER arcv2_timer0.c)
1212
zephyr_library_sources_ifdef(CONFIG_ARM_ARCH_TIMER arm_arch_timer.c)
1313
zephyr_library_sources_ifdef(CONFIG_INTEL_ADSP_TIMER intel_adsp_timer.c)
1414
zephyr_library_sources_ifdef(CONFIG_CC13XX_CC26XX_RTC_TIMER cc13xx_cc26xx_rtc_timer.c)
15+
zephyr_library_sources_ifdef(CONFIG_CC23X0_SYSTIM_TIMER cc23x0_systim_timer.c)
1516
zephyr_library_sources_ifdef(CONFIG_CH32V00X_SYSTICK wch_systick_ch32v00x.c)
1617
zephyr_library_sources_ifdef(CONFIG_CORTEX_M_SYSTICK cortex_m_systick.c)
1718
zephyr_library_sources_ifdef(CONFIG_ESP32_SYS_TIMER esp32_sys_timer.c)

drivers/timer/Kconfig

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ source "drivers/timer/Kconfig.arcv2"
7070
source "drivers/timer/Kconfig.arm_arch"
7171
source "drivers/timer/Kconfig.cavs"
7272
source "drivers/timer/Kconfig.cc13xx_cc26xx_rtc"
73+
source "drivers/timer/Kconfig.cc23x0_systim"
7374
source "drivers/timer/Kconfig.wch_ch32v00x"
7475
source "drivers/timer/Kconfig.cortex_m_systick"
7576
source "drivers/timer/Kconfig.esp32"

drivers/timer/Kconfig.cc23x0_systim

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# Copyright (c) 2024 Texas Instruments Incorporated
2+
# Copyright (c) 2024 BayLibre, SAS
3+
#
4+
# SPDX-License-Identifier: Apache-2.0
5+
6+
config CC23X0_SYSTIM_TIMER
7+
bool "TI SimpleLink CC23X0 system clock timer"
8+
default y
9+
depends on HAS_CC23X0_SDK
10+
select TICKLESS_CAPABLE
11+
help
12+
This module provides the "system clock driver" interfaces
13+
for the TI Simplelink CC23X0 devices.

drivers/timer/cc23x0_systim_timer.c

Lines changed: 154 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,154 @@
1+
/*
2+
* Copyright (c) 2024 Texas Instruments Incorporated
3+
* Copyright (c) 2024 BayLibre, SAS
4+
*
5+
* SPDX-License-Identifier: Apache-2.0
6+
*/
7+
8+
#define DT_DRV_COMPAT ti_cc23x0_systim_timer
9+
10+
/*
11+
* TI SimpleLink CC23X0 timer driver based on SYSTIM
12+
*/
13+
14+
#include <soc.h>
15+
16+
#include <zephyr/device.h>
17+
#include <zephyr/drivers/clock_control.h>
18+
#include <zephyr/drivers/timer/system_timer.h>
19+
#include <zephyr/irq.h>
20+
#include <zephyr/spinlock.h>
21+
#include <zephyr/sys_clock.h>
22+
#include <zephyr/sys/util.h>
23+
24+
#include <inc/hw_types.h>
25+
#include <inc/hw_memmap.h>
26+
#include <inc/hw_systim.h>
27+
#include <inc/hw_evtsvt.h>
28+
29+
/* Kernel tick period in microseconds (same timebase as systim) */
30+
#define TICK_PERIOD_MICRO_SEC (1000000 / CONFIG_SYS_CLOCK_TICKS_PER_SEC)
31+
32+
/*
33+
* Max number of systim ticks into the future
34+
*
35+
* Under the hood, the kernel timer uses the SysTimer whose events trigger
36+
* immediately if the compare value is less than 2^22 systimer ticks in the past
37+
* (4.194sec at 1us resolution). Therefore, the max number of SysTimer ticks you
38+
* can schedule into the future is 2^32 - 2^22 - 1 ticks (~= 4290 sec at 1us
39+
* resolution).
40+
*/
41+
#define SYSTIM_TIMEOUT_MAX 0xFFBFFFFFU
42+
43+
/* Set systim interrupt to lowest priority */
44+
#define SYSTIM_ISR_PRIORITY 3U
45+
46+
/* Keep track of systim counter at previous announcement to the kernel */
47+
static uint32_t last_systim_count;
48+
49+
static void systim_isr(const void *arg);
50+
static int sys_clock_driver_init(void);
51+
52+
/*
53+
* Set system clock timeout.
54+
*/
55+
void sys_clock_set_timeout(int32_t ticks, bool idle)
56+
{
57+
ARG_UNUSED(idle);
58+
59+
/* If timeout is necessary */
60+
if (ticks != K_TICKS_FOREVER) {
61+
/* Get current value as early as possible */
62+
uint32_t now_tick = HWREG(SYSTIM_BASE + SYSTIM_O_TIME1U);
63+
uint32_t timeout = ticks * TICK_PERIOD_MICRO_SEC;
64+
65+
if (timeout > SYSTIM_TIMEOUT_MAX) {
66+
timeout = SYSTIM_TIMEOUT_MAX;
67+
}
68+
/* This should wrap around */
69+
HWREG(SYSTIM_BASE + SYSTIM_O_CH0CC) = now_tick + timeout;
70+
}
71+
}
72+
73+
uint32_t sys_clock_elapsed(void)
74+
{
75+
/* Get current value as early as possible */
76+
uint32_t current_systim_count = HWREG(SYSTIM_BASE + SYSTIM_O_TIME1U);
77+
uint32_t elapsed_systim;
78+
79+
if (current_systim_count >= last_systim_count) {
80+
elapsed_systim = current_systim_count - last_systim_count;
81+
} else {
82+
elapsed_systim = (UINT32_MAX - last_systim_count) + current_systim_count;
83+
}
84+
85+
int32_t elapsed_ticks = elapsed_systim / TICK_PERIOD_MICRO_SEC;
86+
87+
return elapsed_ticks;
88+
}
89+
90+
uint32_t sys_clock_cycle_get_32(void)
91+
{
92+
return HWREG(SYSTIM_BASE + SYSTIM_O_TIME1U);
93+
}
94+
95+
void systim_isr(const void *arg)
96+
{
97+
/* Get current value as early as possible */
98+
uint32_t current_systim_count = HWREG(SYSTIM_BASE + SYSTIM_O_TIME1U);
99+
uint32_t elapsed_systim;
100+
101+
if (current_systim_count >= last_systim_count) {
102+
elapsed_systim = current_systim_count - last_systim_count;
103+
} else {
104+
elapsed_systim = (UINT32_MAX - last_systim_count) + current_systim_count;
105+
}
106+
107+
int32_t elapsed_ticks = elapsed_systim / TICK_PERIOD_MICRO_SEC;
108+
109+
sys_clock_announce(elapsed_ticks);
110+
111+
last_systim_count = current_systim_count;
112+
113+
/* Do not re-arm systim. Zephyr will do so through sys_clock_set_timeout */
114+
}
115+
116+
static int sys_clock_driver_init(void)
117+
{
118+
uint32_t now_tick;
119+
120+
/* Get current value as early as possible */
121+
now_tick = HWREG(SYSTIM_BASE + SYSTIM_O_TIME1U);
122+
last_systim_count = now_tick;
123+
124+
/* Clear any pending interrupts on SysTimer channel 0 */
125+
HWREG(SYSTIM_BASE + SYSTIM_O_ICLR) = SYSTIM_ICLR_EV0_CLR;
126+
127+
/*
128+
* Configure SysTimer channel 0 to compare mode with timer
129+
* resolution of 1 us.
130+
*/
131+
HWREG(SYSTIM_BASE + SYSTIM_O_CH0CFG) = 0;
132+
133+
/* Make SysTimer halt on CPU debug halt */
134+
HWREG(SYSTIM_BASE + SYSTIM_O_EMU) = SYSTIM_EMU_HALT_STOP;
135+
136+
HWREG(EVTSVT_BASE + EVTSVT_O_CPUIRQ16SEL) = EVTSVT_CPUIRQ16SEL_PUBID_SYSTIM0;
137+
138+
/*
139+
* Set IMASK for channel 0. IMASK is used by the power driver to know
140+
* which systimer channels are active.
141+
*/
142+
HWREG(SYSTIM_BASE + SYSTIM_O_IMSET) = SYSTIM_IMSET_EV0_SET;
143+
144+
/* This should wrap around and set a maximum timeout */
145+
HWREG(SYSTIM_BASE + SYSTIM_O_CH0CC) = now_tick + SYSTIM_TIMEOUT_MAX;
146+
147+
/* Take configurable interrupt IRQ16 for systimer */
148+
IRQ_CONNECT(CPUIRQ16_IRQn, SYSTIM_ISR_PRIORITY, systim_isr, 0, 0);
149+
irq_enable(CPUIRQ16_IRQn);
150+
151+
return 0;
152+
}
153+
154+
SYS_INIT(sys_clock_driver_init, PRE_KERNEL_2, CONFIG_SYSTEM_CLOCK_INIT_PRIORITY);
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# Copyright (c) 2024 Baylibre SAS
2+
# SPDX-License-Identifier: Apache-2.0
3+
4+
description: TI SimpleLink CC23x0 Timer Node
5+
6+
compatible: "ti,cc23x0-systim-timer"
7+
8+
include: base.yaml
9+
10+
properties:
11+
reg:
12+
required: true
13+
14+
interrupts:
15+
required: true

0 commit comments

Comments
 (0)