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