2 * TNETV107X: Timer implementation
4 * SPDX-License-Identifier: GPL-2.0+
9 #include <asm/arch/clock.h>
23 #define regs ((struct timer_regs *)CONFIG_SYS_TIMERBASE)
25 #define TIMER_LOAD_VAL (CONFIG_SYS_HZ_CLOCK / CONFIG_SYS_HZ)
26 #define TIM_CLK_DIV 16
28 static ulong timestamp;
33 clk_enable(TNETV107X_LPSC_TIMER0);
35 lastinc = timestamp = 0;
37 /* We are using timer34 in unchained 32-bit mode, full speed */
38 __raw_writel(0x0, ®s->tcr);
39 __raw_writel(0x0, ®s->tgcr);
40 __raw_writel(0x06 | ((TIM_CLK_DIV - 1) << 8), ®s->tgcr);
41 __raw_writel(0x0, ®s->tim34);
42 __raw_writel(TIMER_LOAD_VAL, ®s->prd34);
43 __raw_writel(2 << 22, ®s->tcr);
48 static ulong get_timer_raw(void)
50 ulong now = __raw_readl(®s->tim34);
53 timestamp += now - lastinc;
55 timestamp += now + TIMER_LOAD_VAL - lastinc;
62 ulong get_timer(ulong base)
64 return (get_timer_raw() / (TIMER_LOAD_VAL / TIM_CLK_DIV)) - base;
67 unsigned long long get_ticks(void)
72 void __udelay(unsigned long usec)
78 tmo = CONFIG_SYS_HZ_CLOCK / 1000;
80 tmo /= (1000 * TIM_CLK_DIV);
82 endtime = get_timer_raw() + tmo;
85 ulong now = get_timer_raw();