xref: /netbsd-src/sys/dev/pci/if_ex_pci.c (revision d48f14661dda8638fee055ba15d35bdfb29b9fa8)
1 /*	$NetBSD: if_ex_pci.c,v 1.41 2006/06/17 23:34:26 christos 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.41 2006/06/17 23:34:26 christos 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[0x40>>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 static int	ex_pci_match(struct device *, struct cfdata *, void *);
102 static void	ex_pci_attach(struct device *, struct device *, void *);
103 static void	ex_pci_intr_ack(struct ex_softc *);
104 
105 static int	ex_pci_enable(struct ex_softc *);
106 static void	ex_pci_disable(struct ex_softc *);
107 
108 static void	ex_pci_confreg_restore(struct ex_pci_softc *);
109 static int	ex_d3tod0(pci_chipset_tag_t, pcitag_t, void *, pcireg_t);
110 
111 CFATTACH_DECL(ex_pci, sizeof(struct ex_pci_softc),
112     ex_pci_match, ex_pci_attach, NULL, NULL);
113 
114 static const struct ex_pci_product {
115 	u_int32_t	epp_prodid;	/* PCI product ID */
116 	int		epp_flags;	/* initial softc flags */
117 	const char	*epp_name;	/* device name */
118 } ex_pci_products[] = {
119 	{ PCI_PRODUCT_3COM_3C900TPO,	0,
120 	  "3c900-TPO Ethernet" },
121 	{ PCI_PRODUCT_3COM_3C900COMBO,	0,
122 	  "3c900-COMBO Ethernet" },
123 
124 	{ PCI_PRODUCT_3COM_3C905TX,	EX_CONF_MII,
125 	  "3c905-TX 10/100 Ethernet" },
126 	{ PCI_PRODUCT_3COM_3C905T4,	EX_CONF_MII,
127 	  "3c905-T4 10/100 Ethernet" },
128 
129 	{ PCI_PRODUCT_3COM_3C900BTPO,	EX_CONF_90XB,
130 	  "3c900B-TPO Ethernet" },
131 	{ PCI_PRODUCT_3COM_3C900BCOMBO,	EX_CONF_90XB,
132 	  "3c900B-COMBO Ethernet" },
133 	{ PCI_PRODUCT_3COM_3C900BTPC,   EX_CONF_90XB,
134 	  "3c900B-TPC Ethernet" },
135 
136 	{ PCI_PRODUCT_3COM_3C905BTX,	EX_CONF_90XB|EX_CONF_MII|EX_CONF_INTPHY,
137 	  "3c905B-TX 10/100 Ethernet" },
138 	{ PCI_PRODUCT_3COM_3C905BT4,	EX_CONF_90XB|EX_CONF_MII,
139 	  "3c905B-T4 10/100 Ethernet" },
140 	{ PCI_PRODUCT_3COM_3C905BCOMBO,	EX_CONF_90XB/*|EX_CONF_MII|EX_CONF_INTPHY*/,
141 	  "3c905B-COMBO 10/100 Ethernet" },
142 	{ PCI_PRODUCT_3COM_3C905BFX,	EX_CONF_90XB,
143 	  "3c905B-FX 10/100 Ethernet" },
144 
145 	/* XXX Internal PHY? */
146 	{ PCI_PRODUCT_3COM_3C980SRV,	EX_CONF_90XB,
147 	  "3c980 Server Adapter 10/100 Ethernet" },
148 	{ PCI_PRODUCT_3COM_3C980CTXM,	EX_CONF_90XB|EX_CONF_MII,
149 	  "3c980C-TXM 10/100 Ethernet" },
150 
151 	{ PCI_PRODUCT_3COM_3C905CTX,	EX_CONF_90XB|EX_CONF_MII,
152 	  "3c905C-TX 10/100 Ethernet with mngmt" },
153 
154 	{ PCI_PRODUCT_3COM_3C450TX,		EX_CONF_90XB,
155 	  "3c450-TX 10/100 Ethernet" },
156 
157 	{ PCI_PRODUCT_3COM_3CSOHO100TX,	EX_CONF_90XB,
158 	  "3cSOHO100-TX 10/100 Ethernet" },
159 
160 	{ PCI_PRODUCT_3COM_3C555,
161 	   EX_CONF_90XB | EX_CONF_MII | EX_CONF_EEPROM_OFF |
162 	   EX_CONF_EEPROM_8BIT,
163 	  "3c555 MiniPCI 10/100 Ethernet" },
164 
165 	{ PCI_PRODUCT_3COM_3C556,
166 	   EX_CONF_90XB | EX_CONF_MII | EX_CONF_EEPROM_OFF |
167 	   EX_CONF_PCI_FUNCREG | EX_CONF_RESETHACK | EX_CONF_INV_LED_POLARITY |
168 	   EX_CONF_PHY_POWER | EX_CONF_EEPROM_8BIT,
169 	  "3c556 MiniPCI 10/100 Ethernet" },
170 
171 	{ PCI_PRODUCT_3COM_3C556B,
172 	   EX_CONF_90XB | EX_CONF_MII | EX_CONF_EEPROM_OFF |
173 	   EX_CONF_PCI_FUNCREG | EX_CONF_RESETHACK | EX_CONF_INV_LED_POLARITY |
174 	   EX_CONF_PHY_POWER | EX_CONF_NO_XCVR_PWR,
175 	  "3c556B MiniPCI 10/100 Ethernet" },
176 
177 	{ PCI_PRODUCT_3COM_3C905CXTX,	EX_CONF_90XB|EX_CONF_MII,
178 	  "3c905CX-TX 10/100 Ethernet with mngmt" },
179 
180 	{ PCI_PRODUCT_3COM_3C920BEMBW,	EX_CONF_90XB|EX_CONF_MII,
181 	  "3c920B-EMB-WNM Integrated Fast Ethernet" },
182 
183 	{ 0,				0,
184 	  NULL },
185 };
186 
187 static const struct ex_pci_product *
188 ex_pci_lookup(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 static int
202 ex_pci_match(struct device *parent, struct cfdata *match, void *aux)
203 {
204 	struct pci_attach_args *pa = (struct pci_attach_args *) aux;
205 
206 	if (ex_pci_lookup(pa) != NULL)
207 		return (2);	/* beat ep_pci */
208 
209 	return (0);
210 }
211 
212 static void
213 ex_pci_attach(struct device *parent, struct device *self, void *aux)
214 {
215 	struct ex_softc *sc = (void *)self;
216 	struct ex_pci_softc *psc = (void *)self;
217 	struct pci_attach_args *pa = aux;
218 	pci_chipset_tag_t pc = pa->pa_pc;
219 	pci_intr_handle_t ih;
220 	const struct ex_pci_product *epp;
221 	const char *intrstr = NULL;
222 	int rev;
223 	int error;
224 
225 	aprint_naive(": Ethernet controller\n");
226 
227 	if (pci_mapreg_map(pa, PCI_CBIO, PCI_MAPREG_TYPE_IO, 0,
228 	    &sc->sc_iot, &sc->sc_ioh, NULL, NULL)) {
229 		aprint_error(": can't map i/o space\n");
230 		return;
231 	}
232 
233 	epp = ex_pci_lookup(pa);
234 	if (epp == NULL) {
235 		printf("\n");
236 		panic("ex_pci_attach: impossible");
237 	}
238 
239 	rev = PCI_REVISION(pci_conf_read(pc, pa->pa_tag, PCI_CLASS_REG));
240 	aprint_normal(": 3Com %s (rev. 0x%x)\n", epp->epp_name, rev);
241 
242 	sc->sc_dmat = pa->pa_dmat;
243 
244 	sc->ex_bustype = EX_BUS_PCI;
245 	sc->ex_conf = epp->epp_flags;
246 
247 	/* Enable the card. */
248 	pci_conf_write(pc, pa->pa_tag, PCI_COMMAND_STATUS_REG,
249 	    pci_conf_read(pc, pa->pa_tag, PCI_COMMAND_STATUS_REG) |
250 	    PCI_COMMAND_MASTER_ENABLE);
251 
252 	psc->psc_pc = pc;
253 	psc->psc_tag = pa->pa_tag;
254 	psc->psc_regs[PCI_COMMAND_STATUS_REG>>2] =
255 	    pci_conf_read(pc, pa->pa_tag, PCI_COMMAND_STATUS_REG);
256 	psc->psc_regs[PCI_BHLC_REG>>2] =
257 	    pci_conf_read(pc, pa->pa_tag, PCI_BHLC_REG);
258 	psc->psc_regs[PCI_CBIO>>2] =
259 	    pci_conf_read(pc, pa->pa_tag, PCI_CBIO);
260 
261 	if (sc->ex_conf & EX_CONF_PCI_FUNCREG) {
262 		/* Map PCI function status window. */
263 		if (pci_mapreg_map(pa, PCI_FUNCMEM, PCI_MAPREG_TYPE_MEM, 0,
264 		    &psc->sc_funct, &psc->sc_funch, NULL, NULL)) {
265 			aprint_error(
266 			    "%s: unable to map function status window\n",
267 			    sc->sc_dev.dv_xname);
268 			return;
269 		}
270 		sc->intr_ack = ex_pci_intr_ack;
271 
272 		psc->psc_regs[PCI_FUNCMEM>>2] =
273 		    pci_conf_read(pc, pa->pa_tag, PCI_FUNCMEM);
274 	}
275 
276 	psc->psc_regs[PCI_INTERRUPT_REG>>2] =
277 	    pci_conf_read(pc, pa->pa_tag, PCI_INTERRUPT_REG);
278 
279 	/* power up chip */
280 	switch ((error = pci_activate(pa->pa_pc, pa->pa_tag, sc, ex_d3tod0))) {
281 	case EOPNOTSUPP:
282 		break;
283 	case 0:
284 		sc->enable = ex_pci_enable;
285 		sc->disable = ex_pci_disable;
286 		break;
287 	default:
288 		aprint_error("%s: cannot activate %d\n", sc->sc_dev.dv_xname,
289 		    error);
290 		return;
291 	}
292 	sc->enabled = 1;
293 
294 	/* Map and establish the interrupt. */
295 	if (pci_intr_map(pa, &ih)) {
296 		aprint_error("%s: couldn't map interrupt\n",
297 		    sc->sc_dev.dv_xname);
298 		return;
299 	}
300 
301 	intrstr = pci_intr_string(pc, ih);
302 	sc->sc_ih = pci_intr_establish(pc, ih, IPL_NET, ex_intr, sc);
303 	if (sc->sc_ih == NULL) {
304 		aprint_error("%s: couldn't establish interrupt",
305 		    sc->sc_dev.dv_xname);
306 		if (intrstr != NULL)
307 			aprint_normal(" at %s", intrstr);
308 		aprint_normal("\n");
309 		return;
310 	}
311 	aprint_normal("%s: interrupting at %s\n", sc->sc_dev.dv_xname, intrstr);
312 
313 	ex_config(sc);
314 
315 	if (sc->ex_conf & EX_CONF_PCI_FUNCREG)
316 		bus_space_write_4(psc->sc_funct, psc->sc_funch, PCI_INTR,
317 		    PCI_INTRACK);
318 
319 	if (sc->disable != NULL)
320 		ex_disable(sc);
321 }
322 
323 static void
324 ex_pci_intr_ack(struct ex_softc *sc)
325 {
326 	struct ex_pci_softc *psc = (struct ex_pci_softc *)sc;
327 
328 	bus_space_write_4(psc->sc_funct, psc->sc_funch, PCI_INTR,
329 	    PCI_INTRACK);
330 }
331 
332 static int
333 ex_d3tod0(pci_chipset_tag_t pc, pcitag_t tag, void *ssc, pcireg_t state)
334 {
335 
336 #define PCI_CACHE_LAT_BIST	0x0c
337 #define PCI_BAR0		0x10
338 #define PCI_BAR1		0x14
339 #define PCI_BAR2		0x18
340 #define PCI_BAR3		0x1C
341 #define PCI_BAR4		0x20
342 #define PCI_BAR5		0x24
343 #define PCI_EXP_ROM_BAR		0x30
344 #define PCI_INT_GNT_LAT		0x3c
345 
346 	u_int32_t base0;
347 	u_int32_t base1;
348 	u_int32_t romaddr;
349 	u_int32_t pci_command;
350 	u_int32_t pci_int_lat;
351 	u_int32_t pci_cache_lat;
352 	struct ex_softc *sc = ssc;
353 
354 	if (state != PCI_PMCSR_STATE_D3)
355 		return 0;
356 
357 	aprint_normal("%s: found in power state D%d, "
358 	    "attempting to recover.\n", sc->sc_dev.dv_xname, state);
359 	pci_command = pci_conf_read(pc, tag, PCI_COMMAND_STATUS_REG);
360 	base0 = pci_conf_read(pc, tag, PCI_BAR0);
361 	base1 = pci_conf_read(pc, tag, PCI_BAR1);
362 	romaddr	= pci_conf_read(pc, tag, PCI_EXP_ROM_BAR);
363 	pci_cache_lat= pci_conf_read(pc, tag, PCI_CACHE_LAT_BIST);
364 	pci_int_lat = pci_conf_read(pc, tag, PCI_INT_GNT_LAT);
365 
366 	pci_conf_write(pc, tag, PCI_POWERCTL, 0);
367 	pci_conf_write(pc, tag, PCI_BAR0, base0);
368 	pci_conf_write(pc, tag, PCI_BAR1, base1);
369 	pci_conf_write(pc, tag, PCI_EXP_ROM_BAR, romaddr);
370 	pci_conf_write(pc, tag, PCI_INT_GNT_LAT, pci_int_lat);
371 	pci_conf_write(pc, tag, PCI_CACHE_LAT_BIST, pci_cache_lat);
372 	pci_conf_write(pc, tag, PCI_COMMAND_STATUS_REG,
373 	    (PCI_COMMAND_MASTER_ENABLE | PCI_COMMAND_IO_ENABLE));
374 	aprint_normal("%s: changed power state to D0.\n",
375 	    sc->sc_dev.dv_xname);
376 	return 0;
377 }
378 
379 static void
380 ex_pci_confreg_restore(struct ex_pci_softc *psc)
381 {
382 	struct ex_softc *sc = (void *) psc;
383 	pcireg_t reg;
384 
385 	reg = pci_conf_read(psc->psc_pc, psc->psc_tag, PCI_COMMAND_STATUS_REG);
386 
387 	pci_conf_write(psc->psc_pc, psc->psc_tag,
388 	    PCI_COMMAND_STATUS_REG,
389 	    (reg & 0xffff0000) |
390 	    (psc->psc_regs[PCI_COMMAND_STATUS_REG>>2] & 0xffff));
391 	pci_conf_write(psc->psc_pc, psc->psc_tag, PCI_BHLC_REG,
392 	    psc->psc_regs[PCI_BHLC_REG>>2]);
393 	pci_conf_write(psc->psc_pc, psc->psc_tag, PCI_CBIO,
394 	    psc->psc_regs[PCI_CBIO>>2]);
395 	if (sc->ex_conf & EX_CONF_PCI_FUNCREG)
396 		pci_conf_write(psc->psc_pc, psc->psc_tag, PCI_FUNCMEM,
397 		    psc->psc_regs[PCI_FUNCMEM>>2]);
398 	pci_conf_write(psc->psc_pc, psc->psc_tag, PCI_INTERRUPT_REG,
399 	    psc->psc_regs[PCI_INTERRUPT_REG>>2]);
400 }
401 
402 static int
403 ex_pci_enable(struct ex_softc *sc)
404 {
405 	struct ex_pci_softc *psc = (void *) sc;
406 
407 #if 0
408 	printf("%s: going to power state D0\n", sc->sc_dev.dv_xname);
409 #endif
410 
411 	/* Bring the device into D0 power state. */
412 	pci_conf_write(psc->psc_pc, psc->psc_tag,
413 	    psc->psc_pwrmgmt_csr_reg, psc->psc_pwrmgmt_csr);
414 
415 	/* Now restore the configuration registers. */
416 	ex_pci_confreg_restore(psc);
417 
418 	return (0);
419 }
420 
421 static void
422 ex_pci_disable(struct ex_softc *sc)
423 {
424 	struct ex_pci_softc *psc = (void *) sc;
425 
426 #if 0
427 	printf("%s: going to power state D3\n", sc->sc_dev.dv_xname);
428 #endif
429 
430 	/* Put the device into D3 state. */
431 	pci_conf_write(psc->psc_pc, psc->psc_tag,
432 	    psc->psc_pwrmgmt_csr_reg, (psc->psc_pwrmgmt_csr &
433 	    ~PCI_PMCSR_STATE_MASK) | PCI_PMCSR_STATE_D3);
434 }
435