xref: /openbsd-src/sys/dev/pci/if_gem_pci.c (revision 91f110e064cd7c194e59e019b83bb7496c1c84d4)
1 /*	$OpenBSD: if_gem_pci.c,v 1.34 2012/10/16 10:58:04 jsg 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 #include <machine/bus.h>
57 #include <machine/intr.h>
58 
59 #ifdef __sparc64__
60 #include <dev/ofw/openfirm.h>
61 #endif
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 struct gem_pci_softc {
75 	struct	gem_softc	gsc_gem;	/* GEM device */
76 	bus_space_tag_t		gsc_memt;
77 	bus_space_handle_t	gsc_memh;
78 	bus_size_t		gsc_memsize;
79 	void			*gsc_ih;
80 	pci_chipset_tag_t	gsc_pc;
81 };
82 
83 int	gem_match_pci(struct device *, void *, void *);
84 void	gem_attach_pci(struct device *, struct device *, void *);
85 int	gem_detach_pci(struct device *, int);
86 int	gem_pci_enaddr(struct gem_softc *, struct pci_attach_args *);
87 
88 struct cfattach gem_pci_ca = {
89 	sizeof(struct gem_pci_softc), gem_match_pci, gem_attach_pci,
90 	gem_detach_pci
91 };
92 
93 /*
94  * Attach routines need to be split out to different bus-specific files.
95  */
96 
97 const struct pci_matchid gem_pci_devices[] = {
98 	{ PCI_VENDOR_SUN, PCI_PRODUCT_SUN_ERINETWORK },
99 	{ PCI_VENDOR_SUN, PCI_PRODUCT_SUN_GEMNETWORK },
100 	{ PCI_VENDOR_APPLE, PCI_PRODUCT_APPLE_INTREPID2_GMAC },
101 	{ PCI_VENDOR_APPLE, PCI_PRODUCT_APPLE_K2_GMAC },
102 	{ PCI_VENDOR_APPLE, PCI_PRODUCT_APPLE_PANGEA_GMAC },
103 	{ PCI_VENDOR_APPLE, PCI_PRODUCT_APPLE_SHASTA_GMAC },
104 	{ PCI_VENDOR_APPLE, PCI_PRODUCT_APPLE_UNINORTHGMAC },
105 	{ PCI_VENDOR_APPLE, PCI_PRODUCT_APPLE_UNINORTH2GMAC },
106 };
107 
108 int
109 gem_match_pci(struct device *parent, void *cf, void *aux)
110 {
111 	return (pci_matchbyid((struct pci_attach_args *)aux, gem_pci_devices,
112 	    nitems(gem_pci_devices)));
113 }
114 
115 #define	PROMHDR_PTR_DATA	0x18
116 #define	PROMDATA_PTR_VPD	0x08
117 #define	PROMDATA_DATA2		0x0a
118 
119 static const u_int8_t gem_promhdr[] = { 0x55, 0xaa };
120 static const u_int8_t gem_promdat[] = {
121 	'P', 'C', 'I', 'R',
122 	PCI_VENDOR_SUN & 0xff, PCI_VENDOR_SUN >> 8,
123 	PCI_PRODUCT_SUN_GEMNETWORK & 0xff, PCI_PRODUCT_SUN_GEMNETWORK >> 8
124 };
125 
126 static const u_int8_t gem_promdat2[] = {
127 	0x18, 0x00,			/* structure length */
128 	0x00,				/* structure revision */
129 	0x00,				/* interface revision */
130 	PCI_SUBCLASS_NETWORK_ETHERNET,	/* subclass code */
131 	PCI_CLASS_NETWORK		/* class code */
132 };
133 
134 int
135 gem_pci_enaddr(struct gem_softc *sc, struct pci_attach_args *pa)
136 {
137 	struct pci_vpd *vpd;
138 	bus_space_handle_t romh;
139 	bus_space_tag_t romt;
140 	bus_size_t romsize = 0;
141 	u_int8_t buf[32];
142 	pcireg_t address;
143 	int dataoff, vpdoff;
144 	int rv = -1;
145 
146 	if (pci_mapreg_map(pa, PCI_ROM_REG, PCI_MAPREG_TYPE_MEM, 0,
147 	    &romt, &romh, 0, &romsize, 0))
148 		return (-1);
149 
150 	address = pci_conf_read(pa->pa_pc, pa->pa_tag, PCI_ROM_REG);
151 	address |= PCI_ROM_ENABLE;
152 	pci_conf_write(pa->pa_pc, pa->pa_tag, PCI_ROM_REG, address);
153 
154 	bus_space_read_region_1(romt, romh, 0, buf, sizeof(buf));
155 	if (bcmp(buf, gem_promhdr, sizeof(gem_promhdr)))
156 		goto fail;
157 
158 	dataoff = buf[PROMHDR_PTR_DATA] | (buf[PROMHDR_PTR_DATA + 1] << 8);
159 	if (dataoff < 0x1c)
160 		goto fail;
161 
162 	bus_space_read_region_1(romt, romh, dataoff, buf, sizeof(buf));
163 	if (bcmp(buf, gem_promdat, sizeof(gem_promdat)) ||
164 	    bcmp(buf + PROMDATA_DATA2, gem_promdat2, sizeof(gem_promdat2)))
165 		goto fail;
166 
167 	vpdoff = buf[PROMDATA_PTR_VPD] | (buf[PROMDATA_PTR_VPD + 1] << 8);
168 	if (vpdoff < 0x1c)
169 		goto fail;
170 
171 	bus_space_read_region_1(romt, romh, vpdoff, buf, sizeof(buf));
172 
173 	/*
174 	 * The VPD of gem is not in PCI 2.2 standard format.  The length
175 	 * in the resource header is in big endian.
176 	 */
177 	vpd = (struct pci_vpd *)(buf + 3);
178 	if (!PCI_VPDRES_ISLARGE(buf[0]) ||
179 	    PCI_VPDRES_LARGE_NAME(buf[0]) != PCI_VPDRES_TYPE_VPD)
180 		goto fail;
181 	if (vpd->vpd_key0 != 'N' || vpd->vpd_key1 != 'A')
182 		goto fail;
183 
184 	bcopy(buf + 6, sc->sc_arpcom.ac_enaddr, ETHER_ADDR_LEN);
185 	rv = 0;
186 
187  fail:
188 	if (romsize != 0)
189 		bus_space_unmap(romt, romh, romsize);
190 
191 	address = pci_conf_read(pa->pa_pc, pa->pa_tag, PCI_ROM_REG);
192 	address &= ~PCI_ROM_ENABLE;
193 	pci_conf_write(pa->pa_pc, pa->pa_tag, PCI_ROM_REG, address);
194 
195 	return (rv);
196 }
197 
198 void
199 gem_attach_pci(struct device *parent, struct device *self, void *aux)
200 {
201 	struct pci_attach_args *pa = aux;
202 	struct gem_pci_softc *gsc = (void *)self;
203 	struct gem_softc *sc = &gsc->gsc_gem;
204 	pci_intr_handle_t ih;
205 #ifdef __sparc64__
206 	/* XXX the following declarations should be elsewhere */
207 	extern void myetheraddr(u_char *);
208 #endif
209 	const char *intrstr = NULL;
210 	int type, gotenaddr = 0;
211 
212 	gsc->gsc_pc = pa->pa_pc;
213 
214 	if (pa->pa_memt) {
215 		type = PCI_MAPREG_TYPE_MEM;
216 		sc->sc_bustag = pa->pa_memt;
217 	} else {
218 		type = PCI_MAPREG_TYPE_IO;
219 		sc->sc_bustag = pa->pa_iot;
220 	}
221 
222 	sc->sc_dmatag = pa->pa_dmat;
223 
224 	sc->sc_pci = 1; /* XXXXX should all be done in bus_dma. */
225 
226 	switch (PCI_PRODUCT(pa->pa_id)) {
227 	case PCI_PRODUCT_SUN_GEMNETWORK:
228 		sc->sc_variant = GEM_SUN_GEM;
229 		break;
230 	case PCI_PRODUCT_SUN_ERINETWORK:
231 		sc->sc_variant = GEM_SUN_ERI;
232 		break;
233 	case PCI_PRODUCT_APPLE_K2_GMAC:
234 		sc->sc_variant = GEM_APPLE_K2_GMAC;
235 		break;
236 	default:
237 		sc->sc_variant = GEM_APPLE_GMAC;
238 	}
239 
240 #define PCI_GEM_BASEADDR	0x10
241 	if (pci_mapreg_map(pa, PCI_GEM_BASEADDR, type, 0,
242 	    &gsc->gsc_memt, &gsc->gsc_memh, NULL, &gsc->gsc_memsize, 0) != 0) {
243 		printf(": can't map registers\n");
244 		return;
245 	}
246 
247 	sc->sc_bustag = gsc->gsc_memt;
248 	sc->sc_h1 = gsc->gsc_memh;
249 
250 	if (bus_space_subregion(sc->sc_bustag, sc->sc_h1,
251 	    GEM_PCI_BANK2_OFFSET, GEM_PCI_BANK2_SIZE, &sc->sc_h2)) {
252 		printf(": unable to create bank 2 subregion\n");
253 		bus_space_unmap(gsc->gsc_memt, gsc->gsc_memh, gsc->gsc_memsize);
254 		return;
255 	}
256 
257 	if (gem_pci_enaddr(sc, pa) == 0)
258 		gotenaddr = 1;
259 
260 #ifdef __sparc64__
261 	if (!gotenaddr) {
262 		if (OF_getprop(PCITAG_NODE(pa->pa_tag), "local-mac-address",
263 		    sc->sc_arpcom.ac_enaddr, ETHER_ADDR_LEN) <= 0)
264 			myetheraddr(sc->sc_arpcom.ac_enaddr);
265 		gotenaddr = 1;
266 	}
267 #endif
268 #ifdef __powerpc__
269 	if (!gotenaddr) {
270 		pci_ether_hw_addr(pa->pa_pc, sc->sc_arpcom.ac_enaddr);
271 		gotenaddr = 1;
272 	}
273 #endif
274 
275 	sc->sc_burst = 16;	/* XXX */
276 
277 	if (pci_intr_map(pa, &ih) != 0) {
278 		printf(": couldn't map interrupt\n");
279 		bus_space_unmap(gsc->gsc_memt, gsc->gsc_memh, gsc->gsc_memsize);
280 		return;
281 	}
282 	intrstr = pci_intr_string(pa->pa_pc, ih);
283 	gsc->gsc_ih = pci_intr_establish(pa->pa_pc,
284 	    ih, IPL_NET, gem_intr, sc, self->dv_xname);
285 	if (gsc->gsc_ih == NULL) {
286 		printf(": couldn't establish interrupt");
287 		if (intrstr != NULL)
288 			printf(" at %s", intrstr);
289 		printf("\n");
290 		bus_space_unmap(gsc->gsc_memt, gsc->gsc_memh, gsc->gsc_memsize);
291 		return;
292 	}
293 
294 	printf(": %s", intrstr);
295 
296 	/*
297 	 * call the main configure
298 	 */
299 	gem_config(sc);
300 }
301 
302 int
303 gem_detach_pci(struct device *self, int flags)
304 {
305 	struct gem_pci_softc *gsc = (void *)self;
306 	struct gem_softc *sc = &gsc->gsc_gem;
307 
308 	timeout_del(&sc->sc_tick_ch);
309 	timeout_del(&sc->sc_rx_watchdog);
310 
311 	gem_unconfig(sc);
312 	pci_intr_disestablish(gsc->gsc_pc, gsc->gsc_ih);
313 	bus_space_unmap(gsc->gsc_memt, gsc->gsc_memh, gsc->gsc_memsize);
314 
315 	return (0);
316 }
317