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