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