sdio_irq.c 7.2 KB

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