2 * Cirrus Logic EP93xx timer support.
4 * Copyright (C) 2009, 2010
5 * Matthias Kaehlcke <matthias@kaehlcke.net>
7 * Copyright (C) 2004, 2005
8 * Cory T. Tusar, Videon Central, Inc., <ctusar@videon-central.com>
10 * Based on the original intr.c Cirrus Logic EP93xx Rev D. interrupt support,
13 * See file CREDITS for list of people who contributed to this project.
15 * This program is free software; you can redistribute it and/or modify
16 * it under the terms of the GNU General Public License as published by
17 * the Free Software Foundation; either version 2 of the License, or
18 * (at your option) any later version.
20 * This program is distributed in the hope that it will be useful, but
21 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
22 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
25 * You should have received a copy of the GNU General Public License along
26 * with this program; if not, write to the Free Software Foundation, Inc.,
27 * 675 Mass Ave, Cambridge, MA 02139, USA.
31 #include <linux/types.h>
32 #include <asm/arch/ep93xx.h>
36 #define TIMER_CLKSEL (1 << 3)
37 #define TIMER_ENABLE (1 << 7)
39 #define TIMER_FREQ 508469
40 #define TIMER_MAX_VAL 0xFFFFFFFF
42 static struct ep93xx_timer
44 unsigned long long ticks;
45 unsigned long last_update;
48 static inline unsigned long clk_to_systicks(unsigned long long clk_ticks)
50 unsigned long long sys_ticks = (clk_ticks * CONFIG_SYS_HZ);
51 do_div(sys_ticks, TIMER_FREQ);
53 return (unsigned long)sys_ticks;
56 static inline unsigned long usecs_to_ticks(unsigned long usecs)
65 ticks = usecs * TIMER_FREQ;
66 ticks /= (1000 * 1000);
72 static inline unsigned long read_timer(void)
74 struct timer_regs *timer = (struct timer_regs *)TIMER_BASE;
76 return TIMER_MAX_VAL - readl(&timer->timer3.value);
80 * timer without interrupts
82 unsigned long long get_ticks(void)
84 const unsigned long now = read_timer();
86 if (now >= timer.last_update)
87 timer.ticks += now - timer.last_update;
89 /* an overflow occurred */
90 timer.ticks += TIMER_MAX_VAL - timer.last_update + now;
92 timer.last_update = now;
97 unsigned long get_timer_masked(void)
99 return clk_to_systicks(get_ticks());
102 unsigned long get_timer(unsigned long base)
104 return get_timer_masked() - base;
107 void reset_timer_masked(void)
109 timer.last_update = read_timer();
113 void reset_timer(void)
115 reset_timer_masked();
118 void __udelay(unsigned long usec)
120 const unsigned long target = get_ticks() + usecs_to_ticks(usec);
122 while (get_ticks() < target)
128 struct timer_regs *timer = (struct timer_regs *)TIMER_BASE;
130 /* use timer 3 with 508KHz and free running */
131 writel(TIMER_CLKSEL, &timer->timer3.control);
133 /* set initial timer value 3 */
134 writel(TIMER_MAX_VAL, &timer->timer3.load);
136 /* Enable the timer */
137 writel(TIMER_ENABLE | TIMER_CLKSEL,
138 &timer->timer3.control);
140 reset_timer_masked();
146 * This function is derived from PowerPC code (timebase clock frequency).
147 * On ARM it returns the number of timer ticks per second.
149 unsigned long get_tbclk(void)
151 return CONFIG_SYS_HZ;