if_tlp_pci.c revision 1.4 1 /* $NetBSD: if_tlp_pci.c,v 1.4 1999/09/08 22:29:47 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 #if 0
116 { PCI_VENDOR_DEC, PCI_PRODUCT_DEC_21040,
117 TULIP_CHIP_21040 },
118 { PCI_VENDOR_DEC, PCI_PRODUCT_DEC_21041,
119 TULIP_CHIP_21041 },
120 { PCI_VENDOR_DEC, PCI_PRODUCT_DEC_21140,
121 TULIP_CHIP_21140 },
122 { PCI_VENDOR_DEC, PCI_PRODUCT_DEC_21142,
123 TULIP_CHIP_21142 },
124 #endif
125
126 { PCI_VENDOR_LITEON, PCI_PRODUCT_LITEON_82C168,
127 TULIP_CHIP_82C168 },
128
129 #if 0
130 /*
131 * Note: This is like a MX98715A with Wake-On-LAN and a
132 * 128-bit multicast hash table.
133 */
134 { PCI_VENDOR_LITEON, PCI_PRODUCT_LITEON_82C115,
135 TULIP_CHIP_82C115 },
136 #endif
137
138 #if 0
139 { PCI_VENDOR_MACRONIX, PCI_PRODUCT_MACRONIX_MX98713,
140 TULIP_CHIP_MX98713 },
141 { PCI_VENDOR_MACRONIX, PCI_PRODUCT_MACRONIX_MX987x5,
142 TULIP_CHIP_MX98715 },
143
144 { PCI_VENDOR_COMPEX, PCI_PRODUCT_COMPEX_RL100TX,
145 TULIP_CHIP_MX98713 },
146 #endif
147
148 { PCI_VENDOR_WINBOND, PCI_PRODUCT_WINBOND_W89C840F,
149 TULIP_CHIP_WB89C840F },
150 { PCI_VENDOR_COMPEX, PCI_PRODUCT_COMPEX_RL100ATX,
151 TULIP_CHIP_WB89C840F },
152
153 #if 0
154 { PCI_VENDOR_DAVICOM, PCI_PRODUCT_DAVICOM_DM9102,
155 TULIP_CHIP_DM9102 },
156
157 { PCI_VENDOR_ADMTEK, PCI_PRODUCT_ADMTEK_AL981,
158 TULIP_CHIP_AL981 },
159
160 { PCI_VENDOR_ASIX, PCI_PRODUCT_ASIX_AX88140A,
161 TULIP_CHIP_AX88140 },
162 #endif
163
164 { 0, 0,
165 TULIP_CHIP_INVALID },
166 };
167
168 const char *tlp_pci_chip_names[] = TULIP_CHIP_NAMES;
169
170 const struct tulip_pci_product *tlp_pci_lookup
171 __P((const struct pci_attach_args *));
172
173 const struct tulip_pci_product *
174 tlp_pci_lookup(pa)
175 const struct pci_attach_args *pa;
176 {
177 const struct tulip_pci_product *tpp;
178
179 for (tpp = tlp_pci_products;
180 tlp_pci_chip_names[tpp->tpp_chip] != NULL;
181 tpp++) {
182 if (PCI_VENDOR(pa->pa_id) == tpp->tpp_vendor &&
183 PCI_PRODUCT(pa->pa_id) == tpp->tpp_product)
184 return (tpp);
185 }
186 return (NULL);
187 }
188
189 int
190 tlp_pci_match(parent, match, aux)
191 struct device *parent;
192 struct cfdata *match;
193 void *aux;
194 {
195 struct pci_attach_args *pa = aux;
196
197 if (tlp_pci_lookup(pa) != NULL)
198 return (10); /* beat if_de.c */
199
200 return (0);
201 }
202
203 void
204 tlp_pci_attach(parent, self, aux)
205 struct device *parent, *self;
206 void *aux;
207 {
208 struct tulip_pci_softc *psc = (void *) self;
209 struct tulip_softc *sc = &psc->sc_tulip;
210 struct pci_attach_args *pa = aux;
211 pci_chipset_tag_t pc = pa->pa_pc;
212 pci_intr_handle_t ih;
213 const char *intrstr = NULL;
214 bus_space_tag_t iot, memt;
215 bus_space_handle_t ioh, memh;
216 int ioh_valid, memh_valid, i, j;
217 const struct tulip_pci_product *tpp;
218 u_int8_t enaddr[ETHER_ADDR_LEN], *romdata;
219 u_int16_t rombuf[TULIP_MAX_ROM_SIZE >> 1];
220 u_int32_t val;
221 const char *name = NULL;
222
223 /*
224 * Map the device.
225 */
226 ioh_valid = (pci_mapreg_map(pa, TULIP_PCI_IOBA,
227 PCI_MAPREG_TYPE_IO, 0,
228 &iot, &ioh, NULL, NULL) == 0);
229 memh_valid = (pci_mapreg_map(pa, TULIP_PCI_MMBA,
230 PCI_MAPREG_TYPE_MEM|PCI_MAPREG_MEM_TYPE_32BIT, 0,
231 &memt, &memh, NULL, NULL) == 0);
232
233 if (memh_valid) {
234 sc->sc_st = memt;
235 sc->sc_sh = memh;
236 } else if (ioh_valid) {
237 sc->sc_st = iot;
238 sc->sc_sh = ioh;
239 } else {
240 printf(": unable to map device registers\n");
241 return;
242 }
243
244 tpp = tlp_pci_lookup(pa);
245 if (tpp == NULL) {
246 printf("\n");
247 panic("tlp_pci_attach: impossible");
248 }
249 sc->sc_chip = tpp->tpp_chip;
250
251 /*
252 * Get revision info, and set some chip-specific variables.
253 */
254 sc->sc_rev = PCI_REVISION(pa->pa_class);
255 sc->sc_regshift = 0;
256 switch (sc->sc_chip) {
257 case TULIP_CHIP_21140:
258 if (sc->sc_rev >= 0x20)
259 sc->sc_chip = TULIP_CHIP_21140A;
260 break;
261
262 case TULIP_CHIP_21142:
263 if (sc->sc_rev >= 0x20)
264 sc->sc_chip = TULIP_CHIP_21143;
265 break;
266
267 case TULIP_CHIP_82C168:
268 if (sc->sc_rev >= 0x20)
269 sc->sc_chip = TULIP_CHIP_82C169;
270 break;
271
272 case TULIP_CHIP_MX98713:
273 if (sc->sc_rev >= 0x10)
274 sc->sc_chip = TULIP_CHIP_MX98713A;
275 break;
276
277 case TULIP_CHIP_MX98715:
278 if (sc->sc_rev >= 0x30)
279 sc->sc_chip = TULIP_CHIP_MX98725;
280 break;
281
282 case TULIP_CHIP_WB89C840F:
283 sc->sc_regshift = 1;
284 break;
285
286 case TULIP_CHIP_AX88140:
287 if (sc->sc_rev >= 0x10)
288 sc->sc_chip = TULIP_CHIP_AX88141;
289 break;
290
291 default:
292 /* Nothing. */
293 }
294
295 printf(": %s Ethernet, pass %d.%d\n",
296 tlp_pci_chip_names[sc->sc_chip],
297 (sc->sc_rev >> 4) & 0xf, sc->sc_rev & 0xf);
298
299 sc->sc_dmat = pa->pa_dmat;
300
301 /*
302 * Make sure bus mastering is enabled.
303 */
304 pci_conf_write(pc, pa->pa_tag, PCI_COMMAND_STATUS_REG,
305 pci_conf_read(pc, pa->pa_tag, PCI_COMMAND_STATUS_REG) |
306 PCI_COMMAND_MASTER_ENABLE);
307
308 /*
309 * Get the cacheline size.
310 */
311 sc->sc_cacheline = PCI_CACHELINE(pci_conf_read(pc, pa->pa_tag,
312 PCI_BHLC_REG));
313
314 /*
315 * Read the contents of the Ethernet Address ROM/SROM.
316 */
317 memset(rombuf, 0, sizeof(rombuf));
318 romdata = (u_int8_t *) rombuf;
319 switch (sc->sc_chip) {
320 case TULIP_CHIP_21040:
321 TULIP_WRITE(sc, CSR_MIIROM, MIIROM_SROMCS);
322 for (i = 0; i < sizeof(rombuf); i++) {
323 for (j = 0; j < 10000; j++) {
324 val = TULIP_READ(sc, CSR_MIIROM);
325 if ((val & MIIROM_DN) == 0)
326 break;
327 }
328 romdata[i] = val & MIIROM_DATA;
329 }
330 break;
331
332 case TULIP_CHIP_82C168:
333 case TULIP_CHIP_82C169:
334 /*
335 * The Lite-On PNIC stores the Ethernet address in
336 * the first 3 words of the EEPROM. EEPROM access
337 * is not like the other Tulip chips.
338 */
339 for (i = 0; i < 3; i++) {
340 TULIP_WRITE(sc, CSR_PNIC_SROMCTL,
341 PNIC_SROMCTL_READ | i);
342 for (j = 0; j < 500; j++) {
343 delay(2);
344 val = TULIP_READ(sc, CSR_MIIROM);
345 if ((val & PNIC_MIIROM_BUSY) == 0)
346 break;
347 }
348 if (val & PNIC_MIIROM_BUSY) {
349 printf("%s: EEPROM timed out\n",
350 sc->sc_dev.dv_xname);
351 return;
352 }
353 rombuf[i] = bswap16(val & PNIC_MIIROM_DATA);
354 }
355 break;
356
357 default:
358 tlp_read_srom(sc, 0, sizeof(rombuf) >> 1, rombuf);
359 }
360
361 /*
362 * Deal with chip/board quirks. This includes setting up
363 * the mediasw, and extracting the Ethernet address from
364 * the rombuf.
365 *
366 * XXX Eventually, handle master/slave interrupts on the
367 * XXX multi-port boards which require that.
368 */
369 switch (sc->sc_chip) {
370 case TULIP_CHIP_82C168:
371 case TULIP_CHIP_82C169:
372 /*
373 * Lite-On PNIC's Ethernet address is the first 6
374 * bytes of its EEPROM.
375 */
376 memcpy(enaddr, romdata, ETHER_ADDR_LEN);
377
378 /*
379 * Lite-On PNICs always use the same mediasw; we
380 * select MII vs. internal NWAY automatically.
381 */
382 sc->sc_mediasw = &tlp_pnic_mediasw;
383 break;
384
385 case TULIP_CHIP_WB89C840F:
386 /*
387 * Winbond 89C840F's Ethernet address is the first
388 * 6 bytes of its EEPROM.
389 */
390 memcpy(enaddr, romdata, ETHER_ADDR_LEN);
391
392 /*
393 * Winbond 89C840F has an MII attached to the SIO.
394 */
395 sc->sc_mediasw = &tlp_sio_mii_mediasw;
396 break;
397
398 default:
399 printf("%s: sorry, unable to handle your board\n",
400 sc->sc_dev.dv_xname);
401 return;
402 }
403
404 /*
405 * Map and establish our interrupt.
406 */
407 if (pci_intr_map(pc, pa->pa_intrtag, pa->pa_intrpin,
408 pa->pa_intrline, &ih)) {
409 printf("%s: unable to map interrupt\n", sc->sc_dev.dv_xname);
410 return;
411 }
412 intrstr = pci_intr_string(pc, ih);
413 psc->sc_ih = pci_intr_establish(pc, ih, IPL_NET, tlp_intr, sc);
414 if (psc->sc_ih == NULL) {
415 printf("%s: unable to establish interrupt",
416 sc->sc_dev.dv_xname);
417 if (intrstr != NULL)
418 printf(" at %s", intrstr);
419 printf("\n");
420 return;
421 }
422 printf("%s: interrupting at %s\n", sc->sc_dev.dv_xname, intrstr);
423
424 /*
425 * Finish off the attach.
426 */
427 tlp_attach(sc, name, enaddr);
428 }
429