if_fxp_pci.c revision 1.31 1 /* $NetBSD: if_fxp_pci.c,v 1.31 2003/01/31 00:07:43 thorpej Exp $ */
2
3 /*-
4 * Copyright (c) 1997, 1998, 1999, 2000, 2001 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 * 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 /*
41 * PCI bus front-end for the Intel i82557 fast Ethernet controller
42 * driver. Works with Intel Etherexpress Pro 10+, 100B, 100+ cards.
43 */
44
45 #include <sys/cdefs.h>
46 __KERNEL_RCSID(0, "$NetBSD: if_fxp_pci.c,v 1.31 2003/01/31 00:07:43 thorpej Exp $");
47
48 #include "rnd.h"
49
50 #include <sys/param.h>
51 #include <sys/systm.h>
52 #include <sys/mbuf.h>
53 #include <sys/malloc.h>
54 #include <sys/kernel.h>
55 #include <sys/socket.h>
56 #include <sys/ioctl.h>
57 #include <sys/errno.h>
58 #include <sys/device.h>
59
60 #if NRND > 0
61 #include <sys/rnd.h>
62 #endif
63
64 #include <machine/endian.h>
65
66 #include <net/if.h>
67 #include <net/if_dl.h>
68 #include <net/if_media.h>
69 #include <net/if_ether.h>
70
71 #include <machine/bus.h>
72 #include <machine/intr.h>
73
74 #include <dev/mii/miivar.h>
75
76 #include <dev/ic/i82557reg.h>
77 #include <dev/ic/i82557var.h>
78
79 #include <dev/pci/pcivar.h>
80 #include <dev/pci/pcireg.h>
81 #include <dev/pci/pcidevs.h>
82
83 struct fxp_pci_softc {
84 struct fxp_softc psc_fxp;
85
86 pci_chipset_tag_t psc_pc; /* pci chipset tag */
87 pcireg_t psc_regs[0x20>>2]; /* saved PCI config regs (sparse) */
88 pcitag_t psc_tag; /* pci register tag */
89 void *psc_powerhook; /* power hook */
90
91 int psc_pwrmgmt_csr_reg; /* ACPI power management register */
92 pcireg_t psc_pwrmgmt_csr; /* ...and the contents at D0 */
93 };
94
95 int fxp_pci_match __P((struct device *, struct cfdata *, void *));
96 void fxp_pci_attach __P((struct device *, struct device *, void *));
97
98 int fxp_pci_enable __P((struct fxp_softc *));
99 void fxp_pci_disable __P((struct fxp_softc *));
100
101 static void fxp_pci_confreg_restore __P((struct fxp_pci_softc *psc));
102 static void fxp_pci_power __P((int why, void *arg));
103
104 CFATTACH_DECL(fxp_pci, sizeof(struct fxp_pci_softc),
105 fxp_pci_match, fxp_pci_attach, NULL, NULL);
106
107 const struct fxp_pci_product {
108 u_int32_t fpp_prodid; /* PCI product ID */
109 const char *fpp_name; /* device name */
110 } fxp_pci_products[] = {
111 { PCI_PRODUCT_INTEL_82557,
112 "Intel i82557 Ethernet" },
113 { PCI_PRODUCT_INTEL_82559ER,
114 "Intel i82559ER Ethernet" },
115 { PCI_PRODUCT_INTEL_IN_BUSINESS,
116 "Intel InBusiness Ethernet" },
117 { PCI_PRODUCT_INTEL_82801BA_LAN,
118 "Intel i82562 Ethernet" },
119 { PCI_PRODUCT_INTEL_82801E_LAN_1,
120 "Intel i82559 Ethernet" },
121 { PCI_PRODUCT_INTEL_82801E_LAN_2,
122 "Intel i82559 Ethernet" },
123 { PCI_PRODUCT_INTEL_PRO_100_VE_0,
124 "Intel PRO/100 VE Network Controller" },
125 { PCI_PRODUCT_INTEL_PRO_100_VE_1,
126 "Intel PRO/100 VE Network Controller" },
127 { PCI_PRODUCT_INTEL_PRO_100_VE_2,
128 "Intel PRO/100 VE Network Controller with 82562ET/EZ PHY" },
129 { PCI_PRODUCT_INTEL_PRO_100_VE_3,
130 "Intel PRO/100 VE Network Controller with 82562ET/EZ (CNR) PHY" },
131 { PCI_PRODUCT_INTEL_PRO_100_VE_4,
132 "Intel PRO/100 VE (MOB) Network Controller" },
133 { PCI_PRODUCT_INTEL_PRO_100_VM_0,
134 "Intel PRO/100 VM Network Controller" },
135 { PCI_PRODUCT_INTEL_PRO_100_VM_1,
136 "Intel PRO/100 VM Network Controller" },
137 { PCI_PRODUCT_INTEL_PRO_100_VM_2,
138 "Intel PRO/100 VM Network Controller" },
139 { 0,
140 NULL },
141 };
142
143 static const struct fxp_pci_product *
144 fxp_pci_lookup(const struct pci_attach_args *pa)
145 {
146 const struct fxp_pci_product *fpp;
147
148 if (PCI_VENDOR(pa->pa_id) != PCI_VENDOR_INTEL)
149 return (NULL);
150
151 for (fpp = fxp_pci_products; fpp->fpp_name != NULL; fpp++)
152 if (PCI_PRODUCT(pa->pa_id) == fpp->fpp_prodid)
153 return (fpp);
154
155 return (NULL);
156 }
157
158 int
159 fxp_pci_match(parent, match, aux)
160 struct device *parent;
161 struct cfdata *match;
162 void *aux;
163 {
164 struct pci_attach_args *pa = aux;
165
166 if (fxp_pci_lookup(pa) != NULL)
167 return (1);
168
169 return (0);
170 }
171
172 /*
173 * Restore PCI configuration registers that may have been clobbered.
174 * This is necessary due to bugs on the Sony VAIO Z505-series on-board
175 * ethernet, after an APM suspend/resume, as well as after an ACPI
176 * D3->D0 transition. We call this function from a power hook after
177 * APM resume events, as well as after the ACPI D3->D0 transition.
178 */
179 static void
180 fxp_pci_confreg_restore(psc)
181 struct fxp_pci_softc *psc;
182 {
183 pcireg_t reg;
184
185 #if 0
186 /*
187 * Check to see if the command register is blank -- if so, then
188 * we'll assume that all the clobberable-registers have been
189 * clobbered.
190 */
191
192 /*
193 * In general, the above metric is accurate. Unfortunately,
194 * it is inaccurate across a hibernation. Ideally APM/ACPI
195 * code should take note of hibernation events and execute
196 * a hibernation wakeup hook, but at present a hibernation wake
197 * is indistinguishable from a suspend wake.
198 */
199
200 if (((reg = pci_conf_read(psc->psc_pc, psc->psc_tag,
201 PCI_COMMAND_STATUS_REG)) & 0xffff) != 0)
202 return;
203 #else
204 reg = pci_conf_read(psc->psc_pc, psc->psc_tag, PCI_COMMAND_STATUS_REG);
205 #endif
206
207 pci_conf_write(psc->psc_pc, psc->psc_tag,
208 PCI_COMMAND_STATUS_REG,
209 (reg & 0xffff0000) |
210 (psc->psc_regs[PCI_COMMAND_STATUS_REG>>2] & 0xffff));
211 pci_conf_write(psc->psc_pc, psc->psc_tag, PCI_BHLC_REG,
212 psc->psc_regs[PCI_BHLC_REG>>2]);
213 pci_conf_write(psc->psc_pc, psc->psc_tag, PCI_MAPREG_START+0x0,
214 psc->psc_regs[(PCI_MAPREG_START+0x0)>>2]);
215 pci_conf_write(psc->psc_pc, psc->psc_tag, PCI_MAPREG_START+0x4,
216 psc->psc_regs[(PCI_MAPREG_START+0x4)>>2]);
217 pci_conf_write(psc->psc_pc, psc->psc_tag, PCI_MAPREG_START+0x8,
218 psc->psc_regs[(PCI_MAPREG_START+0x8)>>2]);
219 }
220
221
222 /*
223 * Power handler routine. Called when the system is transitioning into/out
224 * of power save modes. We restore the (bashed) PCI configuration registers
225 * on a resume.
226 */
227 static void
228 fxp_pci_power(why, arg)
229 int why;
230 void *arg;
231 {
232 struct fxp_pci_softc *psc = arg;
233
234 if (why == PWR_RESUME)
235 fxp_pci_confreg_restore(psc);
236 }
237
238 void
239 fxp_pci_attach(parent, self, aux)
240 struct device *parent, *self;
241 void *aux;
242 {
243 struct fxp_pci_softc *psc = (struct fxp_pci_softc *)self;
244 struct fxp_softc *sc = (struct fxp_softc *)self;
245 struct pci_attach_args *pa = aux;
246 pci_chipset_tag_t pc = pa->pa_pc;
247 pci_intr_handle_t ih;
248 const struct fxp_pci_product *fpp;
249 const char *intrstr = NULL;
250 bus_space_tag_t iot, memt;
251 bus_space_handle_t ioh, memh;
252 int ioh_valid, memh_valid;
253 bus_addr_t addr;
254 bus_size_t size;
255 int flags;
256 int pci_pwrmgmt_cap_reg;
257
258 aprint_naive(": Ethernet controller\n");
259
260 /*
261 * Map control/status registers.
262 */
263 ioh_valid = (pci_mapreg_map(pa, FXP_PCI_IOBA,
264 PCI_MAPREG_TYPE_IO, 0,
265 &iot, &ioh, NULL, NULL) == 0);
266
267 /*
268 * Version 2.1 of the PCI spec, page 196, "Address Maps":
269 *
270 * Prefetchable
271 *
272 * Set to one if there are no side effects on reads, the
273 * device returns all bytes regardless of the byte enables,
274 * and host bridges can merge processor writes into this
275 * range without causing errors. Bit must be set to zero
276 * otherwise.
277 *
278 * The 82557 incorrectly sets the "prefetchable" bit, resulting
279 * in errors on systems which will do merged reads and writes.
280 * These errors manifest themselves as all-bits-set when reading
281 * from the EEPROM or other < 4 byte registers.
282 *
283 * We must work around this problem by always forcing the mapping
284 * for memory space to be uncacheable. On systems which cannot
285 * create an uncacheable mapping (because the firmware mapped it
286 * into only cacheable/prefetchable space due to the "prefetchable"
287 * bit), we can fall back onto i/o mapped access.
288 */
289 memh_valid = 0;
290 memt = pa->pa_memt;
291 if (((pa->pa_flags & PCI_FLAGS_MEM_ENABLED) != 0) &&
292 pci_mapreg_info(pa->pa_pc, pa->pa_tag, FXP_PCI_MMBA,
293 PCI_MAPREG_TYPE_MEM|PCI_MAPREG_MEM_TYPE_32BIT,
294 &addr, &size, &flags) == 0) {
295 flags &= ~BUS_SPACE_MAP_PREFETCHABLE;
296 if (bus_space_map(memt, addr, size, flags, &memh) == 0)
297 memh_valid = 1;
298 }
299
300 if (memh_valid) {
301 sc->sc_st = memt;
302 sc->sc_sh = memh;
303 } else if (ioh_valid) {
304 sc->sc_st = iot;
305 sc->sc_sh = ioh;
306 } else {
307 aprint_error(": unable to map device registers\n");
308 return;
309 }
310
311 sc->sc_dmat = pa->pa_dmat;
312
313 fpp = fxp_pci_lookup(pa);
314 if (fpp == NULL) {
315 printf("\n");
316 panic("fxp_pci_attach: impossible");
317 }
318
319 sc->sc_rev = PCI_REVISION(pa->pa_class);
320
321 switch (fpp->fpp_prodid) {
322 case PCI_PRODUCT_INTEL_82557:
323 case PCI_PRODUCT_INTEL_82559ER:
324 case PCI_PRODUCT_INTEL_IN_BUSINESS:
325 {
326 const char *chipname = NULL;
327
328 if (sc->sc_rev >= FXP_REV_82558_A4) {
329 chipname = "i82558 Ethernet";
330 /*
331 * Enable the MWI command for memory writes.
332 */
333 if (pa->pa_flags & PCI_FLAGS_MWI_OKAY)
334 sc->sc_flags |= FXPF_MWI;
335 }
336 if (sc->sc_rev >= FXP_REV_82559_A0)
337 chipname = "i82559 Ethernet";
338 if (sc->sc_rev >= FXP_REV_82559S_A)
339 chipname = "i82559S Ethernet";
340 if (sc->sc_rev >= FXP_REV_82550)
341 chipname = "i82550 Ethernet";
342
343 /*
344 * Mark all i82559 and i82550 revisions as having
345 * the "resume bug". See i82557.c for details.
346 */
347 if (sc->sc_rev >= FXP_REV_82559_A0)
348 sc->sc_flags |= FXPF_HAS_RESUME_BUG;
349
350 aprint_normal(": %s, rev %d\n", chipname != NULL ? chipname :
351 fpp->fpp_name, sc->sc_rev);
352 break;
353 }
354
355 case PCI_PRODUCT_INTEL_82801BA_LAN:
356 aprint_normal(": %s, rev %d\n", fpp->fpp_name, sc->sc_rev);
357
358 /*
359 * The 82801BA Ethernet has a bug which requires us to send a
360 * NOP before a CU_RESUME if we're in 10baseT mode.
361 */
362 if (fpp->fpp_prodid == PCI_PRODUCT_INTEL_82801BA_LAN)
363 sc->sc_flags |= FXPF_HAS_RESUME_BUG;
364 break;
365
366 case PCI_PRODUCT_INTEL_PRO_100_VE_0:
367 case PCI_PRODUCT_INTEL_PRO_100_VE_1:
368 case PCI_PRODUCT_INTEL_PRO_100_VM_0:
369 case PCI_PRODUCT_INTEL_PRO_100_VM_1:
370 case PCI_PRODUCT_INTEL_82562EH_HPNA_0:
371 case PCI_PRODUCT_INTEL_82562EH_HPNA_1:
372 case PCI_PRODUCT_INTEL_82562EH_HPNA_2:
373 case PCI_PRODUCT_INTEL_PRO_100_VM_2:
374 aprint_normal(": %s, rev %d\n", fpp->fpp_name, sc->sc_rev);
375
376 /*
377 * ICH3 chips apparently have problems with the enhanced
378 * features, so just treat them as an i82557. It also
379 * has the resume bug that the ICH2 has.
380 */
381 sc->sc_rev = 1;
382 sc->sc_flags |= FXPF_HAS_RESUME_BUG;
383 break;
384 case PCI_PRODUCT_INTEL_82801E_LAN_1:
385 case PCI_PRODUCT_INTEL_82801E_LAN_2:
386 aprint_normal(": %s, rev %d\n", fpp->fpp_name, sc->sc_rev);
387
388 /*
389 * XXX We have to read the C-ICH's developer's manual
390 * in detail
391 */
392 break;
393 }
394
395 /* Make sure bus-mastering is enabled. */
396 pci_conf_write(pc, pa->pa_tag, PCI_COMMAND_STATUS_REG,
397 pci_conf_read(pc, pa->pa_tag, PCI_COMMAND_STATUS_REG) |
398 PCI_COMMAND_MASTER_ENABLE);
399
400 /*
401 * Under some circumstances (such as APM suspend/resume
402 * cycles, and across ACPI power state changes), the
403 * i82257-family can lose the contents of critical PCI
404 * configuration registers, causing the card to be
405 * non-responsive and useless. This occurs on the Sony VAIO
406 * Z505-series, among others. Preserve them here so they can
407 * be later restored (by fxp_pci_confreg_restore()).
408 */
409 psc->psc_pc = pc;
410 psc->psc_tag = pa->pa_tag;
411 psc->psc_regs[PCI_COMMAND_STATUS_REG>>2] =
412 pci_conf_read(pc, pa->pa_tag, PCI_COMMAND_STATUS_REG);
413 psc->psc_regs[PCI_BHLC_REG>>2] =
414 pci_conf_read(pc, pa->pa_tag, PCI_BHLC_REG);
415 psc->psc_regs[(PCI_MAPREG_START+0x0)>>2] =
416 pci_conf_read(pc, pa->pa_tag, PCI_MAPREG_START+0x0);
417 psc->psc_regs[(PCI_MAPREG_START+0x4)>>2] =
418 pci_conf_read(pc, pa->pa_tag, PCI_MAPREG_START+0x4);
419 psc->psc_regs[(PCI_MAPREG_START+0x8)>>2] =
420 pci_conf_read(pc, pa->pa_tag, PCI_MAPREG_START+0x8);
421
422 /*
423 * Work around BIOS ACPI bugs where the chip is inadvertantly
424 * left in ACPI D3 (lowest power state). First confirm the device
425 * supports ACPI power management, then move it to the D0 (fully
426 * functional) state if it is not already there.
427 */
428 if (pci_get_capability(pc, pa->pa_tag, PCI_CAP_PWRMGMT,
429 &pci_pwrmgmt_cap_reg, 0)) {
430 pcireg_t reg;
431
432 sc->sc_enable = fxp_pci_enable;
433 sc->sc_disable = fxp_pci_disable;
434
435 psc->psc_pwrmgmt_csr_reg = pci_pwrmgmt_cap_reg + PCI_PMCSR;
436 reg = pci_conf_read(pc, pa->pa_tag, psc->psc_pwrmgmt_csr_reg);
437 psc->psc_pwrmgmt_csr = (reg & ~PCI_PMCSR_STATE_MASK) |
438 PCI_PMCSR_STATE_D0;
439 if ((reg & PCI_PMCSR_STATE_MASK) != PCI_PMCSR_STATE_D0)
440 pci_conf_write(pc, pa->pa_tag, psc->psc_pwrmgmt_csr_reg,
441 psc->psc_pwrmgmt_csr);
442 }
443 /* Restore PCI configuration registers. */
444 fxp_pci_confreg_restore(psc);
445
446 sc->sc_enabled = 1;
447
448 /*
449 * Map and establish our interrupt.
450 */
451 if (pci_intr_map(pa, &ih)) {
452 aprint_error("%s: couldn't map interrupt\n",
453 sc->sc_dev.dv_xname);
454 return;
455 }
456 intrstr = pci_intr_string(pc, ih);
457 sc->sc_ih = pci_intr_establish(pc, ih, IPL_NET, fxp_intr, sc);
458 if (sc->sc_ih == NULL) {
459 aprint_error("%s: couldn't establish interrupt",
460 sc->sc_dev.dv_xname);
461 if (intrstr != NULL)
462 aprint_normal(" at %s", intrstr);
463 aprint_normal("\n");
464 return;
465 }
466 aprint_normal("%s: interrupting at %s\n", sc->sc_dev.dv_xname, intrstr);
467
468 /* Finish off the attach. */
469 fxp_attach(sc);
470 if (sc->sc_disable != NULL)
471 fxp_disable(sc);
472
473 /* Add a suspend hook to restore PCI config state */
474 psc->psc_powerhook = powerhook_establish(fxp_pci_power, psc);
475 if (psc->psc_powerhook == NULL)
476 aprint_error(
477 "%s: WARNING: unable to establish pci power hook\n",
478 sc->sc_dev.dv_xname);
479 }
480
481 int
482 fxp_pci_enable(struct fxp_softc *sc)
483 {
484 struct fxp_pci_softc *psc = (void *) sc;
485
486 #if 0
487 printf("%s: going to power state D0\n", sc->sc_dev.dv_xname);
488 #endif
489
490 /* Bring the device into D0 power state. */
491 pci_conf_write(psc->psc_pc, psc->psc_tag,
492 psc->psc_pwrmgmt_csr_reg, psc->psc_pwrmgmt_csr);
493
494 /* Now restore the configuration registers. */
495 fxp_pci_confreg_restore(psc);
496
497 return (0);
498 }
499
500 void
501 fxp_pci_disable(struct fxp_softc *sc)
502 {
503 struct fxp_pci_softc *psc = (void *) sc;
504
505 /*
506 * for some 82558_A4 and 82558_B0, entering D3 state makes
507 * media detection disordered.
508 */
509 if (sc->sc_rev <= FXP_REV_82558_B0)
510 return;
511
512 #if 0
513 printf("%s: going to power state D3\n", sc->sc_dev.dv_xname);
514 #endif
515
516 /* Put the device into D3 state. */
517 pci_conf_write(psc->psc_pc, psc->psc_tag,
518 psc->psc_pwrmgmt_csr_reg, (psc->psc_pwrmgmt_csr &
519 ~PCI_PMCSR_STATE_MASK) | PCI_PMCSR_STATE_D3);
520 }
521