sdio_irq.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  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 "sys/param.h"
  16. #include "hal_sdhost.h"
  17. #include "sdmmc.h"
  18. #include "sdio.h"
  19. #include "sched.h"
  20. #include "_sd_define.h"
  21. #include "_core.h"
  22. #include "_sdio.h"
  23. //#define CONFIG_SDIO_IRQ_SUPPORT
  24. #ifndef portMAX_DELAY
  25. #define portMAX_DELAY 0xffffffffUL
  26. #endif
  27. #ifndef LONG_MAX
  28. #define LONG_MAX ((long)(~0UL>>1))
  29. #endif
  30. #define MAX_SCHEDULE_TIMEOUT LONG_MAX
  31. static int process_sdio_pending_irqs(struct mmc_host *host)
  32. {
  33. struct mmc_card *card = host->card;
  34. int i, ret, count;
  35. unsigned char pending;
  36. struct sdio_func *func;
  37. /*
  38. * Optimization, if there is only 1 function interrupt registered
  39. * and we know an IRQ was signaled then call irq handler directly.
  40. * Otherwise do the full probe.
  41. */
  42. #if 1
  43. func = card->sdio_single_irq;
  44. if (func && host->sdio_irq_pending) {
  45. //if (func) {
  46. //HAL_MSleep(2);
  47. #ifdef SD_PERF_TRACE_ON
  48. if(!(host->sdio_irq_count%3000))
  49. SD_LOGN("d1 wait time %lld ns\n", HAL_PR_SZ_L(host->sdio_irq_times_ns = HAL_GetTimeNs()- host->start_sdio_irq_times_ns));
  50. #endif
  51. func->irq_handler(func);
  52. return 1;
  53. }
  54. #endif
  55. ret = mmc_io_rw_direct(card, 0, 0, SDIO_CCCR_INTx, 0, &pending);
  56. if (ret) {
  57. //SD_LOGE("%s: error %d reading SDIO_CCCR_INTx\n",
  58. // mmc_card_id(card), ret);
  59. SD_LOGE("error %d reading SDIO_CCCR_INTx\n", ret);
  60. return ret;
  61. }
  62. #if 0
  63. if (pending && mmc_card_broken_irq_polling(card) &&
  64. !(host->caps & MMC_CAP_SDIO_IRQ)) {
  65. #endif
  66. if (pending &&
  67. !(host->caps & MMC_CAP_SDIO_IRQ)) {
  68. unsigned char dummy;
  69. /* A fake interrupt could be created when we poll SDIO_CCCR_INTx
  70. * register with a Marvell SD8797 card. A dummy CMD52 read to
  71. * function 0 register 0xff can avoid this.
  72. */
  73. mmc_io_rw_direct(card, 0, 0, 0xff, 0, &dummy);
  74. }
  75. count = 0;
  76. for (i = 1; i <= 7; i++) {
  77. if (pending & (1 << i)) {
  78. func = card->sdio_func[i - 1];
  79. if (!func) {
  80. /*pr_warn("%s: pending IRQ for non-existent function\n",
  81. mmc_card_id(card));*/
  82. SD_LOGW("pending IRQ for non-existent function\n");
  83. ret = -EINVAL;
  84. } else if (func->irq_handler) {
  85. func->irq_handler(func);
  86. count++;
  87. } else {
  88. /*SD_LOGW("%s: pending IRQ with no handler\n",
  89. sdio_func_id(func));*/
  90. SD_LOGW("pending IRQ with no handler\n");
  91. ret = -EINVAL;
  92. }
  93. }
  94. }
  95. if (count)
  96. return count;
  97. return ret;
  98. }
  99. void sdio_run_irqs(struct mmc_host *host)
  100. {
  101. mmc_claim_host(host);
  102. host->sdio_irq_pending = true;
  103. process_sdio_pending_irqs(host);
  104. mmc_release_host(host);
  105. }
  106. //EXPORT_SYMBOL_GPL(sdio_run_irqs);
  107. static int sdio_irq_thread(void *_host)
  108. {
  109. struct mmc_host *host = _host;
  110. struct mmc_card *card = host->card;
  111. struct sched_param param = { .sched_priority = 1 };
  112. unsigned long period, idle_period;
  113. int ret = 0;
  114. /*
  115. * We want to allow for SDIO cards to work even on non SDIO
  116. * aware hosts. One thing that non SDIO host cannot do is
  117. * asynchronous notification of pending SDIO card interrupts
  118. * hence we poll for them in that case.
  119. */
  120. idle_period = 10;
  121. period = (host->caps & MMC_CAP_SDIO_IRQ) ?
  122. 200 : idle_period;
  123. SD_LOGD("%s: IRQ thread started (poll period = %lu jiffies)\n", __func__,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. mmc_claim_host(host);
  139. ret = process_sdio_pending_irqs(host);
  140. host->sdio_irq_pending = false;
  141. mmc_release_host(host);
  142. /*
  143. * Give other threads a chance to run in the presence of
  144. * errors.
  145. */
  146. if (ret < 0) {
  147. if (!HAL_Thread_Should_Stop(host->sdio_irq_thread_stop))
  148. HAL_MSleep(1000);
  149. }
  150. /*
  151. * Adaptive polling frequency based on the assumption
  152. * that an interrupt will be closely followed by more.
  153. * This has a substantial benefit for network devices.
  154. */
  155. #if 0
  156. if (!(host->caps & MMC_CAP_SDIO_IRQ)) {
  157. if (ret > 0)
  158. period /= 2;
  159. else {
  160. period++;
  161. if (period > idle_period)
  162. period = idle_period;
  163. }
  164. }
  165. #endif
  166. //set_current_state(TASK_INTERRUPTIBLE);
  167. if (host->caps & MMC_CAP_SDIO_IRQ){
  168. HAL_SDC_Enable_Sdio_Irq(host, 1);
  169. OS_SemaphoreWait(&host->sdio_irq_signal,portMAX_DELAY);
  170. /*
  171. while(1){
  172. #define readl(addr) (*((volatile unsigned long *)(addr)))
  173. ret = OS_SemaphoreWait(&host->sdio_irq_signal,period);
  174. if(ret == OS_E_TIMEOUT){
  175. printf("Imask %x\n",readl(0x4021000+0x30));
  176. printf("rint %x\n",readl(0x4021000+0x38));
  177. }else if(ret ==OS_OK){
  178. break;
  179. }
  180. }
  181. */
  182. } else if (!HAL_Thread_Should_Stop(host->sdio_irq_thread_stop)){
  183. HAL_MSleep(period);
  184. }
  185. } while (!HAL_Thread_Should_Stop(host->sdio_irq_thread_stop));
  186. if (host->caps & MMC_CAP_SDIO_IRQ)
  187. HAL_SDC_Enable_Sdio_Irq(host, 0);
  188. SD_LOGD("IRQ thread exiting with code %d\n",ret);
  189. HAL_ThreadEnd(host->sdio_irq_thread_stop);
  190. printf("%s,%d\n",__func__,__LINE__);
  191. printf("IRQ thread exiting with code %d\n",ret);
  192. HAL_ThreadDelete();
  193. return ret;
  194. }
  195. static int irq_thread_pri = 20;
  196. void sdio_set_irq_thread_pri(int pri)
  197. {
  198. irq_thread_pri = pri;
  199. }
  200. static int sdio_card_irq_get(struct mmc_card *card)
  201. {
  202. struct mmc_host *host = card->host;
  203. // WARN_ON(!host->claimed);
  204. if (!host->sdio_irqs++) {
  205. if (!(host->caps2 & MMC_CAP2_SDIO_IRQ_NOTHREAD)) {
  206. int ret = 0;
  207. //atomic_set(&host->sdio_irq_thread_abort, 0);
  208. HAL_ATMOTIC_SET(host->sdio_irq_thread_abort, 0);
  209. //host->sdio_irq_thread =
  210. // kthread_run(sdio_irq_thread, host,
  211. // "ksdioirqd/%s", mmc_hostname(host));
  212. OS_ThreadSetInvalid(&host->sdio_irq_thread);
  213. ret = OS_ThreadCreate(&host->sdio_irq_thread, "ksdioirqd", \
  214. (void *)sdio_irq_thread, host,irq_thread_pri, 16*1024);
  215. if (ret != OS_OK) {
  216. //int err = PTR_ERR(host->sdio_irq_thread);
  217. host->sdio_irqs--;
  218. return ret;
  219. }
  220. } else if (host->caps & MMC_CAP_SDIO_IRQ) {
  221. //host->ops->enable_sdio_irq(host, 1);
  222. HAL_SDC_Enable_Sdio_Irq(host, 1);
  223. }
  224. }
  225. return 0;
  226. }
  227. static int sdio_card_irq_put(struct mmc_card *card)
  228. {
  229. struct mmc_host *host = card->host;
  230. /*WARN_ON(!host->claimed);*/
  231. SD_BUG_ON(host->sdio_irqs < 1);
  232. if (!--host->sdio_irqs) {
  233. if (!(host->caps2 & MMC_CAP2_SDIO_IRQ_NOTHREAD)) {
  234. //atomic_set(&host->sdio_irq_thread_abort, 1);
  235. HAL_ATMOTIC_SET(host->sdio_irq_thread_abort, 1);
  236. HAL_ThreadStop(host->sdio_irq_thread_stop);
  237. OS_SemaphoreRelease(&host->sdio_irq_signal);
  238. //kthread_stop(host->sdio_irq_thread);
  239. } else if (host->caps & MMC_CAP_SDIO_IRQ) {
  240. //host->ops->enable_sdio_irq(host, 0);
  241. HAL_SDC_Enable_Sdio_Irq(host, 0);
  242. }
  243. }
  244. return 0;
  245. }
  246. /* If there is only 1 function registered set sdio_single_irq */
  247. static void sdio_single_irq_set(struct mmc_card *card)
  248. {
  249. struct sdio_func *func;
  250. int i;
  251. card->sdio_single_irq = NULL;
  252. if ((card->host->caps & MMC_CAP_SDIO_IRQ) &&
  253. card->host->sdio_irqs == 1)
  254. for (i = 0; i < card->sdio_funcs; i++) {
  255. func = card->sdio_func[i];
  256. if (func && func->irq_handler) {
  257. card->sdio_single_irq = func;
  258. break;
  259. }
  260. }
  261. }
  262. /**
  263. * sdio_claim_irq - claim the IRQ for a SDIO function
  264. * @func: SDIO function
  265. * @handler: IRQ handler callback
  266. *
  267. * Claim and activate the IRQ for the given SDIO function. The provided
  268. * handler will be called when that IRQ is asserted. The host is always
  269. * claimed already when the handler is called so the handler must not
  270. * call sdio_claim_host() nor sdio_release_host().
  271. */
  272. int sdio_claim_irq(struct sdio_func *func, sdio_irq_handler_t *handler)
  273. {
  274. int ret;
  275. unsigned char reg;
  276. struct mmc_card *card = func->card;
  277. SD_BUG_ON(!func);
  278. SD_BUG_ON(!func->card);
  279. SD_LOGD("SDIO: Enabling IRQ for %ld...\n", HAL_PR_SZ_L(func->num));
  280. if (func->irq_handler) {
  281. //SD_LOGD("SDIO: IRQ for %s already in use.\n", sdio_func_id(func));
  282. SD_LOGD("SDIO: IRQ for %ld already in use.\n", HAL_PR_SZ_L(func->num));
  283. return -EBUSY;
  284. }
  285. ret = mmc_io_rw_direct(func->card, 0, 0, SDIO_CCCR_IENx, 0, &reg);
  286. if (ret)
  287. return ret;
  288. reg |= 1 << func->num;
  289. reg |= 1; /* Master interrupt enable */
  290. ret = mmc_io_rw_direct(func->card, 1, 0, SDIO_CCCR_IENx, reg, NULL);
  291. if (ret)
  292. return ret;
  293. func->irq_handler = handler;
  294. ret = sdio_card_irq_get(func->card);
  295. if (ret)
  296. func->irq_handler = NULL;
  297. sdio_single_irq_set(func->card);
  298. return ret;
  299. }
  300. //XPORT_SYMBOL_GPL(sdio_claim_irq);
  301. /**
  302. * sdio_release_irq - release the IRQ for a SDIO function
  303. * @func: SDIO function
  304. *
  305. * Disable and release the IRQ for the given SDIO function.
  306. */
  307. int sdio_release_irq(struct sdio_func *func)
  308. {
  309. int ret;
  310. unsigned char reg;
  311. struct mmc_card *card = func->card;
  312. SD_BUG_ON(!func);
  313. SD_BUG_ON(!func->card);
  314. //SDC_LOGD("SDIO: Disabling IRQ for %s...\n", sdio_func_id(func));
  315. SD_LOGD("SDIO: Disabling IRQ for %ld...\n", HAL_PR_SZ_L(func->num));
  316. if (func->irq_handler) {
  317. func->irq_handler = NULL;
  318. sdio_card_irq_put(func->card);
  319. sdio_single_irq_set(func->card);
  320. }
  321. ret = mmc_io_rw_direct(func->card, 0, 0, SDIO_CCCR_IENx, 0, &reg);
  322. if (ret)
  323. return ret;
  324. reg &= ~(1 << func->num);
  325. /* Disable master interrupt with the last function interrupt */
  326. if (!(reg & 0xFE))
  327. reg = 0;
  328. ret = mmc_io_rw_direct(func->card, 1, 0, SDIO_CCCR_IENx, reg, NULL);
  329. if (ret)
  330. return ret;
  331. return 0;
  332. }
  333. //EXPORT_SYMBOL_GPL(sdio_release_irq);