The Design and Implementation of the FreeBSD Operating System, Second Edition
Now available: The Design and Implementation of the FreeBSD Operating System (Second Edition)


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]

FreeBSD/Linux Kernel Cross Reference
sys/dev/ic/ne2000.c

Version: -  FREEBSD  -  FREEBSD-13-STABLE  -  FREEBSD-13-0  -  FREEBSD-12-STABLE  -  FREEBSD-12-0  -  FREEBSD-11-STABLE  -  FREEBSD-11-0  -  FREEBSD-10-STABLE  -  FREEBSD-10-0  -  FREEBSD-9-STABLE  -  FREEBSD-9-0  -  FREEBSD-8-STABLE  -  FREEBSD-8-0  -  FREEBSD-7-STABLE  -  FREEBSD-7-0  -  FREEBSD-6-STABLE  -  FREEBSD-6-0  -  FREEBSD-5-STABLE  -  FREEBSD-5-0  -  FREEBSD-4-STABLE  -  FREEBSD-3-STABLE  -  FREEBSD22  -  l41  -  OPENBSD  -  linux-2.6  -  MK84  -  PLAN9  -  xnu-8792 
SearchContext: -  none  -  3  -  10 

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

Cache object: a12c5da849344c6d690e0dd4bc5c5bd0


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]


This page is part of the FreeBSD/Linux Linux Kernel Cross-Reference, and was automatically generated using a modified version of the LXR engine.