blob: 722924cdf595b495dbad8f87fd0f5c7749cebc83 [file] [log] [blame]
Nicolas Pitred1496c32007-06-30 16:29:41 +02001/*
2 * linux/drivers/mmc/core/sdio_irq.c
3 *
4 * Author: Nicolas Pitre
5 * Created: June 18, 2007
6 * Copyright: MontaVista Software Inc.
7 *
Pierre Ossmane633b7b2008-08-31 13:38:54 +02008 * Copyright 2008 Pierre Ossman
9 *
Nicolas Pitred1496c32007-06-30 16:29:41 +020010 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or (at
13 * your option) any later version.
14 */
15
16#include <linux/kernel.h>
17#include <linux/sched.h>
18#include <linux/kthread.h>
19#include <linux/wait.h>
20#include <linux/delay.h>
21
22#include <linux/mmc/core.h>
23#include <linux/mmc/host.h>
24#include <linux/mmc/card.h>
25#include <linux/mmc/sdio.h>
26#include <linux/mmc/sdio_func.h>
27
28#include "sdio_ops.h"
29
30static int process_sdio_pending_irqs(struct mmc_card *card)
31{
Pierre Ossman6f4285d2007-09-27 10:48:29 +020032 int i, ret, count;
Nicolas Pitred1496c32007-06-30 16:29:41 +020033 unsigned char pending;
34
35 ret = mmc_io_rw_direct(card, 0, 0, SDIO_CCCR_INTx, 0, &pending);
36 if (ret) {
37 printk(KERN_DEBUG "%s: error %d reading SDIO_CCCR_INTx\n",
38 mmc_card_id(card), ret);
39 return ret;
40 }
41
Pierre Ossman6f4285d2007-09-27 10:48:29 +020042 count = 0;
Nicolas Pitred1496c32007-06-30 16:29:41 +020043 for (i = 1; i <= 7; i++) {
44 if (pending & (1 << i)) {
45 struct sdio_func *func = card->sdio_func[i - 1];
46 if (!func) {
47 printk(KERN_WARNING "%s: pending IRQ for "
48 "non-existant function\n",
Nicolas Pitre3e01e4b2007-10-03 15:32:10 -040049 mmc_card_id(card));
Nicolas Pitre599473c2007-10-03 15:32:57 -040050 ret = -EINVAL;
Nicolas Pitred1496c32007-06-30 16:29:41 +020051 } else if (func->irq_handler) {
52 func->irq_handler(func);
Pierre Ossman6f4285d2007-09-27 10:48:29 +020053 count++;
Nicolas Pitre599473c2007-10-03 15:32:57 -040054 } else {
Nicolas Pitred1496c32007-06-30 16:29:41 +020055 printk(KERN_WARNING "%s: pending IRQ with no handler\n",
56 sdio_func_id(func));
Nicolas Pitre599473c2007-10-03 15:32:57 -040057 ret = -EINVAL;
58 }
Nicolas Pitred1496c32007-06-30 16:29:41 +020059 }
60 }
61
Nicolas Pitre599473c2007-10-03 15:32:57 -040062 if (count)
63 return count;
64
65 return ret;
Nicolas Pitred1496c32007-06-30 16:29:41 +020066}
67
68static int sdio_irq_thread(void *_host)
69{
70 struct mmc_host *host = _host;
71 struct sched_param param = { .sched_priority = 1 };
Pierre Ossman6f4285d2007-09-27 10:48:29 +020072 unsigned long period, idle_period;
Nicolas Pitred1496c32007-06-30 16:29:41 +020073 int ret;
74
75 sched_setscheduler(current, SCHED_FIFO, &param);
76
77 /*
78 * We want to allow for SDIO cards to work even on non SDIO
79 * aware hosts. One thing that non SDIO host cannot do is
80 * asynchronous notification of pending SDIO card interrupts
81 * hence we poll for them in that case.
82 */
Pierre Ossman6f4285d2007-09-27 10:48:29 +020083 idle_period = msecs_to_jiffies(10);
Nicolas Pitre17b759a2007-07-24 02:09:39 -040084 period = (host->caps & MMC_CAP_SDIO_IRQ) ?
Pierre Ossman6f4285d2007-09-27 10:48:29 +020085 MAX_SCHEDULE_TIMEOUT : idle_period;
Nicolas Pitred1496c32007-06-30 16:29:41 +020086
87 pr_debug("%s: IRQ thread started (poll period = %lu jiffies)\n",
88 mmc_hostname(host), period);
89
90 do {
91 /*
92 * We claim the host here on drivers behalf for a couple
93 * reasons:
94 *
95 * 1) it is already needed to retrieve the CCCR_INTx;
96 * 2) we want the driver(s) to clear the IRQ condition ASAP;
97 * 3) we need to control the abort condition locally.
98 *
99 * Just like traditional hard IRQ handlers, we expect SDIO
100 * IRQ handlers to be quick and to the point, so that the
101 * holding of the host lock does not cover too much work
102 * that doesn't require that lock to be held.
103 */
104 ret = __mmc_claim_host(host, &host->sdio_irq_thread_abort);
105 if (ret)
106 break;
107 ret = process_sdio_pending_irqs(host->card);
108 mmc_release_host(host);
109
110 /*
111 * Give other threads a chance to run in the presence of
Pierre Ossmane633b7b2008-08-31 13:38:54 +0200112 * errors.
Nicolas Pitred1496c32007-06-30 16:29:41 +0200113 */
Pierre Ossmane633b7b2008-08-31 13:38:54 +0200114 if (ret < 0) {
115 set_current_state(TASK_INTERRUPTIBLE);
116 if (!kthread_should_stop())
117 schedule_timeout(HZ);
118 set_current_state(TASK_RUNNING);
119 }
Nicolas Pitred1496c32007-06-30 16:29:41 +0200120
Pierre Ossman6f4285d2007-09-27 10:48:29 +0200121 /*
122 * Adaptive polling frequency based on the assumption
123 * that an interrupt will be closely followed by more.
124 * This has a substantial benefit for network devices.
125 */
126 if (!(host->caps & MMC_CAP_SDIO_IRQ)) {
127 if (ret > 0)
128 period /= 2;
129 else {
130 period++;
131 if (period > idle_period)
132 period = idle_period;
133 }
134 }
135
Robert P. J. Day6fee65c2008-03-28 14:34:47 -0700136 set_current_state(TASK_INTERRUPTIBLE);
Nicolas Pitre17b759a2007-07-24 02:09:39 -0400137 if (host->caps & MMC_CAP_SDIO_IRQ)
138 host->ops->enable_sdio_irq(host, 1);
Nicolas Pitred1496c32007-06-30 16:29:41 +0200139 if (!kthread_should_stop())
140 schedule_timeout(period);
Robert P. J. Day6fee65c2008-03-28 14:34:47 -0700141 set_current_state(TASK_RUNNING);
Nicolas Pitred1496c32007-06-30 16:29:41 +0200142 } while (!kthread_should_stop());
143
Nicolas Pitre17b759a2007-07-24 02:09:39 -0400144 if (host->caps & MMC_CAP_SDIO_IRQ)
145 host->ops->enable_sdio_irq(host, 0);
146
Nicolas Pitred1496c32007-06-30 16:29:41 +0200147 pr_debug("%s: IRQ thread exiting with code %d\n",
148 mmc_hostname(host), ret);
149
150 return ret;
151}
152
153static int sdio_card_irq_get(struct mmc_card *card)
154{
155 struct mmc_host *host = card->host;
156
Pierre Ossmand84075c82007-08-09 13:23:56 +0200157 WARN_ON(!host->claimed);
Nicolas Pitred1496c32007-06-30 16:29:41 +0200158
159 if (!host->sdio_irqs++) {
160 atomic_set(&host->sdio_irq_thread_abort, 0);
161 host->sdio_irq_thread =
162 kthread_run(sdio_irq_thread, host, "ksdiorqd");
163 if (IS_ERR(host->sdio_irq_thread)) {
164 int err = PTR_ERR(host->sdio_irq_thread);
165 host->sdio_irqs--;
166 return err;
167 }
168 }
169
170 return 0;
171}
172
173static int sdio_card_irq_put(struct mmc_card *card)
174{
175 struct mmc_host *host = card->host;
176
Pierre Ossmand84075c82007-08-09 13:23:56 +0200177 WARN_ON(!host->claimed);
Nicolas Pitred1496c32007-06-30 16:29:41 +0200178 BUG_ON(host->sdio_irqs < 1);
179
180 if (!--host->sdio_irqs) {
181 atomic_set(&host->sdio_irq_thread_abort, 1);
182 kthread_stop(host->sdio_irq_thread);
183 }
184
185 return 0;
186}
187
188/**
189 * sdio_claim_irq - claim the IRQ for a SDIO function
190 * @func: SDIO function
191 * @handler: IRQ handler callback
192 *
193 * Claim and activate the IRQ for the given SDIO function. The provided
194 * handler will be called when that IRQ is asserted. The host is always
195 * claimed already when the handler is called so the handler must not
196 * call sdio_claim_host() nor sdio_release_host().
197 */
198int sdio_claim_irq(struct sdio_func *func, sdio_irq_handler_t *handler)
199{
200 int ret;
201 unsigned char reg;
202
203 BUG_ON(!func);
204 BUG_ON(!func->card);
205
206 pr_debug("SDIO: Enabling IRQ for %s...\n", sdio_func_id(func));
207
208 if (func->irq_handler) {
209 pr_debug("SDIO: IRQ for %s already in use.\n", sdio_func_id(func));
210 return -EBUSY;
211 }
212
213 ret = mmc_io_rw_direct(func->card, 0, 0, SDIO_CCCR_IENx, 0, &reg);
214 if (ret)
215 return ret;
216
217 reg |= 1 << func->num;
218
219 reg |= 1; /* Master interrupt enable */
220
221 ret = mmc_io_rw_direct(func->card, 1, 0, SDIO_CCCR_IENx, reg, NULL);
222 if (ret)
223 return ret;
224
225 func->irq_handler = handler;
226 ret = sdio_card_irq_get(func->card);
227 if (ret)
228 func->irq_handler = NULL;
229
230 return ret;
231}
232EXPORT_SYMBOL_GPL(sdio_claim_irq);
233
234/**
235 * sdio_release_irq - release the IRQ for a SDIO function
236 * @func: SDIO function
237 *
238 * Disable and release the IRQ for the given SDIO function.
239 */
240int sdio_release_irq(struct sdio_func *func)
241{
242 int ret;
243 unsigned char reg;
244
245 BUG_ON(!func);
246 BUG_ON(!func->card);
247
248 pr_debug("SDIO: Disabling IRQ for %s...\n", sdio_func_id(func));
249
250 if (func->irq_handler) {
251 func->irq_handler = NULL;
252 sdio_card_irq_put(func->card);
253 }
254
255 ret = mmc_io_rw_direct(func->card, 0, 0, SDIO_CCCR_IENx, 0, &reg);
256 if (ret)
257 return ret;
258
259 reg &= ~(1 << func->num);
260
261 /* Disable master interrupt with the last function interrupt */
262 if (!(reg & 0xFE))
263 reg = 0;
264
265 ret = mmc_io_rw_direct(func->card, 1, 0, SDIO_CCCR_IENx, reg, NULL);
266 if (ret)
267 return ret;
268
269 return 0;
270}
271EXPORT_SYMBOL_GPL(sdio_release_irq);
272