at91dci_atmelarm.c revision 184824
1#include <sys/cdefs.h> 2__FBSDID("$FreeBSD: head/sys/dev/usb2/controller/at91dci_atmelarm.c 184824 2008-11-10 20:54:31Z thompsa $"); 3 4/*- 5 * Copyright (c) 2007-2008 Hans Petter Selasky. All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 26 * SUCH DAMAGE. 27 */ 28 29 30#include <dev/usb2/include/usb2_mfunc.h> 31#include <dev/usb2/include/usb2_defs.h> 32#include <dev/usb2/include/usb2_standard.h> 33 34#include <dev/usb2/core/usb2_core.h> 35#include <dev/usb2/core/usb2_busdma.h> 36#include <dev/usb2/core/usb2_process.h> 37#include <dev/usb2/core/usb2_config_td.h> 38#include <dev/usb2/core/usb2_sw_transfer.h> 39#include <dev/usb2/core/usb2_util.h> 40 41#include <dev/usb2/controller/usb2_controller.h> 42#include <dev/usb2/controller/usb2_bus.h> 43#include <dev/usb2/controller/at91dci.h> 44 45#include <sys/rman.h> 46 47#include <arm/at91/at91_pmcvar.h> 48#include <arm/at91/at91rm92reg.h> 49#include <arm/at91/at91_pio_rm9200.h> 50#include <arm/at91/at91_piovar.h> 51 52#define MEM_RID 0 53 54/* Pin Definitions - do they belong here or somewhere else ? */ 55 56#define VBUS_MASK AT91C_PIO_PB24 57#define VBUS_BASE AT91RM92_PIOB_BASE 58 59#define PULLUP_MASK AT91C_PIO_PB22 60#define PULLUP_BASE AT91RM92_PIOB_BASE 61 62static device_probe_t at91_udp_probe; 63static device_attach_t at91_udp_attach; 64static device_detach_t at91_udp_detach; 65static device_shutdown_t at91_udp_shutdown; 66 67struct at91_udp_softc { 68 struct at91dci_softc sc_dci; /* must be first */ 69 struct at91_pmc_clock *sc_iclk; 70 struct at91_pmc_clock *sc_fclk; 71 struct resource *sc_vbus_irq_res; 72 void *sc_vbus_intr_hdl; 73}; 74 75static void 76at91_vbus_interrupt(struct at91_udp_softc *sc) 77{ 78 uint32_t temp; 79 uint8_t vbus_val; 80 81 /* XXX temporary clear interrupts here */ 82 83 temp = at91_pio_gpio_clear_interrupt(VBUS_BASE); 84 85 /* just forward it */ 86 87 vbus_val = at91_pio_gpio_get(VBUS_BASE, VBUS_MASK); 88 (sc->sc_dci.sc_bus.methods->vbus_interrupt) 89 (&sc->sc_dci.sc_bus, vbus_val); 90 return; 91} 92 93static void 94at91_udp_clocks_on(void *arg) 95{ 96 struct at91_udp_softc *sc = arg; 97 98 at91_pmc_clock_enable(sc->sc_iclk); 99 at91_pmc_clock_enable(sc->sc_fclk); 100 return; 101} 102 103static void 104at91_udp_clocks_off(void *arg) 105{ 106 struct at91_udp_softc *sc = arg; 107 108 at91_pmc_clock_disable(sc->sc_fclk); 109 at91_pmc_clock_disable(sc->sc_iclk); 110 return; 111} 112 113static void 114at91_udp_pull_up(void *arg) 115{ 116 at91_pio_gpio_set(PULLUP_BASE, PULLUP_MASK); 117 return; 118} 119 120static void 121at91_udp_pull_down(void *arg) 122{ 123 at91_pio_gpio_clear(PULLUP_BASE, PULLUP_MASK); 124 return; 125} 126 127static int 128at91_udp_probe(device_t dev) 129{ 130 device_set_desc(dev, "AT91 integrated AT91_UDP controller"); 131 return (0); 132} 133 134static int 135at91_udp_attach(device_t dev) 136{ 137 struct at91_udp_softc *sc = device_get_softc(dev); 138 int err; 139 int rid; 140 141 if (sc == NULL) { 142 return (ENXIO); 143 } 144 /* setup AT9100 USB device controller interface softc */ 145 146 sc->sc_dci.sc_clocks_on = &at91_udp_clocks_on; 147 sc->sc_dci.sc_clocks_off = &at91_udp_clocks_off; 148 sc->sc_dci.sc_clocks_arg = sc; 149 sc->sc_dci.sc_pull_up = &at91_udp_pull_up; 150 sc->sc_dci.sc_pull_down = &at91_udp_pull_down; 151 sc->sc_dci.sc_pull_arg = sc; 152 153 /* get all DMA memory */ 154 155 if (usb2_bus_mem_alloc_all(&sc->sc_dci.sc_bus, 156 USB_GET_DMA_TAG(dev), NULL)) { 157 return (ENOMEM); 158 } 159 /* 160 * configure VBUS input pin, enable deglitch and enable 161 * interrupt : 162 */ 163 at91_pio_use_gpio(VBUS_BASE, VBUS_MASK); 164 at91_pio_gpio_input(VBUS_BASE, VBUS_MASK); 165 at91_pio_gpio_set_deglitch(VBUS_BASE, VBUS_MASK, 1); 166 at91_pio_gpio_set_interrupt(VBUS_BASE, VBUS_MASK, 1); 167 168 /* 169 * configure PULLUP output pin : 170 */ 171 at91_pio_use_gpio(PULLUP_BASE, PULLUP_MASK); 172 at91_pio_gpio_output(PULLUP_BASE, PULLUP_MASK, 0); 173 174 at91_udp_pull_down(sc); 175 176 /* wait 10ms for pulldown to stabilise */ 177 usb2_pause_mtx(NULL, 10); 178 179 sc->sc_iclk = at91_pmc_clock_ref("udc_clk"); 180 sc->sc_fclk = at91_pmc_clock_ref("udpck"); 181 182 rid = MEM_RID; 183 sc->sc_dci.sc_io_res = 184 bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid, RF_ACTIVE); 185 186 if (!(sc->sc_dci.sc_io_res)) { 187 err = ENOMEM; 188 goto error; 189 } 190 sc->sc_dci.sc_io_tag = rman_get_bustag(sc->sc_dci.sc_io_res); 191 sc->sc_dci.sc_io_hdl = rman_get_bushandle(sc->sc_dci.sc_io_res); 192 sc->sc_dci.sc_io_size = rman_get_size(sc->sc_dci.sc_io_res); 193 194 rid = 0; 195 sc->sc_dci.sc_irq_res = 196 bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid, RF_ACTIVE); 197 if (!(sc->sc_dci.sc_irq_res)) { 198 goto error; 199 } 200 rid = 1; 201 sc->sc_vbus_irq_res = 202 bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid, RF_ACTIVE); 203 if (!(sc->sc_vbus_irq_res)) { 204 goto error; 205 } 206 sc->sc_dci.sc_bus.bdev = device_add_child(dev, "usbus", -1); 207 if (!(sc->sc_dci.sc_bus.bdev)) { 208 goto error; 209 } 210 device_set_ivars(sc->sc_dci.sc_bus.bdev, &sc->sc_dci.sc_bus); 211 212 err = usb2_config_td_setup(&sc->sc_dci.sc_config_td, sc, 213 &sc->sc_dci.sc_bus.bus_mtx, NULL, 0, 4); 214 if (err) { 215 device_printf(dev, "could not setup config thread!\n"); 216 goto error; 217 } 218#if (__FreeBSD_version >= 700031) 219 err = bus_setup_intr(dev, sc->sc_dci.sc_irq_res, INTR_TYPE_BIO | INTR_MPSAFE, 220 NULL, (void *)at91dci_interrupt, sc, &sc->sc_dci.sc_intr_hdl); 221#else 222 err = bus_setup_intr(dev, sc->sc_dci.sc_irq_res, INTR_TYPE_BIO | INTR_MPSAFE, 223 (void *)at91dci_interrupt, sc, &sc->sc_dci.sc_intr_hdl); 224#endif 225 if (err) { 226 sc->sc_dci.sc_intr_hdl = NULL; 227 goto error; 228 } 229#if (__FreeBSD_version >= 700031) 230 err = bus_setup_intr(dev, sc->sc_vbus_irq_res, INTR_TYPE_BIO | INTR_MPSAFE, 231 NULL, (void *)at91_vbus_interrupt, sc, &sc->sc_vbus_intr_hdl); 232#else 233 err = bus_setup_intr(dev, sc->sc_vbus_irq_res, INTR_TYPE_BIO | INTR_MPSAFE, 234 (void *)at91_vbus_interrupt, sc, &sc->sc_vbus_intr_hdl); 235#endif 236 if (err) { 237 sc->sc_vbus_intr_hdl = NULL; 238 goto error; 239 } 240 err = at91dci_init(&sc->sc_dci); 241 if (!err) { 242 err = device_probe_and_attach(sc->sc_dci.sc_bus.bdev); 243 } 244 if (err) { 245 goto error; 246 } else { 247 /* poll VBUS one time */ 248 at91_vbus_interrupt(sc); 249 } 250 return (0); 251 252error: 253 at91_udp_detach(dev); 254 return (ENXIO); 255} 256 257static int 258at91_udp_detach(device_t dev) 259{ 260 struct at91_udp_softc *sc = device_get_softc(dev); 261 device_t bdev; 262 int err; 263 264 if (sc->sc_dci.sc_bus.bdev) { 265 bdev = sc->sc_dci.sc_bus.bdev; 266 device_detach(bdev); 267 device_delete_child(dev, bdev); 268 } 269 /* during module unload there are lots of children leftover */ 270 device_delete_all_children(dev); 271 272 /* disable Transceiver */ 273 AT91_UDP_WRITE_4(&sc->sc_dci, AT91_UDP_TXVC, AT91_UDP_TXVC_DIS); 274 275 /* disable and clear all interrupts */ 276 AT91_UDP_WRITE_4(&sc->sc_dci, AT91_UDP_IDR, 0xFFFFFFFF); 277 AT91_UDP_WRITE_4(&sc->sc_dci, AT91_UDP_ICR, 0xFFFFFFFF); 278 279 /* disable VBUS interrupt */ 280 at91_pio_gpio_set_interrupt(VBUS_BASE, VBUS_MASK, 0); 281 282 if (sc->sc_vbus_irq_res && sc->sc_vbus_intr_hdl) { 283 err = bus_teardown_intr(dev, sc->sc_vbus_irq_res, 284 sc->sc_vbus_intr_hdl); 285 sc->sc_vbus_intr_hdl = NULL; 286 } 287 if (sc->sc_vbus_irq_res) { 288 bus_release_resource(dev, SYS_RES_IRQ, 1, 289 sc->sc_vbus_irq_res); 290 sc->sc_vbus_irq_res = NULL; 291 } 292 if (sc->sc_dci.sc_irq_res && sc->sc_dci.sc_intr_hdl) { 293 /* 294 * only call at91_udp_uninit() after at91_udp_init() 295 */ 296 at91dci_uninit(&sc->sc_dci); 297 298 err = bus_teardown_intr(dev, sc->sc_dci.sc_irq_res, 299 sc->sc_dci.sc_intr_hdl); 300 sc->sc_dci.sc_intr_hdl = NULL; 301 } 302 if (sc->sc_dci.sc_irq_res) { 303 bus_release_resource(dev, SYS_RES_IRQ, 0, 304 sc->sc_dci.sc_irq_res); 305 sc->sc_dci.sc_irq_res = NULL; 306 } 307 if (sc->sc_dci.sc_io_res) { 308 bus_release_resource(dev, SYS_RES_MEMORY, MEM_RID, 309 sc->sc_dci.sc_io_res); 310 sc->sc_dci.sc_io_res = NULL; 311 } 312 usb2_config_td_unsetup(&sc->sc_dci.sc_config_td); 313 314 usb2_bus_mem_free_all(&sc->sc_dci.sc_bus, NULL); 315 316 /* disable clocks */ 317 at91_pmc_clock_disable(sc->sc_iclk); 318 at91_pmc_clock_disable(sc->sc_fclk); 319 at91_pmc_clock_deref(sc->sc_fclk); 320 at91_pmc_clock_deref(sc->sc_iclk); 321 322 return (0); 323} 324 325static int 326at91_udp_shutdown(device_t dev) 327{ 328 struct at91_udp_softc *sc = device_get_softc(dev); 329 int err; 330 331 err = bus_generic_shutdown(dev); 332 if (err) 333 return (err); 334 335 at91dci_uninit(&sc->sc_dci); 336 337 return (0); 338} 339 340static device_method_t at91_udp_methods[] = { 341 /* Device interface */ 342 DEVMETHOD(device_probe, at91_udp_probe), 343 DEVMETHOD(device_attach, at91_udp_attach), 344 DEVMETHOD(device_detach, at91_udp_detach), 345 DEVMETHOD(device_shutdown, at91_udp_shutdown), 346 347 /* Bus interface */ 348 DEVMETHOD(bus_print_child, bus_generic_print_child), 349 350 {0, 0} 351}; 352 353static driver_t at91_udp_driver = { 354 "at91_udp", 355 at91_udp_methods, 356 sizeof(struct at91_udp_softc), 357}; 358 359static devclass_t at91_udp_devclass; 360 361DRIVER_MODULE(at91_udp, atmelarm, at91_udp_driver, at91_udp_devclass, 0, 0); 362