iopoll.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. /*
  2. * Copyright (c) 2012-2014 The Linux Foundation. All rights reserved.
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License version 2 and
  6. * only version 2 as published by the Free Software Foundation.
  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. */
  14. #ifndef _LINUX_IOPOLL_H
  15. #define _LINUX_IOPOLL_H
  16. #include <linux/kernel.h>
  17. #include <linux/types.h>
  18. #include <linux/hrtimer.h>
  19. #include <linux/delay.h>
  20. #include <asm-generic/errno.h>
  21. #include <asm/io.h>
  22. /**
  23. * readl_poll_timeout - Periodically poll an address until a condition is met or a timeout occurs
  24. * @addr: Address to poll
  25. * @val: Variable to read the value into
  26. * @cond: Break condition (usually involving @val)
  27. * @sleep_us: Maximum time to sleep between reads in uS (0 tight-loops)
  28. * @timeout_us: Timeout in uS, 0 means never timeout
  29. *
  30. * Returns 0 on success and -ETIMEDOUT upon a timeout. In either
  31. * case, the last read value at @addr is stored in @val. Must not
  32. * be called from atomic context if sleep_us or timeout_us are used.
  33. */
  34. #define readl_poll_timeout(addr, val, cond, sleep_us, timeout_us) \
  35. ({ \
  36. ktime_t timeout = ktime_add_us(ktime_get(), timeout_us); \
  37. might_sleep_if(timeout_us); \
  38. for (;;) { \
  39. (val) = readl(addr); \
  40. if (cond) \
  41. break; \
  42. if (timeout_us && ktime_compare(ktime_get(), timeout) > 0) { \
  43. (val) = readl(addr); \
  44. break; \
  45. } \
  46. if (sleep_us) \
  47. usleep_range(DIV_ROUND_UP(sleep_us, 4), sleep_us); \
  48. } \
  49. (cond) ? 0 : -ETIMEDOUT; \
  50. })
  51. /**
  52. * readl_poll_timeout_noirq - Periodically poll an address until a condition is met or a timeout occurs
  53. * @addr: Address to poll
  54. * @val: Variable to read the value into
  55. * @cond: Break condition (usually involving @val)
  56. * @max_reads: Maximum number of reads before giving up
  57. * @time_between_us: Time to udelay() between successive reads
  58. *
  59. * Returns 0 on success and -ETIMEDOUT upon a timeout.
  60. */
  61. #define readl_poll_timeout_noirq(addr, val, cond, max_reads, time_between_us) \
  62. ({ \
  63. int count; \
  64. for (count = (max_reads); count > 0; count--) { \
  65. (val) = readl(addr); \
  66. if (cond) \
  67. break; \
  68. udelay(time_between_us); \
  69. } \
  70. (cond) ? 0 : -ETIMEDOUT; \
  71. })
  72. /**
  73. * readl_poll - Periodically poll an address until a condition is met
  74. * @addr: Address to poll
  75. * @val: Variable to read the value into
  76. * @cond: Break condition (usually involving @val)
  77. * @sleep_us: Maximum time to sleep between reads in uS (0 tight-loops)
  78. *
  79. * Must not be called from atomic context if sleep_us is used.
  80. */
  81. #define readl_poll(addr, val, cond, sleep_us) \
  82. readl_poll_timeout(addr, val, cond, sleep_us, 0)
  83. /**
  84. * readl_tight_poll_timeout - Tight-loop on an address until a condition is met or a timeout occurs
  85. * @addr: Address to poll
  86. * @val: Variable to read the value into
  87. * @cond: Break condition (usually involving @val)
  88. * @timeout_us: Timeout in uS, 0 means never timeout
  89. *
  90. * Returns 0 on success and -ETIMEDOUT upon a timeout. In either
  91. * case, the last read value at @addr is stored in @val. Must not
  92. * be called from atomic context if timeout_us is used.
  93. */
  94. #define readl_tight_poll_timeout(addr, val, cond, timeout_us) \
  95. readl_poll_timeout(addr, val, cond, 0, timeout_us)
  96. /**
  97. * readl_tight_poll - Tight-loop on an address until a condition is met
  98. * @addr: Address to poll
  99. * @val: Variable to read the value into
  100. * @cond: Break condition (usually involving @val)
  101. *
  102. * May be called from atomic context.
  103. */
  104. #define readl_tight_poll(addr, val, cond) \
  105. readl_poll_timeout(addr, val, cond, 0, 0)
  106. #endif /* _LINUX_IOPOLL_H */