sdio_irq.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  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 = msecs_to_jiffies(10);
  64. pr_debug("%s: IRQ thread started (poll period = %lu jiffies)\n",
  65. mmc_hostname(host), period);
  66. do {
  67. /*
  68. * We claim the host here on drivers behalf for a couple
  69. * reasons:
  70. *
  71. * 1) it is already needed to retrieve the CCCR_INTx;
  72. * 2) we want the driver(s) to clear the IRQ condition ASAP;
  73. * 3) we need to control the abort condition locally.
  74. *
  75. * Just like traditional hard IRQ handlers, we expect SDIO
  76. * IRQ handlers to be quick and to the point, so that the
  77. * holding of the host lock does not cover too much work
  78. * that doesn't require that lock to be held.
  79. */
  80. ret = __mmc_claim_host(host, &host->sdio_irq_thread_abort);
  81. if (ret)
  82. break;
  83. ret = process_sdio_pending_irqs(host->card);
  84. mmc_release_host(host);
  85. /*
  86. * Give other threads a chance to run in the presence of
  87. * errors. FIXME: determine if due to card removal and
  88. * possibly exit this thread if so.
  89. */
  90. if (ret)
  91. ssleep(1);
  92. set_task_state(current, TASK_INTERRUPTIBLE);
  93. if (!kthread_should_stop())
  94. schedule_timeout(period);
  95. set_task_state(current, TASK_RUNNING);
  96. } while (!kthread_should_stop());
  97. pr_debug("%s: IRQ thread exiting with code %d\n",
  98. mmc_hostname(host), ret);
  99. return ret;
  100. }
  101. static int sdio_card_irq_get(struct mmc_card *card)
  102. {
  103. struct mmc_host *host = card->host;
  104. BUG_ON(!host->claimed);
  105. if (!host->sdio_irqs++) {
  106. atomic_set(&host->sdio_irq_thread_abort, 0);
  107. host->sdio_irq_thread =
  108. kthread_run(sdio_irq_thread, host, "ksdiorqd");
  109. if (IS_ERR(host->sdio_irq_thread)) {
  110. int err = PTR_ERR(host->sdio_irq_thread);
  111. host->sdio_irqs--;
  112. return err;
  113. }
  114. }
  115. return 0;
  116. }
  117. static int sdio_card_irq_put(struct mmc_card *card)
  118. {
  119. struct mmc_host *host = card->host;
  120. BUG_ON(!host->claimed);
  121. BUG_ON(host->sdio_irqs < 1);
  122. if (!--host->sdio_irqs) {
  123. atomic_set(&host->sdio_irq_thread_abort, 1);
  124. kthread_stop(host->sdio_irq_thread);
  125. }
  126. return 0;
  127. }
  128. /**
  129. * sdio_claim_irq - claim the IRQ for a SDIO function
  130. * @func: SDIO function
  131. * @handler: IRQ handler callback
  132. *
  133. * Claim and activate the IRQ for the given SDIO function. The provided
  134. * handler will be called when that IRQ is asserted. The host is always
  135. * claimed already when the handler is called so the handler must not
  136. * call sdio_claim_host() nor sdio_release_host().
  137. */
  138. int sdio_claim_irq(struct sdio_func *func, sdio_irq_handler_t *handler)
  139. {
  140. int ret;
  141. unsigned char reg;
  142. BUG_ON(!func);
  143. BUG_ON(!func->card);
  144. pr_debug("SDIO: Enabling IRQ for %s...\n", sdio_func_id(func));
  145. if (func->irq_handler) {
  146. pr_debug("SDIO: IRQ for %s already in use.\n", sdio_func_id(func));
  147. return -EBUSY;
  148. }
  149. ret = mmc_io_rw_direct(func->card, 0, 0, SDIO_CCCR_IENx, 0, &reg);
  150. if (ret)
  151. return ret;
  152. reg |= 1 << func->num;
  153. reg |= 1; /* Master interrupt enable */
  154. ret = mmc_io_rw_direct(func->card, 1, 0, SDIO_CCCR_IENx, reg, NULL);
  155. if (ret)
  156. return ret;
  157. func->irq_handler = handler;
  158. ret = sdio_card_irq_get(func->card);
  159. if (ret)
  160. func->irq_handler = NULL;
  161. return ret;
  162. }
  163. EXPORT_SYMBOL_GPL(sdio_claim_irq);
  164. /**
  165. * sdio_release_irq - release the IRQ for a SDIO function
  166. * @func: SDIO function
  167. *
  168. * Disable and release the IRQ for the given SDIO function.
  169. */
  170. int sdio_release_irq(struct sdio_func *func)
  171. {
  172. int ret;
  173. unsigned char reg;
  174. BUG_ON(!func);
  175. BUG_ON(!func->card);
  176. pr_debug("SDIO: Disabling IRQ for %s...\n", sdio_func_id(func));
  177. if (func->irq_handler) {
  178. func->irq_handler = NULL;
  179. sdio_card_irq_put(func->card);
  180. }
  181. ret = mmc_io_rw_direct(func->card, 0, 0, SDIO_CCCR_IENx, 0, &reg);
  182. if (ret)
  183. return ret;
  184. reg &= ~(1 << func->num);
  185. /* Disable master interrupt with the last function interrupt */
  186. if (!(reg & 0xFE))
  187. reg = 0;
  188. ret = mmc_io_rw_direct(func->card, 1, 0, SDIO_CCCR_IENx, reg, NULL);
  189. if (ret)
  190. return ret;
  191. return 0;
  192. }
  193. EXPORT_SYMBOL_GPL(sdio_release_irq);