xref: /openbsd/sys/dev/pci/if_ne_pci.c (revision 0f9891f1)
1 /*	$OpenBSD: if_ne_pci.c,v 1.22 2024/05/24 06:02:56 jsg 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 <sys/param.h>
35 #include <sys/systm.h>
36 #include <sys/device.h>
37 
38 #include <net/if.h>
39 #include <net/if_media.h>
40 
41 #include <netinet/in.h>
42 #include <netinet/if_ether.h>
43 
44 #include <machine/bus.h>
45 #include <machine/intr.h>
46 
47 #include <dev/pci/pcireg.h>
48 #include <dev/pci/pcivar.h>
49 #include <dev/pci/pcidevs.h>
50 
51 #include <dev/mii/miivar.h>
52 
53 #include <dev/ic/dp8390reg.h>
54 #include <dev/ic/dp8390var.h>
55 
56 #include <dev/ic/ne2000reg.h>
57 #include <dev/ic/ne2000var.h>
58 
59 #include <dev/ic/rtl80x9reg.h>
60 #include <dev/ic/rtl80x9var.h>
61 
62 struct ne_pci_softc {
63 	struct ne2000_softc sc_ne2000;		/* real "ne2000" softc */
64 
65 	/* PCI-specific goo */
66 	void *sc_ih;				/* interrupt handle */
67 };
68 
69 int ne_pci_match(struct device *, void *, void *);
70 void ne_pci_attach(struct device *, struct device *, void *);
71 
72 const struct cfattach ne_pci_ca = {
73 	sizeof(struct ne_pci_softc), ne_pci_match, ne_pci_attach
74 };
75 
76 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 *,
81 	    struct ifmediareq *);
82 	void (*npp_init_card)(struct dp8390_softc *);
83 	void (*npp_media_init)(struct dp8390_softc *);
84 } ne_pci_prod[] = {
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_VIATECH,		PCI_PRODUCT_VIATECH_VT86C926,
96 	  NULL,				NULL,
97 	  NULL,				NULL,
98 	  /* VIA Technologies VT86C926 */ },
99 
100 	{ PCI_VENDOR_SURECOM,		PCI_PRODUCT_SURECOM_NE34,
101 	  NULL,				NULL,
102 	  NULL,				NULL,
103 	  /* Surecom NE-34 */ },
104 
105 	{ PCI_VENDOR_NETVIN,		PCI_PRODUCT_NETVIN_NV5000,
106 	  NULL,				NULL,
107 	  NULL,				NULL,
108 	  /* NetVin 5000 */ },
109 
110 	/* XXX The following entries need sanity checking in pcidevs */
111 	{ PCI_VENDOR_COMPEX,		PCI_PRODUCT_COMPEX_COMPEXE,
112 	  NULL,				NULL,
113 	  NULL,				NULL,
114 	  /* Compex */ },
115 
116 	{ PCI_VENDOR_WINBOND2,		PCI_PRODUCT_WINBOND2_W89C940,
117 	  NULL,				NULL,
118 	  NULL,				NULL,
119 	  /* ProLAN */ },
120 
121 	{ PCI_VENDOR_KTI,		PCI_PRODUCT_KTI_KTIE,
122 	  NULL,				NULL,
123 	  NULL,				NULL,
124 	  /* KTI */ },
125 };
126 
127 const struct ne_pci_product *ne_pci_lookup(struct pci_attach_args *);
128 
129 const struct ne_pci_product *
ne_pci_lookup(struct pci_attach_args * pa)130 ne_pci_lookup(struct pci_attach_args *pa)
131 {
132 	const struct ne_pci_product *npp;
133 
134 	for (npp = ne_pci_prod;
135 	    npp < &ne_pci_prod[nitems(ne_pci_prod)];
136 	    npp++) {
137 		if (PCI_VENDOR(pa->pa_id) == npp->npp_vendor &&
138 		    PCI_PRODUCT(pa->pa_id) == npp->npp_product)
139 			return (npp);
140 	}
141 	return (NULL);
142 }
143 
144 /*
145  * PCI constants.
146  * XXX These should be in a common file!
147  */
148 #define PCI_CBIO	0x10		/* Configuration Base IO Address */
149 
150 int
ne_pci_match(struct device * parent,void * match,void * aux)151 ne_pci_match(struct device *parent, void *match, void *aux)
152 {
153 	struct pci_attach_args *pa = aux;
154 
155 	if (ne_pci_lookup(pa) != NULL)
156  		return (1);
157 
158 	return (0);
159 }
160 
161 void
ne_pci_attach(struct device * parent,struct device * self,void * aux)162 ne_pci_attach(struct device *parent, struct device *self, void *aux)
163 {
164 	struct ne_pci_softc *psc = (struct ne_pci_softc *)self;
165 	struct ne2000_softc *nsc = &psc->sc_ne2000;
166 	struct dp8390_softc *dsc = &nsc->sc_dp8390;
167 	struct pci_attach_args *pa = aux;
168 	pci_chipset_tag_t pc = pa->pa_pc;
169 	bus_size_t iosize;
170 	bus_space_tag_t nict;
171 	bus_space_handle_t nich;
172 	bus_space_tag_t asict;
173 	bus_space_handle_t asich;
174 	const char *intrstr;
175 	const struct ne_pci_product *npp;
176 	pci_intr_handle_t ih;
177 
178 	npp = ne_pci_lookup(pa);
179 	if (npp == NULL) {
180 		printf("\n");
181 		panic("ne_pci_attach: impossible");
182 	}
183 
184 	if (pci_mapreg_map(pa, PCI_CBIO, PCI_MAPREG_TYPE_IO, 0,
185 	    &nict, &nich, NULL, &iosize, 0)) {
186 		printf(": can't map i/o space\n");
187 		return;
188 	}
189 
190 	asict = nict;
191 	if (bus_space_subregion(nict, nich, NE2000_ASIC_OFFSET,
192 	    NE2000_ASIC_NPORTS, &asich)) {
193 		printf(": can't subregion i/o space\n");
194 		bus_space_unmap(nict, nich, iosize);
195 		return;
196 	}
197 
198 	dsc->sc_regt = nict;
199 	dsc->sc_regh = nich;
200 
201 	nsc->sc_asict = asict;
202 	nsc->sc_asich = asich;
203 
204 	/* This interface is always enabled. */
205 	dsc->sc_enabled = 1;
206 
207 	dsc->sc_mediachange = npp->npp_mediachange;
208 	dsc->sc_mediastatus = npp->npp_mediastatus;
209 	dsc->sc_media_init = npp->npp_media_init;
210 	dsc->init_card = npp->npp_init_card;
211 
212 	/* Map and establish the interrupt. */
213 	if (pci_intr_map(pa, &ih)) {
214 		printf(": couldn't map interrupt\n");
215 		bus_space_unmap(nict, nich, iosize);
216 		return;
217 	}
218 	intrstr = pci_intr_string(pc, ih);
219 	psc->sc_ih = pci_intr_establish(pc, ih, IPL_NET, dp8390_intr, dsc,
220 		dsc->sc_dev.dv_xname);
221 	if (psc->sc_ih == NULL) {
222 		printf(": couldn't establish interrupt");
223 		if (intrstr != NULL)
224 			printf(" at %s", intrstr);
225 		printf("\n");
226 		bus_space_unmap(nict, nich, iosize);
227 		return;
228 	}
229 	printf(": %s", intrstr);
230 
231 	/*
232 	 * Do generic NE2000 attach.  This will read the station address
233 	 * from the EEPROM.
234 	 */
235 	ne2000_attach(nsc, NULL);
236 }
237