sdio_irq.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  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. #include <linux/kernel.h>
  14. #include <linux/sched.h>
  15. #include <linux/kthread.h>
  16. #include <linux/wait.h>
  17. #include <linux/delay.h>
  18. #include <linux/mmc/core.h>
  19. #include <linux/mmc/host.h>
  20. #include <linux/mmc/card.h>
  21. #include <linux/mmc/sdio.h>
  22. #include <linux/mmc/sdio_func.h>
  23. #include "sdio_ops.h"
  24. static int process_sdio_pending_irqs(struct mmc_card *card)
  25. {
  26. int i, ret;
  27. unsigned char pending;
  28. ret = mmc_io_rw_direct(card, 0, 0, SDIO_CCCR_INTx, 0, &pending);
  29. if (ret) {
  30. printk(KERN_DEBUG "%s: error %d reading SDIO_CCCR_INTx\n",
  31. mmc_card_id(card), ret);
  32. return ret;
  33. }
  34. for (i = 1; i <= 7; i++) {
  35. if (pending & (1 << i)) {
  36. struct sdio_func *func = card->sdio_func[i - 1];
  37. if (!func) {
  38. printk(KERN_WARNING "%s: pending IRQ for "
  39. "non-existant function\n",
  40. sdio_func_id(func));
  41. } else if (func->irq_handler) {
  42. func->irq_handler(func);
  43. } else
  44. printk(KERN_WARNING "%s: pending IRQ with no handler\n",
  45. sdio_func_id(func));
  46. }
  47. }
  48. return 0;
  49. }
  50. static int sdio_irq_thread(void *_host)
  51. {
  52. struct mmc_host *host = _host;
  53. struct sched_param param = { .sched_priority = 1 };
  54. unsigned long period;
  55. int ret;
  56. sched_setscheduler(current, SCHED_FIFO, &param);
  57. /*
  58. * We want to allow for SDIO cards to work even on non SDIO
  59. * aware hosts. One thing that non SDIO host cannot do is
  60. * asynchronous notification of pending SDIO card interrupts
  61. * hence we poll for them in that case.
  62. */
  63. period = (host->caps & MMC_CAP_SDIO_IRQ) ?
  64. MAX_SCHEDULE_TIMEOUT : msecs_to_jiffies(10);
  65. pr_debug("%s: IRQ thread started (poll period = %lu jiffies)\n",
  66. mmc_hostname(host), period);
  67. do {
  68. /*
  69. * We claim the host here on drivers behalf for a couple
  70. * reasons:
  71. *
  72. * 1) it is already needed to retrieve the CCCR_INTx;
  73. * 2) we want the driver(s) to clear the IRQ condition ASAP;
  74. * 3) we need to control the abort condition locally.
  75. *
  76. * Just like traditional hard IRQ handlers, we expect SDIO
  77. * IRQ handlers to be quick and to the point, so that the
  78. * holding of the host lock does not cover too much work
  79. * that doesn't require that lock to be held.
  80. */
  81. ret = __mmc_claim_host(host, &host->sdio_irq_thread_abort);
  82. if (ret)
  83. break;
  84. ret = process_sdio_pending_irqs(host->card);
  85. mmc_release_host(host);
  86. /*
  87. * Give other threads a chance to run in the presence of
  88. * errors. FIXME: determine if due to card removal and
  89. * possibly exit this thread if so.
  90. */
  91. if (ret)
  92. ssleep(1);
  93. set_task_state(current, TASK_INTERRUPTIBLE);
  94. if (host->caps & MMC_CAP_SDIO_IRQ)
  95. host->ops->enable_sdio_irq(host, 1);
  96. if (!kthread_should_stop())
  97. schedule_timeout(period);
  98. set_task_state(current, TASK_RUNNING);
  99. } while (!kthread_should_stop());
  100. if (host->caps & MMC_CAP_SDIO_IRQ)
  101. host->ops->enable_sdio_irq(host, 0);
  102. pr_debug("%s: IRQ thread exiting with code %d\n",
  103. mmc_hostname(host), ret);
  104. return ret;
  105. }
  106. static int sdio_card_irq_get(struct mmc_card *card)
  107. {
  108. struct mmc_host *host = card->host;
  109. WARN_ON(!host->claimed);
  110. if (!host->sdio_irqs++) {
  111. atomic_set(&host->sdio_irq_thread_abort, 0);
  112. host->sdio_irq_thread =
  113. kthread_run(sdio_irq_thread, host, "ksdiorqd");
  114. if (IS_ERR(host->sdio_irq_thread)) {
  115. int err = PTR_ERR(host->sdio_irq_thread);
  116. host->sdio_irqs--;
  117. return err;
  118. }
  119. }
  120. return 0;
  121. }
  122. static int sdio_card_irq_put(struct mmc_card *card)
  123. {
  124. struct mmc_host *host = card->host;
  125. WARN_ON(!host->claimed);
  126. BUG_ON(host->sdio_irqs < 1);
  127. if (!--host->sdio_irqs) {
  128. atomic_set(&host->sdio_irq_thread_abort, 1);
  129. kthread_stop(host->sdio_irq_thread);
  130. }
  131. return 0;
  132. }
  133. /**
  134. * sdio_claim_irq - claim the IRQ for a SDIO function
  135. * @func: SDIO function
  136. * @handler: IRQ handler callback
  137. *
  138. * Claim and activate the IRQ for the given SDIO function. The provided
  139. * handler will be called when that IRQ is asserted. The host is always
  140. * claimed already when the handler is called so the handler must not
  141. * call sdio_claim_host() nor sdio_release_host().
  142. */
  143. int sdio_claim_irq(struct sdio_func *func, sdio_irq_handler_t *handler)
  144. {
  145. int ret;
  146. unsigned char reg;
  147. BUG_ON(!func);
  148. BUG_ON(!func->card);
  149. pr_debug("SDIO: Enabling IRQ for %s...\n", sdio_func_id(func));
  150. if (func->irq_handler) {
  151. pr_debug("SDIO: IRQ for %s already in use.\n", sdio_func_id(func));
  152. return -EBUSY;
  153. }
  154. ret = mmc_io_rw_direct(func->card, 0, 0, SDIO_CCCR_IENx, 0, &reg);
  155. if (ret)
  156. return ret;
  157. reg |= 1 << func->num;
  158. reg |= 1; /* Master interrupt enable */
  159. ret = mmc_io_rw_direct(func->card, 1, 0, SDIO_CCCR_IENx, reg, NULL);
  160. if (ret)
  161. return ret;
  162. func->irq_handler = handler;
  163. ret = sdio_card_irq_get(func->card);
  164. if (ret)
  165. func->irq_handler = NULL;
  166. return ret;
  167. }
  168. EXPORT_SYMBOL_GPL(sdio_claim_irq);
  169. /**
  170. * sdio_release_irq - release the IRQ for a SDIO function
  171. * @func: SDIO function
  172. *
  173. * Disable and release the IRQ for the given SDIO function.
  174. */
  175. int sdio_release_irq(struct sdio_func *func)
  176. {
  177. int ret;
  178. unsigned char reg;
  179. BUG_ON(!func);
  180. BUG_ON(!func->card);
  181. pr_debug("SDIO: Disabling IRQ for %s...\n", sdio_func_id(func));
  182. if (func->irq_handler) {
  183. func->irq_handler = NULL;
  184. sdio_card_irq_put(func->card);
  185. }
  186. ret = mmc_io_rw_direct(func->card, 0, 0, SDIO_CCCR_IENx, 0, &reg);
  187. if (ret)
  188. return ret;
  189. reg &= ~(1 << func->num);
  190. /* Disable master interrupt with the last function interrupt */
  191. if (!(reg & 0xFE))
  192. reg = 0;
  193. ret = mmc_io_rw_direct(func->card, 1, 0, SDIO_CCCR_IENx, reg, NULL);
  194. if (ret)
  195. return ret;
  196. return 0;
  197. }
  198. EXPORT_SYMBOL_GPL(sdio_release_irq);