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