xref: /linux/arch/arm/include/asm/delay.h (revision e5c86679d5e864947a52fb31e45a425dea3e7fa9)
1 /*
2  * Copyright (C) 1995-2004 Russell King
3  *
4  * Delay routines, using a pre-computed "loops_per_second" value.
5  */
6 #ifndef __ASM_ARM_DELAY_H
7 #define __ASM_ARM_DELAY_H
8 
9 #include <asm/memory.h>
10 #include <asm/param.h>	/* HZ */
11 
12 /*
13  * Loop (or tick) based delay:
14  *
15  * loops = loops_per_jiffy * jiffies_per_sec * delay_us / us_per_sec
16  *
17  * where:
18  *
19  * jiffies_per_sec = HZ
20  * us_per_sec = 1000000
21  *
22  * Therefore the constant part is HZ / 1000000 which is a small
23  * fractional number. To make this usable with integer math, we
24  * scale up this constant by 2^31, perform the actual multiplication,
25  * and scale the result back down by 2^31 with a simple shift:
26  *
27  * loops = (loops_per_jiffy * delay_us * UDELAY_MULT) >> 31
28  *
29  * where:
30  *
31  * UDELAY_MULT = 2^31 * HZ / 1000000
32  *             = (2^31 / 1000000) * HZ
33  *             = 2147.483648 * HZ
34  *             = 2147 * HZ + 483648 * HZ / 1000000
35  *
36  * 31 is the biggest scale shift value that won't overflow 32 bits for
37  * delay_us * UDELAY_MULT assuming HZ <= 1000 and delay_us <= 2000.
38  */
39 #define MAX_UDELAY_MS	2
40 #define UDELAY_MULT	UL(2147 * HZ + 483648 * HZ / 1000000)
41 #define UDELAY_SHIFT	31
42 
43 #ifndef __ASSEMBLY__
44 
45 struct delay_timer {
46 	unsigned long (*read_current_timer)(void);
47 	unsigned long freq;
48 };
49 
50 extern struct arm_delay_ops {
51 	void (*delay)(unsigned long);
52 	void (*const_udelay)(unsigned long);
53 	void (*udelay)(unsigned long);
54 	unsigned long ticks_per_jiffy;
55 } arm_delay_ops;
56 
57 #define __delay(n)		arm_delay_ops.delay(n)
58 
59 /*
60  * This function intentionally does not exist; if you see references to
61  * it, it means that you're calling udelay() with an out of range value.
62  *
63  * With currently imposed limits, this means that we support a max delay
64  * of 2000us. Further limits: HZ<=1000
65  */
66 extern void __bad_udelay(void);
67 
68 /*
69  * division by multiplication: you don't have to worry about
70  * loss of precision.
71  *
72  * Use only for very small delays ( < 2 msec).  Should probably use a
73  * lookup table, really, as the multiplications take much too long with
74  * short delays.  This is a "reasonable" implementation, though (and the
75  * first constant multiplications gets optimized away if the delay is
76  * a constant)
77  */
78 #define __udelay(n)		arm_delay_ops.udelay(n)
79 #define __const_udelay(n)	arm_delay_ops.const_udelay(n)
80 
81 #define udelay(n)							\
82 	(__builtin_constant_p(n) ?					\
83 	  ((n) > (MAX_UDELAY_MS * 1000) ? __bad_udelay() :		\
84 			__const_udelay((n) * UDELAY_MULT)) :		\
85 	  __udelay(n))
86 
87 /* Loop-based definitions for assembly code. */
88 extern void __loop_delay(unsigned long loops);
89 extern void __loop_udelay(unsigned long usecs);
90 extern void __loop_const_udelay(unsigned long);
91 
92 /* Delay-loop timer registration. */
93 #define ARCH_HAS_READ_CURRENT_TIMER
94 extern void register_current_timer_delay(const struct delay_timer *timer);
95 
96 #endif /* __ASSEMBLY__ */
97 
98 #endif /* defined(_ARM_DELAY_H) */
99 
100