xref: /netbsd-src/sys/dev/pci/if_ne_pci.c (revision 8ac07aec990b9d2e483062509d0a9fa5b4f57cf2)
1 /*	$NetBSD: if_ne_pci.c,v 1.31 2008/03/12 14:31:11 cube 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.31 2008/03/12 14:31:11 cube 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 <sys/bus.h>
57 #include <sys/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_NEW(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,
183     void *aux)
184 {
185 	struct pci_attach_args *pa = aux;
186 
187 	if (ne_pci_lookup(pa) != NULL)
188 		return (1);
189 
190 	return (0);
191 }
192 
193 static void
194 ne_pci_attach(struct device *parent, struct device *self, void *aux)
195 {
196 	struct ne_pci_softc *psc = device_private(self);
197 	struct ne2000_softc *nsc = &psc->sc_ne2000;
198 	struct dp8390_softc *dsc = &nsc->sc_dp8390;
199 	struct pci_attach_args *pa = aux;
200 	pci_chipset_tag_t pc = pa->pa_pc;
201 	bus_space_tag_t nict;
202 	bus_space_handle_t nich;
203 	bus_space_tag_t asict;
204 	bus_space_handle_t asich;
205 	const char *intrstr;
206 	const struct ne_pci_product *npp;
207 	pci_intr_handle_t ih;
208 	pcireg_t csr;
209 
210 	npp = ne_pci_lookup(pa);
211 	if (npp == NULL) {
212 		printf("\n");
213 		panic("ne_pci_attach: impossible");
214 	}
215 
216 	dsc->sc_dev = self;
217 
218 	printf(": %s Ethernet\n", npp->npp_name);
219 
220 #ifdef IPKDB_NE_PCI
221 	if (ne_pci_isipkdb(pc, pa->pa_tag)) {
222 		nict = ipkdb_softc.sc_ne2000.sc_dp8390.sc_regt;
223 		nich = ipkdb_softc.sc_ne2000.sc_dp8390.sc_regh;
224 		ne_kip->port = nsc;
225 	} else
226 #endif
227 	if (pci_mapreg_map(pa, PCI_CBIO, PCI_MAPREG_TYPE_IO, 0,
228 	    &nict, &nich, NULL, NULL)) {
229 		aprint_error_dev(dsc->sc_dev, "can't map i/o space\n");
230 		return;
231 	}
232 
233 	asict = nict;
234 	if (bus_space_subregion(nict, nich, NE2000_ASIC_OFFSET,
235 	    NE2000_ASIC_NPORTS, &asich)) {
236 		aprint_error_dev(dsc->sc_dev, "can't subregion i/o space\n");
237 		return;
238 	}
239 
240 	dsc->sc_regt = nict;
241 	dsc->sc_regh = nich;
242 
243 	nsc->sc_asict = asict;
244 	nsc->sc_asich = asich;
245 
246 	/* Enable the card. */
247 	csr = pci_conf_read(pc, pa->pa_tag,
248 	    PCI_COMMAND_STATUS_REG);
249 	pci_conf_write(pc, pa->pa_tag, PCI_COMMAND_STATUS_REG,
250 	    csr | PCI_COMMAND_MASTER_ENABLE);
251 
252 	/* This interface is always enabled. */
253 	dsc->sc_enabled = 1;
254 
255 	dsc->sc_mediachange = npp->npp_mediachange;
256 	dsc->sc_mediastatus = npp->npp_mediastatus;
257 	dsc->sc_media_init = npp->npp_media_init;
258 	dsc->init_card = npp->npp_init_card;
259 
260 	/*
261 	 * Do generic NE2000 attach.  This will read the station address
262 	 * from the EEPROM.
263 	 */
264 	ne2000_attach(nsc, NULL);
265 
266 	/* Map and establish the interrupt. */
267 	if (pci_intr_map(pa, &ih)) {
268 		aprint_error_dev(dsc->sc_dev, "couldn't map interrupt\n");
269 		return;
270 	}
271 	intrstr = pci_intr_string(pc, ih);
272 	psc->sc_ih = pci_intr_establish(pc, ih, IPL_NET, dp8390_intr, dsc);
273 	if (psc->sc_ih == NULL) {
274 		aprint_error_dev(dsc->sc_dev, "couldn't establish interrupt");
275 		if (intrstr != NULL)
276 			aprint_error(" at %s", intrstr);
277 		aprint_error("\n");
278 		return;
279 	}
280 	aprint_normal_dev(dsc->sc_dev, "interrupting at %s\n", intrstr);
281 }
282 
283 #ifdef IPKDB_NE_PCI
284 static int
285 ne_pci_isipkdb(pci_chipset_tag_t pc, pcitag_t tag)
286 {
287 	return !memcmp(&pc, &ipkdb_pc, sizeof pc)
288 		&& !memcmp(&tag, &ipkdb_tag, sizeof tag);
289 }
290 
291 int
292 ne_pci_ipkdb_attach(struct ipkdb_if *kip, bus_space_tag_t iot,
293     pci_chipset_tag_t pc, int bus, int dev)
294 {
295 	struct pci_attach_args pa;
296 	bus_space_tag_t nict, asict;
297 	bus_space_handle_t nich, asich;
298 	u_int32_t csr;
299 
300 	pa.pa_iot = iot;
301 	pa.pa_pc = pc;
302 	pa.pa_device = dev;
303 	pa.pa_function = 0;
304 	pa.pa_flags = PCI_FLAGS_IO_ENABLED;
305 	pa.pa_tag = pci_make_tag(pc, bus, dev, /*func*/0);
306 	pa.pa_id = pci_conf_read(pc, pa.pa_tag, PCI_ID_REG);
307 	pa.pa_class = pci_conf_read(pc, pa.pa_tag, PCI_CLASS_REG);
308 	if (ne_pci_lookup(&pa) == NULL)
309 		return -1;
310 
311 	if (pci_mapreg_map(&pa, PCI_CBIO, PCI_MAPREG_TYPE_IO, 0,
312 			&nict, &nich, NULL, NULL))
313 		return -1;
314 
315 	asict = nict;
316 	if (bus_space_subregion(nict, nich, NE2000_ASIC_OFFSET,
317 				NE2000_ASIC_NPORTS, &asich)) {
318 		bus_space_unmap(nict, nich, NE2000_NPORTS);
319 		return -1;
320 	}
321 
322 	/* Enable card */
323 	csr = pci_conf_read(pc, pa.pa_tag, PCI_COMMAND_STATUS_REG);
324 	pci_conf_write(pc, pa.pa_tag, PCI_COMMAND_STATUS_REG,
325 			csr | PCI_COMMAND_MASTER_ENABLE);
326 
327 	ipkdb_softc.sc_ne2000.sc_dp8390.sc_regt = nict;
328 	ipkdb_softc.sc_ne2000.sc_dp8390.sc_regh = nich;
329 	ipkdb_softc.sc_ne2000.sc_asict = asict;
330 	ipkdb_softc.sc_ne2000.sc_asich = asich;
331 
332 	kip->port = &ipkdb_softc;
333 	ipkdb_pc = pc;
334 	ipkdb_tag = pa.pa_tag;
335 	ne_kip = kip;
336 
337 	if (ne2000_ipkdb_attach(kip) < 0) {
338 		bus_space_unmap(nict, nich, NE2000_NPORTS);
339 		return -1;
340 	}
341 
342 	return 0;
343 }
344 #endif /* IPKDB_NE_PCI */
345