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> |
| 14 | #include <asm/errno.h> |
| 15 | #include <asm/io.h> |
| 16 | |
| 17 | /** |
| 18 | * wait_for_bit() waits for bit set/cleared in register |
| 19 | * |
| 20 | * Function polls register waiting for specific bit(s) change |
| 21 | * (either 0->1 or 1->0). It can fail under two conditions: |
| 22 | * - Timeout |
| 23 | * - User interaction (CTRL-C) |
| 24 | * Function succeeds only if all bits of masked register are set/cleared |
| 25 | * (depending on set option). |
| 26 | * |
| 27 | * @param prefix Prefix added to timeout messagge (message visible only |
| 28 | * with debug enabled) |
| 29 | * @param reg Register that will be read (using readl()) |
| 30 | * @param mask Bit(s) of register that must be active |
| 31 | * @param set Selects wait condition (bit set or clear) |
| 32 | * @param timeout_ms Timeout (in miliseconds) |
| 33 | * @param breakable Enables CTRL-C interruption |
| 34 | * @return 0 on success, -ETIMEDOUT or -EINTR on failure |
| 35 | */ |
| 36 | static inline int wait_for_bit(const char *prefix, const u32 *reg, |
| 37 | const u32 mask, const bool set, |
| 38 | const unsigned int timeout_ms, |
| 39 | const bool breakable) |
| 40 | { |
| 41 | u32 val; |
| 42 | unsigned long start = get_timer(0); |
| 43 | |
| 44 | while (1) { |
| 45 | val = readl(reg); |
| 46 | |
| 47 | if (!set) |
| 48 | val = ~val; |
| 49 | |
| 50 | if ((val & mask) == mask) |
| 51 | return 0; |
| 52 | |
| 53 | if (get_timer(start) > timeout_ms) |
| 54 | break; |
| 55 | |
| 56 | if (breakable && ctrlc()) { |
| 57 | puts("Abort\n"); |
| 58 | return -EINTR; |
| 59 | } |
| 60 | |
| 61 | udelay(1); |
| 62 | } |
| 63 | |
| 64 | debug("%s: Timeout (reg=%p mask=%08x wait_set=%i)\n", prefix, reg, mask, |
| 65 | set); |
| 66 | |
| 67 | return -ETIMEDOUT; |
| 68 | } |
| 69 | |
| 70 | |
| 71 | #endif |