Home | History | Annotate | Line # | Download | only in isa
if_ate.c revision 1.21
      1 /*	$NetBSD: if_ate.c,v 1.21 1998/03/22 04:25:37 enami Exp $	*/
      2 
      3 /*
      4  * All Rights Reserved, Copyright (C) Fujitsu Limited 1995
      5  *
      6  * This software may be used, modified, copied, distributed, and sold, in
      7  * both source and binary form provided that the above copyright, these
      8  * terms and the following disclaimer are retained.  The name of the author
      9  * and/or the contributor may not be used to endorse or promote products
     10  * derived from this software without specific prior written permission.
     11  *
     12  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND THE CONTRIBUTOR ``AS IS'' AND
     13  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     14  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     15  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR THE CONTRIBUTOR BE LIABLE
     16  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     17  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     18  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION.
     19  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     20  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     21  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     22  * SUCH DAMAGE.
     23  */
     24 
     25 /*
     26  * Portions copyright (C) 1993, David Greenman.  This software may be used,
     27  * modified, copied, distributed, and sold, in both source and binary form
     28  * provided that the above copyright and these terms are retained.  Under no
     29  * circumstances is the author responsible for the proper functioning of this
     30  * software, nor does the author assume any responsibility for damages
     31  * incurred with its use.
     32  */
     33 
     34 #include <sys/param.h>
     35 #include <sys/systm.h>
     36 #include <sys/device.h>
     37 #include <sys/socket.h>
     38 #include <sys/syslog.h>
     39 
     40 #include <net/if.h>
     41 #include <net/if_ether.h>
     42 #include <net/if_media.h>
     43 
     44 #include <machine/bus.h>
     45 #include <machine/intr.h>
     46 
     47 #include <dev/ic/mb86960reg.h>
     48 #include <dev/ic/mb86960var.h>
     49 
     50 #include <dev/isa/isavar.h>
     51 #include <dev/isa/if_fereg.h>	/* XXX */
     52 
     53 #ifdef __BROKEN_INDIRECT_CONFIG
     54 int	ate_match __P((struct device *, void *, void *));
     55 #else
     56 int	ate_match __P((struct device *, struct cfdata *, void *));
     57 #endif
     58 void	ate_attach __P((struct device *, struct device *, void *));
     59 
     60 struct ate_softc {
     61 	struct	mb86960_softc sc_mb86960;	/* real "mb86960" softc */
     62 
     63 	/* ISA-specific goo. */
     64 	void	*sc_ih;				/* interrupt cookie */
     65 };
     66 
     67 struct cfattach ate_ca = {
     68 	sizeof(struct ate_softc), ate_match, ate_attach
     69 };
     70 
     71 #if NetBSD <= 199712
     72 struct cfdriver ate_cd = {
     73 	NULL, "ate", DV_IFNET
     74 };
     75 #endif
     76 
     77 struct fe_simple_probe_struct {
     78 	u_char port;	/* Offset from the base I/O address. */
     79 	u_char mask;	/* Bits to be checked. */
     80 	u_char bits;	/* Values to be compared against. */
     81 };
     82 
     83 static __inline__ int fe_simple_probe __P((bus_space_tag_t,
     84     bus_space_handle_t, struct fe_simple_probe_struct const *));
     85 static __inline__ void ate_strobe __P((bus_space_tag_t, bus_space_handle_t));
     86 static void ate_read_eeprom __P((bus_space_tag_t, bus_space_handle_t,
     87     u_char *));
     88 static int ate_find __P((bus_space_tag_t, bus_space_handle_t, int *,
     89     int *));
     90 static int ate_detect __P((bus_space_tag_t, bus_space_handle_t,
     91     u_int8_t enaddr[ETHER_ADDR_LEN]));
     92 
     93 static int const ate_iomap[8] = {
     94 	0x260, 0x280, 0x2A0, 0x240, 0x340, 0x320, 0x380, 0x300
     95 };
     96 #define NATE_IOMAP (sizeof (ate_iomap) / sizeof (ate_iomap[0]))
     97 #define ATE_NPORTS 0x20
     98 
     99 /*
    100  * Hardware probe routines.
    101  */
    102 
    103 /*
    104  * Determine if the device is present.
    105  */
    106 int
    107 ate_match(parent, match, aux)
    108 	struct device *parent;
    109 #ifdef __BROKEN_INDIRECT_CONFIG
    110 	void *match;
    111 #else
    112 	struct cfdata *match;
    113 #endif
    114 	void *aux;
    115 {
    116 	struct isa_attach_args *ia = aux;
    117 	bus_space_tag_t iot = ia->ia_iot;
    118 	bus_space_handle_t ioh;
    119 	int i, iobase, irq, rv = 0;
    120 	u_int8_t myea[ETHER_ADDR_LEN];
    121 
    122 	/* Disallow wildcarded values. */
    123 	if (ia->ia_iobase == ISACF_PORT_DEFAULT)
    124 		return (0);
    125 
    126 	/*
    127 	 * See if the sepcified address is valid for MB86965A JLI mode.
    128 	 */
    129 	for (i = 0; i < NATE_IOMAP; i++)
    130 		if (ate_iomap[i] == ia->ia_iobase)
    131 			break;
    132 	if (i == NATE_IOMAP) {
    133 #ifdef DIAGNOSTIC
    134 		printf("ate_match: unknown iobase 0x%x\n", ia->ia_iobase);
    135 #endif
    136 		return (0);
    137 	}
    138 
    139 	/* Map i/o space. */
    140 	if (bus_space_map(iot, ia->ia_iobase, ATE_NPORTS, 0, &ioh)) {
    141 #ifdef DIAGNOSTIC
    142 		printf("ate_match: couldn't map iospace 0x%x\n",
    143 		    ia->ia_iobase);
    144 #endif
    145 		return (0);
    146 	}
    147 
    148 	if (ate_find(iot, ioh, &iobase, &irq) == 0) {
    149 #ifdef DIAGNOSTIC
    150 		printf("ate_match: ate_find failed\n");
    151 #endif
    152 		goto out;
    153 	}
    154 
    155 	if (iobase != ia->ia_iobase) {
    156 #ifdef DIAGNOSTIC
    157 		printf("ate_match: unexpected iobase in board: 0x%x\n",
    158 		    ia->ia_iobase);
    159 #endif
    160 		goto out;
    161 	}
    162 
    163 	if (ate_detect(iot, ioh, myea) == 0) { /* XXX necessary? */
    164 #ifdef DIAGNOSTIC
    165 		printf("ate_match: ate_detect failed\n");
    166 #endif
    167 		goto out;
    168 	}
    169 
    170 	if (ia->ia_irq != ISACF_IRQ_DEFAULT) {
    171 		if (ia->ia_irq != irq) {
    172 			printf("ate_match: irq mismatch; "
    173 			    "kernel configured %d != board configured %d\n",
    174 			    ia->ia_irq, irq);
    175 			goto out;
    176 		}
    177 	} else
    178 		ia->ia_irq = irq;
    179 
    180 	ia->ia_iosize = ATE_NPORTS;
    181 	ia->ia_msize = 0;
    182 	rv = 1;
    183 
    184  out:
    185 	bus_space_unmap(iot, ioh, ATE_NPORTS);
    186 	return (rv);
    187 }
    188 
    189 /*
    190  * Check for specific bits in specific registers have specific values.
    191  */
    192 static __inline__ int
    193 fe_simple_probe (iot, ioh, sp)
    194 	bus_space_tag_t iot;
    195 	bus_space_handle_t ioh;
    196 	struct fe_simple_probe_struct const *sp;
    197 {
    198 	u_int8_t val;
    199 	struct fe_simple_probe_struct const *p;
    200 
    201 	for (p = sp; p->mask != 0; p++) {
    202 		val = bus_space_read_1(iot, ioh, p->port);
    203 		if ((val & p->mask) != p->bits) {
    204 #ifdef DIAGNOSTIC
    205 			printf("fe_simple_probe: %x & %x != %x\n",
    206 			    val, p->mask, p->bits);
    207 #endif
    208 			return (0);
    209 		}
    210 	}
    211 
    212 	return (1);
    213 }
    214 
    215 /*
    216  * Routines to read all bytes from the config EEPROM through MB86965A.
    217  * I'm not sure what exactly I'm doing here...  I was told just to follow
    218  * the steps, and it worked.  Could someone tell me why the following
    219  * code works?  (Or, why all similar codes I tried previously doesn't
    220  * work.)  FIXME.
    221  */
    222 
    223 static __inline__ void
    224 ate_strobe (iot, ioh)
    225 	bus_space_tag_t iot;
    226 	bus_space_handle_t ioh;
    227 {
    228 
    229 	/*
    230 	 * Output same value twice.  To speed-down execution?
    231 	 */
    232 	bus_space_write_1(iot, ioh, FE_BMPR16, FE_B16_SELECT);
    233 	bus_space_write_1(iot, ioh, FE_BMPR16, FE_B16_SELECT);
    234 	bus_space_write_1(iot, ioh, FE_BMPR16, FE_B16_SELECT | FE_B16_CLOCK);
    235 	bus_space_write_1(iot, ioh, FE_BMPR16, FE_B16_SELECT | FE_B16_CLOCK);
    236 	bus_space_write_1(iot, ioh, FE_BMPR16, FE_B16_SELECT);
    237 	bus_space_write_1(iot, ioh, FE_BMPR16, FE_B16_SELECT);
    238 }
    239 
    240 static void
    241 ate_read_eeprom(iot, ioh, data)
    242 	bus_space_tag_t iot;
    243 	bus_space_handle_t ioh;
    244 	u_char *data;
    245 {
    246 	int n, count;
    247 	u_char val, bit;
    248 
    249 	/* Read bytes from EEPROM; two bytes per an iterration. */
    250 	for (n = 0; n < FE_EEPROM_SIZE / 2; n++) {
    251 		/* Reset the EEPROM interface. */
    252 		bus_space_write_1(iot, ioh, FE_BMPR16, 0x00);
    253 		bus_space_write_1(iot, ioh, FE_BMPR17, 0x00);
    254 		bus_space_write_1(iot, ioh, FE_BMPR16, FE_B16_SELECT);
    255 
    256 		/* Start EEPROM access. */
    257 		bus_space_write_1(iot, ioh, FE_BMPR17, FE_B17_DATA);
    258 		ate_strobe(iot, ioh);
    259 
    260 		/* Pass the iterration count to the chip. */
    261 		count = 0x80 | n;
    262 		for (bit = 0x80; bit != 0x00; bit >>= 1) {
    263 			bus_space_write_1(iot, ioh, FE_BMPR17,
    264 			    (count & bit) ? FE_B17_DATA : 0);
    265 			ate_strobe(iot, ioh);
    266 		}
    267 		bus_space_write_1(iot, ioh, FE_BMPR17, 0x00);
    268 
    269 		/* Read a byte. */
    270 		val = 0;
    271 		for (bit = 0x80; bit != 0x00; bit >>= 1) {
    272 			ate_strobe(iot, ioh);
    273 			if (bus_space_read_1(iot, ioh, FE_BMPR17) &
    274 			    FE_B17_DATA)
    275 				val |= bit;
    276 		}
    277 		*data++ = val;
    278 
    279 		/* Read one more byte. */
    280 		val = 0;
    281 		for (bit = 0x80; bit != 0x00; bit >>= 1) {
    282 			ate_strobe(iot, ioh);
    283 			if (bus_space_read_1(iot, ioh, FE_BMPR17) &
    284 			    FE_B17_DATA)
    285 				val |= bit;
    286 		}
    287 		*data++ = val;
    288 	}
    289 
    290 	/* Make sure the EEPROM is turned off. */
    291 	bus_space_write_1(iot, ioh, FE_BMPR16, 0);
    292 	bus_space_write_1(iot, ioh, FE_BMPR17, 0);
    293 
    294 #if FE_DEBUG >= 3
    295 	/* Report what we got. */
    296 	data -= FE_EEPROM_SIZE;
    297 	log(LOG_INFO, "ate_read_eeprom: EEPROM at %04x:"
    298 	    " %02x%02x%02x%02x %02x%02x%02x%02x -"
    299 	    " %02x%02x%02x%02x %02x%02x%02x%02x -"
    300 	    " %02x%02x%02x%02x %02x%02x%02x%02x -"
    301 	    " %02x%02x%02x%02x %02x%02x%02x%02x\n",
    302 	    (int) ioh,		/* XXX */
    303 	    data[ 0], data[ 1], data[ 2], data[ 3],
    304 	    data[ 4], data[ 5], data[ 6], data[ 7],
    305 	    data[ 8], data[ 9], data[10], data[11],
    306 	    data[12], data[13], data[14], data[15],
    307 	    data[16], data[17], data[18], data[19],
    308 	    data[20], data[21], data[22], data[23],
    309 	    data[24], data[25], data[26], data[27],
    310 	    data[28], data[29], data[30], data[31]);
    311 #endif
    312 }
    313 
    314 /*
    315  * Hardware (vendor) specific probe routines.
    316  */
    317 
    318 /*
    319  * Probe and initialization for Allied-Telesis AT1700/RE2000 series.
    320  */
    321 static int
    322 ate_find(iot, ioh, iobase, irq)
    323 	bus_space_tag_t iot;
    324 	bus_space_handle_t ioh;
    325 	int *iobase, *irq;
    326 {
    327 	u_char eeprom[FE_EEPROM_SIZE];
    328 	int n;
    329 
    330 	static int const irqmap[4][4] = {
    331 		{  3,  4,  5,  9 },
    332 		{ 10, 11, 12, 15 },
    333 		{  3, 11,  5, 15 },
    334 		{ 10, 11, 14, 15 },
    335 	};
    336 	static struct fe_simple_probe_struct const probe_table[] = {
    337 		{ FE_DLCR2,  0x70, 0x00 },
    338 		{ FE_DLCR4,  0x08, 0x00 },
    339 		{ FE_DLCR5,  0x80, 0x00 },
    340 #if 0
    341 		{ FE_BMPR16, 0x1B, 0x00 },
    342 		{ FE_BMPR17, 0x7F, 0x00 },
    343 #endif
    344 		{ 0 }
    345 	};
    346 
    347 #if FE_DEBUG >= 4
    348 	log(LOG_INFO, "ate_find: probe (0x%x) for ATI\n", iobase);
    349 #if 0
    350 	fe_dump(LOG_INFO, sc);
    351 #endif
    352 #endif
    353 
    354 	/*
    355 	 * We should test if MB86965A is on the base address now.
    356 	 * Unfortunately, it is very hard to probe it reliably, since
    357 	 * we have no way to reset the chip under software control.
    358 	 * On cold boot, we could check the "signature" bit patterns
    359 	 * described in the Fujitsu document.  On warm boot, however,
    360 	 * we can predict almost nothing about register values.
    361 	 */
    362 	if (!fe_simple_probe(iot, ioh, probe_table))
    363 		return (0);
    364 
    365 	/* Check if our I/O address matches config info on 86965. */
    366 	n = (bus_space_read_1(iot, ioh, FE_BMPR19) & FE_B19_ADDR)
    367 	    >> FE_B19_ADDR_SHIFT;
    368 	*iobase = ate_iomap[n];
    369 
    370 	/*
    371 	 * We are now almost sure we have an AT1700 at the given
    372 	 * address.  So, read EEPROM through 86965.  We have to write
    373 	 * into LSI registers to read from EEPROM.  I want to avoid it
    374 	 * at this stage, but I cannot test the presense of the chip
    375 	 * any further without reading EEPROM.  FIXME.
    376 	 */
    377 	ate_read_eeprom(iot, ioh, eeprom);
    378 
    379 	/* Make sure that config info in EEPROM and 86965 agree. */
    380 	if (eeprom[FE_EEPROM_CONF] != bus_space_read_1(iot, ioh, FE_BMPR19)) {
    381 #ifdef DIAGNOSTIC
    382 		printf("ate_find: "
    383 		    "incorrect configration in eeprom and chip\n");
    384 #endif
    385 		return (0);
    386 	}
    387 
    388 	/*
    389 	 * Try to determine IRQ settings.
    390 	 * Different models use different ranges of IRQs.
    391 	 */
    392 	n = (bus_space_read_1(iot, ioh, FE_BMPR19) & FE_B19_IRQ)
    393 	    >> FE_B19_IRQ_SHIFT;
    394 	switch (eeprom[FE_ATI_EEP_REVISION] & 0xf0) {
    395 	case 0x30:
    396 		*irq = irqmap[3][n];
    397 		break;
    398 	case 0x10:
    399 	case 0x50:
    400 		*irq = irqmap[2][n];
    401 		break;
    402 	case 0x40:
    403 	case 0x60:
    404 		if (eeprom[FE_ATI_EEP_MAGIC] & 0x04) {
    405 			*irq = irqmap[1][n];
    406 			break;
    407 		}
    408 	default:
    409 		*irq = irqmap[0][n];
    410 		break;
    411 	}
    412 
    413 	return (1);
    414 }
    415 
    416 /*
    417  * Determine type and ethernet address.
    418  */
    419 static int
    420 ate_detect(iot, ioh, enaddr)
    421 	bus_space_tag_t iot;
    422 	bus_space_handle_t ioh;
    423 	u_int8_t enaddr[ETHER_ADDR_LEN];
    424 {
    425 	u_char eeprom[FE_EEPROM_SIZE];
    426 	int type;
    427 
    428 	/* Get our station address from EEPROM. */
    429 	ate_read_eeprom(iot, ioh, eeprom);
    430 	bcopy(eeprom + FE_ATI_EEP_ADDR, enaddr, ETHER_ADDR_LEN);
    431 
    432 	/* Make sure we got a valid station address. */
    433 	if ((enaddr[0] & 0x03) != 0x00 ||
    434 	    (enaddr[0] == 0x00 && enaddr[1] == 0x00 && enaddr[2] == 0x00)) {
    435 #ifdef DIAGNOSTIC
    436 		printf("fmv_detect: invalid ethernet address\n");
    437 #endif
    438 		return (0);
    439 	}
    440 
    441 	/*
    442 	 * Determine the card type.
    443 	 */
    444 	switch (eeprom[FE_ATI_EEP_MODEL]) {
    445 	case FE_ATI_MODEL_AT1700T:
    446 		type = FE_TYPE_AT1700T;
    447 		break;
    448 	case FE_ATI_MODEL_AT1700BT:
    449 		type = FE_TYPE_AT1700BT;
    450 		break;
    451 	case FE_ATI_MODEL_AT1700FT:
    452 		type = FE_TYPE_AT1700FT;
    453 		break;
    454 	case FE_ATI_MODEL_AT1700AT:
    455 		type = FE_TYPE_AT1700AT;
    456 		break;
    457 	default:
    458 		type = FE_TYPE_RE2000;
    459 		break;
    460 	}
    461 
    462 	return (type);
    463 }
    464 
    465 void
    466 ate_attach(parent, self, aux)
    467 	struct device *parent, *self;
    468 	void *aux;
    469 {
    470 	struct ate_softc *isc = (struct ate_softc *)self;
    471 	struct mb86960_softc *sc = &isc->sc_mb86960;
    472 	struct isa_attach_args *ia = aux;
    473 	bus_space_tag_t iot = ia->ia_iot;
    474 	bus_space_handle_t ioh;
    475 	u_int8_t myea[ETHER_ADDR_LEN];
    476 	const char *typestr;
    477 	int type;
    478 
    479 	printf("\n");
    480 
    481 	/* Map i/o space. */
    482 	if (bus_space_map(iot, ia->ia_iobase, ATE_NPORTS, 0, &ioh)) {
    483 		printf("%s: can't map i/o space\n", sc->sc_dev.dv_xname);
    484 		return;
    485 	}
    486 
    487 	sc->sc_bst = iot;
    488 	sc->sc_bsh = ioh;
    489 
    490 	/* Determine the card type and get ehternet address. */
    491 	type = ate_detect(iot, ioh, myea);
    492 	switch (type) {
    493 	case FE_TYPE_AT1700T:
    494 		typestr = "AT-1700T";
    495 		break;
    496 	case FE_TYPE_AT1700BT:
    497 		typestr = "AT-1700BT";
    498 		break;
    499 	case FE_TYPE_AT1700FT:
    500 		typestr = "AT-1700FT";
    501 		break;
    502 	case FE_TYPE_AT1700AT:
    503 		typestr = "AT-1700AT";
    504 		break;
    505 	case FE_TYPE_RE2000:
    506 		typestr = "unknown (RE-2000?)";
    507 		break;
    508 
    509 	default:
    510 	  	/* Unknown card type: maybe a new model, but... */
    511 		printf("%s: where did the card go?!\n", sc->sc_dev.dv_xname);
    512 		panic("unknown card");
    513 	}
    514 
    515 	printf("%s: %s Ethernet\n", sc->sc_dev.dv_xname, typestr);
    516 
    517 	/* This interface is always enabled. */
    518 	sc->sc_enabled = 1;
    519 
    520 	/*
    521 	 * Do generic MB86960 attach.
    522 	 */
    523 	mb86960_attach(sc, MB86960_TYPE_86965, myea);
    524 
    525 	mb86960_config(sc, NULL, 0, 0);
    526 
    527 	/* Establish the interrupt handler. */
    528 	isc->sc_ih = isa_intr_establish(ia->ia_ic, ia->ia_irq, IST_EDGE,
    529 	    IPL_NET, mb86960_intr, sc);
    530 	if (isc->sc_ih == NULL)
    531 		printf("%s: couldn't establish interrupt handler\n",
    532 		    sc->sc_dev.dv_xname);
    533 }
    534