3 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
5 * Copyright (c) 2015 Daisuke Aoyama. All rights reserved.
6 * Copyright (c) 2012-2015 Hans Petter Selasky. All rights reserved.
7 * Copyright (c) 2010-2011 Aleksandr Rybalko. All rights reserved.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * This file contains the driver for the DesignWare series USB 2.0 OTG
37 * LIMITATION: Drivers must be bound to all OUT endpoints in the
38 * active configuration for this driver to work properly. Blocking any
39 * OUT endpoint will block all OUT endpoints including the control
40 * endpoint. Usually this is not a problem.
44 * NOTE: Writing to non-existing registers appears to cause an
48 #ifdef USB_GLOBAL_INCLUDE_FILE
49 #include USB_GLOBAL_INCLUDE_FILE
51 #include <sys/stdint.h>
52 #include <sys/stddef.h>
53 #include <sys/param.h>
54 #include <sys/queue.h>
55 #include <sys/types.h>
56 #include <sys/systm.h>
57 #include <sys/kernel.h>
59 #include <sys/module.h>
61 #include <sys/mutex.h>
62 #include <sys/condvar.h>
63 #include <sys/sysctl.h>
65 #include <sys/unistd.h>
66 #include <sys/callout.h>
67 #include <sys/malloc.h>
71 #include <dev/usb/usb.h>
72 #include <dev/usb/usbdi.h>
74 #define USB_DEBUG_VAR dwc_otg_debug
76 #include <dev/usb/usb_core.h>
77 #include <dev/usb/usb_debug.h>
78 #include <dev/usb/usb_busdma.h>
79 #include <dev/usb/usb_process.h>
80 #include <dev/usb/usb_transfer.h>
81 #include <dev/usb/usb_device.h>
82 #include <dev/usb/usb_hub.h>
83 #include <dev/usb/usb_util.h>
85 #include <dev/usb/usb_controller.h>
86 #include <dev/usb/usb_bus.h>
87 #endif /* USB_GLOBAL_INCLUDE_FILE */
89 #include <dev/usb/controller/dwc_otg.h>
90 #include <dev/usb/controller/dwc_otgreg.h>
92 #define DWC_OTG_BUS2SC(bus) \
93 ((struct dwc_otg_softc *)(((uint8_t *)(bus)) - \
94 ((uint8_t *)&(((struct dwc_otg_softc *)0)->sc_bus))))
96 #define DWC_OTG_PC2UDEV(pc) \
97 (USB_DMATAG_TO_XROOT((pc)->tag_parent)->udev)
99 #define DWC_OTG_MSK_GINT_THREAD_IRQ \
100 (GINTSTS_USBRST | GINTSTS_ENUMDONE | GINTSTS_PRTINT | \
101 GINTSTS_WKUPINT | GINTSTS_USBSUSP | GINTMSK_OTGINTMSK | \
104 #ifndef DWC_OTG_PHY_DEFAULT
105 #define DWC_OTG_PHY_DEFAULT DWC_OTG_PHY_ULPI
108 static int dwc_otg_phy_type = DWC_OTG_PHY_DEFAULT;
110 static SYSCTL_NODE(_hw_usb, OID_AUTO, dwc_otg, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
112 SYSCTL_INT(_hw_usb_dwc_otg, OID_AUTO, phy_type, CTLFLAG_RDTUN,
113 &dwc_otg_phy_type, 0, "DWC OTG PHY TYPE - 0/1/2/3 - ULPI/HSIC/INTERNAL/UTMI+");
116 static int dwc_otg_debug = 0;
118 SYSCTL_INT(_hw_usb_dwc_otg, OID_AUTO, debug, CTLFLAG_RWTUN,
119 &dwc_otg_debug, 0, "DWC OTG debug level");
122 #define DWC_OTG_INTR_ENDPT 1
126 static const struct usb_bus_methods dwc_otg_bus_methods;
127 static const struct usb_pipe_methods dwc_otg_device_non_isoc_methods;
128 static const struct usb_pipe_methods dwc_otg_device_isoc_methods;
130 static dwc_otg_cmd_t dwc_otg_setup_rx;
131 static dwc_otg_cmd_t dwc_otg_data_rx;
132 static dwc_otg_cmd_t dwc_otg_data_tx;
133 static dwc_otg_cmd_t dwc_otg_data_tx_sync;
135 static dwc_otg_cmd_t dwc_otg_host_setup_tx;
136 static dwc_otg_cmd_t dwc_otg_host_data_tx;
137 static dwc_otg_cmd_t dwc_otg_host_data_rx;
139 static void dwc_otg_device_done(struct usb_xfer *, usb_error_t);
140 static void dwc_otg_do_poll(struct usb_bus *);
141 static void dwc_otg_standard_done(struct usb_xfer *);
142 static void dwc_otg_root_intr(struct dwc_otg_softc *);
143 static void dwc_otg_interrupt_poll_locked(struct dwc_otg_softc *);
146 * Here is a configuration that the chip supports.
148 static const struct usb_hw_ep_profile dwc_otg_ep_profile[1] = {
151 .max_in_frame_size = 64,/* fixed */
152 .max_out_frame_size = 64, /* fixed */
154 .support_control = 1,
159 dwc_otg_get_hw_ep_profile(struct usb_device *udev,
160 const struct usb_hw_ep_profile **ppf, uint8_t ep_addr)
162 struct dwc_otg_softc *sc;
164 sc = DWC_OTG_BUS2SC(udev->bus);
166 if (ep_addr < sc->sc_dev_ep_max)
167 *ppf = &sc->sc_hw_ep_profile[ep_addr].usb;
173 dwc_otg_write_fifo(struct dwc_otg_softc *sc, struct usb_page_cache *pc,
174 uint32_t offset, uint32_t fifo, uint32_t count)
178 /* round down length to nearest 4-bytes */
181 /* check if we can write the data directly */
182 if (temp != 0 && usb_pc_buffer_is_aligned(pc, offset, temp, 3)) {
183 struct usb_page_search buf_res;
185 /* pre-subtract length */
188 /* iterate buffer list */
190 /* get current buffer pointer */
191 usbd_get_page(pc, offset, &buf_res);
193 if (buf_res.length > temp)
194 buf_res.length = temp;
196 /* transfer data into FIFO */
197 bus_space_write_region_4(sc->sc_io_tag, sc->sc_io_hdl,
198 fifo, buf_res.buffer, buf_res.length / 4);
200 offset += buf_res.length;
201 fifo += buf_res.length;
202 temp -= buf_res.length;
206 /* check for remainder */
208 /* clear topmost word before copy */
209 sc->sc_bounce_buffer[(count - 1) / 4] = 0;
212 usbd_copy_out(pc, offset,
213 sc->sc_bounce_buffer, count);
215 /* transfer data into FIFO */
216 bus_space_write_region_4(sc->sc_io_tag,
217 sc->sc_io_hdl, fifo, sc->sc_bounce_buffer,
223 dwc_otg_read_fifo(struct dwc_otg_softc *sc, struct usb_page_cache *pc,
224 uint32_t offset, uint32_t count)
228 /* round down length to nearest 4-bytes */
231 /* check if we can read the data directly */
232 if (temp != 0 && usb_pc_buffer_is_aligned(pc, offset, temp, 3)) {
233 struct usb_page_search buf_res;
235 /* pre-subtract length */
238 /* iterate buffer list */
240 /* get current buffer pointer */
241 usbd_get_page(pc, offset, &buf_res);
243 if (buf_res.length > temp)
244 buf_res.length = temp;
246 /* transfer data from FIFO */
247 bus_space_read_region_4(sc->sc_io_tag, sc->sc_io_hdl,
248 sc->sc_current_rx_fifo, buf_res.buffer, buf_res.length / 4);
250 offset += buf_res.length;
251 sc->sc_current_rx_fifo += buf_res.length;
252 sc->sc_current_rx_bytes -= buf_res.length;
253 temp -= buf_res.length;
257 /* check for remainder */
259 /* read data into bounce buffer */
260 bus_space_read_region_4(sc->sc_io_tag, sc->sc_io_hdl,
261 sc->sc_current_rx_fifo,
262 sc->sc_bounce_buffer, (count + 3) / 4);
264 /* store data into proper buffer */
265 usbd_copy_in(pc, offset, sc->sc_bounce_buffer, count);
267 /* round length up to nearest 4 bytes */
268 count = (count + 3) & ~3;
270 /* update counters */
271 sc->sc_current_rx_bytes -= count;
272 sc->sc_current_rx_fifo += count;
277 dwc_otg_tx_fifo_reset(struct dwc_otg_softc *sc, uint32_t value)
282 DWC_OTG_WRITE_4(sc, DOTG_GRSTCTL, value);
284 /* wait for reset to complete */
285 for (temp = 0; temp != 16; temp++) {
286 value = DWC_OTG_READ_4(sc, DOTG_GRSTCTL);
287 if (!(value & (GRSTCTL_TXFFLSH | GRSTCTL_RXFFLSH)))
293 dwc_otg_init_fifo(struct dwc_otg_softc *sc, uint8_t mode)
295 struct dwc_otg_profile *pf;
301 fifo_size = sc->sc_fifo_size;
304 * NOTE: Reserved fixed size area at end of RAM, which must
305 * not be allocated to the FIFOs:
309 if (fifo_size < fifo_regs) {
310 DPRINTF("Too little FIFO\n");
314 /* subtract FIFO regs from total once */
315 fifo_size -= fifo_regs;
317 /* split equally for IN and OUT */
320 /* Align to 4 bytes boundary (refer to PGM) */
323 /* set global receive FIFO size */
324 DWC_OTG_WRITE_4(sc, DOTG_GRXFSIZ, fifo_size / 4);
326 tx_start = fifo_size;
328 if (fifo_size < 64) {
329 DPRINTFN(-1, "Not enough data space for EP0 FIFO.\n");
333 if (mode == DWC_MODE_HOST) {
335 /* reset active endpoints */
336 sc->sc_active_rx_ep = 0;
338 /* split equally for periodic and non-periodic */
341 DPRINTF("PTX/NPTX FIFO=%u\n", fifo_size);
343 /* align to 4 bytes boundary */
346 DWC_OTG_WRITE_4(sc, DOTG_GNPTXFSIZ,
347 ((fifo_size / 4) << 16) |
350 tx_start += fifo_size;
352 for (x = 0; x != sc->sc_host_ch_max; x++) {
353 /* enable all host interrupts */
354 DWC_OTG_WRITE_4(sc, DOTG_HCINTMSK(x),
358 DWC_OTG_WRITE_4(sc, DOTG_HPTXFSIZ,
359 ((fifo_size / 4) << 16) |
362 /* reset host channel state */
363 memset(sc->sc_chan_state, 0, sizeof(sc->sc_chan_state));
365 /* enable all host channel interrupts */
366 DWC_OTG_WRITE_4(sc, DOTG_HAINTMSK,
367 (1U << sc->sc_host_ch_max) - 1U);
369 /* enable proper host channel interrupts */
370 sc->sc_irq_mask |= GINTMSK_HCHINTMSK;
371 sc->sc_irq_mask &= ~GINTMSK_IEPINTMSK;
372 DWC_OTG_WRITE_4(sc, DOTG_GINTMSK, sc->sc_irq_mask);
375 if (mode == DWC_MODE_DEVICE) {
377 DWC_OTG_WRITE_4(sc, DOTG_GNPTXFSIZ,
378 (0x10 << 16) | (tx_start / 4));
382 /* setup control endpoint profile */
383 sc->sc_hw_ep_profile[0].usb = dwc_otg_ep_profile[0];
385 /* reset active endpoints */
386 sc->sc_active_rx_ep = 1;
388 for (x = 1; x != sc->sc_dev_ep_max; x++) {
390 pf = sc->sc_hw_ep_profile + x;
392 pf->usb.max_out_frame_size = 1024 * 3;
393 pf->usb.is_simplex = 0; /* assume duplex */
394 pf->usb.support_bulk = 1;
395 pf->usb.support_interrupt = 1;
396 pf->usb.support_isochronous = 1;
397 pf->usb.support_out = 1;
399 if (x < sc->sc_dev_in_ep_max) {
402 limit = (x == 1) ? MIN(DWC_OTG_TX_MAX_FIFO_SIZE,
403 DWC_OTG_MAX_TXN) : MIN(DWC_OTG_MAX_TXN / 2,
404 DWC_OTG_TX_MAX_FIFO_SIZE);
406 /* see if there is enough FIFO space */
407 if (limit <= fifo_size) {
408 pf->max_buffer = limit;
409 pf->usb.support_in = 1;
411 limit = MIN(DWC_OTG_TX_MAX_FIFO_SIZE, 0x40);
412 if (limit <= fifo_size) {
413 pf->usb.support_in = 1;
415 pf->usb.is_simplex = 1;
420 DWC_OTG_WRITE_4(sc, DOTG_DIEPTXF(x),
421 ((limit / 4) << 16) | (tx_start / 4));
424 pf->usb.max_in_frame_size = limit;
426 pf->usb.is_simplex = 1;
429 DPRINTF("FIFO%d = IN:%d / OUT:%d\n", x,
430 pf->usb.max_in_frame_size,
431 pf->usb.max_out_frame_size);
434 /* enable proper device channel interrupts */
435 sc->sc_irq_mask &= ~GINTMSK_HCHINTMSK;
436 sc->sc_irq_mask |= GINTMSK_IEPINTMSK;
437 DWC_OTG_WRITE_4(sc, DOTG_GINTMSK, sc->sc_irq_mask);
441 dwc_otg_tx_fifo_reset(sc, GRSTCTL_RXFFLSH);
443 if (mode != DWC_MODE_OTG) {
444 /* reset all TX FIFOs */
445 dwc_otg_tx_fifo_reset(sc,
446 GRSTCTL_TXFIFO(0x10) |
449 /* reset active endpoints */
450 sc->sc_active_rx_ep = 0;
452 /* reset host channel state */
453 memset(sc->sc_chan_state, 0, sizeof(sc->sc_chan_state));
459 dwc_otg_uses_split(struct usb_device *udev)
462 * When a LOW or FULL speed device is connected directly to
463 * the USB port we don't use split transactions:
465 return (udev->speed != USB_SPEED_HIGH &&
466 udev->parent_hs_hub != NULL &&
467 udev->parent_hs_hub->parent_hub != NULL);
471 dwc_otg_update_host_frame_interval(struct dwc_otg_softc *sc)
475 * Disabled until further. Assuming that the register is already
476 * programmed correctly by the boot loader.
481 /* setup HOST frame interval register, based on existing value */
482 temp = DWC_OTG_READ_4(sc, DOTG_HFIR) & HFIR_FRINT_MASK;
488 /* figure out nearest X-tal value */
496 if (sc->sc_flags.status_high_speed)
501 DPRINTF("HFIR=0x%08x\n", temp);
503 DWC_OTG_WRITE_4(sc, DOTG_HFIR, temp);
508 dwc_otg_clocks_on(struct dwc_otg_softc *sc)
510 if (sc->sc_flags.clocks_off &&
511 sc->sc_flags.port_powered) {
515 /* TODO - platform specific */
517 sc->sc_flags.clocks_off = 0;
522 dwc_otg_clocks_off(struct dwc_otg_softc *sc)
524 if (!sc->sc_flags.clocks_off) {
528 /* TODO - platform specific */
530 sc->sc_flags.clocks_off = 1;
535 dwc_otg_pull_up(struct dwc_otg_softc *sc)
539 /* pullup D+, if possible */
541 if (!sc->sc_flags.d_pulled_up &&
542 sc->sc_flags.port_powered) {
543 sc->sc_flags.d_pulled_up = 1;
545 temp = DWC_OTG_READ_4(sc, DOTG_DCTL);
546 temp &= ~DCTL_SFTDISCON;
547 DWC_OTG_WRITE_4(sc, DOTG_DCTL, temp);
552 dwc_otg_pull_down(struct dwc_otg_softc *sc)
556 /* pulldown D+, if possible */
558 if (sc->sc_flags.d_pulled_up) {
559 sc->sc_flags.d_pulled_up = 0;
561 temp = DWC_OTG_READ_4(sc, DOTG_DCTL);
562 temp |= DCTL_SFTDISCON;
563 DWC_OTG_WRITE_4(sc, DOTG_DCTL, temp);
568 dwc_otg_enable_sof_irq(struct dwc_otg_softc *sc)
570 /* In device mode we don't use the SOF interrupt */
571 if (sc->sc_flags.status_device_mode != 0)
573 /* Ensure the SOF interrupt is not disabled */
575 /* Check if the SOF interrupt is already enabled */
576 if ((sc->sc_irq_mask & GINTMSK_SOFMSK) != 0)
578 sc->sc_irq_mask |= GINTMSK_SOFMSK;
579 DWC_OTG_WRITE_4(sc, DOTG_GINTMSK, sc->sc_irq_mask);
583 dwc_otg_resume_irq(struct dwc_otg_softc *sc)
585 if (sc->sc_flags.status_suspend) {
586 /* update status bits */
587 sc->sc_flags.status_suspend = 0;
588 sc->sc_flags.change_suspend = 1;
590 if (sc->sc_flags.status_device_mode) {
592 * Disable resume interrupt and enable suspend
595 sc->sc_irq_mask &= ~GINTMSK_WKUPINTMSK;
596 sc->sc_irq_mask |= GINTMSK_USBSUSPMSK;
597 DWC_OTG_WRITE_4(sc, DOTG_GINTMSK, sc->sc_irq_mask);
600 /* complete root HUB interrupt endpoint */
601 dwc_otg_root_intr(sc);
606 dwc_otg_suspend_irq(struct dwc_otg_softc *sc)
608 if (!sc->sc_flags.status_suspend) {
609 /* update status bits */
610 sc->sc_flags.status_suspend = 1;
611 sc->sc_flags.change_suspend = 1;
613 if (sc->sc_flags.status_device_mode) {
615 * Disable suspend interrupt and enable resume
618 sc->sc_irq_mask &= ~GINTMSK_USBSUSPMSK;
619 sc->sc_irq_mask |= GINTMSK_WKUPINTMSK;
620 DWC_OTG_WRITE_4(sc, DOTG_GINTMSK, sc->sc_irq_mask);
623 /* complete root HUB interrupt endpoint */
624 dwc_otg_root_intr(sc);
629 dwc_otg_wakeup_peer(struct dwc_otg_softc *sc)
631 if (!sc->sc_flags.status_suspend)
634 DPRINTFN(5, "Remote wakeup\n");
636 if (sc->sc_flags.status_device_mode) {
639 /* enable remote wakeup signalling */
640 temp = DWC_OTG_READ_4(sc, DOTG_DCTL);
641 temp |= DCTL_RMTWKUPSIG;
642 DWC_OTG_WRITE_4(sc, DOTG_DCTL, temp);
644 /* Wait 8ms for remote wakeup to complete. */
645 usb_pause_mtx(&sc->sc_bus.bus_mtx, hz / 125);
647 temp &= ~DCTL_RMTWKUPSIG;
648 DWC_OTG_WRITE_4(sc, DOTG_DCTL, temp);
650 /* enable USB port */
651 DWC_OTG_WRITE_4(sc, DOTG_PCGCCTL, 0);
654 usb_pause_mtx(&sc->sc_bus.bus_mtx, hz / 100);
657 sc->sc_hprt_val |= HPRT_PRTRES;
658 DWC_OTG_WRITE_4(sc, DOTG_HPRT, sc->sc_hprt_val);
660 /* Wait 100ms for resume signalling to complete. */
661 usb_pause_mtx(&sc->sc_bus.bus_mtx, hz / 10);
663 /* clear suspend and resume */
664 sc->sc_hprt_val &= ~(HPRT_PRTSUSP | HPRT_PRTRES);
665 DWC_OTG_WRITE_4(sc, DOTG_HPRT, sc->sc_hprt_val);
668 usb_pause_mtx(&sc->sc_bus.bus_mtx, hz / 250);
671 /* need to fake resume IRQ */
672 dwc_otg_resume_irq(sc);
676 dwc_otg_set_address(struct dwc_otg_softc *sc, uint8_t addr)
680 DPRINTFN(5, "addr=%d\n", addr);
682 temp = DWC_OTG_READ_4(sc, DOTG_DCFG);
683 temp &= ~DCFG_DEVADDR_SET(0x7F);
684 temp |= DCFG_DEVADDR_SET(addr);
685 DWC_OTG_WRITE_4(sc, DOTG_DCFG, temp);
689 dwc_otg_common_rx_ack(struct dwc_otg_softc *sc)
691 DPRINTFN(5, "RX status clear\n");
693 /* enable RX FIFO level interrupt */
694 sc->sc_irq_mask |= GINTMSK_RXFLVLMSK;
695 DWC_OTG_WRITE_4(sc, DOTG_GINTMSK, sc->sc_irq_mask);
697 if (sc->sc_current_rx_bytes != 0) {
698 /* need to dump remaining data */
699 bus_space_read_region_4(sc->sc_io_tag, sc->sc_io_hdl,
700 sc->sc_current_rx_fifo, sc->sc_bounce_buffer,
701 sc->sc_current_rx_bytes / 4);
702 /* clear number of active bytes to receive */
703 sc->sc_current_rx_bytes = 0;
705 /* clear cached status */
706 sc->sc_last_rx_status = 0;
710 dwc_otg_clear_hcint(struct dwc_otg_softc *sc, uint8_t x)
714 /* clear all pending interrupts */
715 hcint = DWC_OTG_READ_4(sc, DOTG_HCINT(x));
716 DWC_OTG_WRITE_4(sc, DOTG_HCINT(x), hcint);
718 /* clear buffered interrupts */
719 sc->sc_chan_state[x].hcint = 0;
723 dwc_otg_host_check_tx_fifo_empty(struct dwc_otg_softc *sc, struct dwc_otg_td *td)
727 temp = DWC_OTG_READ_4(sc, DOTG_GINTSTS);
729 if (td->ep_type == UE_ISOCHRONOUS) {
731 * NOTE: USB INTERRUPT transactions are executed like
732 * USB CONTROL transactions! See the setup standard
733 * chain function for more information.
735 if (!(temp & GINTSTS_PTXFEMP)) {
736 DPRINTF("Periodic TX FIFO is not empty\n");
737 if (!(sc->sc_irq_mask & GINTMSK_PTXFEMPMSK)) {
738 sc->sc_irq_mask |= GINTMSK_PTXFEMPMSK;
739 DWC_OTG_WRITE_4(sc, DOTG_GINTMSK, sc->sc_irq_mask);
741 return (1); /* busy */
744 if (!(temp & GINTSTS_NPTXFEMP)) {
745 DPRINTF("Non-periodic TX FIFO is not empty\n");
746 if (!(sc->sc_irq_mask & GINTMSK_NPTXFEMPMSK)) {
747 sc->sc_irq_mask |= GINTMSK_NPTXFEMPMSK;
748 DWC_OTG_WRITE_4(sc, DOTG_GINTMSK, sc->sc_irq_mask);
750 return (1); /* busy */
753 return (0); /* ready for transmit */
757 dwc_otg_host_channel_alloc(struct dwc_otg_softc *sc,
758 struct dwc_otg_td *td, uint8_t is_out)
764 if (td->channel[0] < DWC_OTG_MAX_CHANNELS)
765 return (0); /* already allocated */
767 /* check if device is suspended */
768 if (DWC_OTG_PC2UDEV(td->pc)->flags.self_suspended != 0)
769 return (1); /* busy - cannot transfer data */
771 /* compute needed TX FIFO size */
773 if (dwc_otg_host_check_tx_fifo_empty(sc, td) != 0)
774 return (1); /* busy - cannot transfer data */
776 z = td->max_packet_count;
777 for (x = y = 0; x != sc->sc_host_ch_max; x++) {
778 /* check if channel is allocated */
779 if (sc->sc_chan_state[x].allocated != 0)
781 /* check if channel is still enabled */
782 if (sc->sc_chan_state[x].wait_halted != 0)
784 /* store channel number */
785 td->channel[y++] = x;
786 /* check if we got all channels */
791 /* reset channel variable */
792 td->channel[0] = DWC_OTG_MAX_CHANNELS;
793 td->channel[1] = DWC_OTG_MAX_CHANNELS;
794 td->channel[2] = DWC_OTG_MAX_CHANNELS;
796 dwc_otg_enable_sof_irq(sc);
797 return (1); /* busy - not enough channels */
800 for (y = 0; y != z; y++) {
804 sc->sc_chan_state[x].allocated = 1;
806 /* set wait halted */
807 sc->sc_chan_state[x].wait_halted = 1;
809 /* clear interrupts */
810 dwc_otg_clear_hcint(sc, x);
812 DPRINTF("CH=%d HCCHAR=0x%08x "
813 "HCSPLT=0x%08x\n", x, td->hcchar, td->hcsplt);
815 /* set active channel */
816 sc->sc_active_rx_ep |= (1 << x);
818 return (0); /* allocated */
822 dwc_otg_host_channel_free_sub(struct dwc_otg_softc *sc, struct dwc_otg_td *td, uint8_t index)
827 if (td->channel[index] >= DWC_OTG_MAX_CHANNELS)
828 return; /* already freed */
831 x = td->channel[index];
832 td->channel[index] = DWC_OTG_MAX_CHANNELS;
834 DPRINTF("CH=%d\n", x);
837 * We need to let programmed host channels run till complete
838 * else the host channel will stop functioning.
840 sc->sc_chan_state[x].allocated = 0;
842 /* ack any pending messages */
843 if (sc->sc_last_rx_status != 0 &&
844 GRXSTSRD_CHNUM_GET(sc->sc_last_rx_status) == x) {
845 dwc_otg_common_rx_ack(sc);
848 /* clear active channel */
849 sc->sc_active_rx_ep &= ~(1 << x);
851 /* check if already halted */
852 if (sc->sc_chan_state[x].wait_halted == 0)
855 /* disable host channel */
856 hcchar = DWC_OTG_READ_4(sc, DOTG_HCCHAR(x));
857 if (hcchar & HCCHAR_CHENA) {
858 DPRINTF("Halting channel %d\n", x);
859 DWC_OTG_WRITE_4(sc, DOTG_HCCHAR(x),
860 hcchar | HCCHAR_CHDIS);
861 /* don't write HCCHAR until the channel is halted */
863 sc->sc_chan_state[x].wait_halted = 0;
868 dwc_otg_host_channel_free(struct dwc_otg_softc *sc, struct dwc_otg_td *td)
871 for (x = 0; x != td->max_packet_count; x++)
872 dwc_otg_host_channel_free_sub(sc, td, x);
876 dwc_otg_host_dump_rx(struct dwc_otg_softc *sc, struct dwc_otg_td *td)
879 /* dump any pending messages */
880 if (sc->sc_last_rx_status == 0)
882 for (x = 0; x != td->max_packet_count; x++) {
883 if (td->channel[x] >= DWC_OTG_MAX_CHANNELS ||
884 td->channel[x] != GRXSTSRD_CHNUM_GET(sc->sc_last_rx_status))
886 dwc_otg_common_rx_ack(sc);
892 dwc_otg_host_setup_tx(struct dwc_otg_softc *sc, struct dwc_otg_td *td)
894 struct usb_device_request req __aligned(4);
899 dwc_otg_host_dump_rx(sc, td);
901 if (td->channel[0] < DWC_OTG_MAX_CHANNELS) {
902 hcint = sc->sc_chan_state[td->channel[0]].hcint;
904 DPRINTF("CH=%d ST=%d HCINT=0x%08x HCCHAR=0x%08x HCTSIZ=0x%08x\n",
905 td->channel[0], td->state, hcint,
906 DWC_OTG_READ_4(sc, DOTG_HCCHAR(td->channel[0])),
907 DWC_OTG_READ_4(sc, DOTG_HCTSIZ(td->channel[0])));
913 if (hcint & (HCINT_RETRY |
914 HCINT_ACK | HCINT_NYET)) {
915 /* give success bits priority over failure bits */
916 } else if (hcint & HCINT_STALL) {
917 DPRINTF("CH=%d STALL\n", td->channel[0]);
921 } else if (hcint & HCINT_ERRORS) {
922 DPRINTF("CH=%d ERROR\n", td->channel[0]);
924 if (td->hcsplt != 0 || td->errcnt >= 3) {
930 if (hcint & (HCINT_ERRORS | HCINT_RETRY |
931 HCINT_ACK | HCINT_NYET)) {
932 if (!(hcint & HCINT_ERRORS))
938 case DWC_CHAN_ST_START:
941 case DWC_CHAN_ST_WAIT_ANE:
942 if (hcint & (HCINT_RETRY | HCINT_ERRORS)) {
944 td->tt_scheduled = 0;
946 } else if (hcint & (HCINT_ACK | HCINT_NYET)) {
947 td->offset += td->tx_bytes;
948 td->remainder -= td->tx_bytes;
950 td->tt_scheduled = 0;
955 case DWC_CHAN_ST_WAIT_S_ANE:
956 if (hcint & (HCINT_RETRY | HCINT_ERRORS)) {
958 td->tt_scheduled = 0;
960 } else if (hcint & (HCINT_ACK | HCINT_NYET)) {
965 case DWC_CHAN_ST_WAIT_C_ANE:
966 if (hcint & HCINT_NYET) {
968 } else if (hcint & (HCINT_RETRY | HCINT_ERRORS)) {
970 td->tt_scheduled = 0;
972 } else if (hcint & HCINT_ACK) {
973 td->offset += td->tx_bytes;
974 td->remainder -= td->tx_bytes;
980 case DWC_CHAN_ST_WAIT_C_PKT:
989 /* free existing channel, if any */
990 dwc_otg_host_channel_free(sc, td);
992 if (sizeof(req) != td->remainder) {
997 if (td->hcsplt != 0) {
998 delta = td->tt_start_slot - sc->sc_last_frame_num - 1;
999 if (td->tt_scheduled == 0 || delta < DWC_OTG_TT_SLOT_MAX) {
1000 td->state = DWC_CHAN_ST_START;
1003 delta = sc->sc_last_frame_num - td->tt_start_slot;
1006 td->tt_scheduled = 0;
1007 td->state = DWC_CHAN_ST_START;
1012 /* allocate a new channel */
1013 if (dwc_otg_host_channel_alloc(sc, td, 1)) {
1014 td->state = DWC_CHAN_ST_START;
1018 if (td->hcsplt != 0) {
1019 td->hcsplt &= ~HCSPLT_COMPSPLT;
1020 td->state = DWC_CHAN_ST_WAIT_S_ANE;
1022 td->state = DWC_CHAN_ST_WAIT_ANE;
1025 /* copy out control request */
1026 usbd_copy_out(td->pc, 0, &req, sizeof(req));
1028 DWC_OTG_WRITE_4(sc, DOTG_HCTSIZ(td->channel[0]),
1029 (sizeof(req) << HCTSIZ_XFERSIZE_SHIFT) |
1030 (1 << HCTSIZ_PKTCNT_SHIFT) |
1031 (HCTSIZ_PID_SETUP << HCTSIZ_PID_SHIFT));
1033 DWC_OTG_WRITE_4(sc, DOTG_HCSPLT(td->channel[0]), td->hcsplt);
1035 hcchar = td->hcchar;
1036 hcchar &= ~(HCCHAR_EPDIR_IN | HCCHAR_EPTYPE_MASK);
1037 hcchar |= UE_CONTROL << HCCHAR_EPTYPE_SHIFT;
1039 /* must enable channel before writing data to FIFO */
1040 DWC_OTG_WRITE_4(sc, DOTG_HCCHAR(td->channel[0]), hcchar);
1042 /* transfer data into FIFO */
1043 bus_space_write_region_4(sc->sc_io_tag, sc->sc_io_hdl,
1044 DOTG_DFIFO(td->channel[0]), (uint32_t *)&req, sizeof(req) / 4);
1046 /* wait until next slot before trying complete split */
1047 td->tt_complete_slot = sc->sc_last_frame_num + 1;
1049 /* store number of bytes transmitted */
1050 td->tx_bytes = sizeof(req);
1054 /* free existing channel, if any */
1055 dwc_otg_host_channel_free(sc, td);
1057 delta = td->tt_complete_slot - sc->sc_last_frame_num - 1;
1058 if (td->tt_scheduled == 0 || delta < DWC_OTG_TT_SLOT_MAX) {
1059 td->state = DWC_CHAN_ST_WAIT_C_PKT;
1062 delta = sc->sc_last_frame_num - td->tt_start_slot;
1063 if (delta > DWC_OTG_TT_SLOT_MAX) {
1064 /* we missed the service interval */
1065 if (td->ep_type != UE_ISOCHRONOUS)
1069 /* allocate a new channel */
1070 if (dwc_otg_host_channel_alloc(sc, td, 0)) {
1071 td->state = DWC_CHAN_ST_WAIT_C_PKT;
1075 /* wait until next slot before trying complete split */
1076 td->tt_complete_slot = sc->sc_last_frame_num + 1;
1078 td->hcsplt |= HCSPLT_COMPSPLT;
1079 td->state = DWC_CHAN_ST_WAIT_C_ANE;
1081 DWC_OTG_WRITE_4(sc, DOTG_HCTSIZ(td->channel[0]),
1082 (HCTSIZ_PID_SETUP << HCTSIZ_PID_SHIFT));
1084 DWC_OTG_WRITE_4(sc, DOTG_HCSPLT(td->channel[0]), td->hcsplt);
1086 hcchar = td->hcchar;
1087 hcchar &= ~(HCCHAR_EPDIR_IN | HCCHAR_EPTYPE_MASK);
1088 hcchar |= UE_CONTROL << HCCHAR_EPTYPE_SHIFT;
1090 /* must enable channel before writing data to FIFO */
1091 DWC_OTG_WRITE_4(sc, DOTG_HCCHAR(td->channel[0]), hcchar);
1094 return (1); /* busy */
1097 dwc_otg_host_channel_free(sc, td);
1098 return (0); /* complete */
1102 dwc_otg_setup_rx(struct dwc_otg_softc *sc, struct dwc_otg_td *td)
1104 struct usb_device_request req __aligned(4);
1108 /* check endpoint status */
1110 if (sc->sc_last_rx_status == 0)
1113 if (GRXSTSRD_CHNUM_GET(sc->sc_last_rx_status) != 0)
1116 if ((sc->sc_last_rx_status & GRXSTSRD_PKTSTS_MASK) !=
1117 GRXSTSRD_STP_DATA) {
1118 if ((sc->sc_last_rx_status & GRXSTSRD_PKTSTS_MASK) !=
1119 GRXSTSRD_STP_COMPLETE || td->remainder != 0) {
1121 dwc_otg_common_rx_ack(sc);
1125 dwc_otg_common_rx_ack(sc);
1126 return (0); /* complete */
1129 if ((sc->sc_last_rx_status & GRXSTSRD_DPID_MASK) !=
1130 GRXSTSRD_DPID_DATA0) {
1132 dwc_otg_common_rx_ack(sc);
1136 DPRINTFN(5, "GRXSTSR=0x%08x\n", sc->sc_last_rx_status);
1138 /* clear did stall */
1141 /* get the packet byte count */
1142 count = GRXSTSRD_BCNT_GET(sc->sc_last_rx_status);
1144 if (count != sizeof(req)) {
1145 DPRINTFN(0, "Unsupported SETUP packet "
1146 "length, %d bytes\n", count);
1148 dwc_otg_common_rx_ack(sc);
1153 dwc_otg_read_fifo(sc, td->pc, 0, sizeof(req));
1155 /* copy out control request */
1156 usbd_copy_out(td->pc, 0, &req, sizeof(req));
1158 td->offset = sizeof(req);
1161 /* sneak peek the set address */
1162 if ((req.bmRequestType == UT_WRITE_DEVICE) &&
1163 (req.bRequest == UR_SET_ADDRESS)) {
1164 /* must write address before ZLP */
1165 dwc_otg_set_address(sc, req.wValue[0] & 0x7F);
1168 /* don't send any data by default */
1169 DWC_OTG_WRITE_4(sc, DOTG_DIEPTSIZ(0), DIEPCTL_EPDIS);
1170 DWC_OTG_WRITE_4(sc, DOTG_DOEPTSIZ(0), DOEPCTL_EPDIS);
1172 /* reset IN endpoint buffer */
1173 dwc_otg_tx_fifo_reset(sc,
1177 /* acknowledge RX status */
1178 dwc_otg_common_rx_ack(sc);
1182 /* abort any ongoing transfer, before enabling again */
1183 if (!td->did_stall) {
1186 DPRINTFN(5, "stalling IN and OUT direction\n");
1188 temp = sc->sc_out_ctl[0];
1190 /* set stall after enabling endpoint */
1191 DWC_OTG_WRITE_4(sc, DOTG_DOEPCTL(0),
1192 temp | DOEPCTL_STALL);
1194 temp = sc->sc_in_ctl[0];
1196 /* set stall assuming endpoint is enabled */
1197 DWC_OTG_WRITE_4(sc, DOTG_DIEPCTL(0),
1198 temp | DIEPCTL_STALL);
1200 return (1); /* not complete */
1204 dwc_otg_host_rate_check_interrupt(struct dwc_otg_softc *sc, struct dwc_otg_td *td)
1208 delta = sc->sc_tmr_val - td->tmr_val;
1210 return (1); /* busy */
1212 td->tmr_val = sc->sc_tmr_val + td->tmr_res;
1214 /* set toggle, if any */
1215 if (td->set_toggle) {
1223 dwc_otg_host_rate_check(struct dwc_otg_softc *sc, struct dwc_otg_td *td)
1225 uint8_t frame_num = (uint8_t)sc->sc_last_frame_num;
1227 if (td->ep_type == UE_ISOCHRONOUS) {
1228 /* non TT isochronous traffic */
1229 if (frame_num & (td->tmr_res - 1))
1231 if ((frame_num ^ td->tmr_val) & td->tmr_res)
1233 td->tmr_val = td->tmr_res + sc->sc_last_frame_num;
1236 } else if (td->ep_type == UE_INTERRUPT) {
1237 if (!td->tt_scheduled)
1239 td->tt_scheduled = 0;
1241 } else if (td->did_nak != 0) {
1242 /* check if we should pause sending queries for 125us */
1243 if (td->tmr_res == frame_num) {
1245 dwc_otg_enable_sof_irq(sc);
1248 } else if (td->set_toggle) {
1252 /* query for data one more time */
1253 td->tmr_res = frame_num;
1261 dwc_otg_host_data_rx_sub(struct dwc_otg_softc *sc, struct dwc_otg_td *td,
1266 /* check endpoint status */
1267 if (sc->sc_last_rx_status == 0)
1270 if (channel >= DWC_OTG_MAX_CHANNELS)
1273 if (GRXSTSRD_CHNUM_GET(sc->sc_last_rx_status) != channel)
1276 switch (sc->sc_last_rx_status & GRXSTSRD_PKTSTS_MASK) {
1277 case GRXSTSRH_IN_DATA:
1279 DPRINTF("DATA ST=%d STATUS=0x%08x\n",
1280 (int)td->state, (int)sc->sc_last_rx_status);
1282 if (sc->sc_chan_state[channel].hcint & HCINT_SOFTWARE_ONLY) {
1284 * When using SPLIT transactions on interrupt
1285 * endpoints, sometimes data occurs twice.
1287 DPRINTF("Data already received\n");
1291 /* get the packet byte count */
1292 count = GRXSTSRD_BCNT_GET(sc->sc_last_rx_status);
1294 /* check for ISOCHRONOUS endpoint */
1295 if (td->ep_type == UE_ISOCHRONOUS) {
1296 if ((sc->sc_last_rx_status & GRXSTSRD_DPID_MASK) !=
1297 GRXSTSRD_DPID_DATA0) {
1298 /* more data to be received */
1299 td->tt_xactpos = HCSPLT_XACTPOS_MIDDLE;
1301 /* all data received */
1302 td->tt_xactpos = HCSPLT_XACTPOS_BEGIN;
1303 /* verify the packet byte count */
1304 if (count != td->remainder) {
1305 /* we have a short packet */
1311 /* verify the packet byte count */
1312 if (count != td->max_packet_size) {
1313 if (count < td->max_packet_size) {
1314 /* we have a short packet */
1318 /* invalid USB packet */
1322 dwc_otg_common_rx_ack(sc);
1327 td->tt_scheduled = 0;
1330 /* verify the packet byte count */
1331 if (count > td->remainder) {
1332 /* invalid USB packet */
1336 dwc_otg_common_rx_ack(sc);
1340 /* read data from FIFO */
1341 dwc_otg_read_fifo(sc, td->pc, td->offset, count);
1343 td->remainder -= count;
1344 td->offset += count;
1345 sc->sc_chan_state[channel].hcint |= HCINT_SOFTWARE_ONLY;
1351 dwc_otg_common_rx_ack(sc);
1359 dwc_otg_host_data_rx(struct dwc_otg_softc *sc, struct dwc_otg_td *td)
1367 for (x = 0; x != td->max_packet_count; x++) {
1368 channel = td->channel[x];
1369 if (channel >= DWC_OTG_MAX_CHANNELS)
1371 hcint |= sc->sc_chan_state[channel].hcint;
1373 DPRINTF("CH=%d ST=%d HCINT=0x%08x HCCHAR=0x%08x HCTSIZ=0x%08x\n",
1374 channel, td->state, hcint,
1375 DWC_OTG_READ_4(sc, DOTG_HCCHAR(channel)),
1376 DWC_OTG_READ_4(sc, DOTG_HCTSIZ(channel)));
1378 /* check interrupt bits */
1379 if (hcint & (HCINT_RETRY |
1380 HCINT_ACK | HCINT_NYET)) {
1381 /* give success bits priority over failure bits */
1382 } else if (hcint & HCINT_STALL) {
1383 DPRINTF("CH=%d STALL\n", channel);
1384 td->error_stall = 1;
1387 } else if (hcint & HCINT_ERRORS) {
1388 DPRINTF("CH=%d ERROR\n", channel);
1390 if (td->hcsplt != 0 || td->errcnt >= 3) {
1391 if (td->ep_type != UE_ISOCHRONOUS) {
1398 /* check channels for data, if any */
1399 if (dwc_otg_host_data_rx_sub(sc, td, channel))
1402 /* refresh interrupt status */
1403 hcint |= sc->sc_chan_state[channel].hcint;
1405 if (hcint & (HCINT_ERRORS | HCINT_RETRY |
1406 HCINT_ACK | HCINT_NYET)) {
1407 if (!(hcint & HCINT_ERRORS))
1412 switch (td->state) {
1413 case DWC_CHAN_ST_START:
1414 if (td->hcsplt != 0)
1419 case DWC_CHAN_ST_WAIT_ANE:
1420 if (hcint & (HCINT_RETRY | HCINT_ERRORS)) {
1421 if (td->ep_type == UE_INTERRUPT) {
1423 * The USB specification does not
1424 * mandate a particular data toggle
1425 * value for USB INTERRUPT
1426 * transfers. Switch the data toggle
1427 * value to receive the packet
1430 if (hcint & HCINT_DATATGLERR) {
1431 DPRINTF("Retrying packet due to "
1432 "data toggle error\n");
1436 } else if (td->ep_type == UE_ISOCHRONOUS) {
1437 if (td->hcsplt != 0) {
1439 * Sometimes the complete
1440 * split packet may be queued
1442 * transaction translator will
1443 * return a NAK. Ignore
1444 * this message and retry the
1445 * complete split instead.
1447 DPRINTF("Retrying complete split\n");
1453 td->tt_scheduled = 0;
1454 if (td->hcsplt != 0)
1458 } else if (hcint & HCINT_NYET) {
1459 if (td->hcsplt != 0) {
1463 /* not a valid token for IN endpoints */
1467 } else if (hcint & HCINT_ACK) {
1468 /* wait for data - ACK arrived first */
1469 if (!(hcint & HCINT_SOFTWARE_ONLY))
1472 if (td->ep_type == UE_ISOCHRONOUS) {
1473 /* check if we are complete */
1474 if (td->tt_xactpos == HCSPLT_XACTPOS_BEGIN) {
1476 } else if (td->hcsplt != 0) {
1479 /* get more packets */
1483 /* check if we are complete */
1484 if ((td->remainder == 0) || (td->got_short != 0)) {
1489 * Else need to receive a zero length
1493 td->tt_scheduled = 0;
1495 if (td->hcsplt != 0)
1503 case DWC_CHAN_ST_WAIT_S_ANE:
1505 * NOTE: The DWC OTG hardware provides a fake ACK in
1506 * case of interrupt and isochronous transfers:
1508 if (hcint & (HCINT_RETRY | HCINT_ERRORS)) {
1510 td->tt_scheduled = 0;
1512 } else if (hcint & HCINT_NYET) {
1513 td->tt_scheduled = 0;
1515 } else if (hcint & HCINT_ACK) {
1521 case DWC_CHAN_ST_WAIT_C_PKT:
1530 /* free existing channel, if any */
1531 dwc_otg_host_channel_free(sc, td);
1533 if (td->hcsplt != 0) {
1534 delta = td->tt_complete_slot - sc->sc_last_frame_num - 1;
1535 if (td->tt_scheduled == 0 || delta < DWC_OTG_TT_SLOT_MAX) {
1536 if (td->ep_type != UE_ISOCHRONOUS) {
1537 td->state = DWC_CHAN_ST_WAIT_C_PKT;
1541 delta = sc->sc_last_frame_num - td->tt_start_slot;
1542 if (delta > DWC_OTG_TT_SLOT_MAX) {
1543 if (td->ep_type != UE_ISOCHRONOUS) {
1544 /* we missed the service interval */
1549 /* complete split */
1550 td->hcsplt |= HCSPLT_COMPSPLT;
1551 } else if (dwc_otg_host_rate_check(sc, td)) {
1552 td->state = DWC_CHAN_ST_WAIT_C_PKT;
1556 /* allocate a new channel */
1557 if (dwc_otg_host_channel_alloc(sc, td, 0)) {
1558 td->state = DWC_CHAN_ST_WAIT_C_PKT;
1562 /* set toggle, if any */
1563 if (td->set_toggle) {
1568 td->state = DWC_CHAN_ST_WAIT_ANE;
1570 for (x = 0; x != td->max_packet_count; x++) {
1571 channel = td->channel[x];
1573 /* receive one packet */
1574 DWC_OTG_WRITE_4(sc, DOTG_HCTSIZ(channel),
1575 (td->max_packet_size << HCTSIZ_XFERSIZE_SHIFT) |
1576 (1 << HCTSIZ_PKTCNT_SHIFT) |
1577 (td->toggle ? (HCTSIZ_PID_DATA1 << HCTSIZ_PID_SHIFT) :
1578 (HCTSIZ_PID_DATA0 << HCTSIZ_PID_SHIFT)));
1580 DWC_OTG_WRITE_4(sc, DOTG_HCSPLT(channel), td->hcsplt);
1582 hcchar = td->hcchar;
1583 hcchar |= HCCHAR_EPDIR_IN;
1585 if (td->ep_type == UE_ISOCHRONOUS) {
1586 if (td->hcsplt != 0) {
1587 /* continously buffer */
1588 if (sc->sc_last_frame_num & 1)
1589 hcchar &= ~HCCHAR_ODDFRM;
1591 hcchar |= HCCHAR_ODDFRM;
1593 /* multi buffer, if any */
1594 if (sc->sc_last_frame_num & 1)
1595 hcchar |= HCCHAR_ODDFRM;
1597 hcchar &= ~HCCHAR_ODDFRM;
1600 hcchar &= ~HCCHAR_ODDFRM;
1603 /* must enable channel before data can be received */
1604 DWC_OTG_WRITE_4(sc, DOTG_HCCHAR(channel), hcchar);
1606 /* wait until next slot before trying complete split */
1607 td->tt_complete_slot = sc->sc_last_frame_num + 1;
1612 /* free existing channel(s), if any */
1613 dwc_otg_host_channel_free(sc, td);
1615 delta = td->tt_start_slot - sc->sc_last_frame_num - 1;
1616 if (td->tt_scheduled == 0 || delta < DWC_OTG_TT_SLOT_MAX) {
1617 td->state = DWC_CHAN_ST_START;
1620 delta = sc->sc_last_frame_num - td->tt_start_slot;
1623 td->tt_scheduled = 0;
1624 td->state = DWC_CHAN_ST_START;
1628 /* allocate a new channel */
1629 if (dwc_otg_host_channel_alloc(sc, td, 0)) {
1630 td->state = DWC_CHAN_ST_START;
1634 channel = td->channel[0];
1636 td->hcsplt &= ~HCSPLT_COMPSPLT;
1637 td->state = DWC_CHAN_ST_WAIT_S_ANE;
1639 /* receive one packet */
1640 DWC_OTG_WRITE_4(sc, DOTG_HCTSIZ(channel),
1641 (HCTSIZ_PID_DATA0 << HCTSIZ_PID_SHIFT));
1643 DWC_OTG_WRITE_4(sc, DOTG_HCSPLT(channel), td->hcsplt);
1645 /* send after next SOF event */
1646 if ((sc->sc_last_frame_num & 1) == 0 &&
1647 td->ep_type == UE_ISOCHRONOUS)
1648 td->hcchar |= HCCHAR_ODDFRM;
1650 td->hcchar &= ~HCCHAR_ODDFRM;
1652 hcchar = td->hcchar;
1653 hcchar |= HCCHAR_EPDIR_IN;
1655 /* wait until next slot before trying complete split */
1656 td->tt_complete_slot = sc->sc_last_frame_num + 1;
1658 /* must enable channel before data can be received */
1659 DWC_OTG_WRITE_4(sc, DOTG_HCCHAR(channel), hcchar);
1661 return (1); /* busy */
1664 dwc_otg_host_channel_free(sc, td);
1665 return (0); /* complete */
1669 dwc_otg_data_rx(struct dwc_otg_softc *sc, struct dwc_otg_td *td)
1677 /* check endpoint status */
1678 if (sc->sc_last_rx_status == 0)
1681 if (GRXSTSRD_CHNUM_GET(sc->sc_last_rx_status) != td->ep_no)
1684 /* check for SETUP packet */
1685 if ((sc->sc_last_rx_status & GRXSTSRD_PKTSTS_MASK) ==
1686 GRXSTSRD_STP_DATA ||
1687 (sc->sc_last_rx_status & GRXSTSRD_PKTSTS_MASK) ==
1688 GRXSTSRD_STP_COMPLETE) {
1689 if (td->remainder == 0) {
1691 * We are actually complete and have
1692 * received the next SETUP
1694 DPRINTFN(5, "faking complete\n");
1695 return (0); /* complete */
1698 * USB Host Aborted the transfer.
1701 return (0); /* complete */
1704 if ((sc->sc_last_rx_status & GRXSTSRD_PKTSTS_MASK) !=
1705 GRXSTSRD_OUT_DATA) {
1707 dwc_otg_common_rx_ack(sc);
1711 /* get the packet byte count */
1712 count = GRXSTSRD_BCNT_GET(sc->sc_last_rx_status);
1714 /* verify the packet byte count */
1715 if (count != td->max_packet_size) {
1716 if (count < td->max_packet_size) {
1717 /* we have a short packet */
1721 /* invalid USB packet */
1725 dwc_otg_common_rx_ack(sc);
1726 return (0); /* we are complete */
1729 /* verify the packet byte count */
1730 if (count > td->remainder) {
1731 /* invalid USB packet */
1735 dwc_otg_common_rx_ack(sc);
1736 return (0); /* we are complete */
1739 /* read data from FIFO */
1740 dwc_otg_read_fifo(sc, td->pc, td->offset, count);
1742 td->remainder -= count;
1743 td->offset += count;
1746 dwc_otg_common_rx_ack(sc);
1748 temp = sc->sc_out_ctl[td->ep_no];
1750 /* check for isochronous mode */
1751 if ((temp & DIEPCTL_EPTYPE_MASK) ==
1752 (DIEPCTL_EPTYPE_ISOC << DIEPCTL_EPTYPE_SHIFT)) {
1753 /* toggle odd or even frame bit */
1754 if (temp & DIEPCTL_SETD1PID) {
1755 temp &= ~DIEPCTL_SETD1PID;
1756 temp |= DIEPCTL_SETD0PID;
1758 temp &= ~DIEPCTL_SETD0PID;
1759 temp |= DIEPCTL_SETD1PID;
1761 sc->sc_out_ctl[td->ep_no] = temp;
1764 /* check if we are complete */
1765 if ((td->remainder == 0) || got_short) {
1766 if (td->short_pkt) {
1767 /* we are complete */
1770 /* else need to receive a zero length packet */
1775 /* enable SETUP and transfer complete interrupt */
1776 if (td->ep_no == 0) {
1777 DWC_OTG_WRITE_4(sc, DOTG_DOEPTSIZ(0),
1778 DXEPTSIZ_SET_MULTI(3) |
1779 DXEPTSIZ_SET_NPKT(1) |
1780 DXEPTSIZ_SET_NBYTES(td->max_packet_size));
1782 /* allow reception of multiple packets */
1783 DWC_OTG_WRITE_4(sc, DOTG_DOEPTSIZ(td->ep_no),
1784 DXEPTSIZ_SET_MULTI(1) |
1785 DXEPTSIZ_SET_NPKT(4) |
1786 DXEPTSIZ_SET_NBYTES(4 *
1787 ((td->max_packet_size + 3) & ~3)));
1789 temp = sc->sc_out_ctl[td->ep_no];
1790 DWC_OTG_WRITE_4(sc, DOTG_DOEPCTL(td->ep_no), temp |
1791 DOEPCTL_EPENA | DOEPCTL_CNAK);
1793 return (1); /* not complete */
1797 dwc_otg_host_data_tx(struct dwc_otg_softc *sc, struct dwc_otg_td *td)
1806 dwc_otg_host_dump_rx(sc, td);
1808 /* check that last channel is complete */
1809 channel = td->channel[td->npkt];
1811 if (channel < DWC_OTG_MAX_CHANNELS) {
1812 hcint = sc->sc_chan_state[channel].hcint;
1814 DPRINTF("CH=%d ST=%d HCINT=0x%08x HCCHAR=0x%08x HCTSIZ=0x%08x\n",
1815 channel, td->state, hcint,
1816 DWC_OTG_READ_4(sc, DOTG_HCCHAR(channel)),
1817 DWC_OTG_READ_4(sc, DOTG_HCTSIZ(channel)));
1819 if (hcint & (HCINT_RETRY |
1820 HCINT_ACK | HCINT_NYET)) {
1821 /* give success bits priority over failure bits */
1822 } else if (hcint & HCINT_STALL) {
1823 DPRINTF("CH=%d STALL\n", channel);
1824 td->error_stall = 1;
1827 } else if (hcint & HCINT_ERRORS) {
1828 DPRINTF("CH=%d ERROR\n", channel);
1830 if (td->hcsplt != 0 || td->errcnt >= 3) {
1836 if (hcint & (HCINT_ERRORS | HCINT_RETRY |
1837 HCINT_ACK | HCINT_NYET)) {
1839 if (!(hcint & HCINT_ERRORS))
1846 switch (td->state) {
1847 case DWC_CHAN_ST_START:
1850 case DWC_CHAN_ST_WAIT_ANE:
1851 if (hcint & (HCINT_RETRY | HCINT_ERRORS)) {
1853 td->tt_scheduled = 0;
1855 } else if (hcint & (HCINT_ACK | HCINT_NYET)) {
1856 td->offset += td->tx_bytes;
1857 td->remainder -= td->tx_bytes;
1859 /* check if next response will be a NAK */
1860 if (hcint & HCINT_NYET)
1864 td->tt_scheduled = 0;
1866 /* check remainder */
1867 if (td->remainder == 0) {
1872 * Else we need to transmit a short
1880 case DWC_CHAN_ST_WAIT_S_ANE:
1881 if (hcint & (HCINT_RETRY | HCINT_ERRORS)) {
1883 td->tt_scheduled = 0;
1885 } else if (hcint & (HCINT_ACK | HCINT_NYET)) {
1891 case DWC_CHAN_ST_WAIT_C_ANE:
1892 if (hcint & HCINT_NYET) {
1894 } else if (hcint & (HCINT_RETRY | HCINT_ERRORS)) {
1896 td->tt_scheduled = 0;
1898 } else if (hcint & HCINT_ACK) {
1899 td->offset += td->tx_bytes;
1900 td->remainder -= td->tx_bytes;
1903 td->tt_scheduled = 0;
1905 /* check remainder */
1906 if (td->remainder == 0) {
1910 /* else we need to transmit a short packet */
1916 case DWC_CHAN_ST_WAIT_C_PKT:
1919 case DWC_CHAN_ST_TX_WAIT_ISOC:
1920 /* Check if ISOCHRONOUS OUT traffic is complete */
1921 if ((hcint & HCINT_HCH_DONE_MASK) == 0)
1924 td->offset += td->tx_bytes;
1925 td->remainder -= td->tx_bytes;
1933 /* free existing channel(s), if any */
1934 dwc_otg_host_channel_free(sc, td);
1936 if (td->hcsplt != 0) {
1937 delta = td->tt_start_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_START;
1942 delta = sc->sc_last_frame_num - td->tt_start_slot;
1945 td->tt_scheduled = 0;
1946 td->state = DWC_CHAN_ST_START;
1949 } else if (dwc_otg_host_rate_check(sc, td)) {
1950 td->state = DWC_CHAN_ST_START;
1954 /* allocate a new channel */
1955 if (dwc_otg_host_channel_alloc(sc, td, 1)) {
1956 td->state = DWC_CHAN_ST_START;
1960 /* set toggle, if any */
1961 if (td->set_toggle) {
1966 if (td->ep_type == UE_ISOCHRONOUS) {
1967 /* ISOCHRONOUS OUT transfers don't have any ACKs */
1968 td->state = DWC_CHAN_ST_TX_WAIT_ISOC;
1969 td->hcsplt &= ~HCSPLT_COMPSPLT;
1970 if (td->hcsplt != 0) {
1971 /* get maximum transfer length */
1972 count = td->remainder;
1973 if (count > HCSPLT_XACTLEN_BURST) {
1974 DPRINTF("TT overflow\n");
1978 /* Update transaction position */
1979 td->hcsplt &= ~HCSPLT_XACTPOS_MASK;
1980 td->hcsplt |= (HCSPLT_XACTPOS_ALL << HCSPLT_XACTPOS_SHIFT);
1982 } else if (td->hcsplt != 0) {
1983 td->hcsplt &= ~HCSPLT_COMPSPLT;
1984 /* Wait for ACK/NAK/ERR from TT */
1985 td->state = DWC_CHAN_ST_WAIT_S_ANE;
1987 /* Wait for ACK/NAK/STALL from device */
1988 td->state = DWC_CHAN_ST_WAIT_ANE;
1993 for (x = 0; x != td->max_packet_count; x++) {
1996 channel = td->channel[x];
1998 /* send one packet at a time */
1999 count = td->max_packet_size;
2000 rem_bytes = td->remainder - td->tx_bytes;
2001 if (rem_bytes < count) {
2002 /* we have a short packet */
2006 if (count == rem_bytes) {
2010 DWC_OTG_WRITE_4(sc, DOTG_HCTSIZ(channel),
2011 (count << HCTSIZ_XFERSIZE_SHIFT) |
2012 (1 << HCTSIZ_PKTCNT_SHIFT) |
2013 (td->toggle ? (HCTSIZ_PID_DATA1 << HCTSIZ_PID_SHIFT) :
2014 (HCTSIZ_PID_DATA0 << HCTSIZ_PID_SHIFT)));
2017 DWC_OTG_WRITE_4(sc, DOTG_HCTSIZ(channel),
2018 (count << HCTSIZ_XFERSIZE_SHIFT) |
2019 (1 << HCTSIZ_PKTCNT_SHIFT) |
2020 (HCTSIZ_PID_DATA1 << HCTSIZ_PID_SHIFT));
2023 DWC_OTG_WRITE_4(sc, DOTG_HCTSIZ(channel),
2024 (count << HCTSIZ_XFERSIZE_SHIFT) |
2025 (1 << HCTSIZ_PKTCNT_SHIFT) |
2026 (HCTSIZ_PID_DATA2 << HCTSIZ_PID_SHIFT));
2029 } else if (td->ep_type == UE_ISOCHRONOUS &&
2030 td->max_packet_count > 1) {
2031 /* ISOCHRONOUS multi packet */
2032 DWC_OTG_WRITE_4(sc, DOTG_HCTSIZ(channel),
2033 (count << HCTSIZ_XFERSIZE_SHIFT) |
2034 (1 << HCTSIZ_PKTCNT_SHIFT) |
2035 (HCTSIZ_PID_MDATA << HCTSIZ_PID_SHIFT));
2037 /* TODO: HCTSIZ_DOPNG */
2038 /* standard BULK/INTERRUPT/CONTROL packet */
2039 DWC_OTG_WRITE_4(sc, DOTG_HCTSIZ(channel),
2040 (count << HCTSIZ_XFERSIZE_SHIFT) |
2041 (1 << HCTSIZ_PKTCNT_SHIFT) |
2042 (td->toggle ? (HCTSIZ_PID_DATA1 << HCTSIZ_PID_SHIFT) :
2043 (HCTSIZ_PID_DATA0 << HCTSIZ_PID_SHIFT)));
2046 DWC_OTG_WRITE_4(sc, DOTG_HCSPLT(channel), td->hcsplt);
2048 hcchar = td->hcchar;
2049 hcchar &= ~HCCHAR_EPDIR_IN;
2051 /* send after next SOF event */
2052 if ((sc->sc_last_frame_num & 1) == 0 &&
2053 td->ep_type == UE_ISOCHRONOUS)
2054 hcchar |= HCCHAR_ODDFRM;
2056 hcchar &= ~HCCHAR_ODDFRM;
2058 /* must enable before writing data to FIFO */
2059 DWC_OTG_WRITE_4(sc, DOTG_HCCHAR(channel), hcchar);
2062 /* write data into FIFO */
2063 dwc_otg_write_fifo(sc, td->pc, td->offset +
2064 td->tx_bytes, DOTG_DFIFO(channel), count);
2067 /* store number of bytes transmitted */
2068 td->tx_bytes += count;
2070 /* store last packet index */
2073 /* check for last packet */
2074 if (count == rem_bytes)
2080 /* free existing channel, if any */
2081 dwc_otg_host_channel_free(sc, td);
2083 delta = td->tt_complete_slot - sc->sc_last_frame_num - 1;
2084 if (td->tt_scheduled == 0 || delta < DWC_OTG_TT_SLOT_MAX) {
2085 td->state = DWC_CHAN_ST_WAIT_C_PKT;
2088 delta = sc->sc_last_frame_num - td->tt_start_slot;
2089 if (delta > DWC_OTG_TT_SLOT_MAX) {
2090 /* we missed the service interval */
2091 if (td->ep_type != UE_ISOCHRONOUS)
2096 /* allocate a new channel */
2097 if (dwc_otg_host_channel_alloc(sc, td, 0)) {
2098 td->state = DWC_CHAN_ST_WAIT_C_PKT;
2102 channel = td->channel[0];
2104 td->hcsplt |= HCSPLT_COMPSPLT;
2105 td->state = DWC_CHAN_ST_WAIT_C_ANE;
2107 DWC_OTG_WRITE_4(sc, DOTG_HCTSIZ(channel),
2108 (HCTSIZ_PID_DATA0 << HCTSIZ_PID_SHIFT));
2110 DWC_OTG_WRITE_4(sc, DOTG_HCSPLT(channel), td->hcsplt);
2112 hcchar = td->hcchar;
2113 hcchar &= ~HCCHAR_EPDIR_IN;
2115 /* receive complete split ASAP */
2116 if ((sc->sc_last_frame_num & 1) != 0 &&
2117 td->ep_type == UE_ISOCHRONOUS)
2118 hcchar |= HCCHAR_ODDFRM;
2120 hcchar &= ~HCCHAR_ODDFRM;
2122 /* must enable channel before data can be received */
2123 DWC_OTG_WRITE_4(sc, DOTG_HCCHAR(channel), hcchar);
2125 /* wait until next slot before trying complete split */
2126 td->tt_complete_slot = sc->sc_last_frame_num + 1;
2128 return (1); /* busy */
2131 dwc_otg_host_channel_free(sc, td);
2132 return (0); /* complete */
2136 dwc_otg_data_tx(struct dwc_otg_softc *sc, struct dwc_otg_td *td)
2138 uint32_t max_buffer;
2145 to = 3; /* don't loop forever! */
2147 max_buffer = sc->sc_hw_ep_profile[td->ep_no].max_buffer;
2150 /* check for for endpoint 0 data */
2152 temp = sc->sc_last_rx_status;
2154 if ((td->ep_no == 0) && (temp != 0) &&
2155 (GRXSTSRD_CHNUM_GET(temp) == 0)) {
2157 if ((temp & GRXSTSRD_PKTSTS_MASK) !=
2158 GRXSTSRD_STP_DATA &&
2159 (temp & GRXSTSRD_PKTSTS_MASK) !=
2160 GRXSTSRD_STP_COMPLETE) {
2162 /* dump data - wrong direction */
2163 dwc_otg_common_rx_ack(sc);
2166 * The current transfer was cancelled
2170 return (0); /* complete */
2174 /* fill in more TX data, if possible */
2175 if (td->tx_bytes != 0) {
2179 /* check if packets have been transferred */
2180 temp = DWC_OTG_READ_4(sc, DOTG_DIEPTSIZ(td->ep_no));
2182 /* get current packet number */
2183 cpkt = DXEPTSIZ_GET_NPKT(temp);
2185 if (cpkt >= td->npkt) {
2188 if (max_buffer != 0) {
2189 fifo_left = (td->npkt - cpkt) *
2190 td->max_packet_size;
2192 if (fifo_left > max_buffer)
2193 fifo_left = max_buffer;
2195 fifo_left = td->max_packet_size;
2199 count = td->tx_bytes;
2200 if (count > fifo_left)
2204 /* write data into FIFO */
2205 dwc_otg_write_fifo(sc, td->pc, td->offset,
2206 DOTG_DFIFO(td->ep_no), count);
2208 td->tx_bytes -= count;
2209 td->remainder -= count;
2210 td->offset += count;
2213 if (td->tx_bytes != 0)
2216 /* check remainder */
2217 if (td->remainder == 0) {
2219 return (0); /* complete */
2221 /* else we need to transmit a short packet */
2228 /* check if not all packets have been transferred */
2229 temp = DWC_OTG_READ_4(sc, DOTG_DIEPTSIZ(td->ep_no));
2231 if (DXEPTSIZ_GET_NPKT(temp) != 0) {
2233 DPRINTFN(5, "busy ep=%d npkt=%d DIEPTSIZ=0x%08x "
2234 "DIEPCTL=0x%08x\n", td->ep_no,
2235 DXEPTSIZ_GET_NPKT(temp),
2236 temp, DWC_OTG_READ_4(sc, DOTG_DIEPCTL(td->ep_no)));
2241 DPRINTFN(5, "rem=%u ep=%d\n", td->remainder, td->ep_no);
2243 /* try to optimise by sending more data */
2244 if ((max_buffer != 0) && ((td->max_packet_size & 3) == 0)) {
2246 /* send multiple packets at the same time */
2247 mpkt = max_buffer / td->max_packet_size;
2252 count = td->remainder;
2253 if (count > 0x7FFFFF)
2254 count = 0x7FFFFF - (0x7FFFFF % td->max_packet_size);
2256 td->npkt = count / td->max_packet_size;
2259 * NOTE: We could use 0x3FE instead of "mpkt" in the
2260 * check below to get more throughput, but then we
2261 * have a dependency towards non-generic chip features
2262 * to disable the TX-FIFO-EMPTY interrupts on a per
2263 * endpoint basis. Increase the maximum buffer size of
2264 * the IN endpoint to increase the performance.
2266 if (td->npkt > mpkt) {
2268 count = td->max_packet_size * mpkt;
2269 } else if ((count == 0) || (count % td->max_packet_size)) {
2270 /* we are transmitting a short packet */
2275 /* send one packet at a time */
2277 count = td->max_packet_size;
2278 if (td->remainder < count) {
2279 /* we have a short packet */
2281 count = td->remainder;
2285 DWC_OTG_WRITE_4(sc, DOTG_DIEPTSIZ(td->ep_no),
2286 DXEPTSIZ_SET_MULTI(1) |
2287 DXEPTSIZ_SET_NPKT(td->npkt) |
2288 DXEPTSIZ_SET_NBYTES(count));
2290 /* make room for buffering */
2293 temp = sc->sc_in_ctl[td->ep_no];
2295 /* check for isochronous mode */
2296 if ((temp & DIEPCTL_EPTYPE_MASK) ==
2297 (DIEPCTL_EPTYPE_ISOC << DIEPCTL_EPTYPE_SHIFT)) {
2298 /* toggle odd or even frame bit */
2299 if (temp & DIEPCTL_SETD1PID) {
2300 temp &= ~DIEPCTL_SETD1PID;
2301 temp |= DIEPCTL_SETD0PID;
2303 temp &= ~DIEPCTL_SETD0PID;
2304 temp |= DIEPCTL_SETD1PID;
2306 sc->sc_in_ctl[td->ep_no] = temp;
2309 /* must enable before writing data to FIFO */
2310 DWC_OTG_WRITE_4(sc, DOTG_DIEPCTL(td->ep_no), temp |
2311 DIEPCTL_EPENA | DIEPCTL_CNAK);
2313 td->tx_bytes = count;
2315 /* check remainder */
2316 if (td->tx_bytes == 0 &&
2317 td->remainder == 0) {
2319 return (0); /* complete */
2321 /* else we need to transmit a short packet */
2326 return (1); /* not complete */
2330 dwc_otg_data_tx_sync(struct dwc_otg_softc *sc, struct dwc_otg_td *td)
2335 * If all packets are transferred we are complete:
2337 temp = DWC_OTG_READ_4(sc, DOTG_DIEPTSIZ(td->ep_no));
2339 /* check that all packets have been transferred */
2340 if (DXEPTSIZ_GET_NPKT(temp) != 0) {
2341 DPRINTFN(5, "busy ep=%d\n", td->ep_no);
2348 /* we only want to know if there is a SETUP packet or free IN packet */
2350 temp = sc->sc_last_rx_status;
2352 if ((td->ep_no == 0) && (temp != 0) &&
2353 (GRXSTSRD_CHNUM_GET(temp) == 0)) {
2355 if ((temp & GRXSTSRD_PKTSTS_MASK) ==
2356 GRXSTSRD_STP_DATA ||
2357 (temp & GRXSTSRD_PKTSTS_MASK) ==
2358 GRXSTSRD_STP_COMPLETE) {
2359 DPRINTFN(5, "faking complete\n");
2361 * Race condition: We are complete!
2365 /* dump data - wrong direction */
2366 dwc_otg_common_rx_ack(sc);
2369 return (1); /* not complete */
2373 dwc_otg_xfer_do_fifo(struct dwc_otg_softc *sc, struct usb_xfer *xfer)
2375 struct dwc_otg_td *td;
2382 td = xfer->td_transfer_cache;
2387 if ((td->func) (sc, td)) {
2388 /* operation in progress */
2391 if (((void *)td) == xfer->td_transfer_last) {
2394 if (td->error_any) {
2396 } else if (td->remainder > 0) {
2398 * We had a short transfer. If there is no alternate
2399 * next, stop processing !
2406 * Fetch the next transfer descriptor and transfer
2407 * some flags to the next transfer descriptor
2409 tmr_res = td->tmr_res;
2410 tmr_val = td->tmr_val;
2411 toggle = td->toggle;
2413 xfer->td_transfer_cache = td;
2414 td->toggle = toggle; /* transfer toggle */
2415 td->tmr_res = tmr_res;
2416 td->tmr_val = tmr_val;
2421 xfer->td_transfer_cache = NULL;
2422 sc->sc_xfer_complete = 1;
2426 dwc_otg_xfer_do_complete_locked(struct dwc_otg_softc *sc, struct usb_xfer *xfer)
2428 struct dwc_otg_td *td;
2432 td = xfer->td_transfer_cache;
2434 /* compute all actual lengths */
2435 dwc_otg_standard_done(xfer);
2442 dwc_otg_timer(void *_sc)
2444 struct dwc_otg_softc *sc = _sc;
2446 USB_BUS_LOCK_ASSERT(&sc->sc_bus, MA_OWNED);
2450 USB_BUS_SPIN_LOCK(&sc->sc_bus);
2452 /* increment timer value */
2455 /* enable SOF interrupt, which will poll jobs */
2456 dwc_otg_enable_sof_irq(sc);
2458 USB_BUS_SPIN_UNLOCK(&sc->sc_bus);
2460 if (sc->sc_timer_active) {
2462 usb_callout_reset(&sc->sc_timer,
2463 hz / (1000 / DWC_OTG_HOST_TIMER_RATE),
2464 &dwc_otg_timer, sc);
2469 dwc_otg_timer_start(struct dwc_otg_softc *sc)
2471 if (sc->sc_timer_active != 0)
2474 sc->sc_timer_active = 1;
2477 usb_callout_reset(&sc->sc_timer,
2478 hz / (1000 / DWC_OTG_HOST_TIMER_RATE),
2479 &dwc_otg_timer, sc);
2483 dwc_otg_timer_stop(struct dwc_otg_softc *sc)
2485 if (sc->sc_timer_active == 0)
2488 sc->sc_timer_active = 0;
2491 usb_callout_stop(&sc->sc_timer);
2495 dwc_otg_compute_isoc_rx_tt_slot(struct dwc_otg_tt_info *pinfo)
2497 if (pinfo->slot_index < DWC_OTG_TT_SLOT_MAX)
2498 pinfo->slot_index++;
2499 return (pinfo->slot_index);
2503 dwc_otg_update_host_transfer_schedule_locked(struct dwc_otg_softc *sc)
2505 TAILQ_HEAD(, usb_xfer) head;
2506 struct usb_xfer *xfer;
2507 struct usb_xfer *xfer_next;
2508 struct dwc_otg_td *td;
2512 temp = DWC_OTG_READ_4(sc, DOTG_HFNUM) & DWC_OTG_FRAME_MASK;
2514 if (sc->sc_last_frame_num == temp)
2517 sc->sc_last_frame_num = temp;
2521 if ((temp & 7) == 0) {
2523 /* reset the schedule */
2524 memset(sc->sc_tt_info, 0, sizeof(sc->sc_tt_info));
2526 TAILQ_FOREACH_SAFE(xfer, &sc->sc_bus.intr_q.head, wait_entry, xfer_next) {
2527 td = xfer->td_transfer_cache;
2528 if (td == NULL || td->ep_type != UE_ISOCHRONOUS)
2531 /* check for IN direction */
2532 if ((td->hcchar & HCCHAR_EPDIR_IN) != 0)
2537 if (td->hcsplt == 0 || td->tt_scheduled != 0)
2541 slot = dwc_otg_compute_isoc_rx_tt_slot(
2542 sc->sc_tt_info + td->tt_index);
2545 * Not enough time to get complete
2551 td->tt_start_slot = temp + slot;
2552 td->tt_scheduled = 1;
2553 TAILQ_REMOVE(&sc->sc_bus.intr_q.head, xfer, wait_entry);
2554 TAILQ_INSERT_TAIL(&head, xfer, wait_entry);
2557 TAILQ_FOREACH_SAFE(xfer, &sc->sc_bus.intr_q.head, wait_entry, xfer_next) {
2558 td = xfer->td_transfer_cache;
2559 if (td == NULL || td->ep_type != UE_ISOCHRONOUS)
2562 /* check for OUT direction */
2563 if ((td->hcchar & HCCHAR_EPDIR_IN) == 0)
2568 if (td->hcsplt == 0 || td->tt_scheduled != 0)
2572 td->tt_start_slot = temp;
2573 td->tt_scheduled = 1;
2574 TAILQ_REMOVE(&sc->sc_bus.intr_q.head, xfer, wait_entry);
2575 TAILQ_INSERT_TAIL(&head, xfer, wait_entry);
2578 TAILQ_FOREACH_SAFE(xfer, &sc->sc_bus.intr_q.head, wait_entry, xfer_next) {
2579 td = xfer->td_transfer_cache;
2580 if (td == NULL || td->ep_type != UE_INTERRUPT)
2583 if (td->tt_scheduled != 0) {
2588 if (dwc_otg_host_rate_check_interrupt(sc, td))
2591 if (td->hcsplt == 0) {
2593 td->tt_scheduled = 1;
2598 td->tt_start_slot = temp;
2600 td->tt_scheduled = 1;
2601 TAILQ_REMOVE(&sc->sc_bus.intr_q.head, xfer, wait_entry);
2602 TAILQ_INSERT_TAIL(&head, xfer, wait_entry);
2605 TAILQ_FOREACH_SAFE(xfer, &sc->sc_bus.intr_q.head, wait_entry, xfer_next) {
2606 td = xfer->td_transfer_cache;
2608 td->ep_type != UE_CONTROL) {
2614 if (td->hcsplt == 0 || td->tt_scheduled != 0)
2618 td->tt_start_slot = temp;
2619 td->tt_scheduled = 1;
2620 TAILQ_REMOVE(&sc->sc_bus.intr_q.head, xfer, wait_entry);
2621 TAILQ_INSERT_TAIL(&head, xfer, wait_entry);
2624 if ((temp & 7) < 6) {
2625 TAILQ_FOREACH_SAFE(xfer, &sc->sc_bus.intr_q.head, wait_entry, xfer_next) {
2626 td = xfer->td_transfer_cache;
2628 td->ep_type != UE_BULK) {
2634 if (td->hcsplt == 0 || td->tt_scheduled != 0)
2638 td->tt_start_slot = temp;
2639 td->tt_scheduled = 1;
2640 TAILQ_REMOVE(&sc->sc_bus.intr_q.head, xfer, wait_entry);
2641 TAILQ_INSERT_TAIL(&head, xfer, wait_entry);
2645 /* Put TT transfers in execution order at the end */
2646 TAILQ_CONCAT(&sc->sc_bus.intr_q.head, &head, wait_entry);
2648 /* move all TT transfers in front, keeping the current order */
2649 TAILQ_FOREACH_SAFE(xfer, &sc->sc_bus.intr_q.head, wait_entry, xfer_next) {
2650 td = xfer->td_transfer_cache;
2651 if (td == NULL || td->hcsplt == 0)
2653 TAILQ_REMOVE(&sc->sc_bus.intr_q.head, xfer, wait_entry);
2654 TAILQ_INSERT_TAIL(&head, xfer, wait_entry);
2656 TAILQ_CONCAT(&head, &sc->sc_bus.intr_q.head, wait_entry);
2657 TAILQ_CONCAT(&sc->sc_bus.intr_q.head, &head, wait_entry);
2659 /* put non-TT non-ISOCHRONOUS transfers last */
2660 TAILQ_FOREACH_SAFE(xfer, &sc->sc_bus.intr_q.head, wait_entry, xfer_next) {
2661 td = xfer->td_transfer_cache;
2662 if (td == NULL || td->hcsplt != 0 || td->ep_type == UE_ISOCHRONOUS)
2664 TAILQ_REMOVE(&sc->sc_bus.intr_q.head, xfer, wait_entry);
2665 TAILQ_INSERT_TAIL(&head, xfer, wait_entry);
2667 TAILQ_CONCAT(&sc->sc_bus.intr_q.head, &head, wait_entry);
2669 if ((temp & 7) == 0) {
2671 DPRINTFN(12, "SOF interrupt #%d, needsof=%d\n",
2672 (int)temp, (int)sc->sc_needsof);
2674 /* update SOF IRQ mask */
2675 if (sc->sc_irq_mask & GINTMSK_SOFMSK) {
2676 if (sc->sc_needsof == 0) {
2677 sc->sc_irq_mask &= ~GINTMSK_SOFMSK;
2678 DWC_OTG_WRITE_4(sc, DOTG_GINTMSK, sc->sc_irq_mask);
2681 if (sc->sc_needsof != 0) {
2682 sc->sc_irq_mask |= GINTMSK_SOFMSK;
2683 DWC_OTG_WRITE_4(sc, DOTG_GINTMSK, sc->sc_irq_mask);
2687 /* clear need SOF flag */
2694 dwc_otg_interrupt_poll_locked(struct dwc_otg_softc *sc)
2696 struct usb_xfer *xfer;
2700 uint8_t got_rx_status;
2703 if (sc->sc_flags.status_device_mode == 0) {
2705 * Update host transfer schedule, so that new
2706 * transfers can be issued:
2708 dwc_otg_update_host_transfer_schedule_locked(sc);
2712 if (++count == 16) {
2713 /* give other interrupts a chance */
2718 /* get all host channel interrupts */
2719 haint = DWC_OTG_READ_4(sc, DOTG_HAINT);
2722 if (x >= sc->sc_host_ch_max)
2724 temp = DWC_OTG_READ_4(sc, DOTG_HCINT(x));
2725 DWC_OTG_WRITE_4(sc, DOTG_HCINT(x), temp);
2726 temp &= ~HCINT_SOFTWARE_ONLY;
2727 sc->sc_chan_state[x].hcint |= temp;
2728 haint &= ~(1U << x);
2731 if (sc->sc_last_rx_status == 0) {
2733 temp = DWC_OTG_READ_4(sc, DOTG_GINTSTS);
2734 if (temp & GINTSTS_RXFLVL) {
2735 /* pop current status */
2736 sc->sc_last_rx_status =
2737 DWC_OTG_READ_4(sc, DOTG_GRXSTSPD);
2740 if (sc->sc_last_rx_status != 0) {
2744 temp = sc->sc_last_rx_status &
2745 GRXSTSRD_PKTSTS_MASK;
2747 /* non-data messages we simply skip */
2748 if (temp != GRXSTSRD_STP_DATA &&
2749 temp != GRXSTSRD_STP_COMPLETE &&
2750 temp != GRXSTSRD_OUT_DATA) {
2751 /* check for halted channel */
2752 if (temp == GRXSTSRH_HALTED) {
2753 ep_no = GRXSTSRD_CHNUM_GET(sc->sc_last_rx_status);
2754 sc->sc_chan_state[ep_no].wait_halted = 0;
2755 DPRINTFN(5, "channel halt complete ch=%u\n", ep_no);
2757 /* store bytes and FIFO offset */
2758 sc->sc_current_rx_bytes = 0;
2759 sc->sc_current_rx_fifo = 0;
2761 /* acknowledge status */
2762 dwc_otg_common_rx_ack(sc);
2766 temp = GRXSTSRD_BCNT_GET(
2767 sc->sc_last_rx_status);
2768 ep_no = GRXSTSRD_CHNUM_GET(
2769 sc->sc_last_rx_status);
2771 /* store bytes and FIFO offset */
2772 sc->sc_current_rx_bytes = (temp + 3) & ~3;
2773 sc->sc_current_rx_fifo = DOTG_DFIFO(ep_no);
2775 DPRINTF("Reading %d bytes from ep %d\n", temp, ep_no);
2777 /* check if we should dump the data */
2778 if (!(sc->sc_active_rx_ep & (1U << ep_no))) {
2779 dwc_otg_common_rx_ack(sc);
2785 DPRINTFN(5, "RX status = 0x%08x: ch=%d pid=%d bytes=%d sts=%d\n",
2786 sc->sc_last_rx_status, ep_no,
2787 (sc->sc_last_rx_status >> 15) & 3,
2788 GRXSTSRD_BCNT_GET(sc->sc_last_rx_status),
2789 (sc->sc_last_rx_status >> 17) & 15);
2796 ep_no = GRXSTSRD_CHNUM_GET(
2797 sc->sc_last_rx_status);
2799 /* check if we should dump the data */
2800 if (!(sc->sc_active_rx_ep & (1U << ep_no))) {
2801 dwc_otg_common_rx_ack(sc);
2809 TAILQ_FOREACH(xfer, &sc->sc_bus.intr_q.head, wait_entry)
2810 dwc_otg_xfer_do_fifo(sc, xfer);
2812 if (got_rx_status) {
2813 /* check if data was consumed */
2814 if (sc->sc_last_rx_status == 0)
2817 /* disable RX FIFO level interrupt */
2818 sc->sc_irq_mask &= ~GINTMSK_RXFLVLMSK;
2819 DWC_OTG_WRITE_4(sc, DOTG_GINTMSK, sc->sc_irq_mask);
2824 dwc_otg_interrupt_complete_locked(struct dwc_otg_softc *sc)
2826 struct usb_xfer *xfer;
2828 /* scan for completion events */
2829 TAILQ_FOREACH(xfer, &sc->sc_bus.intr_q.head, wait_entry) {
2830 if (dwc_otg_xfer_do_complete_locked(sc, xfer))
2836 dwc_otg_vbus_interrupt(struct dwc_otg_softc *sc, uint8_t is_on)
2838 DPRINTFN(5, "vbus = %u\n", is_on);
2841 * If the USB host mode is forced, then assume VBUS is always
2842 * present else rely on the input to this function:
2844 if ((is_on != 0) || (sc->sc_mode == DWC_MODE_HOST)) {
2846 if (!sc->sc_flags.status_vbus) {
2847 sc->sc_flags.status_vbus = 1;
2849 /* complete root HUB interrupt endpoint */
2851 dwc_otg_root_intr(sc);
2854 if (sc->sc_flags.status_vbus) {
2855 sc->sc_flags.status_vbus = 0;
2856 sc->sc_flags.status_bus_reset = 0;
2857 sc->sc_flags.status_suspend = 0;
2858 sc->sc_flags.change_suspend = 0;
2859 sc->sc_flags.change_connect = 1;
2861 /* complete root HUB interrupt endpoint */
2863 dwc_otg_root_intr(sc);
2869 dwc_otg_filter_interrupt(void *arg)
2871 struct dwc_otg_softc *sc = arg;
2872 int retval = FILTER_HANDLED;
2875 USB_BUS_SPIN_LOCK(&sc->sc_bus);
2877 /* read and clear interrupt status */
2878 status = DWC_OTG_READ_4(sc, DOTG_GINTSTS);
2880 /* clear interrupts we are handling here */
2881 DWC_OTG_WRITE_4(sc, DOTG_GINTSTS, status & ~DWC_OTG_MSK_GINT_THREAD_IRQ);
2883 /* check for USB state change interrupts */
2884 if ((status & DWC_OTG_MSK_GINT_THREAD_IRQ) != 0)
2885 retval = FILTER_SCHEDULE_THREAD;
2887 /* clear FIFO empty interrupts */
2888 if (status & sc->sc_irq_mask &
2889 (GINTSTS_PTXFEMP | GINTSTS_NPTXFEMP)) {
2890 sc->sc_irq_mask &= ~(GINTSTS_PTXFEMP | GINTSTS_NPTXFEMP);
2891 DWC_OTG_WRITE_4(sc, DOTG_GINTMSK, sc->sc_irq_mask);
2893 /* clear all IN endpoint interrupts */
2894 if (status & GINTSTS_IEPINT) {
2898 for (x = 0; x != sc->sc_dev_in_ep_max; x++) {
2899 temp = DWC_OTG_READ_4(sc, DOTG_DIEPINT(x));
2901 * NOTE: Need to clear all interrupt bits,
2902 * because some appears to be unmaskable and
2903 * can cause an interrupt loop:
2906 DWC_OTG_WRITE_4(sc, DOTG_DIEPINT(x), temp);
2910 /* poll FIFOs, if any */
2911 dwc_otg_interrupt_poll_locked(sc);
2913 if (sc->sc_xfer_complete != 0)
2914 retval = FILTER_SCHEDULE_THREAD;
2916 USB_BUS_SPIN_UNLOCK(&sc->sc_bus);
2922 dwc_otg_interrupt(void *arg)
2924 struct dwc_otg_softc *sc = arg;
2927 USB_BUS_LOCK(&sc->sc_bus);
2928 USB_BUS_SPIN_LOCK(&sc->sc_bus);
2930 /* read and clear interrupt status */
2931 status = DWC_OTG_READ_4(sc, DOTG_GINTSTS);
2933 /* clear interrupts we are handling here */
2934 DWC_OTG_WRITE_4(sc, DOTG_GINTSTS, status & DWC_OTG_MSK_GINT_THREAD_IRQ);
2936 DPRINTFN(14, "GINTSTS=0x%08x HAINT=0x%08x HFNUM=0x%08x\n",
2937 status, DWC_OTG_READ_4(sc, DOTG_HAINT),
2938 DWC_OTG_READ_4(sc, DOTG_HFNUM));
2940 if (status & GINTSTS_USBRST) {
2942 /* set correct state */
2943 sc->sc_flags.status_device_mode = 1;
2944 sc->sc_flags.status_bus_reset = 0;
2945 sc->sc_flags.status_suspend = 0;
2946 sc->sc_flags.change_suspend = 0;
2947 sc->sc_flags.change_connect = 1;
2949 /* Disable SOF interrupt */
2950 sc->sc_irq_mask &= ~GINTMSK_SOFMSK;
2951 DWC_OTG_WRITE_4(sc, DOTG_GINTMSK, sc->sc_irq_mask);
2953 /* complete root HUB interrupt endpoint */
2954 dwc_otg_root_intr(sc);
2957 /* check for any bus state change interrupts */
2958 if (status & GINTSTS_ENUMDONE) {
2962 DPRINTFN(5, "end of reset\n");
2964 /* set correct state */
2965 sc->sc_flags.status_device_mode = 1;
2966 sc->sc_flags.status_bus_reset = 1;
2967 sc->sc_flags.status_suspend = 0;
2968 sc->sc_flags.change_suspend = 0;
2969 sc->sc_flags.change_connect = 1;
2970 sc->sc_flags.status_low_speed = 0;
2971 sc->sc_flags.port_enabled = 1;
2974 (void) dwc_otg_init_fifo(sc, DWC_MODE_DEVICE);
2976 /* reset function address */
2977 dwc_otg_set_address(sc, 0);
2979 /* figure out enumeration speed */
2980 temp = DWC_OTG_READ_4(sc, DOTG_DSTS);
2981 if (DSTS_ENUMSPD_GET(temp) == DSTS_ENUMSPD_HI)
2982 sc->sc_flags.status_high_speed = 1;
2984 sc->sc_flags.status_high_speed = 0;
2987 * Disable resume and SOF interrupt, and enable
2988 * suspend and RX frame interrupt:
2990 sc->sc_irq_mask &= ~(GINTMSK_WKUPINTMSK | GINTMSK_SOFMSK);
2991 sc->sc_irq_mask |= GINTMSK_USBSUSPMSK;
2992 DWC_OTG_WRITE_4(sc, DOTG_GINTMSK, sc->sc_irq_mask);
2994 /* complete root HUB interrupt endpoint */
2995 dwc_otg_root_intr(sc);
2998 if (status & GINTSTS_PRTINT) {
3001 hprt = DWC_OTG_READ_4(sc, DOTG_HPRT);
3003 /* clear change bits */
3004 DWC_OTG_WRITE_4(sc, DOTG_HPRT, (hprt & (
3005 HPRT_PRTPWR | HPRT_PRTENCHNG |
3006 HPRT_PRTCONNDET | HPRT_PRTOVRCURRCHNG)) |
3009 DPRINTFN(12, "GINTSTS=0x%08x, HPRT=0x%08x\n", status, hprt);
3011 sc->sc_flags.status_device_mode = 0;
3013 if (hprt & HPRT_PRTCONNSTS)
3014 sc->sc_flags.status_bus_reset = 1;
3016 sc->sc_flags.status_bus_reset = 0;
3018 if ((hprt & HPRT_PRTENCHNG) &&
3019 (hprt & HPRT_PRTENA) == 0)
3020 sc->sc_flags.change_enabled = 1;
3022 if (hprt & HPRT_PRTENA)
3023 sc->sc_flags.port_enabled = 1;
3025 sc->sc_flags.port_enabled = 0;
3027 if (hprt & HPRT_PRTOVRCURRCHNG)
3028 sc->sc_flags.change_over_current = 1;
3030 if (hprt & HPRT_PRTOVRCURRACT)
3031 sc->sc_flags.port_over_current = 1;
3033 sc->sc_flags.port_over_current = 0;
3035 if (hprt & HPRT_PRTPWR)
3036 sc->sc_flags.port_powered = 1;
3038 sc->sc_flags.port_powered = 0;
3040 if (((hprt & HPRT_PRTSPD_MASK)
3041 >> HPRT_PRTSPD_SHIFT) == HPRT_PRTSPD_LOW)
3042 sc->sc_flags.status_low_speed = 1;
3044 sc->sc_flags.status_low_speed = 0;
3046 if (((hprt & HPRT_PRTSPD_MASK)
3047 >> HPRT_PRTSPD_SHIFT) == HPRT_PRTSPD_HIGH)
3048 sc->sc_flags.status_high_speed = 1;
3050 sc->sc_flags.status_high_speed = 0;
3052 if (hprt & HPRT_PRTCONNDET)
3053 sc->sc_flags.change_connect = 1;
3055 if (hprt & HPRT_PRTSUSP)
3056 dwc_otg_suspend_irq(sc);
3058 dwc_otg_resume_irq(sc);
3060 /* complete root HUB interrupt endpoint */
3061 dwc_otg_root_intr(sc);
3063 /* update host frame interval */
3064 dwc_otg_update_host_frame_interval(sc);
3068 * If resume and suspend is set at the same time we interpret
3069 * that like RESUME. Resume is set when there is at least 3
3070 * milliseconds of inactivity on the USB BUS.
3072 if (status & GINTSTS_WKUPINT) {
3074 DPRINTFN(5, "resume interrupt\n");
3076 dwc_otg_resume_irq(sc);
3078 } else if (status & GINTSTS_USBSUSP) {
3080 DPRINTFN(5, "suspend interrupt\n");
3082 dwc_otg_suspend_irq(sc);
3085 if (status & (GINTSTS_USBSUSP |
3088 GINTSTS_SESSREQINT)) {
3091 temp = DWC_OTG_READ_4(sc, DOTG_GOTGCTL);
3093 DPRINTFN(5, "GOTGCTL=0x%08x\n", temp);
3095 dwc_otg_vbus_interrupt(sc,
3096 (temp & (GOTGCTL_ASESVLD | GOTGCTL_BSESVLD)) ? 1 : 0);
3099 if (sc->sc_xfer_complete != 0) {
3100 sc->sc_xfer_complete = 0;
3102 /* complete FIFOs, if any */
3103 dwc_otg_interrupt_complete_locked(sc);
3105 USB_BUS_SPIN_UNLOCK(&sc->sc_bus);
3106 USB_BUS_UNLOCK(&sc->sc_bus);
3110 dwc_otg_setup_standard_chain_sub(struct dwc_otg_std_temp *temp)
3112 struct dwc_otg_td *td;
3114 /* get current Transfer Descriptor */
3118 /* prepare for next TD */
3119 temp->td_next = td->obj_next;
3121 /* fill out the Transfer Descriptor */
3122 td->func = temp->func;
3124 td->offset = temp->offset;
3125 td->remainder = temp->len;
3128 td->error_stall = 0;
3130 td->did_stall = temp->did_stall;
3131 td->short_pkt = temp->short_pkt;
3132 td->alt_next = temp->setup_alt_next;
3136 td->channel[0] = DWC_OTG_MAX_CHANNELS;
3137 td->channel[1] = DWC_OTG_MAX_CHANNELS;
3138 td->channel[2] = DWC_OTG_MAX_CHANNELS;
3141 td->tt_scheduled = 0;
3142 td->tt_xactpos = HCSPLT_XACTPOS_BEGIN;
3146 dwc_otg_setup_standard_chain(struct usb_xfer *xfer)
3148 struct dwc_otg_std_temp temp;
3149 struct dwc_otg_td *td;
3154 DPRINTFN(9, "addr=%d endpt=%d sumlen=%d speed=%d\n",
3155 xfer->address, UE_GET_ADDR(xfer->endpointno),
3156 xfer->sumlen, usbd_get_speed(xfer->xroot->udev));
3158 temp.max_frame_size = xfer->max_frame_size;
3160 td = xfer->td_start[0];
3161 xfer->td_transfer_first = td;
3162 xfer->td_transfer_cache = td;
3168 temp.td_next = xfer->td_start[0];
3170 temp.setup_alt_next = xfer->flags_int.short_frames_ok ||
3171 xfer->flags_int.isochronous_xfr;
3172 temp.did_stall = !xfer->flags_int.control_stall;
3174 is_host = (xfer->xroot->udev->flags.usb_mode == USB_MODE_HOST);
3176 /* check if we should prepend a setup message */
3178 if (xfer->flags_int.control_xfr) {
3179 if (xfer->flags_int.control_hdr) {
3182 temp.func = &dwc_otg_host_setup_tx;
3184 temp.func = &dwc_otg_setup_rx;
3186 temp.len = xfer->frlengths[0];
3187 temp.pc = xfer->frbuffers + 0;
3188 temp.short_pkt = temp.len ? 1 : 0;
3190 /* check for last frame */
3191 if (xfer->nframes == 1) {
3192 /* no STATUS stage yet, SETUP is last */
3193 if (xfer->flags_int.control_act)
3194 temp.setup_alt_next = 0;
3197 dwc_otg_setup_standard_chain_sub(&temp);
3204 if (x != xfer->nframes) {
3205 if (xfer->endpointno & UE_DIR_IN) {
3207 temp.func = &dwc_otg_host_data_rx;
3210 temp.func = &dwc_otg_data_tx;
3215 temp.func = &dwc_otg_host_data_tx;
3218 temp.func = &dwc_otg_data_rx;
3223 /* setup "pc" pointer */
3224 temp.pc = xfer->frbuffers + x;
3228 while (x != xfer->nframes) {
3230 /* DATA0 / DATA1 message */
3232 temp.len = xfer->frlengths[x];
3236 if (x == xfer->nframes) {
3237 if (xfer->flags_int.control_xfr) {
3238 if (xfer->flags_int.control_act) {
3239 temp.setup_alt_next = 0;
3242 temp.setup_alt_next = 0;
3245 if (temp.len == 0) {
3247 /* make sure that we send an USB packet */
3253 /* regular data transfer */
3255 temp.short_pkt = (xfer->flags.force_short_xfer ? 0 : 1);
3258 dwc_otg_setup_standard_chain_sub(&temp);
3260 if (xfer->flags_int.isochronous_xfr) {
3261 temp.offset += temp.len;
3263 /* get next Page Cache pointer */
3264 temp.pc = xfer->frbuffers + x;
3268 if (xfer->flags_int.control_xfr) {
3270 /* always setup a valid "pc" pointer for status and sync */
3271 temp.pc = xfer->frbuffers + 0;
3274 temp.setup_alt_next = 0;
3276 /* check if we need to sync */
3278 /* we need a SYNC point after TX */
3279 temp.func = &dwc_otg_data_tx_sync;
3280 dwc_otg_setup_standard_chain_sub(&temp);
3283 /* check if we should append a status stage */
3284 if (!xfer->flags_int.control_act) {
3287 * Send a DATA1 message and invert the current
3288 * endpoint direction.
3290 if (xfer->endpointno & UE_DIR_IN) {
3292 temp.func = &dwc_otg_host_data_tx;
3295 temp.func = &dwc_otg_data_rx;
3300 temp.func = &dwc_otg_host_data_rx;
3303 temp.func = &dwc_otg_data_tx;
3308 dwc_otg_setup_standard_chain_sub(&temp);
3310 /* data toggle should be DATA1 */
3315 /* we need a SYNC point after TX */
3316 temp.func = &dwc_otg_data_tx_sync;
3317 dwc_otg_setup_standard_chain_sub(&temp);
3321 /* check if we need to sync */
3324 temp.pc = xfer->frbuffers + 0;
3327 temp.setup_alt_next = 0;
3329 /* we need a SYNC point after TX */
3330 temp.func = &dwc_otg_data_tx_sync;
3331 dwc_otg_setup_standard_chain_sub(&temp);
3335 /* must have at least one frame! */
3337 xfer->td_transfer_last = td;
3341 struct dwc_otg_softc *sc;
3345 sc = DWC_OTG_BUS2SC(xfer->xroot->bus);
3347 /* get first again */
3348 td = xfer->td_transfer_first;
3349 td->toggle = (xfer->endpoint->toggle_next ? 1 : 0);
3352 (xfer->address << HCCHAR_DEVADDR_SHIFT) |
3353 ((xfer->endpointno & UE_ADDR) << HCCHAR_EPNUM_SHIFT) |
3354 (xfer->max_packet_size << HCCHAR_MPS_SHIFT) |
3358 * We are not always able to meet the timing
3359 * requirements of the USB interrupt endpoint's
3360 * complete split token, when doing transfers going
3361 * via a transaction translator. Use the CONTROL
3362 * transfer type instead of the INTERRUPT transfer
3363 * type in general, as a means to workaround
3364 * that. This trick should work for both FULL and LOW
3365 * speed USB traffic going through a TT. For non-TT
3366 * traffic it works as well. The reason for using
3367 * CONTROL type instead of BULK is that some TTs might
3368 * reject LOW speed BULK traffic.
3370 if (td->ep_type == UE_INTERRUPT)
3371 hcchar |= (UE_CONTROL << HCCHAR_EPTYPE_SHIFT);
3373 hcchar |= (td->ep_type << HCCHAR_EPTYPE_SHIFT);
3375 if (UE_GET_DIR(xfer->endpointno) == UE_DIR_IN)
3376 hcchar |= HCCHAR_EPDIR_IN;
3378 switch (xfer->xroot->udev->speed) {
3380 hcchar |= HCCHAR_LSPDDEV;
3382 case USB_SPEED_FULL:
3383 /* check if root HUB port is running High Speed */
3384 if (dwc_otg_uses_split(xfer->xroot->udev)) {
3385 hcsplt = HCSPLT_SPLTENA |
3386 (xfer->xroot->udev->hs_port_no <<
3387 HCSPLT_PRTADDR_SHIFT) |
3388 (xfer->xroot->udev->hs_hub_addr <<
3389 HCSPLT_HUBADDR_SHIFT);
3393 if (td->ep_type == UE_INTERRUPT) {
3395 ival = xfer->interval / DWC_OTG_HOST_TIMER_RATE;
3398 else if (ival > 127)
3400 td->tmr_val = sc->sc_tmr_val + ival;
3402 } else if (td->ep_type == UE_ISOCHRONOUS) {
3404 td->tmr_val = sc->sc_last_frame_num;
3405 if (td->hcchar & HCCHAR_EPDIR_IN)
3409 td->tmr_res = (uint8_t)sc->sc_last_frame_num;
3412 case USB_SPEED_HIGH:
3414 if (td->ep_type == UE_INTERRUPT) {
3416 hcchar |= ((xfer->max_packet_count & 3)
3417 << HCCHAR_MC_SHIFT);
3418 ival = xfer->interval / DWC_OTG_HOST_TIMER_RATE;
3421 else if (ival > 127)
3423 td->tmr_val = sc->sc_tmr_val + ival;
3425 } else if (td->ep_type == UE_ISOCHRONOUS) {
3426 hcchar |= ((xfer->max_packet_count & 3)
3427 << HCCHAR_MC_SHIFT);
3428 td->tmr_res = 1 << usbd_xfer_get_fps_shift(xfer);
3429 td->tmr_val = sc->sc_last_frame_num;
3430 if (td->hcchar & HCCHAR_EPDIR_IN)
3431 td->tmr_val += td->tmr_res;
3435 td->tmr_res = (uint8_t)sc->sc_last_frame_num;
3445 /* store configuration in all TD's */
3447 td->hcchar = hcchar;
3448 td->hcsplt = hcsplt;
3450 if (((void *)td) == xfer->td_transfer_last)
3459 dwc_otg_timeout(void *arg)
3461 struct usb_xfer *xfer = arg;
3463 DPRINTF("xfer=%p\n", xfer);
3465 USB_BUS_LOCK_ASSERT(xfer->xroot->bus, MA_OWNED);
3467 /* transfer is transferred */
3468 dwc_otg_device_done(xfer, USB_ERR_TIMEOUT);
3472 dwc_otg_start_standard_chain(struct usb_xfer *xfer)
3474 struct dwc_otg_softc *sc = DWC_OTG_BUS2SC(xfer->xroot->bus);
3479 * Poll one time in device mode, which will turn on the
3480 * endpoint interrupts. Else wait for SOF interrupt in host
3483 USB_BUS_SPIN_LOCK(&sc->sc_bus);
3485 if (sc->sc_flags.status_device_mode != 0) {
3486 dwc_otg_xfer_do_fifo(sc, xfer);
3487 if (dwc_otg_xfer_do_complete_locked(sc, xfer))
3490 struct dwc_otg_td *td = xfer->td_transfer_cache;
3491 if (td->ep_type == UE_ISOCHRONOUS &&
3492 (td->hcchar & HCCHAR_EPDIR_IN) == 0) {
3494 * Need to start ISOCHRONOUS OUT transfer ASAP
3495 * because execution is delayed by one 125us
3498 dwc_otg_xfer_do_fifo(sc, xfer);
3499 if (dwc_otg_xfer_do_complete_locked(sc, xfer))
3504 /* put transfer on interrupt queue */
3505 usbd_transfer_enqueue(&xfer->xroot->bus->intr_q, xfer);
3507 /* start timeout, if any */
3508 if (xfer->timeout != 0) {
3509 usbd_transfer_timeout_ms(xfer,
3510 &dwc_otg_timeout, xfer->timeout);
3513 if (sc->sc_flags.status_device_mode != 0)
3516 /* enable SOF interrupt, if any */
3517 dwc_otg_enable_sof_irq(sc);
3519 USB_BUS_SPIN_UNLOCK(&sc->sc_bus);
3523 dwc_otg_root_intr(struct dwc_otg_softc *sc)
3527 USB_BUS_LOCK_ASSERT(&sc->sc_bus, MA_OWNED);
3530 sc->sc_hub_idata[0] = 0x02; /* we only have one port */
3532 uhub_root_intr(&sc->sc_bus, sc->sc_hub_idata,
3533 sizeof(sc->sc_hub_idata));
3537 dwc_otg_standard_done_sub(struct usb_xfer *xfer)
3539 struct dwc_otg_td *td;
3545 td = xfer->td_transfer_cache;
3548 len = td->remainder;
3550 /* store last data toggle */
3551 xfer->endpoint->toggle_next = td->toggle;
3553 if (xfer->aframes != xfer->nframes) {
3555 * Verify the length and subtract
3556 * the remainder from "frlengths[]":
3558 if (len > xfer->frlengths[xfer->aframes]) {
3561 xfer->frlengths[xfer->aframes] -= len;
3564 /* Check for transfer error */
3565 if (td->error_any) {
3566 /* the transfer is finished */
3567 error = (td->error_stall ?
3568 USB_ERR_STALLED : USB_ERR_IOERROR);
3572 /* Check for short transfer */
3574 if (xfer->flags_int.short_frames_ok ||
3575 xfer->flags_int.isochronous_xfr) {
3576 /* follow alt next */
3583 /* the transfer is finished */
3591 /* this USB frame is complete */
3597 /* update transfer cache */
3599 xfer->td_transfer_cache = td;
3605 dwc_otg_standard_done(struct usb_xfer *xfer)
3607 usb_error_t err = 0;
3609 DPRINTFN(13, "xfer=%p endpoint=%p transfer done\n",
3610 xfer, xfer->endpoint);
3614 xfer->td_transfer_cache = xfer->td_transfer_first;
3616 if (xfer->flags_int.control_xfr) {
3618 if (xfer->flags_int.control_hdr) {
3620 err = dwc_otg_standard_done_sub(xfer);
3624 if (xfer->td_transfer_cache == NULL) {
3628 while (xfer->aframes != xfer->nframes) {
3630 err = dwc_otg_standard_done_sub(xfer);
3633 if (xfer->td_transfer_cache == NULL) {
3638 if (xfer->flags_int.control_xfr &&
3639 !xfer->flags_int.control_act) {
3641 err = dwc_otg_standard_done_sub(xfer);
3644 dwc_otg_device_done(xfer, err);
3647 /*------------------------------------------------------------------------*
3648 * dwc_otg_device_done
3650 * NOTE: this function can be called more than one time on the
3651 * same USB transfer!
3652 *------------------------------------------------------------------------*/
3654 dwc_otg_device_done(struct usb_xfer *xfer, usb_error_t error)
3656 struct dwc_otg_softc *sc = DWC_OTG_BUS2SC(xfer->xroot->bus);
3658 DPRINTFN(9, "xfer=%p, endpoint=%p, error=%d\n",
3659 xfer, xfer->endpoint, error);
3661 USB_BUS_SPIN_LOCK(&sc->sc_bus);
3663 if (xfer->flags_int.usb_mode == USB_MODE_DEVICE) {
3664 /* Interrupts are cleared by the interrupt handler */
3666 struct dwc_otg_td *td;
3668 td = xfer->td_transfer_cache;
3670 dwc_otg_host_channel_free(sc, td);
3672 /* dequeue transfer and start next transfer */
3673 usbd_transfer_done(xfer, error);
3675 USB_BUS_SPIN_UNLOCK(&sc->sc_bus);
3679 dwc_otg_xfer_stall(struct usb_xfer *xfer)
3681 dwc_otg_device_done(xfer, USB_ERR_STALLED);
3685 dwc_otg_set_stall(struct usb_device *udev,
3686 struct usb_endpoint *ep, uint8_t *did_stall)
3688 struct dwc_otg_softc *sc;
3693 USB_BUS_LOCK_ASSERT(udev->bus, MA_OWNED);
3696 if (udev->flags.usb_mode != USB_MODE_DEVICE) {
3701 sc = DWC_OTG_BUS2SC(udev->bus);
3703 USB_BUS_SPIN_LOCK(&sc->sc_bus);
3705 /* get endpoint address */
3706 ep_no = ep->edesc->bEndpointAddress;
3708 DPRINTFN(5, "endpoint=0x%x\n", ep_no);
3710 if (ep_no & UE_DIR_IN) {
3711 reg = DOTG_DIEPCTL(ep_no & UE_ADDR);
3712 temp = sc->sc_in_ctl[ep_no & UE_ADDR];
3714 reg = DOTG_DOEPCTL(ep_no & UE_ADDR);
3715 temp = sc->sc_out_ctl[ep_no & UE_ADDR];
3718 /* disable and stall endpoint */
3719 DWC_OTG_WRITE_4(sc, reg, temp | DOEPCTL_EPDIS);
3720 DWC_OTG_WRITE_4(sc, reg, temp | DOEPCTL_STALL);
3722 /* clear active OUT ep */
3723 if (!(ep_no & UE_DIR_IN)) {
3725 sc->sc_active_rx_ep &= ~(1U << (ep_no & UE_ADDR));
3727 if (sc->sc_last_rx_status != 0 &&
3728 (ep_no & UE_ADDR) == GRXSTSRD_CHNUM_GET(
3729 sc->sc_last_rx_status)) {
3731 dwc_otg_common_rx_ack(sc);
3732 /* poll interrupt */
3733 dwc_otg_interrupt_poll_locked(sc);
3734 dwc_otg_interrupt_complete_locked(sc);
3737 USB_BUS_SPIN_UNLOCK(&sc->sc_bus);
3741 dwc_otg_clear_stall_sub_locked(struct dwc_otg_softc *sc, uint32_t mps,
3742 uint8_t ep_no, uint8_t ep_type, uint8_t ep_dir)
3747 if (ep_type == UE_CONTROL) {
3748 /* clearing stall is not needed */
3753 reg = DOTG_DIEPCTL(ep_no);
3755 reg = DOTG_DOEPCTL(ep_no);
3756 sc->sc_active_rx_ep |= (1U << ep_no);
3759 /* round up and mask away the multiplier count */
3760 mps = (mps + 3) & 0x7FC;
3762 if (ep_type == UE_BULK) {
3763 temp = DIEPCTL_EPTYPE_SET(
3764 DIEPCTL_EPTYPE_BULK) |
3766 } else if (ep_type == UE_INTERRUPT) {
3767 temp = DIEPCTL_EPTYPE_SET(
3768 DIEPCTL_EPTYPE_INTERRUPT) |
3771 temp = DIEPCTL_EPTYPE_SET(
3772 DIEPCTL_EPTYPE_ISOC) |
3776 temp |= DIEPCTL_MPS_SET(mps);
3777 temp |= DIEPCTL_TXFNUM_SET(ep_no);
3780 sc->sc_in_ctl[ep_no] = temp;
3782 sc->sc_out_ctl[ep_no] = temp;
3784 DWC_OTG_WRITE_4(sc, reg, temp | DOEPCTL_EPDIS);
3785 DWC_OTG_WRITE_4(sc, reg, temp | DOEPCTL_SETD0PID);
3786 DWC_OTG_WRITE_4(sc, reg, temp | DIEPCTL_SNAK);
3788 /* we only reset the transmit FIFO */
3790 dwc_otg_tx_fifo_reset(sc,
3791 GRSTCTL_TXFIFO(ep_no) |
3795 DOTG_DIEPTSIZ(ep_no), 0);
3798 /* poll interrupt */
3799 dwc_otg_interrupt_poll_locked(sc);
3800 dwc_otg_interrupt_complete_locked(sc);
3804 dwc_otg_clear_stall(struct usb_device *udev, struct usb_endpoint *ep)
3806 struct dwc_otg_softc *sc;
3807 struct usb_endpoint_descriptor *ed;
3809 DPRINTFN(5, "endpoint=%p\n", ep);
3811 USB_BUS_LOCK_ASSERT(udev->bus, MA_OWNED);
3814 if (udev->flags.usb_mode != USB_MODE_DEVICE) {
3819 sc = DWC_OTG_BUS2SC(udev->bus);
3821 USB_BUS_SPIN_LOCK(&sc->sc_bus);
3823 /* get endpoint descriptor */
3826 /* reset endpoint */
3827 dwc_otg_clear_stall_sub_locked(sc,
3828 UGETW(ed->wMaxPacketSize),
3829 (ed->bEndpointAddress & UE_ADDR),
3830 (ed->bmAttributes & UE_XFERTYPE),
3831 (ed->bEndpointAddress & (UE_DIR_IN | UE_DIR_OUT)));
3833 USB_BUS_SPIN_UNLOCK(&sc->sc_bus);
3837 dwc_otg_device_state_change(struct usb_device *udev)
3839 struct dwc_otg_softc *sc;
3843 if (udev->flags.usb_mode != USB_MODE_DEVICE) {
3849 sc = DWC_OTG_BUS2SC(udev->bus);
3851 /* deactivate all other endpoint but the control endpoint */
3852 if (udev->state == USB_STATE_CONFIGURED ||
3853 udev->state == USB_STATE_ADDRESSED) {
3855 USB_BUS_LOCK(&sc->sc_bus);
3857 for (x = 1; x != sc->sc_dev_ep_max; x++) {
3859 if (x < sc->sc_dev_in_ep_max) {
3860 DWC_OTG_WRITE_4(sc, DOTG_DIEPCTL(x),
3862 DWC_OTG_WRITE_4(sc, DOTG_DIEPCTL(x), 0);
3865 DWC_OTG_WRITE_4(sc, DOTG_DOEPCTL(x),
3867 DWC_OTG_WRITE_4(sc, DOTG_DOEPCTL(x), 0);
3869 USB_BUS_UNLOCK(&sc->sc_bus);
3874 dwc_otg_init(struct dwc_otg_softc *sc)
3881 sc->sc_io_tag = rman_get_bustag(sc->sc_io_res);
3882 sc->sc_io_hdl = rman_get_bushandle(sc->sc_io_res);
3883 sc->sc_io_size = rman_get_size(sc->sc_io_res);
3885 /* set up the bus structure */
3886 sc->sc_bus.devices = sc->sc_devices;
3887 sc->sc_bus.devices_max = DWC_OTG_MAX_DEVICES;
3888 sc->sc_bus.dma_bits = 32;
3889 sc->sc_bus.usbrev = USB_REV_2_0;
3890 sc->sc_bus.methods = &dwc_otg_bus_methods;
3892 /* get all DMA memory */
3893 if (usb_bus_mem_alloc_all(&sc->sc_bus,
3894 USB_GET_DMA_TAG(sc->sc_bus.parent), NULL)) {
3898 sc->sc_bus.bdev = device_add_child(sc->sc_bus.parent, "usbus", -1);
3899 if (sc->sc_bus.bdev == NULL)
3902 device_set_ivars(sc->sc_bus.bdev, &sc->sc_bus);
3904 err = bus_setup_intr(sc->sc_bus.parent, sc->sc_irq_res,
3905 INTR_TYPE_TTY | INTR_MPSAFE, &dwc_otg_filter_interrupt,
3906 &dwc_otg_interrupt, sc, &sc->sc_intr_hdl);
3908 sc->sc_intr_hdl = NULL;
3912 usb_callout_init_mtx(&sc->sc_timer,
3913 &sc->sc_bus.bus_mtx, 0);
3915 USB_BUS_LOCK(&sc->sc_bus);
3917 /* turn on clocks */
3918 dwc_otg_clocks_on(sc);
3920 temp = DWC_OTG_READ_4(sc, DOTG_GSNPSID);
3921 DPRINTF("Version = 0x%08x\n", temp);
3924 DWC_OTG_WRITE_4(sc, DOTG_DCTL,
3927 /* wait for host to detect disconnect */
3928 usb_pause_mtx(&sc->sc_bus.bus_mtx, hz / 32);
3930 DWC_OTG_WRITE_4(sc, DOTG_GRSTCTL,
3933 /* wait a little bit for block to reset */
3934 usb_pause_mtx(&sc->sc_bus.bus_mtx, hz / 128);
3936 switch (sc->sc_mode) {
3937 case DWC_MODE_DEVICE:
3938 temp = GUSBCFG_FORCEDEVMODE;
3941 temp = GUSBCFG_FORCEHOSTMODE;
3948 if (sc->sc_phy_type == 0)
3949 sc->sc_phy_type = dwc_otg_phy_type + 1;
3950 if (sc->sc_phy_bits == 0)
3951 sc->sc_phy_bits = 16;
3953 /* select HSIC, ULPI, UTMI+ or internal PHY mode */
3954 switch (sc->sc_phy_type) {
3955 case DWC_OTG_PHY_HSIC:
3956 DWC_OTG_WRITE_4(sc, DOTG_GUSBCFG,
3958 GUSBCFG_TRD_TIM_SET(5) | temp);
3959 DWC_OTG_WRITE_4(sc, DOTG_GOTGCTL,
3962 temp = DWC_OTG_READ_4(sc, DOTG_GLPMCFG);
3963 DWC_OTG_WRITE_4(sc, DOTG_GLPMCFG,
3964 temp & ~GLPMCFG_HSIC_CONN);
3965 DWC_OTG_WRITE_4(sc, DOTG_GLPMCFG,
3966 temp | GLPMCFG_HSIC_CONN);
3968 case DWC_OTG_PHY_ULPI:
3969 DWC_OTG_WRITE_4(sc, DOTG_GUSBCFG,
3970 GUSBCFG_ULPI_UTMI_SEL |
3971 GUSBCFG_TRD_TIM_SET(5) | temp);
3972 DWC_OTG_WRITE_4(sc, DOTG_GOTGCTL, 0);
3974 temp = DWC_OTG_READ_4(sc, DOTG_GLPMCFG);
3975 DWC_OTG_WRITE_4(sc, DOTG_GLPMCFG,
3976 temp & ~GLPMCFG_HSIC_CONN);
3978 case DWC_OTG_PHY_UTMI:
3979 DWC_OTG_WRITE_4(sc, DOTG_GUSBCFG,
3980 (sc->sc_phy_bits == 16 ? GUSBCFG_PHYIF : 0) |
3981 GUSBCFG_TRD_TIM_SET(5) | temp);
3982 DWC_OTG_WRITE_4(sc, DOTG_GOTGCTL, 0);
3984 temp = DWC_OTG_READ_4(sc, DOTG_GLPMCFG);
3985 DWC_OTG_WRITE_4(sc, DOTG_GLPMCFG,
3986 temp & ~GLPMCFG_HSIC_CONN);
3988 case DWC_OTG_PHY_INTERNAL:
3989 DWC_OTG_WRITE_4(sc, DOTG_GUSBCFG,
3991 GUSBCFG_TRD_TIM_SET(5) | temp);
3992 DWC_OTG_WRITE_4(sc, DOTG_GOTGCTL, 0);
3994 temp = DWC_OTG_READ_4(sc, DOTG_GLPMCFG);
3995 DWC_OTG_WRITE_4(sc, DOTG_GLPMCFG,
3996 temp & ~GLPMCFG_HSIC_CONN);
3998 temp = DWC_OTG_READ_4(sc, DOTG_GGPIO);
3999 temp &= ~(DOTG_GGPIO_NOVBUSSENS | DOTG_GGPIO_I2CPADEN);
4000 temp |= (DOTG_GGPIO_VBUSASEN | DOTG_GGPIO_VBUSBSEN |
4002 DWC_OTG_WRITE_4(sc, DOTG_GGPIO, temp);
4008 /* clear global nak */
4009 DWC_OTG_WRITE_4(sc, DOTG_DCTL,
4013 /* disable USB port */
4014 DWC_OTG_WRITE_4(sc, DOTG_PCGCCTL, 0xFFFFFFFF);
4017 usb_pause_mtx(&sc->sc_bus.bus_mtx, hz / 100);
4019 /* enable USB port */
4020 DWC_OTG_WRITE_4(sc, DOTG_PCGCCTL, 0);
4023 usb_pause_mtx(&sc->sc_bus.bus_mtx, hz / 100);
4025 temp = DWC_OTG_READ_4(sc, DOTG_GHWCFG3);
4027 sc->sc_fifo_size = 4 * GHWCFG3_DFIFODEPTH_GET(temp);
4029 temp = DWC_OTG_READ_4(sc, DOTG_GHWCFG2);
4031 sc->sc_dev_ep_max = GHWCFG2_NUMDEVEPS_GET(temp);
4033 if (sc->sc_dev_ep_max > DWC_OTG_MAX_ENDPOINTS)
4034 sc->sc_dev_ep_max = DWC_OTG_MAX_ENDPOINTS;
4036 sc->sc_host_ch_max = GHWCFG2_NUMHSTCHNL_GET(temp);
4038 if (sc->sc_host_ch_max > DWC_OTG_MAX_CHANNELS)
4039 sc->sc_host_ch_max = DWC_OTG_MAX_CHANNELS;
4041 temp = DWC_OTG_READ_4(sc, DOTG_GHWCFG4);
4043 sc->sc_dev_in_ep_max = GHWCFG4_NUM_IN_EP_GET(temp);
4045 DPRINTF("Total FIFO size = %d bytes, Device EPs = %d/%d Host CHs = %d\n",
4046 sc->sc_fifo_size, sc->sc_dev_ep_max, sc->sc_dev_in_ep_max,
4047 sc->sc_host_ch_max);
4050 if (dwc_otg_init_fifo(sc, sc->sc_mode)) {
4051 USB_BUS_UNLOCK(&sc->sc_bus);
4055 /* enable interrupts */
4056 sc->sc_irq_mask |= DWC_OTG_MSK_GINT_THREAD_IRQ;
4057 DWC_OTG_WRITE_4(sc, DOTG_GINTMSK, sc->sc_irq_mask);
4059 if (sc->sc_mode == DWC_MODE_OTG || sc->sc_mode == DWC_MODE_DEVICE) {
4061 /* enable all endpoint interrupts */
4062 temp = DWC_OTG_READ_4(sc, DOTG_GHWCFG2);
4063 if (temp & GHWCFG2_MPI) {
4066 DPRINTF("Disable Multi Process Interrupts\n");
4068 for (x = 0; x != sc->sc_dev_in_ep_max; x++) {
4069 DWC_OTG_WRITE_4(sc, DOTG_DIEPEACHINTMSK(x), 0);
4070 DWC_OTG_WRITE_4(sc, DOTG_DOEPEACHINTMSK(x), 0);
4072 DWC_OTG_WRITE_4(sc, DOTG_DEACHINTMSK, 0);
4074 DWC_OTG_WRITE_4(sc, DOTG_DIEPMSK,
4075 DIEPMSK_XFERCOMPLMSK);
4076 DWC_OTG_WRITE_4(sc, DOTG_DOEPMSK, 0);
4077 DWC_OTG_WRITE_4(sc, DOTG_DAINTMSK, 0xFFFF);
4080 if (sc->sc_mode == DWC_MODE_OTG || sc->sc_mode == DWC_MODE_HOST) {
4082 temp = DWC_OTG_READ_4(sc, DOTG_HCFG);
4083 temp &= ~(HCFG_FSLSSUPP | HCFG_FSLSPCLKSEL_MASK);
4084 temp |= (1 << HCFG_FSLSPCLKSEL_SHIFT);
4085 DWC_OTG_WRITE_4(sc, DOTG_HCFG, temp);
4088 /* only enable global IRQ */
4089 DWC_OTG_WRITE_4(sc, DOTG_GAHBCFG,
4090 GAHBCFG_GLBLINTRMSK);
4092 /* turn off clocks */
4093 dwc_otg_clocks_off(sc);
4095 /* read initial VBUS state */
4097 temp = DWC_OTG_READ_4(sc, DOTG_GOTGCTL);
4099 DPRINTFN(5, "GOTCTL=0x%08x\n", temp);
4101 dwc_otg_vbus_interrupt(sc,
4102 (temp & (GOTGCTL_ASESVLD | GOTGCTL_BSESVLD)) ? 1 : 0);
4104 USB_BUS_UNLOCK(&sc->sc_bus);
4106 /* catch any lost interrupts */
4108 dwc_otg_do_poll(&sc->sc_bus);
4110 return (0); /* success */
4114 dwc_otg_uninit(struct dwc_otg_softc *sc)
4116 USB_BUS_LOCK(&sc->sc_bus);
4118 /* stop host timer */
4119 dwc_otg_timer_stop(sc);
4121 /* set disconnect */
4122 DWC_OTG_WRITE_4(sc, DOTG_DCTL,
4125 /* turn off global IRQ */
4126 DWC_OTG_WRITE_4(sc, DOTG_GAHBCFG, 0);
4128 sc->sc_flags.port_enabled = 0;
4129 sc->sc_flags.port_powered = 0;
4130 sc->sc_flags.status_vbus = 0;
4131 sc->sc_flags.status_bus_reset = 0;
4132 sc->sc_flags.status_suspend = 0;
4133 sc->sc_flags.change_suspend = 0;
4134 sc->sc_flags.change_connect = 1;
4136 dwc_otg_pull_down(sc);
4137 dwc_otg_clocks_off(sc);
4139 USB_BUS_UNLOCK(&sc->sc_bus);
4141 usb_callout_drain(&sc->sc_timer);
4145 dwc_otg_suspend(struct dwc_otg_softc *sc)
4151 dwc_otg_resume(struct dwc_otg_softc *sc)
4157 dwc_otg_do_poll(struct usb_bus *bus)
4159 struct dwc_otg_softc *sc = DWC_OTG_BUS2SC(bus);
4161 USB_BUS_LOCK(&sc->sc_bus);
4162 USB_BUS_SPIN_LOCK(&sc->sc_bus);
4163 dwc_otg_interrupt_poll_locked(sc);
4164 dwc_otg_interrupt_complete_locked(sc);
4165 USB_BUS_SPIN_UNLOCK(&sc->sc_bus);
4166 USB_BUS_UNLOCK(&sc->sc_bus);
4169 /*------------------------------------------------------------------------*
4170 * DWC OTG bulk support
4171 * DWC OTG control support
4172 * DWC OTG interrupt support
4173 *------------------------------------------------------------------------*/
4175 dwc_otg_device_non_isoc_open(struct usb_xfer *xfer)
4180 dwc_otg_device_non_isoc_close(struct usb_xfer *xfer)
4182 dwc_otg_device_done(xfer, USB_ERR_CANCELLED);
4186 dwc_otg_device_non_isoc_enter(struct usb_xfer *xfer)
4191 dwc_otg_device_non_isoc_start(struct usb_xfer *xfer)
4194 dwc_otg_setup_standard_chain(xfer);
4195 dwc_otg_start_standard_chain(xfer);
4198 static const struct usb_pipe_methods dwc_otg_device_non_isoc_methods =
4200 .open = dwc_otg_device_non_isoc_open,
4201 .close = dwc_otg_device_non_isoc_close,
4202 .enter = dwc_otg_device_non_isoc_enter,
4203 .start = dwc_otg_device_non_isoc_start,
4206 /*------------------------------------------------------------------------*
4207 * DWC OTG full speed isochronous support
4208 *------------------------------------------------------------------------*/
4210 dwc_otg_device_isoc_open(struct usb_xfer *xfer)
4215 dwc_otg_device_isoc_close(struct usb_xfer *xfer)
4217 dwc_otg_device_done(xfer, USB_ERR_CANCELLED);
4221 dwc_otg_device_isoc_enter(struct usb_xfer *xfer)
4226 dwc_otg_device_isoc_start(struct usb_xfer *xfer)
4228 struct dwc_otg_softc *sc = DWC_OTG_BUS2SC(xfer->xroot->bus);
4232 uint8_t shift = usbd_xfer_get_fps_shift(xfer);
4234 DPRINTFN(6, "xfer=%p next=%d nframes=%d\n",
4235 xfer, xfer->endpoint->isoc_next, xfer->nframes);
4237 if (xfer->xroot->udev->flags.usb_mode == USB_MODE_HOST) {
4238 temp = DWC_OTG_READ_4(sc, DOTG_HFNUM);
4240 /* get the current frame index */
4241 framenum = (temp & HFNUM_FRNUM_MASK);
4243 temp = DWC_OTG_READ_4(sc, DOTG_DSTS);
4245 /* get the current frame index */
4246 framenum = DSTS_SOFFN_GET(temp);
4250 * Check if port is doing 8000 or 1000 frames per second:
4252 if (sc->sc_flags.status_high_speed)
4255 framenum &= DWC_OTG_FRAME_MASK;
4258 * Compute number of milliseconds worth of data traffic for
4259 * this USB transfer:
4261 if (xfer->xroot->udev->speed == USB_SPEED_HIGH)
4262 msframes = ((xfer->nframes << shift) + 7) / 8;
4264 msframes = xfer->nframes;
4267 * check if the frame index is within the window where the frames
4270 temp = (framenum - xfer->endpoint->isoc_next) & DWC_OTG_FRAME_MASK;
4272 if ((xfer->endpoint->is_synced == 0) || (temp < msframes)) {
4274 * If there is data underflow or the pipe queue is
4275 * empty we schedule the transfer a few frames ahead
4276 * of the current frame position. Else two isochronous
4277 * transfers might overlap.
4279 xfer->endpoint->isoc_next = (framenum + 3) & DWC_OTG_FRAME_MASK;
4280 xfer->endpoint->is_synced = 1;
4281 DPRINTFN(3, "start next=%d\n", xfer->endpoint->isoc_next);
4284 * compute how many milliseconds the insertion is ahead of the
4285 * current frame position:
4287 temp = (xfer->endpoint->isoc_next - framenum) & DWC_OTG_FRAME_MASK;
4290 * pre-compute when the isochronous transfer will be finished:
4292 xfer->isoc_time_complete =
4293 usb_isoc_time_expand(&sc->sc_bus, framenum) + temp + msframes;
4296 dwc_otg_setup_standard_chain(xfer);
4298 /* compute frame number for next insertion */
4299 xfer->endpoint->isoc_next += msframes;
4301 /* start TD chain */
4302 dwc_otg_start_standard_chain(xfer);
4305 static const struct usb_pipe_methods dwc_otg_device_isoc_methods =
4307 .open = dwc_otg_device_isoc_open,
4308 .close = dwc_otg_device_isoc_close,
4309 .enter = dwc_otg_device_isoc_enter,
4310 .start = dwc_otg_device_isoc_start,
4313 /*------------------------------------------------------------------------*
4314 * DWC OTG root control support
4315 *------------------------------------------------------------------------*
4316 * Simulate a hardware HUB by handling all the necessary requests.
4317 *------------------------------------------------------------------------*/
4319 static const struct usb_device_descriptor dwc_otg_devd = {
4320 .bLength = sizeof(struct usb_device_descriptor),
4321 .bDescriptorType = UDESC_DEVICE,
4322 .bcdUSB = {0x00, 0x02},
4323 .bDeviceClass = UDCLASS_HUB,
4324 .bDeviceSubClass = UDSUBCLASS_HUB,
4325 .bDeviceProtocol = UDPROTO_HSHUBSTT,
4326 .bMaxPacketSize = 64,
4327 .bcdDevice = {0x00, 0x01},
4330 .bNumConfigurations = 1,
4333 static const struct dwc_otg_config_desc dwc_otg_confd = {
4335 .bLength = sizeof(struct usb_config_descriptor),
4336 .bDescriptorType = UDESC_CONFIG,
4337 .wTotalLength[0] = sizeof(dwc_otg_confd),
4339 .bConfigurationValue = 1,
4340 .iConfiguration = 0,
4341 .bmAttributes = UC_SELF_POWERED,
4345 .bLength = sizeof(struct usb_interface_descriptor),
4346 .bDescriptorType = UDESC_INTERFACE,
4348 .bInterfaceClass = UICLASS_HUB,
4349 .bInterfaceSubClass = UISUBCLASS_HUB,
4350 .bInterfaceProtocol = 0,
4353 .bLength = sizeof(struct usb_endpoint_descriptor),
4354 .bDescriptorType = UDESC_ENDPOINT,
4355 .bEndpointAddress = (UE_DIR_IN | DWC_OTG_INTR_ENDPT),
4356 .bmAttributes = UE_INTERRUPT,
4357 .wMaxPacketSize[0] = 8,
4362 #define HSETW(ptr, val) ptr = { (uint8_t)(val), (uint8_t)((val) >> 8) }
4364 static const struct usb_hub_descriptor_min dwc_otg_hubd = {
4365 .bDescLength = sizeof(dwc_otg_hubd),
4366 .bDescriptorType = UDESC_HUB,
4368 HSETW(.wHubCharacteristics, (UHD_PWR_NO_SWITCH | UHD_OC_INDIVIDUAL)),
4369 .bPwrOn2PwrGood = 50,
4370 .bHubContrCurrent = 0,
4371 .DeviceRemovable = {0}, /* port is removable */
4374 #define STRING_VENDOR \
4377 #define STRING_PRODUCT \
4378 "O\0T\0G\0 \0R\0o\0o\0t\0 \0H\0U\0B"
4380 USB_MAKE_STRING_DESC(STRING_VENDOR, dwc_otg_vendor);
4381 USB_MAKE_STRING_DESC(STRING_PRODUCT, dwc_otg_product);
4384 dwc_otg_roothub_exec(struct usb_device *udev,
4385 struct usb_device_request *req, const void **pptr, uint16_t *plength)
4387 struct dwc_otg_softc *sc = DWC_OTG_BUS2SC(udev->bus);
4394 USB_BUS_LOCK_ASSERT(&sc->sc_bus, MA_OWNED);
4397 ptr = (const void *)&sc->sc_hub_temp;
4401 value = UGETW(req->wValue);
4402 index = UGETW(req->wIndex);
4404 /* demultiplex the control request */
4406 switch (req->bmRequestType) {
4407 case UT_READ_DEVICE:
4408 switch (req->bRequest) {
4409 case UR_GET_DESCRIPTOR:
4410 goto tr_handle_get_descriptor;
4412 goto tr_handle_get_config;
4414 goto tr_handle_get_status;
4420 case UT_WRITE_DEVICE:
4421 switch (req->bRequest) {
4422 case UR_SET_ADDRESS:
4423 goto tr_handle_set_address;
4425 goto tr_handle_set_config;
4426 case UR_CLEAR_FEATURE:
4427 goto tr_valid; /* nop */
4428 case UR_SET_DESCRIPTOR:
4429 goto tr_valid; /* nop */
4430 case UR_SET_FEATURE:
4436 case UT_WRITE_ENDPOINT:
4437 switch (req->bRequest) {
4438 case UR_CLEAR_FEATURE:
4439 switch (UGETW(req->wValue)) {
4440 case UF_ENDPOINT_HALT:
4441 goto tr_handle_clear_halt;
4442 case UF_DEVICE_REMOTE_WAKEUP:
4443 goto tr_handle_clear_wakeup;
4448 case UR_SET_FEATURE:
4449 switch (UGETW(req->wValue)) {
4450 case UF_ENDPOINT_HALT:
4451 goto tr_handle_set_halt;
4452 case UF_DEVICE_REMOTE_WAKEUP:
4453 goto tr_handle_set_wakeup;
4458 case UR_SYNCH_FRAME:
4459 goto tr_valid; /* nop */
4465 case UT_READ_ENDPOINT:
4466 switch (req->bRequest) {
4468 goto tr_handle_get_ep_status;
4474 case UT_WRITE_INTERFACE:
4475 switch (req->bRequest) {
4476 case UR_SET_INTERFACE:
4477 goto tr_handle_set_interface;
4478 case UR_CLEAR_FEATURE:
4479 goto tr_valid; /* nop */
4480 case UR_SET_FEATURE:
4486 case UT_READ_INTERFACE:
4487 switch (req->bRequest) {
4488 case UR_GET_INTERFACE:
4489 goto tr_handle_get_interface;
4491 goto tr_handle_get_iface_status;
4497 case UT_WRITE_CLASS_INTERFACE:
4498 case UT_WRITE_VENDOR_INTERFACE:
4502 case UT_READ_CLASS_INTERFACE:
4503 case UT_READ_VENDOR_INTERFACE:
4507 case UT_WRITE_CLASS_DEVICE:
4508 switch (req->bRequest) {
4509 case UR_CLEAR_FEATURE:
4511 case UR_SET_DESCRIPTOR:
4512 case UR_SET_FEATURE:
4519 case UT_WRITE_CLASS_OTHER:
4520 switch (req->bRequest) {
4521 case UR_CLEAR_FEATURE:
4522 goto tr_handle_clear_port_feature;
4523 case UR_SET_FEATURE:
4524 goto tr_handle_set_port_feature;
4525 case UR_CLEAR_TT_BUFFER:
4535 case UT_READ_CLASS_OTHER:
4536 switch (req->bRequest) {
4537 case UR_GET_TT_STATE:
4538 goto tr_handle_get_tt_state;
4540 goto tr_handle_get_port_status;
4546 case UT_READ_CLASS_DEVICE:
4547 switch (req->bRequest) {
4548 case UR_GET_DESCRIPTOR:
4549 goto tr_handle_get_class_descriptor;
4551 goto tr_handle_get_class_status;
4562 tr_handle_get_descriptor:
4563 switch (value >> 8) {
4568 len = sizeof(dwc_otg_devd);
4569 ptr = (const void *)&dwc_otg_devd;
4575 len = sizeof(dwc_otg_confd);
4576 ptr = (const void *)&dwc_otg_confd;
4579 switch (value & 0xff) {
4580 case 0: /* Language table */
4581 len = sizeof(usb_string_lang_en);
4582 ptr = (const void *)&usb_string_lang_en;
4585 case 1: /* Vendor */
4586 len = sizeof(dwc_otg_vendor);
4587 ptr = (const void *)&dwc_otg_vendor;
4590 case 2: /* Product */
4591 len = sizeof(dwc_otg_product);
4592 ptr = (const void *)&dwc_otg_product;
4603 tr_handle_get_config:
4605 sc->sc_hub_temp.wValue[0] = sc->sc_conf;
4608 tr_handle_get_status:
4610 USETW(sc->sc_hub_temp.wValue, UDS_SELF_POWERED);
4613 tr_handle_set_address:
4614 if (value & 0xFF00) {
4617 sc->sc_rt_addr = value;
4620 tr_handle_set_config:
4624 sc->sc_conf = value;
4627 tr_handle_get_interface:
4629 sc->sc_hub_temp.wValue[0] = 0;
4632 tr_handle_get_tt_state:
4633 tr_handle_get_class_status:
4634 tr_handle_get_iface_status:
4635 tr_handle_get_ep_status:
4637 USETW(sc->sc_hub_temp.wValue, 0);
4641 tr_handle_set_interface:
4642 tr_handle_set_wakeup:
4643 tr_handle_clear_wakeup:
4644 tr_handle_clear_halt:
4647 tr_handle_clear_port_feature:
4651 DPRINTFN(9, "UR_CLEAR_PORT_FEATURE on port %d\n", index);
4654 case UHF_PORT_SUSPEND:
4655 dwc_otg_wakeup_peer(sc);
4658 case UHF_PORT_ENABLE:
4659 if (sc->sc_flags.status_device_mode == 0) {
4660 DWC_OTG_WRITE_4(sc, DOTG_HPRT,
4661 sc->sc_hprt_val | HPRT_PRTENA);
4663 sc->sc_flags.port_enabled = 0;
4666 case UHF_C_PORT_RESET:
4667 sc->sc_flags.change_reset = 0;
4670 case UHF_C_PORT_ENABLE:
4671 sc->sc_flags.change_enabled = 0;
4674 case UHF_C_PORT_OVER_CURRENT:
4675 sc->sc_flags.change_over_current = 0;
4679 case UHF_PORT_INDICATOR:
4683 case UHF_PORT_POWER:
4684 sc->sc_flags.port_powered = 0;
4685 if (sc->sc_mode == DWC_MODE_HOST || sc->sc_mode == DWC_MODE_OTG) {
4686 sc->sc_hprt_val = 0;
4687 DWC_OTG_WRITE_4(sc, DOTG_HPRT, HPRT_PRTENA);
4689 dwc_otg_pull_down(sc);
4690 dwc_otg_clocks_off(sc);
4693 case UHF_C_PORT_CONNECTION:
4694 /* clear connect change flag */
4695 sc->sc_flags.change_connect = 0;
4698 case UHF_C_PORT_SUSPEND:
4699 sc->sc_flags.change_suspend = 0;
4703 err = USB_ERR_IOERROR;
4708 tr_handle_set_port_feature:
4712 DPRINTFN(9, "UR_SET_PORT_FEATURE\n");
4715 case UHF_PORT_ENABLE:
4718 case UHF_PORT_SUSPEND:
4719 if (sc->sc_flags.status_device_mode == 0) {
4720 /* set suspend BIT */
4721 sc->sc_hprt_val |= HPRT_PRTSUSP;
4722 DWC_OTG_WRITE_4(sc, DOTG_HPRT, sc->sc_hprt_val);
4724 /* generate HUB suspend event */
4725 dwc_otg_suspend_irq(sc);
4729 case UHF_PORT_RESET:
4730 if (sc->sc_flags.status_device_mode == 0) {
4732 DPRINTF("PORT RESET\n");
4734 /* enable PORT reset */
4735 DWC_OTG_WRITE_4(sc, DOTG_HPRT, sc->sc_hprt_val | HPRT_PRTRST);
4737 /* Wait 62.5ms for reset to complete */
4738 usb_pause_mtx(&sc->sc_bus.bus_mtx, hz / 16);
4740 DWC_OTG_WRITE_4(sc, DOTG_HPRT, sc->sc_hprt_val);
4742 /* Wait 62.5ms for reset to complete */
4743 usb_pause_mtx(&sc->sc_bus.bus_mtx, hz / 16);
4746 (void) dwc_otg_init_fifo(sc, DWC_MODE_HOST);
4748 sc->sc_flags.change_reset = 1;
4750 err = USB_ERR_IOERROR;
4755 case UHF_PORT_INDICATOR:
4758 case UHF_PORT_POWER:
4759 sc->sc_flags.port_powered = 1;
4760 if (sc->sc_mode == DWC_MODE_HOST || sc->sc_mode == DWC_MODE_OTG) {
4761 sc->sc_hprt_val |= HPRT_PRTPWR;
4762 DWC_OTG_WRITE_4(sc, DOTG_HPRT, sc->sc_hprt_val);
4764 if (sc->sc_mode == DWC_MODE_DEVICE || sc->sc_mode == DWC_MODE_OTG) {
4765 /* pull up D+, if any */
4766 dwc_otg_pull_up(sc);
4770 err = USB_ERR_IOERROR;
4775 tr_handle_get_port_status:
4777 DPRINTFN(9, "UR_GET_PORT_STATUS\n");
4782 if (sc->sc_flags.status_vbus)
4783 dwc_otg_clocks_on(sc);
4785 dwc_otg_clocks_off(sc);
4787 /* Select Device Side Mode */
4789 if (sc->sc_flags.status_device_mode) {
4790 value = UPS_PORT_MODE_DEVICE;
4791 dwc_otg_timer_stop(sc);
4794 dwc_otg_timer_start(sc);
4797 if (sc->sc_flags.status_high_speed)
4798 value |= UPS_HIGH_SPEED;
4799 else if (sc->sc_flags.status_low_speed)
4800 value |= UPS_LOW_SPEED;
4802 if (sc->sc_flags.port_powered)
4803 value |= UPS_PORT_POWER;
4805 if (sc->sc_flags.port_enabled)
4806 value |= UPS_PORT_ENABLED;
4808 if (sc->sc_flags.port_over_current)
4809 value |= UPS_OVERCURRENT_INDICATOR;
4811 if (sc->sc_flags.status_vbus &&
4812 sc->sc_flags.status_bus_reset)
4813 value |= UPS_CURRENT_CONNECT_STATUS;
4815 if (sc->sc_flags.status_suspend)
4816 value |= UPS_SUSPEND;
4818 USETW(sc->sc_hub_temp.ps.wPortStatus, value);
4822 if (sc->sc_flags.change_enabled)
4823 value |= UPS_C_PORT_ENABLED;
4824 if (sc->sc_flags.change_connect)
4825 value |= UPS_C_CONNECT_STATUS;
4826 if (sc->sc_flags.change_suspend)
4827 value |= UPS_C_SUSPEND;
4828 if (sc->sc_flags.change_reset)
4829 value |= UPS_C_PORT_RESET;
4830 if (sc->sc_flags.change_over_current)
4831 value |= UPS_C_OVERCURRENT_INDICATOR;
4833 USETW(sc->sc_hub_temp.ps.wPortChange, value);
4834 len = sizeof(sc->sc_hub_temp.ps);
4837 tr_handle_get_class_descriptor:
4841 ptr = (const void *)&dwc_otg_hubd;
4842 len = sizeof(dwc_otg_hubd);
4846 err = USB_ERR_STALLED;
4855 dwc_otg_xfer_setup(struct usb_setup_params *parm)
4857 struct usb_xfer *xfer;
4864 xfer = parm->curr_xfer;
4867 * NOTE: This driver does not use any of the parameters that
4868 * are computed from the following values. Just set some
4869 * reasonable dummies:
4871 parm->hc_max_packet_size = 0x500;
4872 parm->hc_max_packet_count = 3;
4873 parm->hc_max_frame_size = 3 * 0x500;
4875 usbd_transfer_setup_sub(parm);
4878 * compute maximum number of TDs
4880 ep_type = (xfer->endpoint->edesc->bmAttributes & UE_XFERTYPE);
4882 if (ep_type == UE_CONTROL) {
4884 ntd = xfer->nframes + 1 /* STATUS */ + 1 /* SYNC 1 */
4885 + 1 /* SYNC 2 */ + 1 /* SYNC 3 */;
4888 ntd = xfer->nframes + 1 /* SYNC */ ;
4892 * check if "usbd_transfer_setup_sub" set an error
4898 * allocate transfer descriptors
4902 ep_no = xfer->endpointno & UE_ADDR;
4905 * Check for a valid endpoint profile in USB device mode:
4907 if (xfer->flags_int.usb_mode == USB_MODE_DEVICE) {
4908 const struct usb_hw_ep_profile *pf;
4910 dwc_otg_get_hw_ep_profile(parm->udev, &pf, ep_no);
4913 /* should not happen */
4914 parm->err = USB_ERR_INVAL;
4920 parm->size[0] += ((-parm->size[0]) & (USB_HOST_ALIGN - 1));
4922 for (n = 0; n != ntd; n++) {
4924 struct dwc_otg_td *td;
4928 td = USB_ADD_BYTES(parm->buf, parm->size[0]);
4930 /* compute shared bandwidth resource index for TT */
4931 if (dwc_otg_uses_split(parm->udev)) {
4932 if (parm->udev->parent_hs_hub->ddesc.bDeviceProtocol == UDPROTO_HSHUBMTT)
4933 td->tt_index = parm->udev->device_index;
4935 td->tt_index = parm->udev->parent_hs_hub->device_index;
4937 td->tt_index = parm->udev->device_index;
4941 td->max_packet_size = xfer->max_packet_size;
4942 td->max_packet_count = xfer->max_packet_count;
4944 if (td->max_packet_count == 0 || td->max_packet_count > 3)
4945 td->max_packet_count = 1;
4947 td->ep_type = ep_type;
4948 td->obj_next = last_obj;
4952 parm->size[0] += sizeof(*td);
4955 xfer->td_start[0] = last_obj;
4959 dwc_otg_xfer_unsetup(struct usb_xfer *xfer)
4965 dwc_otg_ep_init(struct usb_device *udev, struct usb_endpoint_descriptor *edesc,
4966 struct usb_endpoint *ep)
4968 struct dwc_otg_softc *sc = DWC_OTG_BUS2SC(udev->bus);
4970 DPRINTFN(2, "endpoint=%p, addr=%d, endpt=%d, mode=%d (%d,%d)\n",
4972 edesc->bEndpointAddress, udev->flags.usb_mode,
4973 sc->sc_rt_addr, udev->device_index);
4975 if (udev->device_index != sc->sc_rt_addr) {
4977 if (udev->flags.usb_mode == USB_MODE_DEVICE) {
4978 if (udev->speed != USB_SPEED_FULL &&
4979 udev->speed != USB_SPEED_HIGH) {
4984 if (udev->speed == USB_SPEED_HIGH &&
4985 (edesc->wMaxPacketSize[1] & 0x18) != 0 &&
4986 (edesc->bmAttributes & UE_XFERTYPE) != UE_ISOCHRONOUS) {
4988 DPRINTFN(-1, "Non-isochronous high bandwidth "
4989 "endpoint not supported\n");
4993 if ((edesc->bmAttributes & UE_XFERTYPE) == UE_ISOCHRONOUS)
4994 ep->methods = &dwc_otg_device_isoc_methods;
4996 ep->methods = &dwc_otg_device_non_isoc_methods;
5001 dwc_otg_set_hw_power_sleep(struct usb_bus *bus, uint32_t state)
5003 struct dwc_otg_softc *sc = DWC_OTG_BUS2SC(bus);
5006 case USB_HW_POWER_SUSPEND:
5007 dwc_otg_suspend(sc);
5009 case USB_HW_POWER_SHUTDOWN:
5012 case USB_HW_POWER_RESUME:
5021 dwc_otg_get_dma_delay(struct usb_device *udev, uint32_t *pus)
5023 /* DMA delay - wait until any use of memory is finished */
5024 *pus = (2125); /* microseconds */
5028 dwc_otg_device_resume(struct usb_device *udev)
5032 /* poll all transfers again to restart resumed ones */
5033 dwc_otg_do_poll(udev->bus);
5037 dwc_otg_device_suspend(struct usb_device *udev)
5042 static const struct usb_bus_methods dwc_otg_bus_methods =
5044 .endpoint_init = &dwc_otg_ep_init,
5045 .xfer_setup = &dwc_otg_xfer_setup,
5046 .xfer_unsetup = &dwc_otg_xfer_unsetup,
5047 .get_hw_ep_profile = &dwc_otg_get_hw_ep_profile,
5048 .xfer_stall = &dwc_otg_xfer_stall,
5049 .set_stall = &dwc_otg_set_stall,
5050 .clear_stall = &dwc_otg_clear_stall,
5051 .roothub_exec = &dwc_otg_roothub_exec,
5052 .xfer_poll = &dwc_otg_do_poll,
5053 .device_state_change = &dwc_otg_device_state_change,
5054 .set_hw_power_sleep = &dwc_otg_set_hw_power_sleep,
5055 .get_dma_delay = &dwc_otg_get_dma_delay,
5056 .device_resume = &dwc_otg_device_resume,
5057 .device_suspend = &dwc_otg_device_suspend,