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, &host->sdio_irq_thread_abort);
  145. if (ret)
  146. break;
  147. ret = process_sdio_pending_irqs(host);
  148. mmc_release_host(host);
  149. /*
  150. * Give other threads a chance to run in the presence of
  151. * errors.
  152. */
  153. if (ret < 0) {
  154. set_current_state(TASK_INTERRUPTIBLE);
  155. if (!kthread_should_stop())
  156. schedule_timeout(HZ);
  157. set_current_state(TASK_RUNNING);
  158. }
  159. /*
  160. * Adaptive polling frequency based on the assumption
  161. * that an interrupt will be closely followed by more.
  162. * This has a substantial benefit for network devices.
  163. */
  164. if (!(host->caps & MMC_CAP_SDIO_IRQ)) {
  165. if (ret > 0)
  166. period /= 2;
  167. else {
  168. period++;
  169. if (period > idle_period)
  170. period = idle_period;
  171. }
  172. }
  173. set_current_state(TASK_INTERRUPTIBLE);
  174. if (host->caps & MMC_CAP_SDIO_IRQ)
  175. host->ops->enable_sdio_irq(host, 1);
  176. if (!kthread_should_stop())
  177. schedule_timeout(period);
  178. set_current_state(TASK_RUNNING);
  179. } while (!kthread_should_stop());
  180. if (host->caps & MMC_CAP_SDIO_IRQ)
  181. host->ops->enable_sdio_irq(host, 0);
  182. pr_debug("%s: IRQ thread exiting with code %d\n",
  183. mmc_hostname(host), ret);
  184. return ret;
  185. }
  186. static int sdio_card_irq_get(struct mmc_card *card)
  187. {
  188. struct mmc_host *host = card->host;
  189. WARN_ON(!host->claimed);
  190. if (!host->sdio_irqs++) {
  191. if (!(host->caps2 & MMC_CAP2_SDIO_IRQ_NOTHREAD)) {
  192. atomic_set(&host->sdio_irq_thread_abort, 0);
  193. host->sdio_irq_thread =
  194. kthread_run(sdio_irq_thread, host,
  195. "ksdioirqd/%s", mmc_hostname(host));
  196. if (IS_ERR(host->sdio_irq_thread)) {
  197. int err = PTR_ERR(host->sdio_irq_thread);
  198. host->sdio_irqs--;
  199. return err;
  200. }
  201. } else if (host->caps & MMC_CAP_SDIO_IRQ) {
  202. host->ops->enable_sdio_irq(host, 1);
  203. }
  204. }
  205. return 0;
  206. }
  207. static int sdio_card_irq_put(struct mmc_card *card)
  208. {
  209. struct mmc_host *host = card->host;
  210. WARN_ON(!host->claimed);
  211. if (host->sdio_irqs < 1)
  212. return -EINVAL;
  213. if (!--host->sdio_irqs) {
  214. if (!(host->caps2 & MMC_CAP2_SDIO_IRQ_NOTHREAD)) {
  215. atomic_set(&host->sdio_irq_thread_abort, 1);
  216. kthread_stop(host->sdio_irq_thread);
  217. } else if (host->caps & MMC_CAP_SDIO_IRQ) {
  218. host->ops->enable_sdio_irq(host, 0);
  219. }
  220. }
  221. return 0;
  222. }
  223. /* If there is only 1 function registered set sdio_single_irq */
  224. static void sdio_single_irq_set(struct mmc_card *card)
  225. {
  226. struct sdio_func *func;
  227. int i;
  228. card->sdio_single_irq = NULL;
  229. if ((card->host->caps & MMC_CAP_SDIO_IRQ) &&
  230. card->host->sdio_irqs == 1)
  231. for (i = 0; i < card->sdio_funcs; i++) {
  232. func = card->sdio_func[i];
  233. if (func && func->irq_handler) {
  234. card->sdio_single_irq = func;
  235. break;
  236. }
  237. }
  238. }
  239. /**
  240. * sdio_claim_irq - claim the IRQ for a SDIO function
  241. * @func: SDIO function
  242. * @handler: IRQ handler callback
  243. *
  244. * Claim and activate the IRQ for the given SDIO function. The provided
  245. * handler will be called when that IRQ is asserted. The host is always
  246. * claimed already when the handler is called so the handler must not
  247. * call sdio_claim_host() nor sdio_release_host().
  248. */
  249. int sdio_claim_irq(struct sdio_func *func, sdio_irq_handler_t *handler)
  250. {
  251. int ret;
  252. unsigned char reg;
  253. if (!func)
  254. return -EINVAL;
  255. pr_debug("SDIO: Enabling IRQ for %s...\n", sdio_func_id(func));
  256. if (func->irq_handler) {
  257. pr_debug("SDIO: IRQ for %s already in use.\n", sdio_func_id(func));
  258. return -EBUSY;
  259. }
  260. ret = mmc_io_rw_direct(func->card, 0, 0, SDIO_CCCR_IENx, 0, &reg);
  261. if (ret)
  262. return ret;
  263. reg |= 1 << func->num;
  264. reg |= 1; /* Master interrupt enable */
  265. ret = mmc_io_rw_direct(func->card, 1, 0, SDIO_CCCR_IENx, reg, NULL);
  266. if (ret)
  267. return ret;
  268. func->irq_handler = handler;
  269. ret = sdio_card_irq_get(func->card);
  270. if (ret)
  271. func->irq_handler = NULL;
  272. sdio_single_irq_set(func->card);
  273. return ret;
  274. }
  275. EXPORT_SYMBOL_GPL(sdio_claim_irq);
  276. /**
  277. * sdio_release_irq - release the IRQ for a SDIO function
  278. * @func: SDIO function
  279. *
  280. * Disable and release the IRQ for the given SDIO function.
  281. */
  282. int sdio_release_irq(struct sdio_func *func)
  283. {
  284. int ret;
  285. unsigned char reg;
  286. if (!func)
  287. return -EINVAL;
  288. pr_debug("SDIO: Disabling IRQ for %s...\n", sdio_func_id(func));
  289. if (func->irq_handler) {
  290. func->irq_handler = NULL;
  291. sdio_card_irq_put(func->card);
  292. sdio_single_irq_set(func->card);
  293. }
  294. ret = mmc_io_rw_direct(func->card, 0, 0, SDIO_CCCR_IENx, 0, &reg);
  295. if (ret)
  296. return ret;
  297. reg &= ~(1 << func->num);
  298. /* Disable master interrupt with the last function interrupt */
  299. if (!(reg & 0xFE))
  300. reg = 0;
  301. ret = mmc_io_rw_direct(func->card, 1, 0, SDIO_CCCR_IENx, reg, NULL);
  302. if (ret)
  303. return ret;
  304. return 0;
  305. }
  306. EXPORT_SYMBOL_GPL(sdio_release_irq);