Home | History | Annotate | Line # | Download | only in isa
if_ep_isa.c revision 1.28
      1 /*	$NetBSD: if_ep_isa.c,v 1.28 2001/09/02 06:46:57 tsutsui Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 1996, 1997 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  * Copyright (c) 1997 Jonathan Stone <jonathan (at) NetBSD.org>
     42  * Copyright (c) 1994 Herb Peyerl <hpeyerl (at) beer.org>
     43  * All rights reserved.
     44  *
     45  * Redistribution and use in source and binary forms, with or without
     46  * modification, are permitted provided that the following conditions
     47  * are met:
     48  * 1. Redistributions of source code must retain the above copyright
     49  *    notice, this list of conditions and the following disclaimer.
     50  * 2. Redistributions in binary form must reproduce the above copyright
     51  *    notice, this list of conditions and the following disclaimer in the
     52  *    documentation and/or other materials provided with the distribution.
     53  * 3. All advertising materials mentioning features or use of this software
     54  *    must display the following acknowledgement:
     55  *      This product includes software developed by Herb Peyerl.
     56  * 4. The name of Herb Peyerl may not be used to endorse or promote products
     57  *    derived from this software without specific prior written permission.
     58  *
     59  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     60  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     61  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     62  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     63  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     64  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     65  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     66  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     67  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     68  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     69  */
     70 
     71 #include <sys/param.h>
     72 #include <sys/systm.h>
     73 #include <sys/mbuf.h>
     74 #include <sys/socket.h>
     75 #include <sys/ioctl.h>
     76 #include <sys/errno.h>
     77 #include <sys/syslog.h>
     78 #include <sys/select.h>
     79 #include <sys/device.h>
     80 #include <sys/queue.h>
     81 
     82 #include <net/if.h>
     83 #include <net/if_dl.h>
     84 #include <net/if_ether.h>
     85 #include <net/if_media.h>
     86 
     87 #include <machine/cpu.h>
     88 #include <machine/bus.h>
     89 #include <machine/intr.h>
     90 
     91 #include <dev/mii/miivar.h>
     92 
     93 #include <dev/ic/elink3var.h>
     94 #include <dev/ic/elink3reg.h>
     95 
     96 #include <dev/isa/isavar.h>
     97 #include <dev/isa/elink.h>
     98 
     99 int ep_isa_probe __P((struct device *, struct cfdata *, void *));
    100 void ep_isa_attach __P((struct device *, struct device *, void *));
    101 
    102 struct cfattach ep_isa_ca = {
    103 	sizeof(struct ep_softc), ep_isa_probe, ep_isa_attach
    104 };
    105 
    106 static	void epaddcard __P((int, int, int, int));
    107 
    108 /*
    109  * This keeps track of which ISAs have been through an ep probe sequence.
    110  * A simple static variable isn't enough, since it's conceivable that
    111  * a system might have more than one ISA bus.
    112  *
    113  * The "er_bus" member is the unit number of the parent ISA bus, e.g. "0"
    114  * for "isa0".
    115  */
    116 struct ep_isa_done_probe {
    117 	LIST_ENTRY(ep_isa_done_probe)	er_link;
    118 	int				er_bus;
    119 };
    120 static LIST_HEAD(, ep_isa_done_probe) ep_isa_all_probes;
    121 static int ep_isa_probes_initialized;
    122 
    123 #define MAXEPCARDS	20	/* if you have more than 20, you lose */
    124 
    125 static struct epcard {
    126 	int	bus;
    127 	int	iobase;
    128 	int	irq;
    129 	char	available;
    130 	long	model;
    131 } epcards[MAXEPCARDS];
    132 static int nepcards;
    133 
    134 static void
    135 epaddcard(bus, iobase, irq, model)
    136 	int bus, iobase, irq, model;
    137 {
    138 
    139 	if (nepcards >= MAXEPCARDS)
    140 		return;
    141 	epcards[nepcards].bus = bus;
    142 	epcards[nepcards].iobase = iobase;
    143 	epcards[nepcards].irq = (irq == 2) ? 9 : irq;
    144 	epcards[nepcards].model = model;
    145 	epcards[nepcards].available = 1;
    146 	nepcards++;
    147 }
    148 
    149 /*
    150  * 3c509 cards on the ISA bus are probed in ethernet address order.
    151  * The probe sequence requires careful orchestration, and we'd like
    152  * like to allow the irq and base address to be wildcarded. So, we
    153  * probe all the cards the first time epprobe() is called. On subsequent
    154  * calls we look for matching cards.
    155  */
    156 int
    157 ep_isa_probe(parent, match, aux)
    158 	struct device *parent;
    159 	struct cfdata *match;
    160 	void *aux;
    161 {
    162 	struct isa_attach_args *ia = aux;
    163 	bus_space_tag_t iot = ia->ia_iot;
    164 	bus_space_handle_t ioh;
    165 	int slot, iobase, irq, i;
    166 	u_int16_t vendor, model;
    167 	struct ep_isa_done_probe *er;
    168 	int bus = parent->dv_unit;
    169 
    170 	if (ep_isa_probes_initialized == 0) {
    171 		LIST_INIT(&ep_isa_all_probes);
    172 		ep_isa_probes_initialized = 1;
    173 	}
    174 
    175 	/*
    176 	 * Probe this bus if we haven't done so already.
    177 	 */
    178 	for (er = ep_isa_all_probes.lh_first; er != NULL;
    179 	    er = er->er_link.le_next)
    180 		if (er->er_bus == parent->dv_unit)
    181 			goto bus_probed;
    182 
    183 	/*
    184 	 * Mark this bus so we don't probe it again.
    185 	 */
    186 	er = (struct ep_isa_done_probe *)
    187 	    malloc(sizeof(struct ep_isa_done_probe), M_DEVBUF, M_NOWAIT);
    188 	if (er == NULL)
    189 		panic("ep_isa_probe: can't allocate state storage");
    190 
    191 	er->er_bus = bus;
    192 	LIST_INSERT_HEAD(&ep_isa_all_probes, er, er_link);
    193 
    194 	/*
    195 	 * Map the Etherlink ID port for the probe sequence.
    196 	 */
    197 	if (bus_space_map(iot, ELINK_ID_PORT, 1, 0, &ioh)) {
    198 		printf("ep_isa_probe: can't map Etherlink ID port\n");
    199 		return 0;
    200 	}
    201 
    202 	for (slot = 0; slot < MAXEPCARDS; slot++) {
    203 		elink_reset(iot, ioh, parent->dv_unit);
    204 		elink_idseq(iot, ioh, ELINK_509_POLY);
    205 
    206 		/* Untag all the adapters so they will talk to us. */
    207 		if (slot == 0)
    208 			bus_space_write_1(iot, ioh, 0, TAG_ADAPTER + 0);
    209 
    210 		vendor = bswap16(epreadeeprom(iot, ioh, EEPROM_MFG_ID));
    211 		if (vendor != MFG_ID)
    212 			continue;
    213 
    214 		model = bswap16(epreadeeprom(iot, ioh, EEPROM_PROD_ID));
    215 		/*
    216 		 * XXX: Add a new product id to check for other cards
    217 		 * (3c515?) and fix the check in ep_isa_attach.
    218 		 */
    219 		if ((model & 0xfff0) != PROD_ID_3C509) {
    220 #ifndef trusted
    221 			printf(
    222 			 "ep_isa_probe: ignoring model %04x\n", model);
    223 #endif
    224 			continue;
    225 			}
    226 
    227 		iobase = epreadeeprom(iot, ioh, EEPROM_ADDR_CFG);
    228 		iobase = (iobase & 0x1f) * 0x10 + 0x200;
    229 
    230 		irq = epreadeeprom(iot, ioh, EEPROM_RESOURCE_CFG);
    231 		irq >>= 12;
    232 
    233 		/* XXX Should ignore card if non-ISA(EISA) io address? -chb */
    234 
    235 		/*
    236 		 * Don't attach a 3c509 in PnP mode.
    237 		 * PnP mode was added with the 3C509B.
    238 		 * Check some EEPROM registers to make sure this is really
    239 		 * a 3C509B and test whether it is in PnP mode.
    240 		 */
    241 		if ((model & 0xfff0) == PROD_ID_3C509) {
    242 			u_int16_t cksum, eepromrev, eeprom_cap, eeprom_hi;
    243 
    244 
    245 			/*
    246 			 * Fetch all the info we need to ascertain whether
    247 			 * the card is  PnP capable and in PnP mode.
    248 			 * Skip over PnP cards.
    249 			 */
    250 
    251 			/* secondary configurable data checksum */
    252 			cksum = epreadeeprom(iot, ioh, EEPROM_CHECKSUM_EL3)
    253 			    & 0xFF;
    254 			for (i = EEPROM_CONFIG_HIGH;
    255 			    i < EEPROM_CHECKSUM_EL3; i++) {
    256 				cksum ^= epreadeeprom(iot, ioh, i);
    257 			}
    258 			cksum = (cksum & 0xFF) ^ ((cksum >> 8) & 0xFF);
    259 
    260 			eepromrev = epreadeeprom(iot, ioh, EEPROM_SSI);
    261 			eeprom_hi = epreadeeprom(iot, ioh, EEPROM_CONFIG_HIGH);
    262 			eeprom_cap = epreadeeprom(iot, ioh, EEPROM_CAP);
    263 
    264 			/*
    265 			 * Stop card responding to contention in future.
    266 			 * (NB: stops rsponse to all reads from ID port.)
    267 			 */
    268 			bus_space_write_1(iot, ioh, 0, TAG_ADAPTER + 1);
    269 
    270 			if (cksum != 0) {
    271 #if 0
    272 				printf("ep_isa_probe: cksum mismatch 0x%02x\n",
    273 				    (int)cksum);
    274 #endif
    275 			}
    276 			else if ((eepromrev & 0xF) < 1) {
    277 				/* 3C509B is adapter revision level 1. */
    278 #if 0
    279 				printf("ep_isa_probe revision level 0\n");
    280 #endif
    281 			}
    282 			else if (eeprom_cap != 0x2083) {
    283 #if 0
    284 				printf("ep_isa_probe: capabilities word mismatch0x%03x\n",
    285 				    (int)epreadeeprom(iot, ioh, EEPROM_CAP));
    286 #endif
    287 			}
    288 			else
    289 			  /*
    290 			   * we have a 3c509B with PnP capabilities.
    291 			   * Test partly documented bit which toggles when
    292 			   * in  PnP mode.
    293 			   */
    294 			if ((eeprom_hi & 8) != 0) {
    295 				printf("3COM 3C509B Ethernet card in PnP mode\n");
    296 				continue;
    297 			}
    298 		}
    299 
    300 		/*
    301 		 * XXX: this should probably not be done here
    302 		 * because it enables the drq/irq lines from
    303 		 * the board. Perhaps it should be done after
    304 		 * we have checked for irq/drq collisions?
    305 		 *
    306 		 * According to the 3COM docs, this does not enable
    307 		 * the irq lines. -chb
    308 		 */
    309 		bus_space_write_1(iot, ioh, 0, ACTIVATE_ADAPTER_TO_CONFIG);
    310 
    311 		epaddcard(bus, iobase, irq, model);
    312 	}
    313 	/* XXX should we sort by ethernet address? */
    314 
    315 	bus_space_unmap(iot, ioh, 1);
    316 
    317 bus_probed:
    318 
    319 	for (i = 0; i < nepcards; i++) {
    320 		if (epcards[i].bus != bus)
    321 			continue;
    322 		if (epcards[i].available == 0)
    323 			continue;
    324 		if (ia->ia_iobase != IOBASEUNK &&
    325 		    ia->ia_iobase != epcards[i].iobase)
    326 			continue;
    327 		if (ia->ia_irq != IRQUNK &&
    328 		    ia->ia_irq != epcards[i].irq)
    329 			continue;
    330 		goto good;
    331 	}
    332 	return 0;
    333 
    334 good:
    335 	epcards[i].available = 0;
    336 	ia->ia_iobase = epcards[i].iobase;
    337 	ia->ia_irq = epcards[i].irq;
    338 	ia->ia_iosize = 0x10;
    339 	ia->ia_msize = 0;
    340 	ia->ia_aux = (void *)epcards[i].model;
    341 	return 1;
    342 }
    343 
    344 void
    345 ep_isa_attach(parent, self, aux)
    346 	struct device *parent, *self;
    347 	void *aux;
    348 {
    349 	struct ep_softc *sc = (void *)self;
    350 	struct isa_attach_args *ia = aux;
    351 	bus_space_tag_t iot = ia->ia_iot;
    352 	bus_space_handle_t ioh;
    353 	int chipset;
    354 
    355 	/* Map i/o space. */
    356 	if (bus_space_map(iot, ia->ia_iobase, ia->ia_iosize, 0, &ioh)) {
    357 		printf(": can't map i/o space\n");
    358 		return;
    359 	}
    360 
    361 	sc->sc_iot = iot;
    362 	sc->sc_ioh = ioh;
    363 	sc->bustype = ELINK_BUS_ISA;
    364 
    365 	sc->enable = NULL;
    366 	sc->disable = NULL;
    367 	sc->enabled = 1;
    368 
    369 	chipset = (int)(long)ia->ia_aux;
    370 	if ((chipset & 0xfff0) == PROD_ID_3C509) {
    371 		printf(": 3Com 3C509 Ethernet\n");
    372 		epconfig(sc, ELINK_CHIPSET_3C509, NULL);
    373 	} else {
    374 		/*
    375 		 * XXX: Maybe a 3c515, but the check in ep_isa_probe looks
    376 		 * at the moment only for a 3c509.
    377 		 */
    378 		printf(": unknown 3Com Ethernet card: %04x\n", chipset);
    379 		return;
    380 	}
    381 
    382 	sc->sc_ih = isa_intr_establish(ia->ia_ic, ia->ia_irq, IST_EDGE,
    383 	    IPL_NET, epintr, sc);
    384 }
    385