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

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