WIP: irq handler
[ppcskel.git] / time.c
1 /*
2         BootMii - a Free Software replacement for the Nintendo/BroadOn bootloader.
3         Requires mini.
4
5 Copyright (C) 2008              Segher Boessenkool <segher@kernel.crashing.org>
6
7 # This code is licensed to you under the terms of the GNU GPL, version 2;
8 # see file COPYING or http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt
9 */
10
11 #include "bootmii_ppc.h"
12
13 // Timebase frequency is bus frequency / 4.  Ignore roundoff, this
14 // doesn't have to be very accurate.
15 #define TICKS_PER_USEC (243/4)
16
17 u64 mftb(void)
18 {
19   u32 hi, lo, dum;
20   
21   asm("0: mftbu %0 ; mftb %1 ; mftbu %2 ; cmplw %0,%2 ; bne 0b" 
22       : "=r"(hi), "=r"(lo), "=r"(dum)); 
23   return ((u64)hi << 32) | lo;
24 }
25
26 static void __delay(u64 ticks)
27 {
28         u64 start = mftb();
29
30         while (mftb() - start < ticks)
31                 ;
32 }
33
34 void udelay(u32 us)
35 {
36         __delay(TICKS_PER_USEC * (u64)us);
37 }
38