sdio_irq.c 6.2 KB

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