2 * Copyright 2010-2011 Calxeda, Inc.
4 * Based on arm926ejs/mx27/timer.c
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the Free
8 * Software Foundation; either version 2 of the License, or (at your option)
11 * This program is distributed in the hope it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
16 * You should have received a copy of the GNU General Public License along with
17 * this program. If not, see <http://www.gnu.org/licenses/>.
22 #include <linux/types.h> /* for size_t */
23 #include <linux/stddef.h> /* for NULL */
25 #include <asm/arch-armv7/systimer.h>
28 #define SYSTIMER_BASE 0xFFF34000 /* Timer 0 and 1 base */
29 #define SYSTIMER_RATE 150000000
31 static ulong timestamp;
33 static struct systimer *systimer_base = (struct systimer *)SYSTIMER_BASE;
43 writel(SYSTIMER_RELOAD, &systimer_base->timer0load);
44 writel(SYSTIMER_RELOAD, &systimer_base->timer0value);
45 writel(SYSTIMER_EN | SYSTIMER_32BIT, &systimer_base->timer0control);
53 #define TICK_PER_TIME ((SYSTIMER_RATE + CONFIG_SYS_HZ / 2) / CONFIG_SYS_HZ)
54 #define NS_PER_TICK (1000000000 / SYSTIMER_RATE)
56 static inline unsigned long long tick_to_time(unsigned long long tick)
58 do_div(tick, TICK_PER_TIME);
62 static inline unsigned long long time_to_tick(unsigned long long time)
64 return time * TICK_PER_TIME;
67 static inline unsigned long long us_to_tick(unsigned long long us)
69 unsigned long long tick = us << 16;
70 tick += NS_PER_TICK - 1;
71 do_div(tick, NS_PER_TICK);
75 unsigned long long get_ticks(void)
77 ulong now = ~readl(&systimer_base->timer0value);
79 if (now >= lastinc) /* normal mode (non roll) */
80 /* move stamp forward with absolut diff ticks */
81 timestamp += (now - lastinc);
82 else /* we have rollover of incrementer */
83 timestamp += (0xFFFFFFFF - lastinc) + now;
89 * Delay x useconds AND preserve advance timstamp value
90 * assumes timer is ticking at 1 msec
92 void __udelay(ulong usec)
94 unsigned long long tmp;
97 tmo = us_to_tick(usec);
98 tmp = get_ticks() + tmo; /* get current timestamp */
100 while (get_ticks() < tmp) /* loop till event */
104 ulong get_timer(ulong base)
106 return get_timer_masked() - base;
109 void reset_timer_masked(void)
111 lastinc = ~readl(&systimer_base->timer0value);
115 void reset_timer(void)
117 reset_timer_masked();
120 ulong get_timer_masked(void)
122 return tick_to_time(get_ticks());