if_hme_sbus.c revision 119338
1105828Srwatson/*-
2184407Srwatson * Copyright (c) 1999 The NetBSD Foundation, Inc.
3126097Srwatson * All rights reserved.
4172930Srwatson *
5182063Srwatson * This code is derived from software contributed to The NetBSD Foundation
6105828Srwatson * by Paul Kranenburg.
7105828Srwatson *
8105828Srwatson * Redistribution and use in source and binary forms, with or without
9105828Srwatson * modification, are permitted provided that the following conditions
10106393Srwatson * are met:
11106393Srwatson * 1. Redistributions of source code must retain the above copyright
12106393Srwatson *    notice, this list of conditions and the following disclaimer.
13106393Srwatson * 2. Redistributions in binary form must reproduce the above copyright
14105828Srwatson *    notice, this list of conditions and the following disclaimer in the
15172930Srwatson *    documentation and/or other materials provided with the distribution.
16172930Srwatson * 3. All advertising materials mentioning features or use of this software
17172930Srwatson *    must display the following acknowledgement:
18105828Srwatson *        This product includes software developed by the NetBSD
19105828Srwatson *        Foundation, Inc. and its contributors.
20105828Srwatson * 4. Neither the name of The NetBSD Foundation nor the names of its
21105828Srwatson *    contributors may be used to endorse or promote products derived
22105828Srwatson *    from this software without specific prior written permission.
23105828Srwatson *
24105828Srwatson * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
25105828Srwatson * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
26105828Srwatson * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
27105828Srwatson * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
28105828Srwatson * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
29105828Srwatson * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
30105828Srwatson * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
31105828Srwatson * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
32105828Srwatson * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
33105828Srwatson * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
34105828Srwatson * POSSIBILITY OF SUCH DAMAGE.
35105828Srwatson *
36105828Srwatson *	from: NetBSD: if_hme_sbus.c,v 1.9 2001/11/13 06:58:17 lukem Exp
37105828Srwatson *
38105828Srwatson * $FreeBSD: head/sys/dev/hme/if_hme_sbus.c 119338 2003-08-23 00:11:16Z imp $
39105828Srwatson */
40105828Srwatson
41105828Srwatson/*
42105828Srwatson * SBus front-end device driver for the HME ethernet device.
43105828Srwatson */
44172955Srwatson
45105828Srwatson#include <sys/param.h>
46105828Srwatson#include <sys/systm.h>
47105828Srwatson#include <sys/bus.h>
48105828Srwatson#include <sys/kernel.h>
49105828Srwatson#include <sys/resource.h>
50166905Srwatson#include <sys/socket.h>
51164033Srwatson
52105828Srwatson#include <machine/bus.h>
53116701Srwatson#include <machine/ofw_machdep.h>
54183980Sbz#include <machine/resource.h>
55183970Sbz
56105828Srwatson#include <sys/rman.h>
57105828Srwatson
58105828Srwatson#include <dev/ofw/openfirm.h>
59183980Sbz
60183980Sbz#include <net/ethernet.h>
61183980Sbz#include <net/if.h>
62183980Sbz#include <net/if_arp.h>
63165469Srwatson#include <net/if_dl.h>
64105828Srwatson#include <net/if_media.h>
65105828Srwatson
66105828Srwatson#include <mii/mii.h>
67105828Srwatson#include <mii/miivar.h>
68105828Srwatson
69105828Srwatson#include <sparc64/sbus/sbusvar.h>
70105828Srwatson
71181213Srwatson#include <hme/if_hmereg.h>
72105828Srwatson#include <hme/if_hmevar.h>
73181213Srwatson
74105828Srwatson#include "miibus_if.h"
75105828Srwatson
76166533Srwatsonstruct hme_sbus_softc {
77166533Srwatson	struct	hme_softc	hsc_hme;	/* HME device */
78105828Srwatson	struct	resource	*hsc_seb_res;
79173138Srwatson	int			hsc_seb_rid;
80184401Srwatson	struct	resource	*hsc_etx_res;
81105828Srwatson	int			hsc_etx_rid;
82105828Srwatson	struct	resource	*hsc_erx_res;
83181213Srwatson	int			hsc_erx_rid;
84173138Srwatson	struct	resource	*hsc_mac_res;
85105828Srwatson	int			hsc_mac_rid;
86182063Srwatson	struct	resource	*hsc_mif_res;
87182063Srwatson	int			hsc_mif_rid;
88182063Srwatson	struct	resource	*hsc_ires;
89173138Srwatson	int			hsc_irid;
90173138Srwatson	void			*hsc_ih;
91105828Srwatson};
92182063Srwatson
93182063Srwatsonstatic int hme_sbus_probe(device_t);
94182063Srwatsonstatic int hme_sbus_attach(device_t);
95182063Srwatsonstatic int hme_sbus_detach(device_t);
96182063Srwatsonstatic int hme_sbus_suspend(device_t);
97182063Srwatsonstatic int hme_sbus_resume(device_t);
98182063Srwatson
99182063Srwatsonstatic device_method_t hme_sbus_methods[] = {
100173138Srwatson	/* Device interface */
101173138Srwatson	DEVMETHOD(device_probe,		hme_sbus_probe),
102105828Srwatson	DEVMETHOD(device_attach,	hme_sbus_attach),
103173138Srwatson	DEVMETHOD(device_detach,	hme_sbus_detach),
104123173Srwatson	DEVMETHOD(device_suspend,	hme_sbus_suspend),
105123173Srwatson	DEVMETHOD(device_resume,	hme_sbus_resume),
106173138Srwatson	/* Can just use the suspend method here. */
107173138Srwatson	DEVMETHOD(device_shutdown,	hme_sbus_suspend),
108173138Srwatson
109173138Srwatson	/* bus interface */
110105828Srwatson	DEVMETHOD(bus_print_child,	bus_generic_print_child),
111173138Srwatson	DEVMETHOD(bus_driver_added,	bus_generic_driver_added),
112105828Srwatson
113173138Srwatson	/* MII interface */
114105828Srwatson	DEVMETHOD(miibus_readreg,	hme_mii_readreg),
115173138Srwatson	DEVMETHOD(miibus_writereg,	hme_mii_writereg),
116105828Srwatson	DEVMETHOD(miibus_statchg,	hme_mii_statchg),
117184402Srwatson
118184402Srwatson	{ 0, 0 }
119184402Srwatson};
120184402Srwatson
121184402Srwatsonstatic driver_t hme_sbus_driver = {
122184402Srwatson	"hme",
123184402Srwatson	hme_sbus_methods,
124173138Srwatson	sizeof(struct hme_sbus_softc)
125173138Srwatson};
126173138Srwatson
127173138SrwatsonDRIVER_MODULE(hme, sbus, hme_sbus_driver, hme_devclass, 0, 0);
128173138SrwatsonMODULE_DEPEND(hme, ether, 1, 1, 1);
129173138Srwatson
130173138Srwatsonstatic int
131173138Srwatsonhme_sbus_probe(device_t dev)
132173138Srwatson{
133116701Srwatson	char *name;
134173138Srwatson
135105828Srwatson	name = sbus_get_name(dev);
136105828Srwatson	if (strcmp(name, "SUNW,qfe") == 0 ||
137105828Srwatson	    strcmp(name, "SUNW,hme") == 0) {
138173138Srwatson		device_set_desc(dev, "Sun HME 10/100 Ethernet");
139105828Srwatson		return (0);
140173138Srwatson	}
141105828Srwatson	return (ENXIO);
142184401Srwatson}
143105828Srwatson
144173138Srwatsonstatic int
145105828Srwatsonhme_sbus_attach(device_t dev)
146105828Srwatson{
147105828Srwatson	struct hme_sbus_softc *hsc = device_get_softc(dev);
148173138Srwatson	struct hme_softc *sc = &hsc->hsc_hme;
149105828Srwatson	u_int32_t burst;
150105828Srwatson	u_long start, count;
151182063Srwatson	int error;
152182063Srwatson
153182063Srwatson	/*
154182063Srwatson	 * Map five register banks:
155105828Srwatson	 *
156105828Srwatson	 *	bank 0: HME SEB registers
157105828Srwatson	 *	bank 1: HME ETX registers
158184407Srwatson	 *	bank 2: HME ERX registers
159184407Srwatson	 *	bank 3: HME MAC registers
160184407Srwatson	 *	bank 4: HME MIF registers
161184407Srwatson	 *
162184407Srwatson	 */
163184407Srwatson	sc->sc_sebo = sc->sc_etxo = sc->sc_erxo = sc->sc_maco = sc->sc_mifo = 0;
164184407Srwatson	hsc->hsc_seb_rid = 0;
165184407Srwatson	hsc->hsc_seb_res = bus_alloc_resource(dev, SYS_RES_MEMORY,
166184407Srwatson	    &hsc->hsc_seb_rid, 0, ~0, 1, RF_ACTIVE);
167184407Srwatson	if (hsc->hsc_seb_res == NULL) {
168184407Srwatson		device_printf(dev, "cannot map SEB registers\n");
169184407Srwatson		return (ENXIO);
170184407Srwatson	}
171184407Srwatson	sc->sc_sebt = rman_get_bustag(hsc->hsc_seb_res);
172173138Srwatson	sc->sc_sebh = rman_get_bushandle(hsc->hsc_seb_res);
173105828Srwatson
174105828Srwatson	hsc->hsc_etx_rid = 1;
175173138Srwatson	hsc->hsc_etx_res = bus_alloc_resource(dev, SYS_RES_MEMORY,
176105828Srwatson	    &hsc->hsc_etx_rid, 0, ~0, 1, RF_ACTIVE);
177105828Srwatson	if (hsc->hsc_etx_res == NULL) {
178105828Srwatson		device_printf(dev, "cannot map ETX registers\n");
179173138Srwatson		goto fail_seb_res;
180173138Srwatson	}
181105828Srwatson	sc->sc_etxt = rman_get_bustag(hsc->hsc_etx_res);
182105828Srwatson	sc->sc_etxh = rman_get_bushandle(hsc->hsc_etx_res);
183173138Srwatson
184105828Srwatson	hsc->hsc_erx_rid = 2;
185105828Srwatson	hsc->hsc_erx_res = bus_alloc_resource(dev, SYS_RES_MEMORY,
186173138Srwatson	    &hsc->hsc_erx_rid, 0, ~0, 1, RF_ACTIVE);
187105828Srwatson	if (hsc->hsc_erx_res == NULL) {
188182063Srwatson		device_printf(dev, "cannot map ERX registers\n");
189182063Srwatson		goto fail_etx_res;
190182063Srwatson	}
191182063Srwatson	sc->sc_erxt = rman_get_bustag(hsc->hsc_erx_res);
192182063Srwatson	sc->sc_erxh = rman_get_bushandle(hsc->hsc_erx_res);
193182063Srwatson
194182063Srwatson	hsc->hsc_mac_rid = 3;
195182063Srwatson	hsc->hsc_mac_res = bus_alloc_resource(dev, SYS_RES_MEMORY,
196173138Srwatson	    &hsc->hsc_mac_rid, 0, ~0, 1, RF_ACTIVE);
197105828Srwatson	if (hsc->hsc_mac_res == NULL) {
198173138Srwatson		device_printf(dev, "cannot map MAC registers\n");
199173138Srwatson		goto fail_erx_res;
200173138Srwatson	}
201173138Srwatson	sc->sc_mact = rman_get_bustag(hsc->hsc_mac_res);
202173138Srwatson	sc->sc_mach = rman_get_bushandle(hsc->hsc_mac_res);
203105828Srwatson
204105828Srwatson	/*
205105828Srwatson	 * At least on some HMEs, the MIF registers seem to be inside the MAC
206173138Srwatson	 * range, so map try to kluge around it.
207173138Srwatson	 */
208105828Srwatson	hsc->hsc_mif_rid = 4;
209105828Srwatson	hsc->hsc_mif_res = bus_alloc_resource(dev, SYS_RES_MEMORY,
210173138Srwatson	    &hsc->hsc_mif_rid, 0, ~0, 1, RF_ACTIVE);
211173138Srwatson	if (hsc->hsc_mif_res == NULL) {
212105828Srwatson		if (bus_get_resource(dev, SYS_RES_MEMORY, hsc->hsc_mif_rid,
213173138Srwatson		    &start, &count) != 0) {
214173138Srwatson			device_printf(dev, "cannot get MIF registers\n");
215173138Srwatson			goto fail_mac_res;
216105828Srwatson		}
217105828Srwatson		if (start < rman_get_start(hsc->hsc_mac_res) ||
218173138Srwatson		    start + count - 1 > rman_get_end(hsc->hsc_mac_res)) {
219173138Srwatson			device_printf(dev, "cannot move MIF registers to MAC "
220105828Srwatson			    "bank\n");
221105828Srwatson			goto fail_mac_res;
222182063Srwatson		}
223173138Srwatson		sc->sc_mift = sc->sc_mact;
224105828Srwatson		sc->sc_mifh = sc->sc_mach;
225105828Srwatson		sc->sc_mifo = sc->sc_maco + start -
226105828Srwatson		    rman_get_start(hsc->hsc_mac_res);
227183980Sbz	} else {
228183980Sbz		sc->sc_mift = rman_get_bustag(hsc->hsc_mif_res);
229183980Sbz		sc->sc_mifh = rman_get_bushandle(hsc->hsc_mif_res);
230183980Sbz	}
231183980Sbz
232184401Srwatson	hsc->hsc_irid = 0;
233183980Sbz	hsc->hsc_ires = bus_alloc_resource(dev, SYS_RES_IRQ, &hsc->hsc_irid, 0,
234183980Sbz	    ~0, 1, RF_SHAREABLE | RF_ACTIVE);
235183980Sbz	if (hsc->hsc_ires == NULL) {
236183980Sbz		device_printf(dev, "could not allocate interrupt\n");
237183980Sbz		error = ENXIO;
238172955Srwatson		goto fail_mif_res;
239105828Srwatson	}
240105828Srwatson
241105828Srwatson
242184401Srwatson	OF_getetheraddr(dev, sc->sc_arpcom.ac_enaddr);
243105828Srwatson
244105828Srwatson	burst = sbus_get_burstsz(dev);
245105828Srwatson	/* Translate into plain numerical format */
246105828Srwatson	sc->sc_burst =  (burst & SBUS_BURST_32) ? 32 :
247105828Srwatson	    (burst & SBUS_BURST_16) ? 16 : 0;
248172955Srwatson
249105828Srwatson	sc->sc_pci = 0;	/* XXX: should all be done in bus_dma. */
250105828Srwatson	sc->sc_dev = dev;
251105828Srwatson
252184401Srwatson	if ((error = hme_config(sc)) != 0) {
253105828Srwatson		device_printf(dev, "could not be configured\n");
254105828Srwatson		goto fail_ires;
255105828Srwatson	}
256105828Srwatson
257105828Srwatson
258172955Srwatson	if ((error = bus_setup_intr(dev, hsc->hsc_ires, INTR_TYPE_NET, hme_intr,
259105828Srwatson	     sc, &hsc->hsc_ih)) != 0) {
260105828Srwatson		device_printf(dev, "couldn't establish interrupt\n");
261105828Srwatson		hme_detach(sc);
262105828Srwatson		goto fail_ires;
263184401Srwatson	}
264105828Srwatson	return (0);
265105828Srwatson
266105828Srwatsonfail_ires:
267105828Srwatson	bus_release_resource(dev, SYS_RES_IRQ, hsc->hsc_irid, hsc->hsc_ires);
268105828Srwatsonfail_mif_res:
269172955Srwatson	if (hsc->hsc_mif_res != NULL) {
270168976Srwatson		bus_release_resource(dev, SYS_RES_MEMORY, hsc->hsc_mif_rid,
271105828Srwatson		    hsc->hsc_mif_res);
272105828Srwatson	}
273105828Srwatsonfail_mac_res:
274184401Srwatson	bus_release_resource(dev, SYS_RES_MEMORY, hsc->hsc_mac_rid,
275105828Srwatson	    hsc->hsc_mac_res);
276105828Srwatsonfail_erx_res:
277105828Srwatson	bus_release_resource(dev, SYS_RES_MEMORY, hsc->hsc_erx_rid,
278105828Srwatson	    hsc->hsc_erx_res);
279106648Srwatsonfail_etx_res:
280172955Srwatson	bus_release_resource(dev, SYS_RES_MEMORY, hsc->hsc_etx_rid,
281168976Srwatson	    hsc->hsc_etx_res);
282168976Srwatsonfail_seb_res:
283106648Srwatson	bus_release_resource(dev, SYS_RES_MEMORY, hsc->hsc_seb_rid,
284106648Srwatson	    hsc->hsc_seb_res);
285106648Srwatson	return (ENXIO);
286106648Srwatson}
287106648Srwatson
288106648Srwatsonstatic int
289106648Srwatsonhme_sbus_detach(device_t dev)
290106648Srwatson{
291106648Srwatson	struct hme_sbus_softc *hsc = device_get_softc(dev);
292106648Srwatson	struct hme_softc *sc = &hsc->hsc_hme;
293106648Srwatson
294106648Srwatson	hme_detach(sc);
295106648Srwatson
296106648Srwatson	bus_teardown_intr(dev, hsc->hsc_ires, hsc->hsc_ih);
297106648Srwatson	if (hsc->hsc_mif_res != NULL) {
298172955Srwatson		bus_release_resource(dev, SYS_RES_MEMORY, hsc->hsc_mif_rid,
299105828Srwatson		    hsc->hsc_mif_res);
300172955Srwatson	}
301172955Srwatson	bus_release_resource(dev, SYS_RES_MEMORY, hsc->hsc_mac_rid,
302173138Srwatson	    hsc->hsc_mac_res);
303184407Srwatson	bus_release_resource(dev, SYS_RES_MEMORY, hsc->hsc_erx_rid,
304184407Srwatson	    hsc->hsc_erx_res);
305173138Srwatson	bus_release_resource(dev, SYS_RES_MEMORY, hsc->hsc_etx_rid,
306173138Srwatson	    hsc->hsc_etx_res);
307173138Srwatson	bus_release_resource(dev, SYS_RES_MEMORY, hsc->hsc_seb_rid,
308173138Srwatson	    hsc->hsc_seb_res);
309173138Srwatson	return (0);
310183980Sbz}
311172955Srwatson
312172955Srwatsonstatic int
313172955Srwatsonhme_sbus_suspend(device_t dev)
314172955Srwatson{
315172955Srwatson	struct hme_sbus_softc *hsc = device_get_softc(dev);
316105828Srwatson	struct hme_softc *sc = &hsc->hsc_hme;
317105828Srwatson
318172955Srwatson	hme_suspend(sc);
319182063Srwatson	return (0);
320}
321
322static int
323hme_sbus_resume(device_t dev)
324{
325	struct hme_sbus_softc *hsc = device_get_softc(dev);
326	struct hme_softc *sc = &hsc->hsc_hme;
327
328	hme_resume(sc);
329	return (0);
330}
331