4
4
* SPDX-License-Identifier: Apache-2.0
5
5
*/
6
6
#include <zephyr/device.h>
7
+ #include <zephyr/devicetree.h>
7
8
#include <zephyr/drivers/timer/system_timer.h>
8
9
#include <zephyr/sys_clock.h>
9
10
#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
11
48
12
49
#define CYC_PER_TICK ((uint32_t)((uint64_t) (sys_clock_hw_cycles_per_sec() \
13
50
>> CONFIG_RISCV_MACHINE_TIMER_SYSTEM_CLOCK_DIVIDER) \
21
58
static struct k_spinlock lock ;
22
59
static uint64_t last_count ;
23
60
#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 ;
25
62
#endif
26
63
27
64
static uint64_t get_hart_mtimecmp (void )
28
65
{
29
- return RISCV_MTIMECMP_BASE + (_current_cpu -> id * 8 );
66
+ return MTIMECMP_REG + (_current_cpu -> id * 8 );
30
67
}
31
68
32
69
static void set_mtimecmp (uint64_t time )
@@ -51,9 +88,9 @@ static void set_mtimecmp(uint64_t time)
51
88
static uint64_t mtime (void )
52
89
{
53
90
#ifdef CONFIG_64BIT
54
- return * (volatile uint64_t * )RISCV_MTIME_BASE ;
91
+ return * (volatile uint64_t * )MTIME_REG ;
55
92
#else
56
- volatile uint32_t * r = (uint32_t * )RISCV_MTIME_BASE ;
93
+ volatile uint32_t * r = (uint32_t * )MTIME_REG ;
57
94
uint32_t lo , hi ;
58
95
59
96
/* Likewise, must guard against rollover when reading */
@@ -156,18 +193,18 @@ static int sys_clock_driver_init(const struct device *dev)
156
193
{
157
194
ARG_UNUSED (dev );
158
195
159
- IRQ_CONNECT (RISCV_MACHINE_TIMER_IRQ , 0 , timer_isr , NULL , 0 );
196
+ IRQ_CONNECT (TIMER_IRQN , 0 , timer_isr , NULL , 0 );
160
197
last_count = mtime ();
161
198
set_mtimecmp (last_count + CYC_PER_TICK );
162
- irq_enable (RISCV_MACHINE_TIMER_IRQ );
199
+ irq_enable (TIMER_IRQN );
163
200
return 0 ;
164
201
}
165
202
166
203
#ifdef CONFIG_SMP
167
204
void smp_timer_init (void )
168
205
{
169
206
set_mtimecmp (last_count + CYC_PER_TICK );
170
- irq_enable (RISCV_MACHINE_TIMER_IRQ );
207
+ irq_enable (TIMER_IRQN );
171
208
}
172
209
#endif
173
210
0 commit comments