xref: /openbsd/sys/dev/pci/if_gem_pci.c (revision 17df1aa7)
1 /*	$OpenBSD: if_gem_pci.c,v 1.31 2009/10/15 17:54:56 deraadt Exp $	*/
2 /*	$NetBSD: if_gem_pci.c,v 1.1 2001/09/16 00:11:42 eeh Exp $ */
3 
4 /*
5  *
6  * Copyright (C) 2001 Eduardo Horvath.
7  * All rights reserved.
8  *
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR  ``AS IS'' AND
20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR  BE LIABLE
23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29  * SUCH DAMAGE.
30  *
31  */
32 
33 /*
34  * PCI bindings for Sun GEM ethernet controllers.
35  */
36 
37 #include <sys/param.h>
38 #include <sys/systm.h>
39 #include <sys/malloc.h>
40 #include <sys/kernel.h>
41 #include <sys/socket.h>
42 #include <sys/errno.h>
43 #include <sys/device.h>
44 
45 #include <machine/endian.h>
46 
47 #include <net/if.h>
48 #include <net/if_dl.h>
49 #include <net/if_media.h>
50 
51 #ifdef INET
52 #include <netinet/in.h>
53 #include <netinet/if_ether.h>
54 #endif
55 
56 #if NBPFILTER > 0
57 #include <net/bpf.h>
58 #endif
59 
60 #include <machine/bus.h>
61 #include <machine/intr.h>
62 
63 #ifdef __sparc64__
64 #include <dev/ofw/openfirm.h>
65 #endif
66 
67 #include <dev/mii/mii.h>
68 #include <dev/mii/miivar.h>
69 #include <dev/mii/mii_bitbang.h>
70 
71 #include <dev/ic/gemreg.h>
72 #include <dev/ic/gemvar.h>
73 
74 #include <dev/pci/pcivar.h>
75 #include <dev/pci/pcireg.h>
76 #include <dev/pci/pcidevs.h>
77 
78 struct gem_pci_softc {
79 	struct	gem_softc	gsc_gem;	/* GEM device */
80 	bus_space_tag_t		gsc_memt;
81 	bus_space_handle_t	gsc_memh;
82 	bus_size_t		gsc_memsize;
83 	void			*gsc_ih;
84 	pci_chipset_tag_t	gsc_pc;
85 };
86 
87 int	gem_match_pci(struct device *, void *, void *);
88 void	gem_attach_pci(struct device *, struct device *, void *);
89 int	gem_detach_pci(struct device *, int);
90 int	gem_pci_enaddr(struct gem_softc *, struct pci_attach_args *);
91 
92 struct cfattach gem_pci_ca = {
93 	sizeof(struct gem_pci_softc), gem_match_pci, gem_attach_pci,
94 	gem_detach_pci
95 };
96 
97 /*
98  * Attach routines need to be split out to different bus-specific files.
99  */
100 
101 const struct pci_matchid gem_pci_devices[] = {
102 	{ PCI_VENDOR_SUN, PCI_PRODUCT_SUN_ERINETWORK },
103 	{ PCI_VENDOR_SUN, PCI_PRODUCT_SUN_GEMNETWORK },
104 	{ PCI_VENDOR_APPLE, PCI_PRODUCT_APPLE_INTREPID2_GMAC },
105 	{ PCI_VENDOR_APPLE, PCI_PRODUCT_APPLE_K2_GMAC },
106 	{ PCI_VENDOR_APPLE, PCI_PRODUCT_APPLE_PANGEA_GMAC },
107 	{ PCI_VENDOR_APPLE, PCI_PRODUCT_APPLE_SHASTA_GMAC },
108 	{ PCI_VENDOR_APPLE, PCI_PRODUCT_APPLE_UNINORTHGMAC },
109 	{ PCI_VENDOR_APPLE, PCI_PRODUCT_APPLE_UNINORTH2GMAC },
110 };
111 
112 int
113 gem_match_pci(struct device *parent, void *cf, void *aux)
114 {
115 	return (pci_matchbyid((struct pci_attach_args *)aux, gem_pci_devices,
116 	    sizeof(gem_pci_devices)/sizeof(gem_pci_devices[0])));
117 }
118 
119 #define	PROMHDR_PTR_DATA	0x18
120 #define	PROMDATA_PTR_VPD	0x08
121 #define	PROMDATA_DATA2		0x0a
122 
123 static const u_int8_t gem_promhdr[] = { 0x55, 0xaa };
124 static const u_int8_t gem_promdat[] = {
125 	'P', 'C', 'I', 'R',
126 	PCI_VENDOR_SUN & 0xff, PCI_VENDOR_SUN >> 8,
127 	PCI_PRODUCT_SUN_GEMNETWORK & 0xff, PCI_PRODUCT_SUN_GEMNETWORK >> 8
128 };
129 
130 static const u_int8_t gem_promdat2[] = {
131 	0x18, 0x00,			/* structure length */
132 	0x00,				/* structure revision */
133 	0x00,				/* interface revision */
134 	PCI_SUBCLASS_NETWORK_ETHERNET,	/* subclass code */
135 	PCI_CLASS_NETWORK		/* class code */
136 };
137 
138 int
139 gem_pci_enaddr(struct gem_softc *sc, struct pci_attach_args *pa)
140 {
141 	struct pci_vpd *vpd;
142 	bus_space_handle_t romh;
143 	bus_space_tag_t romt;
144 	bus_size_t romsize = 0;
145 	u_int8_t buf[32];
146 	pcireg_t address;
147 	int dataoff, vpdoff;
148 	int rv = -1;
149 
150 	if (pci_mapreg_map(pa, PCI_ROM_REG, PCI_MAPREG_TYPE_MEM, 0,
151 	    &romt, &romh, 0, &romsize, 0))
152 		return (-1);
153 
154 	address = pci_conf_read(pa->pa_pc, pa->pa_tag, PCI_ROM_REG);
155 	address |= PCI_ROM_ENABLE;
156 	pci_conf_write(pa->pa_pc, pa->pa_tag, PCI_ROM_REG, address);
157 
158 	bus_space_read_region_1(romt, romh, 0, buf, sizeof(buf));
159 	if (bcmp(buf, gem_promhdr, sizeof(gem_promhdr)))
160 		goto fail;
161 
162 	dataoff = buf[PROMHDR_PTR_DATA] | (buf[PROMHDR_PTR_DATA + 1] << 8);
163 	if (dataoff < 0x1c)
164 		goto fail;
165 
166 	bus_space_read_region_1(romt, romh, dataoff, buf, sizeof(buf));
167 	if (bcmp(buf, gem_promdat, sizeof(gem_promdat)) ||
168 	    bcmp(buf + PROMDATA_DATA2, gem_promdat2, sizeof(gem_promdat2)))
169 		goto fail;
170 
171 	vpdoff = buf[PROMDATA_PTR_VPD] | (buf[PROMDATA_PTR_VPD + 1] << 8);
172 	if (vpdoff < 0x1c)
173 		goto fail;
174 
175 	bus_space_read_region_1(romt, romh, vpdoff, buf, sizeof(buf));
176 
177 	/*
178 	 * The VPD of gem is not in PCI 2.2 standard format.  The length
179 	 * in the resource header is in big endian.
180 	 */
181 	vpd = (struct pci_vpd *)(buf + 3);
182 	if (!PCI_VPDRES_ISLARGE(buf[0]) ||
183 	    PCI_VPDRES_LARGE_NAME(buf[0]) != PCI_VPDRES_TYPE_VPD)
184 		goto fail;
185 	if (vpd->vpd_key0 != 'N' || vpd->vpd_key1 != 'A')
186 		goto fail;
187 
188 	bcopy(buf + 6, sc->sc_arpcom.ac_enaddr, ETHER_ADDR_LEN);
189 	rv = 0;
190 
191  fail:
192 	if (romsize != 0)
193 		bus_space_unmap(romt, romh, romsize);
194 
195 	address = pci_conf_read(pa->pa_pc, pa->pa_tag, PCI_ROM_REG);
196 	address &= ~PCI_ROM_ENABLE;
197 	pci_conf_write(pa->pa_pc, pa->pa_tag, PCI_ROM_REG, address);
198 
199 	return (rv);
200 }
201 
202 void
203 gem_attach_pci(struct device *parent, struct device *self, void *aux)
204 {
205 	struct pci_attach_args *pa = aux;
206 	struct gem_pci_softc *gsc = (void *)self;
207 	struct gem_softc *sc = &gsc->gsc_gem;
208 	pci_intr_handle_t ih;
209 #ifdef __sparc64__
210 	/* XXX the following declarations should be elsewhere */
211 	extern void myetheraddr(u_char *);
212 #endif
213 	const char *intrstr = NULL;
214 	int type, gotenaddr = 0;
215 
216 	gsc->gsc_pc = pa->pa_pc;
217 
218 	if (pa->pa_memt) {
219 		type = PCI_MAPREG_TYPE_MEM;
220 		sc->sc_bustag = pa->pa_memt;
221 	} else {
222 		type = PCI_MAPREG_TYPE_IO;
223 		sc->sc_bustag = pa->pa_iot;
224 	}
225 
226 	sc->sc_dmatag = pa->pa_dmat;
227 
228 	sc->sc_pci = 1; /* XXXXX should all be done in bus_dma. */
229 
230 	if (PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_SUN_GEMNETWORK)
231 		sc->sc_variant = GEM_SUN_GEM;
232 	else if (PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_SUN_ERINETWORK)
233 		sc->sc_variant = GEM_SUN_ERI;
234 	else if (PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_APPLE_INTREPID2_GMAC)
235 		sc->sc_variant = GEM_APPLE_GMAC;
236 	else if (PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_APPLE_PANGEA_GMAC)
237 		sc->sc_variant = GEM_APPLE_GMAC;
238 	else if (PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_APPLE_SHASTA_GMAC)
239 		sc->sc_variant = GEM_APPLE_GMAC;
240 	else if (PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_APPLE_UNINORTHGMAC)
241 		sc->sc_variant = GEM_APPLE_GMAC;
242 	else if (PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_APPLE_UNINORTH2GMAC)
243 		sc->sc_variant = GEM_APPLE_GMAC;
244 	else if (PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_APPLE_K2_GMAC)
245 		sc->sc_variant = GEM_APPLE_K2_GMAC;
246 
247 #define PCI_GEM_BASEADDR	0x10
248 	if (pci_mapreg_map(pa, PCI_GEM_BASEADDR, type, 0,
249 	    &gsc->gsc_memt, &gsc->gsc_memh, NULL, &gsc->gsc_memsize, 0) != 0) {
250 		printf(": can't map registers\n");
251 		return;
252 	}
253 
254 	sc->sc_bustag = gsc->gsc_memt;
255 	sc->sc_h1 = gsc->gsc_memh;
256 
257 	if (bus_space_subregion(sc->sc_bustag, sc->sc_h1,
258 	    GEM_PCI_BANK2_OFFSET, GEM_PCI_BANK2_SIZE, &sc->sc_h2)) {
259 		printf(": unable to create bank 2 subregion\n");
260 		bus_space_unmap(gsc->gsc_memt, gsc->gsc_memh, gsc->gsc_memsize);
261 		return;
262 	}
263 
264 	if (gem_pci_enaddr(sc, pa) == 0)
265 		gotenaddr = 1;
266 
267 #ifdef __sparc64__
268 	if (!gotenaddr) {
269 		if (OF_getprop(PCITAG_NODE(pa->pa_tag), "local-mac-address",
270 		    sc->sc_arpcom.ac_enaddr, ETHER_ADDR_LEN) <= 0)
271 			myetheraddr(sc->sc_arpcom.ac_enaddr);
272 		gotenaddr = 1;
273 	}
274 #endif
275 #ifdef __powerpc__
276 	if (!gotenaddr) {
277 		pci_ether_hw_addr(pa->pa_pc, sc->sc_arpcom.ac_enaddr);
278 		gotenaddr = 1;
279 	}
280 #endif
281 
282 	sc->sc_burst = 16;	/* XXX */
283 
284 	if (pci_intr_map(pa, &ih) != 0) {
285 		printf(": couldn't map interrupt\n");
286 		bus_space_unmap(gsc->gsc_memt, gsc->gsc_memh, gsc->gsc_memsize);
287 		return;
288 	}
289 	intrstr = pci_intr_string(pa->pa_pc, ih);
290 	gsc->gsc_ih = pci_intr_establish(pa->pa_pc,
291 	    ih, IPL_NET, gem_intr, sc, self->dv_xname);
292 	if (gsc->gsc_ih == NULL) {
293 		printf(": couldn't establish interrupt");
294 		if (intrstr != NULL)
295 			printf(" at %s", intrstr);
296 		printf("\n");
297 		bus_space_unmap(gsc->gsc_memt, gsc->gsc_memh, gsc->gsc_memsize);
298 		return;
299 	}
300 
301 	printf(": %s", intrstr);
302 
303 	/*
304 	 * call the main configure
305 	 */
306 	gem_config(sc);
307 }
308 
309 int
310 gem_detach_pci(struct device *self, int flags)
311 {
312 	struct gem_pci_softc *gsc = (void *)self;
313 	struct gem_softc *sc = &gsc->gsc_gem;
314 
315 	timeout_del(&sc->sc_tick_ch);
316 	timeout_del(&sc->sc_rx_watchdog);
317 
318 	gem_unconfig(sc);
319 	pci_intr_disestablish(gsc->gsc_pc, gsc->gsc_ih);
320 	bus_space_unmap(gsc->gsc_memt, gsc->gsc_memh, gsc->gsc_memsize);
321 
322 	return (0);
323 }
324