musb_otg_atmelarm.c revision 186439
1254721Semaste/* $FreeBSD: head/sys/dev/usb2/controller/musb2_otg_atmelarm.c 186439 2008-12-23 17:36:25Z thompsa $ */ 2254721Semaste/*- 3254721Semaste * Copyright (c) 2008 Hans Petter Selasky. All rights reserved. 4254721Semaste * 5254721Semaste * Redistribution and use in source and binary forms, with or without 6254721Semaste * modification, are permitted provided that the following conditions 7254721Semaste * are met: 8254721Semaste * 1. Redistributions of source code must retain the above copyright 9254721Semaste * notice, this list of conditions and the following disclaimer. 10254721Semaste * 2. Redistributions in binary form must reproduce the above copyright 11254721Semaste * notice, this list of conditions and the following disclaimer in the 12254721Semaste * documentation and/or other materials provided with the distribution. 13254721Semaste * 14254721Semaste * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15254721Semaste * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16254721Semaste * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17254721Semaste * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18254721Semaste * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19254721Semaste * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20254721Semaste * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21254721Semaste * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22254721Semaste * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23254721Semaste * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24254721Semaste * SUCH DAMAGE. 25254721Semaste */ 26254721Semaste 27254721Semaste#include <dev/usb2/include/usb2_mfunc.h> 28254721Semaste#include <dev/usb2/include/usb2_defs.h> 29254721Semaste#include <dev/usb2/include/usb2_standard.h> 30254721Semaste 31254721Semaste#include <dev/usb2/core/usb2_core.h> 32254721Semaste#include <dev/usb2/core/usb2_busdma.h> 33254721Semaste#include <dev/usb2/core/usb2_process.h> 34254721Semaste#include <dev/usb2/core/usb2_config_td.h> 35254721Semaste#include <dev/usb2/core/usb2_sw_transfer.h> 36254721Semaste#include <dev/usb2/core/usb2_util.h> 37254721Semaste 38254721Semaste#include <dev/usb2/controller/usb2_controller.h> 39254721Semaste#include <dev/usb2/controller/usb2_bus.h> 40254721Semaste#include <dev/usb2/controller/musb2_otg.h> 41254721Semaste 42254721Semaste#include <sys/rman.h> 43254721Semaste 44254721Semastestatic device_probe_t musbotg_probe; 45254721Semastestatic device_attach_t musbotg_attach; 46254721Semastestatic device_detach_t musbotg_detach; 47254721Semastestatic device_shutdown_t musbotg_shutdown; 48254721Semaste 49254721Semastestruct musbotg_super_softc { 50254721Semaste struct musbotg_softc sc_otg; /* must be first */ 51254721Semaste}; 52254721Semaste 53254721Semastestatic void 54254721Semastemusbotg_vbus_interrupt(struct musbotg_super_softc *sc) 55254721Semaste{ 56254721Semaste uint8_t vbus_val = 1; /* fake VBUS on - TODO */ 57254721Semaste 58254721Semaste /* just forward it */ 59254721Semaste 60254721Semaste (sc->sc_otg.sc_bus.methods->vbus_interrupt) 61254721Semaste (&sc->sc_otg.sc_bus, vbus_val); 62254721Semaste} 63254721Semaste 64254721Semastestatic void 65254721Semastemusbotg_clocks_on(void *arg) 66254721Semaste{ 67254721Semaste#if 0 68254721Semaste struct musbotg_super_softc *sc = arg; 69254721Semaste 70254721Semaste#endif 71254721Semaste} 72254721Semaste 73254721Semastestatic void 74254721Semastemusbotg_clocks_off(void *arg) 75254721Semaste{ 76254721Semaste#if 0 77254721Semaste struct musbotg_super_softc *sc = arg; 78254721Semaste 79254721Semaste#endif 80254721Semaste} 81254721Semaste 82254721Semastestatic int 83254721Semastemusbotg_probe(device_t dev) 84254721Semaste{ 85254721Semaste device_set_desc(dev, "MUSB OTG integrated USB controller"); 86254721Semaste return (0); 87254721Semaste} 88254721Semaste 89254721Semastestatic int 90254721Semastemusbotg_attach(device_t dev) 91254721Semaste{ 92254721Semaste struct musbotg_super_softc *sc = device_get_softc(dev); 93254721Semaste int err; 94254721Semaste int rid; 95254721Semaste 96254721Semaste if (sc == NULL) { 97254721Semaste return (ENXIO); 98254721Semaste } 99254721Semaste /* setup MUSB OTG USB controller interface softc */ 100254721Semaste 101254721Semaste sc->sc_otg.sc_clocks_on = &musbotg_clocks_on; 102254721Semaste sc->sc_otg.sc_clocks_off = &musbotg_clocks_off; 103254721Semaste sc->sc_otg.sc_clocks_arg = sc; 104254721Semaste 105254721Semaste /* get all DMA memory */ 106254721Semaste 107254721Semaste sc->sc_otg.sc_bus.parent = dev; 108254721Semaste if (usb2_bus_mem_alloc_all(&sc->sc_otg.sc_bus, 109254721Semaste USB_GET_DMA_TAG(dev), NULL)) { 110254721Semaste return (ENOMEM); 111254721Semaste } 112254721Semaste rid = 0; 113254721Semaste sc->sc_otg.sc_io_res = 114254721Semaste bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid, RF_ACTIVE); 115254721Semaste 116254721Semaste if (!(sc->sc_otg.sc_io_res)) { 117254721Semaste err = ENOMEM; 118254721Semaste goto error; 119254721Semaste } 120254721Semaste sc->sc_otg.sc_io_tag = rman_get_bustag(sc->sc_otg.sc_io_res); 121254721Semaste sc->sc_otg.sc_io_hdl = rman_get_bushandle(sc->sc_otg.sc_io_res); 122254721Semaste sc->sc_otg.sc_io_size = rman_get_size(sc->sc_otg.sc_io_res); 123254721Semaste 124254721Semaste rid = 0; 125254721Semaste sc->sc_otg.sc_irq_res = 126254721Semaste bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid, RF_ACTIVE); 127254721Semaste if (!(sc->sc_otg.sc_irq_res)) { 128254721Semaste goto error; 129254721Semaste } 130254721Semaste sc->sc_otg.sc_bus.bdev = device_add_child(dev, "usbus", -1); 131 if (!(sc->sc_otg.sc_bus.bdev)) { 132 goto error; 133 } 134 device_set_ivars(sc->sc_otg.sc_bus.bdev, &sc->sc_otg.sc_bus); 135 136 err = usb2_config_td_setup(&sc->sc_otg.sc_config_td, sc, 137 &sc->sc_otg.sc_bus.bus_mtx, NULL, 0, 4); 138 if (err) { 139 device_printf(dev, "could not setup config thread!\n"); 140 goto error; 141 } 142#if (__FreeBSD_version >= 700031) 143 err = bus_setup_intr(dev, sc->sc_otg.sc_irq_res, INTR_TYPE_BIO | INTR_MPSAFE, 144 NULL, (void *)musbotg_interrupt, sc, &sc->sc_otg.sc_intr_hdl); 145#else 146 err = bus_setup_intr(dev, sc->sc_otg.sc_irq_res, INTR_TYPE_BIO | INTR_MPSAFE, 147 (void *)musbotg_interrupt, sc, &sc->sc_otg.sc_intr_hdl); 148#endif 149 if (err) { 150 sc->sc_otg.sc_intr_hdl = NULL; 151 goto error; 152 } 153 err = musbotg_init(&sc->sc_otg); 154 if (!err) { 155 err = device_probe_and_attach(sc->sc_otg.sc_bus.bdev); 156 } 157 if (err) { 158 goto error; 159 } else { 160 /* poll VBUS one time */ 161 musbotg_vbus_interrupt(sc); 162 } 163 return (0); 164 165error: 166 musbotg_detach(dev); 167 return (ENXIO); 168} 169 170static int 171musbotg_detach(device_t dev) 172{ 173 struct musbotg_super_softc *sc = device_get_softc(dev); 174 device_t bdev; 175 int err; 176 177 if (sc->sc_otg.sc_bus.bdev) { 178 bdev = sc->sc_otg.sc_bus.bdev; 179 device_detach(bdev); 180 device_delete_child(dev, bdev); 181 } 182 /* during module unload there are lots of children leftover */ 183 device_delete_all_children(dev); 184 185 if (sc->sc_otg.sc_irq_res && sc->sc_otg.sc_intr_hdl) { 186 /* 187 * only call musbotg_uninit() after musbotg_init() 188 */ 189 musbotg_uninit(&sc->sc_otg); 190 191 err = bus_teardown_intr(dev, sc->sc_otg.sc_irq_res, 192 sc->sc_otg.sc_intr_hdl); 193 sc->sc_otg.sc_intr_hdl = NULL; 194 } 195 /* free IRQ channel, if any */ 196 if (sc->sc_otg.sc_irq_res) { 197 bus_release_resource(dev, SYS_RES_IRQ, 0, 198 sc->sc_otg.sc_irq_res); 199 sc->sc_otg.sc_irq_res = NULL; 200 } 201 /* free memory resource, if any */ 202 if (sc->sc_otg.sc_io_res) { 203 bus_release_resource(dev, SYS_RES_MEMORY, 0, 204 sc->sc_otg.sc_io_res); 205 sc->sc_otg.sc_io_res = NULL; 206 } 207 usb2_config_td_unsetup(&sc->sc_otg.sc_config_td); 208 209 usb2_bus_mem_free_all(&sc->sc_otg.sc_bus, NULL); 210 211 return (0); 212} 213 214static int 215musbotg_shutdown(device_t dev) 216{ 217 struct musbotg_super_softc *sc = device_get_softc(dev); 218 int err; 219 220 err = bus_generic_shutdown(dev); 221 if (err) 222 return (err); 223 224 musbotg_uninit(&sc->sc_otg); 225 226 return (0); 227} 228 229static device_method_t musbotg_methods[] = { 230 /* Device interface */ 231 DEVMETHOD(device_probe, musbotg_probe), 232 DEVMETHOD(device_attach, musbotg_attach), 233 DEVMETHOD(device_detach, musbotg_detach), 234 DEVMETHOD(device_shutdown, musbotg_shutdown), 235 236 /* Bus interface */ 237 DEVMETHOD(bus_print_child, bus_generic_print_child), 238 239 {0, 0} 240}; 241 242static driver_t musbotg_driver = { 243 "musbotg", 244 musbotg_methods, 245 sizeof(struct musbotg_super_softc), 246}; 247 248static devclass_t musbotg_devclass; 249 250DRIVER_MODULE(musbotg, atmelarm, musbotg_driver, musbotg_devclass, 0, 0); 251MODULE_DEPEND(musbotg, usb2_controller, 1, 1, 1); 252MODULE_DEPEND(musbotg, usb2_core, 1, 1, 1); 253