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",
48 } else if (func->irq_handler) {
49 func->irq_handler(func);
52 printk(KERN_WARNING "%s: pending IRQ with no handler\n",
60 static int sdio_irq_thread(void *_host)
62 struct mmc_host *host = _host;
63 struct sched_param param = { .sched_priority = 1 };
64 unsigned long period, idle_period;
67 sched_setscheduler(current, SCHED_FIFO, ¶m);
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.
75 idle_period = msecs_to_jiffies(10);
76 period = (host->caps & MMC_CAP_SDIO_IRQ) ?
77 MAX_SCHEDULE_TIMEOUT : idle_period;
79 pr_debug("%s: IRQ thread started (poll period = %lu jiffies)\n",
80 mmc_hostname(host), period);
84 * We claim the host here on drivers behalf for a couple
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.
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.
96 ret = __mmc_claim_host(host, &host->sdio_irq_thread_abort);
99 ret = process_sdio_pending_irqs(host->card);
100 mmc_release_host(host);
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.
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.
115 if (!(host->caps & MMC_CAP_SDIO_IRQ)) {
120 if (period > idle_period)
121 period = idle_period;
125 set_task_state(current, TASK_INTERRUPTIBLE);
126 if (host->caps & MMC_CAP_SDIO_IRQ)
127 host->ops->enable_sdio_irq(host, 1);
128 if (!kthread_should_stop())
129 schedule_timeout(period);
130 set_task_state(current, TASK_RUNNING);
131 } while (!kthread_should_stop());
133 if (host->caps & MMC_CAP_SDIO_IRQ)
134 host->ops->enable_sdio_irq(host, 0);
136 pr_debug("%s: IRQ thread exiting with code %d\n",
137 mmc_hostname(host), ret);
142 static int sdio_card_irq_get(struct mmc_card *card)
144 struct mmc_host *host = card->host;
146 WARN_ON(!host->claimed);
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);
162 static int sdio_card_irq_put(struct mmc_card *card)
164 struct mmc_host *host = card->host;
166 WARN_ON(!host->claimed);
167 BUG_ON(host->sdio_irqs < 1);
169 if (!--host->sdio_irqs) {
170 atomic_set(&host->sdio_irq_thread_abort, 1);
171 kthread_stop(host->sdio_irq_thread);
178 * sdio_claim_irq - claim the IRQ for a SDIO function
179 * @func: SDIO function
180 * @handler: IRQ handler callback
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().
187 int sdio_claim_irq(struct sdio_func *func, sdio_irq_handler_t *handler)
195 pr_debug("SDIO: Enabling IRQ for %s...\n", sdio_func_id(func));
197 if (func->irq_handler) {
198 pr_debug("SDIO: IRQ for %s already in use.\n", sdio_func_id(func));
202 ret = mmc_io_rw_direct(func->card, 0, 0, SDIO_CCCR_IENx, 0, ®);
206 reg |= 1 << func->num;
208 reg |= 1; /* Master interrupt enable */
210 ret = mmc_io_rw_direct(func->card, 1, 0, SDIO_CCCR_IENx, reg, NULL);
214 func->irq_handler = handler;
215 ret = sdio_card_irq_get(func->card);
217 func->irq_handler = NULL;
221 EXPORT_SYMBOL_GPL(sdio_claim_irq);
224 * sdio_release_irq - release the IRQ for a SDIO function
225 * @func: SDIO function
227 * Disable and release the IRQ for the given SDIO function.
229 int sdio_release_irq(struct sdio_func *func)
237 pr_debug("SDIO: Disabling IRQ for %s...\n", sdio_func_id(func));
239 if (func->irq_handler) {
240 func->irq_handler = NULL;
241 sdio_card_irq_put(func->card);
244 ret = mmc_io_rw_direct(func->card, 0, 0, SDIO_CCCR_IENx, 0, ®);
248 reg &= ~(1 << func->num);
250 /* Disable master interrupt with the last function interrupt */
254 ret = mmc_io_rw_direct(func->card, 1, 0, SDIO_CCCR_IENx, reg, NULL);
260 EXPORT_SYMBOL_GPL(sdio_release_irq);