blob: b882fe2cb9eb4de22e91607eb871a6fc156031c4 [file] [log] [blame]
Matt Wagantallb3ed9c12012-04-18 12:36:41 -07001/*
Matt Wagantall8a289892013-04-17 16:19:38 -07002 * Copyright (c) 2012-2013 The Linux Foundation. All rights reserved.
Matt Wagantallb3ed9c12012-04-18 12:36:41 -07003 *
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 Wagantall8a289892013-04-17 16:19:38 -070043 if (cond) \
Matt Wagantallb3ed9c12012-04-18 12:36:41 -070044 break; \
Matt Wagantall8a289892013-04-17 16:19:38 -070045 if (timeout_us && time_after(jiffies, timeout)) { \
46 (val) = readl(addr); \
47 break; \
48 } \
Matt Wagantallb3ed9c12012-04-18 12:36:41 -070049 if (sleep_us) \
Matt Wagantalla65161a2012-05-04 11:50:28 -070050 usleep_range(DIV_ROUND_UP(sleep_us, 4), sleep_us); \
Matt Wagantallb3ed9c12012-04-18 12:36:41 -070051 } \
52 (cond) ? 0 : -ETIMEDOUT; \
53})
54
55/**
Vikram Mulukutla7a35f5a2012-04-24 16:50:06 -070056 * 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 Wagantallb3ed9c12012-04-18 12:36:41 -070078 * 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 */