1 /* $NetBSD: if_tlp_cardbus.c,v 1.72 2022/09/25 17:33:19 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 *
20 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
21 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
24 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 * POSSIBILITY OF SUCH DAMAGE.
31 */
32
33 /*
34 * CardBus bus front-end for the Digital Semiconductor ``Tulip'' (21x4x)
35 * Ethernet controller family driver.
36 */
37
38 #include <sys/cdefs.h>
39 __KERNEL_RCSID(0, "$NetBSD: if_tlp_cardbus.c,v 1.72 2022/09/25 17:33:19 thorpej Exp $");
40
41 #include "opt_inet.h"
42
43 #include <sys/param.h>
44 #include <sys/systm.h>
45 #include <sys/mbuf.h>
46 #include <sys/kernel.h>
47 #include <sys/socket.h>
48 #include <sys/ioctl.h>
49 #include <sys/errno.h>
50 #include <sys/device.h>
51
52 #include <machine/endian.h>
53
54 #include <net/if.h>
55 #include <net/if_dl.h>
56 #include <net/if_media.h>
57 #include <net/if_ether.h>
58
59 #ifdef INET
60 #include <netinet/in.h>
61 #include <netinet/if_inarp.h>
62 #endif
63
64
65 #include <sys/bus.h>
66 #include <sys/intr.h>
67
68 #include <dev/mii/miivar.h>
69 #include <dev/mii/mii_bitbang.h>
70
71 #include <dev/ic/tulipreg.h>
72 #include <dev/ic/tulipvar.h>
73
74 #include <dev/pci/pcivar.h>
75 #include <dev/pci/pcireg.h>
76 #include <dev/pci/pcidevs.h>
77
78 #include <dev/cardbus/cardbusvar.h>
79 #include <dev/pci/pcidevs.h>
80
81 /*
82 * PCI configuration space registers used by the Tulip.
83 */
84 #define TULIP_PCI_IOBA PCI_BAR(0) /* i/o mapped base */
85 #define TULIP_PCI_MMBA PCI_BAR(1) /* memory mapped base */
86 #define TULIP_PCI_CFDA 0x40 /* configuration driver area */
87
88 #define CFDA_SLEEP 0x80000000 /* sleep mode */
89 #define CFDA_SNOOZE 0x40000000 /* snooze mode */
90
91 struct tulip_cardbus_softc {
92 struct tulip_softc sc_tulip; /* real Tulip softc */
93
94 /* CardBus-specific goo. */
95 void *sc_ih; /* interrupt handle */
96 cardbus_devfunc_t sc_ct; /* our CardBus devfuncs */
97 pcitag_t sc_tag; /* our CardBus tag */
98 pcireg_t sc_csr;
99 bus_size_t sc_mapsize; /* the size of mapped bus space
100 region */
101
102 int sc_bar_reg; /* which BAR to use */
103 pcireg_t sc_bar_val; /* value of the BAR */
104 };
105
106 int tlp_cardbus_match(device_t, cfdata_t, void *);
107 void tlp_cardbus_attach(device_t, device_t, void *);
108 int tlp_cardbus_detach(device_t, int);
109
110 CFATTACH_DECL_NEW(tlp_cardbus, sizeof(struct tulip_cardbus_softc),
111 tlp_cardbus_match, tlp_cardbus_attach, tlp_cardbus_detach, tlp_activate);
112
113 const struct tulip_cardbus_product {
114 u_int32_t tcp_vendor; /* PCI vendor ID */
115 u_int32_t tcp_product; /* PCI product ID */
116 tulip_chip_t tcp_chip; /* base Tulip chip type */
117 } tlp_cardbus_products[] = {
118 { PCI_VENDOR_DEC, PCI_PRODUCT_DEC_21142,
119 TULIP_CHIP_21142 },
120
121 { PCI_VENDOR_XIRCOM, PCI_PRODUCT_XIRCOM_X3201_3_21143,
122 TULIP_CHIP_X3201_3 },
123
124 { PCI_VENDOR_ADMTEK, PCI_PRODUCT_ADMTEK_AN983,
125 TULIP_CHIP_AN985 },
126
127 { PCI_VENDOR_ACCTON, PCI_PRODUCT_ACCTON_EN2242,
128 TULIP_CHIP_AN985 },
129
130 { PCI_VENDOR_ABOCOM, PCI_PRODUCT_ABOCOM_FE2500,
131 TULIP_CHIP_AN985 },
132
133 { PCI_VENDOR_ABOCOM, PCI_PRODUCT_ABOCOM_PCM200,
134 TULIP_CHIP_AN985 },
135
136 { PCI_VENDOR_ABOCOM, PCI_PRODUCT_ABOCOM_FE2500MX,
137 TULIP_CHIP_AN985 },
138
139 { PCI_VENDOR_HAWKING, PCI_PRODUCT_HAWKING_PN672TX,
140 TULIP_CHIP_AN985 },
141
142 { PCI_VENDOR_ADMTEK, PCI_PRODUCT_ADMTEK_AN985,
143 TULIP_CHIP_AN985 },
144
145 { PCI_VENDOR_MICROSOFT, PCI_PRODUCT_MICROSOFT_MN120,
146 TULIP_CHIP_AN985 },
147
148 { PCI_VENDOR_LINKSYS, PCI_PRODUCT_LINKSYS_PCMPC200,
149 TULIP_CHIP_AN985 },
150
151 { 0, 0,
152 TULIP_CHIP_INVALID },
153 };
154
155 struct tlp_cardbus_quirks {
156 void (*tpq_func)(struct tulip_cardbus_softc *,
157 const u_int8_t *);
158 u_int8_t tpq_oui[3];
159 };
160
161 void tlp_cardbus_lxt_quirks(struct tulip_cardbus_softc *,
162 const u_int8_t *);
163
164 const struct tlp_cardbus_quirks tlp_cardbus_21142_quirks[] = {
165 { tlp_cardbus_lxt_quirks, { 0x00, 0x40, 0x05 } },
166 { NULL, { 0, 0, 0 } }
167 };
168
169 void tlp_cardbus_setup(struct tulip_cardbus_softc *);
170
171 int tlp_cardbus_enable(struct tulip_softc *);
172 void tlp_cardbus_disable(struct tulip_softc *);
173 void tlp_cardbus_power(struct tulip_softc *, int);
174
175 void tlp_cardbus_x3201_reset(struct tulip_softc *);
176
177 const struct tulip_cardbus_product *tlp_cardbus_lookup
178 (const struct cardbus_attach_args *);
179 void tlp_cardbus_get_quirks(struct tulip_cardbus_softc *,
180 const u_int8_t *, const struct tlp_cardbus_quirks *);
181
182 const struct tulip_cardbus_product *
tlp_cardbus_lookup(const struct cardbus_attach_args * ca)183 tlp_cardbus_lookup(const struct cardbus_attach_args *ca)
184 {
185 const struct tulip_cardbus_product *tcp;
186
187 for (tcp = tlp_cardbus_products; tcp->tcp_chip != TULIP_CHIP_INVALID;
188 tcp++) {
189 if (PCI_VENDOR(ca->ca_id) == tcp->tcp_vendor &&
190 PCI_PRODUCT(ca->ca_id) == tcp->tcp_product)
191 return (tcp);
192 }
193 return (NULL);
194 }
195
196 void
tlp_cardbus_get_quirks(struct tulip_cardbus_softc * csc,const u_int8_t * enaddr,const struct tlp_cardbus_quirks * tpq)197 tlp_cardbus_get_quirks(struct tulip_cardbus_softc *csc, const u_int8_t *enaddr,
198 const struct tlp_cardbus_quirks *tpq)
199 {
200
201 for (; tpq->tpq_func != NULL; tpq++) {
202 if (tpq->tpq_oui[0] == enaddr[0] &&
203 tpq->tpq_oui[1] == enaddr[1] &&
204 tpq->tpq_oui[2] == enaddr[2]) {
205 (*tpq->tpq_func)(csc, enaddr);
206 return;
207 }
208 }
209 }
210
211 int
tlp_cardbus_match(device_t parent,cfdata_t match,void * aux)212 tlp_cardbus_match(device_t parent, cfdata_t match, void *aux)
213 {
214 struct cardbus_attach_args *ca = aux;
215
216 if (tlp_cardbus_lookup(ca) != NULL)
217 return (1);
218
219 return (0);
220 }
221
222 void
tlp_cardbus_attach(device_t parent,device_t self,void * aux)223 tlp_cardbus_attach(device_t parent, device_t self, void *aux)
224 {
225 struct tulip_cardbus_softc *csc = device_private(self);
226 struct tulip_softc *sc = &csc->sc_tulip;
227 struct cardbus_attach_args *ca = aux;
228 cardbus_devfunc_t ct = ca->ca_ct;
229 const struct tulip_cardbus_product *tcp;
230 u_int8_t enaddr[ETHER_ADDR_LEN];
231 bus_addr_t adr;
232 pcireg_t reg;
233
234 sc->sc_dev = self;
235 sc->sc_devno = 0;
236 sc->sc_dmat = ca->ca_dmat;
237 csc->sc_ct = ct;
238 csc->sc_tag = ca->ca_tag;
239
240 tcp = tlp_cardbus_lookup(ca);
241 if (tcp == NULL) {
242 printf("\n");
243 panic("tlp_cardbus_attach: impossible");
244 }
245 sc->sc_chip = tcp->tcp_chip;
246
247 /*
248 * By default, Tulip registers are 8 bytes long (4 bytes
249 * followed by a 4 byte pad).
250 */
251 sc->sc_regshift = 3;
252
253 /*
254 * Power management hooks.
255 */
256 sc->sc_enable = tlp_cardbus_enable;
257 sc->sc_disable = tlp_cardbus_disable;
258 sc->sc_power = tlp_cardbus_power;
259
260 /*
261 * Get revision info, and set some chip-specific variables.
262 */
263 sc->sc_rev = PCI_REVISION(ca->ca_class);
264 switch (sc->sc_chip) {
265 case TULIP_CHIP_21142:
266 if (sc->sc_rev >= 0x20)
267 sc->sc_chip = TULIP_CHIP_21143;
268 break;
269
270 case TULIP_CHIP_AN985:
271 /*
272 * The AN983 and AN985 are very similar, and are
273 * differentiated by a "signature" register that
274 * is like, but not identical, to a PCI ID register.
275 */
276 reg = Cardbus_conf_read(ct, csc->sc_tag, 0x80);
277 switch (reg) {
278 case 0x09811317:
279 sc->sc_chip = TULIP_CHIP_AN985;
280 break;
281
282 case 0x09851317:
283 sc->sc_chip = TULIP_CHIP_AN983;
284 break;
285
286 }
287 break;
288
289 default:
290 /* Nothing. -- to make gcc happy */
291 break;
292 }
293
294 aprint_normal(": %s Ethernet, pass %d.%d\n",
295 tlp_chip_name(sc->sc_chip),
296 (sc->sc_rev >> 4) & 0xf, sc->sc_rev & 0xf);
297
298 /*
299 * Map the device.
300 */
301 csc->sc_csr = PCI_COMMAND_MASTER_ENABLE;
302 if (Cardbus_mapreg_map(ct, TULIP_PCI_MMBA,
303 PCI_MAPREG_TYPE_MEM, 0, &sc->sc_st, &sc->sc_sh, &adr,
304 &csc->sc_mapsize) == 0) {
305 csc->sc_csr |= PCI_COMMAND_MEM_ENABLE;
306 csc->sc_bar_reg = TULIP_PCI_MMBA;
307 csc->sc_bar_val = adr | PCI_MAPREG_TYPE_MEM;
308 } else if (Cardbus_mapreg_map(ct, TULIP_PCI_IOBA,
309 PCI_MAPREG_TYPE_IO, 0, &sc->sc_st, &sc->sc_sh, &adr,
310 &csc->sc_mapsize) == 0) {
311 csc->sc_csr |= PCI_COMMAND_IO_ENABLE;
312 csc->sc_bar_reg = TULIP_PCI_IOBA;
313 csc->sc_bar_val = adr | PCI_MAPREG_TYPE_IO;
314 } else {
315 aprint_error_dev(self, "unable to map device registers\n");
316 return;
317 }
318
319 /*
320 * Bring the chip out of powersave mode and initialize the
321 * configuration registers.
322 */
323 tlp_cardbus_setup(csc);
324
325 /*
326 * Read the contents of the Ethernet Address ROM/SROM.
327 */
328 switch (sc->sc_chip) {
329 case TULIP_CHIP_X3201_3:
330 /*
331 * No SROM on this chip.
332 */
333 break;
334
335 default:
336 if (tlp_read_srom(sc) == 0)
337 goto cant_cope;
338 break;
339 }
340
341 /*
342 * Deal with chip/board quirks. This includes setting up
343 * the mediasw, and extracting the Ethernet address from
344 * the rombuf.
345 */
346 switch (sc->sc_chip) {
347 case TULIP_CHIP_21142:
348 case TULIP_CHIP_21143:
349 /* Check for new format SROM. */
350 if (tlp_isv_srom_enaddr(sc, enaddr) != 0) {
351 /*
352 * We start out with the 2114x ISV media switch.
353 * When we search for quirks, we may change to
354 * a different switch.
355 */
356 sc->sc_mediasw = &tlp_2114x_isv_mediasw;
357 } else if (tlp_parse_old_srom(sc, enaddr) == 0) {
358 /*
359 * Not an ISV SROM, and not in old DEC Address
360 * ROM format. Try to snarf it out of the CIS.
361 */
362 if (ca->ca_cis.funce.network.netid_present == 0)
363 goto cant_cope;
364
365 /* Grab the MAC address from the CIS. */
366 memcpy(enaddr, ca->ca_cis.funce.network.netid,
367 sizeof(enaddr));
368 }
369
370 /*
371 * Deal with any quirks this board might have.
372 */
373 tlp_cardbus_get_quirks(csc, enaddr, tlp_cardbus_21142_quirks);
374
375 /*
376 * If we don't already have a media switch, default to
377 * MII-over-SIO, with no special reset routine.
378 */
379 if (sc->sc_mediasw == NULL) {
380 aprint_normal("%s: defaulting to MII-over-SIO; "
381 "no bets...\n", device_xname(self));
382 sc->sc_mediasw = &tlp_sio_mii_mediasw;
383 }
384 break;
385
386 case TULIP_CHIP_AN983:
387 case TULIP_CHIP_AN985:
388 /*
389 * The ADMtek AN985's Ethernet address is located
390 * at offset 8 of its EEPROM.
391 */
392 memcpy(enaddr, &sc->sc_srom[8], ETHER_ADDR_LEN);
393
394 /*
395 * The ADMtek AN985 can be configured in Single-Chip
396 * mode or MAC-only mode. Single-Chip uses the built-in
397 * PHY, MAC-only has an external PHY (usually HomePNA).
398 * The selection is based on an EEPROM setting, and both
399 * PHYs are access via MII attached to SIO.
400 *
401 * The AN985 "ghosts" the internal PHY onto all
402 * MII addresses, so we have to use a media init
403 * routine that limits the search.
404 * XXX How does this work with MAC-only mode?
405 */
406 sc->sc_mediasw = &tlp_an985_mediasw;
407 break;
408
409 case TULIP_CHIP_X3201_3:
410 /*
411 * The X3201 doesn't have an SROM. Lift the MAC address
412 * from the CIS. Also, we have a special media switch:
413 * MII-on-SIO, plus some special GPIO setup.
414 */
415 memcpy(enaddr, ca->ca_cis.funce.network.netid, sizeof(enaddr));
416 sc->sc_reset = tlp_cardbus_x3201_reset;
417 sc->sc_mediasw = &tlp_sio_mii_mediasw;
418 break;
419
420 default:
421 cant_cope:
422 aprint_error_dev(self, "sorry, unable to handle your board\n");
423 return;
424 }
425
426 /*
427 * Finish off the attach.
428 */
429 tlp_attach(sc, enaddr);
430
431 /*
432 * Power down the socket.
433 */
434 Cardbus_function_disable(csc->sc_ct);
435 }
436
437 int
tlp_cardbus_detach(device_t self,int flags)438 tlp_cardbus_detach(device_t self, int flags)
439 {
440 struct tulip_cardbus_softc *csc = device_private(self);
441 struct tulip_softc *sc = &csc->sc_tulip;
442 struct cardbus_devfunc *ct = csc->sc_ct;
443 int rv;
444
445 #if defined(DIAGNOSTIC)
446 if (ct == NULL)
447 panic("%s: data structure lacks", device_xname(self));
448 #endif
449
450 rv = tlp_detach(sc);
451 if (rv)
452 return (rv);
453
454 /*
455 * Unhook the interrupt handler.
456 */
457 if (csc->sc_ih != NULL)
458 Cardbus_intr_disestablish(ct, csc->sc_ih);
459
460 /*
461 * Release bus space and close window.
462 */
463 if (csc->sc_bar_reg != 0)
464 Cardbus_mapreg_unmap(ct, csc->sc_bar_reg,
465 sc->sc_st, sc->sc_sh, csc->sc_mapsize);
466
467 return (0);
468 }
469
470 int
tlp_cardbus_enable(struct tulip_softc * sc)471 tlp_cardbus_enable(struct tulip_softc *sc)
472 {
473 struct tulip_cardbus_softc *csc = (void *) sc;
474 cardbus_devfunc_t ct = csc->sc_ct;
475
476 /*
477 * Power on the socket.
478 */
479 Cardbus_function_enable(ct);
480
481 /*
482 * Set up the PCI configuration registers.
483 */
484 tlp_cardbus_setup(csc);
485
486 /*
487 * Map and establish the interrupt.
488 */
489 csc->sc_ih = Cardbus_intr_establish(ct, IPL_NET, tlp_intr, sc);
490 if (csc->sc_ih == NULL) {
491 aprint_error_dev(sc->sc_dev,
492 "unable to establish interrupt\n");
493 Cardbus_function_disable(csc->sc_ct);
494 return (1);
495 }
496 return (0);
497 }
498
499 void
tlp_cardbus_disable(struct tulip_softc * sc)500 tlp_cardbus_disable(struct tulip_softc *sc)
501 {
502 struct tulip_cardbus_softc *csc = (void *) sc;
503 cardbus_devfunc_t ct = csc->sc_ct;
504
505 /* Unhook the interrupt handler. */
506 Cardbus_intr_disestablish(ct, csc->sc_ih);
507 csc->sc_ih = NULL;
508
509 /* Power down the socket. */
510 Cardbus_function_disable(ct);
511 }
512
513 void
tlp_cardbus_power(struct tulip_softc * sc,int why)514 tlp_cardbus_power(struct tulip_softc *sc, int why)
515 {
516
517 switch (why) {
518 case PWR_RESUME:
519 tlp_cardbus_enable(sc);
520 break;
521 case PWR_SUSPEND:
522 tlp_cardbus_disable(sc);
523 break;
524 }
525 }
526
527 void
tlp_cardbus_setup(struct tulip_cardbus_softc * csc)528 tlp_cardbus_setup(struct tulip_cardbus_softc *csc)
529 {
530 struct tulip_softc *sc = &csc->sc_tulip;
531 cardbus_devfunc_t ct = csc->sc_ct;
532 pcireg_t reg;
533
534 /*
535 * Check to see if the device is in power-save mode, and
536 * bring it out if necessary.
537 */
538 switch (sc->sc_chip) {
539 case TULIP_CHIP_21142:
540 case TULIP_CHIP_21143:
541 case TULIP_CHIP_X3201_3:
542 /*
543 * Clear the "sleep mode" bit in the CFDA register.
544 */
545 reg = Cardbus_conf_read(ct, csc->sc_tag, TULIP_PCI_CFDA);
546 if (reg & (CFDA_SLEEP|CFDA_SNOOZE))
547 Cardbus_conf_write(ct, csc->sc_tag, TULIP_PCI_CFDA,
548 reg & ~(CFDA_SLEEP|CFDA_SNOOZE));
549 break;
550
551 default:
552 /* Nothing. -- to make gcc happy */
553 break;
554 }
555
556 (void)cardbus_set_powerstate(ct, csc->sc_tag, PCI_PWR_D0);
557
558 /* Program the BAR. */
559 Cardbus_conf_write(ct, csc->sc_tag, csc->sc_bar_reg, csc->sc_bar_val);
560
561 /* Enable the appropriate bits in the PCI CSR. */
562 reg = Cardbus_conf_read(ct, csc->sc_tag, PCI_COMMAND_STATUS_REG);
563 reg &= ~(PCI_COMMAND_IO_ENABLE|PCI_COMMAND_MEM_ENABLE);
564 reg |= csc->sc_csr;
565 Cardbus_conf_write(ct, csc->sc_tag, PCI_COMMAND_STATUS_REG, reg);
566
567 /*
568 * Make sure the latency timer is set to some reasonable
569 * value.
570 */
571 reg = Cardbus_conf_read(ct, csc->sc_tag, PCI_BHLC_REG);
572 if (PCI_LATTIMER(reg) < 0x20) {
573 reg &= ~(PCI_LATTIMER_MASK << PCI_LATTIMER_SHIFT);
574 reg |= (0x20 << PCI_LATTIMER_SHIFT);
575 Cardbus_conf_write(ct, csc->sc_tag, PCI_BHLC_REG, reg);
576 }
577 }
578
579 void
tlp_cardbus_x3201_reset(struct tulip_softc * sc)580 tlp_cardbus_x3201_reset(struct tulip_softc *sc)
581 {
582 u_int32_t reg;
583
584 reg = TULIP_READ(sc, CSR_SIAGEN);
585
586 /* make GP[2,0] outputs */
587 TULIP_WRITE(sc, CSR_SIAGEN, (reg & ~SIAGEN_MD) | SIAGEN_CWE |
588 0x00050000);
589 TULIP_WRITE(sc, CSR_SIAGEN, (reg & ~SIAGEN_CWE) | SIAGEN_MD);
590 }
591
592 void
tlp_cardbus_lxt_quirks(struct tulip_cardbus_softc * csc,const u_int8_t * enaddr)593 tlp_cardbus_lxt_quirks(struct tulip_cardbus_softc *csc, const u_int8_t *enaddr)
594 {
595 struct tulip_softc *sc = &csc->sc_tulip;
596
597 sc->sc_mediasw = &tlp_sio_mii_mediasw;
598 }
599