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_otg_hcd_qtd_remove_and_free(hcd, qtd, qh);
64 if (hcd->core_if->dma_desc_enable) {
65 dwc_otg_hcd_qh_free_ddma(hcd, qh);
66 } else if (qh->dw_align_buf) {
68 if (qh->ep_type == UE_ISOCHRONOUS) {
71 buf_size = hcd->core_if->core_params->max_transfer_size;
73 DWC_DEV_DMA_FREE(buf_size, qh->dw_align_buf,
74 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,
337 hcd->non_periodic_channels);
338 status = -DWC_E_NO_SPACE;
345 * Checks that there is sufficient bandwidth for the specified QH in the
346 * periodic schedule. For simplicity, this calculation assumes that all the
347 * transfers in the periodic schedule may occur in the same (micro)frame.
349 * @param hcd The HCD state structure for the DWC OTG controller.
350 * @param qh QH containing periodic bandwidth required.
352 * @return 0 if successful, negative error code otherwise.
354 static int check_periodic_bandwidth(dwc_otg_hcd_t *hcd, dwc_otg_qh_t *qh)
357 int16_t max_claimed_usecs;
361 if ((qh->dev_speed == DWC_OTG_EP_SPEED_HIGH) || qh->do_split) {
364 * Max periodic usecs is 80% x 125 usec = 100 usec.
367 max_claimed_usecs = 100 - qh->usecs;
371 * Max periodic usecs is 90% x 1000 usec = 900 usec.
373 max_claimed_usecs = 900 - qh->usecs;
376 if (hcd->periodic_usecs > max_claimed_usecs) {
377 DWC_INFO("%s: already claimed usecs %d, required usecs %d\n",
378 __func__, hcd->periodic_usecs, qh->usecs);
379 status = -DWC_E_NO_SPACE;
386 * Checks that the max transfer size allowed in a host channel is large enough
387 * to handle the maximum data transfer in a single (micro)frame for a periodic
390 * @param hcd The HCD state structure for the DWC OTG controller.
391 * @param qh QH for a periodic endpoint.
393 * @return 0 if successful, negative error code otherwise.
395 static int check_max_xfer_size(dwc_otg_hcd_t *hcd, dwc_otg_qh_t *qh)
398 uint32_t max_xfer_size;
399 uint32_t max_channel_xfer_size;
403 max_xfer_size = dwc_max_packet(qh->maxp) * dwc_hb_mult(qh->maxp);
404 max_channel_xfer_size = hcd->core_if->core_params->max_transfer_size;
406 if (max_xfer_size > max_channel_xfer_size) {
407 DWC_INFO("%s: Periodic xfer length %d > " "max xfer length for channel %d\n",
408 __func__, max_xfer_size, max_channel_xfer_size);
409 status = -DWC_E_NO_SPACE;
416 * Schedules an interrupt or isochronous transfer in the periodic schedule.
418 * @param hcd The HCD state structure for the DWC OTG controller.
419 * @param qh QH for the periodic transfer. The QH should already contain the
420 * scheduling information.
422 * @return 0 if successful, negative error code otherwise.
424 static int schedule_periodic(dwc_otg_hcd_t *hcd, dwc_otg_qh_t *qh)
428 status = periodic_channel_available(hcd);
430 DWC_INFO("%s: No host channel available for periodic " "transfer.\n", __func__);
434 status = check_periodic_bandwidth(hcd, qh);
436 DWC_INFO("%s: Insufficient periodic bandwidth for " "periodic transfer.\n", __func__);
440 status = check_max_xfer_size(hcd, qh);
442 DWC_INFO("%s: Channel max transfer size too small " "for periodic transfer.\n", __func__);
446 if (hcd->core_if->dma_desc_enable) {
447 /* Don't rely on SOF and start in ready schedule */
448 DWC_LIST_INSERT_TAIL(&hcd->periodic_sched_ready,
451 /* Always start in the inactive schedule. */
452 DWC_LIST_INSERT_TAIL(&hcd->periodic_sched_inactive,
456 /* Reserve the periodic channel. */
457 hcd->periodic_channels++;
459 /* Update claimed usecs per (micro)frame. */
460 hcd->periodic_usecs += qh->usecs;
466 * This function adds a QH to either the non periodic or periodic schedule if
467 * it is not already in the schedule. If the QH is already in the schedule, no
470 * @return 0 if successful, negative error code otherwise.
472 int dwc_otg_hcd_qh_add(dwc_otg_hcd_t *hcd, dwc_otg_qh_t *qh)
475 gintmsk_data_t intr_mask = {.d32 = 0 };
477 if (!DWC_LIST_EMPTY(&qh->qh_list_entry)) {
478 /* QH already in a schedule. */
482 /* Add the new QH to the appropriate schedule */
483 if (dwc_qh_is_non_per(qh)) {
484 /* Always start in the inactive schedule. */
485 DWC_LIST_INSERT_TAIL(&hcd->non_periodic_sched_inactive,
488 status = schedule_periodic(hcd, qh);
489 if (!hcd->periodic_qh_count) {
490 intr_mask.b.sofintr = 1;
491 DWC_MODIFY_REG32(&hcd->core_if->
492 core_global_regs->gintmsk,
493 intr_mask.d32, intr_mask.d32);
495 hcd->periodic_qh_count++;
502 * Removes an interrupt or isochronous transfer from the periodic schedule.
504 * @param hcd The HCD state structure for the DWC OTG controller.
505 * @param qh QH for the periodic transfer.
507 static void deschedule_periodic(dwc_otg_hcd_t *hcd, dwc_otg_qh_t *qh)
509 DWC_LIST_REMOVE_INIT(&qh->qh_list_entry);
511 /* Release the periodic channel reservation. */
512 hcd->periodic_channels--;
514 /* Update claimed usecs per (micro)frame. */
515 hcd->periodic_usecs -= qh->usecs;
519 * Removes a QH from either the non-periodic or periodic schedule. Memory is
522 * @param hcd The HCD state structure.
523 * @param qh QH to remove from schedule. */
524 void dwc_otg_hcd_qh_remove(dwc_otg_hcd_t *hcd, dwc_otg_qh_t *qh)
526 gintmsk_data_t intr_mask = {.d32 = 0 };
528 if (DWC_LIST_EMPTY(&qh->qh_list_entry)) {
529 /* QH is not in a schedule. */
533 if (dwc_qh_is_non_per(qh)) {
534 if (hcd->non_periodic_qh_ptr == &qh->qh_list_entry) {
535 hcd->non_periodic_qh_ptr =
536 hcd->non_periodic_qh_ptr->next;
538 DWC_LIST_REMOVE_INIT(&qh->qh_list_entry);
540 deschedule_periodic(hcd, qh);
541 hcd->periodic_qh_count--;
542 if (!hcd->periodic_qh_count) {
543 intr_mask.b.sofintr = 1;
544 DWC_MODIFY_REG32(&hcd->core_if->
545 core_global_regs->gintmsk,
552 * Deactivates a QH. For non-periodic QHs, removes the QH from the active
553 * non-periodic schedule. The QH is added to the inactive non-periodic
554 * schedule if any QTDs are still attached to the QH.
556 * For periodic QHs, the QH is removed from the periodic queued schedule. If
557 * there are any QTDs still attached to the QH, the QH is added to either the
558 * periodic inactive schedule or the periodic ready schedule and its next
559 * scheduled frame is calculated. The QH is placed in the ready schedule if
560 * the scheduled frame has been reached already. Otherwise it's placed in the
561 * inactive schedule. If there are no QTDs attached to the QH, the QH is
562 * completely removed from the periodic schedule.
564 void dwc_otg_hcd_qh_deactivate(dwc_otg_hcd_t *hcd, dwc_otg_qh_t *qh,
565 int sched_next_periodic_split)
567 if (dwc_qh_is_non_per(qh)) {
568 dwc_otg_hcd_qh_remove(hcd, qh);
569 if (!DWC_CIRCLEQ_EMPTY(&qh->qtd_list)) {
570 /* Add back to inactive non-periodic schedule. */
571 dwc_otg_hcd_qh_add(hcd, qh);
574 uint16_t frame_number = dwc_otg_hcd_get_frame_number(hcd);
577 /* Schedule the next continuing periodic split transfer */
578 if (sched_next_periodic_split) {
580 qh->sched_frame = frame_number;
581 if (dwc_frame_num_le(frame_number,
583 (qh->start_split_frame,
586 * Allow one frame to elapse after start
587 * split microframe before scheduling
588 * complete split, but DONT if we are
589 * doing the next start split in the
590 * same frame for an ISOC out.
592 if ((qh->ep_type != UE_ISOCHRONOUS) ||
593 (qh->ep_is_in != 0)) {
596 (qh->sched_frame, 1);
601 dwc_frame_num_inc(qh->start_split_frame,
604 (qh->sched_frame, frame_number)) {
605 qh->sched_frame = frame_number;
607 qh->sched_frame |= 0x7;
608 qh->start_split_frame = qh->sched_frame;
612 dwc_frame_num_inc(qh->sched_frame, qh->interval);
613 if (dwc_frame_num_le(qh->sched_frame, frame_number)) {
614 qh->sched_frame = frame_number;
618 if (DWC_CIRCLEQ_EMPTY(&qh->qtd_list)) {
619 dwc_otg_hcd_qh_remove(hcd, qh);
622 * Remove from periodic_sched_queued and move to
625 if (qh->sched_frame == frame_number) {
626 DWC_LIST_MOVE_HEAD(&hcd->periodic_sched_ready,
630 (&hcd->periodic_sched_inactive,
638 * This function allocates and initializes a QTD.
640 * @param urb The URB to create a QTD from. Each URB-QTD pair will end up
641 * pointing to each other so each pair should have a unique correlation.
642 * @param atomic_alloc Flag to do atomic alloc if needed
644 * @return Returns pointer to the newly allocated QTD, or NULL on error. */
645 dwc_otg_qtd_t *dwc_otg_hcd_qtd_create(dwc_otg_hcd_urb_t *urb, int atomic_alloc)
649 qtd = dwc_otg_hcd_qtd_alloc(atomic_alloc);
654 dwc_otg_hcd_qtd_init(qtd, urb);
659 * Initializes a QTD structure.
661 * @param qtd The QTD to initialize.
662 * @param urb The URB to use for initialization. */
663 void dwc_otg_hcd_qtd_init(dwc_otg_qtd_t *qtd, dwc_otg_hcd_urb_t *urb)
665 dwc_memset(qtd, 0, sizeof(dwc_otg_qtd_t));
667 if (dwc_otg_hcd_get_pipe_type(&urb->pipe_info) == UE_CONTROL) {
669 * The only time the QTD data toggle is used is on the data
670 * phase of control transfers. This phase always starts with
673 qtd->data_toggle = DWC_OTG_HC_PID_DATA1;
674 qtd->control_phase = DWC_OTG_CONTROL_SETUP;
678 qtd->complete_split = 0;
679 qtd->isoc_split_pos = DWC_HCSPLIT_XACTPOS_ALL;
680 qtd->isoc_split_offset = 0;
683 /* Store the qtd ptr in the urb to reference what QTD. */
689 * This function adds a QTD to the QTD-list of a QH. It will find the correct
690 * QH to place the QTD into. If it does not find a QH, then it will create a
691 * new QH. If the QH to which the QTD is added is not currently scheduled, it
692 * is placed into the proper schedule based on its EP type.
694 * @param[in] qtd The QTD to add
695 * @param[in] hcd The DWC HCD structure
696 * @param[out] qh out parameter to return queue head
697 * @param atomic_alloc Flag to do atomic alloc if needed
699 * @return 0 if successful, negative error code otherwise.
701 int dwc_otg_hcd_qtd_add(dwc_otg_qtd_t *qtd,
702 dwc_otg_hcd_t *hcd, dwc_otg_qh_t **qh,
707 dwc_otg_hcd_urb_t *urb = qtd->urb;
710 * Get the QH which holds the QTD-list to insert to. Create QH if it
714 *qh = dwc_otg_hcd_qh_create(hcd, urb, atomic_alloc);
721 retval = dwc_otg_hcd_qh_add(hcd, *qh);
723 DWC_CIRCLEQ_INSERT_TAIL(&((*qh)->qtd_list), qtd,
732 #endif /* DWC_DEVICE_ONLY */