提交 c7ad13a2 编写于 作者: M Matthias Kaehlcke 提交者: Tom Rix

ep93xx: Refactoring of timer code

ep93xx: Refactoring of the timer code, including the following changes

 * use a free running timer instead of a periodical one
 * use unsigned long long for total number of ticks
 * hold the timer state in a structure instead of separate variables
 * increment the timer counter instead of decrementing it
 * remove unused function udelay_masked()
 * remove unused function set_timer()
Signed-off-by: NMatthias Kaehlcke <matthias@kaehlcke.net>
上级 d9f505e3
...@@ -34,16 +34,18 @@ ...@@ -34,16 +34,18 @@
#include <div64.h> #include <div64.h>
#define TIMER_CLKSEL (1 << 3) #define TIMER_CLKSEL (1 << 3)
#define TIMER_MODE (1 << 6)
#define TIMER_ENABLE (1 << 7) #define TIMER_ENABLE (1 << 7)
#define TIMER_FREQ 508469 #define TIMER_FREQ 508469
#define TIMER_LOAD_VAL (TIMER_FREQ / CONFIG_SYS_HZ) #define TIMER_MAX_VAL 0xFFFFFFFF
static ulong timestamp; static struct ep93xx_timer
static ulong lastdec; {
unsigned long long ticks;
unsigned long last_update;
} timer;
static inline unsigned long clk_to_systicks(unsigned long clk_ticks) static inline unsigned long clk_to_systicks(unsigned long long clk_ticks)
{ {
unsigned long long sys_ticks = (clk_ticks * CONFIG_SYS_HZ); unsigned long long sys_ticks = (clk_ticks * CONFIG_SYS_HZ);
do_div(sys_ticks, TIMER_FREQ); do_div(sys_ticks, TIMER_FREQ);
...@@ -57,10 +59,10 @@ static inline unsigned long usecs_to_ticks(unsigned long usecs) ...@@ -57,10 +59,10 @@ static inline unsigned long usecs_to_ticks(unsigned long usecs)
if (usecs >= 1000) { if (usecs >= 1000) {
ticks = usecs / 1000; ticks = usecs / 1000;
ticks *= (TIMER_LOAD_VAL * CONFIG_SYS_HZ); ticks *= TIMER_FREQ;
ticks /= 1000; ticks /= 1000;
} else { } else {
ticks = usecs * TIMER_LOAD_VAL * CONFIG_SYS_HZ; ticks = usecs * TIMER_FREQ;
ticks /= (1000 * 1000); ticks /= (1000 * 1000);
} }
...@@ -71,7 +73,7 @@ static inline unsigned long read_timer(void) ...@@ -71,7 +73,7 @@ static inline unsigned long read_timer(void)
{ {
struct timer_regs *timer = (struct timer_regs *)TIMER_BASE; struct timer_regs *timer = (struct timer_regs *)TIMER_BASE;
return readl(&timer->timer3.value); return TIMER_MAX_VAL - readl(&timer->timer3.value);
} }
/* /*
...@@ -81,17 +83,15 @@ unsigned long long get_ticks(void) ...@@ -81,17 +83,15 @@ unsigned long long get_ticks(void)
{ {
const unsigned long now = read_timer(); const unsigned long now = read_timer();
if (lastdec >= now) { if (now >= timer.last_update)
/* normal mode */ timer.ticks += now - timer.last_update;
timestamp += lastdec - now; else
} else { /* an overflow occurred */
/* we have an overflow ... */ timer.ticks += TIMER_MAX_VAL - timer.last_update + now;
timestamp += lastdec + TIMER_LOAD_VAL - now;
}
lastdec = now; timer.last_update = now;
return timestamp; return timer.ticks;
} }
unsigned long get_timer_masked(void) unsigned long get_timer_masked(void)
...@@ -106,8 +106,8 @@ unsigned long get_timer(unsigned long base) ...@@ -106,8 +106,8 @@ unsigned long get_timer(unsigned long base)
void reset_timer_masked(void) void reset_timer_masked(void)
{ {
lastdec = read_timer(); timer.last_update = read_timer();
timestamp = 0; timer.ticks = 0;
} }
void reset_timer(void) void reset_timer(void)
...@@ -115,28 +115,11 @@ void reset_timer(void) ...@@ -115,28 +115,11 @@ void reset_timer(void)
reset_timer_masked(); reset_timer_masked();
} }
void set_timer(unsigned long t)
{
timestamp = t;
}
void __udelay(unsigned long usec) void __udelay(unsigned long usec)
{ {
const unsigned long ticks = usecs_to_ticks(usec); const unsigned long target = get_ticks() + usecs_to_ticks(usec);
const unsigned long target = clk_to_systicks(ticks) + get_timer(0);
while (get_timer_masked() < target)
/* noop */;
}
void udelay_masked(unsigned long usec)
{
const unsigned long ticks = usecs_to_ticks(usec);
const unsigned long target = clk_to_systicks(ticks) + get_timer(0);
reset_timer_masked();
while (get_timer_masked() < target) while (get_ticks() < target)
/* noop */; /* noop */;
} }
...@@ -147,12 +130,11 @@ int timer_init(void) ...@@ -147,12 +130,11 @@ int timer_init(void)
/* use timer 3 with 508KHz and free running */ /* use timer 3 with 508KHz and free running */
writel(TIMER_CLKSEL, &timer->timer3.control); writel(TIMER_CLKSEL, &timer->timer3.control);
/* auto load, manual update of Timer 3 */ /* set initial timer value 3 */
lastdec = TIMER_LOAD_VAL; writel(TIMER_MAX_VAL, &timer->timer3.load);
writel(TIMER_LOAD_VAL, &timer->timer3.load);
/* Enable the timer and periodic mode */ /* Enable the timer */
writel(TIMER_ENABLE | TIMER_MODE | TIMER_CLKSEL, writel(TIMER_ENABLE | TIMER_CLKSEL,
&timer->timer3.control); &timer->timer3.control);
reset_timer_masked(); reset_timer_masked();
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册