3 * Jean-Christophe PLAGNIOL-VILLARD <plagnioj@jcrosoft.com>
5 * (C) Copyright 2007-2012
6 * Nobobuhiro Iwamatsu <iwamatsu@nigauri.org>
9 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
11 * SPDX-License-Identifier: GPL-2.0+
16 #include <asm/processor.h>
20 static struct tmu_regs *tmu = (struct tmu_regs *)TMU_BASE;
23 static unsigned long last_tcnt;
24 static unsigned long long overflow_ticks;
26 unsigned long get_tbclk(void)
28 return get_tmu0_clk_rate() >> ((bit + 1) * 2);
31 static inline unsigned long long tick_to_time(unsigned long long tick)
33 tick *= CONFIG_SYS_HZ;
34 do_div(tick, get_tbclk());
39 static inline unsigned long long usec_to_tick(unsigned long long usec)
42 do_div(usec, 1000000);
47 static void tmu_timer_start(unsigned int timer)
51 writeb(readb(&tmu->tstr) | (1 << timer), &tmu->tstr);
54 static void tmu_timer_stop(unsigned int timer)
58 writeb(readb(&tmu->tstr) & ~(1 << timer), &tmu->tstr);
63 bit = (ffs(CONFIG_SYS_TMU_CLK_DIV) >> 1) - 1;
64 writew(readw(&tmu->tcr0) | bit, &tmu->tcr0);
75 unsigned long long get_ticks(void)
77 unsigned long tcnt = 0 - readl(&tmu->tcnt0);
79 if (last_tcnt > tcnt) /* overflow */
83 return (overflow_ticks << 32) | tcnt;
86 void __udelay(unsigned long usec)
88 unsigned long long tmp;
91 tmo = usec_to_tick(usec);
92 tmp = get_ticks() + tmo; /* get current timestamp */
94 while (get_ticks() < tmp) /* loop till event */
98 unsigned long get_timer(unsigned long base)
101 return tick_to_time(get_ticks()) - base;
104 void set_timer(unsigned long t)
106 writel((0 - t), &tmu->tcnt0);
109 void reset_timer(void)