if_tlp_cardbus.c revision 1.23 1 /* $NetBSD: if_tlp_cardbus.c,v 1.23 2000/04/04 19:22:50 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
95 /*
96 * PCI configuration space registers used by the Tulip.
97 */
98 #define TULIP_PCI_IOBA 0x10 /* i/o mapped base */
99 #define TULIP_PCI_MMBA 0x14 /* memory mapped base */
100 #define TULIP_PCI_CFDA 0x40 /* configuration driver area */
101
102 #define CFDA_SLEEP 0x80000000 /* sleep mode */
103 #define CFDA_SNOOZE 0x40000000 /* snooze mode */
104
105 struct tulip_cardbus_softc {
106 struct tulip_softc sc_tulip; /* real Tulip softc */
107
108 /* CardBus-specific goo. */
109 void *sc_ih; /* interrupt handle */
110 cardbus_devfunc_t sc_ct; /* our CardBus devfuncs */
111 cardbustag_t sc_tag; /* our CardBus tag */
112 int sc_csr; /* CSR bits */
113 bus_size_t sc_mapsize; /* the size of mapped bus space
114 region */
115
116 int sc_cben; /* CardBus enables */
117 int sc_bar_reg; /* which BAR to use */
118 pcireg_t sc_bar_val; /* value of the BAR */
119
120 int sc_intrline; /* interrupt line */
121 };
122
123 int tlp_cardbus_match __P((struct device *, struct cfdata *, void *));
124 void tlp_cardbus_attach __P((struct device *, struct device *, void *));
125 int tlp_cardbus_detach __P((struct device *, int));
126
127 struct cfattach tlp_cardbus_ca = {
128 sizeof(struct tulip_cardbus_softc),
129 tlp_cardbus_match, tlp_cardbus_attach,
130 tlp_cardbus_detach, tlp_activate,
131 };
132
133 const struct tulip_cardbus_product {
134 u_int32_t tcp_vendor; /* PCI vendor ID */
135 u_int32_t tcp_product; /* PCI product ID */
136 tulip_chip_t tcp_chip; /* base Tulip chip type */
137 } tlp_cardbus_products[] = {
138 { PCI_VENDOR_DEC, PCI_PRODUCT_DEC_21142,
139 TULIP_CHIP_21142 },
140
141 { PCI_VENDOR_XIRCOM, PCI_PRODUCT_XIRCOM_X3201_3_21143,
142 TULIP_CHIP_X3201_3 },
143
144 { 0, 0,
145 TULIP_CHIP_INVALID },
146 };
147
148 void tlp_cardbus_setup __P((struct tulip_cardbus_softc *));
149
150 int tlp_cardbus_enable __P((struct tulip_softc *));
151 void tlp_cardbus_disable __P((struct tulip_softc *));
152 void tlp_cardbus_power __P((struct tulip_softc *, int));
153
154 void tlp_cardbus_x3201_reset __P((struct tulip_softc *));
155
156 const struct tulip_cardbus_product *tlp_cardbus_lookup
157 __P((const struct cardbus_attach_args *));
158
159 const struct tulip_cardbus_product *
160 tlp_cardbus_lookup(ca)
161 const struct cardbus_attach_args *ca;
162 {
163 const struct tulip_cardbus_product *tcp;
164
165 for (tcp = tlp_cardbus_products;
166 tlp_chip_names[tcp->tcp_chip] != NULL;
167 tcp++) {
168 if (PCI_VENDOR(ca->ca_id) == tcp->tcp_vendor &&
169 PCI_PRODUCT(ca->ca_id) == tcp->tcp_product)
170 return (tcp);
171 }
172 return (NULL);
173 }
174
175 int
176 tlp_cardbus_match(parent, match, aux)
177 struct device *parent;
178 struct cfdata *match;
179 void *aux;
180 {
181 struct cardbus_attach_args *ca = aux;
182
183 if (tlp_cardbus_lookup(ca) != NULL)
184 return (1);
185
186 return (0);
187 }
188
189 void
190 tlp_cardbus_attach(parent, self, aux)
191 struct device *parent, *self;
192 void *aux;
193 {
194 struct tulip_cardbus_softc *csc = (void *)self;
195 struct tulip_softc *sc = &csc->sc_tulip;
196 struct cardbus_attach_args *ca = aux;
197 cardbus_devfunc_t ct = ca->ca_ct;
198 const struct tulip_cardbus_product *tcp;
199 u_int8_t enaddr[ETHER_ADDR_LEN];
200 bus_addr_t adr;
201
202 sc->sc_devno = ca->ca_device;
203 sc->sc_dmat = ca->ca_dmat;
204 csc->sc_ct = ct;
205 csc->sc_tag = ca->ca_tag;
206
207 tcp = tlp_cardbus_lookup(ca);
208 if (tcp == NULL) {
209 printf("\n");
210 panic("tlp_cardbus_attach: impossible");
211 }
212 sc->sc_chip = tcp->tcp_chip;
213
214 /*
215 * By default, Tulip registers are 8 bytes long (4 bytes
216 * followed by a 4 byte pad).
217 */
218 sc->sc_regshift = 3;
219
220 /*
221 * Power management hooks.
222 */
223 sc->sc_enable = tlp_cardbus_enable;
224 sc->sc_disable = tlp_cardbus_disable;
225 sc->sc_power = tlp_cardbus_power;
226
227 /*
228 * Get revision info, and set some chip-specific variables.
229 */
230 sc->sc_rev = PCI_REVISION(ca->ca_class);
231 switch (sc->sc_chip) {
232 case TULIP_CHIP_21142:
233 if (sc->sc_rev >= 0x20)
234 sc->sc_chip = TULIP_CHIP_21143;
235 break;
236
237 default:
238 /* Nothing. */
239 }
240
241 printf(": %s Ethernet, pass %d.%d\n",
242 tlp_chip_names[sc->sc_chip],
243 (sc->sc_rev >> 4) & 0xf, sc->sc_rev & 0xf);
244
245 /*
246 * Map the device.
247 */
248 csc->sc_csr = PCI_COMMAND_MASTER_ENABLE;
249 if (Cardbus_mapreg_map(ct, TULIP_PCI_IOBA,
250 PCI_MAPREG_TYPE_IO, 0, &sc->sc_st, &sc->sc_sh, &adr,
251 &csc->sc_mapsize) == 0) {
252 #if rbus
253 #else
254 (*ct->ct_cf->cardbus_io_open)(cc, 0, adr, adr+csc->sc_mapsize);
255 #endif
256 csc->sc_cben = CARDBUS_IO_ENABLE;
257 csc->sc_csr |= PCI_COMMAND_IO_ENABLE;
258 csc->sc_bar_reg = TULIP_PCI_IOBA;
259 csc->sc_bar_val = adr | PCI_MAPREG_TYPE_IO;
260 } else if (Cardbus_mapreg_map(ct, TULIP_PCI_MMBA,
261 PCI_MAPREG_TYPE_MEM|PCI_MAPREG_MEM_TYPE_32BIT, 0,
262 &sc->sc_st, &sc->sc_sh, &adr, &csc->sc_mapsize) == 0) {
263 #if rbus
264 #else
265 (*ct->ct_cf->cardbus_mem_open)(cc, 0, adr, adr+csc->sc_mapsize);
266 #endif
267 csc->sc_cben = CARDBUS_MEM_ENABLE;
268 csc->sc_csr |= PCI_COMMAND_MEM_ENABLE;
269 csc->sc_bar_reg = TULIP_PCI_MMBA;
270 csc->sc_bar_val = adr | PCI_MAPREG_TYPE_MEM;
271 } else {
272 printf("%s: unable to map device registers\n",
273 sc->sc_dev.dv_xname);
274 return;
275 }
276
277 /*
278 * Bring the chip out of powersave mode and initialize the
279 * configuration registers.
280 */
281 tlp_cardbus_setup(csc);
282
283 /*
284 * Read the contents of the Ethernet Address ROM/SROM.
285 */
286 switch (sc->sc_chip) {
287 case TULIP_CHIP_X3201_3:
288 /*
289 * No SROM on this chip.
290 */
291 break;
292
293 default:
294 if (tlp_read_srom(sc) == 0)
295 goto cant_cope;
296 break;
297 }
298
299 /*
300 * Deal with chip/board quirks. This includes setting up
301 * the mediasw, and extracting the Ethernet address from
302 * the rombuf.
303 */
304 switch (sc->sc_chip) {
305 case TULIP_CHIP_21142:
306 case TULIP_CHIP_21143:
307 /* Check for new format SROM. */
308 if (tlp_isv_srom_enaddr(sc, enaddr) == 0) {
309 /*
310 * Not an ISV SROM; try the old DEC Ethernet Address
311 * ROM format.
312 */
313 if (tlp_parse_old_srom(sc, enaddr) == 0)
314 goto cant_cope;
315 } else {
316 /*
317 * We start out with the 2114x ISV media switch.
318 * When we search for quirks, we may change to
319 * a different switch.
320 */
321 sc->sc_mediasw = &tlp_2114x_isv_mediasw;
322 }
323
324 if (sc->sc_mediasw == NULL) {
325 /*
326 * If there's no MAC address in the CIS, bail out
327 * now.
328 */
329 if (ca->ca_cis.funce.network.netid_present == 0)
330 goto cant_cope;
331
332 /* Grab the MAC address from the CIS. */
333 memcpy(enaddr, ca->ca_cis.funce.network.netid,
334 sizeof(enaddr));
335
336 /*
337 * XXX Assume MII-on-SIO. Should probably use
338 * XXX CIS strings to determine the GPIO reset
339 * XXX information.
340 */
341 sc->sc_mediasw = &tlp_sio_mii_mediasw;
342 }
343 break;
344
345 case TULIP_CHIP_X3201_3:
346 /*
347 * The X3201 doesn't have an SROM. Lift the MAC address
348 * from the CIS. Also, we have a special media switch:
349 * MII-on-SIO, plus some special GPIO setup.
350 */
351 memcpy(enaddr, ca->ca_cis.funce.network.netid, sizeof(enaddr));
352 sc->sc_reset = tlp_cardbus_x3201_reset;
353 sc->sc_mediasw = &tlp_sio_mii_mediasw;
354 break;
355
356 default:
357 cant_cope:
358 printf("%s: sorry, unable to handle your board\n",
359 sc->sc_dev.dv_xname);
360 return;
361 }
362
363 /* Remember which interrupt line. */
364 csc->sc_intrline = ca->ca_intrline;
365
366 /*
367 * The CardBus cards will make it to store-and-forward mode as
368 * soon as you put them under any kind of load, so just start
369 * out there.
370 */
371 sc->sc_txthresh = TXTH_SF;
372
373 /*
374 * Finish off the attach.
375 */
376 tlp_attach(sc, enaddr);
377
378 /*
379 * Power down the socket.
380 */
381 Cardbus_function_disable(csc->sc_ct);
382 }
383
384 int
385 tlp_cardbus_detach(self, flags)
386 struct device *self;
387 int flags;
388 {
389 struct tulip_cardbus_softc *csc = (void *)self;
390 struct tulip_softc *sc = &csc->sc_tulip;
391 struct cardbus_devfunc *ct = csc->sc_ct;
392 int rv;
393
394 #if defined(DIAGNOSTIC)
395 if (ct == NULL)
396 panic("%s: data structure lacks\n", sc->sc_dev.dv_xname);
397 #endif
398
399 rv = tlp_detach(sc);
400 if (rv)
401 return (rv);
402
403 /*
404 * Unhook the interrupt handler.
405 */
406 if (csc->sc_ih != NULL)
407 cardbus_intr_disestablish(ct->ct_cc, ct->ct_cf, csc->sc_ih);
408
409 /*
410 * Release bus space and close window.
411 */
412 if (csc->sc_bar_reg != 0)
413 Cardbus_mapreg_unmap(ct, csc->sc_bar_reg,
414 sc->sc_st, sc->sc_sh, csc->sc_mapsize);
415
416 return (0);
417 }
418
419 int
420 tlp_cardbus_enable(sc)
421 struct tulip_softc *sc;
422 {
423 struct tulip_cardbus_softc *csc = (void *) sc;
424 cardbus_devfunc_t ct = csc->sc_ct;
425 cardbus_chipset_tag_t cc = ct->ct_cc;
426 cardbus_function_tag_t cf = ct->ct_cf;
427
428 /*
429 * Power on the socket.
430 */
431 Cardbus_function_enable(ct);
432
433 /*
434 * Set up the PCI configuration registers.
435 */
436 tlp_cardbus_setup(csc);
437
438 /*
439 * Map and establish the interrupt.
440 */
441 csc->sc_ih = cardbus_intr_establish(cc, cf, csc->sc_intrline, IPL_NET,
442 tlp_intr, sc);
443 if (csc->sc_ih == NULL) {
444 printf("%s: unable to establish interrupt at %d\n",
445 sc->sc_dev.dv_xname, csc->sc_intrline);
446 Cardbus_function_disable(csc->sc_ct);
447 return (1);
448 }
449 printf("%s: interrupting at %d\n", sc->sc_dev.dv_xname,
450 csc->sc_intrline);
451
452 return (0);
453 }
454
455 void
456 tlp_cardbus_disable(sc)
457 struct tulip_softc *sc;
458 {
459 struct tulip_cardbus_softc *csc = (void *) sc;
460 cardbus_devfunc_t ct = csc->sc_ct;
461 cardbus_chipset_tag_t cc = ct->ct_cc;
462 cardbus_function_tag_t cf = ct->ct_cf;
463
464 /* Unhook the interrupt handler. */
465 cardbus_intr_disestablish(cc, cf, csc->sc_ih);
466 csc->sc_ih = NULL;
467
468 /* Power down the socket. */
469 Cardbus_function_disable(ct);
470 }
471
472 void
473 tlp_cardbus_power(sc, why)
474 struct tulip_softc *sc;
475 int why;
476 {
477 struct tulip_cardbus_softc *csc = (void *) sc;
478
479 if (why == PWR_RESUME) {
480 /*
481 * Give the PCI configuration registers a kick
482 * in the head.
483 */
484 #ifdef DIAGNOSTIC
485 if (TULIP_IS_ENABLED(sc) == 0)
486 panic("tlp_cardbus_power");
487 #endif
488 tlp_cardbus_setup(csc);
489 }
490 }
491
492 void
493 tlp_cardbus_setup(csc)
494 struct tulip_cardbus_softc *csc;
495 {
496 struct tulip_softc *sc = &csc->sc_tulip;
497 cardbus_devfunc_t ct = csc->sc_ct;
498 cardbus_chipset_tag_t cc = ct->ct_cc;
499 cardbus_function_tag_t cf = ct->ct_cf;
500 pcireg_t reg;
501 int pmreg;
502
503 /*
504 * Check to see if the device is in power-save mode, and
505 * bring it out if necessary.
506 */
507 switch (sc->sc_chip) {
508 case TULIP_CHIP_21142:
509 case TULIP_CHIP_21143:
510 case TULIP_CHIP_X3201_3:
511 /*
512 * Clear the "sleep mode" bit in the CFDA register.
513 */
514 reg = cardbus_conf_read(cc, cf, csc->sc_tag, TULIP_PCI_CFDA);
515 if (reg & (CFDA_SLEEP|CFDA_SNOOZE))
516 cardbus_conf_write(cc, cf, csc->sc_tag, TULIP_PCI_CFDA,
517 reg & ~(CFDA_SLEEP|CFDA_SNOOZE));
518 break;
519
520 default:
521 /* Nothing. */
522 }
523
524 if (cardbus_get_capability(cc, cf, csc->sc_tag,
525 PCI_CAP_PWRMGMT, &pmreg, 0)) {
526 reg = cardbus_conf_read(cc, cf, csc->sc_tag, pmreg + 4) & 0x03;
527 #if 1 /* XXX Probably not right for CardBus. */
528 if (reg == 3) {
529 /*
530 * The card has lost all configuration data in
531 * this state, so punt.
532 */
533 printf("%s: unable to wake up from power state D3\n",
534 sc->sc_dev.dv_xname);
535 return;
536 }
537 #endif
538 if (reg != 0) {
539 printf("%s: waking up from power state D%d\n",
540 sc->sc_dev.dv_xname, reg);
541 cardbus_conf_write(cc, cf, csc->sc_tag,
542 pmreg + 4, 0);
543 }
544 }
545
546 /* Make sure the right access type is on the CardBus bridge. */
547 (*ct->ct_cf->cardbus_ctrl)(cc, csc->sc_cben);
548 (*ct->ct_cf->cardbus_ctrl)(cc, CARDBUS_BM_ENABLE);
549
550 /* Program the BAR. */
551 cardbus_conf_write(cc, cf, csc->sc_tag, csc->sc_bar_reg,
552 csc->sc_bar_val);
553
554 /* Enable the appropriate bits in the PCI CSR. */
555 reg = cardbus_conf_read(cc, cf, csc->sc_tag, PCI_COMMAND_STATUS_REG);
556 reg &= ~(PCI_COMMAND_IO_ENABLE|PCI_COMMAND_MEM_ENABLE);
557 reg |= csc->sc_csr;
558 cardbus_conf_write(cc, cf, csc->sc_tag, PCI_COMMAND_STATUS_REG, reg);
559
560 /*
561 * Make sure the latency timer is set to some reasonable
562 * value.
563 */
564 reg = cardbus_conf_read(cc, cf, csc->sc_tag, PCI_BHLC_REG);
565 if (PCI_LATTIMER(reg) < 0x20) {
566 reg &= ~(PCI_LATTIMER_MASK << PCI_LATTIMER_SHIFT);
567 reg |= (0x20 << PCI_LATTIMER_SHIFT);
568 cardbus_conf_write(cc, cf, csc->sc_tag, PCI_BHLC_REG, reg);
569 }
570 }
571
572 void
573 tlp_cardbus_x3201_reset(sc)
574 struct tulip_softc *sc;
575 {
576 u_int32_t reg;
577
578 reg = TULIP_READ(sc, CSR_SIAGEN);
579
580 /* make GP[2,0] outputs */
581 TULIP_WRITE(sc, CSR_SIAGEN, (reg & ~SIAGEN_MD) | SIAGEN_CWE |
582 0x00050000);
583 TULIP_WRITE(sc, CSR_SIAGEN, (reg & ~SIAGEN_CWE) | SIAGEN_MD);
584 }
585