| 1 |
|
| 2 |
|
| 3 |
#include <device/mmio.h> |
| 4 |
#include <timer.h> |
| 5 |
#include <delay.h> |
| 6 |
#include <thread.h> |
| 7 |
|
| 8 |
#include <soc/addressmap.h> |
| 9 |
#include <soc/timer.h> |
| 10 |
|
| 11 |
static struct mtk_gpt_regs *const mtk_gpt = (void *)GPT_BASE; |
| 12 |
|
| 13 |
__weak void timer_prepare(void) { } |
| 14 |
|
| 15 |
static uint64_t timer_raw_value(void) |
| 16 |
{ |
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
uint32_t low = read32(&mtk_gpt->gpt6_cnt_l); |
| 23 |
uint32_t high = read32(&mtk_gpt->gpt6_cnt_h); |
| 24 |
|
| 25 |
return low | (uint64_t)high << 32; |
| 26 |
} |
| 27 |
|
| 28 |
void timer_monotonic_get(struct mono_time *mt) |
| 29 |
{ |
| 30 |
mono_time_set_usecs(mt, timer_raw_value() / TIMER_MHZ); |
| 31 |
} |
| 32 |
|
| 33 |
void init_timer(void) |
| 34 |
{ |
| 35 |
timer_prepare(); |
| 36 |
|
| 37 |
|
| 38 |
clrbits32(&mtk_gpt->gpt6_con, GPT6_CON_EN); |
| 39 |
setbits32(&mtk_gpt->gpt6_con, GPT6_CON_CLR); |
| 40 |
|
| 41 |
|
| 42 |
SET32_BITFIELDS(&GPT6_CLOCK_REG(mtk_gpt), |
| 43 |
GPT6_CLK_CLK6, GPT6_CLK_CLK6_SYS, |
| 44 |
GPT6_CLK_CLKDIV6, GPT6_CLK_CLKDIV_DIV1); |
| 45 |
|
| 46 |
SET32_BITFIELDS(&mtk_gpt->gpt6_con, |
| 47 |
GPT6_CON_MODE6, GPT6_MODE_FREERUN, |
| 48 |
GPT6_CON_EN6, GPT6_CON_EN); |
| 49 |
} |