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