1 /* $OpenBSD: if_ne_pci.c,v 1.4 1999/01/11 01:28:03 millert Exp $ */ 2 /* $NetBSD: if_ne_pci.c,v 1.8 1998/07/05 00:51:24 jonathan Exp $ */ 3 4 /*- 5 * Copyright (c) 1997, 1998 The NetBSD Foundation, Inc. 6 * All rights reserved. 7 * 8 * This code is derived from software contributed to The NetBSD Foundation 9 * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility, 10 * NASA Ames Research Center. 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 * 3. All advertising materials mentioning features or use of this software 21 * must display the following acknowledgement: 22 * This product includes software developed by the NetBSD 23 * Foundation, Inc. and its contributors. 24 * 4. Neither the name of The NetBSD Foundation nor the names of its 25 * contributors may be used to endorse or promote products derived 26 * from this software without specific prior written permission. 27 * 28 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 29 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 30 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 31 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 32 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 33 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 34 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 35 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 36 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 37 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 38 * POSSIBILITY OF SUCH DAMAGE. 39 */ 40 41 #include "bpfilter.h" 42 43 #include <sys/param.h> 44 #include <sys/systm.h> 45 #include <sys/mbuf.h> 46 #include <sys/syslog.h> 47 #include <sys/socket.h> 48 #include <sys/device.h> 49 50 #include <net/if.h> 51 #ifdef __NetBSD__ 52 #include <net/if_ether.h> 53 #endif 54 #include <net/if_media.h> 55 56 #ifdef INET 57 #include <netinet/in.h> 58 #ifdef __NetBSD__ 59 #include <netinet/if_inarp.h> 60 #else 61 #include <netinet/if_ether.h> 62 #endif 63 #endif 64 65 #include <machine/bus.h> 66 #include <machine/intr.h> 67 68 #include <dev/pci/pcireg.h> 69 #include <dev/pci/pcivar.h> 70 #include <dev/pci/pcidevs.h> 71 72 #include <dev/ic/dp8390reg.h> 73 #include <dev/ic/dp8390var.h> 74 75 #include <dev/ic/ne2000reg.h> 76 #include <dev/ic/ne2000var.h> 77 78 #include <dev/ic/rtl80x9reg.h> 79 #include <dev/ic/rtl80x9var.h> 80 81 struct ne_pci_softc { 82 struct ne2000_softc sc_ne2000; /* real "ne2000" softc */ 83 84 /* PCI-specific goo */ 85 void *sc_ih; /* interrupt handle */ 86 }; 87 88 int ne_pci_match __P((struct device *, void *, void *)); 89 void ne_pci_attach __P((struct device *, struct device *, void *)); 90 91 struct cfattach ne_pci_ca = { 92 sizeof(struct ne_pci_softc), ne_pci_match, ne_pci_attach 93 }; 94 95 const struct ne_pci_product { 96 pci_vendor_id_t npp_vendor; 97 pci_product_id_t npp_product; 98 int (*npp_mediachange) __P((struct dp8390_softc *)); 99 void (*npp_mediastatus) __P((struct dp8390_softc *, 100 struct ifmediareq *)); 101 void (*npp_init_card) __P((struct dp8390_softc *)); 102 void (*npp_init_media) __P((struct dp8390_softc *, int **, 103 int *, int *)); 104 const char *npp_name; 105 } ne_pci_products[] = { 106 { PCI_VENDOR_REALTEK, PCI_PRODUCT_REALTEK_RT8029, 107 rtl80x9_mediachange, rtl80x9_mediastatus, 108 rtl80x9_init_card, rtl80x9_init_media, 109 "RealTek 8029" }, 110 111 { PCI_VENDOR_WINBOND, PCI_PRODUCT_WINBOND_W89C940F, 112 NULL, NULL, 113 NULL, NULL, 114 "Winbond 89C940F" }, 115 116 { PCI_VENDOR_VIATECH, PCI_PRODUCT_VIATECH_VT86C926, 117 NULL, NULL, 118 NULL, NULL, 119 "VIA Technologies VT86C926" }, 120 121 { PCI_VENDOR_SURECOM, PCI_PRODUCT_SURECOM_NE34, 122 NULL, NULL, 123 NULL, NULL, 124 "Surecom NE-34" }, 125 126 { PCI_VENDOR_NETVIN, PCI_PRODUCT_NETVIN_NV5000, 127 NULL, NULL, 128 NULL, NULL, 129 "NetVin 5000" }, 130 131 /* XXX The following entries need sanity checking in pcidevs */ 132 { PCI_VENDOR_COMPEX, PCI_PRODUCT_COMPEX_COMPEXE, 133 NULL, NULL, 134 NULL, NULL, 135 "Compex" }, 136 137 { PCI_VENDOR_WINBOND2, PCI_PRODUCT_WINBOND2_W89C940, 138 NULL, NULL, 139 NULL, NULL, 140 "ProLAN" }, 141 142 { PCI_VENDOR_KTI, PCI_PRODUCT_KTI_KTIE, 143 NULL, NULL, 144 NULL, NULL, 145 "KTI" }, 146 147 { 0, 0, 148 NULL, NULL, 149 NULL, NULL, 150 NULL }, 151 }; 152 153 const struct ne_pci_product *ne_pci_lookup __P((struct pci_attach_args *)); 154 155 const struct ne_pci_product * 156 ne_pci_lookup(pa) 157 struct pci_attach_args *pa; 158 { 159 const struct ne_pci_product *npp; 160 161 for (npp = ne_pci_products; npp->npp_name != NULL; npp++) { 162 if (PCI_VENDOR(pa->pa_id) == npp->npp_vendor && 163 PCI_PRODUCT(pa->pa_id) == npp->npp_product) 164 return (npp); 165 } 166 return (NULL); 167 } 168 169 /* 170 * PCI constants. 171 * XXX These should be in a common file! 172 */ 173 #define PCI_CBIO 0x10 /* Configuration Base IO Address */ 174 175 int 176 ne_pci_match(parent, match, aux) 177 struct device *parent; 178 void *match, *aux; 179 { 180 struct pci_attach_args *pa = aux; 181 182 if (ne_pci_lookup(pa) != NULL) 183 return (1); 184 185 return (0); 186 } 187 188 void 189 ne_pci_attach(parent, self, aux) 190 struct device *parent, *self; 191 void *aux; 192 { 193 struct ne_pci_softc *psc = (struct ne_pci_softc *)self; 194 struct ne2000_softc *nsc = &psc->sc_ne2000; 195 struct dp8390_softc *dsc = &nsc->sc_dp8390; 196 struct pci_attach_args *pa = aux; 197 pci_chipset_tag_t pc = pa->pa_pc; 198 #ifndef __NetBSD__ 199 bus_addr_t iobase; 200 bus_size_t iosize; 201 #endif 202 bus_space_tag_t nict; 203 bus_space_handle_t nich; 204 bus_space_tag_t asict; 205 bus_space_handle_t asich; 206 const char *intrstr; 207 const struct ne_pci_product *npp; 208 pci_intr_handle_t ih; 209 pcireg_t csr; 210 int *media, nmedia, defmedia; 211 212 npp = ne_pci_lookup(pa); 213 if (npp == NULL) 214 panic("\nne_pci_attach: impossible"); 215 216 printf(": %s Ethernet\n", npp->npp_name); 217 218 #ifdef __NetBSD__ 219 if (pci_mapreg_map(pa, PCI_CBIO, PCI_MAPREG_TYPE_IO, 0, 220 &nict, &nich, NULL, NULL)) { 221 printf(": can't map i/o space\n"); 222 return; 223 } 224 #else 225 if (pci_io_find(pc, pa->pa_tag, PCI_CBIO, &iobase, &iosize)) { 226 printf(": can't find I/O base\n"); 227 return; 228 } 229 230 nict = pa->pa_iot; 231 232 if (bus_space_map(nict, iobase, iosize, 0, &nich)) { 233 printf(": can't map I/O space\n"); 234 return; 235 } 236 #endif 237 238 asict = nict; 239 if (bus_space_subregion(nict, nich, NE2000_ASIC_OFFSET, 240 NE2000_ASIC_NPORTS, &asich)) { 241 printf(": can't subregion i/o space\n"); 242 return; 243 } 244 245 dsc->sc_regt = nict; 246 dsc->sc_regh = nich; 247 248 nsc->sc_asict = asict; 249 nsc->sc_asich = asich; 250 251 /* Enable the card. */ 252 csr = pci_conf_read(pc, pa->pa_tag, 253 PCI_COMMAND_STATUS_REG); 254 pci_conf_write(pc, pa->pa_tag, PCI_COMMAND_STATUS_REG, 255 csr | PCI_COMMAND_MASTER_ENABLE); 256 257 /* This interface is always enabled. */ 258 dsc->sc_enabled = 1; 259 260 if (npp->npp_init_media != NULL) { 261 (*npp->npp_init_media)(dsc, &media, &nmedia, &defmedia); 262 dsc->sc_mediachange = npp->npp_mediachange; 263 dsc->sc_mediastatus = npp->npp_mediastatus; 264 } else { 265 media = NULL; 266 nmedia = 0; 267 defmedia = 0; 268 } 269 270 /* Always fill in init_card; it might be used for non-media stuff. */ 271 dsc->init_card = npp->npp_init_card; 272 273 /* 274 * Do generic NE2000 attach. This will read the station address 275 * from the EEPROM. 276 */ 277 ne2000_attach(nsc, NULL, media, nmedia, defmedia); 278 279 /* Map and establish the interrupt. */ 280 if (pci_intr_map(pc, pa->pa_intrtag, pa->pa_intrpin, 281 pa->pa_intrline, &ih)) { 282 printf("%s: couldn't map interrupt\n", dsc->sc_dev.dv_xname); 283 return; 284 } 285 intrstr = pci_intr_string(pc, ih); 286 psc->sc_ih = pci_intr_establish(pc, ih, IPL_NET, dp8390_intr, dsc, 287 dsc->sc_dev.dv_xname); 288 if (psc->sc_ih == NULL) { 289 printf("%s: couldn't establish interrupt", 290 dsc->sc_dev.dv_xname); 291 if (intrstr != NULL) 292 printf(" at %s", intrstr); 293 printf("\n"); 294 return; 295 } 296 printf("%s: %s\n", dsc->sc_dev.dv_xname, intrstr); 297 } 298