Home | History | Annotate | Line # | Download | only in ic
ne2000.c revision 1.67
      1 /*	$NetBSD: ne2000.c,v 1.67 2010/02/24 22:37:58 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.67 2010/02/24 22:37:58 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 	return (0);
    302 }
    303 
    304 /*
    305  * Detect an NE-2000 or compatible.  Returns a model code.
    306  */
    307 int
    308 ne2000_detect(bus_space_tag_t nict, bus_space_handle_t nich, bus_space_tag_t asict, bus_space_handle_t asich)
    309 {
    310 	static u_int8_t test_pattern[32] = "THIS is A memory TEST pattern";
    311 	u_int8_t test_buffer[32], tmp;
    312 	int i, rv = NE2000_TYPE_UNKNOWN;
    313 	int dmawidth = NE2000_DMAWIDTH_16BIT;
    314 
    315  restart:
    316 	/* Reset the board. */
    317 #ifdef GWETHER
    318 	bus_space_write_1(asict, asich, NE2000_ASIC_RESET, 0);
    319 	ASIC_BARRIER(asict, asich);
    320 	delay(200);
    321 #endif /* GWETHER */
    322 	tmp = bus_space_read_1(asict, asich, NE2000_ASIC_RESET);
    323 	ASIC_BARRIER(asict, asich);
    324 	delay(10000);
    325 
    326 	/*
    327 	 * I don't know if this is necessary; probably cruft leftover from
    328 	 * Clarkson packet driver code. Doesn't do a thing on the boards I've
    329 	 * tested. -DG [note that a outb(0x84, 0) seems to work here, and is
    330 	 * non-invasive...but some boards don't seem to reset and I don't have
    331 	 * complete documentation on what the 'right' thing to do is...so we do
    332 	 * the invasive thing for now.  Yuck.]
    333 	 */
    334 	bus_space_write_1(asict, asich, NE2000_ASIC_RESET, tmp);
    335 	ASIC_BARRIER(asict, asich);
    336 	delay(5000);
    337 
    338 	/*
    339 	 * This is needed because some NE clones apparently don't reset the
    340 	 * NIC properly (or the NIC chip doesn't reset fully on power-up).
    341 	 * XXX - this makes the probe invasive!  Done against my better
    342 	 * judgement.  -DLG
    343 	 */
    344 	bus_space_write_1(nict, nich, ED_P0_CR,
    345 	    ED_CR_RD2 | ED_CR_PAGE_0 | ED_CR_STP);
    346 	NIC_BARRIER(nict, nich);
    347 
    348 	delay(5000);
    349 
    350 	/*
    351 	 * Generic probe routine for testing for the existence of a DS8390.
    352 	 * Must be performed  after the NIC has just been reset.  This
    353 	 * works by looking at certain register values that are guaranteed
    354 	 * to be initialized a certain way after power-up or reset.
    355 	 *
    356 	 * Specifically:
    357 	 *
    358 	 *	Register		reset bits	set bits
    359 	 *	--------		----------	--------
    360 	 *	CR			TXP, STA	RD2, STP
    361 	 *	ISR					RST
    362 	 *	IMR			<all>
    363 	 *	DCR					LAS
    364 	 *	TCR			LB1, LB0
    365 	 *
    366 	 * We only look at CR and ISR, however, since looking at the others
    367 	 * would require changing register pages, which would be intrusive
    368 	 * if this isn't an 8390.
    369 	 */
    370 
    371 	tmp = bus_space_read_1(nict, nich, ED_P0_CR);
    372 	if ((tmp & (ED_CR_RD2 | ED_CR_TXP | ED_CR_STA | ED_CR_STP)) !=
    373 	    (ED_CR_RD2 | ED_CR_STP))
    374 		goto out;
    375 
    376 	tmp = bus_space_read_1(nict, nich, ED_P0_ISR);
    377 	if ((tmp & ED_ISR_RST) != ED_ISR_RST)
    378 		goto out;
    379 
    380 	bus_space_write_1(nict, nich,
    381 	    ED_P0_CR, ED_CR_RD2 | ED_CR_PAGE_0 | ED_CR_STA);
    382 	NIC_BARRIER(nict, nich);
    383 
    384 	for (i = 0; i < 100; i++) {
    385 		if ((bus_space_read_1(nict, nich, ED_P0_ISR) & ED_ISR_RST) ==
    386 		    ED_ISR_RST) {
    387 			/* Ack the reset bit. */
    388 			bus_space_write_1(nict, nich, ED_P0_ISR, ED_ISR_RST);
    389 			NIC_BARRIER(nict, nich);
    390 			break;
    391 		}
    392 		delay(100);
    393 	}
    394 
    395 #if 0
    396 	/* XXX */
    397 	if (i == 100)
    398 		goto out;
    399 #endif
    400 
    401 	/*
    402 	 * Test the ability to read and write to the NIC memory.  This has
    403 	 * the side effect of determining if this is an NE1000 or an NE2000.
    404 	 */
    405 
    406 	/*
    407 	 * This prevents packets from being stored in the NIC memory when
    408 	 * the readmem routine turns on the start bit in the CR.
    409 	 */
    410 	bus_space_write_1(nict, nich, ED_P0_RCR, ED_RCR_MON);
    411 	NIC_BARRIER(nict, nich);
    412 
    413 	/* Temporarily initialize DCR for byte operations. */
    414 	bus_space_write_1(nict, nich, ED_P0_DCR, ED_DCR_FT1 | ED_DCR_LS);
    415 
    416 	bus_space_write_1(nict, nich, ED_P0_PSTART, 8192 >> ED_PAGE_SHIFT);
    417 	bus_space_write_1(nict, nich, ED_P0_PSTOP, 16384 >> ED_PAGE_SHIFT);
    418 
    419 	/*
    420 	 * Write a test pattern in byte mode.  If this fails, then there
    421 	 * probably isn't any memory at 8k - which likely means that the
    422 	 * board is an NE2000.
    423 	 */
    424 	ne2000_writemem(nict, nich, asict, asich, test_pattern, 8192,
    425 	    sizeof(test_pattern), 0, 1);
    426 	ne2000_readmem(nict, nich, asict, asich, 8192, test_buffer,
    427 	    sizeof(test_buffer), 0);
    428 
    429 	if (memcmp(test_pattern, test_buffer, sizeof(test_pattern))) {
    430 		/* not an NE1000 - try NE2000 */
    431 		bus_space_write_1(nict, nich, ED_P0_DCR, ED_DCR_FT1 | ED_DCR_LS
    432 		    | ((dmawidth == NE2000_DMAWIDTH_16BIT) ? ED_DCR_WTS : 0));
    433 		bus_space_write_1(nict, nich, ED_P0_PSTART,
    434 		    16384 >> ED_PAGE_SHIFT);
    435 		bus_space_write_1(nict, nich, ED_P0_PSTOP,
    436 		    32768 >> ED_PAGE_SHIFT);
    437 
    438 		/*
    439 		 * Write the test pattern in word mode.  If this also fails,
    440 		 * then we don't know what this board is.
    441 		 */
    442 		ne2000_writemem(nict, nich, asict, asich, test_pattern, 16384,
    443 		    sizeof(test_pattern), 1, 0);
    444 		ne2000_readmem(nict, nich, asict, asich, 16384, test_buffer,
    445 		    sizeof(test_buffer), 1);
    446 
    447 		if (memcmp(test_pattern, test_buffer, sizeof(test_pattern))) {
    448 			if (dmawidth == NE2000_DMAWIDTH_16BIT) {
    449 				/* try 8bit dma */
    450 				dmawidth = NE2000_DMAWIDTH_8BIT;
    451 				goto restart;
    452 			}
    453 			goto out;	/* not an NE2000 either */
    454 		}
    455 
    456 		rv = NE2000_TYPE_NE2000;
    457 	} else {
    458 		/* We're an NE1000. */
    459 		rv = NE2000_TYPE_NE1000;
    460 		dmawidth = NE2000_DMAWIDTH_8BIT;
    461 	}
    462 
    463 	/* Clear any pending interrupts that might have occurred above. */
    464 	NIC_BARRIER(nict, nich);
    465 	bus_space_write_1(nict, nich, ED_P0_ISR, 0xff);
    466 
    467  out:
    468 	return (rv);
    469 }
    470 
    471 /*
    472  * Write an mbuf chain to the destination NIC memory address using programmed
    473  * I/O.
    474  */
    475 int
    476 ne2000_write_mbuf(struct dp8390_softc *sc, struct mbuf *m, int buf)
    477 {
    478 	struct ne2000_softc *nsc = (struct ne2000_softc *)sc;
    479 	bus_space_tag_t nict = sc->sc_regt;
    480 	bus_space_handle_t nich = sc->sc_regh;
    481 	bus_space_tag_t asict = nsc->sc_asict;
    482 	bus_space_handle_t asich = nsc->sc_asich;
    483 	int savelen, padlen;
    484 	int maxwait = 100;	/* about 120us */
    485 
    486 	savelen = m->m_pkthdr.len;
    487 	if (savelen < ETHER_MIN_LEN - ETHER_CRC_LEN) {
    488 		padlen = ETHER_MIN_LEN - ETHER_CRC_LEN - savelen;
    489 		savelen = ETHER_MIN_LEN - ETHER_CRC_LEN;
    490 	} else
    491 		padlen = 0;
    492 
    493 
    494 	/* Select page 0 registers. */
    495 	NIC_BARRIER(nict, nich);
    496 	bus_space_write_1(nict, nich, ED_P0_CR,
    497 	    ED_CR_RD2 | ED_CR_PAGE_0 | ED_CR_STA);
    498 	NIC_BARRIER(nict, nich);
    499 
    500 	/* Reset remote DMA complete flag. */
    501 	bus_space_write_1(nict, nich, ED_P0_ISR, ED_ISR_RDC);
    502 	NIC_BARRIER(nict, nich);
    503 
    504 	/* Set up DMA byte count. */
    505 	bus_space_write_1(nict, nich, ED_P0_RBCR0, savelen);
    506 	bus_space_write_1(nict, nich, ED_P0_RBCR1, savelen >> 8);
    507 
    508 	/* Set up destination address in NIC mem. */
    509 	bus_space_write_1(nict, nich, ED_P0_RSAR0, buf);
    510 	bus_space_write_1(nict, nich, ED_P0_RSAR1, buf >> 8);
    511 
    512 	/* Set remote DMA write. */
    513 	NIC_BARRIER(nict, nich);
    514 	bus_space_write_1(nict, nich,
    515 	    ED_P0_CR, ED_CR_RD1 | ED_CR_PAGE_0 | ED_CR_STA);
    516 	NIC_BARRIER(nict, nich);
    517 
    518 	/*
    519 	 * Transfer the mbuf chain to the NIC memory.  NE2000 cards
    520 	 * require that data be transferred as words, and only words,
    521 	 * so that case requires some extra code to patch over odd-length
    522 	 * mbufs.
    523 	 */
    524 	if (nsc->sc_type == NE2000_TYPE_NE1000) {
    525 		/* NE1000s are easy. */
    526 		for (; m != 0; m = m->m_next) {
    527 			if (m->m_len) {
    528 				bus_space_write_multi_1(asict, asich,
    529 				    NE2000_ASIC_DATA, mtod(m, u_int8_t *),
    530 				    m->m_len);
    531 			}
    532 		}
    533 		if (padlen) {
    534 			for(; padlen > 0; padlen--)
    535 				bus_space_write_1(asict, asich,
    536 				    NE2000_ASIC_DATA, 0);
    537 		}
    538 	} else {
    539 		/* NE2000s are a bit trickier. */
    540 		u_int8_t *data, savebyte[2];
    541 		int l, leftover;
    542 #ifdef DIAGNOSTIC
    543 		u_int8_t *lim;
    544 #endif
    545 		/* Start out with no leftover data. */
    546 		leftover = 0;
    547 		savebyte[0] = savebyte[1] = 0;
    548 
    549 		for (; m != 0; m = m->m_next) {
    550 			l = m->m_len;
    551 			if (l == 0)
    552 				continue;
    553 			data = mtod(m, u_int8_t *);
    554 #ifdef DIAGNOSTIC
    555 			lim = data + l;
    556 #endif
    557 			while (l > 0) {
    558 				if (leftover) {
    559 					/*
    560 					 * Data left over (from mbuf or
    561 					 * realignment).  Buffer the next
    562 					 * byte, and write it and the
    563 					 * leftover data out.
    564 					 */
    565 					savebyte[1] = *data++;
    566 					l--;
    567 					bus_space_write_stream_2(asict, asich,
    568 					    NE2000_ASIC_DATA,
    569 					    *(u_int16_t *)savebyte);
    570 					leftover = 0;
    571 				} else if (BUS_SPACE_ALIGNED_POINTER(data,
    572 					   u_int16_t) == 0) {
    573 					/*
    574 					 * Unaligned data; buffer the next
    575 					 * byte.
    576 					 */
    577 					savebyte[0] = *data++;
    578 					l--;
    579 					leftover = 1;
    580 				} else {
    581 					/*
    582 					 * Aligned data; output contiguous
    583 					 * words as much as we can, then
    584 					 * buffer the remaining byte, if any.
    585 					 */
    586 					leftover = l & 1;
    587 					l &= ~1;
    588 					bus_space_write_multi_stream_2(asict,
    589 					    asich, NE2000_ASIC_DATA,
    590 					    (u_int16_t *)data, l >> 1);
    591 					data += l;
    592 					if (leftover)
    593 						savebyte[0] = *data++;
    594 					l = 0;
    595 				}
    596 			}
    597 			if (l < 0)
    598 				panic("ne2000_write_mbuf: negative len");
    599 #ifdef DIAGNOSTIC
    600 			if (data != lim)
    601 				panic("ne2000_write_mbuf: data != lim");
    602 #endif
    603 		}
    604 		if (leftover) {
    605 			savebyte[1] = 0;
    606 			bus_space_write_stream_2(asict, asich, NE2000_ASIC_DATA,
    607 			    *(u_int16_t *)savebyte);
    608 		}
    609 		if (padlen) {
    610 			for(; padlen > 1; padlen -= 2)
    611 				bus_space_write_stream_2(asict, asich,
    612 				    NE2000_ASIC_DATA, 0);
    613 		}
    614 	}
    615 	NIC_BARRIER(nict, nich);
    616 
    617 	/* AX88796 doesn't seem to have remote DMA complete */
    618 	if (sc->sc_flags & DP8390_NO_REMOTE_DMA_COMPLETE)
    619 		return(savelen);
    620 
    621 	/*
    622 	 * Wait for remote DMA to complete.  This is necessary because on the
    623 	 * transmit side, data is handled internally by the NIC in bursts, and
    624 	 * we can't start another remote DMA until this one completes.  Not
    625 	 * waiting causes really bad things to happen - like the NIC wedging
    626 	 * the bus.
    627 	 */
    628 	while (((bus_space_read_1(nict, nich, ED_P0_ISR) & ED_ISR_RDC) !=
    629 	    ED_ISR_RDC) && --maxwait) {
    630 		(void)bus_space_read_1(nict, nich, ED_P0_CRDA1);
    631 		(void)bus_space_read_1(nict, nich, ED_P0_CRDA0);
    632 		NIC_BARRIER(nict, nich);
    633 		DELAY(1);
    634 	}
    635 
    636 	if (maxwait == 0) {
    637 		log(LOG_WARNING,
    638 		    "%s: remote transmit DMA failed to complete\n",
    639 		    device_xname(sc->sc_dev));
    640 		dp8390_reset(sc);
    641 	}
    642 
    643 	return (savelen);
    644 }
    645 
    646 /*
    647  * Given a source and destination address, copy 'amout' of a packet from
    648  * the ring buffer into a linear destination buffer.  Takes into account
    649  * ring-wrap.
    650  */
    651 int
    652 ne2000_ring_copy(struct dp8390_softc *sc, int src, void *dstv, u_short amount)
    653 {
    654 	char *dst = dstv;
    655 	struct ne2000_softc *nsc = (struct ne2000_softc *)sc;
    656 	bus_space_tag_t nict = sc->sc_regt;
    657 	bus_space_handle_t nich = sc->sc_regh;
    658 	bus_space_tag_t asict = nsc->sc_asict;
    659 	bus_space_handle_t asich = nsc->sc_asich;
    660 	u_short tmp_amount;
    661 	int useword = nsc->sc_useword;
    662 
    663 	/* Does copy wrap to lower addr in ring buffer? */
    664 	if (src + amount > sc->mem_end) {
    665 		tmp_amount = sc->mem_end - src;
    666 
    667 		/* Copy amount up to end of NIC memory. */
    668 		ne2000_readmem(nict, nich, asict, asich, src,
    669 		    (u_int8_t *)dst, tmp_amount, useword);
    670 
    671 		amount -= tmp_amount;
    672 		src = sc->mem_ring;
    673 		dst += tmp_amount;
    674 	}
    675 
    676 	ne2000_readmem(nict, nich, asict, asich, src, (u_int8_t *)dst,
    677 	    amount, useword);
    678 
    679 	return (src + amount);
    680 }
    681 
    682 void
    683 ne2000_read_hdr(struct dp8390_softc *sc, int buf, struct dp8390_ring *hdr)
    684 {
    685 	struct ne2000_softc *nsc = (struct ne2000_softc *)sc;
    686 
    687 	ne2000_readmem(sc->sc_regt, sc->sc_regh, nsc->sc_asict, nsc->sc_asich,
    688 	    buf, (u_int8_t *)hdr, sizeof(struct dp8390_ring),
    689 	    nsc->sc_useword);
    690 #if BYTE_ORDER == BIG_ENDIAN
    691 	hdr->count = bswap16(hdr->count);
    692 #endif
    693 }
    694 
    695 int
    696 ne2000_test_mem(struct dp8390_softc *sc)
    697 {
    698 
    699 	/* Noop. */
    700 	return (0);
    701 }
    702 
    703 /*
    704  * Given a NIC memory source address and a host memory destination address,
    705  * copy 'amount' from NIC to host using programmed i/o.  The 'amount' is
    706  * rounded up to a word - ok as long as mbufs are word sized.
    707  */
    708 void
    709 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)
    710 {
    711 
    712 	/* Select page 0 registers. */
    713 	NIC_BARRIER(nict, nich);
    714 	bus_space_write_1(nict, nich, ED_P0_CR,
    715 	    ED_CR_RD2 | ED_CR_PAGE_0 | ED_CR_STA);
    716 	NIC_BARRIER(nict, nich);
    717 
    718 	/* Round up to a word. */
    719 	amount = roundup2(amount, sizeof(uint16_t));
    720 
    721 	/* Set up DMA byte count. */
    722 	bus_space_write_1(nict, nich, ED_P0_RBCR0, amount);
    723 	bus_space_write_1(nict, nich, ED_P0_RBCR1, amount >> 8);
    724 
    725 	/* Set up source address in NIC mem. */
    726 	bus_space_write_1(nict, nich, ED_P0_RSAR0, src);
    727 	bus_space_write_1(nict, nich, ED_P0_RSAR1, src >> 8);
    728 
    729 	NIC_BARRIER(nict, nich);
    730 	bus_space_write_1(nict, nich, ED_P0_CR,
    731 	    ED_CR_RD0 | ED_CR_PAGE_0 | ED_CR_STA);
    732 
    733 	ASIC_BARRIER(asict, asich);
    734 	if (useword)
    735 		bus_space_read_multi_stream_2(asict, asich, NE2000_ASIC_DATA,
    736 		    (u_int16_t *)dst, amount >> 1);
    737 	else
    738 		bus_space_read_multi_1(asict, asich, NE2000_ASIC_DATA,
    739 		    dst, amount);
    740 }
    741 
    742 /*
    743  * Stripped down routine for writing a linear buffer to NIC memory.  Only
    744  * used in the probe routine to test the memory.  'len' must be even.
    745  */
    746 void
    747 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)
    748 {
    749 	int maxwait = 100;	/* about 120us */
    750 
    751 	/* Select page 0 registers. */
    752 	NIC_BARRIER(nict, nich);
    753 	bus_space_write_1(nict, nich, ED_P0_CR,
    754 	    ED_CR_RD2 | ED_CR_PAGE_0 | ED_CR_STA);
    755 	NIC_BARRIER(nict, nich);
    756 
    757 	/* Reset remote DMA complete flag. */
    758 	bus_space_write_1(nict, nich, ED_P0_ISR, ED_ISR_RDC);
    759 	NIC_BARRIER(nict, nich);
    760 
    761 	/* Set up DMA byte count. */
    762 	bus_space_write_1(nict, nich, ED_P0_RBCR0, len);
    763 	bus_space_write_1(nict, nich, ED_P0_RBCR1, len >> 8);
    764 
    765 	/* Set up destination address in NIC mem. */
    766 	bus_space_write_1(nict, nich, ED_P0_RSAR0, dst);
    767 	bus_space_write_1(nict, nich, ED_P0_RSAR1, dst >> 8);
    768 
    769 	/* Set remote DMA write. */
    770 	NIC_BARRIER(nict, nich);
    771 	bus_space_write_1(nict, nich, ED_P0_CR,
    772 	    ED_CR_RD1 | ED_CR_PAGE_0 | ED_CR_STA);
    773 	NIC_BARRIER(nict, nich);
    774 
    775 	ASIC_BARRIER(asict, asich);
    776 	if (useword)
    777 		bus_space_write_multi_stream_2(asict, asich, NE2000_ASIC_DATA,
    778 		    (u_int16_t *)src, len >> 1);
    779 	else
    780 		bus_space_write_multi_1(asict, asich, NE2000_ASIC_DATA,
    781 		    src, len);
    782 	ASIC_BARRIER(asict, asich);
    783 
    784 	/*
    785 	 * Wait for remote DMA to complete.  This is necessary because on the
    786 	 * transmit side, data is handled internally by the NIC in bursts, and
    787 	 * we can't start another remote DMA until this one completes.  Not
    788 	 * waiting causes really bad things to happen - like the NIC wedging
    789 	 * the bus.
    790 	 */
    791 	while (((bus_space_read_1(nict, nich, ED_P0_ISR) & ED_ISR_RDC) !=
    792 	    ED_ISR_RDC) && --maxwait)
    793 		DELAY(1);
    794 
    795 	if (!quiet && maxwait == 0)
    796 		printf("ne2000_writemem: failed to complete\n");
    797 }
    798 
    799 int
    800 ne2000_detach(struct ne2000_softc *sc, int flags)
    801 {
    802 
    803 	return (dp8390_detach(&sc->sc_dp8390, flags));
    804 }
    805 
    806 #ifdef IPKDB_NE
    807 /*
    808  * This code is essentially the same as ne2000_attach above.
    809  */
    810 int
    811 ne2000_ipkdb_attach(struct ipkdb_if *kip)
    812 {
    813 	struct ne2000_softc *np = kip->port;
    814 	struct dp8390_softc *dp = &np->sc_dp8390;
    815 	bus_space_tag_t nict = dp->sc_regt;
    816 	bus_space_handle_t nich = dp->sc_regh;
    817 	int i, useword, dmawidth;
    818 
    819 #ifdef GWETHER
    820 	/* Not supported (yet?) */
    821 	return -1;
    822 #endif
    823 
    824 	if (np->sc_type == NE2000_TYPE_UNKNOWN)
    825 		np->sc_type = ne2000_detect(nict, nich,
    826 			np->sc_asict, np->sc_asich);
    827 	if (np->sc_type == NE2000_TYPE_UNKNOWN)
    828 		return -1;
    829 
    830 	useword = np->sc_useword;
    831 	dmawidth = np->sc_dmawidth;
    832 
    833 	dp->cr_proto = ED_CR_RD2;
    834 	dp->dcr_reg = ED_DCR_FT1 | ED_DCR_LS |
    835 	    ((dmawidth == NE2000_DMAWIDTH_16BIT) ? ED_DCR_WTS : 0);
    836 	dp->rcr_proto = 0;
    837 
    838 	dp->test_mem = ne2000_test_mem;
    839 	dp->ring_copy = ne2000_ring_copy;
    840 	dp->write_mbuf = ne2000_write_mbuf;
    841 	dp->read_hdr = ne2000_read_hdr;
    842 
    843 	for (i = 0; i < 16; i++)
    844 		dp->sc_reg_map[i] = i;
    845 
    846 	switch (np->sc_type) {
    847 	case NE2000_TYPE_NE1000:
    848 		dp->mem_start = dp->mem_size = 8192;
    849 		kip->name = "ne1000";
    850 		break;
    851 	case NE2000_TYPE_NE2000:
    852 		dp->mem_start = dp->mem_size = 8192 * 2;
    853 		kip->name = "ne2000";
    854 		break;
    855 	case NE2000_TYPE_DL10019:
    856 	case NE2000_TYPE_DL10022:
    857 		dp->mem_start = dp->mem_size = 8192 * 3;
    858 		kip->name = (np->sc_type == NE2000_TYPE_DL10019) ?
    859 		    "dl10022" : "dl10019";
    860 		break;
    861 	case NE2000_TYPE_AX88190:
    862 	case NE2000_TYPE_AX88790:
    863 		dp->rcr_proto = ED_RCR_INTT;
    864 		dp->sc_flags |= DP8390_DO_AX88190_WORKAROUND;
    865 		dp->mem_start = dp->mem_size = 8192 * 2;
    866 		kip->name = "ax88190";
    867 		break;
    868 	default:
    869 		return -1;
    870 		break;
    871 	}
    872 
    873 	if (dp8390_ipkdb_attach(kip))
    874 		return -1;
    875 
    876 	dp->mem_ring = dp->mem_start
    877 		+ ((dp->txb_cnt * ED_TXBUF_SIZE) << ED_PAGE_SHIFT);
    878 
    879 	if (!(kip->flags & IPKDB_MYHW)) {
    880 		char romdata[16];
    881 
    882 		/* Read the station address. */
    883 		if (np->sc_type == NE2000_TYPE_AX88190 ||
    884 		    np->sc_type == NE2000_TYPE_AX88790) {
    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,
    892 			    ((dmawidth == NE2000_DMAWIDTH_16BIT) ? ED_DCR_WTS : 0));
    893 			ne2000_readmem(nict, nich, np->sc_asict, np->sc_asich,
    894 				AX88190_NODEID_OFFSET, kip->myenetaddr,
    895 				ETHER_ADDR_LEN, useword);
    896 		} else {
    897 			ne2000_readmem(nict, nich, np->sc_asict, np->sc_asich,
    898 				0, romdata, sizeof romdata, useword);
    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 
    911 bool
    912 ne2000_suspend(device_t self, const pmf_qual_t *qual)
    913 {
    914 	struct ne2000_softc *sc = device_private(self);
    915 	struct dp8390_softc *dsc = &sc->sc_dp8390;
    916 	int s;
    917 
    918 	s = splnet();
    919 
    920 	dp8390_stop(dsc);
    921 	dp8390_disable(dsc);
    922 
    923 	splx(s);
    924 	return true;
    925 }
    926 
    927 bool
    928 ne2000_resume(device_t self, const pmf_qual_t *qual)
    929 {
    930 	struct ne2000_softc *sc = device_private(self);
    931 	struct dp8390_softc *dsc = &sc->sc_dp8390;
    932 	struct ifnet *ifp = &dsc->sc_ec.ec_if;
    933 	int s;
    934 
    935 	s = splnet();
    936 
    937 	if (ifp->if_flags & IFF_UP) {
    938 		if (dp8390_enable(dsc) == 0)
    939 			dp8390_init(dsc);
    940 	}
    941 
    942 	splx(s);
    943 	return true;
    944 }
    945