sdio: fix IRQ diagnostic message
[linux-2.6-block.git] / drivers / mmc / core / sdio_irq.c
CommitLineData
d1496c39
NP
1/*
2 * linux/drivers/mmc/core/sdio_irq.c
3 *
4 * Author: Nicolas Pitre
5 * Created: June 18, 2007
6 * Copyright: MontaVista Software Inc.
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or (at
11 * your option) any later version.
12 */
13
14#include <linux/kernel.h>
15#include <linux/sched.h>
16#include <linux/kthread.h>
17#include <linux/wait.h>
18#include <linux/delay.h>
19
20#include <linux/mmc/core.h>
21#include <linux/mmc/host.h>
22#include <linux/mmc/card.h>
23#include <linux/mmc/sdio.h>
24#include <linux/mmc/sdio_func.h>
25
26#include "sdio_ops.h"
27
28static int process_sdio_pending_irqs(struct mmc_card *card)
29{
6f4285d1 30 int i, ret, count;
d1496c39
NP
31 unsigned char pending;
32
33 ret = mmc_io_rw_direct(card, 0, 0, SDIO_CCCR_INTx, 0, &pending);
34 if (ret) {
35 printk(KERN_DEBUG "%s: error %d reading SDIO_CCCR_INTx\n",
36 mmc_card_id(card), ret);
37 return ret;
38 }
39
6f4285d1 40 count = 0;
d1496c39
NP
41 for (i = 1; i <= 7; i++) {
42 if (pending & (1 << i)) {
43 struct sdio_func *func = card->sdio_func[i - 1];
44 if (!func) {
45 printk(KERN_WARNING "%s: pending IRQ for "
46 "non-existant function\n",
3e01e4bc 47 mmc_card_id(card));
d1496c39
NP
48 } else if (func->irq_handler) {
49 func->irq_handler(func);
6f4285d1 50 count++;
d1496c39
NP
51 } else
52 printk(KERN_WARNING "%s: pending IRQ with no handler\n",
53 sdio_func_id(func));
54 }
55 }
56
6f4285d1 57 return count;
d1496c39
NP
58}
59
60static int sdio_irq_thread(void *_host)
61{
62 struct mmc_host *host = _host;
63 struct sched_param param = { .sched_priority = 1 };
6f4285d1 64 unsigned long period, idle_period;
d1496c39
NP
65 int ret;
66
67 sched_setscheduler(current, SCHED_FIFO, &param);
68
69 /*
70 * We want to allow for SDIO cards to work even on non SDIO
71 * aware hosts. One thing that non SDIO host cannot do is
72 * asynchronous notification of pending SDIO card interrupts
73 * hence we poll for them in that case.
74 */
6f4285d1 75 idle_period = msecs_to_jiffies(10);
17b759af 76 period = (host->caps & MMC_CAP_SDIO_IRQ) ?
6f4285d1 77 MAX_SCHEDULE_TIMEOUT : idle_period;
d1496c39
NP
78
79 pr_debug("%s: IRQ thread started (poll period = %lu jiffies)\n",
80 mmc_hostname(host), period);
81
82 do {
83 /*
84 * We claim the host here on drivers behalf for a couple
85 * reasons:
86 *
87 * 1) it is already needed to retrieve the CCCR_INTx;
88 * 2) we want the driver(s) to clear the IRQ condition ASAP;
89 * 3) we need to control the abort condition locally.
90 *
91 * Just like traditional hard IRQ handlers, we expect SDIO
92 * IRQ handlers to be quick and to the point, so that the
93 * holding of the host lock does not cover too much work
94 * that doesn't require that lock to be held.
95 */
96 ret = __mmc_claim_host(host, &host->sdio_irq_thread_abort);
97 if (ret)
98 break;
99 ret = process_sdio_pending_irqs(host->card);
100 mmc_release_host(host);
101
102 /*
103 * Give other threads a chance to run in the presence of
104 * errors. FIXME: determine if due to card removal and
105 * possibly exit this thread if so.
106 */
6f4285d1 107 if (ret < 0)
d1496c39
NP
108 ssleep(1);
109
6f4285d1
PO
110 /*
111 * Adaptive polling frequency based on the assumption
112 * that an interrupt will be closely followed by more.
113 * This has a substantial benefit for network devices.
114 */
115 if (!(host->caps & MMC_CAP_SDIO_IRQ)) {
116 if (ret > 0)
117 period /= 2;
118 else {
119 period++;
120 if (period > idle_period)
121 period = idle_period;
122 }
123 }
124
d1496c39 125 set_task_state(current, TASK_INTERRUPTIBLE);
17b759af
NP
126 if (host->caps & MMC_CAP_SDIO_IRQ)
127 host->ops->enable_sdio_irq(host, 1);
d1496c39
NP
128 if (!kthread_should_stop())
129 schedule_timeout(period);
130 set_task_state(current, TASK_RUNNING);
131 } while (!kthread_should_stop());
132
17b759af
NP
133 if (host->caps & MMC_CAP_SDIO_IRQ)
134 host->ops->enable_sdio_irq(host, 0);
135
d1496c39
NP
136 pr_debug("%s: IRQ thread exiting with code %d\n",
137 mmc_hostname(host), ret);
138
139 return ret;
140}
141
142static int sdio_card_irq_get(struct mmc_card *card)
143{
144 struct mmc_host *host = card->host;
145
d84075c8 146 WARN_ON(!host->claimed);
d1496c39
NP
147
148 if (!host->sdio_irqs++) {
149 atomic_set(&host->sdio_irq_thread_abort, 0);
150 host->sdio_irq_thread =
151 kthread_run(sdio_irq_thread, host, "ksdiorqd");
152 if (IS_ERR(host->sdio_irq_thread)) {
153 int err = PTR_ERR(host->sdio_irq_thread);
154 host->sdio_irqs--;
155 return err;
156 }
157 }
158
159 return 0;
160}
161
162static int sdio_card_irq_put(struct mmc_card *card)
163{
164 struct mmc_host *host = card->host;
165
d84075c8 166 WARN_ON(!host->claimed);
d1496c39
NP
167 BUG_ON(host->sdio_irqs < 1);
168
169 if (!--host->sdio_irqs) {
170 atomic_set(&host->sdio_irq_thread_abort, 1);
171 kthread_stop(host->sdio_irq_thread);
172 }
173
174 return 0;
175}
176
177/**
178 * sdio_claim_irq - claim the IRQ for a SDIO function
179 * @func: SDIO function
180 * @handler: IRQ handler callback
181 *
182 * Claim and activate the IRQ for the given SDIO function. The provided
183 * handler will be called when that IRQ is asserted. The host is always
184 * claimed already when the handler is called so the handler must not
185 * call sdio_claim_host() nor sdio_release_host().
186 */
187int sdio_claim_irq(struct sdio_func *func, sdio_irq_handler_t *handler)
188{
189 int ret;
190 unsigned char reg;
191
192 BUG_ON(!func);
193 BUG_ON(!func->card);
194
195 pr_debug("SDIO: Enabling IRQ for %s...\n", sdio_func_id(func));
196
197 if (func->irq_handler) {
198 pr_debug("SDIO: IRQ for %s already in use.\n", sdio_func_id(func));
199 return -EBUSY;
200 }
201
202 ret = mmc_io_rw_direct(func->card, 0, 0, SDIO_CCCR_IENx, 0, &reg);
203 if (ret)
204 return ret;
205
206 reg |= 1 << func->num;
207
208 reg |= 1; /* Master interrupt enable */
209
210 ret = mmc_io_rw_direct(func->card, 1, 0, SDIO_CCCR_IENx, reg, NULL);
211 if (ret)
212 return ret;
213
214 func->irq_handler = handler;
215 ret = sdio_card_irq_get(func->card);
216 if (ret)
217 func->irq_handler = NULL;
218
219 return ret;
220}
221EXPORT_SYMBOL_GPL(sdio_claim_irq);
222
223/**
224 * sdio_release_irq - release the IRQ for a SDIO function
225 * @func: SDIO function
226 *
227 * Disable and release the IRQ for the given SDIO function.
228 */
229int sdio_release_irq(struct sdio_func *func)
230{
231 int ret;
232 unsigned char reg;
233
234 BUG_ON(!func);
235 BUG_ON(!func->card);
236
237 pr_debug("SDIO: Disabling IRQ for %s...\n", sdio_func_id(func));
238
239 if (func->irq_handler) {
240 func->irq_handler = NULL;
241 sdio_card_irq_put(func->card);
242 }
243
244 ret = mmc_io_rw_direct(func->card, 0, 0, SDIO_CCCR_IENx, 0, &reg);
245 if (ret)
246 return ret;
247
248 reg &= ~(1 << func->num);
249
250 /* Disable master interrupt with the last function interrupt */
251 if (!(reg & 0xFE))
252 reg = 0;
253
254 ret = mmc_io_rw_direct(func->card, 1, 0, SDIO_CCCR_IENx, reg, NULL);
255 if (ret)
256 return ret;
257
258 return 0;
259}
260EXPORT_SYMBOL_GPL(sdio_release_irq);
261