2 * linux/drivers/mmc/core/sdio_irq.c
4 * Author: Nicolas Pitre
5 * Created: June 18, 2007
6 * Copyright: MontaVista Software Inc.
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.
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>
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>
28 static int process_sdio_pending_irqs(struct mmc_card *card)
31 unsigned char pending;
33 ret = mmc_io_rw_direct(card, 0, 0, SDIO_CCCR_INTx, 0, &pending);
35 printk(KERN_DEBUG "%s: error %d reading SDIO_CCCR_INTx\n",
36 mmc_card_id(card), ret);
41 for (i = 1; i <= 7; i++) {
42 if (pending & (1 << i)) {
43 struct sdio_func *func = card->sdio_func[i - 1];
45 printk(KERN_WARNING "%s: pending IRQ for "
46 "non-existant function\n",
49 } else if (func->irq_handler) {
50 func->irq_handler(func);
53 printk(KERN_WARNING "%s: pending IRQ with no handler\n",
66 static int sdio_irq_thread(void *_host)
68 struct mmc_host *host = _host;
69 struct sched_param param = { .sched_priority = 1 };
70 unsigned long period, idle_period;
73 sched_setscheduler(current, SCHED_FIFO, ¶m);
76 * We want to allow for SDIO cards to work even on non SDIO
77 * aware hosts. One thing that non SDIO host cannot do is
78 * asynchronous notification of pending SDIO card interrupts
79 * hence we poll for them in that case.
81 idle_period = msecs_to_jiffies(10);
82 period = (host->caps & MMC_CAP_SDIO_IRQ) ?
83 MAX_SCHEDULE_TIMEOUT : idle_period;
85 pr_debug("%s: IRQ thread started (poll period = %lu jiffies)\n",
86 mmc_hostname(host), period);
90 * We claim the host here on drivers behalf for a couple
93 * 1) it is already needed to retrieve the CCCR_INTx;
94 * 2) we want the driver(s) to clear the IRQ condition ASAP;
95 * 3) we need to control the abort condition locally.
97 * Just like traditional hard IRQ handlers, we expect SDIO
98 * IRQ handlers to be quick and to the point, so that the
99 * holding of the host lock does not cover too much work
100 * that doesn't require that lock to be held.
102 ret = __mmc_claim_host(host, &host->sdio_irq_thread_abort);
105 ret = process_sdio_pending_irqs(host->card);
106 mmc_release_host(host);
109 * Give other threads a chance to run in the presence of
110 * errors. FIXME: determine if due to card removal and
111 * possibly exit this thread if so.
117 * Adaptive polling frequency based on the assumption
118 * that an interrupt will be closely followed by more.
119 * This has a substantial benefit for network devices.
121 if (!(host->caps & MMC_CAP_SDIO_IRQ)) {
126 if (period > idle_period)
127 period = idle_period;
131 set_current_state(TASK_INTERRUPTIBLE);
132 if (host->caps & MMC_CAP_SDIO_IRQ)
133 host->ops->enable_sdio_irq(host, 1);
134 if (!kthread_should_stop())
135 schedule_timeout(period);
136 set_current_state(TASK_RUNNING);
137 } while (!kthread_should_stop());
139 if (host->caps & MMC_CAP_SDIO_IRQ)
140 host->ops->enable_sdio_irq(host, 0);
142 pr_debug("%s: IRQ thread exiting with code %d\n",
143 mmc_hostname(host), ret);
148 static int sdio_card_irq_get(struct mmc_card *card)
150 struct mmc_host *host = card->host;
152 WARN_ON(!host->claimed);
154 if (!host->sdio_irqs++) {
155 atomic_set(&host->sdio_irq_thread_abort, 0);
156 host->sdio_irq_thread =
157 kthread_run(sdio_irq_thread, host, "ksdiorqd");
158 if (IS_ERR(host->sdio_irq_thread)) {
159 int err = PTR_ERR(host->sdio_irq_thread);
168 static int sdio_card_irq_put(struct mmc_card *card)
170 struct mmc_host *host = card->host;
172 WARN_ON(!host->claimed);
173 BUG_ON(host->sdio_irqs < 1);
175 if (!--host->sdio_irqs) {
176 atomic_set(&host->sdio_irq_thread_abort, 1);
177 kthread_stop(host->sdio_irq_thread);
184 * sdio_claim_irq - claim the IRQ for a SDIO function
185 * @func: SDIO function
186 * @handler: IRQ handler callback
188 * Claim and activate the IRQ for the given SDIO function. The provided
189 * handler will be called when that IRQ is asserted. The host is always
190 * claimed already when the handler is called so the handler must not
191 * call sdio_claim_host() nor sdio_release_host().
193 int sdio_claim_irq(struct sdio_func *func, sdio_irq_handler_t *handler)
201 pr_debug("SDIO: Enabling IRQ for %s...\n", sdio_func_id(func));
203 if (func->irq_handler) {
204 pr_debug("SDIO: IRQ for %s already in use.\n", sdio_func_id(func));
208 ret = mmc_io_rw_direct(func->card, 0, 0, SDIO_CCCR_IENx, 0, ®);
212 reg |= 1 << func->num;
214 reg |= 1; /* Master interrupt enable */
216 ret = mmc_io_rw_direct(func->card, 1, 0, SDIO_CCCR_IENx, reg, NULL);
220 func->irq_handler = handler;
221 ret = sdio_card_irq_get(func->card);
223 func->irq_handler = NULL;
227 EXPORT_SYMBOL_GPL(sdio_claim_irq);
230 * sdio_release_irq - release the IRQ for a SDIO function
231 * @func: SDIO function
233 * Disable and release the IRQ for the given SDIO function.
235 int sdio_release_irq(struct sdio_func *func)
243 pr_debug("SDIO: Disabling IRQ for %s...\n", sdio_func_id(func));
245 if (func->irq_handler) {
246 func->irq_handler = NULL;
247 sdio_card_irq_put(func->card);
250 ret = mmc_io_rw_direct(func->card, 0, 0, SDIO_CCCR_IENx, 0, ®);
254 reg &= ~(1 << func->num);
256 /* Disable master interrupt with the last function interrupt */
260 ret = mmc_io_rw_direct(func->card, 1, 0, SDIO_CCCR_IENx, reg, NULL);
266 EXPORT_SYMBOL_GPL(sdio_release_irq);