delay.h 2.8 KB

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