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