sdio_irq.c 8.5 KB

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