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);
191 * Responds with an error status of ETIMEDOUT to all URBs in the non-periodic
192 * and periodic schedules. The QTD associated with each URB is removed from
193 * the schedule and freed. This function may be called when a disconnect is
194 * detected or when the HCD is being stopped.
196 static void kill_all_urbs(dwc_otg_hcd_t *hcd)
198 kill_urbs_in_qh_list(hcd, &hcd->non_periodic_sched_inactive);
199 kill_urbs_in_qh_list(hcd, &hcd->non_periodic_sched_active);
200 kill_urbs_in_qh_list(hcd, &hcd->periodic_sched_inactive);
201 kill_urbs_in_qh_list(hcd, &hcd->periodic_sched_ready);
202 kill_urbs_in_qh_list(hcd, &hcd->periodic_sched_assigned);
203 kill_urbs_in_qh_list(hcd, &hcd->periodic_sched_queued);
207 * Start the connection timer. An OTG host is required to display a
208 * message if the device does not connect within 10 seconds. The
209 * timer is deleted if a port connect interrupt occurs before the
212 static void dwc_otg_hcd_start_connect_timer(dwc_otg_hcd_t *hcd)
214 DWC_TIMER_SCHEDULE(hcd->conn_timer, 10000 /* 10 secs */);
218 * HCD Callback function for disconnect of the HCD.
220 * @param p void pointer to the <code>struct usb_hcd</code>
222 static int32_t dwc_otg_hcd_session_start_cb(void *p)
224 dwc_otg_hcd_t *dwc_otg_hcd;
225 DWC_DEBUGPL(DBG_HCDV, "%s(%p)\n", __func__, p);
227 dwc_otg_hcd_start_connect_timer(dwc_otg_hcd);
232 * HCD Callback function for starting the HCD when A-Cable is
235 * @param p void pointer to the <code>struct usb_hcd</code>
237 static int32_t dwc_otg_hcd_start_cb(void *p)
239 dwc_otg_hcd_t *dwc_otg_hcd = p;
240 dwc_otg_core_if_t *core_if;
242 uint32_t timeout = 50;
244 core_if = dwc_otg_hcd->core_if;
246 if (core_if->op_state == B_HOST) {
248 * Reset the port. During a HNP mode switch the reset
249 * needs to occur within 1ms and have a duration of at
252 hprt0.d32 = dwc_otg_read_hprt0(core_if);
254 DWC_WRITE_REG32(core_if->host_if->hprt0, hprt0.d32);
255 if (core_if->otg_ver) {
257 hprt0.d32 = dwc_otg_read_hprt0(core_if);
259 DWC_WRITE_REG32(core_if->host_if->hprt0, hprt0.d32);
262 /**@todo vahrama: Check the timeout value for OTG 2.0 */
263 if (core_if->otg_ver)
265 DWC_WORKQ_SCHEDULE_DELAYED(core_if->wq_otg,
266 hcd_start_func, dwc_otg_hcd, timeout,
273 * HCD Callback function for disconnect of the HCD.
275 * @param p void pointer to the <code>struct usb_hcd</code>
277 static int32_t dwc_otg_hcd_disconnect_cb(void *p)
280 dwc_otg_hcd_t *dwc_otg_hcd = p;
283 dwc_otg_hcd->non_periodic_qh_ptr = &dwc_otg_hcd->non_periodic_sched_active;
284 dwc_otg_hcd->non_periodic_channels = 0;
285 dwc_otg_hcd->periodic_channels = 0;
286 dwc_otg_hcd->frame_number =0;
288 hprt0.d32 = DWC_READ_REG32(dwc_otg_hcd->core_if->host_if->hprt0);
289 /* In some case, we don't disconnect a usb device, but
290 * disconnect intr was triggered, so check hprt0 here. */
291 if ((!hprt0.b.prtenchng)
292 && (!hprt0.b.prtconndet)
293 && hprt0.b.prtconnsts) {
294 DWC_PRINTF("%s: hprt0 = 0x%08x\n", __func__, hprt0.d32);
298 * Set status flags for the hub driver.
300 dwc_otg_hcd->flags.b.port_connect_status_change = 1;
301 dwc_otg_hcd->flags.b.port_connect_status = 0;
304 * Shutdown any transfers in process by clearing the Tx FIFO Empty
305 * interrupt mask and status bits and disabling subsequent host
306 * channel interrupts.
309 intr.b.nptxfempty = 1;
310 intr.b.ptxfempty = 1;
312 DWC_MODIFY_REG32(&dwc_otg_hcd->core_if->core_global_regs->gintmsk,
314 DWC_MODIFY_REG32(&dwc_otg_hcd->core_if->core_global_regs->gintsts,
318 * Turn off the vbus power only if the core has transitioned to device
319 * mode. If still in host mode, need to keep power on to detect a
322 if (dwc_otg_is_device_mode(dwc_otg_hcd->core_if)) {
323 if (dwc_otg_hcd->core_if->op_state != A_SUSPEND) {
324 hprt0_data_t hprt0 = {.d32 = 0 };
325 DWC_PRINTF("Disconnect: PortPower off\n");
327 DWC_WRITE_REG32(dwc_otg_hcd->core_if->host_if->hprt0,
330 /** Delete timers if become device */
331 del_timers(dwc_otg_hcd);
332 dwc_otg_disable_host_interrupts(dwc_otg_hcd->core_if);
336 /* Respond with an error status to all URBs in the schedule. */
337 kill_all_urbs(dwc_otg_hcd);
339 if (dwc_otg_is_host_mode(dwc_otg_hcd->core_if)) {
340 /* Clean up any host channels that were in use. */
344 dwc_otg_hc_regs_t *hc_regs;
345 hcchar_data_t hcchar;
347 DWC_PRINTF("Disconnect cb-Host\n");
348 if (dwc_otg_hcd->core_if->otg_ver == 1)
349 del_xfer_timers(dwc_otg_hcd);
351 del_timers(dwc_otg_hcd);
353 num_channels = dwc_otg_hcd->core_if->core_params->host_channels;
355 if (!dwc_otg_hcd->core_if->dma_enable) {
356 /* Flush out any channel requests in slave mode. */
357 for (i = 0; i < num_channels; i++) {
358 channel = dwc_otg_hcd->hc_ptr_array[i];
359 if (DWC_CIRCLEQ_EMPTY_ENTRY
360 (channel, hc_list_entry)) {
362 dwc_otg_hcd->core_if->host_if->
365 DWC_READ_REG32(&hc_regs->hcchar);
378 for (i = 0; i < num_channels; i++) {
379 channel = dwc_otg_hcd->hc_ptr_array[i];
380 if (DWC_CIRCLEQ_EMPTY_ENTRY(channel, hc_list_entry)) {
382 dwc_otg_hcd->core_if->host_if->hc_regs[i];
383 hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
385 /* Halt the channel. */
387 DWC_WRITE_REG32(&hc_regs->hcchar,
391 dwc_otg_hc_cleanup(dwc_otg_hcd->core_if,
393 DWC_CIRCLEQ_INSERT_TAIL
394 (&dwc_otg_hcd->free_hc_list, channel,
397 * Added for Descriptor DMA to prevent channel double cleanup
398 * in release_channel_ddma(). Which called from ep_disable
399 * when device disconnect.
407 if (dwc_otg_hcd->fops->disconnect) {
408 dwc_otg_hcd->fops->disconnect(dwc_otg_hcd);
415 * HCD Callback function for stopping the HCD.
417 * @param p void pointer to the <code>struct usb_hcd</code>
419 static int32_t dwc_otg_hcd_stop_cb(void *p)
421 dwc_otg_hcd_t *dwc_otg_hcd = p;
423 DWC_DEBUGPL(DBG_HCDV, "%s(%p)\n", __func__, p);
424 dwc_otg_hcd_stop(dwc_otg_hcd);
428 #ifdef CONFIG_USB_DWC_OTG_LPM
430 * HCD Callback function for sleep of HCD.
432 * @param p void pointer to the <code>struct usb_hcd</code>
434 static int dwc_otg_hcd_sleep_cb(void *p)
436 dwc_otg_hcd_t *hcd = p;
438 dwc_otg_hcd_free_hc_from_lpm(hcd);
445 * HCD Callback function for Remote Wakeup.
447 * @param p void pointer to the <code>struct usb_hcd</code>
449 static int dwc_otg_hcd_rem_wakeup_cb(void *p)
451 dwc_otg_hcd_t *dwc_otg_hcd = p;
452 struct usb_hcd *hcd = dwc_otg_hcd_get_priv_data(dwc_otg_hcd);
454 if (dwc_otg_hcd->core_if->lx_state == DWC_OTG_L2) {
455 dwc_otg_hcd->flags.b.port_suspend_change = 1;
456 usb_hcd_resume_root_hub(hcd);
458 #ifdef CONFIG_USB_DWC_OTG_LPM
460 dwc_otg_hcd->flags.b.port_l1_change = 1;
467 * Halts the DWC_otg host mode operations in a clean manner. USB transfers are
470 void dwc_otg_hcd_stop(dwc_otg_hcd_t *hcd)
472 hprt0_data_t hprt0 = {.d32 = 0 };
473 struct dwc_otg_platform_data *pldata;
474 dwc_irqflags_t flags;
476 pldata = hcd->core_if->otg_dev->pldata;
477 DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD STOP\n");
480 * The root hub should be disconnected before this function is called.
481 * The disconnect will clear the QTD lists (via ..._hcd_urb_dequeue)
482 * and the QH lists (via ..._hcd_endpoint_disable).
484 DWC_SPINLOCK_IRQSAVE(hcd->lock, &flags);
486 DWC_SPINUNLOCK_IRQRESTORE(hcd->lock, flags);
489 * Set status flags for the hub driver.
491 hcd->flags.b.port_connect_status_change = 1;
492 hcd->flags.b.port_connect_status = 0;
494 /* Turn off all host-specific interrupts. */
495 dwc_otg_disable_host_interrupts(hcd->core_if);
497 /* Turn off the vbus power */
498 DWC_PRINTF("PortPower off\n");
500 DWC_WRITE_REG32(hcd->core_if->host_if->hprt0, hprt0.d32);
502 if (pldata->power_enable)
503 pldata->power_enable(0);
508 int dwc_otg_hcd_urb_enqueue(dwc_otg_hcd_t *hcd,
509 dwc_otg_hcd_urb_t *dwc_otg_urb, void **ep_handle,
512 dwc_irqflags_t flags;
515 gintmsk_data_t intr_mask = {.d32 = 0 };
517 if (!hcd->flags.b.port_connect_status) {
518 /* No longer connected. */
519 DWC_DEBUG("Not connected\n");
520 return -DWC_E_NO_DEVICE;
523 qtd = dwc_otg_hcd_qtd_create(dwc_otg_urb, atomic_alloc);
525 DWC_ERROR("DWC OTG HCD URB Enqueue failed creating QTD\n");
526 return -DWC_E_NO_MEMORY;
528 DWC_SPINLOCK_IRQSAVE(hcd->lock, &flags);
530 dwc_otg_hcd_qtd_add(qtd, hcd, (dwc_otg_qh_t **) ep_handle, 1);
533 DWC_ERROR("DWC OTG HCD URB Enqueue failed adding QTD. "
534 "Error status %d\n", retval);
535 dwc_otg_hcd_qtd_free(qtd);
538 DWC_READ_REG32(&hcd->core_if->core_global_regs->gintmsk);
539 if (!intr_mask.b.sofintr && retval == 0) {
540 dwc_otg_transaction_type_e tr_type;
541 if ((qtd->qh->ep_type == UE_BULK)
542 && !(qtd->urb->flags & URB_GIVEBACK_ASAP)) {
543 /* Do not schedule SG transactions until qtd has URB_GIVEBACK_ASAP set */
547 tr_type = dwc_otg_hcd_select_transactions(hcd);
548 if (tr_type != DWC_OTG_TRANSACTION_NONE) {
549 dwc_otg_hcd_queue_transactions(hcd, tr_type);
553 DWC_SPINUNLOCK_IRQRESTORE(hcd->lock, flags);
557 int dwc_otg_hcd_urb_dequeue(dwc_otg_hcd_t *hcd,
558 dwc_otg_hcd_urb_t *dwc_otg_urb)
561 dwc_otg_qtd_t *urb_qtd;
563 urb_qtd = dwc_otg_urb->qtd;
564 if (((uint32_t) urb_qtd & 0xf0000000) == 0) {
565 DWC_PRINTF("%s error: urb_qtd is %p dwc_otg_urb %p!!!\n",
566 __func__, urb_qtd, dwc_otg_urb);
571 if (CHK_DEBUG_LEVEL(DBG_HCDV | DBG_HCD_URB)) {
572 if (urb_qtd->in_process) {
573 dump_channel_info(hcd, qh);
577 if (urb_qtd->in_process && qh->channel) {
578 /* The QTD is in process (it has been assigned to a channel). */
579 if (hcd->flags.b.port_connect_status) {
581 * If still connected (i.e. in host mode), halt the
582 * channel so it can be used for other transfers. If
583 * no longer connected, the host registers can't be
584 * written to halt the channel since the core is in
587 dwc_otg_hc_halt(hcd->core_if, qh->channel,
588 DWC_OTG_HC_XFER_URB_DEQUEUE);
593 * Free the QTD and clean up the associated QH. Leave the QH in the
594 * schedule if it has any remaining QTDs.
597 if (!hcd->core_if->dma_desc_enable) {
598 uint8_t b = urb_qtd->in_process;
599 dwc_otg_hcd_qtd_remove_and_free(hcd, urb_qtd, qh);
601 dwc_otg_hcd_qh_deactivate(hcd, qh, 0);
603 } else if (DWC_CIRCLEQ_EMPTY(&qh->qtd_list)) {
604 dwc_otg_hcd_qh_remove(hcd, qh);
607 dwc_otg_hcd_qtd_remove_and_free(hcd, urb_qtd, qh);
612 int dwc_otg_hcd_endpoint_disable(dwc_otg_hcd_t *hcd, void *ep_handle,
615 dwc_otg_qh_t *qh = (dwc_otg_qh_t *) ep_handle;
617 dwc_irqflags_t flags;
620 retval = -DWC_E_INVALID;
625 retval = -DWC_E_INVALID;
629 DWC_SPINLOCK_IRQSAVE(hcd->lock, &flags);
631 while (!DWC_CIRCLEQ_EMPTY(&qh->qtd_list) && retry) {
632 DWC_SPINUNLOCK_IRQRESTORE(hcd->lock, flags);
635 DWC_SPINLOCK_IRQSAVE(hcd->lock, &flags);
638 dwc_otg_hcd_qh_remove(hcd, qh);
640 DWC_SPINUNLOCK_IRQRESTORE(hcd->lock, flags);
642 * Split dwc_otg_hcd_qh_remove_and_free() into qh_remove
643 * and qh_free to prevent stack dump on DWC_DMA_FREE() with
644 * irq_disabled (spinlock_irqsave) in dwc_otg_hcd_desc_list_free()
645 * and dwc_otg_hcd_frame_list_alloc().
647 dwc_otg_hcd_qh_free(hcd, qh);
653 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 30)
654 int dwc_otg_hcd_endpoint_reset(dwc_otg_hcd_t *hcd, void *ep_handle)
657 dwc_otg_qh_t *qh = (dwc_otg_qh_t *) ep_handle;
659 return -DWC_E_INVALID;
661 qh->data_toggle = DWC_OTG_HC_PID_DATA0;
667 * HCD Callback structure for handling mode switching.
669 static dwc_otg_cil_callbacks_t hcd_cil_callbacks = {
670 .start = dwc_otg_hcd_start_cb,
671 .stop = dwc_otg_hcd_stop_cb,
672 .disconnect = dwc_otg_hcd_disconnect_cb,
673 .session_start = dwc_otg_hcd_session_start_cb,
674 .resume_wakeup = dwc_otg_hcd_rem_wakeup_cb,
675 #ifdef CONFIG_USB_DWC_OTG_LPM
676 .sleep = dwc_otg_hcd_sleep_cb,
682 * Reset tasklet function
684 static void reset_tasklet_func(void *data)
686 dwc_otg_hcd_t *dwc_otg_hcd = (dwc_otg_hcd_t *) data;
687 dwc_otg_core_if_t *core_if = dwc_otg_hcd->core_if;
690 DWC_DEBUGPL(DBG_HCDV, "USB RESET tasklet called\n");
692 hprt0.d32 = dwc_otg_read_hprt0(core_if);
694 DWC_WRITE_REG32(core_if->host_if->hprt0, hprt0.d32);
698 DWC_WRITE_REG32(core_if->host_if->hprt0, hprt0.d32);
699 dwc_otg_hcd->flags.b.port_reset_change = 1;
702 static void qh_list_free(dwc_otg_hcd_t *hcd, dwc_list_link_t *qh_list)
704 dwc_list_link_t *item;
706 dwc_irqflags_t flags;
708 if (!qh_list->next) {
709 /* The list hasn't been initialized yet. */
713 * Hold spinlock here. Not needed in that case if bellow
714 * function is being called from ISR
716 DWC_SPINLOCK_IRQSAVE(hcd->lock, &flags);
717 /* Ensure there are no QTDs or URBs left. */
718 kill_urbs_in_qh_list(hcd, qh_list);
719 DWC_SPINUNLOCK_IRQRESTORE(hcd->lock, flags);
721 DWC_LIST_FOREACH(item, qh_list) {
722 qh = DWC_LIST_ENTRY(item, dwc_otg_qh_t, qh_list_entry);
723 dwc_otg_hcd_qh_remove_and_free(hcd, qh);
728 * Exit from Hibernation if Host did not detect SRP from connected SRP capable
729 * Device during SRP time by host power up.
731 void dwc_otg_hcd_power_up(void *ptr)
733 gpwrdn_data_t gpwrdn = {.d32 = 0 };
734 dwc_otg_core_if_t *core_if = (dwc_otg_core_if_t *) ptr;
736 DWC_PRINTF("%s called\n", __FUNCTION__);
738 if (!core_if->hibernation_suspend) {
739 DWC_PRINTF("Already exited from Hibernation\n");
743 /* Switch on the voltage to the core */
744 gpwrdn.b.pwrdnswtch = 1;
745 DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
750 gpwrdn.b.pwrdnrstn = 1;
751 DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
754 /* Disable power clamps */
756 gpwrdn.b.pwrdnclmp = 1;
757 DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
759 /* Remove reset the core signal */
761 gpwrdn.b.pwrdnrstn = 1;
762 DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, 0, gpwrdn.d32);
765 /* Disable PMU interrupt */
767 gpwrdn.b.pmuintsel = 1;
768 DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
770 core_if->hibernation_suspend = 0;
774 gpwrdn.b.pmuactv = 1;
775 DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
780 gpwrdn.b.dis_vbus = 1;
781 DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
783 core_if->op_state = A_HOST;
784 dwc_otg_core_init(core_if);
785 dwc_otg_enable_global_interrupts(core_if);
786 cil_hcd_start(core_if);
790 * Frees secondary storage associated with the dwc_otg_hcd structure contained
791 * in the struct usb_hcd field.
793 static void dwc_otg_hcd_free(dwc_otg_hcd_t *dwc_otg_hcd)
797 DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD FREE\n");
799 del_timers(dwc_otg_hcd);
801 /* Free memory for QH/QTD lists */
802 qh_list_free(dwc_otg_hcd, &dwc_otg_hcd->non_periodic_sched_inactive);
803 qh_list_free(dwc_otg_hcd, &dwc_otg_hcd->non_periodic_sched_active);
804 qh_list_free(dwc_otg_hcd, &dwc_otg_hcd->periodic_sched_inactive);
805 qh_list_free(dwc_otg_hcd, &dwc_otg_hcd->periodic_sched_ready);
806 qh_list_free(dwc_otg_hcd, &dwc_otg_hcd->periodic_sched_assigned);
807 qh_list_free(dwc_otg_hcd, &dwc_otg_hcd->periodic_sched_queued);
809 /* Free memory for the host channels. */
810 for (i = 0; i < MAX_EPS_CHANNELS; i++) {
811 dwc_hc_t *hc = dwc_otg_hcd->hc_ptr_array[i];
814 if (dwc_otg_hcd->core_if->hc_xfer_timer[i]) {
815 DWC_TIMER_FREE(dwc_otg_hcd->core_if->hc_xfer_timer[i]);
819 DWC_DEBUGPL(DBG_HCDV, "HCD Free channel #%i, hc=%p\n",
825 if (dwc_otg_hcd->core_if->dma_enable) {
826 if (dwc_otg_hcd->status_buf_dma) {
827 DWC_DMA_FREE(DWC_OTG_HCD_STATUS_BUF_SIZE,
828 dwc_otg_hcd->status_buf,
829 dwc_otg_hcd->status_buf_dma);
831 } else if (dwc_otg_hcd->status_buf != NULL) {
832 DWC_FREE(dwc_otg_hcd->status_buf);
834 DWC_SPINLOCK_FREE(dwc_otg_hcd->lock);
835 /* Set core_if's lock pointer to NULL */
836 dwc_otg_hcd->core_if->lock = NULL;
838 DWC_TIMER_FREE(dwc_otg_hcd->conn_timer);
839 DWC_TASK_FREE(dwc_otg_hcd->reset_tasklet);
841 #ifdef DWC_DEV_SRPCAP
842 if (dwc_otg_hcd->core_if->power_down == 2 &&
843 dwc_otg_hcd->core_if->pwron_timer) {
844 DWC_TIMER_FREE(dwc_otg_hcd->core_if->pwron_timer);
847 DWC_FREE(dwc_otg_hcd);
850 int dwc_otg_hcd_init(dwc_otg_hcd_t *hcd, dwc_otg_core_if_t *core_if)
857 hcd->lock = DWC_SPINLOCK_ALLOC();
859 DWC_ERROR("Could not allocate lock for pcd");
861 retval = -DWC_E_NO_MEMORY;
864 hcd->core_if = core_if;
866 /* Register the HCD CIL Callbacks */
867 dwc_otg_cil_register_hcd_callbacks(hcd->core_if,
868 &hcd_cil_callbacks, hcd);
870 /* Initialize the non-periodic schedule. */
871 DWC_LIST_INIT(&hcd->non_periodic_sched_inactive);
872 DWC_LIST_INIT(&hcd->non_periodic_sched_active);
874 /* Initialize the periodic schedule. */
875 DWC_LIST_INIT(&hcd->periodic_sched_inactive);
876 DWC_LIST_INIT(&hcd->periodic_sched_ready);
877 DWC_LIST_INIT(&hcd->periodic_sched_assigned);
878 DWC_LIST_INIT(&hcd->periodic_sched_queued);
881 * Create a host channel descriptor for each host channel implemented
882 * in the controller. Initialize the channel descriptor array.
884 DWC_CIRCLEQ_INIT(&hcd->free_hc_list);
885 num_channels = hcd->core_if->core_params->host_channels;
886 DWC_MEMSET(hcd->hc_ptr_array, 0, sizeof(hcd->hc_ptr_array));
887 for (i = 0; i < num_channels; i++) {
888 channel = DWC_ALLOC(sizeof(dwc_hc_t));
889 if (channel == NULL) {
890 retval = -DWC_E_NO_MEMORY;
891 DWC_ERROR("%s: host channel allocation failed\n",
893 dwc_otg_hcd_free(hcd);
897 hcd->hc_ptr_array[i] = channel;
899 hcd->core_if->hc_xfer_timer[i] =
900 DWC_TIMER_ALLOC("hc timer", hc_xfer_timeout,
901 &hcd->core_if->hc_xfer_info[i]);
903 DWC_DEBUGPL(DBG_HCDV, "HCD Added channel #%d, hc=%p\n", i,
907 /* Initialize the Connection timeout timer. */
908 hcd->conn_timer = DWC_TIMER_ALLOC("Connection timer",
909 dwc_otg_hcd_connect_timeout, hcd);
911 /* Initialize reset tasklet. */
913 DWC_TASK_ALLOC("reset_tasklet", reset_tasklet_func, hcd);
914 #ifdef DWC_DEV_SRPCAP
915 if (hcd->core_if->power_down == 2) {
916 /* Initialize Power on timer for Host power up in case hibernation */
917 hcd->core_if->pwron_timer = DWC_TIMER_ALLOC("PWRON TIMER",
918 dwc_otg_hcd_power_up,
924 * Allocate space for storing data on status transactions. Normally no
925 * data is sent, but this space acts as a bit bucket. This must be
926 * done after usb_add_hcd since that function allocates the DMA buffer
929 if (hcd->core_if->dma_enable) {
931 DWC_DMA_ALLOC_ATOMIC(DWC_OTG_HCD_STATUS_BUF_SIZE,
932 &hcd->status_buf_dma);
934 hcd->status_buf = DWC_ALLOC(DWC_OTG_HCD_STATUS_BUF_SIZE);
936 if (!hcd->status_buf) {
937 retval = -DWC_E_NO_MEMORY;
938 DWC_ERROR("%s: status_buf allocation failed\n", __func__);
939 dwc_otg_hcd_free(hcd);
944 hcd->frame_list = NULL;
945 hcd->frame_list_dma = 0;
946 hcd->periodic_qh_count = 0;
951 void dwc_otg_hcd_remove(dwc_otg_hcd_t *hcd)
953 /* Turn off all host-specific interrupts. */
954 dwc_otg_disable_host_interrupts(hcd->core_if);
956 dwc_otg_hcd_free(hcd);
960 * Initializes dynamic portions of the DWC_otg HCD state.
962 static void dwc_otg_hcd_reinit(dwc_otg_hcd_t *hcd)
967 dwc_hc_t *channel_tmp;
968 dwc_irqflags_t flags;
969 dwc_spinlock_t *temp_lock;
972 hcd->non_periodic_qh_ptr = &hcd->non_periodic_sched_active;
973 hcd->non_periodic_channels = 0;
974 hcd->periodic_channels = 0;
976 DWC_SPINLOCK_IRQSAVE(hcd->lock, &flags);
978 * Put all channels in the free channel list and clean up channel
981 DWC_CIRCLEQ_FOREACH_SAFE(channel, channel_tmp,
982 &hcd->free_hc_list, hc_list_entry) {
983 DWC_CIRCLEQ_REMOVE(&hcd->free_hc_list, channel, hc_list_entry);
986 num_channels = hcd->core_if->core_params->host_channels;
987 for (i = 0; i < num_channels; i++) {
988 channel = hcd->hc_ptr_array[i];
989 DWC_CIRCLEQ_INSERT_TAIL(&hcd->free_hc_list, channel,
991 dwc_otg_hc_cleanup(hcd->core_if, channel);
993 DWC_SPINUNLOCK_IRQRESTORE(hcd->lock, flags);
994 /* Initialize the DWC core for host mode operation. */
995 dwc_otg_core_host_init(hcd->core_if);
997 /* Set core_if's lock pointer to the hcd->lock */
998 /* Should get this lock before modify it */
999 if (hcd->core_if->lock) {
1000 DWC_SPINLOCK_IRQSAVE(hcd->core_if->lock, &flags);
1001 temp_lock = hcd->core_if->lock;
1002 hcd->core_if->lock = hcd->lock;
1003 DWC_SPINUNLOCK_IRQRESTORE(temp_lock, flags);
1005 hcd->core_if->lock = hcd->lock;
1010 * Assigns transactions from a QTD to a free host channel and initializes the
1011 * host channel to perform the transactions. The host channel is removed from
1014 * @param hcd The HCD state structure.
1015 * @param qh Transactions from the first QTD for this QH are selected and
1016 * assigned to a free host channel.
1018 static int assign_and_init_hc(dwc_otg_hcd_t *hcd, dwc_otg_qh_t *qh)
1022 dwc_otg_hcd_urb_t *urb;
1026 DWC_DEBUGPL(DBG_HCDV, "%s(%p,%p)\n", __func__, hcd, qh);
1028 hc = DWC_CIRCLEQ_FIRST(&hcd->free_hc_list);
1030 /* Remove the host channel from the free list. */
1031 DWC_CIRCLEQ_REMOVE_INIT(&hcd->free_hc_list, hc, hc_list_entry);
1033 qtd = DWC_CIRCLEQ_FIRST(&qh->qtd_list);
1037 printk("%s : urb is NULL\n", __func__);
1044 qtd->in_process = 1;
1047 * Use usb_pipedevice to determine device address. This address is
1048 * 0 before the SET_ADDRESS command and the correct address afterward.
1050 hc->dev_addr = dwc_otg_hcd_get_dev_addr(&urb->pipe_info);
1051 hc->ep_num = dwc_otg_hcd_get_ep_num(&urb->pipe_info);
1052 hc->speed = qh->dev_speed;
1053 hc->max_packet = dwc_max_packet(qh->maxp);
1055 hc->xfer_started = 0;
1056 hc->halt_status = DWC_OTG_HC_XFER_NO_HALT_STATUS;
1057 hc->error_state = (qtd->error_count > 0);
1058 hc->halt_on_queue = 0;
1059 hc->halt_pending = 0;
1063 * The following values may be modified in the transfer type section
1064 * below. The xfer_len value may be reduced when the transfer is
1065 * started to accommodate the max widths of the XferSize and PktCnt
1066 * fields in the HCTSIZn register.
1069 hc->ep_is_in = (dwc_otg_hcd_is_pipe_in(&urb->pipe_info) != 0);
1073 hc->do_ping = qh->ping_state;
1075 hc->data_pid_start = qh->data_toggle;
1076 hc->multi_count = 1;
1078 if (hcd->core_if->dma_enable) {
1079 hc->xfer_buff = (uint8_t *) urb->dma + urb->actual_length;
1081 /* For non-dword aligned case */
1082 if (((unsigned long)hc->xfer_buff & 0x3)
1083 && !hcd->core_if->dma_desc_enable) {
1084 ptr = (uint8_t *) urb->buf + urb->actual_length;
1087 hc->xfer_buff = (uint8_t *) urb->buf + urb->actual_length;
1089 hc->xfer_len = urb->length - urb->actual_length;
1093 * Set the split attributes
1098 uint32_t hub_addr, port_addr;
1100 hc->xact_pos = qtd->isoc_split_pos;
1101 hc->complete_split = qtd->complete_split;
1102 hcd->fops->hub_info(hcd, urb->priv, &hub_addr, &port_addr);
1103 hc->hub_addr = (uint8_t) hub_addr;
1104 hc->port_addr = (uint8_t) port_addr;
1107 switch (dwc_otg_hcd_get_pipe_type(&urb->pipe_info)) {
1109 hc->ep_type = DWC_OTG_EP_TYPE_CONTROL;
1110 switch (qtd->control_phase) {
1111 case DWC_OTG_CONTROL_SETUP:
1112 DWC_DEBUGPL(DBG_HCDV, " Control setup transaction\n");
1115 hc->data_pid_start = DWC_OTG_HC_PID_SETUP;
1116 if (hcd->core_if->dma_enable)
1117 hc->xfer_buff = (uint8_t *) urb->setup_dma;
1119 hc->xfer_buff = (uint8_t *) urb->setup_packet;
1124 case DWC_OTG_CONTROL_DATA:
1125 DWC_DEBUGPL(DBG_HCDV, " Control data transaction\n");
1126 hc->data_pid_start = qtd->data_toggle;
1128 case DWC_OTG_CONTROL_STATUS:
1130 * Direction is opposite of data direction or IN if no
1133 DWC_DEBUGPL(DBG_HCDV, " Control status transaction\n");
1134 if (urb->length == 0) {
1138 dwc_otg_hcd_is_pipe_out(&urb->pipe_info);
1143 hc->data_pid_start = DWC_OTG_HC_PID_DATA1;
1146 if (hcd->core_if->dma_enable)
1147 hc->xfer_buff = (uint8_t *) hcd->status_buf_dma;
1149 hc->xfer_buff = (uint8_t *) hcd->status_buf;
1156 hc->ep_type = DWC_OTG_EP_TYPE_BULK;
1159 hc->ep_type = DWC_OTG_EP_TYPE_INTR;
1161 case UE_ISOCHRONOUS:
1163 struct dwc_otg_hcd_iso_packet_desc *frame_desc;
1165 hc->ep_type = DWC_OTG_EP_TYPE_ISOC;
1167 if (hcd->core_if->dma_desc_enable)
1170 frame_desc = &urb->iso_descs[qtd->isoc_frame_index];
1172 frame_desc->status = 0;
1174 if (hcd->core_if->dma_enable) {
1175 hc->xfer_buff = (uint8_t *) urb->dma;
1177 hc->xfer_buff = (uint8_t *) urb->buf;
1180 frame_desc->offset + qtd->isoc_split_offset;
1182 frame_desc->length - qtd->isoc_split_offset;
1184 /* For non-dword aligned buffers */
1185 if (((unsigned long)hc->xfer_buff & 0x3)
1186 && hcd->core_if->dma_enable) {
1188 (uint8_t *) urb->buf + frame_desc->offset +
1189 qtd->isoc_split_offset;
1193 if (hc->xact_pos == DWC_HCSPLIT_XACTPOS_ALL) {
1194 if (hc->xfer_len <= 188) {
1195 hc->xact_pos = DWC_HCSPLIT_XACTPOS_ALL;
1198 DWC_HCSPLIT_XACTPOS_BEGIN;
1204 /* non DWORD-aligned buffer case */
1207 if (hc->ep_type != DWC_OTG_EP_TYPE_ISOC) {
1208 buf_size = hcd->core_if->core_params->max_transfer_size;
1212 if (!qh->dw_align_buf) {
1213 qh->dw_align_buf = DWC_DMA_ALLOC_ATOMIC(buf_size,
1216 if (!qh->dw_align_buf) {
1218 ("%s: Failed to allocate memory to handle "
1219 "non-dword aligned buffer case\n",
1224 if (!hc->ep_is_in) {
1225 dwc_memcpy(qh->dw_align_buf, ptr, hc->xfer_len);
1227 hc->align_buff = qh->dw_align_buf_dma;
1232 if (hc->ep_type == DWC_OTG_EP_TYPE_INTR ||
1233 hc->ep_type == DWC_OTG_EP_TYPE_ISOC) {
1235 * This value may be modified when the transfer is started to
1236 * reflect the actual transfer length.
1238 hc->multi_count = dwc_hb_mult(qh->maxp);
1241 if (hcd->core_if->dma_desc_enable)
1242 hc->desc_list_addr = qh->desc_list_dma;
1244 dwc_otg_hc_init(hcd->core_if, hc);
1250 * This function selects transactions from the HCD transfer schedule and
1251 * assigns them to available host channels. It is called from HCD interrupt
1252 * handler functions.
1254 * @param hcd The HCD state structure.
1256 * @return The types of new transactions that were assigned to host channels.
1258 dwc_otg_transaction_type_e dwc_otg_hcd_select_transactions(dwc_otg_hcd_t *hcd)
1260 dwc_list_link_t *qh_ptr;
1263 dwc_otg_transaction_type_e ret_val = DWC_OTG_TRANSACTION_NONE;
1267 DWC_DEBUGPL(DBG_HCD, " Select Transactions\n");
1270 /* Process entries in the periodic ready list. */
1271 qh_ptr = DWC_LIST_FIRST(&hcd->periodic_sched_ready);
1273 while (qh_ptr != &hcd->periodic_sched_ready &&
1274 !DWC_CIRCLEQ_EMPTY(&hcd->free_hc_list)) {
1276 qh = DWC_LIST_ENTRY(qh_ptr, dwc_otg_qh_t, qh_list_entry);
1277 assign_and_init_hc(hcd, qh);
1280 * Move the QH from the periodic ready schedule to the
1281 * periodic assigned schedule.
1283 qh_ptr = DWC_LIST_NEXT(qh_ptr);
1284 DWC_LIST_MOVE_HEAD(&hcd->periodic_sched_assigned,
1285 &qh->qh_list_entry);
1287 ret_val = DWC_OTG_TRANSACTION_PERIODIC;
1291 * Process entries in the inactive portion of the non-periodic
1292 * schedule. Some free host channels may not be used if they are
1293 * reserved for periodic transfers.
1295 qh_ptr = hcd->non_periodic_sched_inactive.next;
1296 num_channels = hcd->core_if->core_params->host_channels;
1297 while (qh_ptr != &hcd->non_periodic_sched_inactive &&
1298 (hcd->non_periodic_channels <
1299 num_channels - hcd->periodic_channels) &&
1300 !DWC_CIRCLEQ_EMPTY(&hcd->free_hc_list)) {
1302 qh = DWC_LIST_ENTRY(qh_ptr, dwc_otg_qh_t, qh_list_entry);
1304 err = assign_and_init_hc(hcd, qh);
1307 * Move the QH from the non-periodic inactive schedule to the
1308 * non-periodic active schedule.
1310 qh_ptr = DWC_LIST_NEXT(qh_ptr);
1313 DWC_LIST_MOVE_HEAD(&hcd->non_periodic_sched_active,
1314 &qh->qh_list_entry);
1316 if (ret_val == DWC_OTG_TRANSACTION_NONE) {
1317 ret_val = DWC_OTG_TRANSACTION_NON_PERIODIC;
1319 ret_val = DWC_OTG_TRANSACTION_ALL;
1322 hcd->non_periodic_channels++;
1329 * Attempts to queue a single transaction request for a host channel
1330 * associated with either a periodic or non-periodic transfer. This function
1331 * assumes that there is space available in the appropriate request queue. For
1332 * an OUT transfer or SETUP transaction in Slave mode, it checks whether space
1333 * is available in the appropriate Tx FIFO.
1335 * @param hcd The HCD state structure.
1336 * @param hc Host channel descriptor associated with either a periodic or
1337 * non-periodic transfer.
1338 * @param fifo_dwords_avail Number of DWORDs available in the periodic Tx
1339 * FIFO for periodic transfers or the non-periodic Tx FIFO for non-periodic
1342 * @return 1 if a request is queued and more requests may be needed to
1343 * complete the transfer, 0 if no more requests are required for this
1344 * transfer, -1 if there is insufficient space in the Tx FIFO.
1346 static int queue_transaction(dwc_otg_hcd_t *hcd,
1347 dwc_hc_t *hc, uint16_t fifo_dwords_avail)
1351 if (hcd->core_if->dma_enable) {
1352 if (hcd->core_if->dma_desc_enable) {
1353 if (!hc->xfer_started
1354 || (hc->ep_type == DWC_OTG_EP_TYPE_ISOC)) {
1355 dwc_otg_hcd_start_xfer_ddma(hcd, hc->qh);
1356 hc->qh->ping_state = 0;
1358 } else if (!hc->xfer_started) {
1359 if (!hc || !(hc->qh))
1361 dwc_otg_hc_start_transfer(hcd->core_if, hc);
1362 hc->qh->ping_state = 0;
1365 } else if (hc->halt_pending) {
1366 /* Don't queue a request if the channel has been halted. */
1368 } else if (hc->halt_on_queue) {
1369 dwc_otg_hc_halt(hcd->core_if, hc, hc->halt_status);
1371 } else if (hc->do_ping) {
1372 if (!hc->xfer_started) {
1373 dwc_otg_hc_start_transfer(hcd->core_if, hc);
1376 } else if (!hc->ep_is_in || hc->data_pid_start == DWC_OTG_HC_PID_SETUP) {
1377 if ((fifo_dwords_avail * 4) >= hc->max_packet) {
1378 if (!hc->xfer_started) {
1379 dwc_otg_hc_start_transfer(hcd->core_if, hc);
1383 dwc_otg_hc_continue_transfer(hcd->core_if,
1390 if (!hc->xfer_started) {
1391 dwc_otg_hc_start_transfer(hcd->core_if, hc);
1394 retval = dwc_otg_hc_continue_transfer(hcd->core_if, hc);
1402 * Processes periodic channels for the next frame and queues transactions for
1403 * these channels to the DWC_otg controller. After queueing transactions, the
1404 * Periodic Tx FIFO Empty interrupt is enabled if there are more transactions
1405 * to queue as Periodic Tx FIFO or request queue space becomes available.
1406 * Otherwise, the Periodic Tx FIFO Empty interrupt is disabled.
1408 static void process_periodic_channels(dwc_otg_hcd_t *hcd)
1410 hptxsts_data_t tx_status;
1411 dwc_list_link_t *qh_ptr;
1414 int no_queue_space = 0;
1415 int no_fifo_space = 0;
1417 dwc_otg_host_global_regs_t *host_regs;
1418 host_regs = hcd->core_if->host_if->host_global_regs;
1420 DWC_DEBUGPL(DBG_HCDV, "Queue periodic transactions\n");
1422 tx_status.d32 = DWC_READ_REG32(&host_regs->hptxsts);
1423 DWC_DEBUGPL(DBG_HCDV,
1424 " P Tx Req Queue Space Avail (before queue): %d\n",
1425 tx_status.b.ptxqspcavail);
1426 DWC_DEBUGPL(DBG_HCDV, " P Tx FIFO Space Avail (before queue): %d\n",
1427 tx_status.b.ptxfspcavail);
1430 qh_ptr = hcd->periodic_sched_assigned.next;
1431 while (qh_ptr != &hcd->periodic_sched_assigned) {
1432 tx_status.d32 = DWC_READ_REG32(&host_regs->hptxsts);
1433 if (tx_status.b.ptxqspcavail == 0) {
1438 qh = DWC_LIST_ENTRY(qh_ptr, dwc_otg_qh_t, qh_list_entry);
1441 * Set a flag if we're queuing high-bandwidth in slave mode.
1442 * The flag prevents any halts to get into the request queue in
1443 * the middle of multiple high-bandwidth packets getting queued.
1445 if (!hcd->core_if->dma_enable && qh->channel->multi_count > 1) {
1446 hcd->core_if->queuing_high_bandwidth = 1;
1449 queue_transaction(hcd, qh->channel,
1450 tx_status.b.ptxfspcavail);
1457 * In Slave mode, stay on the current transfer until there is
1458 * nothing more to do or the high-bandwidth request count is
1459 * reached. In DMA mode, only need to queue one request. The
1460 * controller automatically handles multiple packets for
1461 * high-bandwidth transfers.
1463 if (hcd->core_if->dma_enable || status == 0 ||
1464 qh->channel->requests == qh->channel->multi_count) {
1465 qh_ptr = qh_ptr->next;
1467 * Move the QH from the periodic assigned schedule to
1468 * the periodic queued schedule.
1470 DWC_LIST_MOVE_HEAD(&hcd->periodic_sched_queued,
1471 &qh->qh_list_entry);
1473 /* done queuing high bandwidth */
1474 hcd->core_if->queuing_high_bandwidth = 0;
1478 if (!hcd->core_if->dma_enable) {
1479 dwc_otg_core_global_regs_t *global_regs;
1480 gintmsk_data_t intr_mask = {.d32 = 0 };
1482 global_regs = hcd->core_if->core_global_regs;
1483 intr_mask.b.ptxfempty = 1;
1485 tx_status.d32 = DWC_READ_REG32(&host_regs->hptxsts);
1486 DWC_DEBUGPL(DBG_HCDV,
1487 " P Tx Req Queue Space Avail (after queue): %d\n",
1488 tx_status.b.ptxqspcavail);
1489 DWC_DEBUGPL(DBG_HCDV,
1490 " P Tx FIFO Space Avail (after queue): %d\n",
1491 tx_status.b.ptxfspcavail);
1493 if (!DWC_LIST_EMPTY(&hcd->periodic_sched_assigned) ||
1494 no_queue_space || no_fifo_space) {
1496 * May need to queue more transactions as the request
1497 * queue or Tx FIFO empties. Enable the periodic Tx
1498 * FIFO empty interrupt. (Always use the half-empty
1499 * level to ensure that new requests are loaded as
1500 * soon as possible.)
1502 DWC_MODIFY_REG32(&global_regs->gintmsk, 0,
1506 * Disable the Tx FIFO empty interrupt since there are
1507 * no more transactions that need to be queued right
1508 * now. This function is called from interrupt
1509 * handlers to queue more transactions as transfer
1512 DWC_MODIFY_REG32(&global_regs->gintmsk, intr_mask.d32,
1519 * Processes active non-periodic channels and queues transactions for these
1520 * channels to the DWC_otg controller. After queueing transactions, the NP Tx
1521 * FIFO Empty interrupt is enabled if there are more transactions to queue as
1522 * NP Tx FIFO or request queue space becomes available. Otherwise, the NP Tx
1523 * FIFO Empty interrupt is disabled.
1525 static void process_non_periodic_channels(dwc_otg_hcd_t *hcd)
1527 gnptxsts_data_t tx_status;
1528 dwc_list_link_t *orig_qh_ptr;
1531 int no_queue_space = 0;
1532 int no_fifo_space = 0;
1535 dwc_otg_core_global_regs_t *global_regs =
1536 hcd->core_if->core_global_regs;
1538 DWC_DEBUGPL(DBG_HCDV, "Queue non-periodic transactions\n");
1540 tx_status.d32 = DWC_READ_REG32(&global_regs->gnptxsts);
1541 DWC_DEBUGPL(DBG_HCDV,
1542 " NP Tx Req Queue Space Avail (before queue): %d\n",
1543 tx_status.b.nptxqspcavail);
1544 DWC_DEBUGPL(DBG_HCDV, " NP Tx FIFO Space Avail (before queue): %d\n",
1545 tx_status.b.nptxfspcavail);
1548 * Keep track of the starting point. Skip over the start-of-list
1551 if (hcd->non_periodic_qh_ptr == &hcd->non_periodic_sched_active) {
1552 hcd->non_periodic_qh_ptr = hcd->non_periodic_qh_ptr->next;
1554 orig_qh_ptr = hcd->non_periodic_qh_ptr;
1557 * Process once through the active list or until no more space is
1558 * available in the request queue or the Tx FIFO.
1561 tx_status.d32 = DWC_READ_REG32(&global_regs->gnptxsts);
1562 if (!hcd->core_if->dma_enable && tx_status.b.nptxqspcavail == 0) {
1567 qh = DWC_LIST_ENTRY(hcd->non_periodic_qh_ptr, dwc_otg_qh_t,
1570 queue_transaction(hcd, qh->channel,
1571 tx_status.b.nptxfspcavail);
1575 } else if (status < 0) {
1580 /* Advance to next QH, skipping start-of-list entry. */
1581 hcd->non_periodic_qh_ptr = hcd->non_periodic_qh_ptr->next;
1582 if (hcd->non_periodic_qh_ptr == &hcd->non_periodic_sched_active) {
1583 hcd->non_periodic_qh_ptr =
1584 hcd->non_periodic_qh_ptr->next;
1587 } while (hcd->non_periodic_qh_ptr != orig_qh_ptr);
1589 if (!hcd->core_if->dma_enable) {
1590 gintmsk_data_t intr_mask = {.d32 = 0 };
1591 intr_mask.b.nptxfempty = 1;
1594 tx_status.d32 = DWC_READ_REG32(&global_regs->gnptxsts);
1595 DWC_DEBUGPL(DBG_HCDV,
1596 " NP Tx Req Queue Space Avail (after queue): %d\n",
1597 tx_status.b.nptxqspcavail);
1598 DWC_DEBUGPL(DBG_HCDV,
1599 " NP Tx FIFO Space Avail (after queue): %d\n",
1600 tx_status.b.nptxfspcavail);
1602 if (more_to_do || no_queue_space || no_fifo_space) {
1604 * May need to queue more transactions as the request
1605 * queue or Tx FIFO empties. Enable the non-periodic
1606 * Tx FIFO empty interrupt. (Always use the half-empty
1607 * level to ensure that new requests are loaded as
1608 * soon as possible.)
1610 DWC_MODIFY_REG32(&global_regs->gintmsk, 0,
1614 * Disable the Tx FIFO empty interrupt since there are
1615 * no more transactions that need to be queued right
1616 * now. This function is called from interrupt
1617 * handlers to queue more transactions as transfer
1620 DWC_MODIFY_REG32(&global_regs->gintmsk, intr_mask.d32,
1627 * This function processes the currently active host channels and queues
1628 * transactions for these channels to the DWC_otg controller. It is called
1629 * from HCD interrupt handler functions.
1631 * @param hcd The HCD state structure.
1632 * @param tr_type The type(s) of transactions to queue (non-periodic,
1633 * periodic, or both).
1635 void dwc_otg_hcd_queue_transactions(dwc_otg_hcd_t *hcd,
1636 dwc_otg_transaction_type_e tr_type)
1639 DWC_DEBUGPL(DBG_HCD, "Queue Transactions\n");
1641 /* Process host channels associated with periodic transfers. */
1642 if ((tr_type == DWC_OTG_TRANSACTION_PERIODIC ||
1643 tr_type == DWC_OTG_TRANSACTION_ALL) &&
1644 !DWC_LIST_EMPTY(&hcd->periodic_sched_assigned)) {
1646 process_periodic_channels(hcd);
1649 /* Process host channels associated with non-periodic transfers. */
1650 if (tr_type == DWC_OTG_TRANSACTION_NON_PERIODIC ||
1651 tr_type == DWC_OTG_TRANSACTION_ALL) {
1652 if (!DWC_LIST_EMPTY(&hcd->non_periodic_sched_active)) {
1653 process_non_periodic_channels(hcd);
1656 * Ensure NP Tx FIFO empty interrupt is disabled when
1657 * there are no non-periodic transfers to process.
1659 gintmsk_data_t gintmsk = {.d32 = 0 };
1660 gintmsk.b.nptxfempty = 1;
1661 DWC_MODIFY_REG32(&hcd->core_if->core_global_regs->
1662 gintmsk, gintmsk.d32, 0);
1667 #ifdef DWC_HS_ELECT_TST
1669 * Quick and dirty hack to implement the HS Electrical Test
1670 * SINGLE_STEP_GET_DEVICE_DESCRIPTOR feature.
1672 * This code was copied from our userspace app "hset". It sends a
1673 * Get Device Descriptor control sequence in two parts, first the
1674 * Setup packet by itself, followed some time later by the In and
1675 * Ack packets. Rather than trying to figure out how to add this
1676 * functionality to the normal driver code, we just hijack the
1677 * hardware, using these two function to drive the hardware
1681 static dwc_otg_core_global_regs_t *global_regs;
1682 static dwc_otg_host_global_regs_t *hc_global_regs;
1683 static dwc_otg_hc_regs_t *hc_regs;
1684 static uint32_t *data_fifo;
1686 static void do_setup(void)
1688 gintsts_data_t gintsts;
1689 hctsiz_data_t hctsiz;
1690 hcchar_data_t hcchar;
1695 DWC_WRITE_REG32(&hc_global_regs->haintmsk, 0x0001);
1698 DWC_WRITE_REG32(&hc_regs->hcintmsk, 0x04a3);
1701 gintsts.d32 = DWC_READ_REG32(&global_regs->gintsts);
1704 haint.d32 = DWC_READ_REG32(&hc_global_regs->haint);
1707 hcint.d32 = DWC_READ_REG32(&hc_regs->hcint);
1710 hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
1713 DWC_WRITE_REG32(&hc_regs->hcint, hcint.d32);
1716 DWC_WRITE_REG32(&hc_global_regs->haint, haint.d32);
1719 DWC_WRITE_REG32(&global_regs->gintsts, gintsts.d32);
1722 gintsts.d32 = DWC_READ_REG32(&global_regs->gintsts);
1725 * Send Setup packet (Get Device Descriptor)
1728 /* Make sure channel is disabled */
1729 hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
1730 if (hcchar.b.chen) {
1732 /* hcchar.b.chen = 1; */
1733 DWC_WRITE_REG32(&hc_regs->hcchar, hcchar.d32);
1738 gintsts.d32 = DWC_READ_REG32(&global_regs->gintsts);
1741 haint.d32 = DWC_READ_REG32(&hc_global_regs->haint);
1744 hcint.d32 = DWC_READ_REG32(&hc_regs->hcint);
1747 hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
1750 DWC_WRITE_REG32(&hc_regs->hcint, hcint.d32);
1753 DWC_WRITE_REG32(&hc_global_regs->haint, haint.d32);
1756 DWC_WRITE_REG32(&global_regs->gintsts, gintsts.d32);
1758 hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
1763 hctsiz.b.xfersize = 8;
1764 hctsiz.b.pktcnt = 1;
1765 hctsiz.b.pid = DWC_OTG_HC_PID_SETUP;
1766 DWC_WRITE_REG32(&hc_regs->hctsiz, hctsiz.d32);
1769 hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
1770 hcchar.b.eptype = DWC_OTG_EP_TYPE_CONTROL;
1775 DWC_WRITE_REG32(&hc_regs->hcchar, hcchar.d32);
1777 /* Fill FIFO with Setup data for Get Device Descriptor */
1778 data_fifo = (uint32_t *) ((char *)global_regs + 0x1000);
1779 DWC_WRITE_REG32(data_fifo++, 0x01000680);
1780 DWC_WRITE_REG32(data_fifo++, 0x00080000);
1782 gintsts.d32 = DWC_READ_REG32(&global_regs->gintsts);
1784 /* Wait for host channel interrupt */
1786 gintsts.d32 = DWC_READ_REG32(&global_regs->gintsts);
1787 } while (gintsts.b.hcintr == 0);
1789 /* Disable HCINTs */
1790 DWC_WRITE_REG32(&hc_regs->hcintmsk, 0x0000);
1792 /* Disable HAINTs */
1793 DWC_WRITE_REG32(&hc_global_regs->haintmsk, 0x0000);
1796 haint.d32 = DWC_READ_REG32(&hc_global_regs->haint);
1799 hcint.d32 = DWC_READ_REG32(&hc_regs->hcint);
1802 hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
1805 DWC_WRITE_REG32(&hc_regs->hcint, hcint.d32);
1808 DWC_WRITE_REG32(&hc_global_regs->haint, haint.d32);
1811 DWC_WRITE_REG32(&global_regs->gintsts, gintsts.d32);
1814 gintsts.d32 = DWC_READ_REG32(&global_regs->gintsts);
1817 static void do_in_ack(void)
1819 gintsts_data_t gintsts;
1820 hctsiz_data_t hctsiz;
1821 hcchar_data_t hcchar;
1824 host_grxsts_data_t grxsts;
1827 DWC_WRITE_REG32(&hc_global_regs->haintmsk, 0x0001);
1830 DWC_WRITE_REG32(&hc_regs->hcintmsk, 0x04a3);
1833 gintsts.d32 = DWC_READ_REG32(&global_regs->gintsts);
1836 haint.d32 = DWC_READ_REG32(&hc_global_regs->haint);
1839 hcint.d32 = DWC_READ_REG32(&hc_regs->hcint);
1842 hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
1845 DWC_WRITE_REG32(&hc_regs->hcint, hcint.d32);
1848 DWC_WRITE_REG32(&hc_global_regs->haint, haint.d32);
1851 DWC_WRITE_REG32(&global_regs->gintsts, gintsts.d32);
1854 gintsts.d32 = DWC_READ_REG32(&global_regs->gintsts);
1857 * Receive Control In packet
1860 /* Make sure channel is disabled */
1861 hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
1862 if (hcchar.b.chen) {
1865 DWC_WRITE_REG32(&hc_regs->hcchar, hcchar.d32);
1870 gintsts.d32 = DWC_READ_REG32(&global_regs->gintsts);
1873 haint.d32 = DWC_READ_REG32(&hc_global_regs->haint);
1876 hcint.d32 = DWC_READ_REG32(&hc_regs->hcint);
1879 hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
1882 DWC_WRITE_REG32(&hc_regs->hcint, hcint.d32);
1885 DWC_WRITE_REG32(&hc_global_regs->haint, haint.d32);
1888 DWC_WRITE_REG32(&global_regs->gintsts, gintsts.d32);
1890 hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
1895 hctsiz.b.xfersize = 8;
1896 hctsiz.b.pktcnt = 1;
1897 hctsiz.b.pid = DWC_OTG_HC_PID_DATA1;
1898 DWC_WRITE_REG32(&hc_regs->hctsiz, hctsiz.d32);
1901 hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
1902 hcchar.b.eptype = DWC_OTG_EP_TYPE_CONTROL;
1907 DWC_WRITE_REG32(&hc_regs->hcchar, hcchar.d32);
1909 gintsts.d32 = DWC_READ_REG32(&global_regs->gintsts);
1911 /* Wait for receive status queue interrupt */
1913 gintsts.d32 = DWC_READ_REG32(&global_regs->gintsts);
1914 } while (gintsts.b.rxstsqlvl == 0);
1917 grxsts.d32 = DWC_READ_REG32(&global_regs->grxstsp);
1919 /* Clear RXSTSQLVL in GINTSTS */
1921 gintsts.b.rxstsqlvl = 1;
1922 DWC_WRITE_REG32(&global_regs->gintsts, gintsts.d32);
1924 switch (grxsts.b.pktsts) {
1925 case DWC_GRXSTS_PKTSTS_IN:
1926 /* Read the data into the host buffer */
1927 if (grxsts.b.bcnt > 0) {
1929 int word_count = (grxsts.b.bcnt + 3) / 4;
1931 data_fifo = (uint32_t *) ((char *)global_regs + 0x1000);
1933 for (i = 0; i < word_count; i++) {
1934 (void)DWC_READ_REG32(data_fifo++);
1943 gintsts.d32 = DWC_READ_REG32(&global_regs->gintsts);
1945 /* Wait for receive status queue interrupt */
1947 gintsts.d32 = DWC_READ_REG32(&global_regs->gintsts);
1948 } while (gintsts.b.rxstsqlvl == 0);
1951 grxsts.d32 = DWC_READ_REG32(&global_regs->grxstsp);
1953 /* Clear RXSTSQLVL in GINTSTS */
1955 gintsts.b.rxstsqlvl = 1;
1956 DWC_WRITE_REG32(&global_regs->gintsts, gintsts.d32);
1958 switch (grxsts.b.pktsts) {
1959 case DWC_GRXSTS_PKTSTS_IN_XFER_COMP:
1966 gintsts.d32 = DWC_READ_REG32(&global_regs->gintsts);
1968 /* Wait for host channel interrupt */
1970 gintsts.d32 = DWC_READ_REG32(&global_regs->gintsts);
1971 } while (gintsts.b.hcintr == 0);
1974 haint.d32 = DWC_READ_REG32(&hc_global_regs->haint);
1977 hcint.d32 = DWC_READ_REG32(&hc_regs->hcint);
1980 hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
1983 DWC_WRITE_REG32(&hc_regs->hcint, hcint.d32);
1986 DWC_WRITE_REG32(&hc_global_regs->haint, haint.d32);
1989 DWC_WRITE_REG32(&global_regs->gintsts, gintsts.d32);
1992 gintsts.d32 = DWC_READ_REG32(&global_regs->gintsts);
1994 /* usleep(100000); */
1999 * Send handshake packet
2003 haint.d32 = DWC_READ_REG32(&hc_global_regs->haint);
2006 hcint.d32 = DWC_READ_REG32(&hc_regs->hcint);
2009 hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
2012 DWC_WRITE_REG32(&hc_regs->hcint, hcint.d32);
2015 DWC_WRITE_REG32(&hc_global_regs->haint, haint.d32);
2018 DWC_WRITE_REG32(&global_regs->gintsts, gintsts.d32);
2021 gintsts.d32 = DWC_READ_REG32(&global_regs->gintsts);
2023 /* Make sure channel is disabled */
2024 hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
2025 if (hcchar.b.chen) {
2028 DWC_WRITE_REG32(&hc_regs->hcchar, hcchar.d32);
2033 gintsts.d32 = DWC_READ_REG32(&global_regs->gintsts);
2036 haint.d32 = DWC_READ_REG32(&hc_global_regs->haint);
2039 hcint.d32 = DWC_READ_REG32(&hc_regs->hcint);
2042 hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
2045 DWC_WRITE_REG32(&hc_regs->hcint, hcint.d32);
2048 DWC_WRITE_REG32(&hc_global_regs->haint, haint.d32);
2051 DWC_WRITE_REG32(&global_regs->gintsts, gintsts.d32);
2053 hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
2058 hctsiz.b.xfersize = 0;
2059 hctsiz.b.pktcnt = 1;
2060 hctsiz.b.pid = DWC_OTG_HC_PID_DATA1;
2061 DWC_WRITE_REG32(&hc_regs->hctsiz, hctsiz.d32);
2064 hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
2065 hcchar.b.eptype = DWC_OTG_EP_TYPE_CONTROL;
2070 DWC_WRITE_REG32(&hc_regs->hcchar, hcchar.d32);
2072 gintsts.d32 = DWC_READ_REG32(&global_regs->gintsts);
2074 /* Wait for host channel interrupt */
2076 gintsts.d32 = DWC_READ_REG32(&global_regs->gintsts);
2077 } while (gintsts.b.hcintr == 0);
2079 /* Disable HCINTs */
2080 DWC_WRITE_REG32(&hc_regs->hcintmsk, 0x0000);
2082 /* Disable HAINTs */
2083 DWC_WRITE_REG32(&hc_global_regs->haintmsk, 0x0000);
2086 haint.d32 = DWC_READ_REG32(&hc_global_regs->haint);
2089 hcint.d32 = DWC_READ_REG32(&hc_regs->hcint);
2092 hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
2095 DWC_WRITE_REG32(&hc_regs->hcint, hcint.d32);
2098 DWC_WRITE_REG32(&hc_global_regs->haint, haint.d32);
2101 DWC_WRITE_REG32(&global_regs->gintsts, gintsts.d32);
2104 gintsts.d32 = DWC_READ_REG32(&global_regs->gintsts);
2108 /** Handles hub class-specific requests. */
2109 int dwc_otg_hcd_hub_control(dwc_otg_hcd_t *dwc_otg_hcd,
2112 uint16_t wIndex, uint8_t *buf, uint16_t wLength)
2116 dwc_otg_core_if_t *core_if = dwc_otg_hcd->core_if;
2117 usb_hub_descriptor_t *hub_desc;
2118 hprt0_data_t hprt0 = {.d32 = 0 };
2120 uint32_t port_status;
2123 case UCR_CLEAR_HUB_FEATURE:
2124 DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD HUB CONTROL - "
2125 "ClearHubFeature 0x%x\n", wValue);
2127 case UHF_C_HUB_LOCAL_POWER:
2128 case UHF_C_HUB_OVER_CURRENT:
2129 /* Nothing required here */
2132 retval = -DWC_E_INVALID;
2133 DWC_ERROR("DWC OTG HCD - "
2134 "ClearHubFeature request %xh unknown\n",
2138 case UCR_CLEAR_PORT_FEATURE:
2139 #ifdef CONFIG_USB_DWC_OTG_LPM
2140 if (wValue != UHF_PORT_L1)
2142 if (!wIndex || wIndex > 1)
2146 case UHF_PORT_ENABLE:
2147 DWC_DEBUGPL(DBG_ANY, "DWC OTG HCD HUB CONTROL - "
2148 "ClearPortFeature USB_PORT_FEAT_ENABLE\n");
2149 hprt0.d32 = dwc_otg_read_hprt0(core_if);
2151 DWC_WRITE_REG32(core_if->host_if->hprt0, hprt0.d32);
2153 case UHF_PORT_SUSPEND:
2154 DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD HUB CONTROL - "
2155 "ClearPortFeature USB_PORT_FEAT_SUSPEND\n");
2157 if (core_if->power_down == 2) {
2158 dwc_otg_host_hibernation_restore(core_if, 0, 0);
2160 DWC_WRITE_REG32(core_if->pcgcctl, 0);
2163 hprt0.d32 = dwc_otg_read_hprt0(core_if);
2165 DWC_WRITE_REG32(core_if->host_if->hprt0,
2167 hprt0.b.prtsusp = 0;
2168 /* Clear Resume bit */
2171 DWC_WRITE_REG32(core_if->host_if->hprt0,
2175 #ifdef CONFIG_USB_DWC_OTG_LPM
2178 pcgcctl_data_t pcgcctl = {.d32 = 0 };
2179 glpmcfg_data_t lpmcfg = {.d32 = 0 };
2182 DWC_READ_REG32(&core_if->core_global_regs->
2184 lpmcfg.b.en_utmi_sleep = 0;
2185 lpmcfg.b.hird_thres &= (~(1 << 4));
2186 lpmcfg.b.prt_sleep_sts = 1;
2187 DWC_WRITE_REG32(&core_if->core_global_regs->
2188 glpmcfg, lpmcfg.d32);
2190 /* Clear Enbl_L1Gating bit. */
2191 pcgcctl.b.enbl_sleep_gating = 1;
2192 DWC_MODIFY_REG32(core_if->pcgcctl, pcgcctl.d32,
2197 hprt0.d32 = dwc_otg_read_hprt0(core_if);
2199 DWC_WRITE_REG32(core_if->host_if->hprt0,
2201 /* This bit will be cleared in wakeup interrupt handle */
2205 case UHF_PORT_POWER:
2206 DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD HUB CONTROL - "
2207 "ClearPortFeature USB_PORT_FEAT_POWER\n");
2208 hprt0.d32 = dwc_otg_read_hprt0(core_if);
2210 DWC_WRITE_REG32(core_if->host_if->hprt0, hprt0.d32);
2212 case UHF_PORT_INDICATOR:
2213 DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD HUB CONTROL - "
2214 "ClearPortFeature USB_PORT_FEAT_INDICATOR\n");
2215 /* Port inidicator not supported */
2217 case UHF_C_PORT_CONNECTION:
2218 /* Clears drivers internal connect status change
2220 DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD HUB CONTROL - "
2221 "ClearPortFeature USB_PORT_FEAT_C_CONNECTION\n");
2222 dwc_otg_hcd->flags.b.port_connect_status_change = 0;
2224 case UHF_C_PORT_RESET:
2225 /* Clears the driver's internal Port Reset Change
2227 DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD HUB CONTROL - "
2228 "ClearPortFeature USB_PORT_FEAT_C_RESET\n");
2229 dwc_otg_hcd->flags.b.port_reset_change = 0;
2231 case UHF_C_PORT_ENABLE:
2232 /* Clears the driver's internal Port
2233 * Enable/Disable Change flag */
2234 DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD HUB CONTROL - "
2235 "ClearPortFeature USB_PORT_FEAT_C_ENABLE\n");
2236 dwc_otg_hcd->flags.b.port_enable_change = 0;
2238 case UHF_C_PORT_SUSPEND:
2239 /* Clears the driver's internal Port Suspend
2240 * Change flag, which is set when resume signaling on
2241 * the host port is complete */
2242 DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD HUB CONTROL - "
2243 "ClearPortFeature USB_PORT_FEAT_C_SUSPEND\n");
2244 dwc_otg_hcd->flags.b.port_suspend_change = 0;
2246 #ifdef CONFIG_USB_DWC_OTG_LPM
2248 dwc_otg_hcd->flags.b.port_l1_change = 0;
2251 case UHF_C_PORT_OVER_CURRENT:
2252 DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD HUB CONTROL - "
2253 "ClearPortFeature USB_PORT_FEAT_C_OVER_CURRENT\n");
2254 dwc_otg_hcd->flags.b.port_over_current_change = 0;
2257 retval = -DWC_E_INVALID;
2258 DWC_ERROR("DWC OTG HCD - "
2259 "ClearPortFeature request %xh "
2260 "unknown or unsupported\n", wValue);
2263 case UCR_GET_HUB_DESCRIPTOR:
2264 DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD HUB CONTROL - "
2265 "GetHubDescriptor\n");
2266 hub_desc = (usb_hub_descriptor_t *) buf;
2267 hub_desc->bDescLength = 9;
2268 hub_desc->bDescriptorType = 0x29;
2269 hub_desc->bNbrPorts = 1;
2270 USETW(hub_desc->wHubCharacteristics, 0x08);
2271 hub_desc->bPwrOn2PwrGood = 1;
2272 hub_desc->bHubContrCurrent = 0;
2273 hub_desc->DeviceRemovable[0] = 0;
2274 hub_desc->DeviceRemovable[1] = 0xff;
2276 case UCR_GET_HUB_STATUS:
2277 DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD HUB CONTROL - "
2279 DWC_MEMSET(buf, 0, 4);
2281 case UCR_GET_PORT_STATUS:
2282 DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD HUB CONTROL - "
2283 "GetPortStatus wIndex = 0x%04x FLAGS=0x%08x\n",
2284 wIndex, dwc_otg_hcd->flags.d32);
2285 if (!wIndex || wIndex > 1)
2290 if (dwc_otg_hcd->flags.b.port_connect_status_change)
2291 port_status |= (1 << UHF_C_PORT_CONNECTION);
2293 if (dwc_otg_hcd->flags.b.port_enable_change)
2294 port_status |= (1 << UHF_C_PORT_ENABLE);
2296 if (dwc_otg_hcd->flags.b.port_suspend_change)
2297 port_status |= (1 << UHF_C_PORT_SUSPEND);
2299 if (dwc_otg_hcd->flags.b.port_l1_change)
2300 port_status |= (1 << UHF_C_PORT_L1);
2302 if (dwc_otg_hcd->flags.b.port_reset_change) {
2303 port_status |= (1 << UHF_C_PORT_RESET);
2306 if (dwc_otg_hcd->flags.b.port_over_current_change) {
2307 DWC_WARN("Overcurrent change detected\n");
2308 port_status |= (1 << UHF_C_PORT_OVER_CURRENT);
2311 if (!dwc_otg_hcd->flags.b.port_connect_status) {
2313 * The port is disconnected, which means the core is
2314 * either in device mode or it soon will be. Just
2315 * return 0's for the remainder of the port status
2316 * since the port register can't be read if the core
2317 * is in device mode.
2319 *((__le32 *) buf) = dwc_cpu_to_le32(&port_status);
2323 hprt0.d32 = DWC_READ_REG32(core_if->host_if->hprt0);
2324 DWC_DEBUGPL(DBG_HCDV, " HPRT0: 0x%08x\n", hprt0.d32);
2326 if (hprt0.b.prtconnsts)
2327 port_status |= (1 << UHF_PORT_CONNECTION);
2330 port_status |= (1 << UHF_PORT_ENABLE);
2332 if (hprt0.b.prtsusp)
2333 port_status |= (1 << UHF_PORT_SUSPEND);
2335 if (hprt0.b.prtovrcurract)
2336 port_status |= (1 << UHF_PORT_OVER_CURRENT);
2339 port_status |= (1 << UHF_PORT_RESET);
2342 port_status |= (1 << UHF_PORT_POWER);
2344 if (hprt0.b.prtspd == DWC_HPRT0_PRTSPD_HIGH_SPEED)
2345 port_status |= (1 << UHF_PORT_HIGH_SPEED);
2346 else if (hprt0.b.prtspd == DWC_HPRT0_PRTSPD_LOW_SPEED)
2347 port_status |= (1 << UHF_PORT_LOW_SPEED);
2349 if (hprt0.b.prttstctl)
2350 port_status |= (1 << UHF_PORT_TEST);
2351 if (dwc_otg_get_lpm_portsleepstatus(dwc_otg_hcd->core_if)) {
2352 port_status |= (1 << UHF_PORT_L1);
2355 For Synopsys HW emulation of Power down wkup_control asserts the
2356 hreset_n and prst_n on suspned. This causes the HPRT0 to be zero.
2357 We intentionally tell the software that port is in L2Suspend state.
2360 if ((core_if->power_down == 2)
2361 && (core_if->hibernation_suspend == 1)) {
2362 port_status |= (1 << UHF_PORT_SUSPEND);
2364 /* USB_PORT_FEAT_INDICATOR unsupported always 0 */
2366 *((__le32 *) buf) = dwc_cpu_to_le32(&port_status);
2369 case UCR_SET_HUB_FEATURE:
2370 DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD HUB CONTROL - "
2372 /* No HUB features supported */
2374 case UCR_SET_PORT_FEATURE:
2375 if (wValue != UHF_PORT_TEST && (!wIndex || wIndex > 1))
2378 if (!dwc_otg_hcd->flags.b.port_connect_status) {
2380 * The port is disconnected, which means the core is
2381 * either in device mode or it soon will be. Just
2382 * return without doing anything since the port
2383 * register can't be written if the core is in device
2390 case UHF_PORT_SUSPEND:
2391 DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD HUB CONTROL - "
2392 "SetPortFeature - USB_PORT_FEAT_SUSPEND\n");
2393 if (dwc_otg_hcd_otg_port(dwc_otg_hcd) != wIndex) {
2396 if (core_if->power_down == 2) {
2398 dwc_irqflags_t flags;
2399 pcgcctl_data_t pcgcctl = {.d32 = 0 };
2400 gpwrdn_data_t gpwrdn = {.d32 = 0 };
2401 gusbcfg_data_t gusbcfg = {.d32 = 0 };
2402 #ifdef DWC_DEV_SRPCAP
2403 int32_t otg_cap_param =
2404 core_if->core_params->otg_cap;
2407 ("Preparing for complete power-off\n");
2409 /* Save registers before hibernation */
2410 dwc_otg_save_global_regs(core_if);
2411 dwc_otg_save_host_regs(core_if);
2413 hprt0.d32 = dwc_otg_read_hprt0(core_if);
2414 hprt0.b.prtsusp = 1;
2416 DWC_WRITE_REG32(core_if->host_if->hprt0,
2418 /* Spin hprt0.b.prtsusp to became 1 */
2420 hprt0.d32 = dwc_otg_read_hprt0(core_if);
2421 if (hprt0.b.prtsusp) {
2425 } while (--timeout);
2427 DWC_WARN("Suspend wasn't genereted\n");
2432 * We need to disable interrupts to prevent servicing of any IRQ
2433 * during going to hibernation
2435 DWC_SPINLOCK_IRQSAVE(dwc_otg_hcd->lock, &flags);
2436 core_if->lx_state = DWC_OTG_L2;
2437 #ifdef DWC_DEV_SRPCAP
2438 hprt0.d32 = dwc_otg_read_hprt0(core_if);
2441 DWC_WRITE_REG32(core_if->host_if->hprt0,
2445 DWC_READ_REG32(&core_if->
2446 core_global_regs->gusbcfg);
2447 if (gusbcfg.b.ulpi_utmi_sel == 1) {
2448 /* ULPI interface */
2449 /* Suspend the Phy Clock */
2451 pcgcctl.b.stoppclk = 1;
2452 DWC_MODIFY_REG32(core_if->pcgcctl, 0,
2455 gpwrdn.b.pmuactv = 1;
2457 (&core_if->core_global_regs->gpwrdn,
2460 /* UTMI+ Interface */
2461 gpwrdn.b.pmuactv = 1;
2463 (&core_if->core_global_regs->gpwrdn,
2466 pcgcctl.b.stoppclk = 1;
2467 DWC_MODIFY_REG32(core_if->pcgcctl, 0,
2471 #ifdef DWC_DEV_SRPCAP
2473 gpwrdn.b.dis_vbus = 1;
2474 DWC_MODIFY_REG32(&core_if->
2475 core_global_regs->gpwrdn, 0,
2479 gpwrdn.b.pmuintsel = 1;
2480 DWC_MODIFY_REG32(&core_if->
2481 core_global_regs->gpwrdn, 0,
2486 #ifdef DWC_DEV_SRPCAP
2487 gpwrdn.b.srp_det_msk = 1;
2489 gpwrdn.b.disconn_det_msk = 1;
2490 gpwrdn.b.lnstchng_msk = 1;
2491 gpwrdn.b.sts_chngint_msk = 1;
2492 DWC_MODIFY_REG32(&core_if->
2493 core_global_regs->gpwrdn, 0,
2497 /* Enable Power Down Clamp and all interrupts in GPWRDN */
2499 gpwrdn.b.pwrdnclmp = 1;
2500 DWC_MODIFY_REG32(&core_if->
2501 core_global_regs->gpwrdn, 0,
2505 /* Switch off VDD */
2507 gpwrdn.b.pwrdnswtch = 1;
2508 DWC_MODIFY_REG32(&core_if->
2509 core_global_regs->gpwrdn, 0,
2512 #ifdef DWC_DEV_SRPCAP
2513 if (otg_cap_param ==
2514 DWC_OTG_CAP_PARAM_HNP_SRP_CAPABLE) {
2515 core_if->pwron_timer_started = 1;
2516 DWC_TIMER_SCHEDULE(core_if->pwron_timer,
2520 /* Save gpwrdn register for further usage if stschng interrupt */
2521 core_if->gr_backup->gpwrdn_local =
2522 DWC_READ_REG32(&core_if->core_global_regs->
2525 /* Set flag to indicate that we are in hibernation */
2526 core_if->hibernation_suspend = 1;
2527 DWC_SPINUNLOCK_IRQRESTORE(dwc_otg_hcd->lock,
2530 DWC_PRINTF("Host hibernation completed\n");
2531 /* Exit from case statement */
2535 if (dwc_otg_hcd_otg_port(dwc_otg_hcd) == wIndex &&
2536 dwc_otg_hcd->fops->get_b_hnp_enable(dwc_otg_hcd)) {
2537 gotgctl_data_t gotgctl = {.d32 = 0 };
2538 gotgctl.b.hstsethnpen = 1;
2539 DWC_MODIFY_REG32(&core_if->
2540 core_global_regs->gotgctl, 0,
2542 core_if->op_state = A_SUSPEND;
2544 hprt0.d32 = dwc_otg_read_hprt0(core_if);
2545 hprt0.b.prtsusp = 1;
2546 DWC_WRITE_REG32(core_if->host_if->hprt0, hprt0.d32);
2548 dwc_irqflags_t flags;
2549 /* Update lx_state */
2550 DWC_SPINLOCK_IRQSAVE(dwc_otg_hcd->lock, &flags);
2551 core_if->lx_state = DWC_OTG_L2;
2552 DWC_SPINUNLOCK_IRQRESTORE(dwc_otg_hcd->lock,
2555 /* Suspend the Phy Clock */
2556 if (core_if->otg_ver == 0) {
2557 pcgcctl_data_t pcgcctl = {.d32 = 0 };
2558 pcgcctl.b.stoppclk = 1;
2559 DWC_MODIFY_REG32(core_if->pcgcctl, 0,
2564 /* For HNP the bus must be suspended for at least 200ms. */
2565 if (dwc_otg_hcd->fops->get_b_hnp_enable(dwc_otg_hcd)) {
2566 pcgcctl_data_t pcgcctl = {.d32 = 0 };
2567 pcgcctl.b.stoppclk = 1;
2568 DWC_MODIFY_REG32(core_if->pcgcctl, pcgcctl.d32,
2573 /** @todo - check how sw can wait for 1 sec to check asesvld??? */
2575 if (core_if->adp_enable) {
2576 gotgctl_data_t gotgctl = {.d32 = 0 };
2577 gpwrdn_data_t gpwrdn;
2579 while (gotgctl.b.asesvld == 1) {
2582 (&core_if->core_global_regs->gotgctl);
2586 /* Enable Power Down Logic */
2588 gpwrdn.b.pmuactv = 1;
2589 DWC_MODIFY_REG32(&core_if->
2590 core_global_regs->gpwrdn, 0,
2593 /* Unmask SRP detected interrupt from Power Down Logic */
2595 gpwrdn.b.srp_det_msk = 1;
2596 DWC_MODIFY_REG32(&core_if->
2597 core_global_regs->gpwrdn, 0,
2600 dwc_otg_adp_probe_start(core_if);
2604 case UHF_PORT_POWER:
2605 DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD HUB CONTROL - "
2606 "SetPortFeature - USB_PORT_FEAT_POWER\n");
2607 hprt0.d32 = dwc_otg_read_hprt0(core_if);
2609 DWC_WRITE_REG32(core_if->host_if->hprt0, hprt0.d32);
2611 case UHF_PORT_RESET:
2612 if ((core_if->power_down == 2)
2613 && (core_if->hibernation_suspend == 1)) {
2614 /* If we are going to exit from Hibernated
2615 * state via USB RESET.
2617 dwc_otg_host_hibernation_restore(core_if, 0, 1);
2619 hprt0.d32 = dwc_otg_read_hprt0(core_if);
2621 DWC_DEBUGPL(DBG_HCD,
2622 "DWC OTG HCD HUB CONTROL - "
2623 "SetPortFeature - USB_PORT_FEAT_RESET\n");
2625 pcgcctl_data_t pcgcctl = {.d32 = 0 };
2626 pcgcctl.b.enbl_sleep_gating = 1;
2627 pcgcctl.b.stoppclk = 1;
2628 DWC_MODIFY_REG32(core_if->pcgcctl,
2630 DWC_WRITE_REG32(core_if->pcgcctl, 0);
2632 #ifdef CONFIG_USB_DWC_OTG_LPM
2634 glpmcfg_data_t lpmcfg;
2636 DWC_READ_REG32(&core_if->
2639 if (lpmcfg.b.prt_sleep_sts) {
2640 lpmcfg.b.en_utmi_sleep = 0;
2641 lpmcfg.b.hird_thres &=
2643 DWC_WRITE_REG32(&core_if->
2651 hprt0.d32 = dwc_otg_read_hprt0(core_if);
2652 /* Clear suspend bit if resetting from suspended state. */
2653 hprt0.b.prtsusp = 0;
2654 /* When B-Host the Port reset bit is set in
2655 * the Start HCD Callback function, so that
2656 * the reset is started within 1ms of the HNP
2657 * success interrupt. */
2658 if (!dwc_otg_hcd_is_b_host(dwc_otg_hcd)) {
2662 ("Indeed it is in host mode hprt0 = %08x\n",
2664 DWC_WRITE_REG32(core_if->host_if->hprt0,
2667 /* Clear reset bit in 10ms (FS/LS) or 50ms (HS) */
2670 DWC_WRITE_REG32(core_if->host_if->hprt0,
2672 core_if->lx_state = DWC_OTG_L0; /* Now back to the on state */
2675 #ifdef DWC_HS_ELECT_TST
2679 gintmsk_data_t gintmsk;
2681 t = (wIndex >> 8); /* MSB wIndex USB */
2682 DWC_DEBUGPL(DBG_HCD,
2683 "DWC OTG HCD HUB CONTROL - "
2684 "SetPortFeature - USB_PORT_FEAT_TEST %d\n",
2686 DWC_WARN("USB_PORT_FEAT_TEST %d\n", t);
2688 hprt0.d32 = dwc_otg_read_hprt0(core_if);
2689 hprt0.b.prttstctl = t;
2690 DWC_WRITE_REG32(core_if->host_if->hprt0,
2693 /* Setup global vars with reg addresses (quick and
2694 * dirty hack, should be cleaned up)
2696 global_regs = core_if->core_global_regs;
2698 core_if->host_if->host_global_regs;
2700 (dwc_otg_hc_regs_t *) ((char *)
2704 (uint32_t *) ((char *)global_regs +
2707 if (t == 6) { /* HS_HOST_PORT_SUSPEND_RESUME */
2708 /* Save current interrupt mask */
2711 (&global_regs->gintmsk);
2713 /* Disable all interrupts while we muck with
2714 * the hardware directly
2716 DWC_WRITE_REG32(&global_regs->
2719 /* 15 second delay per the test spec */
2722 /* Drive suspend on the root port */
2724 dwc_otg_read_hprt0(core_if);
2725 hprt0.b.prtsusp = 1;
2727 DWC_WRITE_REG32(core_if->
2731 /* 15 second delay per the test spec */
2734 /* Drive resume on the root port */
2736 dwc_otg_read_hprt0(core_if);
2737 hprt0.b.prtsusp = 0;
2739 DWC_WRITE_REG32(core_if->
2744 /* Clear the resume bit */
2746 DWC_WRITE_REG32(core_if->
2750 /* Restore interrupts */
2751 DWC_WRITE_REG32(&global_regs->
2754 } else if (t == 7) { /* SINGLE_STEP_GET_DEVICE_DESCRIPTOR setup */
2755 /* Save current interrupt mask */
2758 (&global_regs->gintmsk);
2760 /* Disable all interrupts while we muck with
2761 * the hardware directly
2763 DWC_WRITE_REG32(&global_regs->
2766 /* 15 second delay per the test spec */
2769 /* Send the Setup packet */
2772 /* 15 second delay so nothing else happens for awhile */
2775 /* Restore interrupts */
2776 DWC_WRITE_REG32(&global_regs->
2779 } else if (t == 8) { /* SINGLE_STEP_GET_DEVICE_DESCRIPTOR execute */
2780 /* Save current interrupt mask */
2783 (&global_regs->gintmsk);
2785 /* Disable all interrupts while we muck with
2786 * the hardware directly
2788 DWC_WRITE_REG32(&global_regs->
2791 /* Send the Setup packet */
2794 /* 15 second delay so nothing else happens for awhile */
2797 /* Send the In and Ack packets */
2800 /* 15 second delay so nothing else happens for awhile */
2803 /* Restore interrupts */
2804 DWC_WRITE_REG32(&global_regs->
2811 #endif /* DWC_HS_ELECT_TST */
2813 case UHF_PORT_INDICATOR:
2814 DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD HUB CONTROL - "
2815 "SetPortFeature - USB_PORT_FEAT_INDICATOR\n");
2819 retval = -DWC_E_INVALID;
2820 DWC_ERROR("DWC OTG HCD - "
2821 "SetPortFeature request %xh "
2822 "unknown or unsupported\n", wValue);
2826 #ifdef CONFIG_USB_DWC_OTG_LPM
2827 case UCR_SET_AND_TEST_PORT_FEATURE:
2828 if (wValue != UHF_PORT_L1) {
2832 int portnum, hird, devaddr, remwake;
2833 glpmcfg_data_t lpmcfg;
2834 uint32_t time_usecs;
2835 gintsts_data_t gintsts;
2836 gintmsk_data_t gintmsk;
2838 if (!dwc_otg_get_param_lpm_enable(core_if)) {
2841 if (wValue != UHF_PORT_L1 || wLength != 1) {
2844 /* Check if the port currently is in SLEEP state */
2846 DWC_READ_REG32(&core_if->core_global_regs->glpmcfg);
2847 if (lpmcfg.b.prt_sleep_sts) {
2848 DWC_INFO("Port is already in sleep mode\n");
2849 buf[0] = 0; /* Return success */
2853 portnum = wIndex & 0xf;
2854 hird = (wIndex >> 4) & 0xf;
2855 devaddr = (wIndex >> 8) & 0x7f;
2856 remwake = (wIndex >> 15);
2859 retval = -DWC_E_INVALID;
2861 ("Wrong port number(%d) in SetandTestPortFeature request\n",
2867 ("SetandTestPortFeature request: portnum = %d, hird = %d, devaddr = %d, rewake = %d\n",
2868 portnum, hird, devaddr, remwake);
2869 /* Disable LPM interrupt */
2871 gintmsk.b.lpmtranrcvd = 1;
2872 DWC_MODIFY_REG32(&core_if->core_global_regs->gintmsk,
2875 if (dwc_otg_hcd_send_lpm
2876 (dwc_otg_hcd, devaddr, hird, remwake)) {
2877 retval = -DWC_E_INVALID;
2881 time_usecs = 10 * (lpmcfg.b.retry_count + 1);
2882 /* We will consider timeout if time_usecs microseconds pass,
2883 * and we don't receive LPM transaction status.
2884 * After receiving non-error responce(ACK/NYET/STALL) from device,
2885 * core will set lpmtranrcvd bit.
2889 DWC_READ_REG32(&core_if->core_global_regs->
2891 if (gintsts.b.lpmtranrcvd) {
2895 } while (--time_usecs);
2896 /* lpm_int bit will be cleared in LPM interrupt handler */
2903 if (!gintsts.b.lpmtranrcvd) {
2904 buf[0] = 0x3; /* Completion code is Timeout */
2905 dwc_otg_hcd_free_hc_from_lpm(dwc_otg_hcd);
2908 DWC_READ_REG32(&core_if->core_global_regs->
2910 if (lpmcfg.b.lpm_resp == 0x3) {
2911 /* ACK responce from the device */
2912 buf[0] = 0x00; /* Success */
2913 } else if (lpmcfg.b.lpm_resp == 0x2) {
2914 /* NYET responce from the device */
2917 /* Otherwise responce with Timeout */
2921 DWC_PRINTF("Device responce to LPM trans is %x\n",
2923 DWC_MODIFY_REG32(&core_if->core_global_regs->gintmsk, 0,
2928 #endif /* CONFIG_USB_DWC_OTG_LPM */
2931 retval = -DWC_E_INVALID;
2932 DWC_WARN("DWC OTG HCD - "
2933 "Unknown hub control request type or invalid typeReq: %xh wIndex: %xh wValue: %xh\n",
2934 typeReq, wIndex, wValue);
2941 #ifdef CONFIG_USB_DWC_OTG_LPM
2942 /** Returns index of host channel to perform LPM transaction. */
2943 int dwc_otg_hcd_get_hc_for_lpm_tran(dwc_otg_hcd_t *hcd, uint8_t devaddr)
2945 dwc_otg_core_if_t *core_if = hcd->core_if;
2947 hcchar_data_t hcchar;
2948 gintmsk_data_t gintmsk = {.d32 = 0 };
2950 if (DWC_CIRCLEQ_EMPTY(&hcd->free_hc_list)) {
2951 DWC_PRINTF("No free channel to select for LPM transaction\n");
2955 hc = DWC_CIRCLEQ_FIRST(&hcd->free_hc_list);
2957 /* Mask host channel interrupts. */
2958 gintmsk.b.hcintr = 1;
2959 DWC_MODIFY_REG32(&core_if->core_global_regs->gintmsk, gintmsk.d32, 0);
2961 /* Fill fields that core needs for LPM transaction */
2962 hcchar.b.devaddr = devaddr;
2964 hcchar.b.eptype = DWC_OTG_EP_TYPE_CONTROL;
2966 hcchar.b.lspddev = (hc->speed == DWC_OTG_EP_SPEED_LOW);
2967 hcchar.b.epdir = 0; /* OUT */
2968 DWC_WRITE_REG32(&core_if->host_if->hc_regs[hc->hc_num]->hcchar,
2971 /* Remove the host channel from the free list. */
2972 DWC_CIRCLEQ_REMOVE_INIT(&hcd->free_hc_list, hc, hc_list_entry);
2974 DWC_PRINTF("hcnum = %d devaddr = %d\n", hc->hc_num, devaddr);
2979 /** Release hc after performing LPM transaction */
2980 void dwc_otg_hcd_free_hc_from_lpm(dwc_otg_hcd_t *hcd)
2983 glpmcfg_data_t lpmcfg;
2986 lpmcfg.d32 = DWC_READ_REG32(&hcd->core_if->core_global_regs->glpmcfg);
2987 hc_num = lpmcfg.b.lpm_chan_index;
2989 hc = hcd->hc_ptr_array[hc_num];
2991 DWC_PRINTF("Freeing channel %d after LPM\n", hc_num);
2992 /* Return host channel to free list */
2993 DWC_CIRCLEQ_INSERT_TAIL(&hcd->free_hc_list, hc, hc_list_entry);
2996 int dwc_otg_hcd_send_lpm(dwc_otg_hcd_t *hcd, uint8_t devaddr, uint8_t hird,
2997 uint8_t bRemoteWake)
2999 glpmcfg_data_t lpmcfg;
3000 pcgcctl_data_t pcgcctl = {.d32 = 0 };
3003 channel = dwc_otg_hcd_get_hc_for_lpm_tran(hcd, devaddr);
3008 pcgcctl.b.enbl_sleep_gating = 1;
3009 DWC_MODIFY_REG32(hcd->core_if->pcgcctl, 0, pcgcctl.d32);
3011 /* Read LPM config register */
3012 lpmcfg.d32 = DWC_READ_REG32(&hcd->core_if->core_global_regs->glpmcfg);
3014 /* Program LPM transaction fields */
3015 lpmcfg.b.rem_wkup_en = bRemoteWake;
3016 lpmcfg.b.hird = hird;
3018 if (dwc_otg_get_param_besl_enable(hcd->core_if)) {
3019 lpmcfg.b.hird_thres = 0x16;
3020 lpmcfg.b.en_besl = 1;
3022 lpmcfg.b.hird_thres = 0x1c;
3025 lpmcfg.b.lpm_chan_index = channel;
3026 lpmcfg.b.en_utmi_sleep = 1;
3027 /* Program LPM config register */
3028 DWC_WRITE_REG32(&hcd->core_if->core_global_regs->glpmcfg, lpmcfg.d32);
3030 /* Send LPM transaction */
3031 lpmcfg.b.send_lpm = 1;
3032 DWC_WRITE_REG32(&hcd->core_if->core_global_regs->glpmcfg, lpmcfg.d32);
3037 #endif /* CONFIG_USB_DWC_OTG_LPM */
3039 int dwc_otg_hcd_is_status_changed(dwc_otg_hcd_t *hcd, int port)
3044 return -DWC_E_INVALID;
3047 retval = (hcd->flags.b.port_connect_status_change ||
3048 hcd->flags.b.port_reset_change ||
3049 hcd->flags.b.port_enable_change ||
3050 hcd->flags.b.port_suspend_change ||
3051 hcd->flags.b.port_over_current_change);
3054 DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD HUB STATUS DATA:"
3055 " Root port status changed\n");
3056 DWC_DEBUGPL(DBG_HCDV, " port_connect_status_change: %d\n",
3057 hcd->flags.b.port_connect_status_change);
3058 DWC_DEBUGPL(DBG_HCDV, " port_reset_change: %d\n",
3059 hcd->flags.b.port_reset_change);
3060 DWC_DEBUGPL(DBG_HCDV, " port_enable_change: %d\n",
3061 hcd->flags.b.port_enable_change);
3062 DWC_DEBUGPL(DBG_HCDV, " port_suspend_change: %d\n",
3063 hcd->flags.b.port_suspend_change);
3064 DWC_DEBUGPL(DBG_HCDV, " port_over_current_change: %d\n",
3065 hcd->flags.b.port_over_current_change);
3071 int dwc_otg_hcd_get_frame_number(dwc_otg_hcd_t *dwc_otg_hcd)
3075 DWC_READ_REG32(&dwc_otg_hcd->core_if->host_if->
3076 host_global_regs->hfnum);
3079 DWC_DEBUGPL(DBG_HCDV, "DWC OTG HCD GET FRAME NUMBER %d\n",
3082 return hfnum.b.frnum;
3085 int dwc_otg_hcd_start(dwc_otg_hcd_t *hcd,
3086 struct dwc_otg_hcd_function_ops *fops)
3091 if (!dwc_otg_is_device_mode(hcd->core_if) &&
3092 (!hcd->core_if->adp_enable || hcd->core_if->adp.adp_started)) {
3093 dwc_otg_hcd_reinit(hcd);
3095 retval = -DWC_E_NO_DEVICE;
3101 void *dwc_otg_hcd_get_priv_data(dwc_otg_hcd_t *hcd)
3106 void dwc_otg_hcd_set_priv_data(dwc_otg_hcd_t *hcd, void *priv_data)
3108 hcd->priv = priv_data;
3111 uint32_t dwc_otg_hcd_otg_port(dwc_otg_hcd_t *hcd)
3113 return hcd->otg_port;
3116 uint32_t dwc_otg_hcd_is_b_host(dwc_otg_hcd_t *hcd)
3119 if (hcd->core_if->op_state == B_HOST) {
3128 dwc_otg_hcd_urb_t *dwc_otg_hcd_urb_alloc(dwc_otg_hcd_t *hcd,
3129 int iso_desc_count, int atomic_alloc)
3131 dwc_otg_hcd_urb_t *dwc_otg_urb;
3135 sizeof(*dwc_otg_urb) +
3136 iso_desc_count * sizeof(struct dwc_otg_hcd_iso_packet_desc);
3138 dwc_otg_urb = DWC_ALLOC_ATOMIC(size);
3140 dwc_otg_urb = DWC_ALLOC(size);
3142 dwc_otg_urb->packet_count = iso_desc_count;
3147 void dwc_otg_hcd_urb_set_pipeinfo(dwc_otg_hcd_urb_t *dwc_otg_urb,
3148 uint8_t dev_addr, uint8_t ep_num,
3149 uint8_t ep_type, uint8_t ep_dir, uint16_t mps)
3151 dwc_otg_hcd_fill_pipe(&dwc_otg_urb->pipe_info, dev_addr, ep_num,
3152 ep_type, ep_dir, mps);
3155 ("addr = %d, ep_num = %d, ep_dir = 0x%x, ep_type = 0x%x, mps = %d\n",
3156 dev_addr, ep_num, ep_dir, ep_type, mps);
3160 void dwc_otg_hcd_urb_set_params(dwc_otg_hcd_urb_t *dwc_otg_urb,
3161 void *urb_handle, void *buf, dwc_dma_t dma,
3162 uint32_t buflen, void *setup_packet,
3163 dwc_dma_t setup_dma, uint32_t flags,
3166 dwc_otg_urb->priv = urb_handle;
3167 dwc_otg_urb->buf = buf;
3168 dwc_otg_urb->dma = dma;
3169 dwc_otg_urb->length = buflen;
3170 dwc_otg_urb->setup_packet = setup_packet;
3171 dwc_otg_urb->setup_dma = setup_dma;
3172 dwc_otg_urb->flags = flags;
3173 dwc_otg_urb->interval = interval;
3174 dwc_otg_urb->status = -DWC_E_IN_PROGRESS;
3177 uint32_t dwc_otg_hcd_urb_get_status(dwc_otg_hcd_urb_t *dwc_otg_urb)
3179 return dwc_otg_urb->status;
3182 uint32_t dwc_otg_hcd_urb_get_actual_length(dwc_otg_hcd_urb_t *dwc_otg_urb)
3184 return dwc_otg_urb->actual_length;
3187 uint32_t dwc_otg_hcd_urb_get_error_count(dwc_otg_hcd_urb_t *dwc_otg_urb)
3189 return dwc_otg_urb->error_count;
3192 void dwc_otg_hcd_urb_set_iso_desc_params(dwc_otg_hcd_urb_t *dwc_otg_urb,
3193 int desc_num, uint32_t offset,
3196 dwc_otg_urb->iso_descs[desc_num].offset = offset;
3197 dwc_otg_urb->iso_descs[desc_num].length = length;
3200 uint32_t dwc_otg_hcd_urb_get_iso_desc_status(dwc_otg_hcd_urb_t *dwc_otg_urb,
3203 return dwc_otg_urb->iso_descs[desc_num].status;
3206 uint32_t dwc_otg_hcd_urb_get_iso_desc_actual_length(dwc_otg_hcd_urb_t *
3207 dwc_otg_urb, int desc_num)
3209 return dwc_otg_urb->iso_descs[desc_num].actual_length;
3212 int dwc_otg_hcd_is_bandwidth_allocated(dwc_otg_hcd_t *hcd, void *ep_handle)
3215 dwc_otg_qh_t *qh = (dwc_otg_qh_t *) ep_handle;
3218 if (!DWC_LIST_EMPTY(&qh->qh_list_entry)) {
3225 int dwc_otg_hcd_is_bandwidth_freed(dwc_otg_hcd_t *hcd, void *ep_handle)
3227 dwc_otg_qh_t *qh = (dwc_otg_qh_t *) ep_handle;
3229 DWC_ASSERT(qh, "qh is not allocated\n");
3231 if (DWC_LIST_EMPTY(&qh->qh_list_entry)) {
3238 uint8_t dwc_otg_hcd_get_ep_bandwidth(dwc_otg_hcd_t *hcd, void *ep_handle)
3240 dwc_otg_qh_t *qh = (dwc_otg_qh_t *) ep_handle;
3241 DWC_ASSERT(qh, "qh is not allocated\n");
3245 void dwc_otg_hcd_dump_state(dwc_otg_hcd_t *hcd)
3250 gnptxsts_data_t np_tx_status;
3251 hptxsts_data_t p_tx_status;
3253 num_channels = hcd->core_if->core_params->host_channels;
3256 ("************************************************************\n");
3257 DWC_PRINTF("HCD State:\n");
3258 DWC_PRINTF(" Num channels: %d\n", num_channels);
3259 for (i = 0; i < num_channels; i++) {
3260 dwc_hc_t *hc = hcd->hc_ptr_array[i];
3261 DWC_PRINTF(" Channel %d:\n", i);
3262 DWC_PRINTF(" dev_addr: %d, ep_num: %d, ep_is_in: %d\n",
3263 hc->dev_addr, hc->ep_num, hc->ep_is_in);
3264 DWC_PRINTF(" speed: %d\n", hc->speed);
3265 DWC_PRINTF(" ep_type: %d\n", hc->ep_type);
3266 DWC_PRINTF(" max_packet: %d\n", hc->max_packet);
3267 DWC_PRINTF(" data_pid_start: %d\n", hc->data_pid_start);
3268 DWC_PRINTF(" multi_count: %d\n", hc->multi_count);
3269 DWC_PRINTF(" xfer_started: %d\n", hc->xfer_started);
3270 DWC_PRINTF(" xfer_buff: %p\n", hc->xfer_buff);
3271 DWC_PRINTF(" xfer_len: %d\n", hc->xfer_len);
3272 DWC_PRINTF(" xfer_count: %d\n", hc->xfer_count);
3273 DWC_PRINTF(" halt_on_queue: %d\n", hc->halt_on_queue);
3274 DWC_PRINTF(" halt_pending: %d\n", hc->halt_pending);
3275 DWC_PRINTF(" halt_status: %d\n", hc->halt_status);
3276 DWC_PRINTF(" do_split: %d\n", hc->do_split);
3277 DWC_PRINTF(" complete_split: %d\n", hc->complete_split);
3278 DWC_PRINTF(" hub_addr: %d\n", hc->hub_addr);
3279 DWC_PRINTF(" port_addr: %d\n", hc->port_addr);
3280 DWC_PRINTF(" xact_pos: %d\n", hc->xact_pos);
3281 DWC_PRINTF(" requests: %d\n", hc->requests);
3282 DWC_PRINTF(" qh: %p\n", hc->qh);
3283 if (hc->xfer_started) {
3285 hcchar_data_t hcchar;
3286 hctsiz_data_t hctsiz;
3288 hcintmsk_data_t hcintmsk;
3290 DWC_READ_REG32(&hcd->core_if->host_if->
3291 host_global_regs->hfnum);
3293 DWC_READ_REG32(&hcd->core_if->host_if->hc_regs[i]->
3296 DWC_READ_REG32(&hcd->core_if->host_if->hc_regs[i]->
3299 DWC_READ_REG32(&hcd->core_if->host_if->hc_regs[i]->
3302 DWC_READ_REG32(&hcd->core_if->host_if->hc_regs[i]->
3304 DWC_PRINTF(" hfnum: 0x%08x\n", hfnum.d32);
3305 DWC_PRINTF(" hcchar: 0x%08x\n", hcchar.d32);
3306 DWC_PRINTF(" hctsiz: 0x%08x\n", hctsiz.d32);
3307 DWC_PRINTF(" hcint: 0x%08x\n", hcint.d32);
3308 DWC_PRINTF(" hcintmsk: 0x%08x\n", hcintmsk.d32);
3310 if (hc->xfer_started && hc->qh) {
3312 dwc_otg_hcd_urb_t *urb;
3314 DWC_CIRCLEQ_FOREACH(qtd, &hc->qh->qtd_list,
3316 if (!qtd->in_process)
3320 DWC_PRINTF(" URB Info:\n");
3321 DWC_PRINTF(" qtd: %p, urb: %p\n", qtd,
3324 DWC_PRINTF(" Dev: %d, EP: %d %s\n",
3325 dwc_otg_hcd_get_dev_addr
3327 dwc_otg_hcd_get_ep_num
3329 dwc_otg_hcd_is_pipe_in
3330 (&urb->pipe_info) ? "IN" :
3333 (" Max packet size: %d\n",
3337 (" transfer_buffer: %p\n",
3339 DWC_PRINTF(" transfer_dma: %p\n",
3342 (" transfer_buffer_length: %d\n",
3344 DWC_PRINTF(" actual_length: %d\n",
3345 urb->actual_length);
3350 DWC_PRINTF(" non_periodic_channels: %d\n", hcd->non_periodic_channels);
3351 DWC_PRINTF(" periodic_channels: %d\n", hcd->periodic_channels);
3352 DWC_PRINTF(" periodic_usecs: %d\n", hcd->periodic_usecs);
3354 DWC_READ_REG32(&hcd->core_if->core_global_regs->gnptxsts);
3355 DWC_PRINTF(" NP Tx Req Queue Space Avail: %d\n",
3356 np_tx_status.b.nptxqspcavail);
3357 DWC_PRINTF(" NP Tx FIFO Space Avail: %d\n",
3358 np_tx_status.b.nptxfspcavail);
3360 DWC_READ_REG32(&hcd->core_if->host_if->host_global_regs->hptxsts);
3361 DWC_PRINTF(" P Tx Req Queue Space Avail: %d\n",
3362 p_tx_status.b.ptxqspcavail);
3363 DWC_PRINTF(" P Tx FIFO Space Avail: %d\n", p_tx_status.b.ptxfspcavail);
3364 dwc_otg_hcd_dump_frrem(hcd);
3365 dwc_otg_dump_global_registers(hcd->core_if);
3366 dwc_otg_dump_host_registers(hcd->core_if);
3368 ("************************************************************\n");
3374 void dwc_print_setup_data(uint8_t *setup)
3377 if (CHK_DEBUG_LEVEL(DBG_HCD)) {
3378 DWC_PRINTF("Setup Data = MSB ");
3379 for (i = 7; i >= 0; i--)
3380 DWC_PRINTF("%02x ", setup[i]);
3382 DWC_PRINTF(" bmRequestType Tranfer = %s\n",
3383 (setup[0] & 0x80) ? "Device-to-Host" :
3385 DWC_PRINTF(" bmRequestType Type = ");
3386 switch ((setup[0] & 0x60) >> 5) {
3388 DWC_PRINTF("Standard\n");
3391 DWC_PRINTF("Class\n");
3394 DWC_PRINTF("Vendor\n");
3397 DWC_PRINTF("Reserved\n");
3400 DWC_PRINTF(" bmRequestType Recipient = ");
3401 switch (setup[0] & 0x1f) {
3403 DWC_PRINTF("Device\n");
3406 DWC_PRINTF("Interface\n");
3409 DWC_PRINTF("Endpoint\n");
3412 DWC_PRINTF("Other\n");
3415 DWC_PRINTF("Reserved\n");
3418 DWC_PRINTF(" bRequest = 0x%0x\n", setup[1]);
3419 DWC_PRINTF(" wValue = 0x%0x\n", *((uint16_t *)&setup[2]));
3420 DWC_PRINTF(" wIndex = 0x%0x\n", *((uint16_t *)&setup[4]));
3421 DWC_PRINTF(" wLength = 0x%0x\n\n", *((uint16_t *)&setup[6]));
3426 void dwc_otg_hcd_dump_frrem(dwc_otg_hcd_t *hcd)
3429 DWC_PRINTF("Frame remaining at SOF:\n");
3430 DWC_PRINTF(" samples %u, accum %llu, avg %llu\n",
3431 hcd->frrem_samples, hcd->frrem_accum,
3432 (hcd->frrem_samples > 0) ?
3433 hcd->frrem_accum / hcd->frrem_samples : 0);
3436 DWC_PRINTF("Frame remaining at start_transfer (uframe 7):\n");
3437 DWC_PRINTF(" samples %u, accum %llu, avg %llu\n",
3438 hcd->core_if->hfnum_7_samples,
3439 hcd->core_if->hfnum_7_frrem_accum,
3440 (hcd->core_if->hfnum_7_samples >
3441 0) ? hcd->core_if->hfnum_7_frrem_accum /
3442 hcd->core_if->hfnum_7_samples : 0);
3443 DWC_PRINTF("Frame remaining at start_transfer (uframe 0):\n");
3444 DWC_PRINTF(" samples %u, accum %llu, avg %llu\n",
3445 hcd->core_if->hfnum_0_samples,
3446 hcd->core_if->hfnum_0_frrem_accum,
3447 (hcd->core_if->hfnum_0_samples >
3448 0) ? hcd->core_if->hfnum_0_frrem_accum /
3449 hcd->core_if->hfnum_0_samples : 0);
3450 DWC_PRINTF("Frame remaining at start_transfer (uframe 1-6):\n");
3451 DWC_PRINTF(" samples %u, accum %llu, avg %llu\n",
3452 hcd->core_if->hfnum_other_samples,
3453 hcd->core_if->hfnum_other_frrem_accum,
3454 (hcd->core_if->hfnum_other_samples >
3455 0) ? hcd->core_if->hfnum_other_frrem_accum /
3456 hcd->core_if->hfnum_other_samples : 0);
3459 DWC_PRINTF("Frame remaining at sample point A (uframe 7):\n");
3460 DWC_PRINTF(" samples %u, accum %llu, avg %llu\n",
3461 hcd->hfnum_7_samples_a, hcd->hfnum_7_frrem_accum_a,
3462 (hcd->hfnum_7_samples_a > 0) ?
3463 hcd->hfnum_7_frrem_accum_a / hcd->hfnum_7_samples_a : 0);
3464 DWC_PRINTF("Frame remaining at sample point A (uframe 0):\n");
3465 DWC_PRINTF(" samples %u, accum %llu, avg %llu\n",
3466 hcd->hfnum_0_samples_a, hcd->hfnum_0_frrem_accum_a,
3467 (hcd->hfnum_0_samples_a > 0) ?
3468 hcd->hfnum_0_frrem_accum_a / hcd->hfnum_0_samples_a : 0);
3469 DWC_PRINTF("Frame remaining at sample point A (uframe 1-6):\n");
3470 DWC_PRINTF(" samples %u, accum %llu, avg %llu\n",
3471 hcd->hfnum_other_samples_a, hcd->hfnum_other_frrem_accum_a,
3472 (hcd->hfnum_other_samples_a > 0) ?
3473 hcd->hfnum_other_frrem_accum_a /
3474 hcd->hfnum_other_samples_a : 0);
3477 DWC_PRINTF("Frame remaining at sample point B (uframe 7):\n");
3478 DWC_PRINTF(" samples %u, accum %llu, avg %llu\n",
3479 hcd->hfnum_7_samples_b, hcd->hfnum_7_frrem_accum_b,
3480 (hcd->hfnum_7_samples_b > 0) ?
3481 hcd->hfnum_7_frrem_accum_b / hcd->hfnum_7_samples_b : 0);
3482 DWC_PRINTF("Frame remaining at sample point B (uframe 0):\n");
3483 DWC_PRINTF(" samples %u, accum %llu, avg %llu\n",
3484 hcd->hfnum_0_samples_b, hcd->hfnum_0_frrem_accum_b,
3485 (hcd->hfnum_0_samples_b > 0) ?
3486 hcd->hfnum_0_frrem_accum_b / hcd->hfnum_0_samples_b : 0);
3487 DWC_PRINTF("Frame remaining at sample point B (uframe 1-6):\n");
3488 DWC_PRINTF(" samples %u, accum %llu, avg %llu\n",
3489 hcd->hfnum_other_samples_b, hcd->hfnum_other_frrem_accum_b,
3490 (hcd->hfnum_other_samples_b > 0) ?
3491 hcd->hfnum_other_frrem_accum_b /
3492 hcd->hfnum_other_samples_b : 0);
3496 #endif /* DWC_DEVICE_ONLY */