1 /*
2 * Copyright (c) 2018 - 2019 Antmicro <www.antmicro.com>
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 */
6
7 #define DT_DRV_COMPAT litex_timer0
8
9 #include <zephyr/kernel.h>
10 #include <zephyr/arch/cpu.h>
11 #include <zephyr/init.h>
12 #include <zephyr/irq.h>
13 #include <zephyr/spinlock.h>
14 #include <zephyr/drivers/timer/system_timer.h>
15
16 #include <soc.h>
17
18 #define TIMER_LOAD_ADDR DT_INST_REG_ADDR_BY_NAME(0, load)
19 #define TIMER_RELOAD_ADDR DT_INST_REG_ADDR_BY_NAME(0, reload)
20 #define TIMER_EN_ADDR DT_INST_REG_ADDR_BY_NAME(0, en)
21 #define TIMER_UPDATE_VALUE_ADDR DT_INST_REG_ADDR_BY_NAME(0, update_value)
22 #define TIMER_VALUE_ADDR DT_INST_REG_ADDR_BY_NAME(0, value)
23 #define TIMER_EV_STATUS_ADDR DT_INST_REG_ADDR_BY_NAME(0, ev_status)
24 #define TIMER_EV_PENDING_ADDR DT_INST_REG_ADDR_BY_NAME(0, ev_pending)
25 #define TIMER_EV_ENABLE_ADDR DT_INST_REG_ADDR_BY_NAME(0, ev_enable)
26 #define TIMER_UPTIME_LATCH_ADDR DT_INST_REG_ADDR_BY_NAME(0, uptime_latch)
27 #define TIMER_UPTIME_CYCLES_ADDR DT_INST_REG_ADDR_BY_NAME(0, uptime_cycles)
28
29 #define TIMER_EV 0x1
30 #define TIMER_IRQ DT_INST_IRQN(0)
31 #define TIMER_DISABLE 0x0
32 #define TIMER_ENABLE 0x1
33 #define TIMER_UPTIME_LATCH 0x1
34 #if defined(CONFIG_TEST)
35 const int32_t z_sys_timer_irq_for_test = TIMER_IRQ;
36 #endif
37
litex_timer_irq_handler(const void * device)38 static void litex_timer_irq_handler(const void *device)
39 {
40 unsigned int key = irq_lock();
41
42 litex_write8(TIMER_EV, TIMER_EV_PENDING_ADDR);
43 sys_clock_announce(1);
44
45 irq_unlock(key);
46 }
47
sys_clock_cycle_get_32(void)48 uint32_t sys_clock_cycle_get_32(void)
49 {
50 static struct k_spinlock lock;
51 uint32_t uptime_cycles;
52 k_spinlock_key_t key = k_spin_lock(&lock);
53
54 litex_write8(TIMER_UPTIME_LATCH, TIMER_UPTIME_LATCH_ADDR);
55 uptime_cycles = (uint32_t)litex_read64(TIMER_UPTIME_CYCLES_ADDR);
56
57 k_spin_unlock(&lock, key);
58
59 return uptime_cycles;
60 }
61
sys_clock_cycle_get_64(void)62 uint64_t sys_clock_cycle_get_64(void)
63 {
64 static struct k_spinlock lock;
65 uint64_t uptime_cycles;
66 k_spinlock_key_t key = k_spin_lock(&lock);
67
68 litex_write8(TIMER_UPTIME_LATCH, TIMER_UPTIME_LATCH_ADDR);
69 uptime_cycles = litex_read64(TIMER_UPTIME_CYCLES_ADDR);
70
71 k_spin_unlock(&lock, key);
72
73 return uptime_cycles;
74 }
75
76 /* tickless kernel is not supported */
sys_clock_elapsed(void)77 uint32_t sys_clock_elapsed(void)
78 {
79 return 0;
80 }
81
sys_clock_driver_init(void)82 static int sys_clock_driver_init(void)
83 {
84 IRQ_CONNECT(TIMER_IRQ, DT_INST_IRQ(0, priority),
85 litex_timer_irq_handler, NULL, 0);
86 irq_enable(TIMER_IRQ);
87
88 litex_write8(TIMER_DISABLE, TIMER_EN_ADDR);
89
90 litex_write32(k_ticks_to_cyc_floor32(1), TIMER_RELOAD_ADDR);
91 litex_write32(k_ticks_to_cyc_floor32(1), TIMER_LOAD_ADDR);
92
93 litex_write8(TIMER_ENABLE, TIMER_EN_ADDR);
94 litex_write8(litex_read8(TIMER_EV_PENDING_ADDR), TIMER_EV_PENDING_ADDR);
95 litex_write8(TIMER_EV, TIMER_EV_ENABLE_ADDR);
96
97 return 0;
98 }
99
100 SYS_INIT(sys_clock_driver_init, PRE_KERNEL_2,
101 CONFIG_SYSTEM_CLOCK_INIT_PRIORITY);
102