xref: /openbmc/linux/drivers/mmc/core/sdio_irq.c (revision f15cbe6f1a4b4d9df59142fc8e4abb973302cf44)
1 /*
2  * linux/drivers/mmc/core/sdio_irq.c
3  *
4  * Author:      Nicolas Pitre
5  * Created:     June 18, 2007
6  * Copyright:   MontaVista Software Inc.
7  *
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.
12  */
13 
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>
19 
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>
25 
26 #include "sdio_ops.h"
27 
28 static int process_sdio_pending_irqs(struct mmc_card *card)
29 {
30 	int i, ret, count;
31 	unsigned char pending;
32 
33 	ret = mmc_io_rw_direct(card, 0, 0, SDIO_CCCR_INTx, 0, &pending);
34 	if (ret) {
35 		printk(KERN_DEBUG "%s: error %d reading SDIO_CCCR_INTx\n",
36 		       mmc_card_id(card), ret);
37 		return ret;
38 	}
39 
40 	count = 0;
41 	for (i = 1; i <= 7; i++) {
42 		if (pending & (1 << i)) {
43 			struct sdio_func *func = card->sdio_func[i - 1];
44 			if (!func) {
45 				printk(KERN_WARNING "%s: pending IRQ for "
46 					"non-existant function\n",
47 					mmc_card_id(card));
48 				ret = -EINVAL;
49 			} else if (func->irq_handler) {
50 				func->irq_handler(func);
51 				count++;
52 			} else {
53 				printk(KERN_WARNING "%s: pending IRQ with no handler\n",
54 				       sdio_func_id(func));
55 				ret = -EINVAL;
56 			}
57 		}
58 	}
59 
60 	if (count)
61 		return count;
62 
63 	return ret;
64 }
65 
66 static int sdio_irq_thread(void *_host)
67 {
68 	struct mmc_host *host = _host;
69 	struct sched_param param = { .sched_priority = 1 };
70 	unsigned long period, idle_period;
71 	int ret;
72 
73 	sched_setscheduler(current, SCHED_FIFO, &param);
74 
75 	/*
76 	 * We want to allow for SDIO cards to work even on non SDIO
77 	 * aware hosts.  One thing that non SDIO host cannot do is
78 	 * asynchronous notification of pending SDIO card interrupts
79 	 * hence we poll for them in that case.
80 	 */
81 	idle_period = msecs_to_jiffies(10);
82 	period = (host->caps & MMC_CAP_SDIO_IRQ) ?
83 		MAX_SCHEDULE_TIMEOUT : idle_period;
84 
85 	pr_debug("%s: IRQ thread started (poll period = %lu jiffies)\n",
86 		 mmc_hostname(host), period);
87 
88 	do {
89 		/*
90 		 * We claim the host here on drivers behalf for a couple
91 		 * reasons:
92 		 *
93 		 * 1) it is already needed to retrieve the CCCR_INTx;
94 		 * 2) we want the driver(s) to clear the IRQ condition ASAP;
95 		 * 3) we need to control the abort condition locally.
96 		 *
97 		 * Just like traditional hard IRQ handlers, we expect SDIO
98 		 * IRQ handlers to be quick and to the point, so that the
99 		 * holding of the host lock does not cover too much work
100 		 * that doesn't require that lock to be held.
101 		 */
102 		ret = __mmc_claim_host(host, &host->sdio_irq_thread_abort);
103 		if (ret)
104 			break;
105 		ret = process_sdio_pending_irqs(host->card);
106 		mmc_release_host(host);
107 
108 		/*
109 		 * Give other threads a chance to run in the presence of
110 		 * errors.  FIXME: determine if due to card removal and
111 		 * possibly exit this thread if so.
112 		 */
113 		if (ret < 0)
114 			ssleep(1);
115 
116 		/*
117 		 * Adaptive polling frequency based on the assumption
118 		 * that an interrupt will be closely followed by more.
119 		 * This has a substantial benefit for network devices.
120 		 */
121 		if (!(host->caps & MMC_CAP_SDIO_IRQ)) {
122 			if (ret > 0)
123 				period /= 2;
124 			else {
125 				period++;
126 				if (period > idle_period)
127 					period = idle_period;
128 			}
129 		}
130 
131 		set_current_state(TASK_INTERRUPTIBLE);
132 		if (host->caps & MMC_CAP_SDIO_IRQ)
133 			host->ops->enable_sdio_irq(host, 1);
134 		if (!kthread_should_stop())
135 			schedule_timeout(period);
136 		set_current_state(TASK_RUNNING);
137 	} while (!kthread_should_stop());
138 
139 	if (host->caps & MMC_CAP_SDIO_IRQ)
140 		host->ops->enable_sdio_irq(host, 0);
141 
142 	pr_debug("%s: IRQ thread exiting with code %d\n",
143 		 mmc_hostname(host), ret);
144 
145 	return ret;
146 }
147 
148 static int sdio_card_irq_get(struct mmc_card *card)
149 {
150 	struct mmc_host *host = card->host;
151 
152 	WARN_ON(!host->claimed);
153 
154 	if (!host->sdio_irqs++) {
155 		atomic_set(&host->sdio_irq_thread_abort, 0);
156 		host->sdio_irq_thread =
157 			kthread_run(sdio_irq_thread, host, "ksdiorqd");
158 		if (IS_ERR(host->sdio_irq_thread)) {
159 			int err = PTR_ERR(host->sdio_irq_thread);
160 			host->sdio_irqs--;
161 			return err;
162 		}
163 	}
164 
165 	return 0;
166 }
167 
168 static int sdio_card_irq_put(struct mmc_card *card)
169 {
170 	struct mmc_host *host = card->host;
171 
172 	WARN_ON(!host->claimed);
173 	BUG_ON(host->sdio_irqs < 1);
174 
175 	if (!--host->sdio_irqs) {
176 		atomic_set(&host->sdio_irq_thread_abort, 1);
177 		kthread_stop(host->sdio_irq_thread);
178 	}
179 
180 	return 0;
181 }
182 
183 /**
184  *	sdio_claim_irq - claim the IRQ for a SDIO function
185  *	@func: SDIO function
186  *	@handler: IRQ handler callback
187  *
188  *	Claim and activate the IRQ for the given SDIO function. The provided
189  *	handler will be called when that IRQ is asserted.  The host is always
190  *	claimed already when the handler is called so the handler must not
191  *	call sdio_claim_host() nor sdio_release_host().
192  */
193 int sdio_claim_irq(struct sdio_func *func, sdio_irq_handler_t *handler)
194 {
195 	int ret;
196 	unsigned char reg;
197 
198 	BUG_ON(!func);
199 	BUG_ON(!func->card);
200 
201 	pr_debug("SDIO: Enabling IRQ for %s...\n", sdio_func_id(func));
202 
203 	if (func->irq_handler) {
204 		pr_debug("SDIO: IRQ for %s already in use.\n", sdio_func_id(func));
205 		return -EBUSY;
206 	}
207 
208 	ret = mmc_io_rw_direct(func->card, 0, 0, SDIO_CCCR_IENx, 0, &reg);
209 	if (ret)
210 		return ret;
211 
212 	reg |= 1 << func->num;
213 
214 	reg |= 1; /* Master interrupt enable */
215 
216 	ret = mmc_io_rw_direct(func->card, 1, 0, SDIO_CCCR_IENx, reg, NULL);
217 	if (ret)
218 		return ret;
219 
220 	func->irq_handler = handler;
221 	ret = sdio_card_irq_get(func->card);
222 	if (ret)
223 		func->irq_handler = NULL;
224 
225 	return ret;
226 }
227 EXPORT_SYMBOL_GPL(sdio_claim_irq);
228 
229 /**
230  *	sdio_release_irq - release the IRQ for a SDIO function
231  *	@func: SDIO function
232  *
233  *	Disable and release the IRQ for the given SDIO function.
234  */
235 int sdio_release_irq(struct sdio_func *func)
236 {
237 	int ret;
238 	unsigned char reg;
239 
240 	BUG_ON(!func);
241 	BUG_ON(!func->card);
242 
243 	pr_debug("SDIO: Disabling IRQ for %s...\n", sdio_func_id(func));
244 
245 	if (func->irq_handler) {
246 		func->irq_handler = NULL;
247 		sdio_card_irq_put(func->card);
248 	}
249 
250 	ret = mmc_io_rw_direct(func->card, 0, 0, SDIO_CCCR_IENx, 0, &reg);
251 	if (ret)
252 		return ret;
253 
254 	reg &= ~(1 << func->num);
255 
256 	/* Disable master interrupt with the last function interrupt */
257 	if (!(reg & 0xFE))
258 		reg = 0;
259 
260 	ret = mmc_io_rw_direct(func->card, 1, 0, SDIO_CCCR_IENx, reg, NULL);
261 	if (ret)
262 		return ret;
263 
264 	return 0;
265 }
266 EXPORT_SYMBOL_GPL(sdio_release_irq);
267 
268