2 * Copyright (c) 1999 The NetBSD Foundation, Inc.
5 * This code is derived from software contributed to The NetBSD Foundation
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
18 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
19 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
20 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
21 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27 * POSSIBILITY OF SUCH DAMAGE.
29 * from: NetBSD: if_hme_sbus.c,v 1.19 2004/03/17 17:04:58 pk Exp
32 #include <sys/cdefs.h>
33 __FBSDID("$FreeBSD$");
36 * SBus front-end device driver for the HME ethernet device.
39 #include <sys/param.h>
40 #include <sys/systm.h>
42 #include <sys/kernel.h>
43 #include <sys/module.h>
44 #include <sys/resource.h>
45 #include <sys/socket.h>
47 #include <dev/ofw/ofw_bus.h>
49 #include <machine/bus.h>
50 #include <machine/ofw_machdep.h>
51 #include <machine/resource.h>
55 #include <net/ethernet.h>
57 #include <net/if_arp.h>
58 #include <net/if_dl.h>
59 #include <net/if_media.h>
61 #include <dev/mii/mii.h>
62 #include <dev/mii/miivar.h>
64 #include <sparc64/sbus/sbusvar.h>
66 #include <dev/hme/if_hmereg.h>
67 #include <dev/hme/if_hmevar.h>
69 #include "miibus_if.h"
71 struct hme_sbus_softc {
72 struct hme_softc hsc_hme; /* HME device */
73 struct resource *hsc_seb_res;
74 struct resource *hsc_etx_res;
75 struct resource *hsc_erx_res;
76 struct resource *hsc_mac_res;
77 struct resource *hsc_mif_res;
78 struct resource *hsc_ires;
82 static int hme_sbus_probe(device_t);
83 static int hme_sbus_attach(device_t);
84 static int hme_sbus_detach(device_t);
85 static int hme_sbus_suspend(device_t);
86 static int hme_sbus_resume(device_t);
88 static device_method_t hme_sbus_methods[] = {
89 /* Device interface */
90 DEVMETHOD(device_probe, hme_sbus_probe),
91 DEVMETHOD(device_attach, hme_sbus_attach),
92 DEVMETHOD(device_detach, hme_sbus_detach),
93 DEVMETHOD(device_suspend, hme_sbus_suspend),
94 DEVMETHOD(device_resume, hme_sbus_resume),
95 /* Can just use the suspend method here. */
96 DEVMETHOD(device_shutdown, hme_sbus_suspend),
99 DEVMETHOD(bus_print_child, bus_generic_print_child),
100 DEVMETHOD(bus_driver_added, bus_generic_driver_added),
103 DEVMETHOD(miibus_readreg, hme_mii_readreg),
104 DEVMETHOD(miibus_writereg, hme_mii_writereg),
105 DEVMETHOD(miibus_statchg, hme_mii_statchg),
110 static driver_t hme_sbus_driver = {
113 sizeof(struct hme_sbus_softc)
116 DRIVER_MODULE(hme, sbus, hme_sbus_driver, hme_devclass, 0, 0);
117 MODULE_DEPEND(hme, sbus, 1, 1, 1);
118 MODULE_DEPEND(hme, ether, 1, 1, 1);
121 hme_sbus_probe(device_t dev)
125 name = ofw_bus_get_name(dev);
126 if (strcmp(name, "SUNW,qfe") == 0 ||
127 strcmp(name, "SUNW,hme") == 0) {
128 device_set_desc(dev, "Sun HME 10/100 Ethernet");
135 hme_sbus_attach(device_t dev)
137 struct hme_sbus_softc *hsc;
138 struct hme_softc *sc;
143 hsc = device_get_softc(dev);
145 mtx_init(&sc->sc_lock, device_get_nameunit(dev), MTX_NETWORK_LOCK,
148 * Map five register banks:
150 * bank 0: HME SEB registers
151 * bank 1: HME ETX registers
152 * bank 2: HME ERX registers
153 * bank 3: HME MAC registers
154 * bank 4: HME MIF registers
158 hsc->hsc_seb_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY,
160 if (hsc->hsc_seb_res == NULL) {
161 device_printf(dev, "cannot map SEB registers\n");
165 sc->sc_sebt = rman_get_bustag(hsc->hsc_seb_res);
166 sc->sc_sebh = rman_get_bushandle(hsc->hsc_seb_res);
169 hsc->hsc_etx_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY,
171 if (hsc->hsc_etx_res == NULL) {
172 device_printf(dev, "cannot map ETX registers\n");
176 sc->sc_etxt = rman_get_bustag(hsc->hsc_etx_res);
177 sc->sc_etxh = rman_get_bushandle(hsc->hsc_etx_res);
180 hsc->hsc_erx_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY,
182 if (hsc->hsc_erx_res == NULL) {
183 device_printf(dev, "cannot map ERX registers\n");
187 sc->sc_erxt = rman_get_bustag(hsc->hsc_erx_res);
188 sc->sc_erxh = rman_get_bushandle(hsc->hsc_erx_res);
191 hsc->hsc_mac_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY,
193 if (hsc->hsc_mac_res == NULL) {
194 device_printf(dev, "cannot map MAC registers\n");
198 sc->sc_mact = rman_get_bustag(hsc->hsc_mac_res);
199 sc->sc_mach = rman_get_bushandle(hsc->hsc_mac_res);
202 * At least on some HMEs, the MIF registers seem to be inside the MAC
203 * range, so try to kludge around it.
206 hsc->hsc_mif_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY,
208 if (hsc->hsc_mif_res == NULL) {
209 if (bus_get_resource(dev, SYS_RES_MEMORY, i,
210 &start, &count) != 0) {
211 device_printf(dev, "cannot get MIF registers\n");
215 if (start < rman_get_start(hsc->hsc_mac_res) ||
216 start + count - 1 > rman_get_end(hsc->hsc_mac_res)) {
217 device_printf(dev, "cannot move MIF registers to MAC "
222 sc->sc_mift = sc->sc_mact;
223 bus_space_subregion(sc->sc_mact, sc->sc_mach,
224 start - rman_get_start(hsc->hsc_mac_res), count,
227 sc->sc_mift = rman_get_bustag(hsc->hsc_mif_res);
228 sc->sc_mifh = rman_get_bushandle(hsc->hsc_mif_res);
232 hsc->hsc_ires = bus_alloc_resource_any(dev, SYS_RES_IRQ,
233 &i, RF_SHAREABLE | RF_ACTIVE);
234 if (hsc->hsc_ires == NULL) {
235 device_printf(dev, "could not allocate interrupt\n");
240 OF_getetheraddr(dev, sc->sc_enaddr);
242 burst = sbus_get_burstsz(dev);
243 /* Translate into plain numerical format */
244 if ((burst & SBUS_BURST_64))
246 else if ((burst & SBUS_BURST_32))
248 else if ((burst & SBUS_BURST_16))
256 if ((error = hme_config(sc)) != 0) {
257 device_printf(dev, "could not be configured\n");
261 if ((error = bus_setup_intr(dev, hsc->hsc_ires, INTR_TYPE_NET |
262 INTR_MPSAFE, NULL, hme_intr, sc, &hsc->hsc_ih)) != 0) {
263 device_printf(dev, "couldn't establish interrupt\n");
270 bus_release_resource(dev, SYS_RES_IRQ,
271 rman_get_rid(hsc->hsc_ires), hsc->hsc_ires);
273 if (hsc->hsc_mif_res != NULL) {
274 bus_release_resource(dev, SYS_RES_MEMORY,
275 rman_get_rid(hsc->hsc_mif_res), hsc->hsc_mif_res);
278 bus_release_resource(dev, SYS_RES_MEMORY,
279 rman_get_rid(hsc->hsc_mac_res), hsc->hsc_mac_res);
281 bus_release_resource(dev, SYS_RES_MEMORY,
282 rman_get_rid(hsc->hsc_erx_res), hsc->hsc_erx_res);
284 bus_release_resource(dev, SYS_RES_MEMORY,
285 rman_get_rid(hsc->hsc_etx_res), hsc->hsc_etx_res);
287 bus_release_resource(dev, SYS_RES_MEMORY,
288 rman_get_rid(hsc->hsc_seb_res), hsc->hsc_seb_res);
290 mtx_destroy(&sc->sc_lock);
295 hme_sbus_detach(device_t dev)
297 struct hme_sbus_softc *hsc;
298 struct hme_softc *sc;
300 hsc = device_get_softc(dev);
302 bus_teardown_intr(dev, hsc->hsc_ires, hsc->hsc_ih);
304 bus_release_resource(dev, SYS_RES_IRQ,
305 rman_get_rid(hsc->hsc_ires), hsc->hsc_ires);
306 if (hsc->hsc_mif_res != NULL) {
307 bus_release_resource(dev, SYS_RES_MEMORY,
308 rman_get_rid(hsc->hsc_mif_res), hsc->hsc_mif_res);
310 bus_release_resource(dev, SYS_RES_MEMORY,
311 rman_get_rid(hsc->hsc_mac_res), hsc->hsc_mac_res);
312 bus_release_resource(dev, SYS_RES_MEMORY,
313 rman_get_rid(hsc->hsc_erx_res), hsc->hsc_erx_res);
314 bus_release_resource(dev, SYS_RES_MEMORY,
315 rman_get_rid(hsc->hsc_etx_res), hsc->hsc_etx_res);
316 bus_release_resource(dev, SYS_RES_MEMORY,
317 rman_get_rid(hsc->hsc_seb_res), hsc->hsc_seb_res);
318 mtx_destroy(&sc->sc_lock);
323 hme_sbus_suspend(device_t dev)
325 struct hme_sbus_softc *hsc;
327 hsc = device_get_softc(dev);
328 hme_suspend(&hsc->hsc_hme);
333 hme_sbus_resume(device_t dev)
335 struct hme_sbus_softc *hsc;
337 hsc = device_get_softc(dev);
338 hme_resume(&hsc->hsc_hme);