blob: 5feed3420bcb64a084b7de28ffe4326a4052def5 [file] [log] [blame]
Angel Ponse67ab182020-04-04 18:51:11 +02001/* SPDX-License-Identifier: GPL-2.0-only */
Tristan Shieh4c8d4872018-06-06 13:35:12 +08002
Kyösti Mälkki13f66502019-03-03 08:01:05 +02003#include <device/mmio.h>
Tristan Shieh4c8d4872018-06-06 13:35:12 +08004#include <timer.h>
5#include <delay.h>
6#include <thread.h>
7
8#include <soc/addressmap.h>
9#include <soc/timer.h>
10
11static struct mtk_gpt_regs *const mtk_gpt = (void *)GPT_BASE;
12
13__weak void timer_prepare(void) { /* do nothing */ }
14
Tristan Shieha76e6542019-04-08 11:01:40 +080015static uint64_t timer_raw_value(void)
16{
17 /*
18 * According to "General-Purpose Timer (GPT).pdf", The read operation of
19 * gpt6_cnt_l will make gpt6_cnt_h fixed until the next read operation
20 * of gpt6_cnt_l. Therefore, we must read gpt6_cnt_l before gpt6_cnt_h.
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
Tristan Shieh4c8d4872018-06-06 13:35:12 +080028void timer_monotonic_get(struct mono_time *mt)
29{
Tristan Shieha76e6542019-04-08 11:01:40 +080030 mono_time_set_usecs(mt, timer_raw_value() / GPT_MHZ);
Tristan Shieh4c8d4872018-06-06 13:35:12 +080031}
32
33void init_timer(void)
34{
35 timer_prepare();
36
Tristan Shieha76e6542019-04-08 11:01:40 +080037 /* Disable timer and clear the counter */
Yidi Lin03e002f2021-02-04 18:30:54 +080038 clrbits32(&mtk_gpt->gpt6_con, GPT6_CON_EN);
39 setbits32(&mtk_gpt->gpt6_con, GPT6_CON_CLR);
Tristan Shieh4c8d4872018-06-06 13:35:12 +080040
41 /* Set clock source to system clock and set clock divider to 1 */
Yidi Lin03e002f2021-02-04 18:30:54 +080042 SET32_BITFIELDS(&GPT6_CLOCK_REG(mtk_gpt),
43 GPT6_CLK_CLK6, GPT6_CLK_CLK6_SYS,
44 GPT6_CLK_CLKDIV6, GPT6_CLK_CLKDIV_DIV1);
Tristan Shieha76e6542019-04-08 11:01:40 +080045 /* Set operation mode to FREERUN mode and enable timer */
Yidi Lin03e002f2021-02-04 18:30:54 +080046 SET32_BITFIELDS(&mtk_gpt->gpt6_con,
47 GPT6_CON_MODE6, GPT6_MODE_FREERUN,
48 GPT6_CON_EN6, GPT6_CON_EN);
Tristan Shieh4c8d4872018-06-06 13:35:12 +080049}