sdio_irq.c 8.3 KB

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