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