delay.c 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Delay loops based on the OpenRISC implementation.
  4. *
  5. * Copyright (C) 2012 ARM Limited
  6. *
  7. * Author: Will Deacon <[email protected]>
  8. */
  9. #include <linux/delay.h>
  10. #include <linux/init.h>
  11. #include <linux/kernel.h>
  12. #include <linux/module.h>
  13. #include <linux/timex.h>
  14. #include <clocksource/arm_arch_timer.h>
  15. #define USECS_TO_CYCLES(time_usecs) \
  16. xloops_to_cycles((time_usecs) * 0x10C7UL)
  17. static inline unsigned long xloops_to_cycles(unsigned long xloops)
  18. {
  19. return (xloops * loops_per_jiffy * HZ) >> 32;
  20. }
  21. void __delay(unsigned long cycles)
  22. {
  23. cycles_t start = get_cycles();
  24. if (cpus_have_const_cap(ARM64_HAS_WFXT)) {
  25. u64 end = start + cycles;
  26. /*
  27. * Start with WFIT. If an interrupt makes us resume
  28. * early, use a WFET loop to complete the delay.
  29. */
  30. wfit(end);
  31. while ((get_cycles() - start) < cycles)
  32. wfet(end);
  33. } else if (arch_timer_evtstrm_available()) {
  34. const cycles_t timer_evt_period =
  35. USECS_TO_CYCLES(ARCH_TIMER_EVT_STREAM_PERIOD_US);
  36. while ((get_cycles() - start + timer_evt_period) < cycles)
  37. wfe();
  38. }
  39. while ((get_cycles() - start) < cycles)
  40. cpu_relax();
  41. }
  42. EXPORT_SYMBOL(__delay);
  43. inline void __const_udelay(unsigned long xloops)
  44. {
  45. __delay(xloops_to_cycles(xloops));
  46. }
  47. EXPORT_SYMBOL(__const_udelay);
  48. void __udelay(unsigned long usecs)
  49. {
  50. __const_udelay(usecs * 0x10C7UL); /* 2**32 / 1000000 (rounded up) */
  51. }
  52. EXPORT_SYMBOL(__udelay);
  53. void __ndelay(unsigned long nsecs)
  54. {
  55. __const_udelay(nsecs * 0x5UL); /* 2**32 / 1000000000 (rounded up) */
  56. }
  57. EXPORT_SYMBOL(__ndelay);