if_hme_pci.c revision 117116
1/* 2 * Copyright (c) 2000 Matthew R. Green 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 3. The name of the author may not be used to endorse or promote products 14 * derived from this software without specific prior written permission. 15 * 16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 21 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 22 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 23 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 24 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 26 * SUCH DAMAGE. 27 * 28 * from: NetBSD: if_hme_pci.c,v 1.4 2001/08/27 22:18:49 augustss Exp 29 * 30 * $FreeBSD: head/sys/dev/hme/if_hme_pci.c 117116 2003-07-01 14:11:04Z tmm $ 31 */ 32 33/* 34 * PCI front-end device driver for the HME ethernet device. 35 */ 36 37#include <sys/param.h> 38#include <sys/systm.h> 39#include <sys/bus.h> 40#include <sys/kernel.h> 41#include <sys/resource.h> 42#include <sys/socket.h> 43 44#include <machine/bus.h> 45#include <machine/ofw_machdep.h> 46#include <machine/resource.h> 47 48#include <sys/rman.h> 49 50#include <net/ethernet.h> 51#include <net/if.h> 52#include <net/if_arp.h> 53#include <net/if_dl.h> 54#include <net/if_media.h> 55 56#include <mii/mii.h> 57#include <mii/miivar.h> 58 59#include <pci/pcivar.h> 60#include <pci/pcireg.h> 61 62#include <hme/if_hmereg.h> 63#include <hme/if_hmevar.h> 64 65#include "miibus_if.h" 66 67struct hme_pci_softc { 68 struct hme_softc hsc_hme; /* HME device */ 69 struct resource *hsc_sres; 70 int hsc_srid; 71 struct resource *hsc_ires; 72 int hsc_irid; 73 bus_space_tag_t hsc_memt; 74 bus_space_handle_t hsc_memh; 75 void *hsc_ih; 76}; 77 78static int hme_pci_probe(device_t); 79static int hme_pci_attach(device_t); 80static int hme_pci_detach(device_t); 81static int hme_pci_suspend(device_t); 82static int hme_pci_resume(device_t); 83 84static device_method_t hme_pci_methods[] = { 85 /* Device interface */ 86 DEVMETHOD(device_probe, hme_pci_probe), 87 DEVMETHOD(device_attach, hme_pci_attach), 88 DEVMETHOD(device_detach, hme_pci_detach), 89 DEVMETHOD(device_suspend, hme_pci_suspend), 90 DEVMETHOD(device_resume, hme_pci_resume), 91 /* Can just use the suspend method here. */ 92 DEVMETHOD(device_shutdown, hme_pci_suspend), 93 94 /* bus interface */ 95 DEVMETHOD(bus_print_child, bus_generic_print_child), 96 DEVMETHOD(bus_driver_added, bus_generic_driver_added), 97 98 /* MII interface */ 99 DEVMETHOD(miibus_readreg, hme_mii_readreg), 100 DEVMETHOD(miibus_writereg, hme_mii_writereg), 101 DEVMETHOD(miibus_statchg, hme_mii_statchg), 102 103 { 0, 0 } 104}; 105 106static driver_t hme_pci_driver = { 107 "hme", 108 hme_pci_methods, 109 sizeof(struct hme_pci_softc) 110}; 111 112DRIVER_MODULE(hme, pci, hme_pci_driver, hme_devclass, 0, 0); 113MODULE_DEPEND(hme, pci, 1, 1, 1); 114MODULE_DEPEND(hme, ether, 1, 1, 1); 115 116int 117hme_pci_probe(device_t dev) 118{ 119 120 if (pci_get_vendor(dev) == 0x108e && 121 pci_get_device(dev) == 0x1001) { 122 device_set_desc(dev, "Sun HME 10/100 Ethernet"); 123 return (0); 124 } 125 return (ENXIO); 126} 127 128int 129hme_pci_attach(device_t dev) 130{ 131 struct hme_pci_softc *hsc = device_get_softc(dev); 132 struct hme_softc *sc = &hsc->hsc_hme; 133 int error; 134 135 pci_enable_busmaster(dev); 136 /* 137 * Some Sun HMEs do have their intpin register bogusly set to 0, 138 * although it should be 1. correct that. 139 */ 140 if (pci_get_intpin(dev) == 0) 141 pci_set_intpin(dev, 1); 142 143 sc->sc_pci = 1; 144 sc->sc_dev = dev; 145 146 /* 147 * Map five register banks: 148 * 149 * bank 0: HME SEB registers: +0x0000 150 * bank 1: HME ETX registers: +0x2000 151 * bank 2: HME ERX registers: +0x4000 152 * bank 3: HME MAC registers: +0x6000 153 * bank 4: HME MIF registers: +0x7000 154 * 155 */ 156 hsc->hsc_srid = PCI_HME_BASEADDR; 157 hsc->hsc_sres = bus_alloc_resource(dev, SYS_RES_MEMORY, &hsc->hsc_srid, 158 0, ~0, 1, RF_ACTIVE); 159 if (hsc->hsc_sres == NULL) { 160 device_printf(dev, "could not map device registers\n"); 161 return (ENXIO); 162 } 163 hsc->hsc_irid = 0; 164 hsc->hsc_ires = bus_alloc_resource(dev, SYS_RES_IRQ, &hsc->hsc_irid, 0, 165 ~0, 1, RF_SHAREABLE | RF_ACTIVE); 166 if (hsc->hsc_ires == NULL) { 167 device_printf(dev, "could not allocate interrupt\n"); 168 error = ENXIO; 169 goto fail_sres; 170 } 171 sc->sc_sebt = sc->sc_etxt = sc->sc_erxt = sc->sc_mact = sc->sc_mift = 172 rman_get_bustag(hsc->hsc_sres); 173 sc->sc_sebh = sc->sc_etxh = sc->sc_erxh = sc->sc_mach = sc->sc_mifh = 174 rman_get_bushandle(hsc->hsc_sres); 175 sc->sc_sebo = 0; 176 sc->sc_etxo = 0x2000; 177 sc->sc_erxo = 0x4000; 178 sc->sc_maco = 0x6000; 179 sc->sc_mifo = 0x7000; 180 181 OF_getetheraddr(dev, sc->sc_arpcom.ac_enaddr); 182 183 sc->sc_burst = 64; /* XXX */ 184 185 /* 186 * call the main configure 187 */ 188 if ((error = hme_config(sc)) != 0) { 189 device_printf(dev, "could not be configured\n"); 190 goto fail_ires; 191 } 192 193 if ((error = bus_setup_intr(dev, hsc->hsc_ires, INTR_TYPE_NET, hme_intr, 194 sc, &hsc->hsc_ih)) != 0) { 195 device_printf(dev, "couldn't establish interrupt\n"); 196 hme_detach(sc); 197 goto fail_ires; 198 } 199 return (0); 200 201fail_ires: 202 bus_release_resource(dev, SYS_RES_IRQ, hsc->hsc_irid, hsc->hsc_ires); 203fail_sres: 204 bus_release_resource(dev, SYS_RES_MEMORY, hsc->hsc_srid, hsc->hsc_sres); 205 return (ENXIO); 206} 207 208static int 209hme_pci_detach(device_t dev) 210{ 211 struct hme_pci_softc *hsc = device_get_softc(dev); 212 struct hme_softc *sc = &hsc->hsc_hme; 213 214 hme_detach(sc); 215 216 bus_teardown_intr(dev, hsc->hsc_ires, hsc->hsc_ih); 217 bus_release_resource(dev, SYS_RES_IRQ, hsc->hsc_irid, hsc->hsc_ires); 218 bus_release_resource(dev, SYS_RES_MEMORY, hsc->hsc_srid, hsc->hsc_sres); 219 return (0); 220} 221 222static int 223hme_pci_suspend(device_t dev) 224{ 225 struct hme_pci_softc *hsc = device_get_softc(dev); 226 struct hme_softc *sc = &hsc->hsc_hme; 227 228 hme_suspend(sc); 229 return (0); 230} 231 232static int 233hme_pci_resume(device_t dev) 234{ 235 struct hme_pci_softc *hsc = device_get_softc(dev); 236 struct hme_softc *sc = &hsc->hsc_hme; 237 238 hme_resume(sc); 239 return (0); 240} 241