xref: /openbsd-src/sys/dev/pci/if_ne_pci.c (revision daf88648c0e349d5c02e1504293082072c981640)
1 /*	$OpenBSD: if_ne_pci.c,v 1.16 2006/10/20 17:02:24 brad 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 #include <net/if_media.h>
52 
53 #ifdef INET
54 #include <netinet/in.h>
55 #include <netinet/if_ether.h>
56 #endif
57 
58 #include <machine/bus.h>
59 #include <machine/intr.h>
60 
61 #include <dev/pci/pcireg.h>
62 #include <dev/pci/pcivar.h>
63 #include <dev/pci/pcidevs.h>
64 
65 #include <dev/mii/miivar.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(struct device *, void *, void *);
84 void ne_pci_attach(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 const struct ne_pci_product {
91 	pci_vendor_id_t npp_vendor;
92 	pci_product_id_t npp_product;
93 	int (*npp_mediachange)(struct dp8390_softc *);
94 	void (*npp_mediastatus)(struct dp8390_softc *,
95 	    struct ifmediareq *);
96 	void (*npp_init_card)(struct dp8390_softc *);
97 	void (*npp_media_init)(struct dp8390_softc *);
98 } ne_pci_prod[] = {
99 	{ PCI_VENDOR_REALTEK,		PCI_PRODUCT_REALTEK_RT8029,
100 	  rtl80x9_mediachange,		rtl80x9_mediastatus,
101 	  rtl80x9_init_card,		rtl80x9_media_init,
102 	  /* RealTek 8029 */ },
103 
104 	{ PCI_VENDOR_WINBOND,		PCI_PRODUCT_WINBOND_W89C940F,
105 	  NULL,				NULL,
106 	  NULL,				NULL,
107 	  /* Winbond 89C940F */ },
108 
109 	{ PCI_VENDOR_VIATECH,		PCI_PRODUCT_VIATECH_VT86C926,
110 	  NULL,				NULL,
111 	  NULL,				NULL,
112 	  /* VIA Technologies VT86C926 */ },
113 
114 	{ PCI_VENDOR_SURECOM,		PCI_PRODUCT_SURECOM_NE34,
115 	  NULL,				NULL,
116 	  NULL,				NULL,
117 	  /* Surecom NE-34 */ },
118 
119 	{ PCI_VENDOR_NETVIN,		PCI_PRODUCT_NETVIN_NV5000,
120 	  NULL,				NULL,
121 	  NULL,				NULL,
122 	  /* NetVin 5000 */ },
123 
124 	/* XXX The following entries need sanity checking in pcidevs */
125 	{ PCI_VENDOR_COMPEX,		PCI_PRODUCT_COMPEX_COMPEXE,
126 	  NULL,				NULL,
127 	  NULL,				NULL,
128 	  /* Compex */ },
129 
130 	{ PCI_VENDOR_WINBOND2,		PCI_PRODUCT_WINBOND2_W89C940,
131 	  NULL,				NULL,
132 	  NULL,				NULL,
133 	  /* ProLAN */ },
134 
135 	{ PCI_VENDOR_KTI,		PCI_PRODUCT_KTI_KTIE,
136 	  NULL,				NULL,
137 	  NULL,				NULL,
138 	  /* KTI */ },
139 };
140 
141 const struct ne_pci_product *ne_pci_lookup(struct pci_attach_args *);
142 
143 const struct ne_pci_product *
144 ne_pci_lookup(struct pci_attach_args *pa)
145 {
146 	const struct ne_pci_product *npp;
147 
148 	for (npp = ne_pci_prod;
149 	    npp < &ne_pci_prod[sizeof(ne_pci_prod)/sizeof(ne_pci_prod[0])];
150 	    npp++) {
151 		if (PCI_VENDOR(pa->pa_id) == npp->npp_vendor &&
152 		    PCI_PRODUCT(pa->pa_id) == npp->npp_product)
153 			return (npp);
154 	}
155 	return (NULL);
156 }
157 
158 /*
159  * PCI constants.
160  * XXX These should be in a common file!
161  */
162 #define PCI_CBIO	0x10		/* Configuration Base IO Address */
163 
164 int
165 ne_pci_match(struct device *parent, void *match, void *aux)
166 {
167 	struct pci_attach_args *pa = aux;
168 
169 	if (ne_pci_lookup(pa) != NULL)
170  		return (1);
171 
172 	return (0);
173 }
174 
175 void
176 ne_pci_attach(struct device *parent, struct device *self, void *aux)
177 {
178 	struct ne_pci_softc *psc = (struct ne_pci_softc *)self;
179 	struct ne2000_softc *nsc = &psc->sc_ne2000;
180 	struct dp8390_softc *dsc = &nsc->sc_dp8390;
181 	struct pci_attach_args *pa = aux;
182 	pci_chipset_tag_t pc = pa->pa_pc;
183 	bus_size_t iosize;
184 	bus_space_tag_t nict;
185 	bus_space_handle_t nich;
186 	bus_space_tag_t asict;
187 	bus_space_handle_t asich;
188 	const char *intrstr;
189 	const struct ne_pci_product *npp;
190 	pci_intr_handle_t ih;
191 
192 	npp = ne_pci_lookup(pa);
193 	if (npp == NULL) {
194 		printf("\n");
195 		panic("ne_pci_attach: impossible");
196 	}
197 
198 	if (pci_mapreg_map(pa, PCI_CBIO, PCI_MAPREG_TYPE_IO, 0,
199 	    &nict, &nich, NULL, &iosize, 0)) {
200 		printf(": can't map i/o space\n");
201 		return;
202 	}
203 
204 	asict = nict;
205 	if (bus_space_subregion(nict, nich, NE2000_ASIC_OFFSET,
206 	    NE2000_ASIC_NPORTS, &asich)) {
207 		printf(": can't subregion i/o space\n");
208 		bus_space_unmap(nict, nich, iosize);
209 		return;
210 	}
211 
212 	dsc->sc_regt = nict;
213 	dsc->sc_regh = nich;
214 
215 	nsc->sc_asict = asict;
216 	nsc->sc_asich = asich;
217 
218 	/* This interface is always enabled. */
219 	dsc->sc_enabled = 1;
220 
221 	dsc->sc_mediachange = npp->npp_mediachange;
222 	dsc->sc_mediastatus = npp->npp_mediastatus;
223 	dsc->sc_media_init = npp->npp_media_init;
224 	dsc->init_card = npp->npp_init_card;
225 
226 	/* Map and establish the interrupt. */
227 	if (pci_intr_map(pa, &ih)) {
228 		printf(": couldn't map interrupt\n");
229 		bus_space_unmap(nict, nich, iosize);
230 		return;
231 	}
232 	intrstr = pci_intr_string(pc, ih);
233 	psc->sc_ih = pci_intr_establish(pc, ih, IPL_NET, dp8390_intr, dsc,
234 		dsc->sc_dev.dv_xname);
235 	if (psc->sc_ih == NULL) {
236 		printf(": couldn't establish interrupt");
237 		if (intrstr != NULL)
238 			printf(" at %s", intrstr);
239 		printf("\n");
240 		bus_space_unmap(nict, nich, iosize);
241 		return;
242 	}
243 	printf(": %s", intrstr);
244 
245 	/*
246 	 * Do generic NE2000 attach.  This will read the station address
247 	 * from the EEPROM.
248 	 */
249 	ne2000_attach(nsc, NULL);
250 }
251