]> CyberLeo.Net >> Repos - FreeBSD/releng/9.0.git/blob - sys/dev/gem/if_gem_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 / gem / if_gem_sbus.c
1 /*-
2  * Copyright (C) 2001 Eduardo Horvath.
3  * Copyright (c) 2007 Marius Strobl <marius@FreeBSD.org>
4  * All rights reserved.
5  *
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR  ``AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR  BE LIABLE
20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  *
28  *      from: NetBSD: if_gem_pci.c,v 1.7 2001/10/18 15:09:15 thorpej Exp
29  */
30
31 #include <sys/cdefs.h>
32 __FBSDID("$FreeBSD$");
33
34 /*
35  * SBus bindings for Sun GEM Ethernet controllers
36  */
37
38 #include <sys/param.h>
39 #include <sys/systm.h>
40 #include <sys/bus.h>
41 #include <sys/kernel.h>
42 #include <sys/lock.h>
43 #include <sys/module.h>
44 #include <sys/mutex.h>
45 #include <sys/resource.h>
46 #include <sys/rman.h>
47 #include <sys/socket.h>
48
49 #include <net/ethernet.h>
50 #include <net/if.h>
51
52 #include <dev/ofw/ofw_bus.h>
53
54 #include <machine/bus.h>
55 #include <machine/ofw_machdep.h>
56 #include <machine/resource.h>
57
58 #include <sparc64/sbus/sbusvar.h>
59
60 #include <dev/gem/if_gemreg.h>
61 #include <dev/gem/if_gemvar.h>
62
63 #include "miibus_if.h"
64
65 static device_probe_t gem_sbus_probe;
66 static device_attach_t gem_sbus_attach;
67 static device_detach_t gem_sbus_detach;
68 static device_suspend_t gem_sbus_suspend;
69 static device_resume_t gem_sbus_resume;
70
71 static device_method_t gem_sbus_methods[] = {
72         /* Device interface */
73         DEVMETHOD(device_probe,         gem_sbus_probe),
74         DEVMETHOD(device_attach,        gem_sbus_attach),
75         DEVMETHOD(device_detach,        gem_sbus_detach),
76         DEVMETHOD(device_suspend,       gem_sbus_suspend),
77         DEVMETHOD(device_resume,        gem_sbus_resume),
78         /* Use the suspend handler here, it is all that is required. */
79         DEVMETHOD(device_shutdown,      gem_sbus_suspend),
80
81         /* bus interface */
82         DEVMETHOD(bus_print_child,      bus_generic_print_child),
83         DEVMETHOD(bus_driver_added,     bus_generic_driver_added),
84
85         /* MII interface */
86         DEVMETHOD(miibus_readreg,       gem_mii_readreg),
87         DEVMETHOD(miibus_writereg,      gem_mii_writereg),
88         DEVMETHOD(miibus_statchg,       gem_mii_statchg),
89
90         KOBJMETHOD_END
91 };
92
93 static driver_t gem_sbus_driver = {
94         "gem",
95         gem_sbus_methods,
96         sizeof(struct gem_softc)
97 };
98
99 DRIVER_MODULE(gem, sbus, gem_sbus_driver, gem_devclass, 0, 0);
100 MODULE_DEPEND(gem, sbus, 1, 1, 1);
101 MODULE_DEPEND(gem, ether, 1, 1, 1);
102
103 static int
104 gem_sbus_probe(device_t dev)
105 {
106
107         if (strcmp(ofw_bus_get_name(dev), "network") == 0 &&
108             ofw_bus_get_compat(dev) != NULL &&
109             strcmp(ofw_bus_get_compat(dev), "SUNW,sbus-gem") == 0) {
110                 device_set_desc(dev, "Sun GEM Gigabit Ethernet");
111                 return (0);
112         }
113
114         return (ENXIO);
115 }
116
117 static struct resource_spec gem_sbus_res_spec[] = {
118         { SYS_RES_IRQ, 0, RF_SHAREABLE | RF_ACTIVE },   /* GEM_RES_INTR */
119         { SYS_RES_MEMORY, 1, RF_ACTIVE },               /* GEM_RES_BANK1 */
120         { SYS_RES_MEMORY, 0, RF_ACTIVE },               /* GEM_RES_BANK2 */
121         { -1, 0 }
122 };
123
124 static int
125 gem_sbus_attach(device_t dev)
126 {
127         struct gem_softc *sc;
128         int burst;
129         uint32_t val;
130
131         sc = device_get_softc(dev);
132         sc->sc_variant = GEM_SUN_GEM;
133         sc->sc_dev = dev;
134         /* All known SBus models use a SERDES. */
135         sc->sc_flags = GEM_SERDES;
136
137         if (bus_alloc_resources(dev, gem_sbus_res_spec, sc->sc_res)) {
138                 device_printf(dev, "failed to allocate resources\n");
139                 bus_release_resources(dev, gem_sbus_res_spec, sc->sc_res);
140                 return (ENXIO);
141         }
142
143         GEM_LOCK_INIT(sc, device_get_nameunit(dev));
144
145         OF_getetheraddr(dev, sc->sc_enaddr);
146
147         burst = sbus_get_burstsz(dev);
148         val = GEM_SBUS_CFG_PARITY;
149         if ((burst & SBUS_BURST64_MASK) != 0) {
150                 val |= GEM_SBUS_CFG_64BIT;
151                 burst >>= SBUS_BURST64_SHIFT;
152         }
153         if ((burst & SBUS_BURST_64) != 0)
154                 val |= GEM_SBUS_CFG_BURST_64;
155         else if ((burst & SBUS_BURST_32) != 0)
156                 val |= GEM_SBUS_CFG_BURST_32;
157         else {
158                 device_printf(dev, "unsupported burst size\n");
159                 goto fail;
160         }
161         /* Reset the SBus interface only. */
162         (void)GEM_BANK2_READ_4(sc, GEM_SBUS_BIF_RESET);
163         DELAY(100);
164         GEM_BANK2_WRITE_4(sc, GEM_SBUS_CONFIG, val);
165
166         if (gem_attach(sc) != 0) {
167                 device_printf(dev, "could not be attached\n");
168                 goto fail;
169         }
170
171         if (bus_setup_intr(dev, sc->sc_res[GEM_RES_INTR], INTR_TYPE_NET |
172             INTR_MPSAFE, NULL, gem_intr, sc, &sc->sc_ih) != 0) {
173                 device_printf(dev, "failed to set up interrupt\n");
174                 gem_detach(sc);
175                 goto fail;
176         }
177         return (0);
178
179  fail:
180         GEM_LOCK_DESTROY(sc);
181         bus_release_resources(dev, gem_sbus_res_spec, sc->sc_res);
182         return (ENXIO);
183 }
184
185 static int
186 gem_sbus_detach(device_t dev)
187 {
188         struct gem_softc *sc;
189
190         sc = device_get_softc(dev);
191         bus_teardown_intr(dev, sc->sc_res[GEM_RES_INTR], sc->sc_ih);
192         gem_detach(sc);
193         GEM_LOCK_DESTROY(sc);
194         bus_release_resources(dev, gem_sbus_res_spec, sc->sc_res);
195         return (0);
196 }
197
198 static int
199 gem_sbus_suspend(device_t dev)
200 {
201
202         gem_suspend(device_get_softc(dev));
203         return (0);
204 }
205
206 static int
207 gem_sbus_resume(device_t dev)
208 {
209
210         gem_resume(device_get_softc(dev));
211         return (0);
212 }