2 * hcd.c - DesignWare HS OTG Controller host-mode routines
4 * Copyright (C) 2004-2013 Synopsys, Inc.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions, and the following disclaimer,
11 * without modification.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. The names of the above-listed copyright holders may not be used
16 * to endorse or promote products derived from this software without
17 * specific prior written permission.
19 * ALTERNATIVELY, this software may be distributed under the terms of the
20 * GNU General Public License ("GPL") as published by the Free Software
21 * Foundation; either version 2 of the License, or (at your option) any
24 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
25 * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
26 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
27 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
28 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
29 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
30 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
31 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
32 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
33 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
34 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
38 * This file contains the core HCD code, and implements the Linux hc_driver
41 #include <linux/kernel.h>
42 #include <linux/module.h>
43 #include <linux/spinlock.h>
44 #include <linux/interrupt.h>
45 #include <linux/dma-mapping.h>
46 #include <linux/delay.h>
48 #include <linux/slab.h>
49 #include <linux/usb.h>
51 #include <linux/usb/hcd.h>
52 #include <linux/usb/ch11.h>
58 * dwc2_dump_channel_info() - Prints the state of a host channel
60 * @hsotg: Programming view of DWC_otg controller
61 * @chan: Pointer to the channel to dump
63 * Must be called with interrupt disabled and spinlock held
65 * NOTE: This function will be removed once the peripheral controller code
66 * is integrated and the driver is stable
68 static void dwc2_dump_channel_info(struct dwc2_hsotg
*hsotg
,
69 struct dwc2_host_chan
*chan
)
72 int num_channels
= hsotg
->core_params
->host_channels
;
83 hcchar
= readl(hsotg
->regs
+ HCCHAR(chan
->hc_num
));
84 hcsplt
= readl(hsotg
->regs
+ HCSPLT(chan
->hc_num
));
85 hctsiz
= readl(hsotg
->regs
+ HCTSIZ(chan
->hc_num
));
86 hc_dma
= readl(hsotg
->regs
+ HCDMA(chan
->hc_num
));
88 dev_dbg(hsotg
->dev
, " Assigned to channel %p:\n", chan
);
89 dev_dbg(hsotg
->dev
, " hcchar 0x%08x, hcsplt 0x%08x\n",
91 dev_dbg(hsotg
->dev
, " hctsiz 0x%08x, hc_dma 0x%08x\n",
93 dev_dbg(hsotg
->dev
, " dev_addr: %d, ep_num: %d, ep_is_in: %d\n",
94 chan
->dev_addr
, chan
->ep_num
, chan
->ep_is_in
);
95 dev_dbg(hsotg
->dev
, " ep_type: %d\n", chan
->ep_type
);
96 dev_dbg(hsotg
->dev
, " max_packet: %d\n", chan
->max_packet
);
97 dev_dbg(hsotg
->dev
, " data_pid_start: %d\n", chan
->data_pid_start
);
98 dev_dbg(hsotg
->dev
, " xfer_started: %d\n", chan
->xfer_started
);
99 dev_dbg(hsotg
->dev
, " halt_status: %d\n", chan
->halt_status
);
100 dev_dbg(hsotg
->dev
, " xfer_buf: %p\n", chan
->xfer_buf
);
101 dev_dbg(hsotg
->dev
, " xfer_dma: %08lx\n",
102 (unsigned long)chan
->xfer_dma
);
103 dev_dbg(hsotg
->dev
, " xfer_len: %d\n", chan
->xfer_len
);
104 dev_dbg(hsotg
->dev
, " qh: %p\n", chan
->qh
);
105 dev_dbg(hsotg
->dev
, " NP inactive sched:\n");
106 list_for_each_entry(qh
, &hsotg
->non_periodic_sched_inactive
,
108 dev_dbg(hsotg
->dev
, " %p\n", qh
);
109 dev_dbg(hsotg
->dev
, " NP active sched:\n");
110 list_for_each_entry(qh
, &hsotg
->non_periodic_sched_active
,
112 dev_dbg(hsotg
->dev
, " %p\n", qh
);
113 dev_dbg(hsotg
->dev
, " Channels:\n");
114 for (i
= 0; i
< num_channels
; i
++) {
115 struct dwc2_host_chan
*chan
= hsotg
->hc_ptr_array
[i
];
117 dev_dbg(hsotg
->dev
, " %2d: %p\n", i
, chan
);
119 #endif /* VERBOSE_DEBUG */
123 * Processes all the URBs in a single list of QHs. Completes them with
124 * -ETIMEDOUT and frees the QTD.
126 * Must be called with interrupt disabled and spinlock held
128 static void dwc2_kill_urbs_in_qh_list(struct dwc2_hsotg
*hsotg
,
129 struct list_head
*qh_list
)
131 struct dwc2_qh
*qh
, *qh_tmp
;
132 struct dwc2_qtd
*qtd
, *qtd_tmp
;
134 list_for_each_entry_safe(qh
, qh_tmp
, qh_list
, qh_list_entry
) {
135 list_for_each_entry_safe(qtd
, qtd_tmp
, &qh
->qtd_list
,
137 dwc2_host_complete(hsotg
, qtd
, -ETIMEDOUT
);
138 dwc2_hcd_qtd_unlink_and_free(hsotg
, qtd
, qh
);
143 static void dwc2_qh_list_free(struct dwc2_hsotg
*hsotg
,
144 struct list_head
*qh_list
)
146 struct dwc2_qtd
*qtd
, *qtd_tmp
;
147 struct dwc2_qh
*qh
, *qh_tmp
;
151 /* The list hasn't been initialized yet */
154 spin_lock_irqsave(&hsotg
->lock
, flags
);
156 /* Ensure there are no QTDs or URBs left */
157 dwc2_kill_urbs_in_qh_list(hsotg
, qh_list
);
159 list_for_each_entry_safe(qh
, qh_tmp
, qh_list
, qh_list_entry
) {
160 dwc2_hcd_qh_unlink(hsotg
, qh
);
162 /* Free each QTD in the QH's QTD list */
163 list_for_each_entry_safe(qtd
, qtd_tmp
, &qh
->qtd_list
,
165 dwc2_hcd_qtd_unlink_and_free(hsotg
, qtd
, qh
);
167 spin_unlock_irqrestore(&hsotg
->lock
, flags
);
168 dwc2_hcd_qh_free(hsotg
, qh
);
169 spin_lock_irqsave(&hsotg
->lock
, flags
);
172 spin_unlock_irqrestore(&hsotg
->lock
, flags
);
176 * Responds with an error status of -ETIMEDOUT to all URBs in the non-periodic
177 * and periodic schedules. The QTD associated with each URB is removed from
178 * the schedule and freed. This function may be called when a disconnect is
179 * detected or when the HCD is being stopped.
181 * Must be called with interrupt disabled and spinlock held
183 static void dwc2_kill_all_urbs(struct dwc2_hsotg
*hsotg
)
185 dwc2_kill_urbs_in_qh_list(hsotg
, &hsotg
->non_periodic_sched_inactive
);
186 dwc2_kill_urbs_in_qh_list(hsotg
, &hsotg
->non_periodic_sched_active
);
187 dwc2_kill_urbs_in_qh_list(hsotg
, &hsotg
->periodic_sched_inactive
);
188 dwc2_kill_urbs_in_qh_list(hsotg
, &hsotg
->periodic_sched_ready
);
189 dwc2_kill_urbs_in_qh_list(hsotg
, &hsotg
->periodic_sched_assigned
);
190 dwc2_kill_urbs_in_qh_list(hsotg
, &hsotg
->periodic_sched_queued
);
194 * dwc2_hcd_start() - Starts the HCD when switching to Host mode
196 * @hsotg: Pointer to struct dwc2_hsotg
198 void dwc2_hcd_start(struct dwc2_hsotg
*hsotg
)
202 if (hsotg
->op_state
== OTG_STATE_B_HOST
) {
204 * Reset the port. During a HNP mode switch the reset
205 * needs to occur within 1ms and have a duration of at
208 hprt0
= dwc2_read_hprt0(hsotg
);
210 writel(hprt0
, hsotg
->regs
+ HPRT0
);
213 queue_delayed_work(hsotg
->wq_otg
, &hsotg
->start_work
,
214 msecs_to_jiffies(50));
217 /* Must be called with interrupt disabled and spinlock held */
218 static void dwc2_hcd_cleanup_channels(struct dwc2_hsotg
*hsotg
)
220 int num_channels
= hsotg
->core_params
->host_channels
;
221 struct dwc2_host_chan
*channel
;
225 if (hsotg
->core_params
->dma_enable
<= 0) {
226 /* Flush out any channel requests in slave mode */
227 for (i
= 0; i
< num_channels
; i
++) {
228 channel
= hsotg
->hc_ptr_array
[i
];
229 if (!list_empty(&channel
->hc_list_entry
))
231 hcchar
= readl(hsotg
->regs
+ HCCHAR(i
));
232 if (hcchar
& HCCHAR_CHENA
) {
233 hcchar
&= ~(HCCHAR_CHENA
| HCCHAR_EPDIR
);
234 hcchar
|= HCCHAR_CHDIS
;
235 writel(hcchar
, hsotg
->regs
+ HCCHAR(i
));
240 for (i
= 0; i
< num_channels
; i
++) {
241 channel
= hsotg
->hc_ptr_array
[i
];
242 if (!list_empty(&channel
->hc_list_entry
))
244 hcchar
= readl(hsotg
->regs
+ HCCHAR(i
));
245 if (hcchar
& HCCHAR_CHENA
) {
246 /* Halt the channel */
247 hcchar
|= HCCHAR_CHDIS
;
248 writel(hcchar
, hsotg
->regs
+ HCCHAR(i
));
251 dwc2_hc_cleanup(hsotg
, channel
);
252 list_add_tail(&channel
->hc_list_entry
, &hsotg
->free_hc_list
);
254 * Added for Descriptor DMA to prevent channel double cleanup in
255 * release_channel_ddma(), which is called from ep_disable when
263 * dwc2_hcd_disconnect() - Handles disconnect of the HCD
265 * @hsotg: Pointer to struct dwc2_hsotg
267 * Must be called with interrupt disabled and spinlock held
269 void dwc2_hcd_disconnect(struct dwc2_hsotg
*hsotg
)
273 /* Set status flags for the hub driver */
274 hsotg
->flags
.b
.port_connect_status_change
= 1;
275 hsotg
->flags
.b
.port_connect_status
= 0;
278 * Shutdown any transfers in process by clearing the Tx FIFO Empty
279 * interrupt mask and status bits and disabling subsequent host
280 * channel interrupts.
282 intr
= readl(hsotg
->regs
+ GINTMSK
);
283 intr
&= ~(GINTSTS_NPTXFEMP
| GINTSTS_PTXFEMP
| GINTSTS_HCHINT
);
284 writel(intr
, hsotg
->regs
+ GINTMSK
);
285 intr
= GINTSTS_NPTXFEMP
| GINTSTS_PTXFEMP
| GINTSTS_HCHINT
;
286 writel(intr
, hsotg
->regs
+ GINTSTS
);
289 * Turn off the vbus power only if the core has transitioned to device
290 * mode. If still in host mode, need to keep power on to detect a
293 if (dwc2_is_device_mode(hsotg
)) {
294 if (hsotg
->op_state
!= OTG_STATE_A_SUSPEND
) {
295 dev_dbg(hsotg
->dev
, "Disconnect: PortPower off\n");
296 writel(0, hsotg
->regs
+ HPRT0
);
299 dwc2_disable_host_interrupts(hsotg
);
302 /* Respond with an error status to all URBs in the schedule */
303 dwc2_kill_all_urbs(hsotg
);
305 if (dwc2_is_host_mode(hsotg
))
306 /* Clean up any host channels that were in use */
307 dwc2_hcd_cleanup_channels(hsotg
);
309 dwc2_host_disconnect(hsotg
);
313 * dwc2_hcd_rem_wakeup() - Handles Remote Wakeup
315 * @hsotg: Pointer to struct dwc2_hsotg
317 static void dwc2_hcd_rem_wakeup(struct dwc2_hsotg
*hsotg
)
319 if (hsotg
->lx_state
== DWC2_L2
)
320 hsotg
->flags
.b
.port_suspend_change
= 1;
322 hsotg
->flags
.b
.port_l1_change
= 1;
326 * dwc2_hcd_stop() - Halts the DWC_otg host mode operations in a clean manner
328 * @hsotg: Pointer to struct dwc2_hsotg
330 * Must be called with interrupt disabled and spinlock held
332 void dwc2_hcd_stop(struct dwc2_hsotg
*hsotg
)
334 dev_dbg(hsotg
->dev
, "DWC OTG HCD STOP\n");
337 * The root hub should be disconnected before this function is called.
338 * The disconnect will clear the QTD lists (via ..._hcd_urb_dequeue)
339 * and the QH lists (via ..._hcd_endpoint_disable).
342 /* Turn off all host-specific interrupts */
343 dwc2_disable_host_interrupts(hsotg
);
345 /* Turn off the vbus power */
346 dev_dbg(hsotg
->dev
, "PortPower off\n");
347 writel(0, hsotg
->regs
+ HPRT0
);
350 static int dwc2_hcd_urb_enqueue(struct dwc2_hsotg
*hsotg
,
351 struct dwc2_hcd_urb
*urb
, void **ep_handle
,
354 struct dwc2_qtd
*qtd
;
360 if (!hsotg
->flags
.b
.port_connect_status
) {
361 /* No longer connected */
362 dev_err(hsotg
->dev
, "Not connected\n");
366 dev_speed
= dwc2_host_get_speed(hsotg
, urb
->priv
);
368 /* Some configurations cannot support LS traffic on a FS root port */
369 if ((dev_speed
== USB_SPEED_LOW
) &&
370 (hsotg
->hw_params
.fs_phy_type
== GHWCFG2_FS_PHY_TYPE_DEDICATED
) &&
371 (hsotg
->hw_params
.hs_phy_type
== GHWCFG2_HS_PHY_TYPE_UTMI
)) {
372 u32 hprt0
= readl(hsotg
->regs
+ HPRT0
);
373 u32 prtspd
= (hprt0
& HPRT0_SPD_MASK
) >> HPRT0_SPD_SHIFT
;
375 if (prtspd
== HPRT0_SPD_FULL_SPEED
)
379 qtd
= kzalloc(sizeof(*qtd
), mem_flags
);
383 dwc2_hcd_qtd_init(qtd
, urb
);
384 retval
= dwc2_hcd_qtd_add(hsotg
, qtd
, (struct dwc2_qh
**)ep_handle
,
388 "DWC OTG HCD URB Enqueue failed adding QTD. Error status %d\n",
394 intr_mask
= readl(hsotg
->regs
+ GINTMSK
);
395 if (!(intr_mask
& GINTSTS_SOF
)) {
396 enum dwc2_transaction_type tr_type
;
398 if (qtd
->qh
->ep_type
== USB_ENDPOINT_XFER_BULK
&&
399 !(qtd
->urb
->flags
& URB_GIVEBACK_ASAP
))
401 * Do not schedule SG transactions until qtd has
402 * URB_GIVEBACK_ASAP set
406 spin_lock_irqsave(&hsotg
->lock
, flags
);
407 tr_type
= dwc2_hcd_select_transactions(hsotg
);
408 if (tr_type
!= DWC2_TRANSACTION_NONE
)
409 dwc2_hcd_queue_transactions(hsotg
, tr_type
);
410 spin_unlock_irqrestore(&hsotg
->lock
, flags
);
416 /* Must be called with interrupt disabled and spinlock held */
417 static int dwc2_hcd_urb_dequeue(struct dwc2_hsotg
*hsotg
,
418 struct dwc2_hcd_urb
*urb
)
421 struct dwc2_qtd
*urb_qtd
;
425 dev_dbg(hsotg
->dev
, "## Urb QTD is NULL ##\n");
431 dev_dbg(hsotg
->dev
, "## Urb QTD QH is NULL ##\n");
437 if (urb_qtd
->in_process
&& qh
->channel
) {
438 dwc2_dump_channel_info(hsotg
, qh
->channel
);
440 /* The QTD is in process (it has been assigned to a channel) */
441 if (hsotg
->flags
.b
.port_connect_status
)
443 * If still connected (i.e. in host mode), halt the
444 * channel so it can be used for other transfers. If
445 * no longer connected, the host registers can't be
446 * written to halt the channel since the core is in
449 dwc2_hc_halt(hsotg
, qh
->channel
,
450 DWC2_HC_XFER_URB_DEQUEUE
);
454 * Free the QTD and clean up the associated QH. Leave the QH in the
455 * schedule if it has any remaining QTDs.
457 if (hsotg
->core_params
->dma_desc_enable
<= 0) {
458 u8 in_process
= urb_qtd
->in_process
;
460 dwc2_hcd_qtd_unlink_and_free(hsotg
, urb_qtd
, qh
);
462 dwc2_hcd_qh_deactivate(hsotg
, qh
, 0);
464 } else if (list_empty(&qh
->qtd_list
)) {
465 dwc2_hcd_qh_unlink(hsotg
, qh
);
468 dwc2_hcd_qtd_unlink_and_free(hsotg
, urb_qtd
, qh
);
474 /* Must NOT be called with interrupt disabled or spinlock held */
475 static int dwc2_hcd_endpoint_disable(struct dwc2_hsotg
*hsotg
,
476 struct usb_host_endpoint
*ep
, int retry
)
478 struct dwc2_qtd
*qtd
, *qtd_tmp
;
483 spin_lock_irqsave(&hsotg
->lock
, flags
);
491 while (!list_empty(&qh
->qtd_list
) && retry
--) {
494 "## timeout in dwc2_hcd_endpoint_disable() ##\n");
499 spin_unlock_irqrestore(&hsotg
->lock
, flags
);
500 usleep_range(20000, 40000);
501 spin_lock_irqsave(&hsotg
->lock
, flags
);
509 dwc2_hcd_qh_unlink(hsotg
, qh
);
511 /* Free each QTD in the QH's QTD list */
512 list_for_each_entry_safe(qtd
, qtd_tmp
, &qh
->qtd_list
, qtd_list_entry
)
513 dwc2_hcd_qtd_unlink_and_free(hsotg
, qtd
, qh
);
516 spin_unlock_irqrestore(&hsotg
->lock
, flags
);
517 dwc2_hcd_qh_free(hsotg
, qh
);
523 spin_unlock_irqrestore(&hsotg
->lock
, flags
);
528 /* Must be called with interrupt disabled and spinlock held */
529 static int dwc2_hcd_endpoint_reset(struct dwc2_hsotg
*hsotg
,
530 struct usb_host_endpoint
*ep
)
532 struct dwc2_qh
*qh
= ep
->hcpriv
;
537 qh
->data_toggle
= DWC2_HC_PID_DATA0
;
543 * Initializes dynamic portions of the DWC_otg HCD state
545 * Must be called with interrupt disabled and spinlock held
547 static void dwc2_hcd_reinit(struct dwc2_hsotg
*hsotg
)
549 struct dwc2_host_chan
*chan
, *chan_tmp
;
553 hsotg
->flags
.d32
= 0;
554 hsotg
->non_periodic_qh_ptr
= &hsotg
->non_periodic_sched_active
;
556 if (hsotg
->core_params
->uframe_sched
> 0) {
557 hsotg
->available_host_channels
=
558 hsotg
->core_params
->host_channels
;
560 hsotg
->non_periodic_channels
= 0;
561 hsotg
->periodic_channels
= 0;
565 * Put all channels in the free channel list and clean up channel
568 list_for_each_entry_safe(chan
, chan_tmp
, &hsotg
->free_hc_list
,
570 list_del_init(&chan
->hc_list_entry
);
572 num_channels
= hsotg
->core_params
->host_channels
;
573 for (i
= 0; i
< num_channels
; i
++) {
574 chan
= hsotg
->hc_ptr_array
[i
];
575 list_add_tail(&chan
->hc_list_entry
, &hsotg
->free_hc_list
);
576 dwc2_hc_cleanup(hsotg
, chan
);
579 /* Initialize the DWC core for host mode operation */
580 dwc2_core_host_init(hsotg
);
583 static void dwc2_hc_init_split(struct dwc2_hsotg
*hsotg
,
584 struct dwc2_host_chan
*chan
,
585 struct dwc2_qtd
*qtd
, struct dwc2_hcd_urb
*urb
)
587 int hub_addr
, hub_port
;
590 chan
->xact_pos
= qtd
->isoc_split_pos
;
591 chan
->complete_split
= qtd
->complete_split
;
592 dwc2_host_hub_info(hsotg
, urb
->priv
, &hub_addr
, &hub_port
);
593 chan
->hub_addr
= (u8
)hub_addr
;
594 chan
->hub_port
= (u8
)hub_port
;
597 static void *dwc2_hc_init_xfer(struct dwc2_hsotg
*hsotg
,
598 struct dwc2_host_chan
*chan
,
599 struct dwc2_qtd
*qtd
, void *bufptr
)
601 struct dwc2_hcd_urb
*urb
= qtd
->urb
;
602 struct dwc2_hcd_iso_packet_desc
*frame_desc
;
604 switch (dwc2_hcd_get_pipe_type(&urb
->pipe_info
)) {
605 case USB_ENDPOINT_XFER_CONTROL
:
606 chan
->ep_type
= USB_ENDPOINT_XFER_CONTROL
;
608 switch (qtd
->control_phase
) {
609 case DWC2_CONTROL_SETUP
:
610 dev_vdbg(hsotg
->dev
, " Control setup transaction\n");
613 chan
->data_pid_start
= DWC2_HC_PID_SETUP
;
614 if (hsotg
->core_params
->dma_enable
> 0)
615 chan
->xfer_dma
= urb
->setup_dma
;
617 chan
->xfer_buf
= urb
->setup_packet
;
622 case DWC2_CONTROL_DATA
:
623 dev_vdbg(hsotg
->dev
, " Control data transaction\n");
624 chan
->data_pid_start
= qtd
->data_toggle
;
627 case DWC2_CONTROL_STATUS
:
629 * Direction is opposite of data direction or IN if no
632 dev_vdbg(hsotg
->dev
, " Control status transaction\n");
633 if (urb
->length
== 0)
637 dwc2_hcd_is_pipe_out(&urb
->pipe_info
);
640 chan
->data_pid_start
= DWC2_HC_PID_DATA1
;
642 if (hsotg
->core_params
->dma_enable
> 0)
643 chan
->xfer_dma
= hsotg
->status_buf_dma
;
645 chan
->xfer_buf
= hsotg
->status_buf
;
651 case USB_ENDPOINT_XFER_BULK
:
652 chan
->ep_type
= USB_ENDPOINT_XFER_BULK
;
655 case USB_ENDPOINT_XFER_INT
:
656 chan
->ep_type
= USB_ENDPOINT_XFER_INT
;
659 case USB_ENDPOINT_XFER_ISOC
:
660 chan
->ep_type
= USB_ENDPOINT_XFER_ISOC
;
661 if (hsotg
->core_params
->dma_desc_enable
> 0)
664 frame_desc
= &urb
->iso_descs
[qtd
->isoc_frame_index
];
665 frame_desc
->status
= 0;
667 if (hsotg
->core_params
->dma_enable
> 0) {
668 chan
->xfer_dma
= urb
->dma
;
669 chan
->xfer_dma
+= frame_desc
->offset
+
670 qtd
->isoc_split_offset
;
672 chan
->xfer_buf
= urb
->buf
;
673 chan
->xfer_buf
+= frame_desc
->offset
+
674 qtd
->isoc_split_offset
;
677 chan
->xfer_len
= frame_desc
->length
- qtd
->isoc_split_offset
;
679 /* For non-dword aligned buffers */
680 if (hsotg
->core_params
->dma_enable
> 0 &&
681 (chan
->xfer_dma
& 0x3))
682 bufptr
= (u8
*)urb
->buf
+ frame_desc
->offset
+
683 qtd
->isoc_split_offset
;
687 if (chan
->xact_pos
== DWC2_HCSPLT_XACTPOS_ALL
) {
688 if (chan
->xfer_len
<= 188)
689 chan
->xact_pos
= DWC2_HCSPLT_XACTPOS_ALL
;
691 chan
->xact_pos
= DWC2_HCSPLT_XACTPOS_BEGIN
;
699 static int dwc2_hc_setup_align_buf(struct dwc2_hsotg
*hsotg
, struct dwc2_qh
*qh
,
700 struct dwc2_host_chan
*chan
, void *bufptr
)
704 if (chan
->ep_type
!= USB_ENDPOINT_XFER_ISOC
)
705 buf_size
= hsotg
->core_params
->max_transfer_size
;
709 if (!qh
->dw_align_buf
) {
710 qh
->dw_align_buf
= dma_alloc_coherent(hsotg
->dev
, buf_size
,
711 &qh
->dw_align_buf_dma
,
713 if (!qh
->dw_align_buf
)
717 if (!chan
->ep_is_in
&& chan
->xfer_len
) {
718 dma_sync_single_for_cpu(hsotg
->dev
, chan
->xfer_dma
, buf_size
,
720 memcpy(qh
->dw_align_buf
, bufptr
, chan
->xfer_len
);
721 dma_sync_single_for_device(hsotg
->dev
, chan
->xfer_dma
, buf_size
,
725 chan
->align_buf
= qh
->dw_align_buf_dma
;
730 * dwc2_assign_and_init_hc() - Assigns transactions from a QTD to a free host
731 * channel and initializes the host channel to perform the transactions. The
732 * host channel is removed from the free list.
734 * @hsotg: The HCD state structure
735 * @qh: Transactions from the first QTD for this QH are selected and assigned
736 * to a free host channel
738 static int dwc2_assign_and_init_hc(struct dwc2_hsotg
*hsotg
, struct dwc2_qh
*qh
)
740 struct dwc2_host_chan
*chan
;
741 struct dwc2_hcd_urb
*urb
;
742 struct dwc2_qtd
*qtd
;
746 dev_vdbg(hsotg
->dev
, "%s(%p,%p)\n", __func__
, hsotg
, qh
);
748 if (list_empty(&qh
->qtd_list
)) {
749 dev_dbg(hsotg
->dev
, "No QTDs in QH list\n");
753 if (list_empty(&hsotg
->free_hc_list
)) {
754 dev_dbg(hsotg
->dev
, "No free channel to assign\n");
758 chan
= list_first_entry(&hsotg
->free_hc_list
, struct dwc2_host_chan
,
761 /* Remove host channel from free list */
762 list_del_init(&chan
->hc_list_entry
);
764 qtd
= list_first_entry(&qh
->qtd_list
, struct dwc2_qtd
, qtd_list_entry
);
770 * Use usb_pipedevice to determine device address. This address is
771 * 0 before the SET_ADDRESS command and the correct address afterward.
773 chan
->dev_addr
= dwc2_hcd_get_dev_addr(&urb
->pipe_info
);
774 chan
->ep_num
= dwc2_hcd_get_ep_num(&urb
->pipe_info
);
775 chan
->speed
= qh
->dev_speed
;
776 chan
->max_packet
= dwc2_max_packet(qh
->maxp
);
778 chan
->xfer_started
= 0;
779 chan
->halt_status
= DWC2_HC_XFER_NO_HALT_STATUS
;
780 chan
->error_state
= (qtd
->error_count
> 0);
781 chan
->halt_on_queue
= 0;
782 chan
->halt_pending
= 0;
786 * The following values may be modified in the transfer type section
787 * below. The xfer_len value may be reduced when the transfer is
788 * started to accommodate the max widths of the XferSize and PktCnt
789 * fields in the HCTSIZn register.
792 chan
->ep_is_in
= (dwc2_hcd_is_pipe_in(&urb
->pipe_info
) != 0);
796 chan
->do_ping
= qh
->ping_state
;
798 chan
->data_pid_start
= qh
->data_toggle
;
799 chan
->multi_count
= 1;
801 if (urb
->actual_length
> urb
->length
&&
802 !dwc2_hcd_is_pipe_in(&urb
->pipe_info
))
803 urb
->actual_length
= urb
->length
;
805 if (hsotg
->core_params
->dma_enable
> 0) {
806 chan
->xfer_dma
= urb
->dma
+ urb
->actual_length
;
808 /* For non-dword aligned case */
809 if (hsotg
->core_params
->dma_desc_enable
<= 0 &&
810 (chan
->xfer_dma
& 0x3))
811 bufptr
= (u8
*)urb
->buf
+ urb
->actual_length
;
813 chan
->xfer_buf
= (u8
*)urb
->buf
+ urb
->actual_length
;
816 chan
->xfer_len
= urb
->length
- urb
->actual_length
;
817 chan
->xfer_count
= 0;
819 /* Set the split attributes if required */
821 dwc2_hc_init_split(hsotg
, chan
, qtd
, urb
);
825 /* Set the transfer attributes */
826 bufptr
= dwc2_hc_init_xfer(hsotg
, chan
, qtd
, bufptr
);
828 /* Non DWORD-aligned buffer case */
830 dev_vdbg(hsotg
->dev
, "Non-aligned buffer\n");
831 if (dwc2_hc_setup_align_buf(hsotg
, qh
, chan
, bufptr
)) {
833 "%s: Failed to allocate memory to handle non-dword aligned buffer\n",
835 /* Add channel back to free list */
837 chan
->multi_count
= 0;
838 list_add_tail(&chan
->hc_list_entry
,
839 &hsotg
->free_hc_list
);
848 if (chan
->ep_type
== USB_ENDPOINT_XFER_INT
||
849 chan
->ep_type
== USB_ENDPOINT_XFER_ISOC
)
851 * This value may be modified when the transfer is started
852 * to reflect the actual transfer length
854 chan
->multi_count
= dwc2_hb_mult(qh
->maxp
);
856 if (hsotg
->core_params
->dma_desc_enable
> 0)
857 chan
->desc_list_addr
= qh
->desc_list_dma
;
859 dwc2_hc_init(hsotg
, chan
);
866 * dwc2_hcd_select_transactions() - Selects transactions from the HCD transfer
867 * schedule and assigns them to available host channels. Called from the HCD
868 * interrupt handler functions.
870 * @hsotg: The HCD state structure
872 * Return: The types of new transactions that were assigned to host channels
874 enum dwc2_transaction_type
dwc2_hcd_select_transactions(
875 struct dwc2_hsotg
*hsotg
)
877 enum dwc2_transaction_type ret_val
= DWC2_TRANSACTION_NONE
;
878 struct list_head
*qh_ptr
;
882 #ifdef DWC2_DEBUG_SOF
883 dev_vdbg(hsotg
->dev
, " Select Transactions\n");
886 /* Process entries in the periodic ready list */
887 qh_ptr
= hsotg
->periodic_sched_ready
.next
;
888 while (qh_ptr
!= &hsotg
->periodic_sched_ready
) {
889 if (list_empty(&hsotg
->free_hc_list
))
891 if (hsotg
->core_params
->uframe_sched
> 0) {
892 if (hsotg
->available_host_channels
<= 1)
894 hsotg
->available_host_channels
--;
896 qh
= list_entry(qh_ptr
, struct dwc2_qh
, qh_list_entry
);
897 if (dwc2_assign_and_init_hc(hsotg
, qh
))
901 * Move the QH from the periodic ready schedule to the
902 * periodic assigned schedule
904 qh_ptr
= qh_ptr
->next
;
905 list_move(&qh
->qh_list_entry
, &hsotg
->periodic_sched_assigned
);
906 ret_val
= DWC2_TRANSACTION_PERIODIC
;
910 * Process entries in the inactive portion of the non-periodic
911 * schedule. Some free host channels may not be used if they are
912 * reserved for periodic transfers.
914 num_channels
= hsotg
->core_params
->host_channels
;
915 qh_ptr
= hsotg
->non_periodic_sched_inactive
.next
;
916 while (qh_ptr
!= &hsotg
->non_periodic_sched_inactive
) {
917 if (hsotg
->core_params
->uframe_sched
<= 0 &&
918 hsotg
->non_periodic_channels
>= num_channels
-
919 hsotg
->periodic_channels
)
921 if (list_empty(&hsotg
->free_hc_list
))
923 qh
= list_entry(qh_ptr
, struct dwc2_qh
, qh_list_entry
);
924 if (hsotg
->core_params
->uframe_sched
> 0) {
925 if (hsotg
->available_host_channels
< 1)
927 hsotg
->available_host_channels
--;
930 if (dwc2_assign_and_init_hc(hsotg
, qh
))
934 * Move the QH from the non-periodic inactive schedule to the
935 * non-periodic active schedule
937 qh_ptr
= qh_ptr
->next
;
938 list_move(&qh
->qh_list_entry
,
939 &hsotg
->non_periodic_sched_active
);
941 if (ret_val
== DWC2_TRANSACTION_NONE
)
942 ret_val
= DWC2_TRANSACTION_NON_PERIODIC
;
944 ret_val
= DWC2_TRANSACTION_ALL
;
946 if (hsotg
->core_params
->uframe_sched
<= 0)
947 hsotg
->non_periodic_channels
++;
954 * dwc2_queue_transaction() - Attempts to queue a single transaction request for
955 * a host channel associated with either a periodic or non-periodic transfer
957 * @hsotg: The HCD state structure
958 * @chan: Host channel descriptor associated with either a periodic or
959 * non-periodic transfer
960 * @fifo_dwords_avail: Number of DWORDs available in the periodic Tx FIFO
961 * for periodic transfers or the non-periodic Tx FIFO
962 * for non-periodic transfers
964 * Return: 1 if a request is queued and more requests may be needed to
965 * complete the transfer, 0 if no more requests are required for this
966 * transfer, -1 if there is insufficient space in the Tx FIFO
968 * This function assumes that there is space available in the appropriate
969 * request queue. For an OUT transfer or SETUP transaction in Slave mode,
970 * it checks whether space is available in the appropriate Tx FIFO.
972 * Must be called with interrupt disabled and spinlock held
974 static int dwc2_queue_transaction(struct dwc2_hsotg
*hsotg
,
975 struct dwc2_host_chan
*chan
,
976 u16 fifo_dwords_avail
)
980 if (hsotg
->core_params
->dma_enable
> 0) {
981 if (hsotg
->core_params
->dma_desc_enable
> 0) {
982 if (!chan
->xfer_started
||
983 chan
->ep_type
== USB_ENDPOINT_XFER_ISOC
) {
984 dwc2_hcd_start_xfer_ddma(hsotg
, chan
->qh
);
985 chan
->qh
->ping_state
= 0;
987 } else if (!chan
->xfer_started
) {
988 dwc2_hc_start_transfer(hsotg
, chan
);
989 chan
->qh
->ping_state
= 0;
991 } else if (chan
->halt_pending
) {
992 /* Don't queue a request if the channel has been halted */
993 } else if (chan
->halt_on_queue
) {
994 dwc2_hc_halt(hsotg
, chan
, chan
->halt_status
);
995 } else if (chan
->do_ping
) {
996 if (!chan
->xfer_started
)
997 dwc2_hc_start_transfer(hsotg
, chan
);
998 } else if (!chan
->ep_is_in
||
999 chan
->data_pid_start
== DWC2_HC_PID_SETUP
) {
1000 if ((fifo_dwords_avail
* 4) >= chan
->max_packet
) {
1001 if (!chan
->xfer_started
) {
1002 dwc2_hc_start_transfer(hsotg
, chan
);
1005 retval
= dwc2_hc_continue_transfer(hsotg
, chan
);
1011 if (!chan
->xfer_started
) {
1012 dwc2_hc_start_transfer(hsotg
, chan
);
1015 retval
= dwc2_hc_continue_transfer(hsotg
, chan
);
1023 * Processes periodic channels for the next frame and queues transactions for
1024 * these channels to the DWC_otg controller. After queueing transactions, the
1025 * Periodic Tx FIFO Empty interrupt is enabled if there are more transactions
1026 * to queue as Periodic Tx FIFO or request queue space becomes available.
1027 * Otherwise, the Periodic Tx FIFO Empty interrupt is disabled.
1029 * Must be called with interrupt disabled and spinlock held
1031 static void dwc2_process_periodic_channels(struct dwc2_hsotg
*hsotg
)
1033 struct list_head
*qh_ptr
;
1039 int no_queue_space
= 0;
1040 int no_fifo_space
= 0;
1044 dev_vdbg(hsotg
->dev
, "Queue periodic transactions\n");
1046 tx_status
= readl(hsotg
->regs
+ HPTXSTS
);
1047 qspcavail
= (tx_status
& TXSTS_QSPCAVAIL_MASK
) >>
1048 TXSTS_QSPCAVAIL_SHIFT
;
1049 fspcavail
= (tx_status
& TXSTS_FSPCAVAIL_MASK
) >>
1050 TXSTS_FSPCAVAIL_SHIFT
;
1053 dev_vdbg(hsotg
->dev
, " P Tx Req Queue Space Avail (before queue): %d\n",
1055 dev_vdbg(hsotg
->dev
, " P Tx FIFO Space Avail (before queue): %d\n",
1059 qh_ptr
= hsotg
->periodic_sched_assigned
.next
;
1060 while (qh_ptr
!= &hsotg
->periodic_sched_assigned
) {
1061 tx_status
= readl(hsotg
->regs
+ HPTXSTS
);
1062 qspcavail
= (tx_status
& TXSTS_QSPCAVAIL_MASK
) >>
1063 TXSTS_QSPCAVAIL_SHIFT
;
1064 if (qspcavail
== 0) {
1069 qh
= list_entry(qh_ptr
, struct dwc2_qh
, qh_list_entry
);
1071 qh_ptr
= qh_ptr
->next
;
1075 /* Make sure EP's TT buffer is clean before queueing qtds */
1076 if (qh
->tt_buffer_dirty
) {
1077 qh_ptr
= qh_ptr
->next
;
1082 * Set a flag if we're queuing high-bandwidth in slave mode.
1083 * The flag prevents any halts to get into the request queue in
1084 * the middle of multiple high-bandwidth packets getting queued.
1086 if (hsotg
->core_params
->dma_enable
<= 0 &&
1087 qh
->channel
->multi_count
> 1)
1088 hsotg
->queuing_high_bandwidth
= 1;
1090 fspcavail
= (tx_status
& TXSTS_FSPCAVAIL_MASK
) >>
1091 TXSTS_FSPCAVAIL_SHIFT
;
1092 status
= dwc2_queue_transaction(hsotg
, qh
->channel
, fspcavail
);
1099 * In Slave mode, stay on the current transfer until there is
1100 * nothing more to do or the high-bandwidth request count is
1101 * reached. In DMA mode, only need to queue one request. The
1102 * controller automatically handles multiple packets for
1103 * high-bandwidth transfers.
1105 if (hsotg
->core_params
->dma_enable
> 0 || status
== 0 ||
1106 qh
->channel
->requests
== qh
->channel
->multi_count
) {
1107 qh_ptr
= qh_ptr
->next
;
1109 * Move the QH from the periodic assigned schedule to
1110 * the periodic queued schedule
1112 list_move(&qh
->qh_list_entry
,
1113 &hsotg
->periodic_sched_queued
);
1115 /* done queuing high bandwidth */
1116 hsotg
->queuing_high_bandwidth
= 0;
1120 if (hsotg
->core_params
->dma_enable
<= 0) {
1121 tx_status
= readl(hsotg
->regs
+ HPTXSTS
);
1122 qspcavail
= (tx_status
& TXSTS_QSPCAVAIL_MASK
) >>
1123 TXSTS_QSPCAVAIL_SHIFT
;
1124 fspcavail
= (tx_status
& TXSTS_FSPCAVAIL_MASK
) >>
1125 TXSTS_FSPCAVAIL_SHIFT
;
1127 dev_vdbg(hsotg
->dev
,
1128 " P Tx Req Queue Space Avail (after queue): %d\n",
1130 dev_vdbg(hsotg
->dev
,
1131 " P Tx FIFO Space Avail (after queue): %d\n",
1135 if (!list_empty(&hsotg
->periodic_sched_assigned
) ||
1136 no_queue_space
|| no_fifo_space
) {
1138 * May need to queue more transactions as the request
1139 * queue or Tx FIFO empties. Enable the periodic Tx
1140 * FIFO empty interrupt. (Always use the half-empty
1141 * level to ensure that new requests are loaded as
1142 * soon as possible.)
1144 gintmsk
= readl(hsotg
->regs
+ GINTMSK
);
1145 gintmsk
|= GINTSTS_PTXFEMP
;
1146 writel(gintmsk
, hsotg
->regs
+ GINTMSK
);
1149 * Disable the Tx FIFO empty interrupt since there are
1150 * no more transactions that need to be queued right
1151 * now. This function is called from interrupt
1152 * handlers to queue more transactions as transfer
1155 gintmsk
= readl(hsotg
->regs
+ GINTMSK
);
1156 gintmsk
&= ~GINTSTS_PTXFEMP
;
1157 writel(gintmsk
, hsotg
->regs
+ GINTMSK
);
1163 * Processes active non-periodic channels and queues transactions for these
1164 * channels to the DWC_otg controller. After queueing transactions, the NP Tx
1165 * FIFO Empty interrupt is enabled if there are more transactions to queue as
1166 * NP Tx FIFO or request queue space becomes available. Otherwise, the NP Tx
1167 * FIFO Empty interrupt is disabled.
1169 * Must be called with interrupt disabled and spinlock held
1171 static void dwc2_process_non_periodic_channels(struct dwc2_hsotg
*hsotg
)
1173 struct list_head
*orig_qh_ptr
;
1180 int no_queue_space
= 0;
1181 int no_fifo_space
= 0;
1184 dev_vdbg(hsotg
->dev
, "Queue non-periodic transactions\n");
1186 tx_status
= readl(hsotg
->regs
+ GNPTXSTS
);
1187 qspcavail
= (tx_status
& TXSTS_QSPCAVAIL_MASK
) >>
1188 TXSTS_QSPCAVAIL_SHIFT
;
1189 fspcavail
= (tx_status
& TXSTS_FSPCAVAIL_MASK
) >>
1190 TXSTS_FSPCAVAIL_SHIFT
;
1191 dev_vdbg(hsotg
->dev
, " NP Tx Req Queue Space Avail (before queue): %d\n",
1193 dev_vdbg(hsotg
->dev
, " NP Tx FIFO Space Avail (before queue): %d\n",
1197 * Keep track of the starting point. Skip over the start-of-list
1200 if (hsotg
->non_periodic_qh_ptr
== &hsotg
->non_periodic_sched_active
)
1201 hsotg
->non_periodic_qh_ptr
= hsotg
->non_periodic_qh_ptr
->next
;
1202 orig_qh_ptr
= hsotg
->non_periodic_qh_ptr
;
1205 * Process once through the active list or until no more space is
1206 * available in the request queue or the Tx FIFO
1209 tx_status
= readl(hsotg
->regs
+ GNPTXSTS
);
1210 qspcavail
= (tx_status
& TXSTS_QSPCAVAIL_MASK
) >>
1211 TXSTS_QSPCAVAIL_SHIFT
;
1212 if (hsotg
->core_params
->dma_enable
<= 0 && qspcavail
== 0) {
1217 qh
= list_entry(hsotg
->non_periodic_qh_ptr
, struct dwc2_qh
,
1222 /* Make sure EP's TT buffer is clean before queueing qtds */
1223 if (qh
->tt_buffer_dirty
)
1226 fspcavail
= (tx_status
& TXSTS_FSPCAVAIL_MASK
) >>
1227 TXSTS_FSPCAVAIL_SHIFT
;
1228 status
= dwc2_queue_transaction(hsotg
, qh
->channel
, fspcavail
);
1232 } else if (status
< 0) {
1237 /* Advance to next QH, skipping start-of-list entry */
1238 hsotg
->non_periodic_qh_ptr
= hsotg
->non_periodic_qh_ptr
->next
;
1239 if (hsotg
->non_periodic_qh_ptr
==
1240 &hsotg
->non_periodic_sched_active
)
1241 hsotg
->non_periodic_qh_ptr
=
1242 hsotg
->non_periodic_qh_ptr
->next
;
1243 } while (hsotg
->non_periodic_qh_ptr
!= orig_qh_ptr
);
1245 if (hsotg
->core_params
->dma_enable
<= 0) {
1246 tx_status
= readl(hsotg
->regs
+ GNPTXSTS
);
1247 qspcavail
= (tx_status
& TXSTS_QSPCAVAIL_MASK
) >>
1248 TXSTS_QSPCAVAIL_SHIFT
;
1249 fspcavail
= (tx_status
& TXSTS_FSPCAVAIL_MASK
) >>
1250 TXSTS_FSPCAVAIL_SHIFT
;
1251 dev_vdbg(hsotg
->dev
,
1252 " NP Tx Req Queue Space Avail (after queue): %d\n",
1254 dev_vdbg(hsotg
->dev
,
1255 " NP Tx FIFO Space Avail (after queue): %d\n",
1258 if (more_to_do
|| no_queue_space
|| no_fifo_space
) {
1260 * May need to queue more transactions as the request
1261 * queue or Tx FIFO empties. Enable the non-periodic
1262 * Tx FIFO empty interrupt. (Always use the half-empty
1263 * level to ensure that new requests are loaded as
1264 * soon as possible.)
1266 gintmsk
= readl(hsotg
->regs
+ GINTMSK
);
1267 gintmsk
|= GINTSTS_NPTXFEMP
;
1268 writel(gintmsk
, hsotg
->regs
+ GINTMSK
);
1271 * Disable the Tx FIFO empty interrupt since there are
1272 * no more transactions that need to be queued right
1273 * now. This function is called from interrupt
1274 * handlers to queue more transactions as transfer
1277 gintmsk
= readl(hsotg
->regs
+ GINTMSK
);
1278 gintmsk
&= ~GINTSTS_NPTXFEMP
;
1279 writel(gintmsk
, hsotg
->regs
+ GINTMSK
);
1285 * dwc2_hcd_queue_transactions() - Processes the currently active host channels
1286 * and queues transactions for these channels to the DWC_otg controller. Called
1287 * from the HCD interrupt handler functions.
1289 * @hsotg: The HCD state structure
1290 * @tr_type: The type(s) of transactions to queue (non-periodic, periodic,
1293 * Must be called with interrupt disabled and spinlock held
1295 void dwc2_hcd_queue_transactions(struct dwc2_hsotg
*hsotg
,
1296 enum dwc2_transaction_type tr_type
)
1298 #ifdef DWC2_DEBUG_SOF
1299 dev_vdbg(hsotg
->dev
, "Queue Transactions\n");
1301 /* Process host channels associated with periodic transfers */
1302 if ((tr_type
== DWC2_TRANSACTION_PERIODIC
||
1303 tr_type
== DWC2_TRANSACTION_ALL
) &&
1304 !list_empty(&hsotg
->periodic_sched_assigned
))
1305 dwc2_process_periodic_channels(hsotg
);
1307 /* Process host channels associated with non-periodic transfers */
1308 if (tr_type
== DWC2_TRANSACTION_NON_PERIODIC
||
1309 tr_type
== DWC2_TRANSACTION_ALL
) {
1310 if (!list_empty(&hsotg
->non_periodic_sched_active
)) {
1311 dwc2_process_non_periodic_channels(hsotg
);
1314 * Ensure NP Tx FIFO empty interrupt is disabled when
1315 * there are no non-periodic transfers to process
1317 u32 gintmsk
= readl(hsotg
->regs
+ GINTMSK
);
1319 gintmsk
&= ~GINTSTS_NPTXFEMP
;
1320 writel(gintmsk
, hsotg
->regs
+ GINTMSK
);
1325 static void dwc2_conn_id_status_change(struct work_struct
*work
)
1327 struct dwc2_hsotg
*hsotg
= container_of(work
, struct dwc2_hsotg
,
1332 dev_dbg(hsotg
->dev
, "%s()\n", __func__
);
1334 gotgctl
= readl(hsotg
->regs
+ GOTGCTL
);
1335 dev_dbg(hsotg
->dev
, "gotgctl=%0x\n", gotgctl
);
1336 dev_dbg(hsotg
->dev
, "gotgctl.b.conidsts=%d\n",
1337 !!(gotgctl
& GOTGCTL_CONID_B
));
1339 /* B-Device connector (Device Mode) */
1340 if (gotgctl
& GOTGCTL_CONID_B
) {
1341 /* Wait for switch to device mode */
1342 dev_dbg(hsotg
->dev
, "connId B\n");
1343 while (!dwc2_is_device_mode(hsotg
)) {
1344 dev_info(hsotg
->dev
,
1345 "Waiting for Peripheral Mode, Mode=%s\n",
1346 dwc2_is_host_mode(hsotg
) ? "Host" :
1348 usleep_range(20000, 40000);
1354 "Connection id status change timed out\n");
1355 hsotg
->op_state
= OTG_STATE_B_PERIPHERAL
;
1356 dwc2_core_init(hsotg
, false, -1);
1357 dwc2_enable_global_interrupts(hsotg
);
1359 /* A-Device connector (Host Mode) */
1360 dev_dbg(hsotg
->dev
, "connId A\n");
1361 while (!dwc2_is_host_mode(hsotg
)) {
1362 dev_info(hsotg
->dev
, "Waiting for Host Mode, Mode=%s\n",
1363 dwc2_is_host_mode(hsotg
) ?
1364 "Host" : "Peripheral");
1365 usleep_range(20000, 40000);
1371 "Connection id status change timed out\n");
1372 hsotg
->op_state
= OTG_STATE_A_HOST
;
1374 /* Initialize the Core for Host mode */
1375 dwc2_core_init(hsotg
, false, -1);
1376 dwc2_enable_global_interrupts(hsotg
);
1377 dwc2_hcd_start(hsotg
);
1381 static void dwc2_wakeup_detected(unsigned long data
)
1383 struct dwc2_hsotg
*hsotg
= (struct dwc2_hsotg
*)data
;
1386 dev_dbg(hsotg
->dev
, "%s()\n", __func__
);
1389 * Clear the Resume after 70ms. (Need 20 ms minimum. Use 70 ms
1390 * so that OPT tests pass with all PHYs.)
1392 hprt0
= dwc2_read_hprt0(hsotg
);
1393 dev_dbg(hsotg
->dev
, "Resume: HPRT0=%0x\n", hprt0
);
1394 hprt0
&= ~HPRT0_RES
;
1395 writel(hprt0
, hsotg
->regs
+ HPRT0
);
1396 dev_dbg(hsotg
->dev
, "Clear Resume: HPRT0=%0x\n",
1397 readl(hsotg
->regs
+ HPRT0
));
1399 dwc2_hcd_rem_wakeup(hsotg
);
1401 /* Change to L0 state */
1402 hsotg
->lx_state
= DWC2_L0
;
1405 static int dwc2_host_is_b_hnp_enabled(struct dwc2_hsotg
*hsotg
)
1407 struct usb_hcd
*hcd
= dwc2_hsotg_to_hcd(hsotg
);
1409 return hcd
->self
.b_hnp_enable
;
1412 /* Must NOT be called with interrupt disabled or spinlock held */
1413 static void dwc2_port_suspend(struct dwc2_hsotg
*hsotg
, u16 windex
)
1415 unsigned long flags
;
1420 dev_dbg(hsotg
->dev
, "%s()\n", __func__
);
1422 spin_lock_irqsave(&hsotg
->lock
, flags
);
1424 if (windex
== hsotg
->otg_port
&& dwc2_host_is_b_hnp_enabled(hsotg
)) {
1425 gotgctl
= readl(hsotg
->regs
+ GOTGCTL
);
1426 gotgctl
|= GOTGCTL_HSTSETHNPEN
;
1427 writel(gotgctl
, hsotg
->regs
+ GOTGCTL
);
1428 hsotg
->op_state
= OTG_STATE_A_SUSPEND
;
1431 hprt0
= dwc2_read_hprt0(hsotg
);
1432 hprt0
|= HPRT0_SUSP
;
1433 writel(hprt0
, hsotg
->regs
+ HPRT0
);
1435 /* Update lx_state */
1436 hsotg
->lx_state
= DWC2_L2
;
1438 /* Suspend the Phy Clock */
1439 pcgctl
= readl(hsotg
->regs
+ PCGCTL
);
1440 pcgctl
|= PCGCTL_STOPPCLK
;
1441 writel(pcgctl
, hsotg
->regs
+ PCGCTL
);
1444 /* For HNP the bus must be suspended for at least 200ms */
1445 if (dwc2_host_is_b_hnp_enabled(hsotg
)) {
1446 pcgctl
= readl(hsotg
->regs
+ PCGCTL
);
1447 pcgctl
&= ~PCGCTL_STOPPCLK
;
1448 writel(pcgctl
, hsotg
->regs
+ PCGCTL
);
1450 spin_unlock_irqrestore(&hsotg
->lock
, flags
);
1452 usleep_range(200000, 250000);
1454 spin_unlock_irqrestore(&hsotg
->lock
, flags
);
1458 /* Handles hub class-specific requests */
1459 static int dwc2_hcd_hub_control(struct dwc2_hsotg
*hsotg
, u16 typereq
,
1460 u16 wvalue
, u16 windex
, char *buf
, u16 wlength
)
1462 struct usb_hub_descriptor
*hub_desc
;
1470 case ClearHubFeature
:
1471 dev_dbg(hsotg
->dev
, "ClearHubFeature %1xh\n", wvalue
);
1474 case C_HUB_LOCAL_POWER
:
1475 case C_HUB_OVER_CURRENT
:
1476 /* Nothing required here */
1482 "ClearHubFeature request %1xh unknown\n",
1487 case ClearPortFeature
:
1488 if (wvalue
!= USB_PORT_FEAT_L1
)
1489 if (!windex
|| windex
> 1)
1492 case USB_PORT_FEAT_ENABLE
:
1494 "ClearPortFeature USB_PORT_FEAT_ENABLE\n");
1495 hprt0
= dwc2_read_hprt0(hsotg
);
1497 writel(hprt0
, hsotg
->regs
+ HPRT0
);
1500 case USB_PORT_FEAT_SUSPEND
:
1502 "ClearPortFeature USB_PORT_FEAT_SUSPEND\n");
1503 writel(0, hsotg
->regs
+ PCGCTL
);
1504 usleep_range(20000, 40000);
1506 hprt0
= dwc2_read_hprt0(hsotg
);
1508 writel(hprt0
, hsotg
->regs
+ HPRT0
);
1509 hprt0
&= ~HPRT0_SUSP
;
1510 usleep_range(100000, 150000);
1512 hprt0
&= ~HPRT0_RES
;
1513 writel(hprt0
, hsotg
->regs
+ HPRT0
);
1516 case USB_PORT_FEAT_POWER
:
1518 "ClearPortFeature USB_PORT_FEAT_POWER\n");
1519 hprt0
= dwc2_read_hprt0(hsotg
);
1520 hprt0
&= ~HPRT0_PWR
;
1521 writel(hprt0
, hsotg
->regs
+ HPRT0
);
1524 case USB_PORT_FEAT_INDICATOR
:
1526 "ClearPortFeature USB_PORT_FEAT_INDICATOR\n");
1527 /* Port indicator not supported */
1530 case USB_PORT_FEAT_C_CONNECTION
:
1532 * Clears driver's internal Connect Status Change flag
1535 "ClearPortFeature USB_PORT_FEAT_C_CONNECTION\n");
1536 hsotg
->flags
.b
.port_connect_status_change
= 0;
1539 case USB_PORT_FEAT_C_RESET
:
1540 /* Clears driver's internal Port Reset Change flag */
1542 "ClearPortFeature USB_PORT_FEAT_C_RESET\n");
1543 hsotg
->flags
.b
.port_reset_change
= 0;
1546 case USB_PORT_FEAT_C_ENABLE
:
1548 * Clears the driver's internal Port Enable/Disable
1552 "ClearPortFeature USB_PORT_FEAT_C_ENABLE\n");
1553 hsotg
->flags
.b
.port_enable_change
= 0;
1556 case USB_PORT_FEAT_C_SUSPEND
:
1558 * Clears the driver's internal Port Suspend Change
1559 * flag, which is set when resume signaling on the host
1563 "ClearPortFeature USB_PORT_FEAT_C_SUSPEND\n");
1564 hsotg
->flags
.b
.port_suspend_change
= 0;
1567 case USB_PORT_FEAT_C_PORT_L1
:
1569 "ClearPortFeature USB_PORT_FEAT_C_PORT_L1\n");
1570 hsotg
->flags
.b
.port_l1_change
= 0;
1573 case USB_PORT_FEAT_C_OVER_CURRENT
:
1575 "ClearPortFeature USB_PORT_FEAT_C_OVER_CURRENT\n");
1576 hsotg
->flags
.b
.port_over_current_change
= 0;
1582 "ClearPortFeature request %1xh unknown or unsupported\n",
1587 case GetHubDescriptor
:
1588 dev_dbg(hsotg
->dev
, "GetHubDescriptor\n");
1589 hub_desc
= (struct usb_hub_descriptor
*)buf
;
1590 hub_desc
->bDescLength
= 9;
1591 hub_desc
->bDescriptorType
= 0x29;
1592 hub_desc
->bNbrPorts
= 1;
1593 hub_desc
->wHubCharacteristics
= cpu_to_le16(0x08);
1594 hub_desc
->bPwrOn2PwrGood
= 1;
1595 hub_desc
->bHubContrCurrent
= 0;
1596 hub_desc
->u
.hs
.DeviceRemovable
[0] = 0;
1597 hub_desc
->u
.hs
.DeviceRemovable
[1] = 0xff;
1601 dev_dbg(hsotg
->dev
, "GetHubStatus\n");
1606 dev_vdbg(hsotg
->dev
,
1607 "GetPortStatus wIndex=0x%04x flags=0x%08x\n", windex
,
1609 if (!windex
|| windex
> 1)
1613 if (hsotg
->flags
.b
.port_connect_status_change
)
1614 port_status
|= USB_PORT_STAT_C_CONNECTION
<< 16;
1615 if (hsotg
->flags
.b
.port_enable_change
)
1616 port_status
|= USB_PORT_STAT_C_ENABLE
<< 16;
1617 if (hsotg
->flags
.b
.port_suspend_change
)
1618 port_status
|= USB_PORT_STAT_C_SUSPEND
<< 16;
1619 if (hsotg
->flags
.b
.port_l1_change
)
1620 port_status
|= USB_PORT_STAT_C_L1
<< 16;
1621 if (hsotg
->flags
.b
.port_reset_change
)
1622 port_status
|= USB_PORT_STAT_C_RESET
<< 16;
1623 if (hsotg
->flags
.b
.port_over_current_change
) {
1624 dev_warn(hsotg
->dev
, "Overcurrent change detected\n");
1625 port_status
|= USB_PORT_STAT_C_OVERCURRENT
<< 16;
1628 if (!hsotg
->flags
.b
.port_connect_status
) {
1630 * The port is disconnected, which means the core is
1631 * either in device mode or it soon will be. Just
1632 * return 0's for the remainder of the port status
1633 * since the port register can't be read if the core
1634 * is in device mode.
1636 *(__le32
*)buf
= cpu_to_le32(port_status
);
1640 hprt0
= readl(hsotg
->regs
+ HPRT0
);
1641 dev_vdbg(hsotg
->dev
, " HPRT0: 0x%08x\n", hprt0
);
1643 if (hprt0
& HPRT0_CONNSTS
)
1644 port_status
|= USB_PORT_STAT_CONNECTION
;
1645 if (hprt0
& HPRT0_ENA
)
1646 port_status
|= USB_PORT_STAT_ENABLE
;
1647 if (hprt0
& HPRT0_SUSP
)
1648 port_status
|= USB_PORT_STAT_SUSPEND
;
1649 if (hprt0
& HPRT0_OVRCURRACT
)
1650 port_status
|= USB_PORT_STAT_OVERCURRENT
;
1651 if (hprt0
& HPRT0_RST
)
1652 port_status
|= USB_PORT_STAT_RESET
;
1653 if (hprt0
& HPRT0_PWR
)
1654 port_status
|= USB_PORT_STAT_POWER
;
1656 speed
= (hprt0
& HPRT0_SPD_MASK
) >> HPRT0_SPD_SHIFT
;
1657 if (speed
== HPRT0_SPD_HIGH_SPEED
)
1658 port_status
|= USB_PORT_STAT_HIGH_SPEED
;
1659 else if (speed
== HPRT0_SPD_LOW_SPEED
)
1660 port_status
|= USB_PORT_STAT_LOW_SPEED
;
1662 if (hprt0
& HPRT0_TSTCTL_MASK
)
1663 port_status
|= USB_PORT_STAT_TEST
;
1664 /* USB_PORT_FEAT_INDICATOR unsupported always 0 */
1666 dev_vdbg(hsotg
->dev
, "port_status=%08x\n", port_status
);
1667 *(__le32
*)buf
= cpu_to_le32(port_status
);
1671 dev_dbg(hsotg
->dev
, "SetHubFeature\n");
1672 /* No HUB features supported */
1675 case SetPortFeature
:
1676 dev_dbg(hsotg
->dev
, "SetPortFeature\n");
1677 if (wvalue
!= USB_PORT_FEAT_TEST
&& (!windex
|| windex
> 1))
1680 if (!hsotg
->flags
.b
.port_connect_status
) {
1682 * The port is disconnected, which means the core is
1683 * either in device mode or it soon will be. Just
1684 * return without doing anything since the port
1685 * register can't be written if the core is in device
1692 case USB_PORT_FEAT_SUSPEND
:
1694 "SetPortFeature - USB_PORT_FEAT_SUSPEND\n");
1695 if (windex
!= hsotg
->otg_port
)
1697 dwc2_port_suspend(hsotg
, windex
);
1700 case USB_PORT_FEAT_POWER
:
1702 "SetPortFeature - USB_PORT_FEAT_POWER\n");
1703 hprt0
= dwc2_read_hprt0(hsotg
);
1705 writel(hprt0
, hsotg
->regs
+ HPRT0
);
1708 case USB_PORT_FEAT_RESET
:
1709 hprt0
= dwc2_read_hprt0(hsotg
);
1711 "SetPortFeature - USB_PORT_FEAT_RESET\n");
1712 pcgctl
= readl(hsotg
->regs
+ PCGCTL
);
1713 pcgctl
&= ~(PCGCTL_ENBL_SLEEP_GATING
| PCGCTL_STOPPCLK
);
1714 writel(pcgctl
, hsotg
->regs
+ PCGCTL
);
1715 /* ??? Original driver does this */
1716 writel(0, hsotg
->regs
+ PCGCTL
);
1718 hprt0
= dwc2_read_hprt0(hsotg
);
1719 /* Clear suspend bit if resetting from suspend state */
1720 hprt0
&= ~HPRT0_SUSP
;
1723 * When B-Host the Port reset bit is set in the Start
1724 * HCD Callback function, so that the reset is started
1725 * within 1ms of the HNP success interrupt
1727 if (!dwc2_hcd_is_b_host(hsotg
)) {
1728 hprt0
|= HPRT0_PWR
| HPRT0_RST
;
1730 "In host mode, hprt0=%08x\n", hprt0
);
1731 writel(hprt0
, hsotg
->regs
+ HPRT0
);
1734 /* Clear reset bit in 10ms (FS/LS) or 50ms (HS) */
1735 usleep_range(50000, 70000);
1736 hprt0
&= ~HPRT0_RST
;
1737 writel(hprt0
, hsotg
->regs
+ HPRT0
);
1738 hsotg
->lx_state
= DWC2_L0
; /* Now back to On state */
1741 case USB_PORT_FEAT_INDICATOR
:
1743 "SetPortFeature - USB_PORT_FEAT_INDICATOR\n");
1750 "SetPortFeature %1xh unknown or unsupported\n",
1760 "Unknown hub control request: %1xh wIndex: %1xh wValue: %1xh\n",
1761 typereq
, windex
, wvalue
);
1768 static int dwc2_hcd_is_status_changed(struct dwc2_hsotg
*hsotg
, int port
)
1775 retval
= (hsotg
->flags
.b
.port_connect_status_change
||
1776 hsotg
->flags
.b
.port_reset_change
||
1777 hsotg
->flags
.b
.port_enable_change
||
1778 hsotg
->flags
.b
.port_suspend_change
||
1779 hsotg
->flags
.b
.port_over_current_change
);
1783 "DWC OTG HCD HUB STATUS DATA: Root port status changed\n");
1784 dev_dbg(hsotg
->dev
, " port_connect_status_change: %d\n",
1785 hsotg
->flags
.b
.port_connect_status_change
);
1786 dev_dbg(hsotg
->dev
, " port_reset_change: %d\n",
1787 hsotg
->flags
.b
.port_reset_change
);
1788 dev_dbg(hsotg
->dev
, " port_enable_change: %d\n",
1789 hsotg
->flags
.b
.port_enable_change
);
1790 dev_dbg(hsotg
->dev
, " port_suspend_change: %d\n",
1791 hsotg
->flags
.b
.port_suspend_change
);
1792 dev_dbg(hsotg
->dev
, " port_over_current_change: %d\n",
1793 hsotg
->flags
.b
.port_over_current_change
);
1799 int dwc2_hcd_get_frame_number(struct dwc2_hsotg
*hsotg
)
1801 u32 hfnum
= readl(hsotg
->regs
+ HFNUM
);
1803 #ifdef DWC2_DEBUG_SOF
1804 dev_vdbg(hsotg
->dev
, "DWC OTG HCD GET FRAME NUMBER %d\n",
1805 (hfnum
& HFNUM_FRNUM_MASK
) >> HFNUM_FRNUM_SHIFT
);
1807 return (hfnum
& HFNUM_FRNUM_MASK
) >> HFNUM_FRNUM_SHIFT
;
1810 int dwc2_hcd_is_b_host(struct dwc2_hsotg
*hsotg
)
1812 return hsotg
->op_state
== OTG_STATE_B_HOST
;
1815 static struct dwc2_hcd_urb
*dwc2_hcd_urb_alloc(struct dwc2_hsotg
*hsotg
,
1819 struct dwc2_hcd_urb
*urb
;
1820 u32 size
= sizeof(*urb
) + iso_desc_count
*
1821 sizeof(struct dwc2_hcd_iso_packet_desc
);
1823 urb
= kzalloc(size
, mem_flags
);
1825 urb
->packet_count
= iso_desc_count
;
1829 static void dwc2_hcd_urb_set_pipeinfo(struct dwc2_hsotg
*hsotg
,
1830 struct dwc2_hcd_urb
*urb
, u8 dev_addr
,
1831 u8 ep_num
, u8 ep_type
, u8 ep_dir
, u16 mps
)
1834 ep_type
== USB_ENDPOINT_XFER_BULK
||
1835 ep_type
== USB_ENDPOINT_XFER_CONTROL
)
1836 dev_vdbg(hsotg
->dev
,
1837 "addr=%d, ep_num=%d, ep_dir=%1x, ep_type=%1x, mps=%d\n",
1838 dev_addr
, ep_num
, ep_dir
, ep_type
, mps
);
1839 urb
->pipe_info
.dev_addr
= dev_addr
;
1840 urb
->pipe_info
.ep_num
= ep_num
;
1841 urb
->pipe_info
.pipe_type
= ep_type
;
1842 urb
->pipe_info
.pipe_dir
= ep_dir
;
1843 urb
->pipe_info
.mps
= mps
;
1847 * NOTE: This function will be removed once the peripheral controller code
1848 * is integrated and the driver is stable
1850 void dwc2_hcd_dump_state(struct dwc2_hsotg
*hsotg
)
1853 struct dwc2_host_chan
*chan
;
1854 struct dwc2_hcd_urb
*urb
;
1855 struct dwc2_qtd
*qtd
;
1861 num_channels
= hsotg
->core_params
->host_channels
;
1862 dev_dbg(hsotg
->dev
, "\n");
1864 "************************************************************\n");
1865 dev_dbg(hsotg
->dev
, "HCD State:\n");
1866 dev_dbg(hsotg
->dev
, " Num channels: %d\n", num_channels
);
1868 for (i
= 0; i
< num_channels
; i
++) {
1869 chan
= hsotg
->hc_ptr_array
[i
];
1870 dev_dbg(hsotg
->dev
, " Channel %d:\n", i
);
1872 " dev_addr: %d, ep_num: %d, ep_is_in: %d\n",
1873 chan
->dev_addr
, chan
->ep_num
, chan
->ep_is_in
);
1874 dev_dbg(hsotg
->dev
, " speed: %d\n", chan
->speed
);
1875 dev_dbg(hsotg
->dev
, " ep_type: %d\n", chan
->ep_type
);
1876 dev_dbg(hsotg
->dev
, " max_packet: %d\n", chan
->max_packet
);
1877 dev_dbg(hsotg
->dev
, " data_pid_start: %d\n",
1878 chan
->data_pid_start
);
1879 dev_dbg(hsotg
->dev
, " multi_count: %d\n", chan
->multi_count
);
1880 dev_dbg(hsotg
->dev
, " xfer_started: %d\n",
1881 chan
->xfer_started
);
1882 dev_dbg(hsotg
->dev
, " xfer_buf: %p\n", chan
->xfer_buf
);
1883 dev_dbg(hsotg
->dev
, " xfer_dma: %08lx\n",
1884 (unsigned long)chan
->xfer_dma
);
1885 dev_dbg(hsotg
->dev
, " xfer_len: %d\n", chan
->xfer_len
);
1886 dev_dbg(hsotg
->dev
, " xfer_count: %d\n", chan
->xfer_count
);
1887 dev_dbg(hsotg
->dev
, " halt_on_queue: %d\n",
1888 chan
->halt_on_queue
);
1889 dev_dbg(hsotg
->dev
, " halt_pending: %d\n",
1890 chan
->halt_pending
);
1891 dev_dbg(hsotg
->dev
, " halt_status: %d\n", chan
->halt_status
);
1892 dev_dbg(hsotg
->dev
, " do_split: %d\n", chan
->do_split
);
1893 dev_dbg(hsotg
->dev
, " complete_split: %d\n",
1894 chan
->complete_split
);
1895 dev_dbg(hsotg
->dev
, " hub_addr: %d\n", chan
->hub_addr
);
1896 dev_dbg(hsotg
->dev
, " hub_port: %d\n", chan
->hub_port
);
1897 dev_dbg(hsotg
->dev
, " xact_pos: %d\n", chan
->xact_pos
);
1898 dev_dbg(hsotg
->dev
, " requests: %d\n", chan
->requests
);
1899 dev_dbg(hsotg
->dev
, " qh: %p\n", chan
->qh
);
1901 if (chan
->xfer_started
) {
1902 u32 hfnum
, hcchar
, hctsiz
, hcint
, hcintmsk
;
1904 hfnum
= readl(hsotg
->regs
+ HFNUM
);
1905 hcchar
= readl(hsotg
->regs
+ HCCHAR(i
));
1906 hctsiz
= readl(hsotg
->regs
+ HCTSIZ(i
));
1907 hcint
= readl(hsotg
->regs
+ HCINT(i
));
1908 hcintmsk
= readl(hsotg
->regs
+ HCINTMSK(i
));
1909 dev_dbg(hsotg
->dev
, " hfnum: 0x%08x\n", hfnum
);
1910 dev_dbg(hsotg
->dev
, " hcchar: 0x%08x\n", hcchar
);
1911 dev_dbg(hsotg
->dev
, " hctsiz: 0x%08x\n", hctsiz
);
1912 dev_dbg(hsotg
->dev
, " hcint: 0x%08x\n", hcint
);
1913 dev_dbg(hsotg
->dev
, " hcintmsk: 0x%08x\n", hcintmsk
);
1916 if (!(chan
->xfer_started
&& chan
->qh
))
1919 list_for_each_entry(qtd
, &chan
->qh
->qtd_list
, qtd_list_entry
) {
1920 if (!qtd
->in_process
)
1923 dev_dbg(hsotg
->dev
, " URB Info:\n");
1924 dev_dbg(hsotg
->dev
, " qtd: %p, urb: %p\n",
1928 " Dev: %d, EP: %d %s\n",
1929 dwc2_hcd_get_dev_addr(&urb
->pipe_info
),
1930 dwc2_hcd_get_ep_num(&urb
->pipe_info
),
1931 dwc2_hcd_is_pipe_in(&urb
->pipe_info
) ?
1934 " Max packet size: %d\n",
1935 dwc2_hcd_get_mps(&urb
->pipe_info
));
1937 " transfer_buffer: %p\n",
1940 " transfer_dma: %08lx\n",
1941 (unsigned long)urb
->dma
);
1943 " transfer_buffer_length: %d\n",
1945 dev_dbg(hsotg
->dev
, " actual_length: %d\n",
1946 urb
->actual_length
);
1951 dev_dbg(hsotg
->dev
, " non_periodic_channels: %d\n",
1952 hsotg
->non_periodic_channels
);
1953 dev_dbg(hsotg
->dev
, " periodic_channels: %d\n",
1954 hsotg
->periodic_channels
);
1955 dev_dbg(hsotg
->dev
, " periodic_usecs: %d\n", hsotg
->periodic_usecs
);
1956 np_tx_status
= readl(hsotg
->regs
+ GNPTXSTS
);
1957 dev_dbg(hsotg
->dev
, " NP Tx Req Queue Space Avail: %d\n",
1958 (np_tx_status
& TXSTS_QSPCAVAIL_MASK
) >> TXSTS_QSPCAVAIL_SHIFT
);
1959 dev_dbg(hsotg
->dev
, " NP Tx FIFO Space Avail: %d\n",
1960 (np_tx_status
& TXSTS_FSPCAVAIL_MASK
) >> TXSTS_FSPCAVAIL_SHIFT
);
1961 p_tx_status
= readl(hsotg
->regs
+ HPTXSTS
);
1962 dev_dbg(hsotg
->dev
, " P Tx Req Queue Space Avail: %d\n",
1963 (p_tx_status
& TXSTS_QSPCAVAIL_MASK
) >> TXSTS_QSPCAVAIL_SHIFT
);
1964 dev_dbg(hsotg
->dev
, " P Tx FIFO Space Avail: %d\n",
1965 (p_tx_status
& TXSTS_FSPCAVAIL_MASK
) >> TXSTS_FSPCAVAIL_SHIFT
);
1966 dwc2_hcd_dump_frrem(hsotg
);
1967 dwc2_dump_global_registers(hsotg
);
1968 dwc2_dump_host_registers(hsotg
);
1970 "************************************************************\n");
1971 dev_dbg(hsotg
->dev
, "\n");
1976 * NOTE: This function will be removed once the peripheral controller code
1977 * is integrated and the driver is stable
1979 void dwc2_hcd_dump_frrem(struct dwc2_hsotg
*hsotg
)
1981 #ifdef DWC2_DUMP_FRREM
1982 dev_dbg(hsotg
->dev
, "Frame remaining at SOF:\n");
1983 dev_dbg(hsotg
->dev
, " samples %u, accum %llu, avg %llu\n",
1984 hsotg
->frrem_samples
, hsotg
->frrem_accum
,
1985 hsotg
->frrem_samples
> 0 ?
1986 hsotg
->frrem_accum
/ hsotg
->frrem_samples
: 0);
1987 dev_dbg(hsotg
->dev
, "\n");
1988 dev_dbg(hsotg
->dev
, "Frame remaining at start_transfer (uframe 7):\n");
1989 dev_dbg(hsotg
->dev
, " samples %u, accum %llu, avg %llu\n",
1990 hsotg
->hfnum_7_samples
,
1991 hsotg
->hfnum_7_frrem_accum
,
1992 hsotg
->hfnum_7_samples
> 0 ?
1993 hsotg
->hfnum_7_frrem_accum
/ hsotg
->hfnum_7_samples
: 0);
1994 dev_dbg(hsotg
->dev
, "Frame remaining at start_transfer (uframe 0):\n");
1995 dev_dbg(hsotg
->dev
, " samples %u, accum %llu, avg %llu\n",
1996 hsotg
->hfnum_0_samples
,
1997 hsotg
->hfnum_0_frrem_accum
,
1998 hsotg
->hfnum_0_samples
> 0 ?
1999 hsotg
->hfnum_0_frrem_accum
/ hsotg
->hfnum_0_samples
: 0);
2000 dev_dbg(hsotg
->dev
, "Frame remaining at start_transfer (uframe 1-6):\n");
2001 dev_dbg(hsotg
->dev
, " samples %u, accum %llu, avg %llu\n",
2002 hsotg
->hfnum_other_samples
,
2003 hsotg
->hfnum_other_frrem_accum
,
2004 hsotg
->hfnum_other_samples
> 0 ?
2005 hsotg
->hfnum_other_frrem_accum
/ hsotg
->hfnum_other_samples
:
2007 dev_dbg(hsotg
->dev
, "\n");
2008 dev_dbg(hsotg
->dev
, "Frame remaining at sample point A (uframe 7):\n");
2009 dev_dbg(hsotg
->dev
, " samples %u, accum %llu, avg %llu\n",
2010 hsotg
->hfnum_7_samples_a
, hsotg
->hfnum_7_frrem_accum_a
,
2011 hsotg
->hfnum_7_samples_a
> 0 ?
2012 hsotg
->hfnum_7_frrem_accum_a
/ hsotg
->hfnum_7_samples_a
: 0);
2013 dev_dbg(hsotg
->dev
, "Frame remaining at sample point A (uframe 0):\n");
2014 dev_dbg(hsotg
->dev
, " samples %u, accum %llu, avg %llu\n",
2015 hsotg
->hfnum_0_samples_a
, hsotg
->hfnum_0_frrem_accum_a
,
2016 hsotg
->hfnum_0_samples_a
> 0 ?
2017 hsotg
->hfnum_0_frrem_accum_a
/ hsotg
->hfnum_0_samples_a
: 0);
2018 dev_dbg(hsotg
->dev
, "Frame remaining at sample point A (uframe 1-6):\n");
2019 dev_dbg(hsotg
->dev
, " samples %u, accum %llu, avg %llu\n",
2020 hsotg
->hfnum_other_samples_a
, hsotg
->hfnum_other_frrem_accum_a
,
2021 hsotg
->hfnum_other_samples_a
> 0 ?
2022 hsotg
->hfnum_other_frrem_accum_a
/ hsotg
->hfnum_other_samples_a
2024 dev_dbg(hsotg
->dev
, "\n");
2025 dev_dbg(hsotg
->dev
, "Frame remaining at sample point B (uframe 7):\n");
2026 dev_dbg(hsotg
->dev
, " samples %u, accum %llu, avg %llu\n",
2027 hsotg
->hfnum_7_samples_b
, hsotg
->hfnum_7_frrem_accum_b
,
2028 hsotg
->hfnum_7_samples_b
> 0 ?
2029 hsotg
->hfnum_7_frrem_accum_b
/ hsotg
->hfnum_7_samples_b
: 0);
2030 dev_dbg(hsotg
->dev
, "Frame remaining at sample point B (uframe 0):\n");
2031 dev_dbg(hsotg
->dev
, " samples %u, accum %llu, avg %llu\n",
2032 hsotg
->hfnum_0_samples_b
, hsotg
->hfnum_0_frrem_accum_b
,
2033 (hsotg
->hfnum_0_samples_b
> 0) ?
2034 hsotg
->hfnum_0_frrem_accum_b
/ hsotg
->hfnum_0_samples_b
: 0);
2035 dev_dbg(hsotg
->dev
, "Frame remaining at sample point B (uframe 1-6):\n");
2036 dev_dbg(hsotg
->dev
, " samples %u, accum %llu, avg %llu\n",
2037 hsotg
->hfnum_other_samples_b
, hsotg
->hfnum_other_frrem_accum_b
,
2038 (hsotg
->hfnum_other_samples_b
> 0) ?
2039 hsotg
->hfnum_other_frrem_accum_b
/ hsotg
->hfnum_other_samples_b
2044 struct wrapper_priv_data
{
2045 struct dwc2_hsotg
*hsotg
;
2048 /* Gets the dwc2_hsotg from a usb_hcd */
2049 static struct dwc2_hsotg
*dwc2_hcd_to_hsotg(struct usb_hcd
*hcd
)
2051 struct wrapper_priv_data
*p
;
2053 p
= (struct wrapper_priv_data
*) &hcd
->hcd_priv
;
2057 static int _dwc2_hcd_start(struct usb_hcd
*hcd
);
2059 void dwc2_host_start(struct dwc2_hsotg
*hsotg
)
2061 struct usb_hcd
*hcd
= dwc2_hsotg_to_hcd(hsotg
);
2063 hcd
->self
.is_b_host
= dwc2_hcd_is_b_host(hsotg
);
2064 _dwc2_hcd_start(hcd
);
2067 void dwc2_host_disconnect(struct dwc2_hsotg
*hsotg
)
2069 struct usb_hcd
*hcd
= dwc2_hsotg_to_hcd(hsotg
);
2071 hcd
->self
.is_b_host
= 0;
2074 void dwc2_host_hub_info(struct dwc2_hsotg
*hsotg
, void *context
, int *hub_addr
,
2077 struct urb
*urb
= context
;
2080 *hub_addr
= urb
->dev
->tt
->hub
->devnum
;
2083 *hub_port
= urb
->dev
->ttport
;
2086 int dwc2_host_get_speed(struct dwc2_hsotg
*hsotg
, void *context
)
2088 struct urb
*urb
= context
;
2090 return urb
->dev
->speed
;
2093 static void dwc2_allocate_bus_bandwidth(struct usb_hcd
*hcd
, u16 bw
,
2096 struct usb_bus
*bus
= hcd_to_bus(hcd
);
2099 bus
->bandwidth_allocated
+= bw
/ urb
->interval
;
2100 if (usb_pipetype(urb
->pipe
) == PIPE_ISOCHRONOUS
)
2101 bus
->bandwidth_isoc_reqs
++;
2103 bus
->bandwidth_int_reqs
++;
2106 static void dwc2_free_bus_bandwidth(struct usb_hcd
*hcd
, u16 bw
,
2109 struct usb_bus
*bus
= hcd_to_bus(hcd
);
2112 bus
->bandwidth_allocated
-= bw
/ urb
->interval
;
2113 if (usb_pipetype(urb
->pipe
) == PIPE_ISOCHRONOUS
)
2114 bus
->bandwidth_isoc_reqs
--;
2116 bus
->bandwidth_int_reqs
--;
2120 * Sets the final status of an URB and returns it to the upper layer. Any
2121 * required cleanup of the URB is performed.
2123 * Must be called with interrupt disabled and spinlock held
2125 void dwc2_host_complete(struct dwc2_hsotg
*hsotg
, struct dwc2_qtd
*qtd
,
2132 dev_dbg(hsotg
->dev
, "## %s: qtd is NULL ##\n", __func__
);
2137 dev_dbg(hsotg
->dev
, "## %s: qtd->urb is NULL ##\n", __func__
);
2141 urb
= qtd
->urb
->priv
;
2143 dev_dbg(hsotg
->dev
, "## %s: urb->priv is NULL ##\n", __func__
);
2147 urb
->actual_length
= dwc2_hcd_urb_get_actual_length(qtd
->urb
);
2150 dev_vdbg(hsotg
->dev
,
2151 "%s: urb %p device %d ep %d-%s status %d actual %d\n",
2152 __func__
, urb
, usb_pipedevice(urb
->pipe
),
2153 usb_pipeendpoint(urb
->pipe
),
2154 usb_pipein(urb
->pipe
) ? "IN" : "OUT", status
,
2155 urb
->actual_length
);
2157 if (usb_pipetype(urb
->pipe
) == PIPE_ISOCHRONOUS
&& dbg_perio()) {
2158 for (i
= 0; i
< urb
->number_of_packets
; i
++)
2159 dev_vdbg(hsotg
->dev
, " ISO Desc %d status %d\n",
2160 i
, urb
->iso_frame_desc
[i
].status
);
2163 if (usb_pipetype(urb
->pipe
) == PIPE_ISOCHRONOUS
) {
2164 urb
->error_count
= dwc2_hcd_urb_get_error_count(qtd
->urb
);
2165 for (i
= 0; i
< urb
->number_of_packets
; ++i
) {
2166 urb
->iso_frame_desc
[i
].actual_length
=
2167 dwc2_hcd_urb_get_iso_desc_actual_length(
2169 urb
->iso_frame_desc
[i
].status
=
2170 dwc2_hcd_urb_get_iso_desc_status(qtd
->urb
, i
);
2174 urb
->status
= status
;
2176 if ((urb
->transfer_flags
& URB_SHORT_NOT_OK
) &&
2177 urb
->actual_length
< urb
->transfer_buffer_length
)
2178 urb
->status
= -EREMOTEIO
;
2181 if (usb_pipetype(urb
->pipe
) == PIPE_ISOCHRONOUS
||
2182 usb_pipetype(urb
->pipe
) == PIPE_INTERRUPT
) {
2183 struct usb_host_endpoint
*ep
= urb
->ep
;
2186 dwc2_free_bus_bandwidth(dwc2_hsotg_to_hcd(hsotg
),
2187 dwc2_hcd_get_ep_bandwidth(hsotg
, ep
),
2191 usb_hcd_unlink_urb_from_ep(dwc2_hsotg_to_hcd(hsotg
), urb
);
2196 spin_unlock(&hsotg
->lock
);
2197 usb_hcd_giveback_urb(dwc2_hsotg_to_hcd(hsotg
), urb
, status
);
2198 spin_lock(&hsotg
->lock
);
2202 * Work queue function for starting the HCD when A-Cable is connected
2204 static void dwc2_hcd_start_func(struct work_struct
*work
)
2206 struct dwc2_hsotg
*hsotg
= container_of(work
, struct dwc2_hsotg
,
2209 dev_dbg(hsotg
->dev
, "%s() %p\n", __func__
, hsotg
);
2210 dwc2_host_start(hsotg
);
2214 * Reset work queue function
2216 static void dwc2_hcd_reset_func(struct work_struct
*work
)
2218 struct dwc2_hsotg
*hsotg
= container_of(work
, struct dwc2_hsotg
,
2222 dev_dbg(hsotg
->dev
, "USB RESET function called\n");
2223 hprt0
= dwc2_read_hprt0(hsotg
);
2224 hprt0
&= ~HPRT0_RST
;
2225 writel(hprt0
, hsotg
->regs
+ HPRT0
);
2226 hsotg
->flags
.b
.port_reset_change
= 1;
2230 * =========================================================================
2231 * Linux HC Driver Functions
2232 * =========================================================================
2236 * Initializes the DWC_otg controller and its root hub and prepares it for host
2237 * mode operation. Activates the root port. Returns 0 on success and a negative
2238 * error code on failure.
2240 static int _dwc2_hcd_start(struct usb_hcd
*hcd
)
2242 struct dwc2_hsotg
*hsotg
= dwc2_hcd_to_hsotg(hcd
);
2243 struct usb_bus
*bus
= hcd_to_bus(hcd
);
2244 unsigned long flags
;
2246 dev_dbg(hsotg
->dev
, "DWC OTG HCD START\n");
2248 spin_lock_irqsave(&hsotg
->lock
, flags
);
2250 hcd
->state
= HC_STATE_RUNNING
;
2252 if (dwc2_is_device_mode(hsotg
)) {
2253 spin_unlock_irqrestore(&hsotg
->lock
, flags
);
2254 return 0; /* why 0 ?? */
2257 dwc2_hcd_reinit(hsotg
);
2259 /* Initialize and connect root hub if one is not already attached */
2260 if (bus
->root_hub
) {
2261 dev_dbg(hsotg
->dev
, "DWC OTG HCD Has Root Hub\n");
2262 /* Inform the HUB driver to resume */
2263 usb_hcd_resume_root_hub(hcd
);
2266 spin_unlock_irqrestore(&hsotg
->lock
, flags
);
2271 * Halts the DWC_otg host mode operations in a clean manner. USB transfers are
2274 static void _dwc2_hcd_stop(struct usb_hcd
*hcd
)
2276 struct dwc2_hsotg
*hsotg
= dwc2_hcd_to_hsotg(hcd
);
2277 unsigned long flags
;
2279 spin_lock_irqsave(&hsotg
->lock
, flags
);
2280 dwc2_hcd_stop(hsotg
);
2281 spin_unlock_irqrestore(&hsotg
->lock
, flags
);
2283 usleep_range(1000, 3000);
2286 /* Returns the current frame number */
2287 static int _dwc2_hcd_get_frame_number(struct usb_hcd
*hcd
)
2289 struct dwc2_hsotg
*hsotg
= dwc2_hcd_to_hsotg(hcd
);
2291 return dwc2_hcd_get_frame_number(hsotg
);
2294 static void dwc2_dump_urb_info(struct usb_hcd
*hcd
, struct urb
*urb
,
2297 #ifdef VERBOSE_DEBUG
2298 struct dwc2_hsotg
*hsotg
= dwc2_hcd_to_hsotg(hcd
);
2302 dev_vdbg(hsotg
->dev
, "%s, urb %p\n", fn_name
, urb
);
2303 dev_vdbg(hsotg
->dev
, " Device address: %d\n",
2304 usb_pipedevice(urb
->pipe
));
2305 dev_vdbg(hsotg
->dev
, " Endpoint: %d, %s\n",
2306 usb_pipeendpoint(urb
->pipe
),
2307 usb_pipein(urb
->pipe
) ? "IN" : "OUT");
2309 switch (usb_pipetype(urb
->pipe
)) {
2311 pipetype
= "CONTROL";
2316 case PIPE_INTERRUPT
:
2317 pipetype
= "INTERRUPT";
2319 case PIPE_ISOCHRONOUS
:
2320 pipetype
= "ISOCHRONOUS";
2323 pipetype
= "UNKNOWN";
2327 dev_vdbg(hsotg
->dev
, " Endpoint type: %s %s (%s)\n", pipetype
,
2328 usb_urb_dir_in(urb
) ? "IN" : "OUT", usb_pipein(urb
->pipe
) ?
2331 switch (urb
->dev
->speed
) {
2332 case USB_SPEED_HIGH
:
2335 case USB_SPEED_FULL
:
2346 dev_vdbg(hsotg
->dev
, " Speed: %s\n", speed
);
2347 dev_vdbg(hsotg
->dev
, " Max packet size: %d\n",
2348 usb_maxpacket(urb
->dev
, urb
->pipe
, usb_pipeout(urb
->pipe
)));
2349 dev_vdbg(hsotg
->dev
, " Data buffer length: %d\n",
2350 urb
->transfer_buffer_length
);
2351 dev_vdbg(hsotg
->dev
, " Transfer buffer: %p, Transfer DMA: %08lx\n",
2352 urb
->transfer_buffer
, (unsigned long)urb
->transfer_dma
);
2353 dev_vdbg(hsotg
->dev
, " Setup buffer: %p, Setup DMA: %08lx\n",
2354 urb
->setup_packet
, (unsigned long)urb
->setup_dma
);
2355 dev_vdbg(hsotg
->dev
, " Interval: %d\n", urb
->interval
);
2357 if (usb_pipetype(urb
->pipe
) == PIPE_ISOCHRONOUS
) {
2360 for (i
= 0; i
< urb
->number_of_packets
; i
++) {
2361 dev_vdbg(hsotg
->dev
, " ISO Desc %d:\n", i
);
2362 dev_vdbg(hsotg
->dev
, " offset: %d, length %d\n",
2363 urb
->iso_frame_desc
[i
].offset
,
2364 urb
->iso_frame_desc
[i
].length
);
2371 * Starts processing a USB transfer request specified by a USB Request Block
2372 * (URB). mem_flags indicates the type of memory allocation to use while
2373 * processing this URB.
2375 static int _dwc2_hcd_urb_enqueue(struct usb_hcd
*hcd
, struct urb
*urb
,
2378 struct dwc2_hsotg
*hsotg
= dwc2_hcd_to_hsotg(hcd
);
2379 struct usb_host_endpoint
*ep
= urb
->ep
;
2380 struct dwc2_hcd_urb
*dwc2_urb
;
2383 int alloc_bandwidth
= 0;
2387 unsigned long flags
;
2390 dev_vdbg(hsotg
->dev
, "DWC OTG HCD URB Enqueue\n");
2391 dwc2_dump_urb_info(hcd
, urb
, "urb_enqueue");
2397 if (usb_pipetype(urb
->pipe
) == PIPE_ISOCHRONOUS
||
2398 usb_pipetype(urb
->pipe
) == PIPE_INTERRUPT
) {
2399 spin_lock_irqsave(&hsotg
->lock
, flags
);
2400 if (!dwc2_hcd_is_bandwidth_allocated(hsotg
, ep
))
2401 alloc_bandwidth
= 1;
2402 spin_unlock_irqrestore(&hsotg
->lock
, flags
);
2405 switch (usb_pipetype(urb
->pipe
)) {
2407 ep_type
= USB_ENDPOINT_XFER_CONTROL
;
2409 case PIPE_ISOCHRONOUS
:
2410 ep_type
= USB_ENDPOINT_XFER_ISOC
;
2413 ep_type
= USB_ENDPOINT_XFER_BULK
;
2415 case PIPE_INTERRUPT
:
2416 ep_type
= USB_ENDPOINT_XFER_INT
;
2419 dev_warn(hsotg
->dev
, "Wrong ep type\n");
2422 dwc2_urb
= dwc2_hcd_urb_alloc(hsotg
, urb
->number_of_packets
,
2427 dwc2_hcd_urb_set_pipeinfo(hsotg
, dwc2_urb
, usb_pipedevice(urb
->pipe
),
2428 usb_pipeendpoint(urb
->pipe
), ep_type
,
2429 usb_pipein(urb
->pipe
),
2430 usb_maxpacket(urb
->dev
, urb
->pipe
,
2431 !(usb_pipein(urb
->pipe
))));
2433 buf
= urb
->transfer_buffer
;
2435 if (hcd
->self
.uses_dma
) {
2436 if (!buf
&& (urb
->transfer_dma
& 3)) {
2438 "%s: unaligned transfer with no transfer_buffer",
2445 if (!(urb
->transfer_flags
& URB_NO_INTERRUPT
))
2446 tflags
|= URB_GIVEBACK_ASAP
;
2447 if (urb
->transfer_flags
& URB_ZERO_PACKET
)
2448 tflags
|= URB_SEND_ZERO_PACKET
;
2450 dwc2_urb
->priv
= urb
;
2451 dwc2_urb
->buf
= buf
;
2452 dwc2_urb
->dma
= urb
->transfer_dma
;
2453 dwc2_urb
->length
= urb
->transfer_buffer_length
;
2454 dwc2_urb
->setup_packet
= urb
->setup_packet
;
2455 dwc2_urb
->setup_dma
= urb
->setup_dma
;
2456 dwc2_urb
->flags
= tflags
;
2457 dwc2_urb
->interval
= urb
->interval
;
2458 dwc2_urb
->status
= -EINPROGRESS
;
2460 for (i
= 0; i
< urb
->number_of_packets
; ++i
)
2461 dwc2_hcd_urb_set_iso_desc_params(dwc2_urb
, i
,
2462 urb
->iso_frame_desc
[i
].offset
,
2463 urb
->iso_frame_desc
[i
].length
);
2465 urb
->hcpriv
= dwc2_urb
;
2467 spin_lock_irqsave(&hsotg
->lock
, flags
);
2468 retval
= usb_hcd_link_urb_to_ep(hcd
, urb
);
2469 spin_unlock_irqrestore(&hsotg
->lock
, flags
);
2473 retval
= dwc2_hcd_urb_enqueue(hsotg
, dwc2_urb
, &ep
->hcpriv
, mem_flags
);
2477 if (alloc_bandwidth
) {
2478 spin_lock_irqsave(&hsotg
->lock
, flags
);
2479 dwc2_allocate_bus_bandwidth(hcd
,
2480 dwc2_hcd_get_ep_bandwidth(hsotg
, ep
),
2482 spin_unlock_irqrestore(&hsotg
->lock
, flags
);
2488 spin_lock_irqsave(&hsotg
->lock
, flags
);
2489 dwc2_urb
->priv
= NULL
;
2490 usb_hcd_unlink_urb_from_ep(hcd
, urb
);
2491 spin_unlock_irqrestore(&hsotg
->lock
, flags
);
2500 * Aborts/cancels a USB transfer request. Always returns 0 to indicate success.
2502 static int _dwc2_hcd_urb_dequeue(struct usb_hcd
*hcd
, struct urb
*urb
,
2505 struct dwc2_hsotg
*hsotg
= dwc2_hcd_to_hsotg(hcd
);
2507 unsigned long flags
;
2509 dev_dbg(hsotg
->dev
, "DWC OTG HCD URB Dequeue\n");
2510 dwc2_dump_urb_info(hcd
, urb
, "urb_dequeue");
2512 spin_lock_irqsave(&hsotg
->lock
, flags
);
2514 rc
= usb_hcd_check_unlink_urb(hcd
, urb
, status
);
2519 dev_dbg(hsotg
->dev
, "## urb->hcpriv is NULL ##\n");
2523 rc
= dwc2_hcd_urb_dequeue(hsotg
, urb
->hcpriv
);
2525 usb_hcd_unlink_urb_from_ep(hcd
, urb
);
2530 /* Higher layer software sets URB status */
2531 spin_unlock(&hsotg
->lock
);
2532 usb_hcd_giveback_urb(hcd
, urb
, status
);
2533 spin_lock(&hsotg
->lock
);
2535 dev_dbg(hsotg
->dev
, "Called usb_hcd_giveback_urb()\n");
2536 dev_dbg(hsotg
->dev
, " urb->status = %d\n", urb
->status
);
2538 spin_unlock_irqrestore(&hsotg
->lock
, flags
);
2544 * Frees resources in the DWC_otg controller related to a given endpoint. Also
2545 * clears state in the HCD related to the endpoint. Any URBs for the endpoint
2546 * must already be dequeued.
2548 static void _dwc2_hcd_endpoint_disable(struct usb_hcd
*hcd
,
2549 struct usb_host_endpoint
*ep
)
2551 struct dwc2_hsotg
*hsotg
= dwc2_hcd_to_hsotg(hcd
);
2554 "DWC OTG HCD EP DISABLE: bEndpointAddress=0x%02x, ep->hcpriv=%p\n",
2555 ep
->desc
.bEndpointAddress
, ep
->hcpriv
);
2556 dwc2_hcd_endpoint_disable(hsotg
, ep
, 250);
2560 * Resets endpoint specific parameter values, in current version used to reset
2561 * the data toggle (as a WA). This function can be called from usb_clear_halt
2564 static void _dwc2_hcd_endpoint_reset(struct usb_hcd
*hcd
,
2565 struct usb_host_endpoint
*ep
)
2567 struct dwc2_hsotg
*hsotg
= dwc2_hcd_to_hsotg(hcd
);
2568 unsigned long flags
;
2571 "DWC OTG HCD EP RESET: bEndpointAddress=0x%02x\n",
2572 ep
->desc
.bEndpointAddress
);
2574 spin_lock_irqsave(&hsotg
->lock
, flags
);
2575 dwc2_hcd_endpoint_reset(hsotg
, ep
);
2576 spin_unlock_irqrestore(&hsotg
->lock
, flags
);
2580 * Handles host mode interrupts for the DWC_otg controller. Returns IRQ_NONE if
2581 * there was no interrupt to handle. Returns IRQ_HANDLED if there was a valid
2584 * This function is called by the USB core when an interrupt occurs
2586 static irqreturn_t
_dwc2_hcd_irq(struct usb_hcd
*hcd
)
2588 struct dwc2_hsotg
*hsotg
= dwc2_hcd_to_hsotg(hcd
);
2590 return dwc2_handle_hcd_intr(hsotg
);
2594 * Creates Status Change bitmap for the root hub and root port. The bitmap is
2595 * returned in buf. Bit 0 is the status change indicator for the root hub. Bit 1
2596 * is the status change indicator for the single root port. Returns 1 if either
2597 * change indicator is 1, otherwise returns 0.
2599 static int _dwc2_hcd_hub_status_data(struct usb_hcd
*hcd
, char *buf
)
2601 struct dwc2_hsotg
*hsotg
= dwc2_hcd_to_hsotg(hcd
);
2603 buf
[0] = dwc2_hcd_is_status_changed(hsotg
, 1) << 1;
2607 /* Handles hub class-specific requests */
2608 static int _dwc2_hcd_hub_control(struct usb_hcd
*hcd
, u16 typereq
, u16 wvalue
,
2609 u16 windex
, char *buf
, u16 wlength
)
2611 int retval
= dwc2_hcd_hub_control(dwc2_hcd_to_hsotg(hcd
), typereq
,
2612 wvalue
, windex
, buf
, wlength
);
2616 /* Handles hub TT buffer clear completions */
2617 static void _dwc2_hcd_clear_tt_buffer_complete(struct usb_hcd
*hcd
,
2618 struct usb_host_endpoint
*ep
)
2620 struct dwc2_hsotg
*hsotg
= dwc2_hcd_to_hsotg(hcd
);
2622 unsigned long flags
;
2628 spin_lock_irqsave(&hsotg
->lock
, flags
);
2629 qh
->tt_buffer_dirty
= 0;
2631 if (hsotg
->flags
.b
.port_connect_status
)
2632 dwc2_hcd_queue_transactions(hsotg
, DWC2_TRANSACTION_ALL
);
2634 spin_unlock_irqrestore(&hsotg
->lock
, flags
);
2637 static struct hc_driver dwc2_hc_driver
= {
2638 .description
= "dwc2_hsotg",
2639 .product_desc
= "DWC OTG Controller",
2640 .hcd_priv_size
= sizeof(struct wrapper_priv_data
),
2642 .irq
= _dwc2_hcd_irq
,
2643 .flags
= HCD_MEMORY
| HCD_USB2
,
2645 .start
= _dwc2_hcd_start
,
2646 .stop
= _dwc2_hcd_stop
,
2647 .urb_enqueue
= _dwc2_hcd_urb_enqueue
,
2648 .urb_dequeue
= _dwc2_hcd_urb_dequeue
,
2649 .endpoint_disable
= _dwc2_hcd_endpoint_disable
,
2650 .endpoint_reset
= _dwc2_hcd_endpoint_reset
,
2651 .get_frame_number
= _dwc2_hcd_get_frame_number
,
2653 .hub_status_data
= _dwc2_hcd_hub_status_data
,
2654 .hub_control
= _dwc2_hcd_hub_control
,
2655 .clear_tt_buffer_complete
= _dwc2_hcd_clear_tt_buffer_complete
,
2659 * Frees secondary storage associated with the dwc2_hsotg structure contained
2660 * in the struct usb_hcd field
2662 static void dwc2_hcd_free(struct dwc2_hsotg
*hsotg
)
2668 dev_dbg(hsotg
->dev
, "DWC OTG HCD FREE\n");
2670 /* Free memory for QH/QTD lists */
2671 dwc2_qh_list_free(hsotg
, &hsotg
->non_periodic_sched_inactive
);
2672 dwc2_qh_list_free(hsotg
, &hsotg
->non_periodic_sched_active
);
2673 dwc2_qh_list_free(hsotg
, &hsotg
->periodic_sched_inactive
);
2674 dwc2_qh_list_free(hsotg
, &hsotg
->periodic_sched_ready
);
2675 dwc2_qh_list_free(hsotg
, &hsotg
->periodic_sched_assigned
);
2676 dwc2_qh_list_free(hsotg
, &hsotg
->periodic_sched_queued
);
2678 /* Free memory for the host channels */
2679 for (i
= 0; i
< MAX_EPS_CHANNELS
; i
++) {
2680 struct dwc2_host_chan
*chan
= hsotg
->hc_ptr_array
[i
];
2683 dev_dbg(hsotg
->dev
, "HCD Free channel #%i, chan=%p\n",
2685 hsotg
->hc_ptr_array
[i
] = NULL
;
2690 if (hsotg
->core_params
->dma_enable
> 0) {
2691 if (hsotg
->status_buf
) {
2692 dma_free_coherent(hsotg
->dev
, DWC2_HCD_STATUS_BUF_SIZE
,
2694 hsotg
->status_buf_dma
);
2695 hsotg
->status_buf
= NULL
;
2698 kfree(hsotg
->status_buf
);
2699 hsotg
->status_buf
= NULL
;
2702 ahbcfg
= readl(hsotg
->regs
+ GAHBCFG
);
2704 /* Disable all interrupts */
2705 ahbcfg
&= ~GAHBCFG_GLBL_INTR_EN
;
2706 writel(ahbcfg
, hsotg
->regs
+ GAHBCFG
);
2707 writel(0, hsotg
->regs
+ GINTMSK
);
2709 if (hsotg
->hw_params
.snpsid
>= DWC2_CORE_REV_3_00a
) {
2710 dctl
= readl(hsotg
->regs
+ DCTL
);
2711 dctl
|= DCTL_SFTDISCON
;
2712 writel(dctl
, hsotg
->regs
+ DCTL
);
2715 if (hsotg
->wq_otg
) {
2716 if (!cancel_work_sync(&hsotg
->wf_otg
))
2717 flush_workqueue(hsotg
->wq_otg
);
2718 destroy_workqueue(hsotg
->wq_otg
);
2721 kfree(hsotg
->core_params
);
2722 hsotg
->core_params
= NULL
;
2723 del_timer(&hsotg
->wkp_timer
);
2726 static void dwc2_hcd_release(struct dwc2_hsotg
*hsotg
)
2728 /* Turn off all host-specific interrupts */
2729 dwc2_disable_host_interrupts(hsotg
);
2731 dwc2_hcd_free(hsotg
);
2735 * Sets all parameters to the given value.
2737 * Assumes that the dwc2_core_params struct contains only integers.
2739 void dwc2_set_all_params(struct dwc2_core_params
*params
, int value
)
2741 int *p
= (int *)params
;
2742 size_t size
= sizeof(*params
) / sizeof(*p
);
2745 for (i
= 0; i
< size
; i
++)
2748 EXPORT_SYMBOL_GPL(dwc2_set_all_params
);
2751 * Initializes the HCD. This function allocates memory for and initializes the
2752 * static parts of the usb_hcd and dwc2_hsotg structures. It also registers the
2753 * USB bus with the core and calls the hc_driver->start() function. It returns
2754 * a negative error on failure.
2756 int dwc2_hcd_init(struct dwc2_hsotg
*hsotg
, int irq
,
2757 const struct dwc2_core_params
*params
)
2759 struct usb_hcd
*hcd
;
2760 struct dwc2_host_chan
*channel
;
2762 int i
, num_channels
;
2765 dev_dbg(hsotg
->dev
, "DWC OTG HCD INIT\n");
2767 /* Detect config values from hardware */
2768 retval
= dwc2_get_hwparams(hsotg
);
2775 hcfg
= readl(hsotg
->regs
+ HCFG
);
2776 dev_dbg(hsotg
->dev
, "hcfg=%08x\n", hcfg
);
2778 #ifdef CONFIG_USB_DWC2_TRACK_MISSED_SOFS
2779 hsotg
->frame_num_array
= kzalloc(sizeof(*hsotg
->frame_num_array
) *
2780 FRAME_NUM_ARRAY_SIZE
, GFP_KERNEL
);
2781 if (!hsotg
->frame_num_array
)
2783 hsotg
->last_frame_num_array
= kzalloc(
2784 sizeof(*hsotg
->last_frame_num_array
) *
2785 FRAME_NUM_ARRAY_SIZE
, GFP_KERNEL
);
2786 if (!hsotg
->last_frame_num_array
)
2788 hsotg
->last_frame_num
= HFNUM_MAX_FRNUM
;
2791 hsotg
->core_params
= kzalloc(sizeof(*hsotg
->core_params
), GFP_KERNEL
);
2792 if (!hsotg
->core_params
)
2795 dwc2_set_all_params(hsotg
->core_params
, -1);
2797 /* Validate parameter values */
2798 dwc2_set_parameters(hsotg
, params
);
2800 /* Check if the bus driver or platform code has setup a dma_mask */
2801 if (hsotg
->core_params
->dma_enable
> 0 &&
2802 hsotg
->dev
->dma_mask
== NULL
) {
2803 dev_warn(hsotg
->dev
,
2804 "dma_mask not set, disabling DMA\n");
2805 hsotg
->core_params
->dma_enable
= 0;
2806 hsotg
->core_params
->dma_desc_enable
= 0;
2809 /* Set device flags indicating whether the HCD supports DMA */
2810 if (hsotg
->core_params
->dma_enable
> 0) {
2811 if (dma_set_mask(hsotg
->dev
, DMA_BIT_MASK(32)) < 0)
2812 dev_warn(hsotg
->dev
, "can't set DMA mask\n");
2813 if (dma_set_coherent_mask(hsotg
->dev
, DMA_BIT_MASK(32)) < 0)
2814 dev_warn(hsotg
->dev
, "can't set coherent DMA mask\n");
2817 hcd
= usb_create_hcd(&dwc2_hc_driver
, hsotg
->dev
, dev_name(hsotg
->dev
));
2821 if (hsotg
->core_params
->dma_enable
<= 0)
2822 hcd
->self
.uses_dma
= 0;
2826 spin_lock_init(&hsotg
->lock
);
2827 ((struct wrapper_priv_data
*) &hcd
->hcd_priv
)->hsotg
= hsotg
;
2831 * Disable the global interrupt until all the interrupt handlers are
2834 dwc2_disable_global_interrupts(hsotg
);
2836 /* Initialize the DWC_otg core, and select the Phy type */
2837 retval
= dwc2_core_init(hsotg
, true, irq
);
2841 /* Create new workqueue and init work */
2843 hsotg
->wq_otg
= create_singlethread_workqueue("dwc2");
2844 if (!hsotg
->wq_otg
) {
2845 dev_err(hsotg
->dev
, "Failed to create workqueue\n");
2848 INIT_WORK(&hsotg
->wf_otg
, dwc2_conn_id_status_change
);
2850 setup_timer(&hsotg
->wkp_timer
, dwc2_wakeup_detected
,
2851 (unsigned long)hsotg
);
2853 /* Initialize the non-periodic schedule */
2854 INIT_LIST_HEAD(&hsotg
->non_periodic_sched_inactive
);
2855 INIT_LIST_HEAD(&hsotg
->non_periodic_sched_active
);
2857 /* Initialize the periodic schedule */
2858 INIT_LIST_HEAD(&hsotg
->periodic_sched_inactive
);
2859 INIT_LIST_HEAD(&hsotg
->periodic_sched_ready
);
2860 INIT_LIST_HEAD(&hsotg
->periodic_sched_assigned
);
2861 INIT_LIST_HEAD(&hsotg
->periodic_sched_queued
);
2864 * Create a host channel descriptor for each host channel implemented
2865 * in the controller. Initialize the channel descriptor array.
2867 INIT_LIST_HEAD(&hsotg
->free_hc_list
);
2868 num_channels
= hsotg
->core_params
->host_channels
;
2869 memset(&hsotg
->hc_ptr_array
[0], 0, sizeof(hsotg
->hc_ptr_array
));
2871 for (i
= 0; i
< num_channels
; i
++) {
2872 channel
= kzalloc(sizeof(*channel
), GFP_KERNEL
);
2873 if (channel
== NULL
)
2875 channel
->hc_num
= i
;
2876 hsotg
->hc_ptr_array
[i
] = channel
;
2879 if (hsotg
->core_params
->uframe_sched
> 0)
2880 dwc2_hcd_init_usecs(hsotg
);
2882 /* Initialize hsotg start work */
2883 INIT_DELAYED_WORK(&hsotg
->start_work
, dwc2_hcd_start_func
);
2885 /* Initialize port reset work */
2886 INIT_DELAYED_WORK(&hsotg
->reset_work
, dwc2_hcd_reset_func
);
2889 * Allocate space for storing data on status transactions. Normally no
2890 * data is sent, but this space acts as a bit bucket. This must be
2891 * done after usb_add_hcd since that function allocates the DMA buffer
2894 if (hsotg
->core_params
->dma_enable
> 0)
2895 hsotg
->status_buf
= dma_alloc_coherent(hsotg
->dev
,
2896 DWC2_HCD_STATUS_BUF_SIZE
,
2897 &hsotg
->status_buf_dma
, GFP_KERNEL
);
2899 hsotg
->status_buf
= kzalloc(DWC2_HCD_STATUS_BUF_SIZE
,
2902 if (!hsotg
->status_buf
)
2905 hsotg
->otg_port
= 1;
2906 hsotg
->frame_list
= NULL
;
2907 hsotg
->frame_list_dma
= 0;
2908 hsotg
->periodic_qh_count
= 0;
2910 /* Initiate lx_state to L3 disconnected state */
2911 hsotg
->lx_state
= DWC2_L3
;
2913 hcd
->self
.otg_port
= hsotg
->otg_port
;
2915 /* Don't support SG list at this point */
2916 hcd
->self
.sg_tablesize
= 0;
2919 * Finish generic HCD initialization and start the HCD. This function
2920 * allocates the DMA buffer pool, registers the USB bus, requests the
2921 * IRQ line, and calls hcd_start method.
2923 retval
= usb_add_hcd(hcd
, irq
, IRQF_SHARED
);
2927 device_wakeup_enable(hcd
->self
.controller
);
2929 dwc2_hcd_dump_state(hsotg
);
2931 dwc2_enable_global_interrupts(hsotg
);
2936 dwc2_hcd_release(hsotg
);
2940 kfree(hsotg
->core_params
);
2942 #ifdef CONFIG_USB_DWC2_TRACK_MISSED_SOFS
2943 kfree(hsotg
->last_frame_num_array
);
2944 kfree(hsotg
->frame_num_array
);
2947 dev_err(hsotg
->dev
, "%s() FAILED, returning %d\n", __func__
, retval
);
2950 EXPORT_SYMBOL_GPL(dwc2_hcd_init
);
2954 * Frees memory and resources associated with the HCD and deregisters the bus.
2956 void dwc2_hcd_remove(struct dwc2_hsotg
*hsotg
)
2958 struct usb_hcd
*hcd
;
2960 dev_dbg(hsotg
->dev
, "DWC OTG HCD REMOVE\n");
2962 hcd
= dwc2_hsotg_to_hcd(hsotg
);
2963 dev_dbg(hsotg
->dev
, "hsotg->hcd = %p\n", hcd
);
2966 dev_dbg(hsotg
->dev
, "%s: dwc2_hsotg_to_hcd(hsotg) NULL!\n",
2971 usb_remove_hcd(hcd
);
2973 dwc2_hcd_release(hsotg
);
2976 #ifdef CONFIG_USB_DWC2_TRACK_MISSED_SOFS
2977 kfree(hsotg
->last_frame_num_array
);
2978 kfree(hsotg
->frame_num_array
);
2981 EXPORT_SYMBOL_GPL(dwc2_hcd_remove
);