dwc_otg_cil_intr.c 42 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446
  1. /* ==========================================================================
  2. * $File: //dwh/usb_iip/dev/software/otg/linux/drivers/dwc_otg_cil_intr.c $
  3. * $Revision: #31 $
  4. * $Date: 2011/10/24 $
  5. * $Change: 1871286 $
  6. *
  7. * Synopsys HS OTG Linux Software Driver and documentation (hereinafter,
  8. * "Software") is an Unsupported proprietary work of Synopsys, Inc. unless
  9. * otherwise expressly agreed to in writing between Synopsys and you.
  10. *
  11. * The Software IS NOT an item of Licensed Software or Licensed Product under
  12. * any End User Software License Agreement or Agreement for Licensed Product
  13. * with Synopsys or any supplement thereto. You are permitted to use and
  14. * redistribute this Software in source and binary forms, with or without
  15. * modification, provided that redistributions of source code must retain this
  16. * notice. You may not view, use, disclose, copy or distribute this file or
  17. * any information contained herein except pursuant to this license grant from
  18. * Synopsys. If you do not agree with this notice, including the disclaimer
  19. * below, then you are not authorized to use the Software.
  20. *
  21. * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS" BASIS
  22. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  23. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  24. * ARE HEREBY DISCLAIMED. IN NO EVENT SHALL SYNOPSYS BE LIABLE FOR ANY DIRECT,
  25. * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  26. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  27. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  28. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  29. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  30. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  31. * DAMAGE.
  32. * ========================================================================== */
  33. /** @file
  34. *
  35. * The Core Interface Layer provides basic services for accessing and
  36. * managing the DWC_otg hardware. These services are used by both the
  37. * Host Controller Driver and the Peripheral Controller Driver.
  38. *
  39. * This file contains the Common Interrupt handlers.
  40. */
  41. #include "dwc_os.h"
  42. #include "dwc_otg_regs.h"
  43. #include "dwc_otg_cil.h"
  44. #include "dwc_otg_driver.h"
  45. #include "dwc_otg_pcd.h"
  46. #include "dwc_otg_hcd.h"
  47. #ifdef DEBUG
  48. inline const char *op_state_str(dwc_otg_core_if_t * core_if)
  49. {
  50. return (core_if->op_state == A_HOST ? "a_host" :
  51. (core_if->op_state == A_SUSPEND ? "a_suspend" :
  52. (core_if->op_state == A_PERIPHERAL ? "a_peripheral" :
  53. (core_if->op_state == B_PERIPHERAL ? "b_peripheral" :
  54. (core_if->op_state == B_HOST ? "b_host" : "unknown")))));
  55. }
  56. #endif
  57. static void charger_detect_work(void *_vp)
  58. {
  59. dwc_otg_core_if_t * core_if = (dwc_otg_core_if_t *) _vp;
  60. DWC_DEBUGPL(DBG_HCDV, "%s() %p\n", __func__, core_if);
  61. /* At this point pull up should be disabled */
  62. if(core_if->charger_detect_cb)
  63. core_if->bc_mode = dwc_otg_charger_detect(core_if);
  64. DWC_SPINLOCK(core_if->lock);
  65. if(core_if->session_valid){
  66. /* Save status, turn on pull up */
  67. core_if->dev_if->vbus_on = 1;
  68. if(core_if->dev_if->pull_up){
  69. dwc_otg_device_soft_connect(core_if);
  70. }
  71. }else{
  72. core_if->dev_if->vbus_on = 0;
  73. /* Disable Pull up, defaultly */
  74. if(core_if->dev_if->pull_up)
  75. dwc_otg_device_soft_disconnect(core_if);
  76. }
  77. DWC_SPINUNLOCK(core_if->lock);
  78. /* Check again, and callback */
  79. if(core_if->charger_detect_cb)
  80. core_if->charger_detect_cb(core_if->bc_mode);
  81. }
  82. /** This function will log a debug message
  83. *
  84. * @param core_if Programming view of DWC_otg controller.
  85. */
  86. int32_t dwc_otg_handle_mode_mismatch_intr(dwc_otg_core_if_t * core_if)
  87. {
  88. gintsts_data_t gintsts;
  89. DWC_WARN("Mode Mismatch Interrupt: currently in %s mode\n",
  90. dwc_otg_mode(core_if) ? "Host" : "Device");
  91. /* Clear interrupt */
  92. gintsts.d32 = 0;
  93. gintsts.b.modemismatch = 1;
  94. DWC_WRITE_REG32(&core_if->core_global_regs->gintsts, gintsts.d32);
  95. return 1;
  96. }
  97. /**
  98. * This function handles the OTG Interrupts. It reads the OTG
  99. * Interrupt Register (GOTGINT) to determine what interrupt has
  100. * occurred.
  101. *
  102. * @param core_if Programming view of DWC_otg controller.
  103. */
  104. int32_t dwc_otg_handle_otg_intr(dwc_otg_core_if_t * core_if)
  105. {
  106. dwc_otg_core_global_regs_t *global_regs = core_if->core_global_regs;
  107. gotgint_data_t gotgint;
  108. gotgctl_data_t gotgctl;
  109. gintmsk_data_t gintmsk;
  110. gpwrdn_data_t gpwrdn;
  111. gotgint.d32 = DWC_READ_REG32(&global_regs->gotgint);
  112. gotgctl.d32 = DWC_READ_REG32(&global_regs->gotgctl);
  113. DWC_DEBUGPL(DBG_CIL, "++OTG Interrupt gotgint=%0x [%s]\n", gotgint.d32,
  114. op_state_str(core_if));
  115. if (gotgint.b.sesenddet) {
  116. DWC_DEBUGPL(DBG_ANY, " ++OTG Interrupt: "
  117. "Session End Detected++ (%s)\n",
  118. op_state_str(core_if));
  119. gotgctl.d32 = DWC_READ_REG32(&global_regs->gotgctl);
  120. if (core_if->op_state == B_HOST) {
  121. cil_pcd_start(core_if);
  122. core_if->op_state = B_PERIPHERAL;
  123. } else {
  124. /* If not B_HOST and Device HNP still set. HNP
  125. * Did not succeed!*/
  126. if (gotgctl.b.devhnpen) {
  127. DWC_DEBUGPL(DBG_ANY, "Session End Detected\n");
  128. __DWC_ERROR("Device Not Connected/Responding!\n");
  129. }
  130. core_if->session_valid = 0;
  131. DWC_WORKQ_SCHEDULE(core_if->wq_otg,
  132. charger_detect_work, core_if,
  133. "Charger detect");
  134. /* If Session End Detected the B-Cable has
  135. * been disconnected. */
  136. /* Reset PCD and Gadget driver to a
  137. * clean state. */
  138. core_if->lx_state = DWC_OTG_L0;
  139. DWC_SPINUNLOCK(core_if->lock);
  140. cil_pcd_stop(core_if);
  141. DWC_SPINLOCK(core_if->lock);
  142. if (core_if->adp_enable) {
  143. if (core_if->power_down == 2) {
  144. gpwrdn.d32 = 0;
  145. gpwrdn.b.pwrdnswtch = 1;
  146. DWC_MODIFY_REG32(&core_if->
  147. core_global_regs->
  148. gpwrdn, gpwrdn.d32, 0);
  149. }
  150. gpwrdn.d32 = 0;
  151. gpwrdn.b.pmuintsel = 1;
  152. gpwrdn.b.pmuactv = 1;
  153. DWC_MODIFY_REG32(&core_if->core_global_regs->
  154. gpwrdn, 0, gpwrdn.d32);
  155. dwc_otg_adp_sense_start(core_if);
  156. }
  157. }
  158. gotgctl.d32 = 0;
  159. gotgctl.b.devhnpen = 1;
  160. DWC_MODIFY_REG32(&global_regs->gotgctl, gotgctl.d32, 0);
  161. }
  162. if (gotgint.b.sesreqsucstschng) {
  163. DWC_DEBUGPL(DBG_ANY, " ++OTG Interrupt: "
  164. "Session Reqeust Success Status Change++\n");
  165. gotgctl.d32 = DWC_READ_REG32(&global_regs->gotgctl);
  166. if (gotgctl.b.sesreqscs) {
  167. if ((core_if->core_params->phy_type ==
  168. DWC_PHY_TYPE_PARAM_FS) && (core_if->core_params->i2c_enable)) {
  169. core_if->srp_success = 1;
  170. } else {
  171. DWC_SPINUNLOCK(core_if->lock);
  172. cil_pcd_resume(core_if);
  173. DWC_SPINLOCK(core_if->lock);
  174. /* Clear Session Request */
  175. gotgctl.d32 = 0;
  176. gotgctl.b.sesreq = 1;
  177. DWC_MODIFY_REG32(&global_regs->gotgctl,
  178. gotgctl.d32, 0);
  179. }
  180. }
  181. }
  182. if (gotgint.b.hstnegsucstschng) {
  183. /* Print statements during the HNP interrupt handling
  184. * can cause it to fail.*/
  185. gotgctl.d32 = DWC_READ_REG32(&global_regs->gotgctl);
  186. if (gotgctl.b.hstnegscs) {
  187. if (dwc_otg_is_host_mode(core_if)) {
  188. core_if->op_state = B_HOST;
  189. /*
  190. * Need to disable SOF interrupt immediately.
  191. * When switching from device to host, the PCD
  192. * interrupt handler won't handle the
  193. * interrupt if host mode is already set. The
  194. * HCD interrupt handler won't get called if
  195. * the HCD state is HALT. This means that the
  196. * interrupt does not get handled and Linux
  197. * complains loudly.
  198. */
  199. gintmsk.d32 = 0;
  200. gintmsk.b.sofintr = 1;
  201. DWC_MODIFY_REG32(&global_regs->gintmsk,
  202. gintmsk.d32, 0);
  203. /* Call callback function with spin lock released */
  204. DWC_SPINUNLOCK(core_if->lock);
  205. cil_pcd_stop(core_if);
  206. /*
  207. * Initialize the Core for Host mode.
  208. */
  209. cil_hcd_start(core_if);
  210. DWC_SPINLOCK(core_if->lock);
  211. core_if->op_state = B_HOST;
  212. }
  213. } else {
  214. gotgctl.d32 = 0;
  215. gotgctl.b.hnpreq = 1;
  216. gotgctl.b.devhnpen = 1;
  217. DWC_MODIFY_REG32(&global_regs->gotgctl, gotgctl.d32, 0);
  218. DWC_DEBUGPL(DBG_ANY, "HNP Failed\n");
  219. __DWC_ERROR("Device Not Connected/Responding\n");
  220. }
  221. }
  222. if (gotgint.b.hstnegdet) {
  223. /* The disconnect interrupt is set at the same time as
  224. * Host Negotiation Detected. During the mode
  225. * switch all interrupts are cleared so the disconnect
  226. * interrupt handler will not get executed.
  227. */
  228. DWC_DEBUGPL(DBG_ANY, " ++OTG Interrupt: "
  229. "Host Negotiation Detected++ (%s)\n",
  230. (dwc_otg_is_host_mode(core_if) ? "Host" :
  231. "Device"));
  232. if (dwc_otg_is_device_mode(core_if)) {
  233. DWC_DEBUGPL(DBG_ANY, "a_suspend->a_peripheral (%d)\n",
  234. core_if->op_state);
  235. DWC_SPINUNLOCK(core_if->lock);
  236. cil_hcd_disconnect(core_if);
  237. cil_pcd_start(core_if);
  238. DWC_SPINLOCK(core_if->lock);
  239. core_if->op_state = A_PERIPHERAL;
  240. } else {
  241. /*
  242. * Need to disable SOF interrupt immediately. When
  243. * switching from device to host, the PCD interrupt
  244. * handler won't handle the interrupt if host mode is
  245. * already set. The HCD interrupt handler won't get
  246. * called if the HCD state is HALT. This means that
  247. * the interrupt does not get handled and Linux
  248. * complains loudly.
  249. */
  250. gintmsk.d32 = 0;
  251. gintmsk.b.sofintr = 1;
  252. DWC_MODIFY_REG32(&global_regs->gintmsk, gintmsk.d32, 0);
  253. DWC_SPINUNLOCK(core_if->lock);
  254. cil_pcd_stop(core_if);
  255. cil_hcd_start(core_if);
  256. DWC_SPINLOCK(core_if->lock);
  257. core_if->op_state = A_HOST;
  258. }
  259. }
  260. if (gotgint.b.adevtoutchng) {
  261. DWC_DEBUGPL(DBG_ANY, " ++OTG Interrupt: "
  262. "A-Device Timeout Change++\n");
  263. }
  264. if (gotgint.b.debdone) {
  265. DWC_DEBUGPL(DBG_ANY, " ++OTG Interrupt: " "Debounce Done++\n");
  266. }
  267. /* Clear GOTGINT */
  268. DWC_WRITE_REG32(&core_if->core_global_regs->gotgint, gotgint.d32);
  269. return 1;
  270. }
  271. void w_conn_id_status_change(void *p)
  272. {
  273. dwc_otg_core_if_t *core_if = p;
  274. uint32_t count = 0;
  275. gotgctl_data_t gotgctl = {.d32 = 0 };
  276. gotgctl.d32 = DWC_READ_REG32(&core_if->core_global_regs->gotgctl);
  277. DWC_DEBUGPL(DBG_CIL, "gotgctl=%0x\n", gotgctl.d32);
  278. DWC_DEBUGPL(DBG_CIL, "gotgctl.b.conidsts=%d\n", gotgctl.b.conidsts);
  279. /* B-Device connector (Device Mode) */
  280. if (gotgctl.b.conidsts) {
  281. /* Wait for switch to device mode. */
  282. while (!dwc_otg_is_device_mode(core_if)) {
  283. DWC_PRINTF("Waiting for Peripheral Mode, Mode=%s\n",
  284. (dwc_otg_is_host_mode(core_if) ? "Host" :
  285. "Peripheral"));
  286. dwc_mdelay(100);
  287. if (++count > 10000)
  288. break;
  289. }
  290. DWC_ASSERT(++count < 10000,
  291. "Connection id status change timed out");
  292. core_if->op_state = B_PERIPHERAL;
  293. DWC_PRINTF("DEVICE mode\n");
  294. cil_hcd_stop(core_if);
  295. dwc_otg_core_init(core_if);
  296. dwc_otg_enable_global_interrupts(core_if);
  297. cil_pcd_start(core_if);
  298. } else {
  299. /* A-Device connector (Host Mode) */
  300. while (!dwc_otg_is_host_mode(core_if)) {
  301. DWC_PRINTF("Waiting for Host Mode, Mode=%s\n",
  302. (dwc_otg_is_host_mode(core_if) ? "Host" :
  303. "Peripheral"));
  304. dwc_mdelay(100);
  305. if (++count > 10000)
  306. break;
  307. }
  308. DWC_ASSERT(++count < 10000,
  309. "Connection id status change timed out");
  310. core_if->op_state = A_HOST;
  311. DWC_PRINTF("HOST mode\n");
  312. cil_pcd_stop(core_if);
  313. /*
  314. * Initialize the Core for Host mode.
  315. */
  316. dwc_otg_core_init(core_if);
  317. dwc_otg_enable_global_interrupts(core_if);
  318. cil_hcd_start(core_if);
  319. }
  320. }
  321. /**
  322. * This function handles the Connector ID Status Change Interrupt. It
  323. * reads the OTG Interrupt Register (GOTCTL) to determine whether this
  324. * is a Device to Host Mode transition or a Host Mode to Device
  325. * Transition.
  326. *
  327. * This only occurs when the cable is connected/removed from the PHY
  328. * connector.
  329. *
  330. * @param core_if Programming view of DWC_otg controller.
  331. */
  332. int32_t dwc_otg_handle_conn_id_status_change_intr(dwc_otg_core_if_t * core_if)
  333. {
  334. /*
  335. * Need to disable SOF interrupt immediately. If switching from device
  336. * to host, the PCD interrupt handler won't handle the interrupt if
  337. * host mode is already set. The HCD interrupt handler won't get
  338. * called if the HCD state is HALT. This means that the interrupt does
  339. * not get handled and Linux complains loudly.
  340. */
  341. gintmsk_data_t gintmsk = {.d32 = 0 };
  342. gintsts_data_t gintsts = {.d32 = 0 };
  343. gintmsk.b.sofintr = 1;
  344. DWC_MODIFY_REG32(&core_if->core_global_regs->gintmsk, gintmsk.d32, 0);
  345. DWC_DEBUGPL(DBG_CIL,
  346. " ++Connector ID Status Change Interrupt++ (%s)\n",
  347. (dwc_otg_is_host_mode(core_if) ? "Host" : "Device"));
  348. DWC_SPINUNLOCK(core_if->lock);
  349. /*
  350. * Need to schedule a work, as there are possible DELAY function calls
  351. * Release lock before scheduling workq as it holds spinlock during scheduling
  352. */
  353. DWC_WORKQ_SCHEDULE(core_if->wq_otg, w_conn_id_status_change,
  354. core_if, "connection id status change");
  355. DWC_SPINLOCK(core_if->lock);
  356. /* Set flag and clear interrupt */
  357. gintsts.b.conidstschng = 1;
  358. DWC_WRITE_REG32(&core_if->core_global_regs->gintsts, gintsts.d32);
  359. return 1;
  360. }
  361. /**
  362. * This interrupt indicates that a device is initiating the Session
  363. * Request Protocol to request the host to turn on bus power so a new
  364. * session can begin. The handler responds by turning on bus power. If
  365. * the DWC_otg controller is in low power mode, the handler brings the
  366. * controller out of low power mode before turning on bus power.
  367. *
  368. * @param core_if Programming view of DWC_otg controller.
  369. */
  370. int32_t dwc_otg_handle_session_req_intr(dwc_otg_core_if_t * core_if)
  371. {
  372. gintsts_data_t gintsts;
  373. #ifndef DWC_HOST_ONLY
  374. DWC_DEBUGPL(DBG_ANY, "++Session Request Interrupt++\n");
  375. if (dwc_otg_is_device_mode(core_if)) {
  376. DWC_PRINTF("SRP: Device mode\n");
  377. core_if->session_valid = 1;
  378. DWC_WORKQ_SCHEDULE(core_if->wq_otg,
  379. charger_detect_work, core_if,
  380. "Charger detect");
  381. } else {
  382. hprt0_data_t hprt0;
  383. DWC_PRINTF("SRP: Host mode\n");
  384. /* Turn on the port power bit. */
  385. hprt0.d32 = dwc_otg_read_hprt0(core_if);
  386. hprt0.b.prtpwr = 1;
  387. DWC_WRITE_REG32(core_if->host_if->hprt0, hprt0.d32);
  388. /* Start the Connection timer. So a message can be displayed
  389. * if connect does not occur within 10 seconds. */
  390. cil_hcd_session_start(core_if);
  391. }
  392. #endif
  393. /* Clear interrupt */
  394. gintsts.d32 = 0;
  395. gintsts.b.sessreqintr = 1;
  396. DWC_WRITE_REG32(&core_if->core_global_regs->gintsts, gintsts.d32);
  397. return 1;
  398. }
  399. void w_wakeup_detected(void *p)
  400. {
  401. dwc_otg_core_if_t *core_if = (dwc_otg_core_if_t *) p;
  402. /*
  403. * Clear the Resume after 70ms. (Need 20 ms minimum. Use 70 ms
  404. * so that OPT tests pass with all PHYs).
  405. */
  406. hprt0_data_t hprt0 = {.d32 = 0 };
  407. #if 0
  408. pcgcctl_data_t pcgcctl = {.d32 = 0 };
  409. /* Restart the Phy Clock */
  410. pcgcctl.b.stoppclk = 1;
  411. DWC_MODIFY_REG32(core_if->pcgcctl, pcgcctl.d32, 0);
  412. dwc_udelay(10);
  413. #endif //0
  414. hprt0.d32 = dwc_otg_read_hprt0(core_if);
  415. DWC_DEBUGPL(DBG_ANY, "Resume: HPRT0=%0x\n", hprt0.d32);
  416. // dwc_mdelay(70);
  417. hprt0.b.prtres = 0; /* Resume */
  418. DWC_WRITE_REG32(core_if->host_if->hprt0, hprt0.d32);
  419. DWC_DEBUGPL(DBG_ANY, "Clear Resume: HPRT0=%0x\n",
  420. DWC_READ_REG32(core_if->host_if->hprt0));
  421. cil_hcd_resume(core_if);
  422. /** Change to L0 state*/
  423. core_if->lx_state = DWC_OTG_L0;
  424. }
  425. /**
  426. * This interrupt indicates that the DWC_otg controller has detected a
  427. * resume or remote wakeup sequence. If the DWC_otg controller is in
  428. * low power mode, the handler must brings the controller out of low
  429. * power mode. The controller automatically begins resume
  430. * signaling. The handler schedules a time to stop resume signaling.
  431. */
  432. int32_t dwc_otg_handle_wakeup_detected_intr(dwc_otg_core_if_t * core_if)
  433. {
  434. gintsts_data_t gintsts;
  435. DWC_DEBUGPL(DBG_ANY,
  436. "++Resume and Remote Wakeup Detected Interrupt++\n");
  437. DWC_PRINTF("%s lxstate = %d\n", __func__, core_if->lx_state);
  438. if (dwc_otg_is_device_mode(core_if)) {
  439. dctl_data_t dctl = {.d32 = 0 };
  440. DWC_DEBUGPL(DBG_PCD, "DSTS=0x%0x\n",
  441. DWC_READ_REG32(&core_if->dev_if->
  442. dev_global_regs->dsts));
  443. if (core_if->lx_state == DWC_OTG_L2) {
  444. #ifdef PARTIAL_POWER_DOWN
  445. if (core_if->hwcfg4.b.power_optimiz) {
  446. pcgcctl_data_t power = {.d32 = 0 };
  447. power.d32 = DWC_READ_REG32(core_if->pcgcctl);
  448. DWC_DEBUGPL(DBG_CIL, "PCGCCTL=%0x\n",
  449. power.d32);
  450. power.b.stoppclk = 0;
  451. DWC_WRITE_REG32(core_if->pcgcctl, power.d32);
  452. power.b.pwrclmp = 0;
  453. DWC_WRITE_REG32(core_if->pcgcctl, power.d32);
  454. power.b.rstpdwnmodule = 0;
  455. DWC_WRITE_REG32(core_if->pcgcctl, power.d32);
  456. }
  457. #endif
  458. /* Clear the Remote Wakeup Signaling */
  459. dctl.b.rmtwkupsig = 1;
  460. DWC_MODIFY_REG32(&core_if->dev_if->
  461. dev_global_regs->dctl, dctl.d32, 0);
  462. DWC_SPINUNLOCK(core_if->lock);
  463. if (core_if->pcd_cb && core_if->pcd_cb->resume_wakeup) {
  464. core_if->pcd_cb->resume_wakeup(core_if->pcd_cb_p);
  465. }
  466. DWC_SPINLOCK(core_if->lock);
  467. } else {
  468. glpmcfg_data_t lpmcfg;
  469. lpmcfg.d32 =
  470. DWC_READ_REG32(&core_if->core_global_regs->glpmcfg);
  471. lpmcfg.b.hird_thres &= (~(1 << 4));
  472. DWC_WRITE_REG32(&core_if->core_global_regs->glpmcfg,
  473. lpmcfg.d32);
  474. }
  475. /** Change to L0 state*/
  476. core_if->lx_state = DWC_OTG_L0;
  477. } else {
  478. if (core_if->lx_state != DWC_OTG_L1) {
  479. pcgcctl_data_t pcgcctl = {.d32 = 0 };
  480. /* Restart the Phy Clock */
  481. pcgcctl.b.stoppclk = 1;
  482. DWC_MODIFY_REG32(core_if->pcgcctl, pcgcctl.d32, 0);
  483. DWC_TIMER_SCHEDULE(core_if->wkp_timer, 71);
  484. } else {
  485. /** Change to L0 state*/
  486. core_if->lx_state = DWC_OTG_L0;
  487. }
  488. }
  489. /* Clear interrupt */
  490. gintsts.d32 = 0;
  491. gintsts.b.wkupintr = 1;
  492. DWC_WRITE_REG32(&core_if->core_global_regs->gintsts, gintsts.d32);
  493. return 1;
  494. }
  495. /**
  496. * This interrupt indicates that the Wakeup Logic has detected a
  497. * Device disconnect.
  498. */
  499. static int32_t dwc_otg_handle_pwrdn_disconnect_intr(dwc_otg_core_if_t *core_if)
  500. {
  501. gpwrdn_data_t gpwrdn = { .d32 = 0 };
  502. gpwrdn_data_t gpwrdn_temp = { .d32 = 0 };
  503. gpwrdn_temp.d32 = DWC_READ_REG32(&core_if->core_global_regs->gpwrdn);
  504. DWC_PRINTF("%s called\n", __FUNCTION__);
  505. if (!core_if->hibernation_suspend) {
  506. DWC_PRINTF("Already exited from Hibernation\n");
  507. return 1;
  508. }
  509. /* Switch on the voltage to the core */
  510. gpwrdn.b.pwrdnswtch = 1;
  511. DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  512. dwc_udelay(10);
  513. /* Reset the core */
  514. gpwrdn.d32 = 0;
  515. gpwrdn.b.pwrdnrstn = 1;
  516. DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  517. dwc_udelay(10);
  518. /* Disable power clamps*/
  519. gpwrdn.d32 = 0;
  520. gpwrdn.b.pwrdnclmp = 1;
  521. DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  522. /* Remove reset the core signal */
  523. gpwrdn.d32 = 0;
  524. gpwrdn.b.pwrdnrstn = 1;
  525. DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, 0, gpwrdn.d32);
  526. dwc_udelay(10);
  527. /* Disable PMU interrupt */
  528. gpwrdn.d32 = 0;
  529. gpwrdn.b.pmuintsel = 1;
  530. DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  531. core_if->hibernation_suspend = 0;
  532. /* Disable PMU */
  533. gpwrdn.d32 = 0;
  534. gpwrdn.b.pmuactv = 1;
  535. DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  536. dwc_udelay(10);
  537. if (gpwrdn_temp.b.idsts) {
  538. core_if->op_state = B_PERIPHERAL;
  539. dwc_otg_core_init(core_if);
  540. dwc_otg_enable_global_interrupts(core_if);
  541. cil_pcd_start(core_if);
  542. } else {
  543. core_if->op_state = A_HOST;
  544. dwc_otg_core_init(core_if);
  545. dwc_otg_enable_global_interrupts(core_if);
  546. cil_hcd_start(core_if);
  547. }
  548. return 1;
  549. }
  550. /**
  551. * This interrupt indicates that the Wakeup Logic has detected a
  552. * remote wakeup sequence.
  553. */
  554. static int32_t dwc_otg_handle_pwrdn_wakeup_detected_intr(dwc_otg_core_if_t * core_if)
  555. {
  556. gpwrdn_data_t gpwrdn = {.d32 = 0 };
  557. DWC_DEBUGPL(DBG_ANY,
  558. "++Powerdown Remote Wakeup Detected Interrupt++\n");
  559. if (!core_if->hibernation_suspend) {
  560. DWC_PRINTF("Already exited from Hibernation\n");
  561. return 1;
  562. }
  563. gpwrdn.d32 = DWC_READ_REG32(&core_if->core_global_regs->gpwrdn);
  564. if (gpwrdn.b.idsts) { // Device Mode
  565. if ((core_if->power_down == 2)
  566. && (core_if->hibernation_suspend == 1)) {
  567. dwc_otg_device_hibernation_restore(core_if, 0, 0);
  568. }
  569. } else {
  570. if ((core_if->power_down == 2)
  571. && (core_if->hibernation_suspend == 1)) {
  572. dwc_otg_host_hibernation_restore(core_if, 1, 0);
  573. }
  574. }
  575. return 1;
  576. }
  577. static int32_t dwc_otg_handle_pwrdn_idsts_change(dwc_otg_device_t *otg_dev)
  578. {
  579. gpwrdn_data_t gpwrdn = {.d32 = 0 };
  580. gpwrdn_data_t gpwrdn_temp = {.d32 = 0 };
  581. dwc_otg_core_if_t *core_if = otg_dev->core_if;
  582. DWC_DEBUGPL(DBG_ANY, "%s called\n", __FUNCTION__);
  583. gpwrdn_temp.d32 = DWC_READ_REG32(&core_if->core_global_regs->gpwrdn);
  584. if (core_if->power_down == 2)
  585. {
  586. if (!core_if->hibernation_suspend) {
  587. DWC_PRINTF("Already exited from Hibernation\n");
  588. return 1;
  589. }
  590. DWC_DEBUGPL(DBG_ANY, "Exit from hibernation on ID sts change\n");
  591. /* Switch on the voltage to the core */
  592. gpwrdn.b.pwrdnswtch = 1;
  593. DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  594. dwc_udelay(10);
  595. /* Reset the core */
  596. gpwrdn.d32 = 0;
  597. gpwrdn.b.pwrdnrstn = 1;
  598. DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  599. dwc_udelay(10);
  600. /* Disable power clamps */
  601. gpwrdn.d32 = 0;
  602. gpwrdn.b.pwrdnclmp = 1;
  603. DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  604. /* Remove reset the core signal */
  605. gpwrdn.d32 = 0;
  606. gpwrdn.b.pwrdnrstn = 1;
  607. DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, 0, gpwrdn.d32);
  608. dwc_udelay(10);
  609. /* Disable PMU interrupt */
  610. gpwrdn.d32 = 0;
  611. gpwrdn.b.pmuintsel = 1;
  612. DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  613. /*Indicates that we are exiting from hibernation */
  614. core_if->hibernation_suspend = 0;
  615. /* Disable PMU */
  616. gpwrdn.d32 = 0;
  617. gpwrdn.b.pmuactv = 1;
  618. DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  619. dwc_udelay(10);
  620. gpwrdn.d32 = core_if->gr_backup->gpwrdn_local;
  621. if (gpwrdn.b.dis_vbus == 1) {
  622. gpwrdn.d32 = 0;
  623. gpwrdn.b.dis_vbus = 1;
  624. DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  625. }
  626. if (gpwrdn_temp.b.idsts) {
  627. core_if->op_state = B_PERIPHERAL;
  628. dwc_otg_core_init(core_if);
  629. dwc_otg_enable_global_interrupts(core_if);
  630. cil_pcd_start(core_if);
  631. } else {
  632. core_if->op_state = A_HOST;
  633. dwc_otg_core_init(core_if);
  634. dwc_otg_enable_global_interrupts(core_if);
  635. cil_hcd_start(core_if);
  636. }
  637. }
  638. if (core_if->adp_enable)
  639. {
  640. uint8_t is_host = 0;
  641. DWC_SPINUNLOCK(core_if->lock);
  642. if (!gpwrdn_temp.b.idsts) {
  643. is_host = 1;
  644. }
  645. #if 0
  646. /* Change the core_if's lock to hcd/pcd lock depend on mode? */
  647. #ifndef DWC_HOST_ONLY
  648. if (gpwrdn_temp.b.idsts)
  649. core_if->lock = otg_dev->pcd->lock;
  650. #endif
  651. #ifndef DWC_DEVICE_ONLY
  652. if (!gpwrdn_temp.b.idsts) {
  653. core_if->lock = otg_dev->hcd->lock;
  654. is_host = 1;
  655. }
  656. #endif
  657. #endif
  658. DWC_PRINTF("RESTART ADP\n");
  659. if (core_if->adp.probe_enabled)
  660. dwc_otg_adp_probe_stop(core_if);
  661. if (core_if->adp.sense_enabled)
  662. dwc_otg_adp_sense_stop(core_if);
  663. if (core_if->adp.sense_timer_started)
  664. DWC_TIMER_CANCEL(core_if->adp.sense_timer);
  665. if (core_if->adp.vbuson_timer_started)
  666. DWC_TIMER_CANCEL(core_if->adp.vbuson_timer);
  667. core_if->adp.probe_timer_values[0] = -1;
  668. core_if->adp.probe_timer_values[1] = -1;
  669. core_if->adp.sense_timer_started = 0;
  670. core_if->adp.vbuson_timer_started = 0;
  671. core_if->adp.probe_counter = 0;
  672. core_if->adp.gpwrdn = 0;
  673. /* Disable PMU and restart ADP */
  674. gpwrdn_temp.d32 = 0;
  675. gpwrdn_temp.b.pmuactv = 1;
  676. gpwrdn_temp.b.pmuintsel = 1;
  677. DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  678. DWC_PRINTF("Check point 1\n");
  679. dwc_mdelay(110);
  680. dwc_otg_adp_start(core_if, is_host);
  681. DWC_SPINLOCK(core_if->lock);
  682. }
  683. return 1;
  684. }
  685. static int32_t dwc_otg_handle_pwrdn_session_change(dwc_otg_core_if_t * core_if)
  686. {
  687. gpwrdn_data_t gpwrdn = {.d32 = 0 };
  688. int32_t otg_cap_param = core_if->core_params->otg_cap;
  689. DWC_DEBUGPL(DBG_ANY, "%s called\n", __FUNCTION__);
  690. gpwrdn.d32 = DWC_READ_REG32(&core_if->core_global_regs->gpwrdn);
  691. if (core_if->power_down == 2) {
  692. if (!core_if->hibernation_suspend) {
  693. DWC_PRINTF("Already exited from Hibernation\n");
  694. return 1;
  695. }
  696. if ((otg_cap_param != DWC_OTG_CAP_PARAM_HNP_SRP_CAPABLE ||
  697. otg_cap_param != DWC_OTG_CAP_PARAM_SRP_ONLY_CAPABLE) &&
  698. gpwrdn.b.bsessvld == 0) {
  699. /* Save gpwrdn register for further usage if stschng interrupt */
  700. core_if->gr_backup->gpwrdn_local =
  701. DWC_READ_REG32(&core_if->core_global_regs->gpwrdn);
  702. /*Exit from ISR and wait for stschng interrupt with bsessvld = 1 */
  703. return 1;
  704. }
  705. /* Switch on the voltage to the core */
  706. gpwrdn.d32 = 0;
  707. gpwrdn.b.pwrdnswtch = 1;
  708. DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  709. dwc_udelay(10);
  710. /* Reset the core */
  711. gpwrdn.d32 = 0;
  712. gpwrdn.b.pwrdnrstn = 1;
  713. DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  714. dwc_udelay(10);
  715. /* Disable power clamps */
  716. gpwrdn.d32 = 0;
  717. gpwrdn.b.pwrdnclmp = 1;
  718. DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  719. /* Remove reset the core signal */
  720. gpwrdn.d32 = 0;
  721. gpwrdn.b.pwrdnrstn = 1;
  722. DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, 0, gpwrdn.d32);
  723. dwc_udelay(10);
  724. /* Disable PMU interrupt */
  725. gpwrdn.d32 = 0;
  726. gpwrdn.b.pmuintsel = 1;
  727. DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  728. dwc_udelay(10);
  729. /*Indicates that we are exiting from hibernation */
  730. core_if->hibernation_suspend = 0;
  731. /* Disable PMU */
  732. gpwrdn.d32 = 0;
  733. gpwrdn.b.pmuactv = 1;
  734. DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  735. dwc_udelay(10);
  736. core_if->op_state = B_PERIPHERAL;
  737. dwc_otg_core_init(core_if);
  738. dwc_otg_enable_global_interrupts(core_if);
  739. cil_pcd_start(core_if);
  740. if (otg_cap_param == DWC_OTG_CAP_PARAM_HNP_SRP_CAPABLE ||
  741. otg_cap_param == DWC_OTG_CAP_PARAM_SRP_ONLY_CAPABLE) {
  742. /*
  743. * Initiate SRP after initial ADP probe.
  744. */
  745. dwc_otg_initiate_srp(core_if);
  746. }
  747. }
  748. return 1;
  749. }
  750. /**
  751. * This interrupt indicates that the Wakeup Logic has detected a
  752. * status change either on IDDIG or BSessVld.
  753. */
  754. static uint32_t dwc_otg_handle_pwrdn_stschng_intr(dwc_otg_device_t *otg_dev)
  755. {
  756. int retval;
  757. gpwrdn_data_t gpwrdn = {.d32 = 0 };
  758. gpwrdn_data_t gpwrdn_temp = {.d32 = 0 };
  759. dwc_otg_core_if_t *core_if = otg_dev->core_if;
  760. DWC_PRINTF("%s called\n", __FUNCTION__);
  761. if (core_if->power_down == 2) {
  762. if (core_if->hibernation_suspend <= 0) {
  763. DWC_PRINTF("Already exited from Hibernation\n");
  764. return 1;
  765. } else
  766. gpwrdn_temp.d32 = core_if->gr_backup->gpwrdn_local;
  767. } else {
  768. gpwrdn_temp.d32 = core_if->adp.gpwrdn;
  769. }
  770. gpwrdn.d32 = DWC_READ_REG32(&core_if->core_global_regs->gpwrdn);
  771. DWC_PRINTF("gpwrdn.d32 = %x\n", gpwrdn.d32);
  772. if (gpwrdn.b.idsts ^ gpwrdn_temp.b.idsts) {
  773. retval = dwc_otg_handle_pwrdn_idsts_change(otg_dev);
  774. } else if (gpwrdn.b.bsessvld ^ gpwrdn_temp.b.bsessvld) {
  775. retval = dwc_otg_handle_pwrdn_session_change(core_if);
  776. }
  777. return retval;
  778. }
  779. /**
  780. * This interrupt indicates that the Wakeup Logic has detected a
  781. * SRP.
  782. */
  783. static int32_t dwc_otg_handle_pwrdn_srp_intr(dwc_otg_core_if_t * core_if)
  784. {
  785. gpwrdn_data_t gpwrdn = {.d32 = 0 };
  786. DWC_PRINTF("%s called\n", __FUNCTION__);
  787. if (!core_if->hibernation_suspend) {
  788. DWC_PRINTF("Already exited from Hibernation\n");
  789. return 1;
  790. }
  791. #ifdef DWC_DEV_SRPCAP
  792. if (core_if->pwron_timer_started) {
  793. core_if->pwron_timer_started = 0;
  794. DWC_TIMER_CANCEL(core_if->pwron_timer);
  795. }
  796. #endif
  797. /* Switch on the voltage to the core */
  798. gpwrdn.b.pwrdnswtch = 1;
  799. DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  800. dwc_udelay(10);
  801. /* Reset the core */
  802. gpwrdn.d32 = 0;
  803. gpwrdn.b.pwrdnrstn = 1;
  804. DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  805. dwc_udelay(10);
  806. /* Disable power clamps */
  807. gpwrdn.d32 = 0;
  808. gpwrdn.b.pwrdnclmp = 1;
  809. DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  810. /* Remove reset the core signal */
  811. gpwrdn.d32 = 0;
  812. gpwrdn.b.pwrdnrstn = 1;
  813. DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, 0, gpwrdn.d32);
  814. dwc_udelay(10);
  815. /* Disable PMU interrupt */
  816. gpwrdn.d32 = 0;
  817. gpwrdn.b.pmuintsel = 1;
  818. DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  819. /* Indicates that we are exiting from hibernation */
  820. core_if->hibernation_suspend = 0;
  821. /* Disable PMU */
  822. gpwrdn.d32 = 0;
  823. gpwrdn.b.pmuactv = 1;
  824. DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  825. dwc_udelay(10);
  826. /* Programm Disable VBUS to 0 */
  827. gpwrdn.d32 = 0;
  828. gpwrdn.b.dis_vbus = 1;
  829. DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  830. /*Initialize the core as Host */
  831. core_if->op_state = A_HOST;
  832. dwc_otg_core_init(core_if);
  833. dwc_otg_enable_global_interrupts(core_if);
  834. cil_hcd_start(core_if);
  835. return 1;
  836. }
  837. /** This interrupt indicates that restore command after Hibernation
  838. * was completed by the core. */
  839. int32_t dwc_otg_handle_restore_done_intr(dwc_otg_core_if_t * core_if)
  840. {
  841. pcgcctl_data_t pcgcctl;
  842. DWC_DEBUGPL(DBG_ANY, "++Restore Done Interrupt++\n");
  843. //TODO De-assert restore signal. 8.a
  844. pcgcctl.d32 = DWC_READ_REG32(core_if->pcgcctl);
  845. if (pcgcctl.b.restoremode == 1) {
  846. gintmsk_data_t gintmsk = {.d32 = 0 };
  847. /*
  848. * If restore mode is Remote Wakeup,
  849. * unmask Remote Wakeup interrupt.
  850. */
  851. gintmsk.b.wkupintr = 1;
  852. DWC_MODIFY_REG32(&core_if->core_global_regs->gintmsk,
  853. 0, gintmsk.d32);
  854. }
  855. return 1;
  856. }
  857. /**
  858. * This interrupt indicates that a device has been disconnected from
  859. * the root port.
  860. */
  861. int32_t dwc_otg_handle_disconnect_intr(dwc_otg_core_if_t * core_if)
  862. {
  863. gintsts_data_t gintsts;
  864. DWC_DEBUGPL(DBG_ANY, "++Disconnect Detected Interrupt++ (%s) %s\n",
  865. (dwc_otg_is_host_mode(core_if) ? "Host" : "Device"),
  866. op_state_str(core_if));
  867. /** @todo Consolidate this if statement. */
  868. #ifndef DWC_HOST_ONLY
  869. if (core_if->op_state == B_HOST) {
  870. /* If in device mode Disconnect and stop the HCD, then
  871. * start the PCD. */
  872. DWC_SPINUNLOCK(core_if->lock);
  873. cil_hcd_disconnect(core_if);
  874. cil_pcd_start(core_if);
  875. DWC_SPINLOCK(core_if->lock);
  876. core_if->op_state = B_PERIPHERAL;
  877. } else if (dwc_otg_is_device_mode(core_if)) {
  878. gotgctl_data_t gotgctl = {.d32 = 0 };
  879. gotgctl.d32 =
  880. DWC_READ_REG32(&core_if->core_global_regs->gotgctl);
  881. if (gotgctl.b.hstsethnpen == 1) {
  882. /* Do nothing, if HNP in process the OTG
  883. * interrupt "Host Negotiation Detected"
  884. * interrupt will do the mode switch.
  885. */
  886. } else if (gotgctl.b.devhnpen == 0) {
  887. /* If in device mode Disconnect and stop the HCD, then
  888. * start the PCD. */
  889. DWC_SPINUNLOCK(core_if->lock);
  890. cil_hcd_disconnect(core_if);
  891. cil_pcd_start(core_if);
  892. DWC_SPINLOCK(core_if->lock);
  893. core_if->op_state = B_PERIPHERAL;
  894. } else {
  895. DWC_DEBUGPL(DBG_ANY, "!a_peripheral && !devhnpen\n");
  896. }
  897. } else {
  898. if (core_if->op_state == A_HOST) {
  899. /* A-Cable still connected but device disconnected. */
  900. cil_hcd_disconnect(core_if);
  901. if (core_if->adp_enable) {
  902. gpwrdn_data_t gpwrdn = { .d32 = 0 };
  903. cil_hcd_stop(core_if);
  904. /* Enable Power Down Logic */
  905. gpwrdn.b.pmuintsel = 1;
  906. gpwrdn.b.pmuactv = 1;
  907. DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, 0, gpwrdn.d32);
  908. dwc_otg_adp_probe_start(core_if);
  909. /* Power off the core */
  910. if (core_if->power_down == 2) {
  911. gpwrdn.d32 = 0;
  912. gpwrdn.b.pwrdnswtch = 1;
  913. DWC_MODIFY_REG32(&core_if->
  914. core_global_regs->
  915. gpwrdn, gpwrdn.d32, 0);
  916. }
  917. }
  918. }
  919. }
  920. #endif
  921. /* Change to L3(OFF) state */
  922. core_if->lx_state = DWC_OTG_L3;
  923. gintsts.d32 = 0;
  924. gintsts.b.disconnect = 1;
  925. DWC_WRITE_REG32(&core_if->core_global_regs->gintsts, gintsts.d32);
  926. return 1;
  927. }
  928. /**
  929. * This interrupt indicates that SUSPEND state has been detected on
  930. * the USB.
  931. *
  932. * For HNP the USB Suspend interrupt signals the change from
  933. * "a_peripheral" to "a_host".
  934. *
  935. * When power management is enabled the core will be put in low power
  936. * mode.
  937. */
  938. int32_t dwc_otg_handle_usb_suspend_intr(dwc_otg_core_if_t * core_if)
  939. {
  940. dsts_data_t dsts;
  941. gintsts_data_t gintsts;
  942. dcfg_data_t dcfg;
  943. DWC_DEBUGPL(DBG_ANY, "USB SUSPEND\n");
  944. if (dwc_otg_is_device_mode(core_if)) {
  945. /* Check the Device status register to determine if the Suspend
  946. * state is active. */
  947. dsts.d32 =
  948. DWC_READ_REG32(&core_if->dev_if->dev_global_regs->dsts);
  949. DWC_DEBUGPL(DBG_PCD, "DSTS=0x%0x\n", dsts.d32);
  950. DWC_DEBUGPL(DBG_PCD, "DSTS.Suspend Status=%d "
  951. "HWCFG4.power Optimize=%d\n",
  952. dsts.b.suspsts, core_if->hwcfg4.b.power_optimiz);
  953. #ifdef PARTIAL_POWER_DOWN
  954. /** @todo Add a module parameter for power management. */
  955. if (dsts.b.suspsts && core_if->hwcfg4.b.power_optimiz) {
  956. pcgcctl_data_t power = {.d32 = 0 };
  957. DWC_DEBUGPL(DBG_CIL, "suspend\n");
  958. power.b.pwrclmp = 1;
  959. DWC_WRITE_REG32(core_if->pcgcctl, power.d32);
  960. power.b.rstpdwnmodule = 1;
  961. DWC_MODIFY_REG32(core_if->pcgcctl, 0, power.d32);
  962. power.b.stoppclk = 1;
  963. DWC_MODIFY_REG32(core_if->pcgcctl, 0, power.d32);
  964. } else {
  965. DWC_DEBUGPL(DBG_ANY, "disconnect?\n");
  966. }
  967. #endif
  968. /* PCD callback for suspend. Release the lock inside of callback function */
  969. cil_pcd_suspend(core_if);
  970. if (core_if->power_down == 2)
  971. {
  972. dcfg.d32 = DWC_READ_REG32(&core_if->dev_if->dev_global_regs->dcfg);
  973. DWC_DEBUGPL(DBG_ANY,"lx_state = %08x\n",core_if->lx_state);
  974. DWC_DEBUGPL(DBG_ANY," device address = %08d\n",dcfg.b.devaddr);
  975. if (core_if->lx_state != DWC_OTG_L3 && dcfg.b.devaddr) {
  976. pcgcctl_data_t pcgcctl = {.d32 = 0 };
  977. gpwrdn_data_t gpwrdn = {.d32 = 0 };
  978. gusbcfg_data_t gusbcfg = {.d32 = 0 };
  979. /* Change to L2(suspend) state */
  980. core_if->lx_state = DWC_OTG_L2;
  981. /* Clear interrupt in gintsts */
  982. gintsts.d32 = 0;
  983. gintsts.b.usbsuspend = 1;
  984. DWC_WRITE_REG32(&core_if->core_global_regs->
  985. gintsts, gintsts.d32);
  986. DWC_PRINTF("Start of hibernation completed\n");
  987. dwc_otg_save_global_regs(core_if);
  988. dwc_otg_save_dev_regs(core_if);
  989. gusbcfg.d32 =
  990. DWC_READ_REG32(&core_if->core_global_regs->
  991. gusbcfg);
  992. if (gusbcfg.b.ulpi_utmi_sel == 1) {
  993. /* ULPI interface */
  994. /* Suspend the Phy Clock */
  995. pcgcctl.d32 = 0;
  996. pcgcctl.b.stoppclk = 1;
  997. DWC_MODIFY_REG32(core_if->pcgcctl, 0,
  998. pcgcctl.d32);
  999. dwc_udelay(10);
  1000. gpwrdn.b.pmuactv = 1;
  1001. DWC_MODIFY_REG32(&core_if->
  1002. core_global_regs->
  1003. gpwrdn, 0, gpwrdn.d32);
  1004. } else {
  1005. /* UTMI+ Interface */
  1006. gpwrdn.b.pmuactv = 1;
  1007. DWC_MODIFY_REG32(&core_if->
  1008. core_global_regs->
  1009. gpwrdn, 0, gpwrdn.d32);
  1010. dwc_udelay(10);
  1011. pcgcctl.b.stoppclk = 1;
  1012. DWC_MODIFY_REG32(core_if->pcgcctl, 0,
  1013. pcgcctl.d32);
  1014. dwc_udelay(10);
  1015. }
  1016. /* Set flag to indicate that we are in hibernation */
  1017. core_if->hibernation_suspend = 1;
  1018. /* Enable interrupts from wake up logic */
  1019. gpwrdn.d32 = 0;
  1020. gpwrdn.b.pmuintsel = 1;
  1021. DWC_MODIFY_REG32(&core_if->core_global_regs->
  1022. gpwrdn, 0, gpwrdn.d32);
  1023. dwc_udelay(10);
  1024. /* Unmask device mode interrupts in GPWRDN */
  1025. gpwrdn.d32 = 0;
  1026. gpwrdn.b.rst_det_msk = 1;
  1027. gpwrdn.b.lnstchng_msk = 1;
  1028. gpwrdn.b.sts_chngint_msk = 1;
  1029. DWC_MODIFY_REG32(&core_if->core_global_regs->
  1030. gpwrdn, 0, gpwrdn.d32);
  1031. dwc_udelay(10);
  1032. /* Enable Power Down Clamp */
  1033. gpwrdn.d32 = 0;
  1034. gpwrdn.b.pwrdnclmp = 1;
  1035. DWC_MODIFY_REG32(&core_if->core_global_regs->
  1036. gpwrdn, 0, gpwrdn.d32);
  1037. dwc_udelay(10);
  1038. /* Switch off VDD */
  1039. gpwrdn.d32 = 0;
  1040. gpwrdn.b.pwrdnswtch = 1;
  1041. DWC_MODIFY_REG32(&core_if->core_global_regs->
  1042. gpwrdn, 0, gpwrdn.d32);
  1043. /* Save gpwrdn register for further usage if stschng interrupt */
  1044. core_if->gr_backup->gpwrdn_local =
  1045. DWC_READ_REG32(&core_if->core_global_regs->gpwrdn);
  1046. DWC_PRINTF("Hibernation completed\n");
  1047. return 1;
  1048. }
  1049. }
  1050. } else {
  1051. if (core_if->op_state == A_PERIPHERAL) {
  1052. DWC_DEBUGPL(DBG_ANY, "a_peripheral->a_host\n");
  1053. /* Clear the a_peripheral flag, back to a_host. */
  1054. DWC_SPINUNLOCK(core_if->lock);
  1055. cil_pcd_stop(core_if);
  1056. cil_hcd_start(core_if);
  1057. DWC_SPINLOCK(core_if->lock);
  1058. core_if->op_state = A_HOST;
  1059. }
  1060. }
  1061. /* Change to L2(suspend) state */
  1062. core_if->lx_state = DWC_OTG_L2;
  1063. /* Clear interrupt */
  1064. gintsts.d32 = 0;
  1065. gintsts.b.usbsuspend = 1;
  1066. DWC_WRITE_REG32(&core_if->core_global_regs->gintsts, gintsts.d32);
  1067. return 1;
  1068. }
  1069. #ifdef CONFIG_USB_DWC_OTG_LPM
  1070. /**
  1071. * This function hadles LPM transaction received interrupt.
  1072. */
  1073. static int32_t dwc_otg_handle_lpm_intr(dwc_otg_core_if_t * core_if)
  1074. {
  1075. glpmcfg_data_t lpmcfg;
  1076. gintsts_data_t gintsts;
  1077. if (!core_if->core_params->lpm_enable) {
  1078. DWC_PRINTF("Unexpected LPM interrupt\n");
  1079. }
  1080. lpmcfg.d32 = DWC_READ_REG32(&core_if->core_global_regs->glpmcfg);
  1081. DWC_PRINTF("LPM config register = 0x%08x\n", lpmcfg.d32);
  1082. if (dwc_otg_is_host_mode(core_if)) {
  1083. cil_hcd_sleep(core_if);
  1084. } else {
  1085. lpmcfg.b.hird_thres |= (1 << 4);
  1086. DWC_WRITE_REG32(&core_if->core_global_regs->glpmcfg,
  1087. lpmcfg.d32);
  1088. }
  1089. /* Examine prt_sleep_sts after TL1TokenTetry period max (10 us) */
  1090. dwc_udelay(10);
  1091. lpmcfg.d32 = DWC_READ_REG32(&core_if->core_global_regs->glpmcfg);
  1092. if (lpmcfg.b.prt_sleep_sts) {
  1093. /* Save the current state */
  1094. core_if->lx_state = DWC_OTG_L1;
  1095. }
  1096. /* Clear interrupt */
  1097. gintsts.d32 = 0;
  1098. gintsts.b.lpmtranrcvd = 1;
  1099. DWC_WRITE_REG32(&core_if->core_global_regs->gintsts, gintsts.d32);
  1100. return 1;
  1101. }
  1102. #endif /* CONFIG_USB_DWC_OTG_LPM */
  1103. /**
  1104. * This function returns the Core Interrupt register.
  1105. */
  1106. static inline uint32_t dwc_otg_read_common_intr(dwc_otg_core_if_t * core_if)
  1107. {
  1108. gahbcfg_data_t gahbcfg = {.d32 = 0 };
  1109. gintsts_data_t gintsts;
  1110. gintmsk_data_t gintmsk;
  1111. gintmsk_data_t gintmsk_common = {.d32 = 0 };
  1112. gintmsk_common.b.wkupintr = 1;
  1113. gintmsk_common.b.sessreqintr = 1;
  1114. gintmsk_common.b.conidstschng = 1;
  1115. gintmsk_common.b.otgintr = 1;
  1116. gintmsk_common.b.modemismatch = 1;
  1117. gintmsk_common.b.disconnect = 1;
  1118. gintmsk_common.b.usbsuspend = 1;
  1119. #ifdef CONFIG_USB_DWC_OTG_LPM
  1120. gintmsk_common.b.lpmtranrcvd = 1;
  1121. #endif
  1122. gintmsk_common.b.restoredone = 1;
  1123. /** @todo: The port interrupt occurs while in device
  1124. * mode. Added code to CIL to clear the interrupt for now!
  1125. */
  1126. gintmsk_common.b.portintr = 1;
  1127. gintsts.d32 = DWC_READ_REG32(&core_if->core_global_regs->gintsts);
  1128. gintmsk.d32 = DWC_READ_REG32(&core_if->core_global_regs->gintmsk);
  1129. gahbcfg.d32 = DWC_READ_REG32(&core_if->core_global_regs->gahbcfg);
  1130. #ifdef DEBUG
  1131. /* if any common interrupts set */
  1132. if (gintsts.d32 & gintmsk_common.d32) {
  1133. DWC_DEBUGPL(DBG_ANY, "gintsts=%08x gintmsk=%08x\n",
  1134. gintsts.d32, gintmsk.d32);
  1135. }
  1136. #endif
  1137. if (gahbcfg.b.glblintrmsk)
  1138. return ((gintsts.d32 & gintmsk.d32) & gintmsk_common.d32);
  1139. else
  1140. return 0;
  1141. }
  1142. /* MACRO for clearing interupt bits in GPWRDN register */
  1143. #define CLEAR_GPWRDN_INTR(__core_if,__intr) \
  1144. do { \
  1145. gpwrdn_data_t gpwrdn = {.d32=0}; \
  1146. gpwrdn.b.__intr = 1; \
  1147. DWC_MODIFY_REG32(&__core_if->core_global_regs->gpwrdn, \
  1148. 0, gpwrdn.d32); \
  1149. } while (0)
  1150. /**
  1151. * Common interrupt handler.
  1152. *
  1153. * The common interrupts are those that occur in both Host and Device mode.
  1154. * This handler handles the following interrupts:
  1155. * - Mode Mismatch Interrupt
  1156. * - Disconnect Interrupt
  1157. * - OTG Interrupt
  1158. * - Connector ID Status Change Interrupt
  1159. * - Session Request Interrupt.
  1160. * - Resume / Remote Wakeup Detected Interrupt.
  1161. * - LPM Transaction Received Interrupt
  1162. * - ADP Transaction Received Interrupt
  1163. *
  1164. */
  1165. int32_t dwc_otg_handle_common_intr(void *dev)
  1166. {
  1167. int retval = 0;
  1168. gintsts_data_t gintsts;
  1169. gpwrdn_data_t gpwrdn = {.d32 = 0 };
  1170. dwc_otg_device_t *otg_dev = dev;
  1171. dwc_otg_core_if_t *core_if = otg_dev->core_if;
  1172. gpwrdn.d32 = DWC_READ_REG32(&core_if->core_global_regs->gpwrdn);
  1173. if (dwc_otg_is_device_mode(core_if))
  1174. core_if->frame_num = dwc_otg_get_frame_number(core_if);
  1175. if (core_if->lock)
  1176. DWC_SPINLOCK(core_if->lock);
  1177. if (core_if->hibernation_suspend <= 0) {
  1178. gintsts.d32 = dwc_otg_read_common_intr(core_if);
  1179. if (gintsts.b.modemismatch) {
  1180. retval |= dwc_otg_handle_mode_mismatch_intr(core_if);
  1181. }
  1182. if (gintsts.b.otgintr) {
  1183. retval |= dwc_otg_handle_otg_intr(core_if);
  1184. }
  1185. if (gintsts.b.conidstschng) {
  1186. retval |= dwc_otg_handle_conn_id_status_change_intr(core_if);
  1187. }
  1188. if (gintsts.b.disconnect) {
  1189. retval |= dwc_otg_handle_disconnect_intr(core_if);
  1190. }
  1191. if (gintsts.b.sessreqintr) {
  1192. retval |= dwc_otg_handle_session_req_intr(core_if);
  1193. }
  1194. if (gintsts.b.wkupintr) {
  1195. retval |= dwc_otg_handle_wakeup_detected_intr(core_if);
  1196. }
  1197. if (gintsts.b.usbsuspend) {
  1198. retval |= dwc_otg_handle_usb_suspend_intr(core_if);
  1199. }
  1200. #ifdef CONFIG_USB_DWC_OTG_LPM
  1201. if (gintsts.b.lpmtranrcvd) {
  1202. retval |= dwc_otg_handle_lpm_intr(core_if);
  1203. }
  1204. #endif
  1205. if (gintsts.b.restoredone) {
  1206. gintsts.d32 = 0;
  1207. if (core_if->power_down == 2)
  1208. core_if->hibernation_suspend = -1;
  1209. gintsts.b.restoredone = 1;
  1210. DWC_WRITE_REG32(&core_if->core_global_regs->gintsts,gintsts.d32);
  1211. DWC_PRINTF(" --Restore done interrupt received-- \n");
  1212. retval |= 1;
  1213. }
  1214. if (gintsts.b.portintr && dwc_otg_is_device_mode(core_if)) {
  1215. /* The port interrupt occurs while in device mode with HPRT0
  1216. * Port Enable/Disable.
  1217. */
  1218. gintsts.d32 = 0;
  1219. gintsts.b.portintr = 1;
  1220. DWC_WRITE_REG32(&core_if->core_global_regs->gintsts,gintsts.d32);
  1221. retval |= 1;
  1222. }
  1223. } else {
  1224. DWC_DEBUGPL(DBG_ANY, "gpwrdn=%08x\n", gpwrdn.d32);
  1225. if (gpwrdn.b.disconn_det && gpwrdn.b.disconn_det_msk) {
  1226. CLEAR_GPWRDN_INTR(core_if, disconn_det);
  1227. if (gpwrdn.b.linestate == 0) {
  1228. dwc_otg_handle_pwrdn_disconnect_intr(core_if);
  1229. } else {
  1230. DWC_PRINTF("Disconnect detected while linestate is not 0\n");
  1231. }
  1232. retval |= 1;
  1233. }
  1234. if (gpwrdn.b.lnstschng && gpwrdn.b.lnstchng_msk) {
  1235. CLEAR_GPWRDN_INTR(core_if, lnstschng);
  1236. /* remote wakeup from hibernation */
  1237. if (gpwrdn.b.linestate == 2 || gpwrdn.b.linestate == 1) {
  1238. dwc_otg_handle_pwrdn_wakeup_detected_intr(core_if);
  1239. } else {
  1240. DWC_PRINTF("gpwrdn.linestate = %d\n", gpwrdn.b.linestate);
  1241. }
  1242. retval |= 1;
  1243. }
  1244. if (gpwrdn.b.rst_det && gpwrdn.b.rst_det_msk) {
  1245. CLEAR_GPWRDN_INTR(core_if, rst_det);
  1246. if (gpwrdn.b.linestate == 0) {
  1247. DWC_PRINTF("Reset detected\n");
  1248. retval |= dwc_otg_device_hibernation_restore(core_if, 0, 1);
  1249. }
  1250. }
  1251. if (gpwrdn.b.srp_det && gpwrdn.b.srp_det_msk) {
  1252. CLEAR_GPWRDN_INTR(core_if, srp_det);
  1253. dwc_otg_handle_pwrdn_srp_intr(core_if);
  1254. retval |= 1;
  1255. }
  1256. }
  1257. /* Handle ADP interrupt here */
  1258. if (gpwrdn.b.adp_int) {
  1259. DWC_PRINTF("ADP interrupt\n");
  1260. CLEAR_GPWRDN_INTR(core_if, adp_int);
  1261. dwc_otg_adp_handle_intr(core_if);
  1262. retval |= 1;
  1263. }
  1264. if (gpwrdn.b.sts_chngint && gpwrdn.b.sts_chngint_msk) {
  1265. DWC_PRINTF("STS CHNG interrupt asserted\n");
  1266. CLEAR_GPWRDN_INTR(core_if, sts_chngint);
  1267. dwc_otg_handle_pwrdn_stschng_intr(otg_dev);
  1268. retval |= 1;
  1269. }
  1270. if (core_if->lock)
  1271. DWC_SPINUNLOCK(core_if->lock);
  1272. return retval;
  1273. }