3 * Copyright (c) 2012 Hans Petter Selasky. All rights reserved.
4 * Copyright (c) 2010-2011 Aleksandr Rybalko. All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * This file contains the driver for the DesignWare series USB 2.0 OTG
34 * LIMITATION: Drivers must be bound to all OUT endpoints in the
35 * active configuration for this driver to work properly. Blocking any
36 * OUT endpoint will block all OUT endpoints including the control
37 * endpoint. Usually this is not a problem.
41 * NOTE: Writing to non-existing registers appears to cause an
45 #ifdef USB_GLOBAL_INCLUDE_FILE
46 #include USB_GLOBAL_INCLUDE_FILE
48 #include <sys/stdint.h>
49 #include <sys/stddef.h>
50 #include <sys/param.h>
51 #include <sys/queue.h>
52 #include <sys/types.h>
53 #include <sys/systm.h>
54 #include <sys/kernel.h>
56 #include <sys/module.h>
58 #include <sys/mutex.h>
59 #include <sys/condvar.h>
60 #include <sys/sysctl.h>
62 #include <sys/unistd.h>
63 #include <sys/callout.h>
64 #include <sys/malloc.h>
67 #include <dev/usb/usb.h>
68 #include <dev/usb/usbdi.h>
70 #define USB_DEBUG_VAR dwc_otg_debug
72 #include <dev/usb/usb_core.h>
73 #include <dev/usb/usb_debug.h>
74 #include <dev/usb/usb_busdma.h>
75 #include <dev/usb/usb_process.h>
76 #include <dev/usb/usb_transfer.h>
77 #include <dev/usb/usb_device.h>
78 #include <dev/usb/usb_hub.h>
79 #include <dev/usb/usb_util.h>
81 #include <dev/usb/usb_controller.h>
82 #include <dev/usb/usb_bus.h>
83 #endif /* USB_GLOBAL_INCLUDE_FILE */
85 #include <dev/usb/controller/dwc_otg.h>
86 #include <dev/usb/controller/dwc_otgreg.h>
88 #define DWC_OTG_BUS2SC(bus) \
89 ((struct dwc_otg_softc *)(((uint8_t *)(bus)) - \
90 ((uint8_t *)&(((struct dwc_otg_softc *)0)->sc_bus))))
92 #define DWC_OTG_PC2SC(pc) \
93 DWC_OTG_BUS2SC(USB_DMATAG_TO_XROOT((pc)->tag_parent)->bus)
95 #define DWC_OTG_MSK_GINT_ENABLED \
101 GINTSTS_SESSREQINT | \
102 GINTMSK_OTGINTMSK | \
103 GINTMSK_HCHINTMSK | \
106 static int dwc_otg_use_hsic;
108 static SYSCTL_NODE(_hw_usb, OID_AUTO, dwc_otg, CTLFLAG_RW, 0, "USB DWC OTG");
110 SYSCTL_INT(_hw_usb_dwc_otg, OID_AUTO, use_hsic, CTLFLAG_RD | CTLFLAG_TUN,
111 &dwc_otg_use_hsic, 0, "DWC OTG uses HSIC interface");
112 TUNABLE_INT("hw.usb.dwc_otg.use_hsic", &dwc_otg_use_hsic);
115 static int dwc_otg_debug;
117 SYSCTL_INT(_hw_usb_dwc_otg, OID_AUTO, debug, CTLFLAG_RW,
118 &dwc_otg_debug, 0, "DWC OTG debug level");
121 #define DWC_OTG_INTR_ENDPT 1
125 struct usb_bus_methods dwc_otg_bus_methods;
126 struct usb_pipe_methods dwc_otg_device_non_isoc_methods;
127 struct usb_pipe_methods dwc_otg_device_isoc_methods;
129 static dwc_otg_cmd_t dwc_otg_setup_rx;
130 static dwc_otg_cmd_t dwc_otg_data_rx;
131 static dwc_otg_cmd_t dwc_otg_data_tx;
132 static dwc_otg_cmd_t dwc_otg_data_tx_sync;
134 static dwc_otg_cmd_t dwc_otg_host_setup_tx;
135 static dwc_otg_cmd_t dwc_otg_host_data_tx;
136 static dwc_otg_cmd_t dwc_otg_host_data_rx;
138 static void dwc_otg_device_done(struct usb_xfer *, usb_error_t);
139 static void dwc_otg_do_poll(struct usb_bus *);
140 static void dwc_otg_standard_done(struct usb_xfer *);
141 static void dwc_otg_root_intr(struct dwc_otg_softc *sc);
142 static void dwc_otg_interrupt_poll(struct dwc_otg_softc *sc);
145 * Here is a configuration that the chip supports.
147 static const struct usb_hw_ep_profile dwc_otg_ep_profile[1] = {
150 .max_in_frame_size = 64,/* fixed */
151 .max_out_frame_size = 64, /* fixed */
153 .support_control = 1,
158 dwc_otg_get_hw_ep_profile(struct usb_device *udev,
159 const struct usb_hw_ep_profile **ppf, uint8_t ep_addr)
161 struct dwc_otg_softc *sc;
163 sc = DWC_OTG_BUS2SC(udev->bus);
165 if (ep_addr < sc->sc_dev_ep_max)
166 *ppf = &sc->sc_hw_ep_profile[ep_addr].usb;
172 dwc_otg_init_fifo(struct dwc_otg_softc *sc, uint8_t mode)
174 struct dwc_otg_profile *pf;
180 fifo_size = sc->sc_fifo_size;
182 fifo_regs = 4 * (sc->sc_dev_ep_max + sc->sc_dev_in_ep_max);
184 if (fifo_size >= fifo_regs)
185 fifo_size -= fifo_regs;
189 /* split equally for IN and OUT */
192 DWC_OTG_WRITE_4(sc, DOTG_GRXFSIZ, fifo_size / 4);
194 /* align to 4-bytes */
197 tx_start = fifo_size;
199 if (fifo_size < 0x40) {
200 DPRINTFN(-1, "Not enough data space for EP0 FIFO.\n");
201 USB_BUS_UNLOCK(&sc->sc_bus);
205 if (mode == DWC_MODE_HOST) {
207 /* reset active endpoints */
208 sc->sc_active_rx_ep = 0;
212 DWC_OTG_WRITE_4(sc, DOTG_GNPTXFSIZ,
213 ((fifo_size / 4) << 16) |
216 tx_start += fifo_size;
218 DWC_OTG_WRITE_4(sc, DOTG_HPTXFSIZ,
219 ((fifo_size / 4) << 16) |
222 for (x = 0; x != sc->sc_host_ch_max; x++) {
223 /* enable interrupts */
224 DWC_OTG_WRITE_4(sc, DOTG_HCINTMSK(x),
225 HCINT_STALL | HCINT_BBLERR |
227 HCINT_NAK | HCINT_ACK | HCINT_NYET |
228 HCINT_CHHLTD | HCINT_FRMOVRUN |
232 /* enable host channel interrupts */
233 DWC_OTG_WRITE_4(sc, DOTG_HAINTMSK,
234 (1U << sc->sc_host_ch_max) - 1U);
237 if (mode == DWC_MODE_DEVICE) {
239 DWC_OTG_WRITE_4(sc, DOTG_GNPTXFSIZ,
240 (0x10 << 16) | (tx_start / 4));
244 /* setup control endpoint profile */
245 sc->sc_hw_ep_profile[0].usb = dwc_otg_ep_profile[0];
247 /* reset active endpoints */
248 sc->sc_active_rx_ep = 1;
250 for (x = 1; x != sc->sc_dev_ep_max; x++) {
252 pf = sc->sc_hw_ep_profile + x;
254 pf->usb.max_out_frame_size = 1024 * 3;
255 pf->usb.is_simplex = 0; /* assume duplex */
256 pf->usb.support_bulk = 1;
257 pf->usb.support_interrupt = 1;
258 pf->usb.support_isochronous = 1;
259 pf->usb.support_out = 1;
261 if (x < sc->sc_dev_in_ep_max) {
264 limit = (x == 1) ? DWC_OTG_MAX_TXN :
265 (DWC_OTG_MAX_TXN / 2);
267 if (fifo_size >= limit) {
268 DWC_OTG_WRITE_4(sc, DOTG_DIEPTXF(x),
269 ((limit / 4) << 16) |
273 pf->usb.max_in_frame_size = 0x200;
274 pf->usb.support_in = 1;
275 pf->max_buffer = limit;
277 } else if (fifo_size >= 0x80) {
278 DWC_OTG_WRITE_4(sc, DOTG_DIEPTXF(x),
279 ((0x80 / 4) << 16) | (tx_start / 4));
282 pf->usb.max_in_frame_size = 0x40;
283 pf->usb.support_in = 1;
286 pf->usb.is_simplex = 1;
287 DWC_OTG_WRITE_4(sc, DOTG_DIEPTXF(x),
288 (0x0 << 16) | (tx_start / 4));
291 pf->usb.is_simplex = 1;
294 DPRINTF("FIFO%d = IN:%d / OUT:%d\n", x,
295 pf->usb.max_in_frame_size,
296 pf->usb.max_out_frame_size);
301 DWC_OTG_WRITE_4(sc, DOTG_GRSTCTL,
304 if (mode != DWC_MODE_OTG) {
305 /* reset all TX FIFOs */
306 DWC_OTG_WRITE_4(sc, DOTG_GRSTCTL,
307 GRSTCTL_TXFIFO(0x10) |
310 /* reset active endpoints */
311 sc->sc_active_rx_ep = 0;
317 dwc_otg_clocks_on(struct dwc_otg_softc *sc)
319 if (sc->sc_flags.clocks_off &&
320 sc->sc_flags.port_powered) {
324 /* TODO - platform specific */
326 sc->sc_flags.clocks_off = 0;
331 dwc_otg_clocks_off(struct dwc_otg_softc *sc)
333 if (!sc->sc_flags.clocks_off) {
337 /* TODO - platform specific */
339 sc->sc_flags.clocks_off = 1;
344 dwc_otg_pull_up(struct dwc_otg_softc *sc)
348 /* pullup D+, if possible */
350 if (!sc->sc_flags.d_pulled_up &&
351 sc->sc_flags.port_powered) {
352 sc->sc_flags.d_pulled_up = 1;
354 temp = DWC_OTG_READ_4(sc, DOTG_DCTL);
355 temp &= ~DCTL_SFTDISCON;
356 DWC_OTG_WRITE_4(sc, DOTG_DCTL, temp);
361 dwc_otg_pull_down(struct dwc_otg_softc *sc)
365 /* pulldown D+, if possible */
367 if (sc->sc_flags.d_pulled_up) {
368 sc->sc_flags.d_pulled_up = 0;
370 temp = DWC_OTG_READ_4(sc, DOTG_DCTL);
371 temp |= DCTL_SFTDISCON;
372 DWC_OTG_WRITE_4(sc, DOTG_DCTL, temp);
377 dwc_otg_enable_sof_irq(struct dwc_otg_softc *sc)
379 if (sc->sc_irq_mask & GINTSTS_SOF)
381 sc->sc_irq_mask |= GINTSTS_SOF;
382 DWC_OTG_WRITE_4(sc, DOTG_GINTMSK, sc->sc_irq_mask);
386 dwc_otg_resume_irq(struct dwc_otg_softc *sc)
388 if (sc->sc_flags.status_suspend) {
389 /* update status bits */
390 sc->sc_flags.status_suspend = 0;
391 sc->sc_flags.change_suspend = 1;
393 if (sc->sc_flags.status_device_mode) {
395 * Disable resume interrupt and enable suspend
398 sc->sc_irq_mask &= ~GINTSTS_WKUPINT;
399 sc->sc_irq_mask |= GINTSTS_USBSUSP;
400 DWC_OTG_WRITE_4(sc, DOTG_GINTMSK, sc->sc_irq_mask);
403 /* complete root HUB interrupt endpoint */
404 dwc_otg_root_intr(sc);
409 dwc_otg_suspend_irq(struct dwc_otg_softc *sc)
411 if (!sc->sc_flags.status_suspend) {
412 /* update status bits */
413 sc->sc_flags.status_suspend = 1;
414 sc->sc_flags.change_suspend = 1;
416 if (sc->sc_flags.status_device_mode) {
418 * Disable suspend interrupt and enable resume
421 sc->sc_irq_mask &= ~GINTSTS_USBSUSP;
422 sc->sc_irq_mask |= GINTSTS_WKUPINT;
423 DWC_OTG_WRITE_4(sc, DOTG_GINTMSK, sc->sc_irq_mask);
426 /* complete root HUB interrupt endpoint */
427 dwc_otg_root_intr(sc);
432 dwc_otg_wakeup_peer(struct dwc_otg_softc *sc)
434 if (!sc->sc_flags.status_suspend)
437 DPRINTFN(5, "Remote wakeup\n");
439 if (sc->sc_flags.status_device_mode) {
442 /* enable remote wakeup signalling */
443 temp = DWC_OTG_READ_4(sc, DOTG_DCTL);
444 temp |= DCTL_RMTWKUPSIG;
445 DWC_OTG_WRITE_4(sc, DOTG_DCTL, temp);
447 /* Wait 8ms for remote wakeup to complete. */
448 usb_pause_mtx(&sc->sc_bus.bus_mtx, hz / 125);
450 temp &= ~DCTL_RMTWKUPSIG;
451 DWC_OTG_WRITE_4(sc, DOTG_DCTL, temp);
453 /* enable USB port */
454 DWC_OTG_WRITE_4(sc, DOTG_PCGCCTL, 0);
457 usb_pause_mtx(&sc->sc_bus.bus_mtx, hz / 100);
460 sc->sc_hprt_val |= HPRT_PRTRES;
461 DWC_OTG_WRITE_4(sc, DOTG_HPRT, sc->sc_hprt_val);
463 /* Wait 100ms for resume signalling to complete. */
464 usb_pause_mtx(&sc->sc_bus.bus_mtx, hz / 10);
466 /* clear suspend and resume */
467 sc->sc_hprt_val &= ~(HPRT_PRTSUSP | HPRT_PRTRES);
468 DWC_OTG_WRITE_4(sc, DOTG_HPRT, sc->sc_hprt_val);
471 usb_pause_mtx(&sc->sc_bus.bus_mtx, hz / 250);
474 /* need to fake resume IRQ */
475 dwc_otg_resume_irq(sc);
479 dwc_otg_set_address(struct dwc_otg_softc *sc, uint8_t addr)
483 DPRINTFN(5, "addr=%d\n", addr);
485 temp = DWC_OTG_READ_4(sc, DOTG_DCFG);
486 temp &= ~DCFG_DEVADDR_SET(0x7F);
487 temp |= DCFG_DEVADDR_SET(addr);
488 DWC_OTG_WRITE_4(sc, DOTG_DCFG, temp);
492 dwc_otg_common_rx_ack(struct dwc_otg_softc *sc)
494 DPRINTFN(5, "RX status clear\n");
496 /* enable RX FIFO level interrupt */
497 sc->sc_irq_mask |= GINTSTS_RXFLVL;
498 DWC_OTG_WRITE_4(sc, DOTG_GINTMSK, sc->sc_irq_mask);
500 /* clear cached status */
501 sc->sc_last_rx_status = 0;
505 dwc_otg_clear_hcint(struct dwc_otg_softc *sc, uint8_t x)
509 hcint = DWC_OTG_READ_4(sc, DOTG_HCINT(x));
510 DWC_OTG_WRITE_4(sc, DOTG_HCINT(x), hcint);
512 /* clear buffered interrupts */
513 sc->sc_chan_state[x].hcint = 0;
517 dwc_otg_host_channel_wait(struct dwc_otg_td *td)
519 struct dwc_otg_softc *sc;
524 DPRINTF("CH=%d\n", x);
526 /* get pointer to softc */
527 sc = DWC_OTG_PC2SC(td->pc);
529 if (sc->sc_chan_state[x].wait_sof == 0) {
530 dwc_otg_clear_hcint(sc, x);
531 return (1); /* done */
535 return (0); /* wait */
537 /* find new disabled channel */
538 for (x = 1; x != sc->sc_host_ch_max; x++) {
540 if (sc->sc_chan_state[x].allocated)
542 if (sc->sc_chan_state[x].wait_sof != 0)
545 sc->sc_chan_state[td->channel].allocated = 0;
546 sc->sc_chan_state[x].allocated = 1;
548 if (sc->sc_chan_state[td->channel].suspended) {
549 sc->sc_chan_state[td->channel].suspended = 0;
550 sc->sc_chan_state[x].suspended = 1;
553 /* clear interrupts */
554 dwc_otg_clear_hcint(sc, x);
556 DPRINTF("CH=%d HCCHAR=0x%08x "
557 "HCSPLT=0x%08x\n", x, td->hcchar, td->hcsplt);
559 /* ack any pending messages */
560 if (sc->sc_last_rx_status != 0 &&
561 GRXSTSRD_CHNUM_GET(sc->sc_last_rx_status) == td->channel) {
562 /* get rid of message */
563 dwc_otg_common_rx_ack(sc);
566 /* move active channel */
567 sc->sc_active_rx_ep &= ~(1 << td->channel);
568 sc->sc_active_rx_ep |= (1 << x);
573 return (1); /* new channel allocated */
575 return (0); /* wait */
579 dwc_otg_host_channel_alloc(struct dwc_otg_td *td)
581 struct dwc_otg_softc *sc;
585 if (td->channel < DWC_OTG_MAX_CHANNELS)
586 return (0); /* already allocated */
588 /* get pointer to softc */
589 sc = DWC_OTG_PC2SC(td->pc);
591 if ((td->hcchar & HCCHAR_EPNUM_MASK) == 0) {
595 max_channel = sc->sc_host_ch_max;
599 for (; x != max_channel; x++) {
601 if (sc->sc_chan_state[x].allocated)
603 if (sc->sc_chan_state[x].wait_sof != 0)
606 sc->sc_chan_state[x].allocated = 1;
608 /* clear interrupts */
609 dwc_otg_clear_hcint(sc, x);
611 DPRINTF("CH=%d HCCHAR=0x%08x "
612 "HCSPLT=0x%08x\n", x, td->hcchar, td->hcsplt);
614 /* set active channel */
615 sc->sc_active_rx_ep |= (1 << x);
620 return (0); /* allocated */
622 return (1); /* busy */
626 dwc_otg_host_channel_disable(struct dwc_otg_softc *sc, uint8_t x)
629 if (sc->sc_chan_state[x].wait_sof != 0)
631 hcchar = DWC_OTG_READ_4(sc, DOTG_HCCHAR(x));
632 if (hcchar & (HCCHAR_CHENA | HCCHAR_CHDIS)) {
633 /* disable channel */
634 DWC_OTG_WRITE_4(sc, DOTG_HCCHAR(x),
635 HCCHAR_CHENA | HCCHAR_CHDIS);
636 /* don't re-use channel until next SOF is transmitted */
637 sc->sc_chan_state[x].wait_sof = 2;
638 /* enable SOF interrupt */
639 dwc_otg_enable_sof_irq(sc);
644 dwc_otg_host_channel_free(struct dwc_otg_td *td)
646 struct dwc_otg_softc *sc;
649 if (td->channel >= DWC_OTG_MAX_CHANNELS)
650 return; /* already freed */
654 td->channel = DWC_OTG_MAX_CHANNELS;
656 DPRINTF("CH=%d\n", x);
658 /* get pointer to softc */
659 sc = DWC_OTG_PC2SC(td->pc);
661 dwc_otg_host_channel_disable(sc, x);
663 sc->sc_chan_state[x].allocated = 0;
664 sc->sc_chan_state[x].suspended = 0;
666 /* ack any pending messages */
667 if (sc->sc_last_rx_status != 0 &&
668 GRXSTSRD_CHNUM_GET(sc->sc_last_rx_status) == x) {
669 dwc_otg_common_rx_ack(sc);
672 /* clear active channel */
673 sc->sc_active_rx_ep &= ~(1 << x);
677 dwc_otg_host_setup_tx(struct dwc_otg_td *td)
679 struct usb_device_request req __aligned(4);
680 struct dwc_otg_softc *sc;
684 if (dwc_otg_host_channel_alloc(td))
685 return (1); /* busy */
687 /* get pointer to softc */
688 sc = DWC_OTG_PC2SC(td->pc);
690 hcint = sc->sc_chan_state[td->channel].hcint;
692 DPRINTF("CH=%d ST=%d HCINT=0x%08x HCCHAR=0x%08x HCTSIZ=0x%08x\n",
693 td->channel, td->state, hcint,
694 DWC_OTG_READ_4(sc, DOTG_HCCHAR(td->channel)),
695 DWC_OTG_READ_4(sc, DOTG_HCTSIZ(td->channel)));
697 if (hcint & (HCINT_RETRY |
698 HCINT_ACK | HCINT_NYET)) {
699 /* give success bits priority over failure bits */
700 } else if (hcint & HCINT_STALL) {
701 DPRINTF("CH=%d STALL\n", td->channel);
704 return (0); /* complete */
705 } else if (hcint & HCINT_ERRORS) {
706 DPRINTF("CH=%d ERROR\n", td->channel);
708 if (td->hcsplt != 0 || td->errcnt >= 3) {
710 return (0); /* complete */
714 /* channel must be disabled before we can complete the transfer */
716 if (hcint & (HCINT_ERRORS | HCINT_RETRY |
717 HCINT_ACK | HCINT_NYET)) {
719 dwc_otg_host_channel_disable(sc, td->channel);
721 if (!(hcint & HCINT_ERRORS))
726 case DWC_CHAN_ST_START:
729 case DWC_CHAN_ST_WAIT_ANE:
730 if (hcint & (HCINT_RETRY | HCINT_ERRORS)) {
731 if (!dwc_otg_host_channel_wait(td))
736 if (hcint & (HCINT_ACK | HCINT_NYET)) {
737 if (!dwc_otg_host_channel_wait(td))
739 td->offset += td->tx_bytes;
740 td->remainder -= td->tx_bytes;
742 return (0); /* complete */
745 case DWC_CHAN_ST_WAIT_S_ANE:
746 if (hcint & (HCINT_RETRY | HCINT_ERRORS)) {
747 if (!dwc_otg_host_channel_wait(td))
752 if (hcint & (HCINT_ACK | HCINT_NYET)) {
753 if (!dwc_otg_host_channel_wait(td))
758 case DWC_CHAN_ST_WAIT_C_ANE:
759 if (hcint & HCINT_NYET) {
760 if (!dwc_otg_host_channel_wait(td))
764 if (hcint & (HCINT_RETRY | HCINT_ERRORS)) {
765 if (!dwc_otg_host_channel_wait(td))
770 if (hcint & HCINT_ACK) {
771 if (!dwc_otg_host_channel_wait(td))
773 td->offset += td->tx_bytes;
774 td->remainder -= td->tx_bytes;
776 return (0); /* complete */
779 case DWC_CHAN_ST_TX_PKT_SYNC:
784 return (1); /* busy */
787 if (sizeof(req) != td->remainder) {
789 return (0); /* complete */
793 if (td->hcsplt != 0) {
796 count = DWC_OTG_READ_4(sc, DOTG_HFNUM) & 7;
797 /* check for not first microframe */
799 /* enable SOF interrupt */
800 dwc_otg_enable_sof_irq(sc);
802 td->state = DWC_CHAN_ST_TX_PKT_SYNC;
803 dwc_otg_host_channel_free(td);
804 return (1); /* busy */
807 td->hcsplt &= ~HCSPLT_COMPSPLT;
808 td->state = DWC_CHAN_ST_WAIT_S_ANE;
810 td->state = DWC_CHAN_ST_WAIT_ANE;
813 usbd_copy_out(td->pc, 0, &req, sizeof(req));
815 DWC_OTG_WRITE_4(sc, DOTG_HCTSIZ(td->channel),
816 (sizeof(req) << HCTSIZ_XFERSIZE_SHIFT) |
817 (1 << HCTSIZ_PKTCNT_SHIFT) |
818 (HCTSIZ_PID_SETUP << HCTSIZ_PID_SHIFT));
820 DWC_OTG_WRITE_4(sc, DOTG_HCSPLT(td->channel), td->hcsplt);
823 hcchar &= ~HCCHAR_EPDIR_IN;
825 /* must enable channel before writing data to FIFO */
826 DWC_OTG_WRITE_4(sc, DOTG_HCCHAR(td->channel), hcchar);
828 /* transfer data into FIFO */
829 bus_space_write_region_4(sc->sc_io_tag, sc->sc_io_hdl,
830 DOTG_DFIFO(td->channel), (uint32_t *)&req, sizeof(req) / 4);
832 /* store number of bytes transmitted */
833 td->tx_bytes = sizeof(req);
835 return (1); /* busy */
838 td->hcsplt |= HCSPLT_COMPSPLT;
839 td->state = DWC_CHAN_ST_WAIT_C_ANE;
841 DWC_OTG_WRITE_4(sc, DOTG_HCTSIZ(td->channel),
842 (HCTSIZ_PID_SETUP << HCTSIZ_PID_SHIFT));
844 DWC_OTG_WRITE_4(sc, DOTG_HCSPLT(td->channel), td->hcsplt);
847 hcchar &= ~HCCHAR_EPDIR_IN;
849 /* must enable channel before writing data to FIFO */
850 DWC_OTG_WRITE_4(sc, DOTG_HCCHAR(td->channel), hcchar);
852 return (1); /* busy */
856 dwc_otg_setup_rx(struct dwc_otg_td *td)
858 struct dwc_otg_softc *sc;
859 struct usb_device_request req __aligned(4);
863 /* get pointer to softc */
864 sc = DWC_OTG_PC2SC(td->pc);
866 /* check endpoint status */
868 if (sc->sc_last_rx_status == 0)
871 if (GRXSTSRD_CHNUM_GET(sc->sc_last_rx_status) != 0)
874 if ((sc->sc_last_rx_status & GRXSTSRD_DPID_MASK) !=
875 GRXSTSRD_DPID_DATA0) {
877 dwc_otg_common_rx_ack(sc);
881 if ((sc->sc_last_rx_status & GRXSTSRD_PKTSTS_MASK) !=
884 dwc_otg_common_rx_ack(sc);
888 DPRINTFN(5, "GRXSTSR=0x%08x\n", sc->sc_last_rx_status);
890 /* clear did stall */
893 /* get the packet byte count */
894 count = GRXSTSRD_BCNT_GET(sc->sc_last_rx_status);
896 /* verify data length */
897 if (count != td->remainder) {
898 DPRINTFN(0, "Invalid SETUP packet "
899 "length, %d bytes\n", count);
901 dwc_otg_common_rx_ack(sc);
904 if (count != sizeof(req)) {
905 DPRINTFN(0, "Unsupported SETUP packet "
906 "length, %d bytes\n", count);
908 dwc_otg_common_rx_ack(sc);
912 /* copy in control request */
913 memcpy(&req, sc->sc_rx_bounce_buffer, sizeof(req));
915 /* copy data into real buffer */
916 usbd_copy_in(td->pc, 0, &req, sizeof(req));
918 td->offset = sizeof(req);
921 /* sneak peek the set address */
922 if ((req.bmRequestType == UT_WRITE_DEVICE) &&
923 (req.bRequest == UR_SET_ADDRESS)) {
924 /* must write address before ZLP */
925 dwc_otg_set_address(sc, req.wValue[0] & 0x7F);
928 /* don't send any data by default */
929 DWC_OTG_WRITE_4(sc, DOTG_DIEPTSIZ(0),
930 DXEPTSIZ_SET_NPKT(0) |
931 DXEPTSIZ_SET_NBYTES(0));
933 temp = sc->sc_in_ctl[0];
935 /* enable IN endpoint */
936 DWC_OTG_WRITE_4(sc, DOTG_DIEPCTL(0),
937 temp | DIEPCTL_EPENA);
938 DWC_OTG_WRITE_4(sc, DOTG_DIEPCTL(0),
939 temp | DIEPCTL_SNAK);
941 /* reset IN endpoint buffer */
942 DWC_OTG_WRITE_4(sc, DOTG_GRSTCTL,
946 /* acknowledge RX status */
947 dwc_otg_common_rx_ack(sc);
948 return (0); /* complete */
951 /* abort any ongoing transfer, before enabling again */
953 temp = sc->sc_out_ctl[0];
955 temp |= DOEPCTL_EPENA |
958 /* enable OUT endpoint */
959 DWC_OTG_WRITE_4(sc, DOTG_DOEPCTL(0), temp);
961 if (!td->did_stall) {
964 DPRINTFN(5, "stalling IN and OUT direction\n");
966 /* set stall after enabling endpoint */
967 DWC_OTG_WRITE_4(sc, DOTG_DOEPCTL(0),
968 temp | DOEPCTL_STALL);
970 temp = sc->sc_in_ctl[0];
972 /* set stall assuming endpoint is enabled */
973 DWC_OTG_WRITE_4(sc, DOTG_DIEPCTL(0),
974 temp | DIEPCTL_STALL);
977 /* setup number of buffers to receive */
978 DWC_OTG_WRITE_4(sc, DOTG_DOEPTSIZ(0),
979 DXEPTSIZ_SET_MULTI(3) |
980 DXEPTSIZ_SET_NPKT(1) |
981 DXEPTSIZ_SET_NBYTES(sizeof(req)));
983 return (1); /* not complete */
987 dwc_otg_host_rate_check(struct dwc_otg_td *td)
989 struct dwc_otg_softc *sc;
992 /* get pointer to softc */
993 sc = DWC_OTG_PC2SC(td->pc);
995 ep_type = ((td->hcchar &
996 HCCHAR_EPTYPE_MASK) >> HCCHAR_EPTYPE_SHIFT);
998 if (sc->sc_chan_state[td->channel].suspended)
1001 if (ep_type == UE_ISOCHRONOUS) {
1002 if (td->tmr_val & 1)
1003 td->hcchar |= HCCHAR_ODDFRM;
1005 td->hcchar &= ~HCCHAR_ODDFRM;
1006 td->tmr_val += td->tmr_res;
1007 } else if (ep_type == UE_INTERRUPT) {
1010 delta = sc->sc_tmr_val - td->tmr_val;
1013 td->tmr_val = sc->sc_tmr_val + td->tmr_res;
1014 } else if (td->did_nak != 0) {
1018 if (ep_type == UE_ISOCHRONOUS) {
1020 } else if (td->set_toggle) {
1030 dwc_otg_host_data_rx(struct dwc_otg_td *td)
1032 struct dwc_otg_softc *sc;
1038 if (dwc_otg_host_channel_alloc(td))
1039 return (1); /* busy */
1041 /* get pointer to softc */
1042 sc = DWC_OTG_PC2SC(td->pc);
1044 ep_type = ((td->hcchar &
1045 HCCHAR_EPTYPE_MASK) >> HCCHAR_EPTYPE_SHIFT);
1047 hcint = sc->sc_chan_state[td->channel].hcint;
1049 DPRINTF("CH=%d ST=%d HCINT=0x%08x HCCHAR=0x%08x HCTSIZ=0x%08x\n",
1050 td->channel, td->state, hcint,
1051 DWC_OTG_READ_4(sc, DOTG_HCCHAR(td->channel)),
1052 DWC_OTG_READ_4(sc, DOTG_HCTSIZ(td->channel)));
1054 /* check interrupt bits */
1056 if (hcint & (HCINT_RETRY |
1057 HCINT_ACK | HCINT_NYET)) {
1058 /* give success bits priority over failure bits */
1059 } else if (hcint & HCINT_STALL) {
1060 DPRINTF("CH=%d STALL\n", td->channel);
1061 td->error_stall = 1;
1063 return (0); /* complete */
1064 } else if (hcint & HCINT_ERRORS) {
1065 DPRINTF("CH=%d ERROR\n", td->channel);
1067 if (td->hcsplt != 0 || td->errcnt >= 3) {
1069 return (0); /* complete */
1073 /* channel must be disabled before we can complete the transfer */
1075 if (hcint & (HCINT_ERRORS | HCINT_RETRY |
1076 HCINT_ACK | HCINT_NYET)) {
1078 dwc_otg_host_channel_disable(sc, td->channel);
1080 if (!(hcint & HCINT_ERRORS))
1084 /* check endpoint status */
1085 if (sc->sc_last_rx_status == 0)
1088 if (GRXSTSRD_CHNUM_GET(sc->sc_last_rx_status) != td->channel)
1091 switch (sc->sc_last_rx_status & GRXSTSRD_PKTSTS_MASK) {
1092 case GRXSTSRH_IN_DATA:
1094 DPRINTF("DATA ST=%d STATUS=0x%08x\n",
1095 (int)td->state, (int)sc->sc_last_rx_status);
1097 if (hcint & HCINT_SOFTWARE_ONLY) {
1099 * When using SPLIT transactions on interrupt
1100 * endpoints, sometimes data occurs twice.
1102 DPRINTF("Data already received\n");
1108 /* get the packet byte count */
1109 count = GRXSTSRD_BCNT_GET(sc->sc_last_rx_status);
1111 /* verify the packet byte count */
1112 if (count != td->max_packet_size) {
1113 if (count < td->max_packet_size) {
1114 /* we have a short packet */
1118 /* invalid USB packet */
1122 dwc_otg_common_rx_ack(sc);
1123 return (0); /* we are complete */
1127 /* verify the packet byte count */
1128 if (count > td->remainder) {
1129 /* invalid USB packet */
1133 dwc_otg_common_rx_ack(sc);
1134 return (0); /* we are complete */
1137 usbd_copy_in(td->pc, td->offset,
1138 sc->sc_rx_bounce_buffer, count);
1140 td->remainder -= count;
1141 td->offset += count;
1142 hcint |= HCINT_SOFTWARE_ONLY | HCINT_ACK;
1143 sc->sc_chan_state[td->channel].hcint = hcint;
1151 dwc_otg_common_rx_ack(sc);
1154 switch (td->state) {
1155 case DWC_CHAN_ST_START:
1156 if (td->hcsplt != 0)
1161 case DWC_CHAN_ST_WAIT_ANE:
1162 if (hcint & (HCINT_RETRY | HCINT_ERRORS)) {
1163 if (!dwc_otg_host_channel_wait(td))
1167 if (td->hcsplt != 0)
1172 if (!(hcint & HCINT_SOFTWARE_ONLY)) {
1173 if (hcint & HCINT_NYET) {
1174 if (td->hcsplt != 0) {
1175 if (!dwc_otg_host_channel_wait(td))
1182 if (hcint & (HCINT_ACK | HCINT_NYET)) {
1183 if (!dwc_otg_host_channel_wait(td))
1186 /* check if we are complete */
1187 if ((td->remainder == 0) || (td->got_short != 0)) {
1189 return (0); /* complete */
1192 * Else need to receive a zero length
1196 if (td->hcsplt != 0)
1203 case DWC_CHAN_ST_WAIT_S_ANE:
1204 if (hcint & (HCINT_RETRY | HCINT_ERRORS)) {
1205 if (!dwc_otg_host_channel_wait(td))
1211 if (hcint & (HCINT_ACK | HCINT_NYET)) {
1212 if (!dwc_otg_host_channel_wait(td))
1218 case DWC_CHAN_ST_RX_PKT:
1221 case DWC_CHAN_ST_RX_SPKT:
1224 case DWC_CHAN_ST_RX_SPKT_SYNC:
1225 goto receive_spkt_sync;
1233 if (td->hcsplt != 0) {
1234 count = DWC_OTG_READ_4(sc, DOTG_HFNUM) & 7;
1236 /* check for even microframes */
1237 if (count == td->curr_frame) {
1238 td->state = DWC_CHAN_ST_RX_PKT;
1239 dwc_otg_host_channel_free(td);
1240 /* enable SOF interrupt */
1241 dwc_otg_enable_sof_irq(sc);
1243 } else if (count == 0) {
1244 /* check for start split timeout */
1248 td->curr_frame = count;
1249 td->hcsplt |= HCSPLT_COMPSPLT;
1250 } else if (dwc_otg_host_rate_check(td)) {
1251 td->state = DWC_CHAN_ST_RX_PKT;
1252 dwc_otg_host_channel_free(td);
1256 td->state = DWC_CHAN_ST_WAIT_ANE;
1258 /* receive one packet */
1259 DWC_OTG_WRITE_4(sc, DOTG_HCTSIZ(td->channel),
1260 (td->max_packet_size << HCTSIZ_XFERSIZE_SHIFT) |
1261 (1 << HCTSIZ_PKTCNT_SHIFT) |
1262 (td->toggle ? (HCTSIZ_PID_DATA1 << HCTSIZ_PID_SHIFT) :
1263 (HCTSIZ_PID_DATA0 << HCTSIZ_PID_SHIFT)));
1265 DWC_OTG_WRITE_4(sc, DOTG_HCSPLT(td->channel), td->hcsplt);
1267 hcchar = td->hcchar;
1268 hcchar |= HCCHAR_EPDIR_IN;
1270 /* must enable channel before data can be received */
1271 DWC_OTG_WRITE_4(sc, DOTG_HCCHAR(td->channel), hcchar);
1276 if (dwc_otg_host_rate_check(td)) {
1277 td->state = DWC_CHAN_ST_RX_SPKT;
1278 dwc_otg_host_channel_free(td);
1283 if (ep_type == UE_INTERRUPT ||
1284 ep_type == UE_ISOCHRONOUS) {
1285 count = DWC_OTG_READ_4(sc, DOTG_HFNUM) & 7;
1286 td->curr_frame = count;
1288 /* check for non-zero microframe */
1290 /* enable SOF interrupt */
1291 dwc_otg_enable_sof_irq(sc);
1293 td->state = DWC_CHAN_ST_RX_SPKT_SYNC;
1294 dwc_otg_host_channel_free(td);
1298 count = DWC_OTG_READ_4(sc, DOTG_HFNUM) & 7;
1299 td->curr_frame = count;
1301 /* check for two last frames */
1303 /* enable SOF interrupt */
1304 dwc_otg_enable_sof_irq(sc);
1306 td->state = DWC_CHAN_ST_RX_SPKT_SYNC;
1307 dwc_otg_host_channel_free(td);
1312 td->hcsplt &= ~HCSPLT_COMPSPLT;
1313 td->state = DWC_CHAN_ST_WAIT_S_ANE;
1315 /* receive one packet */
1316 DWC_OTG_WRITE_4(sc, DOTG_HCTSIZ(td->channel),
1317 (td->toggle ? (HCTSIZ_PID_DATA1 << HCTSIZ_PID_SHIFT) :
1318 (HCTSIZ_PID_DATA0 << HCTSIZ_PID_SHIFT)));
1320 DWC_OTG_WRITE_4(sc, DOTG_HCSPLT(td->channel), td->hcsplt);
1322 hcchar = td->hcchar;
1323 hcchar |= HCCHAR_EPDIR_IN;
1325 /* must enable channel before data can be received */
1326 DWC_OTG_WRITE_4(sc, DOTG_HCCHAR(td->channel), hcchar);
1329 return (1); /* busy */
1333 dwc_otg_data_rx(struct dwc_otg_td *td)
1335 struct dwc_otg_softc *sc;
1342 /* get pointer to softc */
1343 sc = DWC_OTG_PC2SC(td->pc);
1345 /* check endpoint status */
1346 if (sc->sc_last_rx_status == 0)
1349 if (GRXSTSRD_CHNUM_GET(sc->sc_last_rx_status) != td->ep_no)
1352 /* check for SETUP packet */
1353 if ((sc->sc_last_rx_status & GRXSTSRD_PKTSTS_MASK) ==
1354 GRXSTSRD_STP_DATA) {
1355 if (td->remainder == 0) {
1357 * We are actually complete and have
1358 * received the next SETUP
1360 DPRINTFN(5, "faking complete\n");
1361 return (0); /* complete */
1364 * USB Host Aborted the transfer.
1367 return (0); /* complete */
1370 if ((sc->sc_last_rx_status & GRXSTSRD_PKTSTS_MASK) !=
1371 GRXSTSRD_OUT_DATA) {
1373 dwc_otg_common_rx_ack(sc);
1377 /* get the packet byte count */
1378 count = GRXSTSRD_BCNT_GET(sc->sc_last_rx_status);
1380 /* verify the packet byte count */
1381 if (count != td->max_packet_size) {
1382 if (count < td->max_packet_size) {
1383 /* we have a short packet */
1387 /* invalid USB packet */
1391 dwc_otg_common_rx_ack(sc);
1392 return (0); /* we are complete */
1395 /* verify the packet byte count */
1396 if (count > td->remainder) {
1397 /* invalid USB packet */
1401 dwc_otg_common_rx_ack(sc);
1402 return (0); /* we are complete */
1405 usbd_copy_in(td->pc, td->offset, sc->sc_rx_bounce_buffer, count);
1406 td->remainder -= count;
1407 td->offset += count;
1410 dwc_otg_common_rx_ack(sc);
1412 /* check if we are complete */
1413 if ((td->remainder == 0) || got_short) {
1414 if (td->short_pkt) {
1415 /* we are complete */
1418 /* else need to receive a zero length packet */
1423 temp = sc->sc_out_ctl[td->ep_no];
1425 temp |= DOEPCTL_EPENA | DOEPCTL_CNAK;
1427 DWC_OTG_WRITE_4(sc, DOTG_DOEPCTL(td->ep_no), temp);
1429 /* enable SETUP and transfer complete interrupt */
1430 if (td->ep_no == 0) {
1431 DWC_OTG_WRITE_4(sc, DOTG_DOEPTSIZ(0),
1432 DXEPTSIZ_SET_NPKT(1) |
1433 DXEPTSIZ_SET_NBYTES(td->max_packet_size));
1435 /* allow reception of multiple packets */
1436 DWC_OTG_WRITE_4(sc, DOTG_DOEPTSIZ(td->ep_no),
1437 DXEPTSIZ_SET_MULTI(1) |
1438 DXEPTSIZ_SET_NPKT(4) |
1439 DXEPTSIZ_SET_NBYTES(4 *
1440 ((td->max_packet_size + 3) & ~3)));
1442 return (1); /* not complete */
1446 dwc_otg_host_data_tx(struct dwc_otg_td *td)
1448 struct dwc_otg_softc *sc;
1454 if (dwc_otg_host_channel_alloc(td))
1455 return (1); /* busy */
1457 /* get pointer to softc */
1458 sc = DWC_OTG_PC2SC(td->pc);
1460 ep_type = ((td->hcchar &
1461 HCCHAR_EPTYPE_MASK) >> HCCHAR_EPTYPE_SHIFT);
1463 hcint = sc->sc_chan_state[td->channel].hcint;
1465 DPRINTF("CH=%d ST=%d HCINT=0x%08x HCCHAR=0x%08x HCTSIZ=0x%08x\n",
1466 td->channel, td->state, hcint,
1467 DWC_OTG_READ_4(sc, DOTG_HCCHAR(td->channel)),
1468 DWC_OTG_READ_4(sc, DOTG_HCTSIZ(td->channel)));
1470 if (hcint & (HCINT_RETRY |
1471 HCINT_ACK | HCINT_NYET)) {
1472 /* give success bits priority over failure bits */
1473 } else if (hcint & HCINT_STALL) {
1474 DPRINTF("CH=%d STALL\n", td->channel);
1475 td->error_stall = 1;
1477 return (0); /* complete */
1478 } else if (hcint & HCINT_ERRORS) {
1479 DPRINTF("CH=%d ERROR\n", td->channel);
1481 if (td->hcsplt != 0 || td->errcnt >= 3) {
1483 return (0); /* complete */
1487 /* channel must be disabled before we can complete the transfer */
1489 if (hcint & (HCINT_ERRORS | HCINT_RETRY |
1490 HCINT_ACK | HCINT_NYET)) {
1492 dwc_otg_host_channel_disable(sc, td->channel);
1494 if (!(hcint & HCINT_ERRORS))
1498 switch (td->state) {
1499 case DWC_CHAN_ST_START:
1502 case DWC_CHAN_ST_WAIT_ANE:
1503 if (hcint & (HCINT_RETRY | HCINT_ERRORS)) {
1504 if (!dwc_otg_host_channel_wait(td))
1509 if (hcint & (HCINT_ACK | HCINT_NYET)) {
1510 if (!dwc_otg_host_channel_wait(td))
1513 td->offset += td->tx_bytes;
1514 td->remainder -= td->tx_bytes;
1517 /* check remainder */
1518 if (td->remainder == 0) {
1520 return (0); /* complete */
1523 * Else we need to transmit a short
1530 case DWC_CHAN_ST_WAIT_S_ANE:
1531 if (hcint & (HCINT_RETRY | HCINT_ERRORS)) {
1532 if (!dwc_otg_host_channel_wait(td))
1537 if (hcint & (HCINT_ACK | HCINT_NYET)) {
1538 if (!dwc_otg_host_channel_wait(td))
1543 case DWC_CHAN_ST_WAIT_C_ANE:
1544 if (hcint & HCINT_NYET) {
1545 if (!dwc_otg_host_channel_wait(td))
1549 if (hcint & (HCINT_RETRY | HCINT_ERRORS)) {
1550 if (!dwc_otg_host_channel_wait(td))
1555 if (hcint & HCINT_ACK) {
1556 if (!dwc_otg_host_channel_wait(td))
1558 td->offset += td->tx_bytes;
1559 td->remainder -= td->tx_bytes;
1562 /* check remainder */
1563 if (td->remainder == 0) {
1565 return (0); /* complete */
1567 /* else we need to transmit a short packet */
1573 case DWC_CHAN_ST_TX_PKT:
1576 case DWC_CHAN_ST_TX_PKT_SYNC:
1579 case DWC_CHAN_ST_TX_CPKT:
1588 if (dwc_otg_host_rate_check(td)) {
1589 td->state = DWC_CHAN_ST_TX_PKT;
1590 dwc_otg_host_channel_free(td);
1595 if (td->hcsplt != 0) {
1596 count = DWC_OTG_READ_4(sc, DOTG_HFNUM) & 7;
1597 /* check for first or last microframe */
1598 if (count == 7 || count == 0) {
1599 /* enable SOF interrupt */
1600 dwc_otg_enable_sof_irq(sc);
1602 td->state = DWC_CHAN_ST_TX_PKT_SYNC;
1603 dwc_otg_host_channel_free(td);
1607 td->hcsplt &= ~HCSPLT_COMPSPLT;
1608 td->state = DWC_CHAN_ST_WAIT_S_ANE;
1610 td->state = DWC_CHAN_ST_WAIT_ANE;
1613 /* send one packet at a time */
1614 count = td->max_packet_size;
1615 if (td->remainder < count) {
1616 /* we have a short packet */
1618 count = td->remainder;
1621 /* TODO: HCTSIZ_DOPNG */
1623 DWC_OTG_WRITE_4(sc, DOTG_HCTSIZ(td->channel),
1624 (count << HCTSIZ_XFERSIZE_SHIFT) |
1625 (1 << HCTSIZ_PKTCNT_SHIFT) |
1626 (td->toggle ? (HCTSIZ_PID_DATA1 << HCTSIZ_PID_SHIFT) :
1627 (HCTSIZ_PID_DATA0 << HCTSIZ_PID_SHIFT)));
1629 DWC_OTG_WRITE_4(sc, DOTG_HCSPLT(td->channel), td->hcsplt);
1631 hcchar = td->hcchar;
1632 hcchar &= ~HCCHAR_EPDIR_IN;
1634 /* must enable before writing data to FIFO */
1635 DWC_OTG_WRITE_4(sc, DOTG_HCCHAR(td->channel), hcchar);
1639 /* clear topmost word before copy */
1640 sc->sc_tx_bounce_buffer[(count - 1) / 4] = 0;
1643 usbd_copy_out(td->pc, td->offset,
1644 sc->sc_tx_bounce_buffer, count);
1646 /* transfer data into FIFO */
1647 bus_space_write_region_4(sc->sc_io_tag, sc->sc_io_hdl,
1648 DOTG_DFIFO(td->channel),
1649 sc->sc_tx_bounce_buffer, (count + 3) / 4);
1652 /* store number of bytes transmitted */
1653 td->tx_bytes = count;
1658 count = DWC_OTG_READ_4(sc, DOTG_HFNUM) & 7;
1659 /* check for first microframe */
1661 /* send packet again */
1665 td->hcsplt |= HCSPLT_COMPSPLT;
1666 td->state = DWC_CHAN_ST_WAIT_C_ANE;
1668 DWC_OTG_WRITE_4(sc, DOTG_HCTSIZ(td->channel),
1669 (td->toggle ? (HCTSIZ_PID_DATA1 << HCTSIZ_PID_SHIFT) :
1670 (HCTSIZ_PID_DATA0 << HCTSIZ_PID_SHIFT)));
1672 DWC_OTG_WRITE_4(sc, DOTG_HCSPLT(td->channel), td->hcsplt);
1674 hcchar = td->hcchar;
1675 hcchar &= ~HCCHAR_EPDIR_IN;
1677 /* must enable channel before writing data to FIFO */
1678 DWC_OTG_WRITE_4(sc, DOTG_HCCHAR(td->channel), hcchar);
1681 return (1); /* busy */
1685 dwc_otg_data_tx(struct dwc_otg_td *td)
1687 struct dwc_otg_softc *sc;
1688 uint32_t max_buffer;
1695 to = 3; /* don't loop forever! */
1697 /* get pointer to softc */
1698 sc = DWC_OTG_PC2SC(td->pc);
1700 max_buffer = sc->sc_hw_ep_profile[td->ep_no].max_buffer;
1703 /* check for for endpoint 0 data */
1705 temp = sc->sc_last_rx_status;
1707 if ((td->ep_no == 0) && (temp != 0) &&
1708 (GRXSTSRD_CHNUM_GET(temp) == 0)) {
1710 if ((temp & GRXSTSRD_PKTSTS_MASK) !=
1711 GRXSTSRD_STP_DATA) {
1713 /* dump data - wrong direction */
1714 dwc_otg_common_rx_ack(sc);
1717 * The current transfer was cancelled
1721 return (0); /* complete */
1725 /* fill in more TX data, if possible */
1726 if (td->tx_bytes != 0) {
1730 /* check if packets have been transferred */
1731 temp = DWC_OTG_READ_4(sc, DOTG_DIEPTSIZ(td->ep_no));
1733 /* get current packet number */
1734 cpkt = DXEPTSIZ_GET_NPKT(temp);
1736 if (cpkt >= td->npkt) {
1739 if (max_buffer != 0) {
1740 fifo_left = (td->npkt - cpkt) *
1741 td->max_packet_size;
1743 if (fifo_left > max_buffer)
1744 fifo_left = max_buffer;
1746 fifo_left = td->max_packet_size;
1750 count = td->tx_bytes;
1751 if (count > fifo_left)
1756 /* clear topmost word before copy */
1757 sc->sc_tx_bounce_buffer[(count - 1) / 4] = 0;
1760 usbd_copy_out(td->pc, td->offset,
1761 sc->sc_tx_bounce_buffer, count);
1763 /* transfer data into FIFO */
1764 bus_space_write_region_4(sc->sc_io_tag, sc->sc_io_hdl,
1765 DOTG_DFIFO(td->ep_no),
1766 sc->sc_tx_bounce_buffer, (count + 3) / 4);
1768 td->tx_bytes -= count;
1769 td->remainder -= count;
1770 td->offset += count;
1773 if (td->tx_bytes != 0)
1776 /* check remainder */
1777 if (td->remainder == 0) {
1779 return (0); /* complete */
1781 /* else we need to transmit a short packet */
1788 /* check if not all packets have been transferred */
1789 temp = DWC_OTG_READ_4(sc, DOTG_DIEPTSIZ(td->ep_no));
1791 if (DXEPTSIZ_GET_NPKT(temp) != 0) {
1793 DPRINTFN(5, "busy ep=%d npkt=%d DIEPTSIZ=0x%08x "
1794 "DIEPCTL=0x%08x\n", td->ep_no,
1795 DXEPTSIZ_GET_NPKT(temp),
1796 temp, DWC_OTG_READ_4(sc, DOTG_DIEPCTL(td->ep_no)));
1801 DPRINTFN(5, "rem=%u ep=%d\n", td->remainder, td->ep_no);
1803 /* try to optimise by sending more data */
1804 if ((max_buffer != 0) && ((td->max_packet_size & 3) == 0)) {
1806 /* send multiple packets at the same time */
1807 mpkt = max_buffer / td->max_packet_size;
1812 count = td->remainder;
1813 if (count > 0x7FFFFF)
1814 count = 0x7FFFFF - (0x7FFFFF % td->max_packet_size);
1816 td->npkt = count / td->max_packet_size;
1819 * NOTE: We could use 0x3FE instead of "mpkt" in the
1820 * check below to get more throughput, but then we
1821 * have a dependency towards non-generic chip features
1822 * to disable the TX-FIFO-EMPTY interrupts on a per
1823 * endpoint basis. Increase the maximum buffer size of
1824 * the IN endpoint to increase the performance.
1826 if (td->npkt > mpkt) {
1828 count = td->max_packet_size * mpkt;
1829 } else if ((count == 0) || (count % td->max_packet_size)) {
1830 /* we are transmitting a short packet */
1835 /* send one packet at a time */
1837 count = td->max_packet_size;
1838 if (td->remainder < count) {
1839 /* we have a short packet */
1841 count = td->remainder;
1845 DWC_OTG_WRITE_4(sc, DOTG_DIEPTSIZ(td->ep_no),
1846 DXEPTSIZ_SET_MULTI(1) |
1847 DXEPTSIZ_SET_NPKT(td->npkt) |
1848 DXEPTSIZ_SET_NBYTES(count));
1850 /* make room for buffering */
1853 temp = sc->sc_in_ctl[td->ep_no];
1855 /* must enable before writing data to FIFO */
1856 DWC_OTG_WRITE_4(sc, DOTG_DIEPCTL(td->ep_no), temp |
1860 td->tx_bytes = count;
1862 /* check remainder */
1863 if (td->tx_bytes == 0 &&
1864 td->remainder == 0) {
1866 return (0); /* complete */
1868 /* else we need to transmit a short packet */
1873 return (1); /* not complete */
1877 dwc_otg_data_tx_sync(struct dwc_otg_td *td)
1879 struct dwc_otg_softc *sc;
1882 /* get pointer to softc */
1883 sc = DWC_OTG_PC2SC(td->pc);
1886 * If all packets are transferred we are complete:
1888 temp = DWC_OTG_READ_4(sc, DOTG_DIEPTSIZ(td->ep_no));
1890 /* check that all packets have been transferred */
1891 if (DXEPTSIZ_GET_NPKT(temp) != 0) {
1892 DPRINTFN(5, "busy ep=%d\n", td->ep_no);
1899 /* we only want to know if there is a SETUP packet or free IN packet */
1901 temp = sc->sc_last_rx_status;
1903 if ((td->ep_no == 0) && (temp != 0) &&
1904 (GRXSTSRD_CHNUM_GET(temp) == 0)) {
1906 if ((temp & GRXSTSRD_PKTSTS_MASK) ==
1907 GRXSTSRD_STP_DATA) {
1908 DPRINTFN(5, "faking complete\n");
1910 * Race condition: We are complete!
1914 /* dump data - wrong direction */
1915 dwc_otg_common_rx_ack(sc);
1918 return (1); /* not complete */
1922 dwc_otg_xfer_do_fifo(struct usb_xfer *xfer)
1924 struct dwc_otg_td *td;
1932 td = xfer->td_transfer_cache;
1935 * If we are suspended in host mode and no channel is
1936 * allocated, simply return:
1938 if (xfer->xroot->udev->flags.self_suspended != 0 &&
1939 xfer->xroot->udev->flags.usb_mode == USB_MODE_HOST &&
1940 td->channel >= DWC_OTG_MAX_CHANNELS) {
1941 return (1); /* not complete */
1944 if ((td->func) (td)) {
1945 /* operation in progress */
1948 if (((void *)td) == xfer->td_transfer_last) {
1951 if (td->error_any) {
1953 } else if (td->remainder > 0) {
1955 * We had a short transfer. If there is no alternate
1956 * next, stop processing !
1963 * Fetch the next transfer descriptor and transfer
1964 * some flags to the next transfer descriptor
1966 tmr_res = td->tmr_res;
1967 tmr_val = td->tmr_val;
1968 toggle = td->toggle;
1969 channel = td->channel;
1971 xfer->td_transfer_cache = td;
1972 td->toggle = toggle; /* transfer toggle */
1973 td->channel = channel; /* transfer channel */
1974 td->tmr_res = tmr_res;
1975 td->tmr_val = tmr_val;
1977 return (1); /* not complete */
1980 /* compute all actual lengths */
1982 dwc_otg_standard_done(xfer);
1983 return (0); /* complete */
1987 dwc_otg_timer(void *_sc)
1989 struct dwc_otg_softc *sc = _sc;
1990 struct usb_xfer *xfer;
1991 struct dwc_otg_td *td;
1993 USB_BUS_LOCK_ASSERT(&sc->sc_bus, MA_OWNED);
1997 /* increment timer value */
2000 TAILQ_FOREACH(xfer, &sc->sc_bus.intr_q.head, wait_entry) {
2001 td = xfer->td_transfer_cache;
2007 dwc_otg_interrupt_poll(sc);
2009 if (sc->sc_timer_active) {
2011 usb_callout_reset(&sc->sc_timer,
2012 hz / (1000 / DWC_OTG_HOST_TIMER_RATE),
2013 &dwc_otg_timer, sc);
2018 dwc_otg_timer_start(struct dwc_otg_softc *sc)
2020 if (sc->sc_timer_active != 0)
2023 sc->sc_timer_active = 1;
2026 usb_callout_reset(&sc->sc_timer,
2027 hz / (1000 / DWC_OTG_HOST_TIMER_RATE),
2028 &dwc_otg_timer, sc);
2032 dwc_otg_timer_stop(struct dwc_otg_softc *sc)
2034 if (sc->sc_timer_active == 0)
2037 sc->sc_timer_active = 0;
2040 usb_callout_stop(&sc->sc_timer);
2044 dwc_otg_interrupt_poll(struct dwc_otg_softc *sc)
2046 struct usb_xfer *xfer;
2048 uint8_t got_rx_status;
2052 /* get all channel interrupts */
2053 for (x = 0; x != sc->sc_host_ch_max; x++) {
2054 temp = DWC_OTG_READ_4(sc, DOTG_HCINT(x));
2056 DWC_OTG_WRITE_4(sc, DOTG_HCINT(x), temp);
2057 temp &= ~HCINT_SOFTWARE_ONLY;
2058 sc->sc_chan_state[x].hcint |= temp;
2062 if (sc->sc_last_rx_status == 0) {
2064 temp = DWC_OTG_READ_4(sc, DOTG_GINTSTS);
2065 if (temp & GINTSTS_RXFLVL) {
2066 /* pop current status */
2067 sc->sc_last_rx_status =
2068 DWC_OTG_READ_4(sc, DOTG_GRXSTSPD);
2071 if (sc->sc_last_rx_status != 0) {
2075 temp = sc->sc_last_rx_status &
2076 GRXSTSRD_PKTSTS_MASK;
2078 /* non-data messages we simply skip */
2079 if (temp != GRXSTSRD_STP_DATA &&
2080 temp != GRXSTSRD_OUT_DATA) {
2081 dwc_otg_common_rx_ack(sc);
2085 temp = GRXSTSRD_BCNT_GET(
2086 sc->sc_last_rx_status);
2087 ep_no = GRXSTSRD_CHNUM_GET(
2088 sc->sc_last_rx_status);
2090 /* receive data, if any */
2092 DPRINTF("Reading %d bytes from ep %d\n", temp, ep_no);
2093 bus_space_read_region_4(sc->sc_io_tag, sc->sc_io_hdl,
2095 sc->sc_rx_bounce_buffer, (temp + 3) / 4);
2098 /* check if we should dump the data */
2099 if (!(sc->sc_active_rx_ep & (1U << ep_no))) {
2100 dwc_otg_common_rx_ack(sc);
2106 DPRINTFN(5, "RX status = 0x%08x: ch=%d pid=%d bytes=%d sts=%d\n",
2107 sc->sc_last_rx_status, ep_no,
2108 (sc->sc_last_rx_status >> 15) & 3,
2109 GRXSTSRD_BCNT_GET(sc->sc_last_rx_status),
2110 (sc->sc_last_rx_status >> 17) & 15);
2117 ep_no = GRXSTSRD_CHNUM_GET(
2118 sc->sc_last_rx_status);
2120 /* check if we should dump the data */
2121 if (!(sc->sc_active_rx_ep & (1U << ep_no))) {
2122 dwc_otg_common_rx_ack(sc);
2129 TAILQ_FOREACH(xfer, &sc->sc_bus.intr_q.head, wait_entry) {
2130 if (!dwc_otg_xfer_do_fifo(xfer)) {
2131 /* queue has been modified */
2136 if (got_rx_status) {
2137 /* check if data was consumed */
2138 if (sc->sc_last_rx_status == 0)
2141 /* disable RX FIFO level interrupt */
2142 sc->sc_irq_mask &= ~GINTSTS_RXFLVL;
2143 DWC_OTG_WRITE_4(sc, DOTG_GINTMSK, sc->sc_irq_mask);
2148 dwc_otg_vbus_interrupt(struct dwc_otg_softc *sc, uint8_t is_on)
2150 DPRINTFN(5, "vbus = %u\n", is_on);
2153 if (!sc->sc_flags.status_vbus) {
2154 sc->sc_flags.status_vbus = 1;
2156 /* complete root HUB interrupt endpoint */
2158 dwc_otg_root_intr(sc);
2161 if (sc->sc_flags.status_vbus) {
2162 sc->sc_flags.status_vbus = 0;
2163 sc->sc_flags.status_bus_reset = 0;
2164 sc->sc_flags.status_suspend = 0;
2165 sc->sc_flags.change_suspend = 0;
2166 sc->sc_flags.change_connect = 1;
2168 /* complete root HUB interrupt endpoint */
2170 dwc_otg_root_intr(sc);
2176 dwc_otg_interrupt(struct dwc_otg_softc *sc)
2180 USB_BUS_LOCK(&sc->sc_bus);
2182 /* read and clear interrupt status */
2183 status = DWC_OTG_READ_4(sc, DOTG_GINTSTS);
2184 DWC_OTG_WRITE_4(sc, DOTG_GINTSTS, status);
2186 DPRINTFN(14, "GINTSTS=0x%08x HAINT=0x%08x HFNUM=0x%08x\n",
2187 status, DWC_OTG_READ_4(sc, DOTG_HAINT),
2188 DWC_OTG_READ_4(sc, DOTG_HFNUM));
2190 if (status & GINTSTS_USBRST) {
2192 /* set correct state */
2193 sc->sc_flags.status_device_mode = 1;
2194 sc->sc_flags.status_bus_reset = 0;
2195 sc->sc_flags.status_suspend = 0;
2196 sc->sc_flags.change_suspend = 0;
2197 sc->sc_flags.change_connect = 1;
2199 /* complete root HUB interrupt endpoint */
2200 dwc_otg_root_intr(sc);
2203 /* check for any bus state change interrupts */
2204 if (status & GINTSTS_ENUMDONE) {
2208 DPRINTFN(5, "end of reset\n");
2210 /* set correct state */
2211 sc->sc_flags.status_device_mode = 1;
2212 sc->sc_flags.status_bus_reset = 1;
2213 sc->sc_flags.status_suspend = 0;
2214 sc->sc_flags.change_suspend = 0;
2215 sc->sc_flags.change_connect = 1;
2216 sc->sc_flags.status_low_speed = 0;
2217 sc->sc_flags.port_enabled = 1;
2220 dwc_otg_init_fifo(sc, DWC_MODE_DEVICE);
2222 /* reset function address */
2223 dwc_otg_set_address(sc, 0);
2225 /* figure out enumeration speed */
2226 temp = DWC_OTG_READ_4(sc, DOTG_DSTS);
2227 if (DSTS_ENUMSPD_GET(temp) == DSTS_ENUMSPD_HI)
2228 sc->sc_flags.status_high_speed = 1;
2230 sc->sc_flags.status_high_speed = 0;
2232 /* disable resume interrupt and enable suspend interrupt */
2234 sc->sc_irq_mask &= ~GINTSTS_WKUPINT;
2235 sc->sc_irq_mask |= GINTSTS_USBSUSP;
2236 DWC_OTG_WRITE_4(sc, DOTG_GINTMSK, sc->sc_irq_mask);
2238 /* complete root HUB interrupt endpoint */
2239 dwc_otg_root_intr(sc);
2242 if (status & GINTSTS_PRTINT) {
2245 hprt = DWC_OTG_READ_4(sc, DOTG_HPRT);
2247 /* clear change bits */
2248 DWC_OTG_WRITE_4(sc, DOTG_HPRT, (hprt & (
2249 HPRT_PRTPWR | HPRT_PRTENCHNG |
2250 HPRT_PRTCONNDET | HPRT_PRTOVRCURRCHNG)) |
2253 DPRINTFN(12, "GINTSTS=0x%08x, HPRT=0x%08x\n", status, hprt);
2255 sc->sc_flags.status_device_mode = 0;
2257 if (hprt & HPRT_PRTCONNSTS)
2258 sc->sc_flags.status_bus_reset = 1;
2260 sc->sc_flags.status_bus_reset = 0;
2262 if (hprt & HPRT_PRTENCHNG)
2263 sc->sc_flags.change_enabled = 1;
2265 if (hprt & HPRT_PRTENA)
2266 sc->sc_flags.port_enabled = 1;
2268 sc->sc_flags.port_enabled = 0;
2270 if (hprt & HPRT_PRTOVRCURRCHNG)
2271 sc->sc_flags.change_over_current = 1;
2273 if (hprt & HPRT_PRTOVRCURRACT)
2274 sc->sc_flags.port_over_current = 1;
2276 sc->sc_flags.port_over_current = 0;
2278 if (hprt & HPRT_PRTPWR)
2279 sc->sc_flags.port_powered = 1;
2281 sc->sc_flags.port_powered = 0;
2283 if (((hprt & HPRT_PRTSPD_MASK)
2284 >> HPRT_PRTSPD_SHIFT) == HPRT_PRTSPD_LOW)
2285 sc->sc_flags.status_low_speed = 1;
2287 sc->sc_flags.status_low_speed = 0;
2289 if (((hprt & HPRT_PRTSPD_MASK)
2290 >> HPRT_PRTSPD_SHIFT) == HPRT_PRTSPD_HIGH)
2291 sc->sc_flags.status_high_speed = 1;
2293 sc->sc_flags.status_high_speed = 0;
2295 if (hprt & HPRT_PRTCONNDET)
2296 sc->sc_flags.change_connect = 1;
2298 if (hprt & HPRT_PRTSUSP)
2299 dwc_otg_suspend_irq(sc);
2301 dwc_otg_resume_irq(sc);
2303 /* complete root HUB interrupt endpoint */
2304 dwc_otg_root_intr(sc);
2308 * If resume and suspend is set at the same time we interpret
2309 * that like RESUME. Resume is set when there is at least 3
2310 * milliseconds of inactivity on the USB BUS.
2312 if (status & GINTSTS_WKUPINT) {
2314 DPRINTFN(5, "resume interrupt\n");
2316 dwc_otg_resume_irq(sc);
2318 } else if (status & GINTSTS_USBSUSP) {
2320 DPRINTFN(5, "suspend interrupt\n");
2322 dwc_otg_suspend_irq(sc);
2325 if (status & (GINTSTS_USBSUSP |
2328 GINTSTS_SESSREQINT)) {
2331 temp = DWC_OTG_READ_4(sc, DOTG_GOTGCTL);
2333 DPRINTFN(5, "GOTGCTL=0x%08x\n", temp);
2335 dwc_otg_vbus_interrupt(sc,
2336 (temp & (GOTGCTL_ASESVLD | GOTGCTL_BSESVLD)) ? 1 : 0);
2339 /* clear all IN endpoint interrupts */
2340 if (status & GINTSTS_IEPINT) {
2344 for (x = 0; x != sc->sc_dev_in_ep_max; x++) {
2345 temp = DWC_OTG_READ_4(sc, DOTG_DIEPINT(x));
2346 if (temp & DIEPMSK_XFERCOMPLMSK) {
2347 DWC_OTG_WRITE_4(sc, DOTG_DIEPINT(x),
2348 DIEPMSK_XFERCOMPLMSK);
2353 /* check for SOF interrupt */
2354 if (status & GINTSTS_SOF) {
2355 if (sc->sc_irq_mask & GINTMSK_SOFMSK) {
2359 DPRINTFN(12, "SOF interrupt\n");
2361 for (x = y = 0; x != sc->sc_host_ch_max; x++) {
2362 if (sc->sc_chan_state[x].wait_sof != 0) {
2363 if (--(sc->sc_chan_state[x].wait_sof) != 0)
2368 sc->sc_irq_mask &= ~GINTMSK_SOFMSK;
2369 DWC_OTG_WRITE_4(sc, DOTG_GINTMSK, sc->sc_irq_mask);
2375 dwc_otg_interrupt_poll(sc);
2377 USB_BUS_UNLOCK(&sc->sc_bus);
2381 dwc_otg_setup_standard_chain_sub(struct dwc_otg_std_temp *temp)
2383 struct dwc_otg_td *td;
2385 /* get current Transfer Descriptor */
2389 /* prepare for next TD */
2390 temp->td_next = td->obj_next;
2392 /* fill out the Transfer Descriptor */
2393 td->func = temp->func;
2395 td->offset = temp->offset;
2396 td->remainder = temp->len;
2399 td->error_stall = 0;
2401 td->did_stall = temp->did_stall;
2402 td->short_pkt = temp->short_pkt;
2403 td->alt_next = temp->setup_alt_next;
2407 td->channel = DWC_OTG_MAX_CHANNELS;
2413 dwc_otg_setup_standard_chain(struct usb_xfer *xfer)
2415 struct dwc_otg_std_temp temp;
2416 struct dwc_otg_td *td;
2421 DPRINTFN(9, "addr=%d endpt=%d sumlen=%d speed=%d\n",
2422 xfer->address, UE_GET_ADDR(xfer->endpointno),
2423 xfer->sumlen, usbd_get_speed(xfer->xroot->udev));
2425 temp.max_frame_size = xfer->max_frame_size;
2427 td = xfer->td_start[0];
2428 xfer->td_transfer_first = td;
2429 xfer->td_transfer_cache = td;
2435 temp.td_next = xfer->td_start[0];
2437 temp.setup_alt_next = xfer->flags_int.short_frames_ok;
2438 temp.did_stall = !xfer->flags_int.control_stall;
2440 is_host = (xfer->xroot->udev->flags.usb_mode == USB_MODE_HOST);
2442 /* check if we should prepend a setup message */
2444 if (xfer->flags_int.control_xfr) {
2445 if (xfer->flags_int.control_hdr) {
2448 temp.func = &dwc_otg_host_setup_tx;
2450 temp.func = &dwc_otg_setup_rx;
2452 temp.len = xfer->frlengths[0];
2453 temp.pc = xfer->frbuffers + 0;
2454 temp.short_pkt = temp.len ? 1 : 0;
2456 /* check for last frame */
2457 if (xfer->nframes == 1) {
2458 /* no STATUS stage yet, SETUP is last */
2459 if (xfer->flags_int.control_act)
2460 temp.setup_alt_next = 0;
2463 dwc_otg_setup_standard_chain_sub(&temp);
2470 if (x != xfer->nframes) {
2471 if (xfer->endpointno & UE_DIR_IN) {
2473 temp.func = &dwc_otg_host_data_rx;
2476 temp.func = &dwc_otg_data_tx;
2481 temp.func = &dwc_otg_host_data_tx;
2484 temp.func = &dwc_otg_data_rx;
2489 /* setup "pc" pointer */
2490 temp.pc = xfer->frbuffers + x;
2494 while (x != xfer->nframes) {
2496 /* DATA0 / DATA1 message */
2498 temp.len = xfer->frlengths[x];
2502 if (x == xfer->nframes) {
2503 if (xfer->flags_int.control_xfr) {
2504 if (xfer->flags_int.control_act) {
2505 temp.setup_alt_next = 0;
2508 temp.setup_alt_next = 0;
2511 if (temp.len == 0) {
2513 /* make sure that we send an USB packet */
2519 /* regular data transfer */
2521 temp.short_pkt = (xfer->flags.force_short_xfer ? 0 : 1);
2524 dwc_otg_setup_standard_chain_sub(&temp);
2526 if (xfer->flags_int.isochronous_xfr) {
2527 temp.offset += temp.len;
2529 /* get next Page Cache pointer */
2530 temp.pc = xfer->frbuffers + x;
2534 if (xfer->flags_int.control_xfr) {
2536 /* always setup a valid "pc" pointer for status and sync */
2537 temp.pc = xfer->frbuffers + 0;
2540 temp.setup_alt_next = 0;
2542 /* check if we need to sync */
2544 /* we need a SYNC point after TX */
2545 temp.func = &dwc_otg_data_tx_sync;
2546 dwc_otg_setup_standard_chain_sub(&temp);
2549 /* check if we should append a status stage */
2550 if (!xfer->flags_int.control_act) {
2553 * Send a DATA1 message and invert the current
2554 * endpoint direction.
2556 if (xfer->endpointno & UE_DIR_IN) {
2558 temp.func = &dwc_otg_host_data_tx;
2561 temp.func = &dwc_otg_data_rx;
2566 temp.func = &dwc_otg_host_data_rx;
2569 temp.func = &dwc_otg_data_tx;
2574 dwc_otg_setup_standard_chain_sub(&temp);
2576 /* data toggle should be DATA1 */
2581 /* we need a SYNC point after TX */
2582 temp.func = &dwc_otg_data_tx_sync;
2583 dwc_otg_setup_standard_chain_sub(&temp);
2587 /* check if we need to sync */
2590 temp.pc = xfer->frbuffers + 0;
2593 temp.setup_alt_next = 0;
2595 /* we need a SYNC point after TX */
2596 temp.func = &dwc_otg_data_tx_sync;
2597 dwc_otg_setup_standard_chain_sub(&temp);
2601 /* must have at least one frame! */
2603 xfer->td_transfer_last = td;
2607 struct dwc_otg_softc *sc;
2612 sc = DWC_OTG_BUS2SC(xfer->xroot->bus);
2613 xfer_type = xfer->endpoint->edesc->bmAttributes & UE_XFERTYPE;
2615 /* get first again */
2616 td = xfer->td_transfer_first;
2617 td->toggle = (xfer->endpoint->toggle_next ? 1 : 0);
2620 (xfer->address << HCCHAR_DEVADDR_SHIFT) |
2621 (xfer_type << HCCHAR_EPTYPE_SHIFT) |
2622 ((xfer->endpointno & UE_ADDR) << HCCHAR_EPNUM_SHIFT) |
2623 (xfer->max_packet_size << HCCHAR_MPS_SHIFT) |
2626 if (usbd_get_speed(xfer->xroot->udev) == USB_SPEED_LOW)
2627 hcchar |= HCCHAR_LSPDDEV;
2628 if (UE_GET_DIR(xfer->endpointno) == UE_DIR_IN)
2629 hcchar |= HCCHAR_EPDIR_IN;
2631 switch (xfer->xroot->udev->speed) {
2632 case USB_SPEED_FULL:
2634 /* check if root HUB port is running High Speed */
2635 if (sc->sc_flags.status_high_speed != 0) {
2636 hcsplt = HCSPLT_SPLTENA |
2637 (xfer->xroot->udev->hs_port_no <<
2638 HCSPLT_PRTADDR_SHIFT) |
2639 (xfer->xroot->udev->hs_hub_addr <<
2640 HCSPLT_HUBADDR_SHIFT);
2641 if (xfer_type == UE_ISOCHRONOUS) /* XXX */
2642 hcsplt |= (3 << HCSPLT_XACTPOS_SHIFT);
2646 if (xfer_type == UE_INTERRUPT) {
2648 ival = xfer->interval / DWC_OTG_HOST_TIMER_RATE;
2651 else if (ival > 127)
2653 td->tmr_val = sc->sc_tmr_val + ival;
2657 case USB_SPEED_HIGH:
2659 if (xfer_type == UE_ISOCHRONOUS ||
2660 xfer_type == UE_INTERRUPT) {
2661 hcchar |= ((xfer->max_packet_count & 3)
2662 << HCCHAR_MC_SHIFT);
2664 if (xfer_type == UE_INTERRUPT) {
2666 ival = xfer->interval / DWC_OTG_HOST_TIMER_RATE;
2669 else if (ival > 127)
2671 td->tmr_val = sc->sc_tmr_val + ival;
2680 if (xfer_type == UE_ISOCHRONOUS) {
2681 td->tmr_val = xfer->endpoint->isoc_next & 0xFF;
2682 td->tmr_res = 1 << usbd_xfer_get_fps_shift(xfer);
2683 } else if (xfer_type != UE_INTERRUPT) {
2688 /* store configuration in all TD's */
2690 td->hcchar = hcchar;
2691 td->hcsplt = hcsplt;
2693 if (((void *)td) == xfer->td_transfer_last)
2702 dwc_otg_timeout(void *arg)
2704 struct usb_xfer *xfer = arg;
2706 DPRINTF("xfer=%p\n", xfer);
2708 USB_BUS_LOCK_ASSERT(xfer->xroot->bus, MA_OWNED);
2710 /* transfer is transferred */
2711 dwc_otg_device_done(xfer, USB_ERR_TIMEOUT);
2715 dwc_otg_start_standard_chain(struct usb_xfer *xfer)
2719 /* poll one time - will turn on interrupts */
2720 if (dwc_otg_xfer_do_fifo(xfer)) {
2722 /* put transfer on interrupt queue */
2723 usbd_transfer_enqueue(&xfer->xroot->bus->intr_q, xfer);
2725 /* start timeout, if any */
2726 if (xfer->timeout != 0) {
2727 usbd_transfer_timeout_ms(xfer,
2728 &dwc_otg_timeout, xfer->timeout);
2734 dwc_otg_root_intr(struct dwc_otg_softc *sc)
2738 USB_BUS_LOCK_ASSERT(&sc->sc_bus, MA_OWNED);
2741 sc->sc_hub_idata[0] = 0x02; /* we only have one port */
2743 uhub_root_intr(&sc->sc_bus, sc->sc_hub_idata,
2744 sizeof(sc->sc_hub_idata));
2748 dwc_otg_standard_done_sub(struct usb_xfer *xfer)
2750 struct dwc_otg_td *td;
2756 td = xfer->td_transfer_cache;
2759 len = td->remainder;
2761 /* store last data toggle */
2762 xfer->endpoint->toggle_next = td->toggle;
2764 if (xfer->aframes != xfer->nframes) {
2766 * Verify the length and subtract
2767 * the remainder from "frlengths[]":
2769 if (len > xfer->frlengths[xfer->aframes]) {
2772 xfer->frlengths[xfer->aframes] -= len;
2775 /* Check for transfer error */
2776 if (td->error_any) {
2777 /* the transfer is finished */
2778 error = (td->error_stall ?
2779 USB_ERR_STALLED : USB_ERR_IOERROR);
2783 /* Check for short transfer */
2785 if (xfer->flags_int.short_frames_ok) {
2786 /* follow alt next */
2793 /* the transfer is finished */
2801 /* this USB frame is complete */
2807 /* update transfer cache */
2809 xfer->td_transfer_cache = td;
2815 dwc_otg_standard_done(struct usb_xfer *xfer)
2817 usb_error_t err = 0;
2819 DPRINTFN(13, "xfer=%p endpoint=%p transfer done\n",
2820 xfer, xfer->endpoint);
2824 xfer->td_transfer_cache = xfer->td_transfer_first;
2826 if (xfer->flags_int.control_xfr) {
2828 if (xfer->flags_int.control_hdr) {
2830 err = dwc_otg_standard_done_sub(xfer);
2834 if (xfer->td_transfer_cache == NULL) {
2838 while (xfer->aframes != xfer->nframes) {
2840 err = dwc_otg_standard_done_sub(xfer);
2843 if (xfer->td_transfer_cache == NULL) {
2848 if (xfer->flags_int.control_xfr &&
2849 !xfer->flags_int.control_act) {
2851 err = dwc_otg_standard_done_sub(xfer);
2854 dwc_otg_device_done(xfer, err);
2857 /*------------------------------------------------------------------------*
2858 * dwc_otg_device_done
2860 * NOTE: this function can be called more than one time on the
2861 * same USB transfer!
2862 *------------------------------------------------------------------------*/
2864 dwc_otg_device_done(struct usb_xfer *xfer, usb_error_t error)
2866 DPRINTFN(9, "xfer=%p, endpoint=%p, error=%d\n",
2867 xfer, xfer->endpoint, error);
2869 if (xfer->flags_int.usb_mode == USB_MODE_DEVICE) {
2870 DPRINTFN(15, "disabled interrupts!\n");
2872 struct dwc_otg_td *td;
2874 td = xfer->td_transfer_first;
2877 dwc_otg_host_channel_free(td);
2879 /* dequeue transfer and start next transfer */
2880 usbd_transfer_done(xfer, error);
2884 dwc_otg_xfer_stall(struct usb_xfer *xfer)
2886 dwc_otg_device_done(xfer, USB_ERR_STALLED);
2890 dwc_otg_set_stall(struct usb_device *udev,
2891 struct usb_endpoint *ep, uint8_t *did_stall)
2893 struct dwc_otg_softc *sc;
2898 USB_BUS_LOCK_ASSERT(udev->bus, MA_OWNED);
2901 if (udev->flags.usb_mode != USB_MODE_DEVICE) {
2906 sc = DWC_OTG_BUS2SC(udev->bus);
2908 /* get endpoint address */
2909 ep_no = ep->edesc->bEndpointAddress;
2911 DPRINTFN(5, "endpoint=0x%x\n", ep_no);
2913 if (ep_no & UE_DIR_IN) {
2914 reg = DOTG_DIEPCTL(ep_no & UE_ADDR);
2915 temp = sc->sc_in_ctl[ep_no & UE_ADDR];
2917 reg = DOTG_DOEPCTL(ep_no & UE_ADDR);
2918 temp = sc->sc_out_ctl[ep_no & UE_ADDR];
2921 /* disable and stall endpoint */
2922 DWC_OTG_WRITE_4(sc, reg, temp | DOEPCTL_EPDIS);
2923 DWC_OTG_WRITE_4(sc, reg, temp | DOEPCTL_STALL);
2925 /* clear active OUT ep */
2926 if (!(ep_no & UE_DIR_IN)) {
2928 sc->sc_active_rx_ep &= ~(1U << (ep_no & UE_ADDR));
2930 if (sc->sc_last_rx_status != 0 &&
2931 (ep_no & UE_ADDR) == GRXSTSRD_CHNUM_GET(
2932 sc->sc_last_rx_status)) {
2934 dwc_otg_common_rx_ack(sc);
2935 /* poll interrupt */
2936 dwc_otg_interrupt_poll(sc);
2942 dwc_otg_clear_stall_sub(struct dwc_otg_softc *sc, uint32_t mps,
2943 uint8_t ep_no, uint8_t ep_type, uint8_t ep_dir)
2948 if (ep_type == UE_CONTROL) {
2949 /* clearing stall is not needed */
2954 reg = DOTG_DIEPCTL(ep_no);
2956 reg = DOTG_DOEPCTL(ep_no);
2957 sc->sc_active_rx_ep |= (1U << ep_no);
2960 /* round up and mask away the multiplier count */
2961 mps = (mps + 3) & 0x7FC;
2963 if (ep_type == UE_BULK) {
2964 temp = DIEPCTL_EPTYPE_SET(
2965 DIEPCTL_EPTYPE_BULK) |
2967 } else if (ep_type == UE_INTERRUPT) {
2968 temp = DIEPCTL_EPTYPE_SET(
2969 DIEPCTL_EPTYPE_INTERRUPT) |
2972 temp = DIEPCTL_EPTYPE_SET(
2973 DIEPCTL_EPTYPE_ISOC) |
2977 temp |= DIEPCTL_MPS_SET(mps);
2978 temp |= DIEPCTL_TXFNUM_SET(ep_no);
2981 sc->sc_in_ctl[ep_no] = temp;
2983 sc->sc_out_ctl[ep_no] = temp;
2985 DWC_OTG_WRITE_4(sc, reg, temp | DOEPCTL_EPDIS);
2986 DWC_OTG_WRITE_4(sc, reg, temp | DOEPCTL_SETD0PID);
2987 DWC_OTG_WRITE_4(sc, reg, temp | DIEPCTL_SNAK);
2989 /* we only reset the transmit FIFO */
2991 DWC_OTG_WRITE_4(sc, DOTG_GRSTCTL,
2992 GRSTCTL_TXFIFO(ep_no) |
2996 DOTG_DIEPTSIZ(ep_no), 0);
2999 /* poll interrupt */
3000 dwc_otg_interrupt_poll(sc);
3004 dwc_otg_clear_stall(struct usb_device *udev, struct usb_endpoint *ep)
3006 struct dwc_otg_softc *sc;
3007 struct usb_endpoint_descriptor *ed;
3009 DPRINTFN(5, "endpoint=%p\n", ep);
3011 USB_BUS_LOCK_ASSERT(udev->bus, MA_OWNED);
3014 if (udev->flags.usb_mode != USB_MODE_DEVICE) {
3019 sc = DWC_OTG_BUS2SC(udev->bus);
3021 /* get endpoint descriptor */
3024 /* reset endpoint */
3025 dwc_otg_clear_stall_sub(sc,
3026 UGETW(ed->wMaxPacketSize),
3027 (ed->bEndpointAddress & UE_ADDR),
3028 (ed->bmAttributes & UE_XFERTYPE),
3029 (ed->bEndpointAddress & (UE_DIR_IN | UE_DIR_OUT)));
3033 dwc_otg_device_state_change(struct usb_device *udev)
3035 struct dwc_otg_softc *sc;
3039 if (udev->flags.usb_mode != USB_MODE_DEVICE) {
3045 sc = DWC_OTG_BUS2SC(udev->bus);
3047 /* deactivate all other endpoint but the control endpoint */
3048 if (udev->state == USB_STATE_CONFIGURED ||
3049 udev->state == USB_STATE_ADDRESSED) {
3051 USB_BUS_LOCK(&sc->sc_bus);
3053 for (x = 1; x != sc->sc_dev_ep_max; x++) {
3055 if (x < sc->sc_dev_in_ep_max) {
3056 DWC_OTG_WRITE_4(sc, DOTG_DIEPCTL(x),
3058 DWC_OTG_WRITE_4(sc, DOTG_DIEPCTL(x), 0);
3061 DWC_OTG_WRITE_4(sc, DOTG_DOEPCTL(x),
3063 DWC_OTG_WRITE_4(sc, DOTG_DOEPCTL(x), 0);
3065 USB_BUS_UNLOCK(&sc->sc_bus);
3070 dwc_otg_init(struct dwc_otg_softc *sc)
3076 /* set up the bus structure */
3077 sc->sc_bus.usbrev = USB_REV_2_0;
3078 sc->sc_bus.methods = &dwc_otg_bus_methods;
3080 usb_callout_init_mtx(&sc->sc_timer,
3081 &sc->sc_bus.bus_mtx, 0);
3083 USB_BUS_LOCK(&sc->sc_bus);
3085 /* turn on clocks */
3086 dwc_otg_clocks_on(sc);
3088 temp = DWC_OTG_READ_4(sc, DOTG_GSNPSID);
3089 DPRINTF("Version = 0x%08x\n", temp);
3092 DWC_OTG_WRITE_4(sc, DOTG_DCTL,
3095 /* wait for host to detect disconnect */
3096 usb_pause_mtx(&sc->sc_bus.bus_mtx, hz / 32);
3098 DWC_OTG_WRITE_4(sc, DOTG_GRSTCTL,
3101 /* wait a little bit for block to reset */
3102 usb_pause_mtx(&sc->sc_bus.bus_mtx, hz / 128);
3104 switch (sc->sc_mode) {
3105 case DWC_MODE_DEVICE:
3106 temp = GUSBCFG_FORCEDEVMODE;
3109 temp = GUSBCFG_FORCEHOSTMODE;
3116 /* select HSIC or non-HSIC mode */
3117 if (dwc_otg_use_hsic) {
3118 DWC_OTG_WRITE_4(sc, DOTG_GUSBCFG,
3120 GUSBCFG_TRD_TIM_SET(5) | temp);
3121 DWC_OTG_WRITE_4(sc, DOTG_GOTGCTL,
3124 temp = DWC_OTG_READ_4(sc, DOTG_GLPMCFG);
3125 DWC_OTG_WRITE_4(sc, DOTG_GLPMCFG,
3126 temp & ~GLPMCFG_HSIC_CONN);
3127 DWC_OTG_WRITE_4(sc, DOTG_GLPMCFG,
3128 temp | GLPMCFG_HSIC_CONN);
3130 DWC_OTG_WRITE_4(sc, DOTG_GUSBCFG,
3131 GUSBCFG_ULPI_UTMI_SEL |
3132 GUSBCFG_TRD_TIM_SET(5) | temp);
3133 DWC_OTG_WRITE_4(sc, DOTG_GOTGCTL, 0);
3135 temp = DWC_OTG_READ_4(sc, DOTG_GLPMCFG);
3136 DWC_OTG_WRITE_4(sc, DOTG_GLPMCFG,
3137 temp & ~GLPMCFG_HSIC_CONN);
3140 /* clear global nak */
3141 DWC_OTG_WRITE_4(sc, DOTG_DCTL,
3145 /* disable USB port */
3146 DWC_OTG_WRITE_4(sc, DOTG_PCGCCTL, 0xFFFFFFFF);
3149 usb_pause_mtx(&sc->sc_bus.bus_mtx, hz / 100);
3151 /* enable USB port */
3152 DWC_OTG_WRITE_4(sc, DOTG_PCGCCTL, 0);
3155 usb_pause_mtx(&sc->sc_bus.bus_mtx, hz / 100);
3158 dwc_otg_pull_up(sc);
3160 temp = DWC_OTG_READ_4(sc, DOTG_GHWCFG3);
3162 sc->sc_fifo_size = 4 * GHWCFG3_DFIFODEPTH_GET(temp);
3164 temp = DWC_OTG_READ_4(sc, DOTG_GHWCFG2);
3166 sc->sc_dev_ep_max = GHWCFG2_NUMDEVEPS_GET(temp);
3168 if (sc->sc_dev_ep_max > DWC_OTG_MAX_ENDPOINTS)
3169 sc->sc_dev_ep_max = DWC_OTG_MAX_ENDPOINTS;
3171 sc->sc_host_ch_max = GHWCFG2_NUMHSTCHNL_GET(temp);
3173 if (sc->sc_host_ch_max > DWC_OTG_MAX_CHANNELS)
3174 sc->sc_host_ch_max = DWC_OTG_MAX_CHANNELS;
3176 temp = DWC_OTG_READ_4(sc, DOTG_GHWCFG4);
3178 sc->sc_dev_in_ep_max = GHWCFG4_NUM_IN_EP_GET(temp);
3180 DPRINTF("Total FIFO size = %d bytes, Device EPs = %d/%d Host CHs = %d\n",
3181 sc->sc_fifo_size, sc->sc_dev_ep_max, sc->sc_dev_in_ep_max,
3182 sc->sc_host_ch_max);
3185 if (dwc_otg_init_fifo(sc, DWC_MODE_OTG))
3188 /* enable interrupts */
3189 sc->sc_irq_mask = DWC_OTG_MSK_GINT_ENABLED;
3190 DWC_OTG_WRITE_4(sc, DOTG_GINTMSK, sc->sc_irq_mask);
3192 if (sc->sc_mode == DWC_MODE_OTG || sc->sc_mode == DWC_MODE_DEVICE) {
3194 /* enable all endpoint interrupts */
3195 temp = DWC_OTG_READ_4(sc, DOTG_GHWCFG2);
3196 if (temp & GHWCFG2_MPI) {
3199 DPRINTF("Multi Process Interrupts\n");
3201 for (x = 0; x != sc->sc_dev_in_ep_max; x++) {
3202 DWC_OTG_WRITE_4(sc, DOTG_DIEPEACHINTMSK(x),
3203 DIEPMSK_XFERCOMPLMSK);
3204 DWC_OTG_WRITE_4(sc, DOTG_DOEPEACHINTMSK(x), 0);
3206 DWC_OTG_WRITE_4(sc, DOTG_DEACHINTMSK, 0xFFFF);
3208 DWC_OTG_WRITE_4(sc, DOTG_DIEPMSK,
3209 DIEPMSK_XFERCOMPLMSK);
3210 DWC_OTG_WRITE_4(sc, DOTG_DOEPMSK, 0);
3211 DWC_OTG_WRITE_4(sc, DOTG_DAINTMSK, 0xFFFF);
3215 if (sc->sc_mode == DWC_MODE_OTG || sc->sc_mode == DWC_MODE_HOST) {
3217 temp = DWC_OTG_READ_4(sc, DOTG_HCFG);
3218 temp &= ~(HCFG_FSLSSUPP | HCFG_FSLSPCLKSEL_MASK);
3219 temp |= (1 << HCFG_FSLSPCLKSEL_SHIFT);
3220 DWC_OTG_WRITE_4(sc, DOTG_HCFG, temp);
3223 /* only enable global IRQ */
3224 DWC_OTG_WRITE_4(sc, DOTG_GAHBCFG,
3225 GAHBCFG_GLBLINTRMSK);
3227 /* turn off clocks */
3228 dwc_otg_clocks_off(sc);
3230 /* read initial VBUS state */
3232 temp = DWC_OTG_READ_4(sc, DOTG_GOTGCTL);
3234 DPRINTFN(5, "GOTCTL=0x%08x\n", temp);
3236 dwc_otg_vbus_interrupt(sc,
3237 (temp & (GOTGCTL_ASESVLD | GOTGCTL_BSESVLD)) ? 1 : 0);
3239 USB_BUS_UNLOCK(&sc->sc_bus);
3241 /* catch any lost interrupts */
3243 dwc_otg_do_poll(&sc->sc_bus);
3245 return (0); /* success */
3249 dwc_otg_uninit(struct dwc_otg_softc *sc)
3251 USB_BUS_LOCK(&sc->sc_bus);
3253 /* stop host timer */
3254 dwc_otg_timer_stop(sc);
3256 /* set disconnect */
3257 DWC_OTG_WRITE_4(sc, DOTG_DCTL,
3260 /* turn off global IRQ */
3261 DWC_OTG_WRITE_4(sc, DOTG_GAHBCFG, 0);
3263 sc->sc_flags.port_enabled = 0;
3264 sc->sc_flags.port_powered = 0;
3265 sc->sc_flags.status_vbus = 0;
3266 sc->sc_flags.status_bus_reset = 0;
3267 sc->sc_flags.status_suspend = 0;
3268 sc->sc_flags.change_suspend = 0;
3269 sc->sc_flags.change_connect = 1;
3271 dwc_otg_pull_down(sc);
3272 dwc_otg_clocks_off(sc);
3274 USB_BUS_UNLOCK(&sc->sc_bus);
3276 usb_callout_drain(&sc->sc_timer);
3280 dwc_otg_suspend(struct dwc_otg_softc *sc)
3286 dwc_otg_resume(struct dwc_otg_softc *sc)
3292 dwc_otg_do_poll(struct usb_bus *bus)
3294 struct dwc_otg_softc *sc = DWC_OTG_BUS2SC(bus);
3296 USB_BUS_LOCK(&sc->sc_bus);
3297 dwc_otg_interrupt_poll(sc);
3298 USB_BUS_UNLOCK(&sc->sc_bus);
3301 /*------------------------------------------------------------------------*
3302 * DWC OTG bulk support
3303 * DWC OTG control support
3304 * DWC OTG interrupt support
3305 *------------------------------------------------------------------------*/
3307 dwc_otg_device_non_isoc_open(struct usb_xfer *xfer)
3312 dwc_otg_device_non_isoc_close(struct usb_xfer *xfer)
3314 dwc_otg_device_done(xfer, USB_ERR_CANCELLED);
3318 dwc_otg_device_non_isoc_enter(struct usb_xfer *xfer)
3323 dwc_otg_device_non_isoc_start(struct usb_xfer *xfer)
3326 dwc_otg_setup_standard_chain(xfer);
3327 dwc_otg_start_standard_chain(xfer);
3330 struct usb_pipe_methods dwc_otg_device_non_isoc_methods =
3332 .open = dwc_otg_device_non_isoc_open,
3333 .close = dwc_otg_device_non_isoc_close,
3334 .enter = dwc_otg_device_non_isoc_enter,
3335 .start = dwc_otg_device_non_isoc_start,
3338 /*------------------------------------------------------------------------*
3339 * DWC OTG full speed isochronous support
3340 *------------------------------------------------------------------------*/
3342 dwc_otg_device_isoc_open(struct usb_xfer *xfer)
3347 dwc_otg_device_isoc_close(struct usb_xfer *xfer)
3349 dwc_otg_device_done(xfer, USB_ERR_CANCELLED);
3353 dwc_otg_device_isoc_enter(struct usb_xfer *xfer)
3355 struct dwc_otg_softc *sc = DWC_OTG_BUS2SC(xfer->xroot->bus);
3358 uint8_t shift = usbd_xfer_get_fps_shift(xfer);
3360 DPRINTFN(6, "xfer=%p next=%d nframes=%d\n",
3361 xfer, xfer->endpoint->isoc_next, xfer->nframes);
3363 if (xfer->xroot->udev->flags.usb_mode == USB_MODE_HOST) {
3364 temp = DWC_OTG_READ_4(sc, DOTG_HFNUM);
3366 /* get the current frame index */
3367 nframes = (temp & HFNUM_FRNUM_MASK);
3369 temp = DWC_OTG_READ_4(sc, DOTG_DSTS);
3371 /* get the current frame index */
3372 nframes = DSTS_SOFFN_GET(temp);
3375 if (sc->sc_flags.status_high_speed)
3378 nframes &= DWC_OTG_FRAME_MASK;
3381 * check if the frame index is within the window where the frames
3384 temp = (nframes - xfer->endpoint->isoc_next) & DWC_OTG_FRAME_MASK;
3386 if ((xfer->endpoint->is_synced == 0) ||
3387 (temp < (((xfer->nframes << shift) + 7) / 8))) {
3389 * If there is data underflow or the pipe queue is
3390 * empty we schedule the transfer a few frames ahead
3391 * of the current frame position. Else two isochronous
3392 * transfers might overlap.
3394 xfer->endpoint->isoc_next = (nframes + 3) & DWC_OTG_FRAME_MASK;
3395 xfer->endpoint->is_synced = 1;
3396 DPRINTFN(3, "start next=%d\n", xfer->endpoint->isoc_next);
3399 * compute how many milliseconds the insertion is ahead of the
3400 * current frame position:
3402 temp = (xfer->endpoint->isoc_next - nframes) & DWC_OTG_FRAME_MASK;
3405 * pre-compute when the isochronous transfer will be finished:
3407 xfer->isoc_time_complete =
3408 usb_isoc_time_expand(&sc->sc_bus, nframes) + temp +
3409 (((xfer->nframes << shift) + 7) / 8);
3412 dwc_otg_setup_standard_chain(xfer);
3414 /* compute frame number for next insertion */
3415 xfer->endpoint->isoc_next += (xfer->nframes << shift);
3419 dwc_otg_device_isoc_start(struct usb_xfer *xfer)
3421 /* start TD chain */
3422 dwc_otg_start_standard_chain(xfer);
3425 struct usb_pipe_methods dwc_otg_device_isoc_methods =
3427 .open = dwc_otg_device_isoc_open,
3428 .close = dwc_otg_device_isoc_close,
3429 .enter = dwc_otg_device_isoc_enter,
3430 .start = dwc_otg_device_isoc_start,
3433 /*------------------------------------------------------------------------*
3434 * DWC OTG root control support
3435 *------------------------------------------------------------------------*
3436 * Simulate a hardware HUB by handling all the necessary requests.
3437 *------------------------------------------------------------------------*/
3439 static const struct usb_device_descriptor dwc_otg_devd = {
3440 .bLength = sizeof(struct usb_device_descriptor),
3441 .bDescriptorType = UDESC_DEVICE,
3442 .bcdUSB = {0x00, 0x02},
3443 .bDeviceClass = UDCLASS_HUB,
3444 .bDeviceSubClass = UDSUBCLASS_HUB,
3445 .bDeviceProtocol = UDPROTO_HSHUBSTT,
3446 .bMaxPacketSize = 64,
3447 .bcdDevice = {0x00, 0x01},
3450 .bNumConfigurations = 1,
3453 static const struct dwc_otg_config_desc dwc_otg_confd = {
3455 .bLength = sizeof(struct usb_config_descriptor),
3456 .bDescriptorType = UDESC_CONFIG,
3457 .wTotalLength[0] = sizeof(dwc_otg_confd),
3459 .bConfigurationValue = 1,
3460 .iConfiguration = 0,
3461 .bmAttributes = UC_SELF_POWERED,
3465 .bLength = sizeof(struct usb_interface_descriptor),
3466 .bDescriptorType = UDESC_INTERFACE,
3468 .bInterfaceClass = UICLASS_HUB,
3469 .bInterfaceSubClass = UISUBCLASS_HUB,
3470 .bInterfaceProtocol = 0,
3473 .bLength = sizeof(struct usb_endpoint_descriptor),
3474 .bDescriptorType = UDESC_ENDPOINT,
3475 .bEndpointAddress = (UE_DIR_IN | DWC_OTG_INTR_ENDPT),
3476 .bmAttributes = UE_INTERRUPT,
3477 .wMaxPacketSize[0] = 8,
3482 #define HSETW(ptr, val) ptr = { (uint8_t)(val), (uint8_t)((val) >> 8) }
3484 static const struct usb_hub_descriptor_min dwc_otg_hubd = {
3485 .bDescLength = sizeof(dwc_otg_hubd),
3486 .bDescriptorType = UDESC_HUB,
3488 HSETW(.wHubCharacteristics, (UHD_PWR_NO_SWITCH | UHD_OC_INDIVIDUAL)),
3489 .bPwrOn2PwrGood = 50,
3490 .bHubContrCurrent = 0,
3491 .DeviceRemovable = {0}, /* port is removable */
3494 #define STRING_VENDOR \
3497 #define STRING_PRODUCT \
3498 "O\0T\0G\0 \0R\0o\0o\0t\0 \0H\0U\0B"
3500 USB_MAKE_STRING_DESC(STRING_VENDOR, dwc_otg_vendor);
3501 USB_MAKE_STRING_DESC(STRING_PRODUCT, dwc_otg_product);
3504 dwc_otg_roothub_exec(struct usb_device *udev,
3505 struct usb_device_request *req, const void **pptr, uint16_t *plength)
3507 struct dwc_otg_softc *sc = DWC_OTG_BUS2SC(udev->bus);
3514 USB_BUS_LOCK_ASSERT(&sc->sc_bus, MA_OWNED);
3517 ptr = (const void *)&sc->sc_hub_temp;
3521 value = UGETW(req->wValue);
3522 index = UGETW(req->wIndex);
3524 /* demultiplex the control request */
3526 switch (req->bmRequestType) {
3527 case UT_READ_DEVICE:
3528 switch (req->bRequest) {
3529 case UR_GET_DESCRIPTOR:
3530 goto tr_handle_get_descriptor;
3532 goto tr_handle_get_config;
3534 goto tr_handle_get_status;
3540 case UT_WRITE_DEVICE:
3541 switch (req->bRequest) {
3542 case UR_SET_ADDRESS:
3543 goto tr_handle_set_address;
3545 goto tr_handle_set_config;
3546 case UR_CLEAR_FEATURE:
3547 goto tr_valid; /* nop */
3548 case UR_SET_DESCRIPTOR:
3549 goto tr_valid; /* nop */
3550 case UR_SET_FEATURE:
3556 case UT_WRITE_ENDPOINT:
3557 switch (req->bRequest) {
3558 case UR_CLEAR_FEATURE:
3559 switch (UGETW(req->wValue)) {
3560 case UF_ENDPOINT_HALT:
3561 goto tr_handle_clear_halt;
3562 case UF_DEVICE_REMOTE_WAKEUP:
3563 goto tr_handle_clear_wakeup;
3568 case UR_SET_FEATURE:
3569 switch (UGETW(req->wValue)) {
3570 case UF_ENDPOINT_HALT:
3571 goto tr_handle_set_halt;
3572 case UF_DEVICE_REMOTE_WAKEUP:
3573 goto tr_handle_set_wakeup;
3578 case UR_SYNCH_FRAME:
3579 goto tr_valid; /* nop */
3585 case UT_READ_ENDPOINT:
3586 switch (req->bRequest) {
3588 goto tr_handle_get_ep_status;
3594 case UT_WRITE_INTERFACE:
3595 switch (req->bRequest) {
3596 case UR_SET_INTERFACE:
3597 goto tr_handle_set_interface;
3598 case UR_CLEAR_FEATURE:
3599 goto tr_valid; /* nop */
3600 case UR_SET_FEATURE:
3606 case UT_READ_INTERFACE:
3607 switch (req->bRequest) {
3608 case UR_GET_INTERFACE:
3609 goto tr_handle_get_interface;
3611 goto tr_handle_get_iface_status;
3617 case UT_WRITE_CLASS_INTERFACE:
3618 case UT_WRITE_VENDOR_INTERFACE:
3622 case UT_READ_CLASS_INTERFACE:
3623 case UT_READ_VENDOR_INTERFACE:
3627 case UT_WRITE_CLASS_DEVICE:
3628 switch (req->bRequest) {
3629 case UR_CLEAR_FEATURE:
3631 case UR_SET_DESCRIPTOR:
3632 case UR_SET_FEATURE:
3639 case UT_WRITE_CLASS_OTHER:
3640 switch (req->bRequest) {
3641 case UR_CLEAR_FEATURE:
3642 goto tr_handle_clear_port_feature;
3643 case UR_SET_FEATURE:
3644 goto tr_handle_set_port_feature;
3645 case UR_CLEAR_TT_BUFFER:
3655 case UT_READ_CLASS_OTHER:
3656 switch (req->bRequest) {
3657 case UR_GET_TT_STATE:
3658 goto tr_handle_get_tt_state;
3660 goto tr_handle_get_port_status;
3666 case UT_READ_CLASS_DEVICE:
3667 switch (req->bRequest) {
3668 case UR_GET_DESCRIPTOR:
3669 goto tr_handle_get_class_descriptor;
3671 goto tr_handle_get_class_status;
3682 tr_handle_get_descriptor:
3683 switch (value >> 8) {
3688 len = sizeof(dwc_otg_devd);
3689 ptr = (const void *)&dwc_otg_devd;
3695 len = sizeof(dwc_otg_confd);
3696 ptr = (const void *)&dwc_otg_confd;
3699 switch (value & 0xff) {
3700 case 0: /* Language table */
3701 len = sizeof(usb_string_lang_en);
3702 ptr = (const void *)&usb_string_lang_en;
3705 case 1: /* Vendor */
3706 len = sizeof(dwc_otg_vendor);
3707 ptr = (const void *)&dwc_otg_vendor;
3710 case 2: /* Product */
3711 len = sizeof(dwc_otg_product);
3712 ptr = (const void *)&dwc_otg_product;
3723 tr_handle_get_config:
3725 sc->sc_hub_temp.wValue[0] = sc->sc_conf;
3728 tr_handle_get_status:
3730 USETW(sc->sc_hub_temp.wValue, UDS_SELF_POWERED);
3733 tr_handle_set_address:
3734 if (value & 0xFF00) {
3737 sc->sc_rt_addr = value;
3740 tr_handle_set_config:
3744 sc->sc_conf = value;
3747 tr_handle_get_interface:
3749 sc->sc_hub_temp.wValue[0] = 0;
3752 tr_handle_get_tt_state:
3753 tr_handle_get_class_status:
3754 tr_handle_get_iface_status:
3755 tr_handle_get_ep_status:
3757 USETW(sc->sc_hub_temp.wValue, 0);
3761 tr_handle_set_interface:
3762 tr_handle_set_wakeup:
3763 tr_handle_clear_wakeup:
3764 tr_handle_clear_halt:
3767 tr_handle_clear_port_feature:
3771 DPRINTFN(9, "UR_CLEAR_PORT_FEATURE on port %d\n", index);
3774 case UHF_PORT_SUSPEND:
3775 dwc_otg_wakeup_peer(sc);
3778 case UHF_PORT_ENABLE:
3779 if (sc->sc_flags.status_device_mode == 0) {
3780 DWC_OTG_WRITE_4(sc, DOTG_HPRT,
3781 sc->sc_hprt_val | HPRT_PRTENA);
3783 sc->sc_flags.port_enabled = 0;
3786 case UHF_C_PORT_RESET:
3787 sc->sc_flags.change_reset = 0;
3790 case UHF_C_PORT_ENABLE:
3791 sc->sc_flags.change_enabled = 0;
3794 case UHF_C_PORT_OVER_CURRENT:
3795 sc->sc_flags.change_over_current = 0;
3799 case UHF_PORT_INDICATOR:
3803 case UHF_PORT_POWER:
3804 sc->sc_flags.port_powered = 0;
3805 if (sc->sc_mode == DWC_MODE_HOST || sc->sc_mode == DWC_MODE_OTG) {
3806 sc->sc_hprt_val = 0;
3807 DWC_OTG_WRITE_4(sc, DOTG_HPRT, HPRT_PRTENA);
3809 dwc_otg_pull_down(sc);
3810 dwc_otg_clocks_off(sc);
3813 case UHF_C_PORT_CONNECTION:
3814 /* clear connect change flag */
3815 sc->sc_flags.change_connect = 0;
3818 case UHF_C_PORT_SUSPEND:
3819 sc->sc_flags.change_suspend = 0;
3823 err = USB_ERR_IOERROR;
3828 tr_handle_set_port_feature:
3832 DPRINTFN(9, "UR_SET_PORT_FEATURE\n");
3835 case UHF_PORT_ENABLE:
3838 case UHF_PORT_SUSPEND:
3839 if (sc->sc_flags.status_device_mode == 0) {
3840 /* set suspend BIT */
3841 sc->sc_hprt_val |= HPRT_PRTSUSP;
3842 DWC_OTG_WRITE_4(sc, DOTG_HPRT, sc->sc_hprt_val);
3844 /* generate HUB suspend event */
3845 dwc_otg_suspend_irq(sc);
3849 case UHF_PORT_RESET:
3850 if (sc->sc_flags.status_device_mode == 0) {
3852 DPRINTF("PORT RESET\n");
3854 /* enable PORT reset */
3855 DWC_OTG_WRITE_4(sc, DOTG_HPRT, sc->sc_hprt_val | HPRT_PRTRST);
3857 /* Wait 62.5ms for reset to complete */
3858 usb_pause_mtx(&sc->sc_bus.bus_mtx, hz / 16);
3860 DWC_OTG_WRITE_4(sc, DOTG_HPRT, sc->sc_hprt_val);
3862 /* Wait 62.5ms for reset to complete */
3863 usb_pause_mtx(&sc->sc_bus.bus_mtx, hz / 16);
3866 dwc_otg_init_fifo(sc, DWC_MODE_HOST);
3868 sc->sc_flags.change_reset = 1;
3870 err = USB_ERR_IOERROR;
3875 case UHF_PORT_INDICATOR:
3878 case UHF_PORT_POWER:
3879 if (sc->sc_mode == DWC_MODE_HOST || sc->sc_mode == DWC_MODE_OTG) {
3880 sc->sc_hprt_val |= HPRT_PRTPWR;
3881 DWC_OTG_WRITE_4(sc, DOTG_HPRT, sc->sc_hprt_val);
3883 sc->sc_flags.port_powered = 1;
3886 err = USB_ERR_IOERROR;
3891 tr_handle_get_port_status:
3893 DPRINTFN(9, "UR_GET_PORT_STATUS\n");
3898 if (sc->sc_flags.status_vbus)
3899 dwc_otg_clocks_on(sc);
3901 dwc_otg_clocks_off(sc);
3903 /* Select Device Side Mode */
3905 if (sc->sc_flags.status_device_mode) {
3906 value = UPS_PORT_MODE_DEVICE;
3907 dwc_otg_timer_stop(sc);
3910 dwc_otg_timer_start(sc);
3913 if (sc->sc_flags.status_high_speed)
3914 value |= UPS_HIGH_SPEED;
3915 else if (sc->sc_flags.status_low_speed)
3916 value |= UPS_LOW_SPEED;
3918 if (sc->sc_flags.port_powered)
3919 value |= UPS_PORT_POWER;
3921 if (sc->sc_flags.port_enabled)
3922 value |= UPS_PORT_ENABLED;
3924 if (sc->sc_flags.port_over_current)
3925 value |= UPS_OVERCURRENT_INDICATOR;
3927 if (sc->sc_flags.status_vbus &&
3928 sc->sc_flags.status_bus_reset)
3929 value |= UPS_CURRENT_CONNECT_STATUS;
3931 if (sc->sc_flags.status_suspend)
3932 value |= UPS_SUSPEND;
3934 USETW(sc->sc_hub_temp.ps.wPortStatus, value);
3938 if (sc->sc_flags.change_connect)
3939 value |= UPS_C_CONNECT_STATUS;
3940 if (sc->sc_flags.change_suspend)
3941 value |= UPS_C_SUSPEND;
3942 if (sc->sc_flags.change_reset)
3943 value |= UPS_C_PORT_RESET;
3944 if (sc->sc_flags.change_over_current)
3945 value |= UPS_C_OVERCURRENT_INDICATOR;
3947 USETW(sc->sc_hub_temp.ps.wPortChange, value);
3948 len = sizeof(sc->sc_hub_temp.ps);
3951 tr_handle_get_class_descriptor:
3955 ptr = (const void *)&dwc_otg_hubd;
3956 len = sizeof(dwc_otg_hubd);
3960 err = USB_ERR_STALLED;
3969 dwc_otg_xfer_setup(struct usb_setup_params *parm)
3971 struct usb_xfer *xfer;
3977 xfer = parm->curr_xfer;
3980 * NOTE: This driver does not use any of the parameters that
3981 * are computed from the following values. Just set some
3982 * reasonable dummies:
3984 parm->hc_max_packet_size = 0x500;
3985 parm->hc_max_packet_count = 1;
3986 parm->hc_max_frame_size = 0x500;
3988 usbd_transfer_setup_sub(parm);
3991 * compute maximum number of TDs
3993 if ((xfer->endpoint->edesc->bmAttributes & UE_XFERTYPE) == UE_CONTROL) {
3995 ntd = xfer->nframes + 1 /* STATUS */ + 1 /* SYNC 1 */
3996 + 1 /* SYNC 2 */ + 1 /* SYNC 3 */;
3999 ntd = xfer->nframes + 1 /* SYNC */ ;
4003 * check if "usbd_transfer_setup_sub" set an error
4009 * allocate transfer descriptors
4013 ep_no = xfer->endpointno & UE_ADDR;
4016 * Check for a valid endpoint profile in USB device mode:
4018 if (xfer->flags_int.usb_mode == USB_MODE_DEVICE) {
4019 const struct usb_hw_ep_profile *pf;
4021 dwc_otg_get_hw_ep_profile(parm->udev, &pf, ep_no);
4024 /* should not happen */
4025 parm->err = USB_ERR_INVAL;
4031 parm->size[0] += ((-parm->size[0]) & (USB_HOST_ALIGN - 1));
4033 for (n = 0; n != ntd; n++) {
4035 struct dwc_otg_td *td;
4039 td = USB_ADD_BYTES(parm->buf, parm->size[0]);
4042 td->max_packet_size = xfer->max_packet_size;
4044 td->obj_next = last_obj;
4048 parm->size[0] += sizeof(*td);
4051 xfer->td_start[0] = last_obj;
4055 dwc_otg_xfer_unsetup(struct usb_xfer *xfer)
4061 dwc_otg_ep_init(struct usb_device *udev, struct usb_endpoint_descriptor *edesc,
4062 struct usb_endpoint *ep)
4064 struct dwc_otg_softc *sc = DWC_OTG_BUS2SC(udev->bus);
4066 DPRINTFN(2, "endpoint=%p, addr=%d, endpt=%d, mode=%d (%d,%d)\n",
4068 edesc->bEndpointAddress, udev->flags.usb_mode,
4069 sc->sc_rt_addr, udev->device_index);
4071 if (udev->device_index != sc->sc_rt_addr) {
4073 if (udev->flags.usb_mode == USB_MODE_DEVICE) {
4074 if (udev->speed != USB_SPEED_FULL &&
4075 udev->speed != USB_SPEED_HIGH) {
4082 mps = UGETW(edesc->wMaxPacketSize);
4084 /* Apply limitations of our USB host driver */
4086 switch (udev->speed) {
4087 case USB_SPEED_HIGH:
4089 DPRINTF("wMaxPacketSize=0x%04x"
4090 "is not supported\n", (int)mps);
4096 case USB_SPEED_FULL:
4099 DPRINTF("wMaxPacketSize=0x%04x"
4100 "is not supported\n", (int)mps);
4107 DPRINTF("Invalid device speed\n");
4113 if ((edesc->bmAttributes & UE_XFERTYPE) == UE_ISOCHRONOUS)
4114 ep->methods = &dwc_otg_device_isoc_methods;
4116 ep->methods = &dwc_otg_device_non_isoc_methods;
4121 dwc_otg_set_hw_power_sleep(struct usb_bus *bus, uint32_t state)
4123 struct dwc_otg_softc *sc = DWC_OTG_BUS2SC(bus);
4126 case USB_HW_POWER_SUSPEND:
4127 dwc_otg_suspend(sc);
4129 case USB_HW_POWER_SHUTDOWN:
4132 case USB_HW_POWER_RESUME:
4141 dwc_otg_get_dma_delay(struct usb_device *udev, uint32_t *pus)
4143 /* DMA delay - wait until any use of memory is finished */
4144 *pus = (2125); /* microseconds */
4148 dwc_otg_device_resume(struct usb_device *udev)
4150 struct dwc_otg_softc *sc = DWC_OTG_BUS2SC(udev->bus);
4151 struct usb_xfer *xfer;
4152 struct dwc_otg_td *td;
4156 /* Enable relevant Host channels before resuming */
4158 USB_BUS_LOCK(udev->bus);
4160 TAILQ_FOREACH(xfer, &sc->sc_bus.intr_q.head, wait_entry) {
4162 if (xfer->xroot->udev == udev) {
4164 td = xfer->td_transfer_cache;
4166 td->channel < DWC_OTG_MAX_CHANNELS)
4167 sc->sc_chan_state[td->channel].suspended = 0;
4171 USB_BUS_UNLOCK(udev->bus);
4173 /* poll all transfers again to restart resumed ones */
4174 dwc_otg_do_poll(udev->bus);
4178 dwc_otg_device_suspend(struct usb_device *udev)
4180 struct dwc_otg_softc *sc = DWC_OTG_BUS2SC(udev->bus);
4181 struct usb_xfer *xfer;
4182 struct dwc_otg_td *td;
4186 /* Disable relevant Host channels before going to suspend */
4188 USB_BUS_LOCK(udev->bus);
4190 TAILQ_FOREACH(xfer, &sc->sc_bus.intr_q.head, wait_entry) {
4192 if (xfer->xroot->udev == udev) {
4194 td = xfer->td_transfer_cache;
4196 td->channel < DWC_OTG_MAX_CHANNELS)
4197 sc->sc_chan_state[td->channel].suspended = 1;
4201 USB_BUS_UNLOCK(udev->bus);
4204 struct usb_bus_methods dwc_otg_bus_methods =
4206 .endpoint_init = &dwc_otg_ep_init,
4207 .xfer_setup = &dwc_otg_xfer_setup,
4208 .xfer_unsetup = &dwc_otg_xfer_unsetup,
4209 .get_hw_ep_profile = &dwc_otg_get_hw_ep_profile,
4210 .xfer_stall = &dwc_otg_xfer_stall,
4211 .set_stall = &dwc_otg_set_stall,
4212 .clear_stall = &dwc_otg_clear_stall,
4213 .roothub_exec = &dwc_otg_roothub_exec,
4214 .xfer_poll = &dwc_otg_do_poll,
4215 .device_state_change = &dwc_otg_device_state_change,
4216 .set_hw_power_sleep = &dwc_otg_set_hw_power_sleep,
4217 .get_dma_delay = &dwc_otg_get_dma_delay,
4218 .device_resume = &dwc_otg_device_resume,
4219 .device_suspend = &dwc_otg_device_suspend,