1 /* $OpenBSD: uhci_pci.c,v 1.28 2010/04/08 00:23:54 tedu 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 * 21 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 22 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 23 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 24 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 25 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 26 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 27 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 28 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 29 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 30 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 31 * POSSIBILITY OF SUCH DAMAGE. 32 */ 33 34 #include <sys/param.h> 35 #include <sys/systm.h> 36 #include <sys/kernel.h> 37 #include <sys/device.h> 38 #include <sys/timeout.h> 39 #include <sys/queue.h> 40 41 #include <machine/bus.h> 42 43 #include <dev/pci/pcivar.h> 44 45 #include <dev/usb/usb.h> 46 #include <dev/usb/usbdi.h> 47 #include <dev/usb/usbdivar.h> 48 #include <dev/usb/usb_mem.h> 49 50 #include <dev/usb/uhcireg.h> 51 #include <dev/usb/uhcivar.h> 52 53 int uhci_pci_match(struct device *, void *, void *); 54 void uhci_pci_attach(struct device *, struct device *, void *); 55 void uhci_pci_attach_deferred(struct device *); 56 int uhci_pci_detach(struct device *, int); 57 58 struct uhci_pci_softc { 59 uhci_softc_t sc; 60 pci_chipset_tag_t sc_pc; 61 pcitag_t sc_tag; 62 void *sc_ih; /* interrupt vectoring */ 63 }; 64 65 struct cfattach uhci_pci_ca = { 66 sizeof(struct uhci_pci_softc), uhci_pci_match, uhci_pci_attach, 67 uhci_pci_detach, uhci_activate 68 }; 69 70 int 71 uhci_pci_match(struct device *parent, void *match, void *aux) 72 { 73 struct pci_attach_args *pa = (struct pci_attach_args *) aux; 74 75 if (PCI_CLASS(pa->pa_class) == PCI_CLASS_SERIALBUS && 76 PCI_SUBCLASS(pa->pa_class) == PCI_SUBCLASS_SERIALBUS_USB && 77 PCI_INTERFACE(pa->pa_class) == PCI_INTERFACE_UHCI) 78 return (1); 79 80 return (0); 81 } 82 83 void 84 uhci_pci_attach(struct device *parent, struct device *self, void *aux) 85 { 86 struct uhci_pci_softc *sc = (struct uhci_pci_softc *)self; 87 struct pci_attach_args *pa = (struct pci_attach_args *)aux; 88 pci_chipset_tag_t pc = pa->pa_pc; 89 pcitag_t tag = pa->pa_tag; 90 char const *intrstr; 91 pci_intr_handle_t ih; 92 const char *vendor; 93 char *devname = sc->sc.sc_bus.bdev.dv_xname; 94 int s; 95 96 /* Map I/O registers */ 97 if (pci_mapreg_map(pa, PCI_CBIO, PCI_MAPREG_TYPE_IO, 0, 98 &sc->sc.iot, &sc->sc.ioh, NULL, &sc->sc.sc_size, 0)) { 99 printf(": can't map i/o space\n"); 100 return; 101 } 102 103 104 /* Disable interrupts, so we don't get any spurious ones. */ 105 s = splhardusb(); 106 bus_space_write_2(sc->sc.iot, sc->sc.ioh, UHCI_INTR, 0); 107 108 sc->sc_pc = pc; 109 sc->sc_tag = tag; 110 sc->sc.sc_bus.dmatag = pa->pa_dmat; 111 112 /* Map and establish the interrupt. */ 113 if (pci_intr_map(pa, &ih)) { 114 printf(": couldn't map interrupt\n"); 115 goto unmap_ret; 116 } 117 intrstr = pci_intr_string(pc, ih); 118 sc->sc_ih = pci_intr_establish(pc, ih, IPL_USB, uhci_intr, sc, 119 devname); 120 if (sc->sc_ih == NULL) { 121 printf(": couldn't establish interrupt"); 122 if (intrstr != NULL) 123 printf(" at %s", intrstr); 124 printf("\n"); 125 goto unmap_ret; 126 } 127 printf(": %s\n", intrstr); 128 129 /* Set LEGSUP register to its default value. */ 130 pci_conf_write(pc, tag, PCI_LEGSUP, PCI_LEGSUP_USBPIRQDEN); 131 132 switch(pci_conf_read(pc, tag, PCI_USBREV) & PCI_USBREV_MASK) { 133 case PCI_USBREV_PRE_1_0: 134 sc->sc.sc_bus.usbrev = USBREV_PRE_1_0; 135 break; 136 case PCI_USBREV_1_0: 137 sc->sc.sc_bus.usbrev = USBREV_1_0; 138 break; 139 case PCI_USBREV_1_1: 140 sc->sc.sc_bus.usbrev = USBREV_1_1; 141 break; 142 default: 143 sc->sc.sc_bus.usbrev = USBREV_UNKNOWN; 144 break; 145 } 146 147 uhci_run(&sc->sc, 0); /* stop the controller */ 148 /* disable interrupts */ 149 bus_space_barrier(sc->sc.iot, sc->sc.ioh, 0, sc->sc.sc_size, 150 BUS_SPACE_BARRIER_READ|BUS_SPACE_BARRIER_WRITE); 151 bus_space_write_2(sc->sc.iot, sc->sc.ioh, UHCI_INTR, 0); 152 153 /* Figure out vendor for root hub descriptor. */ 154 vendor = pci_findvendor(pa->pa_id); 155 sc->sc.sc_id_vendor = PCI_VENDOR(pa->pa_id); 156 if (vendor) 157 strlcpy(sc->sc.sc_vendor, vendor, sizeof (sc->sc.sc_vendor)); 158 else 159 snprintf(sc->sc.sc_vendor, sizeof (sc->sc.sc_vendor), 160 "vendor 0x%04x", PCI_VENDOR(pa->pa_id)); 161 162 config_defer(self, uhci_pci_attach_deferred); 163 164 /* Ignore interrupts for now */ 165 sc->sc.sc_dying = 1; 166 167 splx(s); 168 169 return; 170 171 unmap_ret: 172 bus_space_unmap(sc->sc.iot, sc->sc.ioh, sc->sc.sc_size); 173 splx(s); 174 } 175 176 void 177 uhci_pci_attach_deferred(struct device *self) 178 { 179 struct uhci_pci_softc *sc = (struct uhci_pci_softc *)self; 180 char *devname = sc->sc.sc_bus.bdev.dv_xname; 181 usbd_status r; 182 int s; 183 184 s = splhardusb(); 185 186 sc->sc.sc_dying = 0; 187 r = uhci_init(&sc->sc); 188 if (r != USBD_NORMAL_COMPLETION) { 189 printf("%s: init failed, error=%d\n", devname, r); 190 goto unmap_ret; 191 } 192 splx(s); 193 194 /* Attach usb device. */ 195 sc->sc.sc_child = config_found((void *)sc, &sc->sc.sc_bus, 196 usbctlprint); 197 198 return; 199 200 unmap_ret: 201 bus_space_unmap(sc->sc.iot, sc->sc.ioh, sc->sc.sc_size); 202 splx(s); 203 } 204 205 int 206 uhci_pci_detach(struct device *self, int flags) 207 { 208 struct uhci_pci_softc *sc = (struct uhci_pci_softc *)self; 209 int rv; 210 211 rv = uhci_detach(&sc->sc, flags); 212 if (rv) 213 return (rv); 214 if (sc->sc_ih != NULL) { 215 pci_intr_disestablish(sc->sc_pc, sc->sc_ih); 216 sc->sc_ih = NULL; 217 } 218 if (sc->sc.sc_size) { 219 bus_space_unmap(sc->sc.iot, sc->sc.ioh, sc->sc.sc_size); 220 sc->sc.sc_size = 0; 221 } 222 223 return (0); 224 } 225