Matt Wagantall | b3ed9c1 | 2012-04-18 12:36:41 -0700 | [diff] [blame] | 1 | /* |
Matt Wagantall | 8a28989 | 2013-04-17 16:19:38 -0700 | [diff] [blame] | 2 | * Copyright (c) 2012-2013 The Linux Foundation. All rights reserved. |
Matt Wagantall | b3ed9c1 | 2012-04-18 12:36:41 -0700 | [diff] [blame] | 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); \ |
Matt Wagantall | 8a28989 | 2013-04-17 16:19:38 -0700 | [diff] [blame] | 43 | if (cond) \ |
Matt Wagantall | b3ed9c1 | 2012-04-18 12:36:41 -0700 | [diff] [blame] | 44 | break; \ |
Matt Wagantall | 8a28989 | 2013-04-17 16:19:38 -0700 | [diff] [blame] | 45 | if (timeout_us && time_after(jiffies, timeout)) { \ |
| 46 | (val) = readl(addr); \ |
| 47 | break; \ |
| 48 | } \ |
Matt Wagantall | b3ed9c1 | 2012-04-18 12:36:41 -0700 | [diff] [blame] | 49 | if (sleep_us) \ |
Matt Wagantall | a65161a | 2012-05-04 11:50:28 -0700 | [diff] [blame] | 50 | usleep_range(DIV_ROUND_UP(sleep_us, 4), sleep_us); \ |
Matt Wagantall | b3ed9c1 | 2012-04-18 12:36:41 -0700 | [diff] [blame] | 51 | } \ |
| 52 | (cond) ? 0 : -ETIMEDOUT; \ |
| 53 | }) |
| 54 | |
| 55 | /** |
Vikram Mulukutla | 7a35f5a | 2012-04-24 16:50:06 -0700 | [diff] [blame] | 56 | * readl_poll_timeout_noirq - Periodically poll an address until a condition is met or a timeout occurs |
| 57 | * @addr: Address to poll |
| 58 | * @val: Variable to read the value into |
| 59 | * @cond: Break condition (usually involving @val) |
| 60 | * @max_reads: Maximum number of reads before giving up |
| 61 | * @time_between_us: Time to udelay() between successive reads |
| 62 | * |
| 63 | * Returns 0 on success and -ETIMEDOUT upon a timeout. |
| 64 | */ |
| 65 | #define readl_poll_timeout_noirq(addr, val, cond, max_reads, time_between_us) \ |
| 66 | ({ \ |
| 67 | int count; \ |
| 68 | for (count = (max_reads); count > 0; count--) { \ |
| 69 | (val) = readl(addr); \ |
| 70 | if (cond) \ |
| 71 | break; \ |
| 72 | udelay(time_between_us); \ |
| 73 | } \ |
| 74 | (cond) ? 0 : -ETIMEDOUT; \ |
| 75 | }) |
| 76 | |
| 77 | /** |
Matt Wagantall | b3ed9c1 | 2012-04-18 12:36:41 -0700 | [diff] [blame] | 78 | * readl_poll - Periodically poll 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 | * @sleep_us: Maximum time to sleep between reads in uS (0 tight-loops) |
| 83 | * |
| 84 | * Must not be called from atomic context if sleep_us is used. |
| 85 | */ |
| 86 | #define readl_poll(addr, val, cond, sleep_us) \ |
| 87 | readl_poll_timeout(addr, val, cond, sleep_us, 0) |
| 88 | |
| 89 | /** |
| 90 | * readl_tight_poll_timeout - Tight-loop on an address until a condition is met or a timeout occurs |
| 91 | * @addr: Address to poll |
| 92 | * @val: Variable to read the value into |
| 93 | * @cond: Break condition (usually involving @val) |
| 94 | * @timeout_us: Timeout in uS, 0 means never timeout |
| 95 | * |
| 96 | * Returns 0 on success and -ETIMEDOUT upon a timeout. In either |
| 97 | * case, the last read value at @addr is stored in @val. Must not |
| 98 | * be called from atomic context if timeout_us is used. |
| 99 | */ |
| 100 | #define readl_tight_poll_timeout(addr, val, cond, timeout_us) \ |
| 101 | readl_poll_timeout(addr, val, cond, 0, timeout_us) |
| 102 | |
| 103 | /** |
| 104 | * readl_tight_poll - Tight-loop on an address until a condition is met |
| 105 | * @addr: Address to poll |
| 106 | * @val: Variable to read the value into |
| 107 | * @cond: Break condition (usually involving @val) |
| 108 | * |
| 109 | * May be called from atomic context. |
| 110 | */ |
| 111 | #define readl_tight_poll(addr, val, cond) \ |
| 112 | readl_poll_timeout(addr, val, cond, 0, 0) |
| 113 | |
| 114 | #endif /* _LINUX_IOPOLL_H */ |