root/drivers/net/ne.c

/* [previous][next][first][last][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. ne_probe
  2. ne_probe1
  3. ne_reset_8390
  4. ne_block_input
  5. ne_block_output

   1 /* ne.c: A general non-shared-memory NS8390 ethernet driver for linux. */
   2 /*
   3     Written 1992-94 by Donald Becker.
   4 
   5     Copyright 1993 United States Government as represented by the
   6     Director, National Security Agency.
   7 
   8     This software may be used and distributed according to the terms
   9     of the GNU Public License, incorporated herein by reference.
  10 
  11     The author may be reached as becker@CESDIS.gsfc.nasa.gov, or C/O
  12     Center of Excellence in Space Data and Information Sciences
  13         Code 930.5, Goddard Space Flight Center, Greenbelt MD 20771
  14 
  15     This driver should work with many programmed-I/O 8390-based ethernet
  16     boards.  Currently it supports the NE1000, NE2000, many clones,
  17     and some Cabletron products.
  18 
  19     Changelog:
  20 
  21     Paul Gortmaker      : use ENISR_RDC to monitor Tx PIO uploads, made
  22                           sanity checks and bad clone support optional.
  23 
  24 */
  25 
  26 /* Routines for the NatSemi-based designs (NE[12]000). */
  27 
  28 static char *version =
  29     "ne.c:v1.10 9/23/94 Donald Becker (becker@cesdis.gsfc.nasa.gov)\n";
  30 
  31 #include <linux/kernel.h>
  32 #include <linux/sched.h>
  33 #include <linux/errno.h>
  34 #include <asm/system.h>
  35 #include <asm/io.h>
  36 
  37 #include <linux/netdevice.h>
  38 #include "8390.h"
  39 
  40 /* Some defines that people can play with if so inclined. */
  41 
  42 /* Do we support clones that don't adhere to 14,15 of the SAprom ? */
  43 #define CONFIG_NE_BAD_CLONES
  44 
  45 /* Do we perform extra sanity checks on stuff ? */
  46 /* #define CONFIG_NE_SANITY */
  47 
  48 /* Do we implement the read before write bugfix ? */
  49 /* #define CONFIG_NE_RW_BUGFIX */
  50 
  51 /* ---- No user-serviceable parts below ---- */
  52 
  53 extern struct device *init_etherdev(struct device *dev, int sizeof_private,
  54                                     unsigned long *mem_startp);
  55 
  56 
  57 /* A zero-terminated list of I/O addresses to be probed. */
  58 static unsigned int netcard_portlist[] =
  59 { 0x300, 0x280, 0x320, 0x340, 0x360, 0};
  60 
  61 #ifdef CONFIG_NE_BAD_CLONES
  62 /* A list of bad clones that we none-the-less recognize. */
  63 static struct { char *name8, *name16; unsigned char SAprefix[4];}
  64 bad_clone_list[] = {
  65     {"DE100", "DE200", {0x00, 0xDE, 0x01,}},
  66     {"DE120", "DE220", {0x00, 0x80, 0xc8,}},
  67     {"DFI1000", "DFI2000", {'D', 'F', 'I',}}, /* Original, eh?  */
  68     {"EtherNext UTP8", "EtherNext UTP16", {0x00, 0x00, 0x79}},
  69     {"NE1000","NE2000-invalid", {0x00, 0x00, 0xd8}}, /* Ancient real NE1000. */
  70     {"NN1000", "NN2000",  {0x08, 0x03, 0x08}}, /* Outlaw no-name clone. */
  71     {"4-DIM8","4-DIM16", {0x00,0x00,0x4d,}},  /* Outlaw 4-Dimension cards. */
  72     {0,}
  73 };
  74 #endif
  75 
  76 #define NE_BASE  (dev->base_addr)
  77 #define NE_CMD          0x00
  78 #define NE_DATAPORT     0x10    /* NatSemi-defined port window offset. */
  79 #define NE_RESET        0x1f    /* Issue a read to reset, a write to clear. */
  80 #define NE_IO_EXTENT    0x20
  81 
  82 #define NE1SM_START_PG  0x20    /* First page of TX buffer */
  83 #define NE1SM_STOP_PG   0x40    /* Last page +1 of RX ring */
  84 #define NESM_START_PG   0x40    /* First page of TX buffer */
  85 #define NESM_STOP_PG    0x80    /* Last page +1 of RX ring */
  86 
  87 #define NE_RDC_TIMEOUT  0x02    /* Max wait in jiffies for Tx RDC */
  88 
  89 int ne_probe(struct device *dev);
  90 static int ne_probe1(struct device *dev, int ioaddr);
  91 
  92 static void ne_reset_8390(struct device *dev);
  93 static int ne_block_input(struct device *dev, int count,
  94                           char *buf, int ring_offset);
  95 static void ne_block_output(struct device *dev, const int count,
  96                 const unsigned char *buf, const int start_page);
  97 
  98 
  99 /*  Probe for various non-shared-memory ethercards.
 100 
 101    NEx000-clone boards have a Station Address PROM (SAPROM) in the packet
 102    buffer memory space.  NE2000 clones have 0x57,0x57 in bytes 0x0e,0x0f of
 103    the SAPROM, while other supposed NE2000 clones must be detected by their
 104    SA prefix.
 105 
 106    Reading the SAPROM from a word-wide card with the 8390 set in byte-wide
 107    mode results in doubled values, which can be detected and compensated for.
 108 
 109    The probe is also responsible for initializing the card and filling
 110    in the 'dev' and 'ei_status' structures.
 111 
 112    We use the minimum memory size for some ethercard product lines, iff we can't
 113    distinguish models.  You can increase the packet buffer size by setting
 114    PACKETBUF_MEMSIZE.  Reported Cabletron packet buffer locations are:
 115         E1010   starts at 0x100 and ends at 0x2000.
 116         E1010-x starts at 0x100 and ends at 0x8000. ("-x" means "more memory")
 117         E2010    starts at 0x100 and ends at 0x4000.
 118         E2010-x starts at 0x100 and ends at 0xffff.  */
 119 
 120 #ifdef HAVE_DEVLIST
 121 struct netdev_entry netcard_drv =
 122 {"ne", ne_probe1, NE_IO_EXTENT, netcard_portlist};
 123 #else
 124 
 125 int ne_probe(struct device *dev)
     /* [previous][next][first][last][top][bottom][index][help] */
 126 {
 127     int i;
 128     int base_addr = dev ? dev->base_addr : 0;
 129 
 130     if (base_addr > 0x1ff)      /* Check a single specified location. */
 131         return ne_probe1(dev, base_addr);
 132     else if (base_addr != 0)    /* Don't probe at all. */
 133         return ENXIO;
 134 
 135     for (i = 0; netcard_portlist[i]; i++) {
 136         int ioaddr = netcard_portlist[i];
 137         if (check_region(ioaddr, NE_IO_EXTENT))
 138             continue;
 139         if (ne_probe1(dev, ioaddr) == 0)
 140             return 0;
 141     }
 142 
 143     return ENODEV;
 144 }
 145 #endif
 146 
 147 static int ne_probe1(struct device *dev, int ioaddr)
     /* [previous][next][first][last][top][bottom][index][help] */
 148 {
 149     int i;
 150     unsigned char SA_prom[32];
 151     int wordlength = 2;
 152     char *name = NULL;
 153     int start_page, stop_page;
 154     int neX000, ctron;
 155     int reg0 = inb_p(ioaddr);
 156 
 157     if (reg0 == 0xFF)
 158         return ENODEV;
 159 
 160     /* Do a preliminary verification that we have a 8390. */
 161     {   int regd;
 162         outb_p(E8390_NODMA+E8390_PAGE1+E8390_STOP, ioaddr + E8390_CMD);
 163         regd = inb_p(ioaddr + 0x0d);
 164         outb_p(0xff, ioaddr + 0x0d);
 165         outb_p(E8390_NODMA+E8390_PAGE0, ioaddr + E8390_CMD);
 166         inb_p(ioaddr + EN0_COUNTER0); /* Clear the counter by reading. */
 167         if (inb_p(ioaddr + EN0_COUNTER0) != 0) {
 168             outb_p(reg0, ioaddr);
 169             outb_p(regd, ioaddr + 0x0d);        /* Restore the old values. */
 170             return ENODEV;
 171         }
 172     }
 173 
 174     printk("NE*000 ethercard probe at %#3x:", ioaddr);
 175 
 176     /* Read the 16 bytes of station address PROM.
 177        We must first initialize registers, similar to NS8390_init(eifdev, 0).
 178        We can't reliably read the SAPROM address without this.
 179        (I learned the hard way!). */
 180     {
 181         struct {unsigned char value, offset; } program_seq[] = {
 182             {E8390_NODMA+E8390_PAGE0+E8390_STOP, E8390_CMD}, /* Select page 0*/
 183             {0x48,      EN0_DCFG},      /* Set byte-wide (0x48) access. */
 184             {0x00,      EN0_RCNTLO},    /* Clear the count regs. */
 185             {0x00,      EN0_RCNTHI},
 186             {0x00,      EN0_IMR},       /* Mask completion irq. */
 187             {0xFF,      EN0_ISR},
 188             {E8390_RXOFF, EN0_RXCR},    /* 0x20  Set to monitor */
 189             {E8390_TXOFF, EN0_TXCR},    /* 0x02  and loopback mode. */
 190             {32,        EN0_RCNTLO},
 191             {0x00,      EN0_RCNTHI},
 192             {0x00,      EN0_RSARLO},    /* DMA starting at 0x0000. */
 193             {0x00,      EN0_RSARHI},
 194             {E8390_RREAD+E8390_START, E8390_CMD},
 195         };
 196         for (i = 0; i < sizeof(program_seq)/sizeof(program_seq[0]); i++)
 197             outb_p(program_seq[i].value, ioaddr + program_seq[i].offset);
 198 
 199     }
 200     for(i = 0; i < 32 /*sizeof(SA_prom)*/; i+=2) {
 201         SA_prom[i] = inb(ioaddr + NE_DATAPORT);
 202         SA_prom[i+1] = inb(ioaddr + NE_DATAPORT);
 203         if (SA_prom[i] != SA_prom[i+1])
 204             wordlength = 1;
 205     }
 206 
 207     if (wordlength == 2) {
 208         /* We must set the 8390 for word mode. */
 209         outb_p(0x49, ioaddr + EN0_DCFG);
 210         /* We used to reset the ethercard here, but it doesn't seem
 211            to be necessary. */
 212         /* Un-double the SA_prom values. */
 213         for (i = 0; i < 16; i++)
 214             SA_prom[i] = SA_prom[i+i];
 215         start_page = NESM_START_PG;
 216         stop_page = NESM_STOP_PG;
 217     } else {
 218         start_page = NE1SM_START_PG;
 219         stop_page = NE1SM_STOP_PG;
 220     }
 221 
 222     for(i = 0; i < ETHER_ADDR_LEN; i++) {
 223         dev->dev_addr[i] = SA_prom[i];
 224         printk(" %2.2x", SA_prom[i]);
 225     }
 226 
 227     neX000 = (SA_prom[14] == 0x57  &&  SA_prom[15] == 0x57);
 228     ctron =  (SA_prom[0] == 0x00 && SA_prom[1] == 0x00 && SA_prom[2] == 0x1d);
 229 
 230     /* Set up the rest of the parameters. */
 231     if (neX000) {
 232         name = (wordlength == 2) ? "NE2000" : "NE1000";
 233     } else if (ctron) {
 234         name = "Cabletron";
 235         start_page = 0x01;
 236         stop_page = (wordlength == 2) ? 0x40 : 0x20;
 237     } else {
 238 #ifdef CONFIG_NE_BAD_CLONES
 239         /* Ack!  Well, there might be a *bad* NE*000 clone there.
 240            Check for total bogus addresses. */
 241         for (i = 0; bad_clone_list[i].name8; i++) {
 242             if (SA_prom[0] == bad_clone_list[i].SAprefix[0] &&
 243                 SA_prom[1] == bad_clone_list[i].SAprefix[1] &&
 244                 SA_prom[2] == bad_clone_list[i].SAprefix[2]) {
 245                 if (wordlength == 2) {
 246                     name = bad_clone_list[i].name16;
 247                 } else {
 248                     name = bad_clone_list[i].name8;
 249                 }
 250                 break;
 251             }
 252         }
 253         if (bad_clone_list[i].name8 == NULL) {
 254             printk(" not found (invalid signature %2.2x %2.2x).\n",
 255                    SA_prom[14], SA_prom[15]);
 256             return ENXIO;
 257         }
 258 #else
 259         printk(" not found.\n");
 260         return ENXIO;
 261 #endif
 262 
 263     }
 264 
 265 
 266     if (dev == NULL)
 267         dev = init_etherdev(0, sizeof(struct ei_device), 0);
 268 
 269     if (dev->irq < 2) {
 270         autoirq_setup(0);
 271         outb_p(0x50, ioaddr + EN0_IMR); /* Enable one interrupt. */
 272         outb_p(0x00, ioaddr + EN0_RCNTLO);
 273         outb_p(0x00, ioaddr + EN0_RCNTHI);
 274         outb_p(E8390_RREAD+E8390_START, ioaddr); /* Trigger it... */
 275         outb_p(0x00, ioaddr + EN0_IMR);                 /* Mask it again. */
 276         dev->irq = autoirq_report(0);
 277         if (ei_debug > 2)
 278             printk(" autoirq is %d\n", dev->irq);
 279     } else if (dev->irq == 2)
 280         /* Fixup for users that don't know that IRQ 2 is really IRQ 9,
 281            or don't know which one to set. */
 282         dev->irq = 9;
 283     
 284     /* Snarf the interrupt now.  There's no point in waiting since we cannot
 285        share and the board will usually be enabled. */
 286     {
 287         int irqval = request_irq (dev->irq, ei_interrupt, 0, wordlength==2 ? "ne2000":"ne1000");
 288         if (irqval) {
 289             printk (" unable to get IRQ %d (irqval=%d).\n", dev->irq, irqval);
 290             return EAGAIN;
 291         }
 292     }
 293 
 294     dev->base_addr = ioaddr;
 295 
 296     request_region(ioaddr, NE_IO_EXTENT, wordlength==2 ? "ne2000":"ne1000");
 297 
 298     for(i = 0; i < ETHER_ADDR_LEN; i++)
 299         dev->dev_addr[i] = SA_prom[i];
 300 
 301     ethdev_init(dev);
 302     printk("\n%s: %s found at %#x, using IRQ %d.\n",
 303            dev->name, name, ioaddr, dev->irq);
 304 
 305     if (ei_debug > 0)
 306         printk(version);
 307 
 308     ei_status.name = name;
 309     ei_status.tx_start_page = start_page;
 310     ei_status.stop_page = stop_page;
 311     ei_status.word16 = (wordlength == 2);
 312 
 313     ei_status.rx_start_page = start_page + TX_PAGES;
 314 #ifdef PACKETBUF_MEMSIZE
 315     /* Allow the packet buffer size to be overridden by know-it-alls. */
 316     ei_status.stop_page = ei_status.tx_start_page + PACKETBUF_MEMSIZE;
 317 #endif
 318 
 319     ei_status.reset_8390 = &ne_reset_8390;
 320     ei_status.block_input = &ne_block_input;
 321     ei_status.block_output = &ne_block_output;
 322     NS8390_init(dev, 0);
 323     return 0;
 324 }
 325 
 326 /* Hard reset the card.  This used to pause for the same period that a
 327    8390 reset command required, but that shouldn't be necessary. */
 328 static void
 329 ne_reset_8390(struct device *dev)
     /* [previous][next][first][last][top][bottom][index][help] */
 330 {
 331     int tmp = inb_p(NE_BASE + NE_RESET);
 332     int reset_start_time = jiffies;
 333 
 334     if (ei_debug > 1) printk("resetting the 8390 t=%ld...", jiffies);
 335     ei_status.txing = 0;
 336     ei_status.dmaing = 0;
 337 
 338     outb_p(tmp, NE_BASE + NE_RESET);
 339     /* This check _should_not_ be necessary, omit eventually. */
 340     while ((inb_p(NE_BASE+EN0_ISR) & ENISR_RESET) == 0)
 341         if (jiffies - reset_start_time > 2) {
 342             printk("%s: ne_reset_8390() did not complete.\n", dev->name);
 343             break;
 344         }
 345     outb_p(ENISR_RESET, NE_BASE + EN0_ISR);     /* Ack intr. */
 346 }
 347 
 348 /* Block input and output, similar to the Crynwr packet driver.  If you
 349    are porting to a new ethercard, look at the packet driver source for hints.
 350    The NEx000 doesn't share it on-board packet memory -- you have to put
 351    the packet out through the "remote DMA" dataport using outb. */
 352 
 353 static int
 354 ne_block_input(struct device *dev, int count, char *buf, int ring_offset)
     /* [previous][next][first][last][top][bottom][index][help] */
 355 {
 356 #ifdef CONFIG_NE_SANITY
 357     int xfer_count = count;
 358 #endif
 359     int nic_base = dev->base_addr;
 360 
 361     /* This *shouldn't* happen. If it does, it's the last thing you'll see */
 362     if (ei_status.dmaing) {
 363         if (ei_debug > 0)
 364             printk("%s: DMAing conflict in ne_block_input "
 365                    "[DMAstat:%d][irqlock:%d][intr:%d].\n",
 366                    dev->name, ei_status.dmaing, ei_status.irqlock,
 367                    dev->interrupt);
 368         return 0;
 369     }
 370     ei_status.dmaing |= 0x01;
 371     outb_p(E8390_NODMA+E8390_PAGE0+E8390_START, nic_base+ NE_CMD);
 372     outb_p(count & 0xff, nic_base + EN0_RCNTLO);
 373     outb_p(count >> 8, nic_base + EN0_RCNTHI);
 374     outb_p(ring_offset & 0xff, nic_base + EN0_RSARLO);
 375     outb_p(ring_offset >> 8, nic_base + EN0_RSARHI);
 376     outb_p(E8390_RREAD+E8390_START, nic_base + NE_CMD);
 377     if (ei_status.word16) {
 378       insw(NE_BASE + NE_DATAPORT,buf,count>>1);
 379       if (count & 0x01) {
 380         buf[count-1] = inb(NE_BASE + NE_DATAPORT);
 381 #ifdef CONFIG_NE_SANITY
 382         xfer_count++;
 383 #endif
 384       }
 385     } else {
 386         insb(NE_BASE + NE_DATAPORT, buf, count);
 387     }
 388 
 389     /* This was for the ALPHA version only, but enough people have
 390        been encountering problems so it is still here.  If you see
 391        this message you either 1) have a slightly incompatible clone
 392        or 2) have noise/speed problems with your bus. */
 393 #ifdef CONFIG_NE_SANITY
 394     if (ei_debug > 1) {         /* DMA termination address check... */
 395         int addr, tries = 20;
 396         do {
 397             /* DON'T check for 'inb_p(EN0_ISR) & ENISR_RDC' here
 398                -- it's broken for Rx on some cards! */
 399             int high = inb_p(nic_base + EN0_RSARHI);
 400             int low = inb_p(nic_base + EN0_RSARLO);
 401             addr = (high << 8) + low;
 402             if (((ring_offset + xfer_count) & 0xff) == low)
 403                 break;
 404         } while (--tries > 0);
 405         if (tries <= 0)
 406             printk("%s: RX transfer address mismatch,"
 407                    "%#4.4x (expected) vs. %#4.4x (actual).\n",
 408                    dev->name, ring_offset + xfer_count, addr);
 409     }
 410 #endif
 411     outb_p(ENISR_RDC, nic_base + EN0_ISR);      /* Ack intr. */
 412     ei_status.dmaing &= ~0x01;
 413     return ring_offset + count;
 414 }
 415 
 416 static void
 417 ne_block_output(struct device *dev, int count,
     /* [previous][next][first][last][top][bottom][index][help] */
 418                 const unsigned char *buf, const int start_page)
 419 {
 420 #ifdef CONFIG_NE_SANITY
 421     int retries = 0;
 422 #endif
 423     int nic_base = NE_BASE;
 424     unsigned long dma_start;
 425 
 426     /* Round the count up for word writes.  Do we need to do this?
 427        What effect will an odd byte count have on the 8390?
 428        I should check someday. */
 429     if (ei_status.word16 && (count & 0x01))
 430       count++;
 431 
 432     /* This *shouldn't* happen. If it does, it's the last thing you'll see */
 433     if (ei_status.dmaing) {
 434         if (ei_debug > 0)
 435             printk("%s: DMAing conflict in ne_block_output."
 436                    "[DMAstat:%d][irqlock:%d][intr:%d]\n",
 437                    dev->name, ei_status.dmaing, ei_status.irqlock,
 438                    dev->interrupt);
 439         return;
 440     }
 441     ei_status.dmaing |= 0x01;
 442     /* We should already be in page 0, but to be safe... */
 443     outb_p(E8390_PAGE0+E8390_START+E8390_NODMA, nic_base + NE_CMD);
 444 
 445 #ifdef CONFIG_NE_SANITY
 446  retry:
 447 #endif
 448 
 449 #ifdef CONFIG_NE_RW_BUGFIX 
 450     /* Handle the read-before-write bug the same way as the
 451        Crynwr packet driver -- the NatSemi method doesn't work.
 452        Actually this doesn't always work either, but if you have
 453        problems with your NEx000 this is better than nothing! */
 454     outb_p(0x42, nic_base + EN0_RCNTLO);
 455     outb_p(0x00,   nic_base + EN0_RCNTHI);
 456     outb_p(0x42, nic_base + EN0_RSARLO);
 457     outb_p(0x00, nic_base + EN0_RSARHI);
 458     outb_p(E8390_RREAD+E8390_START, nic_base + NE_CMD);
 459     /* Make certain that the dummy read has occurred. */
 460     SLOW_DOWN_IO;
 461     SLOW_DOWN_IO;
 462     SLOW_DOWN_IO;
 463 #endif  /* rw_bugfix */
 464 
 465     outb_p(ENISR_RDC, nic_base + EN0_ISR);
 466 
 467    /* Now the normal output. */
 468     outb_p(count & 0xff, nic_base + EN0_RCNTLO);
 469     outb_p(count >> 8,   nic_base + EN0_RCNTHI);
 470     outb_p(0x00, nic_base + EN0_RSARLO);
 471     outb_p(start_page, nic_base + EN0_RSARHI);
 472 
 473     outb_p(E8390_RWRITE+E8390_START, nic_base + NE_CMD);
 474     if (ei_status.word16) {
 475         outsw(NE_BASE + NE_DATAPORT, buf, count>>1);
 476     } else {
 477         outsb(NE_BASE + NE_DATAPORT, buf, count);
 478     }
 479 
 480     dma_start = jiffies;
 481 
 482 #ifdef CONFIG_NE_SANITY
 483     /* This was for the ALPHA version only, but enough people have
 484        been encountering problems so it is still here. */
 485     if (ei_debug > 1) {         /* DMA termination address check... */
 486         int addr, tries = 20;
 487         do {
 488             int high = inb_p(nic_base + EN0_RSARHI);
 489             int low = inb_p(nic_base + EN0_RSARLO);
 490             addr = (high << 8) + low;
 491             if ((start_page << 8) + count == addr)
 492                 break;
 493         } while (--tries > 0);
 494         if (tries <= 0) {
 495             printk("%s: Tx packet transfer address mismatch,"
 496                    "%#4.4x (expected) vs. %#4.4x (actual).\n",
 497                    dev->name, (start_page << 8) + count, addr);
 498             if (retries++ == 0)
 499                 goto retry;
 500         }
 501     }
 502 #endif
 503 
 504     while ((inb_p(nic_base + EN0_ISR) & ENISR_RDC) == 0)
 505         if (jiffies - dma_start > NE_RDC_TIMEOUT) {
 506                 printk("%s: timeout waiting for Tx RDC.\n", dev->name);
 507                 ne_reset_8390(dev);
 508                 NS8390_init(dev,1);
 509                 break;
 510         }
 511 
 512     outb_p(ENISR_RDC, nic_base + EN0_ISR);      /* Ack intr. */
 513     ei_status.dmaing &= ~0x01;
 514     return;
 515 }
 516 
 517 
 518 /*
 519  * Local variables:
 520  *  compile-command: "gcc -DKERNEL -Wall -O6 -fomit-frame-pointer -I/usr/src/linux/net/tcp -c ne.c"
 521  *  version-control: t
 522  *  kept-new-versions: 5
 523  * End:
 524  */

/* [previous][next][first][last][top][bottom][index][help] */