sdio_irq.c 8.5 KB

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