Skip to content

Commit 9f5c72e

Browse files
committed
drivers: counter: rts5912: add support timer32 counter driver
Port rts5912 timer32 counter driver on Zephyr Signed-off-by: Titan Chen <[email protected]>
1 parent 19c6240 commit 9f5c72e

File tree

7 files changed

+510
-0
lines changed

7 files changed

+510
-0
lines changed

drivers/counter/CMakeLists.txt

+1
Original file line numberDiff line numberDiff line change
@@ -54,3 +54,4 @@ zephyr_library_sources_ifdef(CONFIG_COUNTER_RTC_MAX32 counter_max32_rt
5454
zephyr_library_sources_ifdef(CONFIG_COUNTER_NXP_MRT counter_nxp_mrt.c)
5555
zephyr_library_sources_ifdef(CONFIG_COUNTER_RA_AGT counter_renesas_ra_agt.c)
5656
zephyr_library_sources_ifdef(CONFIG_COUNTER_RENESAS_RZ_GTM counter_renesas_rz_gtm.c)
57+
zephyr_library_sources_ifdef(CONFIG_COUNTER_REALTEK_RTS5912 counter_realtek_rts5912.c)

drivers/counter/Kconfig

+2
Original file line numberDiff line numberDiff line change
@@ -106,4 +106,6 @@ source "drivers/counter/Kconfig.renesas_ra"
106106

107107
source "drivers/counter/Kconfig.renesas_rz"
108108

109+
source "drivers/counter/Kconfig.rts5912"
110+
109111
endif # COUNTER

drivers/counter/Kconfig.rts5912

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# Realtek counter configuration options
2+
3+
# Copyright (c) 2025 Realtek Corporation
4+
# SPDX-License-Identifier: Apache-2.0
5+
6+
config COUNTER_REALTEK_RTS5912
7+
bool "Realtek rts5912 series counter driver"
8+
default y
9+
depends on DT_HAS_REALTEK_RTS5912_TIMER_ENABLED
10+
help
11+
Enable counter driver for Realtek RTS5912 MCU series. Such driver
12+
will expose the basic timer devices present on the MCU.
+353
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,353 @@
1+
/*
2+
* Copyright (c) 2025 Realtek Corporation
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
#define DT_DRV_COMPAT realtek_rts5912_timer
8+
9+
/**
10+
* @file
11+
* @brief Realtek RTS5912 Counter driver
12+
*
13+
* This is the driver for the 32-bit counters on the Realtek SoCs.
14+
*
15+
* Notes:
16+
* - The counters are running in down counting mode.
17+
* - Interrupts are triggered (if enabled) when the counter
18+
* reaches zero.
19+
* - These are not free running counters where there are separate
20+
* compare values for interrupts. When setting single shot alarms,
21+
* the counter values are changed so that interrupts are triggered
22+
* when the counters reach zero.
23+
*/
24+
25+
#include <zephyr/kernel.h>
26+
#include <zephyr/drivers/counter.h>
27+
#include <soc.h>
28+
#include <errno.h>
29+
#include <stdbool.h>
30+
#include "reg/reg_timer.h"
31+
#include "reg/reg_system.h"
32+
#include <zephyr/drivers/clock_control.h>
33+
#include <zephyr/drivers/clock_control/clock_control_rts5912.h>
34+
#include <zephyr/logging/log.h>
35+
LOG_MODULE_REGISTER(counter_realtek_rts5912, CONFIG_COUNTER_LOG_LEVEL);
36+
37+
struct counter_rts5912_config {
38+
struct counter_config_info info;
39+
void (*config_func)(void);
40+
volatile struct timer32_type *base_address;
41+
uint16_t prescaler;
42+
uint32_t clk_grp;
43+
uint32_t clk_idx;
44+
const struct device *clk_dev;
45+
};
46+
47+
struct counter_rts5912_data {
48+
counter_alarm_callback_t alarm_cb;
49+
counter_top_callback_t top_cb;
50+
void *user_data;
51+
};
52+
53+
#define COUNTER_RTS5912_REG_BASE(_dev) \
54+
((const struct counter_rts5912_config *const)_dev->config)->base_address
55+
56+
static int counter_rts5912_start(const struct device *dev)
57+
{
58+
const struct counter_rts5912_config *config = dev->config;
59+
volatile struct timer32_type *counter = config->base_address;
60+
61+
if (counter->ctrl & TIMER32_CTRL_EN) {
62+
return 0;
63+
}
64+
65+
counter->ctrl |= (TIMER32_CTRL_EN);
66+
67+
LOG_DBG("%p Counter started", dev);
68+
69+
return 0;
70+
}
71+
72+
static int counter_rts5912_stop(const struct device *dev)
73+
{
74+
const struct counter_rts5912_config *config = dev->config;
75+
volatile struct timer32_type *counter = config->base_address;
76+
77+
if (!(counter->ctrl & TIMER32_CTRL_EN)) {
78+
/* Already stopped, nothing to do */
79+
return 0;
80+
}
81+
/* disable timer and disable interrupt. */
82+
counter->ctrl = TIMER32_CTRL_INTEN_DIS;
83+
counter->ldcnt = counter->cnt;
84+
/* w1c interrupt pending status */
85+
counter->intclr |= TIMER32_INTCLR_INTCLR;
86+
87+
LOG_DBG("%p Counter stopped", dev);
88+
89+
return 0;
90+
}
91+
92+
static int counter_rts5912_get_value(const struct device *dev, uint32_t *ticks)
93+
{
94+
const struct counter_rts5912_config *config = dev->config;
95+
volatile struct timer32_type *counter = config->base_address;
96+
97+
*ticks = counter->cnt + 1;
98+
return 0;
99+
}
100+
101+
static int counter_rts5912_set_alarm(const struct device *dev, uint8_t chan_id,
102+
const struct counter_alarm_cfg *alarm_cfg)
103+
{
104+
struct counter_rts5912_data *data = dev->data;
105+
const struct counter_rts5912_config *counter_cfg = dev->config;
106+
volatile struct timer32_type *counter = counter_cfg->base_address;
107+
108+
if (chan_id != 0) {
109+
LOG_ERR("Invalid channel id %u", chan_id);
110+
return -ENOTSUP;
111+
}
112+
113+
/* Interrupts are only triggered when the counter reaches 0.
114+
* So only relative alarms are supported.
115+
*/
116+
if (alarm_cfg->flags & COUNTER_ALARM_CFG_ABSOLUTE) {
117+
return -ENOTSUP;
118+
}
119+
120+
if (data->alarm_cb != NULL) {
121+
return -EBUSY;
122+
}
123+
124+
if (!alarm_cfg->callback) {
125+
return -EINVAL;
126+
}
127+
128+
if (alarm_cfg->ticks > counter_cfg->info.max_top_value) {
129+
return -EINVAL;
130+
}
131+
/* disable timer */
132+
counter->ctrl &= ~TIMER32_CTRL_EN;
133+
/* disable interrupt */
134+
counter->ctrl |= TIMER32_CTRL_INTEN_DIS;
135+
/* set in one-shot mode */
136+
counter->ctrl &= ~TIMER32_CTRL_MDSELS_PERIOD;
137+
/* set load counter */
138+
counter->ldcnt = alarm_cfg->ticks;
139+
140+
data->alarm_cb = alarm_cfg->callback;
141+
data->user_data = alarm_cfg->user_data;
142+
/* w1c interrupt status */
143+
counter->intclr |= TIMER32_INTCLR_INTCLR;
144+
/* enable interrupt */
145+
counter->ctrl &= ~TIMER32_CTRL_INTEN_DIS;
146+
147+
LOG_DBG("%p Counter alarm set to %u ticks", dev, alarm_cfg->ticks);
148+
/* enable timer and re-load PRCNT to CNT */
149+
counter->ctrl |= TIMER32_CTRL_EN;
150+
151+
return 0;
152+
}
153+
154+
static int counter_rts5912_cancel_alarm(const struct device *dev, uint8_t chan_id)
155+
{
156+
struct counter_rts5912_data *data = dev->data;
157+
const struct counter_rts5912_config *config = dev->config;
158+
volatile struct timer32_type *counter = config->base_address;
159+
160+
if (chan_id != 0) {
161+
LOG_ERR("Invalid channel id %u", chan_id);
162+
return -ENOTSUP;
163+
}
164+
165+
counter->ctrl = 0;
166+
167+
data->alarm_cb = NULL;
168+
data->user_data = NULL;
169+
170+
LOG_DBG("%p Counter alarm canceled", dev);
171+
172+
return 0;
173+
}
174+
175+
static uint32_t counter_rts5912_get_pending_int(const struct device *dev)
176+
{
177+
const struct counter_rts5912_config *config = dev->config;
178+
volatile struct timer32_type *counter = config->base_address;
179+
180+
return counter->intsts;
181+
}
182+
183+
static uint32_t counter_rts5912_get_top_value(const struct device *dev)
184+
{
185+
const struct counter_rts5912_config *config = dev->config;
186+
volatile struct timer32_type *counter = config->base_address;
187+
188+
return counter->ldcnt;
189+
}
190+
191+
static int counter_rts5912_set_top_value(const struct device *dev,
192+
const struct counter_top_cfg *cfg)
193+
{
194+
const struct counter_rts5912_config *counter_cfg = dev->config;
195+
struct counter_rts5912_data *data = dev->data;
196+
volatile struct timer32_type *counter = counter_cfg->base_address;
197+
int ret = 0;
198+
199+
if (data->alarm_cb) {
200+
return -EBUSY;
201+
}
202+
203+
if (cfg->ticks > counter_cfg->info.max_top_value) {
204+
return -EINVAL;
205+
}
206+
207+
counter->ctrl &= ~TIMER32_CTRL_EN;
208+
counter->ctrl |= TIMER32_CTRL_INTEN_DIS;
209+
210+
counter->ldcnt = cfg->ticks;
211+
212+
data->top_cb = cfg->callback;
213+
data->user_data = cfg->user_data;
214+
215+
if (data->top_cb) {
216+
/* w1c interrupt status */
217+
counter->intclr |= TIMER32_INTCLR_INTCLR;
218+
/* enable interrupt */
219+
counter->ctrl &= ~TIMER32_CTRL_INTEN_DIS;
220+
/* enable periodic alarm mode */
221+
counter->ctrl |= TIMER32_CTRL_MDSELS_PERIOD;
222+
} else {
223+
counter->ctrl = TIMER32_CTRL_INTEN_DIS;
224+
}
225+
226+
LOG_DBG("%p Counter top value was set to %u", dev, cfg->ticks);
227+
228+
counter->ctrl |= TIMER32_CTRL_EN;
229+
230+
return ret;
231+
}
232+
233+
static void counter_rts5912_isr(const struct device *dev)
234+
{
235+
struct counter_rts5912_data *data = dev->data;
236+
const struct counter_rts5912_config *config = dev->config;
237+
volatile struct timer32_type *counter = config->base_address;
238+
counter_alarm_callback_t alarm_cb;
239+
void *user_data;
240+
241+
/* disable timer */
242+
counter->ctrl &= ~TIMER32_CTRL_EN;
243+
/* disable interrupt */
244+
counter->ctrl |= TIMER32_CTRL_INTEN_DIS;
245+
/* clear interrupt pending status */
246+
counter->intclr |= TIMER32_INTCLR_INTCLR;
247+
248+
LOG_DBG("%p Counter ISR", dev);
249+
250+
if (data->alarm_cb) {
251+
/* Alarm is one-shot, so disable callback */
252+
alarm_cb = data->alarm_cb;
253+
data->alarm_cb = NULL;
254+
user_data = data->user_data;
255+
256+
alarm_cb(dev, 0, counter->cnt + 1, user_data);
257+
} else if (data->top_cb) {
258+
data->top_cb(dev, data->user_data);
259+
/* periodic alarm mode, enable interrupt */
260+
counter->ctrl &= ~TIMER32_CTRL_INTEN_DIS;
261+
/* enable timer again */
262+
counter->ctrl |= TIMER32_CTRL_EN;
263+
}
264+
}
265+
266+
static uint32_t counter_rts5912_get_freq(const struct device *dev)
267+
{
268+
const struct counter_rts5912_config *counter_cfg = dev->config;
269+
270+
return counter_cfg->info.freq;
271+
}
272+
273+
static DEVICE_API(counter, counter_rts5912_api) = {
274+
.start = counter_rts5912_start,
275+
.stop = counter_rts5912_stop,
276+
.get_value = counter_rts5912_get_value,
277+
.set_alarm = counter_rts5912_set_alarm,
278+
.cancel_alarm = counter_rts5912_cancel_alarm,
279+
.set_top_value = counter_rts5912_set_top_value,
280+
.get_pending_int = counter_rts5912_get_pending_int,
281+
.get_top_value = counter_rts5912_get_top_value,
282+
.get_freq = counter_rts5912_get_freq,
283+
};
284+
285+
static int counter_rts5912_init(const struct device *dev)
286+
{
287+
const struct counter_rts5912_config *counter_cfg = dev->config;
288+
volatile struct timer32_type *counter = counter_cfg->base_address;
289+
int rc;
290+
struct rts5912_sccon_subsys sccon_subsys = {
291+
.clk_grp = counter_cfg->clk_grp,
292+
.clk_idx = counter_cfg->clk_idx,
293+
};
294+
295+
if (!device_is_ready(counter_cfg->clk_dev)) {
296+
LOG_ERR("device is not ready");
297+
return -ENODEV;
298+
}
299+
300+
rc = clock_control_on(counter_cfg->clk_dev, (clock_control_subsys_t)&sccon_subsys);
301+
if (rc != 0) {
302+
LOG_ERR("clock power on fail");
303+
return rc;
304+
}
305+
306+
counter_rts5912_stop(dev);
307+
308+
/* Set preload and actually pre-load the counter */
309+
counter->ldcnt = counter_cfg->info.max_top_value;
310+
counter->cnt = counter_cfg->info.max_top_value;
311+
312+
counter_cfg->config_func();
313+
LOG_DBG("Init complete!");
314+
return 0;
315+
}
316+
317+
#define DEV_CONFIG_CLK_DEV_INIT(n) \
318+
.clk_dev = DEVICE_DT_GET(DT_INST_CLOCKS_CTLR(n)), \
319+
.clk_grp = DT_INST_CLOCKS_CELL_BY_NAME(n, tmr32, clk_grp), \
320+
.clk_idx = DT_INST_CLOCKS_CELL_BY_NAME(n, tmr32, clk_idx),
321+
322+
#define COUNTER_RTS5912_INIT(inst) \
323+
static void counter_rts5912_irq_config_##inst(void); \
324+
\
325+
static struct counter_rts5912_data counter_rts5912_dev_data_##inst; \
326+
\
327+
static struct counter_rts5912_config counter_rts5912_dev_config_##inst = { \
328+
.info = \
329+
{ \
330+
.max_top_value = DT_INST_PROP(inst, max_value), \
331+
.freq = DT_INST_PROP(inst, clock_frequency) / \
332+
(1 << DT_INST_PROP(inst, prescaler)), \
333+
.flags = 0, \
334+
.channels = 1, \
335+
}, \
336+
\
337+
.config_func = counter_rts5912_irq_config_##inst, \
338+
.base_address = (struct timer32_type *)DT_INST_REG_ADDR(inst), \
339+
.prescaler = DT_INST_PROP(inst, prescaler), \
340+
DEV_CONFIG_CLK_DEV_INIT(inst)}; \
341+
\
342+
DEVICE_DT_INST_DEFINE(inst, counter_rts5912_init, NULL, &counter_rts5912_dev_data_##inst, \
343+
&counter_rts5912_dev_config_##inst, PRE_KERNEL_1, \
344+
CONFIG_COUNTER_INIT_PRIORITY, &counter_rts5912_api); \
345+
\
346+
static void counter_rts5912_irq_config_##inst(void) \
347+
{ \
348+
IRQ_CONNECT(DT_INST_IRQN(inst), DT_INST_IRQ(inst, priority), counter_rts5912_isr, \
349+
DEVICE_DT_INST_GET(inst), 0); \
350+
irq_enable(DT_INST_IRQN(inst)); \
351+
}
352+
353+
DT_INST_FOREACH_STATUS_OKAY(COUNTER_RTS5912_INIT)

0 commit comments

Comments
 (0)