Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src/sys Add motg(4), a driver for Mentor Graphic's OTG USB contr...



details:   https://anonhg.NetBSD.org/src/rev/1199dd6c7d0b
branches:  trunk
changeset: 330625:1199dd6c7d0b
user:      bouyer <bouyer%NetBSD.org@localhost>
date:      Wed Jul 16 18:22:23 2014 +0000

description:
Add motg(4), a driver for Mentor Graphic's OTG USB controller IP,
as found in several SoCs.
Only host mode is supported, good enough for umass (USB keys), kubd and ums,
and USB2 hubs.
It fails to properly talk to USB/serial adapters at this time.

diffstat:

 sys/conf/files        |     6 +-
 sys/dev/DEVNAMES      |     3 +-
 sys/dev/usb/motg.c    |  2408 +++++++++++++++++++++++++++++++++++++++++++++++++
 sys/dev/usb/motgreg.h |   286 +++++
 sys/dev/usb/motgvar.h |   131 ++
 5 files changed, 2832 insertions(+), 2 deletions(-)

diffs (truncated from 2874 to 300 lines):

diff -r cc3b923a40c6 -r 1199dd6c7d0b sys/conf/files
--- a/sys/conf/files    Wed Jul 16 17:58:35 2014 +0000
+++ b/sys/conf/files    Wed Jul 16 18:22:23 2014 +0000
@@ -1,4 +1,4 @@
-#      $NetBSD: files,v 1.1093 2014/06/12 21:08:31 christos Exp $
+#      $NetBSD: files,v 1.1094 2014/07/16 18:22:23 bouyer Exp $
 #      @(#)files.newconf       7.5 (Berkeley) 5/10/93
 
 version        20100430
@@ -1228,6 +1228,10 @@
 device xhci: usbus, usbroothub, usb_dma
 file   dev/usb/xhci.c                  xhci                    needs-flag
 
+# Mentor graphics OTG IP
+device motg: usbus, usbroothub, usb_dma
+file   dev/usb/motg.c                  motg                    needs-flag
+
 # SL811HS/T USB controller
 defflag        opt_slhci.h     SLHCI_DEBUG SLHCI_TRY_LSVH SLHCI_PROFILE_TRANSFER
 device slhci: usbus, usbroothub
diff -r cc3b923a40c6 -r 1199dd6c7d0b sys/dev/DEVNAMES
--- a/sys/dev/DEVNAMES  Wed Jul 16 17:58:35 2014 +0000
+++ b/sys/dev/DEVNAMES  Wed Jul 16 18:22:23 2014 +0000
@@ -1,4 +1,4 @@
-#      $NetBSD: DEVNAMES,v 1.289 2014/03/31 11:25:49 martin Exp $
+#      $NetBSD: DEVNAMES,v 1.290 2014/07/16 18:22:23 bouyer Exp $
 #
 # This file contains all used device names and defined attributes in
 # alphabetical order. New devices added to the system somewhere should first
@@ -902,6 +902,7 @@
 mms                    bebox
 mms                    dreamcast
 mms                    i386
+motg                   MI
 motoi2c                        MI              Attribute
 moxa                   MI
 mppb                   amiga
diff -r cc3b923a40c6 -r 1199dd6c7d0b sys/dev/usb/motg.c
--- /dev/null   Thu Jan 01 00:00:00 1970 +0000
+++ b/sys/dev/usb/motg.c        Wed Jul 16 18:22:23 2014 +0000
@@ -0,0 +1,2408 @@
+/*     $NetBSD: motg.c,v 1.1 2014/07/16 18:22:23 bouyer Exp $  */
+
+/*
+ * Copyright (c) 1998, 2004, 2011, 2012, 2014 The NetBSD Foundation, Inc.
+ * All rights reserved.
+ *
+ * This code is derived from software contributed to The NetBSD Foundation
+ * by Lennart Augustsson (lennart%augustsson.net@localhost) at
+ * Carlstedt Research & Technology, Jared D. McNeill (jmcneill%invisible.ca@localhost),
+ * Matthew R. Green (mrg%eterna.com.au@localhost), and Manuel Bouyer (bouyer%netbsd.org@localhost).
+ *
+ * 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 NETBSD FOUNDATION, INC. 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 FOUNDATION 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 Mentor Graphics Inventra USB
+ * 2.0 High Speed Dual-Role controller.
+ *
+ * NOTE: The current implementation only supports Device Side Mode!
+ */
+
+#include <sys/cdefs.h>
+__KERNEL_RCSID(0, "$NetBSD: motg.c,v 1.1 2014/07/16 18:22:23 bouyer Exp $");
+
+#include <sys/param.h>
+#include <sys/systm.h>
+#include <sys/kernel.h>
+#include <sys/kmem.h>
+#include <sys/device.h>
+#include <sys/select.h>
+#include <sys/extent.h>
+#include <sys/proc.h>
+#include <sys/queue.h>
+#include <sys/bus.h>
+#include <sys/cpu.h>
+
+#include <machine/endian.h>
+
+#include <dev/usb/usb.h>
+#include <dev/usb/usbdi.h>
+#include <dev/usb/usbdivar.h>
+#include <dev/usb/usb_mem.h>
+#include <dev/usb/usb_quirks.h>
+
+#include <dev/usb/motgreg.h>
+#include <dev/usb/motgvar.h>
+#include <dev/usb/usbroothub_subr.h>
+
+#define MOTG_DEBUG
+#ifdef MOTG_DEBUG
+#define DPRINTF(x)     if (motgdebug) printf x
+#define DPRINTFN(n,x)  if (motgdebug & (n)) printf x
+#define MD_ROOT 0x0002
+#define MD_CTRL 0x0004
+#define MD_BULK 0x0008
+// int motgdebug = MD_ROOT | MD_CTRL | MD_BULK;
+int motgdebug = 0;
+#else
+#define DPRINTF(x)
+#define DPRINTFN(n,x)
+#endif
+
+/* various timeouts, for various speeds */
+/* control NAK timeouts */
+#define NAK_TO_CTRL    10      /* 1024 frames, about 1s */
+#define NAK_TO_CTRL_HIGH 13    /* 8k microframes, about 0.8s */
+
+/* intr/iso polling intervals */
+#define POLL_TO                100     /* 100 frames, about 0.1s */
+#define POLL_TO_HIGH   10      /* 100 microframes, about 0.12s */
+
+/* bulk NAK timeouts */
+#define NAK_TO_BULK    255     /* 255 frames, about 0.25s */
+#define NAK_TO_BULK_HIGH 13    /* 8k microframes, about 1s */
+
+static void            motg_hub_change(struct motg_softc *);
+static usbd_status     motg_root_ctrl_transfer(usbd_xfer_handle);
+static usbd_status     motg_root_ctrl_start(usbd_xfer_handle);
+static void            motg_root_ctrl_abort(usbd_xfer_handle);
+static void            motg_root_ctrl_close(usbd_pipe_handle);
+static void            motg_root_ctrl_done(usbd_xfer_handle);
+
+static usbd_status     motg_root_intr_transfer(usbd_xfer_handle);
+static usbd_status     motg_root_intr_start(usbd_xfer_handle);
+static void            motg_root_intr_abort(usbd_xfer_handle);
+static void            motg_root_intr_close(usbd_pipe_handle);
+static void            motg_root_intr_done(usbd_xfer_handle);
+
+static usbd_status     motg_open(usbd_pipe_handle);
+static void            motg_poll(struct usbd_bus *);
+static void            motg_softintr(void *);
+static usbd_status     motg_allocm(struct usbd_bus *, usb_dma_t *, u_int32_t);
+static void            motg_freem(struct usbd_bus *, usb_dma_t *);
+static usbd_xfer_handle        motg_allocx(struct usbd_bus *);
+static void            motg_freex(struct usbd_bus *, usbd_xfer_handle);
+static void            motg_get_lock(struct usbd_bus *, kmutex_t **);
+static void            motg_noop(usbd_pipe_handle pipe);
+static usbd_status     motg_portreset(struct motg_softc*);
+
+static usbd_status     motg_device_ctrl_transfer(usbd_xfer_handle);
+static usbd_status     motg_device_ctrl_start(usbd_xfer_handle);
+static void            motg_device_ctrl_abort(usbd_xfer_handle);
+static void            motg_device_ctrl_close(usbd_pipe_handle);
+static void            motg_device_ctrl_done(usbd_xfer_handle);
+static usbd_status     motg_device_ctrl_start1(struct motg_softc *);
+static void            motg_device_ctrl_read(usbd_xfer_handle);
+static void            motg_device_ctrl_intr_rx(struct motg_softc *);
+static void            motg_device_ctrl_intr_tx(struct motg_softc *);
+
+static usbd_status     motg_device_data_transfer(usbd_xfer_handle);
+static usbd_status     motg_device_data_start(usbd_xfer_handle);
+static usbd_status     motg_device_data_start1(struct motg_softc *,
+                           struct motg_hw_ep *);
+static void            motg_device_data_abort(usbd_xfer_handle);
+static void            motg_device_data_close(usbd_pipe_handle);
+static void            motg_device_data_done(usbd_xfer_handle);
+static void            motg_device_intr_rx(struct motg_softc *, int);
+static void            motg_device_intr_tx(struct motg_softc *, int);
+static void            motg_device_data_read(usbd_xfer_handle);
+static void            motg_device_data_write(usbd_xfer_handle);
+
+static void            motg_waitintr(struct motg_softc *, usbd_xfer_handle);
+static void            motg_device_clear_toggle(usbd_pipe_handle pipe);
+
+#define MOTG_INTR_ENDPT 1
+#define UBARR(sc) bus_space_barrier((sc)->sc_iot, (sc)->sc_ioh, 0, (sc)->sc_size, \
+                       BUS_SPACE_BARRIER_READ|BUS_SPACE_BARRIER_WRITE)
+#define UWRITE1(sc, r, x) \
+ do { UBARR(sc); bus_space_write_1((sc)->sc_iot, (sc)->sc_ioh, (r), (x)); \
+ } while (/*CONSTCOND*/0)
+#define UWRITE2(sc, r, x) \
+ do { UBARR(sc); bus_space_write_2((sc)->sc_iot, (sc)->sc_ioh, (r), (x)); \
+ } while (/*CONSTCOND*/0)
+#define UWRITE4(sc, r, x) \
+ do { UBARR(sc); bus_space_write_4((sc)->sc_iot, (sc)->sc_ioh, (r), (x)); \
+ } while (/*CONSTCOND*/0)
+
+static __inline uint32_t
+UREAD1(struct motg_softc *sc, bus_size_t r)
+{
+
+       UBARR(sc);
+       return bus_space_read_1(sc->sc_iot, sc->sc_ioh, r);
+}
+static __inline uint32_t
+UREAD2(struct motg_softc *sc, bus_size_t r)
+{
+
+       UBARR(sc);
+       return bus_space_read_2(sc->sc_iot, sc->sc_ioh, r);
+}
+static __inline uint32_t
+UREAD4(struct motg_softc *sc, bus_size_t r)
+{
+
+       UBARR(sc);
+       return bus_space_read_4(sc->sc_iot, sc->sc_ioh, r);
+}
+
+static void
+musbotg_pull_common(struct motg_softc *sc, uint8_t on)  
+{
+        uint8_t val;
+
+        val = UREAD1(sc, MUSB2_REG_POWER);
+        if (on)
+                val |= MUSB2_MASK_SOFTC;
+        else
+                val &= ~MUSB2_MASK_SOFTC;
+
+        UWRITE1(sc, MUSB2_REG_POWER, val);
+}
+
+const struct usbd_bus_methods motg_bus_methods = {
+       .open_pipe =    motg_open,
+       .soft_intr =    motg_softintr,
+       .do_poll =      motg_poll,
+       .allocm =       motg_allocm,
+       .freem =        motg_freem,
+       .allocx =       motg_allocx,
+       .freex =        motg_freex,
+       .get_lock =     motg_get_lock,
+       .new_device =   NULL,
+};
+
+const struct usbd_pipe_methods motg_root_ctrl_methods = {
+       .transfer =     motg_root_ctrl_transfer,
+       .start =        motg_root_ctrl_start,
+       .abort =        motg_root_ctrl_abort,
+       .close =        motg_root_ctrl_close,
+       .cleartoggle =  motg_noop,
+       .done =         motg_root_ctrl_done,
+};
+
+const struct usbd_pipe_methods motg_root_intr_methods = {
+       .transfer =     motg_root_intr_transfer,
+       .start =        motg_root_intr_start,
+       .abort =        motg_root_intr_abort,
+       .close =        motg_root_intr_close,
+       .cleartoggle =  motg_noop,
+       .done =         motg_root_intr_done,
+};
+
+const struct usbd_pipe_methods motg_device_ctrl_methods = {
+       .transfer =     motg_device_ctrl_transfer,
+       .start =        motg_device_ctrl_start,
+       .abort =        motg_device_ctrl_abort,
+       .close =        motg_device_ctrl_close,
+       .cleartoggle =  motg_noop,
+       .done =         motg_device_ctrl_done,
+};
+
+const struct usbd_pipe_methods motg_device_data_methods = {
+       .transfer =     motg_device_data_transfer,
+       .start =        motg_device_data_start,
+       .abort =        motg_device_data_abort,
+       .close =        motg_device_data_close,
+       .cleartoggle =  motg_device_clear_toggle,
+       .done =         motg_device_data_done,
+};
+
+usbd_status
+motg_init(struct motg_softc *sc)
+{
+       uint32_t nrx, ntx, val;
+       int dynfifo;
+       int offset, i;
+
+       if (sc->sc_mode == MOTG_MODE_DEVICE)
+               return USBD_NORMAL_COMPLETION; /* not supported */
+
+       /* disable all interrupts */
+       UWRITE1(sc, MUSB2_REG_INTUSBE, 0);
+       UWRITE2(sc, MUSB2_REG_INTTXE, 0);
+       UWRITE2(sc, MUSB2_REG_INTRXE, 0);
+       /* disable pullup */
+
+       musbotg_pull_common(sc, 0);   
+



Home | Main Index | Thread Index | Old Index