xref: /netbsd-src/sys/dev/pci/if_gem_pci.c (revision b7ae68fde0d8ef1c03714e8bbb1ee7c6118ea93b)
1 /*	$NetBSD: if_gem_pci.c,v 1.21 2006/08/05 21:26:49 sanjayl 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 Sun GEM ethernet controllers.
34  */
35 
36 #include <sys/cdefs.h>
37 __KERNEL_RCSID(0, "$NetBSD: if_gem_pci.c,v 1.21 2006/08/05 21:26:49 sanjayl 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 <machine/bus.h>
61 #include <machine/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 struct gem_pci_softc {
83 	struct	gem_softc	gsc_gem;	/* GEM device */
84 	void			*gsc_ih;
85 };
86 
87 int	gem_match_pci(struct device *, struct cfdata *, void *);
88 void	gem_attach_pci(struct device *, struct device *, void *);
89 
90 CFATTACH_DECL(gem_pci, sizeof(struct gem_pci_softc),
91     gem_match_pci, gem_attach_pci, NULL, NULL);
92 
93 /*
94  * Attach routines need to be split out to different bus-specific files.
95  */
96 
97 int
98 gem_match_pci(parent, cf, aux)
99 	struct device *parent;
100 	struct cfdata *cf;
101 	void *aux;
102 {
103 	struct pci_attach_args *pa = aux;
104 
105 	if (PCI_VENDOR(pa->pa_id) == PCI_VENDOR_SUN &&
106 	    (PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_SUN_ERINETWORK ||
107 	     PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_SUN_GEMNETWORK))
108 		return (1);
109 
110 	if (PCI_VENDOR(pa->pa_id) == PCI_VENDOR_APPLE &&
111 	    (PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_APPLE_GMAC ||
112 	     PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_APPLE_GMAC2 ||
113 	     PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_APPLE_GMAC3 ||
114 	     PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_APPLE_K2_GMAC))
115 		return (1);
116 
117 
118 	return (0);
119 }
120 
121 void
122 gem_attach_pci(parent, self, aux)
123 	struct device *parent, *self;
124 	void *aux;
125 {
126 	struct pci_attach_args *pa = aux;
127 	struct gem_pci_softc *gsc = (void *)self;
128 	struct gem_softc *sc = &gsc->gsc_gem;
129 	pci_intr_handle_t ih;
130 	const char *intrstr;
131 	char devinfo[256];
132 	uint8_t enaddr[ETHER_ADDR_LEN];
133 
134 	aprint_naive(": Ethernet controller\n");
135 
136 	pci_devinfo(pa->pa_id, pa->pa_class, 0, devinfo, sizeof(devinfo));
137 	aprint_normal(": %s (rev. 0x%02x)\n", devinfo,
138 	    PCI_REVISION(pa->pa_class));
139 
140 	sc->sc_dmatag = pa->pa_dmat;
141 
142 	sc->sc_pci = 1;		/* XXX */
143 
144 	if (PCI_VENDOR(pa->pa_id) == PCI_VENDOR_SUN &&
145 	    (PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_SUN_ERINETWORK ||
146 	     PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_SUN_GEMNETWORK))
147 		sc->sc_variant = GEM_SUN_GEM;
148 	else if (PCI_VENDOR(pa->pa_id) == PCI_VENDOR_APPLE &&
149 	    (PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_APPLE_GMAC ||
150 	     PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_APPLE_GMAC2 ||
151 	     PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_APPLE_K2_GMAC))
152 		sc->sc_variant = GEM_APPLE_GMAC;
153 
154 #define PCI_GEM_BASEADDR	(PCI_MAPREG_START + 0x00)
155 
156 	/* XXX Need to check for a 64-bit mem BAR? */
157 	if (pci_mapreg_map(pa, PCI_GEM_BASEADDR,
158 	    PCI_MAPREG_TYPE_MEM | PCI_MAPREG_MEM_TYPE_32BIT, 0,
159 	    &sc->sc_bustag, &sc->sc_h, NULL, NULL) != 0)
160 	{
161 		aprint_error("%s: unable to map device registers\n",
162 		    sc->sc_dev.dv_xname);
163 		return;
164 	}
165 
166 	/*
167 	 * XXX This should be done with properties, when those are
168 	 * XXX fleshed out.
169 	 */
170 #ifdef __sparc__
171 	{
172 		prom_getether(PCITAG_NODE(pa->pa_tag), enaddr);
173 	}
174 #endif /* __sparc__ */
175 #ifdef macppc
176 	{
177 		int node;
178 
179 		node = pcidev_to_ofdev(pa->pa_pc, pa->pa_tag);
180 		if (node == 0) {
181 			aprint_error("%s: unable to locate OpenFirmware node\n",
182 			    sc->sc_dev.dv_xname);
183 			return;
184 		}
185 
186 		OF_getprop(node, "local-mac-address", enaddr, sizeof(enaddr));
187 	}
188 #endif /* macppc */
189 
190 	if (pci_intr_map(pa, &ih) != 0) {
191 		aprint_error("%s: unable to map interrupt\n",
192 		    sc->sc_dev.dv_xname);
193 		return;
194 	}
195 	intrstr = pci_intr_string(pa->pa_pc, ih);
196 	gsc->gsc_ih = pci_intr_establish(pa->pa_pc, ih, IPL_NET, gem_intr, sc);
197 	if (gsc->gsc_ih == NULL) {
198 		aprint_error("%s: unable to establish interrupt",
199 		    sc->sc_dev.dv_xname);
200 		if (intrstr != NULL)
201 			aprint_normal(" at %s", intrstr);
202 		aprint_normal("\n");
203 		return;
204 	}
205 	aprint_normal("%s: interrupting at %s\n", sc->sc_dev.dv_xname, intrstr);
206 
207 	/* Finish off the attach. */
208 	gem_attach(sc, enaddr);
209 }
210