xref: /netbsd-src/sys/dev/pci/if_gem_pci.c (revision da9817918ec7e88db2912a2882967c7570a83f47)
1 /*	$NetBSD: if_gem_pci.c,v 1.35 2009/05/12 08:23:00 cegger Exp $ */
2 
3 /*
4  *
5  * Copyright (C) 2001 Eduardo Horvath.
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  */
31 
32 /*
33  * PCI bindings for Apple GMAC, Sun ERI and Sun GEM Ethernet controllers
34  */
35 
36 #include <sys/cdefs.h>
37 __KERNEL_RCSID(0, "$NetBSD: if_gem_pci.c,v 1.35 2009/05/12 08:23:00 cegger Exp $");
38 
39 #include <sys/param.h>
40 #include <sys/systm.h>
41 #include <sys/malloc.h>
42 #include <sys/kernel.h>
43 #include <sys/socket.h>
44 #include <sys/errno.h>
45 #include <sys/device.h>
46 
47 #include <machine/endian.h>
48 
49 #include <uvm/uvm_extern.h>
50 
51 #include <net/if.h>
52 #include <net/if_dl.h>
53 #include <net/if_media.h>
54 #include <net/if_ether.h>
55 
56 #if NBPFILTER > 0
57 #include <net/bpf.h>
58 #endif
59 
60 #include <sys/bus.h>
61 #include <sys/intr.h>
62 
63 #include <dev/mii/mii.h>
64 #include <dev/mii/miivar.h>
65 #include <dev/mii/mii_bitbang.h>
66 
67 #include <dev/ic/gemreg.h>
68 #include <dev/ic/gemvar.h>
69 
70 #include <dev/pci/pcivar.h>
71 #include <dev/pci/pcireg.h>
72 #include <dev/pci/pcidevs.h>
73 
74 /* XXX Should use Properties when that's fleshed out. */
75 #ifdef macppc
76 #include <dev/ofw/openfirm.h>
77 #endif /* macppc */
78 #ifdef __sparc__
79 #include <machine/promlib.h>
80 #endif
81 
82 #ifndef GEM_USE_LOCAL_MAC_ADDRESS
83 #if defined (macppc) || defined (__sparc__)
84 #define GEM_USE_LOCAL_MAC_ADDRESS	0	/* use system-wide address */
85 #else
86 #define GEM_USE_LOCAL_MAC_ADDRESS	1
87 #endif
88 #endif
89 
90 
91 struct gem_pci_softc {
92 	struct	gem_softc	gsc_gem;	/* GEM device */
93 	void			*gsc_ih;
94 };
95 
96 int	gem_match_pci(device_t, cfdata_t, void *);
97 void	gem_attach_pci(device_t, device_t, void *);
98 
99 CFATTACH_DECL(gem_pci, sizeof(struct gem_pci_softc),
100     gem_match_pci, gem_attach_pci, NULL, NULL);
101 
102 /*
103  * Attach routines need to be split out to different bus-specific files.
104  */
105 
106 int
107 gem_match_pci(device_t parent, cfdata_t cf, void *aux)
108 {
109 	struct pci_attach_args *pa = aux;
110 
111 	if (PCI_VENDOR(pa->pa_id) == PCI_VENDOR_SUN &&
112 	    (PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_SUN_ERINETWORK ||
113 	     PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_SUN_GEMNETWORK))
114 		return (1);
115 
116 	if (PCI_VENDOR(pa->pa_id) == PCI_VENDOR_APPLE &&
117 	    (PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_APPLE_GMAC ||
118 	     PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_APPLE_GMAC2 ||
119 	     PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_APPLE_GMAC3 ||
120  	     PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_APPLE_SHASTA_GMAC ||
121 	     PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_APPLE_K2_GMAC ||
122 	     PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_APPLE_SHASTA_GMAC ||
123 	     PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_APPLE_INTREPID2_GMAC))
124 		return (1);
125 
126 
127 	return (0);
128 }
129 
130 #if GEM_USE_LOCAL_MAC_ADDRESS
131 static inline int
132 gempromvalid(u_int8_t* buf)
133 {
134 	return buf[0] == 0x18 && buf[1] == 0x00 &&	/* structure length */
135 	    buf[2] == 0x00 &&				/* revision */
136 	    (buf[3] == 0x00 ||				/* hme */
137 	     buf[3] == 0x80) &&				/* qfe */
138 	    buf[4] == PCI_SUBCLASS_NETWORK_ETHERNET &&	/* subclass code */
139 	    buf[5] == PCI_CLASS_NETWORK;		/* class code */
140 }
141 
142 static inline int
143 isshared_pins(u_int8_t* buf)
144 {
145 	return buf[0] == 's' && buf[1] == 'h' && buf[2] == 'a' &&
146 	    buf[3] == 'r' && buf[4] == 'e' && buf[5] == 'd' &&
147 	    buf[6] == '-' && buf[7] == 'p' && buf[8] == 'i' &&
148 	    buf[9] == 'n' && buf[10] == 's';
149 }
150 #endif
151 
152 static inline int
153 isserdes(u_int8_t* buf)
154 {
155 	return buf[0] == 's' && buf[1] == 'e' && buf[2] == 'r' &&
156 	    buf[3] == 'd' && buf[4] == 'e' && buf[5] == 's';
157 }
158 
159 void
160 gem_attach_pci(device_t parent, device_t self, void *aux)
161 {
162 	struct pci_attach_args *pa = aux;
163 	struct gem_pci_softc *gsc = device_private(self);
164 	struct gem_softc *sc = &gsc->gsc_gem;
165 	pci_intr_handle_t ih;
166 	const char *intrstr;
167 	char devinfo[256];
168 	uint8_t enaddr[ETHER_ADDR_LEN];
169 #if GEM_USE_LOCAL_MAC_ADDRESS
170 	u_int8_t		*enp;
171 	bus_space_handle_t	romh;
172 	u_int8_t		buf[0x0800];
173 	int			dataoff, vpdoff, serdes;
174 #if GEM_USE_LOCAL_MAC_ADDRESS || defined(GEM_DEBUG)
175 	int i;
176 #endif
177 #ifdef GEM_DEBUG
178 	int j;
179 #endif
180 	struct pci_vpd		*vpd;
181 	static const u_int8_t promhdr[] = { 0x55, 0xaa };
182 #define PROMHDR_PTR_DATA	0x18
183 	static const u_int8_t promdat[] = {
184 		0x50, 0x43, 0x49, 0x52,		/* "PCIR" */
185 		PCI_VENDOR_SUN & 0xff, PCI_VENDOR_SUN >> 8,
186 		PCI_PRODUCT_SUN_GEMNETWORK & 0xff,
187 		PCI_PRODUCT_SUN_GEMNETWORK >> 8
188 	};
189 #define PROMDATA_PTR_VPD	0x08
190 #define PROMDATA_DATA2		0x0a
191 #endif  /* GEM_USE_LOCAL_MAC_ADDRESS */
192 
193 	aprint_naive(": Ethernet controller\n");
194 
195 	pci_devinfo(pa->pa_id, pa->pa_class, 0, devinfo, sizeof(devinfo));
196 	sc->sc_chiprev = PCI_REVISION(pa->pa_class);
197 	aprint_normal(": %s (rev. 0x%02x)\n", devinfo, sc->sc_chiprev);
198 
199 	/*
200 	 * Some Sun GEMs/ERIs do have their intpin register bogusly set to 0,
201 	 * although it should be 1. correct that.
202 	 */
203 	if (pa->pa_intrpin == 0)
204 		pa->pa_intrpin = 1;
205 
206 	sc->sc_variant = GEM_UNKNOWN;
207 
208 	sc->sc_dmatag = pa->pa_dmat;
209 
210 	sc->sc_flags |= GEM_PCI;
211 
212 	if (PCI_VENDOR(pa->pa_id) == PCI_VENDOR_SUN) {
213 		if (PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_SUN_GEMNETWORK)
214 			sc->sc_variant = GEM_SUN_GEM;
215 		if (PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_SUN_ERINETWORK)
216 			sc->sc_variant = GEM_SUN_ERI;
217 	} else if (PCI_VENDOR(pa->pa_id) == PCI_VENDOR_APPLE) {
218 		if (PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_APPLE_GMAC ||
219 		     PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_APPLE_GMAC2 ||
220 		     PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_APPLE_GMAC3 ||
221 		     PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_APPLE_SHASTA_GMAC ||
222 		     PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_APPLE_INTREPID2_GMAC)
223 			sc->sc_variant = GEM_APPLE_GMAC;
224 		if (PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_APPLE_K2_GMAC)
225 			sc->sc_variant = GEM_APPLE_K2_GMAC;
226 	}
227 
228 	if (sc->sc_variant == GEM_UNKNOWN) {
229 		aprint_error_dev(&sc->sc_dev, "unknown adaptor\n");
230 		return;
231 	}
232 
233 #define PCI_GEM_BASEADDR	(PCI_MAPREG_START + 0x00)
234 
235 	/* XXX Need to check for a 64-bit mem BAR? */
236 	if (pci_mapreg_map(pa, PCI_GEM_BASEADDR,
237 	    PCI_MAPREG_TYPE_MEM | PCI_MAPREG_MEM_TYPE_32BIT, 0,
238 	    &sc->sc_bustag, &sc->sc_h1, NULL, NULL) != 0)
239 	{
240 		aprint_error_dev(&sc->sc_dev, "unable to map device registers\n");
241 		return;
242 	}
243 	if (bus_space_subregion(sc->sc_bustag, sc->sc_h1,
244 	    GEM_PCI_BANK2_OFFSET, GEM_PCI_BANK2_SIZE, &sc->sc_h2)) {
245 		aprint_error_dev(&sc->sc_dev, "unable to create bank 2 subregion\n");
246 		return;
247 	}
248 
249 #if GEM_USE_LOCAL_MAC_ADDRESS
250 	/*
251 	 * Dig out VPD (vital product data) and acquire Ethernet address.
252 	 * The VPD of gem resides in the PCI PROM (PCI FCode).
253 	 */
254 	/*
255 	 * ``Writing FCode 3.x Programs'' (newer ones, dated 1997 and later)
256 	 * chapter 2 describes the data structure.
257 	 */
258 
259 	enp = NULL;
260 
261 	if (sc->sc_variant == GEM_SUN_GEM &&
262 	    (bus_space_subregion(sc->sc_bustag, sc->sc_h1,
263 	    GEM_PCI_ROM_OFFSET, GEM_PCI_ROM_SIZE, &romh)) == 0) {
264 
265 		/* read PCI Expansion PROM Header */
266 		bus_space_read_region_1(sc->sc_bustag,
267 		    romh, 0, buf, sizeof buf);
268 
269 		/* Check for "shared-pins = serdes" in FCode. */
270 		i = 0;
271 		serdes = 0;
272 		while (i < (sizeof buf) - sizeof "serdes") {
273 			if (!serdes) {
274 				if (isserdes(&buf[i]))
275 					serdes = 1;
276 			} else {
277 				if (isshared_pins(&buf[i]))
278 					serdes = 2;
279 			}
280 			if (serdes == 2) {
281 				sc->sc_flags |= GEM_SERDES;
282 				break;
283 			}
284 			i++;
285 		}
286 #ifdef GEM_DEBUG
287 		/* PROM dump */
288 		printf("%s: PROM dump (0x0000 to %04lx)\n", device_xname(&sc->sc_dev),
289 		    (sizeof buf) - 1);
290 		i = 0;
291 		j = 0;
292 		printf("  %04x  ", i);
293 		while (i < sizeof buf) {
294 			printf("%02x ", buf[i]);
295 			if (i && !(i % 8))
296 				printf(" ");
297 			if (i && !(i % 16)) {
298 				printf(" ");
299 				while (j < i) {
300 					if (buf[j] > 31 && buf[j] < 128)
301 						printf("%c", buf[j]);
302 					else
303 						printf(".");
304 					j++;
305 				}
306 				j = i;
307 				printf("\n  %04x  ", i);
308 				}
309 			i++;
310 		}
311 		printf("\n");
312 #endif
313 
314 		if (memcmp(buf, promhdr, sizeof promhdr) == 0 &&
315 		    (dataoff = (buf[PROMHDR_PTR_DATA] |
316 			(buf[PROMHDR_PTR_DATA + 1] << 8))) >= 0x1c) {
317 
318 			/* read PCI Expansion PROM Data */
319 			bus_space_read_region_1(sc->sc_bustag, romh, dataoff,
320 			    buf, 64);
321 			if (memcmp(buf, promdat, sizeof promdat) == 0 &&
322 			    gempromvalid(buf + PROMDATA_DATA2) &&
323 			    (vpdoff = (buf[PROMDATA_PTR_VPD] |
324 				(buf[PROMDATA_PTR_VPD + 1] << 8))) >= 0x1c) {
325 
326 				/*
327 				 * The VPD of gem is not in PCI 2.2 standard
328 				 * format.  The length in the resource header
329 				 * is in big endian, and resources are not
330 				 * properly terminated (only one resource
331 				 * and no end tag).
332 				 */
333 				/* read PCI VPD */
334 				bus_space_read_region_1(sc->sc_bustag, romh,
335 				    vpdoff, buf, 64);
336 				vpd = (void *)(buf + 3);
337 				if (PCI_VPDRES_ISLARGE(buf[0]) &&
338 				    PCI_VPDRES_LARGE_NAME(buf[0])
339 					== PCI_VPDRES_TYPE_VPD &&
340 				    vpd->vpd_key0 == 0x4e /* N */ &&
341 				    vpd->vpd_key1 == 0x41 /* A */ &&
342 				    vpd->vpd_len == ETHER_ADDR_LEN) {
343 					/*
344 					 * Ethernet address found
345 					 */
346 					enp = buf + 6;
347 				}
348 			}
349 		}
350 	}
351 
352 	if (enp)
353 		memcpy(enaddr, enp, ETHER_ADDR_LEN);
354 	else
355 #endif  /* GEM_USE_LOCAL_MAC_ADDRESS */
356 #ifdef __sparc__
357 	{
358 		if (strcmp(prom_getpropstring(PCITAG_NODE(pa->pa_tag),
359 		    "shared-pins"), "serdes") == 0)
360 			sc->sc_flags |= GEM_SERDES;
361 		prom_getether(PCITAG_NODE(pa->pa_tag), enaddr);
362 	}
363 #else
364 #ifdef macppc
365 	{
366 		int node;
367 		char sp[6];	/* "serdes" */
368 
369 		node = pcidev_to_ofdev(pa->pa_pc, pa->pa_tag);
370 		if (node == 0) {
371 			aprint_error_dev(&sc->sc_dev, "unable to locate OpenFirmware node\n");
372 			return;
373 		}
374 
375 		OF_getprop(node, "shared-pins", sp, sizeof(sp));
376 		if (isserdes(sp))
377 			sc->sc_flags |= GEM_SERDES;
378 		OF_getprop(node, "local-mac-address", enaddr, sizeof(enaddr));
379 	}
380 #else
381 		printf("%s: no Ethernet address found\n", device_xname(&sc->sc_dev));
382 #endif /* macppc */
383 #endif /* __sparc__ */
384 
385 	if (pci_intr_map(pa, &ih) != 0) {
386 		aprint_error_dev(&sc->sc_dev, "unable to map interrupt\n");
387 		return;
388 	}
389 	intrstr = pci_intr_string(pa->pa_pc, ih);
390 	gsc->gsc_ih = pci_intr_establish(pa->pa_pc, ih, IPL_NET, gem_intr, sc);
391 	if (gsc->gsc_ih == NULL) {
392 		aprint_error_dev(&sc->sc_dev, "unable to establish interrupt");
393 		if (intrstr != NULL)
394 			aprint_normal(" at %s", intrstr);
395 		aprint_normal("\n");
396 		return;
397 	}
398 	aprint_normal_dev(&sc->sc_dev, "interrupting at %s\n", intrstr);
399 
400 	/* Finish off the attach. */
401 	gem_attach(sc, enaddr);
402 }
403