1 /* $NetBSD: dwc2_hcdintr.c,v 1.13 2016/02/14 10:53:30 skrll Exp $ */ 2 3 /* 4 * hcd_intr.c - DesignWare HS OTG Controller host-mode interrupt handling 5 * 6 * Copyright (C) 2004-2013 Synopsys, Inc. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions, and the following disclaimer, 13 * without modification. 14 * 2. Redistributions in binary form must reproduce the above copyright 15 * notice, this list of conditions and the following disclaimer in the 16 * documentation and/or other materials provided with the distribution. 17 * 3. The names of the above-listed copyright holders may not be used 18 * to endorse or promote products derived from this software without 19 * specific prior written permission. 20 * 21 * ALTERNATIVELY, this software may be distributed under the terms of the 22 * GNU General Public License ("GPL") as published by the Free Software 23 * Foundation; either version 2 of the License, or (at your option) any 24 * later version. 25 * 26 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS 27 * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, 28 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 29 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR 30 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 31 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 32 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 33 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 34 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 35 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 36 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 37 */ 38 39 /* 40 * This file contains the interrupt handlers for Host mode 41 */ 42 #include <sys/cdefs.h> 43 __KERNEL_RCSID(0, "$NetBSD: dwc2_hcdintr.c,v 1.13 2016/02/14 10:53:30 skrll Exp $"); 44 45 #include <sys/types.h> 46 #include <sys/pool.h> 47 48 #include <dev/usb/usb.h> 49 #include <dev/usb/usbdi.h> 50 #include <dev/usb/usbdivar.h> 51 #include <dev/usb/usb_mem.h> 52 53 #include <machine/param.h> 54 55 #include <linux/kernel.h> 56 57 #include <dwc2/dwc2.h> 58 #include <dwc2/dwc2var.h> 59 60 #include "dwc2_core.h" 61 #include "dwc2_hcd.h" 62 63 /* This function is for debug only */ 64 static void dwc2_track_missed_sofs(struct dwc2_hsotg *hsotg) 65 { 66 #ifdef CONFIG_USB_DWC2_TRACK_MISSED_SOFS 67 u16 curr_frame_number = hsotg->frame_number; 68 69 if (hsotg->frame_num_idx < FRAME_NUM_ARRAY_SIZE) { 70 if (((hsotg->last_frame_num + 1) & HFNUM_MAX_FRNUM) != 71 curr_frame_number) { 72 hsotg->frame_num_array[hsotg->frame_num_idx] = 73 curr_frame_number; 74 hsotg->last_frame_num_array[hsotg->frame_num_idx] = 75 hsotg->last_frame_num; 76 hsotg->frame_num_idx++; 77 } 78 } else if (!hsotg->dumped_frame_num_array) { 79 int i; 80 81 dev_info(hsotg->dev, "Frame Last Frame\n"); 82 dev_info(hsotg->dev, "----- ----------\n"); 83 for (i = 0; i < FRAME_NUM_ARRAY_SIZE; i++) { 84 dev_info(hsotg->dev, "0x%04x 0x%04x\n", 85 hsotg->frame_num_array[i], 86 hsotg->last_frame_num_array[i]); 87 } 88 hsotg->dumped_frame_num_array = 1; 89 } 90 hsotg->last_frame_num = curr_frame_number; 91 #endif 92 } 93 94 static void dwc2_hc_handle_tt_clear(struct dwc2_hsotg *hsotg, 95 struct dwc2_host_chan *chan, 96 struct dwc2_qtd *qtd) 97 { 98 // struct urb *usb_urb; 99 100 if (!chan->qh) 101 return; 102 103 if (chan->qh->dev_speed == USB_SPEED_HIGH) 104 return; 105 106 if (!qtd->urb) 107 return; 108 109 110 if (qtd->urb->status != -EPIPE && qtd->urb->status != -EREMOTEIO) { 111 chan->qh->tt_buffer_dirty = 1; 112 chan->qh->tt_buffer_dirty = 0; 113 } 114 } 115 116 /* 117 * Handles the start-of-frame interrupt in host mode. Non-periodic 118 * transactions may be queued to the DWC_otg controller for the current 119 * (micro)frame. Periodic transactions may be queued to the controller 120 * for the next (micro)frame. 121 */ 122 static void dwc2_sof_intr(struct dwc2_hsotg *hsotg) 123 { 124 struct list_head *qh_entry; 125 struct dwc2_qh *qh; 126 enum dwc2_transaction_type tr_type; 127 128 /* Clear interrupt */ 129 DWC2_WRITE_4(hsotg, GINTSTS, GINTSTS_SOF); 130 131 #ifdef DEBUG_SOF 132 dev_vdbg(hsotg->dev, "--Start of Frame Interrupt--\n"); 133 #endif 134 135 hsotg->frame_number = dwc2_hcd_get_frame_number(hsotg); 136 137 dwc2_track_missed_sofs(hsotg); 138 139 /* Determine whether any periodic QHs should be executed */ 140 qh_entry = hsotg->periodic_sched_inactive.next; 141 while (qh_entry != &hsotg->periodic_sched_inactive) { 142 qh = list_entry(qh_entry, struct dwc2_qh, qh_list_entry); 143 qh_entry = qh_entry->next; 144 if (dwc2_frame_num_le(qh->sched_frame, hsotg->frame_number)) 145 /* 146 * Move QH to the ready list to be executed next 147 * (micro)frame 148 */ 149 list_move(&qh->qh_list_entry, 150 &hsotg->periodic_sched_ready); 151 } 152 tr_type = dwc2_hcd_select_transactions(hsotg); 153 if (tr_type != DWC2_TRANSACTION_NONE) 154 dwc2_hcd_queue_transactions(hsotg, tr_type); 155 } 156 157 /* 158 * Handles the Rx FIFO Level Interrupt, which indicates that there is 159 * at least one packet in the Rx FIFO. The packets are moved from the FIFO to 160 * memory if the DWC_otg controller is operating in Slave mode. 161 */ 162 static void dwc2_rx_fifo_level_intr(struct dwc2_hsotg *hsotg) 163 { 164 u32 grxsts, chnum, bcnt, pktsts; 165 struct dwc2_host_chan *chan; 166 167 if (dbg_perio()) 168 dev_vdbg(hsotg->dev, "--RxFIFO Level Interrupt--\n"); 169 170 grxsts = DWC2_READ_4(hsotg, GRXSTSP); 171 chnum = (grxsts & GRXSTS_HCHNUM_MASK) >> GRXSTS_HCHNUM_SHIFT; 172 chan = hsotg->hc_ptr_array[chnum]; 173 if (!chan) { 174 dev_err(hsotg->dev, "Unable to get corresponding channel\n"); 175 return; 176 } 177 178 bcnt = (grxsts & GRXSTS_BYTECNT_MASK) >> GRXSTS_BYTECNT_SHIFT; 179 pktsts = (grxsts & GRXSTS_PKTSTS_MASK) >> GRXSTS_PKTSTS_SHIFT; 180 181 /* Packet Status */ 182 if (dbg_perio()) { 183 dev_vdbg(hsotg->dev, " Ch num = %d\n", chnum); 184 dev_vdbg(hsotg->dev, " Count = %d\n", bcnt); 185 dev_vdbg(hsotg->dev, " DPID = %d, chan.dpid = %d\n", 186 (grxsts & GRXSTS_DPID_MASK) >> GRXSTS_DPID_SHIFT, 187 chan->data_pid_start); 188 dev_vdbg(hsotg->dev, " PStatus = %d\n", pktsts); 189 } 190 191 switch (pktsts) { 192 case GRXSTS_PKTSTS_HCHIN: 193 /* Read the data into the host buffer */ 194 if (bcnt > 0) { 195 dwc2_read_packet(hsotg, chan->xfer_buf, bcnt); 196 197 /* Update the HC fields for the next packet received */ 198 chan->xfer_count += bcnt; 199 chan->xfer_buf += bcnt; 200 } 201 break; 202 case GRXSTS_PKTSTS_HCHIN_XFER_COMP: 203 case GRXSTS_PKTSTS_DATATOGGLEERR: 204 case GRXSTS_PKTSTS_HCHHALTED: 205 /* Handled in interrupt, just ignore data */ 206 break; 207 default: 208 dev_err(hsotg->dev, 209 "RxFIFO Level Interrupt: Unknown status %d\n", pktsts); 210 break; 211 } 212 } 213 214 /* 215 * This interrupt occurs when the non-periodic Tx FIFO is half-empty. More 216 * data packets may be written to the FIFO for OUT transfers. More requests 217 * may be written to the non-periodic request queue for IN transfers. This 218 * interrupt is enabled only in Slave mode. 219 */ 220 static void dwc2_np_tx_fifo_empty_intr(struct dwc2_hsotg *hsotg) 221 { 222 dev_vdbg(hsotg->dev, "--Non-Periodic TxFIFO Empty Interrupt--\n"); 223 dwc2_hcd_queue_transactions(hsotg, DWC2_TRANSACTION_NON_PERIODIC); 224 } 225 226 /* 227 * This interrupt occurs when the periodic Tx FIFO is half-empty. More data 228 * packets may be written to the FIFO for OUT transfers. More requests may be 229 * written to the periodic request queue for IN transfers. This interrupt is 230 * enabled only in Slave mode. 231 */ 232 static void dwc2_perio_tx_fifo_empty_intr(struct dwc2_hsotg *hsotg) 233 { 234 if (dbg_perio()) 235 dev_vdbg(hsotg->dev, "--Periodic TxFIFO Empty Interrupt--\n"); 236 dwc2_hcd_queue_transactions(hsotg, DWC2_TRANSACTION_PERIODIC); 237 } 238 239 static void dwc2_hprt0_enable(struct dwc2_hsotg *hsotg, u32 hprt0, 240 u32 *hprt0_modify) 241 { 242 struct dwc2_core_params *params = hsotg->core_params; 243 int do_reset = 0; 244 u32 usbcfg; 245 u32 prtspd; 246 u32 hcfg; 247 u32 fslspclksel; 248 u32 hfir; 249 250 dev_vdbg(hsotg->dev, "%s(%p)\n", __func__, hsotg); 251 252 /* Every time when port enables calculate HFIR.FrInterval */ 253 hfir = DWC2_READ_4(hsotg, HFIR); 254 hfir &= ~HFIR_FRINT_MASK; 255 hfir |= dwc2_calc_frame_interval(hsotg) << HFIR_FRINT_SHIFT & 256 HFIR_FRINT_MASK; 257 DWC2_WRITE_4(hsotg, HFIR, hfir); 258 259 /* Check if we need to adjust the PHY clock speed for low power */ 260 if (!params->host_support_fs_ls_low_power) { 261 /* Port has been enabled, set the reset change flag */ 262 hsotg->flags.b.port_reset_change = 1; 263 264 dwc2_root_intr(hsotg->hsotg_sc); 265 return; 266 } 267 268 usbcfg = DWC2_READ_4(hsotg, GUSBCFG); 269 prtspd = (hprt0 & HPRT0_SPD_MASK) >> HPRT0_SPD_SHIFT; 270 271 if (prtspd == HPRT0_SPD_LOW_SPEED || prtspd == HPRT0_SPD_FULL_SPEED) { 272 /* Low power */ 273 if (!(usbcfg & GUSBCFG_PHY_LP_CLK_SEL)) { 274 /* Set PHY low power clock select for FS/LS devices */ 275 usbcfg |= GUSBCFG_PHY_LP_CLK_SEL; 276 DWC2_WRITE_4(hsotg, GUSBCFG, usbcfg); 277 do_reset = 1; 278 } 279 280 hcfg = DWC2_READ_4(hsotg, HCFG); 281 fslspclksel = (hcfg & HCFG_FSLSPCLKSEL_MASK) >> 282 HCFG_FSLSPCLKSEL_SHIFT; 283 284 if (prtspd == HPRT0_SPD_LOW_SPEED && 285 params->host_ls_low_power_phy_clk == 286 DWC2_HOST_LS_LOW_POWER_PHY_CLK_PARAM_6MHZ) { 287 /* 6 MHZ */ 288 dev_vdbg(hsotg->dev, 289 "FS_PHY programming HCFG to 6 MHz\n"); 290 if (fslspclksel != HCFG_FSLSPCLKSEL_6_MHZ) { 291 fslspclksel = HCFG_FSLSPCLKSEL_6_MHZ; 292 hcfg &= ~HCFG_FSLSPCLKSEL_MASK; 293 hcfg |= fslspclksel << HCFG_FSLSPCLKSEL_SHIFT; 294 DWC2_WRITE_4(hsotg, HCFG, hcfg); 295 do_reset = 1; 296 } 297 } else { 298 /* 48 MHZ */ 299 dev_vdbg(hsotg->dev, 300 "FS_PHY programming HCFG to 48 MHz\n"); 301 if (fslspclksel != HCFG_FSLSPCLKSEL_48_MHZ) { 302 fslspclksel = HCFG_FSLSPCLKSEL_48_MHZ; 303 hcfg &= ~HCFG_FSLSPCLKSEL_MASK; 304 hcfg |= fslspclksel << HCFG_FSLSPCLKSEL_SHIFT; 305 DWC2_WRITE_4(hsotg, HCFG, hcfg); 306 do_reset = 1; 307 } 308 } 309 } else { 310 /* Not low power */ 311 if (usbcfg & GUSBCFG_PHY_LP_CLK_SEL) { 312 usbcfg &= ~GUSBCFG_PHY_LP_CLK_SEL; 313 DWC2_WRITE_4(hsotg, GUSBCFG, usbcfg); 314 do_reset = 1; 315 } 316 } 317 318 if (do_reset) { 319 *hprt0_modify |= HPRT0_RST; 320 DWC2_WRITE_4(hsotg, HPRT0, *hprt0_modify); 321 queue_delayed_work(hsotg->wq_otg, &hsotg->reset_work, 322 msecs_to_jiffies(60)); 323 } else { 324 /* Port has been enabled, set the reset change flag */ 325 hsotg->flags.b.port_reset_change = 1; 326 dwc2_root_intr(hsotg->hsotg_sc); 327 328 } 329 } 330 331 /* 332 * There are multiple conditions that can cause a port interrupt. This function 333 * determines which interrupt conditions have occurred and handles them 334 * appropriately. 335 */ 336 static void dwc2_port_intr(struct dwc2_hsotg *hsotg) 337 { 338 u32 hprt0; 339 u32 hprt0_modify; 340 341 dev_vdbg(hsotg->dev, "--Port Interrupt--\n"); 342 343 hprt0 = DWC2_READ_4(hsotg, HPRT0); 344 hprt0_modify = hprt0; 345 346 /* 347 * Clear appropriate bits in HPRT0 to clear the interrupt bit in 348 * GINTSTS 349 */ 350 hprt0_modify &= ~(HPRT0_ENA | HPRT0_CONNDET | HPRT0_ENACHG | 351 HPRT0_OVRCURRCHG); 352 353 /* 354 * Port Connect Detected 355 * Set flag and clear if detected 356 */ 357 if (hprt0 & HPRT0_CONNDET) { 358 DWC2_WRITE_4(hsotg, HPRT0, hprt0_modify | HPRT0_CONNDET); 359 360 dev_vdbg(hsotg->dev, 361 "--Port Interrupt HPRT0=0x%08x Port Connect Detected--\n", 362 hprt0); 363 dwc2_hcd_connect(hsotg); 364 365 /* 366 * The Hub driver asserts a reset when it sees port connect 367 * status change flag 368 */ 369 } 370 371 /* 372 * Port Enable Changed 373 * Clear if detected - Set internal flag if disabled 374 */ 375 if (hprt0 & HPRT0_ENACHG) { 376 DWC2_WRITE_4(hsotg, HPRT0, hprt0_modify | HPRT0_ENACHG); 377 dev_vdbg(hsotg->dev, 378 " --Port Interrupt HPRT0=0x%08x Port Enable Changed (now %d)--\n", 379 hprt0, !!(hprt0 & HPRT0_ENA)); 380 if (hprt0 & HPRT0_ENA) { 381 hsotg->new_connection = true; 382 dwc2_hprt0_enable(hsotg, hprt0, &hprt0_modify); 383 } else { 384 hsotg->flags.b.port_enable_change = 1; 385 if (hsotg->core_params->dma_desc_fs_enable) { 386 u32 hcfg; 387 388 hsotg->core_params->dma_desc_enable = 0; 389 hsotg->new_connection = false; 390 hcfg = DWC2_READ_4(hsotg, HCFG); 391 hcfg &= ~HCFG_DESCDMA; 392 DWC2_WRITE_4(hsotg, HCFG, hcfg); 393 } 394 } 395 } 396 397 /* Overcurrent Change Interrupt */ 398 if (hprt0 & HPRT0_OVRCURRCHG) { 399 DWC2_WRITE_4(hsotg, HPRT0, hprt0_modify | HPRT0_OVRCURRCHG); 400 dev_vdbg(hsotg->dev, 401 " --Port Interrupt HPRT0=0x%08x Port Overcurrent Changed--\n", 402 hprt0); 403 hsotg->flags.b.port_over_current_change = 1; 404 } 405 406 if (hsotg->flags.b.port_connect_status_change || 407 hsotg->flags.b.port_enable_change || 408 hsotg->flags.b.port_over_current_change) 409 dwc2_root_intr(hsotg->hsotg_sc); 410 } 411 412 /* 413 * Gets the actual length of a transfer after the transfer halts. halt_status 414 * holds the reason for the halt. 415 * 416 * For IN transfers where halt_status is DWC2_HC_XFER_COMPLETE, *short_read 417 * is set to 1 upon return if less than the requested number of bytes were 418 * transferred. short_read may also be NULL on entry, in which case it remains 419 * unchanged. 420 */ 421 static u32 dwc2_get_actual_xfer_length(struct dwc2_hsotg *hsotg, 422 struct dwc2_host_chan *chan, int chnum, 423 struct dwc2_qtd *qtd, 424 enum dwc2_halt_status halt_status, 425 int *short_read) 426 { 427 u32 hctsiz, count, length; 428 429 hctsiz = DWC2_READ_4(hsotg, HCTSIZ(chnum)); 430 431 if (halt_status == DWC2_HC_XFER_COMPLETE) { 432 if (chan->ep_is_in) { 433 count = (hctsiz & TSIZ_XFERSIZE_MASK) >> 434 TSIZ_XFERSIZE_SHIFT; 435 length = chan->xfer_len - count; 436 if (short_read != NULL) 437 *short_read = (count != 0); 438 } else if (chan->qh->do_split) { 439 length = qtd->ssplit_out_xfer_count; 440 } else { 441 length = chan->xfer_len; 442 } 443 } else { 444 /* 445 * Must use the hctsiz.pktcnt field to determine how much data 446 * has been transferred. This field reflects the number of 447 * packets that have been transferred via the USB. This is 448 * always an integral number of packets if the transfer was 449 * halted before its normal completion. (Can't use the 450 * hctsiz.xfersize field because that reflects the number of 451 * bytes transferred via the AHB, not the USB). 452 */ 453 count = (hctsiz & TSIZ_PKTCNT_MASK) >> TSIZ_PKTCNT_SHIFT; 454 length = (chan->start_pkt_count - count) * chan->max_packet; 455 } 456 457 return length; 458 } 459 460 /** 461 * dwc2_update_urb_state() - Updates the state of the URB after a Transfer 462 * Complete interrupt on the host channel. Updates the actual_length field 463 * of the URB based on the number of bytes transferred via the host channel. 464 * Sets the URB status if the data transfer is finished. 465 * 466 * Return: 1 if the data transfer specified by the URB is completely finished, 467 * 0 otherwise 468 */ 469 static int dwc2_update_urb_state(struct dwc2_hsotg *hsotg, 470 struct dwc2_host_chan *chan, int chnum, 471 struct dwc2_hcd_urb *urb, 472 struct dwc2_qtd *qtd) 473 { 474 int xfer_done = 0; 475 int short_read = 0; 476 int xfer_length = dwc2_get_actual_xfer_length(hsotg, chan, chnum, qtd, 477 DWC2_HC_XFER_COMPLETE, 478 &short_read); 479 480 if (urb->actual_length + xfer_length > urb->length) { 481 dev_warn(hsotg->dev, "%s(): trimming xfer length\n", __func__); 482 xfer_length = urb->length - urb->actual_length; 483 } 484 485 /* Non DWORD-aligned buffer case handling */ 486 if (chan->align_buf && xfer_length) { 487 dev_vdbg(hsotg->dev, "%s(): non-aligned buffer\n", __func__); 488 usb_syncmem(urb->usbdma, 0, chan->qh->dw_align_buf_size, 489 chan->ep_is_in ? 490 BUS_DMASYNC_POSTREAD : BUS_DMASYNC_POSTWRITE); 491 if (chan->ep_is_in) 492 memcpy(urb->buf + urb->actual_length, 493 chan->qh->dw_align_buf, xfer_length); 494 usb_syncmem(urb->usbdma, 0, chan->qh->dw_align_buf_size, 495 chan->ep_is_in ? 496 BUS_DMASYNC_PREREAD : BUS_DMASYNC_PREWRITE); 497 } 498 499 dev_vdbg(hsotg->dev, "urb->actual_length=%d xfer_length=%d\n", 500 urb->actual_length, xfer_length); 501 urb->actual_length += xfer_length; 502 503 if (xfer_length && chan->ep_type == USB_ENDPOINT_XFER_BULK && 504 (urb->flags & URB_SEND_ZERO_PACKET) && 505 urb->actual_length >= urb->length && 506 !(urb->length % chan->max_packet)) { 507 xfer_done = 0; 508 } else if (short_read || urb->actual_length >= urb->length) { 509 xfer_done = 1; 510 urb->status = 0; 511 } 512 513 dev_vdbg(hsotg->dev, "DWC_otg: %s: %s, channel %d\n", 514 __func__, (chan->ep_is_in ? "IN" : "OUT"), chnum); 515 dev_vdbg(hsotg->dev, " chan->xfer_len %d\n", chan->xfer_len); 516 dev_vdbg(hsotg->dev, " hctsiz.xfersize %d\n", 517 (DWC2_READ_4(hsotg, HCTSIZ(chnum)) & TSIZ_XFERSIZE_MASK) >> TSIZ_XFERSIZE_SHIFT); 518 dev_vdbg(hsotg->dev, " urb->transfer_buffer_length %d\n", urb->length); 519 dev_vdbg(hsotg->dev, " urb->actual_length %d\n", urb->actual_length); 520 dev_vdbg(hsotg->dev, " short_read %d, xfer_done %d\n", short_read, 521 xfer_done); 522 523 return xfer_done; 524 } 525 526 /* 527 * Save the starting data toggle for the next transfer. The data toggle is 528 * saved in the QH for non-control transfers and it's saved in the QTD for 529 * control transfers. 530 */ 531 void dwc2_hcd_save_data_toggle(struct dwc2_hsotg *hsotg, 532 struct dwc2_host_chan *chan, int chnum, 533 struct dwc2_qtd *qtd) 534 { 535 u32 hctsiz = DWC2_READ_4(hsotg, HCTSIZ(chnum)); 536 u32 pid = (hctsiz & TSIZ_SC_MC_PID_MASK) >> TSIZ_SC_MC_PID_SHIFT; 537 538 if (chan->ep_type != USB_ENDPOINT_XFER_CONTROL) { 539 if (pid == TSIZ_SC_MC_PID_DATA0) 540 chan->qh->data_toggle = DWC2_HC_PID_DATA0; 541 else 542 chan->qh->data_toggle = DWC2_HC_PID_DATA1; 543 } else { 544 if (pid == TSIZ_SC_MC_PID_DATA0) 545 qtd->data_toggle = DWC2_HC_PID_DATA0; 546 else 547 qtd->data_toggle = DWC2_HC_PID_DATA1; 548 } 549 } 550 551 /** 552 * dwc2_update_isoc_urb_state() - Updates the state of an Isochronous URB when 553 * the transfer is stopped for any reason. The fields of the current entry in 554 * the frame descriptor array are set based on the transfer state and the input 555 * halt_status. Completes the Isochronous URB if all the URB frames have been 556 * completed. 557 * 558 * Return: DWC2_HC_XFER_COMPLETE if there are more frames remaining to be 559 * transferred in the URB. Otherwise return DWC2_HC_XFER_URB_COMPLETE. 560 */ 561 static enum dwc2_halt_status dwc2_update_isoc_urb_state( 562 struct dwc2_hsotg *hsotg, struct dwc2_host_chan *chan, 563 int chnum, struct dwc2_qtd *qtd, 564 enum dwc2_halt_status halt_status) 565 { 566 struct dwc2_hcd_iso_packet_desc *frame_desc; 567 struct dwc2_hcd_urb *urb = qtd->urb; 568 569 if (!urb) 570 return DWC2_HC_XFER_NO_HALT_STATUS; 571 572 frame_desc = &urb->iso_descs[qtd->isoc_frame_index]; 573 574 switch (halt_status) { 575 case DWC2_HC_XFER_COMPLETE: 576 frame_desc->status = 0; 577 frame_desc->actual_length = dwc2_get_actual_xfer_length(hsotg, 578 chan, chnum, qtd, halt_status, NULL); 579 580 /* Non DWORD-aligned buffer case handling */ 581 if (chan->align_buf && frame_desc->actual_length) { 582 dev_vdbg(hsotg->dev, "%s(): non-aligned buffer\n", 583 __func__); 584 usb_dma_t *ud = &chan->qh->dw_align_buf_usbdma; 585 586 usb_syncmem(ud, 0, chan->qh->dw_align_buf_size, 587 chan->ep_is_in ? 588 BUS_DMASYNC_POSTREAD : BUS_DMASYNC_POSTWRITE); 589 if (chan->ep_is_in) 590 memcpy(urb->buf + frame_desc->offset + 591 qtd->isoc_split_offset, 592 chan->qh->dw_align_buf, 593 frame_desc->actual_length); 594 usb_syncmem(ud, 0, chan->qh->dw_align_buf_size, 595 chan->ep_is_in ? 596 BUS_DMASYNC_PREREAD : BUS_DMASYNC_PREWRITE); 597 } 598 break; 599 case DWC2_HC_XFER_FRAME_OVERRUN: 600 urb->error_count++; 601 if (chan->ep_is_in) 602 frame_desc->status = -ENOSR; 603 else 604 frame_desc->status = -ECOMM; 605 frame_desc->actual_length = 0; 606 break; 607 case DWC2_HC_XFER_BABBLE_ERR: 608 urb->error_count++; 609 frame_desc->status = -EOVERFLOW; 610 /* Don't need to update actual_length in this case */ 611 break; 612 case DWC2_HC_XFER_XACT_ERR: 613 urb->error_count++; 614 frame_desc->status = -EPROTO; 615 frame_desc->actual_length = dwc2_get_actual_xfer_length(hsotg, 616 chan, chnum, qtd, halt_status, NULL); 617 618 /* Non DWORD-aligned buffer case handling */ 619 if (chan->align_buf && frame_desc->actual_length) { 620 dev_vdbg(hsotg->dev, "%s(): non-aligned buffer\n", 621 __func__); 622 usb_dma_t *ud = &chan->qh->dw_align_buf_usbdma; 623 624 usb_syncmem(ud, 0, chan->qh->dw_align_buf_size, 625 chan->ep_is_in ? 626 BUS_DMASYNC_POSTREAD : BUS_DMASYNC_POSTWRITE); 627 if (chan->ep_is_in) 628 memcpy(urb->buf + frame_desc->offset + 629 qtd->isoc_split_offset, 630 chan->qh->dw_align_buf, 631 frame_desc->actual_length); 632 usb_syncmem(ud, 0, chan->qh->dw_align_buf_size, 633 chan->ep_is_in ? 634 BUS_DMASYNC_PREREAD : BUS_DMASYNC_PREWRITE); 635 } 636 637 /* Skip whole frame */ 638 if (chan->qh->do_split && 639 chan->ep_type == USB_ENDPOINT_XFER_ISOC && chan->ep_is_in && 640 hsotg->core_params->dma_enable > 0) { 641 qtd->complete_split = 0; 642 qtd->isoc_split_offset = 0; 643 } 644 645 break; 646 default: 647 dev_err(hsotg->dev, "Unhandled halt_status (%d)\n", 648 halt_status); 649 break; 650 } 651 652 if (++qtd->isoc_frame_index == urb->packet_count) { 653 /* 654 * urb->status is not used for isoc transfers. The individual 655 * frame_desc statuses are used instead. 656 */ 657 dwc2_host_complete(hsotg, qtd, 0); 658 halt_status = DWC2_HC_XFER_URB_COMPLETE; 659 } else { 660 halt_status = DWC2_HC_XFER_COMPLETE; 661 } 662 663 return halt_status; 664 } 665 666 /* 667 * Frees the first QTD in the QH's list if free_qtd is 1. For non-periodic 668 * QHs, removes the QH from the active non-periodic schedule. If any QTDs are 669 * still linked to the QH, the QH is added to the end of the inactive 670 * non-periodic schedule. For periodic QHs, removes the QH from the periodic 671 * schedule if no more QTDs are linked to the QH. 672 */ 673 static void dwc2_deactivate_qh(struct dwc2_hsotg *hsotg, struct dwc2_qh *qh, 674 int free_qtd) 675 { 676 int continue_split = 0; 677 struct dwc2_qtd *qtd; 678 679 if (dbg_qh(qh)) 680 dev_vdbg(hsotg->dev, " %s(%p,%p,%d)\n", __func__, 681 hsotg, qh, free_qtd); 682 683 if (list_empty(&qh->qtd_list)) { 684 dev_dbg(hsotg->dev, "## QTD list empty ##\n"); 685 goto no_qtd; 686 } 687 688 qtd = list_first_entry(&qh->qtd_list, struct dwc2_qtd, qtd_list_entry); 689 690 if (qtd->complete_split) 691 continue_split = 1; 692 else if (qtd->isoc_split_pos == DWC2_HCSPLT_XACTPOS_MID || 693 qtd->isoc_split_pos == DWC2_HCSPLT_XACTPOS_END) 694 continue_split = 1; 695 696 if (free_qtd) { 697 dwc2_hcd_qtd_unlink_and_free(hsotg, qtd, qh); 698 continue_split = 0; 699 } 700 701 no_qtd: 702 if (qh->channel) 703 qh->channel->align_buf = 0; 704 qh->channel = NULL; 705 dwc2_hcd_qh_deactivate(hsotg, qh, continue_split); 706 } 707 708 /** 709 * dwc2_release_channel() - Releases a host channel for use by other transfers 710 * 711 * @hsotg: The HCD state structure 712 * @chan: The host channel to release 713 * @qtd: The QTD associated with the host channel. This QTD may be 714 * freed if the transfer is complete or an error has occurred. 715 * @halt_status: Reason the channel is being released. This status 716 * determines the actions taken by this function. 717 * 718 * Also attempts to select and queue more transactions since at least one host 719 * channel is available. 720 */ 721 static void dwc2_release_channel(struct dwc2_hsotg *hsotg, 722 struct dwc2_host_chan *chan, 723 struct dwc2_qtd *qtd, 724 enum dwc2_halt_status halt_status) 725 { 726 enum dwc2_transaction_type tr_type; 727 u32 haintmsk; 728 int free_qtd = 0; 729 730 if (dbg_hc(chan)) 731 dev_vdbg(hsotg->dev, " %s: channel %d, halt_status %d\n", 732 __func__, chan->hc_num, halt_status); 733 734 switch (halt_status) { 735 case DWC2_HC_XFER_URB_COMPLETE: 736 free_qtd = 1; 737 break; 738 case DWC2_HC_XFER_AHB_ERR: 739 case DWC2_HC_XFER_STALL: 740 case DWC2_HC_XFER_BABBLE_ERR: 741 free_qtd = 1; 742 break; 743 case DWC2_HC_XFER_XACT_ERR: 744 if (qtd && qtd->error_count >= 3) { 745 dev_vdbg(hsotg->dev, 746 " Complete URB with transaction error\n"); 747 free_qtd = 1; 748 dwc2_host_complete(hsotg, qtd, -EPROTO); 749 } 750 break; 751 case DWC2_HC_XFER_URB_DEQUEUE: 752 /* 753 * The QTD has already been removed and the QH has been 754 * deactivated. Don't want to do anything except release the 755 * host channel and try to queue more transfers. 756 */ 757 goto cleanup; 758 case DWC2_HC_XFER_PERIODIC_INCOMPLETE: 759 dev_vdbg(hsotg->dev, " Complete URB with I/O error\n"); 760 free_qtd = 1; 761 dwc2_host_complete(hsotg, qtd, -EIO); 762 break; 763 case DWC2_HC_XFER_NO_HALT_STATUS: 764 default: 765 break; 766 } 767 768 dwc2_deactivate_qh(hsotg, chan->qh, free_qtd); 769 770 cleanup: 771 /* 772 * Release the host channel for use by other transfers. The cleanup 773 * function clears the channel interrupt enables and conditions, so 774 * there's no need to clear the Channel Halted interrupt separately. 775 */ 776 if (!list_empty(&chan->hc_list_entry)) 777 list_del(&chan->hc_list_entry); 778 dwc2_hc_cleanup(hsotg, chan); 779 list_add_tail(&chan->hc_list_entry, &hsotg->free_hc_list); 780 781 if (hsotg->core_params->uframe_sched > 0) { 782 hsotg->available_host_channels++; 783 } else { 784 switch (chan->ep_type) { 785 case USB_ENDPOINT_XFER_CONTROL: 786 case USB_ENDPOINT_XFER_BULK: 787 hsotg->non_periodic_channels--; 788 break; 789 default: 790 /* 791 * Don't release reservations for periodic channels 792 * here. That's done when a periodic transfer is 793 * descheduled (i.e. when the QH is removed from the 794 * periodic schedule). 795 */ 796 break; 797 } 798 } 799 800 haintmsk = DWC2_READ_4(hsotg, HAINTMSK); 801 haintmsk &= ~(1 << chan->hc_num); 802 DWC2_WRITE_4(hsotg, HAINTMSK, haintmsk); 803 804 /* Try to queue more transfers now that there's a free channel */ 805 tr_type = dwc2_hcd_select_transactions(hsotg); 806 if (tr_type != DWC2_TRANSACTION_NONE) 807 dwc2_hcd_queue_transactions(hsotg, tr_type); 808 } 809 810 /* 811 * Halts a host channel. If the channel cannot be halted immediately because 812 * the request queue is full, this function ensures that the FIFO empty 813 * interrupt for the appropriate queue is enabled so that the halt request can 814 * be queued when there is space in the request queue. 815 * 816 * This function may also be called in DMA mode. In that case, the channel is 817 * simply released since the core always halts the channel automatically in 818 * DMA mode. 819 */ 820 static void dwc2_halt_channel(struct dwc2_hsotg *hsotg, 821 struct dwc2_host_chan *chan, struct dwc2_qtd *qtd, 822 enum dwc2_halt_status halt_status) 823 { 824 if (dbg_hc(chan)) 825 dev_vdbg(hsotg->dev, "%s()\n", __func__); 826 827 if (hsotg->core_params->dma_enable > 0) { 828 if (dbg_hc(chan)) 829 dev_vdbg(hsotg->dev, "DMA enabled\n"); 830 dwc2_release_channel(hsotg, chan, qtd, halt_status); 831 return; 832 } 833 834 /* Slave mode processing */ 835 dwc2_hc_halt(hsotg, chan, halt_status); 836 837 if (chan->halt_on_queue) { 838 u32 gintmsk; 839 840 dev_vdbg(hsotg->dev, "Halt on queue\n"); 841 if (chan->ep_type == USB_ENDPOINT_XFER_CONTROL || 842 chan->ep_type == USB_ENDPOINT_XFER_BULK) { 843 dev_vdbg(hsotg->dev, "control/bulk\n"); 844 /* 845 * Make sure the Non-periodic Tx FIFO empty interrupt 846 * is enabled so that the non-periodic schedule will 847 * be processed 848 */ 849 gintmsk = DWC2_READ_4(hsotg, GINTMSK); 850 gintmsk |= GINTSTS_NPTXFEMP; 851 DWC2_WRITE_4(hsotg, GINTMSK, gintmsk); 852 } else { 853 dev_vdbg(hsotg->dev, "isoc/intr\n"); 854 /* 855 * Move the QH from the periodic queued schedule to 856 * the periodic assigned schedule. This allows the 857 * halt to be queued when the periodic schedule is 858 * processed. 859 */ 860 list_move(&chan->qh->qh_list_entry, 861 &hsotg->periodic_sched_assigned); 862 863 /* 864 * Make sure the Periodic Tx FIFO Empty interrupt is 865 * enabled so that the periodic schedule will be 866 * processed 867 */ 868 gintmsk = DWC2_READ_4(hsotg, GINTMSK); 869 gintmsk |= GINTSTS_PTXFEMP; 870 DWC2_WRITE_4(hsotg, GINTMSK, gintmsk); 871 } 872 } 873 } 874 875 /* 876 * Performs common cleanup for non-periodic transfers after a Transfer 877 * Complete interrupt. This function should be called after any endpoint type 878 * specific handling is finished to release the host channel. 879 */ 880 static void dwc2_complete_non_periodic_xfer(struct dwc2_hsotg *hsotg, 881 struct dwc2_host_chan *chan, 882 int chnum, struct dwc2_qtd *qtd, 883 enum dwc2_halt_status halt_status) 884 { 885 dev_vdbg(hsotg->dev, "%s()\n", __func__); 886 887 qtd->error_count = 0; 888 889 if (chan->hcint & HCINTMSK_NYET) { 890 /* 891 * Got a NYET on the last transaction of the transfer. This 892 * means that the endpoint should be in the PING state at the 893 * beginning of the next transfer. 894 */ 895 dev_vdbg(hsotg->dev, "got NYET\n"); 896 chan->qh->ping_state = 1; 897 } 898 899 /* 900 * Always halt and release the host channel to make it available for 901 * more transfers. There may still be more phases for a control 902 * transfer or more data packets for a bulk transfer at this point, 903 * but the host channel is still halted. A channel will be reassigned 904 * to the transfer when the non-periodic schedule is processed after 905 * the channel is released. This allows transactions to be queued 906 * properly via dwc2_hcd_queue_transactions, which also enables the 907 * Tx FIFO Empty interrupt if necessary. 908 */ 909 if (chan->ep_is_in) { 910 /* 911 * IN transfers in Slave mode require an explicit disable to 912 * halt the channel. (In DMA mode, this call simply releases 913 * the channel.) 914 */ 915 dwc2_halt_channel(hsotg, chan, qtd, halt_status); 916 } else { 917 /* 918 * The channel is automatically disabled by the core for OUT 919 * transfers in Slave mode 920 */ 921 dwc2_release_channel(hsotg, chan, qtd, halt_status); 922 } 923 } 924 925 /* 926 * Performs common cleanup for periodic transfers after a Transfer Complete 927 * interrupt. This function should be called after any endpoint type specific 928 * handling is finished to release the host channel. 929 */ 930 static void dwc2_complete_periodic_xfer(struct dwc2_hsotg *hsotg, 931 struct dwc2_host_chan *chan, int chnum, 932 struct dwc2_qtd *qtd, 933 enum dwc2_halt_status halt_status) 934 { 935 u32 hctsiz = DWC2_READ_4(hsotg, HCTSIZ(chnum)); 936 937 qtd->error_count = 0; 938 939 if (!chan->ep_is_in || (hctsiz & TSIZ_PKTCNT_MASK) == 0) 940 /* Core halts channel in these cases */ 941 dwc2_release_channel(hsotg, chan, qtd, halt_status); 942 else 943 /* Flush any outstanding requests from the Tx queue */ 944 dwc2_halt_channel(hsotg, chan, qtd, halt_status); 945 } 946 947 static int dwc2_xfercomp_isoc_split_in(struct dwc2_hsotg *hsotg, 948 struct dwc2_host_chan *chan, int chnum, 949 struct dwc2_qtd *qtd) 950 { 951 struct dwc2_hcd_iso_packet_desc *frame_desc; 952 u32 len; 953 954 if (!qtd->urb) 955 return 0; 956 957 frame_desc = &qtd->urb->iso_descs[qtd->isoc_frame_index]; 958 len = dwc2_get_actual_xfer_length(hsotg, chan, chnum, qtd, 959 DWC2_HC_XFER_COMPLETE, NULL); 960 if (!len) { 961 qtd->complete_split = 0; 962 qtd->isoc_split_offset = 0; 963 return 0; 964 } 965 966 frame_desc->actual_length += len; 967 968 if (chan->align_buf) { 969 dev_vdbg(hsotg->dev, "%s(): non-aligned buffer\n", __func__); 970 usb_syncmem(qtd->urb->usbdma, chan->qh->dw_align_buf_dma, 971 chan->qh->dw_align_buf_size, BUS_DMASYNC_POSTREAD); 972 memcpy(qtd->urb->buf + frame_desc->offset + 973 qtd->isoc_split_offset, chan->qh->dw_align_buf, len); 974 usb_syncmem(qtd->urb->usbdma, chan->qh->dw_align_buf_dma, 975 chan->qh->dw_align_buf_size, BUS_DMASYNC_PREREAD); 976 } 977 978 qtd->isoc_split_offset += len; 979 980 if (frame_desc->actual_length >= frame_desc->length) { 981 frame_desc->status = 0; 982 qtd->isoc_frame_index++; 983 qtd->complete_split = 0; 984 qtd->isoc_split_offset = 0; 985 } 986 987 if (qtd->isoc_frame_index == qtd->urb->packet_count) { 988 dwc2_host_complete(hsotg, qtd, 0); 989 dwc2_release_channel(hsotg, chan, qtd, 990 DWC2_HC_XFER_URB_COMPLETE); 991 } else { 992 dwc2_release_channel(hsotg, chan, qtd, 993 DWC2_HC_XFER_NO_HALT_STATUS); 994 } 995 996 return 1; /* Indicates that channel released */ 997 } 998 999 /* 1000 * Handles a host channel Transfer Complete interrupt. This handler may be 1001 * called in either DMA mode or Slave mode. 1002 */ 1003 static void dwc2_hc_xfercomp_intr(struct dwc2_hsotg *hsotg, 1004 struct dwc2_host_chan *chan, int chnum, 1005 struct dwc2_qtd *qtd) 1006 { 1007 struct dwc2_hcd_urb *urb = qtd->urb; 1008 enum dwc2_halt_status halt_status = DWC2_HC_XFER_COMPLETE; 1009 int pipe_type; 1010 int urb_xfer_done; 1011 1012 if (dbg_hc(chan)) 1013 dev_vdbg(hsotg->dev, 1014 "--Host Channel %d Interrupt: Transfer Complete--\n", 1015 chnum); 1016 1017 if (!urb) 1018 goto handle_xfercomp_done; 1019 1020 pipe_type = dwc2_hcd_get_pipe_type(&urb->pipe_info); 1021 1022 if (hsotg->core_params->dma_desc_enable > 0) { 1023 dwc2_hcd_complete_xfer_ddma(hsotg, chan, chnum, halt_status); 1024 if (pipe_type == USB_ENDPOINT_XFER_ISOC) 1025 /* Do not disable the interrupt, just clear it */ 1026 return; 1027 goto handle_xfercomp_done; 1028 } 1029 1030 /* Handle xfer complete on CSPLIT */ 1031 if (chan->qh->do_split) { 1032 if (chan->ep_type == USB_ENDPOINT_XFER_ISOC && chan->ep_is_in && 1033 hsotg->core_params->dma_enable > 0) { 1034 if (qtd->complete_split && 1035 dwc2_xfercomp_isoc_split_in(hsotg, chan, chnum, 1036 qtd)) 1037 goto handle_xfercomp_done; 1038 } else { 1039 qtd->complete_split = 0; 1040 } 1041 } 1042 1043 /* Update the QTD and URB states */ 1044 switch (pipe_type) { 1045 case USB_ENDPOINT_XFER_CONTROL: 1046 switch (qtd->control_phase) { 1047 case DWC2_CONTROL_SETUP: 1048 if (urb->length > 0) 1049 qtd->control_phase = DWC2_CONTROL_DATA; 1050 else 1051 qtd->control_phase = DWC2_CONTROL_STATUS; 1052 dev_vdbg(hsotg->dev, 1053 " Control setup transaction done\n"); 1054 halt_status = DWC2_HC_XFER_COMPLETE; 1055 break; 1056 case DWC2_CONTROL_DATA: 1057 urb_xfer_done = dwc2_update_urb_state(hsotg, chan, 1058 chnum, urb, qtd); 1059 if (urb_xfer_done) { 1060 qtd->control_phase = DWC2_CONTROL_STATUS; 1061 dev_vdbg(hsotg->dev, 1062 " Control data transfer done\n"); 1063 } else { 1064 dwc2_hcd_save_data_toggle(hsotg, chan, chnum, 1065 qtd); 1066 } 1067 halt_status = DWC2_HC_XFER_COMPLETE; 1068 break; 1069 case DWC2_CONTROL_STATUS: 1070 dev_vdbg(hsotg->dev, " Control transfer complete\n"); 1071 if (urb->status == -EINPROGRESS) 1072 urb->status = 0; 1073 dwc2_host_complete(hsotg, qtd, urb->status); 1074 halt_status = DWC2_HC_XFER_URB_COMPLETE; 1075 break; 1076 } 1077 1078 dwc2_complete_non_periodic_xfer(hsotg, chan, chnum, qtd, 1079 halt_status); 1080 break; 1081 case USB_ENDPOINT_XFER_BULK: 1082 dev_vdbg(hsotg->dev, " Bulk transfer complete\n"); 1083 urb_xfer_done = dwc2_update_urb_state(hsotg, chan, chnum, urb, 1084 qtd); 1085 if (urb_xfer_done) { 1086 dwc2_host_complete(hsotg, qtd, urb->status); 1087 halt_status = DWC2_HC_XFER_URB_COMPLETE; 1088 } else { 1089 halt_status = DWC2_HC_XFER_COMPLETE; 1090 } 1091 1092 dwc2_hcd_save_data_toggle(hsotg, chan, chnum, qtd); 1093 dwc2_complete_non_periodic_xfer(hsotg, chan, chnum, qtd, 1094 halt_status); 1095 break; 1096 case USB_ENDPOINT_XFER_INT: 1097 dev_vdbg(hsotg->dev, " Interrupt transfer complete\n"); 1098 urb_xfer_done = dwc2_update_urb_state(hsotg, chan, chnum, urb, 1099 qtd); 1100 1101 /* 1102 * Interrupt URB is done on the first transfer complete 1103 * interrupt 1104 */ 1105 if (urb_xfer_done) { 1106 dwc2_host_complete(hsotg, qtd, urb->status); 1107 halt_status = DWC2_HC_XFER_URB_COMPLETE; 1108 } else { 1109 halt_status = DWC2_HC_XFER_COMPLETE; 1110 } 1111 1112 dwc2_hcd_save_data_toggle(hsotg, chan, chnum, qtd); 1113 dwc2_complete_periodic_xfer(hsotg, chan, chnum, qtd, 1114 halt_status); 1115 break; 1116 case USB_ENDPOINT_XFER_ISOC: 1117 if (dbg_perio()) 1118 dev_vdbg(hsotg->dev, " Isochronous transfer complete\n"); 1119 if (qtd->isoc_split_pos == DWC2_HCSPLT_XACTPOS_ALL) 1120 halt_status = dwc2_update_isoc_urb_state(hsotg, chan, 1121 chnum, qtd, DWC2_HC_XFER_COMPLETE); 1122 dwc2_complete_periodic_xfer(hsotg, chan, chnum, qtd, 1123 halt_status); 1124 break; 1125 } 1126 1127 handle_xfercomp_done: 1128 disable_hc_int(hsotg, chnum, HCINTMSK_XFERCOMPL); 1129 } 1130 1131 /* 1132 * Handles a host channel STALL interrupt. This handler may be called in 1133 * either DMA mode or Slave mode. 1134 */ 1135 static void dwc2_hc_stall_intr(struct dwc2_hsotg *hsotg, 1136 struct dwc2_host_chan *chan, int chnum, 1137 struct dwc2_qtd *qtd) 1138 { 1139 struct dwc2_hcd_urb *urb = qtd->urb; 1140 int pipe_type; 1141 1142 dev_dbg(hsotg->dev, "--Host Channel %d Interrupt: STALL Received--\n", 1143 chnum); 1144 1145 if (hsotg->core_params->dma_desc_enable > 0) { 1146 dwc2_hcd_complete_xfer_ddma(hsotg, chan, chnum, 1147 DWC2_HC_XFER_STALL); 1148 goto handle_stall_done; 1149 } 1150 1151 if (!urb) 1152 goto handle_stall_halt; 1153 1154 pipe_type = dwc2_hcd_get_pipe_type(&urb->pipe_info); 1155 1156 if (pipe_type == USB_ENDPOINT_XFER_CONTROL) 1157 dwc2_host_complete(hsotg, qtd, -EPIPE); 1158 1159 if (pipe_type == USB_ENDPOINT_XFER_BULK || 1160 pipe_type == USB_ENDPOINT_XFER_INT) { 1161 dwc2_host_complete(hsotg, qtd, -EPIPE); 1162 /* 1163 * USB protocol requires resetting the data toggle for bulk 1164 * and interrupt endpoints when a CLEAR_FEATURE(ENDPOINT_HALT) 1165 * setup command is issued to the endpoint. Anticipate the 1166 * CLEAR_FEATURE command since a STALL has occurred and reset 1167 * the data toggle now. 1168 */ 1169 chan->qh->data_toggle = 0; 1170 } 1171 1172 handle_stall_halt: 1173 dwc2_halt_channel(hsotg, chan, qtd, DWC2_HC_XFER_STALL); 1174 1175 handle_stall_done: 1176 disable_hc_int(hsotg, chnum, HCINTMSK_STALL); 1177 } 1178 1179 /* 1180 * Updates the state of the URB when a transfer has been stopped due to an 1181 * abnormal condition before the transfer completes. Modifies the 1182 * actual_length field of the URB to reflect the number of bytes that have 1183 * actually been transferred via the host channel. 1184 */ 1185 static void dwc2_update_urb_state_abn(struct dwc2_hsotg *hsotg, 1186 struct dwc2_host_chan *chan, int chnum, 1187 struct dwc2_hcd_urb *urb, 1188 struct dwc2_qtd *qtd, 1189 enum dwc2_halt_status halt_status) 1190 { 1191 u32 xfer_length = dwc2_get_actual_xfer_length(hsotg, chan, chnum, 1192 qtd, halt_status, NULL); 1193 1194 if (urb->actual_length + xfer_length > urb->length) { 1195 dev_warn(hsotg->dev, "%s(): trimming xfer length\n", __func__); 1196 xfer_length = urb->length - urb->actual_length; 1197 } 1198 1199 /* Non DWORD-aligned buffer case handling */ 1200 if (chan->align_buf && xfer_length && chan->ep_is_in) { 1201 dev_vdbg(hsotg->dev, "%s(): non-aligned buffer\n", __func__); 1202 1203 usb_dma_t *ud = &chan->qh->dw_align_buf_usbdma; 1204 1205 usb_syncmem(ud, 0, chan->qh->dw_align_buf_size, 1206 chan->ep_is_in ? 1207 BUS_DMASYNC_POSTREAD : BUS_DMASYNC_POSTWRITE); 1208 if (chan->ep_is_in) 1209 memcpy(urb->buf + urb->actual_length, 1210 chan->qh->dw_align_buf, 1211 xfer_length); 1212 usb_syncmem(ud, 0, chan->qh->dw_align_buf_size, 1213 chan->ep_is_in ? 1214 BUS_DMASYNC_PREREAD : BUS_DMASYNC_PREWRITE); 1215 } 1216 1217 urb->actual_length += xfer_length; 1218 1219 dev_vdbg(hsotg->dev, "DWC_otg: %s: %s, channel %d\n", 1220 __func__, (chan->ep_is_in ? "IN" : "OUT"), chnum); 1221 dev_vdbg(hsotg->dev, " chan->start_pkt_count %d\n", 1222 chan->start_pkt_count); 1223 dev_vdbg(hsotg->dev, " hctsiz.pktcnt %d\n", 1224 (DWC2_READ_4(hsotg, HCTSIZ(chnum)) & TSIZ_PKTCNT_MASK) >> TSIZ_PKTCNT_SHIFT); 1225 dev_vdbg(hsotg->dev, " chan->max_packet %d\n", chan->max_packet); 1226 dev_vdbg(hsotg->dev, " bytes_transferred %d\n", 1227 xfer_length); 1228 dev_vdbg(hsotg->dev, " urb->actual_length %d\n", 1229 urb->actual_length); 1230 dev_vdbg(hsotg->dev, " urb->transfer_buffer_length %d\n", 1231 urb->length); 1232 } 1233 1234 /* 1235 * Handles a host channel NAK interrupt. This handler may be called in either 1236 * DMA mode or Slave mode. 1237 */ 1238 static void dwc2_hc_nak_intr(struct dwc2_hsotg *hsotg, 1239 struct dwc2_host_chan *chan, int chnum, 1240 struct dwc2_qtd *qtd) 1241 { 1242 if (!qtd) { 1243 dev_dbg(hsotg->dev, "%s: qtd is NULL\n", __func__); 1244 return; 1245 } 1246 1247 if (!qtd->urb) { 1248 dev_dbg(hsotg->dev, "%s: qtd->urb is NULL\n", __func__); 1249 return; 1250 } 1251 1252 if (dbg_hc(chan)) 1253 dev_vdbg(hsotg->dev, "--Host Channel %d Interrupt: NAK Received--\n", 1254 chnum); 1255 1256 /* 1257 * Handle NAK for IN/OUT SSPLIT/CSPLIT transfers, bulk, control, and 1258 * interrupt. Re-start the SSPLIT transfer. 1259 */ 1260 if (chan->do_split) { 1261 /* 1262 * When we get control/bulk NAKs then remember this so we holdoff on 1263 * this qh until the beginning of the next frame 1264 */ 1265 switch (dwc2_hcd_get_pipe_type(&qtd->urb->pipe_info)) { 1266 case USB_ENDPOINT_XFER_CONTROL: 1267 case USB_ENDPOINT_XFER_BULK: 1268 chan->qh->nak_frame = dwc2_hcd_get_frame_number(hsotg); 1269 break; 1270 } 1271 1272 if (chan->complete_split) 1273 qtd->error_count = 0; 1274 qtd->complete_split = 0; 1275 dwc2_halt_channel(hsotg, chan, qtd, DWC2_HC_XFER_NAK); 1276 goto handle_nak_done; 1277 } 1278 1279 switch (dwc2_hcd_get_pipe_type(&qtd->urb->pipe_info)) { 1280 case USB_ENDPOINT_XFER_CONTROL: 1281 case USB_ENDPOINT_XFER_BULK: 1282 if (hsotg->core_params->dma_enable > 0 && chan->ep_is_in) { 1283 /* 1284 * NAK interrupts are enabled on bulk/control IN 1285 * transfers in DMA mode for the sole purpose of 1286 * resetting the error count after a transaction error 1287 * occurs. The core will continue transferring data. 1288 */ 1289 qtd->error_count = 0; 1290 break; 1291 } 1292 1293 /* 1294 * NAK interrupts normally occur during OUT transfers in DMA 1295 * or Slave mode. For IN transfers, more requests will be 1296 * queued as request queue space is available. 1297 */ 1298 qtd->error_count = 0; 1299 1300 if (!chan->qh->ping_state) { 1301 dwc2_update_urb_state_abn(hsotg, chan, chnum, qtd->urb, 1302 qtd, DWC2_HC_XFER_NAK); 1303 dwc2_hcd_save_data_toggle(hsotg, chan, chnum, qtd); 1304 1305 if (chan->speed == USB_SPEED_HIGH) 1306 chan->qh->ping_state = 1; 1307 } 1308 1309 /* 1310 * Halt the channel so the transfer can be re-started from 1311 * the appropriate point or the PING protocol will 1312 * start/continue 1313 */ 1314 dwc2_halt_channel(hsotg, chan, qtd, DWC2_HC_XFER_NAK); 1315 break; 1316 case USB_ENDPOINT_XFER_INT: 1317 qtd->error_count = 0; 1318 dwc2_halt_channel(hsotg, chan, qtd, DWC2_HC_XFER_NAK); 1319 break; 1320 case USB_ENDPOINT_XFER_ISOC: 1321 /* Should never get called for isochronous transfers */ 1322 dev_err(hsotg->dev, "NACK interrupt for ISOC transfer\n"); 1323 break; 1324 } 1325 1326 handle_nak_done: 1327 disable_hc_int(hsotg, chnum, HCINTMSK_NAK); 1328 } 1329 1330 /* 1331 * Handles a host channel ACK interrupt. This interrupt is enabled when 1332 * performing the PING protocol in Slave mode, when errors occur during 1333 * either Slave mode or DMA mode, and during Start Split transactions. 1334 */ 1335 static void dwc2_hc_ack_intr(struct dwc2_hsotg *hsotg, 1336 struct dwc2_host_chan *chan, int chnum, 1337 struct dwc2_qtd *qtd) 1338 { 1339 struct dwc2_hcd_iso_packet_desc *frame_desc; 1340 1341 if (dbg_hc(chan)) 1342 dev_vdbg(hsotg->dev, "--Host Channel %d Interrupt: ACK Received--\n", 1343 chnum); 1344 1345 if (chan->do_split) { 1346 /* Handle ACK on SSPLIT. ACK should not occur in CSPLIT. */ 1347 if (!chan->ep_is_in && 1348 chan->data_pid_start != DWC2_HC_PID_SETUP) 1349 qtd->ssplit_out_xfer_count = chan->xfer_len; 1350 1351 if (chan->ep_type != USB_ENDPOINT_XFER_ISOC || chan->ep_is_in) { 1352 qtd->complete_split = 1; 1353 dwc2_halt_channel(hsotg, chan, qtd, DWC2_HC_XFER_ACK); 1354 } else { 1355 /* ISOC OUT */ 1356 switch (chan->xact_pos) { 1357 case DWC2_HCSPLT_XACTPOS_ALL: 1358 break; 1359 case DWC2_HCSPLT_XACTPOS_END: 1360 qtd->isoc_split_pos = DWC2_HCSPLT_XACTPOS_ALL; 1361 qtd->isoc_split_offset = 0; 1362 break; 1363 case DWC2_HCSPLT_XACTPOS_BEGIN: 1364 case DWC2_HCSPLT_XACTPOS_MID: 1365 /* 1366 * For BEGIN or MID, calculate the length for 1367 * the next microframe to determine the correct 1368 * SSPLIT token, either MID or END 1369 */ 1370 frame_desc = &qtd->urb->iso_descs[ 1371 qtd->isoc_frame_index]; 1372 qtd->isoc_split_offset += 188; 1373 1374 if (frame_desc->length - qtd->isoc_split_offset 1375 <= 188) 1376 qtd->isoc_split_pos = 1377 DWC2_HCSPLT_XACTPOS_END; 1378 else 1379 qtd->isoc_split_pos = 1380 DWC2_HCSPLT_XACTPOS_MID; 1381 break; 1382 } 1383 } 1384 } else { 1385 qtd->error_count = 0; 1386 1387 if (chan->qh->ping_state) { 1388 chan->qh->ping_state = 0; 1389 /* 1390 * Halt the channel so the transfer can be re-started 1391 * from the appropriate point. This only happens in 1392 * Slave mode. In DMA mode, the ping_state is cleared 1393 * when the transfer is started because the core 1394 * automatically executes the PING, then the transfer. 1395 */ 1396 dwc2_halt_channel(hsotg, chan, qtd, DWC2_HC_XFER_ACK); 1397 } 1398 } 1399 1400 /* 1401 * If the ACK occurred when _not_ in the PING state, let the channel 1402 * continue transferring data after clearing the error count 1403 */ 1404 disable_hc_int(hsotg, chnum, HCINTMSK_ACK); 1405 } 1406 1407 /* 1408 * Handles a host channel NYET interrupt. This interrupt should only occur on 1409 * Bulk and Control OUT endpoints and for complete split transactions. If a 1410 * NYET occurs at the same time as a Transfer Complete interrupt, it is 1411 * handled in the xfercomp interrupt handler, not here. This handler may be 1412 * called in either DMA mode or Slave mode. 1413 */ 1414 static void dwc2_hc_nyet_intr(struct dwc2_hsotg *hsotg, 1415 struct dwc2_host_chan *chan, int chnum, 1416 struct dwc2_qtd *qtd) 1417 { 1418 if (dbg_hc(chan)) 1419 dev_vdbg(hsotg->dev, "--Host Channel %d Interrupt: NYET Received--\n", 1420 chnum); 1421 1422 /* 1423 * NYET on CSPLIT 1424 * re-do the CSPLIT immediately on non-periodic 1425 */ 1426 if (chan->do_split && chan->complete_split) { 1427 if (chan->ep_is_in && chan->ep_type == USB_ENDPOINT_XFER_ISOC && 1428 hsotg->core_params->dma_enable > 0) { 1429 qtd->complete_split = 0; 1430 qtd->isoc_split_offset = 0; 1431 qtd->isoc_frame_index++; 1432 if (qtd->urb && 1433 qtd->isoc_frame_index == qtd->urb->packet_count) { 1434 dwc2_host_complete(hsotg, qtd, 0); 1435 dwc2_release_channel(hsotg, chan, qtd, 1436 DWC2_HC_XFER_URB_COMPLETE); 1437 } else { 1438 dwc2_release_channel(hsotg, chan, qtd, 1439 DWC2_HC_XFER_NO_HALT_STATUS); 1440 } 1441 goto handle_nyet_done; 1442 } 1443 1444 if (chan->ep_type == USB_ENDPOINT_XFER_INT || 1445 chan->ep_type == USB_ENDPOINT_XFER_ISOC) { 1446 int frnum = dwc2_hcd_get_frame_number(hsotg); 1447 1448 if (dwc2_full_frame_num(frnum) != 1449 dwc2_full_frame_num(chan->qh->sched_frame)) { 1450 /* 1451 * No longer in the same full speed frame. 1452 * Treat this as a transaction error. 1453 */ 1454 #if 0 1455 /* 1456 * Todo: Fix system performance so this can 1457 * be treated as an error. Right now complete 1458 * splits cannot be scheduled precisely enough 1459 * due to other system activity, so this error 1460 * occurs regularly in Slave mode. 1461 */ 1462 qtd->error_count++; 1463 #endif 1464 qtd->complete_split = 0; 1465 dwc2_halt_channel(hsotg, chan, qtd, 1466 DWC2_HC_XFER_XACT_ERR); 1467 /* Todo: add support for isoc release */ 1468 goto handle_nyet_done; 1469 } 1470 } 1471 1472 dwc2_halt_channel(hsotg, chan, qtd, DWC2_HC_XFER_NYET); 1473 goto handle_nyet_done; 1474 } 1475 1476 chan->qh->ping_state = 1; 1477 qtd->error_count = 0; 1478 1479 dwc2_update_urb_state_abn(hsotg, chan, chnum, qtd->urb, qtd, 1480 DWC2_HC_XFER_NYET); 1481 dwc2_hcd_save_data_toggle(hsotg, chan, chnum, qtd); 1482 1483 /* 1484 * Halt the channel and re-start the transfer so the PING protocol 1485 * will start 1486 */ 1487 dwc2_halt_channel(hsotg, chan, qtd, DWC2_HC_XFER_NYET); 1488 1489 handle_nyet_done: 1490 disable_hc_int(hsotg, chnum, HCINTMSK_NYET); 1491 } 1492 1493 /* 1494 * Handles a host channel babble interrupt. This handler may be called in 1495 * either DMA mode or Slave mode. 1496 */ 1497 static void dwc2_hc_babble_intr(struct dwc2_hsotg *hsotg, 1498 struct dwc2_host_chan *chan, int chnum, 1499 struct dwc2_qtd *qtd) 1500 { 1501 dev_dbg(hsotg->dev, "--Host Channel %d Interrupt: Babble Error--\n", 1502 chnum); 1503 1504 // dwc2_hc_handle_tt_clear(hsotg, chan, qtd); 1505 1506 if (hsotg->core_params->dma_desc_enable > 0) { 1507 dwc2_hcd_complete_xfer_ddma(hsotg, chan, chnum, 1508 DWC2_HC_XFER_BABBLE_ERR); 1509 goto disable_int; 1510 } 1511 1512 if (chan->ep_type != USB_ENDPOINT_XFER_ISOC) { 1513 dwc2_host_complete(hsotg, qtd, -EOVERFLOW); 1514 dwc2_halt_channel(hsotg, chan, qtd, DWC2_HC_XFER_BABBLE_ERR); 1515 } else { 1516 enum dwc2_halt_status halt_status; 1517 1518 halt_status = dwc2_update_isoc_urb_state(hsotg, chan, chnum, 1519 qtd, DWC2_HC_XFER_BABBLE_ERR); 1520 dwc2_halt_channel(hsotg, chan, qtd, halt_status); 1521 } 1522 1523 disable_int: 1524 disable_hc_int(hsotg, chnum, HCINTMSK_BBLERR); 1525 } 1526 1527 /* 1528 * Handles a host channel AHB error interrupt. This handler is only called in 1529 * DMA mode. 1530 */ 1531 static void dwc2_hc_ahberr_intr(struct dwc2_hsotg *hsotg, 1532 struct dwc2_host_chan *chan, int chnum, 1533 struct dwc2_qtd *qtd) 1534 { 1535 struct dwc2_hcd_urb *urb = qtd->urb; 1536 1537 dev_dbg(hsotg->dev, "--Host Channel %d Interrupt: AHB Error--\n", 1538 chnum); 1539 1540 if (!urb) 1541 goto handle_ahberr_halt; 1542 1543 // dwc2_hc_handle_tt_clear(hsotg, chan, qtd); 1544 1545 #ifdef DWC2_DEBUG 1546 const char *pipetype, *speed; 1547 1548 u32 hcchar = DWC2_READ_4(hsotg, HCCHAR(chnum)); 1549 u32 hcsplt = DWC2_READ_4(hsotg, HCSPLT(chnum)); 1550 u32 hctsiz = DWC2_READ_4(hsotg, HCTSIZ(chnum)); 1551 u32 hc_dma = DWC2_READ_4(hsotg, HCDMA(chnum)); 1552 1553 dev_err(hsotg->dev, "AHB ERROR, Channel %d\n", chnum); 1554 dev_err(hsotg->dev, " hcchar 0x%08x, hcsplt 0x%08x\n", hcchar, hcsplt); 1555 dev_err(hsotg->dev, " hctsiz 0x%08x, hc_dma 0x%08x\n", hctsiz, hc_dma); 1556 dev_err(hsotg->dev, " Device address: %d\n", 1557 dwc2_hcd_get_dev_addr(&urb->pipe_info)); 1558 dev_err(hsotg->dev, " Endpoint: %d, %s\n", 1559 dwc2_hcd_get_ep_num(&urb->pipe_info), 1560 dwc2_hcd_is_pipe_in(&urb->pipe_info) ? "IN" : "OUT"); 1561 1562 switch (dwc2_hcd_get_pipe_type(&urb->pipe_info)) { 1563 case USB_ENDPOINT_XFER_CONTROL: 1564 pipetype = "CONTROL"; 1565 break; 1566 case USB_ENDPOINT_XFER_BULK: 1567 pipetype = "BULK"; 1568 break; 1569 case USB_ENDPOINT_XFER_INT: 1570 pipetype = "INTERRUPT"; 1571 break; 1572 case USB_ENDPOINT_XFER_ISOC: 1573 pipetype = "ISOCHRONOUS"; 1574 break; 1575 default: 1576 pipetype = "UNKNOWN"; 1577 break; 1578 } 1579 1580 dev_err(hsotg->dev, " Endpoint type: %s\n", pipetype); 1581 1582 switch (chan->speed) { 1583 case USB_SPEED_HIGH: 1584 speed = "HIGH"; 1585 break; 1586 case USB_SPEED_FULL: 1587 speed = "FULL"; 1588 break; 1589 case USB_SPEED_LOW: 1590 speed = "LOW"; 1591 break; 1592 default: 1593 speed = "UNKNOWN"; 1594 break; 1595 } 1596 1597 dev_err(hsotg->dev, " Speed: %s\n", speed); 1598 1599 dev_err(hsotg->dev, " Max packet size: %d\n", 1600 dwc2_hcd_get_mps(&urb->pipe_info)); 1601 dev_err(hsotg->dev, " Data buffer length: %d\n", urb->length); 1602 dev_err(hsotg->dev, " Transfer buffer: %p, Transfer DMA: %08lx\n", 1603 urb->buf, (unsigned long)urb->dma); 1604 dev_err(hsotg->dev, " Setup buffer: %p, Setup DMA: %08lx\n", 1605 urb->setup_packet, (unsigned long)urb->setup_dma); 1606 dev_err(hsotg->dev, " Interval: %d\n", urb->interval); 1607 #endif 1608 1609 /* Core halts the channel for Descriptor DMA mode */ 1610 if (hsotg->core_params->dma_desc_enable > 0) { 1611 dwc2_hcd_complete_xfer_ddma(hsotg, chan, chnum, 1612 DWC2_HC_XFER_AHB_ERR); 1613 goto handle_ahberr_done; 1614 } 1615 1616 dwc2_host_complete(hsotg, qtd, -EIO); 1617 1618 handle_ahberr_halt: 1619 /* 1620 * Force a channel halt. Don't call dwc2_halt_channel because that won't 1621 * write to the HCCHARn register in DMA mode to force the halt. 1622 */ 1623 dwc2_hc_halt(hsotg, chan, DWC2_HC_XFER_AHB_ERR); 1624 1625 handle_ahberr_done: 1626 disable_hc_int(hsotg, chnum, HCINTMSK_AHBERR); 1627 } 1628 1629 /* 1630 * Handles a host channel transaction error interrupt. This handler may be 1631 * called in either DMA mode or Slave mode. 1632 */ 1633 static void dwc2_hc_xacterr_intr(struct dwc2_hsotg *hsotg, 1634 struct dwc2_host_chan *chan, int chnum, 1635 struct dwc2_qtd *qtd) 1636 { 1637 dev_dbg(hsotg->dev, 1638 "--Host Channel %d Interrupt: Transaction Error--\n", chnum); 1639 1640 // dwc2_hc_handle_tt_clear(hsotg, chan, qtd); 1641 1642 if (hsotg->core_params->dma_desc_enable > 0) { 1643 dwc2_hcd_complete_xfer_ddma(hsotg, chan, chnum, 1644 DWC2_HC_XFER_XACT_ERR); 1645 goto handle_xacterr_done; 1646 } 1647 1648 switch (dwc2_hcd_get_pipe_type(&qtd->urb->pipe_info)) { 1649 case USB_ENDPOINT_XFER_CONTROL: 1650 case USB_ENDPOINT_XFER_BULK: 1651 qtd->error_count++; 1652 if (!chan->qh->ping_state) { 1653 1654 dwc2_update_urb_state_abn(hsotg, chan, chnum, qtd->urb, 1655 qtd, DWC2_HC_XFER_XACT_ERR); 1656 dwc2_hcd_save_data_toggle(hsotg, chan, chnum, qtd); 1657 if (!chan->ep_is_in && chan->speed == USB_SPEED_HIGH) 1658 chan->qh->ping_state = 1; 1659 } 1660 1661 /* 1662 * Halt the channel so the transfer can be re-started from 1663 * the appropriate point or the PING protocol will start 1664 */ 1665 dwc2_halt_channel(hsotg, chan, qtd, DWC2_HC_XFER_XACT_ERR); 1666 break; 1667 case USB_ENDPOINT_XFER_INT: 1668 qtd->error_count++; 1669 if (chan->do_split && chan->complete_split) 1670 qtd->complete_split = 0; 1671 dwc2_halt_channel(hsotg, chan, qtd, DWC2_HC_XFER_XACT_ERR); 1672 break; 1673 case USB_ENDPOINT_XFER_ISOC: 1674 { 1675 enum dwc2_halt_status halt_status; 1676 1677 halt_status = dwc2_update_isoc_urb_state(hsotg, chan, 1678 chnum, qtd, DWC2_HC_XFER_XACT_ERR); 1679 dwc2_halt_channel(hsotg, chan, qtd, halt_status); 1680 } 1681 break; 1682 } 1683 1684 handle_xacterr_done: 1685 disable_hc_int(hsotg, chnum, HCINTMSK_XACTERR); 1686 } 1687 1688 /* 1689 * Handles a host channel frame overrun interrupt. This handler may be called 1690 * in either DMA mode or Slave mode. 1691 */ 1692 static void dwc2_hc_frmovrun_intr(struct dwc2_hsotg *hsotg, 1693 struct dwc2_host_chan *chan, int chnum, 1694 struct dwc2_qtd *qtd) 1695 { 1696 enum dwc2_halt_status halt_status; 1697 1698 if (dbg_hc(chan)) 1699 dev_dbg(hsotg->dev, "--Host Channel %d Interrupt: Frame Overrun--\n", 1700 chnum); 1701 1702 dwc2_hc_handle_tt_clear(hsotg, chan, qtd); 1703 1704 switch (dwc2_hcd_get_pipe_type(&qtd->urb->pipe_info)) { 1705 case USB_ENDPOINT_XFER_CONTROL: 1706 case USB_ENDPOINT_XFER_BULK: 1707 break; 1708 case USB_ENDPOINT_XFER_INT: 1709 dwc2_halt_channel(hsotg, chan, qtd, DWC2_HC_XFER_FRAME_OVERRUN); 1710 break; 1711 case USB_ENDPOINT_XFER_ISOC: 1712 halt_status = dwc2_update_isoc_urb_state(hsotg, chan, chnum, 1713 qtd, DWC2_HC_XFER_FRAME_OVERRUN); 1714 dwc2_halt_channel(hsotg, chan, qtd, halt_status); 1715 break; 1716 } 1717 1718 disable_hc_int(hsotg, chnum, HCINTMSK_FRMOVRUN); 1719 } 1720 1721 /* 1722 * Handles a host channel data toggle error interrupt. This handler may be 1723 * called in either DMA mode or Slave mode. 1724 */ 1725 static void dwc2_hc_datatglerr_intr(struct dwc2_hsotg *hsotg, 1726 struct dwc2_host_chan *chan, int chnum, 1727 struct dwc2_qtd *qtd) 1728 { 1729 dev_dbg(hsotg->dev, 1730 "--Host Channel %d Interrupt: Data Toggle Error--\n", chnum); 1731 1732 if (chan->ep_is_in) 1733 qtd->error_count = 0; 1734 else 1735 dev_err(hsotg->dev, 1736 "Data Toggle Error on OUT transfer, channel %d\n", 1737 chnum); 1738 1739 // dwc2_hc_handle_tt_clear(hsotg, chan, qtd); 1740 disable_hc_int(hsotg, chnum, HCINTMSK_DATATGLERR); 1741 } 1742 1743 /* 1744 * For debug only. It checks that a valid halt status is set and that 1745 * HCCHARn.chdis is clear. If there's a problem, corrective action is 1746 * taken and a warning is issued. 1747 * 1748 * Return: true if halt status is ok, false otherwise 1749 */ 1750 static bool dwc2_halt_status_ok(struct dwc2_hsotg *hsotg, 1751 struct dwc2_host_chan *chan, int chnum, 1752 struct dwc2_qtd *qtd) 1753 { 1754 #ifdef DWC2_DEBUG 1755 u32 hcchar; 1756 u32 hctsiz; 1757 u32 hcintmsk; 1758 u32 hcsplt; 1759 1760 if (chan->halt_status == DWC2_HC_XFER_NO_HALT_STATUS) { 1761 /* 1762 * This code is here only as a check. This condition should 1763 * never happen. Ignore the halt if it does occur. 1764 */ 1765 hcchar = DWC2_READ_4(hsotg, HCCHAR(chnum)); 1766 hctsiz = DWC2_READ_4(hsotg, HCTSIZ(chnum)); 1767 hcintmsk = DWC2_READ_4(hsotg, HCINTMSK(chnum)); 1768 hcsplt = DWC2_READ_4(hsotg, HCSPLT(chnum)); 1769 dev_dbg(hsotg->dev, 1770 "%s: chan->halt_status DWC2_HC_XFER_NO_HALT_STATUS,\n", 1771 __func__); 1772 dev_dbg(hsotg->dev, 1773 "channel %d, hcchar 0x%08x, hctsiz 0x%08x,\n", 1774 chnum, hcchar, hctsiz); 1775 dev_dbg(hsotg->dev, 1776 "hcint 0x%08x, hcintmsk 0x%08x, hcsplt 0x%08x,\n", 1777 chan->hcint, hcintmsk, hcsplt); 1778 if (qtd) 1779 dev_dbg(hsotg->dev, "qtd->complete_split %d\n", 1780 qtd->complete_split); 1781 dev_warn(hsotg->dev, 1782 "%s: no halt status, channel %d, ignoring interrupt\n", 1783 __func__, chnum); 1784 return false; 1785 } 1786 1787 /* 1788 * This code is here only as a check. hcchar.chdis should never be set 1789 * when the halt interrupt occurs. Halt the channel again if it does 1790 * occur. 1791 */ 1792 hcchar = DWC2_READ_4(hsotg, HCCHAR(chnum)); 1793 if (hcchar & HCCHAR_CHDIS) { 1794 dev_warn(hsotg->dev, 1795 "%s: hcchar.chdis set unexpectedly, hcchar 0x%08x, trying to halt again\n", 1796 __func__, hcchar); 1797 chan->halt_pending = 0; 1798 dwc2_halt_channel(hsotg, chan, qtd, chan->halt_status); 1799 return false; 1800 } 1801 #endif 1802 1803 return true; 1804 } 1805 1806 /* 1807 * Handles a host Channel Halted interrupt in DMA mode. This handler 1808 * determines the reason the channel halted and proceeds accordingly. 1809 */ 1810 static void dwc2_hc_chhltd_intr_dma(struct dwc2_hsotg *hsotg, 1811 struct dwc2_host_chan *chan, int chnum, 1812 struct dwc2_qtd *qtd) 1813 { 1814 u32 hcintmsk; 1815 int out_nak_enh = 0; 1816 1817 if (dbg_hc(chan)) 1818 dev_vdbg(hsotg->dev, 1819 "--Host Channel %d Interrupt: DMA Channel Halted--\n", 1820 chnum); 1821 1822 /* 1823 * For core with OUT NAK enhancement, the flow for high-speed 1824 * CONTROL/BULK OUT is handled a little differently 1825 */ 1826 if (hsotg->hw_params.snpsid >= DWC2_CORE_REV_2_71a) { 1827 if (chan->speed == USB_SPEED_HIGH && !chan->ep_is_in && 1828 (chan->ep_type == USB_ENDPOINT_XFER_CONTROL || 1829 chan->ep_type == USB_ENDPOINT_XFER_BULK)) { 1830 out_nak_enh = 1; 1831 } 1832 } 1833 1834 if (chan->halt_status == DWC2_HC_XFER_URB_DEQUEUE || 1835 (chan->halt_status == DWC2_HC_XFER_AHB_ERR && 1836 hsotg->core_params->dma_desc_enable <= 0)) { 1837 if (hsotg->core_params->dma_desc_enable > 0) 1838 dwc2_hcd_complete_xfer_ddma(hsotg, chan, chnum, 1839 chan->halt_status); 1840 else 1841 /* 1842 * Just release the channel. A dequeue can happen on a 1843 * transfer timeout. In the case of an AHB Error, the 1844 * channel was forced to halt because there's no way to 1845 * gracefully recover. 1846 */ 1847 dwc2_release_channel(hsotg, chan, qtd, 1848 chan->halt_status); 1849 return; 1850 } 1851 1852 hcintmsk = DWC2_READ_4(hsotg, HCINTMSK(chnum)); 1853 1854 if (chan->hcint & HCINTMSK_XFERCOMPL) { 1855 /* 1856 * Todo: This is here because of a possible hardware bug. Spec 1857 * says that on SPLIT-ISOC OUT transfers in DMA mode that a HALT 1858 * interrupt w/ACK bit set should occur, but I only see the 1859 * XFERCOMP bit, even with it masked out. This is a workaround 1860 * for that behavior. Should fix this when hardware is fixed. 1861 */ 1862 if (chan->ep_type == USB_ENDPOINT_XFER_ISOC && !chan->ep_is_in) 1863 dwc2_hc_ack_intr(hsotg, chan, chnum, qtd); 1864 dwc2_hc_xfercomp_intr(hsotg, chan, chnum, qtd); 1865 } else if (chan->hcint & HCINTMSK_STALL) { 1866 dwc2_hc_stall_intr(hsotg, chan, chnum, qtd); 1867 } else if ((chan->hcint & HCINTMSK_XACTERR) && 1868 hsotg->core_params->dma_desc_enable <= 0) { 1869 if (out_nak_enh) { 1870 if (chan->hcint & 1871 (HCINTMSK_NYET | HCINTMSK_NAK | HCINTMSK_ACK)) { 1872 dev_vdbg(hsotg->dev, 1873 "XactErr with NYET/NAK/ACK\n"); 1874 qtd->error_count = 0; 1875 } else { 1876 dev_vdbg(hsotg->dev, 1877 "XactErr without NYET/NAK/ACK\n"); 1878 } 1879 } 1880 1881 /* 1882 * Must handle xacterr before nak or ack. Could get a xacterr 1883 * at the same time as either of these on a BULK/CONTROL OUT 1884 * that started with a PING. The xacterr takes precedence. 1885 */ 1886 dwc2_hc_xacterr_intr(hsotg, chan, chnum, qtd); 1887 } else if ((chan->hcint & HCINTMSK_XCS_XACT) && 1888 hsotg->core_params->dma_desc_enable > 0) { 1889 dwc2_hc_xacterr_intr(hsotg, chan, chnum, qtd); 1890 } else if ((chan->hcint & HCINTMSK_AHBERR) && 1891 hsotg->core_params->dma_desc_enable > 0) { 1892 dwc2_hc_ahberr_intr(hsotg, chan, chnum, qtd); 1893 } else if (chan->hcint & HCINTMSK_BBLERR) { 1894 dwc2_hc_babble_intr(hsotg, chan, chnum, qtd); 1895 } else if (chan->hcint & HCINTMSK_FRMOVRUN) { 1896 dwc2_hc_frmovrun_intr(hsotg, chan, chnum, qtd); 1897 } else if (!out_nak_enh) { 1898 if (chan->hcint & HCINTMSK_NYET) { 1899 /* 1900 * Must handle nyet before nak or ack. Could get a nyet 1901 * at the same time as either of those on a BULK/CONTROL 1902 * OUT that started with a PING. The nyet takes 1903 * precedence. 1904 */ 1905 dwc2_hc_nyet_intr(hsotg, chan, chnum, qtd); 1906 } else if ((chan->hcint & HCINTMSK_NAK) && 1907 !(hcintmsk & HCINTMSK_NAK)) { 1908 /* 1909 * If nak is not masked, it's because a non-split IN 1910 * transfer is in an error state. In that case, the nak 1911 * is handled by the nak interrupt handler, not here. 1912 * Handle nak here for BULK/CONTROL OUT transfers, which 1913 * halt on a NAK to allow rewinding the buffer pointer. 1914 */ 1915 dwc2_hc_nak_intr(hsotg, chan, chnum, qtd); 1916 } else if ((chan->hcint & HCINTMSK_ACK) && 1917 !(hcintmsk & HCINTMSK_ACK)) { 1918 /* 1919 * If ack is not masked, it's because a non-split IN 1920 * transfer is in an error state. In that case, the ack 1921 * is handled by the ack interrupt handler, not here. 1922 * Handle ack here for split transfers. Start splits 1923 * halt on ACK. 1924 */ 1925 dwc2_hc_ack_intr(hsotg, chan, chnum, qtd); 1926 } else { 1927 if (chan->ep_type == USB_ENDPOINT_XFER_INT || 1928 chan->ep_type == USB_ENDPOINT_XFER_ISOC) { 1929 /* 1930 * A periodic transfer halted with no other 1931 * channel interrupts set. Assume it was halted 1932 * by the core because it could not be completed 1933 * in its scheduled (micro)frame. 1934 */ 1935 dev_dbg(hsotg->dev, 1936 "%s: Halt channel %d (assume incomplete periodic transfer)\n", 1937 __func__, chnum); 1938 dwc2_halt_channel(hsotg, chan, qtd, 1939 DWC2_HC_XFER_PERIODIC_INCOMPLETE); 1940 } else { 1941 dev_err(hsotg->dev, 1942 "%s: Channel %d - ChHltd set, but reason is unknown\n", 1943 __func__, chnum); 1944 dev_err(hsotg->dev, 1945 "hcint 0x%08x, intsts 0x%08x\n", 1946 chan->hcint, 1947 DWC2_READ_4(hsotg, GINTSTS)); 1948 goto error; 1949 } 1950 } 1951 } else { 1952 dev_info(hsotg->dev, 1953 "NYET/NAK/ACK/other in non-error case, 0x%08x\n", 1954 chan->hcint); 1955 error: 1956 /* Failthrough: use 3-strikes rule */ 1957 qtd->error_count++; 1958 dwc2_update_urb_state_abn(hsotg, chan, chnum, qtd->urb, 1959 qtd, DWC2_HC_XFER_XACT_ERR); 1960 dwc2_hcd_save_data_toggle(hsotg, chan, chnum, qtd); 1961 dwc2_halt_channel(hsotg, chan, qtd, DWC2_HC_XFER_XACT_ERR); 1962 } 1963 } 1964 1965 /* 1966 * Handles a host channel Channel Halted interrupt 1967 * 1968 * In slave mode, this handler is called only when the driver specifically 1969 * requests a halt. This occurs during handling other host channel interrupts 1970 * (e.g. nak, xacterr, stall, nyet, etc.). 1971 * 1972 * In DMA mode, this is the interrupt that occurs when the core has finished 1973 * processing a transfer on a channel. Other host channel interrupts (except 1974 * ahberr) are disabled in DMA mode. 1975 */ 1976 static void dwc2_hc_chhltd_intr(struct dwc2_hsotg *hsotg, 1977 struct dwc2_host_chan *chan, int chnum, 1978 struct dwc2_qtd *qtd) 1979 { 1980 if (dbg_hc(chan)) 1981 dev_vdbg(hsotg->dev, "--Host Channel %d Interrupt: Channel Halted--\n", 1982 chnum); 1983 1984 if (hsotg->core_params->dma_enable > 0) { 1985 dwc2_hc_chhltd_intr_dma(hsotg, chan, chnum, qtd); 1986 } else { 1987 if (!dwc2_halt_status_ok(hsotg, chan, chnum, qtd)) 1988 return; 1989 dwc2_release_channel(hsotg, chan, qtd, chan->halt_status); 1990 } 1991 } 1992 1993 /* 1994 * Check if the given qtd is still the top of the list (and thus valid). 1995 * 1996 * If dwc2_hcd_qtd_unlink_and_free() has been called since we grabbed 1997 * the qtd from the top of the list, this will return false (otherwise true). 1998 */ 1999 static bool dwc2_check_qtd_still_ok(struct dwc2_qtd *qtd, struct dwc2_qh *qh) 2000 { 2001 struct dwc2_qtd *cur_head; 2002 2003 if (qh == NULL) 2004 return false; 2005 2006 cur_head = list_first_entry(&qh->qtd_list, struct dwc2_qtd, 2007 qtd_list_entry); 2008 return (cur_head == qtd); 2009 } 2010 2011 /* Handles interrupt for a specific Host Channel */ 2012 static void dwc2_hc_n_intr(struct dwc2_hsotg *hsotg, int chnum) 2013 { 2014 struct dwc2_qtd *qtd; 2015 struct dwc2_host_chan *chan; 2016 u32 hcint, hcintmsk; 2017 2018 chan = hsotg->hc_ptr_array[chnum]; 2019 2020 hcint = DWC2_READ_4(hsotg, HCINT(chnum)); 2021 hcintmsk = DWC2_READ_4(hsotg, HCINTMSK(chnum)); 2022 if (!chan) { 2023 dev_err(hsotg->dev, "## hc_ptr_array for channel is NULL ##\n"); 2024 DWC2_WRITE_4(hsotg, HCINT(chnum), hcint); 2025 return; 2026 } 2027 2028 if (dbg_hc(chan)) { 2029 dev_vdbg(hsotg->dev, "--Host Channel Interrupt--, Channel %d\n", 2030 chnum); 2031 dev_vdbg(hsotg->dev, 2032 " hcint 0x%08x, hcintmsk 0x%08x, hcint&hcintmsk 0x%08x\n", 2033 hcint, hcintmsk, hcint & hcintmsk); 2034 } 2035 2036 DWC2_WRITE_4(hsotg, HCINT(chnum), hcint); 2037 chan->hcint = hcint; 2038 hcint &= hcintmsk; 2039 2040 /* 2041 * If the channel was halted due to a dequeue, the qtd list might 2042 * be empty or at least the first entry will not be the active qtd. 2043 * In this case, take a shortcut and just release the channel. 2044 */ 2045 if (chan->halt_status == DWC2_HC_XFER_URB_DEQUEUE) { 2046 /* 2047 * If the channel was halted, this should be the only 2048 * interrupt unmasked 2049 */ 2050 WARN_ON(hcint != HCINTMSK_CHHLTD); 2051 if (hsotg->core_params->dma_desc_enable > 0) 2052 dwc2_hcd_complete_xfer_ddma(hsotg, chan, chnum, 2053 chan->halt_status); 2054 else 2055 dwc2_release_channel(hsotg, chan, NULL, 2056 chan->halt_status); 2057 return; 2058 } 2059 2060 if (list_empty(&chan->qh->qtd_list)) { 2061 /* 2062 * TODO: Will this ever happen with the 2063 * DWC2_HC_XFER_URB_DEQUEUE handling above? 2064 */ 2065 dev_dbg(hsotg->dev, "## no QTD queued for channel %d ##\n", 2066 chnum); 2067 dev_dbg(hsotg->dev, 2068 " hcint 0x%08x, hcintmsk 0x%08x, hcint&hcintmsk 0x%08x\n", 2069 chan->hcint, hcintmsk, hcint); 2070 chan->halt_status = DWC2_HC_XFER_NO_HALT_STATUS; 2071 disable_hc_int(hsotg, chnum, HCINTMSK_CHHLTD); 2072 chan->hcint = 0; 2073 return; 2074 } 2075 2076 qtd = list_first_entry(&chan->qh->qtd_list, struct dwc2_qtd, 2077 qtd_list_entry); 2078 2079 if (hsotg->core_params->dma_enable <= 0) { 2080 if ((hcint & HCINTMSK_CHHLTD) && hcint != HCINTMSK_CHHLTD) 2081 hcint &= ~HCINTMSK_CHHLTD; 2082 } 2083 2084 if (hcint & HCINTMSK_XFERCOMPL) { 2085 dwc2_hc_xfercomp_intr(hsotg, chan, chnum, qtd); 2086 /* 2087 * If NYET occurred at same time as Xfer Complete, the NYET is 2088 * handled by the Xfer Complete interrupt handler. Don't want 2089 * to call the NYET interrupt handler in this case. 2090 */ 2091 hcint &= ~HCINTMSK_NYET; 2092 } 2093 2094 if (hcint & HCINTMSK_CHHLTD) { 2095 dwc2_hc_chhltd_intr(hsotg, chan, chnum, qtd); 2096 if (!dwc2_check_qtd_still_ok(qtd, chan->qh)) 2097 goto exit; 2098 } 2099 if (hcint & HCINTMSK_AHBERR) { 2100 dwc2_hc_ahberr_intr(hsotg, chan, chnum, qtd); 2101 if (!dwc2_check_qtd_still_ok(qtd, chan->qh)) 2102 goto exit; 2103 } 2104 if (hcint & HCINTMSK_STALL) { 2105 dwc2_hc_stall_intr(hsotg, chan, chnum, qtd); 2106 if (!dwc2_check_qtd_still_ok(qtd, chan->qh)) 2107 goto exit; 2108 } 2109 if (hcint & HCINTMSK_NAK) { 2110 dwc2_hc_nak_intr(hsotg, chan, chnum, qtd); 2111 if (!dwc2_check_qtd_still_ok(qtd, chan->qh)) 2112 goto exit; 2113 } 2114 if (hcint & HCINTMSK_ACK) { 2115 dwc2_hc_ack_intr(hsotg, chan, chnum, qtd); 2116 if (!dwc2_check_qtd_still_ok(qtd, chan->qh)) 2117 goto exit; 2118 } 2119 if (hcint & HCINTMSK_NYET) { 2120 dwc2_hc_nyet_intr(hsotg, chan, chnum, qtd); 2121 if (!dwc2_check_qtd_still_ok(qtd, chan->qh)) 2122 goto exit; 2123 } 2124 if (hcint & HCINTMSK_XACTERR) { 2125 dwc2_hc_xacterr_intr(hsotg, chan, chnum, qtd); 2126 if (!dwc2_check_qtd_still_ok(qtd, chan->qh)) 2127 goto exit; 2128 } 2129 if (hcint & HCINTMSK_BBLERR) { 2130 dwc2_hc_babble_intr(hsotg, chan, chnum, qtd); 2131 if (!dwc2_check_qtd_still_ok(qtd, chan->qh)) 2132 goto exit; 2133 } 2134 if (hcint & HCINTMSK_FRMOVRUN) { 2135 dwc2_hc_frmovrun_intr(hsotg, chan, chnum, qtd); 2136 if (!dwc2_check_qtd_still_ok(qtd, chan->qh)) 2137 goto exit; 2138 } 2139 if (hcint & HCINTMSK_DATATGLERR) { 2140 dwc2_hc_datatglerr_intr(hsotg, chan, chnum, qtd); 2141 if (!dwc2_check_qtd_still_ok(qtd, chan->qh)) 2142 goto exit; 2143 } 2144 2145 exit: 2146 chan->hcint = 0; 2147 } 2148 2149 /* 2150 * This interrupt indicates that one or more host channels has a pending 2151 * interrupt. There are multiple conditions that can cause each host channel 2152 * interrupt. This function determines which conditions have occurred for each 2153 * host channel interrupt and handles them appropriately. 2154 */ 2155 static void dwc2_hc_intr(struct dwc2_hsotg *hsotg) 2156 { 2157 u32 haint; 2158 int i; 2159 2160 haint = DWC2_READ_4(hsotg, HAINT); 2161 if (dbg_perio()) { 2162 dev_vdbg(hsotg->dev, "%s()\n", __func__); 2163 2164 dev_vdbg(hsotg->dev, "HAINT=%08x\n", haint); 2165 } 2166 2167 for (i = 0; i < hsotg->core_params->host_channels; i++) { 2168 if (haint & (1 << i)) 2169 dwc2_hc_n_intr(hsotg, i); 2170 } 2171 } 2172 2173 /* This function handles interrupts for the HCD */ 2174 irqreturn_t dwc2_handle_hcd_intr(struct dwc2_hsotg *hsotg) 2175 { 2176 u32 gintsts, dbg_gintsts; 2177 irqreturn_t retval = IRQ_NONE; 2178 2179 if (!dwc2_is_controller_alive(hsotg)) { 2180 dev_warn(hsotg->dev, "Controller is dead\n"); 2181 return retval; 2182 } 2183 2184 KASSERT(mutex_owned(&hsotg->lock)); 2185 2186 /* Check if HOST Mode */ 2187 if (dwc2_is_host_mode(hsotg)) { 2188 gintsts = dwc2_read_core_intr(hsotg); 2189 if (!gintsts) { 2190 return retval; 2191 } 2192 2193 retval = IRQ_HANDLED; 2194 2195 dbg_gintsts = gintsts; 2196 #ifndef DEBUG_SOF 2197 dbg_gintsts &= ~GINTSTS_SOF; 2198 #endif 2199 if (!dbg_perio()) 2200 dbg_gintsts &= ~(GINTSTS_HCHINT | GINTSTS_RXFLVL | 2201 GINTSTS_PTXFEMP); 2202 2203 /* Only print if there are any non-suppressed interrupts left */ 2204 if (dbg_gintsts) 2205 dev_vdbg(hsotg->dev, 2206 "DWC OTG HCD Interrupt Detected gintsts&gintmsk=0x%08x\n", 2207 gintsts); 2208 2209 if (gintsts & GINTSTS_SOF) 2210 dwc2_sof_intr(hsotg); 2211 if (gintsts & GINTSTS_RXFLVL) 2212 dwc2_rx_fifo_level_intr(hsotg); 2213 if (gintsts & GINTSTS_NPTXFEMP) 2214 dwc2_np_tx_fifo_empty_intr(hsotg); 2215 if (gintsts & GINTSTS_PRTINT) 2216 dwc2_port_intr(hsotg); 2217 if (gintsts & GINTSTS_HCHINT) 2218 dwc2_hc_intr(hsotg); 2219 if (gintsts & GINTSTS_PTXFEMP) 2220 dwc2_perio_tx_fifo_empty_intr(hsotg); 2221 2222 if (dbg_gintsts) { 2223 dev_vdbg(hsotg->dev, 2224 "DWC OTG HCD Finished Servicing Interrupts\n"); 2225 dev_vdbg(hsotg->dev, 2226 "DWC OTG HCD gintsts=0x%08x gintmsk=0x%08x\n", 2227 DWC2_READ_4(hsotg, GINTSTS), 2228 DWC2_READ_4(hsotg, GINTMSK)); 2229 } 2230 } 2231 2232 return retval; 2233 } 2234