1 /* $OpenBSD: sili_pci.c,v 1.9 2008/11/23 12:46:51 dlg Exp $ */ 2 3 /* 4 * Copyright (c) 2007 David Gwynne <dlg@openbsd.org> 5 * 6 * Permission to use, copy, modify, and distribute this software for any 7 * purpose with or without fee is hereby granted, provided that the above 8 * copyright notice and this permission notice appear in all copies. 9 * 10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 17 */ 18 19 #include <sys/param.h> 20 #include <sys/systm.h> 21 #include <sys/kernel.h> 22 #include <sys/malloc.h> 23 #include <sys/device.h> 24 #include <sys/timeout.h> 25 26 #include <machine/bus.h> 27 28 #include <dev/pci/pcireg.h> 29 #include <dev/pci/pcivar.h> 30 #include <dev/pci/pcidevs.h> 31 32 #include <dev/ata/atascsi.h> 33 34 #include <dev/ic/silireg.h> 35 #include <dev/ic/silivar.h> 36 37 int sili_pci_match(struct device *, void *, void *); 38 void sili_pci_attach(struct device *, struct device *, void *); 39 int sili_pci_detach(struct device *, int); 40 41 struct sili_pci_softc { 42 struct sili_softc psc_sili; 43 44 pci_chipset_tag_t psc_pc; 45 pcitag_t psc_tag; 46 47 void *psc_ih; 48 }; 49 50 struct cfattach sili_pci_ca = { 51 sizeof(struct sili_pci_softc), 52 sili_pci_match, 53 sili_pci_attach, 54 sili_pci_detach 55 }; 56 57 struct sili_device { 58 pci_vendor_id_t sd_vendor; 59 pci_product_id_t sd_product; 60 u_int sd_nports; 61 }; 62 63 const struct sili_device *sili_lookup(struct pci_attach_args *); 64 65 static const struct sili_device sili_devices[] = { 66 { PCI_VENDOR_CMDTECH, PCI_PRODUCT_CMDTECH_3124, 4 }, 67 { PCI_VENDOR_CMDTECH, PCI_PRODUCT_CMDTECH_3131, 1 }, 68 { PCI_VENDOR_CMDTECH, PCI_PRODUCT_CMDTECH_3132, 2 }, 69 { PCI_VENDOR_CMDTECH, PCI_PRODUCT_CMDTECH_3531, 1 }, 70 { PCI_VENDOR_CMDTECH, PCI_PRODUCT_CMDTECH_AAR_1220SA, 2 }, 71 { PCI_VENDOR_INTEL, PCI_PRODUCT_INTEL_3124, 4 } 72 }; 73 74 const struct sili_device * 75 sili_lookup(struct pci_attach_args *pa) 76 { 77 int i; 78 const struct sili_device *sd; 79 80 for (i = 0; i < nitems(sili_devices); i++) { 81 sd = &sili_devices[i]; 82 if (sd->sd_vendor == PCI_VENDOR(pa->pa_id) && 83 sd->sd_product == PCI_PRODUCT(pa->pa_id)) 84 return (sd); 85 } 86 87 return (NULL); 88 } 89 90 int 91 sili_pci_match(struct device *parent, void *match, void *aux) 92 { 93 return (sili_lookup((struct pci_attach_args *)aux) != NULL); 94 } 95 96 void 97 sili_pci_attach(struct device *parent, struct device *self, void *aux) 98 { 99 struct sili_pci_softc *psc = (void *)self; 100 struct sili_softc *sc = &psc->psc_sili; 101 struct pci_attach_args *pa = aux; 102 const struct sili_device *sd; 103 pcireg_t memtype; 104 pci_intr_handle_t ih; 105 const char *intrstr; 106 107 sd = sili_lookup(pa); 108 109 psc->psc_pc = pa->pa_pc; 110 psc->psc_tag = pa->pa_tag; 111 psc->psc_ih = NULL; 112 sc->sc_dmat = pa->pa_dmat; 113 sc->sc_ios_global = 0; 114 sc->sc_ios_port = 0; 115 sc->sc_nports = sd->sd_nports; 116 117 memtype = pci_mapreg_type(psc->psc_pc, psc->psc_tag, 118 SILI_PCI_BAR_GLOBAL); 119 if (pci_mapreg_map(pa, SILI_PCI_BAR_GLOBAL, memtype, 0, 120 &sc->sc_iot_global, &sc->sc_ioh_global, 121 NULL, &sc->sc_ios_global, 0) != 0) { 122 printf(": unable to map global registers\n"); 123 return; 124 } 125 126 memtype = pci_mapreg_type(psc->psc_pc, psc->psc_tag, 127 SILI_PCI_BAR_PORT); 128 if (pci_mapreg_map(pa, SILI_PCI_BAR_PORT, memtype, 0, 129 &sc->sc_iot_port, &sc->sc_ioh_port, 130 NULL, &sc->sc_ios_port, 0) != 0) { 131 printf(": unable to map port registers\n"); 132 goto unmap_global; 133 } 134 135 /* hook up the interrupt */ 136 if (pci_intr_map(pa, &ih)) { 137 printf(": unable to map interrupt\n"); 138 goto unmap_port; 139 } 140 intrstr = pci_intr_string(psc->psc_pc, ih); 141 psc->psc_ih = pci_intr_establish(psc->psc_pc, ih, IPL_BIO, 142 sili_intr, sc, sc->sc_dev.dv_xname); 143 if (psc->psc_ih == NULL) { 144 printf(": unable to map interrupt%s%s\n", 145 intrstr == NULL ? "" : " at ", 146 intrstr == NULL ? "" : intrstr); 147 goto unmap_port; 148 } 149 printf(": %s", intrstr); 150 151 if (sili_attach(sc) != 0) { 152 /* error printed by sili_attach */ 153 goto deintr; 154 } 155 156 return; 157 158 deintr: 159 pci_intr_disestablish(psc->psc_pc, psc->psc_ih); 160 psc->psc_ih = NULL; 161 unmap_port: 162 bus_space_unmap(sc->sc_iot_port, sc->sc_ioh_port, sc->sc_ios_port); 163 sc->sc_ios_port = 0; 164 unmap_global: 165 bus_space_unmap(sc->sc_iot_global, sc->sc_ioh_global, 166 sc->sc_ios_global); 167 sc->sc_ios_global = 0; 168 } 169 170 int 171 sili_pci_detach(struct device *self, int flags) 172 { 173 struct sili_pci_softc *psc = (struct sili_pci_softc *)self; 174 struct sili_softc *sc = &psc->psc_sili; 175 int rv; 176 177 rv = sili_detach(sc, flags); 178 if (rv != 0) 179 return (rv); 180 181 if (psc->psc_ih != NULL) { 182 pci_intr_disestablish(psc->psc_pc, psc->psc_ih); 183 psc->psc_ih = NULL; 184 } 185 if (sc->sc_ios_port != 0) { 186 bus_space_unmap(sc->sc_iot_port, sc->sc_ioh_port, 187 sc->sc_ios_port); 188 sc->sc_ios_port = 0; 189 } 190 if (sc->sc_ios_global != 0) { 191 bus_space_unmap(sc->sc_iot_global, sc->sc_ioh_global, 192 sc->sc_ios_global); 193 sc->sc_ios_global = 0; 194 } 195 196 return (0); 197 } 198