usb: dwc_otg_310: fix bad unlock balance issue
[firefly-linux-kernel-4.4.55.git] / drivers / usb / dwc_otg_310 / dwc_otg_cil_intr.c
1 /* ==========================================================================
2  * $File: //dwh/usb_iip/dev/software/otg/linux/drivers/dwc_otg_cil_intr.c $
3  * $Revision: #35 $
4  * $Date: 2012/12/21 $
5  * $Change: 2131568 $
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
34 /** @file
35  *
36  * The Core Interface Layer provides basic services for accessing and
37  * managing the DWC_otg hardware. These services are used by both the
38  * Host Controller Driver and the Peripheral Controller Driver.
39  *
40  * This file contains the Common Interrupt handlers.
41  */
42 #include "common_port/dwc_os.h"
43 #include "dwc_otg_regs.h"
44 #include "dwc_otg_cil.h"
45 #include "dwc_otg_driver.h"
46 #include "dwc_otg_pcd.h"
47 #include "dwc_otg_hcd.h"
48 #include "usbdev_rk.h"
49
50 #ifdef DEBUG
51 inline const char *op_state_str(dwc_otg_core_if_t *core_if)
52 {
53         return (core_if->op_state == A_HOST ? "a_host" :
54                 (core_if->op_state == A_SUSPEND ? "a_suspend" :
55                  (core_if->op_state == A_PERIPHERAL ? "a_peripheral" :
56                   (core_if->op_state == B_PERIPHERAL ? "b_peripheral" :
57                    (core_if->op_state == B_HOST ? "b_host" : "unknown")))));
58 }
59 #endif
60
61 /** This function will log a debug message
62  *
63  * @param core_if Programming view of DWC_otg controller.
64  */
65 int32_t dwc_otg_handle_mode_mismatch_intr(dwc_otg_core_if_t *core_if)
66 {
67         gintsts_data_t gintsts;
68         DWC_WARN("Mode Mismatch Interrupt: currently in %s mode\n",
69                  dwc_otg_mode(core_if) ? "Host" : "Device");
70
71         /* Clear interrupt */
72         gintsts.d32 = 0;
73         gintsts.b.modemismatch = 1;
74         DWC_WRITE_REG32(&core_if->core_global_regs->gintsts, gintsts.d32);
75         return 1;
76 }
77
78 /**
79  * This function handles the OTG Interrupts. It reads the OTG
80  * Interrupt Register (GOTGINT) to determine what interrupt has
81  * occurred.
82  *
83  * @param core_if Programming view of DWC_otg controller.
84  */
85 int32_t dwc_otg_handle_otg_intr(dwc_otg_core_if_t *core_if)
86 {
87         dwc_otg_core_global_regs_t *global_regs = core_if->core_global_regs;
88         gotgint_data_t gotgint;
89         gotgctl_data_t gotgctl;
90         gintmsk_data_t gintmsk;
91         gpwrdn_data_t gpwrdn;
92         dctl_data_t dctl = {.d32 = 0 };
93
94         gotgint.d32 = DWC_READ_REG32(&global_regs->gotgint);
95         gotgctl.d32 = DWC_READ_REG32(&global_regs->gotgctl);
96         DWC_DEBUGPL(DBG_CIL, "++OTG Interrupt gotgint=%0x [%s]\n", gotgint.d32,
97                     op_state_str(core_if));
98
99         if (gotgint.b.sesenddet) {
100                 DWC_DEBUGPL(DBG_ANY, " ++OTG Interrupt: "
101                             "Session End Detected++ (%s)\n",
102                             op_state_str(core_if));
103
104                 /* do soft disconnect */
105                 dctl.d32 =
106                     DWC_READ_REG32(&core_if->dev_if->dev_global_regs->dctl);
107                 dctl.b.sftdiscon = 1;
108                 DWC_WRITE_REG32(&core_if->dev_if->dev_global_regs->dctl,
109                                 dctl.d32);
110                 dwc_otg_disable_global_interrupts(core_if);
111                 core_if->otg_dev->pcd->vbus_status = USB_BC_TYPE_DISCNT;
112
113                 DWC_PRINTF("********session end ,soft disconnect***********\n");
114
115                 gotgctl.d32 = DWC_READ_REG32(&global_regs->gotgctl);
116
117                 if (core_if->op_state == B_HOST) {
118                         cil_pcd_start(core_if);
119                         core_if->op_state = B_PERIPHERAL;
120                 } else {
121                         /* If not B_HOST and Device HNP still set. HNP
122                          * Did not succeed!*/
123                         if (gotgctl.b.devhnpen) {
124                                 DWC_DEBUGPL(DBG_ANY, "Session End Detected\n");
125                                 __DWC_ERROR
126                                     ("Device Not Connected/Responding!\n");
127                         }
128
129                         /* If Session End Detected the B-Cable has
130                          * been disconnected. */
131                         /* Reset PCD and Gadget driver to a
132                          * clean state. */
133                         core_if->lx_state = DWC_OTG_L0;
134                         DWC_SPINUNLOCK(core_if->lock);
135                         cil_pcd_stop(core_if);
136                         DWC_SPINLOCK(core_if->lock);
137
138                         if (core_if->otg_ver) {
139                                 /** PET testing*/
140                                 gotgctl.d32 = 0;
141                                 gotgctl.b.devhnpen = 1;
142                                 DWC_MODIFY_REG32(&global_regs->gotgctl,
143                                                  gotgctl.d32, 0);
144                                 if (core_if->test_mode == 6) {
145                                         /* manukz: old value was 50 */
146                                         DWC_WORKQ_SCHEDULE_DELAYED(core_if->wq_otg,
147                                                                    dwc_otg_initiate_srp,
148                                                                    core_if, 3000,
149                                                                    "initate SRP");
150                                         core_if->test_mode = 0;
151                                 } else if (core_if->adp_enable) {
152                                         if (core_if->power_down == 2) {
153                                                 gpwrdn.d32 = 0;
154                                                 gpwrdn.b.pwrdnswtch = 1;
155                                                 DWC_MODIFY_REG32
156                                                     (&core_if->core_global_regs->gpwrdn,
157                                                      gpwrdn.d32, 0);
158                                         }
159
160                                         gpwrdn.d32 = 0;
161                                         gpwrdn.b.pmuintsel = 1;
162                                         gpwrdn.b.pmuactv = 1;
163                                         DWC_MODIFY_REG32(&core_if->
164                                                          core_global_regs->
165                                                          gpwrdn, 0, gpwrdn.d32);
166                                         dwc_otg_adp_sense_start(core_if);
167                                 }
168                         }
169                 }
170                 if (core_if->otg_ver == 0) {
171                         gotgctl.d32 = 0;
172                         gotgctl.b.devhnpen = 1;
173                         DWC_MODIFY_REG32(&global_regs->gotgctl, gotgctl.d32, 0);
174                 }
175         }
176         if (gotgint.b.sesreqsucstschng) {
177                 DWC_DEBUGPL(DBG_ANY, " ++OTG Interrupt: "
178                             "Session Reqeust Success Status Change++\n");
179                 gotgctl.d32 = DWC_READ_REG32(&global_regs->gotgctl);
180                 if (gotgctl.b.sesreqscs) {
181
182                         if ((core_if->core_params->phy_type ==
183                              DWC_PHY_TYPE_PARAM_FS)
184                             && (core_if->core_params->i2c_enable)) {
185                                 core_if->srp_success = 1;
186                         } else {
187                                 DWC_SPINUNLOCK(core_if->lock);
188                                 cil_pcd_resume(core_if);
189                                 DWC_SPINLOCK(core_if->lock);
190                                 /* Clear Session Request */
191                                 gotgctl.d32 = 0;
192                                 gotgctl.b.sesreq = 1;
193                                 DWC_MODIFY_REG32(&global_regs->gotgctl,
194                                                  gotgctl.d32, 0);
195                         }
196                 }
197         }
198         if (gotgint.b.hstnegsucstschng) {
199                 /* Print statements during the HNP interrupt handling
200                  * can cause it to fail.*/
201                 gotgctl.d32 = DWC_READ_REG32(&global_regs->gotgctl);
202                 /* WA for 3.00a- HW is not setting cur_mode, even sometimes
203                  * this does not help*/
204                 if (core_if->snpsid >= OTG_CORE_REV_3_00a)
205                         dwc_udelay(100);
206                 if (gotgctl.b.hstnegscs) {
207                         if (dwc_otg_is_host_mode(core_if)) {
208                                 core_if->op_state = B_HOST;
209                                 /*
210                                  * Need to disable SOF interrupt immediately.
211                                  * When switching from device to host, the PCD
212                                  * interrupt handler won't handle the
213                                  * interrupt if host mode is already set. The
214                                  * HCD interrupt handler won't get called if
215                                  * the HCD state is HALT. This means that the
216                                  * interrupt does not get handled and Linux
217                                  * complains loudly.
218                                  */
219                                 gintmsk.d32 = 0;
220                                 gintmsk.b.sofintr = 1;
221                                 /* gintmsk.b.usbsuspend = 1; */
222                                 DWC_MODIFY_REG32(&global_regs->gintmsk,
223                                                  gintmsk.d32, 0);
224                                 /* Call callback function with spin lock released */
225                                 DWC_SPINUNLOCK(core_if->lock);
226                                 cil_pcd_stop(core_if);
227                                 /*
228                                  * Initialize the Core for Host mode.
229                                  */
230                                 cil_hcd_start(core_if);
231                                 DWC_SPINLOCK(core_if->lock);
232                         }
233                 } else {
234                         gotgctl.d32 = 0;
235                         gotgctl.b.hnpreq = 1;
236                         gotgctl.b.devhnpen = 1;
237                         DWC_MODIFY_REG32(&global_regs->gotgctl, gotgctl.d32, 0);
238                         DWC_DEBUGPL(DBG_ANY, "HNP Failed\n");
239                         __DWC_ERROR("Device Not Connected/Responding\n");
240                 }
241         }
242         if (gotgint.b.hstnegdet) {
243                 /* The disconnect interrupt is set at the same time as
244                  * Host Negotiation Detected.  During the mode
245                  * switch all interrupts are cleared so the disconnect
246                  * interrupt handler will not get executed.
247                  */
248                 DWC_DEBUGPL(DBG_ANY, " ++OTG Interrupt: "
249                             "Host Negotiation Detected++ (%s)\n",
250                             (dwc_otg_is_host_mode(core_if) ? "Host" :
251                              "Device"));
252                 if (dwc_otg_is_device_mode(core_if)) {
253                         DWC_DEBUGPL(DBG_ANY, "a_suspend->a_peripheral (%d)\n",
254                                     core_if->op_state);
255                         DWC_SPINUNLOCK(core_if->lock);
256                         cil_hcd_disconnect(core_if);
257                         cil_pcd_start(core_if);
258                         DWC_SPINLOCK(core_if->lock);
259                         core_if->op_state = A_PERIPHERAL;
260                 } else {
261                         /*
262                          * Need to disable SOF interrupt immediately. When
263                          * switching from device to host, the PCD interrupt
264                          * handler won't handle the interrupt if host mode is
265                          * already set. The HCD interrupt handler won't get
266                          * called if the HCD state is HALT. This means that
267                          * the interrupt does not get handled and Linux
268                          * complains loudly.
269                          */
270                         gintmsk.d32 = 0;
271                         gintmsk.b.sofintr = 1;
272                         DWC_MODIFY_REG32(&global_regs->gintmsk, gintmsk.d32, 0);
273                         DWC_SPINUNLOCK(core_if->lock);
274                         cil_pcd_stop(core_if);
275                         cil_hcd_start(core_if);
276                         DWC_SPINLOCK(core_if->lock);
277                         core_if->op_state = A_HOST;
278                 }
279         }
280         if (gotgint.b.adevtoutchng) {
281                 DWC_DEBUGPL(DBG_ANY, " ++OTG Interrupt: "
282                             "A-Device Timeout Change++\n");
283         }
284         if (gotgint.b.debdone) {
285                 DWC_DEBUGPL(DBG_ANY, " ++OTG Interrupt: " "Debounce Done++\n");
286                 /* Need to power off VBUS after 10s if OTG2 non-hnp capable host */
287                 if (core_if->otg_ver == 1)
288                         cil_hcd_session_start(core_if);
289         }
290
291         /* Clear GOTGINT */
292         DWC_WRITE_REG32(&core_if->core_global_regs->gotgint, gotgint.d32);
293
294         return 1;
295 }
296
297 void w_conn_id_status_change(void *p)
298 {
299         dwc_otg_core_if_t *core_if = p;
300         uint32_t count = 0;
301         gotgctl_data_t gotgctl = {.d32 = 0 };
302         dwc_otg_pcd_t *pcd = core_if->otg_dev->pcd;
303
304         gotgctl.d32 = DWC_READ_REG32(&core_if->core_global_regs->gotgctl);
305         DWC_DEBUGPL(DBG_CIL, "gotgctl=%0x\n", gotgctl.d32);
306         DWC_DEBUGPL(DBG_CIL, "gotgctl.b.conidsts=%d\n", gotgctl.b.conidsts);
307
308         /* B-Device connector (Device Mode) */
309         if (gotgctl.b.conidsts) {
310                 gotgctl_data_t gotgctl_local;
311
312                 /* Wait for switch to device mode. */
313                 while (!dwc_otg_is_device_mode(core_if)) {
314                         gotgctl_local.d32 =
315                             DWC_READ_REG32(&core_if->core_global_regs->gotgctl);
316                         DWC_DEBUGPL(DBG_ANY,
317                                     "Waiting for Peripheral Mode, Mode=%s count = %d gotgctl=%08x\n",
318                                     (dwc_otg_is_host_mode(core_if) ? "Host" :
319                                      "Peripheral"), count, gotgctl_local.d32);
320                         dwc_mdelay(1);  /* vahrama previous value was 100 */
321                         if (!gotgctl_local.b.conidsts)
322                                 goto host;
323                         if (++count > 10000)
324                                 break;
325                 }
326                 DWC_ASSERT(++count < 10000,
327                            "Connection id status change timed out");
328                 core_if->op_state = B_PERIPHERAL;
329                 cil_hcd_stop(core_if);;
330                 /* pcd->phy_suspend = 1; */
331                 pcd->vbus_status = 0;
332                 dwc_otg_pcd_start_check_vbus_work(pcd);
333                 if (core_if->otg_ver == 0)
334                         dwc_otg_core_init(core_if);
335                 dwc_otg_enable_global_interrupts(core_if);
336                 cil_pcd_start(core_if);
337         } else {
338 host:
339                 /* A-Device connector (Host Mode) */
340                 while (!dwc_otg_is_host_mode(core_if)) {
341                         DWC_DEBUGPL(DBG_ANY, "Waiting for Host Mode, Mode=%s\n",
342                                     (dwc_otg_is_host_mode(core_if) ? "Host" :
343                                      "Peripheral"));
344                         dwc_mdelay(1);  /* vahrama previously was 100 */
345                         if (++count > 10000)
346                                 break;
347                 }
348                 DWC_ASSERT(++count < 10000,
349                            "Connection id status change timed out");
350                 core_if->op_state = A_HOST;
351
352                 cancel_delayed_work(&pcd->check_vbus_work);
353
354                 /*
355                  * Initialize the Core for Host mode.
356                  */
357                 if (core_if->otg_ver)
358                         /* To power off the bus in 10s from the beginning
359                          * of test while denounce has not come yet */
360                         cil_hcd_session_start(core_if);
361                 else
362                         dwc_otg_core_init(core_if);
363                 dwc_otg_enable_global_interrupts(core_if);
364                 cil_hcd_start(core_if);
365         }
366 }
367
368 /**
369  * This function handles the Connector ID Status Change Interrupt.  It
370  * reads the OTG Interrupt Register (GOTCTL) to determine whether this
371  * is a Device to Host Mode transition or a Host Mode to Device
372  * Transition.
373  *
374  * This only occurs when the cable is connected/removed from the PHY
375  * connector.
376  *
377  * @param core_if Programming view of DWC_otg controller.
378  */
379 int32_t dwc_otg_handle_conn_id_status_change_intr(dwc_otg_core_if_t *core_if)
380 {
381
382         /*
383          * Need to disable SOF interrupt immediately. If switching from device
384          * to host, the PCD interrupt handler won't handle the interrupt if
385          * host mode is already set. The HCD interrupt handler won't get
386          * called if the HCD state is HALT. This means that the interrupt does
387          * not get handled and Linux complains loudly.
388          */
389         gintmsk_data_t gintmsk = {.d32 = 0 };
390         gintsts_data_t gintsts = {.d32 = 0 };
391
392         dwc_otg_disable_host_interrupts(core_if);
393         if (core_if->usb_mode != USB_MODE_NORMAL)
394                 goto out;
395
396         gintmsk.b.sofintr = 1;
397         DWC_MODIFY_REG32(&core_if->core_global_regs->gintmsk, gintmsk.d32, 0);
398         DWC_DEBUGPL(DBG_CIL,
399                     " ++Connector ID Status Change Interrupt++  (%s)\n",
400                     (dwc_otg_is_host_mode(core_if) ? "Host" : "Device"));
401
402         DWC_SPINUNLOCK(core_if->lock);
403
404         /*
405          * Need to schedule a work, as there are possible DELAY function calls
406          * Release lock before scheduling workq as it holds spinlock during scheduling
407          */
408
409         /*DWC_WORKQ_SCHEDULE(core_if->wq_otg, w_conn_id_status_change,
410                            core_if, "connection id status change");*/
411         DWC_SPINLOCK(core_if->lock);
412 out:
413         /* Set flag and clear interrupt */
414         gintsts.b.conidstschng = 1;
415         DWC_WRITE_REG32(&core_if->core_global_regs->gintsts, gintsts.d32);
416
417         return 1;
418 }
419
420 /**
421  * This interrupt indicates that a device is initiating the Session
422  * Request Protocol to request the host to turn on bus power so a new
423  * session can begin. The handler responds by turning on bus power. If
424  * the DWC_otg controller is in low power mode, the handler brings the
425  * controller out of low power mode before turning on bus power.
426  *
427  * @param core_if Programming view of DWC_otg controller.
428  */
429 int32_t dwc_otg_handle_session_req_intr(dwc_otg_core_if_t *core_if)
430 {
431         gintsts_data_t gintsts;
432
433 #ifndef DWC_HOST_ONLY
434         DWC_DEBUGPL(DBG_ANY, "++Session Request Interrupt++\n");
435         if (dwc_otg_is_device_mode(core_if)) {
436                 gotgctl_data_t gotgctl = {.d32 = 0 };
437                 DWC_PRINTF("SRP: Device mode\n");
438                 gotgctl.d32 =
439                     DWC_READ_REG32(&core_if->core_global_regs->gotgctl);
440                 if (gotgctl.b.sesreqscs)
441                         DWC_PRINTF("SRP Success\n");
442                 else
443                         DWC_PRINTF("SRP Fail\n");
444                 if (core_if->otg_ver) {
445                         gotgctl.d32 = 0;
446                         gotgctl.b.devhnpen = 1;
447                         DWC_MODIFY_REG32(&core_if->core_global_regs->gotgctl,
448                                          gotgctl.d32, 0);
449                 }
450         } else {
451                 hprt0_data_t hprt0;
452                 DWC_PRINTF("SRP: Host mode\n");
453
454                 /* Turn on the port power bit. */
455                 hprt0.d32 = dwc_otg_read_hprt0(core_if);
456                 hprt0.b.prtpwr = 1;
457                 DWC_WRITE_REG32(core_if->host_if->hprt0, hprt0.d32);
458
459                 /* Start the Connection timer. So a message can be displayed
460                  * if connect does not occur within 10 seconds. */
461                 cil_hcd_session_start(core_if);
462         }
463 #endif
464
465         /* Clear interrupt */
466         gintsts.d32 = 0;
467         gintsts.b.sessreqintr = 1;
468         DWC_WRITE_REG32(&core_if->core_global_regs->gintsts, gintsts.d32);
469
470         return 1;
471 }
472
473 void w_wakeup_detected(void *data)
474 {
475         dwc_otg_core_if_t *core_if = (dwc_otg_core_if_t *) data;
476         /*
477          * Clear the Resume after 70ms. (Need 20 ms minimum. Use 70 ms
478          * so that OPT tests pass with all PHYs).
479          */
480         hprt0_data_t hprt0 = {.d32 = 0 };
481 #if 0
482         pcgcctl_data_t pcgcctl = {.d32 = 0 };
483         /* Restart the Phy Clock */
484         pcgcctl.b.stoppclk = 1;
485         DWC_MODIFY_REG32(core_if->pcgcctl, pcgcctl.d32, 0);
486         dwc_udelay(10);
487 #endif
488         hprt0.d32 = dwc_otg_read_hprt0(core_if);
489         DWC_DEBUGPL(DBG_ANY, "Resume: HPRT0=%0x\n", hprt0.d32);
490         dwc_mdelay(70);
491         hprt0.b.prtres = 0;     /* Resume */
492         DWC_WRITE_REG32(core_if->host_if->hprt0, hprt0.d32);
493         DWC_DEBUGPL(DBG_ANY, "Clear Resume: HPRT0=%0x\n",
494                     DWC_READ_REG32(core_if->host_if->hprt0));
495
496         cil_hcd_resume(core_if);
497
498         /** Change to L0 state*/
499         core_if->lx_state = DWC_OTG_L0;
500 }
501
502 static inline void rk_dwc_otg_phy_soft_reset(dwc_otg_core_if_t *core_if)
503 {
504         struct dwc_otg_platform_data *pldata;
505         guid_data_t guid;
506
507         pldata = core_if->otg_dev->pldata;
508         guid.d32 = core_if->core_global_regs->guid;
509
510         if ((is_rk3288_usb()) && ((guid.d32 & 0x01) == 0)) {
511                 /* only used for HOST20, OTG HOST do not need.
512                  * first, do soft reset usb phy, and then usb phy
513                  * can drive resume signal.
514                  * second, set usb phy into suspend mode and
515                  * normal mode once time, and then usb phy can
516                  * send SOFs immediately after resume.
517                  */
518                 rk3288_cru_set_soft_reset(RK3288_SOFT_RST_USBHOST1PHY, true);
519                 udelay(50);
520                 rk3288_cru_set_soft_reset(RK3288_SOFT_RST_USBHOST1PHY, false);
521                 udelay(50);
522
523                 pldata->phy_suspend(pldata, USB_PHY_SUSPEND);
524                 udelay(50);
525                 pldata->phy_suspend(pldata, USB_PHY_ENABLED);
526                 udelay(100);
527         }
528 }
529
530 /**
531  * This interrupt indicates that the DWC_otg controller has detected a
532  * resume or remote wakeup sequence. If the DWC_otg controller is in
533  * low power mode, the handler must brings the controller out of low
534  * power mode. The controller automatically begins resume
535  * signaling. The handler schedules a time to stop resume signaling.
536  */
537 int32_t dwc_otg_handle_wakeup_detected_intr(dwc_otg_core_if_t *core_if)
538 {
539         gintsts_data_t gintsts;
540
541         DWC_DEBUGPL(DBG_ANY,
542                     "++Resume and Remote Wakeup Detected Interrupt++\n");
543
544         DWC_PRINTF("%s lxstate = %d\n", __func__, core_if->lx_state);
545
546         if (dwc_otg_is_device_mode(core_if)) {
547                 dctl_data_t dctl = {.d32 = 0 };
548                 DWC_DEBUGPL(DBG_PCD, "DSTS=0x%0x\n",
549                             DWC_READ_REG32(&core_if->dev_if->
550                                            dev_global_regs->dsts));
551                 if (core_if->lx_state == DWC_OTG_L2) {
552 #ifdef PARTIAL_POWER_DOWN
553                         if (core_if->hwcfg4.b.power_optimiz) {
554                                 pcgcctl_data_t power = {.d32 = 0 };
555
556                                 power.d32 = DWC_READ_REG32(core_if->pcgcctl);
557                                 DWC_DEBUGPL(DBG_CIL, "PCGCCTL=%0x\n",
558                                             power.d32);
559
560                                 power.b.stoppclk = 0;
561                                 DWC_WRITE_REG32(core_if->pcgcctl, power.d32);
562
563                                 power.b.pwrclmp = 0;
564                                 DWC_WRITE_REG32(core_if->pcgcctl, power.d32);
565
566                                 power.b.rstpdwnmodule = 0;
567                                 DWC_WRITE_REG32(core_if->pcgcctl, power.d32);
568                         }
569 #endif
570                         /* Clear the Remote Wakeup Signaling */
571                         dctl.b.rmtwkupsig = 1;
572                         DWC_MODIFY_REG32(&core_if->dev_if->
573                                          dev_global_regs->dctl, dctl.d32, 0);
574
575                         DWC_SPINUNLOCK(core_if->lock);
576                         if (core_if->pcd_cb && core_if->pcd_cb->resume_wakeup) {
577                                 core_if->pcd_cb->resume_wakeup(core_if->pcd_cb->
578                                                                p);
579                         }
580                         DWC_SPINLOCK(core_if->lock);
581                 } else {
582                         glpmcfg_data_t lpmcfg;
583                         pcgcctl_data_t pcgcctl = {.d32 = 0 };
584
585                         lpmcfg.d32 =
586                             DWC_READ_REG32(&core_if->core_global_regs->glpmcfg);
587                         lpmcfg.b.hird_thres &= (~(1 << 4));
588                         lpmcfg.b.en_utmi_sleep = 0;
589
590                         /* Clear Enbl_L1Gating bit. */
591                         pcgcctl.b.enbl_sleep_gating = 1;
592                         DWC_MODIFY_REG32(core_if->pcgcctl, pcgcctl.d32, 0);
593
594                         DWC_WRITE_REG32(&core_if->core_global_regs->glpmcfg,
595                                         lpmcfg.d32);
596                 }
597                 /** Change to L0 state*/
598                 core_if->lx_state = DWC_OTG_L0;
599         } else {
600                 if (core_if->lx_state != DWC_OTG_L1) {
601                         pcgcctl_data_t pcgcctl = {.d32 = 0 };
602
603                         /* Restart the Phy Clock */
604                         pcgcctl.b.stoppclk = 1;
605                         DWC_MODIFY_REG32(core_if->pcgcctl, pcgcctl.d32, 0);
606                         udelay(10);
607
608                         rk_dwc_otg_phy_soft_reset(core_if);
609
610                         DWC_TASK_SCHEDULE(core_if->wkp_tasklet);
611                 } else {
612                         /** Change to L0 state*/
613                         core_if->lx_state = DWC_OTG_L0;
614                 }
615         }
616
617         /* Clear interrupt */
618         gintsts.d32 = 0;
619         gintsts.b.wkupintr = 1;
620         DWC_WRITE_REG32(&core_if->core_global_regs->gintsts, gintsts.d32);
621
622         return 1;
623 }
624
625 /**
626  * This interrupt indicates that the Wakeup Logic has detected a
627  * Device disconnect.
628  */
629 static int32_t dwc_otg_handle_pwrdn_disconnect_intr(dwc_otg_core_if_t *core_if)
630 {
631         gpwrdn_data_t gpwrdn = {.d32 = 0 };
632         gpwrdn_data_t gpwrdn_temp = {.d32 = 0 };
633         gpwrdn_temp.d32 = DWC_READ_REG32(&core_if->core_global_regs->gpwrdn);
634
635         DWC_PRINTF("%s called\n", __FUNCTION__);
636
637         if (!core_if->hibernation_suspend) {
638                 DWC_PRINTF("Already exited from Hibernation\n");
639                 return 1;
640         }
641
642         /* Switch on the voltage to the core */
643         gpwrdn.b.pwrdnswtch = 1;
644         DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
645         dwc_udelay(10);
646
647         /* Reset the core */
648         gpwrdn.d32 = 0;
649         gpwrdn.b.pwrdnrstn = 1;
650         DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
651         dwc_udelay(10);
652
653         /* Disable power clamps */
654         gpwrdn.d32 = 0;
655         gpwrdn.b.pwrdnclmp = 1;
656         DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
657
658         /* Remove reset the core signal */
659         gpwrdn.d32 = 0;
660         gpwrdn.b.pwrdnrstn = 1;
661         DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, 0, gpwrdn.d32);
662         dwc_udelay(10);
663
664         /* Disable PMU interrupt */
665         gpwrdn.d32 = 0;
666         gpwrdn.b.pmuintsel = 1;
667         DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
668
669         core_if->hibernation_suspend = 0;
670
671         /* Disable PMU */
672         gpwrdn.d32 = 0;
673         gpwrdn.b.pmuactv = 1;
674         DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
675         dwc_udelay(10);
676
677         if (gpwrdn_temp.b.idsts) {
678                 core_if->op_state = B_PERIPHERAL;
679                 dwc_otg_core_init(core_if);
680                 dwc_otg_enable_global_interrupts(core_if);
681                 cil_pcd_start(core_if);
682         } else {
683                 core_if->op_state = A_HOST;
684                 dwc_otg_core_init(core_if);
685                 dwc_otg_enable_global_interrupts(core_if);
686                 cil_hcd_start(core_if);
687         }
688
689         return 1;
690 }
691
692 /**
693  * This interrupt indicates that the Wakeup Logic has detected a
694  * remote wakeup sequence.
695  */
696 static int32_t dwc_otg_handle_pwrdn_wakeup_detected_intr(dwc_otg_core_if_t *
697                                                          core_if)
698 {
699         gpwrdn_data_t gpwrdn = {.d32 = 0 };
700         DWC_DEBUGPL(DBG_ANY,
701                     "++Powerdown Remote Wakeup Detected Interrupt++\n");
702
703         if (!core_if->hibernation_suspend) {
704                 DWC_PRINTF("Already exited from Hibernation\n");
705                 return 1;
706         }
707
708         gpwrdn.d32 = DWC_READ_REG32(&core_if->core_global_regs->gpwrdn);
709         if (gpwrdn.b.idsts) {   /* Device Mode */
710                 if ((core_if->power_down == 2)
711                     && (core_if->hibernation_suspend == 1)) {
712                         dwc_otg_device_hibernation_restore(core_if, 0, 0);
713                 }
714         } else {
715                 if ((core_if->power_down == 2)
716                     && (core_if->hibernation_suspend == 1)) {
717                         dwc_otg_host_hibernation_restore(core_if, 1, 0);
718                 }
719         }
720         return 1;
721 }
722
723 static int32_t dwc_otg_handle_pwrdn_idsts_change(dwc_otg_device_t *otg_dev)
724 {
725         gpwrdn_data_t gpwrdn = {.d32 = 0 };
726         gpwrdn_data_t gpwrdn_temp = {.d32 = 0 };
727         dwc_otg_core_if_t *core_if = otg_dev->core_if;
728
729         DWC_DEBUGPL(DBG_ANY, "%s called\n", __FUNCTION__);
730         gpwrdn_temp.d32 = DWC_READ_REG32(&core_if->core_global_regs->gpwrdn);
731         if (core_if->power_down == 2) {
732                 if (!core_if->hibernation_suspend) {
733                         DWC_PRINTF("Already exited from Hibernation\n");
734                         return 1;
735                 }
736                 DWC_DEBUGPL(DBG_ANY,
737                             "Exit from hibernation on ID sts change\n");
738                 /* Switch on the voltage to the core */
739                 gpwrdn.b.pwrdnswtch = 1;
740                 DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32,
741                                  0);
742                 dwc_udelay(10);
743
744                 /* Reset the core */
745                 gpwrdn.d32 = 0;
746                 gpwrdn.b.pwrdnrstn = 1;
747                 DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32,
748                                  0);
749                 dwc_udelay(10);
750
751                 /* Disable power clamps */
752                 gpwrdn.d32 = 0;
753                 gpwrdn.b.pwrdnclmp = 1;
754                 DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32,
755                                  0);
756
757                 /* Remove reset the core signal */
758                 gpwrdn.d32 = 0;
759                 gpwrdn.b.pwrdnrstn = 1;
760                 DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, 0,
761                                  gpwrdn.d32);
762                 dwc_udelay(10);
763
764                 /* Disable PMU interrupt */
765                 gpwrdn.d32 = 0;
766                 gpwrdn.b.pmuintsel = 1;
767                 DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32,
768                                  0);
769
770                 /*Indicates that we are exiting from hibernation */
771                 core_if->hibernation_suspend = 0;
772
773                 /* Disable PMU */
774                 gpwrdn.d32 = 0;
775                 gpwrdn.b.pmuactv = 1;
776                 DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32,
777                                  0);
778                 dwc_udelay(10);
779
780                 gpwrdn.d32 = core_if->gr_backup->gpwrdn_local;
781                 if (gpwrdn.b.dis_vbus == 1) {
782                         gpwrdn.d32 = 0;
783                         gpwrdn.b.dis_vbus = 1;
784                         DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn,
785                                          gpwrdn.d32, 0);
786                 }
787
788                 if (gpwrdn_temp.b.idsts) {
789                         core_if->op_state = B_PERIPHERAL;
790                         dwc_otg_core_init(core_if);
791                         dwc_otg_enable_global_interrupts(core_if);
792                         cil_pcd_start(core_if);
793                 } else {
794                         core_if->op_state = A_HOST;
795                         dwc_otg_core_init(core_if);
796                         dwc_otg_enable_global_interrupts(core_if);
797                         cil_hcd_start(core_if);
798                 }
799         }
800
801         if (core_if->adp_enable) {
802                 uint8_t is_host = 0;
803                 DWC_SPINUNLOCK(core_if->lock);
804                 /* Change the core_if's lock to hcd/pcd lock depend on mode? */
805 #ifndef DWC_HOST_ONLY
806                 if (gpwrdn_temp.b.idsts)
807                         core_if->lock = otg_dev->pcd->lock;
808 #endif
809 #ifndef DWC_DEVICE_ONLY
810                 if (!gpwrdn_temp.b.idsts) {
811                         core_if->lock = otg_dev->hcd->lock;
812                         is_host = 1;
813                 }
814 #endif
815                 DWC_PRINTF("RESTART ADP\n");
816                 if (core_if->adp.probe_enabled)
817                         dwc_otg_adp_probe_stop(core_if);
818                 if (core_if->adp.sense_enabled)
819                         dwc_otg_adp_sense_stop(core_if);
820                 if (core_if->adp.sense_timer_started)
821                         DWC_TIMER_CANCEL(core_if->adp.sense_timer);
822                 if (core_if->adp.vbuson_timer_started)
823                         DWC_TIMER_CANCEL(core_if->adp.vbuson_timer);
824                 core_if->adp.probe_timer_values[0] = -1;
825                 core_if->adp.probe_timer_values[1] = -1;
826                 core_if->adp.sense_timer_started = 0;
827                 core_if->adp.vbuson_timer_started = 0;
828                 core_if->adp.probe_counter = 0;
829                 core_if->adp.gpwrdn = 0;
830
831                 /* Disable PMU and restart ADP */
832                 gpwrdn_temp.d32 = 0;
833                 gpwrdn_temp.b.pmuactv = 1;
834                 gpwrdn_temp.b.pmuintsel = 1;
835                 DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32,
836                                  0);
837                 DWC_PRINTF("Check point 1\n");
838                 dwc_mdelay(110);
839                 dwc_otg_adp_start(core_if, is_host);
840                 DWC_SPINLOCK(core_if->lock);
841         }
842
843         return 1;
844 }
845
846 static int32_t dwc_otg_handle_pwrdn_session_change(dwc_otg_core_if_t *core_if)
847 {
848         gpwrdn_data_t gpwrdn = {.d32 = 0 };
849         int32_t otg_cap_param = core_if->core_params->otg_cap;
850         DWC_DEBUGPL(DBG_ANY, "%s called\n", __FUNCTION__);
851
852         gpwrdn.d32 = DWC_READ_REG32(&core_if->core_global_regs->gpwrdn);
853         if (core_if->power_down == 2) {
854                 if (!core_if->hibernation_suspend) {
855                         DWC_PRINTF("Already exited from Hibernation\n");
856                         return 1;
857                 }
858
859                 if ((otg_cap_param != DWC_OTG_CAP_PARAM_HNP_SRP_CAPABLE ||
860                      otg_cap_param != DWC_OTG_CAP_PARAM_SRP_ONLY_CAPABLE) &&
861                     gpwrdn.b.bsessvld == 0) {
862                         /* Save gpwrdn register for further usage if stschng interrupt */
863                         core_if->gr_backup->gpwrdn_local =
864                             DWC_READ_REG32(&core_if->core_global_regs->gpwrdn);
865                         /*Exit from ISR and wait for stschng interrupt with bsessvld = 1 */
866                         return 1;
867                 }
868
869                 /* Switch on the voltage to the core */
870                 gpwrdn.d32 = 0;
871                 gpwrdn.b.pwrdnswtch = 1;
872                 DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32,
873                                  0);
874                 dwc_udelay(10);
875
876                 /* Reset the core */
877                 gpwrdn.d32 = 0;
878                 gpwrdn.b.pwrdnrstn = 1;
879                 DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32,
880                                  0);
881                 dwc_udelay(10);
882
883                 /* Disable power clamps */
884                 gpwrdn.d32 = 0;
885                 gpwrdn.b.pwrdnclmp = 1;
886                 DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32,
887                                  0);
888
889                 /* Remove reset the core signal */
890                 gpwrdn.d32 = 0;
891                 gpwrdn.b.pwrdnrstn = 1;
892                 DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, 0,
893                                  gpwrdn.d32);
894                 dwc_udelay(10);
895
896                 /* Disable PMU interrupt */
897                 gpwrdn.d32 = 0;
898                 gpwrdn.b.pmuintsel = 1;
899                 DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32,
900                                  0);
901                 dwc_udelay(10);
902
903                 /*Indicates that we are exiting from hibernation */
904                 core_if->hibernation_suspend = 0;
905
906                 /* Disable PMU */
907                 gpwrdn.d32 = 0;
908                 gpwrdn.b.pmuactv = 1;
909                 DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32,
910                                  0);
911                 dwc_udelay(10);
912
913                 core_if->op_state = B_PERIPHERAL;
914                 dwc_otg_core_init(core_if);
915                 dwc_otg_enable_global_interrupts(core_if);
916                 cil_pcd_start(core_if);
917
918                 if (otg_cap_param == DWC_OTG_CAP_PARAM_HNP_SRP_CAPABLE ||
919                     otg_cap_param == DWC_OTG_CAP_PARAM_SRP_ONLY_CAPABLE) {
920                         /*
921                          * Initiate SRP after initial ADP probe.
922                          */
923                         dwc_otg_initiate_srp(core_if);
924                 }
925         } else if (core_if->adp_enable) {
926                 dwc_otg_adp_probe_stop(core_if);
927                 if (DWC_WORKQ_PENDING(core_if->wq_otg))
928                         core_if->stop_adpprb = 1;
929                 /* Disable Power Down Logic */
930                 gpwrdn.d32 = 0;
931                 gpwrdn.b.pmuintsel = 1;
932                 gpwrdn.b.pmuactv = 1;
933                 DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32,
934                                  0);
935
936                 /*
937                  * Initialize the Core for Device mode.
938                  */
939                 core_if->op_state = B_PERIPHERAL;
940                 cil_pcd_start(core_if);
941                 dwc_otg_enable_global_interrupts(core_if);
942         }
943
944         return 1;
945 }
946
947 /**
948  * This interrupt indicates that the Wakeup Logic has detected a
949  * status change either on IDDIG or BSessVld.
950  */
951 static uint32_t dwc_otg_handle_pwrdn_stschng_intr(dwc_otg_device_t *otg_dev)
952 {
953         int retval;
954         gpwrdn_data_t gpwrdn = {.d32 = 0 };
955         gpwrdn_data_t gpwrdn_temp = {.d32 = 0 };
956         dwc_otg_core_if_t *core_if = otg_dev->core_if;
957
958         DWC_PRINTF("%s called\n", __FUNCTION__);
959
960         if (core_if->power_down == 2) {
961                 if (core_if->hibernation_suspend <= 0) {
962                         DWC_PRINTF("Already exited from Hibernation\n");
963                         return 1;
964                 } else
965                         gpwrdn_temp.d32 = core_if->gr_backup->gpwrdn_local;
966
967         } else {
968                 gpwrdn_temp.d32 = core_if->adp.gpwrdn;
969         }
970
971         gpwrdn.d32 = DWC_READ_REG32(&core_if->core_global_regs->gpwrdn);
972
973         if (gpwrdn.b.idsts ^ gpwrdn_temp.b.idsts) {
974                 retval = dwc_otg_handle_pwrdn_idsts_change(otg_dev);
975         } else if (gpwrdn.b.bsessvld ^ gpwrdn_temp.b.bsessvld) {
976                 retval = dwc_otg_handle_pwrdn_session_change(core_if);
977         }
978
979         return retval;
980 }
981
982 /**
983  * This interrupt indicates that the Wakeup Logic has detected a
984  * SRP.
985  */
986 static int32_t dwc_otg_handle_pwrdn_srp_intr(dwc_otg_core_if_t *core_if)
987 {
988         gpwrdn_data_t gpwrdn = {.d32 = 0 };
989
990         DWC_PRINTF("%s called\n", __FUNCTION__);
991
992         if (!core_if->hibernation_suspend) {
993                 DWC_PRINTF("Already exited from Hibernation\n");
994                 return 1;
995         }
996 #ifdef DWC_DEV_SRPCAP
997         if (core_if->pwron_timer_started) {
998                 core_if->pwron_timer_started = 0;
999                 DWC_TIMER_CANCEL(core_if->pwron_timer);
1000         }
1001 #endif
1002
1003         /* Switch on the voltage to the core */
1004         gpwrdn.b.pwrdnswtch = 1;
1005         DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
1006         dwc_udelay(10);
1007
1008         /* Reset the core */
1009         gpwrdn.d32 = 0;
1010         gpwrdn.b.pwrdnrstn = 1;
1011         DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
1012         dwc_udelay(10);
1013
1014         /* Disable power clamps */
1015         gpwrdn.d32 = 0;
1016         gpwrdn.b.pwrdnclmp = 1;
1017         DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
1018
1019         /* Remove reset the core signal */
1020         gpwrdn.d32 = 0;
1021         gpwrdn.b.pwrdnrstn = 1;
1022         DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, 0, gpwrdn.d32);
1023         dwc_udelay(10);
1024
1025         /* Disable PMU interrupt */
1026         gpwrdn.d32 = 0;
1027         gpwrdn.b.pmuintsel = 1;
1028         DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
1029
1030         /* Indicates that we are exiting from hibernation */
1031         core_if->hibernation_suspend = 0;
1032
1033         /* Disable PMU */
1034         gpwrdn.d32 = 0;
1035         gpwrdn.b.pmuactv = 1;
1036         DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
1037         dwc_udelay(10);
1038
1039         /* Programm Disable VBUS to 0 */
1040         gpwrdn.d32 = 0;
1041         gpwrdn.b.dis_vbus = 1;
1042         DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
1043
1044         /*Initialize the core as Host */
1045         core_if->op_state = A_HOST;
1046         dwc_otg_core_init(core_if);
1047         dwc_otg_enable_global_interrupts(core_if);
1048         cil_hcd_start(core_if);
1049
1050         return 1;
1051 }
1052
1053 /** This interrupt indicates that restore command after Hibernation
1054  * was completed by the core. */
1055 int32_t dwc_otg_handle_restore_done_intr(dwc_otg_core_if_t *core_if)
1056 {
1057         pcgcctl_data_t pcgcctl;
1058         DWC_DEBUGPL(DBG_ANY, "++Restore Done Interrupt++\n");
1059
1060         /* TODO De-assert restore signal. 8.a */
1061         pcgcctl.d32 = DWC_READ_REG32(core_if->pcgcctl);
1062         if (pcgcctl.b.restoremode == 1) {
1063                 gintmsk_data_t gintmsk = {.d32 = 0 };
1064                 /*
1065                  * If restore mode is Remote Wakeup,
1066                  * unmask Remote Wakeup interrupt.
1067                  */
1068                 gintmsk.b.wkupintr = 1;
1069                 DWC_MODIFY_REG32(&core_if->core_global_regs->gintmsk,
1070                                  0, gintmsk.d32);
1071         }
1072
1073         return 1;
1074 }
1075
1076 /**
1077  * This interrupt indicates that a device has been disconnected from
1078  * the root port.
1079  */
1080 int32_t dwc_otg_handle_disconnect_intr(dwc_otg_core_if_t *core_if)
1081 {
1082         gintsts_data_t gintsts;
1083
1084         DWC_DEBUGPL(DBG_ANY, "++Disconnect Detected Interrupt++ (%s) %s\n",
1085                     (dwc_otg_is_host_mode(core_if) ? "Host" : "Device"),
1086                     op_state_str(core_if));
1087
1088 /** @todo Consolidate this if statement. */
1089 #ifndef DWC_HOST_ONLY
1090         if (core_if->op_state == B_HOST) {
1091                 /* If in device mode Disconnect and stop the HCD, then
1092                  * start the PCD. */
1093                 DWC_SPINUNLOCK(core_if->lock);
1094                 cil_hcd_disconnect(core_if);
1095                 cil_pcd_start(core_if);
1096                 DWC_SPINLOCK(core_if->lock);
1097                 core_if->op_state = B_PERIPHERAL;
1098         } else if (dwc_otg_is_device_mode(core_if)) {
1099                 gotgctl_data_t gotgctl = {.d32 = 0 };
1100                 gotgctl.d32 =
1101                     DWC_READ_REG32(&core_if->core_global_regs->gotgctl);
1102                 if (gotgctl.b.hstsethnpen == 1) {
1103                         /* Do nothing, if HNP in process the OTG
1104                          * interrupt "Host Negotiation Detected"
1105                          * interrupt will do the mode switch.
1106                          */
1107                 } else if (gotgctl.b.devhnpen == 0) {
1108                         /* If in device mode Disconnect and stop the HCD, then
1109                          * start the PCD. */
1110                         DWC_SPINUNLOCK(core_if->lock);
1111                         cil_hcd_disconnect(core_if);
1112                         cil_pcd_start(core_if);
1113                         DWC_SPINLOCK(core_if->lock);
1114                         core_if->op_state = B_PERIPHERAL;
1115                 } else {
1116                         DWC_DEBUGPL(DBG_ANY, "!a_peripheral && !devhnpen\n");
1117                 }
1118         } else {
1119                 if (core_if->op_state == A_HOST) {
1120                         /* A-Cable still connected but device disconnected. */
1121                         cil_hcd_disconnect(core_if);
1122                         if (core_if->adp_enable) {
1123                                 gpwrdn_data_t gpwrdn = {.d32 = 0 };
1124                                 cil_hcd_stop(core_if);
1125                                 /* Enable Power Down Logic */
1126                                 gpwrdn.b.pmuintsel = 1;
1127                                 gpwrdn.b.pmuactv = 1;
1128                                 DWC_MODIFY_REG32(&core_if->
1129                                                  core_global_regs->gpwrdn, 0,
1130                                                  gpwrdn.d32);
1131                                 dwc_otg_adp_probe_start(core_if);
1132
1133                                 /* Power off the core */
1134                                 if (core_if->power_down == 2) {
1135                                         gpwrdn.d32 = 0;
1136                                         gpwrdn.b.pwrdnswtch = 1;
1137                                         DWC_MODIFY_REG32
1138                                             (&core_if->core_global_regs->gpwrdn,
1139                                              gpwrdn.d32, 0);
1140                                 }
1141                         }
1142                 }
1143         }
1144 #endif
1145         /* Change to L3(OFF) state */
1146         core_if->lx_state = DWC_OTG_L3;
1147
1148         gintsts.d32 = 0;
1149         gintsts.b.disconnect = 1;
1150         DWC_WRITE_REG32(&core_if->core_global_regs->gintsts, gintsts.d32);
1151         return 1;
1152 }
1153
1154 /**
1155  * This interrupt indicates that SUSPEND state has been detected on
1156  * the USB.
1157  *
1158  * For HNP the USB Suspend interrupt signals the change from
1159  * "a_peripheral" to "a_host".
1160  *
1161  * When power management is enabled the core will be put in low power
1162  * mode.
1163  */
1164 int32_t dwc_otg_handle_usb_suspend_intr(dwc_otg_core_if_t *core_if)
1165 {
1166         dsts_data_t dsts;
1167         gintsts_data_t gintsts;
1168         dcfg_data_t dcfg;
1169
1170         DWC_DEBUGPL(DBG_ANY, "USB SUSPEND\n");
1171
1172         if ((core_if->otg_ver == 1) && (core_if->op_state == A_PERIPHERAL))
1173                 dwc_mdelay(200);        /* vahrama - WA - see BU's mail */
1174
1175         if (dwc_otg_is_device_mode(core_if)) {
1176                 /* Check the Device status register to determine if the Suspend
1177                  * state is active. */
1178                 dsts.d32 =
1179                     DWC_READ_REG32(&core_if->dev_if->dev_global_regs->dsts);
1180                 DWC_DEBUGPL(DBG_PCD, "DSTS=0x%0x\n", dsts.d32);
1181                 DWC_DEBUGPL(DBG_PCD, "DSTS.Suspend Status=%d "
1182                             "HWCFG4.power Optimize=%d\n",
1183                             dsts.b.suspsts, core_if->hwcfg4.b.power_optimiz);
1184
1185 #ifdef PARTIAL_POWER_DOWN
1186 /** @todo Add a module parameter for power management. */
1187
1188                 if (dsts.b.suspsts && core_if->hwcfg4.b.power_optimiz) {
1189                         pcgcctl_data_t power = {.d32 = 0 };
1190                         DWC_DEBUGPL(DBG_CIL, "suspend\n");
1191
1192                         power.b.pwrclmp = 1;
1193                         DWC_WRITE_REG32(core_if->pcgcctl, power.d32);
1194
1195                         power.b.rstpdwnmodule = 1;
1196                         DWC_MODIFY_REG32(core_if->pcgcctl, 0, power.d32);
1197
1198                         power.b.stoppclk = 1;
1199                         DWC_MODIFY_REG32(core_if->pcgcctl, 0, power.d32);
1200
1201                 } else {
1202                         DWC_DEBUGPL(DBG_ANY, "disconnect?\n");
1203                 }
1204 #endif
1205                 /* PCD callback for suspend */
1206                 DWC_SPINUNLOCK(core_if->lock);
1207                 cil_pcd_suspend(core_if);
1208                 DWC_SPINLOCK(core_if->lock);
1209
1210                 if (core_if->power_down == 2) {
1211                         dcfg.d32 =
1212                             DWC_READ_REG32(&core_if->dev_if->dev_global_regs->
1213                                            dcfg);
1214                         DWC_DEBUGPL(DBG_ANY, "lx_state = %08x\n",
1215                                     core_if->lx_state);
1216                         DWC_DEBUGPL(DBG_ANY, " device address = %08d\n",
1217                                     dcfg.b.devaddr);
1218
1219                         if (core_if->lx_state != DWC_OTG_L3 && dcfg.b.devaddr) {
1220                                 pcgcctl_data_t pcgcctl = {.d32 = 0 };
1221                                 gpwrdn_data_t gpwrdn = {.d32 = 0 };
1222                                 gusbcfg_data_t gusbcfg = {.d32 = 0 };
1223
1224                                 /* Change to L2(suspend) state */
1225                                 core_if->lx_state = DWC_OTG_L2;
1226
1227                                 /* Clear interrupt in gintsts */
1228                                 gintsts.d32 = 0;
1229                                 gintsts.b.usbsuspend = 1;
1230                                 DWC_WRITE_REG32(&core_if->
1231                                                 core_global_regs->gintsts,
1232                                                 gintsts.d32);
1233                                 DWC_PRINTF("Start of hibernation completed\n");
1234                                 dwc_otg_save_global_regs(core_if);
1235                                 dwc_otg_save_dev_regs(core_if);
1236
1237                                 gusbcfg.d32 =
1238                                     DWC_READ_REG32(&core_if->
1239                                                    core_global_regs->gusbcfg);
1240                                 if (gusbcfg.b.ulpi_utmi_sel == 1) {
1241                                         /* ULPI interface */
1242                                         /* Suspend the Phy Clock */
1243                                         pcgcctl.d32 = 0;
1244                                         pcgcctl.b.stoppclk = 1;
1245                                         DWC_MODIFY_REG32(core_if->pcgcctl, 0,
1246                                                          pcgcctl.d32);
1247                                         dwc_udelay(10);
1248                                         gpwrdn.b.pmuactv = 1;
1249                                         DWC_MODIFY_REG32
1250                                             (&core_if->core_global_regs->gpwrdn,
1251                                              0, gpwrdn.d32);
1252                                 } else {
1253                                         /* UTMI+ Interface */
1254                                         gpwrdn.b.pmuactv = 1;
1255                                         DWC_MODIFY_REG32
1256                                             (&core_if->core_global_regs->gpwrdn,
1257                                              0, gpwrdn.d32);
1258                                         dwc_udelay(10);
1259                                         pcgcctl.b.stoppclk = 1;
1260                                         DWC_MODIFY_REG32(core_if->pcgcctl, 0,
1261                                                          pcgcctl.d32);
1262                                         dwc_udelay(10);
1263                                 }
1264
1265                                 /* Set flag to indicate that we are in hibernation */
1266                                 core_if->hibernation_suspend = 1;
1267                                 /* Enable interrupts from wake up logic */
1268                                 gpwrdn.d32 = 0;
1269                                 gpwrdn.b.pmuintsel = 1;
1270                                 DWC_MODIFY_REG32(&core_if->
1271                                                  core_global_regs->gpwrdn, 0,
1272                                                  gpwrdn.d32);
1273                                 dwc_udelay(10);
1274
1275                                 /* Unmask device mode interrupts in GPWRDN */
1276                                 gpwrdn.d32 = 0;
1277                                 gpwrdn.b.rst_det_msk = 1;
1278                                 gpwrdn.b.lnstchng_msk = 1;
1279                                 gpwrdn.b.sts_chngint_msk = 1;
1280                                 DWC_MODIFY_REG32(&core_if->
1281                                                  core_global_regs->gpwrdn, 0,
1282                                                  gpwrdn.d32);
1283                                 dwc_udelay(10);
1284
1285                                 /* Enable Power Down Clamp */
1286                                 gpwrdn.d32 = 0;
1287                                 gpwrdn.b.pwrdnclmp = 1;
1288                                 DWC_MODIFY_REG32(&core_if->
1289                                                  core_global_regs->gpwrdn, 0,
1290                                                  gpwrdn.d32);
1291                                 dwc_udelay(10);
1292
1293                                 /* Switch off VDD */
1294                                 gpwrdn.d32 = 0;
1295                                 gpwrdn.b.pwrdnswtch = 1;
1296                                 DWC_MODIFY_REG32(&core_if->
1297                                                  core_global_regs->gpwrdn, 0,
1298                                                  gpwrdn.d32);
1299
1300                                 /* Save gpwrdn register for further usage if stschng interrupt */
1301                                 core_if->gr_backup->gpwrdn_local =
1302                                     DWC_READ_REG32(&core_if->core_global_regs->
1303                                                    gpwrdn);
1304                                 DWC_PRINTF("Hibernation completed\n");
1305
1306                                 return 1;
1307                         }
1308                 } else if (core_if->power_down == 3) {
1309                         pcgcctl_data_t pcgcctl = {.d32 = 0 };
1310                         dcfg.d32 =
1311                             DWC_READ_REG32(&core_if->dev_if->dev_global_regs->
1312                                            dcfg);
1313                         DWC_DEBUGPL(DBG_ANY, "lx_state = %08x\n",
1314                                     core_if->lx_state);
1315                         DWC_DEBUGPL(DBG_ANY, " device address = %08d\n",
1316                                     dcfg.b.devaddr);
1317
1318                         if (core_if->lx_state != DWC_OTG_L3 && dcfg.b.devaddr) {
1319                                 DWC_DEBUGPL(DBG_ANY,
1320                                             "Start entering to extended hibernation\n");
1321                                 core_if->xhib = 1;
1322
1323                                 /* Clear interrupt in gintsts */
1324                                 gintsts.d32 = 0;
1325                                 gintsts.b.usbsuspend = 1;
1326                                 DWC_WRITE_REG32(&core_if->
1327                                                 core_global_regs->gintsts,
1328                                                 gintsts.d32);
1329
1330                                 dwc_otg_save_global_regs(core_if);
1331                                 dwc_otg_save_dev_regs(core_if);
1332
1333                                 /* Wait for 10 PHY clocks */
1334                                 dwc_udelay(10);
1335
1336                                 /* Program GPIO register while entering to xHib */
1337                                 DWC_WRITE_REG32(&core_if->core_global_regs->
1338                                                 ggpio, 0x1);
1339
1340                                 pcgcctl.b.enbl_extnd_hiber = 1;
1341                                 DWC_MODIFY_REG32(core_if->pcgcctl, 0,
1342                                                  pcgcctl.d32);
1343                                 DWC_MODIFY_REG32(core_if->pcgcctl, 0,
1344                                                  pcgcctl.d32);
1345
1346                                 pcgcctl.d32 = 0;
1347                                 pcgcctl.b.extnd_hiber_pwrclmp = 1;
1348                                 DWC_MODIFY_REG32(core_if->pcgcctl, 0,
1349                                                  pcgcctl.d32);
1350
1351                                 pcgcctl.d32 = 0;
1352                                 pcgcctl.b.extnd_hiber_switch = 1;
1353                                 core_if->gr_backup->xhib_gpwrdn =
1354                                     DWC_READ_REG32(&core_if->core_global_regs->
1355                                                    gpwrdn);
1356                                 core_if->gr_backup->xhib_pcgcctl =
1357                                     DWC_READ_REG32(core_if->pcgcctl) | pcgcctl.
1358                                     d32;
1359                                 DWC_MODIFY_REG32(core_if->pcgcctl, 0,
1360                                                  pcgcctl.d32);
1361
1362                                 DWC_DEBUGPL(DBG_ANY,
1363                                             "Finished entering to extended hibernation\n");
1364
1365                                 return 1;
1366                         }
1367                 }
1368                 if ((core_if->otg_ver == 1)
1369                     && (core_if->core_params->otg_cap ==
1370                         DWC_OTG_CAP_PARAM_HNP_SRP_CAPABLE)) {
1371                         gotgctl_data_t gotgctl = {.d32 = 0 };
1372                         gotgctl.d32 =
1373                             DWC_READ_REG32(&core_if->core_global_regs->gotgctl);
1374                         if (gotgctl.b.devhnpen && core_if->otg_ver == 1) {
1375                                 gotgctl_data_t gotgctl = {.d32 = 0 };
1376                                 dwc_mdelay(5);
1377                                 /**@todo Is the gotgctl.devhnpen cleared
1378                                  * by a USB Reset? */
1379                                 gotgctl.b.devhnpen = 1;
1380                                 gotgctl.b.hnpreq = 1;
1381                                 DWC_WRITE_REG32(&core_if->core_global_regs->
1382                                                 gotgctl, gotgctl.d32);
1383                         }
1384                 }
1385         } else {
1386                 if (core_if->op_state == A_PERIPHERAL) {
1387                         DWC_DEBUGPL(DBG_ANY, "a_peripheral->a_host\n");
1388                         /* Clear the a_peripheral flag, back to a_host. */
1389                         DWC_SPINUNLOCK(core_if->lock);
1390                         cil_pcd_stop(core_if);
1391                         cil_hcd_start(core_if);
1392                         DWC_SPINLOCK(core_if->lock);
1393                         core_if->op_state = A_HOST;
1394                 }
1395         }
1396
1397         /* Change to L2(suspend) state */
1398         core_if->lx_state = DWC_OTG_L2;
1399
1400         /* Clear interrupt */
1401         gintsts.d32 = 0;
1402         gintsts.b.usbsuspend = 1;
1403         DWC_WRITE_REG32(&core_if->core_global_regs->gintsts, gintsts.d32);
1404
1405         return 1;
1406 }
1407
1408 static int32_t dwc_otg_handle_xhib_exit_intr(dwc_otg_core_if_t *core_if)
1409 {
1410         gpwrdn_data_t gpwrdn = {.d32 = 0 };
1411         pcgcctl_data_t pcgcctl = {.d32 = 0 };
1412         gahbcfg_data_t gahbcfg = {.d32 = 0 };
1413
1414         dwc_udelay(10);
1415
1416         /* Program GPIO register while entering to xHib */
1417         DWC_WRITE_REG32(&core_if->core_global_regs->ggpio, 0x0);
1418
1419         pcgcctl.d32 = core_if->gr_backup->xhib_pcgcctl;
1420         pcgcctl.b.extnd_hiber_pwrclmp = 0;
1421         DWC_WRITE_REG32(core_if->pcgcctl, pcgcctl.d32);
1422         dwc_udelay(10);
1423
1424         gpwrdn.d32 = core_if->gr_backup->xhib_gpwrdn;
1425         gpwrdn.b.restore = 1;
1426         DWC_WRITE_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32);
1427         dwc_udelay(10);
1428
1429         restore_lpm_i2c_regs(core_if);
1430
1431         pcgcctl.d32 = core_if->gr_backup->pcgcctl_local & (0x3FFFF << 14);
1432         pcgcctl.b.max_xcvrselect = 1;
1433         pcgcctl.b.ess_reg_restored = 0;
1434         pcgcctl.b.extnd_hiber_switch = 0;
1435         pcgcctl.b.extnd_hiber_pwrclmp = 0;
1436         pcgcctl.b.enbl_extnd_hiber = 1;
1437         DWC_WRITE_REG32(core_if->pcgcctl, pcgcctl.d32);
1438
1439         gahbcfg.d32 = core_if->gr_backup->gahbcfg_local;
1440         gahbcfg.b.glblintrmsk = 1;
1441         DWC_WRITE_REG32(&core_if->core_global_regs->gahbcfg, gahbcfg.d32);
1442
1443         DWC_WRITE_REG32(&core_if->core_global_regs->gintsts, 0xFFFFFFFF);
1444         DWC_WRITE_REG32(&core_if->core_global_regs->gintmsk, 0x1 << 16);
1445
1446         DWC_WRITE_REG32(&core_if->core_global_regs->gusbcfg,
1447                         core_if->gr_backup->gusbcfg_local);
1448         DWC_WRITE_REG32(&core_if->dev_if->dev_global_regs->dcfg,
1449                         core_if->dr_backup->dcfg);
1450
1451         pcgcctl.d32 = 0;
1452         pcgcctl.d32 = core_if->gr_backup->pcgcctl_local & (0x3FFFF << 14);
1453         pcgcctl.b.max_xcvrselect = 1;
1454         pcgcctl.d32 |= 0x608;
1455         DWC_WRITE_REG32(core_if->pcgcctl, pcgcctl.d32);
1456         dwc_udelay(10);
1457
1458         pcgcctl.d32 = 0;
1459         pcgcctl.d32 = core_if->gr_backup->pcgcctl_local & (0x3FFFF << 14);
1460         pcgcctl.b.max_xcvrselect = 1;
1461         pcgcctl.b.ess_reg_restored = 1;
1462         pcgcctl.b.enbl_extnd_hiber = 1;
1463         pcgcctl.b.rstpdwnmodule = 1;
1464         pcgcctl.b.restoremode = 1;
1465         DWC_WRITE_REG32(core_if->pcgcctl, pcgcctl.d32);
1466
1467         DWC_DEBUGPL(DBG_ANY, "%s called\n", __FUNCTION__);
1468
1469         return 1;
1470 }
1471
1472 #ifdef CONFIG_USB_DWC_OTG_LPM
1473 /**
1474  * This function hadles LPM transaction received interrupt.
1475  */
1476 static int32_t dwc_otg_handle_lpm_intr(dwc_otg_core_if_t *core_if)
1477 {
1478         glpmcfg_data_t lpmcfg;
1479         gintsts_data_t gintsts;
1480
1481         if (!core_if->core_params->lpm_enable) {
1482                 DWC_PRINTF("Unexpected LPM interrupt\n");
1483         }
1484
1485         lpmcfg.d32 = DWC_READ_REG32(&core_if->core_global_regs->glpmcfg);
1486         DWC_PRINTF("LPM config register = 0x%08x\n", lpmcfg.d32);
1487
1488         if (dwc_otg_is_host_mode(core_if)) {
1489                 cil_hcd_sleep(core_if);
1490         } else {
1491
1492                 pcgcctl_data_t pcgcctl = {.d32 = 0 };
1493
1494                 lpmcfg.b.hird_thres |= (1 << 4);
1495                 lpmcfg.b.en_utmi_sleep = 1;
1496
1497                 pcgcctl.b.enbl_sleep_gating = 1;
1498                 DWC_MODIFY_REG32(core_if->pcgcctl, 0, pcgcctl.d32);
1499
1500                 if (dwc_otg_get_param_besl_enable(core_if)) {
1501                         lpmcfg.b.en_besl = 1;
1502                 }
1503
1504                 DWC_WRITE_REG32(&core_if->core_global_regs->glpmcfg,
1505                                 lpmcfg.d32);
1506         }
1507
1508         /* Examine prt_sleep_sts after TL1TokenTetry period max (10 us) */
1509         dwc_udelay(10);
1510         lpmcfg.d32 = DWC_READ_REG32(&core_if->core_global_regs->glpmcfg);
1511         if (lpmcfg.b.prt_sleep_sts) {
1512                 /* Save the current state */
1513                 core_if->lx_state = DWC_OTG_L1;
1514         }
1515
1516         /* Clear interrupt  */
1517         gintsts.d32 = 0;
1518         gintsts.b.lpmtranrcvd = 1;
1519         DWC_WRITE_REG32(&core_if->core_global_regs->gintsts, gintsts.d32);
1520         return 1;
1521 }
1522 #endif /* CONFIG_USB_DWC_OTG_LPM */
1523
1524 /**
1525  * This function returns the Core Interrupt register.
1526  */
1527 static inline uint32_t dwc_otg_read_common_intr(dwc_otg_core_if_t *core_if)
1528 {
1529         uint32_t retval;
1530
1531         gahbcfg_data_t gahbcfg = {.d32 = 0 };
1532         gintsts_data_t gintsts;
1533         gintmsk_data_t gintmsk;
1534         gintmsk_data_t gintmsk_common = {.d32 = 0 };
1535         gintmsk_common.b.wkupintr = 1;
1536         gintmsk_common.b.sessreqintr = 1;
1537         gintmsk_common.b.conidstschng = 1;
1538         gintmsk_common.b.otgintr = 1;
1539         gintmsk_common.b.modemismatch = 1;
1540         gintmsk_common.b.disconnect = 1;
1541         gintmsk_common.b.usbsuspend = 1;
1542 #ifdef CONFIG_USB_DWC_OTG_LPM
1543         gintmsk_common.b.lpmtranrcvd = 1;
1544 #endif
1545         gintmsk_common.b.restoredone = 1;
1546         /** @todo: The port interrupt occurs while in device
1547          * mode. Added code to CIL to clear the interrupt for now!
1548          */
1549         gintmsk_common.b.portintr = 1;
1550
1551         gintsts.d32 = DWC_READ_REG32(&core_if->core_global_regs->gintsts);
1552         gintmsk.d32 = DWC_READ_REG32(&core_if->core_global_regs->gintmsk);
1553         gahbcfg.d32 = DWC_READ_REG32(&core_if->core_global_regs->gahbcfg);
1554
1555 #ifdef DEBUG
1556         /* if any common interrupts set */
1557         if (gintsts.d32 & gintmsk_common.d32) {
1558                 DWC_DEBUGPL(DBG_ANY, "gintsts=%08x  gintmsk=%08x\n",
1559                             gintsts.d32, gintmsk.d32);
1560         }
1561 #endif
1562         if (gahbcfg.b.glblintrmsk) {
1563                 retval = (gintsts.d32 & gintmsk.d32) & gintmsk_common.d32;
1564                 return retval;
1565         } else {
1566                 return 0;
1567         }
1568 }
1569
1570 /* MACRO for clearing interupt bits in GPWRDN register */
1571 #define CLEAR_GPWRDN_INTR(__core_if, __intr) \
1572 do { \
1573                 gpwrdn_data_t gpwrdn = {.d32 = 0}; \
1574                 gpwrdn.b.__intr = 1; \
1575                 DWC_MODIFY_REG32(&__core_if->core_global_regs->gpwrdn, \
1576                 0, gpwrdn.d32); \
1577 } while (0)
1578
1579 /**
1580  * Common interrupt handler.
1581  *
1582  * The common interrupts are those that occur in both Host and Device mode.
1583  * This handler handles the following interrupts:
1584  * - Mode Mismatch Interrupt
1585  * - Disconnect Interrupt
1586  * - OTG Interrupt
1587  * - Connector ID Status Change Interrupt
1588  * - Session Request Interrupt.
1589  * - Resume / Remote Wakeup Detected Interrupt.
1590  * - LPM Transaction Received Interrupt
1591  * - ADP Transaction Received Interrupt
1592  *
1593  */
1594 int32_t dwc_otg_handle_common_intr(void *dev)
1595 {
1596         int retval = 0;
1597         gintsts_data_t gintsts;
1598         gpwrdn_data_t gpwrdn = {.d32 = 0 };
1599         dwc_otg_device_t *otg_dev = dev;
1600         dwc_otg_core_if_t *core_if = otg_dev->core_if;
1601         gpwrdn.d32 = DWC_READ_REG32(&core_if->core_global_regs->gpwrdn);
1602
1603         if (dwc_otg_check_haps_status(core_if) == -1) {
1604                 DWC_WARN("HAPS is disconnected");
1605                 return retval;
1606         }
1607
1608         if (dwc_otg_is_device_mode(core_if))
1609                 core_if->frame_num = dwc_otg_get_frame_number(core_if);
1610
1611         if (core_if->lock)
1612                 DWC_SPINLOCK(core_if->lock);
1613
1614         if (core_if->power_down == 3 && core_if->xhib == 1) {
1615                 DWC_DEBUGPL(DBG_ANY, "Exiting from xHIB state\n");
1616                 retval |= dwc_otg_handle_xhib_exit_intr(core_if);
1617                 core_if->xhib = 2;
1618                 if (core_if->lock)
1619                         DWC_SPINUNLOCK(core_if->lock);
1620
1621                 return retval;
1622         }
1623
1624         if (core_if->hibernation_suspend <= 0) {
1625                 gintsts.d32 = dwc_otg_read_common_intr(core_if);
1626
1627                 if (gintsts.b.modemismatch) {
1628                         retval |= dwc_otg_handle_mode_mismatch_intr(core_if);
1629                 }
1630                 if (gintsts.b.otgintr) {
1631                         retval |= dwc_otg_handle_otg_intr(core_if);
1632                 }
1633                 if (gintsts.b.conidstschng) {
1634                         retval |=
1635                             dwc_otg_handle_conn_id_status_change_intr(core_if);
1636                 }
1637                 if (gintsts.b.disconnect) {
1638                         retval |= dwc_otg_handle_disconnect_intr(core_if);
1639                 }
1640                 if (gintsts.b.sessreqintr) {
1641                         retval |= dwc_otg_handle_session_req_intr(core_if);
1642                 }
1643                 if (gintsts.b.wkupintr) {
1644                         retval |= dwc_otg_handle_wakeup_detected_intr(core_if);
1645                 }
1646                 if (gintsts.b.usbsuspend) {
1647                         retval |= dwc_otg_handle_usb_suspend_intr(core_if);
1648                 }
1649 #ifdef CONFIG_USB_DWC_OTG_LPM
1650                 if (gintsts.b.lpmtranrcvd) {
1651                         retval |= dwc_otg_handle_lpm_intr(core_if);
1652                 }
1653 #endif
1654                 if (gintsts.b.restoredone) {
1655                         gintsts.d32 = 0;
1656                         if (core_if->power_down == 2)
1657                                 core_if->hibernation_suspend = -1;
1658                         else if (core_if->power_down == 3 && core_if->xhib == 2) {
1659                                 gpwrdn_data_t gpwrdn = {.d32 = 0 };
1660                                 pcgcctl_data_t pcgcctl = {.d32 = 0 };
1661                                 dctl_data_t dctl = {.d32 = 0 };
1662
1663                                 DWC_WRITE_REG32(&core_if->
1664                                                 core_global_regs->gintsts,
1665                                                 0xFFFFFFFF);
1666
1667                                 DWC_DEBUGPL(DBG_ANY,
1668                                             "RESTORE DONE generated\n");
1669
1670                                 gpwrdn.b.restore = 1;
1671                                 DWC_MODIFY_REG32(&core_if->core_global_regs->
1672                                                  gpwrdn, gpwrdn.d32, 0);
1673                                 dwc_udelay(10);
1674
1675                                 pcgcctl.b.rstpdwnmodule = 1;
1676                                 DWC_MODIFY_REG32(core_if->pcgcctl, pcgcctl.d32,
1677                                                  0);
1678
1679                                 DWC_WRITE_REG32(&core_if->core_global_regs->
1680                                                 gusbcfg,
1681                                                 core_if->gr_backup->
1682                                                 gusbcfg_local);
1683                                 DWC_WRITE_REG32(&core_if->dev_if->
1684                                                 dev_global_regs->dcfg,
1685                                                 core_if->dr_backup->dcfg);
1686                                 DWC_WRITE_REG32(&core_if->dev_if->
1687                                                 dev_global_regs->dctl,
1688                                                 core_if->dr_backup->dctl);
1689                                 dwc_udelay(50);
1690
1691                                 dctl.b.pwronprgdone = 1;
1692                                 DWC_MODIFY_REG32(&core_if->dev_if->
1693                                                  dev_global_regs->dctl, 0,
1694                                                  dctl.d32);
1695                                 dwc_udelay(10);
1696
1697                                 dwc_otg_restore_global_regs(core_if);
1698                                 dwc_otg_restore_dev_regs(core_if, 0);
1699
1700                                 dctl.d32 = 0;
1701                                 dctl.b.pwronprgdone = 1;
1702                                 DWC_MODIFY_REG32(&core_if->dev_if->
1703                                                  dev_global_regs->dctl,
1704                                                  dctl.d32, 0);
1705                                 dwc_udelay(10);
1706
1707                                 pcgcctl.d32 = 0;
1708                                 pcgcctl.b.enbl_extnd_hiber = 1;
1709                                 DWC_MODIFY_REG32(core_if->pcgcctl, pcgcctl.d32,
1710                                                  0);
1711
1712                                 /* The core will be in ON STATE */
1713                                 core_if->lx_state = DWC_OTG_L0;
1714                                 core_if->xhib = 0;
1715
1716                                 DWC_SPINUNLOCK(core_if->lock);
1717                                 if (core_if->pcd_cb
1718                                     && core_if->pcd_cb->resume_wakeup) {
1719                                         core_if->pcd_cb->resume_wakeup(core_if->
1720                                                                        pcd_cb->
1721                                                                        p);
1722                                 }
1723                                 DWC_SPINLOCK(core_if->lock);
1724
1725                         }
1726
1727                         gintsts.b.restoredone = 1;
1728                         DWC_WRITE_REG32(&core_if->core_global_regs->gintsts,
1729                                         gintsts.d32);
1730                         DWC_PRINTF(" --Restore done interrupt received-- \n");
1731                         retval |= 1;
1732                 }
1733                 if (gintsts.b.portintr && dwc_otg_is_device_mode(core_if)) {
1734                         /* The port interrupt occurs while in device mode with HPRT0
1735                          * Port Enable/Disable.
1736                          */
1737                         gintsts.d32 = 0;
1738                         gintsts.b.portintr = 1;
1739                         DWC_WRITE_REG32(&core_if->core_global_regs->gintsts,
1740                                         gintsts.d32);
1741                         retval |= 1;
1742
1743                 }
1744         } else {
1745                 DWC_DEBUGPL(DBG_ANY, "gpwrdn=%08x\n", gpwrdn.d32);
1746
1747                 if (gpwrdn.b.disconn_det && gpwrdn.b.disconn_det_msk) {
1748                         CLEAR_GPWRDN_INTR(core_if, disconn_det);
1749                         if (gpwrdn.b.linestate == 0) {
1750                                 dwc_otg_handle_pwrdn_disconnect_intr(core_if);
1751                         } else {
1752                                 DWC_PRINTF
1753                                     ("Disconnect detected while linestate is not 0\n");
1754                         }
1755
1756                         retval |= 1;
1757                 }
1758                 if (gpwrdn.b.lnstschng && gpwrdn.b.lnstchng_msk) {
1759                         CLEAR_GPWRDN_INTR(core_if, lnstschng);
1760                         /* remote wakeup from hibernation */
1761                         if (gpwrdn.b.linestate == 2 || gpwrdn.b.linestate == 1) {
1762                                 dwc_otg_handle_pwrdn_wakeup_detected_intr
1763                                     (core_if);
1764                         } else {
1765                                 DWC_PRINTF("gpwrdn.linestate = %d\n",
1766                                            gpwrdn.b.linestate);
1767                         }
1768                         retval |= 1;
1769                 }
1770                 if (gpwrdn.b.rst_det && gpwrdn.b.rst_det_msk) {
1771                         CLEAR_GPWRDN_INTR(core_if, rst_det);
1772                         if (gpwrdn.b.linestate == 0) {
1773                                 DWC_PRINTF("Reset detected\n");
1774                                 retval |=
1775                                     dwc_otg_device_hibernation_restore(core_if,
1776                                                                        0, 1);
1777                         }
1778                 }
1779                 if (gpwrdn.b.srp_det && gpwrdn.b.srp_det_msk) {
1780                         CLEAR_GPWRDN_INTR(core_if, srp_det);
1781                         dwc_otg_handle_pwrdn_srp_intr(core_if);
1782                         retval |= 1;
1783                 }
1784         }
1785         /* Handle ADP interrupt here */
1786         if (gpwrdn.b.adp_int) {
1787                 DWC_PRINTF("ADP interrupt\n");
1788                 CLEAR_GPWRDN_INTR(core_if, adp_int);
1789                 dwc_otg_adp_handle_intr(core_if);
1790                 retval |= 1;
1791         }
1792         if (gpwrdn.b.sts_chngint && gpwrdn.b.sts_chngint_msk) {
1793                 DWC_PRINTF("STS CHNG interrupt asserted\n");
1794                 CLEAR_GPWRDN_INTR(core_if, sts_chngint);
1795                 dwc_otg_handle_pwrdn_stschng_intr(otg_dev);
1796
1797                 retval |= 1;
1798         }
1799         if (core_if->lock)
1800                 DWC_SPINUNLOCK(core_if->lock);
1801
1802         return retval;
1803 }