delay.h 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. /* SPDX-License-Identifier: GPL-2.0-or-later */
  2. #ifndef _ASM_POWERPC_DELAY_H
  3. #define _ASM_POWERPC_DELAY_H
  4. #ifdef __KERNEL__
  5. #include <linux/processor.h>
  6. #include <asm/time.h>
  7. /*
  8. * Copyright 1996, Paul Mackerras.
  9. * Copyright (C) 2009 Freescale Semiconductor, Inc. All rights reserved.
  10. *
  11. * PPC64 Support added by Dave Engebretsen, Todd Inglett, Mike Corrigan,
  12. * Anton Blanchard.
  13. */
  14. extern void __delay(unsigned long loops);
  15. extern void udelay(unsigned long usecs);
  16. /*
  17. * On shared processor machines the generic implementation of mdelay can
  18. * result in large errors. While each iteration of the loop inside mdelay
  19. * is supposed to take 1ms, the hypervisor could sleep our partition for
  20. * longer (eg 10ms). With the right timing these errors can add up.
  21. *
  22. * Since there is no 32bit overflow issue on 64bit kernels, just call
  23. * udelay directly.
  24. */
  25. #ifdef CONFIG_PPC64
  26. #define mdelay(n) udelay((n) * 1000)
  27. #endif
  28. /**
  29. * spin_event_timeout - spin until a condition gets true or a timeout elapses
  30. * @condition: a C expression to evalate
  31. * @timeout: timeout, in microseconds
  32. * @delay: the number of microseconds to delay between each evaluation of
  33. * @condition
  34. *
  35. * The process spins until the condition evaluates to true (non-zero) or the
  36. * timeout elapses. The return value of this macro is the value of
  37. * @condition when the loop terminates. This allows you to determine the cause
  38. * of the loop terminates. If the return value is zero, then you know a
  39. * timeout has occurred.
  40. *
  41. * This primary purpose of this macro is to poll on a hardware register
  42. * until a status bit changes. The timeout ensures that the loop still
  43. * terminates even if the bit never changes. The delay is for devices that
  44. * need a delay in between successive reads.
  45. *
  46. * gcc will optimize out the if-statement if @delay is a constant.
  47. */
  48. #define spin_event_timeout(condition, timeout, delay) \
  49. ({ \
  50. typeof(condition) __ret; \
  51. unsigned long __loops = tb_ticks_per_usec * timeout; \
  52. unsigned long __start = mftb(); \
  53. \
  54. if (delay) { \
  55. while (!(__ret = (condition)) && \
  56. (tb_ticks_since(__start) <= __loops)) \
  57. udelay(delay); \
  58. } else { \
  59. spin_begin(); \
  60. while (!(__ret = (condition)) && \
  61. (tb_ticks_since(__start) <= __loops)) \
  62. spin_cpu_relax(); \
  63. spin_end(); \
  64. } \
  65. if (!__ret) \
  66. __ret = (condition); \
  67. __ret; \
  68. })
  69. #endif /* __KERNEL__ */
  70. #endif /* _ASM_POWERPC_DELAY_H */