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

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