sdio_irq.c 7.4 KB

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