dwc2_hcdddma.c revision 1.1 1 /* $NetBSD: dwc2_hcdddma.c,v 1.1 2013/09/05 07:53:12 skrll Exp $ */
2
3 /*
4 * hcd_ddma.c - DesignWare HS OTG Controller descriptor DMA routines
5 *
6 * Copyright (C) 2004-2013 Synopsys, Inc.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions, and the following disclaimer,
13 * without modification.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. The names of the above-listed copyright holders may not be used
18 * to endorse or promote products derived from this software without
19 * specific prior written permission.
20 *
21 * ALTERNATIVELY, this software may be distributed under the terms of the
22 * GNU General Public License ("GPL") as published by the Free Software
23 * Foundation; either version 2 of the License, or (at your option) any
24 * later version.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
27 * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
28 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
30 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
31 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
32 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
33 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
34 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
35 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
36 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37 */
38
39 /*
40 * This file contains the Descriptor DMA implementation for Host mode
41 */
42 #include <linux/kernel.h>
43 #include <linux/module.h>
44 #include <linux/spinlock.h>
45 #include <linux/interrupt.h>
46 #include <linux/dma-mapping.h>
47 #include <linux/io.h>
48 #include <linux/slab.h>
49 #include <linux/usb.h>
50
51 #include <linux/usb/hcd.h>
52 #include <linux/usb/ch11.h>
53
54 #include "core.h"
55 #include "hcd.h"
56
57 static u16 dwc2_frame_list_idx(u16 frame)
58 {
59 return frame & (FRLISTEN_64_SIZE - 1);
60 }
61
62 static u16 dwc2_desclist_idx_inc(u16 idx, u16 inc, u8 speed)
63 {
64 return (idx + inc) &
65 ((speed == USB_SPEED_HIGH ? MAX_DMA_DESC_NUM_HS_ISOC :
66 MAX_DMA_DESC_NUM_GENERIC) - 1);
67 }
68
69 static u16 dwc2_desclist_idx_dec(u16 idx, u16 inc, u8 speed)
70 {
71 return (idx - inc) &
72 ((speed == USB_SPEED_HIGH ? MAX_DMA_DESC_NUM_HS_ISOC :
73 MAX_DMA_DESC_NUM_GENERIC) - 1);
74 }
75
76 static u16 dwc2_max_desc_num(struct dwc2_qh *qh)
77 {
78 return (qh->ep_type == USB_ENDPOINT_XFER_ISOC &&
79 qh->dev_speed == USB_SPEED_HIGH) ?
80 MAX_DMA_DESC_NUM_HS_ISOC : MAX_DMA_DESC_NUM_GENERIC;
81 }
82
83 static u16 dwc2_frame_incr_val(struct dwc2_qh *qh)
84 {
85 return qh->dev_speed == USB_SPEED_HIGH ?
86 (qh->interval + 8 - 1) / 8 : qh->interval;
87 }
88
89 static int dwc2_desc_list_alloc(struct dwc2_hsotg *hsotg, struct dwc2_qh *qh,
90 gfp_t flags)
91 {
92 qh->desc_list = dma_alloc_coherent(hsotg->dev,
93 sizeof(struct dwc2_hcd_dma_desc) *
94 dwc2_max_desc_num(qh), &qh->desc_list_dma,
95 flags);
96
97 if (!qh->desc_list)
98 return -ENOMEM;
99
100 memset(qh->desc_list, 0,
101 sizeof(struct dwc2_hcd_dma_desc) * dwc2_max_desc_num(qh));
102
103 qh->n_bytes = kzalloc(sizeof(u32) * dwc2_max_desc_num(qh), flags);
104 if (!qh->n_bytes) {
105 dma_free_coherent(hsotg->dev, sizeof(struct dwc2_hcd_dma_desc)
106 * dwc2_max_desc_num(qh), qh->desc_list,
107 qh->desc_list_dma);
108 qh->desc_list = NULL;
109 return -ENOMEM;
110 }
111
112 return 0;
113 }
114
115 static void dwc2_desc_list_free(struct dwc2_hsotg *hsotg, struct dwc2_qh *qh)
116 {
117 if (qh->desc_list) {
118 dma_free_coherent(hsotg->dev, sizeof(struct dwc2_hcd_dma_desc)
119 * dwc2_max_desc_num(qh), qh->desc_list,
120 qh->desc_list_dma);
121 qh->desc_list = NULL;
122 }
123
124 kfree(qh->n_bytes);
125 qh->n_bytes = NULL;
126 }
127
128 static int dwc2_frame_list_alloc(struct dwc2_hsotg *hsotg, gfp_t mem_flags)
129 {
130 if (hsotg->frame_list)
131 return 0;
132
133 hsotg->frame_list = dma_alloc_coherent(hsotg->dev,
134 4 * FRLISTEN_64_SIZE,
135 &hsotg->frame_list_dma,
136 mem_flags);
137 if (!hsotg->frame_list)
138 return -ENOMEM;
139
140 memset(hsotg->frame_list, 0, 4 * FRLISTEN_64_SIZE);
141 return 0;
142 }
143
144 static void dwc2_frame_list_free(struct dwc2_hsotg *hsotg)
145 {
146 u32 *frame_list;
147 dma_addr_t frame_list_dma;
148 unsigned long flags;
149
150 spin_lock_irqsave(&hsotg->lock, flags);
151
152 if (!hsotg->frame_list) {
153 spin_unlock_irqrestore(&hsotg->lock, flags);
154 return;
155 }
156
157 frame_list = hsotg->frame_list;
158 frame_list_dma = hsotg->frame_list_dma;
159 hsotg->frame_list = NULL;
160
161 spin_unlock_irqrestore(&hsotg->lock, flags);
162
163 dma_free_coherent(hsotg->dev, 4 * FRLISTEN_64_SIZE, frame_list,
164 frame_list_dma);
165 }
166
167 static void dwc2_per_sched_enable(struct dwc2_hsotg *hsotg, u32 fr_list_en)
168 {
169 u32 hcfg;
170 unsigned long flags;
171
172 spin_lock_irqsave(&hsotg->lock, flags);
173
174 hcfg = readl(hsotg->regs + HCFG);
175 if (hcfg & HCFG_PERSCHEDENA) {
176 /* already enabled */
177 spin_unlock_irqrestore(&hsotg->lock, flags);
178 return;
179 }
180
181 writel(hsotg->frame_list_dma, hsotg->regs + HFLBADDR);
182
183 hcfg &= ~HCFG_FRLISTEN_MASK;
184 hcfg |= fr_list_en | HCFG_PERSCHEDENA;
185 dev_vdbg(hsotg->dev, "Enabling Periodic schedule\n");
186 writel(hcfg, hsotg->regs + HCFG);
187
188 spin_unlock_irqrestore(&hsotg->lock, flags);
189 }
190
191 static void dwc2_per_sched_disable(struct dwc2_hsotg *hsotg)
192 {
193 u32 hcfg;
194 unsigned long flags;
195
196 spin_lock_irqsave(&hsotg->lock, flags);
197
198 hcfg = readl(hsotg->regs + HCFG);
199 if (!(hcfg & HCFG_PERSCHEDENA)) {
200 /* already disabled */
201 spin_unlock_irqrestore(&hsotg->lock, flags);
202 return;
203 }
204
205 hcfg &= ~HCFG_PERSCHEDENA;
206 dev_vdbg(hsotg->dev, "Disabling Periodic schedule\n");
207 writel(hcfg, hsotg->regs + HCFG);
208
209 spin_unlock_irqrestore(&hsotg->lock, flags);
210 }
211
212 /*
213 * Activates/Deactivates FrameList entries for the channel based on endpoint
214 * servicing period
215 */
216 static void dwc2_update_frame_list(struct dwc2_hsotg *hsotg, struct dwc2_qh *qh,
217 int enable)
218 {
219 struct dwc2_host_chan *chan;
220 u16 i, j, inc;
221
222 if (!hsotg) {
223 pr_err("hsotg = %p\n", hsotg);
224 return;
225 }
226
227 if (!qh->channel) {
228 dev_err(hsotg->dev, "qh->channel = %p\n", qh->channel);
229 return;
230 }
231
232 if (!hsotg->frame_list) {
233 dev_err(hsotg->dev, "hsotg->frame_list = %p\n",
234 hsotg->frame_list);
235 return;
236 }
237
238 chan = qh->channel;
239 inc = dwc2_frame_incr_val(qh);
240 if (qh->ep_type == USB_ENDPOINT_XFER_ISOC)
241 i = dwc2_frame_list_idx(qh->sched_frame);
242 else
243 i = 0;
244
245 j = i;
246 do {
247 if (enable)
248 hsotg->frame_list[j] |= 1 << chan->hc_num;
249 else
250 hsotg->frame_list[j] &= ~(1 << chan->hc_num);
251 j = (j + inc) & (FRLISTEN_64_SIZE - 1);
252 } while (j != i);
253
254 if (!enable)
255 return;
256
257 chan->schinfo = 0;
258 if (chan->speed == USB_SPEED_HIGH && qh->interval) {
259 j = 1;
260 /* TODO - check this */
261 inc = (8 + qh->interval - 1) / qh->interval;
262 for (i = 0; i < inc; i++) {
263 chan->schinfo |= j;
264 j = j << qh->interval;
265 }
266 } else {
267 chan->schinfo = 0xff;
268 }
269 }
270
271 static void dwc2_release_channel_ddma(struct dwc2_hsotg *hsotg,
272 struct dwc2_qh *qh)
273 {
274 struct dwc2_host_chan *chan = qh->channel;
275
276 if (dwc2_qh_is_non_per(qh)) {
277 if (hsotg->core_params->uframe_sched > 0)
278 hsotg->available_host_channels++;
279 else
280 hsotg->non_periodic_channels--;
281 } else {
282 dwc2_update_frame_list(hsotg, qh, 0);
283 }
284
285 /*
286 * The condition is added to prevent double cleanup try in case of
287 * device disconnect. See channel cleanup in dwc2_hcd_disconnect().
288 */
289 if (chan->qh) {
290 if (!list_empty(&chan->hc_list_entry))
291 list_del(&chan->hc_list_entry);
292 dwc2_hc_cleanup(hsotg, chan);
293 list_add_tail(&chan->hc_list_entry, &hsotg->free_hc_list);
294 chan->qh = NULL;
295 }
296
297 qh->channel = NULL;
298 qh->ntd = 0;
299
300 if (qh->desc_list)
301 memset(qh->desc_list, 0, sizeof(struct dwc2_hcd_dma_desc) *
302 dwc2_max_desc_num(qh));
303 }
304
305 /**
306 * dwc2_hcd_qh_init_ddma() - Initializes a QH structure's Descriptor DMA
307 * related members
308 *
309 * @hsotg: The HCD state structure for the DWC OTG controller
310 * @qh: The QH to init
311 *
312 * Return: 0 if successful, negative error code otherwise
313 *
314 * Allocates memory for the descriptor list. For the first periodic QH,
315 * allocates memory for the FrameList and enables periodic scheduling.
316 */
317 int dwc2_hcd_qh_init_ddma(struct dwc2_hsotg *hsotg, struct dwc2_qh *qh,
318 gfp_t mem_flags)
319 {
320 int retval;
321
322 if (qh->do_split) {
323 dev_err(hsotg->dev,
324 "SPLIT Transfers are not supported in Descriptor DMA mode.\n");
325 retval = -EINVAL;
326 goto err0;
327 }
328
329 retval = dwc2_desc_list_alloc(hsotg, qh, mem_flags);
330 if (retval)
331 goto err0;
332
333 if (qh->ep_type == USB_ENDPOINT_XFER_ISOC ||
334 qh->ep_type == USB_ENDPOINT_XFER_INT) {
335 if (!hsotg->frame_list) {
336 retval = dwc2_frame_list_alloc(hsotg, mem_flags);
337 if (retval)
338 goto err1;
339 /* Enable periodic schedule on first periodic QH */
340 dwc2_per_sched_enable(hsotg, HCFG_FRLISTEN_64);
341 }
342 }
343
344 qh->ntd = 0;
345 return 0;
346
347 err1:
348 dwc2_desc_list_free(hsotg, qh);
349 err0:
350 return retval;
351 }
352
353 /**
354 * dwc2_hcd_qh_free_ddma() - Frees a QH structure's Descriptor DMA related
355 * members
356 *
357 * @hsotg: The HCD state structure for the DWC OTG controller
358 * @qh: The QH to free
359 *
360 * Frees descriptor list memory associated with the QH. If QH is periodic and
361 * the last, frees FrameList memory and disables periodic scheduling.
362 */
363 void dwc2_hcd_qh_free_ddma(struct dwc2_hsotg *hsotg, struct dwc2_qh *qh)
364 {
365 dwc2_desc_list_free(hsotg, qh);
366
367 /*
368 * Channel still assigned due to some reasons.
369 * Seen on Isoc URB dequeue. Channel halted but no subsequent
370 * ChHalted interrupt to release the channel. Afterwards
371 * when it comes here from endpoint disable routine
372 * channel remains assigned.
373 */
374 if (qh->channel)
375 dwc2_release_channel_ddma(hsotg, qh);
376
377 if ((qh->ep_type == USB_ENDPOINT_XFER_ISOC ||
378 qh->ep_type == USB_ENDPOINT_XFER_INT) &&
379 (hsotg->core_params->uframe_sched > 0 ||
380 !hsotg->periodic_channels) && hsotg->frame_list) {
381 dwc2_per_sched_disable(hsotg);
382 dwc2_frame_list_free(hsotg);
383 }
384 }
385
386 static u8 dwc2_frame_to_desc_idx(struct dwc2_qh *qh, u16 frame_idx)
387 {
388 if (qh->dev_speed == USB_SPEED_HIGH)
389 /* Descriptor set (8 descriptors) index which is 8-aligned */
390 return (frame_idx & ((MAX_DMA_DESC_NUM_HS_ISOC / 8) - 1)) * 8;
391 else
392 return frame_idx & (MAX_DMA_DESC_NUM_GENERIC - 1);
393 }
394
395 /*
396 * Determine starting frame for Isochronous transfer.
397 * Few frames skipped to prevent race condition with HC.
398 */
399 static u16 dwc2_calc_starting_frame(struct dwc2_hsotg *hsotg,
400 struct dwc2_qh *qh, u16 *skip_frames)
401 {
402 u16 frame;
403
404 hsotg->frame_number = dwc2_hcd_get_frame_number(hsotg);
405
406 /* sched_frame is always frame number (not uFrame) both in FS and HS! */
407
408 /*
409 * skip_frames is used to limit activated descriptors number
410 * to avoid the situation when HC services the last activated
411 * descriptor firstly.
412 * Example for FS:
413 * Current frame is 1, scheduled frame is 3. Since HC always fetches
414 * the descriptor corresponding to curr_frame+1, the descriptor
415 * corresponding to frame 2 will be fetched. If the number of
416 * descriptors is max=64 (or greather) the list will be fully programmed
417 * with Active descriptors and it is possible case (rare) that the
418 * latest descriptor(considering rollback) corresponding to frame 2 will
419 * be serviced first. HS case is more probable because, in fact, up to
420 * 11 uframes (16 in the code) may be skipped.
421 */
422 if (qh->dev_speed == USB_SPEED_HIGH) {
423 /*
424 * Consider uframe counter also, to start xfer asap. If half of
425 * the frame elapsed skip 2 frames otherwise just 1 frame.
426 * Starting descriptor index must be 8-aligned, so if the
427 * current frame is near to complete the next one is skipped as
428 * well.
429 */
430 if (dwc2_micro_frame_num(hsotg->frame_number) >= 5) {
431 *skip_frames = 2 * 8;
432 frame = dwc2_frame_num_inc(hsotg->frame_number,
433 *skip_frames);
434 } else {
435 *skip_frames = 1 * 8;
436 frame = dwc2_frame_num_inc(hsotg->frame_number,
437 *skip_frames);
438 }
439
440 frame = dwc2_full_frame_num(frame);
441 } else {
442 /*
443 * Two frames are skipped for FS - the current and the next.
444 * But for descriptor programming, 1 frame (descriptor) is
445 * enough, see example above.
446 */
447 *skip_frames = 1;
448 frame = dwc2_frame_num_inc(hsotg->frame_number, 2);
449 }
450
451 return frame;
452 }
453
454 /*
455 * Calculate initial descriptor index for isochronous transfer based on
456 * scheduled frame
457 */
458 static u16 dwc2_recalc_initial_desc_idx(struct dwc2_hsotg *hsotg,
459 struct dwc2_qh *qh)
460 {
461 u16 frame, fr_idx, fr_idx_tmp, skip_frames;
462
463 /*
464 * With current ISOC processing algorithm the channel is being released
465 * when no more QTDs in the list (qh->ntd == 0). Thus this function is
466 * called only when qh->ntd == 0 and qh->channel == 0.
467 *
468 * So qh->channel != NULL branch is not used and just not removed from
469 * the source file. It is required for another possible approach which
470 * is, do not disable and release the channel when ISOC session
471 * completed, just move QH to inactive schedule until new QTD arrives.
472 * On new QTD, the QH moved back to 'ready' schedule, starting frame and
473 * therefore starting desc_index are recalculated. In this case channel
474 * is released only on ep_disable.
475 */
476
477 /*
478 * Calculate starting descriptor index. For INTERRUPT endpoint it is
479 * always 0.
480 */
481 if (qh->channel) {
482 frame = dwc2_calc_starting_frame(hsotg, qh, &skip_frames);
483 /*
484 * Calculate initial descriptor index based on FrameList current
485 * bitmap and servicing period
486 */
487 fr_idx_tmp = dwc2_frame_list_idx(frame);
488 fr_idx = (FRLISTEN_64_SIZE +
489 dwc2_frame_list_idx(qh->sched_frame) - fr_idx_tmp)
490 % dwc2_frame_incr_val(qh);
491 fr_idx = (fr_idx + fr_idx_tmp) % FRLISTEN_64_SIZE;
492 } else {
493 qh->sched_frame = dwc2_calc_starting_frame(hsotg, qh,
494 &skip_frames);
495 fr_idx = dwc2_frame_list_idx(qh->sched_frame);
496 }
497
498 qh->td_first = qh->td_last = dwc2_frame_to_desc_idx(qh, fr_idx);
499
500 return skip_frames;
501 }
502
503 #define ISOC_URB_GIVEBACK_ASAP
504
505 #define MAX_ISOC_XFER_SIZE_FS 1023
506 #define MAX_ISOC_XFER_SIZE_HS 3072
507 #define DESCNUM_THRESHOLD 4
508
509 static void dwc2_fill_host_isoc_dma_desc(struct dwc2_hsotg *hsotg,
510 struct dwc2_qtd *qtd,
511 struct dwc2_qh *qh, u32 max_xfer_size,
512 u16 idx)
513 {
514 struct dwc2_hcd_dma_desc *dma_desc = &qh->desc_list[idx];
515 struct dwc2_hcd_iso_packet_desc *frame_desc;
516
517 memset(dma_desc, 0, sizeof(*dma_desc));
518 frame_desc = &qtd->urb->iso_descs[qtd->isoc_frame_index_last];
519
520 if (frame_desc->length > max_xfer_size)
521 qh->n_bytes[idx] = max_xfer_size;
522 else
523 qh->n_bytes[idx] = frame_desc->length;
524
525 dma_desc->buf = (u32)(qtd->urb->dma + frame_desc->offset);
526 dma_desc->status = qh->n_bytes[idx] << HOST_DMA_ISOC_NBYTES_SHIFT &
527 HOST_DMA_ISOC_NBYTES_MASK;
528
529 #ifdef ISOC_URB_GIVEBACK_ASAP
530 /* Set IOC for each descriptor corresponding to last frame of URB */
531 if (qtd->isoc_frame_index_last == qtd->urb->packet_count)
532 dma_desc->status |= HOST_DMA_IOC;
533 #endif
534
535 qh->ntd++;
536 qtd->isoc_frame_index_last++;
537 }
538
539 static void dwc2_init_isoc_dma_desc(struct dwc2_hsotg *hsotg,
540 struct dwc2_qh *qh, u16 skip_frames)
541 {
542 struct dwc2_qtd *qtd;
543 u32 max_xfer_size;
544 u16 idx, inc, n_desc, ntd_max = 0;
545
546 idx = qh->td_last;
547 inc = qh->interval;
548 n_desc = 0;
549
550 if (qh->interval) {
551 ntd_max = (dwc2_max_desc_num(qh) + qh->interval - 1) /
552 qh->interval;
553 if (skip_frames && !qh->channel)
554 ntd_max -= skip_frames / qh->interval;
555 }
556
557 max_xfer_size = qh->dev_speed == USB_SPEED_HIGH ?
558 MAX_ISOC_XFER_SIZE_HS : MAX_ISOC_XFER_SIZE_FS;
559
560 list_for_each_entry(qtd, &qh->qtd_list, qtd_list_entry) {
561 while (qh->ntd < ntd_max && qtd->isoc_frame_index_last <
562 qtd->urb->packet_count) {
563 if (n_desc > 1)
564 qh->desc_list[n_desc - 1].status |= HOST_DMA_A;
565 dwc2_fill_host_isoc_dma_desc(hsotg, qtd, qh,
566 max_xfer_size, idx);
567 idx = dwc2_desclist_idx_inc(idx, inc, qh->dev_speed);
568 n_desc++;
569 }
570 qtd->in_process = 1;
571 }
572
573 qh->td_last = idx;
574
575 #ifdef ISOC_URB_GIVEBACK_ASAP
576 /* Set IOC for last descriptor if descriptor list is full */
577 if (qh->ntd == ntd_max) {
578 idx = dwc2_desclist_idx_dec(qh->td_last, inc, qh->dev_speed);
579 qh->desc_list[idx].status |= HOST_DMA_IOC;
580 }
581 #else
582 /*
583 * Set IOC bit only for one descriptor. Always try to be ahead of HW
584 * processing, i.e. on IOC generation driver activates next descriptor
585 * but core continues to process descriptors following the one with IOC
586 * set.
587 */
588
589 if (n_desc > DESCNUM_THRESHOLD)
590 /*
591 * Move IOC "up". Required even if there is only one QTD
592 * in the list, because QTDs might continue to be queued,
593 * but during the activation it was only one queued.
594 * Actually more than one QTD might be in the list if this
595 * function called from XferCompletion - QTDs was queued during
596 * HW processing of the previous descriptor chunk.
597 */
598 idx = dwc2_desclist_idx_dec(idx, inc * ((qh->ntd + 1) / 2),
599 qh->dev_speed);
600 else
601 /*
602 * Set the IOC for the latest descriptor if either number of
603 * descriptors is not greater than threshold or no more new
604 * descriptors activated
605 */
606 idx = dwc2_desclist_idx_dec(qh->td_last, inc, qh->dev_speed);
607
608 qh->desc_list[idx].status |= HOST_DMA_IOC;
609 #endif
610
611 if (n_desc) {
612 qh->desc_list[n_desc - 1].status |= HOST_DMA_A;
613 if (n_desc > 1)
614 qh->desc_list[0].status |= HOST_DMA_A;
615 }
616 }
617
618 static void dwc2_fill_host_dma_desc(struct dwc2_hsotg *hsotg,
619 struct dwc2_host_chan *chan,
620 struct dwc2_qtd *qtd, struct dwc2_qh *qh,
621 int n_desc)
622 {
623 struct dwc2_hcd_dma_desc *dma_desc = &qh->desc_list[n_desc];
624 int len = chan->xfer_len;
625
626 if (len > MAX_DMA_DESC_SIZE)
627 len = MAX_DMA_DESC_SIZE - chan->max_packet + 1;
628
629 if (chan->ep_is_in) {
630 int num_packets;
631
632 if (len > 0 && chan->max_packet)
633 num_packets = (len + chan->max_packet - 1)
634 / chan->max_packet;
635 else
636 /* Need 1 packet for transfer length of 0 */
637 num_packets = 1;
638
639 /* Always program an integral # of packets for IN transfers */
640 len = num_packets * chan->max_packet;
641 }
642
643 dma_desc->status = len << HOST_DMA_NBYTES_SHIFT & HOST_DMA_NBYTES_MASK;
644 qh->n_bytes[n_desc] = len;
645
646 if (qh->ep_type == USB_ENDPOINT_XFER_CONTROL &&
647 qtd->control_phase == DWC2_CONTROL_SETUP)
648 dma_desc->status |= HOST_DMA_SUP;
649
650 dma_desc->buf = (u32)chan->xfer_dma;
651
652 /*
653 * Last (or only) descriptor of IN transfer with actual size less
654 * than MaxPacket
655 */
656 if (len > chan->xfer_len) {
657 chan->xfer_len = 0;
658 } else {
659 chan->xfer_dma += len;
660 chan->xfer_len -= len;
661 }
662 }
663
664 static void dwc2_init_non_isoc_dma_desc(struct dwc2_hsotg *hsotg,
665 struct dwc2_qh *qh)
666 {
667 struct dwc2_qtd *qtd;
668 struct dwc2_host_chan *chan = qh->channel;
669 int n_desc = 0;
670
671 dev_vdbg(hsotg->dev, "%s(): qh=%p dma=%08lx len=%d\n", __func__, qh,
672 (unsigned long)chan->xfer_dma, chan->xfer_len);
673
674 /*
675 * Start with chan->xfer_dma initialized in assign_and_init_hc(), then
676 * if SG transfer consists of multiple URBs, this pointer is re-assigned
677 * to the buffer of the currently processed QTD. For non-SG request
678 * there is always one QTD active.
679 */
680
681 list_for_each_entry(qtd, &qh->qtd_list, qtd_list_entry) {
682 dev_vdbg(hsotg->dev, "qtd=%p\n", qtd);
683
684 if (n_desc) {
685 /* SG request - more than 1 QTD */
686 chan->xfer_dma = qtd->urb->dma +
687 qtd->urb->actual_length;
688 chan->xfer_len = qtd->urb->length -
689 qtd->urb->actual_length;
690 dev_vdbg(hsotg->dev, "buf=%08lx len=%d\n",
691 (unsigned long)chan->xfer_dma, chan->xfer_len);
692 }
693
694 qtd->n_desc = 0;
695 do {
696 if (n_desc > 1) {
697 qh->desc_list[n_desc - 1].status |= HOST_DMA_A;
698 dev_vdbg(hsotg->dev,
699 "set A bit in desc %d (%p)\n",
700 n_desc - 1,
701 &qh->desc_list[n_desc - 1]);
702 }
703 dwc2_fill_host_dma_desc(hsotg, chan, qtd, qh, n_desc);
704 dev_vdbg(hsotg->dev,
705 "desc %d (%p) buf=%08x status=%08x\n",
706 n_desc, &qh->desc_list[n_desc],
707 qh->desc_list[n_desc].buf,
708 qh->desc_list[n_desc].status);
709 qtd->n_desc++;
710 n_desc++;
711 } while (chan->xfer_len > 0 &&
712 n_desc != MAX_DMA_DESC_NUM_GENERIC);
713
714 dev_vdbg(hsotg->dev, "n_desc=%d\n", n_desc);
715 qtd->in_process = 1;
716 if (qh->ep_type == USB_ENDPOINT_XFER_CONTROL)
717 break;
718 if (n_desc == MAX_DMA_DESC_NUM_GENERIC)
719 break;
720 }
721
722 if (n_desc) {
723 qh->desc_list[n_desc - 1].status |=
724 HOST_DMA_IOC | HOST_DMA_EOL | HOST_DMA_A;
725 dev_vdbg(hsotg->dev, "set IOC/EOL/A bits in desc %d (%p)\n",
726 n_desc - 1, &qh->desc_list[n_desc - 1]);
727 if (n_desc > 1) {
728 qh->desc_list[0].status |= HOST_DMA_A;
729 dev_vdbg(hsotg->dev, "set A bit in desc 0 (%p)\n",
730 &qh->desc_list[0]);
731 }
732 chan->ntd = n_desc;
733 }
734 }
735
736 /**
737 * dwc2_hcd_start_xfer_ddma() - Starts a transfer in Descriptor DMA mode
738 *
739 * @hsotg: The HCD state structure for the DWC OTG controller
740 * @qh: The QH to init
741 *
742 * Return: 0 if successful, negative error code otherwise
743 *
744 * For Control and Bulk endpoints, initializes descriptor list and starts the
745 * transfer. For Interrupt and Isochronous endpoints, initializes descriptor
746 * list then updates FrameList, marking appropriate entries as active.
747 *
748 * For Isochronous endpoints the starting descriptor index is calculated based
749 * on the scheduled frame, but only on the first transfer descriptor within a
750 * session. Then the transfer is started via enabling the channel.
751 *
752 * For Isochronous endpoints the channel is not halted on XferComplete
753 * interrupt so remains assigned to the endpoint(QH) until session is done.
754 */
755 void dwc2_hcd_start_xfer_ddma(struct dwc2_hsotg *hsotg, struct dwc2_qh *qh)
756 {
757 /* Channel is already assigned */
758 struct dwc2_host_chan *chan = qh->channel;
759 u16 skip_frames = 0;
760
761 switch (chan->ep_type) {
762 case USB_ENDPOINT_XFER_CONTROL:
763 case USB_ENDPOINT_XFER_BULK:
764 dwc2_init_non_isoc_dma_desc(hsotg, qh);
765 dwc2_hc_start_transfer_ddma(hsotg, chan);
766 break;
767 case USB_ENDPOINT_XFER_INT:
768 dwc2_init_non_isoc_dma_desc(hsotg, qh);
769 dwc2_update_frame_list(hsotg, qh, 1);
770 dwc2_hc_start_transfer_ddma(hsotg, chan);
771 break;
772 case USB_ENDPOINT_XFER_ISOC:
773 if (!qh->ntd)
774 skip_frames = dwc2_recalc_initial_desc_idx(hsotg, qh);
775 dwc2_init_isoc_dma_desc(hsotg, qh, skip_frames);
776
777 if (!chan->xfer_started) {
778 dwc2_update_frame_list(hsotg, qh, 1);
779
780 /*
781 * Always set to max, instead of actual size. Otherwise
782 * ntd will be changed with channel being enabled. Not
783 * recommended.
784 */
785 chan->ntd = dwc2_max_desc_num(qh);
786
787 /* Enable channel only once for ISOC */
788 dwc2_hc_start_transfer_ddma(hsotg, chan);
789 }
790
791 break;
792 default:
793 break;
794 }
795 }
796
797 #define DWC2_CMPL_DONE 1
798 #define DWC2_CMPL_STOP 2
799
800 static int dwc2_cmpl_host_isoc_dma_desc(struct dwc2_hsotg *hsotg,
801 struct dwc2_host_chan *chan,
802 struct dwc2_qtd *qtd,
803 struct dwc2_qh *qh, u16 idx)
804 {
805 struct dwc2_hcd_dma_desc *dma_desc = &qh->desc_list[idx];
806 struct dwc2_hcd_iso_packet_desc *frame_desc;
807 u16 remain = 0;
808 int rc = 0;
809
810 if (!qtd->urb)
811 return -EINVAL;
812
813 frame_desc = &qtd->urb->iso_descs[qtd->isoc_frame_index_last];
814 dma_desc->buf = (u32)(qtd->urb->dma + frame_desc->offset);
815 if (chan->ep_is_in)
816 remain = dma_desc->status >> HOST_DMA_ISOC_NBYTES_SHIFT &
817 HOST_DMA_ISOC_NBYTES_MASK >> HOST_DMA_ISOC_NBYTES_SHIFT;
818
819 if ((dma_desc->status & HOST_DMA_STS_MASK) == HOST_DMA_STS_PKTERR) {
820 /*
821 * XactError, or unable to complete all the transactions
822 * in the scheduled micro-frame/frame, both indicated by
823 * HOST_DMA_STS_PKTERR
824 */
825 qtd->urb->error_count++;
826 frame_desc->actual_length = qh->n_bytes[idx] - remain;
827 frame_desc->status = -EPROTO;
828 } else {
829 /* Success */
830 frame_desc->actual_length = qh->n_bytes[idx] - remain;
831 frame_desc->status = 0;
832 }
833
834 if (++qtd->isoc_frame_index == qtd->urb->packet_count) {
835 /*
836 * urb->status is not used for isoc transfers here. The
837 * individual frame_desc status are used instead.
838 */
839 dwc2_host_complete(hsotg, qtd, 0);
840 dwc2_hcd_qtd_unlink_and_free(hsotg, qtd, qh);
841
842 /*
843 * This check is necessary because urb_dequeue can be called
844 * from urb complete callback (sound driver for example). All
845 * pending URBs are dequeued there, so no need for further
846 * processing.
847 */
848 if (chan->halt_status == DWC2_HC_XFER_URB_DEQUEUE)
849 return -1;
850 rc = DWC2_CMPL_DONE;
851 }
852
853 qh->ntd--;
854
855 /* Stop if IOC requested descriptor reached */
856 if (dma_desc->status & HOST_DMA_IOC)
857 rc = DWC2_CMPL_STOP;
858
859 return rc;
860 }
861
862 static void dwc2_complete_isoc_xfer_ddma(struct dwc2_hsotg *hsotg,
863 struct dwc2_host_chan *chan,
864 enum dwc2_halt_status halt_status)
865 {
866 struct dwc2_hcd_iso_packet_desc *frame_desc;
867 struct dwc2_qtd *qtd, *qtd_tmp;
868 struct dwc2_qh *qh;
869 u16 idx;
870 int rc;
871
872 qh = chan->qh;
873 idx = qh->td_first;
874
875 if (chan->halt_status == DWC2_HC_XFER_URB_DEQUEUE) {
876 list_for_each_entry(qtd, &qh->qtd_list, qtd_list_entry)
877 qtd->in_process = 0;
878 return;
879 }
880
881 if (halt_status == DWC2_HC_XFER_AHB_ERR ||
882 halt_status == DWC2_HC_XFER_BABBLE_ERR) {
883 /*
884 * Channel is halted in these error cases, considered as serious
885 * issues.
886 * Complete all URBs marking all frames as failed, irrespective
887 * whether some of the descriptors (frames) succeeded or not.
888 * Pass error code to completion routine as well, to update
889 * urb->status, some of class drivers might use it to stop
890 * queing transfer requests.
891 */
892 int err = halt_status == DWC2_HC_XFER_AHB_ERR ?
893 -EIO : -EOVERFLOW;
894
895 list_for_each_entry_safe(qtd, qtd_tmp, &qh->qtd_list,
896 qtd_list_entry) {
897 if (qtd->urb) {
898 for (idx = 0; idx < qtd->urb->packet_count;
899 idx++) {
900 frame_desc = &qtd->urb->iso_descs[idx];
901 frame_desc->status = err;
902 }
903
904 dwc2_host_complete(hsotg, qtd, err);
905 }
906
907 dwc2_hcd_qtd_unlink_and_free(hsotg, qtd, qh);
908 }
909
910 return;
911 }
912
913 list_for_each_entry_safe(qtd, qtd_tmp, &qh->qtd_list, qtd_list_entry) {
914 if (!qtd->in_process)
915 break;
916 do {
917 rc = dwc2_cmpl_host_isoc_dma_desc(hsotg, chan, qtd, qh,
918 idx);
919 if (rc < 0)
920 return;
921 idx = dwc2_desclist_idx_inc(idx, qh->interval,
922 chan->speed);
923 if (rc == DWC2_CMPL_STOP)
924 goto stop_scan;
925 if (rc == DWC2_CMPL_DONE)
926 break;
927 } while (idx != qh->td_first);
928 }
929
930 stop_scan:
931 qh->td_first = idx;
932 }
933
934 static int dwc2_update_non_isoc_urb_state_ddma(struct dwc2_hsotg *hsotg,
935 struct dwc2_host_chan *chan,
936 struct dwc2_qtd *qtd,
937 struct dwc2_hcd_dma_desc *dma_desc,
938 enum dwc2_halt_status halt_status,
939 u32 n_bytes, int *xfer_done)
940 {
941 struct dwc2_hcd_urb *urb = qtd->urb;
942 u16 remain = 0;
943
944 if (chan->ep_is_in)
945 remain = dma_desc->status >> HOST_DMA_NBYTES_SHIFT &
946 HOST_DMA_NBYTES_MASK >> HOST_DMA_NBYTES_SHIFT;
947
948 dev_vdbg(hsotg->dev, "remain=%d dwc2_urb=%p\n", remain, urb);
949
950 if (halt_status == DWC2_HC_XFER_AHB_ERR) {
951 dev_err(hsotg->dev, "EIO\n");
952 urb->status = -EIO;
953 return 1;
954 }
955
956 if ((dma_desc->status & HOST_DMA_STS_MASK) == HOST_DMA_STS_PKTERR) {
957 switch (halt_status) {
958 case DWC2_HC_XFER_STALL:
959 dev_vdbg(hsotg->dev, "Stall\n");
960 urb->status = -EPIPE;
961 break;
962 case DWC2_HC_XFER_BABBLE_ERR:
963 dev_err(hsotg->dev, "Babble\n");
964 urb->status = -EOVERFLOW;
965 break;
966 case DWC2_HC_XFER_XACT_ERR:
967 dev_err(hsotg->dev, "XactErr\n");
968 urb->status = -EPROTO;
969 break;
970 default:
971 dev_err(hsotg->dev,
972 "%s: Unhandled descriptor error status (%d)\n",
973 __func__, halt_status);
974 break;
975 }
976 return 1;
977 }
978
979 if (dma_desc->status & HOST_DMA_A) {
980 dev_vdbg(hsotg->dev,
981 "Active descriptor encountered on channel %d\n",
982 chan->hc_num);
983 return 0;
984 }
985
986 if (chan->ep_type == USB_ENDPOINT_XFER_CONTROL) {
987 if (qtd->control_phase == DWC2_CONTROL_DATA) {
988 urb->actual_length += n_bytes - remain;
989 if (remain || urb->actual_length >= urb->length) {
990 /*
991 * For Control Data stage do not set urb->status
992 * to 0, to prevent URB callback. Set it when
993 * Status phase is done. See below.
994 */
995 *xfer_done = 1;
996 }
997 } else if (qtd->control_phase == DWC2_CONTROL_STATUS) {
998 urb->status = 0;
999 *xfer_done = 1;
1000 }
1001 /* No handling for SETUP stage */
1002 } else {
1003 /* BULK and INTR */
1004 urb->actual_length += n_bytes - remain;
1005 dev_vdbg(hsotg->dev, "length=%d actual=%d\n", urb->length,
1006 urb->actual_length);
1007 if (remain || urb->actual_length >= urb->length) {
1008 urb->status = 0;
1009 *xfer_done = 1;
1010 }
1011 }
1012
1013 return 0;
1014 }
1015
1016 static int dwc2_process_non_isoc_desc(struct dwc2_hsotg *hsotg,
1017 struct dwc2_host_chan *chan,
1018 int chnum, struct dwc2_qtd *qtd,
1019 int desc_num,
1020 enum dwc2_halt_status halt_status,
1021 int *xfer_done)
1022 {
1023 struct dwc2_qh *qh = chan->qh;
1024 struct dwc2_hcd_urb *urb = qtd->urb;
1025 struct dwc2_hcd_dma_desc *dma_desc;
1026 u32 n_bytes;
1027 int failed;
1028
1029 dev_vdbg(hsotg->dev, "%s()\n", __func__);
1030
1031 if (!urb)
1032 return -EINVAL;
1033
1034 dma_desc = &qh->desc_list[desc_num];
1035 n_bytes = qh->n_bytes[desc_num];
1036 dev_vdbg(hsotg->dev,
1037 "qtd=%p dwc2_urb=%p desc_num=%d desc=%p n_bytes=%d\n",
1038 qtd, urb, desc_num, dma_desc, n_bytes);
1039 failed = dwc2_update_non_isoc_urb_state_ddma(hsotg, chan, qtd, dma_desc,
1040 halt_status, n_bytes,
1041 xfer_done);
1042 if (failed || (*xfer_done && urb->status != -EINPROGRESS)) {
1043 dwc2_host_complete(hsotg, qtd, urb->status);
1044 dwc2_hcd_qtd_unlink_and_free(hsotg, qtd, qh);
1045 dev_vdbg(hsotg->dev, "failed=%1x xfer_done=%1x status=%08x\n",
1046 failed, *xfer_done, urb->status);
1047 return failed;
1048 }
1049
1050 if (qh->ep_type == USB_ENDPOINT_XFER_CONTROL) {
1051 switch (qtd->control_phase) {
1052 case DWC2_CONTROL_SETUP:
1053 if (urb->length > 0)
1054 qtd->control_phase = DWC2_CONTROL_DATA;
1055 else
1056 qtd->control_phase = DWC2_CONTROL_STATUS;
1057 dev_vdbg(hsotg->dev,
1058 " Control setup transaction done\n");
1059 break;
1060 case DWC2_CONTROL_DATA:
1061 if (*xfer_done) {
1062 qtd->control_phase = DWC2_CONTROL_STATUS;
1063 dev_vdbg(hsotg->dev,
1064 " Control data transfer done\n");
1065 } else if (desc_num + 1 == qtd->n_desc) {
1066 /*
1067 * Last descriptor for Control data stage which
1068 * is not completed yet
1069 */
1070 dwc2_hcd_save_data_toggle(hsotg, chan, chnum,
1071 qtd);
1072 }
1073 break;
1074 default:
1075 break;
1076 }
1077 }
1078
1079 return 0;
1080 }
1081
1082 static void dwc2_complete_non_isoc_xfer_ddma(struct dwc2_hsotg *hsotg,
1083 struct dwc2_host_chan *chan,
1084 int chnum,
1085 enum dwc2_halt_status halt_status)
1086 {
1087 struct list_head *qtd_item, *qtd_tmp;
1088 struct dwc2_qh *qh = chan->qh;
1089 struct dwc2_qtd *qtd = NULL;
1090 int xfer_done;
1091 int desc_num = 0;
1092
1093 if (chan->halt_status == DWC2_HC_XFER_URB_DEQUEUE) {
1094 list_for_each_entry(qtd, &qh->qtd_list, qtd_list_entry)
1095 qtd->in_process = 0;
1096 return;
1097 }
1098
1099 list_for_each_safe(qtd_item, qtd_tmp, &qh->qtd_list) {
1100 int i;
1101
1102 qtd = list_entry(qtd_item, struct dwc2_qtd, qtd_list_entry);
1103 xfer_done = 0;
1104
1105 for (i = 0; i < qtd->n_desc; i++) {
1106 if (dwc2_process_non_isoc_desc(hsotg, chan, chnum, qtd,
1107 desc_num, halt_status,
1108 &xfer_done))
1109 break;
1110 desc_num++;
1111 }
1112 }
1113
1114 if (qh->ep_type != USB_ENDPOINT_XFER_CONTROL) {
1115 /*
1116 * Resetting the data toggle for bulk and interrupt endpoints
1117 * in case of stall. See handle_hc_stall_intr().
1118 */
1119 if (halt_status == DWC2_HC_XFER_STALL)
1120 qh->data_toggle = DWC2_HC_PID_DATA0;
1121 else if (qtd)
1122 dwc2_hcd_save_data_toggle(hsotg, chan, chnum, qtd);
1123 }
1124
1125 if (halt_status == DWC2_HC_XFER_COMPLETE) {
1126 if (chan->hcint & HCINTMSK_NYET) {
1127 /*
1128 * Got a NYET on the last transaction of the transfer.
1129 * It means that the endpoint should be in the PING
1130 * state at the beginning of the next transfer.
1131 */
1132 qh->ping_state = 1;
1133 }
1134 }
1135 }
1136
1137 /**
1138 * dwc2_hcd_complete_xfer_ddma() - Scans the descriptor list, updates URB's
1139 * status and calls completion routine for the URB if it's done. Called from
1140 * interrupt handlers.
1141 *
1142 * @hsotg: The HCD state structure for the DWC OTG controller
1143 * @chan: Host channel the transfer is completed on
1144 * @chnum: Index of Host channel registers
1145 * @halt_status: Reason the channel is being halted or just XferComplete
1146 * for isochronous transfers
1147 *
1148 * Releases the channel to be used by other transfers.
1149 * In case of Isochronous endpoint the channel is not halted until the end of
1150 * the session, i.e. QTD list is empty.
1151 * If periodic channel released the FrameList is updated accordingly.
1152 * Calls transaction selection routines to activate pending transfers.
1153 */
1154 void dwc2_hcd_complete_xfer_ddma(struct dwc2_hsotg *hsotg,
1155 struct dwc2_host_chan *chan, int chnum,
1156 enum dwc2_halt_status halt_status)
1157 {
1158 struct dwc2_qh *qh = chan->qh;
1159 int continue_isoc_xfer = 0;
1160 enum dwc2_transaction_type tr_type;
1161
1162 if (chan->ep_type == USB_ENDPOINT_XFER_ISOC) {
1163 dwc2_complete_isoc_xfer_ddma(hsotg, chan, halt_status);
1164
1165 /* Release the channel if halted or session completed */
1166 if (halt_status != DWC2_HC_XFER_COMPLETE ||
1167 list_empty(&qh->qtd_list)) {
1168 /* Halt the channel if session completed */
1169 if (halt_status == DWC2_HC_XFER_COMPLETE)
1170 dwc2_hc_halt(hsotg, chan, halt_status);
1171 dwc2_release_channel_ddma(hsotg, qh);
1172 dwc2_hcd_qh_unlink(hsotg, qh);
1173 } else {
1174 /* Keep in assigned schedule to continue transfer */
1175 list_move(&qh->qh_list_entry,
1176 &hsotg->periodic_sched_assigned);
1177 continue_isoc_xfer = 1;
1178 }
1179 /*
1180 * Todo: Consider the case when period exceeds FrameList size.
1181 * Frame Rollover interrupt should be used.
1182 */
1183 } else {
1184 /*
1185 * Scan descriptor list to complete the URB(s), then release
1186 * the channel
1187 */
1188 dwc2_complete_non_isoc_xfer_ddma(hsotg, chan, chnum,
1189 halt_status);
1190 dwc2_release_channel_ddma(hsotg, qh);
1191 dwc2_hcd_qh_unlink(hsotg, qh);
1192
1193 if (!list_empty(&qh->qtd_list)) {
1194 /*
1195 * Add back to inactive non-periodic schedule on normal
1196 * completion
1197 */
1198 dwc2_hcd_qh_add(hsotg, qh);
1199 }
1200 }
1201
1202 tr_type = dwc2_hcd_select_transactions(hsotg);
1203 if (tr_type != DWC2_TRANSACTION_NONE || continue_isoc_xfer) {
1204 if (continue_isoc_xfer) {
1205 if (tr_type == DWC2_TRANSACTION_NONE)
1206 tr_type = DWC2_TRANSACTION_PERIODIC;
1207 else if (tr_type == DWC2_TRANSACTION_NON_PERIODIC)
1208 tr_type = DWC2_TRANSACTION_ALL;
1209 }
1210 dwc2_hcd_queue_transactions(hsotg, tr_type);
1211 }
1212 }
1213