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 / 256)
31 static ulong timestamp;
33 static struct systimer *systimer_base = (struct systimer *)SYSTIMER_BASE;
43 writel(0, &systimer_base->timer0control);
44 writel(SYSTIMER_RELOAD, &systimer_base->timer0load);
45 writel(SYSTIMER_RELOAD, &systimer_base->timer0value);
46 writel(SYSTIMER_EN | SYSTIMER_32BIT | SYSTIMER_PRESC_256,
47 &systimer_base->timer0control);
55 #define TICK_PER_TIME ((SYSTIMER_RATE + CONFIG_SYS_HZ / 2) / CONFIG_SYS_HZ)
56 #define NS_PER_TICK (1000000000 / SYSTIMER_RATE)
58 static inline unsigned long long tick_to_time(unsigned long long tick)
60 do_div(tick, TICK_PER_TIME);
64 static inline unsigned long long time_to_tick(unsigned long long time)
66 return time * TICK_PER_TIME;
69 static inline unsigned long long us_to_tick(unsigned long long us)
71 unsigned long long tick = us * 1000;
72 tick += NS_PER_TICK - 1;
73 do_div(tick, NS_PER_TICK);
77 unsigned long long get_ticks(void)
79 ulong now = ~readl(&systimer_base->timer0value);
81 if (now >= lastinc) /* normal mode (non roll) */
82 /* move stamp forward with absolut diff ticks */
83 timestamp += (now - lastinc);
84 else /* we have rollover of incrementer */
85 timestamp += (0xFFFFFFFF - lastinc) + now;
91 * Delay x useconds AND preserve advance timstamp value
92 * assumes timer is ticking at 1 msec
94 void __udelay(ulong usec)
96 unsigned long long tmp;
99 tmo = us_to_tick(usec);
100 tmp = get_ticks() + tmo; /* get current timestamp */
102 while (get_ticks() < tmp) /* loop till event */
106 ulong get_timer(ulong base)
108 return get_timer_masked() - base;
111 void reset_timer_masked(void)
113 lastinc = ~readl(&systimer_base->timer0value);
117 void reset_timer(void)
119 reset_timer_masked();
122 ulong get_timer_masked(void)
124 return tick_to_time(get_ticks());
127 ulong get_tbclk(void)
129 return SYSTIMER_RATE;