3 * Copyright (c) 2015 Daisuke Aoyama. All rights reserved.
4 * Copyright (c) 2012-2015 Hans Petter Selasky. All rights reserved.
5 * Copyright (c) 2010-2011 Aleksandr Rybalko. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * This file contains the driver for the DesignWare series USB 2.0 OTG
35 * LIMITATION: Drivers must be bound to all OUT endpoints in the
36 * active configuration for this driver to work properly. Blocking any
37 * OUT endpoint will block all OUT endpoints including the control
38 * endpoint. Usually this is not a problem.
42 * NOTE: Writing to non-existing registers appears to cause an
46 #ifdef USB_GLOBAL_INCLUDE_FILE
47 #include USB_GLOBAL_INCLUDE_FILE
49 #include <sys/stdint.h>
50 #include <sys/stddef.h>
51 #include <sys/param.h>
52 #include <sys/queue.h>
53 #include <sys/types.h>
54 #include <sys/systm.h>
55 #include <sys/kernel.h>
57 #include <sys/module.h>
59 #include <sys/mutex.h>
60 #include <sys/condvar.h>
61 #include <sys/sysctl.h>
63 #include <sys/unistd.h>
64 #include <sys/callout.h>
65 #include <sys/malloc.h>
68 #include <dev/usb/usb.h>
69 #include <dev/usb/usbdi.h>
71 #define USB_DEBUG_VAR dwc_otg_debug
73 #include <dev/usb/usb_core.h>
74 #include <dev/usb/usb_debug.h>
75 #include <dev/usb/usb_busdma.h>
76 #include <dev/usb/usb_process.h>
77 #include <dev/usb/usb_transfer.h>
78 #include <dev/usb/usb_device.h>
79 #include <dev/usb/usb_hub.h>
80 #include <dev/usb/usb_util.h>
82 #include <dev/usb/usb_controller.h>
83 #include <dev/usb/usb_bus.h>
84 #endif /* USB_GLOBAL_INCLUDE_FILE */
86 #include <dev/usb/controller/dwc_otg.h>
87 #include <dev/usb/controller/dwc_otgreg.h>
89 #define DWC_OTG_BUS2SC(bus) \
90 ((struct dwc_otg_softc *)(((uint8_t *)(bus)) - \
91 ((uint8_t *)&(((struct dwc_otg_softc *)0)->sc_bus))))
93 #define DWC_OTG_PC2UDEV(pc) \
94 (USB_DMATAG_TO_XROOT((pc)->tag_parent)->udev)
96 #define DWC_OTG_MSK_GINT_ENABLED \
97 (GINTMSK_ENUMDONEMSK | \
99 GINTMSK_USBSUSPMSK | \
100 GINTMSK_IEPINTMSK | \
101 GINTMSK_SESSREQINTMSK | \
102 GINTMSK_RXFLVLMSK | \
103 GINTMSK_HCHINTMSK | \
104 GINTMSK_OTGINTMSK | \
107 #define DWC_OTG_MSK_GINT_THREAD_IRQ \
108 (GINTSTS_USBRST | GINTSTS_ENUMDONE | GINTSTS_PRTINT | \
109 GINTSTS_WKUPINT | GINTSTS_USBSUSP | GINTMSK_OTGINTMSK | \
112 #define DWC_OTG_PHY_ULPI 0
113 #define DWC_OTG_PHY_HSIC 1
114 #define DWC_OTG_PHY_INTERNAL 2
116 #ifndef DWC_OTG_PHY_DEFAULT
117 #define DWC_OTG_PHY_DEFAULT DWC_OTG_PHY_ULPI
120 static int dwc_otg_phy_type = DWC_OTG_PHY_DEFAULT;
122 static SYSCTL_NODE(_hw_usb, OID_AUTO, dwc_otg, CTLFLAG_RW, 0, "USB DWC OTG");
123 SYSCTL_INT(_hw_usb_dwc_otg, OID_AUTO, phy_type, CTLFLAG_RDTUN,
124 &dwc_otg_phy_type, 0, "DWC OTG PHY TYPE - 0/1/2 - ULPI/HSIC/INTERNAL");
125 TUNABLE_INT("hw.usb.dwc_otg.phy_type", &dwc_otg_phy_type);
128 static int dwc_otg_debug;
130 SYSCTL_INT(_hw_usb_dwc_otg, OID_AUTO, debug, CTLFLAG_RW,
131 &dwc_otg_debug, 0, "DWC OTG debug level");
134 #define DWC_OTG_INTR_ENDPT 1
138 struct usb_bus_methods dwc_otg_bus_methods;
139 struct usb_pipe_methods dwc_otg_device_non_isoc_methods;
140 struct usb_pipe_methods dwc_otg_device_isoc_methods;
142 static dwc_otg_cmd_t dwc_otg_setup_rx;
143 static dwc_otg_cmd_t dwc_otg_data_rx;
144 static dwc_otg_cmd_t dwc_otg_data_tx;
145 static dwc_otg_cmd_t dwc_otg_data_tx_sync;
147 static dwc_otg_cmd_t dwc_otg_host_setup_tx;
148 static dwc_otg_cmd_t dwc_otg_host_data_tx;
149 static dwc_otg_cmd_t dwc_otg_host_data_rx;
151 static void dwc_otg_device_done(struct usb_xfer *, usb_error_t);
152 static void dwc_otg_do_poll(struct usb_bus *);
153 static void dwc_otg_standard_done(struct usb_xfer *);
154 static void dwc_otg_root_intr(struct dwc_otg_softc *);
155 static void dwc_otg_interrupt_poll_locked(struct dwc_otg_softc *);
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 (refer to PGM) */
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 if (mode == DWC_MODE_HOST) {
243 /* reset active endpoints */
244 sc->sc_active_rx_ep = 0;
246 /* split equally for periodic and non-periodic */
249 DPRINTF("PTX/NPTX FIFO=%u\n", fifo_size);
251 /* align to 4 bytes boundary */
254 DWC_OTG_WRITE_4(sc, DOTG_GNPTXFSIZ,
255 ((fifo_size / 4) << 16) |
258 tx_start += fifo_size;
260 for (x = 0; x != sc->sc_host_ch_max; x++) {
261 /* enable all host interrupts */
262 DWC_OTG_WRITE_4(sc, DOTG_HCINTMSK(x),
266 DWC_OTG_WRITE_4(sc, DOTG_HPTXFSIZ,
267 ((fifo_size / 4) << 16) |
270 /* reset host channel state */
271 memset(sc->sc_chan_state, 0, sizeof(sc->sc_chan_state));
273 /* enable all host channel interrupts */
274 DWC_OTG_WRITE_4(sc, DOTG_HAINTMSK,
275 (1U << sc->sc_host_ch_max) - 1U);
278 if (mode == DWC_MODE_DEVICE) {
280 DWC_OTG_WRITE_4(sc, DOTG_GNPTXFSIZ,
281 (0x10 << 16) | (tx_start / 4));
285 /* setup control endpoint profile */
286 sc->sc_hw_ep_profile[0].usb = dwc_otg_ep_profile[0];
288 /* reset active endpoints */
289 sc->sc_active_rx_ep = 1;
291 for (x = 1; x != sc->sc_dev_ep_max; x++) {
293 pf = sc->sc_hw_ep_profile + x;
295 pf->usb.max_out_frame_size = 1024 * 3;
296 pf->usb.is_simplex = 0; /* assume duplex */
297 pf->usb.support_bulk = 1;
298 pf->usb.support_interrupt = 1;
299 pf->usb.support_isochronous = 1;
300 pf->usb.support_out = 1;
302 if (x < sc->sc_dev_in_ep_max) {
305 limit = (x == 1) ? MIN(DWC_OTG_TX_MAX_FIFO_SIZE,
306 DWC_OTG_MAX_TXN) : MIN(DWC_OTG_MAX_TXN / 2,
307 DWC_OTG_TX_MAX_FIFO_SIZE);
309 /* see if there is enough FIFO space */
310 if (limit <= fifo_size) {
311 pf->max_buffer = limit;
312 pf->usb.support_in = 1;
314 limit = MIN(DWC_OTG_TX_MAX_FIFO_SIZE, 0x40);
315 if (limit <= fifo_size) {
316 pf->usb.support_in = 1;
318 pf->usb.is_simplex = 1;
323 DWC_OTG_WRITE_4(sc, DOTG_DIEPTXF(x),
324 ((limit / 4) << 16) | (tx_start / 4));
327 pf->usb.max_in_frame_size = limit;
329 pf->usb.is_simplex = 1;
332 DPRINTF("FIFO%d = IN:%d / OUT:%d\n", x,
333 pf->usb.max_in_frame_size,
334 pf->usb.max_out_frame_size);
339 dwc_otg_tx_fifo_reset(sc, GRSTCTL_RXFFLSH);
341 if (mode != DWC_MODE_OTG) {
342 /* reset all TX FIFOs */
343 dwc_otg_tx_fifo_reset(sc,
344 GRSTCTL_TXFIFO(0x10) |
347 /* reset active endpoints */
348 sc->sc_active_rx_ep = 0;
350 /* reset host channel state */
351 memset(sc->sc_chan_state, 0, sizeof(sc->sc_chan_state));
357 dwc_otg_update_host_frame_interval(struct dwc_otg_softc *sc)
361 * Disabled until further. Assuming that the register is already
362 * programmed correctly by the boot loader.
367 /* setup HOST frame interval register, based on existing value */
368 temp = DWC_OTG_READ_4(sc, DOTG_HFIR) & HFIR_FRINT_MASK;
374 /* figure out nearest X-tal value */
382 if (sc->sc_flags.status_high_speed)
387 DPRINTF("HFIR=0x%08x\n", temp);
389 DWC_OTG_WRITE_4(sc, DOTG_HFIR, temp);
394 dwc_otg_clocks_on(struct dwc_otg_softc *sc)
396 if (sc->sc_flags.clocks_off &&
397 sc->sc_flags.port_powered) {
401 /* TODO - platform specific */
403 sc->sc_flags.clocks_off = 0;
408 dwc_otg_clocks_off(struct dwc_otg_softc *sc)
410 if (!sc->sc_flags.clocks_off) {
414 /* TODO - platform specific */
416 sc->sc_flags.clocks_off = 1;
421 dwc_otg_pull_up(struct dwc_otg_softc *sc)
425 /* pullup D+, if possible */
427 if (!sc->sc_flags.d_pulled_up &&
428 sc->sc_flags.port_powered) {
429 sc->sc_flags.d_pulled_up = 1;
431 temp = DWC_OTG_READ_4(sc, DOTG_DCTL);
432 temp &= ~DCTL_SFTDISCON;
433 DWC_OTG_WRITE_4(sc, DOTG_DCTL, temp);
438 dwc_otg_pull_down(struct dwc_otg_softc *sc)
442 /* pulldown D+, if possible */
444 if (sc->sc_flags.d_pulled_up) {
445 sc->sc_flags.d_pulled_up = 0;
447 temp = DWC_OTG_READ_4(sc, DOTG_DCTL);
448 temp |= DCTL_SFTDISCON;
449 DWC_OTG_WRITE_4(sc, DOTG_DCTL, temp);
454 dwc_otg_enable_sof_irq(struct dwc_otg_softc *sc)
456 /* In device mode we don't use the SOF interrupt */
457 if (sc->sc_flags.status_device_mode != 0)
459 /* Ensure the SOF interrupt is not disabled */
461 /* Check if the SOF interrupt is already enabled */
462 if ((sc->sc_irq_mask & GINTMSK_SOFMSK) != 0)
464 sc->sc_irq_mask |= GINTMSK_SOFMSK;
465 DWC_OTG_WRITE_4(sc, DOTG_GINTMSK, sc->sc_irq_mask);
469 dwc_otg_resume_irq(struct dwc_otg_softc *sc)
471 if (sc->sc_flags.status_suspend) {
472 /* update status bits */
473 sc->sc_flags.status_suspend = 0;
474 sc->sc_flags.change_suspend = 1;
476 if (sc->sc_flags.status_device_mode) {
478 * Disable resume interrupt and enable suspend
481 sc->sc_irq_mask &= ~GINTMSK_WKUPINTMSK;
482 sc->sc_irq_mask |= GINTMSK_USBSUSPMSK;
483 DWC_OTG_WRITE_4(sc, DOTG_GINTMSK, sc->sc_irq_mask);
486 /* complete root HUB interrupt endpoint */
487 dwc_otg_root_intr(sc);
492 dwc_otg_suspend_irq(struct dwc_otg_softc *sc)
494 if (!sc->sc_flags.status_suspend) {
495 /* update status bits */
496 sc->sc_flags.status_suspend = 1;
497 sc->sc_flags.change_suspend = 1;
499 if (sc->sc_flags.status_device_mode) {
501 * Disable suspend interrupt and enable resume
504 sc->sc_irq_mask &= ~GINTMSK_USBSUSPMSK;
505 sc->sc_irq_mask |= GINTMSK_WKUPINTMSK;
506 DWC_OTG_WRITE_4(sc, DOTG_GINTMSK, sc->sc_irq_mask);
509 /* complete root HUB interrupt endpoint */
510 dwc_otg_root_intr(sc);
515 dwc_otg_wakeup_peer(struct dwc_otg_softc *sc)
517 if (!sc->sc_flags.status_suspend)
520 DPRINTFN(5, "Remote wakeup\n");
522 if (sc->sc_flags.status_device_mode) {
525 /* enable remote wakeup signalling */
526 temp = DWC_OTG_READ_4(sc, DOTG_DCTL);
527 temp |= DCTL_RMTWKUPSIG;
528 DWC_OTG_WRITE_4(sc, DOTG_DCTL, temp);
530 /* Wait 8ms for remote wakeup to complete. */
531 usb_pause_mtx(&sc->sc_bus.bus_mtx, hz / 125);
533 temp &= ~DCTL_RMTWKUPSIG;
534 DWC_OTG_WRITE_4(sc, DOTG_DCTL, temp);
536 /* enable USB port */
537 DWC_OTG_WRITE_4(sc, DOTG_PCGCCTL, 0);
540 usb_pause_mtx(&sc->sc_bus.bus_mtx, hz / 100);
543 sc->sc_hprt_val |= HPRT_PRTRES;
544 DWC_OTG_WRITE_4(sc, DOTG_HPRT, sc->sc_hprt_val);
546 /* Wait 100ms for resume signalling to complete. */
547 usb_pause_mtx(&sc->sc_bus.bus_mtx, hz / 10);
549 /* clear suspend and resume */
550 sc->sc_hprt_val &= ~(HPRT_PRTSUSP | HPRT_PRTRES);
551 DWC_OTG_WRITE_4(sc, DOTG_HPRT, sc->sc_hprt_val);
554 usb_pause_mtx(&sc->sc_bus.bus_mtx, hz / 250);
557 /* need to fake resume IRQ */
558 dwc_otg_resume_irq(sc);
562 dwc_otg_set_address(struct dwc_otg_softc *sc, uint8_t addr)
566 DPRINTFN(5, "addr=%d\n", addr);
568 temp = DWC_OTG_READ_4(sc, DOTG_DCFG);
569 temp &= ~DCFG_DEVADDR_SET(0x7F);
570 temp |= DCFG_DEVADDR_SET(addr);
571 DWC_OTG_WRITE_4(sc, DOTG_DCFG, temp);
575 dwc_otg_common_rx_ack(struct dwc_otg_softc *sc)
577 DPRINTFN(5, "RX status clear\n");
579 /* enable RX FIFO level interrupt */
580 sc->sc_irq_mask |= GINTMSK_RXFLVLMSK;
581 DWC_OTG_WRITE_4(sc, DOTG_GINTMSK, sc->sc_irq_mask);
583 /* clear cached status */
584 sc->sc_last_rx_status = 0;
588 dwc_otg_clear_hcint(struct dwc_otg_softc *sc, uint8_t x)
592 /* clear all pending interrupts */
593 hcint = DWC_OTG_READ_4(sc, DOTG_HCINT(x));
594 DWC_OTG_WRITE_4(sc, DOTG_HCINT(x), hcint);
596 /* clear buffered interrupts */
597 sc->sc_chan_state[x].hcint = 0;
601 dwc_otg_host_check_tx_fifo_empty(struct dwc_otg_softc *sc, struct dwc_otg_td *td)
605 temp = DWC_OTG_READ_4(sc, DOTG_GINTSTS);
607 if (td->ep_type == UE_ISOCHRONOUS) {
609 * NOTE: USB INTERRUPT transactions are executed like
610 * USB CONTROL transactions! See the setup standard
611 * chain function for more information.
613 if (!(temp & GINTSTS_PTXFEMP)) {
614 DPRINTF("Periodic TX FIFO is not empty\n");
615 if (!(sc->sc_irq_mask & GINTMSK_PTXFEMPMSK)) {
616 sc->sc_irq_mask |= GINTMSK_PTXFEMPMSK;
617 DWC_OTG_WRITE_4(sc, DOTG_GINTMSK, sc->sc_irq_mask);
619 return (1); /* busy */
622 if (!(temp & GINTSTS_NPTXFEMP)) {
623 DPRINTF("Non-periodic TX FIFO is not empty\n");
624 if (!(sc->sc_irq_mask & GINTMSK_NPTXFEMPMSK)) {
625 sc->sc_irq_mask |= GINTMSK_NPTXFEMPMSK;
626 DWC_OTG_WRITE_4(sc, DOTG_GINTMSK, sc->sc_irq_mask);
628 return (1); /* busy */
631 return (0); /* ready for transmit */
635 dwc_otg_host_channel_alloc(struct dwc_otg_softc *sc,
636 struct dwc_otg_td *td, uint8_t is_out)
642 if (td->channel[0] < DWC_OTG_MAX_CHANNELS)
643 return (0); /* already allocated */
645 /* check if device is suspended */
646 if (DWC_OTG_PC2UDEV(td->pc)->flags.self_suspended != 0)
647 return (1); /* busy - cannot transfer data */
649 /* compute needed TX FIFO size */
651 if (dwc_otg_host_check_tx_fifo_empty(sc, td) != 0)
652 return (1); /* busy - cannot transfer data */
654 z = td->max_packet_count;
655 for (x = y = 0; x != sc->sc_host_ch_max; x++) {
656 /* check if channel is allocated */
657 if (sc->sc_chan_state[x].allocated != 0)
659 /* check if channel is still enabled */
660 if (sc->sc_chan_state[x].wait_halted != 0)
662 /* store channel number */
663 td->channel[y++] = x;
664 /* check if we got all channels */
669 /* reset channel variable */
670 td->channel[0] = DWC_OTG_MAX_CHANNELS;
671 td->channel[1] = DWC_OTG_MAX_CHANNELS;
672 td->channel[2] = DWC_OTG_MAX_CHANNELS;
674 dwc_otg_enable_sof_irq(sc);
675 return (1); /* busy - not enough channels */
678 for (y = 0; y != z; y++) {
682 sc->sc_chan_state[x].allocated = 1;
684 /* set wait halted */
685 sc->sc_chan_state[x].wait_halted = 1;
687 /* clear interrupts */
688 dwc_otg_clear_hcint(sc, x);
690 DPRINTF("CH=%d HCCHAR=0x%08x "
691 "HCSPLT=0x%08x\n", x, td->hcchar, td->hcsplt);
693 /* set active channel */
694 sc->sc_active_rx_ep |= (1 << x);
696 return (0); /* allocated */
700 dwc_otg_host_channel_free_sub(struct dwc_otg_softc *sc, struct dwc_otg_td *td, uint8_t index)
705 if (td->channel[index] >= DWC_OTG_MAX_CHANNELS)
706 return; /* already freed */
709 x = td->channel[index];
710 td->channel[index] = DWC_OTG_MAX_CHANNELS;
712 DPRINTF("CH=%d\n", x);
715 * We need to let programmed host channels run till complete
716 * else the host channel will stop functioning.
718 sc->sc_chan_state[x].allocated = 0;
720 /* ack any pending messages */
721 if (sc->sc_last_rx_status != 0 &&
722 GRXSTSRD_CHNUM_GET(sc->sc_last_rx_status) == x) {
723 dwc_otg_common_rx_ack(sc);
726 /* clear active channel */
727 sc->sc_active_rx_ep &= ~(1 << x);
729 /* check if already halted */
730 if (sc->sc_chan_state[x].wait_halted == 0)
733 /* disable host channel */
734 hcchar = DWC_OTG_READ_4(sc, DOTG_HCCHAR(x));
735 if (hcchar & HCCHAR_CHENA) {
736 DPRINTF("Halting channel %d\n", x);
737 DWC_OTG_WRITE_4(sc, DOTG_HCCHAR(x),
738 hcchar | HCCHAR_CHDIS);
739 /* don't write HCCHAR until the channel is halted */
741 sc->sc_chan_state[x].wait_halted = 0;
746 dwc_otg_host_channel_free(struct dwc_otg_softc *sc, struct dwc_otg_td *td)
749 for (x = 0; x != td->max_packet_count; x++)
750 dwc_otg_host_channel_free_sub(sc, td, x);
754 dwc_otg_host_dump_rx(struct dwc_otg_softc *sc, struct dwc_otg_td *td)
757 /* dump any pending messages */
758 if (sc->sc_last_rx_status == 0)
760 for (x = 0; x != td->max_packet_count; x++) {
761 if (td->channel[x] >= DWC_OTG_MAX_CHANNELS ||
762 td->channel[x] != GRXSTSRD_CHNUM_GET(sc->sc_last_rx_status))
764 dwc_otg_common_rx_ack(sc);
769 dwc_otg_host_setup_tx(struct dwc_otg_softc *sc, struct dwc_otg_td *td)
771 struct usb_device_request req __aligned(4);
776 dwc_otg_host_dump_rx(sc, td);
778 if (td->channel[0] < DWC_OTG_MAX_CHANNELS) {
779 hcint = sc->sc_chan_state[td->channel[0]].hcint;
781 DPRINTF("CH=%d ST=%d HCINT=0x%08x HCCHAR=0x%08x HCTSIZ=0x%08x\n",
782 td->channel[0], td->state, hcint,
783 DWC_OTG_READ_4(sc, DOTG_HCCHAR(td->channel[0])),
784 DWC_OTG_READ_4(sc, DOTG_HCTSIZ(td->channel[0])));
790 if (hcint & (HCINT_RETRY |
791 HCINT_ACK | HCINT_NYET)) {
792 /* give success bits priority over failure bits */
793 } else if (hcint & HCINT_STALL) {
794 DPRINTF("CH=%d STALL\n", td->channel[0]);
798 } else if (hcint & HCINT_ERRORS) {
799 DPRINTF("CH=%d ERROR\n", td->channel[0]);
801 if (td->hcsplt != 0 || td->errcnt >= 3) {
807 if (hcint & (HCINT_ERRORS | HCINT_RETRY |
808 HCINT_ACK | HCINT_NYET)) {
809 if (!(hcint & HCINT_ERRORS))
815 case DWC_CHAN_ST_START:
818 case DWC_CHAN_ST_WAIT_ANE:
819 if (hcint & (HCINT_RETRY | HCINT_ERRORS)) {
821 td->tt_scheduled = 0;
823 } else if (hcint & (HCINT_ACK | HCINT_NYET)) {
824 td->offset += td->tx_bytes;
825 td->remainder -= td->tx_bytes;
827 td->tt_scheduled = 0;
832 case DWC_CHAN_ST_WAIT_S_ANE:
833 if (hcint & (HCINT_RETRY | HCINT_ERRORS)) {
835 td->tt_scheduled = 0;
837 } else if (hcint & (HCINT_ACK | HCINT_NYET)) {
842 case DWC_CHAN_ST_WAIT_C_ANE:
843 if (hcint & HCINT_NYET) {
845 } else if (hcint & (HCINT_RETRY | HCINT_ERRORS)) {
847 td->tt_scheduled = 0;
849 } else if (hcint & HCINT_ACK) {
850 td->offset += td->tx_bytes;
851 td->remainder -= td->tx_bytes;
857 case DWC_CHAN_ST_WAIT_C_PKT:
866 /* free existing channel, if any */
867 dwc_otg_host_channel_free(sc, td);
869 if (sizeof(req) != td->remainder) {
874 if (td->hcsplt != 0) {
875 delta = td->tt_start_slot - sc->sc_last_frame_num - 1;
876 if (td->tt_scheduled == 0 || delta < DWC_OTG_TT_SLOT_MAX) {
877 td->state = DWC_CHAN_ST_START;
880 delta = sc->sc_last_frame_num - td->tt_start_slot;
883 td->tt_scheduled = 0;
884 td->state = DWC_CHAN_ST_START;
889 /* allocate a new channel */
890 if (dwc_otg_host_channel_alloc(sc, td, 1)) {
891 td->state = DWC_CHAN_ST_START;
895 if (td->hcsplt != 0) {
896 td->hcsplt &= ~HCSPLT_COMPSPLT;
897 td->state = DWC_CHAN_ST_WAIT_S_ANE;
899 td->state = DWC_CHAN_ST_WAIT_ANE;
902 usbd_copy_out(td->pc, 0, &req, sizeof(req));
904 DWC_OTG_WRITE_4(sc, DOTG_HCTSIZ(td->channel[0]),
905 (sizeof(req) << HCTSIZ_XFERSIZE_SHIFT) |
906 (1 << HCTSIZ_PKTCNT_SHIFT) |
907 (HCTSIZ_PID_SETUP << HCTSIZ_PID_SHIFT));
909 DWC_OTG_WRITE_4(sc, DOTG_HCSPLT(td->channel[0]), td->hcsplt);
912 hcchar &= ~(HCCHAR_EPDIR_IN | HCCHAR_EPTYPE_MASK);
913 hcchar |= UE_CONTROL << HCCHAR_EPTYPE_SHIFT;
915 /* must enable channel before writing data to FIFO */
916 DWC_OTG_WRITE_4(sc, DOTG_HCCHAR(td->channel[0]), hcchar);
918 /* transfer data into FIFO */
919 bus_space_write_region_4(sc->sc_io_tag, sc->sc_io_hdl,
920 DOTG_DFIFO(td->channel[0]), (uint32_t *)&req, sizeof(req) / 4);
922 /* wait until next slot before trying complete split */
923 td->tt_complete_slot = sc->sc_last_frame_num + 1;
925 /* store number of bytes transmitted */
926 td->tx_bytes = sizeof(req);
930 /* free existing channel, if any */
931 dwc_otg_host_channel_free(sc, td);
933 delta = td->tt_complete_slot - sc->sc_last_frame_num - 1;
934 if (td->tt_scheduled == 0 || delta < DWC_OTG_TT_SLOT_MAX) {
935 td->state = DWC_CHAN_ST_WAIT_C_PKT;
938 delta = sc->sc_last_frame_num - td->tt_start_slot;
939 if (delta > DWC_OTG_TT_SLOT_MAX) {
940 /* we missed the service interval */
941 if (td->ep_type != UE_ISOCHRONOUS)
945 /* allocate a new channel */
946 if (dwc_otg_host_channel_alloc(sc, td, 0)) {
947 td->state = DWC_CHAN_ST_WAIT_C_PKT;
951 /* wait until next slot before trying complete split */
952 td->tt_complete_slot = sc->sc_last_frame_num + 1;
954 td->hcsplt |= HCSPLT_COMPSPLT;
955 td->state = DWC_CHAN_ST_WAIT_C_ANE;
957 DWC_OTG_WRITE_4(sc, DOTG_HCTSIZ(td->channel[0]),
958 (HCTSIZ_PID_SETUP << HCTSIZ_PID_SHIFT));
960 DWC_OTG_WRITE_4(sc, DOTG_HCSPLT(td->channel[0]), td->hcsplt);
963 hcchar &= ~(HCCHAR_EPDIR_IN | HCCHAR_EPTYPE_MASK);
964 hcchar |= UE_CONTROL << HCCHAR_EPTYPE_SHIFT;
966 /* must enable channel before writing data to FIFO */
967 DWC_OTG_WRITE_4(sc, DOTG_HCCHAR(td->channel[0]), hcchar);
970 return (1); /* busy */
973 dwc_otg_host_channel_free(sc, td);
974 return (0); /* complete */
978 dwc_otg_setup_rx(struct dwc_otg_softc *sc, struct dwc_otg_td *td)
980 struct usb_device_request req __aligned(4);
984 /* check endpoint status */
986 if (sc->sc_last_rx_status == 0)
989 if (GRXSTSRD_CHNUM_GET(sc->sc_last_rx_status) != 0)
992 if ((sc->sc_last_rx_status & GRXSTSRD_PKTSTS_MASK) !=
994 if ((sc->sc_last_rx_status & GRXSTSRD_PKTSTS_MASK) !=
995 GRXSTSRD_STP_COMPLETE || td->remainder != 0) {
997 dwc_otg_common_rx_ack(sc);
1001 dwc_otg_common_rx_ack(sc);
1002 return (0); /* complete */
1005 if ((sc->sc_last_rx_status & GRXSTSRD_DPID_MASK) !=
1006 GRXSTSRD_DPID_DATA0) {
1008 dwc_otg_common_rx_ack(sc);
1012 DPRINTFN(5, "GRXSTSR=0x%08x\n", sc->sc_last_rx_status);
1014 /* clear did stall */
1017 /* get the packet byte count */
1018 count = GRXSTSRD_BCNT_GET(sc->sc_last_rx_status);
1020 if (count != sizeof(req)) {
1021 DPRINTFN(0, "Unsupported SETUP packet "
1022 "length, %d bytes\n", count);
1024 dwc_otg_common_rx_ack(sc);
1028 /* copy in control request */
1029 memcpy(&req, sc->sc_rx_bounce_buffer, sizeof(req));
1031 /* copy data into real buffer */
1032 usbd_copy_in(td->pc, 0, &req, sizeof(req));
1034 td->offset = sizeof(req);
1037 /* sneak peek the set address */
1038 if ((req.bmRequestType == UT_WRITE_DEVICE) &&
1039 (req.bRequest == UR_SET_ADDRESS)) {
1040 /* must write address before ZLP */
1041 dwc_otg_set_address(sc, req.wValue[0] & 0x7F);
1044 /* don't send any data by default */
1045 DWC_OTG_WRITE_4(sc, DOTG_DIEPTSIZ(0), DIEPCTL_EPDIS);
1046 DWC_OTG_WRITE_4(sc, DOTG_DOEPTSIZ(0), DOEPCTL_EPDIS);
1048 /* reset IN endpoint buffer */
1049 dwc_otg_tx_fifo_reset(sc,
1053 /* acknowledge RX status */
1054 dwc_otg_common_rx_ack(sc);
1058 /* abort any ongoing transfer, before enabling again */
1059 if (!td->did_stall) {
1062 DPRINTFN(5, "stalling IN and OUT direction\n");
1064 temp = sc->sc_out_ctl[0];
1066 /* set stall after enabling endpoint */
1067 DWC_OTG_WRITE_4(sc, DOTG_DOEPCTL(0),
1068 temp | DOEPCTL_STALL);
1070 temp = sc->sc_in_ctl[0];
1072 /* set stall assuming endpoint is enabled */
1073 DWC_OTG_WRITE_4(sc, DOTG_DIEPCTL(0),
1074 temp | DIEPCTL_STALL);
1076 return (1); /* not complete */
1080 dwc_otg_host_rate_check_interrupt(struct dwc_otg_softc *sc, struct dwc_otg_td *td)
1084 delta = sc->sc_tmr_val - td->tmr_val;
1086 return (1); /* busy */
1088 td->tmr_val = sc->sc_tmr_val + td->tmr_res;
1090 /* set toggle, if any */
1091 if (td->set_toggle) {
1099 dwc_otg_host_rate_check(struct dwc_otg_softc *sc, struct dwc_otg_td *td)
1101 uint8_t frame_num = (uint8_t)sc->sc_last_frame_num;
1103 if (td->ep_type == UE_ISOCHRONOUS) {
1104 /* non TT isochronous traffic */
1105 if (frame_num & (td->tmr_res - 1))
1107 if ((frame_num ^ td->tmr_val) & td->tmr_res)
1109 td->tmr_val = td->tmr_res + sc->sc_last_frame_num;
1112 } else if (td->ep_type == UE_INTERRUPT) {
1113 if (!td->tt_scheduled)
1115 td->tt_scheduled = 0;
1117 } else if (td->did_nak != 0) {
1118 /* check if we should pause sending queries for 125us */
1119 if (td->tmr_res == frame_num) {
1121 dwc_otg_enable_sof_irq(sc);
1124 } else if (td->set_toggle) {
1128 /* query for data one more time */
1129 td->tmr_res = frame_num;
1137 dwc_otg_host_data_rx_sub(struct dwc_otg_softc *sc, struct dwc_otg_td *td,
1142 /* check endpoint status */
1143 if (sc->sc_last_rx_status == 0)
1146 if (channel >= DWC_OTG_MAX_CHANNELS)
1149 if (GRXSTSRD_CHNUM_GET(sc->sc_last_rx_status) != channel)
1152 switch (sc->sc_last_rx_status & GRXSTSRD_PKTSTS_MASK) {
1153 case GRXSTSRH_IN_DATA:
1155 DPRINTF("DATA ST=%d STATUS=0x%08x\n",
1156 (int)td->state, (int)sc->sc_last_rx_status);
1158 if (sc->sc_chan_state[channel].hcint & HCINT_SOFTWARE_ONLY) {
1160 * When using SPLIT transactions on interrupt
1161 * endpoints, sometimes data occurs twice.
1163 DPRINTF("Data already received\n");
1167 /* get the packet byte count */
1168 count = GRXSTSRD_BCNT_GET(sc->sc_last_rx_status);
1170 /* check for ISOCHRONOUS endpoint */
1171 if (td->ep_type == UE_ISOCHRONOUS) {
1172 if ((sc->sc_last_rx_status & GRXSTSRD_DPID_MASK) !=
1173 GRXSTSRD_DPID_DATA0) {
1174 /* more data to be received */
1175 td->tt_xactpos = HCSPLT_XACTPOS_MIDDLE;
1177 /* all data received */
1178 td->tt_xactpos = HCSPLT_XACTPOS_BEGIN;
1179 /* verify the packet byte count */
1180 if (count != td->remainder) {
1181 /* we have a short packet */
1187 /* verify the packet byte count */
1188 if (count != td->max_packet_size) {
1189 if (count < td->max_packet_size) {
1190 /* we have a short packet */
1194 /* invalid USB packet */
1198 dwc_otg_common_rx_ack(sc);
1203 td->tt_scheduled = 0;
1206 /* verify the packet byte count */
1207 if (count > td->remainder) {
1208 /* invalid USB packet */
1212 dwc_otg_common_rx_ack(sc);
1216 usbd_copy_in(td->pc, td->offset,
1217 sc->sc_rx_bounce_buffer, count);
1219 td->remainder -= count;
1220 td->offset += count;
1221 sc->sc_chan_state[channel].hcint |= HCINT_SOFTWARE_ONLY;
1227 dwc_otg_common_rx_ack(sc);
1235 dwc_otg_host_data_rx(struct dwc_otg_softc *sc, struct dwc_otg_td *td)
1243 for (x = 0; x != td->max_packet_count; x++) {
1244 channel = td->channel[x];
1245 if (channel >= DWC_OTG_MAX_CHANNELS)
1247 hcint |= sc->sc_chan_state[channel].hcint;
1249 DPRINTF("CH=%d ST=%d HCINT=0x%08x HCCHAR=0x%08x HCTSIZ=0x%08x\n",
1250 channel, td->state, hcint,
1251 DWC_OTG_READ_4(sc, DOTG_HCCHAR(channel)),
1252 DWC_OTG_READ_4(sc, DOTG_HCTSIZ(channel)));
1254 /* check interrupt bits */
1255 if (hcint & (HCINT_RETRY |
1256 HCINT_ACK | HCINT_NYET)) {
1257 /* give success bits priority over failure bits */
1258 } else if (hcint & HCINT_STALL) {
1259 DPRINTF("CH=%d STALL\n", channel);
1260 td->error_stall = 1;
1263 } else if (hcint & HCINT_ERRORS) {
1264 DPRINTF("CH=%d ERROR\n", channel);
1266 if (td->hcsplt != 0 || td->errcnt >= 3) {
1267 if (td->ep_type != UE_ISOCHRONOUS) {
1274 /* check channels for data, if any */
1275 if (dwc_otg_host_data_rx_sub(sc, td, channel))
1278 /* refresh interrupt status */
1279 hcint |= sc->sc_chan_state[channel].hcint;
1281 if (hcint & (HCINT_ERRORS | HCINT_RETRY |
1282 HCINT_ACK | HCINT_NYET)) {
1283 if (!(hcint & HCINT_ERRORS))
1288 switch (td->state) {
1289 case DWC_CHAN_ST_START:
1290 if (td->hcsplt != 0)
1295 case DWC_CHAN_ST_WAIT_ANE:
1296 if (hcint & (HCINT_RETRY | HCINT_ERRORS)) {
1297 if (td->ep_type == UE_INTERRUPT) {
1299 * The USB specification does not
1300 * mandate a particular data toggle
1301 * value for USB INTERRUPT
1302 * transfers. Switch the data toggle
1303 * value to receive the packet
1306 if (hcint & HCINT_DATATGLERR) {
1307 DPRINTF("Retrying packet due to "
1308 "data toggle error\n");
1312 } else if (td->ep_type == UE_ISOCHRONOUS) {
1316 td->tt_scheduled = 0;
1317 if (td->hcsplt != 0)
1321 } else if (hcint & HCINT_NYET) {
1322 if (td->hcsplt != 0) {
1326 /* not a valid token for IN endpoints */
1330 } else if (hcint & HCINT_ACK) {
1331 /* wait for data - ACK arrived first */
1332 if (!(hcint & HCINT_SOFTWARE_ONLY))
1335 if (td->ep_type == UE_ISOCHRONOUS) {
1336 /* check if we are complete */
1337 if (td->tt_xactpos == HCSPLT_XACTPOS_BEGIN) {
1340 /* get more packets */
1344 /* check if we are complete */
1345 if ((td->remainder == 0) || (td->got_short != 0)) {
1350 * Else need to receive a zero length
1354 td->tt_scheduled = 0;
1356 if (td->hcsplt != 0)
1364 case DWC_CHAN_ST_WAIT_S_ANE:
1366 * NOTE: The DWC OTG hardware provides a fake ACK in
1367 * case of interrupt and isochronous transfers:
1369 if (hcint & (HCINT_RETRY | HCINT_ERRORS)) {
1371 td->tt_scheduled = 0;
1373 } else if (hcint & HCINT_NYET) {
1374 td->tt_scheduled = 0;
1376 } else if (hcint & HCINT_ACK) {
1382 case DWC_CHAN_ST_WAIT_C_PKT:
1391 /* free existing channel, if any */
1392 dwc_otg_host_channel_free(sc, td);
1394 if (td->hcsplt != 0) {
1395 delta = td->tt_complete_slot - sc->sc_last_frame_num - 1;
1396 if (td->tt_scheduled == 0 || delta < DWC_OTG_TT_SLOT_MAX) {
1397 td->state = DWC_CHAN_ST_WAIT_C_PKT;
1400 delta = sc->sc_last_frame_num - td->tt_start_slot;
1401 if (delta > DWC_OTG_TT_SLOT_MAX) {
1402 if (td->ep_type != UE_ISOCHRONOUS) {
1403 /* we missed the service interval */
1408 /* complete split */
1409 td->hcsplt |= HCSPLT_COMPSPLT;
1410 } else if (dwc_otg_host_rate_check(sc, td)) {
1411 td->state = DWC_CHAN_ST_WAIT_C_PKT;
1415 /* allocate a new channel */
1416 if (dwc_otg_host_channel_alloc(sc, td, 0)) {
1417 td->state = DWC_CHAN_ST_WAIT_C_PKT;
1421 /* set toggle, if any */
1422 if (td->set_toggle) {
1427 td->state = DWC_CHAN_ST_WAIT_ANE;
1429 for (x = 0; x != td->max_packet_count; x++) {
1430 channel = td->channel[x];
1432 /* receive one packet */
1433 DWC_OTG_WRITE_4(sc, DOTG_HCTSIZ(channel),
1434 (td->max_packet_size << HCTSIZ_XFERSIZE_SHIFT) |
1435 (1 << HCTSIZ_PKTCNT_SHIFT) |
1436 (td->toggle ? (HCTSIZ_PID_DATA1 << HCTSIZ_PID_SHIFT) :
1437 (HCTSIZ_PID_DATA0 << HCTSIZ_PID_SHIFT)));
1439 DWC_OTG_WRITE_4(sc, DOTG_HCSPLT(channel), td->hcsplt);
1441 hcchar = td->hcchar;
1442 hcchar |= HCCHAR_EPDIR_IN;
1444 /* receive complete split ASAP */
1445 if ((sc->sc_last_frame_num & 1) != 0 &&
1446 td->ep_type == UE_ISOCHRONOUS)
1447 hcchar |= HCCHAR_ODDFRM;
1449 hcchar &= ~HCCHAR_ODDFRM;
1451 /* must enable channel before data can be received */
1452 DWC_OTG_WRITE_4(sc, DOTG_HCCHAR(channel), hcchar);
1454 /* wait until next slot before trying complete split */
1455 td->tt_complete_slot = sc->sc_last_frame_num + 1;
1460 /* free existing channel(s), if any */
1461 dwc_otg_host_channel_free(sc, td);
1463 delta = td->tt_start_slot - sc->sc_last_frame_num - 1;
1464 if (td->tt_scheduled == 0 || delta < DWC_OTG_TT_SLOT_MAX) {
1465 td->state = DWC_CHAN_ST_START;
1468 delta = sc->sc_last_frame_num - td->tt_start_slot;
1471 td->tt_scheduled = 0;
1472 td->state = DWC_CHAN_ST_START;
1476 /* allocate a new channel */
1477 if (dwc_otg_host_channel_alloc(sc, td, 0)) {
1478 td->state = DWC_CHAN_ST_START;
1482 channel = td->channel[0];
1484 td->hcsplt &= ~HCSPLT_COMPSPLT;
1485 td->state = DWC_CHAN_ST_WAIT_S_ANE;
1487 /* receive one packet */
1488 DWC_OTG_WRITE_4(sc, DOTG_HCTSIZ(channel),
1489 (HCTSIZ_PID_DATA0 << HCTSIZ_PID_SHIFT));
1491 DWC_OTG_WRITE_4(sc, DOTG_HCSPLT(channel), td->hcsplt);
1493 /* send after next SOF event */
1494 if ((sc->sc_last_frame_num & 1) == 0 &&
1495 td->ep_type == UE_ISOCHRONOUS)
1496 td->hcchar |= HCCHAR_ODDFRM;
1498 td->hcchar &= ~HCCHAR_ODDFRM;
1500 hcchar = td->hcchar;
1501 hcchar |= HCCHAR_EPDIR_IN;
1503 /* wait until next slot before trying complete split */
1504 td->tt_complete_slot = sc->sc_last_frame_num + 1;
1506 /* must enable channel before data can be received */
1507 DWC_OTG_WRITE_4(sc, DOTG_HCCHAR(channel), hcchar);
1509 return (1); /* busy */
1512 dwc_otg_host_channel_free(sc, td);
1513 return (0); /* complete */
1517 dwc_otg_data_rx(struct dwc_otg_softc *sc, struct dwc_otg_td *td)
1525 /* check endpoint status */
1526 if (sc->sc_last_rx_status == 0)
1529 if (GRXSTSRD_CHNUM_GET(sc->sc_last_rx_status) != td->ep_no)
1532 /* check for SETUP packet */
1533 if ((sc->sc_last_rx_status & GRXSTSRD_PKTSTS_MASK) ==
1534 GRXSTSRD_STP_DATA ||
1535 (sc->sc_last_rx_status & GRXSTSRD_PKTSTS_MASK) ==
1536 GRXSTSRD_STP_COMPLETE) {
1537 if (td->remainder == 0) {
1539 * We are actually complete and have
1540 * received the next SETUP
1542 DPRINTFN(5, "faking complete\n");
1543 return (0); /* complete */
1546 * USB Host Aborted the transfer.
1549 return (0); /* complete */
1552 if ((sc->sc_last_rx_status & GRXSTSRD_PKTSTS_MASK) !=
1553 GRXSTSRD_OUT_DATA) {
1555 dwc_otg_common_rx_ack(sc);
1559 /* get the packet byte count */
1560 count = GRXSTSRD_BCNT_GET(sc->sc_last_rx_status);
1562 /* verify the packet byte count */
1563 if (count != td->max_packet_size) {
1564 if (count < td->max_packet_size) {
1565 /* we have a short packet */
1569 /* invalid USB packet */
1573 dwc_otg_common_rx_ack(sc);
1574 return (0); /* we are complete */
1577 /* verify the packet byte count */
1578 if (count > td->remainder) {
1579 /* invalid USB packet */
1583 dwc_otg_common_rx_ack(sc);
1584 return (0); /* we are complete */
1587 usbd_copy_in(td->pc, td->offset, sc->sc_rx_bounce_buffer, count);
1588 td->remainder -= count;
1589 td->offset += count;
1592 dwc_otg_common_rx_ack(sc);
1594 temp = sc->sc_out_ctl[td->ep_no];
1596 /* check for isochronous mode */
1597 if ((temp & DIEPCTL_EPTYPE_MASK) ==
1598 (DIEPCTL_EPTYPE_ISOC << DIEPCTL_EPTYPE_SHIFT)) {
1599 /* toggle odd or even frame bit */
1600 if (temp & DIEPCTL_SETD1PID) {
1601 temp &= ~DIEPCTL_SETD1PID;
1602 temp |= DIEPCTL_SETD0PID;
1604 temp &= ~DIEPCTL_SETD0PID;
1605 temp |= DIEPCTL_SETD1PID;
1607 sc->sc_out_ctl[td->ep_no] = temp;
1610 /* check if we are complete */
1611 if ((td->remainder == 0) || got_short) {
1612 if (td->short_pkt) {
1613 /* we are complete */
1616 /* else need to receive a zero length packet */
1621 /* enable SETUP and transfer complete interrupt */
1622 if (td->ep_no == 0) {
1623 DWC_OTG_WRITE_4(sc, DOTG_DOEPTSIZ(0),
1624 DXEPTSIZ_SET_MULTI(3) |
1625 DXEPTSIZ_SET_NPKT(1) |
1626 DXEPTSIZ_SET_NBYTES(td->max_packet_size));
1628 /* allow reception of multiple packets */
1629 DWC_OTG_WRITE_4(sc, DOTG_DOEPTSIZ(td->ep_no),
1630 DXEPTSIZ_SET_MULTI(1) |
1631 DXEPTSIZ_SET_NPKT(4) |
1632 DXEPTSIZ_SET_NBYTES(4 *
1633 ((td->max_packet_size + 3) & ~3)));
1635 temp = sc->sc_out_ctl[td->ep_no];
1636 DWC_OTG_WRITE_4(sc, DOTG_DOEPCTL(td->ep_no), temp |
1637 DOEPCTL_EPENA | DOEPCTL_CNAK);
1639 return (1); /* not complete */
1643 dwc_otg_host_data_tx(struct dwc_otg_softc *sc, struct dwc_otg_td *td)
1652 dwc_otg_host_dump_rx(sc, td);
1654 /* check that last channel is complete */
1655 channel = td->channel[td->npkt];
1657 if (channel < DWC_OTG_MAX_CHANNELS) {
1658 hcint = sc->sc_chan_state[channel].hcint;
1660 DPRINTF("CH=%d ST=%d HCINT=0x%08x HCCHAR=0x%08x HCTSIZ=0x%08x\n",
1661 channel, td->state, hcint,
1662 DWC_OTG_READ_4(sc, DOTG_HCCHAR(channel)),
1663 DWC_OTG_READ_4(sc, DOTG_HCTSIZ(channel)));
1665 if (hcint & (HCINT_RETRY |
1666 HCINT_ACK | HCINT_NYET)) {
1667 /* give success bits priority over failure bits */
1668 } else if (hcint & HCINT_STALL) {
1669 DPRINTF("CH=%d STALL\n", channel);
1670 td->error_stall = 1;
1673 } else if (hcint & HCINT_ERRORS) {
1674 DPRINTF("CH=%d ERROR\n", channel);
1676 if (td->hcsplt != 0 || td->errcnt >= 3) {
1682 if (hcint & (HCINT_ERRORS | HCINT_RETRY |
1683 HCINT_ACK | HCINT_NYET)) {
1685 if (!(hcint & HCINT_ERRORS))
1692 switch (td->state) {
1693 case DWC_CHAN_ST_START:
1696 case DWC_CHAN_ST_WAIT_ANE:
1697 if (hcint & (HCINT_RETRY | HCINT_ERRORS)) {
1699 td->tt_scheduled = 0;
1701 } else if (hcint & (HCINT_ACK | HCINT_NYET)) {
1702 td->offset += td->tx_bytes;
1703 td->remainder -= td->tx_bytes;
1705 /* check if next response will be a NAK */
1706 if (hcint & HCINT_NYET)
1710 td->tt_scheduled = 0;
1712 /* check remainder */
1713 if (td->remainder == 0) {
1718 * Else we need to transmit a short
1726 case DWC_CHAN_ST_WAIT_S_ANE:
1727 if (hcint & (HCINT_RETRY | HCINT_ERRORS)) {
1729 td->tt_scheduled = 0;
1731 } else if (hcint & (HCINT_ACK | HCINT_NYET)) {
1737 case DWC_CHAN_ST_WAIT_C_ANE:
1738 if (hcint & HCINT_NYET) {
1740 } else if (hcint & (HCINT_RETRY | HCINT_ERRORS)) {
1742 td->tt_scheduled = 0;
1744 } else if (hcint & HCINT_ACK) {
1745 td->offset += td->tx_bytes;
1746 td->remainder -= td->tx_bytes;
1749 td->tt_scheduled = 0;
1751 /* check remainder */
1752 if (td->remainder == 0) {
1756 /* else we need to transmit a short packet */
1762 case DWC_CHAN_ST_WAIT_C_PKT:
1765 case DWC_CHAN_ST_TX_WAIT_ISOC:
1766 /* Check if ISOCHRONOUS OUT traffic is complete */
1767 if ((hcint & HCINT_HCH_DONE_MASK) == 0)
1770 td->offset += td->tx_bytes;
1771 td->remainder -= td->tx_bytes;
1779 /* free existing channel(s), if any */
1780 dwc_otg_host_channel_free(sc, td);
1782 if (td->hcsplt != 0) {
1783 delta = td->tt_start_slot - sc->sc_last_frame_num - 1;
1784 if (td->tt_scheduled == 0 || delta < DWC_OTG_TT_SLOT_MAX) {
1785 td->state = DWC_CHAN_ST_START;
1788 delta = sc->sc_last_frame_num - td->tt_start_slot;
1791 td->tt_scheduled = 0;
1792 td->state = DWC_CHAN_ST_START;
1795 } else if (dwc_otg_host_rate_check(sc, td)) {
1796 td->state = DWC_CHAN_ST_START;
1800 /* allocate a new channel */
1801 if (dwc_otg_host_channel_alloc(sc, td, 1)) {
1802 td->state = DWC_CHAN_ST_START;
1806 /* set toggle, if any */
1807 if (td->set_toggle) {
1812 if (td->ep_type == UE_ISOCHRONOUS) {
1813 /* ISOCHRONOUS OUT transfers don't have any ACKs */
1814 td->state = DWC_CHAN_ST_TX_WAIT_ISOC;
1815 td->hcsplt &= ~HCSPLT_COMPSPLT;
1816 if (td->hcsplt != 0) {
1817 /* get maximum transfer length */
1818 count = td->remainder;
1819 if (count > HCSPLT_XACTLEN_BURST) {
1820 DPRINTF("TT overflow\n");
1824 /* Update transaction position */
1825 td->hcsplt &= ~HCSPLT_XACTPOS_MASK;
1826 td->hcsplt |= (HCSPLT_XACTPOS_ALL << HCSPLT_XACTPOS_SHIFT);
1828 } else if (td->hcsplt != 0) {
1829 td->hcsplt &= ~HCSPLT_COMPSPLT;
1830 /* Wait for ACK/NAK/ERR from TT */
1831 td->state = DWC_CHAN_ST_WAIT_S_ANE;
1833 /* Wait for ACK/NAK/STALL from device */
1834 td->state = DWC_CHAN_ST_WAIT_ANE;
1839 for (x = 0; x != td->max_packet_count; x++) {
1842 channel = td->channel[x];
1844 /* send one packet at a time */
1845 count = td->max_packet_size;
1846 rem_bytes = td->remainder - td->tx_bytes;
1847 if (rem_bytes < count) {
1848 /* we have a short packet */
1852 if (count == rem_bytes) {
1856 DWC_OTG_WRITE_4(sc, DOTG_HCTSIZ(channel),
1857 (count << HCTSIZ_XFERSIZE_SHIFT) |
1858 (1 << HCTSIZ_PKTCNT_SHIFT) |
1859 (td->toggle ? (HCTSIZ_PID_DATA1 << HCTSIZ_PID_SHIFT) :
1860 (HCTSIZ_PID_DATA0 << HCTSIZ_PID_SHIFT)));
1863 DWC_OTG_WRITE_4(sc, DOTG_HCTSIZ(channel),
1864 (count << HCTSIZ_XFERSIZE_SHIFT) |
1865 (1 << HCTSIZ_PKTCNT_SHIFT) |
1866 (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));
1875 } else if (td->ep_type == UE_ISOCHRONOUS &&
1876 td->max_packet_count > 1) {
1877 /* ISOCHRONOUS multi packet */
1878 DWC_OTG_WRITE_4(sc, DOTG_HCTSIZ(channel),
1879 (count << HCTSIZ_XFERSIZE_SHIFT) |
1880 (1 << HCTSIZ_PKTCNT_SHIFT) |
1881 (HCTSIZ_PID_MDATA << HCTSIZ_PID_SHIFT));
1883 /* TODO: HCTSIZ_DOPNG */
1884 /* standard BULK/INTERRUPT/CONTROL packet */
1885 DWC_OTG_WRITE_4(sc, DOTG_HCTSIZ(channel),
1886 (count << HCTSIZ_XFERSIZE_SHIFT) |
1887 (1 << HCTSIZ_PKTCNT_SHIFT) |
1888 (td->toggle ? (HCTSIZ_PID_DATA1 << HCTSIZ_PID_SHIFT) :
1889 (HCTSIZ_PID_DATA0 << HCTSIZ_PID_SHIFT)));
1892 DWC_OTG_WRITE_4(sc, DOTG_HCSPLT(channel), td->hcsplt);
1894 hcchar = td->hcchar;
1895 hcchar &= ~HCCHAR_EPDIR_IN;
1897 /* send after next SOF event */
1898 if ((sc->sc_last_frame_num & 1) == 0 &&
1899 td->ep_type == UE_ISOCHRONOUS)
1900 hcchar |= HCCHAR_ODDFRM;
1902 hcchar &= ~HCCHAR_ODDFRM;
1904 /* must enable before writing data to FIFO */
1905 DWC_OTG_WRITE_4(sc, DOTG_HCCHAR(channel), hcchar);
1908 /* clear topmost word before copy */
1909 sc->sc_tx_bounce_buffer[(count - 1) / 4] = 0;
1912 usbd_copy_out(td->pc, td->offset + td->tx_bytes,
1913 sc->sc_tx_bounce_buffer, count);
1915 /* transfer data into FIFO */
1916 bus_space_write_region_4(sc->sc_io_tag, sc->sc_io_hdl,
1917 DOTG_DFIFO(channel),
1918 sc->sc_tx_bounce_buffer, (count + 3) / 4);
1921 /* store number of bytes transmitted */
1922 td->tx_bytes += count;
1924 /* store last packet index */
1927 /* check for last packet */
1928 if (count == rem_bytes)
1934 /* free existing channel, if any */
1935 dwc_otg_host_channel_free(sc, td);
1937 delta = td->tt_complete_slot - sc->sc_last_frame_num - 1;
1938 if (td->tt_scheduled == 0 || delta < DWC_OTG_TT_SLOT_MAX) {
1939 td->state = DWC_CHAN_ST_WAIT_C_PKT;
1942 delta = sc->sc_last_frame_num - td->tt_start_slot;
1943 if (delta > DWC_OTG_TT_SLOT_MAX) {
1944 /* we missed the service interval */
1945 if (td->ep_type != UE_ISOCHRONOUS)
1950 /* allocate a new channel */
1951 if (dwc_otg_host_channel_alloc(sc, td, 0)) {
1952 td->state = DWC_CHAN_ST_WAIT_C_PKT;
1956 channel = td->channel[0];
1958 td->hcsplt |= HCSPLT_COMPSPLT;
1959 td->state = DWC_CHAN_ST_WAIT_C_ANE;
1961 DWC_OTG_WRITE_4(sc, DOTG_HCTSIZ(channel),
1962 (HCTSIZ_PID_DATA0 << HCTSIZ_PID_SHIFT));
1964 DWC_OTG_WRITE_4(sc, DOTG_HCSPLT(channel), td->hcsplt);
1966 hcchar = td->hcchar;
1967 hcchar &= ~HCCHAR_EPDIR_IN;
1969 /* receive complete split ASAP */
1970 if ((sc->sc_last_frame_num & 1) != 0 &&
1971 td->ep_type == UE_ISOCHRONOUS)
1972 hcchar |= HCCHAR_ODDFRM;
1974 hcchar &= ~HCCHAR_ODDFRM;
1976 /* must enable channel before data can be received */
1977 DWC_OTG_WRITE_4(sc, DOTG_HCCHAR(channel), hcchar);
1979 /* wait until next slot before trying complete split */
1980 td->tt_complete_slot = sc->sc_last_frame_num + 1;
1982 return (1); /* busy */
1985 dwc_otg_host_channel_free(sc, td);
1986 return (0); /* complete */
1990 dwc_otg_data_tx(struct dwc_otg_softc *sc, struct dwc_otg_td *td)
1992 uint32_t max_buffer;
1999 to = 3; /* don't loop forever! */
2001 max_buffer = sc->sc_hw_ep_profile[td->ep_no].max_buffer;
2004 /* check for for endpoint 0 data */
2006 temp = sc->sc_last_rx_status;
2008 if ((td->ep_no == 0) && (temp != 0) &&
2009 (GRXSTSRD_CHNUM_GET(temp) == 0)) {
2011 if ((temp & GRXSTSRD_PKTSTS_MASK) !=
2012 GRXSTSRD_STP_DATA &&
2013 (temp & GRXSTSRD_PKTSTS_MASK) !=
2014 GRXSTSRD_STP_COMPLETE) {
2016 /* dump data - wrong direction */
2017 dwc_otg_common_rx_ack(sc);
2020 * The current transfer was cancelled
2024 return (0); /* complete */
2028 /* fill in more TX data, if possible */
2029 if (td->tx_bytes != 0) {
2033 /* check if packets have been transferred */
2034 temp = DWC_OTG_READ_4(sc, DOTG_DIEPTSIZ(td->ep_no));
2036 /* get current packet number */
2037 cpkt = DXEPTSIZ_GET_NPKT(temp);
2039 if (cpkt >= td->npkt) {
2042 if (max_buffer != 0) {
2043 fifo_left = (td->npkt - cpkt) *
2044 td->max_packet_size;
2046 if (fifo_left > max_buffer)
2047 fifo_left = max_buffer;
2049 fifo_left = td->max_packet_size;
2053 count = td->tx_bytes;
2054 if (count > fifo_left)
2059 /* clear topmost word before copy */
2060 sc->sc_tx_bounce_buffer[(count - 1) / 4] = 0;
2063 usbd_copy_out(td->pc, td->offset,
2064 sc->sc_tx_bounce_buffer, count);
2066 /* transfer data into FIFO */
2067 bus_space_write_region_4(sc->sc_io_tag, sc->sc_io_hdl,
2068 DOTG_DFIFO(td->ep_no),
2069 sc->sc_tx_bounce_buffer, (count + 3) / 4);
2071 td->tx_bytes -= count;
2072 td->remainder -= count;
2073 td->offset += count;
2076 if (td->tx_bytes != 0)
2079 /* check remainder */
2080 if (td->remainder == 0) {
2082 return (0); /* complete */
2084 /* else we need to transmit a short packet */
2091 /* check if not all packets have been transferred */
2092 temp = DWC_OTG_READ_4(sc, DOTG_DIEPTSIZ(td->ep_no));
2094 if (DXEPTSIZ_GET_NPKT(temp) != 0) {
2096 DPRINTFN(5, "busy ep=%d npkt=%d DIEPTSIZ=0x%08x "
2097 "DIEPCTL=0x%08x\n", td->ep_no,
2098 DXEPTSIZ_GET_NPKT(temp),
2099 temp, DWC_OTG_READ_4(sc, DOTG_DIEPCTL(td->ep_no)));
2104 DPRINTFN(5, "rem=%u ep=%d\n", td->remainder, td->ep_no);
2106 /* try to optimise by sending more data */
2107 if ((max_buffer != 0) && ((td->max_packet_size & 3) == 0)) {
2109 /* send multiple packets at the same time */
2110 mpkt = max_buffer / td->max_packet_size;
2115 count = td->remainder;
2116 if (count > 0x7FFFFF)
2117 count = 0x7FFFFF - (0x7FFFFF % td->max_packet_size);
2119 td->npkt = count / td->max_packet_size;
2122 * NOTE: We could use 0x3FE instead of "mpkt" in the
2123 * check below to get more throughput, but then we
2124 * have a dependency towards non-generic chip features
2125 * to disable the TX-FIFO-EMPTY interrupts on a per
2126 * endpoint basis. Increase the maximum buffer size of
2127 * the IN endpoint to increase the performance.
2129 if (td->npkt > mpkt) {
2131 count = td->max_packet_size * mpkt;
2132 } else if ((count == 0) || (count % td->max_packet_size)) {
2133 /* we are transmitting a short packet */
2138 /* send one packet at a time */
2140 count = td->max_packet_size;
2141 if (td->remainder < count) {
2142 /* we have a short packet */
2144 count = td->remainder;
2148 DWC_OTG_WRITE_4(sc, DOTG_DIEPTSIZ(td->ep_no),
2149 DXEPTSIZ_SET_MULTI(1) |
2150 DXEPTSIZ_SET_NPKT(td->npkt) |
2151 DXEPTSIZ_SET_NBYTES(count));
2153 /* make room for buffering */
2156 temp = sc->sc_in_ctl[td->ep_no];
2158 /* check for isochronous mode */
2159 if ((temp & DIEPCTL_EPTYPE_MASK) ==
2160 (DIEPCTL_EPTYPE_ISOC << DIEPCTL_EPTYPE_SHIFT)) {
2161 /* toggle odd or even frame bit */
2162 if (temp & DIEPCTL_SETD1PID) {
2163 temp &= ~DIEPCTL_SETD1PID;
2164 temp |= DIEPCTL_SETD0PID;
2166 temp &= ~DIEPCTL_SETD0PID;
2167 temp |= DIEPCTL_SETD1PID;
2169 sc->sc_in_ctl[td->ep_no] = temp;
2172 /* must enable before writing data to FIFO */
2173 DWC_OTG_WRITE_4(sc, DOTG_DIEPCTL(td->ep_no), temp |
2174 DIEPCTL_EPENA | DIEPCTL_CNAK);
2176 td->tx_bytes = count;
2178 /* check remainder */
2179 if (td->tx_bytes == 0 &&
2180 td->remainder == 0) {
2182 return (0); /* complete */
2184 /* else we need to transmit a short packet */
2189 return (1); /* not complete */
2193 dwc_otg_data_tx_sync(struct dwc_otg_softc *sc, struct dwc_otg_td *td)
2198 * If all packets are transferred we are complete:
2200 temp = DWC_OTG_READ_4(sc, DOTG_DIEPTSIZ(td->ep_no));
2202 /* check that all packets have been transferred */
2203 if (DXEPTSIZ_GET_NPKT(temp) != 0) {
2204 DPRINTFN(5, "busy ep=%d\n", td->ep_no);
2211 /* we only want to know if there is a SETUP packet or free IN packet */
2213 temp = sc->sc_last_rx_status;
2215 if ((td->ep_no == 0) && (temp != 0) &&
2216 (GRXSTSRD_CHNUM_GET(temp) == 0)) {
2218 if ((temp & GRXSTSRD_PKTSTS_MASK) ==
2219 GRXSTSRD_STP_DATA ||
2220 (temp & GRXSTSRD_PKTSTS_MASK) ==
2221 GRXSTSRD_STP_COMPLETE) {
2222 DPRINTFN(5, "faking complete\n");
2224 * Race condition: We are complete!
2228 /* dump data - wrong direction */
2229 dwc_otg_common_rx_ack(sc);
2232 return (1); /* not complete */
2236 dwc_otg_xfer_do_fifo(struct dwc_otg_softc *sc, struct usb_xfer *xfer)
2238 struct dwc_otg_td *td;
2245 td = xfer->td_transfer_cache;
2250 if ((td->func) (sc, td)) {
2251 /* operation in progress */
2254 if (((void *)td) == xfer->td_transfer_last) {
2257 if (td->error_any) {
2259 } else if (td->remainder > 0) {
2261 * We had a short transfer. If there is no alternate
2262 * next, stop processing !
2269 * Fetch the next transfer descriptor and transfer
2270 * some flags to the next transfer descriptor
2272 tmr_res = td->tmr_res;
2273 tmr_val = td->tmr_val;
2274 toggle = td->toggle;
2276 xfer->td_transfer_cache = td;
2277 td->toggle = toggle; /* transfer toggle */
2278 td->tmr_res = tmr_res;
2279 td->tmr_val = tmr_val;
2284 xfer->td_transfer_cache = NULL;
2285 sc->sc_xfer_complete = 1;
2289 dwc_otg_xfer_do_complete_locked(struct dwc_otg_softc *sc, struct usb_xfer *xfer)
2291 struct dwc_otg_td *td;
2295 td = xfer->td_transfer_cache;
2297 /* compute all actual lengths */
2298 dwc_otg_standard_done(xfer);
2305 dwc_otg_timer(void *_sc)
2307 struct dwc_otg_softc *sc = _sc;
2309 USB_BUS_LOCK_ASSERT(&sc->sc_bus, MA_OWNED);
2313 USB_BUS_SPIN_LOCK(&sc->sc_bus);
2315 /* increment timer value */
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_compute_isoc_rx_tt_slot(struct dwc_otg_tt_info *pinfo)
2360 if (pinfo->slot_index < DWC_OTG_TT_SLOT_MAX)
2361 pinfo->slot_index++;
2362 return (pinfo->slot_index);
2366 dwc_otg_update_host_transfer_schedule_locked(struct dwc_otg_softc *sc)
2368 TAILQ_HEAD(, usb_xfer) head;
2369 struct usb_xfer *xfer;
2370 struct usb_xfer *xfer_next;
2371 struct dwc_otg_td *td;
2375 temp = DWC_OTG_READ_4(sc, DOTG_HFNUM) & DWC_OTG_FRAME_MASK;
2377 if (sc->sc_last_frame_num == temp)
2380 sc->sc_last_frame_num = temp;
2384 if ((temp & 7) == 0) {
2386 /* reset the schedule */
2387 memset(sc->sc_tt_info, 0, sizeof(sc->sc_tt_info));
2389 TAILQ_FOREACH_SAFE(xfer, &sc->sc_bus.intr_q.head, wait_entry, xfer_next) {
2390 td = xfer->td_transfer_cache;
2391 if (td == NULL || td->ep_type != UE_ISOCHRONOUS)
2394 /* check for IN direction */
2395 if ((td->hcchar & HCCHAR_EPDIR_IN) != 0)
2400 if (td->hcsplt == 0 || td->tt_scheduled != 0)
2404 slot = dwc_otg_compute_isoc_rx_tt_slot(
2405 sc->sc_tt_info + td->tt_index);
2408 * Not enough time to get complete
2414 td->tt_start_slot = temp + slot;
2415 td->tt_scheduled = 1;
2416 TAILQ_REMOVE(&sc->sc_bus.intr_q.head, xfer, wait_entry);
2417 TAILQ_INSERT_TAIL(&head, xfer, wait_entry);
2420 TAILQ_FOREACH_SAFE(xfer, &sc->sc_bus.intr_q.head, wait_entry, xfer_next) {
2421 td = xfer->td_transfer_cache;
2422 if (td == NULL || td->ep_type != UE_ISOCHRONOUS)
2425 /* check for OUT direction */
2426 if ((td->hcchar & HCCHAR_EPDIR_IN) == 0)
2431 if (td->hcsplt == 0 || td->tt_scheduled != 0)
2435 td->tt_start_slot = temp;
2436 td->tt_scheduled = 1;
2437 TAILQ_REMOVE(&sc->sc_bus.intr_q.head, xfer, wait_entry);
2438 TAILQ_INSERT_TAIL(&head, xfer, wait_entry);
2441 TAILQ_FOREACH_SAFE(xfer, &sc->sc_bus.intr_q.head, wait_entry, xfer_next) {
2442 td = xfer->td_transfer_cache;
2443 if (td == NULL || td->ep_type != UE_INTERRUPT)
2446 if (td->tt_scheduled != 0) {
2451 if (dwc_otg_host_rate_check_interrupt(sc, td))
2454 if (td->hcsplt == 0) {
2456 td->tt_scheduled = 1;
2461 td->tt_start_slot = temp;
2463 td->tt_scheduled = 1;
2464 TAILQ_REMOVE(&sc->sc_bus.intr_q.head, xfer, wait_entry);
2465 TAILQ_INSERT_TAIL(&head, xfer, wait_entry);
2468 TAILQ_FOREACH_SAFE(xfer, &sc->sc_bus.intr_q.head, wait_entry, xfer_next) {
2469 td = xfer->td_transfer_cache;
2471 td->ep_type != UE_CONTROL) {
2477 if (td->hcsplt == 0 || td->tt_scheduled != 0)
2481 td->tt_start_slot = temp;
2482 td->tt_scheduled = 1;
2483 TAILQ_REMOVE(&sc->sc_bus.intr_q.head, xfer, wait_entry);
2484 TAILQ_INSERT_TAIL(&head, xfer, wait_entry);
2487 if ((temp & 7) < 6) {
2488 TAILQ_FOREACH_SAFE(xfer, &sc->sc_bus.intr_q.head, wait_entry, xfer_next) {
2489 td = xfer->td_transfer_cache;
2491 td->ep_type != UE_BULK) {
2497 if (td->hcsplt == 0 || td->tt_scheduled != 0)
2501 td->tt_start_slot = temp;
2502 td->tt_scheduled = 1;
2503 TAILQ_REMOVE(&sc->sc_bus.intr_q.head, xfer, wait_entry);
2504 TAILQ_INSERT_TAIL(&head, xfer, wait_entry);
2508 /* Put TT transfers in execution order at the end */
2509 TAILQ_CONCAT(&sc->sc_bus.intr_q.head, &head, wait_entry);
2511 /* move all TT transfers in front, keeping the current order */
2512 TAILQ_FOREACH_SAFE(xfer, &sc->sc_bus.intr_q.head, wait_entry, xfer_next) {
2513 td = xfer->td_transfer_cache;
2514 if (td == NULL || td->hcsplt == 0)
2516 TAILQ_REMOVE(&sc->sc_bus.intr_q.head, xfer, wait_entry);
2517 TAILQ_INSERT_TAIL(&head, xfer, wait_entry);
2519 TAILQ_CONCAT(&head, &sc->sc_bus.intr_q.head, wait_entry);
2520 TAILQ_CONCAT(&sc->sc_bus.intr_q.head, &head, wait_entry);
2522 /* put non-TT non-ISOCHRONOUS transfers last */
2523 TAILQ_FOREACH_SAFE(xfer, &sc->sc_bus.intr_q.head, wait_entry, xfer_next) {
2524 td = xfer->td_transfer_cache;
2525 if (td == NULL || td->hcsplt != 0 || td->ep_type == UE_ISOCHRONOUS)
2527 TAILQ_REMOVE(&sc->sc_bus.intr_q.head, xfer, wait_entry);
2528 TAILQ_INSERT_TAIL(&head, xfer, wait_entry);
2530 TAILQ_CONCAT(&sc->sc_bus.intr_q.head, &head, wait_entry);
2532 if ((temp & 7) == 0) {
2534 DPRINTFN(12, "SOF interrupt #%d, needsof=%d\n",
2535 (int)temp, (int)sc->sc_needsof);
2537 /* update SOF IRQ mask */
2538 if (sc->sc_irq_mask & GINTMSK_SOFMSK) {
2539 if (sc->sc_needsof == 0) {
2540 sc->sc_irq_mask &= ~GINTMSK_SOFMSK;
2541 DWC_OTG_WRITE_4(sc, DOTG_GINTMSK, sc->sc_irq_mask);
2544 if (sc->sc_needsof != 0) {
2545 sc->sc_irq_mask |= GINTMSK_SOFMSK;
2546 DWC_OTG_WRITE_4(sc, DOTG_GINTMSK, sc->sc_irq_mask);
2550 /* clear need SOF flag */
2557 dwc_otg_interrupt_poll_locked(struct dwc_otg_softc *sc)
2559 struct usb_xfer *xfer;
2562 uint8_t got_rx_status;
2565 if (sc->sc_flags.status_device_mode == 0) {
2567 * Update host transfer schedule, so that new
2568 * transfers can be issued:
2570 dwc_otg_update_host_transfer_schedule_locked(sc);
2574 if (++count == 16) {
2575 /* give other interrupts a chance */
2579 /* get all host channel interrupts */
2580 for (x = 0; x != sc->sc_host_ch_max; x++) {
2581 temp = DWC_OTG_READ_4(sc, DOTG_HCINT(x));
2583 DWC_OTG_WRITE_4(sc, DOTG_HCINT(x), temp);
2584 temp &= ~HCINT_SOFTWARE_ONLY;
2585 sc->sc_chan_state[x].hcint |= temp;
2589 if (sc->sc_last_rx_status == 0) {
2591 temp = DWC_OTG_READ_4(sc, DOTG_GINTSTS);
2592 if (temp & GINTSTS_RXFLVL) {
2593 /* pop current status */
2594 sc->sc_last_rx_status =
2595 DWC_OTG_READ_4(sc, DOTG_GRXSTSPD);
2598 if (sc->sc_last_rx_status != 0) {
2602 temp = sc->sc_last_rx_status &
2603 GRXSTSRD_PKTSTS_MASK;
2605 /* non-data messages we simply skip */
2606 if (temp != GRXSTSRD_STP_DATA &&
2607 temp != GRXSTSRD_STP_COMPLETE &&
2608 temp != GRXSTSRD_OUT_DATA) {
2609 /* check for halted channel */
2610 if (temp == GRXSTSRH_HALTED) {
2611 ep_no = GRXSTSRD_CHNUM_GET(sc->sc_last_rx_status);
2612 sc->sc_chan_state[ep_no].wait_halted = 0;
2613 DPRINTFN(5, "channel halt complete ch=%u\n", ep_no);
2615 dwc_otg_common_rx_ack(sc);
2619 temp = GRXSTSRD_BCNT_GET(
2620 sc->sc_last_rx_status);
2621 ep_no = GRXSTSRD_CHNUM_GET(
2622 sc->sc_last_rx_status);
2624 /* receive data, if any */
2626 DPRINTF("Reading %d bytes from ep %d\n", temp, ep_no);
2627 bus_space_read_region_4(sc->sc_io_tag, sc->sc_io_hdl,
2629 sc->sc_rx_bounce_buffer, (temp + 3) / 4);
2632 /* check if we should dump the data */
2633 if (!(sc->sc_active_rx_ep & (1U << ep_no))) {
2634 dwc_otg_common_rx_ack(sc);
2640 DPRINTFN(5, "RX status = 0x%08x: ch=%d pid=%d bytes=%d sts=%d\n",
2641 sc->sc_last_rx_status, ep_no,
2642 (sc->sc_last_rx_status >> 15) & 3,
2643 GRXSTSRD_BCNT_GET(sc->sc_last_rx_status),
2644 (sc->sc_last_rx_status >> 17) & 15);
2651 ep_no = GRXSTSRD_CHNUM_GET(
2652 sc->sc_last_rx_status);
2654 /* check if we should dump the data */
2655 if (!(sc->sc_active_rx_ep & (1U << ep_no))) {
2656 dwc_otg_common_rx_ack(sc);
2664 TAILQ_FOREACH(xfer, &sc->sc_bus.intr_q.head, wait_entry)
2665 dwc_otg_xfer_do_fifo(sc, xfer);
2667 if (got_rx_status) {
2668 /* check if data was consumed */
2669 if (sc->sc_last_rx_status == 0)
2672 /* disable RX FIFO level interrupt */
2673 sc->sc_irq_mask &= ~GINTMSK_RXFLVLMSK;
2674 DWC_OTG_WRITE_4(sc, DOTG_GINTMSK, sc->sc_irq_mask);
2679 dwc_otg_interrupt_complete_locked(struct dwc_otg_softc *sc)
2681 struct usb_xfer *xfer;
2683 /* scan for completion events */
2684 TAILQ_FOREACH(xfer, &sc->sc_bus.intr_q.head, wait_entry) {
2685 if (dwc_otg_xfer_do_complete_locked(sc, xfer))
2691 dwc_otg_vbus_interrupt(struct dwc_otg_softc *sc, uint8_t is_on)
2693 DPRINTFN(5, "vbus = %u\n", is_on);
2696 if (!sc->sc_flags.status_vbus) {
2697 sc->sc_flags.status_vbus = 1;
2699 /* complete root HUB interrupt endpoint */
2701 dwc_otg_root_intr(sc);
2704 if (sc->sc_flags.status_vbus) {
2705 sc->sc_flags.status_vbus = 0;
2706 sc->sc_flags.status_bus_reset = 0;
2707 sc->sc_flags.status_suspend = 0;
2708 sc->sc_flags.change_suspend = 0;
2709 sc->sc_flags.change_connect = 1;
2711 /* complete root HUB interrupt endpoint */
2713 dwc_otg_root_intr(sc);
2719 dwc_otg_filter_interrupt(void *arg)
2721 struct dwc_otg_softc *sc = arg;
2722 int retval = FILTER_HANDLED;
2725 USB_BUS_SPIN_LOCK(&sc->sc_bus);
2727 /* read and clear interrupt status */
2728 status = DWC_OTG_READ_4(sc, DOTG_GINTSTS);
2730 /* clear interrupts we are handling here */
2731 DWC_OTG_WRITE_4(sc, DOTG_GINTSTS, status & ~DWC_OTG_MSK_GINT_THREAD_IRQ);
2733 /* check for USB state change interrupts */
2734 if ((status & DWC_OTG_MSK_GINT_THREAD_IRQ) != 0)
2735 retval = FILTER_SCHEDULE_THREAD;
2737 /* clear FIFO empty interrupts */
2738 if (status & sc->sc_irq_mask &
2739 (GINTSTS_PTXFEMP | GINTSTS_NPTXFEMP)) {
2740 sc->sc_irq_mask &= ~(GINTSTS_PTXFEMP | GINTSTS_NPTXFEMP);
2741 DWC_OTG_WRITE_4(sc, DOTG_GINTMSK, sc->sc_irq_mask);
2743 /* clear all IN endpoint interrupts */
2744 if (status & GINTSTS_IEPINT) {
2748 for (x = 0; x != sc->sc_dev_in_ep_max; x++) {
2749 temp = DWC_OTG_READ_4(sc, DOTG_DIEPINT(x));
2750 if (temp & DIEPMSK_XFERCOMPLMSK) {
2751 DWC_OTG_WRITE_4(sc, DOTG_DIEPINT(x),
2752 DIEPMSK_XFERCOMPLMSK);
2757 /* poll FIFOs, if any */
2758 dwc_otg_interrupt_poll_locked(sc);
2760 if (sc->sc_xfer_complete != 0)
2761 retval = FILTER_SCHEDULE_THREAD;
2763 USB_BUS_SPIN_UNLOCK(&sc->sc_bus);
2769 dwc_otg_interrupt(void *arg)
2771 struct dwc_otg_softc *sc = arg;
2774 USB_BUS_LOCK(&sc->sc_bus);
2775 USB_BUS_SPIN_LOCK(&sc->sc_bus);
2777 /* read and clear interrupt status */
2778 status = DWC_OTG_READ_4(sc, DOTG_GINTSTS);
2780 /* clear interrupts we are handling here */
2781 DWC_OTG_WRITE_4(sc, DOTG_GINTSTS, status & DWC_OTG_MSK_GINT_THREAD_IRQ);
2783 DPRINTFN(14, "GINTSTS=0x%08x HAINT=0x%08x HFNUM=0x%08x\n",
2784 status, DWC_OTG_READ_4(sc, DOTG_HAINT),
2785 DWC_OTG_READ_4(sc, DOTG_HFNUM));
2787 if (status & GINTSTS_USBRST) {
2789 /* set correct state */
2790 sc->sc_flags.status_device_mode = 1;
2791 sc->sc_flags.status_bus_reset = 0;
2792 sc->sc_flags.status_suspend = 0;
2793 sc->sc_flags.change_suspend = 0;
2794 sc->sc_flags.change_connect = 1;
2796 /* Disable SOF interrupt */
2797 sc->sc_irq_mask &= ~GINTMSK_SOFMSK;
2798 DWC_OTG_WRITE_4(sc, DOTG_GINTMSK, sc->sc_irq_mask);
2800 /* complete root HUB interrupt endpoint */
2801 dwc_otg_root_intr(sc);
2804 /* check for any bus state change interrupts */
2805 if (status & GINTSTS_ENUMDONE) {
2809 DPRINTFN(5, "end of reset\n");
2811 /* set correct state */
2812 sc->sc_flags.status_device_mode = 1;
2813 sc->sc_flags.status_bus_reset = 1;
2814 sc->sc_flags.status_suspend = 0;
2815 sc->sc_flags.change_suspend = 0;
2816 sc->sc_flags.change_connect = 1;
2817 sc->sc_flags.status_low_speed = 0;
2818 sc->sc_flags.port_enabled = 1;
2821 (void) dwc_otg_init_fifo(sc, DWC_MODE_DEVICE);
2823 /* reset function address */
2824 dwc_otg_set_address(sc, 0);
2826 /* figure out enumeration speed */
2827 temp = DWC_OTG_READ_4(sc, DOTG_DSTS);
2828 if (DSTS_ENUMSPD_GET(temp) == DSTS_ENUMSPD_HI)
2829 sc->sc_flags.status_high_speed = 1;
2831 sc->sc_flags.status_high_speed = 0;
2834 * Disable resume and SOF interrupt, and enable
2835 * suspend and RX frame interrupt:
2837 sc->sc_irq_mask &= ~(GINTMSK_WKUPINTMSK | GINTMSK_SOFMSK);
2838 sc->sc_irq_mask |= GINTMSK_USBSUSPMSK;
2839 DWC_OTG_WRITE_4(sc, DOTG_GINTMSK, sc->sc_irq_mask);
2841 /* complete root HUB interrupt endpoint */
2842 dwc_otg_root_intr(sc);
2845 if (status & GINTSTS_PRTINT) {
2848 hprt = DWC_OTG_READ_4(sc, DOTG_HPRT);
2850 /* clear change bits */
2851 DWC_OTG_WRITE_4(sc, DOTG_HPRT, (hprt & (
2852 HPRT_PRTPWR | HPRT_PRTENCHNG |
2853 HPRT_PRTCONNDET | HPRT_PRTOVRCURRCHNG)) |
2856 DPRINTFN(12, "GINTSTS=0x%08x, HPRT=0x%08x\n", status, hprt);
2858 sc->sc_flags.status_device_mode = 0;
2860 if (hprt & HPRT_PRTCONNSTS)
2861 sc->sc_flags.status_bus_reset = 1;
2863 sc->sc_flags.status_bus_reset = 0;
2865 if (hprt & HPRT_PRTENCHNG)
2866 sc->sc_flags.change_enabled = 1;
2868 if (hprt & HPRT_PRTENA)
2869 sc->sc_flags.port_enabled = 1;
2871 sc->sc_flags.port_enabled = 0;
2873 if (hprt & HPRT_PRTOVRCURRCHNG)
2874 sc->sc_flags.change_over_current = 1;
2876 if (hprt & HPRT_PRTOVRCURRACT)
2877 sc->sc_flags.port_over_current = 1;
2879 sc->sc_flags.port_over_current = 0;
2881 if (hprt & HPRT_PRTPWR)
2882 sc->sc_flags.port_powered = 1;
2884 sc->sc_flags.port_powered = 0;
2886 if (((hprt & HPRT_PRTSPD_MASK)
2887 >> HPRT_PRTSPD_SHIFT) == HPRT_PRTSPD_LOW)
2888 sc->sc_flags.status_low_speed = 1;
2890 sc->sc_flags.status_low_speed = 0;
2892 if (((hprt & HPRT_PRTSPD_MASK)
2893 >> HPRT_PRTSPD_SHIFT) == HPRT_PRTSPD_HIGH)
2894 sc->sc_flags.status_high_speed = 1;
2896 sc->sc_flags.status_high_speed = 0;
2898 if (hprt & HPRT_PRTCONNDET)
2899 sc->sc_flags.change_connect = 1;
2901 if (hprt & HPRT_PRTSUSP)
2902 dwc_otg_suspend_irq(sc);
2904 dwc_otg_resume_irq(sc);
2906 /* complete root HUB interrupt endpoint */
2907 dwc_otg_root_intr(sc);
2909 /* update host frame interval */
2910 dwc_otg_update_host_frame_interval(sc);
2914 * If resume and suspend is set at the same time we interpret
2915 * that like RESUME. Resume is set when there is at least 3
2916 * milliseconds of inactivity on the USB BUS.
2918 if (status & GINTSTS_WKUPINT) {
2920 DPRINTFN(5, "resume interrupt\n");
2922 dwc_otg_resume_irq(sc);
2924 } else if (status & GINTSTS_USBSUSP) {
2926 DPRINTFN(5, "suspend interrupt\n");
2928 dwc_otg_suspend_irq(sc);
2931 if (status & (GINTSTS_USBSUSP |
2934 GINTSTS_SESSREQINT)) {
2937 temp = DWC_OTG_READ_4(sc, DOTG_GOTGCTL);
2939 DPRINTFN(5, "GOTGCTL=0x%08x\n", temp);
2941 dwc_otg_vbus_interrupt(sc,
2942 (temp & (GOTGCTL_ASESVLD | GOTGCTL_BSESVLD)) ? 1 : 0);
2945 if (sc->sc_xfer_complete != 0) {
2946 sc->sc_xfer_complete = 0;
2948 /* complete FIFOs, if any */
2949 dwc_otg_interrupt_complete_locked(sc);
2951 USB_BUS_SPIN_UNLOCK(&sc->sc_bus);
2952 USB_BUS_UNLOCK(&sc->sc_bus);
2956 dwc_otg_setup_standard_chain_sub(struct dwc_otg_std_temp *temp)
2958 struct dwc_otg_td *td;
2960 /* get current Transfer Descriptor */
2964 /* prepare for next TD */
2965 temp->td_next = td->obj_next;
2967 /* fill out the Transfer Descriptor */
2968 td->func = temp->func;
2970 td->offset = temp->offset;
2971 td->remainder = temp->len;
2974 td->error_stall = 0;
2976 td->did_stall = temp->did_stall;
2977 td->short_pkt = temp->short_pkt;
2978 td->alt_next = temp->setup_alt_next;
2982 td->channel[0] = DWC_OTG_MAX_CHANNELS;
2983 td->channel[1] = DWC_OTG_MAX_CHANNELS;
2984 td->channel[2] = DWC_OTG_MAX_CHANNELS;
2987 td->tt_scheduled = 0;
2988 td->tt_xactpos = HCSPLT_XACTPOS_BEGIN;
2992 dwc_otg_setup_standard_chain(struct usb_xfer *xfer)
2994 struct dwc_otg_std_temp temp;
2995 struct dwc_otg_td *td;
3000 DPRINTFN(9, "addr=%d endpt=%d sumlen=%d speed=%d\n",
3001 xfer->address, UE_GET_ADDR(xfer->endpointno),
3002 xfer->sumlen, usbd_get_speed(xfer->xroot->udev));
3004 temp.max_frame_size = xfer->max_frame_size;
3006 td = xfer->td_start[0];
3007 xfer->td_transfer_first = td;
3008 xfer->td_transfer_cache = td;
3014 temp.td_next = xfer->td_start[0];
3016 temp.setup_alt_next = xfer->flags_int.short_frames_ok ||
3017 xfer->flags_int.isochronous_xfr;
3018 temp.did_stall = !xfer->flags_int.control_stall;
3020 is_host = (xfer->xroot->udev->flags.usb_mode == USB_MODE_HOST);
3022 /* check if we should prepend a setup message */
3024 if (xfer->flags_int.control_xfr) {
3025 if (xfer->flags_int.control_hdr) {
3028 temp.func = &dwc_otg_host_setup_tx;
3030 temp.func = &dwc_otg_setup_rx;
3032 temp.len = xfer->frlengths[0];
3033 temp.pc = xfer->frbuffers + 0;
3034 temp.short_pkt = temp.len ? 1 : 0;
3036 /* check for last frame */
3037 if (xfer->nframes == 1) {
3038 /* no STATUS stage yet, SETUP is last */
3039 if (xfer->flags_int.control_act)
3040 temp.setup_alt_next = 0;
3043 dwc_otg_setup_standard_chain_sub(&temp);
3050 if (x != xfer->nframes) {
3051 if (xfer->endpointno & UE_DIR_IN) {
3053 temp.func = &dwc_otg_host_data_rx;
3056 temp.func = &dwc_otg_data_tx;
3061 temp.func = &dwc_otg_host_data_tx;
3064 temp.func = &dwc_otg_data_rx;
3069 /* setup "pc" pointer */
3070 temp.pc = xfer->frbuffers + x;
3074 while (x != xfer->nframes) {
3076 /* DATA0 / DATA1 message */
3078 temp.len = xfer->frlengths[x];
3082 if (x == xfer->nframes) {
3083 if (xfer->flags_int.control_xfr) {
3084 if (xfer->flags_int.control_act) {
3085 temp.setup_alt_next = 0;
3088 temp.setup_alt_next = 0;
3091 if (temp.len == 0) {
3093 /* make sure that we send an USB packet */
3099 /* regular data transfer */
3101 temp.short_pkt = (xfer->flags.force_short_xfer ? 0 : 1);
3104 dwc_otg_setup_standard_chain_sub(&temp);
3106 if (xfer->flags_int.isochronous_xfr) {
3107 temp.offset += temp.len;
3109 /* get next Page Cache pointer */
3110 temp.pc = xfer->frbuffers + x;
3114 if (xfer->flags_int.control_xfr) {
3116 /* always setup a valid "pc" pointer for status and sync */
3117 temp.pc = xfer->frbuffers + 0;
3120 temp.setup_alt_next = 0;
3122 /* check if we need to sync */
3124 /* we need a SYNC point after TX */
3125 temp.func = &dwc_otg_data_tx_sync;
3126 dwc_otg_setup_standard_chain_sub(&temp);
3129 /* check if we should append a status stage */
3130 if (!xfer->flags_int.control_act) {
3133 * Send a DATA1 message and invert the current
3134 * endpoint direction.
3136 if (xfer->endpointno & UE_DIR_IN) {
3138 temp.func = &dwc_otg_host_data_tx;
3141 temp.func = &dwc_otg_data_rx;
3146 temp.func = &dwc_otg_host_data_rx;
3149 temp.func = &dwc_otg_data_tx;
3154 dwc_otg_setup_standard_chain_sub(&temp);
3156 /* data toggle should be DATA1 */
3161 /* we need a SYNC point after TX */
3162 temp.func = &dwc_otg_data_tx_sync;
3163 dwc_otg_setup_standard_chain_sub(&temp);
3167 /* check if we need to sync */
3170 temp.pc = xfer->frbuffers + 0;
3173 temp.setup_alt_next = 0;
3175 /* we need a SYNC point after TX */
3176 temp.func = &dwc_otg_data_tx_sync;
3177 dwc_otg_setup_standard_chain_sub(&temp);
3181 /* must have at least one frame! */
3183 xfer->td_transfer_last = td;
3187 struct dwc_otg_softc *sc;
3191 sc = DWC_OTG_BUS2SC(xfer->xroot->bus);
3193 /* get first again */
3194 td = xfer->td_transfer_first;
3195 td->toggle = (xfer->endpoint->toggle_next ? 1 : 0);
3198 (xfer->address << HCCHAR_DEVADDR_SHIFT) |
3199 ((xfer->endpointno & UE_ADDR) << HCCHAR_EPNUM_SHIFT) |
3200 (xfer->max_packet_size << HCCHAR_MPS_SHIFT) |
3204 * We are not always able to meet the timing
3205 * requirements of the USB interrupt endpoint's
3206 * complete split token, when doing transfers going
3207 * via a transaction translator. Use the CONTROL
3208 * transfer type instead of the INTERRUPT transfer
3209 * type in general, as a means to workaround
3210 * that. This trick should work for both FULL and LOW
3211 * speed USB traffic going through a TT. For non-TT
3212 * traffic it works aswell. The reason for using
3213 * CONTROL type instead of BULK is that some TTs might
3214 * reject LOW speed BULK traffic.
3216 if (td->ep_type == UE_INTERRUPT)
3217 hcchar |= (UE_CONTROL << HCCHAR_EPTYPE_SHIFT);
3219 hcchar |= (td->ep_type << HCCHAR_EPTYPE_SHIFT);
3221 if (usbd_get_speed(xfer->xroot->udev) == USB_SPEED_LOW)
3222 hcchar |= HCCHAR_LSPDDEV;
3223 if (UE_GET_DIR(xfer->endpointno) == UE_DIR_IN)
3224 hcchar |= HCCHAR_EPDIR_IN;
3226 switch (xfer->xroot->udev->speed) {
3227 case USB_SPEED_FULL:
3229 /* check if root HUB port is running High Speed */
3230 if (xfer->xroot->udev->parent_hs_hub != NULL) {
3231 hcsplt = HCSPLT_SPLTENA |
3232 (xfer->xroot->udev->hs_port_no <<
3233 HCSPLT_PRTADDR_SHIFT) |
3234 (xfer->xroot->udev->hs_hub_addr <<
3235 HCSPLT_HUBADDR_SHIFT);
3239 if (td->ep_type == UE_INTERRUPT) {
3241 ival = xfer->interval / DWC_OTG_HOST_TIMER_RATE;
3244 else if (ival > 127)
3246 td->tmr_val = sc->sc_tmr_val + ival;
3248 } else if (td->ep_type == UE_ISOCHRONOUS) {
3250 td->tmr_val = sc->sc_last_frame_num;
3251 if (td->hcchar & HCCHAR_EPDIR_IN)
3255 td->tmr_res = (uint8_t)sc->sc_last_frame_num;
3258 case USB_SPEED_HIGH:
3260 if (td->ep_type == UE_INTERRUPT) {
3262 hcchar |= ((xfer->max_packet_count & 3)
3263 << HCCHAR_MC_SHIFT);
3264 ival = xfer->interval / DWC_OTG_HOST_TIMER_RATE;
3267 else if (ival > 127)
3269 td->tmr_val = sc->sc_tmr_val + ival;
3271 } else if (td->ep_type == UE_ISOCHRONOUS) {
3272 hcchar |= ((xfer->max_packet_count & 3)
3273 << HCCHAR_MC_SHIFT);
3274 td->tmr_res = 1 << usbd_xfer_get_fps_shift(xfer);
3275 td->tmr_val = sc->sc_last_frame_num;
3276 if (td->hcchar & HCCHAR_EPDIR_IN)
3277 td->tmr_val += td->tmr_res;
3281 td->tmr_res = (uint8_t)sc->sc_last_frame_num;
3291 /* store configuration in all TD's */
3293 td->hcchar = hcchar;
3294 td->hcsplt = hcsplt;
3296 if (((void *)td) == xfer->td_transfer_last)
3305 dwc_otg_timeout(void *arg)
3307 struct usb_xfer *xfer = arg;
3309 DPRINTF("xfer=%p\n", xfer);
3311 USB_BUS_LOCK_ASSERT(xfer->xroot->bus, MA_OWNED);
3313 /* transfer is transferred */
3314 dwc_otg_device_done(xfer, USB_ERR_TIMEOUT);
3318 dwc_otg_start_standard_chain(struct usb_xfer *xfer)
3320 struct dwc_otg_softc *sc = DWC_OTG_BUS2SC(xfer->xroot->bus);
3325 * Poll one time in device mode, which will turn on the
3326 * endpoint interrupts. Else wait for SOF interrupt in host
3329 USB_BUS_SPIN_LOCK(&sc->sc_bus);
3331 if (sc->sc_flags.status_device_mode != 0) {
3332 dwc_otg_xfer_do_fifo(sc, xfer);
3333 if (dwc_otg_xfer_do_complete_locked(sc, xfer))
3336 struct dwc_otg_td *td = xfer->td_transfer_cache;
3337 if (td->ep_type == UE_ISOCHRONOUS &&
3338 (td->hcchar & HCCHAR_EPDIR_IN) == 0) {
3340 * Need to start ISOCHRONOUS OUT transfer ASAP
3341 * because execution is delayed by one 125us
3344 dwc_otg_xfer_do_fifo(sc, xfer);
3345 if (dwc_otg_xfer_do_complete_locked(sc, xfer))
3350 /* put transfer on interrupt queue */
3351 usbd_transfer_enqueue(&xfer->xroot->bus->intr_q, xfer);
3353 /* start timeout, if any */
3354 if (xfer->timeout != 0) {
3355 usbd_transfer_timeout_ms(xfer,
3356 &dwc_otg_timeout, xfer->timeout);
3359 if (sc->sc_flags.status_device_mode != 0)
3362 /* enable SOF interrupt, if any */
3363 dwc_otg_enable_sof_irq(sc);
3365 USB_BUS_SPIN_UNLOCK(&sc->sc_bus);
3369 dwc_otg_root_intr(struct dwc_otg_softc *sc)
3373 USB_BUS_LOCK_ASSERT(&sc->sc_bus, MA_OWNED);
3376 sc->sc_hub_idata[0] = 0x02; /* we only have one port */
3378 uhub_root_intr(&sc->sc_bus, sc->sc_hub_idata,
3379 sizeof(sc->sc_hub_idata));
3383 dwc_otg_standard_done_sub(struct usb_xfer *xfer)
3385 struct dwc_otg_td *td;
3391 td = xfer->td_transfer_cache;
3394 len = td->remainder;
3396 /* store last data toggle */
3397 xfer->endpoint->toggle_next = td->toggle;
3399 if (xfer->aframes != xfer->nframes) {
3401 * Verify the length and subtract
3402 * the remainder from "frlengths[]":
3404 if (len > xfer->frlengths[xfer->aframes]) {
3407 xfer->frlengths[xfer->aframes] -= len;
3410 /* Check for transfer error */
3411 if (td->error_any) {
3412 /* the transfer is finished */
3413 error = (td->error_stall ?
3414 USB_ERR_STALLED : USB_ERR_IOERROR);
3418 /* Check for short transfer */
3420 if (xfer->flags_int.short_frames_ok ||
3421 xfer->flags_int.isochronous_xfr) {
3422 /* follow alt next */
3429 /* the transfer is finished */
3437 /* this USB frame is complete */
3443 /* update transfer cache */
3445 xfer->td_transfer_cache = td;
3451 dwc_otg_standard_done(struct usb_xfer *xfer)
3453 usb_error_t err = 0;
3455 DPRINTFN(13, "xfer=%p endpoint=%p transfer done\n",
3456 xfer, xfer->endpoint);
3460 xfer->td_transfer_cache = xfer->td_transfer_first;
3462 if (xfer->flags_int.control_xfr) {
3464 if (xfer->flags_int.control_hdr) {
3466 err = dwc_otg_standard_done_sub(xfer);
3470 if (xfer->td_transfer_cache == NULL) {
3474 while (xfer->aframes != xfer->nframes) {
3476 err = dwc_otg_standard_done_sub(xfer);
3479 if (xfer->td_transfer_cache == NULL) {
3484 if (xfer->flags_int.control_xfr &&
3485 !xfer->flags_int.control_act) {
3487 err = dwc_otg_standard_done_sub(xfer);
3490 dwc_otg_device_done(xfer, err);
3493 /*------------------------------------------------------------------------*
3494 * dwc_otg_device_done
3496 * NOTE: this function can be called more than one time on the
3497 * same USB transfer!
3498 *------------------------------------------------------------------------*/
3500 dwc_otg_device_done(struct usb_xfer *xfer, usb_error_t error)
3502 struct dwc_otg_softc *sc = DWC_OTG_BUS2SC(xfer->xroot->bus);
3504 DPRINTFN(9, "xfer=%p, endpoint=%p, error=%d\n",
3505 xfer, xfer->endpoint, error);
3507 USB_BUS_SPIN_LOCK(&sc->sc_bus);
3509 if (xfer->flags_int.usb_mode == USB_MODE_DEVICE) {
3510 /* Interrupts are cleared by the interrupt handler */
3512 struct dwc_otg_td *td;
3514 td = xfer->td_transfer_cache;
3516 dwc_otg_host_channel_free(sc, td);
3518 /* dequeue transfer and start next transfer */
3519 usbd_transfer_done(xfer, error);
3521 USB_BUS_SPIN_UNLOCK(&sc->sc_bus);
3525 dwc_otg_xfer_stall(struct usb_xfer *xfer)
3527 dwc_otg_device_done(xfer, USB_ERR_STALLED);
3531 dwc_otg_set_stall(struct usb_device *udev,
3532 struct usb_endpoint *ep, uint8_t *did_stall)
3534 struct dwc_otg_softc *sc;
3539 USB_BUS_LOCK_ASSERT(udev->bus, MA_OWNED);
3542 if (udev->flags.usb_mode != USB_MODE_DEVICE) {
3547 sc = DWC_OTG_BUS2SC(udev->bus);
3549 USB_BUS_SPIN_LOCK(&sc->sc_bus);
3551 /* get endpoint address */
3552 ep_no = ep->edesc->bEndpointAddress;
3554 DPRINTFN(5, "endpoint=0x%x\n", ep_no);
3556 if (ep_no & UE_DIR_IN) {
3557 reg = DOTG_DIEPCTL(ep_no & UE_ADDR);
3558 temp = sc->sc_in_ctl[ep_no & UE_ADDR];
3560 reg = DOTG_DOEPCTL(ep_no & UE_ADDR);
3561 temp = sc->sc_out_ctl[ep_no & UE_ADDR];
3564 /* disable and stall endpoint */
3565 DWC_OTG_WRITE_4(sc, reg, temp | DOEPCTL_EPDIS);
3566 DWC_OTG_WRITE_4(sc, reg, temp | DOEPCTL_STALL);
3568 /* clear active OUT ep */
3569 if (!(ep_no & UE_DIR_IN)) {
3571 sc->sc_active_rx_ep &= ~(1U << (ep_no & UE_ADDR));
3573 if (sc->sc_last_rx_status != 0 &&
3574 (ep_no & UE_ADDR) == GRXSTSRD_CHNUM_GET(
3575 sc->sc_last_rx_status)) {
3577 dwc_otg_common_rx_ack(sc);
3578 /* poll interrupt */
3579 dwc_otg_interrupt_poll_locked(sc);
3580 dwc_otg_interrupt_complete_locked(sc);
3583 USB_BUS_SPIN_UNLOCK(&sc->sc_bus);
3587 dwc_otg_clear_stall_sub_locked(struct dwc_otg_softc *sc, uint32_t mps,
3588 uint8_t ep_no, uint8_t ep_type, uint8_t ep_dir)
3593 if (ep_type == UE_CONTROL) {
3594 /* clearing stall is not needed */
3599 reg = DOTG_DIEPCTL(ep_no);
3601 reg = DOTG_DOEPCTL(ep_no);
3602 sc->sc_active_rx_ep |= (1U << ep_no);
3605 /* round up and mask away the multiplier count */
3606 mps = (mps + 3) & 0x7FC;
3608 if (ep_type == UE_BULK) {
3609 temp = DIEPCTL_EPTYPE_SET(
3610 DIEPCTL_EPTYPE_BULK) |
3612 } else if (ep_type == UE_INTERRUPT) {
3613 temp = DIEPCTL_EPTYPE_SET(
3614 DIEPCTL_EPTYPE_INTERRUPT) |
3617 temp = DIEPCTL_EPTYPE_SET(
3618 DIEPCTL_EPTYPE_ISOC) |
3622 temp |= DIEPCTL_MPS_SET(mps);
3623 temp |= DIEPCTL_TXFNUM_SET(ep_no);
3626 sc->sc_in_ctl[ep_no] = temp;
3628 sc->sc_out_ctl[ep_no] = temp;
3630 DWC_OTG_WRITE_4(sc, reg, temp | DOEPCTL_EPDIS);
3631 DWC_OTG_WRITE_4(sc, reg, temp | DOEPCTL_SETD0PID);
3632 DWC_OTG_WRITE_4(sc, reg, temp | DIEPCTL_SNAK);
3634 /* we only reset the transmit FIFO */
3636 dwc_otg_tx_fifo_reset(sc,
3637 GRSTCTL_TXFIFO(ep_no) |
3641 DOTG_DIEPTSIZ(ep_no), 0);
3644 /* poll interrupt */
3645 dwc_otg_interrupt_poll_locked(sc);
3646 dwc_otg_interrupt_complete_locked(sc);
3650 dwc_otg_clear_stall(struct usb_device *udev, struct usb_endpoint *ep)
3652 struct dwc_otg_softc *sc;
3653 struct usb_endpoint_descriptor *ed;
3655 DPRINTFN(5, "endpoint=%p\n", ep);
3657 USB_BUS_LOCK_ASSERT(udev->bus, MA_OWNED);
3660 if (udev->flags.usb_mode != USB_MODE_DEVICE) {
3665 sc = DWC_OTG_BUS2SC(udev->bus);
3667 USB_BUS_SPIN_LOCK(&sc->sc_bus);
3669 /* get endpoint descriptor */
3672 /* reset endpoint */
3673 dwc_otg_clear_stall_sub_locked(sc,
3674 UGETW(ed->wMaxPacketSize),
3675 (ed->bEndpointAddress & UE_ADDR),
3676 (ed->bmAttributes & UE_XFERTYPE),
3677 (ed->bEndpointAddress & (UE_DIR_IN | UE_DIR_OUT)));
3679 USB_BUS_SPIN_UNLOCK(&sc->sc_bus);
3683 dwc_otg_device_state_change(struct usb_device *udev)
3685 struct dwc_otg_softc *sc;
3689 if (udev->flags.usb_mode != USB_MODE_DEVICE) {
3695 sc = DWC_OTG_BUS2SC(udev->bus);
3697 /* deactivate all other endpoint but the control endpoint */
3698 if (udev->state == USB_STATE_CONFIGURED ||
3699 udev->state == USB_STATE_ADDRESSED) {
3701 USB_BUS_LOCK(&sc->sc_bus);
3703 for (x = 1; x != sc->sc_dev_ep_max; x++) {
3705 if (x < sc->sc_dev_in_ep_max) {
3706 DWC_OTG_WRITE_4(sc, DOTG_DIEPCTL(x),
3708 DWC_OTG_WRITE_4(sc, DOTG_DIEPCTL(x), 0);
3711 DWC_OTG_WRITE_4(sc, DOTG_DOEPCTL(x),
3713 DWC_OTG_WRITE_4(sc, DOTG_DOEPCTL(x), 0);
3715 USB_BUS_UNLOCK(&sc->sc_bus);
3720 dwc_otg_init(struct dwc_otg_softc *sc)
3726 /* set up the bus structure */
3727 sc->sc_bus.usbrev = USB_REV_2_0;
3728 sc->sc_bus.methods = &dwc_otg_bus_methods;
3730 usb_callout_init_mtx(&sc->sc_timer,
3731 &sc->sc_bus.bus_mtx, 0);
3733 USB_BUS_LOCK(&sc->sc_bus);
3735 /* turn on clocks */
3736 dwc_otg_clocks_on(sc);
3738 temp = DWC_OTG_READ_4(sc, DOTG_GSNPSID);
3739 DPRINTF("Version = 0x%08x\n", temp);
3742 DWC_OTG_WRITE_4(sc, DOTG_DCTL,
3745 /* wait for host to detect disconnect */
3746 usb_pause_mtx(&sc->sc_bus.bus_mtx, hz / 32);
3748 DWC_OTG_WRITE_4(sc, DOTG_GRSTCTL,
3751 /* wait a little bit for block to reset */
3752 usb_pause_mtx(&sc->sc_bus.bus_mtx, hz / 128);
3754 switch (sc->sc_mode) {
3755 case DWC_MODE_DEVICE:
3756 temp = GUSBCFG_FORCEDEVMODE;
3759 temp = GUSBCFG_FORCEHOSTMODE;
3766 /* select HSIC, ULPI or internal PHY mode */
3767 switch (dwc_otg_phy_type) {
3768 case DWC_OTG_PHY_HSIC:
3769 DWC_OTG_WRITE_4(sc, DOTG_GUSBCFG,
3771 GUSBCFG_TRD_TIM_SET(5) | temp);
3772 DWC_OTG_WRITE_4(sc, DOTG_GOTGCTL,
3775 temp = DWC_OTG_READ_4(sc, DOTG_GLPMCFG);
3776 DWC_OTG_WRITE_4(sc, DOTG_GLPMCFG,
3777 temp & ~GLPMCFG_HSIC_CONN);
3778 DWC_OTG_WRITE_4(sc, DOTG_GLPMCFG,
3779 temp | GLPMCFG_HSIC_CONN);
3781 case DWC_OTG_PHY_ULPI:
3782 DWC_OTG_WRITE_4(sc, DOTG_GUSBCFG,
3783 GUSBCFG_ULPI_UTMI_SEL |
3784 GUSBCFG_TRD_TIM_SET(5) | temp);
3785 DWC_OTG_WRITE_4(sc, DOTG_GOTGCTL, 0);
3787 temp = DWC_OTG_READ_4(sc, DOTG_GLPMCFG);
3788 DWC_OTG_WRITE_4(sc, DOTG_GLPMCFG,
3789 temp & ~GLPMCFG_HSIC_CONN);
3791 case DWC_OTG_PHY_INTERNAL:
3792 DWC_OTG_WRITE_4(sc, DOTG_GUSBCFG,
3794 GUSBCFG_TRD_TIM_SET(5) | temp);
3795 DWC_OTG_WRITE_4(sc, DOTG_GOTGCTL, 0);
3797 temp = DWC_OTG_READ_4(sc, DOTG_GLPMCFG);
3798 DWC_OTG_WRITE_4(sc, DOTG_GLPMCFG,
3799 temp & ~GLPMCFG_HSIC_CONN);
3801 temp = DWC_OTG_READ_4(sc, DOTG_GGPIO);
3802 temp &= ~(DOTG_GGPIO_NOVBUSSENS | DOTG_GGPIO_I2CPADEN);
3803 temp |= (DOTG_GGPIO_VBUSASEN | DOTG_GGPIO_VBUSBSEN |
3805 DWC_OTG_WRITE_4(sc, DOTG_GGPIO, temp);
3811 /* clear global nak */
3812 DWC_OTG_WRITE_4(sc, DOTG_DCTL,
3816 /* disable USB port */
3817 DWC_OTG_WRITE_4(sc, DOTG_PCGCCTL, 0xFFFFFFFF);
3820 usb_pause_mtx(&sc->sc_bus.bus_mtx, hz / 100);
3822 /* enable USB port */
3823 DWC_OTG_WRITE_4(sc, DOTG_PCGCCTL, 0);
3826 usb_pause_mtx(&sc->sc_bus.bus_mtx, hz / 100);
3828 temp = DWC_OTG_READ_4(sc, DOTG_GHWCFG3);
3830 sc->sc_fifo_size = 4 * GHWCFG3_DFIFODEPTH_GET(temp);
3832 temp = DWC_OTG_READ_4(sc, DOTG_GHWCFG2);
3834 sc->sc_dev_ep_max = GHWCFG2_NUMDEVEPS_GET(temp);
3836 if (sc->sc_dev_ep_max > DWC_OTG_MAX_ENDPOINTS)
3837 sc->sc_dev_ep_max = DWC_OTG_MAX_ENDPOINTS;
3839 sc->sc_host_ch_max = GHWCFG2_NUMHSTCHNL_GET(temp);
3841 if (sc->sc_host_ch_max > DWC_OTG_MAX_CHANNELS)
3842 sc->sc_host_ch_max = DWC_OTG_MAX_CHANNELS;
3844 temp = DWC_OTG_READ_4(sc, DOTG_GHWCFG4);
3846 sc->sc_dev_in_ep_max = GHWCFG4_NUM_IN_EP_GET(temp);
3848 DPRINTF("Total FIFO size = %d bytes, Device EPs = %d/%d Host CHs = %d\n",
3849 sc->sc_fifo_size, sc->sc_dev_ep_max, sc->sc_dev_in_ep_max,
3850 sc->sc_host_ch_max);
3853 if (dwc_otg_init_fifo(sc, DWC_MODE_OTG)) {
3854 USB_BUS_UNLOCK(&sc->sc_bus);
3858 /* enable interrupts */
3859 sc->sc_irq_mask = DWC_OTG_MSK_GINT_ENABLED;
3860 DWC_OTG_WRITE_4(sc, DOTG_GINTMSK, sc->sc_irq_mask);
3862 if (sc->sc_mode == DWC_MODE_OTG || sc->sc_mode == DWC_MODE_DEVICE) {
3864 /* enable all endpoint interrupts */
3865 temp = DWC_OTG_READ_4(sc, DOTG_GHWCFG2);
3866 if (temp & GHWCFG2_MPI) {
3869 DPRINTF("Disable Multi Process Interrupts\n");
3871 for (x = 0; x != sc->sc_dev_in_ep_max; x++) {
3872 DWC_OTG_WRITE_4(sc, DOTG_DIEPEACHINTMSK(x), 0);
3873 DWC_OTG_WRITE_4(sc, DOTG_DOEPEACHINTMSK(x), 0);
3875 DWC_OTG_WRITE_4(sc, DOTG_DEACHINTMSK, 0);
3877 DWC_OTG_WRITE_4(sc, DOTG_DIEPMSK,
3878 DIEPMSK_XFERCOMPLMSK);
3879 DWC_OTG_WRITE_4(sc, DOTG_DOEPMSK, 0);
3880 DWC_OTG_WRITE_4(sc, DOTG_DAINTMSK, 0xFFFF);
3883 if (sc->sc_mode == DWC_MODE_OTG || sc->sc_mode == DWC_MODE_HOST) {
3885 temp = DWC_OTG_READ_4(sc, DOTG_HCFG);
3886 temp &= ~(HCFG_FSLSSUPP | HCFG_FSLSPCLKSEL_MASK);
3887 temp |= (1 << HCFG_FSLSPCLKSEL_SHIFT);
3888 DWC_OTG_WRITE_4(sc, DOTG_HCFG, temp);
3891 /* only enable global IRQ */
3892 DWC_OTG_WRITE_4(sc, DOTG_GAHBCFG,
3893 GAHBCFG_GLBLINTRMSK);
3895 /* turn off clocks */
3896 dwc_otg_clocks_off(sc);
3898 /* read initial VBUS state */
3900 temp = DWC_OTG_READ_4(sc, DOTG_GOTGCTL);
3902 DPRINTFN(5, "GOTCTL=0x%08x\n", temp);
3904 dwc_otg_vbus_interrupt(sc,
3905 (temp & (GOTGCTL_ASESVLD | GOTGCTL_BSESVLD)) ? 1 : 0);
3907 USB_BUS_UNLOCK(&sc->sc_bus);
3909 /* catch any lost interrupts */
3911 dwc_otg_do_poll(&sc->sc_bus);
3913 return (0); /* success */
3917 dwc_otg_uninit(struct dwc_otg_softc *sc)
3919 USB_BUS_LOCK(&sc->sc_bus);
3921 /* stop host timer */
3922 dwc_otg_timer_stop(sc);
3924 /* set disconnect */
3925 DWC_OTG_WRITE_4(sc, DOTG_DCTL,
3928 /* turn off global IRQ */
3929 DWC_OTG_WRITE_4(sc, DOTG_GAHBCFG, 0);
3931 sc->sc_flags.port_enabled = 0;
3932 sc->sc_flags.port_powered = 0;
3933 sc->sc_flags.status_vbus = 0;
3934 sc->sc_flags.status_bus_reset = 0;
3935 sc->sc_flags.status_suspend = 0;
3936 sc->sc_flags.change_suspend = 0;
3937 sc->sc_flags.change_connect = 1;
3939 dwc_otg_pull_down(sc);
3940 dwc_otg_clocks_off(sc);
3942 USB_BUS_UNLOCK(&sc->sc_bus);
3944 usb_callout_drain(&sc->sc_timer);
3948 dwc_otg_suspend(struct dwc_otg_softc *sc)
3954 dwc_otg_resume(struct dwc_otg_softc *sc)
3960 dwc_otg_do_poll(struct usb_bus *bus)
3962 struct dwc_otg_softc *sc = DWC_OTG_BUS2SC(bus);
3964 USB_BUS_LOCK(&sc->sc_bus);
3965 USB_BUS_SPIN_LOCK(&sc->sc_bus);
3966 dwc_otg_interrupt_poll_locked(sc);
3967 dwc_otg_interrupt_complete_locked(sc);
3968 USB_BUS_SPIN_UNLOCK(&sc->sc_bus);
3969 USB_BUS_UNLOCK(&sc->sc_bus);
3972 /*------------------------------------------------------------------------*
3973 * DWC OTG bulk support
3974 * DWC OTG control support
3975 * DWC OTG interrupt support
3976 *------------------------------------------------------------------------*/
3978 dwc_otg_device_non_isoc_open(struct usb_xfer *xfer)
3983 dwc_otg_device_non_isoc_close(struct usb_xfer *xfer)
3985 dwc_otg_device_done(xfer, USB_ERR_CANCELLED);
3989 dwc_otg_device_non_isoc_enter(struct usb_xfer *xfer)
3994 dwc_otg_device_non_isoc_start(struct usb_xfer *xfer)
3997 dwc_otg_setup_standard_chain(xfer);
3998 dwc_otg_start_standard_chain(xfer);
4001 struct usb_pipe_methods dwc_otg_device_non_isoc_methods =
4003 .open = dwc_otg_device_non_isoc_open,
4004 .close = dwc_otg_device_non_isoc_close,
4005 .enter = dwc_otg_device_non_isoc_enter,
4006 .start = dwc_otg_device_non_isoc_start,
4009 /*------------------------------------------------------------------------*
4010 * DWC OTG full speed isochronous support
4011 *------------------------------------------------------------------------*/
4013 dwc_otg_device_isoc_open(struct usb_xfer *xfer)
4018 dwc_otg_device_isoc_close(struct usb_xfer *xfer)
4020 dwc_otg_device_done(xfer, USB_ERR_CANCELLED);
4024 dwc_otg_device_isoc_enter(struct usb_xfer *xfer)
4029 dwc_otg_device_isoc_start(struct usb_xfer *xfer)
4031 struct dwc_otg_softc *sc = DWC_OTG_BUS2SC(xfer->xroot->bus);
4035 uint8_t shift = usbd_xfer_get_fps_shift(xfer);
4037 DPRINTFN(6, "xfer=%p next=%d nframes=%d\n",
4038 xfer, xfer->endpoint->isoc_next, xfer->nframes);
4040 if (xfer->xroot->udev->flags.usb_mode == USB_MODE_HOST) {
4041 temp = DWC_OTG_READ_4(sc, DOTG_HFNUM);
4043 /* get the current frame index */
4044 framenum = (temp & HFNUM_FRNUM_MASK);
4046 temp = DWC_OTG_READ_4(sc, DOTG_DSTS);
4048 /* get the current frame index */
4049 framenum = DSTS_SOFFN_GET(temp);
4052 if (xfer->xroot->udev->parent_hs_hub != NULL)
4055 framenum &= DWC_OTG_FRAME_MASK;
4058 * Compute number of milliseconds worth of data traffic for
4059 * this USB transfer:
4061 if (xfer->xroot->udev->speed == USB_SPEED_HIGH)
4062 msframes = ((xfer->nframes << shift) + 7) / 8;
4064 msframes = xfer->nframes;
4067 * check if the frame index is within the window where the frames
4070 temp = (framenum - xfer->endpoint->isoc_next) & DWC_OTG_FRAME_MASK;
4072 if ((xfer->endpoint->is_synced == 0) || (temp < msframes)) {
4074 * If there is data underflow or the pipe queue is
4075 * empty we schedule the transfer a few frames ahead
4076 * of the current frame position. Else two isochronous
4077 * transfers might overlap.
4079 xfer->endpoint->isoc_next = (framenum + 3) & DWC_OTG_FRAME_MASK;
4080 xfer->endpoint->is_synced = 1;
4081 DPRINTFN(3, "start next=%d\n", xfer->endpoint->isoc_next);
4084 * compute how many milliseconds the insertion is ahead of the
4085 * current frame position:
4087 temp = (xfer->endpoint->isoc_next - framenum) & DWC_OTG_FRAME_MASK;
4090 * pre-compute when the isochronous transfer will be finished:
4092 xfer->isoc_time_complete =
4093 usb_isoc_time_expand(&sc->sc_bus, framenum) + temp + msframes;
4096 dwc_otg_setup_standard_chain(xfer);
4098 /* compute frame number for next insertion */
4099 xfer->endpoint->isoc_next += msframes;
4101 /* start TD chain */
4102 dwc_otg_start_standard_chain(xfer);
4105 struct usb_pipe_methods dwc_otg_device_isoc_methods =
4107 .open = dwc_otg_device_isoc_open,
4108 .close = dwc_otg_device_isoc_close,
4109 .enter = dwc_otg_device_isoc_enter,
4110 .start = dwc_otg_device_isoc_start,
4113 /*------------------------------------------------------------------------*
4114 * DWC OTG root control support
4115 *------------------------------------------------------------------------*
4116 * Simulate a hardware HUB by handling all the necessary requests.
4117 *------------------------------------------------------------------------*/
4119 static const struct usb_device_descriptor dwc_otg_devd = {
4120 .bLength = sizeof(struct usb_device_descriptor),
4121 .bDescriptorType = UDESC_DEVICE,
4122 .bcdUSB = {0x00, 0x02},
4123 .bDeviceClass = UDCLASS_HUB,
4124 .bDeviceSubClass = UDSUBCLASS_HUB,
4125 .bDeviceProtocol = UDPROTO_HSHUBSTT,
4126 .bMaxPacketSize = 64,
4127 .bcdDevice = {0x00, 0x01},
4130 .bNumConfigurations = 1,
4133 static const struct dwc_otg_config_desc dwc_otg_confd = {
4135 .bLength = sizeof(struct usb_config_descriptor),
4136 .bDescriptorType = UDESC_CONFIG,
4137 .wTotalLength[0] = sizeof(dwc_otg_confd),
4139 .bConfigurationValue = 1,
4140 .iConfiguration = 0,
4141 .bmAttributes = UC_SELF_POWERED,
4145 .bLength = sizeof(struct usb_interface_descriptor),
4146 .bDescriptorType = UDESC_INTERFACE,
4148 .bInterfaceClass = UICLASS_HUB,
4149 .bInterfaceSubClass = UISUBCLASS_HUB,
4150 .bInterfaceProtocol = 0,
4153 .bLength = sizeof(struct usb_endpoint_descriptor),
4154 .bDescriptorType = UDESC_ENDPOINT,
4155 .bEndpointAddress = (UE_DIR_IN | DWC_OTG_INTR_ENDPT),
4156 .bmAttributes = UE_INTERRUPT,
4157 .wMaxPacketSize[0] = 8,
4162 #define HSETW(ptr, val) ptr = { (uint8_t)(val), (uint8_t)((val) >> 8) }
4164 static const struct usb_hub_descriptor_min dwc_otg_hubd = {
4165 .bDescLength = sizeof(dwc_otg_hubd),
4166 .bDescriptorType = UDESC_HUB,
4168 HSETW(.wHubCharacteristics, (UHD_PWR_NO_SWITCH | UHD_OC_INDIVIDUAL)),
4169 .bPwrOn2PwrGood = 50,
4170 .bHubContrCurrent = 0,
4171 .DeviceRemovable = {0}, /* port is removable */
4174 #define STRING_VENDOR \
4177 #define STRING_PRODUCT \
4178 "O\0T\0G\0 \0R\0o\0o\0t\0 \0H\0U\0B"
4180 USB_MAKE_STRING_DESC(STRING_VENDOR, dwc_otg_vendor);
4181 USB_MAKE_STRING_DESC(STRING_PRODUCT, dwc_otg_product);
4184 dwc_otg_roothub_exec(struct usb_device *udev,
4185 struct usb_device_request *req, const void **pptr, uint16_t *plength)
4187 struct dwc_otg_softc *sc = DWC_OTG_BUS2SC(udev->bus);
4194 USB_BUS_LOCK_ASSERT(&sc->sc_bus, MA_OWNED);
4197 ptr = (const void *)&sc->sc_hub_temp;
4201 value = UGETW(req->wValue);
4202 index = UGETW(req->wIndex);
4204 /* demultiplex the control request */
4206 switch (req->bmRequestType) {
4207 case UT_READ_DEVICE:
4208 switch (req->bRequest) {
4209 case UR_GET_DESCRIPTOR:
4210 goto tr_handle_get_descriptor;
4212 goto tr_handle_get_config;
4214 goto tr_handle_get_status;
4220 case UT_WRITE_DEVICE:
4221 switch (req->bRequest) {
4222 case UR_SET_ADDRESS:
4223 goto tr_handle_set_address;
4225 goto tr_handle_set_config;
4226 case UR_CLEAR_FEATURE:
4227 goto tr_valid; /* nop */
4228 case UR_SET_DESCRIPTOR:
4229 goto tr_valid; /* nop */
4230 case UR_SET_FEATURE:
4236 case UT_WRITE_ENDPOINT:
4237 switch (req->bRequest) {
4238 case UR_CLEAR_FEATURE:
4239 switch (UGETW(req->wValue)) {
4240 case UF_ENDPOINT_HALT:
4241 goto tr_handle_clear_halt;
4242 case UF_DEVICE_REMOTE_WAKEUP:
4243 goto tr_handle_clear_wakeup;
4248 case UR_SET_FEATURE:
4249 switch (UGETW(req->wValue)) {
4250 case UF_ENDPOINT_HALT:
4251 goto tr_handle_set_halt;
4252 case UF_DEVICE_REMOTE_WAKEUP:
4253 goto tr_handle_set_wakeup;
4258 case UR_SYNCH_FRAME:
4259 goto tr_valid; /* nop */
4265 case UT_READ_ENDPOINT:
4266 switch (req->bRequest) {
4268 goto tr_handle_get_ep_status;
4274 case UT_WRITE_INTERFACE:
4275 switch (req->bRequest) {
4276 case UR_SET_INTERFACE:
4277 goto tr_handle_set_interface;
4278 case UR_CLEAR_FEATURE:
4279 goto tr_valid; /* nop */
4280 case UR_SET_FEATURE:
4286 case UT_READ_INTERFACE:
4287 switch (req->bRequest) {
4288 case UR_GET_INTERFACE:
4289 goto tr_handle_get_interface;
4291 goto tr_handle_get_iface_status;
4297 case UT_WRITE_CLASS_INTERFACE:
4298 case UT_WRITE_VENDOR_INTERFACE:
4302 case UT_READ_CLASS_INTERFACE:
4303 case UT_READ_VENDOR_INTERFACE:
4307 case UT_WRITE_CLASS_DEVICE:
4308 switch (req->bRequest) {
4309 case UR_CLEAR_FEATURE:
4311 case UR_SET_DESCRIPTOR:
4312 case UR_SET_FEATURE:
4319 case UT_WRITE_CLASS_OTHER:
4320 switch (req->bRequest) {
4321 case UR_CLEAR_FEATURE:
4322 goto tr_handle_clear_port_feature;
4323 case UR_SET_FEATURE:
4324 goto tr_handle_set_port_feature;
4325 case UR_CLEAR_TT_BUFFER:
4335 case UT_READ_CLASS_OTHER:
4336 switch (req->bRequest) {
4337 case UR_GET_TT_STATE:
4338 goto tr_handle_get_tt_state;
4340 goto tr_handle_get_port_status;
4346 case UT_READ_CLASS_DEVICE:
4347 switch (req->bRequest) {
4348 case UR_GET_DESCRIPTOR:
4349 goto tr_handle_get_class_descriptor;
4351 goto tr_handle_get_class_status;
4362 tr_handle_get_descriptor:
4363 switch (value >> 8) {
4368 len = sizeof(dwc_otg_devd);
4369 ptr = (const void *)&dwc_otg_devd;
4375 len = sizeof(dwc_otg_confd);
4376 ptr = (const void *)&dwc_otg_confd;
4379 switch (value & 0xff) {
4380 case 0: /* Language table */
4381 len = sizeof(usb_string_lang_en);
4382 ptr = (const void *)&usb_string_lang_en;
4385 case 1: /* Vendor */
4386 len = sizeof(dwc_otg_vendor);
4387 ptr = (const void *)&dwc_otg_vendor;
4390 case 2: /* Product */
4391 len = sizeof(dwc_otg_product);
4392 ptr = (const void *)&dwc_otg_product;
4403 tr_handle_get_config:
4405 sc->sc_hub_temp.wValue[0] = sc->sc_conf;
4408 tr_handle_get_status:
4410 USETW(sc->sc_hub_temp.wValue, UDS_SELF_POWERED);
4413 tr_handle_set_address:
4414 if (value & 0xFF00) {
4417 sc->sc_rt_addr = value;
4420 tr_handle_set_config:
4424 sc->sc_conf = value;
4427 tr_handle_get_interface:
4429 sc->sc_hub_temp.wValue[0] = 0;
4432 tr_handle_get_tt_state:
4433 tr_handle_get_class_status:
4434 tr_handle_get_iface_status:
4435 tr_handle_get_ep_status:
4437 USETW(sc->sc_hub_temp.wValue, 0);
4441 tr_handle_set_interface:
4442 tr_handle_set_wakeup:
4443 tr_handle_clear_wakeup:
4444 tr_handle_clear_halt:
4447 tr_handle_clear_port_feature:
4451 DPRINTFN(9, "UR_CLEAR_PORT_FEATURE on port %d\n", index);
4454 case UHF_PORT_SUSPEND:
4455 dwc_otg_wakeup_peer(sc);
4458 case UHF_PORT_ENABLE:
4459 if (sc->sc_flags.status_device_mode == 0) {
4460 DWC_OTG_WRITE_4(sc, DOTG_HPRT,
4461 sc->sc_hprt_val | HPRT_PRTENA);
4463 sc->sc_flags.port_enabled = 0;
4466 case UHF_C_PORT_RESET:
4467 sc->sc_flags.change_reset = 0;
4470 case UHF_C_PORT_ENABLE:
4471 sc->sc_flags.change_enabled = 0;
4474 case UHF_C_PORT_OVER_CURRENT:
4475 sc->sc_flags.change_over_current = 0;
4479 case UHF_PORT_INDICATOR:
4483 case UHF_PORT_POWER:
4484 sc->sc_flags.port_powered = 0;
4485 if (sc->sc_mode == DWC_MODE_HOST || sc->sc_mode == DWC_MODE_OTG) {
4486 sc->sc_hprt_val = 0;
4487 DWC_OTG_WRITE_4(sc, DOTG_HPRT, HPRT_PRTENA);
4489 dwc_otg_pull_down(sc);
4490 dwc_otg_clocks_off(sc);
4493 case UHF_C_PORT_CONNECTION:
4494 /* clear connect change flag */
4495 sc->sc_flags.change_connect = 0;
4498 case UHF_C_PORT_SUSPEND:
4499 sc->sc_flags.change_suspend = 0;
4503 err = USB_ERR_IOERROR;
4508 tr_handle_set_port_feature:
4512 DPRINTFN(9, "UR_SET_PORT_FEATURE\n");
4515 case UHF_PORT_ENABLE:
4518 case UHF_PORT_SUSPEND:
4519 if (sc->sc_flags.status_device_mode == 0) {
4520 /* set suspend BIT */
4521 sc->sc_hprt_val |= HPRT_PRTSUSP;
4522 DWC_OTG_WRITE_4(sc, DOTG_HPRT, sc->sc_hprt_val);
4524 /* generate HUB suspend event */
4525 dwc_otg_suspend_irq(sc);
4529 case UHF_PORT_RESET:
4530 if (sc->sc_flags.status_device_mode == 0) {
4532 DPRINTF("PORT RESET\n");
4534 /* enable PORT reset */
4535 DWC_OTG_WRITE_4(sc, DOTG_HPRT, sc->sc_hprt_val | HPRT_PRTRST);
4537 /* Wait 62.5ms for reset to complete */
4538 usb_pause_mtx(&sc->sc_bus.bus_mtx, hz / 16);
4540 DWC_OTG_WRITE_4(sc, DOTG_HPRT, sc->sc_hprt_val);
4542 /* Wait 62.5ms for reset to complete */
4543 usb_pause_mtx(&sc->sc_bus.bus_mtx, hz / 16);
4546 (void) dwc_otg_init_fifo(sc, DWC_MODE_HOST);
4548 sc->sc_flags.change_reset = 1;
4550 err = USB_ERR_IOERROR;
4555 case UHF_PORT_INDICATOR:
4558 case UHF_PORT_POWER:
4559 sc->sc_flags.port_powered = 1;
4560 if (sc->sc_mode == DWC_MODE_HOST || sc->sc_mode == DWC_MODE_OTG) {
4561 sc->sc_hprt_val |= HPRT_PRTPWR;
4562 DWC_OTG_WRITE_4(sc, DOTG_HPRT, sc->sc_hprt_val);
4564 if (sc->sc_mode == DWC_MODE_DEVICE || sc->sc_mode == DWC_MODE_OTG) {
4565 /* pull up D+, if any */
4566 dwc_otg_pull_up(sc);
4570 err = USB_ERR_IOERROR;
4575 tr_handle_get_port_status:
4577 DPRINTFN(9, "UR_GET_PORT_STATUS\n");
4582 if (sc->sc_flags.status_vbus)
4583 dwc_otg_clocks_on(sc);
4585 dwc_otg_clocks_off(sc);
4587 /* Select Device Side Mode */
4589 if (sc->sc_flags.status_device_mode) {
4590 value = UPS_PORT_MODE_DEVICE;
4591 dwc_otg_timer_stop(sc);
4594 dwc_otg_timer_start(sc);
4597 if (sc->sc_flags.status_high_speed)
4598 value |= UPS_HIGH_SPEED;
4599 else if (sc->sc_flags.status_low_speed)
4600 value |= UPS_LOW_SPEED;
4602 if (sc->sc_flags.port_powered)
4603 value |= UPS_PORT_POWER;
4605 if (sc->sc_flags.port_enabled)
4606 value |= UPS_PORT_ENABLED;
4608 if (sc->sc_flags.port_over_current)
4609 value |= UPS_OVERCURRENT_INDICATOR;
4611 if (sc->sc_flags.status_vbus &&
4612 sc->sc_flags.status_bus_reset)
4613 value |= UPS_CURRENT_CONNECT_STATUS;
4615 if (sc->sc_flags.status_suspend)
4616 value |= UPS_SUSPEND;
4618 USETW(sc->sc_hub_temp.ps.wPortStatus, value);
4622 if (sc->sc_flags.change_connect)
4623 value |= UPS_C_CONNECT_STATUS;
4624 if (sc->sc_flags.change_suspend)
4625 value |= UPS_C_SUSPEND;
4626 if (sc->sc_flags.change_reset)
4627 value |= UPS_C_PORT_RESET;
4628 if (sc->sc_flags.change_over_current)
4629 value |= UPS_C_OVERCURRENT_INDICATOR;
4631 USETW(sc->sc_hub_temp.ps.wPortChange, value);
4632 len = sizeof(sc->sc_hub_temp.ps);
4635 tr_handle_get_class_descriptor:
4639 ptr = (const void *)&dwc_otg_hubd;
4640 len = sizeof(dwc_otg_hubd);
4644 err = USB_ERR_STALLED;
4653 dwc_otg_xfer_setup(struct usb_setup_params *parm)
4655 struct usb_xfer *xfer;
4662 xfer = parm->curr_xfer;
4665 * NOTE: This driver does not use any of the parameters that
4666 * are computed from the following values. Just set some
4667 * reasonable dummies:
4669 parm->hc_max_packet_size = 0x500;
4670 parm->hc_max_packet_count = 3;
4671 parm->hc_max_frame_size = 3 * 0x500;
4673 usbd_transfer_setup_sub(parm);
4676 * compute maximum number of TDs
4678 ep_type = (xfer->endpoint->edesc->bmAttributes & UE_XFERTYPE);
4680 if (ep_type == UE_CONTROL) {
4682 ntd = xfer->nframes + 1 /* STATUS */ + 1 /* SYNC 1 */
4683 + 1 /* SYNC 2 */ + 1 /* SYNC 3 */;
4686 ntd = xfer->nframes + 1 /* SYNC */ ;
4690 * check if "usbd_transfer_setup_sub" set an error
4696 * allocate transfer descriptors
4700 ep_no = xfer->endpointno & UE_ADDR;
4703 * Check for a valid endpoint profile in USB device mode:
4705 if (xfer->flags_int.usb_mode == USB_MODE_DEVICE) {
4706 const struct usb_hw_ep_profile *pf;
4708 dwc_otg_get_hw_ep_profile(parm->udev, &pf, ep_no);
4711 /* should not happen */
4712 parm->err = USB_ERR_INVAL;
4718 parm->size[0] += ((-parm->size[0]) & (USB_HOST_ALIGN - 1));
4720 for (n = 0; n != ntd; n++) {
4722 struct dwc_otg_td *td;
4726 td = USB_ADD_BYTES(parm->buf, parm->size[0]);
4728 /* compute shared bandwidth resource index for TT */
4729 if (parm->udev->parent_hs_hub != NULL && parm->udev->speed != USB_SPEED_HIGH) {
4730 if (parm->udev->parent_hs_hub->ddesc.bDeviceProtocol == UDPROTO_HSHUBMTT)
4731 td->tt_index = parm->udev->device_index;
4733 td->tt_index = parm->udev->parent_hs_hub->device_index;
4735 td->tt_index = parm->udev->device_index;
4739 td->max_packet_size = xfer->max_packet_size;
4740 td->max_packet_count = xfer->max_packet_count;
4742 if (td->max_packet_count == 0 || td->max_packet_count > 3)
4743 td->max_packet_count = 1;
4745 td->ep_type = ep_type;
4746 td->obj_next = last_obj;
4750 parm->size[0] += sizeof(*td);
4753 xfer->td_start[0] = last_obj;
4757 dwc_otg_xfer_unsetup(struct usb_xfer *xfer)
4763 dwc_otg_ep_init(struct usb_device *udev, struct usb_endpoint_descriptor *edesc,
4764 struct usb_endpoint *ep)
4766 struct dwc_otg_softc *sc = DWC_OTG_BUS2SC(udev->bus);
4768 DPRINTFN(2, "endpoint=%p, addr=%d, endpt=%d, mode=%d (%d,%d)\n",
4770 edesc->bEndpointAddress, udev->flags.usb_mode,
4771 sc->sc_rt_addr, udev->device_index);
4773 if (udev->device_index != sc->sc_rt_addr) {
4775 if (udev->flags.usb_mode == USB_MODE_DEVICE) {
4776 if (udev->speed != USB_SPEED_FULL &&
4777 udev->speed != USB_SPEED_HIGH) {
4782 if (udev->speed == USB_SPEED_HIGH &&
4783 (edesc->wMaxPacketSize[1] & 0x18) != 0 &&
4784 (edesc->bmAttributes & UE_XFERTYPE) != UE_ISOCHRONOUS) {
4786 DPRINTFN(-1, "Non-isochronous high bandwidth "
4787 "endpoint not supported\n");
4791 if ((edesc->bmAttributes & UE_XFERTYPE) == UE_ISOCHRONOUS)
4792 ep->methods = &dwc_otg_device_isoc_methods;
4794 ep->methods = &dwc_otg_device_non_isoc_methods;
4799 dwc_otg_set_hw_power_sleep(struct usb_bus *bus, uint32_t state)
4801 struct dwc_otg_softc *sc = DWC_OTG_BUS2SC(bus);
4804 case USB_HW_POWER_SUSPEND:
4805 dwc_otg_suspend(sc);
4807 case USB_HW_POWER_SHUTDOWN:
4810 case USB_HW_POWER_RESUME:
4819 dwc_otg_get_dma_delay(struct usb_device *udev, uint32_t *pus)
4821 /* DMA delay - wait until any use of memory is finished */
4822 *pus = (2125); /* microseconds */
4826 dwc_otg_device_resume(struct usb_device *udev)
4830 /* poll all transfers again to restart resumed ones */
4831 dwc_otg_do_poll(udev->bus);
4835 dwc_otg_device_suspend(struct usb_device *udev)
4840 struct usb_bus_methods dwc_otg_bus_methods =
4842 .endpoint_init = &dwc_otg_ep_init,
4843 .xfer_setup = &dwc_otg_xfer_setup,
4844 .xfer_unsetup = &dwc_otg_xfer_unsetup,
4845 .get_hw_ep_profile = &dwc_otg_get_hw_ep_profile,
4846 .xfer_stall = &dwc_otg_xfer_stall,
4847 .set_stall = &dwc_otg_set_stall,
4848 .clear_stall = &dwc_otg_clear_stall,
4849 .roothub_exec = &dwc_otg_roothub_exec,
4850 .xfer_poll = &dwc_otg_do_poll,
4851 .device_state_change = &dwc_otg_device_state_change,
4852 .set_hw_power_sleep = &dwc_otg_set_hw_power_sleep,
4853 .get_dma_delay = &dwc_otg_get_dma_delay,
4854 .device_resume = &dwc_otg_device_resume,
4855 .device_suspend = &dwc_otg_device_suspend,