1292702Sadrian#include <sys/cdefs.h> 2292702Sadrian__FBSDID("$FreeBSD: stable/11/sys/mips/rt305x/rt305x_ohci.c 308401 2016-11-07 08:36:06Z hselasky $"); 3292702Sadrian 4292702Sadrian/*- 5292702Sadrian * Copyright (c) 2015 Stanislav Galabov. All rights reserved. 6292702Sadrian * Copyright (c) 2010,2011 Aleksandr Rybalko. All rights reserved. 7292702Sadrian * Copyright (c) 2007-2008 Hans Petter Selasky. All rights reserved. 8292702Sadrian * 9292702Sadrian * Redistribution and use in source and binary forms, with or without 10292702Sadrian * modification, are permitted provided that the following conditions 11292702Sadrian * are met: 12292702Sadrian * 1. Redistributions of source code must retain the above copyright 13292702Sadrian * notice, this list of conditions and the following disclaimer. 14292702Sadrian * 2. Redistributions in binary form must reproduce the above copyright 15292702Sadrian * notice, this list of conditions and the following disclaimer in the 16292702Sadrian * documentation and/or other materials provided with the distribution. 17292702Sadrian * 18292702Sadrian * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 19292702Sadrian * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 20292702Sadrian * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 21292702Sadrian * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 22292702Sadrian * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 23292702Sadrian * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 24292702Sadrian * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 25292702Sadrian * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 26292702Sadrian * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 27292702Sadrian * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 28292702Sadrian * SUCH DAMAGE. 29292702Sadrian */ 30292702Sadrian 31292702Sadrian#include <sys/stdint.h> 32292702Sadrian#include <sys/stddef.h> 33292702Sadrian#include <sys/param.h> 34292702Sadrian#include <sys/queue.h> 35292702Sadrian#include <sys/types.h> 36292702Sadrian#include <sys/systm.h> 37292702Sadrian#include <sys/kernel.h> 38292702Sadrian#include <sys/bus.h> 39292702Sadrian#include <sys/module.h> 40292702Sadrian#include <sys/lock.h> 41292702Sadrian#include <sys/mutex.h> 42292702Sadrian#include <sys/condvar.h> 43292702Sadrian#include <sys/sysctl.h> 44292702Sadrian#include <sys/sx.h> 45292702Sadrian#include <sys/unistd.h> 46292702Sadrian#include <sys/callout.h> 47292702Sadrian#include <sys/malloc.h> 48292702Sadrian#include <sys/priv.h> 49292702Sadrian#include <sys/rman.h> 50292702Sadrian 51292702Sadrian#include <dev/usb/usb.h> 52292702Sadrian#include <dev/usb/usbdi.h> 53292702Sadrian 54292702Sadrian#include <dev/usb/usb_core.h> 55292702Sadrian#include <dev/usb/usb_busdma.h> 56292702Sadrian#include <dev/usb/usb_process.h> 57292702Sadrian#include <dev/usb/usb_util.h> 58292702Sadrian 59292702Sadrian#include <dev/usb/usb_controller.h> 60292702Sadrian#include <dev/usb/usb_bus.h> 61292702Sadrian 62292702Sadrian#include <dev/usb/controller/ohci.h> 63292702Sadrian 64292702Sadrian#include <mips/rt305x/rt305xreg.h> 65292702Sadrian#include <mips/rt305x/rt305x_sysctlvar.h> 66292702Sadrian 67292702Sadrian#define OHCI_HC_DEVSTR "Ralink integrated USB controller" 68292702Sadrian 69292702Sadrianstatic device_probe_t ohci_obio_probe; 70292702Sadrianstatic device_attach_t ohci_obio_attach; 71292702Sadrianstatic device_detach_t ohci_obio_detach; 72292702Sadrian 73292702Sadrianstatic int 74292702Sadrianohci_obio_probe(device_t self) 75292702Sadrian{ 76292702Sadrian device_set_desc(self, OHCI_HC_DEVSTR); 77292702Sadrian 78292702Sadrian return (BUS_PROBE_DEFAULT); 79292702Sadrian} 80292702Sadrian 81292702Sadrianstatic int 82292702Sadrianohci_obio_attach(device_t self) 83292702Sadrian{ 84292702Sadrian ohci_softc_t *sc = device_get_softc(self); 85292702Sadrian uint32_t reg; 86292702Sadrian int err; 87292702Sadrian int rid; 88292702Sadrian 89292702Sadrian /* setup controller interface softc */ 90292702Sadrian reg = rt305x_sysctl_get(SYSCTL_SYSCFG1); 91292702Sadrian reg |= SYSCTL_SYSCFG1_USB0_HOST_MODE; 92292702Sadrian rt305x_sysctl_set(SYSCTL_SYSCFG1, reg); 93292702Sadrian 94292702Sadrian reg = rt305x_sysctl_get(SYSCTL_CLKCFG1); 95292702Sadrian reg |= SYSCTL_CLKCFG1_UPHY0_CLK_EN; 96292702Sadrian#ifdef MT7620 97292702Sadrian reg |= SYSCTL_CLKCFG1_UPHY1_CLK_EN; 98292702Sadrian#endif 99292702Sadrian rt305x_sysctl_set(SYSCTL_CLKCFG1, reg); 100292702Sadrian 101292702Sadrian reg = rt305x_sysctl_get(SYSCTL_RSTCTRL); 102292702Sadrian reg |= SYSCTL_RSTCTRL_UPHY0 | SYSCTL_RSTCTRL_UPHY1; 103292702Sadrian rt305x_sysctl_set(SYSCTL_RSTCTRL, reg); 104292702Sadrian reg &= ~(SYSCTL_RSTCTRL_UPHY0 | SYSCTL_RSTCTRL_UPHY1); 105292702Sadrian DELAY(100000); 106292702Sadrian rt305x_sysctl_set(SYSCTL_RSTCTRL, reg); 107292702Sadrian DELAY(100000); 108292702Sadrian 109292702Sadrian /* initialise some bus fields */ 110292702Sadrian sc->sc_bus.parent = self; 111292702Sadrian sc->sc_bus.devices = sc->sc_devices; 112292702Sadrian sc->sc_bus.devices_max = OHCI_MAX_DEVICES; 113292702Sadrian sc->sc_bus.dma_bits = 32; 114292702Sadrian 115292702Sadrian /* get all DMA memory */ 116292702Sadrian if (usb_bus_mem_alloc_all(&sc->sc_bus, 117292702Sadrian USB_GET_DMA_TAG(self), &ohci_iterate_hw_softc)) { 118292702Sadrian printf("No mem\n"); 119292702Sadrian return (ENOMEM); 120292702Sadrian } 121292702Sadrian 122292702Sadrian rid = 0; 123292702Sadrian sc->sc_io_res = bus_alloc_resource_any(self, SYS_RES_MEMORY, &rid, 124292702Sadrian RF_ACTIVE); 125292702Sadrian if (!sc->sc_io_res) { 126292702Sadrian device_printf(self, "Could not map memory\n"); 127292702Sadrian goto error; 128292702Sadrian } 129292702Sadrian sc->sc_io_tag = rman_get_bustag(sc->sc_io_res); 130292702Sadrian sc->sc_io_hdl = rman_get_bushandle(sc->sc_io_res); 131292702Sadrian sc->sc_io_size = rman_get_size(sc->sc_io_res); 132292702Sadrian 133292702Sadrian rid = 0; 134292702Sadrian sc->sc_irq_res = bus_alloc_resource_any(self, SYS_RES_IRQ, &rid, 135292702Sadrian RF_SHAREABLE | RF_ACTIVE); 136292702Sadrian if (sc->sc_irq_res == NULL) { 137292702Sadrian device_printf(self, "Could not allocate irq\n"); 138292702Sadrian goto error; 139292702Sadrian } 140292702Sadrian 141292702Sadrian sc->sc_bus.bdev = device_add_child(self, "usbus", -1); 142292702Sadrian if (!(sc->sc_bus.bdev)) { 143292702Sadrian device_printf(self, "Could not add USB device\n"); 144292702Sadrian goto error; 145292702Sadrian } 146292702Sadrian device_set_ivars(sc->sc_bus.bdev, &sc->sc_bus); 147292702Sadrian device_set_desc(sc->sc_bus.bdev, OHCI_HC_DEVSTR); 148292702Sadrian 149292702Sadrian sprintf(sc->sc_vendor, "Ralink"); 150292702Sadrian 151292702Sadrian err = bus_setup_intr(self, sc->sc_irq_res, INTR_TYPE_BIO | INTR_MPSAFE, 152292702Sadrian NULL, (driver_intr_t *)ohci_interrupt, sc, &sc->sc_intr_hdl); 153292702Sadrian if (err) { 154292702Sadrian device_printf(self, "Could not setup irq, %d\n", err); 155292702Sadrian sc->sc_intr_hdl = NULL; 156292702Sadrian goto error; 157292702Sadrian } 158292702Sadrian 159292702Sadrian err = ohci_init(sc); 160292702Sadrian if (!err) { 161292702Sadrian err = device_probe_and_attach(sc->sc_bus.bdev); 162292702Sadrian } 163292702Sadrian if (err) { 164292702Sadrian device_printf(self, "USB init failed err=%d\n", err); 165292702Sadrian goto error; 166292702Sadrian } 167292702Sadrian return (0); 168292702Sadrian 169292702Sadrianerror: 170292702Sadrian ohci_obio_detach(self); 171292702Sadrian return (ENXIO); 172292702Sadrian} 173292702Sadrian 174292702Sadrianstatic int 175292702Sadrianohci_obio_detach(device_t self) 176292702Sadrian{ 177292702Sadrian ohci_softc_t *sc = device_get_softc(self); 178292702Sadrian int err; 179292702Sadrian 180292702Sadrian /* during module unload there are lots of children leftover */ 181292702Sadrian device_delete_children(self); 182292702Sadrian 183292702Sadrian if (sc->sc_irq_res && sc->sc_intr_hdl) { 184292702Sadrian /* 185292702Sadrian * only call ohci_detach() after ohci_init() 186292702Sadrian */ 187292702Sadrian ohci_detach(sc); 188292702Sadrian 189292702Sadrian /* Stop OHCI clock */ 190292702Sadrian rt305x_sysctl_set(SYSCTL_CLKCFG1, 191292702Sadrian rt305x_sysctl_get(SYSCTL_CLKCFG1) & 192292702Sadrian ~(SYSCTL_CLKCFG1_UPHY0_CLK_EN 193292702Sadrian#ifdef MT7620 194292702Sadrian | SYSCTL_CLKCFG1_UPHY1_CLK_EN 195292702Sadrian#endif 196292702Sadrian )); 197292702Sadrian 198292702Sadrian err = bus_teardown_intr(self, sc->sc_irq_res, sc->sc_intr_hdl); 199292702Sadrian if (err) 200292702Sadrian device_printf(self, "Could not tear down irq, %d\n", 201292702Sadrian err); 202292702Sadrian sc->sc_intr_hdl = NULL; 203292702Sadrian } 204292702Sadrian if (sc->sc_irq_res) { 205292702Sadrian bus_release_resource(self, SYS_RES_IRQ, 0, 206292702Sadrian sc->sc_irq_res); 207292702Sadrian sc->sc_irq_res = NULL; 208292702Sadrian } 209292702Sadrian if (sc->sc_io_res) { 210292702Sadrian bus_release_resource(self, SYS_RES_MEMORY, 0, 211292702Sadrian sc->sc_io_res); 212292702Sadrian sc->sc_io_res = NULL; 213292702Sadrian } 214292702Sadrian usb_bus_mem_free_all(&sc->sc_bus, &ohci_iterate_hw_softc); 215292702Sadrian 216292702Sadrian return (0); 217292702Sadrian} 218292702Sadrian 219292702Sadrianstatic device_method_t ohci_obio_methods[] = { 220292702Sadrian /* Device interface */ 221292702Sadrian DEVMETHOD(device_probe, ohci_obio_probe), 222292702Sadrian DEVMETHOD(device_attach, ohci_obio_attach), 223292702Sadrian DEVMETHOD(device_detach, ohci_obio_detach), 224292702Sadrian DEVMETHOD(device_suspend, bus_generic_suspend), 225292702Sadrian DEVMETHOD(device_resume, bus_generic_resume), 226292702Sadrian DEVMETHOD(device_shutdown, bus_generic_shutdown), 227292702Sadrian 228292702Sadrian DEVMETHOD_END 229292702Sadrian}; 230292702Sadrian 231292702Sadrianstatic driver_t ohci_obio_driver = { 232292702Sadrian .name = "ohci", 233292702Sadrian .methods = ohci_obio_methods, 234292702Sadrian .size = sizeof(ohci_softc_t), 235292702Sadrian}; 236292702Sadrian 237292702Sadrianstatic devclass_t ohci_obio_devclass; 238292702Sadrian 239292702SadrianDRIVER_MODULE(ohci, obio, ohci_obio_driver, ohci_obio_devclass, 0, 0); 240