vserver 1.9.5.x5
[linux-2.6.git] / include / asm-mips / delay.h
1 /*
2  * This file is subject to the terms and conditions of the GNU General Public
3  * License.  See the file "COPYING" in the main directory of this archive
4  * for more details.
5  *
6  * Copyright (C) 1994 by Waldorf Electronics
7  * Copyright (C) 1995 - 2000, 01, 03 by Ralf Baechle
8  * Copyright (C) 1999, 2000 Silicon Graphics, Inc.
9  */
10 #ifndef _ASM_DELAY_H
11 #define _ASM_DELAY_H
12
13 #include <linux/config.h>
14 #include <linux/param.h>
15
16 #include <asm/compiler.h>
17
18 extern unsigned long loops_per_jiffy;
19
20 static inline void __delay(unsigned long loops)
21 {
22         if (sizeof(long) == 4)
23                 __asm__ __volatile__ (
24                 ".set\tnoreorder\n"
25                 "1:\tbnez\t%0,1b\n\t"
26                 "subu\t%0,1\n\t"
27                 ".set\treorder"
28                 : "=r" (loops)
29                 : "0" (loops));
30         else if (sizeof(long) == 8)
31                 __asm__ __volatile__ (
32                 ".set\tnoreorder\n"
33                 "1:\tbnez\t%0,1b\n\t"
34                 "dsubu\t%0,1\n\t"
35                 ".set\treorder"
36                 :"=r" (loops)
37                 :"0" (loops));
38 }
39
40
41 /*
42  * Division by multiplication: you don't have to worry about
43  * loss of precision.
44  *
45  * Use only for very small delays ( < 1 msec).  Should probably use a
46  * lookup table, really, as the multiplications take much too long with
47  * short delays.  This is a "reasonable" implementation, though (and the
48  * first constant multiplications gets optimized away if the delay is
49  * a constant)
50  */
51
52 static inline void __udelay(unsigned long usecs, unsigned long lpj)
53 {
54         unsigned long lo;
55
56         /*
57          * The common rates of 1000 and 128 are rounded wrongly by the
58          * catchall case for 64-bit.  Excessive precission?  Probably ...
59          */
60 #if defined(CONFIG_MIPS64) && (HZ == 128)
61         usecs *= 0x0008637bd05af6c7UL;          /* 2**64 / (1000000 / HZ) */
62 #elif defined(CONFIG_MIPS64) && (HZ == 1000)
63         usecs *= 0x004189374BC6A7f0UL;          /* 2**64 / (1000000 / HZ) */
64 #elif defined(CONFIG_MIPS64)
65         usecs *= (0x8000000000000000UL / (500000 / HZ));
66 #else /* 32-bit junk follows here */
67         usecs *= (unsigned long) (((0x8000000000000000ULL / (500000 / HZ)) +
68                                    0x80000000ULL) >> 32);
69 #endif
70
71         if (sizeof(long) == 4)
72                 __asm__("multu\t%2, %3"
73                 : "=h" (usecs), "=l" (lo)
74                 : "r" (usecs), "r" (lpj)
75                 : GCC_REG_ACCUM);
76         else if (sizeof(long) == 8)
77                 __asm__("dmultu\t%2, %3"
78                 : "=h" (usecs), "=l" (lo)
79                 : "r" (usecs), "r" (lpj)
80                 : GCC_REG_ACCUM);
81
82         __delay(usecs);
83 }
84
85 #ifdef CONFIG_SMP
86 #define __udelay_val cpu_data[smp_processor_id()].udelay_val
87 #else
88 #define __udelay_val loops_per_jiffy
89 #endif
90
91 #define udelay(usecs) __udelay((usecs),__udelay_val)
92
93 #endif /* _ASM_DELAY_H */