| 1 |
/* timer.c — ARM Generic Timer driver */ |
| 2 |
|
| 3 |
#include "timer.h" |
| 4 |
#include "gic.h" |
| 5 |
|
| 6 |
static volatile uint64_t ticks_count = 0; |
| 7 |
|
| 8 |
void timer_init(void) { |
| 9 |
uint64_t freq = timer_get_freq(); |
| 10 |
|
| 11 |
/* Set timer interval for 100Hz (freq/100 ticks per interrupt) */ |
| 12 |
uint64_t interval = freq / 100; |
| 13 |
__asm__ volatile("msr cntp_tval_el0, %0" :: "r"(interval)); |
| 14 |
|
| 15 |
/* Enable timer: unmask (bit 1=0), enable (bit 0=1) */ |
| 16 |
__asm__ volatile("msr cntp_ctl_el0, %0" :: "r"(1UL)); |
| 17 |
|
| 18 |
/* Enable timer IRQ on GIC */ |
| 19 |
gic_enable_irq(IRQ_TIMER_CNTP); |
| 20 |
} |
| 21 |
|
| 22 |
void timer_irq(void) { |
| 23 |
ticks_count++; |
| 24 |
|
| 25 |
/* Acknowledge timer interrupt by writing new interval */ |
| 26 |
uint64_t freq = timer_get_freq(); |
| 27 |
__asm__ volatile("msr cntp_tval_el0, %0" :: "r"(freq / 100)); |
| 28 |
} |
| 29 |
|
| 30 |
uint64_t timer_ticks(void) { |
| 31 |
return ticks_count; |
| 32 |
} |
| 33 |
|