sdio_irq.c 8.1 KB

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