core_intr.c 24 KB

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