xref: /netbsd-src/sys/dev/ic/ne2000.c (revision 404fbe5fb94ca1e054339640cabb2801ce52dd30)
1 /*	$NetBSD: ne2000.c,v 1.59 2008/04/28 20:23:50 martin 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  *
20  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
21  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
24  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30  * POSSIBILITY OF SUCH DAMAGE.
31  */
32 
33 /*
34  * Device driver for National Semiconductor DS8390/WD83C690 based ethernet
35  * adapters.
36  *
37  * Copyright (c) 1994, 1995 Charles M. Hannum.  All rights reserved.
38  *
39  * Copyright (C) 1993, David Greenman.  This software may be used, modified,
40  * copied, distributed, and sold, in both source and binary form provided that
41  * the above copyright and these terms are retained.  Under no circumstances is
42  * the author responsible for the proper functioning of this software, nor does
43  * the author assume any responsibility for damages incurred with its use.
44  */
45 
46 /*
47  * Common code shared by all NE2000-compatible Ethernet interfaces.
48  */
49 
50 #include <sys/cdefs.h>
51 __KERNEL_RCSID(0, "$NetBSD: ne2000.c,v 1.59 2008/04/28 20:23:50 martin Exp $");
52 
53 #include "opt_ipkdb.h"
54 
55 #include <sys/param.h>
56 #include <sys/systm.h>
57 #include <sys/device.h>
58 #include <sys/socket.h>
59 #include <sys/mbuf.h>
60 #include <sys/syslog.h>
61 
62 #include <net/if.h>
63 #include <net/if_dl.h>
64 #include <net/if_types.h>
65 #include <net/if_media.h>
66 
67 #include <net/if_ether.h>
68 
69 #include <sys/bswap.h>
70 #include <sys/bus.h>
71 
72 #ifndef __BUS_SPACE_HAS_STREAM_METHODS
73 #define	bus_space_write_stream_2	bus_space_write_2
74 #define	bus_space_write_multi_stream_2	bus_space_write_multi_2
75 #define	bus_space_read_multi_stream_2	bus_space_read_multi_2
76 #endif /* __BUS_SPACE_HAS_STREAM_METHODS */
77 
78 #ifdef IPKDB_NE
79 #include <ipkdb/ipkdb.h>
80 #endif
81 
82 #include <dev/ic/dp8390reg.h>
83 #include <dev/ic/dp8390var.h>
84 
85 #include <dev/ic/ne2000reg.h>
86 #include <dev/ic/ne2000var.h>
87 
88 #include <dev/ic/ax88190reg.h>
89 
90 int	ne2000_write_mbuf(struct dp8390_softc *, struct mbuf *, int);
91 int	ne2000_ring_copy(struct dp8390_softc *, int, void *, u_short);
92 void	ne2000_read_hdr(struct dp8390_softc *, int, struct dp8390_ring *);
93 int	ne2000_test_mem(struct dp8390_softc *);
94 
95 void	ne2000_writemem(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(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 #define	ASIC_BARRIER(asict, asich) \
102 	bus_space_barrier((asict), (asich), 0, 0x10, \
103 	    BUS_SPACE_BARRIER_READ | BUS_SPACE_BARRIER_WRITE)
104 
105 int
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 == NE2000_TYPE_UNKNOWN)
123 		nsc->sc_type = ne2000_detect(nict, nich, asict, asich);
124 
125 	/*
126 	 * 8k of memory for NE1000, 16k for NE2000 and 24k for the
127 	 * card uses DL10019.
128 	 */
129 	switch (nsc->sc_type) {
130 	case NE2000_TYPE_UNKNOWN:
131 	default:
132 		aprint_error_dev(dsc->sc_dev, "where did the card go?\n");
133 		return (1);
134 	case NE2000_TYPE_NE1000:
135 		memsize = 8192;
136 		useword = 0;
137 		break;
138 	case NE2000_TYPE_NE2000:
139 	case NE2000_TYPE_AX88190:		/* XXX really? */
140 	case NE2000_TYPE_AX88790:
141 		memsize = 8192 * 2;
142 		useword = 1;
143 		break;
144 	case NE2000_TYPE_DL10019:
145 	case NE2000_TYPE_DL10022:
146 		memsize = 8192 * 3;
147 		useword = 1;
148 		break;
149 	}
150 
151 	nsc->sc_useword = useword;
152 
153 	dsc->cr_proto = ED_CR_RD2;
154 	if (nsc->sc_type == NE2000_TYPE_AX88190 ||
155 	    nsc->sc_type == NE2000_TYPE_AX88790) {
156 		dsc->rcr_proto = ED_RCR_INTT;
157 		dsc->sc_flags |= DP8390_DO_AX88190_WORKAROUND;
158 	} else
159 		dsc->rcr_proto = 0;
160 
161 	/*
162 	 * DCR gets:
163 	 *
164 	 *	FIFO threshold to 8, No auto-init Remote DMA,
165 	 *	byte order=80x86.
166 	 *
167 	 * NE1000 gets byte-wide DMA, NE2000 gets word-wide DMA.
168 	 */
169 	dsc->dcr_reg = ED_DCR_FT1 | ED_DCR_LS | (useword ? ED_DCR_WTS : 0);
170 
171 	dsc->test_mem = ne2000_test_mem;
172 	dsc->ring_copy = ne2000_ring_copy;
173 	dsc->write_mbuf = ne2000_write_mbuf;
174 	dsc->read_hdr = ne2000_read_hdr;
175 
176 	/* Registers are linear. */
177 	for (i = 0; i < 16; i++)
178 		dsc->sc_reg_map[i] = i;
179 
180 	/*
181 	 * NIC memory doens't start at zero on an NE board.
182 	 * The start address is tied to the bus width.
183 	 * (It happens to be computed the same way as mem size.)
184 	 */
185 	dsc->mem_start = memsize;
186 
187 #ifdef GWETHER
188 	{
189 		int x, mstart = 0;
190 		int8_t pbuf0[ED_PAGE_SIZE], pbuf[ED_PAGE_SIZE],
191 		    tbuf[ED_PAGE_SIZE];
192 
193 		for (i = 0; i < ED_PAGE_SIZE; i++)
194 			pbuf0[i] = 0;
195 
196 		/* Search for the start of RAM. */
197 		for (x = 1; x < 256; x++) {
198 			ne2000_writemem(nict, nich, asict, asich, pbuf0,
199 			    x << ED_PAGE_SHIFT, ED_PAGE_SIZE, useword, 0);
200 			ne2000_readmem(nict, nich, asict, asich,
201 			    x << ED_PAGE_SHIFT, tbuf, ED_PAGE_SIZE, useword);
202 			if (memcmp(pbuf0, tbuf, ED_PAGE_SIZE) == 0) {
203 				for (i = 0; i < ED_PAGE_SIZE; i++)
204 					pbuf[i] = 255 - x;
205 				ne2000_writemem(nict, nich, asict, asich,
206 				    pbuf, x << ED_PAGE_SHIFT, ED_PAGE_SIZE,
207 				    useword, 0);
208 				ne2000_readmem(nict, nich, asict, asich,
209 				    x << ED_PAGE_SHIFT, tbuf, ED_PAGE_SIZE,
210 				    useword);
211 				if (memcmp(pbuf, tbuf, ED_PAGE_SIZE) == 0) {
212 					mstart = x << ED_PAGE_SHIFT;
213 					memsize = ED_PAGE_SIZE;
214 					break;
215 				}
216 			}
217 		}
218 
219 		if (mstart == 0) {
220 			aprint_error_dev(&dsc->sc_dev, "cannot find start of RAM\n");
221 			return (1);
222 		}
223 
224 		/* Search for the end of RAM. */
225 		for (++x; x < 256; x++) {
226 			ne2000_writemem(nict, nich, asict, asich, pbuf0,
227 			    x << ED_PAGE_SHIFT, ED_PAGE_SIZE, useword, 0);
228 			ne2000_readmem(nict, nich, asict, asich,
229 			    x << ED_PAGE_SHIFT, tbuf, ED_PAGE_SIZE, useword);
230 			if (memcmp(pbuf0, tbuf, ED_PAGE_SIZE) == 0) {
231 				for (i = 0; i < ED_PAGE_SIZE; i++)
232 					pbuf[i] = 255 - x;
233 				ne2000_writemem(nict, nich, asict, asich,
234 				    pbuf, x << ED_PAGE_SHIFT, ED_PAGE_SIZE,
235 				    useword, 0);
236 				ne2000_readmem(nict, nich, asict, asich,
237 				    x << ED_PAGE_SHIFT, tbuf, ED_PAGE_SIZE,
238 				    useword);
239 				if (memcmp(pbuf, tbuf, ED_PAGE_SIZE) == 0)
240 					memsize += ED_PAGE_SIZE;
241 				else
242 					break;
243 			} else
244 				break;
245 		}
246 
247 		printf("%s: RAM start 0x%x, size %d\n",
248 		    device_xname(&dsc->sc_dev), mstart, memsize);
249 
250 		dsc->mem_start = mstart;
251 	}
252 #endif /* GWETHER */
253 
254 	dsc->mem_size = memsize;
255 
256 	if (myea == NULL) {
257 		/* Read the station address. */
258 		if (nsc->sc_type == NE2000_TYPE_AX88190 ||
259 		    nsc->sc_type == NE2000_TYPE_AX88790) {
260 			/* Select page 0 registers. */
261 			NIC_BARRIER(nict, nich);
262 			bus_space_write_1(nict, nich, ED_P0_CR,
263 			    ED_CR_RD2 | ED_CR_PAGE_0 | ED_CR_STA);
264 			NIC_BARRIER(nict, nich);
265 			/* Select word transfer. */
266 			bus_space_write_1(nict, nich, ED_P0_DCR, ED_DCR_WTS);
267 			NIC_BARRIER(nict, nich);
268 			ne2000_readmem(nict, nich, asict, asich,
269 			    AX88190_NODEID_OFFSET, dsc->sc_enaddr,
270 			    ETHER_ADDR_LEN, useword);
271 		} else {
272 			ne2000_readmem(nict, nich, asict, asich, 0, romdata,
273 			    sizeof(romdata), useword);
274 			for (i = 0; i < ETHER_ADDR_LEN; i++)
275 				dsc->sc_enaddr[i] =
276 				    romdata[i * (useword ? 2 : 1)];
277 		}
278 	} else
279 		memcpy(dsc->sc_enaddr, myea, sizeof(dsc->sc_enaddr));
280 
281 	/* Clear any pending interrupts that might have occurred above. */
282 	NIC_BARRIER(nict, nich);
283 	bus_space_write_1(nict, nich, ED_P0_ISR, 0xff);
284 	NIC_BARRIER(nict, nich);
285 
286 	if (dsc->sc_media_init == NULL)
287 		dsc->sc_media_init = dp8390_media_init;
288 
289 	if (dp8390_config(dsc)) {
290 		aprint_error_dev(dsc->sc_dev, "setup failed\n");
291 		return (1);
292 	}
293 
294 	/*
295 	 * We need to compute mem_ring a bit differently; override the
296 	 * value set up in dp8390_config().
297 	 */
298 	dsc->mem_ring =
299 	    dsc->mem_start + ((dsc->txb_cnt * ED_TXBUF_SIZE) << ED_PAGE_SHIFT);
300 
301 	return (0);
302 }
303 
304 /*
305  * Detect an NE-2000 or compatible.  Returns a model code.
306  */
307 int
308 ne2000_detect(nict, nich, asict, asich)
309 	bus_space_tag_t nict;
310 	bus_space_handle_t nich;
311 	bus_space_tag_t asict;
312 	bus_space_handle_t asich;
313 {
314 	static u_int8_t test_pattern[32] = "THIS is A memory TEST pattern";
315 	u_int8_t test_buffer[32], tmp;
316 	int i, rv = NE2000_TYPE_UNKNOWN;
317 
318 	/* Reset the board. */
319 #ifdef GWETHER
320 	bus_space_write_1(asict, asich, NE2000_ASIC_RESET, 0);
321 	ASIC_BARRIER(asict, asich);
322 	delay(200);
323 #endif /* GWETHER */
324 	tmp = bus_space_read_1(asict, asich, NE2000_ASIC_RESET);
325 	ASIC_BARRIER(asict, asich);
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 	ASIC_BARRIER(asict, asich);
338 	delay(5000);
339 
340 	/*
341 	 * This is needed because some NE clones apparently don't reset the
342 	 * NIC properly (or the NIC chip doesn't reset fully on power-up).
343 	 * XXX - this makes the probe invasive!  Done against my better
344 	 * judgement.  -DLG
345 	 */
346 	bus_space_write_1(nict, nich, ED_P0_CR,
347 	    ED_CR_RD2 | ED_CR_PAGE_0 | ED_CR_STP);
348 	NIC_BARRIER(nict, nich);
349 
350 	delay(5000);
351 
352 	/*
353 	 * Generic probe routine for testing for the existence of a DS8390.
354 	 * Must be performed  after the NIC has just been reset.  This
355 	 * works by looking at certain register values that are guaranteed
356 	 * to be initialized a certain way after power-up or reset.
357 	 *
358 	 * Specifically:
359 	 *
360 	 *	Register		reset bits	set bits
361 	 *	--------		----------	--------
362 	 *	CR			TXP, STA	RD2, STP
363 	 *	ISR					RST
364 	 *	IMR			<all>
365 	 *	DCR					LAS
366 	 *	TCR			LB1, LB0
367 	 *
368 	 * We only look at CR and ISR, however, since looking at the others
369 	 * would require changing register pages, which would be intrusive
370 	 * if this isn't an 8390.
371 	 */
372 
373 	tmp = bus_space_read_1(nict, nich, ED_P0_CR);
374 	if ((tmp & (ED_CR_RD2 | ED_CR_TXP | ED_CR_STA | ED_CR_STP)) !=
375 	    (ED_CR_RD2 | ED_CR_STP))
376 		goto out;
377 
378 	tmp = bus_space_read_1(nict, nich, ED_P0_ISR);
379 	if ((tmp & ED_ISR_RST) != ED_ISR_RST)
380 		goto out;
381 
382 	bus_space_write_1(nict, nich,
383 	    ED_P0_CR, ED_CR_RD2 | ED_CR_PAGE_0 | ED_CR_STA);
384 	NIC_BARRIER(nict, nich);
385 
386 	for (i = 0; i < 100; i++) {
387 		if ((bus_space_read_1(nict, nich, ED_P0_ISR) & ED_ISR_RST) ==
388 		    ED_ISR_RST) {
389 			/* Ack the reset bit. */
390 			bus_space_write_1(nict, nich, ED_P0_ISR, ED_ISR_RST);
391 			NIC_BARRIER(nict, nich);
392 			break;
393 		}
394 		delay(100);
395 	}
396 
397 #if 0
398 	/* XXX */
399 	if (i == 100)
400 		goto out;
401 #endif
402 
403 	/*
404 	 * Test the ability to read and write to the NIC memory.  This has
405 	 * the side effect of determining if this is an NE1000 or an NE2000.
406 	 */
407 
408 	/*
409 	 * This prevents packets from being stored in the NIC memory when
410 	 * the readmem routine turns on the start bit in the CR.
411 	 */
412 	bus_space_write_1(nict, nich, ED_P0_RCR, ED_RCR_MON);
413 	NIC_BARRIER(nict, nich);
414 
415 	/* Temporarily initialize DCR for byte operations. */
416 	bus_space_write_1(nict, nich, ED_P0_DCR, ED_DCR_FT1 | ED_DCR_LS);
417 
418 	bus_space_write_1(nict, nich, ED_P0_PSTART, 8192 >> ED_PAGE_SHIFT);
419 	bus_space_write_1(nict, nich, ED_P0_PSTOP, 16384 >> ED_PAGE_SHIFT);
420 
421 	/*
422 	 * Write a test pattern in byte mode.  If this fails, then there
423 	 * probably isn't any memory at 8k - which likely means that the
424 	 * board is an NE2000.
425 	 */
426 	ne2000_writemem(nict, nich, asict, asich, test_pattern, 8192,
427 	    sizeof(test_pattern), 0, 1);
428 	ne2000_readmem(nict, nich, asict, asich, 8192, test_buffer,
429 	    sizeof(test_buffer), 0);
430 
431 	if (memcmp(test_pattern, test_buffer, sizeof(test_pattern))) {
432 		/* not an NE1000 - try NE2000 */
433 		bus_space_write_1(nict, nich, ED_P0_DCR,
434 		    ED_DCR_WTS | ED_DCR_FT1 | ED_DCR_LS);
435 		bus_space_write_1(nict, nich, ED_P0_PSTART,
436 		    16384 >> ED_PAGE_SHIFT);
437 		bus_space_write_1(nict, nich, ED_P0_PSTOP,
438 		    32768 >> ED_PAGE_SHIFT);
439 
440 		/*
441 		 * Write the test pattern in word mode.  If this also fails,
442 		 * then we don't know what this board is.
443 		 */
444 		ne2000_writemem(nict, nich, asict, asich, test_pattern, 16384,
445 		    sizeof(test_pattern), 1, 0);
446 		ne2000_readmem(nict, nich, asict, asich, 16384, test_buffer,
447 		    sizeof(test_buffer), 1);
448 
449 		if (memcmp(test_pattern, test_buffer, sizeof(test_pattern)))
450 			goto out;	/* not an NE2000 either */
451 
452 		rv = NE2000_TYPE_NE2000;
453 	} else {
454 		/* We're an NE1000. */
455 		rv = NE2000_TYPE_NE1000;
456 	}
457 
458 	/* Clear any pending interrupts that might have occurred above. */
459 	NIC_BARRIER(nict, nich);
460 	bus_space_write_1(nict, nich, ED_P0_ISR, 0xff);
461 
462  out:
463 	return (rv);
464 }
465 
466 /*
467  * Write an mbuf chain to the destination NIC memory address using programmed
468  * I/O.
469  */
470 int
471 ne2000_write_mbuf(sc, m, buf)
472 	struct dp8390_softc *sc;
473 	struct mbuf *m;
474 	int buf;
475 {
476 	struct ne2000_softc *nsc = (struct ne2000_softc *)sc;
477 	bus_space_tag_t nict = sc->sc_regt;
478 	bus_space_handle_t nich = sc->sc_regh;
479 	bus_space_tag_t asict = nsc->sc_asict;
480 	bus_space_handle_t asich = nsc->sc_asich;
481 	int savelen, padlen;
482 	int maxwait = 100;	/* about 120us */
483 
484 	savelen = m->m_pkthdr.len;
485 	if (savelen < ETHER_MIN_LEN - ETHER_CRC_LEN) {
486 		padlen = ETHER_MIN_LEN - ETHER_CRC_LEN - savelen;
487 		savelen = ETHER_MIN_LEN - ETHER_CRC_LEN;
488 	} else
489 		padlen = 0;
490 
491 
492 	/* Select page 0 registers. */
493 	NIC_BARRIER(nict, nich);
494 	bus_space_write_1(nict, nich, ED_P0_CR,
495 	    ED_CR_RD2 | ED_CR_PAGE_0 | ED_CR_STA);
496 	NIC_BARRIER(nict, nich);
497 
498 	/* Reset remote DMA complete flag. */
499 	bus_space_write_1(nict, nich, ED_P0_ISR, ED_ISR_RDC);
500 	NIC_BARRIER(nict, nich);
501 
502 	/* Set up DMA byte count. */
503 	bus_space_write_1(nict, nich, ED_P0_RBCR0, savelen);
504 	bus_space_write_1(nict, nich, ED_P0_RBCR1, savelen >> 8);
505 
506 	/* Set up destination address in NIC mem. */
507 	bus_space_write_1(nict, nich, ED_P0_RSAR0, buf);
508 	bus_space_write_1(nict, nich, ED_P0_RSAR1, buf >> 8);
509 
510 	/* Set remote DMA write. */
511 	NIC_BARRIER(nict, nich);
512 	bus_space_write_1(nict, nich,
513 	    ED_P0_CR, ED_CR_RD1 | ED_CR_PAGE_0 | ED_CR_STA);
514 	NIC_BARRIER(nict, nich);
515 
516 	/*
517 	 * Transfer the mbuf chain to the NIC memory.  NE2000 cards
518 	 * require that data be transferred as words, and only words,
519 	 * so that case requires some extra code to patch over odd-length
520 	 * mbufs.
521 	 */
522 	if (nsc->sc_type == NE2000_TYPE_NE1000) {
523 		/* NE1000s are easy. */
524 		for (; m != 0; m = m->m_next) {
525 			if (m->m_len) {
526 				bus_space_write_multi_1(asict, asich,
527 				    NE2000_ASIC_DATA, mtod(m, u_int8_t *),
528 				    m->m_len);
529 			}
530 		}
531 		if (padlen) {
532 			for(; padlen > 0; padlen--)
533 				bus_space_write_1(asict, asich,
534 				    NE2000_ASIC_DATA, 0);
535 		}
536 	} else {
537 		/* NE2000s are a bit trickier. */
538 		u_int8_t *data, savebyte[2];
539 		int l, leftover;
540 #ifdef DIAGNOSTIC
541 		u_int8_t *lim;
542 #endif
543 		/* Start out with no leftover data. */
544 		leftover = 0;
545 		savebyte[0] = savebyte[1] = 0;
546 
547 		for (; m != 0; m = m->m_next) {
548 			l = m->m_len;
549 			if (l == 0)
550 				continue;
551 			data = mtod(m, u_int8_t *);
552 #ifdef DIAGNOSTIC
553 			lim = data + l;
554 #endif
555 			while (l > 0) {
556 				if (leftover) {
557 					/*
558 					 * Data left over (from mbuf or
559 					 * realignment).  Buffer the next
560 					 * byte, and write it and the
561 					 * leftover data out.
562 					 */
563 					savebyte[1] = *data++;
564 					l--;
565 					bus_space_write_stream_2(asict, asich,
566 					    NE2000_ASIC_DATA,
567 					    *(u_int16_t *)savebyte);
568 					leftover = 0;
569 				} else if (BUS_SPACE_ALIGNED_POINTER(data,
570 					   u_int16_t) == 0) {
571 					/*
572 					 * Unaligned data; buffer the next
573 					 * byte.
574 					 */
575 					savebyte[0] = *data++;
576 					l--;
577 					leftover = 1;
578 				} else {
579 					/*
580 					 * Aligned data; output contiguous
581 					 * words as much as we can, then
582 					 * buffer the remaining byte, if any.
583 					 */
584 					leftover = l & 1;
585 					l &= ~1;
586 					bus_space_write_multi_stream_2(asict,
587 					    asich, NE2000_ASIC_DATA,
588 					    (u_int16_t *)data, l >> 1);
589 					data += l;
590 					if (leftover)
591 						savebyte[0] = *data++;
592 					l = 0;
593 				}
594 			}
595 			if (l < 0)
596 				panic("ne2000_write_mbuf: negative len");
597 #ifdef DIAGNOSTIC
598 			if (data != lim)
599 				panic("ne2000_write_mbuf: data != lim");
600 #endif
601 		}
602 		if (leftover) {
603 			savebyte[1] = 0;
604 			bus_space_write_stream_2(asict, asich, NE2000_ASIC_DATA,
605 			    *(u_int16_t *)savebyte);
606 		}
607 		if (padlen) {
608 			for(; padlen > 1; padlen -= 2)
609 				bus_space_write_stream_2(asict, asich,
610 				    NE2000_ASIC_DATA, 0);
611 		}
612 	}
613 	NIC_BARRIER(nict, nich);
614 
615 	/* AX88796 doesn't seem to have remote DMA complete */
616 	if (sc->sc_flags & DP8390_NO_REMOTE_DMA_COMPLETE)
617 		return(savelen);
618 
619 	/*
620 	 * Wait for remote DMA to complete.  This is necessary because on the
621 	 * transmit side, data is handled internally by the NIC in bursts, and
622 	 * we can't start another remote DMA until this one completes.  Not
623 	 * waiting causes really bad things to happen - like the NIC wedging
624 	 * the bus.
625 	 */
626 	while (((bus_space_read_1(nict, nich, ED_P0_ISR) & ED_ISR_RDC) !=
627 	    ED_ISR_RDC) && --maxwait) {
628 		(void)bus_space_read_1(nict, nich, ED_P0_CRDA1);
629 		(void)bus_space_read_1(nict, nich, ED_P0_CRDA0);
630 		NIC_BARRIER(nict, nich);
631 		DELAY(1);
632 	}
633 
634 	if (maxwait == 0) {
635 		log(LOG_WARNING,
636 		    "%s: remote transmit DMA failed to complete\n",
637 		    device_xname(sc->sc_dev));
638 		dp8390_reset(sc);
639 	}
640 
641 	return (savelen);
642 }
643 
644 /*
645  * Given a source and destination address, copy 'amout' of a packet from
646  * the ring buffer into a linear destination buffer.  Takes into account
647  * ring-wrap.
648  */
649 int
650 ne2000_ring_copy(sc, src, dstv, amount)
651 	struct dp8390_softc *sc;
652 	int src;
653 	void *dstv;
654 	u_short amount;
655 {
656 	char *dst = dstv;
657 	struct ne2000_softc *nsc = (struct ne2000_softc *)sc;
658 	bus_space_tag_t nict = sc->sc_regt;
659 	bus_space_handle_t nich = sc->sc_regh;
660 	bus_space_tag_t asict = nsc->sc_asict;
661 	bus_space_handle_t asich = nsc->sc_asich;
662 	u_short tmp_amount;
663 	int useword = nsc->sc_useword;
664 
665 	/* Does copy wrap to lower addr in ring buffer? */
666 	if (src + amount > sc->mem_end) {
667 		tmp_amount = sc->mem_end - src;
668 
669 		/* Copy amount up to end of NIC memory. */
670 		ne2000_readmem(nict, nich, asict, asich, src,
671 		    (u_int8_t *)dst, tmp_amount, useword);
672 
673 		amount -= tmp_amount;
674 		src = sc->mem_ring;
675 		dst += tmp_amount;
676 	}
677 
678 	ne2000_readmem(nict, nich, asict, asich, src, (u_int8_t *)dst,
679 	    amount, useword);
680 
681 	return (src + amount);
682 }
683 
684 void
685 ne2000_read_hdr(sc, buf, hdr)
686 	struct dp8390_softc *sc;
687 	int buf;
688 	struct dp8390_ring *hdr;
689 {
690 	struct ne2000_softc *nsc = (struct ne2000_softc *)sc;
691 
692 	ne2000_readmem(sc->sc_regt, sc->sc_regh, nsc->sc_asict, nsc->sc_asich,
693 	    buf, (u_int8_t *)hdr, sizeof(struct dp8390_ring),
694 	    nsc->sc_useword);
695 #if BYTE_ORDER == BIG_ENDIAN
696 	hdr->count = bswap16(hdr->count);
697 #endif
698 }
699 
700 int
701 ne2000_test_mem(struct dp8390_softc *sc)
702 {
703 
704 	/* Noop. */
705 	return (0);
706 }
707 
708 /*
709  * Given a NIC memory source address and a host memory destination address,
710  * copy 'amount' from NIC to host using programmed i/o.  The 'amount' is
711  * rounded up to a word - ok as long as mbufs are word sized.
712  */
713 void
714 ne2000_readmem(nict, nich, asict, asich, src, dst, amount, useword)
715 	bus_space_tag_t nict;
716 	bus_space_handle_t nich;
717 	bus_space_tag_t asict;
718 	bus_space_handle_t asich;
719 	int src;
720 	u_int8_t *dst;
721 	size_t amount;
722 	int useword;
723 {
724 
725 	/* Select page 0 registers. */
726 	NIC_BARRIER(nict, nich);
727 	bus_space_write_1(nict, nich, ED_P0_CR,
728 	    ED_CR_RD2 | ED_CR_PAGE_0 | ED_CR_STA);
729 	NIC_BARRIER(nict, nich);
730 
731 	/* Round up to a word. */
732 	if (amount & 1)
733 		++amount;
734 
735 	/* Set up DMA byte count. */
736 	bus_space_write_1(nict, nich, ED_P0_RBCR0, amount);
737 	bus_space_write_1(nict, nich, ED_P0_RBCR1, amount >> 8);
738 
739 	/* Set up source address in NIC mem. */
740 	bus_space_write_1(nict, nich, ED_P0_RSAR0, src);
741 	bus_space_write_1(nict, nich, ED_P0_RSAR1, src >> 8);
742 
743 	NIC_BARRIER(nict, nich);
744 	bus_space_write_1(nict, nich, ED_P0_CR,
745 	    ED_CR_RD0 | ED_CR_PAGE_0 | ED_CR_STA);
746 
747 	ASIC_BARRIER(asict, asich);
748 	if (useword)
749 		bus_space_read_multi_stream_2(asict, asich, NE2000_ASIC_DATA,
750 		    (u_int16_t *)dst, amount >> 1);
751 	else
752 		bus_space_read_multi_1(asict, asich, NE2000_ASIC_DATA,
753 		    dst, amount);
754 }
755 
756 /*
757  * Stripped down routine for writing a linear buffer to NIC memory.  Only
758  * used in the probe routine to test the memory.  'len' must be even.
759  */
760 void
761 ne2000_writemem(nict, nich, asict, asich, src, dst, len, useword, quiet)
762 	bus_space_tag_t nict;
763 	bus_space_handle_t nich;
764 	bus_space_tag_t asict;
765 	bus_space_handle_t asich;
766 	u_int8_t *src;
767 	int dst;
768 	size_t len;
769 	int useword;
770 	int quiet;
771 {
772 	int maxwait = 100;	/* about 120us */
773 
774 	/* Select page 0 registers. */
775 	NIC_BARRIER(nict, nich);
776 	bus_space_write_1(nict, nich, ED_P0_CR,
777 	    ED_CR_RD2 | ED_CR_PAGE_0 | ED_CR_STA);
778 	NIC_BARRIER(nict, nich);
779 
780 	/* Reset remote DMA complete flag. */
781 	bus_space_write_1(nict, nich, ED_P0_ISR, ED_ISR_RDC);
782 	NIC_BARRIER(nict, nich);
783 
784 	/* Set up DMA byte count. */
785 	bus_space_write_1(nict, nich, ED_P0_RBCR0, len);
786 	bus_space_write_1(nict, nich, ED_P0_RBCR1, len >> 8);
787 
788 	/* Set up destination address in NIC mem. */
789 	bus_space_write_1(nict, nich, ED_P0_RSAR0, dst);
790 	bus_space_write_1(nict, nich, ED_P0_RSAR1, dst >> 8);
791 
792 	/* Set remote DMA write. */
793 	NIC_BARRIER(nict, nich);
794 	bus_space_write_1(nict, nich, ED_P0_CR,
795 	    ED_CR_RD1 | ED_CR_PAGE_0 | ED_CR_STA);
796 	NIC_BARRIER(nict, nich);
797 
798 	ASIC_BARRIER(asict, asich);
799 	if (useword)
800 		bus_space_write_multi_stream_2(asict, asich, NE2000_ASIC_DATA,
801 		    (u_int16_t *)src, len >> 1);
802 	else
803 		bus_space_write_multi_1(asict, asich, NE2000_ASIC_DATA,
804 		    src, len);
805 	ASIC_BARRIER(asict, asich);
806 
807 	/*
808 	 * Wait for remote DMA to complete.  This is necessary because on the
809 	 * transmit side, data is handled internally by the NIC in bursts, and
810 	 * we can't start another remote DMA until this one completes.  Not
811 	 * waiting causes really bad things to happen - like the NIC wedging
812 	 * the bus.
813 	 */
814 	while (((bus_space_read_1(nict, nich, ED_P0_ISR) & ED_ISR_RDC) !=
815 	    ED_ISR_RDC) && --maxwait)
816 		DELAY(1);
817 
818 	if (!quiet && maxwait == 0)
819 		printf("ne2000_writemem: failed to complete\n");
820 }
821 
822 int
823 ne2000_detach(sc, flags)
824 	struct ne2000_softc *sc;
825 	int flags;
826 {
827 
828 	return (dp8390_detach(&sc->sc_dp8390, flags));
829 }
830 
831 #ifdef IPKDB_NE
832 /*
833  * This code is essentially the same as ne2000_attach above.
834  */
835 int
836 ne2000_ipkdb_attach(kip)
837 	struct ipkdb_if *kip;
838 {
839 	struct ne2000_softc *np = kip->port;
840 	struct dp8390_softc *dp = &np->sc_dp8390;
841 	bus_space_tag_t nict = dp->sc_regt;
842 	bus_space_handle_t nich = dp->sc_regh;
843 	int i, useword;
844 
845 #ifdef GWETHER
846 	/* Not supported (yet?) */
847 	return -1;
848 #endif
849 
850 	if (np->sc_type == 0)
851 		np->sc_type = ne2000_detect(nict, nich,
852 			np->sc_asict, np->sc_asich);
853 	if (np->sc_type == 0)
854 		return -1;
855 
856 	useword = np->sc_useword;
857 
858 	dp->cr_proto = ED_CR_RD2;
859 	dp->dcr_reg = ED_DCR_FT1 | ED_DCR_LS | (useword ? ED_DCR_WTS : 0);
860 	dp->rcr_proto = 0;
861 
862 	dp->test_mem = ne2000_test_mem;
863 	dp->ring_copy = ne2000_ring_copy;
864 	dp->write_mbuf = ne2000_write_mbuf;
865 	dp->read_hdr = ne2000_read_hdr;
866 
867 	for (i = 0; i < 16; i++)
868 		dp->sc_reg_map[i] = i;
869 
870 	switch (np->sc_type) {
871 	case NE2000_TYPE_NE1000:
872 		dp->mem_start = dp->mem_size = 8192;
873 		kip->name = "ne1000";
874 		break;
875 	case NE2000_TYPE_NE2000:
876 		dp->mem_start = dp->mem_size = 8192 * 2;
877 		kip->name = "ne2000";
878 		break;
879 	case NE2000_TYPE_DL10019:
880 	case NE2000_TYPE_DL10022:
881 		dp->mem_start = dp->mem_size = 8192 * 3;
882 		kip->name = (np->sc_type == NE2000_TYPE_DL10019) ?
883 		    "dl10022" : "dl10019";
884 		break;
885 	case NE2000_TYPE_AX88190:
886 	case NE2000_TYPE_AX88790:
887 		dp->rcr_proto = ED_RCR_INTT;
888 		dp->sc_flags |= DP8390_DO_AX88190_WORKAROUND;
889 		dp->mem_start = dp->mem_size = 8192 * 2;
890 		kip->name = "ax88190";
891 		break;
892 	default:
893 		return -1;
894 		break;
895 	}
896 
897 	if (dp8390_ipkdb_attach(kip))
898 		return -1;
899 
900 	dp->mem_ring = dp->mem_start
901 		+ ((dp->txb_cnt * ED_TXBUF_SIZE) << ED_PAGE_SHIFT);
902 
903 	if (!(kip->flags & IPKDB_MYHW)) {
904 		char romdata[16];
905 
906 		/* Read the station address. */
907 		if (np->sc_type == NE2000_TYPE_AX88190 ||
908 		    np->sc_type == NE2000_TYPE_AX88790) {
909 			/* Select page 0 registers. */
910 			NIC_BARRIER(nict, nich);
911 			bus_space_write_1(nict, nich, ED_P0_CR,
912 				ED_CR_RD2 | ED_CR_PAGE_0 | ED_CR_STA);
913 			NIC_BARRIER(nict, nich);
914 			/* Select word transfer */
915 			bus_space_write_1(nict, nich, ED_P0_DCR, ED_DCR_WTS);
916 			ne2000_readmem(nict, nich, np->sc_asict, np->sc_asich,
917 				AX88190_NODEID_OFFSET, kip->myenetaddr,
918 				ETHER_ADDR_LEN, useword);
919 		} else {
920 			ne2000_readmem(nict, nich, np->sc_asict, np->sc_asich,
921 				0, romdata, sizeof romdata, useword);
922 			for (i = 0; i < ETHER_ADDR_LEN; i++)
923 				kip->myenetaddr[i] = romdata[i << useword];
924 		}
925 		kip->flags |= IPKDB_MYHW;
926 
927 	}
928 	dp8390_stop(dp);
929 
930 	return 0;
931 }
932 #endif
933 
934 void
935 ne2000_power(int why, void *arg)
936 {
937 	struct ne2000_softc *sc = arg;
938 	struct dp8390_softc *dsc = &sc->sc_dp8390;
939 	struct ifnet *ifp = &dsc->sc_ec.ec_if;
940 	int s;
941 
942 	s = splnet();
943 	switch (why) {
944 	case PWR_SUSPEND:
945 	case PWR_STANDBY:
946 		dp8390_stop(dsc);
947 		dp8390_disable(dsc);
948 		break;
949 	case PWR_RESUME:
950 		if (ifp->if_flags & IFF_UP) {
951 			if (dp8390_enable(dsc) == 0)
952 				dp8390_init(dsc);
953 		}
954 		break;
955 	case PWR_SOFTSUSPEND:
956 	case PWR_SOFTSTANDBY:
957 	case PWR_SOFTRESUME:
958 		break;
959 	}
960 	splx(s);
961 }
962