Home | History | Annotate | Line # | Download | only in ic
ne2000.c revision 1.64
      1 /*	$NetBSD: ne2000.c,v 1.64 2010/01/08 20:02:39 dyoung 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.64 2010/01/08 20:02:39 dyoung 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(struct ne2000_softc *nsc, u_int8_t *myea)
    107 {
    108 	struct dp8390_softc *dsc = &nsc->sc_dp8390;
    109 	bus_space_tag_t nict = dsc->sc_regt;
    110 	bus_space_handle_t nich = dsc->sc_regh;
    111 	bus_space_tag_t asict = nsc->sc_asict;
    112 	bus_space_handle_t asich = nsc->sc_asich;
    113 	u_int8_t romdata[16];
    114 	int memsize, i, useword, dmawidth;
    115 
    116 	/*
    117 	 * Detect it again unless caller specified it; this gives us
    118 	 * the memory size.
    119 	 */
    120 	if (nsc->sc_type == NE2000_TYPE_UNKNOWN)
    121 		nsc->sc_type = ne2000_detect(nict, nich, asict, asich);
    122 
    123 	/*
    124 	 * 8k of memory for NE1000, 16k for NE2000 and 24k for the
    125 	 * card uses DL10019.
    126 	 */
    127 	switch (nsc->sc_type) {
    128 	case NE2000_TYPE_UNKNOWN:
    129 	default:
    130 		aprint_error_dev(dsc->sc_dev, "where did the card go?\n");
    131 		return (1);
    132 	case NE2000_TYPE_NE1000:
    133 		memsize = 8192;
    134 		useword = 0;
    135 		dmawidth = NE2000_DMAWIDTH_8BIT;
    136 		break;
    137 	case NE2000_TYPE_NE2000:
    138 	case NE2000_TYPE_AX88190:		/* XXX really? */
    139 	case NE2000_TYPE_AX88790:
    140 		memsize = 8192 * 2;
    141 		useword = 1;
    142 		dmawidth = NE2000_DMAWIDTH_16BIT;
    143 		break;
    144 	case NE2000_TYPE_DL10019:
    145 	case NE2000_TYPE_DL10022:
    146 		memsize = 8192 * 3;
    147 		useword = 1;
    148 		dmawidth = NE2000_DMAWIDTH_16BIT;
    149 		break;
    150 	}
    151 
    152 	nsc->sc_useword = useword;
    153 	if (nsc->sc_dmawidth == NE2000_DMAWIDTH_UNKNOWN)
    154 		nsc->sc_dmawidth = dmawidth;
    155 	else
    156 		dmawidth = nsc->sc_dmawidth;
    157 
    158 	dsc->cr_proto = ED_CR_RD2;
    159 	if (nsc->sc_type == NE2000_TYPE_AX88190 ||
    160 	    nsc->sc_type == NE2000_TYPE_AX88790) {
    161 		dsc->rcr_proto = ED_RCR_INTT;
    162 		dsc->sc_flags |= DP8390_DO_AX88190_WORKAROUND;
    163 	} else
    164 		dsc->rcr_proto = 0;
    165 
    166 	/*
    167 	 * DCR gets:
    168 	 *
    169 	 *	FIFO threshold to 8, No auto-init Remote DMA,
    170 	 *	byte order=80x86.
    171 	 *
    172 	 * NE1000 gets byte-wide DMA, NE2000 gets word-wide DMA.
    173 	 */
    174 	dsc->dcr_reg = ED_DCR_FT1 | ED_DCR_LS |
    175 	    ((dmawidth == NE2000_DMAWIDTH_16BIT) ? ED_DCR_WTS : 0);
    176 
    177 	dsc->test_mem = ne2000_test_mem;
    178 	dsc->ring_copy = ne2000_ring_copy;
    179 	dsc->write_mbuf = ne2000_write_mbuf;
    180 	dsc->read_hdr = ne2000_read_hdr;
    181 
    182 	/* Registers are linear. */
    183 	for (i = 0; i < 16; i++)
    184 		dsc->sc_reg_map[i] = i;
    185 
    186 	/*
    187 	 * NIC memory doens't start at zero on an NE board.
    188 	 * The start address is tied to the bus width.
    189 	 * (It happens to be computed the same way as mem size.)
    190 	 */
    191 	dsc->mem_start = memsize;
    192 
    193 #ifdef GWETHER
    194 	{
    195 		int x, mstart = 0;
    196 		int8_t pbuf0[ED_PAGE_SIZE], pbuf[ED_PAGE_SIZE],
    197 		    tbuf[ED_PAGE_SIZE];
    198 
    199 		for (i = 0; i < ED_PAGE_SIZE; i++)
    200 			pbuf0[i] = 0;
    201 
    202 		/* Search for the start of RAM. */
    203 		for (x = 1; x < 256; x++) {
    204 			ne2000_writemem(nict, nich, asict, asich, pbuf0,
    205 			    x << ED_PAGE_SHIFT, ED_PAGE_SIZE, useword, 0);
    206 			ne2000_readmem(nict, nich, asict, asich,
    207 			    x << ED_PAGE_SHIFT, tbuf, ED_PAGE_SIZE, useword);
    208 			if (memcmp(pbuf0, tbuf, ED_PAGE_SIZE) == 0) {
    209 				for (i = 0; i < ED_PAGE_SIZE; i++)
    210 					pbuf[i] = 255 - x;
    211 				ne2000_writemem(nict, nich, asict, asich,
    212 				    pbuf, x << ED_PAGE_SHIFT, ED_PAGE_SIZE,
    213 				    useword, 0);
    214 				ne2000_readmem(nict, nich, asict, asich,
    215 				    x << ED_PAGE_SHIFT, tbuf, ED_PAGE_SIZE,
    216 				    useword);
    217 				if (memcmp(pbuf, tbuf, ED_PAGE_SIZE) == 0) {
    218 					mstart = x << ED_PAGE_SHIFT;
    219 					memsize = ED_PAGE_SIZE;
    220 					break;
    221 				}
    222 			}
    223 		}
    224 
    225 		if (mstart == 0) {
    226 			aprint_error_dev(&dsc->sc_dev, "cannot find start of RAM\n");
    227 			return (1);
    228 		}
    229 
    230 		/* Search for the end of RAM. */
    231 		for (++x; x < 256; x++) {
    232 			ne2000_writemem(nict, nich, asict, asich, pbuf0,
    233 			    x << ED_PAGE_SHIFT, ED_PAGE_SIZE, useword, 0);
    234 			ne2000_readmem(nict, nich, asict, asich,
    235 			    x << ED_PAGE_SHIFT, tbuf, ED_PAGE_SIZE, useword);
    236 			if (memcmp(pbuf0, tbuf, ED_PAGE_SIZE) == 0) {
    237 				for (i = 0; i < ED_PAGE_SIZE; i++)
    238 					pbuf[i] = 255 - x;
    239 				ne2000_writemem(nict, nich, asict, asich,
    240 				    pbuf, x << ED_PAGE_SHIFT, ED_PAGE_SIZE,
    241 				    useword, 0);
    242 				ne2000_readmem(nict, nich, asict, asich,
    243 				    x << ED_PAGE_SHIFT, tbuf, ED_PAGE_SIZE,
    244 				    useword);
    245 				if (memcmp(pbuf, tbuf, ED_PAGE_SIZE) == 0)
    246 					memsize += ED_PAGE_SIZE;
    247 				else
    248 					break;
    249 			} else
    250 				break;
    251 		}
    252 
    253 		printf("%s: RAM start 0x%x, size %d\n",
    254 		    device_xname(&dsc->sc_dev), mstart, memsize);
    255 
    256 		dsc->mem_start = mstart;
    257 	}
    258 #endif /* GWETHER */
    259 
    260 	dsc->mem_size = memsize;
    261 
    262 	if (myea == NULL) {
    263 		/* Read the station address. */
    264 		if (nsc->sc_type == NE2000_TYPE_AX88190 ||
    265 		    nsc->sc_type == NE2000_TYPE_AX88790) {
    266 			/* Select page 0 registers. */
    267 			NIC_BARRIER(nict, nich);
    268 			bus_space_write_1(nict, nich, ED_P0_CR,
    269 			    ED_CR_RD2 | ED_CR_PAGE_0 | ED_CR_STA);
    270 			NIC_BARRIER(nict, nich);
    271 			/* Select word transfer. */
    272 			bus_space_write_1(nict, nich, ED_P0_DCR,
    273 			    ((dmawidth == NE2000_DMAWIDTH_16BIT) ? ED_DCR_WTS : 0));
    274 			NIC_BARRIER(nict, nich);
    275 			ne2000_readmem(nict, nich, asict, asich,
    276 			    AX88190_NODEID_OFFSET, dsc->sc_enaddr,
    277 			    ETHER_ADDR_LEN, useword);
    278 		} else {
    279 			ne2000_readmem(nict, nich, asict, asich, 0, romdata,
    280 			    sizeof(romdata), useword);
    281 			for (i = 0; i < ETHER_ADDR_LEN; i++)
    282 				dsc->sc_enaddr[i] =
    283 				    romdata[i * (useword ? 2 : 1)];
    284 		}
    285 	} else
    286 		memcpy(dsc->sc_enaddr, myea, sizeof(dsc->sc_enaddr));
    287 
    288 	/* Clear any pending interrupts that might have occurred above. */
    289 	NIC_BARRIER(nict, nich);
    290 	bus_space_write_1(nict, nich, ED_P0_ISR, 0xff);
    291 	NIC_BARRIER(nict, nich);
    292 
    293 	if (dsc->sc_media_init == NULL)
    294 		dsc->sc_media_init = dp8390_media_init;
    295 
    296 	if (dp8390_config(dsc)) {
    297 		aprint_error_dev(dsc->sc_dev, "setup failed\n");
    298 		return (1);
    299 	}
    300 
    301 	/*
    302 	 * We need to compute mem_ring a bit differently; override the
    303 	 * value set up in dp8390_config().
    304 	 */
    305 	dsc->mem_ring =
    306 	    dsc->mem_start + ((dsc->txb_cnt * ED_TXBUF_SIZE) << ED_PAGE_SHIFT);
    307 
    308 	return (0);
    309 }
    310 
    311 /*
    312  * Detect an NE-2000 or compatible.  Returns a model code.
    313  */
    314 int
    315 ne2000_detect(bus_space_tag_t nict, bus_space_handle_t nich, bus_space_tag_t asict, bus_space_handle_t asich)
    316 {
    317 	static u_int8_t test_pattern[32] = "THIS is A memory TEST pattern";
    318 	u_int8_t test_buffer[32], tmp;
    319 	int i, rv = NE2000_TYPE_UNKNOWN;
    320 	int dmawidth = NE2000_DMAWIDTH_16BIT;
    321 
    322  restart:
    323 	/* Reset the board. */
    324 #ifdef GWETHER
    325 	bus_space_write_1(asict, asich, NE2000_ASIC_RESET, 0);
    326 	ASIC_BARRIER(asict, asich);
    327 	delay(200);
    328 #endif /* GWETHER */
    329 	tmp = bus_space_read_1(asict, asich, NE2000_ASIC_RESET);
    330 	ASIC_BARRIER(asict, asich);
    331 	delay(10000);
    332 
    333 	/*
    334 	 * I don't know if this is necessary; probably cruft leftover from
    335 	 * Clarkson packet driver code. Doesn't do a thing on the boards I've
    336 	 * tested. -DG [note that a outb(0x84, 0) seems to work here, and is
    337 	 * non-invasive...but some boards don't seem to reset and I don't have
    338 	 * complete documentation on what the 'right' thing to do is...so we do
    339 	 * the invasive thing for now.  Yuck.]
    340 	 */
    341 	bus_space_write_1(asict, asich, NE2000_ASIC_RESET, tmp);
    342 	ASIC_BARRIER(asict, asich);
    343 	delay(5000);
    344 
    345 	/*
    346 	 * This is needed because some NE clones apparently don't reset the
    347 	 * NIC properly (or the NIC chip doesn't reset fully on power-up).
    348 	 * XXX - this makes the probe invasive!  Done against my better
    349 	 * judgement.  -DLG
    350 	 */
    351 	bus_space_write_1(nict, nich, ED_P0_CR,
    352 	    ED_CR_RD2 | ED_CR_PAGE_0 | ED_CR_STP);
    353 	NIC_BARRIER(nict, nich);
    354 
    355 	delay(5000);
    356 
    357 	/*
    358 	 * Generic probe routine for testing for the existence of a DS8390.
    359 	 * Must be performed  after the NIC has just been reset.  This
    360 	 * works by looking at certain register values that are guaranteed
    361 	 * to be initialized a certain way after power-up or reset.
    362 	 *
    363 	 * Specifically:
    364 	 *
    365 	 *	Register		reset bits	set bits
    366 	 *	--------		----------	--------
    367 	 *	CR			TXP, STA	RD2, STP
    368 	 *	ISR					RST
    369 	 *	IMR			<all>
    370 	 *	DCR					LAS
    371 	 *	TCR			LB1, LB0
    372 	 *
    373 	 * We only look at CR and ISR, however, since looking at the others
    374 	 * would require changing register pages, which would be intrusive
    375 	 * if this isn't an 8390.
    376 	 */
    377 
    378 	tmp = bus_space_read_1(nict, nich, ED_P0_CR);
    379 	if ((tmp & (ED_CR_RD2 | ED_CR_TXP | ED_CR_STA | ED_CR_STP)) !=
    380 	    (ED_CR_RD2 | ED_CR_STP))
    381 		goto out;
    382 
    383 	tmp = bus_space_read_1(nict, nich, ED_P0_ISR);
    384 	if ((tmp & ED_ISR_RST) != ED_ISR_RST)
    385 		goto out;
    386 
    387 	bus_space_write_1(nict, nich,
    388 	    ED_P0_CR, ED_CR_RD2 | ED_CR_PAGE_0 | ED_CR_STA);
    389 	NIC_BARRIER(nict, nich);
    390 
    391 	for (i = 0; i < 100; i++) {
    392 		if ((bus_space_read_1(nict, nich, ED_P0_ISR) & ED_ISR_RST) ==
    393 		    ED_ISR_RST) {
    394 			/* Ack the reset bit. */
    395 			bus_space_write_1(nict, nich, ED_P0_ISR, ED_ISR_RST);
    396 			NIC_BARRIER(nict, nich);
    397 			break;
    398 		}
    399 		delay(100);
    400 	}
    401 
    402 #if 0
    403 	/* XXX */
    404 	if (i == 100)
    405 		goto out;
    406 #endif
    407 
    408 	/*
    409 	 * Test the ability to read and write to the NIC memory.  This has
    410 	 * the side effect of determining if this is an NE1000 or an NE2000.
    411 	 */
    412 
    413 	/*
    414 	 * This prevents packets from being stored in the NIC memory when
    415 	 * the readmem routine turns on the start bit in the CR.
    416 	 */
    417 	bus_space_write_1(nict, nich, ED_P0_RCR, ED_RCR_MON);
    418 	NIC_BARRIER(nict, nich);
    419 
    420 	/* Temporarily initialize DCR for byte operations. */
    421 	bus_space_write_1(nict, nich, ED_P0_DCR, ED_DCR_FT1 | ED_DCR_LS);
    422 
    423 	bus_space_write_1(nict, nich, ED_P0_PSTART, 8192 >> ED_PAGE_SHIFT);
    424 	bus_space_write_1(nict, nich, ED_P0_PSTOP, 16384 >> ED_PAGE_SHIFT);
    425 
    426 	/*
    427 	 * Write a test pattern in byte mode.  If this fails, then there
    428 	 * probably isn't any memory at 8k - which likely means that the
    429 	 * board is an NE2000.
    430 	 */
    431 	ne2000_writemem(nict, nich, asict, asich, test_pattern, 8192,
    432 	    sizeof(test_pattern), 0, 1);
    433 	ne2000_readmem(nict, nich, asict, asich, 8192, test_buffer,
    434 	    sizeof(test_buffer), 0);
    435 
    436 	if (memcmp(test_pattern, test_buffer, sizeof(test_pattern))) {
    437 		/* not an NE1000 - try NE2000 */
    438 		bus_space_write_1(nict, nich, ED_P0_DCR, ED_DCR_FT1 | ED_DCR_LS
    439 		    | ((dmawidth == NE2000_DMAWIDTH_16BIT) ? ED_DCR_WTS : 0));
    440 		bus_space_write_1(nict, nich, ED_P0_PSTART,
    441 		    16384 >> ED_PAGE_SHIFT);
    442 		bus_space_write_1(nict, nich, ED_P0_PSTOP,
    443 		    32768 >> ED_PAGE_SHIFT);
    444 
    445 		/*
    446 		 * Write the test pattern in word mode.  If this also fails,
    447 		 * then we don't know what this board is.
    448 		 */
    449 		ne2000_writemem(nict, nich, asict, asich, test_pattern, 16384,
    450 		    sizeof(test_pattern), 1, 0);
    451 		ne2000_readmem(nict, nich, asict, asich, 16384, test_buffer,
    452 		    sizeof(test_buffer), 1);
    453 
    454 		if (memcmp(test_pattern, test_buffer, sizeof(test_pattern))) {
    455 			if (dmawidth == NE2000_DMAWIDTH_16BIT) {
    456 				/* try 8bit dma */
    457 				dmawidth = NE2000_DMAWIDTH_8BIT;
    458 				goto restart;
    459 			}
    460 			goto out;	/* not an NE2000 either */
    461 		}
    462 
    463 		rv = NE2000_TYPE_NE2000;
    464 	} else {
    465 		/* We're an NE1000. */
    466 		rv = NE2000_TYPE_NE1000;
    467 		dmawidth = NE2000_DMAWIDTH_8BIT;
    468 	}
    469 
    470 	/* Clear any pending interrupts that might have occurred above. */
    471 	NIC_BARRIER(nict, nich);
    472 	bus_space_write_1(nict, nich, ED_P0_ISR, 0xff);
    473 
    474  out:
    475 	return (rv);
    476 }
    477 
    478 /*
    479  * Write an mbuf chain to the destination NIC memory address using programmed
    480  * I/O.
    481  */
    482 int
    483 ne2000_write_mbuf(struct dp8390_softc *sc, struct mbuf *m, int buf)
    484 {
    485 	struct ne2000_softc *nsc = (struct ne2000_softc *)sc;
    486 	bus_space_tag_t nict = sc->sc_regt;
    487 	bus_space_handle_t nich = sc->sc_regh;
    488 	bus_space_tag_t asict = nsc->sc_asict;
    489 	bus_space_handle_t asich = nsc->sc_asich;
    490 	int savelen, padlen;
    491 	int maxwait = 100;	/* about 120us */
    492 
    493 	savelen = m->m_pkthdr.len;
    494 	if (savelen < ETHER_MIN_LEN - ETHER_CRC_LEN) {
    495 		padlen = ETHER_MIN_LEN - ETHER_CRC_LEN - savelen;
    496 		savelen = ETHER_MIN_LEN - ETHER_CRC_LEN;
    497 	} else
    498 		padlen = 0;
    499 
    500 
    501 	/* Select page 0 registers. */
    502 	NIC_BARRIER(nict, nich);
    503 	bus_space_write_1(nict, nich, ED_P0_CR,
    504 	    ED_CR_RD2 | ED_CR_PAGE_0 | ED_CR_STA);
    505 	NIC_BARRIER(nict, nich);
    506 
    507 	/* Reset remote DMA complete flag. */
    508 	bus_space_write_1(nict, nich, ED_P0_ISR, ED_ISR_RDC);
    509 	NIC_BARRIER(nict, nich);
    510 
    511 	/* Set up DMA byte count. */
    512 	bus_space_write_1(nict, nich, ED_P0_RBCR0, savelen);
    513 	bus_space_write_1(nict, nich, ED_P0_RBCR1, savelen >> 8);
    514 
    515 	/* Set up destination address in NIC mem. */
    516 	bus_space_write_1(nict, nich, ED_P0_RSAR0, buf);
    517 	bus_space_write_1(nict, nich, ED_P0_RSAR1, buf >> 8);
    518 
    519 	/* Set remote DMA write. */
    520 	NIC_BARRIER(nict, nich);
    521 	bus_space_write_1(nict, nich,
    522 	    ED_P0_CR, ED_CR_RD1 | ED_CR_PAGE_0 | ED_CR_STA);
    523 	NIC_BARRIER(nict, nich);
    524 
    525 	/*
    526 	 * Transfer the mbuf chain to the NIC memory.  NE2000 cards
    527 	 * require that data be transferred as words, and only words,
    528 	 * so that case requires some extra code to patch over odd-length
    529 	 * mbufs.
    530 	 */
    531 	if (nsc->sc_type == NE2000_TYPE_NE1000) {
    532 		/* NE1000s are easy. */
    533 		for (; m != 0; m = m->m_next) {
    534 			if (m->m_len) {
    535 				bus_space_write_multi_1(asict, asich,
    536 				    NE2000_ASIC_DATA, mtod(m, u_int8_t *),
    537 				    m->m_len);
    538 			}
    539 		}
    540 		if (padlen) {
    541 			for(; padlen > 0; padlen--)
    542 				bus_space_write_1(asict, asich,
    543 				    NE2000_ASIC_DATA, 0);
    544 		}
    545 	} else {
    546 		/* NE2000s are a bit trickier. */
    547 		u_int8_t *data, savebyte[2];
    548 		int l, leftover;
    549 #ifdef DIAGNOSTIC
    550 		u_int8_t *lim;
    551 #endif
    552 		/* Start out with no leftover data. */
    553 		leftover = 0;
    554 		savebyte[0] = savebyte[1] = 0;
    555 
    556 		for (; m != 0; m = m->m_next) {
    557 			l = m->m_len;
    558 			if (l == 0)
    559 				continue;
    560 			data = mtod(m, u_int8_t *);
    561 #ifdef DIAGNOSTIC
    562 			lim = data + l;
    563 #endif
    564 			while (l > 0) {
    565 				if (leftover) {
    566 					/*
    567 					 * Data left over (from mbuf or
    568 					 * realignment).  Buffer the next
    569 					 * byte, and write it and the
    570 					 * leftover data out.
    571 					 */
    572 					savebyte[1] = *data++;
    573 					l--;
    574 					bus_space_write_stream_2(asict, asich,
    575 					    NE2000_ASIC_DATA,
    576 					    *(u_int16_t *)savebyte);
    577 					leftover = 0;
    578 				} else if (BUS_SPACE_ALIGNED_POINTER(data,
    579 					   u_int16_t) == 0) {
    580 					/*
    581 					 * Unaligned data; buffer the next
    582 					 * byte.
    583 					 */
    584 					savebyte[0] = *data++;
    585 					l--;
    586 					leftover = 1;
    587 				} else {
    588 					/*
    589 					 * Aligned data; output contiguous
    590 					 * words as much as we can, then
    591 					 * buffer the remaining byte, if any.
    592 					 */
    593 					leftover = l & 1;
    594 					l &= ~1;
    595 					bus_space_write_multi_stream_2(asict,
    596 					    asich, NE2000_ASIC_DATA,
    597 					    (u_int16_t *)data, l >> 1);
    598 					data += l;
    599 					if (leftover)
    600 						savebyte[0] = *data++;
    601 					l = 0;
    602 				}
    603 			}
    604 			if (l < 0)
    605 				panic("ne2000_write_mbuf: negative len");
    606 #ifdef DIAGNOSTIC
    607 			if (data != lim)
    608 				panic("ne2000_write_mbuf: data != lim");
    609 #endif
    610 		}
    611 		if (leftover) {
    612 			savebyte[1] = 0;
    613 			bus_space_write_stream_2(asict, asich, NE2000_ASIC_DATA,
    614 			    *(u_int16_t *)savebyte);
    615 		}
    616 		if (padlen) {
    617 			for(; padlen > 1; padlen -= 2)
    618 				bus_space_write_stream_2(asict, asich,
    619 				    NE2000_ASIC_DATA, 0);
    620 		}
    621 	}
    622 	NIC_BARRIER(nict, nich);
    623 
    624 	/* AX88796 doesn't seem to have remote DMA complete */
    625 	if (sc->sc_flags & DP8390_NO_REMOTE_DMA_COMPLETE)
    626 		return(savelen);
    627 
    628 	/*
    629 	 * Wait for remote DMA to complete.  This is necessary because on the
    630 	 * transmit side, data is handled internally by the NIC in bursts, and
    631 	 * we can't start another remote DMA until this one completes.  Not
    632 	 * waiting causes really bad things to happen - like the NIC wedging
    633 	 * the bus.
    634 	 */
    635 	while (((bus_space_read_1(nict, nich, ED_P0_ISR) & ED_ISR_RDC) !=
    636 	    ED_ISR_RDC) && --maxwait) {
    637 		(void)bus_space_read_1(nict, nich, ED_P0_CRDA1);
    638 		(void)bus_space_read_1(nict, nich, ED_P0_CRDA0);
    639 		NIC_BARRIER(nict, nich);
    640 		DELAY(1);
    641 	}
    642 
    643 	if (maxwait == 0) {
    644 		log(LOG_WARNING,
    645 		    "%s: remote transmit DMA failed to complete\n",
    646 		    device_xname(sc->sc_dev));
    647 		dp8390_reset(sc);
    648 	}
    649 
    650 	return (savelen);
    651 }
    652 
    653 /*
    654  * Given a source and destination address, copy 'amout' of a packet from
    655  * the ring buffer into a linear destination buffer.  Takes into account
    656  * ring-wrap.
    657  */
    658 int
    659 ne2000_ring_copy(struct dp8390_softc *sc, int src, void *dstv, u_short amount)
    660 {
    661 	char *dst = dstv;
    662 	struct ne2000_softc *nsc = (struct ne2000_softc *)sc;
    663 	bus_space_tag_t nict = sc->sc_regt;
    664 	bus_space_handle_t nich = sc->sc_regh;
    665 	bus_space_tag_t asict = nsc->sc_asict;
    666 	bus_space_handle_t asich = nsc->sc_asich;
    667 	u_short tmp_amount;
    668 	int useword = nsc->sc_useword;
    669 
    670 	/* Does copy wrap to lower addr in ring buffer? */
    671 	if (src + amount > sc->mem_end) {
    672 		tmp_amount = sc->mem_end - src;
    673 
    674 		/* Copy amount up to end of NIC memory. */
    675 		ne2000_readmem(nict, nich, asict, asich, src,
    676 		    (u_int8_t *)dst, tmp_amount, useword);
    677 
    678 		amount -= tmp_amount;
    679 		src = sc->mem_ring;
    680 		dst += tmp_amount;
    681 	}
    682 
    683 	ne2000_readmem(nict, nich, asict, asich, src, (u_int8_t *)dst,
    684 	    amount, useword);
    685 
    686 	return (src + amount);
    687 }
    688 
    689 void
    690 ne2000_read_hdr(struct dp8390_softc *sc, int buf, struct dp8390_ring *hdr)
    691 {
    692 	struct ne2000_softc *nsc = (struct ne2000_softc *)sc;
    693 
    694 	ne2000_readmem(sc->sc_regt, sc->sc_regh, nsc->sc_asict, nsc->sc_asich,
    695 	    buf, (u_int8_t *)hdr, sizeof(struct dp8390_ring),
    696 	    nsc->sc_useword);
    697 #if BYTE_ORDER == BIG_ENDIAN
    698 	hdr->count = bswap16(hdr->count);
    699 #endif
    700 }
    701 
    702 int
    703 ne2000_test_mem(struct dp8390_softc *sc)
    704 {
    705 
    706 	/* Noop. */
    707 	return (0);
    708 }
    709 
    710 /*
    711  * Given a NIC memory source address and a host memory destination address,
    712  * copy 'amount' from NIC to host using programmed i/o.  The 'amount' is
    713  * rounded up to a word - ok as long as mbufs are word sized.
    714  */
    715 void
    716 ne2000_readmem(bus_space_tag_t nict, bus_space_handle_t nich, bus_space_tag_t asict, bus_space_handle_t asich, int src, u_int8_t *dst, size_t amount, int useword)
    717 {
    718 
    719 	/* Select page 0 registers. */
    720 	NIC_BARRIER(nict, nich);
    721 	bus_space_write_1(nict, nich, ED_P0_CR,
    722 	    ED_CR_RD2 | ED_CR_PAGE_0 | ED_CR_STA);
    723 	NIC_BARRIER(nict, nich);
    724 
    725 	/* Round up to a word. */
    726 	if (amount & 1)
    727 		++amount;
    728 
    729 	/* Set up DMA byte count. */
    730 	bus_space_write_1(nict, nich, ED_P0_RBCR0, amount);
    731 	bus_space_write_1(nict, nich, ED_P0_RBCR1, amount >> 8);
    732 
    733 	/* Set up source address in NIC mem. */
    734 	bus_space_write_1(nict, nich, ED_P0_RSAR0, src);
    735 	bus_space_write_1(nict, nich, ED_P0_RSAR1, src >> 8);
    736 
    737 	NIC_BARRIER(nict, nich);
    738 	bus_space_write_1(nict, nich, ED_P0_CR,
    739 	    ED_CR_RD0 | ED_CR_PAGE_0 | ED_CR_STA);
    740 
    741 	ASIC_BARRIER(asict, asich);
    742 	if (useword)
    743 		bus_space_read_multi_stream_2(asict, asich, NE2000_ASIC_DATA,
    744 		    (u_int16_t *)dst, amount >> 1);
    745 	else
    746 		bus_space_read_multi_1(asict, asich, NE2000_ASIC_DATA,
    747 		    dst, amount);
    748 }
    749 
    750 /*
    751  * Stripped down routine for writing a linear buffer to NIC memory.  Only
    752  * used in the probe routine to test the memory.  'len' must be even.
    753  */
    754 void
    755 ne2000_writemem(bus_space_tag_t nict, bus_space_handle_t nich, bus_space_tag_t asict, bus_space_handle_t asich, u_int8_t *src, int dst, size_t len, int useword, int quiet)
    756 {
    757 	int maxwait = 100;	/* about 120us */
    758 
    759 	/* Select page 0 registers. */
    760 	NIC_BARRIER(nict, nich);
    761 	bus_space_write_1(nict, nich, ED_P0_CR,
    762 	    ED_CR_RD2 | ED_CR_PAGE_0 | ED_CR_STA);
    763 	NIC_BARRIER(nict, nich);
    764 
    765 	/* Reset remote DMA complete flag. */
    766 	bus_space_write_1(nict, nich, ED_P0_ISR, ED_ISR_RDC);
    767 	NIC_BARRIER(nict, nich);
    768 
    769 	/* Set up DMA byte count. */
    770 	bus_space_write_1(nict, nich, ED_P0_RBCR0, len);
    771 	bus_space_write_1(nict, nich, ED_P0_RBCR1, len >> 8);
    772 
    773 	/* Set up destination address in NIC mem. */
    774 	bus_space_write_1(nict, nich, ED_P0_RSAR0, dst);
    775 	bus_space_write_1(nict, nich, ED_P0_RSAR1, dst >> 8);
    776 
    777 	/* Set remote DMA write. */
    778 	NIC_BARRIER(nict, nich);
    779 	bus_space_write_1(nict, nich, ED_P0_CR,
    780 	    ED_CR_RD1 | ED_CR_PAGE_0 | ED_CR_STA);
    781 	NIC_BARRIER(nict, nich);
    782 
    783 	ASIC_BARRIER(asict, asich);
    784 	if (useword)
    785 		bus_space_write_multi_stream_2(asict, asich, NE2000_ASIC_DATA,
    786 		    (u_int16_t *)src, len >> 1);
    787 	else
    788 		bus_space_write_multi_1(asict, asich, NE2000_ASIC_DATA,
    789 		    src, len);
    790 	ASIC_BARRIER(asict, asich);
    791 
    792 	/*
    793 	 * Wait for remote DMA to complete.  This is necessary because on the
    794 	 * transmit side, data is handled internally by the NIC in bursts, and
    795 	 * we can't start another remote DMA until this one completes.  Not
    796 	 * waiting causes really bad things to happen - like the NIC wedging
    797 	 * the bus.
    798 	 */
    799 	while (((bus_space_read_1(nict, nich, ED_P0_ISR) & ED_ISR_RDC) !=
    800 	    ED_ISR_RDC) && --maxwait)
    801 		DELAY(1);
    802 
    803 	if (!quiet && maxwait == 0)
    804 		printf("ne2000_writemem: failed to complete\n");
    805 }
    806 
    807 int
    808 ne2000_detach(struct ne2000_softc *sc, 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(struct ipkdb_if *kip)
    820 {
    821 	struct ne2000_softc *np = kip->port;
    822 	struct dp8390_softc *dp = &np->sc_dp8390;
    823 	bus_space_tag_t nict = dp->sc_regt;
    824 	bus_space_handle_t nich = dp->sc_regh;
    825 	int i, useword, dmawidth;
    826 
    827 #ifdef GWETHER
    828 	/* Not supported (yet?) */
    829 	return -1;
    830 #endif
    831 
    832 	if (np->sc_type == NE2000_TYPE_UNKNOWN)
    833 		np->sc_type = ne2000_detect(nict, nich,
    834 			np->sc_asict, np->sc_asich);
    835 	if (np->sc_type == NE2000_TYPE_UNKNOWN)
    836 		return -1;
    837 
    838 	useword = np->sc_useword;
    839 	dmawidth = np->sc_dmawidth;
    840 
    841 	dp->cr_proto = ED_CR_RD2;
    842 	dp->dcr_reg = ED_DCR_FT1 | ED_DCR_LS |
    843 	    ((dmawidth == NE2000_DMAWIDTH_16BIT) ? ED_DCR_WTS : 0);
    844 	dp->rcr_proto = 0;
    845 
    846 	dp->test_mem = ne2000_test_mem;
    847 	dp->ring_copy = ne2000_ring_copy;
    848 	dp->write_mbuf = ne2000_write_mbuf;
    849 	dp->read_hdr = ne2000_read_hdr;
    850 
    851 	for (i = 0; i < 16; i++)
    852 		dp->sc_reg_map[i] = i;
    853 
    854 	switch (np->sc_type) {
    855 	case NE2000_TYPE_NE1000:
    856 		dp->mem_start = dp->mem_size = 8192;
    857 		kip->name = "ne1000";
    858 		break;
    859 	case NE2000_TYPE_NE2000:
    860 		dp->mem_start = dp->mem_size = 8192 * 2;
    861 		kip->name = "ne2000";
    862 		break;
    863 	case NE2000_TYPE_DL10019:
    864 	case NE2000_TYPE_DL10022:
    865 		dp->mem_start = dp->mem_size = 8192 * 3;
    866 		kip->name = (np->sc_type == NE2000_TYPE_DL10019) ?
    867 		    "dl10022" : "dl10019";
    868 		break;
    869 	case NE2000_TYPE_AX88190:
    870 	case NE2000_TYPE_AX88790:
    871 		dp->rcr_proto = ED_RCR_INTT;
    872 		dp->sc_flags |= DP8390_DO_AX88190_WORKAROUND;
    873 		dp->mem_start = dp->mem_size = 8192 * 2;
    874 		kip->name = "ax88190";
    875 		break;
    876 	default:
    877 		return -1;
    878 		break;
    879 	}
    880 
    881 	if (dp8390_ipkdb_attach(kip))
    882 		return -1;
    883 
    884 	dp->mem_ring = dp->mem_start
    885 		+ ((dp->txb_cnt * ED_TXBUF_SIZE) << ED_PAGE_SHIFT);
    886 
    887 	if (!(kip->flags & IPKDB_MYHW)) {
    888 		char romdata[16];
    889 
    890 		/* Read the station address. */
    891 		if (np->sc_type == NE2000_TYPE_AX88190 ||
    892 		    np->sc_type == NE2000_TYPE_AX88790) {
    893 			/* Select page 0 registers. */
    894 			NIC_BARRIER(nict, nich);
    895 			bus_space_write_1(nict, nich, ED_P0_CR,
    896 				ED_CR_RD2 | ED_CR_PAGE_0 | ED_CR_STA);
    897 			NIC_BARRIER(nict, nich);
    898 			/* Select word transfer */
    899 			bus_space_write_1(nict, nich, ED_P0_DCR,
    900 			    ((dmawidth == NE2000_DMAWIDTH_16BIT) ? ED_DCR_WTS : 0));
    901 			ne2000_readmem(nict, nich, np->sc_asict, np->sc_asich,
    902 				AX88190_NODEID_OFFSET, kip->myenetaddr,
    903 				ETHER_ADDR_LEN, useword);
    904 		} else {
    905 			ne2000_readmem(nict, nich, np->sc_asict, np->sc_asich,
    906 				0, romdata, sizeof romdata, useword);
    907 			for (i = 0; i < ETHER_ADDR_LEN; i++)
    908 				kip->myenetaddr[i] = romdata[i << useword];
    909 		}
    910 		kip->flags |= IPKDB_MYHW;
    911 
    912 	}
    913 	dp8390_stop(dp);
    914 
    915 	return 0;
    916 }
    917 #endif
    918 
    919 bool
    920 ne2000_suspend(device_t self, pmf_qual_t qual)
    921 {
    922 	struct ne2000_softc *sc = device_private(self);
    923 	struct dp8390_softc *dsc = &sc->sc_dp8390;
    924 	int s;
    925 
    926 	s = splnet();
    927 
    928 	dp8390_stop(dsc);
    929 	dp8390_disable(dsc);
    930 
    931 	splx(s);
    932 	return true;
    933 }
    934 
    935 bool
    936 ne2000_resume(device_t self, pmf_qual_t qual)
    937 {
    938 	struct ne2000_softc *sc = device_private(self);
    939 	struct dp8390_softc *dsc = &sc->sc_dp8390;
    940 	struct ifnet *ifp = &dsc->sc_ec.ec_if;
    941 	int s;
    942 
    943 	s = splnet();
    944 
    945 	if (ifp->if_flags & IFF_UP) {
    946 		if (dp8390_enable(dsc) == 0)
    947 			dp8390_init(dsc);
    948 	}
    949 
    950 	splx(s);
    951 	return true;
    952 }
    953