root/drivers/net/3c503.c

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

DEFINITIONS

This source file includes following definitions.
  1. el2_probe
  2. el2_pio_probe
  3. el2_probe1
  4. el2_open
  5. el2_close
  6. el2_reset_8390
  7. el2_init_card
  8. el2_block_output
  9. el2_block_input

   1 /* 3c503.c: A 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.  This software may be used and
   7     distributed according to the terms of the GNU Public License,
   8     incorporated herein by reference.
   9 
  10     The author may be reached as becker@CESDIS.gsfc.nasa.gov, or C/O
  11     Center of Excellence in Space Data and Information Sciences
  12        Code 930.5, Goddard Space Flight Center, Greenbelt MD 20771
  13 
  14     This driver should work with the 3c503 and 3c503/16.  It should be used
  15     in shared memory mode for best performance, although it may also work
  16     in programmed-I/O mode.
  17 
  18     Sources:
  19     EtherLink II Technical Reference Guide,
  20     3Com Corporation, 5400 Bayfront Plaza, Santa Clara CA 95052-8145
  21     
  22     The Crynwr 3c503 packet driver.
  23 */
  24 
  25 static char *version =
  26     "3c503.c:v1.10 9/23/93  Donald Becker (becker@cesdis.gsfc.nasa.gov)\n";
  27 
  28 #include <linux/kernel.h>
  29 #include <linux/sched.h>
  30 #include <linux/errno.h>
  31 #include <linux/string.h>
  32 #include <linux/delay.h>
  33 #include <asm/io.h>
  34 #include <asm/system.h>
  35 
  36 #include <linux/netdevice.h>
  37 
  38 #include "8390.h"
  39 #include "3c503.h"
  40 
  41 extern struct device *init_etherdev(struct device *dev, int sizeof_private,
  42                                     unsigned long *mem_startp);
  43 
  44 int el2_probe(struct device *dev);
  45 int el2_pio_probe(struct device *dev);
  46 int el2_probe1(struct device *dev, int ioaddr);
  47 
  48 /* A zero-terminated list of I/O addresses to be probed in PIO mode. */
  49 static unsigned int netcard_portlist[] =
  50         { 0x300,0x310,0x330,0x350,0x250,0x280,0x2a0,0x2e0,0};
  51 
  52 #define EL2_IO_EXTENT   16
  53 
  54 #ifdef HAVE_DEVLIST
  55 /* The 3c503 uses two entries, one for the safe memory-mapped probe and
  56    the other for the typical I/O probe. */
  57 struct netdev_entry el2_drv =
  58 {"3c503", el2_probe, EL1_IO_EXTENT, 0};
  59 struct netdev_entry el2pio_drv =
  60 {"3c503pio", el2_pioprobe1, EL1_IO_EXTENT, netcard_portlist};
  61 #endif
  62 
  63 static int el2_open(struct device *dev);
  64 static int el2_close(struct device *dev);
  65 static void el2_reset_8390(struct device *dev);
  66 static void el2_init_card(struct device *dev);
  67 static void el2_block_output(struct device *dev, int count,
  68                              const unsigned char *buf, const start_page);
  69 static int el2_block_input(struct device *dev, int count, char *buf,
  70                            int ring_offset);
  71 
  72 
  73 /* This routine probes for a memory-mapped 3c503 board by looking for
  74    the "location register" at the end of the jumpered boot PROM space.
  75    This works even if a PROM isn't there.
  76 
  77    If the ethercard isn't found there is an optional probe for
  78    ethercard jumpered to programmed-I/O mode.
  79    */
  80 int
  81 el2_probe(struct device *dev)
     /* [previous][next][first][last][top][bottom][index][help] */
  82 {
  83     int *addr, addrs[] = { 0xddffe, 0xd9ffe, 0xcdffe, 0xc9ffe, 0};
  84     int base_addr = dev->base_addr;
  85 
  86     if (base_addr > 0x1ff)      /* Check a single specified location. */
  87         return el2_probe1(dev, base_addr);
  88     else if (base_addr != 0)            /* Don't probe at all. */
  89         return ENXIO;
  90 
  91     for (addr = addrs; *addr; addr++) {
  92         int i;
  93         unsigned int base_bits = *(unsigned char *)*addr;
  94         /* Find first set bit. */
  95         for(i = 7; i >= 0; i--, base_bits >>= 1)
  96             if (base_bits & 0x1)
  97                 break;
  98         if (base_bits != 1)
  99             continue;
 100         if (check_region(netcard_portlist[i], EL2_IO_EXTENT))
 101             continue;
 102         if (el2_probe1(dev, netcard_portlist[i]) == 0)
 103             return 0;
 104     }
 105 #if ! defined(no_probe_nonshared_memory) && ! defined (HAVE_DEVLIST)
 106     return el2_pio_probe(dev);
 107 #else
 108     return ENODEV;
 109 #endif
 110 }
 111 
 112 #ifndef HAVE_DEVLIST
 113 /*  Try all of the locations that aren't obviously empty.  This touches
 114     a lot of locations, and is much riskier than the code above. */
 115 int
 116 el2_pio_probe(struct device *dev)
     /* [previous][next][first][last][top][bottom][index][help] */
 117 {
 118     int i;
 119     int base_addr = dev ? dev->base_addr : 0;
 120 
 121     if (base_addr > 0x1ff)      /* Check a single specified location. */
 122         return el2_probe1(dev, base_addr);
 123     else if (base_addr != 0)    /* Don't probe at all. */
 124         return ENXIO;
 125 
 126     for (i = 0; netcard_portlist[i]; i++) {
 127         int ioaddr = netcard_portlist[i];
 128         if (check_region(ioaddr, EL2_IO_EXTENT))
 129             continue;
 130         if (el2_probe1(dev, ioaddr) == 0)
 131             return 0;
 132     }
 133 
 134     return ENODEV;
 135 }
 136 #endif
 137 
 138 /* Probe for the Etherlink II card at I/O port base IOADDR,
 139    returning non-zero on success.  If found, set the station
 140    address and memory parameters in DEVICE. */
 141 int
 142 el2_probe1(struct device *dev, int ioaddr)
     /* [previous][next][first][last][top][bottom][index][help] */
 143 {
 144     int i, iobase_reg, membase_reg, saved_406;
 145     static unsigned version_printed = 0;
 146 
 147     /* Reset and/or avoid any lurking NE2000 */
 148     if (inb(ioaddr + 0x408) == 0xff) {
 149         udelay(1000);
 150         return ENODEV;
 151     }
 152 
 153     /* We verify that it's a 3C503 board by checking the first three octets
 154        of its ethernet address. */
 155     iobase_reg = inb(ioaddr+0x403);
 156     membase_reg = inb(ioaddr+0x404);
 157     /* ASIC location registers should be 0 or have only a single bit set. */
 158     if (   (iobase_reg  & (iobase_reg - 1))
 159         || (membase_reg & (membase_reg - 1))) {
 160         return ENODEV;
 161     }
 162     saved_406 = inb_p(ioaddr + 0x406);
 163     outb_p(ECNTRL_RESET|ECNTRL_THIN, ioaddr + 0x406); /* Reset it... */
 164     outb_p(ECNTRL_THIN, ioaddr + 0x406);
 165     /* Map the station addr PROM into the lower I/O ports. */
 166     outb(ECNTRL_SAPROM|ECNTRL_THIN, ioaddr + 0x406);
 167     if (   inb(ioaddr + 0) != 0x02
 168         || inb(ioaddr + 1) != 0x60
 169         || inb(ioaddr + 2) != 0x8c) {
 170         /* Restore the register we frobbed. */
 171         outb(saved_406, ioaddr + 0x406);
 172         return ENODEV;
 173     }
 174 
 175     request_region(ioaddr, EL2_IO_EXTENT,"3c503");
 176 
 177     if (dev == NULL)
 178         dev = init_etherdev(0, sizeof(struct ei_device), 0);
 179 
 180     if (ei_debug  &&  version_printed++ == 0)
 181         printk(version);
 182 
 183     dev->base_addr = ioaddr;
 184     ethdev_init(dev);
 185 
 186     printk("%s: 3c503 at %#3x,", dev->name, ioaddr);
 187 
 188     /* Retrieve and print the ethernet address. */
 189     for (i = 0; i < 6; i++)
 190         printk(" %2.2x", dev->dev_addr[i] = inb(ioaddr + i));
 191 
 192     /* Map the 8390 back into the window. */
 193     outb(ECNTRL_THIN, ioaddr + 0x406);
 194 
 195     /* Probe for, turn on and clear the board's shared memory. */
 196     if (ei_debug > 2) printk(" memory jumpers %2.2x ", membase_reg);
 197     outb(EGACFR_NORM, ioaddr + 0x405);  /* Enable RAM */
 198 
 199     /* This should be probed for (or set via an ioctl()) at run-time.
 200        Right now we use a sleazy hack to pass in the interface number
 201        at boot-time via the low bits of the mem_end field.  That value is
 202        unused, and the low bits would be discarded even if it was used. */
 203 #if defined(EI8390_THICK) || defined(EL2_AUI)
 204     ei_status.interface_num = 1;
 205 #else
 206     ei_status.interface_num = dev->mem_end & 0xf;
 207 #endif
 208 
 209     if ((membase_reg & 0xf0) == 0) {
 210         dev->mem_start = 0;
 211     } else {
 212         dev->mem_start = ((membase_reg & 0xc0) ? 0xD8000 : 0xC8000) +
 213             ((membase_reg & 0xA0) ? 0x4000 : 0);
 214 
 215 #define EL2_MEMSIZE (EL2SM_STOP_PG - EL2SM_START_PG)*256
 216 #ifdef EL2MEMTEST
 217         /* This has never found an error, but someone might care. */
 218         {                       /* Check the card's memory. */
 219             int *mem_base = (int *)dev->mem_start;
 220             int memtest_value = 0xbbadf00d;
 221             mem_base[0] = 0xba5eba5e;
 222             for (i = 1; i < EL2_MEMSIZE/sizeof(mem_base[0]); i++) {
 223                 mem_base[i] = memtest_value;
 224                 if (mem_base[0] != 0xba5eba5e
 225                     || mem_base[i] != memtest_value) {
 226                     printk(" memory failure or memory address conflict.\n");
 227                     dev->mem_start = 0;
 228                     break;
 229                 }
 230                 memtest_value += 0x55555555;
 231                 mem_base[i] = 0;
 232             }
 233         }
 234 #endif  /* EL2MEMTEST */
 235         /* Divide the on-board memory into a single maximum-sized transmit
 236            (double-sized for ping-pong transmit) buffer at the base, and
 237            use the rest as a receive ring. */
 238         dev->mem_end = dev->rmem_end = dev->mem_start + EL2_MEMSIZE;
 239         dev->rmem_start = TX_PAGES*256 + dev->mem_start;
 240     }
 241 
 242     /* Finish setting the board's parameters. */
 243     ei_status.name = "3C503";
 244     ei_status.tx_start_page = EL2SM_START_PG;
 245     ei_status.rx_start_page = EL2SM_START_PG + TX_PAGES;
 246     ei_status.stop_page = EL2SM_STOP_PG;
 247     ei_status.reset_8390 = &el2_reset_8390;
 248     ei_status.block_input = &el2_block_input;
 249     ei_status.block_output = &el2_block_output;
 250 
 251     if (dev->irq == 2)
 252         dev->irq = 9;
 253     else if (dev->irq > 5 && dev->irq != 9) {
 254         printk("\n3c503: configured interrupt %d invalid, using autoIRQ.\n",
 255                dev->irq);
 256         dev->irq = 0;
 257     }
 258 
 259     ei_status.saved_irq = dev->irq;
 260 
 261     dev->start = 0;
 262     dev->open = &el2_open;
 263     dev->stop = &el2_close;
 264 
 265     if (dev->mem_start)
 266         printk("\n%s: %s with shared memory at %#6lx-%#6lx,\n",
 267                dev->name, ei_status.name, dev->mem_start, dev->mem_end-1);
 268     else
 269         printk("\n%s: %s using programmed I/O (REJUMPER for SHARED MEMORY).\n",
 270                dev->name, ei_status.name);
 271 
 272     if (ei_debug > 1)
 273         printk(version);
 274 
 275     return 0;
 276 }
 277 
 278 static int
 279 el2_open(struct device *dev)
     /* [previous][next][first][last][top][bottom][index][help] */
 280 {
 281 
 282     if (dev->irq < 2) {
 283         int irqlist[] = {5, 9, 3, 4, 0};
 284         int *irqp = irqlist;
 285 
 286         outb(EGACFR_NORM, E33G_GACFR);  /* Enable RAM and interrupts. */
 287         do {
 288             if (request_irq (*irqp, NULL, 0, "bogus") != -EBUSY) {
 289                 /* Twinkle the interrupt, and check if it's seen. */
 290                 autoirq_setup(0);
 291                 outb_p(0x04 << ((*irqp == 9) ? 2 : *irqp), E33G_IDCFR);
 292                 outb_p(0x00, E33G_IDCFR);
 293                 if (*irqp == autoirq_report(0)   /* It's a good IRQ line! */
 294                     && request_irq (dev->irq = *irqp, &ei_interrupt, 0, "3c503") == 0)
 295                     break;
 296             }
 297         } while (*++irqp);
 298         if (*irqp == 0) {
 299             outb(EGACFR_IRQOFF, E33G_GACFR);    /* disable interrupts. */
 300             return -EAGAIN;
 301         }
 302     } else {
 303         if (request_irq(dev->irq, &ei_interrupt, 0, "3c503")) {
 304             return -EAGAIN;
 305         }
 306     }
 307     el2_init_card(dev);
 308     return ei_open(dev);
 309 }
 310 
 311 static int
 312 el2_close(struct device *dev)
     /* [previous][next][first][last][top][bottom][index][help] */
 313 {
 314     free_irq(dev->irq);
 315     dev->irq = ei_status.saved_irq;
 316     irq2dev_map[dev->irq] = NULL;
 317     outb(EGACFR_IRQOFF, E33G_GACFR);    /* disable interrupts. */
 318 
 319     NS8390_init(dev, 0);
 320     dev->start = 0;
 321 
 322     return 0;
 323 }
 324 
 325 /* This is called whenever we have a unrecoverable failure:
 326        transmit timeout
 327        Bad ring buffer packet header
 328  */
 329 static void
 330 el2_reset_8390(struct device *dev)
     /* [previous][next][first][last][top][bottom][index][help] */
 331 {
 332     if (ei_debug > 1) {
 333         printk("%s: Resetting the 3c503 board...", dev->name);
 334         printk("%#lx=%#02x %#lx=%#02x %#lx=%#02x...", E33G_IDCFR, inb(E33G_IDCFR),
 335                E33G_CNTRL, inb(E33G_CNTRL), E33G_GACFR, inb(E33G_GACFR));
 336     }
 337     outb_p(ECNTRL_RESET|ECNTRL_THIN, E33G_CNTRL);
 338     ei_status.txing = 0;
 339     outb_p(ei_status.interface_num==0 ? ECNTRL_THIN : ECNTRL_AUI, E33G_CNTRL);
 340     el2_init_card(dev);
 341     if (ei_debug > 1) printk("done\n");
 342 }
 343 
 344 /* Initialize the 3c503 GA registers after a reset. */
 345 static void
 346 el2_init_card(struct device *dev)
     /* [previous][next][first][last][top][bottom][index][help] */
 347 {
 348     /* Unmap the station PROM and select the DIX or BNC connector. */
 349     outb_p(ei_status.interface_num==0 ? ECNTRL_THIN : ECNTRL_AUI, E33G_CNTRL);
 350 
 351     /* Set ASIC copy of rx's first and last+1 buffer pages */
 352     /* These must be the same as in the 8390. */
 353     outb(ei_status.rx_start_page, E33G_STARTPG);
 354     outb(ei_status.stop_page,  E33G_STOPPG);
 355 
 356     /* Point the vector pointer registers somewhere ?harmless?. */
 357     outb(0xff, E33G_VP2);       /* Point at the ROM restart location 0xffff0 */
 358     outb(0xff, E33G_VP1);
 359     outb(0x00, E33G_VP0);
 360     /* Turn off all interrupts until we're opened. */
 361     outb_p(0x00,  dev->base_addr + EN0_IMR);
 362     /* Enable IRQs iff started. */
 363     outb(EGACFR_NORM, E33G_GACFR);
 364 
 365     /* Set the interrupt line. */
 366     outb_p((0x04 << (dev->irq == 9 ? 2 : dev->irq)), E33G_IDCFR);
 367     outb_p(8, E33G_DRQCNT);             /* Set burst size to 8 */
 368     outb_p(0x20, E33G_DMAAH);   /* Put a valid addr in the GA DMA */
 369     outb_p(0x00, E33G_DMAAL);
 370     return;                     /* We always succeed */
 371 }
 372 
 373 /* Either use the shared memory (if enabled on the board) or put the packet
 374    out through the ASIC FIFO.  The latter is probably much slower. */
 375 static void
 376 el2_block_output(struct device *dev, int count,
     /* [previous][next][first][last][top][bottom][index][help] */
 377                  const unsigned char *buf, const start_page)
 378 {
 379     int i;                              /* Buffer index */
 380     int boguscount = 0;         /* timeout counter */
 381 
 382     /* This should really be set with during an open(). */
 383     outb(EGACFR_NORM, E33G_GACFR);      /* Enable RAM and interrupts. */
 384 
 385     if (dev->mem_start) {       /* Shared memory transfer */
 386         void *dest_addr = (void *)(dev->mem_start +
 387             ((start_page - ei_status.tx_start_page) << 8));
 388         memcpy(dest_addr, buf, count);
 389         if (ei_debug > 2  &&  memcmp(dest_addr, buf, count))
 390             printk("%s: 3c503 send_packet() bad memory copy @ %#5x.\n",
 391                    dev->name, (int) dest_addr);
 392         return;
 393     }
 394     /* No shared memory, put the packet out the slow way. */
 395     /* Set up then start the internal memory transfer to Tx Start Page */
 396     outb(0x00, E33G_DMAAL);
 397     outb_p(start_page, E33G_DMAAH);
 398     outb_p((ei_status.interface_num ? ECNTRL_AUI : ECNTRL_THIN ) | ECNTRL_OUTPUT
 399            | ECNTRL_START, E33G_CNTRL);
 400 
 401     /* This is the byte copy loop: it should probably be tuned for
 402        speed once everything is working.  I think it is possible
 403        to output 8 bytes between each check of the status bit. */
 404     for(i = 0; i < count; i++) {
 405         if (i % 8 == 0)
 406             while ((inb(E33G_STATUS) & ESTAT_DPRDY) == 0)
 407                 if (++boguscount > (i<<3) + 32) {
 408                     printk("%s: FIFO blocked in el2_block_output (at %d of %d, bc=%d).\n",
 409                            dev->name, i, count, boguscount);
 410                     return;
 411                 }
 412         outb(buf[i], E33G_FIFOH);
 413     }
 414     outb_p(ei_status.interface_num==0 ? ECNTRL_THIN : ECNTRL_AUI, E33G_CNTRL);
 415     return;
 416 }
 417 
 418 /* Returns the new ring pointer. */
 419 static int
 420 el2_block_input(struct device *dev, int count, char *buf, int ring_offset)
     /* [previous][next][first][last][top][bottom][index][help] */
 421 {
 422     int boguscount = 0;
 423     int end_of_ring = dev->rmem_end;
 424     unsigned int i;
 425 
 426     /* Maybe enable shared memory just be to be safe... nahh.*/
 427     if (dev->mem_start) {       /* Use the shared memory. */
 428         ring_offset -= (EL2SM_START_PG<<8);
 429         if (dev->mem_start + ring_offset + count > end_of_ring) {
 430             /* We must wrap the input move. */
 431             int semi_count = end_of_ring - (dev->mem_start + ring_offset);
 432             memcpy(buf, (char *)dev->mem_start + ring_offset, semi_count);
 433             count -= semi_count;
 434             memcpy(buf + semi_count, (char *)dev->rmem_start, count);
 435             return dev->rmem_start + count;
 436         }
 437         memcpy(buf, (char *)dev->mem_start + ring_offset, count);
 438         return ring_offset + count;
 439     }
 440     /* No shared memory, use programmed I/O. */
 441     outb(ring_offset & 0xff, E33G_DMAAL);
 442     outb_p((ring_offset >> 8) & 0xff, E33G_DMAAH);
 443     outb_p((ei_status.interface_num == 0 ? ECNTRL_THIN : ECNTRL_AUI) | ECNTRL_INPUT
 444            | ECNTRL_START, E33G_CNTRL);
 445 
 446     /* This is the byte copy loop: it should probably be tuned for
 447        speed once everything is working. */
 448     for(i = 0; i < count; i++) {
 449         if (i % 8 == 0)
 450             while ((inb(E33G_STATUS) & ESTAT_DPRDY) == 0)
 451                 if (++boguscount > (i<<3) + 32) {
 452                     printk("%s: FIFO blocked in el2_block_input() (at %d of %d, bc=%d).\n",
 453                            dev->name, i, count, boguscount);
 454                     boguscount = 0;
 455                     break;
 456                 }
 457         buf[i] = inb_p(E33G_FIFOH);
 458     }
 459     outb_p(ei_status.interface_num == 0 ? ECNTRL_THIN : ECNTRL_AUI, E33G_CNTRL);
 460     return 0;
 461 }
 462 
 463 /*
 464  * Local variables:
 465  *  version-control: t
 466  *  kept-new-versions: 5
 467  *  c-indent-level: 4
 468  * End:
 469  */

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