xref: /netbsd-src/sys/dev/pci/if_ex_pci.c (revision 17306b8fd0952c7489f93f0230818481e5a1e2c9)
1 /*	$NetBSD: if_ex_pci.c,v 1.16 2001/06/12 22:28:15 thorpej Exp $	*/
2 
3 /*-
4  * Copyright (c) 1998 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by Frank van der Linden; Jason R. Thorpe of the Numerical Aerospace
9  * Simulation Facility, 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 "bpfilter.h"
41 
42 #include <sys/param.h>
43 #include <sys/systm.h>
44 #include <sys/mbuf.h>
45 #include <sys/socket.h>
46 #include <sys/ioctl.h>
47 #include <sys/errno.h>
48 #include <sys/syslog.h>
49 #include <sys/select.h>
50 #include <sys/device.h>
51 
52 #include <net/if.h>
53 #include <net/if_dl.h>
54 #include <net/if_ether.h>
55 #include <net/if_media.h>
56 
57 #if NBPFILTER > 0
58 #include <net/bpf.h>
59 #include <net/bpfdesc.h>
60 #endif
61 
62 #include <machine/cpu.h>
63 #include <machine/bus.h>
64 #include <machine/intr.h>
65 
66 #include <dev/mii/miivar.h>
67 #include <dev/mii/mii.h>
68 
69 #include <dev/ic/elink3var.h>
70 #include <dev/ic/elink3reg.h>
71 #include <dev/ic/elinkxlreg.h>
72 #include <dev/ic/elinkxlvar.h>
73 
74 #include <dev/pci/pcivar.h>
75 #include <dev/pci/pcireg.h>
76 #include <dev/pci/pcidevs.h>
77 
78 struct ex_pci_softc {
79 	struct ex_softc sc_ex;
80 	pci_chipset_tag_t sc_chiptag;
81 	pcitag_t sc_pcitag;
82 };
83 
84 /*
85  * PCI constants.
86  * XXX These should be in a common file!
87  */
88 #define PCI_CONN		0x48    /* Connector type */
89 #define PCI_CBIO		0x10    /* Configuration Base IO Address */
90 #define PCI_POWERCTL		0xe0
91 #define PCI_FUNCMEM		0x18
92 
93 #define PCI_INTRACK		0x00008000
94 
95 int ex_pci_match __P((struct device *, struct cfdata *, void *));
96 void ex_pci_attach __P((struct device *, struct device *, void *));
97 void ex_pci_intr_ack __P((struct ex_softc *));
98 
99 struct cfattach ex_pci_ca = {
100 	sizeof(struct ex_pci_softc), ex_pci_match, ex_pci_attach
101 };
102 
103 const struct ex_pci_product {
104 	u_int32_t	epp_prodid;	/* PCI product ID */
105 	int		epp_flags;	/* initial softc flags */
106 	const char	*epp_name;	/* device name */
107 } ex_pci_products[] = {
108 	{ PCI_PRODUCT_3COM_3C900TPO,	0,
109 	  "3c900-TPO Ethernet" },
110 	{ PCI_PRODUCT_3COM_3C900COMBO,	0,
111 	  "3c900-COMBO Ethernet" },
112 
113 	{ PCI_PRODUCT_3COM_3C905TX,	EX_CONF_MII,
114 	  "3c905-TX 10/100 Ethernet" },
115 	{ PCI_PRODUCT_3COM_3C905T4,	EX_CONF_MII,
116 	  "3c905-T4 10/100 Ethernet" },
117 
118 	{ PCI_PRODUCT_3COM_3C900BTPO,	EX_CONF_90XB,
119 	  "3c900B-TPO Ethernet" },
120 	{ PCI_PRODUCT_3COM_3C900BCOMBO,	EX_CONF_90XB,
121 	  "3c900B-COMBO Ethernet" },
122 	{ PCI_PRODUCT_3COM_3C900BTPC,   EX_CONF_90XB,
123 	  "3c900B-TPC Ethernet" },
124 
125 	{ PCI_PRODUCT_3COM_3C905BTX,	EX_CONF_90XB|EX_CONF_MII|EX_CONF_INTPHY,
126 	  "3c905B-TX 10/100 Ethernet" },
127 	{ PCI_PRODUCT_3COM_3C905BT4,	EX_CONF_90XB|EX_CONF_MII,
128 	  "3c905B-T4 10/100 Ethernet" },
129 	{ PCI_PRODUCT_3COM_3C905BCOMBO,	EX_CONF_90XB/*|EX_CONF_MII|EX_CONF_INTPHY*/,
130 	  "3c905B-COMBO 10/100 Ethernet" },
131 	{ PCI_PRODUCT_3COM_3C905BFX,	EX_CONF_90XB,
132 	  "3c905B-FX 10/100 Ethernet" },
133 
134 	/* XXX Internal PHY? */
135 	{ PCI_PRODUCT_3COM_3C980SRV,	EX_CONF_90XB,
136 	  "3c980 Server Adapter 10/100 Ethernet" },
137 	{ PCI_PRODUCT_3COM_3C980CTXM,	EX_CONF_90XB,
138 	  "3c980C-TXM 10/100 Ethernet" },
139 
140 	{ PCI_PRODUCT_3COM_3C905CTX,	EX_CONF_90XB|EX_CONF_MII,
141 	  "3c905C-TX 10/100 Ethernet with mngmt" },
142 
143 	{ PCI_PRODUCT_3COM_3C450TX,		EX_CONF_90XB,
144 	  "3c450-TX 10/100 Ethernet" },
145 
146 	{ PCI_PRODUCT_3COM_3CSOHO100TX,	EX_CONF_90XB,
147 	  "3cSOHO100-TX 10/100 Ethernet" },
148 
149 	{ PCI_PRODUCT_3COM_3C555,
150 	   EX_CONF_90XB | EX_CONF_MII | EX_CONF_EEPROM_OFF |
151 	   EX_CONF_EEPROM_8BIT,
152 	  "3c555 MiniPCI 10/100 Ethernet" },
153 
154 	{ PCI_PRODUCT_3COM_3C556,
155 	   EX_CONF_90XB | EX_CONF_MII | EX_CONF_EEPROM_OFF |
156 	   EX_CONF_PCI_FUNCREG | EX_CONF_RESETHACK | EX_CONF_INV_LED_POLARITY |
157 	   EX_CONF_PHY_POWER | EX_CONF_EEPROM_8BIT,
158 	  "3c556 MiniPCI 10/100 Ethernet" },
159 
160 	{ PCI_PRODUCT_3COM_3C556B,
161 	   EX_CONF_90XB | EX_CONF_MII | EX_CONF_EEPROM_OFF |
162 	   EX_CONF_PCI_FUNCREG | EX_CONF_RESETHACK | EX_CONF_INV_LED_POLARITY |
163 	   EX_CONF_PHY_POWER,
164 	  "3c556B MiniPCI 10/100 Ethernet" },
165 
166 	{ 0,				0,
167 	  NULL },
168 };
169 
170 const struct ex_pci_product *ex_pci_lookup
171     __P((const struct pci_attach_args *));
172 
173 const struct ex_pci_product *
174 ex_pci_lookup(pa)
175 	const struct pci_attach_args *pa;
176 {
177 	const struct ex_pci_product *epp;
178 
179 	if (PCI_VENDOR(pa->pa_id) != PCI_VENDOR_3COM)
180 		return (NULL);
181 
182 	for (epp = ex_pci_products; epp->epp_name != NULL; epp++)
183 		if (PCI_PRODUCT(pa->pa_id) == epp->epp_prodid)
184 			return (epp);
185 	return (NULL);
186 }
187 
188 int
189 ex_pci_match(parent, match, aux)
190 	struct device *parent;
191 	struct cfdata *match;
192 	void *aux;
193 {
194 	struct pci_attach_args *pa = (struct pci_attach_args *) aux;
195 
196 	if (ex_pci_lookup(pa) != NULL)
197 		return (2);	/* beat ep_pci */
198 
199 	return (0);
200 }
201 
202 void
203 ex_pci_attach(parent, self, aux)
204 	struct device *parent, *self;
205 	void *aux;
206 {
207 	struct ex_softc *sc = (void *)self;
208 	struct ex_pci_softc *psc = (void *)self;
209 	struct pci_attach_args *pa = aux;
210 	pci_chipset_tag_t pc = pa->pa_pc;
211 	pci_intr_handle_t ih;
212 	const struct ex_pci_product *epp;
213 	const char *intrstr = NULL;
214 	int rev, pmreg;
215 	pcireg_t reg;
216 
217 	if (pci_mapreg_map(pa, PCI_CBIO, PCI_MAPREG_TYPE_IO, 0,
218 	    &sc->sc_iot, &sc->sc_ioh, NULL, NULL)) {
219 		printf(": can't map i/o space\n");
220 		return;
221 	}
222 
223 	epp = ex_pci_lookup(pa);
224 	if (epp == NULL) {
225 		printf("\n");
226 		panic("ex_pci_attach: impossible");
227 	}
228 
229 	rev = PCI_REVISION(pci_conf_read(pc, pa->pa_tag, PCI_CLASS_REG));
230 	printf(": 3Com %s (rev. 0x%x)\n", epp->epp_name, rev);
231 
232 	sc->enable = NULL;
233 	sc->disable = NULL;
234 	sc->enabled = 1;
235 
236 	sc->sc_dmat = pa->pa_dmat;
237 
238 	sc->ex_bustype = EX_BUS_PCI;
239 	sc->ex_conf = epp->epp_flags;
240 
241 	/* Enable the card. */
242 	pci_conf_write(pc, pa->pa_tag, PCI_COMMAND_STATUS_REG,
243 	    pci_conf_read(pc, pa->pa_tag, PCI_COMMAND_STATUS_REG) |
244 	    PCI_COMMAND_MASTER_ENABLE);
245 
246 	/* Get it out of power save mode if needed (BIOS bugs) */
247 	if (pci_get_capability(pc, pa->pa_tag, PCI_CAP_PWRMGMT, &pmreg, 0)) {
248 		reg = pci_conf_read(pc, pa->pa_tag, pmreg + 4) & 0x3;
249 		if (reg == 3) {
250 			/*
251 			 * The card has lost all configuration data in
252 			 * this state, so punt.
253 			 */
254 			printf("%s: unable to wake up from power state D3\n",
255 			    sc->sc_dev.dv_xname);
256 			return;
257 		}
258 		if (reg != 0) {
259 			printf("%s: waking up from power state D%d\n",
260 			    sc->sc_dev.dv_xname, reg);
261 			pci_conf_write(pc, pa->pa_tag, pmreg + 4, 0);
262 		}
263 	}
264 
265 	/* Map and establish the interrupt. */
266 	if (pci_intr_map(pa, &ih)) {
267 		printf("%s: couldn't map interrupt\n", sc->sc_dev.dv_xname);
268 		return;
269 	}
270 
271 	if (sc->ex_conf & EX_CONF_PCI_FUNCREG) {
272 		sc->intr_ack = ex_pci_intr_ack;
273 		psc->sc_chiptag = pa->pa_pc;
274 		psc->sc_pcitag = pa->pa_tag;
275 	}
276 
277 	intrstr = pci_intr_string(pc, ih);
278 	sc->sc_ih = pci_intr_establish(pc, ih, IPL_NET, ex_intr, sc);
279 	if (sc->sc_ih == NULL) {
280 		printf("%s: couldn't establish interrupt",
281 		    sc->sc_dev.dv_xname);
282 		if (intrstr != NULL)
283 			printf(" at %s", intrstr);
284 		printf("\n");
285 		return;
286 	}
287 	printf("%s: interrupting at %s\n", sc->sc_dev.dv_xname, intrstr);
288 
289 	ex_config(sc);
290 }
291 
292 void
293 ex_pci_intr_ack(sc)
294 	struct ex_softc *sc;
295 {
296 	struct ex_pci_softc *psc = (struct ex_pci_softc *)sc;
297 
298 	pci_conf_write(psc->sc_chiptag, psc->sc_pcitag, PCI_FUNCMEM + 4,
299 	    PCI_INTRACK);
300 }
301