Home | History | Annotate | Line # | Download | only in isa
if_ate.c revision 1.32
      1 /*	$NetBSD: if_ate.c,v 1.32 2002/09/28 17:18:28 tsutsui 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/cdefs.h>
     35 __KERNEL_RCSID(0, "$NetBSD: if_ate.c,v 1.32 2002/09/28 17:18:28 tsutsui Exp $");
     36 
     37 #include <sys/param.h>
     38 #include <sys/systm.h>
     39 #include <sys/device.h>
     40 #include <sys/socket.h>
     41 #include <sys/syslog.h>
     42 
     43 #include <net/if.h>
     44 #include <net/if_ether.h>
     45 #include <net/if_media.h>
     46 
     47 #include <machine/bus.h>
     48 #include <machine/intr.h>
     49 
     50 #include <dev/ic/mb86960reg.h>
     51 #include <dev/ic/mb86960var.h>
     52 #include <dev/ic/ate_subr.h>
     53 
     54 #include <dev/isa/isavar.h>
     55 #include <dev/isa/if_fereg.h>	/* XXX */
     56 
     57 int	ate_match __P((struct device *, struct cfdata *, void *));
     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 const struct cfattach ate_isa_ca = {
     68 	sizeof(struct ate_softc), ate_match, ate_attach
     69 };
     70 
     71 struct fe_simple_probe_struct {
     72 	u_char port;	/* Offset from the base I/O address. */
     73 	u_char mask;	/* Bits to be checked. */
     74 	u_char bits;	/* Values to be compared against. */
     75 };
     76 
     77 static __inline__ int fe_simple_probe __P((bus_space_tag_t,
     78     bus_space_handle_t, struct fe_simple_probe_struct const *));
     79 static int ate_find __P((bus_space_tag_t, bus_space_handle_t, int *,
     80     int *));
     81 static int ate_detect __P((bus_space_tag_t, bus_space_handle_t,
     82     u_int8_t enaddr[ETHER_ADDR_LEN]));
     83 
     84 static int const ate_iomap[8] = {
     85 	0x260, 0x280, 0x2A0, 0x240, 0x340, 0x320, 0x380, 0x300
     86 };
     87 #define NATE_IOMAP (sizeof (ate_iomap) / sizeof (ate_iomap[0]))
     88 #define ATE_NPORTS 0x20
     89 
     90 /*
     91  * Hardware probe routines.
     92  */
     93 
     94 /*
     95  * Determine if the device is present.
     96  */
     97 int
     98 ate_match(parent, match, aux)
     99 	struct device *parent;
    100 	struct cfdata *match;
    101 	void *aux;
    102 {
    103 	struct isa_attach_args *ia = aux;
    104 	bus_space_tag_t iot = ia->ia_iot;
    105 	bus_space_handle_t ioh;
    106 	int i, iobase, irq, rv = 0;
    107 	u_int8_t myea[ETHER_ADDR_LEN];
    108 
    109 	if (ia->ia_nio < 1)
    110 		return (0);
    111 	if (ia->ia_nirq < 1)
    112 		return (0);
    113 
    114 	if (ISA_DIRECT_CONFIG(ia))
    115 		return (0);
    116 
    117 	/* Disallow wildcarded values. */
    118 	if (ia->ia_io[0].ir_addr == ISACF_PORT_DEFAULT)
    119 		return (0);
    120 
    121 	/*
    122 	 * See if the sepcified address is valid for MB86965A JLI mode.
    123 	 */
    124 	for (i = 0; i < NATE_IOMAP; i++)
    125 		if (ate_iomap[i] == ia->ia_io[0].ir_addr)
    126 			break;
    127 	if (i == NATE_IOMAP) {
    128 #ifdef ATE_DEBUG
    129 		printf("ate_match: unknown iobase 0x%x\n", ia->ia_iobase);
    130 #endif
    131 		return (0);
    132 	}
    133 
    134 	/* Map i/o space. */
    135 	if (bus_space_map(iot, ia->ia_io[0].ir_addr, ATE_NPORTS, 0, &ioh)) {
    136 #ifdef ATE_DEBUG
    137 		printf("ate_match: couldn't map iospace 0x%x\n",
    138 		    ia->ia_io[0].ir_addr);
    139 #endif
    140 		return (0);
    141 	}
    142 
    143 	if (ate_find(iot, ioh, &iobase, &irq) == 0) {
    144 #ifdef ATE_DEBUG
    145 		printf("ate_match: ate_find failed\n");
    146 #endif
    147 		goto out;
    148 	}
    149 
    150 	if (iobase != ia->ia_io[0].ir_addr) {
    151 #ifdef ATE_DEBUG
    152 		printf("ate_match: unexpected iobase in board: 0x%x\n",
    153 		    ia->ia_iobase);
    154 #endif
    155 		goto out;
    156 	}
    157 
    158 	if (ate_detect(iot, ioh, myea) == 0) { /* XXX necessary? */
    159 #ifdef ATE_DEBUG
    160 		printf("ate_match: ate_detect failed\n");
    161 #endif
    162 		goto out;
    163 	}
    164 
    165 	if (ia->ia_irq[0].ir_irq != ISACF_IRQ_DEFAULT) {
    166 		if (ia->ia_irq[0].ir_irq != irq) {
    167 			printf("ate_match: irq mismatch; "
    168 			    "kernel configured %d != board configured %d\n",
    169 			    ia->ia_irq[0].ir_irq, irq);
    170 			goto out;
    171 		}
    172 	} else
    173 		ia->ia_irq[0].ir_irq = irq;
    174 
    175 	ia->ia_nio = 1;
    176 	ia->ia_io[0].ir_size = ATE_NPORTS;
    177 
    178 	ia->ia_nirq = 1;
    179 
    180 	ia->ia_niomem = 0;
    181 	ia->ia_ndrq = 0;
    182 
    183 	rv = 1;
    184 
    185  out:
    186 	bus_space_unmap(iot, ioh, ATE_NPORTS);
    187 	return (rv);
    188 }
    189 
    190 /*
    191  * Check for specific bits in specific registers have specific values.
    192  */
    193 static __inline__ int
    194 fe_simple_probe (iot, ioh, sp)
    195 	bus_space_tag_t iot;
    196 	bus_space_handle_t ioh;
    197 	struct fe_simple_probe_struct const *sp;
    198 {
    199 	u_int8_t val;
    200 	struct fe_simple_probe_struct const *p;
    201 
    202 	for (p = sp; p->mask != 0; p++) {
    203 		val = bus_space_read_1(iot, ioh, p->port);
    204 		if ((val & p->mask) != p->bits) {
    205 #ifdef ATE_DEBUG
    206 			printf("fe_simple_probe: %x & %x != %x\n",
    207 			    val, p->mask, p->bits);
    208 #endif
    209 			return (0);
    210 		}
    211 	}
    212 
    213 	return (1);
    214 }
    215 
    216 /*
    217  * Hardware (vendor) specific probe routines.
    218  */
    219 
    220 /*
    221  * Probe and initialization for Allied-Telesis AT1700/RE2000 series.
    222  */
    223 static int
    224 ate_find(iot, ioh, iobase, irq)
    225 	bus_space_tag_t iot;
    226 	bus_space_handle_t ioh;
    227 	int *iobase, *irq;
    228 {
    229 	u_char eeprom[FE_EEPROM_SIZE];
    230 	int n;
    231 
    232 	static int const irqmap[4][4] = {
    233 		{  3,  4,  5,  9 },
    234 		{ 10, 11, 12, 15 },
    235 		{  3, 11,  5, 15 },
    236 		{ 10, 11, 14, 15 },
    237 	};
    238 	static struct fe_simple_probe_struct const probe_table[] = {
    239 		{ FE_DLCR2,  0x70, 0x00 },
    240 		{ FE_DLCR4,  0x08, 0x00 },
    241 		{ FE_DLCR5,  0x80, 0x00 },
    242 #if 0
    243 		{ FE_BMPR16, 0x1B, 0x00 },
    244 		{ FE_BMPR17, 0x7F, 0x00 },
    245 #endif
    246 		{ 0 }
    247 	};
    248 
    249 #if ATE_DEBUG >= 4
    250 	log(LOG_INFO, "ate_find: probe (0x%x) for ATI\n", iobase);
    251 #if 0
    252 	fe_dump(LOG_INFO, sc);
    253 #endif
    254 #endif
    255 
    256 	/*
    257 	 * We should test if MB86965A is on the base address now.
    258 	 * Unfortunately, it is very hard to probe it reliably, since
    259 	 * we have no way to reset the chip under software control.
    260 	 * On cold boot, we could check the "signature" bit patterns
    261 	 * described in the Fujitsu document.  On warm boot, however,
    262 	 * we can predict almost nothing about register values.
    263 	 */
    264 	if (!fe_simple_probe(iot, ioh, probe_table))
    265 		return (0);
    266 
    267 	/* Check if our I/O address matches config info on 86965. */
    268 	n = (bus_space_read_1(iot, ioh, FE_BMPR19) & FE_B19_ADDR)
    269 	    >> FE_B19_ADDR_SHIFT;
    270 	*iobase = ate_iomap[n];
    271 
    272 	/*
    273 	 * We are now almost sure we have an AT1700 at the given
    274 	 * address.  So, read EEPROM through 86965.  We have to write
    275 	 * into LSI registers to read from EEPROM.  I want to avoid it
    276 	 * at this stage, but I cannot test the presence of the chip
    277 	 * any further without reading EEPROM.  FIXME.
    278 	 */
    279 	ate_read_eeprom(iot, ioh, eeprom);
    280 
    281 	/* Make sure that config info in EEPROM and 86965 agree. */
    282 	if (eeprom[FE_EEPROM_CONF] != bus_space_read_1(iot, ioh, FE_BMPR19)) {
    283 #ifdef DIAGNOSTIC
    284 		printf("ate_find: "
    285 		    "incorrect configration in eeprom and chip\n");
    286 #endif
    287 		return (0);
    288 	}
    289 
    290 	/*
    291 	 * Try to determine IRQ settings.
    292 	 * Different models use different ranges of IRQs.
    293 	 */
    294 	n = (bus_space_read_1(iot, ioh, FE_BMPR19) & FE_B19_IRQ)
    295 	    >> FE_B19_IRQ_SHIFT;
    296 	switch (eeprom[FE_ATI_EEP_REVISION] & 0xf0) {
    297 	case 0x30:
    298 		*irq = irqmap[3][n];
    299 		break;
    300 	case 0x10:
    301 	case 0x50:
    302 		*irq = irqmap[2][n];
    303 		break;
    304 	case 0x40:
    305 	case 0x60:
    306 		if (eeprom[FE_ATI_EEP_MAGIC] & 0x04) {
    307 			*irq = irqmap[1][n];
    308 			break;
    309 		}
    310 	default:
    311 		*irq = irqmap[0][n];
    312 		break;
    313 	}
    314 
    315 	return (1);
    316 }
    317 
    318 /*
    319  * Determine type and ethernet address.
    320  */
    321 static int
    322 ate_detect(iot, ioh, enaddr)
    323 	bus_space_tag_t iot;
    324 	bus_space_handle_t ioh;
    325 	u_int8_t enaddr[ETHER_ADDR_LEN];
    326 {
    327 	u_char eeprom[FE_EEPROM_SIZE];
    328 	int type;
    329 
    330 	/* Get our station address from EEPROM. */
    331 	ate_read_eeprom(iot, ioh, eeprom);
    332 	memcpy(enaddr, eeprom + FE_ATI_EEP_ADDR, ETHER_ADDR_LEN);
    333 
    334 	/* Make sure we got a valid station address. */
    335 	if ((enaddr[0] & 0x03) != 0x00 ||
    336 	    (enaddr[0] == 0x00 && enaddr[1] == 0x00 && enaddr[2] == 0x00)) {
    337 #ifdef ATE_DEBUG
    338 		printf("fmv_detect: invalid ethernet address\n");
    339 #endif
    340 		return (0);
    341 	}
    342 
    343 	/*
    344 	 * Determine the card type.
    345 	 */
    346 	switch (eeprom[FE_ATI_EEP_MODEL]) {
    347 	case FE_ATI_MODEL_AT1700T:
    348 		type = FE_TYPE_AT1700T;
    349 		break;
    350 	case FE_ATI_MODEL_AT1700BT:
    351 		type = FE_TYPE_AT1700BT;
    352 		break;
    353 	case FE_ATI_MODEL_AT1700FT:
    354 		type = FE_TYPE_AT1700FT;
    355 		break;
    356 	case FE_ATI_MODEL_AT1700AT:
    357 		type = FE_TYPE_AT1700AT;
    358 		break;
    359 	default:
    360 		type = FE_TYPE_RE2000;
    361 		break;
    362 	}
    363 
    364 	return (type);
    365 }
    366 
    367 void
    368 ate_attach(parent, self, aux)
    369 	struct device *parent, *self;
    370 	void *aux;
    371 {
    372 	struct ate_softc *isc = (struct ate_softc *)self;
    373 	struct mb86960_softc *sc = &isc->sc_mb86960;
    374 	struct isa_attach_args *ia = aux;
    375 	bus_space_tag_t iot = ia->ia_iot;
    376 	bus_space_handle_t ioh;
    377 	u_int8_t myea[ETHER_ADDR_LEN];
    378 	const char *typestr;
    379 	int type;
    380 
    381 	printf("\n");
    382 
    383 	/* Map i/o space. */
    384 	if (bus_space_map(iot, ia->ia_io[0].ir_addr, ATE_NPORTS, 0, &ioh)) {
    385 		printf("%s: can't map i/o space\n", sc->sc_dev.dv_xname);
    386 		return;
    387 	}
    388 
    389 	sc->sc_bst = iot;
    390 	sc->sc_bsh = ioh;
    391 
    392 	/* Determine the card type and get ethernet address. */
    393 	type = ate_detect(iot, ioh, myea);
    394 	switch (type) {
    395 	case FE_TYPE_AT1700T:
    396 		typestr = "AT-1700T";
    397 		break;
    398 	case FE_TYPE_AT1700BT:
    399 		typestr = "AT-1700BT";
    400 		break;
    401 	case FE_TYPE_AT1700FT:
    402 		typestr = "AT-1700FT";
    403 		break;
    404 	case FE_TYPE_AT1700AT:
    405 		typestr = "AT-1700AT";
    406 		break;
    407 	case FE_TYPE_RE2000:
    408 		typestr = "unknown (RE-2000?)";
    409 		break;
    410 
    411 	default:
    412 	  	/* Unknown card type: maybe a new model, but... */
    413 		printf("%s: where did the card go?!\n", sc->sc_dev.dv_xname);
    414 		panic("unknown card");
    415 	}
    416 
    417 	printf("%s: %s Ethernet\n", sc->sc_dev.dv_xname, typestr);
    418 
    419 	/* This interface is always enabled. */
    420 	sc->sc_flags |= FE_FLAGS_ENABLED;
    421 
    422 	/*
    423 	 * Do generic MB86960 attach.
    424 	 */
    425 	mb86960_attach(sc, MB86960_TYPE_86965, myea);
    426 
    427 	mb86960_config(sc, NULL, 0, 0);
    428 
    429 	/* Establish the interrupt handler. */
    430 	isc->sc_ih = isa_intr_establish(ia->ia_ic, ia->ia_irq[0].ir_irq,
    431 	    IST_EDGE, IPL_NET, mb86960_intr, sc);
    432 	if (isc->sc_ih == NULL)
    433 		printf("%s: couldn't establish interrupt handler\n",
    434 		    sc->sc_dev.dv_xname);
    435 }
    436