2 * linux/drivers/mmc/core/sdio_irq.c
4 * Author: Nicolas Pitre
5 * Created: June 18, 2007
6 * Copyright: MontaVista Software Inc.
8 * Copyright 2008 Pierre Ossman
10 * 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.
16 #include <linux/kernel.h>
17 #include <linux/sched.h>
18 #include <uapi/linux/sched/types.h>
19 #include <linux/kthread.h>
20 #include <linux/export.h>
21 #include <linux/wait.h>
22 #include <linux/delay.h>
24 #include <linux/mmc/core.h>
25 #include <linux/mmc/host.h>
26 #include <linux/mmc/card.h>
27 #include <linux/mmc/sdio.h>
28 #include <linux/mmc/sdio_func.h>
34 static int process_sdio_pending_irqs(struct mmc_host *host)
36 struct mmc_card *card = host->card;
38 bool sdio_irq_pending = host->sdio_irq_pending;
39 unsigned char pending;
40 struct sdio_func *func;
42 /* Don't process SDIO IRQs if the card is suspended. */
43 if (mmc_card_suspended(card))
46 /* Clear the flag to indicate that we have processed the IRQ. */
47 host->sdio_irq_pending = false;
50 * Optimization, if there is only 1 function interrupt registered
51 * and we know an IRQ was signaled then call irq handler directly.
52 * Otherwise do the full probe.
54 func = card->sdio_single_irq;
55 if (func && sdio_irq_pending) {
56 func->irq_handler(func);
60 ret = mmc_io_rw_direct(card, 0, 0, SDIO_CCCR_INTx, 0, &pending);
62 pr_debug("%s: error %d reading SDIO_CCCR_INTx\n",
63 mmc_card_id(card), ret);
67 if (pending && mmc_card_broken_irq_polling(card) &&
68 !(host->caps & MMC_CAP_SDIO_IRQ)) {
71 /* A fake interrupt could be created when we poll SDIO_CCCR_INTx
72 * register with a Marvell SD8797 card. A dummy CMD52 read to
73 * function 0 register 0xff can avoid this.
75 mmc_io_rw_direct(card, 0, 0, 0xff, 0, &dummy);
79 for (i = 1; i <= 7; i++) {
80 if (pending & (1 << i)) {
81 func = card->sdio_func[i - 1];
83 pr_warn("%s: pending IRQ for non-existent function\n",
86 } else if (func->irq_handler) {
87 func->irq_handler(func);
90 pr_warn("%s: pending IRQ with no handler\n",
103 void sdio_run_irqs(struct mmc_host *host)
105 mmc_claim_host(host);
106 if (host->sdio_irqs) {
107 process_sdio_pending_irqs(host);
108 if (host->ops->ack_sdio_irq)
109 host->ops->ack_sdio_irq(host);
111 mmc_release_host(host);
113 EXPORT_SYMBOL_GPL(sdio_run_irqs);
115 void sdio_irq_work(struct work_struct *work)
117 struct mmc_host *host =
118 container_of(work, struct mmc_host, sdio_irq_work.work);
123 void sdio_signal_irq(struct mmc_host *host)
125 host->sdio_irq_pending = true;
126 queue_delayed_work(system_wq, &host->sdio_irq_work, 0);
128 EXPORT_SYMBOL_GPL(sdio_signal_irq);
130 static int sdio_irq_thread(void *_host)
132 struct mmc_host *host = _host;
133 struct sched_param param = { .sched_priority = 1 };
134 unsigned long period, idle_period;
137 sched_setscheduler(current, SCHED_FIFO, ¶m);
140 * We want to allow for SDIO cards to work even on non SDIO
141 * aware hosts. One thing that non SDIO host cannot do is
142 * asynchronous notification of pending SDIO card interrupts
143 * hence we poll for them in that case.
145 idle_period = msecs_to_jiffies(10);
146 period = (host->caps & MMC_CAP_SDIO_IRQ) ?
147 MAX_SCHEDULE_TIMEOUT : idle_period;
149 pr_debug("%s: IRQ thread started (poll period = %lu jiffies)\n",
150 mmc_hostname(host), period);
154 * We claim the host here on drivers behalf for a couple
157 * 1) it is already needed to retrieve the CCCR_INTx;
158 * 2) we want the driver(s) to clear the IRQ condition ASAP;
159 * 3) we need to control the abort condition locally.
161 * Just like traditional hard IRQ handlers, we expect SDIO
162 * IRQ handlers to be quick and to the point, so that the
163 * holding of the host lock does not cover too much work
164 * that doesn't require that lock to be held.
166 ret = __mmc_claim_host(host, &host->sdio_irq_thread_abort);
169 ret = process_sdio_pending_irqs(host);
170 mmc_release_host(host);
173 * Give other threads a chance to run in the presence of
177 set_current_state(TASK_INTERRUPTIBLE);
178 if (!kthread_should_stop())
179 schedule_timeout(HZ);
180 set_current_state(TASK_RUNNING);
184 * Adaptive polling frequency based on the assumption
185 * that an interrupt will be closely followed by more.
186 * This has a substantial benefit for network devices.
188 if (!(host->caps & MMC_CAP_SDIO_IRQ)) {
193 if (period > idle_period)
194 period = idle_period;
198 set_current_state(TASK_INTERRUPTIBLE);
199 if (host->caps & MMC_CAP_SDIO_IRQ)
200 host->ops->enable_sdio_irq(host, 1);
201 if (!kthread_should_stop())
202 schedule_timeout(period);
203 set_current_state(TASK_RUNNING);
204 } while (!kthread_should_stop());
206 if (host->caps & MMC_CAP_SDIO_IRQ)
207 host->ops->enable_sdio_irq(host, 0);
209 pr_debug("%s: IRQ thread exiting with code %d\n",
210 mmc_hostname(host), ret);
215 static int sdio_card_irq_get(struct mmc_card *card)
217 struct mmc_host *host = card->host;
219 WARN_ON(!host->claimed);
221 if (!host->sdio_irqs++) {
222 if (!(host->caps2 & MMC_CAP2_SDIO_IRQ_NOTHREAD)) {
223 atomic_set(&host->sdio_irq_thread_abort, 0);
224 host->sdio_irq_thread =
225 kthread_run(sdio_irq_thread, host,
226 "ksdioirqd/%s", mmc_hostname(host));
227 if (IS_ERR(host->sdio_irq_thread)) {
228 int err = PTR_ERR(host->sdio_irq_thread);
232 } else if (host->caps & MMC_CAP_SDIO_IRQ) {
233 host->ops->enable_sdio_irq(host, 1);
240 static int sdio_card_irq_put(struct mmc_card *card)
242 struct mmc_host *host = card->host;
244 WARN_ON(!host->claimed);
246 if (host->sdio_irqs < 1)
249 if (!--host->sdio_irqs) {
250 if (!(host->caps2 & MMC_CAP2_SDIO_IRQ_NOTHREAD)) {
251 atomic_set(&host->sdio_irq_thread_abort, 1);
252 kthread_stop(host->sdio_irq_thread);
253 } else if (host->caps & MMC_CAP_SDIO_IRQ) {
254 host->ops->enable_sdio_irq(host, 0);
261 /* If there is only 1 function registered set sdio_single_irq */
262 static void sdio_single_irq_set(struct mmc_card *card)
264 struct sdio_func *func;
267 card->sdio_single_irq = NULL;
268 if ((card->host->caps & MMC_CAP_SDIO_IRQ) &&
269 card->host->sdio_irqs == 1)
270 for (i = 0; i < card->sdio_funcs; i++) {
271 func = card->sdio_func[i];
272 if (func && func->irq_handler) {
273 card->sdio_single_irq = func;
280 * sdio_claim_irq - claim the IRQ for a SDIO function
281 * @func: SDIO function
282 * @handler: IRQ handler callback
284 * Claim and activate the IRQ for the given SDIO function. The provided
285 * handler will be called when that IRQ is asserted. The host is always
286 * claimed already when the handler is called so the handler must not
287 * call sdio_claim_host() nor sdio_release_host().
289 int sdio_claim_irq(struct sdio_func *func, sdio_irq_handler_t *handler)
297 pr_debug("SDIO: Enabling IRQ for %s...\n", sdio_func_id(func));
299 if (func->irq_handler) {
300 pr_debug("SDIO: IRQ for %s already in use.\n", sdio_func_id(func));
304 ret = mmc_io_rw_direct(func->card, 0, 0, SDIO_CCCR_IENx, 0, ®);
308 reg |= 1 << func->num;
310 reg |= 1; /* Master interrupt enable */
312 ret = mmc_io_rw_direct(func->card, 1, 0, SDIO_CCCR_IENx, reg, NULL);
316 func->irq_handler = handler;
317 ret = sdio_card_irq_get(func->card);
319 func->irq_handler = NULL;
320 sdio_single_irq_set(func->card);
324 EXPORT_SYMBOL_GPL(sdio_claim_irq);
327 * sdio_release_irq - release the IRQ for a SDIO function
328 * @func: SDIO function
330 * Disable and release the IRQ for the given SDIO function.
332 int sdio_release_irq(struct sdio_func *func)
340 pr_debug("SDIO: Disabling IRQ for %s...\n", sdio_func_id(func));
342 if (func->irq_handler) {
343 func->irq_handler = NULL;
344 sdio_card_irq_put(func->card);
345 sdio_single_irq_set(func->card);
348 ret = mmc_io_rw_direct(func->card, 0, 0, SDIO_CCCR_IENx, 0, ®);
352 reg &= ~(1 << func->num);
354 /* Disable master interrupt with the last function interrupt */
358 ret = mmc_io_rw_direct(func->card, 1, 0, SDIO_CCCR_IENx, reg, NULL);
364 EXPORT_SYMBOL_GPL(sdio_release_irq);