summaryrefslogtreecommitdiffstats
path: root/cpu/arm920t/ep93xx/timer.c
diff options
context:
space:
mode:
Diffstat (limited to 'cpu/arm920t/ep93xx/timer.c')
-rw-r--r--cpu/arm920t/ep93xx/timer.c62
1 files changed, 33 insertions, 29 deletions
diff --git a/cpu/arm920t/ep93xx/timer.c b/cpu/arm920t/ep93xx/timer.c
index 4a0ce4da64..31304b7f97 100644
--- a/cpu/arm920t/ep93xx/timer.c
+++ b/cpu/arm920t/ep93xx/timer.c
@@ -1,7 +1,8 @@
/*
* Cirrus Logic EP93xx timer support.
*
- * Copyright (C) 2009, 2010 Matthias Kaehlcke <matthias@kaehlcke.net>
+ * Copyright (C) 2009, 2010
+ * Matthias Kaehlcke <matthias@kaehlcke.net>
*
* Copyright (C) 2004, 2005
* Cory T. Tusar, Videon Central, Inc., <ctusar@videon-central.com>
@@ -41,9 +42,17 @@
static struct ep93xx_timer
{
unsigned long long ticks;
- unsigned long last_read;
+ unsigned long last_update;
} timer;
+static inline unsigned long clk_to_systicks(unsigned long long clk_ticks)
+{
+ unsigned long long sys_ticks = (clk_ticks * CONFIG_SYS_HZ);
+ do_div(sys_ticks, TIMER_FREQ);
+
+ return (unsigned long)sys_ticks;
+}
+
static inline unsigned long long usecs_to_ticks(unsigned long usecs)
{
unsigned long long ticks = (unsigned long long)usecs * TIMER_FREQ;
@@ -52,18 +61,11 @@ static inline unsigned long long usecs_to_ticks(unsigned long usecs)
return ticks;
}
-static inline void read_timer(void)
+static inline unsigned long read_timer(void)
{
- struct timer_regs *timer_regs = (struct timer_regs *)TIMER_BASE;
- const unsigned long now = TIMER_MAX_VAL - readl(&timer_regs->timer3.value);
-
- if (now >= timer.last_read)
- timer.ticks += now - timer.last_read;
- else
- /* an overflow occurred */
- timer.ticks += TIMER_MAX_VAL - timer.last_read + now;
+ struct timer_regs *timer = (struct timer_regs *)TIMER_BASE;
- timer.last_read = now;
+ return TIMER_MAX_VAL - readl(&timer->timer3.value);
}
/*
@@ -71,14 +73,17 @@ static inline void read_timer(void)
*/
unsigned long long get_ticks(void)
{
- unsigned long long sys_ticks;
+ const unsigned long now = read_timer();
- read_timer();
+ if (now >= timer.last_update)
+ timer.ticks += now - timer.last_update;
+ else
+ /* an overflow occurred */
+ timer.ticks += TIMER_MAX_VAL - timer.last_update + now;
- sys_ticks = timer.ticks * CONFIG_SYS_HZ;
- do_div(sys_ticks, TIMER_FREQ);
+ timer.last_update = now;
- return sys_ticks;
+ return clk_to_systicks(timer.ticks);
}
unsigned long get_timer_masked(void)
@@ -93,7 +98,7 @@ unsigned long get_timer(unsigned long base)
void reset_timer_masked(void)
{
- read_timer();
+ timer.last_update = read_timer();
timer.ticks = 0;
}
@@ -104,29 +109,28 @@ void reset_timer(void)
void __udelay(unsigned long usec)
{
- unsigned long long target;
-
- read_timer();
+ /* read the timer and update timer.ticks */
+ get_ticks();
- target = timer.ticks + usecs_to_ticks(usec);
+ const unsigned long long target = timer.ticks + usecs_to_ticks(usec);
while (timer.ticks < target)
- read_timer();
+ get_ticks();
}
int timer_init(void)
{
- struct timer_regs *timer_regs = (struct timer_regs *)TIMER_BASE;
+ struct timer_regs *timer = (struct timer_regs *)TIMER_BASE;
- /* use timer 3 with 508KHz and free running, not enabled now */
- writel(TIMER_CLKSEL, &timer_regs->timer3.control);
+ /* use timer 3 with 508KHz and free running */
+ writel(TIMER_CLKSEL, &timer->timer3.control);
- /* set initial timer value */
- writel(TIMER_MAX_VAL, &timer_regs->timer3.load);
+ /* set initial timer value 3 */
+ writel(TIMER_MAX_VAL, &timer->timer3.load);
/* Enable the timer */
writel(TIMER_ENABLE | TIMER_CLKSEL,
- &timer_regs->timer3.control);
+ &timer->timer3.control);
reset_timer_masked();
OpenPOWER on IntegriCloud