2 * Copyright 2010-2011 Calxeda, Inc.
4 * Based on arm926ejs/mx27/timer.c
6 * SPDX-License-Identifier: GPL-2.0+
11 #include <linux/types.h> /* for size_t */
12 #include <linux/stddef.h> /* for NULL */
14 #include <asm/arch-armv7/systimer.h>
17 #define SYSTIMER_BASE 0xFFF34000 /* Timer 0 and 1 base */
18 #define SYSTIMER_RATE 150000000
20 static ulong timestamp;
22 static struct systimer *systimer_base = (struct systimer *)SYSTIMER_BASE;
32 writel(SYSTIMER_RELOAD, &systimer_base->timer0load);
33 writel(SYSTIMER_RELOAD, &systimer_base->timer0value);
34 writel(SYSTIMER_EN | SYSTIMER_32BIT, &systimer_base->timer0control);
42 #define TICK_PER_TIME ((SYSTIMER_RATE + CONFIG_SYS_HZ / 2) / CONFIG_SYS_HZ)
43 #define NS_PER_TICK (1000000000 / SYSTIMER_RATE)
45 static inline unsigned long long tick_to_time(unsigned long long tick)
47 do_div(tick, TICK_PER_TIME);
51 static inline unsigned long long time_to_tick(unsigned long long time)
53 return time * TICK_PER_TIME;
56 static inline unsigned long long us_to_tick(unsigned long long us)
58 unsigned long long tick = us * 1000;
59 tick += NS_PER_TICK - 1;
60 do_div(tick, NS_PER_TICK);
64 unsigned long long get_ticks(void)
66 ulong now = ~readl(&systimer_base->timer0value);
68 if (now >= lastinc) /* normal mode (non roll) */
69 /* move stamp forward with absolut diff ticks */
70 timestamp += (now - lastinc);
71 else /* we have rollover of incrementer */
72 timestamp += (0xFFFFFFFF - lastinc) + now;
78 * Delay x useconds AND preserve advance timstamp value
79 * assumes timer is ticking at 1 msec
81 void __udelay(ulong usec)
83 unsigned long long tmp;
86 tmo = us_to_tick(usec);
87 tmp = get_ticks() + tmo; /* get current timestamp */
89 while (get_ticks() < tmp) /* loop till event */
93 ulong get_timer(ulong base)
95 return get_timer_masked() - base;
98 void reset_timer_masked(void)
100 lastinc = ~readl(&systimer_base->timer0value);
104 void reset_timer(void)
106 reset_timer_masked();
109 ulong get_timer_masked(void)
111 return tick_to_time(get_ticks());
114 ulong get_tbclk(void)
116 return CONFIG_SYS_HZ;