1 /* ==========================================================================
2 * $File: //dwh/usb_iip/dev/software/otg/linux/drivers/dwc_otg_hcd_queue.c $
7 * Synopsys HS OTG Linux Software Driver and documentation (hereinafter,
8 * "Software") is an Unsupported proprietary work of Synopsys, Inc. unless
9 * otherwise expressly agreed to in writing between Synopsys and you.
11 * The Software IS NOT an item of Licensed Software or Licensed Product under
12 * any End User Software License Agreement or Agreement for Licensed Product
13 * with Synopsys or any supplement thereto. You are permitted to use and
14 * redistribute this Software in source and binary forms, with or without
15 * modification, provided that redistributions of source code must retain this
16 * notice. You may not view, use, disclose, copy or distribute this file or
17 * any information contained herein except pursuant to this license grant from
18 * Synopsys. If you do not agree with this notice, including the disclaimer
19 * below, then you are not authorized to use the Software.
21 * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS" BASIS
22 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE HEREBY DISCLAIMED. IN NO EVENT SHALL SYNOPSYS BE LIABLE FOR ANY DIRECT,
25 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
26 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
27 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
28 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
32 * ========================================================================== */
33 #ifndef DWC_DEVICE_ONLY
38 * This file contains the functions to manage Queue Heads and Queue
39 * Transfer Descriptors.
42 #include "dwc_otg_hcd.h"
43 #include "dwc_otg_regs.h"
46 * Free each QTD in the QH's QTD-list then free the QH. QH should already be
47 * removed from a list. QTD list should already be empty if called from URB
50 * @param hcd HCD instance.
51 * @param qh The QH to free.
53 void dwc_otg_hcd_qh_free(dwc_otg_hcd_t * hcd, dwc_otg_qh_t * qh)
55 dwc_otg_qtd_t *qtd, *qtd_tmp;
58 /* Free each QTD in the QTD list */
59 DWC_SPINLOCK_IRQSAVE(hcd->lock, &flags);
60 DWC_CIRCLEQ_FOREACH_SAFE(qtd, qtd_tmp, &qh->qtd_list, qtd_list_entry) {
61 DWC_CIRCLEQ_REMOVE(&qh->qtd_list, qtd, qtd_list_entry);
62 dwc_otg_hcd_qtd_free(qtd);
65 if (hcd->core_if->dma_desc_enable) {
66 dwc_otg_hcd_qh_free_ddma(hcd, qh);
67 } else if (qh->dw_align_buf) {
69 if (qh->ep_type == UE_ISOCHRONOUS) {
72 buf_size = hcd->core_if->core_params->max_transfer_size;
74 DWC_DMA_FREE(buf_size, qh->dw_align_buf, qh->dw_align_buf_dma);
78 DWC_SPINUNLOCK_IRQRESTORE(hcd->lock, flags);
82 #define BitStuffTime(bytecount) ((8 * 7* bytecount) / 6)
83 #define HS_HOST_DELAY 5 /* nanoseconds */
84 #define FS_LS_HOST_DELAY 1000 /* nanoseconds */
85 #define HUB_LS_SETUP 333 /* nanoseconds */
86 #define NS_TO_US(ns) ((ns + 500) / 1000)
87 /* convert & round nanoseconds to microseconds */
89 static uint32_t calc_bus_time(int speed, int is_in, int is_isoc, int bytecount)
98 (2083 * (3 + BitStuffTime(bytecount)))) / 1000 +
103 (2083 * (3 + BitStuffTime(bytecount)))) / 1000 +
110 (8354 * (31 + 10 * BitStuffTime(bytecount))) / 1000;
112 retval = 7268 + FS_LS_HOST_DELAY + retval;
114 retval = 6265 + FS_LS_HOST_DELAY + retval;
118 (8354 * (31 + 10 * BitStuffTime(bytecount))) / 1000;
119 retval = 9107 + FS_LS_HOST_DELAY + retval;
125 (67667 * (31 + 10 * BitStuffTime(bytecount))) /
128 64060 + (2 * HUB_LS_SETUP) + FS_LS_HOST_DELAY +
132 (66700 * (31 + 10 * BitStuffTime(bytecount))) /
135 64107 + (2 * HUB_LS_SETUP) + FS_LS_HOST_DELAY +
140 DWC_WARN("Unknown device speed\n");
144 return NS_TO_US(retval);
148 * Initializes a QH structure.
150 * @param hcd The HCD state structure for the DWC OTG controller.
151 * @param qh The QH to init.
152 * @param urb Holds the information about the device/endpoint that we need
153 * to initialize the QH.
155 #define SCHEDULE_SLOP 10
156 void qh_init(dwc_otg_hcd_t * hcd, dwc_otg_qh_t * qh, dwc_otg_hcd_urb_t * urb)
160 uint32_t hub_addr, hub_port;
162 dwc_memset(qh, 0, sizeof(dwc_otg_qh_t));
165 qh->ep_type = dwc_otg_hcd_get_pipe_type(&urb->pipe_info);
166 qh->ep_is_in = dwc_otg_hcd_is_pipe_in(&urb->pipe_info) ? 1 : 0;
168 qh->data_toggle = DWC_OTG_HC_PID_DATA0;
169 qh->maxp = dwc_otg_hcd_get_mps(&urb->pipe_info);
170 DWC_CIRCLEQ_INIT(&qh->qtd_list);
171 DWC_LIST_INIT(&qh->qh_list_entry);
174 /* FS/LS Enpoint on HS Hub
175 * NOT virtual root hub */
176 dev_speed = hcd->fops->speed(hcd, urb->priv);
178 hcd->fops->hub_info(hcd, urb->priv, &hub_addr, &hub_port);
181 if (((dev_speed == USB_SPEED_LOW) ||
182 (dev_speed == USB_SPEED_FULL)) &&
183 (hub_addr != 0 && hub_addr != 1)) {
185 "QH init: EP %d: TT found at hub addr %d, for port %d\n",
186 dwc_otg_hcd_get_ep_num(&urb->pipe_info), hub_addr,
191 if (qh->ep_type == UE_INTERRUPT || qh->ep_type == UE_ISOCHRONOUS) {
192 /* Compute scheduling parameters once and save them. */
195 /** @todo Account for split transfers in the bus time. */
197 dwc_hb_mult(qh->maxp) * dwc_max_packet(qh->maxp);
200 calc_bus_time((qh->do_split ? USB_SPEED_HIGH : dev_speed),
201 qh->ep_is_in, (qh->ep_type == UE_ISOCHRONOUS),
203 /* Start in a slightly future (micro)frame. */
204 qh->sched_frame = dwc_frame_num_inc(hcd->frame_number,
206 qh->interval = urb->interval;
209 /* Increase interrupt polling rate for debugging. */
210 if (qh->ep_type == UE_INTERRUPT) {
214 hprt.d32 = DWC_READ_REG32(hcd->core_if->host_if->hprt0);
215 if ((hprt.b.prtspd == DWC_HPRT0_PRTSPD_HIGH_SPEED) &&
216 ((dev_speed == USB_SPEED_LOW) ||
217 (dev_speed == USB_SPEED_FULL))) {
219 qh->sched_frame |= 0x7;
220 qh->start_split_frame = qh->sched_frame;
225 DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD QH Initialized\n");
226 DWC_DEBUGPL(DBG_HCDV, "DWC OTG HCD QH - qh = %p\n", qh);
227 DWC_DEBUGPL(DBG_HCDV, "DWC OTG HCD QH - Device Address = %d\n",
228 dwc_otg_hcd_get_dev_addr(&urb->pipe_info));
229 DWC_DEBUGPL(DBG_HCDV, "DWC OTG HCD QH - Endpoint %d, %s\n",
230 dwc_otg_hcd_get_ep_num(&urb->pipe_info),
231 dwc_otg_hcd_is_pipe_in(&urb->pipe_info) ? "IN" : "OUT");
234 qh->dev_speed = DWC_OTG_EP_SPEED_LOW;
238 qh->dev_speed = DWC_OTG_EP_SPEED_FULL;
242 qh->dev_speed = DWC_OTG_EP_SPEED_HIGH;
249 DWC_DEBUGPL(DBG_HCDV, "DWC OTG HCD QH - Speed = %s\n", speed);
251 switch (qh->ep_type) {
253 type = "isochronous";
269 DWC_DEBUGPL(DBG_HCDV, "DWC OTG HCD QH - Type = %s\n", type);
272 if (qh->ep_type == UE_INTERRUPT) {
273 DWC_DEBUGPL(DBG_HCDV, "DWC OTG HCD QH - usecs = %d\n",
275 DWC_DEBUGPL(DBG_HCDV, "DWC OTG HCD QH - interval = %d\n",
283 * This function allocates and initializes a QH.
285 * @param hcd The HCD state structure for the DWC OTG controller.
286 * @param urb Holds the information about the device/endpoint that we need
287 * to initialize the QH.
288 * @param atomic_alloc Flag to do atomic allocation if needed
290 * @return Returns pointer to the newly allocated QH, or NULL on error. */
291 dwc_otg_qh_t *dwc_otg_hcd_qh_create(dwc_otg_hcd_t * hcd,
292 dwc_otg_hcd_urb_t * urb, int atomic_alloc)
296 /* Allocate memory */
297 /** @todo add memflags argument */
298 qh = dwc_otg_hcd_qh_alloc(atomic_alloc);
300 DWC_ERROR("qh allocation failed");
304 qh_init(hcd, qh, urb);
306 if (hcd->core_if->dma_desc_enable
307 && (dwc_otg_hcd_qh_init_ddma(hcd, qh) < 0)) {
308 dwc_otg_hcd_qh_free(hcd, qh);
316 * Checks that a channel is available for a periodic transfer.
318 * @return 0 if successful, negative error code otherise.
320 static int periodic_channel_available(dwc_otg_hcd_t * hcd)
323 * Currently assuming that there is a dedicated host channnel for each
324 * periodic transaction plus at least one host channel for
325 * non-periodic transactions.
330 num_channels = hcd->core_if->core_params->host_channels;
331 if ((hcd->periodic_channels + hcd->non_periodic_channels < num_channels)
332 && (hcd->periodic_channels < num_channels - 1)) {
335 DWC_INFO("%s: Total channels: %d, Periodic: %d, Non-periodic: %d\n",
336 __func__, num_channels, hcd->periodic_channels, hcd->non_periodic_channels); //NOTICE
337 status = -DWC_E_NO_SPACE;
344 * Checks that there is sufficient bandwidth for the specified QH in the
345 * periodic schedule. For simplicity, this calculation assumes that all the
346 * transfers in the periodic schedule may occur in the same (micro)frame.
348 * @param hcd The HCD state structure for the DWC OTG controller.
349 * @param qh QH containing periodic bandwidth required.
351 * @return 0 if successful, negative error code otherwise.
353 static int check_periodic_bandwidth(dwc_otg_hcd_t * hcd, dwc_otg_qh_t * qh)
356 int16_t max_claimed_usecs;
360 if ((qh->dev_speed == DWC_OTG_EP_SPEED_HIGH) || qh->do_split) {
363 * Max periodic usecs is 80% x 125 usec = 100 usec.
366 max_claimed_usecs = 100 - qh->usecs;
370 * Max periodic usecs is 90% x 1000 usec = 900 usec.
372 max_claimed_usecs = 900 - qh->usecs;
375 if (hcd->periodic_usecs > max_claimed_usecs) {
376 DWC_INFO("%s: already claimed usecs %d, required usecs %d\n", __func__, hcd->periodic_usecs, qh->usecs); //NOTICE
377 status = -DWC_E_NO_SPACE;
384 * Checks that the max transfer size allowed in a host channel is large enough
385 * to handle the maximum data transfer in a single (micro)frame for a periodic
388 * @param hcd The HCD state structure for the DWC OTG controller.
389 * @param qh QH for a periodic endpoint.
391 * @return 0 if successful, negative error code otherwise.
393 static int check_max_xfer_size(dwc_otg_hcd_t * hcd, dwc_otg_qh_t * qh)
396 uint32_t max_xfer_size;
397 uint32_t max_channel_xfer_size;
401 max_xfer_size = dwc_max_packet(qh->maxp) * dwc_hb_mult(qh->maxp);
402 max_channel_xfer_size = hcd->core_if->core_params->max_transfer_size;
404 if (max_xfer_size > max_channel_xfer_size) {
405 DWC_INFO("%s: Periodic xfer length %d > " "max xfer length for channel %d\n",
406 __func__, max_xfer_size, max_channel_xfer_size); //NOTICE
407 status = -DWC_E_NO_SPACE;
414 * Schedules an interrupt or isochronous transfer in the periodic schedule.
416 * @param hcd The HCD state structure for the DWC OTG controller.
417 * @param qh QH for the periodic transfer. The QH should already contain the
418 * scheduling information.
420 * @return 0 if successful, negative error code otherwise.
422 static int schedule_periodic(dwc_otg_hcd_t * hcd, dwc_otg_qh_t * qh)
426 status = periodic_channel_available(hcd);
428 DWC_INFO("%s: No host channel available for periodic " "transfer.\n", __func__); //NOTICE
432 status = check_periodic_bandwidth(hcd, qh);
434 DWC_INFO("%s: Insufficient periodic bandwidth for " "periodic transfer.\n", __func__); //NOTICE
438 status = check_max_xfer_size(hcd, qh);
440 DWC_INFO("%s: Channel max transfer size too small " "for periodic transfer.\n", __func__); //NOTICE
444 if (hcd->core_if->dma_desc_enable) {
445 /* Don't rely on SOF and start in ready schedule */
446 DWC_LIST_INSERT_TAIL(&hcd->periodic_sched_ready, &qh->qh_list_entry);
449 /* Always start in the inactive schedule. */
450 DWC_LIST_INSERT_TAIL(&hcd->periodic_sched_inactive, &qh->qh_list_entry);
453 /* Reserve the periodic channel. */
454 hcd->periodic_channels++;
456 /* Update claimed usecs per (micro)frame. */
457 hcd->periodic_usecs += qh->usecs;
463 * This function adds a QH to either the non periodic or periodic schedule if
464 * it is not already in the schedule. If the QH is already in the schedule, no
467 * @return 0 if successful, negative error code otherwise.
469 int dwc_otg_hcd_qh_add(dwc_otg_hcd_t * hcd, dwc_otg_qh_t * qh)
472 gintmsk_data_t intr_mask = {.d32 = 0 };
474 if (!DWC_LIST_EMPTY(&qh->qh_list_entry)) {
475 /* QH already in a schedule. */
479 /* Add the new QH to the appropriate schedule */
480 if (dwc_qh_is_non_per(qh)) {
481 /* Always start in the inactive schedule. */
482 DWC_LIST_INSERT_TAIL(&hcd->non_periodic_sched_inactive,
485 status = schedule_periodic(hcd, qh);
486 if ( !hcd->periodic_qh_count ) {
487 intr_mask.b.sofintr = 1;
488 DWC_MODIFY_REG32(&hcd->core_if->core_global_regs->gintmsk,
489 intr_mask.d32, intr_mask.d32);
491 hcd->periodic_qh_count++;
498 * Removes an interrupt or isochronous transfer from the periodic schedule.
500 * @param hcd The HCD state structure for the DWC OTG controller.
501 * @param qh QH for the periodic transfer.
503 static void deschedule_periodic(dwc_otg_hcd_t * hcd, dwc_otg_qh_t * qh)
505 DWC_LIST_REMOVE_INIT(&qh->qh_list_entry);
507 /* Release the periodic channel reservation. */
508 hcd->periodic_channels--;
510 /* Update claimed usecs per (micro)frame. */
511 hcd->periodic_usecs -= qh->usecs;
515 * Removes a QH from either the non-periodic or periodic schedule. Memory is
518 * @param hcd The HCD state structure.
519 * @param qh QH to remove from schedule. */
520 void dwc_otg_hcd_qh_remove(dwc_otg_hcd_t * hcd, dwc_otg_qh_t * qh)
522 gintmsk_data_t intr_mask = {.d32 = 0 };
524 if (DWC_LIST_EMPTY(&qh->qh_list_entry)) {
525 /* QH is not in a schedule. */
529 if (dwc_qh_is_non_per(qh)) {
530 if (hcd->non_periodic_qh_ptr == &qh->qh_list_entry) {
531 hcd->non_periodic_qh_ptr =
532 hcd->non_periodic_qh_ptr->next;
534 DWC_LIST_REMOVE_INIT(&qh->qh_list_entry);
536 deschedule_periodic(hcd, qh);
537 hcd->periodic_qh_count--;
538 if( !hcd->periodic_qh_count ) {
539 intr_mask.b.sofintr = 1;
540 DWC_MODIFY_REG32(&hcd->core_if->core_global_regs->gintmsk,
547 * Deactivates a QH. For non-periodic QHs, removes the QH from the active
548 * non-periodic schedule. The QH is added to the inactive non-periodic
549 * schedule if any QTDs are still attached to the QH.
551 * For periodic QHs, the QH is removed from the periodic queued schedule. If
552 * there are any QTDs still attached to the QH, the QH is added to either the
553 * periodic inactive schedule or the periodic ready schedule and its next
554 * scheduled frame is calculated. The QH is placed in the ready schedule if
555 * the scheduled frame has been reached already. Otherwise it's placed in the
556 * inactive schedule. If there are no QTDs attached to the QH, the QH is
557 * completely removed from the periodic schedule.
559 void dwc_otg_hcd_qh_deactivate(dwc_otg_hcd_t * hcd, dwc_otg_qh_t * qh,
560 int sched_next_periodic_split)
562 if (dwc_qh_is_non_per(qh)) {
563 dwc_otg_hcd_qh_remove(hcd, qh);
564 if (!DWC_CIRCLEQ_EMPTY(&qh->qtd_list)) {
565 /* Add back to inactive non-periodic schedule. */
566 dwc_otg_hcd_qh_add(hcd, qh);
569 uint16_t frame_number = dwc_otg_hcd_get_frame_number(hcd);
572 /* Schedule the next continuing periodic split transfer */
573 if (sched_next_periodic_split) {
575 qh->sched_frame = frame_number;
576 if (dwc_frame_num_le(frame_number,
578 (qh->start_split_frame,
581 * Allow one frame to elapse after start
582 * split microframe before scheduling
583 * complete split, but DONT if we are
584 * doing the next start split in the
585 * same frame for an ISOC out.
587 if ((qh->ep_type != UE_ISOCHRONOUS) ||
588 (qh->ep_is_in != 0)) {
590 dwc_frame_num_inc(qh->sched_frame, 1);
595 dwc_frame_num_inc(qh->start_split_frame,
598 (qh->sched_frame, frame_number)) {
599 qh->sched_frame = frame_number;
601 qh->sched_frame |= 0x7;
602 qh->start_split_frame = qh->sched_frame;
606 dwc_frame_num_inc(qh->sched_frame, qh->interval);
607 if (dwc_frame_num_le(qh->sched_frame, frame_number)) {
608 qh->sched_frame = frame_number;
612 if (DWC_CIRCLEQ_EMPTY(&qh->qtd_list)) {
613 dwc_otg_hcd_qh_remove(hcd, qh);
616 * Remove from periodic_sched_queued and move to
619 if (qh->sched_frame == frame_number) {
620 DWC_LIST_MOVE_HEAD(&hcd->periodic_sched_ready,
624 (&hcd->periodic_sched_inactive,
632 * This function allocates and initializes a QTD.
634 * @param urb The URB to create a QTD from. Each URB-QTD pair will end up
635 * pointing to each other so each pair should have a unique correlation.
636 * @param atomic_alloc Flag to do atomic alloc if needed
638 * @return Returns pointer to the newly allocated QTD, or NULL on error. */
639 dwc_otg_qtd_t *dwc_otg_hcd_qtd_create(dwc_otg_hcd_urb_t * urb, int atomic_alloc)
643 qtd = dwc_otg_hcd_qtd_alloc(atomic_alloc);
648 dwc_otg_hcd_qtd_init(qtd, urb);
653 * Initializes a QTD structure.
655 * @param qtd The QTD to initialize.
656 * @param urb The URB to use for initialization. */
657 void dwc_otg_hcd_qtd_init(dwc_otg_qtd_t * qtd, dwc_otg_hcd_urb_t * urb)
659 dwc_memset(qtd, 0, sizeof(dwc_otg_qtd_t));
661 if (dwc_otg_hcd_get_pipe_type(&urb->pipe_info) == UE_CONTROL) {
663 * The only time the QTD data toggle is used is on the data
664 * phase of control transfers. This phase always starts with
667 qtd->data_toggle = DWC_OTG_HC_PID_DATA1;
668 qtd->control_phase = DWC_OTG_CONTROL_SETUP;
672 qtd->complete_split = 0;
673 qtd->isoc_split_pos = DWC_HCSPLIT_XACTPOS_ALL;
674 qtd->isoc_split_offset = 0;
677 /* Store the qtd ptr in the urb to reference what QTD. */
683 * This function adds a QTD to the QTD-list of a QH. It will find the correct
684 * QH to place the QTD into. If it does not find a QH, then it will create a
685 * new QH. If the QH to which the QTD is added is not currently scheduled, it
686 * is placed into the proper schedule based on its EP type.
688 * @param[in] qtd The QTD to add
689 * @param[in] hcd The DWC HCD structure
690 * @param[out] qh out parameter to return queue head
691 * @param atomic_alloc Flag to do atomic alloc if needed
693 * @return 0 if successful, negative error code otherwise.
695 int dwc_otg_hcd_qtd_add(dwc_otg_qtd_t * qtd,
696 dwc_otg_hcd_t * hcd, dwc_otg_qh_t ** qh, int atomic_alloc)
699 dwc_irqflags_t flags;
701 dwc_otg_hcd_urb_t *urb = qtd->urb;
704 * Get the QH which holds the QTD-list to insert to. Create QH if it
708 *qh = dwc_otg_hcd_qh_create(hcd, urb, atomic_alloc);
714 DWC_SPINLOCK_IRQSAVE(hcd->lock, &flags);
715 retval = dwc_otg_hcd_qh_add(hcd, *qh);
717 DWC_CIRCLEQ_INSERT_TAIL(&((*qh)->qtd_list), qtd,
720 DWC_SPINUNLOCK_IRQRESTORE(hcd->lock, flags);
727 #endif /* DWC_DEVICE_ONLY */