xref: /openbsd-src/sys/dev/ic/ne2000.c (revision a28daedfc357b214be5c701aa8ba8adb29a7f1c2)
1 /*	$OpenBSD: ne2000.c,v 1.24 2008/06/26 05:42:16 ray Exp $	*/
2 /*	$NetBSD: ne2000.c,v 1.12 1998/06/10 01:15:50 thorpej Exp $	*/
3 
4 /*-
5  * Copyright (c) 1997, 1998 The NetBSD Foundation, Inc.
6  * All rights reserved.
7  *
8  * This code is derived from software contributed to The NetBSD Foundation
9  * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
10  * NASA Ames Research Center.
11  *
12  * Redistribution and use in source and binary forms, with or without
13  * modification, are permitted provided that the following conditions
14  * are met:
15  * 1. Redistributions of source code must retain the above copyright
16  *    notice, this list of conditions and the following disclaimer.
17  * 2. Redistributions in binary form must reproduce the above copyright
18  *    notice, this list of conditions and the following disclaimer in the
19  *    documentation and/or other materials provided with the distribution.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
22  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
23  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
24  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
25  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31  * POSSIBILITY OF SUCH DAMAGE.
32  */
33 
34 /*
35  * Device driver for National Semiconductor DS8390/WD83C690 based ethernet
36  * adapters.
37  *
38  * Copyright (c) 1994, 1995 Charles M. Hannum.  All rights reserved.
39  *
40  * Copyright (C) 1993, David Greenman.  This software may be used, modified,
41  * copied, distributed, and sold, in both source and binary form provided that
42  * the above copyright and these terms are retained.  Under no circumstances is
43  * the author responsible for the proper functioning of this software, nor does
44  * the author assume any responsibility for damages incurred with its use.
45  */
46 
47 /*
48  * Common code shared by all NE2000-compatible Ethernet interfaces.
49  */
50 
51 #include <sys/param.h>
52 #include <sys/systm.h>
53 #include <sys/device.h>
54 #include <sys/socket.h>
55 #include <sys/mbuf.h>
56 #include <sys/syslog.h>
57 
58 #include <net/if.h>
59 #include <net/if_dl.h>
60 #include <net/if_types.h>
61 #include <net/if_media.h>
62 
63 #include <netinet/in.h>
64 #include <netinet/if_ether.h>
65 
66 #include <machine/bus.h>
67 
68 #include <dev/ic/dp8390reg.h>
69 #include <dev/ic/dp8390var.h>
70 
71 #include <dev/ic/ne2000reg.h>
72 #include <dev/ic/ne2000var.h>
73 
74 #include <dev/ic/ax88190reg.h>
75 
76 int	ne2000_write_mbuf(struct dp8390_softc *, struct mbuf *, int);
77 int	ne2000_ring_copy(struct dp8390_softc *, int, caddr_t, u_short);
78 void	ne2000_read_hdr(struct dp8390_softc *, int, struct dp8390_ring *);
79 int	ne2000_test_mem(struct dp8390_softc *);
80 
81 void	ne2000_writemem(bus_space_tag_t, bus_space_handle_t,
82 	    bus_space_tag_t, bus_space_handle_t, u_int8_t *, int, size_t, int);
83 void	ne2000_readmem(bus_space_tag_t, bus_space_handle_t,
84 	    bus_space_tag_t, bus_space_handle_t, int, u_int8_t *, size_t, int);
85 
86 #define ASIC_BARRIER(asict, asich) \
87 	bus_space_barrier((asict), (asich), 0, 0x10, \
88 	    BUS_SPACE_BARRIER_READ | BUS_SPACE_BARRIER_WRITE)
89 
90 struct cfdriver ne_cd = {
91 	NULL, "ne", DV_IFNET
92 };
93 
94 int
95 ne2000_attach(struct ne2000_softc *nsc, u_int8_t *myea)
96 {
97 	struct dp8390_softc *dsc = &nsc->sc_dp8390;
98 	bus_space_tag_t nict = dsc->sc_regt;
99 	bus_space_handle_t nich = dsc->sc_regh;
100 	bus_space_tag_t asict = nsc->sc_asict;
101 	bus_space_handle_t asich = nsc->sc_asich;
102 	u_int8_t romdata[16];
103 	int memsize, i, useword;
104 
105 	/*
106 	 * Detect it again unless caller specified it; this gives us
107 	 * the memory size.
108 	 */
109 	if (nsc->sc_type == NE2000_TYPE_UNKNOWN)
110 		nsc->sc_type = ne2000_detect(nsc);
111 
112 	/*
113 	 * 8k of memory for NE1000, 16k otherwise.
114 	 */
115 	switch (nsc->sc_type) {
116 	case NE2000_TYPE_UNKNOWN:
117 	default:
118 		printf(": where did the card go?\n");
119 		return (1);
120 	case NE2000_TYPE_NE1000:
121 		memsize = 8192;
122 		useword = 0;
123 		break;
124 	case NE2000_TYPE_NE2000:
125 	case NE2000_TYPE_AX88190:		/* XXX really? */
126 	case NE2000_TYPE_AX88790:
127 	case NE2000_TYPE_DL10019:
128 	case NE2000_TYPE_DL10022:
129 		memsize = 8192 * 2;
130 		useword = 1;
131 		break;
132  	}
133 
134 	nsc->sc_useword = useword;
135 
136 	dsc->cr_proto = ED_CR_RD2;
137 	if (nsc->sc_type == NE2000_TYPE_AX88190 ||
138 	    nsc->sc_type == NE2000_TYPE_AX88790) {
139 		dsc->rcr_proto = ED_RCR_INTT;
140 		dsc->sc_flags |= DP8390_DO_AX88190_WORKAROUND;
141 	} else
142 		dsc->rcr_proto = 0;
143 
144 	/*
145 	 * DCR gets:
146 	 *
147 	 *	FIFO threshold to 8, No auto-init Remote DMA,
148 	 *	byte order=80x86.
149 	 *
150 	 * NE1000 gets byte-wide DMA, NE2000 gets word-wide DMA.
151 	 */
152 	dsc->dcr_reg = ED_DCR_FT1 | ED_DCR_LS | (useword ? ED_DCR_WTS : 0);
153 
154 	dsc->test_mem = ne2000_test_mem;
155 	dsc->ring_copy = ne2000_ring_copy;
156 	dsc->write_mbuf = ne2000_write_mbuf;
157 	dsc->read_hdr = ne2000_read_hdr;
158 
159 	/* Registers are linear. */
160 	for (i = 0; i < 16; i++)
161 		dsc->sc_reg_map[i] = i;
162 
163 	/*
164 	 * NIC memory doens't start at zero on an NE board.
165 	 * The start address is tied to the bus width.
166 	 * (It happens to be computed the same way as mem size.)
167 	 */
168 	dsc->mem_start = memsize;
169 
170 #ifdef GWETHER
171 	{
172 		int x, mstart = 0;
173 		int8_t pbuf0[ED_PAGE_SIZE], pbuf[ED_PAGE_SIZE],
174 		    tbuf[ED_PAGE_SIZE];
175 
176 		for (i = 0; i < ED_PAGE_SIZE; i++)
177 			pbuf0[i] = 0;
178 
179 		/* Search for the start of RAM. */
180 		for (x = 1; x < 256; x++) {
181 			ne2000_writemem(nict, nich, asict, asich, pbuf0,
182 			    x << ED_PAGE_SHIFT, ED_PAGE_SIZE, useword);
183 			ne2000_readmem(nict, nich, asict, asich,
184 			    x << ED_PAGE_SHIFT, tbuf, ED_PAGE_SIZE, useword);
185 			if (bcmp(pbuf0, tbuf, ED_PAGE_SIZE) == 0) {
186 				for (i = 0; i < ED_PAGE_SIZE; i++)
187 					pbuf[i] = 255 - x;
188 				ne2000_writemem(nict, nich, asict, asich,
189 				    pbuf, x << ED_PAGE_SHIFT, ED_PAGE_SIZE,
190 				    useword);
191 				ne2000_readmem(nict, nich, asict, asich,
192 				    x << ED_PAGE_SHIFT, tbuf, ED_PAGE_SIZE,
193 				    useword);
194 				if (bcmp(pbuf, tbuf, ED_PAGE_SIZE) == 0) {
195 					mstart = x << ED_PAGE_SHIFT;
196 					memsize = ED_PAGE_SIZE;
197 					break;
198 				}
199 			}
200 		}
201 
202 		if (mstart == 0) {
203 			printf(": cannot find start of RAM\n");
204 			return;
205 		}
206 
207 		/* Search for the end of RAM. */
208 		for (++x; x < 256; x++) {
209 			ne2000_writemem(nict, nich, asict, asich, pbuf0,
210 			    x << ED_PAGE_SHIFT, ED_PAGE_SIZE, useword);
211 			ne2000_readmem(nict, nich, asict, asich,
212 			    x << ED_PAGE_SHIFT, tbuf, ED_PAGE_SIZE, useword);
213 			if (bcmp(pbuf0, tbuf, ED_PAGE_SIZE) == 0) {
214 				for (i = 0; i < ED_PAGE_SIZE; i++)
215 					pbuf[i] = 255 - x;
216 				ne2000_writemem(nict, nich, asict, asich,
217 				    pbuf, x << ED_PAGE_SHIFT, ED_PAGE_SIZE,
218 				    useword);
219 				ne2000_readmem(nict, nich, asict, asich,
220 				    x << ED_PAGE_SHIFT, tbuf, ED_PAGE_SIZE,
221 				    useword);
222 				if (bcmp(pbuf, tbuf, ED_PAGE_SIZE) == 0)
223 					memsize += ED_PAGE_SIZE;
224 				else
225 					break;
226 			} else
227 				break;
228 		}
229 
230 		printf(": RAM start 0x%x, size %d\n",
231 		    mstart, memsize);
232 
233 		dsc->mem_start = mstart;
234 	}
235 #endif /* GWETHER */
236 
237 	dsc->mem_size = memsize;
238 
239 	if (myea == NULL) {
240 		/* Read the station address. */
241 		if (nsc->sc_type == NE2000_TYPE_AX88190 ||
242 		    nsc->sc_type == NE2000_TYPE_AX88790) {
243 			/* Select page 0 registers. */
244 			NIC_BARRIER(nict, nich);
245 			bus_space_write_1(nict, nich, ED_P0_CR,
246 			    ED_CR_RD2 | ED_CR_PAGE_0 | ED_CR_STA);
247 			NIC_BARRIER(nict, nich);
248 			/* Select word transfer. */
249 			bus_space_write_1(nict, nich, ED_P0_DCR, ED_DCR_WTS);
250 			NIC_BARRIER(nict, nich);
251 			ne2000_readmem(nict, nich, asict, asich,
252 			    AX88190_NODEID_OFFSET, dsc->sc_arpcom.ac_enaddr,
253 			    ETHER_ADDR_LEN, useword);
254 		} else {
255 			ne2000_readmem(nict, nich, asict, asich, 0, romdata,
256 			    sizeof(romdata), useword);
257 			for (i = 0; i < ETHER_ADDR_LEN; i++)
258 				dsc->sc_arpcom.ac_enaddr[i] =
259 				    romdata[i * (useword ? 2 : 1)];
260 		}
261 	} else
262 		bcopy(myea, dsc->sc_arpcom.ac_enaddr, ETHER_ADDR_LEN);
263 
264 	/* Clear any pending interrupts that might have occurred above. */
265 	NIC_BARRIER(nict, nich);
266 	bus_space_write_1(nict, nich, ED_P0_ISR, 0xff);
267 	NIC_BARRIER(nict, nich);
268 
269 	if (dsc->sc_media_init == NULL)
270 		dsc->sc_media_init = dp8390_media_init;
271 
272 	if (dp8390_config(dsc)) {
273 		printf(": setup failed\n");
274 		return (1);
275 	}
276 
277 	/*
278 	 * We need to compute mem_ring a bit differently; override the
279 	 * value set up in dp8390_config().
280 	 */
281 	dsc->mem_ring =
282 	    dsc->mem_start + ((dsc->txb_cnt * ED_TXBUF_SIZE) << ED_PAGE_SHIFT);
283 
284 	return (0);
285 }
286 
287 /*
288  * Detect an NE-2000 or compatible.  Returns a model code.
289  */
290 int
291 ne2000_detect(struct ne2000_softc *nsc)
292 {
293 	struct dp8390_softc *dsc = &nsc->sc_dp8390;
294 	bus_space_tag_t nict = dsc->sc_regt;
295 	bus_space_handle_t nich = dsc->sc_regh;
296 	bus_space_tag_t asict = nsc->sc_asict;
297 	bus_space_handle_t asich = nsc->sc_asich;
298 	static u_int8_t test_pattern[32] = "THIS is A memory TEST pattern";
299 	u_int8_t test_buffer[32], tmp;
300 	int state, i, rv = 0;
301 
302 	state = dsc->sc_enabled;
303 	dsc->sc_enabled = 0;
304 
305 	/* Reset the board. */
306 #ifdef GWETHER
307 	bus_space_write_1(asict, asich, NE2000_ASIC_RESET, 0);
308 	ASIC_BARRIER(asict, asich);
309 	delay(200);
310 #endif /* GWETHER */
311 	tmp = bus_space_read_1(asict, asich, NE2000_ASIC_RESET);
312 	ASIC_BARRIER(asict, asich);
313 	delay(10000);
314 
315 	/*
316 	 * I don't know if this is necessary; probably cruft leftover from
317 	 * Clarkson packet driver code. Doesn't do a thing on the boards I've
318 	 * tested. -DG [note that a outb(0x84, 0) seems to work here, and is
319 	 * non-invasive...but some boards don't seem to reset and I don't have
320 	 * complete documentation on what the 'right' thing to do is...so we do
321 	 * the invasive thing for now.  Yuck.]
322 	 */
323 	bus_space_write_1(asict, asich, NE2000_ASIC_RESET, tmp);
324 	ASIC_BARRIER(asict, asich);
325 	delay(5000);
326 
327 	/*
328 	 * This is needed because some NE clones apparently don't reset the
329 	 * NIC properly (or the NIC chip doesn't reset fully on power-up).
330 	 * XXX - this makes the probe invasive!  Done against my better
331 	 * judgement.  -DLG
332 	 */
333 	bus_space_write_1(nict, nich, ED_P0_CR,
334 	    ED_CR_RD2 | ED_CR_PAGE_0 | ED_CR_STP);
335 	NIC_BARRIER(nict, nich);
336 
337 	delay(5000);
338 
339 	/*
340 	 * Generic probe routine for testing for the existence of a DS8390.
341 	 * Must be performed  after the NIC has just been reset.  This
342 	 * works by looking at certain register values that are guaranteed
343 	 * to be initialized a certain way after power-up or reset.
344 	 *
345 	 * Specifically:
346 	 *
347 	 *	Register		reset bits	set bits
348 	 *	--------		----------	--------
349 	 *	CR			TXP, STA	RD2, STP
350 	 *	ISR					RST
351 	 *	IMR			<all>
352 	 *	DCR					LAS
353 	 *	TCR			LB1, LB0
354 	 *
355 	 * We only look at CR and ISR, however, since looking at the others
356 	 * would require changing register pages, which would be intrusive
357 	 * if this isn't an 8390.
358 	 */
359 
360 	tmp = bus_space_read_1(nict, nich, ED_P0_CR);
361 	if ((tmp & (ED_CR_RD2 | ED_CR_TXP | ED_CR_STA | ED_CR_STP)) !=
362 	    (ED_CR_RD2 | ED_CR_STP))
363 		goto out;
364 
365 	tmp = bus_space_read_1(nict, nich, ED_P0_ISR);
366 	if ((tmp & ED_ISR_RST) != ED_ISR_RST)
367 		goto out;
368 
369 	bus_space_write_1(nict, nich,
370 	    ED_P0_CR, ED_CR_RD2 | ED_CR_PAGE_0 | ED_CR_STA);
371 	NIC_BARRIER(nict, nich);
372 
373 	for (i = 0; i < 100; i++) {
374 		if ((bus_space_read_1(nict, nich, ED_P0_ISR) & ED_ISR_RST) ==
375 		    ED_ISR_RST) {
376 			/* Ack the reset bit. */
377 			bus_space_write_1(nict, nich, ED_P0_ISR, ED_ISR_RST);
378 			NIC_BARRIER(nict, nich);
379 			break;
380 		}
381 		delay(100);
382 	}
383 
384 #if 0
385 	/* XXX */
386 	if (i == 100)
387 		goto out;
388 #endif
389 
390 	/*
391 	 * Test the ability to read and write to the NIC memory.  This has
392 	 * the side effect of determining if this is an NE1000 or an NE2000.
393 	 */
394 
395 	/*
396 	 * This prevents packets from being stored in the NIC memory when
397 	 * the readmem routine turns on the start bit in the CR.
398 	 */
399 	bus_space_write_1(nict, nich, ED_P0_RCR, ED_RCR_MON);
400 	NIC_BARRIER(nict, nich);
401 
402 	/* Temporarily initialize DCR for byte operations. */
403 	bus_space_write_1(nict, nich, ED_P0_DCR, ED_DCR_FT1 | ED_DCR_LS);
404 
405 	bus_space_write_1(nict, nich, ED_P0_PSTART, 8192 >> ED_PAGE_SHIFT);
406 	bus_space_write_1(nict, nich, ED_P0_PSTOP, 16384 >> ED_PAGE_SHIFT);
407 
408 	/*
409 	 * Write a test pattern in byte mode.  If this fails, then there
410 	 * probably isn't any memory at 8k - which likely means that the
411 	 * board is an NE2000.
412 	 */
413 	ne2000_writemem(nict, nich, asict, asich, test_pattern, 8192,
414 	    sizeof(test_pattern), 0);
415 	ne2000_readmem(nict, nich, asict, asich, 8192, test_buffer,
416 	    sizeof(test_buffer), 0);
417 
418 	if (bcmp(test_pattern, test_buffer, sizeof(test_pattern))) {
419 		/* not an NE1000 - try NE2000 */
420 		bus_space_write_1(nict, nich, ED_P0_DCR,
421 		    ED_DCR_WTS | ED_DCR_FT1 | ED_DCR_LS);
422 		bus_space_write_1(nict, nich, ED_P0_PSTART,
423 		    16384 >> ED_PAGE_SHIFT);
424 		bus_space_write_1(nict, nich, ED_P0_PSTOP,
425 		    32768 >> ED_PAGE_SHIFT);
426 
427 		/*
428 		 * Write the test pattern in word mode.  If this also fails,
429 		 * then we don't know what this board is.
430 		 */
431 		ne2000_writemem(nict, nich, asict, asich, test_pattern, 16384,
432 		    sizeof(test_pattern), 1);
433 		ne2000_readmem(nict, nich, asict, asich, 16384, test_buffer,
434 		    sizeof(test_buffer), 1);
435 
436 		if (bcmp(test_pattern, test_buffer, sizeof(test_pattern)))
437 			goto out;	/* not an NE2000 either */
438 
439 		rv = NE2000_TYPE_NE2000;
440 	} else {
441 		/* We're an NE1000. */
442 		rv = NE2000_TYPE_NE1000;
443 	}
444 
445 	/* Clear any pending interrupts that might have occurred above. */
446 	NIC_BARRIER(nict, nich);
447 	bus_space_write_1(nict, nich, ED_P0_ISR, 0xff);
448 
449  out:
450 	dsc->sc_enabled = state;
451 
452 	return (rv);
453 }
454 
455 /*
456  * Write an mbuf chain to the destination NIC memory address using programmed
457  * I/O.
458  */
459 int
460 ne2000_write_mbuf(struct dp8390_softc *sc, struct mbuf *m, int buf)
461 {
462 	struct ne2000_softc *nsc = (struct ne2000_softc *)sc;
463 	bus_space_tag_t nict = sc->sc_regt;
464 	bus_space_handle_t nich = sc->sc_regh;
465 	bus_space_tag_t asict = nsc->sc_asict;
466 	bus_space_handle_t asich = nsc->sc_asich;
467 	int savelen;
468 	int maxwait = 100;	/* about 120us */
469 
470 	savelen = m->m_pkthdr.len;
471 
472 	/* Select page 0 registers. */
473 	NIC_BARRIER(nict, nich);
474 	bus_space_write_1(nict, nich, ED_P0_CR,
475 	    ED_CR_RD2 | ED_CR_PAGE_0 | ED_CR_STA);
476 	NIC_BARRIER(nict, nich);
477 
478 	/* Reset remote DMA complete flag. */
479 	bus_space_write_1(nict, nich, ED_P0_ISR, ED_ISR_RDC);
480 	NIC_BARRIER(nict, nich);
481 
482 	/* Set up DMA byte count. */
483 	bus_space_write_1(nict, nich, ED_P0_RBCR0, savelen);
484 	bus_space_write_1(nict, nich, ED_P0_RBCR1, savelen >> 8);
485 
486 	/* Set up destination address in NIC mem. */
487 	bus_space_write_1(nict, nich, ED_P0_RSAR0, buf);
488 	bus_space_write_1(nict, nich, ED_P0_RSAR1, buf >> 8);
489 
490 	/* Set remote DMA write. */
491 	NIC_BARRIER(nict, nich);
492 	bus_space_write_1(nict, nich,
493 	    ED_P0_CR, ED_CR_RD1 | ED_CR_PAGE_0 | ED_CR_STA);
494 	NIC_BARRIER(nict, nich);
495 
496 	/*
497 	 * Transfer the mbuf chain to the NIC memory.  NE2000 cards
498 	 * require that data be transferred as words, and only words,
499 	 * so that case requires some extra code to patch over odd-length
500 	 * mbufs.
501 	 */
502 	if (nsc->sc_type == NE2000_TYPE_NE1000) {
503 		/* NE1000s are easy. */
504 		for (; m != 0; m = m->m_next) {
505 			if (m->m_len) {
506 				bus_space_write_multi_1(asict, asich,
507 				    NE2000_ASIC_DATA, mtod(m, u_int8_t *),
508 				    m->m_len);
509 			}
510 		}
511 	} else {
512 		/* NE2000s are a bit trickier. */
513 		u_int8_t *data, savebyte[2];
514 		int l, leftover;
515 #ifdef DIAGNOSTIC
516 		u_int8_t *lim;
517 #endif
518 		/* Start out with no leftover data. */
519 		leftover = 0;
520 		savebyte[0] = savebyte[1] = 0;
521 
522 		for (; m != 0; m = m->m_next) {
523 			l = m->m_len;
524 			if (l == 0)
525 				continue;
526 			data = mtod(m, u_int8_t *);
527 #ifdef DIAGNOSTIC
528 			lim = data + l;
529 #endif
530 			while (l > 0) {
531 				if (leftover) {
532 					/*
533 					 * Data left over (from mbuf or
534 					 * realignment).  Buffer the next
535 					 * byte, and write it and the
536 					 * leftover data out.
537 					 */
538 					savebyte[1] = *data++;
539 					l--;
540 					bus_space_write_raw_multi_2(asict,
541 					    asich, NE2000_ASIC_DATA,
542 					    savebyte, 2);
543 					leftover = 0;
544 				} else if (ALIGNED_POINTER(data,
545 					   u_int16_t) == 0) {
546 					/*
547 					 * Unaligned data; buffer the next
548 					 * byte.
549 					 */
550 					savebyte[0] = *data++;
551 					l--;
552 					leftover = 1;
553 				} else {
554 					/*
555 					 * Aligned data; output contiguous
556 					 * words as much as we can, then
557 					 * buffer the remaining byte, if any.
558 					 */
559 					leftover = l & 1;
560 					l &= ~1;
561 					bus_space_write_raw_multi_2(asict,
562 					    asich, NE2000_ASIC_DATA, data, l);
563 					data += l;
564 					if (leftover)
565 						savebyte[0] = *data++;
566 					l = 0;
567 				}
568 			}
569 			if (l < 0)
570 				panic("ne2000_write_mbuf: negative len");
571 #ifdef DIAGNOSTIC
572 			if (data != lim)
573 				panic("ne2000_write_mbuf: data != lim");
574 #endif
575 		}
576 		if (leftover) {
577 			savebyte[1] = 0;
578 			bus_space_write_raw_multi_2(asict, asich,
579 			    NE2000_ASIC_DATA, savebyte, 2);
580 		}
581 	}
582 	NIC_BARRIER(nict, nich);
583 
584 	/* AX88796 doesn't seem to have remote DMA complete */
585 	if (sc->sc_flags & DP8390_NO_REMOTE_DMA_COMPLETE)
586 		return (savelen);
587 
588 	/*
589 	 * Wait for remote DMA to complete.  This is necessary because on the
590 	 * transmit side, data is handled internally by the NIC in bursts, and
591 	 * we can't start another remote DMA until this one completes.  Not
592 	 * waiting causes really bad things to happen - like the NIC wedging
593 	 * the bus.
594 	 */
595 	while (((bus_space_read_1(nict, nich, ED_P0_ISR) & ED_ISR_RDC) !=
596 	    ED_ISR_RDC) && --maxwait) {
597 		bus_space_read_1(nict, nich, ED_P0_CRDA1);
598 		bus_space_read_1(nict, nich, ED_P0_CRDA0);
599 		NIC_BARRIER(nict, nich);
600 		DELAY(1);
601 	}
602 
603 	if (maxwait == 0) {
604 		log(LOG_WARNING,
605 		    "%s: remote transmit DMA failed to complete\n",
606 		    sc->sc_dev.dv_xname);
607 		dp8390_reset(sc);
608 	}
609 
610 	return (savelen);
611 }
612 
613 /*
614  * Given a source and destination address, copy 'amount' of a packet from
615  * the ring buffer into a linear destination buffer.  Takes into account
616  * ring-wrap.
617  */
618 int
619 ne2000_ring_copy(struct dp8390_softc *sc, int src, caddr_t dst,
620     u_short amount)
621 {
622 	struct ne2000_softc *nsc = (struct ne2000_softc *)sc;
623 	bus_space_tag_t nict = sc->sc_regt;
624 	bus_space_handle_t nich = sc->sc_regh;
625 	bus_space_tag_t asict = nsc->sc_asict;
626 	bus_space_handle_t asich = nsc->sc_asich;
627 	u_short tmp_amount;
628 	int useword = nsc->sc_useword;
629 
630 	/* Does copy wrap to lower addr in ring buffer? */
631 	if (src + amount > sc->mem_end) {
632 		tmp_amount = sc->mem_end - src;
633 
634 		/* Copy amount up to end of NIC memory. */
635 		ne2000_readmem(nict, nich, asict, asich, src,
636 		    (u_int8_t *)dst, tmp_amount, useword);
637 
638 		amount -= tmp_amount;
639 		src = sc->mem_ring;
640 		dst += tmp_amount;
641 	}
642 
643 	ne2000_readmem(nict, nich, asict, asich, src, (u_int8_t *)dst,
644 	    amount, useword);
645 
646 	return (src + amount);
647 }
648 
649 void
650 ne2000_read_hdr(struct dp8390_softc *sc, int buf, struct dp8390_ring *hdr)
651 {
652 	struct ne2000_softc *nsc = (struct ne2000_softc *)sc;
653 
654 	ne2000_readmem(sc->sc_regt, sc->sc_regh, nsc->sc_asict, nsc->sc_asich,
655 	    buf, (u_int8_t *)hdr, sizeof(struct dp8390_ring),
656 	    nsc->sc_useword);
657 #if BYTE_ORDER == BIG_ENDIAN
658 	hdr->count = swap16(hdr->count);
659 #endif
660 }
661 
662 int
663 ne2000_test_mem(struct dp8390_softc *sc)
664 {
665 	/* Noop. */
666 	return (0);
667 }
668 
669 /*
670  * Given a NIC memory source address and a host memory destination address,
671  * copy 'amount' from NIC to host using programmed i/o.  The 'amount' is
672  * rounded up to a word - ok as long as mbufs are word sized.
673  */
674 void
675 ne2000_readmem(bus_space_tag_t nict, bus_space_handle_t nich,
676     bus_space_tag_t asict, bus_space_handle_t asich, int src,
677     u_int8_t *dst, size_t amount, int useword)
678 {
679 
680 	/* Select page 0 registers. */
681 	NIC_BARRIER(nict, nich);
682 	bus_space_write_1(nict, nich, ED_P0_CR,
683 	    ED_CR_RD2 | ED_CR_PAGE_0 | ED_CR_STA);
684 	NIC_BARRIER(nict, nich);
685 
686 	/* Round up to a word. */
687 	if (amount & 1)
688 		++amount;
689 
690 	/* Set up DMA byte count. */
691 	bus_space_write_1(nict, nich, ED_P0_RBCR0, amount);
692 	bus_space_write_1(nict, nich, ED_P0_RBCR1, amount >> 8);
693 
694 	/* Set up source address in NIC mem. */
695 	bus_space_write_1(nict, nich, ED_P0_RSAR0, src);
696 	bus_space_write_1(nict, nich, ED_P0_RSAR1, src >> 8);
697 
698 	NIC_BARRIER(nict, nich);
699 	bus_space_write_1(nict, nich, ED_P0_CR,
700 	    ED_CR_RD0 | ED_CR_PAGE_0 | ED_CR_STA);
701 
702 	ASIC_BARRIER(asict, asich);
703 	if (useword)
704 		bus_space_read_raw_multi_2(asict, asich, NE2000_ASIC_DATA,
705 		    dst, amount);
706 	else
707 		bus_space_read_multi_1(asict, asich, NE2000_ASIC_DATA,
708 		    dst, amount);
709 }
710 
711 /*
712  * Stripped down routine for writing a linear buffer to NIC memory.  Only
713  * used in the probe routine to test the memory.  'len' must be even.
714  */
715 void
716 ne2000_writemem(bus_space_tag_t nict, bus_space_handle_t nich,
717     bus_space_tag_t asict, bus_space_handle_t asich, u_int8_t *src,
718     int dst, size_t len, int useword)
719 {
720 	int maxwait = 100;	/* about 120us */
721 
722 	/* Select page 0 registers. */
723 	NIC_BARRIER(nict, nich);
724 	bus_space_write_1(nict, nich, ED_P0_CR,
725 	    ED_CR_RD2 | ED_CR_PAGE_0 | ED_CR_STA);
726 	NIC_BARRIER(nict, nich);
727 
728 	/* Reset remote DMA complete flag. */
729 	bus_space_write_1(nict, nich, ED_P0_ISR, ED_ISR_RDC);
730 	NIC_BARRIER(nict, nich);
731 
732 	/* Set up DMA byte count. */
733 	bus_space_write_1(nict, nich, ED_P0_RBCR0, len);
734 	bus_space_write_1(nict, nich, ED_P0_RBCR1, len >> 8);
735 
736 	/* Set up destination address in NIC mem. */
737 	bus_space_write_1(nict, nich, ED_P0_RSAR0, dst);
738 	bus_space_write_1(nict, nich, ED_P0_RSAR1, dst >> 8);
739 
740 	/* Set remote DMA write. */
741 	NIC_BARRIER(nict, nich);
742 	bus_space_write_1(nict, nich, ED_P0_CR,
743 	    ED_CR_RD1 | ED_CR_PAGE_0 | ED_CR_STA);
744 	NIC_BARRIER(nict, nich);
745 
746 	ASIC_BARRIER(asict, asich);
747 	if (useword)
748 		bus_space_write_raw_multi_2(asict, asich, NE2000_ASIC_DATA,
749 		    src, len);
750 	else
751 		bus_space_write_multi_1(asict, asich, NE2000_ASIC_DATA,
752 		    src, len);
753 	ASIC_BARRIER(asict, asich);
754 
755 	/*
756 	 * Wait for remote DMA to complete.  This is necessary because on the
757 	 * transmit side, data is handled internally by the NIC in bursts, and
758 	 * we can't start another remote DMA until this one completes.  Not
759 	 * waiting causes really bad things to happen - like the NIC wedging
760 	 * the bus.
761 	 */
762 	while (((bus_space_read_1(nict, nich, ED_P0_ISR) & ED_ISR_RDC) !=
763 	    ED_ISR_RDC) && --maxwait)
764 		DELAY(1);
765 }
766 
767 int
768 ne2000_detach(struct ne2000_softc *sc, int flags)
769 {
770 	return (dp8390_detach(&sc->sc_dp8390, flags));
771 }
772