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