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 * See file CREDITS for list of people who contributed to this
14 * This program is free software; you can redistribute it and/or
15 * modify it under the terms of the GNU General Public License as
16 * published by the Free Software Foundation; either version 2 of
17 * the License, or (at your option) any later version.
19 * This program is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
24 * You should have received a copy of the GNU General Public License
25 * along with this program; if not, write to the Free Software
26 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
32 #include <asm/processor.h>
37 static struct tmu_regs *tmu = (struct tmu_regs *)TMU_BASE;
40 static unsigned long last_tcnt;
41 static unsigned long long overflow_ticks;
43 unsigned long get_tbclk(void)
45 return get_tmu0_clk_rate() >> ((bit + 1) * 2);
48 static inline unsigned long long tick_to_time(unsigned long long tick)
50 tick *= CONFIG_SYS_HZ;
51 do_div(tick, get_tbclk());
56 static inline unsigned long long usec_to_tick(unsigned long long usec)
59 do_div(usec, 1000000);
64 static void tmu_timer_start(unsigned int timer)
68 writeb(readb(&tmu->tstr) | (1 << timer), &tmu->tstr);
71 static void tmu_timer_stop(unsigned int timer)
75 writeb(readb(&tmu->tstr) & ~(1 << timer), &tmu->tstr);
80 bit = (ffs(CONFIG_SYS_TMU_CLK_DIV) >> 1) - 1;
81 writew(readw(&tmu->tcr0) | bit, &tmu->tcr0);
92 unsigned long long get_ticks(void)
94 unsigned long tcnt = 0 - readl(&tmu->tcnt0);
96 if (last_tcnt > tcnt) /* overflow */
100 return (overflow_ticks << 32) | tcnt;
103 void __udelay(unsigned long usec)
105 unsigned long long tmp;
108 tmo = usec_to_tick(usec);
109 tmp = get_ticks() + tmo; /* get current timestamp */
111 while (get_ticks() < tmp) /* loop till event */
115 unsigned long get_timer(unsigned long base)
118 return tick_to_time(get_ticks()) - base;
121 void set_timer(unsigned long t)
123 writel((0 - t), &tmu->tcnt0);
126 void reset_timer(void)