blob: 97aa6ca3a12e5bd018c7151dc716a56d0cc8f41a [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 Andersonced9eee2016-01-28 18:20:04 -0800151 if (dwc2_frame_num_le(qh->next_active_frame,
152 hsotg->frame_number)) {
153 dwc2_sch_vdbg(hsotg, "QH=%p ready fn=%04x, nxt=%04x\n",
154 qh, hsotg->frame_number,
155 qh->next_active_frame);
Douglas Anderson74fc4a72016-01-28 18:19:58 -0800156
Paul Zimmerman7359d482013-03-11 17:47:59 -0700157 /*
158 * Move QH to the ready list to be executed next
159 * (micro)frame
160 */
Douglas Anderson94ef7ae2016-01-28 18:19:56 -0800161 list_move_tail(&qh->qh_list_entry,
Paul Zimmerman7359d482013-03-11 17:47:59 -0700162 &hsotg->periodic_sched_ready);
Douglas Anderson74fc4a72016-01-28 18:19:58 -0800163 }
Paul Zimmerman7359d482013-03-11 17:47:59 -0700164 }
165 tr_type = dwc2_hcd_select_transactions(hsotg);
166 if (tr_type != DWC2_TRANSACTION_NONE)
167 dwc2_hcd_queue_transactions(hsotg, tr_type);
Paul Zimmerman7359d482013-03-11 17:47:59 -0700168}
169
170/*
171 * Handles the Rx FIFO Level Interrupt, which indicates that there is
172 * at least one packet in the Rx FIFO. The packets are moved from the FIFO to
173 * memory if the DWC_otg controller is operating in Slave mode.
174 */
175static void dwc2_rx_fifo_level_intr(struct dwc2_hsotg *hsotg)
176{
177 u32 grxsts, chnum, bcnt, dpid, pktsts;
178 struct dwc2_host_chan *chan;
179
Matthijs Kooijmanb49977a2013-04-10 09:55:50 +0200180 if (dbg_perio())
181 dev_vdbg(hsotg->dev, "--RxFIFO Level Interrupt--\n");
Paul Zimmerman7359d482013-03-11 17:47:59 -0700182
Antti Seppälä95c8bc32015-08-20 21:41:07 +0300183 grxsts = dwc2_readl(hsotg->regs + GRXSTSP);
Matthijs Kooijmand6ec53e2013-08-30 18:45:15 +0200184 chnum = (grxsts & GRXSTS_HCHNUM_MASK) >> GRXSTS_HCHNUM_SHIFT;
Paul Zimmerman7359d482013-03-11 17:47:59 -0700185 chan = hsotg->hc_ptr_array[chnum];
186 if (!chan) {
187 dev_err(hsotg->dev, "Unable to get corresponding channel\n");
188 return;
189 }
190
Matthijs Kooijmand6ec53e2013-08-30 18:45:15 +0200191 bcnt = (grxsts & GRXSTS_BYTECNT_MASK) >> GRXSTS_BYTECNT_SHIFT;
192 dpid = (grxsts & GRXSTS_DPID_MASK) >> GRXSTS_DPID_SHIFT;
Matthijs Kooijmanf9234632013-08-30 18:45:13 +0200193 pktsts = (grxsts & GRXSTS_PKTSTS_MASK) >> GRXSTS_PKTSTS_SHIFT;
Paul Zimmerman7359d482013-03-11 17:47:59 -0700194
195 /* Packet Status */
Matthijs Kooijmanb49977a2013-04-10 09:55:50 +0200196 if (dbg_perio()) {
197 dev_vdbg(hsotg->dev, " Ch num = %d\n", chnum);
198 dev_vdbg(hsotg->dev, " Count = %d\n", bcnt);
199 dev_vdbg(hsotg->dev, " DPID = %d, chan.dpid = %d\n", dpid,
200 chan->data_pid_start);
Matthijs Kooijmanf9234632013-08-30 18:45:13 +0200201 dev_vdbg(hsotg->dev, " PStatus = %d\n", pktsts);
Matthijs Kooijmanb49977a2013-04-10 09:55:50 +0200202 }
Paul Zimmerman7359d482013-03-11 17:47:59 -0700203
204 switch (pktsts) {
205 case GRXSTS_PKTSTS_HCHIN:
206 /* Read the data into the host buffer */
207 if (bcnt > 0) {
208 dwc2_read_packet(hsotg, chan->xfer_buf, bcnt);
209
210 /* Update the HC fields for the next packet received */
211 chan->xfer_count += bcnt;
212 chan->xfer_buf += bcnt;
213 }
214 break;
215 case GRXSTS_PKTSTS_HCHIN_XFER_COMP:
216 case GRXSTS_PKTSTS_DATATOGGLEERR:
217 case GRXSTS_PKTSTS_HCHHALTED:
218 /* Handled in interrupt, just ignore data */
219 break;
220 default:
221 dev_err(hsotg->dev,
222 "RxFIFO Level Interrupt: Unknown status %d\n", pktsts);
223 break;
224 }
225}
226
227/*
228 * This interrupt occurs when the non-periodic Tx FIFO is half-empty. More
229 * data packets may be written to the FIFO for OUT transfers. More requests
230 * may be written to the non-periodic request queue for IN transfers. This
231 * interrupt is enabled only in Slave mode.
232 */
233static void dwc2_np_tx_fifo_empty_intr(struct dwc2_hsotg *hsotg)
234{
235 dev_vdbg(hsotg->dev, "--Non-Periodic TxFIFO Empty Interrupt--\n");
236 dwc2_hcd_queue_transactions(hsotg, DWC2_TRANSACTION_NON_PERIODIC);
237}
238
239/*
240 * This interrupt occurs when the periodic Tx FIFO is half-empty. More data
241 * packets may be written to the FIFO for OUT transfers. More requests may be
242 * written to the periodic request queue for IN transfers. This interrupt is
243 * enabled only in Slave mode.
244 */
245static void dwc2_perio_tx_fifo_empty_intr(struct dwc2_hsotg *hsotg)
246{
Matthijs Kooijmanb49977a2013-04-10 09:55:50 +0200247 if (dbg_perio())
248 dev_vdbg(hsotg->dev, "--Periodic TxFIFO Empty Interrupt--\n");
Paul Zimmerman7359d482013-03-11 17:47:59 -0700249 dwc2_hcd_queue_transactions(hsotg, DWC2_TRANSACTION_PERIODIC);
250}
251
252static void dwc2_hprt0_enable(struct dwc2_hsotg *hsotg, u32 hprt0,
253 u32 *hprt0_modify)
254{
255 struct dwc2_core_params *params = hsotg->core_params;
256 int do_reset = 0;
257 u32 usbcfg;
258 u32 prtspd;
259 u32 hcfg;
Matthijs Kooijmanbcc5def2013-04-29 19:42:00 +0000260 u32 fslspclksel;
Paul Zimmerman7359d482013-03-11 17:47:59 -0700261 u32 hfir;
262
263 dev_vdbg(hsotg->dev, "%s(%p)\n", __func__, hsotg);
264
265 /* Every time when port enables calculate HFIR.FrInterval */
Antti Seppälä95c8bc32015-08-20 21:41:07 +0300266 hfir = dwc2_readl(hsotg->regs + HFIR);
Paul Zimmerman7359d482013-03-11 17:47:59 -0700267 hfir &= ~HFIR_FRINT_MASK;
268 hfir |= dwc2_calc_frame_interval(hsotg) << HFIR_FRINT_SHIFT &
269 HFIR_FRINT_MASK;
Antti Seppälä95c8bc32015-08-20 21:41:07 +0300270 dwc2_writel(hfir, hsotg->regs + HFIR);
Paul Zimmerman7359d482013-03-11 17:47:59 -0700271
272 /* Check if we need to adjust the PHY clock speed for low power */
273 if (!params->host_support_fs_ls_low_power) {
274 /* Port has been enabled, set the reset change flag */
275 hsotg->flags.b.port_reset_change = 1;
276 return;
277 }
278
Antti Seppälä95c8bc32015-08-20 21:41:07 +0300279 usbcfg = dwc2_readl(hsotg->regs + GUSBCFG);
Matthijs Kooijmanf9234632013-08-30 18:45:13 +0200280 prtspd = (hprt0 & HPRT0_SPD_MASK) >> HPRT0_SPD_SHIFT;
Paul Zimmerman7359d482013-03-11 17:47:59 -0700281
282 if (prtspd == HPRT0_SPD_LOW_SPEED || prtspd == HPRT0_SPD_FULL_SPEED) {
283 /* Low power */
284 if (!(usbcfg & GUSBCFG_PHY_LP_CLK_SEL)) {
285 /* Set PHY low power clock select for FS/LS devices */
286 usbcfg |= GUSBCFG_PHY_LP_CLK_SEL;
Antti Seppälä95c8bc32015-08-20 21:41:07 +0300287 dwc2_writel(usbcfg, hsotg->regs + GUSBCFG);
Paul Zimmerman7359d482013-03-11 17:47:59 -0700288 do_reset = 1;
289 }
290
Antti Seppälä95c8bc32015-08-20 21:41:07 +0300291 hcfg = dwc2_readl(hsotg->regs + HCFG);
Matthijs Kooijmanf9234632013-08-30 18:45:13 +0200292 fslspclksel = (hcfg & HCFG_FSLSPCLKSEL_MASK) >>
293 HCFG_FSLSPCLKSEL_SHIFT;
Paul Zimmerman7359d482013-03-11 17:47:59 -0700294
295 if (prtspd == HPRT0_SPD_LOW_SPEED &&
296 params->host_ls_low_power_phy_clk ==
297 DWC2_HOST_LS_LOW_POWER_PHY_CLK_PARAM_6MHZ) {
298 /* 6 MHZ */
299 dev_vdbg(hsotg->dev,
300 "FS_PHY programming HCFG to 6 MHz\n");
Matthijs Kooijmanbcc5def2013-04-29 19:42:00 +0000301 if (fslspclksel != HCFG_FSLSPCLKSEL_6_MHZ) {
Matthijs Kooijmanf9234632013-08-30 18:45:13 +0200302 fslspclksel = HCFG_FSLSPCLKSEL_6_MHZ;
Paul Zimmerman7359d482013-03-11 17:47:59 -0700303 hcfg &= ~HCFG_FSLSPCLKSEL_MASK;
Matthijs Kooijmanf9234632013-08-30 18:45:13 +0200304 hcfg |= fslspclksel << HCFG_FSLSPCLKSEL_SHIFT;
Antti Seppälä95c8bc32015-08-20 21:41:07 +0300305 dwc2_writel(hcfg, hsotg->regs + HCFG);
Paul Zimmerman7359d482013-03-11 17:47:59 -0700306 do_reset = 1;
307 }
308 } else {
309 /* 48 MHZ */
310 dev_vdbg(hsotg->dev,
311 "FS_PHY programming HCFG to 48 MHz\n");
Matthijs Kooijmanbcc5def2013-04-29 19:42:00 +0000312 if (fslspclksel != HCFG_FSLSPCLKSEL_48_MHZ) {
Matthijs Kooijmanf9234632013-08-30 18:45:13 +0200313 fslspclksel = HCFG_FSLSPCLKSEL_48_MHZ;
Paul Zimmerman7359d482013-03-11 17:47:59 -0700314 hcfg &= ~HCFG_FSLSPCLKSEL_MASK;
Matthijs Kooijmanf9234632013-08-30 18:45:13 +0200315 hcfg |= fslspclksel << HCFG_FSLSPCLKSEL_SHIFT;
Antti Seppälä95c8bc32015-08-20 21:41:07 +0300316 dwc2_writel(hcfg, hsotg->regs + HCFG);
Paul Zimmerman7359d482013-03-11 17:47:59 -0700317 do_reset = 1;
318 }
319 }
320 } else {
321 /* Not low power */
322 if (usbcfg & GUSBCFG_PHY_LP_CLK_SEL) {
323 usbcfg &= ~GUSBCFG_PHY_LP_CLK_SEL;
Antti Seppälä95c8bc32015-08-20 21:41:07 +0300324 dwc2_writel(usbcfg, hsotg->regs + GUSBCFG);
Paul Zimmerman7359d482013-03-11 17:47:59 -0700325 do_reset = 1;
326 }
327 }
328
329 if (do_reset) {
330 *hprt0_modify |= HPRT0_RST;
Douglas Anderson29539012015-11-20 09:06:28 -0800331 dwc2_writel(*hprt0_modify, hsotg->regs + HPRT0);
Paul Zimmerman7359d482013-03-11 17:47:59 -0700332 queue_delayed_work(hsotg->wq_otg, &hsotg->reset_work,
333 msecs_to_jiffies(60));
334 } else {
335 /* Port has been enabled, set the reset change flag */
336 hsotg->flags.b.port_reset_change = 1;
337 }
338}
339
340/*
341 * There are multiple conditions that can cause a port interrupt. This function
342 * determines which interrupt conditions have occurred and handles them
343 * appropriately.
344 */
345static void dwc2_port_intr(struct dwc2_hsotg *hsotg)
346{
347 u32 hprt0;
348 u32 hprt0_modify;
349
350 dev_vdbg(hsotg->dev, "--Port Interrupt--\n");
351
Antti Seppälä95c8bc32015-08-20 21:41:07 +0300352 hprt0 = dwc2_readl(hsotg->regs + HPRT0);
Paul Zimmerman7359d482013-03-11 17:47:59 -0700353 hprt0_modify = hprt0;
354
355 /*
356 * Clear appropriate bits in HPRT0 to clear the interrupt bit in
357 * GINTSTS
358 */
359 hprt0_modify &= ~(HPRT0_ENA | HPRT0_CONNDET | HPRT0_ENACHG |
360 HPRT0_OVRCURRCHG);
361
362 /*
363 * Port Connect Detected
364 * Set flag and clear if detected
365 */
366 if (hprt0 & HPRT0_CONNDET) {
Douglas Anderson29539012015-11-20 09:06:28 -0800367 dwc2_writel(hprt0_modify | HPRT0_CONNDET, hsotg->regs + HPRT0);
368
Paul Zimmerman7359d482013-03-11 17:47:59 -0700369 dev_vdbg(hsotg->dev,
370 "--Port Interrupt HPRT0=0x%08x Port Connect Detected--\n",
371 hprt0);
Douglas Anderson6a659532015-11-19 13:23:14 -0800372 dwc2_hcd_connect(hsotg);
Paul Zimmerman7359d482013-03-11 17:47:59 -0700373
374 /*
375 * The Hub driver asserts a reset when it sees port connect
376 * status change flag
377 */
378 }
379
380 /*
381 * Port Enable Changed
382 * Clear if detected - Set internal flag if disabled
383 */
384 if (hprt0 & HPRT0_ENACHG) {
Douglas Anderson29539012015-11-20 09:06:28 -0800385 dwc2_writel(hprt0_modify | HPRT0_ENACHG, hsotg->regs + HPRT0);
Paul Zimmerman7359d482013-03-11 17:47:59 -0700386 dev_vdbg(hsotg->dev,
387 " --Port Interrupt HPRT0=0x%08x Port Enable Changed (now %d)--\n",
388 hprt0, !!(hprt0 & HPRT0_ENA));
Mian Yousaf Kaukabfbb9e222015-11-20 11:49:28 +0100389 if (hprt0 & HPRT0_ENA) {
390 hsotg->new_connection = true;
Paul Zimmerman7359d482013-03-11 17:47:59 -0700391 dwc2_hprt0_enable(hsotg, hprt0, &hprt0_modify);
Mian Yousaf Kaukabfbb9e222015-11-20 11:49:28 +0100392 } else {
Paul Zimmerman7359d482013-03-11 17:47:59 -0700393 hsotg->flags.b.port_enable_change = 1;
Mian Yousaf Kaukabfbb9e222015-11-20 11:49:28 +0100394 if (hsotg->core_params->dma_desc_fs_enable) {
395 u32 hcfg;
396
397 hsotg->core_params->dma_desc_enable = 0;
398 hsotg->new_connection = false;
399 hcfg = dwc2_readl(hsotg->regs + HCFG);
400 hcfg &= ~HCFG_DESCDMA;
401 dwc2_writel(hcfg, hsotg->regs + HCFG);
402 }
403 }
Paul Zimmerman7359d482013-03-11 17:47:59 -0700404 }
405
406 /* Overcurrent Change Interrupt */
407 if (hprt0 & HPRT0_OVRCURRCHG) {
Douglas Anderson29539012015-11-20 09:06:28 -0800408 dwc2_writel(hprt0_modify | HPRT0_OVRCURRCHG,
409 hsotg->regs + HPRT0);
Paul Zimmerman7359d482013-03-11 17:47:59 -0700410 dev_vdbg(hsotg->dev,
411 " --Port Interrupt HPRT0=0x%08x Port Overcurrent Changed--\n",
412 hprt0);
413 hsotg->flags.b.port_over_current_change = 1;
Paul Zimmerman7359d482013-03-11 17:47:59 -0700414 }
Paul Zimmerman7359d482013-03-11 17:47:59 -0700415}
416
417/*
418 * Gets the actual length of a transfer after the transfer halts. halt_status
419 * holds the reason for the halt.
420 *
421 * For IN transfers where halt_status is DWC2_HC_XFER_COMPLETE, *short_read
422 * is set to 1 upon return if less than the requested number of bytes were
423 * transferred. short_read may also be NULL on entry, in which case it remains
424 * unchanged.
425 */
426static u32 dwc2_get_actual_xfer_length(struct dwc2_hsotg *hsotg,
427 struct dwc2_host_chan *chan, int chnum,
428 struct dwc2_qtd *qtd,
429 enum dwc2_halt_status halt_status,
430 int *short_read)
431{
432 u32 hctsiz, count, length;
433
Antti Seppälä95c8bc32015-08-20 21:41:07 +0300434 hctsiz = dwc2_readl(hsotg->regs + HCTSIZ(chnum));
Paul Zimmerman7359d482013-03-11 17:47:59 -0700435
436 if (halt_status == DWC2_HC_XFER_COMPLETE) {
437 if (chan->ep_is_in) {
Matthijs Kooijmand6ec53e2013-08-30 18:45:15 +0200438 count = (hctsiz & TSIZ_XFERSIZE_MASK) >>
439 TSIZ_XFERSIZE_SHIFT;
Paul Zimmerman7359d482013-03-11 17:47:59 -0700440 length = chan->xfer_len - count;
441 if (short_read != NULL)
442 *short_read = (count != 0);
443 } else if (chan->qh->do_split) {
444 length = qtd->ssplit_out_xfer_count;
445 } else {
446 length = chan->xfer_len;
447 }
448 } else {
449 /*
450 * Must use the hctsiz.pktcnt field to determine how much data
451 * has been transferred. This field reflects the number of
452 * packets that have been transferred via the USB. This is
453 * always an integral number of packets if the transfer was
454 * halted before its normal completion. (Can't use the
455 * hctsiz.xfersize field because that reflects the number of
456 * bytes transferred via the AHB, not the USB).
457 */
Matthijs Kooijmand6ec53e2013-08-30 18:45:15 +0200458 count = (hctsiz & TSIZ_PKTCNT_MASK) >> TSIZ_PKTCNT_SHIFT;
Paul Zimmerman7359d482013-03-11 17:47:59 -0700459 length = (chan->start_pkt_count - count) * chan->max_packet;
460 }
461
462 return length;
463}
464
465/**
466 * dwc2_update_urb_state() - Updates the state of the URB after a Transfer
467 * Complete interrupt on the host channel. Updates the actual_length field
468 * of the URB based on the number of bytes transferred via the host channel.
469 * Sets the URB status if the data transfer is finished.
470 *
471 * Return: 1 if the data transfer specified by the URB is completely finished,
472 * 0 otherwise
473 */
474static int dwc2_update_urb_state(struct dwc2_hsotg *hsotg,
475 struct dwc2_host_chan *chan, int chnum,
476 struct dwc2_hcd_urb *urb,
477 struct dwc2_qtd *qtd)
478{
479 u32 hctsiz;
480 int xfer_done = 0;
481 int short_read = 0;
482 int xfer_length = dwc2_get_actual_xfer_length(hsotg, chan, chnum, qtd,
483 DWC2_HC_XFER_COMPLETE,
484 &short_read);
485
486 if (urb->actual_length + xfer_length > urb->length) {
487 dev_warn(hsotg->dev, "%s(): trimming xfer length\n", __func__);
488 xfer_length = urb->length - urb->actual_length;
489 }
490
Paul Zimmerman7359d482013-03-11 17:47:59 -0700491 dev_vdbg(hsotg->dev, "urb->actual_length=%d xfer_length=%d\n",
492 urb->actual_length, xfer_length);
493 urb->actual_length += xfer_length;
494
495 if (xfer_length && chan->ep_type == USB_ENDPOINT_XFER_BULK &&
496 (urb->flags & URB_SEND_ZERO_PACKET) &&
497 urb->actual_length >= urb->length &&
498 !(urb->length % chan->max_packet)) {
499 xfer_done = 0;
500 } else if (short_read || urb->actual_length >= urb->length) {
501 xfer_done = 1;
502 urb->status = 0;
503 }
504
Antti Seppälä95c8bc32015-08-20 21:41:07 +0300505 hctsiz = dwc2_readl(hsotg->regs + HCTSIZ(chnum));
Paul Zimmerman7359d482013-03-11 17:47:59 -0700506 dev_vdbg(hsotg->dev, "DWC_otg: %s: %s, channel %d\n",
507 __func__, (chan->ep_is_in ? "IN" : "OUT"), chnum);
508 dev_vdbg(hsotg->dev, " chan->xfer_len %d\n", chan->xfer_len);
509 dev_vdbg(hsotg->dev, " hctsiz.xfersize %d\n",
Matthijs Kooijmand6ec53e2013-08-30 18:45:15 +0200510 (hctsiz & TSIZ_XFERSIZE_MASK) >> TSIZ_XFERSIZE_SHIFT);
Paul Zimmerman7359d482013-03-11 17:47:59 -0700511 dev_vdbg(hsotg->dev, " urb->transfer_buffer_length %d\n", urb->length);
512 dev_vdbg(hsotg->dev, " urb->actual_length %d\n", urb->actual_length);
513 dev_vdbg(hsotg->dev, " short_read %d, xfer_done %d\n", short_read,
514 xfer_done);
515
516 return xfer_done;
517}
518
519/*
520 * Save the starting data toggle for the next transfer. The data toggle is
521 * saved in the QH for non-control transfers and it's saved in the QTD for
522 * control transfers.
523 */
524void dwc2_hcd_save_data_toggle(struct dwc2_hsotg *hsotg,
525 struct dwc2_host_chan *chan, int chnum,
526 struct dwc2_qtd *qtd)
527{
Antti Seppälä95c8bc32015-08-20 21:41:07 +0300528 u32 hctsiz = dwc2_readl(hsotg->regs + HCTSIZ(chnum));
Matthijs Kooijmanf9234632013-08-30 18:45:13 +0200529 u32 pid = (hctsiz & TSIZ_SC_MC_PID_MASK) >> TSIZ_SC_MC_PID_SHIFT;
Paul Zimmerman7359d482013-03-11 17:47:59 -0700530
531 if (chan->ep_type != USB_ENDPOINT_XFER_CONTROL) {
Tang, Jianqiang62943b72016-02-16 15:02:07 -0800532 if (WARN(!chan || !chan->qh,
533 "chan->qh must be specified for non-control eps\n"))
534 return;
535
Paul Zimmerman7359d482013-03-11 17:47:59 -0700536 if (pid == TSIZ_SC_MC_PID_DATA0)
537 chan->qh->data_toggle = DWC2_HC_PID_DATA0;
538 else
539 chan->qh->data_toggle = DWC2_HC_PID_DATA1;
540 } else {
Tang, Jianqiang62943b72016-02-16 15:02:07 -0800541 if (WARN(!qtd,
542 "qtd must be specified for control eps\n"))
543 return;
544
Paul Zimmerman7359d482013-03-11 17:47:59 -0700545 if (pid == TSIZ_SC_MC_PID_DATA0)
546 qtd->data_toggle = DWC2_HC_PID_DATA0;
547 else
548 qtd->data_toggle = DWC2_HC_PID_DATA1;
549 }
550}
551
552/**
553 * dwc2_update_isoc_urb_state() - Updates the state of an Isochronous URB when
554 * the transfer is stopped for any reason. The fields of the current entry in
555 * the frame descriptor array are set based on the transfer state and the input
556 * halt_status. Completes the Isochronous URB if all the URB frames have been
557 * completed.
558 *
559 * Return: DWC2_HC_XFER_COMPLETE if there are more frames remaining to be
560 * transferred in the URB. Otherwise return DWC2_HC_XFER_URB_COMPLETE.
561 */
562static enum dwc2_halt_status dwc2_update_isoc_urb_state(
563 struct dwc2_hsotg *hsotg, struct dwc2_host_chan *chan,
564 int chnum, struct dwc2_qtd *qtd,
565 enum dwc2_halt_status halt_status)
566{
567 struct dwc2_hcd_iso_packet_desc *frame_desc;
568 struct dwc2_hcd_urb *urb = qtd->urb;
569
570 if (!urb)
571 return DWC2_HC_XFER_NO_HALT_STATUS;
572
573 frame_desc = &urb->iso_descs[qtd->isoc_frame_index];
574
575 switch (halt_status) {
576 case DWC2_HC_XFER_COMPLETE:
577 frame_desc->status = 0;
578 frame_desc->actual_length = dwc2_get_actual_xfer_length(hsotg,
579 chan, chnum, qtd, halt_status, NULL);
Paul Zimmerman7359d482013-03-11 17:47:59 -0700580 break;
581 case DWC2_HC_XFER_FRAME_OVERRUN:
582 urb->error_count++;
583 if (chan->ep_is_in)
584 frame_desc->status = -ENOSR;
585 else
586 frame_desc->status = -ECOMM;
587 frame_desc->actual_length = 0;
588 break;
589 case DWC2_HC_XFER_BABBLE_ERR:
590 urb->error_count++;
591 frame_desc->status = -EOVERFLOW;
592 /* Don't need to update actual_length in this case */
593 break;
594 case DWC2_HC_XFER_XACT_ERR:
595 urb->error_count++;
596 frame_desc->status = -EPROTO;
597 frame_desc->actual_length = dwc2_get_actual_xfer_length(hsotg,
598 chan, chnum, qtd, halt_status, NULL);
599
Paul Zimmerman7359d482013-03-11 17:47:59 -0700600 /* Skip whole frame */
601 if (chan->qh->do_split &&
602 chan->ep_type == USB_ENDPOINT_XFER_ISOC && chan->ep_is_in &&
603 hsotg->core_params->dma_enable > 0) {
604 qtd->complete_split = 0;
605 qtd->isoc_split_offset = 0;
606 }
607
608 break;
609 default:
610 dev_err(hsotg->dev, "Unhandled halt_status (%d)\n",
611 halt_status);
612 break;
613 }
614
615 if (++qtd->isoc_frame_index == urb->packet_count) {
616 /*
617 * urb->status is not used for isoc transfers. The individual
618 * frame_desc statuses are used instead.
619 */
Paul Zimmerman0d012b92013-07-13 14:53:48 -0700620 dwc2_host_complete(hsotg, qtd, 0);
Paul Zimmerman7359d482013-03-11 17:47:59 -0700621 halt_status = DWC2_HC_XFER_URB_COMPLETE;
622 } else {
623 halt_status = DWC2_HC_XFER_COMPLETE;
624 }
625
626 return halt_status;
627}
628
629/*
630 * Frees the first QTD in the QH's list if free_qtd is 1. For non-periodic
631 * QHs, removes the QH from the active non-periodic schedule. If any QTDs are
632 * still linked to the QH, the QH is added to the end of the inactive
633 * non-periodic schedule. For periodic QHs, removes the QH from the periodic
634 * schedule if no more QTDs are linked to the QH.
635 */
636static void dwc2_deactivate_qh(struct dwc2_hsotg *hsotg, struct dwc2_qh *qh,
637 int free_qtd)
638{
639 int continue_split = 0;
640 struct dwc2_qtd *qtd;
641
Matthijs Kooijmanb49977a2013-04-10 09:55:50 +0200642 if (dbg_qh(qh))
643 dev_vdbg(hsotg->dev, " %s(%p,%p,%d)\n", __func__,
644 hsotg, qh, free_qtd);
Paul Zimmerman7359d482013-03-11 17:47:59 -0700645
646 if (list_empty(&qh->qtd_list)) {
647 dev_dbg(hsotg->dev, "## QTD list empty ##\n");
648 goto no_qtd;
649 }
650
651 qtd = list_first_entry(&qh->qtd_list, struct dwc2_qtd, qtd_list_entry);
652
653 if (qtd->complete_split)
654 continue_split = 1;
655 else if (qtd->isoc_split_pos == DWC2_HCSPLT_XACTPOS_MID ||
656 qtd->isoc_split_pos == DWC2_HCSPLT_XACTPOS_END)
657 continue_split = 1;
658
659 if (free_qtd) {
660 dwc2_hcd_qtd_unlink_and_free(hsotg, qtd, qh);
661 continue_split = 0;
662 }
663
664no_qtd:
Paul Zimmerman7359d482013-03-11 17:47:59 -0700665 qh->channel = NULL;
666 dwc2_hcd_qh_deactivate(hsotg, qh, continue_split);
667}
668
669/**
670 * dwc2_release_channel() - Releases a host channel for use by other transfers
671 *
672 * @hsotg: The HCD state structure
673 * @chan: The host channel to release
674 * @qtd: The QTD associated with the host channel. This QTD may be
675 * freed if the transfer is complete or an error has occurred.
676 * @halt_status: Reason the channel is being released. This status
677 * determines the actions taken by this function.
678 *
679 * Also attempts to select and queue more transactions since at least one host
680 * channel is available.
681 */
682static void dwc2_release_channel(struct dwc2_hsotg *hsotg,
683 struct dwc2_host_chan *chan,
684 struct dwc2_qtd *qtd,
685 enum dwc2_halt_status halt_status)
686{
687 enum dwc2_transaction_type tr_type;
688 u32 haintmsk;
689 int free_qtd = 0;
690
Matthijs Kooijmanb49977a2013-04-10 09:55:50 +0200691 if (dbg_hc(chan))
692 dev_vdbg(hsotg->dev, " %s: channel %d, halt_status %d\n",
693 __func__, chan->hc_num, halt_status);
Paul Zimmerman7359d482013-03-11 17:47:59 -0700694
695 switch (halt_status) {
696 case DWC2_HC_XFER_URB_COMPLETE:
697 free_qtd = 1;
698 break;
699 case DWC2_HC_XFER_AHB_ERR:
700 case DWC2_HC_XFER_STALL:
701 case DWC2_HC_XFER_BABBLE_ERR:
702 free_qtd = 1;
703 break;
704 case DWC2_HC_XFER_XACT_ERR:
Matthijs Kooijman8509f2f2013-03-25 12:00:25 -0700705 if (qtd && qtd->error_count >= 3) {
Paul Zimmerman7359d482013-03-11 17:47:59 -0700706 dev_vdbg(hsotg->dev,
707 " Complete URB with transaction error\n");
708 free_qtd = 1;
Paul Zimmerman0d012b92013-07-13 14:53:48 -0700709 dwc2_host_complete(hsotg, qtd, -EPROTO);
Paul Zimmerman7359d482013-03-11 17:47:59 -0700710 }
711 break;
712 case DWC2_HC_XFER_URB_DEQUEUE:
713 /*
714 * The QTD has already been removed and the QH has been
715 * deactivated. Don't want to do anything except release the
716 * host channel and try to queue more transfers.
717 */
718 goto cleanup;
719 case DWC2_HC_XFER_PERIODIC_INCOMPLETE:
720 dev_vdbg(hsotg->dev, " Complete URB with I/O error\n");
721 free_qtd = 1;
Paul Zimmerman0d012b92013-07-13 14:53:48 -0700722 dwc2_host_complete(hsotg, qtd, -EIO);
Paul Zimmerman7359d482013-03-11 17:47:59 -0700723 break;
724 case DWC2_HC_XFER_NO_HALT_STATUS:
725 default:
726 break;
727 }
728
729 dwc2_deactivate_qh(hsotg, chan->qh, free_qtd);
730
731cleanup:
732 /*
733 * Release the host channel for use by other transfers. The cleanup
734 * function clears the channel interrupt enables and conditions, so
735 * there's no need to clear the Channel Halted interrupt separately.
736 */
737 if (!list_empty(&chan->hc_list_entry))
738 list_del(&chan->hc_list_entry);
739 dwc2_hc_cleanup(hsotg, chan);
740 list_add_tail(&chan->hc_list_entry, &hsotg->free_hc_list);
741
Dom Cobley20f2eb92013-09-23 14:23:34 -0700742 if (hsotg->core_params->uframe_sched > 0) {
743 hsotg->available_host_channels++;
744 } else {
745 switch (chan->ep_type) {
746 case USB_ENDPOINT_XFER_CONTROL:
747 case USB_ENDPOINT_XFER_BULK:
748 hsotg->non_periodic_channels--;
749 break;
750 default:
751 /*
752 * Don't release reservations for periodic channels
753 * here. That's done when a periodic transfer is
754 * descheduled (i.e. when the QH is removed from the
755 * periodic schedule).
756 */
757 break;
758 }
Paul Zimmerman7359d482013-03-11 17:47:59 -0700759 }
760
Antti Seppälä95c8bc32015-08-20 21:41:07 +0300761 haintmsk = dwc2_readl(hsotg->regs + HAINTMSK);
Paul Zimmerman7359d482013-03-11 17:47:59 -0700762 haintmsk &= ~(1 << chan->hc_num);
Antti Seppälä95c8bc32015-08-20 21:41:07 +0300763 dwc2_writel(haintmsk, hsotg->regs + HAINTMSK);
Paul Zimmerman7359d482013-03-11 17:47:59 -0700764
765 /* Try to queue more transfers now that there's a free channel */
766 tr_type = dwc2_hcd_select_transactions(hsotg);
767 if (tr_type != DWC2_TRANSACTION_NONE)
768 dwc2_hcd_queue_transactions(hsotg, tr_type);
769}
770
771/*
772 * Halts a host channel. If the channel cannot be halted immediately because
773 * the request queue is full, this function ensures that the FIFO empty
774 * interrupt for the appropriate queue is enabled so that the halt request can
775 * be queued when there is space in the request queue.
776 *
777 * This function may also be called in DMA mode. In that case, the channel is
778 * simply released since the core always halts the channel automatically in
779 * DMA mode.
780 */
781static void dwc2_halt_channel(struct dwc2_hsotg *hsotg,
782 struct dwc2_host_chan *chan, struct dwc2_qtd *qtd,
783 enum dwc2_halt_status halt_status)
784{
Matthijs Kooijmanb49977a2013-04-10 09:55:50 +0200785 if (dbg_hc(chan))
786 dev_vdbg(hsotg->dev, "%s()\n", __func__);
Paul Zimmerman7359d482013-03-11 17:47:59 -0700787
788 if (hsotg->core_params->dma_enable > 0) {
Matthijs Kooijmanb49977a2013-04-10 09:55:50 +0200789 if (dbg_hc(chan))
790 dev_vdbg(hsotg->dev, "DMA enabled\n");
Paul Zimmerman7359d482013-03-11 17:47:59 -0700791 dwc2_release_channel(hsotg, chan, qtd, halt_status);
792 return;
793 }
794
795 /* Slave mode processing */
796 dwc2_hc_halt(hsotg, chan, halt_status);
797
798 if (chan->halt_on_queue) {
799 u32 gintmsk;
800
801 dev_vdbg(hsotg->dev, "Halt on queue\n");
802 if (chan->ep_type == USB_ENDPOINT_XFER_CONTROL ||
803 chan->ep_type == USB_ENDPOINT_XFER_BULK) {
804 dev_vdbg(hsotg->dev, "control/bulk\n");
805 /*
806 * Make sure the Non-periodic Tx FIFO empty interrupt
807 * is enabled so that the non-periodic schedule will
808 * be processed
809 */
Antti Seppälä95c8bc32015-08-20 21:41:07 +0300810 gintmsk = dwc2_readl(hsotg->regs + GINTMSK);
Paul Zimmerman7359d482013-03-11 17:47:59 -0700811 gintmsk |= GINTSTS_NPTXFEMP;
Antti Seppälä95c8bc32015-08-20 21:41:07 +0300812 dwc2_writel(gintmsk, hsotg->regs + GINTMSK);
Paul Zimmerman7359d482013-03-11 17:47:59 -0700813 } else {
814 dev_vdbg(hsotg->dev, "isoc/intr\n");
815 /*
816 * Move the QH from the periodic queued schedule to
817 * the periodic assigned schedule. This allows the
818 * halt to be queued when the periodic schedule is
819 * processed.
820 */
Douglas Anderson94ef7ae2016-01-28 18:19:56 -0800821 list_move_tail(&chan->qh->qh_list_entry,
Paul Zimmerman7359d482013-03-11 17:47:59 -0700822 &hsotg->periodic_sched_assigned);
823
824 /*
825 * Make sure the Periodic Tx FIFO Empty interrupt is
826 * enabled so that the periodic schedule will be
827 * processed
828 */
Antti Seppälä95c8bc32015-08-20 21:41:07 +0300829 gintmsk = dwc2_readl(hsotg->regs + GINTMSK);
Paul Zimmerman7359d482013-03-11 17:47:59 -0700830 gintmsk |= GINTSTS_PTXFEMP;
Antti Seppälä95c8bc32015-08-20 21:41:07 +0300831 dwc2_writel(gintmsk, hsotg->regs + GINTMSK);
Paul Zimmerman7359d482013-03-11 17:47:59 -0700832 }
833 }
834}
835
836/*
837 * Performs common cleanup for non-periodic transfers after a Transfer
838 * Complete interrupt. This function should be called after any endpoint type
839 * specific handling is finished to release the host channel.
840 */
841static void dwc2_complete_non_periodic_xfer(struct dwc2_hsotg *hsotg,
842 struct dwc2_host_chan *chan,
843 int chnum, struct dwc2_qtd *qtd,
844 enum dwc2_halt_status halt_status)
845{
846 dev_vdbg(hsotg->dev, "%s()\n", __func__);
847
848 qtd->error_count = 0;
849
850 if (chan->hcint & HCINTMSK_NYET) {
851 /*
852 * Got a NYET on the last transaction of the transfer. This
853 * means that the endpoint should be in the PING state at the
854 * beginning of the next transfer.
855 */
856 dev_vdbg(hsotg->dev, "got NYET\n");
857 chan->qh->ping_state = 1;
858 }
859
860 /*
861 * Always halt and release the host channel to make it available for
862 * more transfers. There may still be more phases for a control
863 * transfer or more data packets for a bulk transfer at this point,
864 * but the host channel is still halted. A channel will be reassigned
865 * to the transfer when the non-periodic schedule is processed after
866 * the channel is released. This allows transactions to be queued
867 * properly via dwc2_hcd_queue_transactions, which also enables the
868 * Tx FIFO Empty interrupt if necessary.
869 */
870 if (chan->ep_is_in) {
871 /*
872 * IN transfers in Slave mode require an explicit disable to
873 * halt the channel. (In DMA mode, this call simply releases
874 * the channel.)
875 */
876 dwc2_halt_channel(hsotg, chan, qtd, halt_status);
877 } else {
878 /*
879 * The channel is automatically disabled by the core for OUT
880 * transfers in Slave mode
881 */
882 dwc2_release_channel(hsotg, chan, qtd, halt_status);
883 }
884}
885
886/*
887 * Performs common cleanup for periodic transfers after a Transfer Complete
888 * interrupt. This function should be called after any endpoint type specific
889 * handling is finished to release the host channel.
890 */
891static void dwc2_complete_periodic_xfer(struct dwc2_hsotg *hsotg,
892 struct dwc2_host_chan *chan, int chnum,
893 struct dwc2_qtd *qtd,
894 enum dwc2_halt_status halt_status)
895{
Antti Seppälä95c8bc32015-08-20 21:41:07 +0300896 u32 hctsiz = dwc2_readl(hsotg->regs + HCTSIZ(chnum));
Paul Zimmerman7359d482013-03-11 17:47:59 -0700897
898 qtd->error_count = 0;
899
900 if (!chan->ep_is_in || (hctsiz & TSIZ_PKTCNT_MASK) == 0)
901 /* Core halts channel in these cases */
902 dwc2_release_channel(hsotg, chan, qtd, halt_status);
903 else
904 /* Flush any outstanding requests from the Tx queue */
905 dwc2_halt_channel(hsotg, chan, qtd, halt_status);
906}
907
908static int dwc2_xfercomp_isoc_split_in(struct dwc2_hsotg *hsotg,
909 struct dwc2_host_chan *chan, int chnum,
910 struct dwc2_qtd *qtd)
911{
912 struct dwc2_hcd_iso_packet_desc *frame_desc;
913 u32 len;
914
915 if (!qtd->urb)
916 return 0;
917
918 frame_desc = &qtd->urb->iso_descs[qtd->isoc_frame_index];
919 len = dwc2_get_actual_xfer_length(hsotg, chan, chnum, qtd,
920 DWC2_HC_XFER_COMPLETE, NULL);
921 if (!len) {
922 qtd->complete_split = 0;
923 qtd->isoc_split_offset = 0;
924 return 0;
925 }
926
927 frame_desc->actual_length += len;
928
Paul Zimmerman7359d482013-03-11 17:47:59 -0700929 qtd->isoc_split_offset += len;
930
931 if (frame_desc->actual_length >= frame_desc->length) {
932 frame_desc->status = 0;
933 qtd->isoc_frame_index++;
934 qtd->complete_split = 0;
935 qtd->isoc_split_offset = 0;
936 }
937
938 if (qtd->isoc_frame_index == qtd->urb->packet_count) {
Paul Zimmerman0d012b92013-07-13 14:53:48 -0700939 dwc2_host_complete(hsotg, qtd, 0);
Paul Zimmerman7359d482013-03-11 17:47:59 -0700940 dwc2_release_channel(hsotg, chan, qtd,
941 DWC2_HC_XFER_URB_COMPLETE);
942 } else {
943 dwc2_release_channel(hsotg, chan, qtd,
944 DWC2_HC_XFER_NO_HALT_STATUS);
945 }
946
947 return 1; /* Indicates that channel released */
948}
949
950/*
951 * Handles a host channel Transfer Complete interrupt. This handler may be
952 * called in either DMA mode or Slave mode.
953 */
954static void dwc2_hc_xfercomp_intr(struct dwc2_hsotg *hsotg,
955 struct dwc2_host_chan *chan, int chnum,
956 struct dwc2_qtd *qtd)
957{
958 struct dwc2_hcd_urb *urb = qtd->urb;
Paul Zimmerman7359d482013-03-11 17:47:59 -0700959 enum dwc2_halt_status halt_status = DWC2_HC_XFER_COMPLETE;
Paul Zimmerman2b54fa62014-02-12 17:44:35 -0800960 int pipe_type;
Paul Zimmerman7359d482013-03-11 17:47:59 -0700961 int urb_xfer_done;
962
Matthijs Kooijmanb49977a2013-04-10 09:55:50 +0200963 if (dbg_hc(chan))
964 dev_vdbg(hsotg->dev,
965 "--Host Channel %d Interrupt: Transfer Complete--\n",
966 chnum);
Paul Zimmerman7359d482013-03-11 17:47:59 -0700967
Paul Zimmerman2b54fa62014-02-12 17:44:35 -0800968 if (!urb)
969 goto handle_xfercomp_done;
970
971 pipe_type = dwc2_hcd_get_pipe_type(&urb->pipe_info);
972
Paul Zimmerman7359d482013-03-11 17:47:59 -0700973 if (hsotg->core_params->dma_desc_enable > 0) {
974 dwc2_hcd_complete_xfer_ddma(hsotg, chan, chnum, halt_status);
975 if (pipe_type == USB_ENDPOINT_XFER_ISOC)
976 /* Do not disable the interrupt, just clear it */
977 return;
978 goto handle_xfercomp_done;
979 }
980
981 /* Handle xfer complete on CSPLIT */
982 if (chan->qh->do_split) {
983 if (chan->ep_type == USB_ENDPOINT_XFER_ISOC && chan->ep_is_in &&
984 hsotg->core_params->dma_enable > 0) {
985 if (qtd->complete_split &&
986 dwc2_xfercomp_isoc_split_in(hsotg, chan, chnum,
987 qtd))
988 goto handle_xfercomp_done;
989 } else {
990 qtd->complete_split = 0;
991 }
992 }
993
Paul Zimmerman7359d482013-03-11 17:47:59 -0700994 /* Update the QTD and URB states */
995 switch (pipe_type) {
996 case USB_ENDPOINT_XFER_CONTROL:
997 switch (qtd->control_phase) {
998 case DWC2_CONTROL_SETUP:
999 if (urb->length > 0)
1000 qtd->control_phase = DWC2_CONTROL_DATA;
1001 else
1002 qtd->control_phase = DWC2_CONTROL_STATUS;
1003 dev_vdbg(hsotg->dev,
1004 " Control setup transaction done\n");
1005 halt_status = DWC2_HC_XFER_COMPLETE;
1006 break;
1007 case DWC2_CONTROL_DATA:
1008 urb_xfer_done = dwc2_update_urb_state(hsotg, chan,
1009 chnum, urb, qtd);
1010 if (urb_xfer_done) {
1011 qtd->control_phase = DWC2_CONTROL_STATUS;
1012 dev_vdbg(hsotg->dev,
1013 " Control data transfer done\n");
1014 } else {
1015 dwc2_hcd_save_data_toggle(hsotg, chan, chnum,
1016 qtd);
1017 }
1018 halt_status = DWC2_HC_XFER_COMPLETE;
1019 break;
1020 case DWC2_CONTROL_STATUS:
1021 dev_vdbg(hsotg->dev, " Control transfer complete\n");
1022 if (urb->status == -EINPROGRESS)
1023 urb->status = 0;
Paul Zimmerman0d012b92013-07-13 14:53:48 -07001024 dwc2_host_complete(hsotg, qtd, urb->status);
Paul Zimmerman7359d482013-03-11 17:47:59 -07001025 halt_status = DWC2_HC_XFER_URB_COMPLETE;
1026 break;
1027 }
1028
1029 dwc2_complete_non_periodic_xfer(hsotg, chan, chnum, qtd,
1030 halt_status);
1031 break;
1032 case USB_ENDPOINT_XFER_BULK:
1033 dev_vdbg(hsotg->dev, " Bulk transfer complete\n");
1034 urb_xfer_done = dwc2_update_urb_state(hsotg, chan, chnum, urb,
1035 qtd);
1036 if (urb_xfer_done) {
Paul Zimmerman0d012b92013-07-13 14:53:48 -07001037 dwc2_host_complete(hsotg, qtd, urb->status);
Paul Zimmerman7359d482013-03-11 17:47:59 -07001038 halt_status = DWC2_HC_XFER_URB_COMPLETE;
1039 } else {
1040 halt_status = DWC2_HC_XFER_COMPLETE;
1041 }
1042
1043 dwc2_hcd_save_data_toggle(hsotg, chan, chnum, qtd);
1044 dwc2_complete_non_periodic_xfer(hsotg, chan, chnum, qtd,
1045 halt_status);
1046 break;
1047 case USB_ENDPOINT_XFER_INT:
1048 dev_vdbg(hsotg->dev, " Interrupt transfer complete\n");
1049 urb_xfer_done = dwc2_update_urb_state(hsotg, chan, chnum, urb,
1050 qtd);
1051
1052 /*
1053 * Interrupt URB is done on the first transfer complete
1054 * interrupt
1055 */
1056 if (urb_xfer_done) {
Paul Zimmerman0d012b92013-07-13 14:53:48 -07001057 dwc2_host_complete(hsotg, qtd, urb->status);
1058 halt_status = DWC2_HC_XFER_URB_COMPLETE;
Paul Zimmerman7359d482013-03-11 17:47:59 -07001059 } else {
Paul Zimmerman0d012b92013-07-13 14:53:48 -07001060 halt_status = DWC2_HC_XFER_COMPLETE;
Paul Zimmerman7359d482013-03-11 17:47:59 -07001061 }
1062
1063 dwc2_hcd_save_data_toggle(hsotg, chan, chnum, qtd);
1064 dwc2_complete_periodic_xfer(hsotg, chan, chnum, qtd,
1065 halt_status);
1066 break;
1067 case USB_ENDPOINT_XFER_ISOC:
Matthijs Kooijmanb49977a2013-04-10 09:55:50 +02001068 if (dbg_perio())
1069 dev_vdbg(hsotg->dev, " Isochronous transfer complete\n");
Paul Zimmerman7359d482013-03-11 17:47:59 -07001070 if (qtd->isoc_split_pos == DWC2_HCSPLT_XACTPOS_ALL)
1071 halt_status = dwc2_update_isoc_urb_state(hsotg, chan,
1072 chnum, qtd, DWC2_HC_XFER_COMPLETE);
1073 dwc2_complete_periodic_xfer(hsotg, chan, chnum, qtd,
1074 halt_status);
1075 break;
1076 }
1077
1078handle_xfercomp_done:
1079 disable_hc_int(hsotg, chnum, HCINTMSK_XFERCOMPL);
1080}
1081
1082/*
1083 * Handles a host channel STALL interrupt. This handler may be called in
1084 * either DMA mode or Slave mode.
1085 */
1086static void dwc2_hc_stall_intr(struct dwc2_hsotg *hsotg,
1087 struct dwc2_host_chan *chan, int chnum,
1088 struct dwc2_qtd *qtd)
1089{
1090 struct dwc2_hcd_urb *urb = qtd->urb;
Paul Zimmerman2b54fa62014-02-12 17:44:35 -08001091 int pipe_type;
Paul Zimmerman7359d482013-03-11 17:47:59 -07001092
1093 dev_dbg(hsotg->dev, "--Host Channel %d Interrupt: STALL Received--\n",
1094 chnum);
1095
1096 if (hsotg->core_params->dma_desc_enable > 0) {
1097 dwc2_hcd_complete_xfer_ddma(hsotg, chan, chnum,
1098 DWC2_HC_XFER_STALL);
1099 goto handle_stall_done;
1100 }
1101
1102 if (!urb)
1103 goto handle_stall_halt;
1104
Paul Zimmerman2b54fa62014-02-12 17:44:35 -08001105 pipe_type = dwc2_hcd_get_pipe_type(&urb->pipe_info);
1106
Paul Zimmerman7359d482013-03-11 17:47:59 -07001107 if (pipe_type == USB_ENDPOINT_XFER_CONTROL)
Paul Zimmerman0d012b92013-07-13 14:53:48 -07001108 dwc2_host_complete(hsotg, qtd, -EPIPE);
Paul Zimmerman7359d482013-03-11 17:47:59 -07001109
1110 if (pipe_type == USB_ENDPOINT_XFER_BULK ||
1111 pipe_type == USB_ENDPOINT_XFER_INT) {
Paul Zimmerman0d012b92013-07-13 14:53:48 -07001112 dwc2_host_complete(hsotg, qtd, -EPIPE);
Paul Zimmerman7359d482013-03-11 17:47:59 -07001113 /*
1114 * USB protocol requires resetting the data toggle for bulk
1115 * and interrupt endpoints when a CLEAR_FEATURE(ENDPOINT_HALT)
1116 * setup command is issued to the endpoint. Anticipate the
1117 * CLEAR_FEATURE command since a STALL has occurred and reset
1118 * the data toggle now.
1119 */
1120 chan->qh->data_toggle = 0;
1121 }
1122
1123handle_stall_halt:
1124 dwc2_halt_channel(hsotg, chan, qtd, DWC2_HC_XFER_STALL);
1125
1126handle_stall_done:
1127 disable_hc_int(hsotg, chnum, HCINTMSK_STALL);
1128}
1129
1130/*
1131 * Updates the state of the URB when a transfer has been stopped due to an
1132 * abnormal condition before the transfer completes. Modifies the
1133 * actual_length field of the URB to reflect the number of bytes that have
1134 * actually been transferred via the host channel.
1135 */
1136static void dwc2_update_urb_state_abn(struct dwc2_hsotg *hsotg,
1137 struct dwc2_host_chan *chan, int chnum,
1138 struct dwc2_hcd_urb *urb,
1139 struct dwc2_qtd *qtd,
1140 enum dwc2_halt_status halt_status)
1141{
1142 u32 xfer_length = dwc2_get_actual_xfer_length(hsotg, chan, chnum,
1143 qtd, halt_status, NULL);
1144 u32 hctsiz;
1145
1146 if (urb->actual_length + xfer_length > urb->length) {
1147 dev_warn(hsotg->dev, "%s(): trimming xfer length\n", __func__);
1148 xfer_length = urb->length - urb->actual_length;
1149 }
1150
Paul Zimmerman7359d482013-03-11 17:47:59 -07001151 urb->actual_length += xfer_length;
1152
Antti Seppälä95c8bc32015-08-20 21:41:07 +03001153 hctsiz = dwc2_readl(hsotg->regs + HCTSIZ(chnum));
Paul Zimmerman7359d482013-03-11 17:47:59 -07001154 dev_vdbg(hsotg->dev, "DWC_otg: %s: %s, channel %d\n",
1155 __func__, (chan->ep_is_in ? "IN" : "OUT"), chnum);
1156 dev_vdbg(hsotg->dev, " chan->start_pkt_count %d\n",
1157 chan->start_pkt_count);
1158 dev_vdbg(hsotg->dev, " hctsiz.pktcnt %d\n",
Matthijs Kooijmand6ec53e2013-08-30 18:45:15 +02001159 (hctsiz & TSIZ_PKTCNT_MASK) >> TSIZ_PKTCNT_SHIFT);
Paul Zimmerman7359d482013-03-11 17:47:59 -07001160 dev_vdbg(hsotg->dev, " chan->max_packet %d\n", chan->max_packet);
1161 dev_vdbg(hsotg->dev, " bytes_transferred %d\n",
1162 xfer_length);
1163 dev_vdbg(hsotg->dev, " urb->actual_length %d\n",
1164 urb->actual_length);
1165 dev_vdbg(hsotg->dev, " urb->transfer_buffer_length %d\n",
1166 urb->length);
1167}
1168
1169/*
1170 * Handles a host channel NAK interrupt. This handler may be called in either
1171 * DMA mode or Slave mode.
1172 */
1173static void dwc2_hc_nak_intr(struct dwc2_hsotg *hsotg,
1174 struct dwc2_host_chan *chan, int chnum,
1175 struct dwc2_qtd *qtd)
1176{
Gregory Herreroe4991232015-04-29 22:09:20 +02001177 if (!qtd) {
1178 dev_dbg(hsotg->dev, "%s: qtd is NULL\n", __func__);
1179 return;
1180 }
1181
1182 if (!qtd->urb) {
1183 dev_dbg(hsotg->dev, "%s: qtd->urb is NULL\n", __func__);
1184 return;
1185 }
1186
Matthijs Kooijmanb49977a2013-04-10 09:55:50 +02001187 if (dbg_hc(chan))
1188 dev_vdbg(hsotg->dev, "--Host Channel %d Interrupt: NAK Received--\n",
1189 chnum);
Paul Zimmerman7359d482013-03-11 17:47:59 -07001190
1191 /*
1192 * Handle NAK for IN/OUT SSPLIT/CSPLIT transfers, bulk, control, and
1193 * interrupt. Re-start the SSPLIT transfer.
1194 */
1195 if (chan->do_split) {
1196 if (chan->complete_split)
1197 qtd->error_count = 0;
1198 qtd->complete_split = 0;
1199 dwc2_halt_channel(hsotg, chan, qtd, DWC2_HC_XFER_NAK);
1200 goto handle_nak_done;
1201 }
1202
1203 switch (dwc2_hcd_get_pipe_type(&qtd->urb->pipe_info)) {
1204 case USB_ENDPOINT_XFER_CONTROL:
1205 case USB_ENDPOINT_XFER_BULK:
1206 if (hsotg->core_params->dma_enable > 0 && chan->ep_is_in) {
1207 /*
1208 * NAK interrupts are enabled on bulk/control IN
1209 * transfers in DMA mode for the sole purpose of
1210 * resetting the error count after a transaction error
1211 * occurs. The core will continue transferring data.
1212 */
1213 qtd->error_count = 0;
1214 break;
1215 }
1216
1217 /*
1218 * NAK interrupts normally occur during OUT transfers in DMA
1219 * or Slave mode. For IN transfers, more requests will be
1220 * queued as request queue space is available.
1221 */
1222 qtd->error_count = 0;
1223
1224 if (!chan->qh->ping_state) {
1225 dwc2_update_urb_state_abn(hsotg, chan, chnum, qtd->urb,
1226 qtd, DWC2_HC_XFER_NAK);
1227 dwc2_hcd_save_data_toggle(hsotg, chan, chnum, qtd);
1228
1229 if (chan->speed == USB_SPEED_HIGH)
1230 chan->qh->ping_state = 1;
1231 }
1232
1233 /*
1234 * Halt the channel so the transfer can be re-started from
1235 * the appropriate point or the PING protocol will
1236 * start/continue
1237 */
1238 dwc2_halt_channel(hsotg, chan, qtd, DWC2_HC_XFER_NAK);
1239 break;
1240 case USB_ENDPOINT_XFER_INT:
1241 qtd->error_count = 0;
1242 dwc2_halt_channel(hsotg, chan, qtd, DWC2_HC_XFER_NAK);
1243 break;
1244 case USB_ENDPOINT_XFER_ISOC:
1245 /* Should never get called for isochronous transfers */
1246 dev_err(hsotg->dev, "NACK interrupt for ISOC transfer\n");
1247 break;
1248 }
1249
1250handle_nak_done:
1251 disable_hc_int(hsotg, chnum, HCINTMSK_NAK);
1252}
1253
1254/*
1255 * Handles a host channel ACK interrupt. This interrupt is enabled when
1256 * performing the PING protocol in Slave mode, when errors occur during
1257 * either Slave mode or DMA mode, and during Start Split transactions.
1258 */
1259static void dwc2_hc_ack_intr(struct dwc2_hsotg *hsotg,
1260 struct dwc2_host_chan *chan, int chnum,
1261 struct dwc2_qtd *qtd)
1262{
1263 struct dwc2_hcd_iso_packet_desc *frame_desc;
1264
Matthijs Kooijmanb49977a2013-04-10 09:55:50 +02001265 if (dbg_hc(chan))
1266 dev_vdbg(hsotg->dev, "--Host Channel %d Interrupt: ACK Received--\n",
1267 chnum);
Paul Zimmerman7359d482013-03-11 17:47:59 -07001268
1269 if (chan->do_split) {
1270 /* Handle ACK on SSPLIT. ACK should not occur in CSPLIT. */
1271 if (!chan->ep_is_in &&
1272 chan->data_pid_start != DWC2_HC_PID_SETUP)
1273 qtd->ssplit_out_xfer_count = chan->xfer_len;
1274
1275 if (chan->ep_type != USB_ENDPOINT_XFER_ISOC || chan->ep_is_in) {
1276 qtd->complete_split = 1;
1277 dwc2_halt_channel(hsotg, chan, qtd, DWC2_HC_XFER_ACK);
1278 } else {
1279 /* ISOC OUT */
1280 switch (chan->xact_pos) {
1281 case DWC2_HCSPLT_XACTPOS_ALL:
1282 break;
1283 case DWC2_HCSPLT_XACTPOS_END:
1284 qtd->isoc_split_pos = DWC2_HCSPLT_XACTPOS_ALL;
1285 qtd->isoc_split_offset = 0;
1286 break;
1287 case DWC2_HCSPLT_XACTPOS_BEGIN:
1288 case DWC2_HCSPLT_XACTPOS_MID:
1289 /*
1290 * For BEGIN or MID, calculate the length for
1291 * the next microframe to determine the correct
1292 * SSPLIT token, either MID or END
1293 */
1294 frame_desc = &qtd->urb->iso_descs[
1295 qtd->isoc_frame_index];
1296 qtd->isoc_split_offset += 188;
1297
1298 if (frame_desc->length - qtd->isoc_split_offset
1299 <= 188)
1300 qtd->isoc_split_pos =
1301 DWC2_HCSPLT_XACTPOS_END;
1302 else
1303 qtd->isoc_split_pos =
1304 DWC2_HCSPLT_XACTPOS_MID;
1305 break;
1306 }
1307 }
1308 } else {
1309 qtd->error_count = 0;
1310
1311 if (chan->qh->ping_state) {
1312 chan->qh->ping_state = 0;
1313 /*
1314 * Halt the channel so the transfer can be re-started
1315 * from the appropriate point. This only happens in
1316 * Slave mode. In DMA mode, the ping_state is cleared
1317 * when the transfer is started because the core
1318 * automatically executes the PING, then the transfer.
1319 */
1320 dwc2_halt_channel(hsotg, chan, qtd, DWC2_HC_XFER_ACK);
1321 }
1322 }
1323
1324 /*
1325 * If the ACK occurred when _not_ in the PING state, let the channel
1326 * continue transferring data after clearing the error count
1327 */
1328 disable_hc_int(hsotg, chnum, HCINTMSK_ACK);
1329}
1330
1331/*
1332 * Handles a host channel NYET interrupt. This interrupt should only occur on
1333 * Bulk and Control OUT endpoints and for complete split transactions. If a
1334 * NYET occurs at the same time as a Transfer Complete interrupt, it is
1335 * handled in the xfercomp interrupt handler, not here. This handler may be
1336 * called in either DMA mode or Slave mode.
1337 */
1338static void dwc2_hc_nyet_intr(struct dwc2_hsotg *hsotg,
1339 struct dwc2_host_chan *chan, int chnum,
1340 struct dwc2_qtd *qtd)
1341{
Matthijs Kooijmanb49977a2013-04-10 09:55:50 +02001342 if (dbg_hc(chan))
1343 dev_vdbg(hsotg->dev, "--Host Channel %d Interrupt: NYET Received--\n",
1344 chnum);
Paul Zimmerman7359d482013-03-11 17:47:59 -07001345
1346 /*
1347 * NYET on CSPLIT
1348 * re-do the CSPLIT immediately on non-periodic
1349 */
1350 if (chan->do_split && chan->complete_split) {
1351 if (chan->ep_is_in && chan->ep_type == USB_ENDPOINT_XFER_ISOC &&
1352 hsotg->core_params->dma_enable > 0) {
1353 qtd->complete_split = 0;
1354 qtd->isoc_split_offset = 0;
Paul Zimmerman0d012b92013-07-13 14:53:48 -07001355 qtd->isoc_frame_index++;
Paul Zimmerman7902c162013-04-22 14:00:18 -07001356 if (qtd->urb &&
Paul Zimmerman0d012b92013-07-13 14:53:48 -07001357 qtd->isoc_frame_index == qtd->urb->packet_count) {
1358 dwc2_host_complete(hsotg, qtd, 0);
Paul Zimmerman7359d482013-03-11 17:47:59 -07001359 dwc2_release_channel(hsotg, chan, qtd,
Paul Zimmerman7902c162013-04-22 14:00:18 -07001360 DWC2_HC_XFER_URB_COMPLETE);
Paul Zimmerman7359d482013-03-11 17:47:59 -07001361 } else {
1362 dwc2_release_channel(hsotg, chan, qtd,
1363 DWC2_HC_XFER_NO_HALT_STATUS);
1364 }
1365 goto handle_nyet_done;
1366 }
1367
1368 if (chan->ep_type == USB_ENDPOINT_XFER_INT ||
1369 chan->ep_type == USB_ENDPOINT_XFER_ISOC) {
1370 int frnum = dwc2_hcd_get_frame_number(hsotg);
1371
1372 if (dwc2_full_frame_num(frnum) !=
Douglas Andersonced9eee2016-01-28 18:20:04 -08001373 dwc2_full_frame_num(chan->qh->next_active_frame)) {
Paul Zimmerman7359d482013-03-11 17:47:59 -07001374 /*
1375 * No longer in the same full speed frame.
1376 * Treat this as a transaction error.
1377 */
1378#if 0
1379 /*
1380 * Todo: Fix system performance so this can
1381 * be treated as an error. Right now complete
1382 * splits cannot be scheduled precisely enough
1383 * due to other system activity, so this error
1384 * occurs regularly in Slave mode.
1385 */
1386 qtd->error_count++;
1387#endif
1388 qtd->complete_split = 0;
1389 dwc2_halt_channel(hsotg, chan, qtd,
1390 DWC2_HC_XFER_XACT_ERR);
1391 /* Todo: add support for isoc release */
1392 goto handle_nyet_done;
1393 }
1394 }
1395
1396 dwc2_halt_channel(hsotg, chan, qtd, DWC2_HC_XFER_NYET);
1397 goto handle_nyet_done;
1398 }
1399
1400 chan->qh->ping_state = 1;
1401 qtd->error_count = 0;
1402
1403 dwc2_update_urb_state_abn(hsotg, chan, chnum, qtd->urb, qtd,
1404 DWC2_HC_XFER_NYET);
1405 dwc2_hcd_save_data_toggle(hsotg, chan, chnum, qtd);
1406
1407 /*
1408 * Halt the channel and re-start the transfer so the PING protocol
1409 * will start
1410 */
1411 dwc2_halt_channel(hsotg, chan, qtd, DWC2_HC_XFER_NYET);
1412
1413handle_nyet_done:
1414 disable_hc_int(hsotg, chnum, HCINTMSK_NYET);
1415}
1416
1417/*
1418 * Handles a host channel babble interrupt. This handler may be called in
1419 * either DMA mode or Slave mode.
1420 */
1421static void dwc2_hc_babble_intr(struct dwc2_hsotg *hsotg,
1422 struct dwc2_host_chan *chan, int chnum,
1423 struct dwc2_qtd *qtd)
1424{
1425 dev_dbg(hsotg->dev, "--Host Channel %d Interrupt: Babble Error--\n",
1426 chnum);
1427
Paul Zimmerman0d012b92013-07-13 14:53:48 -07001428 dwc2_hc_handle_tt_clear(hsotg, chan, qtd);
1429
Paul Zimmerman7359d482013-03-11 17:47:59 -07001430 if (hsotg->core_params->dma_desc_enable > 0) {
1431 dwc2_hcd_complete_xfer_ddma(hsotg, chan, chnum,
1432 DWC2_HC_XFER_BABBLE_ERR);
Paul Zimmerman0d012b92013-07-13 14:53:48 -07001433 goto disable_int;
Paul Zimmerman7359d482013-03-11 17:47:59 -07001434 }
1435
1436 if (chan->ep_type != USB_ENDPOINT_XFER_ISOC) {
Paul Zimmerman0d012b92013-07-13 14:53:48 -07001437 dwc2_host_complete(hsotg, qtd, -EOVERFLOW);
Paul Zimmerman7359d482013-03-11 17:47:59 -07001438 dwc2_halt_channel(hsotg, chan, qtd, DWC2_HC_XFER_BABBLE_ERR);
1439 } else {
1440 enum dwc2_halt_status halt_status;
1441
1442 halt_status = dwc2_update_isoc_urb_state(hsotg, chan, chnum,
1443 qtd, DWC2_HC_XFER_BABBLE_ERR);
1444 dwc2_halt_channel(hsotg, chan, qtd, halt_status);
1445 }
1446
Paul Zimmerman0d012b92013-07-13 14:53:48 -07001447disable_int:
Paul Zimmerman7359d482013-03-11 17:47:59 -07001448 disable_hc_int(hsotg, chnum, HCINTMSK_BBLERR);
1449}
1450
1451/*
1452 * Handles a host channel AHB error interrupt. This handler is only called in
1453 * DMA mode.
1454 */
1455static void dwc2_hc_ahberr_intr(struct dwc2_hsotg *hsotg,
1456 struct dwc2_host_chan *chan, int chnum,
1457 struct dwc2_qtd *qtd)
1458{
1459 struct dwc2_hcd_urb *urb = qtd->urb;
1460 char *pipetype, *speed;
1461 u32 hcchar;
1462 u32 hcsplt;
1463 u32 hctsiz;
1464 u32 hc_dma;
1465
1466 dev_dbg(hsotg->dev, "--Host Channel %d Interrupt: AHB Error--\n",
1467 chnum);
1468
1469 if (!urb)
1470 goto handle_ahberr_halt;
1471
Paul Zimmerman0d012b92013-07-13 14:53:48 -07001472 dwc2_hc_handle_tt_clear(hsotg, chan, qtd);
1473
Antti Seppälä95c8bc32015-08-20 21:41:07 +03001474 hcchar = dwc2_readl(hsotg->regs + HCCHAR(chnum));
1475 hcsplt = dwc2_readl(hsotg->regs + HCSPLT(chnum));
1476 hctsiz = dwc2_readl(hsotg->regs + HCTSIZ(chnum));
1477 hc_dma = dwc2_readl(hsotg->regs + HCDMA(chnum));
Paul Zimmerman7359d482013-03-11 17:47:59 -07001478
1479 dev_err(hsotg->dev, "AHB ERROR, Channel %d\n", chnum);
1480 dev_err(hsotg->dev, " hcchar 0x%08x, hcsplt 0x%08x\n", hcchar, hcsplt);
1481 dev_err(hsotg->dev, " hctsiz 0x%08x, hc_dma 0x%08x\n", hctsiz, hc_dma);
1482 dev_err(hsotg->dev, " Device address: %d\n",
1483 dwc2_hcd_get_dev_addr(&urb->pipe_info));
1484 dev_err(hsotg->dev, " Endpoint: %d, %s\n",
1485 dwc2_hcd_get_ep_num(&urb->pipe_info),
1486 dwc2_hcd_is_pipe_in(&urb->pipe_info) ? "IN" : "OUT");
1487
1488 switch (dwc2_hcd_get_pipe_type(&urb->pipe_info)) {
1489 case USB_ENDPOINT_XFER_CONTROL:
1490 pipetype = "CONTROL";
1491 break;
1492 case USB_ENDPOINT_XFER_BULK:
1493 pipetype = "BULK";
1494 break;
1495 case USB_ENDPOINT_XFER_INT:
1496 pipetype = "INTERRUPT";
1497 break;
1498 case USB_ENDPOINT_XFER_ISOC:
1499 pipetype = "ISOCHRONOUS";
1500 break;
1501 default:
1502 pipetype = "UNKNOWN";
1503 break;
1504 }
1505
1506 dev_err(hsotg->dev, " Endpoint type: %s\n", pipetype);
1507
1508 switch (chan->speed) {
1509 case USB_SPEED_HIGH:
1510 speed = "HIGH";
1511 break;
1512 case USB_SPEED_FULL:
1513 speed = "FULL";
1514 break;
1515 case USB_SPEED_LOW:
1516 speed = "LOW";
1517 break;
1518 default:
1519 speed = "UNKNOWN";
1520 break;
1521 }
1522
1523 dev_err(hsotg->dev, " Speed: %s\n", speed);
1524
1525 dev_err(hsotg->dev, " Max packet size: %d\n",
1526 dwc2_hcd_get_mps(&urb->pipe_info));
1527 dev_err(hsotg->dev, " Data buffer length: %d\n", urb->length);
Paul Zimmerman157dfaa2013-03-14 13:12:00 -07001528 dev_err(hsotg->dev, " Transfer buffer: %p, Transfer DMA: %08lx\n",
1529 urb->buf, (unsigned long)urb->dma);
1530 dev_err(hsotg->dev, " Setup buffer: %p, Setup DMA: %08lx\n",
1531 urb->setup_packet, (unsigned long)urb->setup_dma);
Paul Zimmerman7359d482013-03-11 17:47:59 -07001532 dev_err(hsotg->dev, " Interval: %d\n", urb->interval);
1533
1534 /* Core halts the channel for Descriptor DMA mode */
1535 if (hsotg->core_params->dma_desc_enable > 0) {
1536 dwc2_hcd_complete_xfer_ddma(hsotg, chan, chnum,
1537 DWC2_HC_XFER_AHB_ERR);
1538 goto handle_ahberr_done;
1539 }
1540
Paul Zimmerman0d012b92013-07-13 14:53:48 -07001541 dwc2_host_complete(hsotg, qtd, -EIO);
Paul Zimmerman7359d482013-03-11 17:47:59 -07001542
1543handle_ahberr_halt:
1544 /*
1545 * Force a channel halt. Don't call dwc2_halt_channel because that won't
1546 * write to the HCCHARn register in DMA mode to force the halt.
1547 */
1548 dwc2_hc_halt(hsotg, chan, DWC2_HC_XFER_AHB_ERR);
1549
1550handle_ahberr_done:
Paul Zimmerman7359d482013-03-11 17:47:59 -07001551 disable_hc_int(hsotg, chnum, HCINTMSK_AHBERR);
1552}
1553
1554/*
1555 * Handles a host channel transaction error interrupt. This handler may be
1556 * called in either DMA mode or Slave mode.
1557 */
1558static void dwc2_hc_xacterr_intr(struct dwc2_hsotg *hsotg,
1559 struct dwc2_host_chan *chan, int chnum,
1560 struct dwc2_qtd *qtd)
1561{
1562 dev_dbg(hsotg->dev,
1563 "--Host Channel %d Interrupt: Transaction Error--\n", chnum);
1564
Paul Zimmerman0d012b92013-07-13 14:53:48 -07001565 dwc2_hc_handle_tt_clear(hsotg, chan, qtd);
1566
Paul Zimmerman7359d482013-03-11 17:47:59 -07001567 if (hsotg->core_params->dma_desc_enable > 0) {
1568 dwc2_hcd_complete_xfer_ddma(hsotg, chan, chnum,
1569 DWC2_HC_XFER_XACT_ERR);
1570 goto handle_xacterr_done;
1571 }
1572
1573 switch (dwc2_hcd_get_pipe_type(&qtd->urb->pipe_info)) {
1574 case USB_ENDPOINT_XFER_CONTROL:
1575 case USB_ENDPOINT_XFER_BULK:
1576 qtd->error_count++;
1577 if (!chan->qh->ping_state) {
1578
1579 dwc2_update_urb_state_abn(hsotg, chan, chnum, qtd->urb,
1580 qtd, DWC2_HC_XFER_XACT_ERR);
1581 dwc2_hcd_save_data_toggle(hsotg, chan, chnum, qtd);
1582 if (!chan->ep_is_in && chan->speed == USB_SPEED_HIGH)
1583 chan->qh->ping_state = 1;
1584 }
1585
1586 /*
1587 * Halt the channel so the transfer can be re-started from
1588 * the appropriate point or the PING protocol will start
1589 */
1590 dwc2_halt_channel(hsotg, chan, qtd, DWC2_HC_XFER_XACT_ERR);
1591 break;
1592 case USB_ENDPOINT_XFER_INT:
1593 qtd->error_count++;
1594 if (chan->do_split && chan->complete_split)
1595 qtd->complete_split = 0;
1596 dwc2_halt_channel(hsotg, chan, qtd, DWC2_HC_XFER_XACT_ERR);
1597 break;
1598 case USB_ENDPOINT_XFER_ISOC:
1599 {
1600 enum dwc2_halt_status halt_status;
1601
1602 halt_status = dwc2_update_isoc_urb_state(hsotg, chan,
1603 chnum, qtd, DWC2_HC_XFER_XACT_ERR);
1604 dwc2_halt_channel(hsotg, chan, qtd, halt_status);
1605 }
1606 break;
1607 }
1608
1609handle_xacterr_done:
Paul Zimmerman7359d482013-03-11 17:47:59 -07001610 disable_hc_int(hsotg, chnum, HCINTMSK_XACTERR);
1611}
1612
1613/*
1614 * Handles a host channel frame overrun interrupt. This handler may be called
1615 * in either DMA mode or Slave mode.
1616 */
1617static void dwc2_hc_frmovrun_intr(struct dwc2_hsotg *hsotg,
1618 struct dwc2_host_chan *chan, int chnum,
1619 struct dwc2_qtd *qtd)
1620{
1621 enum dwc2_halt_status halt_status;
1622
Matthijs Kooijmanb49977a2013-04-10 09:55:50 +02001623 if (dbg_hc(chan))
1624 dev_dbg(hsotg->dev, "--Host Channel %d Interrupt: Frame Overrun--\n",
1625 chnum);
Paul Zimmerman7359d482013-03-11 17:47:59 -07001626
Paul Zimmerman0d012b92013-07-13 14:53:48 -07001627 dwc2_hc_handle_tt_clear(hsotg, chan, qtd);
1628
Paul Zimmerman7359d482013-03-11 17:47:59 -07001629 switch (dwc2_hcd_get_pipe_type(&qtd->urb->pipe_info)) {
1630 case USB_ENDPOINT_XFER_CONTROL:
1631 case USB_ENDPOINT_XFER_BULK:
1632 break;
1633 case USB_ENDPOINT_XFER_INT:
1634 dwc2_halt_channel(hsotg, chan, qtd, DWC2_HC_XFER_FRAME_OVERRUN);
1635 break;
1636 case USB_ENDPOINT_XFER_ISOC:
1637 halt_status = dwc2_update_isoc_urb_state(hsotg, chan, chnum,
1638 qtd, DWC2_HC_XFER_FRAME_OVERRUN);
1639 dwc2_halt_channel(hsotg, chan, qtd, halt_status);
1640 break;
1641 }
1642
Paul Zimmerman7359d482013-03-11 17:47:59 -07001643 disable_hc_int(hsotg, chnum, HCINTMSK_FRMOVRUN);
1644}
1645
1646/*
1647 * Handles a host channel data toggle error interrupt. This handler may be
1648 * called in either DMA mode or Slave mode.
1649 */
1650static void dwc2_hc_datatglerr_intr(struct dwc2_hsotg *hsotg,
1651 struct dwc2_host_chan *chan, int chnum,
1652 struct dwc2_qtd *qtd)
1653{
1654 dev_dbg(hsotg->dev,
1655 "--Host Channel %d Interrupt: Data Toggle Error--\n", chnum);
1656
1657 if (chan->ep_is_in)
1658 qtd->error_count = 0;
1659 else
1660 dev_err(hsotg->dev,
1661 "Data Toggle Error on OUT transfer, channel %d\n",
1662 chnum);
1663
1664 dwc2_hc_handle_tt_clear(hsotg, chan, qtd);
1665 disable_hc_int(hsotg, chnum, HCINTMSK_DATATGLERR);
1666}
1667
1668/*
1669 * For debug only. It checks that a valid halt status is set and that
1670 * HCCHARn.chdis is clear. If there's a problem, corrective action is
1671 * taken and a warning is issued.
1672 *
1673 * Return: true if halt status is ok, false otherwise
1674 */
1675static bool dwc2_halt_status_ok(struct dwc2_hsotg *hsotg,
1676 struct dwc2_host_chan *chan, int chnum,
1677 struct dwc2_qtd *qtd)
1678{
1679#ifdef DEBUG
1680 u32 hcchar;
1681 u32 hctsiz;
1682 u32 hcintmsk;
1683 u32 hcsplt;
1684
1685 if (chan->halt_status == DWC2_HC_XFER_NO_HALT_STATUS) {
1686 /*
1687 * This code is here only as a check. This condition should
1688 * never happen. Ignore the halt if it does occur.
1689 */
Antti Seppälä95c8bc32015-08-20 21:41:07 +03001690 hcchar = dwc2_readl(hsotg->regs + HCCHAR(chnum));
1691 hctsiz = dwc2_readl(hsotg->regs + HCTSIZ(chnum));
1692 hcintmsk = dwc2_readl(hsotg->regs + HCINTMSK(chnum));
1693 hcsplt = dwc2_readl(hsotg->regs + HCSPLT(chnum));
Paul Zimmerman7359d482013-03-11 17:47:59 -07001694 dev_dbg(hsotg->dev,
1695 "%s: chan->halt_status DWC2_HC_XFER_NO_HALT_STATUS,\n",
1696 __func__);
1697 dev_dbg(hsotg->dev,
1698 "channel %d, hcchar 0x%08x, hctsiz 0x%08x,\n",
1699 chnum, hcchar, hctsiz);
1700 dev_dbg(hsotg->dev,
1701 "hcint 0x%08x, hcintmsk 0x%08x, hcsplt 0x%08x,\n",
1702 chan->hcint, hcintmsk, hcsplt);
Matthijs Kooijman8509f2f2013-03-25 12:00:25 -07001703 if (qtd)
1704 dev_dbg(hsotg->dev, "qtd->complete_split %d\n",
1705 qtd->complete_split);
Paul Zimmerman7359d482013-03-11 17:47:59 -07001706 dev_warn(hsotg->dev,
1707 "%s: no halt status, channel %d, ignoring interrupt\n",
1708 __func__, chnum);
1709 return false;
1710 }
1711
1712 /*
1713 * This code is here only as a check. hcchar.chdis should never be set
1714 * when the halt interrupt occurs. Halt the channel again if it does
1715 * occur.
1716 */
Antti Seppälä95c8bc32015-08-20 21:41:07 +03001717 hcchar = dwc2_readl(hsotg->regs + HCCHAR(chnum));
Paul Zimmerman7359d482013-03-11 17:47:59 -07001718 if (hcchar & HCCHAR_CHDIS) {
1719 dev_warn(hsotg->dev,
1720 "%s: hcchar.chdis set unexpectedly, hcchar 0x%08x, trying to halt again\n",
1721 __func__, hcchar);
1722 chan->halt_pending = 0;
1723 dwc2_halt_channel(hsotg, chan, qtd, chan->halt_status);
1724 return false;
1725 }
1726#endif
1727
1728 return true;
1729}
1730
1731/*
1732 * Handles a host Channel Halted interrupt in DMA mode. This handler
1733 * determines the reason the channel halted and proceeds accordingly.
1734 */
1735static void dwc2_hc_chhltd_intr_dma(struct dwc2_hsotg *hsotg,
1736 struct dwc2_host_chan *chan, int chnum,
1737 struct dwc2_qtd *qtd)
1738{
1739 u32 hcintmsk;
1740 int out_nak_enh = 0;
1741
Matthijs Kooijmanb49977a2013-04-10 09:55:50 +02001742 if (dbg_hc(chan))
1743 dev_vdbg(hsotg->dev,
1744 "--Host Channel %d Interrupt: DMA Channel Halted--\n",
1745 chnum);
Paul Zimmerman7359d482013-03-11 17:47:59 -07001746
1747 /*
1748 * For core with OUT NAK enhancement, the flow for high-speed
1749 * CONTROL/BULK OUT is handled a little differently
1750 */
Matthijs Kooijman9badec22013-08-30 18:45:21 +02001751 if (hsotg->hw_params.snpsid >= DWC2_CORE_REV_2_71a) {
Paul Zimmerman7359d482013-03-11 17:47:59 -07001752 if (chan->speed == USB_SPEED_HIGH && !chan->ep_is_in &&
1753 (chan->ep_type == USB_ENDPOINT_XFER_CONTROL ||
1754 chan->ep_type == USB_ENDPOINT_XFER_BULK)) {
1755 out_nak_enh = 1;
1756 }
1757 }
1758
1759 if (chan->halt_status == DWC2_HC_XFER_URB_DEQUEUE ||
1760 (chan->halt_status == DWC2_HC_XFER_AHB_ERR &&
1761 hsotg->core_params->dma_desc_enable <= 0)) {
1762 if (hsotg->core_params->dma_desc_enable > 0)
1763 dwc2_hcd_complete_xfer_ddma(hsotg, chan, chnum,
1764 chan->halt_status);
1765 else
1766 /*
1767 * Just release the channel. A dequeue can happen on a
1768 * transfer timeout. In the case of an AHB Error, the
1769 * channel was forced to halt because there's no way to
1770 * gracefully recover.
1771 */
1772 dwc2_release_channel(hsotg, chan, qtd,
1773 chan->halt_status);
1774 return;
1775 }
1776
Antti Seppälä95c8bc32015-08-20 21:41:07 +03001777 hcintmsk = dwc2_readl(hsotg->regs + HCINTMSK(chnum));
Paul Zimmerman7359d482013-03-11 17:47:59 -07001778
1779 if (chan->hcint & HCINTMSK_XFERCOMPL) {
1780 /*
1781 * Todo: This is here because of a possible hardware bug. Spec
1782 * says that on SPLIT-ISOC OUT transfers in DMA mode that a HALT
1783 * interrupt w/ACK bit set should occur, but I only see the
1784 * XFERCOMP bit, even with it masked out. This is a workaround
1785 * for that behavior. Should fix this when hardware is fixed.
1786 */
1787 if (chan->ep_type == USB_ENDPOINT_XFER_ISOC && !chan->ep_is_in)
1788 dwc2_hc_ack_intr(hsotg, chan, chnum, qtd);
1789 dwc2_hc_xfercomp_intr(hsotg, chan, chnum, qtd);
1790 } else if (chan->hcint & HCINTMSK_STALL) {
1791 dwc2_hc_stall_intr(hsotg, chan, chnum, qtd);
1792 } else if ((chan->hcint & HCINTMSK_XACTERR) &&
1793 hsotg->core_params->dma_desc_enable <= 0) {
1794 if (out_nak_enh) {
1795 if (chan->hcint &
1796 (HCINTMSK_NYET | HCINTMSK_NAK | HCINTMSK_ACK)) {
1797 dev_vdbg(hsotg->dev,
1798 "XactErr with NYET/NAK/ACK\n");
1799 qtd->error_count = 0;
1800 } else {
1801 dev_vdbg(hsotg->dev,
1802 "XactErr without NYET/NAK/ACK\n");
1803 }
1804 }
1805
1806 /*
1807 * Must handle xacterr before nak or ack. Could get a xacterr
1808 * at the same time as either of these on a BULK/CONTROL OUT
1809 * that started with a PING. The xacterr takes precedence.
1810 */
1811 dwc2_hc_xacterr_intr(hsotg, chan, chnum, qtd);
1812 } else if ((chan->hcint & HCINTMSK_XCS_XACT) &&
1813 hsotg->core_params->dma_desc_enable > 0) {
1814 dwc2_hc_xacterr_intr(hsotg, chan, chnum, qtd);
1815 } else if ((chan->hcint & HCINTMSK_AHBERR) &&
1816 hsotg->core_params->dma_desc_enable > 0) {
1817 dwc2_hc_ahberr_intr(hsotg, chan, chnum, qtd);
1818 } else if (chan->hcint & HCINTMSK_BBLERR) {
1819 dwc2_hc_babble_intr(hsotg, chan, chnum, qtd);
1820 } else if (chan->hcint & HCINTMSK_FRMOVRUN) {
1821 dwc2_hc_frmovrun_intr(hsotg, chan, chnum, qtd);
1822 } else if (!out_nak_enh) {
1823 if (chan->hcint & HCINTMSK_NYET) {
1824 /*
1825 * Must handle nyet before nak or ack. Could get a nyet
1826 * at the same time as either of those on a BULK/CONTROL
1827 * OUT that started with a PING. The nyet takes
1828 * precedence.
1829 */
1830 dwc2_hc_nyet_intr(hsotg, chan, chnum, qtd);
1831 } else if ((chan->hcint & HCINTMSK_NAK) &&
1832 !(hcintmsk & HCINTMSK_NAK)) {
1833 /*
1834 * If nak is not masked, it's because a non-split IN
1835 * transfer is in an error state. In that case, the nak
1836 * is handled by the nak interrupt handler, not here.
1837 * Handle nak here for BULK/CONTROL OUT transfers, which
1838 * halt on a NAK to allow rewinding the buffer pointer.
1839 */
1840 dwc2_hc_nak_intr(hsotg, chan, chnum, qtd);
1841 } else if ((chan->hcint & HCINTMSK_ACK) &&
1842 !(hcintmsk & HCINTMSK_ACK)) {
1843 /*
1844 * If ack is not masked, it's because a non-split IN
1845 * transfer is in an error state. In that case, the ack
1846 * is handled by the ack interrupt handler, not here.
1847 * Handle ack here for split transfers. Start splits
1848 * halt on ACK.
1849 */
1850 dwc2_hc_ack_intr(hsotg, chan, chnum, qtd);
1851 } else {
1852 if (chan->ep_type == USB_ENDPOINT_XFER_INT ||
1853 chan->ep_type == USB_ENDPOINT_XFER_ISOC) {
1854 /*
1855 * A periodic transfer halted with no other
1856 * channel interrupts set. Assume it was halted
1857 * by the core because it could not be completed
1858 * in its scheduled (micro)frame.
1859 */
1860 dev_dbg(hsotg->dev,
1861 "%s: Halt channel %d (assume incomplete periodic transfer)\n",
1862 __func__, chnum);
1863 dwc2_halt_channel(hsotg, chan, qtd,
1864 DWC2_HC_XFER_PERIODIC_INCOMPLETE);
1865 } else {
1866 dev_err(hsotg->dev,
1867 "%s: Channel %d - ChHltd set, but reason is unknown\n",
1868 __func__, chnum);
1869 dev_err(hsotg->dev,
1870 "hcint 0x%08x, intsts 0x%08x\n",
1871 chan->hcint,
Antti Seppälä95c8bc32015-08-20 21:41:07 +03001872 dwc2_readl(hsotg->regs + GINTSTS));
Nick Hudson151d0cb2014-09-11 15:22:48 -07001873 goto error;
Paul Zimmerman7359d482013-03-11 17:47:59 -07001874 }
1875 }
1876 } else {
1877 dev_info(hsotg->dev,
1878 "NYET/NAK/ACK/other in non-error case, 0x%08x\n",
1879 chan->hcint);
Nick Hudson151d0cb2014-09-11 15:22:48 -07001880error:
1881 /* Failthrough: use 3-strikes rule */
1882 qtd->error_count++;
1883 dwc2_update_urb_state_abn(hsotg, chan, chnum, qtd->urb,
1884 qtd, DWC2_HC_XFER_XACT_ERR);
1885 dwc2_hcd_save_data_toggle(hsotg, chan, chnum, qtd);
1886 dwc2_halt_channel(hsotg, chan, qtd, DWC2_HC_XFER_XACT_ERR);
Paul Zimmerman7359d482013-03-11 17:47:59 -07001887 }
1888}
1889
1890/*
1891 * Handles a host channel Channel Halted interrupt
1892 *
1893 * In slave mode, this handler is called only when the driver specifically
1894 * requests a halt. This occurs during handling other host channel interrupts
1895 * (e.g. nak, xacterr, stall, nyet, etc.).
1896 *
1897 * In DMA mode, this is the interrupt that occurs when the core has finished
1898 * processing a transfer on a channel. Other host channel interrupts (except
1899 * ahberr) are disabled in DMA mode.
1900 */
1901static void dwc2_hc_chhltd_intr(struct dwc2_hsotg *hsotg,
1902 struct dwc2_host_chan *chan, int chnum,
1903 struct dwc2_qtd *qtd)
1904{
Matthijs Kooijmanb49977a2013-04-10 09:55:50 +02001905 if (dbg_hc(chan))
1906 dev_vdbg(hsotg->dev, "--Host Channel %d Interrupt: Channel Halted--\n",
1907 chnum);
Paul Zimmerman7359d482013-03-11 17:47:59 -07001908
1909 if (hsotg->core_params->dma_enable > 0) {
1910 dwc2_hc_chhltd_intr_dma(hsotg, chan, chnum, qtd);
1911 } else {
1912 if (!dwc2_halt_status_ok(hsotg, chan, chnum, qtd))
1913 return;
1914 dwc2_release_channel(hsotg, chan, qtd, chan->halt_status);
1915 }
1916}
1917
Doug Andersondc873082015-10-16 16:01:32 -07001918/*
1919 * Check if the given qtd is still the top of the list (and thus valid).
1920 *
1921 * If dwc2_hcd_qtd_unlink_and_free() has been called since we grabbed
1922 * the qtd from the top of the list, this will return false (otherwise true).
1923 */
1924static bool dwc2_check_qtd_still_ok(struct dwc2_qtd *qtd, struct dwc2_qh *qh)
1925{
1926 struct dwc2_qtd *cur_head;
1927
1928 if (qh == NULL)
1929 return false;
1930
1931 cur_head = list_first_entry(&qh->qtd_list, struct dwc2_qtd,
1932 qtd_list_entry);
1933 return (cur_head == qtd);
1934}
1935
Paul Zimmerman7359d482013-03-11 17:47:59 -07001936/* Handles interrupt for a specific Host Channel */
1937static void dwc2_hc_n_intr(struct dwc2_hsotg *hsotg, int chnum)
1938{
1939 struct dwc2_qtd *qtd;
1940 struct dwc2_host_chan *chan;
1941 u32 hcint, hcintmsk;
1942
Matthijs Kooijmanb49977a2013-04-10 09:55:50 +02001943 chan = hsotg->hc_ptr_array[chnum];
1944
Antti Seppälä95c8bc32015-08-20 21:41:07 +03001945 hcint = dwc2_readl(hsotg->regs + HCINT(chnum));
1946 hcintmsk = dwc2_readl(hsotg->regs + HCINTMSK(chnum));
Paul Zimmerman7359d482013-03-11 17:47:59 -07001947 if (!chan) {
1948 dev_err(hsotg->dev, "## hc_ptr_array for channel is NULL ##\n");
Antti Seppälä95c8bc32015-08-20 21:41:07 +03001949 dwc2_writel(hcint, hsotg->regs + HCINT(chnum));
Paul Zimmerman7359d482013-03-11 17:47:59 -07001950 return;
1951 }
1952
Rashika Kheria723a2312013-10-30 04:16:55 +05301953 if (dbg_hc(chan)) {
1954 dev_vdbg(hsotg->dev, "--Host Channel Interrupt--, Channel %d\n",
1955 chnum);
1956 dev_vdbg(hsotg->dev,
1957 " hcint 0x%08x, hcintmsk 0x%08x, hcint&hcintmsk 0x%08x\n",
1958 hcint, hcintmsk, hcint & hcintmsk);
1959 }
1960
Antti Seppälä95c8bc32015-08-20 21:41:07 +03001961 dwc2_writel(hcint, hsotg->regs + HCINT(chnum));
Douglas Anderson16e80212016-01-28 18:19:55 -08001962
1963 /*
1964 * If we got an interrupt after someone called
1965 * dwc2_hcd_endpoint_disable() we don't want to crash below
1966 */
1967 if (!chan->qh) {
1968 dev_warn(hsotg->dev, "Interrupt on disabled channel\n");
1969 return;
1970 }
1971
Paul Zimmerman7359d482013-03-11 17:47:59 -07001972 chan->hcint = hcint;
1973 hcint &= hcintmsk;
1974
Matthijs Kooijman8509f2f2013-03-25 12:00:25 -07001975 /*
1976 * If the channel was halted due to a dequeue, the qtd list might
1977 * be empty or at least the first entry will not be the active qtd.
1978 * In this case, take a shortcut and just release the channel.
1979 */
1980 if (chan->halt_status == DWC2_HC_XFER_URB_DEQUEUE) {
1981 /*
1982 * If the channel was halted, this should be the only
1983 * interrupt unmasked
1984 */
1985 WARN_ON(hcint != HCINTMSK_CHHLTD);
1986 if (hsotg->core_params->dma_desc_enable > 0)
1987 dwc2_hcd_complete_xfer_ddma(hsotg, chan, chnum,
1988 chan->halt_status);
1989 else
1990 dwc2_release_channel(hsotg, chan, NULL,
1991 chan->halt_status);
1992 return;
1993 }
1994
Paul Zimmerman7359d482013-03-11 17:47:59 -07001995 if (list_empty(&chan->qh->qtd_list)) {
Matthijs Kooijman8509f2f2013-03-25 12:00:25 -07001996 /*
1997 * TODO: Will this ever happen with the
1998 * DWC2_HC_XFER_URB_DEQUEUE handling above?
1999 */
Paul Zimmerman7359d482013-03-11 17:47:59 -07002000 dev_dbg(hsotg->dev, "## no QTD queued for channel %d ##\n",
2001 chnum);
2002 dev_dbg(hsotg->dev,
2003 " hcint 0x%08x, hcintmsk 0x%08x, hcint&hcintmsk 0x%08x\n",
2004 chan->hcint, hcintmsk, hcint);
2005 chan->halt_status = DWC2_HC_XFER_NO_HALT_STATUS;
2006 disable_hc_int(hsotg, chnum, HCINTMSK_CHHLTD);
2007 chan->hcint = 0;
2008 return;
2009 }
2010
2011 qtd = list_first_entry(&chan->qh->qtd_list, struct dwc2_qtd,
2012 qtd_list_entry);
2013
2014 if (hsotg->core_params->dma_enable <= 0) {
2015 if ((hcint & HCINTMSK_CHHLTD) && hcint != HCINTMSK_CHHLTD)
2016 hcint &= ~HCINTMSK_CHHLTD;
2017 }
2018
2019 if (hcint & HCINTMSK_XFERCOMPL) {
2020 dwc2_hc_xfercomp_intr(hsotg, chan, chnum, qtd);
2021 /*
2022 * If NYET occurred at same time as Xfer Complete, the NYET is
2023 * handled by the Xfer Complete interrupt handler. Don't want
2024 * to call the NYET interrupt handler in this case.
2025 */
2026 hcint &= ~HCINTMSK_NYET;
2027 }
Paul Zimmerman7359d482013-03-11 17:47:59 -07002028
Doug Andersondc873082015-10-16 16:01:32 -07002029 if (hcint & HCINTMSK_CHHLTD) {
2030 dwc2_hc_chhltd_intr(hsotg, chan, chnum, qtd);
2031 if (!dwc2_check_qtd_still_ok(qtd, chan->qh))
2032 goto exit;
2033 }
2034 if (hcint & HCINTMSK_AHBERR) {
2035 dwc2_hc_ahberr_intr(hsotg, chan, chnum, qtd);
2036 if (!dwc2_check_qtd_still_ok(qtd, chan->qh))
2037 goto exit;
2038 }
2039 if (hcint & HCINTMSK_STALL) {
2040 dwc2_hc_stall_intr(hsotg, chan, chnum, qtd);
2041 if (!dwc2_check_qtd_still_ok(qtd, chan->qh))
2042 goto exit;
2043 }
2044 if (hcint & HCINTMSK_NAK) {
2045 dwc2_hc_nak_intr(hsotg, chan, chnum, qtd);
2046 if (!dwc2_check_qtd_still_ok(qtd, chan->qh))
2047 goto exit;
2048 }
2049 if (hcint & HCINTMSK_ACK) {
2050 dwc2_hc_ack_intr(hsotg, chan, chnum, qtd);
2051 if (!dwc2_check_qtd_still_ok(qtd, chan->qh))
2052 goto exit;
2053 }
2054 if (hcint & HCINTMSK_NYET) {
2055 dwc2_hc_nyet_intr(hsotg, chan, chnum, qtd);
2056 if (!dwc2_check_qtd_still_ok(qtd, chan->qh))
2057 goto exit;
2058 }
2059 if (hcint & HCINTMSK_XACTERR) {
2060 dwc2_hc_xacterr_intr(hsotg, chan, chnum, qtd);
2061 if (!dwc2_check_qtd_still_ok(qtd, chan->qh))
2062 goto exit;
2063 }
2064 if (hcint & HCINTMSK_BBLERR) {
2065 dwc2_hc_babble_intr(hsotg, chan, chnum, qtd);
2066 if (!dwc2_check_qtd_still_ok(qtd, chan->qh))
2067 goto exit;
2068 }
2069 if (hcint & HCINTMSK_FRMOVRUN) {
2070 dwc2_hc_frmovrun_intr(hsotg, chan, chnum, qtd);
2071 if (!dwc2_check_qtd_still_ok(qtd, chan->qh))
2072 goto exit;
2073 }
2074 if (hcint & HCINTMSK_DATATGLERR) {
2075 dwc2_hc_datatglerr_intr(hsotg, chan, chnum, qtd);
2076 if (!dwc2_check_qtd_still_ok(qtd, chan->qh))
2077 goto exit;
2078 }
2079
2080exit:
Paul Zimmerman7359d482013-03-11 17:47:59 -07002081 chan->hcint = 0;
2082}
2083
2084/*
2085 * This interrupt indicates that one or more host channels has a pending
2086 * interrupt. There are multiple conditions that can cause each host channel
2087 * interrupt. This function determines which conditions have occurred for each
2088 * host channel interrupt and handles them appropriately.
2089 */
2090static void dwc2_hc_intr(struct dwc2_hsotg *hsotg)
2091{
2092 u32 haint;
2093 int i;
Douglas Andersonc9c8ac02016-01-28 18:19:57 -08002094 struct dwc2_host_chan *chan, *chan_tmp;
Paul Zimmerman7359d482013-03-11 17:47:59 -07002095
Antti Seppälä95c8bc32015-08-20 21:41:07 +03002096 haint = dwc2_readl(hsotg->regs + HAINT);
Matthijs Kooijmanb49977a2013-04-10 09:55:50 +02002097 if (dbg_perio()) {
2098 dev_vdbg(hsotg->dev, "%s()\n", __func__);
2099
2100 dev_vdbg(hsotg->dev, "HAINT=%08x\n", haint);
2101 }
Paul Zimmerman7359d482013-03-11 17:47:59 -07002102
Douglas Andersonc9c8ac02016-01-28 18:19:57 -08002103 /*
2104 * According to USB 2.0 spec section 11.18.8, a host must
2105 * issue complete-split transactions in a microframe for a
2106 * set of full-/low-speed endpoints in the same relative
2107 * order as the start-splits were issued in a microframe for.
2108 */
2109 list_for_each_entry_safe(chan, chan_tmp, &hsotg->split_order,
2110 split_order_list_entry) {
2111 int hc_num = chan->hc_num;
2112
2113 if (haint & (1 << hc_num)) {
2114 dwc2_hc_n_intr(hsotg, hc_num);
2115 haint &= ~(1 << hc_num);
2116 }
2117 }
2118
Paul Zimmerman7359d482013-03-11 17:47:59 -07002119 for (i = 0; i < hsotg->core_params->host_channels; i++) {
2120 if (haint & (1 << i))
2121 dwc2_hc_n_intr(hsotg, i);
2122 }
2123}
2124
2125/* This function handles interrupts for the HCD */
Matthijs Kooijmanca18f4a2013-04-25 23:39:15 +02002126irqreturn_t dwc2_handle_hcd_intr(struct dwc2_hsotg *hsotg)
Paul Zimmerman7359d482013-03-11 17:47:59 -07002127{
Matthijs Kooijmanb49977a2013-04-10 09:55:50 +02002128 u32 gintsts, dbg_gintsts;
Matthijs Kooijman6aafb002013-04-25 23:39:14 +02002129 irqreturn_t retval = IRQ_NONE;
Paul Zimmerman7359d482013-03-11 17:47:59 -07002130
Paul Zimmerman54216ac2013-11-25 13:42:44 -08002131 if (!dwc2_is_controller_alive(hsotg)) {
Paul Zimmerman057715f2013-11-22 16:43:51 -08002132 dev_warn(hsotg->dev, "Controller is dead\n");
Matthijs Kooijman6aafb002013-04-25 23:39:14 +02002133 return retval;
Paul Zimmerman7359d482013-03-11 17:47:59 -07002134 }
2135
2136 spin_lock(&hsotg->lock);
2137
2138 /* Check if HOST Mode */
2139 if (dwc2_is_host_mode(hsotg)) {
2140 gintsts = dwc2_read_core_intr(hsotg);
2141 if (!gintsts) {
2142 spin_unlock(&hsotg->lock);
Matthijs Kooijman6aafb002013-04-25 23:39:14 +02002143 return retval;
Paul Zimmerman7359d482013-03-11 17:47:59 -07002144 }
2145
Matthijs Kooijman6aafb002013-04-25 23:39:14 +02002146 retval = IRQ_HANDLED;
Paul Zimmerman7359d482013-03-11 17:47:59 -07002147
Matthijs Kooijmanb49977a2013-04-10 09:55:50 +02002148 dbg_gintsts = gintsts;
Paul Zimmerman7359d482013-03-11 17:47:59 -07002149#ifndef DEBUG_SOF
Matthijs Kooijmanb49977a2013-04-10 09:55:50 +02002150 dbg_gintsts &= ~GINTSTS_SOF;
Paul Zimmerman7359d482013-03-11 17:47:59 -07002151#endif
Matthijs Kooijmanb49977a2013-04-10 09:55:50 +02002152 if (!dbg_perio())
2153 dbg_gintsts &= ~(GINTSTS_HCHINT | GINTSTS_RXFLVL |
2154 GINTSTS_PTXFEMP);
2155
2156 /* Only print if there are any non-suppressed interrupts left */
2157 if (dbg_gintsts)
Paul Zimmerman7359d482013-03-11 17:47:59 -07002158 dev_vdbg(hsotg->dev,
2159 "DWC OTG HCD Interrupt Detected gintsts&gintmsk=0x%08x\n",
2160 gintsts);
2161
2162 if (gintsts & GINTSTS_SOF)
2163 dwc2_sof_intr(hsotg);
2164 if (gintsts & GINTSTS_RXFLVL)
2165 dwc2_rx_fifo_level_intr(hsotg);
2166 if (gintsts & GINTSTS_NPTXFEMP)
2167 dwc2_np_tx_fifo_empty_intr(hsotg);
Paul Zimmerman7359d482013-03-11 17:47:59 -07002168 if (gintsts & GINTSTS_PRTINT)
2169 dwc2_port_intr(hsotg);
2170 if (gintsts & GINTSTS_HCHINT)
2171 dwc2_hc_intr(hsotg);
2172 if (gintsts & GINTSTS_PTXFEMP)
2173 dwc2_perio_tx_fifo_empty_intr(hsotg);
2174
Matthijs Kooijmanb49977a2013-04-10 09:55:50 +02002175 if (dbg_gintsts) {
Paul Zimmerman7359d482013-03-11 17:47:59 -07002176 dev_vdbg(hsotg->dev,
2177 "DWC OTG HCD Finished Servicing Interrupts\n");
2178 dev_vdbg(hsotg->dev,
2179 "DWC OTG HCD gintsts=0x%08x gintmsk=0x%08x\n",
Antti Seppälä95c8bc32015-08-20 21:41:07 +03002180 dwc2_readl(hsotg->regs + GINTSTS),
2181 dwc2_readl(hsotg->regs + GINTMSK));
Paul Zimmerman7359d482013-03-11 17:47:59 -07002182 }
Paul Zimmerman7359d482013-03-11 17:47:59 -07002183 }
2184
2185 spin_unlock(&hsotg->lock);
2186
2187 return retval;
2188}