sdio_irq.c 6.3 KB

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