1 /* 2 * core_intr.c - DesignWare HS OTG Controller common interrupt handling 3 * 4 * Copyright (C) 2004-2013 Synopsys, Inc. 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: 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. 18 * 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 22 * later version. 23 * 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. 35 */ 36 37 /* 38 * This file contains the common interrupt handlers 39 */ 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> 46 #include <linux/io.h> 47 #include <linux/slab.h> 48 #include <linux/usb.h> 49 50 #include <linux/usb/hcd.h> 51 #include <linux/usb/ch11.h> 52 53 #include "core.h" 54 #include "hcd.h" 55 56 static const char *dwc2_op_state_str(struct dwc2_hsotg *hsotg) 57 { 58 switch (hsotg->op_state) { 59 case OTG_STATE_A_HOST: 60 return "a_host"; 61 case OTG_STATE_A_SUSPEND: 62 return "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: 68 return "b_host"; 69 default: 70 return "unknown"; 71 } 72 } 73 74 /** 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. 78 * 79 * @hsotg: Programming view of DWC_otg controller 80 */ 81 static void dwc2_handle_usb_port_intr(struct dwc2_hsotg *hsotg) 82 { 83 u32 hprt0 = dwc2_readl(hsotg->regs + HPRT0); 84 85 if (hprt0 & HPRT0_ENACHG) { 86 hprt0 &= ~HPRT0_ENA; 87 dwc2_writel(hprt0, hsotg->regs + HPRT0); 88 } 89 } 90 91 /** 92 * dwc2_handle_mode_mismatch_intr() - Logs a mode mismatch warning message 93 * 94 * @hsotg: Programming view of DWC_otg controller 95 */ 96 static void dwc2_handle_mode_mismatch_intr(struct dwc2_hsotg *hsotg) 97 { 98 /* Clear interrupt */ 99 dwc2_writel(GINTSTS_MODEMIS, hsotg->regs + GINTSTS); 100 101 dev_warn(hsotg->dev, "Mode Mismatch Interrupt: currently in %s mode\n", 102 dwc2_is_host_mode(hsotg) ? "Host" : "Device"); 103 } 104 105 /** 106 * dwc2_handle_otg_intr() - Handles the OTG Interrupts. It reads the OTG 107 * Interrupt Register (GOTGINT) to determine what interrupt has occurred. 108 * 109 * @hsotg: Programming view of DWC_otg controller 110 */ 111 static void dwc2_handle_otg_intr(struct dwc2_hsotg *hsotg) 112 { 113 u32 gotgint; 114 u32 gotgctl; 115 u32 gintmsk; 116 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)); 121 122 if (gotgint & GOTGINT_SES_END_DET) { 123 dev_dbg(hsotg->dev, 124 " ++OTG Interrupt: Session End Detected++ (%s)\n", 125 dwc2_op_state_str(hsotg)); 126 gotgctl = dwc2_readl(hsotg->regs + GOTGCTL); 127 128 if (dwc2_is_device_mode(hsotg)) 129 dwc2_hsotg_disconnect(hsotg); 130 131 if (hsotg->op_state == OTG_STATE_B_HOST) { 132 hsotg->op_state = OTG_STATE_B_PERIPHERAL; 133 } else { 134 /* 135 * If not B_HOST and Device HNP still set, HNP did 136 * not succeed! 137 */ 138 if (gotgctl & GOTGCTL_DEVHNPEN) { 139 dev_dbg(hsotg->dev, "Session End Detected\n"); 140 dev_err(hsotg->dev, 141 "Device Not Connected/Responding!\n"); 142 } 143 144 /* 145 * If Session End Detected the B-Cable has been 146 * disconnected 147 */ 148 /* Reset to a clean state */ 149 hsotg->lx_state = DWC2_L0; 150 } 151 152 gotgctl = dwc2_readl(hsotg->regs + GOTGCTL); 153 gotgctl &= ~GOTGCTL_DEVHNPEN; 154 dwc2_writel(gotgctl, hsotg->regs + GOTGCTL); 155 } 156 157 if (gotgint & GOTGINT_SES_REQ_SUC_STS_CHNG) { 158 dev_dbg(hsotg->dev, 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; 165 } else { 166 /* Clear Session Request */ 167 gotgctl = dwc2_readl(hsotg->regs + GOTGCTL); 168 gotgctl &= ~GOTGCTL_SESREQ; 169 dwc2_writel(gotgctl, hsotg->regs + GOTGCTL); 170 } 171 } 172 } 173 174 if (gotgint & GOTGINT_HST_NEG_SUC_STS_CHNG) { 175 /* 176 * Print statements during the HNP interrupt handling 177 * can cause it to fail 178 */ 179 gotgctl = dwc2_readl(hsotg->regs + GOTGCTL); 180 /* 181 * WA for 3.00a- HW is not setting cur_mode, even sometimes 182 * this does not help 183 */ 184 if (hsotg->hw_params.snpsid >= DWC2_CORE_REV_3_00a) 185 udelay(100); 186 if (gotgctl & GOTGCTL_HSTNEGSCS) { 187 if (dwc2_is_host_mode(hsotg)) { 188 hsotg->op_state = OTG_STATE_B_HOST; 189 /* 190 * Need to disable SOF interrupt immediately. 191 * When switching from device to host, the PCD 192 * interrupt handler won't handle the 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 197 * complains loudly. 198 */ 199 gintmsk = dwc2_readl(hsotg->regs + GINTMSK); 200 gintmsk &= ~GINTSTS_SOF; 201 dwc2_writel(gintmsk, hsotg->regs + GINTMSK); 202 203 /* 204 * Call callback function with spin lock 205 * released 206 */ 207 spin_unlock(&hsotg->lock); 208 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; 213 } 214 } else { 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"); 219 dev_err(hsotg->dev, 220 "Device Not Connected/Responding\n"); 221 } 222 } 223 224 if (gotgint & GOTGINT_HST_NEG_DET) { 225 /* 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. 230 */ 231 dev_dbg(hsotg->dev, 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", 236 hsotg->op_state); 237 spin_unlock(&hsotg->lock); 238 dwc2_hcd_disconnect(hsotg, false); 239 spin_lock(&hsotg->lock); 240 hsotg->op_state = OTG_STATE_A_PERIPHERAL; 241 } else { 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; 250 } 251 } 252 253 if (gotgint & GOTGINT_A_DEV_TOUT_CHG) 254 dev_dbg(hsotg->dev, 255 " ++OTG Interrupt: A-Device Timeout Change++\n"); 256 if (gotgint & GOTGINT_DBNCE_DONE) 257 dev_dbg(hsotg->dev, " ++OTG Interrupt: Debounce Done++\n"); 258 259 /* Clear GOTGINT */ 260 dwc2_writel(gotgint, hsotg->regs + GOTGINT); 261 } 262 263 /** 264 * dwc2_handle_conn_id_status_change_intr() - Handles the Connector ID Status 265 * Change Interrupt 266 * 267 * @hsotg: Programming view of DWC_otg controller 268 * 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. 272 */ 273 static void dwc2_handle_conn_id_status_change_intr(struct dwc2_hsotg *hsotg) 274 { 275 u32 gintmsk; 276 277 /* Clear interrupt */ 278 dwc2_writel(GINTSTS_CONIDSTSCHNG, hsotg->regs + GINTSTS); 279 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); 284 285 dev_dbg(hsotg->dev, " ++Connector ID Status Change Interrupt++ (%s)\n", 286 dwc2_is_host_mode(hsotg) ? "Host" : "Device"); 287 288 /* 289 * Need to schedule a work, as there are possible DELAY function calls. 290 * Release lock before scheduling workq as it holds spinlock during 291 * scheduling. 292 */ 293 if (hsotg->wq_otg) { 294 spin_unlock(&hsotg->lock); 295 queue_work(hsotg->wq_otg, &hsotg->wf_otg); 296 spin_lock(&hsotg->lock); 297 } 298 } 299 300 /** 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 304 * 305 * @hsotg: Programming view of DWC_otg controller 306 * 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. 310 */ 311 static void dwc2_handle_session_req_intr(struct dwc2_hsotg *hsotg) 312 { 313 int ret; 314 315 /* Clear interrupt */ 316 dwc2_writel(GINTSTS_SESSREQINT, hsotg->regs + GINTSTS); 317 318 dev_dbg(hsotg->dev, "Session request interrupt - lx_state=%d\n", 319 hsotg->lx_state); 320 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)) 325 dev_err(hsotg->dev, 326 "exit hibernation failed\n"); 327 } 328 329 /* 330 * Report disconnect if there is any previous session 331 * established 332 */ 333 dwc2_hsotg_disconnect(hsotg); 334 } 335 } 336 337 /* 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. 343 */ 344 static void dwc2_handle_wakeup_detected_intr(struct dwc2_hsotg *hsotg) 345 { 346 int ret; 347 348 /* Clear interrupt */ 349 dwc2_writel(GINTSTS_WKUPINT, hsotg->regs + GINTSTS); 350 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); 353 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); 359 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"); 366 367 call_gadget(hsotg, resume); 368 } 369 /* Change to L0 state */ 370 hsotg->lx_state = DWC2_L0; 371 } else { 372 if (hsotg->params.hibernation) 373 return; 374 375 if (hsotg->lx_state != DWC2_L1) { 376 u32 pcgcctl = dwc2_readl(hsotg->regs + PCGCTL); 377 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)); 383 } else { 384 /* Change to L0 state */ 385 hsotg->lx_state = DWC2_L0; 386 } 387 } 388 } 389 390 /* 391 * This interrupt indicates that a device has been disconnected from the 392 * root port 393 */ 394 static void dwc2_handle_disconnect_intr(struct dwc2_hsotg *hsotg) 395 { 396 dwc2_writel(GINTSTS_DISCONNINT, hsotg->regs + GINTSTS); 397 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)); 401 402 if (hsotg->op_state == OTG_STATE_A_HOST) 403 dwc2_hcd_disconnect(hsotg, false); 404 } 405 406 /* 407 * This interrupt indicates that SUSPEND state has been detected on the USB. 408 * 409 * For HNP the USB Suspend interrupt signals the change from "a_peripheral" 410 * to "a_host". 411 * 412 * When power management is enabled the core will be put in low power mode. 413 */ 414 static void dwc2_handle_usb_suspend_intr(struct dwc2_hsotg *hsotg) 415 { 416 u32 dsts; 417 int ret; 418 419 /* Clear interrupt */ 420 dwc2_writel(GINTSTS_USBSUSP, hsotg->regs + GINTSTS); 421 422 dev_dbg(hsotg->dev, "USB SUSPEND\n"); 423 424 if (dwc2_is_device_mode(hsotg)) { 425 /* 426 * Check the Device status register to determine if the Suspend 427 * state is active 428 */ 429 dsts = dwc2_readl(hsotg->regs + DSTS); 430 dev_dbg(hsotg->dev, "DSTS=0x%0x\n", dsts); 431 dev_dbg(hsotg->dev, 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)) { 438 dev_dbg(hsotg->dev, 439 "ignore suspend request before enumeration\n"); 440 return; 441 } 442 443 ret = dwc2_enter_hibernation(hsotg); 444 if (ret) { 445 if (ret != -ENOTSUPP) 446 dev_err(hsotg->dev, 447 "enter hibernation failed\n"); 448 goto skip_power_saving; 449 } 450 451 udelay(100); 452 453 /* Ask phy to be suspended */ 454 if (!IS_ERR_OR_NULL(hsotg->uphy)) 455 usb_phy_set_suspend(hsotg->uphy, true); 456 skip_power_saving: 457 /* 458 * Change to L2 (suspend) state before releasing 459 * spinlock 460 */ 461 hsotg->lx_state = DWC2_L2; 462 463 /* Call gadget suspend callback */ 464 call_gadget(hsotg, suspend); 465 } 466 } else { 467 if (hsotg->op_state == OTG_STATE_A_PERIPHERAL) { 468 dev_dbg(hsotg->dev, "a_peripheral->a_host\n"); 469 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; 477 } 478 } 479 } 480 481 #define GINTMSK_COMMON (GINTSTS_WKUPINT | GINTSTS_SESSREQINT | \ 482 GINTSTS_CONIDSTSCHNG | GINTSTS_OTGINT | \ 483 GINTSTS_MODEMIS | GINTSTS_DISCONNINT | \ 484 GINTSTS_USBSUSP | GINTSTS_PRTINT) 485 486 /* 487 * This function returns the Core Interrupt register 488 */ 489 static u32 dwc2_read_common_intr(struct dwc2_hsotg *hsotg) 490 { 491 u32 gintsts; 492 u32 gintmsk; 493 u32 gahbcfg; 494 u32 gintmsk_common = GINTMSK_COMMON; 495 496 gintsts = dwc2_readl(hsotg->regs + GINTSTS); 497 gintmsk = dwc2_readl(hsotg->regs + GINTMSK); 498 gahbcfg = dwc2_readl(hsotg->regs + GAHBCFG); 499 500 /* If any common interrupts set */ 501 if (gintsts & gintmsk_common) 502 dev_dbg(hsotg->dev, "gintsts=%08x gintmsk=%08x\n", 503 gintsts, gintmsk); 504 505 if (gahbcfg & GAHBCFG_GLBL_INTR_EN) 506 return gintsts & gintmsk & gintmsk_common; 507 else 508 return 0; 509 } 510 511 /* 512 * Common interrupt handler 513 * 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 517 * - OTG Interrupt 518 * - Connector ID Status Change Interrupt 519 * - Disconnect Interrupt 520 * - Session Request Interrupt 521 * - Resume / Remote Wakeup Detected Interrupt 522 * - Suspend Interrupt 523 */ 524 irqreturn_t dwc2_handle_common_intr(int irq, void *dev) 525 { 526 struct dwc2_hsotg *hsotg = dev; 527 u32 gintsts; 528 irqreturn_t retval = IRQ_NONE; 529 530 spin_lock(&hsotg->lock); 531 532 if (!dwc2_is_controller_alive(hsotg)) { 533 dev_warn(hsotg->dev, "Controller is dead\n"); 534 goto out; 535 } 536 537 gintsts = dwc2_read_common_intr(hsotg); 538 if (gintsts & ~GINTSTS_PRTINT) 539 retval = IRQ_HANDLED; 540 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); 555 556 if (gintsts & GINTSTS_PRTINT) { 557 /* 558 * The port interrupt occurs while in device mode with HPRT0 559 * Port Enable/Disable 560 */ 561 if (dwc2_is_device_mode(hsotg)) { 562 dev_dbg(hsotg->dev, 563 " --Port interrupt received in Device mode--\n"); 564 dwc2_handle_usb_port_intr(hsotg); 565 retval = IRQ_HANDLED; 566 } 567 } 568 569 out: 570 spin_unlock(&hsotg->lock); 571 return retval; 572 } 573