blob: e986b7f717c4159f537e0d34b0abc1ae2d01b746 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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
Will Deacond0a533b2012-07-06 15:47:17 +01009#include <asm/memory.h>
Peter Teichmann6d4518d2006-03-20 17:10:09 +000010#include <asm/param.h> /* HZ */
11
Nicolas Pitre207b1152016-10-07 05:38:35 +010012/*
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 */
Will Deacond0a533b2012-07-06 15:47:17 +010039#define MAX_UDELAY_MS 2
Russell Kingfb833b12016-10-05 23:40:43 +010040#define UDELAY_MULT UL(2147 * HZ + 483648 * HZ / 1000000)
Nicolas Pitre215e3622015-02-25 22:50:39 +010041#define UDELAY_SHIFT 31
Will Deacond0a533b2012-07-06 15:47:17 +010042
43#ifndef __ASSEMBLY__
44
Jonathan Austin56942fe2012-09-21 18:51:44 +010045struct delay_timer {
46 unsigned long (*read_current_timer)(void);
47 unsigned long freq;
48};
49
Will Deacond0a533b2012-07-06 15:47:17 +010050extern struct arm_delay_ops {
51 void (*delay)(unsigned long);
52 void (*const_udelay)(unsigned long);
53 void (*udelay)(unsigned long);
Will Deacon6f3d90e2013-03-28 11:17:55 +010054 unsigned long ticks_per_jiffy;
Will Deacond0a533b2012-07-06 15:47:17 +010055} arm_delay_ops;
56
57#define __delay(n) arm_delay_ops.delay(n)
Linus Torvalds1da177e2005-04-16 15:20:36 -070058
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
Nicolas Pitre215e3622015-02-25 22:50:39 +010064 * of 2000us. Further limits: HZ<=1000
Linus Torvalds1da177e2005-04-16 15:20:36 -070065 */
66extern void __bad_udelay(void);
67
68/*
69 * division by multiplication: you don't have to worry about
70 * loss of precision.
71 *
Will Deacond0a533b2012-07-06 15:47:17 +010072 * Use only for very small delays ( < 2 msec). Should probably use a
Linus Torvalds1da177e2005-04-16 15:20:36 -070073 * 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 */
Will Deacond0a533b2012-07-06 15:47:17 +010078#define __udelay(n) arm_delay_ops.udelay(n)
79#define __const_udelay(n) arm_delay_ops.const_udelay(n)
Linus Torvalds1da177e2005-04-16 15:20:36 -070080
Peter Teichmann6d4518d2006-03-20 17:10:09 +000081#define udelay(n) \
82 (__builtin_constant_p(n) ? \
83 ((n) > (MAX_UDELAY_MS * 1000) ? __bad_udelay() : \
Will Deacond0a533b2012-07-06 15:47:17 +010084 __const_udelay((n) * UDELAY_MULT)) : \
Linus Torvalds1da177e2005-04-16 15:20:36 -070085 __udelay(n))
86
Will Deacond0a533b2012-07-06 15:47:17 +010087/* Loop-based definitions for assembly code. */
88extern void __loop_delay(unsigned long loops);
89extern void __loop_udelay(unsigned long usecs);
90extern void __loop_const_udelay(unsigned long);
91
Jonathan Austin56942fe2012-09-21 18:51:44 +010092/* Delay-loop timer registration. */
93#define ARCH_HAS_READ_CURRENT_TIMER
94extern void register_current_timer_delay(const struct delay_timer *timer);
95
Will Deacond0a533b2012-07-06 15:47:17 +010096#endif /* __ASSEMBLY__ */
97
Linus Torvalds1da177e2005-04-16 15:20:36 -070098#endif /* defined(_ARM_DELAY_H) */
99