xref: /netbsd-src/sys/dev/pci/if_ex_pci.c (revision 453f6b99a313f2f372963fe81f55bf6f811e3f55)
1 /*	$NetBSD: if_ex_pci.c,v 1.32 2003/01/31 00:07:42 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 <sys/cdefs.h>
41 __KERNEL_RCSID(0, "$NetBSD: if_ex_pci.c,v 1.32 2003/01/31 00:07:42 thorpej Exp $");
42 
43 #include <sys/param.h>
44 #include <sys/systm.h>
45 #include <sys/mbuf.h>
46 #include <sys/socket.h>
47 #include <sys/ioctl.h>
48 #include <sys/errno.h>
49 #include <sys/syslog.h>
50 #include <sys/select.h>
51 #include <sys/device.h>
52 
53 #include <net/if.h>
54 #include <net/if_dl.h>
55 #include <net/if_ether.h>
56 #include <net/if_media.h>
57 
58 #include <machine/cpu.h>
59 #include <machine/bus.h>
60 #include <machine/intr.h>
61 
62 #include <dev/mii/miivar.h>
63 #include <dev/mii/mii.h>
64 
65 #include <dev/ic/elink3var.h>
66 #include <dev/ic/elink3reg.h>
67 #include <dev/ic/elinkxlreg.h>
68 #include <dev/ic/elinkxlvar.h>
69 
70 #include <dev/pci/pcivar.h>
71 #include <dev/pci/pcireg.h>
72 #include <dev/pci/pcidevs.h>
73 
74 struct ex_pci_softc {
75 	struct ex_softc sc_ex;
76 
77 	/* PCI function status space. 556,556B requests it. */
78 	bus_space_tag_t sc_funct;
79 	bus_space_handle_t sc_funch;
80 
81 	pci_chipset_tag_t psc_pc;	/* pci chipset tag */
82 	pcireg_t psc_regs[0x20>>2];	/* saved PCI config regs (sparse) */
83 	pcitag_t psc_tag;		/* pci device tag */
84 
85 	int psc_pwrmgmt_csr_reg;	/* ACPI power management register */
86 	pcireg_t psc_pwrmgmt_csr;	/* ...and the contents at D0 */
87 };
88 
89 /*
90  * PCI constants.
91  * XXX These should be in a common file!
92  */
93 #define PCI_CONN		0x48    /* Connector type */
94 #define PCI_CBIO		0x10    /* Configuration Base IO Address */
95 #define PCI_POWERCTL		0xe0
96 #define PCI_FUNCMEM		0x18
97 
98 #define PCI_INTR		4
99 #define PCI_INTRACK		0x00008000
100 
101 int ex_pci_match __P((struct device *, struct cfdata *, void *));
102 void ex_pci_attach __P((struct device *, struct device *, void *));
103 void ex_pci_intr_ack __P((struct ex_softc *));
104 
105 int ex_pci_enable __P((struct ex_softc *));
106 void ex_pci_disable __P((struct ex_softc *));
107 
108 void ex_pci_confreg_restore __P((struct ex_pci_softc *));
109 
110 CFATTACH_DECL(ex_pci, sizeof(struct ex_pci_softc),
111     ex_pci_match, ex_pci_attach, NULL, NULL);
112 
113 const struct ex_pci_product {
114 	u_int32_t	epp_prodid;	/* PCI product ID */
115 	int		epp_flags;	/* initial softc flags */
116 	const char	*epp_name;	/* device name */
117 } ex_pci_products[] = {
118 	{ PCI_PRODUCT_3COM_3C900TPO,	0,
119 	  "3c900-TPO Ethernet" },
120 	{ PCI_PRODUCT_3COM_3C900COMBO,	0,
121 	  "3c900-COMBO Ethernet" },
122 
123 	{ PCI_PRODUCT_3COM_3C905TX,	EX_CONF_MII,
124 	  "3c905-TX 10/100 Ethernet" },
125 	{ PCI_PRODUCT_3COM_3C905T4,	EX_CONF_MII,
126 	  "3c905-T4 10/100 Ethernet" },
127 
128 	{ PCI_PRODUCT_3COM_3C900BTPO,	EX_CONF_90XB,
129 	  "3c900B-TPO Ethernet" },
130 	{ PCI_PRODUCT_3COM_3C900BCOMBO,	EX_CONF_90XB,
131 	  "3c900B-COMBO Ethernet" },
132 	{ PCI_PRODUCT_3COM_3C900BTPC,   EX_CONF_90XB,
133 	  "3c900B-TPC Ethernet" },
134 
135 	{ PCI_PRODUCT_3COM_3C905BTX,	EX_CONF_90XB|EX_CONF_MII|EX_CONF_INTPHY,
136 	  "3c905B-TX 10/100 Ethernet" },
137 	{ PCI_PRODUCT_3COM_3C905BT4,	EX_CONF_90XB|EX_CONF_MII,
138 	  "3c905B-T4 10/100 Ethernet" },
139 	{ PCI_PRODUCT_3COM_3C905BCOMBO,	EX_CONF_90XB/*|EX_CONF_MII|EX_CONF_INTPHY*/,
140 	  "3c905B-COMBO 10/100 Ethernet" },
141 	{ PCI_PRODUCT_3COM_3C905BFX,	EX_CONF_90XB,
142 	  "3c905B-FX 10/100 Ethernet" },
143 
144 	/* XXX Internal PHY? */
145 	{ PCI_PRODUCT_3COM_3C980SRV,	EX_CONF_90XB,
146 	  "3c980 Server Adapter 10/100 Ethernet" },
147 	{ PCI_PRODUCT_3COM_3C980CTXM,	EX_CONF_90XB|EX_CONF_MII,
148 	  "3c980C-TXM 10/100 Ethernet" },
149 
150 	{ PCI_PRODUCT_3COM_3C905CTX,	EX_CONF_90XB|EX_CONF_MII,
151 	  "3c905C-TX 10/100 Ethernet with mngmt" },
152 
153 	{ PCI_PRODUCT_3COM_3C450TX,		EX_CONF_90XB,
154 	  "3c450-TX 10/100 Ethernet" },
155 
156 	{ PCI_PRODUCT_3COM_3CSOHO100TX,	EX_CONF_90XB,
157 	  "3cSOHO100-TX 10/100 Ethernet" },
158 
159 	{ PCI_PRODUCT_3COM_3C555,
160 	   EX_CONF_90XB | EX_CONF_MII | EX_CONF_EEPROM_OFF |
161 	   EX_CONF_EEPROM_8BIT,
162 	  "3c555 MiniPCI 10/100 Ethernet" },
163 
164 	{ PCI_PRODUCT_3COM_3C556,
165 	   EX_CONF_90XB | EX_CONF_MII | EX_CONF_EEPROM_OFF |
166 	   EX_CONF_PCI_FUNCREG | EX_CONF_RESETHACK | EX_CONF_INV_LED_POLARITY |
167 	   EX_CONF_PHY_POWER | EX_CONF_EEPROM_8BIT,
168 	  "3c556 MiniPCI 10/100 Ethernet" },
169 
170 	{ PCI_PRODUCT_3COM_3C556B,
171 	   EX_CONF_90XB | EX_CONF_MII | EX_CONF_EEPROM_OFF |
172 	   EX_CONF_PCI_FUNCREG | EX_CONF_RESETHACK | EX_CONF_INV_LED_POLARITY |
173 	   EX_CONF_PHY_POWER,
174 	  "3c556B MiniPCI 10/100 Ethernet" },
175 
176 	{ PCI_PRODUCT_3COM_3C905CXTX,	EX_CONF_90XB|EX_CONF_MII,
177 	  "3c905CX-TX 10/100 Ethernet with mngmt" },
178 
179 	{ 0,				0,
180 	  NULL },
181 };
182 
183 const struct ex_pci_product *ex_pci_lookup
184     __P((const struct pci_attach_args *));
185 
186 const struct ex_pci_product *
187 ex_pci_lookup(pa)
188 	const struct pci_attach_args *pa;
189 {
190 	const struct ex_pci_product *epp;
191 
192 	if (PCI_VENDOR(pa->pa_id) != PCI_VENDOR_3COM)
193 		return (NULL);
194 
195 	for (epp = ex_pci_products; epp->epp_name != NULL; epp++)
196 		if (PCI_PRODUCT(pa->pa_id) == epp->epp_prodid)
197 			return (epp);
198 	return (NULL);
199 }
200 
201 int
202 ex_pci_match(parent, match, aux)
203 	struct device *parent;
204 	struct cfdata *match;
205 	void *aux;
206 {
207 	struct pci_attach_args *pa = (struct pci_attach_args *) aux;
208 
209 	if (ex_pci_lookup(pa) != NULL)
210 		return (2);	/* beat ep_pci */
211 
212 	return (0);
213 }
214 
215 void
216 ex_pci_attach(parent, self, aux)
217 	struct device *parent, *self;
218 	void *aux;
219 {
220 	struct ex_softc *sc = (void *)self;
221 	struct ex_pci_softc *psc = (void *)self;
222 	struct pci_attach_args *pa = aux;
223 	pci_chipset_tag_t pc = pa->pa_pc;
224 	pci_intr_handle_t ih;
225 	const struct ex_pci_product *epp;
226 	const char *intrstr = NULL;
227 	int rev, pmreg;
228 	pcireg_t reg;
229 
230 	aprint_naive(": Ethernet controller\n");
231 
232 	if (pci_mapreg_map(pa, PCI_CBIO, PCI_MAPREG_TYPE_IO, 0,
233 	    &sc->sc_iot, &sc->sc_ioh, NULL, NULL)) {
234 		aprint_error(": can't map i/o space\n");
235 		return;
236 	}
237 
238 	epp = ex_pci_lookup(pa);
239 	if (epp == NULL) {
240 		printf("\n");
241 		panic("ex_pci_attach: impossible");
242 	}
243 
244 	rev = PCI_REVISION(pci_conf_read(pc, pa->pa_tag, PCI_CLASS_REG));
245 	aprint_normal(": 3Com %s (rev. 0x%x)\n", epp->epp_name, rev);
246 
247 	sc->sc_dmat = pa->pa_dmat;
248 
249 	sc->ex_bustype = EX_BUS_PCI;
250 	sc->ex_conf = epp->epp_flags;
251 
252 	/* Enable the card. */
253 	pci_conf_write(pc, pa->pa_tag, PCI_COMMAND_STATUS_REG,
254 	    pci_conf_read(pc, pa->pa_tag, PCI_COMMAND_STATUS_REG) |
255 	    PCI_COMMAND_MASTER_ENABLE);
256 
257 	psc->psc_pc = pc;
258 	psc->psc_tag = pa->pa_tag;
259 	psc->psc_regs[PCI_COMMAND_STATUS_REG>>2] =
260 	    pci_conf_read(pc, pa->pa_tag, PCI_COMMAND_STATUS_REG);
261 	psc->psc_regs[PCI_BHLC_REG>>2] =
262 	    pci_conf_read(pc, pa->pa_tag, PCI_BHLC_REG);
263 	psc->psc_regs[PCI_CBIO>>2] =
264 	    pci_conf_read(pc, pa->pa_tag, PCI_CBIO);
265 
266 	if (sc->ex_conf & EX_CONF_PCI_FUNCREG) {
267 		/* Map PCI function status window. */
268 		if (pci_mapreg_map(pa, PCI_FUNCMEM, PCI_MAPREG_TYPE_MEM, 0,
269 		    &psc->sc_funct, &psc->sc_funch, NULL, NULL)) {
270 			aprint_error(
271 			    "%s: unable to map function status window\n",
272 			    sc->sc_dev.dv_xname);
273 			return;
274 		}
275 		sc->intr_ack = ex_pci_intr_ack;
276 
277 		psc->psc_regs[PCI_FUNCMEM>>2] =
278 		    pci_conf_read(pc, pa->pa_tag, PCI_FUNCMEM);
279 	}
280 
281 	psc->psc_regs[PCI_INTERRUPT_REG>>2] =
282 	    pci_conf_read(pc, pa->pa_tag, PCI_INTERRUPT_REG);
283 
284 	/* Get it out of power save mode if needed (BIOS bugs) */
285 	if (pci_get_capability(pc, pa->pa_tag, PCI_CAP_PWRMGMT, &pmreg, 0)) {
286 		sc->enable = ex_pci_enable;
287 		sc->disable = ex_pci_disable;
288 
289 		psc->psc_pwrmgmt_csr_reg = pmreg + PCI_PMCSR;
290 		reg = pci_conf_read(pc, pa->pa_tag, psc->psc_pwrmgmt_csr_reg);
291 
292 		psc->psc_pwrmgmt_csr = (reg & ~PCI_PMCSR_STATE_MASK) |
293 		    PCI_PMCSR_STATE_D0;
294 
295 		switch (reg & PCI_PMCSR_STATE_MASK) {
296 		case PCI_PMCSR_STATE_D3:
297 			/*
298 			 * The card has lost all configuration data in
299 			 * this state, so punt.
300 			 */
301 			aprint_error(
302 			    "%s: unable to wake up from power state D3\n",
303 			    sc->sc_dev.dv_xname);
304 			return;
305 		case PCI_PMCSR_STATE_D1:
306 		case PCI_PMCSR_STATE_D2:
307 			aprint_normal("%s: waking up from power state D%d\n",
308 			    sc->sc_dev.dv_xname, reg);
309 			pci_conf_write(pc, pa->pa_tag, pmreg + PCI_PMCSR,
310 			    (reg & ~PCI_PMCSR_STATE_MASK) | PCI_PMCSR_STATE_D0);
311 			break;
312 		}
313 	}
314 
315 	sc->enabled = 1;
316 
317 	/* Map and establish the interrupt. */
318 	if (pci_intr_map(pa, &ih)) {
319 		aprint_error("%s: couldn't map interrupt\n",
320 		    sc->sc_dev.dv_xname);
321 		return;
322 	}
323 
324 	intrstr = pci_intr_string(pc, ih);
325 	sc->sc_ih = pci_intr_establish(pc, ih, IPL_NET, ex_intr, sc);
326 	if (sc->sc_ih == NULL) {
327 		aprint_error("%s: couldn't establish interrupt",
328 		    sc->sc_dev.dv_xname);
329 		if (intrstr != NULL)
330 			aprint_normal(" at %s", intrstr);
331 		aprint_normal("\n");
332 		return;
333 	}
334 	aprint_normal("%s: interrupting at %s\n", sc->sc_dev.dv_xname, intrstr);
335 
336 	ex_config(sc);
337 
338 	if (sc->ex_conf & EX_CONF_PCI_FUNCREG)
339 		bus_space_write_4(psc->sc_funct, psc->sc_funch, PCI_INTR,
340 		    PCI_INTRACK);
341 
342 	if (sc->disable != NULL)
343 		ex_disable(sc);
344 }
345 
346 void
347 ex_pci_intr_ack(sc)
348 	struct ex_softc *sc;
349 {
350 	struct ex_pci_softc *psc = (struct ex_pci_softc *)sc;
351 
352 	bus_space_write_4(psc->sc_funct, psc->sc_funch, PCI_INTR,
353 	    PCI_INTRACK);
354 }
355 
356 void
357 ex_pci_confreg_restore(psc)
358 	struct ex_pci_softc *psc;
359 {
360 	struct ex_softc *sc = (void *) psc;
361 	pcireg_t reg;
362 
363 	reg = pci_conf_read(psc->psc_pc, psc->psc_tag, PCI_COMMAND_STATUS_REG);
364 
365 	pci_conf_write(psc->psc_pc, psc->psc_tag,
366 	    PCI_COMMAND_STATUS_REG,
367 	    (reg & 0xffff0000) |
368 	    (psc->psc_regs[PCI_COMMAND_STATUS_REG>>2] & 0xffff));
369 	pci_conf_write(psc->psc_pc, psc->psc_tag, PCI_BHLC_REG,
370 	    psc->psc_regs[PCI_BHLC_REG>>2]);
371 	pci_conf_write(psc->psc_pc, psc->psc_tag, PCI_CBIO,
372 	    psc->psc_regs[PCI_CBIO>>2]);
373 	if (sc->ex_conf & EX_CONF_PCI_FUNCREG)
374 		pci_conf_write(psc->psc_pc, psc->psc_tag, PCI_FUNCMEM,
375 		    psc->psc_regs[PCI_FUNCMEM>>2]);
376 	pci_conf_write(psc->psc_pc, psc->psc_tag, PCI_INTERRUPT_REG,
377 	    psc->psc_regs[PCI_INTERRUPT_REG>>2]);
378 }
379 
380 int
381 ex_pci_enable(sc)
382 	struct ex_softc *sc;
383 {
384 	struct ex_pci_softc *psc = (void *) sc;
385 
386 #if 0
387 	printf("%s: going to power state D0\n", sc->sc_dev.dv_xname);
388 #endif
389 
390 	/* Bring the device into D0 power state. */
391 	pci_conf_write(psc->psc_pc, psc->psc_tag,
392 	    psc->psc_pwrmgmt_csr_reg, psc->psc_pwrmgmt_csr);
393 
394 	/* Now restore the configuration registers. */
395 	ex_pci_confreg_restore(psc);
396 
397 	return (0);
398 }
399 
400 void
401 ex_pci_disable(sc)
402 	struct ex_softc *sc;
403 {
404 	struct ex_pci_softc *psc = (void *) sc;
405 
406 #if 0
407 	printf("%s: going to power state D3\n", sc->sc_dev.dv_xname);
408 #endif
409 
410 	/* Put the device into D3 state. */
411 	pci_conf_write(psc->psc_pc, psc->psc_tag,
412 	    psc->psc_pwrmgmt_csr_reg, (psc->psc_pwrmgmt_csr &
413 	    ~PCI_PMCSR_STATE_MASK) | PCI_PMCSR_STATE_D3);
414 }
415