sdio_irq.c 8.1 KB

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