1 /* $OpenBSD: uhci_cardbus.c,v 1.16 2022/04/06 18:59:28 naddy Exp $ */ 2 3 /* 4 * Copyright (c) 1998 The NetBSD Foundation, Inc. 5 * All rights reserved. 6 * 7 * This code is derived from software contributed to The NetBSD Foundation 8 * by Lennart Augustsson (lennart@augustsson.net) at 9 * Carlstedt Research & Technology. 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/device.h> 37 38 #include <machine/bus.h> 39 40 #include <dev/cardbus/cardbusvar.h> 41 42 #include <dev/usb/usb.h> 43 #include <dev/usb/usbdi.h> 44 #include <dev/usb/usbdivar.h> 45 #include <dev/usb/usb_mem.h> 46 47 #include <dev/usb/uhcireg.h> 48 #include <dev/usb/uhcivar.h> 49 50 int uhci_cardbus_match(struct device *, void *, void *); 51 void uhci_cardbus_attach(struct device *, struct device *, void *); 52 int uhci_cardbus_detach(struct device *, int); 53 54 struct uhci_cardbus_softc { 55 struct uhci_softc sc; 56 cardbus_chipset_tag_t sc_cc; 57 cardbus_function_tag_t sc_cf; 58 cardbus_devfunc_t sc_ct; 59 void *sc_ih; /* interrupt vectoring */ 60 }; 61 62 const struct cfattach uhci_cardbus_ca = { 63 sizeof(struct uhci_cardbus_softc), uhci_cardbus_match, 64 uhci_cardbus_attach, uhci_cardbus_detach, uhci_activate 65 }; 66 67 #define cardbus_findvendor pci_findvendor 68 69 int 70 uhci_cardbus_match(struct device *parent, void *match, void *aux) 71 { 72 struct cardbus_attach_args *ca = (struct cardbus_attach_args *)aux; 73 74 if (PCI_CLASS(ca->ca_class) == PCI_CLASS_SERIALBUS && 75 PCI_SUBCLASS(ca->ca_class) == PCI_SUBCLASS_SERIALBUS_USB && 76 PCI_INTERFACE(ca->ca_class) == PCI_INTERFACE_UHCI) 77 return (1); 78 79 return (0); 80 } 81 82 void 83 uhci_cardbus_attach(struct device *parent, struct device *self, void *aux) 84 { 85 struct uhci_cardbus_softc *sc = (struct uhci_cardbus_softc *)self; 86 struct cardbus_attach_args *ca = aux; 87 cardbus_devfunc_t ct = ca->ca_ct; 88 cardbus_chipset_tag_t cc = ct->ct_cc; 89 pci_chipset_tag_t pc = ca->ca_pc; 90 cardbus_function_tag_t cf = ct->ct_cf; 91 pcireg_t csr; 92 usbd_status r; 93 const char *vendor; 94 const char *devname = sc->sc.sc_bus.bdev.dv_xname; 95 96 /* Map I/O registers */ 97 if (Cardbus_mapreg_map(ct, PCI_CBIO, PCI_MAPREG_TYPE_IO, 0, 98 &sc->sc.iot, &sc->sc.ioh, NULL, &sc->sc.sc_size)) { 99 printf(": can't map io space\n"); 100 return; 101 } 102 103 /* Disable interrupts, so we don't get any spurious ones. */ 104 bus_space_write_2(sc->sc.iot, sc->sc.ioh, UHCI_INTR, 0); 105 106 sc->sc_cc = cc; 107 sc->sc_cf = cf; 108 sc->sc_ct = ct; 109 sc->sc.sc_bus.dmatag = ca->ca_dmat; 110 111 (ct->ct_cf->cardbus_ctrl)(cc, CARDBUS_IO_ENABLE); 112 (ct->ct_cf->cardbus_ctrl)(cc, CARDBUS_BM_ENABLE); 113 114 /* Enable the device. */ 115 csr = pci_conf_read(pc, ca->ca_tag, 116 PCI_COMMAND_STATUS_REG); 117 pci_conf_write(pc, ca->ca_tag, PCI_COMMAND_STATUS_REG, 118 csr | PCI_COMMAND_MASTER_ENABLE 119 | PCI_COMMAND_IO_ENABLE); 120 121 sc->sc_ih = cardbus_intr_establish(cc, cf, ca->ca_intrline, 122 IPL_USB, uhci_intr, sc, devname); 123 if (sc->sc_ih == NULL) { 124 printf(": couldn't establish interrupt\n"); 125 return; 126 } 127 printf(": irq %d\n", ca->ca_intrline); 128 129 /* Set LEGSUP register to its default value. */ 130 pci_conf_write(pc, ca->ca_tag, PCI_LEGSUP, 131 PCI_LEGSUP_USBPIRQDEN); 132 133 switch(pci_conf_read(pc, ca->ca_tag, PCI_USBREV) & PCI_USBREV_MASK) { 134 case PCI_USBREV_PRE_1_0: 135 sc->sc.sc_bus.usbrev = USBREV_PRE_1_0; 136 break; 137 case PCI_USBREV_1_0: 138 sc->sc.sc_bus.usbrev = USBREV_1_0; 139 break; 140 case PCI_USBREV_1_1: 141 sc->sc.sc_bus.usbrev = USBREV_1_1; 142 break; 143 default: 144 sc->sc.sc_bus.usbrev = USBREV_UNKNOWN; 145 break; 146 } 147 148 uhci_run(&sc->sc, 0); /* stop the controller */ 149 /* disable interrupts */ 150 bus_space_barrier(sc->sc.iot, sc->sc.ioh, 0, sc->sc.sc_size, 151 BUS_SPACE_BARRIER_READ|BUS_SPACE_BARRIER_WRITE); 152 bus_space_write_2(sc->sc.iot, sc->sc.ioh, UHCI_INTR, 0); 153 154 /* Figure out vendor for root hub descriptor. */ 155 vendor = cardbus_findvendor(ca->ca_id); 156 sc->sc.sc_id_vendor = PCI_VENDOR(ca->ca_id); 157 if (vendor) 158 strlcpy(sc->sc.sc_vendor, vendor, sizeof (sc->sc.sc_vendor)); 159 else 160 snprintf(sc->sc.sc_vendor, sizeof(sc->sc.sc_vendor), 161 "vendor 0x%04x", PCI_VENDOR(ca->ca_id)); 162 163 r = uhci_init(&sc->sc); 164 if (r != USBD_NORMAL_COMPLETION) { 165 printf("%s: init failed, error=%d\n", devname, r); 166 bus_space_unmap(sc->sc.iot, sc->sc.ioh, sc->sc.sc_size); 167 return; 168 } 169 170 /* Attach usb device. */ 171 config_found(self, &sc->sc.sc_bus, usbctlprint); 172 } 173 174 int 175 uhci_cardbus_detach(struct device *self, int flags) 176 { 177 struct uhci_cardbus_softc *sc = (struct uhci_cardbus_softc *)self; 178 struct cardbus_devfunc *ct = sc->sc_ct; 179 int rv; 180 181 rv = uhci_detach(self, flags); 182 if (rv) 183 return (rv); 184 185 if (sc->sc_ih != NULL) { 186 cardbus_intr_disestablish(sc->sc_cc, sc->sc_cf, sc->sc_ih); 187 sc->sc_ih = NULL; 188 } 189 190 if (sc->sc.sc_size) { 191 Cardbus_mapreg_unmap(ct, PCI_CBIO, sc->sc.iot, 192 sc->sc.ioh, sc->sc.sc_size); 193 sc->sc.sc_size = 0; 194 } 195 196 return (0); 197 } 198