at91_ohci.c revision 190633
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 190633 2009-04-01 20:23:47Z piso $"); 27 28#include <dev/usb/usb_mfunc.h> 29#include <dev/usb/usb.h> 30 31#include <dev/usb/usb_core.h> 32#include <dev/usb/usb_busdma.h> 33#include <dev/usb/usb_process.h> 34#include <dev/usb/usb_sw_transfer.h> 35#include <dev/usb/usb_util.h> 36 37#include <dev/usb/usb_controller.h> 38#include <dev/usb/usb_bus.h> 39#include <dev/usb/controller/ohci.h> 40 41#include <sys/rman.h> 42 43#include <arm/at91/at91_pmcvar.h> 44 45#define MEM_RID 0 46 47static device_probe_t ohci_atmelarm_probe; 48static device_attach_t ohci_atmelarm_attach; 49static device_detach_t ohci_atmelarm_detach; 50 51struct at91_ohci_softc { 52 struct ohci_softc sc_ohci; /* must be first */ 53 struct at91_pmc_clock *iclk; 54 struct at91_pmc_clock *fclk; 55}; 56 57static int 58ohci_atmelarm_probe(device_t dev) 59{ 60 device_set_desc(dev, "AT91 integrated OHCI controller"); 61 return (BUS_PROBE_DEFAULT); 62} 63 64static int 65ohci_atmelarm_attach(device_t dev) 66{ 67 struct at91_ohci_softc *sc = device_get_softc(dev); 68 int err; 69 int rid; 70 71 /* initialise some bus fields */ 72 sc->sc_ohci.sc_bus.parent = dev; 73 sc->sc_ohci.sc_bus.devices = sc->sc_ohci.sc_devices; 74 sc->sc_ohci.sc_bus.devices_max = OHCI_MAX_DEVICES; 75 76 /* get all DMA memory */ 77 if (usb2_bus_mem_alloc_all(&sc->sc_ohci.sc_bus, 78 USB_GET_DMA_TAG(dev), &ohci_iterate_hw_softc)) { 79 return (ENOMEM); 80 } 81 sc->iclk = at91_pmc_clock_ref("ohci_clk"); 82 sc->fclk = at91_pmc_clock_ref("uhpck"); 83 84 sc->sc_ohci.sc_dev = dev; 85 86 rid = MEM_RID; 87 sc->sc_ohci.sc_io_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, 88 &rid, RF_ACTIVE); 89 90 if (!(sc->sc_ohci.sc_io_res)) { 91 err = ENOMEM; 92 goto error; 93 } 94 sc->sc_ohci.sc_io_tag = rman_get_bustag(sc->sc_ohci.sc_io_res); 95 sc->sc_ohci.sc_io_hdl = rman_get_bushandle(sc->sc_ohci.sc_io_res); 96 sc->sc_ohci.sc_io_size = rman_get_size(sc->sc_ohci.sc_io_res); 97 98 rid = 0; 99 sc->sc_ohci.sc_irq_res = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid, 100 RF_ACTIVE); 101 if (!(sc->sc_ohci.sc_irq_res)) { 102 goto error; 103 } 104 sc->sc_ohci.sc_bus.bdev = device_add_child(dev, "usbus", -1); 105 if (!(sc->sc_ohci.sc_bus.bdev)) { 106 goto error; 107 } 108 device_set_ivars(sc->sc_ohci.sc_bus.bdev, &sc->sc_ohci.sc_bus); 109 110 strlcpy(sc->sc_ohci.sc_vendor, "Atmel", sizeof(sc->sc_ohci.sc_vendor)); 111 112#if (__FreeBSD_version >= 700031) 113 err = bus_setup_intr(dev, sc->sc_ohci.sc_irq_res, INTR_TYPE_BIO | INTR_MPSAFE, 114 NULL, (driver_intr_t *)ohci_interrupt, sc, &sc->sc_ohci.sc_intr_hdl); 115#else 116 err = bus_setup_intr(dev, sc->sc_ohci.sc_irq_res, INTR_TYPE_BIO | INTR_MPSAFE, 117 (driver_intr_t *)ohci_interrupt, sc, &sc->sc_ohci.sc_intr_hdl); 118#endif 119 if (err) { 120 sc->sc_ohci.sc_intr_hdl = NULL; 121 goto error; 122 } 123 /* 124 * turn on the clocks from the AT91's point of view. Keep the unit in reset. 125 */ 126 at91_pmc_clock_enable(sc->iclk); 127 at91_pmc_clock_enable(sc->fclk); 128 bus_space_write_4(sc->sc_ohci.sc_io_tag, sc->sc_ohci.sc_io_hdl, 129 OHCI_CONTROL, 0); 130 131 err = ohci_init(&sc->sc_ohci); 132 if (!err) { 133 err = device_probe_and_attach(sc->sc_ohci.sc_bus.bdev); 134 } 135 if (err) { 136 goto error; 137 } 138 return (0); 139 140error: 141 ohci_atmelarm_detach(dev); 142 return (ENXIO); 143} 144 145static int 146ohci_atmelarm_detach(device_t dev) 147{ 148 struct at91_ohci_softc *sc = device_get_softc(dev); 149 device_t bdev; 150 int err; 151 152 if (sc->sc_ohci.sc_bus.bdev) { 153 bdev = sc->sc_ohci.sc_bus.bdev; 154 device_detach(bdev); 155 device_delete_child(dev, bdev); 156 } 157 /* during module unload there are lots of children leftover */ 158 device_delete_all_children(dev); 159 160 /* 161 * Put the controller into reset, then disable clocks and do 162 * the MI tear down. We have to disable the clocks/hardware 163 * after we do the rest of the teardown. We also disable the 164 * clocks in the opposite order we acquire them, but that 165 * doesn't seem to be absolutely necessary. We free up the 166 * clocks after we disable them, so the system could, in 167 * theory, reuse them. 168 */ 169 bus_space_write_4(sc->sc_ohci.sc_io_tag, sc->sc_ohci.sc_io_hdl, 170 OHCI_CONTROL, 0); 171 172 at91_pmc_clock_disable(sc->fclk); 173 at91_pmc_clock_disable(sc->iclk); 174 at91_pmc_clock_deref(sc->fclk); 175 at91_pmc_clock_deref(sc->iclk); 176 177 if (sc->sc_ohci.sc_irq_res && sc->sc_ohci.sc_intr_hdl) { 178 /* 179 * only call ohci_detach() after ohci_init() 180 */ 181 ohci_detach(&sc->sc_ohci); 182 183 err = bus_teardown_intr(dev, sc->sc_ohci.sc_irq_res, sc->sc_ohci.sc_intr_hdl); 184 sc->sc_ohci.sc_intr_hdl = NULL; 185 } 186 if (sc->sc_ohci.sc_irq_res) { 187 bus_release_resource(dev, SYS_RES_IRQ, 0, sc->sc_ohci.sc_irq_res); 188 sc->sc_ohci.sc_irq_res = NULL; 189 } 190 if (sc->sc_ohci.sc_io_res) { 191 bus_release_resource(dev, SYS_RES_MEMORY, MEM_RID, 192 sc->sc_ohci.sc_io_res); 193 sc->sc_ohci.sc_io_res = NULL; 194 } 195 usb2_bus_mem_free_all(&sc->sc_ohci.sc_bus, &ohci_iterate_hw_softc); 196 197 return (0); 198} 199 200static device_method_t ohci_methods[] = { 201 /* Device interface */ 202 DEVMETHOD(device_probe, ohci_atmelarm_probe), 203 DEVMETHOD(device_attach, ohci_atmelarm_attach), 204 DEVMETHOD(device_detach, ohci_atmelarm_detach), 205 DEVMETHOD(device_shutdown, bus_generic_shutdown), 206 207 /* Bus interface */ 208 DEVMETHOD(bus_print_child, bus_generic_print_child), 209 210 {0, 0} 211}; 212 213static driver_t ohci_driver = { 214 "ohci", 215 ohci_methods, 216 sizeof(struct at91_ohci_softc), 217}; 218 219static devclass_t ohci_devclass; 220 221DRIVER_MODULE(ohci, atmelarm, ohci_driver, ohci_devclass, 0, 0); 222MODULE_DEPEND(ohci, usb, 1, 1, 1); 223