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