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