MOXA linux-2.6.x / linux-2.6.19-uc1 from UC-7110-LX-BOOTLOADER-1.9_VERSION-4.2.tgz
[linux-2.6.19-moxart.git] / include / asm-nios2nommu / delay.h
blobda0a18414a30487c4a007c23ba922625cc3430f6
1 #ifndef _NIOS_DELAY_H
2 #define _NIOS_DELAY_H
4 /*--------------------------------------------------------------------
6 * include/asm-nios2nommu/delay.h
8 * Derived from various works, Alpha, ix86, M68K, Sparc, ...et al
10 * Copyright (C) 2004 Microtronix Datacom Ltd
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
23 * Jan/20/2004 dgt NiosII
25 ---------------------------------------------------------------------*/
28 #include <asm/param.h>
30 extern __inline__ void __delay(unsigned long loops)
32 int dummy;
34 __asm__ __volatile__(
35 "1: \n\t"
36 " beq %0,zero,2f\n\t"
37 " addi %0, %0, -1\n\t"
38 " br 1b\n\t"
39 "2: \n\t"
41 : "=r" (dummy) /* Need output for optimizer */
43 : "0" (loops) /* %0 Input */
48 * Note that 19 * 226 == 4294 ==~ 2^32 / 10^6, so
49 * loops = (4294 * usecs * loops_per_jiffy * HZ) / 2^32.
51 * The mul instruction gives us loops = (a * b) / 2^32.
52 * We choose a = usecs * 19 * HZ and b = loops_per_jiffy * 226
53 * because this lets us support a wide range of HZ and
54 * loops_per_jiffy values without either a or b overflowing 2^32.
55 * Thus we need usecs * HZ <= (2^32 - 1) / 19 = 226050910 and
56 * loops_per_jiffy <= (2^32 - 1) / 226 = 19004280
57 * (which corresponds to ~3800 bogomips at HZ = 100).
58 * -- paulus
60 #define __MAX_UDELAY (226050910UL/HZ) /* maximum udelay argument */
61 #define __MAX_NDELAY (4294967295UL/HZ) /* maximum ndelay argument */
63 extern unsigned long loops_per_jiffy;
65 extern __inline__ void __udelay(unsigned int x)
67 unsigned int loops;
69 __asm__("mulxuu %0,%1,%2" : "=r" (loops) :
70 "r" (x), "r" (loops_per_jiffy * 226));
71 __delay(loops);
74 extern __inline__ void __ndelay(unsigned int x)
76 unsigned int loops;
78 __asm__("mulxuu %0,%1,%2" : "=r" (loops) :
79 "r" (x), "r" (loops_per_jiffy * 5));
80 __delay(loops);
83 extern void __bad_udelay(void); /* deliberately undefined */
84 extern void __bad_ndelay(void); /* deliberately undefined */
86 #define udelay(n) (__builtin_constant_p(n)? \
87 ((n) > __MAX_UDELAY? __bad_udelay(): __udelay((n) * (19 * HZ))) : \
88 __udelay((n) * (19 * HZ)))
90 #define ndelay(n) (__builtin_constant_p(n)? \
91 ((n) > __MAX_NDELAY? __bad_ndelay(): __ndelay((n) * HZ)) : \
92 __ndelay((n) * HZ))
94 #define muldiv(a, b, c) (((a)*(b))/(c))
96 #endif /* defined(_NIOS_DELAY_H) */