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
  10. init_module
  11. cleanup_module

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

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