2 * core_intr.c - DesignWare HS OTG Controller common interrupt handling
4 * Copyright (C) 2004-2013 Synopsys, Inc.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions, and the following disclaimer,
11 * without modification.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. The names of the above-listed copyright holders may not be used
16 * to endorse or promote products derived from this software without
17 * specific prior written permission.
19 * ALTERNATIVELY, this software may be distributed under the terms of the
20 * GNU General Public License ("GPL") as published by the Free Software
21 * Foundation; either version 2 of the License, or (at your option) any
24 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
25 * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
26 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
27 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
28 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
29 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
30 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
31 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
32 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
33 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
34 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
38 * This file contains the common interrupt handlers
40 #include <linux/kernel.h>
41 #include <linux/module.h>
42 #include <linux/moduleparam.h>
43 #include <linux/spinlock.h>
44 #include <linux/interrupt.h>
45 #include <linux/dma-mapping.h>
47 #include <linux/slab.h>
48 #include <linux/usb.h>
50 #include <linux/usb/hcd.h>
51 #include <linux/usb/ch11.h>
56 static const char *dwc2_op_state_str(struct dwc2_hsotg
*hsotg
)
58 switch (hsotg
->op_state
) {
59 case OTG_STATE_A_HOST
:
61 case OTG_STATE_A_SUSPEND
:
63 case OTG_STATE_A_PERIPHERAL
:
64 return "a_peripheral";
65 case OTG_STATE_B_PERIPHERAL
:
66 return "b_peripheral";
67 case OTG_STATE_B_HOST
:
75 * dwc2_handle_usb_port_intr - handles OTG PRTINT interrupts.
76 * When the PRTINT interrupt fires, there are certain status bits in the Host
77 * Port that needs to get cleared.
79 * @hsotg: Programming view of DWC_otg controller
81 static void dwc2_handle_usb_port_intr(struct dwc2_hsotg
*hsotg
)
83 u32 hprt0
= dwc2_readl(hsotg
->regs
+ HPRT0
);
85 if (hprt0
& HPRT0_ENACHG
) {
87 dwc2_writel(hprt0
, hsotg
->regs
+ HPRT0
);
92 * dwc2_handle_mode_mismatch_intr() - Logs a mode mismatch warning message
94 * @hsotg: Programming view of DWC_otg controller
96 static void dwc2_handle_mode_mismatch_intr(struct dwc2_hsotg
*hsotg
)
99 dwc2_writel(GINTSTS_MODEMIS
, hsotg
->regs
+ GINTSTS
);
101 dev_warn(hsotg
->dev
, "Mode Mismatch Interrupt: currently in %s mode\n",
102 dwc2_is_host_mode(hsotg
) ? "Host" : "Device");
106 * dwc2_handle_otg_intr() - Handles the OTG Interrupts. It reads the OTG
107 * Interrupt Register (GOTGINT) to determine what interrupt has occurred.
109 * @hsotg: Programming view of DWC_otg controller
111 static void dwc2_handle_otg_intr(struct dwc2_hsotg
*hsotg
)
117 gotgint
= dwc2_readl(hsotg
->regs
+ GOTGINT
);
118 gotgctl
= dwc2_readl(hsotg
->regs
+ GOTGCTL
);
119 dev_dbg(hsotg
->dev
, "++OTG Interrupt gotgint=%0x [%s]\n", gotgint
,
120 dwc2_op_state_str(hsotg
));
122 if (gotgint
& GOTGINT_SES_END_DET
) {
124 " ++OTG Interrupt: Session End Detected++ (%s)\n",
125 dwc2_op_state_str(hsotg
));
126 gotgctl
= dwc2_readl(hsotg
->regs
+ GOTGCTL
);
128 if (dwc2_is_device_mode(hsotg
))
129 dwc2_hsotg_disconnect(hsotg
);
131 if (hsotg
->op_state
== OTG_STATE_B_HOST
) {
132 hsotg
->op_state
= OTG_STATE_B_PERIPHERAL
;
135 * If not B_HOST and Device HNP still set, HNP did
138 if (gotgctl
& GOTGCTL_DEVHNPEN
) {
139 dev_dbg(hsotg
->dev
, "Session End Detected\n");
141 "Device Not Connected/Responding!\n");
145 * If Session End Detected the B-Cable has been
148 /* Reset to a clean state */
149 hsotg
->lx_state
= DWC2_L0
;
152 gotgctl
= dwc2_readl(hsotg
->regs
+ GOTGCTL
);
153 gotgctl
&= ~GOTGCTL_DEVHNPEN
;
154 dwc2_writel(gotgctl
, hsotg
->regs
+ GOTGCTL
);
157 if (gotgint
& GOTGINT_SES_REQ_SUC_STS_CHNG
) {
159 " ++OTG Interrupt: Session Request Success Status Change++\n");
160 gotgctl
= dwc2_readl(hsotg
->regs
+ GOTGCTL
);
161 if (gotgctl
& GOTGCTL_SESREQSCS
) {
162 if (hsotg
->core_params
->phy_type
==
163 DWC2_PHY_TYPE_PARAM_FS
164 && hsotg
->core_params
->i2c_enable
> 0) {
165 hsotg
->srp_success
= 1;
167 /* Clear Session Request */
168 gotgctl
= dwc2_readl(hsotg
->regs
+ GOTGCTL
);
169 gotgctl
&= ~GOTGCTL_SESREQ
;
170 dwc2_writel(gotgctl
, hsotg
->regs
+ GOTGCTL
);
175 if (gotgint
& GOTGINT_HST_NEG_SUC_STS_CHNG
) {
177 * Print statements during the HNP interrupt handling
178 * can cause it to fail
180 gotgctl
= dwc2_readl(hsotg
->regs
+ GOTGCTL
);
182 * WA for 3.00a- HW is not setting cur_mode, even sometimes
185 if (hsotg
->hw_params
.snpsid
>= DWC2_CORE_REV_3_00a
)
187 if (gotgctl
& GOTGCTL_HSTNEGSCS
) {
188 if (dwc2_is_host_mode(hsotg
)) {
189 hsotg
->op_state
= OTG_STATE_B_HOST
;
191 * Need to disable SOF interrupt immediately.
192 * When switching from device to host, the PCD
193 * interrupt handler won't handle the interrupt
194 * if host mode is already set. The HCD
195 * interrupt handler won't get called if the
196 * HCD state is HALT. This means that the
197 * interrupt does not get handled and Linux
200 gintmsk
= dwc2_readl(hsotg
->regs
+ GINTMSK
);
201 gintmsk
&= ~GINTSTS_SOF
;
202 dwc2_writel(gintmsk
, hsotg
->regs
+ GINTMSK
);
205 * Call callback function with spin lock
208 spin_unlock(&hsotg
->lock
);
210 /* Initialize the Core for Host mode */
211 dwc2_hcd_start(hsotg
);
212 spin_lock(&hsotg
->lock
);
213 hsotg
->op_state
= OTG_STATE_B_HOST
;
216 gotgctl
= dwc2_readl(hsotg
->regs
+ GOTGCTL
);
217 gotgctl
&= ~(GOTGCTL_HNPREQ
| GOTGCTL_DEVHNPEN
);
218 dwc2_writel(gotgctl
, hsotg
->regs
+ GOTGCTL
);
219 dev_dbg(hsotg
->dev
, "HNP Failed\n");
221 "Device Not Connected/Responding\n");
225 if (gotgint
& GOTGINT_HST_NEG_DET
) {
227 * The disconnect interrupt is set at the same time as
228 * Host Negotiation Detected. During the mode switch all
229 * interrupts are cleared so the disconnect interrupt
230 * handler will not get executed.
233 " ++OTG Interrupt: Host Negotiation Detected++ (%s)\n",
234 (dwc2_is_host_mode(hsotg
) ? "Host" : "Device"));
235 if (dwc2_is_device_mode(hsotg
)) {
236 dev_dbg(hsotg
->dev
, "a_suspend->a_peripheral (%d)\n",
238 spin_unlock(&hsotg
->lock
);
239 dwc2_hcd_disconnect(hsotg
, false);
240 spin_lock(&hsotg
->lock
);
241 hsotg
->op_state
= OTG_STATE_A_PERIPHERAL
;
243 /* Need to disable SOF interrupt immediately */
244 gintmsk
= dwc2_readl(hsotg
->regs
+ GINTMSK
);
245 gintmsk
&= ~GINTSTS_SOF
;
246 dwc2_writel(gintmsk
, hsotg
->regs
+ GINTMSK
);
247 spin_unlock(&hsotg
->lock
);
248 dwc2_hcd_start(hsotg
);
249 spin_lock(&hsotg
->lock
);
250 hsotg
->op_state
= OTG_STATE_A_HOST
;
254 if (gotgint
& GOTGINT_A_DEV_TOUT_CHG
)
256 " ++OTG Interrupt: A-Device Timeout Change++\n");
257 if (gotgint
& GOTGINT_DBNCE_DONE
)
258 dev_dbg(hsotg
->dev
, " ++OTG Interrupt: Debounce Done++\n");
261 dwc2_writel(gotgint
, hsotg
->regs
+ GOTGINT
);
265 * dwc2_handle_conn_id_status_change_intr() - Handles the Connector ID Status
268 * @hsotg: Programming view of DWC_otg controller
270 * Reads the OTG Interrupt Register (GOTCTL) to determine whether this is a
271 * Device to Host Mode transition or a Host to Device Mode transition. This only
272 * occurs when the cable is connected/removed from the PHY connector.
274 static void dwc2_handle_conn_id_status_change_intr(struct dwc2_hsotg
*hsotg
)
278 /* Clear interrupt */
279 dwc2_writel(GINTSTS_CONIDSTSCHNG
, hsotg
->regs
+ GINTSTS
);
281 /* Need to disable SOF interrupt immediately */
282 gintmsk
= dwc2_readl(hsotg
->regs
+ GINTMSK
);
283 gintmsk
&= ~GINTSTS_SOF
;
284 dwc2_writel(gintmsk
, hsotg
->regs
+ GINTMSK
);
286 dev_dbg(hsotg
->dev
, " ++Connector ID Status Change Interrupt++ (%s)\n",
287 dwc2_is_host_mode(hsotg
) ? "Host" : "Device");
290 * Need to schedule a work, as there are possible DELAY function calls.
291 * Release lock before scheduling workq as it holds spinlock during
295 spin_unlock(&hsotg
->lock
);
296 queue_work(hsotg
->wq_otg
, &hsotg
->wf_otg
);
297 spin_lock(&hsotg
->lock
);
302 * dwc2_handle_session_req_intr() - This interrupt indicates that a device is
303 * initiating the Session Request Protocol to request the host to turn on bus
304 * power so a new session can begin
306 * @hsotg: Programming view of DWC_otg controller
308 * This handler responds by turning on bus power. If the DWC_otg controller is
309 * in low power mode, this handler brings the controller out of low power mode
310 * before turning on bus power.
312 static void dwc2_handle_session_req_intr(struct dwc2_hsotg
*hsotg
)
316 /* Clear interrupt */
317 dwc2_writel(GINTSTS_SESSREQINT
, hsotg
->regs
+ GINTSTS
);
319 dev_dbg(hsotg
->dev
, "Session request interrupt - lx_state=%d\n",
322 if (dwc2_is_device_mode(hsotg
)) {
323 if (hsotg
->lx_state
== DWC2_L2
) {
324 ret
= dwc2_exit_hibernation(hsotg
, true);
325 if (ret
&& (ret
!= -ENOTSUPP
))
327 "exit hibernation failed\n");
331 * Report disconnect if there is any previous session
334 dwc2_hsotg_disconnect(hsotg
);
339 * This interrupt indicates that the DWC_otg controller has detected a
340 * resume or remote wakeup sequence. If the DWC_otg controller is in
341 * low power mode, the handler must brings the controller out of low
342 * power mode. The controller automatically begins resume signaling.
343 * The handler schedules a time to stop resume signaling.
345 static void dwc2_handle_wakeup_detected_intr(struct dwc2_hsotg
*hsotg
)
349 /* Clear interrupt */
350 dwc2_writel(GINTSTS_WKUPINT
, hsotg
->regs
+ GINTSTS
);
352 dev_dbg(hsotg
->dev
, "++Resume or Remote Wakeup Detected Interrupt++\n");
353 dev_dbg(hsotg
->dev
, "%s lxstate = %d\n", __func__
, hsotg
->lx_state
);
355 if (dwc2_is_device_mode(hsotg
)) {
356 dev_dbg(hsotg
->dev
, "DSTS=0x%0x\n",
357 dwc2_readl(hsotg
->regs
+ DSTS
));
358 if (hsotg
->lx_state
== DWC2_L2
) {
359 u32 dctl
= dwc2_readl(hsotg
->regs
+ DCTL
);
361 /* Clear Remote Wakeup Signaling */
362 dctl
&= ~DCTL_RMTWKUPSIG
;
363 dwc2_writel(dctl
, hsotg
->regs
+ DCTL
);
364 ret
= dwc2_exit_hibernation(hsotg
, true);
365 if (ret
&& (ret
!= -ENOTSUPP
))
366 dev_err(hsotg
->dev
, "exit hibernation failed\n");
368 call_gadget(hsotg
, resume
);
370 /* Change to L0 state */
371 hsotg
->lx_state
= DWC2_L0
;
373 if (hsotg
->core_params
->hibernation
)
376 if (hsotg
->lx_state
!= DWC2_L1
) {
377 u32 pcgcctl
= dwc2_readl(hsotg
->regs
+ PCGCTL
);
379 /* Restart the Phy Clock */
380 pcgcctl
&= ~PCGCTL_STOPPCLK
;
381 dwc2_writel(pcgcctl
, hsotg
->regs
+ PCGCTL
);
382 mod_timer(&hsotg
->wkp_timer
,
383 jiffies
+ msecs_to_jiffies(71));
385 /* Change to L0 state */
386 hsotg
->lx_state
= DWC2_L0
;
392 * This interrupt indicates that a device has been disconnected from the
395 static void dwc2_handle_disconnect_intr(struct dwc2_hsotg
*hsotg
)
397 dwc2_writel(GINTSTS_DISCONNINT
, hsotg
->regs
+ GINTSTS
);
399 dev_dbg(hsotg
->dev
, "++Disconnect Detected Interrupt++ (%s) %s\n",
400 dwc2_is_host_mode(hsotg
) ? "Host" : "Device",
401 dwc2_op_state_str(hsotg
));
403 if (hsotg
->op_state
== OTG_STATE_A_HOST
)
404 dwc2_hcd_disconnect(hsotg
, false);
408 * This interrupt indicates that SUSPEND state has been detected on the USB.
410 * For HNP the USB Suspend interrupt signals the change from "a_peripheral"
413 * When power management is enabled the core will be put in low power mode.
415 static void dwc2_handle_usb_suspend_intr(struct dwc2_hsotg
*hsotg
)
420 /* Clear interrupt */
421 dwc2_writel(GINTSTS_USBSUSP
, hsotg
->regs
+ GINTSTS
);
423 dev_dbg(hsotg
->dev
, "USB SUSPEND\n");
425 if (dwc2_is_device_mode(hsotg
)) {
427 * Check the Device status register to determine if the Suspend
430 dsts
= dwc2_readl(hsotg
->regs
+ DSTS
);
431 dev_dbg(hsotg
->dev
, "DSTS=0x%0x\n", dsts
);
433 "DSTS.Suspend Status=%d HWCFG4.Power Optimize=%d\n",
434 !!(dsts
& DSTS_SUSPSTS
),
435 hsotg
->hw_params
.power_optimized
);
436 if ((dsts
& DSTS_SUSPSTS
) && hsotg
->hw_params
.power_optimized
) {
437 /* Ignore suspend request before enumeration */
438 if (!dwc2_is_device_connected(hsotg
)) {
440 "ignore suspend request before enumeration\n");
444 ret
= dwc2_enter_hibernation(hsotg
);
446 if (ret
!= -ENOTSUPP
)
448 "enter hibernation failed\n");
449 goto skip_power_saving
;
454 /* Ask phy to be suspended */
455 if (!IS_ERR_OR_NULL(hsotg
->uphy
))
456 usb_phy_set_suspend(hsotg
->uphy
, true);
459 * Change to L2 (suspend) state before releasing
462 hsotg
->lx_state
= DWC2_L2
;
464 /* Call gadget suspend callback */
465 call_gadget(hsotg
, suspend
);
468 if (hsotg
->op_state
== OTG_STATE_A_PERIPHERAL
) {
469 dev_dbg(hsotg
->dev
, "a_peripheral->a_host\n");
471 /* Change to L2 (suspend) state */
472 hsotg
->lx_state
= DWC2_L2
;
473 /* Clear the a_peripheral flag, back to a_host */
474 spin_unlock(&hsotg
->lock
);
475 dwc2_hcd_start(hsotg
);
476 spin_lock(&hsotg
->lock
);
477 hsotg
->op_state
= OTG_STATE_A_HOST
;
482 #define GINTMSK_COMMON (GINTSTS_WKUPINT | GINTSTS_SESSREQINT | \
483 GINTSTS_CONIDSTSCHNG | GINTSTS_OTGINT | \
484 GINTSTS_MODEMIS | GINTSTS_DISCONNINT | \
485 GINTSTS_USBSUSP | GINTSTS_PRTINT)
488 * This function returns the Core Interrupt register
490 static u32
dwc2_read_common_intr(struct dwc2_hsotg
*hsotg
)
495 u32 gintmsk_common
= GINTMSK_COMMON
;
497 gintsts
= dwc2_readl(hsotg
->regs
+ GINTSTS
);
498 gintmsk
= dwc2_readl(hsotg
->regs
+ GINTMSK
);
499 gahbcfg
= dwc2_readl(hsotg
->regs
+ GAHBCFG
);
501 /* If any common interrupts set */
502 if (gintsts
& gintmsk_common
)
503 dev_dbg(hsotg
->dev
, "gintsts=%08x gintmsk=%08x\n",
506 if (gahbcfg
& GAHBCFG_GLBL_INTR_EN
)
507 return gintsts
& gintmsk
& gintmsk_common
;
513 * Common interrupt handler
515 * The common interrupts are those that occur in both Host and Device mode.
516 * This handler handles the following interrupts:
517 * - Mode Mismatch Interrupt
519 * - Connector ID Status Change Interrupt
520 * - Disconnect Interrupt
521 * - Session Request Interrupt
522 * - Resume / Remote Wakeup Detected Interrupt
523 * - Suspend Interrupt
525 irqreturn_t
dwc2_handle_common_intr(int irq
, void *dev
)
527 struct dwc2_hsotg
*hsotg
= dev
;
529 irqreturn_t retval
= IRQ_NONE
;
531 spin_lock(&hsotg
->lock
);
533 if (!dwc2_is_controller_alive(hsotg
)) {
534 dev_warn(hsotg
->dev
, "Controller is dead\n");
538 gintsts
= dwc2_read_common_intr(hsotg
);
539 if (gintsts
& ~GINTSTS_PRTINT
)
540 retval
= IRQ_HANDLED
;
542 if (gintsts
& GINTSTS_MODEMIS
)
543 dwc2_handle_mode_mismatch_intr(hsotg
);
544 if (gintsts
& GINTSTS_OTGINT
)
545 dwc2_handle_otg_intr(hsotg
);
546 if (gintsts
& GINTSTS_CONIDSTSCHNG
)
547 dwc2_handle_conn_id_status_change_intr(hsotg
);
548 if (gintsts
& GINTSTS_DISCONNINT
)
549 dwc2_handle_disconnect_intr(hsotg
);
550 if (gintsts
& GINTSTS_SESSREQINT
)
551 dwc2_handle_session_req_intr(hsotg
);
552 if (gintsts
& GINTSTS_WKUPINT
)
553 dwc2_handle_wakeup_detected_intr(hsotg
);
554 if (gintsts
& GINTSTS_USBSUSP
)
555 dwc2_handle_usb_suspend_intr(hsotg
);
557 if (gintsts
& GINTSTS_PRTINT
) {
559 * The port interrupt occurs while in device mode with HPRT0
560 * Port Enable/Disable
562 if (dwc2_is_device_mode(hsotg
)) {
564 " --Port interrupt received in Device mode--\n");
565 dwc2_handle_usb_port_intr(hsotg
);
566 retval
= IRQ_HANDLED
;
571 spin_unlock(&hsotg
->lock
);