xref: /netbsd-src/sys/dev/ic/ne2000.c (revision 3cec974c61d7fac0a37c0377723a33214a458c8b)
1 /*	$NetBSD: ne2000.c,v 1.32 2001/02/12 18:49:04 thorpej 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 "opt_ipkdb.h"
58 
59 #include <sys/param.h>
60 #include <sys/systm.h>
61 #include <sys/device.h>
62 #include <sys/socket.h>
63 #include <sys/mbuf.h>
64 #include <sys/syslog.h>
65 
66 #include <net/if.h>
67 #include <net/if_dl.h>
68 #include <net/if_types.h>
69 #include <net/if_media.h>
70 
71 #include <net/if_ether.h>
72 
73 #include <machine/bswap.h>
74 #include <machine/bus.h>
75 
76 #ifndef __BUS_SPACE_HAS_STREAM_METHODS
77 #define	bus_space_write_stream_2	bus_space_write_2
78 #define	bus_space_write_multi_stream_2	bus_space_write_multi_2
79 #define	bus_space_read_multi_stream_2	bus_space_read_multi_2
80 #endif /* __BUS_SPACE_HAS_STREAM_METHODS */
81 
82 #ifdef IPKDB_NE
83 #include <ipkdb/ipkdb.h>
84 #endif
85 
86 #include <dev/ic/dp8390reg.h>
87 #include <dev/ic/dp8390var.h>
88 
89 #include <dev/ic/ne2000reg.h>
90 #include <dev/ic/ne2000var.h>
91 
92 #if BYTE_ORDER == BIG_ENDIAN
93 #include <machine/bswap.h>
94 #endif
95 
96 int	ne2000_write_mbuf __P((struct dp8390_softc *, struct mbuf *, int));
97 int	ne2000_ring_copy __P((struct dp8390_softc *, int, caddr_t, u_short));
98 void	ne2000_read_hdr __P((struct dp8390_softc *, int, struct dp8390_ring *));
99 int	ne2000_test_mem __P((struct dp8390_softc *));
100 
101 void	ne2000_writemem __P((bus_space_tag_t, bus_space_handle_t,
102 	    bus_space_tag_t, bus_space_handle_t, u_int8_t *, int, size_t,
103 	    int, int));
104 void	ne2000_readmem __P((bus_space_tag_t, bus_space_handle_t,
105 	    bus_space_tag_t, bus_space_handle_t, int, u_int8_t *, size_t, int));
106 
107 int
108 ne2000_attach(nsc, myea)
109 	struct ne2000_softc *nsc;
110 	u_int8_t *myea;
111 {
112 	struct dp8390_softc *dsc = &nsc->sc_dp8390;
113 	bus_space_tag_t nict = dsc->sc_regt;
114 	bus_space_handle_t nich = dsc->sc_regh;
115 	bus_space_tag_t asict = nsc->sc_asict;
116 	bus_space_handle_t asich = nsc->sc_asich;
117 	u_int8_t romdata[16];
118 	int memsize, i, useword;
119 
120 	/*
121 	 * Detect it again unless caller specified it; this gives us
122 	 * the memory size.
123 	 */
124 	if (nsc->sc_type == 0) {
125 		nsc->sc_type = ne2000_detect(nict, nich, asict, asich);
126 		if (nsc->sc_type == 0) {
127 			printf("%s: where did the card go?\n",
128 			    dsc->sc_dev.dv_xname);
129 			return (1);
130 		}
131 	}
132 
133 	useword = NE2000_USE_WORD(nsc);
134 
135 	dsc->cr_proto = ED_CR_RD2;
136 	if (nsc->sc_type == NE2000_TYPE_AX88190) {
137 		dsc->rcr_proto = ED_RCR_INTT;
138 		dsc->sc_flags |= DP8390_DO_AX88190_WORKAROUND;
139 	} else
140 		dsc->rcr_proto = 0;
141 
142 	/*
143 	 * DCR gets:
144 	 *
145 	 *	FIFO threshold to 8, No auto-init Remote DMA,
146 	 *	byte order=80x86.
147 	 *
148 	 * NE1000 gets byte-wide DMA, NE2000 gets word-wide DMA.
149 	 */
150 	dsc->dcr_reg = ED_DCR_FT1 | ED_DCR_LS | (useword ? ED_DCR_WTS : 0);
151 
152 	dsc->test_mem = ne2000_test_mem;
153 	dsc->ring_copy = ne2000_ring_copy;
154 	dsc->write_mbuf = ne2000_write_mbuf;
155 	dsc->read_hdr = ne2000_read_hdr;
156 
157 	/* Registers are linear. */
158 	for (i = 0; i < 16; i++)
159 		dsc->sc_reg_map[i] = i;
160 
161 	/*
162 	 * 8k of memory for NE1000, 16k for NE2000 and 24k for the
163 	 * card uses DL10019.
164 	 */
165 	switch (nsc->sc_type) {
166 	case NE2000_TYPE_NE1000:
167 		memsize = 8192;
168 		break;
169 	case NE2000_TYPE_NE2000:
170 	case NE2000_TYPE_AX88190:		/* XXX really? */
171 		memsize = 8192 * 2;
172 		break;
173 	case NE2000_TYPE_DL10019:
174 		memsize = 8192 * 3;
175 		break;
176 	}
177 
178 	/*
179 	 * NIC memory doens't start at zero on an NE board.
180 	 * The start address is tied to the bus width.
181 	 * (It happens to be computed the same way as mem size.)
182 	 */
183 	dsc->mem_start = memsize;
184 
185 #ifdef GWETHER
186 	{
187 		int x, mstart = 0;
188 		int8_t pbuf0[ED_PAGE_SIZE], pbuf[ED_PAGE_SIZE],
189 		    tbuf[ED_PAGE_SIZE];
190 
191 		for (i = 0; i < ED_PAGE_SIZE; i++)
192 			pbuf0[i] = 0;
193 
194 		/* Search for the start of RAM. */
195 		for (x = 1; x < 256; x++) {
196 			ne2000_writemem(nict, nich, asict, asich, pbuf0,
197 			    x << ED_PAGE_SHIFT, ED_PAGE_SIZE, useword, 0);
198 			ne2000_readmem(nict, nich, asict, asich,
199 			    x << ED_PAGE_SHIFT, tbuf, ED_PAGE_SIZE, useword);
200 			if (bcmp(pbuf0, tbuf, ED_PAGE_SIZE) == 0) {
201 				for (i = 0; i < ED_PAGE_SIZE; i++)
202 					pbuf[i] = 255 - x;
203 				ne2000_writemem(nict, nich, asict, asich,
204 				    pbuf, x << ED_PAGE_SHIFT, ED_PAGE_SIZE,
205 				    useword, 0);
206 				ne2000_readmem(nict, nich, asict, asich,
207 				    x << ED_PAGE_SHIFT, tbuf, ED_PAGE_SIZE,
208 				    useword);
209 				if (bcmp(pbuf, tbuf, ED_PAGE_SIZE) == 0) {
210 					mstart = x << ED_PAGE_SHIFT;
211 					memsize = ED_PAGE_SIZE;
212 					break;
213 				}
214 			}
215 		}
216 
217 		if (mstart == 0) {
218 			printf("%s: cannot find start of RAM\n",
219 			    dsc->sc_dev.dv_xname);
220 			return (1);
221 		}
222 
223 		/* Search for the end of RAM. */
224 		for (++x; x < 256; x++) {
225 			ne2000_writemem(nict, nich, asict, asich, pbuf0,
226 			    x << ED_PAGE_SHIFT, ED_PAGE_SIZE, useword, 0);
227 			ne2000_readmem(nict, nich, asict, asich,
228 			    x << ED_PAGE_SHIFT, tbuf, ED_PAGE_SIZE, useword);
229 			if (bcmp(pbuf0, tbuf, ED_PAGE_SIZE) == 0) {
230 				for (i = 0; i < ED_PAGE_SIZE; i++)
231 					pbuf[i] = 255 - x;
232 				ne2000_writemem(nict, nich, asict, asich,
233 				    pbuf, x << ED_PAGE_SHIFT, ED_PAGE_SIZE,
234 				    useword, 0);
235 				ne2000_readmem(nict, nich, asict, asich,
236 				    x << ED_PAGE_SHIFT, tbuf, ED_PAGE_SIZE,
237 				    useword);
238 				if (bcmp(pbuf, tbuf, ED_PAGE_SIZE) == 0)
239 					memsize += ED_PAGE_SIZE;
240 				else
241 					break;
242 			} else
243 				break;
244 		}
245 
246 		printf("%s: RAM start 0x%x, size %d\n",
247 		    dsc->sc_dev.dv_xname, mstart, memsize);
248 
249 		dsc->mem_start = mstart;
250 	}
251 #endif /* GWETHER */
252 
253 	dsc->mem_size = memsize;
254 
255 	if (myea == NULL) {
256 		/* Read the station address. */
257 		if (nsc->sc_type == NE2000_TYPE_AX88190) {
258 			/* Select page 0 registers. */
259 			NIC_BARRIER(nict, nich);
260 			bus_space_write_1(nict, nich, ED_P0_CR,
261 			    ED_CR_RD2 | ED_CR_PAGE_0 | ED_CR_STA);
262 			NIC_BARRIER(nict, nich);
263 			/* Select word transfer. */
264 			bus_space_write_1(nict, nich, ED_P0_DCR, ED_DCR_WTS);
265 			NIC_BARRIER(nict, nich);
266 			ne2000_readmem(nict, nich, asict, asich,
267 			    NE2000_AX88190_NODEID_OFFSET, dsc->sc_enaddr,
268 			    ETHER_ADDR_LEN, useword);
269 		} else {
270 			ne2000_readmem(nict, nich, asict, asich, 0, romdata,
271 			    sizeof(romdata), useword);
272 			for (i = 0; i < ETHER_ADDR_LEN; i++)
273 				dsc->sc_enaddr[i] =
274 				    romdata[i * (useword ? 2 : 1)];
275 		}
276 	} else
277 		bcopy(myea, dsc->sc_enaddr, sizeof(dsc->sc_enaddr));
278 
279 	/* Clear any pending interrupts that might have occurred above. */
280 	NIC_BARRIER(nict, nich);
281 	bus_space_write_1(nict, nich, ED_P0_ISR, 0xff);
282 	NIC_BARRIER(nict, nich);
283 
284 	if (dsc->sc_media_init == NULL)
285 		dsc->sc_media_init = dp8390_media_init;
286 
287 	if (dp8390_config(dsc)) {
288 		printf("%s: setup failed\n", dsc->sc_dev.dv_xname);
289 		return (1);
290 	}
291 
292 	/*
293 	 * We need to compute mem_ring a bit differently; override the
294 	 * value set up in dp8390_config().
295 	 */
296 	dsc->mem_ring =
297 	    dsc->mem_start + ((dsc->txb_cnt * ED_TXBUF_SIZE) << ED_PAGE_SHIFT);
298 
299 	return (0);
300 }
301 
302 /*
303  * Detect an NE-2000 or compatible.  Returns a model code.
304  */
305 int
306 ne2000_detect(nict, nich, asict, asich)
307 	bus_space_tag_t nict;
308 	bus_space_handle_t nich;
309 	bus_space_tag_t asict;
310 	bus_space_handle_t asich;
311 {
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 	bus_space_barrier(nict, nich, 0, NE2000_NPORTS,
320 			  BUS_SPACE_BARRIER_READ | BUS_SPACE_BARRIER_WRITE);
321 	delay(200);
322 #endif /* GWETHER */
323 	tmp = bus_space_read_1(asict, asich, NE2000_ASIC_RESET);
324 	bus_space_barrier(nict, nich, 0, NE2000_NPORTS,
325 			  BUS_SPACE_BARRIER_READ | BUS_SPACE_BARRIER_WRITE);
326 	delay(10000);
327 
328 	/*
329 	 * I don't know if this is necessary; probably cruft leftover from
330 	 * Clarkson packet driver code. Doesn't do a thing on the boards I've
331 	 * tested. -DG [note that a outb(0x84, 0) seems to work here, and is
332 	 * non-invasive...but some boards don't seem to reset and I don't have
333 	 * complete documentation on what the 'right' thing to do is...so we do
334 	 * the invasive thing for now.  Yuck.]
335 	 */
336 	bus_space_write_1(asict, asich, NE2000_ASIC_RESET, tmp);
337 	bus_space_barrier(nict, nich, 0, NE2000_NPORTS,
338 			  BUS_SPACE_BARRIER_READ | BUS_SPACE_BARRIER_WRITE);
339 	delay(5000);
340 
341 	/*
342 	 * This is needed because some NE clones apparently don't reset the
343 	 * NIC properly (or the NIC chip doesn't reset fully on power-up).
344 	 * XXX - this makes the probe invasive!  Done against my better
345 	 * judgement.  -DLG
346 	 */
347 	bus_space_write_1(nict, nich, ED_P0_CR,
348 	    ED_CR_RD2 | ED_CR_PAGE_0 | ED_CR_STP);
349 	NIC_BARRIER(nict, nich);
350 
351 	delay(5000);
352 
353 	/*
354 	 * Generic probe routine for testing for the existance of a DS8390.
355 	 * Must be performed  after the NIC has just been reset.  This
356 	 * works by looking at certain register values that are guaranteed
357 	 * to be initialized a certain way after power-up or reset.
358 	 *
359 	 * Specifically:
360 	 *
361 	 *	Register		reset bits	set bits
362 	 *	--------		----------	--------
363 	 *	CR			TXP, STA	RD2, STP
364 	 *	ISR					RST
365 	 *	IMR			<all>
366 	 *	DCR					LAS
367 	 *	TCR			LB1, LB0
368 	 *
369 	 * We only look at CR and ISR, however, since looking at the others
370 	 * would require changing register pages, which would be intrusive
371 	 * if this isn't an 8390.
372 	 */
373 
374 	tmp = bus_space_read_1(nict, nich, ED_P0_CR);
375 	if ((tmp & (ED_CR_RD2 | ED_CR_TXP | ED_CR_STA | ED_CR_STP)) !=
376 	    (ED_CR_RD2 | ED_CR_STP))
377 		goto out;
378 
379 	tmp = bus_space_read_1(nict, nich, ED_P0_ISR);
380 	if ((tmp & ED_ISR_RST) != ED_ISR_RST)
381 		goto out;
382 
383 	bus_space_write_1(nict, nich,
384 	    ED_P0_CR, ED_CR_RD2 | ED_CR_PAGE_0 | ED_CR_STA);
385 	NIC_BARRIER(nict, nich);
386 
387 	for (i = 0; i < 100; i++) {
388 		if ((bus_space_read_1(nict, nich, ED_P0_ISR) & ED_ISR_RST) ==
389 		    ED_ISR_RST) {
390 			/* Ack the reset bit. */
391 			bus_space_write_1(nict, nich, ED_P0_ISR, ED_ISR_RST);
392 			NIC_BARRIER(nict, nich);
393 			break;
394 		}
395 		delay(100);
396 	}
397 
398 #if 0
399 	/* XXX */
400 	if (i == 100)
401 		goto out;
402 #endif
403 
404 	/*
405 	 * Test the ability to read and write to the NIC memory.  This has
406 	 * the side effect of determining if this is an NE1000 or an NE2000.
407 	 */
408 
409 	/*
410 	 * This prevents packets from being stored in the NIC memory when
411 	 * the readmem routine turns on the start bit in the CR.
412 	 */
413 	bus_space_write_1(nict, nich, ED_P0_RCR, ED_RCR_MON);
414 	NIC_BARRIER(nict, nich);
415 
416 	/* Temporarily initialize DCR for byte operations. */
417 	bus_space_write_1(nict, nich, ED_P0_DCR, ED_DCR_FT1 | ED_DCR_LS);
418 
419 	bus_space_write_1(nict, nich, ED_P0_PSTART, 8192 >> ED_PAGE_SHIFT);
420 	bus_space_write_1(nict, nich, ED_P0_PSTOP, 16384 >> ED_PAGE_SHIFT);
421 
422 	/*
423 	 * Write a test pattern in byte mode.  If this fails, then there
424 	 * probably isn't any memory at 8k - which likely means that the
425 	 * board is an NE2000.
426 	 */
427 	ne2000_writemem(nict, nich, asict, asich, test_pattern, 8192,
428 	    sizeof(test_pattern), 0, 1);
429 	ne2000_readmem(nict, nich, asict, asich, 8192, test_buffer,
430 	    sizeof(test_buffer), 0);
431 
432 	if (bcmp(test_pattern, test_buffer, sizeof(test_pattern))) {
433 		/* not an NE1000 - try NE2000 */
434 		bus_space_write_1(nict, nich, ED_P0_DCR,
435 		    ED_DCR_WTS | ED_DCR_FT1 | ED_DCR_LS);
436 		bus_space_write_1(nict, nich, ED_P0_PSTART,
437 		    16384 >> ED_PAGE_SHIFT);
438 		bus_space_write_1(nict, nich, ED_P0_PSTOP,
439 		    32768 >> ED_PAGE_SHIFT);
440 
441 		/*
442 		 * Write the test pattern in word mode.  If this also fails,
443 		 * then we don't know what this board is.
444 		 */
445 		ne2000_writemem(nict, nich, asict, asich, test_pattern, 16384,
446 		    sizeof(test_pattern), 1, 0);
447 		ne2000_readmem(nict, nich, asict, asich, 16384, test_buffer,
448 		    sizeof(test_buffer), 1);
449 
450 		if (bcmp(test_pattern, test_buffer, sizeof(test_pattern)))
451 			goto out;	/* not an NE2000 either */
452 
453 		rv = NE2000_TYPE_NE2000;
454 	} else {
455 		/* We're an NE1000. */
456 		rv = NE2000_TYPE_NE1000;
457 	}
458 
459 	/* Clear any pending interrupts that might have occurred above. */
460 	NIC_BARRIER(nict, nich);
461 	bus_space_write_1(nict, nich, ED_P0_ISR, 0xff);
462 
463  out:
464 	return (rv);
465 }
466 
467 /*
468  * Write an mbuf chain to the destination NIC memory address using programmed
469  * I/O.
470  */
471 int
472 ne2000_write_mbuf(sc, m, buf)
473 	struct dp8390_softc *sc;
474 	struct mbuf *m;
475 	int buf;
476 {
477 	struct ne2000_softc *nsc = (struct ne2000_softc *)sc;
478 	bus_space_tag_t nict = sc->sc_regt;
479 	bus_space_handle_t nich = sc->sc_regh;
480 	bus_space_tag_t asict = nsc->sc_asict;
481 	bus_space_handle_t asich = nsc->sc_asich;
482 	int savelen;
483 	int maxwait = 100;	/* about 120us */
484 
485 	savelen = m->m_pkthdr.len;
486 
487 	/* Select page 0 registers. */
488 	NIC_BARRIER(nict, nich);
489 	bus_space_write_1(nict, nich, ED_P0_CR,
490 	    ED_CR_RD2 | ED_CR_PAGE_0 | ED_CR_STA);
491 	NIC_BARRIER(nict, nich);
492 
493 	/* Reset remote DMA complete flag. */
494 	bus_space_write_1(nict, nich, ED_P0_ISR, ED_ISR_RDC);
495 	NIC_BARRIER(nict, nich);
496 
497 	/* Set up DMA byte count. */
498 	bus_space_write_1(nict, nich, ED_P0_RBCR0, savelen);
499 	bus_space_write_1(nict, nich, ED_P0_RBCR1, savelen >> 8);
500 
501 	/* Set up destination address in NIC mem. */
502 	bus_space_write_1(nict, nich, ED_P0_RSAR0, buf);
503 	bus_space_write_1(nict, nich, ED_P0_RSAR1, buf >> 8);
504 
505 	/* Set remote DMA write. */
506 	NIC_BARRIER(nict, nich);
507 	bus_space_write_1(nict, nich,
508 	    ED_P0_CR, ED_CR_RD1 | ED_CR_PAGE_0 | ED_CR_STA);
509 	NIC_BARRIER(nict, nich);
510 
511 	/*
512 	 * Transfer the mbuf chain to the NIC memory.  NE2000 cards
513 	 * require that data be transferred as words, and only words,
514 	 * so that case requires some extra code to patch over odd-length
515 	 * mbufs.
516 	 */
517 	if (nsc->sc_type == NE2000_TYPE_NE1000) {
518 		/* NE1000s are easy. */
519 		for (; m != 0; m = m->m_next) {
520 			if (m->m_len) {
521 				bus_space_write_multi_1(asict, asich,
522 				    NE2000_ASIC_DATA, mtod(m, u_int8_t *),
523 				    m->m_len);
524 			}
525 		}
526 	} else {
527 		/* NE2000s are a bit trickier. */
528 		u_int8_t *data, savebyte[2];
529 		int l, leftover;
530 #ifdef DIAGNOSTIC
531 		u_int8_t *lim;
532 #endif
533 		/* Start out with no leftover data. */
534 		leftover = 0;
535 		savebyte[0] = savebyte[1] = 0;
536 
537 		for (; m != 0; m = m->m_next) {
538 			l = m->m_len;
539 			if (l == 0)
540 				continue;
541 			data = mtod(m, u_int8_t *);
542 #ifdef DIAGNOSTIC
543 			lim = data + l;
544 #endif
545 			while (l > 0) {
546 				if (leftover) {
547 					/*
548 					 * Data left over (from mbuf or
549 					 * realignment).  Buffer the next
550 					 * byte, and write it and the
551 					 * leftover data out.
552 					 */
553 					savebyte[1] = *data++;
554 					l--;
555 					bus_space_write_stream_2(asict, asich,
556 					    NE2000_ASIC_DATA,
557 					    *(u_int16_t *)savebyte);
558 					leftover = 0;
559 				} else if (BUS_SPACE_ALIGNED_POINTER(data,
560 					   u_int16_t) == 0) {
561 					/*
562 					 * Unaligned data; buffer the next
563 					 * byte.
564 					 */
565 					savebyte[0] = *data++;
566 					l--;
567 					leftover = 1;
568 				} else {
569 					/*
570 					 * Aligned data; output contiguous
571 					 * words as much as we can, then
572 					 * buffer the remaining byte, if any.
573 					 */
574 					leftover = l & 1;
575 					l &= ~1;
576 					bus_space_write_multi_stream_2(asict,
577 					    asich, NE2000_ASIC_DATA,
578 					    (u_int16_t *)data, l >> 1);
579 					data += l;
580 					if (leftover)
581 						savebyte[0] = *data++;
582 					l = 0;
583 				}
584 			}
585 			if (l < 0)
586 				panic("ne2000_write_mbuf: negative len");
587 #ifdef DIAGNOSTIC
588 			if (data != lim)
589 				panic("ne2000_write_mbuf: data != lim");
590 #endif
591 		}
592 		if (leftover) {
593 			savebyte[1] = 0;
594 			bus_space_write_stream_2(asict, asich, NE2000_ASIC_DATA,
595 			    *(u_int16_t *)savebyte);
596 		}
597 	}
598 	NIC_BARRIER(nict, nich);
599 
600 	/*
601 	 * Wait for remote DMA to complete.  This is necessary because on the
602 	 * transmit side, data is handled internally by the NIC in bursts, and
603 	 * we can't start another remote DMA until this one completes.  Not
604 	 * waiting causes really bad things to happen - like the NIC wedging
605 	 * the bus.
606 	 */
607 	while (((bus_space_read_1(nict, nich, ED_P0_ISR) & ED_ISR_RDC) !=
608 	    ED_ISR_RDC) && --maxwait) {
609 		bus_space_read_1(nict, nich, ED_P0_CRDA1);
610 		bus_space_read_1(nict, nich, ED_P0_CRDA0);
611 		NIC_BARRIER(nict, nich);
612 		DELAY(1);
613 	}
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 'amout' of a packet from
627  * the ring buffer into a linear destination buffer.  Takes into account
628  * ring-wrap.
629  */
630 int
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
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 = bswap16(hdr->count);
677 #endif
678 }
679 
680 int
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
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 	NIC_BARRIER(nict, nich);
708 	bus_space_write_1(nict, nich, ED_P0_CR,
709 	    ED_CR_RD2 | ED_CR_PAGE_0 | ED_CR_STA);
710 	NIC_BARRIER(nict, nich);
711 
712 	/* Round up to a word. */
713 	if (amount & 1)
714 		++amount;
715 
716 	/* Set up DMA byte count. */
717 	bus_space_write_1(nict, nich, ED_P0_RBCR0, amount);
718 	bus_space_write_1(nict, nich, ED_P0_RBCR1, amount >> 8);
719 
720 	/* Set up source address in NIC mem. */
721 	bus_space_write_1(nict, nich, ED_P0_RSAR0, src);
722 	bus_space_write_1(nict, nich, ED_P0_RSAR1, src >> 8);
723 
724 	NIC_BARRIER(nict, nich);
725 	bus_space_write_1(nict, nich, ED_P0_CR,
726 	    ED_CR_RD0 | ED_CR_PAGE_0 | ED_CR_STA);
727 
728 	bus_space_barrier(nict, nich, 0, NE2000_NPORTS,
729 			  BUS_SPACE_BARRIER_READ | BUS_SPACE_BARRIER_WRITE);
730 	if (useword)
731 		bus_space_read_multi_stream_2(asict, asich, NE2000_ASIC_DATA,
732 		    (u_int16_t *)dst, amount >> 1);
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
743 ne2000_writemem(nict, nich, asict, asich, src, dst, len, useword, quiet)
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 	int quiet;
753 {
754 	int maxwait = 100;	/* about 120us */
755 
756 	/* Select page 0 registers. */
757 	NIC_BARRIER(nict, nich);
758 	bus_space_write_1(nict, nich, ED_P0_CR,
759 	    ED_CR_RD2 | ED_CR_PAGE_0 | ED_CR_STA);
760 	NIC_BARRIER(nict, nich);
761 
762 	/* Reset remote DMA complete flag. */
763 	bus_space_write_1(nict, nich, ED_P0_ISR, ED_ISR_RDC);
764 	NIC_BARRIER(nict, nich);
765 
766 	/* Set up DMA byte count. */
767 	bus_space_write_1(nict, nich, ED_P0_RBCR0, len);
768 	bus_space_write_1(nict, nich, ED_P0_RBCR1, len >> 8);
769 
770 	/* Set up destination address in NIC mem. */
771 	bus_space_write_1(nict, nich, ED_P0_RSAR0, dst);
772 	bus_space_write_1(nict, nich, ED_P0_RSAR1, dst >> 8);
773 
774 	/* Set remote DMA write. */
775 	NIC_BARRIER(nict, nich);
776 	bus_space_write_1(nict, nich, ED_P0_CR,
777 	    ED_CR_RD1 | ED_CR_PAGE_0 | ED_CR_STA);
778 
779 	bus_space_barrier(nict, nich, 0, NE2000_NPORTS,
780 			  BUS_SPACE_BARRIER_READ | BUS_SPACE_BARRIER_WRITE);
781 	if (useword)
782 		bus_space_write_multi_stream_2(asict, asich, NE2000_ASIC_DATA,
783 		    (u_int16_t *)src, len >> 1);
784 	else
785 		bus_space_write_multi_1(asict, asich, NE2000_ASIC_DATA,
786 		    src, len);
787 
788 	/*
789 	 * Wait for remote DMA to complete.  This is necessary because on the
790 	 * transmit side, data is handled internally by the NIC in bursts, and
791 	 * we can't start another remote DMA until this one completes.  Not
792 	 * waiting causes really bad things to happen - like the NIC wedging
793 	 * the bus.
794 	 */
795 	bus_space_barrier(nict, nich, 0, NE2000_NPORTS,
796 			  BUS_SPACE_BARRIER_READ | BUS_SPACE_BARRIER_WRITE);
797 	while (((bus_space_read_1(nict, nich, ED_P0_ISR) & ED_ISR_RDC) !=
798 	    ED_ISR_RDC) && --maxwait)
799 		DELAY(1);
800 
801 	if (!quiet && maxwait == 0)
802 		printf("ne2000_writemem: failed to complete\n");
803 }
804 
805 int
806 ne2000_detach(sc, flags)
807 	struct ne2000_softc *sc;
808 	int flags;
809 {
810 
811 	return (dp8390_detach(&sc->sc_dp8390, flags));
812 }
813 
814 #ifdef IPKDB_NE
815 /*
816  * This code is essentially the same as ne2000_attach above.
817  */
818 int
819 ne2000_ipkdb_attach(kip)
820 	struct ipkdb_if *kip;
821 {
822 	struct ne2000_softc *np = kip->port;
823 	struct dp8390_softc *dp = &np->sc_dp8390;
824 	bus_space_tag_t nict = dp->sc_regt;
825 	bus_space_handle_t nich = dp->sc_regh;
826 	int i, useword;
827 
828 #ifdef GWETHER
829 	/* Not supported (yet?) */
830 	return -1;
831 #endif
832 
833 	if (np->sc_type == 0)
834 		np->sc_type = ne2000_detect(nict, nich,
835 			np->sc_asict, np->sc_asich);
836 	if (np->sc_type == 0)
837 		return -1;
838 
839 	useword = NE2000_USE_WORD(np);
840 
841 	dp->cr_proto = ED_CR_RD2;
842 	dp->dcr_reg = ED_DCR_FT1 | ED_DCR_LS | (useword ? ED_DCR_WTS : 0);
843 	dp->rcr_proto = 0;
844 
845 	dp->test_mem = ne2000_test_mem;
846 	dp->ring_copy = ne2000_ring_copy;
847 	dp->write_mbuf = ne2000_write_mbuf;
848 	dp->read_hdr = ne2000_read_hdr;
849 
850 	for (i = 0; i < 16; i++)
851 		dp->sc_reg_map[i] = i;
852 
853 	switch (np->sc_type) {
854 	case NE2000_TYPE_NE1000:
855 		dp->mem_start = dp->mem_size = 8192;
856 		kip->name = "ne1000";
857 		break;
858 	case NE2000_TYPE_NE2000:
859 		dp->mem_start = dp->mem_size = 8192 * 2;
860 		kip->name = "ne2000";
861 		break;
862 	case NE2000_TYPE_DL10019:
863 		dp->mem_start = dp->mem_size = 8192 * 3;
864 		kip->name = "dl10019";
865 		break;
866 	case NE2000_TYPE_AX88190:
867 		dp->rcr_proto = ED_RCR_INTT;
868 		dp->sc_flags |= DP8390_DO_AX88190_WORKAROUND;
869 		dp->mem_start = dp->mem_size = 8192 * 2;
870 		kip->name = "ax88190";
871 		break;
872 	}
873 
874 	if (dp8390_ipkdb_attach(kip))
875 		return -1;
876 
877 	dp->mem_ring = dp->mem_start
878 		+ ((dp->txb_cnt * ED_TXBUF_SIZE) << ED_PAGE_SHIFT);
879 
880 	if (!(kip->flags & IPKDB_MYHW)) {
881 		char romdata[16];
882 
883 		/* Read the station address. */
884 		if (np->sc_type == NE2000_TYPE_AX88190) {
885 			/* Select page 0 registers. */
886 			NIC_BARRIER(nict, nich);
887 			bus_space_write_1(nict, nich, ED_P0_CR,
888 				ED_CR_RD2 | ED_CR_PAGE_0 | ED_CR_STA);
889 			NIC_BARRIER(nict, nich);
890 			/* Select word transfer */
891 			bus_space_write_1(nict, nich, ED_P0_DCR, ED_DCR_WTS);
892 			ne2000_readmem(nict, nich, np->sc_asict, np->sc_asich,
893 				NE2000_AX88190_NODEID_OFFSET, kip->myenetaddr,
894 				ETHER_ADDR_LEN, useword);
895 		} else {
896 			ne2000_readmem(nict, nich, np->sc_asict, np->sc_asich,
897 				0, romdata, sizeof romdata, useword);
898 			useword = useword ? 2 : 1;
899 			for (i = 0; i < ETHER_ADDR_LEN; i++)
900 				kip->myenetaddr[i] = romdata[i * useword];
901 		}
902 		kip->flags |= IPKDB_MYHW;
903 
904 	}
905 	dp8390_stop(dp);
906 
907 	return 0;
908 }
909 #endif
910