blob: 4f80b72372b4160594366199b53f5261fc6d3f14 [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001/* SPDX-License-Identifier: GPL-2.0 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
3 * Copyright (C) 1995-2004 Russell King
4 *
5 * Delay routines, using a pre-computed "loops_per_second" value.
6 */
7#ifndef __ASM_ARM_DELAY_H
8#define __ASM_ARM_DELAY_H
9
Will Deacond0a533b2012-07-06 15:47:17 +010010#include <asm/memory.h>
Peter Teichmann6d4518d2006-03-20 17:10:09 +000011#include <asm/param.h> /* HZ */
12
Nicolas Pitre207b1152016-10-07 05:38:35 +010013/*
14 * Loop (or tick) based delay:
15 *
16 * loops = loops_per_jiffy * jiffies_per_sec * delay_us / us_per_sec
17 *
18 * where:
19 *
20 * jiffies_per_sec = HZ
21 * us_per_sec = 1000000
22 *
23 * Therefore the constant part is HZ / 1000000 which is a small
24 * fractional number. To make this usable with integer math, we
25 * scale up this constant by 2^31, perform the actual multiplication,
26 * and scale the result back down by 2^31 with a simple shift:
27 *
28 * loops = (loops_per_jiffy * delay_us * UDELAY_MULT) >> 31
29 *
30 * where:
31 *
32 * UDELAY_MULT = 2^31 * HZ / 1000000
33 * = (2^31 / 1000000) * HZ
34 * = 2147.483648 * HZ
35 * = 2147 * HZ + 483648 * HZ / 1000000
36 *
37 * 31 is the biggest scale shift value that won't overflow 32 bits for
38 * delay_us * UDELAY_MULT assuming HZ <= 1000 and delay_us <= 2000.
39 */
Will Deacond0a533b2012-07-06 15:47:17 +010040#define MAX_UDELAY_MS 2
Russell Kingfb833b12016-10-05 23:40:43 +010041#define UDELAY_MULT UL(2147 * HZ + 483648 * HZ / 1000000)
Nicolas Pitre215e3622015-02-25 22:50:39 +010042#define UDELAY_SHIFT 31
Will Deacond0a533b2012-07-06 15:47:17 +010043
44#ifndef __ASSEMBLY__
45
Jonathan Austin56942fe2012-09-21 18:51:44 +010046struct delay_timer {
47 unsigned long (*read_current_timer)(void);
48 unsigned long freq;
49};
50
Will Deacond0a533b2012-07-06 15:47:17 +010051extern struct arm_delay_ops {
52 void (*delay)(unsigned long);
53 void (*const_udelay)(unsigned long);
54 void (*udelay)(unsigned long);
Will Deacon6f3d90e2013-03-28 11:17:55 +010055 unsigned long ticks_per_jiffy;
Will Deacond0a533b2012-07-06 15:47:17 +010056} arm_delay_ops;
57
58#define __delay(n) arm_delay_ops.delay(n)
Linus Torvalds1da177e2005-04-16 15:20:36 -070059
60/*
61 * This function intentionally does not exist; if you see references to
62 * it, it means that you're calling udelay() with an out of range value.
63 *
64 * With currently imposed limits, this means that we support a max delay
Nicolas Pitre215e3622015-02-25 22:50:39 +010065 * of 2000us. Further limits: HZ<=1000
Linus Torvalds1da177e2005-04-16 15:20:36 -070066 */
67extern void __bad_udelay(void);
68
69/*
70 * division by multiplication: you don't have to worry about
71 * loss of precision.
72 *
Will Deacond0a533b2012-07-06 15:47:17 +010073 * Use only for very small delays ( < 2 msec). Should probably use a
Linus Torvalds1da177e2005-04-16 15:20:36 -070074 * lookup table, really, as the multiplications take much too long with
75 * short delays. This is a "reasonable" implementation, though (and the
76 * first constant multiplications gets optimized away if the delay is
77 * a constant)
78 */
Will Deacond0a533b2012-07-06 15:47:17 +010079#define __udelay(n) arm_delay_ops.udelay(n)
80#define __const_udelay(n) arm_delay_ops.const_udelay(n)
Linus Torvalds1da177e2005-04-16 15:20:36 -070081
Peter Teichmann6d4518d2006-03-20 17:10:09 +000082#define udelay(n) \
83 (__builtin_constant_p(n) ? \
84 ((n) > (MAX_UDELAY_MS * 1000) ? __bad_udelay() : \
Will Deacond0a533b2012-07-06 15:47:17 +010085 __const_udelay((n) * UDELAY_MULT)) : \
Linus Torvalds1da177e2005-04-16 15:20:36 -070086 __udelay(n))
87
Will Deacond0a533b2012-07-06 15:47:17 +010088/* Loop-based definitions for assembly code. */
89extern void __loop_delay(unsigned long loops);
90extern void __loop_udelay(unsigned long usecs);
91extern void __loop_const_udelay(unsigned long);
92
Jonathan Austin56942fe2012-09-21 18:51:44 +010093/* Delay-loop timer registration. */
94#define ARCH_HAS_READ_CURRENT_TIMER
95extern void register_current_timer_delay(const struct delay_timer *timer);
96
Will Deacond0a533b2012-07-06 15:47:17 +010097#endif /* __ASSEMBLY__ */
98
Linus Torvalds1da177e2005-04-16 15:20:36 -070099#endif /* defined(_ARM_DELAY_H) */
100