xhci_pci.c revision 239617
1213379Shselasky/*- 2213379Shselasky * Copyright (c) 2010 Hans Petter Selasky. All rights reserved. 3213379Shselasky * 4213379Shselasky * Redistribution and use in source and binary forms, with or without 5213379Shselasky * modification, are permitted provided that the following conditions 6213379Shselasky * are met: 7213379Shselasky * 1. Redistributions of source code must retain the above copyright 8213379Shselasky * notice, this list of conditions and the following disclaimer. 9213379Shselasky * 2. Redistributions in binary form must reproduce the above copyright 10213379Shselasky * notice, this list of conditions and the following disclaimer in the 11213379Shselasky * documentation and/or other materials provided with the distribution. 12213379Shselasky * 13213379Shselasky * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 14213379Shselasky * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 15213379Shselasky * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 16213379Shselasky * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 17213379Shselasky * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 18213379Shselasky * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 19213379Shselasky * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 20213379Shselasky * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 21213379Shselasky * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 22213379Shselasky * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 23213379Shselasky * SUCH DAMAGE. 24213379Shselasky */ 25213379Shselasky 26213379Shselasky#include <sys/cdefs.h> 27213379Shselasky__FBSDID("$FreeBSD: head/sys/dev/usb/controller/xhci_pci.c 239617 2012-08-23 17:40:20Z hselasky $"); 28213379Shselasky 29213379Shselasky#include <sys/stdint.h> 30213379Shselasky#include <sys/stddef.h> 31213379Shselasky#include <sys/param.h> 32213379Shselasky#include <sys/queue.h> 33213379Shselasky#include <sys/types.h> 34213379Shselasky#include <sys/systm.h> 35213379Shselasky#include <sys/kernel.h> 36213379Shselasky#include <sys/bus.h> 37213379Shselasky#include <sys/module.h> 38213379Shselasky#include <sys/lock.h> 39213379Shselasky#include <sys/mutex.h> 40213379Shselasky#include <sys/condvar.h> 41213379Shselasky#include <sys/sysctl.h> 42213379Shselasky#include <sys/sx.h> 43213379Shselasky#include <sys/unistd.h> 44213379Shselasky#include <sys/callout.h> 45213379Shselasky#include <sys/malloc.h> 46213379Shselasky#include <sys/priv.h> 47213379Shselasky 48213379Shselasky#include <dev/usb/usb.h> 49213379Shselasky#include <dev/usb/usbdi.h> 50213379Shselasky 51213379Shselasky#include <dev/usb/usb_core.h> 52213379Shselasky#include <dev/usb/usb_busdma.h> 53213379Shselasky#include <dev/usb/usb_process.h> 54213379Shselasky#include <dev/usb/usb_util.h> 55213379Shselasky 56213379Shselasky#include <dev/usb/usb_controller.h> 57213379Shselasky#include <dev/usb/usb_bus.h> 58213379Shselasky#include <dev/usb/usb_pci.h> 59213379Shselasky#include <dev/usb/controller/xhci.h> 60213379Shselasky#include <dev/usb/controller/xhcireg.h> 61228483Shselasky#include "usb_if.h" 62213379Shselasky 63213379Shselaskystatic device_probe_t xhci_pci_probe; 64213379Shselaskystatic device_attach_t xhci_pci_attach; 65213379Shselaskystatic device_detach_t xhci_pci_detach; 66228483Shselaskystatic usb_take_controller_t xhci_pci_take_controller; 67213379Shselasky 68213379Shselaskystatic device_method_t xhci_device_methods[] = { 69213379Shselasky /* device interface */ 70213379Shselasky DEVMETHOD(device_probe, xhci_pci_probe), 71213379Shselasky DEVMETHOD(device_attach, xhci_pci_attach), 72213379Shselasky DEVMETHOD(device_detach, xhci_pci_detach), 73228483Shselasky DEVMETHOD(device_suspend, bus_generic_suspend), 74228483Shselasky DEVMETHOD(device_resume, bus_generic_resume), 75228483Shselasky DEVMETHOD(device_shutdown, bus_generic_shutdown), 76228483Shselasky DEVMETHOD(usb_take_controller, xhci_pci_take_controller), 77213379Shselasky 78227843Smarius DEVMETHOD_END 79213379Shselasky}; 80213379Shselasky 81213379Shselaskystatic driver_t xhci_driver = { 82213379Shselasky .name = "xhci", 83213379Shselasky .methods = xhci_device_methods, 84213379Shselasky .size = sizeof(struct xhci_softc), 85213379Shselasky}; 86213379Shselasky 87213379Shselaskystatic devclass_t xhci_devclass; 88213379Shselasky 89213379ShselaskyDRIVER_MODULE(xhci, pci, xhci_driver, xhci_devclass, 0, 0); 90213379ShselaskyMODULE_DEPEND(xhci, usb, 1, 1, 1); 91213379Shselasky 92213379Shselasky 93213379Shselaskystatic const char * 94213379Shselaskyxhci_pci_match(device_t self) 95213379Shselasky{ 96238015Smav uint32_t device_id = pci_get_devid(self); 97238015Smav 98238015Smav switch (device_id) { 99238015Smav case 0x01941033: 100238015Smav return ("NEC uPD720200 USB 3.0 controller"); 101238015Smav 102238015Smav case 0x1e318086: 103238015Smav return ("Intel Panther Point USB 3.0 controller"); 104238551Smav case 0x8c318086: 105238551Smav return ("Intel Lynx Point USB 3.0 controller"); 106238015Smav 107238015Smav default: 108238015Smav break; 109238015Smav } 110238015Smav 111213379Shselasky if ((pci_get_class(self) == PCIC_SERIALBUS) 112213379Shselasky && (pci_get_subclass(self) == PCIS_SERIALBUS_USB) 113222018Sru && (pci_get_progif(self) == PCIP_SERIALBUS_USB_XHCI)) { 114213379Shselasky return ("XHCI (generic) USB 3.0 controller"); 115213379Shselasky } 116213379Shselasky return (NULL); /* dunno */ 117213379Shselasky} 118213379Shselasky 119213379Shselaskystatic int 120213379Shselaskyxhci_pci_probe(device_t self) 121213379Shselasky{ 122213379Shselasky const char *desc = xhci_pci_match(self); 123213379Shselasky 124213379Shselasky if (desc) { 125213379Shselasky device_set_desc(self, desc); 126213379Shselasky return (0); 127213379Shselasky } else { 128213379Shselasky return (ENXIO); 129213379Shselasky } 130213379Shselasky} 131213379Shselasky 132213379Shselaskystatic int 133213379Shselaskyxhci_pci_attach(device_t self) 134213379Shselasky{ 135213379Shselasky struct xhci_softc *sc = device_get_softc(self); 136213379Shselasky int err; 137213379Shselasky int rid; 138213379Shselasky 139213379Shselasky /* XXX check for 64-bit capability */ 140213379Shselasky 141213379Shselasky if (xhci_init(sc, self)) { 142213379Shselasky device_printf(self, "Could not initialize softc\n"); 143213379Shselasky goto error; 144213379Shselasky } 145213379Shselasky 146213379Shselasky pci_enable_busmaster(self); 147213379Shselasky 148213379Shselasky rid = PCI_XHCI_CBMEM; 149213379Shselasky sc->sc_io_res = bus_alloc_resource_any(self, SYS_RES_MEMORY, &rid, 150213379Shselasky RF_ACTIVE); 151213379Shselasky if (!sc->sc_io_res) { 152213379Shselasky device_printf(self, "Could not map memory\n"); 153213379Shselasky goto error; 154213379Shselasky } 155213379Shselasky sc->sc_io_tag = rman_get_bustag(sc->sc_io_res); 156213379Shselasky sc->sc_io_hdl = rman_get_bushandle(sc->sc_io_res); 157213379Shselasky sc->sc_io_size = rman_get_size(sc->sc_io_res); 158213379Shselasky 159213379Shselasky rid = 0; 160213379Shselasky sc->sc_irq_res = bus_alloc_resource_any(self, SYS_RES_IRQ, &rid, 161213379Shselasky RF_SHAREABLE | RF_ACTIVE); 162213379Shselasky if (sc->sc_irq_res == NULL) { 163213379Shselasky device_printf(self, "Could not allocate IRQ\n"); 164213379Shselasky goto error; 165213379Shselasky } 166213379Shselasky sc->sc_bus.bdev = device_add_child(self, "usbus", -1); 167213379Shselasky if (sc->sc_bus.bdev == NULL) { 168213379Shselasky device_printf(self, "Could not add USB device\n"); 169213379Shselasky goto error; 170213379Shselasky } 171213379Shselasky device_set_ivars(sc->sc_bus.bdev, &sc->sc_bus); 172213379Shselasky 173213379Shselasky sprintf(sc->sc_vendor, "0x%04x", pci_get_vendor(self)); 174213379Shselasky 175213379Shselasky#if (__FreeBSD_version >= 700031) 176213379Shselasky err = bus_setup_intr(self, sc->sc_irq_res, INTR_TYPE_BIO | INTR_MPSAFE, 177213379Shselasky NULL, (driver_intr_t *)xhci_interrupt, sc, &sc->sc_intr_hdl); 178213379Shselasky#else 179213379Shselasky err = bus_setup_intr(self, sc->sc_irq_res, INTR_TYPE_BIO | INTR_MPSAFE, 180213379Shselasky (driver_intr_t *)xhci_interrupt, sc, &sc->sc_intr_hdl); 181213379Shselasky#endif 182213379Shselasky if (err) { 183213379Shselasky device_printf(self, "Could not setup IRQ, err=%d\n", err); 184213379Shselasky sc->sc_intr_hdl = NULL; 185213379Shselasky goto error; 186213379Shselasky } 187228483Shselasky xhci_pci_take_controller(self); 188213379Shselasky 189213379Shselasky err = xhci_halt_controller(sc); 190213379Shselasky 191213379Shselasky if (err == 0) 192213379Shselasky err = xhci_start_controller(sc); 193213379Shselasky 194213379Shselasky if (err == 0) 195213379Shselasky err = device_probe_and_attach(sc->sc_bus.bdev); 196213379Shselasky 197213379Shselasky if (err) { 198213379Shselasky device_printf(self, "XHCI halt/start/probe failed err=%d\n", err); 199213379Shselasky goto error; 200213379Shselasky } 201213379Shselasky return (0); 202213379Shselasky 203213379Shselaskyerror: 204213379Shselasky xhci_pci_detach(self); 205213379Shselasky return (ENXIO); 206213379Shselasky} 207213379Shselasky 208213379Shselaskystatic int 209213379Shselaskyxhci_pci_detach(device_t self) 210213379Shselasky{ 211213379Shselasky struct xhci_softc *sc = device_get_softc(self); 212213379Shselasky device_t bdev; 213213379Shselasky 214213379Shselasky if (sc->sc_bus.bdev != NULL) { 215213379Shselasky bdev = sc->sc_bus.bdev; 216213379Shselasky device_detach(bdev); 217213379Shselasky device_delete_child(self, bdev); 218213379Shselasky } 219213379Shselasky /* during module unload there are lots of children leftover */ 220227849Shselasky device_delete_children(self); 221213379Shselasky 222213379Shselasky pci_disable_busmaster(self); 223213379Shselasky 224213379Shselasky if (sc->sc_irq_res && sc->sc_intr_hdl) { 225213379Shselasky 226213379Shselasky xhci_halt_controller(sc); 227213379Shselasky 228213379Shselasky bus_teardown_intr(self, sc->sc_irq_res, sc->sc_intr_hdl); 229213379Shselasky sc->sc_intr_hdl = NULL; 230213379Shselasky } 231213379Shselasky if (sc->sc_irq_res) { 232213379Shselasky bus_release_resource(self, SYS_RES_IRQ, 0, sc->sc_irq_res); 233213379Shselasky sc->sc_irq_res = NULL; 234213379Shselasky } 235213379Shselasky if (sc->sc_io_res) { 236213379Shselasky bus_release_resource(self, SYS_RES_MEMORY, PCI_XHCI_CBMEM, 237213379Shselasky sc->sc_io_res); 238213379Shselasky sc->sc_io_res = NULL; 239213379Shselasky } 240213379Shselasky 241213379Shselasky xhci_uninit(sc); 242213379Shselasky 243213379Shselasky return (0); 244213379Shselasky} 245213379Shselasky 246228483Shselaskystatic int 247228483Shselaskyxhci_pci_take_controller(device_t self) 248213379Shselasky{ 249213379Shselasky struct xhci_softc *sc = device_get_softc(self); 250238551Smav uint32_t device_id = pci_get_devid(self); 251213379Shselasky uint32_t cparams; 252213379Shselasky uint32_t eecp; 253213379Shselasky uint32_t eec; 254213379Shselasky uint16_t to; 255213379Shselasky uint8_t bios_sem; 256213379Shselasky 257213379Shselasky cparams = XREAD4(sc, capa, XHCI_HCSPARAMS0); 258213379Shselasky 259213379Shselasky eec = -1; 260213379Shselasky 261213379Shselasky /* Synchronise with the BIOS if it owns the controller. */ 262213379Shselasky for (eecp = XHCI_HCS0_XECP(cparams) << 2; eecp != 0 && XHCI_XECP_NEXT(eec); 263213379Shselasky eecp += XHCI_XECP_NEXT(eec) << 2) { 264213379Shselasky eec = XREAD4(sc, capa, eecp); 265213379Shselasky 266213379Shselasky if (XHCI_XECP_ID(eec) != XHCI_ID_USB_LEGACY) 267213379Shselasky continue; 268213379Shselasky bios_sem = XREAD1(sc, capa, eecp + 269213379Shselasky XHCI_XECP_BIOS_SEM); 270213379Shselasky if (bios_sem == 0) 271213379Shselasky continue; 272213379Shselasky device_printf(sc->sc_bus.bdev, "waiting for BIOS " 273213379Shselasky "to give up control\n"); 274213379Shselasky XWRITE1(sc, capa, eecp + 275213379Shselasky XHCI_XECP_OS_SEM, 1); 276213379Shselasky to = 500; 277213379Shselasky while (1) { 278213379Shselasky bios_sem = XREAD1(sc, capa, eecp + 279213379Shselasky XHCI_XECP_BIOS_SEM); 280213379Shselasky if (bios_sem == 0) 281213379Shselasky break; 282213379Shselasky 283213379Shselasky if (--to == 0) { 284213379Shselasky device_printf(sc->sc_bus.bdev, 285213379Shselasky "timed out waiting for BIOS\n"); 286213379Shselasky break; 287213379Shselasky } 288213379Shselasky usb_pause_mtx(NULL, hz / 100); /* wait 10ms */ 289213379Shselasky } 290213379Shselasky } 291238551Smav 292238551Smav /* On Intel chipsets reroute ports from EHCI to XHCI controller. */ 293238551Smav if (device_id == 0x1e318086 /* Panther Point */ || 294238551Smav device_id == 0x8c318086 /* Lynx Point */) { 295239617Shselasky uint32_t temp = xhci_get_port_route(); 296239617Shselasky pci_write_config(self, PCI_XHCI_INTEL_USB3_PSSEN, temp, 4); 297239617Shselasky pci_write_config(self, PCI_XHCI_INTEL_XUSB2PR, temp, 4); 298238551Smav } 299228483Shselasky return (0); 300213379Shselasky} 301