sdio_irq.c 7.7 KB

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