delay.c 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. /*
  2. * Delay loops based on the OpenRISC implementation.
  3. *
  4. * Copyright (C) 2012 ARM Limited
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License version 2 as
  8. * published by the Free Software Foundation.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. *
  18. * Author: Will Deacon <will.deacon@arm.com>
  19. */
  20. #include <linux/delay.h>
  21. #include <linux/init.h>
  22. #include <linux/kernel.h>
  23. #include <linux/module.h>
  24. #include <linux/timex.h>
  25. #include <clocksource/arm_arch_timer.h>
  26. #define USECS_TO_CYCLES(time_usecs) \
  27. xloops_to_cycles((time_usecs) * 0x10C7UL)
  28. static inline unsigned long xloops_to_cycles(unsigned long xloops)
  29. {
  30. return (xloops * loops_per_jiffy * HZ) >> 32;
  31. }
  32. void __delay(unsigned long cycles)
  33. {
  34. cycles_t start = get_cycles();
  35. if (arch_timer_evtstrm_available()) {
  36. const cycles_t timer_evt_period =
  37. USECS_TO_CYCLES(ARCH_TIMER_EVT_STREAM_PERIOD_US);
  38. while ((get_cycles() - start + timer_evt_period) < cycles)
  39. wfe();
  40. }
  41. while ((get_cycles() - start) < cycles)
  42. cpu_relax();
  43. }
  44. EXPORT_SYMBOL(__delay);
  45. inline void __const_udelay(unsigned long xloops)
  46. {
  47. __delay(xloops_to_cycles(xloops));
  48. }
  49. EXPORT_SYMBOL(__const_udelay);
  50. void __udelay(unsigned long usecs)
  51. {
  52. __const_udelay(usecs * 0x10C7UL); /* 2**32 / 1000000 (rounded up) */
  53. }
  54. EXPORT_SYMBOL(__udelay);
  55. void __ndelay(unsigned long nsecs)
  56. {
  57. __const_udelay(nsecs * 0x5UL); /* 2**32 / 1000000000 (rounded up) */
  58. }
  59. EXPORT_SYMBOL(__ndelay);