core_intr.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834
  1. // SPDX-License-Identifier: (GPL-2.0+ OR BSD-3-Clause)
  2. /*
  3. * core_intr.c - DesignWare HS OTG Controller common interrupt handling
  4. *
  5. * Copyright (C) 2004-2013 Synopsys, Inc.
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions
  9. * are met:
  10. * 1. Redistributions of source code must retain the above copyright
  11. * notice, this list of conditions, and the following disclaimer,
  12. * without modification.
  13. * 2. Redistributions in binary form must reproduce the above copyright
  14. * notice, this list of conditions and the following disclaimer in the
  15. * documentation and/or other materials provided with the distribution.
  16. * 3. The names of the above-listed copyright holders may not be used
  17. * to endorse or promote products derived from this software without
  18. * specific prior written permission.
  19. *
  20. * ALTERNATIVELY, this software may be distributed under the terms of the
  21. * GNU General Public License ("GPL") as published by the Free Software
  22. * Foundation; either version 2 of the License, or (at your option) any
  23. * later version.
  24. *
  25. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  26. * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  27. * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  28. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  29. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  30. * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  31. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  32. * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  33. * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  34. * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  35. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  36. */
  37. /*
  38. * This file contains the common interrupt handlers
  39. */
  40. #include <linux/kernel.h>
  41. #include <linux/module.h>
  42. #include <linux/moduleparam.h>
  43. #include <linux/spinlock.h>
  44. #include <linux/interrupt.h>
  45. #include <linux/dma-mapping.h>
  46. #include <linux/io.h>
  47. #include <linux/slab.h>
  48. #include <linux/usb.h>
  49. #include <linux/usb/hcd.h>
  50. #include <linux/usb/ch11.h>
  51. #include "core.h"
  52. #include "hcd.h"
  53. static const char *dwc2_op_state_str(struct dwc2_hsotg *hsotg)
  54. {
  55. switch (hsotg->op_state) {
  56. case OTG_STATE_A_HOST:
  57. return "a_host";
  58. case OTG_STATE_A_SUSPEND:
  59. return "a_suspend";
  60. case OTG_STATE_A_PERIPHERAL:
  61. return "a_peripheral";
  62. case OTG_STATE_B_PERIPHERAL:
  63. return "b_peripheral";
  64. case OTG_STATE_B_HOST:
  65. return "b_host";
  66. default:
  67. return "unknown";
  68. }
  69. }
  70. /**
  71. * dwc2_handle_usb_port_intr - handles OTG PRTINT interrupts.
  72. * When the PRTINT interrupt fires, there are certain status bits in the Host
  73. * Port that needs to get cleared.
  74. *
  75. * @hsotg: Programming view of DWC_otg controller
  76. */
  77. static void dwc2_handle_usb_port_intr(struct dwc2_hsotg *hsotg)
  78. {
  79. u32 hprt0 = dwc2_readl(hsotg, HPRT0);
  80. if (hprt0 & HPRT0_ENACHG) {
  81. hprt0 &= ~HPRT0_ENA;
  82. dwc2_writel(hsotg, hprt0, HPRT0);
  83. }
  84. }
  85. /**
  86. * dwc2_handle_mode_mismatch_intr() - Logs a mode mismatch warning message
  87. *
  88. * @hsotg: Programming view of DWC_otg controller
  89. */
  90. static void dwc2_handle_mode_mismatch_intr(struct dwc2_hsotg *hsotg)
  91. {
  92. /* Clear interrupt */
  93. dwc2_writel(hsotg, GINTSTS_MODEMIS, GINTSTS);
  94. dev_warn(hsotg->dev, "Mode Mismatch Interrupt: currently in %s mode\n",
  95. dwc2_is_host_mode(hsotg) ? "Host" : "Device");
  96. }
  97. /**
  98. * dwc2_handle_otg_intr() - Handles the OTG Interrupts. It reads the OTG
  99. * Interrupt Register (GOTGINT) to determine what interrupt has occurred.
  100. *
  101. * @hsotg: Programming view of DWC_otg controller
  102. */
  103. static void dwc2_handle_otg_intr(struct dwc2_hsotg *hsotg)
  104. {
  105. u32 gotgint;
  106. u32 gotgctl;
  107. u32 gintmsk;
  108. gotgint = dwc2_readl(hsotg, GOTGINT);
  109. gotgctl = dwc2_readl(hsotg, GOTGCTL);
  110. dev_dbg(hsotg->dev, "++OTG Interrupt gotgint=%0x [%s]\n", gotgint,
  111. dwc2_op_state_str(hsotg));
  112. if (gotgint & GOTGINT_SES_END_DET) {
  113. dev_dbg(hsotg->dev,
  114. " ++OTG Interrupt: Session End Detected++ (%s)\n",
  115. dwc2_op_state_str(hsotg));
  116. gotgctl = dwc2_readl(hsotg, GOTGCTL);
  117. if (dwc2_is_device_mode(hsotg))
  118. dwc2_hsotg_disconnect(hsotg);
  119. if (hsotg->op_state == OTG_STATE_B_HOST) {
  120. hsotg->op_state = OTG_STATE_B_PERIPHERAL;
  121. } else {
  122. /*
  123. * If not B_HOST and Device HNP still set, HNP did
  124. * not succeed!
  125. */
  126. if (gotgctl & GOTGCTL_DEVHNPEN) {
  127. dev_dbg(hsotg->dev, "Session End Detected\n");
  128. dev_err(hsotg->dev,
  129. "Device Not Connected/Responding!\n");
  130. }
  131. /*
  132. * If Session End Detected the B-Cable has been
  133. * disconnected
  134. */
  135. /* Reset to a clean state */
  136. hsotg->lx_state = DWC2_L0;
  137. }
  138. gotgctl = dwc2_readl(hsotg, GOTGCTL);
  139. gotgctl &= ~GOTGCTL_DEVHNPEN;
  140. dwc2_writel(hsotg, gotgctl, GOTGCTL);
  141. }
  142. if (gotgint & GOTGINT_SES_REQ_SUC_STS_CHNG) {
  143. dev_dbg(hsotg->dev,
  144. " ++OTG Interrupt: Session Request Success Status Change++\n");
  145. gotgctl = dwc2_readl(hsotg, GOTGCTL);
  146. if (gotgctl & GOTGCTL_SESREQSCS) {
  147. if (hsotg->params.phy_type == DWC2_PHY_TYPE_PARAM_FS &&
  148. hsotg->params.i2c_enable) {
  149. hsotg->srp_success = 1;
  150. } else {
  151. /* Clear Session Request */
  152. gotgctl = dwc2_readl(hsotg, GOTGCTL);
  153. gotgctl &= ~GOTGCTL_SESREQ;
  154. dwc2_writel(hsotg, gotgctl, GOTGCTL);
  155. }
  156. }
  157. }
  158. if (gotgint & GOTGINT_HST_NEG_SUC_STS_CHNG) {
  159. /*
  160. * Print statements during the HNP interrupt handling
  161. * can cause it to fail
  162. */
  163. gotgctl = dwc2_readl(hsotg, GOTGCTL);
  164. /*
  165. * WA for 3.00a- HW is not setting cur_mode, even sometimes
  166. * this does not help
  167. */
  168. if (hsotg->hw_params.snpsid >= DWC2_CORE_REV_3_00a)
  169. udelay(100);
  170. if (gotgctl & GOTGCTL_HSTNEGSCS) {
  171. if (dwc2_is_host_mode(hsotg)) {
  172. hsotg->op_state = OTG_STATE_B_HOST;
  173. /*
  174. * Need to disable SOF interrupt immediately.
  175. * When switching from device to host, the PCD
  176. * interrupt handler won't handle the interrupt
  177. * if host mode is already set. The HCD
  178. * interrupt handler won't get called if the
  179. * HCD state is HALT. This means that the
  180. * interrupt does not get handled and Linux
  181. * complains loudly.
  182. */
  183. gintmsk = dwc2_readl(hsotg, GINTMSK);
  184. gintmsk &= ~GINTSTS_SOF;
  185. dwc2_writel(hsotg, gintmsk, GINTMSK);
  186. /*
  187. * Call callback function with spin lock
  188. * released
  189. */
  190. spin_unlock(&hsotg->lock);
  191. /* Initialize the Core for Host mode */
  192. dwc2_hcd_start(hsotg);
  193. spin_lock(&hsotg->lock);
  194. hsotg->op_state = OTG_STATE_B_HOST;
  195. }
  196. } else {
  197. gotgctl = dwc2_readl(hsotg, GOTGCTL);
  198. gotgctl &= ~(GOTGCTL_HNPREQ | GOTGCTL_DEVHNPEN);
  199. dwc2_writel(hsotg, gotgctl, GOTGCTL);
  200. dev_dbg(hsotg->dev, "HNP Failed\n");
  201. dev_err(hsotg->dev,
  202. "Device Not Connected/Responding\n");
  203. }
  204. }
  205. if (gotgint & GOTGINT_HST_NEG_DET) {
  206. /*
  207. * The disconnect interrupt is set at the same time as
  208. * Host Negotiation Detected. During the mode switch all
  209. * interrupts are cleared so the disconnect interrupt
  210. * handler will not get executed.
  211. */
  212. dev_dbg(hsotg->dev,
  213. " ++OTG Interrupt: Host Negotiation Detected++ (%s)\n",
  214. (dwc2_is_host_mode(hsotg) ? "Host" : "Device"));
  215. if (dwc2_is_device_mode(hsotg)) {
  216. dev_dbg(hsotg->dev, "a_suspend->a_peripheral (%d)\n",
  217. hsotg->op_state);
  218. spin_unlock(&hsotg->lock);
  219. dwc2_hcd_disconnect(hsotg, false);
  220. spin_lock(&hsotg->lock);
  221. hsotg->op_state = OTG_STATE_A_PERIPHERAL;
  222. } else {
  223. /* Need to disable SOF interrupt immediately */
  224. gintmsk = dwc2_readl(hsotg, GINTMSK);
  225. gintmsk &= ~GINTSTS_SOF;
  226. dwc2_writel(hsotg, gintmsk, GINTMSK);
  227. spin_unlock(&hsotg->lock);
  228. dwc2_hcd_start(hsotg);
  229. spin_lock(&hsotg->lock);
  230. hsotg->op_state = OTG_STATE_A_HOST;
  231. }
  232. }
  233. if (gotgint & GOTGINT_A_DEV_TOUT_CHG)
  234. dev_dbg(hsotg->dev,
  235. " ++OTG Interrupt: A-Device Timeout Change++\n");
  236. if (gotgint & GOTGINT_DBNCE_DONE)
  237. dev_dbg(hsotg->dev, " ++OTG Interrupt: Debounce Done++\n");
  238. /* Clear GOTGINT */
  239. dwc2_writel(hsotg, gotgint, GOTGINT);
  240. }
  241. /**
  242. * dwc2_handle_conn_id_status_change_intr() - Handles the Connector ID Status
  243. * Change Interrupt
  244. *
  245. * @hsotg: Programming view of DWC_otg controller
  246. *
  247. * Reads the OTG Interrupt Register (GOTCTL) to determine whether this is a
  248. * Device to Host Mode transition or a Host to Device Mode transition. This only
  249. * occurs when the cable is connected/removed from the PHY connector.
  250. */
  251. static void dwc2_handle_conn_id_status_change_intr(struct dwc2_hsotg *hsotg)
  252. {
  253. u32 gintmsk;
  254. /* Clear interrupt */
  255. dwc2_writel(hsotg, GINTSTS_CONIDSTSCHNG, GINTSTS);
  256. /* Need to disable SOF interrupt immediately */
  257. gintmsk = dwc2_readl(hsotg, GINTMSK);
  258. gintmsk &= ~GINTSTS_SOF;
  259. dwc2_writel(hsotg, gintmsk, GINTMSK);
  260. dev_dbg(hsotg->dev, " ++Connector ID Status Change Interrupt++ (%s)\n",
  261. dwc2_is_host_mode(hsotg) ? "Host" : "Device");
  262. /*
  263. * Need to schedule a work, as there are possible DELAY function calls.
  264. * Release lock before scheduling workq as it holds spinlock during
  265. * scheduling.
  266. */
  267. if (hsotg->wq_otg) {
  268. spin_unlock(&hsotg->lock);
  269. queue_work(hsotg->wq_otg, &hsotg->wf_otg);
  270. spin_lock(&hsotg->lock);
  271. }
  272. }
  273. /**
  274. * dwc2_handle_session_req_intr() - This interrupt indicates that a device is
  275. * initiating the Session Request Protocol to request the host to turn on bus
  276. * power so a new session can begin
  277. *
  278. * @hsotg: Programming view of DWC_otg controller
  279. *
  280. * This handler responds by turning on bus power. If the DWC_otg controller is
  281. * in low power mode, this handler brings the controller out of low power mode
  282. * before turning on bus power.
  283. */
  284. static void dwc2_handle_session_req_intr(struct dwc2_hsotg *hsotg)
  285. {
  286. int ret;
  287. /* Clear interrupt */
  288. dwc2_writel(hsotg, GINTSTS_SESSREQINT, GINTSTS);
  289. dev_dbg(hsotg->dev, "Session request interrupt - lx_state=%d\n",
  290. hsotg->lx_state);
  291. if (dwc2_is_device_mode(hsotg)) {
  292. if (hsotg->lx_state == DWC2_L2) {
  293. ret = dwc2_exit_partial_power_down(hsotg, true);
  294. if (ret && (ret != -ENOTSUPP))
  295. dev_err(hsotg->dev,
  296. "exit power_down failed\n");
  297. }
  298. /*
  299. * Report disconnect if there is any previous session
  300. * established
  301. */
  302. dwc2_hsotg_disconnect(hsotg);
  303. }
  304. }
  305. /**
  306. * dwc2_wakeup_from_lpm_l1 - Exit the device from LPM L1 state
  307. *
  308. * @hsotg: Programming view of DWC_otg controller
  309. *
  310. */
  311. static void dwc2_wakeup_from_lpm_l1(struct dwc2_hsotg *hsotg)
  312. {
  313. u32 glpmcfg;
  314. u32 i = 0;
  315. if (hsotg->lx_state != DWC2_L1) {
  316. dev_err(hsotg->dev, "Core isn't in DWC2_L1 state\n");
  317. return;
  318. }
  319. glpmcfg = dwc2_readl(hsotg, GLPMCFG);
  320. if (dwc2_is_device_mode(hsotg)) {
  321. dev_dbg(hsotg->dev, "Exit from L1 state\n");
  322. glpmcfg &= ~GLPMCFG_ENBLSLPM;
  323. glpmcfg &= ~GLPMCFG_HIRD_THRES_EN;
  324. dwc2_writel(hsotg, glpmcfg, GLPMCFG);
  325. do {
  326. glpmcfg = dwc2_readl(hsotg, GLPMCFG);
  327. if (!(glpmcfg & (GLPMCFG_COREL1RES_MASK |
  328. GLPMCFG_L1RESUMEOK | GLPMCFG_SLPSTS)))
  329. break;
  330. udelay(1);
  331. } while (++i < 200);
  332. if (i == 200) {
  333. dev_err(hsotg->dev, "Failed to exit L1 sleep state in 200us.\n");
  334. return;
  335. }
  336. dwc2_gadget_init_lpm(hsotg);
  337. } else {
  338. /* TODO */
  339. dev_err(hsotg->dev, "Host side LPM is not supported.\n");
  340. return;
  341. }
  342. /* Change to L0 state */
  343. hsotg->lx_state = DWC2_L0;
  344. /* Inform gadget to exit from L1 */
  345. call_gadget(hsotg, resume);
  346. }
  347. /*
  348. * This interrupt indicates that the DWC_otg controller has detected a
  349. * resume or remote wakeup sequence. If the DWC_otg controller is in
  350. * low power mode, the handler must brings the controller out of low
  351. * power mode. The controller automatically begins resume signaling.
  352. * The handler schedules a time to stop resume signaling.
  353. */
  354. static void dwc2_handle_wakeup_detected_intr(struct dwc2_hsotg *hsotg)
  355. {
  356. int ret;
  357. /* Clear interrupt */
  358. dwc2_writel(hsotg, GINTSTS_WKUPINT, GINTSTS);
  359. dev_dbg(hsotg->dev, "++Resume or Remote Wakeup Detected Interrupt++\n");
  360. dev_dbg(hsotg->dev, "%s lxstate = %d\n", __func__, hsotg->lx_state);
  361. if (hsotg->lx_state == DWC2_L1) {
  362. dwc2_wakeup_from_lpm_l1(hsotg);
  363. return;
  364. }
  365. if (dwc2_is_device_mode(hsotg)) {
  366. dev_dbg(hsotg->dev, "DSTS=0x%0x\n",
  367. dwc2_readl(hsotg, DSTS));
  368. if (hsotg->lx_state == DWC2_L2) {
  369. u32 dctl = dwc2_readl(hsotg, DCTL);
  370. /* Clear Remote Wakeup Signaling */
  371. dctl &= ~DCTL_RMTWKUPSIG;
  372. dwc2_writel(hsotg, dctl, DCTL);
  373. ret = dwc2_exit_partial_power_down(hsotg, true);
  374. if (ret && (ret != -ENOTSUPP))
  375. dev_err(hsotg->dev, "exit power_down failed\n");
  376. call_gadget(hsotg, resume);
  377. }
  378. /* Change to L0 state */
  379. hsotg->lx_state = DWC2_L0;
  380. } else {
  381. if (hsotg->params.power_down)
  382. return;
  383. if (hsotg->lx_state != DWC2_L1) {
  384. u32 pcgcctl = dwc2_readl(hsotg, PCGCTL);
  385. /* Restart the Phy Clock */
  386. pcgcctl &= ~PCGCTL_STOPPCLK;
  387. dwc2_writel(hsotg, pcgcctl, PCGCTL);
  388. mod_timer(&hsotg->wkp_timer,
  389. jiffies + msecs_to_jiffies(71));
  390. } else {
  391. /* Change to L0 state */
  392. hsotg->lx_state = DWC2_L0;
  393. }
  394. }
  395. }
  396. /*
  397. * This interrupt indicates that a device has been disconnected from the
  398. * root port
  399. */
  400. static void dwc2_handle_disconnect_intr(struct dwc2_hsotg *hsotg)
  401. {
  402. dwc2_writel(hsotg, GINTSTS_DISCONNINT, GINTSTS);
  403. dev_dbg(hsotg->dev, "++Disconnect Detected Interrupt++ (%s) %s\n",
  404. dwc2_is_host_mode(hsotg) ? "Host" : "Device",
  405. dwc2_op_state_str(hsotg));
  406. if (hsotg->op_state == OTG_STATE_A_HOST)
  407. dwc2_hcd_disconnect(hsotg, false);
  408. }
  409. /*
  410. * This interrupt indicates that SUSPEND state has been detected on the USB.
  411. *
  412. * For HNP the USB Suspend interrupt signals the change from "a_peripheral"
  413. * to "a_host".
  414. *
  415. * When power management is enabled the core will be put in low power mode.
  416. */
  417. static void dwc2_handle_usb_suspend_intr(struct dwc2_hsotg *hsotg)
  418. {
  419. u32 dsts;
  420. int ret;
  421. /* Clear interrupt */
  422. dwc2_writel(hsotg, GINTSTS_USBSUSP, GINTSTS);
  423. dev_dbg(hsotg->dev, "USB SUSPEND\n");
  424. if (dwc2_is_device_mode(hsotg)) {
  425. /*
  426. * Check the Device status register to determine if the Suspend
  427. * state is active
  428. */
  429. dsts = dwc2_readl(hsotg, DSTS);
  430. dev_dbg(hsotg->dev, "%s: DSTS=0x%0x\n", __func__, dsts);
  431. dev_dbg(hsotg->dev,
  432. "DSTS.Suspend Status=%d HWCFG4.Power Optimize=%d HWCFG4.Hibernation=%d\n",
  433. !!(dsts & DSTS_SUSPSTS),
  434. hsotg->hw_params.power_optimized,
  435. hsotg->hw_params.hibernation);
  436. /* Ignore suspend request before enumeration */
  437. if (!dwc2_is_device_connected(hsotg)) {
  438. dev_dbg(hsotg->dev,
  439. "ignore suspend request before enumeration\n");
  440. return;
  441. }
  442. if (dsts & DSTS_SUSPSTS) {
  443. if (hsotg->hw_params.power_optimized) {
  444. ret = dwc2_enter_partial_power_down(hsotg);
  445. if (ret) {
  446. if (ret != -ENOTSUPP)
  447. dev_err(hsotg->dev,
  448. "%s: enter partial_power_down failed\n",
  449. __func__);
  450. goto skip_power_saving;
  451. }
  452. udelay(100);
  453. /* Ask phy to be suspended */
  454. if (!IS_ERR_OR_NULL(hsotg->uphy))
  455. usb_phy_set_suspend(hsotg->uphy, true);
  456. }
  457. if (hsotg->hw_params.hibernation) {
  458. ret = dwc2_enter_hibernation(hsotg, 0);
  459. if (ret && ret != -ENOTSUPP)
  460. dev_err(hsotg->dev,
  461. "%s: enter hibernation failed\n",
  462. __func__);
  463. }
  464. skip_power_saving:
  465. /*
  466. * Change to L2 (suspend) state before releasing
  467. * spinlock
  468. */
  469. hsotg->lx_state = DWC2_L2;
  470. /* Call gadget suspend callback */
  471. call_gadget(hsotg, suspend);
  472. }
  473. } else {
  474. if (hsotg->op_state == OTG_STATE_A_PERIPHERAL) {
  475. dev_dbg(hsotg->dev, "a_peripheral->a_host\n");
  476. /* Change to L2 (suspend) state */
  477. hsotg->lx_state = DWC2_L2;
  478. /* Clear the a_peripheral flag, back to a_host */
  479. spin_unlock(&hsotg->lock);
  480. dwc2_hcd_start(hsotg);
  481. spin_lock(&hsotg->lock);
  482. hsotg->op_state = OTG_STATE_A_HOST;
  483. }
  484. }
  485. }
  486. /**
  487. * dwc2_handle_lpm_intr - GINTSTS_LPMTRANRCVD Interrupt handler
  488. *
  489. * @hsotg: Programming view of DWC_otg controller
  490. *
  491. */
  492. static void dwc2_handle_lpm_intr(struct dwc2_hsotg *hsotg)
  493. {
  494. u32 glpmcfg;
  495. u32 pcgcctl;
  496. u32 hird;
  497. u32 hird_thres;
  498. u32 hird_thres_en;
  499. u32 enslpm;
  500. /* Clear interrupt */
  501. dwc2_writel(hsotg, GINTSTS_LPMTRANRCVD, GINTSTS);
  502. glpmcfg = dwc2_readl(hsotg, GLPMCFG);
  503. if (!(glpmcfg & GLPMCFG_LPMCAP)) {
  504. dev_err(hsotg->dev, "Unexpected LPM interrupt\n");
  505. return;
  506. }
  507. hird = (glpmcfg & GLPMCFG_HIRD_MASK) >> GLPMCFG_HIRD_SHIFT;
  508. hird_thres = (glpmcfg & GLPMCFG_HIRD_THRES_MASK &
  509. ~GLPMCFG_HIRD_THRES_EN) >> GLPMCFG_HIRD_THRES_SHIFT;
  510. hird_thres_en = glpmcfg & GLPMCFG_HIRD_THRES_EN;
  511. enslpm = glpmcfg & GLPMCFG_ENBLSLPM;
  512. if (dwc2_is_device_mode(hsotg)) {
  513. dev_dbg(hsotg->dev, "HIRD_THRES_EN = %d\n", hird_thres_en);
  514. if (hird_thres_en && hird >= hird_thres) {
  515. dev_dbg(hsotg->dev, "L1 with utmi_l1_suspend_n\n");
  516. } else if (enslpm) {
  517. dev_dbg(hsotg->dev, "L1 with utmi_sleep_n\n");
  518. } else {
  519. dev_dbg(hsotg->dev, "Entering Sleep with L1 Gating\n");
  520. pcgcctl = dwc2_readl(hsotg, PCGCTL);
  521. pcgcctl |= PCGCTL_ENBL_SLEEP_GATING;
  522. dwc2_writel(hsotg, pcgcctl, PCGCTL);
  523. }
  524. /**
  525. * Examine prt_sleep_sts after TL1TokenTetry period max (10 us)
  526. */
  527. udelay(10);
  528. glpmcfg = dwc2_readl(hsotg, GLPMCFG);
  529. if (glpmcfg & GLPMCFG_SLPSTS) {
  530. /* Save the current state */
  531. hsotg->lx_state = DWC2_L1;
  532. dev_dbg(hsotg->dev,
  533. "Core is in L1 sleep glpmcfg=%08x\n", glpmcfg);
  534. /* Inform gadget that we are in L1 state */
  535. call_gadget(hsotg, suspend);
  536. }
  537. }
  538. }
  539. #define GINTMSK_COMMON (GINTSTS_WKUPINT | GINTSTS_SESSREQINT | \
  540. GINTSTS_CONIDSTSCHNG | GINTSTS_OTGINT | \
  541. GINTSTS_MODEMIS | GINTSTS_DISCONNINT | \
  542. GINTSTS_USBSUSP | GINTSTS_PRTINT | \
  543. GINTSTS_LPMTRANRCVD)
  544. /*
  545. * This function returns the Core Interrupt register
  546. */
  547. static u32 dwc2_read_common_intr(struct dwc2_hsotg *hsotg)
  548. {
  549. u32 gintsts;
  550. u32 gintmsk;
  551. u32 gahbcfg;
  552. u32 gintmsk_common = GINTMSK_COMMON;
  553. gintsts = dwc2_readl(hsotg, GINTSTS);
  554. gintmsk = dwc2_readl(hsotg, GINTMSK);
  555. gahbcfg = dwc2_readl(hsotg, GAHBCFG);
  556. /* If any common interrupts set */
  557. if (gintsts & gintmsk_common)
  558. dev_dbg(hsotg->dev, "gintsts=%08x gintmsk=%08x\n",
  559. gintsts, gintmsk);
  560. if (gahbcfg & GAHBCFG_GLBL_INTR_EN)
  561. return gintsts & gintmsk & gintmsk_common;
  562. else
  563. return 0;
  564. }
  565. /*
  566. * GPWRDN interrupt handler.
  567. *
  568. * The GPWRDN interrupts are those that occur in both Host and
  569. * Device mode while core is in hibernated state.
  570. */
  571. static void dwc2_handle_gpwrdn_intr(struct dwc2_hsotg *hsotg)
  572. {
  573. u32 gpwrdn;
  574. int linestate;
  575. gpwrdn = dwc2_readl(hsotg, GPWRDN);
  576. /* clear all interrupt */
  577. dwc2_writel(hsotg, gpwrdn, GPWRDN);
  578. linestate = (gpwrdn & GPWRDN_LINESTATE_MASK) >> GPWRDN_LINESTATE_SHIFT;
  579. dev_dbg(hsotg->dev,
  580. "%s: dwc2_handle_gpwrdwn_intr called gpwrdn= %08x\n", __func__,
  581. gpwrdn);
  582. if ((gpwrdn & GPWRDN_DISCONN_DET) &&
  583. (gpwrdn & GPWRDN_DISCONN_DET_MSK) && !linestate) {
  584. u32 gpwrdn_tmp;
  585. dev_dbg(hsotg->dev, "%s: GPWRDN_DISCONN_DET\n", __func__);
  586. /* Switch-on voltage to the core */
  587. gpwrdn_tmp = dwc2_readl(hsotg, GPWRDN);
  588. gpwrdn_tmp &= ~GPWRDN_PWRDNSWTCH;
  589. dwc2_writel(hsotg, gpwrdn_tmp, GPWRDN);
  590. udelay(10);
  591. /* Reset core */
  592. gpwrdn_tmp = dwc2_readl(hsotg, GPWRDN);
  593. gpwrdn_tmp &= ~GPWRDN_PWRDNRSTN;
  594. dwc2_writel(hsotg, gpwrdn_tmp, GPWRDN);
  595. udelay(10);
  596. /* Disable Power Down Clamp */
  597. gpwrdn_tmp = dwc2_readl(hsotg, GPWRDN);
  598. gpwrdn_tmp &= ~GPWRDN_PWRDNCLMP;
  599. dwc2_writel(hsotg, gpwrdn_tmp, GPWRDN);
  600. udelay(10);
  601. /* Deassert reset core */
  602. gpwrdn_tmp = dwc2_readl(hsotg, GPWRDN);
  603. gpwrdn_tmp |= GPWRDN_PWRDNRSTN;
  604. dwc2_writel(hsotg, gpwrdn_tmp, GPWRDN);
  605. udelay(10);
  606. /* Disable PMU interrupt */
  607. gpwrdn_tmp = dwc2_readl(hsotg, GPWRDN);
  608. gpwrdn_tmp &= ~GPWRDN_PMUINTSEL;
  609. dwc2_writel(hsotg, gpwrdn_tmp, GPWRDN);
  610. /* De-assert Wakeup Logic */
  611. gpwrdn_tmp = dwc2_readl(hsotg, GPWRDN);
  612. gpwrdn_tmp &= ~GPWRDN_PMUACTV;
  613. dwc2_writel(hsotg, gpwrdn_tmp, GPWRDN);
  614. hsotg->hibernated = 0;
  615. if (gpwrdn & GPWRDN_IDSTS) {
  616. hsotg->op_state = OTG_STATE_B_PERIPHERAL;
  617. dwc2_core_init(hsotg, false);
  618. dwc2_enable_global_interrupts(hsotg);
  619. dwc2_hsotg_core_init_disconnected(hsotg, false);
  620. dwc2_hsotg_core_connect(hsotg);
  621. } else {
  622. hsotg->op_state = OTG_STATE_A_HOST;
  623. /* Initialize the Core for Host mode */
  624. dwc2_core_init(hsotg, false);
  625. dwc2_enable_global_interrupts(hsotg);
  626. dwc2_hcd_start(hsotg);
  627. }
  628. }
  629. if ((gpwrdn & GPWRDN_LNSTSCHG) &&
  630. (gpwrdn & GPWRDN_LNSTSCHG_MSK) && linestate) {
  631. dev_dbg(hsotg->dev, "%s: GPWRDN_LNSTSCHG\n", __func__);
  632. if (hsotg->hw_params.hibernation &&
  633. hsotg->hibernated) {
  634. if (gpwrdn & GPWRDN_IDSTS) {
  635. dwc2_exit_hibernation(hsotg, 0, 0, 0);
  636. call_gadget(hsotg, resume);
  637. } else {
  638. dwc2_exit_hibernation(hsotg, 1, 0, 1);
  639. }
  640. }
  641. }
  642. if ((gpwrdn & GPWRDN_RST_DET) && (gpwrdn & GPWRDN_RST_DET_MSK)) {
  643. dev_dbg(hsotg->dev, "%s: GPWRDN_RST_DET\n", __func__);
  644. if (!linestate && (gpwrdn & GPWRDN_BSESSVLD))
  645. dwc2_exit_hibernation(hsotg, 0, 1, 0);
  646. }
  647. if ((gpwrdn & GPWRDN_STS_CHGINT) &&
  648. (gpwrdn & GPWRDN_STS_CHGINT_MSK) && linestate) {
  649. dev_dbg(hsotg->dev, "%s: GPWRDN_STS_CHGINT\n", __func__);
  650. if (hsotg->hw_params.hibernation &&
  651. hsotg->hibernated) {
  652. if (gpwrdn & GPWRDN_IDSTS) {
  653. dwc2_exit_hibernation(hsotg, 0, 0, 0);
  654. call_gadget(hsotg, resume);
  655. } else {
  656. dwc2_exit_hibernation(hsotg, 1, 0, 1);
  657. }
  658. }
  659. }
  660. }
  661. /*
  662. * Common interrupt handler
  663. *
  664. * The common interrupts are those that occur in both Host and Device mode.
  665. * This handler handles the following interrupts:
  666. * - Mode Mismatch Interrupt
  667. * - OTG Interrupt
  668. * - Connector ID Status Change Interrupt
  669. * - Disconnect Interrupt
  670. * - Session Request Interrupt
  671. * - Resume / Remote Wakeup Detected Interrupt
  672. * - Suspend Interrupt
  673. */
  674. irqreturn_t dwc2_handle_common_intr(int irq, void *dev)
  675. {
  676. struct dwc2_hsotg *hsotg = dev;
  677. u32 gintsts;
  678. irqreturn_t retval = IRQ_NONE;
  679. spin_lock(&hsotg->lock);
  680. if (!dwc2_is_controller_alive(hsotg)) {
  681. dev_warn(hsotg->dev, "Controller is dead\n");
  682. goto out;
  683. }
  684. /* Reading current frame number value in device or host modes. */
  685. if (dwc2_is_device_mode(hsotg))
  686. hsotg->frame_number = (dwc2_readl(hsotg, DSTS)
  687. & DSTS_SOFFN_MASK) >> DSTS_SOFFN_SHIFT;
  688. else
  689. hsotg->frame_number = (dwc2_readl(hsotg, HFNUM)
  690. & HFNUM_FRNUM_MASK) >> HFNUM_FRNUM_SHIFT;
  691. gintsts = dwc2_read_common_intr(hsotg);
  692. if (gintsts & ~GINTSTS_PRTINT)
  693. retval = IRQ_HANDLED;
  694. /* In case of hibernated state gintsts must not work */
  695. if (hsotg->hibernated) {
  696. dwc2_handle_gpwrdn_intr(hsotg);
  697. retval = IRQ_HANDLED;
  698. goto out;
  699. }
  700. if (gintsts & GINTSTS_MODEMIS)
  701. dwc2_handle_mode_mismatch_intr(hsotg);
  702. if (gintsts & GINTSTS_OTGINT)
  703. dwc2_handle_otg_intr(hsotg);
  704. if (gintsts & GINTSTS_CONIDSTSCHNG)
  705. dwc2_handle_conn_id_status_change_intr(hsotg);
  706. if (gintsts & GINTSTS_DISCONNINT)
  707. dwc2_handle_disconnect_intr(hsotg);
  708. if (gintsts & GINTSTS_SESSREQINT)
  709. dwc2_handle_session_req_intr(hsotg);
  710. if (gintsts & GINTSTS_WKUPINT)
  711. dwc2_handle_wakeup_detected_intr(hsotg);
  712. if (gintsts & GINTSTS_USBSUSP)
  713. dwc2_handle_usb_suspend_intr(hsotg);
  714. if (gintsts & GINTSTS_LPMTRANRCVD)
  715. dwc2_handle_lpm_intr(hsotg);
  716. if (gintsts & GINTSTS_PRTINT) {
  717. /*
  718. * The port interrupt occurs while in device mode with HPRT0
  719. * Port Enable/Disable
  720. */
  721. if (dwc2_is_device_mode(hsotg)) {
  722. dev_dbg(hsotg->dev,
  723. " --Port interrupt received in Device mode--\n");
  724. dwc2_handle_usb_port_intr(hsotg);
  725. retval = IRQ_HANDLED;
  726. }
  727. }
  728. out:
  729. spin_unlock(&hsotg->lock);
  730. return retval;
  731. }