if_tlp_cardbus.c revision 1.19 1 /* $NetBSD: if_tlp_cardbus.c,v 1.19 2000/03/14 02:08:21 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 * TODO:
45 *
46 * - power management
47 */
48
49 #include "opt_inet.h"
50 #include "opt_ns.h"
51 #include "bpfilter.h"
52
53 #include <sys/param.h>
54 #include <sys/systm.h>
55 #include <sys/mbuf.h>
56 #include <sys/malloc.h>
57 #include <sys/kernel.h>
58 #include <sys/socket.h>
59 #include <sys/ioctl.h>
60 #include <sys/errno.h>
61 #include <sys/device.h>
62
63 #include <machine/endian.h>
64
65 #include <net/if.h>
66 #include <net/if_dl.h>
67 #include <net/if_media.h>
68 #include <net/if_ether.h>
69
70 #if NBPFILTER > 0
71 #include <net/bpf.h>
72 #endif
73
74 #ifdef INET
75 #include <netinet/in.h>
76 #include <netinet/if_inarp.h>
77 #endif
78
79 #ifdef NS
80 #include <netns/ns.h>
81 #include <netns/ns_if.h>
82 #endif
83
84 #include <machine/bus.h>
85 #include <machine/intr.h>
86
87 #include <dev/mii/miivar.h>
88 #include <dev/mii/mii_bitbang.h>
89
90 #include <dev/ic/tulipreg.h>
91 #include <dev/ic/tulipvar.h>
92
93 #include <dev/pci/pcivar.h>
94 #include <dev/pci/pcireg.h>
95 #include <dev/pci/pcidevs.h>
96
97 #include <dev/cardbus/cardbusvar.h>
98
99 /*
100 * PCI configuration space registers used by the Tulip.
101 */
102 #define TULIP_PCI_IOBA 0x10 /* i/o mapped base */
103 #define TULIP_PCI_MMBA 0x14 /* memory mapped base */
104 #define TULIP_PCI_CFDA 0x40 /* configuration driver area */
105
106 #define CFDA_SLEEP 0x80000000 /* sleep mode */
107 #define CFDA_SNOOZE 0x40000000 /* snooze mode */
108
109 struct tulip_cardbus_softc {
110 struct tulip_softc sc_tulip; /* real Tulip softc */
111
112 /* CardBus-specific goo. */
113 void *sc_ih; /* interrupt handle */
114 cardbus_devfunc_t sc_ct; /* our CardBus devfuncs */
115 cardbustag_t sc_tag; /* our CardBus tag */
116 int sc_csr; /* CSR bits */
117 bus_size_t sc_mapsize; /* the size of mapped bus space
118 region */
119 int sc_attached; /* device is attached */
120 /* product info */
121 const struct tulip_cardbus_product *sc_product;
122
123 int sc_cben; /* CardBus enables */
124 int sc_bar_reg; /* which BAR to use */
125 pcireg_t sc_bar_val; /* value of the BAR */
126 };
127
128 int tlp_cardbus_match __P((struct device *, struct cfdata *, void *));
129 void tlp_cardbus_attach __P((struct device *, struct device *, void *));
130 int tlp_cardbus_detach __P((struct device *, int));
131
132 struct cfattach tlp_cardbus_ca = {
133 sizeof(struct tulip_cardbus_softc),
134 tlp_cardbus_match, tlp_cardbus_attach,
135 tlp_cardbus_detach, tlp_activate,
136 };
137
138 const struct tulip_cardbus_product {
139 u_int32_t tcp_vendor; /* PCI vendor ID */
140 u_int32_t tcp_product; /* PCI product ID */
141 tulip_chip_t tcp_chip; /* base Tulip chip type */
142 int tcp_pmreg; /* power management register offset */
143 } tlp_cardbus_products[] = {
144 { PCI_VENDOR_DEC, PCI_PRODUCT_DEC_21142,
145 TULIP_CHIP_21142, 0xe0 },
146
147 { PCI_VENDOR_XIRCOM, PCI_PRODUCT_XIRCOM_X3201_3_21143,
148 TULIP_CHIP_X3201_3, 0xe0 },
149
150 { 0, 0,
151 TULIP_CHIP_INVALID, 0 },
152 };
153
154 void tlp_cardbus_setup __P((struct tulip_cardbus_softc *));
155
156 void tlp_cardbus_x3201_reset __P((struct tulip_softc *));
157
158 const struct tulip_cardbus_product *tlp_cardbus_lookup
159 __P((const struct cardbus_attach_args *));
160
161 const struct tulip_cardbus_product *
162 tlp_cardbus_lookup(ca)
163 const struct cardbus_attach_args *ca;
164 {
165 const struct tulip_cardbus_product *tcp;
166
167 for (tcp = tlp_cardbus_products;
168 tlp_chip_names[tcp->tcp_chip] != NULL;
169 tcp++) {
170 if (PCI_VENDOR(ca->ca_id) == tcp->tcp_vendor &&
171 PCI_PRODUCT(ca->ca_id) == tcp->tcp_product)
172 return (tcp);
173 }
174 return (NULL);
175 }
176
177 int
178 tlp_cardbus_match(parent, match, aux)
179 struct device *parent;
180 struct cfdata *match;
181 void *aux;
182 {
183 struct cardbus_attach_args *ca = aux;
184
185 if (tlp_cardbus_lookup(ca) != NULL)
186 return (1);
187
188 return (0);
189 }
190
191 void
192 tlp_cardbus_attach(parent, self, aux)
193 struct device *parent, *self;
194 void *aux;
195 {
196 struct tulip_cardbus_softc *csc = (void *)self;
197 struct tulip_softc *sc = &csc->sc_tulip;
198 struct cardbus_attach_args *ca = aux;
199 cardbus_devfunc_t ct = ca->ca_ct;
200 cardbus_chipset_tag_t cc = ct->ct_cc;
201 cardbus_function_tag_t cf = ct->ct_cf;
202 const struct tulip_cardbus_product *tcp;
203 u_int8_t enaddr[ETHER_ADDR_LEN];
204 bus_addr_t adr;
205
206 sc->sc_devno = ca->ca_device;
207 sc->sc_dmat = ca->ca_dmat;
208 csc->sc_ct = ct;
209 csc->sc_tag = ca->ca_tag;
210
211 tcp = tlp_cardbus_lookup(ca);
212 if (tcp == NULL) {
213 printf("\n");
214 panic("tlp_cardbus_attach: impossible");
215 }
216 sc->sc_chip = tcp->tcp_chip;
217 csc->sc_product = tcp;
218
219 /*
220 * By default, Tulip registers are 8 bytes long (4 bytes
221 * followed by a 4 byte pad).
222 */
223 sc->sc_regshift = 3;
224
225 /*
226 * Get revision info, and set some chip-specific variables.
227 */
228 sc->sc_rev = PCI_REVISION(ca->ca_class);
229 switch (sc->sc_chip) {
230 case TULIP_CHIP_21142:
231 if (sc->sc_rev >= 0x20)
232 sc->sc_chip = TULIP_CHIP_21143;
233 break;
234
235 default:
236 /* Nothing. */
237 }
238
239 printf(": %s Ethernet, pass %d.%d\n",
240 tlp_chip_names[sc->sc_chip],
241 (sc->sc_rev >> 4) & 0xf, sc->sc_rev & 0xf);
242
243 /*
244 * Map the device.
245 */
246 csc->sc_csr = PCI_COMMAND_MASTER_ENABLE;
247 if (Cardbus_mapreg_map(ct, TULIP_PCI_IOBA,
248 PCI_MAPREG_TYPE_IO, 0, &sc->sc_st, &sc->sc_sh, &adr,
249 &csc->sc_mapsize) == 0) {
250 #if rbus
251 #else
252 (*ct->ct_cf->cardbus_io_open)(cc, 0, adr, adr+csc->sc_mapsize);
253 #endif
254 csc->sc_cben = CARDBUS_IO_ENABLE;
255 csc->sc_csr |= PCI_COMMAND_IO_ENABLE;
256 csc->sc_bar_reg = TULIP_PCI_IOBA;
257 csc->sc_bar_val = adr | PCI_MAPREG_TYPE_IO;
258 } else if (Cardbus_mapreg_map(ct, TULIP_PCI_MMBA,
259 PCI_MAPREG_TYPE_MEM|PCI_MAPREG_MEM_TYPE_32BIT, 0,
260 &sc->sc_st, &sc->sc_sh, &adr, &csc->sc_mapsize) == 0) {
261 #if rbus
262 #else
263 (*ct->ct_cf->cardbus_mem_open)(cc, 0, adr, adr+csc->sc_mapsize);
264 #endif
265 csc->sc_cben = CARDBUS_MEM_ENABLE;
266 csc->sc_csr |= PCI_COMMAND_MEM_ENABLE;
267 csc->sc_bar_reg = TULIP_PCI_MMBA;
268 csc->sc_bar_val = adr | PCI_MAPREG_TYPE_MEM;
269 } else {
270 printf("%s: unable to map device registers\n",
271 sc->sc_dev.dv_xname);
272 return;
273 }
274
275 /*
276 * Bring the chip out of powersave mode and initialize the
277 * configuration registers.
278 */
279 tlp_cardbus_setup(csc);
280
281 /*
282 * Read the contents of the Ethernet Address ROM/SROM.
283 */
284 switch (sc->sc_chip) {
285 case TULIP_CHIP_X3201_3:
286 /*
287 * No SROM on this chip.
288 */
289 break;
290
291 default:
292 if (tlp_read_srom(sc) == 0)
293 goto cant_cope;
294 break;
295 }
296
297 /*
298 * Deal with chip/board quirks. This includes setting up
299 * the mediasw, and extracting the Ethernet address from
300 * the rombuf.
301 */
302 switch (sc->sc_chip) {
303 case TULIP_CHIP_21142:
304 case TULIP_CHIP_21143:
305 /* Check for new format SROM. */
306 if (tlp_isv_srom_enaddr(sc, enaddr) == 0) {
307 /*
308 * Not an ISV SROM; try the old DEC Ethernet Address
309 * ROM format.
310 */
311 if (tlp_parse_old_srom(sc, enaddr) == 0)
312 goto cant_cope;
313 } else {
314 /*
315 * We start out with the 2114x ISV media switch.
316 * When we search for quirks, we may change to
317 * a different switch.
318 */
319 sc->sc_mediasw = &tlp_2114x_isv_mediasw;
320 }
321
322 /*
323 * Bail out now if we can't deal with this board.
324 */
325 if (sc->sc_mediasw == NULL)
326 goto cant_cope;
327 break;
328
329 case TULIP_CHIP_X3201_3:
330 /*
331 * The X3201 doesn't have an SROM. Lift the MAC address
332 * from the CIS. Also, we have a special media switch:
333 * MII-on-SIO, plus some special GPIO setup.
334 */
335 memcpy(enaddr, ca->ca_cis.funce.network.netid, sizeof(enaddr));
336 sc->sc_reset = tlp_cardbus_x3201_reset;
337 sc->sc_mediasw = &tlp_sio_mii_mediasw;
338 break;
339
340 default:
341 cant_cope:
342 printf("%s: sorry, unable to handle your board\n",
343 sc->sc_dev.dv_xname);
344 return;
345 }
346
347 /*
348 * Map and establish the interrupt.
349 */
350 csc->sc_ih = cardbus_intr_establish(cc, cf, ca->ca_intrline, IPL_NET,
351 tlp_intr, sc);
352 if (csc->sc_ih == NULL) {
353 printf("%s: unable to establish interrupt at %d\n",
354 sc->sc_dev.dv_xname, ca->ca_intrline);
355 return;
356 }
357 printf("%s: interrupting at %d\n", sc->sc_dev.dv_xname,
358 ca->ca_intrline);
359
360 /*
361 * Finish off the attach.
362 */
363 csc->sc_attached = 1;
364 tlp_attach(sc, enaddr);
365 }
366
367 int
368 tlp_cardbus_detach(self, flags)
369 struct device *self;
370 int flags;
371 {
372 struct tulip_cardbus_softc *csc = (void *)self;
373 struct tulip_softc *sc = &csc->sc_tulip;
374 struct cardbus_devfunc *ct = csc->sc_ct;
375 int rv;
376 int reg;
377
378 #if defined(DIAGNOSTIC)
379 if (ct == NULL) {
380 panic("%s: data structure lacks\n", sc->sc_dev.dv_xname);
381 }
382 #endif
383
384 if (csc->sc_attached) {
385 rv = tlp_detach(sc);
386 if (rv)
387 return (rv);
388 }
389
390 /*
391 * Unhook the interrupt handler.
392 */
393 cardbus_intr_disestablish(ct->ct_cc, ct->ct_cf, csc->sc_ih);
394
395 /*
396 * release bus space and close window
397 */
398 if (csc->sc_csr & PCI_COMMAND_MEM_ENABLE)
399 reg = TULIP_PCI_MMBA;
400 else
401 reg = TULIP_PCI_IOBA;
402 Cardbus_mapreg_unmap(ct, reg, sc->sc_st, sc->sc_sh,
403 csc->sc_mapsize);
404
405 return (0);
406 }
407
408 void
409 tlp_cardbus_setup(csc)
410 struct tulip_cardbus_softc *csc;
411 {
412 struct tulip_softc *sc = &csc->sc_tulip;
413 const struct tulip_cardbus_product *tcp = csc->sc_product;
414 cardbus_devfunc_t ct = csc->sc_ct;
415 cardbus_chipset_tag_t cc = ct->ct_cc;
416 cardbus_function_tag_t cf = ct->ct_cf;
417 pcireg_t reg;
418
419 /*
420 * Check to see if the device is in power-save mode, and
421 * bring it out if necessary.
422 */
423 switch (sc->sc_chip) {
424 case TULIP_CHIP_21142:
425 case TULIP_CHIP_21143:
426 case TULIP_CHIP_X3201_3:
427 /*
428 * Clear the "sleep mode" bit in the CFDA register.
429 */
430 reg = cardbus_conf_read(cc, cf, csc->sc_tag, TULIP_PCI_CFDA);
431 if (reg & (CFDA_SLEEP|CFDA_SNOOZE))
432 cardbus_conf_write(cc, cf, csc->sc_tag, TULIP_PCI_CFDA,
433 reg & ~(CFDA_SLEEP|CFDA_SNOOZE));
434 break;
435
436 default:
437 /* Nothing. */
438 }
439
440 if (cardbus_get_capability(cc, cf, csc->sc_tag,
441 PCI_CAP_PWRMGMT, 0, 0)) {
442 if (tcp->tcp_pmreg == 0) {
443 printf("%s: don't know location of PMCSR for this "
444 "chip\n", sc->sc_dev.dv_xname);
445 return;
446 }
447 reg = cardbus_conf_read(cc, cf, csc->sc_tag,
448 tcp->tcp_pmreg) & 0x03;
449 #if 1 /* XXX Probably not right for CardBus. */
450 if (reg == 3) {
451 /*
452 * The card has lost all configuration data in
453 * this state, so punt.
454 */
455 printf("%s: unable to wake up from power state D3\n",
456 sc->sc_dev.dv_xname);
457 return;
458 }
459 #endif
460 if (reg != 0) {
461 printf("%s: waking up from power state D%d\n",
462 sc->sc_dev.dv_xname, reg);
463 cardbus_conf_write(cc, cf, csc->sc_tag,
464 tcp->tcp_pmreg, 0);
465 }
466 }
467
468 /* Make sure the right access type is on the CardBus bridge. */
469 (*ct->ct_cf->cardbus_ctrl)(cc, csc->sc_cben);
470 (*ct->ct_cf->cardbus_ctrl)(cc, CARDBUS_BM_ENABLE);
471
472 /* Program the BAR. */
473 cardbus_conf_write(cc, cf, csc->sc_tag, csc->sc_bar_reg,
474 csc->sc_bar_val);
475
476 /* Enable the appropriate bits in the PCI CSR. */
477 reg = cardbus_conf_read(cc, cf, csc->sc_tag, PCI_COMMAND_STATUS_REG);
478 reg &= ~(PCI_COMMAND_IO_ENABLE|PCI_COMMAND_MEM_ENABLE);
479 reg |= csc->sc_csr;
480 cardbus_conf_write(cc, cf, csc->sc_tag, PCI_COMMAND_STATUS_REG, reg);
481
482 /*
483 * Make sure the latency timer is set to some reasonable
484 * value.
485 */
486 reg = cardbus_conf_read(cc, cf, csc->sc_tag, PCI_BHLC_REG);
487 if (PCI_LATTIMER(reg) < 0x20) {
488 reg &= ~(PCI_LATTIMER_MASK << PCI_LATTIMER_SHIFT);
489 reg |= (0x20 << PCI_LATTIMER_SHIFT);
490 cardbus_conf_write(cc, cf, csc->sc_tag, PCI_BHLC_REG, reg);
491 }
492 }
493
494 void
495 tlp_cardbus_x3201_reset(sc)
496 struct tulip_softc *sc;
497 {
498 u_int32_t reg;
499
500 reg = TULIP_READ(sc, CSR_SIAGEN);
501
502 /* make GP[2,0] outputs */
503 TULIP_WRITE(sc, CSR_SIAGEN, (reg & ~SIAGEN_MD) | SIAGEN_CWE |
504 0x00050000);
505 TULIP_WRITE(sc, CSR_SIAGEN, (reg & ~SIAGEN_CWE) | SIAGEN_MD);
506 }
507