1 /* $OpenBSD: uhci_pci.c,v 1.34 2019/09/05 17:59:12 bluhm 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 int uhci_pci_activate(struct device *, int); 58 59 struct uhci_pci_softc { 60 struct uhci_softc sc; 61 pci_chipset_tag_t sc_pc; 62 pcitag_t sc_tag; 63 void *sc_ih; /* interrupt vectoring */ 64 }; 65 66 struct cfattach uhci_pci_ca = { 67 sizeof(struct uhci_pci_softc), uhci_pci_match, uhci_pci_attach, 68 uhci_pci_detach, uhci_pci_activate 69 }; 70 71 int 72 uhci_pci_match(struct device *parent, void *match, void *aux) 73 { 74 struct pci_attach_args *pa = (struct pci_attach_args *) aux; 75 76 if (PCI_CLASS(pa->pa_class) == PCI_CLASS_SERIALBUS && 77 PCI_SUBCLASS(pa->pa_class) == PCI_SUBCLASS_SERIALBUS_USB && 78 PCI_INTERFACE(pa->pa_class) == PCI_INTERFACE_UHCI) 79 return (1); 80 81 return (0); 82 } 83 84 int 85 uhci_pci_activate(struct device *self, int act) 86 { 87 struct uhci_pci_softc *sc = (struct uhci_pci_softc *)self; 88 89 if (sc->sc.sc_size == 0) 90 return 0; 91 92 /* On resume, set legacy support attribute and enable intrs */ 93 switch (act) { 94 case DVACT_RESUME: 95 pci_conf_write(sc->sc_pc, sc->sc_tag, 96 PCI_LEGSUP, PCI_LEGSUP_USBPIRQDEN); 97 bus_space_barrier(sc->sc.iot, sc->sc.ioh, 0, sc->sc.sc_size, 98 BUS_SPACE_BARRIER_READ|BUS_SPACE_BARRIER_WRITE); 99 bus_space_write_2(sc->sc.iot, sc->sc.ioh, UHCI_INTR, 0); 100 break; 101 } 102 103 return uhci_activate(self, act); 104 } 105 106 void 107 uhci_pci_attach(struct device *parent, struct device *self, void *aux) 108 { 109 struct uhci_pci_softc *sc = (struct uhci_pci_softc *)self; 110 struct pci_attach_args *pa = (struct pci_attach_args *)aux; 111 pci_chipset_tag_t pc = pa->pa_pc; 112 pcitag_t tag = pa->pa_tag; 113 char const *intrstr; 114 pci_intr_handle_t ih; 115 const char *vendor; 116 char *devname = sc->sc.sc_bus.bdev.dv_xname; 117 int s; 118 119 /* Map I/O registers */ 120 if (pci_mapreg_map(pa, PCI_CBIO, PCI_MAPREG_TYPE_IO, 0, 121 &sc->sc.iot, &sc->sc.ioh, NULL, &sc->sc.sc_size, 0)) { 122 printf(": can't map i/o space\n"); 123 return; 124 } 125 126 127 /* Disable interrupts, so we don't get any spurious ones. */ 128 s = splhardusb(); 129 bus_space_write_2(sc->sc.iot, sc->sc.ioh, UHCI_INTR, 0); 130 131 sc->sc_pc = pc; 132 sc->sc_tag = tag; 133 sc->sc.sc_bus.dmatag = pa->pa_dmat; 134 135 /* Map and establish the interrupt. */ 136 if (pci_intr_map(pa, &ih)) { 137 printf(": couldn't map interrupt\n"); 138 goto unmap_ret; 139 } 140 intrstr = pci_intr_string(pc, ih); 141 sc->sc_ih = pci_intr_establish(pc, ih, IPL_USB, uhci_intr, sc, 142 devname); 143 if (sc->sc_ih == NULL) { 144 printf(": couldn't establish interrupt"); 145 if (intrstr != NULL) 146 printf(" at %s", intrstr); 147 printf("\n"); 148 goto unmap_ret; 149 } 150 printf(": %s\n", intrstr); 151 152 /* Set LEGSUP register to its default value. */ 153 pci_conf_write(pc, tag, PCI_LEGSUP, PCI_LEGSUP_USBPIRQDEN); 154 155 switch(pci_conf_read(pc, tag, PCI_USBREV) & PCI_USBREV_MASK) { 156 case PCI_USBREV_PRE_1_0: 157 sc->sc.sc_bus.usbrev = USBREV_PRE_1_0; 158 break; 159 case PCI_USBREV_1_0: 160 sc->sc.sc_bus.usbrev = USBREV_1_0; 161 break; 162 case PCI_USBREV_1_1: 163 sc->sc.sc_bus.usbrev = USBREV_1_1; 164 break; 165 default: 166 sc->sc.sc_bus.usbrev = USBREV_UNKNOWN; 167 break; 168 } 169 170 uhci_run(&sc->sc, 0); /* stop the controller */ 171 /* disable interrupts */ 172 bus_space_barrier(sc->sc.iot, sc->sc.ioh, 0, sc->sc.sc_size, 173 BUS_SPACE_BARRIER_READ|BUS_SPACE_BARRIER_WRITE); 174 bus_space_write_2(sc->sc.iot, sc->sc.ioh, UHCI_INTR, 0); 175 176 /* Figure out vendor for root hub descriptor. */ 177 vendor = pci_findvendor(pa->pa_id); 178 sc->sc.sc_id_vendor = PCI_VENDOR(pa->pa_id); 179 if (vendor) 180 strlcpy(sc->sc.sc_vendor, vendor, sizeof (sc->sc.sc_vendor)); 181 else 182 snprintf(sc->sc.sc_vendor, sizeof (sc->sc.sc_vendor), 183 "vendor 0x%04x", PCI_VENDOR(pa->pa_id)); 184 185 config_defer(self, uhci_pci_attach_deferred); 186 187 /* Ignore interrupts for now */ 188 sc->sc.sc_bus.dying = 1; 189 190 splx(s); 191 192 return; 193 194 unmap_ret: 195 bus_space_unmap(sc->sc.iot, sc->sc.ioh, sc->sc.sc_size); 196 sc->sc.sc_size = 0; 197 splx(s); 198 } 199 200 void 201 uhci_pci_attach_deferred(struct device *self) 202 { 203 struct uhci_pci_softc *sc = (struct uhci_pci_softc *)self; 204 char *devname = sc->sc.sc_bus.bdev.dv_xname; 205 usbd_status r; 206 int s; 207 208 s = splhardusb(); 209 210 sc->sc.sc_bus.dying = 0; 211 r = uhci_init(&sc->sc); 212 if (r != USBD_NORMAL_COMPLETION) { 213 printf("%s: init failed, error=%d\n", devname, r); 214 goto unmap_ret; 215 } 216 splx(s); 217 218 /* Attach usb device. */ 219 config_found(self, &sc->sc.sc_bus, usbctlprint); 220 return; 221 222 unmap_ret: 223 bus_space_unmap(sc->sc.iot, sc->sc.ioh, sc->sc.sc_size); 224 pci_intr_disestablish(sc->sc_pc, sc->sc_ih); 225 sc->sc.sc_size = 0; 226 splx(s); 227 } 228 229 int 230 uhci_pci_detach(struct device *self, int flags) 231 { 232 struct uhci_pci_softc *sc = (struct uhci_pci_softc *)self; 233 int rv; 234 235 rv = uhci_detach(self, flags); 236 if (rv) 237 return (rv); 238 if (sc->sc_ih != NULL) { 239 pci_intr_disestablish(sc->sc_pc, sc->sc_ih); 240 sc->sc_ih = NULL; 241 } 242 if (sc->sc.sc_size) { 243 bus_space_unmap(sc->sc.iot, sc->sc.ioh, sc->sc.sc_size); 244 sc->sc.sc_size = 0; 245 } 246 247 return (0); 248 } 249