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