xref: /netbsd/sys/dev/pci/if_ne_pci.c (revision bf9ec67e)
1 /*	$NetBSD: if_ne_pci.c,v 1.21 2001/11/13 07:48:44 lukem Exp $	*/
2 
3 /*-
4  * Copyright (c) 1997, 1998 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
9  * NASA Ames Research Center.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  * 1. Redistributions of source code must retain the above copyright
15  *    notice, this list of conditions and the following disclaimer.
16  * 2. Redistributions in binary form must reproduce the above copyright
17  *    notice, this list of conditions and the following disclaimer in the
18  *    documentation and/or other materials provided with the distribution.
19  * 3. All advertising materials mentioning features or use of this software
20  *    must display the following acknowledgement:
21  *	This product includes software developed by the NetBSD
22  *	Foundation, Inc. and its contributors.
23  * 4. Neither the name of The NetBSD Foundation nor the names of its
24  *    contributors may be used to endorse or promote products derived
25  *    from this software without specific prior written permission.
26  *
27  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
28  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
29  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
30  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
31  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
32  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
33  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
34  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
35  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
36  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
37  * POSSIBILITY OF SUCH DAMAGE.
38  */
39 
40 #include <sys/cdefs.h>
41 __KERNEL_RCSID(0, "$NetBSD: if_ne_pci.c,v 1.21 2001/11/13 07:48:44 lukem Exp $");
42 
43 #include "opt_ipkdb.h"
44 
45 #include <sys/param.h>
46 #include <sys/systm.h>
47 #include <sys/mbuf.h>
48 #include <sys/syslog.h>
49 #include <sys/socket.h>
50 #include <sys/device.h>
51 
52 #include <net/if.h>
53 #include <net/if_ether.h>
54 #include <net/if_media.h>
55 
56 #include <machine/bus.h>
57 #include <machine/intr.h>
58 
59 #ifdef IPKDB_NE_PCI
60 #include <ipkdb/ipkdb.h>
61 #endif
62 
63 #include <dev/pci/pcireg.h>
64 #include <dev/pci/pcivar.h>
65 #include <dev/pci/pcidevs.h>
66 
67 #include <dev/ic/dp8390reg.h>
68 #include <dev/ic/dp8390var.h>
69 
70 #include <dev/ic/ne2000reg.h>
71 #include <dev/ic/ne2000var.h>
72 
73 #include <dev/ic/rtl80x9reg.h>
74 #include <dev/ic/rtl80x9var.h>
75 
76 struct ne_pci_softc {
77 	struct ne2000_softc sc_ne2000;		/* real "ne2000" softc */
78 
79 	/* PCI-specific goo */
80 	void *sc_ih;				/* interrupt handle */
81 };
82 
83 int ne_pci_match __P((struct device *, struct cfdata *, void *));
84 void ne_pci_attach __P((struct device *, struct device *, void *));
85 
86 struct cfattach ne_pci_ca = {
87 	sizeof(struct ne_pci_softc), ne_pci_match, ne_pci_attach
88 };
89 
90 #ifdef IPKDB_NE_PCI
91 static struct ne_pci_softc ipkdb_softc;
92 static pci_chipset_tag_t ipkdb_pc;
93 static pcitag_t ipkdb_tag;
94 static struct ipkdb_if *ne_kip;
95 
96 int ne_pci_ipkdb_attach __P((struct ipkdb_if *, bus_space_tag_t,       /* XXX */
97 			pci_chipset_tag_t, int, int));
98 
99 static int ne_pci_isipkdb __P((pci_chipset_tag_t, pcitag_t));
100 #endif
101 
102 const struct ne_pci_product {
103 	pci_vendor_id_t npp_vendor;
104 	pci_product_id_t npp_product;
105 	int (*npp_mediachange) __P((struct dp8390_softc *));
106 	void (*npp_mediastatus) __P((struct dp8390_softc *,
107 	    struct ifmediareq *));
108 	void (*npp_init_card) __P((struct dp8390_softc *));
109 	void (*npp_media_init) __P((struct dp8390_softc *));
110 	const char *npp_name;
111 } ne_pci_products[] = {
112 	{ PCI_VENDOR_REALTEK,		PCI_PRODUCT_REALTEK_RT8029,
113 	  rtl80x9_mediachange,		rtl80x9_mediastatus,
114 	  rtl80x9_init_card,		rtl80x9_media_init,
115 	  "RealTek 8029" },
116 
117 	{ PCI_VENDOR_WINBOND,		PCI_PRODUCT_WINBOND_W89C940F,
118 	  NULL,				NULL,
119 	  NULL,				NULL,
120 	  "Winbond 89C940F" },
121 
122 	{ PCI_VENDOR_WINBOND,		PCI_PRODUCT_WINBOND_W89C940F_1,
123 	  NULL,				NULL,
124 	  NULL,				NULL,
125 	  "Winbond 89C940F" },
126 
127 	{ PCI_VENDOR_VIATECH,		PCI_PRODUCT_VIATECH_VT86C926,
128 	  NULL,				NULL,
129 	  NULL,				NULL,
130 	  "VIA Technologies VT86C926" },
131 
132 	{ PCI_VENDOR_SURECOM,		PCI_PRODUCT_SURECOM_NE34,
133 	  NULL,				NULL,
134 	  NULL,				NULL,
135 	  "Surecom NE-34" },
136 
137 	{ PCI_VENDOR_NETVIN,		PCI_PRODUCT_NETVIN_5000,
138 	  NULL,				NULL,
139 	  NULL,				NULL,
140 	  "NetVin 5000" },
141 
142 	/* XXX The following entries need sanity checking in pcidevs */
143 	{ PCI_VENDOR_COMPEX,		PCI_PRODUCT_COMPEX_NE2KETHER,
144 	  NULL,				NULL,
145 	  NULL,				NULL,
146 	  "Compex" },
147 
148 	{ PCI_VENDOR_PROLAN,		PCI_PRODUCT_PROLAN_NE2KETHER,
149 	  NULL,				NULL,
150 	  NULL,				NULL,
151 	  "ProLAN" },
152 
153 	{ PCI_VENDOR_KTI,		PCI_PRODUCT_KTI_NE2KETHER,
154 	  NULL,				NULL,
155 	  NULL,				NULL,
156 	  "KTI" },
157 
158 	{ 0,				0,
159 	  NULL,				NULL,
160 	  NULL,				NULL,
161 	  NULL },
162 };
163 
164 const struct ne_pci_product *ne_pci_lookup
165     __P((const struct pci_attach_args *));
166 
167 const struct ne_pci_product *
168 ne_pci_lookup(pa)
169 	const struct pci_attach_args *pa;
170 {
171 	const struct ne_pci_product *npp;
172 
173 	for (npp = ne_pci_products; npp->npp_name != NULL; npp++) {
174 		if (PCI_VENDOR(pa->pa_id) == npp->npp_vendor &&
175 		    PCI_PRODUCT(pa->pa_id) == npp->npp_product)
176 			return (npp);
177 	}
178 	return (NULL);
179 }
180 
181 /*
182  * PCI constants.
183  * XXX These should be in a common file!
184  */
185 #define PCI_CBIO	0x10		/* Configuration Base IO Address */
186 
187 int
188 ne_pci_match(parent, match, aux)
189 	struct device *parent;
190 	struct cfdata *match;
191 	void *aux;
192 {
193 	struct pci_attach_args *pa = aux;
194 
195 	if (ne_pci_lookup(pa) != NULL)
196 		return (1);
197 
198 	return (0);
199 }
200 
201 void
202 ne_pci_attach(parent, self, aux)
203 	struct device *parent, *self;
204 	void *aux;
205 {
206 	struct ne_pci_softc *psc = (struct ne_pci_softc *)self;
207 	struct ne2000_softc *nsc = &psc->sc_ne2000;
208 	struct dp8390_softc *dsc = &nsc->sc_dp8390;
209 	struct pci_attach_args *pa = aux;
210 	pci_chipset_tag_t pc = pa->pa_pc;
211 	bus_space_tag_t nict;
212 	bus_space_handle_t nich;
213 	bus_space_tag_t asict;
214 	bus_space_handle_t asich;
215 	const char *intrstr;
216 	const struct ne_pci_product *npp;
217 	pci_intr_handle_t ih;
218 	pcireg_t csr;
219 
220 	npp = ne_pci_lookup(pa);
221 	if (npp == NULL) {
222 		printf("\n");
223 		panic("ne_pci_attach: impossible");
224 	}
225 
226 	printf(": %s Ethernet\n", npp->npp_name);
227 
228 #ifdef IPKDB_NE_PCI
229 	if (ne_pci_isipkdb(pc, pa->pa_tag)) {
230 		nict = ipkdb_softc.sc_ne2000.sc_dp8390.sc_regt;
231 		nich = ipkdb_softc.sc_ne2000.sc_dp8390.sc_regh;
232 		ne_kip->port = nsc;
233 	} else
234 #endif
235 	if (pci_mapreg_map(pa, PCI_CBIO, PCI_MAPREG_TYPE_IO, 0,
236 	    &nict, &nich, NULL, NULL)) {
237 		printf("%s: can't map i/o space\n", dsc->sc_dev.dv_xname);
238 		return;
239 	}
240 
241 	asict = nict;
242 	if (bus_space_subregion(nict, nich, NE2000_ASIC_OFFSET,
243 	    NE2000_ASIC_NPORTS, &asich)) {
244 		printf("%s: can't subregion i/o space\n", dsc->sc_dev.dv_xname);
245 		return;
246 	}
247 
248 	dsc->sc_regt = nict;
249 	dsc->sc_regh = nich;
250 
251 	nsc->sc_asict = asict;
252 	nsc->sc_asich = asich;
253 
254 	/* Enable the card. */
255 	csr = pci_conf_read(pc, pa->pa_tag,
256 	    PCI_COMMAND_STATUS_REG);
257 	pci_conf_write(pc, pa->pa_tag, PCI_COMMAND_STATUS_REG,
258 	    csr | PCI_COMMAND_MASTER_ENABLE);
259 
260 	/* This interface is always enabled. */
261 	dsc->sc_enabled = 1;
262 
263 	dsc->sc_mediachange = npp->npp_mediachange;
264 	dsc->sc_mediastatus = npp->npp_mediastatus;
265 	dsc->sc_media_init = npp->npp_media_init;
266 	dsc->init_card = npp->npp_init_card;
267 
268 	/*
269 	 * Do generic NE2000 attach.  This will read the station address
270 	 * from the EEPROM.
271 	 */
272 	ne2000_attach(nsc, NULL);
273 
274 	/* Map and establish the interrupt. */
275 	if (pci_intr_map(pa, &ih)) {
276 		printf("%s: couldn't map interrupt\n", dsc->sc_dev.dv_xname);
277 		return;
278 	}
279 	intrstr = pci_intr_string(pc, ih);
280 	psc->sc_ih = pci_intr_establish(pc, ih, IPL_NET, dp8390_intr, dsc);
281 	if (psc->sc_ih == NULL) {
282 		printf("%s: couldn't establish interrupt",
283 		    dsc->sc_dev.dv_xname);
284 		if (intrstr != NULL)
285 			printf(" at %s", intrstr);
286 		printf("\n");
287 		return;
288 	}
289 	printf("%s: interrupting at %s\n", dsc->sc_dev.dv_xname, intrstr);
290 }
291 
292 #ifdef IPKDB_NE_PCI
293 static int
294 ne_pci_isipkdb(pc, tag)
295 	pci_chipset_tag_t pc;
296 	pcitag_t tag;
297 {
298 	return !memcmp(&pc, &ipkdb_pc, sizeof pc)
299 		&& !memcmp(&tag, &ipkdb_tag, sizeof tag);
300 }
301 
302 int
303 ne_pci_ipkdb_attach(kip, iot, pc, bus, dev)
304 	struct ipkdb_if *kip;
305 	bus_space_tag_t iot;
306 	pci_chipset_tag_t pc;
307 	int bus, dev;
308 {
309 	struct pci_attach_args pa;
310 	bus_space_tag_t nict, asict;
311 	bus_space_handle_t nich, asich;
312 	u_int32_t csr;
313 
314 	pa.pa_iot = iot;
315 	pa.pa_pc = pc;
316 	pa.pa_device = dev;
317 	pa.pa_function = 0;
318 	pa.pa_flags = PCI_FLAGS_IO_ENABLED;
319 	pa.pa_tag = pci_make_tag(pc, bus, dev, /*func*/0);
320 	pa.pa_id = pci_conf_read(pc, pa.pa_tag, PCI_ID_REG);
321 	pa.pa_class = pci_conf_read(pc, pa.pa_tag, PCI_CLASS_REG);
322 	if (ne_pci_lookup(&pa) == NULL)
323 		return -1;
324 
325 	if (pci_mapreg_map(&pa, PCI_CBIO, PCI_MAPREG_TYPE_IO, 0,
326 			&nict, &nich, NULL, NULL))
327 		return -1;
328 
329 	asict = nict;
330 	if (bus_space_subregion(nict, nich, NE2000_ASIC_OFFSET,
331 				NE2000_ASIC_NPORTS, &asich)) {
332 		bus_space_unmap(nict, nich, NE2000_NPORTS);
333 		return -1;
334 	}
335 
336 	/* Enable card */
337 	csr = pci_conf_read(pc, pa.pa_tag, PCI_COMMAND_STATUS_REG);
338 	pci_conf_write(pc, pa.pa_tag, PCI_COMMAND_STATUS_REG,
339 			csr | PCI_COMMAND_MASTER_ENABLE);
340 
341 	ipkdb_softc.sc_ne2000.sc_dp8390.sc_regt = nict;
342 	ipkdb_softc.sc_ne2000.sc_dp8390.sc_regh = nich;
343 	ipkdb_softc.sc_ne2000.sc_asict = asict;
344 	ipkdb_softc.sc_ne2000.sc_asich = asich;
345 
346 	kip->port = &ipkdb_softc;
347 	ipkdb_pc = pc;
348 	ipkdb_tag = pa.pa_tag;
349 	ne_kip = kip;
350 
351 	if (ne2000_ipkdb_attach(kip) < 0) {
352 		bus_space_unmap(nict, nich, NE2000_NPORTS);
353 		return -1;
354 	}
355 
356 	return 0;
357 }
358 #endif
359