From owner-svn-src-head@freebsd.org Mon Sep 5 16:06:53 2016 Return-Path: Delivered-To: svn-src-head@mailman.ysv.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:1900:2254:206a::19:1]) by mailman.ysv.freebsd.org (Postfix) with ESMTP id A76D0A9DC0E; Mon, 5 Sep 2016 16:06:53 +0000 (UTC) (envelope-from mizhka@FreeBSD.org) Received: from repo.freebsd.org (repo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:0]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (Client did not present a certificate) by mx1.freebsd.org (Postfix) with ESMTPS id 723372C3; Mon, 5 Sep 2016 16:06:53 +0000 (UTC) (envelope-from mizhka@FreeBSD.org) Received: from repo.freebsd.org ([127.0.1.37]) by repo.freebsd.org (8.15.2/8.15.2) with ESMTP id u85G6qDM060741; Mon, 5 Sep 2016 16:06:52 GMT (envelope-from mizhka@FreeBSD.org) Received: (from mizhka@localhost) by repo.freebsd.org (8.15.2/8.15.2/Submit) id u85G6qnm060736; Mon, 5 Sep 2016 16:06:52 GMT (envelope-from mizhka@FreeBSD.org) Message-Id: <201609051606.u85G6qnm060736@repo.freebsd.org> X-Authentication-Warning: repo.freebsd.org: mizhka set sender to mizhka@FreeBSD.org using -f From: Michael Zhilin Date: Mon, 5 Sep 2016 16:06:52 +0000 (UTC) To: src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-head@freebsd.org Subject: svn commit: r305426 - in head/sys: dev/bhnd/cores/usb mips/broadcom mips/conf X-SVN-Group: head MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-BeenThere: svn-src-head@freebsd.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: SVN commit messages for the src tree for head/-current List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Mon, 05 Sep 2016 16:06:53 -0000 Author: mizhka Date: Mon Sep 5 16:06:52 2016 New Revision: 305426 URL: https://svnweb.freebsd.org/changeset/base/305426 Log: [BHND/USB] Port of EHCI/OHCI support from ZRouter This patch adds driver implementation for BHND USB core. Driver has been imported from ZRouter project with small adaptions for FreeBSD 11. Also it's enabled for BroadCom MIPS74k boards by default. It's fully tested on Asus boards (RT-N16: external USB, RT-N53: USB bus between SoC and WiFi chips). Reviewed by: adrian (mentor), ray Approved by: adrian (mentor) Obtained from: ZRouter Differential Revision: https://reviews.freebsd.org/D7781 Added: head/sys/dev/bhnd/cores/usb/ head/sys/dev/bhnd/cores/usb/bhnd_ehci.c (contents, props changed) head/sys/dev/bhnd/cores/usb/bhnd_ohci.c (contents, props changed) head/sys/dev/bhnd/cores/usb/bhnd_usb.c (contents, props changed) head/sys/dev/bhnd/cores/usb/bhnd_usbvar.h (contents, props changed) Modified: head/sys/mips/broadcom/files.broadcom head/sys/mips/conf/BCM Added: head/sys/dev/bhnd/cores/usb/bhnd_ehci.c ============================================================================== --- /dev/null 00:00:00 1970 (empty, because file is newly added) +++ head/sys/dev/bhnd/cores/usb/bhnd_ehci.c Mon Sep 5 16:06:52 2016 (r305426) @@ -0,0 +1,267 @@ +/*- + * Copyright (C) 2008 MARVELL INTERNATIONAL LTD. + * Copyright (c) 2010, Aleksandr Rybalko + * All rights reserved. + * + * Developed by Semihalf. + * + * 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. + * 3. Neither the name of MARVELL nor the names of contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY 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 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. + */ + +#include +__FBSDID("$FreeBSD$"); + +/* + * BHND attachment driver for the USB Enhanced Host Controller. + * Ported from ZRouter with insignificant adaptations for FreeBSD11. + */ + +#include "opt_bus.h" + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include +#include + +#include +#include +#include +#include + +#include +#include +#include +#include + +#include + +#define EHCI_HC_DEVSTR "Broadcom EHCI" + +#define USB_BRIDGE_INTR_CAUSE 0x210 +#define USB_BRIDGE_INTR_MASK 0x214 + +static device_attach_t bhnd_ehci_attach; +static device_detach_t bhnd_ehci_detach; + +static int bhnd_ehci_probe(device_t self); +static void bhnd_ehci_post_reset(struct ehci_softc *ehci_softc); + +static int +bhnd_ehci_probe(device_t self) +{ + + device_set_desc(self, EHCI_HC_DEVSTR); + + return (BUS_PROBE_DEFAULT); +} + +static void +bhnd_ehci_post_reset(struct ehci_softc *ehci_softc) +{ + uint32_t usbmode; + + /* Force HOST mode */ + usbmode = EOREAD4(ehci_softc, EHCI_USBMODE_NOLPM); + usbmode &= ~EHCI_UM_CM; + usbmode |= EHCI_UM_CM_HOST; + EOWRITE4(ehci_softc, EHCI_USBMODE_NOLPM, usbmode); +} + +static int +bhnd_ehci_attach(device_t self) +{ + ehci_softc_t *sc; + int err; + int rid; + + sc = device_get_softc(self); + /* initialise some bus fields */ + sc->sc_bus.parent = self; + sc->sc_bus.devices = sc->sc_devices; + sc->sc_bus.devices_max = EHCI_MAX_DEVICES; + sc->sc_bus.usbrev = USB_REV_2_0; + sc->sc_bus.dma_bits = 32; + + /* get all DMA memory */ + if ((err = usb_bus_mem_alloc_all(&sc->sc_bus, USB_GET_DMA_TAG(self), + &ehci_iterate_hw_softc)) != 0) { + BHND_ERROR_DEV(self, "can't allocate DMA memory: %d", err); + return (ENOMEM); + } + + rid = 0; + sc->sc_io_res = bus_alloc_resource_any(self, SYS_RES_MEMORY, &rid, + RF_ACTIVE); + if (!sc->sc_io_res) { + BHND_ERROR_DEV(self, "Could not map memory"); + 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(self, SYS_RES_IRQ, &rid, + RF_SHAREABLE | RF_ACTIVE); + + if (sc->sc_irq_res == NULL) { + BHND_ERROR_DEV(self, "Could not allocate error irq"); + bhnd_ehci_detach(self); + return (ENXIO); + } + + sc->sc_bus.bdev = device_add_child(self, "usbus", -1); + if (!sc->sc_bus.bdev) { + BHND_ERROR_DEV(self, "Could not add USB device"); + goto error; + } + device_set_ivars(sc->sc_bus.bdev, &sc->sc_bus); + device_set_desc(sc->sc_bus.bdev, EHCI_HC_DEVSTR); + + sprintf(sc->sc_vendor, "Broadcom"); + + err = bus_setup_intr(self, sc->sc_irq_res, INTR_TYPE_BIO | INTR_MPSAFE, + NULL, (driver_intr_t *)ehci_interrupt, sc, &sc->sc_intr_hdl); + if (err) { + BHND_ERROR_DEV(self, "Could not setup irq, %d", err); + sc->sc_intr_hdl = NULL; + goto error; + } + + sc->sc_flags |= EHCI_SCFLG_LOSTINTRBUG; + sc->sc_vendor_post_reset = bhnd_ehci_post_reset; + + err = ehci_init(sc); + if (!err) { + err = device_probe_and_attach(sc->sc_bus.bdev); + } + if (err) { + BHND_ERROR_DEV(self, "USB init failed err=%d", err); + goto error; + } + return (0); + +error: + bhnd_ehci_detach(self); + return (ENXIO); +} + +static int +bhnd_ehci_detach(device_t self) +{ + ehci_softc_t *sc; + device_t bdev; + int err; + + sc = device_get_softc(self); + + if (sc->sc_bus.bdev) { + bdev = sc->sc_bus.bdev; + device_detach(bdev); + device_delete_child(self, bdev); + } + /* during module unload there are lots of children leftover */ + device_delete_children(self); + + /* + * disable interrupts that might have been switched on in ehci_init + */ +#ifdef notyet + if (sc->sc_io_res) { + EWRITE4(sc, EHCI_USBINTR, 0); + EWRITE4(sc, USB_BRIDGE_INTR_MASK, 0); + } +#endif + if (sc->sc_irq_res && sc->sc_intr_hdl) { + /* + * only call ehci_detach() after ehci_init() + */ + ehci_detach(sc); + + err = bus_teardown_intr(self, sc->sc_irq_res, sc->sc_intr_hdl); + + if (err) + /* XXX or should we panic? */ + BHND_ERROR_DEV(self, "Could not tear down irq, %d", err); + + sc->sc_intr_hdl = NULL; + } + if (sc->sc_irq_res) { + bus_release_resource(self, SYS_RES_IRQ, 0, sc->sc_irq_res); + sc->sc_irq_res = NULL; + } + if (sc->sc_io_res) { + bus_release_resource(self, SYS_RES_MEMORY, 0, sc->sc_io_res); + sc->sc_io_res = NULL; + } + usb_bus_mem_free_all(&sc->sc_bus, &ehci_iterate_hw_softc); + + return (0); +} + +static device_method_t ehci_methods[] = { + /* Device interface */ + DEVMETHOD(device_probe, bhnd_ehci_probe), + DEVMETHOD(device_attach, bhnd_ehci_attach), + DEVMETHOD(device_detach, bhnd_ehci_detach), + DEVMETHOD(device_suspend, bus_generic_suspend), + DEVMETHOD(device_resume, bus_generic_resume), + DEVMETHOD(device_shutdown, bus_generic_shutdown), + + /* Bus interface */ + DEVMETHOD(bus_print_child, bus_generic_print_child), + + {0, 0} +}; + +static driver_t ehci_driver = { + "ehci", + ehci_methods, + sizeof(ehci_softc_t), +}; + +static devclass_t ehci_devclass; + +DRIVER_MODULE(ehci, bhnd_usb, ehci_driver, ehci_devclass, 0, 0); +MODULE_DEPEND(ehci, usb, 1, 1, 1); Added: head/sys/dev/bhnd/cores/usb/bhnd_ohci.c ============================================================================== --- /dev/null 00:00:00 1970 (empty, because file is newly added) +++ head/sys/dev/bhnd/cores/usb/bhnd_ohci.c Mon Sep 5 16:06:52 2016 (r305426) @@ -0,0 +1,228 @@ +/*- + * Copyright (c) 1998 The NetBSD Foundation, Inc. + * Copyright (c) 2010, Aleksandr Rybalko + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by Lennart Augustsson (augustss@carlstedt.se) at + * Carlstedt Research & Technology. + * + * 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. + */ + +#include +__FBSDID("$FreeBSD$"); + +/* + * USB Open Host Controller driver. + * + * OHCI spec: http://www.intel.com/design/usb/ohci11d.pdf + */ + +/* The low level controller code for OHCI has been split into + * SIBA probes and OHCI specific code. This was done to facilitate the + * sharing of code between *BSD's + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include +#include + +#include +#include +#include +#include + +#include +#include +#include +#include + +static device_probe_t bhnd_ohci_probe; +static device_attach_t bhnd_ohci_attach; +static device_detach_t bhnd_ohci_detach; + +static int +bhnd_ohci_probe(device_t self) +{ + device_set_desc(self, "Broadcom OHCI"); + return (0); +} + +static int +bhnd_ohci_attach(device_t self) +{ + ohci_softc_t *sc; + int rid; + int err; + + sc = device_get_softc(self); + /* initialise some bus fields */ + sc->sc_bus.parent = self; + sc->sc_bus.devices = sc->sc_devices; + sc->sc_bus.devices_max = OHCI_MAX_DEVICES; + sc->sc_bus.dma_bits = 32; + + /* get all DMA memory */ + if (usb_bus_mem_alloc_all(&sc->sc_bus, USB_GET_DMA_TAG(self), + &ohci_iterate_hw_softc)) { + return (ENOMEM); + } + sc->sc_dev = self; + + rid = 0; + sc->sc_io_res = bus_alloc_resource_any(self, SYS_RES_MEMORY, &rid, + RF_ACTIVE); + if (!sc->sc_io_res) { + device_printf(self, "Could not map memory\n"); + 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(self, SYS_RES_IRQ, &rid, + RF_SHAREABLE | RF_ACTIVE); + if (sc->sc_irq_res == NULL) { + device_printf(self, "Could not allocate irq\n"); + goto error; + } + sc->sc_bus.bdev = device_add_child(self, "usbus", -1); + if (!sc->sc_bus.bdev) { + device_printf(self, "Could not add USB device\n"); + goto error; + } + device_set_ivars(sc->sc_bus.bdev, &sc->sc_bus); + + strlcpy(sc->sc_vendor, "Broadcom", sizeof(sc->sc_vendor)); + + err = bus_setup_intr(self, sc->sc_irq_res, INTR_TYPE_BIO | INTR_MPSAFE, + NULL, (driver_intr_t *)ohci_interrupt, sc, &sc->sc_intr_hdl); + + if (err) { + device_printf(self, "Could not setup irq, %d\n", err); + sc->sc_intr_hdl = NULL; + goto error; + } + err = ohci_init(sc); + if (!err) { + err = device_probe_and_attach(sc->sc_bus.bdev); + } + if (err) { + device_printf(self, "USB init failed\n"); + goto error; + } + return (0); + +error: + bhnd_ohci_detach(self); + return (ENXIO); +} + +static int +bhnd_ohci_detach(device_t self) +{ + ohci_softc_t *sc; + device_t bdev; + + sc = device_get_softc(self); + + if (sc->sc_bus.bdev) { + bdev = sc->sc_bus.bdev; + device_detach(bdev); + device_delete_child(self, bdev); + } + /* during module unload there are lots of children leftover */ + device_delete_children(self); + + if (sc->sc_irq_res && sc->sc_intr_hdl) { + /* + * only call ohci_detach() after ohci_init() + */ + ohci_detach(sc); + + int err = bus_teardown_intr(self, sc->sc_irq_res, sc->sc_intr_hdl); + + if (err) { + /* XXX or should we panic? */ + device_printf(self, "Could not tear down irq, %d\n", + err); + } + sc->sc_intr_hdl = NULL; + } + if (sc->sc_irq_res) { + bus_release_resource(self, SYS_RES_IRQ, 0, sc->sc_irq_res); + sc->sc_irq_res = NULL; + } + if (sc->sc_io_res) { + bus_release_resource(self, SYS_RES_MEMORY, 0, + sc->sc_io_res); + sc->sc_io_res = NULL; + } + usb_bus_mem_free_all(&sc->sc_bus, &ohci_iterate_hw_softc); + + return (0); +} + +static device_method_t bhnd_ohci_methods[] = { + /* Device interface */ + DEVMETHOD(device_probe, bhnd_ohci_probe), + DEVMETHOD(device_attach, bhnd_ohci_attach), + DEVMETHOD(device_detach, bhnd_ohci_detach), + DEVMETHOD(device_suspend, bus_generic_suspend), + DEVMETHOD(device_resume, bus_generic_resume), + DEVMETHOD(device_shutdown, bus_generic_shutdown), + + DEVMETHOD_END +}; + +static driver_t ohci_driver = { + .name = "ohci", + .methods = bhnd_ohci_methods, + .size = sizeof(struct ohci_softc), +}; + +static devclass_t ohci_devclass; + +DRIVER_MODULE(ohci, bhnd_usb, ohci_driver, ohci_devclass, 0, 0); +MODULE_DEPEND(ohci, usb, 1, 1, 1); Added: head/sys/dev/bhnd/cores/usb/bhnd_usb.c ============================================================================== --- /dev/null 00:00:00 1970 (empty, because file is newly added) +++ head/sys/dev/bhnd/cores/usb/bhnd_usb.c Mon Sep 5 16:06:52 2016 (r305426) @@ -0,0 +1,487 @@ +/*- + * Copyright (c) 2010, Aleksandr Rybalko + * All rights reserved. + * + * 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 unmodified, 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. + */ + +#include +__FBSDID("$FreeBSD$"); + +/* + * Ported version of BroadCom USB core driver from ZRouter project + */ +#include +#include +#include +#include +#include +#include +#include +#include + +#include +#include + +#include +#include + +#include + +#include "bhnd_usbvar.h" + +/****************************** Variables ************************************/ +static const struct bhnd_device bhnd_usb_devs[] = { + BHND_DEVICE(BCM, USB, "USB1.1 Host/Device core", NULL), + BHND_DEVICE(BCM, USB20H, "USB2.0 Host core", NULL), + BHND_DEVICE(BCM, USB20D, "USB2.0 Device core", NULL), + BHND_DEVICE(BCM, USB11H, "USB1.1 Host core", NULL), + BHND_DEVICE(BCM, USB11D, "USB1.1 Device core", NULL), + BHND_DEVICE_END +}; + +/****************************** Prototypes ***********************************/ + +static int bhnd_usb_attach(device_t); +static int bhnd_usb_probe(device_t); +static device_t bhnd_usb_add_child(device_t dev, u_int order, const char *name, + int unit); +static int bhnd_usb_print_all_resources(device_t dev); +static int bhnd_usb_print_child(device_t bus, device_t child); + +static struct resource * bhnd_usb_alloc_resource(device_t bus, + device_t child, int type, int *rid, + rman_res_t start, rman_res_t end, + rman_res_t count, u_int flags); +static int bhnd_usb_release_resource(device_t dev, + device_t child, int type, int rid, + struct resource *r); + +static struct resource_list * bhnd_usb_get_reslist(device_t dev, + device_t child); + +static int +bhnd_usb_probe(device_t dev) +{ + const struct bhnd_device *id; + + id = bhnd_device_lookup(dev, bhnd_usb_devs, sizeof(bhnd_usb_devs[0])); + if (id == NULL) + return (ENXIO); + + device_set_desc(dev, id->desc); + return (BUS_PROBE_DEFAULT); +} + +static int +bhnd_usb_attach(device_t dev) +{ + struct bhnd_usb_softc *sc; + int rid; + uint32_t tmp; + int tries, err; + + sc = device_get_softc(dev); + + BHND_BUS_RESET_CORE(device_get_parent(dev), dev, 0); + + /* + * Allocate the resources which the parent bus has already + * determined for us. + * XXX: There are few windows (usually 2), RID should be chip-specific + */ + rid = 0; + sc->sc_mem = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid, RF_ACTIVE); + if (sc->sc_mem == NULL) { + BHND_ERROR_DEV(dev, "unable to allocate memory"); + return (ENXIO); + } + + sc->sc_bt = rman_get_bustag(sc->sc_mem); + sc->sc_bh = rman_get_bushandle(sc->sc_mem); + sc->sc_maddr = rman_get_start(sc->sc_mem); + sc->sc_msize = rman_get_size(sc->sc_mem); + + rid = 0; + sc->sc_irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid, + RF_SHAREABLE | RF_ACTIVE); + if (sc->sc_irq == NULL) { + BHND_ERROR_DEV(dev, "unable to allocate IRQ"); + return (ENXIO); + } + + sc->sc_irqn = rman_get_start(sc->sc_irq); + + sc->mem_rman.rm_start = sc->sc_maddr; + sc->mem_rman.rm_end = sc->sc_maddr + sc->sc_msize - 1; + sc->mem_rman.rm_type = RMAN_ARRAY; + sc->mem_rman.rm_descr = "BHND USB core I/O memory addresses"; + if (rman_init(&sc->mem_rman) != 0 || + rman_manage_region(&sc->mem_rman, sc->mem_rman.rm_start, + sc->mem_rman.rm_end) != 0) { + panic("%s: sc->mem_rman", __func__); + } + + sc->irq_rman.rm_start = sc->sc_irqn; + sc->irq_rman.rm_end = sc->sc_irqn; + sc->irq_rman.rm_type = RMAN_ARRAY; + sc->irq_rman.rm_descr = "BHND USB core IRQ"; + /* + * BHND USB share same IRQ between OHCI and EHCI + */ + if (rman_init(&sc->irq_rman) != 0 || + rman_manage_region(&sc->irq_rman, sc->irq_rman.rm_start, + sc->irq_rman.rm_end) != 0) + panic("%s: failed to set up IRQ rman", __func__); + + /* TODO: macros for registers */ + bus_write_4(sc->sc_mem, 0x200, 0x7ff); + DELAY(100); + +#define OHCI_CONTROL 0x04 + bus_write_4(sc->sc_mem, OHCI_CONTROL, 0); + + if ( bhnd_get_device(dev) == BHND_COREID_USB20H) { + + uint32_t rev; + BHND_INFO_DEV(dev, "USB HOST 2.0 setup for rev %d", rev); + rev = bhnd_get_hwrev(dev); + if (rev == 1/* ? == 2 */) { + /* SiBa code */ + + /* Change Flush control reg */ + tmp = bus_read_4(sc->sc_mem, 0x400) & ~0x8; + bus_write_4(sc->sc_mem, 0x400, tmp); + tmp = bus_read_4(sc->sc_mem, 0x400); + BHND_DEBUG_DEV(dev, "USB20H fcr: 0x%x", tmp); + + /* Change Shim control reg */ + tmp = bus_read_4(sc->sc_mem, 0x304) & ~0x100; + bus_write_4(sc->sc_mem, 0x304, tmp); + tmp = bus_read_4(sc->sc_mem, 0x304); + BHND_DEBUG_DEV(dev, "USB20H shim: 0x%x", tmp); + } else if (rev >= 5) { + /* BCMA code */ + err = bhnd_alloc_pmu(dev); + if(err) { + BHND_ERROR_DEV(dev, "can't alloc pmu: %d", err); + return (err); + } + + err = bhnd_request_ext_rsrc(dev, 1); + if(err) { + BHND_ERROR_DEV(dev, "can't req ext: %d", err); + return (err); + } + /* Take out of resets */ + bus_write_4(sc->sc_mem, 0x200, 0x4ff); + DELAY(25); + bus_write_4(sc->sc_mem, 0x200, 0x6ff); + DELAY(25); + + /* Make sure digital and AFE are locked in USB PHY */ + bus_write_4(sc->sc_mem, 0x524, 0x6b); + DELAY(50); + bus_read_4(sc->sc_mem, 0x524); + DELAY(50); + bus_write_4(sc->sc_mem, 0x524, 0xab); + DELAY(50); + bus_read_4(sc->sc_mem, 0x524); + DELAY(50); + bus_write_4(sc->sc_mem, 0x524, 0x2b); + DELAY(50); + bus_read_4(sc->sc_mem, 0x524); + DELAY(50); + bus_write_4(sc->sc_mem, 0x524, 0x10ab); + DELAY(50); + bus_read_4(sc->sc_mem, 0x524); + + tries = 10000; + for (;;) { + DELAY(10); + tmp = bus_read_4(sc->sc_mem, 0x528); + if (tmp & 0xc000) + break; + if (--tries != 0) + continue; + + tmp = bus_read_4(sc->sc_mem, 0x528); + BHND_ERROR_DEV(dev, "USB20H mdio_rddata 0x%08x", tmp); + } + + /* XXX: Puzzle code */ + bus_write_4(sc->sc_mem, 0x528, 0x80000000); + bus_read_4(sc->sc_mem, 0x314); + DELAY(265); + bus_write_4(sc->sc_mem, 0x200, 0x7ff); + DELAY(10); + + /* Take USB and HSIC out of non-driving modes */ + bus_write_4(sc->sc_mem, 0x510, 0); + } + } + + bus_generic_probe(dev); + + if (bhnd_get_device(dev) == BHND_COREID_USB20H && + ( bhnd_get_hwrev(dev) > 0)) + bhnd_usb_add_child(dev, 0, "ehci", -1); + bhnd_usb_add_child(dev, 1, "ohci", -1); + + bus_generic_attach(dev); + + return (0); +} + +static struct resource * +bhnd_usb_alloc_resource(device_t bus, device_t child, int type, int *rid, + rman_res_t start, rman_res_t end, rman_res_t count, u_int flags) +{ + struct resource *rv; + struct resource_list *rl; + struct resource_list_entry *rle; + int isdefault, needactivate; + struct bhnd_usb_softc *sc = device_get_softc(bus); + + isdefault = RMAN_IS_DEFAULT_RANGE(start,end); + needactivate = flags & RF_ACTIVE; + rl = BUS_GET_RESOURCE_LIST(bus, child); + rle = NULL; + + if (isdefault) { + BHND_INFO_DEV(bus, "trying allocate def %d - %d for %s", type, + *rid, device_get_nameunit(child) ); + rle = resource_list_find(rl, type, *rid); + if (rle == NULL) + return (NULL); + if (rle->res != NULL) + panic("%s: resource entry is busy", __func__); + start = rle->start; + end = rle->end; + count = rle->count; + } else { + BHND_INFO_DEV(bus, "trying allocate %d - %d (%jx-%jx) for %s", type, + *rid, start, end, device_get_nameunit(child) ); + } + + /* + * If the request is for a resource which we manage, + * attempt to satisfy the allocation ourselves. + */ + if (type == SYS_RES_MEMORY) { + + rv = rman_reserve_resource(&sc->mem_rman, start, end, count, + flags, child); + if (rv == 0) { + BHND_ERROR_DEV(bus, "could not reserve resource"); + return (0); + } + + rman_set_rid(rv, *rid); + + if (needactivate && + bus_activate_resource(child, type, *rid, rv)) { + BHND_ERROR_DEV(bus, "could not activate resource"); + rman_release_resource(rv); + return (0); + } + + return (rv); + } + + if (type == SYS_RES_IRQ) { + + rv = rman_reserve_resource(&sc->irq_rman, start, end, count, + flags, child); + if (rv == 0) { + BHND_ERROR_DEV(bus, "could not reserve resource"); + return (0); + } + + rman_set_rid(rv, *rid); + + if (needactivate && + bus_activate_resource(child, type, *rid, rv)) { + BHND_ERROR_DEV(bus, "could not activate resource"); + rman_release_resource(rv); + return (0); + } + + return (rv); + } + + /* + * Pass the request to the parent. + */ + return (resource_list_alloc(rl, bus, child, type, rid, + start, end, count, flags)); +} + +static struct resource_list * +bhnd_usb_get_reslist(device_t dev, device_t child) +{ + struct bhnd_usb_devinfo *sdi; + + sdi = device_get_ivars(child); + + return (&sdi->sdi_rl); +} + +static int +bhnd_usb_release_resource(device_t dev, device_t child, int type, + int rid, struct resource *r) +{ + struct resource_list *rl; + struct resource_list_entry *rle; + + rl = bhnd_usb_get_reslist(dev, child); + if (rl == NULL) + return (EINVAL); + rle = resource_list_find(rl, type, rid); + if (rle == NULL) + return (EINVAL); + rman_release_resource(r); + rle->res = NULL; + + return (0); +} + +static int +bhnd_usb_print_all_resources(device_t dev) +{ + struct bhnd_usb_devinfo *sdi; + struct resource_list *rl; + int retval; + + retval = 0; + sdi = device_get_ivars(dev); + rl = &sdi->sdi_rl; + + if (STAILQ_FIRST(rl)) + retval += printf(" at"); + + retval += resource_list_print_type(rl, "mem", SYS_RES_MEMORY, "%jx"); + retval += resource_list_print_type(rl, "irq", SYS_RES_IRQ, "%ld"); + + return (retval); +} + +static int +bhnd_usb_print_child(device_t bus, device_t child) +{ + int retval = 0; + + retval += bus_print_child_header(bus, child); + retval += bhnd_usb_print_all_resources(child); + if (device_get_flags(child)) + retval += printf(" flags %#x", device_get_flags(child)); + retval += printf(" on %s\n", device_get_nameunit(bus)); + + return (retval); +} + +static device_t +bhnd_usb_add_child(device_t dev, u_int order, const char *name, int unit) +{ + struct bhnd_usb_softc *sc; + struct bhnd_usb_devinfo *sdi; + device_t child; + + sc = device_get_softc(dev); + child = device_add_child_ordered(dev, order, name, unit); + + if (child == NULL) + return (NULL); + + sdi = malloc(sizeof(struct bhnd_usb_devinfo), M_DEVBUF, M_NOWAIT|M_ZERO); + if (sdi == NULL) + return (NULL); + + if (strncmp(name, "ohci", 4) == 0) + { + sdi->sdi_maddr = sc->sc_maddr + 0x000; + sdi->sdi_msize = 0x200; + sdi->sdi_irq = sc->sc_irqn; + BHND_INFO_DEV(dev, "ohci: irq=%d maddr=0x%jx", sdi->sdi_irq, + sdi->sdi_maddr); + } + else if (strncmp(name, "ehci", 4) == 0) + { + sdi->sdi_maddr = sc->sc_maddr + 0x000; + sdi->sdi_msize = 0x1000; + sdi->sdi_irq = sc->sc_irqn; + BHND_INFO_DEV(dev, "ehci: irq=%d maddr=0x%jx", sdi->sdi_irq, + sdi->sdi_maddr); + } + else + { + panic("Unknown subdevice"); + /* Unknown subdevice */ + sdi->sdi_maddr = 1; + sdi->sdi_msize = 1; + sdi->sdi_irq = 1; + } + + resource_list_init(&sdi->sdi_rl); + + /* + * Determine memory window on bus and irq if one is needed. + */ + resource_list_add(&sdi->sdi_rl, SYS_RES_MEMORY, 0, + sdi->sdi_maddr, sdi->sdi_maddr + sdi->sdi_msize - 1, sdi->sdi_msize); + + resource_list_add(&sdi->sdi_rl, SYS_RES_IRQ, 0, + sdi->sdi_irq, sdi->sdi_irq, 1); + + device_set_ivars(child, sdi); + return (child); + +} + +static device_method_t bhnd_usb_methods[] = { + /* Device interface */ + DEVMETHOD(device_attach, bhnd_usb_attach), + DEVMETHOD(device_probe, bhnd_usb_probe), + + /* Bus interface */ + DEVMETHOD(bus_add_child, bhnd_usb_add_child), + DEVMETHOD(bus_alloc_resource, bhnd_usb_alloc_resource), + DEVMETHOD(bus_get_resource_list, bhnd_usb_get_reslist), + DEVMETHOD(bus_print_child, bhnd_usb_print_child), + DEVMETHOD(bus_release_resource, bhnd_usb_release_resource), + /* Bus interface: generic part */ + DEVMETHOD(bus_activate_resource, bus_generic_activate_resource), + DEVMETHOD(bus_deactivate_resource, bus_generic_deactivate_resource), + DEVMETHOD(bus_setup_intr, bus_generic_setup_intr), + DEVMETHOD(bus_teardown_intr, bus_generic_teardown_intr), + + DEVMETHOD_END +}; + +static devclass_t bhnd_usb_devclass; + +DEFINE_CLASS_0(bhnd_usb, bhnd_usb_driver, bhnd_usb_methods, + sizeof(struct bhnd_usb_softc)); +DRIVER_MODULE(bhnd_usb, bhnd, bhnd_usb_driver, bhnd_usb_devclass, 0, 0); + +MODULE_VERSION(bhnd_usb, 1); Added: head/sys/dev/bhnd/cores/usb/bhnd_usbvar.h ============================================================================== --- /dev/null 00:00:00 1970 (empty, because file is newly added) +++ head/sys/dev/bhnd/cores/usb/bhnd_usbvar.h Mon Sep 5 16:06:52 2016 (r305426) @@ -0,0 +1,59 @@ +/*- + * Copyright (c) 2010, Aleksandr Rybalko + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions *** DIFF OUTPUT TRUNCATED AT 1000 LINES ***