1 // SPDX-License-Identifier: (GPL-2.0+ OR BSD-3-Clause)
3 * hcd_ddma.c - DesignWare HS OTG Controller descriptor DMA routines
5 * Copyright (C) 2004-2013 Synopsys, Inc.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions, and the following disclaimer,
12 * without modification.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. The names of the above-listed copyright holders may not be used
17 * to endorse or promote products derived from this software without
18 * specific prior written permission.
20 * ALTERNATIVELY, this software may be distributed under the terms of the
21 * GNU General Public License ("GPL") as published by the Free Software
22 * Foundation; either version 2 of the License, or (at your option) any
25 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
26 * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
27 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
29 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
30 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
31 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
32 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
34 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
39 * This file contains the Descriptor DMA implementation for Host mode
41 #include <linux/kernel.h>
42 #include <linux/module.h>
43 #include <linux/spinlock.h>
44 #include <linux/interrupt.h>
45 #include <linux/dma-mapping.h>
47 #include <linux/slab.h>
48 #include <linux/usb.h>
50 #include <linux/usb/hcd.h>
51 #include <linux/usb/ch11.h>
56 static u16
dwc2_frame_list_idx(u16 frame
)
58 return frame
& (FRLISTEN_64_SIZE
- 1);
61 static u16
dwc2_desclist_idx_inc(u16 idx
, u16 inc
, u8 speed
)
64 ((speed
== USB_SPEED_HIGH
? MAX_DMA_DESC_NUM_HS_ISOC
:
65 MAX_DMA_DESC_NUM_GENERIC
) - 1);
68 static u16
dwc2_desclist_idx_dec(u16 idx
, u16 inc
, u8 speed
)
71 ((speed
== USB_SPEED_HIGH
? MAX_DMA_DESC_NUM_HS_ISOC
:
72 MAX_DMA_DESC_NUM_GENERIC
) - 1);
75 static u16
dwc2_max_desc_num(struct dwc2_qh
*qh
)
77 return (qh
->ep_type
== USB_ENDPOINT_XFER_ISOC
&&
78 qh
->dev_speed
== USB_SPEED_HIGH
) ?
79 MAX_DMA_DESC_NUM_HS_ISOC
: MAX_DMA_DESC_NUM_GENERIC
;
82 static u16
dwc2_frame_incr_val(struct dwc2_qh
*qh
)
84 return qh
->dev_speed
== USB_SPEED_HIGH
?
85 (qh
->host_interval
+ 8 - 1) / 8 : qh
->host_interval
;
88 static int dwc2_desc_list_alloc(struct dwc2_hsotg
*hsotg
, struct dwc2_qh
*qh
,
91 struct kmem_cache
*desc_cache
;
93 if (qh
->ep_type
== USB_ENDPOINT_XFER_ISOC
&&
94 qh
->dev_speed
== USB_SPEED_HIGH
)
95 desc_cache
= hsotg
->desc_hsisoc_cache
;
97 desc_cache
= hsotg
->desc_gen_cache
;
99 qh
->desc_list_sz
= sizeof(struct dwc2_dma_desc
) *
100 dwc2_max_desc_num(qh
);
102 qh
->desc_list
= kmem_cache_zalloc(desc_cache
, flags
| GFP_DMA
);
106 qh
->desc_list_dma
= dma_map_single(hsotg
->dev
, qh
->desc_list
,
110 qh
->n_bytes
= kcalloc(dwc2_max_desc_num(qh
), sizeof(u32
), flags
);
112 dma_unmap_single(hsotg
->dev
, qh
->desc_list_dma
,
115 kmem_cache_free(desc_cache
, qh
->desc_list
);
116 qh
->desc_list
= NULL
;
123 static void dwc2_desc_list_free(struct dwc2_hsotg
*hsotg
, struct dwc2_qh
*qh
)
125 struct kmem_cache
*desc_cache
;
127 if (qh
->ep_type
== USB_ENDPOINT_XFER_ISOC
&&
128 qh
->dev_speed
== USB_SPEED_HIGH
)
129 desc_cache
= hsotg
->desc_hsisoc_cache
;
131 desc_cache
= hsotg
->desc_gen_cache
;
134 dma_unmap_single(hsotg
->dev
, qh
->desc_list_dma
,
135 qh
->desc_list_sz
, DMA_FROM_DEVICE
);
136 kmem_cache_free(desc_cache
, qh
->desc_list
);
137 qh
->desc_list
= NULL
;
144 static int dwc2_frame_list_alloc(struct dwc2_hsotg
*hsotg
, gfp_t mem_flags
)
146 if (hsotg
->frame_list
)
149 hsotg
->frame_list_sz
= 4 * FRLISTEN_64_SIZE
;
150 hsotg
->frame_list
= kzalloc(hsotg
->frame_list_sz
, GFP_ATOMIC
| GFP_DMA
);
151 if (!hsotg
->frame_list
)
154 hsotg
->frame_list_dma
= dma_map_single(hsotg
->dev
, hsotg
->frame_list
,
155 hsotg
->frame_list_sz
,
161 static void dwc2_frame_list_free(struct dwc2_hsotg
*hsotg
)
165 spin_lock_irqsave(&hsotg
->lock
, flags
);
167 if (!hsotg
->frame_list
) {
168 spin_unlock_irqrestore(&hsotg
->lock
, flags
);
172 dma_unmap_single(hsotg
->dev
, hsotg
->frame_list_dma
,
173 hsotg
->frame_list_sz
, DMA_FROM_DEVICE
);
175 kfree(hsotg
->frame_list
);
176 hsotg
->frame_list
= NULL
;
178 spin_unlock_irqrestore(&hsotg
->lock
, flags
);
181 static void dwc2_per_sched_enable(struct dwc2_hsotg
*hsotg
, u32 fr_list_en
)
186 spin_lock_irqsave(&hsotg
->lock
, flags
);
188 hcfg
= dwc2_readl(hsotg
, HCFG
);
189 if (hcfg
& HCFG_PERSCHEDENA
) {
190 /* already enabled */
191 spin_unlock_irqrestore(&hsotg
->lock
, flags
);
195 dwc2_writel(hsotg
, hsotg
->frame_list_dma
, HFLBADDR
);
197 hcfg
&= ~HCFG_FRLISTEN_MASK
;
198 hcfg
|= fr_list_en
| HCFG_PERSCHEDENA
;
199 dev_vdbg(hsotg
->dev
, "Enabling Periodic schedule\n");
200 dwc2_writel(hsotg
, hcfg
, HCFG
);
202 spin_unlock_irqrestore(&hsotg
->lock
, flags
);
205 static void dwc2_per_sched_disable(struct dwc2_hsotg
*hsotg
)
210 spin_lock_irqsave(&hsotg
->lock
, flags
);
212 hcfg
= dwc2_readl(hsotg
, HCFG
);
213 if (!(hcfg
& HCFG_PERSCHEDENA
)) {
214 /* already disabled */
215 spin_unlock_irqrestore(&hsotg
->lock
, flags
);
219 hcfg
&= ~HCFG_PERSCHEDENA
;
220 dev_vdbg(hsotg
->dev
, "Disabling Periodic schedule\n");
221 dwc2_writel(hsotg
, hcfg
, HCFG
);
223 spin_unlock_irqrestore(&hsotg
->lock
, flags
);
227 * Activates/Deactivates FrameList entries for the channel based on endpoint
230 static void dwc2_update_frame_list(struct dwc2_hsotg
*hsotg
, struct dwc2_qh
*qh
,
233 struct dwc2_host_chan
*chan
;
237 pr_err("hsotg = %p\n", hsotg
);
242 dev_err(hsotg
->dev
, "qh->channel = %p\n", qh
->channel
);
246 if (!hsotg
->frame_list
) {
247 dev_err(hsotg
->dev
, "hsotg->frame_list = %p\n",
253 inc
= dwc2_frame_incr_val(qh
);
254 if (qh
->ep_type
== USB_ENDPOINT_XFER_ISOC
)
255 i
= dwc2_frame_list_idx(qh
->next_active_frame
);
262 hsotg
->frame_list
[j
] |= 1 << chan
->hc_num
;
264 hsotg
->frame_list
[j
] &= ~(1 << chan
->hc_num
);
265 j
= (j
+ inc
) & (FRLISTEN_64_SIZE
- 1);
269 * Sync frame list since controller will access it if periodic
270 * channel is currently enabled.
272 dma_sync_single_for_device(hsotg
->dev
,
273 hsotg
->frame_list_dma
,
274 hsotg
->frame_list_sz
,
281 if (chan
->speed
== USB_SPEED_HIGH
&& qh
->host_interval
) {
283 /* TODO - check this */
284 inc
= (8 + qh
->host_interval
- 1) / qh
->host_interval
;
285 for (i
= 0; i
< inc
; i
++) {
287 j
= j
<< qh
->host_interval
;
290 chan
->schinfo
= 0xff;
294 static void dwc2_release_channel_ddma(struct dwc2_hsotg
*hsotg
,
297 struct dwc2_host_chan
*chan
= qh
->channel
;
299 if (dwc2_qh_is_non_per(qh
)) {
300 if (hsotg
->params
.uframe_sched
)
301 hsotg
->available_host_channels
++;
303 hsotg
->non_periodic_channels
--;
305 dwc2_update_frame_list(hsotg
, qh
, 0);
306 hsotg
->available_host_channels
++;
310 * The condition is added to prevent double cleanup try in case of
311 * device disconnect. See channel cleanup in dwc2_hcd_disconnect().
314 if (!list_empty(&chan
->hc_list_entry
))
315 list_del(&chan
->hc_list_entry
);
316 dwc2_hc_cleanup(hsotg
, chan
);
317 list_add_tail(&chan
->hc_list_entry
, &hsotg
->free_hc_list
);
325 memset(qh
->desc_list
, 0, sizeof(struct dwc2_dma_desc
) *
326 dwc2_max_desc_num(qh
));
330 * dwc2_hcd_qh_init_ddma() - Initializes a QH structure's Descriptor DMA
333 * @hsotg: The HCD state structure for the DWC OTG controller
334 * @qh: The QH to init
335 * @mem_flags: Indicates the type of memory allocation
337 * Return: 0 if successful, negative error code otherwise
339 * Allocates memory for the descriptor list. For the first periodic QH,
340 * allocates memory for the FrameList and enables periodic scheduling.
342 int dwc2_hcd_qh_init_ddma(struct dwc2_hsotg
*hsotg
, struct dwc2_qh
*qh
,
349 "SPLIT Transfers are not supported in Descriptor DMA mode.\n");
354 retval
= dwc2_desc_list_alloc(hsotg
, qh
, mem_flags
);
358 if (qh
->ep_type
== USB_ENDPOINT_XFER_ISOC
||
359 qh
->ep_type
== USB_ENDPOINT_XFER_INT
) {
360 if (!hsotg
->frame_list
) {
361 retval
= dwc2_frame_list_alloc(hsotg
, mem_flags
);
364 /* Enable periodic schedule on first periodic QH */
365 dwc2_per_sched_enable(hsotg
, HCFG_FRLISTEN_64
);
373 dwc2_desc_list_free(hsotg
, qh
);
379 * dwc2_hcd_qh_free_ddma() - Frees a QH structure's Descriptor DMA related
382 * @hsotg: The HCD state structure for the DWC OTG controller
383 * @qh: The QH to free
385 * Frees descriptor list memory associated with the QH. If QH is periodic and
386 * the last, frees FrameList memory and disables periodic scheduling.
388 void dwc2_hcd_qh_free_ddma(struct dwc2_hsotg
*hsotg
, struct dwc2_qh
*qh
)
392 dwc2_desc_list_free(hsotg
, qh
);
395 * Channel still assigned due to some reasons.
396 * Seen on Isoc URB dequeue. Channel halted but no subsequent
397 * ChHalted interrupt to release the channel. Afterwards
398 * when it comes here from endpoint disable routine
399 * channel remains assigned.
401 spin_lock_irqsave(&hsotg
->lock
, flags
);
403 dwc2_release_channel_ddma(hsotg
, qh
);
404 spin_unlock_irqrestore(&hsotg
->lock
, flags
);
406 if ((qh
->ep_type
== USB_ENDPOINT_XFER_ISOC
||
407 qh
->ep_type
== USB_ENDPOINT_XFER_INT
) &&
408 (hsotg
->params
.uframe_sched
||
409 !hsotg
->periodic_channels
) && hsotg
->frame_list
) {
410 dwc2_per_sched_disable(hsotg
);
411 dwc2_frame_list_free(hsotg
);
415 static u8
dwc2_frame_to_desc_idx(struct dwc2_qh
*qh
, u16 frame_idx
)
417 if (qh
->dev_speed
== USB_SPEED_HIGH
)
418 /* Descriptor set (8 descriptors) index which is 8-aligned */
419 return (frame_idx
& ((MAX_DMA_DESC_NUM_HS_ISOC
/ 8) - 1)) * 8;
421 return frame_idx
& (MAX_DMA_DESC_NUM_GENERIC
- 1);
425 * Determine starting frame for Isochronous transfer.
426 * Few frames skipped to prevent race condition with HC.
428 static u16
dwc2_calc_starting_frame(struct dwc2_hsotg
*hsotg
,
429 struct dwc2_qh
*qh
, u16
*skip_frames
)
433 hsotg
->frame_number
= dwc2_hcd_get_frame_number(hsotg
);
436 * next_active_frame is always frame number (not uFrame) both in FS
441 * skip_frames is used to limit activated descriptors number
442 * to avoid the situation when HC services the last activated
443 * descriptor firstly.
445 * Current frame is 1, scheduled frame is 3. Since HC always fetches
446 * the descriptor corresponding to curr_frame+1, the descriptor
447 * corresponding to frame 2 will be fetched. If the number of
448 * descriptors is max=64 (or greather) the list will be fully programmed
449 * with Active descriptors and it is possible case (rare) that the
450 * latest descriptor(considering rollback) corresponding to frame 2 will
451 * be serviced first. HS case is more probable because, in fact, up to
452 * 11 uframes (16 in the code) may be skipped.
454 if (qh
->dev_speed
== USB_SPEED_HIGH
) {
456 * Consider uframe counter also, to start xfer asap. If half of
457 * the frame elapsed skip 2 frames otherwise just 1 frame.
458 * Starting descriptor index must be 8-aligned, so if the
459 * current frame is near to complete the next one is skipped as
462 if (dwc2_micro_frame_num(hsotg
->frame_number
) >= 5) {
463 *skip_frames
= 2 * 8;
464 frame
= dwc2_frame_num_inc(hsotg
->frame_number
,
467 *skip_frames
= 1 * 8;
468 frame
= dwc2_frame_num_inc(hsotg
->frame_number
,
472 frame
= dwc2_full_frame_num(frame
);
475 * Two frames are skipped for FS - the current and the next.
476 * But for descriptor programming, 1 frame (descriptor) is
477 * enough, see example above.
480 frame
= dwc2_frame_num_inc(hsotg
->frame_number
, 2);
487 * Calculate initial descriptor index for isochronous transfer based on
490 static u16
dwc2_recalc_initial_desc_idx(struct dwc2_hsotg
*hsotg
,
493 u16 frame
, fr_idx
, fr_idx_tmp
, skip_frames
;
496 * With current ISOC processing algorithm the channel is being released
497 * when no more QTDs in the list (qh->ntd == 0). Thus this function is
498 * called only when qh->ntd == 0 and qh->channel == 0.
500 * So qh->channel != NULL branch is not used and just not removed from
501 * the source file. It is required for another possible approach which
502 * is, do not disable and release the channel when ISOC session
503 * completed, just move QH to inactive schedule until new QTD arrives.
504 * On new QTD, the QH moved back to 'ready' schedule, starting frame and
505 * therefore starting desc_index are recalculated. In this case channel
506 * is released only on ep_disable.
510 * Calculate starting descriptor index. For INTERRUPT endpoint it is
514 frame
= dwc2_calc_starting_frame(hsotg
, qh
, &skip_frames
);
516 * Calculate initial descriptor index based on FrameList current
517 * bitmap and servicing period
519 fr_idx_tmp
= dwc2_frame_list_idx(frame
);
520 fr_idx
= (FRLISTEN_64_SIZE
+
521 dwc2_frame_list_idx(qh
->next_active_frame
) -
522 fr_idx_tmp
) % dwc2_frame_incr_val(qh
);
523 fr_idx
= (fr_idx
+ fr_idx_tmp
) % FRLISTEN_64_SIZE
;
525 qh
->next_active_frame
= dwc2_calc_starting_frame(hsotg
, qh
,
527 fr_idx
= dwc2_frame_list_idx(qh
->next_active_frame
);
530 qh
->td_first
= qh
->td_last
= dwc2_frame_to_desc_idx(qh
, fr_idx
);
535 #define ISOC_URB_GIVEBACK_ASAP
537 #define MAX_ISOC_XFER_SIZE_FS 1023
538 #define MAX_ISOC_XFER_SIZE_HS 3072
539 #define DESCNUM_THRESHOLD 4
541 static void dwc2_fill_host_isoc_dma_desc(struct dwc2_hsotg
*hsotg
,
542 struct dwc2_qtd
*qtd
,
543 struct dwc2_qh
*qh
, u32 max_xfer_size
,
546 struct dwc2_dma_desc
*dma_desc
= &qh
->desc_list
[idx
];
547 struct dwc2_hcd_iso_packet_desc
*frame_desc
;
549 memset(dma_desc
, 0, sizeof(*dma_desc
));
550 frame_desc
= &qtd
->urb
->iso_descs
[qtd
->isoc_frame_index_last
];
552 if (frame_desc
->length
> max_xfer_size
)
553 qh
->n_bytes
[idx
] = max_xfer_size
;
555 qh
->n_bytes
[idx
] = frame_desc
->length
;
557 dma_desc
->buf
= (u32
)(qtd
->urb
->dma
+ frame_desc
->offset
);
558 dma_desc
->status
= qh
->n_bytes
[idx
] << HOST_DMA_ISOC_NBYTES_SHIFT
&
559 HOST_DMA_ISOC_NBYTES_MASK
;
562 dma_desc
->status
|= HOST_DMA_A
;
565 qtd
->isoc_frame_index_last
++;
567 #ifdef ISOC_URB_GIVEBACK_ASAP
568 /* Set IOC for each descriptor corresponding to last frame of URB */
569 if (qtd
->isoc_frame_index_last
== qtd
->urb
->packet_count
)
570 dma_desc
->status
|= HOST_DMA_IOC
;
573 dma_sync_single_for_device(hsotg
->dev
,
575 (idx
* sizeof(struct dwc2_dma_desc
)),
576 sizeof(struct dwc2_dma_desc
),
580 static void dwc2_init_isoc_dma_desc(struct dwc2_hsotg
*hsotg
,
581 struct dwc2_qh
*qh
, u16 skip_frames
)
583 struct dwc2_qtd
*qtd
;
585 u16 idx
, inc
, n_desc
= 0, ntd_max
= 0;
590 inc
= qh
->host_interval
;
591 hsotg
->frame_number
= dwc2_hcd_get_frame_number(hsotg
);
592 cur_idx
= dwc2_frame_list_idx(hsotg
->frame_number
);
593 next_idx
= dwc2_desclist_idx_inc(qh
->td_last
, inc
, qh
->dev_speed
);
596 * Ensure current frame number didn't overstep last scheduled
597 * descriptor. If it happens, the only way to recover is to move
598 * qh->td_last to current frame number + 1.
599 * So that next isoc descriptor will be scheduled on frame number + 1
600 * and not on a past frame.
602 if (dwc2_frame_idx_num_gt(cur_idx
, next_idx
) || (cur_idx
== next_idx
)) {
605 "current frame number overstep last descriptor\n");
606 qh
->td_last
= dwc2_desclist_idx_inc(cur_idx
, inc
,
612 if (qh
->host_interval
) {
613 ntd_max
= (dwc2_max_desc_num(qh
) + qh
->host_interval
- 1) /
615 if (skip_frames
&& !qh
->channel
)
616 ntd_max
-= skip_frames
/ qh
->host_interval
;
619 max_xfer_size
= qh
->dev_speed
== USB_SPEED_HIGH
?
620 MAX_ISOC_XFER_SIZE_HS
: MAX_ISOC_XFER_SIZE_FS
;
622 list_for_each_entry(qtd
, &qh
->qtd_list
, qtd_list_entry
) {
623 if (qtd
->in_process
&&
624 qtd
->isoc_frame_index_last
==
625 qtd
->urb
->packet_count
)
628 qtd
->isoc_td_first
= idx
;
629 while (qh
->ntd
< ntd_max
&& qtd
->isoc_frame_index_last
<
630 qtd
->urb
->packet_count
) {
631 dwc2_fill_host_isoc_dma_desc(hsotg
, qtd
, qh
,
633 idx
= dwc2_desclist_idx_inc(idx
, inc
, qh
->dev_speed
);
636 qtd
->isoc_td_last
= idx
;
642 #ifdef ISOC_URB_GIVEBACK_ASAP
643 /* Set IOC for last descriptor if descriptor list is full */
644 if (qh
->ntd
== ntd_max
) {
645 idx
= dwc2_desclist_idx_dec(qh
->td_last
, inc
, qh
->dev_speed
);
646 qh
->desc_list
[idx
].status
|= HOST_DMA_IOC
;
647 dma_sync_single_for_device(hsotg
->dev
,
648 qh
->desc_list_dma
+ (idx
*
649 sizeof(struct dwc2_dma_desc
)),
650 sizeof(struct dwc2_dma_desc
),
655 * Set IOC bit only for one descriptor. Always try to be ahead of HW
656 * processing, i.e. on IOC generation driver activates next descriptor
657 * but core continues to process descriptors following the one with IOC
661 if (n_desc
> DESCNUM_THRESHOLD
)
663 * Move IOC "up". Required even if there is only one QTD
664 * in the list, because QTDs might continue to be queued,
665 * but during the activation it was only one queued.
666 * Actually more than one QTD might be in the list if this
667 * function called from XferCompletion - QTDs was queued during
668 * HW processing of the previous descriptor chunk.
670 idx
= dwc2_desclist_idx_dec(idx
, inc
* ((qh
->ntd
+ 1) / 2),
674 * Set the IOC for the latest descriptor if either number of
675 * descriptors is not greater than threshold or no more new
676 * descriptors activated
678 idx
= dwc2_desclist_idx_dec(qh
->td_last
, inc
, qh
->dev_speed
);
680 qh
->desc_list
[idx
].status
|= HOST_DMA_IOC
;
681 dma_sync_single_for_device(hsotg
->dev
,
683 (idx
* sizeof(struct dwc2_dma_desc
)),
684 sizeof(struct dwc2_dma_desc
),
689 static void dwc2_fill_host_dma_desc(struct dwc2_hsotg
*hsotg
,
690 struct dwc2_host_chan
*chan
,
691 struct dwc2_qtd
*qtd
, struct dwc2_qh
*qh
,
694 struct dwc2_dma_desc
*dma_desc
= &qh
->desc_list
[n_desc
];
695 int len
= chan
->xfer_len
;
697 if (len
> HOST_DMA_NBYTES_LIMIT
- (chan
->max_packet
- 1))
698 len
= HOST_DMA_NBYTES_LIMIT
- (chan
->max_packet
- 1);
700 if (chan
->ep_is_in
) {
703 if (len
> 0 && chan
->max_packet
)
704 num_packets
= (len
+ chan
->max_packet
- 1)
707 /* Need 1 packet for transfer length of 0 */
710 /* Always program an integral # of packets for IN transfers */
711 len
= num_packets
* chan
->max_packet
;
714 dma_desc
->status
= len
<< HOST_DMA_NBYTES_SHIFT
& HOST_DMA_NBYTES_MASK
;
715 qh
->n_bytes
[n_desc
] = len
;
717 if (qh
->ep_type
== USB_ENDPOINT_XFER_CONTROL
&&
718 qtd
->control_phase
== DWC2_CONTROL_SETUP
)
719 dma_desc
->status
|= HOST_DMA_SUP
;
721 dma_desc
->buf
= (u32
)chan
->xfer_dma
;
723 dma_sync_single_for_device(hsotg
->dev
,
725 (n_desc
* sizeof(struct dwc2_dma_desc
)),
726 sizeof(struct dwc2_dma_desc
),
730 * Last (or only) descriptor of IN transfer with actual size less
733 if (len
> chan
->xfer_len
) {
736 chan
->xfer_dma
+= len
;
737 chan
->xfer_len
-= len
;
741 static void dwc2_init_non_isoc_dma_desc(struct dwc2_hsotg
*hsotg
,
744 struct dwc2_qtd
*qtd
;
745 struct dwc2_host_chan
*chan
= qh
->channel
;
748 dev_vdbg(hsotg
->dev
, "%s(): qh=%p dma=%08lx len=%d\n", __func__
, qh
,
749 (unsigned long)chan
->xfer_dma
, chan
->xfer_len
);
752 * Start with chan->xfer_dma initialized in assign_and_init_hc(), then
753 * if SG transfer consists of multiple URBs, this pointer is re-assigned
754 * to the buffer of the currently processed QTD. For non-SG request
755 * there is always one QTD active.
758 list_for_each_entry(qtd
, &qh
->qtd_list
, qtd_list_entry
) {
759 dev_vdbg(hsotg
->dev
, "qtd=%p\n", qtd
);
762 /* SG request - more than 1 QTD */
763 chan
->xfer_dma
= qtd
->urb
->dma
+
764 qtd
->urb
->actual_length
;
765 chan
->xfer_len
= qtd
->urb
->length
-
766 qtd
->urb
->actual_length
;
767 dev_vdbg(hsotg
->dev
, "buf=%08lx len=%d\n",
768 (unsigned long)chan
->xfer_dma
, chan
->xfer_len
);
774 qh
->desc_list
[n_desc
- 1].status
|= HOST_DMA_A
;
776 "set A bit in desc %d (%p)\n",
778 &qh
->desc_list
[n_desc
- 1]);
779 dma_sync_single_for_device(hsotg
->dev
,
782 sizeof(struct dwc2_dma_desc
)),
783 sizeof(struct dwc2_dma_desc
),
786 dwc2_fill_host_dma_desc(hsotg
, chan
, qtd
, qh
, n_desc
);
788 "desc %d (%p) buf=%08x status=%08x\n",
789 n_desc
, &qh
->desc_list
[n_desc
],
790 qh
->desc_list
[n_desc
].buf
,
791 qh
->desc_list
[n_desc
].status
);
794 } while (chan
->xfer_len
> 0 &&
795 n_desc
!= MAX_DMA_DESC_NUM_GENERIC
);
797 dev_vdbg(hsotg
->dev
, "n_desc=%d\n", n_desc
);
799 if (qh
->ep_type
== USB_ENDPOINT_XFER_CONTROL
)
801 if (n_desc
== MAX_DMA_DESC_NUM_GENERIC
)
806 qh
->desc_list
[n_desc
- 1].status
|=
807 HOST_DMA_IOC
| HOST_DMA_EOL
| HOST_DMA_A
;
808 dev_vdbg(hsotg
->dev
, "set IOC/EOL/A bits in desc %d (%p)\n",
809 n_desc
- 1, &qh
->desc_list
[n_desc
- 1]);
810 dma_sync_single_for_device(hsotg
->dev
,
811 qh
->desc_list_dma
+ (n_desc
- 1) *
812 sizeof(struct dwc2_dma_desc
),
813 sizeof(struct dwc2_dma_desc
),
816 qh
->desc_list
[0].status
|= HOST_DMA_A
;
817 dev_vdbg(hsotg
->dev
, "set A bit in desc 0 (%p)\n",
819 dma_sync_single_for_device(hsotg
->dev
,
821 sizeof(struct dwc2_dma_desc
),
829 * dwc2_hcd_start_xfer_ddma() - Starts a transfer in Descriptor DMA mode
831 * @hsotg: The HCD state structure for the DWC OTG controller
832 * @qh: The QH to init
834 * Return: 0 if successful, negative error code otherwise
836 * For Control and Bulk endpoints, initializes descriptor list and starts the
837 * transfer. For Interrupt and Isochronous endpoints, initializes descriptor
838 * list then updates FrameList, marking appropriate entries as active.
840 * For Isochronous endpoints the starting descriptor index is calculated based
841 * on the scheduled frame, but only on the first transfer descriptor within a
842 * session. Then the transfer is started via enabling the channel.
844 * For Isochronous endpoints the channel is not halted on XferComplete
845 * interrupt so remains assigned to the endpoint(QH) until session is done.
847 void dwc2_hcd_start_xfer_ddma(struct dwc2_hsotg
*hsotg
, struct dwc2_qh
*qh
)
849 /* Channel is already assigned */
850 struct dwc2_host_chan
*chan
= qh
->channel
;
853 switch (chan
->ep_type
) {
854 case USB_ENDPOINT_XFER_CONTROL
:
855 case USB_ENDPOINT_XFER_BULK
:
856 dwc2_init_non_isoc_dma_desc(hsotg
, qh
);
857 dwc2_hc_start_transfer_ddma(hsotg
, chan
);
859 case USB_ENDPOINT_XFER_INT
:
860 dwc2_init_non_isoc_dma_desc(hsotg
, qh
);
861 dwc2_update_frame_list(hsotg
, qh
, 1);
862 dwc2_hc_start_transfer_ddma(hsotg
, chan
);
864 case USB_ENDPOINT_XFER_ISOC
:
866 skip_frames
= dwc2_recalc_initial_desc_idx(hsotg
, qh
);
867 dwc2_init_isoc_dma_desc(hsotg
, qh
, skip_frames
);
869 if (!chan
->xfer_started
) {
870 dwc2_update_frame_list(hsotg
, qh
, 1);
873 * Always set to max, instead of actual size. Otherwise
874 * ntd will be changed with channel being enabled. Not
877 chan
->ntd
= dwc2_max_desc_num(qh
);
879 /* Enable channel only once for ISOC */
880 dwc2_hc_start_transfer_ddma(hsotg
, chan
);
889 #define DWC2_CMPL_DONE 1
890 #define DWC2_CMPL_STOP 2
892 static int dwc2_cmpl_host_isoc_dma_desc(struct dwc2_hsotg
*hsotg
,
893 struct dwc2_host_chan
*chan
,
894 struct dwc2_qtd
*qtd
,
895 struct dwc2_qh
*qh
, u16 idx
)
897 struct dwc2_dma_desc
*dma_desc
;
898 struct dwc2_hcd_iso_packet_desc
*frame_desc
;
905 dma_sync_single_for_cpu(hsotg
->dev
, qh
->desc_list_dma
+ (idx
*
906 sizeof(struct dwc2_dma_desc
)),
907 sizeof(struct dwc2_dma_desc
),
910 dma_desc
= &qh
->desc_list
[idx
];
912 frame_desc
= &qtd
->urb
->iso_descs
[qtd
->isoc_frame_index_last
];
913 dma_desc
->buf
= (u32
)(qtd
->urb
->dma
+ frame_desc
->offset
);
915 remain
= (dma_desc
->status
& HOST_DMA_ISOC_NBYTES_MASK
) >>
916 HOST_DMA_ISOC_NBYTES_SHIFT
;
918 if ((dma_desc
->status
& HOST_DMA_STS_MASK
) == HOST_DMA_STS_PKTERR
) {
920 * XactError, or unable to complete all the transactions
921 * in the scheduled micro-frame/frame, both indicated by
922 * HOST_DMA_STS_PKTERR
924 qtd
->urb
->error_count
++;
925 frame_desc
->actual_length
= qh
->n_bytes
[idx
] - remain
;
926 frame_desc
->status
= -EPROTO
;
929 frame_desc
->actual_length
= qh
->n_bytes
[idx
] - remain
;
930 frame_desc
->status
= 0;
933 if (++qtd
->isoc_frame_index
== qtd
->urb
->packet_count
) {
935 * urb->status is not used for isoc transfers here. The
936 * individual frame_desc status are used instead.
938 dwc2_host_complete(hsotg
, qtd
, 0);
939 dwc2_hcd_qtd_unlink_and_free(hsotg
, qtd
, qh
);
942 * This check is necessary because urb_dequeue can be called
943 * from urb complete callback (sound driver for example). All
944 * pending URBs are dequeued there, so no need for further
947 if (chan
->halt_status
== DWC2_HC_XFER_URB_DEQUEUE
)
954 /* Stop if IOC requested descriptor reached */
955 if (dma_desc
->status
& HOST_DMA_IOC
)
961 static void dwc2_complete_isoc_xfer_ddma(struct dwc2_hsotg
*hsotg
,
962 struct dwc2_host_chan
*chan
,
963 enum dwc2_halt_status halt_status
)
965 struct dwc2_hcd_iso_packet_desc
*frame_desc
;
966 struct dwc2_qtd
*qtd
, *qtd_tmp
;
974 if (chan
->halt_status
== DWC2_HC_XFER_URB_DEQUEUE
) {
975 list_for_each_entry(qtd
, &qh
->qtd_list
, qtd_list_entry
)
980 if (halt_status
== DWC2_HC_XFER_AHB_ERR
||
981 halt_status
== DWC2_HC_XFER_BABBLE_ERR
) {
983 * Channel is halted in these error cases, considered as serious
985 * Complete all URBs marking all frames as failed, irrespective
986 * whether some of the descriptors (frames) succeeded or not.
987 * Pass error code to completion routine as well, to update
988 * urb->status, some of class drivers might use it to stop
989 * queing transfer requests.
991 int err
= halt_status
== DWC2_HC_XFER_AHB_ERR
?
994 list_for_each_entry_safe(qtd
, qtd_tmp
, &qh
->qtd_list
,
997 for (idx
= 0; idx
< qtd
->urb
->packet_count
;
999 frame_desc
= &qtd
->urb
->iso_descs
[idx
];
1000 frame_desc
->status
= err
;
1003 dwc2_host_complete(hsotg
, qtd
, err
);
1006 dwc2_hcd_qtd_unlink_and_free(hsotg
, qtd
, qh
);
1012 list_for_each_entry_safe(qtd
, qtd_tmp
, &qh
->qtd_list
, qtd_list_entry
) {
1013 if (!qtd
->in_process
)
1017 * Ensure idx corresponds to descriptor where first urb of this
1018 * qtd was added. In fact, during isoc desc init, dwc2 may skip
1019 * an index if current frame number is already over this index.
1021 if (idx
!= qtd
->isoc_td_first
) {
1022 dev_vdbg(hsotg
->dev
,
1023 "try to complete %d instead of %d\n",
1024 idx
, qtd
->isoc_td_first
);
1025 idx
= qtd
->isoc_td_first
;
1029 struct dwc2_qtd
*qtd_next
;
1032 rc
= dwc2_cmpl_host_isoc_dma_desc(hsotg
, chan
, qtd
, qh
,
1036 idx
= dwc2_desclist_idx_inc(idx
, qh
->host_interval
,
1041 if (rc
== DWC2_CMPL_DONE
)
1044 /* rc == DWC2_CMPL_STOP */
1046 if (qh
->host_interval
>= 32)
1050 cur_idx
= dwc2_frame_list_idx(hsotg
->frame_number
);
1051 qtd_next
= list_first_entry(&qh
->qtd_list
,
1054 if (dwc2_frame_idx_num_gt(cur_idx
,
1055 qtd_next
->isoc_td_last
))
1060 } while (idx
!= qh
->td_first
);
1067 static int dwc2_update_non_isoc_urb_state_ddma(struct dwc2_hsotg
*hsotg
,
1068 struct dwc2_host_chan
*chan
,
1069 struct dwc2_qtd
*qtd
,
1070 struct dwc2_dma_desc
*dma_desc
,
1071 enum dwc2_halt_status halt_status
,
1072 u32 n_bytes
, int *xfer_done
)
1074 struct dwc2_hcd_urb
*urb
= qtd
->urb
;
1078 remain
= (dma_desc
->status
& HOST_DMA_NBYTES_MASK
) >>
1079 HOST_DMA_NBYTES_SHIFT
;
1081 dev_vdbg(hsotg
->dev
, "remain=%d dwc2_urb=%p\n", remain
, urb
);
1083 if (halt_status
== DWC2_HC_XFER_AHB_ERR
) {
1084 dev_err(hsotg
->dev
, "EIO\n");
1089 if ((dma_desc
->status
& HOST_DMA_STS_MASK
) == HOST_DMA_STS_PKTERR
) {
1090 switch (halt_status
) {
1091 case DWC2_HC_XFER_STALL
:
1092 dev_vdbg(hsotg
->dev
, "Stall\n");
1093 urb
->status
= -EPIPE
;
1095 case DWC2_HC_XFER_BABBLE_ERR
:
1096 dev_err(hsotg
->dev
, "Babble\n");
1097 urb
->status
= -EOVERFLOW
;
1099 case DWC2_HC_XFER_XACT_ERR
:
1100 dev_err(hsotg
->dev
, "XactErr\n");
1101 urb
->status
= -EPROTO
;
1105 "%s: Unhandled descriptor error status (%d)\n",
1106 __func__
, halt_status
);
1112 if (dma_desc
->status
& HOST_DMA_A
) {
1113 dev_vdbg(hsotg
->dev
,
1114 "Active descriptor encountered on channel %d\n",
1119 if (chan
->ep_type
== USB_ENDPOINT_XFER_CONTROL
) {
1120 if (qtd
->control_phase
== DWC2_CONTROL_DATA
) {
1121 urb
->actual_length
+= n_bytes
- remain
;
1122 if (remain
|| urb
->actual_length
>= urb
->length
) {
1124 * For Control Data stage do not set urb->status
1125 * to 0, to prevent URB callback. Set it when
1126 * Status phase is done. See below.
1130 } else if (qtd
->control_phase
== DWC2_CONTROL_STATUS
) {
1134 /* No handling for SETUP stage */
1137 urb
->actual_length
+= n_bytes
- remain
;
1138 dev_vdbg(hsotg
->dev
, "length=%d actual=%d\n", urb
->length
,
1139 urb
->actual_length
);
1140 if (remain
|| urb
->actual_length
>= urb
->length
) {
1149 static int dwc2_process_non_isoc_desc(struct dwc2_hsotg
*hsotg
,
1150 struct dwc2_host_chan
*chan
,
1151 int chnum
, struct dwc2_qtd
*qtd
,
1153 enum dwc2_halt_status halt_status
,
1156 struct dwc2_qh
*qh
= chan
->qh
;
1157 struct dwc2_hcd_urb
*urb
= qtd
->urb
;
1158 struct dwc2_dma_desc
*dma_desc
;
1162 dev_vdbg(hsotg
->dev
, "%s()\n", __func__
);
1167 dma_sync_single_for_cpu(hsotg
->dev
,
1168 qh
->desc_list_dma
+ (desc_num
*
1169 sizeof(struct dwc2_dma_desc
)),
1170 sizeof(struct dwc2_dma_desc
),
1173 dma_desc
= &qh
->desc_list
[desc_num
];
1174 n_bytes
= qh
->n_bytes
[desc_num
];
1175 dev_vdbg(hsotg
->dev
,
1176 "qtd=%p dwc2_urb=%p desc_num=%d desc=%p n_bytes=%d\n",
1177 qtd
, urb
, desc_num
, dma_desc
, n_bytes
);
1178 failed
= dwc2_update_non_isoc_urb_state_ddma(hsotg
, chan
, qtd
, dma_desc
,
1179 halt_status
, n_bytes
,
1181 if (failed
|| (*xfer_done
&& urb
->status
!= -EINPROGRESS
)) {
1182 dwc2_host_complete(hsotg
, qtd
, urb
->status
);
1183 dwc2_hcd_qtd_unlink_and_free(hsotg
, qtd
, qh
);
1184 dev_vdbg(hsotg
->dev
, "failed=%1x xfer_done=%1x\n",
1185 failed
, *xfer_done
);
1189 if (qh
->ep_type
== USB_ENDPOINT_XFER_CONTROL
) {
1190 switch (qtd
->control_phase
) {
1191 case DWC2_CONTROL_SETUP
:
1192 if (urb
->length
> 0)
1193 qtd
->control_phase
= DWC2_CONTROL_DATA
;
1195 qtd
->control_phase
= DWC2_CONTROL_STATUS
;
1196 dev_vdbg(hsotg
->dev
,
1197 " Control setup transaction done\n");
1199 case DWC2_CONTROL_DATA
:
1201 qtd
->control_phase
= DWC2_CONTROL_STATUS
;
1202 dev_vdbg(hsotg
->dev
,
1203 " Control data transfer done\n");
1204 } else if (desc_num
+ 1 == qtd
->n_desc
) {
1206 * Last descriptor for Control data stage which
1207 * is not completed yet
1209 dwc2_hcd_save_data_toggle(hsotg
, chan
, chnum
,
1221 static void dwc2_complete_non_isoc_xfer_ddma(struct dwc2_hsotg
*hsotg
,
1222 struct dwc2_host_chan
*chan
,
1224 enum dwc2_halt_status halt_status
)
1226 struct list_head
*qtd_item
, *qtd_tmp
;
1227 struct dwc2_qh
*qh
= chan
->qh
;
1228 struct dwc2_qtd
*qtd
= NULL
;
1232 if (chan
->halt_status
== DWC2_HC_XFER_URB_DEQUEUE
) {
1233 list_for_each_entry(qtd
, &qh
->qtd_list
, qtd_list_entry
)
1234 qtd
->in_process
= 0;
1238 list_for_each_safe(qtd_item
, qtd_tmp
, &qh
->qtd_list
) {
1242 qtd
= list_entry(qtd_item
, struct dwc2_qtd
, qtd_list_entry
);
1244 qtd_desc_count
= qtd
->n_desc
;
1246 for (i
= 0; i
< qtd_desc_count
; i
++) {
1247 if (dwc2_process_non_isoc_desc(hsotg
, chan
, chnum
, qtd
,
1248 desc_num
, halt_status
,
1259 if (qh
->ep_type
!= USB_ENDPOINT_XFER_CONTROL
) {
1261 * Resetting the data toggle for bulk and interrupt endpoints
1262 * in case of stall. See handle_hc_stall_intr().
1264 if (halt_status
== DWC2_HC_XFER_STALL
)
1265 qh
->data_toggle
= DWC2_HC_PID_DATA0
;
1267 dwc2_hcd_save_data_toggle(hsotg
, chan
, chnum
, NULL
);
1270 if (halt_status
== DWC2_HC_XFER_COMPLETE
) {
1271 if (chan
->hcint
& HCINTMSK_NYET
) {
1273 * Got a NYET on the last transaction of the transfer.
1274 * It means that the endpoint should be in the PING
1275 * state at the beginning of the next transfer.
1283 * dwc2_hcd_complete_xfer_ddma() - Scans the descriptor list, updates URB's
1284 * status and calls completion routine for the URB if it's done. Called from
1285 * interrupt handlers.
1287 * @hsotg: The HCD state structure for the DWC OTG controller
1288 * @chan: Host channel the transfer is completed on
1289 * @chnum: Index of Host channel registers
1290 * @halt_status: Reason the channel is being halted or just XferComplete
1291 * for isochronous transfers
1293 * Releases the channel to be used by other transfers.
1294 * In case of Isochronous endpoint the channel is not halted until the end of
1295 * the session, i.e. QTD list is empty.
1296 * If periodic channel released the FrameList is updated accordingly.
1297 * Calls transaction selection routines to activate pending transfers.
1299 void dwc2_hcd_complete_xfer_ddma(struct dwc2_hsotg
*hsotg
,
1300 struct dwc2_host_chan
*chan
, int chnum
,
1301 enum dwc2_halt_status halt_status
)
1303 struct dwc2_qh
*qh
= chan
->qh
;
1304 int continue_isoc_xfer
= 0;
1305 enum dwc2_transaction_type tr_type
;
1307 if (chan
->ep_type
== USB_ENDPOINT_XFER_ISOC
) {
1308 dwc2_complete_isoc_xfer_ddma(hsotg
, chan
, halt_status
);
1310 /* Release the channel if halted or session completed */
1311 if (halt_status
!= DWC2_HC_XFER_COMPLETE
||
1312 list_empty(&qh
->qtd_list
)) {
1313 struct dwc2_qtd
*qtd
, *qtd_tmp
;
1316 * Kill all remainings QTDs since channel has been
1319 list_for_each_entry_safe(qtd
, qtd_tmp
,
1322 dwc2_host_complete(hsotg
, qtd
,
1324 dwc2_hcd_qtd_unlink_and_free(hsotg
,
1328 /* Halt the channel if session completed */
1329 if (halt_status
== DWC2_HC_XFER_COMPLETE
)
1330 dwc2_hc_halt(hsotg
, chan
, halt_status
);
1331 dwc2_release_channel_ddma(hsotg
, qh
);
1332 dwc2_hcd_qh_unlink(hsotg
, qh
);
1334 /* Keep in assigned schedule to continue transfer */
1335 list_move_tail(&qh
->qh_list_entry
,
1336 &hsotg
->periodic_sched_assigned
);
1338 * If channel has been halted during giveback of urb
1339 * then prevent any new scheduling.
1341 if (!chan
->halt_status
)
1342 continue_isoc_xfer
= 1;
1345 * Todo: Consider the case when period exceeds FrameList size.
1346 * Frame Rollover interrupt should be used.
1350 * Scan descriptor list to complete the URB(s), then release
1353 dwc2_complete_non_isoc_xfer_ddma(hsotg
, chan
, chnum
,
1355 dwc2_release_channel_ddma(hsotg
, qh
);
1356 dwc2_hcd_qh_unlink(hsotg
, qh
);
1358 if (!list_empty(&qh
->qtd_list
)) {
1360 * Add back to inactive non-periodic schedule on normal
1363 dwc2_hcd_qh_add(hsotg
, qh
);
1367 tr_type
= dwc2_hcd_select_transactions(hsotg
);
1368 if (tr_type
!= DWC2_TRANSACTION_NONE
|| continue_isoc_xfer
) {
1369 if (continue_isoc_xfer
) {
1370 if (tr_type
== DWC2_TRANSACTION_NONE
)
1371 tr_type
= DWC2_TRANSACTION_PERIODIC
;
1372 else if (tr_type
== DWC2_TRANSACTION_NON_PERIODIC
)
1373 tr_type
= DWC2_TRANSACTION_ALL
;
1375 dwc2_hcd_queue_transactions(hsotg
, tr_type
);