3 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
5 * Copyright (c) 2015 Daisuke Aoyama. All rights reserved.
6 * Copyright (c) 2012-2015 Hans Petter Selasky. All rights reserved.
7 * Copyright (c) 2010-2011 Aleksandr Rybalko. All rights reserved.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * This file contains the driver for the DesignWare series USB 2.0 OTG
37 * LIMITATION: Drivers must be bound to all OUT endpoints in the
38 * active configuration for this driver to work properly. Blocking any
39 * OUT endpoint will block all OUT endpoints including the control
40 * endpoint. Usually this is not a problem.
44 * NOTE: Writing to non-existing registers appears to cause an
48 #ifdef USB_GLOBAL_INCLUDE_FILE
49 #include USB_GLOBAL_INCLUDE_FILE
51 #include <sys/stdint.h>
52 #include <sys/stddef.h>
53 #include <sys/param.h>
54 #include <sys/queue.h>
55 #include <sys/types.h>
56 #include <sys/systm.h>
57 #include <sys/kernel.h>
59 #include <sys/module.h>
61 #include <sys/mutex.h>
62 #include <sys/condvar.h>
63 #include <sys/sysctl.h>
65 #include <sys/unistd.h>
66 #include <sys/callout.h>
67 #include <sys/malloc.h>
70 #include <dev/usb/usb.h>
71 #include <dev/usb/usbdi.h>
73 #define USB_DEBUG_VAR dwc_otg_debug
75 #include <dev/usb/usb_core.h>
76 #include <dev/usb/usb_debug.h>
77 #include <dev/usb/usb_busdma.h>
78 #include <dev/usb/usb_process.h>
79 #include <dev/usb/usb_transfer.h>
80 #include <dev/usb/usb_device.h>
81 #include <dev/usb/usb_hub.h>
82 #include <dev/usb/usb_util.h>
84 #include <dev/usb/usb_controller.h>
85 #include <dev/usb/usb_bus.h>
86 #endif /* USB_GLOBAL_INCLUDE_FILE */
88 #include <dev/usb/controller/dwc_otg.h>
89 #include <dev/usb/controller/dwc_otgreg.h>
91 #define DWC_OTG_BUS2SC(bus) \
92 ((struct dwc_otg_softc *)(((uint8_t *)(bus)) - \
93 ((uint8_t *)&(((struct dwc_otg_softc *)0)->sc_bus))))
95 #define DWC_OTG_PC2UDEV(pc) \
96 (USB_DMATAG_TO_XROOT((pc)->tag_parent)->udev)
98 #define DWC_OTG_MSK_GINT_THREAD_IRQ \
99 (GINTSTS_USBRST | GINTSTS_ENUMDONE | GINTSTS_PRTINT | \
100 GINTSTS_WKUPINT | GINTSTS_USBSUSP | GINTMSK_OTGINTMSK | \
103 #ifndef DWC_OTG_PHY_DEFAULT
104 #define DWC_OTG_PHY_DEFAULT DWC_OTG_PHY_ULPI
107 static int dwc_otg_phy_type = DWC_OTG_PHY_DEFAULT;
109 static SYSCTL_NODE(_hw_usb, OID_AUTO, dwc_otg, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
111 SYSCTL_INT(_hw_usb_dwc_otg, OID_AUTO, phy_type, CTLFLAG_RDTUN,
112 &dwc_otg_phy_type, 0, "DWC OTG PHY TYPE - 0/1/2/3 - ULPI/HSIC/INTERNAL/UTMI+");
115 static int dwc_otg_debug = 0;
117 SYSCTL_INT(_hw_usb_dwc_otg, OID_AUTO, debug, CTLFLAG_RWTUN,
118 &dwc_otg_debug, 0, "DWC OTG debug level");
121 #define DWC_OTG_INTR_ENDPT 1
125 static const struct usb_bus_methods dwc_otg_bus_methods;
126 static const struct usb_pipe_methods dwc_otg_device_non_isoc_methods;
127 static const struct usb_pipe_methods dwc_otg_device_isoc_methods;
129 static dwc_otg_cmd_t dwc_otg_setup_rx;
130 static dwc_otg_cmd_t dwc_otg_data_rx;
131 static dwc_otg_cmd_t dwc_otg_data_tx;
132 static dwc_otg_cmd_t dwc_otg_data_tx_sync;
134 static dwc_otg_cmd_t dwc_otg_host_setup_tx;
135 static dwc_otg_cmd_t dwc_otg_host_data_tx;
136 static dwc_otg_cmd_t dwc_otg_host_data_rx;
138 static void dwc_otg_device_done(struct usb_xfer *, usb_error_t);
139 static void dwc_otg_do_poll(struct usb_bus *);
140 static void dwc_otg_standard_done(struct usb_xfer *);
141 static void dwc_otg_root_intr(struct dwc_otg_softc *);
142 static void dwc_otg_interrupt_poll_locked(struct dwc_otg_softc *);
145 * Here is a configuration that the chip supports.
147 static const struct usb_hw_ep_profile dwc_otg_ep_profile[1] = {
150 .max_in_frame_size = 64,/* fixed */
151 .max_out_frame_size = 64, /* fixed */
153 .support_control = 1,
158 dwc_otg_get_hw_ep_profile(struct usb_device *udev,
159 const struct usb_hw_ep_profile **ppf, uint8_t ep_addr)
161 struct dwc_otg_softc *sc;
163 sc = DWC_OTG_BUS2SC(udev->bus);
165 if (ep_addr < sc->sc_dev_ep_max)
166 *ppf = &sc->sc_hw_ep_profile[ep_addr].usb;
172 dwc_otg_write_fifo(struct dwc_otg_softc *sc, struct usb_page_cache *pc,
173 uint32_t offset, uint32_t fifo, uint32_t count)
177 /* round down length to nearest 4-bytes */
180 /* check if we can write the data directly */
181 if (temp != 0 && usb_pc_buffer_is_aligned(pc, offset, temp, 3)) {
182 struct usb_page_search buf_res;
184 /* pre-subtract length */
187 /* iterate buffer list */
189 /* get current buffer pointer */
190 usbd_get_page(pc, offset, &buf_res);
192 if (buf_res.length > temp)
193 buf_res.length = temp;
195 /* transfer data into FIFO */
196 bus_space_write_region_4(sc->sc_io_tag, sc->sc_io_hdl,
197 fifo, buf_res.buffer, buf_res.length / 4);
199 offset += buf_res.length;
200 fifo += buf_res.length;
201 temp -= buf_res.length;
205 /* check for remainder */
207 /* clear topmost word before copy */
208 sc->sc_bounce_buffer[(count - 1) / 4] = 0;
211 usbd_copy_out(pc, offset,
212 sc->sc_bounce_buffer, count);
214 /* transfer data into FIFO */
215 bus_space_write_region_4(sc->sc_io_tag,
216 sc->sc_io_hdl, fifo, sc->sc_bounce_buffer,
222 dwc_otg_read_fifo(struct dwc_otg_softc *sc, struct usb_page_cache *pc,
223 uint32_t offset, uint32_t count)
227 /* round down length to nearest 4-bytes */
230 /* check if we can read the data directly */
231 if (temp != 0 && usb_pc_buffer_is_aligned(pc, offset, temp, 3)) {
232 struct usb_page_search buf_res;
234 /* pre-subtract length */
237 /* iterate buffer list */
239 /* get current buffer pointer */
240 usbd_get_page(pc, offset, &buf_res);
242 if (buf_res.length > temp)
243 buf_res.length = temp;
245 /* transfer data from FIFO */
246 bus_space_read_region_4(sc->sc_io_tag, sc->sc_io_hdl,
247 sc->sc_current_rx_fifo, buf_res.buffer, buf_res.length / 4);
249 offset += buf_res.length;
250 sc->sc_current_rx_fifo += buf_res.length;
251 sc->sc_current_rx_bytes -= buf_res.length;
252 temp -= buf_res.length;
256 /* check for remainder */
258 /* read data into bounce buffer */
259 bus_space_read_region_4(sc->sc_io_tag, sc->sc_io_hdl,
260 sc->sc_current_rx_fifo,
261 sc->sc_bounce_buffer, (count + 3) / 4);
263 /* store data into proper buffer */
264 usbd_copy_in(pc, offset, sc->sc_bounce_buffer, count);
266 /* round length up to nearest 4 bytes */
267 count = (count + 3) & ~3;
269 /* update counters */
270 sc->sc_current_rx_bytes -= count;
271 sc->sc_current_rx_fifo += count;
276 dwc_otg_tx_fifo_reset(struct dwc_otg_softc *sc, uint32_t value)
281 DWC_OTG_WRITE_4(sc, DOTG_GRSTCTL, value);
283 /* wait for reset to complete */
284 for (temp = 0; temp != 16; temp++) {
285 value = DWC_OTG_READ_4(sc, DOTG_GRSTCTL);
286 if (!(value & (GRSTCTL_TXFFLSH | GRSTCTL_RXFFLSH)))
292 dwc_otg_init_fifo(struct dwc_otg_softc *sc, uint8_t mode)
294 struct dwc_otg_profile *pf;
300 fifo_size = sc->sc_fifo_size;
303 * NOTE: Reserved fixed size area at end of RAM, which must
304 * not be allocated to the FIFOs:
308 if (fifo_size < fifo_regs) {
309 DPRINTF("Too little FIFO\n");
313 /* subtract FIFO regs from total once */
314 fifo_size -= fifo_regs;
316 /* split equally for IN and OUT */
319 /* Align to 4 bytes boundary (refer to PGM) */
322 /* set global receive FIFO size */
323 DWC_OTG_WRITE_4(sc, DOTG_GRXFSIZ, fifo_size / 4);
325 tx_start = fifo_size;
327 if (fifo_size < 64) {
328 DPRINTFN(-1, "Not enough data space for EP0 FIFO.\n");
332 if (mode == DWC_MODE_HOST) {
334 /* reset active endpoints */
335 sc->sc_active_rx_ep = 0;
337 /* split equally for periodic and non-periodic */
340 DPRINTF("PTX/NPTX FIFO=%u\n", fifo_size);
342 /* align to 4 bytes boundary */
345 DWC_OTG_WRITE_4(sc, DOTG_GNPTXFSIZ,
346 ((fifo_size / 4) << 16) |
349 tx_start += fifo_size;
351 for (x = 0; x != sc->sc_host_ch_max; x++) {
352 /* enable all host interrupts */
353 DWC_OTG_WRITE_4(sc, DOTG_HCINTMSK(x),
357 DWC_OTG_WRITE_4(sc, DOTG_HPTXFSIZ,
358 ((fifo_size / 4) << 16) |
361 /* reset host channel state */
362 memset(sc->sc_chan_state, 0, sizeof(sc->sc_chan_state));
364 /* enable all host channel interrupts */
365 DWC_OTG_WRITE_4(sc, DOTG_HAINTMSK,
366 (1U << sc->sc_host_ch_max) - 1U);
368 /* enable proper host channel interrupts */
369 sc->sc_irq_mask |= GINTMSK_HCHINTMSK;
370 sc->sc_irq_mask &= ~GINTMSK_IEPINTMSK;
371 DWC_OTG_WRITE_4(sc, DOTG_GINTMSK, sc->sc_irq_mask);
374 if (mode == DWC_MODE_DEVICE) {
376 DWC_OTG_WRITE_4(sc, DOTG_GNPTXFSIZ,
377 (0x10 << 16) | (tx_start / 4));
381 /* setup control endpoint profile */
382 sc->sc_hw_ep_profile[0].usb = dwc_otg_ep_profile[0];
384 /* reset active endpoints */
385 sc->sc_active_rx_ep = 1;
387 for (x = 1; x != sc->sc_dev_ep_max; x++) {
389 pf = sc->sc_hw_ep_profile + x;
391 pf->usb.max_out_frame_size = 1024 * 3;
392 pf->usb.is_simplex = 0; /* assume duplex */
393 pf->usb.support_bulk = 1;
394 pf->usb.support_interrupt = 1;
395 pf->usb.support_isochronous = 1;
396 pf->usb.support_out = 1;
398 if (x < sc->sc_dev_in_ep_max) {
401 limit = (x == 1) ? MIN(DWC_OTG_TX_MAX_FIFO_SIZE,
402 DWC_OTG_MAX_TXN) : MIN(DWC_OTG_MAX_TXN / 2,
403 DWC_OTG_TX_MAX_FIFO_SIZE);
405 /* see if there is enough FIFO space */
406 if (limit <= fifo_size) {
407 pf->max_buffer = limit;
408 pf->usb.support_in = 1;
410 limit = MIN(DWC_OTG_TX_MAX_FIFO_SIZE, 0x40);
411 if (limit <= fifo_size) {
412 pf->usb.support_in = 1;
414 pf->usb.is_simplex = 1;
419 DWC_OTG_WRITE_4(sc, DOTG_DIEPTXF(x),
420 ((limit / 4) << 16) | (tx_start / 4));
423 pf->usb.max_in_frame_size = limit;
425 pf->usb.is_simplex = 1;
428 DPRINTF("FIFO%d = IN:%d / OUT:%d\n", x,
429 pf->usb.max_in_frame_size,
430 pf->usb.max_out_frame_size);
433 /* enable proper device channel interrupts */
434 sc->sc_irq_mask &= ~GINTMSK_HCHINTMSK;
435 sc->sc_irq_mask |= GINTMSK_IEPINTMSK;
436 DWC_OTG_WRITE_4(sc, DOTG_GINTMSK, sc->sc_irq_mask);
440 dwc_otg_tx_fifo_reset(sc, GRSTCTL_RXFFLSH);
442 if (mode != DWC_MODE_OTG) {
443 /* reset all TX FIFOs */
444 dwc_otg_tx_fifo_reset(sc,
445 GRSTCTL_TXFIFO(0x10) |
448 /* reset active endpoints */
449 sc->sc_active_rx_ep = 0;
451 /* reset host channel state */
452 memset(sc->sc_chan_state, 0, sizeof(sc->sc_chan_state));
458 dwc_otg_uses_split(struct usb_device *udev)
461 * When a LOW or FULL speed device is connected directly to
462 * the USB port we don't use split transactions:
464 return (udev->speed != USB_SPEED_HIGH &&
465 udev->parent_hs_hub != NULL &&
466 udev->parent_hs_hub->parent_hub != NULL);
470 dwc_otg_update_host_frame_interval(struct dwc_otg_softc *sc)
474 * Disabled until further. Assuming that the register is already
475 * programmed correctly by the boot loader.
480 /* setup HOST frame interval register, based on existing value */
481 temp = DWC_OTG_READ_4(sc, DOTG_HFIR) & HFIR_FRINT_MASK;
487 /* figure out nearest X-tal value */
495 if (sc->sc_flags.status_high_speed)
500 DPRINTF("HFIR=0x%08x\n", temp);
502 DWC_OTG_WRITE_4(sc, DOTG_HFIR, temp);
507 dwc_otg_clocks_on(struct dwc_otg_softc *sc)
509 if (sc->sc_flags.clocks_off &&
510 sc->sc_flags.port_powered) {
514 /* TODO - platform specific */
516 sc->sc_flags.clocks_off = 0;
521 dwc_otg_clocks_off(struct dwc_otg_softc *sc)
523 if (!sc->sc_flags.clocks_off) {
527 /* TODO - platform specific */
529 sc->sc_flags.clocks_off = 1;
534 dwc_otg_pull_up(struct dwc_otg_softc *sc)
538 /* pullup D+, if possible */
540 if (!sc->sc_flags.d_pulled_up &&
541 sc->sc_flags.port_powered) {
542 sc->sc_flags.d_pulled_up = 1;
544 temp = DWC_OTG_READ_4(sc, DOTG_DCTL);
545 temp &= ~DCTL_SFTDISCON;
546 DWC_OTG_WRITE_4(sc, DOTG_DCTL, temp);
551 dwc_otg_pull_down(struct dwc_otg_softc *sc)
555 /* pulldown D+, if possible */
557 if (sc->sc_flags.d_pulled_up) {
558 sc->sc_flags.d_pulled_up = 0;
560 temp = DWC_OTG_READ_4(sc, DOTG_DCTL);
561 temp |= DCTL_SFTDISCON;
562 DWC_OTG_WRITE_4(sc, DOTG_DCTL, temp);
567 dwc_otg_enable_sof_irq(struct dwc_otg_softc *sc)
569 /* In device mode we don't use the SOF interrupt */
570 if (sc->sc_flags.status_device_mode != 0)
572 /* Ensure the SOF interrupt is not disabled */
574 /* Check if the SOF interrupt is already enabled */
575 if ((sc->sc_irq_mask & GINTMSK_SOFMSK) != 0)
577 sc->sc_irq_mask |= GINTMSK_SOFMSK;
578 DWC_OTG_WRITE_4(sc, DOTG_GINTMSK, sc->sc_irq_mask);
582 dwc_otg_resume_irq(struct dwc_otg_softc *sc)
584 if (sc->sc_flags.status_suspend) {
585 /* update status bits */
586 sc->sc_flags.status_suspend = 0;
587 sc->sc_flags.change_suspend = 1;
589 if (sc->sc_flags.status_device_mode) {
591 * Disable resume interrupt and enable suspend
594 sc->sc_irq_mask &= ~GINTMSK_WKUPINTMSK;
595 sc->sc_irq_mask |= GINTMSK_USBSUSPMSK;
596 DWC_OTG_WRITE_4(sc, DOTG_GINTMSK, sc->sc_irq_mask);
599 /* complete root HUB interrupt endpoint */
600 dwc_otg_root_intr(sc);
605 dwc_otg_suspend_irq(struct dwc_otg_softc *sc)
607 if (!sc->sc_flags.status_suspend) {
608 /* update status bits */
609 sc->sc_flags.status_suspend = 1;
610 sc->sc_flags.change_suspend = 1;
612 if (sc->sc_flags.status_device_mode) {
614 * Disable suspend interrupt and enable resume
617 sc->sc_irq_mask &= ~GINTMSK_USBSUSPMSK;
618 sc->sc_irq_mask |= GINTMSK_WKUPINTMSK;
619 DWC_OTG_WRITE_4(sc, DOTG_GINTMSK, sc->sc_irq_mask);
622 /* complete root HUB interrupt endpoint */
623 dwc_otg_root_intr(sc);
628 dwc_otg_wakeup_peer(struct dwc_otg_softc *sc)
630 if (!sc->sc_flags.status_suspend)
633 DPRINTFN(5, "Remote wakeup\n");
635 if (sc->sc_flags.status_device_mode) {
638 /* enable remote wakeup signalling */
639 temp = DWC_OTG_READ_4(sc, DOTG_DCTL);
640 temp |= DCTL_RMTWKUPSIG;
641 DWC_OTG_WRITE_4(sc, DOTG_DCTL, temp);
643 /* Wait 8ms for remote wakeup to complete. */
644 usb_pause_mtx(&sc->sc_bus.bus_mtx, hz / 125);
646 temp &= ~DCTL_RMTWKUPSIG;
647 DWC_OTG_WRITE_4(sc, DOTG_DCTL, temp);
649 /* enable USB port */
650 DWC_OTG_WRITE_4(sc, DOTG_PCGCCTL, 0);
653 usb_pause_mtx(&sc->sc_bus.bus_mtx, hz / 100);
656 sc->sc_hprt_val |= HPRT_PRTRES;
657 DWC_OTG_WRITE_4(sc, DOTG_HPRT, sc->sc_hprt_val);
659 /* Wait 100ms for resume signalling to complete. */
660 usb_pause_mtx(&sc->sc_bus.bus_mtx, hz / 10);
662 /* clear suspend and resume */
663 sc->sc_hprt_val &= ~(HPRT_PRTSUSP | HPRT_PRTRES);
664 DWC_OTG_WRITE_4(sc, DOTG_HPRT, sc->sc_hprt_val);
667 usb_pause_mtx(&sc->sc_bus.bus_mtx, hz / 250);
670 /* need to fake resume IRQ */
671 dwc_otg_resume_irq(sc);
675 dwc_otg_set_address(struct dwc_otg_softc *sc, uint8_t addr)
679 DPRINTFN(5, "addr=%d\n", addr);
681 temp = DWC_OTG_READ_4(sc, DOTG_DCFG);
682 temp &= ~DCFG_DEVADDR_SET(0x7F);
683 temp |= DCFG_DEVADDR_SET(addr);
684 DWC_OTG_WRITE_4(sc, DOTG_DCFG, temp);
688 dwc_otg_common_rx_ack(struct dwc_otg_softc *sc)
690 DPRINTFN(5, "RX status clear\n");
692 /* enable RX FIFO level interrupt */
693 sc->sc_irq_mask |= GINTMSK_RXFLVLMSK;
694 DWC_OTG_WRITE_4(sc, DOTG_GINTMSK, sc->sc_irq_mask);
696 if (sc->sc_current_rx_bytes != 0) {
697 /* need to dump remaining data */
698 bus_space_read_region_4(sc->sc_io_tag, sc->sc_io_hdl,
699 sc->sc_current_rx_fifo, sc->sc_bounce_buffer,
700 sc->sc_current_rx_bytes / 4);
701 /* clear number of active bytes to receive */
702 sc->sc_current_rx_bytes = 0;
704 /* clear cached status */
705 sc->sc_last_rx_status = 0;
709 dwc_otg_clear_hcint(struct dwc_otg_softc *sc, uint8_t x)
713 /* clear all pending interrupts */
714 hcint = DWC_OTG_READ_4(sc, DOTG_HCINT(x));
715 DWC_OTG_WRITE_4(sc, DOTG_HCINT(x), hcint);
717 /* clear buffered interrupts */
718 sc->sc_chan_state[x].hcint = 0;
722 dwc_otg_host_check_tx_fifo_empty(struct dwc_otg_softc *sc, struct dwc_otg_td *td)
726 temp = DWC_OTG_READ_4(sc, DOTG_GINTSTS);
728 if (td->ep_type == UE_ISOCHRONOUS) {
730 * NOTE: USB INTERRUPT transactions are executed like
731 * USB CONTROL transactions! See the setup standard
732 * chain function for more information.
734 if (!(temp & GINTSTS_PTXFEMP)) {
735 DPRINTF("Periodic TX FIFO is not empty\n");
736 if (!(sc->sc_irq_mask & GINTMSK_PTXFEMPMSK)) {
737 sc->sc_irq_mask |= GINTMSK_PTXFEMPMSK;
738 DWC_OTG_WRITE_4(sc, DOTG_GINTMSK, sc->sc_irq_mask);
740 return (1); /* busy */
743 if (!(temp & GINTSTS_NPTXFEMP)) {
744 DPRINTF("Non-periodic TX FIFO is not empty\n");
745 if (!(sc->sc_irq_mask & GINTMSK_NPTXFEMPMSK)) {
746 sc->sc_irq_mask |= GINTMSK_NPTXFEMPMSK;
747 DWC_OTG_WRITE_4(sc, DOTG_GINTMSK, sc->sc_irq_mask);
749 return (1); /* busy */
752 return (0); /* ready for transmit */
756 dwc_otg_host_channel_alloc(struct dwc_otg_softc *sc,
757 struct dwc_otg_td *td, uint8_t is_out)
763 if (td->channel[0] < DWC_OTG_MAX_CHANNELS)
764 return (0); /* already allocated */
766 /* check if device is suspended */
767 if (DWC_OTG_PC2UDEV(td->pc)->flags.self_suspended != 0)
768 return (1); /* busy - cannot transfer data */
770 /* compute needed TX FIFO size */
772 if (dwc_otg_host_check_tx_fifo_empty(sc, td) != 0)
773 return (1); /* busy - cannot transfer data */
775 z = td->max_packet_count;
776 for (x = y = 0; x != sc->sc_host_ch_max; x++) {
777 /* check if channel is allocated */
778 if (sc->sc_chan_state[x].allocated != 0)
780 /* check if channel is still enabled */
781 if (sc->sc_chan_state[x].wait_halted != 0)
783 /* store channel number */
784 td->channel[y++] = x;
785 /* check if we got all channels */
790 /* reset channel variable */
791 td->channel[0] = DWC_OTG_MAX_CHANNELS;
792 td->channel[1] = DWC_OTG_MAX_CHANNELS;
793 td->channel[2] = DWC_OTG_MAX_CHANNELS;
795 dwc_otg_enable_sof_irq(sc);
796 return (1); /* busy - not enough channels */
799 for (y = 0; y != z; y++) {
803 sc->sc_chan_state[x].allocated = 1;
805 /* set wait halted */
806 sc->sc_chan_state[x].wait_halted = 1;
808 /* clear interrupts */
809 dwc_otg_clear_hcint(sc, x);
811 DPRINTF("CH=%d HCCHAR=0x%08x "
812 "HCSPLT=0x%08x\n", x, td->hcchar, td->hcsplt);
814 /* set active channel */
815 sc->sc_active_rx_ep |= (1 << x);
817 return (0); /* allocated */
821 dwc_otg_host_channel_free_sub(struct dwc_otg_softc *sc, struct dwc_otg_td *td, uint8_t index)
826 if (td->channel[index] >= DWC_OTG_MAX_CHANNELS)
827 return; /* already freed */
830 x = td->channel[index];
831 td->channel[index] = DWC_OTG_MAX_CHANNELS;
833 DPRINTF("CH=%d\n", x);
836 * We need to let programmed host channels run till complete
837 * else the host channel will stop functioning.
839 sc->sc_chan_state[x].allocated = 0;
841 /* ack any pending messages */
842 if (sc->sc_last_rx_status != 0 &&
843 GRXSTSRD_CHNUM_GET(sc->sc_last_rx_status) == x) {
844 dwc_otg_common_rx_ack(sc);
847 /* clear active channel */
848 sc->sc_active_rx_ep &= ~(1 << x);
850 /* check if already halted */
851 if (sc->sc_chan_state[x].wait_halted == 0)
854 /* disable host channel */
855 hcchar = DWC_OTG_READ_4(sc, DOTG_HCCHAR(x));
856 if (hcchar & HCCHAR_CHENA) {
857 DPRINTF("Halting channel %d\n", x);
858 DWC_OTG_WRITE_4(sc, DOTG_HCCHAR(x),
859 hcchar | HCCHAR_CHDIS);
860 /* don't write HCCHAR until the channel is halted */
862 sc->sc_chan_state[x].wait_halted = 0;
867 dwc_otg_host_channel_free(struct dwc_otg_softc *sc, struct dwc_otg_td *td)
870 for (x = 0; x != td->max_packet_count; x++)
871 dwc_otg_host_channel_free_sub(sc, td, x);
875 dwc_otg_host_dump_rx(struct dwc_otg_softc *sc, struct dwc_otg_td *td)
878 /* dump any pending messages */
879 if (sc->sc_last_rx_status == 0)
881 for (x = 0; x != td->max_packet_count; x++) {
882 if (td->channel[x] >= DWC_OTG_MAX_CHANNELS ||
883 td->channel[x] != GRXSTSRD_CHNUM_GET(sc->sc_last_rx_status))
885 dwc_otg_common_rx_ack(sc);
891 dwc_otg_host_setup_tx(struct dwc_otg_softc *sc, struct dwc_otg_td *td)
893 struct usb_device_request req __aligned(4);
898 dwc_otg_host_dump_rx(sc, td);
900 if (td->channel[0] < DWC_OTG_MAX_CHANNELS) {
901 hcint = sc->sc_chan_state[td->channel[0]].hcint;
903 DPRINTF("CH=%d ST=%d HCINT=0x%08x HCCHAR=0x%08x HCTSIZ=0x%08x\n",
904 td->channel[0], td->state, hcint,
905 DWC_OTG_READ_4(sc, DOTG_HCCHAR(td->channel[0])),
906 DWC_OTG_READ_4(sc, DOTG_HCTSIZ(td->channel[0])));
912 if (hcint & (HCINT_RETRY |
913 HCINT_ACK | HCINT_NYET)) {
914 /* give success bits priority over failure bits */
915 } else if (hcint & HCINT_STALL) {
916 DPRINTF("CH=%d STALL\n", td->channel[0]);
920 } else if (hcint & HCINT_ERRORS) {
921 DPRINTF("CH=%d ERROR\n", td->channel[0]);
923 if (td->hcsplt != 0 || td->errcnt >= 3) {
929 if (hcint & (HCINT_ERRORS | HCINT_RETRY |
930 HCINT_ACK | HCINT_NYET)) {
931 if (!(hcint & HCINT_ERRORS))
937 case DWC_CHAN_ST_START:
940 case DWC_CHAN_ST_WAIT_ANE:
941 if (hcint & (HCINT_RETRY | HCINT_ERRORS)) {
943 td->tt_scheduled = 0;
945 } else if (hcint & (HCINT_ACK | HCINT_NYET)) {
946 td->offset += td->tx_bytes;
947 td->remainder -= td->tx_bytes;
949 td->tt_scheduled = 0;
954 case DWC_CHAN_ST_WAIT_S_ANE:
955 if (hcint & (HCINT_RETRY | HCINT_ERRORS)) {
957 td->tt_scheduled = 0;
959 } else if (hcint & (HCINT_ACK | HCINT_NYET)) {
964 case DWC_CHAN_ST_WAIT_C_ANE:
965 if (hcint & HCINT_NYET) {
967 } else if (hcint & (HCINT_RETRY | HCINT_ERRORS)) {
969 td->tt_scheduled = 0;
971 } else if (hcint & HCINT_ACK) {
972 td->offset += td->tx_bytes;
973 td->remainder -= td->tx_bytes;
979 case DWC_CHAN_ST_WAIT_C_PKT:
988 /* free existing channel, if any */
989 dwc_otg_host_channel_free(sc, td);
991 if (sizeof(req) != td->remainder) {
996 if (td->hcsplt != 0) {
997 delta = td->tt_start_slot - sc->sc_last_frame_num - 1;
998 if (td->tt_scheduled == 0 || delta < DWC_OTG_TT_SLOT_MAX) {
999 td->state = DWC_CHAN_ST_START;
1002 delta = sc->sc_last_frame_num - td->tt_start_slot;
1005 td->tt_scheduled = 0;
1006 td->state = DWC_CHAN_ST_START;
1011 /* allocate a new channel */
1012 if (dwc_otg_host_channel_alloc(sc, td, 1)) {
1013 td->state = DWC_CHAN_ST_START;
1017 if (td->hcsplt != 0) {
1018 td->hcsplt &= ~HCSPLT_COMPSPLT;
1019 td->state = DWC_CHAN_ST_WAIT_S_ANE;
1021 td->state = DWC_CHAN_ST_WAIT_ANE;
1024 /* copy out control request */
1025 usbd_copy_out(td->pc, 0, &req, sizeof(req));
1027 DWC_OTG_WRITE_4(sc, DOTG_HCTSIZ(td->channel[0]),
1028 (sizeof(req) << HCTSIZ_XFERSIZE_SHIFT) |
1029 (1 << HCTSIZ_PKTCNT_SHIFT) |
1030 (HCTSIZ_PID_SETUP << HCTSIZ_PID_SHIFT));
1032 DWC_OTG_WRITE_4(sc, DOTG_HCSPLT(td->channel[0]), td->hcsplt);
1034 hcchar = td->hcchar;
1035 hcchar &= ~(HCCHAR_EPDIR_IN | HCCHAR_EPTYPE_MASK);
1036 hcchar |= UE_CONTROL << HCCHAR_EPTYPE_SHIFT;
1038 /* must enable channel before writing data to FIFO */
1039 DWC_OTG_WRITE_4(sc, DOTG_HCCHAR(td->channel[0]), hcchar);
1041 /* transfer data into FIFO */
1042 bus_space_write_region_4(sc->sc_io_tag, sc->sc_io_hdl,
1043 DOTG_DFIFO(td->channel[0]), (uint32_t *)&req, sizeof(req) / 4);
1045 /* wait until next slot before trying complete split */
1046 td->tt_complete_slot = sc->sc_last_frame_num + 1;
1048 /* store number of bytes transmitted */
1049 td->tx_bytes = sizeof(req);
1053 /* free existing channel, if any */
1054 dwc_otg_host_channel_free(sc, td);
1056 delta = td->tt_complete_slot - sc->sc_last_frame_num - 1;
1057 if (td->tt_scheduled == 0 || delta < DWC_OTG_TT_SLOT_MAX) {
1058 td->state = DWC_CHAN_ST_WAIT_C_PKT;
1061 delta = sc->sc_last_frame_num - td->tt_start_slot;
1062 if (delta > DWC_OTG_TT_SLOT_MAX) {
1063 /* we missed the service interval */
1064 if (td->ep_type != UE_ISOCHRONOUS)
1068 /* allocate a new channel */
1069 if (dwc_otg_host_channel_alloc(sc, td, 0)) {
1070 td->state = DWC_CHAN_ST_WAIT_C_PKT;
1074 /* wait until next slot before trying complete split */
1075 td->tt_complete_slot = sc->sc_last_frame_num + 1;
1077 td->hcsplt |= HCSPLT_COMPSPLT;
1078 td->state = DWC_CHAN_ST_WAIT_C_ANE;
1080 DWC_OTG_WRITE_4(sc, DOTG_HCTSIZ(td->channel[0]),
1081 (HCTSIZ_PID_SETUP << HCTSIZ_PID_SHIFT));
1083 DWC_OTG_WRITE_4(sc, DOTG_HCSPLT(td->channel[0]), td->hcsplt);
1085 hcchar = td->hcchar;
1086 hcchar &= ~(HCCHAR_EPDIR_IN | HCCHAR_EPTYPE_MASK);
1087 hcchar |= UE_CONTROL << HCCHAR_EPTYPE_SHIFT;
1089 /* must enable channel before writing data to FIFO */
1090 DWC_OTG_WRITE_4(sc, DOTG_HCCHAR(td->channel[0]), hcchar);
1093 return (1); /* busy */
1096 dwc_otg_host_channel_free(sc, td);
1097 return (0); /* complete */
1101 dwc_otg_setup_rx(struct dwc_otg_softc *sc, struct dwc_otg_td *td)
1103 struct usb_device_request req __aligned(4);
1107 /* check endpoint status */
1109 if (sc->sc_last_rx_status == 0)
1112 if (GRXSTSRD_CHNUM_GET(sc->sc_last_rx_status) != 0)
1115 if ((sc->sc_last_rx_status & GRXSTSRD_PKTSTS_MASK) !=
1116 GRXSTSRD_STP_DATA) {
1117 if ((sc->sc_last_rx_status & GRXSTSRD_PKTSTS_MASK) !=
1118 GRXSTSRD_STP_COMPLETE || td->remainder != 0) {
1120 dwc_otg_common_rx_ack(sc);
1124 dwc_otg_common_rx_ack(sc);
1125 return (0); /* complete */
1128 if ((sc->sc_last_rx_status & GRXSTSRD_DPID_MASK) !=
1129 GRXSTSRD_DPID_DATA0) {
1131 dwc_otg_common_rx_ack(sc);
1135 DPRINTFN(5, "GRXSTSR=0x%08x\n", sc->sc_last_rx_status);
1137 /* clear did stall */
1140 /* get the packet byte count */
1141 count = GRXSTSRD_BCNT_GET(sc->sc_last_rx_status);
1143 if (count != sizeof(req)) {
1144 DPRINTFN(0, "Unsupported SETUP packet "
1145 "length, %d bytes\n", count);
1147 dwc_otg_common_rx_ack(sc);
1152 dwc_otg_read_fifo(sc, td->pc, 0, sizeof(req));
1154 /* copy out control request */
1155 usbd_copy_out(td->pc, 0, &req, sizeof(req));
1157 td->offset = sizeof(req);
1160 /* sneak peek the set address */
1161 if ((req.bmRequestType == UT_WRITE_DEVICE) &&
1162 (req.bRequest == UR_SET_ADDRESS)) {
1163 /* must write address before ZLP */
1164 dwc_otg_set_address(sc, req.wValue[0] & 0x7F);
1167 /* don't send any data by default */
1168 DWC_OTG_WRITE_4(sc, DOTG_DIEPTSIZ(0), DIEPCTL_EPDIS);
1169 DWC_OTG_WRITE_4(sc, DOTG_DOEPTSIZ(0), DOEPCTL_EPDIS);
1171 /* reset IN endpoint buffer */
1172 dwc_otg_tx_fifo_reset(sc,
1176 /* acknowledge RX status */
1177 dwc_otg_common_rx_ack(sc);
1181 /* abort any ongoing transfer, before enabling again */
1182 if (!td->did_stall) {
1185 DPRINTFN(5, "stalling IN and OUT direction\n");
1187 temp = sc->sc_out_ctl[0];
1189 /* set stall after enabling endpoint */
1190 DWC_OTG_WRITE_4(sc, DOTG_DOEPCTL(0),
1191 temp | DOEPCTL_STALL);
1193 temp = sc->sc_in_ctl[0];
1195 /* set stall assuming endpoint is enabled */
1196 DWC_OTG_WRITE_4(sc, DOTG_DIEPCTL(0),
1197 temp | DIEPCTL_STALL);
1199 return (1); /* not complete */
1203 dwc_otg_host_rate_check_interrupt(struct dwc_otg_softc *sc, struct dwc_otg_td *td)
1207 delta = sc->sc_tmr_val - td->tmr_val;
1209 return (1); /* busy */
1211 td->tmr_val = sc->sc_tmr_val + td->tmr_res;
1213 /* set toggle, if any */
1214 if (td->set_toggle) {
1222 dwc_otg_host_rate_check(struct dwc_otg_softc *sc, struct dwc_otg_td *td)
1224 uint8_t frame_num = (uint8_t)sc->sc_last_frame_num;
1226 if (td->ep_type == UE_ISOCHRONOUS) {
1227 /* non TT isochronous traffic */
1228 if (frame_num & (td->tmr_res - 1))
1230 if ((frame_num ^ td->tmr_val) & td->tmr_res)
1232 td->tmr_val = td->tmr_res + sc->sc_last_frame_num;
1235 } else if (td->ep_type == UE_INTERRUPT) {
1236 if (!td->tt_scheduled)
1238 td->tt_scheduled = 0;
1240 } else if (td->did_nak != 0) {
1241 /* check if we should pause sending queries for 125us */
1242 if (td->tmr_res == frame_num) {
1244 dwc_otg_enable_sof_irq(sc);
1247 } else if (td->set_toggle) {
1251 /* query for data one more time */
1252 td->tmr_res = frame_num;
1260 dwc_otg_host_data_rx_sub(struct dwc_otg_softc *sc, struct dwc_otg_td *td,
1265 /* check endpoint status */
1266 if (sc->sc_last_rx_status == 0)
1269 if (channel >= DWC_OTG_MAX_CHANNELS)
1272 if (GRXSTSRD_CHNUM_GET(sc->sc_last_rx_status) != channel)
1275 switch (sc->sc_last_rx_status & GRXSTSRD_PKTSTS_MASK) {
1276 case GRXSTSRH_IN_DATA:
1278 DPRINTF("DATA ST=%d STATUS=0x%08x\n",
1279 (int)td->state, (int)sc->sc_last_rx_status);
1281 if (sc->sc_chan_state[channel].hcint & HCINT_SOFTWARE_ONLY) {
1283 * When using SPLIT transactions on interrupt
1284 * endpoints, sometimes data occurs twice.
1286 DPRINTF("Data already received\n");
1290 /* get the packet byte count */
1291 count = GRXSTSRD_BCNT_GET(sc->sc_last_rx_status);
1293 /* check for ISOCHRONOUS endpoint */
1294 if (td->ep_type == UE_ISOCHRONOUS) {
1295 if ((sc->sc_last_rx_status & GRXSTSRD_DPID_MASK) !=
1296 GRXSTSRD_DPID_DATA0) {
1297 /* more data to be received */
1298 td->tt_xactpos = HCSPLT_XACTPOS_MIDDLE;
1300 /* all data received */
1301 td->tt_xactpos = HCSPLT_XACTPOS_BEGIN;
1302 /* verify the packet byte count */
1303 if (count != td->remainder) {
1304 /* we have a short packet */
1310 /* verify the packet byte count */
1311 if (count != td->max_packet_size) {
1312 if (count < td->max_packet_size) {
1313 /* we have a short packet */
1317 /* invalid USB packet */
1321 dwc_otg_common_rx_ack(sc);
1326 td->tt_scheduled = 0;
1329 /* verify the packet byte count */
1330 if (count > td->remainder) {
1331 /* invalid USB packet */
1335 dwc_otg_common_rx_ack(sc);
1339 /* read data from FIFO */
1340 dwc_otg_read_fifo(sc, td->pc, td->offset, count);
1342 td->remainder -= count;
1343 td->offset += count;
1344 sc->sc_chan_state[channel].hcint |= HCINT_SOFTWARE_ONLY;
1350 dwc_otg_common_rx_ack(sc);
1358 dwc_otg_host_data_rx(struct dwc_otg_softc *sc, struct dwc_otg_td *td)
1366 for (x = 0; x != td->max_packet_count; x++) {
1367 channel = td->channel[x];
1368 if (channel >= DWC_OTG_MAX_CHANNELS)
1370 hcint |= sc->sc_chan_state[channel].hcint;
1372 DPRINTF("CH=%d ST=%d HCINT=0x%08x HCCHAR=0x%08x HCTSIZ=0x%08x\n",
1373 channel, td->state, hcint,
1374 DWC_OTG_READ_4(sc, DOTG_HCCHAR(channel)),
1375 DWC_OTG_READ_4(sc, DOTG_HCTSIZ(channel)));
1377 /* check interrupt bits */
1378 if (hcint & (HCINT_RETRY |
1379 HCINT_ACK | HCINT_NYET)) {
1380 /* give success bits priority over failure bits */
1381 } else if (hcint & HCINT_STALL) {
1382 DPRINTF("CH=%d STALL\n", channel);
1383 td->error_stall = 1;
1386 } else if (hcint & HCINT_ERRORS) {
1387 DPRINTF("CH=%d ERROR\n", channel);
1389 if (td->hcsplt != 0 || td->errcnt >= 3) {
1390 if (td->ep_type != UE_ISOCHRONOUS) {
1397 /* check channels for data, if any */
1398 if (dwc_otg_host_data_rx_sub(sc, td, channel))
1401 /* refresh interrupt status */
1402 hcint |= sc->sc_chan_state[channel].hcint;
1404 if (hcint & (HCINT_ERRORS | HCINT_RETRY |
1405 HCINT_ACK | HCINT_NYET)) {
1406 if (!(hcint & HCINT_ERRORS))
1411 switch (td->state) {
1412 case DWC_CHAN_ST_START:
1413 if (td->hcsplt != 0)
1418 case DWC_CHAN_ST_WAIT_ANE:
1419 if (hcint & (HCINT_RETRY | HCINT_ERRORS)) {
1420 if (td->ep_type == UE_INTERRUPT) {
1422 * The USB specification does not
1423 * mandate a particular data toggle
1424 * value for USB INTERRUPT
1425 * transfers. Switch the data toggle
1426 * value to receive the packet
1429 if (hcint & HCINT_DATATGLERR) {
1430 DPRINTF("Retrying packet due to "
1431 "data toggle error\n");
1435 } else if (td->ep_type == UE_ISOCHRONOUS) {
1436 if (td->hcsplt != 0) {
1438 * Sometimes the complete
1439 * split packet may be queued
1441 * transaction translator will
1442 * return a NAK. Ignore
1443 * this message and retry the
1444 * complete split instead.
1446 DPRINTF("Retrying complete split\n");
1452 td->tt_scheduled = 0;
1453 if (td->hcsplt != 0)
1457 } else if (hcint & HCINT_NYET) {
1458 if (td->hcsplt != 0) {
1462 /* not a valid token for IN endpoints */
1466 } else if (hcint & HCINT_ACK) {
1467 /* wait for data - ACK arrived first */
1468 if (!(hcint & HCINT_SOFTWARE_ONLY))
1471 if (td->ep_type == UE_ISOCHRONOUS) {
1472 /* check if we are complete */
1473 if (td->tt_xactpos == HCSPLT_XACTPOS_BEGIN) {
1475 } else if (td->hcsplt != 0) {
1478 /* get more packets */
1482 /* check if we are complete */
1483 if ((td->remainder == 0) || (td->got_short != 0)) {
1488 * Else need to receive a zero length
1492 td->tt_scheduled = 0;
1494 if (td->hcsplt != 0)
1502 case DWC_CHAN_ST_WAIT_S_ANE:
1504 * NOTE: The DWC OTG hardware provides a fake ACK in
1505 * case of interrupt and isochronous transfers:
1507 if (hcint & (HCINT_RETRY | HCINT_ERRORS)) {
1509 td->tt_scheduled = 0;
1511 } else if (hcint & HCINT_NYET) {
1512 td->tt_scheduled = 0;
1514 } else if (hcint & HCINT_ACK) {
1520 case DWC_CHAN_ST_WAIT_C_PKT:
1529 /* free existing channel, if any */
1530 dwc_otg_host_channel_free(sc, td);
1532 if (td->hcsplt != 0) {
1533 delta = td->tt_complete_slot - sc->sc_last_frame_num - 1;
1534 if (td->tt_scheduled == 0 || delta < DWC_OTG_TT_SLOT_MAX) {
1535 if (td->ep_type != UE_ISOCHRONOUS) {
1536 td->state = DWC_CHAN_ST_WAIT_C_PKT;
1540 delta = sc->sc_last_frame_num - td->tt_start_slot;
1541 if (delta > DWC_OTG_TT_SLOT_MAX) {
1542 if (td->ep_type != UE_ISOCHRONOUS) {
1543 /* we missed the service interval */
1548 /* complete split */
1549 td->hcsplt |= HCSPLT_COMPSPLT;
1550 } else if (dwc_otg_host_rate_check(sc, td)) {
1551 td->state = DWC_CHAN_ST_WAIT_C_PKT;
1555 /* allocate a new channel */
1556 if (dwc_otg_host_channel_alloc(sc, td, 0)) {
1557 td->state = DWC_CHAN_ST_WAIT_C_PKT;
1561 /* set toggle, if any */
1562 if (td->set_toggle) {
1567 td->state = DWC_CHAN_ST_WAIT_ANE;
1569 for (x = 0; x != td->max_packet_count; x++) {
1570 channel = td->channel[x];
1572 /* receive one packet */
1573 DWC_OTG_WRITE_4(sc, DOTG_HCTSIZ(channel),
1574 (td->max_packet_size << HCTSIZ_XFERSIZE_SHIFT) |
1575 (1 << HCTSIZ_PKTCNT_SHIFT) |
1576 (td->toggle ? (HCTSIZ_PID_DATA1 << HCTSIZ_PID_SHIFT) :
1577 (HCTSIZ_PID_DATA0 << HCTSIZ_PID_SHIFT)));
1579 DWC_OTG_WRITE_4(sc, DOTG_HCSPLT(channel), td->hcsplt);
1581 hcchar = td->hcchar;
1582 hcchar |= HCCHAR_EPDIR_IN;
1584 if (td->ep_type == UE_ISOCHRONOUS) {
1585 if (td->hcsplt != 0) {
1586 /* continously buffer */
1587 if (sc->sc_last_frame_num & 1)
1588 hcchar &= ~HCCHAR_ODDFRM;
1590 hcchar |= HCCHAR_ODDFRM;
1592 /* multi buffer, if any */
1593 if (sc->sc_last_frame_num & 1)
1594 hcchar |= HCCHAR_ODDFRM;
1596 hcchar &= ~HCCHAR_ODDFRM;
1599 hcchar &= ~HCCHAR_ODDFRM;
1602 /* must enable channel before data can be received */
1603 DWC_OTG_WRITE_4(sc, DOTG_HCCHAR(channel), hcchar);
1605 /* wait until next slot before trying complete split */
1606 td->tt_complete_slot = sc->sc_last_frame_num + 1;
1611 /* free existing channel(s), if any */
1612 dwc_otg_host_channel_free(sc, td);
1614 delta = td->tt_start_slot - sc->sc_last_frame_num - 1;
1615 if (td->tt_scheduled == 0 || delta < DWC_OTG_TT_SLOT_MAX) {
1616 td->state = DWC_CHAN_ST_START;
1619 delta = sc->sc_last_frame_num - td->tt_start_slot;
1622 td->tt_scheduled = 0;
1623 td->state = DWC_CHAN_ST_START;
1627 /* allocate a new channel */
1628 if (dwc_otg_host_channel_alloc(sc, td, 0)) {
1629 td->state = DWC_CHAN_ST_START;
1633 channel = td->channel[0];
1635 td->hcsplt &= ~HCSPLT_COMPSPLT;
1636 td->state = DWC_CHAN_ST_WAIT_S_ANE;
1638 /* receive one packet */
1639 DWC_OTG_WRITE_4(sc, DOTG_HCTSIZ(channel),
1640 (HCTSIZ_PID_DATA0 << HCTSIZ_PID_SHIFT));
1642 DWC_OTG_WRITE_4(sc, DOTG_HCSPLT(channel), td->hcsplt);
1644 /* send after next SOF event */
1645 if ((sc->sc_last_frame_num & 1) == 0 &&
1646 td->ep_type == UE_ISOCHRONOUS)
1647 td->hcchar |= HCCHAR_ODDFRM;
1649 td->hcchar &= ~HCCHAR_ODDFRM;
1651 hcchar = td->hcchar;
1652 hcchar |= HCCHAR_EPDIR_IN;
1654 /* wait until next slot before trying complete split */
1655 td->tt_complete_slot = sc->sc_last_frame_num + 1;
1657 /* must enable channel before data can be received */
1658 DWC_OTG_WRITE_4(sc, DOTG_HCCHAR(channel), hcchar);
1660 return (1); /* busy */
1663 dwc_otg_host_channel_free(sc, td);
1664 return (0); /* complete */
1668 dwc_otg_data_rx(struct dwc_otg_softc *sc, struct dwc_otg_td *td)
1676 /* check endpoint status */
1677 if (sc->sc_last_rx_status == 0)
1680 if (GRXSTSRD_CHNUM_GET(sc->sc_last_rx_status) != td->ep_no)
1683 /* check for SETUP packet */
1684 if ((sc->sc_last_rx_status & GRXSTSRD_PKTSTS_MASK) ==
1685 GRXSTSRD_STP_DATA ||
1686 (sc->sc_last_rx_status & GRXSTSRD_PKTSTS_MASK) ==
1687 GRXSTSRD_STP_COMPLETE) {
1688 if (td->remainder == 0) {
1690 * We are actually complete and have
1691 * received the next SETUP
1693 DPRINTFN(5, "faking complete\n");
1694 return (0); /* complete */
1697 * USB Host Aborted the transfer.
1700 return (0); /* complete */
1703 if ((sc->sc_last_rx_status & GRXSTSRD_PKTSTS_MASK) !=
1704 GRXSTSRD_OUT_DATA) {
1706 dwc_otg_common_rx_ack(sc);
1710 /* get the packet byte count */
1711 count = GRXSTSRD_BCNT_GET(sc->sc_last_rx_status);
1713 /* verify the packet byte count */
1714 if (count != td->max_packet_size) {
1715 if (count < td->max_packet_size) {
1716 /* we have a short packet */
1720 /* invalid USB packet */
1724 dwc_otg_common_rx_ack(sc);
1725 return (0); /* we are complete */
1728 /* verify the packet byte count */
1729 if (count > td->remainder) {
1730 /* invalid USB packet */
1734 dwc_otg_common_rx_ack(sc);
1735 return (0); /* we are complete */
1738 /* read data from FIFO */
1739 dwc_otg_read_fifo(sc, td->pc, td->offset, count);
1741 td->remainder -= count;
1742 td->offset += count;
1745 dwc_otg_common_rx_ack(sc);
1747 temp = sc->sc_out_ctl[td->ep_no];
1749 /* check for isochronous mode */
1750 if ((temp & DIEPCTL_EPTYPE_MASK) ==
1751 (DIEPCTL_EPTYPE_ISOC << DIEPCTL_EPTYPE_SHIFT)) {
1752 /* toggle odd or even frame bit */
1753 if (temp & DIEPCTL_SETD1PID) {
1754 temp &= ~DIEPCTL_SETD1PID;
1755 temp |= DIEPCTL_SETD0PID;
1757 temp &= ~DIEPCTL_SETD0PID;
1758 temp |= DIEPCTL_SETD1PID;
1760 sc->sc_out_ctl[td->ep_no] = temp;
1763 /* check if we are complete */
1764 if ((td->remainder == 0) || got_short) {
1765 if (td->short_pkt) {
1766 /* we are complete */
1769 /* else need to receive a zero length packet */
1774 /* enable SETUP and transfer complete interrupt */
1775 if (td->ep_no == 0) {
1776 DWC_OTG_WRITE_4(sc, DOTG_DOEPTSIZ(0),
1777 DXEPTSIZ_SET_MULTI(3) |
1778 DXEPTSIZ_SET_NPKT(1) |
1779 DXEPTSIZ_SET_NBYTES(td->max_packet_size));
1781 /* allow reception of multiple packets */
1782 DWC_OTG_WRITE_4(sc, DOTG_DOEPTSIZ(td->ep_no),
1783 DXEPTSIZ_SET_MULTI(1) |
1784 DXEPTSIZ_SET_NPKT(4) |
1785 DXEPTSIZ_SET_NBYTES(4 *
1786 ((td->max_packet_size + 3) & ~3)));
1788 temp = sc->sc_out_ctl[td->ep_no];
1789 DWC_OTG_WRITE_4(sc, DOTG_DOEPCTL(td->ep_no), temp |
1790 DOEPCTL_EPENA | DOEPCTL_CNAK);
1792 return (1); /* not complete */
1796 dwc_otg_host_data_tx(struct dwc_otg_softc *sc, struct dwc_otg_td *td)
1805 dwc_otg_host_dump_rx(sc, td);
1807 /* check that last channel is complete */
1808 channel = td->channel[td->npkt];
1810 if (channel < DWC_OTG_MAX_CHANNELS) {
1811 hcint = sc->sc_chan_state[channel].hcint;
1813 DPRINTF("CH=%d ST=%d HCINT=0x%08x HCCHAR=0x%08x HCTSIZ=0x%08x\n",
1814 channel, td->state, hcint,
1815 DWC_OTG_READ_4(sc, DOTG_HCCHAR(channel)),
1816 DWC_OTG_READ_4(sc, DOTG_HCTSIZ(channel)));
1818 if (hcint & (HCINT_RETRY |
1819 HCINT_ACK | HCINT_NYET)) {
1820 /* give success bits priority over failure bits */
1821 } else if (hcint & HCINT_STALL) {
1822 DPRINTF("CH=%d STALL\n", channel);
1823 td->error_stall = 1;
1826 } else if (hcint & HCINT_ERRORS) {
1827 DPRINTF("CH=%d ERROR\n", channel);
1829 if (td->hcsplt != 0 || td->errcnt >= 3) {
1835 if (hcint & (HCINT_ERRORS | HCINT_RETRY |
1836 HCINT_ACK | HCINT_NYET)) {
1838 if (!(hcint & HCINT_ERRORS))
1845 switch (td->state) {
1846 case DWC_CHAN_ST_START:
1849 case DWC_CHAN_ST_WAIT_ANE:
1850 if (hcint & (HCINT_RETRY | HCINT_ERRORS)) {
1852 td->tt_scheduled = 0;
1854 } else if (hcint & (HCINT_ACK | HCINT_NYET)) {
1855 td->offset += td->tx_bytes;
1856 td->remainder -= td->tx_bytes;
1858 /* check if next response will be a NAK */
1859 if (hcint & HCINT_NYET)
1863 td->tt_scheduled = 0;
1865 /* check remainder */
1866 if (td->remainder == 0) {
1871 * Else we need to transmit a short
1879 case DWC_CHAN_ST_WAIT_S_ANE:
1880 if (hcint & (HCINT_RETRY | HCINT_ERRORS)) {
1882 td->tt_scheduled = 0;
1884 } else if (hcint & (HCINT_ACK | HCINT_NYET)) {
1890 case DWC_CHAN_ST_WAIT_C_ANE:
1891 if (hcint & HCINT_NYET) {
1893 } else if (hcint & (HCINT_RETRY | HCINT_ERRORS)) {
1895 td->tt_scheduled = 0;
1897 } else if (hcint & HCINT_ACK) {
1898 td->offset += td->tx_bytes;
1899 td->remainder -= td->tx_bytes;
1902 td->tt_scheduled = 0;
1904 /* check remainder */
1905 if (td->remainder == 0) {
1909 /* else we need to transmit a short packet */
1915 case DWC_CHAN_ST_WAIT_C_PKT:
1918 case DWC_CHAN_ST_TX_WAIT_ISOC:
1919 /* Check if ISOCHRONOUS OUT traffic is complete */
1920 if ((hcint & HCINT_HCH_DONE_MASK) == 0)
1923 td->offset += td->tx_bytes;
1924 td->remainder -= td->tx_bytes;
1932 /* free existing channel(s), if any */
1933 dwc_otg_host_channel_free(sc, td);
1935 if (td->hcsplt != 0) {
1936 delta = td->tt_start_slot - sc->sc_last_frame_num - 1;
1937 if (td->tt_scheduled == 0 || delta < DWC_OTG_TT_SLOT_MAX) {
1938 td->state = DWC_CHAN_ST_START;
1941 delta = sc->sc_last_frame_num - td->tt_start_slot;
1944 td->tt_scheduled = 0;
1945 td->state = DWC_CHAN_ST_START;
1948 } else if (dwc_otg_host_rate_check(sc, td)) {
1949 td->state = DWC_CHAN_ST_START;
1953 /* allocate a new channel */
1954 if (dwc_otg_host_channel_alloc(sc, td, 1)) {
1955 td->state = DWC_CHAN_ST_START;
1959 /* set toggle, if any */
1960 if (td->set_toggle) {
1965 if (td->ep_type == UE_ISOCHRONOUS) {
1966 /* ISOCHRONOUS OUT transfers don't have any ACKs */
1967 td->state = DWC_CHAN_ST_TX_WAIT_ISOC;
1968 td->hcsplt &= ~HCSPLT_COMPSPLT;
1969 if (td->hcsplt != 0) {
1970 /* get maximum transfer length */
1971 count = td->remainder;
1972 if (count > HCSPLT_XACTLEN_BURST) {
1973 DPRINTF("TT overflow\n");
1977 /* Update transaction position */
1978 td->hcsplt &= ~HCSPLT_XACTPOS_MASK;
1979 td->hcsplt |= (HCSPLT_XACTPOS_ALL << HCSPLT_XACTPOS_SHIFT);
1981 } else if (td->hcsplt != 0) {
1982 td->hcsplt &= ~HCSPLT_COMPSPLT;
1983 /* Wait for ACK/NAK/ERR from TT */
1984 td->state = DWC_CHAN_ST_WAIT_S_ANE;
1986 /* Wait for ACK/NAK/STALL from device */
1987 td->state = DWC_CHAN_ST_WAIT_ANE;
1992 for (x = 0; x != td->max_packet_count; x++) {
1995 channel = td->channel[x];
1997 /* send one packet at a time */
1998 count = td->max_packet_size;
1999 rem_bytes = td->remainder - td->tx_bytes;
2000 if (rem_bytes < count) {
2001 /* we have a short packet */
2005 if (count == rem_bytes) {
2009 DWC_OTG_WRITE_4(sc, DOTG_HCTSIZ(channel),
2010 (count << HCTSIZ_XFERSIZE_SHIFT) |
2011 (1 << HCTSIZ_PKTCNT_SHIFT) |
2012 (td->toggle ? (HCTSIZ_PID_DATA1 << HCTSIZ_PID_SHIFT) :
2013 (HCTSIZ_PID_DATA0 << HCTSIZ_PID_SHIFT)));
2016 DWC_OTG_WRITE_4(sc, DOTG_HCTSIZ(channel),
2017 (count << HCTSIZ_XFERSIZE_SHIFT) |
2018 (1 << HCTSIZ_PKTCNT_SHIFT) |
2019 (HCTSIZ_PID_DATA1 << HCTSIZ_PID_SHIFT));
2022 DWC_OTG_WRITE_4(sc, DOTG_HCTSIZ(channel),
2023 (count << HCTSIZ_XFERSIZE_SHIFT) |
2024 (1 << HCTSIZ_PKTCNT_SHIFT) |
2025 (HCTSIZ_PID_DATA2 << HCTSIZ_PID_SHIFT));
2028 } else if (td->ep_type == UE_ISOCHRONOUS &&
2029 td->max_packet_count > 1) {
2030 /* ISOCHRONOUS multi packet */
2031 DWC_OTG_WRITE_4(sc, DOTG_HCTSIZ(channel),
2032 (count << HCTSIZ_XFERSIZE_SHIFT) |
2033 (1 << HCTSIZ_PKTCNT_SHIFT) |
2034 (HCTSIZ_PID_MDATA << HCTSIZ_PID_SHIFT));
2036 /* TODO: HCTSIZ_DOPNG */
2037 /* standard BULK/INTERRUPT/CONTROL packet */
2038 DWC_OTG_WRITE_4(sc, DOTG_HCTSIZ(channel),
2039 (count << HCTSIZ_XFERSIZE_SHIFT) |
2040 (1 << HCTSIZ_PKTCNT_SHIFT) |
2041 (td->toggle ? (HCTSIZ_PID_DATA1 << HCTSIZ_PID_SHIFT) :
2042 (HCTSIZ_PID_DATA0 << HCTSIZ_PID_SHIFT)));
2045 DWC_OTG_WRITE_4(sc, DOTG_HCSPLT(channel), td->hcsplt);
2047 hcchar = td->hcchar;
2048 hcchar &= ~HCCHAR_EPDIR_IN;
2050 /* send after next SOF event */
2051 if ((sc->sc_last_frame_num & 1) == 0 &&
2052 td->ep_type == UE_ISOCHRONOUS)
2053 hcchar |= HCCHAR_ODDFRM;
2055 hcchar &= ~HCCHAR_ODDFRM;
2057 /* must enable before writing data to FIFO */
2058 DWC_OTG_WRITE_4(sc, DOTG_HCCHAR(channel), hcchar);
2061 /* write data into FIFO */
2062 dwc_otg_write_fifo(sc, td->pc, td->offset +
2063 td->tx_bytes, DOTG_DFIFO(channel), count);
2066 /* store number of bytes transmitted */
2067 td->tx_bytes += count;
2069 /* store last packet index */
2072 /* check for last packet */
2073 if (count == rem_bytes)
2079 /* free existing channel, if any */
2080 dwc_otg_host_channel_free(sc, td);
2082 delta = td->tt_complete_slot - sc->sc_last_frame_num - 1;
2083 if (td->tt_scheduled == 0 || delta < DWC_OTG_TT_SLOT_MAX) {
2084 td->state = DWC_CHAN_ST_WAIT_C_PKT;
2087 delta = sc->sc_last_frame_num - td->tt_start_slot;
2088 if (delta > DWC_OTG_TT_SLOT_MAX) {
2089 /* we missed the service interval */
2090 if (td->ep_type != UE_ISOCHRONOUS)
2095 /* allocate a new channel */
2096 if (dwc_otg_host_channel_alloc(sc, td, 0)) {
2097 td->state = DWC_CHAN_ST_WAIT_C_PKT;
2101 channel = td->channel[0];
2103 td->hcsplt |= HCSPLT_COMPSPLT;
2104 td->state = DWC_CHAN_ST_WAIT_C_ANE;
2106 DWC_OTG_WRITE_4(sc, DOTG_HCTSIZ(channel),
2107 (HCTSIZ_PID_DATA0 << HCTSIZ_PID_SHIFT));
2109 DWC_OTG_WRITE_4(sc, DOTG_HCSPLT(channel), td->hcsplt);
2111 hcchar = td->hcchar;
2112 hcchar &= ~HCCHAR_EPDIR_IN;
2114 /* receive complete split ASAP */
2115 if ((sc->sc_last_frame_num & 1) != 0 &&
2116 td->ep_type == UE_ISOCHRONOUS)
2117 hcchar |= HCCHAR_ODDFRM;
2119 hcchar &= ~HCCHAR_ODDFRM;
2121 /* must enable channel before data can be received */
2122 DWC_OTG_WRITE_4(sc, DOTG_HCCHAR(channel), hcchar);
2124 /* wait until next slot before trying complete split */
2125 td->tt_complete_slot = sc->sc_last_frame_num + 1;
2127 return (1); /* busy */
2130 dwc_otg_host_channel_free(sc, td);
2131 return (0); /* complete */
2135 dwc_otg_data_tx(struct dwc_otg_softc *sc, struct dwc_otg_td *td)
2137 uint32_t max_buffer;
2144 to = 3; /* don't loop forever! */
2146 max_buffer = sc->sc_hw_ep_profile[td->ep_no].max_buffer;
2149 /* check for for endpoint 0 data */
2151 temp = sc->sc_last_rx_status;
2153 if ((td->ep_no == 0) && (temp != 0) &&
2154 (GRXSTSRD_CHNUM_GET(temp) == 0)) {
2156 if ((temp & GRXSTSRD_PKTSTS_MASK) !=
2157 GRXSTSRD_STP_DATA &&
2158 (temp & GRXSTSRD_PKTSTS_MASK) !=
2159 GRXSTSRD_STP_COMPLETE) {
2161 /* dump data - wrong direction */
2162 dwc_otg_common_rx_ack(sc);
2165 * The current transfer was cancelled
2169 return (0); /* complete */
2173 /* fill in more TX data, if possible */
2174 if (td->tx_bytes != 0) {
2178 /* check if packets have been transferred */
2179 temp = DWC_OTG_READ_4(sc, DOTG_DIEPTSIZ(td->ep_no));
2181 /* get current packet number */
2182 cpkt = DXEPTSIZ_GET_NPKT(temp);
2184 if (cpkt >= td->npkt) {
2187 if (max_buffer != 0) {
2188 fifo_left = (td->npkt - cpkt) *
2189 td->max_packet_size;
2191 if (fifo_left > max_buffer)
2192 fifo_left = max_buffer;
2194 fifo_left = td->max_packet_size;
2198 count = td->tx_bytes;
2199 if (count > fifo_left)
2203 /* write data into FIFO */
2204 dwc_otg_write_fifo(sc, td->pc, td->offset,
2205 DOTG_DFIFO(td->ep_no), count);
2207 td->tx_bytes -= count;
2208 td->remainder -= count;
2209 td->offset += count;
2212 if (td->tx_bytes != 0)
2215 /* check remainder */
2216 if (td->remainder == 0) {
2218 return (0); /* complete */
2220 /* else we need to transmit a short packet */
2227 /* check if not all packets have been transferred */
2228 temp = DWC_OTG_READ_4(sc, DOTG_DIEPTSIZ(td->ep_no));
2230 if (DXEPTSIZ_GET_NPKT(temp) != 0) {
2232 DPRINTFN(5, "busy ep=%d npkt=%d DIEPTSIZ=0x%08x "
2233 "DIEPCTL=0x%08x\n", td->ep_no,
2234 DXEPTSIZ_GET_NPKT(temp),
2235 temp, DWC_OTG_READ_4(sc, DOTG_DIEPCTL(td->ep_no)));
2240 DPRINTFN(5, "rem=%u ep=%d\n", td->remainder, td->ep_no);
2242 /* try to optimise by sending more data */
2243 if ((max_buffer != 0) && ((td->max_packet_size & 3) == 0)) {
2245 /* send multiple packets at the same time */
2246 mpkt = max_buffer / td->max_packet_size;
2251 count = td->remainder;
2252 if (count > 0x7FFFFF)
2253 count = 0x7FFFFF - (0x7FFFFF % td->max_packet_size);
2255 td->npkt = count / td->max_packet_size;
2258 * NOTE: We could use 0x3FE instead of "mpkt" in the
2259 * check below to get more throughput, but then we
2260 * have a dependency towards non-generic chip features
2261 * to disable the TX-FIFO-EMPTY interrupts on a per
2262 * endpoint basis. Increase the maximum buffer size of
2263 * the IN endpoint to increase the performance.
2265 if (td->npkt > mpkt) {
2267 count = td->max_packet_size * mpkt;
2268 } else if ((count == 0) || (count % td->max_packet_size)) {
2269 /* we are transmitting a short packet */
2274 /* send one packet at a time */
2276 count = td->max_packet_size;
2277 if (td->remainder < count) {
2278 /* we have a short packet */
2280 count = td->remainder;
2284 DWC_OTG_WRITE_4(sc, DOTG_DIEPTSIZ(td->ep_no),
2285 DXEPTSIZ_SET_MULTI(1) |
2286 DXEPTSIZ_SET_NPKT(td->npkt) |
2287 DXEPTSIZ_SET_NBYTES(count));
2289 /* make room for buffering */
2292 temp = sc->sc_in_ctl[td->ep_no];
2294 /* check for isochronous mode */
2295 if ((temp & DIEPCTL_EPTYPE_MASK) ==
2296 (DIEPCTL_EPTYPE_ISOC << DIEPCTL_EPTYPE_SHIFT)) {
2297 /* toggle odd or even frame bit */
2298 if (temp & DIEPCTL_SETD1PID) {
2299 temp &= ~DIEPCTL_SETD1PID;
2300 temp |= DIEPCTL_SETD0PID;
2302 temp &= ~DIEPCTL_SETD0PID;
2303 temp |= DIEPCTL_SETD1PID;
2305 sc->sc_in_ctl[td->ep_no] = temp;
2308 /* must enable before writing data to FIFO */
2309 DWC_OTG_WRITE_4(sc, DOTG_DIEPCTL(td->ep_no), temp |
2310 DIEPCTL_EPENA | DIEPCTL_CNAK);
2312 td->tx_bytes = count;
2314 /* check remainder */
2315 if (td->tx_bytes == 0 &&
2316 td->remainder == 0) {
2318 return (0); /* complete */
2320 /* else we need to transmit a short packet */
2325 return (1); /* not complete */
2329 dwc_otg_data_tx_sync(struct dwc_otg_softc *sc, struct dwc_otg_td *td)
2334 * If all packets are transferred we are complete:
2336 temp = DWC_OTG_READ_4(sc, DOTG_DIEPTSIZ(td->ep_no));
2338 /* check that all packets have been transferred */
2339 if (DXEPTSIZ_GET_NPKT(temp) != 0) {
2340 DPRINTFN(5, "busy ep=%d\n", td->ep_no);
2347 /* we only want to know if there is a SETUP packet or free IN packet */
2349 temp = sc->sc_last_rx_status;
2351 if ((td->ep_no == 0) && (temp != 0) &&
2352 (GRXSTSRD_CHNUM_GET(temp) == 0)) {
2354 if ((temp & GRXSTSRD_PKTSTS_MASK) ==
2355 GRXSTSRD_STP_DATA ||
2356 (temp & GRXSTSRD_PKTSTS_MASK) ==
2357 GRXSTSRD_STP_COMPLETE) {
2358 DPRINTFN(5, "faking complete\n");
2360 * Race condition: We are complete!
2364 /* dump data - wrong direction */
2365 dwc_otg_common_rx_ack(sc);
2368 return (1); /* not complete */
2372 dwc_otg_xfer_do_fifo(struct dwc_otg_softc *sc, struct usb_xfer *xfer)
2374 struct dwc_otg_td *td;
2381 td = xfer->td_transfer_cache;
2386 if ((td->func) (sc, td)) {
2387 /* operation in progress */
2390 if (((void *)td) == xfer->td_transfer_last) {
2393 if (td->error_any) {
2395 } else if (td->remainder > 0) {
2397 * We had a short transfer. If there is no alternate
2398 * next, stop processing !
2405 * Fetch the next transfer descriptor and transfer
2406 * some flags to the next transfer descriptor
2408 tmr_res = td->tmr_res;
2409 tmr_val = td->tmr_val;
2410 toggle = td->toggle;
2412 xfer->td_transfer_cache = td;
2413 td->toggle = toggle; /* transfer toggle */
2414 td->tmr_res = tmr_res;
2415 td->tmr_val = tmr_val;
2420 xfer->td_transfer_cache = NULL;
2421 sc->sc_xfer_complete = 1;
2425 dwc_otg_xfer_do_complete_locked(struct dwc_otg_softc *sc, struct usb_xfer *xfer)
2427 struct dwc_otg_td *td;
2431 td = xfer->td_transfer_cache;
2433 /* compute all actual lengths */
2434 dwc_otg_standard_done(xfer);
2441 dwc_otg_timer(void *_sc)
2443 struct dwc_otg_softc *sc = _sc;
2445 USB_BUS_LOCK_ASSERT(&sc->sc_bus, MA_OWNED);
2449 USB_BUS_SPIN_LOCK(&sc->sc_bus);
2451 /* increment timer value */
2454 /* enable SOF interrupt, which will poll jobs */
2455 dwc_otg_enable_sof_irq(sc);
2457 USB_BUS_SPIN_UNLOCK(&sc->sc_bus);
2459 if (sc->sc_timer_active) {
2461 usb_callout_reset(&sc->sc_timer,
2462 hz / (1000 / DWC_OTG_HOST_TIMER_RATE),
2463 &dwc_otg_timer, sc);
2468 dwc_otg_timer_start(struct dwc_otg_softc *sc)
2470 if (sc->sc_timer_active != 0)
2473 sc->sc_timer_active = 1;
2476 usb_callout_reset(&sc->sc_timer,
2477 hz / (1000 / DWC_OTG_HOST_TIMER_RATE),
2478 &dwc_otg_timer, sc);
2482 dwc_otg_timer_stop(struct dwc_otg_softc *sc)
2484 if (sc->sc_timer_active == 0)
2487 sc->sc_timer_active = 0;
2490 usb_callout_stop(&sc->sc_timer);
2494 dwc_otg_compute_isoc_rx_tt_slot(struct dwc_otg_tt_info *pinfo)
2496 if (pinfo->slot_index < DWC_OTG_TT_SLOT_MAX)
2497 pinfo->slot_index++;
2498 return (pinfo->slot_index);
2502 dwc_otg_update_host_transfer_schedule_locked(struct dwc_otg_softc *sc)
2504 TAILQ_HEAD(, usb_xfer) head;
2505 struct usb_xfer *xfer;
2506 struct usb_xfer *xfer_next;
2507 struct dwc_otg_td *td;
2511 temp = DWC_OTG_READ_4(sc, DOTG_HFNUM) & DWC_OTG_FRAME_MASK;
2513 if (sc->sc_last_frame_num == temp)
2516 sc->sc_last_frame_num = temp;
2520 if ((temp & 7) == 0) {
2522 /* reset the schedule */
2523 memset(sc->sc_tt_info, 0, sizeof(sc->sc_tt_info));
2525 TAILQ_FOREACH_SAFE(xfer, &sc->sc_bus.intr_q.head, wait_entry, xfer_next) {
2526 td = xfer->td_transfer_cache;
2527 if (td == NULL || td->ep_type != UE_ISOCHRONOUS)
2530 /* check for IN direction */
2531 if ((td->hcchar & HCCHAR_EPDIR_IN) != 0)
2536 if (td->hcsplt == 0 || td->tt_scheduled != 0)
2540 slot = dwc_otg_compute_isoc_rx_tt_slot(
2541 sc->sc_tt_info + td->tt_index);
2544 * Not enough time to get complete
2550 td->tt_start_slot = temp + slot;
2551 td->tt_scheduled = 1;
2552 TAILQ_REMOVE(&sc->sc_bus.intr_q.head, xfer, wait_entry);
2553 TAILQ_INSERT_TAIL(&head, xfer, wait_entry);
2556 TAILQ_FOREACH_SAFE(xfer, &sc->sc_bus.intr_q.head, wait_entry, xfer_next) {
2557 td = xfer->td_transfer_cache;
2558 if (td == NULL || td->ep_type != UE_ISOCHRONOUS)
2561 /* check for OUT direction */
2562 if ((td->hcchar & HCCHAR_EPDIR_IN) == 0)
2567 if (td->hcsplt == 0 || td->tt_scheduled != 0)
2571 td->tt_start_slot = temp;
2572 td->tt_scheduled = 1;
2573 TAILQ_REMOVE(&sc->sc_bus.intr_q.head, xfer, wait_entry);
2574 TAILQ_INSERT_TAIL(&head, xfer, wait_entry);
2577 TAILQ_FOREACH_SAFE(xfer, &sc->sc_bus.intr_q.head, wait_entry, xfer_next) {
2578 td = xfer->td_transfer_cache;
2579 if (td == NULL || td->ep_type != UE_INTERRUPT)
2582 if (td->tt_scheduled != 0) {
2587 if (dwc_otg_host_rate_check_interrupt(sc, td))
2590 if (td->hcsplt == 0) {
2592 td->tt_scheduled = 1;
2597 td->tt_start_slot = temp;
2599 td->tt_scheduled = 1;
2600 TAILQ_REMOVE(&sc->sc_bus.intr_q.head, xfer, wait_entry);
2601 TAILQ_INSERT_TAIL(&head, xfer, wait_entry);
2604 TAILQ_FOREACH_SAFE(xfer, &sc->sc_bus.intr_q.head, wait_entry, xfer_next) {
2605 td = xfer->td_transfer_cache;
2607 td->ep_type != UE_CONTROL) {
2613 if (td->hcsplt == 0 || td->tt_scheduled != 0)
2617 td->tt_start_slot = temp;
2618 td->tt_scheduled = 1;
2619 TAILQ_REMOVE(&sc->sc_bus.intr_q.head, xfer, wait_entry);
2620 TAILQ_INSERT_TAIL(&head, xfer, wait_entry);
2623 if ((temp & 7) < 6) {
2624 TAILQ_FOREACH_SAFE(xfer, &sc->sc_bus.intr_q.head, wait_entry, xfer_next) {
2625 td = xfer->td_transfer_cache;
2627 td->ep_type != UE_BULK) {
2633 if (td->hcsplt == 0 || td->tt_scheduled != 0)
2637 td->tt_start_slot = temp;
2638 td->tt_scheduled = 1;
2639 TAILQ_REMOVE(&sc->sc_bus.intr_q.head, xfer, wait_entry);
2640 TAILQ_INSERT_TAIL(&head, xfer, wait_entry);
2644 /* Put TT transfers in execution order at the end */
2645 TAILQ_CONCAT(&sc->sc_bus.intr_q.head, &head, wait_entry);
2647 /* move all TT transfers in front, keeping the current order */
2648 TAILQ_FOREACH_SAFE(xfer, &sc->sc_bus.intr_q.head, wait_entry, xfer_next) {
2649 td = xfer->td_transfer_cache;
2650 if (td == NULL || td->hcsplt == 0)
2652 TAILQ_REMOVE(&sc->sc_bus.intr_q.head, xfer, wait_entry);
2653 TAILQ_INSERT_TAIL(&head, xfer, wait_entry);
2655 TAILQ_CONCAT(&head, &sc->sc_bus.intr_q.head, wait_entry);
2656 TAILQ_CONCAT(&sc->sc_bus.intr_q.head, &head, wait_entry);
2658 /* put non-TT non-ISOCHRONOUS transfers last */
2659 TAILQ_FOREACH_SAFE(xfer, &sc->sc_bus.intr_q.head, wait_entry, xfer_next) {
2660 td = xfer->td_transfer_cache;
2661 if (td == NULL || td->hcsplt != 0 || td->ep_type == UE_ISOCHRONOUS)
2663 TAILQ_REMOVE(&sc->sc_bus.intr_q.head, xfer, wait_entry);
2664 TAILQ_INSERT_TAIL(&head, xfer, wait_entry);
2666 TAILQ_CONCAT(&sc->sc_bus.intr_q.head, &head, wait_entry);
2668 if ((temp & 7) == 0) {
2670 DPRINTFN(12, "SOF interrupt #%d, needsof=%d\n",
2671 (int)temp, (int)sc->sc_needsof);
2673 /* update SOF IRQ mask */
2674 if (sc->sc_irq_mask & GINTMSK_SOFMSK) {
2675 if (sc->sc_needsof == 0) {
2676 sc->sc_irq_mask &= ~GINTMSK_SOFMSK;
2677 DWC_OTG_WRITE_4(sc, DOTG_GINTMSK, sc->sc_irq_mask);
2680 if (sc->sc_needsof != 0) {
2681 sc->sc_irq_mask |= GINTMSK_SOFMSK;
2682 DWC_OTG_WRITE_4(sc, DOTG_GINTMSK, sc->sc_irq_mask);
2686 /* clear need SOF flag */
2693 dwc_otg_interrupt_poll_locked(struct dwc_otg_softc *sc)
2695 struct usb_xfer *xfer;
2699 uint8_t got_rx_status;
2702 if (sc->sc_flags.status_device_mode == 0) {
2704 * Update host transfer schedule, so that new
2705 * transfers can be issued:
2707 dwc_otg_update_host_transfer_schedule_locked(sc);
2711 if (++count == 16) {
2712 /* give other interrupts a chance */
2717 /* get all host channel interrupts */
2718 haint = DWC_OTG_READ_4(sc, DOTG_HAINT);
2721 if (x >= sc->sc_host_ch_max)
2723 temp = DWC_OTG_READ_4(sc, DOTG_HCINT(x));
2724 DWC_OTG_WRITE_4(sc, DOTG_HCINT(x), temp);
2725 temp &= ~HCINT_SOFTWARE_ONLY;
2726 sc->sc_chan_state[x].hcint |= temp;
2727 haint &= ~(1U << x);
2730 if (sc->sc_last_rx_status == 0) {
2732 temp = DWC_OTG_READ_4(sc, DOTG_GINTSTS);
2733 if (temp & GINTSTS_RXFLVL) {
2734 /* pop current status */
2735 sc->sc_last_rx_status =
2736 DWC_OTG_READ_4(sc, DOTG_GRXSTSPD);
2739 if (sc->sc_last_rx_status != 0) {
2743 temp = sc->sc_last_rx_status &
2744 GRXSTSRD_PKTSTS_MASK;
2746 /* non-data messages we simply skip */
2747 if (temp != GRXSTSRD_STP_DATA &&
2748 temp != GRXSTSRD_STP_COMPLETE &&
2749 temp != GRXSTSRD_OUT_DATA) {
2750 /* check for halted channel */
2751 if (temp == GRXSTSRH_HALTED) {
2752 ep_no = GRXSTSRD_CHNUM_GET(sc->sc_last_rx_status);
2753 sc->sc_chan_state[ep_no].wait_halted = 0;
2754 DPRINTFN(5, "channel halt complete ch=%u\n", ep_no);
2756 /* store bytes and FIFO offset */
2757 sc->sc_current_rx_bytes = 0;
2758 sc->sc_current_rx_fifo = 0;
2760 /* acknowledge status */
2761 dwc_otg_common_rx_ack(sc);
2765 temp = GRXSTSRD_BCNT_GET(
2766 sc->sc_last_rx_status);
2767 ep_no = GRXSTSRD_CHNUM_GET(
2768 sc->sc_last_rx_status);
2770 /* store bytes and FIFO offset */
2771 sc->sc_current_rx_bytes = (temp + 3) & ~3;
2772 sc->sc_current_rx_fifo = DOTG_DFIFO(ep_no);
2774 DPRINTF("Reading %d bytes from ep %d\n", temp, ep_no);
2776 /* check if we should dump the data */
2777 if (!(sc->sc_active_rx_ep & (1U << ep_no))) {
2778 dwc_otg_common_rx_ack(sc);
2784 DPRINTFN(5, "RX status = 0x%08x: ch=%d pid=%d bytes=%d sts=%d\n",
2785 sc->sc_last_rx_status, ep_no,
2786 (sc->sc_last_rx_status >> 15) & 3,
2787 GRXSTSRD_BCNT_GET(sc->sc_last_rx_status),
2788 (sc->sc_last_rx_status >> 17) & 15);
2795 ep_no = GRXSTSRD_CHNUM_GET(
2796 sc->sc_last_rx_status);
2798 /* check if we should dump the data */
2799 if (!(sc->sc_active_rx_ep & (1U << ep_no))) {
2800 dwc_otg_common_rx_ack(sc);
2808 TAILQ_FOREACH(xfer, &sc->sc_bus.intr_q.head, wait_entry)
2809 dwc_otg_xfer_do_fifo(sc, xfer);
2811 if (got_rx_status) {
2812 /* check if data was consumed */
2813 if (sc->sc_last_rx_status == 0)
2816 /* disable RX FIFO level interrupt */
2817 sc->sc_irq_mask &= ~GINTMSK_RXFLVLMSK;
2818 DWC_OTG_WRITE_4(sc, DOTG_GINTMSK, sc->sc_irq_mask);
2823 dwc_otg_interrupt_complete_locked(struct dwc_otg_softc *sc)
2825 struct usb_xfer *xfer;
2827 /* scan for completion events */
2828 TAILQ_FOREACH(xfer, &sc->sc_bus.intr_q.head, wait_entry) {
2829 if (dwc_otg_xfer_do_complete_locked(sc, xfer))
2835 dwc_otg_vbus_interrupt(struct dwc_otg_softc *sc, uint8_t is_on)
2837 DPRINTFN(5, "vbus = %u\n", is_on);
2840 * If the USB host mode is forced, then assume VBUS is always
2841 * present else rely on the input to this function:
2843 if ((is_on != 0) || (sc->sc_mode == DWC_MODE_HOST)) {
2845 if (!sc->sc_flags.status_vbus) {
2846 sc->sc_flags.status_vbus = 1;
2848 /* complete root HUB interrupt endpoint */
2850 dwc_otg_root_intr(sc);
2853 if (sc->sc_flags.status_vbus) {
2854 sc->sc_flags.status_vbus = 0;
2855 sc->sc_flags.status_bus_reset = 0;
2856 sc->sc_flags.status_suspend = 0;
2857 sc->sc_flags.change_suspend = 0;
2858 sc->sc_flags.change_connect = 1;
2860 /* complete root HUB interrupt endpoint */
2862 dwc_otg_root_intr(sc);
2868 dwc_otg_filter_interrupt(void *arg)
2870 struct dwc_otg_softc *sc = arg;
2871 int retval = FILTER_HANDLED;
2874 USB_BUS_SPIN_LOCK(&sc->sc_bus);
2876 /* read and clear interrupt status */
2877 status = DWC_OTG_READ_4(sc, DOTG_GINTSTS);
2879 /* clear interrupts we are handling here */
2880 DWC_OTG_WRITE_4(sc, DOTG_GINTSTS, status & ~DWC_OTG_MSK_GINT_THREAD_IRQ);
2882 /* check for USB state change interrupts */
2883 if ((status & DWC_OTG_MSK_GINT_THREAD_IRQ) != 0)
2884 retval = FILTER_SCHEDULE_THREAD;
2886 /* clear FIFO empty interrupts */
2887 if (status & sc->sc_irq_mask &
2888 (GINTSTS_PTXFEMP | GINTSTS_NPTXFEMP)) {
2889 sc->sc_irq_mask &= ~(GINTSTS_PTXFEMP | GINTSTS_NPTXFEMP);
2890 DWC_OTG_WRITE_4(sc, DOTG_GINTMSK, sc->sc_irq_mask);
2892 /* clear all IN endpoint interrupts */
2893 if (status & GINTSTS_IEPINT) {
2897 for (x = 0; x != sc->sc_dev_in_ep_max; x++) {
2898 temp = DWC_OTG_READ_4(sc, DOTG_DIEPINT(x));
2900 * NOTE: Need to clear all interrupt bits,
2901 * because some appears to be unmaskable and
2902 * can cause an interrupt loop:
2905 DWC_OTG_WRITE_4(sc, DOTG_DIEPINT(x), temp);
2909 /* poll FIFOs, if any */
2910 dwc_otg_interrupt_poll_locked(sc);
2912 if (sc->sc_xfer_complete != 0)
2913 retval = FILTER_SCHEDULE_THREAD;
2915 USB_BUS_SPIN_UNLOCK(&sc->sc_bus);
2921 dwc_otg_interrupt(void *arg)
2923 struct dwc_otg_softc *sc = arg;
2926 USB_BUS_LOCK(&sc->sc_bus);
2927 USB_BUS_SPIN_LOCK(&sc->sc_bus);
2929 /* read and clear interrupt status */
2930 status = DWC_OTG_READ_4(sc, DOTG_GINTSTS);
2932 /* clear interrupts we are handling here */
2933 DWC_OTG_WRITE_4(sc, DOTG_GINTSTS, status & DWC_OTG_MSK_GINT_THREAD_IRQ);
2935 DPRINTFN(14, "GINTSTS=0x%08x HAINT=0x%08x HFNUM=0x%08x\n",
2936 status, DWC_OTG_READ_4(sc, DOTG_HAINT),
2937 DWC_OTG_READ_4(sc, DOTG_HFNUM));
2939 if (status & GINTSTS_USBRST) {
2941 /* set correct state */
2942 sc->sc_flags.status_device_mode = 1;
2943 sc->sc_flags.status_bus_reset = 0;
2944 sc->sc_flags.status_suspend = 0;
2945 sc->sc_flags.change_suspend = 0;
2946 sc->sc_flags.change_connect = 1;
2948 /* Disable SOF interrupt */
2949 sc->sc_irq_mask &= ~GINTMSK_SOFMSK;
2950 DWC_OTG_WRITE_4(sc, DOTG_GINTMSK, sc->sc_irq_mask);
2952 /* complete root HUB interrupt endpoint */
2953 dwc_otg_root_intr(sc);
2956 /* check for any bus state change interrupts */
2957 if (status & GINTSTS_ENUMDONE) {
2961 DPRINTFN(5, "end of reset\n");
2963 /* set correct state */
2964 sc->sc_flags.status_device_mode = 1;
2965 sc->sc_flags.status_bus_reset = 1;
2966 sc->sc_flags.status_suspend = 0;
2967 sc->sc_flags.change_suspend = 0;
2968 sc->sc_flags.change_connect = 1;
2969 sc->sc_flags.status_low_speed = 0;
2970 sc->sc_flags.port_enabled = 1;
2973 (void) dwc_otg_init_fifo(sc, DWC_MODE_DEVICE);
2975 /* reset function address */
2976 dwc_otg_set_address(sc, 0);
2978 /* figure out enumeration speed */
2979 temp = DWC_OTG_READ_4(sc, DOTG_DSTS);
2980 if (DSTS_ENUMSPD_GET(temp) == DSTS_ENUMSPD_HI)
2981 sc->sc_flags.status_high_speed = 1;
2983 sc->sc_flags.status_high_speed = 0;
2986 * Disable resume and SOF interrupt, and enable
2987 * suspend and RX frame interrupt:
2989 sc->sc_irq_mask &= ~(GINTMSK_WKUPINTMSK | GINTMSK_SOFMSK);
2990 sc->sc_irq_mask |= GINTMSK_USBSUSPMSK;
2991 DWC_OTG_WRITE_4(sc, DOTG_GINTMSK, sc->sc_irq_mask);
2993 /* complete root HUB interrupt endpoint */
2994 dwc_otg_root_intr(sc);
2997 if (status & GINTSTS_PRTINT) {
3000 hprt = DWC_OTG_READ_4(sc, DOTG_HPRT);
3002 /* clear change bits */
3003 DWC_OTG_WRITE_4(sc, DOTG_HPRT, (hprt & (
3004 HPRT_PRTPWR | HPRT_PRTENCHNG |
3005 HPRT_PRTCONNDET | HPRT_PRTOVRCURRCHNG)) |
3008 DPRINTFN(12, "GINTSTS=0x%08x, HPRT=0x%08x\n", status, hprt);
3010 sc->sc_flags.status_device_mode = 0;
3012 if (hprt & HPRT_PRTCONNSTS)
3013 sc->sc_flags.status_bus_reset = 1;
3015 sc->sc_flags.status_bus_reset = 0;
3017 if ((hprt & HPRT_PRTENCHNG) &&
3018 (hprt & HPRT_PRTENA) == 0)
3019 sc->sc_flags.change_enabled = 1;
3021 if (hprt & HPRT_PRTENA)
3022 sc->sc_flags.port_enabled = 1;
3024 sc->sc_flags.port_enabled = 0;
3026 if (hprt & HPRT_PRTOVRCURRCHNG)
3027 sc->sc_flags.change_over_current = 1;
3029 if (hprt & HPRT_PRTOVRCURRACT)
3030 sc->sc_flags.port_over_current = 1;
3032 sc->sc_flags.port_over_current = 0;
3034 if (hprt & HPRT_PRTPWR)
3035 sc->sc_flags.port_powered = 1;
3037 sc->sc_flags.port_powered = 0;
3039 if (((hprt & HPRT_PRTSPD_MASK)
3040 >> HPRT_PRTSPD_SHIFT) == HPRT_PRTSPD_LOW)
3041 sc->sc_flags.status_low_speed = 1;
3043 sc->sc_flags.status_low_speed = 0;
3045 if (((hprt & HPRT_PRTSPD_MASK)
3046 >> HPRT_PRTSPD_SHIFT) == HPRT_PRTSPD_HIGH)
3047 sc->sc_flags.status_high_speed = 1;
3049 sc->sc_flags.status_high_speed = 0;
3051 if (hprt & HPRT_PRTCONNDET)
3052 sc->sc_flags.change_connect = 1;
3054 if (hprt & HPRT_PRTSUSP)
3055 dwc_otg_suspend_irq(sc);
3057 dwc_otg_resume_irq(sc);
3059 /* complete root HUB interrupt endpoint */
3060 dwc_otg_root_intr(sc);
3062 /* update host frame interval */
3063 dwc_otg_update_host_frame_interval(sc);
3067 * If resume and suspend is set at the same time we interpret
3068 * that like RESUME. Resume is set when there is at least 3
3069 * milliseconds of inactivity on the USB BUS.
3071 if (status & GINTSTS_WKUPINT) {
3073 DPRINTFN(5, "resume interrupt\n");
3075 dwc_otg_resume_irq(sc);
3077 } else if (status & GINTSTS_USBSUSP) {
3079 DPRINTFN(5, "suspend interrupt\n");
3081 dwc_otg_suspend_irq(sc);
3084 if (status & (GINTSTS_USBSUSP |
3087 GINTSTS_SESSREQINT)) {
3090 temp = DWC_OTG_READ_4(sc, DOTG_GOTGCTL);
3092 DPRINTFN(5, "GOTGCTL=0x%08x\n", temp);
3094 dwc_otg_vbus_interrupt(sc,
3095 (temp & (GOTGCTL_ASESVLD | GOTGCTL_BSESVLD)) ? 1 : 0);
3098 if (sc->sc_xfer_complete != 0) {
3099 sc->sc_xfer_complete = 0;
3101 /* complete FIFOs, if any */
3102 dwc_otg_interrupt_complete_locked(sc);
3104 USB_BUS_SPIN_UNLOCK(&sc->sc_bus);
3105 USB_BUS_UNLOCK(&sc->sc_bus);
3109 dwc_otg_setup_standard_chain_sub(struct dwc_otg_std_temp *temp)
3111 struct dwc_otg_td *td;
3113 /* get current Transfer Descriptor */
3117 /* prepare for next TD */
3118 temp->td_next = td->obj_next;
3120 /* fill out the Transfer Descriptor */
3121 td->func = temp->func;
3123 td->offset = temp->offset;
3124 td->remainder = temp->len;
3127 td->error_stall = 0;
3129 td->did_stall = temp->did_stall;
3130 td->short_pkt = temp->short_pkt;
3131 td->alt_next = temp->setup_alt_next;
3135 td->channel[0] = DWC_OTG_MAX_CHANNELS;
3136 td->channel[1] = DWC_OTG_MAX_CHANNELS;
3137 td->channel[2] = DWC_OTG_MAX_CHANNELS;
3140 td->tt_scheduled = 0;
3141 td->tt_xactpos = HCSPLT_XACTPOS_BEGIN;
3145 dwc_otg_setup_standard_chain(struct usb_xfer *xfer)
3147 struct dwc_otg_std_temp temp;
3148 struct dwc_otg_td *td;
3153 DPRINTFN(9, "addr=%d endpt=%d sumlen=%d speed=%d\n",
3154 xfer->address, UE_GET_ADDR(xfer->endpointno),
3155 xfer->sumlen, usbd_get_speed(xfer->xroot->udev));
3157 temp.max_frame_size = xfer->max_frame_size;
3159 td = xfer->td_start[0];
3160 xfer->td_transfer_first = td;
3161 xfer->td_transfer_cache = td;
3167 temp.td_next = xfer->td_start[0];
3169 temp.setup_alt_next = xfer->flags_int.short_frames_ok ||
3170 xfer->flags_int.isochronous_xfr;
3171 temp.did_stall = !xfer->flags_int.control_stall;
3173 is_host = (xfer->xroot->udev->flags.usb_mode == USB_MODE_HOST);
3175 /* check if we should prepend a setup message */
3177 if (xfer->flags_int.control_xfr) {
3178 if (xfer->flags_int.control_hdr) {
3181 temp.func = &dwc_otg_host_setup_tx;
3183 temp.func = &dwc_otg_setup_rx;
3185 temp.len = xfer->frlengths[0];
3186 temp.pc = xfer->frbuffers + 0;
3187 temp.short_pkt = temp.len ? 1 : 0;
3189 /* check for last frame */
3190 if (xfer->nframes == 1) {
3191 /* no STATUS stage yet, SETUP is last */
3192 if (xfer->flags_int.control_act)
3193 temp.setup_alt_next = 0;
3196 dwc_otg_setup_standard_chain_sub(&temp);
3203 if (x != xfer->nframes) {
3204 if (xfer->endpointno & UE_DIR_IN) {
3206 temp.func = &dwc_otg_host_data_rx;
3209 temp.func = &dwc_otg_data_tx;
3214 temp.func = &dwc_otg_host_data_tx;
3217 temp.func = &dwc_otg_data_rx;
3222 /* setup "pc" pointer */
3223 temp.pc = xfer->frbuffers + x;
3227 while (x != xfer->nframes) {
3229 /* DATA0 / DATA1 message */
3231 temp.len = xfer->frlengths[x];
3235 if (x == xfer->nframes) {
3236 if (xfer->flags_int.control_xfr) {
3237 if (xfer->flags_int.control_act) {
3238 temp.setup_alt_next = 0;
3241 temp.setup_alt_next = 0;
3244 if (temp.len == 0) {
3246 /* make sure that we send an USB packet */
3252 /* regular data transfer */
3254 temp.short_pkt = (xfer->flags.force_short_xfer ? 0 : 1);
3257 dwc_otg_setup_standard_chain_sub(&temp);
3259 if (xfer->flags_int.isochronous_xfr) {
3260 temp.offset += temp.len;
3262 /* get next Page Cache pointer */
3263 temp.pc = xfer->frbuffers + x;
3267 if (xfer->flags_int.control_xfr) {
3269 /* always setup a valid "pc" pointer for status and sync */
3270 temp.pc = xfer->frbuffers + 0;
3273 temp.setup_alt_next = 0;
3275 /* check if we need to sync */
3277 /* we need a SYNC point after TX */
3278 temp.func = &dwc_otg_data_tx_sync;
3279 dwc_otg_setup_standard_chain_sub(&temp);
3282 /* check if we should append a status stage */
3283 if (!xfer->flags_int.control_act) {
3286 * Send a DATA1 message and invert the current
3287 * endpoint direction.
3289 if (xfer->endpointno & UE_DIR_IN) {
3291 temp.func = &dwc_otg_host_data_tx;
3294 temp.func = &dwc_otg_data_rx;
3299 temp.func = &dwc_otg_host_data_rx;
3302 temp.func = &dwc_otg_data_tx;
3307 dwc_otg_setup_standard_chain_sub(&temp);
3309 /* data toggle should be DATA1 */
3314 /* we need a SYNC point after TX */
3315 temp.func = &dwc_otg_data_tx_sync;
3316 dwc_otg_setup_standard_chain_sub(&temp);
3320 /* check if we need to sync */
3323 temp.pc = xfer->frbuffers + 0;
3326 temp.setup_alt_next = 0;
3328 /* we need a SYNC point after TX */
3329 temp.func = &dwc_otg_data_tx_sync;
3330 dwc_otg_setup_standard_chain_sub(&temp);
3334 /* must have at least one frame! */
3336 xfer->td_transfer_last = td;
3340 struct dwc_otg_softc *sc;
3344 sc = DWC_OTG_BUS2SC(xfer->xroot->bus);
3346 /* get first again */
3347 td = xfer->td_transfer_first;
3348 td->toggle = (xfer->endpoint->toggle_next ? 1 : 0);
3351 (xfer->address << HCCHAR_DEVADDR_SHIFT) |
3352 ((xfer->endpointno & UE_ADDR) << HCCHAR_EPNUM_SHIFT) |
3353 (xfer->max_packet_size << HCCHAR_MPS_SHIFT) |
3357 * We are not always able to meet the timing
3358 * requirements of the USB interrupt endpoint's
3359 * complete split token, when doing transfers going
3360 * via a transaction translator. Use the CONTROL
3361 * transfer type instead of the INTERRUPT transfer
3362 * type in general, as a means to workaround
3363 * that. This trick should work for both FULL and LOW
3364 * speed USB traffic going through a TT. For non-TT
3365 * traffic it works as well. The reason for using
3366 * CONTROL type instead of BULK is that some TTs might
3367 * reject LOW speed BULK traffic.
3369 if (td->ep_type == UE_INTERRUPT)
3370 hcchar |= (UE_CONTROL << HCCHAR_EPTYPE_SHIFT);
3372 hcchar |= (td->ep_type << HCCHAR_EPTYPE_SHIFT);
3374 if (UE_GET_DIR(xfer->endpointno) == UE_DIR_IN)
3375 hcchar |= HCCHAR_EPDIR_IN;
3377 switch (xfer->xroot->udev->speed) {
3379 hcchar |= HCCHAR_LSPDDEV;
3381 case USB_SPEED_FULL:
3382 /* check if root HUB port is running High Speed */
3383 if (dwc_otg_uses_split(xfer->xroot->udev)) {
3384 hcsplt = HCSPLT_SPLTENA |
3385 (xfer->xroot->udev->hs_port_no <<
3386 HCSPLT_PRTADDR_SHIFT) |
3387 (xfer->xroot->udev->hs_hub_addr <<
3388 HCSPLT_HUBADDR_SHIFT);
3392 if (td->ep_type == UE_INTERRUPT) {
3394 ival = xfer->interval / DWC_OTG_HOST_TIMER_RATE;
3397 else if (ival > 127)
3399 td->tmr_val = sc->sc_tmr_val + ival;
3401 } else if (td->ep_type == UE_ISOCHRONOUS) {
3403 td->tmr_val = sc->sc_last_frame_num;
3404 if (td->hcchar & HCCHAR_EPDIR_IN)
3408 td->tmr_res = (uint8_t)sc->sc_last_frame_num;
3411 case USB_SPEED_HIGH:
3413 if (td->ep_type == UE_INTERRUPT) {
3415 hcchar |= ((xfer->max_packet_count & 3)
3416 << HCCHAR_MC_SHIFT);
3417 ival = xfer->interval / DWC_OTG_HOST_TIMER_RATE;
3420 else if (ival > 127)
3422 td->tmr_val = sc->sc_tmr_val + ival;
3424 } else if (td->ep_type == UE_ISOCHRONOUS) {
3425 hcchar |= ((xfer->max_packet_count & 3)
3426 << HCCHAR_MC_SHIFT);
3427 td->tmr_res = 1 << usbd_xfer_get_fps_shift(xfer);
3428 td->tmr_val = sc->sc_last_frame_num;
3429 if (td->hcchar & HCCHAR_EPDIR_IN)
3430 td->tmr_val += td->tmr_res;
3434 td->tmr_res = (uint8_t)sc->sc_last_frame_num;
3444 /* store configuration in all TD's */
3446 td->hcchar = hcchar;
3447 td->hcsplt = hcsplt;
3449 if (((void *)td) == xfer->td_transfer_last)
3458 dwc_otg_timeout(void *arg)
3460 struct usb_xfer *xfer = arg;
3462 DPRINTF("xfer=%p\n", xfer);
3464 USB_BUS_LOCK_ASSERT(xfer->xroot->bus, MA_OWNED);
3466 /* transfer is transferred */
3467 dwc_otg_device_done(xfer, USB_ERR_TIMEOUT);
3471 dwc_otg_start_standard_chain(struct usb_xfer *xfer)
3473 struct dwc_otg_softc *sc = DWC_OTG_BUS2SC(xfer->xroot->bus);
3478 * Poll one time in device mode, which will turn on the
3479 * endpoint interrupts. Else wait for SOF interrupt in host
3482 USB_BUS_SPIN_LOCK(&sc->sc_bus);
3484 if (sc->sc_flags.status_device_mode != 0) {
3485 dwc_otg_xfer_do_fifo(sc, xfer);
3486 if (dwc_otg_xfer_do_complete_locked(sc, xfer))
3489 struct dwc_otg_td *td = xfer->td_transfer_cache;
3490 if (td->ep_type == UE_ISOCHRONOUS &&
3491 (td->hcchar & HCCHAR_EPDIR_IN) == 0) {
3493 * Need to start ISOCHRONOUS OUT transfer ASAP
3494 * because execution is delayed by one 125us
3497 dwc_otg_xfer_do_fifo(sc, xfer);
3498 if (dwc_otg_xfer_do_complete_locked(sc, xfer))
3503 /* put transfer on interrupt queue */
3504 usbd_transfer_enqueue(&xfer->xroot->bus->intr_q, xfer);
3506 /* start timeout, if any */
3507 if (xfer->timeout != 0) {
3508 usbd_transfer_timeout_ms(xfer,
3509 &dwc_otg_timeout, xfer->timeout);
3512 if (sc->sc_flags.status_device_mode != 0)
3515 /* enable SOF interrupt, if any */
3516 dwc_otg_enable_sof_irq(sc);
3518 USB_BUS_SPIN_UNLOCK(&sc->sc_bus);
3522 dwc_otg_root_intr(struct dwc_otg_softc *sc)
3526 USB_BUS_LOCK_ASSERT(&sc->sc_bus, MA_OWNED);
3529 sc->sc_hub_idata[0] = 0x02; /* we only have one port */
3531 uhub_root_intr(&sc->sc_bus, sc->sc_hub_idata,
3532 sizeof(sc->sc_hub_idata));
3536 dwc_otg_standard_done_sub(struct usb_xfer *xfer)
3538 struct dwc_otg_td *td;
3544 td = xfer->td_transfer_cache;
3547 len = td->remainder;
3549 /* store last data toggle */
3550 xfer->endpoint->toggle_next = td->toggle;
3552 if (xfer->aframes != xfer->nframes) {
3554 * Verify the length and subtract
3555 * the remainder from "frlengths[]":
3557 if (len > xfer->frlengths[xfer->aframes]) {
3560 xfer->frlengths[xfer->aframes] -= len;
3563 /* Check for transfer error */
3564 if (td->error_any) {
3565 /* the transfer is finished */
3566 error = (td->error_stall ?
3567 USB_ERR_STALLED : USB_ERR_IOERROR);
3571 /* Check for short transfer */
3573 if (xfer->flags_int.short_frames_ok ||
3574 xfer->flags_int.isochronous_xfr) {
3575 /* follow alt next */
3582 /* the transfer is finished */
3590 /* this USB frame is complete */
3596 /* update transfer cache */
3598 xfer->td_transfer_cache = td;
3604 dwc_otg_standard_done(struct usb_xfer *xfer)
3606 usb_error_t err = 0;
3608 DPRINTFN(13, "xfer=%p endpoint=%p transfer done\n",
3609 xfer, xfer->endpoint);
3613 xfer->td_transfer_cache = xfer->td_transfer_first;
3615 if (xfer->flags_int.control_xfr) {
3617 if (xfer->flags_int.control_hdr) {
3619 err = dwc_otg_standard_done_sub(xfer);
3623 if (xfer->td_transfer_cache == NULL) {
3627 while (xfer->aframes != xfer->nframes) {
3629 err = dwc_otg_standard_done_sub(xfer);
3632 if (xfer->td_transfer_cache == NULL) {
3637 if (xfer->flags_int.control_xfr &&
3638 !xfer->flags_int.control_act) {
3640 err = dwc_otg_standard_done_sub(xfer);
3643 dwc_otg_device_done(xfer, err);
3646 /*------------------------------------------------------------------------*
3647 * dwc_otg_device_done
3649 * NOTE: this function can be called more than one time on the
3650 * same USB transfer!
3651 *------------------------------------------------------------------------*/
3653 dwc_otg_device_done(struct usb_xfer *xfer, usb_error_t error)
3655 struct dwc_otg_softc *sc = DWC_OTG_BUS2SC(xfer->xroot->bus);
3657 DPRINTFN(9, "xfer=%p, endpoint=%p, error=%d\n",
3658 xfer, xfer->endpoint, error);
3660 USB_BUS_SPIN_LOCK(&sc->sc_bus);
3662 if (xfer->flags_int.usb_mode == USB_MODE_DEVICE) {
3663 /* Interrupts are cleared by the interrupt handler */
3665 struct dwc_otg_td *td;
3667 td = xfer->td_transfer_cache;
3669 dwc_otg_host_channel_free(sc, td);
3671 /* dequeue transfer and start next transfer */
3672 usbd_transfer_done(xfer, error);
3674 USB_BUS_SPIN_UNLOCK(&sc->sc_bus);
3678 dwc_otg_xfer_stall(struct usb_xfer *xfer)
3680 dwc_otg_device_done(xfer, USB_ERR_STALLED);
3684 dwc_otg_set_stall(struct usb_device *udev,
3685 struct usb_endpoint *ep, uint8_t *did_stall)
3687 struct dwc_otg_softc *sc;
3692 USB_BUS_LOCK_ASSERT(udev->bus, MA_OWNED);
3695 if (udev->flags.usb_mode != USB_MODE_DEVICE) {
3700 sc = DWC_OTG_BUS2SC(udev->bus);
3702 USB_BUS_SPIN_LOCK(&sc->sc_bus);
3704 /* get endpoint address */
3705 ep_no = ep->edesc->bEndpointAddress;
3707 DPRINTFN(5, "endpoint=0x%x\n", ep_no);
3709 if (ep_no & UE_DIR_IN) {
3710 reg = DOTG_DIEPCTL(ep_no & UE_ADDR);
3711 temp = sc->sc_in_ctl[ep_no & UE_ADDR];
3713 reg = DOTG_DOEPCTL(ep_no & UE_ADDR);
3714 temp = sc->sc_out_ctl[ep_no & UE_ADDR];
3717 /* disable and stall endpoint */
3718 DWC_OTG_WRITE_4(sc, reg, temp | DOEPCTL_EPDIS);
3719 DWC_OTG_WRITE_4(sc, reg, temp | DOEPCTL_STALL);
3721 /* clear active OUT ep */
3722 if (!(ep_no & UE_DIR_IN)) {
3724 sc->sc_active_rx_ep &= ~(1U << (ep_no & UE_ADDR));
3726 if (sc->sc_last_rx_status != 0 &&
3727 (ep_no & UE_ADDR) == GRXSTSRD_CHNUM_GET(
3728 sc->sc_last_rx_status)) {
3730 dwc_otg_common_rx_ack(sc);
3731 /* poll interrupt */
3732 dwc_otg_interrupt_poll_locked(sc);
3733 dwc_otg_interrupt_complete_locked(sc);
3736 USB_BUS_SPIN_UNLOCK(&sc->sc_bus);
3740 dwc_otg_clear_stall_sub_locked(struct dwc_otg_softc *sc, uint32_t mps,
3741 uint8_t ep_no, uint8_t ep_type, uint8_t ep_dir)
3746 if (ep_type == UE_CONTROL) {
3747 /* clearing stall is not needed */
3752 reg = DOTG_DIEPCTL(ep_no);
3754 reg = DOTG_DOEPCTL(ep_no);
3755 sc->sc_active_rx_ep |= (1U << ep_no);
3758 /* round up and mask away the multiplier count */
3759 mps = (mps + 3) & 0x7FC;
3761 if (ep_type == UE_BULK) {
3762 temp = DIEPCTL_EPTYPE_SET(
3763 DIEPCTL_EPTYPE_BULK) |
3765 } else if (ep_type == UE_INTERRUPT) {
3766 temp = DIEPCTL_EPTYPE_SET(
3767 DIEPCTL_EPTYPE_INTERRUPT) |
3770 temp = DIEPCTL_EPTYPE_SET(
3771 DIEPCTL_EPTYPE_ISOC) |
3775 temp |= DIEPCTL_MPS_SET(mps);
3776 temp |= DIEPCTL_TXFNUM_SET(ep_no);
3779 sc->sc_in_ctl[ep_no] = temp;
3781 sc->sc_out_ctl[ep_no] = temp;
3783 DWC_OTG_WRITE_4(sc, reg, temp | DOEPCTL_EPDIS);
3784 DWC_OTG_WRITE_4(sc, reg, temp | DOEPCTL_SETD0PID);
3785 DWC_OTG_WRITE_4(sc, reg, temp | DIEPCTL_SNAK);
3787 /* we only reset the transmit FIFO */
3789 dwc_otg_tx_fifo_reset(sc,
3790 GRSTCTL_TXFIFO(ep_no) |
3794 DOTG_DIEPTSIZ(ep_no), 0);
3797 /* poll interrupt */
3798 dwc_otg_interrupt_poll_locked(sc);
3799 dwc_otg_interrupt_complete_locked(sc);
3803 dwc_otg_clear_stall(struct usb_device *udev, struct usb_endpoint *ep)
3805 struct dwc_otg_softc *sc;
3806 struct usb_endpoint_descriptor *ed;
3808 DPRINTFN(5, "endpoint=%p\n", ep);
3810 USB_BUS_LOCK_ASSERT(udev->bus, MA_OWNED);
3813 if (udev->flags.usb_mode != USB_MODE_DEVICE) {
3818 sc = DWC_OTG_BUS2SC(udev->bus);
3820 USB_BUS_SPIN_LOCK(&sc->sc_bus);
3822 /* get endpoint descriptor */
3825 /* reset endpoint */
3826 dwc_otg_clear_stall_sub_locked(sc,
3827 UGETW(ed->wMaxPacketSize),
3828 (ed->bEndpointAddress & UE_ADDR),
3829 (ed->bmAttributes & UE_XFERTYPE),
3830 (ed->bEndpointAddress & (UE_DIR_IN | UE_DIR_OUT)));
3832 USB_BUS_SPIN_UNLOCK(&sc->sc_bus);
3836 dwc_otg_device_state_change(struct usb_device *udev)
3838 struct dwc_otg_softc *sc;
3842 if (udev->flags.usb_mode != USB_MODE_DEVICE) {
3848 sc = DWC_OTG_BUS2SC(udev->bus);
3850 /* deactivate all other endpoint but the control endpoint */
3851 if (udev->state == USB_STATE_CONFIGURED ||
3852 udev->state == USB_STATE_ADDRESSED) {
3854 USB_BUS_LOCK(&sc->sc_bus);
3856 for (x = 1; x != sc->sc_dev_ep_max; x++) {
3858 if (x < sc->sc_dev_in_ep_max) {
3859 DWC_OTG_WRITE_4(sc, DOTG_DIEPCTL(x),
3861 DWC_OTG_WRITE_4(sc, DOTG_DIEPCTL(x), 0);
3864 DWC_OTG_WRITE_4(sc, DOTG_DOEPCTL(x),
3866 DWC_OTG_WRITE_4(sc, DOTG_DOEPCTL(x), 0);
3868 USB_BUS_UNLOCK(&sc->sc_bus);
3873 dwc_otg_init(struct dwc_otg_softc *sc)
3879 /* set up the bus structure */
3880 sc->sc_bus.usbrev = USB_REV_2_0;
3881 sc->sc_bus.methods = &dwc_otg_bus_methods;
3883 usb_callout_init_mtx(&sc->sc_timer,
3884 &sc->sc_bus.bus_mtx, 0);
3886 USB_BUS_LOCK(&sc->sc_bus);
3888 /* turn on clocks */
3889 dwc_otg_clocks_on(sc);
3891 temp = DWC_OTG_READ_4(sc, DOTG_GSNPSID);
3892 DPRINTF("Version = 0x%08x\n", temp);
3895 DWC_OTG_WRITE_4(sc, DOTG_DCTL,
3898 /* wait for host to detect disconnect */
3899 usb_pause_mtx(&sc->sc_bus.bus_mtx, hz / 32);
3901 DWC_OTG_WRITE_4(sc, DOTG_GRSTCTL,
3904 /* wait a little bit for block to reset */
3905 usb_pause_mtx(&sc->sc_bus.bus_mtx, hz / 128);
3907 switch (sc->sc_mode) {
3908 case DWC_MODE_DEVICE:
3909 temp = GUSBCFG_FORCEDEVMODE;
3912 temp = GUSBCFG_FORCEHOSTMODE;
3919 if (sc->sc_phy_type == 0)
3920 sc->sc_phy_type = dwc_otg_phy_type + 1;
3921 if (sc->sc_phy_bits == 0)
3922 sc->sc_phy_bits = 16;
3924 /* select HSIC, ULPI, UTMI+ or internal PHY mode */
3925 switch (sc->sc_phy_type) {
3926 case DWC_OTG_PHY_HSIC:
3927 DWC_OTG_WRITE_4(sc, DOTG_GUSBCFG,
3929 GUSBCFG_TRD_TIM_SET(5) | temp);
3930 DWC_OTG_WRITE_4(sc, DOTG_GOTGCTL,
3933 temp = DWC_OTG_READ_4(sc, DOTG_GLPMCFG);
3934 DWC_OTG_WRITE_4(sc, DOTG_GLPMCFG,
3935 temp & ~GLPMCFG_HSIC_CONN);
3936 DWC_OTG_WRITE_4(sc, DOTG_GLPMCFG,
3937 temp | GLPMCFG_HSIC_CONN);
3939 case DWC_OTG_PHY_ULPI:
3940 DWC_OTG_WRITE_4(sc, DOTG_GUSBCFG,
3941 GUSBCFG_ULPI_UTMI_SEL |
3942 GUSBCFG_TRD_TIM_SET(5) | temp);
3943 DWC_OTG_WRITE_4(sc, DOTG_GOTGCTL, 0);
3945 temp = DWC_OTG_READ_4(sc, DOTG_GLPMCFG);
3946 DWC_OTG_WRITE_4(sc, DOTG_GLPMCFG,
3947 temp & ~GLPMCFG_HSIC_CONN);
3949 case DWC_OTG_PHY_UTMI:
3950 DWC_OTG_WRITE_4(sc, DOTG_GUSBCFG,
3951 (sc->sc_phy_bits == 16 ? GUSBCFG_PHYIF : 0) |
3952 GUSBCFG_TRD_TIM_SET(5) | temp);
3953 DWC_OTG_WRITE_4(sc, DOTG_GOTGCTL, 0);
3955 temp = DWC_OTG_READ_4(sc, DOTG_GLPMCFG);
3956 DWC_OTG_WRITE_4(sc, DOTG_GLPMCFG,
3957 temp & ~GLPMCFG_HSIC_CONN);
3959 case DWC_OTG_PHY_INTERNAL:
3960 DWC_OTG_WRITE_4(sc, DOTG_GUSBCFG,
3962 GUSBCFG_TRD_TIM_SET(5) | temp);
3963 DWC_OTG_WRITE_4(sc, DOTG_GOTGCTL, 0);
3965 temp = DWC_OTG_READ_4(sc, DOTG_GLPMCFG);
3966 DWC_OTG_WRITE_4(sc, DOTG_GLPMCFG,
3967 temp & ~GLPMCFG_HSIC_CONN);
3969 temp = DWC_OTG_READ_4(sc, DOTG_GGPIO);
3970 temp &= ~(DOTG_GGPIO_NOVBUSSENS | DOTG_GGPIO_I2CPADEN);
3971 temp |= (DOTG_GGPIO_VBUSASEN | DOTG_GGPIO_VBUSBSEN |
3973 DWC_OTG_WRITE_4(sc, DOTG_GGPIO, temp);
3979 /* clear global nak */
3980 DWC_OTG_WRITE_4(sc, DOTG_DCTL,
3984 /* disable USB port */
3985 DWC_OTG_WRITE_4(sc, DOTG_PCGCCTL, 0xFFFFFFFF);
3988 usb_pause_mtx(&sc->sc_bus.bus_mtx, hz / 100);
3990 /* enable USB port */
3991 DWC_OTG_WRITE_4(sc, DOTG_PCGCCTL, 0);
3994 usb_pause_mtx(&sc->sc_bus.bus_mtx, hz / 100);
3996 temp = DWC_OTG_READ_4(sc, DOTG_GHWCFG3);
3998 sc->sc_fifo_size = 4 * GHWCFG3_DFIFODEPTH_GET(temp);
4000 temp = DWC_OTG_READ_4(sc, DOTG_GHWCFG2);
4002 sc->sc_dev_ep_max = GHWCFG2_NUMDEVEPS_GET(temp);
4004 if (sc->sc_dev_ep_max > DWC_OTG_MAX_ENDPOINTS)
4005 sc->sc_dev_ep_max = DWC_OTG_MAX_ENDPOINTS;
4007 sc->sc_host_ch_max = GHWCFG2_NUMHSTCHNL_GET(temp);
4009 if (sc->sc_host_ch_max > DWC_OTG_MAX_CHANNELS)
4010 sc->sc_host_ch_max = DWC_OTG_MAX_CHANNELS;
4012 temp = DWC_OTG_READ_4(sc, DOTG_GHWCFG4);
4014 sc->sc_dev_in_ep_max = GHWCFG4_NUM_IN_EP_GET(temp);
4016 DPRINTF("Total FIFO size = %d bytes, Device EPs = %d/%d Host CHs = %d\n",
4017 sc->sc_fifo_size, sc->sc_dev_ep_max, sc->sc_dev_in_ep_max,
4018 sc->sc_host_ch_max);
4021 if (dwc_otg_init_fifo(sc, sc->sc_mode)) {
4022 USB_BUS_UNLOCK(&sc->sc_bus);
4026 /* enable interrupts */
4027 sc->sc_irq_mask |= DWC_OTG_MSK_GINT_THREAD_IRQ;
4028 DWC_OTG_WRITE_4(sc, DOTG_GINTMSK, sc->sc_irq_mask);
4030 if (sc->sc_mode == DWC_MODE_OTG || sc->sc_mode == DWC_MODE_DEVICE) {
4032 /* enable all endpoint interrupts */
4033 temp = DWC_OTG_READ_4(sc, DOTG_GHWCFG2);
4034 if (temp & GHWCFG2_MPI) {
4037 DPRINTF("Disable Multi Process Interrupts\n");
4039 for (x = 0; x != sc->sc_dev_in_ep_max; x++) {
4040 DWC_OTG_WRITE_4(sc, DOTG_DIEPEACHINTMSK(x), 0);
4041 DWC_OTG_WRITE_4(sc, DOTG_DOEPEACHINTMSK(x), 0);
4043 DWC_OTG_WRITE_4(sc, DOTG_DEACHINTMSK, 0);
4045 DWC_OTG_WRITE_4(sc, DOTG_DIEPMSK,
4046 DIEPMSK_XFERCOMPLMSK);
4047 DWC_OTG_WRITE_4(sc, DOTG_DOEPMSK, 0);
4048 DWC_OTG_WRITE_4(sc, DOTG_DAINTMSK, 0xFFFF);
4051 if (sc->sc_mode == DWC_MODE_OTG || sc->sc_mode == DWC_MODE_HOST) {
4053 temp = DWC_OTG_READ_4(sc, DOTG_HCFG);
4054 temp &= ~(HCFG_FSLSSUPP | HCFG_FSLSPCLKSEL_MASK);
4055 temp |= (1 << HCFG_FSLSPCLKSEL_SHIFT);
4056 DWC_OTG_WRITE_4(sc, DOTG_HCFG, temp);
4059 /* only enable global IRQ */
4060 DWC_OTG_WRITE_4(sc, DOTG_GAHBCFG,
4061 GAHBCFG_GLBLINTRMSK);
4063 /* turn off clocks */
4064 dwc_otg_clocks_off(sc);
4066 /* read initial VBUS state */
4068 temp = DWC_OTG_READ_4(sc, DOTG_GOTGCTL);
4070 DPRINTFN(5, "GOTCTL=0x%08x\n", temp);
4072 dwc_otg_vbus_interrupt(sc,
4073 (temp & (GOTGCTL_ASESVLD | GOTGCTL_BSESVLD)) ? 1 : 0);
4075 USB_BUS_UNLOCK(&sc->sc_bus);
4077 /* catch any lost interrupts */
4079 dwc_otg_do_poll(&sc->sc_bus);
4081 return (0); /* success */
4085 dwc_otg_uninit(struct dwc_otg_softc *sc)
4087 USB_BUS_LOCK(&sc->sc_bus);
4089 /* stop host timer */
4090 dwc_otg_timer_stop(sc);
4092 /* set disconnect */
4093 DWC_OTG_WRITE_4(sc, DOTG_DCTL,
4096 /* turn off global IRQ */
4097 DWC_OTG_WRITE_4(sc, DOTG_GAHBCFG, 0);
4099 sc->sc_flags.port_enabled = 0;
4100 sc->sc_flags.port_powered = 0;
4101 sc->sc_flags.status_vbus = 0;
4102 sc->sc_flags.status_bus_reset = 0;
4103 sc->sc_flags.status_suspend = 0;
4104 sc->sc_flags.change_suspend = 0;
4105 sc->sc_flags.change_connect = 1;
4107 dwc_otg_pull_down(sc);
4108 dwc_otg_clocks_off(sc);
4110 USB_BUS_UNLOCK(&sc->sc_bus);
4112 usb_callout_drain(&sc->sc_timer);
4116 dwc_otg_suspend(struct dwc_otg_softc *sc)
4122 dwc_otg_resume(struct dwc_otg_softc *sc)
4128 dwc_otg_do_poll(struct usb_bus *bus)
4130 struct dwc_otg_softc *sc = DWC_OTG_BUS2SC(bus);
4132 USB_BUS_LOCK(&sc->sc_bus);
4133 USB_BUS_SPIN_LOCK(&sc->sc_bus);
4134 dwc_otg_interrupt_poll_locked(sc);
4135 dwc_otg_interrupt_complete_locked(sc);
4136 USB_BUS_SPIN_UNLOCK(&sc->sc_bus);
4137 USB_BUS_UNLOCK(&sc->sc_bus);
4140 /*------------------------------------------------------------------------*
4141 * DWC OTG bulk support
4142 * DWC OTG control support
4143 * DWC OTG interrupt support
4144 *------------------------------------------------------------------------*/
4146 dwc_otg_device_non_isoc_open(struct usb_xfer *xfer)
4151 dwc_otg_device_non_isoc_close(struct usb_xfer *xfer)
4153 dwc_otg_device_done(xfer, USB_ERR_CANCELLED);
4157 dwc_otg_device_non_isoc_enter(struct usb_xfer *xfer)
4162 dwc_otg_device_non_isoc_start(struct usb_xfer *xfer)
4165 dwc_otg_setup_standard_chain(xfer);
4166 dwc_otg_start_standard_chain(xfer);
4169 static const struct usb_pipe_methods dwc_otg_device_non_isoc_methods =
4171 .open = dwc_otg_device_non_isoc_open,
4172 .close = dwc_otg_device_non_isoc_close,
4173 .enter = dwc_otg_device_non_isoc_enter,
4174 .start = dwc_otg_device_non_isoc_start,
4177 /*------------------------------------------------------------------------*
4178 * DWC OTG full speed isochronous support
4179 *------------------------------------------------------------------------*/
4181 dwc_otg_device_isoc_open(struct usb_xfer *xfer)
4186 dwc_otg_device_isoc_close(struct usb_xfer *xfer)
4188 dwc_otg_device_done(xfer, USB_ERR_CANCELLED);
4192 dwc_otg_device_isoc_enter(struct usb_xfer *xfer)
4197 dwc_otg_device_isoc_start(struct usb_xfer *xfer)
4199 struct dwc_otg_softc *sc = DWC_OTG_BUS2SC(xfer->xroot->bus);
4203 uint8_t shift = usbd_xfer_get_fps_shift(xfer);
4205 DPRINTFN(6, "xfer=%p next=%d nframes=%d\n",
4206 xfer, xfer->endpoint->isoc_next, xfer->nframes);
4208 if (xfer->xroot->udev->flags.usb_mode == USB_MODE_HOST) {
4209 temp = DWC_OTG_READ_4(sc, DOTG_HFNUM);
4211 /* get the current frame index */
4212 framenum = (temp & HFNUM_FRNUM_MASK);
4214 temp = DWC_OTG_READ_4(sc, DOTG_DSTS);
4216 /* get the current frame index */
4217 framenum = DSTS_SOFFN_GET(temp);
4221 * Check if port is doing 8000 or 1000 frames per second:
4223 if (sc->sc_flags.status_high_speed)
4226 framenum &= DWC_OTG_FRAME_MASK;
4229 * Compute number of milliseconds worth of data traffic for
4230 * this USB transfer:
4232 if (xfer->xroot->udev->speed == USB_SPEED_HIGH)
4233 msframes = ((xfer->nframes << shift) + 7) / 8;
4235 msframes = xfer->nframes;
4238 * check if the frame index is within the window where the frames
4241 temp = (framenum - xfer->endpoint->isoc_next) & DWC_OTG_FRAME_MASK;
4243 if ((xfer->endpoint->is_synced == 0) || (temp < msframes)) {
4245 * If there is data underflow or the pipe queue is
4246 * empty we schedule the transfer a few frames ahead
4247 * of the current frame position. Else two isochronous
4248 * transfers might overlap.
4250 xfer->endpoint->isoc_next = (framenum + 3) & DWC_OTG_FRAME_MASK;
4251 xfer->endpoint->is_synced = 1;
4252 DPRINTFN(3, "start next=%d\n", xfer->endpoint->isoc_next);
4255 * compute how many milliseconds the insertion is ahead of the
4256 * current frame position:
4258 temp = (xfer->endpoint->isoc_next - framenum) & DWC_OTG_FRAME_MASK;
4261 * pre-compute when the isochronous transfer will be finished:
4263 xfer->isoc_time_complete =
4264 usb_isoc_time_expand(&sc->sc_bus, framenum) + temp + msframes;
4267 dwc_otg_setup_standard_chain(xfer);
4269 /* compute frame number for next insertion */
4270 xfer->endpoint->isoc_next += msframes;
4272 /* start TD chain */
4273 dwc_otg_start_standard_chain(xfer);
4276 static const struct usb_pipe_methods dwc_otg_device_isoc_methods =
4278 .open = dwc_otg_device_isoc_open,
4279 .close = dwc_otg_device_isoc_close,
4280 .enter = dwc_otg_device_isoc_enter,
4281 .start = dwc_otg_device_isoc_start,
4284 /*------------------------------------------------------------------------*
4285 * DWC OTG root control support
4286 *------------------------------------------------------------------------*
4287 * Simulate a hardware HUB by handling all the necessary requests.
4288 *------------------------------------------------------------------------*/
4290 static const struct usb_device_descriptor dwc_otg_devd = {
4291 .bLength = sizeof(struct usb_device_descriptor),
4292 .bDescriptorType = UDESC_DEVICE,
4293 .bcdUSB = {0x00, 0x02},
4294 .bDeviceClass = UDCLASS_HUB,
4295 .bDeviceSubClass = UDSUBCLASS_HUB,
4296 .bDeviceProtocol = UDPROTO_HSHUBSTT,
4297 .bMaxPacketSize = 64,
4298 .bcdDevice = {0x00, 0x01},
4301 .bNumConfigurations = 1,
4304 static const struct dwc_otg_config_desc dwc_otg_confd = {
4306 .bLength = sizeof(struct usb_config_descriptor),
4307 .bDescriptorType = UDESC_CONFIG,
4308 .wTotalLength[0] = sizeof(dwc_otg_confd),
4310 .bConfigurationValue = 1,
4311 .iConfiguration = 0,
4312 .bmAttributes = UC_SELF_POWERED,
4316 .bLength = sizeof(struct usb_interface_descriptor),
4317 .bDescriptorType = UDESC_INTERFACE,
4319 .bInterfaceClass = UICLASS_HUB,
4320 .bInterfaceSubClass = UISUBCLASS_HUB,
4321 .bInterfaceProtocol = 0,
4324 .bLength = sizeof(struct usb_endpoint_descriptor),
4325 .bDescriptorType = UDESC_ENDPOINT,
4326 .bEndpointAddress = (UE_DIR_IN | DWC_OTG_INTR_ENDPT),
4327 .bmAttributes = UE_INTERRUPT,
4328 .wMaxPacketSize[0] = 8,
4333 #define HSETW(ptr, val) ptr = { (uint8_t)(val), (uint8_t)((val) >> 8) }
4335 static const struct usb_hub_descriptor_min dwc_otg_hubd = {
4336 .bDescLength = sizeof(dwc_otg_hubd),
4337 .bDescriptorType = UDESC_HUB,
4339 HSETW(.wHubCharacteristics, (UHD_PWR_NO_SWITCH | UHD_OC_INDIVIDUAL)),
4340 .bPwrOn2PwrGood = 50,
4341 .bHubContrCurrent = 0,
4342 .DeviceRemovable = {0}, /* port is removable */
4345 #define STRING_VENDOR \
4348 #define STRING_PRODUCT \
4349 "O\0T\0G\0 \0R\0o\0o\0t\0 \0H\0U\0B"
4351 USB_MAKE_STRING_DESC(STRING_VENDOR, dwc_otg_vendor);
4352 USB_MAKE_STRING_DESC(STRING_PRODUCT, dwc_otg_product);
4355 dwc_otg_roothub_exec(struct usb_device *udev,
4356 struct usb_device_request *req, const void **pptr, uint16_t *plength)
4358 struct dwc_otg_softc *sc = DWC_OTG_BUS2SC(udev->bus);
4365 USB_BUS_LOCK_ASSERT(&sc->sc_bus, MA_OWNED);
4368 ptr = (const void *)&sc->sc_hub_temp;
4372 value = UGETW(req->wValue);
4373 index = UGETW(req->wIndex);
4375 /* demultiplex the control request */
4377 switch (req->bmRequestType) {
4378 case UT_READ_DEVICE:
4379 switch (req->bRequest) {
4380 case UR_GET_DESCRIPTOR:
4381 goto tr_handle_get_descriptor;
4383 goto tr_handle_get_config;
4385 goto tr_handle_get_status;
4391 case UT_WRITE_DEVICE:
4392 switch (req->bRequest) {
4393 case UR_SET_ADDRESS:
4394 goto tr_handle_set_address;
4396 goto tr_handle_set_config;
4397 case UR_CLEAR_FEATURE:
4398 goto tr_valid; /* nop */
4399 case UR_SET_DESCRIPTOR:
4400 goto tr_valid; /* nop */
4401 case UR_SET_FEATURE:
4407 case UT_WRITE_ENDPOINT:
4408 switch (req->bRequest) {
4409 case UR_CLEAR_FEATURE:
4410 switch (UGETW(req->wValue)) {
4411 case UF_ENDPOINT_HALT:
4412 goto tr_handle_clear_halt;
4413 case UF_DEVICE_REMOTE_WAKEUP:
4414 goto tr_handle_clear_wakeup;
4419 case UR_SET_FEATURE:
4420 switch (UGETW(req->wValue)) {
4421 case UF_ENDPOINT_HALT:
4422 goto tr_handle_set_halt;
4423 case UF_DEVICE_REMOTE_WAKEUP:
4424 goto tr_handle_set_wakeup;
4429 case UR_SYNCH_FRAME:
4430 goto tr_valid; /* nop */
4436 case UT_READ_ENDPOINT:
4437 switch (req->bRequest) {
4439 goto tr_handle_get_ep_status;
4445 case UT_WRITE_INTERFACE:
4446 switch (req->bRequest) {
4447 case UR_SET_INTERFACE:
4448 goto tr_handle_set_interface;
4449 case UR_CLEAR_FEATURE:
4450 goto tr_valid; /* nop */
4451 case UR_SET_FEATURE:
4457 case UT_READ_INTERFACE:
4458 switch (req->bRequest) {
4459 case UR_GET_INTERFACE:
4460 goto tr_handle_get_interface;
4462 goto tr_handle_get_iface_status;
4468 case UT_WRITE_CLASS_INTERFACE:
4469 case UT_WRITE_VENDOR_INTERFACE:
4473 case UT_READ_CLASS_INTERFACE:
4474 case UT_READ_VENDOR_INTERFACE:
4478 case UT_WRITE_CLASS_DEVICE:
4479 switch (req->bRequest) {
4480 case UR_CLEAR_FEATURE:
4482 case UR_SET_DESCRIPTOR:
4483 case UR_SET_FEATURE:
4490 case UT_WRITE_CLASS_OTHER:
4491 switch (req->bRequest) {
4492 case UR_CLEAR_FEATURE:
4493 goto tr_handle_clear_port_feature;
4494 case UR_SET_FEATURE:
4495 goto tr_handle_set_port_feature;
4496 case UR_CLEAR_TT_BUFFER:
4506 case UT_READ_CLASS_OTHER:
4507 switch (req->bRequest) {
4508 case UR_GET_TT_STATE:
4509 goto tr_handle_get_tt_state;
4511 goto tr_handle_get_port_status;
4517 case UT_READ_CLASS_DEVICE:
4518 switch (req->bRequest) {
4519 case UR_GET_DESCRIPTOR:
4520 goto tr_handle_get_class_descriptor;
4522 goto tr_handle_get_class_status;
4533 tr_handle_get_descriptor:
4534 switch (value >> 8) {
4539 len = sizeof(dwc_otg_devd);
4540 ptr = (const void *)&dwc_otg_devd;
4546 len = sizeof(dwc_otg_confd);
4547 ptr = (const void *)&dwc_otg_confd;
4550 switch (value & 0xff) {
4551 case 0: /* Language table */
4552 len = sizeof(usb_string_lang_en);
4553 ptr = (const void *)&usb_string_lang_en;
4556 case 1: /* Vendor */
4557 len = sizeof(dwc_otg_vendor);
4558 ptr = (const void *)&dwc_otg_vendor;
4561 case 2: /* Product */
4562 len = sizeof(dwc_otg_product);
4563 ptr = (const void *)&dwc_otg_product;
4574 tr_handle_get_config:
4576 sc->sc_hub_temp.wValue[0] = sc->sc_conf;
4579 tr_handle_get_status:
4581 USETW(sc->sc_hub_temp.wValue, UDS_SELF_POWERED);
4584 tr_handle_set_address:
4585 if (value & 0xFF00) {
4588 sc->sc_rt_addr = value;
4591 tr_handle_set_config:
4595 sc->sc_conf = value;
4598 tr_handle_get_interface:
4600 sc->sc_hub_temp.wValue[0] = 0;
4603 tr_handle_get_tt_state:
4604 tr_handle_get_class_status:
4605 tr_handle_get_iface_status:
4606 tr_handle_get_ep_status:
4608 USETW(sc->sc_hub_temp.wValue, 0);
4612 tr_handle_set_interface:
4613 tr_handle_set_wakeup:
4614 tr_handle_clear_wakeup:
4615 tr_handle_clear_halt:
4618 tr_handle_clear_port_feature:
4622 DPRINTFN(9, "UR_CLEAR_PORT_FEATURE on port %d\n", index);
4625 case UHF_PORT_SUSPEND:
4626 dwc_otg_wakeup_peer(sc);
4629 case UHF_PORT_ENABLE:
4630 if (sc->sc_flags.status_device_mode == 0) {
4631 DWC_OTG_WRITE_4(sc, DOTG_HPRT,
4632 sc->sc_hprt_val | HPRT_PRTENA);
4634 sc->sc_flags.port_enabled = 0;
4637 case UHF_C_PORT_RESET:
4638 sc->sc_flags.change_reset = 0;
4641 case UHF_C_PORT_ENABLE:
4642 sc->sc_flags.change_enabled = 0;
4645 case UHF_C_PORT_OVER_CURRENT:
4646 sc->sc_flags.change_over_current = 0;
4650 case UHF_PORT_INDICATOR:
4654 case UHF_PORT_POWER:
4655 sc->sc_flags.port_powered = 0;
4656 if (sc->sc_mode == DWC_MODE_HOST || sc->sc_mode == DWC_MODE_OTG) {
4657 sc->sc_hprt_val = 0;
4658 DWC_OTG_WRITE_4(sc, DOTG_HPRT, HPRT_PRTENA);
4660 dwc_otg_pull_down(sc);
4661 dwc_otg_clocks_off(sc);
4664 case UHF_C_PORT_CONNECTION:
4665 /* clear connect change flag */
4666 sc->sc_flags.change_connect = 0;
4669 case UHF_C_PORT_SUSPEND:
4670 sc->sc_flags.change_suspend = 0;
4674 err = USB_ERR_IOERROR;
4679 tr_handle_set_port_feature:
4683 DPRINTFN(9, "UR_SET_PORT_FEATURE\n");
4686 case UHF_PORT_ENABLE:
4689 case UHF_PORT_SUSPEND:
4690 if (sc->sc_flags.status_device_mode == 0) {
4691 /* set suspend BIT */
4692 sc->sc_hprt_val |= HPRT_PRTSUSP;
4693 DWC_OTG_WRITE_4(sc, DOTG_HPRT, sc->sc_hprt_val);
4695 /* generate HUB suspend event */
4696 dwc_otg_suspend_irq(sc);
4700 case UHF_PORT_RESET:
4701 if (sc->sc_flags.status_device_mode == 0) {
4703 DPRINTF("PORT RESET\n");
4705 /* enable PORT reset */
4706 DWC_OTG_WRITE_4(sc, DOTG_HPRT, sc->sc_hprt_val | HPRT_PRTRST);
4708 /* Wait 62.5ms for reset to complete */
4709 usb_pause_mtx(&sc->sc_bus.bus_mtx, hz / 16);
4711 DWC_OTG_WRITE_4(sc, DOTG_HPRT, sc->sc_hprt_val);
4713 /* Wait 62.5ms for reset to complete */
4714 usb_pause_mtx(&sc->sc_bus.bus_mtx, hz / 16);
4717 (void) dwc_otg_init_fifo(sc, DWC_MODE_HOST);
4719 sc->sc_flags.change_reset = 1;
4721 err = USB_ERR_IOERROR;
4726 case UHF_PORT_INDICATOR:
4729 case UHF_PORT_POWER:
4730 sc->sc_flags.port_powered = 1;
4731 if (sc->sc_mode == DWC_MODE_HOST || sc->sc_mode == DWC_MODE_OTG) {
4732 sc->sc_hprt_val |= HPRT_PRTPWR;
4733 DWC_OTG_WRITE_4(sc, DOTG_HPRT, sc->sc_hprt_val);
4735 if (sc->sc_mode == DWC_MODE_DEVICE || sc->sc_mode == DWC_MODE_OTG) {
4736 /* pull up D+, if any */
4737 dwc_otg_pull_up(sc);
4741 err = USB_ERR_IOERROR;
4746 tr_handle_get_port_status:
4748 DPRINTFN(9, "UR_GET_PORT_STATUS\n");
4753 if (sc->sc_flags.status_vbus)
4754 dwc_otg_clocks_on(sc);
4756 dwc_otg_clocks_off(sc);
4758 /* Select Device Side Mode */
4760 if (sc->sc_flags.status_device_mode) {
4761 value = UPS_PORT_MODE_DEVICE;
4762 dwc_otg_timer_stop(sc);
4765 dwc_otg_timer_start(sc);
4768 if (sc->sc_flags.status_high_speed)
4769 value |= UPS_HIGH_SPEED;
4770 else if (sc->sc_flags.status_low_speed)
4771 value |= UPS_LOW_SPEED;
4773 if (sc->sc_flags.port_powered)
4774 value |= UPS_PORT_POWER;
4776 if (sc->sc_flags.port_enabled)
4777 value |= UPS_PORT_ENABLED;
4779 if (sc->sc_flags.port_over_current)
4780 value |= UPS_OVERCURRENT_INDICATOR;
4782 if (sc->sc_flags.status_vbus &&
4783 sc->sc_flags.status_bus_reset)
4784 value |= UPS_CURRENT_CONNECT_STATUS;
4786 if (sc->sc_flags.status_suspend)
4787 value |= UPS_SUSPEND;
4789 USETW(sc->sc_hub_temp.ps.wPortStatus, value);
4793 if (sc->sc_flags.change_enabled)
4794 value |= UPS_C_PORT_ENABLED;
4795 if (sc->sc_flags.change_connect)
4796 value |= UPS_C_CONNECT_STATUS;
4797 if (sc->sc_flags.change_suspend)
4798 value |= UPS_C_SUSPEND;
4799 if (sc->sc_flags.change_reset)
4800 value |= UPS_C_PORT_RESET;
4801 if (sc->sc_flags.change_over_current)
4802 value |= UPS_C_OVERCURRENT_INDICATOR;
4804 USETW(sc->sc_hub_temp.ps.wPortChange, value);
4805 len = sizeof(sc->sc_hub_temp.ps);
4808 tr_handle_get_class_descriptor:
4812 ptr = (const void *)&dwc_otg_hubd;
4813 len = sizeof(dwc_otg_hubd);
4817 err = USB_ERR_STALLED;
4826 dwc_otg_xfer_setup(struct usb_setup_params *parm)
4828 struct usb_xfer *xfer;
4835 xfer = parm->curr_xfer;
4838 * NOTE: This driver does not use any of the parameters that
4839 * are computed from the following values. Just set some
4840 * reasonable dummies:
4842 parm->hc_max_packet_size = 0x500;
4843 parm->hc_max_packet_count = 3;
4844 parm->hc_max_frame_size = 3 * 0x500;
4846 usbd_transfer_setup_sub(parm);
4849 * compute maximum number of TDs
4851 ep_type = (xfer->endpoint->edesc->bmAttributes & UE_XFERTYPE);
4853 if (ep_type == UE_CONTROL) {
4855 ntd = xfer->nframes + 1 /* STATUS */ + 1 /* SYNC 1 */
4856 + 1 /* SYNC 2 */ + 1 /* SYNC 3 */;
4859 ntd = xfer->nframes + 1 /* SYNC */ ;
4863 * check if "usbd_transfer_setup_sub" set an error
4869 * allocate transfer descriptors
4873 ep_no = xfer->endpointno & UE_ADDR;
4876 * Check for a valid endpoint profile in USB device mode:
4878 if (xfer->flags_int.usb_mode == USB_MODE_DEVICE) {
4879 const struct usb_hw_ep_profile *pf;
4881 dwc_otg_get_hw_ep_profile(parm->udev, &pf, ep_no);
4884 /* should not happen */
4885 parm->err = USB_ERR_INVAL;
4891 parm->size[0] += ((-parm->size[0]) & (USB_HOST_ALIGN - 1));
4893 for (n = 0; n != ntd; n++) {
4895 struct dwc_otg_td *td;
4899 td = USB_ADD_BYTES(parm->buf, parm->size[0]);
4901 /* compute shared bandwidth resource index for TT */
4902 if (dwc_otg_uses_split(parm->udev)) {
4903 if (parm->udev->parent_hs_hub->ddesc.bDeviceProtocol == UDPROTO_HSHUBMTT)
4904 td->tt_index = parm->udev->device_index;
4906 td->tt_index = parm->udev->parent_hs_hub->device_index;
4908 td->tt_index = parm->udev->device_index;
4912 td->max_packet_size = xfer->max_packet_size;
4913 td->max_packet_count = xfer->max_packet_count;
4915 if (td->max_packet_count == 0 || td->max_packet_count > 3)
4916 td->max_packet_count = 1;
4918 td->ep_type = ep_type;
4919 td->obj_next = last_obj;
4923 parm->size[0] += sizeof(*td);
4926 xfer->td_start[0] = last_obj;
4930 dwc_otg_xfer_unsetup(struct usb_xfer *xfer)
4936 dwc_otg_ep_init(struct usb_device *udev, struct usb_endpoint_descriptor *edesc,
4937 struct usb_endpoint *ep)
4939 struct dwc_otg_softc *sc = DWC_OTG_BUS2SC(udev->bus);
4941 DPRINTFN(2, "endpoint=%p, addr=%d, endpt=%d, mode=%d (%d,%d)\n",
4943 edesc->bEndpointAddress, udev->flags.usb_mode,
4944 sc->sc_rt_addr, udev->device_index);
4946 if (udev->device_index != sc->sc_rt_addr) {
4948 if (udev->flags.usb_mode == USB_MODE_DEVICE) {
4949 if (udev->speed != USB_SPEED_FULL &&
4950 udev->speed != USB_SPEED_HIGH) {
4955 if (udev->speed == USB_SPEED_HIGH &&
4956 (edesc->wMaxPacketSize[1] & 0x18) != 0 &&
4957 (edesc->bmAttributes & UE_XFERTYPE) != UE_ISOCHRONOUS) {
4959 DPRINTFN(-1, "Non-isochronous high bandwidth "
4960 "endpoint not supported\n");
4964 if ((edesc->bmAttributes & UE_XFERTYPE) == UE_ISOCHRONOUS)
4965 ep->methods = &dwc_otg_device_isoc_methods;
4967 ep->methods = &dwc_otg_device_non_isoc_methods;
4972 dwc_otg_set_hw_power_sleep(struct usb_bus *bus, uint32_t state)
4974 struct dwc_otg_softc *sc = DWC_OTG_BUS2SC(bus);
4977 case USB_HW_POWER_SUSPEND:
4978 dwc_otg_suspend(sc);
4980 case USB_HW_POWER_SHUTDOWN:
4983 case USB_HW_POWER_RESUME:
4992 dwc_otg_get_dma_delay(struct usb_device *udev, uint32_t *pus)
4994 /* DMA delay - wait until any use of memory is finished */
4995 *pus = (2125); /* microseconds */
4999 dwc_otg_device_resume(struct usb_device *udev)
5003 /* poll all transfers again to restart resumed ones */
5004 dwc_otg_do_poll(udev->bus);
5008 dwc_otg_device_suspend(struct usb_device *udev)
5013 static const struct usb_bus_methods dwc_otg_bus_methods =
5015 .endpoint_init = &dwc_otg_ep_init,
5016 .xfer_setup = &dwc_otg_xfer_setup,
5017 .xfer_unsetup = &dwc_otg_xfer_unsetup,
5018 .get_hw_ep_profile = &dwc_otg_get_hw_ep_profile,
5019 .xfer_stall = &dwc_otg_xfer_stall,
5020 .set_stall = &dwc_otg_set_stall,
5021 .clear_stall = &dwc_otg_clear_stall,
5022 .roothub_exec = &dwc_otg_roothub_exec,
5023 .xfer_poll = &dwc_otg_do_poll,
5024 .device_state_change = &dwc_otg_device_state_change,
5025 .set_hw_power_sleep = &dwc_otg_set_hw_power_sleep,
5026 .get_dma_delay = &dwc_otg_get_dma_delay,
5027 .device_resume = &dwc_otg_device_resume,
5028 .device_suspend = &dwc_otg_device_suspend,