ohci_ec.c revision 201468
1/*- 2 * Copyright (c) 2009 Yohanes Nugroho <yohanes@gmail.com> 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 15 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 16 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 17 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 18 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 19 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 20 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 21 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 23 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 24 */ 25 26#include <sys/cdefs.h> 27__FBSDID("$FreeBSD: head/sys/arm/econa/ohci_ec.c 201468 2010-01-04 03:35:45Z rpaulo $"); 28 29#include <sys/stdint.h> 30#include <sys/stddef.h> 31#include <sys/param.h> 32#include <sys/queue.h> 33#include <sys/types.h> 34#include <sys/systm.h> 35#include <sys/kernel.h> 36#include <sys/bus.h> 37#include <sys/linker_set.h> 38#include <sys/module.h> 39#include <sys/lock.h> 40#include <sys/mutex.h> 41#include <sys/condvar.h> 42#include <sys/sysctl.h> 43#include <sys/sx.h> 44#include <sys/unistd.h> 45#include <sys/callout.h> 46#include <sys/malloc.h> 47#include <sys/priv.h> 48 49#include <dev/usb/usb.h> 50#include <dev/usb/usbdi.h> 51 52#include <dev/usb/usb_core.h> 53#include <dev/usb/usb_busdma.h> 54#include <dev/usb/usb_process.h> 55#include <dev/usb/usb_util.h> 56 57#include <dev/usb/usb_controller.h> 58#include <dev/usb/usb_bus.h> 59#include <dev/usb/controller/ohci.h> 60#include <dev/usb/controller/ohcireg.h> 61 62#include <sys/rman.h> 63 64#include <arm/econa/econa_reg.h> 65 66#define MEM_RID 0 67 68static device_probe_t ohci_ec_probe; 69static device_attach_t ohci_ec_attach; 70static device_detach_t ohci_ec_detach; 71 72struct ec_ohci_softc { 73 struct ohci_softc sc_ohci; /* must be first */ 74}; 75 76static int 77ohci_ec_probe(device_t dev) 78{ 79 device_set_desc(dev, "Econa integrated OHCI controller"); 80 return (BUS_PROBE_DEFAULT); 81} 82 83static int 84ohci_ec_attach(device_t dev) 85{ 86 struct ec_ohci_softc *sc = device_get_softc(dev); 87 bus_space_handle_t bsh; 88 int err; 89 int rid; 90 91 /* initialise some bus fields */ 92 sc->sc_ohci.sc_bus.parent = dev; 93 sc->sc_ohci.sc_bus.devices = sc->sc_ohci.sc_devices; 94 sc->sc_ohci.sc_bus.devices_max = OHCI_MAX_DEVICES; 95 96 /* get all DMA memory */ 97 if (usb_bus_mem_alloc_all(&sc->sc_ohci.sc_bus, 98 USB_GET_DMA_TAG(dev), &ohci_iterate_hw_softc)) { 99 return (ENOMEM); 100 } 101 sc->sc_ohci.sc_dev = dev; 102 103 rid = MEM_RID; 104 105 sc->sc_ohci.sc_io_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, 106 &rid, RF_ACTIVE); 107 108 if (!(sc->sc_ohci.sc_io_res)) { 109 err = ENOMEM; 110 goto error; 111 } 112 sc->sc_ohci.sc_io_tag = rman_get_bustag(sc->sc_ohci.sc_io_res); 113 bsh = rman_get_bushandle(sc->sc_ohci.sc_io_res); 114 /* Undocumented magic initialization */ 115 bus_space_write_4((sc)->sc_ohci.sc_io_tag, bsh,0x04, 0x146); 116 117 bus_space_write_4((sc)->sc_ohci.sc_io_tag, bsh,0x44, 0x0200); 118 119 DELAY(1000); 120 121 sc->sc_ohci.sc_io_size = rman_get_size(sc->sc_ohci.sc_io_res); 122 123 if (bus_space_subregion(sc->sc_ohci.sc_io_tag, bsh, 0x4000000, 124 sc->sc_ohci.sc_io_size, &sc->sc_ohci.sc_io_hdl) != 0) 125 panic("%s: unable to subregion USB host registers", 126 device_get_name(dev)); 127 128 rid = 0; 129 sc->sc_ohci.sc_irq_res = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid, 130 RF_ACTIVE); 131 if (!(sc->sc_ohci.sc_irq_res)) { 132 goto error; 133 } 134 sc->sc_ohci.sc_bus.bdev = device_add_child(dev, "usbus", -1); 135 if (!(sc->sc_ohci.sc_bus.bdev)) { 136 goto error; 137 } 138 device_set_ivars(sc->sc_ohci.sc_bus.bdev, &sc->sc_ohci.sc_bus); 139 140 strlcpy(sc->sc_ohci.sc_vendor, "Cavium", 141 sizeof(sc->sc_ohci.sc_vendor)); 142 143#if (__FreeBSD_version >= 700031) 144 err = bus_setup_intr(dev, sc->sc_ohci.sc_irq_res, 145 INTR_TYPE_BIO | INTR_MPSAFE, NULL, 146 (driver_intr_t *)ohci_interrupt, sc, 147 &sc->sc_ohci.sc_intr_hdl); 148#else 149 err = bus_setup_intr(dev, sc->sc_ohci.sc_irq_res, 150 INTR_TYPE_BIO | INTR_MPSAFE, 151 (driver_intr_t *)ohci_interrupt, sc, 152 &sc->sc_ohci.sc_intr_hdl); 153#endif 154 if (err) { 155 sc->sc_ohci.sc_intr_hdl = NULL; 156 goto error; 157 } 158 159 bus_space_write_4(sc->sc_ohci.sc_io_tag, sc->sc_ohci.sc_io_hdl, 160 OHCI_CONTROL, 0); 161 162 err = ohci_init(&sc->sc_ohci); 163 if (!err) { 164 err = device_probe_and_attach(sc->sc_ohci.sc_bus.bdev); 165 } 166 if (err) { 167 goto error; 168 } 169 return (0); 170 171error: 172 ohci_ec_detach(dev); 173 return (ENXIO); 174} 175 176static int 177ohci_ec_detach(device_t dev) 178{ 179 struct ec_ohci_softc *sc = device_get_softc(dev); 180 device_t bdev; 181 int err; 182 183 if (sc->sc_ohci.sc_bus.bdev) { 184 bdev = sc->sc_ohci.sc_bus.bdev; 185 device_detach(bdev); 186 device_delete_child(dev, bdev); 187 } 188 /* during module unload there are lots of children leftover */ 189 device_delete_all_children(dev); 190 191 bus_space_write_4(sc->sc_ohci.sc_io_tag, sc->sc_ohci.sc_io_hdl, 192 OHCI_CONTROL, 0); 193 194 if (sc->sc_ohci.sc_irq_res && sc->sc_ohci.sc_intr_hdl) { 195 /* 196 * only call ohci_detach() after ohci_init() 197 */ 198 ohci_detach(&sc->sc_ohci); 199 200 err = bus_teardown_intr(dev, sc->sc_ohci.sc_irq_res, 201 sc->sc_ohci.sc_intr_hdl); 202 sc->sc_ohci.sc_intr_hdl = NULL; 203 } 204 if (sc->sc_ohci.sc_irq_res) { 205 bus_release_resource(dev, SYS_RES_IRQ, 0, 206 sc->sc_ohci.sc_irq_res); 207 sc->sc_ohci.sc_irq_res = NULL; 208 } 209 if (sc->sc_ohci.sc_io_res) { 210 bus_release_resource(dev, SYS_RES_MEMORY, MEM_RID, 211 sc->sc_ohci.sc_io_res); 212 sc->sc_ohci.sc_io_res = NULL; 213 } 214 usb_bus_mem_free_all(&sc->sc_ohci.sc_bus, &ohci_iterate_hw_softc); 215 216 return (0); 217} 218 219static device_method_t ohci_methods[] = { 220 /* Device interface */ 221 DEVMETHOD(device_probe, ohci_ec_probe), 222 DEVMETHOD(device_attach, ohci_ec_attach), 223 DEVMETHOD(device_detach, ohci_ec_detach), 224 DEVMETHOD(device_shutdown, bus_generic_shutdown), 225 226 /* Bus interface */ 227 DEVMETHOD(bus_print_child, bus_generic_print_child), 228 229 {0, 0} 230}; 231 232static driver_t ohci_driver = { 233 "ohci", 234 ohci_methods, 235 sizeof(struct ec_ohci_softc), 236}; 237 238static devclass_t ohci_devclass; 239 240DRIVER_MODULE(ohci, econaarm, ohci_driver, ohci_devclass, 0, 0); 241MODULE_DEPEND(ohci, usb, 1, 1, 1); 242