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