xref: /netbsd-src/sys/dev/pci/if_ne_pci.c (revision 27578b9aac214cc7796ead81dcc5427e79d5f2a0)
1 /*	$NetBSD: if_ne_pci.c,v 1.20 2001/07/08 18:02:28 thorpej 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 "opt_ipkdb.h"
41 
42 #include <sys/param.h>
43 #include <sys/systm.h>
44 #include <sys/mbuf.h>
45 #include <sys/syslog.h>
46 #include <sys/socket.h>
47 #include <sys/device.h>
48 
49 #include <net/if.h>
50 #include <net/if_ether.h>
51 #include <net/if_media.h>
52 
53 #include <machine/bus.h>
54 #include <machine/intr.h>
55 
56 #ifdef IPKDB_NE_PCI
57 #include <ipkdb/ipkdb.h>
58 #endif
59 
60 #include <dev/pci/pcireg.h>
61 #include <dev/pci/pcivar.h>
62 #include <dev/pci/pcidevs.h>
63 
64 #include <dev/ic/dp8390reg.h>
65 #include <dev/ic/dp8390var.h>
66 
67 #include <dev/ic/ne2000reg.h>
68 #include <dev/ic/ne2000var.h>
69 
70 #include <dev/ic/rtl80x9reg.h>
71 #include <dev/ic/rtl80x9var.h>
72 
73 struct ne_pci_softc {
74 	struct ne2000_softc sc_ne2000;		/* real "ne2000" softc */
75 
76 	/* PCI-specific goo */
77 	void *sc_ih;				/* interrupt handle */
78 };
79 
80 int ne_pci_match __P((struct device *, struct cfdata *, void *));
81 void ne_pci_attach __P((struct device *, struct device *, void *));
82 
83 struct cfattach ne_pci_ca = {
84 	sizeof(struct ne_pci_softc), ne_pci_match, ne_pci_attach
85 };
86 
87 #ifdef IPKDB_NE_PCI
88 static struct ne_pci_softc ipkdb_softc;
89 static pci_chipset_tag_t ipkdb_pc;
90 static pcitag_t ipkdb_tag;
91 static struct ipkdb_if *ne_kip;
92 
93 int ne_pci_ipkdb_attach __P((struct ipkdb_if *, bus_space_tag_t,       /* XXX */
94 			pci_chipset_tag_t, int, int));
95 
96 static int ne_pci_isipkdb __P((pci_chipset_tag_t, pcitag_t));
97 #endif
98 
99 const struct ne_pci_product {
100 	pci_vendor_id_t npp_vendor;
101 	pci_product_id_t npp_product;
102 	int (*npp_mediachange) __P((struct dp8390_softc *));
103 	void (*npp_mediastatus) __P((struct dp8390_softc *,
104 	    struct ifmediareq *));
105 	void (*npp_init_card) __P((struct dp8390_softc *));
106 	void (*npp_media_init) __P((struct dp8390_softc *));
107 	const char *npp_name;
108 } ne_pci_products[] = {
109 	{ PCI_VENDOR_REALTEK,		PCI_PRODUCT_REALTEK_RT8029,
110 	  rtl80x9_mediachange,		rtl80x9_mediastatus,
111 	  rtl80x9_init_card,		rtl80x9_media_init,
112 	  "RealTek 8029" },
113 
114 	{ PCI_VENDOR_WINBOND,		PCI_PRODUCT_WINBOND_W89C940F,
115 	  NULL,				NULL,
116 	  NULL,				NULL,
117 	  "Winbond 89C940F" },
118 
119 	{ PCI_VENDOR_WINBOND,		PCI_PRODUCT_WINBOND_W89C940F_1,
120 	  NULL,				NULL,
121 	  NULL,				NULL,
122 	  "Winbond 89C940F" },
123 
124 	{ PCI_VENDOR_VIATECH,		PCI_PRODUCT_VIATECH_VT86C926,
125 	  NULL,				NULL,
126 	  NULL,				NULL,
127 	  "VIA Technologies VT86C926" },
128 
129 	{ PCI_VENDOR_SURECOM,		PCI_PRODUCT_SURECOM_NE34,
130 	  NULL,				NULL,
131 	  NULL,				NULL,
132 	  "Surecom NE-34" },
133 
134 	{ PCI_VENDOR_NETVIN,		PCI_PRODUCT_NETVIN_5000,
135 	  NULL,				NULL,
136 	  NULL,				NULL,
137 	  "NetVin 5000" },
138 
139 	/* XXX The following entries need sanity checking in pcidevs */
140 	{ PCI_VENDOR_COMPEX,		PCI_PRODUCT_COMPEX_NE2KETHER,
141 	  NULL,				NULL,
142 	  NULL,				NULL,
143 	  "Compex" },
144 
145 	{ PCI_VENDOR_PROLAN,		PCI_PRODUCT_PROLAN_NE2KETHER,
146 	  NULL,				NULL,
147 	  NULL,				NULL,
148 	  "ProLAN" },
149 
150 	{ PCI_VENDOR_KTI,		PCI_PRODUCT_KTI_NE2KETHER,
151 	  NULL,				NULL,
152 	  NULL,				NULL,
153 	  "KTI" },
154 
155 	{ 0,				0,
156 	  NULL,				NULL,
157 	  NULL,				NULL,
158 	  NULL },
159 };
160 
161 const struct ne_pci_product *ne_pci_lookup
162     __P((const struct pci_attach_args *));
163 
164 const struct ne_pci_product *
165 ne_pci_lookup(pa)
166 	const struct pci_attach_args *pa;
167 {
168 	const struct ne_pci_product *npp;
169 
170 	for (npp = ne_pci_products; npp->npp_name != NULL; npp++) {
171 		if (PCI_VENDOR(pa->pa_id) == npp->npp_vendor &&
172 		    PCI_PRODUCT(pa->pa_id) == npp->npp_product)
173 			return (npp);
174 	}
175 	return (NULL);
176 }
177 
178 /*
179  * PCI constants.
180  * XXX These should be in a common file!
181  */
182 #define PCI_CBIO	0x10		/* Configuration Base IO Address */
183 
184 int
185 ne_pci_match(parent, match, aux)
186 	struct device *parent;
187 	struct cfdata *match;
188 	void *aux;
189 {
190 	struct pci_attach_args *pa = aux;
191 
192 	if (ne_pci_lookup(pa) != NULL)
193 		return (1);
194 
195 	return (0);
196 }
197 
198 void
199 ne_pci_attach(parent, self, aux)
200 	struct device *parent, *self;
201 	void *aux;
202 {
203 	struct ne_pci_softc *psc = (struct ne_pci_softc *)self;
204 	struct ne2000_softc *nsc = &psc->sc_ne2000;
205 	struct dp8390_softc *dsc = &nsc->sc_dp8390;
206 	struct pci_attach_args *pa = aux;
207 	pci_chipset_tag_t pc = pa->pa_pc;
208 	bus_space_tag_t nict;
209 	bus_space_handle_t nich;
210 	bus_space_tag_t asict;
211 	bus_space_handle_t asich;
212 	const char *intrstr;
213 	const struct ne_pci_product *npp;
214 	pci_intr_handle_t ih;
215 	pcireg_t csr;
216 
217 	npp = ne_pci_lookup(pa);
218 	if (npp == NULL) {
219 		printf("\n");
220 		panic("ne_pci_attach: impossible");
221 	}
222 
223 	printf(": %s Ethernet\n", npp->npp_name);
224 
225 #ifdef IPKDB_NE_PCI
226 	if (ne_pci_isipkdb(pc, pa->pa_tag)) {
227 		nict = ipkdb_softc.sc_ne2000.sc_dp8390.sc_regt;
228 		nich = ipkdb_softc.sc_ne2000.sc_dp8390.sc_regh;
229 		ne_kip->port = nsc;
230 	} else
231 #endif
232 	if (pci_mapreg_map(pa, PCI_CBIO, PCI_MAPREG_TYPE_IO, 0,
233 	    &nict, &nich, NULL, NULL)) {
234 		printf("%s: can't map i/o space\n", dsc->sc_dev.dv_xname);
235 		return;
236 	}
237 
238 	asict = nict;
239 	if (bus_space_subregion(nict, nich, NE2000_ASIC_OFFSET,
240 	    NE2000_ASIC_NPORTS, &asich)) {
241 		printf("%s: can't subregion i/o space\n", dsc->sc_dev.dv_xname);
242 		return;
243 	}
244 
245 	dsc->sc_regt = nict;
246 	dsc->sc_regh = nich;
247 
248 	nsc->sc_asict = asict;
249 	nsc->sc_asich = asich;
250 
251 	/* Enable the card. */
252 	csr = pci_conf_read(pc, pa->pa_tag,
253 	    PCI_COMMAND_STATUS_REG);
254 	pci_conf_write(pc, pa->pa_tag, PCI_COMMAND_STATUS_REG,
255 	    csr | PCI_COMMAND_MASTER_ENABLE);
256 
257 	/* This interface is always enabled. */
258 	dsc->sc_enabled = 1;
259 
260 	dsc->sc_mediachange = npp->npp_mediachange;
261 	dsc->sc_mediastatus = npp->npp_mediastatus;
262 	dsc->sc_media_init = npp->npp_media_init;
263 	dsc->init_card = npp->npp_init_card;
264 
265 	/*
266 	 * Do generic NE2000 attach.  This will read the station address
267 	 * from the EEPROM.
268 	 */
269 	ne2000_attach(nsc, NULL);
270 
271 	/* Map and establish the interrupt. */
272 	if (pci_intr_map(pa, &ih)) {
273 		printf("%s: couldn't map interrupt\n", dsc->sc_dev.dv_xname);
274 		return;
275 	}
276 	intrstr = pci_intr_string(pc, ih);
277 	psc->sc_ih = pci_intr_establish(pc, ih, IPL_NET, dp8390_intr, dsc);
278 	if (psc->sc_ih == NULL) {
279 		printf("%s: couldn't establish interrupt",
280 		    dsc->sc_dev.dv_xname);
281 		if (intrstr != NULL)
282 			printf(" at %s", intrstr);
283 		printf("\n");
284 		return;
285 	}
286 	printf("%s: interrupting at %s\n", dsc->sc_dev.dv_xname, intrstr);
287 }
288 
289 #ifdef IPKDB_NE_PCI
290 static int
291 ne_pci_isipkdb(pc, tag)
292 	pci_chipset_tag_t pc;
293 	pcitag_t tag;
294 {
295 	return !memcmp(&pc, &ipkdb_pc, sizeof pc)
296 		&& !memcmp(&tag, &ipkdb_tag, sizeof tag);
297 }
298 
299 int
300 ne_pci_ipkdb_attach(kip, iot, pc, bus, dev)
301 	struct ipkdb_if *kip;
302 	bus_space_tag_t iot;
303 	pci_chipset_tag_t pc;
304 	int bus, dev;
305 {
306 	struct pci_attach_args pa;
307 	bus_space_tag_t nict, asict;
308 	bus_space_handle_t nich, asich;
309 	u_int32_t csr;
310 
311 	pa.pa_iot = iot;
312 	pa.pa_pc = pc;
313 	pa.pa_device = dev;
314 	pa.pa_function = 0;
315 	pa.pa_flags = PCI_FLAGS_IO_ENABLED;
316 	pa.pa_tag = pci_make_tag(pc, bus, dev, /*func*/0);
317 	pa.pa_id = pci_conf_read(pc, pa.pa_tag, PCI_ID_REG);
318 	pa.pa_class = pci_conf_read(pc, pa.pa_tag, PCI_CLASS_REG);
319 	if (ne_pci_lookup(&pa) == NULL)
320 		return -1;
321 
322 	if (pci_mapreg_map(&pa, PCI_CBIO, PCI_MAPREG_TYPE_IO, 0,
323 			&nict, &nich, NULL, NULL))
324 		return -1;
325 
326 	asict = nict;
327 	if (bus_space_subregion(nict, nich, NE2000_ASIC_OFFSET,
328 				NE2000_ASIC_NPORTS, &asich)) {
329 		bus_space_unmap(nict, nich, NE2000_NPORTS);
330 		return -1;
331 	}
332 
333 	/* Enable card */
334 	csr = pci_conf_read(pc, pa.pa_tag, PCI_COMMAND_STATUS_REG);
335 	pci_conf_write(pc, pa.pa_tag, PCI_COMMAND_STATUS_REG,
336 			csr | PCI_COMMAND_MASTER_ENABLE);
337 
338 	ipkdb_softc.sc_ne2000.sc_dp8390.sc_regt = nict;
339 	ipkdb_softc.sc_ne2000.sc_dp8390.sc_regh = nich;
340 	ipkdb_softc.sc_ne2000.sc_asict = asict;
341 	ipkdb_softc.sc_ne2000.sc_asich = asich;
342 
343 	kip->port = &ipkdb_softc;
344 	ipkdb_pc = pc;
345 	ipkdb_tag = pa.pa_tag;
346 	ne_kip = kip;
347 
348 	if (ne2000_ipkdb_attach(kip) < 0) {
349 		bus_space_unmap(nict, nich, NE2000_NPORTS);
350 		return -1;
351 	}
352 
353 	return 0;
354 }
355 #endif
356