Mateusz Kulikowski | cc42c6b | 2016-01-23 11:54:29 +0100 | [diff] [blame] | 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 | |
| 9 | #ifndef __WAIT_BIT_H |
| 10 | #define __WAIT_BIT_H |
| 11 | |
| 12 | #include <common.h> |
| 13 | #include <console.h> |
Michal Simek | d16a04b | 2016-12-12 09:46:09 +0100 | [diff] [blame] | 14 | #include <watchdog.h> |
Masahiro Yamada | 56a931c | 2016-09-21 11:28:55 +0900 | [diff] [blame] | 15 | #include <linux/errno.h> |
Mateusz Kulikowski | cc42c6b | 2016-01-23 11:54:29 +0100 | [diff] [blame] | 16 | #include <asm/io.h> |
| 17 | |
| 18 | /** |
| 19 | * wait_for_bit() waits for bit set/cleared in register |
| 20 | * |
| 21 | * Function polls register waiting for specific bit(s) change |
| 22 | * (either 0->1 or 1->0). It can fail under two conditions: |
| 23 | * - Timeout |
| 24 | * - User interaction (CTRL-C) |
| 25 | * Function succeeds only if all bits of masked register are set/cleared |
| 26 | * (depending on set option). |
| 27 | * |
| 28 | * @param prefix Prefix added to timeout messagge (message visible only |
| 29 | * with debug enabled) |
| 30 | * @param reg Register that will be read (using readl()) |
| 31 | * @param mask Bit(s) of register that must be active |
| 32 | * @param set Selects wait condition (bit set or clear) |
| 33 | * @param timeout_ms Timeout (in miliseconds) |
| 34 | * @param breakable Enables CTRL-C interruption |
| 35 | * @return 0 on success, -ETIMEDOUT or -EINTR on failure |
| 36 | */ |
| 37 | static inline int wait_for_bit(const char *prefix, const u32 *reg, |
| 38 | const u32 mask, const bool set, |
| 39 | const unsigned int timeout_ms, |
| 40 | const bool breakable) |
| 41 | { |
| 42 | u32 val; |
| 43 | unsigned long start = get_timer(0); |
| 44 | |
| 45 | while (1) { |
| 46 | val = readl(reg); |
| 47 | |
| 48 | if (!set) |
| 49 | val = ~val; |
| 50 | |
| 51 | if ((val & mask) == mask) |
| 52 | return 0; |
| 53 | |
| 54 | if (get_timer(start) > timeout_ms) |
| 55 | break; |
| 56 | |
| 57 | if (breakable && ctrlc()) { |
| 58 | puts("Abort\n"); |
| 59 | return -EINTR; |
| 60 | } |
| 61 | |
| 62 | udelay(1); |
Michal Simek | d16a04b | 2016-12-12 09:46:09 +0100 | [diff] [blame] | 63 | WATCHDOG_RESET(); |
Mateusz Kulikowski | cc42c6b | 2016-01-23 11:54:29 +0100 | [diff] [blame] | 64 | } |
| 65 | |
| 66 | debug("%s: Timeout (reg=%p mask=%08x wait_set=%i)\n", prefix, reg, mask, |
| 67 | set); |
| 68 | |
| 69 | return -ETIMEDOUT; |
| 70 | } |
| 71 | |
Álvaro Fernández Rojas | 952831c | 2018-01-23 17:14:54 +0100 | [diff] [blame^] | 72 | /** |
| 73 | * wait_for_bit_x() waits for bit set/cleared in register |
| 74 | * |
| 75 | * Function polls register waiting for specific bit(s) change |
| 76 | * (either 0->1 or 1->0). It can fail under two conditions: |
| 77 | * - Timeout |
| 78 | * - User interaction (CTRL-C) |
| 79 | * Function succeeds only if all bits of masked register are set/cleared |
| 80 | * (depending on set option). |
| 81 | * |
| 82 | * @param reg Register that will be read (using read_x()) |
| 83 | * @param mask Bit(s) of register that must be active |
| 84 | * @param set Selects wait condition (bit set or clear) |
| 85 | * @param timeout_ms Timeout (in milliseconds) |
| 86 | * @param breakable Enables CTRL-C interruption |
| 87 | * @return 0 on success, -ETIMEDOUT or -EINTR on failure |
| 88 | */ |
| 89 | |
| 90 | #define BUILD_WAIT_FOR_BIT(sfx, type, read) \ |
| 91 | \ |
| 92 | static inline int wait_for_bit_##sfx(const void *reg, \ |
| 93 | const type mask, \ |
| 94 | const bool set, \ |
| 95 | const unsigned int timeout_ms, \ |
| 96 | const bool breakable) \ |
| 97 | { \ |
| 98 | type val; \ |
| 99 | unsigned long start = get_timer(0); \ |
| 100 | \ |
| 101 | while (1) { \ |
| 102 | val = read(reg); \ |
| 103 | \ |
| 104 | if (!set) \ |
| 105 | val = ~val; \ |
| 106 | \ |
| 107 | if ((val & mask) == mask) \ |
| 108 | return 0; \ |
| 109 | \ |
| 110 | if (get_timer(start) > timeout_ms) \ |
| 111 | break; \ |
| 112 | \ |
| 113 | if (breakable && ctrlc()) { \ |
| 114 | puts("Abort\n"); \ |
| 115 | return -EINTR; \ |
| 116 | } \ |
| 117 | \ |
| 118 | udelay(1); \ |
| 119 | WATCHDOG_RESET(); \ |
| 120 | } \ |
| 121 | \ |
| 122 | debug("%s: Timeout (reg=%p mask=%x wait_set=%i)\n", __func__, \ |
| 123 | reg, mask, set); \ |
| 124 | \ |
| 125 | return -ETIMEDOUT; \ |
| 126 | } |
| 127 | |
| 128 | BUILD_WAIT_FOR_BIT(8, u8, readb) |
| 129 | BUILD_WAIT_FOR_BIT(le16, u16, readw) |
| 130 | #ifdef readw_be |
| 131 | BUILD_WAIT_FOR_BIT(be16, u16, readw_be) |
| 132 | #endif |
| 133 | BUILD_WAIT_FOR_BIT(le32, u32, readl) |
| 134 | #ifdef readl_be |
| 135 | BUILD_WAIT_FOR_BIT(be32, u32, readl_be) |
| 136 | #endif |
Mateusz Kulikowski | cc42c6b | 2016-01-23 11:54:29 +0100 | [diff] [blame] | 137 | |
| 138 | #endif |