sdio_irq.c 8.6 KB

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