1 /* $NetBSD: if_ep_pci.c,v 1.54 2018/06/23 06:57:24 maxv 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 Jason R. Thorpe of the Numerical Aerospace Simulation Facility, 9 * NASA Ames Research Center. 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 /* 34 * Copyright (c) 1997 Jonathan Stone <jonathan@NetBSD.org> 35 * Copyright (c) 1994 Herb Peyerl <hpeyerl@beer.org> 36 * All rights reserved. 37 * 38 * Redistribution and use in source and binary forms, with or without 39 * modification, are permitted provided that the following conditions 40 * are met: 41 * 1. Redistributions of source code must retain the above copyright 42 * notice, this list of conditions and the following disclaimer. 43 * 2. Redistributions in binary form must reproduce the above copyright 44 * notice, this list of conditions and the following disclaimer in the 45 * documentation and/or other materials provided with the distribution. 46 * 3. All advertising materials mentioning features or use of this software 47 * must display the following acknowledgement: 48 * This product includes software developed by Herb Peyerl. 49 * 4. The name of Herb Peyerl may not be used to endorse or promote products 50 * derived from this software without specific prior written permission. 51 * 52 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 53 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 54 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 55 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 56 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 57 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 58 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 59 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 60 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 61 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 62 */ 63 64 #include <sys/cdefs.h> 65 __KERNEL_RCSID(0, "$NetBSD: if_ep_pci.c,v 1.54 2018/06/23 06:57:24 maxv Exp $"); 66 67 #include <sys/param.h> 68 #include <sys/systm.h> 69 #include <sys/mbuf.h> 70 #include <sys/socket.h> 71 #include <sys/ioctl.h> 72 #include <sys/errno.h> 73 #include <sys/syslog.h> 74 #include <sys/select.h> 75 #include <sys/device.h> 76 77 #include <net/if.h> 78 #include <net/if_dl.h> 79 #include <net/if_ether.h> 80 #include <net/if_media.h> 81 82 #include <sys/cpu.h> 83 #include <sys/bus.h> 84 #include <sys/intr.h> 85 86 #include <dev/mii/miivar.h> 87 88 #include <dev/ic/elink3var.h> 89 #include <dev/ic/elink3reg.h> 90 91 #include <dev/pci/pcivar.h> 92 #include <dev/pci/pcireg.h> 93 #include <dev/pci/pcidevs.h> 94 95 /* 96 * PCI constants. 97 */ 98 #define PCI_CBIO PCI_BAR(0) /* Configuration Base IO Address */ 99 100 static int ep_pci_match(device_t , cfdata_t, void *); 101 static void ep_pci_attach(device_t , device_t , void *); 102 103 CFATTACH_DECL_NEW(ep_pci, sizeof(struct ep_softc), 104 ep_pci_match, ep_pci_attach, NULL, NULL); 105 106 static const struct ep_pci_product { 107 u_int32_t epp_prodid; /* PCI product ID */ 108 u_short epp_chipset; /* 3Com chipset used */ 109 int epp_flags; /* initial softc flags */ 110 const char *epp_name; /* device name */ 111 } ep_pci_products[] = { 112 { PCI_PRODUCT_3COM_3C590, ELINK_CHIPSET_VORTEX, 113 0, 114 "3c590 Ethernet" }, 115 116 /* 117 * Note: The 3c595-MII is an MII connector for an 118 * external PHY. We treat it as `manual' in the 119 * core driver. 120 */ 121 { PCI_PRODUCT_3COM_3C595TX, ELINK_CHIPSET_VORTEX, 122 0, 123 "3c595-TX 10/100 Ethernet" }, 124 { PCI_PRODUCT_3COM_3C595T4, ELINK_CHIPSET_VORTEX, 125 0, 126 "3c595-T4 10/100 Ethernet" }, 127 { PCI_PRODUCT_3COM_3C595MII, ELINK_CHIPSET_VORTEX, 128 0, 129 "3c595-MII 10/100 Ethernet" }, 130 131 { PCI_PRODUCT_3COM_3C900TPO, ELINK_CHIPSET_BOOMERANG, 132 0, 133 "3c900-TPO Ethernet" }, 134 { PCI_PRODUCT_3COM_3C900COMBO, ELINK_CHIPSET_BOOMERANG, 135 0, 136 "3c900-COMBO Ethernet" }, 137 138 { PCI_PRODUCT_3COM_3C905TX, ELINK_CHIPSET_BOOMERANG, 139 ELINK_FLAGS_MII, 140 "3c905-TX 10/100 Ethernet" }, 141 { PCI_PRODUCT_3COM_3C905T4, ELINK_CHIPSET_BOOMERANG, 142 ELINK_FLAGS_MII, 143 "3c905-T4 10/100 Ethernet" }, 144 145 { 0, 0, 146 0, NULL }, 147 }; 148 149 static const struct ep_pci_product * 150 ep_pci_lookup(const struct pci_attach_args *pa) 151 { 152 const struct ep_pci_product *epp; 153 154 if (PCI_VENDOR(pa->pa_id) != PCI_VENDOR_3COM) 155 return (NULL); 156 157 for (epp = ep_pci_products; epp->epp_name != NULL; epp++) 158 if (PCI_PRODUCT(pa->pa_id) == epp->epp_prodid) 159 return (epp); 160 161 return (NULL); 162 } 163 164 static int 165 ep_pci_match(device_t parent, cfdata_t match, void *aux) 166 { 167 struct pci_attach_args *pa = (struct pci_attach_args *) aux; 168 169 if (ep_pci_lookup(pa) != NULL) 170 return (1); 171 172 return (0); 173 } 174 175 static void 176 ep_pci_attach(device_t parent, device_t self, void *aux) 177 { 178 struct ep_softc *sc = device_private(self); 179 struct pci_attach_args *pa = aux; 180 pci_chipset_tag_t pc = pa->pa_pc; 181 pci_intr_handle_t ih; 182 const struct ep_pci_product *epp; 183 const char *intrstr = NULL; 184 char intrbuf[PCI_INTRSTR_LEN]; 185 186 aprint_naive(": Ethernet controller\n"); 187 188 if (pci_mapreg_map(pa, PCI_CBIO, PCI_MAPREG_TYPE_IO, 0, 189 &sc->sc_iot, &sc->sc_ioh, NULL, NULL)) { 190 aprint_error(": can't map i/o space\n"); 191 return; 192 } 193 194 epp = ep_pci_lookup(pa); 195 if (epp == NULL) { 196 printf("\n"); 197 panic("ep_pci_attach: impossible"); 198 } 199 200 aprint_normal(": 3Com %s\n", epp->epp_name); 201 202 sc->sc_dev = self; 203 sc->enable = NULL; 204 sc->disable = NULL; 205 sc->enabled = 1; 206 207 sc->bustype = ELINK_BUS_PCI; 208 sc->ep_flags = epp->epp_flags; 209 210 /* Enable the card. */ 211 pci_conf_write(pc, pa->pa_tag, PCI_COMMAND_STATUS_REG, 212 pci_conf_read(pc, pa->pa_tag, PCI_COMMAND_STATUS_REG) | 213 PCI_COMMAND_MASTER_ENABLE); 214 215 /* Map and establish the interrupt. */ 216 if (pci_intr_map(pa, &ih)) { 217 aprint_error_dev(sc->sc_dev, "couldn't map interrupt\n"); 218 return; 219 } 220 intrstr = pci_intr_string(pc, ih, intrbuf, sizeof(intrbuf)); 221 sc->sc_ih = pci_intr_establish(pc, ih, IPL_NET, epintr, sc); 222 if (sc->sc_ih == NULL) { 223 aprint_error_dev(sc->sc_dev, "couldn't establish interrupt"); 224 if (intrstr != NULL) 225 aprint_error(" at %s", intrstr); 226 aprint_error("\n"); 227 return; 228 } 229 aprint_normal_dev(sc->sc_dev, "interrupting at %s\n", intrstr); 230 231 epconfig(sc, epp->epp_chipset, NULL); 232 } 233