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