delay.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (C) 2012 Regents of the University of California
  4. */
  5. #include <linux/delay.h>
  6. #include <linux/math.h>
  7. #include <linux/param.h>
  8. #include <linux/timex.h>
  9. #include <linux/types.h>
  10. #include <linux/export.h>
  11. #include <asm/processor.h>
  12. /*
  13. * This is copies from arch/arm/include/asm/delay.h
  14. *
  15. * Loop (or tick) based delay:
  16. *
  17. * loops = loops_per_jiffy * jiffies_per_sec * delay_us / us_per_sec
  18. *
  19. * where:
  20. *
  21. * jiffies_per_sec = HZ
  22. * us_per_sec = 1000000
  23. *
  24. * Therefore the constant part is HZ / 1000000 which is a small
  25. * fractional number. To make this usable with integer math, we
  26. * scale up this constant by 2^31, perform the actual multiplication,
  27. * and scale the result back down by 2^31 with a simple shift:
  28. *
  29. * loops = (loops_per_jiffy * delay_us * UDELAY_MULT) >> 31
  30. *
  31. * where:
  32. *
  33. * UDELAY_MULT = 2^31 * HZ / 1000000
  34. * = (2^31 / 1000000) * HZ
  35. * = 2147.483648 * HZ
  36. * = 2147 * HZ + 483648 * HZ / 1000000
  37. *
  38. * 31 is the biggest scale shift value that won't overflow 32 bits for
  39. * delay_us * UDELAY_MULT assuming HZ <= 1000 and delay_us <= 2000.
  40. */
  41. #define MAX_UDELAY_US 2000
  42. #define MAX_UDELAY_HZ 1000
  43. #define UDELAY_MULT (2147UL * HZ + 483648UL * HZ / 1000000UL)
  44. #define UDELAY_SHIFT 31
  45. #if HZ > MAX_UDELAY_HZ
  46. #error "HZ > MAX_UDELAY_HZ"
  47. #endif
  48. /*
  49. * RISC-V supports both UDELAY and NDELAY. This is largely the same as above,
  50. * but with different constants. I added 10 bits to the shift to get this, but
  51. * the result is that I need a 64-bit multiply, which is slow on 32-bit
  52. * platforms.
  53. *
  54. * NDELAY_MULT = 2^41 * HZ / 1000000000
  55. * = (2^41 / 1000000000) * HZ
  56. * = 2199.02325555 * HZ
  57. * = 2199 * HZ + 23255550 * HZ / 1000000000
  58. *
  59. * The maximum here is to avoid 64-bit overflow, but it isn't checked as it
  60. * won't happen.
  61. */
  62. #define MAX_NDELAY_NS (1ULL << 42)
  63. #define MAX_NDELAY_HZ MAX_UDELAY_HZ
  64. #define NDELAY_MULT ((unsigned long long)(2199ULL * HZ + 23255550ULL * HZ / 1000000000ULL))
  65. #define NDELAY_SHIFT 41
  66. #if HZ > MAX_NDELAY_HZ
  67. #error "HZ > MAX_NDELAY_HZ"
  68. #endif
  69. void __delay(unsigned long cycles)
  70. {
  71. u64 t0 = get_cycles();
  72. while ((unsigned long)(get_cycles() - t0) < cycles)
  73. cpu_relax();
  74. }
  75. EXPORT_SYMBOL(__delay);
  76. void udelay(unsigned long usecs)
  77. {
  78. u64 ucycles = (u64)usecs * lpj_fine * UDELAY_MULT;
  79. u64 n;
  80. if (unlikely(usecs > MAX_UDELAY_US)) {
  81. n = (u64)usecs * riscv_timebase;
  82. do_div(n, 1000000);
  83. __delay(n);
  84. return;
  85. }
  86. __delay(ucycles >> UDELAY_SHIFT);
  87. }
  88. EXPORT_SYMBOL(udelay);
  89. void ndelay(unsigned long nsecs)
  90. {
  91. /*
  92. * This doesn't bother checking for overflow, as it won't happen (it's
  93. * an hour) of delay.
  94. */
  95. unsigned long long ncycles = nsecs * lpj_fine * NDELAY_MULT;
  96. __delay(ncycles >> NDELAY_SHIFT);
  97. }
  98. EXPORT_SYMBOL(ndelay);