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 = msecs_to_jiffies(10);
75 pr_debug("%s: IRQ thread started (poll period = %lu jiffies)\n",
76 mmc_hostname(host), period);
80 * We claim the host here on drivers behalf for a couple
83 * 1) it is already needed to retrieve the CCCR_INTx;
84 * 2) we want the driver(s) to clear the IRQ condition ASAP;
85 * 3) we need to control the abort condition locally.
87 * Just like traditional hard IRQ handlers, we expect SDIO
88 * IRQ handlers to be quick and to the point, so that the
89 * holding of the host lock does not cover too much work
90 * that doesn't require that lock to be held.
92 ret = __mmc_claim_host(host, &host->sdio_irq_thread_abort);
95 ret = process_sdio_pending_irqs(host->card);
96 mmc_release_host(host);
99 * Give other threads a chance to run in the presence of
100 * errors. FIXME: determine if due to card removal and
101 * possibly exit this thread if so.
106 set_task_state(current, TASK_INTERRUPTIBLE);
107 if (!kthread_should_stop())
108 schedule_timeout(period);
109 set_task_state(current, TASK_RUNNING);
110 } while (!kthread_should_stop());
112 pr_debug("%s: IRQ thread exiting with code %d\n",
113 mmc_hostname(host), ret);
118 static int sdio_card_irq_get(struct mmc_card *card)
120 struct mmc_host *host = card->host;
122 BUG_ON(!host->claimed);
124 if (!host->sdio_irqs++) {
125 atomic_set(&host->sdio_irq_thread_abort, 0);
126 host->sdio_irq_thread =
127 kthread_run(sdio_irq_thread, host, "ksdiorqd");
128 if (IS_ERR(host->sdio_irq_thread)) {
129 int err = PTR_ERR(host->sdio_irq_thread);
138 static int sdio_card_irq_put(struct mmc_card *card)
140 struct mmc_host *host = card->host;
142 BUG_ON(!host->claimed);
143 BUG_ON(host->sdio_irqs < 1);
145 if (!--host->sdio_irqs) {
146 atomic_set(&host->sdio_irq_thread_abort, 1);
147 kthread_stop(host->sdio_irq_thread);
154 * sdio_claim_irq - claim the IRQ for a SDIO function
155 * @func: SDIO function
156 * @handler: IRQ handler callback
158 * Claim and activate the IRQ for the given SDIO function. The provided
159 * handler will be called when that IRQ is asserted. The host is always
160 * claimed already when the handler is called so the handler must not
161 * call sdio_claim_host() nor sdio_release_host().
163 int sdio_claim_irq(struct sdio_func *func, sdio_irq_handler_t *handler)
171 pr_debug("SDIO: Enabling IRQ for %s...\n", sdio_func_id(func));
173 if (func->irq_handler) {
174 pr_debug("SDIO: IRQ for %s already in use.\n", sdio_func_id(func));
178 ret = mmc_io_rw_direct(func->card, 0, 0, SDIO_CCCR_IENx, 0, ®);
182 reg |= 1 << func->num;
184 reg |= 1; /* Master interrupt enable */
186 ret = mmc_io_rw_direct(func->card, 1, 0, SDIO_CCCR_IENx, reg, NULL);
190 func->irq_handler = handler;
191 ret = sdio_card_irq_get(func->card);
193 func->irq_handler = NULL;
197 EXPORT_SYMBOL_GPL(sdio_claim_irq);
200 * sdio_release_irq - release the IRQ for a SDIO function
201 * @func: SDIO function
203 * Disable and release the IRQ for the given SDIO function.
205 int sdio_release_irq(struct sdio_func *func)
213 pr_debug("SDIO: Disabling IRQ for %s...\n", sdio_func_id(func));
215 if (func->irq_handler) {
216 func->irq_handler = NULL;
217 sdio_card_irq_put(func->card);
220 ret = mmc_io_rw_direct(func->card, 0, 0, SDIO_CCCR_IENx, 0, ®);
224 reg &= ~(1 << func->num);
226 /* Disable master interrupt with the last function interrupt */
230 ret = mmc_io_rw_direct(func->card, 1, 0, SDIO_CCCR_IENx, reg, NULL);
236 EXPORT_SYMBOL_GPL(sdio_release_irq);