3 * Copyright (c) 2012 Hans Petter Selasky. All rights reserved.
4 * Copyright (c) 2010-2011 Aleksandr Rybalko. All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * This file contains the driver for the DesignWare series USB 2.0 OTG
34 * LIMITATION: Drivers must be bound to all OUT endpoints in the
35 * active configuration for this driver to work properly. Blocking any
36 * OUT endpoint will block all OUT endpoints including the control
37 * endpoint. Usually this is not a problem.
41 * NOTE: Writing to non-existing registers appears to cause an
45 #ifdef USB_GLOBAL_INCLUDE_FILE
46 #include USB_GLOBAL_INCLUDE_FILE
48 #include <sys/stdint.h>
49 #include <sys/stddef.h>
50 #include <sys/param.h>
51 #include <sys/queue.h>
52 #include <sys/types.h>
53 #include <sys/systm.h>
54 #include <sys/kernel.h>
56 #include <sys/module.h>
58 #include <sys/mutex.h>
59 #include <sys/condvar.h>
60 #include <sys/sysctl.h>
62 #include <sys/unistd.h>
63 #include <sys/callout.h>
64 #include <sys/malloc.h>
67 #include <dev/usb/usb.h>
68 #include <dev/usb/usbdi.h>
70 #define USB_DEBUG_VAR dwc_otg_debug
72 #include <dev/usb/usb_core.h>
73 #include <dev/usb/usb_debug.h>
74 #include <dev/usb/usb_busdma.h>
75 #include <dev/usb/usb_process.h>
76 #include <dev/usb/usb_transfer.h>
77 #include <dev/usb/usb_device.h>
78 #include <dev/usb/usb_hub.h>
79 #include <dev/usb/usb_util.h>
81 #include <dev/usb/usb_controller.h>
82 #include <dev/usb/usb_bus.h>
83 #endif /* USB_GLOBAL_INCLUDE_FILE */
85 #include <dev/usb/controller/dwc_otg.h>
86 #include <dev/usb/controller/dwc_otgreg.h>
88 #define DWC_OTG_BUS2SC(bus) \
89 ((struct dwc_otg_softc *)(((uint8_t *)(bus)) - \
90 ((uint8_t *)&(((struct dwc_otg_softc *)0)->sc_bus))))
92 #define DWC_OTG_PC2UDEV(pc) \
93 (USB_DMATAG_TO_XROOT((pc)->tag_parent)->udev)
95 #define DWC_OTG_MSK_GINT_ENABLED \
96 (GINTMSK_ENUMDONEMSK | \
98 GINTMSK_USBSUSPMSK | \
100 GINTMSK_SESSREQINTMSK | \
101 GINTMSK_RXFLVLMSK | \
102 GINTMSK_HCHINTMSK | \
103 GINTMSK_OTGINTMSK | \
106 #define DWC_OTG_MSK_GINT_THREAD_IRQ \
107 (GINTSTS_USBRST | GINTSTS_ENUMDONE | GINTSTS_PRTINT | \
108 GINTSTS_WKUPINT | GINTSTS_USBSUSP | GINTMSK_OTGINTMSK | \
111 #define DWC_OTG_PHY_ULPI 0
112 #define DWC_OTG_PHY_HSIC 1
113 #define DWC_OTG_PHY_INTERNAL 2
115 #ifndef DWC_OTG_PHY_DEFAULT
116 #define DWC_OTG_PHY_DEFAULT DWC_OTG_PHY_ULPI
119 static int dwc_otg_phy_type = DWC_OTG_PHY_DEFAULT;
121 static SYSCTL_NODE(_hw_usb, OID_AUTO, dwc_otg, CTLFLAG_RW, 0, "USB DWC OTG");
122 SYSCTL_INT(_hw_usb_dwc_otg, OID_AUTO, phy_type, CTLFLAG_RDTUN,
123 &dwc_otg_phy_type, 0, "DWC OTG PHY TYPE - 0/1/2 - ULPI/HSIC/INTERNAL");
124 TUNABLE_INT("hw.usb.dwc_otg.phy_type", &dwc_otg_phy_type);
127 static int dwc_otg_debug;
129 SYSCTL_INT(_hw_usb_dwc_otg, OID_AUTO, debug, CTLFLAG_RW,
130 &dwc_otg_debug, 0, "DWC OTG debug level");
133 #define DWC_OTG_INTR_ENDPT 1
137 struct usb_bus_methods dwc_otg_bus_methods;
138 struct usb_pipe_methods dwc_otg_device_non_isoc_methods;
139 struct usb_pipe_methods dwc_otg_device_isoc_methods;
141 static dwc_otg_cmd_t dwc_otg_setup_rx;
142 static dwc_otg_cmd_t dwc_otg_data_rx;
143 static dwc_otg_cmd_t dwc_otg_data_tx;
144 static dwc_otg_cmd_t dwc_otg_data_tx_sync;
146 static dwc_otg_cmd_t dwc_otg_host_setup_tx;
147 static dwc_otg_cmd_t dwc_otg_host_data_tx;
148 static dwc_otg_cmd_t dwc_otg_host_data_rx;
150 static void dwc_otg_device_done(struct usb_xfer *, usb_error_t);
151 static void dwc_otg_do_poll(struct usb_bus *);
152 static void dwc_otg_standard_done(struct usb_xfer *);
153 static void dwc_otg_root_intr(struct dwc_otg_softc *);
154 static void dwc_otg_interrupt_poll_locked(struct dwc_otg_softc *);
155 static void dwc_otg_host_channel_disable(struct dwc_otg_softc *, uint8_t);
158 * Here is a configuration that the chip supports.
160 static const struct usb_hw_ep_profile dwc_otg_ep_profile[1] = {
163 .max_in_frame_size = 64,/* fixed */
164 .max_out_frame_size = 64, /* fixed */
166 .support_control = 1,
171 dwc_otg_get_hw_ep_profile(struct usb_device *udev,
172 const struct usb_hw_ep_profile **ppf, uint8_t ep_addr)
174 struct dwc_otg_softc *sc;
176 sc = DWC_OTG_BUS2SC(udev->bus);
178 if (ep_addr < sc->sc_dev_ep_max)
179 *ppf = &sc->sc_hw_ep_profile[ep_addr].usb;
185 dwc_otg_tx_fifo_reset(struct dwc_otg_softc *sc, uint32_t value)
190 DWC_OTG_WRITE_4(sc, DOTG_GRSTCTL, value);
192 /* wait for reset to complete */
193 for (temp = 0; temp != 16; temp++) {
194 value = DWC_OTG_READ_4(sc, DOTG_GRSTCTL);
195 if (!(value & (GRSTCTL_TXFFLSH | GRSTCTL_RXFFLSH)))
201 dwc_otg_init_fifo(struct dwc_otg_softc *sc, uint8_t mode)
203 struct dwc_otg_profile *pf;
209 fifo_size = sc->sc_fifo_size;
212 * NOTE: Reserved fixed size area at end of RAM, which must
213 * not be allocated to the FIFOs:
217 if (fifo_size < fifo_regs) {
218 DPRINTF("Too little FIFO\n");
222 /* subtract FIFO regs from total once */
223 fifo_size -= fifo_regs;
225 /* split equally for IN and OUT */
228 /* align to 4 bytes boundary */
231 /* set global receive FIFO size */
232 DWC_OTG_WRITE_4(sc, DOTG_GRXFSIZ, fifo_size / 4);
234 tx_start = fifo_size;
236 if (fifo_size < 64) {
237 DPRINTFN(-1, "Not enough data space for EP0 FIFO.\n");
241 /* disable any leftover host channels */
242 for (x = 0; x != sc->sc_host_ch_max; x++) {
243 if (sc->sc_chan_state[x].wait_sof == 0)
245 dwc_otg_host_channel_disable(sc, x);
248 if (mode == DWC_MODE_HOST) {
250 /* reset active endpoints */
251 sc->sc_active_rx_ep = 0;
253 /* split equally for periodic and non-periodic */
256 /* align to 4 bytes boundary */
259 DWC_OTG_WRITE_4(sc, DOTG_GNPTXFSIZ,
260 ((fifo_size / 4) << 16) |
263 tx_start += fifo_size;
265 for (x = 0; x != sc->sc_host_ch_max; x++) {
266 /* disable all host interrupts */
267 DWC_OTG_WRITE_4(sc, DOTG_HCINTMSK(x),
271 DWC_OTG_WRITE_4(sc, DOTG_HPTXFSIZ,
272 ((fifo_size / 4) << 16) |
275 /* reset host channel state */
276 memset(sc->sc_chan_state, 0, sizeof(sc->sc_chan_state));
278 /* reset FIFO TX levels */
279 sc->sc_tx_cur_p_level = 0;
280 sc->sc_tx_cur_np_level = 0;
282 /* store maximum periodic and non-periodic FIFO TX size */
283 sc->sc_tx_max_size = fifo_size;
285 /* enable all host channel interrupts */
286 DWC_OTG_WRITE_4(sc, DOTG_HAINTMSK,
287 (1U << sc->sc_host_ch_max) - 1U);
290 if (mode == DWC_MODE_DEVICE) {
292 DWC_OTG_WRITE_4(sc, DOTG_GNPTXFSIZ,
293 (0x10 << 16) | (tx_start / 4));
297 /* setup control endpoint profile */
298 sc->sc_hw_ep_profile[0].usb = dwc_otg_ep_profile[0];
300 /* reset active endpoints */
301 sc->sc_active_rx_ep = 1;
303 for (x = 1; x != sc->sc_dev_ep_max; x++) {
305 pf = sc->sc_hw_ep_profile + x;
307 pf->usb.max_out_frame_size = 1024 * 3;
308 pf->usb.is_simplex = 0; /* assume duplex */
309 pf->usb.support_bulk = 1;
310 pf->usb.support_interrupt = 1;
311 pf->usb.support_isochronous = 1;
312 pf->usb.support_out = 1;
314 if (x < sc->sc_dev_in_ep_max) {
317 limit = (x == 1) ? DWC_OTG_MAX_TXN :
318 (DWC_OTG_MAX_TXN / 2);
320 if (fifo_size >= limit) {
321 DWC_OTG_WRITE_4(sc, DOTG_DIEPTXF(x),
322 ((limit / 4) << 16) |
326 pf->usb.max_in_frame_size = 0x200;
327 pf->usb.support_in = 1;
328 pf->max_buffer = limit;
330 } else if (fifo_size >= 0x80) {
331 DWC_OTG_WRITE_4(sc, DOTG_DIEPTXF(x),
332 ((0x80 / 4) << 16) | (tx_start / 4));
335 pf->usb.max_in_frame_size = 0x40;
336 pf->usb.support_in = 1;
339 pf->usb.is_simplex = 1;
340 DWC_OTG_WRITE_4(sc, DOTG_DIEPTXF(x),
341 (0x0 << 16) | (tx_start / 4));
344 pf->usb.is_simplex = 1;
347 DPRINTF("FIFO%d = IN:%d / OUT:%d\n", x,
348 pf->usb.max_in_frame_size,
349 pf->usb.max_out_frame_size);
354 dwc_otg_tx_fifo_reset(sc, GRSTCTL_RXFFLSH);
356 if (mode != DWC_MODE_OTG) {
357 /* reset all TX FIFOs */
358 dwc_otg_tx_fifo_reset(sc,
359 GRSTCTL_TXFIFO(0x10) |
362 /* reset active endpoints */
363 sc->sc_active_rx_ep = 0;
365 /* reset periodic and non-periodic FIFO TX size */
366 sc->sc_tx_max_size = fifo_size;
368 /* reset host channel state */
369 memset(sc->sc_chan_state, 0, sizeof(sc->sc_chan_state));
371 /* reset FIFO TX levels */
372 sc->sc_tx_cur_p_level = 0;
373 sc->sc_tx_cur_np_level = 0;
379 dwc_otg_update_host_frame_interval(struct dwc_otg_softc *sc)
383 * Disabled until further. Assuming that the register is already
384 * programmed correctly by the boot loader.
389 /* setup HOST frame interval register, based on existing value */
390 temp = DWC_OTG_READ_4(sc, DOTG_HFIR) & HFIR_FRINT_MASK;
396 /* figure out nearest X-tal value */
404 if (sc->sc_flags.status_high_speed)
409 DPRINTF("HFIR=0x%08x\n", temp);
411 DWC_OTG_WRITE_4(sc, DOTG_HFIR, temp);
416 dwc_otg_clocks_on(struct dwc_otg_softc *sc)
418 if (sc->sc_flags.clocks_off &&
419 sc->sc_flags.port_powered) {
423 /* TODO - platform specific */
425 sc->sc_flags.clocks_off = 0;
430 dwc_otg_clocks_off(struct dwc_otg_softc *sc)
432 if (!sc->sc_flags.clocks_off) {
436 /* TODO - platform specific */
438 sc->sc_flags.clocks_off = 1;
443 dwc_otg_pull_up(struct dwc_otg_softc *sc)
447 /* pullup D+, if possible */
449 if (!sc->sc_flags.d_pulled_up &&
450 sc->sc_flags.port_powered) {
451 sc->sc_flags.d_pulled_up = 1;
453 temp = DWC_OTG_READ_4(sc, DOTG_DCTL);
454 temp &= ~DCTL_SFTDISCON;
455 DWC_OTG_WRITE_4(sc, DOTG_DCTL, temp);
460 dwc_otg_pull_down(struct dwc_otg_softc *sc)
464 /* pulldown D+, if possible */
466 if (sc->sc_flags.d_pulled_up) {
467 sc->sc_flags.d_pulled_up = 0;
469 temp = DWC_OTG_READ_4(sc, DOTG_DCTL);
470 temp |= DCTL_SFTDISCON;
471 DWC_OTG_WRITE_4(sc, DOTG_DCTL, temp);
476 dwc_otg_enable_sof_irq(struct dwc_otg_softc *sc)
478 /* In device mode we don't use the SOF interrupt */
479 if (sc->sc_flags.status_device_mode != 0 ||
480 (sc->sc_irq_mask & GINTMSK_SOFMSK) != 0)
482 sc->sc_irq_mask |= GINTMSK_SOFMSK;
483 DWC_OTG_WRITE_4(sc, DOTG_GINTMSK, sc->sc_irq_mask);
487 dwc_otg_resume_irq(struct dwc_otg_softc *sc)
489 if (sc->sc_flags.status_suspend) {
490 /* update status bits */
491 sc->sc_flags.status_suspend = 0;
492 sc->sc_flags.change_suspend = 1;
494 if (sc->sc_flags.status_device_mode) {
496 * Disable resume interrupt and enable suspend
499 sc->sc_irq_mask &= ~GINTMSK_WKUPINTMSK;
500 sc->sc_irq_mask |= GINTMSK_USBSUSPMSK;
501 DWC_OTG_WRITE_4(sc, DOTG_GINTMSK, sc->sc_irq_mask);
504 /* complete root HUB interrupt endpoint */
505 dwc_otg_root_intr(sc);
510 dwc_otg_suspend_irq(struct dwc_otg_softc *sc)
512 if (!sc->sc_flags.status_suspend) {
513 /* update status bits */
514 sc->sc_flags.status_suspend = 1;
515 sc->sc_flags.change_suspend = 1;
517 if (sc->sc_flags.status_device_mode) {
519 * Disable suspend interrupt and enable resume
522 sc->sc_irq_mask &= ~GINTMSK_USBSUSPMSK;
523 sc->sc_irq_mask |= GINTMSK_WKUPINTMSK;
524 DWC_OTG_WRITE_4(sc, DOTG_GINTMSK, sc->sc_irq_mask);
527 /* complete root HUB interrupt endpoint */
528 dwc_otg_root_intr(sc);
533 dwc_otg_wakeup_peer(struct dwc_otg_softc *sc)
535 if (!sc->sc_flags.status_suspend)
538 DPRINTFN(5, "Remote wakeup\n");
540 if (sc->sc_flags.status_device_mode) {
543 /* enable remote wakeup signalling */
544 temp = DWC_OTG_READ_4(sc, DOTG_DCTL);
545 temp |= DCTL_RMTWKUPSIG;
546 DWC_OTG_WRITE_4(sc, DOTG_DCTL, temp);
548 /* Wait 8ms for remote wakeup to complete. */
549 usb_pause_mtx(&sc->sc_bus.bus_mtx, hz / 125);
551 temp &= ~DCTL_RMTWKUPSIG;
552 DWC_OTG_WRITE_4(sc, DOTG_DCTL, temp);
554 /* enable USB port */
555 DWC_OTG_WRITE_4(sc, DOTG_PCGCCTL, 0);
558 usb_pause_mtx(&sc->sc_bus.bus_mtx, hz / 100);
561 sc->sc_hprt_val |= HPRT_PRTRES;
562 DWC_OTG_WRITE_4(sc, DOTG_HPRT, sc->sc_hprt_val);
564 /* Wait 100ms for resume signalling to complete. */
565 usb_pause_mtx(&sc->sc_bus.bus_mtx, hz / 10);
567 /* clear suspend and resume */
568 sc->sc_hprt_val &= ~(HPRT_PRTSUSP | HPRT_PRTRES);
569 DWC_OTG_WRITE_4(sc, DOTG_HPRT, sc->sc_hprt_val);
572 usb_pause_mtx(&sc->sc_bus.bus_mtx, hz / 250);
575 /* need to fake resume IRQ */
576 dwc_otg_resume_irq(sc);
580 dwc_otg_set_address(struct dwc_otg_softc *sc, uint8_t addr)
584 DPRINTFN(5, "addr=%d\n", addr);
586 temp = DWC_OTG_READ_4(sc, DOTG_DCFG);
587 temp &= ~DCFG_DEVADDR_SET(0x7F);
588 temp |= DCFG_DEVADDR_SET(addr);
589 DWC_OTG_WRITE_4(sc, DOTG_DCFG, temp);
593 dwc_otg_common_rx_ack(struct dwc_otg_softc *sc)
595 DPRINTFN(5, "RX status clear\n");
597 /* enable RX FIFO level interrupt */
598 sc->sc_irq_mask |= GINTMSK_RXFLVLMSK;
599 DWC_OTG_WRITE_4(sc, DOTG_GINTMSK, sc->sc_irq_mask);
601 /* clear cached status */
602 sc->sc_last_rx_status = 0;
606 dwc_otg_clear_hcint(struct dwc_otg_softc *sc, uint8_t x)
610 /* clear all pending interrupts */
611 hcint = DWC_OTG_READ_4(sc, DOTG_HCINT(x));
612 DWC_OTG_WRITE_4(sc, DOTG_HCINT(x), hcint);
614 /* clear buffered interrupts */
615 sc->sc_chan_state[x].hcint = 0;
619 dwc_otg_host_channel_alloc(struct dwc_otg_softc *sc, struct dwc_otg_td *td, uint8_t is_out)
625 if (td->channel < DWC_OTG_MAX_CHANNELS)
626 return (0); /* already allocated */
628 /* check if device is suspended */
629 if (DWC_OTG_PC2UDEV(td->pc)->flags.self_suspended != 0)
630 return (1); /* busy - cannot transfer data */
632 /* compute needed TX FIFO size */
634 if (td->ep_type == UE_ISOCHRONOUS) {
635 tx_p_size = td->max_packet_size;
637 if (td->hcsplt != 0 && tx_p_size > HCSPLT_XACTLEN_BURST)
638 tx_p_size = HCSPLT_XACTLEN_BURST;
639 if ((sc->sc_tx_cur_p_level + tx_p_size) > sc->sc_tx_max_size) {
640 DPRINTF("Too little FIFO space\n");
641 return (1); /* too little FIFO */
645 tx_np_size = td->max_packet_size;
646 if (td->hcsplt != 0 && tx_np_size > HCSPLT_XACTLEN_BURST)
647 tx_np_size = HCSPLT_XACTLEN_BURST;
648 if ((sc->sc_tx_cur_np_level + tx_np_size) > sc->sc_tx_max_size) {
649 DPRINTF("Too little FIFO space\n");
650 return (1); /* too little FIFO */
654 /* not a TX transaction */
659 for (x = 0; x != sc->sc_host_ch_max; x++) {
660 if (sc->sc_chan_state[x].allocated != 0)
662 /* check if channel is still enabled */
663 if (sc->sc_chan_state[x].wait_sof != 0)
666 sc->sc_chan_state[x].allocated = 1;
667 sc->sc_chan_state[x].tx_p_size = tx_p_size;
668 sc->sc_chan_state[x].tx_np_size = tx_np_size;
670 /* keep track of used TX FIFO, if any */
671 sc->sc_tx_cur_p_level += tx_p_size;
672 sc->sc_tx_cur_np_level += tx_np_size;
674 /* clear interrupts */
675 dwc_otg_clear_hcint(sc, x);
677 DPRINTF("CH=%d HCCHAR=0x%08x "
678 "HCSPLT=0x%08x\n", x, td->hcchar, td->hcsplt);
680 /* set active channel */
681 sc->sc_active_rx_ep |= (1 << x);
686 return (0); /* allocated */
689 dwc_otg_enable_sof_irq(sc);
690 return (1); /* busy */
694 dwc_otg_host_channel_free(struct dwc_otg_softc *sc, struct dwc_otg_td *td)
698 if (td->channel >= DWC_OTG_MAX_CHANNELS)
699 return; /* already freed */
703 td->channel = DWC_OTG_MAX_CHANNELS;
705 DPRINTF("CH=%d\n", x);
708 * We need to let programmed host channels run till complete
709 * else the host channel will stop functioning. Assume that
710 * after a fixed given amount of time the host channel is no
711 * longer doing any USB traffic:
713 if (td->ep_type == UE_ISOCHRONOUS) {
714 /* double buffered */
715 sc->sc_chan_state[x].wait_sof = DWC_OTG_SLOT_IDLE_MAX;
717 /* single buffered */
718 sc->sc_chan_state[x].wait_sof = DWC_OTG_SLOT_IDLE_MIN;
721 sc->sc_chan_state[x].allocated = 0;
723 /* ack any pending messages */
724 if (sc->sc_last_rx_status != 0 &&
725 GRXSTSRD_CHNUM_GET(sc->sc_last_rx_status) == x) {
726 dwc_otg_common_rx_ack(sc);
729 /* clear active channel */
730 sc->sc_active_rx_ep &= ~(1 << x);
734 dwc_otg_host_dump_rx(struct dwc_otg_softc *sc, struct dwc_otg_td *td)
736 /* dump any pending messages */
737 if (sc->sc_last_rx_status != 0) {
738 if (td->channel < DWC_OTG_MAX_CHANNELS &&
739 td->channel == GRXSTSRD_CHNUM_GET(sc->sc_last_rx_status)) {
740 dwc_otg_common_rx_ack(sc);
746 dwc_otg_host_setup_tx(struct dwc_otg_softc *sc, struct dwc_otg_td *td)
748 struct usb_device_request req __aligned(4);
753 dwc_otg_host_dump_rx(sc, td);
755 if (td->channel < DWC_OTG_MAX_CHANNELS) {
756 hcint = sc->sc_chan_state[td->channel].hcint;
758 DPRINTF("CH=%d ST=%d HCINT=0x%08x HCCHAR=0x%08x HCTSIZ=0x%08x\n",
759 td->channel, td->state, hcint,
760 DWC_OTG_READ_4(sc, DOTG_HCCHAR(td->channel)),
761 DWC_OTG_READ_4(sc, DOTG_HCTSIZ(td->channel)));
767 if (hcint & (HCINT_RETRY |
768 HCINT_ACK | HCINT_NYET)) {
769 /* give success bits priority over failure bits */
770 } else if (hcint & HCINT_STALL) {
771 DPRINTF("CH=%d STALL\n", td->channel);
775 } else if (hcint & HCINT_ERRORS) {
776 DPRINTF("CH=%d ERROR\n", td->channel);
778 if (td->hcsplt != 0 || td->errcnt >= 3) {
784 if (hcint & (HCINT_ERRORS | HCINT_RETRY |
785 HCINT_ACK | HCINT_NYET)) {
786 if (!(hcint & HCINT_ERRORS))
792 case DWC_CHAN_ST_START:
795 case DWC_CHAN_ST_WAIT_ANE:
796 if (hcint & (HCINT_RETRY | HCINT_ERRORS)) {
798 td->tt_scheduled = 0;
800 } else if (hcint & (HCINT_ACK | HCINT_NYET)) {
801 td->offset += td->tx_bytes;
802 td->remainder -= td->tx_bytes;
804 td->tt_scheduled = 0;
809 case DWC_CHAN_ST_WAIT_S_ANE:
810 if (hcint & (HCINT_RETRY | HCINT_ERRORS)) {
812 td->tt_scheduled = 0;
814 } else if (hcint & (HCINT_ACK | HCINT_NYET)) {
819 case DWC_CHAN_ST_WAIT_C_ANE:
820 if (hcint & HCINT_NYET) {
822 } else if (hcint & (HCINT_RETRY | HCINT_ERRORS)) {
824 td->tt_scheduled = 0;
826 } else if (hcint & HCINT_ACK) {
827 td->offset += td->tx_bytes;
828 td->remainder -= td->tx_bytes;
834 case DWC_CHAN_ST_WAIT_C_PKT:
843 /* free existing channel, if any */
844 dwc_otg_host_channel_free(sc, td);
846 if (sizeof(req) != td->remainder) {
851 if (td->hcsplt != 0) {
852 delta = td->tt_start_slot - sc->sc_last_frame_num - 1;
853 if (td->tt_scheduled == 0 || delta < DWC_OTG_TT_SLOT_MAX) {
854 td->state = DWC_CHAN_ST_START;
857 delta = sc->sc_last_frame_num - td->tt_start_slot;
860 td->tt_scheduled = 0;
861 td->state = DWC_CHAN_ST_START;
866 /* allocate a new channel */
867 if (dwc_otg_host_channel_alloc(sc, td, 1)) {
868 td->state = DWC_CHAN_ST_START;
872 if (td->hcsplt != 0) {
873 td->hcsplt &= ~HCSPLT_COMPSPLT;
874 td->state = DWC_CHAN_ST_WAIT_S_ANE;
876 td->state = DWC_CHAN_ST_WAIT_ANE;
879 usbd_copy_out(td->pc, 0, &req, sizeof(req));
881 DWC_OTG_WRITE_4(sc, DOTG_HCTSIZ(td->channel),
882 (sizeof(req) << HCTSIZ_XFERSIZE_SHIFT) |
883 (1 << HCTSIZ_PKTCNT_SHIFT) |
884 (HCTSIZ_PID_SETUP << HCTSIZ_PID_SHIFT));
886 DWC_OTG_WRITE_4(sc, DOTG_HCSPLT(td->channel), td->hcsplt);
889 hcchar &= ~(HCCHAR_EPDIR_IN | HCCHAR_EPTYPE_MASK);
890 hcchar |= UE_CONTROL << HCCHAR_EPTYPE_SHIFT;
892 /* must enable channel before writing data to FIFO */
893 DWC_OTG_WRITE_4(sc, DOTG_HCCHAR(td->channel), hcchar);
895 /* transfer data into FIFO */
896 bus_space_write_region_4(sc->sc_io_tag, sc->sc_io_hdl,
897 DOTG_DFIFO(td->channel), (uint32_t *)&req, sizeof(req) / 4);
899 /* wait until next slot before trying complete split */
900 td->tt_complete_slot = sc->sc_last_frame_num + 1;
902 /* store number of bytes transmitted */
903 td->tx_bytes = sizeof(req);
907 /* free existing channel, if any */
908 dwc_otg_host_channel_free(sc, td);
910 delta = td->tt_complete_slot - sc->sc_last_frame_num - 1;
911 if (td->tt_scheduled == 0 || delta < DWC_OTG_TT_SLOT_MAX) {
912 td->state = DWC_CHAN_ST_WAIT_C_PKT;
915 delta = sc->sc_last_frame_num - td->tt_start_slot;
916 if (delta > DWC_OTG_TT_SLOT_MAX) {
917 /* we missed the service interval */
918 if (td->ep_type != UE_ISOCHRONOUS)
922 /* allocate a new channel */
923 if (dwc_otg_host_channel_alloc(sc, td, 0)) {
924 td->state = DWC_CHAN_ST_WAIT_C_PKT;
928 /* wait until next slot before trying complete split */
929 td->tt_complete_slot = sc->sc_last_frame_num + 1;
931 td->hcsplt |= HCSPLT_COMPSPLT;
932 td->state = DWC_CHAN_ST_WAIT_C_ANE;
934 DWC_OTG_WRITE_4(sc, DOTG_HCTSIZ(td->channel),
935 (HCTSIZ_PID_SETUP << HCTSIZ_PID_SHIFT));
937 DWC_OTG_WRITE_4(sc, DOTG_HCSPLT(td->channel), td->hcsplt);
940 hcchar &= ~(HCCHAR_EPDIR_IN | HCCHAR_EPTYPE_MASK);
941 hcchar |= UE_CONTROL << HCCHAR_EPTYPE_SHIFT;
943 /* must enable channel before writing data to FIFO */
944 DWC_OTG_WRITE_4(sc, DOTG_HCCHAR(td->channel), hcchar);
947 return (1); /* busy */
950 dwc_otg_host_channel_free(sc, td);
951 return (0); /* complete */
955 dwc_otg_setup_rx(struct dwc_otg_softc *sc, struct dwc_otg_td *td)
957 struct usb_device_request req __aligned(4);
961 /* check endpoint status */
963 if (sc->sc_last_rx_status == 0)
966 if (GRXSTSRD_CHNUM_GET(sc->sc_last_rx_status) != 0)
969 if ((sc->sc_last_rx_status & GRXSTSRD_PKTSTS_MASK) !=
971 if ((sc->sc_last_rx_status & GRXSTSRD_PKTSTS_MASK) !=
972 GRXSTSRD_STP_COMPLETE || td->remainder != 0) {
974 dwc_otg_common_rx_ack(sc);
978 dwc_otg_common_rx_ack(sc);
979 return (0); /* complete */
982 if ((sc->sc_last_rx_status & GRXSTSRD_DPID_MASK) !=
983 GRXSTSRD_DPID_DATA0) {
985 dwc_otg_common_rx_ack(sc);
989 DPRINTFN(5, "GRXSTSR=0x%08x\n", sc->sc_last_rx_status);
991 /* clear did stall */
994 /* get the packet byte count */
995 count = GRXSTSRD_BCNT_GET(sc->sc_last_rx_status);
997 if (count != sizeof(req)) {
998 DPRINTFN(0, "Unsupported SETUP packet "
999 "length, %d bytes\n", count);
1001 dwc_otg_common_rx_ack(sc);
1005 /* copy in control request */
1006 memcpy(&req, sc->sc_rx_bounce_buffer, sizeof(req));
1008 /* copy data into real buffer */
1009 usbd_copy_in(td->pc, 0, &req, sizeof(req));
1011 td->offset = sizeof(req);
1014 /* sneak peek the set address */
1015 if ((req.bmRequestType == UT_WRITE_DEVICE) &&
1016 (req.bRequest == UR_SET_ADDRESS)) {
1017 /* must write address before ZLP */
1018 dwc_otg_set_address(sc, req.wValue[0] & 0x7F);
1021 /* don't send any data by default */
1022 DWC_OTG_WRITE_4(sc, DOTG_DIEPTSIZ(0), DIEPCTL_EPDIS);
1023 DWC_OTG_WRITE_4(sc, DOTG_DOEPTSIZ(0), DOEPCTL_EPDIS);
1025 /* reset IN endpoint buffer */
1026 dwc_otg_tx_fifo_reset(sc,
1030 /* acknowledge RX status */
1031 dwc_otg_common_rx_ack(sc);
1035 /* abort any ongoing transfer, before enabling again */
1036 if (!td->did_stall) {
1039 DPRINTFN(5, "stalling IN and OUT direction\n");
1041 temp = sc->sc_out_ctl[0];
1043 /* set stall after enabling endpoint */
1044 DWC_OTG_WRITE_4(sc, DOTG_DOEPCTL(0),
1045 temp | DOEPCTL_STALL);
1047 temp = sc->sc_in_ctl[0];
1049 /* set stall assuming endpoint is enabled */
1050 DWC_OTG_WRITE_4(sc, DOTG_DIEPCTL(0),
1051 temp | DIEPCTL_STALL);
1053 return (1); /* not complete */
1057 dwc_otg_host_rate_check_interrupt(struct dwc_otg_softc *sc, struct dwc_otg_td *td)
1061 delta = sc->sc_tmr_val - td->tmr_val;
1063 return (1); /* busy */
1065 td->tmr_val = sc->sc_tmr_val + td->tmr_res;
1067 /* set toggle, if any */
1068 if (td->set_toggle) {
1076 dwc_otg_host_rate_check(struct dwc_otg_softc *sc, struct dwc_otg_td *td)
1078 if (td->ep_type == UE_ISOCHRONOUS) {
1079 /* non TT isochronous traffic */
1080 if ((td->tmr_val != 0) ||
1081 (sc->sc_last_frame_num & (td->tmr_res - 1))) {
1084 td->tmr_val = 1; /* executed */
1087 } else if (td->ep_type == UE_INTERRUPT) {
1088 if (!td->tt_scheduled)
1090 td->tt_scheduled = 0;
1091 } else if (td->did_nak >= DWC_OTG_NAK_MAX) {
1093 } else if (td->set_toggle) {
1103 dwc_otg_host_data_rx_sub(struct dwc_otg_softc *sc, struct dwc_otg_td *td)
1108 /* check endpoint status */
1109 if (sc->sc_last_rx_status == 0)
1112 channel = td->channel;
1113 if (channel >= DWC_OTG_MAX_CHANNELS)
1116 if (GRXSTSRD_CHNUM_GET(sc->sc_last_rx_status) != channel)
1119 switch (sc->sc_last_rx_status & GRXSTSRD_PKTSTS_MASK) {
1120 case GRXSTSRH_IN_DATA:
1122 DPRINTF("DATA ST=%d STATUS=0x%08x\n",
1123 (int)td->state, (int)sc->sc_last_rx_status);
1125 if (sc->sc_chan_state[channel].hcint & HCINT_SOFTWARE_ONLY) {
1127 * When using SPLIT transactions on interrupt
1128 * endpoints, sometimes data occurs twice.
1130 DPRINTF("Data already received\n");
1134 /* get the packet byte count */
1135 count = GRXSTSRD_BCNT_GET(sc->sc_last_rx_status);
1137 /* check for isochronous transfer or high-speed bandwidth endpoint */
1138 if (td->ep_type == UE_ISOCHRONOUS || td->max_packet_count > 1) {
1139 if ((sc->sc_last_rx_status & GRXSTSRD_DPID_MASK) != GRXSTSRD_DPID_DATA0) {
1140 td->tt_xactpos = HCSPLT_XACTPOS_MIDDLE;
1142 td->tt_xactpos = HCSPLT_XACTPOS_BEGIN;
1144 /* verify the packet byte count */
1145 if (count < td->max_packet_size) {
1146 /* we have a short packet */
1153 /* verify the packet byte count */
1154 if (count != td->max_packet_size) {
1155 if (count < td->max_packet_size) {
1156 /* we have a short packet */
1160 /* invalid USB packet */
1164 dwc_otg_common_rx_ack(sc);
1169 td->tt_scheduled = 0;
1172 /* verify the packet byte count */
1173 if (count > td->remainder) {
1174 /* invalid USB packet */
1178 dwc_otg_common_rx_ack(sc);
1182 usbd_copy_in(td->pc, td->offset,
1183 sc->sc_rx_bounce_buffer, count);
1185 td->remainder -= count;
1186 td->offset += count;
1187 sc->sc_chan_state[channel].hcint |= HCINT_SOFTWARE_ONLY;
1193 dwc_otg_common_rx_ack(sc);
1201 dwc_otg_host_data_rx(struct dwc_otg_softc *sc, struct dwc_otg_td *td)
1208 channel = td->channel;
1210 if (channel < DWC_OTG_MAX_CHANNELS) {
1211 hcint = sc->sc_chan_state[channel].hcint;
1213 DPRINTF("CH=%d ST=%d HCINT=0x%08x HCCHAR=0x%08x HCTSIZ=0x%08x\n",
1214 channel, td->state, hcint,
1215 DWC_OTG_READ_4(sc, DOTG_HCCHAR(channel)),
1216 DWC_OTG_READ_4(sc, DOTG_HCTSIZ(channel)));
1218 /* check interrupt bits */
1219 if (hcint & (HCINT_RETRY |
1220 HCINT_ACK | HCINT_NYET)) {
1221 /* give success bits priority over failure bits */
1222 } else if (hcint & HCINT_STALL) {
1223 DPRINTF("CH=%d STALL\n", channel);
1224 td->error_stall = 1;
1227 } else if (hcint & HCINT_ERRORS) {
1228 DPRINTF("CH=%d ERROR\n", channel);
1230 if (td->hcsplt != 0 || td->errcnt >= 3) {
1231 if (td->ep_type != UE_ISOCHRONOUS) {
1238 /* check channels for data, if any */
1239 if (dwc_otg_host_data_rx_sub(sc, td))
1242 /* refresh interrupt status */
1243 hcint = sc->sc_chan_state[channel].hcint;
1245 if (hcint & (HCINT_ERRORS | HCINT_RETRY |
1246 HCINT_ACK | HCINT_NYET)) {
1247 if (!(hcint & HCINT_ERRORS))
1254 switch (td->state) {
1255 case DWC_CHAN_ST_START:
1256 if (td->hcsplt != 0)
1261 case DWC_CHAN_ST_WAIT_ANE:
1262 if (hcint & (HCINT_RETRY | HCINT_ERRORS)) {
1263 if (td->ep_type == UE_INTERRUPT) {
1265 * The USB specification does not
1266 * mandate a particular data toggle
1267 * value for USB INTERRUPT
1268 * transfers. Switch the data toggle
1269 * value to receive the packet
1272 if (hcint & HCINT_DATATGLERR) {
1273 DPRINTF("Retrying packet due to "
1274 "data toggle error\n");
1280 td->tt_scheduled = 0;
1281 if (td->hcsplt != 0)
1285 } else if (hcint & HCINT_NYET) {
1286 if (td->hcsplt != 0) {
1290 /* not a valid token for IN endpoints */
1294 } else if (hcint & HCINT_ACK) {
1295 /* wait for data - ACK arrived first */
1296 if (!(hcint & HCINT_SOFTWARE_ONLY))
1299 if (td->ep_type == UE_ISOCHRONOUS) {
1300 /* check if we are complete */
1301 if ((td->remainder == 0) ||
1302 (td->tt_xactpos == HCSPLT_XACTPOS_BEGIN)) {
1305 /* get another packet */
1308 /* check if we are complete */
1309 if ((td->remainder == 0) || (td->got_short != 0)) {
1314 * Else need to receive a zero length
1318 td->tt_scheduled = 0;
1320 if (td->hcsplt != 0)
1328 case DWC_CHAN_ST_WAIT_S_ANE:
1330 * NOTE: The DWC OTG hardware provides a fake ACK in
1331 * case of interrupt and isochronous transfers:
1333 if (hcint & (HCINT_RETRY | HCINT_ERRORS)) {
1335 td->tt_scheduled = 0;
1337 } else if (hcint & HCINT_NYET) {
1338 td->tt_scheduled = 0;
1340 } else if (hcint & HCINT_ACK) {
1346 case DWC_CHAN_ST_WAIT_C_PKT:
1355 /* free existing channel, if any */
1356 dwc_otg_host_channel_free(sc, td);
1358 if (td->hcsplt != 0) {
1359 delta = td->tt_complete_slot - sc->sc_last_frame_num - 1;
1360 if (td->tt_scheduled == 0 || delta < DWC_OTG_TT_SLOT_MAX) {
1361 td->state = DWC_CHAN_ST_WAIT_C_PKT;
1364 delta = sc->sc_last_frame_num - td->tt_start_slot;
1365 if (delta > DWC_OTG_TT_SLOT_MAX) {
1366 if (td->ep_type != UE_ISOCHRONOUS) {
1367 /* we missed the service interval */
1372 /* complete split */
1373 td->hcsplt |= HCSPLT_COMPSPLT;
1374 } else if (td->tt_xactpos == HCSPLT_XACTPOS_BEGIN &&
1375 dwc_otg_host_rate_check(sc, td)) {
1376 td->state = DWC_CHAN_ST_WAIT_C_PKT;
1380 /* allocate a new channel */
1381 if (dwc_otg_host_channel_alloc(sc, td, 0)) {
1382 td->state = DWC_CHAN_ST_WAIT_C_PKT;
1386 channel = td->channel;
1388 /* set toggle, if any */
1389 if (td->set_toggle) {
1394 td->state = DWC_CHAN_ST_WAIT_ANE;
1396 /* receive one packet */
1397 DWC_OTG_WRITE_4(sc, DOTG_HCTSIZ(channel),
1398 (td->max_packet_size << HCTSIZ_XFERSIZE_SHIFT) |
1399 (1 << HCTSIZ_PKTCNT_SHIFT) |
1400 (td->toggle ? (HCTSIZ_PID_DATA1 << HCTSIZ_PID_SHIFT) :
1401 (HCTSIZ_PID_DATA0 << HCTSIZ_PID_SHIFT)));
1403 DWC_OTG_WRITE_4(sc, DOTG_HCSPLT(channel), td->hcsplt);
1405 hcchar = td->hcchar;
1406 hcchar |= HCCHAR_EPDIR_IN;
1408 /* receive complete split ASAP */
1409 if ((sc->sc_last_frame_num & 1) != 0)
1410 hcchar |= HCCHAR_ODDFRM;
1412 hcchar &= ~HCCHAR_ODDFRM;
1414 /* must enable channel before data can be received */
1415 DWC_OTG_WRITE_4(sc, DOTG_HCCHAR(channel), hcchar);
1417 /* wait until next slot before trying complete split */
1418 td->tt_complete_slot = sc->sc_last_frame_num + 1;
1423 /* free existing channel(s), if any */
1424 dwc_otg_host_channel_free(sc, td);
1426 delta = td->tt_start_slot - sc->sc_last_frame_num - 1;
1427 if (td->tt_scheduled == 0 || delta < DWC_OTG_TT_SLOT_MAX) {
1428 td->state = DWC_CHAN_ST_START;
1431 delta = sc->sc_last_frame_num - td->tt_start_slot;
1434 td->tt_scheduled = 0;
1435 td->state = DWC_CHAN_ST_START;
1439 /* allocate a new channel */
1440 if (dwc_otg_host_channel_alloc(sc, td, 0)) {
1441 td->state = DWC_CHAN_ST_START;
1445 channel = td->channel;
1447 td->hcsplt &= ~HCSPLT_COMPSPLT;
1448 td->state = DWC_CHAN_ST_WAIT_S_ANE;
1450 /* receive one packet */
1451 DWC_OTG_WRITE_4(sc, DOTG_HCTSIZ(channel),
1452 (HCTSIZ_PID_DATA0 << HCTSIZ_PID_SHIFT));
1454 DWC_OTG_WRITE_4(sc, DOTG_HCSPLT(channel), td->hcsplt);
1456 /* send after next SOF event */
1457 if ((sc->sc_last_frame_num & 1) == 0)
1458 td->hcchar |= HCCHAR_ODDFRM;
1460 td->hcchar &= ~HCCHAR_ODDFRM;
1462 hcchar = td->hcchar;
1463 hcchar |= HCCHAR_EPDIR_IN;
1465 /* wait until next slot before trying complete split */
1466 td->tt_complete_slot = sc->sc_last_frame_num + 1;
1468 /* must enable channel before data can be received */
1469 DWC_OTG_WRITE_4(sc, DOTG_HCCHAR(channel), hcchar);
1471 return (1); /* busy */
1474 dwc_otg_host_channel_free(sc, td);
1475 return (0); /* complete */
1479 dwc_otg_data_rx(struct dwc_otg_softc *sc, struct dwc_otg_td *td)
1487 /* check endpoint status */
1488 if (sc->sc_last_rx_status == 0)
1491 if (GRXSTSRD_CHNUM_GET(sc->sc_last_rx_status) != td->ep_no)
1494 /* check for SETUP packet */
1495 if ((sc->sc_last_rx_status & GRXSTSRD_PKTSTS_MASK) ==
1496 GRXSTSRD_STP_DATA ||
1497 (sc->sc_last_rx_status & GRXSTSRD_PKTSTS_MASK) ==
1498 GRXSTSRD_STP_COMPLETE) {
1499 if (td->remainder == 0) {
1501 * We are actually complete and have
1502 * received the next SETUP
1504 DPRINTFN(5, "faking complete\n");
1505 return (0); /* complete */
1508 * USB Host Aborted the transfer.
1511 return (0); /* complete */
1514 if ((sc->sc_last_rx_status & GRXSTSRD_PKTSTS_MASK) !=
1515 GRXSTSRD_OUT_DATA) {
1517 dwc_otg_common_rx_ack(sc);
1521 /* get the packet byte count */
1522 count = GRXSTSRD_BCNT_GET(sc->sc_last_rx_status);
1524 /* verify the packet byte count */
1525 if (count != td->max_packet_size) {
1526 if (count < td->max_packet_size) {
1527 /* we have a short packet */
1531 /* invalid USB packet */
1535 dwc_otg_common_rx_ack(sc);
1536 return (0); /* we are complete */
1539 /* verify the packet byte count */
1540 if (count > td->remainder) {
1541 /* invalid USB packet */
1545 dwc_otg_common_rx_ack(sc);
1546 return (0); /* we are complete */
1549 usbd_copy_in(td->pc, td->offset, sc->sc_rx_bounce_buffer, count);
1550 td->remainder -= count;
1551 td->offset += count;
1554 dwc_otg_common_rx_ack(sc);
1556 temp = sc->sc_out_ctl[td->ep_no];
1558 /* check for isochronous mode */
1559 if ((temp & DIEPCTL_EPTYPE_MASK) ==
1560 (DIEPCTL_EPTYPE_ISOC << DIEPCTL_EPTYPE_SHIFT)) {
1561 /* toggle odd or even frame bit */
1562 if (temp & DIEPCTL_SETD1PID) {
1563 temp &= ~DIEPCTL_SETD1PID;
1564 temp |= DIEPCTL_SETD0PID;
1566 temp &= ~DIEPCTL_SETD0PID;
1567 temp |= DIEPCTL_SETD1PID;
1569 sc->sc_out_ctl[td->ep_no] = temp;
1572 /* check if we are complete */
1573 if ((td->remainder == 0) || got_short) {
1574 if (td->short_pkt) {
1575 /* we are complete */
1578 /* else need to receive a zero length packet */
1583 /* enable SETUP and transfer complete interrupt */
1584 if (td->ep_no == 0) {
1585 DWC_OTG_WRITE_4(sc, DOTG_DOEPTSIZ(0),
1586 DXEPTSIZ_SET_MULTI(3) |
1587 DXEPTSIZ_SET_NPKT(1) |
1588 DXEPTSIZ_SET_NBYTES(td->max_packet_size));
1590 /* allow reception of multiple packets */
1591 DWC_OTG_WRITE_4(sc, DOTG_DOEPTSIZ(td->ep_no),
1592 DXEPTSIZ_SET_MULTI(1) |
1593 DXEPTSIZ_SET_NPKT(4) |
1594 DXEPTSIZ_SET_NBYTES(4 *
1595 ((td->max_packet_size + 3) & ~3)));
1597 temp = sc->sc_out_ctl[td->ep_no];
1598 DWC_OTG_WRITE_4(sc, DOTG_DOEPCTL(td->ep_no), temp |
1599 DOEPCTL_EPENA | DOEPCTL_CNAK);
1601 return (1); /* not complete */
1605 dwc_otg_host_data_tx(struct dwc_otg_softc *sc, struct dwc_otg_td *td)
1613 dwc_otg_host_dump_rx(sc, td);
1615 channel = td->channel;
1617 if (channel < DWC_OTG_MAX_CHANNELS) {
1618 hcint = sc->sc_chan_state[channel].hcint;
1620 DPRINTF("CH=%d ST=%d HCINT=0x%08x HCCHAR=0x%08x HCTSIZ=0x%08x\n",
1621 channel, td->state, hcint,
1622 DWC_OTG_READ_4(sc, DOTG_HCCHAR(channel)),
1623 DWC_OTG_READ_4(sc, DOTG_HCTSIZ(channel)));
1625 if (hcint & (HCINT_RETRY |
1626 HCINT_ACK | HCINT_NYET)) {
1627 /* give success bits priority over failure bits */
1628 } else if (hcint & HCINT_STALL) {
1629 DPRINTF("CH=%d STALL\n", channel);
1630 td->error_stall = 1;
1633 } else if (hcint & HCINT_ERRORS) {
1634 DPRINTF("CH=%d ERROR\n", channel);
1636 if (td->hcsplt != 0 || td->errcnt >= 3) {
1642 if (hcint & (HCINT_ERRORS | HCINT_RETRY |
1643 HCINT_ACK | HCINT_NYET)) {
1645 if (!(hcint & HCINT_ERRORS))
1652 switch (td->state) {
1653 case DWC_CHAN_ST_START:
1656 case DWC_CHAN_ST_WAIT_ANE:
1657 if (hcint & (HCINT_RETRY | HCINT_ERRORS)) {
1659 td->tt_scheduled = 0;
1661 } else if (hcint & (HCINT_ACK | HCINT_NYET)) {
1662 td->offset += td->tx_bytes;
1663 td->remainder -= td->tx_bytes;
1666 td->tt_scheduled = 0;
1668 /* check remainder */
1669 if (td->remainder == 0) {
1674 * Else we need to transmit a short
1682 case DWC_CHAN_ST_WAIT_S_ANE:
1683 if (hcint & (HCINT_RETRY | HCINT_ERRORS)) {
1685 td->tt_scheduled = 0;
1687 } else if (hcint & (HCINT_ACK | HCINT_NYET)) {
1693 case DWC_CHAN_ST_WAIT_C_ANE:
1694 if (hcint & HCINT_NYET) {
1696 } else if (hcint & (HCINT_RETRY | HCINT_ERRORS)) {
1698 td->tt_scheduled = 0;
1700 } else if (hcint & HCINT_ACK) {
1701 td->offset += td->tx_bytes;
1702 td->remainder -= td->tx_bytes;
1705 td->tt_scheduled = 0;
1707 /* check remainder */
1708 if (td->remainder == 0) {
1712 /* else we need to transmit a short packet */
1718 case DWC_CHAN_ST_WAIT_C_PKT:
1721 case DWC_CHAN_ST_TX_WAIT_ISOC:
1723 /* Check if isochronous OUT traffic is complete */
1724 if ((hcint & HCINT_HCH_DONE_MASK) == 0)
1727 td->offset += td->tx_bytes;
1728 td->remainder -= td->tx_bytes;
1730 if (td->hcsplt != 0 || td->remainder == 0)
1733 /* check for next packet */
1734 if (td->max_packet_count > 1)
1737 /* free existing channel, if any */
1738 dwc_otg_host_channel_free(sc, td);
1740 td->state = DWC_CHAN_ST_TX_PKT_ISOC;
1744 case DWC_CHAN_ST_TX_PKT_ISOC:
1745 if (dwc_otg_host_channel_alloc(sc, td, 1))
1747 channel = td->channel;
1755 /* free existing channel(s), if any */
1756 dwc_otg_host_channel_free(sc, td);
1758 if (td->hcsplt != 0) {
1759 delta = td->tt_start_slot - sc->sc_last_frame_num - 1;
1760 if (td->tt_scheduled == 0 || delta < DWC_OTG_TT_SLOT_MAX) {
1761 td->state = DWC_CHAN_ST_START;
1764 delta = sc->sc_last_frame_num - td->tt_start_slot;
1767 td->tt_scheduled = 0;
1768 td->state = DWC_CHAN_ST_START;
1771 } else if (dwc_otg_host_rate_check(sc, td)) {
1772 td->state = DWC_CHAN_ST_START;
1776 /* allocate a new channel */
1777 if (dwc_otg_host_channel_alloc(sc, td, 1)) {
1778 td->state = DWC_CHAN_ST_START;
1782 channel = td->channel;
1784 /* set toggle, if any */
1785 if (td->set_toggle) {
1790 if (td->ep_type == UE_ISOCHRONOUS) {
1792 /* Isochronous OUT transfers don't have any ACKs */
1793 td->state = DWC_CHAN_ST_TX_WAIT_ISOC;
1794 td->hcsplt &= ~HCSPLT_COMPSPLT;
1795 if (td->hcsplt != 0) {
1796 /* get maximum transfer length */
1797 count = td->remainder;
1798 if (count > HCSPLT_XACTLEN_BURST) {
1799 DPRINTF("TT overflow\n");
1803 /* Update transaction position */
1804 td->hcsplt &= ~HCSPLT_XACTPOS_MASK;
1805 td->hcsplt |= (HCSPLT_XACTPOS_ALL << HCSPLT_XACTPOS_SHIFT);
1807 /* send one packet at a time */
1808 count = td->max_packet_size;
1809 if (td->remainder < count) {
1810 /* we have a short packet */
1812 count = td->remainder;
1815 } else if (td->hcsplt != 0) {
1817 td->hcsplt &= ~HCSPLT_COMPSPLT;
1819 /* Wait for ACK/NAK/ERR from TT */
1820 td->state = DWC_CHAN_ST_WAIT_S_ANE;
1822 /* send one packet at a time */
1823 count = td->max_packet_size;
1824 if (td->remainder < count) {
1825 /* we have a short packet */
1827 count = td->remainder;
1830 /* Wait for ACK/NAK/STALL from device */
1831 td->state = DWC_CHAN_ST_WAIT_ANE;
1833 /* send one packet at a time */
1834 count = td->max_packet_size;
1835 if (td->remainder < count) {
1836 /* we have a short packet */
1838 count = td->remainder;
1842 /* check for High-Speed multi-packets */
1843 if ((td->hcsplt == 0) && (td->max_packet_count > 1)) {
1844 if (td->npkt == 0) {
1845 if (td->remainder >= (3 * td->max_packet_size))
1847 else if (td->remainder >= (2 * td->max_packet_size))
1852 if (td->npkt > td->max_packet_count)
1853 td->npkt = td->max_packet_count;
1855 td->tt_xactpos = 1; /* overload */
1857 if (td->tt_xactpos == td->npkt) {
1858 if (td->npkt == 1) {
1859 DWC_OTG_WRITE_4(sc, DOTG_HCTSIZ(channel),
1860 (count << HCTSIZ_XFERSIZE_SHIFT) |
1861 (1 << HCTSIZ_PKTCNT_SHIFT) |
1862 (HCTSIZ_PID_DATA0 << HCTSIZ_PID_SHIFT));
1863 } else if (td->npkt == 2) {
1864 DWC_OTG_WRITE_4(sc, DOTG_HCTSIZ(channel),
1865 (count << HCTSIZ_XFERSIZE_SHIFT) |
1866 (1 << HCTSIZ_PKTCNT_SHIFT) |
1867 (HCTSIZ_PID_DATA1 << HCTSIZ_PID_SHIFT));
1869 DWC_OTG_WRITE_4(sc, DOTG_HCTSIZ(channel),
1870 (count << HCTSIZ_XFERSIZE_SHIFT) |
1871 (1 << HCTSIZ_PKTCNT_SHIFT) |
1872 (HCTSIZ_PID_DATA2 << HCTSIZ_PID_SHIFT));
1876 DWC_OTG_WRITE_4(sc, DOTG_HCTSIZ(channel),
1877 (count << HCTSIZ_XFERSIZE_SHIFT) |
1878 (1 << HCTSIZ_PKTCNT_SHIFT) |
1879 (HCTSIZ_PID_MDATA << HCTSIZ_PID_SHIFT));
1882 /* TODO: HCTSIZ_DOPNG */
1884 DWC_OTG_WRITE_4(sc, DOTG_HCTSIZ(channel),
1885 (count << HCTSIZ_XFERSIZE_SHIFT) |
1886 (1 << HCTSIZ_PKTCNT_SHIFT) |
1887 (td->toggle ? (HCTSIZ_PID_DATA1 << HCTSIZ_PID_SHIFT) :
1888 (HCTSIZ_PID_DATA0 << HCTSIZ_PID_SHIFT)));
1891 DWC_OTG_WRITE_4(sc, DOTG_HCSPLT(channel), td->hcsplt);
1893 hcchar = td->hcchar;
1894 hcchar &= ~HCCHAR_EPDIR_IN;
1896 /* send after next SOF event */
1897 if ((sc->sc_last_frame_num & 1) == 0)
1898 hcchar |= HCCHAR_ODDFRM;
1900 hcchar &= ~HCCHAR_ODDFRM;
1902 /* must enable before writing data to FIFO */
1903 DWC_OTG_WRITE_4(sc, DOTG_HCCHAR(channel), hcchar);
1907 /* clear topmost word before copy */
1908 sc->sc_tx_bounce_buffer[(count - 1) / 4] = 0;
1911 usbd_copy_out(td->pc, td->offset,
1912 sc->sc_tx_bounce_buffer, count);
1914 /* transfer data into FIFO */
1915 bus_space_write_region_4(sc->sc_io_tag, sc->sc_io_hdl,
1916 DOTG_DFIFO(channel),
1917 sc->sc_tx_bounce_buffer, (count + 3) / 4);
1920 /* store number of bytes transmitted */
1921 td->tx_bytes = count;
1925 /* free existing channel, if any */
1926 dwc_otg_host_channel_free(sc, td);
1928 delta = td->tt_complete_slot - sc->sc_last_frame_num - 1;
1929 if (td->tt_scheduled == 0 || delta < DWC_OTG_TT_SLOT_MAX) {
1930 td->state = DWC_CHAN_ST_WAIT_C_PKT;
1933 delta = sc->sc_last_frame_num - td->tt_start_slot;
1934 if (delta > DWC_OTG_TT_SLOT_MAX) {
1935 /* we missed the service interval */
1936 if (td->ep_type != UE_ISOCHRONOUS)
1941 /* allocate a new channel */
1942 if (dwc_otg_host_channel_alloc(sc, td, 0)) {
1943 td->state = DWC_CHAN_ST_WAIT_C_PKT;
1947 channel = td->channel;
1949 td->hcsplt |= HCSPLT_COMPSPLT;
1950 td->state = DWC_CHAN_ST_WAIT_C_ANE;
1952 DWC_OTG_WRITE_4(sc, DOTG_HCTSIZ(channel),
1953 (HCTSIZ_PID_DATA0 << HCTSIZ_PID_SHIFT));
1955 DWC_OTG_WRITE_4(sc, DOTG_HCSPLT(channel), td->hcsplt);
1957 hcchar = td->hcchar;
1958 hcchar &= ~HCCHAR_EPDIR_IN;
1960 /* receive complete split ASAP */
1961 if ((sc->sc_last_frame_num & 1) != 0)
1962 hcchar |= HCCHAR_ODDFRM;
1964 hcchar &= ~HCCHAR_ODDFRM;
1966 /* must enable channel before data can be received */
1967 DWC_OTG_WRITE_4(sc, DOTG_HCCHAR(channel), hcchar);
1969 /* wait until next slot before trying complete split */
1970 td->tt_complete_slot = sc->sc_last_frame_num + 1;
1972 return (1); /* busy */
1975 dwc_otg_host_channel_free(sc, td);
1976 return (0); /* complete */
1980 dwc_otg_data_tx(struct dwc_otg_softc *sc, struct dwc_otg_td *td)
1982 uint32_t max_buffer;
1989 to = 3; /* don't loop forever! */
1991 max_buffer = sc->sc_hw_ep_profile[td->ep_no].max_buffer;
1994 /* check for for endpoint 0 data */
1996 temp = sc->sc_last_rx_status;
1998 if ((td->ep_no == 0) && (temp != 0) &&
1999 (GRXSTSRD_CHNUM_GET(temp) == 0)) {
2001 if ((temp & GRXSTSRD_PKTSTS_MASK) !=
2002 GRXSTSRD_STP_DATA &&
2003 (temp & GRXSTSRD_PKTSTS_MASK) !=
2004 GRXSTSRD_STP_COMPLETE) {
2006 /* dump data - wrong direction */
2007 dwc_otg_common_rx_ack(sc);
2010 * The current transfer was cancelled
2014 return (0); /* complete */
2018 /* fill in more TX data, if possible */
2019 if (td->tx_bytes != 0) {
2023 /* check if packets have been transferred */
2024 temp = DWC_OTG_READ_4(sc, DOTG_DIEPTSIZ(td->ep_no));
2026 /* get current packet number */
2027 cpkt = DXEPTSIZ_GET_NPKT(temp);
2029 if (cpkt >= td->npkt) {
2032 if (max_buffer != 0) {
2033 fifo_left = (td->npkt - cpkt) *
2034 td->max_packet_size;
2036 if (fifo_left > max_buffer)
2037 fifo_left = max_buffer;
2039 fifo_left = td->max_packet_size;
2043 count = td->tx_bytes;
2044 if (count > fifo_left)
2049 /* clear topmost word before copy */
2050 sc->sc_tx_bounce_buffer[(count - 1) / 4] = 0;
2053 usbd_copy_out(td->pc, td->offset,
2054 sc->sc_tx_bounce_buffer, count);
2056 /* transfer data into FIFO */
2057 bus_space_write_region_4(sc->sc_io_tag, sc->sc_io_hdl,
2058 DOTG_DFIFO(td->ep_no),
2059 sc->sc_tx_bounce_buffer, (count + 3) / 4);
2061 td->tx_bytes -= count;
2062 td->remainder -= count;
2063 td->offset += count;
2066 if (td->tx_bytes != 0)
2069 /* check remainder */
2070 if (td->remainder == 0) {
2072 return (0); /* complete */
2074 /* else we need to transmit a short packet */
2081 /* check if not all packets have been transferred */
2082 temp = DWC_OTG_READ_4(sc, DOTG_DIEPTSIZ(td->ep_no));
2084 if (DXEPTSIZ_GET_NPKT(temp) != 0) {
2086 DPRINTFN(5, "busy ep=%d npkt=%d DIEPTSIZ=0x%08x "
2087 "DIEPCTL=0x%08x\n", td->ep_no,
2088 DXEPTSIZ_GET_NPKT(temp),
2089 temp, DWC_OTG_READ_4(sc, DOTG_DIEPCTL(td->ep_no)));
2094 DPRINTFN(5, "rem=%u ep=%d\n", td->remainder, td->ep_no);
2096 /* try to optimise by sending more data */
2097 if ((max_buffer != 0) && ((td->max_packet_size & 3) == 0)) {
2099 /* send multiple packets at the same time */
2100 mpkt = max_buffer / td->max_packet_size;
2105 count = td->remainder;
2106 if (count > 0x7FFFFF)
2107 count = 0x7FFFFF - (0x7FFFFF % td->max_packet_size);
2109 td->npkt = count / td->max_packet_size;
2112 * NOTE: We could use 0x3FE instead of "mpkt" in the
2113 * check below to get more throughput, but then we
2114 * have a dependency towards non-generic chip features
2115 * to disable the TX-FIFO-EMPTY interrupts on a per
2116 * endpoint basis. Increase the maximum buffer size of
2117 * the IN endpoint to increase the performance.
2119 if (td->npkt > mpkt) {
2121 count = td->max_packet_size * mpkt;
2122 } else if ((count == 0) || (count % td->max_packet_size)) {
2123 /* we are transmitting a short packet */
2128 /* send one packet at a time */
2130 count = td->max_packet_size;
2131 if (td->remainder < count) {
2132 /* we have a short packet */
2134 count = td->remainder;
2138 DWC_OTG_WRITE_4(sc, DOTG_DIEPTSIZ(td->ep_no),
2139 DXEPTSIZ_SET_MULTI(1) |
2140 DXEPTSIZ_SET_NPKT(td->npkt) |
2141 DXEPTSIZ_SET_NBYTES(count));
2143 /* make room for buffering */
2146 temp = sc->sc_in_ctl[td->ep_no];
2148 /* check for isochronous mode */
2149 if ((temp & DIEPCTL_EPTYPE_MASK) ==
2150 (DIEPCTL_EPTYPE_ISOC << DIEPCTL_EPTYPE_SHIFT)) {
2151 /* toggle odd or even frame bit */
2152 if (temp & DIEPCTL_SETD1PID) {
2153 temp &= ~DIEPCTL_SETD1PID;
2154 temp |= DIEPCTL_SETD0PID;
2156 temp &= ~DIEPCTL_SETD0PID;
2157 temp |= DIEPCTL_SETD1PID;
2159 sc->sc_in_ctl[td->ep_no] = temp;
2162 /* must enable before writing data to FIFO */
2163 DWC_OTG_WRITE_4(sc, DOTG_DIEPCTL(td->ep_no), temp |
2164 DIEPCTL_EPENA | DIEPCTL_CNAK);
2166 td->tx_bytes = count;
2168 /* check remainder */
2169 if (td->tx_bytes == 0 &&
2170 td->remainder == 0) {
2172 return (0); /* complete */
2174 /* else we need to transmit a short packet */
2179 return (1); /* not complete */
2183 dwc_otg_data_tx_sync(struct dwc_otg_softc *sc, struct dwc_otg_td *td)
2188 * If all packets are transferred we are complete:
2190 temp = DWC_OTG_READ_4(sc, DOTG_DIEPTSIZ(td->ep_no));
2192 /* check that all packets have been transferred */
2193 if (DXEPTSIZ_GET_NPKT(temp) != 0) {
2194 DPRINTFN(5, "busy ep=%d\n", td->ep_no);
2201 /* we only want to know if there is a SETUP packet or free IN packet */
2203 temp = sc->sc_last_rx_status;
2205 if ((td->ep_no == 0) && (temp != 0) &&
2206 (GRXSTSRD_CHNUM_GET(temp) == 0)) {
2208 if ((temp & GRXSTSRD_PKTSTS_MASK) ==
2209 GRXSTSRD_STP_DATA ||
2210 (temp & GRXSTSRD_PKTSTS_MASK) ==
2211 GRXSTSRD_STP_COMPLETE) {
2212 DPRINTFN(5, "faking complete\n");
2214 * Race condition: We are complete!
2218 /* dump data - wrong direction */
2219 dwc_otg_common_rx_ack(sc);
2222 return (1); /* not complete */
2226 dwc_otg_xfer_do_fifo(struct dwc_otg_softc *sc, struct usb_xfer *xfer)
2228 struct dwc_otg_td *td;
2235 td = xfer->td_transfer_cache;
2240 if ((td->func) (sc, td)) {
2241 /* operation in progress */
2244 if (((void *)td) == xfer->td_transfer_last) {
2247 if (td->error_any) {
2249 } else if (td->remainder > 0) {
2251 * We had a short transfer. If there is no alternate
2252 * next, stop processing !
2259 * Fetch the next transfer descriptor and transfer
2260 * some flags to the next transfer descriptor
2262 tmr_res = td->tmr_res;
2263 tmr_val = td->tmr_val;
2264 toggle = td->toggle;
2266 xfer->td_transfer_cache = td;
2267 td->toggle = toggle; /* transfer toggle */
2268 td->tmr_res = tmr_res;
2269 td->tmr_val = tmr_val;
2274 xfer->td_transfer_cache = NULL;
2275 sc->sc_xfer_complete = 1;
2279 dwc_otg_xfer_do_complete_locked(struct dwc_otg_softc *sc, struct usb_xfer *xfer)
2281 struct dwc_otg_td *td;
2285 td = xfer->td_transfer_cache;
2287 /* compute all actual lengths */
2288 dwc_otg_standard_done(xfer);
2295 dwc_otg_timer(void *_sc)
2297 struct dwc_otg_softc *sc = _sc;
2298 struct usb_xfer *xfer;
2299 struct dwc_otg_td *td;
2301 USB_BUS_LOCK_ASSERT(&sc->sc_bus, MA_OWNED);
2305 USB_BUS_SPIN_LOCK(&sc->sc_bus);
2307 /* increment timer value */
2310 TAILQ_FOREACH(xfer, &sc->sc_bus.intr_q.head, wait_entry) {
2311 td = xfer->td_transfer_cache;
2313 /* reset NAK counter */
2318 /* enable SOF interrupt, which will poll jobs */
2319 dwc_otg_enable_sof_irq(sc);
2321 USB_BUS_SPIN_UNLOCK(&sc->sc_bus);
2323 if (sc->sc_timer_active) {
2325 usb_callout_reset(&sc->sc_timer,
2326 hz / (1000 / DWC_OTG_HOST_TIMER_RATE),
2327 &dwc_otg_timer, sc);
2332 dwc_otg_timer_start(struct dwc_otg_softc *sc)
2334 if (sc->sc_timer_active != 0)
2337 sc->sc_timer_active = 1;
2340 usb_callout_reset(&sc->sc_timer,
2341 hz / (1000 / DWC_OTG_HOST_TIMER_RATE),
2342 &dwc_otg_timer, sc);
2346 dwc_otg_timer_stop(struct dwc_otg_softc *sc)
2348 if (sc->sc_timer_active == 0)
2351 sc->sc_timer_active = 0;
2354 usb_callout_stop(&sc->sc_timer);
2358 dwc_otg_host_channel_disable(struct dwc_otg_softc *sc, uint8_t x)
2362 hcchar = DWC_OTG_READ_4(sc, DOTG_HCCHAR(x));
2364 /* disable host channel, if any */
2365 if (hcchar & (HCCHAR_CHENA | HCCHAR_CHDIS)) {
2366 /* disable channel */
2367 DWC_OTG_WRITE_4(sc, DOTG_HCCHAR(x),
2368 HCCHAR_CHENA | HCCHAR_CHDIS);
2369 /* wait for chip to get its brains in order */
2370 sc->sc_chan_state[x].wait_sof = 2;
2373 /* release TX FIFO usage, if any */
2374 sc->sc_tx_cur_p_level -= sc->sc_chan_state[x].tx_p_size;
2375 sc->sc_tx_cur_np_level -= sc->sc_chan_state[x].tx_np_size;
2377 /* don't release TX FIFO usage twice */
2378 sc->sc_chan_state[x].tx_p_size = 0;
2379 sc->sc_chan_state[x].tx_np_size = 0;
2383 dwc_otg_compute_isoc_rx_tt_slot(struct dwc_otg_tt_info *pinfo)
2385 if (pinfo->slot_index < DWC_OTG_TT_SLOT_MAX)
2386 pinfo->slot_index++;
2387 return (pinfo->slot_index);
2391 dwc_otg_update_host_transfer_schedule_locked(struct dwc_otg_softc *sc)
2393 TAILQ_HEAD(, usb_xfer) head;
2394 struct usb_xfer *xfer;
2395 struct usb_xfer *xfer_next;
2396 struct dwc_otg_td *td;
2401 temp = DWC_OTG_READ_4(sc, DOTG_HFNUM) & DWC_OTG_FRAME_MASK;
2403 if (sc->sc_last_frame_num == temp)
2406 sc->sc_last_frame_num = temp;
2410 for (x = 0; x != sc->sc_host_ch_max; x++) {
2411 if (sc->sc_chan_state[x].wait_sof == 0)
2415 if (--(sc->sc_chan_state[x].wait_sof) == 0)
2416 dwc_otg_host_channel_disable(sc, x);
2419 if ((temp & 7) == 0) {
2421 /* reset the schedule */
2422 memset(sc->sc_tt_info, 0, sizeof(sc->sc_tt_info));
2424 TAILQ_FOREACH_SAFE(xfer, &sc->sc_bus.intr_q.head, wait_entry, xfer_next) {
2425 td = xfer->td_transfer_cache;
2426 if (td == NULL || td->ep_type != UE_ISOCHRONOUS)
2429 /* check for IN direction */
2430 if ((td->hcchar & HCCHAR_EPDIR_IN) != 0)
2433 /* execute more frames */
2438 if (td->hcsplt == 0 || td->tt_scheduled != 0)
2442 slot = dwc_otg_compute_isoc_rx_tt_slot(
2443 sc->sc_tt_info + td->tt_index);
2446 * Not enough time to get complete
2452 td->tt_start_slot = temp + slot;
2453 td->tt_scheduled = 1;
2454 TAILQ_REMOVE(&sc->sc_bus.intr_q.head, xfer, wait_entry);
2455 TAILQ_INSERT_TAIL(&head, xfer, wait_entry);
2458 TAILQ_FOREACH_SAFE(xfer, &sc->sc_bus.intr_q.head, wait_entry, xfer_next) {
2459 td = xfer->td_transfer_cache;
2460 if (td == NULL || td->ep_type != UE_ISOCHRONOUS)
2463 /* check for OUT direction */
2464 if ((td->hcchar & HCCHAR_EPDIR_IN) == 0)
2467 /* execute more frames */
2472 if (td->hcsplt == 0 || td->tt_scheduled != 0)
2476 td->tt_start_slot = temp;
2477 td->tt_scheduled = 1;
2478 TAILQ_REMOVE(&sc->sc_bus.intr_q.head, xfer, wait_entry);
2479 TAILQ_INSERT_TAIL(&head, xfer, wait_entry);
2482 TAILQ_FOREACH_SAFE(xfer, &sc->sc_bus.intr_q.head, wait_entry, xfer_next) {
2483 td = xfer->td_transfer_cache;
2484 if (td == NULL || td->ep_type != UE_INTERRUPT)
2487 if (td->tt_scheduled != 0) {
2492 if (dwc_otg_host_rate_check_interrupt(sc, td))
2495 if (td->hcsplt == 0) {
2497 td->tt_scheduled = 1;
2502 td->tt_start_slot = temp;
2504 td->tt_scheduled = 1;
2505 TAILQ_REMOVE(&sc->sc_bus.intr_q.head, xfer, wait_entry);
2506 TAILQ_INSERT_TAIL(&head, xfer, wait_entry);
2509 TAILQ_FOREACH_SAFE(xfer, &sc->sc_bus.intr_q.head, wait_entry, xfer_next) {
2510 td = xfer->td_transfer_cache;
2512 td->ep_type != UE_CONTROL ||
2513 td->did_nak >= DWC_OTG_NAK_MAX) {
2519 if (td->hcsplt == 0 || td->tt_scheduled != 0)
2523 td->tt_start_slot = temp;
2524 td->tt_scheduled = 1;
2525 TAILQ_REMOVE(&sc->sc_bus.intr_q.head, xfer, wait_entry);
2526 TAILQ_INSERT_TAIL(&head, xfer, wait_entry);
2529 if ((temp & 7) < 6) {
2530 TAILQ_FOREACH_SAFE(xfer, &sc->sc_bus.intr_q.head, wait_entry, xfer_next) {
2531 td = xfer->td_transfer_cache;
2533 td->ep_type != UE_BULK ||
2534 td->did_nak >= DWC_OTG_NAK_MAX) {
2540 if (td->hcsplt == 0 || td->tt_scheduled != 0)
2544 td->tt_start_slot = temp;
2545 td->tt_scheduled = 1;
2546 TAILQ_REMOVE(&sc->sc_bus.intr_q.head, xfer, wait_entry);
2547 TAILQ_INSERT_TAIL(&head, xfer, wait_entry);
2551 /* Put TT transfers in execution order at the end */
2552 TAILQ_CONCAT(&sc->sc_bus.intr_q.head, &head, wait_entry);
2554 /* move all TT transfers in front, keeping the current order */
2555 TAILQ_FOREACH_SAFE(xfer, &sc->sc_bus.intr_q.head, wait_entry, xfer_next) {
2556 td = xfer->td_transfer_cache;
2557 if (td == NULL || td->hcsplt == 0)
2559 TAILQ_REMOVE(&sc->sc_bus.intr_q.head, xfer, wait_entry);
2560 TAILQ_INSERT_TAIL(&head, xfer, wait_entry);
2562 TAILQ_CONCAT(&head, &sc->sc_bus.intr_q.head, wait_entry);
2563 TAILQ_CONCAT(&sc->sc_bus.intr_q.head, &head, wait_entry);
2565 /* put non-TT BULK transfers last */
2566 TAILQ_FOREACH_SAFE(xfer, &sc->sc_bus.intr_q.head, wait_entry, xfer_next) {
2567 td = xfer->td_transfer_cache;
2568 if (td == NULL || td->hcsplt != 0 || td->ep_type != UE_BULK)
2570 TAILQ_REMOVE(&sc->sc_bus.intr_q.head, xfer, wait_entry);
2571 TAILQ_INSERT_TAIL(&head, xfer, wait_entry);
2573 TAILQ_CONCAT(&sc->sc_bus.intr_q.head, &head, wait_entry);
2575 if ((temp & 7) == 0) {
2577 DPRINTFN(12, "SOF interrupt #%d, needsof=%d\n",
2578 (int)temp, (int)sc->sc_needsof);
2580 /* update SOF IRQ mask */
2581 if (sc->sc_irq_mask & GINTMSK_SOFMSK) {
2582 if (sc->sc_needsof == 0) {
2583 sc->sc_irq_mask &= ~GINTMSK_SOFMSK;
2584 DWC_OTG_WRITE_4(sc, DOTG_GINTMSK, sc->sc_irq_mask);
2587 if (sc->sc_needsof != 0) {
2588 sc->sc_irq_mask |= GINTMSK_SOFMSK;
2589 DWC_OTG_WRITE_4(sc, DOTG_GINTMSK, sc->sc_irq_mask);
2593 /* clear need SOF flag */
2600 dwc_otg_interrupt_poll_locked(struct dwc_otg_softc *sc)
2602 struct usb_xfer *xfer;
2604 uint8_t got_rx_status;
2608 /* get all channel interrupts */
2609 for (x = 0; x != sc->sc_host_ch_max; x++) {
2610 temp = DWC_OTG_READ_4(sc, DOTG_HCINT(x));
2612 DWC_OTG_WRITE_4(sc, DOTG_HCINT(x), temp);
2613 temp &= ~HCINT_SOFTWARE_ONLY;
2614 sc->sc_chan_state[x].hcint |= temp;
2618 if (sc->sc_last_rx_status == 0) {
2620 temp = DWC_OTG_READ_4(sc, DOTG_GINTSTS);
2621 if (temp & GINTSTS_RXFLVL) {
2622 /* pop current status */
2623 sc->sc_last_rx_status =
2624 DWC_OTG_READ_4(sc, DOTG_GRXSTSPD);
2627 if (sc->sc_last_rx_status != 0) {
2631 temp = sc->sc_last_rx_status &
2632 GRXSTSRD_PKTSTS_MASK;
2634 /* non-data messages we simply skip */
2635 if (temp != GRXSTSRD_STP_DATA &&
2636 temp != GRXSTSRD_STP_COMPLETE &&
2637 temp != GRXSTSRD_OUT_DATA) {
2638 dwc_otg_common_rx_ack(sc);
2642 temp = GRXSTSRD_BCNT_GET(
2643 sc->sc_last_rx_status);
2644 ep_no = GRXSTSRD_CHNUM_GET(
2645 sc->sc_last_rx_status);
2647 /* receive data, if any */
2649 DPRINTF("Reading %d bytes from ep %d\n", temp, ep_no);
2650 bus_space_read_region_4(sc->sc_io_tag, sc->sc_io_hdl,
2652 sc->sc_rx_bounce_buffer, (temp + 3) / 4);
2655 /* check if we should dump the data */
2656 if (!(sc->sc_active_rx_ep & (1U << ep_no))) {
2657 dwc_otg_common_rx_ack(sc);
2663 DPRINTFN(5, "RX status = 0x%08x: ch=%d pid=%d bytes=%d sts=%d\n",
2664 sc->sc_last_rx_status, ep_no,
2665 (sc->sc_last_rx_status >> 15) & 3,
2666 GRXSTSRD_BCNT_GET(sc->sc_last_rx_status),
2667 (sc->sc_last_rx_status >> 17) & 15);
2674 ep_no = GRXSTSRD_CHNUM_GET(
2675 sc->sc_last_rx_status);
2677 /* check if we should dump the data */
2678 if (!(sc->sc_active_rx_ep & (1U << ep_no))) {
2679 dwc_otg_common_rx_ack(sc);
2687 TAILQ_FOREACH(xfer, &sc->sc_bus.intr_q.head, wait_entry)
2688 dwc_otg_xfer_do_fifo(sc, xfer);
2690 if (got_rx_status) {
2691 /* check if data was consumed */
2692 if (sc->sc_last_rx_status == 0)
2695 /* disable RX FIFO level interrupt */
2696 sc->sc_irq_mask &= ~GINTMSK_RXFLVLMSK;
2697 DWC_OTG_WRITE_4(sc, DOTG_GINTMSK, sc->sc_irq_mask);
2700 if (sc->sc_flags.status_device_mode == 0 && sc->sc_xfer_complete == 0) {
2701 /* update host transfer schedule, so that new transfers can be issued */
2702 if (dwc_otg_update_host_transfer_schedule_locked(sc))
2708 dwc_otg_interrupt_complete_locked(struct dwc_otg_softc *sc)
2710 struct usb_xfer *xfer;
2712 /* scan for completion events */
2713 TAILQ_FOREACH(xfer, &sc->sc_bus.intr_q.head, wait_entry) {
2714 if (dwc_otg_xfer_do_complete_locked(sc, xfer))
2720 dwc_otg_vbus_interrupt(struct dwc_otg_softc *sc, uint8_t is_on)
2722 DPRINTFN(5, "vbus = %u\n", is_on);
2725 if (!sc->sc_flags.status_vbus) {
2726 sc->sc_flags.status_vbus = 1;
2728 /* complete root HUB interrupt endpoint */
2730 dwc_otg_root_intr(sc);
2733 if (sc->sc_flags.status_vbus) {
2734 sc->sc_flags.status_vbus = 0;
2735 sc->sc_flags.status_bus_reset = 0;
2736 sc->sc_flags.status_suspend = 0;
2737 sc->sc_flags.change_suspend = 0;
2738 sc->sc_flags.change_connect = 1;
2740 /* complete root HUB interrupt endpoint */
2742 dwc_otg_root_intr(sc);
2748 dwc_otg_filter_interrupt(void *arg)
2750 struct dwc_otg_softc *sc = arg;
2751 int retval = FILTER_HANDLED;
2754 USB_BUS_SPIN_LOCK(&sc->sc_bus);
2756 /* read and clear interrupt status */
2757 status = DWC_OTG_READ_4(sc, DOTG_GINTSTS);
2759 /* clear interrupts we are handling here */
2760 DWC_OTG_WRITE_4(sc, DOTG_GINTSTS, status & ~DWC_OTG_MSK_GINT_THREAD_IRQ);
2762 /* check for USB state change interrupts */
2763 if ((status & DWC_OTG_MSK_GINT_THREAD_IRQ) != 0)
2764 retval = FILTER_SCHEDULE_THREAD;
2766 /* clear all IN endpoint interrupts */
2767 if (status & GINTSTS_IEPINT) {
2771 for (x = 0; x != sc->sc_dev_in_ep_max; x++) {
2772 temp = DWC_OTG_READ_4(sc, DOTG_DIEPINT(x));
2773 if (temp & DIEPMSK_XFERCOMPLMSK) {
2774 DWC_OTG_WRITE_4(sc, DOTG_DIEPINT(x),
2775 DIEPMSK_XFERCOMPLMSK);
2780 /* poll FIFOs, if any */
2781 dwc_otg_interrupt_poll_locked(sc);
2783 if (sc->sc_xfer_complete != 0)
2784 retval = FILTER_SCHEDULE_THREAD;
2786 USB_BUS_SPIN_UNLOCK(&sc->sc_bus);
2792 dwc_otg_interrupt(void *arg)
2794 struct dwc_otg_softc *sc = arg;
2797 USB_BUS_LOCK(&sc->sc_bus);
2798 USB_BUS_SPIN_LOCK(&sc->sc_bus);
2800 /* read and clear interrupt status */
2801 status = DWC_OTG_READ_4(sc, DOTG_GINTSTS);
2803 /* clear interrupts we are handling here */
2804 DWC_OTG_WRITE_4(sc, DOTG_GINTSTS, status & DWC_OTG_MSK_GINT_THREAD_IRQ);
2806 DPRINTFN(14, "GINTSTS=0x%08x HAINT=0x%08x HFNUM=0x%08x\n",
2807 status, DWC_OTG_READ_4(sc, DOTG_HAINT),
2808 DWC_OTG_READ_4(sc, DOTG_HFNUM));
2810 if (status & GINTSTS_USBRST) {
2812 /* set correct state */
2813 sc->sc_flags.status_device_mode = 1;
2814 sc->sc_flags.status_bus_reset = 0;
2815 sc->sc_flags.status_suspend = 0;
2816 sc->sc_flags.change_suspend = 0;
2817 sc->sc_flags.change_connect = 1;
2819 /* Disable SOF interrupt */
2820 sc->sc_irq_mask &= ~GINTMSK_SOFMSK;
2821 DWC_OTG_WRITE_4(sc, DOTG_GINTMSK, sc->sc_irq_mask);
2823 /* complete root HUB interrupt endpoint */
2824 dwc_otg_root_intr(sc);
2827 /* check for any bus state change interrupts */
2828 if (status & GINTSTS_ENUMDONE) {
2832 DPRINTFN(5, "end of reset\n");
2834 /* set correct state */
2835 sc->sc_flags.status_device_mode = 1;
2836 sc->sc_flags.status_bus_reset = 1;
2837 sc->sc_flags.status_suspend = 0;
2838 sc->sc_flags.change_suspend = 0;
2839 sc->sc_flags.change_connect = 1;
2840 sc->sc_flags.status_low_speed = 0;
2841 sc->sc_flags.port_enabled = 1;
2844 (void) dwc_otg_init_fifo(sc, DWC_MODE_DEVICE);
2846 /* reset function address */
2847 dwc_otg_set_address(sc, 0);
2849 /* figure out enumeration speed */
2850 temp = DWC_OTG_READ_4(sc, DOTG_DSTS);
2851 if (DSTS_ENUMSPD_GET(temp) == DSTS_ENUMSPD_HI)
2852 sc->sc_flags.status_high_speed = 1;
2854 sc->sc_flags.status_high_speed = 0;
2857 * Disable resume and SOF interrupt, and enable
2858 * suspend and RX frame interrupt:
2860 sc->sc_irq_mask &= ~(GINTMSK_WKUPINTMSK | GINTMSK_SOFMSK);
2861 sc->sc_irq_mask |= GINTMSK_USBSUSPMSK;
2862 DWC_OTG_WRITE_4(sc, DOTG_GINTMSK, sc->sc_irq_mask);
2864 /* complete root HUB interrupt endpoint */
2865 dwc_otg_root_intr(sc);
2868 if (status & GINTSTS_PRTINT) {
2871 hprt = DWC_OTG_READ_4(sc, DOTG_HPRT);
2873 /* clear change bits */
2874 DWC_OTG_WRITE_4(sc, DOTG_HPRT, (hprt & (
2875 HPRT_PRTPWR | HPRT_PRTENCHNG |
2876 HPRT_PRTCONNDET | HPRT_PRTOVRCURRCHNG)) |
2879 DPRINTFN(12, "GINTSTS=0x%08x, HPRT=0x%08x\n", status, hprt);
2881 sc->sc_flags.status_device_mode = 0;
2883 if (hprt & HPRT_PRTCONNSTS)
2884 sc->sc_flags.status_bus_reset = 1;
2886 sc->sc_flags.status_bus_reset = 0;
2888 if (hprt & HPRT_PRTENCHNG)
2889 sc->sc_flags.change_enabled = 1;
2891 if (hprt & HPRT_PRTENA)
2892 sc->sc_flags.port_enabled = 1;
2894 sc->sc_flags.port_enabled = 0;
2896 if (hprt & HPRT_PRTOVRCURRCHNG)
2897 sc->sc_flags.change_over_current = 1;
2899 if (hprt & HPRT_PRTOVRCURRACT)
2900 sc->sc_flags.port_over_current = 1;
2902 sc->sc_flags.port_over_current = 0;
2904 if (hprt & HPRT_PRTPWR)
2905 sc->sc_flags.port_powered = 1;
2907 sc->sc_flags.port_powered = 0;
2909 if (((hprt & HPRT_PRTSPD_MASK)
2910 >> HPRT_PRTSPD_SHIFT) == HPRT_PRTSPD_LOW)
2911 sc->sc_flags.status_low_speed = 1;
2913 sc->sc_flags.status_low_speed = 0;
2915 if (((hprt & HPRT_PRTSPD_MASK)
2916 >> HPRT_PRTSPD_SHIFT) == HPRT_PRTSPD_HIGH)
2917 sc->sc_flags.status_high_speed = 1;
2919 sc->sc_flags.status_high_speed = 0;
2921 if (hprt & HPRT_PRTCONNDET)
2922 sc->sc_flags.change_connect = 1;
2924 if (hprt & HPRT_PRTSUSP)
2925 dwc_otg_suspend_irq(sc);
2927 dwc_otg_resume_irq(sc);
2929 /* complete root HUB interrupt endpoint */
2930 dwc_otg_root_intr(sc);
2932 /* update host frame interval */
2933 dwc_otg_update_host_frame_interval(sc);
2937 * If resume and suspend is set at the same time we interpret
2938 * that like RESUME. Resume is set when there is at least 3
2939 * milliseconds of inactivity on the USB BUS.
2941 if (status & GINTSTS_WKUPINT) {
2943 DPRINTFN(5, "resume interrupt\n");
2945 dwc_otg_resume_irq(sc);
2947 } else if (status & GINTSTS_USBSUSP) {
2949 DPRINTFN(5, "suspend interrupt\n");
2951 dwc_otg_suspend_irq(sc);
2954 if (status & (GINTSTS_USBSUSP |
2957 GINTSTS_SESSREQINT)) {
2960 temp = DWC_OTG_READ_4(sc, DOTG_GOTGCTL);
2962 DPRINTFN(5, "GOTGCTL=0x%08x\n", temp);
2964 dwc_otg_vbus_interrupt(sc,
2965 (temp & (GOTGCTL_ASESVLD | GOTGCTL_BSESVLD)) ? 1 : 0);
2968 if (sc->sc_xfer_complete != 0) {
2969 sc->sc_xfer_complete = 0;
2971 /* complete FIFOs, if any */
2972 dwc_otg_interrupt_complete_locked(sc);
2974 if (sc->sc_flags.status_device_mode == 0) {
2975 /* update host transfer schedule, so that new transfers can be issued */
2976 if (dwc_otg_update_host_transfer_schedule_locked(sc))
2977 dwc_otg_interrupt_poll_locked(sc);
2980 USB_BUS_SPIN_UNLOCK(&sc->sc_bus);
2981 USB_BUS_UNLOCK(&sc->sc_bus);
2985 dwc_otg_setup_standard_chain_sub(struct dwc_otg_std_temp *temp)
2987 struct dwc_otg_td *td;
2989 /* get current Transfer Descriptor */
2993 /* prepare for next TD */
2994 temp->td_next = td->obj_next;
2996 /* fill out the Transfer Descriptor */
2997 td->func = temp->func;
2999 td->offset = temp->offset;
3000 td->remainder = temp->len;
3003 td->error_stall = 0;
3005 td->did_stall = temp->did_stall;
3006 td->short_pkt = temp->short_pkt;
3007 td->alt_next = temp->setup_alt_next;
3011 td->channel = DWC_OTG_MAX_CHANNELS;
3014 td->tt_scheduled = 0;
3015 td->tt_xactpos = HCSPLT_XACTPOS_BEGIN;
3019 dwc_otg_setup_standard_chain(struct usb_xfer *xfer)
3021 struct dwc_otg_std_temp temp;
3022 struct dwc_otg_td *td;
3027 DPRINTFN(9, "addr=%d endpt=%d sumlen=%d speed=%d\n",
3028 xfer->address, UE_GET_ADDR(xfer->endpointno),
3029 xfer->sumlen, usbd_get_speed(xfer->xroot->udev));
3031 temp.max_frame_size = xfer->max_frame_size;
3033 td = xfer->td_start[0];
3034 xfer->td_transfer_first = td;
3035 xfer->td_transfer_cache = td;
3041 temp.td_next = xfer->td_start[0];
3043 temp.setup_alt_next = xfer->flags_int.short_frames_ok ||
3044 xfer->flags_int.isochronous_xfr;
3045 temp.did_stall = !xfer->flags_int.control_stall;
3047 is_host = (xfer->xroot->udev->flags.usb_mode == USB_MODE_HOST);
3049 /* check if we should prepend a setup message */
3051 if (xfer->flags_int.control_xfr) {
3052 if (xfer->flags_int.control_hdr) {
3055 temp.func = &dwc_otg_host_setup_tx;
3057 temp.func = &dwc_otg_setup_rx;
3059 temp.len = xfer->frlengths[0];
3060 temp.pc = xfer->frbuffers + 0;
3061 temp.short_pkt = temp.len ? 1 : 0;
3063 /* check for last frame */
3064 if (xfer->nframes == 1) {
3065 /* no STATUS stage yet, SETUP is last */
3066 if (xfer->flags_int.control_act)
3067 temp.setup_alt_next = 0;
3070 dwc_otg_setup_standard_chain_sub(&temp);
3077 if (x != xfer->nframes) {
3078 if (xfer->endpointno & UE_DIR_IN) {
3080 temp.func = &dwc_otg_host_data_rx;
3083 temp.func = &dwc_otg_data_tx;
3088 temp.func = &dwc_otg_host_data_tx;
3091 temp.func = &dwc_otg_data_rx;
3096 /* setup "pc" pointer */
3097 temp.pc = xfer->frbuffers + x;
3101 while (x != xfer->nframes) {
3103 /* DATA0 / DATA1 message */
3105 temp.len = xfer->frlengths[x];
3109 if (x == xfer->nframes) {
3110 if (xfer->flags_int.control_xfr) {
3111 if (xfer->flags_int.control_act) {
3112 temp.setup_alt_next = 0;
3115 temp.setup_alt_next = 0;
3118 if (temp.len == 0) {
3120 /* make sure that we send an USB packet */
3126 /* regular data transfer */
3128 temp.short_pkt = (xfer->flags.force_short_xfer ? 0 : 1);
3131 dwc_otg_setup_standard_chain_sub(&temp);
3133 if (xfer->flags_int.isochronous_xfr) {
3134 temp.offset += temp.len;
3136 /* get next Page Cache pointer */
3137 temp.pc = xfer->frbuffers + x;
3141 if (xfer->flags_int.control_xfr) {
3143 /* always setup a valid "pc" pointer for status and sync */
3144 temp.pc = xfer->frbuffers + 0;
3147 temp.setup_alt_next = 0;
3149 /* check if we need to sync */
3151 /* we need a SYNC point after TX */
3152 temp.func = &dwc_otg_data_tx_sync;
3153 dwc_otg_setup_standard_chain_sub(&temp);
3156 /* check if we should append a status stage */
3157 if (!xfer->flags_int.control_act) {
3160 * Send a DATA1 message and invert the current
3161 * endpoint direction.
3163 if (xfer->endpointno & UE_DIR_IN) {
3165 temp.func = &dwc_otg_host_data_tx;
3168 temp.func = &dwc_otg_data_rx;
3173 temp.func = &dwc_otg_host_data_rx;
3176 temp.func = &dwc_otg_data_tx;
3181 dwc_otg_setup_standard_chain_sub(&temp);
3183 /* data toggle should be DATA1 */
3188 /* we need a SYNC point after TX */
3189 temp.func = &dwc_otg_data_tx_sync;
3190 dwc_otg_setup_standard_chain_sub(&temp);
3194 /* check if we need to sync */
3197 temp.pc = xfer->frbuffers + 0;
3200 temp.setup_alt_next = 0;
3202 /* we need a SYNC point after TX */
3203 temp.func = &dwc_otg_data_tx_sync;
3204 dwc_otg_setup_standard_chain_sub(&temp);
3208 /* must have at least one frame! */
3210 xfer->td_transfer_last = td;
3214 struct dwc_otg_softc *sc;
3218 sc = DWC_OTG_BUS2SC(xfer->xroot->bus);
3220 /* get first again */
3221 td = xfer->td_transfer_first;
3222 td->toggle = (xfer->endpoint->toggle_next ? 1 : 0);
3225 (xfer->address << HCCHAR_DEVADDR_SHIFT) |
3226 ((xfer->endpointno & UE_ADDR) << HCCHAR_EPNUM_SHIFT) |
3227 (xfer->max_packet_size << HCCHAR_MPS_SHIFT) |
3231 * We are not always able to meet the timing
3232 * requirements of the USB interrupt endpoint's
3233 * complete split token, when doing transfers going
3234 * via a transaction translator. Use the CONTROL
3235 * transfer type instead of the INTERRUPT transfer
3236 * type in general, as a means to workaround
3237 * that. This trick should work for both FULL and LOW
3238 * speed USB traffic going through a TT. For non-TT
3239 * traffic it works aswell. The reason for using
3240 * CONTROL type instead of BULK is that some TTs might
3241 * reject LOW speed BULK traffic.
3243 if (td->ep_type == UE_INTERRUPT)
3244 hcchar |= (UE_CONTROL << HCCHAR_EPTYPE_SHIFT);
3246 hcchar |= (td->ep_type << HCCHAR_EPTYPE_SHIFT);
3248 if (usbd_get_speed(xfer->xroot->udev) == USB_SPEED_LOW)
3249 hcchar |= HCCHAR_LSPDDEV;
3250 if (UE_GET_DIR(xfer->endpointno) == UE_DIR_IN)
3251 hcchar |= HCCHAR_EPDIR_IN;
3253 switch (xfer->xroot->udev->speed) {
3254 case USB_SPEED_FULL:
3256 /* check if root HUB port is running High Speed */
3257 if (xfer->xroot->udev->parent_hs_hub != NULL) {
3258 hcsplt = HCSPLT_SPLTENA |
3259 (xfer->xroot->udev->hs_port_no <<
3260 HCSPLT_PRTADDR_SHIFT) |
3261 (xfer->xroot->udev->hs_hub_addr <<
3262 HCSPLT_HUBADDR_SHIFT);
3266 if (td->ep_type == UE_INTERRUPT) {
3268 ival = xfer->interval / DWC_OTG_HOST_TIMER_RATE;
3271 else if (ival > 127)
3273 td->tmr_val = sc->sc_tmr_val + ival;
3275 } else if (td->ep_type == UE_ISOCHRONOUS) {
3283 case USB_SPEED_HIGH:
3285 if (td->ep_type == UE_INTERRUPT) {
3288 hcchar |= ((xfer->max_packet_count & 3)
3289 << HCCHAR_MC_SHIFT);
3291 ival = xfer->interval / DWC_OTG_HOST_TIMER_RATE;
3294 else if (ival > 127)
3296 td->tmr_val = sc->sc_tmr_val + ival;
3298 } else if (td->ep_type == UE_ISOCHRONOUS) {
3299 hcchar |= ((xfer->max_packet_count & 3)
3300 << HCCHAR_MC_SHIFT);
3302 td->tmr_res = 1 << usbd_xfer_get_fps_shift(xfer);
3315 /* store configuration in all TD's */
3317 td->hcchar = hcchar;
3318 td->hcsplt = hcsplt;
3320 if (((void *)td) == xfer->td_transfer_last)
3329 dwc_otg_timeout(void *arg)
3331 struct usb_xfer *xfer = arg;
3333 DPRINTF("xfer=%p\n", xfer);
3335 USB_BUS_LOCK_ASSERT(xfer->xroot->bus, MA_OWNED);
3337 /* transfer is transferred */
3338 dwc_otg_device_done(xfer, USB_ERR_TIMEOUT);
3342 dwc_otg_start_standard_chain(struct usb_xfer *xfer)
3344 struct dwc_otg_softc *sc = DWC_OTG_BUS2SC(xfer->xroot->bus);
3345 struct usb_xfer_root *xroot;
3346 struct dwc_otg_td *td;
3351 * Poll one time in device mode, which will turn on the
3352 * endpoint interrupts. Else wait for SOF interrupt in host
3355 USB_BUS_SPIN_LOCK(&sc->sc_bus);
3357 if (sc->sc_flags.status_device_mode != 0) {
3358 dwc_otg_xfer_do_fifo(sc, xfer);
3359 if (dwc_otg_xfer_do_complete_locked(sc, xfer))
3363 /* put transfer on interrupt queue */
3364 usbd_transfer_enqueue(&xfer->xroot->bus->intr_q, xfer);
3366 /* start timeout, if any */
3367 if (xfer->timeout != 0) {
3368 usbd_transfer_timeout_ms(xfer,
3369 &dwc_otg_timeout, xfer->timeout);
3372 if (sc->sc_flags.status_device_mode != 0)
3375 /* enable SOF interrupt, if any */
3376 dwc_otg_enable_sof_irq(sc);
3378 td = xfer->td_transfer_cache;
3379 if (td->ep_type != UE_BULK)
3382 xroot = xfer->xroot;
3385 * Optimise the ping-pong effect by waking up other BULK
3386 * transfers belonging to the same device group:
3388 TAILQ_FOREACH(xfer, &sc->sc_bus.intr_q.head, wait_entry) {
3389 td = xfer->td_transfer_cache;
3390 if (td == NULL || td->ep_type != UE_BULK || xfer->xroot != xroot)
3392 /* reset NAK counter */
3396 USB_BUS_SPIN_UNLOCK(&sc->sc_bus);
3400 dwc_otg_root_intr(struct dwc_otg_softc *sc)
3404 USB_BUS_LOCK_ASSERT(&sc->sc_bus, MA_OWNED);
3407 sc->sc_hub_idata[0] = 0x02; /* we only have one port */
3409 uhub_root_intr(&sc->sc_bus, sc->sc_hub_idata,
3410 sizeof(sc->sc_hub_idata));
3414 dwc_otg_standard_done_sub(struct usb_xfer *xfer)
3416 struct dwc_otg_td *td;
3422 td = xfer->td_transfer_cache;
3425 len = td->remainder;
3427 /* store last data toggle */
3428 xfer->endpoint->toggle_next = td->toggle;
3430 if (xfer->aframes != xfer->nframes) {
3432 * Verify the length and subtract
3433 * the remainder from "frlengths[]":
3435 if (len > xfer->frlengths[xfer->aframes]) {
3438 xfer->frlengths[xfer->aframes] -= len;
3441 /* Check for transfer error */
3442 if (td->error_any) {
3443 /* the transfer is finished */
3444 error = (td->error_stall ?
3445 USB_ERR_STALLED : USB_ERR_IOERROR);
3449 /* Check for short transfer */
3451 if (xfer->flags_int.short_frames_ok ||
3452 xfer->flags_int.isochronous_xfr) {
3453 /* follow alt next */
3460 /* the transfer is finished */
3468 /* this USB frame is complete */
3474 /* update transfer cache */
3476 xfer->td_transfer_cache = td;
3482 dwc_otg_standard_done(struct usb_xfer *xfer)
3484 usb_error_t err = 0;
3486 DPRINTFN(13, "xfer=%p endpoint=%p transfer done\n",
3487 xfer, xfer->endpoint);
3491 xfer->td_transfer_cache = xfer->td_transfer_first;
3493 if (xfer->flags_int.control_xfr) {
3495 if (xfer->flags_int.control_hdr) {
3497 err = dwc_otg_standard_done_sub(xfer);
3501 if (xfer->td_transfer_cache == NULL) {
3505 while (xfer->aframes != xfer->nframes) {
3507 err = dwc_otg_standard_done_sub(xfer);
3510 if (xfer->td_transfer_cache == NULL) {
3515 if (xfer->flags_int.control_xfr &&
3516 !xfer->flags_int.control_act) {
3518 err = dwc_otg_standard_done_sub(xfer);
3521 dwc_otg_device_done(xfer, err);
3524 /*------------------------------------------------------------------------*
3525 * dwc_otg_device_done
3527 * NOTE: this function can be called more than one time on the
3528 * same USB transfer!
3529 *------------------------------------------------------------------------*/
3531 dwc_otg_device_done(struct usb_xfer *xfer, usb_error_t error)
3533 struct dwc_otg_softc *sc = DWC_OTG_BUS2SC(xfer->xroot->bus);
3535 DPRINTFN(9, "xfer=%p, endpoint=%p, error=%d\n",
3536 xfer, xfer->endpoint, error);
3538 USB_BUS_SPIN_LOCK(&sc->sc_bus);
3540 if (xfer->flags_int.usb_mode == USB_MODE_DEVICE) {
3541 /* Interrupts are cleared by the interrupt handler */
3543 struct dwc_otg_td *td;
3545 td = xfer->td_transfer_cache;
3547 dwc_otg_host_channel_free(sc, td);
3549 /* dequeue transfer and start next transfer */
3550 usbd_transfer_done(xfer, error);
3552 USB_BUS_SPIN_UNLOCK(&sc->sc_bus);
3556 dwc_otg_xfer_stall(struct usb_xfer *xfer)
3558 dwc_otg_device_done(xfer, USB_ERR_STALLED);
3562 dwc_otg_set_stall(struct usb_device *udev,
3563 struct usb_endpoint *ep, uint8_t *did_stall)
3565 struct dwc_otg_softc *sc;
3570 USB_BUS_LOCK_ASSERT(udev->bus, MA_OWNED);
3573 if (udev->flags.usb_mode != USB_MODE_DEVICE) {
3578 sc = DWC_OTG_BUS2SC(udev->bus);
3580 USB_BUS_SPIN_LOCK(&sc->sc_bus);
3582 /* get endpoint address */
3583 ep_no = ep->edesc->bEndpointAddress;
3585 DPRINTFN(5, "endpoint=0x%x\n", ep_no);
3587 if (ep_no & UE_DIR_IN) {
3588 reg = DOTG_DIEPCTL(ep_no & UE_ADDR);
3589 temp = sc->sc_in_ctl[ep_no & UE_ADDR];
3591 reg = DOTG_DOEPCTL(ep_no & UE_ADDR);
3592 temp = sc->sc_out_ctl[ep_no & UE_ADDR];
3595 /* disable and stall endpoint */
3596 DWC_OTG_WRITE_4(sc, reg, temp | DOEPCTL_EPDIS);
3597 DWC_OTG_WRITE_4(sc, reg, temp | DOEPCTL_STALL);
3599 /* clear active OUT ep */
3600 if (!(ep_no & UE_DIR_IN)) {
3602 sc->sc_active_rx_ep &= ~(1U << (ep_no & UE_ADDR));
3604 if (sc->sc_last_rx_status != 0 &&
3605 (ep_no & UE_ADDR) == GRXSTSRD_CHNUM_GET(
3606 sc->sc_last_rx_status)) {
3608 dwc_otg_common_rx_ack(sc);
3609 /* poll interrupt */
3610 dwc_otg_interrupt_poll_locked(sc);
3611 dwc_otg_interrupt_complete_locked(sc);
3614 USB_BUS_SPIN_UNLOCK(&sc->sc_bus);
3618 dwc_otg_clear_stall_sub_locked(struct dwc_otg_softc *sc, uint32_t mps,
3619 uint8_t ep_no, uint8_t ep_type, uint8_t ep_dir)
3624 if (ep_type == UE_CONTROL) {
3625 /* clearing stall is not needed */
3630 reg = DOTG_DIEPCTL(ep_no);
3632 reg = DOTG_DOEPCTL(ep_no);
3633 sc->sc_active_rx_ep |= (1U << ep_no);
3636 /* round up and mask away the multiplier count */
3637 mps = (mps + 3) & 0x7FC;
3639 if (ep_type == UE_BULK) {
3640 temp = DIEPCTL_EPTYPE_SET(
3641 DIEPCTL_EPTYPE_BULK) |
3643 } else if (ep_type == UE_INTERRUPT) {
3644 temp = DIEPCTL_EPTYPE_SET(
3645 DIEPCTL_EPTYPE_INTERRUPT) |
3648 temp = DIEPCTL_EPTYPE_SET(
3649 DIEPCTL_EPTYPE_ISOC) |
3653 temp |= DIEPCTL_MPS_SET(mps);
3654 temp |= DIEPCTL_TXFNUM_SET(ep_no);
3657 sc->sc_in_ctl[ep_no] = temp;
3659 sc->sc_out_ctl[ep_no] = temp;
3661 DWC_OTG_WRITE_4(sc, reg, temp | DOEPCTL_EPDIS);
3662 DWC_OTG_WRITE_4(sc, reg, temp | DOEPCTL_SETD0PID);
3663 DWC_OTG_WRITE_4(sc, reg, temp | DIEPCTL_SNAK);
3665 /* we only reset the transmit FIFO */
3667 dwc_otg_tx_fifo_reset(sc,
3668 GRSTCTL_TXFIFO(ep_no) |
3672 DOTG_DIEPTSIZ(ep_no), 0);
3675 /* poll interrupt */
3676 dwc_otg_interrupt_poll_locked(sc);
3677 dwc_otg_interrupt_complete_locked(sc);
3681 dwc_otg_clear_stall(struct usb_device *udev, struct usb_endpoint *ep)
3683 struct dwc_otg_softc *sc;
3684 struct usb_endpoint_descriptor *ed;
3686 DPRINTFN(5, "endpoint=%p\n", ep);
3688 USB_BUS_LOCK_ASSERT(udev->bus, MA_OWNED);
3691 if (udev->flags.usb_mode != USB_MODE_DEVICE) {
3696 sc = DWC_OTG_BUS2SC(udev->bus);
3698 USB_BUS_SPIN_LOCK(&sc->sc_bus);
3700 /* get endpoint descriptor */
3703 /* reset endpoint */
3704 dwc_otg_clear_stall_sub_locked(sc,
3705 UGETW(ed->wMaxPacketSize),
3706 (ed->bEndpointAddress & UE_ADDR),
3707 (ed->bmAttributes & UE_XFERTYPE),
3708 (ed->bEndpointAddress & (UE_DIR_IN | UE_DIR_OUT)));
3710 USB_BUS_SPIN_UNLOCK(&sc->sc_bus);
3714 dwc_otg_device_state_change(struct usb_device *udev)
3716 struct dwc_otg_softc *sc;
3720 if (udev->flags.usb_mode != USB_MODE_DEVICE) {
3726 sc = DWC_OTG_BUS2SC(udev->bus);
3728 /* deactivate all other endpoint but the control endpoint */
3729 if (udev->state == USB_STATE_CONFIGURED ||
3730 udev->state == USB_STATE_ADDRESSED) {
3732 USB_BUS_LOCK(&sc->sc_bus);
3734 for (x = 1; x != sc->sc_dev_ep_max; x++) {
3736 if (x < sc->sc_dev_in_ep_max) {
3737 DWC_OTG_WRITE_4(sc, DOTG_DIEPCTL(x),
3739 DWC_OTG_WRITE_4(sc, DOTG_DIEPCTL(x), 0);
3742 DWC_OTG_WRITE_4(sc, DOTG_DOEPCTL(x),
3744 DWC_OTG_WRITE_4(sc, DOTG_DOEPCTL(x), 0);
3746 USB_BUS_UNLOCK(&sc->sc_bus);
3751 dwc_otg_init(struct dwc_otg_softc *sc)
3757 /* set up the bus structure */
3758 sc->sc_bus.usbrev = USB_REV_2_0;
3759 sc->sc_bus.methods = &dwc_otg_bus_methods;
3761 usb_callout_init_mtx(&sc->sc_timer,
3762 &sc->sc_bus.bus_mtx, 0);
3764 USB_BUS_LOCK(&sc->sc_bus);
3766 /* turn on clocks */
3767 dwc_otg_clocks_on(sc);
3769 temp = DWC_OTG_READ_4(sc, DOTG_GSNPSID);
3770 DPRINTF("Version = 0x%08x\n", temp);
3773 DWC_OTG_WRITE_4(sc, DOTG_DCTL,
3776 /* wait for host to detect disconnect */
3777 usb_pause_mtx(&sc->sc_bus.bus_mtx, hz / 32);
3779 DWC_OTG_WRITE_4(sc, DOTG_GRSTCTL,
3782 /* wait a little bit for block to reset */
3783 usb_pause_mtx(&sc->sc_bus.bus_mtx, hz / 128);
3785 switch (sc->sc_mode) {
3786 case DWC_MODE_DEVICE:
3787 temp = GUSBCFG_FORCEDEVMODE;
3790 temp = GUSBCFG_FORCEHOSTMODE;
3797 /* select HSIC, ULPI or internal PHY mode */
3798 switch (dwc_otg_phy_type) {
3799 case DWC_OTG_PHY_HSIC:
3800 DWC_OTG_WRITE_4(sc, DOTG_GUSBCFG,
3802 GUSBCFG_TRD_TIM_SET(5) | temp);
3803 DWC_OTG_WRITE_4(sc, DOTG_GOTGCTL,
3806 temp = DWC_OTG_READ_4(sc, DOTG_GLPMCFG);
3807 DWC_OTG_WRITE_4(sc, DOTG_GLPMCFG,
3808 temp & ~GLPMCFG_HSIC_CONN);
3809 DWC_OTG_WRITE_4(sc, DOTG_GLPMCFG,
3810 temp | GLPMCFG_HSIC_CONN);
3812 case DWC_OTG_PHY_ULPI:
3813 DWC_OTG_WRITE_4(sc, DOTG_GUSBCFG,
3814 GUSBCFG_ULPI_UTMI_SEL |
3815 GUSBCFG_TRD_TIM_SET(5) | temp);
3816 DWC_OTG_WRITE_4(sc, DOTG_GOTGCTL, 0);
3818 temp = DWC_OTG_READ_4(sc, DOTG_GLPMCFG);
3819 DWC_OTG_WRITE_4(sc, DOTG_GLPMCFG,
3820 temp & ~GLPMCFG_HSIC_CONN);
3822 case DWC_OTG_PHY_INTERNAL:
3823 DWC_OTG_WRITE_4(sc, DOTG_GUSBCFG,
3825 GUSBCFG_TRD_TIM_SET(5) | temp);
3826 DWC_OTG_WRITE_4(sc, DOTG_GOTGCTL, 0);
3828 temp = DWC_OTG_READ_4(sc, DOTG_GLPMCFG);
3829 DWC_OTG_WRITE_4(sc, DOTG_GLPMCFG,
3830 temp & ~GLPMCFG_HSIC_CONN);
3832 temp = DWC_OTG_READ_4(sc, DOTG_GGPIO);
3833 temp &= ~(DOTG_GGPIO_NOVBUSSENS | DOTG_GGPIO_I2CPADEN);
3834 temp |= (DOTG_GGPIO_VBUSASEN | DOTG_GGPIO_VBUSBSEN |
3836 DWC_OTG_WRITE_4(sc, DOTG_GGPIO, temp);
3842 /* clear global nak */
3843 DWC_OTG_WRITE_4(sc, DOTG_DCTL,
3847 /* disable USB port */
3848 DWC_OTG_WRITE_4(sc, DOTG_PCGCCTL, 0xFFFFFFFF);
3851 usb_pause_mtx(&sc->sc_bus.bus_mtx, hz / 100);
3853 /* enable USB port */
3854 DWC_OTG_WRITE_4(sc, DOTG_PCGCCTL, 0);
3857 usb_pause_mtx(&sc->sc_bus.bus_mtx, hz / 100);
3859 temp = DWC_OTG_READ_4(sc, DOTG_GHWCFG3);
3861 sc->sc_fifo_size = 4 * GHWCFG3_DFIFODEPTH_GET(temp);
3863 temp = DWC_OTG_READ_4(sc, DOTG_GHWCFG2);
3865 sc->sc_dev_ep_max = GHWCFG2_NUMDEVEPS_GET(temp);
3867 if (sc->sc_dev_ep_max > DWC_OTG_MAX_ENDPOINTS)
3868 sc->sc_dev_ep_max = DWC_OTG_MAX_ENDPOINTS;
3870 sc->sc_host_ch_max = GHWCFG2_NUMHSTCHNL_GET(temp);
3872 if (sc->sc_host_ch_max > DWC_OTG_MAX_CHANNELS)
3873 sc->sc_host_ch_max = DWC_OTG_MAX_CHANNELS;
3875 temp = DWC_OTG_READ_4(sc, DOTG_GHWCFG4);
3877 sc->sc_dev_in_ep_max = GHWCFG4_NUM_IN_EP_GET(temp);
3879 DPRINTF("Total FIFO size = %d bytes, Device EPs = %d/%d Host CHs = %d\n",
3880 sc->sc_fifo_size, sc->sc_dev_ep_max, sc->sc_dev_in_ep_max,
3881 sc->sc_host_ch_max);
3884 if (dwc_otg_init_fifo(sc, DWC_MODE_OTG)) {
3885 USB_BUS_UNLOCK(&sc->sc_bus);
3889 /* enable interrupts */
3890 sc->sc_irq_mask = DWC_OTG_MSK_GINT_ENABLED;
3891 DWC_OTG_WRITE_4(sc, DOTG_GINTMSK, sc->sc_irq_mask);
3893 if (sc->sc_mode == DWC_MODE_OTG || sc->sc_mode == DWC_MODE_DEVICE) {
3895 /* enable all endpoint interrupts */
3896 temp = DWC_OTG_READ_4(sc, DOTG_GHWCFG2);
3897 if (temp & GHWCFG2_MPI) {
3900 DPRINTF("Disable Multi Process Interrupts\n");
3902 for (x = 0; x != sc->sc_dev_in_ep_max; x++) {
3903 DWC_OTG_WRITE_4(sc, DOTG_DIEPEACHINTMSK(x), 0);
3904 DWC_OTG_WRITE_4(sc, DOTG_DOEPEACHINTMSK(x), 0);
3906 DWC_OTG_WRITE_4(sc, DOTG_DEACHINTMSK, 0);
3908 DWC_OTG_WRITE_4(sc, DOTG_DIEPMSK,
3909 DIEPMSK_XFERCOMPLMSK);
3910 DWC_OTG_WRITE_4(sc, DOTG_DOEPMSK, 0);
3911 DWC_OTG_WRITE_4(sc, DOTG_DAINTMSK, 0xFFFF);
3914 if (sc->sc_mode == DWC_MODE_OTG || sc->sc_mode == DWC_MODE_HOST) {
3916 temp = DWC_OTG_READ_4(sc, DOTG_HCFG);
3917 temp &= ~(HCFG_FSLSSUPP | HCFG_FSLSPCLKSEL_MASK);
3918 temp |= (1 << HCFG_FSLSPCLKSEL_SHIFT);
3919 DWC_OTG_WRITE_4(sc, DOTG_HCFG, temp);
3922 /* only enable global IRQ */
3923 DWC_OTG_WRITE_4(sc, DOTG_GAHBCFG,
3924 GAHBCFG_GLBLINTRMSK);
3926 /* turn off clocks */
3927 dwc_otg_clocks_off(sc);
3929 /* read initial VBUS state */
3931 temp = DWC_OTG_READ_4(sc, DOTG_GOTGCTL);
3933 DPRINTFN(5, "GOTCTL=0x%08x\n", temp);
3935 dwc_otg_vbus_interrupt(sc,
3936 (temp & (GOTGCTL_ASESVLD | GOTGCTL_BSESVLD)) ? 1 : 0);
3938 USB_BUS_UNLOCK(&sc->sc_bus);
3940 /* catch any lost interrupts */
3942 dwc_otg_do_poll(&sc->sc_bus);
3944 return (0); /* success */
3948 dwc_otg_uninit(struct dwc_otg_softc *sc)
3950 USB_BUS_LOCK(&sc->sc_bus);
3952 /* stop host timer */
3953 dwc_otg_timer_stop(sc);
3955 /* set disconnect */
3956 DWC_OTG_WRITE_4(sc, DOTG_DCTL,
3959 /* turn off global IRQ */
3960 DWC_OTG_WRITE_4(sc, DOTG_GAHBCFG, 0);
3962 sc->sc_flags.port_enabled = 0;
3963 sc->sc_flags.port_powered = 0;
3964 sc->sc_flags.status_vbus = 0;
3965 sc->sc_flags.status_bus_reset = 0;
3966 sc->sc_flags.status_suspend = 0;
3967 sc->sc_flags.change_suspend = 0;
3968 sc->sc_flags.change_connect = 1;
3970 dwc_otg_pull_down(sc);
3971 dwc_otg_clocks_off(sc);
3973 USB_BUS_UNLOCK(&sc->sc_bus);
3975 usb_callout_drain(&sc->sc_timer);
3979 dwc_otg_suspend(struct dwc_otg_softc *sc)
3985 dwc_otg_resume(struct dwc_otg_softc *sc)
3991 dwc_otg_do_poll(struct usb_bus *bus)
3993 struct dwc_otg_softc *sc = DWC_OTG_BUS2SC(bus);
3995 USB_BUS_LOCK(&sc->sc_bus);
3996 USB_BUS_SPIN_LOCK(&sc->sc_bus);
3997 dwc_otg_interrupt_poll_locked(sc);
3998 dwc_otg_interrupt_complete_locked(sc);
3999 if (sc->sc_flags.status_device_mode == 0) {
4000 /* update host transfer schedule, so that new transfers can be issued */
4001 if (dwc_otg_update_host_transfer_schedule_locked(sc))
4002 dwc_otg_interrupt_poll_locked(sc);
4004 USB_BUS_SPIN_UNLOCK(&sc->sc_bus);
4005 USB_BUS_UNLOCK(&sc->sc_bus);
4008 /*------------------------------------------------------------------------*
4009 * DWC OTG bulk support
4010 * DWC OTG control support
4011 * DWC OTG interrupt support
4012 *------------------------------------------------------------------------*/
4014 dwc_otg_device_non_isoc_open(struct usb_xfer *xfer)
4019 dwc_otg_device_non_isoc_close(struct usb_xfer *xfer)
4021 dwc_otg_device_done(xfer, USB_ERR_CANCELLED);
4025 dwc_otg_device_non_isoc_enter(struct usb_xfer *xfer)
4030 dwc_otg_device_non_isoc_start(struct usb_xfer *xfer)
4033 dwc_otg_setup_standard_chain(xfer);
4034 dwc_otg_start_standard_chain(xfer);
4037 struct usb_pipe_methods dwc_otg_device_non_isoc_methods =
4039 .open = dwc_otg_device_non_isoc_open,
4040 .close = dwc_otg_device_non_isoc_close,
4041 .enter = dwc_otg_device_non_isoc_enter,
4042 .start = dwc_otg_device_non_isoc_start,
4045 /*------------------------------------------------------------------------*
4046 * DWC OTG full speed isochronous support
4047 *------------------------------------------------------------------------*/
4049 dwc_otg_device_isoc_open(struct usb_xfer *xfer)
4054 dwc_otg_device_isoc_close(struct usb_xfer *xfer)
4056 dwc_otg_device_done(xfer, USB_ERR_CANCELLED);
4060 dwc_otg_device_isoc_enter(struct usb_xfer *xfer)
4065 dwc_otg_device_isoc_start(struct usb_xfer *xfer)
4067 struct dwc_otg_softc *sc = DWC_OTG_BUS2SC(xfer->xroot->bus);
4071 uint8_t shift = usbd_xfer_get_fps_shift(xfer);
4073 DPRINTFN(6, "xfer=%p next=%d nframes=%d\n",
4074 xfer, xfer->endpoint->isoc_next, xfer->nframes);
4076 if (xfer->xroot->udev->flags.usb_mode == USB_MODE_HOST) {
4077 temp = DWC_OTG_READ_4(sc, DOTG_HFNUM);
4079 /* get the current frame index */
4080 framenum = (temp & HFNUM_FRNUM_MASK);
4082 temp = DWC_OTG_READ_4(sc, DOTG_DSTS);
4084 /* get the current frame index */
4085 framenum = DSTS_SOFFN_GET(temp);
4088 if (xfer->xroot->udev->parent_hs_hub != NULL)
4091 framenum &= DWC_OTG_FRAME_MASK;
4094 * Compute number of milliseconds worth of data traffic for
4095 * this USB transfer:
4097 if (xfer->xroot->udev->speed == USB_SPEED_HIGH)
4098 msframes = ((xfer->nframes << shift) + 7) / 8;
4100 msframes = xfer->nframes;
4103 * check if the frame index is within the window where the frames
4106 temp = (framenum - xfer->endpoint->isoc_next) & DWC_OTG_FRAME_MASK;
4108 if ((xfer->endpoint->is_synced == 0) || (temp < msframes)) {
4110 * If there is data underflow or the pipe queue is
4111 * empty we schedule the transfer a few frames ahead
4112 * of the current frame position. Else two isochronous
4113 * transfers might overlap.
4115 xfer->endpoint->isoc_next = (framenum + 3) & DWC_OTG_FRAME_MASK;
4116 xfer->endpoint->is_synced = 1;
4117 DPRINTFN(3, "start next=%d\n", xfer->endpoint->isoc_next);
4120 * compute how many milliseconds the insertion is ahead of the
4121 * current frame position:
4123 temp = (xfer->endpoint->isoc_next - framenum) & DWC_OTG_FRAME_MASK;
4126 * pre-compute when the isochronous transfer will be finished:
4128 xfer->isoc_time_complete =
4129 usb_isoc_time_expand(&sc->sc_bus, framenum) + temp + msframes;
4132 dwc_otg_setup_standard_chain(xfer);
4134 /* compute frame number for next insertion */
4135 xfer->endpoint->isoc_next += msframes;
4137 /* start TD chain */
4138 dwc_otg_start_standard_chain(xfer);
4141 struct usb_pipe_methods dwc_otg_device_isoc_methods =
4143 .open = dwc_otg_device_isoc_open,
4144 .close = dwc_otg_device_isoc_close,
4145 .enter = dwc_otg_device_isoc_enter,
4146 .start = dwc_otg_device_isoc_start,
4149 /*------------------------------------------------------------------------*
4150 * DWC OTG root control support
4151 *------------------------------------------------------------------------*
4152 * Simulate a hardware HUB by handling all the necessary requests.
4153 *------------------------------------------------------------------------*/
4155 static const struct usb_device_descriptor dwc_otg_devd = {
4156 .bLength = sizeof(struct usb_device_descriptor),
4157 .bDescriptorType = UDESC_DEVICE,
4158 .bcdUSB = {0x00, 0x02},
4159 .bDeviceClass = UDCLASS_HUB,
4160 .bDeviceSubClass = UDSUBCLASS_HUB,
4161 .bDeviceProtocol = UDPROTO_HSHUBSTT,
4162 .bMaxPacketSize = 64,
4163 .bcdDevice = {0x00, 0x01},
4166 .bNumConfigurations = 1,
4169 static const struct dwc_otg_config_desc dwc_otg_confd = {
4171 .bLength = sizeof(struct usb_config_descriptor),
4172 .bDescriptorType = UDESC_CONFIG,
4173 .wTotalLength[0] = sizeof(dwc_otg_confd),
4175 .bConfigurationValue = 1,
4176 .iConfiguration = 0,
4177 .bmAttributes = UC_SELF_POWERED,
4181 .bLength = sizeof(struct usb_interface_descriptor),
4182 .bDescriptorType = UDESC_INTERFACE,
4184 .bInterfaceClass = UICLASS_HUB,
4185 .bInterfaceSubClass = UISUBCLASS_HUB,
4186 .bInterfaceProtocol = 0,
4189 .bLength = sizeof(struct usb_endpoint_descriptor),
4190 .bDescriptorType = UDESC_ENDPOINT,
4191 .bEndpointAddress = (UE_DIR_IN | DWC_OTG_INTR_ENDPT),
4192 .bmAttributes = UE_INTERRUPT,
4193 .wMaxPacketSize[0] = 8,
4198 #define HSETW(ptr, val) ptr = { (uint8_t)(val), (uint8_t)((val) >> 8) }
4200 static const struct usb_hub_descriptor_min dwc_otg_hubd = {
4201 .bDescLength = sizeof(dwc_otg_hubd),
4202 .bDescriptorType = UDESC_HUB,
4204 HSETW(.wHubCharacteristics, (UHD_PWR_NO_SWITCH | UHD_OC_INDIVIDUAL)),
4205 .bPwrOn2PwrGood = 50,
4206 .bHubContrCurrent = 0,
4207 .DeviceRemovable = {0}, /* port is removable */
4210 #define STRING_VENDOR \
4213 #define STRING_PRODUCT \
4214 "O\0T\0G\0 \0R\0o\0o\0t\0 \0H\0U\0B"
4216 USB_MAKE_STRING_DESC(STRING_VENDOR, dwc_otg_vendor);
4217 USB_MAKE_STRING_DESC(STRING_PRODUCT, dwc_otg_product);
4220 dwc_otg_roothub_exec(struct usb_device *udev,
4221 struct usb_device_request *req, const void **pptr, uint16_t *plength)
4223 struct dwc_otg_softc *sc = DWC_OTG_BUS2SC(udev->bus);
4230 USB_BUS_LOCK_ASSERT(&sc->sc_bus, MA_OWNED);
4233 ptr = (const void *)&sc->sc_hub_temp;
4237 value = UGETW(req->wValue);
4238 index = UGETW(req->wIndex);
4240 /* demultiplex the control request */
4242 switch (req->bmRequestType) {
4243 case UT_READ_DEVICE:
4244 switch (req->bRequest) {
4245 case UR_GET_DESCRIPTOR:
4246 goto tr_handle_get_descriptor;
4248 goto tr_handle_get_config;
4250 goto tr_handle_get_status;
4256 case UT_WRITE_DEVICE:
4257 switch (req->bRequest) {
4258 case UR_SET_ADDRESS:
4259 goto tr_handle_set_address;
4261 goto tr_handle_set_config;
4262 case UR_CLEAR_FEATURE:
4263 goto tr_valid; /* nop */
4264 case UR_SET_DESCRIPTOR:
4265 goto tr_valid; /* nop */
4266 case UR_SET_FEATURE:
4272 case UT_WRITE_ENDPOINT:
4273 switch (req->bRequest) {
4274 case UR_CLEAR_FEATURE:
4275 switch (UGETW(req->wValue)) {
4276 case UF_ENDPOINT_HALT:
4277 goto tr_handle_clear_halt;
4278 case UF_DEVICE_REMOTE_WAKEUP:
4279 goto tr_handle_clear_wakeup;
4284 case UR_SET_FEATURE:
4285 switch (UGETW(req->wValue)) {
4286 case UF_ENDPOINT_HALT:
4287 goto tr_handle_set_halt;
4288 case UF_DEVICE_REMOTE_WAKEUP:
4289 goto tr_handle_set_wakeup;
4294 case UR_SYNCH_FRAME:
4295 goto tr_valid; /* nop */
4301 case UT_READ_ENDPOINT:
4302 switch (req->bRequest) {
4304 goto tr_handle_get_ep_status;
4310 case UT_WRITE_INTERFACE:
4311 switch (req->bRequest) {
4312 case UR_SET_INTERFACE:
4313 goto tr_handle_set_interface;
4314 case UR_CLEAR_FEATURE:
4315 goto tr_valid; /* nop */
4316 case UR_SET_FEATURE:
4322 case UT_READ_INTERFACE:
4323 switch (req->bRequest) {
4324 case UR_GET_INTERFACE:
4325 goto tr_handle_get_interface;
4327 goto tr_handle_get_iface_status;
4333 case UT_WRITE_CLASS_INTERFACE:
4334 case UT_WRITE_VENDOR_INTERFACE:
4338 case UT_READ_CLASS_INTERFACE:
4339 case UT_READ_VENDOR_INTERFACE:
4343 case UT_WRITE_CLASS_DEVICE:
4344 switch (req->bRequest) {
4345 case UR_CLEAR_FEATURE:
4347 case UR_SET_DESCRIPTOR:
4348 case UR_SET_FEATURE:
4355 case UT_WRITE_CLASS_OTHER:
4356 switch (req->bRequest) {
4357 case UR_CLEAR_FEATURE:
4358 goto tr_handle_clear_port_feature;
4359 case UR_SET_FEATURE:
4360 goto tr_handle_set_port_feature;
4361 case UR_CLEAR_TT_BUFFER:
4371 case UT_READ_CLASS_OTHER:
4372 switch (req->bRequest) {
4373 case UR_GET_TT_STATE:
4374 goto tr_handle_get_tt_state;
4376 goto tr_handle_get_port_status;
4382 case UT_READ_CLASS_DEVICE:
4383 switch (req->bRequest) {
4384 case UR_GET_DESCRIPTOR:
4385 goto tr_handle_get_class_descriptor;
4387 goto tr_handle_get_class_status;
4398 tr_handle_get_descriptor:
4399 switch (value >> 8) {
4404 len = sizeof(dwc_otg_devd);
4405 ptr = (const void *)&dwc_otg_devd;
4411 len = sizeof(dwc_otg_confd);
4412 ptr = (const void *)&dwc_otg_confd;
4415 switch (value & 0xff) {
4416 case 0: /* Language table */
4417 len = sizeof(usb_string_lang_en);
4418 ptr = (const void *)&usb_string_lang_en;
4421 case 1: /* Vendor */
4422 len = sizeof(dwc_otg_vendor);
4423 ptr = (const void *)&dwc_otg_vendor;
4426 case 2: /* Product */
4427 len = sizeof(dwc_otg_product);
4428 ptr = (const void *)&dwc_otg_product;
4439 tr_handle_get_config:
4441 sc->sc_hub_temp.wValue[0] = sc->sc_conf;
4444 tr_handle_get_status:
4446 USETW(sc->sc_hub_temp.wValue, UDS_SELF_POWERED);
4449 tr_handle_set_address:
4450 if (value & 0xFF00) {
4453 sc->sc_rt_addr = value;
4456 tr_handle_set_config:
4460 sc->sc_conf = value;
4463 tr_handle_get_interface:
4465 sc->sc_hub_temp.wValue[0] = 0;
4468 tr_handle_get_tt_state:
4469 tr_handle_get_class_status:
4470 tr_handle_get_iface_status:
4471 tr_handle_get_ep_status:
4473 USETW(sc->sc_hub_temp.wValue, 0);
4477 tr_handle_set_interface:
4478 tr_handle_set_wakeup:
4479 tr_handle_clear_wakeup:
4480 tr_handle_clear_halt:
4483 tr_handle_clear_port_feature:
4487 DPRINTFN(9, "UR_CLEAR_PORT_FEATURE on port %d\n", index);
4490 case UHF_PORT_SUSPEND:
4491 dwc_otg_wakeup_peer(sc);
4494 case UHF_PORT_ENABLE:
4495 if (sc->sc_flags.status_device_mode == 0) {
4496 DWC_OTG_WRITE_4(sc, DOTG_HPRT,
4497 sc->sc_hprt_val | HPRT_PRTENA);
4499 sc->sc_flags.port_enabled = 0;
4502 case UHF_C_PORT_RESET:
4503 sc->sc_flags.change_reset = 0;
4506 case UHF_C_PORT_ENABLE:
4507 sc->sc_flags.change_enabled = 0;
4510 case UHF_C_PORT_OVER_CURRENT:
4511 sc->sc_flags.change_over_current = 0;
4515 case UHF_PORT_INDICATOR:
4519 case UHF_PORT_POWER:
4520 sc->sc_flags.port_powered = 0;
4521 if (sc->sc_mode == DWC_MODE_HOST || sc->sc_mode == DWC_MODE_OTG) {
4522 sc->sc_hprt_val = 0;
4523 DWC_OTG_WRITE_4(sc, DOTG_HPRT, HPRT_PRTENA);
4525 dwc_otg_pull_down(sc);
4526 dwc_otg_clocks_off(sc);
4529 case UHF_C_PORT_CONNECTION:
4530 /* clear connect change flag */
4531 sc->sc_flags.change_connect = 0;
4534 case UHF_C_PORT_SUSPEND:
4535 sc->sc_flags.change_suspend = 0;
4539 err = USB_ERR_IOERROR;
4544 tr_handle_set_port_feature:
4548 DPRINTFN(9, "UR_SET_PORT_FEATURE\n");
4551 case UHF_PORT_ENABLE:
4554 case UHF_PORT_SUSPEND:
4555 if (sc->sc_flags.status_device_mode == 0) {
4556 /* set suspend BIT */
4557 sc->sc_hprt_val |= HPRT_PRTSUSP;
4558 DWC_OTG_WRITE_4(sc, DOTG_HPRT, sc->sc_hprt_val);
4560 /* generate HUB suspend event */
4561 dwc_otg_suspend_irq(sc);
4565 case UHF_PORT_RESET:
4566 if (sc->sc_flags.status_device_mode == 0) {
4568 DPRINTF("PORT RESET\n");
4570 /* enable PORT reset */
4571 DWC_OTG_WRITE_4(sc, DOTG_HPRT, sc->sc_hprt_val | HPRT_PRTRST);
4573 /* Wait 62.5ms for reset to complete */
4574 usb_pause_mtx(&sc->sc_bus.bus_mtx, hz / 16);
4576 DWC_OTG_WRITE_4(sc, DOTG_HPRT, sc->sc_hprt_val);
4578 /* Wait 62.5ms for reset to complete */
4579 usb_pause_mtx(&sc->sc_bus.bus_mtx, hz / 16);
4582 (void) dwc_otg_init_fifo(sc, DWC_MODE_HOST);
4584 sc->sc_flags.change_reset = 1;
4586 err = USB_ERR_IOERROR;
4591 case UHF_PORT_INDICATOR:
4594 case UHF_PORT_POWER:
4595 sc->sc_flags.port_powered = 1;
4596 if (sc->sc_mode == DWC_MODE_HOST || sc->sc_mode == DWC_MODE_OTG) {
4597 sc->sc_hprt_val |= HPRT_PRTPWR;
4598 DWC_OTG_WRITE_4(sc, DOTG_HPRT, sc->sc_hprt_val);
4600 if (sc->sc_mode == DWC_MODE_DEVICE || sc->sc_mode == DWC_MODE_OTG) {
4601 /* pull up D+, if any */
4602 dwc_otg_pull_up(sc);
4606 err = USB_ERR_IOERROR;
4611 tr_handle_get_port_status:
4613 DPRINTFN(9, "UR_GET_PORT_STATUS\n");
4618 if (sc->sc_flags.status_vbus)
4619 dwc_otg_clocks_on(sc);
4621 dwc_otg_clocks_off(sc);
4623 /* Select Device Side Mode */
4625 if (sc->sc_flags.status_device_mode) {
4626 value = UPS_PORT_MODE_DEVICE;
4627 dwc_otg_timer_stop(sc);
4630 dwc_otg_timer_start(sc);
4633 if (sc->sc_flags.status_high_speed)
4634 value |= UPS_HIGH_SPEED;
4635 else if (sc->sc_flags.status_low_speed)
4636 value |= UPS_LOW_SPEED;
4638 if (sc->sc_flags.port_powered)
4639 value |= UPS_PORT_POWER;
4641 if (sc->sc_flags.port_enabled)
4642 value |= UPS_PORT_ENABLED;
4644 if (sc->sc_flags.port_over_current)
4645 value |= UPS_OVERCURRENT_INDICATOR;
4647 if (sc->sc_flags.status_vbus &&
4648 sc->sc_flags.status_bus_reset)
4649 value |= UPS_CURRENT_CONNECT_STATUS;
4651 if (sc->sc_flags.status_suspend)
4652 value |= UPS_SUSPEND;
4654 USETW(sc->sc_hub_temp.ps.wPortStatus, value);
4658 if (sc->sc_flags.change_connect)
4659 value |= UPS_C_CONNECT_STATUS;
4660 if (sc->sc_flags.change_suspend)
4661 value |= UPS_C_SUSPEND;
4662 if (sc->sc_flags.change_reset)
4663 value |= UPS_C_PORT_RESET;
4664 if (sc->sc_flags.change_over_current)
4665 value |= UPS_C_OVERCURRENT_INDICATOR;
4667 USETW(sc->sc_hub_temp.ps.wPortChange, value);
4668 len = sizeof(sc->sc_hub_temp.ps);
4671 tr_handle_get_class_descriptor:
4675 ptr = (const void *)&dwc_otg_hubd;
4676 len = sizeof(dwc_otg_hubd);
4680 err = USB_ERR_STALLED;
4689 dwc_otg_xfer_setup(struct usb_setup_params *parm)
4691 struct usb_xfer *xfer;
4698 xfer = parm->curr_xfer;
4701 * NOTE: This driver does not use any of the parameters that
4702 * are computed from the following values. Just set some
4703 * reasonable dummies:
4705 parm->hc_max_packet_size = 0x500;
4706 parm->hc_max_packet_count = 3;
4707 parm->hc_max_frame_size = 3 * 0x500;
4709 usbd_transfer_setup_sub(parm);
4712 * compute maximum number of TDs
4714 ep_type = (xfer->endpoint->edesc->bmAttributes & UE_XFERTYPE);
4716 if (ep_type == UE_CONTROL) {
4718 ntd = xfer->nframes + 1 /* STATUS */ + 1 /* SYNC 1 */
4719 + 1 /* SYNC 2 */ + 1 /* SYNC 3 */;
4722 ntd = xfer->nframes + 1 /* SYNC */ ;
4726 * check if "usbd_transfer_setup_sub" set an error
4732 * allocate transfer descriptors
4736 ep_no = xfer->endpointno & UE_ADDR;
4739 * Check for a valid endpoint profile in USB device mode:
4741 if (xfer->flags_int.usb_mode == USB_MODE_DEVICE) {
4742 const struct usb_hw_ep_profile *pf;
4744 dwc_otg_get_hw_ep_profile(parm->udev, &pf, ep_no);
4747 /* should not happen */
4748 parm->err = USB_ERR_INVAL;
4754 parm->size[0] += ((-parm->size[0]) & (USB_HOST_ALIGN - 1));
4756 for (n = 0; n != ntd; n++) {
4758 struct dwc_otg_td *td;
4762 td = USB_ADD_BYTES(parm->buf, parm->size[0]);
4764 /* compute shared bandwidth resource index for TT */
4765 if (parm->udev->parent_hs_hub != NULL && parm->udev->speed != USB_SPEED_HIGH) {
4766 if (parm->udev->parent_hs_hub->ddesc.bDeviceProtocol == UDPROTO_HSHUBMTT)
4767 td->tt_index = parm->udev->device_index;
4769 td->tt_index = parm->udev->parent_hs_hub->device_index;
4771 td->tt_index = parm->udev->device_index;
4775 td->max_packet_size = xfer->max_packet_size;
4776 td->max_packet_count = xfer->max_packet_count;
4778 td->ep_type = ep_type;
4779 td->obj_next = last_obj;
4783 parm->size[0] += sizeof(*td);
4786 xfer->td_start[0] = last_obj;
4790 dwc_otg_xfer_unsetup(struct usb_xfer *xfer)
4796 dwc_otg_ep_init(struct usb_device *udev, struct usb_endpoint_descriptor *edesc,
4797 struct usb_endpoint *ep)
4799 struct dwc_otg_softc *sc = DWC_OTG_BUS2SC(udev->bus);
4801 DPRINTFN(2, "endpoint=%p, addr=%d, endpt=%d, mode=%d (%d,%d)\n",
4803 edesc->bEndpointAddress, udev->flags.usb_mode,
4804 sc->sc_rt_addr, udev->device_index);
4806 if (udev->device_index != sc->sc_rt_addr) {
4808 if (udev->flags.usb_mode == USB_MODE_DEVICE) {
4809 if (udev->speed != USB_SPEED_FULL &&
4810 udev->speed != USB_SPEED_HIGH) {
4815 if (udev->speed == USB_SPEED_HIGH) {
4816 if ((UGETW(edesc->wMaxPacketSize) >> 11) & 3) {
4817 /* high bandwidth endpoint - not tested */
4818 DPRINTF("High Bandwidth Endpoint - not tested\n");
4823 if ((edesc->bmAttributes & UE_XFERTYPE) == UE_ISOCHRONOUS)
4824 ep->methods = &dwc_otg_device_isoc_methods;
4826 ep->methods = &dwc_otg_device_non_isoc_methods;
4831 dwc_otg_set_hw_power_sleep(struct usb_bus *bus, uint32_t state)
4833 struct dwc_otg_softc *sc = DWC_OTG_BUS2SC(bus);
4836 case USB_HW_POWER_SUSPEND:
4837 dwc_otg_suspend(sc);
4839 case USB_HW_POWER_SHUTDOWN:
4842 case USB_HW_POWER_RESUME:
4851 dwc_otg_get_dma_delay(struct usb_device *udev, uint32_t *pus)
4853 /* DMA delay - wait until any use of memory is finished */
4854 *pus = (2125); /* microseconds */
4858 dwc_otg_device_resume(struct usb_device *udev)
4862 /* poll all transfers again to restart resumed ones */
4863 dwc_otg_do_poll(udev->bus);
4867 dwc_otg_device_suspend(struct usb_device *udev)
4872 struct usb_bus_methods dwc_otg_bus_methods =
4874 .endpoint_init = &dwc_otg_ep_init,
4875 .xfer_setup = &dwc_otg_xfer_setup,
4876 .xfer_unsetup = &dwc_otg_xfer_unsetup,
4877 .get_hw_ep_profile = &dwc_otg_get_hw_ep_profile,
4878 .xfer_stall = &dwc_otg_xfer_stall,
4879 .set_stall = &dwc_otg_set_stall,
4880 .clear_stall = &dwc_otg_clear_stall,
4881 .roothub_exec = &dwc_otg_roothub_exec,
4882 .xfer_poll = &dwc_otg_do_poll,
4883 .device_state_change = &dwc_otg_device_state_change,
4884 .set_hw_power_sleep = &dwc_otg_set_hw_power_sleep,
4885 .get_dma_delay = &dwc_otg_get_dma_delay,
4886 .device_resume = &dwc_otg_device_resume,
4887 .device_suspend = &dwc_otg_device_suspend,