if_tlp_pci.c revision 1.7 1 /* $NetBSD: if_tlp_pci.c,v 1.7 1999/09/14 05:59:53 thorpej Exp $ */
2
3 /*-
4 * Copyright (c) 1998, 1999 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 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 <net/if.h>
60 #include <net/if_dl.h>
61 #include <net/if_media.h>
62 #include <net/if_ether.h>
63
64 #if NBPFILTER > 0
65 #include <net/bpf.h>
66 #endif
67
68 #ifdef INET
69 #include <netinet/in.h>
70 #include <netinet/if_inarp.h>
71 #endif
72
73 #ifdef NS
74 #include <netns/ns.h>
75 #include <netns/ns_if.h>
76 #endif
77
78 #include <machine/bus.h>
79 #include <machine/intr.h>
80
81 #include <dev/mii/miivar.h>
82
83 #include <dev/ic/tulipreg.h>
84 #include <dev/ic/tulipvar.h>
85
86 #include <dev/pci/pcivar.h>
87 #include <dev/pci/pcireg.h>
88 #include <dev/pci/pcidevs.h>
89
90 /*
91 * PCI configuration space registers used by the Tulip.
92 */
93 #define TULIP_PCI_IOBA 0x10 /* i/o mapped base */
94 #define TULIP_PCI_MMBA 0x14 /* memory mapped base */
95
96 struct tulip_pci_softc {
97 struct tulip_softc sc_tulip; /* real Tulip softc */
98
99 /* PCI-specific goo. */
100 void *sc_ih; /* interrupt handle */
101 };
102
103 int tlp_pci_match __P((struct device *, struct cfdata *, void *));
104 void tlp_pci_attach __P((struct device *, struct device *, void *));
105
106 struct cfattach tlp_pci_ca = {
107 sizeof(struct tulip_pci_softc), tlp_pci_match, tlp_pci_attach,
108 };
109
110 const struct tulip_pci_product {
111 u_int32_t tpp_vendor; /* PCI vendor ID */
112 u_int32_t tpp_product; /* PCI product ID */
113 tulip_chip_t tpp_chip; /* base Tulip chip type */
114 } tlp_pci_products[] = {
115 #ifdef TLP_MATCH_21040
116 { PCI_VENDOR_DEC, PCI_PRODUCT_DEC_21040,
117 TULIP_CHIP_21040 },
118 #endif
119 #ifdef TLP_MATCH_21041
120 { PCI_VENDOR_DEC, PCI_PRODUCT_DEC_21041,
121 TULIP_CHIP_21041 },
122 #endif
123 #ifdef TLP_MATCH_21140
124 { PCI_VENDOR_DEC, PCI_PRODUCT_DEC_21140,
125 TULIP_CHIP_21140 },
126 #endif
127 #ifdef TLP_MATCH_21142
128 { PCI_VENDOR_DEC, PCI_PRODUCT_DEC_21142,
129 TULIP_CHIP_21142 },
130 #endif
131
132 { PCI_VENDOR_LITEON, PCI_PRODUCT_LITEON_82C168,
133 TULIP_CHIP_82C168 },
134
135 #if 0
136 /*
137 * Note: This is like a MX98715A with Wake-On-LAN and a
138 * 128-bit multicast hash table.
139 */
140 { PCI_VENDOR_LITEON, PCI_PRODUCT_LITEON_82C115,
141 TULIP_CHIP_82C115 },
142 #endif
143
144 #if 0
145 { PCI_VENDOR_MACRONIX, PCI_PRODUCT_MACRONIX_MX98713,
146 TULIP_CHIP_MX98713 },
147 { PCI_VENDOR_MACRONIX, PCI_PRODUCT_MACRONIX_MX987x5,
148 TULIP_CHIP_MX98715 },
149
150 { PCI_VENDOR_COMPEX, PCI_PRODUCT_COMPEX_RL100TX,
151 TULIP_CHIP_MX98713 },
152 #endif
153
154 { PCI_VENDOR_WINBOND, PCI_PRODUCT_WINBOND_W89C840F,
155 TULIP_CHIP_WB89C840F },
156 { PCI_VENDOR_COMPEX, PCI_PRODUCT_COMPEX_RL100ATX,
157 TULIP_CHIP_WB89C840F },
158
159 #if 0
160 { PCI_VENDOR_DAVICOM, PCI_PRODUCT_DAVICOM_DM9102,
161 TULIP_CHIP_DM9102 },
162
163 { PCI_VENDOR_ADMTEK, PCI_PRODUCT_ADMTEK_AL981,
164 TULIP_CHIP_AL981 },
165
166 { PCI_VENDOR_ASIX, PCI_PRODUCT_ASIX_AX88140A,
167 TULIP_CHIP_AX88140 },
168 #endif
169
170 { 0, 0,
171 TULIP_CHIP_INVALID },
172 };
173
174 const char *tlp_pci_chip_names[] = TULIP_CHIP_NAMES;
175
176 const struct tulip_pci_product *tlp_pci_lookup
177 __P((const struct pci_attach_args *));
178
179 const struct tulip_pci_product *
180 tlp_pci_lookup(pa)
181 const struct pci_attach_args *pa;
182 {
183 const struct tulip_pci_product *tpp;
184
185 for (tpp = tlp_pci_products;
186 tlp_pci_chip_names[tpp->tpp_chip] != NULL;
187 tpp++) {
188 if (PCI_VENDOR(pa->pa_id) == tpp->tpp_vendor &&
189 PCI_PRODUCT(pa->pa_id) == tpp->tpp_product)
190 return (tpp);
191 }
192 return (NULL);
193 }
194
195 int
196 tlp_pci_match(parent, match, aux)
197 struct device *parent;
198 struct cfdata *match;
199 void *aux;
200 {
201 struct pci_attach_args *pa = aux;
202
203 if (tlp_pci_lookup(pa) != NULL)
204 return (10); /* beat if_de.c */
205
206 return (0);
207 }
208
209 void
210 tlp_pci_attach(parent, self, aux)
211 struct device *parent, *self;
212 void *aux;
213 {
214 struct tulip_pci_softc *psc = (void *) self;
215 struct tulip_softc *sc = &psc->sc_tulip;
216 struct pci_attach_args *pa = aux;
217 pci_chipset_tag_t pc = pa->pa_pc;
218 pci_intr_handle_t ih;
219 const char *intrstr = NULL;
220 bus_space_tag_t iot, memt;
221 bus_space_handle_t ioh, memh;
222 int ioh_valid, memh_valid, i, j;
223 const struct tulip_pci_product *tpp;
224 u_int8_t enaddr[ETHER_ADDR_LEN];
225 u_int32_t val;
226 const char *name = NULL;
227
228 /*
229 * Map the device.
230 */
231 ioh_valid = (pci_mapreg_map(pa, TULIP_PCI_IOBA,
232 PCI_MAPREG_TYPE_IO, 0,
233 &iot, &ioh, NULL, NULL) == 0);
234 memh_valid = (pci_mapreg_map(pa, TULIP_PCI_MMBA,
235 PCI_MAPREG_TYPE_MEM|PCI_MAPREG_MEM_TYPE_32BIT, 0,
236 &memt, &memh, NULL, NULL) == 0);
237
238 if (memh_valid) {
239 sc->sc_st = memt;
240 sc->sc_sh = memh;
241 } else if (ioh_valid) {
242 sc->sc_st = iot;
243 sc->sc_sh = ioh;
244 } else {
245 printf(": unable to map device registers\n");
246 return;
247 }
248
249 tpp = tlp_pci_lookup(pa);
250 if (tpp == NULL) {
251 printf("\n");
252 panic("tlp_pci_attach: impossible");
253 }
254 sc->sc_chip = tpp->tpp_chip;
255
256 /*
257 * By default, Tulip registers are 8 bytes long (4 bytes
258 * followed by a 4 byte pad).
259 */
260 sc->sc_regshift = 3;
261
262 /*
263 * Get revision info, and set some chip-specific variables.
264 */
265 sc->sc_rev = PCI_REVISION(pa->pa_class);
266 switch (sc->sc_chip) {
267 case TULIP_CHIP_21140:
268 if (sc->sc_rev >= 0x20)
269 sc->sc_chip = TULIP_CHIP_21140A;
270 break;
271
272 case TULIP_CHIP_21142:
273 if (sc->sc_rev >= 0x20)
274 sc->sc_chip = TULIP_CHIP_21143;
275 break;
276
277 case TULIP_CHIP_82C168:
278 if (sc->sc_rev >= 0x20)
279 sc->sc_chip = TULIP_CHIP_82C169;
280 break;
281
282 case TULIP_CHIP_MX98713:
283 if (sc->sc_rev >= 0x10)
284 sc->sc_chip = TULIP_CHIP_MX98713A;
285 break;
286
287 case TULIP_CHIP_MX98715:
288 if (sc->sc_rev >= 0x30)
289 sc->sc_chip = TULIP_CHIP_MX98725;
290 break;
291
292 case TULIP_CHIP_WB89C840F:
293 sc->sc_regshift = 2;
294 break;
295
296 case TULIP_CHIP_AX88140:
297 if (sc->sc_rev >= 0x10)
298 sc->sc_chip = TULIP_CHIP_AX88141;
299 break;
300
301 default:
302 /* Nothing. */
303 }
304
305 printf(": %s Ethernet, pass %d.%d\n",
306 tlp_pci_chip_names[sc->sc_chip],
307 (sc->sc_rev >> 4) & 0xf, sc->sc_rev & 0xf);
308
309 sc->sc_dmat = pa->pa_dmat;
310
311 /*
312 * Make sure bus mastering is enabled.
313 */
314 pci_conf_write(pc, pa->pa_tag, PCI_COMMAND_STATUS_REG,
315 pci_conf_read(pc, pa->pa_tag, PCI_COMMAND_STATUS_REG) |
316 PCI_COMMAND_MASTER_ENABLE);
317
318 /*
319 * Get the cacheline size.
320 */
321 sc->sc_cacheline = PCI_CACHELINE(pci_conf_read(pc, pa->pa_tag,
322 PCI_BHLC_REG));
323
324 /*
325 * Read the contents of the Ethernet Address ROM/SROM.
326 */
327 memset(sc->sc_srom, 0, sizeof(sc->sc_srom));
328 switch (sc->sc_chip) {
329 case TULIP_CHIP_21040:
330 TULIP_WRITE(sc, CSR_MIIROM, MIIROM_SROMCS);
331 for (i = 0; i < sizeof(sc->sc_srom); i++) {
332 for (j = 0; j < 10000; j++) {
333 val = TULIP_READ(sc, CSR_MIIROM);
334 if ((val & MIIROM_DN) == 0)
335 break;
336 }
337 sc->sc_srom[i] = val & MIIROM_DATA;
338 }
339 break;
340
341 case TULIP_CHIP_82C168:
342 case TULIP_CHIP_82C169:
343 {
344 u_int16_t *rombuf = (u_int16_t *)sc->sc_srom;
345
346 /*
347 * The Lite-On PNIC stores the Ethernet address in
348 * the first 3 words of the EEPROM. EEPROM access
349 * is not like the other Tulip chips.
350 */
351 for (i = 0; i < 3; i++) {
352 TULIP_WRITE(sc, CSR_PNIC_SROMCTL,
353 PNIC_SROMCTL_READ | i);
354 for (j = 0; j < 500; j++) {
355 delay(2);
356 val = TULIP_READ(sc, CSR_MIIROM);
357 if ((val & PNIC_MIIROM_BUSY) == 0)
358 break;
359 }
360 if (val & PNIC_MIIROM_BUSY) {
361 printf("%s: EEPROM timed out\n",
362 sc->sc_dev.dv_xname);
363 return;
364 }
365 rombuf[i] = bswap16(val & PNIC_MIIROM_DATA);
366 }
367 break;
368 }
369
370 default:
371 tlp_read_srom(sc, 0, sizeof(sc->sc_srom) >> 2,
372 (u_int16_t *)sc->sc_srom);
373 }
374
375 /*
376 * Deal with chip/board quirks. This includes setting up
377 * the mediasw, and extracting the Ethernet address from
378 * the rombuf.
379 *
380 * XXX Eventually, handle master/slave interrupts on the
381 * XXX multi-port boards which require that.
382 */
383 switch (sc->sc_chip) {
384 case TULIP_CHIP_21040:
385 /*
386 * None of the 21040 boards have the new-style SROMs.
387 */
388 if (tlp_parse_old_srom(sc, enaddr) == 0) {
389 printf("%s: unable to decode old-style SROM\n",
390 sc->sc_dev.dv_xname);
391 return;
392 }
393
394 /*
395 * All 21040 boards start out with the same
396 * media switch.
397 */
398 sc->sc_mediasw = &tlp_21040_mediasw;
399
400 /*
401 * XXX Eventually we should attempt to identify
402 * XXX other boards, and set the media appropriately.
403 */
404 break;
405
406 case TULIP_CHIP_82C168:
407 case TULIP_CHIP_82C169:
408 /*
409 * Lite-On PNIC's Ethernet address is the first 6
410 * bytes of its EEPROM.
411 */
412 memcpy(enaddr, sc->sc_srom, ETHER_ADDR_LEN);
413
414 /*
415 * Lite-On PNICs always use the same mediasw; we
416 * select MII vs. internal NWAY automatically.
417 */
418 sc->sc_mediasw = &tlp_pnic_mediasw;
419 break;
420
421 case TULIP_CHIP_WB89C840F:
422 /*
423 * Winbond 89C840F's Ethernet address is the first
424 * 6 bytes of its EEPROM.
425 */
426 memcpy(enaddr, sc->sc_srom, ETHER_ADDR_LEN);
427
428 /*
429 * Winbond 89C840F has an MII attached to the SIO.
430 */
431 sc->sc_mediasw = &tlp_sio_mii_mediasw;
432 break;
433
434 default:
435 printf("%s: sorry, unable to handle your board\n",
436 sc->sc_dev.dv_xname);
437 return;
438 }
439
440 /*
441 * Map and establish our interrupt.
442 */
443 if (pci_intr_map(pc, pa->pa_intrtag, pa->pa_intrpin,
444 pa->pa_intrline, &ih)) {
445 printf("%s: unable to map interrupt\n", sc->sc_dev.dv_xname);
446 return;
447 }
448 intrstr = pci_intr_string(pc, ih);
449 psc->sc_ih = pci_intr_establish(pc, ih, IPL_NET, tlp_intr, sc);
450 if (psc->sc_ih == NULL) {
451 printf("%s: unable to establish interrupt",
452 sc->sc_dev.dv_xname);
453 if (intrstr != NULL)
454 printf(" at %s", intrstr);
455 printf("\n");
456 return;
457 }
458 printf("%s: interrupting at %s\n", sc->sc_dev.dv_xname, intrstr);
459
460 /*
461 * Finish off the attach.
462 */
463 tlp_attach(sc, name, enaddr);
464 }
465