blob: f28053ce8cad4cfc2aa4be1e5be903507405c51c [file] [log] [blame]
Matt Wagantallb3ed9c12012-04-18 12:36:41 -07001/*
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 Wagantalla65161a2012-05-04 11:50:28 -070046 usleep_range(DIV_ROUND_UP(sleep_us, 4), sleep_us); \
Matt Wagantallb3ed9c12012-04-18 12:36:41 -070047 } \
48 (cond) ? 0 : -ETIMEDOUT; \
49})
50
51/**
Vikram Mulukutla7a35f5a2012-04-24 16:50:06 -070052 * readl_poll_timeout_noirq - Periodically poll an address until a condition is met or a timeout occurs
53 * @addr: Address to poll
54 * @val: Variable to read the value into
55 * @cond: Break condition (usually involving @val)
56 * @max_reads: Maximum number of reads before giving up
57 * @time_between_us: Time to udelay() between successive reads
58 *
59 * Returns 0 on success and -ETIMEDOUT upon a timeout.
60 */
61#define readl_poll_timeout_noirq(addr, val, cond, max_reads, time_between_us) \
62({ \
63 int count; \
64 for (count = (max_reads); count > 0; count--) { \
65 (val) = readl(addr); \
66 if (cond) \
67 break; \
68 udelay(time_between_us); \
69 } \
70 (cond) ? 0 : -ETIMEDOUT; \
71})
72
73/**
Matt Wagantallb3ed9c12012-04-18 12:36:41 -070074 * readl_poll - Periodically poll an address until a condition is met
75 * @addr: Address to poll
76 * @val: Variable to read the value into
77 * @cond: Break condition (usually involving @val)
78 * @sleep_us: Maximum time to sleep between reads in uS (0 tight-loops)
79 *
80 * Must not be called from atomic context if sleep_us is used.
81 */
82#define readl_poll(addr, val, cond, sleep_us) \
83 readl_poll_timeout(addr, val, cond, sleep_us, 0)
84
85/**
86 * readl_tight_poll_timeout - Tight-loop on an address until a condition is met or a timeout occurs
87 * @addr: Address to poll
88 * @val: Variable to read the value into
89 * @cond: Break condition (usually involving @val)
90 * @timeout_us: Timeout in uS, 0 means never timeout
91 *
92 * Returns 0 on success and -ETIMEDOUT upon a timeout. In either
93 * case, the last read value at @addr is stored in @val. Must not
94 * be called from atomic context if timeout_us is used.
95 */
96#define readl_tight_poll_timeout(addr, val, cond, timeout_us) \
97 readl_poll_timeout(addr, val, cond, 0, timeout_us)
98
99/**
100 * readl_tight_poll - Tight-loop on an address until a condition is met
101 * @addr: Address to poll
102 * @val: Variable to read the value into
103 * @cond: Break condition (usually involving @val)
104 *
105 * May be called from atomic context.
106 */
107#define readl_tight_poll(addr, val, cond) \
108 readl_poll_timeout(addr, val, cond, 0, 0)
109
110#endif /* _LINUX_IOPOLL_H */