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