Skip to content

Commit 1c41423

Browse files
gmarullcarlescufi
authored andcommitted
drivers: timer: riscv_machine_timer: obtain registers/IRQ from DT
Obtain machine timer addresses and IRQ from Devicetree. Note that driver supports multiple compatibles because mtime/mtimecmp registers are implemented in different ways depending on the vendor. That means Devicetree representations can be slightly different and so code to collect the information needs to treat each compatible differently. Signed-off-by: Gerard Marull-Paretas <[email protected]>
1 parent 00f51ef commit 1c41423

File tree

1 file changed

+45
-8
lines changed

1 file changed

+45
-8
lines changed

drivers/timer/riscv_machine_timer.c

Lines changed: 45 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,47 @@
44
* SPDX-License-Identifier: Apache-2.0
55
*/
66
#include <zephyr/device.h>
7+
#include <zephyr/devicetree.h>
78
#include <zephyr/drivers/timer/system_timer.h>
89
#include <zephyr/sys_clock.h>
910
#include <zephyr/spinlock.h>
10-
#include <soc.h>
11+
12+
/* neorv32-machine-timer */
13+
#if DT_HAS_COMPAT_STATUS_OKAY(andestech_machine_timer)
14+
#define DT_DRV_COMPAT andestech_machine_timer
15+
16+
#define MTIME_REG DT_INST_REG_ADDR(0)
17+
#define MTIMECMP_REG (DT_INST_REG_ADDR(0) + 8)
18+
#define TIMER_IRQN DT_INST_IRQN(0)
19+
/* neorv32-machine-timer */
20+
#elif DT_HAS_COMPAT_STATUS_OKAY(neorv32_machine_timer)
21+
#define DT_DRV_COMPAT neorv32_machine_timer
22+
23+
#define MTIME_REG DT_INST_REG_ADDR(0)
24+
#define MTIMECMP_REG (DT_INST_REG_ADDR(0) + 8)
25+
#define TIMER_IRQN DT_INST_IRQN(0)
26+
/* nuclei,systimer */
27+
#elif DT_HAS_COMPAT_STATUS_OKAY(nuclei_systimer)
28+
#define DT_DRV_COMPAT nuclei_systimer
29+
30+
#define MTIME_REG DT_INST_REG_ADDR(0)
31+
#define MTIMECMP_REG (DT_INST_REG_ADDR(0) + 8)
32+
#define TIMER_IRQN DT_INST_IRQN(0)
33+
/* sifive,clint0 */
34+
#elif DT_HAS_COMPAT_STATUS_OKAY(sifive_clint0)
35+
#define DT_DRV_COMPAT sifive_clint0
36+
37+
#define MTIME_REG (DT_INST_REG_ADDR(0) + 0xbff8U)
38+
#define MTIMECMP_REG (DT_INST_REG_ADDR(0) + 0x4000U)
39+
#define TIMER_IRQN DT_INST_IRQ_BY_IDX(0, 1, irq)
40+
/* telink,machine-timer */
41+
#elif DT_HAS_COMPAT_STATUS_OKAY(telink_machine_timer)
42+
#define DT_DRV_COMPAT telink_machine_timer
43+
44+
#define MTIME_REG DT_INST_REG_ADDR(0)
45+
#define MTIMECMP_REG (DT_INST_REG_ADDR(0) + 8)
46+
#define TIMER_IRQN DT_INST_IRQN(0)
47+
#endif
1148

1249
#define CYC_PER_TICK ((uint32_t)((uint64_t) (sys_clock_hw_cycles_per_sec() \
1350
>> CONFIG_RISCV_MACHINE_TIMER_SYSTEM_CLOCK_DIVIDER) \
@@ -21,12 +58,12 @@
2158
static struct k_spinlock lock;
2259
static uint64_t last_count;
2360
#if defined(CONFIG_TEST)
24-
const int32_t z_sys_timer_irq_for_test = RISCV_MACHINE_TIMER_IRQ;
61+
const int32_t z_sys_timer_irq_for_test = TIMER_IRQN;
2562
#endif
2663

2764
static uint64_t get_hart_mtimecmp(void)
2865
{
29-
return RISCV_MTIMECMP_BASE + (_current_cpu->id * 8);
66+
return MTIMECMP_REG + (_current_cpu->id * 8);
3067
}
3168

3269
static void set_mtimecmp(uint64_t time)
@@ -51,9 +88,9 @@ static void set_mtimecmp(uint64_t time)
5188
static uint64_t mtime(void)
5289
{
5390
#ifdef CONFIG_64BIT
54-
return *(volatile uint64_t *)RISCV_MTIME_BASE;
91+
return *(volatile uint64_t *)MTIME_REG;
5592
#else
56-
volatile uint32_t *r = (uint32_t *)RISCV_MTIME_BASE;
93+
volatile uint32_t *r = (uint32_t *)MTIME_REG;
5794
uint32_t lo, hi;
5895

5996
/* Likewise, must guard against rollover when reading */
@@ -156,18 +193,18 @@ static int sys_clock_driver_init(const struct device *dev)
156193
{
157194
ARG_UNUSED(dev);
158195

159-
IRQ_CONNECT(RISCV_MACHINE_TIMER_IRQ, 0, timer_isr, NULL, 0);
196+
IRQ_CONNECT(TIMER_IRQN, 0, timer_isr, NULL, 0);
160197
last_count = mtime();
161198
set_mtimecmp(last_count + CYC_PER_TICK);
162-
irq_enable(RISCV_MACHINE_TIMER_IRQ);
199+
irq_enable(TIMER_IRQN);
163200
return 0;
164201
}
165202

166203
#ifdef CONFIG_SMP
167204
void smp_timer_init(void)
168205
{
169206
set_mtimecmp(last_count + CYC_PER_TICK);
170-
irq_enable(RISCV_MACHINE_TIMER_IRQ);
207+
irq_enable(TIMER_IRQN);
171208
}
172209
#endif
173210

0 commit comments

Comments
 (0)