1 /*- 2 * Copyright (C) 2001 Eduardo Horvath. 3 * All rights reserved. 4 * 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: 9 * 1. Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. 11 * 2. Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in the 13 * documentation and/or other materials provided with the distribution. 14 * 15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND 16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE 19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 25 * SUCH DAMAGE. 26 * 27 * from: NetBSD: if_gem_pci.c,v 1.7 2001/10/18 15:09:15 thorpej Exp 28 * 29 */ 30 31 #include <sys/cdefs.h> 32 __FBSDID("$FreeBSD$"); 33 34 /* 35 * PCI bindings for Sun GEM ethernet controllers. 36 */ 37 38 #include <sys/param.h> 39 #include <sys/systm.h> 40 #include <sys/bus.h> 41 #include <sys/malloc.h> 42 #include <sys/kernel.h> 43 #include <sys/lock.h> 44 #include <sys/module.h> 45 #include <sys/mutex.h> 46 #include <sys/resource.h> 47 #include <sys/socket.h> 48 49 #include <machine/endian.h> 50 51 #include <net/ethernet.h> 52 #include <net/if.h> 53 #include <net/if_arp.h> 54 #include <net/if_dl.h> 55 #include <net/if_media.h> 56 57 #include <machine/bus.h> 58 #include <machine/resource.h> 59 #include <dev/ofw/openfirm.h> 60 #include <machine/ofw_machdep.h> 61 62 #include <sys/rman.h> 63 64 #include <dev/mii/mii.h> 65 #include <dev/mii/miivar.h> 66 67 #include <dev/gem/if_gemreg.h> 68 #include <dev/gem/if_gemvar.h> 69 70 #include <dev/pci/pcivar.h> 71 #include <dev/pci/pcireg.h> 72 73 #include "miibus_if.h" 74 75 static int gem_pci_probe(device_t); 76 static int gem_pci_attach(device_t); 77 static int gem_pci_detach(device_t); 78 static int gem_pci_suspend(device_t); 79 static int gem_pci_resume(device_t); 80 81 static device_method_t gem_pci_methods[] = { 82 /* Device interface */ 83 DEVMETHOD(device_probe, gem_pci_probe), 84 DEVMETHOD(device_attach, gem_pci_attach), 85 DEVMETHOD(device_detach, gem_pci_detach), 86 DEVMETHOD(device_suspend, gem_pci_suspend), 87 DEVMETHOD(device_resume, gem_pci_resume), 88 /* Use the suspend handler here, it is all that is required. */ 89 DEVMETHOD(device_shutdown, gem_pci_suspend), 90 91 /* bus interface */ 92 DEVMETHOD(bus_print_child, bus_generic_print_child), 93 DEVMETHOD(bus_driver_added, bus_generic_driver_added), 94 95 /* MII interface */ 96 DEVMETHOD(miibus_readreg, gem_mii_readreg), 97 DEVMETHOD(miibus_writereg, gem_mii_writereg), 98 DEVMETHOD(miibus_statchg, gem_mii_statchg), 99 100 { 0, 0 } 101 }; 102 103 static driver_t gem_pci_driver = { 104 "gem", 105 gem_pci_methods, 106 sizeof(struct gem_softc) 107 }; 108 109 110 DRIVER_MODULE(gem, pci, gem_pci_driver, gem_devclass, 0, 0); 111 MODULE_DEPEND(gem, pci, 1, 1, 1); 112 MODULE_DEPEND(gem, ether, 1, 1, 1); 113 114 struct gem_pci_dev { 115 u_int32_t gpd_devid; 116 int gpd_variant; 117 char *gpd_desc; 118 } gem_pci_devlist[] = { 119 { 0x1101108e, GEM_SUN_GEM, "Sun ERI 10/100 Ethernet Adaptor" }, 120 { 0x2bad108e, GEM_SUN_GEM, "Sun GEM Gigabit Ethernet Adaptor" }, 121 { 0x0021106b, GEM_APPLE_GMAC, "Apple GMAC Ethernet Adaptor" }, 122 { 0x0024106b, GEM_APPLE_GMAC, "Apple GMAC2 Ethernet Adaptor" }, 123 { 0x0032106b, GEM_APPLE_GMAC, "Apple GMAC3 Ethernet Adaptor" }, 124 { 0, 0, NULL } 125 }; 126 127 /* 128 * Attach routines need to be split out to different bus-specific files. 129 */ 130 static int 131 gem_pci_probe(dev) 132 device_t dev; 133 { 134 int i; 135 u_int32_t devid; 136 struct gem_softc *sc; 137 138 devid = pci_get_devid(dev); 139 for (i = 0; gem_pci_devlist[i].gpd_desc != NULL; i++) { 140 if (devid == gem_pci_devlist[i].gpd_devid) { 141 device_set_desc(dev, gem_pci_devlist[i].gpd_desc); 142 sc = device_get_softc(dev); 143 sc->sc_variant = gem_pci_devlist[i].gpd_variant; 144 return (BUS_PROBE_DEFAULT); 145 } 146 } 147 148 return (ENXIO); 149 } 150 151 static struct resource_spec gem_pci_res_spec[] = { 152 { SYS_RES_MEMORY, PCI_GEM_BASEADDR, RF_ACTIVE }, 153 { SYS_RES_IRQ, 0, RF_SHAREABLE | RF_ACTIVE }, 154 { -1, 0 } 155 }; 156 157 static int 158 gem_pci_attach(dev) 159 device_t dev; 160 { 161 struct gem_softc *sc = device_get_softc(dev); 162 163 pci_enable_busmaster(dev); 164 165 /* 166 * Some Sun GEMs/ERIs do have their intpin register bogusly set to 0, 167 * although it should be 1. correct that. 168 */ 169 if (pci_get_intpin(dev) == 0) 170 pci_set_intpin(dev, 1); 171 172 sc->sc_dev = dev; 173 sc->sc_pci = 1; /* XXX */ 174 175 if (bus_alloc_resources(dev, gem_pci_res_spec, sc->sc_res)) { 176 device_printf(dev, "failed to allocate resources\n"); 177 bus_release_resources(dev, gem_pci_res_spec, sc->sc_res); 178 return (ENXIO); 179 } 180 181 GEM_LOCK_INIT(sc, device_get_nameunit(dev)); 182 183 /* All platform that this driver is used on must provide this. */ 184 OF_getetheraddr(dev, sc->sc_enaddr); 185 186 /* 187 * call the main configure 188 */ 189 if (gem_attach(sc) != 0) { 190 device_printf(dev, "could not be configured\n"); 191 goto fail; 192 } 193 194 if (bus_setup_intr(dev, sc->sc_res[1], INTR_TYPE_NET | INTR_MPSAFE, 195 NULL, gem_intr, sc, &sc->sc_ih) != 0) { 196 device_printf(dev, "failed to set up interrupt\n"); 197 gem_detach(sc); 198 goto fail; 199 } 200 return (0); 201 202 fail: 203 bus_release_resources(dev, gem_pci_res_spec, sc->sc_res); 204 GEM_LOCK_DESTROY(sc); 205 return (ENXIO); 206 } 207 208 static int 209 gem_pci_detach(dev) 210 device_t dev; 211 { 212 struct gem_softc *sc = device_get_softc(dev); 213 214 bus_teardown_intr(dev, sc->sc_res[1], sc->sc_ih); 215 gem_detach(sc); 216 GEM_LOCK_DESTROY(sc); 217 bus_release_resources(dev, gem_pci_res_spec, sc->sc_res); 218 return (0); 219 } 220 221 static int 222 gem_pci_suspend(dev) 223 device_t dev; 224 { 225 struct gem_softc *sc = device_get_softc(dev); 226 227 gem_suspend(sc); 228 return (0); 229 } 230 231 static int 232 gem_pci_resume(dev) 233 device_t dev; 234 { 235 struct gem_softc *sc = device_get_softc(dev); 236 237 gem_resume(sc); 238 return (0); 239 } 240