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