sdio_irq.c 8.7 KB

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