if_hme_sbus.c revision 108976
191396Stmm/*- 291396Stmm * Copyright (c) 1999 The NetBSD Foundation, Inc. 391396Stmm * All rights reserved. 491396Stmm * 591396Stmm * This code is derived from software contributed to The NetBSD Foundation 691396Stmm * by Paul Kranenburg. 791396Stmm * 891396Stmm * Redistribution and use in source and binary forms, with or without 991396Stmm * modification, are permitted provided that the following conditions 1091396Stmm * are met: 1191396Stmm * 1. Redistributions of source code must retain the above copyright 1291396Stmm * notice, this list of conditions and the following disclaimer. 1391396Stmm * 2. Redistributions in binary form must reproduce the above copyright 1491396Stmm * notice, this list of conditions and the following disclaimer in the 1591396Stmm * documentation and/or other materials provided with the distribution. 1691396Stmm * 3. All advertising materials mentioning features or use of this software 1791396Stmm * must display the following acknowledgement: 1891396Stmm * This product includes software developed by the NetBSD 1991396Stmm * Foundation, Inc. and its contributors. 2091396Stmm * 4. Neither the name of The NetBSD Foundation nor the names of its 2191396Stmm * contributors may be used to endorse or promote products derived 2291396Stmm * from this software without specific prior written permission. 2391396Stmm * 2491396Stmm * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 2591396Stmm * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 2691396Stmm * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 2791396Stmm * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 2891396Stmm * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 2991396Stmm * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 3091396Stmm * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 3191396Stmm * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 3291396Stmm * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 3391396Stmm * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 3491396Stmm * POSSIBILITY OF SUCH DAMAGE. 3591396Stmm * 3691396Stmm * from: NetBSD: if_hme_sbus.c,v 1.9 2001/11/13 06:58:17 lukem Exp 3791396Stmm * 3891396Stmm * $FreeBSD: head/sys/dev/hme/if_hme_sbus.c 108976 2003-01-09 00:45:10Z tmm $ 3991396Stmm */ 4091396Stmm 4191396Stmm/* 4291396Stmm * SBus front-end device driver for the HME ethernet device. 4391396Stmm */ 4491396Stmm 4591396Stmm#include <sys/param.h> 4691396Stmm#include <sys/systm.h> 4791396Stmm#include <sys/bus.h> 4891396Stmm#include <sys/kernel.h> 4991396Stmm#include <sys/resource.h> 5091396Stmm#include <sys/socket.h> 5191396Stmm 5291396Stmm#include <machine/bus.h> 5391396Stmm#include <machine/ofw_machdep.h> 5491396Stmm#include <machine/resource.h> 5591396Stmm 5691396Stmm#include <sys/rman.h> 5791396Stmm 5891396Stmm#include <ofw/openfirm.h> 5991396Stmm 6091396Stmm#include <net/ethernet.h> 6191396Stmm#include <net/if.h> 6291396Stmm#include <net/if_arp.h> 6391396Stmm#include <net/if_dl.h> 6491396Stmm#include <net/if_media.h> 6591396Stmm 6691396Stmm#include <mii/mii.h> 6791396Stmm#include <mii/miivar.h> 6891396Stmm 6991396Stmm#include <sparc64/sbus/sbusvar.h> 7091396Stmm 7191396Stmm#include <hme/if_hmereg.h> 7291396Stmm#include <hme/if_hmevar.h> 7391396Stmm 7491396Stmm#include "miibus_if.h" 7591396Stmm 7691396Stmmstruct hme_sbus_softc { 7791396Stmm struct hme_softc hsc_hme; /* HME device */ 7891396Stmm struct resource *hsc_seb_res; 7991396Stmm int hsc_seb_rid; 8091396Stmm struct resource *hsc_etx_res; 8191396Stmm int hsc_etx_rid; 8291396Stmm struct resource *hsc_erx_res; 8391396Stmm int hsc_erx_rid; 8491396Stmm struct resource *hsc_mac_res; 8591396Stmm int hsc_mac_rid; 8691396Stmm struct resource *hsc_mif_res; 8791396Stmm int hsc_mif_rid; 8891396Stmm struct resource *hsc_ires; 8991396Stmm int hsc_irid; 9091396Stmm void *hsc_ih; 9191396Stmm}; 9291396Stmm 9391396Stmmstatic int hme_sbus_probe(device_t); 9491396Stmmstatic int hme_sbus_attach(device_t); 95108976Stmmstatic int hme_sbus_detach(device_t); 96108976Stmmstatic int hme_sbus_suspend(device_t); 97108976Stmmstatic int hme_sbus_resume(device_t); 9891396Stmm 9991396Stmmstatic device_method_t hme_sbus_methods[] = { 10091396Stmm /* Device interface */ 10191396Stmm DEVMETHOD(device_probe, hme_sbus_probe), 10291396Stmm DEVMETHOD(device_attach, hme_sbus_attach), 103108976Stmm DEVMETHOD(device_detach, hme_sbus_detach), 104108976Stmm DEVMETHOD(device_suspend, hme_sbus_suspend), 105108976Stmm DEVMETHOD(device_resume, hme_sbus_resume), 106108976Stmm /* Can just use the suspend method here. */ 107108976Stmm DEVMETHOD(device_shutdown, hme_sbus_suspend), 10891396Stmm 10991396Stmm /* bus interface */ 11091396Stmm DEVMETHOD(bus_print_child, bus_generic_print_child), 11191396Stmm DEVMETHOD(bus_driver_added, bus_generic_driver_added), 11291396Stmm 11391396Stmm /* MII interface */ 11491396Stmm DEVMETHOD(miibus_readreg, hme_mii_readreg), 11591396Stmm DEVMETHOD(miibus_writereg, hme_mii_writereg), 11691396Stmm DEVMETHOD(miibus_statchg, hme_mii_statchg), 11791396Stmm 11891396Stmm { 0, 0 } 11991396Stmm}; 12091396Stmm 12191396Stmmstatic driver_t hme_sbus_driver = { 12291396Stmm "hme", 12391396Stmm hme_sbus_methods, 12491396Stmm sizeof(struct hme_sbus_softc) 12591396Stmm}; 12691396Stmm 12791396StmmDRIVER_MODULE(if_hme, sbus, hme_sbus_driver, hme_devclass, 0, 0); 12891396Stmm 12991396Stmmstatic int 13091396Stmmhme_sbus_probe(device_t dev) 13191396Stmm{ 13291396Stmm char *name; 13391396Stmm 13491396Stmm name = sbus_get_name(dev); 13591396Stmm if (strcmp(name, "SUNW,qfe") == 0 || 13691396Stmm strcmp(name, "SUNW,hme") == 0) { 13791396Stmm device_set_desc(dev, "Sun HME 10/100 Ethernet"); 13891396Stmm return (0); 13991396Stmm } 14091396Stmm return (ENXIO); 14191396Stmm} 14291396Stmm 14391396Stmmstatic int 14491396Stmmhme_sbus_attach(device_t dev) 14591396Stmm{ 14691396Stmm struct hme_sbus_softc *hsc = device_get_softc(dev); 14791396Stmm struct hme_softc *sc = &hsc->hsc_hme; 14891396Stmm u_int32_t burst; 14991396Stmm u_long start, count; 15091396Stmm int error; 15191396Stmm 15291396Stmm /* 15391396Stmm * Map five register banks: 15491396Stmm * 15591396Stmm * bank 0: HME SEB registers 15691396Stmm * bank 1: HME ETX registers 15791396Stmm * bank 2: HME ERX registers 15891396Stmm * bank 3: HME MAC registers 15991396Stmm * bank 4: HME MIF registers 16091396Stmm * 16191396Stmm */ 16291396Stmm sc->sc_sebo = sc->sc_etxo = sc->sc_erxo = sc->sc_maco = sc->sc_mifo = 0; 16391396Stmm hsc->hsc_seb_rid = 0; 16491396Stmm hsc->hsc_seb_res = bus_alloc_resource(dev, SYS_RES_MEMORY, 16591396Stmm &hsc->hsc_seb_rid, 0, ~0, 1, RF_ACTIVE); 16691396Stmm if (hsc->hsc_seb_res == NULL) { 16791396Stmm device_printf(dev, "cannot map SEB registers\n"); 16891396Stmm return (ENXIO); 16991396Stmm } 17091396Stmm sc->sc_sebt = rman_get_bustag(hsc->hsc_seb_res); 17191396Stmm sc->sc_sebh = rman_get_bushandle(hsc->hsc_seb_res); 17291396Stmm 17391396Stmm hsc->hsc_etx_rid = 1; 17491396Stmm hsc->hsc_etx_res = bus_alloc_resource(dev, SYS_RES_MEMORY, 17591396Stmm &hsc->hsc_etx_rid, 0, ~0, 1, RF_ACTIVE); 17691396Stmm if (hsc->hsc_etx_res == NULL) { 17791396Stmm device_printf(dev, "cannot map ETX registers\n"); 17891396Stmm goto fail_seb_res; 17991396Stmm } 18091396Stmm sc->sc_etxt = rman_get_bustag(hsc->hsc_etx_res); 18191396Stmm sc->sc_etxh = rman_get_bushandle(hsc->hsc_etx_res); 18291396Stmm 18391396Stmm hsc->hsc_erx_rid = 2; 18491396Stmm hsc->hsc_erx_res = bus_alloc_resource(dev, SYS_RES_MEMORY, 18591396Stmm &hsc->hsc_erx_rid, 0, ~0, 1, RF_ACTIVE); 18691396Stmm if (hsc->hsc_erx_res == NULL) { 18791396Stmm device_printf(dev, "cannot map ERX registers\n"); 18891396Stmm goto fail_etx_res; 18991396Stmm } 19091396Stmm sc->sc_erxt = rman_get_bustag(hsc->hsc_erx_res); 19191396Stmm sc->sc_erxh = rman_get_bushandle(hsc->hsc_erx_res); 19291396Stmm 19391396Stmm hsc->hsc_mac_rid = 3; 19491396Stmm hsc->hsc_mac_res = bus_alloc_resource(dev, SYS_RES_MEMORY, 19591396Stmm &hsc->hsc_mac_rid, 0, ~0, 1, RF_ACTIVE); 19691396Stmm if (hsc->hsc_mac_res == NULL) { 19791396Stmm device_printf(dev, "cannot map MAC registers\n"); 19891396Stmm goto fail_erx_res; 19991396Stmm } 20091396Stmm sc->sc_mact = rman_get_bustag(hsc->hsc_mac_res); 20191396Stmm sc->sc_mach = rman_get_bushandle(hsc->hsc_mac_res); 20291396Stmm 20391396Stmm /* 20491396Stmm * At least on some HMEs, the MIF registers seem to be inside the MAC 20591396Stmm * range, so map try to kluge around it. 20691396Stmm */ 20791396Stmm hsc->hsc_mif_rid = 4; 20891396Stmm hsc->hsc_mif_res = bus_alloc_resource(dev, SYS_RES_MEMORY, 20991396Stmm &hsc->hsc_mif_rid, 0, ~0, 1, RF_ACTIVE); 21091396Stmm if (hsc->hsc_mif_res == NULL) { 21191396Stmm if (bus_get_resource(dev, SYS_RES_MEMORY, hsc->hsc_mif_rid, 21291396Stmm &start, &count) != 0) { 21391396Stmm device_printf(dev, "cannot get MIF registers\n"); 21491396Stmm goto fail_mac_res; 21591396Stmm } 21691396Stmm if (start < rman_get_start(hsc->hsc_mac_res) || 21791396Stmm start + count - 1 > rman_get_end(hsc->hsc_mac_res)) { 21891396Stmm device_printf(dev, "cannot move MIF registers to MAC " 21991396Stmm "bank\n"); 22091396Stmm goto fail_mac_res; 22191396Stmm } 22291396Stmm sc->sc_mift = sc->sc_mact; 22391396Stmm sc->sc_mifh = sc->sc_mach; 22491396Stmm sc->sc_mifo = sc->sc_maco + start - 22591396Stmm rman_get_start(hsc->hsc_mac_res); 22691396Stmm } else { 22791396Stmm sc->sc_mift = rman_get_bustag(hsc->hsc_mif_res); 22891396Stmm sc->sc_mifh = rman_get_bushandle(hsc->hsc_mif_res); 22991396Stmm } 23091396Stmm 23191396Stmm hsc->hsc_irid = 0; 23291396Stmm hsc->hsc_ires = bus_alloc_resource(dev, SYS_RES_IRQ, &hsc->hsc_irid, 0, 23393043Stmm ~0, 1, RF_SHAREABLE | RF_ACTIVE); 23491396Stmm if (hsc->hsc_ires == NULL) { 23591396Stmm device_printf(dev, "could not allocate interrupt\n"); 23691396Stmm error = ENXIO; 23791396Stmm goto fail_mif_res; 23891396Stmm } 23991396Stmm 24091396Stmm 24191396Stmm OF_getetheraddr(dev, sc->sc_arpcom.ac_enaddr); 24291396Stmm 24391396Stmm burst = sbus_get_burstsz(dev); 24491396Stmm /* Translate into plain numerical format */ 24591396Stmm sc->sc_burst = (burst & SBUS_BURST_32) ? 32 : 24691396Stmm (burst & SBUS_BURST_16) ? 16 : 0; 24791396Stmm 24891396Stmm sc->sc_pci = 0; /* XXX: should all be done in bus_dma. */ 24991396Stmm sc->sc_dev = dev; 25091396Stmm 25191396Stmm if ((error = hme_config(sc)) != 0) { 25291396Stmm device_printf(dev, "could not be configured\n"); 25391396Stmm goto fail_ires; 25491396Stmm } 25591396Stmm 25691396Stmm 25791396Stmm if ((error = bus_setup_intr(dev, hsc->hsc_ires, INTR_TYPE_NET, hme_intr, 25891396Stmm sc, &hsc->hsc_ih)) != 0) { 25991396Stmm device_printf(dev, "couldn't establish interrupt\n"); 260108976Stmm hme_detach(sc); 26191396Stmm goto fail_ires; 26291396Stmm } 26391396Stmm return (0); 26491396Stmm 26591396Stmmfail_ires: 26691396Stmm bus_release_resource(dev, SYS_RES_IRQ, hsc->hsc_irid, hsc->hsc_ires); 26791396Stmmfail_mif_res: 26891396Stmm if (hsc->hsc_mif_res != NULL) { 26991396Stmm bus_release_resource(dev, SYS_RES_MEMORY, hsc->hsc_mif_rid, 27091396Stmm hsc->hsc_mif_res); 27191396Stmm } 27291396Stmmfail_mac_res: 27391396Stmm bus_release_resource(dev, SYS_RES_MEMORY, hsc->hsc_mac_rid, 27491396Stmm hsc->hsc_mac_res); 27591396Stmmfail_erx_res: 27691396Stmm bus_release_resource(dev, SYS_RES_MEMORY, hsc->hsc_erx_rid, 27791396Stmm hsc->hsc_erx_res); 27891396Stmmfail_etx_res: 27991396Stmm bus_release_resource(dev, SYS_RES_MEMORY, hsc->hsc_etx_rid, 28091396Stmm hsc->hsc_etx_res); 28191396Stmmfail_seb_res: 28291396Stmm bus_release_resource(dev, SYS_RES_MEMORY, hsc->hsc_seb_rid, 28391396Stmm hsc->hsc_seb_res); 28491396Stmm return (ENXIO); 28591396Stmm} 286108976Stmm 287108976Stmmstatic int 288108976Stmmhme_sbus_detach(device_t dev) 289108976Stmm{ 290108976Stmm struct hme_sbus_softc *hsc = device_get_softc(dev); 291108976Stmm struct hme_softc *sc = &hsc->hsc_hme; 292108976Stmm 293108976Stmm hme_detach(sc); 294108976Stmm 295108976Stmm bus_teardown_intr(dev, hsc->hsc_ires, hsc->hsc_ih); 296108976Stmm if (hsc->hsc_mif_res != NULL) { 297108976Stmm bus_release_resource(dev, SYS_RES_MEMORY, hsc->hsc_mif_rid, 298108976Stmm hsc->hsc_mif_res); 299108976Stmm } 300108976Stmm bus_release_resource(dev, SYS_RES_MEMORY, hsc->hsc_mac_rid, 301108976Stmm hsc->hsc_mac_res); 302108976Stmm bus_release_resource(dev, SYS_RES_MEMORY, hsc->hsc_erx_rid, 303108976Stmm hsc->hsc_erx_res); 304108976Stmm bus_release_resource(dev, SYS_RES_MEMORY, hsc->hsc_etx_rid, 305108976Stmm hsc->hsc_etx_res); 306108976Stmm bus_release_resource(dev, SYS_RES_MEMORY, hsc->hsc_seb_rid, 307108976Stmm hsc->hsc_seb_res); 308108976Stmm return (0); 309108976Stmm} 310108976Stmm 311108976Stmmstatic int 312108976Stmmhme_sbus_suspend(device_t dev) 313108976Stmm{ 314108976Stmm struct hme_sbus_softc *hsc = device_get_softc(dev); 315108976Stmm struct hme_softc *sc = &hsc->hsc_hme; 316108976Stmm 317108976Stmm hme_suspend(sc); 318108976Stmm return (0); 319108976Stmm} 320108976Stmm 321108976Stmmstatic int 322108976Stmmhme_sbus_resume(device_t dev) 323108976Stmm{ 324108976Stmm struct hme_sbus_softc *hsc = device_get_softc(dev); 325108976Stmm struct hme_softc *sc = &hsc->hsc_hme; 326108976Stmm 327108976Stmm hme_resume(sc); 328108976Stmm return (0); 329108976Stmm} 330