Home | History | Annotate | Line # | Download | only in ic
ne2000.c revision 1.26
      1 /*	$NetBSD: ne2000.c,v 1.26 2000/02/09 14:42:35 enami 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  * Common code shared by all NE2000-compatible Ethernet interfaces.
     55  */
     56 
     57 #include <sys/param.h>
     58 #include <sys/systm.h>
     59 #include <sys/device.h>
     60 #include <sys/socket.h>
     61 #include <sys/mbuf.h>
     62 #include <sys/syslog.h>
     63 
     64 #include <net/if.h>
     65 #include <net/if_dl.h>
     66 #include <net/if_types.h>
     67 #include <net/if_media.h>
     68 
     69 #include <net/if_ether.h>
     70 
     71 #include <machine/bswap.h>
     72 #include <machine/bus.h>
     73 
     74 #ifndef __BUS_SPACE_HAS_STREAM_METHODS
     75 #define	bus_space_write_stream_2	bus_space_write_2
     76 #define	bus_space_write_multi_stream_2	bus_space_write_multi_2
     77 #define	bus_space_read_multi_stream_2	bus_space_read_multi_2
     78 #endif /* __BUS_SPACE_HAS_STREAM_METHODS */
     79 
     80 #include <dev/ic/dp8390reg.h>
     81 #include <dev/ic/dp8390var.h>
     82 
     83 #include <dev/ic/ne2000reg.h>
     84 #include <dev/ic/ne2000var.h>
     85 
     86 #if BYTE_ORDER == BIG_ENDIAN
     87 #include <machine/bswap.h>
     88 #endif
     89 
     90 int	ne2000_write_mbuf __P((struct dp8390_softc *, struct mbuf *, int));
     91 int	ne2000_ring_copy __P((struct dp8390_softc *, int, caddr_t, u_short));
     92 void	ne2000_read_hdr __P((struct dp8390_softc *, int, struct dp8390_ring *));
     93 int	ne2000_test_mem __P((struct dp8390_softc *));
     94 
     95 void	ne2000_writemem __P((bus_space_tag_t, bus_space_handle_t,
     96 	    bus_space_tag_t, bus_space_handle_t, u_int8_t *, int, size_t,
     97 	    int, int));
     98 void	ne2000_readmem __P((bus_space_tag_t, bus_space_handle_t,
     99 	    bus_space_tag_t, bus_space_handle_t, int, u_int8_t *, size_t, int));
    100 
    101 int
    102 ne2000_attach(nsc, myea, media, nmedia, defmedia)
    103 	struct ne2000_softc *nsc;
    104 	u_int8_t *myea;
    105 	int *media, nmedia, defmedia;
    106 {
    107 	struct dp8390_softc *dsc = &nsc->sc_dp8390;
    108 	bus_space_tag_t nict = dsc->sc_regt;
    109 	bus_space_handle_t nich = dsc->sc_regh;
    110 	bus_space_tag_t asict = nsc->sc_asict;
    111 	bus_space_handle_t asich = nsc->sc_asich;
    112 	u_int8_t romdata[16];
    113 	int memsize, i, useword;
    114 
    115 	/*
    116 	 * Detect it again unless caller specified it; this gives us
    117 	 * the memory size.
    118 	 */
    119 	if (nsc->sc_type == 0) {
    120 		nsc->sc_type = ne2000_detect(nict, nich, asict, asich);
    121 		if (nsc->sc_type == 0) {
    122 			printf("%s: where did the card go?\n",
    123 			    dsc->sc_dev.dv_xname);
    124 			return (1);
    125 		}
    126 	}
    127 
    128 	useword = NE2000_USE_WORD(nsc);
    129 
    130 	dsc->cr_proto = ED_CR_RD2;
    131 
    132 	/*
    133 	 * DCR gets:
    134 	 *
    135 	 *	FIFO threshold to 8, No auto-init Remote DMA,
    136 	 *	byte order=80x86.
    137 	 *
    138 	 * NE1000 gets byte-wide DMA, NE2000 gets word-wide DMA.
    139 	 */
    140 	dsc->dcr_reg = ED_DCR_FT1 | ED_DCR_LS | (useword ? ED_DCR_WTS : 0);
    141 
    142 	dsc->test_mem = ne2000_test_mem;
    143 	dsc->ring_copy = ne2000_ring_copy;
    144 	dsc->write_mbuf = ne2000_write_mbuf;
    145 	dsc->read_hdr = ne2000_read_hdr;
    146 
    147 	/* Registers are linear. */
    148 	for (i = 0; i < 16; i++)
    149 		dsc->sc_reg_map[i] = i;
    150 
    151 	/*
    152 	 * 8k of memory for NE1000, 16k for NE2000 and 24k for the
    153 	 * card uses DL10019.
    154 	 */
    155 	switch (nsc->sc_type) {
    156 	case NE2000_TYPE_NE1000:
    157 		memsize = 8192;
    158 		break;
    159 	case NE2000_TYPE_NE2000:
    160 		memsize = 8192 * 2;
    161 		break;
    162 	case NE2000_TYPE_DL10019:
    163 		memsize = 8192 * 3;
    164 		break;
    165 	}
    166 
    167 	/*
    168 	 * NIC memory doens't start at zero on an NE board.
    169 	 * The start address is tied to the bus width.
    170 	 * (It happens to be computed the same way as mem size.)
    171 	 */
    172 	dsc->mem_start = memsize;
    173 
    174 #ifdef GWETHER
    175 	{
    176 		int x, mstart = 0;
    177 		int8_t pbuf0[ED_PAGE_SIZE], pbuf[ED_PAGE_SIZE],
    178 		    tbuf[ED_PAGE_SIZE];
    179 
    180 		for (i = 0; i < ED_PAGE_SIZE; i++)
    181 			pbuf0[i] = 0;
    182 
    183 		/* Search for the start of RAM. */
    184 		for (x = 1; x < 256; x++) {
    185 			ne2000_writemem(nict, nich, asict, asich, pbuf0,
    186 			    x << ED_PAGE_SHIFT, ED_PAGE_SIZE, useword, 0);
    187 			ne2000_readmem(nict, nich, asict, asich,
    188 			    x << ED_PAGE_SHIFT, tbuf, ED_PAGE_SIZE, useword);
    189 			if (bcmp(pbuf0, tbuf, ED_PAGE_SIZE) == 0) {
    190 				for (i = 0; i < ED_PAGE_SIZE; i++)
    191 					pbuf[i] = 255 - x;
    192 				ne2000_writemem(nict, nich, asict, asich,
    193 				    pbuf, x << ED_PAGE_SHIFT, ED_PAGE_SIZE,
    194 				    useword, 0);
    195 				ne2000_readmem(nict, nich, asict, asich,
    196 				    x << ED_PAGE_SHIFT, tbuf, ED_PAGE_SIZE,
    197 				    useword);
    198 				if (bcmp(pbuf, tbuf, ED_PAGE_SIZE) == 0) {
    199 					mstart = x << ED_PAGE_SHIFT;
    200 					memsize = ED_PAGE_SIZE;
    201 					break;
    202 				}
    203 			}
    204 		}
    205 
    206 		if (mstart == 0) {
    207 			printf("%s: cannot find start of RAM\n",
    208 			    dsc->sc_dev.dv_xname);
    209 			return (1);
    210 		}
    211 
    212 		/* Search for the end of RAM. */
    213 		for (++x; x < 256; x++) {
    214 			ne2000_writemem(nict, nich, asict, asich, pbuf0,
    215 			    x << ED_PAGE_SHIFT, ED_PAGE_SIZE, useword, 0);
    216 			ne2000_readmem(nict, nich, asict, asich,
    217 			    x << ED_PAGE_SHIFT, tbuf, ED_PAGE_SIZE, useword);
    218 			if (bcmp(pbuf0, tbuf, ED_PAGE_SIZE) == 0) {
    219 				for (i = 0; i < ED_PAGE_SIZE; i++)
    220 					pbuf[i] = 255 - x;
    221 				ne2000_writemem(nict, nich, asict, asich,
    222 				    pbuf, x << ED_PAGE_SHIFT, ED_PAGE_SIZE,
    223 				    useword, 0);
    224 				ne2000_readmem(nict, nich, asict, asich,
    225 				    x << ED_PAGE_SHIFT, tbuf, ED_PAGE_SIZE,
    226 				    useword);
    227 				if (bcmp(pbuf, tbuf, ED_PAGE_SIZE) == 0)
    228 					memsize += ED_PAGE_SIZE;
    229 				else
    230 					break;
    231 			} else
    232 				break;
    233 		}
    234 
    235 		printf("%s: RAM start 0x%x, size %d\n",
    236 		    dsc->sc_dev.dv_xname, mstart, memsize);
    237 
    238 		dsc->mem_start = mstart;
    239 	}
    240 #endif /* GWETHER */
    241 
    242 	dsc->mem_size = memsize;
    243 
    244 	if (myea == NULL) {
    245 		/* Read the station address. */
    246 		ne2000_readmem(nict, nich, asict, asich, 0, romdata,
    247 		    sizeof(romdata), useword);
    248 		for (i = 0; i < ETHER_ADDR_LEN; i++)
    249 			dsc->sc_enaddr[i] = romdata[i * (useword ? 2 : 1)];
    250 	} else
    251 		bcopy(myea, dsc->sc_enaddr, sizeof(dsc->sc_enaddr));
    252 
    253 	/* Clear any pending interrupts that might have occurred above. */
    254 	bus_space_write_1(nict, nich, ED_P0_ISR, 0xff);
    255 
    256 	if (dp8390_config(dsc, media, nmedia, defmedia)) {
    257 		printf("%s: setup failed\n", dsc->sc_dev.dv_xname);
    258 		return (1);
    259 	}
    260 
    261 	/*
    262 	 * We need to compute mem_ring a bit differently; override the
    263 	 * value set up in dp8390_config().
    264 	 */
    265 	dsc->mem_ring =
    266 	    dsc->mem_start + ((dsc->txb_cnt * ED_TXBUF_SIZE) << ED_PAGE_SHIFT);
    267 	return (0);
    268 }
    269 
    270 /*
    271  * Detect an NE-2000 or compatible.  Returns a model code.
    272  */
    273 int
    274 ne2000_detect(nict, nich, asict, asich)
    275 	bus_space_tag_t nict;
    276 	bus_space_handle_t nich;
    277 	bus_space_tag_t asict;
    278 	bus_space_handle_t asich;
    279 {
    280 	static u_int8_t test_pattern[32] = "THIS is A memory TEST pattern";
    281 	u_int8_t test_buffer[32], tmp;
    282 	int i, rv = 0;
    283 
    284 	/* Reset the board. */
    285 #ifdef GWETHER
    286 	bus_space_write_1(asict, asich, NE2000_ASIC_RESET, 0);
    287 	delay(200);
    288 #endif /* GWETHER */
    289 	tmp = bus_space_read_1(asict, asich, NE2000_ASIC_RESET);
    290 	delay(10000);
    291 
    292 	/*
    293 	 * I don't know if this is necessary; probably cruft leftover from
    294 	 * Clarkson packet driver code. Doesn't do a thing on the boards I've
    295 	 * tested. -DG [note that a outb(0x84, 0) seems to work here, and is
    296 	 * non-invasive...but some boards don't seem to reset and I don't have
    297 	 * complete documentation on what the 'right' thing to do is...so we do
    298 	 * the invasive thing for now.  Yuck.]
    299 	 */
    300 	bus_space_write_1(asict, asich, NE2000_ASIC_RESET, tmp);
    301 	delay(5000);
    302 
    303 	/*
    304 	 * This is needed because some NE clones apparently don't reset the
    305 	 * NIC properly (or the NIC chip doesn't reset fully on power-up).
    306 	 * XXX - this makes the probe invasive!  Done against my better
    307 	 * judgement.  -DLG
    308 	 */
    309 	bus_space_write_1(nict, nich, ED_P0_CR,
    310 	    ED_CR_RD2 | ED_CR_PAGE_0 | ED_CR_STP);
    311 
    312 	delay(5000);
    313 
    314 	/*
    315 	 * Generic probe routine for testing for the existance of a DS8390.
    316 	 * Must be performed  after the NIC has just been reset.  This
    317 	 * works by looking at certain register values that are guaranteed
    318 	 * to be initialized a certain way after power-up or reset.
    319 	 *
    320 	 * Specifically:
    321 	 *
    322 	 *	Register		reset bits	set bits
    323 	 *	--------		----------	--------
    324 	 *	CR			TXP, STA	RD2, STP
    325 	 *	ISR					RST
    326 	 *	IMR			<all>
    327 	 *	DCR					LAS
    328 	 *	TCR			LB1, LB0
    329 	 *
    330 	 * We only look at CR and ISR, however, since looking at the others
    331 	 * would require changing register pages, which would be intrusive
    332 	 * if this isn't an 8390.
    333 	 */
    334 
    335 	tmp = bus_space_read_1(nict, nich, ED_P0_CR);
    336 	if ((tmp & (ED_CR_RD2 | ED_CR_TXP | ED_CR_STA | ED_CR_STP)) !=
    337 	    (ED_CR_RD2 | ED_CR_STP))
    338 		goto out;
    339 
    340 	tmp = bus_space_read_1(nict, nich, ED_P0_ISR);
    341 	if ((tmp & ED_ISR_RST) != ED_ISR_RST)
    342 		goto out;
    343 
    344 	bus_space_write_1(nict, nich,
    345 	    ED_P0_CR, ED_CR_RD2 | ED_CR_PAGE_0 | ED_CR_STA);
    346 
    347 	for (i = 0; i < 100; i++) {
    348 		if ((bus_space_read_1(nict, nich, ED_P0_ISR) & ED_ISR_RST) ==
    349 		    ED_ISR_RST) {
    350 			/* Ack the reset bit. */
    351 			bus_space_write_1(nict, nich, ED_P0_ISR, ED_ISR_RST);
    352 			break;
    353 		}
    354 		delay(100);
    355 	}
    356 
    357 #if 0
    358 	/* XXX */
    359 	if (i == 100)
    360 		goto out;
    361 #endif
    362 
    363 	/*
    364 	 * Test the ability to read and write to the NIC memory.  This has
    365 	 * the side effect of determining if this is an NE1000 or an NE2000.
    366 	 */
    367 
    368 	/*
    369 	 * This prevents packets from being stored in the NIC memory when
    370 	 * the readmem routine turns on the start bit in the CR.
    371 	 */
    372 	bus_space_write_1(nict, nich, ED_P0_RCR, ED_RCR_MON);
    373 
    374 	/* Temporarily initialize DCR for byte operations. */
    375 	bus_space_write_1(nict, nich, ED_P0_DCR, ED_DCR_FT1 | ED_DCR_LS);
    376 
    377 	bus_space_write_1(nict, nich, ED_P0_PSTART, 8192 >> ED_PAGE_SHIFT);
    378 	bus_space_write_1(nict, nich, ED_P0_PSTOP, 16384 >> ED_PAGE_SHIFT);
    379 
    380 	/*
    381 	 * Write a test pattern in byte mode.  If this fails, then there
    382 	 * probably isn't any memory at 8k - which likely means that the
    383 	 * board is an NE2000.
    384 	 */
    385 	ne2000_writemem(nict, nich, asict, asich, test_pattern, 8192,
    386 	    sizeof(test_pattern), 0, 1);
    387 	ne2000_readmem(nict, nich, asict, asich, 8192, test_buffer,
    388 	    sizeof(test_buffer), 0);
    389 
    390 	if (bcmp(test_pattern, test_buffer, sizeof(test_pattern))) {
    391 		/* not an NE1000 - try NE2000 */
    392 		bus_space_write_1(nict, nich, ED_P0_DCR,
    393 		    ED_DCR_WTS | ED_DCR_FT1 | ED_DCR_LS);
    394 		bus_space_write_1(nict, nich, ED_P0_PSTART,
    395 		    16384 >> ED_PAGE_SHIFT);
    396 		bus_space_write_1(nict, nich, ED_P0_PSTOP,
    397 		    32768 >> ED_PAGE_SHIFT);
    398 
    399 		/*
    400 		 * Write the test pattern in word mode.  If this also fails,
    401 		 * then we don't know what this board is.
    402 		 */
    403 		ne2000_writemem(nict, nich, asict, asich, test_pattern, 16384,
    404 		    sizeof(test_pattern), 1, 0);
    405 		ne2000_readmem(nict, nich, asict, asich, 16384, test_buffer,
    406 		    sizeof(test_buffer), 1);
    407 
    408 		if (bcmp(test_pattern, test_buffer, sizeof(test_pattern)))
    409 			goto out;	/* not an NE2000 either */
    410 
    411 		rv = NE2000_TYPE_NE2000;
    412 	} else {
    413 		/* We're an NE1000. */
    414 		rv = NE2000_TYPE_NE1000;
    415 	}
    416 
    417 	/* Clear any pending interrupts that might have occurred above. */
    418 	bus_space_write_1(nict, nich, ED_P0_ISR, 0xff);
    419 
    420  out:
    421 	return (rv);
    422 }
    423 
    424 /*
    425  * Write an mbuf chain to the destination NIC memory address using programmed
    426  * I/O.
    427  */
    428 int
    429 ne2000_write_mbuf(sc, m, buf)
    430 	struct dp8390_softc *sc;
    431 	struct mbuf *m;
    432 	int buf;
    433 {
    434 	struct ne2000_softc *nsc = (struct ne2000_softc *)sc;
    435 	bus_space_tag_t nict = sc->sc_regt;
    436 	bus_space_handle_t nich = sc->sc_regh;
    437 	bus_space_tag_t asict = nsc->sc_asict;
    438 	bus_space_handle_t asich = nsc->sc_asich;
    439 	int savelen;
    440 	int maxwait = 100;	/* about 120us */
    441 
    442 	savelen = m->m_pkthdr.len;
    443 
    444 	/* Select page 0 registers. */
    445 	bus_space_write_1(nict, nich, ED_P0_CR,
    446 	    ED_CR_RD2 | ED_CR_PAGE_0 | ED_CR_STA);
    447 
    448 	/* Reset remote DMA complete flag. */
    449 	bus_space_write_1(nict, nich, ED_P0_ISR, ED_ISR_RDC);
    450 
    451 	/* Set up DMA byte count. */
    452 	bus_space_write_1(nict, nich, ED_P0_RBCR0, savelen);
    453 	bus_space_write_1(nict, nich, ED_P0_RBCR1, savelen >> 8);
    454 
    455 	/* Set up destination address in NIC mem. */
    456 	bus_space_write_1(nict, nich, ED_P0_RSAR0, buf);
    457 	bus_space_write_1(nict, nich, ED_P0_RSAR1, buf >> 8);
    458 
    459 	/* Set remote DMA write. */
    460 	bus_space_write_1(nict, nich,
    461 	    ED_P0_CR, ED_CR_RD1 | ED_CR_PAGE_0 | ED_CR_STA);
    462 
    463 	/*
    464 	 * Transfer the mbuf chain to the NIC memory.  NE2000 cards
    465 	 * require that data be transferred as words, and only words,
    466 	 * so that case requires some extra code to patch over odd-length
    467 	 * mbufs.
    468 	 */
    469 	if (nsc->sc_type == NE2000_TYPE_NE1000) {
    470 		/* NE1000s are easy. */
    471 		for (; m != 0; m = m->m_next) {
    472 			if (m->m_len) {
    473 				bus_space_write_multi_1(asict, asich,
    474 				    NE2000_ASIC_DATA, mtod(m, u_int8_t *),
    475 				    m->m_len);
    476 			}
    477 		}
    478 	} else {
    479 		/* NE2000s are a bit trickier. */
    480 		u_int8_t *data, savebyte[2];
    481 		int l, leftover;
    482 #ifdef DIAGNOSTIC
    483 		u_int8_t *lim;
    484 #endif
    485 		/* Start out with no leftover data. */
    486 		leftover = 0;
    487 		savebyte[0] = savebyte[1] = 0;
    488 
    489 		for (; m != 0; m = m->m_next) {
    490 			l = m->m_len;
    491 			if (l == 0)
    492 				continue;
    493 			data = mtod(m, u_int8_t *);
    494 #ifdef DIAGNOSTIC
    495 			lim = data + l;
    496 #endif
    497 			while (l > 0) {
    498 				if (leftover) {
    499 					/*
    500 					 * Data left over (from mbuf or
    501 					 * realignment).  Buffer the next
    502 					 * byte, and write it and the
    503 					 * leftover data out.
    504 					 */
    505 					savebyte[1] = *data++;
    506 					l--;
    507 					bus_space_write_stream_2(asict, asich,
    508 					    NE2000_ASIC_DATA,
    509 					    *(u_int16_t *)savebyte);
    510 					leftover = 0;
    511 				} else if (BUS_SPACE_ALIGNED_POINTER(data,
    512 					   u_int16_t) == 0) {
    513 					/*
    514 					 * Unaligned data; buffer the next
    515 					 * byte.
    516 					 */
    517 					savebyte[0] = *data++;
    518 					l--;
    519 					leftover = 1;
    520 				} else {
    521 					/*
    522 					 * Aligned data; output contiguous
    523 					 * words as much as we can, then
    524 					 * buffer the remaining byte, if any.
    525 					 */
    526 					leftover = l & 1;
    527 					l &= ~1;
    528 					bus_space_write_multi_stream_2(asict,
    529 					    asich, NE2000_ASIC_DATA,
    530 					    (u_int16_t *)data, l >> 1);
    531 					data += l;
    532 					if (leftover)
    533 						savebyte[0] = *data++;
    534 					l = 0;
    535 				}
    536 			}
    537 			if (l < 0)
    538 				panic("ne2000_write_mbuf: negative len");
    539 #ifdef DIAGNOSTIC
    540 			if (data != lim)
    541 				panic("ne2000_write_mbuf: data != lim");
    542 #endif
    543 		}
    544 		if (leftover) {
    545 			savebyte[1] = 0;
    546 			bus_space_write_stream_2(asict, asich, NE2000_ASIC_DATA,
    547 			    *(u_int16_t *)savebyte);
    548 		}
    549 	}
    550 
    551 	/*
    552 	 * Wait for remote DMA to complete.  This is necessary because on the
    553 	 * transmit side, data is handled internally by the NIC in bursts, and
    554 	 * we can't start another remote DMA until this one completes.  Not
    555 	 * waiting causes really bad things to happen - like the NIC wedging
    556 	 * the bus.
    557 	 */
    558 	while (((bus_space_read_1(nict, nich, ED_P0_ISR) & ED_ISR_RDC) !=
    559 	    ED_ISR_RDC) && --maxwait);
    560 
    561 	if (maxwait == 0) {
    562 		log(LOG_WARNING,
    563 		    "%s: remote transmit DMA failed to complete\n",
    564 		    sc->sc_dev.dv_xname);
    565 		dp8390_reset(sc);
    566 	}
    567 
    568 	return (savelen);
    569 }
    570 
    571 /*
    572  * Given a source and destination address, copy 'amout' of a packet from
    573  * the ring buffer into a linear destination buffer.  Takes into account
    574  * ring-wrap.
    575  */
    576 int
    577 ne2000_ring_copy(sc, src, dst, amount)
    578 	struct dp8390_softc *sc;
    579 	int src;
    580 	caddr_t dst;
    581 	u_short amount;
    582 {
    583 	struct ne2000_softc *nsc = (struct ne2000_softc *)sc;
    584 	bus_space_tag_t nict = sc->sc_regt;
    585 	bus_space_handle_t nich = sc->sc_regh;
    586 	bus_space_tag_t asict = nsc->sc_asict;
    587 	bus_space_handle_t asich = nsc->sc_asich;
    588 	u_short tmp_amount;
    589 	int useword = NE2000_USE_WORD(nsc);
    590 
    591 	/* Does copy wrap to lower addr in ring buffer? */
    592 	if (src + amount > sc->mem_end) {
    593 		tmp_amount = sc->mem_end - src;
    594 
    595 		/* Copy amount up to end of NIC memory. */
    596 		ne2000_readmem(nict, nich, asict, asich, src,
    597 		    (u_int8_t *)dst, tmp_amount, useword);
    598 
    599 		amount -= tmp_amount;
    600 		src = sc->mem_ring;
    601 		dst += tmp_amount;
    602 	}
    603 
    604 	ne2000_readmem(nict, nich, asict, asich, src, (u_int8_t *)dst,
    605 	    amount, useword);
    606 
    607 	return (src + amount);
    608 }
    609 
    610 void
    611 ne2000_read_hdr(sc, buf, hdr)
    612 	struct dp8390_softc *sc;
    613 	int buf;
    614 	struct dp8390_ring *hdr;
    615 {
    616 	struct ne2000_softc *nsc = (struct ne2000_softc *)sc;
    617 
    618 	ne2000_readmem(sc->sc_regt, sc->sc_regh, nsc->sc_asict, nsc->sc_asich,
    619 	    buf, (u_int8_t *)hdr, sizeof(struct dp8390_ring),
    620 	    NE2000_USE_WORD(nsc));
    621 #if BYTE_ORDER == BIG_ENDIAN
    622 	hdr->count = bswap16(hdr->count);
    623 #endif
    624 }
    625 
    626 int
    627 ne2000_test_mem(sc)
    628 	struct dp8390_softc *sc;
    629 {
    630 
    631 	/* Noop. */
    632 	return (0);
    633 }
    634 
    635 /*
    636  * Given a NIC memory source address and a host memory destination address,
    637  * copy 'amount' from NIC to host using programmed i/o.  The 'amount' is
    638  * rounded up to a word - ok as long as mbufs are word sized.
    639  */
    640 void
    641 ne2000_readmem(nict, nich, asict, asich, src, dst, amount, useword)
    642 	bus_space_tag_t nict;
    643 	bus_space_handle_t nich;
    644 	bus_space_tag_t asict;
    645 	bus_space_handle_t asich;
    646 	int src;
    647 	u_int8_t *dst;
    648 	size_t amount;
    649 	int useword;
    650 {
    651 
    652 	/* Select page 0 registers. */
    653 	bus_space_write_1(nict, nich, ED_P0_CR,
    654 	    ED_CR_RD2 | ED_CR_PAGE_0 | ED_CR_STA);
    655 
    656 	/* Round up to a word. */
    657 	if (amount & 1)
    658 		++amount;
    659 
    660 	/* Set up DMA byte count. */
    661 	bus_space_write_1(nict, nich, ED_P0_RBCR0, amount);
    662 	bus_space_write_1(nict, nich, ED_P0_RBCR1, amount >> 8);
    663 
    664 	/* Set up source address in NIC mem. */
    665 	bus_space_write_1(nict, nich, ED_P0_RSAR0, src);
    666 	bus_space_write_1(nict, nich, ED_P0_RSAR1, src >> 8);
    667 
    668 	bus_space_write_1(nict, nich, ED_P0_CR,
    669 	    ED_CR_RD0 | ED_CR_PAGE_0 | ED_CR_STA);
    670 
    671 	if (useword)
    672 		bus_space_read_multi_stream_2(asict, asich, NE2000_ASIC_DATA,
    673 		    (u_int16_t *)dst, amount >> 1);
    674 	else
    675 		bus_space_read_multi_1(asict, asich, NE2000_ASIC_DATA,
    676 		    dst, amount);
    677 }
    678 
    679 /*
    680  * Stripped down routine for writing a linear buffer to NIC memory.  Only
    681  * used in the probe routine to test the memory.  'len' must be even.
    682  */
    683 void
    684 ne2000_writemem(nict, nich, asict, asich, src, dst, len, useword, quiet)
    685 	bus_space_tag_t nict;
    686 	bus_space_handle_t nich;
    687 	bus_space_tag_t asict;
    688 	bus_space_handle_t asich;
    689 	u_int8_t *src;
    690 	int dst;
    691 	size_t len;
    692 	int useword;
    693 	int quiet;
    694 {
    695 	int maxwait = 100;	/* about 120us */
    696 
    697 	/* Select page 0 registers. */
    698 	bus_space_write_1(nict, nich, ED_P0_CR,
    699 	    ED_CR_RD2 | ED_CR_PAGE_0 | ED_CR_STA);
    700 
    701 	/* Reset remote DMA complete flag. */
    702 	bus_space_write_1(nict, nich, ED_P0_ISR, ED_ISR_RDC);
    703 
    704 	/* Set up DMA byte count. */
    705 	bus_space_write_1(nict, nich, ED_P0_RBCR0, len);
    706 	bus_space_write_1(nict, nich, ED_P0_RBCR1, len >> 8);
    707 
    708 	/* Set up destination address in NIC mem. */
    709 	bus_space_write_1(nict, nich, ED_P0_RSAR0, dst);
    710 	bus_space_write_1(nict, nich, ED_P0_RSAR1, dst >> 8);
    711 
    712 	/* Set remote DMA write. */
    713 	bus_space_write_1(nict, nich, ED_P0_CR,
    714 	    ED_CR_RD1 | ED_CR_PAGE_0 | ED_CR_STA);
    715 
    716 	if (useword)
    717 		bus_space_write_multi_stream_2(asict, asich, NE2000_ASIC_DATA,
    718 		    (u_int16_t *)src, len >> 1);
    719 	else
    720 		bus_space_write_multi_1(asict, asich, NE2000_ASIC_DATA,
    721 		    src, len);
    722 
    723 	/*
    724 	 * Wait for remote DMA to complete.  This is necessary because on the
    725 	 * transmit side, data is handled internally by the NIC in bursts, and
    726 	 * we can't start another remote DMA until this one completes.  Not
    727 	 * waiting causes really bad things to happen - like the NIC wedging
    728 	 * the bus.
    729 	 */
    730 	while (((bus_space_read_1(nict, nich, ED_P0_ISR) & ED_ISR_RDC) !=
    731 	    ED_ISR_RDC) && --maxwait);
    732 
    733 	if (!quiet && maxwait == 0)
    734 		printf("ne2000_writemem: failed to complete\n");
    735 }
    736 
    737 int
    738 ne2000_detach(sc, flags)
    739 	struct ne2000_softc *sc;
    740 	int flags;
    741 {
    742 
    743 	return (dp8390_detach(&sc->sc_dp8390, flags));
    744 }
    745