Home | History | Annotate | Line # | Download | only in isa
if_ix.c revision 1.30
      1 /*	$NetBSD: if_ix.c,v 1.30 2009/03/14 15:36:18 dsl Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 1998 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Rafal K. Boni.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  *
     19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29  * POSSIBILITY OF SUCH DAMAGE.
     30  */
     31 
     32 #include <sys/cdefs.h>
     33 __KERNEL_RCSID(0, "$NetBSD: if_ix.c,v 1.30 2009/03/14 15:36:18 dsl Exp $");
     34 
     35 #include <sys/param.h>
     36 #include <sys/systm.h>
     37 #include <sys/mbuf.h>
     38 #include <sys/errno.h>
     39 #include <sys/device.h>
     40 #include <sys/protosw.h>
     41 #include <sys/socket.h>
     42 
     43 #include <net/if.h>
     44 #include <net/if_dl.h>
     45 #include <net/if_types.h>
     46 #include <net/if_media.h>
     47 #include <net/if_ether.h>
     48 
     49 #include <sys/cpu.h>
     50 #include <sys/bus.h>
     51 #include <sys/intr.h>
     52 
     53 #include <dev/isa/isareg.h>
     54 #include <dev/isa/isavar.h>
     55 
     56 #include <dev/ic/i82586reg.h>
     57 #include <dev/ic/i82586var.h>
     58 #include <dev/isa/if_ixreg.h>
     59 
     60 #ifdef IX_DEBUG
     61 #define DPRINTF(x)	printf x
     62 #else
     63 #define DPRINTF(x)
     64 #endif
     65 
     66 int ix_media[] = {
     67 	IFM_ETHER | IFM_10_5,
     68 	IFM_ETHER | IFM_10_2,
     69 	IFM_ETHER | IFM_10_T,
     70 };
     71 #define NIX_MEDIA       (sizeof(ix_media) / sizeof(ix_media[0]))
     72 
     73 struct ix_softc {
     74 	struct ie_softc sc_ie;
     75 
     76 	bus_space_tag_t sc_regt;	/* space tag for registers */
     77 	bus_space_handle_t sc_regh;	/* space handle for registers */
     78 
     79 	u_int8_t	use_pio;	/* use PIO rather than shared mem */
     80 	u_int16_t	irq_encoded;	/* encoded IRQ */
     81 	void		*sc_ih;		/* interrupt handle */
     82 };
     83 
     84 static void 	ix_reset(struct ie_softc *, int);
     85 static void 	ix_atten(struct ie_softc *, int);
     86 static int 	ix_intrhook(struct ie_softc *, int);
     87 
     88 static void     ix_copyin(struct ie_softc *, void *, int, size_t);
     89 static void     ix_copyout(struct ie_softc *, const void *, int, size_t);
     90 
     91 static void	ix_bus_barrier(struct ie_softc *, int, int, int);
     92 
     93 static u_int16_t ix_read_16(struct ie_softc *, int);
     94 static void	ix_write_16(struct ie_softc *, int, u_int16_t);
     95 static void	ix_write_24(struct ie_softc *, int, int);
     96 static void	ix_zeromem (struct ie_softc *, int, int);
     97 
     98 static void	ix_mediastatus(struct ie_softc *, struct ifmediareq *);
     99 
    100 static u_int16_t ix_read_eeprom(bus_space_tag_t, bus_space_handle_t, int);
    101 static void	ix_eeprom_outbits(bus_space_tag_t, bus_space_handle_t, int, int);
    102 static int	ix_eeprom_inbits (bus_space_tag_t, bus_space_handle_t);
    103 static void	ix_eeprom_clock  (bus_space_tag_t, bus_space_handle_t, int);
    104 
    105 int ix_match(struct device *, struct cfdata *, void *);
    106 void ix_attach(struct device *, struct device *, void *);
    107 
    108 /*
    109  * EtherExpress/16 support routines
    110  */
    111 static void
    112 ix_reset(struct ie_softc *sc, int why)
    113 {
    114 	struct ix_softc* isc = (struct ix_softc *) sc;
    115 
    116 	switch (why) {
    117 	case CHIP_PROBE:
    118 		bus_space_write_1(isc->sc_regt, isc->sc_regh, IX_ECTRL,
    119 				  IX_RESET_586);
    120 		delay(100);
    121 		bus_space_write_1(isc->sc_regt, isc->sc_regh, IX_ECTRL, 0);
    122 		delay(100);
    123 		break;
    124 
    125 	case CARD_RESET:
    126 		break;
    127     }
    128 }
    129 
    130 static void
    131 ix_atten(struct ie_softc *sc, int why)
    132 {
    133 	struct ix_softc* isc = (struct ix_softc *) sc;
    134 	bus_space_write_1(isc->sc_regt, isc->sc_regh, IX_ATTN, 0);
    135 }
    136 
    137 static u_int16_t
    138 ix_read_eeprom(bus_space_tag_t iot, bus_space_handle_t ioh, int location)
    139 {
    140 	int ectrl, edata;
    141 
    142 	ectrl = bus_space_read_1(iot, ioh, IX_ECTRL);
    143 	ectrl &= IX_ECTRL_MASK;
    144 	ectrl |= IX_ECTRL_EECS;
    145 	bus_space_write_1(iot, ioh, IX_ECTRL, ectrl);
    146 
    147 	ix_eeprom_outbits(iot, ioh, IX_EEPROM_READ, IX_EEPROM_OPSIZE1);
    148 	ix_eeprom_outbits(iot, ioh, location, IX_EEPROM_ADDR_SIZE);
    149 	edata = ix_eeprom_inbits(iot, ioh);
    150 	ectrl = bus_space_read_1(iot, ioh, IX_ECTRL);
    151 	ectrl &= ~(IX_RESET_ASIC | IX_ECTRL_EEDI | IX_ECTRL_EECS);
    152 	bus_space_write_1(iot, ioh, IX_ECTRL, ectrl);
    153 	ix_eeprom_clock(iot, ioh, 1);
    154 	ix_eeprom_clock(iot, ioh, 0);
    155 	return (edata);
    156 }
    157 
    158 static void
    159 ix_eeprom_outbits(iot, ioh, edata, count)
    160 	bus_space_tag_t iot;
    161 	bus_space_handle_t ioh;
    162 	int edata, count;
    163 {
    164 	int ectrl, i;
    165 
    166 	ectrl = bus_space_read_1(iot, ioh, IX_ECTRL);
    167 	ectrl &= ~IX_RESET_ASIC;
    168 	for (i = count - 1; i >= 0; i--) {
    169 		ectrl &= ~IX_ECTRL_EEDI;
    170 		if (edata & (1 << i)) {
    171 			ectrl |= IX_ECTRL_EEDI;
    172 		}
    173 		bus_space_write_1(iot, ioh, IX_ECTRL, ectrl);
    174 		delay(1);	/* eeprom data must be setup for 0.4 uSec */
    175 		ix_eeprom_clock(iot, ioh, 1);
    176 		ix_eeprom_clock(iot, ioh, 0);
    177 	}
    178 	ectrl &= ~IX_ECTRL_EEDI;
    179 	bus_space_write_1(iot, ioh, IX_ECTRL, ectrl);
    180 	delay(1);		/* eeprom data must be held for 0.4 uSec */
    181 }
    182 
    183 static int
    184 ix_eeprom_inbits(bus_space_tag_t iot, bus_space_handle_t ioh)
    185 {
    186 	int ectrl, edata, i;
    187 
    188 	ectrl = bus_space_read_1(iot, ioh, IX_ECTRL);
    189 	ectrl &= ~IX_RESET_ASIC;
    190 	for (edata = 0, i = 0; i < 16; i++) {
    191 		edata = edata << 1;
    192 		ix_eeprom_clock(iot, ioh, 1);
    193 		ectrl = bus_space_read_1(iot, ioh, IX_ECTRL);
    194 		if (ectrl & IX_ECTRL_EEDO) {
    195 			edata |= 1;
    196 		}
    197 		ix_eeprom_clock(iot, ioh, 0);
    198 	}
    199 	return (edata);
    200 }
    201 
    202 static void
    203 ix_eeprom_clock(bus_space_tag_t iot, bus_space_handle_t ioh, int state)
    204 {
    205 	int ectrl;
    206 
    207 	ectrl = bus_space_read_1(iot, ioh, IX_ECTRL);
    208 	ectrl &= ~(IX_RESET_ASIC | IX_ECTRL_EESK);
    209 	if (state) {
    210 		ectrl |= IX_ECTRL_EESK;
    211 	}
    212 	bus_space_write_1(iot, ioh, IX_ECTRL, ectrl);
    213 	delay(9);		/* EESK must be stable for 8.38 uSec */
    214 }
    215 
    216 static int
    217 ix_intrhook(struct ie_softc *sc, int where)
    218 {
    219 	struct ix_softc* isc = (struct ix_softc *) sc;
    220 
    221 	switch (where) {
    222 	case INTR_ENTER:
    223 		/* entering ISR: disable card interrupts */
    224 		bus_space_write_1(isc->sc_regt, isc->sc_regh,
    225 				  IX_IRQ, isc->irq_encoded);
    226 		break;
    227 
    228 	case INTR_EXIT:
    229 		/* exiting ISR: re-enable card interrupts */
    230 		bus_space_write_1(isc->sc_regt, isc->sc_regh, IX_IRQ,
    231     				  isc->irq_encoded | IX_IRQ_ENABLE);
    232 	break;
    233     }
    234 
    235     return 1;
    236 }
    237 
    238 
    239 static void
    240 ix_copyin (sc, dst, offset, size)
    241         struct ie_softc *sc;
    242         void *dst;
    243         int offset;
    244         size_t size;
    245 {
    246 	int i, dribble;
    247 	u_int8_t* bptr = dst;
    248 	u_int16_t* wptr = dst;
    249 	struct ix_softc* isc = (struct ix_softc *) sc;
    250 
    251 	if (isc->use_pio) {
    252 		/* Reset read pointer to the specified offset */
    253 		bus_space_barrier(sc->bt, sc->bh, IX_DATAPORT, 2,
    254 						  BUS_SPACE_BARRIER_READ);
    255 		bus_space_write_2(sc->bt, sc->bh, IX_READPTR, offset);
    256 		bus_space_barrier(sc->bt, sc->bh, IX_READPTR, 2,
    257 						  BUS_SPACE_BARRIER_WRITE);
    258 	} else {
    259 	bus_space_barrier(sc->bt, sc->bh, offset, size,
    260 			  BUS_SPACE_BARRIER_READ);
    261 	}
    262 
    263 	if (offset % 2) {
    264 		if (isc->use_pio)
    265 			*bptr = bus_space_read_1(sc->bt, sc->bh, IX_DATAPORT);
    266 		else
    267 		*bptr = bus_space_read_1(sc->bt, sc->bh, offset);
    268 		offset++; bptr++; size--;
    269 	}
    270 
    271 	dribble = size % 2;
    272 	wptr = (u_int16_t*) bptr;
    273 
    274 	if (isc->use_pio) {
    275 		for(i = 0; i <  size / 2; i++) {
    276 			*wptr = bus_space_read_2(sc->bt, sc->bh, IX_DATAPORT);
    277 			wptr++;
    278 		}
    279 	} else {
    280 		bus_space_read_region_2(sc->bt, sc->bh, offset,
    281 					(u_int16_t *) bptr, size / 2);
    282 	}
    283 
    284 	if (dribble) {
    285 		bptr += size - 1;
    286 		offset += size - 1;
    287 
    288 		if (isc->use_pio)
    289 			*bptr = bus_space_read_1(sc->bt, sc->bh, IX_DATAPORT);
    290 		else
    291 		*bptr = bus_space_read_1(sc->bt, sc->bh, offset);
    292 	}
    293 }
    294 
    295 static void
    296 ix_copyout (sc, src, offset, size)
    297         struct ie_softc *sc;
    298         const void *src;
    299         int offset;
    300         size_t size;
    301 {
    302 	int i, dribble;
    303 	int osize = size;
    304 	int ooffset = offset;
    305 	const u_int8_t* bptr = src;
    306 	const u_int16_t* wptr = src;
    307 	struct ix_softc* isc = (struct ix_softc *) sc;
    308 
    309 	if (isc->use_pio) {
    310 		/* Reset write pointer to the specified offset */
    311 		bus_space_write_2(sc->bt, sc->bh, IX_WRITEPTR, offset);
    312 		bus_space_barrier(sc->bt, sc->bh, IX_WRITEPTR, 2,
    313 						  BUS_SPACE_BARRIER_WRITE);
    314 	}
    315 
    316 	if (offset % 2) {
    317 		if (isc->use_pio)
    318 			bus_space_write_1(sc->bt, sc->bh, IX_DATAPORT, *bptr);
    319 		else
    320 		bus_space_write_1(sc->bt, sc->bh, offset, *bptr);
    321 		offset++; bptr++; size--;
    322 	}
    323 
    324 	dribble = size % 2;
    325 	wptr = (const u_int16_t*) bptr;
    326 
    327 	if (isc->use_pio) {
    328 		for(i = 0; i < size / 2; i++) {
    329 			bus_space_write_2(sc->bt, sc->bh, IX_DATAPORT, *wptr);
    330 			wptr++;
    331 		}
    332 	} else {
    333 		bus_space_write_region_2(sc->bt, sc->bh, offset,
    334 		    (const u_int16_t *)bptr, size / 2);
    335 	}
    336 
    337 	if (dribble) {
    338 		bptr += size - 1;
    339 		offset += size - 1;
    340 
    341 		if (isc->use_pio)
    342 			bus_space_write_1(sc->bt, sc->bh, IX_DATAPORT, *bptr);
    343 		else
    344 		bus_space_write_1(sc->bt, sc->bh, offset, *bptr);
    345 	}
    346 
    347 	if (isc->use_pio)
    348 		bus_space_barrier(sc->bt, sc->bh, IX_DATAPORT, 2,
    349 						  BUS_SPACE_BARRIER_WRITE);
    350 	else
    351 	bus_space_barrier(sc->bt, sc->bh, ooffset, osize,
    352 			  BUS_SPACE_BARRIER_WRITE);
    353 }
    354 
    355 static void
    356 ix_bus_barrier(sc, offset, length, flags)
    357         struct ie_softc *sc;
    358         int offset, length, flags;
    359 {
    360 	struct ix_softc* isc = (struct ix_softc *) sc;
    361 
    362 	if (isc->use_pio)
    363 		bus_space_barrier(sc->bt, sc->bh, IX_DATAPORT, 2, flags);
    364 	else
    365 		bus_space_barrier(sc->bt, sc->bh, offset, length, flags);
    366 }
    367 
    368 static u_int16_t
    369 ix_read_16 (sc, offset)
    370         struct ie_softc *sc;
    371         int offset;
    372 {
    373 	struct ix_softc* isc = (struct ix_softc *) sc;
    374 
    375 	if (isc->use_pio) {
    376 		bus_space_barrier(sc->bt, sc->bh, IX_DATAPORT, 2,
    377 						  BUS_SPACE_BARRIER_READ);
    378 
    379 		/* Reset read pointer to the specified offset */
    380 		bus_space_write_2(sc->bt, sc->bh, IX_READPTR, offset);
    381 		bus_space_barrier(sc->bt, sc->bh, IX_READPTR, 2,
    382 						  BUS_SPACE_BARRIER_WRITE);
    383 
    384 		return bus_space_read_2(sc->bt, sc->bh, IX_DATAPORT);
    385 	} else {
    386 		bus_space_barrier(sc->bt, sc->bh, offset, 2,
    387 						  BUS_SPACE_BARRIER_READ);
    388         return bus_space_read_2(sc->bt, sc->bh, offset);
    389 	}
    390 }
    391 
    392 static void
    393 ix_write_16 (sc, offset, value)
    394         struct ie_softc *sc;
    395         int offset;
    396         u_int16_t value;
    397 {
    398 	struct ix_softc* isc = (struct ix_softc *) sc;
    399 
    400 	if (isc->use_pio) {
    401 		/* Reset write pointer to the specified offset */
    402 		bus_space_write_2(sc->bt, sc->bh, IX_WRITEPTR, offset);
    403 		bus_space_barrier(sc->bt, sc->bh, IX_WRITEPTR, 2,
    404 						  BUS_SPACE_BARRIER_WRITE);
    405 
    406 		bus_space_write_2(sc->bt, sc->bh, IX_DATAPORT, value);
    407 		bus_space_barrier(sc->bt, sc->bh, IX_DATAPORT, 2,
    408 						  BUS_SPACE_BARRIER_WRITE);
    409 	} else {
    410         bus_space_write_2(sc->bt, sc->bh, offset, value);
    411 		bus_space_barrier(sc->bt, sc->bh, offset, 2,
    412 						  BUS_SPACE_BARRIER_WRITE);
    413 	}
    414 }
    415 
    416 static void
    417 ix_write_24 (sc, offset, addr)
    418         struct ie_softc *sc;
    419         int offset, addr;
    420 {
    421 	char* ptr;
    422 	struct ix_softc* isc = (struct ix_softc *) sc;
    423 	int val = addr + (u_long) sc->sc_maddr - (u_long) sc->sc_iobase;
    424 
    425 	if (isc->use_pio) {
    426 		/* Reset write pointer to the specified offset */
    427 		bus_space_write_2(sc->bt, sc->bh, IX_WRITEPTR, offset);
    428 		bus_space_barrier(sc->bt, sc->bh, IX_WRITEPTR, 2,
    429 						  BUS_SPACE_BARRIER_WRITE);
    430 
    431 		ptr = (char*) &val;
    432 		bus_space_write_2(sc->bt, sc->bh, IX_DATAPORT,
    433 						  *((u_int16_t *)ptr));
    434 		bus_space_write_2(sc->bt, sc->bh, IX_DATAPORT,
    435 						  *((u_int16_t *)(ptr + 2)));
    436 		bus_space_barrier(sc->bt, sc->bh, IX_DATAPORT, 2,
    437 						  BUS_SPACE_BARRIER_WRITE);
    438 	} else {
    439         	bus_space_write_4(sc->bt, sc->bh, offset, val);
    440 		bus_space_barrier(sc->bt, sc->bh, offset, 4,
    441 						  BUS_SPACE_BARRIER_WRITE);
    442 	}
    443 }
    444 
    445 static void
    446 ix_zeromem(sc, offset, count)
    447         struct ie_softc *sc;
    448         int offset, count;
    449 {
    450 	int i;
    451 	int dribble;
    452 	struct ix_softc* isc = (struct ix_softc *) sc;
    453 
    454 	if (isc->use_pio) {
    455 		/* Reset write pointer to the specified offset */
    456 		bus_space_write_2(sc->bt, sc->bh, IX_WRITEPTR, offset);
    457 		bus_space_barrier(sc->bt, sc->bh, IX_WRITEPTR, 2,
    458 						  BUS_SPACE_BARRIER_WRITE);
    459 
    460 		if (offset % 2) {
    461 			bus_space_write_1(sc->bt, sc->bh, IX_DATAPORT, 0);
    462 			count--;
    463 		}
    464 
    465 	        dribble = count % 2;
    466 		for(i = 0; i < count / 2; i++)
    467 			bus_space_write_2(sc->bt, sc->bh, IX_DATAPORT, 0);
    468 
    469 		if (dribble)
    470 			bus_space_write_1(sc->bt, sc->bh, IX_DATAPORT, 0);
    471 
    472 		bus_space_barrier(sc->bt, sc->bh, IX_DATAPORT, 2,
    473 						  BUS_SPACE_BARRIER_WRITE);
    474 	} else {
    475 		bus_space_set_region_1(sc->bt, sc->bh, offset, 0, count);
    476 		bus_space_barrier(sc->bt, sc->bh, offset, count,
    477 						  BUS_SPACE_BARRIER_WRITE);
    478 	}
    479 }
    480 
    481 static void
    482 ix_mediastatus(struct ie_softc *sc, struct ifmediareq *ifmr)
    483 {
    484         struct ifmedia *ifm = &sc->sc_media;
    485 
    486         /*
    487          * The currently selected media is always the active media.
    488          */
    489         ifmr->ifm_active = ifm->ifm_cur->ifm_media;
    490 }
    491 
    492 int
    493 ix_match(struct device *parent, struct cfdata *cf, void *aux)
    494 {
    495 	int i;
    496 	int rv = 0;
    497 	bus_addr_t maddr;
    498 	bus_size_t msiz;
    499 	u_short checksum = 0;
    500 	bus_space_handle_t ioh;
    501 	bus_space_tag_t iot;
    502 	u_int8_t val, bart_config;
    503 	u_short pg, adjust, decode, edecode;
    504 	u_short board_id, id_var1, id_var2, irq, irq_encoded;
    505 	struct isa_attach_args * const ia = aux;
    506 	short irq_translate[] = {0, 0x09, 0x03, 0x04, 0x05, 0x0a, 0x0b, 0};
    507 
    508 	if (ia->ia_nio < 1)
    509 		return (0);
    510 	if (ia->ia_niomem < 1)
    511 		return (0);
    512 	if (ia->ia_nirq < 1)
    513 		return (0);
    514 
    515 	if (ISA_DIRECT_CONFIG(ia))
    516 		return (0);
    517 
    518 	iot = ia->ia_iot;
    519 
    520 	if (ia->ia_io[0].ir_addr == ISA_UNKNOWN_PORT)
    521 		return (0);
    522 
    523 	if (bus_space_map(iot, ia->ia_io[0].ir_addr,
    524 			  IX_IOSIZE, 0, &ioh) != 0) {
    525 		DPRINTF(("Can't map io space at 0x%x\n", ia->ia_iobase));
    526 		return (0);
    527 	}
    528 
    529 	/* XXX: reset any ee16 at the current iobase */
    530 	bus_space_write_1(iot, ioh, IX_ECTRL, IX_RESET_ASIC);
    531 	bus_space_write_1(iot, ioh, IX_ECTRL, 0);
    532 	delay(240);
    533 
    534 	/* now look for ee16. */
    535 	board_id = id_var1 = id_var2 = 0;
    536 	for (i = 0; i < 4 ; i++) {
    537 		id_var1 = bus_space_read_1(iot, ioh, IX_ID_PORT);
    538 		id_var2 = ((id_var1 & 0x03) << 2);
    539 		board_id |= (( id_var1 >> 4)  << id_var2);
    540 	}
    541 
    542 	if (board_id != IX_ID) {
    543 		DPRINTF(("BART ID mismatch (got 0x%04x, expected 0x%04x)\n",
    544 			board_id, IX_ID));
    545 		goto out;
    546 	}
    547 
    548 	/*
    549 	 * The shared RAM size and location of the EE16 is encoded into
    550 	 * EEPROM location 6.  The location of the first set bit tells us
    551 	 * the memory address (0xc0000 + (0x4000 * FSB)), where FSB is the
    552 	 * number of the first set bit.  The zeroes are then shifted out,
    553 	 * and the results is the memory size (1 = 16k, 3 = 32k, 7 = 48k,
    554 	 * 0x0f = 64k).
    555 	 *
    556 	 * Examples:
    557 	 *   0x3c -> 64k@0xc8000, 0x70 -> 48k@0xd0000, 0xc0 -> 32k@0xd8000
    558 	 *   0x80 -> 16k@0xdc000.
    559 	 *
    560 	 * Side note: this comes from reading the old driver rather than
    561 	 * from a more definitive source, so it could be out-of-whack
    562 	 * with what the card can do...
    563 	 */
    564 
    565 	val = ix_read_eeprom(iot, ioh, 6) & 0xff;
    566 	for (pg = 0; pg < 8; pg++) {
    567 		if (val & 1)
    568 			break;
    569 		val >>= 1;
    570 	}
    571 
    572 	maddr = 0xc0000 + (pg * 0x4000);
    573 
    574 	switch (val) {
    575 	case 0x00:
    576 		maddr = 0;
    577 		msiz = 0;
    578 		break;
    579 
    580 	case 0x01:
    581 		msiz = 16 * 1024;
    582 		break;
    583 
    584 	case 0x03:
    585 		msiz = 32 * 1024;
    586 		break;
    587 
    588 	case 0x07:
    589 		msiz = 48 * 1024;
    590 		break;
    591 
    592 	case 0x0f:
    593 		msiz = 64 * 1024;
    594 		break;
    595 
    596 	default:
    597 		DPRINTF(("invalid memory size %02x\n", val));
    598 		goto out;
    599 	}
    600 
    601 	if (ia->ia_iomem[0].ir_addr != ISA_UNKNOWN_IOMEM &&
    602 	    ia->ia_iomem[0].ir_addr != maddr) {
    603 		DPRINTF((
    604 		  "ix_match: memaddr of board @ 0x%x doesn't match config\n",
    605 		  ia->ia_iobase));
    606 		goto out;
    607 	}
    608 
    609 	if (ia->ia_iomem[0].ir_size != ISA_UNKNOWN_IOSIZ &&
    610 	    ia->ia_iomem[0].ir_size != msiz) {
    611 		DPRINTF((
    612 		   "ix_match: memsize of board @ 0x%x doesn't match config\n",
    613 		   ia->ia_iobase));
    614 		goto out;
    615 	}
    616 
    617 	/* need to put the 586 in RESET, and leave it */
    618 	bus_space_write_1(iot, ioh, IX_ECTRL, IX_RESET_586);
    619 
    620 	/* read the eeprom and checksum it, should == IX_ID */
    621 	for(i = 0; i < 0x40; i++)
    622 		checksum += ix_read_eeprom(iot, ioh, i);
    623 
    624 	if (checksum != IX_ID) {
    625 		DPRINTF(("checksum mismatch (got 0x%04x, expected 0x%04x\n",
    626 			checksum, IX_ID));
    627 		goto out;
    628 	}
    629 
    630 	/*
    631 	 * Only do the following bit if using memory-mapped access.  For
    632 	 * boards with no mapped memory, we use PIO.  We also use PIO for
    633 	 * boards with 16K of mapped memory, as those setups don't seem
    634 	 * to work otherwise.
    635 	 */
    636 	if (msiz != 0 && msiz != 16384) {
    637 		/* Set board up with memory-mapping info */
    638 	adjust = IX_MCTRL_FMCS16 | (pg & 0x3) << 2;
    639 	decode = ((1 << (ia->ia_iomem[0].ir_size / 16384)) - 1) << pg;
    640 	edecode = ((~decode >> 4) & 0xF0) | (decode >> 8);
    641 
    642 	bus_space_write_1(iot, ioh, IX_MEMDEC, decode & 0xFF);
    643 	bus_space_write_1(iot, ioh, IX_MCTRL, adjust);
    644 	bus_space_write_1(iot, ioh, IX_MPCTRL, (~decode & 0xFF));
    645 
    646 		/* XXX disable Exxx */
    647 		bus_space_write_1(iot, ioh, IX_MECTRL, edecode);
    648 	}
    649 
    650 	/*
    651 	 * Get the encoded interrupt number from the EEPROM, check it
    652 	 * against the passed in IRQ.  Issue a warning if they do not
    653 	 * match, and fail the probe.  If irq is 'ISA_UNKNOWN_IRQ' then we
    654 	 * use the EEPROM irq, and continue.
    655 	 */
    656 	irq_encoded = ix_read_eeprom(iot, ioh, IX_EEPROM_CONFIG1);
    657 	irq_encoded = (irq_encoded & IX_EEPROM_IRQ) >> IX_EEPROM_IRQ_SHIFT;
    658 	irq = irq_translate[irq_encoded];
    659 	if (ia->ia_irq[0].ir_irq != ISA_UNKNOWN_IRQ &&
    660 	    irq != ia->ia_irq[0].ir_irq) {
    661 		DPRINTF(("board IRQ %d does not match config\n", irq));
    662 		goto out;
    663 	}
    664 
    665 	/* disable the board interrupts */
    666 	bus_space_write_1(iot, ioh, IX_IRQ, irq_encoded);
    667 
    668 	bart_config = bus_space_read_1(iot, ioh, IX_CONFIG);
    669 	bart_config |= IX_BART_LOOPBACK;
    670 	bart_config |= IX_BART_MCS16_TEST; /* inb doesn't get bit! */
    671 	bus_space_write_1(iot, ioh, IX_CONFIG, bart_config);
    672 	bart_config = bus_space_read_1(iot, ioh, IX_CONFIG);
    673 
    674 	bus_space_write_1(iot, ioh, IX_ECTRL, 0);
    675 	delay(100);
    676 
    677 	rv = 1;
    678 
    679 	ia->ia_nio = 1;
    680 	ia->ia_io[0].ir_size = IX_IOSIZE;
    681 
    682 	ia->ia_niomem = 1;
    683 	ia->ia_iomem[0].ir_addr = maddr;
    684 	ia->ia_iomem[0].ir_size = msiz;
    685 
    686 	ia->ia_nirq = 1;
    687 	ia->ia_irq[0].ir_irq = irq;
    688 
    689 	DPRINTF(("ix_match: found board @ 0x%x\n", ia->ia_iobase));
    690 
    691 out:
    692 	bus_space_unmap(iot, ioh, IX_IOSIZE);
    693 	return (rv);
    694 }
    695 
    696 void
    697 ix_attach(struct device *parent, struct device *self, void *aux)
    698 {
    699 	struct ix_softc *isc = (void *)self;
    700 	struct ie_softc *sc = &isc->sc_ie;
    701 	struct isa_attach_args *ia = aux;
    702 
    703 	int media;
    704 	int i, memsize;
    705 	u_int8_t bart_config;
    706 	bus_space_tag_t iot;
    707 	u_int8_t bpat, bval;
    708 	u_int16_t wpat, wval;
    709 	bus_space_handle_t ioh, memh;
    710 	u_short irq_encoded;
    711 	u_int8_t ethaddr[ETHER_ADDR_LEN];
    712 
    713 	iot = ia->ia_iot;
    714 
    715 	/*
    716 	 * Shared memory access seems to fail on 16K mapped boards, so
    717 	 * disable shared memory access if the board is in 16K mode.  If
    718 	 * no memory is mapped, we have no choice but to use PIO
    719 	 */
    720 	isc->use_pio = (ia->ia_iomem[0].ir_size <= (16 * 1024));
    721 
    722 	if (bus_space_map(iot, ia->ia_io[0].ir_addr,
    723 			  ia->ia_io[0].ir_size, 0, &ioh) != 0) {
    724 
    725 		DPRINTF(("\n%s: can't map i/o space 0x%x-0x%x\n",
    726 			  device_xname(&sc->sc_dev), ia->ia_[0].ir_addr,
    727 			  ia->ia_io[0].ir_addr + ia->ia_io[0].ir_size - 1));
    728 		return;
    729 	}
    730 
    731 	/* We map memory even if using PIO so something else doesn't grab it */
    732 	if (ia->ia_iomem[0].ir_size) {
    733 	if (bus_space_map(ia->ia_memt, ia->ia_iomem[0].ir_addr,
    734 			  ia->ia_iomem[0].ir_size, 0, &memh) != 0) {
    735 		DPRINTF(("\n%s: can't map iomem space 0x%x-0x%x\n",
    736 			device_xname(&sc->sc_dev), ia->ia_iomem[0].ir_addr,
    737 			ia->ia_iomem[0].ir_addr + ia->ia_iomem[0].ir_size - 1));
    738 		bus_space_unmap(iot, ioh, ia->ia_io[0].ir_size);
    739 		return;
    740 	}
    741 	}
    742 
    743 	isc->sc_regt = iot;
    744 	isc->sc_regh = ioh;
    745 
    746 	/*
    747 	 * Get the hardware ethernet address from the EEPROM and
    748 	 * save it in the softc for use by the 586 setup code.
    749 	 */
    750 	wval = ix_read_eeprom(iot, ioh, IX_EEPROM_ENET_HIGH);
    751 	ethaddr[1] = wval & 0xFF;
    752 	ethaddr[0] = wval >> 8;
    753 	wval = ix_read_eeprom(iot, ioh, IX_EEPROM_ENET_MID);
    754 	ethaddr[3] = wval & 0xFF;
    755 	ethaddr[2] = wval >> 8;
    756 	wval = ix_read_eeprom(iot, ioh, IX_EEPROM_ENET_LOW);
    757 	ethaddr[5] = wval & 0xFF;
    758 	ethaddr[4] = wval >> 8;
    759 
    760 	sc->hwinit = NULL;
    761 	sc->hwreset = ix_reset;
    762 	sc->chan_attn = ix_atten;
    763 	sc->intrhook = ix_intrhook;
    764 
    765 	sc->memcopyin = ix_copyin;
    766 	sc->memcopyout = ix_copyout;
    767 
    768 	/* If using PIO, make sure to setup single-byte read/write functions */
    769 	if (isc->use_pio) {
    770 		sc->ie_bus_barrier = ix_bus_barrier;
    771 	} else {
    772 		sc->ie_bus_barrier = NULL;
    773 	}
    774 
    775 	sc->ie_bus_read16 = ix_read_16;
    776 	sc->ie_bus_write16 = ix_write_16;
    777 	sc->ie_bus_write24 = ix_write_24;
    778 
    779 	sc->do_xmitnopchain = 0;
    780 
    781 	sc->sc_mediachange = NULL;
    782 	sc->sc_mediastatus = ix_mediastatus;
    783 
    784 	if (isc->use_pio) {
    785 		sc->bt = iot;
    786 		sc->bh = ioh;
    787 
    788 		/*
    789 		 * If using PIO, the memory size is bounded by on-card memory,
    790 		 * not by how much is mapped into the memory-mapped region, so
    791 		 * determine how much total memory we have to play with here.
    792 		 */
    793 		for(memsize = 64 * 1024; memsize; memsize -= 16 * 1024) {
    794 			/* warm up shared memory, the zero it all out */
    795 			ix_zeromem(sc, 0, 32);
    796 			ix_zeromem(sc, 0, memsize);
    797 
    798 			/* Reset write pointer to the start of RAM */
    799 			bus_space_write_2(iot, ioh, IX_WRITEPTR, 0);
    800 			bus_space_barrier(iot, ioh, IX_WRITEPTR, 2,
    801 						    BUS_SPACE_BARRIER_WRITE);
    802 
    803 			/* write test pattern */
    804 			for(i = 0, wpat = 1; i < memsize; i += 2) {
    805 				bus_space_write_2(iot, ioh, IX_DATAPORT, wpat);
    806 				wpat += 3;
    807 			}
    808 
    809 			/* Flush all reads & writes to data port */
    810 			bus_space_barrier(iot, ioh, IX_DATAPORT, 2,
    811 						    BUS_SPACE_BARRIER_READ |
    812 						    BUS_SPACE_BARRIER_WRITE);
    813 
    814 			/* Reset read pointer to beginning of card RAM */
    815 			bus_space_write_2(iot, ioh, IX_READPTR, 0);
    816 			bus_space_barrier(iot, ioh, IX_READPTR, 2,
    817 						    BUS_SPACE_BARRIER_WRITE);
    818 
    819 			/* read and verify test pattern */
    820 			for(i = 0, wpat = 1; i < memsize; i += 2) {
    821 				wval = bus_space_read_2(iot, ioh, IX_DATAPORT);
    822 
    823 				if (wval != wpat)
    824 					break;
    825 
    826 				wpat += 3;
    827 			}
    828 
    829 			/* If we failed, try next size down */
    830 			if (i != memsize)
    831 				continue;
    832 
    833 			/* Now try it all with byte reads/writes */
    834 			ix_zeromem(sc, 0, 32);
    835 			ix_zeromem(sc, 0, memsize);
    836 
    837 			/* Reset write pointer to start of card RAM */
    838 			bus_space_write_2(iot, ioh, IX_WRITEPTR, 0);
    839 			bus_space_barrier(iot, ioh, IX_WRITEPTR, 2,
    840 						    BUS_SPACE_BARRIER_WRITE);
    841 
    842 			/* write out test pattern */
    843 			for(i = 0, bpat = 1; i < memsize; i++) {
    844 				bus_space_write_1(iot, ioh, IX_DATAPORT, bpat);
    845 				bpat += 3;
    846 			}
    847 
    848 			/* Flush all reads & writes to data port */
    849 			bus_space_barrier(iot, ioh, IX_DATAPORT, 2,
    850 						    BUS_SPACE_BARRIER_READ |
    851 						    BUS_SPACE_BARRIER_WRITE);
    852 
    853 			/* Reset read pointer to beginning of card RAM */
    854 			bus_space_write_2(iot, ioh, IX_READPTR, 0);
    855 			bus_space_barrier(iot, ioh, IX_READPTR, 2,
    856 						    BUS_SPACE_BARRIER_WRITE);
    857 
    858 			/* read and verify test pattern */
    859 			for(i = 0, bpat = 1; i < memsize; i++) {
    860 				bval = bus_space_read_1(iot, ioh, IX_DATAPORT);
    861 
    862 				if (bval != bpat)
    863 				bpat += 3;
    864 			}
    865 
    866 			/* If we got through all of memory, we're done! */
    867 			if (i == memsize)
    868 				break;
    869 		}
    870 
    871 		/* Memory tests failed, punt... */
    872 		if (memsize == 0)  {
    873 			DPRINTF(("\n%s: can't determine size of on-card RAM\n",
    874 				device_xname(&sc->sc_dev)));
    875 			bus_space_unmap(iot, ioh, ia->ia_io[0].ir_size);
    876 			return;
    877 		}
    878 
    879 		sc->bt = iot;
    880 		sc->bh = ioh;
    881 
    882 		sc->sc_msize = memsize;
    883 		sc->sc_maddr = (void*) 0;
    884 	} else {
    885 	sc->bt = ia->ia_memt;
    886 	sc->bh = memh;
    887 
    888 	sc->sc_msize = ia->ia_iomem[0].ir_size;
    889 	sc->sc_maddr = (void *)memh;
    890 	}
    891 
    892 	/* Map i/o space. */
    893 	sc->sc_iobase = (char *)sc->sc_maddr + sc->sc_msize - (1 << 24);
    894 
    895 	/* set up pointers to important on-card control structures */
    896 	sc->iscp = 0;
    897 	sc->scb = IE_ISCP_SZ;
    898 	sc->scp = sc->sc_msize + IE_SCP_ADDR - (1 << 24);
    899 
    900 	sc->buf_area = sc->scb + IE_SCB_SZ;
    901 	sc->buf_area_sz = sc->sc_msize - IE_ISCP_SZ - IE_SCB_SZ - IE_SCP_SZ;
    902 
    903 	/* zero card memory */
    904 	ix_zeromem(sc, 0, 32);
    905 	ix_zeromem(sc, 0, sc->sc_msize);
    906 
    907 	/* set card to 16-bit bus mode */
    908 	if (isc->use_pio) {
    909 		bus_space_write_2(sc->bt, sc->bh, IX_WRITEPTR,
    910 				  	    IE_SCP_BUS_USE((u_long)sc->scp));
    911 		bus_space_barrier(sc->bt, sc->bh, IX_WRITEPTR, 2,
    912 					          BUS_SPACE_BARRIER_WRITE);
    913 
    914 		bus_space_write_1(sc->bt, sc->bh, IX_DATAPORT,
    915 				  IE_SYSBUS_16BIT);
    916 	} else {
    917 		bus_space_write_1(sc->bt, sc->bh,
    918 				  IE_SCP_BUS_USE((u_long)sc->scp),
    919 				  IE_SYSBUS_16BIT);
    920 	}
    921 
    922 	/* set up pointers to key structures */
    923 	ix_write_24(sc, IE_SCP_ISCP((u_long)sc->scp), (u_long) sc->iscp);
    924 	ix_write_16(sc, IE_ISCP_SCB((u_long)sc->iscp), (u_long) sc->scb);
    925 	ix_write_24(sc, IE_ISCP_BASE((u_long)sc->iscp), (u_long) sc->iscp);
    926 
    927 	/* flush setup of pointers, check if chip answers */
    928 	if (isc->use_pio) {
    929 		bus_space_barrier(sc->bt, sc->bh, 0, IX_IOSIZE,
    930 				  BUS_SPACE_BARRIER_WRITE);
    931 	} else {
    932 	bus_space_barrier(sc->bt, sc->bh, 0, sc->sc_msize,
    933 			  BUS_SPACE_BARRIER_WRITE);
    934 	}
    935 
    936 	if (!i82586_proberam(sc)) {
    937 		DPRINTF(("\n%s: Can't talk to i82586!\n",
    938 			device_xname(&sc->sc_dev)));
    939 		bus_space_unmap(iot, ioh, ia->ia_io[0].ir_size);
    940 
    941 		if (ia->ia_iomem[0].ir_size)
    942 		bus_space_unmap(ia->ia_memt, memh, ia->ia_iomem[0].ir_size);
    943 		return;
    944 	}
    945 
    946 	/* Figure out which media is being used... */
    947 	if (ix_read_eeprom(iot, ioh, IX_EEPROM_CONFIG1) &
    948 				IX_EEPROM_MEDIA_EXT) {
    949 		if (ix_read_eeprom(iot, ioh, IX_EEPROM_MEDIA) &
    950 				IX_EEPROM_MEDIA_TP)
    951 			media = IFM_ETHER | IFM_10_T;
    952 		else
    953 			media = IFM_ETHER | IFM_10_2;
    954 	} else
    955 		media = IFM_ETHER | IFM_10_5;
    956 
    957 	/* Take the card out of lookback */
    958 	bart_config = bus_space_read_1(iot, ioh, IX_CONFIG);
    959 	bart_config &= ~IX_BART_LOOPBACK;
    960 	bart_config |= IX_BART_MCS16_TEST; /* inb doesn't get bit! */
    961 	bus_space_write_1(iot, ioh, IX_CONFIG, bart_config);
    962 	bart_config = bus_space_read_1(iot, ioh, IX_CONFIG);
    963 
    964 	irq_encoded = ix_read_eeprom(iot, ioh,
    965 				     IX_EEPROM_CONFIG1);
    966 	irq_encoded = (irq_encoded & IX_EEPROM_IRQ) >> IX_EEPROM_IRQ_SHIFT;
    967 
    968 	/* Enable interrupts */
    969 	bus_space_write_1(iot, ioh, IX_IRQ,
    970 			  irq_encoded | IX_IRQ_ENABLE);
    971 
    972 	/* Flush all writes to registers */
    973 	bus_space_barrier(iot, ioh, 0, ia->ia_io[0].ir_size,
    974 	    BUS_SPACE_BARRIER_WRITE);
    975 
    976 	isc->irq_encoded = irq_encoded;
    977 
    978 	i82586_attach(sc, "EtherExpress/16", ethaddr,
    979 		      ix_media, NIX_MEDIA, media);
    980 
    981 	if (isc->use_pio)
    982 		aprint_error_dev(&sc->sc_dev, "unsupported memory config, using PIO to access %d bytes of memory\n", sc->sc_msize);
    983 
    984 	isc->sc_ih = isa_intr_establish(ia->ia_ic, ia->ia_irq[0].ir_irq,
    985 	    IST_EDGE, IPL_NET, i82586_intr, sc);
    986 	if (isc->sc_ih == NULL) {
    987 		DPRINTF(("\n%s: can't establish interrupt\n",
    988 			device_xname(&sc->sc_dev)));
    989 	}
    990 }
    991 
    992 CFATTACH_DECL(ix, sizeof(struct ix_softc),
    993     ix_match, ix_attach, NULL, NULL);
    994