sdio_irq.c 6.4 KB

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