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