if_tlp_cardbus.c revision 1.25 1 /* $NetBSD: if_tlp_cardbus.c,v 1.25 2001/01/16 18:55:00 thorpej Exp $ */
2
3 /*-
4 * Copyright (c) 1999, 2000 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 * CardBus bus front-end for the Digital Semiconductor ``Tulip'' (21x4x)
42 * Ethernet controller family driver.
43 */
44
45 #include "opt_inet.h"
46 #include "opt_ns.h"
47 #include "bpfilter.h"
48
49 #include <sys/param.h>
50 #include <sys/systm.h>
51 #include <sys/mbuf.h>
52 #include <sys/malloc.h>
53 #include <sys/kernel.h>
54 #include <sys/socket.h>
55 #include <sys/ioctl.h>
56 #include <sys/errno.h>
57 #include <sys/device.h>
58
59 #include <machine/endian.h>
60
61 #include <net/if.h>
62 #include <net/if_dl.h>
63 #include <net/if_media.h>
64 #include <net/if_ether.h>
65
66 #if NBPFILTER > 0
67 #include <net/bpf.h>
68 #endif
69
70 #ifdef INET
71 #include <netinet/in.h>
72 #include <netinet/if_inarp.h>
73 #endif
74
75 #ifdef NS
76 #include <netns/ns.h>
77 #include <netns/ns_if.h>
78 #endif
79
80 #include <machine/bus.h>
81 #include <machine/intr.h>
82
83 #include <dev/mii/miivar.h>
84 #include <dev/mii/mii_bitbang.h>
85
86 #include <dev/ic/tulipreg.h>
87 #include <dev/ic/tulipvar.h>
88
89 #include <dev/pci/pcivar.h>
90 #include <dev/pci/pcireg.h>
91 #include <dev/pci/pcidevs.h>
92
93 #include <dev/cardbus/cardbusvar.h>
94 #include <dev/cardbus/cardbusdevs.h>
95
96 /*
97 * PCI configuration space registers used by the Tulip.
98 */
99 #define TULIP_PCI_IOBA 0x10 /* i/o mapped base */
100 #define TULIP_PCI_MMBA 0x14 /* memory mapped base */
101 #define TULIP_PCI_CFDA 0x40 /* configuration driver area */
102
103 #define CFDA_SLEEP 0x80000000 /* sleep mode */
104 #define CFDA_SNOOZE 0x40000000 /* snooze mode */
105
106 struct tulip_cardbus_softc {
107 struct tulip_softc sc_tulip; /* real Tulip softc */
108
109 /* CardBus-specific goo. */
110 void *sc_ih; /* interrupt handle */
111 cardbus_devfunc_t sc_ct; /* our CardBus devfuncs */
112 cardbustag_t sc_tag; /* our CardBus tag */
113 int sc_csr; /* CSR bits */
114 bus_size_t sc_mapsize; /* the size of mapped bus space
115 region */
116
117 int sc_cben; /* CardBus enables */
118 int sc_bar_reg; /* which BAR to use */
119 pcireg_t sc_bar_val; /* value of the BAR */
120
121 int sc_intrline; /* interrupt line */
122 };
123
124 int tlp_cardbus_match __P((struct device *, struct cfdata *, void *));
125 void tlp_cardbus_attach __P((struct device *, struct device *, void *));
126 int tlp_cardbus_detach __P((struct device *, int));
127
128 struct cfattach tlp_cardbus_ca = {
129 sizeof(struct tulip_cardbus_softc),
130 tlp_cardbus_match, tlp_cardbus_attach,
131 tlp_cardbus_detach, tlp_activate,
132 };
133
134 const struct tulip_cardbus_product {
135 u_int32_t tcp_vendor; /* PCI vendor ID */
136 u_int32_t tcp_product; /* PCI product ID */
137 tulip_chip_t tcp_chip; /* base Tulip chip type */
138 } tlp_cardbus_products[] = {
139 { PCI_VENDOR_DEC, PCI_PRODUCT_DEC_21142,
140 TULIP_CHIP_21142 },
141
142 { PCI_VENDOR_XIRCOM, PCI_PRODUCT_XIRCOM_X3201_3_21143,
143 TULIP_CHIP_X3201_3 },
144
145 { CARDBUS_VENDOR_ACCTON, CARDBUS_PRODUCT_ACCTON_EN2242,
146 TULIP_CHIP_AN985 },
147
148 { CARDBUS_VENDOR_ABOCOM, CARDBUS_PRODUCT_ABOCOM_FE2500,
149 TULIP_CHIP_AN985 },
150
151 { 0, 0,
152 TULIP_CHIP_INVALID },
153 };
154
155 void tlp_cardbus_setup __P((struct tulip_cardbus_softc *));
156
157 int tlp_cardbus_enable __P((struct tulip_softc *));
158 void tlp_cardbus_disable __P((struct tulip_softc *));
159 void tlp_cardbus_power __P((struct tulip_softc *, int));
160
161 void tlp_cardbus_x3201_reset __P((struct tulip_softc *));
162
163 const struct tulip_cardbus_product *tlp_cardbus_lookup
164 __P((const struct cardbus_attach_args *));
165
166 const struct tulip_cardbus_product *
167 tlp_cardbus_lookup(ca)
168 const struct cardbus_attach_args *ca;
169 {
170 const struct tulip_cardbus_product *tcp;
171
172 for (tcp = tlp_cardbus_products;
173 tlp_chip_names[tcp->tcp_chip] != NULL;
174 tcp++) {
175 if (PCI_VENDOR(ca->ca_id) == tcp->tcp_vendor &&
176 PCI_PRODUCT(ca->ca_id) == tcp->tcp_product)
177 return (tcp);
178 }
179 return (NULL);
180 }
181
182 int
183 tlp_cardbus_match(parent, match, aux)
184 struct device *parent;
185 struct cfdata *match;
186 void *aux;
187 {
188 struct cardbus_attach_args *ca = aux;
189
190 if (tlp_cardbus_lookup(ca) != NULL)
191 return (1);
192
193 return (0);
194 }
195
196 void
197 tlp_cardbus_attach(parent, self, aux)
198 struct device *parent, *self;
199 void *aux;
200 {
201 struct tulip_cardbus_softc *csc = (void *)self;
202 struct tulip_softc *sc = &csc->sc_tulip;
203 struct cardbus_attach_args *ca = aux;
204 cardbus_devfunc_t ct = ca->ca_ct;
205 const struct tulip_cardbus_product *tcp;
206 u_int8_t enaddr[ETHER_ADDR_LEN];
207 bus_addr_t adr;
208 pcireg_t reg;
209
210 sc->sc_devno = ca->ca_device;
211 sc->sc_dmat = ca->ca_dmat;
212 csc->sc_ct = ct;
213 csc->sc_tag = ca->ca_tag;
214
215 tcp = tlp_cardbus_lookup(ca);
216 if (tcp == NULL) {
217 printf("\n");
218 panic("tlp_cardbus_attach: impossible");
219 }
220 sc->sc_chip = tcp->tcp_chip;
221
222 /*
223 * By default, Tulip registers are 8 bytes long (4 bytes
224 * followed by a 4 byte pad).
225 */
226 sc->sc_regshift = 3;
227
228 /*
229 * Power management hooks.
230 */
231 sc->sc_enable = tlp_cardbus_enable;
232 sc->sc_disable = tlp_cardbus_disable;
233 sc->sc_power = tlp_cardbus_power;
234
235 /*
236 * Get revision info, and set some chip-specific variables.
237 */
238 sc->sc_rev = PCI_REVISION(ca->ca_class);
239 switch (sc->sc_chip) {
240 case TULIP_CHIP_21142:
241 if (sc->sc_rev >= 0x20)
242 sc->sc_chip = TULIP_CHIP_21143;
243 break;
244
245 case TULIP_CHIP_AN985:
246 /*
247 * The AN983 and AN985 are very similar, and are
248 * differentiated by a "signature" register that
249 * is like, but not identical, to a PCI ID register.
250 */
251 reg = cardbus_conf_read(ct->ct_cc, ct->ct_cf, csc->sc_tag,
252 0x80);
253 switch (reg) {
254 case 0x09811317:
255 sc->sc_chip = TULIP_CHIP_AN985;
256 break;
257
258 case 0x09851317:
259 sc->sc_chip = TULIP_CHIP_AN983;
260 break;
261
262 default:
263 /* Unknown -- use default. */
264 }
265 break;
266
267 default:
268 /* Nothing. */
269 }
270
271 printf(": %s Ethernet, pass %d.%d\n",
272 tlp_chip_names[sc->sc_chip],
273 (sc->sc_rev >> 4) & 0xf, sc->sc_rev & 0xf);
274
275 /*
276 * Map the device.
277 */
278 csc->sc_csr = PCI_COMMAND_MASTER_ENABLE;
279 if (Cardbus_mapreg_map(ct, TULIP_PCI_IOBA,
280 PCI_MAPREG_TYPE_IO, 0, &sc->sc_st, &sc->sc_sh, &adr,
281 &csc->sc_mapsize) == 0) {
282 #if rbus
283 #else
284 (*ct->ct_cf->cardbus_io_open)(cc, 0, adr, adr+csc->sc_mapsize);
285 #endif
286 csc->sc_cben = CARDBUS_IO_ENABLE;
287 csc->sc_csr |= PCI_COMMAND_IO_ENABLE;
288 csc->sc_bar_reg = TULIP_PCI_IOBA;
289 csc->sc_bar_val = adr | PCI_MAPREG_TYPE_IO;
290 } else if (Cardbus_mapreg_map(ct, TULIP_PCI_MMBA,
291 PCI_MAPREG_TYPE_MEM|PCI_MAPREG_MEM_TYPE_32BIT, 0,
292 &sc->sc_st, &sc->sc_sh, &adr, &csc->sc_mapsize) == 0) {
293 #if rbus
294 #else
295 (*ct->ct_cf->cardbus_mem_open)(cc, 0, adr, adr+csc->sc_mapsize);
296 #endif
297 csc->sc_cben = CARDBUS_MEM_ENABLE;
298 csc->sc_csr |= PCI_COMMAND_MEM_ENABLE;
299 csc->sc_bar_reg = TULIP_PCI_MMBA;
300 csc->sc_bar_val = adr | PCI_MAPREG_TYPE_MEM;
301 } else {
302 printf("%s: unable to map device registers\n",
303 sc->sc_dev.dv_xname);
304 return;
305 }
306
307 /*
308 * Bring the chip out of powersave mode and initialize the
309 * configuration registers.
310 */
311 tlp_cardbus_setup(csc);
312
313 /*
314 * Read the contents of the Ethernet Address ROM/SROM.
315 */
316 switch (sc->sc_chip) {
317 case TULIP_CHIP_X3201_3:
318 /*
319 * No SROM on this chip.
320 */
321 break;
322
323 default:
324 if (tlp_read_srom(sc) == 0)
325 goto cant_cope;
326 break;
327 }
328
329 /*
330 * Deal with chip/board quirks. This includes setting up
331 * the mediasw, and extracting the Ethernet address from
332 * the rombuf.
333 */
334 switch (sc->sc_chip) {
335 case TULIP_CHIP_21142:
336 case TULIP_CHIP_21143:
337 /* Check for new format SROM. */
338 if (tlp_isv_srom_enaddr(sc, enaddr) != 0) {
339 /*
340 * We start out with the 2114x ISV media switch.
341 * When we search for quirks, we may change to
342 * a different switch.
343 */
344 sc->sc_mediasw = &tlp_2114x_isv_mediasw;
345 } else if (tlp_parse_old_srom(sc, enaddr) == 0) {
346 /*
347 * Not an ISV SROM, and not in old DEC Address
348 * ROM format. Try to snarf it out of the CIS.
349 */
350 if (ca->ca_cis.funce.network.netid_present == 0)
351 goto cant_cope;
352
353 /* Grab the MAC address from the CIS. */
354 memcpy(enaddr, ca->ca_cis.funce.network.netid,
355 sizeof(enaddr));
356 }
357
358 /* XXX XXX XXX QUIRKS XXX XXX XXX */
359
360 /*
361 * If we don't already have a media switch, default to
362 * MII-over-SIO, with no special reset routine.
363 */
364 if (sc->sc_mediasw == NULL) {
365 printf("%s: defaulting to MII-over-SIO; no bets...\n",
366 sc->sc_dev.dv_xname);
367 sc->sc_mediasw = &tlp_sio_mii_mediasw;
368 }
369 break;
370
371 case TULIP_CHIP_AN983:
372 case TULIP_CHIP_AN985:
373 /*
374 * The ADMtek AN985's Ethernet address is located
375 * at offset 8 of its EEPROM.
376 */
377 memcpy(enaddr, &sc->sc_srom[8], ETHER_ADDR_LEN);
378
379 /*
380 * The ADMtek AN985 can be configured in Single-Chip
381 * mode or MAC-only mode. Single-Chip uses the built-in
382 * PHY, MAC-only has an external PHY (usually HomePNA).
383 * The selection is based on an EEPROM setting, and both
384 * PHYs are access via MII attached to SIO.
385 *
386 * The AN985 "ghosts" the internal PHY onto all
387 * MII addresses, so we have to use a media init
388 * routine that limits the search.
389 * XXX How does this work with MAC-only mode?
390 */
391 sc->sc_mediasw = &tlp_an985_mediasw;
392 break;
393
394 case TULIP_CHIP_X3201_3:
395 /*
396 * The X3201 doesn't have an SROM. Lift the MAC address
397 * from the CIS. Also, we have a special media switch:
398 * MII-on-SIO, plus some special GPIO setup.
399 */
400 memcpy(enaddr, ca->ca_cis.funce.network.netid, sizeof(enaddr));
401 sc->sc_reset = tlp_cardbus_x3201_reset;
402 sc->sc_mediasw = &tlp_sio_mii_mediasw;
403 break;
404
405 default:
406 cant_cope:
407 printf("%s: sorry, unable to handle your board\n",
408 sc->sc_dev.dv_xname);
409 return;
410 }
411
412 /* Remember which interrupt line. */
413 csc->sc_intrline = ca->ca_intrline;
414
415 /*
416 * The CardBus cards will make it to store-and-forward mode as
417 * soon as you put them under any kind of load, so just start
418 * out there.
419 */
420 sc->sc_txthresh = TXTH_SF;
421
422 /*
423 * Finish off the attach.
424 */
425 tlp_attach(sc, enaddr);
426
427 /*
428 * Power down the socket.
429 */
430 Cardbus_function_disable(csc->sc_ct);
431 }
432
433 int
434 tlp_cardbus_detach(self, flags)
435 struct device *self;
436 int flags;
437 {
438 struct tulip_cardbus_softc *csc = (void *)self;
439 struct tulip_softc *sc = &csc->sc_tulip;
440 struct cardbus_devfunc *ct = csc->sc_ct;
441 int rv;
442
443 #if defined(DIAGNOSTIC)
444 if (ct == NULL)
445 panic("%s: data structure lacks\n", sc->sc_dev.dv_xname);
446 #endif
447
448 rv = tlp_detach(sc);
449 if (rv)
450 return (rv);
451
452 /*
453 * Unhook the interrupt handler.
454 */
455 if (csc->sc_ih != NULL)
456 cardbus_intr_disestablish(ct->ct_cc, ct->ct_cf, csc->sc_ih);
457
458 /*
459 * Release bus space and close window.
460 */
461 if (csc->sc_bar_reg != 0)
462 Cardbus_mapreg_unmap(ct, csc->sc_bar_reg,
463 sc->sc_st, sc->sc_sh, csc->sc_mapsize);
464
465 return (0);
466 }
467
468 int
469 tlp_cardbus_enable(sc)
470 struct tulip_softc *sc;
471 {
472 struct tulip_cardbus_softc *csc = (void *) sc;
473 cardbus_devfunc_t ct = csc->sc_ct;
474 cardbus_chipset_tag_t cc = ct->ct_cc;
475 cardbus_function_tag_t cf = ct->ct_cf;
476
477 /*
478 * Power on the socket.
479 */
480 Cardbus_function_enable(ct);
481
482 /*
483 * Set up the PCI configuration registers.
484 */
485 tlp_cardbus_setup(csc);
486
487 /*
488 * Map and establish the interrupt.
489 */
490 csc->sc_ih = cardbus_intr_establish(cc, cf, csc->sc_intrline, IPL_NET,
491 tlp_intr, sc);
492 if (csc->sc_ih == NULL) {
493 printf("%s: unable to establish interrupt at %d\n",
494 sc->sc_dev.dv_xname, csc->sc_intrline);
495 Cardbus_function_disable(csc->sc_ct);
496 return (1);
497 }
498 printf("%s: interrupting at %d\n", sc->sc_dev.dv_xname,
499 csc->sc_intrline);
500
501 return (0);
502 }
503
504 void
505 tlp_cardbus_disable(sc)
506 struct tulip_softc *sc;
507 {
508 struct tulip_cardbus_softc *csc = (void *) sc;
509 cardbus_devfunc_t ct = csc->sc_ct;
510 cardbus_chipset_tag_t cc = ct->ct_cc;
511 cardbus_function_tag_t cf = ct->ct_cf;
512
513 /* Unhook the interrupt handler. */
514 cardbus_intr_disestablish(cc, cf, csc->sc_ih);
515 csc->sc_ih = NULL;
516
517 /* Power down the socket. */
518 Cardbus_function_disable(ct);
519 }
520
521 void
522 tlp_cardbus_power(sc, why)
523 struct tulip_softc *sc;
524 int why;
525 {
526 struct tulip_cardbus_softc *csc = (void *) sc;
527
528 if (why == PWR_RESUME) {
529 /*
530 * Give the PCI configuration registers a kick
531 * in the head.
532 */
533 #ifdef DIAGNOSTIC
534 if (TULIP_IS_ENABLED(sc) == 0)
535 panic("tlp_cardbus_power");
536 #endif
537 tlp_cardbus_setup(csc);
538 }
539 }
540
541 void
542 tlp_cardbus_setup(csc)
543 struct tulip_cardbus_softc *csc;
544 {
545 struct tulip_softc *sc = &csc->sc_tulip;
546 cardbus_devfunc_t ct = csc->sc_ct;
547 cardbus_chipset_tag_t cc = ct->ct_cc;
548 cardbus_function_tag_t cf = ct->ct_cf;
549 pcireg_t reg;
550 int pmreg;
551
552 /*
553 * Check to see if the device is in power-save mode, and
554 * bring it out if necessary.
555 */
556 switch (sc->sc_chip) {
557 case TULIP_CHIP_21142:
558 case TULIP_CHIP_21143:
559 case TULIP_CHIP_X3201_3:
560 /*
561 * Clear the "sleep mode" bit in the CFDA register.
562 */
563 reg = cardbus_conf_read(cc, cf, csc->sc_tag, TULIP_PCI_CFDA);
564 if (reg & (CFDA_SLEEP|CFDA_SNOOZE))
565 cardbus_conf_write(cc, cf, csc->sc_tag, TULIP_PCI_CFDA,
566 reg & ~(CFDA_SLEEP|CFDA_SNOOZE));
567 break;
568
569 default:
570 /* Nothing. */
571 }
572
573 if (cardbus_get_capability(cc, cf, csc->sc_tag,
574 PCI_CAP_PWRMGMT, &pmreg, 0)) {
575 reg = cardbus_conf_read(cc, cf, csc->sc_tag, pmreg + 4) & 0x03;
576 #if 1 /* XXX Probably not right for CardBus. */
577 if (reg == 3) {
578 /*
579 * The card has lost all configuration data in
580 * this state, so punt.
581 */
582 printf("%s: unable to wake up from power state D3\n",
583 sc->sc_dev.dv_xname);
584 return;
585 }
586 #endif
587 if (reg != 0) {
588 printf("%s: waking up from power state D%d\n",
589 sc->sc_dev.dv_xname, reg);
590 cardbus_conf_write(cc, cf, csc->sc_tag,
591 pmreg + 4, 0);
592 }
593 }
594
595 /* Make sure the right access type is on the CardBus bridge. */
596 (*ct->ct_cf->cardbus_ctrl)(cc, csc->sc_cben);
597 (*ct->ct_cf->cardbus_ctrl)(cc, CARDBUS_BM_ENABLE);
598
599 /* Program the BAR. */
600 cardbus_conf_write(cc, cf, csc->sc_tag, csc->sc_bar_reg,
601 csc->sc_bar_val);
602
603 /* Enable the appropriate bits in the PCI CSR. */
604 reg = cardbus_conf_read(cc, cf, csc->sc_tag, PCI_COMMAND_STATUS_REG);
605 reg &= ~(PCI_COMMAND_IO_ENABLE|PCI_COMMAND_MEM_ENABLE);
606 reg |= csc->sc_csr;
607 cardbus_conf_write(cc, cf, csc->sc_tag, PCI_COMMAND_STATUS_REG, reg);
608
609 /*
610 * Make sure the latency timer is set to some reasonable
611 * value.
612 */
613 reg = cardbus_conf_read(cc, cf, csc->sc_tag, PCI_BHLC_REG);
614 if (PCI_LATTIMER(reg) < 0x20) {
615 reg &= ~(PCI_LATTIMER_MASK << PCI_LATTIMER_SHIFT);
616 reg |= (0x20 << PCI_LATTIMER_SHIFT);
617 cardbus_conf_write(cc, cf, csc->sc_tag, PCI_BHLC_REG, reg);
618 }
619 }
620
621 void
622 tlp_cardbus_x3201_reset(sc)
623 struct tulip_softc *sc;
624 {
625 u_int32_t reg;
626
627 reg = TULIP_READ(sc, CSR_SIAGEN);
628
629 /* make GP[2,0] outputs */
630 TULIP_WRITE(sc, CSR_SIAGEN, (reg & ~SIAGEN_MD) | SIAGEN_CWE |
631 0x00050000);
632 TULIP_WRITE(sc, CSR_SIAGEN, (reg & ~SIAGEN_CWE) | SIAGEN_MD);
633 }
634