ep93xx timer: Fix get_ticks() and usecs_to_ticks()
PATCH 1/1 ep93xx timer: Fix get_ticks() and usecs_to_ticks()
ep93xx timer: Make get_ticks() consistent with get_tbclk(), returning a value with CONFIG_SYS_HZ resolution. This fix obsoletes the function clk_to_systicks(), leading to a simplification of the timer code. Further a variable in usecs_to_ticks() is promoted to a 64-bit value, to avoid overflows of intermediate values.
--- cpu/arm920t/ep93xx/timer.c | 83 +++++++++++++++++++------------------------ 1 files changed, 37 insertions(+), 46 deletions(-)
cpu/arm920t/ep93xx/timer.c +37 -46
@@ -1,8 +1,7 @@/* * Cirrus Logic EP93xx timer support. * -* Copyright (C) 2009, 2010 -* Matthias Kaehlcke <[email protected]> +* Copyright (C) 2009, 2010 Matthias Kaehlcke <[email protected]> * * Copyright (C) 2004, 2005 * Cory T. Tusar, Videon Central, Inc., <[email protected]>
@@ -36,67 +35,55 @@#define TIMER_CLKSEL (1 << 3)#define TIMER_ENABLE (1 << 7) -#define TIMER_FREQ 508469 +#define TIMER_FREQ 508469/* ticks / second */ #define TIMER_MAX_VAL 0xFFFFFFFFstatic struct ep93xx_timer {unsigned long long ticks; -unsigned long last_update; +unsigned long last_read; } timer; -static inline unsigned long clk_to_systicks (unsigned long long clk_ticks) +static inline unsigned long long usecs_to_ticks (unsigned long usecs) { -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 usecs_to_ticks (unsigned long usecs) -{ -unsigned long ticks; - -if (usecs >=1000 ) { - ticks = usecs /1000 ; - ticks *=TIMER_FREQ ; - ticks /=1000 ; - }else { - ticks = usecs *TIMER_FREQ ; - ticks /= (1000 *1000 ); - } +unsigned long long ticks = usecs *TIMER_FREQ ; +do_div (ticks,1000 *1000 );return ticks; } -static inline unsigned long read_timer (void ) +static inline void read_timer (void ) { -struct timer_regs *timer = (struct timer_regs *)TIMER_BASE ; +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; -return TIMER_MAX_VAL -readl (&timer->timer3.value); + timer.last_read = now; }/* -* timer without interrupts +* Get the number of ticks (in CONFIG_SYS_HZ resolution) */ unsigned long long get_ticks (void ) { -const unsigned long now =read_timer (); +unsigned long long sys_ticks; -if (now >= timer.last_update) - timer.ticks += now - timer.last_update; -else -/* an overflow occurred */ - timer.ticks +=TIMER_MAX_VAL - timer.last_update + now; +read_timer (); - timer.last_update = now; + sys_ticks = timer.ticks *CONFIG_SYS_HZ ; +do_div (sys_ticks,TIMER_FREQ ); -return timer.ticks; +return sys_ticks; }unsigned long get_timer_masked (void ) { -return clk_to_systicks (get_ticks ()); +return get_ticks (); }unsigned long get_timer (unsigned long base)
@@ -106,7 +93,7 @@ unsigned long get_timer(unsigned long base)void reset_timer_masked (void ) { - timer.last_update =read_timer (); +read_timer (); timer.ticks =0 ; }
@@ -117,25 +104,29 @@ void reset_timer(void)void __udelay (unsigned long usec) { -const unsigned long target =get_ticks () +usecs_to_ticks (usec); +unsigned long long target; + +read_timer (); + + target = timer.ticks +usecs_to_ticks (usec); -while (get_ticks () < target) -/* noop */ ; +while (timer.ticks < target) +read_timer (); }int timer_init (void ) { -struct timer_regs *timer = (struct timer_regs *)TIMER_BASE ; +struct timer_regs *timer_regs = (struct timer_regs *)TIMER_BASE ; -/* use timer 3 with 508KHz and free running */ -writel (TIMER_CLKSEL , &timer->timer3.control); +/* use timer 3 with 508KHz and free running, not enabled now */ +writel (TIMER_CLKSEL , &timer_regs->timer3.control); -/* set initial timer value 3 */ -writel (TIMER_MAX_VAL , &timer->timer3.load); +/* set initial timer value */ +writel (TIMER_MAX_VAL , &timer_regs->timer3.load);/* Enable the timer */ writel (TIMER_ENABLE |TIMER_CLKSEL , - &timer->timer3.control); + &timer_regs->timer3.control);reset_timer_masked ();
Signature · 1 line
1.6.5
PATCH 1/1 ep93xx timer: Fix get_ticks() and usecs_to_ticks()
Quoted 9 lines · Matthias Kaehlcke
ep93xx timer: Make
get_ticks()consistent withget_tbclk(), returning a value withCONFIG_SYS_HZresolution. This fix obsoletes the functionclk_to_systicks(), leading to a simplification of the timer code. Further a variable inusecs_to_ticks()is promoted to a 64-bit value, to avoid overflows of intermediate values.Signed-off-by Matthias Kaehlcke <[email protected]>Tested-by Alessandro Rubini <[email protected]>
Please rebase.
I applied the earlier version.
I think these changes should be a much smaller patch. Tom
PATCH 1/1 ep93xx timer: Fix get_ticks() and usecs_to_ticks()
Hi Tom,
El Sat, Feb 27, 2010 at 05:41:26PM -0600 Tom ha dit:
Quoted 7 lines
Quoted 6 lines · Matthias Kaehlcke
ep93xx timer: Make
get_ticks()consistent withget_tbclk(), returning a value withCONFIG_SYS_HZresolution. This fix obsoletes the functionclk_to_systicks(), leading to a simplification of the timer code. Further a variable inusecs_to_ticks()is promoted to a 64-bit value, to avoid overflows of intermediate values.
Quoted 5 lines
Quoted 3 lines
Signed-off-by Matthias Kaehlcke <[email protected]>Tested-by Alessandro Rubini <[email protected]>Please rebase.
I applied the earlier version.
it is based on the earlier version
Quoted 1 line
I think these changes should be a much smaller patch.
the patch without rebasing would have been shorter than the earlier one(s), that's why i proposed reverting the other one(s).
making get_ticks() work with CONFIG_SYS_HZ resolution implied moving code around functions and removing code from others, resulting in a larger patch (but in resume in more compact and more readable code).
best regards
Signature · 10 lines
Matthias Kaehlcke
Embedded Linux Developer
Barcelona
Comunicar bichos a <[email protected]>
(LANG=es_ES uname --help)
.''`.
using free software / Debian GNU/Linux | http://debian.org : :' :
`. `'`
gpg --keyserver pgp.mit.edu --recv-keys 47D8E5D4 `-