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