xref: /netbsd-src/sys/dev/pci/if_ne_pci.c (revision d91f98a8715141154279122ae81737cb65179572)
1 /*	$NetBSD: if_ne_pci.c,v 1.40 2019/01/27 02:08:42 pgoyette 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  *
20  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
21  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
24  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30  * POSSIBILITY OF SUCH DAMAGE.
31  */
32 
33 #include <sys/cdefs.h>
34 __KERNEL_RCSID(0, "$NetBSD: if_ne_pci.c,v 1.40 2019/01/27 02:08:42 pgoyette Exp $");
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_ether.h>
45 #include <net/if_media.h>
46 
47 #include <sys/bus.h>
48 #include <sys/intr.h>
49 
50 #include <dev/pci/pcireg.h>
51 #include <dev/pci/pcivar.h>
52 #include <dev/pci/pcidevs.h>
53 
54 #include <dev/ic/dp8390reg.h>
55 #include <dev/ic/dp8390var.h>
56 
57 #include <dev/ic/ne2000reg.h>
58 #include <dev/ic/ne2000var.h>
59 
60 #include <dev/ic/rtl80x9reg.h>
61 #include <dev/ic/rtl80x9var.h>
62 
63 struct ne_pci_softc {
64 	struct ne2000_softc sc_ne2000;		/* real "ne2000" softc */
65 
66 	/* PCI-specific goo */
67 	void *sc_ih;				/* interrupt handle */
68 };
69 
70 static int	ne_pci_match(device_t, cfdata_t, void *);
71 static void	ne_pci_attach(device_t, device_t, void *);
72 
73 CFATTACH_DECL_NEW(ne_pci, sizeof(struct ne_pci_softc),
74     ne_pci_match, ne_pci_attach, NULL, NULL);
75 
76 static const struct ne_pci_product {
77 	pci_vendor_id_t npp_vendor;
78 	pci_product_id_t npp_product;
79 	int (*npp_mediachange)(struct dp8390_softc *);
80 	void (*npp_mediastatus)(struct dp8390_softc *, struct ifmediareq *);
81 	void (*npp_init_card)(struct dp8390_softc *);
82 	void (*npp_media_init)(struct dp8390_softc *);
83 	const char *npp_name;
84 } ne_pci_products[] = {
85 	{ PCI_VENDOR_REALTEK,		PCI_PRODUCT_REALTEK_RT8029,
86 	  rtl80x9_mediachange,		rtl80x9_mediastatus,
87 	  rtl80x9_init_card,		rtl80x9_media_init,
88 	  "Realtek 8029" },
89 
90 	{ PCI_VENDOR_WINBOND,		PCI_PRODUCT_WINBOND_W89C940F,
91 	  NULL,				NULL,
92 	  NULL,				NULL,
93 	  "Winbond 89C940F" },
94 
95 	{ PCI_VENDOR_WINBOND,		PCI_PRODUCT_WINBOND_W89C940F_1,
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_5000,
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_NE2KETHER,
117 	  NULL,				NULL,
118 	  NULL,				NULL,
119 	  "Compex" },
120 
121 	{ PCI_VENDOR_PROLAN,		PCI_PRODUCT_PROLAN_NE2KETHER,
122 	  NULL,				NULL,
123 	  NULL,				NULL,
124 	  "ProLAN" },
125 
126 	{ PCI_VENDOR_KTI,		PCI_PRODUCT_KTI_NE2KETHER,
127 	  NULL,				NULL,
128 	  NULL,				NULL,
129 	  "KTI" },
130 
131 	{ 0,				0,
132 	  NULL,				NULL,
133 	  NULL,				NULL,
134 	  NULL },
135 };
136 
137 static const struct ne_pci_product *
ne_pci_lookup(const struct pci_attach_args * pa)138 ne_pci_lookup(const struct pci_attach_args *pa)
139 {
140 	const struct ne_pci_product *npp;
141 
142 	for (npp = ne_pci_products; npp->npp_name != NULL; npp++) {
143 		if (PCI_VENDOR(pa->pa_id) == npp->npp_vendor &&
144 		    PCI_PRODUCT(pa->pa_id) == npp->npp_product)
145 			return (npp);
146 	}
147 	return (NULL);
148 }
149 
150 /*
151  * PCI constants.
152  * XXX These should be in a common file!
153  */
154 #define PCI_CBIO PCI_BAR(0)		/* Configuration Base IO Address */
155 
156 static int
ne_pci_match(device_t parent,cfdata_t match,void * aux)157 ne_pci_match(device_t parent, cfdata_t match, void *aux)
158 {
159 	struct pci_attach_args *pa = aux;
160 
161 	if (ne_pci_lookup(pa) != NULL)
162 		return (1);
163 
164 	return (0);
165 }
166 
167 static void
ne_pci_attach(device_t parent,device_t self,void * aux)168 ne_pci_attach(device_t parent, device_t self, void *aux)
169 {
170 	struct ne_pci_softc *psc = device_private(self);
171 	struct ne2000_softc *nsc = &psc->sc_ne2000;
172 	struct dp8390_softc *dsc = &nsc->sc_dp8390;
173 	struct pci_attach_args *pa = aux;
174 	pci_chipset_tag_t pc = pa->pa_pc;
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 	pcireg_t csr;
183 	char intrbuf[PCI_INTRSTR_LEN];
184 
185 	npp = ne_pci_lookup(pa);
186 	if (npp == NULL) {
187 		printf("\n");
188 		panic("ne_pci_attach: impossible");
189 	}
190 
191 	dsc->sc_dev = self;
192 
193 	printf(": %s Ethernet\n", npp->npp_name);
194 
195 	if (pci_mapreg_map(pa, PCI_CBIO, PCI_MAPREG_TYPE_IO, 0,
196 	    &nict, &nich, NULL, NULL)) {
197 		aprint_error_dev(dsc->sc_dev, "can't map i/o space\n");
198 		return;
199 	}
200 
201 	asict = nict;
202 	if (bus_space_subregion(nict, nich, NE2000_ASIC_OFFSET,
203 	    NE2000_ASIC_NPORTS, &asich)) {
204 		aprint_error_dev(dsc->sc_dev, "can't subregion i/o space\n");
205 		return;
206 	}
207 
208 	dsc->sc_regt = nict;
209 	dsc->sc_regh = nich;
210 
211 	nsc->sc_asict = asict;
212 	nsc->sc_asich = asich;
213 
214 	/* Enable the card. */
215 	csr = pci_conf_read(pc, pa->pa_tag,
216 	    PCI_COMMAND_STATUS_REG);
217 	pci_conf_write(pc, pa->pa_tag, PCI_COMMAND_STATUS_REG,
218 	    csr | PCI_COMMAND_MASTER_ENABLE);
219 
220 	/* This interface is always enabled. */
221 	dsc->sc_enabled = 1;
222 
223 	dsc->sc_mediachange = npp->npp_mediachange;
224 	dsc->sc_mediastatus = npp->npp_mediastatus;
225 	dsc->sc_media_init = npp->npp_media_init;
226 	dsc->init_card = npp->npp_init_card;
227 
228 	/*
229 	 * Do generic NE2000 attach.  This will read the station address
230 	 * from the EEPROM.
231 	 */
232 	ne2000_attach(nsc, NULL);
233 
234 	/* Map and establish the interrupt. */
235 	if (pci_intr_map(pa, &ih)) {
236 		aprint_error_dev(dsc->sc_dev, "couldn't map interrupt\n");
237 		return;
238 	}
239 	intrstr = pci_intr_string(pc, ih, intrbuf, sizeof(intrbuf));
240 	psc->sc_ih = pci_intr_establish_xname(pc, ih, IPL_NET, dp8390_intr,
241 	    dsc, device_xname(self));
242 	if (psc->sc_ih == NULL) {
243 		aprint_error_dev(dsc->sc_dev, "couldn't establish interrupt");
244 		if (intrstr != NULL)
245 			aprint_error(" at %s", intrstr);
246 		aprint_error("\n");
247 		return;
248 	}
249 	aprint_normal_dev(dsc->sc_dev, "interrupting at %s\n", intrstr);
250 }
251