3 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
5 * Copyright (c) 2015 Daisuke Aoyama. All rights reserved.
6 * Copyright (c) 2012-2015 Hans Petter Selasky. All rights reserved.
7 * Copyright (c) 2010-2011 Aleksandr Rybalko. All rights reserved.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * This file contains the driver for the DesignWare series USB 2.0 OTG
37 * LIMITATION: Drivers must be bound to all OUT endpoints in the
38 * active configuration for this driver to work properly. Blocking any
39 * OUT endpoint will block all OUT endpoints including the control
40 * endpoint. Usually this is not a problem.
44 * NOTE: Writing to non-existing registers appears to cause an
48 #ifdef USB_GLOBAL_INCLUDE_FILE
49 #include USB_GLOBAL_INCLUDE_FILE
51 #include <sys/stdint.h>
52 #include <sys/stddef.h>
53 #include <sys/param.h>
54 #include <sys/queue.h>
55 #include <sys/types.h>
56 #include <sys/systm.h>
57 #include <sys/kernel.h>
59 #include <sys/module.h>
61 #include <sys/mutex.h>
62 #include <sys/condvar.h>
63 #include <sys/sysctl.h>
65 #include <sys/unistd.h>
66 #include <sys/callout.h>
67 #include <sys/malloc.h>
70 #include <dev/usb/usb.h>
71 #include <dev/usb/usbdi.h>
73 #define USB_DEBUG_VAR dwc_otg_debug
75 #include <dev/usb/usb_core.h>
76 #include <dev/usb/usb_debug.h>
77 #include <dev/usb/usb_busdma.h>
78 #include <dev/usb/usb_process.h>
79 #include <dev/usb/usb_transfer.h>
80 #include <dev/usb/usb_device.h>
81 #include <dev/usb/usb_hub.h>
82 #include <dev/usb/usb_util.h>
84 #include <dev/usb/usb_controller.h>
85 #include <dev/usb/usb_bus.h>
86 #endif /* USB_GLOBAL_INCLUDE_FILE */
88 #include <dev/usb/controller/dwc_otg.h>
89 #include <dev/usb/controller/dwc_otgreg.h>
91 #define DWC_OTG_BUS2SC(bus) \
92 ((struct dwc_otg_softc *)(((uint8_t *)(bus)) - \
93 ((uint8_t *)&(((struct dwc_otg_softc *)0)->sc_bus))))
95 #define DWC_OTG_PC2UDEV(pc) \
96 (USB_DMATAG_TO_XROOT((pc)->tag_parent)->udev)
98 #define DWC_OTG_MSK_GINT_THREAD_IRQ \
99 (GINTSTS_USBRST | GINTSTS_ENUMDONE | GINTSTS_PRTINT | \
100 GINTSTS_WKUPINT | GINTSTS_USBSUSP | GINTMSK_OTGINTMSK | \
103 #ifndef DWC_OTG_PHY_DEFAULT
104 #define DWC_OTG_PHY_DEFAULT DWC_OTG_PHY_ULPI
107 static int dwc_otg_phy_type = DWC_OTG_PHY_DEFAULT;
109 static SYSCTL_NODE(_hw_usb, OID_AUTO, dwc_otg, CTLFLAG_RW, 0, "USB DWC OTG");
110 SYSCTL_INT(_hw_usb_dwc_otg, OID_AUTO, phy_type, CTLFLAG_RDTUN,
111 &dwc_otg_phy_type, 0, "DWC OTG PHY TYPE - 0/1/2/3 - ULPI/HSIC/INTERNAL/UTMI+");
114 static int dwc_otg_debug = 0;
116 SYSCTL_INT(_hw_usb_dwc_otg, OID_AUTO, debug, CTLFLAG_RWTUN,
117 &dwc_otg_debug, 0, "DWC OTG debug level");
120 #define DWC_OTG_INTR_ENDPT 1
124 static const struct usb_bus_methods dwc_otg_bus_methods;
125 static const struct usb_pipe_methods dwc_otg_device_non_isoc_methods;
126 static const struct usb_pipe_methods dwc_otg_device_isoc_methods;
128 static dwc_otg_cmd_t dwc_otg_setup_rx;
129 static dwc_otg_cmd_t dwc_otg_data_rx;
130 static dwc_otg_cmd_t dwc_otg_data_tx;
131 static dwc_otg_cmd_t dwc_otg_data_tx_sync;
133 static dwc_otg_cmd_t dwc_otg_host_setup_tx;
134 static dwc_otg_cmd_t dwc_otg_host_data_tx;
135 static dwc_otg_cmd_t dwc_otg_host_data_rx;
137 static void dwc_otg_device_done(struct usb_xfer *, usb_error_t);
138 static void dwc_otg_do_poll(struct usb_bus *);
139 static void dwc_otg_standard_done(struct usb_xfer *);
140 static void dwc_otg_root_intr(struct dwc_otg_softc *);
141 static void dwc_otg_interrupt_poll_locked(struct dwc_otg_softc *);
144 * Here is a configuration that the chip supports.
146 static const struct usb_hw_ep_profile dwc_otg_ep_profile[1] = {
149 .max_in_frame_size = 64,/* fixed */
150 .max_out_frame_size = 64, /* fixed */
152 .support_control = 1,
157 dwc_otg_get_hw_ep_profile(struct usb_device *udev,
158 const struct usb_hw_ep_profile **ppf, uint8_t ep_addr)
160 struct dwc_otg_softc *sc;
162 sc = DWC_OTG_BUS2SC(udev->bus);
164 if (ep_addr < sc->sc_dev_ep_max)
165 *ppf = &sc->sc_hw_ep_profile[ep_addr].usb;
171 dwc_otg_write_fifo(struct dwc_otg_softc *sc, struct usb_page_cache *pc,
172 uint32_t offset, uint32_t fifo, uint32_t count)
176 /* round down length to nearest 4-bytes */
179 /* check if we can write the data directly */
180 if (temp != 0 && usb_pc_buffer_is_aligned(pc, offset, temp, 3)) {
181 struct usb_page_search buf_res;
183 /* pre-subtract length */
186 /* iterate buffer list */
188 /* get current buffer pointer */
189 usbd_get_page(pc, offset, &buf_res);
191 if (buf_res.length > temp)
192 buf_res.length = temp;
194 /* transfer data into FIFO */
195 bus_space_write_region_4(sc->sc_io_tag, sc->sc_io_hdl,
196 fifo, buf_res.buffer, buf_res.length / 4);
198 offset += buf_res.length;
199 fifo += buf_res.length;
200 temp -= buf_res.length;
204 /* check for remainder */
206 /* clear topmost word before copy */
207 sc->sc_bounce_buffer[(count - 1) / 4] = 0;
210 usbd_copy_out(pc, offset,
211 sc->sc_bounce_buffer, count);
213 /* transfer data into FIFO */
214 bus_space_write_region_4(sc->sc_io_tag,
215 sc->sc_io_hdl, fifo, sc->sc_bounce_buffer,
221 dwc_otg_read_fifo(struct dwc_otg_softc *sc, struct usb_page_cache *pc,
222 uint32_t offset, uint32_t count)
226 /* round down length to nearest 4-bytes */
229 /* check if we can read the data directly */
230 if (temp != 0 && usb_pc_buffer_is_aligned(pc, offset, temp, 3)) {
231 struct usb_page_search buf_res;
233 /* pre-subtract length */
236 /* iterate buffer list */
238 /* get current buffer pointer */
239 usbd_get_page(pc, offset, &buf_res);
241 if (buf_res.length > temp)
242 buf_res.length = temp;
244 /* transfer data from FIFO */
245 bus_space_read_region_4(sc->sc_io_tag, sc->sc_io_hdl,
246 sc->sc_current_rx_fifo, buf_res.buffer, buf_res.length / 4);
248 offset += buf_res.length;
249 sc->sc_current_rx_fifo += buf_res.length;
250 sc->sc_current_rx_bytes -= buf_res.length;
251 temp -= buf_res.length;
255 /* check for remainder */
257 /* read data into bounce buffer */
258 bus_space_read_region_4(sc->sc_io_tag, sc->sc_io_hdl,
259 sc->sc_current_rx_fifo,
260 sc->sc_bounce_buffer, (count + 3) / 4);
262 /* store data into proper buffer */
263 usbd_copy_in(pc, offset, sc->sc_bounce_buffer, count);
265 /* round length up to nearest 4 bytes */
266 count = (count + 3) & ~3;
268 /* update counters */
269 sc->sc_current_rx_bytes -= count;
270 sc->sc_current_rx_fifo += count;
275 dwc_otg_tx_fifo_reset(struct dwc_otg_softc *sc, uint32_t value)
280 DWC_OTG_WRITE_4(sc, DOTG_GRSTCTL, value);
282 /* wait for reset to complete */
283 for (temp = 0; temp != 16; temp++) {
284 value = DWC_OTG_READ_4(sc, DOTG_GRSTCTL);
285 if (!(value & (GRSTCTL_TXFFLSH | GRSTCTL_RXFFLSH)))
291 dwc_otg_init_fifo(struct dwc_otg_softc *sc, uint8_t mode)
293 struct dwc_otg_profile *pf;
299 fifo_size = sc->sc_fifo_size;
302 * NOTE: Reserved fixed size area at end of RAM, which must
303 * not be allocated to the FIFOs:
307 if (fifo_size < fifo_regs) {
308 DPRINTF("Too little FIFO\n");
312 /* subtract FIFO regs from total once */
313 fifo_size -= fifo_regs;
315 /* split equally for IN and OUT */
318 /* Align to 4 bytes boundary (refer to PGM) */
321 /* set global receive FIFO size */
322 DWC_OTG_WRITE_4(sc, DOTG_GRXFSIZ, fifo_size / 4);
324 tx_start = fifo_size;
326 if (fifo_size < 64) {
327 DPRINTFN(-1, "Not enough data space for EP0 FIFO.\n");
331 if (mode == DWC_MODE_HOST) {
333 /* reset active endpoints */
334 sc->sc_active_rx_ep = 0;
336 /* split equally for periodic and non-periodic */
339 DPRINTF("PTX/NPTX FIFO=%u\n", fifo_size);
341 /* align to 4 bytes boundary */
344 DWC_OTG_WRITE_4(sc, DOTG_GNPTXFSIZ,
345 ((fifo_size / 4) << 16) |
348 tx_start += fifo_size;
350 for (x = 0; x != sc->sc_host_ch_max; x++) {
351 /* enable all host interrupts */
352 DWC_OTG_WRITE_4(sc, DOTG_HCINTMSK(x),
356 DWC_OTG_WRITE_4(sc, DOTG_HPTXFSIZ,
357 ((fifo_size / 4) << 16) |
360 /* reset host channel state */
361 memset(sc->sc_chan_state, 0, sizeof(sc->sc_chan_state));
363 /* enable all host channel interrupts */
364 DWC_OTG_WRITE_4(sc, DOTG_HAINTMSK,
365 (1U << sc->sc_host_ch_max) - 1U);
367 /* enable proper host channel interrupts */
368 sc->sc_irq_mask |= GINTMSK_HCHINTMSK;
369 sc->sc_irq_mask &= ~GINTMSK_IEPINTMSK;
370 DWC_OTG_WRITE_4(sc, DOTG_GINTMSK, sc->sc_irq_mask);
373 if (mode == DWC_MODE_DEVICE) {
375 DWC_OTG_WRITE_4(sc, DOTG_GNPTXFSIZ,
376 (0x10 << 16) | (tx_start / 4));
380 /* setup control endpoint profile */
381 sc->sc_hw_ep_profile[0].usb = dwc_otg_ep_profile[0];
383 /* reset active endpoints */
384 sc->sc_active_rx_ep = 1;
386 for (x = 1; x != sc->sc_dev_ep_max; x++) {
388 pf = sc->sc_hw_ep_profile + x;
390 pf->usb.max_out_frame_size = 1024 * 3;
391 pf->usb.is_simplex = 0; /* assume duplex */
392 pf->usb.support_bulk = 1;
393 pf->usb.support_interrupt = 1;
394 pf->usb.support_isochronous = 1;
395 pf->usb.support_out = 1;
397 if (x < sc->sc_dev_in_ep_max) {
400 limit = (x == 1) ? MIN(DWC_OTG_TX_MAX_FIFO_SIZE,
401 DWC_OTG_MAX_TXN) : MIN(DWC_OTG_MAX_TXN / 2,
402 DWC_OTG_TX_MAX_FIFO_SIZE);
404 /* see if there is enough FIFO space */
405 if (limit <= fifo_size) {
406 pf->max_buffer = limit;
407 pf->usb.support_in = 1;
409 limit = MIN(DWC_OTG_TX_MAX_FIFO_SIZE, 0x40);
410 if (limit <= fifo_size) {
411 pf->usb.support_in = 1;
413 pf->usb.is_simplex = 1;
418 DWC_OTG_WRITE_4(sc, DOTG_DIEPTXF(x),
419 ((limit / 4) << 16) | (tx_start / 4));
422 pf->usb.max_in_frame_size = limit;
424 pf->usb.is_simplex = 1;
427 DPRINTF("FIFO%d = IN:%d / OUT:%d\n", x,
428 pf->usb.max_in_frame_size,
429 pf->usb.max_out_frame_size);
432 /* enable proper device channel interrupts */
433 sc->sc_irq_mask &= ~GINTMSK_HCHINTMSK;
434 sc->sc_irq_mask |= GINTMSK_IEPINTMSK;
435 DWC_OTG_WRITE_4(sc, DOTG_GINTMSK, sc->sc_irq_mask);
439 dwc_otg_tx_fifo_reset(sc, GRSTCTL_RXFFLSH);
441 if (mode != DWC_MODE_OTG) {
442 /* reset all TX FIFOs */
443 dwc_otg_tx_fifo_reset(sc,
444 GRSTCTL_TXFIFO(0x10) |
447 /* reset active endpoints */
448 sc->sc_active_rx_ep = 0;
450 /* reset host channel state */
451 memset(sc->sc_chan_state, 0, sizeof(sc->sc_chan_state));
457 dwc_otg_uses_split(struct usb_device *udev)
460 * When a LOW or FULL speed device is connected directly to
461 * the USB port we don't use split transactions:
463 return (udev->speed != USB_SPEED_HIGH &&
464 udev->parent_hs_hub != NULL &&
465 udev->parent_hs_hub->parent_hub != NULL);
469 dwc_otg_update_host_frame_interval(struct dwc_otg_softc *sc)
473 * Disabled until further. Assuming that the register is already
474 * programmed correctly by the boot loader.
479 /* setup HOST frame interval register, based on existing value */
480 temp = DWC_OTG_READ_4(sc, DOTG_HFIR) & HFIR_FRINT_MASK;
486 /* figure out nearest X-tal value */
494 if (sc->sc_flags.status_high_speed)
499 DPRINTF("HFIR=0x%08x\n", temp);
501 DWC_OTG_WRITE_4(sc, DOTG_HFIR, temp);
506 dwc_otg_clocks_on(struct dwc_otg_softc *sc)
508 if (sc->sc_flags.clocks_off &&
509 sc->sc_flags.port_powered) {
513 /* TODO - platform specific */
515 sc->sc_flags.clocks_off = 0;
520 dwc_otg_clocks_off(struct dwc_otg_softc *sc)
522 if (!sc->sc_flags.clocks_off) {
526 /* TODO - platform specific */
528 sc->sc_flags.clocks_off = 1;
533 dwc_otg_pull_up(struct dwc_otg_softc *sc)
537 /* pullup D+, if possible */
539 if (!sc->sc_flags.d_pulled_up &&
540 sc->sc_flags.port_powered) {
541 sc->sc_flags.d_pulled_up = 1;
543 temp = DWC_OTG_READ_4(sc, DOTG_DCTL);
544 temp &= ~DCTL_SFTDISCON;
545 DWC_OTG_WRITE_4(sc, DOTG_DCTL, temp);
550 dwc_otg_pull_down(struct dwc_otg_softc *sc)
554 /* pulldown D+, if possible */
556 if (sc->sc_flags.d_pulled_up) {
557 sc->sc_flags.d_pulled_up = 0;
559 temp = DWC_OTG_READ_4(sc, DOTG_DCTL);
560 temp |= DCTL_SFTDISCON;
561 DWC_OTG_WRITE_4(sc, DOTG_DCTL, temp);
566 dwc_otg_enable_sof_irq(struct dwc_otg_softc *sc)
568 /* In device mode we don't use the SOF interrupt */
569 if (sc->sc_flags.status_device_mode != 0)
571 /* Ensure the SOF interrupt is not disabled */
573 /* Check if the SOF interrupt is already enabled */
574 if ((sc->sc_irq_mask & GINTMSK_SOFMSK) != 0)
576 sc->sc_irq_mask |= GINTMSK_SOFMSK;
577 DWC_OTG_WRITE_4(sc, DOTG_GINTMSK, sc->sc_irq_mask);
581 dwc_otg_resume_irq(struct dwc_otg_softc *sc)
583 if (sc->sc_flags.status_suspend) {
584 /* update status bits */
585 sc->sc_flags.status_suspend = 0;
586 sc->sc_flags.change_suspend = 1;
588 if (sc->sc_flags.status_device_mode) {
590 * Disable resume interrupt and enable suspend
593 sc->sc_irq_mask &= ~GINTMSK_WKUPINTMSK;
594 sc->sc_irq_mask |= GINTMSK_USBSUSPMSK;
595 DWC_OTG_WRITE_4(sc, DOTG_GINTMSK, sc->sc_irq_mask);
598 /* complete root HUB interrupt endpoint */
599 dwc_otg_root_intr(sc);
604 dwc_otg_suspend_irq(struct dwc_otg_softc *sc)
606 if (!sc->sc_flags.status_suspend) {
607 /* update status bits */
608 sc->sc_flags.status_suspend = 1;
609 sc->sc_flags.change_suspend = 1;
611 if (sc->sc_flags.status_device_mode) {
613 * Disable suspend interrupt and enable resume
616 sc->sc_irq_mask &= ~GINTMSK_USBSUSPMSK;
617 sc->sc_irq_mask |= GINTMSK_WKUPINTMSK;
618 DWC_OTG_WRITE_4(sc, DOTG_GINTMSK, sc->sc_irq_mask);
621 /* complete root HUB interrupt endpoint */
622 dwc_otg_root_intr(sc);
627 dwc_otg_wakeup_peer(struct dwc_otg_softc *sc)
629 if (!sc->sc_flags.status_suspend)
632 DPRINTFN(5, "Remote wakeup\n");
634 if (sc->sc_flags.status_device_mode) {
637 /* enable remote wakeup signalling */
638 temp = DWC_OTG_READ_4(sc, DOTG_DCTL);
639 temp |= DCTL_RMTWKUPSIG;
640 DWC_OTG_WRITE_4(sc, DOTG_DCTL, temp);
642 /* Wait 8ms for remote wakeup to complete. */
643 usb_pause_mtx(&sc->sc_bus.bus_mtx, hz / 125);
645 temp &= ~DCTL_RMTWKUPSIG;
646 DWC_OTG_WRITE_4(sc, DOTG_DCTL, temp);
648 /* enable USB port */
649 DWC_OTG_WRITE_4(sc, DOTG_PCGCCTL, 0);
652 usb_pause_mtx(&sc->sc_bus.bus_mtx, hz / 100);
655 sc->sc_hprt_val |= HPRT_PRTRES;
656 DWC_OTG_WRITE_4(sc, DOTG_HPRT, sc->sc_hprt_val);
658 /* Wait 100ms for resume signalling to complete. */
659 usb_pause_mtx(&sc->sc_bus.bus_mtx, hz / 10);
661 /* clear suspend and resume */
662 sc->sc_hprt_val &= ~(HPRT_PRTSUSP | HPRT_PRTRES);
663 DWC_OTG_WRITE_4(sc, DOTG_HPRT, sc->sc_hprt_val);
666 usb_pause_mtx(&sc->sc_bus.bus_mtx, hz / 250);
669 /* need to fake resume IRQ */
670 dwc_otg_resume_irq(sc);
674 dwc_otg_set_address(struct dwc_otg_softc *sc, uint8_t addr)
678 DPRINTFN(5, "addr=%d\n", addr);
680 temp = DWC_OTG_READ_4(sc, DOTG_DCFG);
681 temp &= ~DCFG_DEVADDR_SET(0x7F);
682 temp |= DCFG_DEVADDR_SET(addr);
683 DWC_OTG_WRITE_4(sc, DOTG_DCFG, temp);
687 dwc_otg_common_rx_ack(struct dwc_otg_softc *sc)
689 DPRINTFN(5, "RX status clear\n");
691 /* enable RX FIFO level interrupt */
692 sc->sc_irq_mask |= GINTMSK_RXFLVLMSK;
693 DWC_OTG_WRITE_4(sc, DOTG_GINTMSK, sc->sc_irq_mask);
695 if (sc->sc_current_rx_bytes != 0) {
696 /* need to dump remaining data */
697 bus_space_read_region_4(sc->sc_io_tag, sc->sc_io_hdl,
698 sc->sc_current_rx_fifo, sc->sc_bounce_buffer,
699 sc->sc_current_rx_bytes / 4);
700 /* clear number of active bytes to receive */
701 sc->sc_current_rx_bytes = 0;
703 /* clear cached status */
704 sc->sc_last_rx_status = 0;
708 dwc_otg_clear_hcint(struct dwc_otg_softc *sc, uint8_t x)
712 /* clear all pending interrupts */
713 hcint = DWC_OTG_READ_4(sc, DOTG_HCINT(x));
714 DWC_OTG_WRITE_4(sc, DOTG_HCINT(x), hcint);
716 /* clear buffered interrupts */
717 sc->sc_chan_state[x].hcint = 0;
721 dwc_otg_host_check_tx_fifo_empty(struct dwc_otg_softc *sc, struct dwc_otg_td *td)
725 temp = DWC_OTG_READ_4(sc, DOTG_GINTSTS);
727 if (td->ep_type == UE_ISOCHRONOUS) {
729 * NOTE: USB INTERRUPT transactions are executed like
730 * USB CONTROL transactions! See the setup standard
731 * chain function for more information.
733 if (!(temp & GINTSTS_PTXFEMP)) {
734 DPRINTF("Periodic TX FIFO is not empty\n");
735 if (!(sc->sc_irq_mask & GINTMSK_PTXFEMPMSK)) {
736 sc->sc_irq_mask |= GINTMSK_PTXFEMPMSK;
737 DWC_OTG_WRITE_4(sc, DOTG_GINTMSK, sc->sc_irq_mask);
739 return (1); /* busy */
742 if (!(temp & GINTSTS_NPTXFEMP)) {
743 DPRINTF("Non-periodic TX FIFO is not empty\n");
744 if (!(sc->sc_irq_mask & GINTMSK_NPTXFEMPMSK)) {
745 sc->sc_irq_mask |= GINTMSK_NPTXFEMPMSK;
746 DWC_OTG_WRITE_4(sc, DOTG_GINTMSK, sc->sc_irq_mask);
748 return (1); /* busy */
751 return (0); /* ready for transmit */
755 dwc_otg_host_channel_alloc(struct dwc_otg_softc *sc,
756 struct dwc_otg_td *td, uint8_t is_out)
762 if (td->channel[0] < DWC_OTG_MAX_CHANNELS)
763 return (0); /* already allocated */
765 /* check if device is suspended */
766 if (DWC_OTG_PC2UDEV(td->pc)->flags.self_suspended != 0)
767 return (1); /* busy - cannot transfer data */
769 /* compute needed TX FIFO size */
771 if (dwc_otg_host_check_tx_fifo_empty(sc, td) != 0)
772 return (1); /* busy - cannot transfer data */
774 z = td->max_packet_count;
775 for (x = y = 0; x != sc->sc_host_ch_max; x++) {
776 /* check if channel is allocated */
777 if (sc->sc_chan_state[x].allocated != 0)
779 /* check if channel is still enabled */
780 if (sc->sc_chan_state[x].wait_halted != 0)
782 /* store channel number */
783 td->channel[y++] = x;
784 /* check if we got all channels */
789 /* reset channel variable */
790 td->channel[0] = DWC_OTG_MAX_CHANNELS;
791 td->channel[1] = DWC_OTG_MAX_CHANNELS;
792 td->channel[2] = DWC_OTG_MAX_CHANNELS;
794 dwc_otg_enable_sof_irq(sc);
795 return (1); /* busy - not enough channels */
798 for (y = 0; y != z; y++) {
802 sc->sc_chan_state[x].allocated = 1;
804 /* set wait halted */
805 sc->sc_chan_state[x].wait_halted = 1;
807 /* clear interrupts */
808 dwc_otg_clear_hcint(sc, x);
810 DPRINTF("CH=%d HCCHAR=0x%08x "
811 "HCSPLT=0x%08x\n", x, td->hcchar, td->hcsplt);
813 /* set active channel */
814 sc->sc_active_rx_ep |= (1 << x);
816 return (0); /* allocated */
820 dwc_otg_host_channel_free_sub(struct dwc_otg_softc *sc, struct dwc_otg_td *td, uint8_t index)
825 if (td->channel[index] >= DWC_OTG_MAX_CHANNELS)
826 return; /* already freed */
829 x = td->channel[index];
830 td->channel[index] = DWC_OTG_MAX_CHANNELS;
832 DPRINTF("CH=%d\n", x);
835 * We need to let programmed host channels run till complete
836 * else the host channel will stop functioning.
838 sc->sc_chan_state[x].allocated = 0;
840 /* ack any pending messages */
841 if (sc->sc_last_rx_status != 0 &&
842 GRXSTSRD_CHNUM_GET(sc->sc_last_rx_status) == x) {
843 dwc_otg_common_rx_ack(sc);
846 /* clear active channel */
847 sc->sc_active_rx_ep &= ~(1 << x);
849 /* check if already halted */
850 if (sc->sc_chan_state[x].wait_halted == 0)
853 /* disable host channel */
854 hcchar = DWC_OTG_READ_4(sc, DOTG_HCCHAR(x));
855 if (hcchar & HCCHAR_CHENA) {
856 DPRINTF("Halting channel %d\n", x);
857 DWC_OTG_WRITE_4(sc, DOTG_HCCHAR(x),
858 hcchar | HCCHAR_CHDIS);
859 /* don't write HCCHAR until the channel is halted */
861 sc->sc_chan_state[x].wait_halted = 0;
866 dwc_otg_host_channel_free(struct dwc_otg_softc *sc, struct dwc_otg_td *td)
869 for (x = 0; x != td->max_packet_count; x++)
870 dwc_otg_host_channel_free_sub(sc, td, x);
874 dwc_otg_host_dump_rx(struct dwc_otg_softc *sc, struct dwc_otg_td *td)
877 /* dump any pending messages */
878 if (sc->sc_last_rx_status == 0)
880 for (x = 0; x != td->max_packet_count; x++) {
881 if (td->channel[x] >= DWC_OTG_MAX_CHANNELS ||
882 td->channel[x] != GRXSTSRD_CHNUM_GET(sc->sc_last_rx_status))
884 dwc_otg_common_rx_ack(sc);
890 dwc_otg_host_setup_tx(struct dwc_otg_softc *sc, struct dwc_otg_td *td)
892 struct usb_device_request req __aligned(4);
897 dwc_otg_host_dump_rx(sc, td);
899 if (td->channel[0] < DWC_OTG_MAX_CHANNELS) {
900 hcint = sc->sc_chan_state[td->channel[0]].hcint;
902 DPRINTF("CH=%d ST=%d HCINT=0x%08x HCCHAR=0x%08x HCTSIZ=0x%08x\n",
903 td->channel[0], td->state, hcint,
904 DWC_OTG_READ_4(sc, DOTG_HCCHAR(td->channel[0])),
905 DWC_OTG_READ_4(sc, DOTG_HCTSIZ(td->channel[0])));
911 if (hcint & (HCINT_RETRY |
912 HCINT_ACK | HCINT_NYET)) {
913 /* give success bits priority over failure bits */
914 } else if (hcint & HCINT_STALL) {
915 DPRINTF("CH=%d STALL\n", td->channel[0]);
919 } else if (hcint & HCINT_ERRORS) {
920 DPRINTF("CH=%d ERROR\n", td->channel[0]);
922 if (td->hcsplt != 0 || td->errcnt >= 3) {
928 if (hcint & (HCINT_ERRORS | HCINT_RETRY |
929 HCINT_ACK | HCINT_NYET)) {
930 if (!(hcint & HCINT_ERRORS))
936 case DWC_CHAN_ST_START:
939 case DWC_CHAN_ST_WAIT_ANE:
940 if (hcint & (HCINT_RETRY | HCINT_ERRORS)) {
942 td->tt_scheduled = 0;
944 } else if (hcint & (HCINT_ACK | HCINT_NYET)) {
945 td->offset += td->tx_bytes;
946 td->remainder -= td->tx_bytes;
948 td->tt_scheduled = 0;
953 case DWC_CHAN_ST_WAIT_S_ANE:
954 if (hcint & (HCINT_RETRY | HCINT_ERRORS)) {
956 td->tt_scheduled = 0;
958 } else if (hcint & (HCINT_ACK | HCINT_NYET)) {
963 case DWC_CHAN_ST_WAIT_C_ANE:
964 if (hcint & HCINT_NYET) {
966 } else if (hcint & (HCINT_RETRY | HCINT_ERRORS)) {
968 td->tt_scheduled = 0;
970 } else if (hcint & HCINT_ACK) {
971 td->offset += td->tx_bytes;
972 td->remainder -= td->tx_bytes;
978 case DWC_CHAN_ST_WAIT_C_PKT:
987 /* free existing channel, if any */
988 dwc_otg_host_channel_free(sc, td);
990 if (sizeof(req) != td->remainder) {
995 if (td->hcsplt != 0) {
996 delta = td->tt_start_slot - sc->sc_last_frame_num - 1;
997 if (td->tt_scheduled == 0 || delta < DWC_OTG_TT_SLOT_MAX) {
998 td->state = DWC_CHAN_ST_START;
1001 delta = sc->sc_last_frame_num - td->tt_start_slot;
1004 td->tt_scheduled = 0;
1005 td->state = DWC_CHAN_ST_START;
1010 /* allocate a new channel */
1011 if (dwc_otg_host_channel_alloc(sc, td, 1)) {
1012 td->state = DWC_CHAN_ST_START;
1016 if (td->hcsplt != 0) {
1017 td->hcsplt &= ~HCSPLT_COMPSPLT;
1018 td->state = DWC_CHAN_ST_WAIT_S_ANE;
1020 td->state = DWC_CHAN_ST_WAIT_ANE;
1023 /* copy out control request */
1024 usbd_copy_out(td->pc, 0, &req, sizeof(req));
1026 DWC_OTG_WRITE_4(sc, DOTG_HCTSIZ(td->channel[0]),
1027 (sizeof(req) << HCTSIZ_XFERSIZE_SHIFT) |
1028 (1 << HCTSIZ_PKTCNT_SHIFT) |
1029 (HCTSIZ_PID_SETUP << HCTSIZ_PID_SHIFT));
1031 DWC_OTG_WRITE_4(sc, DOTG_HCSPLT(td->channel[0]), td->hcsplt);
1033 hcchar = td->hcchar;
1034 hcchar &= ~(HCCHAR_EPDIR_IN | HCCHAR_EPTYPE_MASK);
1035 hcchar |= UE_CONTROL << HCCHAR_EPTYPE_SHIFT;
1037 /* must enable channel before writing data to FIFO */
1038 DWC_OTG_WRITE_4(sc, DOTG_HCCHAR(td->channel[0]), hcchar);
1040 /* transfer data into FIFO */
1041 bus_space_write_region_4(sc->sc_io_tag, sc->sc_io_hdl,
1042 DOTG_DFIFO(td->channel[0]), (uint32_t *)&req, sizeof(req) / 4);
1044 /* wait until next slot before trying complete split */
1045 td->tt_complete_slot = sc->sc_last_frame_num + 1;
1047 /* store number of bytes transmitted */
1048 td->tx_bytes = sizeof(req);
1052 /* free existing channel, if any */
1053 dwc_otg_host_channel_free(sc, td);
1055 delta = td->tt_complete_slot - sc->sc_last_frame_num - 1;
1056 if (td->tt_scheduled == 0 || delta < DWC_OTG_TT_SLOT_MAX) {
1057 td->state = DWC_CHAN_ST_WAIT_C_PKT;
1060 delta = sc->sc_last_frame_num - td->tt_start_slot;
1061 if (delta > DWC_OTG_TT_SLOT_MAX) {
1062 /* we missed the service interval */
1063 if (td->ep_type != UE_ISOCHRONOUS)
1067 /* allocate a new channel */
1068 if (dwc_otg_host_channel_alloc(sc, td, 0)) {
1069 td->state = DWC_CHAN_ST_WAIT_C_PKT;
1073 /* wait until next slot before trying complete split */
1074 td->tt_complete_slot = sc->sc_last_frame_num + 1;
1076 td->hcsplt |= HCSPLT_COMPSPLT;
1077 td->state = DWC_CHAN_ST_WAIT_C_ANE;
1079 DWC_OTG_WRITE_4(sc, DOTG_HCTSIZ(td->channel[0]),
1080 (HCTSIZ_PID_SETUP << HCTSIZ_PID_SHIFT));
1082 DWC_OTG_WRITE_4(sc, DOTG_HCSPLT(td->channel[0]), td->hcsplt);
1084 hcchar = td->hcchar;
1085 hcchar &= ~(HCCHAR_EPDIR_IN | HCCHAR_EPTYPE_MASK);
1086 hcchar |= UE_CONTROL << HCCHAR_EPTYPE_SHIFT;
1088 /* must enable channel before writing data to FIFO */
1089 DWC_OTG_WRITE_4(sc, DOTG_HCCHAR(td->channel[0]), hcchar);
1092 return (1); /* busy */
1095 dwc_otg_host_channel_free(sc, td);
1096 return (0); /* complete */
1100 dwc_otg_setup_rx(struct dwc_otg_softc *sc, struct dwc_otg_td *td)
1102 struct usb_device_request req __aligned(4);
1106 /* check endpoint status */
1108 if (sc->sc_last_rx_status == 0)
1111 if (GRXSTSRD_CHNUM_GET(sc->sc_last_rx_status) != 0)
1114 if ((sc->sc_last_rx_status & GRXSTSRD_PKTSTS_MASK) !=
1115 GRXSTSRD_STP_DATA) {
1116 if ((sc->sc_last_rx_status & GRXSTSRD_PKTSTS_MASK) !=
1117 GRXSTSRD_STP_COMPLETE || td->remainder != 0) {
1119 dwc_otg_common_rx_ack(sc);
1123 dwc_otg_common_rx_ack(sc);
1124 return (0); /* complete */
1127 if ((sc->sc_last_rx_status & GRXSTSRD_DPID_MASK) !=
1128 GRXSTSRD_DPID_DATA0) {
1130 dwc_otg_common_rx_ack(sc);
1134 DPRINTFN(5, "GRXSTSR=0x%08x\n", sc->sc_last_rx_status);
1136 /* clear did stall */
1139 /* get the packet byte count */
1140 count = GRXSTSRD_BCNT_GET(sc->sc_last_rx_status);
1142 if (count != sizeof(req)) {
1143 DPRINTFN(0, "Unsupported SETUP packet "
1144 "length, %d bytes\n", count);
1146 dwc_otg_common_rx_ack(sc);
1151 dwc_otg_read_fifo(sc, td->pc, 0, sizeof(req));
1153 /* copy out control request */
1154 usbd_copy_out(td->pc, 0, &req, sizeof(req));
1156 td->offset = sizeof(req);
1159 /* sneak peek the set address */
1160 if ((req.bmRequestType == UT_WRITE_DEVICE) &&
1161 (req.bRequest == UR_SET_ADDRESS)) {
1162 /* must write address before ZLP */
1163 dwc_otg_set_address(sc, req.wValue[0] & 0x7F);
1166 /* don't send any data by default */
1167 DWC_OTG_WRITE_4(sc, DOTG_DIEPTSIZ(0), DIEPCTL_EPDIS);
1168 DWC_OTG_WRITE_4(sc, DOTG_DOEPTSIZ(0), DOEPCTL_EPDIS);
1170 /* reset IN endpoint buffer */
1171 dwc_otg_tx_fifo_reset(sc,
1175 /* acknowledge RX status */
1176 dwc_otg_common_rx_ack(sc);
1180 /* abort any ongoing transfer, before enabling again */
1181 if (!td->did_stall) {
1184 DPRINTFN(5, "stalling IN and OUT direction\n");
1186 temp = sc->sc_out_ctl[0];
1188 /* set stall after enabling endpoint */
1189 DWC_OTG_WRITE_4(sc, DOTG_DOEPCTL(0),
1190 temp | DOEPCTL_STALL);
1192 temp = sc->sc_in_ctl[0];
1194 /* set stall assuming endpoint is enabled */
1195 DWC_OTG_WRITE_4(sc, DOTG_DIEPCTL(0),
1196 temp | DIEPCTL_STALL);
1198 return (1); /* not complete */
1202 dwc_otg_host_rate_check_interrupt(struct dwc_otg_softc *sc, struct dwc_otg_td *td)
1206 delta = sc->sc_tmr_val - td->tmr_val;
1208 return (1); /* busy */
1210 td->tmr_val = sc->sc_tmr_val + td->tmr_res;
1212 /* set toggle, if any */
1213 if (td->set_toggle) {
1221 dwc_otg_host_rate_check(struct dwc_otg_softc *sc, struct dwc_otg_td *td)
1223 uint8_t frame_num = (uint8_t)sc->sc_last_frame_num;
1225 if (td->ep_type == UE_ISOCHRONOUS) {
1226 /* non TT isochronous traffic */
1227 if (frame_num & (td->tmr_res - 1))
1229 if ((frame_num ^ td->tmr_val) & td->tmr_res)
1231 td->tmr_val = td->tmr_res + sc->sc_last_frame_num;
1234 } else if (td->ep_type == UE_INTERRUPT) {
1235 if (!td->tt_scheduled)
1237 td->tt_scheduled = 0;
1239 } else if (td->did_nak != 0) {
1240 /* check if we should pause sending queries for 125us */
1241 if (td->tmr_res == frame_num) {
1243 dwc_otg_enable_sof_irq(sc);
1246 } else if (td->set_toggle) {
1250 /* query for data one more time */
1251 td->tmr_res = frame_num;
1259 dwc_otg_host_data_rx_sub(struct dwc_otg_softc *sc, struct dwc_otg_td *td,
1264 /* check endpoint status */
1265 if (sc->sc_last_rx_status == 0)
1268 if (channel >= DWC_OTG_MAX_CHANNELS)
1271 if (GRXSTSRD_CHNUM_GET(sc->sc_last_rx_status) != channel)
1274 switch (sc->sc_last_rx_status & GRXSTSRD_PKTSTS_MASK) {
1275 case GRXSTSRH_IN_DATA:
1277 DPRINTF("DATA ST=%d STATUS=0x%08x\n",
1278 (int)td->state, (int)sc->sc_last_rx_status);
1280 if (sc->sc_chan_state[channel].hcint & HCINT_SOFTWARE_ONLY) {
1282 * When using SPLIT transactions on interrupt
1283 * endpoints, sometimes data occurs twice.
1285 DPRINTF("Data already received\n");
1289 /* get the packet byte count */
1290 count = GRXSTSRD_BCNT_GET(sc->sc_last_rx_status);
1292 /* check for ISOCHRONOUS endpoint */
1293 if (td->ep_type == UE_ISOCHRONOUS) {
1294 if ((sc->sc_last_rx_status & GRXSTSRD_DPID_MASK) !=
1295 GRXSTSRD_DPID_DATA0) {
1296 /* more data to be received */
1297 td->tt_xactpos = HCSPLT_XACTPOS_MIDDLE;
1299 /* all data received */
1300 td->tt_xactpos = HCSPLT_XACTPOS_BEGIN;
1301 /* verify the packet byte count */
1302 if (count != td->remainder) {
1303 /* we have a short packet */
1309 /* verify the packet byte count */
1310 if (count != td->max_packet_size) {
1311 if (count < td->max_packet_size) {
1312 /* we have a short packet */
1316 /* invalid USB packet */
1320 dwc_otg_common_rx_ack(sc);
1325 td->tt_scheduled = 0;
1328 /* verify the packet byte count */
1329 if (count > td->remainder) {
1330 /* invalid USB packet */
1334 dwc_otg_common_rx_ack(sc);
1338 /* read data from FIFO */
1339 dwc_otg_read_fifo(sc, td->pc, td->offset, count);
1341 td->remainder -= count;
1342 td->offset += count;
1343 sc->sc_chan_state[channel].hcint |= HCINT_SOFTWARE_ONLY;
1349 dwc_otg_common_rx_ack(sc);
1357 dwc_otg_host_data_rx(struct dwc_otg_softc *sc, struct dwc_otg_td *td)
1365 for (x = 0; x != td->max_packet_count; x++) {
1366 channel = td->channel[x];
1367 if (channel >= DWC_OTG_MAX_CHANNELS)
1369 hcint |= sc->sc_chan_state[channel].hcint;
1371 DPRINTF("CH=%d ST=%d HCINT=0x%08x HCCHAR=0x%08x HCTSIZ=0x%08x\n",
1372 channel, td->state, hcint,
1373 DWC_OTG_READ_4(sc, DOTG_HCCHAR(channel)),
1374 DWC_OTG_READ_4(sc, DOTG_HCTSIZ(channel)));
1376 /* check interrupt bits */
1377 if (hcint & (HCINT_RETRY |
1378 HCINT_ACK | HCINT_NYET)) {
1379 /* give success bits priority over failure bits */
1380 } else if (hcint & HCINT_STALL) {
1381 DPRINTF("CH=%d STALL\n", channel);
1382 td->error_stall = 1;
1385 } else if (hcint & HCINT_ERRORS) {
1386 DPRINTF("CH=%d ERROR\n", channel);
1388 if (td->hcsplt != 0 || td->errcnt >= 3) {
1389 if (td->ep_type != UE_ISOCHRONOUS) {
1396 /* check channels for data, if any */
1397 if (dwc_otg_host_data_rx_sub(sc, td, channel))
1400 /* refresh interrupt status */
1401 hcint |= sc->sc_chan_state[channel].hcint;
1403 if (hcint & (HCINT_ERRORS | HCINT_RETRY |
1404 HCINT_ACK | HCINT_NYET)) {
1405 if (!(hcint & HCINT_ERRORS))
1410 switch (td->state) {
1411 case DWC_CHAN_ST_START:
1412 if (td->hcsplt != 0)
1417 case DWC_CHAN_ST_WAIT_ANE:
1418 if (hcint & (HCINT_RETRY | HCINT_ERRORS)) {
1419 if (td->ep_type == UE_INTERRUPT) {
1421 * The USB specification does not
1422 * mandate a particular data toggle
1423 * value for USB INTERRUPT
1424 * transfers. Switch the data toggle
1425 * value to receive the packet
1428 if (hcint & HCINT_DATATGLERR) {
1429 DPRINTF("Retrying packet due to "
1430 "data toggle error\n");
1434 } else if (td->ep_type == UE_ISOCHRONOUS) {
1435 if (td->hcsplt != 0) {
1437 * Sometimes the complete
1438 * split packet may be queued
1440 * transaction translator will
1441 * return a NAK. Ignore
1442 * this message and retry the
1443 * complete split instead.
1445 DPRINTF("Retrying complete split\n");
1451 td->tt_scheduled = 0;
1452 if (td->hcsplt != 0)
1456 } else if (hcint & HCINT_NYET) {
1457 if (td->hcsplt != 0) {
1461 /* not a valid token for IN endpoints */
1465 } else if (hcint & HCINT_ACK) {
1466 /* wait for data - ACK arrived first */
1467 if (!(hcint & HCINT_SOFTWARE_ONLY))
1470 if (td->ep_type == UE_ISOCHRONOUS) {
1471 /* check if we are complete */
1472 if (td->tt_xactpos == HCSPLT_XACTPOS_BEGIN) {
1474 } else if (td->hcsplt != 0) {
1477 /* get more packets */
1481 /* check if we are complete */
1482 if ((td->remainder == 0) || (td->got_short != 0)) {
1487 * Else need to receive a zero length
1491 td->tt_scheduled = 0;
1493 if (td->hcsplt != 0)
1501 case DWC_CHAN_ST_WAIT_S_ANE:
1503 * NOTE: The DWC OTG hardware provides a fake ACK in
1504 * case of interrupt and isochronous transfers:
1506 if (hcint & (HCINT_RETRY | HCINT_ERRORS)) {
1508 td->tt_scheduled = 0;
1510 } else if (hcint & HCINT_NYET) {
1511 td->tt_scheduled = 0;
1513 } else if (hcint & HCINT_ACK) {
1519 case DWC_CHAN_ST_WAIT_C_PKT:
1528 /* free existing channel, if any */
1529 dwc_otg_host_channel_free(sc, td);
1531 if (td->hcsplt != 0) {
1532 delta = td->tt_complete_slot - sc->sc_last_frame_num - 1;
1533 if (td->tt_scheduled == 0 || delta < DWC_OTG_TT_SLOT_MAX) {
1534 if (td->ep_type != UE_ISOCHRONOUS) {
1535 td->state = DWC_CHAN_ST_WAIT_C_PKT;
1539 delta = sc->sc_last_frame_num - td->tt_start_slot;
1540 if (delta > DWC_OTG_TT_SLOT_MAX) {
1541 if (td->ep_type != UE_ISOCHRONOUS) {
1542 /* we missed the service interval */
1547 /* complete split */
1548 td->hcsplt |= HCSPLT_COMPSPLT;
1549 } else if (dwc_otg_host_rate_check(sc, td)) {
1550 td->state = DWC_CHAN_ST_WAIT_C_PKT;
1554 /* allocate a new channel */
1555 if (dwc_otg_host_channel_alloc(sc, td, 0)) {
1556 td->state = DWC_CHAN_ST_WAIT_C_PKT;
1560 /* set toggle, if any */
1561 if (td->set_toggle) {
1566 td->state = DWC_CHAN_ST_WAIT_ANE;
1568 for (x = 0; x != td->max_packet_count; x++) {
1569 channel = td->channel[x];
1571 /* receive one packet */
1572 DWC_OTG_WRITE_4(sc, DOTG_HCTSIZ(channel),
1573 (td->max_packet_size << HCTSIZ_XFERSIZE_SHIFT) |
1574 (1 << HCTSIZ_PKTCNT_SHIFT) |
1575 (td->toggle ? (HCTSIZ_PID_DATA1 << HCTSIZ_PID_SHIFT) :
1576 (HCTSIZ_PID_DATA0 << HCTSIZ_PID_SHIFT)));
1578 DWC_OTG_WRITE_4(sc, DOTG_HCSPLT(channel), td->hcsplt);
1580 hcchar = td->hcchar;
1581 hcchar |= HCCHAR_EPDIR_IN;
1583 if (td->ep_type == UE_ISOCHRONOUS) {
1584 if (td->hcsplt != 0) {
1585 /* continously buffer */
1586 if (sc->sc_last_frame_num & 1)
1587 hcchar &= ~HCCHAR_ODDFRM;
1589 hcchar |= HCCHAR_ODDFRM;
1591 /* multi buffer, if any */
1592 if (sc->sc_last_frame_num & 1)
1593 hcchar |= HCCHAR_ODDFRM;
1595 hcchar &= ~HCCHAR_ODDFRM;
1598 hcchar &= ~HCCHAR_ODDFRM;
1601 /* must enable channel before data can be received */
1602 DWC_OTG_WRITE_4(sc, DOTG_HCCHAR(channel), hcchar);
1604 /* wait until next slot before trying complete split */
1605 td->tt_complete_slot = sc->sc_last_frame_num + 1;
1610 /* free existing channel(s), if any */
1611 dwc_otg_host_channel_free(sc, td);
1613 delta = td->tt_start_slot - sc->sc_last_frame_num - 1;
1614 if (td->tt_scheduled == 0 || delta < DWC_OTG_TT_SLOT_MAX) {
1615 td->state = DWC_CHAN_ST_START;
1618 delta = sc->sc_last_frame_num - td->tt_start_slot;
1621 td->tt_scheduled = 0;
1622 td->state = DWC_CHAN_ST_START;
1626 /* allocate a new channel */
1627 if (dwc_otg_host_channel_alloc(sc, td, 0)) {
1628 td->state = DWC_CHAN_ST_START;
1632 channel = td->channel[0];
1634 td->hcsplt &= ~HCSPLT_COMPSPLT;
1635 td->state = DWC_CHAN_ST_WAIT_S_ANE;
1637 /* receive one packet */
1638 DWC_OTG_WRITE_4(sc, DOTG_HCTSIZ(channel),
1639 (HCTSIZ_PID_DATA0 << HCTSIZ_PID_SHIFT));
1641 DWC_OTG_WRITE_4(sc, DOTG_HCSPLT(channel), td->hcsplt);
1643 /* send after next SOF event */
1644 if ((sc->sc_last_frame_num & 1) == 0 &&
1645 td->ep_type == UE_ISOCHRONOUS)
1646 td->hcchar |= HCCHAR_ODDFRM;
1648 td->hcchar &= ~HCCHAR_ODDFRM;
1650 hcchar = td->hcchar;
1651 hcchar |= HCCHAR_EPDIR_IN;
1653 /* wait until next slot before trying complete split */
1654 td->tt_complete_slot = sc->sc_last_frame_num + 1;
1656 /* must enable channel before data can be received */
1657 DWC_OTG_WRITE_4(sc, DOTG_HCCHAR(channel), hcchar);
1659 return (1); /* busy */
1662 dwc_otg_host_channel_free(sc, td);
1663 return (0); /* complete */
1667 dwc_otg_data_rx(struct dwc_otg_softc *sc, struct dwc_otg_td *td)
1675 /* check endpoint status */
1676 if (sc->sc_last_rx_status == 0)
1679 if (GRXSTSRD_CHNUM_GET(sc->sc_last_rx_status) != td->ep_no)
1682 /* check for SETUP packet */
1683 if ((sc->sc_last_rx_status & GRXSTSRD_PKTSTS_MASK) ==
1684 GRXSTSRD_STP_DATA ||
1685 (sc->sc_last_rx_status & GRXSTSRD_PKTSTS_MASK) ==
1686 GRXSTSRD_STP_COMPLETE) {
1687 if (td->remainder == 0) {
1689 * We are actually complete and have
1690 * received the next SETUP
1692 DPRINTFN(5, "faking complete\n");
1693 return (0); /* complete */
1696 * USB Host Aborted the transfer.
1699 return (0); /* complete */
1702 if ((sc->sc_last_rx_status & GRXSTSRD_PKTSTS_MASK) !=
1703 GRXSTSRD_OUT_DATA) {
1705 dwc_otg_common_rx_ack(sc);
1709 /* get the packet byte count */
1710 count = GRXSTSRD_BCNT_GET(sc->sc_last_rx_status);
1712 /* verify the packet byte count */
1713 if (count != td->max_packet_size) {
1714 if (count < td->max_packet_size) {
1715 /* we have a short packet */
1719 /* invalid USB packet */
1723 dwc_otg_common_rx_ack(sc);
1724 return (0); /* we are complete */
1727 /* verify the packet byte count */
1728 if (count > td->remainder) {
1729 /* invalid USB packet */
1733 dwc_otg_common_rx_ack(sc);
1734 return (0); /* we are complete */
1737 /* read data from FIFO */
1738 dwc_otg_read_fifo(sc, td->pc, td->offset, count);
1740 td->remainder -= count;
1741 td->offset += count;
1744 dwc_otg_common_rx_ack(sc);
1746 temp = sc->sc_out_ctl[td->ep_no];
1748 /* check for isochronous mode */
1749 if ((temp & DIEPCTL_EPTYPE_MASK) ==
1750 (DIEPCTL_EPTYPE_ISOC << DIEPCTL_EPTYPE_SHIFT)) {
1751 /* toggle odd or even frame bit */
1752 if (temp & DIEPCTL_SETD1PID) {
1753 temp &= ~DIEPCTL_SETD1PID;
1754 temp |= DIEPCTL_SETD0PID;
1756 temp &= ~DIEPCTL_SETD0PID;
1757 temp |= DIEPCTL_SETD1PID;
1759 sc->sc_out_ctl[td->ep_no] = temp;
1762 /* check if we are complete */
1763 if ((td->remainder == 0) || got_short) {
1764 if (td->short_pkt) {
1765 /* we are complete */
1768 /* else need to receive a zero length packet */
1773 /* enable SETUP and transfer complete interrupt */
1774 if (td->ep_no == 0) {
1775 DWC_OTG_WRITE_4(sc, DOTG_DOEPTSIZ(0),
1776 DXEPTSIZ_SET_MULTI(3) |
1777 DXEPTSIZ_SET_NPKT(1) |
1778 DXEPTSIZ_SET_NBYTES(td->max_packet_size));
1780 /* allow reception of multiple packets */
1781 DWC_OTG_WRITE_4(sc, DOTG_DOEPTSIZ(td->ep_no),
1782 DXEPTSIZ_SET_MULTI(1) |
1783 DXEPTSIZ_SET_NPKT(4) |
1784 DXEPTSIZ_SET_NBYTES(4 *
1785 ((td->max_packet_size + 3) & ~3)));
1787 temp = sc->sc_out_ctl[td->ep_no];
1788 DWC_OTG_WRITE_4(sc, DOTG_DOEPCTL(td->ep_no), temp |
1789 DOEPCTL_EPENA | DOEPCTL_CNAK);
1791 return (1); /* not complete */
1795 dwc_otg_host_data_tx(struct dwc_otg_softc *sc, struct dwc_otg_td *td)
1804 dwc_otg_host_dump_rx(sc, td);
1806 /* check that last channel is complete */
1807 channel = td->channel[td->npkt];
1809 if (channel < DWC_OTG_MAX_CHANNELS) {
1810 hcint = sc->sc_chan_state[channel].hcint;
1812 DPRINTF("CH=%d ST=%d HCINT=0x%08x HCCHAR=0x%08x HCTSIZ=0x%08x\n",
1813 channel, td->state, hcint,
1814 DWC_OTG_READ_4(sc, DOTG_HCCHAR(channel)),
1815 DWC_OTG_READ_4(sc, DOTG_HCTSIZ(channel)));
1817 if (hcint & (HCINT_RETRY |
1818 HCINT_ACK | HCINT_NYET)) {
1819 /* give success bits priority over failure bits */
1820 } else if (hcint & HCINT_STALL) {
1821 DPRINTF("CH=%d STALL\n", channel);
1822 td->error_stall = 1;
1825 } else if (hcint & HCINT_ERRORS) {
1826 DPRINTF("CH=%d ERROR\n", channel);
1828 if (td->hcsplt != 0 || td->errcnt >= 3) {
1834 if (hcint & (HCINT_ERRORS | HCINT_RETRY |
1835 HCINT_ACK | HCINT_NYET)) {
1837 if (!(hcint & HCINT_ERRORS))
1844 switch (td->state) {
1845 case DWC_CHAN_ST_START:
1848 case DWC_CHAN_ST_WAIT_ANE:
1849 if (hcint & (HCINT_RETRY | HCINT_ERRORS)) {
1851 td->tt_scheduled = 0;
1853 } else if (hcint & (HCINT_ACK | HCINT_NYET)) {
1854 td->offset += td->tx_bytes;
1855 td->remainder -= td->tx_bytes;
1857 /* check if next response will be a NAK */
1858 if (hcint & HCINT_NYET)
1862 td->tt_scheduled = 0;
1864 /* check remainder */
1865 if (td->remainder == 0) {
1870 * Else we need to transmit a short
1878 case DWC_CHAN_ST_WAIT_S_ANE:
1879 if (hcint & (HCINT_RETRY | HCINT_ERRORS)) {
1881 td->tt_scheduled = 0;
1883 } else if (hcint & (HCINT_ACK | HCINT_NYET)) {
1889 case DWC_CHAN_ST_WAIT_C_ANE:
1890 if (hcint & HCINT_NYET) {
1892 } else if (hcint & (HCINT_RETRY | HCINT_ERRORS)) {
1894 td->tt_scheduled = 0;
1896 } else if (hcint & HCINT_ACK) {
1897 td->offset += td->tx_bytes;
1898 td->remainder -= td->tx_bytes;
1901 td->tt_scheduled = 0;
1903 /* check remainder */
1904 if (td->remainder == 0) {
1908 /* else we need to transmit a short packet */
1914 case DWC_CHAN_ST_WAIT_C_PKT:
1917 case DWC_CHAN_ST_TX_WAIT_ISOC:
1918 /* Check if ISOCHRONOUS OUT traffic is complete */
1919 if ((hcint & HCINT_HCH_DONE_MASK) == 0)
1922 td->offset += td->tx_bytes;
1923 td->remainder -= td->tx_bytes;
1931 /* free existing channel(s), if any */
1932 dwc_otg_host_channel_free(sc, td);
1934 if (td->hcsplt != 0) {
1935 delta = td->tt_start_slot - sc->sc_last_frame_num - 1;
1936 if (td->tt_scheduled == 0 || delta < DWC_OTG_TT_SLOT_MAX) {
1937 td->state = DWC_CHAN_ST_START;
1940 delta = sc->sc_last_frame_num - td->tt_start_slot;
1943 td->tt_scheduled = 0;
1944 td->state = DWC_CHAN_ST_START;
1947 } else if (dwc_otg_host_rate_check(sc, td)) {
1948 td->state = DWC_CHAN_ST_START;
1952 /* allocate a new channel */
1953 if (dwc_otg_host_channel_alloc(sc, td, 1)) {
1954 td->state = DWC_CHAN_ST_START;
1958 /* set toggle, if any */
1959 if (td->set_toggle) {
1964 if (td->ep_type == UE_ISOCHRONOUS) {
1965 /* ISOCHRONOUS OUT transfers don't have any ACKs */
1966 td->state = DWC_CHAN_ST_TX_WAIT_ISOC;
1967 td->hcsplt &= ~HCSPLT_COMPSPLT;
1968 if (td->hcsplt != 0) {
1969 /* get maximum transfer length */
1970 count = td->remainder;
1971 if (count > HCSPLT_XACTLEN_BURST) {
1972 DPRINTF("TT overflow\n");
1976 /* Update transaction position */
1977 td->hcsplt &= ~HCSPLT_XACTPOS_MASK;
1978 td->hcsplt |= (HCSPLT_XACTPOS_ALL << HCSPLT_XACTPOS_SHIFT);
1980 } else if (td->hcsplt != 0) {
1981 td->hcsplt &= ~HCSPLT_COMPSPLT;
1982 /* Wait for ACK/NAK/ERR from TT */
1983 td->state = DWC_CHAN_ST_WAIT_S_ANE;
1985 /* Wait for ACK/NAK/STALL from device */
1986 td->state = DWC_CHAN_ST_WAIT_ANE;
1991 for (x = 0; x != td->max_packet_count; x++) {
1994 channel = td->channel[x];
1996 /* send one packet at a time */
1997 count = td->max_packet_size;
1998 rem_bytes = td->remainder - td->tx_bytes;
1999 if (rem_bytes < count) {
2000 /* we have a short packet */
2004 if (count == rem_bytes) {
2008 DWC_OTG_WRITE_4(sc, DOTG_HCTSIZ(channel),
2009 (count << HCTSIZ_XFERSIZE_SHIFT) |
2010 (1 << HCTSIZ_PKTCNT_SHIFT) |
2011 (td->toggle ? (HCTSIZ_PID_DATA1 << HCTSIZ_PID_SHIFT) :
2012 (HCTSIZ_PID_DATA0 << HCTSIZ_PID_SHIFT)));
2015 DWC_OTG_WRITE_4(sc, DOTG_HCTSIZ(channel),
2016 (count << HCTSIZ_XFERSIZE_SHIFT) |
2017 (1 << HCTSIZ_PKTCNT_SHIFT) |
2018 (HCTSIZ_PID_DATA1 << HCTSIZ_PID_SHIFT));
2021 DWC_OTG_WRITE_4(sc, DOTG_HCTSIZ(channel),
2022 (count << HCTSIZ_XFERSIZE_SHIFT) |
2023 (1 << HCTSIZ_PKTCNT_SHIFT) |
2024 (HCTSIZ_PID_DATA2 << HCTSIZ_PID_SHIFT));
2027 } else if (td->ep_type == UE_ISOCHRONOUS &&
2028 td->max_packet_count > 1) {
2029 /* ISOCHRONOUS multi packet */
2030 DWC_OTG_WRITE_4(sc, DOTG_HCTSIZ(channel),
2031 (count << HCTSIZ_XFERSIZE_SHIFT) |
2032 (1 << HCTSIZ_PKTCNT_SHIFT) |
2033 (HCTSIZ_PID_MDATA << HCTSIZ_PID_SHIFT));
2035 /* TODO: HCTSIZ_DOPNG */
2036 /* standard BULK/INTERRUPT/CONTROL packet */
2037 DWC_OTG_WRITE_4(sc, DOTG_HCTSIZ(channel),
2038 (count << HCTSIZ_XFERSIZE_SHIFT) |
2039 (1 << HCTSIZ_PKTCNT_SHIFT) |
2040 (td->toggle ? (HCTSIZ_PID_DATA1 << HCTSIZ_PID_SHIFT) :
2041 (HCTSIZ_PID_DATA0 << HCTSIZ_PID_SHIFT)));
2044 DWC_OTG_WRITE_4(sc, DOTG_HCSPLT(channel), td->hcsplt);
2046 hcchar = td->hcchar;
2047 hcchar &= ~HCCHAR_EPDIR_IN;
2049 /* send after next SOF event */
2050 if ((sc->sc_last_frame_num & 1) == 0 &&
2051 td->ep_type == UE_ISOCHRONOUS)
2052 hcchar |= HCCHAR_ODDFRM;
2054 hcchar &= ~HCCHAR_ODDFRM;
2056 /* must enable before writing data to FIFO */
2057 DWC_OTG_WRITE_4(sc, DOTG_HCCHAR(channel), hcchar);
2060 /* write data into FIFO */
2061 dwc_otg_write_fifo(sc, td->pc, td->offset +
2062 td->tx_bytes, DOTG_DFIFO(channel), count);
2065 /* store number of bytes transmitted */
2066 td->tx_bytes += count;
2068 /* store last packet index */
2071 /* check for last packet */
2072 if (count == rem_bytes)
2078 /* free existing channel, if any */
2079 dwc_otg_host_channel_free(sc, td);
2081 delta = td->tt_complete_slot - sc->sc_last_frame_num - 1;
2082 if (td->tt_scheduled == 0 || delta < DWC_OTG_TT_SLOT_MAX) {
2083 td->state = DWC_CHAN_ST_WAIT_C_PKT;
2086 delta = sc->sc_last_frame_num - td->tt_start_slot;
2087 if (delta > DWC_OTG_TT_SLOT_MAX) {
2088 /* we missed the service interval */
2089 if (td->ep_type != UE_ISOCHRONOUS)
2094 /* allocate a new channel */
2095 if (dwc_otg_host_channel_alloc(sc, td, 0)) {
2096 td->state = DWC_CHAN_ST_WAIT_C_PKT;
2100 channel = td->channel[0];
2102 td->hcsplt |= HCSPLT_COMPSPLT;
2103 td->state = DWC_CHAN_ST_WAIT_C_ANE;
2105 DWC_OTG_WRITE_4(sc, DOTG_HCTSIZ(channel),
2106 (HCTSIZ_PID_DATA0 << HCTSIZ_PID_SHIFT));
2108 DWC_OTG_WRITE_4(sc, DOTG_HCSPLT(channel), td->hcsplt);
2110 hcchar = td->hcchar;
2111 hcchar &= ~HCCHAR_EPDIR_IN;
2113 /* receive complete split ASAP */
2114 if ((sc->sc_last_frame_num & 1) != 0 &&
2115 td->ep_type == UE_ISOCHRONOUS)
2116 hcchar |= HCCHAR_ODDFRM;
2118 hcchar &= ~HCCHAR_ODDFRM;
2120 /* must enable channel before data can be received */
2121 DWC_OTG_WRITE_4(sc, DOTG_HCCHAR(channel), hcchar);
2123 /* wait until next slot before trying complete split */
2124 td->tt_complete_slot = sc->sc_last_frame_num + 1;
2126 return (1); /* busy */
2129 dwc_otg_host_channel_free(sc, td);
2130 return (0); /* complete */
2134 dwc_otg_data_tx(struct dwc_otg_softc *sc, struct dwc_otg_td *td)
2136 uint32_t max_buffer;
2143 to = 3; /* don't loop forever! */
2145 max_buffer = sc->sc_hw_ep_profile[td->ep_no].max_buffer;
2148 /* check for for endpoint 0 data */
2150 temp = sc->sc_last_rx_status;
2152 if ((td->ep_no == 0) && (temp != 0) &&
2153 (GRXSTSRD_CHNUM_GET(temp) == 0)) {
2155 if ((temp & GRXSTSRD_PKTSTS_MASK) !=
2156 GRXSTSRD_STP_DATA &&
2157 (temp & GRXSTSRD_PKTSTS_MASK) !=
2158 GRXSTSRD_STP_COMPLETE) {
2160 /* dump data - wrong direction */
2161 dwc_otg_common_rx_ack(sc);
2164 * The current transfer was cancelled
2168 return (0); /* complete */
2172 /* fill in more TX data, if possible */
2173 if (td->tx_bytes != 0) {
2177 /* check if packets have been transferred */
2178 temp = DWC_OTG_READ_4(sc, DOTG_DIEPTSIZ(td->ep_no));
2180 /* get current packet number */
2181 cpkt = DXEPTSIZ_GET_NPKT(temp);
2183 if (cpkt >= td->npkt) {
2186 if (max_buffer != 0) {
2187 fifo_left = (td->npkt - cpkt) *
2188 td->max_packet_size;
2190 if (fifo_left > max_buffer)
2191 fifo_left = max_buffer;
2193 fifo_left = td->max_packet_size;
2197 count = td->tx_bytes;
2198 if (count > fifo_left)
2202 /* write data into FIFO */
2203 dwc_otg_write_fifo(sc, td->pc, td->offset,
2204 DOTG_DFIFO(td->ep_no), count);
2206 td->tx_bytes -= count;
2207 td->remainder -= count;
2208 td->offset += count;
2211 if (td->tx_bytes != 0)
2214 /* check remainder */
2215 if (td->remainder == 0) {
2217 return (0); /* complete */
2219 /* else we need to transmit a short packet */
2226 /* check if not all packets have been transferred */
2227 temp = DWC_OTG_READ_4(sc, DOTG_DIEPTSIZ(td->ep_no));
2229 if (DXEPTSIZ_GET_NPKT(temp) != 0) {
2231 DPRINTFN(5, "busy ep=%d npkt=%d DIEPTSIZ=0x%08x "
2232 "DIEPCTL=0x%08x\n", td->ep_no,
2233 DXEPTSIZ_GET_NPKT(temp),
2234 temp, DWC_OTG_READ_4(sc, DOTG_DIEPCTL(td->ep_no)));
2239 DPRINTFN(5, "rem=%u ep=%d\n", td->remainder, td->ep_no);
2241 /* try to optimise by sending more data */
2242 if ((max_buffer != 0) && ((td->max_packet_size & 3) == 0)) {
2244 /* send multiple packets at the same time */
2245 mpkt = max_buffer / td->max_packet_size;
2250 count = td->remainder;
2251 if (count > 0x7FFFFF)
2252 count = 0x7FFFFF - (0x7FFFFF % td->max_packet_size);
2254 td->npkt = count / td->max_packet_size;
2257 * NOTE: We could use 0x3FE instead of "mpkt" in the
2258 * check below to get more throughput, but then we
2259 * have a dependency towards non-generic chip features
2260 * to disable the TX-FIFO-EMPTY interrupts on a per
2261 * endpoint basis. Increase the maximum buffer size of
2262 * the IN endpoint to increase the performance.
2264 if (td->npkt > mpkt) {
2266 count = td->max_packet_size * mpkt;
2267 } else if ((count == 0) || (count % td->max_packet_size)) {
2268 /* we are transmitting a short packet */
2273 /* send one packet at a time */
2275 count = td->max_packet_size;
2276 if (td->remainder < count) {
2277 /* we have a short packet */
2279 count = td->remainder;
2283 DWC_OTG_WRITE_4(sc, DOTG_DIEPTSIZ(td->ep_no),
2284 DXEPTSIZ_SET_MULTI(1) |
2285 DXEPTSIZ_SET_NPKT(td->npkt) |
2286 DXEPTSIZ_SET_NBYTES(count));
2288 /* make room for buffering */
2291 temp = sc->sc_in_ctl[td->ep_no];
2293 /* check for isochronous mode */
2294 if ((temp & DIEPCTL_EPTYPE_MASK) ==
2295 (DIEPCTL_EPTYPE_ISOC << DIEPCTL_EPTYPE_SHIFT)) {
2296 /* toggle odd or even frame bit */
2297 if (temp & DIEPCTL_SETD1PID) {
2298 temp &= ~DIEPCTL_SETD1PID;
2299 temp |= DIEPCTL_SETD0PID;
2301 temp &= ~DIEPCTL_SETD0PID;
2302 temp |= DIEPCTL_SETD1PID;
2304 sc->sc_in_ctl[td->ep_no] = temp;
2307 /* must enable before writing data to FIFO */
2308 DWC_OTG_WRITE_4(sc, DOTG_DIEPCTL(td->ep_no), temp |
2309 DIEPCTL_EPENA | DIEPCTL_CNAK);
2311 td->tx_bytes = count;
2313 /* check remainder */
2314 if (td->tx_bytes == 0 &&
2315 td->remainder == 0) {
2317 return (0); /* complete */
2319 /* else we need to transmit a short packet */
2324 return (1); /* not complete */
2328 dwc_otg_data_tx_sync(struct dwc_otg_softc *sc, struct dwc_otg_td *td)
2333 * If all packets are transferred we are complete:
2335 temp = DWC_OTG_READ_4(sc, DOTG_DIEPTSIZ(td->ep_no));
2337 /* check that all packets have been transferred */
2338 if (DXEPTSIZ_GET_NPKT(temp) != 0) {
2339 DPRINTFN(5, "busy ep=%d\n", td->ep_no);
2346 /* we only want to know if there is a SETUP packet or free IN packet */
2348 temp = sc->sc_last_rx_status;
2350 if ((td->ep_no == 0) && (temp != 0) &&
2351 (GRXSTSRD_CHNUM_GET(temp) == 0)) {
2353 if ((temp & GRXSTSRD_PKTSTS_MASK) ==
2354 GRXSTSRD_STP_DATA ||
2355 (temp & GRXSTSRD_PKTSTS_MASK) ==
2356 GRXSTSRD_STP_COMPLETE) {
2357 DPRINTFN(5, "faking complete\n");
2359 * Race condition: We are complete!
2363 /* dump data - wrong direction */
2364 dwc_otg_common_rx_ack(sc);
2367 return (1); /* not complete */
2371 dwc_otg_xfer_do_fifo(struct dwc_otg_softc *sc, struct usb_xfer *xfer)
2373 struct dwc_otg_td *td;
2380 td = xfer->td_transfer_cache;
2385 if ((td->func) (sc, td)) {
2386 /* operation in progress */
2389 if (((void *)td) == xfer->td_transfer_last) {
2392 if (td->error_any) {
2394 } else if (td->remainder > 0) {
2396 * We had a short transfer. If there is no alternate
2397 * next, stop processing !
2404 * Fetch the next transfer descriptor and transfer
2405 * some flags to the next transfer descriptor
2407 tmr_res = td->tmr_res;
2408 tmr_val = td->tmr_val;
2409 toggle = td->toggle;
2411 xfer->td_transfer_cache = td;
2412 td->toggle = toggle; /* transfer toggle */
2413 td->tmr_res = tmr_res;
2414 td->tmr_val = tmr_val;
2419 xfer->td_transfer_cache = NULL;
2420 sc->sc_xfer_complete = 1;
2424 dwc_otg_xfer_do_complete_locked(struct dwc_otg_softc *sc, struct usb_xfer *xfer)
2426 struct dwc_otg_td *td;
2430 td = xfer->td_transfer_cache;
2432 /* compute all actual lengths */
2433 dwc_otg_standard_done(xfer);
2440 dwc_otg_timer(void *_sc)
2442 struct dwc_otg_softc *sc = _sc;
2444 USB_BUS_LOCK_ASSERT(&sc->sc_bus, MA_OWNED);
2448 USB_BUS_SPIN_LOCK(&sc->sc_bus);
2450 /* increment timer value */
2453 /* enable SOF interrupt, which will poll jobs */
2454 dwc_otg_enable_sof_irq(sc);
2456 USB_BUS_SPIN_UNLOCK(&sc->sc_bus);
2458 if (sc->sc_timer_active) {
2460 usb_callout_reset(&sc->sc_timer,
2461 hz / (1000 / DWC_OTG_HOST_TIMER_RATE),
2462 &dwc_otg_timer, sc);
2467 dwc_otg_timer_start(struct dwc_otg_softc *sc)
2469 if (sc->sc_timer_active != 0)
2472 sc->sc_timer_active = 1;
2475 usb_callout_reset(&sc->sc_timer,
2476 hz / (1000 / DWC_OTG_HOST_TIMER_RATE),
2477 &dwc_otg_timer, sc);
2481 dwc_otg_timer_stop(struct dwc_otg_softc *sc)
2483 if (sc->sc_timer_active == 0)
2486 sc->sc_timer_active = 0;
2489 usb_callout_stop(&sc->sc_timer);
2493 dwc_otg_compute_isoc_rx_tt_slot(struct dwc_otg_tt_info *pinfo)
2495 if (pinfo->slot_index < DWC_OTG_TT_SLOT_MAX)
2496 pinfo->slot_index++;
2497 return (pinfo->slot_index);
2501 dwc_otg_update_host_transfer_schedule_locked(struct dwc_otg_softc *sc)
2503 TAILQ_HEAD(, usb_xfer) head;
2504 struct usb_xfer *xfer;
2505 struct usb_xfer *xfer_next;
2506 struct dwc_otg_td *td;
2510 temp = DWC_OTG_READ_4(sc, DOTG_HFNUM) & DWC_OTG_FRAME_MASK;
2512 if (sc->sc_last_frame_num == temp)
2515 sc->sc_last_frame_num = temp;
2519 if ((temp & 7) == 0) {
2521 /* reset the schedule */
2522 memset(sc->sc_tt_info, 0, sizeof(sc->sc_tt_info));
2524 TAILQ_FOREACH_SAFE(xfer, &sc->sc_bus.intr_q.head, wait_entry, xfer_next) {
2525 td = xfer->td_transfer_cache;
2526 if (td == NULL || td->ep_type != UE_ISOCHRONOUS)
2529 /* check for IN direction */
2530 if ((td->hcchar & HCCHAR_EPDIR_IN) != 0)
2535 if (td->hcsplt == 0 || td->tt_scheduled != 0)
2539 slot = dwc_otg_compute_isoc_rx_tt_slot(
2540 sc->sc_tt_info + td->tt_index);
2543 * Not enough time to get complete
2549 td->tt_start_slot = temp + slot;
2550 td->tt_scheduled = 1;
2551 TAILQ_REMOVE(&sc->sc_bus.intr_q.head, xfer, wait_entry);
2552 TAILQ_INSERT_TAIL(&head, xfer, wait_entry);
2555 TAILQ_FOREACH_SAFE(xfer, &sc->sc_bus.intr_q.head, wait_entry, xfer_next) {
2556 td = xfer->td_transfer_cache;
2557 if (td == NULL || td->ep_type != UE_ISOCHRONOUS)
2560 /* check for OUT direction */
2561 if ((td->hcchar & HCCHAR_EPDIR_IN) == 0)
2566 if (td->hcsplt == 0 || td->tt_scheduled != 0)
2570 td->tt_start_slot = temp;
2571 td->tt_scheduled = 1;
2572 TAILQ_REMOVE(&sc->sc_bus.intr_q.head, xfer, wait_entry);
2573 TAILQ_INSERT_TAIL(&head, xfer, wait_entry);
2576 TAILQ_FOREACH_SAFE(xfer, &sc->sc_bus.intr_q.head, wait_entry, xfer_next) {
2577 td = xfer->td_transfer_cache;
2578 if (td == NULL || td->ep_type != UE_INTERRUPT)
2581 if (td->tt_scheduled != 0) {
2586 if (dwc_otg_host_rate_check_interrupt(sc, td))
2589 if (td->hcsplt == 0) {
2591 td->tt_scheduled = 1;
2596 td->tt_start_slot = temp;
2598 td->tt_scheduled = 1;
2599 TAILQ_REMOVE(&sc->sc_bus.intr_q.head, xfer, wait_entry);
2600 TAILQ_INSERT_TAIL(&head, xfer, wait_entry);
2603 TAILQ_FOREACH_SAFE(xfer, &sc->sc_bus.intr_q.head, wait_entry, xfer_next) {
2604 td = xfer->td_transfer_cache;
2606 td->ep_type != UE_CONTROL) {
2612 if (td->hcsplt == 0 || td->tt_scheduled != 0)
2616 td->tt_start_slot = temp;
2617 td->tt_scheduled = 1;
2618 TAILQ_REMOVE(&sc->sc_bus.intr_q.head, xfer, wait_entry);
2619 TAILQ_INSERT_TAIL(&head, xfer, wait_entry);
2622 if ((temp & 7) < 6) {
2623 TAILQ_FOREACH_SAFE(xfer, &sc->sc_bus.intr_q.head, wait_entry, xfer_next) {
2624 td = xfer->td_transfer_cache;
2626 td->ep_type != UE_BULK) {
2632 if (td->hcsplt == 0 || td->tt_scheduled != 0)
2636 td->tt_start_slot = temp;
2637 td->tt_scheduled = 1;
2638 TAILQ_REMOVE(&sc->sc_bus.intr_q.head, xfer, wait_entry);
2639 TAILQ_INSERT_TAIL(&head, xfer, wait_entry);
2643 /* Put TT transfers in execution order at the end */
2644 TAILQ_CONCAT(&sc->sc_bus.intr_q.head, &head, wait_entry);
2646 /* move all TT transfers in front, keeping the current order */
2647 TAILQ_FOREACH_SAFE(xfer, &sc->sc_bus.intr_q.head, wait_entry, xfer_next) {
2648 td = xfer->td_transfer_cache;
2649 if (td == NULL || td->hcsplt == 0)
2651 TAILQ_REMOVE(&sc->sc_bus.intr_q.head, xfer, wait_entry);
2652 TAILQ_INSERT_TAIL(&head, xfer, wait_entry);
2654 TAILQ_CONCAT(&head, &sc->sc_bus.intr_q.head, wait_entry);
2655 TAILQ_CONCAT(&sc->sc_bus.intr_q.head, &head, wait_entry);
2657 /* put non-TT non-ISOCHRONOUS transfers last */
2658 TAILQ_FOREACH_SAFE(xfer, &sc->sc_bus.intr_q.head, wait_entry, xfer_next) {
2659 td = xfer->td_transfer_cache;
2660 if (td == NULL || td->hcsplt != 0 || td->ep_type == UE_ISOCHRONOUS)
2662 TAILQ_REMOVE(&sc->sc_bus.intr_q.head, xfer, wait_entry);
2663 TAILQ_INSERT_TAIL(&head, xfer, wait_entry);
2665 TAILQ_CONCAT(&sc->sc_bus.intr_q.head, &head, wait_entry);
2667 if ((temp & 7) == 0) {
2669 DPRINTFN(12, "SOF interrupt #%d, needsof=%d\n",
2670 (int)temp, (int)sc->sc_needsof);
2672 /* update SOF IRQ mask */
2673 if (sc->sc_irq_mask & GINTMSK_SOFMSK) {
2674 if (sc->sc_needsof == 0) {
2675 sc->sc_irq_mask &= ~GINTMSK_SOFMSK;
2676 DWC_OTG_WRITE_4(sc, DOTG_GINTMSK, sc->sc_irq_mask);
2679 if (sc->sc_needsof != 0) {
2680 sc->sc_irq_mask |= GINTMSK_SOFMSK;
2681 DWC_OTG_WRITE_4(sc, DOTG_GINTMSK, sc->sc_irq_mask);
2685 /* clear need SOF flag */
2692 dwc_otg_interrupt_poll_locked(struct dwc_otg_softc *sc)
2694 struct usb_xfer *xfer;
2698 uint8_t got_rx_status;
2701 if (sc->sc_flags.status_device_mode == 0) {
2703 * Update host transfer schedule, so that new
2704 * transfers can be issued:
2706 dwc_otg_update_host_transfer_schedule_locked(sc);
2710 if (++count == 16) {
2711 /* give other interrupts a chance */
2716 /* get all host channel interrupts */
2717 haint = DWC_OTG_READ_4(sc, DOTG_HAINT);
2720 if (x >= sc->sc_host_ch_max)
2722 temp = DWC_OTG_READ_4(sc, DOTG_HCINT(x));
2723 DWC_OTG_WRITE_4(sc, DOTG_HCINT(x), temp);
2724 temp &= ~HCINT_SOFTWARE_ONLY;
2725 sc->sc_chan_state[x].hcint |= temp;
2726 haint &= ~(1U << x);
2729 if (sc->sc_last_rx_status == 0) {
2731 temp = DWC_OTG_READ_4(sc, DOTG_GINTSTS);
2732 if (temp & GINTSTS_RXFLVL) {
2733 /* pop current status */
2734 sc->sc_last_rx_status =
2735 DWC_OTG_READ_4(sc, DOTG_GRXSTSPD);
2738 if (sc->sc_last_rx_status != 0) {
2742 temp = sc->sc_last_rx_status &
2743 GRXSTSRD_PKTSTS_MASK;
2745 /* non-data messages we simply skip */
2746 if (temp != GRXSTSRD_STP_DATA &&
2747 temp != GRXSTSRD_STP_COMPLETE &&
2748 temp != GRXSTSRD_OUT_DATA) {
2749 /* check for halted channel */
2750 if (temp == GRXSTSRH_HALTED) {
2751 ep_no = GRXSTSRD_CHNUM_GET(sc->sc_last_rx_status);
2752 sc->sc_chan_state[ep_no].wait_halted = 0;
2753 DPRINTFN(5, "channel halt complete ch=%u\n", ep_no);
2755 /* store bytes and FIFO offset */
2756 sc->sc_current_rx_bytes = 0;
2757 sc->sc_current_rx_fifo = 0;
2759 /* acknowledge status */
2760 dwc_otg_common_rx_ack(sc);
2764 temp = GRXSTSRD_BCNT_GET(
2765 sc->sc_last_rx_status);
2766 ep_no = GRXSTSRD_CHNUM_GET(
2767 sc->sc_last_rx_status);
2769 /* store bytes and FIFO offset */
2770 sc->sc_current_rx_bytes = (temp + 3) & ~3;
2771 sc->sc_current_rx_fifo = DOTG_DFIFO(ep_no);
2773 DPRINTF("Reading %d bytes from ep %d\n", temp, ep_no);
2775 /* check if we should dump the data */
2776 if (!(sc->sc_active_rx_ep & (1U << ep_no))) {
2777 dwc_otg_common_rx_ack(sc);
2783 DPRINTFN(5, "RX status = 0x%08x: ch=%d pid=%d bytes=%d sts=%d\n",
2784 sc->sc_last_rx_status, ep_no,
2785 (sc->sc_last_rx_status >> 15) & 3,
2786 GRXSTSRD_BCNT_GET(sc->sc_last_rx_status),
2787 (sc->sc_last_rx_status >> 17) & 15);
2794 ep_no = GRXSTSRD_CHNUM_GET(
2795 sc->sc_last_rx_status);
2797 /* check if we should dump the data */
2798 if (!(sc->sc_active_rx_ep & (1U << ep_no))) {
2799 dwc_otg_common_rx_ack(sc);
2807 TAILQ_FOREACH(xfer, &sc->sc_bus.intr_q.head, wait_entry)
2808 dwc_otg_xfer_do_fifo(sc, xfer);
2810 if (got_rx_status) {
2811 /* check if data was consumed */
2812 if (sc->sc_last_rx_status == 0)
2815 /* disable RX FIFO level interrupt */
2816 sc->sc_irq_mask &= ~GINTMSK_RXFLVLMSK;
2817 DWC_OTG_WRITE_4(sc, DOTG_GINTMSK, sc->sc_irq_mask);
2822 dwc_otg_interrupt_complete_locked(struct dwc_otg_softc *sc)
2824 struct usb_xfer *xfer;
2826 /* scan for completion events */
2827 TAILQ_FOREACH(xfer, &sc->sc_bus.intr_q.head, wait_entry) {
2828 if (dwc_otg_xfer_do_complete_locked(sc, xfer))
2834 dwc_otg_vbus_interrupt(struct dwc_otg_softc *sc, uint8_t is_on)
2836 DPRINTFN(5, "vbus = %u\n", is_on);
2839 * If the USB host mode is forced, then assume VBUS is always
2840 * present else rely on the input to this function:
2842 if ((is_on != 0) || (sc->sc_mode == DWC_MODE_HOST)) {
2844 if (!sc->sc_flags.status_vbus) {
2845 sc->sc_flags.status_vbus = 1;
2847 /* complete root HUB interrupt endpoint */
2849 dwc_otg_root_intr(sc);
2852 if (sc->sc_flags.status_vbus) {
2853 sc->sc_flags.status_vbus = 0;
2854 sc->sc_flags.status_bus_reset = 0;
2855 sc->sc_flags.status_suspend = 0;
2856 sc->sc_flags.change_suspend = 0;
2857 sc->sc_flags.change_connect = 1;
2859 /* complete root HUB interrupt endpoint */
2861 dwc_otg_root_intr(sc);
2867 dwc_otg_filter_interrupt(void *arg)
2869 struct dwc_otg_softc *sc = arg;
2870 int retval = FILTER_HANDLED;
2873 USB_BUS_SPIN_LOCK(&sc->sc_bus);
2875 /* read and clear interrupt status */
2876 status = DWC_OTG_READ_4(sc, DOTG_GINTSTS);
2878 /* clear interrupts we are handling here */
2879 DWC_OTG_WRITE_4(sc, DOTG_GINTSTS, status & ~DWC_OTG_MSK_GINT_THREAD_IRQ);
2881 /* check for USB state change interrupts */
2882 if ((status & DWC_OTG_MSK_GINT_THREAD_IRQ) != 0)
2883 retval = FILTER_SCHEDULE_THREAD;
2885 /* clear FIFO empty interrupts */
2886 if (status & sc->sc_irq_mask &
2887 (GINTSTS_PTXFEMP | GINTSTS_NPTXFEMP)) {
2888 sc->sc_irq_mask &= ~(GINTSTS_PTXFEMP | GINTSTS_NPTXFEMP);
2889 DWC_OTG_WRITE_4(sc, DOTG_GINTMSK, sc->sc_irq_mask);
2891 /* clear all IN endpoint interrupts */
2892 if (status & GINTSTS_IEPINT) {
2896 for (x = 0; x != sc->sc_dev_in_ep_max; x++) {
2897 temp = DWC_OTG_READ_4(sc, DOTG_DIEPINT(x));
2899 * NOTE: Need to clear all interrupt bits,
2900 * because some appears to be unmaskable and
2901 * can cause an interrupt loop:
2904 DWC_OTG_WRITE_4(sc, DOTG_DIEPINT(x), temp);
2908 /* poll FIFOs, if any */
2909 dwc_otg_interrupt_poll_locked(sc);
2911 if (sc->sc_xfer_complete != 0)
2912 retval = FILTER_SCHEDULE_THREAD;
2914 USB_BUS_SPIN_UNLOCK(&sc->sc_bus);
2920 dwc_otg_interrupt(void *arg)
2922 struct dwc_otg_softc *sc = arg;
2925 USB_BUS_LOCK(&sc->sc_bus);
2926 USB_BUS_SPIN_LOCK(&sc->sc_bus);
2928 /* read and clear interrupt status */
2929 status = DWC_OTG_READ_4(sc, DOTG_GINTSTS);
2931 /* clear interrupts we are handling here */
2932 DWC_OTG_WRITE_4(sc, DOTG_GINTSTS, status & DWC_OTG_MSK_GINT_THREAD_IRQ);
2934 DPRINTFN(14, "GINTSTS=0x%08x HAINT=0x%08x HFNUM=0x%08x\n",
2935 status, DWC_OTG_READ_4(sc, DOTG_HAINT),
2936 DWC_OTG_READ_4(sc, DOTG_HFNUM));
2938 if (status & GINTSTS_USBRST) {
2940 /* set correct state */
2941 sc->sc_flags.status_device_mode = 1;
2942 sc->sc_flags.status_bus_reset = 0;
2943 sc->sc_flags.status_suspend = 0;
2944 sc->sc_flags.change_suspend = 0;
2945 sc->sc_flags.change_connect = 1;
2947 /* Disable SOF interrupt */
2948 sc->sc_irq_mask &= ~GINTMSK_SOFMSK;
2949 DWC_OTG_WRITE_4(sc, DOTG_GINTMSK, sc->sc_irq_mask);
2951 /* complete root HUB interrupt endpoint */
2952 dwc_otg_root_intr(sc);
2955 /* check for any bus state change interrupts */
2956 if (status & GINTSTS_ENUMDONE) {
2960 DPRINTFN(5, "end of reset\n");
2962 /* set correct state */
2963 sc->sc_flags.status_device_mode = 1;
2964 sc->sc_flags.status_bus_reset = 1;
2965 sc->sc_flags.status_suspend = 0;
2966 sc->sc_flags.change_suspend = 0;
2967 sc->sc_flags.change_connect = 1;
2968 sc->sc_flags.status_low_speed = 0;
2969 sc->sc_flags.port_enabled = 1;
2972 (void) dwc_otg_init_fifo(sc, DWC_MODE_DEVICE);
2974 /* reset function address */
2975 dwc_otg_set_address(sc, 0);
2977 /* figure out enumeration speed */
2978 temp = DWC_OTG_READ_4(sc, DOTG_DSTS);
2979 if (DSTS_ENUMSPD_GET(temp) == DSTS_ENUMSPD_HI)
2980 sc->sc_flags.status_high_speed = 1;
2982 sc->sc_flags.status_high_speed = 0;
2985 * Disable resume and SOF interrupt, and enable
2986 * suspend and RX frame interrupt:
2988 sc->sc_irq_mask &= ~(GINTMSK_WKUPINTMSK | GINTMSK_SOFMSK);
2989 sc->sc_irq_mask |= GINTMSK_USBSUSPMSK;
2990 DWC_OTG_WRITE_4(sc, DOTG_GINTMSK, sc->sc_irq_mask);
2992 /* complete root HUB interrupt endpoint */
2993 dwc_otg_root_intr(sc);
2996 if (status & GINTSTS_PRTINT) {
2999 hprt = DWC_OTG_READ_4(sc, DOTG_HPRT);
3001 /* clear change bits */
3002 DWC_OTG_WRITE_4(sc, DOTG_HPRT, (hprt & (
3003 HPRT_PRTPWR | HPRT_PRTENCHNG |
3004 HPRT_PRTCONNDET | HPRT_PRTOVRCURRCHNG)) |
3007 DPRINTFN(12, "GINTSTS=0x%08x, HPRT=0x%08x\n", status, hprt);
3009 sc->sc_flags.status_device_mode = 0;
3011 if (hprt & HPRT_PRTCONNSTS)
3012 sc->sc_flags.status_bus_reset = 1;
3014 sc->sc_flags.status_bus_reset = 0;
3016 if ((hprt & HPRT_PRTENCHNG) &&
3017 (hprt & HPRT_PRTENA) == 0)
3018 sc->sc_flags.change_enabled = 1;
3020 if (hprt & HPRT_PRTENA)
3021 sc->sc_flags.port_enabled = 1;
3023 sc->sc_flags.port_enabled = 0;
3025 if (hprt & HPRT_PRTOVRCURRCHNG)
3026 sc->sc_flags.change_over_current = 1;
3028 if (hprt & HPRT_PRTOVRCURRACT)
3029 sc->sc_flags.port_over_current = 1;
3031 sc->sc_flags.port_over_current = 0;
3033 if (hprt & HPRT_PRTPWR)
3034 sc->sc_flags.port_powered = 1;
3036 sc->sc_flags.port_powered = 0;
3038 if (((hprt & HPRT_PRTSPD_MASK)
3039 >> HPRT_PRTSPD_SHIFT) == HPRT_PRTSPD_LOW)
3040 sc->sc_flags.status_low_speed = 1;
3042 sc->sc_flags.status_low_speed = 0;
3044 if (((hprt & HPRT_PRTSPD_MASK)
3045 >> HPRT_PRTSPD_SHIFT) == HPRT_PRTSPD_HIGH)
3046 sc->sc_flags.status_high_speed = 1;
3048 sc->sc_flags.status_high_speed = 0;
3050 if (hprt & HPRT_PRTCONNDET)
3051 sc->sc_flags.change_connect = 1;
3053 if (hprt & HPRT_PRTSUSP)
3054 dwc_otg_suspend_irq(sc);
3056 dwc_otg_resume_irq(sc);
3058 /* complete root HUB interrupt endpoint */
3059 dwc_otg_root_intr(sc);
3061 /* update host frame interval */
3062 dwc_otg_update_host_frame_interval(sc);
3066 * If resume and suspend is set at the same time we interpret
3067 * that like RESUME. Resume is set when there is at least 3
3068 * milliseconds of inactivity on the USB BUS.
3070 if (status & GINTSTS_WKUPINT) {
3072 DPRINTFN(5, "resume interrupt\n");
3074 dwc_otg_resume_irq(sc);
3076 } else if (status & GINTSTS_USBSUSP) {
3078 DPRINTFN(5, "suspend interrupt\n");
3080 dwc_otg_suspend_irq(sc);
3083 if (status & (GINTSTS_USBSUSP |
3086 GINTSTS_SESSREQINT)) {
3089 temp = DWC_OTG_READ_4(sc, DOTG_GOTGCTL);
3091 DPRINTFN(5, "GOTGCTL=0x%08x\n", temp);
3093 dwc_otg_vbus_interrupt(sc,
3094 (temp & (GOTGCTL_ASESVLD | GOTGCTL_BSESVLD)) ? 1 : 0);
3097 if (sc->sc_xfer_complete != 0) {
3098 sc->sc_xfer_complete = 0;
3100 /* complete FIFOs, if any */
3101 dwc_otg_interrupt_complete_locked(sc);
3103 USB_BUS_SPIN_UNLOCK(&sc->sc_bus);
3104 USB_BUS_UNLOCK(&sc->sc_bus);
3108 dwc_otg_setup_standard_chain_sub(struct dwc_otg_std_temp *temp)
3110 struct dwc_otg_td *td;
3112 /* get current Transfer Descriptor */
3116 /* prepare for next TD */
3117 temp->td_next = td->obj_next;
3119 /* fill out the Transfer Descriptor */
3120 td->func = temp->func;
3122 td->offset = temp->offset;
3123 td->remainder = temp->len;
3126 td->error_stall = 0;
3128 td->did_stall = temp->did_stall;
3129 td->short_pkt = temp->short_pkt;
3130 td->alt_next = temp->setup_alt_next;
3134 td->channel[0] = DWC_OTG_MAX_CHANNELS;
3135 td->channel[1] = DWC_OTG_MAX_CHANNELS;
3136 td->channel[2] = DWC_OTG_MAX_CHANNELS;
3139 td->tt_scheduled = 0;
3140 td->tt_xactpos = HCSPLT_XACTPOS_BEGIN;
3144 dwc_otg_setup_standard_chain(struct usb_xfer *xfer)
3146 struct dwc_otg_std_temp temp;
3147 struct dwc_otg_td *td;
3152 DPRINTFN(9, "addr=%d endpt=%d sumlen=%d speed=%d\n",
3153 xfer->address, UE_GET_ADDR(xfer->endpointno),
3154 xfer->sumlen, usbd_get_speed(xfer->xroot->udev));
3156 temp.max_frame_size = xfer->max_frame_size;
3158 td = xfer->td_start[0];
3159 xfer->td_transfer_first = td;
3160 xfer->td_transfer_cache = td;
3166 temp.td_next = xfer->td_start[0];
3168 temp.setup_alt_next = xfer->flags_int.short_frames_ok ||
3169 xfer->flags_int.isochronous_xfr;
3170 temp.did_stall = !xfer->flags_int.control_stall;
3172 is_host = (xfer->xroot->udev->flags.usb_mode == USB_MODE_HOST);
3174 /* check if we should prepend a setup message */
3176 if (xfer->flags_int.control_xfr) {
3177 if (xfer->flags_int.control_hdr) {
3180 temp.func = &dwc_otg_host_setup_tx;
3182 temp.func = &dwc_otg_setup_rx;
3184 temp.len = xfer->frlengths[0];
3185 temp.pc = xfer->frbuffers + 0;
3186 temp.short_pkt = temp.len ? 1 : 0;
3188 /* check for last frame */
3189 if (xfer->nframes == 1) {
3190 /* no STATUS stage yet, SETUP is last */
3191 if (xfer->flags_int.control_act)
3192 temp.setup_alt_next = 0;
3195 dwc_otg_setup_standard_chain_sub(&temp);
3202 if (x != xfer->nframes) {
3203 if (xfer->endpointno & UE_DIR_IN) {
3205 temp.func = &dwc_otg_host_data_rx;
3208 temp.func = &dwc_otg_data_tx;
3213 temp.func = &dwc_otg_host_data_tx;
3216 temp.func = &dwc_otg_data_rx;
3221 /* setup "pc" pointer */
3222 temp.pc = xfer->frbuffers + x;
3226 while (x != xfer->nframes) {
3228 /* DATA0 / DATA1 message */
3230 temp.len = xfer->frlengths[x];
3234 if (x == xfer->nframes) {
3235 if (xfer->flags_int.control_xfr) {
3236 if (xfer->flags_int.control_act) {
3237 temp.setup_alt_next = 0;
3240 temp.setup_alt_next = 0;
3243 if (temp.len == 0) {
3245 /* make sure that we send an USB packet */
3251 /* regular data transfer */
3253 temp.short_pkt = (xfer->flags.force_short_xfer ? 0 : 1);
3256 dwc_otg_setup_standard_chain_sub(&temp);
3258 if (xfer->flags_int.isochronous_xfr) {
3259 temp.offset += temp.len;
3261 /* get next Page Cache pointer */
3262 temp.pc = xfer->frbuffers + x;
3266 if (xfer->flags_int.control_xfr) {
3268 /* always setup a valid "pc" pointer for status and sync */
3269 temp.pc = xfer->frbuffers + 0;
3272 temp.setup_alt_next = 0;
3274 /* check if we need to sync */
3276 /* we need a SYNC point after TX */
3277 temp.func = &dwc_otg_data_tx_sync;
3278 dwc_otg_setup_standard_chain_sub(&temp);
3281 /* check if we should append a status stage */
3282 if (!xfer->flags_int.control_act) {
3285 * Send a DATA1 message and invert the current
3286 * endpoint direction.
3288 if (xfer->endpointno & UE_DIR_IN) {
3290 temp.func = &dwc_otg_host_data_tx;
3293 temp.func = &dwc_otg_data_rx;
3298 temp.func = &dwc_otg_host_data_rx;
3301 temp.func = &dwc_otg_data_tx;
3306 dwc_otg_setup_standard_chain_sub(&temp);
3308 /* data toggle should be DATA1 */
3313 /* we need a SYNC point after TX */
3314 temp.func = &dwc_otg_data_tx_sync;
3315 dwc_otg_setup_standard_chain_sub(&temp);
3319 /* check if we need to sync */
3322 temp.pc = xfer->frbuffers + 0;
3325 temp.setup_alt_next = 0;
3327 /* we need a SYNC point after TX */
3328 temp.func = &dwc_otg_data_tx_sync;
3329 dwc_otg_setup_standard_chain_sub(&temp);
3333 /* must have at least one frame! */
3335 xfer->td_transfer_last = td;
3339 struct dwc_otg_softc *sc;
3343 sc = DWC_OTG_BUS2SC(xfer->xroot->bus);
3345 /* get first again */
3346 td = xfer->td_transfer_first;
3347 td->toggle = (xfer->endpoint->toggle_next ? 1 : 0);
3350 (xfer->address << HCCHAR_DEVADDR_SHIFT) |
3351 ((xfer->endpointno & UE_ADDR) << HCCHAR_EPNUM_SHIFT) |
3352 (xfer->max_packet_size << HCCHAR_MPS_SHIFT) |
3356 * We are not always able to meet the timing
3357 * requirements of the USB interrupt endpoint's
3358 * complete split token, when doing transfers going
3359 * via a transaction translator. Use the CONTROL
3360 * transfer type instead of the INTERRUPT transfer
3361 * type in general, as a means to workaround
3362 * that. This trick should work for both FULL and LOW
3363 * speed USB traffic going through a TT. For non-TT
3364 * traffic it works as well. The reason for using
3365 * CONTROL type instead of BULK is that some TTs might
3366 * reject LOW speed BULK traffic.
3368 if (td->ep_type == UE_INTERRUPT)
3369 hcchar |= (UE_CONTROL << HCCHAR_EPTYPE_SHIFT);
3371 hcchar |= (td->ep_type << HCCHAR_EPTYPE_SHIFT);
3373 if (UE_GET_DIR(xfer->endpointno) == UE_DIR_IN)
3374 hcchar |= HCCHAR_EPDIR_IN;
3376 switch (xfer->xroot->udev->speed) {
3378 hcchar |= HCCHAR_LSPDDEV;
3380 case USB_SPEED_FULL:
3381 /* check if root HUB port is running High Speed */
3382 if (dwc_otg_uses_split(xfer->xroot->udev)) {
3383 hcsplt = HCSPLT_SPLTENA |
3384 (xfer->xroot->udev->hs_port_no <<
3385 HCSPLT_PRTADDR_SHIFT) |
3386 (xfer->xroot->udev->hs_hub_addr <<
3387 HCSPLT_HUBADDR_SHIFT);
3391 if (td->ep_type == UE_INTERRUPT) {
3393 ival = xfer->interval / DWC_OTG_HOST_TIMER_RATE;
3396 else if (ival > 127)
3398 td->tmr_val = sc->sc_tmr_val + ival;
3400 } else if (td->ep_type == UE_ISOCHRONOUS) {
3402 td->tmr_val = sc->sc_last_frame_num;
3403 if (td->hcchar & HCCHAR_EPDIR_IN)
3407 td->tmr_res = (uint8_t)sc->sc_last_frame_num;
3410 case USB_SPEED_HIGH:
3412 if (td->ep_type == UE_INTERRUPT) {
3414 hcchar |= ((xfer->max_packet_count & 3)
3415 << HCCHAR_MC_SHIFT);
3416 ival = xfer->interval / DWC_OTG_HOST_TIMER_RATE;
3419 else if (ival > 127)
3421 td->tmr_val = sc->sc_tmr_val + ival;
3423 } else if (td->ep_type == UE_ISOCHRONOUS) {
3424 hcchar |= ((xfer->max_packet_count & 3)
3425 << HCCHAR_MC_SHIFT);
3426 td->tmr_res = 1 << usbd_xfer_get_fps_shift(xfer);
3427 td->tmr_val = sc->sc_last_frame_num;
3428 if (td->hcchar & HCCHAR_EPDIR_IN)
3429 td->tmr_val += td->tmr_res;
3433 td->tmr_res = (uint8_t)sc->sc_last_frame_num;
3443 /* store configuration in all TD's */
3445 td->hcchar = hcchar;
3446 td->hcsplt = hcsplt;
3448 if (((void *)td) == xfer->td_transfer_last)
3457 dwc_otg_timeout(void *arg)
3459 struct usb_xfer *xfer = arg;
3461 DPRINTF("xfer=%p\n", xfer);
3463 USB_BUS_LOCK_ASSERT(xfer->xroot->bus, MA_OWNED);
3465 /* transfer is transferred */
3466 dwc_otg_device_done(xfer, USB_ERR_TIMEOUT);
3470 dwc_otg_start_standard_chain(struct usb_xfer *xfer)
3472 struct dwc_otg_softc *sc = DWC_OTG_BUS2SC(xfer->xroot->bus);
3477 * Poll one time in device mode, which will turn on the
3478 * endpoint interrupts. Else wait for SOF interrupt in host
3481 USB_BUS_SPIN_LOCK(&sc->sc_bus);
3483 if (sc->sc_flags.status_device_mode != 0) {
3484 dwc_otg_xfer_do_fifo(sc, xfer);
3485 if (dwc_otg_xfer_do_complete_locked(sc, xfer))
3488 struct dwc_otg_td *td = xfer->td_transfer_cache;
3489 if (td->ep_type == UE_ISOCHRONOUS &&
3490 (td->hcchar & HCCHAR_EPDIR_IN) == 0) {
3492 * Need to start ISOCHRONOUS OUT transfer ASAP
3493 * because execution is delayed by one 125us
3496 dwc_otg_xfer_do_fifo(sc, xfer);
3497 if (dwc_otg_xfer_do_complete_locked(sc, xfer))
3502 /* put transfer on interrupt queue */
3503 usbd_transfer_enqueue(&xfer->xroot->bus->intr_q, xfer);
3505 /* start timeout, if any */
3506 if (xfer->timeout != 0) {
3507 usbd_transfer_timeout_ms(xfer,
3508 &dwc_otg_timeout, xfer->timeout);
3511 if (sc->sc_flags.status_device_mode != 0)
3514 /* enable SOF interrupt, if any */
3515 dwc_otg_enable_sof_irq(sc);
3517 USB_BUS_SPIN_UNLOCK(&sc->sc_bus);
3521 dwc_otg_root_intr(struct dwc_otg_softc *sc)
3525 USB_BUS_LOCK_ASSERT(&sc->sc_bus, MA_OWNED);
3528 sc->sc_hub_idata[0] = 0x02; /* we only have one port */
3530 uhub_root_intr(&sc->sc_bus, sc->sc_hub_idata,
3531 sizeof(sc->sc_hub_idata));
3535 dwc_otg_standard_done_sub(struct usb_xfer *xfer)
3537 struct dwc_otg_td *td;
3543 td = xfer->td_transfer_cache;
3546 len = td->remainder;
3548 /* store last data toggle */
3549 xfer->endpoint->toggle_next = td->toggle;
3551 if (xfer->aframes != xfer->nframes) {
3553 * Verify the length and subtract
3554 * the remainder from "frlengths[]":
3556 if (len > xfer->frlengths[xfer->aframes]) {
3559 xfer->frlengths[xfer->aframes] -= len;
3562 /* Check for transfer error */
3563 if (td->error_any) {
3564 /* the transfer is finished */
3565 error = (td->error_stall ?
3566 USB_ERR_STALLED : USB_ERR_IOERROR);
3570 /* Check for short transfer */
3572 if (xfer->flags_int.short_frames_ok ||
3573 xfer->flags_int.isochronous_xfr) {
3574 /* follow alt next */
3581 /* the transfer is finished */
3589 /* this USB frame is complete */
3595 /* update transfer cache */
3597 xfer->td_transfer_cache = td;
3603 dwc_otg_standard_done(struct usb_xfer *xfer)
3605 usb_error_t err = 0;
3607 DPRINTFN(13, "xfer=%p endpoint=%p transfer done\n",
3608 xfer, xfer->endpoint);
3612 xfer->td_transfer_cache = xfer->td_transfer_first;
3614 if (xfer->flags_int.control_xfr) {
3616 if (xfer->flags_int.control_hdr) {
3618 err = dwc_otg_standard_done_sub(xfer);
3622 if (xfer->td_transfer_cache == NULL) {
3626 while (xfer->aframes != xfer->nframes) {
3628 err = dwc_otg_standard_done_sub(xfer);
3631 if (xfer->td_transfer_cache == NULL) {
3636 if (xfer->flags_int.control_xfr &&
3637 !xfer->flags_int.control_act) {
3639 err = dwc_otg_standard_done_sub(xfer);
3642 dwc_otg_device_done(xfer, err);
3645 /*------------------------------------------------------------------------*
3646 * dwc_otg_device_done
3648 * NOTE: this function can be called more than one time on the
3649 * same USB transfer!
3650 *------------------------------------------------------------------------*/
3652 dwc_otg_device_done(struct usb_xfer *xfer, usb_error_t error)
3654 struct dwc_otg_softc *sc = DWC_OTG_BUS2SC(xfer->xroot->bus);
3656 DPRINTFN(9, "xfer=%p, endpoint=%p, error=%d\n",
3657 xfer, xfer->endpoint, error);
3659 USB_BUS_SPIN_LOCK(&sc->sc_bus);
3661 if (xfer->flags_int.usb_mode == USB_MODE_DEVICE) {
3662 /* Interrupts are cleared by the interrupt handler */
3664 struct dwc_otg_td *td;
3666 td = xfer->td_transfer_cache;
3668 dwc_otg_host_channel_free(sc, td);
3670 /* dequeue transfer and start next transfer */
3671 usbd_transfer_done(xfer, error);
3673 USB_BUS_SPIN_UNLOCK(&sc->sc_bus);
3677 dwc_otg_xfer_stall(struct usb_xfer *xfer)
3679 dwc_otg_device_done(xfer, USB_ERR_STALLED);
3683 dwc_otg_set_stall(struct usb_device *udev,
3684 struct usb_endpoint *ep, uint8_t *did_stall)
3686 struct dwc_otg_softc *sc;
3691 USB_BUS_LOCK_ASSERT(udev->bus, MA_OWNED);
3694 if (udev->flags.usb_mode != USB_MODE_DEVICE) {
3699 sc = DWC_OTG_BUS2SC(udev->bus);
3701 USB_BUS_SPIN_LOCK(&sc->sc_bus);
3703 /* get endpoint address */
3704 ep_no = ep->edesc->bEndpointAddress;
3706 DPRINTFN(5, "endpoint=0x%x\n", ep_no);
3708 if (ep_no & UE_DIR_IN) {
3709 reg = DOTG_DIEPCTL(ep_no & UE_ADDR);
3710 temp = sc->sc_in_ctl[ep_no & UE_ADDR];
3712 reg = DOTG_DOEPCTL(ep_no & UE_ADDR);
3713 temp = sc->sc_out_ctl[ep_no & UE_ADDR];
3716 /* disable and stall endpoint */
3717 DWC_OTG_WRITE_4(sc, reg, temp | DOEPCTL_EPDIS);
3718 DWC_OTG_WRITE_4(sc, reg, temp | DOEPCTL_STALL);
3720 /* clear active OUT ep */
3721 if (!(ep_no & UE_DIR_IN)) {
3723 sc->sc_active_rx_ep &= ~(1U << (ep_no & UE_ADDR));
3725 if (sc->sc_last_rx_status != 0 &&
3726 (ep_no & UE_ADDR) == GRXSTSRD_CHNUM_GET(
3727 sc->sc_last_rx_status)) {
3729 dwc_otg_common_rx_ack(sc);
3730 /* poll interrupt */
3731 dwc_otg_interrupt_poll_locked(sc);
3732 dwc_otg_interrupt_complete_locked(sc);
3735 USB_BUS_SPIN_UNLOCK(&sc->sc_bus);
3739 dwc_otg_clear_stall_sub_locked(struct dwc_otg_softc *sc, uint32_t mps,
3740 uint8_t ep_no, uint8_t ep_type, uint8_t ep_dir)
3745 if (ep_type == UE_CONTROL) {
3746 /* clearing stall is not needed */
3751 reg = DOTG_DIEPCTL(ep_no);
3753 reg = DOTG_DOEPCTL(ep_no);
3754 sc->sc_active_rx_ep |= (1U << ep_no);
3757 /* round up and mask away the multiplier count */
3758 mps = (mps + 3) & 0x7FC;
3760 if (ep_type == UE_BULK) {
3761 temp = DIEPCTL_EPTYPE_SET(
3762 DIEPCTL_EPTYPE_BULK) |
3764 } else if (ep_type == UE_INTERRUPT) {
3765 temp = DIEPCTL_EPTYPE_SET(
3766 DIEPCTL_EPTYPE_INTERRUPT) |
3769 temp = DIEPCTL_EPTYPE_SET(
3770 DIEPCTL_EPTYPE_ISOC) |
3774 temp |= DIEPCTL_MPS_SET(mps);
3775 temp |= DIEPCTL_TXFNUM_SET(ep_no);
3778 sc->sc_in_ctl[ep_no] = temp;
3780 sc->sc_out_ctl[ep_no] = temp;
3782 DWC_OTG_WRITE_4(sc, reg, temp | DOEPCTL_EPDIS);
3783 DWC_OTG_WRITE_4(sc, reg, temp | DOEPCTL_SETD0PID);
3784 DWC_OTG_WRITE_4(sc, reg, temp | DIEPCTL_SNAK);
3786 /* we only reset the transmit FIFO */
3788 dwc_otg_tx_fifo_reset(sc,
3789 GRSTCTL_TXFIFO(ep_no) |
3793 DOTG_DIEPTSIZ(ep_no), 0);
3796 /* poll interrupt */
3797 dwc_otg_interrupt_poll_locked(sc);
3798 dwc_otg_interrupt_complete_locked(sc);
3802 dwc_otg_clear_stall(struct usb_device *udev, struct usb_endpoint *ep)
3804 struct dwc_otg_softc *sc;
3805 struct usb_endpoint_descriptor *ed;
3807 DPRINTFN(5, "endpoint=%p\n", ep);
3809 USB_BUS_LOCK_ASSERT(udev->bus, MA_OWNED);
3812 if (udev->flags.usb_mode != USB_MODE_DEVICE) {
3817 sc = DWC_OTG_BUS2SC(udev->bus);
3819 USB_BUS_SPIN_LOCK(&sc->sc_bus);
3821 /* get endpoint descriptor */
3824 /* reset endpoint */
3825 dwc_otg_clear_stall_sub_locked(sc,
3826 UGETW(ed->wMaxPacketSize),
3827 (ed->bEndpointAddress & UE_ADDR),
3828 (ed->bmAttributes & UE_XFERTYPE),
3829 (ed->bEndpointAddress & (UE_DIR_IN | UE_DIR_OUT)));
3831 USB_BUS_SPIN_UNLOCK(&sc->sc_bus);
3835 dwc_otg_device_state_change(struct usb_device *udev)
3837 struct dwc_otg_softc *sc;
3841 if (udev->flags.usb_mode != USB_MODE_DEVICE) {
3847 sc = DWC_OTG_BUS2SC(udev->bus);
3849 /* deactivate all other endpoint but the control endpoint */
3850 if (udev->state == USB_STATE_CONFIGURED ||
3851 udev->state == USB_STATE_ADDRESSED) {
3853 USB_BUS_LOCK(&sc->sc_bus);
3855 for (x = 1; x != sc->sc_dev_ep_max; x++) {
3857 if (x < sc->sc_dev_in_ep_max) {
3858 DWC_OTG_WRITE_4(sc, DOTG_DIEPCTL(x),
3860 DWC_OTG_WRITE_4(sc, DOTG_DIEPCTL(x), 0);
3863 DWC_OTG_WRITE_4(sc, DOTG_DOEPCTL(x),
3865 DWC_OTG_WRITE_4(sc, DOTG_DOEPCTL(x), 0);
3867 USB_BUS_UNLOCK(&sc->sc_bus);
3872 dwc_otg_init(struct dwc_otg_softc *sc)
3878 /* set up the bus structure */
3879 sc->sc_bus.usbrev = USB_REV_2_0;
3880 sc->sc_bus.methods = &dwc_otg_bus_methods;
3882 usb_callout_init_mtx(&sc->sc_timer,
3883 &sc->sc_bus.bus_mtx, 0);
3885 USB_BUS_LOCK(&sc->sc_bus);
3887 /* turn on clocks */
3888 dwc_otg_clocks_on(sc);
3890 temp = DWC_OTG_READ_4(sc, DOTG_GSNPSID);
3891 DPRINTF("Version = 0x%08x\n", temp);
3894 DWC_OTG_WRITE_4(sc, DOTG_DCTL,
3897 /* wait for host to detect disconnect */
3898 usb_pause_mtx(&sc->sc_bus.bus_mtx, hz / 32);
3900 DWC_OTG_WRITE_4(sc, DOTG_GRSTCTL,
3903 /* wait a little bit for block to reset */
3904 usb_pause_mtx(&sc->sc_bus.bus_mtx, hz / 128);
3906 switch (sc->sc_mode) {
3907 case DWC_MODE_DEVICE:
3908 temp = GUSBCFG_FORCEDEVMODE;
3911 temp = GUSBCFG_FORCEHOSTMODE;
3918 if (sc->sc_phy_type == 0)
3919 sc->sc_phy_type = dwc_otg_phy_type + 1;
3920 if (sc->sc_phy_bits == 0)
3921 sc->sc_phy_bits = 16;
3923 /* select HSIC, ULPI, UTMI+ or internal PHY mode */
3924 switch (sc->sc_phy_type) {
3925 case DWC_OTG_PHY_HSIC:
3926 DWC_OTG_WRITE_4(sc, DOTG_GUSBCFG,
3928 GUSBCFG_TRD_TIM_SET(5) | temp);
3929 DWC_OTG_WRITE_4(sc, DOTG_GOTGCTL,
3932 temp = DWC_OTG_READ_4(sc, DOTG_GLPMCFG);
3933 DWC_OTG_WRITE_4(sc, DOTG_GLPMCFG,
3934 temp & ~GLPMCFG_HSIC_CONN);
3935 DWC_OTG_WRITE_4(sc, DOTG_GLPMCFG,
3936 temp | GLPMCFG_HSIC_CONN);
3938 case DWC_OTG_PHY_ULPI:
3939 DWC_OTG_WRITE_4(sc, DOTG_GUSBCFG,
3940 GUSBCFG_ULPI_UTMI_SEL |
3941 GUSBCFG_TRD_TIM_SET(5) | temp);
3942 DWC_OTG_WRITE_4(sc, DOTG_GOTGCTL, 0);
3944 temp = DWC_OTG_READ_4(sc, DOTG_GLPMCFG);
3945 DWC_OTG_WRITE_4(sc, DOTG_GLPMCFG,
3946 temp & ~GLPMCFG_HSIC_CONN);
3948 case DWC_OTG_PHY_UTMI:
3949 DWC_OTG_WRITE_4(sc, DOTG_GUSBCFG,
3950 (sc->sc_phy_bits == 16 ? GUSBCFG_PHYIF : 0) |
3951 GUSBCFG_TRD_TIM_SET(5) | temp);
3952 DWC_OTG_WRITE_4(sc, DOTG_GOTGCTL, 0);
3954 temp = DWC_OTG_READ_4(sc, DOTG_GLPMCFG);
3955 DWC_OTG_WRITE_4(sc, DOTG_GLPMCFG,
3956 temp & ~GLPMCFG_HSIC_CONN);
3958 case DWC_OTG_PHY_INTERNAL:
3959 DWC_OTG_WRITE_4(sc, DOTG_GUSBCFG,
3961 GUSBCFG_TRD_TIM_SET(5) | temp);
3962 DWC_OTG_WRITE_4(sc, DOTG_GOTGCTL, 0);
3964 temp = DWC_OTG_READ_4(sc, DOTG_GLPMCFG);
3965 DWC_OTG_WRITE_4(sc, DOTG_GLPMCFG,
3966 temp & ~GLPMCFG_HSIC_CONN);
3968 temp = DWC_OTG_READ_4(sc, DOTG_GGPIO);
3969 temp &= ~(DOTG_GGPIO_NOVBUSSENS | DOTG_GGPIO_I2CPADEN);
3970 temp |= (DOTG_GGPIO_VBUSASEN | DOTG_GGPIO_VBUSBSEN |
3972 DWC_OTG_WRITE_4(sc, DOTG_GGPIO, temp);
3978 /* clear global nak */
3979 DWC_OTG_WRITE_4(sc, DOTG_DCTL,
3983 /* disable USB port */
3984 DWC_OTG_WRITE_4(sc, DOTG_PCGCCTL, 0xFFFFFFFF);
3987 usb_pause_mtx(&sc->sc_bus.bus_mtx, hz / 100);
3989 /* enable USB port */
3990 DWC_OTG_WRITE_4(sc, DOTG_PCGCCTL, 0);
3993 usb_pause_mtx(&sc->sc_bus.bus_mtx, hz / 100);
3995 temp = DWC_OTG_READ_4(sc, DOTG_GHWCFG3);
3997 sc->sc_fifo_size = 4 * GHWCFG3_DFIFODEPTH_GET(temp);
3999 temp = DWC_OTG_READ_4(sc, DOTG_GHWCFG2);
4001 sc->sc_dev_ep_max = GHWCFG2_NUMDEVEPS_GET(temp);
4003 if (sc->sc_dev_ep_max > DWC_OTG_MAX_ENDPOINTS)
4004 sc->sc_dev_ep_max = DWC_OTG_MAX_ENDPOINTS;
4006 sc->sc_host_ch_max = GHWCFG2_NUMHSTCHNL_GET(temp);
4008 if (sc->sc_host_ch_max > DWC_OTG_MAX_CHANNELS)
4009 sc->sc_host_ch_max = DWC_OTG_MAX_CHANNELS;
4011 temp = DWC_OTG_READ_4(sc, DOTG_GHWCFG4);
4013 sc->sc_dev_in_ep_max = GHWCFG4_NUM_IN_EP_GET(temp);
4015 DPRINTF("Total FIFO size = %d bytes, Device EPs = %d/%d Host CHs = %d\n",
4016 sc->sc_fifo_size, sc->sc_dev_ep_max, sc->sc_dev_in_ep_max,
4017 sc->sc_host_ch_max);
4020 if (dwc_otg_init_fifo(sc, sc->sc_mode)) {
4021 USB_BUS_UNLOCK(&sc->sc_bus);
4025 /* enable interrupts */
4026 sc->sc_irq_mask |= DWC_OTG_MSK_GINT_THREAD_IRQ;
4027 DWC_OTG_WRITE_4(sc, DOTG_GINTMSK, sc->sc_irq_mask);
4029 if (sc->sc_mode == DWC_MODE_OTG || sc->sc_mode == DWC_MODE_DEVICE) {
4031 /* enable all endpoint interrupts */
4032 temp = DWC_OTG_READ_4(sc, DOTG_GHWCFG2);
4033 if (temp & GHWCFG2_MPI) {
4036 DPRINTF("Disable Multi Process Interrupts\n");
4038 for (x = 0; x != sc->sc_dev_in_ep_max; x++) {
4039 DWC_OTG_WRITE_4(sc, DOTG_DIEPEACHINTMSK(x), 0);
4040 DWC_OTG_WRITE_4(sc, DOTG_DOEPEACHINTMSK(x), 0);
4042 DWC_OTG_WRITE_4(sc, DOTG_DEACHINTMSK, 0);
4044 DWC_OTG_WRITE_4(sc, DOTG_DIEPMSK,
4045 DIEPMSK_XFERCOMPLMSK);
4046 DWC_OTG_WRITE_4(sc, DOTG_DOEPMSK, 0);
4047 DWC_OTG_WRITE_4(sc, DOTG_DAINTMSK, 0xFFFF);
4050 if (sc->sc_mode == DWC_MODE_OTG || sc->sc_mode == DWC_MODE_HOST) {
4052 temp = DWC_OTG_READ_4(sc, DOTG_HCFG);
4053 temp &= ~(HCFG_FSLSSUPP | HCFG_FSLSPCLKSEL_MASK);
4054 temp |= (1 << HCFG_FSLSPCLKSEL_SHIFT);
4055 DWC_OTG_WRITE_4(sc, DOTG_HCFG, temp);
4058 /* only enable global IRQ */
4059 DWC_OTG_WRITE_4(sc, DOTG_GAHBCFG,
4060 GAHBCFG_GLBLINTRMSK);
4062 /* turn off clocks */
4063 dwc_otg_clocks_off(sc);
4065 /* read initial VBUS state */
4067 temp = DWC_OTG_READ_4(sc, DOTG_GOTGCTL);
4069 DPRINTFN(5, "GOTCTL=0x%08x\n", temp);
4071 dwc_otg_vbus_interrupt(sc,
4072 (temp & (GOTGCTL_ASESVLD | GOTGCTL_BSESVLD)) ? 1 : 0);
4074 USB_BUS_UNLOCK(&sc->sc_bus);
4076 /* catch any lost interrupts */
4078 dwc_otg_do_poll(&sc->sc_bus);
4080 return (0); /* success */
4084 dwc_otg_uninit(struct dwc_otg_softc *sc)
4086 USB_BUS_LOCK(&sc->sc_bus);
4088 /* stop host timer */
4089 dwc_otg_timer_stop(sc);
4091 /* set disconnect */
4092 DWC_OTG_WRITE_4(sc, DOTG_DCTL,
4095 /* turn off global IRQ */
4096 DWC_OTG_WRITE_4(sc, DOTG_GAHBCFG, 0);
4098 sc->sc_flags.port_enabled = 0;
4099 sc->sc_flags.port_powered = 0;
4100 sc->sc_flags.status_vbus = 0;
4101 sc->sc_flags.status_bus_reset = 0;
4102 sc->sc_flags.status_suspend = 0;
4103 sc->sc_flags.change_suspend = 0;
4104 sc->sc_flags.change_connect = 1;
4106 dwc_otg_pull_down(sc);
4107 dwc_otg_clocks_off(sc);
4109 USB_BUS_UNLOCK(&sc->sc_bus);
4111 usb_callout_drain(&sc->sc_timer);
4115 dwc_otg_suspend(struct dwc_otg_softc *sc)
4121 dwc_otg_resume(struct dwc_otg_softc *sc)
4127 dwc_otg_do_poll(struct usb_bus *bus)
4129 struct dwc_otg_softc *sc = DWC_OTG_BUS2SC(bus);
4131 USB_BUS_LOCK(&sc->sc_bus);
4132 USB_BUS_SPIN_LOCK(&sc->sc_bus);
4133 dwc_otg_interrupt_poll_locked(sc);
4134 dwc_otg_interrupt_complete_locked(sc);
4135 USB_BUS_SPIN_UNLOCK(&sc->sc_bus);
4136 USB_BUS_UNLOCK(&sc->sc_bus);
4139 /*------------------------------------------------------------------------*
4140 * DWC OTG bulk support
4141 * DWC OTG control support
4142 * DWC OTG interrupt support
4143 *------------------------------------------------------------------------*/
4145 dwc_otg_device_non_isoc_open(struct usb_xfer *xfer)
4150 dwc_otg_device_non_isoc_close(struct usb_xfer *xfer)
4152 dwc_otg_device_done(xfer, USB_ERR_CANCELLED);
4156 dwc_otg_device_non_isoc_enter(struct usb_xfer *xfer)
4161 dwc_otg_device_non_isoc_start(struct usb_xfer *xfer)
4164 dwc_otg_setup_standard_chain(xfer);
4165 dwc_otg_start_standard_chain(xfer);
4168 static const struct usb_pipe_methods dwc_otg_device_non_isoc_methods =
4170 .open = dwc_otg_device_non_isoc_open,
4171 .close = dwc_otg_device_non_isoc_close,
4172 .enter = dwc_otg_device_non_isoc_enter,
4173 .start = dwc_otg_device_non_isoc_start,
4176 /*------------------------------------------------------------------------*
4177 * DWC OTG full speed isochronous support
4178 *------------------------------------------------------------------------*/
4180 dwc_otg_device_isoc_open(struct usb_xfer *xfer)
4185 dwc_otg_device_isoc_close(struct usb_xfer *xfer)
4187 dwc_otg_device_done(xfer, USB_ERR_CANCELLED);
4191 dwc_otg_device_isoc_enter(struct usb_xfer *xfer)
4196 dwc_otg_device_isoc_start(struct usb_xfer *xfer)
4198 struct dwc_otg_softc *sc = DWC_OTG_BUS2SC(xfer->xroot->bus);
4202 uint8_t shift = usbd_xfer_get_fps_shift(xfer);
4204 DPRINTFN(6, "xfer=%p next=%d nframes=%d\n",
4205 xfer, xfer->endpoint->isoc_next, xfer->nframes);
4207 if (xfer->xroot->udev->flags.usb_mode == USB_MODE_HOST) {
4208 temp = DWC_OTG_READ_4(sc, DOTG_HFNUM);
4210 /* get the current frame index */
4211 framenum = (temp & HFNUM_FRNUM_MASK);
4213 temp = DWC_OTG_READ_4(sc, DOTG_DSTS);
4215 /* get the current frame index */
4216 framenum = DSTS_SOFFN_GET(temp);
4220 * Check if port is doing 8000 or 1000 frames per second:
4222 if (sc->sc_flags.status_high_speed)
4225 framenum &= DWC_OTG_FRAME_MASK;
4228 * Compute number of milliseconds worth of data traffic for
4229 * this USB transfer:
4231 if (xfer->xroot->udev->speed == USB_SPEED_HIGH)
4232 msframes = ((xfer->nframes << shift) + 7) / 8;
4234 msframes = xfer->nframes;
4237 * check if the frame index is within the window where the frames
4240 temp = (framenum - xfer->endpoint->isoc_next) & DWC_OTG_FRAME_MASK;
4242 if ((xfer->endpoint->is_synced == 0) || (temp < msframes)) {
4244 * If there is data underflow or the pipe queue is
4245 * empty we schedule the transfer a few frames ahead
4246 * of the current frame position. Else two isochronous
4247 * transfers might overlap.
4249 xfer->endpoint->isoc_next = (framenum + 3) & DWC_OTG_FRAME_MASK;
4250 xfer->endpoint->is_synced = 1;
4251 DPRINTFN(3, "start next=%d\n", xfer->endpoint->isoc_next);
4254 * compute how many milliseconds the insertion is ahead of the
4255 * current frame position:
4257 temp = (xfer->endpoint->isoc_next - framenum) & DWC_OTG_FRAME_MASK;
4260 * pre-compute when the isochronous transfer will be finished:
4262 xfer->isoc_time_complete =
4263 usb_isoc_time_expand(&sc->sc_bus, framenum) + temp + msframes;
4266 dwc_otg_setup_standard_chain(xfer);
4268 /* compute frame number for next insertion */
4269 xfer->endpoint->isoc_next += msframes;
4271 /* start TD chain */
4272 dwc_otg_start_standard_chain(xfer);
4275 static const struct usb_pipe_methods dwc_otg_device_isoc_methods =
4277 .open = dwc_otg_device_isoc_open,
4278 .close = dwc_otg_device_isoc_close,
4279 .enter = dwc_otg_device_isoc_enter,
4280 .start = dwc_otg_device_isoc_start,
4283 /*------------------------------------------------------------------------*
4284 * DWC OTG root control support
4285 *------------------------------------------------------------------------*
4286 * Simulate a hardware HUB by handling all the necessary requests.
4287 *------------------------------------------------------------------------*/
4289 static const struct usb_device_descriptor dwc_otg_devd = {
4290 .bLength = sizeof(struct usb_device_descriptor),
4291 .bDescriptorType = UDESC_DEVICE,
4292 .bcdUSB = {0x00, 0x02},
4293 .bDeviceClass = UDCLASS_HUB,
4294 .bDeviceSubClass = UDSUBCLASS_HUB,
4295 .bDeviceProtocol = UDPROTO_HSHUBSTT,
4296 .bMaxPacketSize = 64,
4297 .bcdDevice = {0x00, 0x01},
4300 .bNumConfigurations = 1,
4303 static const struct dwc_otg_config_desc dwc_otg_confd = {
4305 .bLength = sizeof(struct usb_config_descriptor),
4306 .bDescriptorType = UDESC_CONFIG,
4307 .wTotalLength[0] = sizeof(dwc_otg_confd),
4309 .bConfigurationValue = 1,
4310 .iConfiguration = 0,
4311 .bmAttributes = UC_SELF_POWERED,
4315 .bLength = sizeof(struct usb_interface_descriptor),
4316 .bDescriptorType = UDESC_INTERFACE,
4318 .bInterfaceClass = UICLASS_HUB,
4319 .bInterfaceSubClass = UISUBCLASS_HUB,
4320 .bInterfaceProtocol = 0,
4323 .bLength = sizeof(struct usb_endpoint_descriptor),
4324 .bDescriptorType = UDESC_ENDPOINT,
4325 .bEndpointAddress = (UE_DIR_IN | DWC_OTG_INTR_ENDPT),
4326 .bmAttributes = UE_INTERRUPT,
4327 .wMaxPacketSize[0] = 8,
4332 #define HSETW(ptr, val) ptr = { (uint8_t)(val), (uint8_t)((val) >> 8) }
4334 static const struct usb_hub_descriptor_min dwc_otg_hubd = {
4335 .bDescLength = sizeof(dwc_otg_hubd),
4336 .bDescriptorType = UDESC_HUB,
4338 HSETW(.wHubCharacteristics, (UHD_PWR_NO_SWITCH | UHD_OC_INDIVIDUAL)),
4339 .bPwrOn2PwrGood = 50,
4340 .bHubContrCurrent = 0,
4341 .DeviceRemovable = {0}, /* port is removable */
4344 #define STRING_VENDOR \
4347 #define STRING_PRODUCT \
4348 "O\0T\0G\0 \0R\0o\0o\0t\0 \0H\0U\0B"
4350 USB_MAKE_STRING_DESC(STRING_VENDOR, dwc_otg_vendor);
4351 USB_MAKE_STRING_DESC(STRING_PRODUCT, dwc_otg_product);
4354 dwc_otg_roothub_exec(struct usb_device *udev,
4355 struct usb_device_request *req, const void **pptr, uint16_t *plength)
4357 struct dwc_otg_softc *sc = DWC_OTG_BUS2SC(udev->bus);
4364 USB_BUS_LOCK_ASSERT(&sc->sc_bus, MA_OWNED);
4367 ptr = (const void *)&sc->sc_hub_temp;
4371 value = UGETW(req->wValue);
4372 index = UGETW(req->wIndex);
4374 /* demultiplex the control request */
4376 switch (req->bmRequestType) {
4377 case UT_READ_DEVICE:
4378 switch (req->bRequest) {
4379 case UR_GET_DESCRIPTOR:
4380 goto tr_handle_get_descriptor;
4382 goto tr_handle_get_config;
4384 goto tr_handle_get_status;
4390 case UT_WRITE_DEVICE:
4391 switch (req->bRequest) {
4392 case UR_SET_ADDRESS:
4393 goto tr_handle_set_address;
4395 goto tr_handle_set_config;
4396 case UR_CLEAR_FEATURE:
4397 goto tr_valid; /* nop */
4398 case UR_SET_DESCRIPTOR:
4399 goto tr_valid; /* nop */
4400 case UR_SET_FEATURE:
4406 case UT_WRITE_ENDPOINT:
4407 switch (req->bRequest) {
4408 case UR_CLEAR_FEATURE:
4409 switch (UGETW(req->wValue)) {
4410 case UF_ENDPOINT_HALT:
4411 goto tr_handle_clear_halt;
4412 case UF_DEVICE_REMOTE_WAKEUP:
4413 goto tr_handle_clear_wakeup;
4418 case UR_SET_FEATURE:
4419 switch (UGETW(req->wValue)) {
4420 case UF_ENDPOINT_HALT:
4421 goto tr_handle_set_halt;
4422 case UF_DEVICE_REMOTE_WAKEUP:
4423 goto tr_handle_set_wakeup;
4428 case UR_SYNCH_FRAME:
4429 goto tr_valid; /* nop */
4435 case UT_READ_ENDPOINT:
4436 switch (req->bRequest) {
4438 goto tr_handle_get_ep_status;
4444 case UT_WRITE_INTERFACE:
4445 switch (req->bRequest) {
4446 case UR_SET_INTERFACE:
4447 goto tr_handle_set_interface;
4448 case UR_CLEAR_FEATURE:
4449 goto tr_valid; /* nop */
4450 case UR_SET_FEATURE:
4456 case UT_READ_INTERFACE:
4457 switch (req->bRequest) {
4458 case UR_GET_INTERFACE:
4459 goto tr_handle_get_interface;
4461 goto tr_handle_get_iface_status;
4467 case UT_WRITE_CLASS_INTERFACE:
4468 case UT_WRITE_VENDOR_INTERFACE:
4472 case UT_READ_CLASS_INTERFACE:
4473 case UT_READ_VENDOR_INTERFACE:
4477 case UT_WRITE_CLASS_DEVICE:
4478 switch (req->bRequest) {
4479 case UR_CLEAR_FEATURE:
4481 case UR_SET_DESCRIPTOR:
4482 case UR_SET_FEATURE:
4489 case UT_WRITE_CLASS_OTHER:
4490 switch (req->bRequest) {
4491 case UR_CLEAR_FEATURE:
4492 goto tr_handle_clear_port_feature;
4493 case UR_SET_FEATURE:
4494 goto tr_handle_set_port_feature;
4495 case UR_CLEAR_TT_BUFFER:
4505 case UT_READ_CLASS_OTHER:
4506 switch (req->bRequest) {
4507 case UR_GET_TT_STATE:
4508 goto tr_handle_get_tt_state;
4510 goto tr_handle_get_port_status;
4516 case UT_READ_CLASS_DEVICE:
4517 switch (req->bRequest) {
4518 case UR_GET_DESCRIPTOR:
4519 goto tr_handle_get_class_descriptor;
4521 goto tr_handle_get_class_status;
4532 tr_handle_get_descriptor:
4533 switch (value >> 8) {
4538 len = sizeof(dwc_otg_devd);
4539 ptr = (const void *)&dwc_otg_devd;
4545 len = sizeof(dwc_otg_confd);
4546 ptr = (const void *)&dwc_otg_confd;
4549 switch (value & 0xff) {
4550 case 0: /* Language table */
4551 len = sizeof(usb_string_lang_en);
4552 ptr = (const void *)&usb_string_lang_en;
4555 case 1: /* Vendor */
4556 len = sizeof(dwc_otg_vendor);
4557 ptr = (const void *)&dwc_otg_vendor;
4560 case 2: /* Product */
4561 len = sizeof(dwc_otg_product);
4562 ptr = (const void *)&dwc_otg_product;
4573 tr_handle_get_config:
4575 sc->sc_hub_temp.wValue[0] = sc->sc_conf;
4578 tr_handle_get_status:
4580 USETW(sc->sc_hub_temp.wValue, UDS_SELF_POWERED);
4583 tr_handle_set_address:
4584 if (value & 0xFF00) {
4587 sc->sc_rt_addr = value;
4590 tr_handle_set_config:
4594 sc->sc_conf = value;
4597 tr_handle_get_interface:
4599 sc->sc_hub_temp.wValue[0] = 0;
4602 tr_handle_get_tt_state:
4603 tr_handle_get_class_status:
4604 tr_handle_get_iface_status:
4605 tr_handle_get_ep_status:
4607 USETW(sc->sc_hub_temp.wValue, 0);
4611 tr_handle_set_interface:
4612 tr_handle_set_wakeup:
4613 tr_handle_clear_wakeup:
4614 tr_handle_clear_halt:
4617 tr_handle_clear_port_feature:
4621 DPRINTFN(9, "UR_CLEAR_PORT_FEATURE on port %d\n", index);
4624 case UHF_PORT_SUSPEND:
4625 dwc_otg_wakeup_peer(sc);
4628 case UHF_PORT_ENABLE:
4629 if (sc->sc_flags.status_device_mode == 0) {
4630 DWC_OTG_WRITE_4(sc, DOTG_HPRT,
4631 sc->sc_hprt_val | HPRT_PRTENA);
4633 sc->sc_flags.port_enabled = 0;
4636 case UHF_C_PORT_RESET:
4637 sc->sc_flags.change_reset = 0;
4640 case UHF_C_PORT_ENABLE:
4641 sc->sc_flags.change_enabled = 0;
4644 case UHF_C_PORT_OVER_CURRENT:
4645 sc->sc_flags.change_over_current = 0;
4649 case UHF_PORT_INDICATOR:
4653 case UHF_PORT_POWER:
4654 sc->sc_flags.port_powered = 0;
4655 if (sc->sc_mode == DWC_MODE_HOST || sc->sc_mode == DWC_MODE_OTG) {
4656 sc->sc_hprt_val = 0;
4657 DWC_OTG_WRITE_4(sc, DOTG_HPRT, HPRT_PRTENA);
4659 dwc_otg_pull_down(sc);
4660 dwc_otg_clocks_off(sc);
4663 case UHF_C_PORT_CONNECTION:
4664 /* clear connect change flag */
4665 sc->sc_flags.change_connect = 0;
4668 case UHF_C_PORT_SUSPEND:
4669 sc->sc_flags.change_suspend = 0;
4673 err = USB_ERR_IOERROR;
4678 tr_handle_set_port_feature:
4682 DPRINTFN(9, "UR_SET_PORT_FEATURE\n");
4685 case UHF_PORT_ENABLE:
4688 case UHF_PORT_SUSPEND:
4689 if (sc->sc_flags.status_device_mode == 0) {
4690 /* set suspend BIT */
4691 sc->sc_hprt_val |= HPRT_PRTSUSP;
4692 DWC_OTG_WRITE_4(sc, DOTG_HPRT, sc->sc_hprt_val);
4694 /* generate HUB suspend event */
4695 dwc_otg_suspend_irq(sc);
4699 case UHF_PORT_RESET:
4700 if (sc->sc_flags.status_device_mode == 0) {
4702 DPRINTF("PORT RESET\n");
4704 /* enable PORT reset */
4705 DWC_OTG_WRITE_4(sc, DOTG_HPRT, sc->sc_hprt_val | HPRT_PRTRST);
4707 /* Wait 62.5ms for reset to complete */
4708 usb_pause_mtx(&sc->sc_bus.bus_mtx, hz / 16);
4710 DWC_OTG_WRITE_4(sc, DOTG_HPRT, sc->sc_hprt_val);
4712 /* Wait 62.5ms for reset to complete */
4713 usb_pause_mtx(&sc->sc_bus.bus_mtx, hz / 16);
4716 (void) dwc_otg_init_fifo(sc, DWC_MODE_HOST);
4718 sc->sc_flags.change_reset = 1;
4720 err = USB_ERR_IOERROR;
4725 case UHF_PORT_INDICATOR:
4728 case UHF_PORT_POWER:
4729 sc->sc_flags.port_powered = 1;
4730 if (sc->sc_mode == DWC_MODE_HOST || sc->sc_mode == DWC_MODE_OTG) {
4731 sc->sc_hprt_val |= HPRT_PRTPWR;
4732 DWC_OTG_WRITE_4(sc, DOTG_HPRT, sc->sc_hprt_val);
4734 if (sc->sc_mode == DWC_MODE_DEVICE || sc->sc_mode == DWC_MODE_OTG) {
4735 /* pull up D+, if any */
4736 dwc_otg_pull_up(sc);
4740 err = USB_ERR_IOERROR;
4745 tr_handle_get_port_status:
4747 DPRINTFN(9, "UR_GET_PORT_STATUS\n");
4752 if (sc->sc_flags.status_vbus)
4753 dwc_otg_clocks_on(sc);
4755 dwc_otg_clocks_off(sc);
4757 /* Select Device Side Mode */
4759 if (sc->sc_flags.status_device_mode) {
4760 value = UPS_PORT_MODE_DEVICE;
4761 dwc_otg_timer_stop(sc);
4764 dwc_otg_timer_start(sc);
4767 if (sc->sc_flags.status_high_speed)
4768 value |= UPS_HIGH_SPEED;
4769 else if (sc->sc_flags.status_low_speed)
4770 value |= UPS_LOW_SPEED;
4772 if (sc->sc_flags.port_powered)
4773 value |= UPS_PORT_POWER;
4775 if (sc->sc_flags.port_enabled)
4776 value |= UPS_PORT_ENABLED;
4778 if (sc->sc_flags.port_over_current)
4779 value |= UPS_OVERCURRENT_INDICATOR;
4781 if (sc->sc_flags.status_vbus &&
4782 sc->sc_flags.status_bus_reset)
4783 value |= UPS_CURRENT_CONNECT_STATUS;
4785 if (sc->sc_flags.status_suspend)
4786 value |= UPS_SUSPEND;
4788 USETW(sc->sc_hub_temp.ps.wPortStatus, value);
4792 if (sc->sc_flags.change_enabled)
4793 value |= UPS_C_PORT_ENABLED;
4794 if (sc->sc_flags.change_connect)
4795 value |= UPS_C_CONNECT_STATUS;
4796 if (sc->sc_flags.change_suspend)
4797 value |= UPS_C_SUSPEND;
4798 if (sc->sc_flags.change_reset)
4799 value |= UPS_C_PORT_RESET;
4800 if (sc->sc_flags.change_over_current)
4801 value |= UPS_C_OVERCURRENT_INDICATOR;
4803 USETW(sc->sc_hub_temp.ps.wPortChange, value);
4804 len = sizeof(sc->sc_hub_temp.ps);
4807 tr_handle_get_class_descriptor:
4811 ptr = (const void *)&dwc_otg_hubd;
4812 len = sizeof(dwc_otg_hubd);
4816 err = USB_ERR_STALLED;
4825 dwc_otg_xfer_setup(struct usb_setup_params *parm)
4827 struct usb_xfer *xfer;
4834 xfer = parm->curr_xfer;
4837 * NOTE: This driver does not use any of the parameters that
4838 * are computed from the following values. Just set some
4839 * reasonable dummies:
4841 parm->hc_max_packet_size = 0x500;
4842 parm->hc_max_packet_count = 3;
4843 parm->hc_max_frame_size = 3 * 0x500;
4845 usbd_transfer_setup_sub(parm);
4848 * compute maximum number of TDs
4850 ep_type = (xfer->endpoint->edesc->bmAttributes & UE_XFERTYPE);
4852 if (ep_type == UE_CONTROL) {
4854 ntd = xfer->nframes + 1 /* STATUS */ + 1 /* SYNC 1 */
4855 + 1 /* SYNC 2 */ + 1 /* SYNC 3 */;
4858 ntd = xfer->nframes + 1 /* SYNC */ ;
4862 * check if "usbd_transfer_setup_sub" set an error
4868 * allocate transfer descriptors
4872 ep_no = xfer->endpointno & UE_ADDR;
4875 * Check for a valid endpoint profile in USB device mode:
4877 if (xfer->flags_int.usb_mode == USB_MODE_DEVICE) {
4878 const struct usb_hw_ep_profile *pf;
4880 dwc_otg_get_hw_ep_profile(parm->udev, &pf, ep_no);
4883 /* should not happen */
4884 parm->err = USB_ERR_INVAL;
4890 parm->size[0] += ((-parm->size[0]) & (USB_HOST_ALIGN - 1));
4892 for (n = 0; n != ntd; n++) {
4894 struct dwc_otg_td *td;
4898 td = USB_ADD_BYTES(parm->buf, parm->size[0]);
4900 /* compute shared bandwidth resource index for TT */
4901 if (dwc_otg_uses_split(parm->udev)) {
4902 if (parm->udev->parent_hs_hub->ddesc.bDeviceProtocol == UDPROTO_HSHUBMTT)
4903 td->tt_index = parm->udev->device_index;
4905 td->tt_index = parm->udev->parent_hs_hub->device_index;
4907 td->tt_index = parm->udev->device_index;
4911 td->max_packet_size = xfer->max_packet_size;
4912 td->max_packet_count = xfer->max_packet_count;
4914 if (td->max_packet_count == 0 || td->max_packet_count > 3)
4915 td->max_packet_count = 1;
4917 td->ep_type = ep_type;
4918 td->obj_next = last_obj;
4922 parm->size[0] += sizeof(*td);
4925 xfer->td_start[0] = last_obj;
4929 dwc_otg_xfer_unsetup(struct usb_xfer *xfer)
4935 dwc_otg_ep_init(struct usb_device *udev, struct usb_endpoint_descriptor *edesc,
4936 struct usb_endpoint *ep)
4938 struct dwc_otg_softc *sc = DWC_OTG_BUS2SC(udev->bus);
4940 DPRINTFN(2, "endpoint=%p, addr=%d, endpt=%d, mode=%d (%d,%d)\n",
4942 edesc->bEndpointAddress, udev->flags.usb_mode,
4943 sc->sc_rt_addr, udev->device_index);
4945 if (udev->device_index != sc->sc_rt_addr) {
4947 if (udev->flags.usb_mode == USB_MODE_DEVICE) {
4948 if (udev->speed != USB_SPEED_FULL &&
4949 udev->speed != USB_SPEED_HIGH) {
4954 if (udev->speed == USB_SPEED_HIGH &&
4955 (edesc->wMaxPacketSize[1] & 0x18) != 0 &&
4956 (edesc->bmAttributes & UE_XFERTYPE) != UE_ISOCHRONOUS) {
4958 DPRINTFN(-1, "Non-isochronous high bandwidth "
4959 "endpoint not supported\n");
4963 if ((edesc->bmAttributes & UE_XFERTYPE) == UE_ISOCHRONOUS)
4964 ep->methods = &dwc_otg_device_isoc_methods;
4966 ep->methods = &dwc_otg_device_non_isoc_methods;
4971 dwc_otg_set_hw_power_sleep(struct usb_bus *bus, uint32_t state)
4973 struct dwc_otg_softc *sc = DWC_OTG_BUS2SC(bus);
4976 case USB_HW_POWER_SUSPEND:
4977 dwc_otg_suspend(sc);
4979 case USB_HW_POWER_SHUTDOWN:
4982 case USB_HW_POWER_RESUME:
4991 dwc_otg_get_dma_delay(struct usb_device *udev, uint32_t *pus)
4993 /* DMA delay - wait until any use of memory is finished */
4994 *pus = (2125); /* microseconds */
4998 dwc_otg_device_resume(struct usb_device *udev)
5002 /* poll all transfers again to restart resumed ones */
5003 dwc_otg_do_poll(udev->bus);
5007 dwc_otg_device_suspend(struct usb_device *udev)
5012 static const struct usb_bus_methods dwc_otg_bus_methods =
5014 .endpoint_init = &dwc_otg_ep_init,
5015 .xfer_setup = &dwc_otg_xfer_setup,
5016 .xfer_unsetup = &dwc_otg_xfer_unsetup,
5017 .get_hw_ep_profile = &dwc_otg_get_hw_ep_profile,
5018 .xfer_stall = &dwc_otg_xfer_stall,
5019 .set_stall = &dwc_otg_set_stall,
5020 .clear_stall = &dwc_otg_clear_stall,
5021 .roothub_exec = &dwc_otg_roothub_exec,
5022 .xfer_poll = &dwc_otg_do_poll,
5023 .device_state_change = &dwc_otg_device_state_change,
5024 .set_hw_power_sleep = &dwc_otg_set_hw_power_sleep,
5025 .get_dma_delay = &dwc_otg_get_dma_delay,
5026 .device_resume = &dwc_otg_device_resume,
5027 .device_suspend = &dwc_otg_device_suspend,