1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * linux/drivers/mmc/core/sdio_irq.c
5 * Author: Nicolas Pitre
6 * Created: June 18, 2007
7 * Copyright: MontaVista Software Inc.
9 * Copyright 2008 Pierre Ossman
12 #include <linux/kernel.h>
13 #include <linux/sched.h>
14 #include <uapi/linux/sched/types.h>
15 #include <linux/kthread.h>
16 #include <linux/export.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>
30 static int process_sdio_pending_irqs(struct mmc_host
*host
)
32 struct mmc_card
*card
= host
->card
;
34 bool sdio_irq_pending
= host
->sdio_irq_pending
;
35 unsigned char pending
;
36 struct sdio_func
*func
;
38 /* Don't process SDIO IRQs if the card is suspended. */
39 if (mmc_card_suspended(card
))
42 /* Clear the flag to indicate that we have processed the IRQ. */
43 host
->sdio_irq_pending
= false;
46 * Optimization, if there is only 1 function interrupt registered
47 * and we know an IRQ was signaled then call irq handler directly.
48 * Otherwise do the full probe.
50 func
= card
->sdio_single_irq
;
51 if (func
&& sdio_irq_pending
) {
52 func
->irq_handler(func
);
56 ret
= mmc_io_rw_direct(card
, 0, 0, SDIO_CCCR_INTx
, 0, &pending
);
58 pr_debug("%s: error %d reading SDIO_CCCR_INTx\n",
59 mmc_card_id(card
), ret
);
63 if (pending
&& mmc_card_broken_irq_polling(card
) &&
64 !(host
->caps
& MMC_CAP_SDIO_IRQ
)) {
67 /* A fake interrupt could be created when we poll SDIO_CCCR_INTx
68 * register with a Marvell SD8797 card. A dummy CMD52 read to
69 * function 0 register 0xff can avoid this.
71 mmc_io_rw_direct(card
, 0, 0, 0xff, 0, &dummy
);
75 for (i
= 1; i
<= 7; i
++) {
76 if (pending
& (1 << i
)) {
77 func
= card
->sdio_func
[i
- 1];
79 pr_warn("%s: pending IRQ for non-existent function\n",
82 } else if (func
->irq_handler
) {
83 func
->irq_handler(func
);
86 pr_warn("%s: pending IRQ with no handler\n",
99 static void sdio_run_irqs(struct mmc_host
*host
)
101 mmc_claim_host(host
);
102 if (host
->sdio_irqs
) {
103 process_sdio_pending_irqs(host
);
104 if (host
->ops
->ack_sdio_irq
)
105 host
->ops
->ack_sdio_irq(host
);
107 mmc_release_host(host
);
110 void sdio_irq_work(struct work_struct
*work
)
112 struct mmc_host
*host
=
113 container_of(work
, struct mmc_host
, sdio_irq_work
.work
);
118 void sdio_signal_irq(struct mmc_host
*host
)
120 host
->sdio_irq_pending
= true;
121 queue_delayed_work(system_wq
, &host
->sdio_irq_work
, 0);
123 EXPORT_SYMBOL_GPL(sdio_signal_irq
);
125 static int sdio_irq_thread(void *_host
)
127 struct mmc_host
*host
= _host
;
128 struct sched_param param
= { .sched_priority
= 1 };
129 unsigned long period
, idle_period
;
132 sched_setscheduler(current
, SCHED_FIFO
, ¶m
);
135 * We want to allow for SDIO cards to work even on non SDIO
136 * aware hosts. One thing that non SDIO host cannot do is
137 * asynchronous notification of pending SDIO card interrupts
138 * hence we poll for them in that case.
140 idle_period
= msecs_to_jiffies(10);
141 period
= (host
->caps
& MMC_CAP_SDIO_IRQ
) ?
142 MAX_SCHEDULE_TIMEOUT
: idle_period
;
144 pr_debug("%s: IRQ thread started (poll period = %lu jiffies)\n",
145 mmc_hostname(host
), period
);
149 * We claim the host here on drivers behalf for a couple
152 * 1) it is already needed to retrieve the CCCR_INTx;
153 * 2) we want the driver(s) to clear the IRQ condition ASAP;
154 * 3) we need to control the abort condition locally.
156 * Just like traditional hard IRQ handlers, we expect SDIO
157 * IRQ handlers to be quick and to the point, so that the
158 * holding of the host lock does not cover too much work
159 * that doesn't require that lock to be held.
161 ret
= __mmc_claim_host(host
, NULL
,
162 &host
->sdio_irq_thread_abort
);
165 ret
= process_sdio_pending_irqs(host
);
166 mmc_release_host(host
);
169 * Give other threads a chance to run in the presence of
173 set_current_state(TASK_INTERRUPTIBLE
);
174 if (!kthread_should_stop())
175 schedule_timeout(HZ
);
176 set_current_state(TASK_RUNNING
);
180 * Adaptive polling frequency based on the assumption
181 * that an interrupt will be closely followed by more.
182 * This has a substantial benefit for network devices.
184 if (!(host
->caps
& MMC_CAP_SDIO_IRQ
)) {
189 if (period
> idle_period
)
190 period
= idle_period
;
194 set_current_state(TASK_INTERRUPTIBLE
);
195 if (host
->caps
& MMC_CAP_SDIO_IRQ
)
196 host
->ops
->enable_sdio_irq(host
, 1);
197 if (!kthread_should_stop())
198 schedule_timeout(period
);
199 set_current_state(TASK_RUNNING
);
200 } while (!kthread_should_stop());
202 if (host
->caps
& MMC_CAP_SDIO_IRQ
)
203 host
->ops
->enable_sdio_irq(host
, 0);
205 pr_debug("%s: IRQ thread exiting with code %d\n",
206 mmc_hostname(host
), ret
);
211 static int sdio_card_irq_get(struct mmc_card
*card
)
213 struct mmc_host
*host
= card
->host
;
215 WARN_ON(!host
->claimed
);
217 if (!host
->sdio_irqs
++) {
218 if (!(host
->caps2
& MMC_CAP2_SDIO_IRQ_NOTHREAD
)) {
219 atomic_set(&host
->sdio_irq_thread_abort
, 0);
220 host
->sdio_irq_thread
=
221 kthread_run(sdio_irq_thread
, host
,
222 "ksdioirqd/%s", mmc_hostname(host
));
223 if (IS_ERR(host
->sdio_irq_thread
)) {
224 int err
= PTR_ERR(host
->sdio_irq_thread
);
228 } else if (host
->caps
& MMC_CAP_SDIO_IRQ
) {
229 host
->ops
->enable_sdio_irq(host
, 1);
236 static int sdio_card_irq_put(struct mmc_card
*card
)
238 struct mmc_host
*host
= card
->host
;
240 WARN_ON(!host
->claimed
);
242 if (host
->sdio_irqs
< 1)
245 if (!--host
->sdio_irqs
) {
246 if (!(host
->caps2
& MMC_CAP2_SDIO_IRQ_NOTHREAD
)) {
247 atomic_set(&host
->sdio_irq_thread_abort
, 1);
248 kthread_stop(host
->sdio_irq_thread
);
249 } else if (host
->caps
& MMC_CAP_SDIO_IRQ
) {
250 host
->ops
->enable_sdio_irq(host
, 0);
257 /* If there is only 1 function registered set sdio_single_irq */
258 static void sdio_single_irq_set(struct mmc_card
*card
)
260 struct sdio_func
*func
;
263 card
->sdio_single_irq
= NULL
;
264 if ((card
->host
->caps
& MMC_CAP_SDIO_IRQ
) &&
265 card
->host
->sdio_irqs
== 1)
266 for (i
= 0; i
< card
->sdio_funcs
; i
++) {
267 func
= card
->sdio_func
[i
];
268 if (func
&& func
->irq_handler
) {
269 card
->sdio_single_irq
= func
;
276 * sdio_claim_irq - claim the IRQ for a SDIO function
277 * @func: SDIO function
278 * @handler: IRQ handler callback
280 * Claim and activate the IRQ for the given SDIO function. The provided
281 * handler will be called when that IRQ is asserted. The host is always
282 * claimed already when the handler is called so the handler should not
283 * call sdio_claim_host() or sdio_release_host().
285 int sdio_claim_irq(struct sdio_func
*func
, sdio_irq_handler_t
*handler
)
293 pr_debug("SDIO: Enabling IRQ for %s...\n", sdio_func_id(func
));
295 if (func
->irq_handler
) {
296 pr_debug("SDIO: IRQ for %s already in use.\n", sdio_func_id(func
));
300 ret
= mmc_io_rw_direct(func
->card
, 0, 0, SDIO_CCCR_IENx
, 0, ®
);
304 reg
|= 1 << func
->num
;
306 reg
|= 1; /* Master interrupt enable */
308 ret
= mmc_io_rw_direct(func
->card
, 1, 0, SDIO_CCCR_IENx
, reg
, NULL
);
312 func
->irq_handler
= handler
;
313 ret
= sdio_card_irq_get(func
->card
);
315 func
->irq_handler
= NULL
;
316 sdio_single_irq_set(func
->card
);
320 EXPORT_SYMBOL_GPL(sdio_claim_irq
);
323 * sdio_release_irq - release the IRQ for a SDIO function
324 * @func: SDIO function
326 * Disable and release the IRQ for the given SDIO function.
328 int sdio_release_irq(struct sdio_func
*func
)
336 pr_debug("SDIO: Disabling IRQ for %s...\n", sdio_func_id(func
));
338 if (func
->irq_handler
) {
339 func
->irq_handler
= NULL
;
340 sdio_card_irq_put(func
->card
);
341 sdio_single_irq_set(func
->card
);
344 ret
= mmc_io_rw_direct(func
->card
, 0, 0, SDIO_CCCR_IENx
, 0, ®
);
348 reg
&= ~(1 << func
->num
);
350 /* Disable master interrupt with the last function interrupt */
354 ret
= mmc_io_rw_direct(func
->card
, 1, 0, SDIO_CCCR_IENx
, reg
, NULL
);
360 EXPORT_SYMBOL_GPL(sdio_release_irq
);