1 /* ==========================================================================
2 * $File: //dwh/usb_iip/dev/software/otg/linux/drivers/dwc_otg_hcd.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
36 * This file implements HCD Core. All code in this file is portable and doesn't
37 * use any OS specific functions.
38 * Interface provided by HCD Core is defined in <code><hcd_if.h></code>
42 #include "dwc_otg_hcd.h"
43 #include "dwc_otg_regs.h"
44 #include "usbdev_rk.h"
45 #include "dwc_otg_driver.h"
46 #include <linux/usb.h>
47 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 35)
48 #include <../drivers/usb/core/hcd.h>
50 #include <linux/usb/hcd.h>
53 dwc_otg_hcd_t *dwc_otg_hcd_alloc_hcd(void)
55 return DWC_ALLOC(sizeof(dwc_otg_hcd_t));
59 * Connection timeout function. An OTG host is required to display a
60 * message if the device does not connect within 10 seconds.
62 void dwc_otg_hcd_connect_timeout(void *ptr)
65 DWC_DEBUGPL(DBG_HCDV, "%s(%p)\n", __func__, ptr);
66 DWC_PRINTF("Connect Timeout\n");
67 __DWC_ERROR("Device Not Connected/Responding\n");
68 /** Remove buspower after 10s */
70 if (hcd->core_if->otg_ver)
71 dwc_otg_set_prtpower(hcd->core_if, 0);
75 static void dump_channel_info(dwc_otg_hcd_t *hcd, dwc_otg_qh_t *qh)
77 if (qh->channel != NULL) {
78 dwc_hc_t *hc = qh->channel;
79 dwc_list_link_t *item;
80 dwc_otg_qh_t *qh_item;
81 int num_channels = hcd->core_if->core_params->host_channels;
84 dwc_otg_hc_regs_t *hc_regs;
90 hc_regs = hcd->core_if->host_if->hc_regs[hc->hc_num];
91 hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
92 hcsplt.d32 = DWC_READ_REG32(&hc_regs->hcsplt);
93 hctsiz.d32 = DWC_READ_REG32(&hc_regs->hctsiz);
94 hcdma = DWC_READ_REG32(&hc_regs->hcdma);
96 DWC_PRINTF(" Assigned to channel %p:\n", hc);
97 DWC_PRINTF(" hcchar 0x%08x, hcsplt 0x%08x\n", hcchar.d32,
99 DWC_PRINTF(" hctsiz 0x%08x, hcdma 0x%08x\n", hctsiz.d32,
101 DWC_PRINTF(" dev_addr: %d, ep_num: %d, ep_is_in: %d\n",
102 hc->dev_addr, hc->ep_num, hc->ep_is_in);
103 DWC_PRINTF(" ep_type: %d\n", hc->ep_type);
104 DWC_PRINTF(" max_packet: %d\n", hc->max_packet);
105 DWC_PRINTF(" data_pid_start: %d\n", hc->data_pid_start);
106 DWC_PRINTF(" xfer_started: %d\n", hc->xfer_started);
107 DWC_PRINTF(" halt_status: %d\n", hc->halt_status);
108 DWC_PRINTF(" xfer_buff: %p\n", hc->xfer_buff);
109 DWC_PRINTF(" xfer_len: %d\n", hc->xfer_len);
110 DWC_PRINTF(" qh: %p\n", hc->qh);
111 DWC_PRINTF(" NP inactive sched:\n");
112 DWC_LIST_FOREACH(item, &hcd->non_periodic_sched_inactive) {
114 DWC_LIST_ENTRY(item, dwc_otg_qh_t, qh_list_entry);
115 DWC_PRINTF(" %p\n", qh_item);
117 DWC_PRINTF(" NP active sched:\n");
118 DWC_LIST_FOREACH(item, &hcd->non_periodic_sched_active) {
120 DWC_LIST_ENTRY(item, dwc_otg_qh_t, qh_list_entry);
121 DWC_PRINTF(" %p\n", qh_item);
123 DWC_PRINTF(" Channels: \n");
124 for (i = 0; i < num_channels; i++) {
125 dwc_hc_t *hc = hcd->hc_ptr_array[i];
126 DWC_PRINTF(" %2d: %p\n", i, hc);
133 * Work queue function for starting the HCD when A-Cable is connected.
134 * The hcd_start() must be called in a process context.
136 static void hcd_start_func(void *_vp)
138 dwc_otg_hcd_t *hcd = (dwc_otg_hcd_t *) _vp;
140 DWC_DEBUGPL(DBG_HCDV, "%s() %p\n", __func__, hcd);
142 hcd->fops->start(hcd);
146 static void del_xfer_timers(dwc_otg_hcd_t *hcd)
150 int num_channels = hcd->core_if->core_params->host_channels;
151 for (i = 0; i < num_channels; i++) {
152 DWC_TIMER_CANCEL(hcd->core_if->hc_xfer_timer[i]);
157 static void del_timers(dwc_otg_hcd_t *hcd)
159 del_xfer_timers(hcd);
160 DWC_TIMER_CANCEL(hcd->conn_timer);
164 * Processes all the URBs in a single list of QHs. Completes them with
165 * -ETIMEDOUT and frees the QTD.
167 static void kill_urbs_in_qh_list(dwc_otg_hcd_t *hcd, dwc_list_link_t *qh_list)
169 dwc_list_link_t *qh_item;
171 dwc_otg_qtd_t *qtd, *qtd_tmp;
173 DWC_LIST_FOREACH(qh_item, qh_list) {
174 qh = DWC_LIST_ENTRY(qh_item, dwc_otg_qh_t, qh_list_entry);
175 DWC_CIRCLEQ_FOREACH_SAFE(qtd, qtd_tmp,
176 &qh->qtd_list, qtd_list_entry) {
177 qtd = DWC_CIRCLEQ_FIRST(&qh->qtd_list);
178 if (qtd->urb != NULL) {
179 hcd->fops->complete(hcd, qtd->urb->priv,
180 qtd->urb, -DWC_E_SHUTDOWN);
181 dwc_otg_hcd_qtd_remove_and_free(hcd, qtd, qh);
189 * Responds with an error status of ETIMEDOUT to all URBs in the non-periodic
190 * and periodic schedules. The QTD associated with each URB is removed from
191 * the schedule and freed. This function may be called when a disconnect is
192 * detected or when the HCD is being stopped.
194 static void kill_all_urbs(dwc_otg_hcd_t *hcd)
196 kill_urbs_in_qh_list(hcd, &hcd->non_periodic_sched_inactive);
197 kill_urbs_in_qh_list(hcd, &hcd->non_periodic_sched_active);
198 kill_urbs_in_qh_list(hcd, &hcd->periodic_sched_inactive);
199 kill_urbs_in_qh_list(hcd, &hcd->periodic_sched_ready);
200 kill_urbs_in_qh_list(hcd, &hcd->periodic_sched_assigned);
201 kill_urbs_in_qh_list(hcd, &hcd->periodic_sched_queued);
205 * Start the connection timer. An OTG host is required to display a
206 * message if the device does not connect within 10 seconds. The
207 * timer is deleted if a port connect interrupt occurs before the
210 static void dwc_otg_hcd_start_connect_timer(dwc_otg_hcd_t *hcd)
212 DWC_TIMER_SCHEDULE(hcd->conn_timer, 10000 /* 10 secs */);
216 * HCD Callback function for disconnect of the HCD.
218 * @param p void pointer to the <code>struct usb_hcd</code>
220 static int32_t dwc_otg_hcd_session_start_cb(void *p)
222 dwc_otg_hcd_t *dwc_otg_hcd;
223 DWC_DEBUGPL(DBG_HCDV, "%s(%p)\n", __func__, p);
225 dwc_otg_hcd_start_connect_timer(dwc_otg_hcd);
230 * HCD Callback function for starting the HCD when A-Cable is
233 * @param p void pointer to the <code>struct usb_hcd</code>
235 static int32_t dwc_otg_hcd_start_cb(void *p)
237 dwc_otg_hcd_t *dwc_otg_hcd = p;
238 dwc_otg_core_if_t *core_if;
240 uint32_t timeout = 50;
242 core_if = dwc_otg_hcd->core_if;
244 if (core_if->op_state == B_HOST) {
246 * Reset the port. During a HNP mode switch the reset
247 * needs to occur within 1ms and have a duration of at
250 hprt0.d32 = dwc_otg_read_hprt0(core_if);
252 DWC_WRITE_REG32(core_if->host_if->hprt0, hprt0.d32);
253 if (core_if->otg_ver) {
255 hprt0.d32 = dwc_otg_read_hprt0(core_if);
257 DWC_WRITE_REG32(core_if->host_if->hprt0, hprt0.d32);
260 /**@todo vahrama: Check the timeout value for OTG 2.0 */
261 if (core_if->otg_ver)
263 DWC_WORKQ_SCHEDULE_DELAYED(core_if->wq_otg,
264 hcd_start_func, dwc_otg_hcd, timeout,
271 * HCD Callback function for disconnect of the HCD.
273 * @param p void pointer to the <code>struct usb_hcd</code>
275 static int32_t dwc_otg_hcd_disconnect_cb(void *p)
278 dwc_otg_hcd_t *dwc_otg_hcd = p;
281 * Set status flags for the hub driver.
283 dwc_otg_hcd->flags.b.port_connect_status_change = 1;
284 dwc_otg_hcd->flags.b.port_connect_status = 0;
287 * Shutdown any transfers in process by clearing the Tx FIFO Empty
288 * interrupt mask and status bits and disabling subsequent host
289 * channel interrupts.
292 intr.b.nptxfempty = 1;
293 intr.b.ptxfempty = 1;
295 DWC_MODIFY_REG32(&dwc_otg_hcd->core_if->core_global_regs->gintmsk,
297 DWC_MODIFY_REG32(&dwc_otg_hcd->core_if->core_global_regs->gintsts,
301 * Turn off the vbus power only if the core has transitioned to device
302 * mode. If still in host mode, need to keep power on to detect a
305 if (dwc_otg_is_device_mode(dwc_otg_hcd->core_if)) {
306 if (dwc_otg_hcd->core_if->op_state != A_SUSPEND) {
307 hprt0_data_t hprt0 = {.d32 = 0 };
308 DWC_PRINTF("Disconnect: PortPower off\n");
310 DWC_WRITE_REG32(dwc_otg_hcd->core_if->host_if->hprt0,
313 /** Delete timers if become device */
314 del_timers(dwc_otg_hcd);
315 dwc_otg_disable_host_interrupts(dwc_otg_hcd->core_if);
318 /* Respond with an error status to all URBs in the schedule. */
319 kill_all_urbs(dwc_otg_hcd);
321 if (dwc_otg_is_host_mode(dwc_otg_hcd->core_if)) {
322 /* Clean up any host channels that were in use. */
326 dwc_otg_hc_regs_t *hc_regs;
327 hcchar_data_t hcchar;
329 DWC_PRINTF("Disconnect cb-Host\n");
330 if (dwc_otg_hcd->core_if->otg_ver == 1)
331 del_xfer_timers(dwc_otg_hcd);
333 del_timers(dwc_otg_hcd);
335 num_channels = dwc_otg_hcd->core_if->core_params->host_channels;
337 if (!dwc_otg_hcd->core_if->dma_enable) {
338 /* Flush out any channel requests in slave mode. */
339 for (i = 0; i < num_channels; i++) {
340 channel = dwc_otg_hcd->hc_ptr_array[i];
341 if (DWC_CIRCLEQ_EMPTY_ENTRY
342 (channel, hc_list_entry)) {
344 dwc_otg_hcd->core_if->host_if->
347 DWC_READ_REG32(&hc_regs->hcchar);
360 for (i = 0; i < num_channels; i++) {
361 channel = dwc_otg_hcd->hc_ptr_array[i];
362 if (DWC_CIRCLEQ_EMPTY_ENTRY(channel, hc_list_entry)) {
364 dwc_otg_hcd->core_if->host_if->hc_regs[i];
365 hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
367 /* Halt the channel. */
369 DWC_WRITE_REG32(&hc_regs->hcchar,
373 dwc_otg_hc_cleanup(dwc_otg_hcd->core_if,
375 DWC_CIRCLEQ_INSERT_TAIL
376 (&dwc_otg_hcd->free_hc_list, channel,
379 * Added for Descriptor DMA to prevent channel double cleanup
380 * in release_channel_ddma(). Which called from ep_disable
381 * when device disconnect.
388 if (dwc_otg_hcd->fops->disconnect) {
389 dwc_otg_hcd->fops->disconnect(dwc_otg_hcd);
396 * HCD Callback function for stopping the HCD.
398 * @param p void pointer to the <code>struct usb_hcd</code>
400 static int32_t dwc_otg_hcd_stop_cb(void *p)
402 dwc_otg_hcd_t *dwc_otg_hcd = p;
404 DWC_DEBUGPL(DBG_HCDV, "%s(%p)\n", __func__, p);
405 dwc_otg_hcd_stop(dwc_otg_hcd);
409 #ifdef CONFIG_USB_DWC_OTG_LPM
411 * HCD Callback function for sleep of HCD.
413 * @param p void pointer to the <code>struct usb_hcd</code>
415 static int dwc_otg_hcd_sleep_cb(void *p)
417 dwc_otg_hcd_t *hcd = p;
419 dwc_otg_hcd_free_hc_from_lpm(hcd);
426 * HCD Callback function for Remote Wakeup.
428 * @param p void pointer to the <code>struct usb_hcd</code>
430 extern inline struct usb_hcd *dwc_otg_hcd_to_hcd(dwc_otg_hcd_t *dwc_otg_hcd);
431 static int dwc_otg_hcd_rem_wakeup_cb(void *p)
433 dwc_otg_hcd_t *dwc_otg_hcd = p;
434 struct usb_hcd *hcd = dwc_otg_hcd_to_hcd(dwc_otg_hcd);
436 if (dwc_otg_hcd->core_if->lx_state == DWC_OTG_L2) {
437 dwc_otg_hcd->flags.b.port_suspend_change = 1;
438 usb_hcd_resume_root_hub(hcd);
440 #ifdef CONFIG_USB_DWC_OTG_LPM
442 dwc_otg_hcd->flags.b.port_l1_change = 1;
449 * Halts the DWC_otg host mode operations in a clean manner. USB transfers are
452 void dwc_otg_hcd_stop(dwc_otg_hcd_t *hcd)
454 hprt0_data_t hprt0 = {.d32 = 0 };
455 struct dwc_otg_platform_data *pldata;
456 pldata = hcd->core_if->otg_dev->pldata;
458 DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD STOP\n");
461 * The root hub should be disconnected before this function is called.
462 * The disconnect will clear the QTD lists (via ..._hcd_urb_dequeue)
463 * and the QH lists (via ..._hcd_endpoint_disable).
466 /* Turn off all host-specific interrupts. */
467 dwc_otg_disable_host_interrupts(hcd->core_if);
469 /* Turn off the vbus power */
470 DWC_PRINTF("PortPower off\n");
472 DWC_WRITE_REG32(hcd->core_if->host_if->hprt0, hprt0.d32);
474 if (pldata->power_enable)
475 pldata->power_enable(0);
480 int dwc_otg_hcd_urb_enqueue(dwc_otg_hcd_t *hcd,
481 dwc_otg_hcd_urb_t *dwc_otg_urb, void **ep_handle,
484 dwc_irqflags_t flags;
487 gintmsk_data_t intr_mask = {.d32 = 0 };
489 if (!hcd->flags.b.port_connect_status) {
490 /* No longer connected. */
491 DWC_DEBUG("Not connected\n");
492 return -DWC_E_NO_DEVICE;
495 qtd = dwc_otg_hcd_qtd_create(dwc_otg_urb, atomic_alloc);
497 DWC_ERROR("DWC OTG HCD URB Enqueue failed creating QTD\n");
498 return -DWC_E_NO_MEMORY;
502 dwc_otg_hcd_qtd_add(qtd, hcd, (dwc_otg_qh_t **) ep_handle,
505 DWC_SPINLOCK_IRQSAVE(hcd->lock, &flags);
507 DWC_ERROR("DWC OTG HCD URB Enqueue failed adding QTD. "
508 "Error status %d\n", retval);
509 dwc_otg_hcd_qtd_free(qtd);
512 DWC_READ_REG32(&hcd->core_if->core_global_regs->gintmsk);
513 if (!intr_mask.b.sofintr && retval == 0) {
514 dwc_otg_transaction_type_e tr_type;
515 if ((qtd->qh->ep_type == UE_BULK)
516 && !(qtd->urb->flags & URB_GIVEBACK_ASAP)) {
517 /* Do not schedule SG transactions until qtd has URB_GIVEBACK_ASAP set */
521 tr_type = dwc_otg_hcd_select_transactions(hcd);
522 if (tr_type != DWC_OTG_TRANSACTION_NONE) {
523 dwc_otg_hcd_queue_transactions(hcd, tr_type);
527 DWC_SPINUNLOCK_IRQRESTORE(hcd->lock, flags);
531 int dwc_otg_hcd_urb_dequeue(dwc_otg_hcd_t *hcd,
532 dwc_otg_hcd_urb_t *dwc_otg_urb)
535 dwc_otg_qtd_t *urb_qtd;
537 urb_qtd = dwc_otg_urb->qtd;
538 if (((uint32_t) urb_qtd & 0xf0000000) == 0) {
539 DWC_PRINTF("%s error: urb_qtd is %p dwc_otg_urb %p!!!\n",
540 __func__, urb_qtd, dwc_otg_urb);
545 if (CHK_DEBUG_LEVEL(DBG_HCDV | DBG_HCD_URB)) {
546 if (urb_qtd->in_process) {
547 dump_channel_info(hcd, qh);
551 if (urb_qtd->in_process && qh->channel) {
552 /* The QTD is in process (it has been assigned to a channel). */
553 if (hcd->flags.b.port_connect_status) {
555 * If still connected (i.e. in host mode), halt the
556 * channel so it can be used for other transfers. If
557 * no longer connected, the host registers can't be
558 * written to halt the channel since the core is in
561 dwc_otg_hc_halt(hcd->core_if, qh->channel,
562 DWC_OTG_HC_XFER_URB_DEQUEUE);
567 * Free the QTD and clean up the associated QH. Leave the QH in the
568 * schedule if it has any remaining QTDs.
571 if (!hcd->core_if->dma_desc_enable) {
572 uint8_t b = urb_qtd->in_process;
573 dwc_otg_hcd_qtd_remove_and_free(hcd, urb_qtd, qh);
575 dwc_otg_hcd_qh_deactivate(hcd, qh, 0);
577 } else if (DWC_CIRCLEQ_EMPTY(&qh->qtd_list)) {
578 dwc_otg_hcd_qh_remove(hcd, qh);
581 dwc_otg_hcd_qtd_remove_and_free(hcd, urb_qtd, qh);
586 int dwc_otg_hcd_endpoint_disable(dwc_otg_hcd_t *hcd, void *ep_handle,
589 dwc_otg_qh_t *qh = (dwc_otg_qh_t *) ep_handle;
591 dwc_irqflags_t flags;
594 retval = -DWC_E_INVALID;
599 retval = -DWC_E_INVALID;
603 DWC_SPINLOCK_IRQSAVE(hcd->lock, &flags);
605 while (!DWC_CIRCLEQ_EMPTY(&qh->qtd_list) && retry) {
606 DWC_SPINUNLOCK_IRQRESTORE(hcd->lock, flags);
609 DWC_SPINLOCK_IRQSAVE(hcd->lock, &flags);
612 dwc_otg_hcd_qh_remove(hcd, qh);
614 DWC_SPINUNLOCK_IRQRESTORE(hcd->lock, flags);
616 * Split dwc_otg_hcd_qh_remove_and_free() into qh_remove
617 * and qh_free to prevent stack dump on DWC_DMA_FREE() with
618 * irq_disabled (spinlock_irqsave) in dwc_otg_hcd_desc_list_free()
619 * and dwc_otg_hcd_frame_list_alloc().
621 dwc_otg_hcd_qh_free(hcd, qh);
627 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 30)
628 int dwc_otg_hcd_endpoint_reset(dwc_otg_hcd_t *hcd, void *ep_handle)
631 dwc_otg_qh_t *qh = (dwc_otg_qh_t *) ep_handle;
633 return -DWC_E_INVALID;
635 qh->data_toggle = DWC_OTG_HC_PID_DATA0;
641 * HCD Callback structure for handling mode switching.
643 static dwc_otg_cil_callbacks_t hcd_cil_callbacks = {
644 .start = dwc_otg_hcd_start_cb,
645 .stop = dwc_otg_hcd_stop_cb,
646 .disconnect = dwc_otg_hcd_disconnect_cb,
647 .session_start = dwc_otg_hcd_session_start_cb,
648 .resume_wakeup = dwc_otg_hcd_rem_wakeup_cb,
649 #ifdef CONFIG_USB_DWC_OTG_LPM
650 .sleep = dwc_otg_hcd_sleep_cb,
656 * Reset tasklet function
658 static void reset_tasklet_func(void *data)
660 dwc_otg_hcd_t *dwc_otg_hcd = (dwc_otg_hcd_t *) data;
661 dwc_otg_core_if_t *core_if = dwc_otg_hcd->core_if;
664 DWC_DEBUGPL(DBG_HCDV, "USB RESET tasklet called\n");
666 hprt0.d32 = dwc_otg_read_hprt0(core_if);
668 DWC_WRITE_REG32(core_if->host_if->hprt0, hprt0.d32);
672 DWC_WRITE_REG32(core_if->host_if->hprt0, hprt0.d32);
673 dwc_otg_hcd->flags.b.port_reset_change = 1;
676 static void qh_list_free(dwc_otg_hcd_t *hcd, dwc_list_link_t *qh_list)
678 dwc_list_link_t *item;
680 dwc_irqflags_t flags;
682 if (!qh_list->next) {
683 /* The list hasn't been initialized yet. */
687 * Hold spinlock here. Not needed in that case if bellow
688 * function is being called from ISR
690 DWC_SPINLOCK_IRQSAVE(hcd->lock, &flags);
691 /* Ensure there are no QTDs or URBs left. */
692 kill_urbs_in_qh_list(hcd, qh_list);
693 DWC_SPINUNLOCK_IRQRESTORE(hcd->lock, flags);
695 DWC_LIST_FOREACH(item, qh_list) {
696 qh = DWC_LIST_ENTRY(item, dwc_otg_qh_t, qh_list_entry);
697 dwc_otg_hcd_qh_remove_and_free(hcd, qh);
702 * Exit from Hibernation if Host did not detect SRP from connected SRP capable
703 * Device during SRP time by host power up.
705 void dwc_otg_hcd_power_up(void *ptr)
707 gpwrdn_data_t gpwrdn = {.d32 = 0 };
708 dwc_otg_core_if_t *core_if = (dwc_otg_core_if_t *) ptr;
710 DWC_PRINTF("%s called\n", __FUNCTION__);
712 if (!core_if->hibernation_suspend) {
713 DWC_PRINTF("Already exited from Hibernation\n");
717 /* Switch on the voltage to the core */
718 gpwrdn.b.pwrdnswtch = 1;
719 DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
724 gpwrdn.b.pwrdnrstn = 1;
725 DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
728 /* Disable power clamps */
730 gpwrdn.b.pwrdnclmp = 1;
731 DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
733 /* Remove reset the core signal */
735 gpwrdn.b.pwrdnrstn = 1;
736 DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, 0, gpwrdn.d32);
739 /* Disable PMU interrupt */
741 gpwrdn.b.pmuintsel = 1;
742 DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
744 core_if->hibernation_suspend = 0;
748 gpwrdn.b.pmuactv = 1;
749 DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
754 gpwrdn.b.dis_vbus = 1;
755 DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
757 core_if->op_state = A_HOST;
758 dwc_otg_core_init(core_if);
759 dwc_otg_enable_global_interrupts(core_if);
760 cil_hcd_start(core_if);
764 * Frees secondary storage associated with the dwc_otg_hcd structure contained
765 * in the struct usb_hcd field.
767 static void dwc_otg_hcd_free(dwc_otg_hcd_t *dwc_otg_hcd)
771 DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD FREE\n");
773 del_timers(dwc_otg_hcd);
775 /* Free memory for QH/QTD lists */
776 qh_list_free(dwc_otg_hcd, &dwc_otg_hcd->non_periodic_sched_inactive);
777 qh_list_free(dwc_otg_hcd, &dwc_otg_hcd->non_periodic_sched_active);
778 qh_list_free(dwc_otg_hcd, &dwc_otg_hcd->periodic_sched_inactive);
779 qh_list_free(dwc_otg_hcd, &dwc_otg_hcd->periodic_sched_ready);
780 qh_list_free(dwc_otg_hcd, &dwc_otg_hcd->periodic_sched_assigned);
781 qh_list_free(dwc_otg_hcd, &dwc_otg_hcd->periodic_sched_queued);
783 /* Free memory for the host channels. */
784 for (i = 0; i < MAX_EPS_CHANNELS; i++) {
785 dwc_hc_t *hc = dwc_otg_hcd->hc_ptr_array[i];
788 if (dwc_otg_hcd->core_if->hc_xfer_timer[i]) {
789 DWC_TIMER_FREE(dwc_otg_hcd->core_if->hc_xfer_timer[i]);
793 DWC_DEBUGPL(DBG_HCDV, "HCD Free channel #%i, hc=%p\n",
799 if (dwc_otg_hcd->core_if->dma_enable) {
800 if (dwc_otg_hcd->status_buf_dma) {
801 DWC_DMA_FREE(DWC_OTG_HCD_STATUS_BUF_SIZE,
802 dwc_otg_hcd->status_buf,
803 dwc_otg_hcd->status_buf_dma);
805 } else if (dwc_otg_hcd->status_buf != NULL) {
806 DWC_FREE(dwc_otg_hcd->status_buf);
808 DWC_SPINLOCK_FREE(dwc_otg_hcd->lock);
809 /* Set core_if's lock pointer to NULL */
810 dwc_otg_hcd->core_if->lock = NULL;
812 DWC_TIMER_FREE(dwc_otg_hcd->conn_timer);
813 DWC_TASK_FREE(dwc_otg_hcd->reset_tasklet);
815 #ifdef DWC_DEV_SRPCAP
816 if (dwc_otg_hcd->core_if->power_down == 2 &&
817 dwc_otg_hcd->core_if->pwron_timer) {
818 DWC_TIMER_FREE(dwc_otg_hcd->core_if->pwron_timer);
821 DWC_FREE(dwc_otg_hcd);
824 int dwc_otg_hcd_init(dwc_otg_hcd_t *hcd, dwc_otg_core_if_t *core_if)
831 hcd->lock = DWC_SPINLOCK_ALLOC();
833 DWC_ERROR("Could not allocate lock for pcd");
835 retval = -DWC_E_NO_MEMORY;
838 hcd->core_if = core_if;
840 /* Register the HCD CIL Callbacks */
841 dwc_otg_cil_register_hcd_callbacks(hcd->core_if,
842 &hcd_cil_callbacks, hcd);
844 /* Initialize the non-periodic schedule. */
845 DWC_LIST_INIT(&hcd->non_periodic_sched_inactive);
846 DWC_LIST_INIT(&hcd->non_periodic_sched_active);
848 /* Initialize the periodic schedule. */
849 DWC_LIST_INIT(&hcd->periodic_sched_inactive);
850 DWC_LIST_INIT(&hcd->periodic_sched_ready);
851 DWC_LIST_INIT(&hcd->periodic_sched_assigned);
852 DWC_LIST_INIT(&hcd->periodic_sched_queued);
855 * Create a host channel descriptor for each host channel implemented
856 * in the controller. Initialize the channel descriptor array.
858 DWC_CIRCLEQ_INIT(&hcd->free_hc_list);
859 num_channels = hcd->core_if->core_params->host_channels;
860 DWC_MEMSET(hcd->hc_ptr_array, 0, sizeof(hcd->hc_ptr_array));
861 for (i = 0; i < num_channels; i++) {
862 channel = DWC_ALLOC(sizeof(dwc_hc_t));
863 if (channel == NULL) {
864 retval = -DWC_E_NO_MEMORY;
865 DWC_ERROR("%s: host channel allocation failed\n",
867 dwc_otg_hcd_free(hcd);
871 hcd->hc_ptr_array[i] = channel;
873 hcd->core_if->hc_xfer_timer[i] =
874 DWC_TIMER_ALLOC("hc timer", hc_xfer_timeout,
875 &hcd->core_if->hc_xfer_info[i]);
877 DWC_DEBUGPL(DBG_HCDV, "HCD Added channel #%d, hc=%p\n", i,
881 /* Initialize the Connection timeout timer. */
882 hcd->conn_timer = DWC_TIMER_ALLOC("Connection timer",
883 dwc_otg_hcd_connect_timeout, hcd);
885 /* Initialize reset tasklet. */
887 DWC_TASK_ALLOC("reset_tasklet", reset_tasklet_func, hcd);
888 #ifdef DWC_DEV_SRPCAP
889 if (hcd->core_if->power_down == 2) {
890 /* Initialize Power on timer for Host power up in case hibernation */
891 hcd->core_if->pwron_timer = DWC_TIMER_ALLOC("PWRON TIMER",
892 dwc_otg_hcd_power_up,
898 * Allocate space for storing data on status transactions. Normally no
899 * data is sent, but this space acts as a bit bucket. This must be
900 * done after usb_add_hcd since that function allocates the DMA buffer
903 if (hcd->core_if->dma_enable) {
905 DWC_DMA_ALLOC_ATOMIC(DWC_OTG_HCD_STATUS_BUF_SIZE,
906 &hcd->status_buf_dma);
908 hcd->status_buf = DWC_ALLOC(DWC_OTG_HCD_STATUS_BUF_SIZE);
910 if (!hcd->status_buf) {
911 retval = -DWC_E_NO_MEMORY;
912 DWC_ERROR("%s: status_buf allocation failed\n", __func__);
913 dwc_otg_hcd_free(hcd);
918 hcd->frame_list = NULL;
919 hcd->frame_list_dma = 0;
920 hcd->periodic_qh_count = 0;
925 void dwc_otg_hcd_remove(dwc_otg_hcd_t *hcd)
927 /* Turn off all host-specific interrupts. */
928 dwc_otg_disable_host_interrupts(hcd->core_if);
930 dwc_otg_hcd_free(hcd);
934 * Initializes dynamic portions of the DWC_otg HCD state.
936 static void dwc_otg_hcd_reinit(dwc_otg_hcd_t *hcd)
941 dwc_hc_t *channel_tmp;
945 hcd->non_periodic_qh_ptr = &hcd->non_periodic_sched_active;
946 hcd->non_periodic_channels = 0;
947 hcd->periodic_channels = 0;
950 * Put all channels in the free channel list and clean up channel
953 DWC_CIRCLEQ_FOREACH_SAFE(channel, channel_tmp,
954 &hcd->free_hc_list, hc_list_entry) {
955 DWC_CIRCLEQ_REMOVE(&hcd->free_hc_list, channel, hc_list_entry);
958 num_channels = hcd->core_if->core_params->host_channels;
959 for (i = 0; i < num_channels; i++) {
960 channel = hcd->hc_ptr_array[i];
961 DWC_CIRCLEQ_INSERT_TAIL(&hcd->free_hc_list, channel,
963 dwc_otg_hc_cleanup(hcd->core_if, channel);
966 /* Initialize the DWC core for host mode operation. */
967 dwc_otg_core_host_init(hcd->core_if);
969 /* Set core_if's lock pointer to the hcd->lock */
970 hcd->core_if->lock = hcd->lock;
974 * Assigns transactions from a QTD to a free host channel and initializes the
975 * host channel to perform the transactions. The host channel is removed from
978 * @param hcd The HCD state structure.
979 * @param qh Transactions from the first QTD for this QH are selected and
980 * assigned to a free host channel.
982 static int assign_and_init_hc(dwc_otg_hcd_t *hcd, dwc_otg_qh_t *qh)
986 dwc_otg_hcd_urb_t *urb;
990 DWC_DEBUGPL(DBG_HCDV, "%s(%p,%p)\n", __func__, hcd, qh);
992 hc = DWC_CIRCLEQ_FIRST(&hcd->free_hc_list);
994 /* Remove the host channel from the free list. */
995 DWC_CIRCLEQ_REMOVE_INIT(&hcd->free_hc_list, hc, hc_list_entry);
997 qtd = DWC_CIRCLEQ_FIRST(&qh->qtd_list);
1001 printk("%s : urb is NULL\n", __func__);
1008 qtd->in_process = 1;
1011 * Use usb_pipedevice to determine device address. This address is
1012 * 0 before the SET_ADDRESS command and the correct address afterward.
1014 hc->dev_addr = dwc_otg_hcd_get_dev_addr(&urb->pipe_info);
1015 hc->ep_num = dwc_otg_hcd_get_ep_num(&urb->pipe_info);
1016 hc->speed = qh->dev_speed;
1017 hc->max_packet = dwc_max_packet(qh->maxp);
1019 hc->xfer_started = 0;
1020 hc->halt_status = DWC_OTG_HC_XFER_NO_HALT_STATUS;
1021 hc->error_state = (qtd->error_count > 0);
1022 hc->halt_on_queue = 0;
1023 hc->halt_pending = 0;
1027 * The following values may be modified in the transfer type section
1028 * below. The xfer_len value may be reduced when the transfer is
1029 * started to accommodate the max widths of the XferSize and PktCnt
1030 * fields in the HCTSIZn register.
1033 hc->ep_is_in = (dwc_otg_hcd_is_pipe_in(&urb->pipe_info) != 0);
1037 hc->do_ping = qh->ping_state;
1039 hc->data_pid_start = qh->data_toggle;
1040 hc->multi_count = 1;
1042 if (hcd->core_if->dma_enable) {
1043 hc->xfer_buff = (uint8_t *) urb->dma + urb->actual_length;
1045 /* For non-dword aligned case */
1046 if (((unsigned long)hc->xfer_buff & 0x3)
1047 && !hcd->core_if->dma_desc_enable) {
1048 ptr = (uint8_t *) urb->buf + urb->actual_length;
1051 hc->xfer_buff = (uint8_t *) urb->buf + urb->actual_length;
1053 hc->xfer_len = urb->length - urb->actual_length;
1057 * Set the split attributes
1061 uint32_t hub_addr, port_addr;
1063 hc->xact_pos = qtd->isoc_split_pos;
1064 hc->complete_split = qtd->complete_split;
1065 hcd->fops->hub_info(hcd, urb->priv, &hub_addr, &port_addr);
1066 hc->hub_addr = (uint8_t) hub_addr;
1067 hc->port_addr = (uint8_t) port_addr;
1070 switch (dwc_otg_hcd_get_pipe_type(&urb->pipe_info)) {
1072 hc->ep_type = DWC_OTG_EP_TYPE_CONTROL;
1073 switch (qtd->control_phase) {
1074 case DWC_OTG_CONTROL_SETUP:
1075 DWC_DEBUGPL(DBG_HCDV, " Control setup transaction\n");
1078 hc->data_pid_start = DWC_OTG_HC_PID_SETUP;
1079 if (hcd->core_if->dma_enable)
1080 hc->xfer_buff = (uint8_t *) urb->setup_dma;
1082 hc->xfer_buff = (uint8_t *) urb->setup_packet;
1087 case DWC_OTG_CONTROL_DATA:
1088 DWC_DEBUGPL(DBG_HCDV, " Control data transaction\n");
1089 hc->data_pid_start = qtd->data_toggle;
1091 case DWC_OTG_CONTROL_STATUS:
1093 * Direction is opposite of data direction or IN if no
1096 DWC_DEBUGPL(DBG_HCDV, " Control status transaction\n");
1097 if (urb->length == 0) {
1101 dwc_otg_hcd_is_pipe_out(&urb->pipe_info);
1106 hc->data_pid_start = DWC_OTG_HC_PID_DATA1;
1109 if (hcd->core_if->dma_enable)
1110 hc->xfer_buff = (uint8_t *) hcd->status_buf_dma;
1112 hc->xfer_buff = (uint8_t *) hcd->status_buf;
1119 hc->ep_type = DWC_OTG_EP_TYPE_BULK;
1122 hc->ep_type = DWC_OTG_EP_TYPE_INTR;
1124 case UE_ISOCHRONOUS:
1126 struct dwc_otg_hcd_iso_packet_desc *frame_desc;
1128 hc->ep_type = DWC_OTG_EP_TYPE_ISOC;
1130 if (hcd->core_if->dma_desc_enable)
1133 frame_desc = &urb->iso_descs[qtd->isoc_frame_index];
1135 frame_desc->status = 0;
1137 if (hcd->core_if->dma_enable) {
1138 hc->xfer_buff = (uint8_t *) urb->dma;
1140 hc->xfer_buff = (uint8_t *) urb->buf;
1143 frame_desc->offset + qtd->isoc_split_offset;
1145 frame_desc->length - qtd->isoc_split_offset;
1147 /* For non-dword aligned buffers */
1148 if (((unsigned long)hc->xfer_buff & 0x3)
1149 && hcd->core_if->dma_enable) {
1151 (uint8_t *) urb->buf + frame_desc->offset +
1152 qtd->isoc_split_offset;
1156 if (hc->xact_pos == DWC_HCSPLIT_XACTPOS_ALL) {
1157 if (hc->xfer_len <= 188) {
1158 hc->xact_pos = DWC_HCSPLIT_XACTPOS_ALL;
1161 DWC_HCSPLIT_XACTPOS_BEGIN;
1167 /* non DWORD-aligned buffer case */
1170 if (hc->ep_type != DWC_OTG_EP_TYPE_ISOC) {
1171 buf_size = hcd->core_if->core_params->max_transfer_size;
1175 if (!qh->dw_align_buf) {
1176 qh->dw_align_buf = DWC_DMA_ALLOC_ATOMIC(buf_size,
1179 if (!qh->dw_align_buf) {
1181 ("%s: Failed to allocate memory to handle "
1182 "non-dword aligned buffer case\n",
1187 if (!hc->ep_is_in) {
1188 dwc_memcpy(qh->dw_align_buf, ptr, hc->xfer_len);
1190 hc->align_buff = qh->dw_align_buf_dma;
1195 if (hc->ep_type == DWC_OTG_EP_TYPE_INTR ||
1196 hc->ep_type == DWC_OTG_EP_TYPE_ISOC) {
1198 * This value may be modified when the transfer is started to
1199 * reflect the actual transfer length.
1201 hc->multi_count = dwc_hb_mult(qh->maxp);
1204 if (hcd->core_if->dma_desc_enable)
1205 hc->desc_list_addr = qh->desc_list_dma;
1207 dwc_otg_hc_init(hcd->core_if, hc);
1213 * This function selects transactions from the HCD transfer schedule and
1214 * assigns them to available host channels. It is called from HCD interrupt
1215 * handler functions.
1217 * @param hcd The HCD state structure.
1219 * @return The types of new transactions that were assigned to host channels.
1221 dwc_otg_transaction_type_e dwc_otg_hcd_select_transactions(dwc_otg_hcd_t *hcd)
1223 dwc_list_link_t *qh_ptr;
1226 dwc_otg_transaction_type_e ret_val = DWC_OTG_TRANSACTION_NONE;
1230 DWC_DEBUGPL(DBG_HCD, " Select Transactions\n");
1233 /* Process entries in the periodic ready list. */
1234 qh_ptr = DWC_LIST_FIRST(&hcd->periodic_sched_ready);
1236 while (qh_ptr != &hcd->periodic_sched_ready &&
1237 !DWC_CIRCLEQ_EMPTY(&hcd->free_hc_list)) {
1239 qh = DWC_LIST_ENTRY(qh_ptr, dwc_otg_qh_t, qh_list_entry);
1240 assign_and_init_hc(hcd, qh);
1243 * Move the QH from the periodic ready schedule to the
1244 * periodic assigned schedule.
1246 qh_ptr = DWC_LIST_NEXT(qh_ptr);
1247 DWC_LIST_MOVE_HEAD(&hcd->periodic_sched_assigned,
1248 &qh->qh_list_entry);
1250 ret_val = DWC_OTG_TRANSACTION_PERIODIC;
1254 * Process entries in the inactive portion of the non-periodic
1255 * schedule. Some free host channels may not be used if they are
1256 * reserved for periodic transfers.
1258 qh_ptr = hcd->non_periodic_sched_inactive.next;
1259 num_channels = hcd->core_if->core_params->host_channels;
1260 while (qh_ptr != &hcd->non_periodic_sched_inactive &&
1261 (hcd->non_periodic_channels <
1262 num_channels - hcd->periodic_channels) &&
1263 !DWC_CIRCLEQ_EMPTY(&hcd->free_hc_list)) {
1265 qh = DWC_LIST_ENTRY(qh_ptr, dwc_otg_qh_t, qh_list_entry);
1267 err = assign_and_init_hc(hcd, qh);
1270 * Move the QH from the non-periodic inactive schedule to the
1271 * non-periodic active schedule.
1273 qh_ptr = DWC_LIST_NEXT(qh_ptr);
1276 DWC_LIST_MOVE_HEAD(&hcd->non_periodic_sched_active,
1277 &qh->qh_list_entry);
1279 if (ret_val == DWC_OTG_TRANSACTION_NONE) {
1280 ret_val = DWC_OTG_TRANSACTION_NON_PERIODIC;
1282 ret_val = DWC_OTG_TRANSACTION_ALL;
1285 hcd->non_periodic_channels++;
1292 * Attempts to queue a single transaction request for a host channel
1293 * associated with either a periodic or non-periodic transfer. This function
1294 * assumes that there is space available in the appropriate request queue. For
1295 * an OUT transfer or SETUP transaction in Slave mode, it checks whether space
1296 * is available in the appropriate Tx FIFO.
1298 * @param hcd The HCD state structure.
1299 * @param hc Host channel descriptor associated with either a periodic or
1300 * non-periodic transfer.
1301 * @param fifo_dwords_avail Number of DWORDs available in the periodic Tx
1302 * FIFO for periodic transfers or the non-periodic Tx FIFO for non-periodic
1305 * @return 1 if a request is queued and more requests may be needed to
1306 * complete the transfer, 0 if no more requests are required for this
1307 * transfer, -1 if there is insufficient space in the Tx FIFO.
1309 static int queue_transaction(dwc_otg_hcd_t *hcd,
1310 dwc_hc_t *hc, uint16_t fifo_dwords_avail)
1314 if (hcd->core_if->dma_enable) {
1315 if (hcd->core_if->dma_desc_enable) {
1316 if (!hc->xfer_started
1317 || (hc->ep_type == DWC_OTG_EP_TYPE_ISOC)) {
1318 dwc_otg_hcd_start_xfer_ddma(hcd, hc->qh);
1319 hc->qh->ping_state = 0;
1321 } else if (!hc->xfer_started) {
1322 dwc_otg_hc_start_transfer(hcd->core_if, hc);
1323 hc->qh->ping_state = 0;
1326 } else if (hc->halt_pending) {
1327 /* Don't queue a request if the channel has been halted. */
1329 } else if (hc->halt_on_queue) {
1330 dwc_otg_hc_halt(hcd->core_if, hc, hc->halt_status);
1332 } else if (hc->do_ping) {
1333 if (!hc->xfer_started) {
1334 dwc_otg_hc_start_transfer(hcd->core_if, hc);
1337 } else if (!hc->ep_is_in || hc->data_pid_start == DWC_OTG_HC_PID_SETUP) {
1338 if ((fifo_dwords_avail * 4) >= hc->max_packet) {
1339 if (!hc->xfer_started) {
1340 dwc_otg_hc_start_transfer(hcd->core_if, hc);
1344 dwc_otg_hc_continue_transfer(hcd->core_if,
1351 if (!hc->xfer_started) {
1352 dwc_otg_hc_start_transfer(hcd->core_if, hc);
1355 retval = dwc_otg_hc_continue_transfer(hcd->core_if, hc);
1363 * Processes periodic channels for the next frame and queues transactions for
1364 * these channels to the DWC_otg controller. After queueing transactions, the
1365 * Periodic Tx FIFO Empty interrupt is enabled if there are more transactions
1366 * to queue as Periodic Tx FIFO or request queue space becomes available.
1367 * Otherwise, the Periodic Tx FIFO Empty interrupt is disabled.
1369 static void process_periodic_channels(dwc_otg_hcd_t *hcd)
1371 hptxsts_data_t tx_status;
1372 dwc_list_link_t *qh_ptr;
1375 int no_queue_space = 0;
1376 int no_fifo_space = 0;
1378 dwc_otg_host_global_regs_t *host_regs;
1379 host_regs = hcd->core_if->host_if->host_global_regs;
1381 DWC_DEBUGPL(DBG_HCDV, "Queue periodic transactions\n");
1383 tx_status.d32 = DWC_READ_REG32(&host_regs->hptxsts);
1384 DWC_DEBUGPL(DBG_HCDV,
1385 " P Tx Req Queue Space Avail (before queue): %d\n",
1386 tx_status.b.ptxqspcavail);
1387 DWC_DEBUGPL(DBG_HCDV, " P Tx FIFO Space Avail (before queue): %d\n",
1388 tx_status.b.ptxfspcavail);
1391 qh_ptr = hcd->periodic_sched_assigned.next;
1392 while (qh_ptr != &hcd->periodic_sched_assigned) {
1393 tx_status.d32 = DWC_READ_REG32(&host_regs->hptxsts);
1394 if (tx_status.b.ptxqspcavail == 0) {
1399 qh = DWC_LIST_ENTRY(qh_ptr, dwc_otg_qh_t, qh_list_entry);
1402 * Set a flag if we're queuing high-bandwidth in slave mode.
1403 * The flag prevents any halts to get into the request queue in
1404 * the middle of multiple high-bandwidth packets getting queued.
1406 if (!hcd->core_if->dma_enable && qh->channel->multi_count > 1) {
1407 hcd->core_if->queuing_high_bandwidth = 1;
1410 queue_transaction(hcd, qh->channel,
1411 tx_status.b.ptxfspcavail);
1418 * In Slave mode, stay on the current transfer until there is
1419 * nothing more to do or the high-bandwidth request count is
1420 * reached. In DMA mode, only need to queue one request. The
1421 * controller automatically handles multiple packets for
1422 * high-bandwidth transfers.
1424 if (hcd->core_if->dma_enable || status == 0 ||
1425 qh->channel->requests == qh->channel->multi_count) {
1426 qh_ptr = qh_ptr->next;
1428 * Move the QH from the periodic assigned schedule to
1429 * the periodic queued schedule.
1431 DWC_LIST_MOVE_HEAD(&hcd->periodic_sched_queued,
1432 &qh->qh_list_entry);
1434 /* done queuing high bandwidth */
1435 hcd->core_if->queuing_high_bandwidth = 0;
1439 if (!hcd->core_if->dma_enable) {
1440 dwc_otg_core_global_regs_t *global_regs;
1441 gintmsk_data_t intr_mask = {.d32 = 0 };
1443 global_regs = hcd->core_if->core_global_regs;
1444 intr_mask.b.ptxfempty = 1;
1446 tx_status.d32 = DWC_READ_REG32(&host_regs->hptxsts);
1447 DWC_DEBUGPL(DBG_HCDV,
1448 " P Tx Req Queue Space Avail (after queue): %d\n",
1449 tx_status.b.ptxqspcavail);
1450 DWC_DEBUGPL(DBG_HCDV,
1451 " P Tx FIFO Space Avail (after queue): %d\n",
1452 tx_status.b.ptxfspcavail);
1454 if (!DWC_LIST_EMPTY(&hcd->periodic_sched_assigned) ||
1455 no_queue_space || no_fifo_space) {
1457 * May need to queue more transactions as the request
1458 * queue or Tx FIFO empties. Enable the periodic Tx
1459 * FIFO empty interrupt. (Always use the half-empty
1460 * level to ensure that new requests are loaded as
1461 * soon as possible.)
1463 DWC_MODIFY_REG32(&global_regs->gintmsk, 0,
1467 * Disable the Tx FIFO empty interrupt since there are
1468 * no more transactions that need to be queued right
1469 * now. This function is called from interrupt
1470 * handlers to queue more transactions as transfer
1473 DWC_MODIFY_REG32(&global_regs->gintmsk, intr_mask.d32,
1480 * Processes active non-periodic channels and queues transactions for these
1481 * channels to the DWC_otg controller. After queueing transactions, the NP Tx
1482 * FIFO Empty interrupt is enabled if there are more transactions to queue as
1483 * NP Tx FIFO or request queue space becomes available. Otherwise, the NP Tx
1484 * FIFO Empty interrupt is disabled.
1486 static void process_non_periodic_channels(dwc_otg_hcd_t *hcd)
1488 gnptxsts_data_t tx_status;
1489 dwc_list_link_t *orig_qh_ptr;
1492 int no_queue_space = 0;
1493 int no_fifo_space = 0;
1496 dwc_otg_core_global_regs_t *global_regs =
1497 hcd->core_if->core_global_regs;
1499 DWC_DEBUGPL(DBG_HCDV, "Queue non-periodic transactions\n");
1501 tx_status.d32 = DWC_READ_REG32(&global_regs->gnptxsts);
1502 DWC_DEBUGPL(DBG_HCDV,
1503 " NP Tx Req Queue Space Avail (before queue): %d\n",
1504 tx_status.b.nptxqspcavail);
1505 DWC_DEBUGPL(DBG_HCDV, " NP Tx FIFO Space Avail (before queue): %d\n",
1506 tx_status.b.nptxfspcavail);
1509 * Keep track of the starting point. Skip over the start-of-list
1512 if (hcd->non_periodic_qh_ptr == &hcd->non_periodic_sched_active) {
1513 hcd->non_periodic_qh_ptr = hcd->non_periodic_qh_ptr->next;
1515 orig_qh_ptr = hcd->non_periodic_qh_ptr;
1518 * Process once through the active list or until no more space is
1519 * available in the request queue or the Tx FIFO.
1522 tx_status.d32 = DWC_READ_REG32(&global_regs->gnptxsts);
1523 if (!hcd->core_if->dma_enable && tx_status.b.nptxqspcavail == 0) {
1528 qh = DWC_LIST_ENTRY(hcd->non_periodic_qh_ptr, dwc_otg_qh_t,
1531 queue_transaction(hcd, qh->channel,
1532 tx_status.b.nptxfspcavail);
1536 } else if (status < 0) {
1541 /* Advance to next QH, skipping start-of-list entry. */
1542 hcd->non_periodic_qh_ptr = hcd->non_periodic_qh_ptr->next;
1543 if (hcd->non_periodic_qh_ptr == &hcd->non_periodic_sched_active) {
1544 hcd->non_periodic_qh_ptr =
1545 hcd->non_periodic_qh_ptr->next;
1548 } while (hcd->non_periodic_qh_ptr != orig_qh_ptr);
1550 if (!hcd->core_if->dma_enable) {
1551 gintmsk_data_t intr_mask = {.d32 = 0 };
1552 intr_mask.b.nptxfempty = 1;
1555 tx_status.d32 = DWC_READ_REG32(&global_regs->gnptxsts);
1556 DWC_DEBUGPL(DBG_HCDV,
1557 " NP Tx Req Queue Space Avail (after queue): %d\n",
1558 tx_status.b.nptxqspcavail);
1559 DWC_DEBUGPL(DBG_HCDV,
1560 " NP Tx FIFO Space Avail (after queue): %d\n",
1561 tx_status.b.nptxfspcavail);
1563 if (more_to_do || no_queue_space || no_fifo_space) {
1565 * May need to queue more transactions as the request
1566 * queue or Tx FIFO empties. Enable the non-periodic
1567 * Tx FIFO empty interrupt. (Always use the half-empty
1568 * level to ensure that new requests are loaded as
1569 * soon as possible.)
1571 DWC_MODIFY_REG32(&global_regs->gintmsk, 0,
1575 * Disable the Tx FIFO empty interrupt since there are
1576 * no more transactions that need to be queued right
1577 * now. This function is called from interrupt
1578 * handlers to queue more transactions as transfer
1581 DWC_MODIFY_REG32(&global_regs->gintmsk, intr_mask.d32,
1588 * This function processes the currently active host channels and queues
1589 * transactions for these channels to the DWC_otg controller. It is called
1590 * from HCD interrupt handler functions.
1592 * @param hcd The HCD state structure.
1593 * @param tr_type The type(s) of transactions to queue (non-periodic,
1594 * periodic, or both).
1596 void dwc_otg_hcd_queue_transactions(dwc_otg_hcd_t *hcd,
1597 dwc_otg_transaction_type_e tr_type)
1600 DWC_DEBUGPL(DBG_HCD, "Queue Transactions\n");
1602 /* Process host channels associated with periodic transfers. */
1603 if ((tr_type == DWC_OTG_TRANSACTION_PERIODIC ||
1604 tr_type == DWC_OTG_TRANSACTION_ALL) &&
1605 !DWC_LIST_EMPTY(&hcd->periodic_sched_assigned)) {
1607 process_periodic_channels(hcd);
1610 /* Process host channels associated with non-periodic transfers. */
1611 if (tr_type == DWC_OTG_TRANSACTION_NON_PERIODIC ||
1612 tr_type == DWC_OTG_TRANSACTION_ALL) {
1613 if (!DWC_LIST_EMPTY(&hcd->non_periodic_sched_active)) {
1614 process_non_periodic_channels(hcd);
1617 * Ensure NP Tx FIFO empty interrupt is disabled when
1618 * there are no non-periodic transfers to process.
1620 gintmsk_data_t gintmsk = {.d32 = 0 };
1621 gintmsk.b.nptxfempty = 1;
1622 DWC_MODIFY_REG32(&hcd->core_if->core_global_regs->
1623 gintmsk, gintmsk.d32, 0);
1628 #ifdef DWC_HS_ELECT_TST
1630 * Quick and dirty hack to implement the HS Electrical Test
1631 * SINGLE_STEP_GET_DEVICE_DESCRIPTOR feature.
1633 * This code was copied from our userspace app "hset". It sends a
1634 * Get Device Descriptor control sequence in two parts, first the
1635 * Setup packet by itself, followed some time later by the In and
1636 * Ack packets. Rather than trying to figure out how to add this
1637 * functionality to the normal driver code, we just hijack the
1638 * hardware, using these two function to drive the hardware
1642 static dwc_otg_core_global_regs_t *global_regs;
1643 static dwc_otg_host_global_regs_t *hc_global_regs;
1644 static dwc_otg_hc_regs_t *hc_regs;
1645 static uint32_t *data_fifo;
1647 static void do_setup(void)
1649 gintsts_data_t gintsts;
1650 hctsiz_data_t hctsiz;
1651 hcchar_data_t hcchar;
1656 DWC_WRITE_REG32(&hc_global_regs->haintmsk, 0x0001);
1659 DWC_WRITE_REG32(&hc_regs->hcintmsk, 0x04a3);
1662 gintsts.d32 = DWC_READ_REG32(&global_regs->gintsts);
1665 haint.d32 = DWC_READ_REG32(&hc_global_regs->haint);
1668 hcint.d32 = DWC_READ_REG32(&hc_regs->hcint);
1671 hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
1674 DWC_WRITE_REG32(&hc_regs->hcint, hcint.d32);
1677 DWC_WRITE_REG32(&hc_global_regs->haint, haint.d32);
1680 DWC_WRITE_REG32(&global_regs->gintsts, gintsts.d32);
1683 gintsts.d32 = DWC_READ_REG32(&global_regs->gintsts);
1686 * Send Setup packet (Get Device Descriptor)
1689 /* Make sure channel is disabled */
1690 hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
1691 if (hcchar.b.chen) {
1693 /* hcchar.b.chen = 1; */
1694 DWC_WRITE_REG32(&hc_regs->hcchar, hcchar.d32);
1699 gintsts.d32 = DWC_READ_REG32(&global_regs->gintsts);
1702 haint.d32 = DWC_READ_REG32(&hc_global_regs->haint);
1705 hcint.d32 = DWC_READ_REG32(&hc_regs->hcint);
1708 hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
1711 DWC_WRITE_REG32(&hc_regs->hcint, hcint.d32);
1714 DWC_WRITE_REG32(&hc_global_regs->haint, haint.d32);
1717 DWC_WRITE_REG32(&global_regs->gintsts, gintsts.d32);
1719 hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
1724 hctsiz.b.xfersize = 8;
1725 hctsiz.b.pktcnt = 1;
1726 hctsiz.b.pid = DWC_OTG_HC_PID_SETUP;
1727 DWC_WRITE_REG32(&hc_regs->hctsiz, hctsiz.d32);
1730 hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
1731 hcchar.b.eptype = DWC_OTG_EP_TYPE_CONTROL;
1736 DWC_WRITE_REG32(&hc_regs->hcchar, hcchar.d32);
1738 /* Fill FIFO with Setup data for Get Device Descriptor */
1739 data_fifo = (uint32_t *) ((char *)global_regs + 0x1000);
1740 DWC_WRITE_REG32(data_fifo++, 0x01000680);
1741 DWC_WRITE_REG32(data_fifo++, 0x00080000);
1743 gintsts.d32 = DWC_READ_REG32(&global_regs->gintsts);
1745 /* Wait for host channel interrupt */
1747 gintsts.d32 = DWC_READ_REG32(&global_regs->gintsts);
1748 } while (gintsts.b.hcintr == 0);
1750 /* Disable HCINTs */
1751 DWC_WRITE_REG32(&hc_regs->hcintmsk, 0x0000);
1753 /* Disable HAINTs */
1754 DWC_WRITE_REG32(&hc_global_regs->haintmsk, 0x0000);
1757 haint.d32 = DWC_READ_REG32(&hc_global_regs->haint);
1760 hcint.d32 = DWC_READ_REG32(&hc_regs->hcint);
1763 hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
1766 DWC_WRITE_REG32(&hc_regs->hcint, hcint.d32);
1769 DWC_WRITE_REG32(&hc_global_regs->haint, haint.d32);
1772 DWC_WRITE_REG32(&global_regs->gintsts, gintsts.d32);
1775 gintsts.d32 = DWC_READ_REG32(&global_regs->gintsts);
1778 static void do_in_ack(void)
1780 gintsts_data_t gintsts;
1781 hctsiz_data_t hctsiz;
1782 hcchar_data_t hcchar;
1785 host_grxsts_data_t grxsts;
1788 DWC_WRITE_REG32(&hc_global_regs->haintmsk, 0x0001);
1791 DWC_WRITE_REG32(&hc_regs->hcintmsk, 0x04a3);
1794 gintsts.d32 = DWC_READ_REG32(&global_regs->gintsts);
1797 haint.d32 = DWC_READ_REG32(&hc_global_regs->haint);
1800 hcint.d32 = DWC_READ_REG32(&hc_regs->hcint);
1803 hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
1806 DWC_WRITE_REG32(&hc_regs->hcint, hcint.d32);
1809 DWC_WRITE_REG32(&hc_global_regs->haint, haint.d32);
1812 DWC_WRITE_REG32(&global_regs->gintsts, gintsts.d32);
1815 gintsts.d32 = DWC_READ_REG32(&global_regs->gintsts);
1818 * Receive Control In packet
1821 /* Make sure channel is disabled */
1822 hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
1823 if (hcchar.b.chen) {
1826 DWC_WRITE_REG32(&hc_regs->hcchar, hcchar.d32);
1831 gintsts.d32 = DWC_READ_REG32(&global_regs->gintsts);
1834 haint.d32 = DWC_READ_REG32(&hc_global_regs->haint);
1837 hcint.d32 = DWC_READ_REG32(&hc_regs->hcint);
1840 hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
1843 DWC_WRITE_REG32(&hc_regs->hcint, hcint.d32);
1846 DWC_WRITE_REG32(&hc_global_regs->haint, haint.d32);
1849 DWC_WRITE_REG32(&global_regs->gintsts, gintsts.d32);
1851 hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
1856 hctsiz.b.xfersize = 8;
1857 hctsiz.b.pktcnt = 1;
1858 hctsiz.b.pid = DWC_OTG_HC_PID_DATA1;
1859 DWC_WRITE_REG32(&hc_regs->hctsiz, hctsiz.d32);
1862 hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
1863 hcchar.b.eptype = DWC_OTG_EP_TYPE_CONTROL;
1868 DWC_WRITE_REG32(&hc_regs->hcchar, hcchar.d32);
1870 gintsts.d32 = DWC_READ_REG32(&global_regs->gintsts);
1872 /* Wait for receive status queue interrupt */
1874 gintsts.d32 = DWC_READ_REG32(&global_regs->gintsts);
1875 } while (gintsts.b.rxstsqlvl == 0);
1878 grxsts.d32 = DWC_READ_REG32(&global_regs->grxstsp);
1880 /* Clear RXSTSQLVL in GINTSTS */
1882 gintsts.b.rxstsqlvl = 1;
1883 DWC_WRITE_REG32(&global_regs->gintsts, gintsts.d32);
1885 switch (grxsts.b.pktsts) {
1886 case DWC_GRXSTS_PKTSTS_IN:
1887 /* Read the data into the host buffer */
1888 if (grxsts.b.bcnt > 0) {
1890 int word_count = (grxsts.b.bcnt + 3) / 4;
1892 data_fifo = (uint32_t *) ((char *)global_regs + 0x1000);
1894 for (i = 0; i < word_count; i++) {
1895 (void)DWC_READ_REG32(data_fifo++);
1904 gintsts.d32 = DWC_READ_REG32(&global_regs->gintsts);
1906 /* Wait for receive status queue interrupt */
1908 gintsts.d32 = DWC_READ_REG32(&global_regs->gintsts);
1909 } while (gintsts.b.rxstsqlvl == 0);
1912 grxsts.d32 = DWC_READ_REG32(&global_regs->grxstsp);
1914 /* Clear RXSTSQLVL in GINTSTS */
1916 gintsts.b.rxstsqlvl = 1;
1917 DWC_WRITE_REG32(&global_regs->gintsts, gintsts.d32);
1919 switch (grxsts.b.pktsts) {
1920 case DWC_GRXSTS_PKTSTS_IN_XFER_COMP:
1927 gintsts.d32 = DWC_READ_REG32(&global_regs->gintsts);
1929 /* Wait for host channel interrupt */
1931 gintsts.d32 = DWC_READ_REG32(&global_regs->gintsts);
1932 } while (gintsts.b.hcintr == 0);
1935 haint.d32 = DWC_READ_REG32(&hc_global_regs->haint);
1938 hcint.d32 = DWC_READ_REG32(&hc_regs->hcint);
1941 hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
1944 DWC_WRITE_REG32(&hc_regs->hcint, hcint.d32);
1947 DWC_WRITE_REG32(&hc_global_regs->haint, haint.d32);
1950 DWC_WRITE_REG32(&global_regs->gintsts, gintsts.d32);
1953 gintsts.d32 = DWC_READ_REG32(&global_regs->gintsts);
1955 /* usleep(100000); */
1960 * Send handshake packet
1964 haint.d32 = DWC_READ_REG32(&hc_global_regs->haint);
1967 hcint.d32 = DWC_READ_REG32(&hc_regs->hcint);
1970 hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
1973 DWC_WRITE_REG32(&hc_regs->hcint, hcint.d32);
1976 DWC_WRITE_REG32(&hc_global_regs->haint, haint.d32);
1979 DWC_WRITE_REG32(&global_regs->gintsts, gintsts.d32);
1982 gintsts.d32 = DWC_READ_REG32(&global_regs->gintsts);
1984 /* Make sure channel is disabled */
1985 hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
1986 if (hcchar.b.chen) {
1989 DWC_WRITE_REG32(&hc_regs->hcchar, hcchar.d32);
1994 gintsts.d32 = DWC_READ_REG32(&global_regs->gintsts);
1997 haint.d32 = DWC_READ_REG32(&hc_global_regs->haint);
2000 hcint.d32 = DWC_READ_REG32(&hc_regs->hcint);
2003 hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
2006 DWC_WRITE_REG32(&hc_regs->hcint, hcint.d32);
2009 DWC_WRITE_REG32(&hc_global_regs->haint, haint.d32);
2012 DWC_WRITE_REG32(&global_regs->gintsts, gintsts.d32);
2014 hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
2019 hctsiz.b.xfersize = 0;
2020 hctsiz.b.pktcnt = 1;
2021 hctsiz.b.pid = DWC_OTG_HC_PID_DATA1;
2022 DWC_WRITE_REG32(&hc_regs->hctsiz, hctsiz.d32);
2025 hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
2026 hcchar.b.eptype = DWC_OTG_EP_TYPE_CONTROL;
2031 DWC_WRITE_REG32(&hc_regs->hcchar, hcchar.d32);
2033 gintsts.d32 = DWC_READ_REG32(&global_regs->gintsts);
2035 /* Wait for host channel interrupt */
2037 gintsts.d32 = DWC_READ_REG32(&global_regs->gintsts);
2038 } while (gintsts.b.hcintr == 0);
2040 /* Disable HCINTs */
2041 DWC_WRITE_REG32(&hc_regs->hcintmsk, 0x0000);
2043 /* Disable HAINTs */
2044 DWC_WRITE_REG32(&hc_global_regs->haintmsk, 0x0000);
2047 haint.d32 = DWC_READ_REG32(&hc_global_regs->haint);
2050 hcint.d32 = DWC_READ_REG32(&hc_regs->hcint);
2053 hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
2056 DWC_WRITE_REG32(&hc_regs->hcint, hcint.d32);
2059 DWC_WRITE_REG32(&hc_global_regs->haint, haint.d32);
2062 DWC_WRITE_REG32(&global_regs->gintsts, gintsts.d32);
2065 gintsts.d32 = DWC_READ_REG32(&global_regs->gintsts);
2069 /** Handles hub class-specific requests. */
2070 int dwc_otg_hcd_hub_control(dwc_otg_hcd_t *dwc_otg_hcd,
2073 uint16_t wIndex, uint8_t *buf, uint16_t wLength)
2077 dwc_otg_core_if_t *core_if = dwc_otg_hcd->core_if;
2078 usb_hub_descriptor_t *hub_desc;
2079 hprt0_data_t hprt0 = {.d32 = 0 };
2081 uint32_t port_status;
2084 case UCR_CLEAR_HUB_FEATURE:
2085 DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD HUB CONTROL - "
2086 "ClearHubFeature 0x%x\n", wValue);
2088 case UHF_C_HUB_LOCAL_POWER:
2089 case UHF_C_HUB_OVER_CURRENT:
2090 /* Nothing required here */
2093 retval = -DWC_E_INVALID;
2094 DWC_ERROR("DWC OTG HCD - "
2095 "ClearHubFeature request %xh unknown\n",
2099 case UCR_CLEAR_PORT_FEATURE:
2100 #ifdef CONFIG_USB_DWC_OTG_LPM
2101 if (wValue != UHF_PORT_L1)
2103 if (!wIndex || wIndex > 1)
2107 case UHF_PORT_ENABLE:
2108 DWC_DEBUGPL(DBG_ANY, "DWC OTG HCD HUB CONTROL - "
2109 "ClearPortFeature USB_PORT_FEAT_ENABLE\n");
2110 hprt0.d32 = dwc_otg_read_hprt0(core_if);
2112 DWC_WRITE_REG32(core_if->host_if->hprt0, hprt0.d32);
2114 case UHF_PORT_SUSPEND:
2115 DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD HUB CONTROL - "
2116 "ClearPortFeature USB_PORT_FEAT_SUSPEND\n");
2118 if (core_if->power_down == 2) {
2119 dwc_otg_host_hibernation_restore(core_if, 0, 0);
2121 DWC_WRITE_REG32(core_if->pcgcctl, 0);
2124 hprt0.d32 = dwc_otg_read_hprt0(core_if);
2126 DWC_WRITE_REG32(core_if->host_if->hprt0,
2128 hprt0.b.prtsusp = 0;
2129 /* Clear Resume bit */
2132 DWC_WRITE_REG32(core_if->host_if->hprt0,
2136 #ifdef CONFIG_USB_DWC_OTG_LPM
2139 pcgcctl_data_t pcgcctl = {.d32 = 0 };
2140 glpmcfg_data_t lpmcfg = {.d32 = 0 };
2143 DWC_READ_REG32(&core_if->core_global_regs->
2145 lpmcfg.b.en_utmi_sleep = 0;
2146 lpmcfg.b.hird_thres &= (~(1 << 4));
2147 lpmcfg.b.prt_sleep_sts = 1;
2148 DWC_WRITE_REG32(&core_if->core_global_regs->
2149 glpmcfg, lpmcfg.d32);
2151 /* Clear Enbl_L1Gating bit. */
2152 pcgcctl.b.enbl_sleep_gating = 1;
2153 DWC_MODIFY_REG32(core_if->pcgcctl, pcgcctl.d32,
2158 hprt0.d32 = dwc_otg_read_hprt0(core_if);
2160 DWC_WRITE_REG32(core_if->host_if->hprt0,
2162 /* This bit will be cleared in wakeup interrupt handle */
2166 case UHF_PORT_POWER:
2167 DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD HUB CONTROL - "
2168 "ClearPortFeature USB_PORT_FEAT_POWER\n");
2169 hprt0.d32 = dwc_otg_read_hprt0(core_if);
2171 DWC_WRITE_REG32(core_if->host_if->hprt0, hprt0.d32);
2173 case UHF_PORT_INDICATOR:
2174 DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD HUB CONTROL - "
2175 "ClearPortFeature USB_PORT_FEAT_INDICATOR\n");
2176 /* Port inidicator not supported */
2178 case UHF_C_PORT_CONNECTION:
2179 /* Clears drivers internal connect status change
2181 DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD HUB CONTROL - "
2182 "ClearPortFeature USB_PORT_FEAT_C_CONNECTION\n");
2183 dwc_otg_hcd->flags.b.port_connect_status_change = 0;
2185 case UHF_C_PORT_RESET:
2186 /* Clears the driver's internal Port Reset Change
2188 DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD HUB CONTROL - "
2189 "ClearPortFeature USB_PORT_FEAT_C_RESET\n");
2190 dwc_otg_hcd->flags.b.port_reset_change = 0;
2192 case UHF_C_PORT_ENABLE:
2193 /* Clears the driver's internal Port
2194 * Enable/Disable Change flag */
2195 DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD HUB CONTROL - "
2196 "ClearPortFeature USB_PORT_FEAT_C_ENABLE\n");
2197 dwc_otg_hcd->flags.b.port_enable_change = 0;
2199 case UHF_C_PORT_SUSPEND:
2200 /* Clears the driver's internal Port Suspend
2201 * Change flag, which is set when resume signaling on
2202 * the host port is complete */
2203 DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD HUB CONTROL - "
2204 "ClearPortFeature USB_PORT_FEAT_C_SUSPEND\n");
2205 dwc_otg_hcd->flags.b.port_suspend_change = 0;
2207 #ifdef CONFIG_USB_DWC_OTG_LPM
2209 dwc_otg_hcd->flags.b.port_l1_change = 0;
2212 case UHF_C_PORT_OVER_CURRENT:
2213 DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD HUB CONTROL - "
2214 "ClearPortFeature USB_PORT_FEAT_C_OVER_CURRENT\n");
2215 dwc_otg_hcd->flags.b.port_over_current_change = 0;
2218 retval = -DWC_E_INVALID;
2219 DWC_ERROR("DWC OTG HCD - "
2220 "ClearPortFeature request %xh "
2221 "unknown or unsupported\n", wValue);
2224 case UCR_GET_HUB_DESCRIPTOR:
2225 DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD HUB CONTROL - "
2226 "GetHubDescriptor\n");
2227 hub_desc = (usb_hub_descriptor_t *) buf;
2228 hub_desc->bDescLength = 9;
2229 hub_desc->bDescriptorType = 0x29;
2230 hub_desc->bNbrPorts = 1;
2231 USETW(hub_desc->wHubCharacteristics, 0x08);
2232 hub_desc->bPwrOn2PwrGood = 1;
2233 hub_desc->bHubContrCurrent = 0;
2234 hub_desc->DeviceRemovable[0] = 0;
2235 hub_desc->DeviceRemovable[1] = 0xff;
2237 case UCR_GET_HUB_STATUS:
2238 DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD HUB CONTROL - "
2240 DWC_MEMSET(buf, 0, 4);
2242 case UCR_GET_PORT_STATUS:
2243 DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD HUB CONTROL - "
2244 "GetPortStatus wIndex = 0x%04x FLAGS=0x%08x\n",
2245 wIndex, dwc_otg_hcd->flags.d32);
2246 if (!wIndex || wIndex > 1)
2251 if (dwc_otg_hcd->flags.b.port_connect_status_change)
2252 port_status |= (1 << UHF_C_PORT_CONNECTION);
2254 if (dwc_otg_hcd->flags.b.port_enable_change)
2255 port_status |= (1 << UHF_C_PORT_ENABLE);
2257 if (dwc_otg_hcd->flags.b.port_suspend_change)
2258 port_status |= (1 << UHF_C_PORT_SUSPEND);
2260 if (dwc_otg_hcd->flags.b.port_l1_change)
2261 port_status |= (1 << UHF_C_PORT_L1);
2263 if (dwc_otg_hcd->flags.b.port_reset_change) {
2264 port_status |= (1 << UHF_C_PORT_RESET);
2267 if (dwc_otg_hcd->flags.b.port_over_current_change) {
2268 DWC_WARN("Overcurrent change detected\n");
2269 port_status |= (1 << UHF_C_PORT_OVER_CURRENT);
2272 if (!dwc_otg_hcd->flags.b.port_connect_status) {
2274 * The port is disconnected, which means the core is
2275 * either in device mode or it soon will be. Just
2276 * return 0's for the remainder of the port status
2277 * since the port register can't be read if the core
2278 * is in device mode.
2280 *((__le32 *) buf) = dwc_cpu_to_le32(&port_status);
2284 hprt0.d32 = DWC_READ_REG32(core_if->host_if->hprt0);
2285 DWC_DEBUGPL(DBG_HCDV, " HPRT0: 0x%08x\n", hprt0.d32);
2287 if (hprt0.b.prtconnsts)
2288 port_status |= (1 << UHF_PORT_CONNECTION);
2291 port_status |= (1 << UHF_PORT_ENABLE);
2293 if (hprt0.b.prtsusp)
2294 port_status |= (1 << UHF_PORT_SUSPEND);
2296 if (hprt0.b.prtovrcurract)
2297 port_status |= (1 << UHF_PORT_OVER_CURRENT);
2300 port_status |= (1 << UHF_PORT_RESET);
2303 port_status |= (1 << UHF_PORT_POWER);
2305 if (hprt0.b.prtspd == DWC_HPRT0_PRTSPD_HIGH_SPEED)
2306 port_status |= (1 << UHF_PORT_HIGH_SPEED);
2307 else if (hprt0.b.prtspd == DWC_HPRT0_PRTSPD_LOW_SPEED)
2308 port_status |= (1 << UHF_PORT_LOW_SPEED);
2310 if (hprt0.b.prttstctl)
2311 port_status |= (1 << UHF_PORT_TEST);
2312 if (dwc_otg_get_lpm_portsleepstatus(dwc_otg_hcd->core_if)) {
2313 port_status |= (1 << UHF_PORT_L1);
2316 For Synopsys HW emulation of Power down wkup_control asserts the
2317 hreset_n and prst_n on suspned. This causes the HPRT0 to be zero.
2318 We intentionally tell the software that port is in L2Suspend state.
2321 if ((core_if->power_down == 2)
2322 && (core_if->hibernation_suspend == 1)) {
2323 port_status |= (1 << UHF_PORT_SUSPEND);
2325 /* USB_PORT_FEAT_INDICATOR unsupported always 0 */
2327 *((__le32 *) buf) = dwc_cpu_to_le32(&port_status);
2330 case UCR_SET_HUB_FEATURE:
2331 DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD HUB CONTROL - "
2333 /* No HUB features supported */
2335 case UCR_SET_PORT_FEATURE:
2336 if (wValue != UHF_PORT_TEST && (!wIndex || wIndex > 1))
2339 if (!dwc_otg_hcd->flags.b.port_connect_status) {
2341 * The port is disconnected, which means the core is
2342 * either in device mode or it soon will be. Just
2343 * return without doing anything since the port
2344 * register can't be written if the core is in device
2351 case UHF_PORT_SUSPEND:
2352 DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD HUB CONTROL - "
2353 "SetPortFeature - USB_PORT_FEAT_SUSPEND\n");
2354 if (dwc_otg_hcd_otg_port(dwc_otg_hcd) != wIndex) {
2357 if (core_if->power_down == 2) {
2359 dwc_irqflags_t flags;
2360 pcgcctl_data_t pcgcctl = {.d32 = 0 };
2361 gpwrdn_data_t gpwrdn = {.d32 = 0 };
2362 gusbcfg_data_t gusbcfg = {.d32 = 0 };
2363 #ifdef DWC_DEV_SRPCAP
2364 int32_t otg_cap_param =
2365 core_if->core_params->otg_cap;
2368 ("Preparing for complete power-off\n");
2370 /* Save registers before hibernation */
2371 dwc_otg_save_global_regs(core_if);
2372 dwc_otg_save_host_regs(core_if);
2374 hprt0.d32 = dwc_otg_read_hprt0(core_if);
2375 hprt0.b.prtsusp = 1;
2377 DWC_WRITE_REG32(core_if->host_if->hprt0,
2379 /* Spin hprt0.b.prtsusp to became 1 */
2381 hprt0.d32 = dwc_otg_read_hprt0(core_if);
2382 if (hprt0.b.prtsusp) {
2386 } while (--timeout);
2388 DWC_WARN("Suspend wasn't genereted\n");
2393 * We need to disable interrupts to prevent servicing of any IRQ
2394 * during going to hibernation
2396 DWC_SPINLOCK_IRQSAVE(dwc_otg_hcd->lock, &flags);
2397 core_if->lx_state = DWC_OTG_L2;
2398 #ifdef DWC_DEV_SRPCAP
2399 hprt0.d32 = dwc_otg_read_hprt0(core_if);
2402 DWC_WRITE_REG32(core_if->host_if->hprt0,
2406 DWC_READ_REG32(&core_if->
2407 core_global_regs->gusbcfg);
2408 if (gusbcfg.b.ulpi_utmi_sel == 1) {
2409 /* ULPI interface */
2410 /* Suspend the Phy Clock */
2412 pcgcctl.b.stoppclk = 1;
2413 DWC_MODIFY_REG32(core_if->pcgcctl, 0,
2416 gpwrdn.b.pmuactv = 1;
2418 (&core_if->core_global_regs->gpwrdn,
2421 /* UTMI+ Interface */
2422 gpwrdn.b.pmuactv = 1;
2424 (&core_if->core_global_regs->gpwrdn,
2427 pcgcctl.b.stoppclk = 1;
2428 DWC_MODIFY_REG32(core_if->pcgcctl, 0,
2432 #ifdef DWC_DEV_SRPCAP
2434 gpwrdn.b.dis_vbus = 1;
2435 DWC_MODIFY_REG32(&core_if->
2436 core_global_regs->gpwrdn, 0,
2440 gpwrdn.b.pmuintsel = 1;
2441 DWC_MODIFY_REG32(&core_if->
2442 core_global_regs->gpwrdn, 0,
2447 #ifdef DWC_DEV_SRPCAP
2448 gpwrdn.b.srp_det_msk = 1;
2450 gpwrdn.b.disconn_det_msk = 1;
2451 gpwrdn.b.lnstchng_msk = 1;
2452 gpwrdn.b.sts_chngint_msk = 1;
2453 DWC_MODIFY_REG32(&core_if->
2454 core_global_regs->gpwrdn, 0,
2458 /* Enable Power Down Clamp and all interrupts in GPWRDN */
2460 gpwrdn.b.pwrdnclmp = 1;
2461 DWC_MODIFY_REG32(&core_if->
2462 core_global_regs->gpwrdn, 0,
2466 /* Switch off VDD */
2468 gpwrdn.b.pwrdnswtch = 1;
2469 DWC_MODIFY_REG32(&core_if->
2470 core_global_regs->gpwrdn, 0,
2473 #ifdef DWC_DEV_SRPCAP
2474 if (otg_cap_param ==
2475 DWC_OTG_CAP_PARAM_HNP_SRP_CAPABLE) {
2476 core_if->pwron_timer_started = 1;
2477 DWC_TIMER_SCHEDULE(core_if->pwron_timer,
2481 /* Save gpwrdn register for further usage if stschng interrupt */
2482 core_if->gr_backup->gpwrdn_local =
2483 DWC_READ_REG32(&core_if->core_global_regs->
2486 /* Set flag to indicate that we are in hibernation */
2487 core_if->hibernation_suspend = 1;
2488 DWC_SPINUNLOCK_IRQRESTORE(dwc_otg_hcd->lock,
2491 DWC_PRINTF("Host hibernation completed\n");
2492 /* Exit from case statement */
2496 if (dwc_otg_hcd_otg_port(dwc_otg_hcd) == wIndex &&
2497 dwc_otg_hcd->fops->get_b_hnp_enable(dwc_otg_hcd)) {
2498 gotgctl_data_t gotgctl = {.d32 = 0 };
2499 gotgctl.b.hstsethnpen = 1;
2500 DWC_MODIFY_REG32(&core_if->
2501 core_global_regs->gotgctl, 0,
2503 core_if->op_state = A_SUSPEND;
2505 hprt0.d32 = dwc_otg_read_hprt0(core_if);
2506 hprt0.b.prtsusp = 1;
2507 DWC_WRITE_REG32(core_if->host_if->hprt0, hprt0.d32);
2509 dwc_irqflags_t flags;
2510 /* Update lx_state */
2511 DWC_SPINLOCK_IRQSAVE(dwc_otg_hcd->lock, &flags);
2512 core_if->lx_state = DWC_OTG_L2;
2513 DWC_SPINUNLOCK_IRQRESTORE(dwc_otg_hcd->lock,
2516 /* Suspend the Phy Clock */
2517 if (core_if->otg_ver == 0) {
2518 pcgcctl_data_t pcgcctl = {.d32 = 0 };
2519 pcgcctl.b.stoppclk = 1;
2520 DWC_MODIFY_REG32(core_if->pcgcctl, 0,
2525 /* For HNP the bus must be suspended for at least 200ms. */
2526 if (dwc_otg_hcd->fops->get_b_hnp_enable(dwc_otg_hcd)) {
2527 pcgcctl_data_t pcgcctl = {.d32 = 0 };
2528 pcgcctl.b.stoppclk = 1;
2529 DWC_MODIFY_REG32(core_if->pcgcctl, pcgcctl.d32,
2534 /** @todo - check how sw can wait for 1 sec to check asesvld??? */
2536 if (core_if->adp_enable) {
2537 gotgctl_data_t gotgctl = {.d32 = 0 };
2538 gpwrdn_data_t gpwrdn;
2540 while (gotgctl.b.asesvld == 1) {
2543 (&core_if->core_global_regs->gotgctl);
2547 /* Enable Power Down Logic */
2549 gpwrdn.b.pmuactv = 1;
2550 DWC_MODIFY_REG32(&core_if->
2551 core_global_regs->gpwrdn, 0,
2554 /* Unmask SRP detected interrupt from Power Down Logic */
2556 gpwrdn.b.srp_det_msk = 1;
2557 DWC_MODIFY_REG32(&core_if->
2558 core_global_regs->gpwrdn, 0,
2561 dwc_otg_adp_probe_start(core_if);
2565 case UHF_PORT_POWER:
2566 DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD HUB CONTROL - "
2567 "SetPortFeature - USB_PORT_FEAT_POWER\n");
2568 hprt0.d32 = dwc_otg_read_hprt0(core_if);
2570 DWC_WRITE_REG32(core_if->host_if->hprt0, hprt0.d32);
2572 case UHF_PORT_RESET:
2573 if ((core_if->power_down == 2)
2574 && (core_if->hibernation_suspend == 1)) {
2575 /* If we are going to exit from Hibernated
2576 * state via USB RESET.
2578 dwc_otg_host_hibernation_restore(core_if, 0, 1);
2580 hprt0.d32 = dwc_otg_read_hprt0(core_if);
2582 DWC_DEBUGPL(DBG_HCD,
2583 "DWC OTG HCD HUB CONTROL - "
2584 "SetPortFeature - USB_PORT_FEAT_RESET\n");
2586 pcgcctl_data_t pcgcctl = {.d32 = 0 };
2587 pcgcctl.b.enbl_sleep_gating = 1;
2588 pcgcctl.b.stoppclk = 1;
2589 DWC_MODIFY_REG32(core_if->pcgcctl,
2591 DWC_WRITE_REG32(core_if->pcgcctl, 0);
2593 #ifdef CONFIG_USB_DWC_OTG_LPM
2595 glpmcfg_data_t lpmcfg;
2597 DWC_READ_REG32(&core_if->
2600 if (lpmcfg.b.prt_sleep_sts) {
2601 lpmcfg.b.en_utmi_sleep = 0;
2602 lpmcfg.b.hird_thres &=
2604 DWC_WRITE_REG32(&core_if->
2612 hprt0.d32 = dwc_otg_read_hprt0(core_if);
2613 /* Clear suspend bit if resetting from suspended state. */
2614 hprt0.b.prtsusp = 0;
2615 /* When B-Host the Port reset bit is set in
2616 * the Start HCD Callback function, so that
2617 * the reset is started within 1ms of the HNP
2618 * success interrupt. */
2619 if (!dwc_otg_hcd_is_b_host(dwc_otg_hcd)) {
2623 ("Indeed it is in host mode hprt0 = %08x\n",
2625 DWC_WRITE_REG32(core_if->host_if->hprt0,
2628 /* Clear reset bit in 10ms (FS/LS) or 50ms (HS) */
2631 DWC_WRITE_REG32(core_if->host_if->hprt0,
2633 core_if->lx_state = DWC_OTG_L0; /* Now back to the on state */
2636 #ifdef DWC_HS_ELECT_TST
2640 gintmsk_data_t gintmsk;
2642 t = (wIndex >> 8); /* MSB wIndex USB */
2643 DWC_DEBUGPL(DBG_HCD,
2644 "DWC OTG HCD HUB CONTROL - "
2645 "SetPortFeature - USB_PORT_FEAT_TEST %d\n",
2647 DWC_WARN("USB_PORT_FEAT_TEST %d\n", t);
2649 hprt0.d32 = dwc_otg_read_hprt0(core_if);
2650 hprt0.b.prttstctl = t;
2651 DWC_WRITE_REG32(core_if->host_if->hprt0,
2654 /* Setup global vars with reg addresses (quick and
2655 * dirty hack, should be cleaned up)
2657 global_regs = core_if->core_global_regs;
2659 core_if->host_if->host_global_regs;
2661 (dwc_otg_hc_regs_t *) ((char *)
2665 (uint32_t *) ((char *)global_regs +
2668 if (t == 6) { /* HS_HOST_PORT_SUSPEND_RESUME */
2669 /* Save current interrupt mask */
2672 (&global_regs->gintmsk);
2674 /* Disable all interrupts while we muck with
2675 * the hardware directly
2677 DWC_WRITE_REG32(&global_regs->
2680 /* 15 second delay per the test spec */
2683 /* Drive suspend on the root port */
2685 dwc_otg_read_hprt0(core_if);
2686 hprt0.b.prtsusp = 1;
2688 DWC_WRITE_REG32(core_if->
2692 /* 15 second delay per the test spec */
2695 /* Drive resume on the root port */
2697 dwc_otg_read_hprt0(core_if);
2698 hprt0.b.prtsusp = 0;
2700 DWC_WRITE_REG32(core_if->
2705 /* Clear the resume bit */
2707 DWC_WRITE_REG32(core_if->
2711 /* Restore interrupts */
2712 DWC_WRITE_REG32(&global_regs->
2715 } else if (t == 7) { /* SINGLE_STEP_GET_DEVICE_DESCRIPTOR setup */
2716 /* Save current interrupt mask */
2719 (&global_regs->gintmsk);
2721 /* Disable all interrupts while we muck with
2722 * the hardware directly
2724 DWC_WRITE_REG32(&global_regs->
2727 /* 15 second delay per the test spec */
2730 /* Send the Setup packet */
2733 /* 15 second delay so nothing else happens for awhile */
2736 /* Restore interrupts */
2737 DWC_WRITE_REG32(&global_regs->
2740 } else if (t == 8) { /* SINGLE_STEP_GET_DEVICE_DESCRIPTOR execute */
2741 /* Save current interrupt mask */
2744 (&global_regs->gintmsk);
2746 /* Disable all interrupts while we muck with
2747 * the hardware directly
2749 DWC_WRITE_REG32(&global_regs->
2752 /* Send the Setup packet */
2755 /* 15 second delay so nothing else happens for awhile */
2758 /* Send the In and Ack packets */
2761 /* 15 second delay so nothing else happens for awhile */
2764 /* Restore interrupts */
2765 DWC_WRITE_REG32(&global_regs->
2772 #endif /* DWC_HS_ELECT_TST */
2774 case UHF_PORT_INDICATOR:
2775 DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD HUB CONTROL - "
2776 "SetPortFeature - USB_PORT_FEAT_INDICATOR\n");
2780 retval = -DWC_E_INVALID;
2781 DWC_ERROR("DWC OTG HCD - "
2782 "SetPortFeature request %xh "
2783 "unknown or unsupported\n", wValue);
2787 #ifdef CONFIG_USB_DWC_OTG_LPM
2788 case UCR_SET_AND_TEST_PORT_FEATURE:
2789 if (wValue != UHF_PORT_L1) {
2793 int portnum, hird, devaddr, remwake;
2794 glpmcfg_data_t lpmcfg;
2795 uint32_t time_usecs;
2796 gintsts_data_t gintsts;
2797 gintmsk_data_t gintmsk;
2799 if (!dwc_otg_get_param_lpm_enable(core_if)) {
2802 if (wValue != UHF_PORT_L1 || wLength != 1) {
2805 /* Check if the port currently is in SLEEP state */
2807 DWC_READ_REG32(&core_if->core_global_regs->glpmcfg);
2808 if (lpmcfg.b.prt_sleep_sts) {
2809 DWC_INFO("Port is already in sleep mode\n");
2810 buf[0] = 0; /* Return success */
2814 portnum = wIndex & 0xf;
2815 hird = (wIndex >> 4) & 0xf;
2816 devaddr = (wIndex >> 8) & 0x7f;
2817 remwake = (wIndex >> 15);
2820 retval = -DWC_E_INVALID;
2822 ("Wrong port number(%d) in SetandTestPortFeature request\n",
2828 ("SetandTestPortFeature request: portnum = %d, hird = %d, devaddr = %d, rewake = %d\n",
2829 portnum, hird, devaddr, remwake);
2830 /* Disable LPM interrupt */
2832 gintmsk.b.lpmtranrcvd = 1;
2833 DWC_MODIFY_REG32(&core_if->core_global_regs->gintmsk,
2836 if (dwc_otg_hcd_send_lpm
2837 (dwc_otg_hcd, devaddr, hird, remwake)) {
2838 retval = -DWC_E_INVALID;
2842 time_usecs = 10 * (lpmcfg.b.retry_count + 1);
2843 /* We will consider timeout if time_usecs microseconds pass,
2844 * and we don't receive LPM transaction status.
2845 * After receiving non-error responce(ACK/NYET/STALL) from device,
2846 * core will set lpmtranrcvd bit.
2850 DWC_READ_REG32(&core_if->core_global_regs->
2852 if (gintsts.b.lpmtranrcvd) {
2856 } while (--time_usecs);
2857 /* lpm_int bit will be cleared in LPM interrupt handler */
2864 if (!gintsts.b.lpmtranrcvd) {
2865 buf[0] = 0x3; /* Completion code is Timeout */
2866 dwc_otg_hcd_free_hc_from_lpm(dwc_otg_hcd);
2869 DWC_READ_REG32(&core_if->core_global_regs->
2871 if (lpmcfg.b.lpm_resp == 0x3) {
2872 /* ACK responce from the device */
2873 buf[0] = 0x00; /* Success */
2874 } else if (lpmcfg.b.lpm_resp == 0x2) {
2875 /* NYET responce from the device */
2878 /* Otherwise responce with Timeout */
2882 DWC_PRINTF("Device responce to LPM trans is %x\n",
2884 DWC_MODIFY_REG32(&core_if->core_global_regs->gintmsk, 0,
2889 #endif /* CONFIG_USB_DWC_OTG_LPM */
2892 retval = -DWC_E_INVALID;
2893 DWC_WARN("DWC OTG HCD - "
2894 "Unknown hub control request type or invalid typeReq: %xh wIndex: %xh wValue: %xh\n",
2895 typeReq, wIndex, wValue);
2902 #ifdef CONFIG_USB_DWC_OTG_LPM
2903 /** Returns index of host channel to perform LPM transaction. */
2904 int dwc_otg_hcd_get_hc_for_lpm_tran(dwc_otg_hcd_t *hcd, uint8_t devaddr)
2906 dwc_otg_core_if_t *core_if = hcd->core_if;
2908 hcchar_data_t hcchar;
2909 gintmsk_data_t gintmsk = {.d32 = 0 };
2911 if (DWC_CIRCLEQ_EMPTY(&hcd->free_hc_list)) {
2912 DWC_PRINTF("No free channel to select for LPM transaction\n");
2916 hc = DWC_CIRCLEQ_FIRST(&hcd->free_hc_list);
2918 /* Mask host channel interrupts. */
2919 gintmsk.b.hcintr = 1;
2920 DWC_MODIFY_REG32(&core_if->core_global_regs->gintmsk, gintmsk.d32, 0);
2922 /* Fill fields that core needs for LPM transaction */
2923 hcchar.b.devaddr = devaddr;
2925 hcchar.b.eptype = DWC_OTG_EP_TYPE_CONTROL;
2927 hcchar.b.lspddev = (hc->speed == DWC_OTG_EP_SPEED_LOW);
2928 hcchar.b.epdir = 0; /* OUT */
2929 DWC_WRITE_REG32(&core_if->host_if->hc_regs[hc->hc_num]->hcchar,
2932 /* Remove the host channel from the free list. */
2933 DWC_CIRCLEQ_REMOVE_INIT(&hcd->free_hc_list, hc, hc_list_entry);
2935 DWC_PRINTF("hcnum = %d devaddr = %d\n", hc->hc_num, devaddr);
2940 /** Release hc after performing LPM transaction */
2941 void dwc_otg_hcd_free_hc_from_lpm(dwc_otg_hcd_t *hcd)
2944 glpmcfg_data_t lpmcfg;
2947 lpmcfg.d32 = DWC_READ_REG32(&hcd->core_if->core_global_regs->glpmcfg);
2948 hc_num = lpmcfg.b.lpm_chan_index;
2950 hc = hcd->hc_ptr_array[hc_num];
2952 DWC_PRINTF("Freeing channel %d after LPM\n", hc_num);
2953 /* Return host channel to free list */
2954 DWC_CIRCLEQ_INSERT_TAIL(&hcd->free_hc_list, hc, hc_list_entry);
2957 int dwc_otg_hcd_send_lpm(dwc_otg_hcd_t *hcd, uint8_t devaddr, uint8_t hird,
2958 uint8_t bRemoteWake)
2960 glpmcfg_data_t lpmcfg;
2961 pcgcctl_data_t pcgcctl = {.d32 = 0 };
2964 channel = dwc_otg_hcd_get_hc_for_lpm_tran(hcd, devaddr);
2969 pcgcctl.b.enbl_sleep_gating = 1;
2970 DWC_MODIFY_REG32(hcd->core_if->pcgcctl, 0, pcgcctl.d32);
2972 /* Read LPM config register */
2973 lpmcfg.d32 = DWC_READ_REG32(&hcd->core_if->core_global_regs->glpmcfg);
2975 /* Program LPM transaction fields */
2976 lpmcfg.b.rem_wkup_en = bRemoteWake;
2977 lpmcfg.b.hird = hird;
2979 if (dwc_otg_get_param_besl_enable(hcd->core_if)) {
2980 lpmcfg.b.hird_thres = 0x16;
2981 lpmcfg.b.en_besl = 1;
2983 lpmcfg.b.hird_thres = 0x1c;
2986 lpmcfg.b.lpm_chan_index = channel;
2987 lpmcfg.b.en_utmi_sleep = 1;
2988 /* Program LPM config register */
2989 DWC_WRITE_REG32(&hcd->core_if->core_global_regs->glpmcfg, lpmcfg.d32);
2991 /* Send LPM transaction */
2992 lpmcfg.b.send_lpm = 1;
2993 DWC_WRITE_REG32(&hcd->core_if->core_global_regs->glpmcfg, lpmcfg.d32);
2998 #endif /* CONFIG_USB_DWC_OTG_LPM */
3000 int dwc_otg_hcd_is_status_changed(dwc_otg_hcd_t *hcd, int port)
3005 return -DWC_E_INVALID;
3008 retval = (hcd->flags.b.port_connect_status_change ||
3009 hcd->flags.b.port_reset_change ||
3010 hcd->flags.b.port_enable_change ||
3011 hcd->flags.b.port_suspend_change ||
3012 hcd->flags.b.port_over_current_change);
3015 DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD HUB STATUS DATA:"
3016 " Root port status changed\n");
3017 DWC_DEBUGPL(DBG_HCDV, " port_connect_status_change: %d\n",
3018 hcd->flags.b.port_connect_status_change);
3019 DWC_DEBUGPL(DBG_HCDV, " port_reset_change: %d\n",
3020 hcd->flags.b.port_reset_change);
3021 DWC_DEBUGPL(DBG_HCDV, " port_enable_change: %d\n",
3022 hcd->flags.b.port_enable_change);
3023 DWC_DEBUGPL(DBG_HCDV, " port_suspend_change: %d\n",
3024 hcd->flags.b.port_suspend_change);
3025 DWC_DEBUGPL(DBG_HCDV, " port_over_current_change: %d\n",
3026 hcd->flags.b.port_over_current_change);
3032 int dwc_otg_hcd_get_frame_number(dwc_otg_hcd_t *dwc_otg_hcd)
3036 DWC_READ_REG32(&dwc_otg_hcd->core_if->host_if->
3037 host_global_regs->hfnum);
3040 DWC_DEBUGPL(DBG_HCDV, "DWC OTG HCD GET FRAME NUMBER %d\n",
3043 return hfnum.b.frnum;
3046 int dwc_otg_hcd_start(dwc_otg_hcd_t *hcd,
3047 struct dwc_otg_hcd_function_ops *fops)
3052 if (!dwc_otg_is_device_mode(hcd->core_if) &&
3053 (!hcd->core_if->adp_enable || hcd->core_if->adp.adp_started)) {
3054 dwc_otg_hcd_reinit(hcd);
3056 retval = -DWC_E_NO_DEVICE;
3062 void *dwc_otg_hcd_get_priv_data(dwc_otg_hcd_t *hcd)
3067 void dwc_otg_hcd_set_priv_data(dwc_otg_hcd_t *hcd, void *priv_data)
3069 hcd->priv = priv_data;
3072 uint32_t dwc_otg_hcd_otg_port(dwc_otg_hcd_t *hcd)
3074 return hcd->otg_port;
3077 uint32_t dwc_otg_hcd_is_b_host(dwc_otg_hcd_t *hcd)
3080 if (hcd->core_if->op_state == B_HOST) {
3089 dwc_otg_hcd_urb_t *dwc_otg_hcd_urb_alloc(dwc_otg_hcd_t *hcd,
3090 int iso_desc_count, int atomic_alloc)
3092 dwc_otg_hcd_urb_t *dwc_otg_urb;
3096 sizeof(*dwc_otg_urb) +
3097 iso_desc_count * sizeof(struct dwc_otg_hcd_iso_packet_desc);
3099 dwc_otg_urb = DWC_ALLOC_ATOMIC(size);
3101 dwc_otg_urb = DWC_ALLOC(size);
3103 dwc_otg_urb->packet_count = iso_desc_count;
3108 void dwc_otg_hcd_urb_set_pipeinfo(dwc_otg_hcd_urb_t *dwc_otg_urb,
3109 uint8_t dev_addr, uint8_t ep_num,
3110 uint8_t ep_type, uint8_t ep_dir, uint16_t mps)
3112 dwc_otg_hcd_fill_pipe(&dwc_otg_urb->pipe_info, dev_addr, ep_num,
3113 ep_type, ep_dir, mps);
3116 ("addr = %d, ep_num = %d, ep_dir = 0x%x, ep_type = 0x%x, mps = %d\n",
3117 dev_addr, ep_num, ep_dir, ep_type, mps);
3121 void dwc_otg_hcd_urb_set_params(dwc_otg_hcd_urb_t *dwc_otg_urb,
3122 void *urb_handle, void *buf, dwc_dma_t dma,
3123 uint32_t buflen, void *setup_packet,
3124 dwc_dma_t setup_dma, uint32_t flags,
3127 dwc_otg_urb->priv = urb_handle;
3128 dwc_otg_urb->buf = buf;
3129 dwc_otg_urb->dma = dma;
3130 dwc_otg_urb->length = buflen;
3131 dwc_otg_urb->setup_packet = setup_packet;
3132 dwc_otg_urb->setup_dma = setup_dma;
3133 dwc_otg_urb->flags = flags;
3134 dwc_otg_urb->interval = interval;
3135 dwc_otg_urb->status = -DWC_E_IN_PROGRESS;
3138 uint32_t dwc_otg_hcd_urb_get_status(dwc_otg_hcd_urb_t *dwc_otg_urb)
3140 return dwc_otg_urb->status;
3143 uint32_t dwc_otg_hcd_urb_get_actual_length(dwc_otg_hcd_urb_t *dwc_otg_urb)
3145 return dwc_otg_urb->actual_length;
3148 uint32_t dwc_otg_hcd_urb_get_error_count(dwc_otg_hcd_urb_t *dwc_otg_urb)
3150 return dwc_otg_urb->error_count;
3153 void dwc_otg_hcd_urb_set_iso_desc_params(dwc_otg_hcd_urb_t *dwc_otg_urb,
3154 int desc_num, uint32_t offset,
3157 dwc_otg_urb->iso_descs[desc_num].offset = offset;
3158 dwc_otg_urb->iso_descs[desc_num].length = length;
3161 uint32_t dwc_otg_hcd_urb_get_iso_desc_status(dwc_otg_hcd_urb_t *dwc_otg_urb,
3164 return dwc_otg_urb->iso_descs[desc_num].status;
3167 uint32_t dwc_otg_hcd_urb_get_iso_desc_actual_length(dwc_otg_hcd_urb_t *
3168 dwc_otg_urb, int desc_num)
3170 return dwc_otg_urb->iso_descs[desc_num].actual_length;
3173 int dwc_otg_hcd_is_bandwidth_allocated(dwc_otg_hcd_t *hcd, void *ep_handle)
3176 dwc_otg_qh_t *qh = (dwc_otg_qh_t *) ep_handle;
3179 if (!DWC_LIST_EMPTY(&qh->qh_list_entry)) {
3186 int dwc_otg_hcd_is_bandwidth_freed(dwc_otg_hcd_t *hcd, void *ep_handle)
3188 dwc_otg_qh_t *qh = (dwc_otg_qh_t *) ep_handle;
3190 DWC_ASSERT(qh, "qh is not allocated\n");
3192 if (DWC_LIST_EMPTY(&qh->qh_list_entry)) {
3199 uint8_t dwc_otg_hcd_get_ep_bandwidth(dwc_otg_hcd_t *hcd, void *ep_handle)
3201 dwc_otg_qh_t *qh = (dwc_otg_qh_t *) ep_handle;
3202 DWC_ASSERT(qh, "qh is not allocated\n");
3206 void dwc_otg_hcd_dump_state(dwc_otg_hcd_t *hcd)
3211 gnptxsts_data_t np_tx_status;
3212 hptxsts_data_t p_tx_status;
3214 num_channels = hcd->core_if->core_params->host_channels;
3217 ("************************************************************\n");
3218 DWC_PRINTF("HCD State:\n");
3219 DWC_PRINTF(" Num channels: %d\n", num_channels);
3220 for (i = 0; i < num_channels; i++) {
3221 dwc_hc_t *hc = hcd->hc_ptr_array[i];
3222 DWC_PRINTF(" Channel %d:\n", i);
3223 DWC_PRINTF(" dev_addr: %d, ep_num: %d, ep_is_in: %d\n",
3224 hc->dev_addr, hc->ep_num, hc->ep_is_in);
3225 DWC_PRINTF(" speed: %d\n", hc->speed);
3226 DWC_PRINTF(" ep_type: %d\n", hc->ep_type);
3227 DWC_PRINTF(" max_packet: %d\n", hc->max_packet);
3228 DWC_PRINTF(" data_pid_start: %d\n", hc->data_pid_start);
3229 DWC_PRINTF(" multi_count: %d\n", hc->multi_count);
3230 DWC_PRINTF(" xfer_started: %d\n", hc->xfer_started);
3231 DWC_PRINTF(" xfer_buff: %p\n", hc->xfer_buff);
3232 DWC_PRINTF(" xfer_len: %d\n", hc->xfer_len);
3233 DWC_PRINTF(" xfer_count: %d\n", hc->xfer_count);
3234 DWC_PRINTF(" halt_on_queue: %d\n", hc->halt_on_queue);
3235 DWC_PRINTF(" halt_pending: %d\n", hc->halt_pending);
3236 DWC_PRINTF(" halt_status: %d\n", hc->halt_status);
3237 DWC_PRINTF(" do_split: %d\n", hc->do_split);
3238 DWC_PRINTF(" complete_split: %d\n", hc->complete_split);
3239 DWC_PRINTF(" hub_addr: %d\n", hc->hub_addr);
3240 DWC_PRINTF(" port_addr: %d\n", hc->port_addr);
3241 DWC_PRINTF(" xact_pos: %d\n", hc->xact_pos);
3242 DWC_PRINTF(" requests: %d\n", hc->requests);
3243 DWC_PRINTF(" qh: %p\n", hc->qh);
3244 if (hc->xfer_started) {
3246 hcchar_data_t hcchar;
3247 hctsiz_data_t hctsiz;
3249 hcintmsk_data_t hcintmsk;
3251 DWC_READ_REG32(&hcd->core_if->host_if->
3252 host_global_regs->hfnum);
3254 DWC_READ_REG32(&hcd->core_if->host_if->hc_regs[i]->
3257 DWC_READ_REG32(&hcd->core_if->host_if->hc_regs[i]->
3260 DWC_READ_REG32(&hcd->core_if->host_if->hc_regs[i]->
3263 DWC_READ_REG32(&hcd->core_if->host_if->hc_regs[i]->
3265 DWC_PRINTF(" hfnum: 0x%08x\n", hfnum.d32);
3266 DWC_PRINTF(" hcchar: 0x%08x\n", hcchar.d32);
3267 DWC_PRINTF(" hctsiz: 0x%08x\n", hctsiz.d32);
3268 DWC_PRINTF(" hcint: 0x%08x\n", hcint.d32);
3269 DWC_PRINTF(" hcintmsk: 0x%08x\n", hcintmsk.d32);
3271 if (hc->xfer_started && hc->qh) {
3273 dwc_otg_hcd_urb_t *urb;
3275 DWC_CIRCLEQ_FOREACH(qtd, &hc->qh->qtd_list,
3277 if (!qtd->in_process)
3281 DWC_PRINTF(" URB Info:\n");
3282 DWC_PRINTF(" qtd: %p, urb: %p\n", qtd,
3285 DWC_PRINTF(" Dev: %d, EP: %d %s\n",
3286 dwc_otg_hcd_get_dev_addr
3288 dwc_otg_hcd_get_ep_num
3290 dwc_otg_hcd_is_pipe_in
3291 (&urb->pipe_info) ? "IN" :
3294 (" Max packet size: %d\n",
3298 (" transfer_buffer: %p\n",
3300 DWC_PRINTF(" transfer_dma: %p\n",
3303 (" transfer_buffer_length: %d\n",
3305 DWC_PRINTF(" actual_length: %d\n",
3306 urb->actual_length);
3311 DWC_PRINTF(" non_periodic_channels: %d\n", hcd->non_periodic_channels);
3312 DWC_PRINTF(" periodic_channels: %d\n", hcd->periodic_channels);
3313 DWC_PRINTF(" periodic_usecs: %d\n", hcd->periodic_usecs);
3315 DWC_READ_REG32(&hcd->core_if->core_global_regs->gnptxsts);
3316 DWC_PRINTF(" NP Tx Req Queue Space Avail: %d\n",
3317 np_tx_status.b.nptxqspcavail);
3318 DWC_PRINTF(" NP Tx FIFO Space Avail: %d\n",
3319 np_tx_status.b.nptxfspcavail);
3321 DWC_READ_REG32(&hcd->core_if->host_if->host_global_regs->hptxsts);
3322 DWC_PRINTF(" P Tx Req Queue Space Avail: %d\n",
3323 p_tx_status.b.ptxqspcavail);
3324 DWC_PRINTF(" P Tx FIFO Space Avail: %d\n", p_tx_status.b.ptxfspcavail);
3325 dwc_otg_hcd_dump_frrem(hcd);
3326 dwc_otg_dump_global_registers(hcd->core_if);
3327 dwc_otg_dump_host_registers(hcd->core_if);
3329 ("************************************************************\n");
3335 void dwc_print_setup_data(uint8_t *setup)
3338 if (CHK_DEBUG_LEVEL(DBG_HCD)) {
3339 DWC_PRINTF("Setup Data = MSB ");
3340 for (i = 7; i >= 0; i--)
3341 DWC_PRINTF("%02x ", setup[i]);
3343 DWC_PRINTF(" bmRequestType Tranfer = %s\n",
3344 (setup[0] & 0x80) ? "Device-to-Host" :
3346 DWC_PRINTF(" bmRequestType Type = ");
3347 switch ((setup[0] & 0x60) >> 5) {
3349 DWC_PRINTF("Standard\n");
3352 DWC_PRINTF("Class\n");
3355 DWC_PRINTF("Vendor\n");
3358 DWC_PRINTF("Reserved\n");
3361 DWC_PRINTF(" bmRequestType Recipient = ");
3362 switch (setup[0] & 0x1f) {
3364 DWC_PRINTF("Device\n");
3367 DWC_PRINTF("Interface\n");
3370 DWC_PRINTF("Endpoint\n");
3373 DWC_PRINTF("Other\n");
3376 DWC_PRINTF("Reserved\n");
3379 DWC_PRINTF(" bRequest = 0x%0x\n", setup[1]);
3380 DWC_PRINTF(" wValue = 0x%0x\n", *((uint16_t *)&setup[2]));
3381 DWC_PRINTF(" wIndex = 0x%0x\n", *((uint16_t *)&setup[4]));
3382 DWC_PRINTF(" wLength = 0x%0x\n\n", *((uint16_t *)&setup[6]));
3387 void dwc_otg_hcd_dump_frrem(dwc_otg_hcd_t *hcd)
3390 DWC_PRINTF("Frame remaining at SOF:\n");
3391 DWC_PRINTF(" samples %u, accum %llu, avg %llu\n",
3392 hcd->frrem_samples, hcd->frrem_accum,
3393 (hcd->frrem_samples > 0) ?
3394 hcd->frrem_accum / hcd->frrem_samples : 0);
3397 DWC_PRINTF("Frame remaining at start_transfer (uframe 7):\n");
3398 DWC_PRINTF(" samples %u, accum %llu, avg %llu\n",
3399 hcd->core_if->hfnum_7_samples,
3400 hcd->core_if->hfnum_7_frrem_accum,
3401 (hcd->core_if->hfnum_7_samples >
3402 0) ? hcd->core_if->hfnum_7_frrem_accum /
3403 hcd->core_if->hfnum_7_samples : 0);
3404 DWC_PRINTF("Frame remaining at start_transfer (uframe 0):\n");
3405 DWC_PRINTF(" samples %u, accum %llu, avg %llu\n",
3406 hcd->core_if->hfnum_0_samples,
3407 hcd->core_if->hfnum_0_frrem_accum,
3408 (hcd->core_if->hfnum_0_samples >
3409 0) ? hcd->core_if->hfnum_0_frrem_accum /
3410 hcd->core_if->hfnum_0_samples : 0);
3411 DWC_PRINTF("Frame remaining at start_transfer (uframe 1-6):\n");
3412 DWC_PRINTF(" samples %u, accum %llu, avg %llu\n",
3413 hcd->core_if->hfnum_other_samples,
3414 hcd->core_if->hfnum_other_frrem_accum,
3415 (hcd->core_if->hfnum_other_samples >
3416 0) ? hcd->core_if->hfnum_other_frrem_accum /
3417 hcd->core_if->hfnum_other_samples : 0);
3420 DWC_PRINTF("Frame remaining at sample point A (uframe 7):\n");
3421 DWC_PRINTF(" samples %u, accum %llu, avg %llu\n",
3422 hcd->hfnum_7_samples_a, hcd->hfnum_7_frrem_accum_a,
3423 (hcd->hfnum_7_samples_a > 0) ?
3424 hcd->hfnum_7_frrem_accum_a / hcd->hfnum_7_samples_a : 0);
3425 DWC_PRINTF("Frame remaining at sample point A (uframe 0):\n");
3426 DWC_PRINTF(" samples %u, accum %llu, avg %llu\n",
3427 hcd->hfnum_0_samples_a, hcd->hfnum_0_frrem_accum_a,
3428 (hcd->hfnum_0_samples_a > 0) ?
3429 hcd->hfnum_0_frrem_accum_a / hcd->hfnum_0_samples_a : 0);
3430 DWC_PRINTF("Frame remaining at sample point A (uframe 1-6):\n");
3431 DWC_PRINTF(" samples %u, accum %llu, avg %llu\n",
3432 hcd->hfnum_other_samples_a, hcd->hfnum_other_frrem_accum_a,
3433 (hcd->hfnum_other_samples_a > 0) ?
3434 hcd->hfnum_other_frrem_accum_a /
3435 hcd->hfnum_other_samples_a : 0);
3438 DWC_PRINTF("Frame remaining at sample point B (uframe 7):\n");
3439 DWC_PRINTF(" samples %u, accum %llu, avg %llu\n",
3440 hcd->hfnum_7_samples_b, hcd->hfnum_7_frrem_accum_b,
3441 (hcd->hfnum_7_samples_b > 0) ?
3442 hcd->hfnum_7_frrem_accum_b / hcd->hfnum_7_samples_b : 0);
3443 DWC_PRINTF("Frame remaining at sample point B (uframe 0):\n");
3444 DWC_PRINTF(" samples %u, accum %llu, avg %llu\n",
3445 hcd->hfnum_0_samples_b, hcd->hfnum_0_frrem_accum_b,
3446 (hcd->hfnum_0_samples_b > 0) ?
3447 hcd->hfnum_0_frrem_accum_b / hcd->hfnum_0_samples_b : 0);
3448 DWC_PRINTF("Frame remaining at sample point B (uframe 1-6):\n");
3449 DWC_PRINTF(" samples %u, accum %llu, avg %llu\n",
3450 hcd->hfnum_other_samples_b, hcd->hfnum_other_frrem_accum_b,
3451 (hcd->hfnum_other_samples_b > 0) ?
3452 hcd->hfnum_other_frrem_accum_b /
3453 hcd->hfnum_other_samples_b : 0);
3457 #endif /* DWC_DEVICE_ONLY */