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