interrupt.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490
  1. /*
  2. * Copyright (c) 2012 Qualcomm Atheros, Inc.
  3. *
  4. * Permission to use, copy, modify, and/or distribute this software for any
  5. * purpose with or without fee is hereby granted, provided that the above
  6. * copyright notice and this permission notice appear in all copies.
  7. *
  8. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  9. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  10. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  11. * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  12. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  13. * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  14. * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  15. */
  16. #include <linux/interrupt.h>
  17. #include "wil6210.h"
  18. /**
  19. * Theory of operation:
  20. *
  21. * There is ISR pseudo-cause register,
  22. * dma_rgf->DMA_RGF.PSEUDO_CAUSE.PSEUDO_CAUSE
  23. * Its bits represents OR'ed bits from 3 real ISR registers:
  24. * TX, RX, and MISC.
  25. *
  26. * Registers may be configured to either "write 1 to clear" or
  27. * "clear on read" mode
  28. *
  29. * When handling interrupt, one have to mask/unmask interrupts for the
  30. * real ISR registers, or hardware may malfunction.
  31. *
  32. */
  33. #define WIL6210_IRQ_DISABLE (0xFFFFFFFFUL)
  34. #define WIL6210_IMC_RX BIT_DMA_EP_RX_ICR_RX_DONE
  35. #define WIL6210_IMC_TX (BIT_DMA_EP_TX_ICR_TX_DONE | \
  36. BIT_DMA_EP_TX_ICR_TX_DONE_N(0))
  37. #define WIL6210_IMC_MISC (ISR_MISC_FW_READY | \
  38. ISR_MISC_MBOX_EVT | \
  39. ISR_MISC_FW_ERROR)
  40. #define WIL6210_IRQ_PSEUDO_MASK (u32)(~(BIT_DMA_PSEUDO_CAUSE_RX | \
  41. BIT_DMA_PSEUDO_CAUSE_TX | \
  42. BIT_DMA_PSEUDO_CAUSE_MISC))
  43. #if defined(CONFIG_WIL6210_ISR_COR)
  44. /* configure to Clear-On-Read mode */
  45. #define WIL_ICR_ICC_VALUE (0xFFFFFFFFUL)
  46. static inline void wil_icr_clear(u32 x, void __iomem *addr)
  47. {
  48. }
  49. #else /* defined(CONFIG_WIL6210_ISR_COR) */
  50. /* configure to Write-1-to-Clear mode */
  51. #define WIL_ICR_ICC_VALUE (0UL)
  52. static inline void wil_icr_clear(u32 x, void __iomem *addr)
  53. {
  54. iowrite32(x, addr);
  55. }
  56. #endif /* defined(CONFIG_WIL6210_ISR_COR) */
  57. static inline u32 wil_ioread32_and_clear(void __iomem *addr)
  58. {
  59. u32 x = ioread32(addr);
  60. wil_icr_clear(x, addr);
  61. return x;
  62. }
  63. static void wil6210_mask_irq_tx(struct wil6210_priv *wil)
  64. {
  65. iowrite32(WIL6210_IRQ_DISABLE, wil->csr +
  66. HOSTADDR(RGF_DMA_EP_TX_ICR) +
  67. offsetof(struct RGF_ICR, IMS));
  68. }
  69. static void wil6210_mask_irq_rx(struct wil6210_priv *wil)
  70. {
  71. iowrite32(WIL6210_IRQ_DISABLE, wil->csr +
  72. HOSTADDR(RGF_DMA_EP_RX_ICR) +
  73. offsetof(struct RGF_ICR, IMS));
  74. }
  75. static void wil6210_mask_irq_misc(struct wil6210_priv *wil)
  76. {
  77. iowrite32(WIL6210_IRQ_DISABLE, wil->csr +
  78. HOSTADDR(RGF_DMA_EP_MISC_ICR) +
  79. offsetof(struct RGF_ICR, IMS));
  80. }
  81. static void wil6210_mask_irq_pseudo(struct wil6210_priv *wil)
  82. {
  83. wil_dbg_irq(wil, "%s()\n", __func__);
  84. iowrite32(WIL6210_IRQ_DISABLE, wil->csr +
  85. HOSTADDR(RGF_DMA_PSEUDO_CAUSE_MASK_SW));
  86. clear_bit(wil_status_irqen, &wil->status);
  87. }
  88. static void wil6210_unmask_irq_tx(struct wil6210_priv *wil)
  89. {
  90. iowrite32(WIL6210_IMC_TX, wil->csr +
  91. HOSTADDR(RGF_DMA_EP_TX_ICR) +
  92. offsetof(struct RGF_ICR, IMC));
  93. }
  94. static void wil6210_unmask_irq_rx(struct wil6210_priv *wil)
  95. {
  96. iowrite32(WIL6210_IMC_RX, wil->csr +
  97. HOSTADDR(RGF_DMA_EP_RX_ICR) +
  98. offsetof(struct RGF_ICR, IMC));
  99. }
  100. static void wil6210_unmask_irq_misc(struct wil6210_priv *wil)
  101. {
  102. iowrite32(WIL6210_IMC_MISC, wil->csr +
  103. HOSTADDR(RGF_DMA_EP_MISC_ICR) +
  104. offsetof(struct RGF_ICR, IMC));
  105. }
  106. static void wil6210_unmask_irq_pseudo(struct wil6210_priv *wil)
  107. {
  108. wil_dbg_irq(wil, "%s()\n", __func__);
  109. set_bit(wil_status_irqen, &wil->status);
  110. iowrite32(WIL6210_IRQ_PSEUDO_MASK, wil->csr +
  111. HOSTADDR(RGF_DMA_PSEUDO_CAUSE_MASK_SW));
  112. }
  113. void wil6210_disable_irq(struct wil6210_priv *wil)
  114. {
  115. wil_dbg_irq(wil, "%s()\n", __func__);
  116. wil6210_mask_irq_tx(wil);
  117. wil6210_mask_irq_rx(wil);
  118. wil6210_mask_irq_misc(wil);
  119. wil6210_mask_irq_pseudo(wil);
  120. }
  121. void wil6210_enable_irq(struct wil6210_priv *wil)
  122. {
  123. wil_dbg_irq(wil, "%s()\n", __func__);
  124. iowrite32(WIL_ICR_ICC_VALUE, wil->csr + HOSTADDR(RGF_DMA_EP_RX_ICR) +
  125. offsetof(struct RGF_ICR, ICC));
  126. iowrite32(WIL_ICR_ICC_VALUE, wil->csr + HOSTADDR(RGF_DMA_EP_TX_ICR) +
  127. offsetof(struct RGF_ICR, ICC));
  128. iowrite32(WIL_ICR_ICC_VALUE, wil->csr + HOSTADDR(RGF_DMA_EP_MISC_ICR) +
  129. offsetof(struct RGF_ICR, ICC));
  130. wil6210_unmask_irq_pseudo(wil);
  131. wil6210_unmask_irq_tx(wil);
  132. wil6210_unmask_irq_rx(wil);
  133. wil6210_unmask_irq_misc(wil);
  134. }
  135. static irqreturn_t wil6210_irq_rx(int irq, void *cookie)
  136. {
  137. struct wil6210_priv *wil = cookie;
  138. u32 isr = wil_ioread32_and_clear(wil->csr +
  139. HOSTADDR(RGF_DMA_EP_RX_ICR) +
  140. offsetof(struct RGF_ICR, ICR));
  141. wil_dbg_irq(wil, "ISR RX 0x%08x\n", isr);
  142. if (!isr) {
  143. wil_err(wil, "spurious IRQ: RX\n");
  144. return IRQ_NONE;
  145. }
  146. wil6210_mask_irq_rx(wil);
  147. if (isr & BIT_DMA_EP_RX_ICR_RX_DONE) {
  148. wil_dbg_irq(wil, "RX done\n");
  149. isr &= ~BIT_DMA_EP_RX_ICR_RX_DONE;
  150. wil_rx_handle(wil);
  151. }
  152. if (isr)
  153. wil_err(wil, "un-handled RX ISR bits 0x%08x\n", isr);
  154. wil6210_unmask_irq_rx(wil);
  155. return IRQ_HANDLED;
  156. }
  157. static irqreturn_t wil6210_irq_tx(int irq, void *cookie)
  158. {
  159. struct wil6210_priv *wil = cookie;
  160. u32 isr = wil_ioread32_and_clear(wil->csr +
  161. HOSTADDR(RGF_DMA_EP_TX_ICR) +
  162. offsetof(struct RGF_ICR, ICR));
  163. wil_dbg_irq(wil, "ISR TX 0x%08x\n", isr);
  164. if (!isr) {
  165. wil_err(wil, "spurious IRQ: TX\n");
  166. return IRQ_NONE;
  167. }
  168. wil6210_mask_irq_tx(wil);
  169. if (isr & BIT_DMA_EP_TX_ICR_TX_DONE) {
  170. uint i;
  171. wil_dbg_irq(wil, "TX done\n");
  172. isr &= ~BIT_DMA_EP_TX_ICR_TX_DONE;
  173. for (i = 0; i < 24; i++) {
  174. u32 mask = BIT_DMA_EP_TX_ICR_TX_DONE_N(i);
  175. if (isr & mask) {
  176. isr &= ~mask;
  177. wil_dbg_irq(wil, "TX done(%i)\n", i);
  178. wil_tx_complete(wil, i);
  179. }
  180. }
  181. }
  182. if (isr)
  183. wil_err(wil, "un-handled TX ISR bits 0x%08x\n", isr);
  184. wil6210_unmask_irq_tx(wil);
  185. return IRQ_HANDLED;
  186. }
  187. static void wil_notify_fw_error(struct wil6210_priv *wil)
  188. {
  189. struct device *dev = &wil_to_ndev(wil)->dev;
  190. char *envp[3] = {
  191. [0] = "SOURCE=wil6210",
  192. [1] = "EVENT=FW_ERROR",
  193. [2] = NULL,
  194. };
  195. kobject_uevent_env(&dev->kobj, KOBJ_CHANGE, envp);
  196. }
  197. static irqreturn_t wil6210_irq_misc(int irq, void *cookie)
  198. {
  199. struct wil6210_priv *wil = cookie;
  200. u32 isr = wil_ioread32_and_clear(wil->csr +
  201. HOSTADDR(RGF_DMA_EP_MISC_ICR) +
  202. offsetof(struct RGF_ICR, ICR));
  203. wil_dbg_irq(wil, "ISR MISC 0x%08x\n", isr);
  204. if (!isr) {
  205. wil_err(wil, "spurious IRQ: MISC\n");
  206. return IRQ_NONE;
  207. }
  208. wil6210_mask_irq_misc(wil);
  209. if (isr & ISR_MISC_FW_ERROR) {
  210. wil_dbg_irq(wil, "IRQ: Firmware error\n");
  211. clear_bit(wil_status_fwready, &wil->status);
  212. wil_notify_fw_error(wil);
  213. isr &= ~ISR_MISC_FW_ERROR;
  214. }
  215. if (isr & ISR_MISC_FW_READY) {
  216. wil_dbg_irq(wil, "IRQ: FW ready\n");
  217. /**
  218. * Actual FW ready indicated by the
  219. * WMI_FW_READY_EVENTID
  220. */
  221. isr &= ~ISR_MISC_FW_READY;
  222. }
  223. wil->isr_misc = isr;
  224. if (isr) {
  225. return IRQ_WAKE_THREAD;
  226. } else {
  227. wil6210_unmask_irq_misc(wil);
  228. return IRQ_HANDLED;
  229. }
  230. }
  231. static irqreturn_t wil6210_irq_misc_thread(int irq, void *cookie)
  232. {
  233. struct wil6210_priv *wil = cookie;
  234. u32 isr = wil->isr_misc;
  235. wil_dbg_irq(wil, "Thread ISR MISC 0x%08x\n", isr);
  236. if (isr & ISR_MISC_MBOX_EVT) {
  237. wil_dbg_irq(wil, "MBOX event\n");
  238. wmi_recv_cmd(wil);
  239. isr &= ~ISR_MISC_MBOX_EVT;
  240. }
  241. if (isr)
  242. wil_err(wil, "un-handled MISC ISR bits 0x%08x\n", isr);
  243. wil->isr_misc = 0;
  244. wil6210_unmask_irq_misc(wil);
  245. return IRQ_HANDLED;
  246. }
  247. /**
  248. * thread IRQ handler
  249. */
  250. static irqreturn_t wil6210_thread_irq(int irq, void *cookie)
  251. {
  252. struct wil6210_priv *wil = cookie;
  253. wil_dbg_irq(wil, "Thread IRQ\n");
  254. /* Discover real IRQ cause */
  255. if (wil->isr_misc)
  256. wil6210_irq_misc_thread(irq, cookie);
  257. wil6210_unmask_irq_pseudo(wil);
  258. return IRQ_HANDLED;
  259. }
  260. /* DEBUG
  261. * There is subtle bug in hardware that causes IRQ to raise when it should be
  262. * masked. It is quite rare and hard to debug.
  263. *
  264. * Catch irq issue if it happens and print all I can.
  265. */
  266. static int wil6210_debug_irq_mask(struct wil6210_priv *wil, u32 pseudo_cause)
  267. {
  268. if (!test_bit(wil_status_irqen, &wil->status)) {
  269. u32 icm_rx = wil_ioread32_and_clear(wil->csr +
  270. HOSTADDR(RGF_DMA_EP_RX_ICR) +
  271. offsetof(struct RGF_ICR, ICM));
  272. u32 icr_rx = wil_ioread32_and_clear(wil->csr +
  273. HOSTADDR(RGF_DMA_EP_RX_ICR) +
  274. offsetof(struct RGF_ICR, ICR));
  275. u32 imv_rx = ioread32(wil->csr +
  276. HOSTADDR(RGF_DMA_EP_RX_ICR) +
  277. offsetof(struct RGF_ICR, IMV));
  278. u32 icm_tx = wil_ioread32_and_clear(wil->csr +
  279. HOSTADDR(RGF_DMA_EP_TX_ICR) +
  280. offsetof(struct RGF_ICR, ICM));
  281. u32 icr_tx = wil_ioread32_and_clear(wil->csr +
  282. HOSTADDR(RGF_DMA_EP_TX_ICR) +
  283. offsetof(struct RGF_ICR, ICR));
  284. u32 imv_tx = ioread32(wil->csr +
  285. HOSTADDR(RGF_DMA_EP_TX_ICR) +
  286. offsetof(struct RGF_ICR, IMV));
  287. u32 icm_misc = wil_ioread32_and_clear(wil->csr +
  288. HOSTADDR(RGF_DMA_EP_MISC_ICR) +
  289. offsetof(struct RGF_ICR, ICM));
  290. u32 icr_misc = wil_ioread32_and_clear(wil->csr +
  291. HOSTADDR(RGF_DMA_EP_MISC_ICR) +
  292. offsetof(struct RGF_ICR, ICR));
  293. u32 imv_misc = ioread32(wil->csr +
  294. HOSTADDR(RGF_DMA_EP_MISC_ICR) +
  295. offsetof(struct RGF_ICR, IMV));
  296. wil_err(wil, "IRQ when it should be masked: pseudo 0x%08x\n"
  297. "Rx icm:icr:imv 0x%08x 0x%08x 0x%08x\n"
  298. "Tx icm:icr:imv 0x%08x 0x%08x 0x%08x\n"
  299. "Misc icm:icr:imv 0x%08x 0x%08x 0x%08x\n",
  300. pseudo_cause,
  301. icm_rx, icr_rx, imv_rx,
  302. icm_tx, icr_tx, imv_tx,
  303. icm_misc, icr_misc, imv_misc);
  304. return -EINVAL;
  305. }
  306. return 0;
  307. }
  308. static irqreturn_t wil6210_hardirq(int irq, void *cookie)
  309. {
  310. irqreturn_t rc = IRQ_HANDLED;
  311. struct wil6210_priv *wil = cookie;
  312. u32 pseudo_cause = ioread32(wil->csr + HOSTADDR(RGF_DMA_PSEUDO_CAUSE));
  313. /**
  314. * pseudo_cause is Clear-On-Read, no need to ACK
  315. */
  316. if ((pseudo_cause == 0) || ((pseudo_cause & 0xff) == 0xff))
  317. return IRQ_NONE;
  318. /* FIXME: IRQ mask debug */
  319. if (wil6210_debug_irq_mask(wil, pseudo_cause))
  320. return IRQ_NONE;
  321. wil_dbg_irq(wil, "Pseudo IRQ 0x%08x\n", pseudo_cause);
  322. wil6210_mask_irq_pseudo(wil);
  323. /* Discover real IRQ cause
  324. * There are 2 possible phases for every IRQ:
  325. * - hard IRQ handler called right here
  326. * - threaded handler called later
  327. *
  328. * Hard IRQ handler reads and clears ISR.
  329. *
  330. * If threaded handler requested, hard IRQ handler
  331. * returns IRQ_WAKE_THREAD and saves ISR register value
  332. * for the threaded handler use.
  333. *
  334. * voting for wake thread - need at least 1 vote
  335. */
  336. if ((pseudo_cause & BIT_DMA_PSEUDO_CAUSE_RX) &&
  337. (wil6210_irq_rx(irq, cookie) == IRQ_WAKE_THREAD))
  338. rc = IRQ_WAKE_THREAD;
  339. if ((pseudo_cause & BIT_DMA_PSEUDO_CAUSE_TX) &&
  340. (wil6210_irq_tx(irq, cookie) == IRQ_WAKE_THREAD))
  341. rc = IRQ_WAKE_THREAD;
  342. if ((pseudo_cause & BIT_DMA_PSEUDO_CAUSE_MISC) &&
  343. (wil6210_irq_misc(irq, cookie) == IRQ_WAKE_THREAD))
  344. rc = IRQ_WAKE_THREAD;
  345. /* if thread is requested, it will unmask IRQ */
  346. if (rc != IRQ_WAKE_THREAD)
  347. wil6210_unmask_irq_pseudo(wil);
  348. return rc;
  349. }
  350. static int wil6210_request_3msi(struct wil6210_priv *wil, int irq)
  351. {
  352. int rc;
  353. /*
  354. * IRQ's are in the following order:
  355. * - Tx
  356. * - Rx
  357. * - Misc
  358. */
  359. rc = request_irq(irq, wil6210_irq_tx, IRQF_SHARED,
  360. WIL_NAME"_tx", wil);
  361. if (rc)
  362. return rc;
  363. rc = request_irq(irq + 1, wil6210_irq_rx, IRQF_SHARED,
  364. WIL_NAME"_rx", wil);
  365. if (rc)
  366. goto free0;
  367. rc = request_threaded_irq(irq + 2, wil6210_irq_misc,
  368. wil6210_irq_misc_thread,
  369. IRQF_SHARED, WIL_NAME"_misc", wil);
  370. if (rc)
  371. goto free1;
  372. return 0;
  373. /* error branch */
  374. free1:
  375. free_irq(irq + 1, wil);
  376. free0:
  377. free_irq(irq, wil);
  378. return rc;
  379. }
  380. int wil6210_init_irq(struct wil6210_priv *wil, int irq)
  381. {
  382. int rc;
  383. if (wil->n_msi == 3)
  384. rc = wil6210_request_3msi(wil, irq);
  385. else
  386. rc = request_threaded_irq(irq, wil6210_hardirq,
  387. wil6210_thread_irq,
  388. wil->n_msi ? 0 : IRQF_SHARED,
  389. WIL_NAME, wil);
  390. if (rc)
  391. return rc;
  392. wil6210_enable_irq(wil);
  393. return 0;
  394. }
  395. void wil6210_fini_irq(struct wil6210_priv *wil, int irq)
  396. {
  397. wil6210_disable_irq(wil);
  398. free_irq(irq, wil);
  399. if (wil->n_msi == 3) {
  400. free_irq(irq + 1, wil);
  401. free_irq(irq + 2, wil);
  402. }
  403. }