xref: /openbsd/sys/dev/pci/if_ne_pci.c (revision 09467b48)
1 /*	$OpenBSD: if_ne_pci.c,v 1.20 2014/12/22 02:28:52 tedu 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  *
21  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
22  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
23  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
24  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
25  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31  * POSSIBILITY OF SUCH DAMAGE.
32  */
33 
34 #include "bpfilter.h"
35 
36 #include <sys/param.h>
37 #include <sys/systm.h>
38 #include <sys/mbuf.h>
39 #include <sys/syslog.h>
40 #include <sys/socket.h>
41 #include <sys/device.h>
42 
43 #include <net/if.h>
44 #include <net/if_media.h>
45 
46 #include <netinet/in.h>
47 #include <netinet/if_ether.h>
48 
49 #include <machine/bus.h>
50 #include <machine/intr.h>
51 
52 #include <dev/pci/pcireg.h>
53 #include <dev/pci/pcivar.h>
54 #include <dev/pci/pcidevs.h>
55 
56 #include <dev/mii/miivar.h>
57 
58 #include <dev/ic/dp8390reg.h>
59 #include <dev/ic/dp8390var.h>
60 
61 #include <dev/ic/ne2000reg.h>
62 #include <dev/ic/ne2000var.h>
63 
64 #include <dev/ic/rtl80x9reg.h>
65 #include <dev/ic/rtl80x9var.h>
66 
67 struct ne_pci_softc {
68 	struct ne2000_softc sc_ne2000;		/* real "ne2000" softc */
69 
70 	/* PCI-specific goo */
71 	void *sc_ih;				/* interrupt handle */
72 };
73 
74 int ne_pci_match(struct device *, void *, void *);
75 void ne_pci_attach(struct device *, struct device *, void *);
76 
77 struct cfattach ne_pci_ca = {
78 	sizeof(struct ne_pci_softc), ne_pci_match, ne_pci_attach
79 };
80 
81 const struct ne_pci_product {
82 	pci_vendor_id_t npp_vendor;
83 	pci_product_id_t npp_product;
84 	int (*npp_mediachange)(struct dp8390_softc *);
85 	void (*npp_mediastatus)(struct dp8390_softc *,
86 	    struct ifmediareq *);
87 	void (*npp_init_card)(struct dp8390_softc *);
88 	void (*npp_media_init)(struct dp8390_softc *);
89 } ne_pci_prod[] = {
90 	{ PCI_VENDOR_REALTEK,		PCI_PRODUCT_REALTEK_RT8029,
91 	  rtl80x9_mediachange,		rtl80x9_mediastatus,
92 	  rtl80x9_init_card,		rtl80x9_media_init,
93 	  /* Realtek 8029 */ },
94 
95 	{ PCI_VENDOR_WINBOND,		PCI_PRODUCT_WINBOND_W89C940F,
96 	  NULL,				NULL,
97 	  NULL,				NULL,
98 	  /* Winbond 89C940F */ },
99 
100 	{ PCI_VENDOR_VIATECH,		PCI_PRODUCT_VIATECH_VT86C926,
101 	  NULL,				NULL,
102 	  NULL,				NULL,
103 	  /* VIA Technologies VT86C926 */ },
104 
105 	{ PCI_VENDOR_SURECOM,		PCI_PRODUCT_SURECOM_NE34,
106 	  NULL,				NULL,
107 	  NULL,				NULL,
108 	  /* Surecom NE-34 */ },
109 
110 	{ PCI_VENDOR_NETVIN,		PCI_PRODUCT_NETVIN_NV5000,
111 	  NULL,				NULL,
112 	  NULL,				NULL,
113 	  /* NetVin 5000 */ },
114 
115 	/* XXX The following entries need sanity checking in pcidevs */
116 	{ PCI_VENDOR_COMPEX,		PCI_PRODUCT_COMPEX_COMPEXE,
117 	  NULL,				NULL,
118 	  NULL,				NULL,
119 	  /* Compex */ },
120 
121 	{ PCI_VENDOR_WINBOND2,		PCI_PRODUCT_WINBOND2_W89C940,
122 	  NULL,				NULL,
123 	  NULL,				NULL,
124 	  /* ProLAN */ },
125 
126 	{ PCI_VENDOR_KTI,		PCI_PRODUCT_KTI_KTIE,
127 	  NULL,				NULL,
128 	  NULL,				NULL,
129 	  /* KTI */ },
130 };
131 
132 const struct ne_pci_product *ne_pci_lookup(struct pci_attach_args *);
133 
134 const struct ne_pci_product *
135 ne_pci_lookup(struct pci_attach_args *pa)
136 {
137 	const struct ne_pci_product *npp;
138 
139 	for (npp = ne_pci_prod;
140 	    npp < &ne_pci_prod[nitems(ne_pci_prod)];
141 	    npp++) {
142 		if (PCI_VENDOR(pa->pa_id) == npp->npp_vendor &&
143 		    PCI_PRODUCT(pa->pa_id) == npp->npp_product)
144 			return (npp);
145 	}
146 	return (NULL);
147 }
148 
149 /*
150  * PCI constants.
151  * XXX These should be in a common file!
152  */
153 #define PCI_CBIO	0x10		/* Configuration Base IO Address */
154 
155 int
156 ne_pci_match(struct device *parent, void *match, void *aux)
157 {
158 	struct pci_attach_args *pa = aux;
159 
160 	if (ne_pci_lookup(pa) != NULL)
161  		return (1);
162 
163 	return (0);
164 }
165 
166 void
167 ne_pci_attach(struct device *parent, struct device *self, void *aux)
168 {
169 	struct ne_pci_softc *psc = (struct ne_pci_softc *)self;
170 	struct ne2000_softc *nsc = &psc->sc_ne2000;
171 	struct dp8390_softc *dsc = &nsc->sc_dp8390;
172 	struct pci_attach_args *pa = aux;
173 	pci_chipset_tag_t pc = pa->pa_pc;
174 	bus_size_t iosize;
175 	bus_space_tag_t nict;
176 	bus_space_handle_t nich;
177 	bus_space_tag_t asict;
178 	bus_space_handle_t asich;
179 	const char *intrstr;
180 	const struct ne_pci_product *npp;
181 	pci_intr_handle_t ih;
182 
183 	npp = ne_pci_lookup(pa);
184 	if (npp == NULL) {
185 		printf("\n");
186 		panic("ne_pci_attach: impossible");
187 	}
188 
189 	if (pci_mapreg_map(pa, PCI_CBIO, PCI_MAPREG_TYPE_IO, 0,
190 	    &nict, &nich, NULL, &iosize, 0)) {
191 		printf(": can't map i/o space\n");
192 		return;
193 	}
194 
195 	asict = nict;
196 	if (bus_space_subregion(nict, nich, NE2000_ASIC_OFFSET,
197 	    NE2000_ASIC_NPORTS, &asich)) {
198 		printf(": can't subregion i/o space\n");
199 		bus_space_unmap(nict, nich, iosize);
200 		return;
201 	}
202 
203 	dsc->sc_regt = nict;
204 	dsc->sc_regh = nich;
205 
206 	nsc->sc_asict = asict;
207 	nsc->sc_asich = asich;
208 
209 	/* This interface is always enabled. */
210 	dsc->sc_enabled = 1;
211 
212 	dsc->sc_mediachange = npp->npp_mediachange;
213 	dsc->sc_mediastatus = npp->npp_mediastatus;
214 	dsc->sc_media_init = npp->npp_media_init;
215 	dsc->init_card = npp->npp_init_card;
216 
217 	/* Map and establish the interrupt. */
218 	if (pci_intr_map(pa, &ih)) {
219 		printf(": couldn't map interrupt\n");
220 		bus_space_unmap(nict, nich, iosize);
221 		return;
222 	}
223 	intrstr = pci_intr_string(pc, ih);
224 	psc->sc_ih = pci_intr_establish(pc, ih, IPL_NET, dp8390_intr, dsc,
225 		dsc->sc_dev.dv_xname);
226 	if (psc->sc_ih == NULL) {
227 		printf(": couldn't establish interrupt");
228 		if (intrstr != NULL)
229 			printf(" at %s", intrstr);
230 		printf("\n");
231 		bus_space_unmap(nict, nich, iosize);
232 		return;
233 	}
234 	printf(": %s", intrstr);
235 
236 	/*
237 	 * Do generic NE2000 attach.  This will read the station address
238 	 * from the EEPROM.
239 	 */
240 	ne2000_attach(nsc, NULL);
241 }
242