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
->params
.phy_type
== DWC2_PHY_TYPE_PARAM_FS
&&
163 hsotg
->params
.i2c_enable
) {
164 hsotg
->srp_success
= 1;
166 /* Clear Session Request */
167 gotgctl
= dwc2_readl(hsotg
->regs
+ GOTGCTL
);
168 gotgctl
&= ~GOTGCTL_SESREQ
;
169 dwc2_writel(gotgctl
, hsotg
->regs
+ GOTGCTL
);
174 if (gotgint
& GOTGINT_HST_NEG_SUC_STS_CHNG
) {
176 * Print statements during the HNP interrupt handling
177 * can cause it to fail
179 gotgctl
= dwc2_readl(hsotg
->regs
+ GOTGCTL
);
181 * WA for 3.00a- HW is not setting cur_mode, even sometimes
184 if (hsotg
->hw_params
.snpsid
>= DWC2_CORE_REV_3_00a
)
186 if (gotgctl
& GOTGCTL_HSTNEGSCS
) {
187 if (dwc2_is_host_mode(hsotg
)) {
188 hsotg
->op_state
= OTG_STATE_B_HOST
;
190 * Need to disable SOF interrupt immediately.
191 * When switching from device to host, the PCD
192 * interrupt handler won't handle the interrupt
193 * if host mode is already set. The HCD
194 * interrupt handler won't get called if the
195 * HCD state is HALT. This means that the
196 * interrupt does not get handled and Linux
199 gintmsk
= dwc2_readl(hsotg
->regs
+ GINTMSK
);
200 gintmsk
&= ~GINTSTS_SOF
;
201 dwc2_writel(gintmsk
, hsotg
->regs
+ GINTMSK
);
204 * Call callback function with spin lock
207 spin_unlock(&hsotg
->lock
);
209 /* Initialize the Core for Host mode */
210 dwc2_hcd_start(hsotg
);
211 spin_lock(&hsotg
->lock
);
212 hsotg
->op_state
= OTG_STATE_B_HOST
;
215 gotgctl
= dwc2_readl(hsotg
->regs
+ GOTGCTL
);
216 gotgctl
&= ~(GOTGCTL_HNPREQ
| GOTGCTL_DEVHNPEN
);
217 dwc2_writel(gotgctl
, hsotg
->regs
+ GOTGCTL
);
218 dev_dbg(hsotg
->dev
, "HNP Failed\n");
220 "Device Not Connected/Responding\n");
224 if (gotgint
& GOTGINT_HST_NEG_DET
) {
226 * The disconnect interrupt is set at the same time as
227 * Host Negotiation Detected. During the mode switch all
228 * interrupts are cleared so the disconnect interrupt
229 * handler will not get executed.
232 " ++OTG Interrupt: Host Negotiation Detected++ (%s)\n",
233 (dwc2_is_host_mode(hsotg
) ? "Host" : "Device"));
234 if (dwc2_is_device_mode(hsotg
)) {
235 dev_dbg(hsotg
->dev
, "a_suspend->a_peripheral (%d)\n",
237 spin_unlock(&hsotg
->lock
);
238 dwc2_hcd_disconnect(hsotg
, false);
239 spin_lock(&hsotg
->lock
);
240 hsotg
->op_state
= OTG_STATE_A_PERIPHERAL
;
242 /* Need to disable SOF interrupt immediately */
243 gintmsk
= dwc2_readl(hsotg
->regs
+ GINTMSK
);
244 gintmsk
&= ~GINTSTS_SOF
;
245 dwc2_writel(gintmsk
, hsotg
->regs
+ GINTMSK
);
246 spin_unlock(&hsotg
->lock
);
247 dwc2_hcd_start(hsotg
);
248 spin_lock(&hsotg
->lock
);
249 hsotg
->op_state
= OTG_STATE_A_HOST
;
253 if (gotgint
& GOTGINT_A_DEV_TOUT_CHG
)
255 " ++OTG Interrupt: A-Device Timeout Change++\n");
256 if (gotgint
& GOTGINT_DBNCE_DONE
)
257 dev_dbg(hsotg
->dev
, " ++OTG Interrupt: Debounce Done++\n");
260 dwc2_writel(gotgint
, hsotg
->regs
+ GOTGINT
);
264 * dwc2_handle_conn_id_status_change_intr() - Handles the Connector ID Status
267 * @hsotg: Programming view of DWC_otg controller
269 * Reads the OTG Interrupt Register (GOTCTL) to determine whether this is a
270 * Device to Host Mode transition or a Host to Device Mode transition. This only
271 * occurs when the cable is connected/removed from the PHY connector.
273 static void dwc2_handle_conn_id_status_change_intr(struct dwc2_hsotg
*hsotg
)
277 /* Clear interrupt */
278 dwc2_writel(GINTSTS_CONIDSTSCHNG
, hsotg
->regs
+ GINTSTS
);
280 /* Need to disable SOF interrupt immediately */
281 gintmsk
= dwc2_readl(hsotg
->regs
+ GINTMSK
);
282 gintmsk
&= ~GINTSTS_SOF
;
283 dwc2_writel(gintmsk
, hsotg
->regs
+ GINTMSK
);
285 dev_dbg(hsotg
->dev
, " ++Connector ID Status Change Interrupt++ (%s)\n",
286 dwc2_is_host_mode(hsotg
) ? "Host" : "Device");
289 * Need to schedule a work, as there are possible DELAY function calls.
290 * Release lock before scheduling workq as it holds spinlock during
294 spin_unlock(&hsotg
->lock
);
295 queue_work(hsotg
->wq_otg
, &hsotg
->wf_otg
);
296 spin_lock(&hsotg
->lock
);
301 * dwc2_handle_session_req_intr() - This interrupt indicates that a device is
302 * initiating the Session Request Protocol to request the host to turn on bus
303 * power so a new session can begin
305 * @hsotg: Programming view of DWC_otg controller
307 * This handler responds by turning on bus power. If the DWC_otg controller is
308 * in low power mode, this handler brings the controller out of low power mode
309 * before turning on bus power.
311 static void dwc2_handle_session_req_intr(struct dwc2_hsotg
*hsotg
)
315 /* Clear interrupt */
316 dwc2_writel(GINTSTS_SESSREQINT
, hsotg
->regs
+ GINTSTS
);
318 dev_dbg(hsotg
->dev
, "Session request interrupt - lx_state=%d\n",
321 if (dwc2_is_device_mode(hsotg
)) {
322 if (hsotg
->lx_state
== DWC2_L2
) {
323 ret
= dwc2_exit_hibernation(hsotg
, true);
324 if (ret
&& (ret
!= -ENOTSUPP
))
326 "exit hibernation failed\n");
330 * Report disconnect if there is any previous session
333 dwc2_hsotg_disconnect(hsotg
);
338 * This interrupt indicates that the DWC_otg controller has detected a
339 * resume or remote wakeup sequence. If the DWC_otg controller is in
340 * low power mode, the handler must brings the controller out of low
341 * power mode. The controller automatically begins resume signaling.
342 * The handler schedules a time to stop resume signaling.
344 static void dwc2_handle_wakeup_detected_intr(struct dwc2_hsotg
*hsotg
)
348 /* Clear interrupt */
349 dwc2_writel(GINTSTS_WKUPINT
, hsotg
->regs
+ GINTSTS
);
351 dev_dbg(hsotg
->dev
, "++Resume or Remote Wakeup Detected Interrupt++\n");
352 dev_dbg(hsotg
->dev
, "%s lxstate = %d\n", __func__
, hsotg
->lx_state
);
354 if (dwc2_is_device_mode(hsotg
)) {
355 dev_dbg(hsotg
->dev
, "DSTS=0x%0x\n",
356 dwc2_readl(hsotg
->regs
+ DSTS
));
357 if (hsotg
->lx_state
== DWC2_L2
) {
358 u32 dctl
= dwc2_readl(hsotg
->regs
+ DCTL
);
360 /* Clear Remote Wakeup Signaling */
361 dctl
&= ~DCTL_RMTWKUPSIG
;
362 dwc2_writel(dctl
, hsotg
->regs
+ DCTL
);
363 ret
= dwc2_exit_hibernation(hsotg
, true);
364 if (ret
&& (ret
!= -ENOTSUPP
))
365 dev_err(hsotg
->dev
, "exit hibernation failed\n");
367 call_gadget(hsotg
, resume
);
369 /* Change to L0 state */
370 hsotg
->lx_state
= DWC2_L0
;
372 if (hsotg
->params
.hibernation
)
375 if (hsotg
->lx_state
!= DWC2_L1
) {
376 u32 pcgcctl
= dwc2_readl(hsotg
->regs
+ PCGCTL
);
378 /* Restart the Phy Clock */
379 pcgcctl
&= ~PCGCTL_STOPPCLK
;
380 dwc2_writel(pcgcctl
, hsotg
->regs
+ PCGCTL
);
381 mod_timer(&hsotg
->wkp_timer
,
382 jiffies
+ msecs_to_jiffies(71));
384 /* Change to L0 state */
385 hsotg
->lx_state
= DWC2_L0
;
391 * This interrupt indicates that a device has been disconnected from the
394 static void dwc2_handle_disconnect_intr(struct dwc2_hsotg
*hsotg
)
396 dwc2_writel(GINTSTS_DISCONNINT
, hsotg
->regs
+ GINTSTS
);
398 dev_dbg(hsotg
->dev
, "++Disconnect Detected Interrupt++ (%s) %s\n",
399 dwc2_is_host_mode(hsotg
) ? "Host" : "Device",
400 dwc2_op_state_str(hsotg
));
402 if (hsotg
->op_state
== OTG_STATE_A_HOST
)
403 dwc2_hcd_disconnect(hsotg
, false);
407 * This interrupt indicates that SUSPEND state has been detected on the USB.
409 * For HNP the USB Suspend interrupt signals the change from "a_peripheral"
412 * When power management is enabled the core will be put in low power mode.
414 static void dwc2_handle_usb_suspend_intr(struct dwc2_hsotg
*hsotg
)
419 /* Clear interrupt */
420 dwc2_writel(GINTSTS_USBSUSP
, hsotg
->regs
+ GINTSTS
);
422 dev_dbg(hsotg
->dev
, "USB SUSPEND\n");
424 if (dwc2_is_device_mode(hsotg
)) {
426 * Check the Device status register to determine if the Suspend
429 dsts
= dwc2_readl(hsotg
->regs
+ DSTS
);
430 dev_dbg(hsotg
->dev
, "DSTS=0x%0x\n", dsts
);
432 "DSTS.Suspend Status=%d HWCFG4.Power Optimize=%d\n",
433 !!(dsts
& DSTS_SUSPSTS
),
434 hsotg
->hw_params
.power_optimized
);
435 if ((dsts
& DSTS_SUSPSTS
) && hsotg
->hw_params
.power_optimized
) {
436 /* Ignore suspend request before enumeration */
437 if (!dwc2_is_device_connected(hsotg
)) {
439 "ignore suspend request before enumeration\n");
443 ret
= dwc2_enter_hibernation(hsotg
);
445 if (ret
!= -ENOTSUPP
)
447 "enter hibernation failed\n");
448 goto skip_power_saving
;
453 /* Ask phy to be suspended */
454 if (!IS_ERR_OR_NULL(hsotg
->uphy
))
455 usb_phy_set_suspend(hsotg
->uphy
, true);
458 * Change to L2 (suspend) state before releasing
461 hsotg
->lx_state
= DWC2_L2
;
463 /* Call gadget suspend callback */
464 call_gadget(hsotg
, suspend
);
467 if (hsotg
->op_state
== OTG_STATE_A_PERIPHERAL
) {
468 dev_dbg(hsotg
->dev
, "a_peripheral->a_host\n");
470 /* Change to L2 (suspend) state */
471 hsotg
->lx_state
= DWC2_L2
;
472 /* Clear the a_peripheral flag, back to a_host */
473 spin_unlock(&hsotg
->lock
);
474 dwc2_hcd_start(hsotg
);
475 spin_lock(&hsotg
->lock
);
476 hsotg
->op_state
= OTG_STATE_A_HOST
;
481 #define GINTMSK_COMMON (GINTSTS_WKUPINT | GINTSTS_SESSREQINT | \
482 GINTSTS_CONIDSTSCHNG | GINTSTS_OTGINT | \
483 GINTSTS_MODEMIS | GINTSTS_DISCONNINT | \
484 GINTSTS_USBSUSP | GINTSTS_PRTINT)
487 * This function returns the Core Interrupt register
489 static u32
dwc2_read_common_intr(struct dwc2_hsotg
*hsotg
)
494 u32 gintmsk_common
= GINTMSK_COMMON
;
496 gintsts
= dwc2_readl(hsotg
->regs
+ GINTSTS
);
497 gintmsk
= dwc2_readl(hsotg
->regs
+ GINTMSK
);
498 gahbcfg
= dwc2_readl(hsotg
->regs
+ GAHBCFG
);
500 /* If any common interrupts set */
501 if (gintsts
& gintmsk_common
)
502 dev_dbg(hsotg
->dev
, "gintsts=%08x gintmsk=%08x\n",
505 if (gahbcfg
& GAHBCFG_GLBL_INTR_EN
)
506 return gintsts
& gintmsk
& gintmsk_common
;
512 * Common interrupt handler
514 * The common interrupts are those that occur in both Host and Device mode.
515 * This handler handles the following interrupts:
516 * - Mode Mismatch Interrupt
518 * - Connector ID Status Change Interrupt
519 * - Disconnect Interrupt
520 * - Session Request Interrupt
521 * - Resume / Remote Wakeup Detected Interrupt
522 * - Suspend Interrupt
524 irqreturn_t
dwc2_handle_common_intr(int irq
, void *dev
)
526 struct dwc2_hsotg
*hsotg
= dev
;
528 irqreturn_t retval
= IRQ_NONE
;
530 spin_lock(&hsotg
->lock
);
532 if (!dwc2_is_controller_alive(hsotg
)) {
533 dev_warn(hsotg
->dev
, "Controller is dead\n");
537 gintsts
= dwc2_read_common_intr(hsotg
);
538 if (gintsts
& ~GINTSTS_PRTINT
)
539 retval
= IRQ_HANDLED
;
541 if (gintsts
& GINTSTS_MODEMIS
)
542 dwc2_handle_mode_mismatch_intr(hsotg
);
543 if (gintsts
& GINTSTS_OTGINT
)
544 dwc2_handle_otg_intr(hsotg
);
545 if (gintsts
& GINTSTS_CONIDSTSCHNG
)
546 dwc2_handle_conn_id_status_change_intr(hsotg
);
547 if (gintsts
& GINTSTS_DISCONNINT
)
548 dwc2_handle_disconnect_intr(hsotg
);
549 if (gintsts
& GINTSTS_SESSREQINT
)
550 dwc2_handle_session_req_intr(hsotg
);
551 if (gintsts
& GINTSTS_WKUPINT
)
552 dwc2_handle_wakeup_detected_intr(hsotg
);
553 if (gintsts
& GINTSTS_USBSUSP
)
554 dwc2_handle_usb_suspend_intr(hsotg
);
556 if (gintsts
& GINTSTS_PRTINT
) {
558 * The port interrupt occurs while in device mode with HPRT0
559 * Port Enable/Disable
561 if (dwc2_is_device_mode(hsotg
)) {
563 " --Port interrupt received in Device mode--\n");
564 dwc2_handle_usb_port_intr(hsotg
);
565 retval
= IRQ_HANDLED
;
570 spin_unlock(&hsotg
->lock
);