openrisc: delay: fix handling of counter overflow
authorWill Deacon <will.deacon@arm.com>
Tue, 7 Aug 2012 16:59:54 +0000 (17:59 +0100)
committerJonas Bonn <jonas@southpole.se>
Sat, 1 Sep 2012 14:36:14 +0000 (16:36 +0200)
If the counter overflows during a __delay operation, we will exit the
loop prematurely. For example, calling __delay(0x100) with the counter
at 0xffffff00 gives us a target of 0x0. The unsigned comparison in the
while loop will likely be false on the first iteration (if the counter
is now anything other than 0) and we will return early.

This patch fixes the problem by comparing deltas in the loop rather than
absolute values.

Cc: Jon Masters <jcm@redhat.com>
Signed-off-by: Will Deacon <will.deacon@arm.com>
Signed-off-by: Jonas Bonn <jonas@southpole.se>
arch/openrisc/lib/delay.c

index 0c12407d3d5c8fd91508cb9c0646645a459b0b56..c330767c921747ac089339bcf565c5151160fe7e 100644 (file)
@@ -30,9 +30,9 @@ int __devinit read_current_timer(unsigned long *timer_value)
 
 void __delay(unsigned long cycles)
 {
-       cycles_t target = get_cycles() + cycles;
+       cycles_t start = get_cycles();
 
-       while (get_cycles() < target)
+       while ((get_cycles() - start) < cycles)
                cpu_relax();
 }
 EXPORT_SYMBOL(__delay);