1188410Sthompsa/*- 2188410Sthompsa * Copyright (c) 2008 Sam Leffler. All rights reserved. 3188410Sthompsa * 4188410Sthompsa * Redistribution and use in source and binary forms, with or without 5188410Sthompsa * modification, are permitted provided that the following conditions 6188410Sthompsa * are met: 7188410Sthompsa * 1. Redistributions of source code must retain the above copyright 8188410Sthompsa * notice, this list of conditions and the following disclaimer. 9188410Sthompsa * 2. Redistributions in binary form must reproduce the above copyright 10188410Sthompsa * notice, this list of conditions and the following disclaimer in the 11188410Sthompsa * documentation and/or other materials provided with the distribution. 12188410Sthompsa * 13188410Sthompsa * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 14188410Sthompsa * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 15188410Sthompsa * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 16188410Sthompsa * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 17188410Sthompsa * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 18188410Sthompsa * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 19188410Sthompsa * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 20188410Sthompsa * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 21188410Sthompsa * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 22188410Sthompsa * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 23188410Sthompsa */ 24188410Sthompsa 25188410Sthompsa/* 26188410Sthompsa * IXP435 attachment driver for the USB Enhanced Host Controller. 27188410Sthompsa */ 28188410Sthompsa 29188410Sthompsa#include <sys/cdefs.h> 30188410Sthompsa__FBSDID("$FreeBSD$"); 31188410Sthompsa 32188410Sthompsa#include "opt_bus.h" 33188410Sthompsa 34194677Sthompsa#include <sys/stdint.h> 35194677Sthompsa#include <sys/stddef.h> 36194677Sthompsa#include <sys/param.h> 37194677Sthompsa#include <sys/queue.h> 38194677Sthompsa#include <sys/types.h> 39194677Sthompsa#include <sys/systm.h> 40194677Sthompsa#include <sys/kernel.h> 41194677Sthompsa#include <sys/bus.h> 42194677Sthompsa#include <sys/module.h> 43194677Sthompsa#include <sys/lock.h> 44194677Sthompsa#include <sys/mutex.h> 45194677Sthompsa#include <sys/condvar.h> 46194677Sthompsa#include <sys/sysctl.h> 47194677Sthompsa#include <sys/sx.h> 48194677Sthompsa#include <sys/unistd.h> 49194677Sthompsa#include <sys/callout.h> 50194677Sthompsa#include <sys/malloc.h> 51194677Sthompsa#include <sys/priv.h> 52194677Sthompsa 53188942Sthompsa#include <dev/usb/usb.h> 54194677Sthompsa#include <dev/usb/usbdi.h> 55188410Sthompsa 56188942Sthompsa#include <dev/usb/usb_core.h> 57188942Sthompsa#include <dev/usb/usb_busdma.h> 58188942Sthompsa#include <dev/usb/usb_process.h> 59188942Sthompsa#include <dev/usb/usb_util.h> 60188410Sthompsa 61188942Sthompsa#include <dev/usb/usb_controller.h> 62188942Sthompsa#include <dev/usb/usb_bus.h> 63188942Sthompsa#include <dev/usb/controller/ehci.h> 64198151Sthompsa#include <dev/usb/controller/ehcireg.h> 65188410Sthompsa 66188410Sthompsa#include <arm/xscale/ixp425/ixp425reg.h> 67188410Sthompsa#include <arm/xscale/ixp425/ixp425var.h> 68188410Sthompsa 69188410Sthompsa#define EHCI_VENDORID_IXP4XX 0x42fa05 70188410Sthompsa#define EHCI_HC_DEVSTR "IXP4XX Integrated USB 2.0 controller" 71188410Sthompsa 72188410Sthompsastruct ixp_ehci_softc { 73188410Sthompsa ehci_softc_t base; /* storage for EHCI code */ 74188410Sthompsa bus_space_tag_t iot; 75188410Sthompsa bus_space_handle_t ioh; 76188410Sthompsa struct bus_space tag; /* tag for private bus space ops */ 77188410Sthompsa}; 78188410Sthompsa 79188410Sthompsastatic device_attach_t ehci_ixp_attach; 80188410Sthompsastatic device_detach_t ehci_ixp_detach; 81188410Sthompsa 82188410Sthompsastatic uint8_t ehci_bs_r_1(void *, bus_space_handle_t, bus_size_t); 83188410Sthompsastatic void ehci_bs_w_1(void *, bus_space_handle_t, bus_size_t, u_int8_t); 84188410Sthompsastatic uint16_t ehci_bs_r_2(void *, bus_space_handle_t, bus_size_t); 85188410Sthompsastatic void ehci_bs_w_2(void *, bus_space_handle_t, bus_size_t, uint16_t); 86188410Sthompsastatic uint32_t ehci_bs_r_4(void *, bus_space_handle_t, bus_size_t); 87188410Sthompsastatic void ehci_bs_w_4(void *, bus_space_handle_t, bus_size_t, uint32_t); 88188410Sthompsa 89188410Sthompsastatic int 90188410Sthompsaehci_ixp_probe(device_t self) 91188410Sthompsa{ 92188410Sthompsa 93188410Sthompsa device_set_desc(self, EHCI_HC_DEVSTR); 94188410Sthompsa 95188410Sthompsa return (BUS_PROBE_DEFAULT); 96188410Sthompsa} 97188410Sthompsa 98188410Sthompsastatic int 99188410Sthompsaehci_ixp_attach(device_t self) 100188410Sthompsa{ 101188410Sthompsa struct ixp_ehci_softc *isc = device_get_softc(self); 102188410Sthompsa ehci_softc_t *sc = &isc->base; 103188410Sthompsa int err; 104188410Sthompsa int rid; 105188410Sthompsa 106188410Sthompsa /* initialise some bus fields */ 107188410Sthompsa sc->sc_bus.parent = self; 108188410Sthompsa sc->sc_bus.devices = sc->sc_devices; 109188410Sthompsa sc->sc_bus.devices_max = EHCI_MAX_DEVICES; 110188410Sthompsa 111188410Sthompsa /* get all DMA memory */ 112194228Sthompsa if (usb_bus_mem_alloc_all(&sc->sc_bus, 113188410Sthompsa USB_GET_DMA_TAG(self), &ehci_iterate_hw_softc)) { 114188410Sthompsa return (ENOMEM); 115188410Sthompsa } 116188410Sthompsa 117188410Sthompsa /* NB: hints fix the memory location and irq */ 118188410Sthompsa 119188410Sthompsa rid = 0; 120188410Sthompsa sc->sc_io_res = bus_alloc_resource_any(self, SYS_RES_MEMORY, &rid, RF_ACTIVE); 121188410Sthompsa if (!sc->sc_io_res) { 122188410Sthompsa device_printf(self, "Could not map memory\n"); 123188410Sthompsa goto error; 124188410Sthompsa } 125188410Sthompsa 126188410Sthompsa /* 127188410Sthompsa * Craft special resource for bus space ops that handle 128188410Sthompsa * byte-alignment of non-word addresses. Also, since 129188410Sthompsa * we're already intercepting bus space ops we handle 130188410Sthompsa * the register window offset that could otherwise be 131188410Sthompsa * done with bus_space_subregion. 132188410Sthompsa */ 133188410Sthompsa isc->iot = rman_get_bustag(sc->sc_io_res); 134188410Sthompsa isc->tag.bs_cookie = isc->iot; 135188410Sthompsa /* read single */ 136188410Sthompsa isc->tag.bs_r_1 = ehci_bs_r_1, 137188410Sthompsa isc->tag.bs_r_2 = ehci_bs_r_2, 138188410Sthompsa isc->tag.bs_r_4 = ehci_bs_r_4, 139188410Sthompsa /* write (single) */ 140188410Sthompsa isc->tag.bs_w_1 = ehci_bs_w_1, 141188410Sthompsa isc->tag.bs_w_2 = ehci_bs_w_2, 142188410Sthompsa isc->tag.bs_w_4 = ehci_bs_w_4, 143188410Sthompsa 144188410Sthompsa sc->sc_io_tag = &isc->tag; 145188410Sthompsa sc->sc_io_hdl = rman_get_bushandle(sc->sc_io_res); 146188410Sthompsa sc->sc_io_size = IXP435_USB1_SIZE - 0x100; 147188410Sthompsa 148188410Sthompsa rid = 0; 149188410Sthompsa sc->sc_irq_res = bus_alloc_resource_any(self, SYS_RES_IRQ, &rid, 150188410Sthompsa RF_ACTIVE); 151188410Sthompsa if (sc->sc_irq_res == NULL) { 152188410Sthompsa device_printf(self, "Could not allocate irq\n"); 153188410Sthompsa goto error; 154188410Sthompsa } 155188410Sthompsa sc->sc_bus.bdev = device_add_child(self, "usbus", -1); 156188410Sthompsa if (!sc->sc_bus.bdev) { 157188410Sthompsa device_printf(self, "Could not add USB device\n"); 158188410Sthompsa goto error; 159188410Sthompsa } 160188410Sthompsa device_set_ivars(sc->sc_bus.bdev, &sc->sc_bus); 161188410Sthompsa device_set_desc(sc->sc_bus.bdev, EHCI_HC_DEVSTR); 162188410Sthompsa 163188410Sthompsa sprintf(sc->sc_vendor, "Intel"); 164188410Sthompsa 165188410Sthompsa 166188410Sthompsa err = bus_setup_intr(self, sc->sc_irq_res, INTR_TYPE_BIO | INTR_MPSAFE, 167190183Sthompsa NULL, (driver_intr_t *)ehci_interrupt, sc, &sc->sc_intr_hdl); 168188410Sthompsa if (err) { 169188410Sthompsa device_printf(self, "Could not setup irq, %d\n", err); 170188410Sthompsa sc->sc_intr_hdl = NULL; 171188410Sthompsa goto error; 172188410Sthompsa } 173188410Sthompsa 174188410Sthompsa /* 175188410Sthompsa * Arrange to force Host mode, select big-endian byte alignment, 176188410Sthompsa * and arrange to not terminate reset operations (the adapter 177188410Sthompsa * will ignore it if we do but might as well save a reg write). 178188410Sthompsa * Also, the controller has an embedded Transaction Translator 179188410Sthompsa * which means port speed must be read from the Port Status 180188410Sthompsa * register following a port enable. 181188410Sthompsa */ 182188410Sthompsa sc->sc_flags |= EHCI_SCFLG_TT 183188410Sthompsa | EHCI_SCFLG_SETMODE 184188410Sthompsa | EHCI_SCFLG_BIGEDESC 185188410Sthompsa | EHCI_SCFLG_BIGEMMIO 186188410Sthompsa | EHCI_SCFLG_NORESTERM 187188410Sthompsa ; 188188410Sthompsa 189188410Sthompsa err = ehci_init(sc); 190188410Sthompsa if (!err) { 191188410Sthompsa err = device_probe_and_attach(sc->sc_bus.bdev); 192188410Sthompsa } 193188410Sthompsa if (err) { 194188410Sthompsa device_printf(self, "USB init failed err=%d\n", err); 195188410Sthompsa goto error; 196188410Sthompsa } 197188410Sthompsa return (0); 198188410Sthompsa 199188410Sthompsaerror: 200188410Sthompsa ehci_ixp_detach(self); 201188410Sthompsa return (ENXIO); 202188410Sthompsa} 203188410Sthompsa 204188410Sthompsastatic int 205188410Sthompsaehci_ixp_detach(device_t self) 206188410Sthompsa{ 207188410Sthompsa struct ixp_ehci_softc *isc = device_get_softc(self); 208188410Sthompsa ehci_softc_t *sc = &isc->base; 209188410Sthompsa device_t bdev; 210188410Sthompsa int err; 211188410Sthompsa 212188410Sthompsa if (sc->sc_bus.bdev) { 213188410Sthompsa bdev = sc->sc_bus.bdev; 214188410Sthompsa device_detach(bdev); 215188410Sthompsa device_delete_child(self, bdev); 216188410Sthompsa } 217188410Sthompsa /* during module unload there are lots of children leftover */ 218227849Shselasky device_delete_children(self); 219188410Sthompsa 220188410Sthompsa if (sc->sc_irq_res && sc->sc_intr_hdl) { 221188410Sthompsa /* 222188410Sthompsa * only call ehci_detach() after ehci_init() 223188410Sthompsa */ 224188410Sthompsa ehci_detach(sc); 225188410Sthompsa 226188410Sthompsa err = bus_teardown_intr(self, sc->sc_irq_res, sc->sc_intr_hdl); 227188410Sthompsa 228188410Sthompsa if (err) 229188410Sthompsa /* XXX or should we panic? */ 230188410Sthompsa device_printf(self, "Could not tear down irq, %d\n", 231188410Sthompsa err); 232188410Sthompsa sc->sc_intr_hdl = NULL; 233188410Sthompsa } 234188410Sthompsa 235188410Sthompsa if (sc->sc_irq_res) { 236188410Sthompsa bus_release_resource(self, SYS_RES_IRQ, 0, sc->sc_irq_res); 237188410Sthompsa sc->sc_irq_res = NULL; 238188410Sthompsa } 239188410Sthompsa if (sc->sc_io_res) { 240188410Sthompsa bus_release_resource(self, SYS_RES_MEMORY, 0, 241188410Sthompsa sc->sc_io_res); 242188410Sthompsa sc->sc_io_res = NULL; 243188410Sthompsa } 244194228Sthompsa usb_bus_mem_free_all(&sc->sc_bus, &ehci_iterate_hw_softc); 245188410Sthompsa 246188410Sthompsa return (0); 247188410Sthompsa} 248188410Sthompsa 249188410Sthompsa/* 250188410Sthompsa * Bus space accessors for PIO operations. 251188410Sthompsa */ 252188410Sthompsa 253188410Sthompsastatic uint8_t 254188410Sthompsaehci_bs_r_1(void *t, bus_space_handle_t h, bus_size_t o) 255188410Sthompsa{ 256188410Sthompsa return bus_space_read_1((bus_space_tag_t) t, h, 257188410Sthompsa 0x100 + (o &~ 3) + (3 - (o & 3))); 258188410Sthompsa} 259188410Sthompsa 260188410Sthompsastatic void 261188410Sthompsaehci_bs_w_1(void *t, bus_space_handle_t h, bus_size_t o, u_int8_t v) 262188410Sthompsa{ 263188410Sthompsa panic("%s", __func__); 264188410Sthompsa} 265188410Sthompsa 266188410Sthompsastatic uint16_t 267188410Sthompsaehci_bs_r_2(void *t, bus_space_handle_t h, bus_size_t o) 268188410Sthompsa{ 269188410Sthompsa return bus_space_read_2((bus_space_tag_t) t, h, 270188410Sthompsa 0x100 + (o &~ 3) + (2 - (o & 3))); 271188410Sthompsa} 272188410Sthompsa 273188410Sthompsastatic void 274188410Sthompsaehci_bs_w_2(void *t, bus_space_handle_t h, bus_size_t o, uint16_t v) 275188410Sthompsa{ 276188410Sthompsa panic("%s", __func__); 277188410Sthompsa} 278188410Sthompsa 279188410Sthompsastatic uint32_t 280188410Sthompsaehci_bs_r_4(void *t, bus_space_handle_t h, bus_size_t o) 281188410Sthompsa{ 282188410Sthompsa return bus_space_read_4((bus_space_tag_t) t, h, 0x100 + o); 283188410Sthompsa} 284188410Sthompsa 285188410Sthompsastatic void 286188410Sthompsaehci_bs_w_4(void *t, bus_space_handle_t h, bus_size_t o, uint32_t v) 287188410Sthompsa{ 288188410Sthompsa bus_space_write_4((bus_space_tag_t) t, h, 0x100 + o, v); 289188410Sthompsa} 290188410Sthompsa 291188410Sthompsastatic device_method_t ehci_methods[] = { 292188410Sthompsa /* Device interface */ 293188410Sthompsa DEVMETHOD(device_probe, ehci_ixp_probe), 294188410Sthompsa DEVMETHOD(device_attach, ehci_ixp_attach), 295188410Sthompsa DEVMETHOD(device_detach, ehci_ixp_detach), 296228483Shselasky DEVMETHOD(device_suspend, bus_generic_suspend), 297228483Shselasky DEVMETHOD(device_resume, bus_generic_resume), 298228483Shselasky DEVMETHOD(device_shutdown, bus_generic_shutdown), 299188410Sthompsa 300227843Smarius DEVMETHOD_END 301188410Sthompsa}; 302188410Sthompsa 303188410Sthompsastatic driver_t ehci_driver = { 304188410Sthompsa "ehci", 305188410Sthompsa ehci_methods, 306188410Sthompsa sizeof(struct ixp_ehci_softc), 307188410Sthompsa}; 308188410Sthompsa 309188410Sthompsastatic devclass_t ehci_devclass; 310188410Sthompsa 311188410SthompsaDRIVER_MODULE(ehci, ixp, ehci_driver, ehci_devclass, 0, 0); 312188942SthompsaMODULE_DEPEND(ehci, usb, 1, 1, 1); 313