]> CyberLeo.Net >> Repos - FreeBSD/releng/9.0.git/blob - sys/dev/hme/if_hme_sbus.c
Copy stable/9 to releng/9.0 as part of the FreeBSD 9.0-RELEASE release
[FreeBSD/releng/9.0.git] / sys / dev / hme / if_hme_sbus.c
1 /*-
2  * Copyright (c) 1999 The NetBSD Foundation, Inc.
3  * All rights reserved.
4  *
5  * This code is derived from software contributed to The NetBSD Foundation
6  * by Paul Kranenburg.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
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  *
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.
28  *
29  *      from: NetBSD: if_hme_sbus.c,v 1.19 2004/03/17 17:04:58 pk Exp
30  */
31
32 #include <sys/cdefs.h>
33 __FBSDID("$FreeBSD$");
34
35 /*
36  * SBus front-end device driver for the HME ethernet device.
37  */
38
39 #include <sys/param.h>
40 #include <sys/systm.h>
41 #include <sys/bus.h>
42 #include <sys/kernel.h>
43 #include <sys/module.h>
44 #include <sys/resource.h>
45 #include <sys/socket.h>
46
47 #include <dev/ofw/ofw_bus.h>
48
49 #include <machine/bus.h>
50 #include <machine/ofw_machdep.h>
51 #include <machine/resource.h>
52
53 #include <sys/rman.h>
54
55 #include <net/ethernet.h>
56 #include <net/if.h>
57 #include <net/if_arp.h>
58 #include <net/if_dl.h>
59 #include <net/if_media.h>
60
61 #include <dev/mii/mii.h>
62 #include <dev/mii/miivar.h>
63
64 #include <sparc64/sbus/sbusvar.h>
65
66 #include <dev/hme/if_hmereg.h>
67 #include <dev/hme/if_hmevar.h>
68
69 #include "miibus_if.h"
70
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;
79         void                    *hsc_ih;
80 };
81
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);
87
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),
97
98         /* bus interface */
99         DEVMETHOD(bus_print_child,      bus_generic_print_child),
100         DEVMETHOD(bus_driver_added,     bus_generic_driver_added),
101
102         /* MII interface */
103         DEVMETHOD(miibus_readreg,       hme_mii_readreg),
104         DEVMETHOD(miibus_writereg,      hme_mii_writereg),
105         DEVMETHOD(miibus_statchg,       hme_mii_statchg),
106
107         { 0, 0 }
108 };
109
110 static driver_t hme_sbus_driver = {
111         "hme",
112         hme_sbus_methods,
113         sizeof(struct hme_sbus_softc)
114 };
115
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);
119
120 static int
121 hme_sbus_probe(device_t dev)
122 {
123         const char *name;
124
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");
129                 return (0);
130         }
131         return (ENXIO);
132 }
133
134 static int
135 hme_sbus_attach(device_t dev)
136 {
137         struct hme_sbus_softc *hsc;
138         struct hme_softc *sc;
139         u_long start, count;
140         uint32_t burst;
141         int i, error = 0;
142
143         hsc = device_get_softc(dev);
144         sc = &hsc->hsc_hme;
145         mtx_init(&sc->sc_lock, device_get_nameunit(dev), MTX_NETWORK_LOCK,
146             MTX_DEF);
147         /*
148          * Map five register banks:
149          *
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
155          *
156          */
157         i = 0;
158         hsc->hsc_seb_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY,
159             &i, RF_ACTIVE);
160         if (hsc->hsc_seb_res == NULL) {
161                 device_printf(dev, "cannot map SEB registers\n");
162                 error = ENXIO;
163                 goto fail_mtx_res;
164         }
165         sc->sc_sebt = rman_get_bustag(hsc->hsc_seb_res);
166         sc->sc_sebh = rman_get_bushandle(hsc->hsc_seb_res);
167
168         i = 1;
169         hsc->hsc_etx_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY,
170             &i, RF_ACTIVE);
171         if (hsc->hsc_etx_res == NULL) {
172                 device_printf(dev, "cannot map ETX registers\n");
173                 error = ENXIO;
174                 goto fail_seb_res;
175         }
176         sc->sc_etxt = rman_get_bustag(hsc->hsc_etx_res);
177         sc->sc_etxh = rman_get_bushandle(hsc->hsc_etx_res);
178
179         i = 2;
180         hsc->hsc_erx_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY,
181             &i, RF_ACTIVE);
182         if (hsc->hsc_erx_res == NULL) {
183                 device_printf(dev, "cannot map ERX registers\n");
184                 error = ENXIO;
185                 goto fail_etx_res;
186         }
187         sc->sc_erxt = rman_get_bustag(hsc->hsc_erx_res);
188         sc->sc_erxh = rman_get_bushandle(hsc->hsc_erx_res);
189
190         i = 3;
191         hsc->hsc_mac_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY,
192             &i, RF_ACTIVE);
193         if (hsc->hsc_mac_res == NULL) {
194                 device_printf(dev, "cannot map MAC registers\n");
195                 error = ENXIO;
196                 goto fail_erx_res;
197         }
198         sc->sc_mact = rman_get_bustag(hsc->hsc_mac_res);
199         sc->sc_mach = rman_get_bushandle(hsc->hsc_mac_res);
200
201         /*
202          * At least on some HMEs, the MIF registers seem to be inside the MAC
203          * range, so try to kludge around it.
204          */
205         i = 4;
206         hsc->hsc_mif_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY,
207             &i, RF_ACTIVE);
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");
212                         error = ENXIO;
213                         goto fail_mac_res;
214                 }
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 "
218                             "bank\n");
219                         error = ENXIO;
220                         goto fail_mac_res;
221                 }
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,
225                     &sc->sc_mifh);
226         } else {
227                 sc->sc_mift = rman_get_bustag(hsc->hsc_mif_res);
228                 sc->sc_mifh = rman_get_bushandle(hsc->hsc_mif_res);
229         }
230
231         i = 0;
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");
236                 error = ENXIO;
237                 goto fail_mif_res;
238         }
239
240         OF_getetheraddr(dev, sc->sc_enaddr);
241
242         burst = sbus_get_burstsz(dev);
243         /* Translate into plain numerical format */
244         if ((burst & SBUS_BURST_64))
245                 sc->sc_burst = 64;
246         else if ((burst & SBUS_BURST_32))
247                 sc->sc_burst = 32;
248         else if ((burst & SBUS_BURST_16))
249                 sc->sc_burst = 16;
250         else
251                  sc->sc_burst = 0;
252
253         sc->sc_dev = dev;
254         sc->sc_flags = 0;
255
256         if ((error = hme_config(sc)) != 0) {
257                 device_printf(dev, "could not be configured\n");
258                 goto fail_ires;
259         }
260
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");
264                 hme_detach(sc);
265                 goto fail_ires;
266         }
267         return (0);
268
269 fail_ires:
270         bus_release_resource(dev, SYS_RES_IRQ,
271             rman_get_rid(hsc->hsc_ires), hsc->hsc_ires);
272 fail_mif_res:
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);
276         }
277 fail_mac_res:
278         bus_release_resource(dev, SYS_RES_MEMORY,
279             rman_get_rid(hsc->hsc_mac_res), hsc->hsc_mac_res);
280 fail_erx_res:
281         bus_release_resource(dev, SYS_RES_MEMORY,
282             rman_get_rid(hsc->hsc_erx_res), hsc->hsc_erx_res);
283 fail_etx_res:
284         bus_release_resource(dev, SYS_RES_MEMORY,
285             rman_get_rid(hsc->hsc_etx_res), hsc->hsc_etx_res);
286 fail_seb_res:
287         bus_release_resource(dev, SYS_RES_MEMORY,
288             rman_get_rid(hsc->hsc_seb_res), hsc->hsc_seb_res);
289 fail_mtx_res:
290         mtx_destroy(&sc->sc_lock);
291         return (error);
292 }
293
294 static int
295 hme_sbus_detach(device_t dev)
296 {
297         struct hme_sbus_softc *hsc;
298         struct hme_softc *sc;
299
300         hsc = device_get_softc(dev);
301         sc = &hsc->hsc_hme;
302         bus_teardown_intr(dev, hsc->hsc_ires, hsc->hsc_ih);
303         hme_detach(sc);
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);
309         }
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);
319         return (0);
320 }
321
322 static int
323 hme_sbus_suspend(device_t dev)
324 {
325         struct hme_sbus_softc *hsc;
326
327         hsc = device_get_softc(dev);
328         hme_suspend(&hsc->hsc_hme);
329         return (0);
330 }
331
332 static int
333 hme_sbus_resume(device_t dev)
334 {
335         struct hme_sbus_softc *hsc;
336
337         hsc = device_get_softc(dev);
338         hme_resume(&hsc->hsc_hme);
339         return (0);
340 }