xref: /openbmc/linux/arch/arm/include/asm/delay.h (revision 2612e3bbc0386368a850140a6c9b990cd496a5ec)
1b2441318SGreg Kroah-Hartman /* SPDX-License-Identifier: GPL-2.0 */
24baa9922SRussell King /*
34baa9922SRussell King  * Copyright (C) 1995-2004 Russell King
44baa9922SRussell King  *
54baa9922SRussell King  * Delay routines, using a pre-computed "loops_per_second" value.
64baa9922SRussell King  */
74baa9922SRussell King #ifndef __ASM_ARM_DELAY_H
84baa9922SRussell King #define __ASM_ARM_DELAY_H
94baa9922SRussell King 
10*a9ff6961SLinus Walleij #include <asm/page.h>
114baa9922SRussell King #include <asm/param.h>	/* HZ */
124baa9922SRussell King 
13207b1150SNicolas Pitre /*
14207b1150SNicolas Pitre  * Loop (or tick) based delay:
15207b1150SNicolas Pitre  *
16207b1150SNicolas Pitre  * loops = loops_per_jiffy * jiffies_per_sec * delay_us / us_per_sec
17207b1150SNicolas Pitre  *
18207b1150SNicolas Pitre  * where:
19207b1150SNicolas Pitre  *
20207b1150SNicolas Pitre  * jiffies_per_sec = HZ
21207b1150SNicolas Pitre  * us_per_sec = 1000000
22207b1150SNicolas Pitre  *
23207b1150SNicolas Pitre  * Therefore the constant part is HZ / 1000000 which is a small
24207b1150SNicolas Pitre  * fractional number. To make this usable with integer math, we
25207b1150SNicolas Pitre  * scale up this constant by 2^31, perform the actual multiplication,
26207b1150SNicolas Pitre  * and scale the result back down by 2^31 with a simple shift:
27207b1150SNicolas Pitre  *
28207b1150SNicolas Pitre  * loops = (loops_per_jiffy * delay_us * UDELAY_MULT) >> 31
29207b1150SNicolas Pitre  *
30207b1150SNicolas Pitre  * where:
31207b1150SNicolas Pitre  *
32207b1150SNicolas Pitre  * UDELAY_MULT = 2^31 * HZ / 1000000
33207b1150SNicolas Pitre  *             = (2^31 / 1000000) * HZ
34207b1150SNicolas Pitre  *             = 2147.483648 * HZ
35207b1150SNicolas Pitre  *             = 2147 * HZ + 483648 * HZ / 1000000
36207b1150SNicolas Pitre  *
37207b1150SNicolas Pitre  * 31 is the biggest scale shift value that won't overflow 32 bits for
38207b1150SNicolas Pitre  * delay_us * UDELAY_MULT assuming HZ <= 1000 and delay_us <= 2000.
39207b1150SNicolas Pitre  */
40d0a533b1SWill Deacon #define MAX_UDELAY_MS	2
41fb833b1fSRussell King #define UDELAY_MULT	UL(2147 * HZ + 483648 * HZ / 1000000)
42215e362dSNicolas Pitre #define UDELAY_SHIFT	31
43d0a533b1SWill Deacon 
44d0a533b1SWill Deacon #ifndef __ASSEMBLY__
45d0a533b1SWill Deacon 
4656942fecSJonathan Austin struct delay_timer {
4756942fecSJonathan Austin 	unsigned long (*read_current_timer)(void);
4856942fecSJonathan Austin 	unsigned long freq;
4956942fecSJonathan Austin };
5056942fecSJonathan Austin 
51d0a533b1SWill Deacon extern struct arm_delay_ops {
52d0a533b1SWill Deacon 	void (*delay)(unsigned long);
53d0a533b1SWill Deacon 	void (*const_udelay)(unsigned long);
54d0a533b1SWill Deacon 	void (*udelay)(unsigned long);
556f3d90e5SWill Deacon 	unsigned long ticks_per_jiffy;
56d0a533b1SWill Deacon } arm_delay_ops;
57d0a533b1SWill Deacon 
58d0a533b1SWill Deacon #define __delay(n)		arm_delay_ops.delay(n)
594baa9922SRussell King 
604baa9922SRussell King /*
614baa9922SRussell King  * This function intentionally does not exist; if you see references to
624baa9922SRussell King  * it, it means that you're calling udelay() with an out of range value.
634baa9922SRussell King  *
644baa9922SRussell King  * With currently imposed limits, this means that we support a max delay
65215e362dSNicolas Pitre  * of 2000us. Further limits: HZ<=1000
664baa9922SRussell King  */
674baa9922SRussell King extern void __bad_udelay(void);
684baa9922SRussell King 
694baa9922SRussell King /*
704baa9922SRussell King  * division by multiplication: you don't have to worry about
714baa9922SRussell King  * loss of precision.
724baa9922SRussell King  *
73d0a533b1SWill Deacon  * Use only for very small delays ( < 2 msec).  Should probably use a
744baa9922SRussell King  * lookup table, really, as the multiplications take much too long with
754baa9922SRussell King  * short delays.  This is a "reasonable" implementation, though (and the
764baa9922SRussell King  * first constant multiplications gets optimized away if the delay is
774baa9922SRussell King  * a constant)
784baa9922SRussell King  */
79d0a533b1SWill Deacon #define __udelay(n)		arm_delay_ops.udelay(n)
80d0a533b1SWill Deacon #define __const_udelay(n)	arm_delay_ops.const_udelay(n)
814baa9922SRussell King 
824baa9922SRussell King #define udelay(n)							\
834baa9922SRussell King 	(__builtin_constant_p(n) ?					\
844baa9922SRussell King 	  ((n) > (MAX_UDELAY_MS * 1000) ? __bad_udelay() :		\
85d0a533b1SWill Deacon 			__const_udelay((n) * UDELAY_MULT)) :		\
864baa9922SRussell King 	  __udelay(n))
874baa9922SRussell King 
88d0a533b1SWill Deacon /* Loop-based definitions for assembly code. */
89d0a533b1SWill Deacon extern void __loop_delay(unsigned long loops);
90d0a533b1SWill Deacon extern void __loop_udelay(unsigned long usecs);
91d0a533b1SWill Deacon extern void __loop_const_udelay(unsigned long);
92d0a533b1SWill Deacon 
9356942fecSJonathan Austin /* Delay-loop timer registration. */
9456942fecSJonathan Austin #define ARCH_HAS_READ_CURRENT_TIMER
9556942fecSJonathan Austin extern void register_current_timer_delay(const struct delay_timer *timer);
9656942fecSJonathan Austin 
97d0a533b1SWill Deacon #endif /* __ASSEMBLY__ */
98d0a533b1SWill Deacon 
994baa9922SRussell King #endif /* defined(_ARM_DELAY_H) */
1004baa9922SRussell King 
101