1 1.11 skrll /* $NetBSD: dwc2_coreintr.c,v 1.11 2016/02/24 22:17:54 skrll Exp $ */ 2 1.5 skrll 3 1.1 skrll /* 4 1.1 skrll * core_intr.c - DesignWare HS OTG Controller common interrupt handling 5 1.1 skrll * 6 1.1 skrll * Copyright (C) 2004-2013 Synopsys, Inc. 7 1.1 skrll * 8 1.1 skrll * Redistribution and use in source and binary forms, with or without 9 1.1 skrll * modification, are permitted provided that the following conditions 10 1.1 skrll * are met: 11 1.1 skrll * 1. Redistributions of source code must retain the above copyright 12 1.1 skrll * notice, this list of conditions, and the following disclaimer, 13 1.1 skrll * without modification. 14 1.1 skrll * 2. Redistributions in binary form must reproduce the above copyright 15 1.1 skrll * notice, this list of conditions and the following disclaimer in the 16 1.1 skrll * documentation and/or other materials provided with the distribution. 17 1.1 skrll * 3. The names of the above-listed copyright holders may not be used 18 1.1 skrll * to endorse or promote products derived from this software without 19 1.1 skrll * specific prior written permission. 20 1.1 skrll * 21 1.1 skrll * ALTERNATIVELY, this software may be distributed under the terms of the 22 1.1 skrll * GNU General Public License ("GPL") as published by the Free Software 23 1.1 skrll * Foundation; either version 2 of the License, or (at your option) any 24 1.1 skrll * later version. 25 1.1 skrll * 26 1.1 skrll * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS 27 1.1 skrll * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, 28 1.1 skrll * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 29 1.1 skrll * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR 30 1.1 skrll * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 31 1.1 skrll * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 32 1.1 skrll * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 33 1.1 skrll * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 34 1.1 skrll * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 35 1.1 skrll * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 36 1.1 skrll * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 37 1.1 skrll */ 38 1.1 skrll 39 1.1 skrll /* 40 1.1 skrll * This file contains the common interrupt handlers 41 1.1 skrll */ 42 1.2 skrll 43 1.2 skrll #include <sys/cdefs.h> 44 1.11 skrll __KERNEL_RCSID(0, "$NetBSD: dwc2_coreintr.c,v 1.11 2016/02/24 22:17:54 skrll Exp $"); 45 1.2 skrll 46 1.2 skrll #include <sys/param.h> 47 1.2 skrll #include <sys/kernel.h> 48 1.2 skrll #include <sys/mutex.h> 49 1.2 skrll #include <sys/pool.h> 50 1.2 skrll #include <sys/bus.h> 51 1.2 skrll #include <sys/callout.h> 52 1.2 skrll 53 1.2 skrll #include <dev/usb/usb.h> 54 1.2 skrll #include <dev/usb/usbdi.h> 55 1.2 skrll #include <dev/usb/usbdivar.h> 56 1.2 skrll #include <dev/usb/usb_mem.h> 57 1.2 skrll 58 1.1 skrll #include <linux/kernel.h> 59 1.2 skrll #include <linux/list.h> 60 1.9 skrll #include <linux/err.h> 61 1.1 skrll 62 1.2 skrll #include <dwc2/dwc2.h> 63 1.2 skrll #include <dwc2/dwc2var.h> 64 1.1 skrll 65 1.2 skrll #include "dwc2_core.h" 66 1.2 skrll #include "dwc2_hcd.h" 67 1.1 skrll 68 1.2 skrll #ifdef DWC2_DEBUG 69 1.1 skrll static const char *dwc2_op_state_str(struct dwc2_hsotg *hsotg) 70 1.1 skrll { 71 1.1 skrll switch (hsotg->op_state) { 72 1.1 skrll case OTG_STATE_A_HOST: 73 1.1 skrll return "a_host"; 74 1.1 skrll case OTG_STATE_A_SUSPEND: 75 1.1 skrll return "a_suspend"; 76 1.1 skrll case OTG_STATE_A_PERIPHERAL: 77 1.1 skrll return "a_peripheral"; 78 1.1 skrll case OTG_STATE_B_PERIPHERAL: 79 1.1 skrll return "b_peripheral"; 80 1.1 skrll case OTG_STATE_B_HOST: 81 1.1 skrll return "b_host"; 82 1.1 skrll default: 83 1.1 skrll return "unknown"; 84 1.1 skrll } 85 1.7 skrll } 86 1.8 skrll #endif 87 1.7 skrll 88 1.7 skrll /** 89 1.7 skrll * dwc2_handle_usb_port_intr - handles OTG PRTINT interrupts. 90 1.7 skrll * When the PRTINT interrupt fires, there are certain status bits in the Host 91 1.7 skrll * Port that needs to get cleared. 92 1.7 skrll * 93 1.7 skrll * @hsotg: Programming view of DWC_otg controller 94 1.7 skrll */ 95 1.7 skrll static void dwc2_handle_usb_port_intr(struct dwc2_hsotg *hsotg) 96 1.7 skrll { 97 1.7 skrll u32 hprt0 = DWC2_READ_4(hsotg, HPRT0); 98 1.7 skrll 99 1.7 skrll if (hprt0 & HPRT0_ENACHG) { 100 1.7 skrll hprt0 &= ~HPRT0_ENA; 101 1.7 skrll DWC2_WRITE_4(hsotg, HPRT0, hprt0); 102 1.7 skrll } 103 1.1 skrll } 104 1.1 skrll 105 1.1 skrll /** 106 1.1 skrll * dwc2_handle_mode_mismatch_intr() - Logs a mode mismatch warning message 107 1.1 skrll * 108 1.1 skrll * @hsotg: Programming view of DWC_otg controller 109 1.1 skrll */ 110 1.1 skrll static void dwc2_handle_mode_mismatch_intr(struct dwc2_hsotg *hsotg) 111 1.1 skrll { 112 1.10 skrll /* Clear interrupt */ 113 1.10 skrll DWC2_WRITE_4(hsotg, GINTSTS, GINTSTS_MODEMIS); 114 1.10 skrll 115 1.1 skrll dev_warn(hsotg->dev, "Mode Mismatch Interrupt: currently in %s mode\n", 116 1.1 skrll dwc2_is_host_mode(hsotg) ? "Host" : "Device"); 117 1.1 skrll } 118 1.1 skrll 119 1.1 skrll /** 120 1.1 skrll * dwc2_handle_otg_intr() - Handles the OTG Interrupts. It reads the OTG 121 1.1 skrll * Interrupt Register (GOTGINT) to determine what interrupt has occurred. 122 1.1 skrll * 123 1.1 skrll * @hsotg: Programming view of DWC_otg controller 124 1.1 skrll */ 125 1.1 skrll static void dwc2_handle_otg_intr(struct dwc2_hsotg *hsotg) 126 1.1 skrll { 127 1.1 skrll u32 gotgint; 128 1.1 skrll u32 gotgctl; 129 1.1 skrll u32 gintmsk; 130 1.1 skrll 131 1.2 skrll gotgint = DWC2_READ_4(hsotg, GOTGINT); 132 1.2 skrll gotgctl = DWC2_READ_4(hsotg, GOTGCTL); 133 1.1 skrll dev_dbg(hsotg->dev, "++OTG Interrupt gotgint=%0x [%s]\n", gotgint, 134 1.1 skrll dwc2_op_state_str(hsotg)); 135 1.1 skrll 136 1.1 skrll if (gotgint & GOTGINT_SES_END_DET) { 137 1.1 skrll dev_dbg(hsotg->dev, 138 1.1 skrll " ++OTG Interrupt: Session End Detected++ (%s)\n", 139 1.1 skrll dwc2_op_state_str(hsotg)); 140 1.2 skrll gotgctl = DWC2_READ_4(hsotg, GOTGCTL); 141 1.1 skrll 142 1.9 skrll if (dwc2_is_device_mode(hsotg)) 143 1.10 skrll dwc2_hsotg_disconnect(hsotg); 144 1.9 skrll 145 1.1 skrll if (hsotg->op_state == OTG_STATE_B_HOST) { 146 1.1 skrll hsotg->op_state = OTG_STATE_B_PERIPHERAL; 147 1.1 skrll } else { 148 1.1 skrll /* 149 1.1 skrll * If not B_HOST and Device HNP still set, HNP did 150 1.1 skrll * not succeed! 151 1.1 skrll */ 152 1.1 skrll if (gotgctl & GOTGCTL_DEVHNPEN) { 153 1.1 skrll dev_dbg(hsotg->dev, "Session End Detected\n"); 154 1.1 skrll dev_err(hsotg->dev, 155 1.1 skrll "Device Not Connected/Responding!\n"); 156 1.1 skrll } 157 1.1 skrll 158 1.1 skrll /* 159 1.1 skrll * If Session End Detected the B-Cable has been 160 1.1 skrll * disconnected 161 1.1 skrll */ 162 1.1 skrll /* Reset to a clean state */ 163 1.1 skrll hsotg->lx_state = DWC2_L0; 164 1.1 skrll } 165 1.1 skrll 166 1.2 skrll gotgctl = DWC2_READ_4(hsotg, GOTGCTL); 167 1.1 skrll gotgctl &= ~GOTGCTL_DEVHNPEN; 168 1.2 skrll DWC2_WRITE_4(hsotg, GOTGCTL, gotgctl); 169 1.1 skrll } 170 1.1 skrll 171 1.1 skrll if (gotgint & GOTGINT_SES_REQ_SUC_STS_CHNG) { 172 1.1 skrll dev_dbg(hsotg->dev, 173 1.1 skrll " ++OTG Interrupt: Session Request Success Status Change++\n"); 174 1.2 skrll gotgctl = DWC2_READ_4(hsotg, GOTGCTL); 175 1.1 skrll if (gotgctl & GOTGCTL_SESREQSCS) { 176 1.1 skrll if (hsotg->core_params->phy_type == 177 1.1 skrll DWC2_PHY_TYPE_PARAM_FS 178 1.1 skrll && hsotg->core_params->i2c_enable > 0) { 179 1.1 skrll hsotg->srp_success = 1; 180 1.1 skrll } else { 181 1.1 skrll /* Clear Session Request */ 182 1.2 skrll gotgctl = DWC2_READ_4(hsotg, GOTGCTL); 183 1.1 skrll gotgctl &= ~GOTGCTL_SESREQ; 184 1.2 skrll DWC2_WRITE_4(hsotg, GOTGCTL, gotgctl); 185 1.1 skrll } 186 1.1 skrll } 187 1.1 skrll } 188 1.1 skrll 189 1.1 skrll if (gotgint & GOTGINT_HST_NEG_SUC_STS_CHNG) { 190 1.1 skrll /* 191 1.1 skrll * Print statements during the HNP interrupt handling 192 1.1 skrll * can cause it to fail 193 1.1 skrll */ 194 1.2 skrll gotgctl = DWC2_READ_4(hsotg, GOTGCTL); 195 1.1 skrll /* 196 1.1 skrll * WA for 3.00a- HW is not setting cur_mode, even sometimes 197 1.1 skrll * this does not help 198 1.1 skrll */ 199 1.3 skrll if (hsotg->hw_params.snpsid >= DWC2_CORE_REV_3_00a) 200 1.1 skrll udelay(100); 201 1.1 skrll if (gotgctl & GOTGCTL_HSTNEGSCS) { 202 1.1 skrll if (dwc2_is_host_mode(hsotg)) { 203 1.1 skrll hsotg->op_state = OTG_STATE_B_HOST; 204 1.1 skrll /* 205 1.1 skrll * Need to disable SOF interrupt immediately. 206 1.1 skrll * When switching from device to host, the PCD 207 1.1 skrll * interrupt handler won't handle the interrupt 208 1.1 skrll * if host mode is already set. The HCD 209 1.1 skrll * interrupt handler won't get called if the 210 1.1 skrll * HCD state is HALT. This means that the 211 1.1 skrll * interrupt does not get handled and Linux 212 1.1 skrll * complains loudly. 213 1.1 skrll */ 214 1.2 skrll gintmsk = DWC2_READ_4(hsotg, GINTMSK); 215 1.1 skrll gintmsk &= ~GINTSTS_SOF; 216 1.2 skrll DWC2_WRITE_4(hsotg, GINTMSK, gintmsk); 217 1.1 skrll 218 1.1 skrll /* 219 1.1 skrll * Call callback function with spin lock 220 1.1 skrll * released 221 1.1 skrll */ 222 1.1 skrll spin_unlock(&hsotg->lock); 223 1.1 skrll 224 1.1 skrll /* Initialize the Core for Host mode */ 225 1.1 skrll dwc2_hcd_start(hsotg); 226 1.1 skrll spin_lock(&hsotg->lock); 227 1.1 skrll hsotg->op_state = OTG_STATE_B_HOST; 228 1.1 skrll } 229 1.1 skrll } else { 230 1.2 skrll gotgctl = DWC2_READ_4(hsotg, GOTGCTL); 231 1.1 skrll gotgctl &= ~(GOTGCTL_HNPREQ | GOTGCTL_DEVHNPEN); 232 1.2 skrll DWC2_WRITE_4(hsotg, GOTGCTL, gotgctl); 233 1.1 skrll dev_dbg(hsotg->dev, "HNP Failed\n"); 234 1.1 skrll dev_err(hsotg->dev, 235 1.1 skrll "Device Not Connected/Responding\n"); 236 1.1 skrll } 237 1.1 skrll } 238 1.1 skrll 239 1.1 skrll if (gotgint & GOTGINT_HST_NEG_DET) { 240 1.1 skrll /* 241 1.1 skrll * The disconnect interrupt is set at the same time as 242 1.1 skrll * Host Negotiation Detected. During the mode switch all 243 1.1 skrll * interrupts are cleared so the disconnect interrupt 244 1.1 skrll * handler will not get executed. 245 1.1 skrll */ 246 1.1 skrll dev_dbg(hsotg->dev, 247 1.1 skrll " ++OTG Interrupt: Host Negotiation Detected++ (%s)\n", 248 1.1 skrll (dwc2_is_host_mode(hsotg) ? "Host" : "Device")); 249 1.1 skrll if (dwc2_is_device_mode(hsotg)) { 250 1.1 skrll dev_dbg(hsotg->dev, "a_suspend->a_peripheral (%d)\n", 251 1.1 skrll hsotg->op_state); 252 1.1 skrll spin_unlock(&hsotg->lock); 253 1.10 skrll dwc2_hcd_disconnect(hsotg, false); 254 1.1 skrll spin_lock(&hsotg->lock); 255 1.1 skrll hsotg->op_state = OTG_STATE_A_PERIPHERAL; 256 1.1 skrll } else { 257 1.1 skrll /* Need to disable SOF interrupt immediately */ 258 1.2 skrll gintmsk = DWC2_READ_4(hsotg, GINTMSK); 259 1.1 skrll gintmsk &= ~GINTSTS_SOF; 260 1.2 skrll DWC2_WRITE_4(hsotg, GINTMSK, gintmsk); 261 1.1 skrll spin_unlock(&hsotg->lock); 262 1.1 skrll dwc2_hcd_start(hsotg); 263 1.1 skrll spin_lock(&hsotg->lock); 264 1.1 skrll hsotg->op_state = OTG_STATE_A_HOST; 265 1.1 skrll } 266 1.1 skrll } 267 1.1 skrll 268 1.1 skrll if (gotgint & GOTGINT_A_DEV_TOUT_CHG) 269 1.1 skrll dev_dbg(hsotg->dev, 270 1.1 skrll " ++OTG Interrupt: A-Device Timeout Change++\n"); 271 1.1 skrll if (gotgint & GOTGINT_DBNCE_DONE) 272 1.1 skrll dev_dbg(hsotg->dev, " ++OTG Interrupt: Debounce Done++\n"); 273 1.1 skrll 274 1.1 skrll /* Clear GOTGINT */ 275 1.2 skrll DWC2_WRITE_4(hsotg, GOTGINT, gotgint); 276 1.1 skrll } 277 1.1 skrll 278 1.1 skrll /** 279 1.1 skrll * dwc2_handle_conn_id_status_change_intr() - Handles the Connector ID Status 280 1.1 skrll * Change Interrupt 281 1.1 skrll * 282 1.1 skrll * @hsotg: Programming view of DWC_otg controller 283 1.1 skrll * 284 1.1 skrll * Reads the OTG Interrupt Register (GOTCTL) to determine whether this is a 285 1.1 skrll * Device to Host Mode transition or a Host to Device Mode transition. This only 286 1.1 skrll * occurs when the cable is connected/removed from the PHY connector. 287 1.1 skrll */ 288 1.1 skrll static void dwc2_handle_conn_id_status_change_intr(struct dwc2_hsotg *hsotg) 289 1.1 skrll { 290 1.10 skrll u32 gintmsk; 291 1.10 skrll 292 1.10 skrll /* Clear interrupt */ 293 1.10 skrll DWC2_WRITE_4(hsotg, GINTSTS, GINTSTS_CONIDSTSCHNG); 294 1.1 skrll 295 1.1 skrll /* Need to disable SOF interrupt immediately */ 296 1.10 skrll gintmsk = DWC2_READ_4(hsotg, GINTMSK); 297 1.1 skrll gintmsk &= ~GINTSTS_SOF; 298 1.2 skrll DWC2_WRITE_4(hsotg, GINTMSK, gintmsk); 299 1.1 skrll 300 1.1 skrll dev_dbg(hsotg->dev, " ++Connector ID Status Change Interrupt++ (%s)\n", 301 1.1 skrll dwc2_is_host_mode(hsotg) ? "Host" : "Device"); 302 1.1 skrll 303 1.1 skrll /* 304 1.1 skrll * Need to schedule a work, as there are possible DELAY function calls. 305 1.1 skrll * Release lock before scheduling workq as it holds spinlock during 306 1.1 skrll * scheduling. 307 1.1 skrll */ 308 1.9 skrll if (hsotg->wq_otg) { 309 1.9 skrll spin_unlock(&hsotg->lock); 310 1.11 skrll queue_work(hsotg->wq_otg, &hsotg->wf_otg); 311 1.9 skrll spin_lock(&hsotg->lock); 312 1.9 skrll } 313 1.1 skrll } 314 1.1 skrll 315 1.1 skrll /** 316 1.1 skrll * dwc2_handle_session_req_intr() - This interrupt indicates that a device is 317 1.1 skrll * initiating the Session Request Protocol to request the host to turn on bus 318 1.1 skrll * power so a new session can begin 319 1.1 skrll * 320 1.1 skrll * @hsotg: Programming view of DWC_otg controller 321 1.1 skrll * 322 1.1 skrll * This handler responds by turning on bus power. If the DWC_otg controller is 323 1.1 skrll * in low power mode, this handler brings the controller out of low power mode 324 1.1 skrll * before turning on bus power. 325 1.1 skrll */ 326 1.1 skrll static void dwc2_handle_session_req_intr(struct dwc2_hsotg *hsotg) 327 1.1 skrll { 328 1.10 skrll int ret; 329 1.1 skrll 330 1.1 skrll /* Clear interrupt */ 331 1.2 skrll DWC2_WRITE_4(hsotg, GINTSTS, GINTSTS_SESSREQINT); 332 1.9 skrll 333 1.10 skrll dev_dbg(hsotg->dev, "Session request interrupt - lx_state=%d\n", 334 1.10 skrll hsotg->lx_state); 335 1.10 skrll 336 1.10 skrll if (dwc2_is_device_mode(hsotg)) { 337 1.10 skrll if (hsotg->lx_state == DWC2_L2) { 338 1.10 skrll ret = dwc2_exit_hibernation(hsotg, true); 339 1.10 skrll if (ret && (ret != -ENOTSUPP)) 340 1.10 skrll dev_err(hsotg->dev, 341 1.10 skrll "exit hibernation failed\n"); 342 1.10 skrll } 343 1.10 skrll 344 1.10 skrll /* 345 1.10 skrll * Report disconnect if there is any previous session 346 1.10 skrll * established 347 1.10 skrll */ 348 1.10 skrll dwc2_hsotg_disconnect(hsotg); 349 1.10 skrll } 350 1.1 skrll } 351 1.1 skrll 352 1.1 skrll /* 353 1.1 skrll * This interrupt indicates that the DWC_otg controller has detected a 354 1.1 skrll * resume or remote wakeup sequence. If the DWC_otg controller is in 355 1.1 skrll * low power mode, the handler must brings the controller out of low 356 1.1 skrll * power mode. The controller automatically begins resume signaling. 357 1.1 skrll * The handler schedules a time to stop resume signaling. 358 1.1 skrll */ 359 1.1 skrll static void dwc2_handle_wakeup_detected_intr(struct dwc2_hsotg *hsotg) 360 1.1 skrll { 361 1.9 skrll int ret; 362 1.10 skrll 363 1.10 skrll /* Clear interrupt */ 364 1.10 skrll DWC2_WRITE_4(hsotg, GINTSTS, GINTSTS_WKUPINT); 365 1.10 skrll 366 1.1 skrll dev_dbg(hsotg->dev, "++Resume or Remote Wakeup Detected Interrupt++\n"); 367 1.1 skrll dev_dbg(hsotg->dev, "%s lxstate = %d\n", __func__, hsotg->lx_state); 368 1.1 skrll 369 1.1 skrll if (dwc2_is_device_mode(hsotg)) { 370 1.2 skrll dev_dbg(hsotg->dev, "DSTS=0x%0x\n", DWC2_READ_4(hsotg, DSTS)); 371 1.1 skrll if (hsotg->lx_state == DWC2_L2) { 372 1.2 skrll u32 dctl = DWC2_READ_4(hsotg, DCTL); 373 1.1 skrll 374 1.1 skrll /* Clear Remote Wakeup Signaling */ 375 1.1 skrll dctl &= ~DCTL_RMTWKUPSIG; 376 1.2 skrll DWC2_WRITE_4(hsotg, DCTL, dctl); 377 1.9 skrll ret = dwc2_exit_hibernation(hsotg, true); 378 1.9 skrll if (ret && (ret != -ENOTSUPP)) 379 1.9 skrll dev_err(hsotg->dev, "exit hibernation failed\n"); 380 1.9 skrll 381 1.9 skrll call_gadget(hsotg, resume); 382 1.1 skrll } 383 1.1 skrll /* Change to L0 state */ 384 1.1 skrll hsotg->lx_state = DWC2_L0; 385 1.1 skrll } else { 386 1.10 skrll if (hsotg->core_params->hibernation) 387 1.10 skrll return; 388 1.10 skrll 389 1.1 skrll if (hsotg->lx_state != DWC2_L1) { 390 1.2 skrll u32 pcgcctl = DWC2_READ_4(hsotg, PCGCTL); 391 1.1 skrll 392 1.1 skrll /* Restart the Phy Clock */ 393 1.1 skrll pcgcctl &= ~PCGCTL_STOPPCLK; 394 1.2 skrll DWC2_WRITE_4(hsotg, PCGCTL, pcgcctl); 395 1.2 skrll callout_reset(&hsotg->wkp_timer, mstohz(71), 396 1.2 skrll dwc2_wakeup_detected, hsotg); 397 1.1 skrll } else { 398 1.1 skrll /* Change to L0 state */ 399 1.1 skrll hsotg->lx_state = DWC2_L0; 400 1.1 skrll } 401 1.1 skrll } 402 1.1 skrll } 403 1.1 skrll 404 1.1 skrll /* 405 1.1 skrll * This interrupt indicates that a device has been disconnected from the 406 1.1 skrll * root port 407 1.1 skrll */ 408 1.1 skrll static void dwc2_handle_disconnect_intr(struct dwc2_hsotg *hsotg) 409 1.1 skrll { 410 1.10 skrll DWC2_WRITE_4(hsotg, GINTSTS, GINTSTS_DISCONNINT); 411 1.10 skrll 412 1.1 skrll dev_dbg(hsotg->dev, "++Disconnect Detected Interrupt++ (%s) %s\n", 413 1.1 skrll dwc2_is_host_mode(hsotg) ? "Host" : "Device", 414 1.1 skrll dwc2_op_state_str(hsotg)); 415 1.1 skrll 416 1.9 skrll if (hsotg->op_state == OTG_STATE_A_HOST) 417 1.10 skrll dwc2_hcd_disconnect(hsotg, false); 418 1.1 skrll } 419 1.1 skrll 420 1.1 skrll /* 421 1.1 skrll * This interrupt indicates that SUSPEND state has been detected on the USB. 422 1.1 skrll * 423 1.1 skrll * For HNP the USB Suspend interrupt signals the change from "a_peripheral" 424 1.1 skrll * to "a_host". 425 1.1 skrll * 426 1.1 skrll * When power management is enabled the core will be put in low power mode. 427 1.1 skrll */ 428 1.1 skrll static void dwc2_handle_usb_suspend_intr(struct dwc2_hsotg *hsotg) 429 1.1 skrll { 430 1.9 skrll u32 dsts; 431 1.9 skrll int ret; 432 1.9 skrll 433 1.10 skrll /* Clear interrupt */ 434 1.10 skrll DWC2_WRITE_4(hsotg, GINTSTS, GINTSTS_USBSUSP); 435 1.10 skrll 436 1.1 skrll dev_dbg(hsotg->dev, "USB SUSPEND\n"); 437 1.1 skrll 438 1.1 skrll if (dwc2_is_device_mode(hsotg)) { 439 1.1 skrll /* 440 1.1 skrll * Check the Device status register to determine if the Suspend 441 1.1 skrll * state is active 442 1.1 skrll */ 443 1.2 skrll dsts = DWC2_READ_4(hsotg, DSTS); 444 1.1 skrll dev_dbg(hsotg->dev, "DSTS=0x%0x\n", dsts); 445 1.1 skrll dev_dbg(hsotg->dev, 446 1.1 skrll "DSTS.Suspend Status=%d HWCFG4.Power Optimize=%d\n", 447 1.1 skrll !!(dsts & DSTS_SUSPSTS), 448 1.3 skrll hsotg->hw_params.power_optimized); 449 1.9 skrll if ((dsts & DSTS_SUSPSTS) && hsotg->hw_params.power_optimized) { 450 1.9 skrll /* Ignore suspend request before enumeration */ 451 1.9 skrll if (!dwc2_is_device_connected(hsotg)) { 452 1.9 skrll dev_dbg(hsotg->dev, 453 1.9 skrll "ignore suspend request before enumeration\n"); 454 1.10 skrll return; 455 1.9 skrll } 456 1.9 skrll 457 1.9 skrll ret = dwc2_enter_hibernation(hsotg); 458 1.9 skrll if (ret) { 459 1.9 skrll if (ret != -ENOTSUPP) 460 1.9 skrll dev_err(hsotg->dev, 461 1.9 skrll "enter hibernation failed\n"); 462 1.9 skrll goto skip_power_saving; 463 1.9 skrll } 464 1.9 skrll 465 1.9 skrll udelay(100); 466 1.9 skrll 467 1.9 skrll /* Ask phy to be suspended */ 468 1.9 skrll if (!IS_ERR_OR_NULL(hsotg->uphy)) 469 1.9 skrll usb_phy_set_suspend(hsotg->uphy, true); 470 1.9 skrll skip_power_saving: 471 1.9 skrll /* 472 1.9 skrll * Change to L2 (suspend) state before releasing 473 1.9 skrll * spinlock 474 1.9 skrll */ 475 1.9 skrll hsotg->lx_state = DWC2_L2; 476 1.9 skrll 477 1.9 skrll /* Call gadget suspend callback */ 478 1.9 skrll call_gadget(hsotg, suspend); 479 1.9 skrll } 480 1.1 skrll } else { 481 1.1 skrll if (hsotg->op_state == OTG_STATE_A_PERIPHERAL) { 482 1.1 skrll dev_dbg(hsotg->dev, "a_peripheral->a_host\n"); 483 1.1 skrll 484 1.9 skrll /* Change to L2 (suspend) state */ 485 1.9 skrll hsotg->lx_state = DWC2_L2; 486 1.1 skrll /* Clear the a_peripheral flag, back to a_host */ 487 1.1 skrll spin_unlock(&hsotg->lock); 488 1.1 skrll dwc2_hcd_start(hsotg); 489 1.1 skrll spin_lock(&hsotg->lock); 490 1.1 skrll hsotg->op_state = OTG_STATE_A_HOST; 491 1.1 skrll } 492 1.1 skrll } 493 1.1 skrll } 494 1.1 skrll 495 1.1 skrll #define GINTMSK_COMMON (GINTSTS_WKUPINT | GINTSTS_SESSREQINT | \ 496 1.1 skrll GINTSTS_CONIDSTSCHNG | GINTSTS_OTGINT | \ 497 1.1 skrll GINTSTS_MODEMIS | GINTSTS_DISCONNINT | \ 498 1.1 skrll GINTSTS_USBSUSP | GINTSTS_PRTINT) 499 1.1 skrll 500 1.1 skrll /* 501 1.1 skrll * This function returns the Core Interrupt register 502 1.1 skrll */ 503 1.1 skrll static u32 dwc2_read_common_intr(struct dwc2_hsotg *hsotg) 504 1.1 skrll { 505 1.1 skrll u32 gintsts; 506 1.1 skrll u32 gintmsk; 507 1.1 skrll u32 gahbcfg; 508 1.1 skrll u32 gintmsk_common = GINTMSK_COMMON; 509 1.1 skrll 510 1.2 skrll gintsts = DWC2_READ_4(hsotg, GINTSTS); 511 1.2 skrll gintmsk = DWC2_READ_4(hsotg, GINTMSK); 512 1.2 skrll gahbcfg = DWC2_READ_4(hsotg, GAHBCFG); 513 1.1 skrll 514 1.1 skrll /* If any common interrupts set */ 515 1.1 skrll if (gintsts & gintmsk_common) 516 1.1 skrll dev_dbg(hsotg->dev, "gintsts=%08x gintmsk=%08x\n", 517 1.1 skrll gintsts, gintmsk); 518 1.1 skrll 519 1.1 skrll if (gahbcfg & GAHBCFG_GLBL_INTR_EN) 520 1.1 skrll return gintsts & gintmsk & gintmsk_common; 521 1.1 skrll else 522 1.1 skrll return 0; 523 1.1 skrll } 524 1.1 skrll 525 1.1 skrll /* 526 1.1 skrll * Common interrupt handler 527 1.1 skrll * 528 1.1 skrll * The common interrupts are those that occur in both Host and Device mode. 529 1.1 skrll * This handler handles the following interrupts: 530 1.1 skrll * - Mode Mismatch Interrupt 531 1.1 skrll * - OTG Interrupt 532 1.1 skrll * - Connector ID Status Change Interrupt 533 1.1 skrll * - Disconnect Interrupt 534 1.1 skrll * - Session Request Interrupt 535 1.1 skrll * - Resume / Remote Wakeup Detected Interrupt 536 1.1 skrll * - Suspend Interrupt 537 1.1 skrll */ 538 1.2 skrll irqreturn_t dwc2_handle_common_intr(void *dev) 539 1.1 skrll { 540 1.1 skrll struct dwc2_hsotg *hsotg = dev; 541 1.1 skrll u32 gintsts; 542 1.1 skrll irqreturn_t retval = IRQ_NONE; 543 1.1 skrll 544 1.7 skrll if (!dwc2_is_controller_alive(hsotg)) { 545 1.7 skrll dev_warn(hsotg->dev, "Controller is dead\n"); 546 1.1 skrll goto out; 547 1.1 skrll } 548 1.1 skrll 549 1.4 skrll KASSERT(mutex_owned(&hsotg->lock)); 550 1.1 skrll 551 1.1 skrll gintsts = dwc2_read_common_intr(hsotg); 552 1.1 skrll if (gintsts & ~GINTSTS_PRTINT) 553 1.1 skrll retval = IRQ_HANDLED; 554 1.1 skrll 555 1.1 skrll if (gintsts & GINTSTS_MODEMIS) 556 1.1 skrll dwc2_handle_mode_mismatch_intr(hsotg); 557 1.1 skrll if (gintsts & GINTSTS_OTGINT) 558 1.1 skrll dwc2_handle_otg_intr(hsotg); 559 1.1 skrll if (gintsts & GINTSTS_CONIDSTSCHNG) 560 1.1 skrll dwc2_handle_conn_id_status_change_intr(hsotg); 561 1.1 skrll if (gintsts & GINTSTS_DISCONNINT) 562 1.1 skrll dwc2_handle_disconnect_intr(hsotg); 563 1.1 skrll if (gintsts & GINTSTS_SESSREQINT) 564 1.1 skrll dwc2_handle_session_req_intr(hsotg); 565 1.1 skrll if (gintsts & GINTSTS_WKUPINT) 566 1.1 skrll dwc2_handle_wakeup_detected_intr(hsotg); 567 1.1 skrll if (gintsts & GINTSTS_USBSUSP) 568 1.1 skrll dwc2_handle_usb_suspend_intr(hsotg); 569 1.1 skrll 570 1.1 skrll if (gintsts & GINTSTS_PRTINT) { 571 1.1 skrll /* 572 1.1 skrll * The port interrupt occurs while in device mode with HPRT0 573 1.1 skrll * Port Enable/Disable 574 1.1 skrll */ 575 1.1 skrll if (dwc2_is_device_mode(hsotg)) { 576 1.1 skrll dev_dbg(hsotg->dev, 577 1.1 skrll " --Port interrupt received in Device mode--\n"); 578 1.7 skrll dwc2_handle_usb_port_intr(hsotg); 579 1.3 skrll retval = IRQ_HANDLED; 580 1.1 skrll } 581 1.1 skrll } 582 1.1 skrll 583 1.1 skrll out: 584 1.1 skrll return retval; 585 1.1 skrll } 586