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>
22 static struct tmu_regs *tmu = (struct tmu_regs *)TMU_BASE;
25 static unsigned long last_tcnt;
26 static unsigned long long overflow_ticks;
28 unsigned long get_tbclk(void)
30 return get_tmu0_clk_rate() >> ((bit + 1) * 2);
33 static inline unsigned long long tick_to_time(unsigned long long tick)
35 tick *= CONFIG_SYS_HZ;
36 do_div(tick, get_tbclk());
41 static inline unsigned long long usec_to_tick(unsigned long long usec)
44 do_div(usec, 1000000);
49 static void tmu_timer_start(unsigned int timer)
53 writeb(readb(&tmu->tstr) | (1 << timer), &tmu->tstr);
56 static void tmu_timer_stop(unsigned int timer)
60 writeb(readb(&tmu->tstr) & ~(1 << timer), &tmu->tstr);
65 bit = (ffs(CONFIG_SYS_TMU_CLK_DIV) >> 1) - 1;
66 writew((readw(&tmu->tcr0) & ~TCR_TPSC) | bit, &tmu->tcr0);
77 unsigned long long get_ticks(void)
79 unsigned long tcnt = 0 - readl(&tmu->tcnt0);
81 if (last_tcnt > tcnt) /* overflow */
85 return (overflow_ticks << 32) | tcnt;
88 void __udelay(unsigned long usec)
90 unsigned long long tmp;
93 tmo = usec_to_tick(usec);
94 tmp = get_ticks() + tmo; /* get current timestamp */
96 while (get_ticks() < tmp) /* loop till event */
100 unsigned long get_timer(unsigned long base)
103 return tick_to_time(get_ticks()) - base;
106 void set_timer(unsigned long t)
108 writel((0 - t), &tmu->tcnt0);
111 void reset_timer(void)