xref: /openbmc/linux/drivers/usb/dwc2/hcd_ddma.c (revision dde4c1bf5df0f852e497e5644d3578885b969fdb)
1197ba5f4SPaul Zimmerman /*
2197ba5f4SPaul Zimmerman  * hcd_ddma.c - DesignWare HS OTG Controller descriptor DMA routines
3197ba5f4SPaul Zimmerman  *
4197ba5f4SPaul Zimmerman  * Copyright (C) 2004-2013 Synopsys, Inc.
5197ba5f4SPaul Zimmerman  *
6197ba5f4SPaul Zimmerman  * Redistribution and use in source and binary forms, with or without
7197ba5f4SPaul Zimmerman  * modification, are permitted provided that the following conditions
8197ba5f4SPaul Zimmerman  * are met:
9197ba5f4SPaul Zimmerman  * 1. Redistributions of source code must retain the above copyright
10197ba5f4SPaul Zimmerman  *    notice, this list of conditions, and the following disclaimer,
11197ba5f4SPaul Zimmerman  *    without modification.
12197ba5f4SPaul Zimmerman  * 2. Redistributions in binary form must reproduce the above copyright
13197ba5f4SPaul Zimmerman  *    notice, this list of conditions and the following disclaimer in the
14197ba5f4SPaul Zimmerman  *    documentation and/or other materials provided with the distribution.
15197ba5f4SPaul Zimmerman  * 3. The names of the above-listed copyright holders may not be used
16197ba5f4SPaul Zimmerman  *    to endorse or promote products derived from this software without
17197ba5f4SPaul Zimmerman  *    specific prior written permission.
18197ba5f4SPaul Zimmerman  *
19197ba5f4SPaul Zimmerman  * ALTERNATIVELY, this software may be distributed under the terms of the
20197ba5f4SPaul Zimmerman  * GNU General Public License ("GPL") as published by the Free Software
21197ba5f4SPaul Zimmerman  * Foundation; either version 2 of the License, or (at your option) any
22197ba5f4SPaul Zimmerman  * later version.
23197ba5f4SPaul Zimmerman  *
24197ba5f4SPaul Zimmerman  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
25197ba5f4SPaul Zimmerman  * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
26197ba5f4SPaul Zimmerman  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
27197ba5f4SPaul Zimmerman  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
28197ba5f4SPaul Zimmerman  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
29197ba5f4SPaul Zimmerman  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
30197ba5f4SPaul Zimmerman  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
31197ba5f4SPaul Zimmerman  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
32197ba5f4SPaul Zimmerman  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
33197ba5f4SPaul Zimmerman  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
34197ba5f4SPaul Zimmerman  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35197ba5f4SPaul Zimmerman  */
36197ba5f4SPaul Zimmerman 
37197ba5f4SPaul Zimmerman /*
38197ba5f4SPaul Zimmerman  * This file contains the Descriptor DMA implementation for Host mode
39197ba5f4SPaul Zimmerman  */
40197ba5f4SPaul Zimmerman #include <linux/kernel.h>
41197ba5f4SPaul Zimmerman #include <linux/module.h>
42197ba5f4SPaul Zimmerman #include <linux/spinlock.h>
43197ba5f4SPaul Zimmerman #include <linux/interrupt.h>
44197ba5f4SPaul Zimmerman #include <linux/dma-mapping.h>
45197ba5f4SPaul Zimmerman #include <linux/io.h>
46197ba5f4SPaul Zimmerman #include <linux/slab.h>
47197ba5f4SPaul Zimmerman #include <linux/usb.h>
48197ba5f4SPaul Zimmerman 
49197ba5f4SPaul Zimmerman #include <linux/usb/hcd.h>
50197ba5f4SPaul Zimmerman #include <linux/usb/ch11.h>
51197ba5f4SPaul Zimmerman 
52197ba5f4SPaul Zimmerman #include "core.h"
53197ba5f4SPaul Zimmerman #include "hcd.h"
54197ba5f4SPaul Zimmerman 
55197ba5f4SPaul Zimmerman static u16 dwc2_frame_list_idx(u16 frame)
56197ba5f4SPaul Zimmerman {
57197ba5f4SPaul Zimmerman 	return frame & (FRLISTEN_64_SIZE - 1);
58197ba5f4SPaul Zimmerman }
59197ba5f4SPaul Zimmerman 
60197ba5f4SPaul Zimmerman static u16 dwc2_desclist_idx_inc(u16 idx, u16 inc, u8 speed)
61197ba5f4SPaul Zimmerman {
62197ba5f4SPaul Zimmerman 	return (idx + inc) &
63197ba5f4SPaul Zimmerman 		((speed == USB_SPEED_HIGH ? MAX_DMA_DESC_NUM_HS_ISOC :
64197ba5f4SPaul Zimmerman 		  MAX_DMA_DESC_NUM_GENERIC) - 1);
65197ba5f4SPaul Zimmerman }
66197ba5f4SPaul Zimmerman 
67197ba5f4SPaul Zimmerman static u16 dwc2_desclist_idx_dec(u16 idx, u16 inc, u8 speed)
68197ba5f4SPaul Zimmerman {
69197ba5f4SPaul Zimmerman 	return (idx - inc) &
70197ba5f4SPaul Zimmerman 		((speed == USB_SPEED_HIGH ? MAX_DMA_DESC_NUM_HS_ISOC :
71197ba5f4SPaul Zimmerman 		  MAX_DMA_DESC_NUM_GENERIC) - 1);
72197ba5f4SPaul Zimmerman }
73197ba5f4SPaul Zimmerman 
74197ba5f4SPaul Zimmerman static u16 dwc2_max_desc_num(struct dwc2_qh *qh)
75197ba5f4SPaul Zimmerman {
76197ba5f4SPaul Zimmerman 	return (qh->ep_type == USB_ENDPOINT_XFER_ISOC &&
77197ba5f4SPaul Zimmerman 		qh->dev_speed == USB_SPEED_HIGH) ?
78197ba5f4SPaul Zimmerman 		MAX_DMA_DESC_NUM_HS_ISOC : MAX_DMA_DESC_NUM_GENERIC;
79197ba5f4SPaul Zimmerman }
80197ba5f4SPaul Zimmerman 
81197ba5f4SPaul Zimmerman static u16 dwc2_frame_incr_val(struct dwc2_qh *qh)
82197ba5f4SPaul Zimmerman {
83197ba5f4SPaul Zimmerman 	return qh->dev_speed == USB_SPEED_HIGH ?
84197ba5f4SPaul Zimmerman 	       (qh->interval + 8 - 1) / 8 : qh->interval;
85197ba5f4SPaul Zimmerman }
86197ba5f4SPaul Zimmerman 
87197ba5f4SPaul Zimmerman static int dwc2_desc_list_alloc(struct dwc2_hsotg *hsotg, struct dwc2_qh *qh,
88197ba5f4SPaul Zimmerman 				gfp_t flags)
89197ba5f4SPaul Zimmerman {
90197ba5f4SPaul Zimmerman 	qh->desc_list = dma_alloc_coherent(hsotg->dev,
91197ba5f4SPaul Zimmerman 				sizeof(struct dwc2_hcd_dma_desc) *
92197ba5f4SPaul Zimmerman 				dwc2_max_desc_num(qh), &qh->desc_list_dma,
93197ba5f4SPaul Zimmerman 				flags);
94197ba5f4SPaul Zimmerman 
95197ba5f4SPaul Zimmerman 	if (!qh->desc_list)
96197ba5f4SPaul Zimmerman 		return -ENOMEM;
97197ba5f4SPaul Zimmerman 
98197ba5f4SPaul Zimmerman 	memset(qh->desc_list, 0,
99197ba5f4SPaul Zimmerman 	       sizeof(struct dwc2_hcd_dma_desc) * dwc2_max_desc_num(qh));
100197ba5f4SPaul Zimmerman 
101197ba5f4SPaul Zimmerman 	qh->n_bytes = kzalloc(sizeof(u32) * dwc2_max_desc_num(qh), flags);
102197ba5f4SPaul Zimmerman 	if (!qh->n_bytes) {
103197ba5f4SPaul Zimmerman 		dma_free_coherent(hsotg->dev, sizeof(struct dwc2_hcd_dma_desc)
104197ba5f4SPaul Zimmerman 				  * dwc2_max_desc_num(qh), qh->desc_list,
105197ba5f4SPaul Zimmerman 				  qh->desc_list_dma);
106197ba5f4SPaul Zimmerman 		qh->desc_list = NULL;
107197ba5f4SPaul Zimmerman 		return -ENOMEM;
108197ba5f4SPaul Zimmerman 	}
109197ba5f4SPaul Zimmerman 
110197ba5f4SPaul Zimmerman 	return 0;
111197ba5f4SPaul Zimmerman }
112197ba5f4SPaul Zimmerman 
113197ba5f4SPaul Zimmerman static void dwc2_desc_list_free(struct dwc2_hsotg *hsotg, struct dwc2_qh *qh)
114197ba5f4SPaul Zimmerman {
115197ba5f4SPaul Zimmerman 	if (qh->desc_list) {
116197ba5f4SPaul Zimmerman 		dma_free_coherent(hsotg->dev, sizeof(struct dwc2_hcd_dma_desc)
117197ba5f4SPaul Zimmerman 				  * dwc2_max_desc_num(qh), qh->desc_list,
118197ba5f4SPaul Zimmerman 				  qh->desc_list_dma);
119197ba5f4SPaul Zimmerman 		qh->desc_list = NULL;
120197ba5f4SPaul Zimmerman 	}
121197ba5f4SPaul Zimmerman 
122197ba5f4SPaul Zimmerman 	kfree(qh->n_bytes);
123197ba5f4SPaul Zimmerman 	qh->n_bytes = NULL;
124197ba5f4SPaul Zimmerman }
125197ba5f4SPaul Zimmerman 
126197ba5f4SPaul Zimmerman static int dwc2_frame_list_alloc(struct dwc2_hsotg *hsotg, gfp_t mem_flags)
127197ba5f4SPaul Zimmerman {
128197ba5f4SPaul Zimmerman 	if (hsotg->frame_list)
129197ba5f4SPaul Zimmerman 		return 0;
130197ba5f4SPaul Zimmerman 
131197ba5f4SPaul Zimmerman 	hsotg->frame_list = dma_alloc_coherent(hsotg->dev,
132197ba5f4SPaul Zimmerman 					       4 * FRLISTEN_64_SIZE,
133197ba5f4SPaul Zimmerman 					       &hsotg->frame_list_dma,
134197ba5f4SPaul Zimmerman 					       mem_flags);
135197ba5f4SPaul Zimmerman 	if (!hsotg->frame_list)
136197ba5f4SPaul Zimmerman 		return -ENOMEM;
137197ba5f4SPaul Zimmerman 
138197ba5f4SPaul Zimmerman 	memset(hsotg->frame_list, 0, 4 * FRLISTEN_64_SIZE);
139197ba5f4SPaul Zimmerman 	return 0;
140197ba5f4SPaul Zimmerman }
141197ba5f4SPaul Zimmerman 
142197ba5f4SPaul Zimmerman static void dwc2_frame_list_free(struct dwc2_hsotg *hsotg)
143197ba5f4SPaul Zimmerman {
144197ba5f4SPaul Zimmerman 	u32 *frame_list;
145197ba5f4SPaul Zimmerman 	dma_addr_t frame_list_dma;
146197ba5f4SPaul Zimmerman 	unsigned long flags;
147197ba5f4SPaul Zimmerman 
148197ba5f4SPaul Zimmerman 	spin_lock_irqsave(&hsotg->lock, flags);
149197ba5f4SPaul Zimmerman 
150197ba5f4SPaul Zimmerman 	if (!hsotg->frame_list) {
151197ba5f4SPaul Zimmerman 		spin_unlock_irqrestore(&hsotg->lock, flags);
152197ba5f4SPaul Zimmerman 		return;
153197ba5f4SPaul Zimmerman 	}
154197ba5f4SPaul Zimmerman 
155197ba5f4SPaul Zimmerman 	frame_list = hsotg->frame_list;
156197ba5f4SPaul Zimmerman 	frame_list_dma = hsotg->frame_list_dma;
157197ba5f4SPaul Zimmerman 	hsotg->frame_list = NULL;
158197ba5f4SPaul Zimmerman 
159197ba5f4SPaul Zimmerman 	spin_unlock_irqrestore(&hsotg->lock, flags);
160197ba5f4SPaul Zimmerman 
161197ba5f4SPaul Zimmerman 	dma_free_coherent(hsotg->dev, 4 * FRLISTEN_64_SIZE, frame_list,
162197ba5f4SPaul Zimmerman 			  frame_list_dma);
163197ba5f4SPaul Zimmerman }
164197ba5f4SPaul Zimmerman 
165197ba5f4SPaul Zimmerman static void dwc2_per_sched_enable(struct dwc2_hsotg *hsotg, u32 fr_list_en)
166197ba5f4SPaul Zimmerman {
167197ba5f4SPaul Zimmerman 	u32 hcfg;
168197ba5f4SPaul Zimmerman 	unsigned long flags;
169197ba5f4SPaul Zimmerman 
170197ba5f4SPaul Zimmerman 	spin_lock_irqsave(&hsotg->lock, flags);
171197ba5f4SPaul Zimmerman 
17295c8bc36SAntti Seppälä 	hcfg = dwc2_readl(hsotg->regs + HCFG);
173197ba5f4SPaul Zimmerman 	if (hcfg & HCFG_PERSCHEDENA) {
174197ba5f4SPaul Zimmerman 		/* already enabled */
175197ba5f4SPaul Zimmerman 		spin_unlock_irqrestore(&hsotg->lock, flags);
176197ba5f4SPaul Zimmerman 		return;
177197ba5f4SPaul Zimmerman 	}
178197ba5f4SPaul Zimmerman 
17995c8bc36SAntti Seppälä 	dwc2_writel(hsotg->frame_list_dma, hsotg->regs + HFLBADDR);
180197ba5f4SPaul Zimmerman 
181197ba5f4SPaul Zimmerman 	hcfg &= ~HCFG_FRLISTEN_MASK;
182197ba5f4SPaul Zimmerman 	hcfg |= fr_list_en | HCFG_PERSCHEDENA;
183197ba5f4SPaul Zimmerman 	dev_vdbg(hsotg->dev, "Enabling Periodic schedule\n");
18495c8bc36SAntti Seppälä 	dwc2_writel(hcfg, hsotg->regs + HCFG);
185197ba5f4SPaul Zimmerman 
186197ba5f4SPaul Zimmerman 	spin_unlock_irqrestore(&hsotg->lock, flags);
187197ba5f4SPaul Zimmerman }
188197ba5f4SPaul Zimmerman 
189197ba5f4SPaul Zimmerman static void dwc2_per_sched_disable(struct dwc2_hsotg *hsotg)
190197ba5f4SPaul Zimmerman {
191197ba5f4SPaul Zimmerman 	u32 hcfg;
192197ba5f4SPaul Zimmerman 	unsigned long flags;
193197ba5f4SPaul Zimmerman 
194197ba5f4SPaul Zimmerman 	spin_lock_irqsave(&hsotg->lock, flags);
195197ba5f4SPaul Zimmerman 
19695c8bc36SAntti Seppälä 	hcfg = dwc2_readl(hsotg->regs + HCFG);
197197ba5f4SPaul Zimmerman 	if (!(hcfg & HCFG_PERSCHEDENA)) {
198197ba5f4SPaul Zimmerman 		/* already disabled */
199197ba5f4SPaul Zimmerman 		spin_unlock_irqrestore(&hsotg->lock, flags);
200197ba5f4SPaul Zimmerman 		return;
201197ba5f4SPaul Zimmerman 	}
202197ba5f4SPaul Zimmerman 
203197ba5f4SPaul Zimmerman 	hcfg &= ~HCFG_PERSCHEDENA;
204197ba5f4SPaul Zimmerman 	dev_vdbg(hsotg->dev, "Disabling Periodic schedule\n");
20595c8bc36SAntti Seppälä 	dwc2_writel(hcfg, hsotg->regs + HCFG);
206197ba5f4SPaul Zimmerman 
207197ba5f4SPaul Zimmerman 	spin_unlock_irqrestore(&hsotg->lock, flags);
208197ba5f4SPaul Zimmerman }
209197ba5f4SPaul Zimmerman 
210197ba5f4SPaul Zimmerman /*
211197ba5f4SPaul Zimmerman  * Activates/Deactivates FrameList entries for the channel based on endpoint
212197ba5f4SPaul Zimmerman  * servicing period
213197ba5f4SPaul Zimmerman  */
214197ba5f4SPaul Zimmerman static void dwc2_update_frame_list(struct dwc2_hsotg *hsotg, struct dwc2_qh *qh,
215197ba5f4SPaul Zimmerman 				   int enable)
216197ba5f4SPaul Zimmerman {
217197ba5f4SPaul Zimmerman 	struct dwc2_host_chan *chan;
218197ba5f4SPaul Zimmerman 	u16 i, j, inc;
219197ba5f4SPaul Zimmerman 
220197ba5f4SPaul Zimmerman 	if (!hsotg) {
221197ba5f4SPaul Zimmerman 		pr_err("hsotg = %p\n", hsotg);
222197ba5f4SPaul Zimmerman 		return;
223197ba5f4SPaul Zimmerman 	}
224197ba5f4SPaul Zimmerman 
225197ba5f4SPaul Zimmerman 	if (!qh->channel) {
226197ba5f4SPaul Zimmerman 		dev_err(hsotg->dev, "qh->channel = %p\n", qh->channel);
227197ba5f4SPaul Zimmerman 		return;
228197ba5f4SPaul Zimmerman 	}
229197ba5f4SPaul Zimmerman 
230197ba5f4SPaul Zimmerman 	if (!hsotg->frame_list) {
231197ba5f4SPaul Zimmerman 		dev_err(hsotg->dev, "hsotg->frame_list = %p\n",
232197ba5f4SPaul Zimmerman 			hsotg->frame_list);
233197ba5f4SPaul Zimmerman 		return;
234197ba5f4SPaul Zimmerman 	}
235197ba5f4SPaul Zimmerman 
236197ba5f4SPaul Zimmerman 	chan = qh->channel;
237197ba5f4SPaul Zimmerman 	inc = dwc2_frame_incr_val(qh);
238197ba5f4SPaul Zimmerman 	if (qh->ep_type == USB_ENDPOINT_XFER_ISOC)
239197ba5f4SPaul Zimmerman 		i = dwc2_frame_list_idx(qh->sched_frame);
240197ba5f4SPaul Zimmerman 	else
241197ba5f4SPaul Zimmerman 		i = 0;
242197ba5f4SPaul Zimmerman 
243197ba5f4SPaul Zimmerman 	j = i;
244197ba5f4SPaul Zimmerman 	do {
245197ba5f4SPaul Zimmerman 		if (enable)
246197ba5f4SPaul Zimmerman 			hsotg->frame_list[j] |= 1 << chan->hc_num;
247197ba5f4SPaul Zimmerman 		else
248197ba5f4SPaul Zimmerman 			hsotg->frame_list[j] &= ~(1 << chan->hc_num);
249197ba5f4SPaul Zimmerman 		j = (j + inc) & (FRLISTEN_64_SIZE - 1);
250197ba5f4SPaul Zimmerman 	} while (j != i);
251197ba5f4SPaul Zimmerman 
252197ba5f4SPaul Zimmerman 	if (!enable)
253197ba5f4SPaul Zimmerman 		return;
254197ba5f4SPaul Zimmerman 
255197ba5f4SPaul Zimmerman 	chan->schinfo = 0;
256197ba5f4SPaul Zimmerman 	if (chan->speed == USB_SPEED_HIGH && qh->interval) {
257197ba5f4SPaul Zimmerman 		j = 1;
258197ba5f4SPaul Zimmerman 		/* TODO - check this */
259197ba5f4SPaul Zimmerman 		inc = (8 + qh->interval - 1) / qh->interval;
260197ba5f4SPaul Zimmerman 		for (i = 0; i < inc; i++) {
261197ba5f4SPaul Zimmerman 			chan->schinfo |= j;
262197ba5f4SPaul Zimmerman 			j = j << qh->interval;
263197ba5f4SPaul Zimmerman 		}
264197ba5f4SPaul Zimmerman 	} else {
265197ba5f4SPaul Zimmerman 		chan->schinfo = 0xff;
266197ba5f4SPaul Zimmerman 	}
267197ba5f4SPaul Zimmerman }
268197ba5f4SPaul Zimmerman 
269197ba5f4SPaul Zimmerman static void dwc2_release_channel_ddma(struct dwc2_hsotg *hsotg,
270197ba5f4SPaul Zimmerman 				      struct dwc2_qh *qh)
271197ba5f4SPaul Zimmerman {
272197ba5f4SPaul Zimmerman 	struct dwc2_host_chan *chan = qh->channel;
273197ba5f4SPaul Zimmerman 
274197ba5f4SPaul Zimmerman 	if (dwc2_qh_is_non_per(qh)) {
275197ba5f4SPaul Zimmerman 		if (hsotg->core_params->uframe_sched > 0)
276197ba5f4SPaul Zimmerman 			hsotg->available_host_channels++;
277197ba5f4SPaul Zimmerman 		else
278197ba5f4SPaul Zimmerman 			hsotg->non_periodic_channels--;
279197ba5f4SPaul Zimmerman 	} else {
280197ba5f4SPaul Zimmerman 		dwc2_update_frame_list(hsotg, qh, 0);
281197ba5f4SPaul Zimmerman 	}
282197ba5f4SPaul Zimmerman 
283197ba5f4SPaul Zimmerman 	/*
284197ba5f4SPaul Zimmerman 	 * The condition is added to prevent double cleanup try in case of
285197ba5f4SPaul Zimmerman 	 * device disconnect. See channel cleanup in dwc2_hcd_disconnect().
286197ba5f4SPaul Zimmerman 	 */
287197ba5f4SPaul Zimmerman 	if (chan->qh) {
288197ba5f4SPaul Zimmerman 		if (!list_empty(&chan->hc_list_entry))
289197ba5f4SPaul Zimmerman 			list_del(&chan->hc_list_entry);
290197ba5f4SPaul Zimmerman 		dwc2_hc_cleanup(hsotg, chan);
291197ba5f4SPaul Zimmerman 		list_add_tail(&chan->hc_list_entry, &hsotg->free_hc_list);
292197ba5f4SPaul Zimmerman 		chan->qh = NULL;
293197ba5f4SPaul Zimmerman 	}
294197ba5f4SPaul Zimmerman 
295197ba5f4SPaul Zimmerman 	qh->channel = NULL;
296197ba5f4SPaul Zimmerman 	qh->ntd = 0;
297197ba5f4SPaul Zimmerman 
298197ba5f4SPaul Zimmerman 	if (qh->desc_list)
299197ba5f4SPaul Zimmerman 		memset(qh->desc_list, 0, sizeof(struct dwc2_hcd_dma_desc) *
300197ba5f4SPaul Zimmerman 		       dwc2_max_desc_num(qh));
301197ba5f4SPaul Zimmerman }
302197ba5f4SPaul Zimmerman 
303197ba5f4SPaul Zimmerman /**
304197ba5f4SPaul Zimmerman  * dwc2_hcd_qh_init_ddma() - Initializes a QH structure's Descriptor DMA
305197ba5f4SPaul Zimmerman  * related members
306197ba5f4SPaul Zimmerman  *
307197ba5f4SPaul Zimmerman  * @hsotg: The HCD state structure for the DWC OTG controller
308197ba5f4SPaul Zimmerman  * @qh:    The QH to init
309197ba5f4SPaul Zimmerman  *
310197ba5f4SPaul Zimmerman  * Return: 0 if successful, negative error code otherwise
311197ba5f4SPaul Zimmerman  *
312197ba5f4SPaul Zimmerman  * Allocates memory for the descriptor list. For the first periodic QH,
313197ba5f4SPaul Zimmerman  * allocates memory for the FrameList and enables periodic scheduling.
314197ba5f4SPaul Zimmerman  */
315197ba5f4SPaul Zimmerman int dwc2_hcd_qh_init_ddma(struct dwc2_hsotg *hsotg, struct dwc2_qh *qh,
316197ba5f4SPaul Zimmerman 			  gfp_t mem_flags)
317197ba5f4SPaul Zimmerman {
318197ba5f4SPaul Zimmerman 	int retval;
319197ba5f4SPaul Zimmerman 
320197ba5f4SPaul Zimmerman 	if (qh->do_split) {
321197ba5f4SPaul Zimmerman 		dev_err(hsotg->dev,
322197ba5f4SPaul Zimmerman 			"SPLIT Transfers are not supported in Descriptor DMA mode.\n");
323197ba5f4SPaul Zimmerman 		retval = -EINVAL;
324197ba5f4SPaul Zimmerman 		goto err0;
325197ba5f4SPaul Zimmerman 	}
326197ba5f4SPaul Zimmerman 
327197ba5f4SPaul Zimmerman 	retval = dwc2_desc_list_alloc(hsotg, qh, mem_flags);
328197ba5f4SPaul Zimmerman 	if (retval)
329197ba5f4SPaul Zimmerman 		goto err0;
330197ba5f4SPaul Zimmerman 
331197ba5f4SPaul Zimmerman 	if (qh->ep_type == USB_ENDPOINT_XFER_ISOC ||
332197ba5f4SPaul Zimmerman 	    qh->ep_type == USB_ENDPOINT_XFER_INT) {
333197ba5f4SPaul Zimmerman 		if (!hsotg->frame_list) {
334197ba5f4SPaul Zimmerman 			retval = dwc2_frame_list_alloc(hsotg, mem_flags);
335197ba5f4SPaul Zimmerman 			if (retval)
336197ba5f4SPaul Zimmerman 				goto err1;
337197ba5f4SPaul Zimmerman 			/* Enable periodic schedule on first periodic QH */
338197ba5f4SPaul Zimmerman 			dwc2_per_sched_enable(hsotg, HCFG_FRLISTEN_64);
339197ba5f4SPaul Zimmerman 		}
340197ba5f4SPaul Zimmerman 	}
341197ba5f4SPaul Zimmerman 
342197ba5f4SPaul Zimmerman 	qh->ntd = 0;
343197ba5f4SPaul Zimmerman 	return 0;
344197ba5f4SPaul Zimmerman 
345197ba5f4SPaul Zimmerman err1:
346197ba5f4SPaul Zimmerman 	dwc2_desc_list_free(hsotg, qh);
347197ba5f4SPaul Zimmerman err0:
348197ba5f4SPaul Zimmerman 	return retval;
349197ba5f4SPaul Zimmerman }
350197ba5f4SPaul Zimmerman 
351197ba5f4SPaul Zimmerman /**
352197ba5f4SPaul Zimmerman  * dwc2_hcd_qh_free_ddma() - Frees a QH structure's Descriptor DMA related
353197ba5f4SPaul Zimmerman  * members
354197ba5f4SPaul Zimmerman  *
355197ba5f4SPaul Zimmerman  * @hsotg: The HCD state structure for the DWC OTG controller
356197ba5f4SPaul Zimmerman  * @qh:    The QH to free
357197ba5f4SPaul Zimmerman  *
358197ba5f4SPaul Zimmerman  * Frees descriptor list memory associated with the QH. If QH is periodic and
359197ba5f4SPaul Zimmerman  * the last, frees FrameList memory and disables periodic scheduling.
360197ba5f4SPaul Zimmerman  */
361197ba5f4SPaul Zimmerman void dwc2_hcd_qh_free_ddma(struct dwc2_hsotg *hsotg, struct dwc2_qh *qh)
362197ba5f4SPaul Zimmerman {
363197ba5f4SPaul Zimmerman 	dwc2_desc_list_free(hsotg, qh);
364197ba5f4SPaul Zimmerman 
365197ba5f4SPaul Zimmerman 	/*
366197ba5f4SPaul Zimmerman 	 * Channel still assigned due to some reasons.
367197ba5f4SPaul Zimmerman 	 * Seen on Isoc URB dequeue. Channel halted but no subsequent
368197ba5f4SPaul Zimmerman 	 * ChHalted interrupt to release the channel. Afterwards
369197ba5f4SPaul Zimmerman 	 * when it comes here from endpoint disable routine
370197ba5f4SPaul Zimmerman 	 * channel remains assigned.
371197ba5f4SPaul Zimmerman 	 */
372197ba5f4SPaul Zimmerman 	if (qh->channel)
373197ba5f4SPaul Zimmerman 		dwc2_release_channel_ddma(hsotg, qh);
374197ba5f4SPaul Zimmerman 
375197ba5f4SPaul Zimmerman 	if ((qh->ep_type == USB_ENDPOINT_XFER_ISOC ||
376197ba5f4SPaul Zimmerman 	     qh->ep_type == USB_ENDPOINT_XFER_INT) &&
377197ba5f4SPaul Zimmerman 	    (hsotg->core_params->uframe_sched > 0 ||
378197ba5f4SPaul Zimmerman 	     !hsotg->periodic_channels) && hsotg->frame_list) {
379197ba5f4SPaul Zimmerman 		dwc2_per_sched_disable(hsotg);
380197ba5f4SPaul Zimmerman 		dwc2_frame_list_free(hsotg);
381197ba5f4SPaul Zimmerman 	}
382197ba5f4SPaul Zimmerman }
383197ba5f4SPaul Zimmerman 
384197ba5f4SPaul Zimmerman static u8 dwc2_frame_to_desc_idx(struct dwc2_qh *qh, u16 frame_idx)
385197ba5f4SPaul Zimmerman {
386197ba5f4SPaul Zimmerman 	if (qh->dev_speed == USB_SPEED_HIGH)
387197ba5f4SPaul Zimmerman 		/* Descriptor set (8 descriptors) index which is 8-aligned */
388197ba5f4SPaul Zimmerman 		return (frame_idx & ((MAX_DMA_DESC_NUM_HS_ISOC / 8) - 1)) * 8;
389197ba5f4SPaul Zimmerman 	else
390197ba5f4SPaul Zimmerman 		return frame_idx & (MAX_DMA_DESC_NUM_GENERIC - 1);
391197ba5f4SPaul Zimmerman }
392197ba5f4SPaul Zimmerman 
393197ba5f4SPaul Zimmerman /*
394197ba5f4SPaul Zimmerman  * Determine starting frame for Isochronous transfer.
395197ba5f4SPaul Zimmerman  * Few frames skipped to prevent race condition with HC.
396197ba5f4SPaul Zimmerman  */
397197ba5f4SPaul Zimmerman static u16 dwc2_calc_starting_frame(struct dwc2_hsotg *hsotg,
398197ba5f4SPaul Zimmerman 				    struct dwc2_qh *qh, u16 *skip_frames)
399197ba5f4SPaul Zimmerman {
400197ba5f4SPaul Zimmerman 	u16 frame;
401197ba5f4SPaul Zimmerman 
402197ba5f4SPaul Zimmerman 	hsotg->frame_number = dwc2_hcd_get_frame_number(hsotg);
403197ba5f4SPaul Zimmerman 
404197ba5f4SPaul Zimmerman 	/* sched_frame is always frame number (not uFrame) both in FS and HS! */
405197ba5f4SPaul Zimmerman 
406197ba5f4SPaul Zimmerman 	/*
407197ba5f4SPaul Zimmerman 	 * skip_frames is used to limit activated descriptors number
408197ba5f4SPaul Zimmerman 	 * to avoid the situation when HC services the last activated
409197ba5f4SPaul Zimmerman 	 * descriptor firstly.
410197ba5f4SPaul Zimmerman 	 * Example for FS:
411197ba5f4SPaul Zimmerman 	 * Current frame is 1, scheduled frame is 3. Since HC always fetches
412197ba5f4SPaul Zimmerman 	 * the descriptor corresponding to curr_frame+1, the descriptor
413197ba5f4SPaul Zimmerman 	 * corresponding to frame 2 will be fetched. If the number of
414197ba5f4SPaul Zimmerman 	 * descriptors is max=64 (or greather) the list will be fully programmed
415197ba5f4SPaul Zimmerman 	 * with Active descriptors and it is possible case (rare) that the
416197ba5f4SPaul Zimmerman 	 * latest descriptor(considering rollback) corresponding to frame 2 will
417197ba5f4SPaul Zimmerman 	 * be serviced first. HS case is more probable because, in fact, up to
418197ba5f4SPaul Zimmerman 	 * 11 uframes (16 in the code) may be skipped.
419197ba5f4SPaul Zimmerman 	 */
420197ba5f4SPaul Zimmerman 	if (qh->dev_speed == USB_SPEED_HIGH) {
421197ba5f4SPaul Zimmerman 		/*
422197ba5f4SPaul Zimmerman 		 * Consider uframe counter also, to start xfer asap. If half of
423197ba5f4SPaul Zimmerman 		 * the frame elapsed skip 2 frames otherwise just 1 frame.
424197ba5f4SPaul Zimmerman 		 * Starting descriptor index must be 8-aligned, so if the
425197ba5f4SPaul Zimmerman 		 * current frame is near to complete the next one is skipped as
426197ba5f4SPaul Zimmerman 		 * well.
427197ba5f4SPaul Zimmerman 		 */
428197ba5f4SPaul Zimmerman 		if (dwc2_micro_frame_num(hsotg->frame_number) >= 5) {
429197ba5f4SPaul Zimmerman 			*skip_frames = 2 * 8;
430197ba5f4SPaul Zimmerman 			frame = dwc2_frame_num_inc(hsotg->frame_number,
431197ba5f4SPaul Zimmerman 						   *skip_frames);
432197ba5f4SPaul Zimmerman 		} else {
433197ba5f4SPaul Zimmerman 			*skip_frames = 1 * 8;
434197ba5f4SPaul Zimmerman 			frame = dwc2_frame_num_inc(hsotg->frame_number,
435197ba5f4SPaul Zimmerman 						   *skip_frames);
436197ba5f4SPaul Zimmerman 		}
437197ba5f4SPaul Zimmerman 
438197ba5f4SPaul Zimmerman 		frame = dwc2_full_frame_num(frame);
439197ba5f4SPaul Zimmerman 	} else {
440197ba5f4SPaul Zimmerman 		/*
441197ba5f4SPaul Zimmerman 		 * Two frames are skipped for FS - the current and the next.
442197ba5f4SPaul Zimmerman 		 * But for descriptor programming, 1 frame (descriptor) is
443197ba5f4SPaul Zimmerman 		 * enough, see example above.
444197ba5f4SPaul Zimmerman 		 */
445197ba5f4SPaul Zimmerman 		*skip_frames = 1;
446197ba5f4SPaul Zimmerman 		frame = dwc2_frame_num_inc(hsotg->frame_number, 2);
447197ba5f4SPaul Zimmerman 	}
448197ba5f4SPaul Zimmerman 
449197ba5f4SPaul Zimmerman 	return frame;
450197ba5f4SPaul Zimmerman }
451197ba5f4SPaul Zimmerman 
452197ba5f4SPaul Zimmerman /*
453197ba5f4SPaul Zimmerman  * Calculate initial descriptor index for isochronous transfer based on
454197ba5f4SPaul Zimmerman  * scheduled frame
455197ba5f4SPaul Zimmerman  */
456197ba5f4SPaul Zimmerman static u16 dwc2_recalc_initial_desc_idx(struct dwc2_hsotg *hsotg,
457197ba5f4SPaul Zimmerman 					struct dwc2_qh *qh)
458197ba5f4SPaul Zimmerman {
459197ba5f4SPaul Zimmerman 	u16 frame, fr_idx, fr_idx_tmp, skip_frames;
460197ba5f4SPaul Zimmerman 
461197ba5f4SPaul Zimmerman 	/*
462197ba5f4SPaul Zimmerman 	 * With current ISOC processing algorithm the channel is being released
463197ba5f4SPaul Zimmerman 	 * when no more QTDs in the list (qh->ntd == 0). Thus this function is
464197ba5f4SPaul Zimmerman 	 * called only when qh->ntd == 0 and qh->channel == 0.
465197ba5f4SPaul Zimmerman 	 *
466197ba5f4SPaul Zimmerman 	 * So qh->channel != NULL branch is not used and just not removed from
467197ba5f4SPaul Zimmerman 	 * the source file. It is required for another possible approach which
468197ba5f4SPaul Zimmerman 	 * is, do not disable and release the channel when ISOC session
469197ba5f4SPaul Zimmerman 	 * completed, just move QH to inactive schedule until new QTD arrives.
470197ba5f4SPaul Zimmerman 	 * On new QTD, the QH moved back to 'ready' schedule, starting frame and
471197ba5f4SPaul Zimmerman 	 * therefore starting desc_index are recalculated. In this case channel
472197ba5f4SPaul Zimmerman 	 * is released only on ep_disable.
473197ba5f4SPaul Zimmerman 	 */
474197ba5f4SPaul Zimmerman 
475197ba5f4SPaul Zimmerman 	/*
476197ba5f4SPaul Zimmerman 	 * Calculate starting descriptor index. For INTERRUPT endpoint it is
477197ba5f4SPaul Zimmerman 	 * always 0.
478197ba5f4SPaul Zimmerman 	 */
479197ba5f4SPaul Zimmerman 	if (qh->channel) {
480197ba5f4SPaul Zimmerman 		frame = dwc2_calc_starting_frame(hsotg, qh, &skip_frames);
481197ba5f4SPaul Zimmerman 		/*
482197ba5f4SPaul Zimmerman 		 * Calculate initial descriptor index based on FrameList current
483197ba5f4SPaul Zimmerman 		 * bitmap and servicing period
484197ba5f4SPaul Zimmerman 		 */
485197ba5f4SPaul Zimmerman 		fr_idx_tmp = dwc2_frame_list_idx(frame);
486197ba5f4SPaul Zimmerman 		fr_idx = (FRLISTEN_64_SIZE +
487197ba5f4SPaul Zimmerman 			  dwc2_frame_list_idx(qh->sched_frame) - fr_idx_tmp)
488197ba5f4SPaul Zimmerman 			 % dwc2_frame_incr_val(qh);
489197ba5f4SPaul Zimmerman 		fr_idx = (fr_idx + fr_idx_tmp) % FRLISTEN_64_SIZE;
490197ba5f4SPaul Zimmerman 	} else {
491197ba5f4SPaul Zimmerman 		qh->sched_frame = dwc2_calc_starting_frame(hsotg, qh,
492197ba5f4SPaul Zimmerman 							   &skip_frames);
493197ba5f4SPaul Zimmerman 		fr_idx = dwc2_frame_list_idx(qh->sched_frame);
494197ba5f4SPaul Zimmerman 	}
495197ba5f4SPaul Zimmerman 
496197ba5f4SPaul Zimmerman 	qh->td_first = qh->td_last = dwc2_frame_to_desc_idx(qh, fr_idx);
497197ba5f4SPaul Zimmerman 
498197ba5f4SPaul Zimmerman 	return skip_frames;
499197ba5f4SPaul Zimmerman }
500197ba5f4SPaul Zimmerman 
501197ba5f4SPaul Zimmerman #define ISOC_URB_GIVEBACK_ASAP
502197ba5f4SPaul Zimmerman 
503197ba5f4SPaul Zimmerman #define MAX_ISOC_XFER_SIZE_FS	1023
504197ba5f4SPaul Zimmerman #define MAX_ISOC_XFER_SIZE_HS	3072
505197ba5f4SPaul Zimmerman #define DESCNUM_THRESHOLD	4
506197ba5f4SPaul Zimmerman 
507197ba5f4SPaul Zimmerman static void dwc2_fill_host_isoc_dma_desc(struct dwc2_hsotg *hsotg,
508197ba5f4SPaul Zimmerman 					 struct dwc2_qtd *qtd,
509197ba5f4SPaul Zimmerman 					 struct dwc2_qh *qh, u32 max_xfer_size,
510197ba5f4SPaul Zimmerman 					 u16 idx)
511197ba5f4SPaul Zimmerman {
512197ba5f4SPaul Zimmerman 	struct dwc2_hcd_dma_desc *dma_desc = &qh->desc_list[idx];
513197ba5f4SPaul Zimmerman 	struct dwc2_hcd_iso_packet_desc *frame_desc;
514197ba5f4SPaul Zimmerman 
515197ba5f4SPaul Zimmerman 	memset(dma_desc, 0, sizeof(*dma_desc));
516197ba5f4SPaul Zimmerman 	frame_desc = &qtd->urb->iso_descs[qtd->isoc_frame_index_last];
517197ba5f4SPaul Zimmerman 
518197ba5f4SPaul Zimmerman 	if (frame_desc->length > max_xfer_size)
519197ba5f4SPaul Zimmerman 		qh->n_bytes[idx] = max_xfer_size;
520197ba5f4SPaul Zimmerman 	else
521197ba5f4SPaul Zimmerman 		qh->n_bytes[idx] = frame_desc->length;
522197ba5f4SPaul Zimmerman 
523197ba5f4SPaul Zimmerman 	dma_desc->buf = (u32)(qtd->urb->dma + frame_desc->offset);
524197ba5f4SPaul Zimmerman 	dma_desc->status = qh->n_bytes[idx] << HOST_DMA_ISOC_NBYTES_SHIFT &
525197ba5f4SPaul Zimmerman 			   HOST_DMA_ISOC_NBYTES_MASK;
526197ba5f4SPaul Zimmerman 
527*dde4c1bfSGregory Herrero 	/* Set active bit */
528*dde4c1bfSGregory Herrero 	dma_desc->status |= HOST_DMA_A;
529*dde4c1bfSGregory Herrero 
5303ac38d26SGregory Herrero 	qh->ntd++;
5313ac38d26SGregory Herrero 	qtd->isoc_frame_index_last++;
5323ac38d26SGregory Herrero 
533197ba5f4SPaul Zimmerman #ifdef ISOC_URB_GIVEBACK_ASAP
534197ba5f4SPaul Zimmerman 	/* Set IOC for each descriptor corresponding to last frame of URB */
535197ba5f4SPaul Zimmerman 	if (qtd->isoc_frame_index_last == qtd->urb->packet_count)
536197ba5f4SPaul Zimmerman 		dma_desc->status |= HOST_DMA_IOC;
537197ba5f4SPaul Zimmerman #endif
538197ba5f4SPaul Zimmerman 
539197ba5f4SPaul Zimmerman }
540197ba5f4SPaul Zimmerman 
541197ba5f4SPaul Zimmerman static void dwc2_init_isoc_dma_desc(struct dwc2_hsotg *hsotg,
542197ba5f4SPaul Zimmerman 				    struct dwc2_qh *qh, u16 skip_frames)
543197ba5f4SPaul Zimmerman {
544197ba5f4SPaul Zimmerman 	struct dwc2_qtd *qtd;
545197ba5f4SPaul Zimmerman 	u32 max_xfer_size;
546197ba5f4SPaul Zimmerman 	u16 idx, inc, n_desc, ntd_max = 0;
547197ba5f4SPaul Zimmerman 
548197ba5f4SPaul Zimmerman 	idx = qh->td_last;
549197ba5f4SPaul Zimmerman 	inc = qh->interval;
550197ba5f4SPaul Zimmerman 	n_desc = 0;
551197ba5f4SPaul Zimmerman 
552197ba5f4SPaul Zimmerman 	if (qh->interval) {
553197ba5f4SPaul Zimmerman 		ntd_max = (dwc2_max_desc_num(qh) + qh->interval - 1) /
554197ba5f4SPaul Zimmerman 				qh->interval;
555197ba5f4SPaul Zimmerman 		if (skip_frames && !qh->channel)
556197ba5f4SPaul Zimmerman 			ntd_max -= skip_frames / qh->interval;
557197ba5f4SPaul Zimmerman 	}
558197ba5f4SPaul Zimmerman 
559197ba5f4SPaul Zimmerman 	max_xfer_size = qh->dev_speed == USB_SPEED_HIGH ?
560197ba5f4SPaul Zimmerman 			MAX_ISOC_XFER_SIZE_HS : MAX_ISOC_XFER_SIZE_FS;
561197ba5f4SPaul Zimmerman 
562197ba5f4SPaul Zimmerman 	list_for_each_entry(qtd, &qh->qtd_list, qtd_list_entry) {
563197ba5f4SPaul Zimmerman 		while (qh->ntd < ntd_max && qtd->isoc_frame_index_last <
564197ba5f4SPaul Zimmerman 						qtd->urb->packet_count) {
565197ba5f4SPaul Zimmerman 			dwc2_fill_host_isoc_dma_desc(hsotg, qtd, qh,
566197ba5f4SPaul Zimmerman 						     max_xfer_size, idx);
567197ba5f4SPaul Zimmerman 			idx = dwc2_desclist_idx_inc(idx, inc, qh->dev_speed);
568197ba5f4SPaul Zimmerman 			n_desc++;
569197ba5f4SPaul Zimmerman 		}
570197ba5f4SPaul Zimmerman 		qtd->in_process = 1;
571197ba5f4SPaul Zimmerman 	}
572197ba5f4SPaul Zimmerman 
573197ba5f4SPaul Zimmerman 	qh->td_last = idx;
574197ba5f4SPaul Zimmerman 
575197ba5f4SPaul Zimmerman #ifdef ISOC_URB_GIVEBACK_ASAP
576197ba5f4SPaul Zimmerman 	/* Set IOC for last descriptor if descriptor list is full */
577197ba5f4SPaul Zimmerman 	if (qh->ntd == ntd_max) {
578197ba5f4SPaul Zimmerman 		idx = dwc2_desclist_idx_dec(qh->td_last, inc, qh->dev_speed);
579197ba5f4SPaul Zimmerman 		qh->desc_list[idx].status |= HOST_DMA_IOC;
580197ba5f4SPaul Zimmerman 	}
581197ba5f4SPaul Zimmerman #else
582197ba5f4SPaul Zimmerman 	/*
583197ba5f4SPaul Zimmerman 	 * Set IOC bit only for one descriptor. Always try to be ahead of HW
584197ba5f4SPaul Zimmerman 	 * processing, i.e. on IOC generation driver activates next descriptor
585197ba5f4SPaul Zimmerman 	 * but core continues to process descriptors following the one with IOC
586197ba5f4SPaul Zimmerman 	 * set.
587197ba5f4SPaul Zimmerman 	 */
588197ba5f4SPaul Zimmerman 
589197ba5f4SPaul Zimmerman 	if (n_desc > DESCNUM_THRESHOLD)
590197ba5f4SPaul Zimmerman 		/*
591197ba5f4SPaul Zimmerman 		 * Move IOC "up". Required even if there is only one QTD
592197ba5f4SPaul Zimmerman 		 * in the list, because QTDs might continue to be queued,
593197ba5f4SPaul Zimmerman 		 * but during the activation it was only one queued.
594197ba5f4SPaul Zimmerman 		 * Actually more than one QTD might be in the list if this
595197ba5f4SPaul Zimmerman 		 * function called from XferCompletion - QTDs was queued during
596197ba5f4SPaul Zimmerman 		 * HW processing of the previous descriptor chunk.
597197ba5f4SPaul Zimmerman 		 */
598197ba5f4SPaul Zimmerman 		idx = dwc2_desclist_idx_dec(idx, inc * ((qh->ntd + 1) / 2),
599197ba5f4SPaul Zimmerman 					    qh->dev_speed);
600197ba5f4SPaul Zimmerman 	else
601197ba5f4SPaul Zimmerman 		/*
602197ba5f4SPaul Zimmerman 		 * Set the IOC for the latest descriptor if either number of
603197ba5f4SPaul Zimmerman 		 * descriptors is not greater than threshold or no more new
604197ba5f4SPaul Zimmerman 		 * descriptors activated
605197ba5f4SPaul Zimmerman 		 */
606197ba5f4SPaul Zimmerman 		idx = dwc2_desclist_idx_dec(qh->td_last, inc, qh->dev_speed);
607197ba5f4SPaul Zimmerman 
608197ba5f4SPaul Zimmerman 	qh->desc_list[idx].status |= HOST_DMA_IOC;
609197ba5f4SPaul Zimmerman #endif
610197ba5f4SPaul Zimmerman }
611197ba5f4SPaul Zimmerman 
612197ba5f4SPaul Zimmerman static void dwc2_fill_host_dma_desc(struct dwc2_hsotg *hsotg,
613197ba5f4SPaul Zimmerman 				    struct dwc2_host_chan *chan,
614197ba5f4SPaul Zimmerman 				    struct dwc2_qtd *qtd, struct dwc2_qh *qh,
615197ba5f4SPaul Zimmerman 				    int n_desc)
616197ba5f4SPaul Zimmerman {
617197ba5f4SPaul Zimmerman 	struct dwc2_hcd_dma_desc *dma_desc = &qh->desc_list[n_desc];
618197ba5f4SPaul Zimmerman 	int len = chan->xfer_len;
619197ba5f4SPaul Zimmerman 
620197ba5f4SPaul Zimmerman 	if (len > MAX_DMA_DESC_SIZE - (chan->max_packet - 1))
621197ba5f4SPaul Zimmerman 		len = MAX_DMA_DESC_SIZE - (chan->max_packet - 1);
622197ba5f4SPaul Zimmerman 
623197ba5f4SPaul Zimmerman 	if (chan->ep_is_in) {
624197ba5f4SPaul Zimmerman 		int num_packets;
625197ba5f4SPaul Zimmerman 
626197ba5f4SPaul Zimmerman 		if (len > 0 && chan->max_packet)
627197ba5f4SPaul Zimmerman 			num_packets = (len + chan->max_packet - 1)
628197ba5f4SPaul Zimmerman 					/ chan->max_packet;
629197ba5f4SPaul Zimmerman 		else
630197ba5f4SPaul Zimmerman 			/* Need 1 packet for transfer length of 0 */
631197ba5f4SPaul Zimmerman 			num_packets = 1;
632197ba5f4SPaul Zimmerman 
633197ba5f4SPaul Zimmerman 		/* Always program an integral # of packets for IN transfers */
634197ba5f4SPaul Zimmerman 		len = num_packets * chan->max_packet;
635197ba5f4SPaul Zimmerman 	}
636197ba5f4SPaul Zimmerman 
637197ba5f4SPaul Zimmerman 	dma_desc->status = len << HOST_DMA_NBYTES_SHIFT & HOST_DMA_NBYTES_MASK;
638197ba5f4SPaul Zimmerman 	qh->n_bytes[n_desc] = len;
639197ba5f4SPaul Zimmerman 
640197ba5f4SPaul Zimmerman 	if (qh->ep_type == USB_ENDPOINT_XFER_CONTROL &&
641197ba5f4SPaul Zimmerman 	    qtd->control_phase == DWC2_CONTROL_SETUP)
642197ba5f4SPaul Zimmerman 		dma_desc->status |= HOST_DMA_SUP;
643197ba5f4SPaul Zimmerman 
644197ba5f4SPaul Zimmerman 	dma_desc->buf = (u32)chan->xfer_dma;
645197ba5f4SPaul Zimmerman 
646197ba5f4SPaul Zimmerman 	/*
647197ba5f4SPaul Zimmerman 	 * Last (or only) descriptor of IN transfer with actual size less
648197ba5f4SPaul Zimmerman 	 * than MaxPacket
649197ba5f4SPaul Zimmerman 	 */
650197ba5f4SPaul Zimmerman 	if (len > chan->xfer_len) {
651197ba5f4SPaul Zimmerman 		chan->xfer_len = 0;
652197ba5f4SPaul Zimmerman 	} else {
653197ba5f4SPaul Zimmerman 		chan->xfer_dma += len;
654197ba5f4SPaul Zimmerman 		chan->xfer_len -= len;
655197ba5f4SPaul Zimmerman 	}
656197ba5f4SPaul Zimmerman }
657197ba5f4SPaul Zimmerman 
658197ba5f4SPaul Zimmerman static void dwc2_init_non_isoc_dma_desc(struct dwc2_hsotg *hsotg,
659197ba5f4SPaul Zimmerman 					struct dwc2_qh *qh)
660197ba5f4SPaul Zimmerman {
661197ba5f4SPaul Zimmerman 	struct dwc2_qtd *qtd;
662197ba5f4SPaul Zimmerman 	struct dwc2_host_chan *chan = qh->channel;
663197ba5f4SPaul Zimmerman 	int n_desc = 0;
664197ba5f4SPaul Zimmerman 
665197ba5f4SPaul Zimmerman 	dev_vdbg(hsotg->dev, "%s(): qh=%p dma=%08lx len=%d\n", __func__, qh,
666197ba5f4SPaul Zimmerman 		 (unsigned long)chan->xfer_dma, chan->xfer_len);
667197ba5f4SPaul Zimmerman 
668197ba5f4SPaul Zimmerman 	/*
669197ba5f4SPaul Zimmerman 	 * Start with chan->xfer_dma initialized in assign_and_init_hc(), then
670197ba5f4SPaul Zimmerman 	 * if SG transfer consists of multiple URBs, this pointer is re-assigned
671197ba5f4SPaul Zimmerman 	 * to the buffer of the currently processed QTD. For non-SG request
672197ba5f4SPaul Zimmerman 	 * there is always one QTD active.
673197ba5f4SPaul Zimmerman 	 */
674197ba5f4SPaul Zimmerman 
675197ba5f4SPaul Zimmerman 	list_for_each_entry(qtd, &qh->qtd_list, qtd_list_entry) {
676197ba5f4SPaul Zimmerman 		dev_vdbg(hsotg->dev, "qtd=%p\n", qtd);
677197ba5f4SPaul Zimmerman 
678197ba5f4SPaul Zimmerman 		if (n_desc) {
679197ba5f4SPaul Zimmerman 			/* SG request - more than 1 QTD */
680197ba5f4SPaul Zimmerman 			chan->xfer_dma = qtd->urb->dma +
681197ba5f4SPaul Zimmerman 					qtd->urb->actual_length;
682197ba5f4SPaul Zimmerman 			chan->xfer_len = qtd->urb->length -
683197ba5f4SPaul Zimmerman 					qtd->urb->actual_length;
684197ba5f4SPaul Zimmerman 			dev_vdbg(hsotg->dev, "buf=%08lx len=%d\n",
685197ba5f4SPaul Zimmerman 				 (unsigned long)chan->xfer_dma, chan->xfer_len);
686197ba5f4SPaul Zimmerman 		}
687197ba5f4SPaul Zimmerman 
688197ba5f4SPaul Zimmerman 		qtd->n_desc = 0;
689197ba5f4SPaul Zimmerman 		do {
690197ba5f4SPaul Zimmerman 			if (n_desc > 1) {
691197ba5f4SPaul Zimmerman 				qh->desc_list[n_desc - 1].status |= HOST_DMA_A;
692197ba5f4SPaul Zimmerman 				dev_vdbg(hsotg->dev,
693197ba5f4SPaul Zimmerman 					 "set A bit in desc %d (%p)\n",
694197ba5f4SPaul Zimmerman 					 n_desc - 1,
695197ba5f4SPaul Zimmerman 					 &qh->desc_list[n_desc - 1]);
696197ba5f4SPaul Zimmerman 			}
697197ba5f4SPaul Zimmerman 			dwc2_fill_host_dma_desc(hsotg, chan, qtd, qh, n_desc);
698197ba5f4SPaul Zimmerman 			dev_vdbg(hsotg->dev,
699197ba5f4SPaul Zimmerman 				 "desc %d (%p) buf=%08x status=%08x\n",
700197ba5f4SPaul Zimmerman 				 n_desc, &qh->desc_list[n_desc],
701197ba5f4SPaul Zimmerman 				 qh->desc_list[n_desc].buf,
702197ba5f4SPaul Zimmerman 				 qh->desc_list[n_desc].status);
703197ba5f4SPaul Zimmerman 			qtd->n_desc++;
704197ba5f4SPaul Zimmerman 			n_desc++;
705197ba5f4SPaul Zimmerman 		} while (chan->xfer_len > 0 &&
706197ba5f4SPaul Zimmerman 			 n_desc != MAX_DMA_DESC_NUM_GENERIC);
707197ba5f4SPaul Zimmerman 
708197ba5f4SPaul Zimmerman 		dev_vdbg(hsotg->dev, "n_desc=%d\n", n_desc);
709197ba5f4SPaul Zimmerman 		qtd->in_process = 1;
710197ba5f4SPaul Zimmerman 		if (qh->ep_type == USB_ENDPOINT_XFER_CONTROL)
711197ba5f4SPaul Zimmerman 			break;
712197ba5f4SPaul Zimmerman 		if (n_desc == MAX_DMA_DESC_NUM_GENERIC)
713197ba5f4SPaul Zimmerman 			break;
714197ba5f4SPaul Zimmerman 	}
715197ba5f4SPaul Zimmerman 
716197ba5f4SPaul Zimmerman 	if (n_desc) {
717197ba5f4SPaul Zimmerman 		qh->desc_list[n_desc - 1].status |=
718197ba5f4SPaul Zimmerman 				HOST_DMA_IOC | HOST_DMA_EOL | HOST_DMA_A;
719197ba5f4SPaul Zimmerman 		dev_vdbg(hsotg->dev, "set IOC/EOL/A bits in desc %d (%p)\n",
720197ba5f4SPaul Zimmerman 			 n_desc - 1, &qh->desc_list[n_desc - 1]);
721197ba5f4SPaul Zimmerman 		if (n_desc > 1) {
722197ba5f4SPaul Zimmerman 			qh->desc_list[0].status |= HOST_DMA_A;
723197ba5f4SPaul Zimmerman 			dev_vdbg(hsotg->dev, "set A bit in desc 0 (%p)\n",
724197ba5f4SPaul Zimmerman 				 &qh->desc_list[0]);
725197ba5f4SPaul Zimmerman 		}
726197ba5f4SPaul Zimmerman 		chan->ntd = n_desc;
727197ba5f4SPaul Zimmerman 	}
728197ba5f4SPaul Zimmerman }
729197ba5f4SPaul Zimmerman 
730197ba5f4SPaul Zimmerman /**
731197ba5f4SPaul Zimmerman  * dwc2_hcd_start_xfer_ddma() - Starts a transfer in Descriptor DMA mode
732197ba5f4SPaul Zimmerman  *
733197ba5f4SPaul Zimmerman  * @hsotg: The HCD state structure for the DWC OTG controller
734197ba5f4SPaul Zimmerman  * @qh:    The QH to init
735197ba5f4SPaul Zimmerman  *
736197ba5f4SPaul Zimmerman  * Return: 0 if successful, negative error code otherwise
737197ba5f4SPaul Zimmerman  *
738197ba5f4SPaul Zimmerman  * For Control and Bulk endpoints, initializes descriptor list and starts the
739197ba5f4SPaul Zimmerman  * transfer. For Interrupt and Isochronous endpoints, initializes descriptor
740197ba5f4SPaul Zimmerman  * list then updates FrameList, marking appropriate entries as active.
741197ba5f4SPaul Zimmerman  *
742197ba5f4SPaul Zimmerman  * For Isochronous endpoints the starting descriptor index is calculated based
743197ba5f4SPaul Zimmerman  * on the scheduled frame, but only on the first transfer descriptor within a
744197ba5f4SPaul Zimmerman  * session. Then the transfer is started via enabling the channel.
745197ba5f4SPaul Zimmerman  *
746197ba5f4SPaul Zimmerman  * For Isochronous endpoints the channel is not halted on XferComplete
747197ba5f4SPaul Zimmerman  * interrupt so remains assigned to the endpoint(QH) until session is done.
748197ba5f4SPaul Zimmerman  */
749197ba5f4SPaul Zimmerman void dwc2_hcd_start_xfer_ddma(struct dwc2_hsotg *hsotg, struct dwc2_qh *qh)
750197ba5f4SPaul Zimmerman {
751197ba5f4SPaul Zimmerman 	/* Channel is already assigned */
752197ba5f4SPaul Zimmerman 	struct dwc2_host_chan *chan = qh->channel;
753197ba5f4SPaul Zimmerman 	u16 skip_frames = 0;
754197ba5f4SPaul Zimmerman 
755197ba5f4SPaul Zimmerman 	switch (chan->ep_type) {
756197ba5f4SPaul Zimmerman 	case USB_ENDPOINT_XFER_CONTROL:
757197ba5f4SPaul Zimmerman 	case USB_ENDPOINT_XFER_BULK:
758197ba5f4SPaul Zimmerman 		dwc2_init_non_isoc_dma_desc(hsotg, qh);
759197ba5f4SPaul Zimmerman 		dwc2_hc_start_transfer_ddma(hsotg, chan);
760197ba5f4SPaul Zimmerman 		break;
761197ba5f4SPaul Zimmerman 	case USB_ENDPOINT_XFER_INT:
762197ba5f4SPaul Zimmerman 		dwc2_init_non_isoc_dma_desc(hsotg, qh);
763197ba5f4SPaul Zimmerman 		dwc2_update_frame_list(hsotg, qh, 1);
764197ba5f4SPaul Zimmerman 		dwc2_hc_start_transfer_ddma(hsotg, chan);
765197ba5f4SPaul Zimmerman 		break;
766197ba5f4SPaul Zimmerman 	case USB_ENDPOINT_XFER_ISOC:
767197ba5f4SPaul Zimmerman 		if (!qh->ntd)
768197ba5f4SPaul Zimmerman 			skip_frames = dwc2_recalc_initial_desc_idx(hsotg, qh);
769197ba5f4SPaul Zimmerman 		dwc2_init_isoc_dma_desc(hsotg, qh, skip_frames);
770197ba5f4SPaul Zimmerman 
771197ba5f4SPaul Zimmerman 		if (!chan->xfer_started) {
772197ba5f4SPaul Zimmerman 			dwc2_update_frame_list(hsotg, qh, 1);
773197ba5f4SPaul Zimmerman 
774197ba5f4SPaul Zimmerman 			/*
775197ba5f4SPaul Zimmerman 			 * Always set to max, instead of actual size. Otherwise
776197ba5f4SPaul Zimmerman 			 * ntd will be changed with channel being enabled. Not
777197ba5f4SPaul Zimmerman 			 * recommended.
778197ba5f4SPaul Zimmerman 			 */
779197ba5f4SPaul Zimmerman 			chan->ntd = dwc2_max_desc_num(qh);
780197ba5f4SPaul Zimmerman 
781197ba5f4SPaul Zimmerman 			/* Enable channel only once for ISOC */
782197ba5f4SPaul Zimmerman 			dwc2_hc_start_transfer_ddma(hsotg, chan);
783197ba5f4SPaul Zimmerman 		}
784197ba5f4SPaul Zimmerman 
785197ba5f4SPaul Zimmerman 		break;
786197ba5f4SPaul Zimmerman 	default:
787197ba5f4SPaul Zimmerman 		break;
788197ba5f4SPaul Zimmerman 	}
789197ba5f4SPaul Zimmerman }
790197ba5f4SPaul Zimmerman 
791197ba5f4SPaul Zimmerman #define DWC2_CMPL_DONE		1
792197ba5f4SPaul Zimmerman #define DWC2_CMPL_STOP		2
793197ba5f4SPaul Zimmerman 
794197ba5f4SPaul Zimmerman static int dwc2_cmpl_host_isoc_dma_desc(struct dwc2_hsotg *hsotg,
795197ba5f4SPaul Zimmerman 					struct dwc2_host_chan *chan,
796197ba5f4SPaul Zimmerman 					struct dwc2_qtd *qtd,
797197ba5f4SPaul Zimmerman 					struct dwc2_qh *qh, u16 idx)
798197ba5f4SPaul Zimmerman {
799197ba5f4SPaul Zimmerman 	struct dwc2_hcd_dma_desc *dma_desc = &qh->desc_list[idx];
800197ba5f4SPaul Zimmerman 	struct dwc2_hcd_iso_packet_desc *frame_desc;
801197ba5f4SPaul Zimmerman 	u16 remain = 0;
802197ba5f4SPaul Zimmerman 	int rc = 0;
803197ba5f4SPaul Zimmerman 
804197ba5f4SPaul Zimmerman 	if (!qtd->urb)
805197ba5f4SPaul Zimmerman 		return -EINVAL;
806197ba5f4SPaul Zimmerman 
807197ba5f4SPaul Zimmerman 	frame_desc = &qtd->urb->iso_descs[qtd->isoc_frame_index_last];
808197ba5f4SPaul Zimmerman 	dma_desc->buf = (u32)(qtd->urb->dma + frame_desc->offset);
809197ba5f4SPaul Zimmerman 	if (chan->ep_is_in)
810197ba5f4SPaul Zimmerman 		remain = (dma_desc->status & HOST_DMA_ISOC_NBYTES_MASK) >>
811197ba5f4SPaul Zimmerman 			 HOST_DMA_ISOC_NBYTES_SHIFT;
812197ba5f4SPaul Zimmerman 
813197ba5f4SPaul Zimmerman 	if ((dma_desc->status & HOST_DMA_STS_MASK) == HOST_DMA_STS_PKTERR) {
814197ba5f4SPaul Zimmerman 		/*
815197ba5f4SPaul Zimmerman 		 * XactError, or unable to complete all the transactions
816197ba5f4SPaul Zimmerman 		 * in the scheduled micro-frame/frame, both indicated by
817197ba5f4SPaul Zimmerman 		 * HOST_DMA_STS_PKTERR
818197ba5f4SPaul Zimmerman 		 */
819197ba5f4SPaul Zimmerman 		qtd->urb->error_count++;
820197ba5f4SPaul Zimmerman 		frame_desc->actual_length = qh->n_bytes[idx] - remain;
821197ba5f4SPaul Zimmerman 		frame_desc->status = -EPROTO;
822197ba5f4SPaul Zimmerman 	} else {
823197ba5f4SPaul Zimmerman 		/* Success */
824197ba5f4SPaul Zimmerman 		frame_desc->actual_length = qh->n_bytes[idx] - remain;
825197ba5f4SPaul Zimmerman 		frame_desc->status = 0;
826197ba5f4SPaul Zimmerman 	}
827197ba5f4SPaul Zimmerman 
828197ba5f4SPaul Zimmerman 	if (++qtd->isoc_frame_index == qtd->urb->packet_count) {
829197ba5f4SPaul Zimmerman 		/*
830197ba5f4SPaul Zimmerman 		 * urb->status is not used for isoc transfers here. The
831197ba5f4SPaul Zimmerman 		 * individual frame_desc status are used instead.
832197ba5f4SPaul Zimmerman 		 */
833197ba5f4SPaul Zimmerman 		dwc2_host_complete(hsotg, qtd, 0);
834197ba5f4SPaul Zimmerman 		dwc2_hcd_qtd_unlink_and_free(hsotg, qtd, qh);
835197ba5f4SPaul Zimmerman 
836197ba5f4SPaul Zimmerman 		/*
837197ba5f4SPaul Zimmerman 		 * This check is necessary because urb_dequeue can be called
838197ba5f4SPaul Zimmerman 		 * from urb complete callback (sound driver for example). All
839197ba5f4SPaul Zimmerman 		 * pending URBs are dequeued there, so no need for further
840197ba5f4SPaul Zimmerman 		 * processing.
841197ba5f4SPaul Zimmerman 		 */
842197ba5f4SPaul Zimmerman 		if (chan->halt_status == DWC2_HC_XFER_URB_DEQUEUE)
843197ba5f4SPaul Zimmerman 			return -1;
844197ba5f4SPaul Zimmerman 		rc = DWC2_CMPL_DONE;
845197ba5f4SPaul Zimmerman 	}
846197ba5f4SPaul Zimmerman 
847197ba5f4SPaul Zimmerman 	qh->ntd--;
848197ba5f4SPaul Zimmerman 
849197ba5f4SPaul Zimmerman 	/* Stop if IOC requested descriptor reached */
850197ba5f4SPaul Zimmerman 	if (dma_desc->status & HOST_DMA_IOC)
851197ba5f4SPaul Zimmerman 		rc = DWC2_CMPL_STOP;
852197ba5f4SPaul Zimmerman 
853197ba5f4SPaul Zimmerman 	return rc;
854197ba5f4SPaul Zimmerman }
855197ba5f4SPaul Zimmerman 
856197ba5f4SPaul Zimmerman static void dwc2_complete_isoc_xfer_ddma(struct dwc2_hsotg *hsotg,
857197ba5f4SPaul Zimmerman 					 struct dwc2_host_chan *chan,
858197ba5f4SPaul Zimmerman 					 enum dwc2_halt_status halt_status)
859197ba5f4SPaul Zimmerman {
860197ba5f4SPaul Zimmerman 	struct dwc2_hcd_iso_packet_desc *frame_desc;
861197ba5f4SPaul Zimmerman 	struct dwc2_qtd *qtd, *qtd_tmp;
862197ba5f4SPaul Zimmerman 	struct dwc2_qh *qh;
863197ba5f4SPaul Zimmerman 	u16 idx;
864197ba5f4SPaul Zimmerman 	int rc;
865197ba5f4SPaul Zimmerman 
866197ba5f4SPaul Zimmerman 	qh = chan->qh;
867197ba5f4SPaul Zimmerman 	idx = qh->td_first;
868197ba5f4SPaul Zimmerman 
869197ba5f4SPaul Zimmerman 	if (chan->halt_status == DWC2_HC_XFER_URB_DEQUEUE) {
870197ba5f4SPaul Zimmerman 		list_for_each_entry(qtd, &qh->qtd_list, qtd_list_entry)
871197ba5f4SPaul Zimmerman 			qtd->in_process = 0;
872197ba5f4SPaul Zimmerman 		return;
873197ba5f4SPaul Zimmerman 	}
874197ba5f4SPaul Zimmerman 
875197ba5f4SPaul Zimmerman 	if (halt_status == DWC2_HC_XFER_AHB_ERR ||
876197ba5f4SPaul Zimmerman 	    halt_status == DWC2_HC_XFER_BABBLE_ERR) {
877197ba5f4SPaul Zimmerman 		/*
878197ba5f4SPaul Zimmerman 		 * Channel is halted in these error cases, considered as serious
879197ba5f4SPaul Zimmerman 		 * issues.
880197ba5f4SPaul Zimmerman 		 * Complete all URBs marking all frames as failed, irrespective
881197ba5f4SPaul Zimmerman 		 * whether some of the descriptors (frames) succeeded or not.
882197ba5f4SPaul Zimmerman 		 * Pass error code to completion routine as well, to update
883197ba5f4SPaul Zimmerman 		 * urb->status, some of class drivers might use it to stop
884197ba5f4SPaul Zimmerman 		 * queing transfer requests.
885197ba5f4SPaul Zimmerman 		 */
886197ba5f4SPaul Zimmerman 		int err = halt_status == DWC2_HC_XFER_AHB_ERR ?
887197ba5f4SPaul Zimmerman 			  -EIO : -EOVERFLOW;
888197ba5f4SPaul Zimmerman 
889197ba5f4SPaul Zimmerman 		list_for_each_entry_safe(qtd, qtd_tmp, &qh->qtd_list,
890197ba5f4SPaul Zimmerman 					 qtd_list_entry) {
891197ba5f4SPaul Zimmerman 			if (qtd->urb) {
892197ba5f4SPaul Zimmerman 				for (idx = 0; idx < qtd->urb->packet_count;
893197ba5f4SPaul Zimmerman 				     idx++) {
894197ba5f4SPaul Zimmerman 					frame_desc = &qtd->urb->iso_descs[idx];
895197ba5f4SPaul Zimmerman 					frame_desc->status = err;
896197ba5f4SPaul Zimmerman 				}
897197ba5f4SPaul Zimmerman 
898197ba5f4SPaul Zimmerman 				dwc2_host_complete(hsotg, qtd, err);
899197ba5f4SPaul Zimmerman 			}
900197ba5f4SPaul Zimmerman 
901197ba5f4SPaul Zimmerman 			dwc2_hcd_qtd_unlink_and_free(hsotg, qtd, qh);
902197ba5f4SPaul Zimmerman 		}
903197ba5f4SPaul Zimmerman 
904197ba5f4SPaul Zimmerman 		return;
905197ba5f4SPaul Zimmerman 	}
906197ba5f4SPaul Zimmerman 
907197ba5f4SPaul Zimmerman 	list_for_each_entry_safe(qtd, qtd_tmp, &qh->qtd_list, qtd_list_entry) {
908197ba5f4SPaul Zimmerman 		if (!qtd->in_process)
909197ba5f4SPaul Zimmerman 			break;
910197ba5f4SPaul Zimmerman 		do {
911197ba5f4SPaul Zimmerman 			rc = dwc2_cmpl_host_isoc_dma_desc(hsotg, chan, qtd, qh,
912197ba5f4SPaul Zimmerman 							  idx);
913197ba5f4SPaul Zimmerman 			if (rc < 0)
914197ba5f4SPaul Zimmerman 				return;
915197ba5f4SPaul Zimmerman 			idx = dwc2_desclist_idx_inc(idx, qh->interval,
916197ba5f4SPaul Zimmerman 						    chan->speed);
917197ba5f4SPaul Zimmerman 			if (rc == DWC2_CMPL_STOP)
918197ba5f4SPaul Zimmerman 				goto stop_scan;
919197ba5f4SPaul Zimmerman 			if (rc == DWC2_CMPL_DONE)
920197ba5f4SPaul Zimmerman 				break;
921197ba5f4SPaul Zimmerman 		} while (idx != qh->td_first);
922197ba5f4SPaul Zimmerman 	}
923197ba5f4SPaul Zimmerman 
924197ba5f4SPaul Zimmerman stop_scan:
925197ba5f4SPaul Zimmerman 	qh->td_first = idx;
926197ba5f4SPaul Zimmerman }
927197ba5f4SPaul Zimmerman 
928197ba5f4SPaul Zimmerman static int dwc2_update_non_isoc_urb_state_ddma(struct dwc2_hsotg *hsotg,
929197ba5f4SPaul Zimmerman 					struct dwc2_host_chan *chan,
930197ba5f4SPaul Zimmerman 					struct dwc2_qtd *qtd,
931197ba5f4SPaul Zimmerman 					struct dwc2_hcd_dma_desc *dma_desc,
932197ba5f4SPaul Zimmerman 					enum dwc2_halt_status halt_status,
933197ba5f4SPaul Zimmerman 					u32 n_bytes, int *xfer_done)
934197ba5f4SPaul Zimmerman {
935197ba5f4SPaul Zimmerman 	struct dwc2_hcd_urb *urb = qtd->urb;
936197ba5f4SPaul Zimmerman 	u16 remain = 0;
937197ba5f4SPaul Zimmerman 
938197ba5f4SPaul Zimmerman 	if (chan->ep_is_in)
939197ba5f4SPaul Zimmerman 		remain = (dma_desc->status & HOST_DMA_NBYTES_MASK) >>
940197ba5f4SPaul Zimmerman 			 HOST_DMA_NBYTES_SHIFT;
941197ba5f4SPaul Zimmerman 
942197ba5f4SPaul Zimmerman 	dev_vdbg(hsotg->dev, "remain=%d dwc2_urb=%p\n", remain, urb);
943197ba5f4SPaul Zimmerman 
944197ba5f4SPaul Zimmerman 	if (halt_status == DWC2_HC_XFER_AHB_ERR) {
945197ba5f4SPaul Zimmerman 		dev_err(hsotg->dev, "EIO\n");
946197ba5f4SPaul Zimmerman 		urb->status = -EIO;
947197ba5f4SPaul Zimmerman 		return 1;
948197ba5f4SPaul Zimmerman 	}
949197ba5f4SPaul Zimmerman 
950197ba5f4SPaul Zimmerman 	if ((dma_desc->status & HOST_DMA_STS_MASK) == HOST_DMA_STS_PKTERR) {
951197ba5f4SPaul Zimmerman 		switch (halt_status) {
952197ba5f4SPaul Zimmerman 		case DWC2_HC_XFER_STALL:
953197ba5f4SPaul Zimmerman 			dev_vdbg(hsotg->dev, "Stall\n");
954197ba5f4SPaul Zimmerman 			urb->status = -EPIPE;
955197ba5f4SPaul Zimmerman 			break;
956197ba5f4SPaul Zimmerman 		case DWC2_HC_XFER_BABBLE_ERR:
957197ba5f4SPaul Zimmerman 			dev_err(hsotg->dev, "Babble\n");
958197ba5f4SPaul Zimmerman 			urb->status = -EOVERFLOW;
959197ba5f4SPaul Zimmerman 			break;
960197ba5f4SPaul Zimmerman 		case DWC2_HC_XFER_XACT_ERR:
961197ba5f4SPaul Zimmerman 			dev_err(hsotg->dev, "XactErr\n");
962197ba5f4SPaul Zimmerman 			urb->status = -EPROTO;
963197ba5f4SPaul Zimmerman 			break;
964197ba5f4SPaul Zimmerman 		default:
965197ba5f4SPaul Zimmerman 			dev_err(hsotg->dev,
966197ba5f4SPaul Zimmerman 				"%s: Unhandled descriptor error status (%d)\n",
967197ba5f4SPaul Zimmerman 				__func__, halt_status);
968197ba5f4SPaul Zimmerman 			break;
969197ba5f4SPaul Zimmerman 		}
970197ba5f4SPaul Zimmerman 		return 1;
971197ba5f4SPaul Zimmerman 	}
972197ba5f4SPaul Zimmerman 
973197ba5f4SPaul Zimmerman 	if (dma_desc->status & HOST_DMA_A) {
974197ba5f4SPaul Zimmerman 		dev_vdbg(hsotg->dev,
975197ba5f4SPaul Zimmerman 			 "Active descriptor encountered on channel %d\n",
976197ba5f4SPaul Zimmerman 			 chan->hc_num);
977197ba5f4SPaul Zimmerman 		return 0;
978197ba5f4SPaul Zimmerman 	}
979197ba5f4SPaul Zimmerman 
980197ba5f4SPaul Zimmerman 	if (chan->ep_type == USB_ENDPOINT_XFER_CONTROL) {
981197ba5f4SPaul Zimmerman 		if (qtd->control_phase == DWC2_CONTROL_DATA) {
982197ba5f4SPaul Zimmerman 			urb->actual_length += n_bytes - remain;
983197ba5f4SPaul Zimmerman 			if (remain || urb->actual_length >= urb->length) {
984197ba5f4SPaul Zimmerman 				/*
985197ba5f4SPaul Zimmerman 				 * For Control Data stage do not set urb->status
986197ba5f4SPaul Zimmerman 				 * to 0, to prevent URB callback. Set it when
987197ba5f4SPaul Zimmerman 				 * Status phase is done. See below.
988197ba5f4SPaul Zimmerman 				 */
989197ba5f4SPaul Zimmerman 				*xfer_done = 1;
990197ba5f4SPaul Zimmerman 			}
991197ba5f4SPaul Zimmerman 		} else if (qtd->control_phase == DWC2_CONTROL_STATUS) {
992197ba5f4SPaul Zimmerman 			urb->status = 0;
993197ba5f4SPaul Zimmerman 			*xfer_done = 1;
994197ba5f4SPaul Zimmerman 		}
995197ba5f4SPaul Zimmerman 		/* No handling for SETUP stage */
996197ba5f4SPaul Zimmerman 	} else {
997197ba5f4SPaul Zimmerman 		/* BULK and INTR */
998197ba5f4SPaul Zimmerman 		urb->actual_length += n_bytes - remain;
999197ba5f4SPaul Zimmerman 		dev_vdbg(hsotg->dev, "length=%d actual=%d\n", urb->length,
1000197ba5f4SPaul Zimmerman 			 urb->actual_length);
1001197ba5f4SPaul Zimmerman 		if (remain || urb->actual_length >= urb->length) {
1002197ba5f4SPaul Zimmerman 			urb->status = 0;
1003197ba5f4SPaul Zimmerman 			*xfer_done = 1;
1004197ba5f4SPaul Zimmerman 		}
1005197ba5f4SPaul Zimmerman 	}
1006197ba5f4SPaul Zimmerman 
1007197ba5f4SPaul Zimmerman 	return 0;
1008197ba5f4SPaul Zimmerman }
1009197ba5f4SPaul Zimmerman 
1010197ba5f4SPaul Zimmerman static int dwc2_process_non_isoc_desc(struct dwc2_hsotg *hsotg,
1011197ba5f4SPaul Zimmerman 				      struct dwc2_host_chan *chan,
1012197ba5f4SPaul Zimmerman 				      int chnum, struct dwc2_qtd *qtd,
1013197ba5f4SPaul Zimmerman 				      int desc_num,
1014197ba5f4SPaul Zimmerman 				      enum dwc2_halt_status halt_status,
1015197ba5f4SPaul Zimmerman 				      int *xfer_done)
1016197ba5f4SPaul Zimmerman {
1017197ba5f4SPaul Zimmerman 	struct dwc2_qh *qh = chan->qh;
1018197ba5f4SPaul Zimmerman 	struct dwc2_hcd_urb *urb = qtd->urb;
1019197ba5f4SPaul Zimmerman 	struct dwc2_hcd_dma_desc *dma_desc;
1020197ba5f4SPaul Zimmerman 	u32 n_bytes;
1021197ba5f4SPaul Zimmerman 	int failed;
1022197ba5f4SPaul Zimmerman 
1023197ba5f4SPaul Zimmerman 	dev_vdbg(hsotg->dev, "%s()\n", __func__);
1024197ba5f4SPaul Zimmerman 
1025197ba5f4SPaul Zimmerman 	if (!urb)
1026197ba5f4SPaul Zimmerman 		return -EINVAL;
1027197ba5f4SPaul Zimmerman 
1028197ba5f4SPaul Zimmerman 	dma_desc = &qh->desc_list[desc_num];
1029197ba5f4SPaul Zimmerman 	n_bytes = qh->n_bytes[desc_num];
1030197ba5f4SPaul Zimmerman 	dev_vdbg(hsotg->dev,
1031197ba5f4SPaul Zimmerman 		 "qtd=%p dwc2_urb=%p desc_num=%d desc=%p n_bytes=%d\n",
1032197ba5f4SPaul Zimmerman 		 qtd, urb, desc_num, dma_desc, n_bytes);
1033197ba5f4SPaul Zimmerman 	failed = dwc2_update_non_isoc_urb_state_ddma(hsotg, chan, qtd, dma_desc,
1034197ba5f4SPaul Zimmerman 						     halt_status, n_bytes,
1035197ba5f4SPaul Zimmerman 						     xfer_done);
1036197ba5f4SPaul Zimmerman 	if (failed || (*xfer_done && urb->status != -EINPROGRESS)) {
1037197ba5f4SPaul Zimmerman 		dwc2_host_complete(hsotg, qtd, urb->status);
1038197ba5f4SPaul Zimmerman 		dwc2_hcd_qtd_unlink_and_free(hsotg, qtd, qh);
1039197ba5f4SPaul Zimmerman 		dev_vdbg(hsotg->dev, "failed=%1x xfer_done=%1x status=%08x\n",
1040197ba5f4SPaul Zimmerman 			 failed, *xfer_done, urb->status);
1041197ba5f4SPaul Zimmerman 		return failed;
1042197ba5f4SPaul Zimmerman 	}
1043197ba5f4SPaul Zimmerman 
1044197ba5f4SPaul Zimmerman 	if (qh->ep_type == USB_ENDPOINT_XFER_CONTROL) {
1045197ba5f4SPaul Zimmerman 		switch (qtd->control_phase) {
1046197ba5f4SPaul Zimmerman 		case DWC2_CONTROL_SETUP:
1047197ba5f4SPaul Zimmerman 			if (urb->length > 0)
1048197ba5f4SPaul Zimmerman 				qtd->control_phase = DWC2_CONTROL_DATA;
1049197ba5f4SPaul Zimmerman 			else
1050197ba5f4SPaul Zimmerman 				qtd->control_phase = DWC2_CONTROL_STATUS;
1051197ba5f4SPaul Zimmerman 			dev_vdbg(hsotg->dev,
1052197ba5f4SPaul Zimmerman 				 "  Control setup transaction done\n");
1053197ba5f4SPaul Zimmerman 			break;
1054197ba5f4SPaul Zimmerman 		case DWC2_CONTROL_DATA:
1055197ba5f4SPaul Zimmerman 			if (*xfer_done) {
1056197ba5f4SPaul Zimmerman 				qtd->control_phase = DWC2_CONTROL_STATUS;
1057197ba5f4SPaul Zimmerman 				dev_vdbg(hsotg->dev,
1058197ba5f4SPaul Zimmerman 					 "  Control data transfer done\n");
1059197ba5f4SPaul Zimmerman 			} else if (desc_num + 1 == qtd->n_desc) {
1060197ba5f4SPaul Zimmerman 				/*
1061197ba5f4SPaul Zimmerman 				 * Last descriptor for Control data stage which
1062197ba5f4SPaul Zimmerman 				 * is not completed yet
1063197ba5f4SPaul Zimmerman 				 */
1064197ba5f4SPaul Zimmerman 				dwc2_hcd_save_data_toggle(hsotg, chan, chnum,
1065197ba5f4SPaul Zimmerman 							  qtd);
1066197ba5f4SPaul Zimmerman 			}
1067197ba5f4SPaul Zimmerman 			break;
1068197ba5f4SPaul Zimmerman 		default:
1069197ba5f4SPaul Zimmerman 			break;
1070197ba5f4SPaul Zimmerman 		}
1071197ba5f4SPaul Zimmerman 	}
1072197ba5f4SPaul Zimmerman 
1073197ba5f4SPaul Zimmerman 	return 0;
1074197ba5f4SPaul Zimmerman }
1075197ba5f4SPaul Zimmerman 
1076197ba5f4SPaul Zimmerman static void dwc2_complete_non_isoc_xfer_ddma(struct dwc2_hsotg *hsotg,
1077197ba5f4SPaul Zimmerman 					     struct dwc2_host_chan *chan,
1078197ba5f4SPaul Zimmerman 					     int chnum,
1079197ba5f4SPaul Zimmerman 					     enum dwc2_halt_status halt_status)
1080197ba5f4SPaul Zimmerman {
1081197ba5f4SPaul Zimmerman 	struct list_head *qtd_item, *qtd_tmp;
1082197ba5f4SPaul Zimmerman 	struct dwc2_qh *qh = chan->qh;
1083197ba5f4SPaul Zimmerman 	struct dwc2_qtd *qtd = NULL;
1084197ba5f4SPaul Zimmerman 	int xfer_done;
1085197ba5f4SPaul Zimmerman 	int desc_num = 0;
1086197ba5f4SPaul Zimmerman 
1087197ba5f4SPaul Zimmerman 	if (chan->halt_status == DWC2_HC_XFER_URB_DEQUEUE) {
1088197ba5f4SPaul Zimmerman 		list_for_each_entry(qtd, &qh->qtd_list, qtd_list_entry)
1089197ba5f4SPaul Zimmerman 			qtd->in_process = 0;
1090197ba5f4SPaul Zimmerman 		return;
1091197ba5f4SPaul Zimmerman 	}
1092197ba5f4SPaul Zimmerman 
1093197ba5f4SPaul Zimmerman 	list_for_each_safe(qtd_item, qtd_tmp, &qh->qtd_list) {
1094197ba5f4SPaul Zimmerman 		int i;
1095197ba5f4SPaul Zimmerman 
1096197ba5f4SPaul Zimmerman 		qtd = list_entry(qtd_item, struct dwc2_qtd, qtd_list_entry);
1097197ba5f4SPaul Zimmerman 		xfer_done = 0;
1098197ba5f4SPaul Zimmerman 
1099197ba5f4SPaul Zimmerman 		for (i = 0; i < qtd->n_desc; i++) {
1100197ba5f4SPaul Zimmerman 			if (dwc2_process_non_isoc_desc(hsotg, chan, chnum, qtd,
1101197ba5f4SPaul Zimmerman 						       desc_num, halt_status,
1102197ba5f4SPaul Zimmerman 						       &xfer_done)) {
1103197ba5f4SPaul Zimmerman 				qtd = NULL;
1104197ba5f4SPaul Zimmerman 				break;
1105197ba5f4SPaul Zimmerman 			}
1106197ba5f4SPaul Zimmerman 			desc_num++;
1107197ba5f4SPaul Zimmerman 		}
1108197ba5f4SPaul Zimmerman 	}
1109197ba5f4SPaul Zimmerman 
1110197ba5f4SPaul Zimmerman 	if (qh->ep_type != USB_ENDPOINT_XFER_CONTROL) {
1111197ba5f4SPaul Zimmerman 		/*
1112197ba5f4SPaul Zimmerman 		 * Resetting the data toggle for bulk and interrupt endpoints
1113197ba5f4SPaul Zimmerman 		 * in case of stall. See handle_hc_stall_intr().
1114197ba5f4SPaul Zimmerman 		 */
1115197ba5f4SPaul Zimmerman 		if (halt_status == DWC2_HC_XFER_STALL)
1116197ba5f4SPaul Zimmerman 			qh->data_toggle = DWC2_HC_PID_DATA0;
1117197ba5f4SPaul Zimmerman 		else if (qtd)
1118197ba5f4SPaul Zimmerman 			dwc2_hcd_save_data_toggle(hsotg, chan, chnum, qtd);
1119197ba5f4SPaul Zimmerman 	}
1120197ba5f4SPaul Zimmerman 
1121197ba5f4SPaul Zimmerman 	if (halt_status == DWC2_HC_XFER_COMPLETE) {
1122197ba5f4SPaul Zimmerman 		if (chan->hcint & HCINTMSK_NYET) {
1123197ba5f4SPaul Zimmerman 			/*
1124197ba5f4SPaul Zimmerman 			 * Got a NYET on the last transaction of the transfer.
1125197ba5f4SPaul Zimmerman 			 * It means that the endpoint should be in the PING
1126197ba5f4SPaul Zimmerman 			 * state at the beginning of the next transfer.
1127197ba5f4SPaul Zimmerman 			 */
1128197ba5f4SPaul Zimmerman 			qh->ping_state = 1;
1129197ba5f4SPaul Zimmerman 		}
1130197ba5f4SPaul Zimmerman 	}
1131197ba5f4SPaul Zimmerman }
1132197ba5f4SPaul Zimmerman 
1133197ba5f4SPaul Zimmerman /**
1134197ba5f4SPaul Zimmerman  * dwc2_hcd_complete_xfer_ddma() - Scans the descriptor list, updates URB's
1135197ba5f4SPaul Zimmerman  * status and calls completion routine for the URB if it's done. Called from
1136197ba5f4SPaul Zimmerman  * interrupt handlers.
1137197ba5f4SPaul Zimmerman  *
1138197ba5f4SPaul Zimmerman  * @hsotg:       The HCD state structure for the DWC OTG controller
1139197ba5f4SPaul Zimmerman  * @chan:        Host channel the transfer is completed on
1140197ba5f4SPaul Zimmerman  * @chnum:       Index of Host channel registers
1141197ba5f4SPaul Zimmerman  * @halt_status: Reason the channel is being halted or just XferComplete
1142197ba5f4SPaul Zimmerman  *               for isochronous transfers
1143197ba5f4SPaul Zimmerman  *
1144197ba5f4SPaul Zimmerman  * Releases the channel to be used by other transfers.
1145197ba5f4SPaul Zimmerman  * In case of Isochronous endpoint the channel is not halted until the end of
1146197ba5f4SPaul Zimmerman  * the session, i.e. QTD list is empty.
1147197ba5f4SPaul Zimmerman  * If periodic channel released the FrameList is updated accordingly.
1148197ba5f4SPaul Zimmerman  * Calls transaction selection routines to activate pending transfers.
1149197ba5f4SPaul Zimmerman  */
1150197ba5f4SPaul Zimmerman void dwc2_hcd_complete_xfer_ddma(struct dwc2_hsotg *hsotg,
1151197ba5f4SPaul Zimmerman 				 struct dwc2_host_chan *chan, int chnum,
1152197ba5f4SPaul Zimmerman 				 enum dwc2_halt_status halt_status)
1153197ba5f4SPaul Zimmerman {
1154197ba5f4SPaul Zimmerman 	struct dwc2_qh *qh = chan->qh;
1155197ba5f4SPaul Zimmerman 	int continue_isoc_xfer = 0;
1156197ba5f4SPaul Zimmerman 	enum dwc2_transaction_type tr_type;
1157197ba5f4SPaul Zimmerman 
1158197ba5f4SPaul Zimmerman 	if (chan->ep_type == USB_ENDPOINT_XFER_ISOC) {
1159197ba5f4SPaul Zimmerman 		dwc2_complete_isoc_xfer_ddma(hsotg, chan, halt_status);
1160197ba5f4SPaul Zimmerman 
1161197ba5f4SPaul Zimmerman 		/* Release the channel if halted or session completed */
1162197ba5f4SPaul Zimmerman 		if (halt_status != DWC2_HC_XFER_COMPLETE ||
1163197ba5f4SPaul Zimmerman 		    list_empty(&qh->qtd_list)) {
1164197ba5f4SPaul Zimmerman 			/* Halt the channel if session completed */
1165197ba5f4SPaul Zimmerman 			if (halt_status == DWC2_HC_XFER_COMPLETE)
1166197ba5f4SPaul Zimmerman 				dwc2_hc_halt(hsotg, chan, halt_status);
1167197ba5f4SPaul Zimmerman 			dwc2_release_channel_ddma(hsotg, qh);
1168197ba5f4SPaul Zimmerman 			dwc2_hcd_qh_unlink(hsotg, qh);
1169197ba5f4SPaul Zimmerman 		} else {
1170197ba5f4SPaul Zimmerman 			/* Keep in assigned schedule to continue transfer */
1171197ba5f4SPaul Zimmerman 			list_move(&qh->qh_list_entry,
1172197ba5f4SPaul Zimmerman 				  &hsotg->periodic_sched_assigned);
1173197ba5f4SPaul Zimmerman 			continue_isoc_xfer = 1;
1174197ba5f4SPaul Zimmerman 		}
1175197ba5f4SPaul Zimmerman 		/*
1176197ba5f4SPaul Zimmerman 		 * Todo: Consider the case when period exceeds FrameList size.
1177197ba5f4SPaul Zimmerman 		 * Frame Rollover interrupt should be used.
1178197ba5f4SPaul Zimmerman 		 */
1179197ba5f4SPaul Zimmerman 	} else {
1180197ba5f4SPaul Zimmerman 		/*
1181197ba5f4SPaul Zimmerman 		 * Scan descriptor list to complete the URB(s), then release
1182197ba5f4SPaul Zimmerman 		 * the channel
1183197ba5f4SPaul Zimmerman 		 */
1184197ba5f4SPaul Zimmerman 		dwc2_complete_non_isoc_xfer_ddma(hsotg, chan, chnum,
1185197ba5f4SPaul Zimmerman 						 halt_status);
1186197ba5f4SPaul Zimmerman 		dwc2_release_channel_ddma(hsotg, qh);
1187197ba5f4SPaul Zimmerman 		dwc2_hcd_qh_unlink(hsotg, qh);
1188197ba5f4SPaul Zimmerman 
1189197ba5f4SPaul Zimmerman 		if (!list_empty(&qh->qtd_list)) {
1190197ba5f4SPaul Zimmerman 			/*
1191197ba5f4SPaul Zimmerman 			 * Add back to inactive non-periodic schedule on normal
1192197ba5f4SPaul Zimmerman 			 * completion
1193197ba5f4SPaul Zimmerman 			 */
1194197ba5f4SPaul Zimmerman 			dwc2_hcd_qh_add(hsotg, qh);
1195197ba5f4SPaul Zimmerman 		}
1196197ba5f4SPaul Zimmerman 	}
1197197ba5f4SPaul Zimmerman 
1198197ba5f4SPaul Zimmerman 	tr_type = dwc2_hcd_select_transactions(hsotg);
1199197ba5f4SPaul Zimmerman 	if (tr_type != DWC2_TRANSACTION_NONE || continue_isoc_xfer) {
1200197ba5f4SPaul Zimmerman 		if (continue_isoc_xfer) {
1201197ba5f4SPaul Zimmerman 			if (tr_type == DWC2_TRANSACTION_NONE)
1202197ba5f4SPaul Zimmerman 				tr_type = DWC2_TRANSACTION_PERIODIC;
1203197ba5f4SPaul Zimmerman 			else if (tr_type == DWC2_TRANSACTION_NON_PERIODIC)
1204197ba5f4SPaul Zimmerman 				tr_type = DWC2_TRANSACTION_ALL;
1205197ba5f4SPaul Zimmerman 		}
1206197ba5f4SPaul Zimmerman 		dwc2_hcd_queue_transactions(hsotg, tr_type);
1207197ba5f4SPaul Zimmerman 	}
1208197ba5f4SPaul Zimmerman }
1209