core_intr.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492
  1. /*
  2. * core_intr.c - DesignWare HS OTG Controller common interrupt handling
  3. *
  4. * Copyright (C) 2004-2013 Synopsys, Inc.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions
  8. * are met:
  9. * 1. Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions, and the following disclaimer,
  11. * without modification.
  12. * 2. Redistributions in binary form must reproduce the above copyright
  13. * notice, this list of conditions and the following disclaimer in the
  14. * documentation and/or other materials provided with the distribution.
  15. * 3. The names of the above-listed copyright holders may not be used
  16. * to endorse or promote products derived from this software without
  17. * specific prior written permission.
  18. *
  19. * ALTERNATIVELY, this software may be distributed under the terms of the
  20. * GNU General Public License ("GPL") as published by the Free Software
  21. * Foundation; either version 2 of the License, or (at your option) any
  22. * later version.
  23. *
  24. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  25. * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  26. * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  27. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  28. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  29. * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  30. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  31. * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  32. * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  33. * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  34. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  35. */
  36. /*
  37. * This file contains the common interrupt handlers
  38. */
  39. #include <linux/kernel.h>
  40. #include <linux/module.h>
  41. #include <linux/moduleparam.h>
  42. #include <linux/spinlock.h>
  43. #include <linux/interrupt.h>
  44. #include <linux/dma-mapping.h>
  45. #include <linux/io.h>
  46. #include <linux/slab.h>
  47. #include <linux/usb.h>
  48. #include <linux/usb/hcd.h>
  49. #include <linux/usb/ch11.h>
  50. #include "core.h"
  51. #include "hcd.h"
  52. static const char *dwc2_op_state_str(struct dwc2_hsotg *hsotg)
  53. {
  54. switch (hsotg->op_state) {
  55. case OTG_STATE_A_HOST:
  56. return "a_host";
  57. case OTG_STATE_A_SUSPEND:
  58. return "a_suspend";
  59. case OTG_STATE_A_PERIPHERAL:
  60. return "a_peripheral";
  61. case OTG_STATE_B_PERIPHERAL:
  62. return "b_peripheral";
  63. case OTG_STATE_B_HOST:
  64. return "b_host";
  65. default:
  66. return "unknown";
  67. }
  68. }
  69. /**
  70. * dwc2_handle_mode_mismatch_intr() - Logs a mode mismatch warning message
  71. *
  72. * @hsotg: Programming view of DWC_otg controller
  73. */
  74. static void dwc2_handle_mode_mismatch_intr(struct dwc2_hsotg *hsotg)
  75. {
  76. dev_warn(hsotg->dev, "Mode Mismatch Interrupt: currently in %s mode\n",
  77. dwc2_is_host_mode(hsotg) ? "Host" : "Device");
  78. /* Clear interrupt */
  79. writel(GINTSTS_MODEMIS, hsotg->regs + GINTSTS);
  80. }
  81. /**
  82. * dwc2_handle_otg_intr() - Handles the OTG Interrupts. It reads the OTG
  83. * Interrupt Register (GOTGINT) to determine what interrupt has occurred.
  84. *
  85. * @hsotg: Programming view of DWC_otg controller
  86. */
  87. static void dwc2_handle_otg_intr(struct dwc2_hsotg *hsotg)
  88. {
  89. u32 gotgint;
  90. u32 gotgctl;
  91. u32 gintmsk;
  92. gotgint = readl(hsotg->regs + GOTGINT);
  93. gotgctl = readl(hsotg->regs + GOTGCTL);
  94. dev_dbg(hsotg->dev, "++OTG Interrupt gotgint=%0x [%s]\n", gotgint,
  95. dwc2_op_state_str(hsotg));
  96. if (gotgint & GOTGINT_SES_END_DET) {
  97. dev_dbg(hsotg->dev,
  98. " ++OTG Interrupt: Session End Detected++ (%s)\n",
  99. dwc2_op_state_str(hsotg));
  100. gotgctl = readl(hsotg->regs + GOTGCTL);
  101. if (hsotg->op_state == OTG_STATE_B_HOST) {
  102. hsotg->op_state = OTG_STATE_B_PERIPHERAL;
  103. } else {
  104. /*
  105. * If not B_HOST and Device HNP still set, HNP did
  106. * not succeed!
  107. */
  108. if (gotgctl & GOTGCTL_DEVHNPEN) {
  109. dev_dbg(hsotg->dev, "Session End Detected\n");
  110. dev_err(hsotg->dev,
  111. "Device Not Connected/Responding!\n");
  112. }
  113. /*
  114. * If Session End Detected the B-Cable has been
  115. * disconnected
  116. */
  117. /* Reset to a clean state */
  118. hsotg->lx_state = DWC2_L0;
  119. }
  120. gotgctl = readl(hsotg->regs + GOTGCTL);
  121. gotgctl &= ~GOTGCTL_DEVHNPEN;
  122. writel(gotgctl, hsotg->regs + GOTGCTL);
  123. }
  124. if (gotgint & GOTGINT_SES_REQ_SUC_STS_CHNG) {
  125. dev_dbg(hsotg->dev,
  126. " ++OTG Interrupt: Session Request Success Status Change++\n");
  127. gotgctl = readl(hsotg->regs + GOTGCTL);
  128. if (gotgctl & GOTGCTL_SESREQSCS) {
  129. if (hsotg->core_params->phy_type ==
  130. DWC2_PHY_TYPE_PARAM_FS
  131. && hsotg->core_params->i2c_enable > 0) {
  132. hsotg->srp_success = 1;
  133. } else {
  134. /* Clear Session Request */
  135. gotgctl = readl(hsotg->regs + GOTGCTL);
  136. gotgctl &= ~GOTGCTL_SESREQ;
  137. writel(gotgctl, hsotg->regs + GOTGCTL);
  138. }
  139. }
  140. }
  141. if (gotgint & GOTGINT_HST_NEG_SUC_STS_CHNG) {
  142. /*
  143. * Print statements during the HNP interrupt handling
  144. * can cause it to fail
  145. */
  146. gotgctl = readl(hsotg->regs + GOTGCTL);
  147. /*
  148. * WA for 3.00a- HW is not setting cur_mode, even sometimes
  149. * this does not help
  150. */
  151. if (hsotg->hw_params.snpsid >= DWC2_CORE_REV_3_00a)
  152. udelay(100);
  153. if (gotgctl & GOTGCTL_HSTNEGSCS) {
  154. if (dwc2_is_host_mode(hsotg)) {
  155. hsotg->op_state = OTG_STATE_B_HOST;
  156. /*
  157. * Need to disable SOF interrupt immediately.
  158. * When switching from device to host, the PCD
  159. * interrupt handler won't handle the interrupt
  160. * if host mode is already set. The HCD
  161. * interrupt handler won't get called if the
  162. * HCD state is HALT. This means that the
  163. * interrupt does not get handled and Linux
  164. * complains loudly.
  165. */
  166. gintmsk = readl(hsotg->regs + GINTMSK);
  167. gintmsk &= ~GINTSTS_SOF;
  168. writel(gintmsk, hsotg->regs + GINTMSK);
  169. /*
  170. * Call callback function with spin lock
  171. * released
  172. */
  173. spin_unlock(&hsotg->lock);
  174. /* Initialize the Core for Host mode */
  175. dwc2_hcd_start(hsotg);
  176. spin_lock(&hsotg->lock);
  177. hsotg->op_state = OTG_STATE_B_HOST;
  178. }
  179. } else {
  180. gotgctl = readl(hsotg->regs + GOTGCTL);
  181. gotgctl &= ~(GOTGCTL_HNPREQ | GOTGCTL_DEVHNPEN);
  182. writel(gotgctl, hsotg->regs + GOTGCTL);
  183. dev_dbg(hsotg->dev, "HNP Failed\n");
  184. dev_err(hsotg->dev,
  185. "Device Not Connected/Responding\n");
  186. }
  187. }
  188. if (gotgint & GOTGINT_HST_NEG_DET) {
  189. /*
  190. * The disconnect interrupt is set at the same time as
  191. * Host Negotiation Detected. During the mode switch all
  192. * interrupts are cleared so the disconnect interrupt
  193. * handler will not get executed.
  194. */
  195. dev_dbg(hsotg->dev,
  196. " ++OTG Interrupt: Host Negotiation Detected++ (%s)\n",
  197. (dwc2_is_host_mode(hsotg) ? "Host" : "Device"));
  198. if (dwc2_is_device_mode(hsotg)) {
  199. dev_dbg(hsotg->dev, "a_suspend->a_peripheral (%d)\n",
  200. hsotg->op_state);
  201. spin_unlock(&hsotg->lock);
  202. dwc2_hcd_disconnect(hsotg);
  203. spin_lock(&hsotg->lock);
  204. hsotg->op_state = OTG_STATE_A_PERIPHERAL;
  205. } else {
  206. /* Need to disable SOF interrupt immediately */
  207. gintmsk = readl(hsotg->regs + GINTMSK);
  208. gintmsk &= ~GINTSTS_SOF;
  209. writel(gintmsk, hsotg->regs + GINTMSK);
  210. spin_unlock(&hsotg->lock);
  211. dwc2_hcd_start(hsotg);
  212. spin_lock(&hsotg->lock);
  213. hsotg->op_state = OTG_STATE_A_HOST;
  214. }
  215. }
  216. if (gotgint & GOTGINT_A_DEV_TOUT_CHG)
  217. dev_dbg(hsotg->dev,
  218. " ++OTG Interrupt: A-Device Timeout Change++\n");
  219. if (gotgint & GOTGINT_DBNCE_DONE)
  220. dev_dbg(hsotg->dev, " ++OTG Interrupt: Debounce Done++\n");
  221. /* Clear GOTGINT */
  222. writel(gotgint, hsotg->regs + GOTGINT);
  223. }
  224. /**
  225. * dwc2_handle_conn_id_status_change_intr() - Handles the Connector ID Status
  226. * Change Interrupt
  227. *
  228. * @hsotg: Programming view of DWC_otg controller
  229. *
  230. * Reads the OTG Interrupt Register (GOTCTL) to determine whether this is a
  231. * Device to Host Mode transition or a Host to Device Mode transition. This only
  232. * occurs when the cable is connected/removed from the PHY connector.
  233. */
  234. static void dwc2_handle_conn_id_status_change_intr(struct dwc2_hsotg *hsotg)
  235. {
  236. u32 gintmsk = readl(hsotg->regs + GINTMSK);
  237. /* Need to disable SOF interrupt immediately */
  238. gintmsk &= ~GINTSTS_SOF;
  239. writel(gintmsk, hsotg->regs + GINTMSK);
  240. dev_dbg(hsotg->dev, " ++Connector ID Status Change Interrupt++ (%s)\n",
  241. dwc2_is_host_mode(hsotg) ? "Host" : "Device");
  242. /*
  243. * Need to schedule a work, as there are possible DELAY function calls.
  244. * Release lock before scheduling workq as it holds spinlock during
  245. * scheduling.
  246. */
  247. spin_unlock(&hsotg->lock);
  248. queue_work(hsotg->wq_otg, &hsotg->wf_otg);
  249. spin_lock(&hsotg->lock);
  250. /* Clear interrupt */
  251. writel(GINTSTS_CONIDSTSCHNG, hsotg->regs + GINTSTS);
  252. }
  253. /**
  254. * dwc2_handle_session_req_intr() - This interrupt indicates that a device is
  255. * initiating the Session Request Protocol to request the host to turn on bus
  256. * power so a new session can begin
  257. *
  258. * @hsotg: Programming view of DWC_otg controller
  259. *
  260. * This handler responds by turning on bus power. If the DWC_otg controller is
  261. * in low power mode, this handler brings the controller out of low power mode
  262. * before turning on bus power.
  263. */
  264. static void dwc2_handle_session_req_intr(struct dwc2_hsotg *hsotg)
  265. {
  266. dev_dbg(hsotg->dev, "++Session Request Interrupt++\n");
  267. /* Clear interrupt */
  268. writel(GINTSTS_SESSREQINT, hsotg->regs + GINTSTS);
  269. }
  270. /*
  271. * This interrupt indicates that the DWC_otg controller has detected a
  272. * resume or remote wakeup sequence. If the DWC_otg controller is in
  273. * low power mode, the handler must brings the controller out of low
  274. * power mode. The controller automatically begins resume signaling.
  275. * The handler schedules a time to stop resume signaling.
  276. */
  277. static void dwc2_handle_wakeup_detected_intr(struct dwc2_hsotg *hsotg)
  278. {
  279. dev_dbg(hsotg->dev, "++Resume or Remote Wakeup Detected Interrupt++\n");
  280. dev_dbg(hsotg->dev, "%s lxstate = %d\n", __func__, hsotg->lx_state);
  281. if (dwc2_is_device_mode(hsotg)) {
  282. dev_dbg(hsotg->dev, "DSTS=0x%0x\n", readl(hsotg->regs + DSTS));
  283. if (hsotg->lx_state == DWC2_L2) {
  284. u32 dctl = readl(hsotg->regs + DCTL);
  285. /* Clear Remote Wakeup Signaling */
  286. dctl &= ~DCTL_RMTWKUPSIG;
  287. writel(dctl, hsotg->regs + DCTL);
  288. }
  289. /* Change to L0 state */
  290. hsotg->lx_state = DWC2_L0;
  291. } else {
  292. if (hsotg->lx_state != DWC2_L1) {
  293. u32 pcgcctl = readl(hsotg->regs + PCGCTL);
  294. /* Restart the Phy Clock */
  295. pcgcctl &= ~PCGCTL_STOPPCLK;
  296. writel(pcgcctl, hsotg->regs + PCGCTL);
  297. mod_timer(&hsotg->wkp_timer,
  298. jiffies + msecs_to_jiffies(71));
  299. } else {
  300. /* Change to L0 state */
  301. hsotg->lx_state = DWC2_L0;
  302. }
  303. }
  304. /* Clear interrupt */
  305. writel(GINTSTS_WKUPINT, hsotg->regs + GINTSTS);
  306. }
  307. /*
  308. * This interrupt indicates that a device has been disconnected from the
  309. * root port
  310. */
  311. static void dwc2_handle_disconnect_intr(struct dwc2_hsotg *hsotg)
  312. {
  313. dev_dbg(hsotg->dev, "++Disconnect Detected Interrupt++ (%s) %s\n",
  314. dwc2_is_host_mode(hsotg) ? "Host" : "Device",
  315. dwc2_op_state_str(hsotg));
  316. /* Change to L3 (OFF) state */
  317. hsotg->lx_state = DWC2_L3;
  318. writel(GINTSTS_DISCONNINT, hsotg->regs + GINTSTS);
  319. }
  320. /*
  321. * This interrupt indicates that SUSPEND state has been detected on the USB.
  322. *
  323. * For HNP the USB Suspend interrupt signals the change from "a_peripheral"
  324. * to "a_host".
  325. *
  326. * When power management is enabled the core will be put in low power mode.
  327. */
  328. static void dwc2_handle_usb_suspend_intr(struct dwc2_hsotg *hsotg)
  329. {
  330. u32 dsts;
  331. dev_dbg(hsotg->dev, "USB SUSPEND\n");
  332. if (dwc2_is_device_mode(hsotg)) {
  333. /*
  334. * Check the Device status register to determine if the Suspend
  335. * state is active
  336. */
  337. dsts = readl(hsotg->regs + DSTS);
  338. dev_dbg(hsotg->dev, "DSTS=0x%0x\n", dsts);
  339. dev_dbg(hsotg->dev,
  340. "DSTS.Suspend Status=%d HWCFG4.Power Optimize=%d\n",
  341. !!(dsts & DSTS_SUSPSTS),
  342. hsotg->hw_params.power_optimized);
  343. } else {
  344. if (hsotg->op_state == OTG_STATE_A_PERIPHERAL) {
  345. dev_dbg(hsotg->dev, "a_peripheral->a_host\n");
  346. /* Clear the a_peripheral flag, back to a_host */
  347. spin_unlock(&hsotg->lock);
  348. dwc2_hcd_start(hsotg);
  349. spin_lock(&hsotg->lock);
  350. hsotg->op_state = OTG_STATE_A_HOST;
  351. }
  352. }
  353. /* Change to L2 (suspend) state */
  354. hsotg->lx_state = DWC2_L2;
  355. /* Clear interrupt */
  356. writel(GINTSTS_USBSUSP, hsotg->regs + GINTSTS);
  357. }
  358. #define GINTMSK_COMMON (GINTSTS_WKUPINT | GINTSTS_SESSREQINT | \
  359. GINTSTS_CONIDSTSCHNG | GINTSTS_OTGINT | \
  360. GINTSTS_MODEMIS | GINTSTS_DISCONNINT | \
  361. GINTSTS_USBSUSP | GINTSTS_PRTINT)
  362. /*
  363. * This function returns the Core Interrupt register
  364. */
  365. static u32 dwc2_read_common_intr(struct dwc2_hsotg *hsotg)
  366. {
  367. u32 gintsts;
  368. u32 gintmsk;
  369. u32 gahbcfg;
  370. u32 gintmsk_common = GINTMSK_COMMON;
  371. gintsts = readl(hsotg->regs + GINTSTS);
  372. gintmsk = readl(hsotg->regs + GINTMSK);
  373. gahbcfg = readl(hsotg->regs + GAHBCFG);
  374. /* If any common interrupts set */
  375. if (gintsts & gintmsk_common)
  376. dev_dbg(hsotg->dev, "gintsts=%08x gintmsk=%08x\n",
  377. gintsts, gintmsk);
  378. if (gahbcfg & GAHBCFG_GLBL_INTR_EN)
  379. return gintsts & gintmsk & gintmsk_common;
  380. else
  381. return 0;
  382. }
  383. /*
  384. * Common interrupt handler
  385. *
  386. * The common interrupts are those that occur in both Host and Device mode.
  387. * This handler handles the following interrupts:
  388. * - Mode Mismatch Interrupt
  389. * - OTG Interrupt
  390. * - Connector ID Status Change Interrupt
  391. * - Disconnect Interrupt
  392. * - Session Request Interrupt
  393. * - Resume / Remote Wakeup Detected Interrupt
  394. * - Suspend Interrupt
  395. */
  396. irqreturn_t dwc2_handle_common_intr(int irq, void *dev)
  397. {
  398. struct dwc2_hsotg *hsotg = dev;
  399. u32 gintsts;
  400. irqreturn_t retval = IRQ_NONE;
  401. if (!dwc2_is_controller_alive(hsotg)) {
  402. dev_warn(hsotg->dev, "Controller is dead\n");
  403. goto out;
  404. }
  405. spin_lock(&hsotg->lock);
  406. gintsts = dwc2_read_common_intr(hsotg);
  407. if (gintsts & ~GINTSTS_PRTINT)
  408. retval = IRQ_HANDLED;
  409. if (gintsts & GINTSTS_MODEMIS)
  410. dwc2_handle_mode_mismatch_intr(hsotg);
  411. if (gintsts & GINTSTS_OTGINT)
  412. dwc2_handle_otg_intr(hsotg);
  413. if (gintsts & GINTSTS_CONIDSTSCHNG)
  414. dwc2_handle_conn_id_status_change_intr(hsotg);
  415. if (gintsts & GINTSTS_DISCONNINT)
  416. dwc2_handle_disconnect_intr(hsotg);
  417. if (gintsts & GINTSTS_SESSREQINT)
  418. dwc2_handle_session_req_intr(hsotg);
  419. if (gintsts & GINTSTS_WKUPINT)
  420. dwc2_handle_wakeup_detected_intr(hsotg);
  421. if (gintsts & GINTSTS_USBSUSP)
  422. dwc2_handle_usb_suspend_intr(hsotg);
  423. if (gintsts & GINTSTS_PRTINT) {
  424. /*
  425. * The port interrupt occurs while in device mode with HPRT0
  426. * Port Enable/Disable
  427. */
  428. if (dwc2_is_device_mode(hsotg)) {
  429. dev_dbg(hsotg->dev,
  430. " --Port interrupt received in Device mode--\n");
  431. gintsts = GINTSTS_PRTINT;
  432. writel(gintsts, hsotg->regs + GINTSTS);
  433. retval = 1;
  434. }
  435. }
  436. spin_unlock(&hsotg->lock);
  437. out:
  438. return retval;
  439. }
  440. EXPORT_SYMBOL_GPL(dwc2_handle_common_intr);