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