]> www.pilppa.org Git - linux-2.6-omap-h63xx.git/blob - drivers/mmc/core/sdio_irq.c
sdio: add interface for host side SDIO interrupt reporting
[linux-2.6-omap-h63xx.git] / drivers / mmc / core / sdio_irq.c
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;
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         for (i = 1; i <= 7; i++) {
41                 if (pending & (1 << i)) {
42                         struct sdio_func *func = card->sdio_func[i - 1];
43                         if (!func) {
44                                 printk(KERN_WARNING "%s: pending IRQ for "
45                                         "non-existant function\n",
46                                         sdio_func_id(func));
47                         } else if (func->irq_handler) {
48                                 func->irq_handler(func);
49                         } else
50                                 printk(KERN_WARNING "%s: pending IRQ with no handler\n",
51                                        sdio_func_id(func));
52                 }
53         }
54
55         return 0;
56 }
57
58 static int sdio_irq_thread(void *_host)
59 {
60         struct mmc_host *host = _host;
61         struct sched_param param = { .sched_priority = 1 };
62         unsigned long period;
63         int ret;
64
65         sched_setscheduler(current, SCHED_FIFO, &param);
66
67         /*
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.
72          */
73         period = (host->caps & MMC_CAP_SDIO_IRQ) ?
74                 MAX_SCHEDULE_TIMEOUT : msecs_to_jiffies(10);
75
76         pr_debug("%s: IRQ thread started (poll period = %lu jiffies)\n",
77                  mmc_hostname(host), period);
78
79         do {
80                 /*
81                  * We claim the host here on drivers behalf for a couple
82                  * reasons:
83                  *
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.
87                  *
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.
92                  */
93                 ret = __mmc_claim_host(host, &host->sdio_irq_thread_abort);
94                 if (ret)
95                         break;
96                 ret = process_sdio_pending_irqs(host->card);
97                 mmc_release_host(host);
98
99                 /*
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.
103                  */
104                 if (ret)
105                         ssleep(1);
106
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());
114
115         if (host->caps & MMC_CAP_SDIO_IRQ)
116                 host->ops->enable_sdio_irq(host, 0);
117
118         pr_debug("%s: IRQ thread exiting with code %d\n",
119                  mmc_hostname(host), ret);
120
121         return ret;
122 }
123
124 static int sdio_card_irq_get(struct mmc_card *card)
125 {
126         struct mmc_host *host = card->host;
127
128         BUG_ON(!host->claimed);
129
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);
136                         host->sdio_irqs--;
137                         return err;
138                 }
139         }
140
141         return 0;
142 }
143
144 static int sdio_card_irq_put(struct mmc_card *card)
145 {
146         struct mmc_host *host = card->host;
147
148         BUG_ON(!host->claimed);
149         BUG_ON(host->sdio_irqs < 1);
150
151         if (!--host->sdio_irqs) {
152                 atomic_set(&host->sdio_irq_thread_abort, 1);
153                 kthread_stop(host->sdio_irq_thread);
154         }
155
156         return 0;
157 }
158
159 /**
160  *      sdio_claim_irq - claim the IRQ for a SDIO function
161  *      @func: SDIO function
162  *      @handler: IRQ handler callback
163  *
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().
168  */
169 int sdio_claim_irq(struct sdio_func *func, sdio_irq_handler_t *handler)
170 {
171         int ret;
172         unsigned char reg;
173
174         BUG_ON(!func);
175         BUG_ON(!func->card);
176
177         pr_debug("SDIO: Enabling IRQ for %s...\n", sdio_func_id(func));
178
179         if (func->irq_handler) {
180                 pr_debug("SDIO: IRQ for %s already in use.\n", sdio_func_id(func));
181                 return -EBUSY;
182         }
183
184         ret = mmc_io_rw_direct(func->card, 0, 0, SDIO_CCCR_IENx, 0, &reg);
185         if (ret)
186                 return ret;
187
188         reg |= 1 << func->num;
189
190         reg |= 1; /* Master interrupt enable */
191
192         ret = mmc_io_rw_direct(func->card, 1, 0, SDIO_CCCR_IENx, reg, NULL);
193         if (ret)
194                 return ret;
195
196         func->irq_handler = handler;
197         ret = sdio_card_irq_get(func->card);
198         if (ret)
199                 func->irq_handler = NULL;
200
201         return ret;
202 }
203 EXPORT_SYMBOL_GPL(sdio_claim_irq);
204
205 /**
206  *      sdio_release_irq - release the IRQ for a SDIO function
207  *      @func: SDIO function
208  *
209  *      Disable and release the IRQ for the given SDIO function.
210  */
211 int sdio_release_irq(struct sdio_func *func)
212 {
213         int ret;
214         unsigned char reg;
215
216         BUG_ON(!func);
217         BUG_ON(!func->card);
218
219         pr_debug("SDIO: Disabling IRQ for %s...\n", sdio_func_id(func));
220
221         if (func->irq_handler) {
222                 func->irq_handler = NULL;
223                 sdio_card_irq_put(func->card);
224         }
225
226         ret = mmc_io_rw_direct(func->card, 0, 0, SDIO_CCCR_IENx, 0, &reg);
227         if (ret)
228                 return ret;
229
230         reg &= ~(1 << func->num);
231
232         /* Disable master interrupt with the last function interrupt */
233         if (!(reg & 0xFE))
234                 reg = 0;
235
236         ret = mmc_io_rw_direct(func->card, 1, 0, SDIO_CCCR_IENx, reg, NULL);
237         if (ret)
238                 return ret;
239
240         return 0;
241 }
242 EXPORT_SYMBOL_GPL(sdio_release_irq);
243