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 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 __delay(unsigned long loops)
22 {
23 __asm__ __volatile__ (
24 ".set\tnoreorder\n"
25 "1:\tbnez\t%0,1b\n\t"
26 "dsubu\t%0,1\n\t"
27 ".set\treorder"
28 :"=r" (loops)
29 :"0" (loops));
30 }
31
32 /*
33 * Division by multiplication: you don't have to worry about
34 * loss of precision.
35 *
36 * Use only for very small delays ( < 1 msec). Should probably use a
37 * lookup table, really, as the multiplications take much too long with
38 * short delays. This is a "reasonable" implementation, though (and the
39 * first constant multiplications gets optimized away if the delay is
40 * a constant)
41 */
__udelay(unsigned long usecs,unsigned long lpj)42 static inline void __udelay(unsigned long usecs, unsigned long lpj)
43 {
44 unsigned long lo;
45
46 /*
47 * The common rates of 1000 and 128 are rounded wrongly by the
48 * catchall case. Excessive precission? Probably ...
49 */
50 #if (HZ == 128)
51 usecs *= 0x0008637bd05af6c7UL; /* 2**64 / (1000000 / HZ) */
52 #elif (HZ == 1000)
53 usecs *= 0x004189374BC6A7f0UL; /* 2**64 / (1000000 / HZ) */
54 #else
55 usecs *= (0x8000000000000000UL / (500000 / HZ));
56 #endif
57 __asm__("dmultu\t%2,%3"
58 : "=h" (usecs), "=l" (lo)
59 : "r" (usecs), "r" (lpj)
60 : GCC_REG_ACCUM);
61 __delay(usecs);
62 }
63
__ndelay(unsigned long nsecs,unsigned long lpj)64 static inline void __ndelay(unsigned long nsecs, unsigned long lpj)
65 {
66 unsigned long lo;
67
68 /*
69 * The common rates of 1000 and 128 are rounded wrongly by the
70 * catchall case. Excessive precission? Probably ...
71 */
72 #if (HZ == 128)
73 nsecs *= 0x000001ad7f29abcbUL; /* 2**64 / (1000000000 / HZ) */
74 #elif (HZ == 1000)
75 nsecs *= 0x0010c6f7a0b5eeUL; /* 2**64 / (1000000000 / HZ) */
76 #else
77 nsecs *= (0x8000000000000000UL / (500000000 / HZ));
78 #endif
79 __asm__("dmultu\t%2,%3"
80 : "=h" (nsecs), "=l" (lo)
81 : "r" (nsecs), "r" (lpj)
82 : GCC_REG_ACCUM);
83 __delay(nsecs);
84 }
85
86 #ifdef CONFIG_SMP
87 #define __udelay_val cpu_data[smp_processor_id()].udelay_val
88 #else
89 #define __udelay_val loops_per_jiffy
90 #endif
91
92 #define udelay(usecs) __udelay((usecs),__udelay_val)
93 #define ndelay(nsecs) __ndelay((nsecs),__udelay_val)
94
95 #endif /* _ASM_DELAY_H */
96