1 /* $OpenBSD: if_gem_pci.c,v 1.5 2001/12/14 02:43:56 drahn Exp $ */ 2 /* $NetBSD: if_gem_pci.c,v 1.1 2001/09/16 00:11:42 eeh Exp $ */ 3 4 /* 5 * 6 * Copyright (C) 2001 Eduardo Horvath. 7 * All rights reserved. 8 * 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND 20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 22 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE 23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 29 * SUCH DAMAGE. 30 * 31 */ 32 33 /* 34 * PCI bindings for Sun GEM ethernet controllers. 35 */ 36 37 #include <sys/param.h> 38 #include <sys/systm.h> 39 #include <sys/malloc.h> 40 #include <sys/kernel.h> 41 #include <sys/socket.h> 42 #include <sys/errno.h> 43 #include <sys/device.h> 44 45 #include <machine/endian.h> 46 47 #include <uvm/uvm_extern.h> 48 49 #include <net/if.h> 50 #include <net/if_dl.h> 51 #include <net/if_media.h> 52 53 #ifdef INET 54 #include <netinet/in.h> 55 #include <netinet/if_ether.h> 56 #endif 57 58 #if NBPFILTER > 0 59 #include <net/bpf.h> 60 #endif 61 62 #include <machine/bus.h> 63 #include <machine/intr.h> 64 65 #include <dev/mii/mii.h> 66 #include <dev/mii/miivar.h> 67 #include <dev/mii/mii_bitbang.h> 68 69 #include <dev/ic/gemreg.h> 70 #include <dev/ic/gemvar.h> 71 72 #include <dev/pci/pcivar.h> 73 #include <dev/pci/pcireg.h> 74 #include <dev/pci/pcidevs.h> 75 76 struct gem_pci_softc { 77 struct gem_softc gsc_gem; /* GEM device */ 78 bus_space_tag_t gsc_memt; 79 bus_space_handle_t gsc_memh; 80 void *gsc_ih; 81 }; 82 83 int gem_match_pci __P((struct device *, void *, void *)); 84 void gem_attach_pci __P((struct device *, struct device *, void *)); 85 86 struct cfattach gem_pci_ca = { 87 sizeof(struct gem_pci_softc), gem_match_pci, gem_attach_pci 88 }; 89 90 /* 91 * Attach routines need to be split out to different bus-specific files. 92 */ 93 94 int 95 gem_match_pci(parent, cf, aux) 96 struct device *parent; 97 void *cf; 98 void *aux; 99 { 100 struct pci_attach_args *pa = aux; 101 102 if (PCI_VENDOR(pa->pa_id) == PCI_VENDOR_SUN && 103 (PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_SUN_ERINETWORK || 104 PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_SUN_GEMNETWORK)) 105 return (1); 106 107 if (PCI_VENDOR(pa->pa_id) == PCI_VENDOR_APPLE && 108 (PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_APPLE_GMAC || 109 PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_APPLE_GMAC2)) 110 return (1); 111 112 return (0); 113 } 114 115 void 116 gem_attach_pci(parent, self, aux) 117 struct device *parent, *self; 118 void *aux; 119 { 120 struct pci_attach_args *pa = aux; 121 struct gem_pci_softc *gsc = (void *)self; 122 struct gem_softc *sc = &gsc->gsc_gem; 123 pci_intr_handle_t intrhandle; 124 #ifdef __sparc__ 125 /* XXX the following declarations should be elsewhere */ 126 extern void myetheraddr __P((u_char *)); 127 #endif 128 const char *intrstr; 129 int type; 130 131 if (pa->pa_memt) { 132 type = PCI_MAPREG_TYPE_MEM; 133 sc->sc_bustag = pa->pa_memt; 134 } else { 135 type = PCI_MAPREG_TYPE_IO; 136 sc->sc_bustag = pa->pa_iot; 137 } 138 139 sc->sc_dmatag = pa->pa_dmat; 140 141 sc->sc_pci = 1; /* XXXXX should all be done in bus_dma. */ 142 143 #define PCI_GEM_BASEADDR 0x10 144 if (pci_mapreg_map(pa, PCI_GEM_BASEADDR, type, 0, 145 &gsc->gsc_memt, &gsc->gsc_memh, NULL, NULL, 0) != 0) 146 { 147 printf(": could not map gem registers\n"); 148 return; 149 } 150 151 sc->sc_bustag = gsc->gsc_memt; 152 sc->sc_h = gsc->gsc_memh; 153 154 #if 0 155 /* SBUS compatible stuff? */ 156 sc->sc_seb = gsc->gsc_memh; 157 sc->sc_etx = gsc->gsc_memh + 0x2000; 158 sc->sc_erx = gsc->gsc_memh + 0x4000; 159 sc->sc_mac = gsc->gsc_memh + 0x6000; 160 sc->sc_mif = gsc->gsc_memh + 0x7000; 161 #endif 162 #ifdef __sparc__ 163 myetheraddr(sc->sc_enaddr); 164 #endif 165 #ifdef __powerpc__ 166 pci_ether_hw_addr(pa->pa_pc, sc->sc_enaddr); 167 #endif 168 169 sc->sc_burst = 16; /* XXX */ 170 171 printf("\n"); 172 /* 173 * call the main configure 174 */ 175 gem_config(sc); 176 177 if (pci_intr_map(pa, &intrhandle) != 0) { 178 printf("%s: couldn't map interrupt\n", 179 sc->sc_dev.dv_xname); 180 return; /* bus_unmap ? */ 181 } 182 intrstr = pci_intr_string(pa->pa_pc, intrhandle); 183 gsc->gsc_ih = pci_intr_establish(pa->pa_pc, 184 intrhandle, IPL_NET, gem_intr, sc, self->dv_xname); 185 if (gsc->gsc_ih != NULL) { 186 printf("%s: using %s for interrupt\n", 187 sc->sc_dev.dv_xname, 188 intrstr ? intrstr : "unknown interrupt"); 189 } else { 190 printf("%s: couldn't establish interrupt", 191 sc->sc_dev.dv_xname); 192 if (intrstr != NULL) 193 printf(" at %s", intrstr); 194 printf("\n"); 195 return; /* bus_unmap ? */ 196 } 197 } 198