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