11590Srgrimes/*-
21590Srgrimes * Copyright (C) 2001 Eduardo Horvath.
31590Srgrimes * Copyright (c) 2007 Marius Strobl <marius@FreeBSD.org>
41590Srgrimes * All rights reserved.
51590Srgrimes *
61590Srgrimes *
71590Srgrimes * Redistribution and use in source and binary forms, with or without
81590Srgrimes * modification, are permitted provided that the following conditions
91590Srgrimes * are met:
101590Srgrimes * 1. Redistributions of source code must retain the above copyright
111590Srgrimes *    notice, this list of conditions and the following disclaimer.
121590Srgrimes * 2. Redistributions in binary form must reproduce the above copyright
131590Srgrimes *    notice, this list of conditions and the following disclaimer in the
141590Srgrimes *    documentation and/or other materials provided with the distribution.
151590Srgrimes *
161590Srgrimes * THIS SOFTWARE IS PROVIDED BY THE AUTHOR  ``AS IS'' AND
171590Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
181590Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
191590Srgrimes * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR  BE LIABLE
201590Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
211590Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
221590Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
231590Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
241590Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
251590Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
261590Srgrimes * SUCH DAMAGE.
271590Srgrimes *
281590Srgrimes *	from: NetBSD: if_gem_pci.c,v 1.7 2001/10/18 15:09:15 thorpej Exp
291590Srgrimes */
301590Srgrimes
311590Srgrimes#include <sys/cdefs.h>
321590Srgrimes__FBSDID("$FreeBSD: releng/11.0/sys/dev/gem/if_gem_sbus.c 227843 2011-11-22 21:28:20Z marius $");
331590Srgrimes
341590Srgrimes/*
351590Srgrimes * SBus bindings for Sun GEM Ethernet controllers
361590Srgrimes */
3727419Scharnier
381590Srgrimes#include <sys/param.h>
391590Srgrimes#include <sys/systm.h>
401590Srgrimes#include <sys/bus.h>
411590Srgrimes#include <sys/kernel.h>
421590Srgrimes#include <sys/lock.h>
431590Srgrimes#include <sys/module.h>
44116390Scharnier#include <sys/mutex.h>
451590Srgrimes#include <sys/resource.h>
461590Srgrimes#include <sys/rman.h>
47116390Scharnier#include <sys/socket.h>
4827419Scharnier
49116390Scharnier#include <net/ethernet.h>
5099112Sobrien#include <net/if.h>
5199112Sobrien
521590Srgrimes#include <dev/ofw/ofw_bus.h>
531590Srgrimes
5427419Scharnier#include <machine/bus.h>
551590Srgrimes#include <machine/ofw_machdep.h>
561590Srgrimes#include <machine/resource.h>
571590Srgrimes
581590Srgrimes#include <sparc64/sbus/sbusvar.h>
591590Srgrimes
6085632Sschweikh#include <dev/gem/if_gemreg.h>
611590Srgrimes#include <dev/gem/if_gemvar.h>
621590Srgrimes
6385632Sschweikh#include "miibus_if.h"
641590Srgrimes
6585632Sschweikhstatic device_probe_t gem_sbus_probe;
6685632Sschweikhstatic device_attach_t gem_sbus_attach;
6793440Sdwmalonestatic device_detach_t gem_sbus_detach;
681590Srgrimesstatic device_suspend_t gem_sbus_suspend;
6993440Sdwmalonestatic device_resume_t gem_sbus_resume;
701590Srgrimes
711590Srgrimesstatic device_method_t gem_sbus_methods[] = {
721590Srgrimes	/* Device interface */
73116390Scharnier	DEVMETHOD(device_probe,		gem_sbus_probe),
74116390Scharnier	DEVMETHOD(device_attach,	gem_sbus_attach),
7585632Sschweikh	DEVMETHOD(device_detach,	gem_sbus_detach),
7685632Sschweikh	DEVMETHOD(device_suspend,	gem_sbus_suspend),
771590Srgrimes	DEVMETHOD(device_resume,	gem_sbus_resume),
781590Srgrimes	/* Use the suspend handler here, it is all that is required. */
791590Srgrimes	DEVMETHOD(device_shutdown,	gem_sbus_suspend),
801590Srgrimes
811590Srgrimes	/* MII interface */
821590Srgrimes	DEVMETHOD(miibus_readreg,	gem_mii_readreg),
831590Srgrimes	DEVMETHOD(miibus_writereg,	gem_mii_writereg),
8485632Sschweikh	DEVMETHOD(miibus_statchg,	gem_mii_statchg),
851590Srgrimes
8698771Sjmallett	DEVMETHOD_END
871590Srgrimes};
881590Srgrimes
89105244Scharnierstatic driver_t gem_sbus_driver = {
901590Srgrimes	"gem",
911590Srgrimes	gem_sbus_methods,
921590Srgrimes	sizeof(struct gem_softc)
931590Srgrimes};
9493440Sdwmalone
951590SrgrimesDRIVER_MODULE(gem, sbus, gem_sbus_driver, gem_devclass, 0, 0);
961590SrgrimesMODULE_DEPEND(gem, sbus, 1, 1, 1);
971590SrgrimesMODULE_DEPEND(gem, ether, 1, 1, 1);
981590Srgrimes
991590Srgrimesstatic int
1001590Srgrimesgem_sbus_probe(device_t dev)
1011590Srgrimes{
1021590Srgrimes
1031590Srgrimes	if (strcmp(ofw_bus_get_name(dev), "network") == 0 &&
104116390Scharnier	    ofw_bus_get_compat(dev) != NULL &&
1051590Srgrimes	    strcmp(ofw_bus_get_compat(dev), "SUNW,sbus-gem") == 0) {
1061590Srgrimes		device_set_desc(dev, "Sun GEM Gigabit Ethernet");
1071590Srgrimes		return (0);
1081590Srgrimes	}
1091590Srgrimes
1101590Srgrimes	return (ENXIO);
111116390Scharnier}
112116390Scharnier
1131590Srgrimesstatic struct resource_spec gem_sbus_res_spec[] = {
114116390Scharnier	{ SYS_RES_IRQ, 0, RF_SHAREABLE | RF_ACTIVE },	/* GEM_RES_INTR */
115116390Scharnier	{ SYS_RES_MEMORY, 1, RF_ACTIVE },		/* GEM_RES_BANK1 */
1161590Srgrimes	{ SYS_RES_MEMORY, 0, RF_ACTIVE },		/* GEM_RES_BANK2 */
117116390Scharnier	{ -1, 0 }
118116390Scharnier};
1191590Srgrimes
120116390Scharnierstatic int
121116390Scharniergem_sbus_attach(device_t dev)
1221590Srgrimes{
1231590Srgrimes	struct gem_softc *sc;
1241590Srgrimes	int burst;
1251590Srgrimes	uint32_t val;
1261590Srgrimes
1271590Srgrimes	sc = device_get_softc(dev);
1281590Srgrimes	sc->sc_variant = GEM_SUN_GEM;
1291590Srgrimes	sc->sc_dev = dev;
1301590Srgrimes	/* All known SBus models use a SERDES. */
1311590Srgrimes	sc->sc_flags = GEM_SERDES;
1321590Srgrimes
1331590Srgrimes	if (bus_alloc_resources(dev, gem_sbus_res_spec, sc->sc_res)) {
1341590Srgrimes		device_printf(dev, "failed to allocate resources\n");
1351590Srgrimes		bus_release_resources(dev, gem_sbus_res_spec, sc->sc_res);
136116390Scharnier		return (ENXIO);
137116390Scharnier	}
1381590Srgrimes
1391590Srgrimes	GEM_LOCK_INIT(sc, device_get_nameunit(dev));
1401590Srgrimes
1411590Srgrimes	OF_getetheraddr(dev, sc->sc_enaddr);
1421590Srgrimes
1431590Srgrimes	burst = sbus_get_burstsz(dev);
1441590Srgrimes	val = GEM_SBUS_CFG_PARITY;
1451590Srgrimes	if ((burst & SBUS_BURST64_MASK) != 0) {
1461590Srgrimes		val |= GEM_SBUS_CFG_64BIT;
1471590Srgrimes		burst >>= SBUS_BURST64_SHIFT;
1481590Srgrimes	}
1491590Srgrimes	if ((burst & SBUS_BURST_64) != 0)
1501590Srgrimes		val |= GEM_SBUS_CFG_BURST_64;
1511590Srgrimes	else if ((burst & SBUS_BURST_32) != 0)
1521590Srgrimes		val |= GEM_SBUS_CFG_BURST_32;
1531590Srgrimes	else {
1541590Srgrimes		device_printf(dev, "unsupported burst size\n");
1551590Srgrimes		goto fail;
1561590Srgrimes	}
1571590Srgrimes	/* Reset the SBus interface only. */
1581590Srgrimes	(void)GEM_BANK2_READ_4(sc, GEM_SBUS_BIF_RESET);
1591590Srgrimes	DELAY(100);
1601590Srgrimes	GEM_BANK2_WRITE_4(sc, GEM_SBUS_CONFIG, val);
1611590Srgrimes
1621590Srgrimes	if (gem_attach(sc) != 0) {
1631590Srgrimes		device_printf(dev, "could not be attached\n");
1641590Srgrimes		goto fail;
1651590Srgrimes	}
1661590Srgrimes
1671590Srgrimes	if (bus_setup_intr(dev, sc->sc_res[GEM_RES_INTR], INTR_TYPE_NET |
1681590Srgrimes	    INTR_MPSAFE, NULL, gem_intr, sc, &sc->sc_ih) != 0) {
1691590Srgrimes		device_printf(dev, "failed to set up interrupt\n");
1701590Srgrimes		gem_detach(sc);
1711590Srgrimes		goto fail;
1721590Srgrimes	}
1731590Srgrimes	return (0);
1741590Srgrimes
1751590Srgrimes fail:
1761590Srgrimes	GEM_LOCK_DESTROY(sc);
1771590Srgrimes	bus_release_resources(dev, gem_sbus_res_spec, sc->sc_res);
17869795Sobrien	return (ENXIO);
1791590Srgrimes}
1801590Srgrimes
1811590Srgrimesstatic int
1821590Srgrimesgem_sbus_detach(device_t dev)
1831590Srgrimes{
1841590Srgrimes	struct gem_softc *sc;
1851590Srgrimes
1861590Srgrimes	sc = device_get_softc(dev);
1871590Srgrimes	bus_teardown_intr(dev, sc->sc_res[GEM_RES_INTR], sc->sc_ih);
1881590Srgrimes	gem_detach(sc);
1891590Srgrimes	GEM_LOCK_DESTROY(sc);
1901590Srgrimes	bus_release_resources(dev, gem_sbus_res_spec, sc->sc_res);
1911590Srgrimes	return (0);
1921590Srgrimes}
1931590Srgrimes
1941590Srgrimesstatic int
1951590Srgrimesgem_sbus_suspend(device_t dev)
1961590Srgrimes{
1971590Srgrimes
1981590Srgrimes	gem_suspend(device_get_softc(dev));
1991590Srgrimes	return (0);
2001590Srgrimes}
2011590Srgrimes
2021590Srgrimesstatic int
20362894Skrisgem_sbus_resume(device_t dev)
2041590Srgrimes{
2051590Srgrimes
2061590Srgrimes	gem_resume(device_get_softc(dev));
2071590Srgrimes	return (0);
2081590Srgrimes}
2091590Srgrimes