core_intr.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856
  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. u32 hprt0;
  288. /* Clear interrupt */
  289. dwc2_writel(hsotg, GINTSTS_SESSREQINT, GINTSTS);
  290. dev_dbg(hsotg->dev, "Session request interrupt - lx_state=%d\n",
  291. hsotg->lx_state);
  292. if (dwc2_is_device_mode(hsotg)) {
  293. if (hsotg->lx_state == DWC2_L2) {
  294. ret = dwc2_exit_partial_power_down(hsotg, true);
  295. if (ret && (ret != -ENOTSUPP))
  296. dev_err(hsotg->dev,
  297. "exit power_down failed\n");
  298. }
  299. /*
  300. * Report disconnect if there is any previous session
  301. * established
  302. */
  303. dwc2_hsotg_disconnect(hsotg);
  304. } else {
  305. /* Turn on the port power bit. */
  306. hprt0 = dwc2_read_hprt0(hsotg);
  307. hprt0 |= HPRT0_PWR;
  308. dwc2_writel(hsotg, hprt0, HPRT0);
  309. /* Connect hcd after port power is set. */
  310. dwc2_hcd_connect(hsotg);
  311. }
  312. }
  313. /**
  314. * dwc2_wakeup_from_lpm_l1 - Exit the device from LPM L1 state
  315. *
  316. * @hsotg: Programming view of DWC_otg controller
  317. *
  318. */
  319. static void dwc2_wakeup_from_lpm_l1(struct dwc2_hsotg *hsotg)
  320. {
  321. u32 glpmcfg;
  322. u32 i = 0;
  323. if (hsotg->lx_state != DWC2_L1) {
  324. dev_err(hsotg->dev, "Core isn't in DWC2_L1 state\n");
  325. return;
  326. }
  327. glpmcfg = dwc2_readl(hsotg, GLPMCFG);
  328. if (dwc2_is_device_mode(hsotg)) {
  329. dev_dbg(hsotg->dev, "Exit from L1 state\n");
  330. glpmcfg &= ~GLPMCFG_ENBLSLPM;
  331. glpmcfg &= ~GLPMCFG_HIRD_THRES_EN;
  332. dwc2_writel(hsotg, glpmcfg, GLPMCFG);
  333. do {
  334. glpmcfg = dwc2_readl(hsotg, GLPMCFG);
  335. if (!(glpmcfg & (GLPMCFG_COREL1RES_MASK |
  336. GLPMCFG_L1RESUMEOK | GLPMCFG_SLPSTS)))
  337. break;
  338. udelay(1);
  339. } while (++i < 200);
  340. if (i == 200) {
  341. dev_err(hsotg->dev, "Failed to exit L1 sleep state in 200us.\n");
  342. return;
  343. }
  344. dwc2_gadget_init_lpm(hsotg);
  345. } else {
  346. /* TODO */
  347. dev_err(hsotg->dev, "Host side LPM is not supported.\n");
  348. return;
  349. }
  350. /* Change to L0 state */
  351. hsotg->lx_state = DWC2_L0;
  352. /* Inform gadget to exit from L1 */
  353. call_gadget(hsotg, resume);
  354. }
  355. /*
  356. * This interrupt indicates that the DWC_otg controller has detected a
  357. * resume or remote wakeup sequence. If the DWC_otg controller is in
  358. * low power mode, the handler must brings the controller out of low
  359. * power mode. The controller automatically begins resume signaling.
  360. * The handler schedules a time to stop resume signaling.
  361. */
  362. static void dwc2_handle_wakeup_detected_intr(struct dwc2_hsotg *hsotg)
  363. {
  364. int ret;
  365. /* Clear interrupt */
  366. dwc2_writel(hsotg, GINTSTS_WKUPINT, GINTSTS);
  367. dev_dbg(hsotg->dev, "++Resume or Remote Wakeup Detected Interrupt++\n");
  368. dev_dbg(hsotg->dev, "%s lxstate = %d\n", __func__, hsotg->lx_state);
  369. if (hsotg->lx_state == DWC2_L1) {
  370. dwc2_wakeup_from_lpm_l1(hsotg);
  371. return;
  372. }
  373. if (dwc2_is_device_mode(hsotg)) {
  374. dev_dbg(hsotg->dev, "DSTS=0x%0x\n",
  375. dwc2_readl(hsotg, DSTS));
  376. if (hsotg->lx_state == DWC2_L2) {
  377. u32 dctl = dwc2_readl(hsotg, DCTL);
  378. /* Clear Remote Wakeup Signaling */
  379. dctl &= ~DCTL_RMTWKUPSIG;
  380. dwc2_writel(hsotg, dctl, DCTL);
  381. ret = dwc2_exit_partial_power_down(hsotg, true);
  382. if (ret && (ret != -ENOTSUPP))
  383. dev_err(hsotg->dev, "exit power_down failed\n");
  384. /* Change to L0 state */
  385. hsotg->lx_state = DWC2_L0;
  386. call_gadget(hsotg, resume);
  387. } else {
  388. /* Change to L0 state */
  389. hsotg->lx_state = DWC2_L0;
  390. }
  391. } else {
  392. if (hsotg->params.power_down)
  393. return;
  394. if (hsotg->lx_state != DWC2_L1) {
  395. u32 pcgcctl = dwc2_readl(hsotg, PCGCTL);
  396. /* Restart the Phy Clock */
  397. pcgcctl &= ~PCGCTL_STOPPCLK;
  398. dwc2_writel(hsotg, pcgcctl, PCGCTL);
  399. mod_timer(&hsotg->wkp_timer,
  400. jiffies + msecs_to_jiffies(71));
  401. } else {
  402. /* Change to L0 state */
  403. hsotg->lx_state = DWC2_L0;
  404. }
  405. }
  406. }
  407. /*
  408. * This interrupt indicates that a device has been disconnected from the
  409. * root port
  410. */
  411. static void dwc2_handle_disconnect_intr(struct dwc2_hsotg *hsotg)
  412. {
  413. dwc2_writel(hsotg, GINTSTS_DISCONNINT, GINTSTS);
  414. dev_dbg(hsotg->dev, "++Disconnect Detected Interrupt++ (%s) %s\n",
  415. dwc2_is_host_mode(hsotg) ? "Host" : "Device",
  416. dwc2_op_state_str(hsotg));
  417. if (hsotg->op_state == OTG_STATE_A_HOST)
  418. dwc2_hcd_disconnect(hsotg, false);
  419. }
  420. /*
  421. * This interrupt indicates that SUSPEND state has been detected on the USB.
  422. *
  423. * For HNP the USB Suspend interrupt signals the change from "a_peripheral"
  424. * to "a_host".
  425. *
  426. * When power management is enabled the core will be put in low power mode.
  427. */
  428. static void dwc2_handle_usb_suspend_intr(struct dwc2_hsotg *hsotg)
  429. {
  430. u32 dsts;
  431. int ret;
  432. /* Clear interrupt */
  433. dwc2_writel(hsotg, GINTSTS_USBSUSP, GINTSTS);
  434. dev_dbg(hsotg->dev, "USB SUSPEND\n");
  435. if (dwc2_is_device_mode(hsotg)) {
  436. /*
  437. * Check the Device status register to determine if the Suspend
  438. * state is active
  439. */
  440. dsts = dwc2_readl(hsotg, DSTS);
  441. dev_dbg(hsotg->dev, "%s: DSTS=0x%0x\n", __func__, dsts);
  442. dev_dbg(hsotg->dev,
  443. "DSTS.Suspend Status=%d HWCFG4.Power Optimize=%d HWCFG4.Hibernation=%d\n",
  444. !!(dsts & DSTS_SUSPSTS),
  445. hsotg->hw_params.power_optimized,
  446. hsotg->hw_params.hibernation);
  447. /* Ignore suspend request before enumeration */
  448. if (!dwc2_is_device_connected(hsotg)) {
  449. dev_dbg(hsotg->dev,
  450. "ignore suspend request before enumeration\n");
  451. return;
  452. }
  453. if (dsts & DSTS_SUSPSTS) {
  454. if (hsotg->hw_params.power_optimized) {
  455. ret = dwc2_enter_partial_power_down(hsotg);
  456. if (ret) {
  457. if (ret != -ENOTSUPP)
  458. dev_err(hsotg->dev,
  459. "%s: enter partial_power_down failed\n",
  460. __func__);
  461. goto skip_power_saving;
  462. }
  463. udelay(100);
  464. /* Ask phy to be suspended */
  465. if (!IS_ERR_OR_NULL(hsotg->uphy))
  466. usb_phy_set_suspend(hsotg->uphy, true);
  467. }
  468. if (hsotg->hw_params.hibernation) {
  469. ret = dwc2_enter_hibernation(hsotg, 0);
  470. if (ret && ret != -ENOTSUPP)
  471. dev_err(hsotg->dev,
  472. "%s: enter hibernation failed\n",
  473. __func__);
  474. }
  475. skip_power_saving:
  476. /*
  477. * Change to L2 (suspend) state before releasing
  478. * spinlock
  479. */
  480. hsotg->lx_state = DWC2_L2;
  481. /* Call gadget suspend callback */
  482. call_gadget(hsotg, suspend);
  483. }
  484. } else {
  485. if (hsotg->op_state == OTG_STATE_A_PERIPHERAL) {
  486. dev_dbg(hsotg->dev, "a_peripheral->a_host\n");
  487. /* Change to L2 (suspend) state */
  488. hsotg->lx_state = DWC2_L2;
  489. /* Clear the a_peripheral flag, back to a_host */
  490. spin_unlock(&hsotg->lock);
  491. dwc2_hcd_start(hsotg);
  492. spin_lock(&hsotg->lock);
  493. hsotg->op_state = OTG_STATE_A_HOST;
  494. }
  495. }
  496. }
  497. /**
  498. * dwc2_handle_lpm_intr - GINTSTS_LPMTRANRCVD Interrupt handler
  499. *
  500. * @hsotg: Programming view of DWC_otg controller
  501. *
  502. */
  503. static void dwc2_handle_lpm_intr(struct dwc2_hsotg *hsotg)
  504. {
  505. u32 glpmcfg;
  506. u32 pcgcctl;
  507. u32 hird;
  508. u32 hird_thres;
  509. u32 hird_thres_en;
  510. u32 enslpm;
  511. /* Clear interrupt */
  512. dwc2_writel(hsotg, GINTSTS_LPMTRANRCVD, GINTSTS);
  513. glpmcfg = dwc2_readl(hsotg, GLPMCFG);
  514. if (!(glpmcfg & GLPMCFG_LPMCAP)) {
  515. dev_err(hsotg->dev, "Unexpected LPM interrupt\n");
  516. return;
  517. }
  518. hird = (glpmcfg & GLPMCFG_HIRD_MASK) >> GLPMCFG_HIRD_SHIFT;
  519. hird_thres = (glpmcfg & GLPMCFG_HIRD_THRES_MASK &
  520. ~GLPMCFG_HIRD_THRES_EN) >> GLPMCFG_HIRD_THRES_SHIFT;
  521. hird_thres_en = glpmcfg & GLPMCFG_HIRD_THRES_EN;
  522. enslpm = glpmcfg & GLPMCFG_ENBLSLPM;
  523. if (dwc2_is_device_mode(hsotg)) {
  524. dev_dbg(hsotg->dev, "HIRD_THRES_EN = %d\n", hird_thres_en);
  525. if (hird_thres_en && hird >= hird_thres) {
  526. dev_dbg(hsotg->dev, "L1 with utmi_l1_suspend_n\n");
  527. } else if (enslpm) {
  528. dev_dbg(hsotg->dev, "L1 with utmi_sleep_n\n");
  529. } else {
  530. dev_dbg(hsotg->dev, "Entering Sleep with L1 Gating\n");
  531. pcgcctl = dwc2_readl(hsotg, PCGCTL);
  532. pcgcctl |= PCGCTL_ENBL_SLEEP_GATING;
  533. dwc2_writel(hsotg, pcgcctl, PCGCTL);
  534. }
  535. /**
  536. * Examine prt_sleep_sts after TL1TokenTetry period max (10 us)
  537. */
  538. udelay(10);
  539. glpmcfg = dwc2_readl(hsotg, GLPMCFG);
  540. if (glpmcfg & GLPMCFG_SLPSTS) {
  541. /* Save the current state */
  542. hsotg->lx_state = DWC2_L1;
  543. dev_dbg(hsotg->dev,
  544. "Core is in L1 sleep glpmcfg=%08x\n", glpmcfg);
  545. /* Inform gadget that we are in L1 state */
  546. call_gadget(hsotg, suspend);
  547. }
  548. }
  549. }
  550. #define GINTMSK_COMMON (GINTSTS_WKUPINT | GINTSTS_SESSREQINT | \
  551. GINTSTS_CONIDSTSCHNG | GINTSTS_OTGINT | \
  552. GINTSTS_MODEMIS | GINTSTS_DISCONNINT | \
  553. GINTSTS_USBSUSP | GINTSTS_PRTINT | \
  554. GINTSTS_LPMTRANRCVD)
  555. /*
  556. * This function returns the Core Interrupt register
  557. */
  558. static u32 dwc2_read_common_intr(struct dwc2_hsotg *hsotg)
  559. {
  560. u32 gintsts;
  561. u32 gintmsk;
  562. u32 gahbcfg;
  563. u32 gintmsk_common = GINTMSK_COMMON;
  564. gintsts = dwc2_readl(hsotg, GINTSTS);
  565. gintmsk = dwc2_readl(hsotg, GINTMSK);
  566. gahbcfg = dwc2_readl(hsotg, GAHBCFG);
  567. /* If any common interrupts set */
  568. if (gintsts & gintmsk_common)
  569. dev_dbg(hsotg->dev, "gintsts=%08x gintmsk=%08x\n",
  570. gintsts, gintmsk);
  571. if (gahbcfg & GAHBCFG_GLBL_INTR_EN)
  572. return gintsts & gintmsk & gintmsk_common;
  573. else
  574. return 0;
  575. }
  576. /**
  577. * dwc_handle_gpwrdn_disc_det() - Handles the gpwrdn disconnect detect.
  578. * Exits hibernation without restoring registers.
  579. *
  580. * @hsotg: Programming view of DWC_otg controller
  581. * @gpwrdn: GPWRDN register
  582. */
  583. static inline void dwc_handle_gpwrdn_disc_det(struct dwc2_hsotg *hsotg,
  584. u32 gpwrdn)
  585. {
  586. u32 gpwrdn_tmp;
  587. /* Switch-on voltage to the core */
  588. gpwrdn_tmp = dwc2_readl(hsotg, GPWRDN);
  589. gpwrdn_tmp &= ~GPWRDN_PWRDNSWTCH;
  590. dwc2_writel(hsotg, gpwrdn_tmp, GPWRDN);
  591. udelay(5);
  592. /* Reset core */
  593. gpwrdn_tmp = dwc2_readl(hsotg, GPWRDN);
  594. gpwrdn_tmp &= ~GPWRDN_PWRDNRSTN;
  595. dwc2_writel(hsotg, gpwrdn_tmp, GPWRDN);
  596. udelay(5);
  597. /* Disable Power Down Clamp */
  598. gpwrdn_tmp = dwc2_readl(hsotg, GPWRDN);
  599. gpwrdn_tmp &= ~GPWRDN_PWRDNCLMP;
  600. dwc2_writel(hsotg, gpwrdn_tmp, GPWRDN);
  601. udelay(5);
  602. /* Deassert reset core */
  603. gpwrdn_tmp = dwc2_readl(hsotg, GPWRDN);
  604. gpwrdn_tmp |= GPWRDN_PWRDNRSTN;
  605. dwc2_writel(hsotg, gpwrdn_tmp, GPWRDN);
  606. udelay(5);
  607. /* Disable PMU interrupt */
  608. gpwrdn_tmp = dwc2_readl(hsotg, GPWRDN);
  609. gpwrdn_tmp &= ~GPWRDN_PMUINTSEL;
  610. dwc2_writel(hsotg, gpwrdn_tmp, GPWRDN);
  611. /* De-assert Wakeup Logic */
  612. gpwrdn_tmp = dwc2_readl(hsotg, GPWRDN);
  613. gpwrdn_tmp &= ~GPWRDN_PMUACTV;
  614. dwc2_writel(hsotg, gpwrdn_tmp, GPWRDN);
  615. hsotg->hibernated = 0;
  616. hsotg->bus_suspended = 0;
  617. if (gpwrdn & GPWRDN_IDSTS) {
  618. hsotg->op_state = OTG_STATE_B_PERIPHERAL;
  619. dwc2_core_init(hsotg, false);
  620. dwc2_enable_global_interrupts(hsotg);
  621. dwc2_hsotg_core_init_disconnected(hsotg, false);
  622. dwc2_hsotg_core_connect(hsotg);
  623. } else {
  624. hsotg->op_state = OTG_STATE_A_HOST;
  625. /* Initialize the Core for Host mode */
  626. dwc2_core_init(hsotg, false);
  627. dwc2_enable_global_interrupts(hsotg);
  628. dwc2_hcd_start(hsotg);
  629. }
  630. }
  631. /*
  632. * GPWRDN interrupt handler.
  633. *
  634. * The GPWRDN interrupts are those that occur in both Host and
  635. * Device mode while core is in hibernated state.
  636. */
  637. static void dwc2_handle_gpwrdn_intr(struct dwc2_hsotg *hsotg)
  638. {
  639. u32 gpwrdn;
  640. int linestate;
  641. gpwrdn = dwc2_readl(hsotg, GPWRDN);
  642. /* clear all interrupt */
  643. dwc2_writel(hsotg, gpwrdn, GPWRDN);
  644. linestate = (gpwrdn & GPWRDN_LINESTATE_MASK) >> GPWRDN_LINESTATE_SHIFT;
  645. dev_dbg(hsotg->dev,
  646. "%s: dwc2_handle_gpwrdwn_intr called gpwrdn= %08x\n", __func__,
  647. gpwrdn);
  648. if ((gpwrdn & GPWRDN_DISCONN_DET) &&
  649. (gpwrdn & GPWRDN_DISCONN_DET_MSK) && !linestate) {
  650. dev_dbg(hsotg->dev, "%s: GPWRDN_DISCONN_DET\n", __func__);
  651. /*
  652. * Call disconnect detect function to exit from
  653. * hibernation
  654. */
  655. dwc_handle_gpwrdn_disc_det(hsotg, gpwrdn);
  656. } else if ((gpwrdn & GPWRDN_LNSTSCHG) &&
  657. (gpwrdn & GPWRDN_LNSTSCHG_MSK) && linestate) {
  658. dev_dbg(hsotg->dev, "%s: GPWRDN_LNSTSCHG\n", __func__);
  659. if (hsotg->hw_params.hibernation &&
  660. hsotg->hibernated) {
  661. if (gpwrdn & GPWRDN_IDSTS) {
  662. dwc2_exit_hibernation(hsotg, 0, 0, 0);
  663. call_gadget(hsotg, resume);
  664. } else {
  665. dwc2_exit_hibernation(hsotg, 1, 0, 1);
  666. }
  667. }
  668. } else if ((gpwrdn & GPWRDN_RST_DET) &&
  669. (gpwrdn & GPWRDN_RST_DET_MSK)) {
  670. dev_dbg(hsotg->dev, "%s: GPWRDN_RST_DET\n", __func__);
  671. if (!linestate && (gpwrdn & GPWRDN_BSESSVLD))
  672. dwc2_exit_hibernation(hsotg, 0, 1, 0);
  673. } else if ((gpwrdn & GPWRDN_STS_CHGINT) &&
  674. (gpwrdn & GPWRDN_STS_CHGINT_MSK)) {
  675. dev_dbg(hsotg->dev, "%s: GPWRDN_STS_CHGINT\n", __func__);
  676. /*
  677. * As GPWRDN_STS_CHGINT exit from hibernation flow is
  678. * the same as in GPWRDN_DISCONN_DET flow. Call
  679. * disconnect detect helper function to exit from
  680. * hibernation.
  681. */
  682. dwc_handle_gpwrdn_disc_det(hsotg, gpwrdn);
  683. }
  684. }
  685. /*
  686. * Common interrupt handler
  687. *
  688. * The common interrupts are those that occur in both Host and Device mode.
  689. * This handler handles the following interrupts:
  690. * - Mode Mismatch Interrupt
  691. * - OTG Interrupt
  692. * - Connector ID Status Change Interrupt
  693. * - Disconnect Interrupt
  694. * - Session Request Interrupt
  695. * - Resume / Remote Wakeup Detected Interrupt
  696. * - Suspend Interrupt
  697. */
  698. irqreturn_t dwc2_handle_common_intr(int irq, void *dev)
  699. {
  700. struct dwc2_hsotg *hsotg = dev;
  701. u32 gintsts;
  702. irqreturn_t retval = IRQ_NONE;
  703. spin_lock(&hsotg->lock);
  704. if (!dwc2_is_controller_alive(hsotg)) {
  705. dev_warn(hsotg->dev, "Controller is dead\n");
  706. goto out;
  707. }
  708. /* Reading current frame number value in device or host modes. */
  709. if (dwc2_is_device_mode(hsotg))
  710. hsotg->frame_number = (dwc2_readl(hsotg, DSTS)
  711. & DSTS_SOFFN_MASK) >> DSTS_SOFFN_SHIFT;
  712. else
  713. hsotg->frame_number = (dwc2_readl(hsotg, HFNUM)
  714. & HFNUM_FRNUM_MASK) >> HFNUM_FRNUM_SHIFT;
  715. gintsts = dwc2_read_common_intr(hsotg);
  716. if (gintsts & ~GINTSTS_PRTINT)
  717. retval = IRQ_HANDLED;
  718. /* In case of hibernated state gintsts must not work */
  719. if (hsotg->hibernated) {
  720. dwc2_handle_gpwrdn_intr(hsotg);
  721. retval = IRQ_HANDLED;
  722. goto out;
  723. }
  724. if (gintsts & GINTSTS_MODEMIS)
  725. dwc2_handle_mode_mismatch_intr(hsotg);
  726. if (gintsts & GINTSTS_OTGINT)
  727. dwc2_handle_otg_intr(hsotg);
  728. if (gintsts & GINTSTS_CONIDSTSCHNG)
  729. dwc2_handle_conn_id_status_change_intr(hsotg);
  730. if (gintsts & GINTSTS_DISCONNINT)
  731. dwc2_handle_disconnect_intr(hsotg);
  732. if (gintsts & GINTSTS_SESSREQINT)
  733. dwc2_handle_session_req_intr(hsotg);
  734. if (gintsts & GINTSTS_WKUPINT)
  735. dwc2_handle_wakeup_detected_intr(hsotg);
  736. if (gintsts & GINTSTS_USBSUSP)
  737. dwc2_handle_usb_suspend_intr(hsotg);
  738. if (gintsts & GINTSTS_LPMTRANRCVD)
  739. dwc2_handle_lpm_intr(hsotg);
  740. if (gintsts & GINTSTS_PRTINT) {
  741. /*
  742. * The port interrupt occurs while in device mode with HPRT0
  743. * Port Enable/Disable
  744. */
  745. if (dwc2_is_device_mode(hsotg)) {
  746. dev_dbg(hsotg->dev,
  747. " --Port interrupt received in Device mode--\n");
  748. dwc2_handle_usb_port_intr(hsotg);
  749. retval = IRQ_HANDLED;
  750. }
  751. }
  752. out:
  753. spin_unlock(&hsotg->lock);
  754. return retval;
  755. }