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