blob: 3d686540d250838cda10c592ed8157b84c816e3a [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{
Douglas Andersond82a810e2016-01-28 18:20:02 -080090 struct usb_device *root_hub = dwc2_hsotg_to_hcd(hsotg)->self.root_hub;
Paul Zimmerman7359d482013-03-11 17:47:59 -070091 struct urb *usb_urb;
92
Paul Zimmerman399fdf92013-07-13 14:53:50 -070093 if (!chan->qh)
94 return;
95
96 if (chan->qh->dev_speed == USB_SPEED_HIGH)
97 return;
98
99 if (!qtd->urb)
Paul Zimmerman7359d482013-03-11 17:47:59 -0700100 return;
101
102 usb_urb = qtd->urb->priv;
Paul Zimmerman399fdf92013-07-13 14:53:50 -0700103 if (!usb_urb || !usb_urb->dev || !usb_urb->dev->tt)
Paul Zimmerman7359d482013-03-11 17:47:59 -0700104 return;
105
Douglas Andersond82a810e2016-01-28 18:20:02 -0800106 /*
107 * The root hub doesn't really have a TT, but Linux thinks it
108 * does because how could you have a "high speed hub" that
109 * directly talks directly to low speed devices without a TT?
110 * It's all lies. Lies, I tell you.
111 */
112 if (usb_urb->dev->tt->hub == root_hub)
113 return;
114
Paul Zimmerman399fdf92013-07-13 14:53:50 -0700115 if (qtd->urb->status != -EPIPE && qtd->urb->status != -EREMOTEIO) {
Paul Zimmerman7359d482013-03-11 17:47:59 -0700116 chan->qh->tt_buffer_dirty = 1;
117 if (usb_hub_clear_tt_buffer(usb_urb))
118 /* Clear failed; let's hope things work anyway */
119 chan->qh->tt_buffer_dirty = 0;
120 }
121}
122
123/*
124 * Handles the start-of-frame interrupt in host mode. Non-periodic
125 * transactions may be queued to the DWC_otg controller for the current
126 * (micro)frame. Periodic transactions may be queued to the controller
127 * for the next (micro)frame.
128 */
129static void dwc2_sof_intr(struct dwc2_hsotg *hsotg)
130{
131 struct list_head *qh_entry;
132 struct dwc2_qh *qh;
Paul Zimmerman7359d482013-03-11 17:47:59 -0700133 enum dwc2_transaction_type tr_type;
134
Douglas Anderson29539012015-11-20 09:06:28 -0800135 /* Clear interrupt */
136 dwc2_writel(GINTSTS_SOF, hsotg->regs + GINTSTS);
137
Paul Zimmerman7359d482013-03-11 17:47:59 -0700138#ifdef DEBUG_SOF
139 dev_vdbg(hsotg->dev, "--Start of Frame Interrupt--\n");
140#endif
141
Matthijs Kooijman37e1dcc2013-04-29 19:40:23 +0000142 hsotg->frame_number = dwc2_hcd_get_frame_number(hsotg);
Paul Zimmerman7359d482013-03-11 17:47:59 -0700143
144 dwc2_track_missed_sofs(hsotg);
145
146 /* Determine whether any periodic QHs should be executed */
147 qh_entry = hsotg->periodic_sched_inactive.next;
148 while (qh_entry != &hsotg->periodic_sched_inactive) {
149 qh = list_entry(qh_entry, struct dwc2_qh, qh_list_entry);
150 qh_entry = qh_entry->next;
Douglas Anderson74fc4a72016-01-28 18:19:58 -0800151 if (dwc2_frame_num_le(qh->sched_frame, hsotg->frame_number)) {
152 dwc2_sch_vdbg(hsotg, "QH=%p ready fn=%04x, sch=%04x\n",
153 qh, hsotg->frame_number, qh->sched_frame);
154
Paul Zimmerman7359d482013-03-11 17:47:59 -0700155 /*
156 * Move QH to the ready list to be executed next
157 * (micro)frame
158 */
Douglas Anderson94ef7ae2016-01-28 18:19:56 -0800159 list_move_tail(&qh->qh_list_entry,
Paul Zimmerman7359d482013-03-11 17:47:59 -0700160 &hsotg->periodic_sched_ready);
Douglas Anderson74fc4a72016-01-28 18:19:58 -0800161 }
Paul Zimmerman7359d482013-03-11 17:47:59 -0700162 }
163 tr_type = dwc2_hcd_select_transactions(hsotg);
164 if (tr_type != DWC2_TRANSACTION_NONE)
165 dwc2_hcd_queue_transactions(hsotg, tr_type);
Paul Zimmerman7359d482013-03-11 17:47:59 -0700166}
167
168/*
169 * Handles the Rx FIFO Level Interrupt, which indicates that there is
170 * at least one packet in the Rx FIFO. The packets are moved from the FIFO to
171 * memory if the DWC_otg controller is operating in Slave mode.
172 */
173static void dwc2_rx_fifo_level_intr(struct dwc2_hsotg *hsotg)
174{
175 u32 grxsts, chnum, bcnt, dpid, pktsts;
176 struct dwc2_host_chan *chan;
177
Matthijs Kooijmanb49977a2013-04-10 09:55:50 +0200178 if (dbg_perio())
179 dev_vdbg(hsotg->dev, "--RxFIFO Level Interrupt--\n");
Paul Zimmerman7359d482013-03-11 17:47:59 -0700180
Antti Seppälä95c8bc32015-08-20 21:41:07 +0300181 grxsts = dwc2_readl(hsotg->regs + GRXSTSP);
Matthijs Kooijmand6ec53e2013-08-30 18:45:15 +0200182 chnum = (grxsts & GRXSTS_HCHNUM_MASK) >> GRXSTS_HCHNUM_SHIFT;
Paul Zimmerman7359d482013-03-11 17:47:59 -0700183 chan = hsotg->hc_ptr_array[chnum];
184 if (!chan) {
185 dev_err(hsotg->dev, "Unable to get corresponding channel\n");
186 return;
187 }
188
Matthijs Kooijmand6ec53e2013-08-30 18:45:15 +0200189 bcnt = (grxsts & GRXSTS_BYTECNT_MASK) >> GRXSTS_BYTECNT_SHIFT;
190 dpid = (grxsts & GRXSTS_DPID_MASK) >> GRXSTS_DPID_SHIFT;
Matthijs Kooijmanf9234632013-08-30 18:45:13 +0200191 pktsts = (grxsts & GRXSTS_PKTSTS_MASK) >> GRXSTS_PKTSTS_SHIFT;
Paul Zimmerman7359d482013-03-11 17:47:59 -0700192
193 /* Packet Status */
Matthijs Kooijmanb49977a2013-04-10 09:55:50 +0200194 if (dbg_perio()) {
195 dev_vdbg(hsotg->dev, " Ch num = %d\n", chnum);
196 dev_vdbg(hsotg->dev, " Count = %d\n", bcnt);
197 dev_vdbg(hsotg->dev, " DPID = %d, chan.dpid = %d\n", dpid,
198 chan->data_pid_start);
Matthijs Kooijmanf9234632013-08-30 18:45:13 +0200199 dev_vdbg(hsotg->dev, " PStatus = %d\n", pktsts);
Matthijs Kooijmanb49977a2013-04-10 09:55:50 +0200200 }
Paul Zimmerman7359d482013-03-11 17:47:59 -0700201
202 switch (pktsts) {
203 case GRXSTS_PKTSTS_HCHIN:
204 /* Read the data into the host buffer */
205 if (bcnt > 0) {
206 dwc2_read_packet(hsotg, chan->xfer_buf, bcnt);
207
208 /* Update the HC fields for the next packet received */
209 chan->xfer_count += bcnt;
210 chan->xfer_buf += bcnt;
211 }
212 break;
213 case GRXSTS_PKTSTS_HCHIN_XFER_COMP:
214 case GRXSTS_PKTSTS_DATATOGGLEERR:
215 case GRXSTS_PKTSTS_HCHHALTED:
216 /* Handled in interrupt, just ignore data */
217 break;
218 default:
219 dev_err(hsotg->dev,
220 "RxFIFO Level Interrupt: Unknown status %d\n", pktsts);
221 break;
222 }
223}
224
225/*
226 * This interrupt occurs when the non-periodic Tx FIFO is half-empty. More
227 * data packets may be written to the FIFO for OUT transfers. More requests
228 * may be written to the non-periodic request queue for IN transfers. This
229 * interrupt is enabled only in Slave mode.
230 */
231static void dwc2_np_tx_fifo_empty_intr(struct dwc2_hsotg *hsotg)
232{
233 dev_vdbg(hsotg->dev, "--Non-Periodic TxFIFO Empty Interrupt--\n");
234 dwc2_hcd_queue_transactions(hsotg, DWC2_TRANSACTION_NON_PERIODIC);
235}
236
237/*
238 * This interrupt occurs when the periodic Tx FIFO is half-empty. More data
239 * packets may be written to the FIFO for OUT transfers. More requests may be
240 * written to the periodic request queue for IN transfers. This interrupt is
241 * enabled only in Slave mode.
242 */
243static void dwc2_perio_tx_fifo_empty_intr(struct dwc2_hsotg *hsotg)
244{
Matthijs Kooijmanb49977a2013-04-10 09:55:50 +0200245 if (dbg_perio())
246 dev_vdbg(hsotg->dev, "--Periodic TxFIFO Empty Interrupt--\n");
Paul Zimmerman7359d482013-03-11 17:47:59 -0700247 dwc2_hcd_queue_transactions(hsotg, DWC2_TRANSACTION_PERIODIC);
248}
249
250static void dwc2_hprt0_enable(struct dwc2_hsotg *hsotg, u32 hprt0,
251 u32 *hprt0_modify)
252{
253 struct dwc2_core_params *params = hsotg->core_params;
254 int do_reset = 0;
255 u32 usbcfg;
256 u32 prtspd;
257 u32 hcfg;
Matthijs Kooijmanbcc5def2013-04-29 19:42:00 +0000258 u32 fslspclksel;
Paul Zimmerman7359d482013-03-11 17:47:59 -0700259 u32 hfir;
260
261 dev_vdbg(hsotg->dev, "%s(%p)\n", __func__, hsotg);
262
263 /* Every time when port enables calculate HFIR.FrInterval */
Antti Seppälä95c8bc32015-08-20 21:41:07 +0300264 hfir = dwc2_readl(hsotg->regs + HFIR);
Paul Zimmerman7359d482013-03-11 17:47:59 -0700265 hfir &= ~HFIR_FRINT_MASK;
266 hfir |= dwc2_calc_frame_interval(hsotg) << HFIR_FRINT_SHIFT &
267 HFIR_FRINT_MASK;
Antti Seppälä95c8bc32015-08-20 21:41:07 +0300268 dwc2_writel(hfir, hsotg->regs + HFIR);
Paul Zimmerman7359d482013-03-11 17:47:59 -0700269
270 /* Check if we need to adjust the PHY clock speed for low power */
271 if (!params->host_support_fs_ls_low_power) {
272 /* Port has been enabled, set the reset change flag */
273 hsotg->flags.b.port_reset_change = 1;
274 return;
275 }
276
Antti Seppälä95c8bc32015-08-20 21:41:07 +0300277 usbcfg = dwc2_readl(hsotg->regs + GUSBCFG);
Matthijs Kooijmanf9234632013-08-30 18:45:13 +0200278 prtspd = (hprt0 & HPRT0_SPD_MASK) >> HPRT0_SPD_SHIFT;
Paul Zimmerman7359d482013-03-11 17:47:59 -0700279
280 if (prtspd == HPRT0_SPD_LOW_SPEED || prtspd == HPRT0_SPD_FULL_SPEED) {
281 /* Low power */
282 if (!(usbcfg & GUSBCFG_PHY_LP_CLK_SEL)) {
283 /* Set PHY low power clock select for FS/LS devices */
284 usbcfg |= GUSBCFG_PHY_LP_CLK_SEL;
Antti Seppälä95c8bc32015-08-20 21:41:07 +0300285 dwc2_writel(usbcfg, hsotg->regs + GUSBCFG);
Paul Zimmerman7359d482013-03-11 17:47:59 -0700286 do_reset = 1;
287 }
288
Antti Seppälä95c8bc32015-08-20 21:41:07 +0300289 hcfg = dwc2_readl(hsotg->regs + HCFG);
Matthijs Kooijmanf9234632013-08-30 18:45:13 +0200290 fslspclksel = (hcfg & HCFG_FSLSPCLKSEL_MASK) >>
291 HCFG_FSLSPCLKSEL_SHIFT;
Paul Zimmerman7359d482013-03-11 17:47:59 -0700292
293 if (prtspd == HPRT0_SPD_LOW_SPEED &&
294 params->host_ls_low_power_phy_clk ==
295 DWC2_HOST_LS_LOW_POWER_PHY_CLK_PARAM_6MHZ) {
296 /* 6 MHZ */
297 dev_vdbg(hsotg->dev,
298 "FS_PHY programming HCFG to 6 MHz\n");
Matthijs Kooijmanbcc5def2013-04-29 19:42:00 +0000299 if (fslspclksel != HCFG_FSLSPCLKSEL_6_MHZ) {
Matthijs Kooijmanf9234632013-08-30 18:45:13 +0200300 fslspclksel = HCFG_FSLSPCLKSEL_6_MHZ;
Paul Zimmerman7359d482013-03-11 17:47:59 -0700301 hcfg &= ~HCFG_FSLSPCLKSEL_MASK;
Matthijs Kooijmanf9234632013-08-30 18:45:13 +0200302 hcfg |= fslspclksel << HCFG_FSLSPCLKSEL_SHIFT;
Antti Seppälä95c8bc32015-08-20 21:41:07 +0300303 dwc2_writel(hcfg, hsotg->regs + HCFG);
Paul Zimmerman7359d482013-03-11 17:47:59 -0700304 do_reset = 1;
305 }
306 } else {
307 /* 48 MHZ */
308 dev_vdbg(hsotg->dev,
309 "FS_PHY programming HCFG to 48 MHz\n");
Matthijs Kooijmanbcc5def2013-04-29 19:42:00 +0000310 if (fslspclksel != HCFG_FSLSPCLKSEL_48_MHZ) {
Matthijs Kooijmanf9234632013-08-30 18:45:13 +0200311 fslspclksel = HCFG_FSLSPCLKSEL_48_MHZ;
Paul Zimmerman7359d482013-03-11 17:47:59 -0700312 hcfg &= ~HCFG_FSLSPCLKSEL_MASK;
Matthijs Kooijmanf9234632013-08-30 18:45:13 +0200313 hcfg |= fslspclksel << HCFG_FSLSPCLKSEL_SHIFT;
Antti Seppälä95c8bc32015-08-20 21:41:07 +0300314 dwc2_writel(hcfg, hsotg->regs + HCFG);
Paul Zimmerman7359d482013-03-11 17:47:59 -0700315 do_reset = 1;
316 }
317 }
318 } else {
319 /* Not low power */
320 if (usbcfg & GUSBCFG_PHY_LP_CLK_SEL) {
321 usbcfg &= ~GUSBCFG_PHY_LP_CLK_SEL;
Antti Seppälä95c8bc32015-08-20 21:41:07 +0300322 dwc2_writel(usbcfg, hsotg->regs + GUSBCFG);
Paul Zimmerman7359d482013-03-11 17:47:59 -0700323 do_reset = 1;
324 }
325 }
326
327 if (do_reset) {
328 *hprt0_modify |= HPRT0_RST;
Douglas Anderson29539012015-11-20 09:06:28 -0800329 dwc2_writel(*hprt0_modify, hsotg->regs + HPRT0);
Paul Zimmerman7359d482013-03-11 17:47:59 -0700330 queue_delayed_work(hsotg->wq_otg, &hsotg->reset_work,
331 msecs_to_jiffies(60));
332 } else {
333 /* Port has been enabled, set the reset change flag */
334 hsotg->flags.b.port_reset_change = 1;
335 }
336}
337
338/*
339 * There are multiple conditions that can cause a port interrupt. This function
340 * determines which interrupt conditions have occurred and handles them
341 * appropriately.
342 */
343static void dwc2_port_intr(struct dwc2_hsotg *hsotg)
344{
345 u32 hprt0;
346 u32 hprt0_modify;
347
348 dev_vdbg(hsotg->dev, "--Port Interrupt--\n");
349
Antti Seppälä95c8bc32015-08-20 21:41:07 +0300350 hprt0 = dwc2_readl(hsotg->regs + HPRT0);
Paul Zimmerman7359d482013-03-11 17:47:59 -0700351 hprt0_modify = hprt0;
352
353 /*
354 * Clear appropriate bits in HPRT0 to clear the interrupt bit in
355 * GINTSTS
356 */
357 hprt0_modify &= ~(HPRT0_ENA | HPRT0_CONNDET | HPRT0_ENACHG |
358 HPRT0_OVRCURRCHG);
359
360 /*
361 * Port Connect Detected
362 * Set flag and clear if detected
363 */
364 if (hprt0 & HPRT0_CONNDET) {
Douglas Anderson29539012015-11-20 09:06:28 -0800365 dwc2_writel(hprt0_modify | HPRT0_CONNDET, hsotg->regs + HPRT0);
366
Paul Zimmerman7359d482013-03-11 17:47:59 -0700367 dev_vdbg(hsotg->dev,
368 "--Port Interrupt HPRT0=0x%08x Port Connect Detected--\n",
369 hprt0);
Douglas Anderson6a659532015-11-19 13:23:14 -0800370 dwc2_hcd_connect(hsotg);
Paul Zimmerman7359d482013-03-11 17:47:59 -0700371
372 /*
373 * The Hub driver asserts a reset when it sees port connect
374 * status change flag
375 */
376 }
377
378 /*
379 * Port Enable Changed
380 * Clear if detected - Set internal flag if disabled
381 */
382 if (hprt0 & HPRT0_ENACHG) {
Douglas Anderson29539012015-11-20 09:06:28 -0800383 dwc2_writel(hprt0_modify | HPRT0_ENACHG, hsotg->regs + HPRT0);
Paul Zimmerman7359d482013-03-11 17:47:59 -0700384 dev_vdbg(hsotg->dev,
385 " --Port Interrupt HPRT0=0x%08x Port Enable Changed (now %d)--\n",
386 hprt0, !!(hprt0 & HPRT0_ENA));
Mian Yousaf Kaukabfbb9e222015-11-20 11:49:28 +0100387 if (hprt0 & HPRT0_ENA) {
388 hsotg->new_connection = true;
Paul Zimmerman7359d482013-03-11 17:47:59 -0700389 dwc2_hprt0_enable(hsotg, hprt0, &hprt0_modify);
Mian Yousaf Kaukabfbb9e222015-11-20 11:49:28 +0100390 } else {
Paul Zimmerman7359d482013-03-11 17:47:59 -0700391 hsotg->flags.b.port_enable_change = 1;
Mian Yousaf Kaukabfbb9e222015-11-20 11:49:28 +0100392 if (hsotg->core_params->dma_desc_fs_enable) {
393 u32 hcfg;
394
395 hsotg->core_params->dma_desc_enable = 0;
396 hsotg->new_connection = false;
397 hcfg = dwc2_readl(hsotg->regs + HCFG);
398 hcfg &= ~HCFG_DESCDMA;
399 dwc2_writel(hcfg, hsotg->regs + HCFG);
400 }
401 }
Paul Zimmerman7359d482013-03-11 17:47:59 -0700402 }
403
404 /* Overcurrent Change Interrupt */
405 if (hprt0 & HPRT0_OVRCURRCHG) {
Douglas Anderson29539012015-11-20 09:06:28 -0800406 dwc2_writel(hprt0_modify | HPRT0_OVRCURRCHG,
407 hsotg->regs + HPRT0);
Paul Zimmerman7359d482013-03-11 17:47:59 -0700408 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;
Paul Zimmerman7359d482013-03-11 17:47:59 -0700412 }
Paul Zimmerman7359d482013-03-11 17:47:59 -0700413}
414
415/*
416 * Gets the actual length of a transfer after the transfer halts. halt_status
417 * holds the reason for the halt.
418 *
419 * For IN transfers where halt_status is DWC2_HC_XFER_COMPLETE, *short_read
420 * is set to 1 upon return if less than the requested number of bytes were
421 * transferred. short_read may also be NULL on entry, in which case it remains
422 * unchanged.
423 */
424static u32 dwc2_get_actual_xfer_length(struct dwc2_hsotg *hsotg,
425 struct dwc2_host_chan *chan, int chnum,
426 struct dwc2_qtd *qtd,
427 enum dwc2_halt_status halt_status,
428 int *short_read)
429{
430 u32 hctsiz, count, length;
431
Antti Seppälä95c8bc32015-08-20 21:41:07 +0300432 hctsiz = dwc2_readl(hsotg->regs + HCTSIZ(chnum));
Paul Zimmerman7359d482013-03-11 17:47:59 -0700433
434 if (halt_status == DWC2_HC_XFER_COMPLETE) {
435 if (chan->ep_is_in) {
Matthijs Kooijmand6ec53e2013-08-30 18:45:15 +0200436 count = (hctsiz & TSIZ_XFERSIZE_MASK) >>
437 TSIZ_XFERSIZE_SHIFT;
Paul Zimmerman7359d482013-03-11 17:47:59 -0700438 length = chan->xfer_len - count;
439 if (short_read != NULL)
440 *short_read = (count != 0);
441 } else if (chan->qh->do_split) {
442 length = qtd->ssplit_out_xfer_count;
443 } else {
444 length = chan->xfer_len;
445 }
446 } else {
447 /*
448 * Must use the hctsiz.pktcnt field to determine how much data
449 * has been transferred. This field reflects the number of
450 * packets that have been transferred via the USB. This is
451 * always an integral number of packets if the transfer was
452 * halted before its normal completion. (Can't use the
453 * hctsiz.xfersize field because that reflects the number of
454 * bytes transferred via the AHB, not the USB).
455 */
Matthijs Kooijmand6ec53e2013-08-30 18:45:15 +0200456 count = (hctsiz & TSIZ_PKTCNT_MASK) >> TSIZ_PKTCNT_SHIFT;
Paul Zimmerman7359d482013-03-11 17:47:59 -0700457 length = (chan->start_pkt_count - count) * chan->max_packet;
458 }
459
460 return length;
461}
462
463/**
464 * dwc2_update_urb_state() - Updates the state of the URB after a Transfer
465 * Complete interrupt on the host channel. Updates the actual_length field
466 * of the URB based on the number of bytes transferred via the host channel.
467 * Sets the URB status if the data transfer is finished.
468 *
469 * Return: 1 if the data transfer specified by the URB is completely finished,
470 * 0 otherwise
471 */
472static int dwc2_update_urb_state(struct dwc2_hsotg *hsotg,
473 struct dwc2_host_chan *chan, int chnum,
474 struct dwc2_hcd_urb *urb,
475 struct dwc2_qtd *qtd)
476{
477 u32 hctsiz;
478 int xfer_done = 0;
479 int short_read = 0;
480 int xfer_length = dwc2_get_actual_xfer_length(hsotg, chan, chnum, qtd,
481 DWC2_HC_XFER_COMPLETE,
482 &short_read);
483
484 if (urb->actual_length + xfer_length > urb->length) {
485 dev_warn(hsotg->dev, "%s(): trimming xfer length\n", __func__);
486 xfer_length = urb->length - urb->actual_length;
487 }
488
Paul Zimmerman7359d482013-03-11 17:47:59 -0700489 dev_vdbg(hsotg->dev, "urb->actual_length=%d xfer_length=%d\n",
490 urb->actual_length, xfer_length);
491 urb->actual_length += xfer_length;
492
493 if (xfer_length && chan->ep_type == USB_ENDPOINT_XFER_BULK &&
494 (urb->flags & URB_SEND_ZERO_PACKET) &&
495 urb->actual_length >= urb->length &&
496 !(urb->length % chan->max_packet)) {
497 xfer_done = 0;
498 } else if (short_read || urb->actual_length >= urb->length) {
499 xfer_done = 1;
500 urb->status = 0;
501 }
502
Antti Seppälä95c8bc32015-08-20 21:41:07 +0300503 hctsiz = dwc2_readl(hsotg->regs + HCTSIZ(chnum));
Paul Zimmerman7359d482013-03-11 17:47:59 -0700504 dev_vdbg(hsotg->dev, "DWC_otg: %s: %s, channel %d\n",
505 __func__, (chan->ep_is_in ? "IN" : "OUT"), chnum);
506 dev_vdbg(hsotg->dev, " chan->xfer_len %d\n", chan->xfer_len);
507 dev_vdbg(hsotg->dev, " hctsiz.xfersize %d\n",
Matthijs Kooijmand6ec53e2013-08-30 18:45:15 +0200508 (hctsiz & TSIZ_XFERSIZE_MASK) >> TSIZ_XFERSIZE_SHIFT);
Paul Zimmerman7359d482013-03-11 17:47:59 -0700509 dev_vdbg(hsotg->dev, " urb->transfer_buffer_length %d\n", urb->length);
510 dev_vdbg(hsotg->dev, " urb->actual_length %d\n", urb->actual_length);
511 dev_vdbg(hsotg->dev, " short_read %d, xfer_done %d\n", short_read,
512 xfer_done);
513
514 return xfer_done;
515}
516
517/*
518 * Save the starting data toggle for the next transfer. The data toggle is
519 * saved in the QH for non-control transfers and it's saved in the QTD for
520 * control transfers.
521 */
522void dwc2_hcd_save_data_toggle(struct dwc2_hsotg *hsotg,
523 struct dwc2_host_chan *chan, int chnum,
524 struct dwc2_qtd *qtd)
525{
Antti Seppälä95c8bc32015-08-20 21:41:07 +0300526 u32 hctsiz = dwc2_readl(hsotg->regs + HCTSIZ(chnum));
Matthijs Kooijmanf9234632013-08-30 18:45:13 +0200527 u32 pid = (hctsiz & TSIZ_SC_MC_PID_MASK) >> TSIZ_SC_MC_PID_SHIFT;
Paul Zimmerman7359d482013-03-11 17:47:59 -0700528
529 if (chan->ep_type != USB_ENDPOINT_XFER_CONTROL) {
Tang, Jianqiang62943b72016-02-16 15:02:07 -0800530 if (WARN(!chan || !chan->qh,
531 "chan->qh must be specified for non-control eps\n"))
532 return;
533
Paul Zimmerman7359d482013-03-11 17:47:59 -0700534 if (pid == TSIZ_SC_MC_PID_DATA0)
535 chan->qh->data_toggle = DWC2_HC_PID_DATA0;
536 else
537 chan->qh->data_toggle = DWC2_HC_PID_DATA1;
538 } else {
Tang, Jianqiang62943b72016-02-16 15:02:07 -0800539 if (WARN(!qtd,
540 "qtd must be specified for control eps\n"))
541 return;
542
Paul Zimmerman7359d482013-03-11 17:47:59 -0700543 if (pid == TSIZ_SC_MC_PID_DATA0)
544 qtd->data_toggle = DWC2_HC_PID_DATA0;
545 else
546 qtd->data_toggle = DWC2_HC_PID_DATA1;
547 }
548}
549
550/**
551 * dwc2_update_isoc_urb_state() - Updates the state of an Isochronous URB when
552 * the transfer is stopped for any reason. The fields of the current entry in
553 * the frame descriptor array are set based on the transfer state and the input
554 * halt_status. Completes the Isochronous URB if all the URB frames have been
555 * completed.
556 *
557 * Return: DWC2_HC_XFER_COMPLETE if there are more frames remaining to be
558 * transferred in the URB. Otherwise return DWC2_HC_XFER_URB_COMPLETE.
559 */
560static enum dwc2_halt_status dwc2_update_isoc_urb_state(
561 struct dwc2_hsotg *hsotg, struct dwc2_host_chan *chan,
562 int chnum, struct dwc2_qtd *qtd,
563 enum dwc2_halt_status halt_status)
564{
565 struct dwc2_hcd_iso_packet_desc *frame_desc;
566 struct dwc2_hcd_urb *urb = qtd->urb;
567
568 if (!urb)
569 return DWC2_HC_XFER_NO_HALT_STATUS;
570
571 frame_desc = &urb->iso_descs[qtd->isoc_frame_index];
572
573 switch (halt_status) {
574 case DWC2_HC_XFER_COMPLETE:
575 frame_desc->status = 0;
576 frame_desc->actual_length = dwc2_get_actual_xfer_length(hsotg,
577 chan, chnum, qtd, halt_status, NULL);
Paul Zimmerman7359d482013-03-11 17:47:59 -0700578 break;
579 case DWC2_HC_XFER_FRAME_OVERRUN:
580 urb->error_count++;
581 if (chan->ep_is_in)
582 frame_desc->status = -ENOSR;
583 else
584 frame_desc->status = -ECOMM;
585 frame_desc->actual_length = 0;
586 break;
587 case DWC2_HC_XFER_BABBLE_ERR:
588 urb->error_count++;
589 frame_desc->status = -EOVERFLOW;
590 /* Don't need to update actual_length in this case */
591 break;
592 case DWC2_HC_XFER_XACT_ERR:
593 urb->error_count++;
594 frame_desc->status = -EPROTO;
595 frame_desc->actual_length = dwc2_get_actual_xfer_length(hsotg,
596 chan, chnum, qtd, halt_status, NULL);
597
Paul Zimmerman7359d482013-03-11 17:47:59 -0700598 /* Skip whole frame */
599 if (chan->qh->do_split &&
600 chan->ep_type == USB_ENDPOINT_XFER_ISOC && chan->ep_is_in &&
601 hsotg->core_params->dma_enable > 0) {
602 qtd->complete_split = 0;
603 qtd->isoc_split_offset = 0;
604 }
605
606 break;
607 default:
608 dev_err(hsotg->dev, "Unhandled halt_status (%d)\n",
609 halt_status);
610 break;
611 }
612
613 if (++qtd->isoc_frame_index == urb->packet_count) {
614 /*
615 * urb->status is not used for isoc transfers. The individual
616 * frame_desc statuses are used instead.
617 */
Paul Zimmerman0d012b92013-07-13 14:53:48 -0700618 dwc2_host_complete(hsotg, qtd, 0);
Paul Zimmerman7359d482013-03-11 17:47:59 -0700619 halt_status = DWC2_HC_XFER_URB_COMPLETE;
620 } else {
621 halt_status = DWC2_HC_XFER_COMPLETE;
622 }
623
624 return halt_status;
625}
626
627/*
628 * Frees the first QTD in the QH's list if free_qtd is 1. For non-periodic
629 * QHs, removes the QH from the active non-periodic schedule. If any QTDs are
630 * still linked to the QH, the QH is added to the end of the inactive
631 * non-periodic schedule. For periodic QHs, removes the QH from the periodic
632 * schedule if no more QTDs are linked to the QH.
633 */
634static void dwc2_deactivate_qh(struct dwc2_hsotg *hsotg, struct dwc2_qh *qh,
635 int free_qtd)
636{
637 int continue_split = 0;
638 struct dwc2_qtd *qtd;
639
Matthijs Kooijmanb49977a2013-04-10 09:55:50 +0200640 if (dbg_qh(qh))
641 dev_vdbg(hsotg->dev, " %s(%p,%p,%d)\n", __func__,
642 hsotg, qh, free_qtd);
Paul Zimmerman7359d482013-03-11 17:47:59 -0700643
644 if (list_empty(&qh->qtd_list)) {
645 dev_dbg(hsotg->dev, "## QTD list empty ##\n");
646 goto no_qtd;
647 }
648
649 qtd = list_first_entry(&qh->qtd_list, struct dwc2_qtd, qtd_list_entry);
650
651 if (qtd->complete_split)
652 continue_split = 1;
653 else if (qtd->isoc_split_pos == DWC2_HCSPLT_XACTPOS_MID ||
654 qtd->isoc_split_pos == DWC2_HCSPLT_XACTPOS_END)
655 continue_split = 1;
656
657 if (free_qtd) {
658 dwc2_hcd_qtd_unlink_and_free(hsotg, qtd, qh);
659 continue_split = 0;
660 }
661
662no_qtd:
Paul Zimmerman7359d482013-03-11 17:47:59 -0700663 qh->channel = NULL;
664 dwc2_hcd_qh_deactivate(hsotg, qh, continue_split);
665}
666
667/**
668 * dwc2_release_channel() - Releases a host channel for use by other transfers
669 *
670 * @hsotg: The HCD state structure
671 * @chan: The host channel to release
672 * @qtd: The QTD associated with the host channel. This QTD may be
673 * freed if the transfer is complete or an error has occurred.
674 * @halt_status: Reason the channel is being released. This status
675 * determines the actions taken by this function.
676 *
677 * Also attempts to select and queue more transactions since at least one host
678 * channel is available.
679 */
680static void dwc2_release_channel(struct dwc2_hsotg *hsotg,
681 struct dwc2_host_chan *chan,
682 struct dwc2_qtd *qtd,
683 enum dwc2_halt_status halt_status)
684{
685 enum dwc2_transaction_type tr_type;
686 u32 haintmsk;
687 int free_qtd = 0;
688
Matthijs Kooijmanb49977a2013-04-10 09:55:50 +0200689 if (dbg_hc(chan))
690 dev_vdbg(hsotg->dev, " %s: channel %d, halt_status %d\n",
691 __func__, chan->hc_num, halt_status);
Paul Zimmerman7359d482013-03-11 17:47:59 -0700692
693 switch (halt_status) {
694 case DWC2_HC_XFER_URB_COMPLETE:
695 free_qtd = 1;
696 break;
697 case DWC2_HC_XFER_AHB_ERR:
698 case DWC2_HC_XFER_STALL:
699 case DWC2_HC_XFER_BABBLE_ERR:
700 free_qtd = 1;
701 break;
702 case DWC2_HC_XFER_XACT_ERR:
Matthijs Kooijman8509f2f2013-03-25 12:00:25 -0700703 if (qtd && qtd->error_count >= 3) {
Paul Zimmerman7359d482013-03-11 17:47:59 -0700704 dev_vdbg(hsotg->dev,
705 " Complete URB with transaction error\n");
706 free_qtd = 1;
Paul Zimmerman0d012b92013-07-13 14:53:48 -0700707 dwc2_host_complete(hsotg, qtd, -EPROTO);
Paul Zimmerman7359d482013-03-11 17:47:59 -0700708 }
709 break;
710 case DWC2_HC_XFER_URB_DEQUEUE:
711 /*
712 * The QTD has already been removed and the QH has been
713 * deactivated. Don't want to do anything except release the
714 * host channel and try to queue more transfers.
715 */
716 goto cleanup;
717 case DWC2_HC_XFER_PERIODIC_INCOMPLETE:
718 dev_vdbg(hsotg->dev, " Complete URB with I/O error\n");
719 free_qtd = 1;
Paul Zimmerman0d012b92013-07-13 14:53:48 -0700720 dwc2_host_complete(hsotg, qtd, -EIO);
Paul Zimmerman7359d482013-03-11 17:47:59 -0700721 break;
722 case DWC2_HC_XFER_NO_HALT_STATUS:
723 default:
724 break;
725 }
726
727 dwc2_deactivate_qh(hsotg, chan->qh, free_qtd);
728
729cleanup:
730 /*
731 * Release the host channel for use by other transfers. The cleanup
732 * function clears the channel interrupt enables and conditions, so
733 * there's no need to clear the Channel Halted interrupt separately.
734 */
735 if (!list_empty(&chan->hc_list_entry))
736 list_del(&chan->hc_list_entry);
737 dwc2_hc_cleanup(hsotg, chan);
738 list_add_tail(&chan->hc_list_entry, &hsotg->free_hc_list);
739
Dom Cobley20f2eb92013-09-23 14:23:34 -0700740 if (hsotg->core_params->uframe_sched > 0) {
741 hsotg->available_host_channels++;
742 } else {
743 switch (chan->ep_type) {
744 case USB_ENDPOINT_XFER_CONTROL:
745 case USB_ENDPOINT_XFER_BULK:
746 hsotg->non_periodic_channels--;
747 break;
748 default:
749 /*
750 * Don't release reservations for periodic channels
751 * here. That's done when a periodic transfer is
752 * descheduled (i.e. when the QH is removed from the
753 * periodic schedule).
754 */
755 break;
756 }
Paul Zimmerman7359d482013-03-11 17:47:59 -0700757 }
758
Antti Seppälä95c8bc32015-08-20 21:41:07 +0300759 haintmsk = dwc2_readl(hsotg->regs + HAINTMSK);
Paul Zimmerman7359d482013-03-11 17:47:59 -0700760 haintmsk &= ~(1 << chan->hc_num);
Antti Seppälä95c8bc32015-08-20 21:41:07 +0300761 dwc2_writel(haintmsk, hsotg->regs + HAINTMSK);
Paul Zimmerman7359d482013-03-11 17:47:59 -0700762
763 /* Try to queue more transfers now that there's a free channel */
764 tr_type = dwc2_hcd_select_transactions(hsotg);
765 if (tr_type != DWC2_TRANSACTION_NONE)
766 dwc2_hcd_queue_transactions(hsotg, tr_type);
767}
768
769/*
770 * Halts a host channel. If the channel cannot be halted immediately because
771 * the request queue is full, this function ensures that the FIFO empty
772 * interrupt for the appropriate queue is enabled so that the halt request can
773 * be queued when there is space in the request queue.
774 *
775 * This function may also be called in DMA mode. In that case, the channel is
776 * simply released since the core always halts the channel automatically in
777 * DMA mode.
778 */
779static void dwc2_halt_channel(struct dwc2_hsotg *hsotg,
780 struct dwc2_host_chan *chan, struct dwc2_qtd *qtd,
781 enum dwc2_halt_status halt_status)
782{
Matthijs Kooijmanb49977a2013-04-10 09:55:50 +0200783 if (dbg_hc(chan))
784 dev_vdbg(hsotg->dev, "%s()\n", __func__);
Paul Zimmerman7359d482013-03-11 17:47:59 -0700785
786 if (hsotg->core_params->dma_enable > 0) {
Matthijs Kooijmanb49977a2013-04-10 09:55:50 +0200787 if (dbg_hc(chan))
788 dev_vdbg(hsotg->dev, "DMA enabled\n");
Paul Zimmerman7359d482013-03-11 17:47:59 -0700789 dwc2_release_channel(hsotg, chan, qtd, halt_status);
790 return;
791 }
792
793 /* Slave mode processing */
794 dwc2_hc_halt(hsotg, chan, halt_status);
795
796 if (chan->halt_on_queue) {
797 u32 gintmsk;
798
799 dev_vdbg(hsotg->dev, "Halt on queue\n");
800 if (chan->ep_type == USB_ENDPOINT_XFER_CONTROL ||
801 chan->ep_type == USB_ENDPOINT_XFER_BULK) {
802 dev_vdbg(hsotg->dev, "control/bulk\n");
803 /*
804 * Make sure the Non-periodic Tx FIFO empty interrupt
805 * is enabled so that the non-periodic schedule will
806 * be processed
807 */
Antti Seppälä95c8bc32015-08-20 21:41:07 +0300808 gintmsk = dwc2_readl(hsotg->regs + GINTMSK);
Paul Zimmerman7359d482013-03-11 17:47:59 -0700809 gintmsk |= GINTSTS_NPTXFEMP;
Antti Seppälä95c8bc32015-08-20 21:41:07 +0300810 dwc2_writel(gintmsk, hsotg->regs + GINTMSK);
Paul Zimmerman7359d482013-03-11 17:47:59 -0700811 } else {
812 dev_vdbg(hsotg->dev, "isoc/intr\n");
813 /*
814 * Move the QH from the periodic queued schedule to
815 * the periodic assigned schedule. This allows the
816 * halt to be queued when the periodic schedule is
817 * processed.
818 */
Douglas Anderson94ef7ae2016-01-28 18:19:56 -0800819 list_move_tail(&chan->qh->qh_list_entry,
Paul Zimmerman7359d482013-03-11 17:47:59 -0700820 &hsotg->periodic_sched_assigned);
821
822 /*
823 * Make sure the Periodic Tx FIFO Empty interrupt is
824 * enabled so that the periodic schedule will be
825 * processed
826 */
Antti Seppälä95c8bc32015-08-20 21:41:07 +0300827 gintmsk = dwc2_readl(hsotg->regs + GINTMSK);
Paul Zimmerman7359d482013-03-11 17:47:59 -0700828 gintmsk |= GINTSTS_PTXFEMP;
Antti Seppälä95c8bc32015-08-20 21:41:07 +0300829 dwc2_writel(gintmsk, hsotg->regs + GINTMSK);
Paul Zimmerman7359d482013-03-11 17:47:59 -0700830 }
831 }
832}
833
834/*
835 * Performs common cleanup for non-periodic transfers after a Transfer
836 * Complete interrupt. This function should be called after any endpoint type
837 * specific handling is finished to release the host channel.
838 */
839static void dwc2_complete_non_periodic_xfer(struct dwc2_hsotg *hsotg,
840 struct dwc2_host_chan *chan,
841 int chnum, struct dwc2_qtd *qtd,
842 enum dwc2_halt_status halt_status)
843{
844 dev_vdbg(hsotg->dev, "%s()\n", __func__);
845
846 qtd->error_count = 0;
847
848 if (chan->hcint & HCINTMSK_NYET) {
849 /*
850 * Got a NYET on the last transaction of the transfer. This
851 * means that the endpoint should be in the PING state at the
852 * beginning of the next transfer.
853 */
854 dev_vdbg(hsotg->dev, "got NYET\n");
855 chan->qh->ping_state = 1;
856 }
857
858 /*
859 * Always halt and release the host channel to make it available for
860 * more transfers. There may still be more phases for a control
861 * transfer or more data packets for a bulk transfer at this point,
862 * but the host channel is still halted. A channel will be reassigned
863 * to the transfer when the non-periodic schedule is processed after
864 * the channel is released. This allows transactions to be queued
865 * properly via dwc2_hcd_queue_transactions, which also enables the
866 * Tx FIFO Empty interrupt if necessary.
867 */
868 if (chan->ep_is_in) {
869 /*
870 * IN transfers in Slave mode require an explicit disable to
871 * halt the channel. (In DMA mode, this call simply releases
872 * the channel.)
873 */
874 dwc2_halt_channel(hsotg, chan, qtd, halt_status);
875 } else {
876 /*
877 * The channel is automatically disabled by the core for OUT
878 * transfers in Slave mode
879 */
880 dwc2_release_channel(hsotg, chan, qtd, halt_status);
881 }
882}
883
884/*
885 * Performs common cleanup for periodic transfers after a Transfer Complete
886 * interrupt. This function should be called after any endpoint type specific
887 * handling is finished to release the host channel.
888 */
889static void dwc2_complete_periodic_xfer(struct dwc2_hsotg *hsotg,
890 struct dwc2_host_chan *chan, int chnum,
891 struct dwc2_qtd *qtd,
892 enum dwc2_halt_status halt_status)
893{
Antti Seppälä95c8bc32015-08-20 21:41:07 +0300894 u32 hctsiz = dwc2_readl(hsotg->regs + HCTSIZ(chnum));
Paul Zimmerman7359d482013-03-11 17:47:59 -0700895
896 qtd->error_count = 0;
897
898 if (!chan->ep_is_in || (hctsiz & TSIZ_PKTCNT_MASK) == 0)
899 /* Core halts channel in these cases */
900 dwc2_release_channel(hsotg, chan, qtd, halt_status);
901 else
902 /* Flush any outstanding requests from the Tx queue */
903 dwc2_halt_channel(hsotg, chan, qtd, halt_status);
904}
905
906static int dwc2_xfercomp_isoc_split_in(struct dwc2_hsotg *hsotg,
907 struct dwc2_host_chan *chan, int chnum,
908 struct dwc2_qtd *qtd)
909{
910 struct dwc2_hcd_iso_packet_desc *frame_desc;
911 u32 len;
912
913 if (!qtd->urb)
914 return 0;
915
916 frame_desc = &qtd->urb->iso_descs[qtd->isoc_frame_index];
917 len = dwc2_get_actual_xfer_length(hsotg, chan, chnum, qtd,
918 DWC2_HC_XFER_COMPLETE, NULL);
919 if (!len) {
920 qtd->complete_split = 0;
921 qtd->isoc_split_offset = 0;
922 return 0;
923 }
924
925 frame_desc->actual_length += len;
926
Paul Zimmerman7359d482013-03-11 17:47:59 -0700927 qtd->isoc_split_offset += len;
928
929 if (frame_desc->actual_length >= frame_desc->length) {
930 frame_desc->status = 0;
931 qtd->isoc_frame_index++;
932 qtd->complete_split = 0;
933 qtd->isoc_split_offset = 0;
934 }
935
936 if (qtd->isoc_frame_index == qtd->urb->packet_count) {
Paul Zimmerman0d012b92013-07-13 14:53:48 -0700937 dwc2_host_complete(hsotg, qtd, 0);
Paul Zimmerman7359d482013-03-11 17:47:59 -0700938 dwc2_release_channel(hsotg, chan, qtd,
939 DWC2_HC_XFER_URB_COMPLETE);
940 } else {
941 dwc2_release_channel(hsotg, chan, qtd,
942 DWC2_HC_XFER_NO_HALT_STATUS);
943 }
944
945 return 1; /* Indicates that channel released */
946}
947
948/*
949 * Handles a host channel Transfer Complete interrupt. This handler may be
950 * called in either DMA mode or Slave mode.
951 */
952static void dwc2_hc_xfercomp_intr(struct dwc2_hsotg *hsotg,
953 struct dwc2_host_chan *chan, int chnum,
954 struct dwc2_qtd *qtd)
955{
956 struct dwc2_hcd_urb *urb = qtd->urb;
Paul Zimmerman7359d482013-03-11 17:47:59 -0700957 enum dwc2_halt_status halt_status = DWC2_HC_XFER_COMPLETE;
Paul Zimmerman2b54fa62014-02-12 17:44:35 -0800958 int pipe_type;
Paul Zimmerman7359d482013-03-11 17:47:59 -0700959 int urb_xfer_done;
960
Matthijs Kooijmanb49977a2013-04-10 09:55:50 +0200961 if (dbg_hc(chan))
962 dev_vdbg(hsotg->dev,
963 "--Host Channel %d Interrupt: Transfer Complete--\n",
964 chnum);
Paul Zimmerman7359d482013-03-11 17:47:59 -0700965
Paul Zimmerman2b54fa62014-02-12 17:44:35 -0800966 if (!urb)
967 goto handle_xfercomp_done;
968
969 pipe_type = dwc2_hcd_get_pipe_type(&urb->pipe_info);
970
Paul Zimmerman7359d482013-03-11 17:47:59 -0700971 if (hsotg->core_params->dma_desc_enable > 0) {
972 dwc2_hcd_complete_xfer_ddma(hsotg, chan, chnum, halt_status);
973 if (pipe_type == USB_ENDPOINT_XFER_ISOC)
974 /* Do not disable the interrupt, just clear it */
975 return;
976 goto handle_xfercomp_done;
977 }
978
979 /* Handle xfer complete on CSPLIT */
980 if (chan->qh->do_split) {
981 if (chan->ep_type == USB_ENDPOINT_XFER_ISOC && chan->ep_is_in &&
982 hsotg->core_params->dma_enable > 0) {
983 if (qtd->complete_split &&
984 dwc2_xfercomp_isoc_split_in(hsotg, chan, chnum,
985 qtd))
986 goto handle_xfercomp_done;
987 } else {
988 qtd->complete_split = 0;
989 }
990 }
991
Paul Zimmerman7359d482013-03-11 17:47:59 -0700992 /* Update the QTD and URB states */
993 switch (pipe_type) {
994 case USB_ENDPOINT_XFER_CONTROL:
995 switch (qtd->control_phase) {
996 case DWC2_CONTROL_SETUP:
997 if (urb->length > 0)
998 qtd->control_phase = DWC2_CONTROL_DATA;
999 else
1000 qtd->control_phase = DWC2_CONTROL_STATUS;
1001 dev_vdbg(hsotg->dev,
1002 " Control setup transaction done\n");
1003 halt_status = DWC2_HC_XFER_COMPLETE;
1004 break;
1005 case DWC2_CONTROL_DATA:
1006 urb_xfer_done = dwc2_update_urb_state(hsotg, chan,
1007 chnum, urb, qtd);
1008 if (urb_xfer_done) {
1009 qtd->control_phase = DWC2_CONTROL_STATUS;
1010 dev_vdbg(hsotg->dev,
1011 " Control data transfer done\n");
1012 } else {
1013 dwc2_hcd_save_data_toggle(hsotg, chan, chnum,
1014 qtd);
1015 }
1016 halt_status = DWC2_HC_XFER_COMPLETE;
1017 break;
1018 case DWC2_CONTROL_STATUS:
1019 dev_vdbg(hsotg->dev, " Control transfer complete\n");
1020 if (urb->status == -EINPROGRESS)
1021 urb->status = 0;
Paul Zimmerman0d012b92013-07-13 14:53:48 -07001022 dwc2_host_complete(hsotg, qtd, urb->status);
Paul Zimmerman7359d482013-03-11 17:47:59 -07001023 halt_status = DWC2_HC_XFER_URB_COMPLETE;
1024 break;
1025 }
1026
1027 dwc2_complete_non_periodic_xfer(hsotg, chan, chnum, qtd,
1028 halt_status);
1029 break;
1030 case USB_ENDPOINT_XFER_BULK:
1031 dev_vdbg(hsotg->dev, " Bulk transfer complete\n");
1032 urb_xfer_done = dwc2_update_urb_state(hsotg, chan, chnum, urb,
1033 qtd);
1034 if (urb_xfer_done) {
Paul Zimmerman0d012b92013-07-13 14:53:48 -07001035 dwc2_host_complete(hsotg, qtd, urb->status);
Paul Zimmerman7359d482013-03-11 17:47:59 -07001036 halt_status = DWC2_HC_XFER_URB_COMPLETE;
1037 } else {
1038 halt_status = DWC2_HC_XFER_COMPLETE;
1039 }
1040
1041 dwc2_hcd_save_data_toggle(hsotg, chan, chnum, qtd);
1042 dwc2_complete_non_periodic_xfer(hsotg, chan, chnum, qtd,
1043 halt_status);
1044 break;
1045 case USB_ENDPOINT_XFER_INT:
1046 dev_vdbg(hsotg->dev, " Interrupt transfer complete\n");
1047 urb_xfer_done = dwc2_update_urb_state(hsotg, chan, chnum, urb,
1048 qtd);
1049
1050 /*
1051 * Interrupt URB is done on the first transfer complete
1052 * interrupt
1053 */
1054 if (urb_xfer_done) {
Paul Zimmerman0d012b92013-07-13 14:53:48 -07001055 dwc2_host_complete(hsotg, qtd, urb->status);
1056 halt_status = DWC2_HC_XFER_URB_COMPLETE;
Paul Zimmerman7359d482013-03-11 17:47:59 -07001057 } else {
Paul Zimmerman0d012b92013-07-13 14:53:48 -07001058 halt_status = DWC2_HC_XFER_COMPLETE;
Paul Zimmerman7359d482013-03-11 17:47:59 -07001059 }
1060
1061 dwc2_hcd_save_data_toggle(hsotg, chan, chnum, qtd);
1062 dwc2_complete_periodic_xfer(hsotg, chan, chnum, qtd,
1063 halt_status);
1064 break;
1065 case USB_ENDPOINT_XFER_ISOC:
Matthijs Kooijmanb49977a2013-04-10 09:55:50 +02001066 if (dbg_perio())
1067 dev_vdbg(hsotg->dev, " Isochronous transfer complete\n");
Paul Zimmerman7359d482013-03-11 17:47:59 -07001068 if (qtd->isoc_split_pos == DWC2_HCSPLT_XACTPOS_ALL)
1069 halt_status = dwc2_update_isoc_urb_state(hsotg, chan,
1070 chnum, qtd, DWC2_HC_XFER_COMPLETE);
1071 dwc2_complete_periodic_xfer(hsotg, chan, chnum, qtd,
1072 halt_status);
1073 break;
1074 }
1075
1076handle_xfercomp_done:
1077 disable_hc_int(hsotg, chnum, HCINTMSK_XFERCOMPL);
1078}
1079
1080/*
1081 * Handles a host channel STALL interrupt. This handler may be called in
1082 * either DMA mode or Slave mode.
1083 */
1084static void dwc2_hc_stall_intr(struct dwc2_hsotg *hsotg,
1085 struct dwc2_host_chan *chan, int chnum,
1086 struct dwc2_qtd *qtd)
1087{
1088 struct dwc2_hcd_urb *urb = qtd->urb;
Paul Zimmerman2b54fa62014-02-12 17:44:35 -08001089 int pipe_type;
Paul Zimmerman7359d482013-03-11 17:47:59 -07001090
1091 dev_dbg(hsotg->dev, "--Host Channel %d Interrupt: STALL Received--\n",
1092 chnum);
1093
1094 if (hsotg->core_params->dma_desc_enable > 0) {
1095 dwc2_hcd_complete_xfer_ddma(hsotg, chan, chnum,
1096 DWC2_HC_XFER_STALL);
1097 goto handle_stall_done;
1098 }
1099
1100 if (!urb)
1101 goto handle_stall_halt;
1102
Paul Zimmerman2b54fa62014-02-12 17:44:35 -08001103 pipe_type = dwc2_hcd_get_pipe_type(&urb->pipe_info);
1104
Paul Zimmerman7359d482013-03-11 17:47:59 -07001105 if (pipe_type == USB_ENDPOINT_XFER_CONTROL)
Paul Zimmerman0d012b92013-07-13 14:53:48 -07001106 dwc2_host_complete(hsotg, qtd, -EPIPE);
Paul Zimmerman7359d482013-03-11 17:47:59 -07001107
1108 if (pipe_type == USB_ENDPOINT_XFER_BULK ||
1109 pipe_type == USB_ENDPOINT_XFER_INT) {
Paul Zimmerman0d012b92013-07-13 14:53:48 -07001110 dwc2_host_complete(hsotg, qtd, -EPIPE);
Paul Zimmerman7359d482013-03-11 17:47:59 -07001111 /*
1112 * USB protocol requires resetting the data toggle for bulk
1113 * and interrupt endpoints when a CLEAR_FEATURE(ENDPOINT_HALT)
1114 * setup command is issued to the endpoint. Anticipate the
1115 * CLEAR_FEATURE command since a STALL has occurred and reset
1116 * the data toggle now.
1117 */
1118 chan->qh->data_toggle = 0;
1119 }
1120
1121handle_stall_halt:
1122 dwc2_halt_channel(hsotg, chan, qtd, DWC2_HC_XFER_STALL);
1123
1124handle_stall_done:
1125 disable_hc_int(hsotg, chnum, HCINTMSK_STALL);
1126}
1127
1128/*
1129 * Updates the state of the URB when a transfer has been stopped due to an
1130 * abnormal condition before the transfer completes. Modifies the
1131 * actual_length field of the URB to reflect the number of bytes that have
1132 * actually been transferred via the host channel.
1133 */
1134static void dwc2_update_urb_state_abn(struct dwc2_hsotg *hsotg,
1135 struct dwc2_host_chan *chan, int chnum,
1136 struct dwc2_hcd_urb *urb,
1137 struct dwc2_qtd *qtd,
1138 enum dwc2_halt_status halt_status)
1139{
1140 u32 xfer_length = dwc2_get_actual_xfer_length(hsotg, chan, chnum,
1141 qtd, halt_status, NULL);
1142 u32 hctsiz;
1143
1144 if (urb->actual_length + xfer_length > urb->length) {
1145 dev_warn(hsotg->dev, "%s(): trimming xfer length\n", __func__);
1146 xfer_length = urb->length - urb->actual_length;
1147 }
1148
Paul Zimmerman7359d482013-03-11 17:47:59 -07001149 urb->actual_length += xfer_length;
1150
Antti Seppälä95c8bc32015-08-20 21:41:07 +03001151 hctsiz = dwc2_readl(hsotg->regs + HCTSIZ(chnum));
Paul Zimmerman7359d482013-03-11 17:47:59 -07001152 dev_vdbg(hsotg->dev, "DWC_otg: %s: %s, channel %d\n",
1153 __func__, (chan->ep_is_in ? "IN" : "OUT"), chnum);
1154 dev_vdbg(hsotg->dev, " chan->start_pkt_count %d\n",
1155 chan->start_pkt_count);
1156 dev_vdbg(hsotg->dev, " hctsiz.pktcnt %d\n",
Matthijs Kooijmand6ec53e2013-08-30 18:45:15 +02001157 (hctsiz & TSIZ_PKTCNT_MASK) >> TSIZ_PKTCNT_SHIFT);
Paul Zimmerman7359d482013-03-11 17:47:59 -07001158 dev_vdbg(hsotg->dev, " chan->max_packet %d\n", chan->max_packet);
1159 dev_vdbg(hsotg->dev, " bytes_transferred %d\n",
1160 xfer_length);
1161 dev_vdbg(hsotg->dev, " urb->actual_length %d\n",
1162 urb->actual_length);
1163 dev_vdbg(hsotg->dev, " urb->transfer_buffer_length %d\n",
1164 urb->length);
1165}
1166
1167/*
1168 * Handles a host channel NAK interrupt. This handler may be called in either
1169 * DMA mode or Slave mode.
1170 */
1171static void dwc2_hc_nak_intr(struct dwc2_hsotg *hsotg,
1172 struct dwc2_host_chan *chan, int chnum,
1173 struct dwc2_qtd *qtd)
1174{
Gregory Herreroe4991232015-04-29 22:09:20 +02001175 if (!qtd) {
1176 dev_dbg(hsotg->dev, "%s: qtd is NULL\n", __func__);
1177 return;
1178 }
1179
1180 if (!qtd->urb) {
1181 dev_dbg(hsotg->dev, "%s: qtd->urb is NULL\n", __func__);
1182 return;
1183 }
1184
Matthijs Kooijmanb49977a2013-04-10 09:55:50 +02001185 if (dbg_hc(chan))
1186 dev_vdbg(hsotg->dev, "--Host Channel %d Interrupt: NAK Received--\n",
1187 chnum);
Paul Zimmerman7359d482013-03-11 17:47:59 -07001188
1189 /*
1190 * Handle NAK for IN/OUT SSPLIT/CSPLIT transfers, bulk, control, and
1191 * interrupt. Re-start the SSPLIT transfer.
1192 */
1193 if (chan->do_split) {
1194 if (chan->complete_split)
1195 qtd->error_count = 0;
1196 qtd->complete_split = 0;
1197 dwc2_halt_channel(hsotg, chan, qtd, DWC2_HC_XFER_NAK);
1198 goto handle_nak_done;
1199 }
1200
1201 switch (dwc2_hcd_get_pipe_type(&qtd->urb->pipe_info)) {
1202 case USB_ENDPOINT_XFER_CONTROL:
1203 case USB_ENDPOINT_XFER_BULK:
1204 if (hsotg->core_params->dma_enable > 0 && chan->ep_is_in) {
1205 /*
1206 * NAK interrupts are enabled on bulk/control IN
1207 * transfers in DMA mode for the sole purpose of
1208 * resetting the error count after a transaction error
1209 * occurs. The core will continue transferring data.
1210 */
1211 qtd->error_count = 0;
1212 break;
1213 }
1214
1215 /*
1216 * NAK interrupts normally occur during OUT transfers in DMA
1217 * or Slave mode. For IN transfers, more requests will be
1218 * queued as request queue space is available.
1219 */
1220 qtd->error_count = 0;
1221
1222 if (!chan->qh->ping_state) {
1223 dwc2_update_urb_state_abn(hsotg, chan, chnum, qtd->urb,
1224 qtd, DWC2_HC_XFER_NAK);
1225 dwc2_hcd_save_data_toggle(hsotg, chan, chnum, qtd);
1226
1227 if (chan->speed == USB_SPEED_HIGH)
1228 chan->qh->ping_state = 1;
1229 }
1230
1231 /*
1232 * Halt the channel so the transfer can be re-started from
1233 * the appropriate point or the PING protocol will
1234 * start/continue
1235 */
1236 dwc2_halt_channel(hsotg, chan, qtd, DWC2_HC_XFER_NAK);
1237 break;
1238 case USB_ENDPOINT_XFER_INT:
1239 qtd->error_count = 0;
1240 dwc2_halt_channel(hsotg, chan, qtd, DWC2_HC_XFER_NAK);
1241 break;
1242 case USB_ENDPOINT_XFER_ISOC:
1243 /* Should never get called for isochronous transfers */
1244 dev_err(hsotg->dev, "NACK interrupt for ISOC transfer\n");
1245 break;
1246 }
1247
1248handle_nak_done:
1249 disable_hc_int(hsotg, chnum, HCINTMSK_NAK);
1250}
1251
1252/*
1253 * Handles a host channel ACK interrupt. This interrupt is enabled when
1254 * performing the PING protocol in Slave mode, when errors occur during
1255 * either Slave mode or DMA mode, and during Start Split transactions.
1256 */
1257static void dwc2_hc_ack_intr(struct dwc2_hsotg *hsotg,
1258 struct dwc2_host_chan *chan, int chnum,
1259 struct dwc2_qtd *qtd)
1260{
1261 struct dwc2_hcd_iso_packet_desc *frame_desc;
1262
Matthijs Kooijmanb49977a2013-04-10 09:55:50 +02001263 if (dbg_hc(chan))
1264 dev_vdbg(hsotg->dev, "--Host Channel %d Interrupt: ACK Received--\n",
1265 chnum);
Paul Zimmerman7359d482013-03-11 17:47:59 -07001266
1267 if (chan->do_split) {
1268 /* Handle ACK on SSPLIT. ACK should not occur in CSPLIT. */
1269 if (!chan->ep_is_in &&
1270 chan->data_pid_start != DWC2_HC_PID_SETUP)
1271 qtd->ssplit_out_xfer_count = chan->xfer_len;
1272
1273 if (chan->ep_type != USB_ENDPOINT_XFER_ISOC || chan->ep_is_in) {
1274 qtd->complete_split = 1;
1275 dwc2_halt_channel(hsotg, chan, qtd, DWC2_HC_XFER_ACK);
1276 } else {
1277 /* ISOC OUT */
1278 switch (chan->xact_pos) {
1279 case DWC2_HCSPLT_XACTPOS_ALL:
1280 break;
1281 case DWC2_HCSPLT_XACTPOS_END:
1282 qtd->isoc_split_pos = DWC2_HCSPLT_XACTPOS_ALL;
1283 qtd->isoc_split_offset = 0;
1284 break;
1285 case DWC2_HCSPLT_XACTPOS_BEGIN:
1286 case DWC2_HCSPLT_XACTPOS_MID:
1287 /*
1288 * For BEGIN or MID, calculate the length for
1289 * the next microframe to determine the correct
1290 * SSPLIT token, either MID or END
1291 */
1292 frame_desc = &qtd->urb->iso_descs[
1293 qtd->isoc_frame_index];
1294 qtd->isoc_split_offset += 188;
1295
1296 if (frame_desc->length - qtd->isoc_split_offset
1297 <= 188)
1298 qtd->isoc_split_pos =
1299 DWC2_HCSPLT_XACTPOS_END;
1300 else
1301 qtd->isoc_split_pos =
1302 DWC2_HCSPLT_XACTPOS_MID;
1303 break;
1304 }
1305 }
1306 } else {
1307 qtd->error_count = 0;
1308
1309 if (chan->qh->ping_state) {
1310 chan->qh->ping_state = 0;
1311 /*
1312 * Halt the channel so the transfer can be re-started
1313 * from the appropriate point. This only happens in
1314 * Slave mode. In DMA mode, the ping_state is cleared
1315 * when the transfer is started because the core
1316 * automatically executes the PING, then the transfer.
1317 */
1318 dwc2_halt_channel(hsotg, chan, qtd, DWC2_HC_XFER_ACK);
1319 }
1320 }
1321
1322 /*
1323 * If the ACK occurred when _not_ in the PING state, let the channel
1324 * continue transferring data after clearing the error count
1325 */
1326 disable_hc_int(hsotg, chnum, HCINTMSK_ACK);
1327}
1328
1329/*
1330 * Handles a host channel NYET interrupt. This interrupt should only occur on
1331 * Bulk and Control OUT endpoints and for complete split transactions. If a
1332 * NYET occurs at the same time as a Transfer Complete interrupt, it is
1333 * handled in the xfercomp interrupt handler, not here. This handler may be
1334 * called in either DMA mode or Slave mode.
1335 */
1336static void dwc2_hc_nyet_intr(struct dwc2_hsotg *hsotg,
1337 struct dwc2_host_chan *chan, int chnum,
1338 struct dwc2_qtd *qtd)
1339{
Matthijs Kooijmanb49977a2013-04-10 09:55:50 +02001340 if (dbg_hc(chan))
1341 dev_vdbg(hsotg->dev, "--Host Channel %d Interrupt: NYET Received--\n",
1342 chnum);
Paul Zimmerman7359d482013-03-11 17:47:59 -07001343
1344 /*
1345 * NYET on CSPLIT
1346 * re-do the CSPLIT immediately on non-periodic
1347 */
1348 if (chan->do_split && chan->complete_split) {
1349 if (chan->ep_is_in && chan->ep_type == USB_ENDPOINT_XFER_ISOC &&
1350 hsotg->core_params->dma_enable > 0) {
1351 qtd->complete_split = 0;
1352 qtd->isoc_split_offset = 0;
Paul Zimmerman0d012b92013-07-13 14:53:48 -07001353 qtd->isoc_frame_index++;
Paul Zimmerman7902c162013-04-22 14:00:18 -07001354 if (qtd->urb &&
Paul Zimmerman0d012b92013-07-13 14:53:48 -07001355 qtd->isoc_frame_index == qtd->urb->packet_count) {
1356 dwc2_host_complete(hsotg, qtd, 0);
Paul Zimmerman7359d482013-03-11 17:47:59 -07001357 dwc2_release_channel(hsotg, chan, qtd,
Paul Zimmerman7902c162013-04-22 14:00:18 -07001358 DWC2_HC_XFER_URB_COMPLETE);
Paul Zimmerman7359d482013-03-11 17:47:59 -07001359 } else {
1360 dwc2_release_channel(hsotg, chan, qtd,
1361 DWC2_HC_XFER_NO_HALT_STATUS);
1362 }
1363 goto handle_nyet_done;
1364 }
1365
1366 if (chan->ep_type == USB_ENDPOINT_XFER_INT ||
1367 chan->ep_type == USB_ENDPOINT_XFER_ISOC) {
1368 int frnum = dwc2_hcd_get_frame_number(hsotg);
1369
1370 if (dwc2_full_frame_num(frnum) !=
1371 dwc2_full_frame_num(chan->qh->sched_frame)) {
1372 /*
1373 * No longer in the same full speed frame.
1374 * Treat this as a transaction error.
1375 */
1376#if 0
1377 /*
1378 * Todo: Fix system performance so this can
1379 * be treated as an error. Right now complete
1380 * splits cannot be scheduled precisely enough
1381 * due to other system activity, so this error
1382 * occurs regularly in Slave mode.
1383 */
1384 qtd->error_count++;
1385#endif
1386 qtd->complete_split = 0;
1387 dwc2_halt_channel(hsotg, chan, qtd,
1388 DWC2_HC_XFER_XACT_ERR);
1389 /* Todo: add support for isoc release */
1390 goto handle_nyet_done;
1391 }
1392 }
1393
1394 dwc2_halt_channel(hsotg, chan, qtd, DWC2_HC_XFER_NYET);
1395 goto handle_nyet_done;
1396 }
1397
1398 chan->qh->ping_state = 1;
1399 qtd->error_count = 0;
1400
1401 dwc2_update_urb_state_abn(hsotg, chan, chnum, qtd->urb, qtd,
1402 DWC2_HC_XFER_NYET);
1403 dwc2_hcd_save_data_toggle(hsotg, chan, chnum, qtd);
1404
1405 /*
1406 * Halt the channel and re-start the transfer so the PING protocol
1407 * will start
1408 */
1409 dwc2_halt_channel(hsotg, chan, qtd, DWC2_HC_XFER_NYET);
1410
1411handle_nyet_done:
1412 disable_hc_int(hsotg, chnum, HCINTMSK_NYET);
1413}
1414
1415/*
1416 * Handles a host channel babble interrupt. This handler may be called in
1417 * either DMA mode or Slave mode.
1418 */
1419static void dwc2_hc_babble_intr(struct dwc2_hsotg *hsotg,
1420 struct dwc2_host_chan *chan, int chnum,
1421 struct dwc2_qtd *qtd)
1422{
1423 dev_dbg(hsotg->dev, "--Host Channel %d Interrupt: Babble Error--\n",
1424 chnum);
1425
Paul Zimmerman0d012b92013-07-13 14:53:48 -07001426 dwc2_hc_handle_tt_clear(hsotg, chan, qtd);
1427
Paul Zimmerman7359d482013-03-11 17:47:59 -07001428 if (hsotg->core_params->dma_desc_enable > 0) {
1429 dwc2_hcd_complete_xfer_ddma(hsotg, chan, chnum,
1430 DWC2_HC_XFER_BABBLE_ERR);
Paul Zimmerman0d012b92013-07-13 14:53:48 -07001431 goto disable_int;
Paul Zimmerman7359d482013-03-11 17:47:59 -07001432 }
1433
1434 if (chan->ep_type != USB_ENDPOINT_XFER_ISOC) {
Paul Zimmerman0d012b92013-07-13 14:53:48 -07001435 dwc2_host_complete(hsotg, qtd, -EOVERFLOW);
Paul Zimmerman7359d482013-03-11 17:47:59 -07001436 dwc2_halt_channel(hsotg, chan, qtd, DWC2_HC_XFER_BABBLE_ERR);
1437 } else {
1438 enum dwc2_halt_status halt_status;
1439
1440 halt_status = dwc2_update_isoc_urb_state(hsotg, chan, chnum,
1441 qtd, DWC2_HC_XFER_BABBLE_ERR);
1442 dwc2_halt_channel(hsotg, chan, qtd, halt_status);
1443 }
1444
Paul Zimmerman0d012b92013-07-13 14:53:48 -07001445disable_int:
Paul Zimmerman7359d482013-03-11 17:47:59 -07001446 disable_hc_int(hsotg, chnum, HCINTMSK_BBLERR);
1447}
1448
1449/*
1450 * Handles a host channel AHB error interrupt. This handler is only called in
1451 * DMA mode.
1452 */
1453static void dwc2_hc_ahberr_intr(struct dwc2_hsotg *hsotg,
1454 struct dwc2_host_chan *chan, int chnum,
1455 struct dwc2_qtd *qtd)
1456{
1457 struct dwc2_hcd_urb *urb = qtd->urb;
1458 char *pipetype, *speed;
1459 u32 hcchar;
1460 u32 hcsplt;
1461 u32 hctsiz;
1462 u32 hc_dma;
1463
1464 dev_dbg(hsotg->dev, "--Host Channel %d Interrupt: AHB Error--\n",
1465 chnum);
1466
1467 if (!urb)
1468 goto handle_ahberr_halt;
1469
Paul Zimmerman0d012b92013-07-13 14:53:48 -07001470 dwc2_hc_handle_tt_clear(hsotg, chan, qtd);
1471
Antti Seppälä95c8bc32015-08-20 21:41:07 +03001472 hcchar = dwc2_readl(hsotg->regs + HCCHAR(chnum));
1473 hcsplt = dwc2_readl(hsotg->regs + HCSPLT(chnum));
1474 hctsiz = dwc2_readl(hsotg->regs + HCTSIZ(chnum));
1475 hc_dma = dwc2_readl(hsotg->regs + HCDMA(chnum));
Paul Zimmerman7359d482013-03-11 17:47:59 -07001476
1477 dev_err(hsotg->dev, "AHB ERROR, Channel %d\n", chnum);
1478 dev_err(hsotg->dev, " hcchar 0x%08x, hcsplt 0x%08x\n", hcchar, hcsplt);
1479 dev_err(hsotg->dev, " hctsiz 0x%08x, hc_dma 0x%08x\n", hctsiz, hc_dma);
1480 dev_err(hsotg->dev, " Device address: %d\n",
1481 dwc2_hcd_get_dev_addr(&urb->pipe_info));
1482 dev_err(hsotg->dev, " Endpoint: %d, %s\n",
1483 dwc2_hcd_get_ep_num(&urb->pipe_info),
1484 dwc2_hcd_is_pipe_in(&urb->pipe_info) ? "IN" : "OUT");
1485
1486 switch (dwc2_hcd_get_pipe_type(&urb->pipe_info)) {
1487 case USB_ENDPOINT_XFER_CONTROL:
1488 pipetype = "CONTROL";
1489 break;
1490 case USB_ENDPOINT_XFER_BULK:
1491 pipetype = "BULK";
1492 break;
1493 case USB_ENDPOINT_XFER_INT:
1494 pipetype = "INTERRUPT";
1495 break;
1496 case USB_ENDPOINT_XFER_ISOC:
1497 pipetype = "ISOCHRONOUS";
1498 break;
1499 default:
1500 pipetype = "UNKNOWN";
1501 break;
1502 }
1503
1504 dev_err(hsotg->dev, " Endpoint type: %s\n", pipetype);
1505
1506 switch (chan->speed) {
1507 case USB_SPEED_HIGH:
1508 speed = "HIGH";
1509 break;
1510 case USB_SPEED_FULL:
1511 speed = "FULL";
1512 break;
1513 case USB_SPEED_LOW:
1514 speed = "LOW";
1515 break;
1516 default:
1517 speed = "UNKNOWN";
1518 break;
1519 }
1520
1521 dev_err(hsotg->dev, " Speed: %s\n", speed);
1522
1523 dev_err(hsotg->dev, " Max packet size: %d\n",
1524 dwc2_hcd_get_mps(&urb->pipe_info));
1525 dev_err(hsotg->dev, " Data buffer length: %d\n", urb->length);
Paul Zimmerman157dfaa2013-03-14 13:12:00 -07001526 dev_err(hsotg->dev, " Transfer buffer: %p, Transfer DMA: %08lx\n",
1527 urb->buf, (unsigned long)urb->dma);
1528 dev_err(hsotg->dev, " Setup buffer: %p, Setup DMA: %08lx\n",
1529 urb->setup_packet, (unsigned long)urb->setup_dma);
Paul Zimmerman7359d482013-03-11 17:47:59 -07001530 dev_err(hsotg->dev, " Interval: %d\n", urb->interval);
1531
1532 /* Core halts the channel for Descriptor DMA mode */
1533 if (hsotg->core_params->dma_desc_enable > 0) {
1534 dwc2_hcd_complete_xfer_ddma(hsotg, chan, chnum,
1535 DWC2_HC_XFER_AHB_ERR);
1536 goto handle_ahberr_done;
1537 }
1538
Paul Zimmerman0d012b92013-07-13 14:53:48 -07001539 dwc2_host_complete(hsotg, qtd, -EIO);
Paul Zimmerman7359d482013-03-11 17:47:59 -07001540
1541handle_ahberr_halt:
1542 /*
1543 * Force a channel halt. Don't call dwc2_halt_channel because that won't
1544 * write to the HCCHARn register in DMA mode to force the halt.
1545 */
1546 dwc2_hc_halt(hsotg, chan, DWC2_HC_XFER_AHB_ERR);
1547
1548handle_ahberr_done:
Paul Zimmerman7359d482013-03-11 17:47:59 -07001549 disable_hc_int(hsotg, chnum, HCINTMSK_AHBERR);
1550}
1551
1552/*
1553 * Handles a host channel transaction error interrupt. This handler may be
1554 * called in either DMA mode or Slave mode.
1555 */
1556static void dwc2_hc_xacterr_intr(struct dwc2_hsotg *hsotg,
1557 struct dwc2_host_chan *chan, int chnum,
1558 struct dwc2_qtd *qtd)
1559{
1560 dev_dbg(hsotg->dev,
1561 "--Host Channel %d Interrupt: Transaction Error--\n", chnum);
1562
Paul Zimmerman0d012b92013-07-13 14:53:48 -07001563 dwc2_hc_handle_tt_clear(hsotg, chan, qtd);
1564
Paul Zimmerman7359d482013-03-11 17:47:59 -07001565 if (hsotg->core_params->dma_desc_enable > 0) {
1566 dwc2_hcd_complete_xfer_ddma(hsotg, chan, chnum,
1567 DWC2_HC_XFER_XACT_ERR);
1568 goto handle_xacterr_done;
1569 }
1570
1571 switch (dwc2_hcd_get_pipe_type(&qtd->urb->pipe_info)) {
1572 case USB_ENDPOINT_XFER_CONTROL:
1573 case USB_ENDPOINT_XFER_BULK:
1574 qtd->error_count++;
1575 if (!chan->qh->ping_state) {
1576
1577 dwc2_update_urb_state_abn(hsotg, chan, chnum, qtd->urb,
1578 qtd, DWC2_HC_XFER_XACT_ERR);
1579 dwc2_hcd_save_data_toggle(hsotg, chan, chnum, qtd);
1580 if (!chan->ep_is_in && chan->speed == USB_SPEED_HIGH)
1581 chan->qh->ping_state = 1;
1582 }
1583
1584 /*
1585 * Halt the channel so the transfer can be re-started from
1586 * the appropriate point or the PING protocol will start
1587 */
1588 dwc2_halt_channel(hsotg, chan, qtd, DWC2_HC_XFER_XACT_ERR);
1589 break;
1590 case USB_ENDPOINT_XFER_INT:
1591 qtd->error_count++;
1592 if (chan->do_split && chan->complete_split)
1593 qtd->complete_split = 0;
1594 dwc2_halt_channel(hsotg, chan, qtd, DWC2_HC_XFER_XACT_ERR);
1595 break;
1596 case USB_ENDPOINT_XFER_ISOC:
1597 {
1598 enum dwc2_halt_status halt_status;
1599
1600 halt_status = dwc2_update_isoc_urb_state(hsotg, chan,
1601 chnum, qtd, DWC2_HC_XFER_XACT_ERR);
1602 dwc2_halt_channel(hsotg, chan, qtd, halt_status);
1603 }
1604 break;
1605 }
1606
1607handle_xacterr_done:
Paul Zimmerman7359d482013-03-11 17:47:59 -07001608 disable_hc_int(hsotg, chnum, HCINTMSK_XACTERR);
1609}
1610
1611/*
1612 * Handles a host channel frame overrun interrupt. This handler may be called
1613 * in either DMA mode or Slave mode.
1614 */
1615static void dwc2_hc_frmovrun_intr(struct dwc2_hsotg *hsotg,
1616 struct dwc2_host_chan *chan, int chnum,
1617 struct dwc2_qtd *qtd)
1618{
1619 enum dwc2_halt_status halt_status;
1620
Matthijs Kooijmanb49977a2013-04-10 09:55:50 +02001621 if (dbg_hc(chan))
1622 dev_dbg(hsotg->dev, "--Host Channel %d Interrupt: Frame Overrun--\n",
1623 chnum);
Paul Zimmerman7359d482013-03-11 17:47:59 -07001624
Paul Zimmerman0d012b92013-07-13 14:53:48 -07001625 dwc2_hc_handle_tt_clear(hsotg, chan, qtd);
1626
Paul Zimmerman7359d482013-03-11 17:47:59 -07001627 switch (dwc2_hcd_get_pipe_type(&qtd->urb->pipe_info)) {
1628 case USB_ENDPOINT_XFER_CONTROL:
1629 case USB_ENDPOINT_XFER_BULK:
1630 break;
1631 case USB_ENDPOINT_XFER_INT:
1632 dwc2_halt_channel(hsotg, chan, qtd, DWC2_HC_XFER_FRAME_OVERRUN);
1633 break;
1634 case USB_ENDPOINT_XFER_ISOC:
1635 halt_status = dwc2_update_isoc_urb_state(hsotg, chan, chnum,
1636 qtd, DWC2_HC_XFER_FRAME_OVERRUN);
1637 dwc2_halt_channel(hsotg, chan, qtd, halt_status);
1638 break;
1639 }
1640
Paul Zimmerman7359d482013-03-11 17:47:59 -07001641 disable_hc_int(hsotg, chnum, HCINTMSK_FRMOVRUN);
1642}
1643
1644/*
1645 * Handles a host channel data toggle error interrupt. This handler may be
1646 * called in either DMA mode or Slave mode.
1647 */
1648static void dwc2_hc_datatglerr_intr(struct dwc2_hsotg *hsotg,
1649 struct dwc2_host_chan *chan, int chnum,
1650 struct dwc2_qtd *qtd)
1651{
1652 dev_dbg(hsotg->dev,
1653 "--Host Channel %d Interrupt: Data Toggle Error--\n", chnum);
1654
1655 if (chan->ep_is_in)
1656 qtd->error_count = 0;
1657 else
1658 dev_err(hsotg->dev,
1659 "Data Toggle Error on OUT transfer, channel %d\n",
1660 chnum);
1661
1662 dwc2_hc_handle_tt_clear(hsotg, chan, qtd);
1663 disable_hc_int(hsotg, chnum, HCINTMSK_DATATGLERR);
1664}
1665
1666/*
1667 * For debug only. It checks that a valid halt status is set and that
1668 * HCCHARn.chdis is clear. If there's a problem, corrective action is
1669 * taken and a warning is issued.
1670 *
1671 * Return: true if halt status is ok, false otherwise
1672 */
1673static bool dwc2_halt_status_ok(struct dwc2_hsotg *hsotg,
1674 struct dwc2_host_chan *chan, int chnum,
1675 struct dwc2_qtd *qtd)
1676{
1677#ifdef DEBUG
1678 u32 hcchar;
1679 u32 hctsiz;
1680 u32 hcintmsk;
1681 u32 hcsplt;
1682
1683 if (chan->halt_status == DWC2_HC_XFER_NO_HALT_STATUS) {
1684 /*
1685 * This code is here only as a check. This condition should
1686 * never happen. Ignore the halt if it does occur.
1687 */
Antti Seppälä95c8bc32015-08-20 21:41:07 +03001688 hcchar = dwc2_readl(hsotg->regs + HCCHAR(chnum));
1689 hctsiz = dwc2_readl(hsotg->regs + HCTSIZ(chnum));
1690 hcintmsk = dwc2_readl(hsotg->regs + HCINTMSK(chnum));
1691 hcsplt = dwc2_readl(hsotg->regs + HCSPLT(chnum));
Paul Zimmerman7359d482013-03-11 17:47:59 -07001692 dev_dbg(hsotg->dev,
1693 "%s: chan->halt_status DWC2_HC_XFER_NO_HALT_STATUS,\n",
1694 __func__);
1695 dev_dbg(hsotg->dev,
1696 "channel %d, hcchar 0x%08x, hctsiz 0x%08x,\n",
1697 chnum, hcchar, hctsiz);
1698 dev_dbg(hsotg->dev,
1699 "hcint 0x%08x, hcintmsk 0x%08x, hcsplt 0x%08x,\n",
1700 chan->hcint, hcintmsk, hcsplt);
Matthijs Kooijman8509f2f2013-03-25 12:00:25 -07001701 if (qtd)
1702 dev_dbg(hsotg->dev, "qtd->complete_split %d\n",
1703 qtd->complete_split);
Paul Zimmerman7359d482013-03-11 17:47:59 -07001704 dev_warn(hsotg->dev,
1705 "%s: no halt status, channel %d, ignoring interrupt\n",
1706 __func__, chnum);
1707 return false;
1708 }
1709
1710 /*
1711 * This code is here only as a check. hcchar.chdis should never be set
1712 * when the halt interrupt occurs. Halt the channel again if it does
1713 * occur.
1714 */
Antti Seppälä95c8bc32015-08-20 21:41:07 +03001715 hcchar = dwc2_readl(hsotg->regs + HCCHAR(chnum));
Paul Zimmerman7359d482013-03-11 17:47:59 -07001716 if (hcchar & HCCHAR_CHDIS) {
1717 dev_warn(hsotg->dev,
1718 "%s: hcchar.chdis set unexpectedly, hcchar 0x%08x, trying to halt again\n",
1719 __func__, hcchar);
1720 chan->halt_pending = 0;
1721 dwc2_halt_channel(hsotg, chan, qtd, chan->halt_status);
1722 return false;
1723 }
1724#endif
1725
1726 return true;
1727}
1728
1729/*
1730 * Handles a host Channel Halted interrupt in DMA mode. This handler
1731 * determines the reason the channel halted and proceeds accordingly.
1732 */
1733static void dwc2_hc_chhltd_intr_dma(struct dwc2_hsotg *hsotg,
1734 struct dwc2_host_chan *chan, int chnum,
1735 struct dwc2_qtd *qtd)
1736{
1737 u32 hcintmsk;
1738 int out_nak_enh = 0;
1739
Matthijs Kooijmanb49977a2013-04-10 09:55:50 +02001740 if (dbg_hc(chan))
1741 dev_vdbg(hsotg->dev,
1742 "--Host Channel %d Interrupt: DMA Channel Halted--\n",
1743 chnum);
Paul Zimmerman7359d482013-03-11 17:47:59 -07001744
1745 /*
1746 * For core with OUT NAK enhancement, the flow for high-speed
1747 * CONTROL/BULK OUT is handled a little differently
1748 */
Matthijs Kooijman9badec22013-08-30 18:45:21 +02001749 if (hsotg->hw_params.snpsid >= DWC2_CORE_REV_2_71a) {
Paul Zimmerman7359d482013-03-11 17:47:59 -07001750 if (chan->speed == USB_SPEED_HIGH && !chan->ep_is_in &&
1751 (chan->ep_type == USB_ENDPOINT_XFER_CONTROL ||
1752 chan->ep_type == USB_ENDPOINT_XFER_BULK)) {
1753 out_nak_enh = 1;
1754 }
1755 }
1756
1757 if (chan->halt_status == DWC2_HC_XFER_URB_DEQUEUE ||
1758 (chan->halt_status == DWC2_HC_XFER_AHB_ERR &&
1759 hsotg->core_params->dma_desc_enable <= 0)) {
1760 if (hsotg->core_params->dma_desc_enable > 0)
1761 dwc2_hcd_complete_xfer_ddma(hsotg, chan, chnum,
1762 chan->halt_status);
1763 else
1764 /*
1765 * Just release the channel. A dequeue can happen on a
1766 * transfer timeout. In the case of an AHB Error, the
1767 * channel was forced to halt because there's no way to
1768 * gracefully recover.
1769 */
1770 dwc2_release_channel(hsotg, chan, qtd,
1771 chan->halt_status);
1772 return;
1773 }
1774
Antti Seppälä95c8bc32015-08-20 21:41:07 +03001775 hcintmsk = dwc2_readl(hsotg->regs + HCINTMSK(chnum));
Paul Zimmerman7359d482013-03-11 17:47:59 -07001776
1777 if (chan->hcint & HCINTMSK_XFERCOMPL) {
1778 /*
1779 * Todo: This is here because of a possible hardware bug. Spec
1780 * says that on SPLIT-ISOC OUT transfers in DMA mode that a HALT
1781 * interrupt w/ACK bit set should occur, but I only see the
1782 * XFERCOMP bit, even with it masked out. This is a workaround
1783 * for that behavior. Should fix this when hardware is fixed.
1784 */
1785 if (chan->ep_type == USB_ENDPOINT_XFER_ISOC && !chan->ep_is_in)
1786 dwc2_hc_ack_intr(hsotg, chan, chnum, qtd);
1787 dwc2_hc_xfercomp_intr(hsotg, chan, chnum, qtd);
1788 } else if (chan->hcint & HCINTMSK_STALL) {
1789 dwc2_hc_stall_intr(hsotg, chan, chnum, qtd);
1790 } else if ((chan->hcint & HCINTMSK_XACTERR) &&
1791 hsotg->core_params->dma_desc_enable <= 0) {
1792 if (out_nak_enh) {
1793 if (chan->hcint &
1794 (HCINTMSK_NYET | HCINTMSK_NAK | HCINTMSK_ACK)) {
1795 dev_vdbg(hsotg->dev,
1796 "XactErr with NYET/NAK/ACK\n");
1797 qtd->error_count = 0;
1798 } else {
1799 dev_vdbg(hsotg->dev,
1800 "XactErr without NYET/NAK/ACK\n");
1801 }
1802 }
1803
1804 /*
1805 * Must handle xacterr before nak or ack. Could get a xacterr
1806 * at the same time as either of these on a BULK/CONTROL OUT
1807 * that started with a PING. The xacterr takes precedence.
1808 */
1809 dwc2_hc_xacterr_intr(hsotg, chan, chnum, qtd);
1810 } else if ((chan->hcint & HCINTMSK_XCS_XACT) &&
1811 hsotg->core_params->dma_desc_enable > 0) {
1812 dwc2_hc_xacterr_intr(hsotg, chan, chnum, qtd);
1813 } else if ((chan->hcint & HCINTMSK_AHBERR) &&
1814 hsotg->core_params->dma_desc_enable > 0) {
1815 dwc2_hc_ahberr_intr(hsotg, chan, chnum, qtd);
1816 } else if (chan->hcint & HCINTMSK_BBLERR) {
1817 dwc2_hc_babble_intr(hsotg, chan, chnum, qtd);
1818 } else if (chan->hcint & HCINTMSK_FRMOVRUN) {
1819 dwc2_hc_frmovrun_intr(hsotg, chan, chnum, qtd);
1820 } else if (!out_nak_enh) {
1821 if (chan->hcint & HCINTMSK_NYET) {
1822 /*
1823 * Must handle nyet before nak or ack. Could get a nyet
1824 * at the same time as either of those on a BULK/CONTROL
1825 * OUT that started with a PING. The nyet takes
1826 * precedence.
1827 */
1828 dwc2_hc_nyet_intr(hsotg, chan, chnum, qtd);
1829 } else if ((chan->hcint & HCINTMSK_NAK) &&
1830 !(hcintmsk & HCINTMSK_NAK)) {
1831 /*
1832 * If nak is not masked, it's because a non-split IN
1833 * transfer is in an error state. In that case, the nak
1834 * is handled by the nak interrupt handler, not here.
1835 * Handle nak here for BULK/CONTROL OUT transfers, which
1836 * halt on a NAK to allow rewinding the buffer pointer.
1837 */
1838 dwc2_hc_nak_intr(hsotg, chan, chnum, qtd);
1839 } else if ((chan->hcint & HCINTMSK_ACK) &&
1840 !(hcintmsk & HCINTMSK_ACK)) {
1841 /*
1842 * If ack is not masked, it's because a non-split IN
1843 * transfer is in an error state. In that case, the ack
1844 * is handled by the ack interrupt handler, not here.
1845 * Handle ack here for split transfers. Start splits
1846 * halt on ACK.
1847 */
1848 dwc2_hc_ack_intr(hsotg, chan, chnum, qtd);
1849 } else {
1850 if (chan->ep_type == USB_ENDPOINT_XFER_INT ||
1851 chan->ep_type == USB_ENDPOINT_XFER_ISOC) {
1852 /*
1853 * A periodic transfer halted with no other
1854 * channel interrupts set. Assume it was halted
1855 * by the core because it could not be completed
1856 * in its scheduled (micro)frame.
1857 */
1858 dev_dbg(hsotg->dev,
1859 "%s: Halt channel %d (assume incomplete periodic transfer)\n",
1860 __func__, chnum);
1861 dwc2_halt_channel(hsotg, chan, qtd,
1862 DWC2_HC_XFER_PERIODIC_INCOMPLETE);
1863 } else {
1864 dev_err(hsotg->dev,
1865 "%s: Channel %d - ChHltd set, but reason is unknown\n",
1866 __func__, chnum);
1867 dev_err(hsotg->dev,
1868 "hcint 0x%08x, intsts 0x%08x\n",
1869 chan->hcint,
Antti Seppälä95c8bc32015-08-20 21:41:07 +03001870 dwc2_readl(hsotg->regs + GINTSTS));
Nick Hudson151d0cb2014-09-11 15:22:48 -07001871 goto error;
Paul Zimmerman7359d482013-03-11 17:47:59 -07001872 }
1873 }
1874 } else {
1875 dev_info(hsotg->dev,
1876 "NYET/NAK/ACK/other in non-error case, 0x%08x\n",
1877 chan->hcint);
Nick Hudson151d0cb2014-09-11 15:22:48 -07001878error:
1879 /* Failthrough: use 3-strikes rule */
1880 qtd->error_count++;
1881 dwc2_update_urb_state_abn(hsotg, chan, chnum, qtd->urb,
1882 qtd, DWC2_HC_XFER_XACT_ERR);
1883 dwc2_hcd_save_data_toggle(hsotg, chan, chnum, qtd);
1884 dwc2_halt_channel(hsotg, chan, qtd, DWC2_HC_XFER_XACT_ERR);
Paul Zimmerman7359d482013-03-11 17:47:59 -07001885 }
1886}
1887
1888/*
1889 * Handles a host channel Channel Halted interrupt
1890 *
1891 * In slave mode, this handler is called only when the driver specifically
1892 * requests a halt. This occurs during handling other host channel interrupts
1893 * (e.g. nak, xacterr, stall, nyet, etc.).
1894 *
1895 * In DMA mode, this is the interrupt that occurs when the core has finished
1896 * processing a transfer on a channel. Other host channel interrupts (except
1897 * ahberr) are disabled in DMA mode.
1898 */
1899static void dwc2_hc_chhltd_intr(struct dwc2_hsotg *hsotg,
1900 struct dwc2_host_chan *chan, int chnum,
1901 struct dwc2_qtd *qtd)
1902{
Matthijs Kooijmanb49977a2013-04-10 09:55:50 +02001903 if (dbg_hc(chan))
1904 dev_vdbg(hsotg->dev, "--Host Channel %d Interrupt: Channel Halted--\n",
1905 chnum);
Paul Zimmerman7359d482013-03-11 17:47:59 -07001906
1907 if (hsotg->core_params->dma_enable > 0) {
1908 dwc2_hc_chhltd_intr_dma(hsotg, chan, chnum, qtd);
1909 } else {
1910 if (!dwc2_halt_status_ok(hsotg, chan, chnum, qtd))
1911 return;
1912 dwc2_release_channel(hsotg, chan, qtd, chan->halt_status);
1913 }
1914}
1915
Doug Andersondc873082015-10-16 16:01:32 -07001916/*
1917 * Check if the given qtd is still the top of the list (and thus valid).
1918 *
1919 * If dwc2_hcd_qtd_unlink_and_free() has been called since we grabbed
1920 * the qtd from the top of the list, this will return false (otherwise true).
1921 */
1922static bool dwc2_check_qtd_still_ok(struct dwc2_qtd *qtd, struct dwc2_qh *qh)
1923{
1924 struct dwc2_qtd *cur_head;
1925
1926 if (qh == NULL)
1927 return false;
1928
1929 cur_head = list_first_entry(&qh->qtd_list, struct dwc2_qtd,
1930 qtd_list_entry);
1931 return (cur_head == qtd);
1932}
1933
Paul Zimmerman7359d482013-03-11 17:47:59 -07001934/* Handles interrupt for a specific Host Channel */
1935static void dwc2_hc_n_intr(struct dwc2_hsotg *hsotg, int chnum)
1936{
1937 struct dwc2_qtd *qtd;
1938 struct dwc2_host_chan *chan;
1939 u32 hcint, hcintmsk;
1940
Matthijs Kooijmanb49977a2013-04-10 09:55:50 +02001941 chan = hsotg->hc_ptr_array[chnum];
1942
Antti Seppälä95c8bc32015-08-20 21:41:07 +03001943 hcint = dwc2_readl(hsotg->regs + HCINT(chnum));
1944 hcintmsk = dwc2_readl(hsotg->regs + HCINTMSK(chnum));
Paul Zimmerman7359d482013-03-11 17:47:59 -07001945 if (!chan) {
1946 dev_err(hsotg->dev, "## hc_ptr_array for channel is NULL ##\n");
Antti Seppälä95c8bc32015-08-20 21:41:07 +03001947 dwc2_writel(hcint, hsotg->regs + HCINT(chnum));
Paul Zimmerman7359d482013-03-11 17:47:59 -07001948 return;
1949 }
1950
Rashika Kheria723a2312013-10-30 04:16:55 +05301951 if (dbg_hc(chan)) {
1952 dev_vdbg(hsotg->dev, "--Host Channel Interrupt--, Channel %d\n",
1953 chnum);
1954 dev_vdbg(hsotg->dev,
1955 " hcint 0x%08x, hcintmsk 0x%08x, hcint&hcintmsk 0x%08x\n",
1956 hcint, hcintmsk, hcint & hcintmsk);
1957 }
1958
Antti Seppälä95c8bc32015-08-20 21:41:07 +03001959 dwc2_writel(hcint, hsotg->regs + HCINT(chnum));
Douglas Anderson16e80212016-01-28 18:19:55 -08001960
1961 /*
1962 * If we got an interrupt after someone called
1963 * dwc2_hcd_endpoint_disable() we don't want to crash below
1964 */
1965 if (!chan->qh) {
1966 dev_warn(hsotg->dev, "Interrupt on disabled channel\n");
1967 return;
1968 }
1969
Paul Zimmerman7359d482013-03-11 17:47:59 -07001970 chan->hcint = hcint;
1971 hcint &= hcintmsk;
1972
Matthijs Kooijman8509f2f2013-03-25 12:00:25 -07001973 /*
1974 * If the channel was halted due to a dequeue, the qtd list might
1975 * be empty or at least the first entry will not be the active qtd.
1976 * In this case, take a shortcut and just release the channel.
1977 */
1978 if (chan->halt_status == DWC2_HC_XFER_URB_DEQUEUE) {
1979 /*
1980 * If the channel was halted, this should be the only
1981 * interrupt unmasked
1982 */
1983 WARN_ON(hcint != HCINTMSK_CHHLTD);
1984 if (hsotg->core_params->dma_desc_enable > 0)
1985 dwc2_hcd_complete_xfer_ddma(hsotg, chan, chnum,
1986 chan->halt_status);
1987 else
1988 dwc2_release_channel(hsotg, chan, NULL,
1989 chan->halt_status);
1990 return;
1991 }
1992
Paul Zimmerman7359d482013-03-11 17:47:59 -07001993 if (list_empty(&chan->qh->qtd_list)) {
Matthijs Kooijman8509f2f2013-03-25 12:00:25 -07001994 /*
1995 * TODO: Will this ever happen with the
1996 * DWC2_HC_XFER_URB_DEQUEUE handling above?
1997 */
Paul Zimmerman7359d482013-03-11 17:47:59 -07001998 dev_dbg(hsotg->dev, "## no QTD queued for channel %d ##\n",
1999 chnum);
2000 dev_dbg(hsotg->dev,
2001 " hcint 0x%08x, hcintmsk 0x%08x, hcint&hcintmsk 0x%08x\n",
2002 chan->hcint, hcintmsk, hcint);
2003 chan->halt_status = DWC2_HC_XFER_NO_HALT_STATUS;
2004 disable_hc_int(hsotg, chnum, HCINTMSK_CHHLTD);
2005 chan->hcint = 0;
2006 return;
2007 }
2008
2009 qtd = list_first_entry(&chan->qh->qtd_list, struct dwc2_qtd,
2010 qtd_list_entry);
2011
2012 if (hsotg->core_params->dma_enable <= 0) {
2013 if ((hcint & HCINTMSK_CHHLTD) && hcint != HCINTMSK_CHHLTD)
2014 hcint &= ~HCINTMSK_CHHLTD;
2015 }
2016
2017 if (hcint & HCINTMSK_XFERCOMPL) {
2018 dwc2_hc_xfercomp_intr(hsotg, chan, chnum, qtd);
2019 /*
2020 * If NYET occurred at same time as Xfer Complete, the NYET is
2021 * handled by the Xfer Complete interrupt handler. Don't want
2022 * to call the NYET interrupt handler in this case.
2023 */
2024 hcint &= ~HCINTMSK_NYET;
2025 }
Paul Zimmerman7359d482013-03-11 17:47:59 -07002026
Doug Andersondc873082015-10-16 16:01:32 -07002027 if (hcint & HCINTMSK_CHHLTD) {
2028 dwc2_hc_chhltd_intr(hsotg, chan, chnum, qtd);
2029 if (!dwc2_check_qtd_still_ok(qtd, chan->qh))
2030 goto exit;
2031 }
2032 if (hcint & HCINTMSK_AHBERR) {
2033 dwc2_hc_ahberr_intr(hsotg, chan, chnum, qtd);
2034 if (!dwc2_check_qtd_still_ok(qtd, chan->qh))
2035 goto exit;
2036 }
2037 if (hcint & HCINTMSK_STALL) {
2038 dwc2_hc_stall_intr(hsotg, chan, chnum, qtd);
2039 if (!dwc2_check_qtd_still_ok(qtd, chan->qh))
2040 goto exit;
2041 }
2042 if (hcint & HCINTMSK_NAK) {
2043 dwc2_hc_nak_intr(hsotg, chan, chnum, qtd);
2044 if (!dwc2_check_qtd_still_ok(qtd, chan->qh))
2045 goto exit;
2046 }
2047 if (hcint & HCINTMSK_ACK) {
2048 dwc2_hc_ack_intr(hsotg, chan, chnum, qtd);
2049 if (!dwc2_check_qtd_still_ok(qtd, chan->qh))
2050 goto exit;
2051 }
2052 if (hcint & HCINTMSK_NYET) {
2053 dwc2_hc_nyet_intr(hsotg, chan, chnum, qtd);
2054 if (!dwc2_check_qtd_still_ok(qtd, chan->qh))
2055 goto exit;
2056 }
2057 if (hcint & HCINTMSK_XACTERR) {
2058 dwc2_hc_xacterr_intr(hsotg, chan, chnum, qtd);
2059 if (!dwc2_check_qtd_still_ok(qtd, chan->qh))
2060 goto exit;
2061 }
2062 if (hcint & HCINTMSK_BBLERR) {
2063 dwc2_hc_babble_intr(hsotg, chan, chnum, qtd);
2064 if (!dwc2_check_qtd_still_ok(qtd, chan->qh))
2065 goto exit;
2066 }
2067 if (hcint & HCINTMSK_FRMOVRUN) {
2068 dwc2_hc_frmovrun_intr(hsotg, chan, chnum, qtd);
2069 if (!dwc2_check_qtd_still_ok(qtd, chan->qh))
2070 goto exit;
2071 }
2072 if (hcint & HCINTMSK_DATATGLERR) {
2073 dwc2_hc_datatglerr_intr(hsotg, chan, chnum, qtd);
2074 if (!dwc2_check_qtd_still_ok(qtd, chan->qh))
2075 goto exit;
2076 }
2077
2078exit:
Paul Zimmerman7359d482013-03-11 17:47:59 -07002079 chan->hcint = 0;
2080}
2081
2082/*
2083 * This interrupt indicates that one or more host channels has a pending
2084 * interrupt. There are multiple conditions that can cause each host channel
2085 * interrupt. This function determines which conditions have occurred for each
2086 * host channel interrupt and handles them appropriately.
2087 */
2088static void dwc2_hc_intr(struct dwc2_hsotg *hsotg)
2089{
2090 u32 haint;
2091 int i;
Douglas Andersonc9c8ac02016-01-28 18:19:57 -08002092 struct dwc2_host_chan *chan, *chan_tmp;
Paul Zimmerman7359d482013-03-11 17:47:59 -07002093
Antti Seppälä95c8bc32015-08-20 21:41:07 +03002094 haint = dwc2_readl(hsotg->regs + HAINT);
Matthijs Kooijmanb49977a2013-04-10 09:55:50 +02002095 if (dbg_perio()) {
2096 dev_vdbg(hsotg->dev, "%s()\n", __func__);
2097
2098 dev_vdbg(hsotg->dev, "HAINT=%08x\n", haint);
2099 }
Paul Zimmerman7359d482013-03-11 17:47:59 -07002100
Douglas Andersonc9c8ac02016-01-28 18:19:57 -08002101 /*
2102 * According to USB 2.0 spec section 11.18.8, a host must
2103 * issue complete-split transactions in a microframe for a
2104 * set of full-/low-speed endpoints in the same relative
2105 * order as the start-splits were issued in a microframe for.
2106 */
2107 list_for_each_entry_safe(chan, chan_tmp, &hsotg->split_order,
2108 split_order_list_entry) {
2109 int hc_num = chan->hc_num;
2110
2111 if (haint & (1 << hc_num)) {
2112 dwc2_hc_n_intr(hsotg, hc_num);
2113 haint &= ~(1 << hc_num);
2114 }
2115 }
2116
Paul Zimmerman7359d482013-03-11 17:47:59 -07002117 for (i = 0; i < hsotg->core_params->host_channels; i++) {
2118 if (haint & (1 << i))
2119 dwc2_hc_n_intr(hsotg, i);
2120 }
2121}
2122
2123/* This function handles interrupts for the HCD */
Matthijs Kooijmanca18f4a2013-04-25 23:39:15 +02002124irqreturn_t dwc2_handle_hcd_intr(struct dwc2_hsotg *hsotg)
Paul Zimmerman7359d482013-03-11 17:47:59 -07002125{
Matthijs Kooijmanb49977a2013-04-10 09:55:50 +02002126 u32 gintsts, dbg_gintsts;
Matthijs Kooijman6aafb002013-04-25 23:39:14 +02002127 irqreturn_t retval = IRQ_NONE;
Paul Zimmerman7359d482013-03-11 17:47:59 -07002128
Paul Zimmerman54216ac2013-11-25 13:42:44 -08002129 if (!dwc2_is_controller_alive(hsotg)) {
Paul Zimmerman057715f2013-11-22 16:43:51 -08002130 dev_warn(hsotg->dev, "Controller is dead\n");
Matthijs Kooijman6aafb002013-04-25 23:39:14 +02002131 return retval;
Paul Zimmerman7359d482013-03-11 17:47:59 -07002132 }
2133
2134 spin_lock(&hsotg->lock);
2135
2136 /* Check if HOST Mode */
2137 if (dwc2_is_host_mode(hsotg)) {
2138 gintsts = dwc2_read_core_intr(hsotg);
2139 if (!gintsts) {
2140 spin_unlock(&hsotg->lock);
Matthijs Kooijman6aafb002013-04-25 23:39:14 +02002141 return retval;
Paul Zimmerman7359d482013-03-11 17:47:59 -07002142 }
2143
Matthijs Kooijman6aafb002013-04-25 23:39:14 +02002144 retval = IRQ_HANDLED;
Paul Zimmerman7359d482013-03-11 17:47:59 -07002145
Matthijs Kooijmanb49977a2013-04-10 09:55:50 +02002146 dbg_gintsts = gintsts;
Paul Zimmerman7359d482013-03-11 17:47:59 -07002147#ifndef DEBUG_SOF
Matthijs Kooijmanb49977a2013-04-10 09:55:50 +02002148 dbg_gintsts &= ~GINTSTS_SOF;
Paul Zimmerman7359d482013-03-11 17:47:59 -07002149#endif
Matthijs Kooijmanb49977a2013-04-10 09:55:50 +02002150 if (!dbg_perio())
2151 dbg_gintsts &= ~(GINTSTS_HCHINT | GINTSTS_RXFLVL |
2152 GINTSTS_PTXFEMP);
2153
2154 /* Only print if there are any non-suppressed interrupts left */
2155 if (dbg_gintsts)
Paul Zimmerman7359d482013-03-11 17:47:59 -07002156 dev_vdbg(hsotg->dev,
2157 "DWC OTG HCD Interrupt Detected gintsts&gintmsk=0x%08x\n",
2158 gintsts);
2159
2160 if (gintsts & GINTSTS_SOF)
2161 dwc2_sof_intr(hsotg);
2162 if (gintsts & GINTSTS_RXFLVL)
2163 dwc2_rx_fifo_level_intr(hsotg);
2164 if (gintsts & GINTSTS_NPTXFEMP)
2165 dwc2_np_tx_fifo_empty_intr(hsotg);
Paul Zimmerman7359d482013-03-11 17:47:59 -07002166 if (gintsts & GINTSTS_PRTINT)
2167 dwc2_port_intr(hsotg);
2168 if (gintsts & GINTSTS_HCHINT)
2169 dwc2_hc_intr(hsotg);
2170 if (gintsts & GINTSTS_PTXFEMP)
2171 dwc2_perio_tx_fifo_empty_intr(hsotg);
2172
Matthijs Kooijmanb49977a2013-04-10 09:55:50 +02002173 if (dbg_gintsts) {
Paul Zimmerman7359d482013-03-11 17:47:59 -07002174 dev_vdbg(hsotg->dev,
2175 "DWC OTG HCD Finished Servicing Interrupts\n");
2176 dev_vdbg(hsotg->dev,
2177 "DWC OTG HCD gintsts=0x%08x gintmsk=0x%08x\n",
Antti Seppälä95c8bc32015-08-20 21:41:07 +03002178 dwc2_readl(hsotg->regs + GINTSTS),
2179 dwc2_readl(hsotg->regs + GINTMSK));
Paul Zimmerman7359d482013-03-11 17:47:59 -07002180 }
Paul Zimmerman7359d482013-03-11 17:47:59 -07002181 }
2182
2183 spin_unlock(&hsotg->lock);
2184
2185 return retval;
2186}