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
)
37 " addi %0, %0, -1\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).
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
)
69 __asm__("mulxuu %0,%1,%2" : "=r" (loops
) :
70 "r" (x
), "r" (loops_per_jiffy
* 226));
74 extern __inline__
void __ndelay(unsigned int x
)
78 __asm__("mulxuu %0,%1,%2" : "=r" (loops
) :
79 "r" (x
), "r" (loops_per_jiffy
* 5));
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)) : \
94 #define muldiv(a, b, c) (((a)*(b))/(c))
96 #endif /* defined(_NIOS_DELAY_H) */