xref: /freebsd/sys/dev/gem/if_gem_pci.c (revision 1e413cf93298b5b97441a21d9a50fdcd0ee9945e)
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  * PCI bindings for Apple GMAC, Sun ERI and 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 <machine/bus.h>
53 #if defined(__powerpc__) || defined(__sparc64__)
54 #include <dev/ofw/openfirm.h>
55 #include <machine/ofw_machdep.h>
56 #endif
57 #include <machine/resource.h>
58 
59 #include <dev/gem/if_gemreg.h>
60 #include <dev/gem/if_gemvar.h>
61 
62 #include <dev/pci/pcireg.h>
63 #include <dev/pci/pcivar.h>
64 
65 #include "miibus_if.h"
66 
67 static int	gem_pci_attach(device_t dev);
68 static int	gem_pci_detach(device_t dev);
69 static int	gem_pci_probe(device_t dev);
70 static int	gem_pci_resume(device_t dev);
71 static int	gem_pci_suspend(device_t dev);
72 
73 static device_method_t gem_pci_methods[] = {
74 	/* Device interface */
75 	DEVMETHOD(device_probe,		gem_pci_probe),
76 	DEVMETHOD(device_attach,	gem_pci_attach),
77 	DEVMETHOD(device_detach,	gem_pci_detach),
78 	DEVMETHOD(device_suspend,	gem_pci_suspend),
79 	DEVMETHOD(device_resume,	gem_pci_resume),
80 	/* Use the suspend handler here, it is all that is required. */
81 	DEVMETHOD(device_shutdown,	gem_pci_suspend),
82 
83 	/* bus interface */
84 	DEVMETHOD(bus_print_child,	bus_generic_print_child),
85 	DEVMETHOD(bus_driver_added,	bus_generic_driver_added),
86 
87 	/* MII interface */
88 	DEVMETHOD(miibus_readreg,	gem_mii_readreg),
89 	DEVMETHOD(miibus_writereg,	gem_mii_writereg),
90 	DEVMETHOD(miibus_statchg,	gem_mii_statchg),
91 
92 	{ 0, 0 }
93 };
94 
95 static driver_t gem_pci_driver = {
96 	"gem",
97 	gem_pci_methods,
98 	sizeof(struct gem_softc)
99 };
100 
101 DRIVER_MODULE(gem, pci, gem_pci_driver, gem_devclass, 0, 0);
102 MODULE_DEPEND(gem, pci, 1, 1, 1);
103 MODULE_DEPEND(gem, ether, 1, 1, 1);
104 
105 static const struct gem_pci_dev {
106 	uint32_t	gpd_devid;
107 	int		gpd_variant;
108 	const char	*gpd_desc;
109 } gem_pci_devlist[] = {
110 	{ 0x1101108e, GEM_SUN_ERI,	"Sun ERI 10/100 Ethernet" },
111 	{ 0x2bad108e, GEM_SUN_GEM,	"Sun GEM Gigabit Ethernet" },
112 	{ 0x0021106b, GEM_APPLE_GMAC,	"Apple UniNorth GMAC Ethernet" },
113 	{ 0x0024106b, GEM_APPLE_GMAC,	"Apple Pangea GMAC Ethernet" },
114 	{ 0x0032106b, GEM_APPLE_GMAC,	"Apple UniNorth2 GMAC Ethernet" },
115 	{ 0x004c106b, GEM_APPLE_K2_GMAC,"Apple K2 GMAC Ethernet" },
116 	{ 0x0051106b, GEM_APPLE_GMAC,	"Apple Shasta GMAC Ethernet" },
117 	{ 0x006b106b, GEM_APPLE_GMAC,	"Apple Intrepid 2 GMAC Ethernet" },
118 	{ 0, 0, NULL }
119 };
120 
121 static int
122 gem_pci_probe(device_t dev)
123 {
124 	int i;
125 
126 	for (i = 0; gem_pci_devlist[i].gpd_desc != NULL; i++) {
127 		if (pci_get_devid(dev) == gem_pci_devlist[i].gpd_devid) {
128 			device_set_desc(dev, gem_pci_devlist[i].gpd_desc);
129 			return (BUS_PROBE_DEFAULT);
130 		}
131 	}
132 
133 	return (ENXIO);
134 }
135 
136 static struct resource_spec gem_pci_res_spec[] = {
137 	{ SYS_RES_MEMORY, PCIR_BAR(0), RF_ACTIVE },
138 	{ SYS_RES_IRQ, 0, RF_SHAREABLE | RF_ACTIVE },
139 	{ -1, 0 }
140 };
141 
142 static int
143 gem_pci_attach(device_t dev)
144 {
145 	struct gem_softc *sc;
146 	int i;
147 #if !(defined(__powerpc__) || defined(__sparc64__))
148 	int j;
149 #endif
150 
151 	sc = device_get_softc(dev);
152 	sc->sc_variant = GEM_UNKNOWN;
153 	for (i = 0; gem_pci_devlist[i].gpd_desc != NULL; i++) {
154 		if (pci_get_devid(dev) == gem_pci_devlist[i].gpd_devid) {
155 			sc->sc_variant = gem_pci_devlist[i].gpd_variant;
156 			break;
157 		}
158 	}
159 	if (sc->sc_variant == GEM_UNKNOWN) {
160 		device_printf(dev, "unknown adaptor\n");
161 		return (ENXIO);
162 	}
163 
164 	pci_enable_busmaster(dev);
165 
166 	/*
167 	 * Some Sun GEMs/ERIs do have their intpin register bogusly set to 0,
168 	 * although it should be 1.  Correct that.
169 	 */
170 	if (pci_get_intpin(dev) == 0)
171 		pci_set_intpin(dev, 1);
172 
173 	sc->sc_dev = dev;
174 	sc->sc_flags |= GEM_PCI;
175 
176 	if (bus_alloc_resources(dev, gem_pci_res_spec, sc->sc_res)) {
177 		device_printf(dev, "failed to allocate resources\n");
178 		bus_release_resources(dev, gem_pci_res_spec, sc->sc_res);
179 		return (ENXIO);
180 	}
181 
182 	GEM_LOCK_INIT(sc, device_get_nameunit(dev));
183 
184 #if defined(__powerpc__) || defined(__sparc64__)
185 	OF_getetheraddr(dev, sc->sc_enaddr);
186 #else
187 	/*
188 	 * Dig out VPD (vital product data) and read NA (network address).
189 	 * The VPD of GEM resides in the PCI Expansion ROM (PCI FCode) and
190 	 * can't be accessed via the PCI capability pointer.
191 	 * ``Writing FCode 3.x Programs'' (newer ones, dated 1997 and later)
192 	 * chapter 2 describes the data structure.
193 	 */
194 
195 #define	PCI_ROMHDR_SIZE			0x1c
196 #define	PCI_ROMHDR_SIG			0x00
197 #define	PCI_ROMHDR_SIG_MAGIC		0xaa55		/* little endian */
198 #define	PCI_ROMHDR_PTR_DATA		0x18
199 #define	PCI_ROM_SIZE			0x18
200 #define	PCI_ROM_SIG			0x00
201 #define	PCI_ROM_SIG_MAGIC		0x52494350	/* "PCIR", endian */
202 							/* reversed */
203 #define	PCI_ROM_VENDOR			0x04
204 #define	PCI_ROM_DEVICE			0x06
205 #define	PCI_ROM_PTR_VPD			0x08
206 #define	PCI_VPDRES_BYTE0		0x00
207 #define	PCI_VPDRES_ISLARGE(x)		((x) & 0x80)
208 #define	PCI_VPDRES_LARGE_NAME(x)	((x) & 0x7f)
209 #define	PCI_VPDRES_TYPE_VPD		0x10		/* large */
210 #define	PCI_VPDRES_LARGE_LEN_LSB	0x01
211 #define	PCI_VPDRES_LARGE_LEN_MSB	0x02
212 #define	PCI_VPDRES_LARGE_DATA		0x03
213 #define	PCI_VPD_SIZE			0x03
214 #define	PCI_VPD_KEY0			0x00
215 #define	PCI_VPD_KEY1			0x01
216 #define	PCI_VPD_LEN			0x02
217 #define	PCI_VPD_DATA			0x03
218 
219 #define	GEM_ROM_READ_N(n, sc, offs)					\
220 	bus_read_ ## n ((sc)->sc_res[0], GEM_PCI_ROM_OFFSET + (offs))
221 #define	GEM_ROM_READ_1(sc, offs)	GEM_ROM_READ_N(1, (sc), (offs))
222 #define	GEM_ROM_READ_2(sc, offs)	GEM_ROM_READ_N(2, (sc), (offs))
223 #define	GEM_ROM_READ_4(sc, offs)	GEM_ROM_READ_N(4, (sc), (offs))
224 
225 	/* Read PCI Expansion ROM header. */
226 	if (GEM_ROM_READ_2(sc, PCI_ROMHDR_SIG) != PCI_ROMHDR_SIG_MAGIC ||
227 	    (i = GEM_ROM_READ_2(sc, PCI_ROMHDR_PTR_DATA)) <
228 	    PCI_ROMHDR_SIZE) {
229 		device_printf(dev, "unexpected PCI Expansion ROM header\n");
230 		goto fail;
231 	}
232 
233 	/* Read PCI Expansion ROM data. */
234 	if (GEM_ROM_READ_4(sc, i + PCI_ROM_SIG) != PCI_ROM_SIG_MAGIC ||
235 	    GEM_ROM_READ_2(sc, i + PCI_ROM_VENDOR) != pci_get_vendor(dev) ||
236 	    GEM_ROM_READ_2(sc, i + PCI_ROM_DEVICE) != pci_get_device(dev) ||
237 	    (j = GEM_ROM_READ_2(sc, i + PCI_ROM_PTR_VPD)) <
238 	    i + PCI_ROM_SIZE) {
239 		device_printf(dev, "unexpected PCI Expansion ROM data\n");
240 		goto fail;
241 	}
242 
243 	/*
244 	 * Read PCI VPD.
245 	 * SUNW,pci-gem cards have a single large resource VPD-R tag
246 	 * containing one NA.  The VPD used is not in PCI 2.2 standard
247 	 * format however.  The length in the resource header is in big
248 	 * endian and the end tag is non-standard (0x79) and followed
249 	 * by an all-zero "checksum" byte.  Sun calls this a "Fresh
250 	 * Choice Ethernet" VPD...
251 	 */
252 	if (PCI_VPDRES_ISLARGE(GEM_ROM_READ_1(sc,
253 	    j + PCI_VPDRES_BYTE0)) == 0 ||
254 	    PCI_VPDRES_LARGE_NAME(GEM_ROM_READ_1(sc,
255 	    j + PCI_VPDRES_BYTE0)) !=
256 	    PCI_VPDRES_TYPE_VPD ||
257 	    (GEM_ROM_READ_1(sc, j + PCI_VPDRES_LARGE_LEN_LSB) << 8 |
258 	    GEM_ROM_READ_1(sc, j + PCI_VPDRES_LARGE_LEN_MSB)) !=
259 	    PCI_VPD_SIZE + ETHER_ADDR_LEN ||
260 	    GEM_ROM_READ_1(sc, j + PCI_VPDRES_LARGE_DATA + PCI_VPD_KEY0) !=
261 	    0x4e /* N */ ||
262 	    GEM_ROM_READ_1(sc, j + PCI_VPDRES_LARGE_DATA + PCI_VPD_KEY1) !=
263 	    0x41 /* A */ ||
264 	    GEM_ROM_READ_1(sc, j + PCI_VPDRES_LARGE_DATA + PCI_VPD_LEN) !=
265 	    ETHER_ADDR_LEN ||
266 	    GEM_ROM_READ_1(sc, j + PCI_VPDRES_LARGE_DATA + PCI_VPD_DATA +
267 	    ETHER_ADDR_LEN) != 0x79) {
268 		device_printf(dev, "unexpected PCI VPD\n");
269 		goto fail;
270 	}
271 	bus_read_region_1(sc->sc_res[0], GEM_PCI_ROM_OFFSET + j +
272 	    PCI_VPDRES_LARGE_DATA + PCI_VPD_DATA, sc->sc_enaddr,
273 	    ETHER_ADDR_LEN);
274 #endif
275 
276 	if (gem_attach(sc) != 0) {
277 		device_printf(dev, "could not be attached\n");
278 		goto fail;
279 	}
280 
281 	if (bus_setup_intr(dev, sc->sc_res[1], INTR_TYPE_NET | INTR_MPSAFE,
282 	    NULL, gem_intr, sc, &sc->sc_ih) != 0) {
283 		device_printf(dev, "failed to set up interrupt\n");
284 		gem_detach(sc);
285 		goto fail;
286 	}
287 	return (0);
288 
289  fail:
290 	GEM_LOCK_DESTROY(sc);
291 	bus_release_resources(dev, gem_pci_res_spec, sc->sc_res);
292 	return (ENXIO);
293 }
294 
295 static int
296 gem_pci_detach(device_t dev)
297 {
298 	struct gem_softc *sc;
299 
300 	sc = device_get_softc(dev);
301 	bus_teardown_intr(dev, sc->sc_res[1], sc->sc_ih);
302 	gem_detach(sc);
303 	GEM_LOCK_DESTROY(sc);
304 	bus_release_resources(dev, gem_pci_res_spec, sc->sc_res);
305 	return (0);
306 }
307 
308 static int
309 gem_pci_suspend(device_t dev)
310 {
311 	struct gem_softc *sc;
312 
313 	sc = device_get_softc(dev);
314 	gem_suspend(sc);
315 	return (0);
316 }
317 
318 static int
319 gem_pci_resume(device_t dev)
320 {
321 	struct gem_softc *sc;
322 
323 	sc = device_get_softc(dev);
324 	gem_resume(sc);
325 	return (0);
326 }
327