1 /* $OpenBSD: uhci_pci.c,v 1.22 2006/08/25 04:17:00 pascoe Exp $ */ 2 /* $NetBSD: uhci_pci.c,v 1.24 2002/10/02 16:51:58 thorpej Exp $ */ 3 4 /* 5 * Copyright (c) 1998 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) at 10 * Carlstedt Research & Technology. 11 * 12 * Redistribution and use in source and binary forms, with or without 13 * modification, are permitted provided that the following conditions 14 * are met: 15 * 1. Redistributions of source code must retain the above copyright 16 * notice, this list of conditions and the following disclaimer. 17 * 2. Redistributions in binary form must reproduce the above copyright 18 * notice, this list of conditions and the following disclaimer in the 19 * documentation and/or other materials provided with the distribution. 20 * 3. All advertising materials mentioning features or use of this software 21 * must display the following acknowledgement: 22 * This product includes software developed by the NetBSD 23 * Foundation, Inc. and its contributors. 24 * 4. Neither the name of The NetBSD Foundation nor the names of its 25 * contributors may be used to endorse or promote products derived 26 * from this software without specific prior written permission. 27 * 28 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 29 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 30 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 31 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 32 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 33 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 34 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 35 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 36 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 37 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 38 * POSSIBILITY OF SUCH DAMAGE. 39 */ 40 41 #include <sys/param.h> 42 #include <sys/systm.h> 43 #include <sys/kernel.h> 44 #include <sys/device.h> 45 #include <sys/proc.h> 46 #include <sys/queue.h> 47 48 #include <machine/bus.h> 49 50 #include <dev/pci/pcivar.h> 51 52 #include <dev/usb/usb.h> 53 #include <dev/usb/usbdi.h> 54 #include <dev/usb/usbdivar.h> 55 #include <dev/usb/usb_mem.h> 56 57 #include <dev/usb/uhcireg.h> 58 #include <dev/usb/uhcivar.h> 59 60 int uhci_pci_match(struct device *, void *, void *); 61 void uhci_pci_attach(struct device *, struct device *, void *); 62 int uhci_pci_detach(struct device *, int); 63 64 struct uhci_pci_softc { 65 uhci_softc_t sc; 66 pci_chipset_tag_t sc_pc; 67 pcitag_t sc_tag; 68 void *sc_ih; /* interrupt vectoring */ 69 }; 70 71 struct cfattach uhci_pci_ca = { 72 sizeof(struct uhci_pci_softc), uhci_pci_match, uhci_pci_attach, 73 uhci_pci_detach, uhci_activate 74 }; 75 76 int 77 uhci_pci_match(struct device *parent, void *match, void *aux) 78 { 79 struct pci_attach_args *pa = (struct pci_attach_args *) aux; 80 81 if (PCI_CLASS(pa->pa_class) == PCI_CLASS_SERIALBUS && 82 PCI_SUBCLASS(pa->pa_class) == PCI_SUBCLASS_SERIALBUS_USB && 83 PCI_INTERFACE(pa->pa_class) == PCI_INTERFACE_UHCI) 84 return (1); 85 86 return (0); 87 } 88 89 void 90 uhci_pci_attach(struct device *parent, struct device *self, void *aux) 91 { 92 struct uhci_pci_softc *sc = (struct uhci_pci_softc *)self; 93 struct pci_attach_args *pa = (struct pci_attach_args *)aux; 94 pci_chipset_tag_t pc = pa->pa_pc; 95 pcitag_t tag = pa->pa_tag; 96 char const *intrstr; 97 pci_intr_handle_t ih; 98 const char *vendor; 99 char *devname = sc->sc.sc_bus.bdev.dv_xname; 100 usbd_status r; 101 int s; 102 103 #if defined(__NetBSD__) 104 char devinfo[256]; 105 106 pci_devinfo(pa->pa_id, pa->pa_class, 0, devinfo); 107 printf(": %s (rev. 0x%02x)", devinfo, PCI_REVISION(pa->pa_class)); 108 #endif 109 110 /* Map I/O registers */ 111 if (pci_mapreg_map(pa, PCI_CBIO, PCI_MAPREG_TYPE_IO, 0, 112 &sc->sc.iot, &sc->sc.ioh, NULL, &sc->sc.sc_size, 0)) { 113 printf(": can't map i/o space\n"); 114 return; 115 } 116 117 118 /* Disable interrupts, so we don't get any spurious ones. */ 119 s = splhardusb(); 120 bus_space_write_2(sc->sc.iot, sc->sc.ioh, UHCI_INTR, 0); 121 122 sc->sc_pc = pc; 123 sc->sc_tag = tag; 124 sc->sc.sc_bus.dmatag = pa->pa_dmat; 125 126 /* Map and establish the interrupt. */ 127 if (pci_intr_map(pa, &ih)) { 128 printf(": couldn't map interrupt\n"); 129 goto unmap_ret; 130 } 131 intrstr = pci_intr_string(pc, ih); 132 sc->sc_ih = pci_intr_establish(pc, ih, IPL_USB, uhci_intr, sc, 133 devname); 134 if (sc->sc_ih == NULL) { 135 printf(": couldn't establish interrupt"); 136 if (intrstr != NULL) 137 printf(" at %s", intrstr); 138 printf("\n"); 139 goto unmap_ret; 140 } 141 printf(": %s\n", intrstr); 142 143 /* Set LEGSUP register to its default value. */ 144 pci_conf_write(pc, tag, PCI_LEGSUP, PCI_LEGSUP_USBPIRQDEN); 145 146 switch(pci_conf_read(pc, tag, PCI_USBREV) & PCI_USBREV_MASK) { 147 case PCI_USBREV_PRE_1_0: 148 sc->sc.sc_bus.usbrev = USBREV_PRE_1_0; 149 break; 150 case PCI_USBREV_1_0: 151 sc->sc.sc_bus.usbrev = USBREV_1_0; 152 break; 153 case PCI_USBREV_1_1: 154 sc->sc.sc_bus.usbrev = USBREV_1_1; 155 break; 156 default: 157 sc->sc.sc_bus.usbrev = USBREV_UNKNOWN; 158 break; 159 } 160 161 uhci_run(&sc->sc, 0); /* stop the controller */ 162 /* disable interrupts */ 163 bus_space_barrier(sc->sc.iot, sc->sc.ioh, 0, sc->sc.sc_size, 164 BUS_SPACE_BARRIER_READ|BUS_SPACE_BARRIER_WRITE); 165 bus_space_write_2(sc->sc.iot, sc->sc.ioh, UHCI_INTR, 0); 166 167 /* Figure out vendor for root hub descriptor. */ 168 vendor = pci_findvendor(pa->pa_id); 169 sc->sc.sc_id_vendor = PCI_VENDOR(pa->pa_id); 170 if (vendor) 171 strlcpy(sc->sc.sc_vendor, vendor, sizeof (sc->sc.sc_vendor)); 172 else 173 snprintf(sc->sc.sc_vendor, sizeof (sc->sc.sc_vendor), 174 "vendor 0x%04x", PCI_VENDOR(pa->pa_id)); 175 176 r = uhci_init(&sc->sc); 177 if (r != USBD_NORMAL_COMPLETION) { 178 printf("%s: init failed, error=%d\n", devname, r); 179 goto unmap_ret; 180 } 181 splx(s); 182 183 /* Attach usb device. */ 184 sc->sc.sc_child = config_found((void *)sc, &sc->sc.sc_bus, 185 usbctlprint); 186 187 return; 188 189 unmap_ret: 190 bus_space_unmap(sc->sc.iot, sc->sc.ioh, sc->sc.sc_size); 191 splx(s); 192 } 193 194 int 195 uhci_pci_detach(struct device *self, int flags) 196 { 197 struct uhci_pci_softc *sc = (struct uhci_pci_softc *)self; 198 int rv; 199 200 rv = uhci_detach(&sc->sc, flags); 201 if (rv) 202 return (rv); 203 if (sc->sc_ih != NULL) { 204 pci_intr_disestablish(sc->sc_pc, sc->sc_ih); 205 sc->sc_ih = NULL; 206 } 207 if (sc->sc.sc_size) { 208 bus_space_unmap(sc->sc.iot, sc->sc.ioh, sc->sc.sc_size); 209 sc->sc.sc_size = 0; 210 } 211 212 return (0); 213 } 214