at91_ohci.c revision 198151
1/*- 2 * Copyright (c) 2006 M. Warner Losh. All rights reserved. 3 * 4 * Redistribution and use in source and binary forms, with or without 5 * modification, are permitted provided that the following conditions 6 * are met: 7 * 1. Redistributions of source code must retain the above copyright 8 * notice, this list of conditions and the following disclaimer. 9 * 2. Redistributions in binary form must reproduce the above copyright 10 * notice, this list of conditions and the following disclaimer in the 11 * documentation and/or other materials provided with the distribution. 12 * 13 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 14 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 15 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 16 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 17 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 18 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 19 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 20 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 21 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 22 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 23 */ 24 25#include <sys/cdefs.h> 26__FBSDID("$FreeBSD: head/sys/dev/usb/controller/ohci_atmelarm.c 198151 2009-10-15 20:07:08Z thompsa $"); 27 28#include <sys/stdint.h> 29#include <sys/stddef.h> 30#include <sys/param.h> 31#include <sys/queue.h> 32#include <sys/types.h> 33#include <sys/systm.h> 34#include <sys/kernel.h> 35#include <sys/bus.h> 36#include <sys/linker_set.h> 37#include <sys/module.h> 38#include <sys/lock.h> 39#include <sys/mutex.h> 40#include <sys/condvar.h> 41#include <sys/sysctl.h> 42#include <sys/sx.h> 43#include <sys/unistd.h> 44#include <sys/callout.h> 45#include <sys/malloc.h> 46#include <sys/priv.h> 47 48#include <dev/usb/usb.h> 49#include <dev/usb/usbdi.h> 50 51#include <dev/usb/usb_core.h> 52#include <dev/usb/usb_busdma.h> 53#include <dev/usb/usb_process.h> 54#include <dev/usb/usb_util.h> 55 56#include <dev/usb/usb_controller.h> 57#include <dev/usb/usb_bus.h> 58#include <dev/usb/controller/ohci.h> 59#include <dev/usb/controller/ohcireg.h> 60 61#include <sys/rman.h> 62 63#include <arm/at91/at91_pmcvar.h> 64 65#define MEM_RID 0 66 67static device_probe_t ohci_atmelarm_probe; 68static device_attach_t ohci_atmelarm_attach; 69static device_detach_t ohci_atmelarm_detach; 70 71struct at91_ohci_softc { 72 struct ohci_softc sc_ohci; /* must be first */ 73 struct at91_pmc_clock *iclk; 74 struct at91_pmc_clock *fclk; 75}; 76 77static int 78ohci_atmelarm_probe(device_t dev) 79{ 80 device_set_desc(dev, "AT91 integrated OHCI controller"); 81 return (BUS_PROBE_DEFAULT); 82} 83 84static int 85ohci_atmelarm_attach(device_t dev) 86{ 87 struct at91_ohci_softc *sc = device_get_softc(dev); 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->iclk = at91_pmc_clock_ref("ohci_clk"); 102 sc->fclk = at91_pmc_clock_ref("uhpck"); 103 104 sc->sc_ohci.sc_dev = dev; 105 106 rid = MEM_RID; 107 sc->sc_ohci.sc_io_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, 108 &rid, RF_ACTIVE); 109 110 if (!(sc->sc_ohci.sc_io_res)) { 111 err = ENOMEM; 112 goto error; 113 } 114 sc->sc_ohci.sc_io_tag = rman_get_bustag(sc->sc_ohci.sc_io_res); 115 sc->sc_ohci.sc_io_hdl = rman_get_bushandle(sc->sc_ohci.sc_io_res); 116 sc->sc_ohci.sc_io_size = rman_get_size(sc->sc_ohci.sc_io_res); 117 118 rid = 0; 119 sc->sc_ohci.sc_irq_res = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid, 120 RF_ACTIVE); 121 if (!(sc->sc_ohci.sc_irq_res)) { 122 goto error; 123 } 124 sc->sc_ohci.sc_bus.bdev = device_add_child(dev, "usbus", -1); 125 if (!(sc->sc_ohci.sc_bus.bdev)) { 126 goto error; 127 } 128 device_set_ivars(sc->sc_ohci.sc_bus.bdev, &sc->sc_ohci.sc_bus); 129 130 strlcpy(sc->sc_ohci.sc_vendor, "Atmel", sizeof(sc->sc_ohci.sc_vendor)); 131 132#if (__FreeBSD_version >= 700031) 133 err = bus_setup_intr(dev, sc->sc_ohci.sc_irq_res, INTR_TYPE_BIO | INTR_MPSAFE, 134 NULL, (driver_intr_t *)ohci_interrupt, sc, &sc->sc_ohci.sc_intr_hdl); 135#else 136 err = bus_setup_intr(dev, sc->sc_ohci.sc_irq_res, INTR_TYPE_BIO | INTR_MPSAFE, 137 (driver_intr_t *)ohci_interrupt, sc, &sc->sc_ohci.sc_intr_hdl); 138#endif 139 if (err) { 140 sc->sc_ohci.sc_intr_hdl = NULL; 141 goto error; 142 } 143 /* 144 * turn on the clocks from the AT91's point of view. Keep the unit in reset. 145 */ 146 at91_pmc_clock_enable(sc->iclk); 147 at91_pmc_clock_enable(sc->fclk); 148 bus_space_write_4(sc->sc_ohci.sc_io_tag, sc->sc_ohci.sc_io_hdl, 149 OHCI_CONTROL, 0); 150 151 err = ohci_init(&sc->sc_ohci); 152 if (!err) { 153 err = device_probe_and_attach(sc->sc_ohci.sc_bus.bdev); 154 } 155 if (err) { 156 goto error; 157 } 158 return (0); 159 160error: 161 ohci_atmelarm_detach(dev); 162 return (ENXIO); 163} 164 165static int 166ohci_atmelarm_detach(device_t dev) 167{ 168 struct at91_ohci_softc *sc = device_get_softc(dev); 169 device_t bdev; 170 int err; 171 172 if (sc->sc_ohci.sc_bus.bdev) { 173 bdev = sc->sc_ohci.sc_bus.bdev; 174 device_detach(bdev); 175 device_delete_child(dev, bdev); 176 } 177 /* during module unload there are lots of children leftover */ 178 device_delete_all_children(dev); 179 180 /* 181 * Put the controller into reset, then disable clocks and do 182 * the MI tear down. We have to disable the clocks/hardware 183 * after we do the rest of the teardown. We also disable the 184 * clocks in the opposite order we acquire them, but that 185 * doesn't seem to be absolutely necessary. We free up the 186 * clocks after we disable them, so the system could, in 187 * theory, reuse them. 188 */ 189 bus_space_write_4(sc->sc_ohci.sc_io_tag, sc->sc_ohci.sc_io_hdl, 190 OHCI_CONTROL, 0); 191 192 at91_pmc_clock_disable(sc->fclk); 193 at91_pmc_clock_disable(sc->iclk); 194 at91_pmc_clock_deref(sc->fclk); 195 at91_pmc_clock_deref(sc->iclk); 196 197 if (sc->sc_ohci.sc_irq_res && sc->sc_ohci.sc_intr_hdl) { 198 /* 199 * only call ohci_detach() after ohci_init() 200 */ 201 ohci_detach(&sc->sc_ohci); 202 203 err = bus_teardown_intr(dev, sc->sc_ohci.sc_irq_res, sc->sc_ohci.sc_intr_hdl); 204 sc->sc_ohci.sc_intr_hdl = NULL; 205 } 206 if (sc->sc_ohci.sc_irq_res) { 207 bus_release_resource(dev, SYS_RES_IRQ, 0, sc->sc_ohci.sc_irq_res); 208 sc->sc_ohci.sc_irq_res = NULL; 209 } 210 if (sc->sc_ohci.sc_io_res) { 211 bus_release_resource(dev, SYS_RES_MEMORY, MEM_RID, 212 sc->sc_ohci.sc_io_res); 213 sc->sc_ohci.sc_io_res = NULL; 214 } 215 usb_bus_mem_free_all(&sc->sc_ohci.sc_bus, &ohci_iterate_hw_softc); 216 217 return (0); 218} 219 220static device_method_t ohci_methods[] = { 221 /* Device interface */ 222 DEVMETHOD(device_probe, ohci_atmelarm_probe), 223 DEVMETHOD(device_attach, ohci_atmelarm_attach), 224 DEVMETHOD(device_detach, ohci_atmelarm_detach), 225 DEVMETHOD(device_shutdown, bus_generic_shutdown), 226 227 /* Bus interface */ 228 DEVMETHOD(bus_print_child, bus_generic_print_child), 229 230 {0, 0} 231}; 232 233static driver_t ohci_driver = { 234 "ohci", 235 ohci_methods, 236 sizeof(struct at91_ohci_softc), 237}; 238 239static devclass_t ohci_devclass; 240 241DRIVER_MODULE(ohci, atmelarm, ohci_driver, ohci_devclass, 0, 0); 242MODULE_DEPEND(ohci, usb, 1, 1, 1); 243