wait_bit.h 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. /*
  2. * Wait for bit with timeout and ctrlc
  3. *
  4. * (C) Copyright 2015 Mateusz Kulikowski <mateusz.kulikowski@gmail.com>
  5. *
  6. * SPDX-License-Identifier: GPL-2.0+
  7. */
  8. #ifndef __WAIT_BIT_H
  9. #define __WAIT_BIT_H
  10. #include <common.h>
  11. #include <console.h>
  12. #include <linux/errno.h>
  13. #include <asm/io.h>
  14. /**
  15. * wait_for_bit() waits for bit set/cleared in register
  16. *
  17. * Function polls register waiting for specific bit(s) change
  18. * (either 0->1 or 1->0). It can fail under two conditions:
  19. * - Timeout
  20. * - User interaction (CTRL-C)
  21. * Function succeeds only if all bits of masked register are set/cleared
  22. * (depending on set option).
  23. *
  24. * @param prefix Prefix added to timeout messagge (message visible only
  25. * with debug enabled)
  26. * @param reg Register that will be read (using readl())
  27. * @param mask Bit(s) of register that must be active
  28. * @param set Selects wait condition (bit set or clear)
  29. * @param timeout_ms Timeout (in miliseconds)
  30. * @param breakable Enables CTRL-C interruption
  31. * @return 0 on success, -ETIMEDOUT or -EINTR on failure
  32. */
  33. static inline int wait_for_bit(const char *prefix, const u32 *reg,
  34. const u32 mask, const bool set,
  35. const unsigned int timeout_ms,
  36. const bool breakable)
  37. {
  38. u32 val;
  39. unsigned long start = get_timer(0);
  40. while (1) {
  41. val = readl(reg);
  42. if (!set)
  43. val = ~val;
  44. if ((val & mask) == mask)
  45. return 0;
  46. if (get_timer(start) > timeout_ms)
  47. break;
  48. if (breakable && ctrlc()) {
  49. puts("Abort\n");
  50. return -EINTR;
  51. }
  52. udelay(1);
  53. }
  54. debug("%s: Timeout (reg=%p mask=%08x wait_set=%i)\n", prefix, reg, mask,
  55. set);
  56. return -ETIMEDOUT;
  57. }
  58. #endif