Matt Wagantall | b3ed9c1 | 2012-04-18 12:36:41 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2012 Code Aurora Forum. 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 | |
| 15 | #ifndef _LINUX_IOPOLL_H |
| 16 | #define _LINUX_IOPOLL_H |
| 17 | |
| 18 | #include <linux/kernel.h> |
| 19 | #include <linux/types.h> |
| 20 | #include <linux/jiffies.h> |
| 21 | #include <linux/delay.h> |
| 22 | #include <asm-generic/errno.h> |
| 23 | #include <asm/io.h> |
| 24 | |
| 25 | /** |
| 26 | * readl_poll_timeout - Periodically poll an address until a condition is met or a timeout occurs |
| 27 | * @addr: Address to poll |
| 28 | * @val: Variable to read the value into |
| 29 | * @cond: Break condition (usually involving @val) |
| 30 | * @sleep_us: Maximum time to sleep between reads in uS (0 tight-loops) |
| 31 | * @timeout_us: Timeout in uS, 0 means never timeout |
| 32 | * |
| 33 | * Returns 0 on success and -ETIMEDOUT upon a timeout. In either |
| 34 | * case, the last read value at @addr is stored in @val. Must not |
| 35 | * be called from atomic context if sleep_us or timeout_us are used. |
| 36 | */ |
| 37 | #define readl_poll_timeout(addr, val, cond, sleep_us, timeout_us) \ |
| 38 | ({ \ |
| 39 | unsigned long timeout = jiffies + usecs_to_jiffies(timeout_us); \ |
| 40 | might_sleep_if(timeout_us); \ |
| 41 | for (;;) { \ |
| 42 | (val) = readl(addr); \ |
| 43 | if ((cond) || (timeout_us && time_after(jiffies, timeout))) \ |
| 44 | break; \ |
| 45 | if (sleep_us) \ |
Matt Wagantall | a65161a | 2012-05-04 11:50:28 -0700 | [diff] [blame] | 46 | usleep_range(DIV_ROUND_UP(sleep_us, 4), sleep_us); \ |
Matt Wagantall | b3ed9c1 | 2012-04-18 12:36:41 -0700 | [diff] [blame] | 47 | } \ |
| 48 | (cond) ? 0 : -ETIMEDOUT; \ |
| 49 | }) |
| 50 | |
| 51 | /** |
| 52 | * readl_poll - Periodically poll an address until a condition is met |
| 53 | * @addr: Address to poll |
| 54 | * @val: Variable to read the value into |
| 55 | * @cond: Break condition (usually involving @val) |
| 56 | * @sleep_us: Maximum time to sleep between reads in uS (0 tight-loops) |
| 57 | * |
| 58 | * Must not be called from atomic context if sleep_us is used. |
| 59 | */ |
| 60 | #define readl_poll(addr, val, cond, sleep_us) \ |
| 61 | readl_poll_timeout(addr, val, cond, sleep_us, 0) |
| 62 | |
| 63 | /** |
| 64 | * readl_tight_poll_timeout - Tight-loop on an address until a condition is met or a timeout occurs |
| 65 | * @addr: Address to poll |
| 66 | * @val: Variable to read the value into |
| 67 | * @cond: Break condition (usually involving @val) |
| 68 | * @timeout_us: Timeout in uS, 0 means never timeout |
| 69 | * |
| 70 | * Returns 0 on success and -ETIMEDOUT upon a timeout. In either |
| 71 | * case, the last read value at @addr is stored in @val. Must not |
| 72 | * be called from atomic context if timeout_us is used. |
| 73 | */ |
| 74 | #define readl_tight_poll_timeout(addr, val, cond, timeout_us) \ |
| 75 | readl_poll_timeout(addr, val, cond, 0, timeout_us) |
| 76 | |
| 77 | /** |
| 78 | * readl_tight_poll - Tight-loop on an address until a condition is met |
| 79 | * @addr: Address to poll |
| 80 | * @val: Variable to read the value into |
| 81 | * @cond: Break condition (usually involving @val) |
| 82 | * |
| 83 | * May be called from atomic context. |
| 84 | */ |
| 85 | #define readl_tight_poll(addr, val, cond) \ |
| 86 | readl_poll_timeout(addr, val, cond, 0, 0) |
| 87 | |
| 88 | #endif /* _LINUX_IOPOLL_H */ |