root/drivers/net/ne.c

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

DEFINITIONS

This source file includes following definitions.
  1. ne_probe
  2. neprobe1
  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,1993 by Donald Becker.
   4 
   5     Copyright 1993 United States Government as represented by the
   6     Director, National Security Agency.  This software may be used and
   7     distributed according to the terms of the GNU Public License,
   8     incorporated herein by reference.
   9 
  10     This driver should work with many 8390-based ethernet boards.  Currently
  11     it support the NE1000, NE2000, clones, and some Cabletron products.
  12 
  13     The Author may be reached as becker@super.org or
  14     C/O Supercomputing Research Ctr., 17100 Science Dr., Bowie MD 20715
  15 */
  16 
  17 /* Routines for the NatSemi-based designs (NE[12]000). */
  18 
  19 static char *version =
  20     "ne.c:v0.99-14g 12/21/93 Donald Becker (becker@super.org)\n";
  21 
  22 #include <linux/config.h>
  23 #include <linux/kernel.h>
  24 #include <linux/sched.h>
  25 #include <linux/errno.h>
  26 #include <asm/system.h>
  27 #include <asm/io.h>
  28 #ifndef port_read
  29 #include "iow.h"
  30 #endif
  31 
  32 #include "dev.h"
  33 #include "8390.h"
  34 
  35 #define NE_BASE  (dev->base_addr)
  36 #define NE_CMD          0x00
  37 #define NE_DATAPORT     0x10    /* NatSemi-defined port window offset. */
  38 #define NE_RESET        0x1f    /* Issue a read to reset, a write to clear. */
  39 
  40 #define NE1SM_START_PG  0x20    /* First page of TX buffer */
  41 #define NE1SM_STOP_PG   0x40    /* Last page +1 of RX ring */
  42 #define NESM_START_PG   0x40    /* First page of TX buffer */
  43 #define NESM_STOP_PG    0x80    /* Last page +1 of RX ring */
  44 
  45 int ne_probe(struct device *dev);
  46 static int neprobe1(int ioaddr, struct device *dev, int verbose);
  47 
  48 static void ne_reset_8390(struct device *dev);
  49 static int ne_block_input(struct device *dev, int count,
  50                           char *buf, int ring_offset);
  51 static void ne_block_output(struct device *dev, const int count,
  52                 const unsigned char *buf, const int start_page);
  53 
  54 
  55 /*  Probe for various non-shared-memory ethercards.
  56 
  57    NEx000-clone boards have a Station Address PROM (SAPROM) in the packet
  58    buffer memory space.  NE2000 clones have 0x57,0x57 in bytes 0x0e,0x0f of
  59    the SAPROM, while other supposed NE2000 clones must be detected by their
  60    SA prefix.
  61 
  62    Reading the SAPROM from a word-wide card with the 8390 set in byte-wide
  63    mode results in doubled values, which can be detected and compansated for.
  64 
  65    The probe is also responsible for initializing the card and filling
  66    in the 'dev' and 'ei_status' structures.
  67 
  68    We use the minimum memory size for some ethercard product lines, iff we can't
  69    distinguish models.  You can increase the packet buffer size by setting
  70    PACKETBUF_MEMSIZE.  Reported Cabletron packet buffer locations are:
  71         E1010   starts at 0x100 and ends at 0x2000.
  72         E1010-x starts at 0x100 and ends at 0x8000. ("-x" means "more memory")
  73         E2010    starts at 0x100 and ends at 0x4000.
  74         E2010-x starts at 0x100 and ends at 0xffff.  */
  75 
  76 int ne_probe(struct device *dev)
     /* [previous][next][first][last][top][bottom][index][help] */
  77 {
  78     int *port, ports[] = {0x300, 0x280, 0x320, 0x340, 0x360, 0};
  79     short ioaddr = dev->base_addr;
  80 
  81     if (ioaddr < 0)
  82         return ENXIO;           /* Don't probe at all. */
  83     if (ioaddr > 0x100)
  84         return ! neprobe1(ioaddr, dev, 1);
  85 
  86     for (port = &ports[0]; *port; port++) {
  87 #ifdef HAVE_PORTRESERVE
  88         if (check_region(*port, 32))
  89             continue;
  90 #endif
  91         if (inb_p(*port) != 0xff && neprobe1(*port, dev, 0)) {
  92             dev->base_addr = *port;
  93             return 0;
  94         }
  95     }
  96     dev->base_addr = ioaddr;
  97     return ENODEV;
  98 }
  99 
 100 static int neprobe1(int ioaddr, struct device *dev, int verbose)
     /* [previous][next][first][last][top][bottom][index][help] */
 101 {
 102     int i;
 103     unsigned char SA_prom[32];
 104     int wordlength = 2;
 105     char *name;
 106     int start_page, stop_page;
 107     int neX000, ctron, dlink, dfi;
 108     int reg0 = inb(ioaddr);
 109 
 110     if ( reg0 == 0xFF)
 111         return 0;
 112 
 113     /* Do a quick preliminary check that we have a 8390. */
 114     {   int regd;
 115         outb_p(E8390_NODMA+E8390_PAGE1+E8390_STOP, ioaddr + E8390_CMD);
 116         regd = inb_p(ioaddr + 0x0d);
 117         outb_p(0xff, ioaddr + 0x0d);
 118         outb_p(E8390_NODMA+E8390_PAGE0, ioaddr + E8390_CMD);
 119         inb_p(ioaddr + EN0_COUNTER0); /* Clear the counter by reading. */
 120         if (inb_p(ioaddr + EN0_COUNTER0) != 0) {
 121             outb_p(reg0, ioaddr);
 122             outb(regd, ioaddr + 0x0d);  /* Restore the old values. */
 123             return 0;
 124         }
 125     }
 126 
 127     printk("NE*000 ethercard probe at %#3x:", ioaddr);
 128 
 129     /* Read the 16 bytes of station address prom, returning 1 for
 130        an eight-bit interface and 2 for a 16-bit interface.
 131        We must first initialize registers, similar to NS8390_init(eifdev, 0).
 132        We can't reliably read the SAPROM address without this.
 133        (I learned the hard way!). */
 134     {
 135         struct {unsigned char value, offset; } program_seq[] = {
 136             {E8390_NODMA+E8390_PAGE0+E8390_STOP, E8390_CMD}, /* Select page 0*/
 137             {0x48,      EN0_DCFG},      /* Set byte-wide (0x48) access. */
 138             {0x00,      EN0_RCNTLO},    /* Clear the count regs. */
 139             {0x00,      EN0_RCNTHI},
 140             {0x00,      EN0_IMR},       /* Mask completion irq. */
 141             {0xFF,      EN0_ISR},
 142             {E8390_RXOFF, EN0_RXCR},    /* 0x20  Set to monitor */
 143             {E8390_TXOFF, EN0_TXCR},    /* 0x02  and loopback mode. */
 144             {32,        EN0_RCNTLO},
 145             {0x00,      EN0_RCNTHI},
 146             {0x00,      EN0_RSARLO},    /* DMA starting at 0x0000. */
 147             {0x00,      EN0_RSARHI},
 148             {E8390_RREAD+E8390_START, E8390_CMD},
 149         };
 150         for (i = 0; i < sizeof(program_seq)/sizeof(program_seq[0]); i++)
 151             outb_p(program_seq[i].value, ioaddr + program_seq[i].offset);
 152     }
 153     for(i = 0; i < 32 /*sizeof(SA_prom)*/; i+=2) {
 154         SA_prom[i] = inb(ioaddr + NE_DATAPORT);
 155         SA_prom[i+1] = inb(ioaddr + NE_DATAPORT);
 156         if (SA_prom[i] != SA_prom[i+1])
 157             wordlength = 1;
 158     }
 159 
 160     if (wordlength == 2) {
 161         /* We must set the 8390 for word mode, AND RESET IT. */
 162         int tmp;
 163         outb_p(0x49, ioaddr + EN0_DCFG);
 164         tmp = inb_p(NE_BASE + NE_RESET);
 165         outb(tmp, NE_BASE + NE_RESET);
 166         /* Un-double the SA_prom values. */
 167         for (i = 0; i < 16; i++)
 168             SA_prom[i] = SA_prom[i+i];
 169     }
 170 
 171 #if defined(show_all_SAPROM)
 172     /* If your ethercard isn't detected define this to see the SA_PROM. */
 173     for(i = 0; i < sizeof(SA_prom); i++)
 174         printk(" %2.2x", SA_prom[i]);
 175 #else
 176     for(i = 0; i < ETHER_ADDR_LEN; i++) {
 177         dev->dev_addr[i] = SA_prom[i];
 178         printk(" %2.2x", SA_prom[i]);
 179     }
 180 #endif
 181 
 182     neX000 = (SA_prom[14] == 0x57  &&  SA_prom[15] == 0x57);
 183     ctron =  (SA_prom[0] == 0x00 && SA_prom[1] == 0x00 && SA_prom[2] == 0x1d);
 184     dlink =  (SA_prom[0] == 0x00 && SA_prom[1] == 0xDE && SA_prom[2] == 0x01);
 185     dfi   =  (SA_prom[0] == 'D' && SA_prom[1] == 'F' && SA_prom[2] == 'I');
 186 
 187     /* Set up the rest of the parameters. */
 188     if (neX000 || dlink || dfi) {
 189         if (wordlength == 2) {
 190             name = dlink ? "DE200" : "NE2000";
 191             start_page = NESM_START_PG;
 192             stop_page = NESM_STOP_PG;
 193         } else {
 194             name = dlink ? "DE100" : "NE1000";
 195             start_page = NE1SM_START_PG;
 196             stop_page = NE1SM_STOP_PG;
 197         }
 198     } else if (ctron) {
 199         name = "Cabletron";
 200         start_page = 0x01;
 201         stop_page = (wordlength == 2) ? 0x40 : 0x20;
 202     } else {
 203         printk(" not found.\n");
 204         return 0;
 205     }
 206 
 207     if (dev->irq < 2) {
 208         autoirq_setup(0);
 209         outb_p(0x50, ioaddr + EN0_IMR); /* Enable one interrupt. */
 210         outb_p(0x00, ioaddr + EN0_RCNTLO);
 211         outb_p(0x00, ioaddr + EN0_RCNTHI);
 212         outb_p(E8390_RREAD+E8390_START, ioaddr); /* Trigger it... */
 213         outb_p(0x00, ioaddr + EN0_IMR);                 /* Mask it again. */
 214         dev->irq = autoirq_report(0);
 215         if (ei_debug > 2)
 216             printk(" autoirq is %d", dev->irq);
 217     } else if (dev->irq == 2)
 218         /* Fixup for users that don't know that IRQ 2 is really IRQ 9,
 219            or don't know which one to set. */
 220         dev->irq = 9;
 221     
 222     /* Snarf the interrupt now.  There's no point in waiting since we cannot
 223        share and the board will usually be enabled. */
 224     {
 225         int irqval = irqaction (dev->irq, &ei_sigaction);
 226         if (irqval) {
 227             printk (" unable to get IRQ %d (irqval=%d).\n", dev->irq, irqval);
 228             return 0;
 229         }
 230     }
 231 
 232     dev->base_addr = ioaddr;
 233 
 234 #ifdef HAVE_PORTRESERVE
 235     snarf_region(ioaddr, 32);
 236 #endif
 237 
 238     ethdev_init(dev);
 239     printk("\n%s: %s found at %#x, using IRQ %d.\n",
 240            dev->name, name, ioaddr, dev->irq);
 241 
 242     if (ei_debug > 0)
 243         printk(version);
 244 
 245     ei_status.name = name;
 246     ei_status.tx_start_page = start_page;
 247     ei_status.stop_page = stop_page;
 248     ei_status.word16 = (wordlength == 2);
 249 
 250     ei_status.rx_start_page = start_page + TX_PAGES;
 251 #ifdef PACKETBUF_MEMSIZE
 252     /* Allow the packet buffer size to be overridden by know-it-alls. */
 253     ei_status.stop_page = ei_status.tx_start_page + PACKETBUF_MEMSIZE;
 254 #endif
 255 
 256     ei_status.reset_8390 = &ne_reset_8390;
 257     ei_status.block_input = &ne_block_input;
 258     ei_status.block_output = &ne_block_output;
 259     NS8390_init(dev, 0);
 260     return dev->base_addr;
 261 }
 262 
 263 /* Hard reset the card.  This used to pause for the same period that a
 264    8390 reset command required, but that shouldn't be necessary. */
 265 static void
 266 ne_reset_8390(struct device *dev)
     /* [previous][next][first][last][top][bottom][index][help] */
 267 {
 268     int tmp = inb_p(NE_BASE + NE_RESET);
 269     int reset_start_time = jiffies;
 270 
 271     if (ei_debug > 1) printk("resetting the 8390 t=%d...", jiffies);
 272     ei_status.txing = 0;
 273 
 274     outb_p(tmp, NE_BASE + NE_RESET);
 275     /* This check _should_not_ be necessary, omit eventually. */
 276     while ((inb_p(NE_BASE+EN0_ISR) & ENISR_RESET) == 0)
 277         if (jiffies - reset_start_time > 2) {
 278             printk("%s: ne_reset_8390() did not complete.\n", dev->name);
 279             break;
 280         }
 281 }
 282 
 283 /* Block input and output, similar to the Crynwr packet driver.  If you
 284    porting to a new ethercard look at the packet driver source for hints.
 285    The NEx000 doesn't share it on-board packet memory -- you have to put
 286    the packet out through the "remote DMA" dataport using outb. */
 287 
 288 static int
 289 ne_block_input(struct device *dev, int count, char *buf, int ring_offset)
     /* [previous][next][first][last][top][bottom][index][help] */
 290 {
 291     int xfer_count = count;
 292     int nic_base = dev->base_addr;
 293 
 294     if (ei_status.dmaing) {
 295         if (ei_debug > 0)
 296             printk("%s: DMAing conflict in ne_block_input."
 297                    "[DMAstat:%1x][irqlock:%1x]\n",
 298                    dev->name, ei_status.dmaing, ei_status.irqlock);
 299         return 0;
 300     }
 301     ei_status.dmaing |= 0x01;
 302     outb_p(E8390_NODMA+E8390_PAGE0+E8390_START, nic_base+ NE_CMD);
 303     outb_p(count & 0xff, nic_base + EN0_RCNTLO);
 304     outb_p(count >> 8, nic_base + EN0_RCNTHI);
 305     outb_p(ring_offset & 0xff, nic_base + EN0_RSARLO);
 306     outb_p(ring_offset >> 8, nic_base + EN0_RSARHI);
 307     outb_p(E8390_RREAD+E8390_START, nic_base + NE_CMD);
 308     if (ei_status.word16) {
 309       port_read(NE_BASE + NE_DATAPORT,buf,count>>1);
 310       if (count & 0x01)
 311         buf[count-1] = inb(NE_BASE + NE_DATAPORT), xfer_count++;
 312     } else {
 313         port_read_b(NE_BASE + NE_DATAPORT, buf, count);
 314     }
 315 
 316     /* This was for the ALPHA version only, but enough people have
 317        encountering problems that it is still here.  If you see
 318        this message you either 1) have an slightly imcompatible clone
 319        or 2) have noise/speed problems with your bus. */
 320     if (ei_debug > 1) {         /* DMA termination address check... */
 321         int addr, tries = 20;
 322         do {
 323             /* DON'T check for 'inb_p(EN0_ISR) & ENISR_RDC' here
 324                -- it's broken! Check the "DMA" address instead. */
 325             int high = inb_p(nic_base + EN0_RSARHI);
 326             int low = inb_p(nic_base + EN0_RSARLO);
 327             addr = (high << 8) + low;
 328             if (((ring_offset + xfer_count) & 0xff) == low)
 329                 break;
 330         } while (--tries > 0);
 331         if (tries <= 0)
 332             printk("%s: RX transfer address mismatch,"
 333                    "%#4.4x (expected) vs. %#4.4x (actual).\n",
 334                    dev->name, ring_offset + xfer_count, addr);
 335     }
 336     ei_status.dmaing &= ~0x01;
 337     return ring_offset + count;
 338 }
 339 
 340 static void
 341 ne_block_output(struct device *dev, int count,
     /* [previous][next][first][last][top][bottom][index][help] */
 342                 const unsigned char *buf, const int start_page)
 343 {
 344     int retries = 0;
 345     int nic_base = NE_BASE;
 346 
 347     /* Round the count up for word writes.  Do we need to do this?
 348        What effect will an odd byte count have on the 8390?
 349        I should check someday. */
 350     if (ei_status.word16 && (count & 0x01))
 351       count++;
 352     if (ei_status.dmaing) {
 353         if (ei_debug > 0)
 354             printk("%s: DMAing conflict in ne_block_output."
 355                    "[DMAstat:%1x][irqlock:%1x]\n",
 356                    dev->name, ei_status.dmaing, ei_status.irqlock);
 357         return;
 358     }
 359     ei_status.dmaing |= 0x02;
 360     /* We should already be in page 0, but to be safe... */
 361     outb_p(E8390_PAGE0+E8390_START+E8390_NODMA, nic_base + NE_CMD);
 362 
 363  retry:
 364 #if defined(rw_bugfix)
 365     /* Handle the read-before-write bug the same way as the
 366        Crynwr packet driver -- the NatSemi method doesn't work.
 367        Actually this doesn't aways work either, but if you have
 368        problems with your NEx000 this is better than nothing! */
 369     outb_p(0x42, nic_base + EN0_RCNTLO);
 370     outb_p(0x00,   nic_base + EN0_RCNTHI);
 371     outb_p(0x42, nic_base + EN0_RSARLO);
 372     outb_p(0x00, nic_base + EN0_RSARHI);
 373     outb_p(E8390_RREAD+E8390_START, nic_base + NE_CMD);
 374     /* Make certain that the dummy read has occured. */
 375     SLOW_DOWN_IO;
 376     SLOW_DOWN_IO;
 377     SLOW_DOWN_IO;
 378 #endif  /* rw_bugfix */
 379 
 380     /* Now the normal output. */
 381     outb_p(count & 0xff, nic_base + EN0_RCNTLO);
 382     outb_p(count >> 8,   nic_base + EN0_RCNTHI);
 383     outb_p(0x00, nic_base + EN0_RSARLO);
 384     outb_p(start_page, nic_base + EN0_RSARHI);
 385 
 386     outb_p(E8390_RWRITE+E8390_START, nic_base + NE_CMD);
 387     if (ei_status.word16) {
 388         port_write(NE_BASE + NE_DATAPORT, buf, count>>1);
 389     } else {
 390         port_write_b(NE_BASE + NE_DATAPORT, buf, count);
 391     }
 392 
 393     /* This was for the ALPHA version only, but enough people have
 394        encountering problems that it is still here. */
 395     if (ei_debug > 1) {         /* DMA termination address check... */
 396         int addr, tries = 20;
 397         do {
 398             /* DON'T check for 'inb_p(EN0_ISR) & ENISR_RDC' here
 399                -- it's broken! Check the "DMA" address instead. */
 400             int high = inb_p(nic_base + EN0_RSARHI);
 401             int low = inb_p(nic_base + EN0_RSARLO);
 402             addr = (high << 8) + low;
 403             if ((start_page << 8) + count == addr)
 404                 break;
 405         } while (--tries > 0);
 406         if (tries <= 0) {
 407             printk("%s: Tx packet transfer address mismatch,"
 408                    "%#4.4x (expected) vs. %#4.4x (actual).\n",
 409                    dev->name, (start_page << 8) + count, addr);
 410             if (retries++ == 0)
 411                 goto retry;
 412         }
 413     }
 414     ei_status.dmaing &= ~0x02;
 415     return;
 416 }
 417 
 418 
 419 /*
 420  * Local variables:
 421  *  compile-command: "gcc -DKERNEL -Wall -O6 -fomit-frame-pointer -I/usr/src/linux/net/tcp -c ne.c"
 422  *  version-control: t
 423  *  kept-new-versions: 5
 424  * End:
 425  */

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