delay.c 3.1 KB

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