3 * Copyright (c) 2015 Daisuke Aoyama. All rights reserved.
4 * Copyright (c) 2012-2015 Hans Petter Selasky. All rights reserved.
5 * Copyright (c) 2010-2011 Aleksandr Rybalko. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * This file contains the driver for the DesignWare series USB 2.0 OTG
35 * LIMITATION: Drivers must be bound to all OUT endpoints in the
36 * active configuration for this driver to work properly. Blocking any
37 * OUT endpoint will block all OUT endpoints including the control
38 * endpoint. Usually this is not a problem.
42 * NOTE: Writing to non-existing registers appears to cause an
46 #ifdef USB_GLOBAL_INCLUDE_FILE
47 #include USB_GLOBAL_INCLUDE_FILE
49 #include <sys/stdint.h>
50 #include <sys/stddef.h>
51 #include <sys/param.h>
52 #include <sys/queue.h>
53 #include <sys/types.h>
54 #include <sys/systm.h>
55 #include <sys/kernel.h>
57 #include <sys/module.h>
59 #include <sys/mutex.h>
60 #include <sys/condvar.h>
61 #include <sys/sysctl.h>
63 #include <sys/unistd.h>
64 #include <sys/callout.h>
65 #include <sys/malloc.h>
68 #include <dev/usb/usb.h>
69 #include <dev/usb/usbdi.h>
71 #define USB_DEBUG_VAR dwc_otg_debug
73 #include <dev/usb/usb_core.h>
74 #include <dev/usb/usb_debug.h>
75 #include <dev/usb/usb_busdma.h>
76 #include <dev/usb/usb_process.h>
77 #include <dev/usb/usb_transfer.h>
78 #include <dev/usb/usb_device.h>
79 #include <dev/usb/usb_hub.h>
80 #include <dev/usb/usb_util.h>
82 #include <dev/usb/usb_controller.h>
83 #include <dev/usb/usb_bus.h>
84 #endif /* USB_GLOBAL_INCLUDE_FILE */
86 #include <dev/usb/controller/dwc_otg.h>
87 #include <dev/usb/controller/dwc_otgreg.h>
89 #define DWC_OTG_BUS2SC(bus) \
90 ((struct dwc_otg_softc *)(((uint8_t *)(bus)) - \
91 ((uint8_t *)&(((struct dwc_otg_softc *)0)->sc_bus))))
93 #define DWC_OTG_PC2UDEV(pc) \
94 (USB_DMATAG_TO_XROOT((pc)->tag_parent)->udev)
96 #define DWC_OTG_MSK_GINT_THREAD_IRQ \
97 (GINTSTS_USBRST | GINTSTS_ENUMDONE | GINTSTS_PRTINT | \
98 GINTSTS_WKUPINT | GINTSTS_USBSUSP | GINTMSK_OTGINTMSK | \
101 #define DWC_OTG_PHY_ULPI 0
102 #define DWC_OTG_PHY_HSIC 1
103 #define DWC_OTG_PHY_INTERNAL 2
105 #ifndef DWC_OTG_PHY_DEFAULT
106 #define DWC_OTG_PHY_DEFAULT DWC_OTG_PHY_ULPI
109 static int dwc_otg_phy_type = DWC_OTG_PHY_DEFAULT;
111 static SYSCTL_NODE(_hw_usb, OID_AUTO, dwc_otg, CTLFLAG_RW, 0, "USB DWC OTG");
112 SYSCTL_INT(_hw_usb_dwc_otg, OID_AUTO, phy_type, CTLFLAG_RDTUN,
113 &dwc_otg_phy_type, 0, "DWC OTG PHY TYPE - 0/1/2 - ULPI/HSIC/INTERNAL");
114 TUNABLE_INT("hw.usb.dwc_otg.phy_type", &dwc_otg_phy_type);
117 static int dwc_otg_debug;
119 SYSCTL_INT(_hw_usb_dwc_otg, OID_AUTO, debug, CTLFLAG_RW,
120 &dwc_otg_debug, 0, "DWC OTG debug level");
123 #define DWC_OTG_INTR_ENDPT 1
127 struct usb_bus_methods dwc_otg_bus_methods;
128 struct usb_pipe_methods dwc_otg_device_non_isoc_methods;
129 struct usb_pipe_methods dwc_otg_device_isoc_methods;
131 static dwc_otg_cmd_t dwc_otg_setup_rx;
132 static dwc_otg_cmd_t dwc_otg_data_rx;
133 static dwc_otg_cmd_t dwc_otg_data_tx;
134 static dwc_otg_cmd_t dwc_otg_data_tx_sync;
136 static dwc_otg_cmd_t dwc_otg_host_setup_tx;
137 static dwc_otg_cmd_t dwc_otg_host_data_tx;
138 static dwc_otg_cmd_t dwc_otg_host_data_rx;
140 static void dwc_otg_device_done(struct usb_xfer *, usb_error_t);
141 static void dwc_otg_do_poll(struct usb_bus *);
142 static void dwc_otg_standard_done(struct usb_xfer *);
143 static void dwc_otg_root_intr(struct dwc_otg_softc *);
144 static void dwc_otg_interrupt_poll_locked(struct dwc_otg_softc *);
147 * Here is a configuration that the chip supports.
149 static const struct usb_hw_ep_profile dwc_otg_ep_profile[1] = {
152 .max_in_frame_size = 64,/* fixed */
153 .max_out_frame_size = 64, /* fixed */
155 .support_control = 1,
160 dwc_otg_get_hw_ep_profile(struct usb_device *udev,
161 const struct usb_hw_ep_profile **ppf, uint8_t ep_addr)
163 struct dwc_otg_softc *sc;
165 sc = DWC_OTG_BUS2SC(udev->bus);
167 if (ep_addr < sc->sc_dev_ep_max)
168 *ppf = &sc->sc_hw_ep_profile[ep_addr].usb;
174 dwc_otg_write_fifo(struct dwc_otg_softc *sc, struct usb_page_cache *pc,
175 uint32_t offset, uint32_t fifo, uint32_t count)
179 /* round down length to nearest 4-bytes */
182 /* check if we can write the data directly */
183 if (temp != 0 && usb_pc_buffer_is_aligned(pc, offset, temp, 3)) {
184 struct usb_page_search buf_res;
186 /* pre-subtract length */
189 /* iterate buffer list */
191 /* get current buffer pointer */
192 usbd_get_page(pc, offset, &buf_res);
194 if (buf_res.length > temp)
195 buf_res.length = temp;
197 /* transfer data into FIFO */
198 bus_space_write_region_4(sc->sc_io_tag, sc->sc_io_hdl,
199 fifo, buf_res.buffer, buf_res.length / 4);
201 offset += buf_res.length;
202 fifo += buf_res.length;
203 temp -= buf_res.length;
207 /* check for remainder */
209 /* clear topmost word before copy */
210 sc->sc_bounce_buffer[(count - 1) / 4] = 0;
213 usbd_copy_out(pc, offset,
214 sc->sc_bounce_buffer, count);
216 /* transfer data into FIFO */
217 bus_space_write_region_4(sc->sc_io_tag,
218 sc->sc_io_hdl, fifo, sc->sc_bounce_buffer,
224 dwc_otg_read_fifo(struct dwc_otg_softc *sc, struct usb_page_cache *pc,
225 uint32_t offset, uint32_t count)
229 /* round down length to nearest 4-bytes */
232 /* check if we can read the data directly */
233 if (temp != 0 && usb_pc_buffer_is_aligned(pc, offset, temp, 3)) {
234 struct usb_page_search buf_res;
236 /* pre-subtract length */
239 /* iterate buffer list */
241 /* get current buffer pointer */
242 usbd_get_page(pc, offset, &buf_res);
244 if (buf_res.length > temp)
245 buf_res.length = temp;
247 /* transfer data from FIFO */
248 bus_space_read_region_4(sc->sc_io_tag, sc->sc_io_hdl,
249 sc->sc_current_rx_fifo, buf_res.buffer, buf_res.length / 4);
251 offset += buf_res.length;
252 sc->sc_current_rx_fifo += buf_res.length;
253 sc->sc_current_rx_bytes -= buf_res.length;
254 temp -= buf_res.length;
258 /* check for remainder */
260 /* read data into bounce buffer */
261 bus_space_read_region_4(sc->sc_io_tag, sc->sc_io_hdl,
262 sc->sc_current_rx_fifo,
263 sc->sc_bounce_buffer, (count + 3) / 4);
265 /* store data into proper buffer */
266 usbd_copy_in(pc, offset, sc->sc_bounce_buffer, count);
268 /* round length up to nearest 4 bytes */
269 count = (count + 3) & ~3;
271 /* update counters */
272 sc->sc_current_rx_bytes -= count;
273 sc->sc_current_rx_fifo += count;
278 dwc_otg_tx_fifo_reset(struct dwc_otg_softc *sc, uint32_t value)
283 DWC_OTG_WRITE_4(sc, DOTG_GRSTCTL, value);
285 /* wait for reset to complete */
286 for (temp = 0; temp != 16; temp++) {
287 value = DWC_OTG_READ_4(sc, DOTG_GRSTCTL);
288 if (!(value & (GRSTCTL_TXFFLSH | GRSTCTL_RXFFLSH)))
294 dwc_otg_init_fifo(struct dwc_otg_softc *sc, uint8_t mode)
296 struct dwc_otg_profile *pf;
302 fifo_size = sc->sc_fifo_size;
305 * NOTE: Reserved fixed size area at end of RAM, which must
306 * not be allocated to the FIFOs:
310 if (fifo_size < fifo_regs) {
311 DPRINTF("Too little FIFO\n");
315 /* subtract FIFO regs from total once */
316 fifo_size -= fifo_regs;
318 /* split equally for IN and OUT */
321 /* Align to 4 bytes boundary (refer to PGM) */
324 /* set global receive FIFO size */
325 DWC_OTG_WRITE_4(sc, DOTG_GRXFSIZ, fifo_size / 4);
327 tx_start = fifo_size;
329 if (fifo_size < 64) {
330 DPRINTFN(-1, "Not enough data space for EP0 FIFO.\n");
334 if (mode == DWC_MODE_HOST) {
336 /* reset active endpoints */
337 sc->sc_active_rx_ep = 0;
339 /* split equally for periodic and non-periodic */
342 DPRINTF("PTX/NPTX FIFO=%u\n", fifo_size);
344 /* align to 4 bytes boundary */
347 DWC_OTG_WRITE_4(sc, DOTG_GNPTXFSIZ,
348 ((fifo_size / 4) << 16) |
351 tx_start += fifo_size;
353 for (x = 0; x != sc->sc_host_ch_max; x++) {
354 /* enable all host interrupts */
355 DWC_OTG_WRITE_4(sc, DOTG_HCINTMSK(x),
359 DWC_OTG_WRITE_4(sc, DOTG_HPTXFSIZ,
360 ((fifo_size / 4) << 16) |
363 /* reset host channel state */
364 memset(sc->sc_chan_state, 0, sizeof(sc->sc_chan_state));
366 /* enable all host channel interrupts */
367 DWC_OTG_WRITE_4(sc, DOTG_HAINTMSK,
368 (1U << sc->sc_host_ch_max) - 1U);
370 /* enable proper host channel interrupts */
371 sc->sc_irq_mask |= GINTMSK_HCHINTMSK;
372 sc->sc_irq_mask &= ~GINTMSK_IEPINTMSK;
373 DWC_OTG_WRITE_4(sc, DOTG_GINTMSK, sc->sc_irq_mask);
376 if (mode == DWC_MODE_DEVICE) {
378 DWC_OTG_WRITE_4(sc, DOTG_GNPTXFSIZ,
379 (0x10 << 16) | (tx_start / 4));
383 /* setup control endpoint profile */
384 sc->sc_hw_ep_profile[0].usb = dwc_otg_ep_profile[0];
386 /* reset active endpoints */
387 sc->sc_active_rx_ep = 1;
389 for (x = 1; x != sc->sc_dev_ep_max; x++) {
391 pf = sc->sc_hw_ep_profile + x;
393 pf->usb.max_out_frame_size = 1024 * 3;
394 pf->usb.is_simplex = 0; /* assume duplex */
395 pf->usb.support_bulk = 1;
396 pf->usb.support_interrupt = 1;
397 pf->usb.support_isochronous = 1;
398 pf->usb.support_out = 1;
400 if (x < sc->sc_dev_in_ep_max) {
403 limit = (x == 1) ? MIN(DWC_OTG_TX_MAX_FIFO_SIZE,
404 DWC_OTG_MAX_TXN) : MIN(DWC_OTG_MAX_TXN / 2,
405 DWC_OTG_TX_MAX_FIFO_SIZE);
407 /* see if there is enough FIFO space */
408 if (limit <= fifo_size) {
409 pf->max_buffer = limit;
410 pf->usb.support_in = 1;
412 limit = MIN(DWC_OTG_TX_MAX_FIFO_SIZE, 0x40);
413 if (limit <= fifo_size) {
414 pf->usb.support_in = 1;
416 pf->usb.is_simplex = 1;
421 DWC_OTG_WRITE_4(sc, DOTG_DIEPTXF(x),
422 ((limit / 4) << 16) | (tx_start / 4));
425 pf->usb.max_in_frame_size = limit;
427 pf->usb.is_simplex = 1;
430 DPRINTF("FIFO%d = IN:%d / OUT:%d\n", x,
431 pf->usb.max_in_frame_size,
432 pf->usb.max_out_frame_size);
435 /* enable proper device channel interrupts */
436 sc->sc_irq_mask &= ~GINTMSK_HCHINTMSK;
437 sc->sc_irq_mask |= GINTMSK_IEPINTMSK;
438 DWC_OTG_WRITE_4(sc, DOTG_GINTMSK, sc->sc_irq_mask);
442 dwc_otg_tx_fifo_reset(sc, GRSTCTL_RXFFLSH);
444 if (mode != DWC_MODE_OTG) {
445 /* reset all TX FIFOs */
446 dwc_otg_tx_fifo_reset(sc,
447 GRSTCTL_TXFIFO(0x10) |
450 /* reset active endpoints */
451 sc->sc_active_rx_ep = 0;
453 /* reset host channel state */
454 memset(sc->sc_chan_state, 0, sizeof(sc->sc_chan_state));
460 dwc_otg_uses_split(struct usb_device *udev)
463 * When a LOW or FULL speed device is connected directly to
464 * the USB port we don't use split transactions:
466 return (udev->speed != USB_SPEED_HIGH &&
467 udev->parent_hs_hub != NULL &&
468 udev->parent_hs_hub->parent_hub != NULL);
472 dwc_otg_update_host_frame_interval(struct dwc_otg_softc *sc)
476 * Disabled until further. Assuming that the register is already
477 * programmed correctly by the boot loader.
482 /* setup HOST frame interval register, based on existing value */
483 temp = DWC_OTG_READ_4(sc, DOTG_HFIR) & HFIR_FRINT_MASK;
489 /* figure out nearest X-tal value */
497 if (sc->sc_flags.status_high_speed)
502 DPRINTF("HFIR=0x%08x\n", temp);
504 DWC_OTG_WRITE_4(sc, DOTG_HFIR, temp);
509 dwc_otg_clocks_on(struct dwc_otg_softc *sc)
511 if (sc->sc_flags.clocks_off &&
512 sc->sc_flags.port_powered) {
516 /* TODO - platform specific */
518 sc->sc_flags.clocks_off = 0;
523 dwc_otg_clocks_off(struct dwc_otg_softc *sc)
525 if (!sc->sc_flags.clocks_off) {
529 /* TODO - platform specific */
531 sc->sc_flags.clocks_off = 1;
536 dwc_otg_pull_up(struct dwc_otg_softc *sc)
540 /* pullup D+, if possible */
542 if (!sc->sc_flags.d_pulled_up &&
543 sc->sc_flags.port_powered) {
544 sc->sc_flags.d_pulled_up = 1;
546 temp = DWC_OTG_READ_4(sc, DOTG_DCTL);
547 temp &= ~DCTL_SFTDISCON;
548 DWC_OTG_WRITE_4(sc, DOTG_DCTL, temp);
553 dwc_otg_pull_down(struct dwc_otg_softc *sc)
557 /* pulldown D+, if possible */
559 if (sc->sc_flags.d_pulled_up) {
560 sc->sc_flags.d_pulled_up = 0;
562 temp = DWC_OTG_READ_4(sc, DOTG_DCTL);
563 temp |= DCTL_SFTDISCON;
564 DWC_OTG_WRITE_4(sc, DOTG_DCTL, temp);
569 dwc_otg_enable_sof_irq(struct dwc_otg_softc *sc)
571 /* In device mode we don't use the SOF interrupt */
572 if (sc->sc_flags.status_device_mode != 0)
574 /* Ensure the SOF interrupt is not disabled */
576 /* Check if the SOF interrupt is already enabled */
577 if ((sc->sc_irq_mask & GINTMSK_SOFMSK) != 0)
579 sc->sc_irq_mask |= GINTMSK_SOFMSK;
580 DWC_OTG_WRITE_4(sc, DOTG_GINTMSK, sc->sc_irq_mask);
584 dwc_otg_resume_irq(struct dwc_otg_softc *sc)
586 if (sc->sc_flags.status_suspend) {
587 /* update status bits */
588 sc->sc_flags.status_suspend = 0;
589 sc->sc_flags.change_suspend = 1;
591 if (sc->sc_flags.status_device_mode) {
593 * Disable resume interrupt and enable suspend
596 sc->sc_irq_mask &= ~GINTMSK_WKUPINTMSK;
597 sc->sc_irq_mask |= GINTMSK_USBSUSPMSK;
598 DWC_OTG_WRITE_4(sc, DOTG_GINTMSK, sc->sc_irq_mask);
601 /* complete root HUB interrupt endpoint */
602 dwc_otg_root_intr(sc);
607 dwc_otg_suspend_irq(struct dwc_otg_softc *sc)
609 if (!sc->sc_flags.status_suspend) {
610 /* update status bits */
611 sc->sc_flags.status_suspend = 1;
612 sc->sc_flags.change_suspend = 1;
614 if (sc->sc_flags.status_device_mode) {
616 * Disable suspend interrupt and enable resume
619 sc->sc_irq_mask &= ~GINTMSK_USBSUSPMSK;
620 sc->sc_irq_mask |= GINTMSK_WKUPINTMSK;
621 DWC_OTG_WRITE_4(sc, DOTG_GINTMSK, sc->sc_irq_mask);
624 /* complete root HUB interrupt endpoint */
625 dwc_otg_root_intr(sc);
630 dwc_otg_wakeup_peer(struct dwc_otg_softc *sc)
632 if (!sc->sc_flags.status_suspend)
635 DPRINTFN(5, "Remote wakeup\n");
637 if (sc->sc_flags.status_device_mode) {
640 /* enable remote wakeup signalling */
641 temp = DWC_OTG_READ_4(sc, DOTG_DCTL);
642 temp |= DCTL_RMTWKUPSIG;
643 DWC_OTG_WRITE_4(sc, DOTG_DCTL, temp);
645 /* Wait 8ms for remote wakeup to complete. */
646 usb_pause_mtx(&sc->sc_bus.bus_mtx, hz / 125);
648 temp &= ~DCTL_RMTWKUPSIG;
649 DWC_OTG_WRITE_4(sc, DOTG_DCTL, temp);
651 /* enable USB port */
652 DWC_OTG_WRITE_4(sc, DOTG_PCGCCTL, 0);
655 usb_pause_mtx(&sc->sc_bus.bus_mtx, hz / 100);
658 sc->sc_hprt_val |= HPRT_PRTRES;
659 DWC_OTG_WRITE_4(sc, DOTG_HPRT, sc->sc_hprt_val);
661 /* Wait 100ms for resume signalling to complete. */
662 usb_pause_mtx(&sc->sc_bus.bus_mtx, hz / 10);
664 /* clear suspend and resume */
665 sc->sc_hprt_val &= ~(HPRT_PRTSUSP | HPRT_PRTRES);
666 DWC_OTG_WRITE_4(sc, DOTG_HPRT, sc->sc_hprt_val);
669 usb_pause_mtx(&sc->sc_bus.bus_mtx, hz / 250);
672 /* need to fake resume IRQ */
673 dwc_otg_resume_irq(sc);
677 dwc_otg_set_address(struct dwc_otg_softc *sc, uint8_t addr)
681 DPRINTFN(5, "addr=%d\n", addr);
683 temp = DWC_OTG_READ_4(sc, DOTG_DCFG);
684 temp &= ~DCFG_DEVADDR_SET(0x7F);
685 temp |= DCFG_DEVADDR_SET(addr);
686 DWC_OTG_WRITE_4(sc, DOTG_DCFG, temp);
690 dwc_otg_common_rx_ack(struct dwc_otg_softc *sc)
692 DPRINTFN(5, "RX status clear\n");
694 /* enable RX FIFO level interrupt */
695 sc->sc_irq_mask |= GINTMSK_RXFLVLMSK;
696 DWC_OTG_WRITE_4(sc, DOTG_GINTMSK, sc->sc_irq_mask);
698 if (sc->sc_current_rx_bytes != 0) {
699 /* need to dump remaining data */
700 bus_space_read_region_4(sc->sc_io_tag, sc->sc_io_hdl,
701 sc->sc_current_rx_fifo, sc->sc_bounce_buffer,
702 sc->sc_current_rx_bytes / 4);
703 /* clear number of active bytes to receive */
704 sc->sc_current_rx_bytes = 0;
706 /* clear cached status */
707 sc->sc_last_rx_status = 0;
711 dwc_otg_clear_hcint(struct dwc_otg_softc *sc, uint8_t x)
715 /* clear all pending interrupts */
716 hcint = DWC_OTG_READ_4(sc, DOTG_HCINT(x));
717 DWC_OTG_WRITE_4(sc, DOTG_HCINT(x), hcint);
719 /* clear buffered interrupts */
720 sc->sc_chan_state[x].hcint = 0;
724 dwc_otg_host_check_tx_fifo_empty(struct dwc_otg_softc *sc, struct dwc_otg_td *td)
728 temp = DWC_OTG_READ_4(sc, DOTG_GINTSTS);
730 if (td->ep_type == UE_ISOCHRONOUS) {
732 * NOTE: USB INTERRUPT transactions are executed like
733 * USB CONTROL transactions! See the setup standard
734 * chain function for more information.
736 if (!(temp & GINTSTS_PTXFEMP)) {
737 DPRINTF("Periodic TX FIFO is not empty\n");
738 if (!(sc->sc_irq_mask & GINTMSK_PTXFEMPMSK)) {
739 sc->sc_irq_mask |= GINTMSK_PTXFEMPMSK;
740 DWC_OTG_WRITE_4(sc, DOTG_GINTMSK, sc->sc_irq_mask);
742 return (1); /* busy */
745 if (!(temp & GINTSTS_NPTXFEMP)) {
746 DPRINTF("Non-periodic TX FIFO is not empty\n");
747 if (!(sc->sc_irq_mask & GINTMSK_NPTXFEMPMSK)) {
748 sc->sc_irq_mask |= GINTMSK_NPTXFEMPMSK;
749 DWC_OTG_WRITE_4(sc, DOTG_GINTMSK, sc->sc_irq_mask);
751 return (1); /* busy */
754 return (0); /* ready for transmit */
758 dwc_otg_host_channel_alloc(struct dwc_otg_softc *sc,
759 struct dwc_otg_td *td, uint8_t is_out)
765 if (td->channel[0] < DWC_OTG_MAX_CHANNELS)
766 return (0); /* already allocated */
768 /* check if device is suspended */
769 if (DWC_OTG_PC2UDEV(td->pc)->flags.self_suspended != 0)
770 return (1); /* busy - cannot transfer data */
772 /* compute needed TX FIFO size */
774 if (dwc_otg_host_check_tx_fifo_empty(sc, td) != 0)
775 return (1); /* busy - cannot transfer data */
777 z = td->max_packet_count;
778 for (x = y = 0; x != sc->sc_host_ch_max; x++) {
779 /* check if channel is allocated */
780 if (sc->sc_chan_state[x].allocated != 0)
782 /* check if channel is still enabled */
783 if (sc->sc_chan_state[x].wait_halted != 0)
785 /* store channel number */
786 td->channel[y++] = x;
787 /* check if we got all channels */
792 /* reset channel variable */
793 td->channel[0] = DWC_OTG_MAX_CHANNELS;
794 td->channel[1] = DWC_OTG_MAX_CHANNELS;
795 td->channel[2] = DWC_OTG_MAX_CHANNELS;
797 dwc_otg_enable_sof_irq(sc);
798 return (1); /* busy - not enough channels */
801 for (y = 0; y != z; y++) {
805 sc->sc_chan_state[x].allocated = 1;
807 /* set wait halted */
808 sc->sc_chan_state[x].wait_halted = 1;
810 /* clear interrupts */
811 dwc_otg_clear_hcint(sc, x);
813 DPRINTF("CH=%d HCCHAR=0x%08x "
814 "HCSPLT=0x%08x\n", x, td->hcchar, td->hcsplt);
816 /* set active channel */
817 sc->sc_active_rx_ep |= (1 << x);
819 return (0); /* allocated */
823 dwc_otg_host_channel_free_sub(struct dwc_otg_softc *sc, struct dwc_otg_td *td, uint8_t index)
828 if (td->channel[index] >= DWC_OTG_MAX_CHANNELS)
829 return; /* already freed */
832 x = td->channel[index];
833 td->channel[index] = DWC_OTG_MAX_CHANNELS;
835 DPRINTF("CH=%d\n", x);
838 * We need to let programmed host channels run till complete
839 * else the host channel will stop functioning.
841 sc->sc_chan_state[x].allocated = 0;
843 /* ack any pending messages */
844 if (sc->sc_last_rx_status != 0 &&
845 GRXSTSRD_CHNUM_GET(sc->sc_last_rx_status) == x) {
846 dwc_otg_common_rx_ack(sc);
849 /* clear active channel */
850 sc->sc_active_rx_ep &= ~(1 << x);
852 /* check if already halted */
853 if (sc->sc_chan_state[x].wait_halted == 0)
856 /* disable host channel */
857 hcchar = DWC_OTG_READ_4(sc, DOTG_HCCHAR(x));
858 if (hcchar & HCCHAR_CHENA) {
859 DPRINTF("Halting channel %d\n", x);
860 DWC_OTG_WRITE_4(sc, DOTG_HCCHAR(x),
861 hcchar | HCCHAR_CHDIS);
862 /* don't write HCCHAR until the channel is halted */
864 sc->sc_chan_state[x].wait_halted = 0;
869 dwc_otg_host_channel_free(struct dwc_otg_softc *sc, struct dwc_otg_td *td)
872 for (x = 0; x != td->max_packet_count; x++)
873 dwc_otg_host_channel_free_sub(sc, td, x);
877 dwc_otg_host_dump_rx(struct dwc_otg_softc *sc, struct dwc_otg_td *td)
880 /* dump any pending messages */
881 if (sc->sc_last_rx_status == 0)
883 for (x = 0; x != td->max_packet_count; x++) {
884 if (td->channel[x] >= DWC_OTG_MAX_CHANNELS ||
885 td->channel[x] != GRXSTSRD_CHNUM_GET(sc->sc_last_rx_status))
887 dwc_otg_common_rx_ack(sc);
893 dwc_otg_host_setup_tx(struct dwc_otg_softc *sc, struct dwc_otg_td *td)
895 struct usb_device_request req __aligned(4);
900 dwc_otg_host_dump_rx(sc, td);
902 if (td->channel[0] < DWC_OTG_MAX_CHANNELS) {
903 hcint = sc->sc_chan_state[td->channel[0]].hcint;
905 DPRINTF("CH=%d ST=%d HCINT=0x%08x HCCHAR=0x%08x HCTSIZ=0x%08x\n",
906 td->channel[0], td->state, hcint,
907 DWC_OTG_READ_4(sc, DOTG_HCCHAR(td->channel[0])),
908 DWC_OTG_READ_4(sc, DOTG_HCTSIZ(td->channel[0])));
914 if (hcint & (HCINT_RETRY |
915 HCINT_ACK | HCINT_NYET)) {
916 /* give success bits priority over failure bits */
917 } else if (hcint & HCINT_STALL) {
918 DPRINTF("CH=%d STALL\n", td->channel[0]);
922 } else if (hcint & HCINT_ERRORS) {
923 DPRINTF("CH=%d ERROR\n", td->channel[0]);
925 if (td->hcsplt != 0 || td->errcnt >= 3) {
931 if (hcint & (HCINT_ERRORS | HCINT_RETRY |
932 HCINT_ACK | HCINT_NYET)) {
933 if (!(hcint & HCINT_ERRORS))
939 case DWC_CHAN_ST_START:
942 case DWC_CHAN_ST_WAIT_ANE:
943 if (hcint & (HCINT_RETRY | HCINT_ERRORS)) {
945 td->tt_scheduled = 0;
947 } else if (hcint & (HCINT_ACK | HCINT_NYET)) {
948 td->offset += td->tx_bytes;
949 td->remainder -= td->tx_bytes;
951 td->tt_scheduled = 0;
956 case DWC_CHAN_ST_WAIT_S_ANE:
957 if (hcint & (HCINT_RETRY | HCINT_ERRORS)) {
959 td->tt_scheduled = 0;
961 } else if (hcint & (HCINT_ACK | HCINT_NYET)) {
966 case DWC_CHAN_ST_WAIT_C_ANE:
967 if (hcint & HCINT_NYET) {
969 } else if (hcint & (HCINT_RETRY | HCINT_ERRORS)) {
971 td->tt_scheduled = 0;
973 } else if (hcint & HCINT_ACK) {
974 td->offset += td->tx_bytes;
975 td->remainder -= td->tx_bytes;
981 case DWC_CHAN_ST_WAIT_C_PKT:
990 /* free existing channel, if any */
991 dwc_otg_host_channel_free(sc, td);
993 if (sizeof(req) != td->remainder) {
998 if (td->hcsplt != 0) {
999 delta = td->tt_start_slot - sc->sc_last_frame_num - 1;
1000 if (td->tt_scheduled == 0 || delta < DWC_OTG_TT_SLOT_MAX) {
1001 td->state = DWC_CHAN_ST_START;
1004 delta = sc->sc_last_frame_num - td->tt_start_slot;
1007 td->tt_scheduled = 0;
1008 td->state = DWC_CHAN_ST_START;
1013 /* allocate a new channel */
1014 if (dwc_otg_host_channel_alloc(sc, td, 1)) {
1015 td->state = DWC_CHAN_ST_START;
1019 if (td->hcsplt != 0) {
1020 td->hcsplt &= ~HCSPLT_COMPSPLT;
1021 td->state = DWC_CHAN_ST_WAIT_S_ANE;
1023 td->state = DWC_CHAN_ST_WAIT_ANE;
1026 /* copy out control request */
1027 usbd_copy_out(td->pc, 0, &req, sizeof(req));
1029 DWC_OTG_WRITE_4(sc, DOTG_HCTSIZ(td->channel[0]),
1030 (sizeof(req) << HCTSIZ_XFERSIZE_SHIFT) |
1031 (1 << HCTSIZ_PKTCNT_SHIFT) |
1032 (HCTSIZ_PID_SETUP << HCTSIZ_PID_SHIFT));
1034 DWC_OTG_WRITE_4(sc, DOTG_HCSPLT(td->channel[0]), td->hcsplt);
1036 hcchar = td->hcchar;
1037 hcchar &= ~(HCCHAR_EPDIR_IN | HCCHAR_EPTYPE_MASK);
1038 hcchar |= UE_CONTROL << HCCHAR_EPTYPE_SHIFT;
1040 /* must enable channel before writing data to FIFO */
1041 DWC_OTG_WRITE_4(sc, DOTG_HCCHAR(td->channel[0]), hcchar);
1043 /* transfer data into FIFO */
1044 bus_space_write_region_4(sc->sc_io_tag, sc->sc_io_hdl,
1045 DOTG_DFIFO(td->channel[0]), (uint32_t *)&req, sizeof(req) / 4);
1047 /* wait until next slot before trying complete split */
1048 td->tt_complete_slot = sc->sc_last_frame_num + 1;
1050 /* store number of bytes transmitted */
1051 td->tx_bytes = sizeof(req);
1055 /* free existing channel, if any */
1056 dwc_otg_host_channel_free(sc, td);
1058 delta = td->tt_complete_slot - sc->sc_last_frame_num - 1;
1059 if (td->tt_scheduled == 0 || delta < DWC_OTG_TT_SLOT_MAX) {
1060 td->state = DWC_CHAN_ST_WAIT_C_PKT;
1063 delta = sc->sc_last_frame_num - td->tt_start_slot;
1064 if (delta > DWC_OTG_TT_SLOT_MAX) {
1065 /* we missed the service interval */
1066 if (td->ep_type != UE_ISOCHRONOUS)
1070 /* allocate a new channel */
1071 if (dwc_otg_host_channel_alloc(sc, td, 0)) {
1072 td->state = DWC_CHAN_ST_WAIT_C_PKT;
1076 /* wait until next slot before trying complete split */
1077 td->tt_complete_slot = sc->sc_last_frame_num + 1;
1079 td->hcsplt |= HCSPLT_COMPSPLT;
1080 td->state = DWC_CHAN_ST_WAIT_C_ANE;
1082 DWC_OTG_WRITE_4(sc, DOTG_HCTSIZ(td->channel[0]),
1083 (HCTSIZ_PID_SETUP << HCTSIZ_PID_SHIFT));
1085 DWC_OTG_WRITE_4(sc, DOTG_HCSPLT(td->channel[0]), td->hcsplt);
1087 hcchar = td->hcchar;
1088 hcchar &= ~(HCCHAR_EPDIR_IN | HCCHAR_EPTYPE_MASK);
1089 hcchar |= UE_CONTROL << HCCHAR_EPTYPE_SHIFT;
1091 /* must enable channel before writing data to FIFO */
1092 DWC_OTG_WRITE_4(sc, DOTG_HCCHAR(td->channel[0]), hcchar);
1095 return (1); /* busy */
1098 dwc_otg_host_channel_free(sc, td);
1099 return (0); /* complete */
1103 dwc_otg_setup_rx(struct dwc_otg_softc *sc, struct dwc_otg_td *td)
1105 struct usb_device_request req __aligned(4);
1109 /* check endpoint status */
1111 if (sc->sc_last_rx_status == 0)
1114 if (GRXSTSRD_CHNUM_GET(sc->sc_last_rx_status) != 0)
1117 if ((sc->sc_last_rx_status & GRXSTSRD_PKTSTS_MASK) !=
1118 GRXSTSRD_STP_DATA) {
1119 if ((sc->sc_last_rx_status & GRXSTSRD_PKTSTS_MASK) !=
1120 GRXSTSRD_STP_COMPLETE || td->remainder != 0) {
1122 dwc_otg_common_rx_ack(sc);
1126 dwc_otg_common_rx_ack(sc);
1127 return (0); /* complete */
1130 if ((sc->sc_last_rx_status & GRXSTSRD_DPID_MASK) !=
1131 GRXSTSRD_DPID_DATA0) {
1133 dwc_otg_common_rx_ack(sc);
1137 DPRINTFN(5, "GRXSTSR=0x%08x\n", sc->sc_last_rx_status);
1139 /* clear did stall */
1142 /* get the packet byte count */
1143 count = GRXSTSRD_BCNT_GET(sc->sc_last_rx_status);
1145 if (count != sizeof(req)) {
1146 DPRINTFN(0, "Unsupported SETUP packet "
1147 "length, %d bytes\n", count);
1149 dwc_otg_common_rx_ack(sc);
1154 dwc_otg_read_fifo(sc, td->pc, 0, sizeof(req));
1156 /* copy out control request */
1157 usbd_copy_out(td->pc, 0, &req, sizeof(req));
1159 td->offset = sizeof(req);
1162 /* sneak peek the set address */
1163 if ((req.bmRequestType == UT_WRITE_DEVICE) &&
1164 (req.bRequest == UR_SET_ADDRESS)) {
1165 /* must write address before ZLP */
1166 dwc_otg_set_address(sc, req.wValue[0] & 0x7F);
1169 /* don't send any data by default */
1170 DWC_OTG_WRITE_4(sc, DOTG_DIEPTSIZ(0), DIEPCTL_EPDIS);
1171 DWC_OTG_WRITE_4(sc, DOTG_DOEPTSIZ(0), DOEPCTL_EPDIS);
1173 /* reset IN endpoint buffer */
1174 dwc_otg_tx_fifo_reset(sc,
1178 /* acknowledge RX status */
1179 dwc_otg_common_rx_ack(sc);
1183 /* abort any ongoing transfer, before enabling again */
1184 if (!td->did_stall) {
1187 DPRINTFN(5, "stalling IN and OUT direction\n");
1189 temp = sc->sc_out_ctl[0];
1191 /* set stall after enabling endpoint */
1192 DWC_OTG_WRITE_4(sc, DOTG_DOEPCTL(0),
1193 temp | DOEPCTL_STALL);
1195 temp = sc->sc_in_ctl[0];
1197 /* set stall assuming endpoint is enabled */
1198 DWC_OTG_WRITE_4(sc, DOTG_DIEPCTL(0),
1199 temp | DIEPCTL_STALL);
1201 return (1); /* not complete */
1205 dwc_otg_host_rate_check_interrupt(struct dwc_otg_softc *sc, struct dwc_otg_td *td)
1209 delta = sc->sc_tmr_val - td->tmr_val;
1211 return (1); /* busy */
1213 td->tmr_val = sc->sc_tmr_val + td->tmr_res;
1215 /* set toggle, if any */
1216 if (td->set_toggle) {
1224 dwc_otg_host_rate_check(struct dwc_otg_softc *sc, struct dwc_otg_td *td)
1226 uint8_t frame_num = (uint8_t)sc->sc_last_frame_num;
1228 if (td->ep_type == UE_ISOCHRONOUS) {
1229 /* non TT isochronous traffic */
1230 if (frame_num & (td->tmr_res - 1))
1232 if ((frame_num ^ td->tmr_val) & td->tmr_res)
1234 td->tmr_val = td->tmr_res + sc->sc_last_frame_num;
1237 } else if (td->ep_type == UE_INTERRUPT) {
1238 if (!td->tt_scheduled)
1240 td->tt_scheduled = 0;
1242 } else if (td->did_nak != 0) {
1243 /* check if we should pause sending queries for 125us */
1244 if (td->tmr_res == frame_num) {
1246 dwc_otg_enable_sof_irq(sc);
1249 } else if (td->set_toggle) {
1253 /* query for data one more time */
1254 td->tmr_res = frame_num;
1262 dwc_otg_host_data_rx_sub(struct dwc_otg_softc *sc, struct dwc_otg_td *td,
1267 /* check endpoint status */
1268 if (sc->sc_last_rx_status == 0)
1271 if (channel >= DWC_OTG_MAX_CHANNELS)
1274 if (GRXSTSRD_CHNUM_GET(sc->sc_last_rx_status) != channel)
1277 switch (sc->sc_last_rx_status & GRXSTSRD_PKTSTS_MASK) {
1278 case GRXSTSRH_IN_DATA:
1280 DPRINTF("DATA ST=%d STATUS=0x%08x\n",
1281 (int)td->state, (int)sc->sc_last_rx_status);
1283 if (sc->sc_chan_state[channel].hcint & HCINT_SOFTWARE_ONLY) {
1285 * When using SPLIT transactions on interrupt
1286 * endpoints, sometimes data occurs twice.
1288 DPRINTF("Data already received\n");
1292 /* get the packet byte count */
1293 count = GRXSTSRD_BCNT_GET(sc->sc_last_rx_status);
1295 /* check for ISOCHRONOUS endpoint */
1296 if (td->ep_type == UE_ISOCHRONOUS) {
1297 if ((sc->sc_last_rx_status & GRXSTSRD_DPID_MASK) !=
1298 GRXSTSRD_DPID_DATA0) {
1299 /* more data to be received */
1300 td->tt_xactpos = HCSPLT_XACTPOS_MIDDLE;
1302 /* all data received */
1303 td->tt_xactpos = HCSPLT_XACTPOS_BEGIN;
1304 /* verify the packet byte count */
1305 if (count != td->remainder) {
1306 /* we have a short packet */
1312 /* verify the packet byte count */
1313 if (count != td->max_packet_size) {
1314 if (count < td->max_packet_size) {
1315 /* we have a short packet */
1319 /* invalid USB packet */
1323 dwc_otg_common_rx_ack(sc);
1328 td->tt_scheduled = 0;
1331 /* verify the packet byte count */
1332 if (count > td->remainder) {
1333 /* invalid USB packet */
1337 dwc_otg_common_rx_ack(sc);
1341 /* read data from FIFO */
1342 dwc_otg_read_fifo(sc, td->pc, td->offset, count);
1344 td->remainder -= count;
1345 td->offset += count;
1346 sc->sc_chan_state[channel].hcint |= HCINT_SOFTWARE_ONLY;
1352 dwc_otg_common_rx_ack(sc);
1360 dwc_otg_host_data_rx(struct dwc_otg_softc *sc, struct dwc_otg_td *td)
1368 for (x = 0; x != td->max_packet_count; x++) {
1369 channel = td->channel[x];
1370 if (channel >= DWC_OTG_MAX_CHANNELS)
1372 hcint |= sc->sc_chan_state[channel].hcint;
1374 DPRINTF("CH=%d ST=%d HCINT=0x%08x HCCHAR=0x%08x HCTSIZ=0x%08x\n",
1375 channel, td->state, hcint,
1376 DWC_OTG_READ_4(sc, DOTG_HCCHAR(channel)),
1377 DWC_OTG_READ_4(sc, DOTG_HCTSIZ(channel)));
1379 /* check interrupt bits */
1380 if (hcint & (HCINT_RETRY |
1381 HCINT_ACK | HCINT_NYET)) {
1382 /* give success bits priority over failure bits */
1383 } else if (hcint & HCINT_STALL) {
1384 DPRINTF("CH=%d STALL\n", channel);
1385 td->error_stall = 1;
1388 } else if (hcint & HCINT_ERRORS) {
1389 DPRINTF("CH=%d ERROR\n", channel);
1391 if (td->hcsplt != 0 || td->errcnt >= 3) {
1392 if (td->ep_type != UE_ISOCHRONOUS) {
1399 /* check channels for data, if any */
1400 if (dwc_otg_host_data_rx_sub(sc, td, channel))
1403 /* refresh interrupt status */
1404 hcint |= sc->sc_chan_state[channel].hcint;
1406 if (hcint & (HCINT_ERRORS | HCINT_RETRY |
1407 HCINT_ACK | HCINT_NYET)) {
1408 if (!(hcint & HCINT_ERRORS))
1413 switch (td->state) {
1414 case DWC_CHAN_ST_START:
1415 if (td->hcsplt != 0)
1420 case DWC_CHAN_ST_WAIT_ANE:
1421 if (hcint & (HCINT_RETRY | HCINT_ERRORS)) {
1422 if (td->ep_type == UE_INTERRUPT) {
1424 * The USB specification does not
1425 * mandate a particular data toggle
1426 * value for USB INTERRUPT
1427 * transfers. Switch the data toggle
1428 * value to receive the packet
1431 if (hcint & HCINT_DATATGLERR) {
1432 DPRINTF("Retrying packet due to "
1433 "data toggle error\n");
1437 } else if (td->ep_type == UE_ISOCHRONOUS) {
1438 if (td->hcsplt != 0) {
1440 * Sometimes the complete
1441 * split packet may be queued
1443 * transaction translator will
1444 * return a NAK. Ignore
1445 * this message and retry the
1446 * complete split instead.
1448 DPRINTF("Retrying complete split\n");
1454 td->tt_scheduled = 0;
1455 if (td->hcsplt != 0)
1459 } else if (hcint & HCINT_NYET) {
1460 if (td->hcsplt != 0) {
1464 /* not a valid token for IN endpoints */
1468 } else if (hcint & HCINT_ACK) {
1469 /* wait for data - ACK arrived first */
1470 if (!(hcint & HCINT_SOFTWARE_ONLY))
1473 if (td->ep_type == UE_ISOCHRONOUS) {
1474 /* check if we are complete */
1475 if (td->tt_xactpos == HCSPLT_XACTPOS_BEGIN) {
1477 } else if (td->hcsplt != 0) {
1480 /* get more packets */
1484 /* check if we are complete */
1485 if ((td->remainder == 0) || (td->got_short != 0)) {
1490 * Else need to receive a zero length
1494 td->tt_scheduled = 0;
1496 if (td->hcsplt != 0)
1504 case DWC_CHAN_ST_WAIT_S_ANE:
1506 * NOTE: The DWC OTG hardware provides a fake ACK in
1507 * case of interrupt and isochronous transfers:
1509 if (hcint & (HCINT_RETRY | HCINT_ERRORS)) {
1511 td->tt_scheduled = 0;
1513 } else if (hcint & HCINT_NYET) {
1514 td->tt_scheduled = 0;
1516 } else if (hcint & HCINT_ACK) {
1522 case DWC_CHAN_ST_WAIT_C_PKT:
1531 /* free existing channel, if any */
1532 dwc_otg_host_channel_free(sc, td);
1534 if (td->hcsplt != 0) {
1535 delta = td->tt_complete_slot - sc->sc_last_frame_num - 1;
1536 if (td->tt_scheduled == 0 || delta < DWC_OTG_TT_SLOT_MAX) {
1537 if (td->ep_type != UE_ISOCHRONOUS) {
1538 td->state = DWC_CHAN_ST_WAIT_C_PKT;
1542 delta = sc->sc_last_frame_num - td->tt_start_slot;
1543 if (delta > DWC_OTG_TT_SLOT_MAX) {
1544 if (td->ep_type != UE_ISOCHRONOUS) {
1545 /* we missed the service interval */
1550 /* complete split */
1551 td->hcsplt |= HCSPLT_COMPSPLT;
1552 } else if (dwc_otg_host_rate_check(sc, td)) {
1553 td->state = DWC_CHAN_ST_WAIT_C_PKT;
1557 /* allocate a new channel */
1558 if (dwc_otg_host_channel_alloc(sc, td, 0)) {
1559 td->state = DWC_CHAN_ST_WAIT_C_PKT;
1563 /* set toggle, if any */
1564 if (td->set_toggle) {
1569 td->state = DWC_CHAN_ST_WAIT_ANE;
1571 for (x = 0; x != td->max_packet_count; x++) {
1572 channel = td->channel[x];
1574 /* receive one packet */
1575 DWC_OTG_WRITE_4(sc, DOTG_HCTSIZ(channel),
1576 (td->max_packet_size << HCTSIZ_XFERSIZE_SHIFT) |
1577 (1 << HCTSIZ_PKTCNT_SHIFT) |
1578 (td->toggle ? (HCTSIZ_PID_DATA1 << HCTSIZ_PID_SHIFT) :
1579 (HCTSIZ_PID_DATA0 << HCTSIZ_PID_SHIFT)));
1581 DWC_OTG_WRITE_4(sc, DOTG_HCSPLT(channel), td->hcsplt);
1583 hcchar = td->hcchar;
1584 hcchar |= HCCHAR_EPDIR_IN;
1586 if (td->ep_type == UE_ISOCHRONOUS) {
1587 if (td->hcsplt != 0) {
1588 /* continously buffer */
1589 if (sc->sc_last_frame_num & 1)
1590 hcchar &= ~HCCHAR_ODDFRM;
1592 hcchar |= HCCHAR_ODDFRM;
1594 /* multi buffer, if any */
1595 if (sc->sc_last_frame_num & 1)
1596 hcchar |= HCCHAR_ODDFRM;
1598 hcchar &= ~HCCHAR_ODDFRM;
1601 hcchar &= ~HCCHAR_ODDFRM;
1604 /* must enable channel before data can be received */
1605 DWC_OTG_WRITE_4(sc, DOTG_HCCHAR(channel), hcchar);
1607 /* wait until next slot before trying complete split */
1608 td->tt_complete_slot = sc->sc_last_frame_num + 1;
1613 /* free existing channel(s), if any */
1614 dwc_otg_host_channel_free(sc, td);
1616 delta = td->tt_start_slot - sc->sc_last_frame_num - 1;
1617 if (td->tt_scheduled == 0 || delta < DWC_OTG_TT_SLOT_MAX) {
1618 td->state = DWC_CHAN_ST_START;
1621 delta = sc->sc_last_frame_num - td->tt_start_slot;
1624 td->tt_scheduled = 0;
1625 td->state = DWC_CHAN_ST_START;
1629 /* allocate a new channel */
1630 if (dwc_otg_host_channel_alloc(sc, td, 0)) {
1631 td->state = DWC_CHAN_ST_START;
1635 channel = td->channel[0];
1637 td->hcsplt &= ~HCSPLT_COMPSPLT;
1638 td->state = DWC_CHAN_ST_WAIT_S_ANE;
1640 /* receive one packet */
1641 DWC_OTG_WRITE_4(sc, DOTG_HCTSIZ(channel),
1642 (HCTSIZ_PID_DATA0 << HCTSIZ_PID_SHIFT));
1644 DWC_OTG_WRITE_4(sc, DOTG_HCSPLT(channel), td->hcsplt);
1646 /* send after next SOF event */
1647 if ((sc->sc_last_frame_num & 1) == 0 &&
1648 td->ep_type == UE_ISOCHRONOUS)
1649 td->hcchar |= HCCHAR_ODDFRM;
1651 td->hcchar &= ~HCCHAR_ODDFRM;
1653 hcchar = td->hcchar;
1654 hcchar |= HCCHAR_EPDIR_IN;
1656 /* wait until next slot before trying complete split */
1657 td->tt_complete_slot = sc->sc_last_frame_num + 1;
1659 /* must enable channel before data can be received */
1660 DWC_OTG_WRITE_4(sc, DOTG_HCCHAR(channel), hcchar);
1662 return (1); /* busy */
1665 dwc_otg_host_channel_free(sc, td);
1666 return (0); /* complete */
1670 dwc_otg_data_rx(struct dwc_otg_softc *sc, struct dwc_otg_td *td)
1678 /* check endpoint status */
1679 if (sc->sc_last_rx_status == 0)
1682 if (GRXSTSRD_CHNUM_GET(sc->sc_last_rx_status) != td->ep_no)
1685 /* check for SETUP packet */
1686 if ((sc->sc_last_rx_status & GRXSTSRD_PKTSTS_MASK) ==
1687 GRXSTSRD_STP_DATA ||
1688 (sc->sc_last_rx_status & GRXSTSRD_PKTSTS_MASK) ==
1689 GRXSTSRD_STP_COMPLETE) {
1690 if (td->remainder == 0) {
1692 * We are actually complete and have
1693 * received the next SETUP
1695 DPRINTFN(5, "faking complete\n");
1696 return (0); /* complete */
1699 * USB Host Aborted the transfer.
1702 return (0); /* complete */
1705 if ((sc->sc_last_rx_status & GRXSTSRD_PKTSTS_MASK) !=
1706 GRXSTSRD_OUT_DATA) {
1708 dwc_otg_common_rx_ack(sc);
1712 /* get the packet byte count */
1713 count = GRXSTSRD_BCNT_GET(sc->sc_last_rx_status);
1715 /* verify the packet byte count */
1716 if (count != td->max_packet_size) {
1717 if (count < td->max_packet_size) {
1718 /* we have a short packet */
1722 /* invalid USB packet */
1726 dwc_otg_common_rx_ack(sc);
1727 return (0); /* we are complete */
1730 /* verify the packet byte count */
1731 if (count > td->remainder) {
1732 /* invalid USB packet */
1736 dwc_otg_common_rx_ack(sc);
1737 return (0); /* we are complete */
1740 /* read data from FIFO */
1741 dwc_otg_read_fifo(sc, td->pc, td->offset, count);
1743 td->remainder -= count;
1744 td->offset += count;
1747 dwc_otg_common_rx_ack(sc);
1749 temp = sc->sc_out_ctl[td->ep_no];
1751 /* check for isochronous mode */
1752 if ((temp & DIEPCTL_EPTYPE_MASK) ==
1753 (DIEPCTL_EPTYPE_ISOC << DIEPCTL_EPTYPE_SHIFT)) {
1754 /* toggle odd or even frame bit */
1755 if (temp & DIEPCTL_SETD1PID) {
1756 temp &= ~DIEPCTL_SETD1PID;
1757 temp |= DIEPCTL_SETD0PID;
1759 temp &= ~DIEPCTL_SETD0PID;
1760 temp |= DIEPCTL_SETD1PID;
1762 sc->sc_out_ctl[td->ep_no] = temp;
1765 /* check if we are complete */
1766 if ((td->remainder == 0) || got_short) {
1767 if (td->short_pkt) {
1768 /* we are complete */
1771 /* else need to receive a zero length packet */
1776 /* enable SETUP and transfer complete interrupt */
1777 if (td->ep_no == 0) {
1778 DWC_OTG_WRITE_4(sc, DOTG_DOEPTSIZ(0),
1779 DXEPTSIZ_SET_MULTI(3) |
1780 DXEPTSIZ_SET_NPKT(1) |
1781 DXEPTSIZ_SET_NBYTES(td->max_packet_size));
1783 /* allow reception of multiple packets */
1784 DWC_OTG_WRITE_4(sc, DOTG_DOEPTSIZ(td->ep_no),
1785 DXEPTSIZ_SET_MULTI(1) |
1786 DXEPTSIZ_SET_NPKT(4) |
1787 DXEPTSIZ_SET_NBYTES(4 *
1788 ((td->max_packet_size + 3) & ~3)));
1790 temp = sc->sc_out_ctl[td->ep_no];
1791 DWC_OTG_WRITE_4(sc, DOTG_DOEPCTL(td->ep_no), temp |
1792 DOEPCTL_EPENA | DOEPCTL_CNAK);
1794 return (1); /* not complete */
1798 dwc_otg_host_data_tx(struct dwc_otg_softc *sc, struct dwc_otg_td *td)
1807 dwc_otg_host_dump_rx(sc, td);
1809 /* check that last channel is complete */
1810 channel = td->channel[td->npkt];
1812 if (channel < DWC_OTG_MAX_CHANNELS) {
1813 hcint = sc->sc_chan_state[channel].hcint;
1815 DPRINTF("CH=%d ST=%d HCINT=0x%08x HCCHAR=0x%08x HCTSIZ=0x%08x\n",
1816 channel, td->state, hcint,
1817 DWC_OTG_READ_4(sc, DOTG_HCCHAR(channel)),
1818 DWC_OTG_READ_4(sc, DOTG_HCTSIZ(channel)));
1820 if (hcint & (HCINT_RETRY |
1821 HCINT_ACK | HCINT_NYET)) {
1822 /* give success bits priority over failure bits */
1823 } else if (hcint & HCINT_STALL) {
1824 DPRINTF("CH=%d STALL\n", channel);
1825 td->error_stall = 1;
1828 } else if (hcint & HCINT_ERRORS) {
1829 DPRINTF("CH=%d ERROR\n", channel);
1831 if (td->hcsplt != 0 || td->errcnt >= 3) {
1837 if (hcint & (HCINT_ERRORS | HCINT_RETRY |
1838 HCINT_ACK | HCINT_NYET)) {
1840 if (!(hcint & HCINT_ERRORS))
1847 switch (td->state) {
1848 case DWC_CHAN_ST_START:
1851 case DWC_CHAN_ST_WAIT_ANE:
1852 if (hcint & (HCINT_RETRY | HCINT_ERRORS)) {
1854 td->tt_scheduled = 0;
1856 } else if (hcint & (HCINT_ACK | HCINT_NYET)) {
1857 td->offset += td->tx_bytes;
1858 td->remainder -= td->tx_bytes;
1860 /* check if next response will be a NAK */
1861 if (hcint & HCINT_NYET)
1865 td->tt_scheduled = 0;
1867 /* check remainder */
1868 if (td->remainder == 0) {
1873 * Else we need to transmit a short
1881 case DWC_CHAN_ST_WAIT_S_ANE:
1882 if (hcint & (HCINT_RETRY | HCINT_ERRORS)) {
1884 td->tt_scheduled = 0;
1886 } else if (hcint & (HCINT_ACK | HCINT_NYET)) {
1892 case DWC_CHAN_ST_WAIT_C_ANE:
1893 if (hcint & HCINT_NYET) {
1895 } else if (hcint & (HCINT_RETRY | HCINT_ERRORS)) {
1897 td->tt_scheduled = 0;
1899 } else if (hcint & HCINT_ACK) {
1900 td->offset += td->tx_bytes;
1901 td->remainder -= td->tx_bytes;
1904 td->tt_scheduled = 0;
1906 /* check remainder */
1907 if (td->remainder == 0) {
1911 /* else we need to transmit a short packet */
1917 case DWC_CHAN_ST_WAIT_C_PKT:
1920 case DWC_CHAN_ST_TX_WAIT_ISOC:
1921 /* Check if ISOCHRONOUS OUT traffic is complete */
1922 if ((hcint & HCINT_HCH_DONE_MASK) == 0)
1925 td->offset += td->tx_bytes;
1926 td->remainder -= td->tx_bytes;
1934 /* free existing channel(s), if any */
1935 dwc_otg_host_channel_free(sc, td);
1937 if (td->hcsplt != 0) {
1938 delta = td->tt_start_slot - sc->sc_last_frame_num - 1;
1939 if (td->tt_scheduled == 0 || delta < DWC_OTG_TT_SLOT_MAX) {
1940 td->state = DWC_CHAN_ST_START;
1943 delta = sc->sc_last_frame_num - td->tt_start_slot;
1946 td->tt_scheduled = 0;
1947 td->state = DWC_CHAN_ST_START;
1950 } else if (dwc_otg_host_rate_check(sc, td)) {
1951 td->state = DWC_CHAN_ST_START;
1955 /* allocate a new channel */
1956 if (dwc_otg_host_channel_alloc(sc, td, 1)) {
1957 td->state = DWC_CHAN_ST_START;
1961 /* set toggle, if any */
1962 if (td->set_toggle) {
1967 if (td->ep_type == UE_ISOCHRONOUS) {
1968 /* ISOCHRONOUS OUT transfers don't have any ACKs */
1969 td->state = DWC_CHAN_ST_TX_WAIT_ISOC;
1970 td->hcsplt &= ~HCSPLT_COMPSPLT;
1971 if (td->hcsplt != 0) {
1972 /* get maximum transfer length */
1973 count = td->remainder;
1974 if (count > HCSPLT_XACTLEN_BURST) {
1975 DPRINTF("TT overflow\n");
1979 /* Update transaction position */
1980 td->hcsplt &= ~HCSPLT_XACTPOS_MASK;
1981 td->hcsplt |= (HCSPLT_XACTPOS_ALL << HCSPLT_XACTPOS_SHIFT);
1983 } else if (td->hcsplt != 0) {
1984 td->hcsplt &= ~HCSPLT_COMPSPLT;
1985 /* Wait for ACK/NAK/ERR from TT */
1986 td->state = DWC_CHAN_ST_WAIT_S_ANE;
1988 /* Wait for ACK/NAK/STALL from device */
1989 td->state = DWC_CHAN_ST_WAIT_ANE;
1994 for (x = 0; x != td->max_packet_count; x++) {
1997 channel = td->channel[x];
1999 /* send one packet at a time */
2000 count = td->max_packet_size;
2001 rem_bytes = td->remainder - td->tx_bytes;
2002 if (rem_bytes < count) {
2003 /* we have a short packet */
2007 if (count == rem_bytes) {
2011 DWC_OTG_WRITE_4(sc, DOTG_HCTSIZ(channel),
2012 (count << HCTSIZ_XFERSIZE_SHIFT) |
2013 (1 << HCTSIZ_PKTCNT_SHIFT) |
2014 (td->toggle ? (HCTSIZ_PID_DATA1 << HCTSIZ_PID_SHIFT) :
2015 (HCTSIZ_PID_DATA0 << HCTSIZ_PID_SHIFT)));
2018 DWC_OTG_WRITE_4(sc, DOTG_HCTSIZ(channel),
2019 (count << HCTSIZ_XFERSIZE_SHIFT) |
2020 (1 << HCTSIZ_PKTCNT_SHIFT) |
2021 (HCTSIZ_PID_DATA1 << HCTSIZ_PID_SHIFT));
2024 DWC_OTG_WRITE_4(sc, DOTG_HCTSIZ(channel),
2025 (count << HCTSIZ_XFERSIZE_SHIFT) |
2026 (1 << HCTSIZ_PKTCNT_SHIFT) |
2027 (HCTSIZ_PID_DATA2 << HCTSIZ_PID_SHIFT));
2030 } else if (td->ep_type == UE_ISOCHRONOUS &&
2031 td->max_packet_count > 1) {
2032 /* ISOCHRONOUS multi packet */
2033 DWC_OTG_WRITE_4(sc, DOTG_HCTSIZ(channel),
2034 (count << HCTSIZ_XFERSIZE_SHIFT) |
2035 (1 << HCTSIZ_PKTCNT_SHIFT) |
2036 (HCTSIZ_PID_MDATA << HCTSIZ_PID_SHIFT));
2038 /* TODO: HCTSIZ_DOPNG */
2039 /* standard BULK/INTERRUPT/CONTROL packet */
2040 DWC_OTG_WRITE_4(sc, DOTG_HCTSIZ(channel),
2041 (count << HCTSIZ_XFERSIZE_SHIFT) |
2042 (1 << HCTSIZ_PKTCNT_SHIFT) |
2043 (td->toggle ? (HCTSIZ_PID_DATA1 << HCTSIZ_PID_SHIFT) :
2044 (HCTSIZ_PID_DATA0 << HCTSIZ_PID_SHIFT)));
2047 DWC_OTG_WRITE_4(sc, DOTG_HCSPLT(channel), td->hcsplt);
2049 hcchar = td->hcchar;
2050 hcchar &= ~HCCHAR_EPDIR_IN;
2052 /* send after next SOF event */
2053 if ((sc->sc_last_frame_num & 1) == 0 &&
2054 td->ep_type == UE_ISOCHRONOUS)
2055 hcchar |= HCCHAR_ODDFRM;
2057 hcchar &= ~HCCHAR_ODDFRM;
2059 /* must enable before writing data to FIFO */
2060 DWC_OTG_WRITE_4(sc, DOTG_HCCHAR(channel), hcchar);
2063 /* write data into FIFO */
2064 dwc_otg_write_fifo(sc, td->pc, td->offset +
2065 td->tx_bytes, DOTG_DFIFO(channel), count);
2068 /* store number of bytes transmitted */
2069 td->tx_bytes += count;
2071 /* store last packet index */
2074 /* check for last packet */
2075 if (count == rem_bytes)
2081 /* free existing channel, if any */
2082 dwc_otg_host_channel_free(sc, td);
2084 delta = td->tt_complete_slot - sc->sc_last_frame_num - 1;
2085 if (td->tt_scheduled == 0 || delta < DWC_OTG_TT_SLOT_MAX) {
2086 td->state = DWC_CHAN_ST_WAIT_C_PKT;
2089 delta = sc->sc_last_frame_num - td->tt_start_slot;
2090 if (delta > DWC_OTG_TT_SLOT_MAX) {
2091 /* we missed the service interval */
2092 if (td->ep_type != UE_ISOCHRONOUS)
2097 /* allocate a new channel */
2098 if (dwc_otg_host_channel_alloc(sc, td, 0)) {
2099 td->state = DWC_CHAN_ST_WAIT_C_PKT;
2103 channel = td->channel[0];
2105 td->hcsplt |= HCSPLT_COMPSPLT;
2106 td->state = DWC_CHAN_ST_WAIT_C_ANE;
2108 DWC_OTG_WRITE_4(sc, DOTG_HCTSIZ(channel),
2109 (HCTSIZ_PID_DATA0 << HCTSIZ_PID_SHIFT));
2111 DWC_OTG_WRITE_4(sc, DOTG_HCSPLT(channel), td->hcsplt);
2113 hcchar = td->hcchar;
2114 hcchar &= ~HCCHAR_EPDIR_IN;
2116 /* receive complete split ASAP */
2117 if ((sc->sc_last_frame_num & 1) != 0 &&
2118 td->ep_type == UE_ISOCHRONOUS)
2119 hcchar |= HCCHAR_ODDFRM;
2121 hcchar &= ~HCCHAR_ODDFRM;
2123 /* must enable channel before data can be received */
2124 DWC_OTG_WRITE_4(sc, DOTG_HCCHAR(channel), hcchar);
2126 /* wait until next slot before trying complete split */
2127 td->tt_complete_slot = sc->sc_last_frame_num + 1;
2129 return (1); /* busy */
2132 dwc_otg_host_channel_free(sc, td);
2133 return (0); /* complete */
2137 dwc_otg_data_tx(struct dwc_otg_softc *sc, struct dwc_otg_td *td)
2139 uint32_t max_buffer;
2146 to = 3; /* don't loop forever! */
2148 max_buffer = sc->sc_hw_ep_profile[td->ep_no].max_buffer;
2151 /* check for for endpoint 0 data */
2153 temp = sc->sc_last_rx_status;
2155 if ((td->ep_no == 0) && (temp != 0) &&
2156 (GRXSTSRD_CHNUM_GET(temp) == 0)) {
2158 if ((temp & GRXSTSRD_PKTSTS_MASK) !=
2159 GRXSTSRD_STP_DATA &&
2160 (temp & GRXSTSRD_PKTSTS_MASK) !=
2161 GRXSTSRD_STP_COMPLETE) {
2163 /* dump data - wrong direction */
2164 dwc_otg_common_rx_ack(sc);
2167 * The current transfer was cancelled
2171 return (0); /* complete */
2175 /* fill in more TX data, if possible */
2176 if (td->tx_bytes != 0) {
2180 /* check if packets have been transferred */
2181 temp = DWC_OTG_READ_4(sc, DOTG_DIEPTSIZ(td->ep_no));
2183 /* get current packet number */
2184 cpkt = DXEPTSIZ_GET_NPKT(temp);
2186 if (cpkt >= td->npkt) {
2189 if (max_buffer != 0) {
2190 fifo_left = (td->npkt - cpkt) *
2191 td->max_packet_size;
2193 if (fifo_left > max_buffer)
2194 fifo_left = max_buffer;
2196 fifo_left = td->max_packet_size;
2200 count = td->tx_bytes;
2201 if (count > fifo_left)
2205 /* write data into FIFO */
2206 dwc_otg_write_fifo(sc, td->pc, td->offset,
2207 DOTG_DFIFO(td->ep_no), count);
2209 td->tx_bytes -= count;
2210 td->remainder -= count;
2211 td->offset += count;
2214 if (td->tx_bytes != 0)
2217 /* check remainder */
2218 if (td->remainder == 0) {
2220 return (0); /* complete */
2222 /* else we need to transmit a short packet */
2229 /* check if not all packets have been transferred */
2230 temp = DWC_OTG_READ_4(sc, DOTG_DIEPTSIZ(td->ep_no));
2232 if (DXEPTSIZ_GET_NPKT(temp) != 0) {
2234 DPRINTFN(5, "busy ep=%d npkt=%d DIEPTSIZ=0x%08x "
2235 "DIEPCTL=0x%08x\n", td->ep_no,
2236 DXEPTSIZ_GET_NPKT(temp),
2237 temp, DWC_OTG_READ_4(sc, DOTG_DIEPCTL(td->ep_no)));
2242 DPRINTFN(5, "rem=%u ep=%d\n", td->remainder, td->ep_no);
2244 /* try to optimise by sending more data */
2245 if ((max_buffer != 0) && ((td->max_packet_size & 3) == 0)) {
2247 /* send multiple packets at the same time */
2248 mpkt = max_buffer / td->max_packet_size;
2253 count = td->remainder;
2254 if (count > 0x7FFFFF)
2255 count = 0x7FFFFF - (0x7FFFFF % td->max_packet_size);
2257 td->npkt = count / td->max_packet_size;
2260 * NOTE: We could use 0x3FE instead of "mpkt" in the
2261 * check below to get more throughput, but then we
2262 * have a dependency towards non-generic chip features
2263 * to disable the TX-FIFO-EMPTY interrupts on a per
2264 * endpoint basis. Increase the maximum buffer size of
2265 * the IN endpoint to increase the performance.
2267 if (td->npkt > mpkt) {
2269 count = td->max_packet_size * mpkt;
2270 } else if ((count == 0) || (count % td->max_packet_size)) {
2271 /* we are transmitting a short packet */
2276 /* send one packet at a time */
2278 count = td->max_packet_size;
2279 if (td->remainder < count) {
2280 /* we have a short packet */
2282 count = td->remainder;
2286 DWC_OTG_WRITE_4(sc, DOTG_DIEPTSIZ(td->ep_no),
2287 DXEPTSIZ_SET_MULTI(1) |
2288 DXEPTSIZ_SET_NPKT(td->npkt) |
2289 DXEPTSIZ_SET_NBYTES(count));
2291 /* make room for buffering */
2294 temp = sc->sc_in_ctl[td->ep_no];
2296 /* check for isochronous mode */
2297 if ((temp & DIEPCTL_EPTYPE_MASK) ==
2298 (DIEPCTL_EPTYPE_ISOC << DIEPCTL_EPTYPE_SHIFT)) {
2299 /* toggle odd or even frame bit */
2300 if (temp & DIEPCTL_SETD1PID) {
2301 temp &= ~DIEPCTL_SETD1PID;
2302 temp |= DIEPCTL_SETD0PID;
2304 temp &= ~DIEPCTL_SETD0PID;
2305 temp |= DIEPCTL_SETD1PID;
2307 sc->sc_in_ctl[td->ep_no] = temp;
2310 /* must enable before writing data to FIFO */
2311 DWC_OTG_WRITE_4(sc, DOTG_DIEPCTL(td->ep_no), temp |
2312 DIEPCTL_EPENA | DIEPCTL_CNAK);
2314 td->tx_bytes = count;
2316 /* check remainder */
2317 if (td->tx_bytes == 0 &&
2318 td->remainder == 0) {
2320 return (0); /* complete */
2322 /* else we need to transmit a short packet */
2327 return (1); /* not complete */
2331 dwc_otg_data_tx_sync(struct dwc_otg_softc *sc, struct dwc_otg_td *td)
2336 * If all packets are transferred we are complete:
2338 temp = DWC_OTG_READ_4(sc, DOTG_DIEPTSIZ(td->ep_no));
2340 /* check that all packets have been transferred */
2341 if (DXEPTSIZ_GET_NPKT(temp) != 0) {
2342 DPRINTFN(5, "busy ep=%d\n", td->ep_no);
2349 /* we only want to know if there is a SETUP packet or free IN packet */
2351 temp = sc->sc_last_rx_status;
2353 if ((td->ep_no == 0) && (temp != 0) &&
2354 (GRXSTSRD_CHNUM_GET(temp) == 0)) {
2356 if ((temp & GRXSTSRD_PKTSTS_MASK) ==
2357 GRXSTSRD_STP_DATA ||
2358 (temp & GRXSTSRD_PKTSTS_MASK) ==
2359 GRXSTSRD_STP_COMPLETE) {
2360 DPRINTFN(5, "faking complete\n");
2362 * Race condition: We are complete!
2366 /* dump data - wrong direction */
2367 dwc_otg_common_rx_ack(sc);
2370 return (1); /* not complete */
2374 dwc_otg_xfer_do_fifo(struct dwc_otg_softc *sc, struct usb_xfer *xfer)
2376 struct dwc_otg_td *td;
2383 td = xfer->td_transfer_cache;
2388 if ((td->func) (sc, td)) {
2389 /* operation in progress */
2392 if (((void *)td) == xfer->td_transfer_last) {
2395 if (td->error_any) {
2397 } else if (td->remainder > 0) {
2399 * We had a short transfer. If there is no alternate
2400 * next, stop processing !
2407 * Fetch the next transfer descriptor and transfer
2408 * some flags to the next transfer descriptor
2410 tmr_res = td->tmr_res;
2411 tmr_val = td->tmr_val;
2412 toggle = td->toggle;
2414 xfer->td_transfer_cache = td;
2415 td->toggle = toggle; /* transfer toggle */
2416 td->tmr_res = tmr_res;
2417 td->tmr_val = tmr_val;
2422 xfer->td_transfer_cache = NULL;
2423 sc->sc_xfer_complete = 1;
2427 dwc_otg_xfer_do_complete_locked(struct dwc_otg_softc *sc, struct usb_xfer *xfer)
2429 struct dwc_otg_td *td;
2433 td = xfer->td_transfer_cache;
2435 /* compute all actual lengths */
2436 dwc_otg_standard_done(xfer);
2443 dwc_otg_timer(void *_sc)
2445 struct dwc_otg_softc *sc = _sc;
2447 USB_BUS_LOCK_ASSERT(&sc->sc_bus, MA_OWNED);
2451 USB_BUS_SPIN_LOCK(&sc->sc_bus);
2453 /* increment timer value */
2456 /* enable SOF interrupt, which will poll jobs */
2457 dwc_otg_enable_sof_irq(sc);
2459 USB_BUS_SPIN_UNLOCK(&sc->sc_bus);
2461 if (sc->sc_timer_active) {
2463 usb_callout_reset(&sc->sc_timer,
2464 hz / (1000 / DWC_OTG_HOST_TIMER_RATE),
2465 &dwc_otg_timer, sc);
2470 dwc_otg_timer_start(struct dwc_otg_softc *sc)
2472 if (sc->sc_timer_active != 0)
2475 sc->sc_timer_active = 1;
2478 usb_callout_reset(&sc->sc_timer,
2479 hz / (1000 / DWC_OTG_HOST_TIMER_RATE),
2480 &dwc_otg_timer, sc);
2484 dwc_otg_timer_stop(struct dwc_otg_softc *sc)
2486 if (sc->sc_timer_active == 0)
2489 sc->sc_timer_active = 0;
2492 usb_callout_stop(&sc->sc_timer);
2496 dwc_otg_compute_isoc_rx_tt_slot(struct dwc_otg_tt_info *pinfo)
2498 if (pinfo->slot_index < DWC_OTG_TT_SLOT_MAX)
2499 pinfo->slot_index++;
2500 return (pinfo->slot_index);
2504 dwc_otg_update_host_transfer_schedule_locked(struct dwc_otg_softc *sc)
2506 TAILQ_HEAD(, usb_xfer) head;
2507 struct usb_xfer *xfer;
2508 struct usb_xfer *xfer_next;
2509 struct dwc_otg_td *td;
2513 temp = DWC_OTG_READ_4(sc, DOTG_HFNUM) & DWC_OTG_FRAME_MASK;
2515 if (sc->sc_last_frame_num == temp)
2518 sc->sc_last_frame_num = temp;
2522 if ((temp & 7) == 0) {
2524 /* reset the schedule */
2525 memset(sc->sc_tt_info, 0, sizeof(sc->sc_tt_info));
2527 TAILQ_FOREACH_SAFE(xfer, &sc->sc_bus.intr_q.head, wait_entry, xfer_next) {
2528 td = xfer->td_transfer_cache;
2529 if (td == NULL || td->ep_type != UE_ISOCHRONOUS)
2532 /* check for IN direction */
2533 if ((td->hcchar & HCCHAR_EPDIR_IN) != 0)
2538 if (td->hcsplt == 0 || td->tt_scheduled != 0)
2542 slot = dwc_otg_compute_isoc_rx_tt_slot(
2543 sc->sc_tt_info + td->tt_index);
2546 * Not enough time to get complete
2552 td->tt_start_slot = temp + slot;
2553 td->tt_scheduled = 1;
2554 TAILQ_REMOVE(&sc->sc_bus.intr_q.head, xfer, wait_entry);
2555 TAILQ_INSERT_TAIL(&head, xfer, wait_entry);
2558 TAILQ_FOREACH_SAFE(xfer, &sc->sc_bus.intr_q.head, wait_entry, xfer_next) {
2559 td = xfer->td_transfer_cache;
2560 if (td == NULL || td->ep_type != UE_ISOCHRONOUS)
2563 /* check for OUT direction */
2564 if ((td->hcchar & HCCHAR_EPDIR_IN) == 0)
2569 if (td->hcsplt == 0 || td->tt_scheduled != 0)
2573 td->tt_start_slot = temp;
2574 td->tt_scheduled = 1;
2575 TAILQ_REMOVE(&sc->sc_bus.intr_q.head, xfer, wait_entry);
2576 TAILQ_INSERT_TAIL(&head, xfer, wait_entry);
2579 TAILQ_FOREACH_SAFE(xfer, &sc->sc_bus.intr_q.head, wait_entry, xfer_next) {
2580 td = xfer->td_transfer_cache;
2581 if (td == NULL || td->ep_type != UE_INTERRUPT)
2584 if (td->tt_scheduled != 0) {
2589 if (dwc_otg_host_rate_check_interrupt(sc, td))
2592 if (td->hcsplt == 0) {
2594 td->tt_scheduled = 1;
2599 td->tt_start_slot = temp;
2601 td->tt_scheduled = 1;
2602 TAILQ_REMOVE(&sc->sc_bus.intr_q.head, xfer, wait_entry);
2603 TAILQ_INSERT_TAIL(&head, xfer, wait_entry);
2606 TAILQ_FOREACH_SAFE(xfer, &sc->sc_bus.intr_q.head, wait_entry, xfer_next) {
2607 td = xfer->td_transfer_cache;
2609 td->ep_type != UE_CONTROL) {
2615 if (td->hcsplt == 0 || td->tt_scheduled != 0)
2619 td->tt_start_slot = temp;
2620 td->tt_scheduled = 1;
2621 TAILQ_REMOVE(&sc->sc_bus.intr_q.head, xfer, wait_entry);
2622 TAILQ_INSERT_TAIL(&head, xfer, wait_entry);
2625 if ((temp & 7) < 6) {
2626 TAILQ_FOREACH_SAFE(xfer, &sc->sc_bus.intr_q.head, wait_entry, xfer_next) {
2627 td = xfer->td_transfer_cache;
2629 td->ep_type != UE_BULK) {
2635 if (td->hcsplt == 0 || td->tt_scheduled != 0)
2639 td->tt_start_slot = temp;
2640 td->tt_scheduled = 1;
2641 TAILQ_REMOVE(&sc->sc_bus.intr_q.head, xfer, wait_entry);
2642 TAILQ_INSERT_TAIL(&head, xfer, wait_entry);
2646 /* Put TT transfers in execution order at the end */
2647 TAILQ_CONCAT(&sc->sc_bus.intr_q.head, &head, wait_entry);
2649 /* move all TT transfers in front, keeping the current order */
2650 TAILQ_FOREACH_SAFE(xfer, &sc->sc_bus.intr_q.head, wait_entry, xfer_next) {
2651 td = xfer->td_transfer_cache;
2652 if (td == NULL || td->hcsplt == 0)
2654 TAILQ_REMOVE(&sc->sc_bus.intr_q.head, xfer, wait_entry);
2655 TAILQ_INSERT_TAIL(&head, xfer, wait_entry);
2657 TAILQ_CONCAT(&head, &sc->sc_bus.intr_q.head, wait_entry);
2658 TAILQ_CONCAT(&sc->sc_bus.intr_q.head, &head, wait_entry);
2660 /* put non-TT non-ISOCHRONOUS transfers last */
2661 TAILQ_FOREACH_SAFE(xfer, &sc->sc_bus.intr_q.head, wait_entry, xfer_next) {
2662 td = xfer->td_transfer_cache;
2663 if (td == NULL || td->hcsplt != 0 || td->ep_type == UE_ISOCHRONOUS)
2665 TAILQ_REMOVE(&sc->sc_bus.intr_q.head, xfer, wait_entry);
2666 TAILQ_INSERT_TAIL(&head, xfer, wait_entry);
2668 TAILQ_CONCAT(&sc->sc_bus.intr_q.head, &head, wait_entry);
2670 if ((temp & 7) == 0) {
2672 DPRINTFN(12, "SOF interrupt #%d, needsof=%d\n",
2673 (int)temp, (int)sc->sc_needsof);
2675 /* update SOF IRQ mask */
2676 if (sc->sc_irq_mask & GINTMSK_SOFMSK) {
2677 if (sc->sc_needsof == 0) {
2678 sc->sc_irq_mask &= ~GINTMSK_SOFMSK;
2679 DWC_OTG_WRITE_4(sc, DOTG_GINTMSK, sc->sc_irq_mask);
2682 if (sc->sc_needsof != 0) {
2683 sc->sc_irq_mask |= GINTMSK_SOFMSK;
2684 DWC_OTG_WRITE_4(sc, DOTG_GINTMSK, sc->sc_irq_mask);
2688 /* clear need SOF flag */
2695 dwc_otg_interrupt_poll_locked(struct dwc_otg_softc *sc)
2697 struct usb_xfer *xfer;
2701 uint8_t got_rx_status;
2704 if (sc->sc_flags.status_device_mode == 0) {
2706 * Update host transfer schedule, so that new
2707 * transfers can be issued:
2709 dwc_otg_update_host_transfer_schedule_locked(sc);
2713 if (++count == 16) {
2714 /* give other interrupts a chance */
2719 /* get all host channel interrupts */
2720 haint = DWC_OTG_READ_4(sc, DOTG_HAINT);
2723 if (x >= sc->sc_host_ch_max)
2725 temp = DWC_OTG_READ_4(sc, DOTG_HCINT(x));
2726 DWC_OTG_WRITE_4(sc, DOTG_HCINT(x), temp);
2727 temp &= ~HCINT_SOFTWARE_ONLY;
2728 sc->sc_chan_state[x].hcint |= temp;
2729 haint &= ~(1U << x);
2732 if (sc->sc_last_rx_status == 0) {
2734 temp = DWC_OTG_READ_4(sc, DOTG_GINTSTS);
2735 if (temp & GINTSTS_RXFLVL) {
2736 /* pop current status */
2737 sc->sc_last_rx_status =
2738 DWC_OTG_READ_4(sc, DOTG_GRXSTSPD);
2741 if (sc->sc_last_rx_status != 0) {
2745 temp = sc->sc_last_rx_status &
2746 GRXSTSRD_PKTSTS_MASK;
2748 /* non-data messages we simply skip */
2749 if (temp != GRXSTSRD_STP_DATA &&
2750 temp != GRXSTSRD_STP_COMPLETE &&
2751 temp != GRXSTSRD_OUT_DATA) {
2752 /* check for halted channel */
2753 if (temp == GRXSTSRH_HALTED) {
2754 ep_no = GRXSTSRD_CHNUM_GET(sc->sc_last_rx_status);
2755 sc->sc_chan_state[ep_no].wait_halted = 0;
2756 DPRINTFN(5, "channel halt complete ch=%u\n", ep_no);
2758 /* store bytes and FIFO offset */
2759 sc->sc_current_rx_bytes = 0;
2760 sc->sc_current_rx_fifo = 0;
2762 /* acknowledge status */
2763 dwc_otg_common_rx_ack(sc);
2767 temp = GRXSTSRD_BCNT_GET(
2768 sc->sc_last_rx_status);
2769 ep_no = GRXSTSRD_CHNUM_GET(
2770 sc->sc_last_rx_status);
2772 /* store bytes and FIFO offset */
2773 sc->sc_current_rx_bytes = (temp + 3) & ~3;
2774 sc->sc_current_rx_fifo = DOTG_DFIFO(ep_no);
2776 DPRINTF("Reading %d bytes from ep %d\n", temp, ep_no);
2778 /* check if we should dump the data */
2779 if (!(sc->sc_active_rx_ep & (1U << ep_no))) {
2780 dwc_otg_common_rx_ack(sc);
2786 DPRINTFN(5, "RX status = 0x%08x: ch=%d pid=%d bytes=%d sts=%d\n",
2787 sc->sc_last_rx_status, ep_no,
2788 (sc->sc_last_rx_status >> 15) & 3,
2789 GRXSTSRD_BCNT_GET(sc->sc_last_rx_status),
2790 (sc->sc_last_rx_status >> 17) & 15);
2797 ep_no = GRXSTSRD_CHNUM_GET(
2798 sc->sc_last_rx_status);
2800 /* check if we should dump the data */
2801 if (!(sc->sc_active_rx_ep & (1U << ep_no))) {
2802 dwc_otg_common_rx_ack(sc);
2810 TAILQ_FOREACH(xfer, &sc->sc_bus.intr_q.head, wait_entry)
2811 dwc_otg_xfer_do_fifo(sc, xfer);
2813 if (got_rx_status) {
2814 /* check if data was consumed */
2815 if (sc->sc_last_rx_status == 0)
2818 /* disable RX FIFO level interrupt */
2819 sc->sc_irq_mask &= ~GINTMSK_RXFLVLMSK;
2820 DWC_OTG_WRITE_4(sc, DOTG_GINTMSK, sc->sc_irq_mask);
2825 dwc_otg_interrupt_complete_locked(struct dwc_otg_softc *sc)
2827 struct usb_xfer *xfer;
2829 /* scan for completion events */
2830 TAILQ_FOREACH(xfer, &sc->sc_bus.intr_q.head, wait_entry) {
2831 if (dwc_otg_xfer_do_complete_locked(sc, xfer))
2837 dwc_otg_vbus_interrupt(struct dwc_otg_softc *sc, uint8_t is_on)
2839 DPRINTFN(5, "vbus = %u\n", is_on);
2842 if (!sc->sc_flags.status_vbus) {
2843 sc->sc_flags.status_vbus = 1;
2845 /* complete root HUB interrupt endpoint */
2847 dwc_otg_root_intr(sc);
2850 if (sc->sc_flags.status_vbus) {
2851 sc->sc_flags.status_vbus = 0;
2852 sc->sc_flags.status_bus_reset = 0;
2853 sc->sc_flags.status_suspend = 0;
2854 sc->sc_flags.change_suspend = 0;
2855 sc->sc_flags.change_connect = 1;
2857 /* complete root HUB interrupt endpoint */
2859 dwc_otg_root_intr(sc);
2865 dwc_otg_filter_interrupt(void *arg)
2867 struct dwc_otg_softc *sc = arg;
2868 int retval = FILTER_HANDLED;
2871 USB_BUS_SPIN_LOCK(&sc->sc_bus);
2873 /* read and clear interrupt status */
2874 status = DWC_OTG_READ_4(sc, DOTG_GINTSTS);
2876 /* clear interrupts we are handling here */
2877 DWC_OTG_WRITE_4(sc, DOTG_GINTSTS, status & ~DWC_OTG_MSK_GINT_THREAD_IRQ);
2879 /* check for USB state change interrupts */
2880 if ((status & DWC_OTG_MSK_GINT_THREAD_IRQ) != 0)
2881 retval = FILTER_SCHEDULE_THREAD;
2883 /* clear FIFO empty interrupts */
2884 if (status & sc->sc_irq_mask &
2885 (GINTSTS_PTXFEMP | GINTSTS_NPTXFEMP)) {
2886 sc->sc_irq_mask &= ~(GINTSTS_PTXFEMP | GINTSTS_NPTXFEMP);
2887 DWC_OTG_WRITE_4(sc, DOTG_GINTMSK, sc->sc_irq_mask);
2889 /* clear all IN endpoint interrupts */
2890 if (status & GINTSTS_IEPINT) {
2894 for (x = 0; x != sc->sc_dev_in_ep_max; x++) {
2895 temp = DWC_OTG_READ_4(sc, DOTG_DIEPINT(x));
2897 * NOTE: Need to clear all interrupt bits,
2898 * because some appears to be unmaskable and
2899 * can cause an interrupt loop:
2902 DWC_OTG_WRITE_4(sc, DOTG_DIEPINT(x), temp);
2906 /* poll FIFOs, if any */
2907 dwc_otg_interrupt_poll_locked(sc);
2909 if (sc->sc_xfer_complete != 0)
2910 retval = FILTER_SCHEDULE_THREAD;
2912 USB_BUS_SPIN_UNLOCK(&sc->sc_bus);
2918 dwc_otg_interrupt(void *arg)
2920 struct dwc_otg_softc *sc = arg;
2923 USB_BUS_LOCK(&sc->sc_bus);
2924 USB_BUS_SPIN_LOCK(&sc->sc_bus);
2926 /* read and clear interrupt status */
2927 status = DWC_OTG_READ_4(sc, DOTG_GINTSTS);
2929 /* clear interrupts we are handling here */
2930 DWC_OTG_WRITE_4(sc, DOTG_GINTSTS, status & DWC_OTG_MSK_GINT_THREAD_IRQ);
2932 DPRINTFN(14, "GINTSTS=0x%08x HAINT=0x%08x HFNUM=0x%08x\n",
2933 status, DWC_OTG_READ_4(sc, DOTG_HAINT),
2934 DWC_OTG_READ_4(sc, DOTG_HFNUM));
2936 if (status & GINTSTS_USBRST) {
2938 /* set correct state */
2939 sc->sc_flags.status_device_mode = 1;
2940 sc->sc_flags.status_bus_reset = 0;
2941 sc->sc_flags.status_suspend = 0;
2942 sc->sc_flags.change_suspend = 0;
2943 sc->sc_flags.change_connect = 1;
2945 /* Disable SOF interrupt */
2946 sc->sc_irq_mask &= ~GINTMSK_SOFMSK;
2947 DWC_OTG_WRITE_4(sc, DOTG_GINTMSK, sc->sc_irq_mask);
2949 /* complete root HUB interrupt endpoint */
2950 dwc_otg_root_intr(sc);
2953 /* check for any bus state change interrupts */
2954 if (status & GINTSTS_ENUMDONE) {
2958 DPRINTFN(5, "end of reset\n");
2960 /* set correct state */
2961 sc->sc_flags.status_device_mode = 1;
2962 sc->sc_flags.status_bus_reset = 1;
2963 sc->sc_flags.status_suspend = 0;
2964 sc->sc_flags.change_suspend = 0;
2965 sc->sc_flags.change_connect = 1;
2966 sc->sc_flags.status_low_speed = 0;
2967 sc->sc_flags.port_enabled = 1;
2970 (void) dwc_otg_init_fifo(sc, DWC_MODE_DEVICE);
2972 /* reset function address */
2973 dwc_otg_set_address(sc, 0);
2975 /* figure out enumeration speed */
2976 temp = DWC_OTG_READ_4(sc, DOTG_DSTS);
2977 if (DSTS_ENUMSPD_GET(temp) == DSTS_ENUMSPD_HI)
2978 sc->sc_flags.status_high_speed = 1;
2980 sc->sc_flags.status_high_speed = 0;
2983 * Disable resume and SOF interrupt, and enable
2984 * suspend and RX frame interrupt:
2986 sc->sc_irq_mask &= ~(GINTMSK_WKUPINTMSK | GINTMSK_SOFMSK);
2987 sc->sc_irq_mask |= GINTMSK_USBSUSPMSK;
2988 DWC_OTG_WRITE_4(sc, DOTG_GINTMSK, sc->sc_irq_mask);
2990 /* complete root HUB interrupt endpoint */
2991 dwc_otg_root_intr(sc);
2994 if (status & GINTSTS_PRTINT) {
2997 hprt = DWC_OTG_READ_4(sc, DOTG_HPRT);
2999 /* clear change bits */
3000 DWC_OTG_WRITE_4(sc, DOTG_HPRT, (hprt & (
3001 HPRT_PRTPWR | HPRT_PRTENCHNG |
3002 HPRT_PRTCONNDET | HPRT_PRTOVRCURRCHNG)) |
3005 DPRINTFN(12, "GINTSTS=0x%08x, HPRT=0x%08x\n", status, hprt);
3007 sc->sc_flags.status_device_mode = 0;
3009 if (hprt & HPRT_PRTCONNSTS)
3010 sc->sc_flags.status_bus_reset = 1;
3012 sc->sc_flags.status_bus_reset = 0;
3014 if ((hprt & HPRT_PRTENCHNG) &&
3015 (hprt & HPRT_PRTENA) == 0)
3016 sc->sc_flags.change_enabled = 1;
3018 if (hprt & HPRT_PRTENA)
3019 sc->sc_flags.port_enabled = 1;
3021 sc->sc_flags.port_enabled = 0;
3023 if (hprt & HPRT_PRTOVRCURRCHNG)
3024 sc->sc_flags.change_over_current = 1;
3026 if (hprt & HPRT_PRTOVRCURRACT)
3027 sc->sc_flags.port_over_current = 1;
3029 sc->sc_flags.port_over_current = 0;
3031 if (hprt & HPRT_PRTPWR)
3032 sc->sc_flags.port_powered = 1;
3034 sc->sc_flags.port_powered = 0;
3036 if (((hprt & HPRT_PRTSPD_MASK)
3037 >> HPRT_PRTSPD_SHIFT) == HPRT_PRTSPD_LOW)
3038 sc->sc_flags.status_low_speed = 1;
3040 sc->sc_flags.status_low_speed = 0;
3042 if (((hprt & HPRT_PRTSPD_MASK)
3043 >> HPRT_PRTSPD_SHIFT) == HPRT_PRTSPD_HIGH)
3044 sc->sc_flags.status_high_speed = 1;
3046 sc->sc_flags.status_high_speed = 0;
3048 if (hprt & HPRT_PRTCONNDET)
3049 sc->sc_flags.change_connect = 1;
3051 if (hprt & HPRT_PRTSUSP)
3052 dwc_otg_suspend_irq(sc);
3054 dwc_otg_resume_irq(sc);
3056 /* complete root HUB interrupt endpoint */
3057 dwc_otg_root_intr(sc);
3059 /* update host frame interval */
3060 dwc_otg_update_host_frame_interval(sc);
3064 * If resume and suspend is set at the same time we interpret
3065 * that like RESUME. Resume is set when there is at least 3
3066 * milliseconds of inactivity on the USB BUS.
3068 if (status & GINTSTS_WKUPINT) {
3070 DPRINTFN(5, "resume interrupt\n");
3072 dwc_otg_resume_irq(sc);
3074 } else if (status & GINTSTS_USBSUSP) {
3076 DPRINTFN(5, "suspend interrupt\n");
3078 dwc_otg_suspend_irq(sc);
3081 if (status & (GINTSTS_USBSUSP |
3084 GINTSTS_SESSREQINT)) {
3087 temp = DWC_OTG_READ_4(sc, DOTG_GOTGCTL);
3089 DPRINTFN(5, "GOTGCTL=0x%08x\n", temp);
3091 dwc_otg_vbus_interrupt(sc,
3092 (temp & (GOTGCTL_ASESVLD | GOTGCTL_BSESVLD)) ? 1 : 0);
3095 if (sc->sc_xfer_complete != 0) {
3096 sc->sc_xfer_complete = 0;
3098 /* complete FIFOs, if any */
3099 dwc_otg_interrupt_complete_locked(sc);
3101 USB_BUS_SPIN_UNLOCK(&sc->sc_bus);
3102 USB_BUS_UNLOCK(&sc->sc_bus);
3106 dwc_otg_setup_standard_chain_sub(struct dwc_otg_std_temp *temp)
3108 struct dwc_otg_td *td;
3110 /* get current Transfer Descriptor */
3114 /* prepare for next TD */
3115 temp->td_next = td->obj_next;
3117 /* fill out the Transfer Descriptor */
3118 td->func = temp->func;
3120 td->offset = temp->offset;
3121 td->remainder = temp->len;
3124 td->error_stall = 0;
3126 td->did_stall = temp->did_stall;
3127 td->short_pkt = temp->short_pkt;
3128 td->alt_next = temp->setup_alt_next;
3132 td->channel[0] = DWC_OTG_MAX_CHANNELS;
3133 td->channel[1] = DWC_OTG_MAX_CHANNELS;
3134 td->channel[2] = DWC_OTG_MAX_CHANNELS;
3137 td->tt_scheduled = 0;
3138 td->tt_xactpos = HCSPLT_XACTPOS_BEGIN;
3142 dwc_otg_setup_standard_chain(struct usb_xfer *xfer)
3144 struct dwc_otg_std_temp temp;
3145 struct dwc_otg_td *td;
3150 DPRINTFN(9, "addr=%d endpt=%d sumlen=%d speed=%d\n",
3151 xfer->address, UE_GET_ADDR(xfer->endpointno),
3152 xfer->sumlen, usbd_get_speed(xfer->xroot->udev));
3154 temp.max_frame_size = xfer->max_frame_size;
3156 td = xfer->td_start[0];
3157 xfer->td_transfer_first = td;
3158 xfer->td_transfer_cache = td;
3164 temp.td_next = xfer->td_start[0];
3166 temp.setup_alt_next = xfer->flags_int.short_frames_ok ||
3167 xfer->flags_int.isochronous_xfr;
3168 temp.did_stall = !xfer->flags_int.control_stall;
3170 is_host = (xfer->xroot->udev->flags.usb_mode == USB_MODE_HOST);
3172 /* check if we should prepend a setup message */
3174 if (xfer->flags_int.control_xfr) {
3175 if (xfer->flags_int.control_hdr) {
3178 temp.func = &dwc_otg_host_setup_tx;
3180 temp.func = &dwc_otg_setup_rx;
3182 temp.len = xfer->frlengths[0];
3183 temp.pc = xfer->frbuffers + 0;
3184 temp.short_pkt = temp.len ? 1 : 0;
3186 /* check for last frame */
3187 if (xfer->nframes == 1) {
3188 /* no STATUS stage yet, SETUP is last */
3189 if (xfer->flags_int.control_act)
3190 temp.setup_alt_next = 0;
3193 dwc_otg_setup_standard_chain_sub(&temp);
3200 if (x != xfer->nframes) {
3201 if (xfer->endpointno & UE_DIR_IN) {
3203 temp.func = &dwc_otg_host_data_rx;
3206 temp.func = &dwc_otg_data_tx;
3211 temp.func = &dwc_otg_host_data_tx;
3214 temp.func = &dwc_otg_data_rx;
3219 /* setup "pc" pointer */
3220 temp.pc = xfer->frbuffers + x;
3224 while (x != xfer->nframes) {
3226 /* DATA0 / DATA1 message */
3228 temp.len = xfer->frlengths[x];
3232 if (x == xfer->nframes) {
3233 if (xfer->flags_int.control_xfr) {
3234 if (xfer->flags_int.control_act) {
3235 temp.setup_alt_next = 0;
3238 temp.setup_alt_next = 0;
3241 if (temp.len == 0) {
3243 /* make sure that we send an USB packet */
3249 /* regular data transfer */
3251 temp.short_pkt = (xfer->flags.force_short_xfer ? 0 : 1);
3254 dwc_otg_setup_standard_chain_sub(&temp);
3256 if (xfer->flags_int.isochronous_xfr) {
3257 temp.offset += temp.len;
3259 /* get next Page Cache pointer */
3260 temp.pc = xfer->frbuffers + x;
3264 if (xfer->flags_int.control_xfr) {
3266 /* always setup a valid "pc" pointer for status and sync */
3267 temp.pc = xfer->frbuffers + 0;
3270 temp.setup_alt_next = 0;
3272 /* check if we need to sync */
3274 /* we need a SYNC point after TX */
3275 temp.func = &dwc_otg_data_tx_sync;
3276 dwc_otg_setup_standard_chain_sub(&temp);
3279 /* check if we should append a status stage */
3280 if (!xfer->flags_int.control_act) {
3283 * Send a DATA1 message and invert the current
3284 * endpoint direction.
3286 if (xfer->endpointno & UE_DIR_IN) {
3288 temp.func = &dwc_otg_host_data_tx;
3291 temp.func = &dwc_otg_data_rx;
3296 temp.func = &dwc_otg_host_data_rx;
3299 temp.func = &dwc_otg_data_tx;
3304 dwc_otg_setup_standard_chain_sub(&temp);
3306 /* data toggle should be DATA1 */
3311 /* we need a SYNC point after TX */
3312 temp.func = &dwc_otg_data_tx_sync;
3313 dwc_otg_setup_standard_chain_sub(&temp);
3317 /* check if we need to sync */
3320 temp.pc = xfer->frbuffers + 0;
3323 temp.setup_alt_next = 0;
3325 /* we need a SYNC point after TX */
3326 temp.func = &dwc_otg_data_tx_sync;
3327 dwc_otg_setup_standard_chain_sub(&temp);
3331 /* must have at least one frame! */
3333 xfer->td_transfer_last = td;
3337 struct dwc_otg_softc *sc;
3341 sc = DWC_OTG_BUS2SC(xfer->xroot->bus);
3343 /* get first again */
3344 td = xfer->td_transfer_first;
3345 td->toggle = (xfer->endpoint->toggle_next ? 1 : 0);
3348 (xfer->address << HCCHAR_DEVADDR_SHIFT) |
3349 ((xfer->endpointno & UE_ADDR) << HCCHAR_EPNUM_SHIFT) |
3350 (xfer->max_packet_size << HCCHAR_MPS_SHIFT) |
3354 * We are not always able to meet the timing
3355 * requirements of the USB interrupt endpoint's
3356 * complete split token, when doing transfers going
3357 * via a transaction translator. Use the CONTROL
3358 * transfer type instead of the INTERRUPT transfer
3359 * type in general, as a means to workaround
3360 * that. This trick should work for both FULL and LOW
3361 * speed USB traffic going through a TT. For non-TT
3362 * traffic it works aswell. The reason for using
3363 * CONTROL type instead of BULK is that some TTs might
3364 * reject LOW speed BULK traffic.
3366 if (td->ep_type == UE_INTERRUPT)
3367 hcchar |= (UE_CONTROL << HCCHAR_EPTYPE_SHIFT);
3369 hcchar |= (td->ep_type << HCCHAR_EPTYPE_SHIFT);
3371 if (UE_GET_DIR(xfer->endpointno) == UE_DIR_IN)
3372 hcchar |= HCCHAR_EPDIR_IN;
3374 switch (xfer->xroot->udev->speed) {
3376 hcchar |= HCCHAR_LSPDDEV;
3378 case USB_SPEED_FULL:
3379 /* check if root HUB port is running High Speed */
3380 if (dwc_otg_uses_split(xfer->xroot->udev)) {
3381 hcsplt = HCSPLT_SPLTENA |
3382 (xfer->xroot->udev->hs_port_no <<
3383 HCSPLT_PRTADDR_SHIFT) |
3384 (xfer->xroot->udev->hs_hub_addr <<
3385 HCSPLT_HUBADDR_SHIFT);
3389 if (td->ep_type == UE_INTERRUPT) {
3391 ival = xfer->interval / DWC_OTG_HOST_TIMER_RATE;
3394 else if (ival > 127)
3396 td->tmr_val = sc->sc_tmr_val + ival;
3398 } else if (td->ep_type == UE_ISOCHRONOUS) {
3400 td->tmr_val = sc->sc_last_frame_num;
3401 if (td->hcchar & HCCHAR_EPDIR_IN)
3405 td->tmr_res = (uint8_t)sc->sc_last_frame_num;
3408 case USB_SPEED_HIGH:
3410 if (td->ep_type == UE_INTERRUPT) {
3412 hcchar |= ((xfer->max_packet_count & 3)
3413 << HCCHAR_MC_SHIFT);
3414 ival = xfer->interval / DWC_OTG_HOST_TIMER_RATE;
3417 else if (ival > 127)
3419 td->tmr_val = sc->sc_tmr_val + ival;
3421 } else if (td->ep_type == UE_ISOCHRONOUS) {
3422 hcchar |= ((xfer->max_packet_count & 3)
3423 << HCCHAR_MC_SHIFT);
3424 td->tmr_res = 1 << usbd_xfer_get_fps_shift(xfer);
3425 td->tmr_val = sc->sc_last_frame_num;
3426 if (td->hcchar & HCCHAR_EPDIR_IN)
3427 td->tmr_val += td->tmr_res;
3431 td->tmr_res = (uint8_t)sc->sc_last_frame_num;
3441 /* store configuration in all TD's */
3443 td->hcchar = hcchar;
3444 td->hcsplt = hcsplt;
3446 if (((void *)td) == xfer->td_transfer_last)
3455 dwc_otg_timeout(void *arg)
3457 struct usb_xfer *xfer = arg;
3459 DPRINTF("xfer=%p\n", xfer);
3461 USB_BUS_LOCK_ASSERT(xfer->xroot->bus, MA_OWNED);
3463 /* transfer is transferred */
3464 dwc_otg_device_done(xfer, USB_ERR_TIMEOUT);
3468 dwc_otg_start_standard_chain(struct usb_xfer *xfer)
3470 struct dwc_otg_softc *sc = DWC_OTG_BUS2SC(xfer->xroot->bus);
3475 * Poll one time in device mode, which will turn on the
3476 * endpoint interrupts. Else wait for SOF interrupt in host
3479 USB_BUS_SPIN_LOCK(&sc->sc_bus);
3481 if (sc->sc_flags.status_device_mode != 0) {
3482 dwc_otg_xfer_do_fifo(sc, xfer);
3483 if (dwc_otg_xfer_do_complete_locked(sc, xfer))
3486 struct dwc_otg_td *td = xfer->td_transfer_cache;
3487 if (td->ep_type == UE_ISOCHRONOUS &&
3488 (td->hcchar & HCCHAR_EPDIR_IN) == 0) {
3490 * Need to start ISOCHRONOUS OUT transfer ASAP
3491 * because execution is delayed by one 125us
3494 dwc_otg_xfer_do_fifo(sc, xfer);
3495 if (dwc_otg_xfer_do_complete_locked(sc, xfer))
3500 /* put transfer on interrupt queue */
3501 usbd_transfer_enqueue(&xfer->xroot->bus->intr_q, xfer);
3503 /* start timeout, if any */
3504 if (xfer->timeout != 0) {
3505 usbd_transfer_timeout_ms(xfer,
3506 &dwc_otg_timeout, xfer->timeout);
3509 if (sc->sc_flags.status_device_mode != 0)
3512 /* enable SOF interrupt, if any */
3513 dwc_otg_enable_sof_irq(sc);
3515 USB_BUS_SPIN_UNLOCK(&sc->sc_bus);
3519 dwc_otg_root_intr(struct dwc_otg_softc *sc)
3523 USB_BUS_LOCK_ASSERT(&sc->sc_bus, MA_OWNED);
3526 sc->sc_hub_idata[0] = 0x02; /* we only have one port */
3528 uhub_root_intr(&sc->sc_bus, sc->sc_hub_idata,
3529 sizeof(sc->sc_hub_idata));
3533 dwc_otg_standard_done_sub(struct usb_xfer *xfer)
3535 struct dwc_otg_td *td;
3541 td = xfer->td_transfer_cache;
3544 len = td->remainder;
3546 /* store last data toggle */
3547 xfer->endpoint->toggle_next = td->toggle;
3549 if (xfer->aframes != xfer->nframes) {
3551 * Verify the length and subtract
3552 * the remainder from "frlengths[]":
3554 if (len > xfer->frlengths[xfer->aframes]) {
3557 xfer->frlengths[xfer->aframes] -= len;
3560 /* Check for transfer error */
3561 if (td->error_any) {
3562 /* the transfer is finished */
3563 error = (td->error_stall ?
3564 USB_ERR_STALLED : USB_ERR_IOERROR);
3568 /* Check for short transfer */
3570 if (xfer->flags_int.short_frames_ok ||
3571 xfer->flags_int.isochronous_xfr) {
3572 /* follow alt next */
3579 /* the transfer is finished */
3587 /* this USB frame is complete */
3593 /* update transfer cache */
3595 xfer->td_transfer_cache = td;
3601 dwc_otg_standard_done(struct usb_xfer *xfer)
3603 usb_error_t err = 0;
3605 DPRINTFN(13, "xfer=%p endpoint=%p transfer done\n",
3606 xfer, xfer->endpoint);
3610 xfer->td_transfer_cache = xfer->td_transfer_first;
3612 if (xfer->flags_int.control_xfr) {
3614 if (xfer->flags_int.control_hdr) {
3616 err = dwc_otg_standard_done_sub(xfer);
3620 if (xfer->td_transfer_cache == NULL) {
3624 while (xfer->aframes != xfer->nframes) {
3626 err = dwc_otg_standard_done_sub(xfer);
3629 if (xfer->td_transfer_cache == NULL) {
3634 if (xfer->flags_int.control_xfr &&
3635 !xfer->flags_int.control_act) {
3637 err = dwc_otg_standard_done_sub(xfer);
3640 dwc_otg_device_done(xfer, err);
3643 /*------------------------------------------------------------------------*
3644 * dwc_otg_device_done
3646 * NOTE: this function can be called more than one time on the
3647 * same USB transfer!
3648 *------------------------------------------------------------------------*/
3650 dwc_otg_device_done(struct usb_xfer *xfer, usb_error_t error)
3652 struct dwc_otg_softc *sc = DWC_OTG_BUS2SC(xfer->xroot->bus);
3654 DPRINTFN(9, "xfer=%p, endpoint=%p, error=%d\n",
3655 xfer, xfer->endpoint, error);
3657 USB_BUS_SPIN_LOCK(&sc->sc_bus);
3659 if (xfer->flags_int.usb_mode == USB_MODE_DEVICE) {
3660 /* Interrupts are cleared by the interrupt handler */
3662 struct dwc_otg_td *td;
3664 td = xfer->td_transfer_cache;
3666 dwc_otg_host_channel_free(sc, td);
3668 /* dequeue transfer and start next transfer */
3669 usbd_transfer_done(xfer, error);
3671 USB_BUS_SPIN_UNLOCK(&sc->sc_bus);
3675 dwc_otg_xfer_stall(struct usb_xfer *xfer)
3677 dwc_otg_device_done(xfer, USB_ERR_STALLED);
3681 dwc_otg_set_stall(struct usb_device *udev,
3682 struct usb_endpoint *ep, uint8_t *did_stall)
3684 struct dwc_otg_softc *sc;
3689 USB_BUS_LOCK_ASSERT(udev->bus, MA_OWNED);
3692 if (udev->flags.usb_mode != USB_MODE_DEVICE) {
3697 sc = DWC_OTG_BUS2SC(udev->bus);
3699 USB_BUS_SPIN_LOCK(&sc->sc_bus);
3701 /* get endpoint address */
3702 ep_no = ep->edesc->bEndpointAddress;
3704 DPRINTFN(5, "endpoint=0x%x\n", ep_no);
3706 if (ep_no & UE_DIR_IN) {
3707 reg = DOTG_DIEPCTL(ep_no & UE_ADDR);
3708 temp = sc->sc_in_ctl[ep_no & UE_ADDR];
3710 reg = DOTG_DOEPCTL(ep_no & UE_ADDR);
3711 temp = sc->sc_out_ctl[ep_no & UE_ADDR];
3714 /* disable and stall endpoint */
3715 DWC_OTG_WRITE_4(sc, reg, temp | DOEPCTL_EPDIS);
3716 DWC_OTG_WRITE_4(sc, reg, temp | DOEPCTL_STALL);
3718 /* clear active OUT ep */
3719 if (!(ep_no & UE_DIR_IN)) {
3721 sc->sc_active_rx_ep &= ~(1U << (ep_no & UE_ADDR));
3723 if (sc->sc_last_rx_status != 0 &&
3724 (ep_no & UE_ADDR) == GRXSTSRD_CHNUM_GET(
3725 sc->sc_last_rx_status)) {
3727 dwc_otg_common_rx_ack(sc);
3728 /* poll interrupt */
3729 dwc_otg_interrupt_poll_locked(sc);
3730 dwc_otg_interrupt_complete_locked(sc);
3733 USB_BUS_SPIN_UNLOCK(&sc->sc_bus);
3737 dwc_otg_clear_stall_sub_locked(struct dwc_otg_softc *sc, uint32_t mps,
3738 uint8_t ep_no, uint8_t ep_type, uint8_t ep_dir)
3743 if (ep_type == UE_CONTROL) {
3744 /* clearing stall is not needed */
3749 reg = DOTG_DIEPCTL(ep_no);
3751 reg = DOTG_DOEPCTL(ep_no);
3752 sc->sc_active_rx_ep |= (1U << ep_no);
3755 /* round up and mask away the multiplier count */
3756 mps = (mps + 3) & 0x7FC;
3758 if (ep_type == UE_BULK) {
3759 temp = DIEPCTL_EPTYPE_SET(
3760 DIEPCTL_EPTYPE_BULK) |
3762 } else if (ep_type == UE_INTERRUPT) {
3763 temp = DIEPCTL_EPTYPE_SET(
3764 DIEPCTL_EPTYPE_INTERRUPT) |
3767 temp = DIEPCTL_EPTYPE_SET(
3768 DIEPCTL_EPTYPE_ISOC) |
3772 temp |= DIEPCTL_MPS_SET(mps);
3773 temp |= DIEPCTL_TXFNUM_SET(ep_no);
3776 sc->sc_in_ctl[ep_no] = temp;
3778 sc->sc_out_ctl[ep_no] = temp;
3780 DWC_OTG_WRITE_4(sc, reg, temp | DOEPCTL_EPDIS);
3781 DWC_OTG_WRITE_4(sc, reg, temp | DOEPCTL_SETD0PID);
3782 DWC_OTG_WRITE_4(sc, reg, temp | DIEPCTL_SNAK);
3784 /* we only reset the transmit FIFO */
3786 dwc_otg_tx_fifo_reset(sc,
3787 GRSTCTL_TXFIFO(ep_no) |
3791 DOTG_DIEPTSIZ(ep_no), 0);
3794 /* poll interrupt */
3795 dwc_otg_interrupt_poll_locked(sc);
3796 dwc_otg_interrupt_complete_locked(sc);
3800 dwc_otg_clear_stall(struct usb_device *udev, struct usb_endpoint *ep)
3802 struct dwc_otg_softc *sc;
3803 struct usb_endpoint_descriptor *ed;
3805 DPRINTFN(5, "endpoint=%p\n", ep);
3807 USB_BUS_LOCK_ASSERT(udev->bus, MA_OWNED);
3810 if (udev->flags.usb_mode != USB_MODE_DEVICE) {
3815 sc = DWC_OTG_BUS2SC(udev->bus);
3817 USB_BUS_SPIN_LOCK(&sc->sc_bus);
3819 /* get endpoint descriptor */
3822 /* reset endpoint */
3823 dwc_otg_clear_stall_sub_locked(sc,
3824 UGETW(ed->wMaxPacketSize),
3825 (ed->bEndpointAddress & UE_ADDR),
3826 (ed->bmAttributes & UE_XFERTYPE),
3827 (ed->bEndpointAddress & (UE_DIR_IN | UE_DIR_OUT)));
3829 USB_BUS_SPIN_UNLOCK(&sc->sc_bus);
3833 dwc_otg_device_state_change(struct usb_device *udev)
3835 struct dwc_otg_softc *sc;
3839 if (udev->flags.usb_mode != USB_MODE_DEVICE) {
3845 sc = DWC_OTG_BUS2SC(udev->bus);
3847 /* deactivate all other endpoint but the control endpoint */
3848 if (udev->state == USB_STATE_CONFIGURED ||
3849 udev->state == USB_STATE_ADDRESSED) {
3851 USB_BUS_LOCK(&sc->sc_bus);
3853 for (x = 1; x != sc->sc_dev_ep_max; x++) {
3855 if (x < sc->sc_dev_in_ep_max) {
3856 DWC_OTG_WRITE_4(sc, DOTG_DIEPCTL(x),
3858 DWC_OTG_WRITE_4(sc, DOTG_DIEPCTL(x), 0);
3861 DWC_OTG_WRITE_4(sc, DOTG_DOEPCTL(x),
3863 DWC_OTG_WRITE_4(sc, DOTG_DOEPCTL(x), 0);
3865 USB_BUS_UNLOCK(&sc->sc_bus);
3870 dwc_otg_init(struct dwc_otg_softc *sc)
3876 /* set up the bus structure */
3877 sc->sc_bus.usbrev = USB_REV_2_0;
3878 sc->sc_bus.methods = &dwc_otg_bus_methods;
3880 usb_callout_init_mtx(&sc->sc_timer,
3881 &sc->sc_bus.bus_mtx, 0);
3883 USB_BUS_LOCK(&sc->sc_bus);
3885 /* turn on clocks */
3886 dwc_otg_clocks_on(sc);
3888 temp = DWC_OTG_READ_4(sc, DOTG_GSNPSID);
3889 DPRINTF("Version = 0x%08x\n", temp);
3892 DWC_OTG_WRITE_4(sc, DOTG_DCTL,
3895 /* wait for host to detect disconnect */
3896 usb_pause_mtx(&sc->sc_bus.bus_mtx, hz / 32);
3898 DWC_OTG_WRITE_4(sc, DOTG_GRSTCTL,
3901 /* wait a little bit for block to reset */
3902 usb_pause_mtx(&sc->sc_bus.bus_mtx, hz / 128);
3904 switch (sc->sc_mode) {
3905 case DWC_MODE_DEVICE:
3906 temp = GUSBCFG_FORCEDEVMODE;
3909 temp = GUSBCFG_FORCEHOSTMODE;
3916 /* select HSIC, ULPI or internal PHY mode */
3917 switch (dwc_otg_phy_type) {
3918 case DWC_OTG_PHY_HSIC:
3919 DWC_OTG_WRITE_4(sc, DOTG_GUSBCFG,
3921 GUSBCFG_TRD_TIM_SET(5) | temp);
3922 DWC_OTG_WRITE_4(sc, DOTG_GOTGCTL,
3925 temp = DWC_OTG_READ_4(sc, DOTG_GLPMCFG);
3926 DWC_OTG_WRITE_4(sc, DOTG_GLPMCFG,
3927 temp & ~GLPMCFG_HSIC_CONN);
3928 DWC_OTG_WRITE_4(sc, DOTG_GLPMCFG,
3929 temp | GLPMCFG_HSIC_CONN);
3931 case DWC_OTG_PHY_ULPI:
3932 DWC_OTG_WRITE_4(sc, DOTG_GUSBCFG,
3933 GUSBCFG_ULPI_UTMI_SEL |
3934 GUSBCFG_TRD_TIM_SET(5) | temp);
3935 DWC_OTG_WRITE_4(sc, DOTG_GOTGCTL, 0);
3937 temp = DWC_OTG_READ_4(sc, DOTG_GLPMCFG);
3938 DWC_OTG_WRITE_4(sc, DOTG_GLPMCFG,
3939 temp & ~GLPMCFG_HSIC_CONN);
3941 case DWC_OTG_PHY_INTERNAL:
3942 DWC_OTG_WRITE_4(sc, DOTG_GUSBCFG,
3944 GUSBCFG_TRD_TIM_SET(5) | temp);
3945 DWC_OTG_WRITE_4(sc, DOTG_GOTGCTL, 0);
3947 temp = DWC_OTG_READ_4(sc, DOTG_GLPMCFG);
3948 DWC_OTG_WRITE_4(sc, DOTG_GLPMCFG,
3949 temp & ~GLPMCFG_HSIC_CONN);
3951 temp = DWC_OTG_READ_4(sc, DOTG_GGPIO);
3952 temp &= ~(DOTG_GGPIO_NOVBUSSENS | DOTG_GGPIO_I2CPADEN);
3953 temp |= (DOTG_GGPIO_VBUSASEN | DOTG_GGPIO_VBUSBSEN |
3955 DWC_OTG_WRITE_4(sc, DOTG_GGPIO, temp);
3961 /* clear global nak */
3962 DWC_OTG_WRITE_4(sc, DOTG_DCTL,
3966 /* disable USB port */
3967 DWC_OTG_WRITE_4(sc, DOTG_PCGCCTL, 0xFFFFFFFF);
3970 usb_pause_mtx(&sc->sc_bus.bus_mtx, hz / 100);
3972 /* enable USB port */
3973 DWC_OTG_WRITE_4(sc, DOTG_PCGCCTL, 0);
3976 usb_pause_mtx(&sc->sc_bus.bus_mtx, hz / 100);
3978 temp = DWC_OTG_READ_4(sc, DOTG_GHWCFG3);
3980 sc->sc_fifo_size = 4 * GHWCFG3_DFIFODEPTH_GET(temp);
3982 temp = DWC_OTG_READ_4(sc, DOTG_GHWCFG2);
3984 sc->sc_dev_ep_max = GHWCFG2_NUMDEVEPS_GET(temp);
3986 if (sc->sc_dev_ep_max > DWC_OTG_MAX_ENDPOINTS)
3987 sc->sc_dev_ep_max = DWC_OTG_MAX_ENDPOINTS;
3989 sc->sc_host_ch_max = GHWCFG2_NUMHSTCHNL_GET(temp);
3991 if (sc->sc_host_ch_max > DWC_OTG_MAX_CHANNELS)
3992 sc->sc_host_ch_max = DWC_OTG_MAX_CHANNELS;
3994 temp = DWC_OTG_READ_4(sc, DOTG_GHWCFG4);
3996 sc->sc_dev_in_ep_max = GHWCFG4_NUM_IN_EP_GET(temp);
3998 DPRINTF("Total FIFO size = %d bytes, Device EPs = %d/%d Host CHs = %d\n",
3999 sc->sc_fifo_size, sc->sc_dev_ep_max, sc->sc_dev_in_ep_max,
4000 sc->sc_host_ch_max);
4003 if (dwc_otg_init_fifo(sc, DWC_MODE_OTG)) {
4004 USB_BUS_UNLOCK(&sc->sc_bus);
4008 /* enable interrupts */
4009 sc->sc_irq_mask |= DWC_OTG_MSK_GINT_THREAD_IRQ;
4010 DWC_OTG_WRITE_4(sc, DOTG_GINTMSK, sc->sc_irq_mask);
4012 if (sc->sc_mode == DWC_MODE_OTG || sc->sc_mode == DWC_MODE_DEVICE) {
4014 /* enable all endpoint interrupts */
4015 temp = DWC_OTG_READ_4(sc, DOTG_GHWCFG2);
4016 if (temp & GHWCFG2_MPI) {
4019 DPRINTF("Disable Multi Process Interrupts\n");
4021 for (x = 0; x != sc->sc_dev_in_ep_max; x++) {
4022 DWC_OTG_WRITE_4(sc, DOTG_DIEPEACHINTMSK(x), 0);
4023 DWC_OTG_WRITE_4(sc, DOTG_DOEPEACHINTMSK(x), 0);
4025 DWC_OTG_WRITE_4(sc, DOTG_DEACHINTMSK, 0);
4027 DWC_OTG_WRITE_4(sc, DOTG_DIEPMSK,
4028 DIEPMSK_XFERCOMPLMSK);
4029 DWC_OTG_WRITE_4(sc, DOTG_DOEPMSK, 0);
4030 DWC_OTG_WRITE_4(sc, DOTG_DAINTMSK, 0xFFFF);
4033 if (sc->sc_mode == DWC_MODE_OTG || sc->sc_mode == DWC_MODE_HOST) {
4035 temp = DWC_OTG_READ_4(sc, DOTG_HCFG);
4036 temp &= ~(HCFG_FSLSSUPP | HCFG_FSLSPCLKSEL_MASK);
4037 temp |= (1 << HCFG_FSLSPCLKSEL_SHIFT);
4038 DWC_OTG_WRITE_4(sc, DOTG_HCFG, temp);
4041 /* only enable global IRQ */
4042 DWC_OTG_WRITE_4(sc, DOTG_GAHBCFG,
4043 GAHBCFG_GLBLINTRMSK);
4045 /* turn off clocks */
4046 dwc_otg_clocks_off(sc);
4048 /* read initial VBUS state */
4050 temp = DWC_OTG_READ_4(sc, DOTG_GOTGCTL);
4052 DPRINTFN(5, "GOTCTL=0x%08x\n", temp);
4054 dwc_otg_vbus_interrupt(sc,
4055 (temp & (GOTGCTL_ASESVLD | GOTGCTL_BSESVLD)) ? 1 : 0);
4057 USB_BUS_UNLOCK(&sc->sc_bus);
4059 /* catch any lost interrupts */
4061 dwc_otg_do_poll(&sc->sc_bus);
4063 return (0); /* success */
4067 dwc_otg_uninit(struct dwc_otg_softc *sc)
4069 USB_BUS_LOCK(&sc->sc_bus);
4071 /* stop host timer */
4072 dwc_otg_timer_stop(sc);
4074 /* set disconnect */
4075 DWC_OTG_WRITE_4(sc, DOTG_DCTL,
4078 /* turn off global IRQ */
4079 DWC_OTG_WRITE_4(sc, DOTG_GAHBCFG, 0);
4081 sc->sc_flags.port_enabled = 0;
4082 sc->sc_flags.port_powered = 0;
4083 sc->sc_flags.status_vbus = 0;
4084 sc->sc_flags.status_bus_reset = 0;
4085 sc->sc_flags.status_suspend = 0;
4086 sc->sc_flags.change_suspend = 0;
4087 sc->sc_flags.change_connect = 1;
4089 dwc_otg_pull_down(sc);
4090 dwc_otg_clocks_off(sc);
4092 USB_BUS_UNLOCK(&sc->sc_bus);
4094 usb_callout_drain(&sc->sc_timer);
4098 dwc_otg_suspend(struct dwc_otg_softc *sc)
4104 dwc_otg_resume(struct dwc_otg_softc *sc)
4110 dwc_otg_do_poll(struct usb_bus *bus)
4112 struct dwc_otg_softc *sc = DWC_OTG_BUS2SC(bus);
4114 USB_BUS_LOCK(&sc->sc_bus);
4115 USB_BUS_SPIN_LOCK(&sc->sc_bus);
4116 dwc_otg_interrupt_poll_locked(sc);
4117 dwc_otg_interrupt_complete_locked(sc);
4118 USB_BUS_SPIN_UNLOCK(&sc->sc_bus);
4119 USB_BUS_UNLOCK(&sc->sc_bus);
4122 /*------------------------------------------------------------------------*
4123 * DWC OTG bulk support
4124 * DWC OTG control support
4125 * DWC OTG interrupt support
4126 *------------------------------------------------------------------------*/
4128 dwc_otg_device_non_isoc_open(struct usb_xfer *xfer)
4133 dwc_otg_device_non_isoc_close(struct usb_xfer *xfer)
4135 dwc_otg_device_done(xfer, USB_ERR_CANCELLED);
4139 dwc_otg_device_non_isoc_enter(struct usb_xfer *xfer)
4144 dwc_otg_device_non_isoc_start(struct usb_xfer *xfer)
4147 dwc_otg_setup_standard_chain(xfer);
4148 dwc_otg_start_standard_chain(xfer);
4151 struct usb_pipe_methods dwc_otg_device_non_isoc_methods =
4153 .open = dwc_otg_device_non_isoc_open,
4154 .close = dwc_otg_device_non_isoc_close,
4155 .enter = dwc_otg_device_non_isoc_enter,
4156 .start = dwc_otg_device_non_isoc_start,
4159 /*------------------------------------------------------------------------*
4160 * DWC OTG full speed isochronous support
4161 *------------------------------------------------------------------------*/
4163 dwc_otg_device_isoc_open(struct usb_xfer *xfer)
4168 dwc_otg_device_isoc_close(struct usb_xfer *xfer)
4170 dwc_otg_device_done(xfer, USB_ERR_CANCELLED);
4174 dwc_otg_device_isoc_enter(struct usb_xfer *xfer)
4179 dwc_otg_device_isoc_start(struct usb_xfer *xfer)
4181 struct dwc_otg_softc *sc = DWC_OTG_BUS2SC(xfer->xroot->bus);
4185 uint8_t shift = usbd_xfer_get_fps_shift(xfer);
4187 DPRINTFN(6, "xfer=%p next=%d nframes=%d\n",
4188 xfer, xfer->endpoint->isoc_next, xfer->nframes);
4190 if (xfer->xroot->udev->flags.usb_mode == USB_MODE_HOST) {
4191 temp = DWC_OTG_READ_4(sc, DOTG_HFNUM);
4193 /* get the current frame index */
4194 framenum = (temp & HFNUM_FRNUM_MASK);
4196 temp = DWC_OTG_READ_4(sc, DOTG_DSTS);
4198 /* get the current frame index */
4199 framenum = DSTS_SOFFN_GET(temp);
4203 * Check if port is doing 8000 or 1000 frames per second:
4205 if (sc->sc_flags.status_high_speed)
4208 framenum &= DWC_OTG_FRAME_MASK;
4211 * Compute number of milliseconds worth of data traffic for
4212 * this USB transfer:
4214 if (xfer->xroot->udev->speed == USB_SPEED_HIGH)
4215 msframes = ((xfer->nframes << shift) + 7) / 8;
4217 msframes = xfer->nframes;
4220 * check if the frame index is within the window where the frames
4223 temp = (framenum - xfer->endpoint->isoc_next) & DWC_OTG_FRAME_MASK;
4225 if ((xfer->endpoint->is_synced == 0) || (temp < msframes)) {
4227 * If there is data underflow or the pipe queue is
4228 * empty we schedule the transfer a few frames ahead
4229 * of the current frame position. Else two isochronous
4230 * transfers might overlap.
4232 xfer->endpoint->isoc_next = (framenum + 3) & DWC_OTG_FRAME_MASK;
4233 xfer->endpoint->is_synced = 1;
4234 DPRINTFN(3, "start next=%d\n", xfer->endpoint->isoc_next);
4237 * compute how many milliseconds the insertion is ahead of the
4238 * current frame position:
4240 temp = (xfer->endpoint->isoc_next - framenum) & DWC_OTG_FRAME_MASK;
4243 * pre-compute when the isochronous transfer will be finished:
4245 xfer->isoc_time_complete =
4246 usb_isoc_time_expand(&sc->sc_bus, framenum) + temp + msframes;
4249 dwc_otg_setup_standard_chain(xfer);
4251 /* compute frame number for next insertion */
4252 xfer->endpoint->isoc_next += msframes;
4254 /* start TD chain */
4255 dwc_otg_start_standard_chain(xfer);
4258 struct usb_pipe_methods dwc_otg_device_isoc_methods =
4260 .open = dwc_otg_device_isoc_open,
4261 .close = dwc_otg_device_isoc_close,
4262 .enter = dwc_otg_device_isoc_enter,
4263 .start = dwc_otg_device_isoc_start,
4266 /*------------------------------------------------------------------------*
4267 * DWC OTG root control support
4268 *------------------------------------------------------------------------*
4269 * Simulate a hardware HUB by handling all the necessary requests.
4270 *------------------------------------------------------------------------*/
4272 static const struct usb_device_descriptor dwc_otg_devd = {
4273 .bLength = sizeof(struct usb_device_descriptor),
4274 .bDescriptorType = UDESC_DEVICE,
4275 .bcdUSB = {0x00, 0x02},
4276 .bDeviceClass = UDCLASS_HUB,
4277 .bDeviceSubClass = UDSUBCLASS_HUB,
4278 .bDeviceProtocol = UDPROTO_HSHUBSTT,
4279 .bMaxPacketSize = 64,
4280 .bcdDevice = {0x00, 0x01},
4283 .bNumConfigurations = 1,
4286 static const struct dwc_otg_config_desc dwc_otg_confd = {
4288 .bLength = sizeof(struct usb_config_descriptor),
4289 .bDescriptorType = UDESC_CONFIG,
4290 .wTotalLength[0] = sizeof(dwc_otg_confd),
4292 .bConfigurationValue = 1,
4293 .iConfiguration = 0,
4294 .bmAttributes = UC_SELF_POWERED,
4298 .bLength = sizeof(struct usb_interface_descriptor),
4299 .bDescriptorType = UDESC_INTERFACE,
4301 .bInterfaceClass = UICLASS_HUB,
4302 .bInterfaceSubClass = UISUBCLASS_HUB,
4303 .bInterfaceProtocol = 0,
4306 .bLength = sizeof(struct usb_endpoint_descriptor),
4307 .bDescriptorType = UDESC_ENDPOINT,
4308 .bEndpointAddress = (UE_DIR_IN | DWC_OTG_INTR_ENDPT),
4309 .bmAttributes = UE_INTERRUPT,
4310 .wMaxPacketSize[0] = 8,
4315 #define HSETW(ptr, val) ptr = { (uint8_t)(val), (uint8_t)((val) >> 8) }
4317 static const struct usb_hub_descriptor_min dwc_otg_hubd = {
4318 .bDescLength = sizeof(dwc_otg_hubd),
4319 .bDescriptorType = UDESC_HUB,
4321 HSETW(.wHubCharacteristics, (UHD_PWR_NO_SWITCH | UHD_OC_INDIVIDUAL)),
4322 .bPwrOn2PwrGood = 50,
4323 .bHubContrCurrent = 0,
4324 .DeviceRemovable = {0}, /* port is removable */
4327 #define STRING_VENDOR \
4330 #define STRING_PRODUCT \
4331 "O\0T\0G\0 \0R\0o\0o\0t\0 \0H\0U\0B"
4333 USB_MAKE_STRING_DESC(STRING_VENDOR, dwc_otg_vendor);
4334 USB_MAKE_STRING_DESC(STRING_PRODUCT, dwc_otg_product);
4337 dwc_otg_roothub_exec(struct usb_device *udev,
4338 struct usb_device_request *req, const void **pptr, uint16_t *plength)
4340 struct dwc_otg_softc *sc = DWC_OTG_BUS2SC(udev->bus);
4347 USB_BUS_LOCK_ASSERT(&sc->sc_bus, MA_OWNED);
4350 ptr = (const void *)&sc->sc_hub_temp;
4354 value = UGETW(req->wValue);
4355 index = UGETW(req->wIndex);
4357 /* demultiplex the control request */
4359 switch (req->bmRequestType) {
4360 case UT_READ_DEVICE:
4361 switch (req->bRequest) {
4362 case UR_GET_DESCRIPTOR:
4363 goto tr_handle_get_descriptor;
4365 goto tr_handle_get_config;
4367 goto tr_handle_get_status;
4373 case UT_WRITE_DEVICE:
4374 switch (req->bRequest) {
4375 case UR_SET_ADDRESS:
4376 goto tr_handle_set_address;
4378 goto tr_handle_set_config;
4379 case UR_CLEAR_FEATURE:
4380 goto tr_valid; /* nop */
4381 case UR_SET_DESCRIPTOR:
4382 goto tr_valid; /* nop */
4383 case UR_SET_FEATURE:
4389 case UT_WRITE_ENDPOINT:
4390 switch (req->bRequest) {
4391 case UR_CLEAR_FEATURE:
4392 switch (UGETW(req->wValue)) {
4393 case UF_ENDPOINT_HALT:
4394 goto tr_handle_clear_halt;
4395 case UF_DEVICE_REMOTE_WAKEUP:
4396 goto tr_handle_clear_wakeup;
4401 case UR_SET_FEATURE:
4402 switch (UGETW(req->wValue)) {
4403 case UF_ENDPOINT_HALT:
4404 goto tr_handle_set_halt;
4405 case UF_DEVICE_REMOTE_WAKEUP:
4406 goto tr_handle_set_wakeup;
4411 case UR_SYNCH_FRAME:
4412 goto tr_valid; /* nop */
4418 case UT_READ_ENDPOINT:
4419 switch (req->bRequest) {
4421 goto tr_handle_get_ep_status;
4427 case UT_WRITE_INTERFACE:
4428 switch (req->bRequest) {
4429 case UR_SET_INTERFACE:
4430 goto tr_handle_set_interface;
4431 case UR_CLEAR_FEATURE:
4432 goto tr_valid; /* nop */
4433 case UR_SET_FEATURE:
4439 case UT_READ_INTERFACE:
4440 switch (req->bRequest) {
4441 case UR_GET_INTERFACE:
4442 goto tr_handle_get_interface;
4444 goto tr_handle_get_iface_status;
4450 case UT_WRITE_CLASS_INTERFACE:
4451 case UT_WRITE_VENDOR_INTERFACE:
4455 case UT_READ_CLASS_INTERFACE:
4456 case UT_READ_VENDOR_INTERFACE:
4460 case UT_WRITE_CLASS_DEVICE:
4461 switch (req->bRequest) {
4462 case UR_CLEAR_FEATURE:
4464 case UR_SET_DESCRIPTOR:
4465 case UR_SET_FEATURE:
4472 case UT_WRITE_CLASS_OTHER:
4473 switch (req->bRequest) {
4474 case UR_CLEAR_FEATURE:
4475 goto tr_handle_clear_port_feature;
4476 case UR_SET_FEATURE:
4477 goto tr_handle_set_port_feature;
4478 case UR_CLEAR_TT_BUFFER:
4488 case UT_READ_CLASS_OTHER:
4489 switch (req->bRequest) {
4490 case UR_GET_TT_STATE:
4491 goto tr_handle_get_tt_state;
4493 goto tr_handle_get_port_status;
4499 case UT_READ_CLASS_DEVICE:
4500 switch (req->bRequest) {
4501 case UR_GET_DESCRIPTOR:
4502 goto tr_handle_get_class_descriptor;
4504 goto tr_handle_get_class_status;
4515 tr_handle_get_descriptor:
4516 switch (value >> 8) {
4521 len = sizeof(dwc_otg_devd);
4522 ptr = (const void *)&dwc_otg_devd;
4528 len = sizeof(dwc_otg_confd);
4529 ptr = (const void *)&dwc_otg_confd;
4532 switch (value & 0xff) {
4533 case 0: /* Language table */
4534 len = sizeof(usb_string_lang_en);
4535 ptr = (const void *)&usb_string_lang_en;
4538 case 1: /* Vendor */
4539 len = sizeof(dwc_otg_vendor);
4540 ptr = (const void *)&dwc_otg_vendor;
4543 case 2: /* Product */
4544 len = sizeof(dwc_otg_product);
4545 ptr = (const void *)&dwc_otg_product;
4556 tr_handle_get_config:
4558 sc->sc_hub_temp.wValue[0] = sc->sc_conf;
4561 tr_handle_get_status:
4563 USETW(sc->sc_hub_temp.wValue, UDS_SELF_POWERED);
4566 tr_handle_set_address:
4567 if (value & 0xFF00) {
4570 sc->sc_rt_addr = value;
4573 tr_handle_set_config:
4577 sc->sc_conf = value;
4580 tr_handle_get_interface:
4582 sc->sc_hub_temp.wValue[0] = 0;
4585 tr_handle_get_tt_state:
4586 tr_handle_get_class_status:
4587 tr_handle_get_iface_status:
4588 tr_handle_get_ep_status:
4590 USETW(sc->sc_hub_temp.wValue, 0);
4594 tr_handle_set_interface:
4595 tr_handle_set_wakeup:
4596 tr_handle_clear_wakeup:
4597 tr_handle_clear_halt:
4600 tr_handle_clear_port_feature:
4604 DPRINTFN(9, "UR_CLEAR_PORT_FEATURE on port %d\n", index);
4607 case UHF_PORT_SUSPEND:
4608 dwc_otg_wakeup_peer(sc);
4611 case UHF_PORT_ENABLE:
4612 if (sc->sc_flags.status_device_mode == 0) {
4613 DWC_OTG_WRITE_4(sc, DOTG_HPRT,
4614 sc->sc_hprt_val | HPRT_PRTENA);
4616 sc->sc_flags.port_enabled = 0;
4619 case UHF_C_PORT_RESET:
4620 sc->sc_flags.change_reset = 0;
4623 case UHF_C_PORT_ENABLE:
4624 sc->sc_flags.change_enabled = 0;
4627 case UHF_C_PORT_OVER_CURRENT:
4628 sc->sc_flags.change_over_current = 0;
4632 case UHF_PORT_INDICATOR:
4636 case UHF_PORT_POWER:
4637 sc->sc_flags.port_powered = 0;
4638 if (sc->sc_mode == DWC_MODE_HOST || sc->sc_mode == DWC_MODE_OTG) {
4639 sc->sc_hprt_val = 0;
4640 DWC_OTG_WRITE_4(sc, DOTG_HPRT, HPRT_PRTENA);
4642 dwc_otg_pull_down(sc);
4643 dwc_otg_clocks_off(sc);
4646 case UHF_C_PORT_CONNECTION:
4647 /* clear connect change flag */
4648 sc->sc_flags.change_connect = 0;
4651 case UHF_C_PORT_SUSPEND:
4652 sc->sc_flags.change_suspend = 0;
4656 err = USB_ERR_IOERROR;
4661 tr_handle_set_port_feature:
4665 DPRINTFN(9, "UR_SET_PORT_FEATURE\n");
4668 case UHF_PORT_ENABLE:
4671 case UHF_PORT_SUSPEND:
4672 if (sc->sc_flags.status_device_mode == 0) {
4673 /* set suspend BIT */
4674 sc->sc_hprt_val |= HPRT_PRTSUSP;
4675 DWC_OTG_WRITE_4(sc, DOTG_HPRT, sc->sc_hprt_val);
4677 /* generate HUB suspend event */
4678 dwc_otg_suspend_irq(sc);
4682 case UHF_PORT_RESET:
4683 if (sc->sc_flags.status_device_mode == 0) {
4685 DPRINTF("PORT RESET\n");
4687 /* enable PORT reset */
4688 DWC_OTG_WRITE_4(sc, DOTG_HPRT, sc->sc_hprt_val | HPRT_PRTRST);
4690 /* Wait 62.5ms for reset to complete */
4691 usb_pause_mtx(&sc->sc_bus.bus_mtx, hz / 16);
4693 DWC_OTG_WRITE_4(sc, DOTG_HPRT, sc->sc_hprt_val);
4695 /* Wait 62.5ms for reset to complete */
4696 usb_pause_mtx(&sc->sc_bus.bus_mtx, hz / 16);
4699 (void) dwc_otg_init_fifo(sc, DWC_MODE_HOST);
4701 sc->sc_flags.change_reset = 1;
4703 err = USB_ERR_IOERROR;
4708 case UHF_PORT_INDICATOR:
4711 case UHF_PORT_POWER:
4712 sc->sc_flags.port_powered = 1;
4713 if (sc->sc_mode == DWC_MODE_HOST || sc->sc_mode == DWC_MODE_OTG) {
4714 sc->sc_hprt_val |= HPRT_PRTPWR;
4715 DWC_OTG_WRITE_4(sc, DOTG_HPRT, sc->sc_hprt_val);
4717 if (sc->sc_mode == DWC_MODE_DEVICE || sc->sc_mode == DWC_MODE_OTG) {
4718 /* pull up D+, if any */
4719 dwc_otg_pull_up(sc);
4723 err = USB_ERR_IOERROR;
4728 tr_handle_get_port_status:
4730 DPRINTFN(9, "UR_GET_PORT_STATUS\n");
4735 if (sc->sc_flags.status_vbus)
4736 dwc_otg_clocks_on(sc);
4738 dwc_otg_clocks_off(sc);
4740 /* Select Device Side Mode */
4742 if (sc->sc_flags.status_device_mode) {
4743 value = UPS_PORT_MODE_DEVICE;
4744 dwc_otg_timer_stop(sc);
4747 dwc_otg_timer_start(sc);
4750 if (sc->sc_flags.status_high_speed)
4751 value |= UPS_HIGH_SPEED;
4752 else if (sc->sc_flags.status_low_speed)
4753 value |= UPS_LOW_SPEED;
4755 if (sc->sc_flags.port_powered)
4756 value |= UPS_PORT_POWER;
4758 if (sc->sc_flags.port_enabled)
4759 value |= UPS_PORT_ENABLED;
4761 if (sc->sc_flags.port_over_current)
4762 value |= UPS_OVERCURRENT_INDICATOR;
4764 if (sc->sc_flags.status_vbus &&
4765 sc->sc_flags.status_bus_reset)
4766 value |= UPS_CURRENT_CONNECT_STATUS;
4768 if (sc->sc_flags.status_suspend)
4769 value |= UPS_SUSPEND;
4771 USETW(sc->sc_hub_temp.ps.wPortStatus, value);
4775 if (sc->sc_flags.change_enabled)
4776 value |= UPS_C_PORT_ENABLED;
4777 if (sc->sc_flags.change_connect)
4778 value |= UPS_C_CONNECT_STATUS;
4779 if (sc->sc_flags.change_suspend)
4780 value |= UPS_C_SUSPEND;
4781 if (sc->sc_flags.change_reset)
4782 value |= UPS_C_PORT_RESET;
4783 if (sc->sc_flags.change_over_current)
4784 value |= UPS_C_OVERCURRENT_INDICATOR;
4786 USETW(sc->sc_hub_temp.ps.wPortChange, value);
4787 len = sizeof(sc->sc_hub_temp.ps);
4790 tr_handle_get_class_descriptor:
4794 ptr = (const void *)&dwc_otg_hubd;
4795 len = sizeof(dwc_otg_hubd);
4799 err = USB_ERR_STALLED;
4808 dwc_otg_xfer_setup(struct usb_setup_params *parm)
4810 struct usb_xfer *xfer;
4817 xfer = parm->curr_xfer;
4820 * NOTE: This driver does not use any of the parameters that
4821 * are computed from the following values. Just set some
4822 * reasonable dummies:
4824 parm->hc_max_packet_size = 0x500;
4825 parm->hc_max_packet_count = 3;
4826 parm->hc_max_frame_size = 3 * 0x500;
4828 usbd_transfer_setup_sub(parm);
4831 * compute maximum number of TDs
4833 ep_type = (xfer->endpoint->edesc->bmAttributes & UE_XFERTYPE);
4835 if (ep_type == UE_CONTROL) {
4837 ntd = xfer->nframes + 1 /* STATUS */ + 1 /* SYNC 1 */
4838 + 1 /* SYNC 2 */ + 1 /* SYNC 3 */;
4841 ntd = xfer->nframes + 1 /* SYNC */ ;
4845 * check if "usbd_transfer_setup_sub" set an error
4851 * allocate transfer descriptors
4855 ep_no = xfer->endpointno & UE_ADDR;
4858 * Check for a valid endpoint profile in USB device mode:
4860 if (xfer->flags_int.usb_mode == USB_MODE_DEVICE) {
4861 const struct usb_hw_ep_profile *pf;
4863 dwc_otg_get_hw_ep_profile(parm->udev, &pf, ep_no);
4866 /* should not happen */
4867 parm->err = USB_ERR_INVAL;
4873 parm->size[0] += ((-parm->size[0]) & (USB_HOST_ALIGN - 1));
4875 for (n = 0; n != ntd; n++) {
4877 struct dwc_otg_td *td;
4881 td = USB_ADD_BYTES(parm->buf, parm->size[0]);
4883 /* compute shared bandwidth resource index for TT */
4884 if (dwc_otg_uses_split(parm->udev)) {
4885 if (parm->udev->parent_hs_hub->ddesc.bDeviceProtocol == UDPROTO_HSHUBMTT)
4886 td->tt_index = parm->udev->device_index;
4888 td->tt_index = parm->udev->parent_hs_hub->device_index;
4890 td->tt_index = parm->udev->device_index;
4894 td->max_packet_size = xfer->max_packet_size;
4895 td->max_packet_count = xfer->max_packet_count;
4897 if (td->max_packet_count == 0 || td->max_packet_count > 3)
4898 td->max_packet_count = 1;
4900 td->ep_type = ep_type;
4901 td->obj_next = last_obj;
4905 parm->size[0] += sizeof(*td);
4908 xfer->td_start[0] = last_obj;
4912 dwc_otg_xfer_unsetup(struct usb_xfer *xfer)
4918 dwc_otg_ep_init(struct usb_device *udev, struct usb_endpoint_descriptor *edesc,
4919 struct usb_endpoint *ep)
4921 struct dwc_otg_softc *sc = DWC_OTG_BUS2SC(udev->bus);
4923 DPRINTFN(2, "endpoint=%p, addr=%d, endpt=%d, mode=%d (%d,%d)\n",
4925 edesc->bEndpointAddress, udev->flags.usb_mode,
4926 sc->sc_rt_addr, udev->device_index);
4928 if (udev->device_index != sc->sc_rt_addr) {
4930 if (udev->flags.usb_mode == USB_MODE_DEVICE) {
4931 if (udev->speed != USB_SPEED_FULL &&
4932 udev->speed != USB_SPEED_HIGH) {
4937 if (udev->speed == USB_SPEED_HIGH &&
4938 (edesc->wMaxPacketSize[1] & 0x18) != 0 &&
4939 (edesc->bmAttributes & UE_XFERTYPE) != UE_ISOCHRONOUS) {
4941 DPRINTFN(-1, "Non-isochronous high bandwidth "
4942 "endpoint not supported\n");
4946 if ((edesc->bmAttributes & UE_XFERTYPE) == UE_ISOCHRONOUS)
4947 ep->methods = &dwc_otg_device_isoc_methods;
4949 ep->methods = &dwc_otg_device_non_isoc_methods;
4954 dwc_otg_set_hw_power_sleep(struct usb_bus *bus, uint32_t state)
4956 struct dwc_otg_softc *sc = DWC_OTG_BUS2SC(bus);
4959 case USB_HW_POWER_SUSPEND:
4960 dwc_otg_suspend(sc);
4962 case USB_HW_POWER_SHUTDOWN:
4965 case USB_HW_POWER_RESUME:
4974 dwc_otg_get_dma_delay(struct usb_device *udev, uint32_t *pus)
4976 /* DMA delay - wait until any use of memory is finished */
4977 *pus = (2125); /* microseconds */
4981 dwc_otg_device_resume(struct usb_device *udev)
4985 /* poll all transfers again to restart resumed ones */
4986 dwc_otg_do_poll(udev->bus);
4990 dwc_otg_device_suspend(struct usb_device *udev)
4995 struct usb_bus_methods dwc_otg_bus_methods =
4997 .endpoint_init = &dwc_otg_ep_init,
4998 .xfer_setup = &dwc_otg_xfer_setup,
4999 .xfer_unsetup = &dwc_otg_xfer_unsetup,
5000 .get_hw_ep_profile = &dwc_otg_get_hw_ep_profile,
5001 .xfer_stall = &dwc_otg_xfer_stall,
5002 .set_stall = &dwc_otg_set_stall,
5003 .clear_stall = &dwc_otg_clear_stall,
5004 .roothub_exec = &dwc_otg_roothub_exec,
5005 .xfer_poll = &dwc_otg_do_poll,
5006 .device_state_change = &dwc_otg_device_state_change,
5007 .set_hw_power_sleep = &dwc_otg_set_hw_power_sleep,
5008 .get_dma_delay = &dwc_otg_get_dma_delay,
5009 .device_resume = &dwc_otg_device_resume,
5010 .device_suspend = &dwc_otg_device_suspend,