[OpenBIOS] [PATCH] PowerPC: Fix milliseconds prom call

Aurelien Jarno aurelien at aurel32.net
Wed Jan 14 15:46:35 CET 2009


The current implementation of the milliseconds prom call on PowerPC is
totally buggy:
- The timer frequency returned by get_timer_freq() does not correspond
  to the timer which is read.
- The dividend and the divisor of the division are swapped
- If called very often, this function is not precise
- Depending on the timer frequency and of the frequency of the calls,
  the variable overflow after a few dozen of seconds.

Please find in this patch a totally new implementation. This fixes the
problem observed with the quik when a timeout is defined.

The timer frequency is defined using #define, I wonder if there is a 
better place to put it.

It reads both low and high part of the timer to make sure there is no
overflow. It also removes the function that returns 0 on the first call
as this is not needed according to IEEE 1275-1994. Finally it computes
the real value of the timer each time, instead of adding a small value
to a variable at each function calls, in order to get a correct 
precision if this method is call very often.

diff --git a/arch/ppc/qemu/methods.c b/arch/ppc/qemu/methods.c
index ee34158..fa5aeb2 100644
--- a/arch/ppc/qemu/methods.c
+++ b/arch/ppc/qemu/methods.c
@@ -122,19 +122,28 @@ ciface_quiesce( ulong args[], ulong ret[] )
 }
 
 /* ( -- ms ) */
+#define TIMER_FREQUENCY 16600000ULL
+
 static void
 ciface_milliseconds( ulong args[], ulong ret[] )
 {
-        extern unsigned long get_timer_freq(void);
-	static ulong mticks=0, usecs=0;
-	ulong t;
-
-	asm volatile("mftb %0" : "=r" (t) : );
-	if( mticks )
-		usecs += get_timer_freq() / 1000000 * ( t-mticks );
-	mticks = t;
-
-	PUSH( usecs/1000 );
+	ulong tbu, tbl, temp;
+	ullong ticks, msecs;
+
+	asm volatile(
+		"1:\n"
+		"mftbu  %2\n"
+		"mftb   %0\n"
+		"mftbu  %1\n"
+		"cmpw   %2,%1\n"
+		"bne    1b\n"
+		: "=r"(tbl), "=r"(tbu), "=r"(temp)
+		:
+		: "cc");
+
+	ticks = (((ullong)tbu) << 32) | (ullong)tbl;
+	msecs = (1000 * ticks) / TIMER_FREQUENCY;
+	PUSH( msecs );
 }

-- 
  .''`.  Aurelien Jarno	            | GPG: 1024D/F1BCDB73
 : :' :  Debian developer           | Electrical Engineer
 `. `'   aurel32 at debian.org         | aurelien at aurel32.net
   `-    people.debian.org/~aurel32 | www.aurel32.net



More information about the OpenBIOS mailing list