summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorhselasky <hselasky@FreeBSD.org>2014-05-16 15:50:21 +0000
committerhselasky <hselasky@FreeBSD.org>2014-05-16 15:50:21 +0000
commit529b37774dce86245509fe6a7b185774725c79a4 (patch)
tree47f421c05726eaa95cd486faded5da9d08c5a5c2
parentd8fcccb1e7737c256708d2600db76500510872ba (diff)
downloadFreeBSD-src-529b37774dce86245509fe6a7b185774725c79a4.zip
FreeBSD-src-529b37774dce86245509fe6a7b185774725c79a4.tar.gz
Rename "saf1761_dci_xxx" into "saf1761_otg_xxx" to reflect that this
driver supports both host and device side mode. Sponsored by: DARPA, AFRL
-rw-r--r--sys/dev/usb/controller/saf1761_otg.c2703
-rw-r--r--sys/dev/usb/controller/saf1761_otg.h157
-rw-r--r--sys/dev/usb/controller/saf1761_otg_fdt.c253
-rw-r--r--sys/dev/usb/controller/saf1761_otg_reg.h214
-rw-r--r--sys/modules/usb/saf1761/Makefile2
5 files changed, 3328 insertions, 1 deletions
diff --git a/sys/dev/usb/controller/saf1761_otg.c b/sys/dev/usb/controller/saf1761_otg.c
new file mode 100644
index 0000000..36e8c2c
--- /dev/null
+++ b/sys/dev/usb/controller/saf1761_otg.c
@@ -0,0 +1,2703 @@
+/* $FreeBSD$ */
+/*-
+ * Copyright (c) 2014 Hans Petter Selasky <hselasky@FreeBSD.org>
+ * All rights reserved.
+ *
+ * This software was developed by SRI International and the University of
+ * Cambridge Computer Laboratory under DARPA/AFRL contract (FA8750-10-C-0237)
+ * ("CTSRD"), as part of the DARPA CRASH research programme.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ */
+
+/*
+ * This file contains the driver for the SAF1761 series USB OTG
+ * controller.
+ *
+ * Datasheet is available from:
+ * http://www.nxp.com/products/automotive/multimedia/usb/SAF1761BE.html
+ */
+
+#ifdef USB_GLOBAL_INCLUDE_FILE
+#include USB_GLOBAL_INCLUDE_FILE
+#else
+#include <sys/stdint.h>
+#include <sys/stddef.h>
+#include <sys/param.h>
+#include <sys/queue.h>
+#include <sys/types.h>
+#include <sys/systm.h>
+#include <sys/kernel.h>
+#include <sys/bus.h>
+#include <sys/module.h>
+#include <sys/lock.h>
+#include <sys/mutex.h>
+#include <sys/condvar.h>
+#include <sys/sysctl.h>
+#include <sys/sx.h>
+#include <sys/unistd.h>
+#include <sys/callout.h>
+#include <sys/malloc.h>
+#include <sys/priv.h>
+
+#include <dev/usb/usb.h>
+#include <dev/usb/usbdi.h>
+
+#define USB_DEBUG_VAR saf1761_otg_debug
+
+#include <dev/usb/usb_core.h>
+#include <dev/usb/usb_debug.h>
+#include <dev/usb/usb_busdma.h>
+#include <dev/usb/usb_process.h>
+#include <dev/usb/usb_transfer.h>
+#include <dev/usb/usb_device.h>
+#include <dev/usb/usb_hub.h>
+#include <dev/usb/usb_util.h>
+
+#include <dev/usb/usb_controller.h>
+#include <dev/usb/usb_bus.h>
+#endif /* USB_GLOBAL_INCLUDE_FILE */
+
+#include <dev/usb/controller/saf1761_otg.h>
+#include <dev/usb/controller/saf1761_otg_reg.h>
+
+#define SAF1761_OTG_BUS2SC(bus) \
+ ((struct saf1761_otg_softc *)(((uint8_t *)(bus)) - \
+ ((uint8_t *)&(((struct saf1761_otg_softc *)0)->sc_bus))))
+
+#ifdef USB_DEBUG
+static int saf1761_otg_debug = 0;
+static int saf1761_otg_forcefs = 0;
+
+static
+SYSCTL_NODE(_hw_usb, OID_AUTO, saf1761_otg, CTLFLAG_RW, 0,
+ "USB SAF1761 DCI");
+
+SYSCTL_INT(_hw_usb_saf1761_otg, OID_AUTO, debug, CTLFLAG_RW,
+ &saf1761_otg_debug, 0, "SAF1761 DCI debug level");
+SYSCTL_INT(_hw_usb_saf1761_otg, OID_AUTO, forcefs, CTLFLAG_RW,
+ &saf1761_otg_forcefs, 0, "SAF1761 DCI force FULL speed");
+#endif
+
+#define SAF1761_OTG_INTR_ENDPT 1
+
+/* prototypes */
+
+static const struct usb_bus_methods saf1761_otg_bus_methods;
+static const struct usb_pipe_methods saf1761_otg_non_isoc_methods;
+static const struct usb_pipe_methods saf1761_otg_device_isoc_methods;
+
+static saf1761_otg_cmd_t saf1761_host_setup_tx;
+static saf1761_otg_cmd_t saf1761_host_bulk_data_rx;
+static saf1761_otg_cmd_t saf1761_host_bulk_data_tx;
+static saf1761_otg_cmd_t saf1761_host_intr_data_rx;
+static saf1761_otg_cmd_t saf1761_host_intr_data_tx;
+static saf1761_otg_cmd_t saf1761_host_isoc_data_rx;
+static saf1761_otg_cmd_t saf1761_host_isoc_data_tx;
+static saf1761_otg_cmd_t saf1761_device_setup_rx;
+static saf1761_otg_cmd_t saf1761_device_data_rx;
+static saf1761_otg_cmd_t saf1761_device_data_tx;
+static saf1761_otg_cmd_t saf1761_device_data_tx_sync;
+static void saf1761_otg_device_done(struct usb_xfer *, usb_error_t);
+static void saf1761_otg_do_poll(struct usb_bus *);
+static void saf1761_otg_standard_done(struct usb_xfer *);
+static void saf1761_otg_intr_set(struct usb_xfer *, uint8_t);
+static void saf1761_otg_root_intr(struct saf1761_otg_softc *);
+
+/*
+ * Here is a list of what the SAF1761 chip can support. The main
+ * limitation is that the sum of the buffer sizes must be less than
+ * 8192 bytes.
+ */
+static const struct usb_hw_ep_profile saf1761_otg_ep_profile[] = {
+
+ [0] = {
+ .max_in_frame_size = 64,
+ .max_out_frame_size = 64,
+ .is_simplex = 0,
+ .support_control = 1,
+ },
+ [1] = {
+ .max_in_frame_size = SOTG_HS_MAX_PACKET_SIZE,
+ .max_out_frame_size = SOTG_HS_MAX_PACKET_SIZE,
+ .is_simplex = 0,
+ .support_interrupt = 1,
+ .support_bulk = 1,
+ .support_isochronous = 1,
+ .support_in = 1,
+ .support_out = 1,
+ },
+};
+
+static void
+saf1761_otg_get_hw_ep_profile(struct usb_device *udev,
+ const struct usb_hw_ep_profile **ppf, uint8_t ep_addr)
+{
+ if (ep_addr == 0) {
+ *ppf = saf1761_otg_ep_profile + 0;
+ } else if (ep_addr < 8) {
+ *ppf = saf1761_otg_ep_profile + 1;
+ } else {
+ *ppf = NULL;
+ }
+}
+
+static void
+saf1761_otg_pull_up(struct saf1761_otg_softc *sc)
+{
+ /* activate pullup on D+, if possible */
+
+ if (!sc->sc_flags.d_pulled_up && sc->sc_flags.port_powered) {
+ DPRINTF("\n");
+
+ sc->sc_flags.d_pulled_up = 1;
+ }
+}
+
+static void
+saf1761_otg_pull_down(struct saf1761_otg_softc *sc)
+{
+ /* release pullup on D+, if possible */
+
+ if (sc->sc_flags.d_pulled_up) {
+ DPRINTF("\n");
+
+ sc->sc_flags.d_pulled_up = 0;
+ }
+}
+
+static void
+saf1761_otg_wakeup_peer(struct saf1761_otg_softc *sc)
+{
+ uint16_t temp;
+
+ if (!(sc->sc_flags.status_suspend))
+ return;
+
+ DPRINTFN(5, "\n");
+
+ temp = SAF1761_READ_2(sc, SOTG_MODE);
+ SAF1761_WRITE_2(sc, SOTG_MODE, temp | SOTG_MODE_SNDRSU);
+ SAF1761_WRITE_2(sc, SOTG_MODE, temp & ~SOTG_MODE_SNDRSU);
+
+ /* Wait 8ms for remote wakeup to complete. */
+ usb_pause_mtx(&sc->sc_bus.bus_mtx, hz / 125);
+
+}
+
+static uint8_t
+saf1761_host_channel_alloc(struct saf1761_otg_softc *sc, struct saf1761_otg_td *td)
+{
+ uint32_t x;
+
+ if (td->channel < SOTG_HOST_CHANNEL_MAX)
+ return (0);
+
+ switch (td->ep_type) {
+ case UE_INTERRUPT:
+ for (x = 0; x != 32; x++) {
+ if (sc->sc_host_intr_map & (1 << x))
+ continue;
+ sc->sc_host_intr_map |= (1 << x);
+ td->channel = 32 + x;
+ return (0);
+ }
+ break;
+ case UE_ISOCHRONOUS:
+ for (x = 0; x != 32; x++) {
+ if (sc->sc_host_isoc_map & (1 << x))
+ continue;
+ sc->sc_host_isoc_map |= (1 << x);
+ td->channel = 64 + x;
+ return (0);
+ }
+ break;
+ default:
+ for (x = 0; x != 32; x++) {
+ if (sc->sc_host_async_map & (1 << x))
+ continue;
+ sc->sc_host_async_map |= (1 << x);
+ td->channel = x;
+ return (0);
+ }
+ break;
+ }
+ return (1);
+}
+
+static void
+saf1761_host_channel_free(struct saf1761_otg_softc *sc, struct saf1761_otg_td *td)
+{
+ uint32_t x;
+
+ if (td->channel >= SOTG_HOST_CHANNEL_MAX)
+ return;
+
+ /* disable channel */
+ SAF1761_WRITE_4(sc, SOTG_ASYNC_PDT(td->channel) + (4 * 3), 0);
+ SAF1761_WRITE_4(sc, SOTG_ASYNC_PDT(td->channel) + (4 * 0), 0);
+
+ switch (td->ep_type) {
+ case UE_INTERRUPT:
+ x = td->channel - 32;
+ sc->sc_host_intr_map &= ~(1 << x);
+ td->channel = SOTG_HOST_CHANNEL_MAX;
+ break;
+ case UE_ISOCHRONOUS:
+ x = td->channel - 64;
+ sc->sc_host_isoc_map &= ~(1 << x);
+ td->channel = SOTG_HOST_CHANNEL_MAX;
+ break;
+ default:
+ x = td->channel - 64;
+ sc->sc_host_async_map &= ~(1 << x);
+ td->channel = SOTG_HOST_CHANNEL_MAX;
+ break;
+ }
+}
+
+static void
+saf1761_read_host_fifo_1(struct saf1761_otg_softc *sc, struct saf1761_otg_td *td,
+ void *buf, uint32_t len)
+{
+ bus_space_read_region_1((sc)->sc_io_tag, (sc)->sc_io_hdl,
+ SOTG_DATA_ADDR(td->channel), buf, len);
+}
+
+static void
+saf1761_write_host_fifo_1(struct saf1761_otg_softc *sc, struct saf1761_otg_td *td,
+ void *buf, uint32_t len)
+{
+ bus_space_write_region_1((sc)->sc_io_tag, (sc)->sc_io_hdl,
+ SOTG_DATA_ADDR(td->channel), buf, len);
+}
+
+static uint8_t
+saf1761_host_setup_tx(struct saf1761_otg_softc *sc, struct saf1761_otg_td *td)
+{
+ struct usb_device_request req __aligned(4);
+ uint32_t status;
+ uint32_t count;
+
+ if (td->channel < SOTG_HOST_CHANNEL_MAX) {
+ status = SAF1761_READ_4(sc, SOTG_ASYNC_PDT(td->channel) + (4 * 3));
+ if (status & (1 << 31)) {
+ goto busy;
+ } else if (status & (1 << 30)) {
+ td->error_stall = 1;
+ td->error_any = 1;
+ } else if (status & (3 << 28)) {
+ td->error_any = 1;
+ }
+ count = (status & 0x7FFF);
+
+ saf1761_host_channel_free(sc, td);
+ goto complete;
+ }
+ if (saf1761_host_channel_alloc(sc, td))
+ goto busy;
+
+ if (sizeof(req) != td->remainder) {
+ td->error_any = 1;
+ goto complete;
+ }
+
+ count = sizeof(req);
+
+ usbd_copy_out(td->pc, 0, &req, count);
+
+ saf1761_write_host_fifo_1(sc, td, &req, count);
+
+ SAF1761_WRITE_4(sc, SOTG_ASYNC_PDT(td->channel) + (4 * 7), 0);
+ SAF1761_WRITE_4(sc, SOTG_ASYNC_PDT(td->channel) + (4 * 6), 0);
+ SAF1761_WRITE_4(sc, SOTG_ASYNC_PDT(td->channel) + (4 * 5), 0);
+ SAF1761_WRITE_4(sc, SOTG_ASYNC_PDT(td->channel) + (4 * 4), 0);
+ SAF1761_WRITE_4(sc, SOTG_ASYNC_PDT(td->channel) + (4 * 3),
+ (1 << 31) | (td->toggle << 25) | (3 << 23));
+ SAF1761_WRITE_4(sc, SOTG_ASYNC_PDT(td->channel) + (4 * 2),
+ SOTG_HC_MEMORY_ADDR(SOTG_DATA_ADDR(td->channel)) << 8);
+
+ SAF1761_WRITE_4(sc, SOTG_ASYNC_PDT(td->channel) + (4 * 1),
+ td->dw1_value |
+ (2 << 10) /* SETUP PID */ |
+ (td->ep_index >> 1));
+
+ SAF1761_WRITE_4(sc, SOTG_ASYNC_PDT(td->channel) + (4 * 0),
+ (td->ep_index << 31) |
+ (1 << 29) /* pkt-multiplier */ |
+ (td->max_packet_size << 18) /* wMaxPacketSize */ |
+ (count << 3) /* transfer count */ |
+ 1 /* valid */);
+
+ td->offset += count;
+ td->remainder -= count;
+ td->toggle = 1;
+busy:
+ return (1); /* busy */
+complete:
+ return (0); /* complete */
+}
+
+static uint8_t
+saf1761_host_bulk_data_rx(struct saf1761_otg_softc *sc, struct saf1761_otg_td *td)
+{
+ if (td->channel < SOTG_HOST_CHANNEL_MAX) {
+ uint32_t status;
+ uint32_t count;
+ uint8_t got_short;
+
+ status = SAF1761_READ_4(sc, SOTG_ASYNC_PDT(td->channel) + (4 * 3));
+
+ if (status & (1 << 31)) {
+ goto busy;
+ } else if (status & (1 << 30)) {
+ td->error_stall = 1;
+ td->error_any = 1;
+ goto complete;
+ } else if (status & (3 << 28)) {
+ td->error_any = 1;
+ goto complete;
+ }
+ count = (status & 0x7FFF);
+ got_short = 0;
+
+ /* verify the packet byte count */
+ if (count != td->max_packet_size) {
+ if (count < td->max_packet_size) {
+ /* we have a short packet */
+ td->short_pkt = 1;
+ got_short = 1;
+ } else {
+ /* invalid USB packet */
+ td->error_any = 1;
+ goto complete;
+ }
+ }
+ td->toggle ^= 1;
+
+ /* verify the packet byte count */
+ if (count > td->remainder) {
+ /* invalid USB packet */
+ td->error_any = 1;
+ goto complete;
+ }
+
+ saf1761_read_host_fifo_1(sc, td,
+ sc->sc_bounce_buffer, count);
+ usbd_copy_in(td->pc, td->offset,
+ sc->sc_bounce_buffer, count);
+
+ td->remainder -= count;
+ td->offset += count;
+
+ saf1761_host_channel_free(sc, td);
+
+ /* check if we are complete */
+ if ((td->remainder == 0) || got_short) {
+ if (td->short_pkt)
+ goto complete;
+ /* else need to receive a zero length packet */
+ }
+ }
+ if (saf1761_host_channel_alloc(sc, td))
+ goto busy;
+
+ /* set toggle, if any */
+ if (td->set_toggle) {
+ td->set_toggle = 0;
+ td->toggle = 1;
+ }
+
+ /* receive one more packet */
+
+ SAF1761_WRITE_4(sc, SOTG_ASYNC_PDT(td->channel) + (4 * 7), 0);
+ SAF1761_WRITE_4(sc, SOTG_ASYNC_PDT(td->channel) + (4 * 6), 0);
+ SAF1761_WRITE_4(sc, SOTG_ASYNC_PDT(td->channel) + (4 * 5), 0);
+ SAF1761_WRITE_4(sc, SOTG_ASYNC_PDT(td->channel) + (4 * 4), 0);
+ SAF1761_WRITE_4(sc, SOTG_ASYNC_PDT(td->channel) + (4 * 3),
+ (1 << 31) | (td->toggle << 25) | (3 << 23));
+ SAF1761_WRITE_4(sc, SOTG_ASYNC_PDT(td->channel) + (4 * 2),
+ SOTG_HC_MEMORY_ADDR(SOTG_DATA_ADDR(td->channel)) << 8);
+
+ SAF1761_WRITE_4(sc, SOTG_ASYNC_PDT(td->channel) + (4 * 1),
+ td->dw1_value |
+ (1 << 10) /* IN-PID */ |
+ (td->ep_index >> 1));
+
+ SAF1761_WRITE_4(sc, SOTG_ASYNC_PDT(td->channel) + (4 * 0),
+ (td->ep_index << 31) |
+ (1 << 29) /* pkt-multiplier */ |
+ (td->max_packet_size << 18) /* wMaxPacketSize */ |
+ (td->max_packet_size << 3) /* transfer count */ |
+ 1 /* valid */);
+busy:
+ return (1); /* busy */
+complete:
+ return (0); /* complete */
+}
+
+static uint8_t
+saf1761_host_bulk_data_tx(struct saf1761_otg_softc *sc, struct saf1761_otg_td *td)
+{
+ uint32_t count;
+
+ if (td->channel < SOTG_HOST_CHANNEL_MAX) {
+ uint32_t status;
+
+ status = SAF1761_READ_4(sc, SOTG_ASYNC_PDT(td->channel) + (4 * 3));
+ if (status & (1 << 31)) {
+ goto busy;
+ } else if (status & (1 << 30)) {
+ td->error_stall = 1;
+ td->error_any = 1;
+ } else if (status & (3 << 28)) {
+ td->error_any = 1;
+ }
+
+ saf1761_host_channel_free(sc, td);
+
+ /* check remainder */
+ if (td->remainder == 0) {
+ if (td->short_pkt)
+ goto complete;
+ /* else we need to transmit a short packet */
+ }
+ }
+ if (saf1761_host_channel_alloc(sc, td))
+ goto busy;
+
+ count = td->max_packet_size;
+ if (td->remainder < count) {
+ /* we have a short packet */
+ td->short_pkt = 1;
+ count = td->remainder;
+ }
+
+ usbd_copy_out(td->pc, td->offset, sc->sc_bounce_buffer, count);
+ saf1761_write_host_fifo_1(sc, td, sc->sc_bounce_buffer, count);
+
+ /* set toggle, if any */
+ if (td->set_toggle) {
+ td->set_toggle = 0;
+ td->toggle = 1;
+ }
+
+ SAF1761_WRITE_4(sc, SOTG_ASYNC_PDT(td->channel) + (4 * 7), 0);
+ SAF1761_WRITE_4(sc, SOTG_ASYNC_PDT(td->channel) + (4 * 6), 0);
+ SAF1761_WRITE_4(sc, SOTG_ASYNC_PDT(td->channel) + (4 * 5), 0);
+ SAF1761_WRITE_4(sc, SOTG_ASYNC_PDT(td->channel) + (4 * 4), 0);
+ SAF1761_WRITE_4(sc, SOTG_ASYNC_PDT(td->channel) + (4 * 3),
+ (1 << 31) | (td->toggle << 25) | (3 << 23));
+ SAF1761_WRITE_4(sc, SOTG_ASYNC_PDT(td->channel) + (4 * 2),
+ SOTG_HC_MEMORY_ADDR(SOTG_DATA_ADDR(td->channel)) << 8);
+
+ SAF1761_WRITE_4(sc, SOTG_ASYNC_PDT(td->channel) + (4 * 1),
+ td->dw1_value |
+ (0 << 10) /* OUT-PID */ |
+ (td->ep_index >> 1));
+
+ SAF1761_WRITE_4(sc, SOTG_ASYNC_PDT(td->channel) + (4 * 0),
+ (td->ep_index << 31) |
+ (1 << 29) /* pkt-multiplier */ |
+ (td->max_packet_size << 18) /* wMaxPacketSize */ |
+ (count << 3) /* transfer count */ |
+ 1 /* valid */);
+
+ td->offset += count;
+ td->remainder -= count;
+ td->toggle ^= 1;
+busy:
+ return (1); /* busy */
+complete:
+ return (0); /* complete */
+}
+
+static uint8_t
+saf1761_host_intr_data_rx(struct saf1761_otg_softc *sc, struct saf1761_otg_td *td)
+{
+ return (1); /* busy */
+}
+
+static uint8_t
+saf1761_host_intr_data_tx(struct saf1761_otg_softc *sc, struct saf1761_otg_td *td)
+{
+ return (1); /* busy */
+}
+
+static uint8_t
+saf1761_host_isoc_data_rx(struct saf1761_otg_softc *sc, struct saf1761_otg_td *td)
+{
+ return (1); /* busy */
+}
+
+static uint8_t
+saf1761_host_isoc_data_tx(struct saf1761_otg_softc *sc, struct saf1761_otg_td *td)
+{
+ return (1); /* busy */
+}
+
+static void
+saf1761_otg_set_address(struct saf1761_otg_softc *sc, uint8_t addr)
+{
+ DPRINTFN(5, "addr=%d\n", addr);
+
+ SAF1761_WRITE_1(sc, SOTG_ADDRESS, addr | SOTG_ADDRESS_ENABLE);
+}
+
+static void
+saf1761_read_device_fifo_1(struct saf1761_otg_softc *sc, void *buf, uint32_t len)
+{
+ bus_space_read_multi_1((sc)->sc_io_tag, (sc)->sc_io_hdl,
+ SOTG_DATA_PORT, buf, len);
+}
+
+static void
+saf1761_write_device_fifo_1(struct saf1761_otg_softc *sc, void *buf, uint32_t len)
+{
+ bus_space_write_multi_1((sc)->sc_io_tag, (sc)->sc_io_hdl,
+ SOTG_DATA_PORT, buf, len);
+}
+
+static uint8_t
+saf1761_device_setup_rx(struct saf1761_otg_softc *sc, struct saf1761_otg_td *td)
+{
+ struct usb_device_request req;
+ uint16_t count;
+
+ /* select the correct endpoint */
+ SAF1761_WRITE_1(sc, SOTG_EP_INDEX, SOTG_EP_INDEX_EP0SETUP);
+
+ /* check buffer status */
+ if ((SAF1761_READ_1(sc, SOTG_DCBUFFERSTATUS) &
+ SOTG_DCBUFFERSTATUS_FILLED_MASK) == 0)
+ goto busy;
+
+ /* read buffer length */
+ count = SAF1761_READ_2(sc, SOTG_BUF_LENGTH);
+
+ DPRINTFN(5, "count=%u rem=%u\n", count, td->remainder);
+
+ /* clear did stall */
+ td->did_stall = 0;
+
+ /* clear stall */
+ SAF1761_WRITE_1(sc, SOTG_CTRL_FUNC, 0);
+
+ /* verify data length */
+ if (count != td->remainder) {
+ DPRINTFN(0, "Invalid SETUP packet "
+ "length, %d bytes\n", count);
+ goto busy;
+ }
+ if (count != sizeof(req)) {
+ DPRINTFN(0, "Unsupported SETUP packet "
+ "length, %d bytes\n", count);
+ goto busy;
+ }
+ /* receive data */
+ saf1761_read_device_fifo_1(sc, &req, sizeof(req));
+
+ /* copy data into real buffer */
+ usbd_copy_in(td->pc, 0, &req, sizeof(req));
+
+ td->offset = sizeof(req);
+ td->remainder = 0;
+
+ /* sneak peek the set address */
+ if ((req.bmRequestType == UT_WRITE_DEVICE) &&
+ (req.bRequest == UR_SET_ADDRESS)) {
+ sc->sc_dv_addr = req.wValue[0] & 0x7F;
+ DPRINTF("Set address %d\n", sc->sc_dv_addr);
+ } else {
+ sc->sc_dv_addr = 0xFF;
+ }
+ return (0); /* complete */
+
+busy:
+ /* abort any ongoing transfer */
+ if (!td->did_stall) {
+ DPRINTFN(5, "stalling\n");
+
+ /* set stall */
+ SAF1761_WRITE_1(sc, SOTG_CTRL_FUNC, SOTG_CTRL_FUNC_STALL);
+
+ td->did_stall = 1;
+ }
+ return (1); /* not complete */
+}
+
+static uint8_t
+saf1761_device_data_rx(struct saf1761_otg_softc *sc, struct saf1761_otg_td *td)
+{
+ struct usb_page_search buf_res;
+ uint16_t count;
+ uint8_t got_short = 0;
+
+ if (td->ep_index == 0) {
+ /* select the correct endpoint */
+ SAF1761_WRITE_1(sc, SOTG_EP_INDEX, SOTG_EP_INDEX_EP0SETUP);
+
+ /* check buffer status */
+ if ((SAF1761_READ_1(sc, SOTG_DCBUFFERSTATUS) &
+ SOTG_DCBUFFERSTATUS_FILLED_MASK) != 0) {
+
+ if (td->remainder == 0) {
+ /*
+ * We are actually complete and have
+ * received the next SETUP:
+ */
+ DPRINTFN(5, "faking complete\n");
+ return (0); /* complete */
+ }
+ DPRINTFN(5, "SETUP packet while receiving data\n");
+ /*
+ * USB Host Aborted the transfer.
+ */
+ td->error_any = 1;
+ return (0); /* complete */
+ }
+ }
+ /* select the correct endpoint */
+ SAF1761_WRITE_1(sc, SOTG_EP_INDEX,
+ (td->ep_index << SOTG_EP_INDEX_ENDP_INDEX_SHIFT) |
+ SOTG_EP_INDEX_DIR_OUT);
+
+ /* enable data stage */
+ if (td->set_toggle) {
+ td->set_toggle = 0;
+ SAF1761_WRITE_1(sc, SOTG_CTRL_FUNC, SOTG_CTRL_FUNC_DSEN);
+ }
+
+ /* check buffer status */
+ if ((SAF1761_READ_1(sc, SOTG_DCBUFFERSTATUS) &
+ SOTG_DCBUFFERSTATUS_FILLED_MASK) == 0) {
+ return (1); /* not complete */
+ }
+ /* read buffer length */
+ count = SAF1761_READ_2(sc, SOTG_BUF_LENGTH);
+
+ DPRINTFN(5, "rem=%u count=0x%04x\n", td->remainder, count);
+
+ /* verify the packet byte count */
+ if (count != td->max_packet_size) {
+ if (count < td->max_packet_size) {
+ /* we have a short packet */
+ td->short_pkt = 1;
+ got_short = 1;
+ } else {
+ /* invalid USB packet */
+ td->error_any = 1;
+ return (0); /* we are complete */
+ }
+ }
+ /* verify the packet byte count */
+ if (count > td->remainder) {
+ /* invalid USB packet */
+ td->error_any = 1;
+ return (0); /* we are complete */
+ }
+ while (count > 0) {
+ usbd_get_page(td->pc, td->offset, &buf_res);
+
+ /* get correct length */
+ if (buf_res.length > count)
+ buf_res.length = count;
+
+ /* receive data */
+ saf1761_read_device_fifo_1(sc, buf_res.buffer, buf_res.length);
+
+ /* update counters */
+ count -= buf_res.length;
+ td->offset += buf_res.length;
+ td->remainder -= buf_res.length;
+ }
+ /* check if we are complete */
+ if ((td->remainder == 0) || got_short) {
+ if (td->short_pkt) {
+ /* we are complete */
+ return (0);
+ }
+ /* else need to receive a zero length packet */
+ }
+ return (1); /* not complete */
+}
+
+static uint8_t
+saf1761_device_data_tx(struct saf1761_otg_softc *sc, struct saf1761_otg_td *td)
+{
+ struct usb_page_search buf_res;
+ uint16_t count;
+ uint16_t count_old;
+
+ if (td->ep_index == 0) {
+ /* select the correct endpoint */
+ SAF1761_WRITE_1(sc, SOTG_EP_INDEX, SOTG_EP_INDEX_EP0SETUP);
+
+ /* check buffer status */
+ if ((SAF1761_READ_1(sc, SOTG_DCBUFFERSTATUS) &
+ SOTG_DCBUFFERSTATUS_FILLED_MASK) != 0) {
+ DPRINTFN(5, "SETUP abort\n");
+ /*
+ * USB Host Aborted the transfer.
+ */
+ td->error_any = 1;
+ return (0); /* complete */
+ }
+ }
+ /* select the correct endpoint */
+ SAF1761_WRITE_1(sc, SOTG_EP_INDEX,
+ (td->ep_index << SOTG_EP_INDEX_ENDP_INDEX_SHIFT) |
+ SOTG_EP_INDEX_DIR_IN);
+
+ /* check buffer status */
+ if ((SAF1761_READ_1(sc, SOTG_DCBUFFERSTATUS) &
+ SOTG_DCBUFFERSTATUS_FILLED_MASK) != 0) {
+ return (1); /* not complete */
+ }
+
+ /* enable data stage */
+ if (td->set_toggle) {
+ td->set_toggle = 0;
+ SAF1761_WRITE_1(sc, SOTG_CTRL_FUNC, SOTG_CTRL_FUNC_DSEN);
+ }
+
+ DPRINTFN(5, "rem=%u\n", td->remainder);
+
+ count = td->max_packet_size;
+ if (td->remainder < count) {
+ /* we have a short packet */
+ td->short_pkt = 1;
+ count = td->remainder;
+ }
+ count_old = count;
+
+ while (count > 0) {
+
+ usbd_get_page(td->pc, td->offset, &buf_res);
+
+ /* get correct length */
+ if (buf_res.length > count)
+ buf_res.length = count;
+
+ /* transmit data */
+ saf1761_write_device_fifo_1(sc, buf_res.buffer, buf_res.length);
+
+ /* update counters */
+ count -= buf_res.length;
+ td->offset += buf_res.length;
+ td->remainder -= buf_res.length;
+ }
+
+ if (td->ep_index == 0) {
+ if (count_old < SOTG_FS_MAX_PACKET_SIZE) {
+ /* set end of packet */
+ SAF1761_WRITE_1(sc, SOTG_CTRL_FUNC, SOTG_CTRL_FUNC_VENDP);
+ }
+ } else {
+ if (count_old < SOTG_HS_MAX_PACKET_SIZE) {
+ /* set end of packet */
+ SAF1761_WRITE_1(sc, SOTG_CTRL_FUNC, SOTG_CTRL_FUNC_VENDP);
+ }
+ }
+
+ /* check remainder */
+ if (td->remainder == 0) {
+ if (td->short_pkt) {
+ return (0); /* complete */
+ }
+ /* else we need to transmit a short packet */
+ }
+ return (1); /* not complete */
+}
+
+static uint8_t
+saf1761_device_data_tx_sync(struct saf1761_otg_softc *sc, struct saf1761_otg_td *td)
+{
+ if (td->ep_index == 0) {
+ /* select the correct endpoint */
+ SAF1761_WRITE_1(sc, SOTG_EP_INDEX, SOTG_EP_INDEX_EP0SETUP);
+
+ /* check buffer status */
+ if ((SAF1761_READ_1(sc, SOTG_DCBUFFERSTATUS) &
+ SOTG_DCBUFFERSTATUS_FILLED_MASK) != 0) {
+ DPRINTFN(5, "Faking complete\n");
+ return (0); /* complete */
+ }
+ }
+ /* select the correct endpoint */
+ SAF1761_WRITE_1(sc, SOTG_EP_INDEX,
+ (td->ep_index << SOTG_EP_INDEX_ENDP_INDEX_SHIFT) |
+ SOTG_EP_INDEX_DIR_IN);
+
+ /* check buffer status */
+ if ((SAF1761_READ_1(sc, SOTG_DCBUFFERSTATUS) &
+ SOTG_DCBUFFERSTATUS_FILLED_MASK) != 0)
+ return (1); /* busy */
+
+ if (sc->sc_dv_addr != 0xFF) {
+ /* write function address */
+ saf1761_otg_set_address(sc, sc->sc_dv_addr);
+ }
+ return (0); /* complete */
+}
+
+static uint8_t
+saf1761_otg_xfer_do_fifo(struct saf1761_otg_softc *sc, struct usb_xfer *xfer)
+{
+ struct saf1761_otg_td *td;
+ uint8_t toggle;
+
+ DPRINTFN(9, "\n");
+
+ td = xfer->td_transfer_cache;
+ while (1) {
+ if ((td->func) (sc, td)) {
+ /* operation in progress */
+ break;
+ }
+ if (((void *)td) == xfer->td_transfer_last) {
+ goto done;
+ }
+ if (td->error_any) {
+ goto done;
+ } else if (td->remainder > 0) {
+ /*
+ * We had a short transfer. If there is no alternate
+ * next, stop processing !
+ */
+ if (!td->alt_next) {
+ goto done;
+ }
+ }
+ /*
+ * Fetch the next transfer descriptor.
+ */
+ toggle = td->toggle;
+ td = td->obj_next;
+ td->toggle = toggle;
+ xfer->td_transfer_cache = td;
+ }
+ return (1); /* not complete */
+
+done:
+ /* compute all actual lengths */
+
+ saf1761_otg_standard_done(xfer);
+
+ return (0); /* complete */
+}
+
+static void
+saf1761_otg_interrupt_poll(struct saf1761_otg_softc *sc)
+{
+ struct usb_xfer *xfer;
+
+repeat:
+ TAILQ_FOREACH(xfer, &sc->sc_bus.intr_q.head, wait_entry) {
+ if (!saf1761_otg_xfer_do_fifo(sc, xfer)) {
+ /* queue has been modified */
+ goto repeat;
+ }
+ }
+}
+
+static void
+saf1761_otg_wait_suspend(struct saf1761_otg_softc *sc, uint8_t on)
+{
+ if (on) {
+ sc->sc_intr_enable |= SOTG_DCINTERRUPT_IESUSP;
+ sc->sc_intr_enable &= ~SOTG_DCINTERRUPT_IERESM;
+ } else {
+ sc->sc_intr_enable &= ~SOTG_DCINTERRUPT_IESUSP;
+ sc->sc_intr_enable |= SOTG_DCINTERRUPT_IERESM;
+ }
+ SAF1761_WRITE_4(sc, SOTG_DCINTERRUPT_EN, sc->sc_intr_enable);
+}
+
+static void
+saf1761_otg_update_vbus(struct saf1761_otg_softc *sc)
+{
+ uint16_t status;
+
+ /* read fresh status */
+ status = SAF1761_READ_2(sc, SOTG_STATUS);
+
+ DPRINTFN(4, "STATUS=0x%04x\n", status);
+
+ if ((status & SOTG_STATUS_VBUS_VLD) &&
+ (status & SOTG_STATUS_ID)) {
+ /* VBUS present and device mode */
+ if (!sc->sc_flags.status_vbus) {
+ sc->sc_flags.status_vbus = 1;
+
+ /* complete root HUB interrupt endpoint */
+ saf1761_otg_root_intr(sc);
+ }
+ } else {
+ /* VBUS not-present or host mode */
+ if (sc->sc_flags.status_vbus) {
+ sc->sc_flags.status_vbus = 0;
+ sc->sc_flags.status_bus_reset = 0;
+ sc->sc_flags.status_suspend = 0;
+ sc->sc_flags.change_suspend = 0;
+ sc->sc_flags.change_connect = 1;
+
+ /* complete root HUB interrupt endpoint */
+ saf1761_otg_root_intr(sc);
+ }
+ }
+}
+
+void
+saf1761_otg_interrupt(struct saf1761_otg_softc *sc)
+{
+ uint32_t status;
+
+ USB_BUS_LOCK(&sc->sc_bus);
+
+ status = SAF1761_READ_4(sc, SOTG_DCINTERRUPT);
+
+ /* acknowledge all interrupts */
+ SAF1761_WRITE_4(sc, SOTG_DCINTERRUPT, status);
+
+ DPRINTF("DCINTERRUPT=0x%08x SOF=0x%04x\n", status,
+ SAF1761_READ_2(sc, SOTG_FRAME_NUM));
+
+ /* update VBUS and ID bits, if any */
+ if (status & SOTG_DCINTERRUPT_IEVBUS) {
+ saf1761_otg_update_vbus(sc);
+ }
+
+ if (status & SOTG_DCINTERRUPT_IEBRST) {
+ /* unlock device */
+ SAF1761_WRITE_2(sc, SOTG_UNLOCK_DEVICE,
+ SOTG_UNLOCK_DEVICE_CODE);
+
+ /* Enable device address */
+ SAF1761_WRITE_1(sc, SOTG_ADDRESS,
+ SOTG_ADDRESS_ENABLE);
+
+ sc->sc_flags.status_bus_reset = 1;
+ sc->sc_flags.status_suspend = 0;
+ sc->sc_flags.change_suspend = 0;
+ sc->sc_flags.change_connect = 1;
+
+ /* disable resume interrupt */
+ saf1761_otg_wait_suspend(sc, 1);
+ /* complete root HUB interrupt endpoint */
+ saf1761_otg_root_intr(sc);
+ }
+ /*
+ * If "RESUME" and "SUSPEND" is set at the same time we
+ * interpret that like "RESUME". Resume is set when there is
+ * at least 3 milliseconds of inactivity on the USB BUS:
+ */
+ if (status & SOTG_DCINTERRUPT_IERESM) {
+ /* unlock device */
+ SAF1761_WRITE_2(sc, SOTG_UNLOCK_DEVICE,
+ SOTG_UNLOCK_DEVICE_CODE);
+
+ if (sc->sc_flags.status_suspend) {
+ sc->sc_flags.status_suspend = 0;
+ sc->sc_flags.change_suspend = 1;
+ /* disable resume interrupt */
+ saf1761_otg_wait_suspend(sc, 1);
+ /* complete root HUB interrupt endpoint */
+ saf1761_otg_root_intr(sc);
+ }
+ } else if (status & SOTG_DCINTERRUPT_IESUSP) {
+ if (!sc->sc_flags.status_suspend) {
+ sc->sc_flags.status_suspend = 1;
+ sc->sc_flags.change_suspend = 1;
+ /* enable resume interrupt */
+ saf1761_otg_wait_suspend(sc, 0);
+ /* complete root HUB interrupt endpoint */
+ saf1761_otg_root_intr(sc);
+ }
+ }
+ /* poll all active transfers */
+ saf1761_otg_interrupt_poll(sc);
+
+ USB_BUS_UNLOCK(&sc->sc_bus);
+}
+
+static void
+saf1761_otg_setup_standard_chain_sub(struct saf1761_otg_std_temp *temp)
+{
+ struct saf1761_otg_td *td;
+
+ /* get current Transfer Descriptor */
+ td = temp->td_next;
+ temp->td = td;
+
+ /* prepare for next TD */
+ temp->td_next = td->obj_next;
+
+ /* fill out the Transfer Descriptor */
+ td->func = temp->func;
+ td->pc = temp->pc;
+ td->offset = temp->offset;
+ td->remainder = temp->len;
+ td->error_any = 0;
+ td->error_stall = 0;
+ td->set_toggle = 0;
+ td->did_stall = temp->did_stall;
+ td->short_pkt = temp->short_pkt;
+ td->alt_next = temp->setup_alt_next;
+ td->channel = SOTG_HOST_CHANNEL_MAX;
+}
+
+static void
+saf1761_otg_setup_standard_chain(struct usb_xfer *xfer)
+{
+ struct saf1761_otg_std_temp temp;
+ struct saf1761_otg_softc *sc;
+ struct saf1761_otg_td *td;
+ uint32_t x;
+ uint8_t ep_no;
+ uint8_t ep_type;
+ uint8_t need_sync;
+ uint8_t is_host;
+
+ DPRINTFN(9, "addr=%d endpt=%d sumlen=%d speed=%d\n",
+ xfer->address, UE_GET_ADDR(xfer->endpointno),
+ xfer->sumlen, usbd_get_speed(xfer->xroot->udev));
+
+ temp.max_frame_size = xfer->max_frame_size;
+
+ td = xfer->td_start[0];
+ xfer->td_transfer_first = td;
+ xfer->td_transfer_cache = td;
+
+ /* setup temp */
+
+ temp.pc = NULL;
+ temp.td = NULL;
+ temp.td_next = xfer->td_start[0];
+ temp.offset = 0;
+ temp.setup_alt_next = xfer->flags_int.short_frames_ok;
+ temp.did_stall = !xfer->flags_int.control_stall;
+
+ is_host = (xfer->xroot->udev->flags.usb_mode == USB_MODE_HOST);
+
+ sc = SAF1761_OTG_BUS2SC(xfer->xroot->bus);
+ ep_no = (xfer->endpointno & UE_ADDR);
+ ep_type = (xfer->endpoint->edesc->bmAttributes & UE_XFERTYPE);
+
+ /* check if we should prepend a setup message */
+
+ if (xfer->flags_int.control_xfr) {
+ if (xfer->flags_int.control_hdr) {
+
+ if (is_host)
+ temp.func = &saf1761_host_setup_tx;
+ else
+ temp.func = &saf1761_device_setup_rx;
+
+ temp.len = xfer->frlengths[0];
+ temp.pc = xfer->frbuffers + 0;
+ temp.short_pkt = temp.len ? 1 : 0;
+ /* check for last frame */
+ if (xfer->nframes == 1) {
+ /* no STATUS stage yet, SETUP is last */
+ if (xfer->flags_int.control_act)
+ temp.setup_alt_next = 0;
+ }
+ saf1761_otg_setup_standard_chain_sub(&temp);
+ }
+ x = 1;
+ } else {
+ x = 0;
+ }
+
+ if (x != xfer->nframes) {
+ if (xfer->endpointno & UE_DIR_IN) {
+ if (is_host) {
+ if (ep_type == UE_INTERRUPT)
+ temp.func = &saf1761_host_intr_data_rx;
+ else if (ep_type == UE_ISOCHRONOUS)
+ temp.func = &saf1761_host_isoc_data_rx;
+ else
+ temp.func = &saf1761_host_bulk_data_rx;
+ need_sync = 0;
+ } else {
+ temp.func = &saf1761_device_data_tx;
+ need_sync = 1;
+ }
+ } else {
+ if (is_host) {
+ if (ep_type == UE_INTERRUPT)
+ temp.func = &saf1761_host_intr_data_tx;
+ else if (ep_type == UE_ISOCHRONOUS)
+ temp.func = &saf1761_host_isoc_data_tx;
+ else
+ temp.func = &saf1761_host_bulk_data_tx;
+ need_sync = 0;
+ } else {
+ temp.func = &saf1761_device_data_rx;
+ need_sync = 0;
+ }
+ }
+
+ /* setup "pc" pointer */
+ temp.pc = xfer->frbuffers + x;
+ } else {
+ need_sync = 0;
+ }
+
+ while (x != xfer->nframes) {
+
+ /* DATA0 / DATA1 message */
+
+ temp.len = xfer->frlengths[x];
+
+ x++;
+
+ if (x == xfer->nframes) {
+ if (xfer->flags_int.control_xfr) {
+ if (xfer->flags_int.control_act) {
+ temp.setup_alt_next = 0;
+ }
+ } else {
+ temp.setup_alt_next = 0;
+ }
+ }
+ if (temp.len == 0) {
+
+ /* make sure that we send an USB packet */
+
+ temp.short_pkt = 0;
+
+ } else {
+
+ /* regular data transfer */
+
+ temp.short_pkt = (xfer->flags.force_short_xfer) ? 0 : 1;
+ }
+
+ saf1761_otg_setup_standard_chain_sub(&temp);
+
+ if (xfer->flags_int.isochronous_xfr) {
+ temp.offset += temp.len;
+ } else {
+ /* get next Page Cache pointer */
+ temp.pc = xfer->frbuffers + x;
+ }
+ }
+
+ /* check for control transfer */
+ if (xfer->flags_int.control_xfr) {
+ /* always setup a valid "pc" pointer for status and sync */
+ temp.pc = xfer->frbuffers + 0;
+ temp.len = 0;
+ temp.short_pkt = 0;
+ temp.setup_alt_next = 0;
+
+ /* check if we should append a status stage */
+ if (!xfer->flags_int.control_act) {
+
+ /*
+ * Send a DATA1 message and invert the current
+ * endpoint direction.
+ */
+ if (xfer->endpointno & UE_DIR_IN) {
+ if (is_host) {
+ temp.func = &saf1761_host_bulk_data_tx;
+ need_sync = 0;
+ } else {
+ temp.func = &saf1761_device_data_rx;
+ need_sync = 0;
+ }
+ } else {
+ if (is_host) {
+ temp.func = &saf1761_host_bulk_data_rx;
+ need_sync = 0;
+ } else {
+ temp.func = &saf1761_device_data_tx;
+ need_sync = 1;
+ }
+ }
+ temp.len = 0;
+ temp.short_pkt = 0;
+
+ saf1761_otg_setup_standard_chain_sub(&temp);
+
+ /* data toggle should be DATA1 */
+ td = temp.td;
+ td->set_toggle = 1;
+
+ if (need_sync) {
+ /* we need a SYNC point after TX */
+ temp.func = &saf1761_device_data_tx_sync;
+ saf1761_otg_setup_standard_chain_sub(&temp);
+ }
+ }
+ } else {
+ if (need_sync) {
+ temp.pc = xfer->frbuffers + 0;
+ temp.len = 0;
+ temp.short_pkt = 0;
+ temp.setup_alt_next = 0;
+
+ /* we need a SYNC point after TX */
+ temp.func = &saf1761_device_data_tx_sync;
+ saf1761_otg_setup_standard_chain_sub(&temp);
+ }
+ }
+
+ /* must have at least one frame! */
+ td = temp.td;
+ xfer->td_transfer_last = td;
+
+ if (is_host) {
+ /* get first again */
+ td = xfer->td_transfer_first;
+ td->toggle = (xfer->endpoint->toggle_next ? 1 : 0);
+ }
+}
+
+static void
+saf1761_otg_timeout(void *arg)
+{
+ struct usb_xfer *xfer = arg;
+
+ DPRINTF("xfer=%p\n", xfer);
+
+ USB_BUS_LOCK_ASSERT(xfer->xroot->bus, MA_OWNED);
+
+ /* transfer is transferred */
+ saf1761_otg_device_done(xfer, USB_ERR_TIMEOUT);
+}
+
+static void
+saf1761_otg_intr_set(struct usb_xfer *xfer, uint8_t set)
+{
+ struct saf1761_otg_softc *sc = SAF1761_OTG_BUS2SC(xfer->xroot->bus);
+ uint8_t ep_no = (xfer->endpointno & UE_ADDR);
+ uint32_t mask;
+
+ DPRINTFN(15, "endpoint=%d set=%d\n", xfer->endpointno, set);
+
+ if (ep_no == 0) {
+ mask = SOTG_DCINTERRUPT_IEPRX(0) |
+ SOTG_DCINTERRUPT_IEPTX(0) |
+ SOTG_DCINTERRUPT_IEP0SETUP;
+ } else if (xfer->endpointno & UE_DIR_IN) {
+ mask = SOTG_DCINTERRUPT_IEPTX(ep_no);
+ } else {
+ mask = SOTG_DCINTERRUPT_IEPRX(ep_no);
+ }
+
+ if (set)
+ sc->sc_intr_enable |= mask;
+ else
+ sc->sc_intr_enable &= ~mask;
+
+ SAF1761_WRITE_4(sc, SOTG_DCINTERRUPT_EN, sc->sc_intr_enable);
+}
+
+static void
+saf1761_otg_start_standard_chain(struct usb_xfer *xfer)
+{
+ struct saf1761_otg_softc *sc = SAF1761_OTG_BUS2SC(xfer->xroot->bus);
+
+ DPRINTFN(9, "\n");
+
+ /* poll one time */
+ if (saf1761_otg_xfer_do_fifo(sc, xfer)) {
+
+ /*
+ * Only enable the endpoint interrupt when we are
+ * actually waiting for data, hence we are dealing
+ * with level triggered interrupts !
+ */
+ saf1761_otg_intr_set(xfer, 1);
+
+ /* put transfer on interrupt queue */
+ usbd_transfer_enqueue(&xfer->xroot->bus->intr_q, xfer);
+
+ /* start timeout, if any */
+ if (xfer->timeout != 0) {
+ usbd_transfer_timeout_ms(xfer,
+ &saf1761_otg_timeout, xfer->timeout);
+ }
+ }
+}
+
+static void
+saf1761_otg_root_intr(struct saf1761_otg_softc *sc)
+{
+ DPRINTFN(9, "\n");
+
+ USB_BUS_LOCK_ASSERT(&sc->sc_bus, MA_OWNED);
+
+ /* set port bit - we only have one port */
+ sc->sc_hub_idata[0] = 0x02;
+
+ uhub_root_intr(&sc->sc_bus, sc->sc_hub_idata,
+ sizeof(sc->sc_hub_idata));
+}
+
+static usb_error_t
+saf1761_otg_standard_done_sub(struct usb_xfer *xfer)
+{
+ struct saf1761_otg_td *td;
+ uint32_t len;
+ usb_error_t error;
+
+ DPRINTFN(9, "\n");
+
+ td = xfer->td_transfer_cache;
+
+ do {
+ len = td->remainder;
+
+ /* store last data toggle */
+ xfer->endpoint->toggle_next = td->toggle;
+
+ if (xfer->aframes != xfer->nframes) {
+ /*
+ * Verify the length and subtract
+ * the remainder from "frlengths[]":
+ */
+ if (len > xfer->frlengths[xfer->aframes]) {
+ td->error_any = 1;
+ } else {
+ xfer->frlengths[xfer->aframes] -= len;
+ }
+ }
+ /* Check for transfer error */
+ if (td->error_any) {
+ /* the transfer is finished */
+ error = (td->error_stall ?
+ USB_ERR_STALLED : USB_ERR_IOERROR);
+ td = NULL;
+ break;
+ }
+ /* Check for short transfer */
+ if (len > 0) {
+ if (xfer->flags_int.short_frames_ok) {
+ /* follow alt next */
+ if (td->alt_next) {
+ td = td->obj_next;
+ } else {
+ td = NULL;
+ }
+ } else {
+ /* the transfer is finished */
+ td = NULL;
+ }
+ error = 0;
+ break;
+ }
+ td = td->obj_next;
+
+ /* this USB frame is complete */
+ error = 0;
+ break;
+
+ } while (0);
+
+ /* update transfer cache */
+
+ xfer->td_transfer_cache = td;
+
+ return (error);
+}
+
+static void
+saf1761_otg_standard_done(struct usb_xfer *xfer)
+{
+ usb_error_t err = 0;
+
+ DPRINTFN(13, "xfer=%p endpoint=%p transfer done\n",
+ xfer, xfer->endpoint);
+
+ /* reset scanner */
+
+ xfer->td_transfer_cache = xfer->td_transfer_first;
+
+ if (xfer->flags_int.control_xfr) {
+
+ if (xfer->flags_int.control_hdr) {
+
+ err = saf1761_otg_standard_done_sub(xfer);
+ }
+ xfer->aframes = 1;
+
+ if (xfer->td_transfer_cache == NULL) {
+ goto done;
+ }
+ }
+ while (xfer->aframes != xfer->nframes) {
+
+ err = saf1761_otg_standard_done_sub(xfer);
+ xfer->aframes++;
+
+ if (xfer->td_transfer_cache == NULL) {
+ goto done;
+ }
+ }
+
+ if (xfer->flags_int.control_xfr &&
+ !xfer->flags_int.control_act) {
+
+ err = saf1761_otg_standard_done_sub(xfer);
+ }
+done:
+ saf1761_otg_device_done(xfer, err);
+}
+
+/*------------------------------------------------------------------------*
+ * saf1761_otg_device_done
+ *
+ * NOTE: this function can be called more than one time on the
+ * same USB transfer!
+ *------------------------------------------------------------------------*/
+static void
+saf1761_otg_device_done(struct usb_xfer *xfer, usb_error_t error)
+{
+ struct saf1761_otg_softc *sc = SAF1761_OTG_BUS2SC(xfer->xroot->bus);
+
+ USB_BUS_LOCK_ASSERT(xfer->xroot->bus, MA_OWNED);
+
+ DPRINTFN(2, "xfer=%p, endpoint=%p, error=%d\n",
+ xfer, xfer->endpoint, error);
+
+ if (xfer->flags_int.usb_mode == USB_MODE_DEVICE) {
+ saf1761_otg_intr_set(xfer, 0);
+ } else {
+ struct saf1761_otg_td *td;
+
+ td = xfer->td_transfer_first;
+
+ if (td != NULL)
+ saf1761_host_channel_free(sc, td);
+ }
+
+ /* dequeue transfer and start next transfer */
+ usbd_transfer_done(xfer, error);
+}
+
+static void
+saf1761_otg_xfer_stall(struct usb_xfer *xfer)
+{
+ saf1761_otg_device_done(xfer, USB_ERR_STALLED);
+}
+
+static void
+saf1761_otg_set_stall(struct usb_device *udev,
+ struct usb_endpoint *ep, uint8_t *did_stall)
+{
+ struct saf1761_otg_softc *sc;
+ uint8_t ep_no;
+ uint8_t ep_type;
+ uint8_t ep_dir;
+
+ USB_BUS_LOCK_ASSERT(udev->bus, MA_OWNED);
+
+ /* check mode */
+ if (udev->flags.usb_mode != USB_MODE_DEVICE) {
+ /* not supported */
+ return;
+ }
+
+ DPRINTFN(5, "endpoint=%p\n", ep);
+
+ /* set FORCESTALL */
+ sc = SAF1761_OTG_BUS2SC(udev->bus);
+ ep_no = (ep->edesc->bEndpointAddress & UE_ADDR);
+ ep_dir = (ep->edesc->bEndpointAddress & (UE_DIR_IN | UE_DIR_OUT));
+ ep_type = (ep->edesc->bmAttributes & UE_XFERTYPE);
+
+ if (ep_type == UE_CONTROL) {
+ /* should not happen */
+ return;
+ }
+ /* select the correct endpoint */
+ SAF1761_WRITE_1(sc, SOTG_EP_INDEX,
+ (ep_no << SOTG_EP_INDEX_ENDP_INDEX_SHIFT) |
+ ((ep_dir == UE_DIR_IN) ? SOTG_EP_INDEX_DIR_IN :
+ SOTG_EP_INDEX_DIR_OUT));
+
+ /* set stall */
+ SAF1761_WRITE_1(sc, SOTG_CTRL_FUNC, SOTG_CTRL_FUNC_STALL);
+}
+
+static void
+saf1761_otg_clear_stall_sub(struct saf1761_otg_softc *sc,
+ uint8_t ep_no, uint8_t ep_type, uint8_t ep_dir)
+{
+ if (ep_type == UE_CONTROL) {
+ /* clearing stall is not needed */
+ return;
+ }
+ /* select the correct endpoint */
+ SAF1761_WRITE_1(sc, SOTG_EP_INDEX,
+ (ep_no << SOTG_EP_INDEX_ENDP_INDEX_SHIFT) |
+ ((ep_dir == UE_DIR_IN) ? SOTG_EP_INDEX_DIR_IN :
+ SOTG_EP_INDEX_DIR_OUT));
+
+ /* disable endpoint */
+ SAF1761_WRITE_2(sc, SOTG_EP_TYPE, 0);
+ /* enable endpoint again - will clear data toggle */
+ SAF1761_WRITE_2(sc, SOTG_EP_TYPE, ep_type | SOTG_EP_TYPE_ENABLE);
+
+ /* clear buffer */
+ SAF1761_WRITE_1(sc, SOTG_CTRL_FUNC, SOTG_CTRL_FUNC_CLBUF);
+ /* clear stall */
+ SAF1761_WRITE_1(sc, SOTG_CTRL_FUNC, 0);
+}
+
+static void
+saf1761_otg_clear_stall(struct usb_device *udev, struct usb_endpoint *ep)
+{
+ struct saf1761_otg_softc *sc;
+ struct usb_endpoint_descriptor *ed;
+
+ USB_BUS_LOCK_ASSERT(udev->bus, MA_OWNED);
+
+ DPRINTFN(5, "endpoint=%p\n", ep);
+
+ /* check mode */
+ if (udev->flags.usb_mode != USB_MODE_DEVICE) {
+ /* not supported */
+ return;
+ }
+ /* get softc */
+ sc = SAF1761_OTG_BUS2SC(udev->bus);
+
+ /* get endpoint descriptor */
+ ed = ep->edesc;
+
+ /* reset endpoint */
+ saf1761_otg_clear_stall_sub(sc,
+ (ed->bEndpointAddress & UE_ADDR),
+ (ed->bmAttributes & UE_XFERTYPE),
+ (ed->bEndpointAddress & (UE_DIR_IN | UE_DIR_OUT)));
+}
+
+usb_error_t
+saf1761_otg_init(struct saf1761_otg_softc *sc)
+{
+ const struct usb_hw_ep_profile *pf;
+ uint32_t x;
+
+ DPRINTF("\n");
+
+ /* set up the bus structure */
+ sc->sc_bus.usbrev = USB_REV_2_0;
+ sc->sc_bus.methods = &saf1761_otg_bus_methods;
+
+ USB_BUS_LOCK(&sc->sc_bus);
+
+ /* Enable interrupts */
+ sc->sc_hw_mode |= SOTG_HW_MODE_CTRL_GLOBAL_INTR_EN |
+ SOTG_HW_MODE_CTRL_COMN_INT;
+
+ /*
+ * Set correct hardware mode, must be written twice if bus
+ * width is changed:
+ */
+ SAF1761_WRITE_2(sc, SOTG_HW_MODE_CTRL, sc->sc_hw_mode);
+ SAF1761_WRITE_4(sc, SOTG_HW_MODE_CTRL, sc->sc_hw_mode);
+
+ DPRINTF("DCID=0x%08x\n", SAF1761_READ_4(sc, SOTG_DCCHIP_ID));
+
+ /* reset device controller */
+ SAF1761_WRITE_2(sc, SOTG_MODE, SOTG_MODE_SFRESET);
+ SAF1761_WRITE_2(sc, SOTG_MODE, 0);
+
+ /* wait a bit */
+ DELAY(1000);
+
+ /* reset host controller */
+ SAF1761_WRITE_4(sc, SOTG_SW_RESET, SOTG_SW_RESET_HC);
+ SAF1761_WRITE_4(sc, SOTG_USBCMD, SOTG_USBCMD_HCRESET);
+
+ /* wait a bit */
+ DELAY(1000);
+
+ SAF1761_WRITE_4(sc, SOTG_SW_RESET, 0);
+ SAF1761_WRITE_4(sc, SOTG_USBCMD, 0);
+
+ /* wait a bit */
+ DELAY(1000);
+
+ /* do a pulldown */
+ saf1761_otg_pull_down(sc);
+
+ /* wait 10ms for pulldown to stabilise */
+ usb_pause_mtx(&sc->sc_bus.bus_mtx, hz / 100);
+
+ for (x = 1;; x++) {
+
+ saf1761_otg_get_hw_ep_profile(NULL, &pf, x);
+ if (pf == NULL)
+ break;
+
+ /* select the correct endpoint */
+ SAF1761_WRITE_1(sc, SOTG_EP_INDEX,
+ (x << SOTG_EP_INDEX_ENDP_INDEX_SHIFT) |
+ SOTG_EP_INDEX_DIR_IN);
+
+ /* select the maximum packet size */
+ SAF1761_WRITE_2(sc, SOTG_EP_MAXPACKET, pf->max_in_frame_size);
+
+ /* select the correct endpoint */
+ SAF1761_WRITE_1(sc, SOTG_EP_INDEX,
+ (x << SOTG_EP_INDEX_ENDP_INDEX_SHIFT) |
+ SOTG_EP_INDEX_DIR_OUT);
+
+ /* select the maximum packet size */
+ SAF1761_WRITE_2(sc, SOTG_EP_MAXPACKET, pf->max_out_frame_size);
+ }
+
+ /* enable interrupts */
+ SAF1761_WRITE_2(sc, SOTG_MODE, SOTG_MODE_GLINTENA |
+ SOTG_MODE_CLKAON | SOTG_MODE_WKUPCS);
+
+ /* set default values */
+ SAF1761_WRITE_1(sc, SOTG_INTERRUPT_CFG,
+ SOTG_INTERRUPT_CFG_CDBGMOD |
+ SOTG_INTERRUPT_CFG_DDBGMODIN |
+ SOTG_INTERRUPT_CFG_DDBGMODOUT);
+
+ /* enable VBUS and ID interrupt */
+ SAF1761_WRITE_2(sc, SOTG_IRQ_ENABLE_CLR, 0xFFFF);
+ SAF1761_WRITE_2(sc, SOTG_IRQ_ENABLE_SET,
+ SOTG_IRQ_ID | SOTG_IRQ_VBUS_VLD);
+
+ /* enable interrupts */
+ sc->sc_intr_enable = SOTG_DCINTERRUPT_IEVBUS |
+ SOTG_DCINTERRUPT_IEBRST | SOTG_DCINTERRUPT_IESUSP;
+ SAF1761_WRITE_4(sc, SOTG_DCINTERRUPT_EN, sc->sc_intr_enable);
+
+ /* connect ATX port 1 to device controller */
+ SAF1761_WRITE_2(sc, SOTG_CTRL_CLR, 0xFFFF);
+ SAF1761_WRITE_2(sc, SOTG_CTRL_SET, SOTG_CTRL_SW_SEL_HC_DC |
+ SOTG_CTRL_BDIS_ACON_EN);
+
+ /* disable device address */
+ SAF1761_WRITE_1(sc, SOTG_ADDRESS, 0);
+
+ /* enable host controller clock */
+ SAF1761_WRITE_4(sc, SOTG_POWER_DOWN, SOTG_POWER_DOWN_HC_CLK_EN);
+
+ /* wait 10ms for clock */
+ usb_pause_mtx(&sc->sc_bus.bus_mtx, hz / 100);
+
+ /* enable configuration flag */
+ SAF1761_WRITE_4(sc, SOTG_CONFIGFLAG, SOTG_CONFIGFLAG_ENABLE);
+
+ /* clear RAM block */
+ for (x = 0x400; x != 0x10000; x += 4)
+ SAF1761_WRITE_4(sc, x, 0);
+
+ /* start the HC */
+ SAF1761_WRITE_4(sc, SOTG_USBCMD, SOTG_USBCMD_RS);
+
+ /* poll initial VBUS status */
+ saf1761_otg_update_vbus(sc);
+
+ USB_BUS_UNLOCK(&sc->sc_bus);
+
+ /* catch any lost interrupts */
+
+ saf1761_otg_do_poll(&sc->sc_bus);
+
+ return (0); /* success */
+}
+
+void
+saf1761_otg_uninit(struct saf1761_otg_softc *sc)
+{
+ USB_BUS_LOCK(&sc->sc_bus);
+
+ /* disable all interrupts */
+ SAF1761_WRITE_2(sc, SOTG_MODE, 0);
+
+ sc->sc_flags.port_powered = 0;
+ sc->sc_flags.status_vbus = 0;
+ sc->sc_flags.status_bus_reset = 0;
+ sc->sc_flags.status_suspend = 0;
+ sc->sc_flags.change_suspend = 0;
+ sc->sc_flags.change_connect = 1;
+
+ saf1761_otg_pull_down(sc);
+ USB_BUS_UNLOCK(&sc->sc_bus);
+}
+
+static void
+saf1761_otg_suspend(struct saf1761_otg_softc *sc)
+{
+ /* TODO */
+}
+
+static void
+saf1761_otg_resume(struct saf1761_otg_softc *sc)
+{
+ /* TODO */
+}
+
+static void
+saf1761_otg_do_poll(struct usb_bus *bus)
+{
+ struct saf1761_otg_softc *sc = SAF1761_OTG_BUS2SC(bus);
+
+ USB_BUS_LOCK(&sc->sc_bus);
+ saf1761_otg_interrupt_poll(sc);
+ USB_BUS_UNLOCK(&sc->sc_bus);
+}
+
+/*------------------------------------------------------------------------*
+ * saf1761_otg control support
+ * saf1761_otg interrupt support
+ * saf1761_otg bulk support
+ *------------------------------------------------------------------------*/
+static void
+saf1761_otg_device_non_isoc_open(struct usb_xfer *xfer)
+{
+ return;
+}
+
+static void
+saf1761_otg_device_non_isoc_close(struct usb_xfer *xfer)
+{
+ saf1761_otg_device_done(xfer, USB_ERR_CANCELLED);
+}
+
+static void
+saf1761_otg_device_non_isoc_enter(struct usb_xfer *xfer)
+{
+ return;
+}
+
+static void
+saf1761_otg_device_non_isoc_start(struct usb_xfer *xfer)
+{
+ /* setup TDs */
+ saf1761_otg_setup_standard_chain(xfer);
+ saf1761_otg_start_standard_chain(xfer);
+}
+
+static const struct usb_pipe_methods saf1761_otg_non_isoc_methods =
+{
+ .open = saf1761_otg_device_non_isoc_open,
+ .close = saf1761_otg_device_non_isoc_close,
+ .enter = saf1761_otg_device_non_isoc_enter,
+ .start = saf1761_otg_device_non_isoc_start,
+};
+
+/*------------------------------------------------------------------------*
+ * saf1761_otg isochronous support
+ *------------------------------------------------------------------------*/
+static void
+saf1761_otg_device_isoc_open(struct usb_xfer *xfer)
+{
+ return;
+}
+
+static void
+saf1761_otg_device_isoc_close(struct usb_xfer *xfer)
+{
+ saf1761_otg_device_done(xfer, USB_ERR_CANCELLED);
+}
+
+static void
+saf1761_otg_device_isoc_enter(struct usb_xfer *xfer)
+{
+ struct saf1761_otg_softc *sc = SAF1761_OTG_BUS2SC(xfer->xroot->bus);
+ uint32_t temp;
+ uint32_t nframes;
+
+ DPRINTFN(6, "xfer=%p next=%d nframes=%d\n",
+ xfer, xfer->endpoint->isoc_next, xfer->nframes);
+
+ /* get the current frame index - we don't need the high bits */
+
+ nframes = SAF1761_READ_2(sc, SOTG_FRAME_NUM);
+
+ /*
+ * check if the frame index is within the window where the
+ * frames will be inserted
+ */
+ temp = (nframes - xfer->endpoint->isoc_next) & SOTG_FRAME_NUM_SOFR_MASK;
+
+ if ((xfer->endpoint->is_synced == 0) ||
+ (temp < xfer->nframes)) {
+ /*
+ * If there is data underflow or the pipe queue is
+ * empty we schedule the transfer a few frames ahead
+ * of the current frame position. Else two isochronous
+ * transfers might overlap.
+ */
+ xfer->endpoint->isoc_next = (nframes + 3) & SOTG_FRAME_NUM_SOFR_MASK;
+ xfer->endpoint->is_synced = 1;
+ DPRINTFN(3, "start next=%d\n", xfer->endpoint->isoc_next);
+ }
+ /*
+ * compute how many milliseconds the insertion is ahead of the
+ * current frame position:
+ */
+ temp = (xfer->endpoint->isoc_next - nframes) & SOTG_FRAME_NUM_SOFR_MASK;
+
+ /*
+ * pre-compute when the isochronous transfer will be finished:
+ */
+ xfer->isoc_time_complete =
+ usb_isoc_time_expand(&sc->sc_bus, nframes) + temp +
+ xfer->nframes;
+
+ /* compute frame number for next insertion */
+ xfer->endpoint->isoc_next += xfer->nframes;
+
+ /* setup TDs */
+ saf1761_otg_setup_standard_chain(xfer);
+}
+
+static void
+saf1761_otg_device_isoc_start(struct usb_xfer *xfer)
+{
+ /* start TD chain */
+ saf1761_otg_start_standard_chain(xfer);
+}
+
+static const struct usb_pipe_methods saf1761_otg_device_isoc_methods =
+{
+ .open = saf1761_otg_device_isoc_open,
+ .close = saf1761_otg_device_isoc_close,
+ .enter = saf1761_otg_device_isoc_enter,
+ .start = saf1761_otg_device_isoc_start,
+};
+
+/*------------------------------------------------------------------------*
+ * saf1761_otg root control support
+ *------------------------------------------------------------------------*
+ * Simulate a hardware HUB by handling all the necessary requests.
+ *------------------------------------------------------------------------*/
+
+static const struct usb_device_descriptor saf1761_otg_devd = {
+ .bLength = sizeof(struct usb_device_descriptor),
+ .bDescriptorType = UDESC_DEVICE,
+ .bcdUSB = {0x00, 0x02},
+ .bDeviceClass = UDCLASS_HUB,
+ .bDeviceSubClass = UDSUBCLASS_HUB,
+ .bDeviceProtocol = UDPROTO_FSHUB,
+ .bMaxPacketSize = 64,
+ .bcdDevice = {0x00, 0x01},
+ .iManufacturer = 1,
+ .iProduct = 2,
+ .bNumConfigurations = 1,
+};
+
+static const struct usb_device_qualifier saf1761_otg_odevd = {
+ .bLength = sizeof(struct usb_device_qualifier),
+ .bDescriptorType = UDESC_DEVICE_QUALIFIER,
+ .bcdUSB = {0x00, 0x02},
+ .bDeviceClass = UDCLASS_HUB,
+ .bDeviceSubClass = UDSUBCLASS_HUB,
+ .bDeviceProtocol = UDPROTO_FSHUB,
+ .bMaxPacketSize0 = 0,
+ .bNumConfigurations = 0,
+};
+
+static const struct saf1761_otg_config_desc saf1761_otg_confd = {
+ .confd = {
+ .bLength = sizeof(struct usb_config_descriptor),
+ .bDescriptorType = UDESC_CONFIG,
+ .wTotalLength[0] = sizeof(saf1761_otg_confd),
+ .bNumInterface = 1,
+ .bConfigurationValue = 1,
+ .iConfiguration = 0,
+ .bmAttributes = UC_SELF_POWERED,
+ .bMaxPower = 0,
+ },
+ .ifcd = {
+ .bLength = sizeof(struct usb_interface_descriptor),
+ .bDescriptorType = UDESC_INTERFACE,
+ .bNumEndpoints = 1,
+ .bInterfaceClass = UICLASS_HUB,
+ .bInterfaceSubClass = UISUBCLASS_HUB,
+ .bInterfaceProtocol = 0,
+ },
+
+ .endpd = {
+ .bLength = sizeof(struct usb_endpoint_descriptor),
+ .bDescriptorType = UDESC_ENDPOINT,
+ .bEndpointAddress = (UE_DIR_IN | SAF1761_OTG_INTR_ENDPT),
+ .bmAttributes = UE_INTERRUPT,
+ .wMaxPacketSize[0] = 8,
+ .bInterval = 255,
+ },
+};
+
+#define HSETW(ptr, val) ptr = { (uint8_t)(val), (uint8_t)((val) >> 8) }
+
+static const struct usb_hub_descriptor_min saf1761_otg_hubd = {
+ .bDescLength = sizeof(saf1761_otg_hubd),
+ .bDescriptorType = UDESC_HUB,
+ .bNbrPorts = SOTG_NUM_PORTS,
+ HSETW(.wHubCharacteristics, (UHD_PWR_NO_SWITCH | UHD_OC_INDIVIDUAL)),
+ .bPwrOn2PwrGood = 50,
+ .bHubContrCurrent = 0,
+ .DeviceRemovable = {0}, /* port is removable */
+};
+
+#define STRING_VENDOR \
+ "N\0X\0P"
+
+#define STRING_PRODUCT \
+ "D\0C\0I\0 \0R\0o\0o\0t\0 \0H\0U\0B"
+
+USB_MAKE_STRING_DESC(STRING_VENDOR, saf1761_otg_vendor);
+USB_MAKE_STRING_DESC(STRING_PRODUCT, saf1761_otg_product);
+
+static usb_error_t
+saf1761_otg_roothub_exec(struct usb_device *udev,
+ struct usb_device_request *req, const void **pptr, uint16_t *plength)
+{
+ struct saf1761_otg_softc *sc = SAF1761_OTG_BUS2SC(udev->bus);
+ const void *ptr;
+ uint16_t len;
+ uint16_t value;
+ uint16_t index;
+ uint32_t temp;
+ uint32_t i;
+ usb_error_t err;
+
+ USB_BUS_LOCK_ASSERT(&sc->sc_bus, MA_OWNED);
+
+ /* buffer reset */
+ ptr = (const void *)&sc->sc_hub_temp;
+ len = 0;
+ err = 0;
+
+ value = UGETW(req->wValue);
+ index = UGETW(req->wIndex);
+
+ /* demultiplex the control request */
+
+ switch (req->bmRequestType) {
+ case UT_READ_DEVICE:
+ switch (req->bRequest) {
+ case UR_GET_DESCRIPTOR:
+ goto tr_handle_get_descriptor;
+ case UR_GET_CONFIG:
+ goto tr_handle_get_config;
+ case UR_GET_STATUS:
+ goto tr_handle_get_status;
+ default:
+ goto tr_stalled;
+ }
+ break;
+
+ case UT_WRITE_DEVICE:
+ switch (req->bRequest) {
+ case UR_SET_ADDRESS:
+ goto tr_handle_set_address;
+ case UR_SET_CONFIG:
+ goto tr_handle_set_config;
+ case UR_CLEAR_FEATURE:
+ goto tr_valid; /* nop */
+ case UR_SET_DESCRIPTOR:
+ goto tr_valid; /* nop */
+ case UR_SET_FEATURE:
+ default:
+ goto tr_stalled;
+ }
+ break;
+
+ case UT_WRITE_ENDPOINT:
+ switch (req->bRequest) {
+ case UR_CLEAR_FEATURE:
+ switch (UGETW(req->wValue)) {
+ case UF_ENDPOINT_HALT:
+ goto tr_handle_clear_halt;
+ case UF_DEVICE_REMOTE_WAKEUP:
+ goto tr_handle_clear_wakeup;
+ default:
+ goto tr_stalled;
+ }
+ break;
+ case UR_SET_FEATURE:
+ switch (UGETW(req->wValue)) {
+ case UF_ENDPOINT_HALT:
+ goto tr_handle_set_halt;
+ case UF_DEVICE_REMOTE_WAKEUP:
+ goto tr_handle_set_wakeup;
+ default:
+ goto tr_stalled;
+ }
+ break;
+ case UR_SYNCH_FRAME:
+ goto tr_valid; /* nop */
+ default:
+ goto tr_stalled;
+ }
+ break;
+
+ case UT_READ_ENDPOINT:
+ switch (req->bRequest) {
+ case UR_GET_STATUS:
+ goto tr_handle_get_ep_status;
+ default:
+ goto tr_stalled;
+ }
+ break;
+
+ case UT_WRITE_INTERFACE:
+ switch (req->bRequest) {
+ case UR_SET_INTERFACE:
+ goto tr_handle_set_interface;
+ case UR_CLEAR_FEATURE:
+ goto tr_valid; /* nop */
+ case UR_SET_FEATURE:
+ default:
+ goto tr_stalled;
+ }
+ break;
+
+ case UT_READ_INTERFACE:
+ switch (req->bRequest) {
+ case UR_GET_INTERFACE:
+ goto tr_handle_get_interface;
+ case UR_GET_STATUS:
+ goto tr_handle_get_iface_status;
+ default:
+ goto tr_stalled;
+ }
+ break;
+
+ case UT_WRITE_CLASS_INTERFACE:
+ case UT_WRITE_VENDOR_INTERFACE:
+ /* XXX forward */
+ break;
+
+ case UT_READ_CLASS_INTERFACE:
+ case UT_READ_VENDOR_INTERFACE:
+ /* XXX forward */
+ break;
+
+ case UT_WRITE_CLASS_DEVICE:
+ switch (req->bRequest) {
+ case UR_CLEAR_FEATURE:
+ goto tr_valid;
+ case UR_SET_DESCRIPTOR:
+ case UR_SET_FEATURE:
+ break;
+ default:
+ goto tr_stalled;
+ }
+ break;
+
+ case UT_WRITE_CLASS_OTHER:
+ switch (req->bRequest) {
+ case UR_CLEAR_FEATURE:
+ if (index == SOTG_HOST_PORT_NUM)
+ goto tr_handle_clear_port_feature_host;
+ else if (index == SOTG_DEVICE_PORT_NUM)
+ goto tr_handle_clear_port_feature_device;
+ else
+ goto tr_stalled;
+ case UR_SET_FEATURE:
+ if (index == SOTG_HOST_PORT_NUM)
+ goto tr_handle_set_port_feature_host;
+ else if (index == SOTG_DEVICE_PORT_NUM)
+ goto tr_handle_set_port_feature_device;
+ else
+ goto tr_stalled;
+ case UR_CLEAR_TT_BUFFER:
+ case UR_RESET_TT:
+ case UR_STOP_TT:
+ goto tr_valid;
+
+ default:
+ goto tr_stalled;
+ }
+ break;
+
+ case UT_READ_CLASS_OTHER:
+ switch (req->bRequest) {
+ case UR_GET_TT_STATE:
+ goto tr_handle_get_tt_state;
+ case UR_GET_STATUS:
+ if (index == SOTG_HOST_PORT_NUM)
+ goto tr_handle_get_port_status_host;
+ else if (index == SOTG_DEVICE_PORT_NUM)
+ goto tr_handle_get_port_status_device;
+ else
+ goto tr_stalled;
+ default:
+ goto tr_stalled;
+ }
+ break;
+
+ case UT_READ_CLASS_DEVICE:
+ switch (req->bRequest) {
+ case UR_GET_DESCRIPTOR:
+ goto tr_handle_get_class_descriptor;
+ case UR_GET_STATUS:
+ goto tr_handle_get_class_status;
+
+ default:
+ goto tr_stalled;
+ }
+ break;
+ default:
+ goto tr_stalled;
+ }
+ goto tr_valid;
+
+tr_handle_get_descriptor:
+ switch (value >> 8) {
+ case UDESC_DEVICE:
+ if (value & 0xff)
+ goto tr_stalled;
+ len = sizeof(saf1761_otg_devd);
+ ptr = (const void *)&saf1761_otg_devd;
+ goto tr_valid;
+ case UDESC_DEVICE_QUALIFIER:
+ if (value & 0xff)
+ goto tr_stalled;
+ len = sizeof(saf1761_otg_odevd);
+ ptr = (const void *)&saf1761_otg_odevd;
+ goto tr_valid;
+ case UDESC_CONFIG:
+ if (value & 0xff)
+ goto tr_stalled;
+ len = sizeof(saf1761_otg_confd);
+ ptr = (const void *)&saf1761_otg_confd;
+ goto tr_valid;
+ case UDESC_STRING:
+ switch (value & 0xff) {
+ case 0: /* Language table */
+ len = sizeof(usb_string_lang_en);
+ ptr = (const void *)&usb_string_lang_en;
+ goto tr_valid;
+
+ case 1: /* Vendor */
+ len = sizeof(saf1761_otg_vendor);
+ ptr = (const void *)&saf1761_otg_vendor;
+ goto tr_valid;
+
+ case 2: /* Product */
+ len = sizeof(saf1761_otg_product);
+ ptr = (const void *)&saf1761_otg_product;
+ goto tr_valid;
+ default:
+ break;
+ }
+ break;
+ default:
+ goto tr_stalled;
+ }
+ goto tr_stalled;
+
+tr_handle_get_config:
+ len = 1;
+ sc->sc_hub_temp.wValue[0] = sc->sc_conf;
+ goto tr_valid;
+
+tr_handle_get_status:
+ len = 2;
+ USETW(sc->sc_hub_temp.wValue, UDS_SELF_POWERED);
+ goto tr_valid;
+
+tr_handle_set_address:
+ if (value & 0xFF00)
+ goto tr_stalled;
+
+ sc->sc_rt_addr = value;
+ goto tr_valid;
+
+tr_handle_set_config:
+ if (value >= 2)
+ goto tr_stalled;
+ sc->sc_conf = value;
+ goto tr_valid;
+
+tr_handle_get_interface:
+ len = 1;
+ sc->sc_hub_temp.wValue[0] = 0;
+ goto tr_valid;
+
+tr_handle_get_tt_state:
+tr_handle_get_class_status:
+tr_handle_get_iface_status:
+tr_handle_get_ep_status:
+ len = 2;
+ USETW(sc->sc_hub_temp.wValue, 0);
+ goto tr_valid;
+
+tr_handle_set_halt:
+tr_handle_set_interface:
+tr_handle_set_wakeup:
+tr_handle_clear_wakeup:
+tr_handle_clear_halt:
+ goto tr_valid;
+
+tr_handle_clear_port_feature_device:
+ DPRINTFN(9, "UR_CLEAR_FEATURE on port %d\n", index);
+
+ switch (value) {
+ case UHF_PORT_SUSPEND:
+ saf1761_otg_wakeup_peer(sc);
+ break;
+
+ case UHF_PORT_ENABLE:
+ sc->sc_flags.port_enabled = 0;
+ break;
+
+ case UHF_PORT_TEST:
+ case UHF_PORT_INDICATOR:
+ case UHF_C_PORT_ENABLE:
+ case UHF_C_PORT_OVER_CURRENT:
+ case UHF_C_PORT_RESET:
+ /* nops */
+ break;
+ case UHF_PORT_POWER:
+ sc->sc_flags.port_powered = 0;
+ saf1761_otg_pull_down(sc);
+ break;
+ case UHF_C_PORT_CONNECTION:
+ sc->sc_flags.change_connect = 0;
+ break;
+ case UHF_C_PORT_SUSPEND:
+ sc->sc_flags.change_suspend = 0;
+ break;
+ default:
+ err = USB_ERR_IOERROR;
+ goto tr_valid;
+ }
+ goto tr_valid;
+
+tr_handle_clear_port_feature_host:
+ DPRINTFN(9, "UR_CLEAR_FEATURE on port %d\n", index);
+
+ temp = SAF1761_READ_4(sc, SOTG_PORTSC1);
+
+ switch (value) {
+ case UHF_PORT_ENABLE:
+ SAF1761_WRITE_4(sc, SOTG_PORTSC1, temp & ~SOTG_PORTSC1_PED);
+ break;
+ case UHF_PORT_SUSPEND:
+ if ((temp & SOTG_PORTSC1_SUSP) && (!(temp & SOTG_PORTSC1_FPR)))
+ SAF1761_WRITE_4(sc, SOTG_PORTSC1, temp | SOTG_PORTSC1_FPR);
+
+ /* wait 20ms for resume sequence to complete */
+ usb_pause_mtx(&sc->sc_bus.bus_mtx, hz / 50);
+
+ SAF1761_WRITE_4(sc, SOTG_PORTSC1, temp & ~(SOTG_PORTSC1_SUSP |
+ SOTG_PORTSC1_FPR | SOTG_PORTSC1_LS /* High Speed */ ));
+
+ /* 4ms settle time */
+ usb_pause_mtx(&sc->sc_bus.bus_mtx, hz / 250);
+ break;
+ case UHF_PORT_INDICATOR:
+ SAF1761_WRITE_4(sc, SOTG_PORTSC1, temp & ~SOTG_PORTSC1_PIC);
+ break;
+ case UHF_PORT_TEST:
+ case UHF_C_PORT_ENABLE:
+ case UHF_C_PORT_OVER_CURRENT:
+ case UHF_C_PORT_RESET:
+ case UHF_C_PORT_SUSPEND:
+ /* NOPs */
+ break;
+ case UHF_PORT_POWER:
+ SAF1761_WRITE_4(sc, SOTG_PORTSC1, temp & ~SOTG_PORTSC1_PP);
+ break;
+ case UHF_C_PORT_CONNECTION:
+ SAF1761_WRITE_4(sc, SOTG_PORTSC1, temp & ~SOTG_PORTSC1_ECSC);
+ break;
+ default:
+ err = USB_ERR_IOERROR;
+ goto tr_valid;
+ }
+ goto tr_valid;
+
+tr_handle_set_port_feature_device:
+ DPRINTFN(9, "UR_SET_FEATURE on port %d\n", index);
+
+ switch (value) {
+ case UHF_PORT_ENABLE:
+ sc->sc_flags.port_enabled = 1;
+ break;
+ case UHF_PORT_SUSPEND:
+ case UHF_PORT_RESET:
+ case UHF_PORT_TEST:
+ case UHF_PORT_INDICATOR:
+ /* nops */
+ break;
+ case UHF_PORT_POWER:
+ sc->sc_flags.port_powered = 1;
+ break;
+ default:
+ err = USB_ERR_IOERROR;
+ goto tr_valid;
+ }
+ goto tr_valid;
+
+tr_handle_set_port_feature_host:
+ DPRINTFN(9, "UR_SET_FEATURE on port %d\n", index);
+
+ temp = SAF1761_READ_4(sc, SOTG_PORTSC1);
+
+ switch (value) {
+ case UHF_PORT_ENABLE:
+ SAF1761_WRITE_4(sc, SOTG_PORTSC1, temp | SOTG_PORTSC1_PED);
+ break;
+ case UHF_PORT_SUSPEND:
+ SAF1761_WRITE_4(sc, SOTG_PORTSC1, temp | SOTG_PORTSC1_SUSP);
+ break;
+ case UHF_PORT_RESET:
+ DPRINTFN(6, "reset port %d\n", index);
+
+ /* Start reset sequence. */
+ temp &= ~(SOTG_PORTSC1_PED | SOTG_PORTSC1_PR);
+
+ SAF1761_WRITE_4(sc, SOTG_PORTSC1, temp | SOTG_PORTSC1_PR);
+
+ /* Wait for reset to complete. */
+ usb_pause_mtx(&sc->sc_bus.bus_mtx,
+ USB_MS_TO_TICKS(usb_port_root_reset_delay));
+
+ SAF1761_WRITE_4(sc, SOTG_PORTSC1, temp);
+
+ /* Wait for HC to complete reset. */
+ usb_pause_mtx(&sc->sc_bus.bus_mtx, USB_MS_TO_TICKS(2));
+
+ temp = SAF1761_READ_4(sc, SOTG_PORTSC1);
+
+ DPRINTF("After reset, status=0x%08x\n", temp);
+ if (temp & SOTG_PORTSC1_PR) {
+ device_printf(sc->sc_bus.bdev, "port reset timeout\n");
+ err = USB_ERR_TIMEOUT;
+ goto tr_valid;
+ }
+ if (!(temp & SOTG_PORTSC1_PED)) {
+ /* Not a high speed device, give up ownership.*/
+ SAF1761_WRITE_4(sc, SOTG_PORTSC1, temp | SOTG_PORTSC1_PO);
+ break;
+ }
+ sc->sc_isreset = 1;
+ DPRINTF("port %d reset, status = 0x%08x\n", index, temp);
+ break;
+ case UHF_PORT_POWER:
+ DPRINTFN(3, "set port power %d\n", index);
+ SAF1761_WRITE_4(sc, SOTG_PORTSC1, temp | SOTG_PORTSC1_PP);
+ break;
+
+ case UHF_PORT_TEST:
+ DPRINTFN(3, "set port test %d\n", index);
+ break;
+
+ case UHF_PORT_INDICATOR:
+ DPRINTFN(3, "set port ind %d\n", index);
+ SAF1761_WRITE_4(sc, SOTG_PORTSC1, temp | SOTG_PORTSC1_PIC);
+ break;
+ default:
+ err = USB_ERR_IOERROR;
+ goto tr_valid;
+ }
+ goto tr_valid;
+
+tr_handle_get_port_status_device:
+
+ DPRINTFN(9, "UR_GET_PORT_STATUS on port %d\n", index);
+
+ if (sc->sc_flags.status_vbus) {
+ saf1761_otg_pull_up(sc);
+ } else {
+ saf1761_otg_pull_down(sc);
+ }
+
+ /* Select FULL-speed and Device Side Mode */
+
+ value = UPS_PORT_MODE_DEVICE;
+
+ if (sc->sc_flags.port_powered)
+ value |= UPS_PORT_POWER;
+
+ if (sc->sc_flags.port_enabled)
+ value |= UPS_PORT_ENABLED;
+
+ if (sc->sc_flags.status_vbus &&
+ sc->sc_flags.status_bus_reset)
+ value |= UPS_CURRENT_CONNECT_STATUS;
+
+ if (sc->sc_flags.status_suspend)
+ value |= UPS_SUSPEND;
+
+ USETW(sc->sc_hub_temp.ps.wPortStatus, value);
+
+ value = 0;
+
+ if (sc->sc_flags.change_connect)
+ value |= UPS_C_CONNECT_STATUS;
+
+ if (sc->sc_flags.change_suspend)
+ value |= UPS_C_SUSPEND;
+
+ USETW(sc->sc_hub_temp.ps.wPortChange, value);
+ len = sizeof(sc->sc_hub_temp.ps);
+ goto tr_valid;
+
+tr_handle_get_port_status_host:
+
+ temp = SAF1761_READ_4(sc, SOTG_PORTSC1);
+
+ DPRINTFN(9, "port status=0x%04x\n", temp);
+ i = UPS_HIGH_SPEED;
+
+ if (temp & SOTG_PORTSC1_ECCS)
+ i |= UPS_CURRENT_CONNECT_STATUS;
+ if (temp & SOTG_PORTSC1_PED)
+ i |= UPS_PORT_ENABLED;
+ if ((temp & SOTG_PORTSC1_SUSP) && !(temp & SOTG_PORTSC1_FPR))
+ i |= UPS_SUSPEND;
+ if (temp & SOTG_PORTSC1_PR)
+ i |= UPS_RESET;
+ if (temp & SOTG_PORTSC1_PP)
+ i |= UPS_PORT_POWER;
+
+ USETW(sc->sc_hub_temp.ps.wPortStatus, i);
+ i = 0;
+
+ if (temp & SOTG_PORTSC1_ECSC)
+ i |= UPS_C_CONNECT_STATUS;
+ if (temp & SOTG_PORTSC1_FPR)
+ i |= UPS_C_SUSPEND;
+ if (sc->sc_isreset)
+ i |= UPS_C_PORT_RESET;
+ USETW(sc->sc_hub_temp.ps.wPortChange, i);
+ len = sizeof(sc->sc_hub_temp.ps);
+ goto tr_valid;
+
+tr_handle_get_class_descriptor:
+ if (value & 0xFF)
+ goto tr_stalled;
+ ptr = (const void *)&saf1761_otg_hubd;
+ len = sizeof(saf1761_otg_hubd);
+ goto tr_valid;
+
+tr_stalled:
+ err = USB_ERR_STALLED;
+tr_valid:
+ *plength = len;
+ *pptr = ptr;
+ return (err);
+}
+
+static void
+saf1761_otg_xfer_setup(struct usb_setup_params *parm)
+{
+ struct saf1761_otg_softc *sc;
+ struct usb_xfer *xfer;
+ void *last_obj;
+ uint32_t dw1;
+ uint32_t ntd;
+ uint32_t n;
+ uint8_t ep_no;
+ uint8_t ep_type;
+
+ sc = SAF1761_OTG_BUS2SC(parm->udev->bus);
+ xfer = parm->curr_xfer;
+
+ /*
+ * NOTE: This driver does not use any of the parameters that
+ * are computed from the following values. Just set some
+ * reasonable dummies:
+ */
+ parm->hc_max_packet_size = 0x500;
+ parm->hc_max_packet_count = 1;
+ parm->hc_max_frame_size = 0x500;
+
+ usbd_transfer_setup_sub(parm);
+
+ /*
+ * Compute maximum number of TDs:
+ */
+ ep_type = (xfer->endpoint->edesc->bmAttributes & UE_XFERTYPE);
+
+ if (ep_type == UE_CONTROL) {
+
+ ntd = xfer->nframes + 1 /* STATUS */ + 1 /* SYNC */ ;
+
+ } else {
+ ntd = xfer->nframes + 1 /* SYNC */ ;
+ }
+
+ /*
+ * check if "usbd_transfer_setup_sub" set an error
+ */
+ if (parm->err)
+ return;
+
+ /*
+ * allocate transfer descriptors
+ */
+ last_obj = NULL;
+
+ ep_no = xfer->endpointno & UE_ADDR;
+
+ /*
+ * Check profile stuff
+ */
+ if (parm->udev->flags.usb_mode == USB_MODE_DEVICE) {
+ const struct usb_hw_ep_profile *pf;
+
+ saf1761_otg_get_hw_ep_profile(parm->udev, &pf, ep_no);
+
+ if (pf == NULL) {
+ /* should not happen */
+ parm->err = USB_ERR_INVAL;
+ return;
+ }
+ }
+
+ dw1 = (xfer->address << 3) | (ep_type << 12);
+
+ switch (parm->udev->speed) {
+ case USB_SPEED_FULL:
+ case USB_SPEED_LOW:
+ /* check if root HUB port is running High Speed */
+ if (parm->udev->parent_hs_hub != NULL) {
+ dw1 |= (1 << 14);
+ dw1 |= (parm->udev->hs_port_no << 18);
+ dw1 |= (parm->udev->hs_hub_addr << 25);
+ if (parm->udev->speed == USB_SPEED_LOW)
+ dw1 |= (1 << 17);
+ }
+ break;
+ default:
+ break;
+ }
+
+ /* align data */
+ parm->size[0] += ((-parm->size[0]) & (USB_HOST_ALIGN - 1));
+
+ for (n = 0; n != ntd; n++) {
+
+ struct saf1761_otg_td *td;
+
+ if (parm->buf) {
+
+ td = USB_ADD_BYTES(parm->buf, parm->size[0]);
+
+ /* init TD */
+ td->max_packet_size = xfer->max_packet_size;
+ td->ep_index = ep_no;
+ td->ep_type = ep_type;
+ td->dw1_value = dw1;
+
+ td->obj_next = last_obj;
+
+ last_obj = td;
+ }
+ parm->size[0] += sizeof(*td);
+ }
+
+ xfer->td_start[0] = last_obj;
+}
+
+static void
+saf1761_otg_xfer_unsetup(struct usb_xfer *xfer)
+{
+}
+
+static void
+saf1761_otg_ep_init(struct usb_device *udev, struct usb_endpoint_descriptor *edesc,
+ struct usb_endpoint *ep)
+{
+ struct saf1761_otg_softc *sc = SAF1761_OTG_BUS2SC(udev->bus);
+
+ DPRINTFN(2, "endpoint=%p, addr=%d, endpt=%d, mode=%d (%d)\n",
+ ep, udev->address,
+ edesc->bEndpointAddress, udev->flags.usb_mode,
+ sc->sc_rt_addr);
+
+ if (udev->parent_hub == NULL) {
+ /* root HUB has special endpoint handling */
+ return;
+ }
+
+ if (udev->flags.usb_mode == USB_MODE_DEVICE) {
+ if (udev->speed != USB_SPEED_FULL &&
+ udev->speed != USB_SPEED_HIGH) {
+ /* not supported */
+ return;
+ }
+ switch (edesc->bmAttributes & UE_XFERTYPE) {
+ case UE_ISOCHRONOUS:
+ ep->methods = &saf1761_otg_device_isoc_methods;
+ break;
+ default:
+ ep->methods = &saf1761_otg_non_isoc_methods;
+ break;
+ }
+ } else {
+ switch (edesc->bmAttributes & UE_XFERTYPE) {
+ case UE_CONTROL:
+ case UE_BULK:
+ ep->methods = &saf1761_otg_non_isoc_methods;
+ break;
+ default:
+ /* TODO */
+ break;
+ }
+ }
+}
+
+static void
+saf1761_otg_set_hw_power_sleep(struct usb_bus *bus, uint32_t state)
+{
+ struct saf1761_otg_softc *sc = SAF1761_OTG_BUS2SC(bus);
+
+ switch (state) {
+ case USB_HW_POWER_SUSPEND:
+ saf1761_otg_suspend(sc);
+ break;
+ case USB_HW_POWER_SHUTDOWN:
+ saf1761_otg_uninit(sc);
+ break;
+ case USB_HW_POWER_RESUME:
+ saf1761_otg_resume(sc);
+ break;
+ default:
+ break;
+ }
+}
+
+static const struct usb_bus_methods saf1761_otg_bus_methods =
+{
+ .endpoint_init = &saf1761_otg_ep_init,
+ .xfer_setup = &saf1761_otg_xfer_setup,
+ .xfer_unsetup = &saf1761_otg_xfer_unsetup,
+ .get_hw_ep_profile = &saf1761_otg_get_hw_ep_profile,
+ .xfer_stall = &saf1761_otg_xfer_stall,
+ .set_stall = &saf1761_otg_set_stall,
+ .clear_stall = &saf1761_otg_clear_stall,
+ .roothub_exec = &saf1761_otg_roothub_exec,
+ .xfer_poll = &saf1761_otg_do_poll,
+ .set_hw_power_sleep = saf1761_otg_set_hw_power_sleep,
+};
diff --git a/sys/dev/usb/controller/saf1761_otg.h b/sys/dev/usb/controller/saf1761_otg.h
new file mode 100644
index 0000000..55c4542
--- /dev/null
+++ b/sys/dev/usb/controller/saf1761_otg.h
@@ -0,0 +1,157 @@
+/* $FreeBSD$ */
+/*-
+ * Copyright (c) 2014 Hans Petter Selasky <hselasky@FreeBSD.org>
+ * All rights reserved.
+ *
+ * This software was developed by SRI International and the University of
+ * Cambridge Computer Laboratory under DARPA/AFRL contract (FA8750-10-C-0237)
+ * ("CTSRD"), as part of the DARPA CRASH research programme.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ */
+
+#ifndef _SAF1761_OTG_H_
+#define _SAF1761_OTG_H_
+
+#define SOTG_MAX_DEVICES (USB_MIN_DEVICES + 1)
+#define SOTG_FS_MAX_PACKET_SIZE 64
+#define SOTG_HS_MAX_PACKET_SIZE 512
+#define SOTG_NUM_PORTS 2 /* one Device and one Host port */
+#define SOTG_HOST_PORT_NUM 1
+#define SOTG_DEVICE_PORT_NUM 2
+#define SOTG_HOST_CHANNEL_MAX (3 * 32)
+
+#define SAF1761_READ_1(sc, reg) \
+ bus_space_read_1((sc)->sc_io_tag, (sc)->sc_io_hdl, reg)
+#define SAF1761_READ_2(sc, reg) \
+ bus_space_read_2((sc)->sc_io_tag, (sc)->sc_io_hdl, reg)
+#define SAF1761_READ_4(sc, reg) \
+ bus_space_read_4((sc)->sc_io_tag, (sc)->sc_io_hdl, reg)
+
+#define SAF1761_WRITE_1(sc, reg, data) \
+ bus_space_write_1((sc)->sc_io_tag, (sc)->sc_io_hdl, reg, data)
+#define SAF1761_WRITE_2(sc, reg, data) \
+ bus_space_write_2((sc)->sc_io_tag, (sc)->sc_io_hdl, reg, data)
+#define SAF1761_WRITE_4(sc, reg, data) \
+ bus_space_write_4((sc)->sc_io_tag, (sc)->sc_io_hdl, reg, data)
+
+struct saf1761_otg_softc;
+struct saf1761_otg_td;
+
+typedef uint8_t (saf1761_otg_cmd_t)(struct saf1761_otg_softc *, struct saf1761_otg_td *td);
+
+struct saf1761_otg_td {
+ struct saf1761_otg_td *obj_next;
+ saf1761_otg_cmd_t *func;
+ struct usb_page_cache *pc;
+ uint32_t offset;
+ uint32_t remainder;
+ uint32_t dw1_value;
+ uint16_t max_packet_size;
+ uint8_t ep_index;
+ uint8_t ep_type;
+ uint8_t channel;
+ uint8_t error_any:1;
+ uint8_t error_stall:1;
+ uint8_t alt_next:1;
+ uint8_t short_pkt:1;
+ uint8_t did_stall:1;
+ uint8_t toggle:1;
+ uint8_t set_toggle:1;
+};
+
+struct saf1761_otg_std_temp {
+ saf1761_otg_cmd_t *func;
+ struct usb_page_cache *pc;
+ struct saf1761_otg_td *td;
+ struct saf1761_otg_td *td_next;
+ uint32_t len;
+ uint32_t offset;
+ uint16_t max_frame_size;
+ uint8_t short_pkt;
+ /*
+ * short_pkt = 0: transfer should be short terminated
+ * short_pkt = 1: transfer should not be short terminated
+ */
+ uint8_t setup_alt_next;
+ uint8_t did_stall;
+};
+
+struct saf1761_otg_config_desc {
+ struct usb_config_descriptor confd;
+ struct usb_interface_descriptor ifcd;
+ struct usb_endpoint_descriptor endpd;
+} __packed;
+
+union saf1761_otg_hub_temp {
+ uWord wValue;
+ struct usb_port_status ps;
+};
+
+struct saf1761_otg_flags {
+ uint8_t change_connect:1;
+ uint8_t change_suspend:1;
+ uint8_t status_suspend:1; /* set if suspended */
+ uint8_t status_vbus:1; /* set if present */
+ uint8_t status_bus_reset:1; /* set if reset complete */
+ uint8_t clocks_off:1;
+ uint8_t port_powered:1;
+ uint8_t port_enabled:1;
+ uint8_t d_pulled_up:1;
+};
+
+struct saf1761_otg_softc {
+ struct usb_bus sc_bus;
+ union saf1761_otg_hub_temp sc_hub_temp;
+
+ struct usb_device *sc_devices[SOTG_MAX_DEVICES];
+ struct resource *sc_io_res;
+ struct resource *sc_irq_res;
+ void *sc_intr_hdl;
+ bus_size_t sc_io_size;
+ bus_space_tag_t sc_io_tag;
+ bus_space_handle_t sc_io_hdl;
+
+ uint32_t sc_host_async_map;
+ uint32_t sc_host_intr_map;
+ uint32_t sc_host_isoc_map;
+ uint32_t sc_intr_enable; /* enabled interrupts */
+ uint32_t sc_hw_mode; /* hardware mode */
+
+ uint8_t sc_bounce_buffer[1024] __aligned(4);
+ uint8_t sc_rt_addr; /* root HUB address */
+ uint8_t sc_dv_addr; /* device address */
+ uint8_t sc_conf; /* root HUB config */
+ uint8_t sc_isreset; /* host mode */
+
+ uint8_t sc_hub_idata[1];
+
+ struct saf1761_otg_flags sc_flags;
+};
+
+/* prototypes */
+
+usb_error_t saf1761_otg_init(struct saf1761_otg_softc *sc);
+void saf1761_otg_uninit(struct saf1761_otg_softc *sc);
+void saf1761_otg_interrupt(struct saf1761_otg_softc *sc);
+
+#endif /* _SAF1761_OTG_H_ */
diff --git a/sys/dev/usb/controller/saf1761_otg_fdt.c b/sys/dev/usb/controller/saf1761_otg_fdt.c
new file mode 100644
index 0000000..b0119d9
--- /dev/null
+++ b/sys/dev/usb/controller/saf1761_otg_fdt.c
@@ -0,0 +1,253 @@
+/* $FreeBSD$ */
+/*-
+ * Copyright (c) 2014 Hans Petter Selasky <hselasky@FreeBSD.org>
+ * All rights reserved.
+ *
+ * This software was developed by SRI International and the University of
+ * Cambridge Computer Laboratory under DARPA/AFRL contract (FA8750-10-C-0237)
+ * ("CTSRD"), as part of the DARPA CRASH research programme.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ */
+
+#ifdef USB_GLOBAL_INCLUDE_FILE
+#include USB_GLOBAL_INCLUDE_FILE
+#else
+#include <sys/stdint.h>
+#include <sys/stddef.h>
+#include <sys/param.h>
+#include <sys/queue.h>
+#include <sys/types.h>
+#include <sys/systm.h>
+#include <sys/kernel.h>
+#include <sys/bus.h>
+#include <sys/module.h>
+#include <sys/lock.h>
+#include <sys/mutex.h>
+#include <sys/condvar.h>
+#include <sys/sysctl.h>
+#include <sys/sx.h>
+#include <sys/unistd.h>
+#include <sys/callout.h>
+#include <sys/malloc.h>
+#include <sys/priv.h>
+#include <sys/rman.h>
+
+#include <dev/fdt/fdt_common.h>
+
+#include <dev/ofw/openfirm.h>
+#include <dev/ofw/ofw_bus.h>
+#include <dev/ofw/ofw_bus_subr.h>
+
+#include <dev/usb/usb.h>
+#include <dev/usb/usbdi.h>
+
+#include <dev/usb/usb_core.h>
+#include <dev/usb/usb_busdma.h>
+#include <dev/usb/usb_process.h>
+#include <dev/usb/usb_transfer.h>
+#include <dev/usb/usb_device.h>
+#include <dev/usb/usb_hub.h>
+#include <dev/usb/usb_util.h>
+
+#include <dev/usb/usb_controller.h>
+#include <dev/usb/usb_bus.h>
+#endif /* USB_GLOBAL_INCLUDE_FILE */
+
+#include <dev/usb/controller/saf1761_otg.h>
+#include <dev/usb/controller/saf1761_otg_reg.h>
+
+static device_probe_t saf1761_otg_fdt_probe;
+static device_attach_t saf1761_otg_fdt_attach;
+static device_detach_t saf1761_otg_fdt_detach;
+
+static device_method_t saf1761_otg_methods[] = {
+ /* Device interface */
+ DEVMETHOD(device_probe, saf1761_otg_fdt_probe),
+ DEVMETHOD(device_attach, saf1761_otg_fdt_attach),
+ DEVMETHOD(device_detach, saf1761_otg_fdt_detach),
+ DEVMETHOD(device_suspend, bus_generic_suspend),
+ DEVMETHOD(device_resume, bus_generic_resume),
+ DEVMETHOD(device_shutdown, bus_generic_shutdown),
+
+ DEVMETHOD_END
+};
+
+static driver_t saf1761_otg_driver = {
+ .name = "saf1761",
+ .methods = saf1761_otg_methods,
+ .size = sizeof(struct saf1761_otg_softc),
+};
+
+static devclass_t saf1761_otg_devclass;
+
+DRIVER_MODULE(saf1761, simplebus, saf1761_otg_driver, saf1761_otg_devclass, 0, 0);
+MODULE_DEPEND(saf1761, usb, 1, 1, 1);
+
+static int
+saf1761_otg_fdt_probe(device_t dev)
+{
+ if (!ofw_bus_status_okay(dev))
+ return (ENXIO);
+
+ if (!ofw_bus_is_compatible(dev, "nxp,usb-isp1761"))
+ return (ENXIO);
+
+ device_set_desc(dev, "ISP1761/SAF1761 DCI USB 2.0 Device Controller");
+
+ return (0);
+}
+
+static int
+saf1761_otg_fdt_attach(device_t dev)
+{
+ struct saf1761_otg_softc *sc = device_get_softc(dev);
+ char param[24];
+ int err;
+ int rid;
+
+ /* get configuration from FDT */
+
+ /* get bus-width, if any */
+ if (OF_getprop(ofw_bus_get_node(dev), "bus-width",
+ &param, sizeof(param)) > 0) {
+ param[sizeof(param) - 1] = 0;
+ if (strcmp(param, "32") == 0)
+ sc->sc_hw_mode |= SOTG_HW_MODE_CTRL_DATA_BUS_WIDTH;
+ } else {
+ /* assume 32-bit data bus */
+ sc->sc_hw_mode |= SOTG_HW_MODE_CTRL_DATA_BUS_WIDTH;
+ }
+
+ /* get analog over-current setting */
+ if (OF_getprop(ofw_bus_get_node(dev), "analog-oc",
+ &param, sizeof(param)) > 0) {
+ sc->sc_hw_mode |= SOTG_HW_MODE_CTRL_ANA_DIGI_OC;
+ }
+
+ /* get DACK polarity */
+ if (OF_getprop(ofw_bus_get_node(dev), "dack-polarity",
+ &param, sizeof(param)) > 0) {
+ sc->sc_hw_mode |= SOTG_HW_MODE_CTRL_DACK_POL;
+ }
+
+ /* get DREQ polarity */
+ if (OF_getprop(ofw_bus_get_node(dev), "dreq-polarity",
+ &param, sizeof(param)) > 0) {
+ sc->sc_hw_mode |= SOTG_HW_MODE_CTRL_DREQ_POL;
+ }
+
+ /* initialise some bus fields */
+ sc->sc_bus.parent = dev;
+ sc->sc_bus.devices = sc->sc_devices;
+ sc->sc_bus.devices_max = SOTG_MAX_DEVICES;
+
+ /* get all DMA memory */
+ if (usb_bus_mem_alloc_all(&sc->sc_bus,
+ USB_GET_DMA_TAG(dev), NULL)) {
+ return (ENOMEM);
+ }
+ rid = 0;
+ sc->sc_io_res =
+ bus_alloc_resource_any(dev, SYS_RES_IOPORT, &rid, RF_ACTIVE);
+
+ if (!sc->sc_io_res) {
+ goto error;
+ }
+ sc->sc_io_tag = rman_get_bustag(sc->sc_io_res);
+ sc->sc_io_hdl = rman_get_bushandle(sc->sc_io_res);
+ sc->sc_io_size = rman_get_size(sc->sc_io_res);
+
+ rid = 0;
+ sc->sc_irq_res = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid,
+ RF_SHAREABLE | RF_ACTIVE);
+ if (sc->sc_irq_res == NULL) {
+ goto error;
+ }
+ sc->sc_bus.bdev = device_add_child(dev, "usbus", -1);
+ if (!(sc->sc_bus.bdev)) {
+ goto error;
+ }
+ device_set_ivars(sc->sc_bus.bdev, &sc->sc_bus);
+
+ err = bus_setup_intr(dev, sc->sc_irq_res, INTR_TYPE_BIO | INTR_MPSAFE,
+ NULL, (driver_intr_t *)saf1761_otg_interrupt, sc, &sc->sc_intr_hdl);
+ if (err) {
+ sc->sc_intr_hdl = NULL;
+ goto error;
+ }
+ err = saf1761_otg_init(sc);
+ if (err) {
+ device_printf(dev, "Init failed\n");
+ goto error;
+ }
+ err = device_probe_and_attach(sc->sc_bus.bdev);
+ if (err) {
+ device_printf(dev, "USB probe and attach failed\n");
+ goto error;
+ }
+ return (0);
+
+error:
+ saf1761_otg_fdt_detach(dev);
+ return (ENXIO);
+}
+
+static int
+saf1761_otg_fdt_detach(device_t dev)
+{
+ struct saf1761_otg_softc *sc = device_get_softc(dev);
+ device_t bdev;
+ int err;
+
+ if (sc->sc_bus.bdev) {
+ bdev = sc->sc_bus.bdev;
+ device_detach(bdev);
+ device_delete_child(dev, bdev);
+ }
+ /* during module unload there are lots of children leftover */
+ device_delete_children(dev);
+
+ if (sc->sc_irq_res && sc->sc_intr_hdl) {
+ /*
+ * Only call uninit() after init()
+ */
+ saf1761_otg_uninit(sc);
+
+ err = bus_teardown_intr(dev, sc->sc_irq_res,
+ sc->sc_intr_hdl);
+ sc->sc_intr_hdl = NULL;
+ }
+ if (sc->sc_irq_res) {
+ bus_release_resource(dev, SYS_RES_IRQ, 0,
+ sc->sc_irq_res);
+ sc->sc_irq_res = NULL;
+ }
+ if (sc->sc_io_res) {
+ bus_release_resource(dev, SYS_RES_IOPORT, 0,
+ sc->sc_io_res);
+ sc->sc_io_res = NULL;
+ }
+ usb_bus_mem_free_all(&sc->sc_bus, NULL);
+
+ return (0);
+}
diff --git a/sys/dev/usb/controller/saf1761_otg_reg.h b/sys/dev/usb/controller/saf1761_otg_reg.h
new file mode 100644
index 0000000..2b74866
--- /dev/null
+++ b/sys/dev/usb/controller/saf1761_otg_reg.h
@@ -0,0 +1,214 @@
+/* $FreeBSD$ */
+/*-
+ * Copyright (c) 2014 Hans Petter Selasky <hselasky@FreeBSD.org>
+ * All rights reserved.
+ *
+ * This software was developed by SRI International and the University of
+ * Cambridge Computer Laboratory under DARPA/AFRL contract (FA8750-10-C-0237)
+ * ("CTSRD"), as part of the DARPA CRASH research programme.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ */
+
+#ifndef _SAF1761_OTG_REG_H_
+#define _SAF1761_OTG_REG_H_
+
+/* Global registers */
+
+#define SOTG_VEND_ID 0x370
+#define SOTG_PROD_ID 0x372
+#define SOTG_CTRL_SET 0x374
+#define SOTG_CTRL_CLR 0x376
+#define SOTG_CTRL_OTG_DISABLE (1 << 10)
+#define SOTG_CTRL_OTG_SE0_EN (1 << 9)
+#define SOTG_CTRL_BDIS_ACON_EN (1 << 8)
+#define SOTG_CTRL_SW_SEL_HC_DC (1 << 7)
+#define SOTG_CTRL_VBUS_CHRG (1 << 6)
+#define SOTG_CTRL_VBUS_DISCHRG (1 << 5)
+#define SOTG_CTRL_VBUS_DRV (1 << 4)
+#define SOTG_CTRL_SEL_CP_EXT (1 << 3)
+#define SOTG_CTRL_DM_PULL_DOWN (1 << 2)
+#define SOTG_CTRL_DP_PULL_DOWN (1 << 1)
+#define SOTG_CTRL_DP_PULL_UP (1 << 0)
+#define SOTG_STATUS 0x378
+#define SOTG_STATUS_B_SE0_SRP (1 << 8)
+#define SOTG_STATUS_B_SESS_END (1 << 7)
+#define SOTG_STATUS_RMT_CONN (1 << 4)
+#define SOTG_STATUS_ID (1 << 3)
+#define SOTG_STATUS_DP_SRP (1 << 2)
+#define SOTG_STATUS_A_B_SESS_VLD (1 << 1)
+#define SOTG_STATUS_VBUS_VLD (1 << 0)
+#define SOTG_IRQ_LATCH_SET 0x37C
+#define SOTG_IRQ_LATCH_CLR 0x37E
+#define SOTG_IRQ_ENABLE_SET 0x380
+#define SOTG_IRQ_ENABLE_CLR 0x382
+#define SOTG_IRQ_RISE_SET 0x384
+#define SOTG_IRQ_RISE_CLR 0x386
+#define SOTG_IRQ_OTG_TMR_TIMEOUT (1 << 9)
+#define SOTG_IRQ_B_SE0_SRP (1 << 8)
+#define SOTG_IRQ_B_SESS_END (1 << 7)
+#define SOTG_IRQ_BDIS_ACON (1 << 6)
+#define SOTG_IRQ_OTG_RESUME (1 << 5)
+#define SOTG_IRQ_RMT_CONN (1 << 4)
+#define SOTG_IRQ_ID (1 << 3)
+#define SOTG_IRQ_DP_SRP (1 << 2)
+#define SOTG_IRQ_A_B_SESS_VLD (1 << 1)
+#define SOTG_IRQ_VBUS_VLD (1 << 0)
+#define SOTG_TIMER_LOW_SET 0x388
+#define SOTG_TIMER_LOW_CLR 0x38A
+#define SOTG_TIMER_HIGH_SET 0x38C
+#define SOTG_TIMER_HIGH_CLR 0x38E
+#define SOTG_TIMER_START_TMR (1U << 15)
+
+/* Peripheral controller specific registers */
+
+#define SOTG_ADDRESS 0x200
+#define SOTG_ADDRESS_ENABLE (1 << 7)
+#define SOTG_MODE 0x20C
+#define SOTG_MODE_DMACLK_ON (1 << 9)
+#define SOTG_MODE_VBUSSTAT (1 << 8)
+#define SOTG_MODE_CLKAON (1 << 7)
+#define SOTG_MODE_SNDRSU (1 << 6)
+#define SOTG_MODE_GOSUSP (1 << 5)
+#define SOTG_MODE_SFRESET (1 << 4)
+#define SOTG_MODE_GLINTENA (1 << 3)
+#define SOTG_MODE_WKUPCS (1 << 2)
+#define SOTG_INTERRUPT_CFG 0x210
+#define SOTG_INTERRUPT_CFG_CDBGMOD (3 << 6)
+#define SOTG_INTERRUPT_CFG_DDBGMODIN (3 << 4)
+#define SOTG_INTERRUPT_CFG_DDBGMODOUT (3 << 2)
+#define SOTG_INTERRUPT_CFG_INTLVL (1 << 1)
+#define SOTG_INTERRUPT_CFG_INTPOL (1 << 0)
+#define SOTG_DEBUG 0x212
+#define SOTG_DEBUG_SET (1 << 0)
+#define SOTG_DCINTERRUPT_EN 0x214
+#define SOTG_HW_MODE_CTRL 0x300
+#define SOTG_HW_MODE_CTRL_ALL_ATX_RESET (1 << 31)
+#define SOTG_HW_MODE_CTRL_ANA_DIGI_OC (1 << 15)
+#define SOTG_HW_MODE_CTRL_DEV_DMA (1 << 11)
+#define SOTG_HW_MODE_CTRL_COMN_INT (1 << 10)
+#define SOTG_HW_MODE_CTRL_COMN_DMA (1 << 9)
+#define SOTG_HW_MODE_CTRL_DATA_BUS_WIDTH (1 << 8)
+#define SOTG_HW_MODE_CTRL_DACK_POL (1 << 6)
+#define SOTG_HW_MODE_CTRL_DREQ_POL (1 << 5)
+#define SOTG_HW_MODE_CTRL_INTR_POL (1 << 2)
+#define SOTG_HW_MODE_CTRL_INTR_LEVEL (1 << 1)
+#define SOTG_HW_MODE_CTRL_GLOBAL_INTR_EN (1 << 0)
+#define SOTG_OTG_CTRL 0x374
+#define SOTG_EP_INDEX 0x22c
+#define SOTG_EP_INDEX_EP0SETUP (1 << 5)
+#define SOTG_EP_INDEX_ENDP_INDEX_MASK (15 << 1)
+#define SOTG_EP_INDEX_ENDP_INDEX_SHIFT 1
+#define SOTG_EP_INDEX_DIR_IN (1 << 0)
+#define SOTG_EP_INDEX_DIR_OUT 0
+#define SOTG_CTRL_FUNC 0x228
+#define SOTG_CTRL_FUNC_CLBUF (1 << 4)
+#define SOTG_CTRL_FUNC_VENDP (1 << 3)
+#define SOTG_CTRL_FUNC_DSEN (1 << 2)
+#define SOTG_CTRL_FUNC_STATUS (1 << 1)
+#define SOTG_CTRL_FUNC_STALL (1 << 0)
+#define SOTG_DATA_PORT 0x220
+#define SOTG_BUF_LENGTH 0x21C
+#define SOTG_DCBUFFERSTATUS 0x21E
+#define SOTG_DCBUFFERSTATUS_FILLED_MASK (3 << 0)
+#define SOTG_EP_MAXPACKET 0x204
+#define SOTG_EP_TYPE 0x208
+#define SOTG_EP_TYPE_NOEMPPKT (1 << 4)
+#define SOTG_EP_TYPE_ENABLE (1 << 3)
+#define SOTG_EP_TYPE_DBLBUF (1 << 2)
+#define SOTG_EP_TYPE_EP_TYPE (3 << 0)
+#define SOTG_DMA_CMD 0x230
+#define SOTG_DMA_XFER_COUNT 0x234
+#define SOTG_DCDMA_CFG 0x238
+#define SOTG_DMA_HW 0x23C
+#define SOTG_DMA_IRQ_REASON 0x250
+#define SOTG_DMA_IRQ_ENABLE 0x254
+#define SOTG_DMA_EP 0x258
+#define SOTG_BURST_COUNTER 0x264
+#define SOTG_DCINTERRUPT 0x218
+#define SOTG_DCINTERRUPT_IEPRX(n) (1 << (10 + (2*(n))))
+#define SOTG_DCINTERRUPT_IEPTX(n) (1 << (11 + (2*(n))))
+#define SOTG_DCINTERRUPT_IEP0SETUP (1 << 8)
+#define SOTG_DCINTERRUPT_IEVBUS (1 << 7)
+#define SOTG_DCINTERRUPT_IEDMA (1 << 6)
+#define SOTG_DCINTERRUPT_IEHS_STA (1 << 5)
+#define SOTG_DCINTERRUPT_IERESM (1 << 4)
+#define SOTG_DCINTERRUPT_IESUSP (1 << 3)
+#define SOTG_DCINTERRUPT_IEPSOF (1 << 2)
+#define SOTG_DCINTERRUPT_IESOF (1 << 1)
+#define SOTG_DCINTERRUPT_IEBRST (1 << 0)
+#define SOTG_DCCHIP_ID 0x270
+#define SOTG_FRAME_NUM 0x274
+#define SOTG_FRAME_NUM_MICROSOFR_MASK 0x3800
+#define SOTG_FRAME_NUM_MICROSOFR_SHIFT 11
+#define SOTG_FRAME_NUM_SOFR_MASK 0x7FF
+#define SOTG_DCSCRATCH 0x278
+#define SOTG_UNLOCK_DEVICE 0x27C
+#define SOTG_UNLOCK_DEVICE_CODE 0xAA37
+#define SOTG_IRQ_PULSE_WIDTH 0x280
+#define SOTG_TEST_MODE 0x284
+#define SOTG_TEST_MODE_FORCEHS (1 << 7)
+#define SOTG_TEST_MODE_FORCEFS (1 << 4)
+#define SOTG_TEST_MODE_PRBS (1 << 3)
+#define SOTG_TEST_MODE_KSTATE (1 << 2)
+#define SOTG_TEST_MODE_JSTATE (1 << 1)
+#define SOTG_TEST_MODE_SE0_NAK (1 << 0)
+
+/* Host controller specific registers */
+
+#define SOTG_CONFIGFLAG 0x0060
+#define SOTG_CONFIGFLAG_ENABLE (1 << 0)
+#define SOTG_PORTSC1 0x0064
+#define SOTG_PORTSC1_PIC (3 << 14)
+#define SOTG_PORTSC1_PO (1 << 13)
+#define SOTG_PORTSC1_PP (1 << 12)
+#define SOTG_PORTSC1_LS (3 << 10)
+#define SOTG_PORTSC1_PR (1 << 8)
+#define SOTG_PORTSC1_SUSP (1 << 7)
+#define SOTG_PORTSC1_FPR (1 << 6)
+#define SOTG_PORTSC1_PED (1 << 2)
+#define SOTG_PORTSC1_ECSC (1 << 1)
+#define SOTG_PORTSC1_ECCS (1 << 0)
+#define SOTG_DATA_ADDR(x) (0x400 + (512 * (x)))
+#define SOTG_ASYNC_PDT(x) (0x400 + (60 * 1024) + ((x) * 32))
+#define SOTG_INTR_PDT(x) (0x400 + (61 * 1024) + ((x) * 32))
+#define SOTG_ISOC_PDT(x) (0x400 + (62 * 1024) + ((x) * 32))
+#define SOTG_HC_MEMORY_ADDR(x) (((x) - 0x400) >> 3)
+#define SOTG_SW_RESET 0x30C
+#define SOTG_SW_RESET_HC (1 << 1)
+#define SOTG_SW_RESET_ALL (1 << 0)
+#define SOTG_POWER_DOWN 0x354
+#define SOTG_POWER_DOWN_PORT3_PD (1 << 12)
+#define SOTG_POWER_DOWN_PORT2_PD (1 << 11)
+#define SOTG_POWER_DOWN_VBATDET_PWR (1 << 10)
+#define SOTG_POWER_DOWN_BIAS_EN (1 << 5)
+#define SOTG_POWER_DOWN_VREG_ON (1 << 4)
+#define SOTG_POWER_DOWN_OC3_PWR (1 << 3)
+#define SOTG_POWER_DOWN_OC2_PWR (1 << 2)
+#define SOTG_POWER_DOWN_OC1_PWR (1 << 1)
+#define SOTG_POWER_DOWN_HC_CLK_EN (1 << 0)
+#define SOTG_USBCMD 0x20
+#define SOTG_USBCMD_LHCR (1 << 7)
+#define SOTG_USBCMD_HCRESET (1 << 1)
+#define SOTG_USBCMD_RS (1 << 0)
+
+#endif /* _SAF1761_OTG_REG_H_ */
diff --git a/sys/modules/usb/saf1761/Makefile b/sys/modules/usb/saf1761/Makefile
index bd1be7d..5610a38 100644
--- a/sys/modules/usb/saf1761/Makefile
+++ b/sys/modules/usb/saf1761/Makefile
@@ -36,7 +36,7 @@ S= ${.CURDIR}/../../..
KMOD= saf1761
SRCS= bus_if.h device_if.h usb_if.h \
opt_bus.h opt_usb.h ofw_bus_if.h \
- saf1761_dci.c saf1761_dci_fdt.c \
+ saf1761_otg.c saf1761_otg_fdt.c \
pci_if.h
.include <bsd.kmod.mk>
OpenPOWER on IntegriCloud