1 /* $OpenBSD: ehci_pci.c,v 1.15 2009/03/29 21:53:52 sthen Exp $ */ 2 /* $NetBSD: ehci_pci.c,v 1.15 2004/04/23 21:13:06 itojun Exp $ */ 3 4 /* 5 * Copyright (c) 2001, 2002 The NetBSD Foundation, Inc. 6 * All rights reserved. 7 * 8 * This code is derived from software contributed to The NetBSD Foundation 9 * by Lennart Augustsson (lennart@augustsson.net). 10 * 11 * Redistribution and use in source and binary forms, with or without 12 * modification, are permitted provided that the following conditions 13 * are met: 14 * 1. Redistributions of source code must retain the above copyright 15 * notice, this list of conditions and the following disclaimer. 16 * 2. Redistributions in binary form must reproduce the above copyright 17 * notice, this list of conditions and the following disclaimer in the 18 * documentation and/or other materials provided with the distribution. 19 * 20 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 21 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 22 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 23 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 24 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 30 * POSSIBILITY OF SUCH DAMAGE. 31 */ 32 33 #include <sys/param.h> 34 #include <sys/systm.h> 35 #include <sys/kernel.h> 36 #include <sys/rwlock.h> 37 #include <sys/device.h> 38 #include <sys/proc.h> 39 #include <sys/queue.h> 40 41 #include <machine/bus.h> 42 43 #include <dev/pci/pcidevs.h> 44 #include <dev/pci/pcivar.h> 45 46 #include <dev/usb/usb.h> 47 #include <dev/usb/usbdi.h> 48 #include <dev/usb/usbdivar.h> 49 #include <dev/usb/usb_mem.h> 50 51 #include <dev/usb/ehcireg.h> 52 #include <dev/usb/ehcivar.h> 53 54 #ifdef EHCI_DEBUG 55 #define DPRINTF(x) if (ehcidebug) printf x 56 extern int ehcidebug; 57 #else 58 #define DPRINTF(x) 59 #endif 60 61 struct ehci_pci_softc { 62 ehci_softc_t sc; 63 pci_chipset_tag_t sc_pc; 64 pcitag_t sc_tag; 65 void *sc_ih; /* interrupt vectoring */ 66 }; 67 68 int ehci_pci_match(struct device *, void *, void *); 69 void ehci_pci_attach(struct device *, struct device *, void *); 70 int ehci_pci_detach(struct device *, int); 71 void ehci_pci_givecontroller(struct ehci_pci_softc *); 72 void ehci_pci_takecontroller(struct ehci_pci_softc *); 73 void ehci_pci_shutdown(void *); 74 75 struct cfattach ehci_pci_ca = { 76 sizeof(struct ehci_pci_softc), ehci_pci_match, ehci_pci_attach, 77 ehci_pci_detach, ehci_activate 78 }; 79 80 81 int 82 ehci_pci_match(struct device *parent, void *match, void *aux) 83 { 84 struct pci_attach_args *pa = (struct pci_attach_args *) aux; 85 86 if (PCI_CLASS(pa->pa_class) == PCI_CLASS_SERIALBUS && 87 PCI_SUBCLASS(pa->pa_class) == PCI_SUBCLASS_SERIALBUS_USB && 88 PCI_INTERFACE(pa->pa_class) == PCI_INTERFACE_EHCI) 89 return (1); 90 91 return (0); 92 } 93 94 void 95 ehci_pci_attach(struct device *parent, struct device *self, void *aux) 96 { 97 struct ehci_pci_softc *sc = (struct ehci_pci_softc *)self; 98 struct pci_attach_args *pa = (struct pci_attach_args *)aux; 99 pci_chipset_tag_t pc = pa->pa_pc; 100 pcitag_t tag = pa->pa_tag; 101 char const *intrstr; 102 pci_intr_handle_t ih; 103 const char *vendor; 104 char *devname = sc->sc.sc_bus.bdev.dv_xname; 105 usbd_status r; 106 int s; 107 108 /* Map I/O registers */ 109 if (pci_mapreg_map(pa, PCI_CBMEM, PCI_MAPREG_TYPE_MEM, 0, 110 &sc->sc.iot, &sc->sc.ioh, NULL, &sc->sc.sc_size, 0)) { 111 printf(": can't map mem space\n"); 112 return; 113 } 114 115 sc->sc_pc = pc; 116 sc->sc_tag = tag; 117 sc->sc.sc_bus.dmatag = pa->pa_dmat; 118 119 /* Disable interrupts, so we don't get any spurious ones. */ 120 s = splhardusb(); 121 sc->sc.sc_offs = EREAD1(&sc->sc, EHCI_CAPLENGTH); 122 DPRINTF(("%s: offs=%d\n", devname, sc->sc.sc_offs)); 123 EOWRITE2(&sc->sc, EHCI_USBINTR, 0); 124 125 /* Map and establish the interrupt. */ 126 if (pci_intr_map(pa, &ih)) { 127 printf(": couldn't map interrupt\n"); 128 goto unmap_ret; 129 } 130 intrstr = pci_intr_string(pc, ih); 131 sc->sc_ih = pci_intr_establish(pc, ih, IPL_USB, ehci_intr, sc, devname); 132 if (sc->sc_ih == NULL) { 133 printf(": couldn't establish interrupt"); 134 if (intrstr != NULL) 135 printf(" at %s", intrstr); 136 printf("\n"); 137 goto unmap_ret; 138 } 139 printf(": %s\n", intrstr); 140 141 switch(pci_conf_read(pc, tag, PCI_USBREV) & PCI_USBREV_MASK) { 142 case PCI_USBREV_PRE_1_0: 143 case PCI_USBREV_1_0: 144 case PCI_USBREV_1_1: 145 sc->sc.sc_bus.usbrev = USBREV_UNKNOWN; 146 printf("%s: pre-2.0 USB rev\n", devname); 147 goto unmap_ret; 148 case PCI_USBREV_2_0: 149 sc->sc.sc_bus.usbrev = USBREV_2_0; 150 break; 151 default: 152 sc->sc.sc_bus.usbrev = USBREV_UNKNOWN; 153 break; 154 } 155 156 /* Figure out vendor for root hub descriptor. */ 157 vendor = pci_findvendor(pa->pa_id); 158 sc->sc.sc_id_vendor = PCI_VENDOR(pa->pa_id); 159 if (vendor) 160 strlcpy(sc->sc.sc_vendor, vendor, sizeof(sc->sc.sc_vendor)); 161 else 162 snprintf(sc->sc.sc_vendor, sizeof(sc->sc.sc_vendor), 163 "vendor 0x%04x", PCI_VENDOR(pa->pa_id)); 164 165 /* Enable workaround for dropped interrupts as required */ 166 if (sc->sc.sc_id_vendor == PCI_VENDOR_VIATECH) 167 sc->sc.sc_flags |= EHCIF_DROPPED_INTR_WORKAROUND; 168 169 ehci_pci_takecontroller(sc); 170 r = ehci_init(&sc->sc); 171 if (r != USBD_NORMAL_COMPLETION) { 172 printf("%s: init failed, error=%d\n", devname, r); 173 goto unmap_ret; 174 } 175 176 sc->sc.sc_shutdownhook = shutdownhook_establish(ehci_pci_shutdown, sc); 177 splx(s); 178 179 /* Attach usb device. */ 180 sc->sc.sc_child = config_found((void *)sc, &sc->sc.sc_bus, 181 usbctlprint); 182 183 return; 184 185 unmap_ret: 186 bus_space_unmap(sc->sc.iot, sc->sc.ioh, sc->sc.sc_size); 187 splx(s); 188 } 189 190 int 191 ehci_pci_detach(struct device *self, int flags) 192 { 193 struct ehci_pci_softc *sc = (struct ehci_pci_softc *)self; 194 int rv; 195 196 rv = ehci_detach(&sc->sc, flags); 197 if (rv) 198 return (rv); 199 if (sc->sc_ih != NULL) { 200 pci_intr_disestablish(sc->sc_pc, sc->sc_ih); 201 sc->sc_ih = NULL; 202 } 203 if (sc->sc.sc_size) { 204 bus_space_unmap(sc->sc.iot, sc->sc.ioh, sc->sc.sc_size); 205 sc->sc.sc_size = 0; 206 } 207 return (0); 208 } 209 210 #if 0 /* not used */ 211 void 212 ehci_pci_givecontroller(struct ehci_pci_softc *sc) 213 { 214 u_int32_t cparams, eec, legsup; 215 int eecp; 216 217 cparams = EREAD4(&sc->sc, EHCI_HCCPARAMS); 218 for (eecp = EHCI_HCC_EECP(cparams); eecp != 0; 219 eecp = EHCI_EECP_NEXT(eec)) { 220 eec = pci_conf_read(sc->sc_pc, sc->sc_tag, eecp); 221 if (EHCI_EECP_ID(eec) != EHCI_EC_LEGSUP) 222 continue; 223 legsup = eec; 224 pci_conf_write(sc->sc_pc, sc->sc_tag, eecp, 225 legsup & ~EHCI_LEGSUP_OSOWNED); 226 } 227 } 228 #endif 229 230 void 231 ehci_pci_takecontroller(struct ehci_pci_softc *sc) 232 { 233 u_int32_t cparams, eec, legsup; 234 int eecp, i; 235 236 cparams = EREAD4(&sc->sc, EHCI_HCCPARAMS); 237 /* Synchronise with the BIOS if it owns the controller. */ 238 for (eecp = EHCI_HCC_EECP(cparams); eecp != 0; 239 eecp = EHCI_EECP_NEXT(eec)) { 240 eec = pci_conf_read(sc->sc_pc, sc->sc_tag, eecp); 241 if (EHCI_EECP_ID(eec) != EHCI_EC_LEGSUP) 242 continue; 243 legsup = eec; 244 if (legsup & EHCI_LEGSUP_BIOSOWNED) { 245 pci_conf_write(sc->sc_pc, sc->sc_tag, eecp, 246 legsup | EHCI_LEGSUP_OSOWNED); 247 DPRINTF(("%s: waiting for BIOS to give up control\n", 248 sc->sc.sc_bus.bdev.dv_xname)); 249 for (i = 0; i < 5000; i++) { 250 legsup = pci_conf_read(sc->sc_pc, sc->sc_tag, 251 eecp); 252 if ((legsup & EHCI_LEGSUP_BIOSOWNED) == 0) 253 break; 254 DELAY(1000); 255 } 256 if (legsup & EHCI_LEGSUP_BIOSOWNED) 257 printf("%s: timed out waiting for BIOS\n", 258 sc->sc.sc_bus.bdev.dv_xname); 259 } 260 } 261 } 262 263 void 264 ehci_pci_shutdown(void *v) 265 { 266 struct ehci_pci_softc *sc = (struct ehci_pci_softc *)v; 267 268 ehci_shutdown(&sc->sc); 269 #if 0 270 /* best not to do this anymore; BIOS SMM spins? */ 271 ehci_pci_givecontroller(sc); 272 #endif 273 } 274