Home | History | Annotate | Line # | Download | only in isa
if_ec.c revision 1.16
      1 /*	$NetBSD: if_ec.c,v 1.16 2002/01/07 21:47:06 thorpej Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 1997, 1998 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
      9  * NASA Ames Research Center.
     10  *
     11  * Redistribution and use in source and binary forms, with or without
     12  * modification, are permitted provided that the following conditions
     13  * are met:
     14  * 1. Redistributions of source code must retain the above copyright
     15  *    notice, this list of conditions and the following disclaimer.
     16  * 2. Redistributions in binary form must reproduce the above copyright
     17  *    notice, this list of conditions and the following disclaimer in the
     18  *    documentation and/or other materials provided with the distribution.
     19  * 3. All advertising materials mentioning features or use of this software
     20  *    must display the following acknowledgement:
     21  *	This product includes software developed by the NetBSD
     22  *	Foundation, Inc. and its contributors.
     23  * 4. Neither the name of The NetBSD Foundation nor the names of its
     24  *    contributors may be used to endorse or promote products derived
     25  *    from this software without specific prior written permission.
     26  *
     27  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     28  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     29  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     30  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     31  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     32  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     33  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     34  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     35  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     36  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     37  * POSSIBILITY OF SUCH DAMAGE.
     38  */
     39 
     40 /*
     41  * Device driver for National Semiconductor DS8390/WD83C690 based ethernet
     42  * adapters.
     43  *
     44  * Copyright (c) 1994, 1995 Charles M. Hannum.  All rights reserved.
     45  *
     46  * Copyright (C) 1993, David Greenman.  This software may be used, modified,
     47  * copied, distributed, and sold, in both source and binary form provided that
     48  * the above copyright and these terms are retained.  Under no circumstances is
     49  * the author responsible for the proper functioning of this software, nor does
     50  * the author assume any responsibility for damages incurred with its use.
     51  */
     52 
     53 /*
     54  * Device driver for the 3Com Etherlink II (3c503).
     55  */
     56 
     57 #include <sys/cdefs.h>
     58 __KERNEL_RCSID(0, "$NetBSD: if_ec.c,v 1.16 2002/01/07 21:47:06 thorpej Exp $");
     59 
     60 #include <sys/param.h>
     61 #include <sys/systm.h>
     62 #include <sys/device.h>
     63 #include <sys/socket.h>
     64 #include <sys/mbuf.h>
     65 #include <sys/syslog.h>
     66 
     67 #include <net/if.h>
     68 #include <net/if_dl.h>
     69 #include <net/if_types.h>
     70 #include <net/if_media.h>
     71 
     72 #include <net/if_ether.h>
     73 
     74 #include <machine/bus.h>
     75 #include <machine/intr.h>
     76 
     77 #include <dev/isa/isareg.h>
     78 #include <dev/isa/isavar.h>
     79 
     80 #include <dev/ic/dp8390reg.h>
     81 #include <dev/ic/dp8390var.h>
     82 
     83 #include <dev/isa/if_ecreg.h>
     84 
     85 struct ec_softc {
     86 	struct dp8390_softc sc_dp8390;
     87 
     88 	bus_space_tag_t sc_asict;	/* space tag for ASIC */
     89 	bus_space_handle_t sc_asich;	/* space handle for ASIC */
     90 
     91 	int sc_16bitp;			/* are we 16 bit? */
     92 
     93 	void *sc_ih;			/* interrupt handle */
     94 };
     95 
     96 int	ec_probe __P((struct device *, struct cfdata *, void *));
     97 void	ec_attach __P((struct device *, struct device *, void *));
     98 
     99 struct cfattach ec_ca = {
    100 	sizeof(struct ec_softc), ec_probe, ec_attach
    101 };
    102 
    103 int	ec_set_media __P((struct ec_softc *, int));
    104 
    105 void	ec_media_init __P((struct dp8390_softc *));
    106 
    107 int	ec_mediachange __P((struct dp8390_softc *));
    108 void	ec_mediastatus __P((struct dp8390_softc *, struct ifmediareq *));
    109 
    110 void	ec_init_card __P((struct dp8390_softc *));
    111 int	ec_write_mbuf __P((struct dp8390_softc *, struct mbuf *, int));
    112 int	ec_ring_copy __P((struct dp8390_softc *, int, caddr_t, u_short));
    113 void	ec_read_hdr __P((struct dp8390_softc *, int, struct dp8390_ring *));
    114 int	ec_fake_test_mem __P((struct dp8390_softc *));
    115 int	ec_test_mem __P((struct dp8390_softc *));
    116 
    117 __inline void ec_readmem __P((struct ec_softc *, int, u_int8_t *, int));
    118 
    119 static const int ec_iobase[] = {
    120 	0x2e0, 0x2a0, 0x280, 0x250, 0x350, 0x330, 0x310, 0x300,
    121 };
    122 #define	NEC_IOBASE	(sizeof(ec_iobase) / sizeof(ec_iobase[0]))
    123 
    124 static const int ec_membase[] = {
    125 	ISACF_IOMEM_DEFAULT, ISACF_IOMEM_DEFAULT, ISACF_IOMEM_DEFAULT,
    126 	ISACF_IOMEM_DEFAULT, 0xc8000, 0xcc000, 0xd8000, 0xdc000,
    127 };
    128 #define	NEC_MEMBASE	(sizeof(ec_membase) / sizeof(ec_membase[0]))
    129 
    130 int
    131 ec_probe(parent, match, aux)
    132 	struct device *parent;
    133 	struct cfdata *match;
    134 	void *aux;
    135 {
    136 	struct isa_attach_args *ia = aux;
    137 	bus_space_tag_t nict, asict, memt;
    138 	bus_space_handle_t nich, asich, memh;
    139 	bus_size_t memsize;
    140 	int nich_valid, asich_valid, memh_valid;
    141 	int i, rv = 0;
    142 	u_int8_t x;
    143 
    144 	nict = asict = ia->ia_iot;
    145 	memt = ia->ia_memt;
    146 
    147 	nich_valid = asich_valid = memh_valid = 0;
    148 
    149 	/*
    150 	 * Hmm, a 16-bit card has 16k of memory, but only an 8k window
    151 	 * to it.
    152 	 */
    153 	memsize = 8192;
    154 
    155 	if (ia->ia_nio < 1)
    156 		return (0);
    157 	if (ia->ia_niomem < 1)
    158 		return (0);
    159 	if (ia->ia_nirq < 1)
    160 		return (0);
    161 
    162 	if (ISA_DIRECT_CONFIG(ia))
    163 		return (0);
    164 
    165 	/* Disallow wildcarded i/o addresses. */
    166 	if (ia->ia_io[0].ir_addr == ISACF_PORT_DEFAULT)
    167 		return (0);
    168 
    169 	/* Disallow wildcarded mem address. */
    170 	if (ia->ia_iomem[0].ir_addr == ISACF_IOMEM_DEFAULT)
    171 		return (0);
    172 
    173 	/* Validate the i/o base. */
    174 	for (i = 0; i < NEC_IOBASE; i++)
    175 		if (ia->ia_io[0].ir_addr == ec_iobase[i])
    176 			break;
    177 	if (i == NEC_IOBASE)
    178 		return (0);
    179 
    180 	/* Validate the mem base. */
    181 	for (i = 0; i < NEC_MEMBASE; i++) {
    182 		if (ec_membase[i] == ISACF_IOMEM_DEFAULT)
    183 			continue;
    184 		if (ia->ia_iomem[0].ir_addr == ec_membase[i])
    185 			break;
    186 	}
    187 	if (i == NEC_MEMBASE)
    188 		return (0);
    189 
    190 	/* Attempt to map the NIC space. */
    191 	if (bus_space_map(nict, ia->ia_io[0].ir_addr + ELINK2_NIC_OFFSET,
    192 	    ELINK2_NIC_PORTS, 0, &nich))
    193 		goto out;
    194 	nich_valid = 1;
    195 
    196 	/* Attempt to map the ASIC space. */
    197 	if (bus_space_map(asict, ia->ia_io[0].ir_addr + ELINK2_ASIC_OFFSET,
    198 	    ELINK2_ASIC_PORTS, 0, &asich))
    199 		goto out;
    200 	asich_valid = 1;
    201 
    202 	/* Attempt to map the memory space. */
    203 	if (bus_space_map(memt, ia->ia_iomem[0].ir_addr, memsize, 0, &memh))
    204 		goto out;
    205 	memh_valid = 1;
    206 
    207 	/*
    208 	 * Verify that the kernel configured I/O address matches the
    209 	 * board configured I/O address.
    210 	 *
    211 	 * This is really only useful to see if something that looks like
    212 	 * the board is there; after all, we're already talking to it at
    213 	 * this point.
    214 	 */
    215 	x = bus_space_read_1(asict, asich, ELINK2_BCFR);
    216 	if (x == 0 || (x & (x - 1)) != 0)
    217 		goto out;
    218 	i = ffs(x) - 1;
    219 	if (ia->ia_io[0].ir_addr != ec_iobase[i])
    220 		goto out;
    221 
    222 	/*
    223 	 * ...and for the memory address.  Note we do not support
    224 	 * cards configured with shared memory disabled.
    225 	 */
    226 	x = bus_space_read_1(asict, asich, ELINK2_PCFR);
    227 	if (x == 0 || (x & (x - 1)) != 0)
    228 		goto out;
    229 	i = ffs(x) - 1;
    230 	if (ia->ia_iomem[0].ir_addr != ec_membase[i])
    231 		goto out;
    232 
    233 	/* So, we say we've found it! */
    234 	ia->ia_nio = 1;		/* XXX Really 2! */
    235 	ia->ia_io[0].ir_size = ELINK2_NIC_PORTS;
    236 
    237 	ia->ia_niomem = 1;
    238 	ia->ia_iomem[0].ir_size = memsize;
    239 
    240 	ia->ia_nirq = 1;
    241 
    242 	ia->ia_ndrq = 0;
    243 
    244 	rv = 1;
    245 
    246  out:
    247 	if (nich_valid)
    248 		bus_space_unmap(nict, nich, ELINK2_NIC_PORTS);
    249 	if (asich_valid)
    250 		bus_space_unmap(asict, asich, ELINK2_ASIC_PORTS);
    251 	if (memh_valid)
    252 		bus_space_unmap(memt, memh, memsize);
    253 	return (rv);
    254 }
    255 
    256 void
    257 ec_attach(parent, self, aux)
    258 	struct device *parent, *self;
    259 	void *aux;
    260 {
    261 	struct ec_softc *esc = (struct ec_softc *)self;
    262 	struct dp8390_softc *sc = &esc->sc_dp8390;
    263 	struct isa_attach_args *ia = aux;
    264 	bus_space_tag_t nict, asict, memt;
    265 	bus_space_handle_t nich, asich, memh;
    266 	bus_size_t memsize;
    267 	u_int8_t tmp;
    268 	int i;
    269 
    270 	printf("\n");
    271 
    272 	nict = asict = ia->ia_iot;
    273 	memt = ia->ia_memt;
    274 
    275 	/*
    276 	 * Hmm, a 16-bit card has 16k of memory, but only an 8k window
    277 	 * to it.
    278 	 */
    279 	memsize = ia->ia_iomem[0].ir_size;
    280 
    281 	/* Map the NIC space. */
    282 	if (bus_space_map(nict, ia->ia_io[0].ir_addr + ELINK2_NIC_OFFSET,
    283 	    ELINK2_NIC_PORTS, 0, &nich)) {
    284 		printf("%s: can't map nic i/o space\n",
    285 		    sc->sc_dev.dv_xname);
    286 		return;
    287 	}
    288 
    289 	/* Map the ASIC space. */
    290 	if (bus_space_map(asict, ia->ia_io[0].ir_addr + ELINK2_ASIC_OFFSET,
    291 	    ELINK2_ASIC_PORTS, 0, &asich)) {
    292 		printf("%s: can't map asic i/o space\n",
    293 		    sc->sc_dev.dv_xname);
    294 		return;
    295 	}
    296 
    297 	/* Map the memory space. */
    298 	if (bus_space_map(memt, ia->ia_iomem[0].ir_addr, memsize, 0, &memh)) {
    299 		printf("%s: can't map shared memory\n",
    300 		    sc->sc_dev.dv_xname);
    301 		return;
    302 	}
    303 
    304 	esc->sc_asict = asict;
    305 	esc->sc_asich = asich;
    306 
    307 	sc->sc_regt = nict;
    308 	sc->sc_regh = nich;
    309 
    310 	sc->sc_buft = memt;
    311 	sc->sc_bufh = memh;
    312 
    313 	/* Interface is always enabled. */
    314 	sc->sc_enabled = 1;
    315 
    316 	/* Registers are linear. */
    317 	for (i = 0; i < 16; i++)
    318 		sc->sc_reg_map[i] = i;
    319 
    320 	/* Now we can use the NIC_{GET,PUT}() macros. */
    321 
    322 	/*
    323 	 * Reset NIC and ASIC.  Enable on-board transeiver throughout
    324 	 * reset sequence since it will lock up if the cable isn't
    325 	 * connected if we don't.
    326 	 */
    327 	bus_space_write_1(asict, asich, ELINK2_CR,
    328 	    ELINK2_CR_RST | ELINK2_CR_XSEL);
    329 
    330 	/* Wait for a while, then un-reset it. */
    331 	delay(50);
    332 
    333 	/*
    334 	 * The 3Com ASIC defaults to rather strange settings for the CR
    335 	 * after a reset.  It's important to set it again after the
    336 	 * following write (this is done when we map the PROM below).
    337 	 */
    338 	bus_space_write_1(asict, asich, ELINK2_CR, ELINK2_CR_XSEL);
    339 
    340 	/* Wait a bit for the NIC to recover from the reset. */
    341 	delay(5000);
    342 
    343 	/*
    344 	 * Get the station address from on-board ROM.
    345 	 *
    346 	 * First, map Ethernet address PROM over the top of where the NIC
    347 	 * registers normally appear.
    348 	 */
    349 	bus_space_write_1(asict, asich, ELINK2_CR,
    350 	    ELINK2_CR_XSEL | ELINK2_CR_EALO);
    351 
    352 	for (i = 0; i < ETHER_ADDR_LEN; i++)
    353 		sc->sc_enaddr[i] = NIC_GET(nict, nich, i);
    354 
    355 	/*
    356 	 * Unmap PROM - select NIC registers.  The proper setting of the
    357 	 * transceiver is set in later in ec_init_card() via dp8390_init().
    358 	 */
    359 	bus_space_write_1(asict, asich, ELINK2_CR, ELINK2_CR_XSEL);
    360 
    361 	/* Determine if this is an 8-bit or 16-bit board. */
    362 
    363 	/* Select page 0 registers. */
    364 	NIC_PUT(nict, nich, ED_P0_CR, ED_CR_RD2 | ED_CR_PAGE_0 | ED_CR_STP);
    365 
    366 	/*
    367 	 * Attempt to clear WTS.  If it doesn't clear, then this is a
    368 	 * 16-bit board.
    369 	 */
    370 	NIC_PUT(nict, nich, ED_P0_DCR, 0);
    371 
    372 	/* Select page 2 registers. */
    373 	NIC_PUT(nict, nich, ED_P0_CR, ED_CR_RD2 | ED_CR_PAGE_2 | ED_CR_STP);
    374 
    375 	/* The 3c503 forces the WTS bit to a one if this is a 16-bit board. */
    376 	if (NIC_GET(nict, nich, ED_P2_DCR) & ED_DCR_WTS)
    377 		esc->sc_16bitp = 1;
    378 	else
    379 		esc->sc_16bitp = 0;
    380 
    381 	printf("%s: 3Com 3c503 Ethernet (%s-bit)\n",
    382 	    sc->sc_dev.dv_xname, esc->sc_16bitp ? "16" : "8");
    383 
    384 	/* Select page 0 registers. */
    385 	NIC_PUT(nict, nich, ED_P2_CR, ED_CR_RD2 | ED_CR_PAGE_0 | ED_CR_STP);
    386 
    387 	sc->cr_proto = ED_CR_RD2;
    388 
    389 	/*
    390 	 * DCR gets:
    391 	 *
    392 	 *	FIFO threshold to 8, No auto-init Remote DMA,
    393 	 *	byte order=80x86.
    394 	 *
    395 	 * 16-bit cards also get word-wide DMA transfers.
    396 	 */
    397 	sc->dcr_reg = ED_DCR_FT1 | ED_DCR_LS |
    398 	    (esc->sc_16bitp ? ED_DCR_WTS : 0);
    399 
    400 	sc->test_mem = ec_fake_test_mem;
    401 	sc->ring_copy = ec_ring_copy;
    402 	sc->write_mbuf = ec_write_mbuf;
    403 	sc->read_hdr = ec_read_hdr;
    404 	sc->init_card = ec_init_card;
    405 
    406 	sc->sc_media_init = ec_media_init;
    407 
    408 	sc->sc_mediachange = ec_mediachange;
    409 	sc->sc_mediastatus = ec_mediastatus;
    410 
    411 	sc->mem_start = 0;
    412 	sc->mem_size = memsize;
    413 
    414 	/* Do generic parts of attach. */
    415 	if (dp8390_config(sc)) {
    416 		printf("%s: configuration failed\n", sc->sc_dev.dv_xname);
    417 		return;
    418 	}
    419 
    420 	/*
    421 	 * We need to override the way dp8390_config() set up our
    422 	 * shared memory.
    423 	 *
    424 	 * We have an entire 8k window to put the transmit buffers on the
    425 	 * 16-bit boards.  But since the 16bit 3c503's shared memory is only
    426 	 * fast enough to overlap the loading of one full-size packet, trying
    427 	 * to load more than 2 buffers can actually leave the transmitter idle
    428 	 * during the load.  So 2 seems the best value.  (Although a mix of
    429 	 * variable-sized packets might change this assumption.  Nonetheless,
    430 	 * we optimize for linear transfers of same-size packets.)
    431 	 */
    432 	if (esc->sc_16bitp) {
    433 		if (sc->sc_dev.dv_cfdata->cf_flags & DP8390_NO_MULTI_BUFFERING)
    434 			sc->txb_cnt = 1;
    435 		else
    436 			sc->txb_cnt = 2;
    437 
    438 		sc->tx_page_start = ELINK2_TX_PAGE_OFFSET_16BIT;
    439 		sc->rec_page_start = ELINK2_RX_PAGE_OFFSET_16BIT;
    440 		sc->rec_page_stop = (memsize >> ED_PAGE_SHIFT) +
    441 		    sc->rec_page_start;
    442 		sc->mem_ring = sc->mem_start;
    443 	} else {
    444 		sc->txb_cnt = 1;
    445 		sc->tx_page_start = ELINK2_TX_PAGE_OFFSET_8BIT;
    446 		sc->rec_page_start = sc->tx_page_start + ED_TXBUF_SIZE;
    447 		sc->rec_page_stop = (memsize >> ED_PAGE_SHIFT) +
    448 		    sc->tx_page_start;
    449 		sc->mem_ring = sc->mem_start +
    450 		    (ED_TXBUF_SIZE << ED_PAGE_SHIFT);
    451 	}
    452 
    453 	/*
    454 	 * Initialize CA page start/stop registers.  Probably only needed
    455 	 * if doing DMA, but what the Hell.
    456 	 */
    457 	bus_space_write_1(asict, asich, ELINK2_PSTR, sc->rec_page_start);
    458 	bus_space_write_1(asict, asich, ELINK2_PSPR, sc->rec_page_stop);
    459 
    460 	/*
    461 	 * Program the IRQ.
    462 	 */
    463 	switch (ia->ia_irq[0].ir_irq) {
    464 	case 9:	tmp = ELINK2_IDCFR_IRQ2; break;
    465 	case 3:	tmp = ELINK2_IDCFR_IRQ3; break;
    466 	case 4:	tmp = ELINK2_IDCFR_IRQ4; break;
    467 	case 5:	tmp = ELINK2_IDCFR_IRQ5; break;
    468 		break;
    469 
    470 	case ISACF_IRQ_DEFAULT:
    471 		printf("%s: wildcarded IRQ is not allowed\n",
    472 		    sc->sc_dev.dv_xname);
    473 		return;
    474 
    475 	default:
    476 		printf("%s: invalid IRQ %d, must be 3, 4, 5, or 9\n",
    477 		    sc->sc_dev.dv_xname, ia->ia_irq[0].ir_irq);
    478 		return;
    479 	}
    480 
    481 	bus_space_write_1(asict, asich, ELINK2_IDCFR, tmp);
    482 
    483 	/*
    484 	 * Initialize the GA configuration register.  Set bank and enable
    485 	 * shared memory.
    486 	 */
    487 	bus_space_write_1(asict, asich, ELINK2_GACFR,
    488 	    ELINK2_GACFR_RSEL | ELINK2_GACFR_MBS0);
    489 
    490 	/*
    491 	 * Intialize "Vector Pointer" registers.  These gawd-awful things
    492 	 * are compared to 20 bits of the address on the ISA, and if they
    493 	 * match, the shared memory is disabled.  We se them to 0xffff0...
    494 	 * allegedly the reset vector.
    495 	 */
    496 	bus_space_write_1(asict, asich, ELINK2_VPTR2, 0xff);
    497 	bus_space_write_1(asict, asich, ELINK2_VPTR1, 0xff);
    498 	bus_space_write_1(asict, asich, ELINK2_VPTR0, 0x00);
    499 
    500 	/*
    501 	 * Now run the real memory test.
    502 	 */
    503 	if (ec_test_mem(sc)) {
    504 		printf("%s: memory test failed\n", sc->sc_dev.dv_xname);
    505 		return;
    506 	}
    507 
    508 	/* Establish interrupt handler. */
    509 	esc->sc_ih = isa_intr_establish(ia->ia_ic, ia->ia_irq[0].ir_irq,
    510 	    IST_EDGE, IPL_NET, dp8390_intr, sc);
    511 	if (esc->sc_ih == NULL)
    512 		printf("%s: can't establish interrupt\n", sc->sc_dev.dv_xname);
    513 }
    514 
    515 int
    516 ec_fake_test_mem(sc)
    517 	struct dp8390_softc *sc;
    518 {
    519 
    520 	/*
    521 	 * We have to do this after we initialize the GA, but we
    522 	 * have to do that after calling dp8390_config(), which
    523 	 * wants to test memory.  Put this noop here, and then
    524 	 * actually test memory later.
    525 	 */
    526 	return (0);
    527 }
    528 
    529 int
    530 ec_test_mem(sc)
    531 	struct dp8390_softc *sc;
    532 {
    533 	struct ec_softc *esc = (struct ec_softc *)sc;
    534 	bus_space_tag_t memt = sc->sc_buft;
    535 	bus_space_handle_t memh = sc->sc_bufh;
    536 	bus_size_t memsize = sc->mem_size;
    537 	int i;
    538 
    539 	if (esc->sc_16bitp)
    540 		bus_space_set_region_2(memt, memh, 0, 0, memsize >> 1);
    541 	else
    542 		bus_space_set_region_1(memt, memh, 0, 0, memsize);
    543 
    544 	if (esc->sc_16bitp) {
    545 		for (i = 0; i < memsize; i += 2) {
    546 			if (bus_space_read_2(memt, memh, i) != 0)
    547 				goto fail;
    548 		}
    549 	} else {
    550 		for (i = 0; i < memsize; i++) {
    551 			if (bus_space_read_1(memt, memh, i) != 0)
    552 				goto fail;
    553 		}
    554 	}
    555 
    556 	return (0);
    557 
    558  fail:
    559 	printf("%s: failed to clear shared memory at offset 0x%x\n",
    560 	    sc->sc_dev.dv_xname, i);
    561 	return (1);
    562 }
    563 
    564 /*
    565  * Given a NIC memory source address and a host memory destination address,
    566  * copy 'len' from NIC to host using shared memory.  The 'len' is rounded
    567  * up to a word - ok as long as mbufs are word-sized.
    568  */
    569 __inline void
    570 ec_readmem(esc, from, to, len)
    571 	struct ec_softc *esc;
    572 	int from;
    573 	u_int8_t *to;
    574 	int len;
    575 {
    576 	bus_space_tag_t memt = esc->sc_dp8390.sc_buft;
    577 	bus_space_handle_t memh = esc->sc_dp8390.sc_bufh;
    578 
    579 	if (len & 1)
    580 		++len;
    581 
    582 	if (esc->sc_16bitp)
    583 		bus_space_read_region_2(memt, memh, from, (u_int16_t *)to,
    584 		    len >> 1);
    585 	else
    586 		bus_space_read_region_1(memt, memh, from, to, len);
    587 }
    588 
    589 int
    590 ec_write_mbuf(sc, m, buf)
    591 	struct dp8390_softc *sc;
    592 	struct mbuf *m;
    593 	int buf;
    594 {
    595 	struct ec_softc *esc = (struct ec_softc *)sc;
    596 	bus_space_tag_t asict = esc->sc_asict;
    597 	bus_space_handle_t asich = esc->sc_asich;
    598 	bus_space_tag_t memt = esc->sc_dp8390.sc_buft;
    599 	bus_space_handle_t memh = esc->sc_dp8390.sc_bufh;
    600 	u_int8_t *data, savebyte[2];
    601 	int savelen, len, leftover;
    602 #ifdef DIAGNOSTIC
    603 	u_int8_t *lim;
    604 #endif
    605 
    606 	savelen = m->m_pkthdr.len;
    607 
    608 	/*
    609 	 * 8-bit boards are simple: we're already in the correct
    610 	 * page, and no alignment tricks are necessary.
    611 	 */
    612 	if (esc->sc_16bitp == 0) {
    613 		for (; m != NULL; buf += m->m_len, m = m->m_next)
    614 			bus_space_write_region_1(memt, memh, buf,
    615 			    mtod(m, u_int8_t *), m->m_len);
    616 		return (savelen);
    617 	}
    618 
    619 	/*
    620 	 * If it's a 16-bit board, we have transmit buffers
    621 	 * in a different page; switch to it.
    622 	 */
    623 	if (esc->sc_16bitp)
    624 		bus_space_write_1(asict, asich, ELINK2_GACFR,
    625 		    ELINK2_GACFR_RSEL);
    626 
    627 	/* Start out with no leftover data. */
    628 	leftover = 0;
    629 	savebyte[0] = savebyte[1] = 0;
    630 
    631 	for (; m != NULL; m = m->m_next) {
    632 		len = m->m_len;
    633 		if (len == 0)
    634 			continue;
    635 		data = mtod(m, u_int8_t *);
    636 #ifdef DIAGNOSTIC
    637 		lim = data + len;
    638 #endif
    639 		while (len > 0) {
    640 			if (leftover) {
    641 				/*
    642 				 * Data left over (from mbuf or realignment).
    643 				 * Buffer the next byte, and write it and
    644 				 * the leftover data out.
    645 				 */
    646 				savebyte[1] = *data++;
    647 				len--;
    648 				bus_space_write_2(memt, memh, buf,
    649 				    *(u_int16_t *)savebyte);
    650 				buf += 2;
    651 				leftover = 0;
    652 			} else if (BUS_SPACE_ALIGNED_POINTER(data, u_int16_t)
    653 				   == 0) {
    654 				/*
    655 				 * Unaligned data; buffer the next byte.
    656 				 */
    657 				savebyte[0] = *data++;
    658 				len--;
    659 				leftover = 1;
    660 			} else {
    661 				/*
    662 				 * Aligned data; output contiguous words as
    663 				 * much as we can, then buffer the remaining
    664 				 * byte, if any.
    665 				 */
    666 				leftover = len & 1;
    667 				len &= ~1;
    668 				bus_space_write_region_2(memt, memh, buf,
    669 				    (u_int16_t *)data, len >> 1);
    670 				data += len;
    671 				buf += len;
    672 				if (leftover)
    673 					savebyte[0] = *data++;
    674 				len = 0;
    675 			}
    676 		}
    677 		if (len < 0)
    678 			panic("ec_write_mbuf: negative len");
    679 #ifdef DIAGNOSTIC
    680 		if (data != lim)
    681 			panic("ec_write_mbuf: data != lim");
    682 #endif
    683 	}
    684 	if (leftover) {
    685 		savebyte[1] = 0;
    686 		bus_space_write_2(memt, memh, buf, *(u_int16_t *)savebyte);
    687 	}
    688 
    689 	/*
    690 	 * Switch back to receive page.
    691 	 */
    692 	if (esc->sc_16bitp)
    693 		bus_space_write_1(asict, asich, ELINK2_GACFR,
    694 		    ELINK2_GACFR_RSEL | ELINK2_GACFR_MBS0);
    695 
    696 	return (savelen);
    697 }
    698 
    699 int
    700 ec_ring_copy(sc, src, dst, amount)
    701 	struct dp8390_softc *sc;
    702 	int src;
    703 	caddr_t dst;
    704 	u_short amount;
    705 {
    706 	struct ec_softc *esc = (struct ec_softc *)sc;
    707 	u_short tmp_amount;
    708 
    709 	/* Does copy wrap to lower addr in ring buffer? */
    710 	if (src + amount > sc->mem_end) {
    711 		tmp_amount = sc->mem_end - src;
    712 
    713 		/* Copy amount up to end of NIC memory. */
    714 		ec_readmem(esc, src, dst, tmp_amount);
    715 
    716 		amount -= tmp_amount;
    717 		src = sc->mem_ring;
    718 		dst += tmp_amount;
    719 	}
    720 
    721 	ec_readmem(esc, src, dst, amount);
    722 
    723 	return (src + amount);
    724 }
    725 
    726 void
    727 ec_read_hdr(sc, packet_ptr, packet_hdrp)
    728 	struct dp8390_softc *sc;
    729 	int packet_ptr;
    730 	struct dp8390_ring *packet_hdrp;
    731 {
    732 	struct ec_softc *esc = (struct ec_softc *)sc;
    733 
    734 	ec_readmem(esc, packet_ptr, (u_int8_t *)packet_hdrp,
    735 	    sizeof(struct dp8390_ring));
    736 #if BYTE_ORDER == BIG_ENDIAN
    737 	packet_hdrp->count = bswap16(packet_hdrp->count);
    738 #endif
    739 }
    740 
    741 void
    742 ec_media_init(struct dp8390_softc *sc)
    743 {
    744 
    745 	ifmedia_init(&sc->sc_media, 0, dp8390_mediachange, dp8390_mediastatus);
    746 	ifmedia_add(&sc->sc_media, IFM_ETHER|IFM_10_2, 0, NULL);
    747 	ifmedia_add(&sc->sc_media, IFM_ETHER|IFM_10_5, 0, NULL);
    748 	ifmedia_set(&sc->sc_media, IFM_ETHER|IFM_10_2);
    749 }
    750 
    751 int
    752 ec_mediachange(sc)
    753 	struct dp8390_softc *sc;
    754 {
    755 	struct ec_softc *esc = (struct ec_softc *)sc;
    756 	struct ifmedia *ifm = &sc->sc_media;
    757 
    758 	return (ec_set_media(esc, ifm->ifm_media));
    759 }
    760 
    761 void
    762 ec_mediastatus(sc, ifmr)
    763 	struct dp8390_softc *sc;
    764 	struct ifmediareq *ifmr;
    765 {
    766 	struct ifmedia *ifm = &sc->sc_media;
    767 
    768 	/*
    769 	 * The currently selected media is always the active media.
    770 	 */
    771 	ifmr->ifm_active = ifm->ifm_cur->ifm_media;
    772 }
    773 
    774 void
    775 ec_init_card(sc)
    776 	struct dp8390_softc *sc;
    777 {
    778 	struct ec_softc *esc = (struct ec_softc *)sc;
    779 	struct ifmedia *ifm = &sc->sc_media;
    780 
    781 	(void) ec_set_media(esc, ifm->ifm_cur->ifm_media);
    782 }
    783 
    784 int
    785 ec_set_media(esc, media)
    786 	struct ec_softc *esc;
    787 	int media;
    788 {
    789 	u_int8_t new;
    790 
    791 	if (IFM_TYPE(media) != IFM_ETHER)
    792 		return (EINVAL);
    793 
    794 	switch (IFM_SUBTYPE(media)) {
    795 	case IFM_10_2:
    796 		new = ELINK2_CR_XSEL;
    797 		break;
    798 
    799 	case IFM_10_5:
    800 		new = 0;
    801 		break;
    802 
    803 	default:
    804 		return (EINVAL);
    805 	}
    806 
    807 	bus_space_write_1(esc->sc_asict, esc->sc_asich, ELINK2_CR, new);
    808 	return (0);
    809 }
    810