iopoll.h 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. /*
  3. * Copyright (c) 2012-2014 The Linux Foundation. All rights reserved.
  4. */
  5. #ifndef _LINUX_IOPOLL_H
  6. #define _LINUX_IOPOLL_H
  7. #include <linux/delay.h>
  8. #include <linux/errno.h>
  9. #include <linux/io.h>
  10. #include <time.h>
  11. /**
  12. * read_poll_timeout - Periodically poll an address until a condition is met or a timeout occurs
  13. * @op: accessor function (takes @addr as its only argument)
  14. * @addr: Address to poll
  15. * @val: Variable to read the value into
  16. * @cond: Break condition (usually involving @val)
  17. * @sleep_us: Maximum time to sleep in us
  18. * @timeout_us: Timeout in us, 0 means never timeout
  19. *
  20. * Returns 0 on success and -ETIMEDOUT upon a timeout. In either
  21. * case, the last read value at @addr is stored in @val.
  22. *
  23. * When available, you'll probably want to use one of the specialized
  24. * macros defined below rather than this macro directly.
  25. */
  26. #define read_poll_timeout(op, addr, val, cond, sleep_us, timeout_us) \
  27. ({ \
  28. unsigned long timeout = timer_get_us() + timeout_us; \
  29. for (;;) { \
  30. (val) = op(addr); \
  31. if (cond) \
  32. break; \
  33. if (timeout_us && time_after(timer_get_us(), timeout)) { \
  34. (val) = op(addr); \
  35. break; \
  36. } \
  37. if (sleep_us) \
  38. udelay(sleep_us); \
  39. } \
  40. (cond) ? 0 : -ETIMEDOUT; \
  41. })
  42. #define readx_poll_sleep_timeout(op, addr, val, cond, sleep_us, timeout_us) \
  43. read_poll_timeout(op, addr, val, cond, sleep_us, timeout_us)
  44. #define readl_poll_sleep_timeout(addr, val, cond, sleep_us, timeout_us) \
  45. readx_poll_sleep_timeout(readl, addr, val, cond, sleep_us, timeout_us)
  46. #define readx_poll_timeout(op, addr, val, cond, timeout_us) \
  47. read_poll_timeout(op, addr, val, cond, false, timeout_us)
  48. #define readb_poll_timeout(addr, val, cond, timeout_us) \
  49. readx_poll_timeout(readb, addr, val, cond, timeout_us)
  50. #define readw_poll_timeout(addr, val, cond, timeout_us) \
  51. readx_poll_timeout(readw, addr, val, cond, timeout_us)
  52. #define readl_poll_timeout(addr, val, cond, timeout_us) \
  53. readx_poll_timeout(readl, addr, val, cond, timeout_us)
  54. #define readq_poll_timeout(addr, val, cond, timeout_us) \
  55. readx_poll_timeout(readq, addr, val, cond, timeout_us)
  56. #define readb_relaxed_poll_timeout(addr, val, cond, timeout_us) \
  57. readx_poll_timeout(readb_relaxed, addr, val, cond, timeout_us)
  58. #define readw_relaxed_poll_timeout(addr, val, cond, timeout_us) \
  59. readx_poll_timeout(readw_relaxed, addr, val, cond, timeout_us)
  60. #define readl_relaxed_poll_timeout(addr, val, cond, timeout_us) \
  61. readx_poll_timeout(readl_relaxed, addr, val, cond, timeout_us)
  62. #define readq_relaxed_poll_timeout(addr, val, cond, timeout_us) \
  63. readx_poll_timeout(readq_relaxed, addr, val, cond, timeout_us)
  64. #endif /* _LINUX_IOPOLL_H */