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.
16 * 3. All advertising materials mentioning features or use of this software
17 * must display the following acknowledgement:
18 * This product includes software developed by the NetBSD
19 * Foundation, Inc. and its contributors.
20 * 4. Neither the name of The NetBSD Foundation nor the names of its
21 * contributors may be used to endorse or promote products derived
22 * from this software without specific prior written permission.
24 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
25 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
26 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
27 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
28 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
29 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
30 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
31 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
32 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
33 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
34 * POSSIBILITY OF SUCH DAMAGE.
36 * from: NetBSD: if_hme_sbus.c,v 1.19 2004/03/17 17:04:58 pk Exp
39 #include <sys/cdefs.h>
40 __FBSDID("$FreeBSD$");
43 * SBus front-end device driver for the HME ethernet device.
46 #include <sys/param.h>
47 #include <sys/systm.h>
49 #include <sys/kernel.h>
50 #include <sys/module.h>
51 #include <sys/resource.h>
52 #include <sys/socket.h>
54 #include <dev/ofw/ofw_bus.h>
56 #include <machine/bus.h>
57 #include <machine/ofw_machdep.h>
58 #include <machine/resource.h>
62 #include <net/ethernet.h>
64 #include <net/if_arp.h>
65 #include <net/if_dl.h>
66 #include <net/if_media.h>
68 #include <dev/mii/mii.h>
69 #include <dev/mii/miivar.h>
71 #include <sparc64/sbus/sbusvar.h>
73 #include <dev/hme/if_hmereg.h>
74 #include <dev/hme/if_hmevar.h>
76 #include "miibus_if.h"
78 struct hme_sbus_softc {
79 struct hme_softc hsc_hme; /* HME device */
80 struct resource *hsc_seb_res;
82 struct resource *hsc_etx_res;
84 struct resource *hsc_erx_res;
86 struct resource *hsc_mac_res;
88 struct resource *hsc_mif_res;
90 struct resource *hsc_ires;
95 static int hme_sbus_probe(device_t);
96 static int hme_sbus_attach(device_t);
97 static int hme_sbus_detach(device_t);
98 static int hme_sbus_suspend(device_t);
99 static int hme_sbus_resume(device_t);
101 static device_method_t hme_sbus_methods[] = {
102 /* Device interface */
103 DEVMETHOD(device_probe, hme_sbus_probe),
104 DEVMETHOD(device_attach, hme_sbus_attach),
105 DEVMETHOD(device_detach, hme_sbus_detach),
106 DEVMETHOD(device_suspend, hme_sbus_suspend),
107 DEVMETHOD(device_resume, hme_sbus_resume),
108 /* Can just use the suspend method here. */
109 DEVMETHOD(device_shutdown, hme_sbus_suspend),
112 DEVMETHOD(bus_print_child, bus_generic_print_child),
113 DEVMETHOD(bus_driver_added, bus_generic_driver_added),
116 DEVMETHOD(miibus_readreg, hme_mii_readreg),
117 DEVMETHOD(miibus_writereg, hme_mii_writereg),
118 DEVMETHOD(miibus_statchg, hme_mii_statchg),
123 static driver_t hme_sbus_driver = {
126 sizeof(struct hme_sbus_softc)
129 DRIVER_MODULE(hme, sbus, hme_sbus_driver, hme_devclass, 0, 0);
130 MODULE_DEPEND(hme, ether, 1, 1, 1);
133 hme_sbus_probe(device_t dev)
137 name = ofw_bus_get_name(dev);
138 if (strcmp(name, "SUNW,qfe") == 0 ||
139 strcmp(name, "SUNW,hme") == 0) {
140 device_set_desc(dev, "Sun HME 10/100 Ethernet");
147 hme_sbus_attach(device_t dev)
149 struct hme_sbus_softc *hsc = device_get_softc(dev);
150 struct hme_softc *sc = &hsc->hsc_hme;
155 mtx_init(&sc->sc_lock, device_get_nameunit(dev), MTX_NETWORK_LOCK,
158 * Map five register banks:
160 * bank 0: HME SEB registers
161 * bank 1: HME ETX registers
162 * bank 2: HME ERX registers
163 * bank 3: HME MAC registers
164 * bank 4: HME MIF registers
167 hsc->hsc_seb_rid = 0;
168 hsc->hsc_seb_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY,
169 &hsc->hsc_seb_rid, RF_ACTIVE);
170 if (hsc->hsc_seb_res == NULL) {
171 device_printf(dev, "cannot map SEB registers\n");
175 sc->sc_sebt = rman_get_bustag(hsc->hsc_seb_res);
176 sc->sc_sebh = rman_get_bushandle(hsc->hsc_seb_res);
178 hsc->hsc_etx_rid = 1;
179 hsc->hsc_etx_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY,
180 &hsc->hsc_etx_rid, RF_ACTIVE);
181 if (hsc->hsc_etx_res == NULL) {
182 device_printf(dev, "cannot map ETX registers\n");
186 sc->sc_etxt = rman_get_bustag(hsc->hsc_etx_res);
187 sc->sc_etxh = rman_get_bushandle(hsc->hsc_etx_res);
189 hsc->hsc_erx_rid = 2;
190 hsc->hsc_erx_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY,
191 &hsc->hsc_erx_rid, RF_ACTIVE);
192 if (hsc->hsc_erx_res == NULL) {
193 device_printf(dev, "cannot map ERX registers\n");
197 sc->sc_erxt = rman_get_bustag(hsc->hsc_erx_res);
198 sc->sc_erxh = rman_get_bushandle(hsc->hsc_erx_res);
200 hsc->hsc_mac_rid = 3;
201 hsc->hsc_mac_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY,
202 &hsc->hsc_mac_rid, RF_ACTIVE);
203 if (hsc->hsc_mac_res == NULL) {
204 device_printf(dev, "cannot map MAC registers\n");
208 sc->sc_mact = rman_get_bustag(hsc->hsc_mac_res);
209 sc->sc_mach = rman_get_bushandle(hsc->hsc_mac_res);
212 * At least on some HMEs, the MIF registers seem to be inside the MAC
213 * range, so try to kludge around it.
215 hsc->hsc_mif_rid = 4;
216 hsc->hsc_mif_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY,
217 &hsc->hsc_mif_rid, RF_ACTIVE);
218 if (hsc->hsc_mif_res == NULL) {
219 if (bus_get_resource(dev, SYS_RES_MEMORY, hsc->hsc_mif_rid,
220 &start, &count) != 0) {
221 device_printf(dev, "cannot get MIF registers\n");
225 if (start < rman_get_start(hsc->hsc_mac_res) ||
226 start + count - 1 > rman_get_end(hsc->hsc_mac_res)) {
227 device_printf(dev, "cannot move MIF registers to MAC "
232 sc->sc_mift = sc->sc_mact;
233 bus_space_subregion(sc->sc_mact, sc->sc_mach,
234 start - rman_get_start(hsc->hsc_mac_res), count,
237 sc->sc_mift = rman_get_bustag(hsc->hsc_mif_res);
238 sc->sc_mifh = rman_get_bushandle(hsc->hsc_mif_res);
242 hsc->hsc_ires = bus_alloc_resource_any(dev, SYS_RES_IRQ,
243 &hsc->hsc_irid, RF_SHAREABLE | RF_ACTIVE);
244 if (hsc->hsc_ires == NULL) {
245 device_printf(dev, "could not allocate interrupt\n");
250 OF_getetheraddr(dev, sc->sc_enaddr);
252 burst = sbus_get_burstsz(dev);
253 /* Translate into plain numerical format */
254 if ((burst & SBUS_BURST_64))
256 else if ((burst & SBUS_BURST_32))
258 else if ((burst & SBUS_BURST_16))
266 if ((error = hme_config(sc)) != 0) {
267 device_printf(dev, "could not be configured\n");
271 if ((error = bus_setup_intr(dev, hsc->hsc_ires, INTR_TYPE_NET |
272 INTR_MPSAFE, NULL, hme_intr, sc, &hsc->hsc_ih)) != 0) {
273 device_printf(dev, "couldn't establish interrupt\n");
280 bus_release_resource(dev, SYS_RES_IRQ, hsc->hsc_irid, hsc->hsc_ires);
282 if (hsc->hsc_mif_res != NULL) {
283 bus_release_resource(dev, SYS_RES_MEMORY, hsc->hsc_mif_rid,
287 bus_release_resource(dev, SYS_RES_MEMORY, hsc->hsc_mac_rid,
290 bus_release_resource(dev, SYS_RES_MEMORY, hsc->hsc_erx_rid,
293 bus_release_resource(dev, SYS_RES_MEMORY, hsc->hsc_etx_rid,
296 bus_release_resource(dev, SYS_RES_MEMORY, hsc->hsc_seb_rid,
299 mtx_destroy(&sc->sc_lock);
304 hme_sbus_detach(device_t dev)
306 struct hme_sbus_softc *hsc = device_get_softc(dev);
307 struct hme_softc *sc = &hsc->hsc_hme;
309 bus_teardown_intr(dev, hsc->hsc_ires, hsc->hsc_ih);
311 bus_release_resource(dev, SYS_RES_IRQ, hsc->hsc_irid, hsc->hsc_ires);
312 if (hsc->hsc_mif_res != NULL) {
313 bus_release_resource(dev, SYS_RES_MEMORY, hsc->hsc_mif_rid,
316 bus_release_resource(dev, SYS_RES_MEMORY, hsc->hsc_mac_rid,
318 bus_release_resource(dev, SYS_RES_MEMORY, hsc->hsc_erx_rid,
320 bus_release_resource(dev, SYS_RES_MEMORY, hsc->hsc_etx_rid,
322 bus_release_resource(dev, SYS_RES_MEMORY, hsc->hsc_seb_rid,
324 mtx_destroy(&sc->sc_lock);
329 hme_sbus_suspend(device_t dev)
331 struct hme_sbus_softc *hsc = device_get_softc(dev);
332 struct hme_softc *sc = &hsc->hsc_hme;
339 hme_sbus_resume(device_t dev)
341 struct hme_sbus_softc *hsc = device_get_softc(dev);
342 struct hme_softc *sc = &hsc->hsc_hme;