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);
40 for (i = 1; i <= 7; i++) {
41 if (pending & (1 << i)) {
42 struct sdio_func *func = card->sdio_func[i - 1];
44 printk(KERN_WARNING "%s: pending IRQ for "
45 "non-existant function\n",
47 } else if (func->irq_handler) {
48 func->irq_handler(func);
50 printk(KERN_WARNING "%s: pending IRQ with no handler\n",
58 static int sdio_irq_thread(void *_host)
60 struct mmc_host *host = _host;
61 struct sched_param param = { .sched_priority = 1 };
65 sched_setscheduler(current, SCHED_FIFO, ¶m);
68 * We want to allow for SDIO cards to work even on non SDIO
69 * aware hosts. One thing that non SDIO host cannot do is
70 * asynchronous notification of pending SDIO card interrupts
71 * hence we poll for them in that case.
73 period = (host->caps & MMC_CAP_SDIO_IRQ) ?
74 MAX_SCHEDULE_TIMEOUT : msecs_to_jiffies(10);
76 pr_debug("%s: IRQ thread started (poll period = %lu jiffies)\n",
77 mmc_hostname(host), period);
81 * We claim the host here on drivers behalf for a couple
84 * 1) it is already needed to retrieve the CCCR_INTx;
85 * 2) we want the driver(s) to clear the IRQ condition ASAP;
86 * 3) we need to control the abort condition locally.
88 * Just like traditional hard IRQ handlers, we expect SDIO
89 * IRQ handlers to be quick and to the point, so that the
90 * holding of the host lock does not cover too much work
91 * that doesn't require that lock to be held.
93 ret = __mmc_claim_host(host, &host->sdio_irq_thread_abort);
96 ret = process_sdio_pending_irqs(host->card);
97 mmc_release_host(host);
100 * Give other threads a chance to run in the presence of
101 * errors. FIXME: determine if due to card removal and
102 * possibly exit this thread if so.
107 set_task_state(current, TASK_INTERRUPTIBLE);
108 if (host->caps & MMC_CAP_SDIO_IRQ)
109 host->ops->enable_sdio_irq(host, 1);
110 if (!kthread_should_stop())
111 schedule_timeout(period);
112 set_task_state(current, TASK_RUNNING);
113 } while (!kthread_should_stop());
115 if (host->caps & MMC_CAP_SDIO_IRQ)
116 host->ops->enable_sdio_irq(host, 0);
118 pr_debug("%s: IRQ thread exiting with code %d\n",
119 mmc_hostname(host), ret);
124 static int sdio_card_irq_get(struct mmc_card *card)
126 struct mmc_host *host = card->host;
128 BUG_ON(!host->claimed);
130 if (!host->sdio_irqs++) {
131 atomic_set(&host->sdio_irq_thread_abort, 0);
132 host->sdio_irq_thread =
133 kthread_run(sdio_irq_thread, host, "ksdiorqd");
134 if (IS_ERR(host->sdio_irq_thread)) {
135 int err = PTR_ERR(host->sdio_irq_thread);
144 static int sdio_card_irq_put(struct mmc_card *card)
146 struct mmc_host *host = card->host;
148 BUG_ON(!host->claimed);
149 BUG_ON(host->sdio_irqs < 1);
151 if (!--host->sdio_irqs) {
152 atomic_set(&host->sdio_irq_thread_abort, 1);
153 kthread_stop(host->sdio_irq_thread);
160 * sdio_claim_irq - claim the IRQ for a SDIO function
161 * @func: SDIO function
162 * @handler: IRQ handler callback
164 * Claim and activate the IRQ for the given SDIO function. The provided
165 * handler will be called when that IRQ is asserted. The host is always
166 * claimed already when the handler is called so the handler must not
167 * call sdio_claim_host() nor sdio_release_host().
169 int sdio_claim_irq(struct sdio_func *func, sdio_irq_handler_t *handler)
177 pr_debug("SDIO: Enabling IRQ for %s...\n", sdio_func_id(func));
179 if (func->irq_handler) {
180 pr_debug("SDIO: IRQ for %s already in use.\n", sdio_func_id(func));
184 ret = mmc_io_rw_direct(func->card, 0, 0, SDIO_CCCR_IENx, 0, ®);
188 reg |= 1 << func->num;
190 reg |= 1; /* Master interrupt enable */
192 ret = mmc_io_rw_direct(func->card, 1, 0, SDIO_CCCR_IENx, reg, NULL);
196 func->irq_handler = handler;
197 ret = sdio_card_irq_get(func->card);
199 func->irq_handler = NULL;
203 EXPORT_SYMBOL_GPL(sdio_claim_irq);
206 * sdio_release_irq - release the IRQ for a SDIO function
207 * @func: SDIO function
209 * Disable and release the IRQ for the given SDIO function.
211 int sdio_release_irq(struct sdio_func *func)
219 pr_debug("SDIO: Disabling IRQ for %s...\n", sdio_func_id(func));
221 if (func->irq_handler) {
222 func->irq_handler = NULL;
223 sdio_card_irq_put(func->card);
226 ret = mmc_io_rw_direct(func->card, 0, 0, SDIO_CCCR_IENx, 0, ®);
230 reg &= ~(1 << func->num);
232 /* Disable master interrupt with the last function interrupt */
236 ret = mmc_io_rw_direct(func->card, 1, 0, SDIO_CCCR_IENx, reg, NULL);
242 EXPORT_SYMBOL_GPL(sdio_release_irq);