home *** CD-ROM | disk | FTP | other *** search
/ Il CD di internet / CD.iso / SOURCE / KERNEL-S / V1.2 / LINUX-1.2 / LINUX-1 / linux / drivers / net / 3c503.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-02-27  |  14.6 KB  |  469 lines

  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)
  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)
  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)
  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)
  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)
  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.  
  321.     return 0;
  322. }
  323.  
  324. /* This is called whenever we have a unrecoverable failure:
  325.        transmit timeout
  326.        Bad ring buffer packet header
  327.  */
  328. static void
  329. el2_reset_8390(struct device *dev)
  330. {
  331.     if (ei_debug > 1) {
  332.     printk("%s: Resetting the 3c503 board...", dev->name);
  333.     printk("%#lx=%#02x %#lx=%#02x %#lx=%#02x...", E33G_IDCFR, inb(E33G_IDCFR),
  334.            E33G_CNTRL, inb(E33G_CNTRL), E33G_GACFR, inb(E33G_GACFR));
  335.     }
  336.     outb_p(ECNTRL_RESET|ECNTRL_THIN, E33G_CNTRL);
  337.     ei_status.txing = 0;
  338.     outb_p(ei_status.interface_num==0 ? ECNTRL_THIN : ECNTRL_AUI, E33G_CNTRL);
  339.     el2_init_card(dev);
  340.     if (ei_debug > 1) printk("done\n");
  341. }
  342.  
  343. /* Initialize the 3c503 GA registers after a reset. */
  344. static void
  345. el2_init_card(struct device *dev)
  346. {
  347.     /* Unmap the station PROM and select the DIX or BNC connector. */
  348.     outb_p(ei_status.interface_num==0 ? ECNTRL_THIN : ECNTRL_AUI, E33G_CNTRL);
  349.  
  350.     /* Set ASIC copy of rx's first and last+1 buffer pages */
  351.     /* These must be the same as in the 8390. */
  352.     outb(ei_status.rx_start_page, E33G_STARTPG);
  353.     outb(ei_status.stop_page,  E33G_STOPPG);
  354.  
  355.     /* Point the vector pointer registers somewhere ?harmless?. */
  356.     outb(0xff, E33G_VP2);    /* Point at the ROM restart location 0xffff0 */
  357.     outb(0xff, E33G_VP1);
  358.     outb(0x00, E33G_VP0);
  359.     /* Turn off all interrupts until we're opened. */
  360.     outb_p(0x00,  dev->base_addr + EN0_IMR);
  361.     /* Enable IRQs iff started. */
  362.     outb(EGACFR_NORM, E33G_GACFR);
  363.  
  364.     /* Set the interrupt line. */
  365.     outb_p((0x04 << (dev->irq == 9 ? 2 : dev->irq)), E33G_IDCFR);
  366.     outb_p(8, E33G_DRQCNT);        /* Set burst size to 8 */
  367.     outb_p(0x20, E33G_DMAAH);    /* Put a valid addr in the GA DMA */
  368.     outb_p(0x00, E33G_DMAAL);
  369.     return;            /* We always succeed */
  370. }
  371.  
  372. /* Either use the shared memory (if enabled on the board) or put the packet
  373.    out through the ASIC FIFO.  The latter is probably much slower. */
  374. static void
  375. el2_block_output(struct device *dev, int count,
  376.          const unsigned char *buf, const start_page)
  377. {
  378.     int i;                /* Buffer index */
  379.     int boguscount = 0;        /* timeout counter */
  380.  
  381.     /* This should really be set with during an open(). */
  382.     outb(EGACFR_NORM, E33G_GACFR);    /* Enable RAM and interrupts. */
  383.  
  384.     if (dev->mem_start) {    /* Shared memory transfer */
  385.     void *dest_addr = (void *)(dev->mem_start +
  386.         ((start_page - ei_status.tx_start_page) << 8));
  387.     memcpy(dest_addr, buf, count);
  388.     if (ei_debug > 2  &&  memcmp(dest_addr, buf, count))
  389.         printk("%s: 3c503 send_packet() bad memory copy @ %#5x.\n",
  390.            dev->name, (int) dest_addr);
  391.     return;
  392.     }
  393.     /* No shared memory, put the packet out the slow way. */
  394.     /* Set up then start the internal memory transfer to Tx Start Page */
  395.     outb(0x00, E33G_DMAAL);
  396.     outb_p(start_page, E33G_DMAAH);
  397.     outb_p((ei_status.interface_num ? ECNTRL_AUI : ECNTRL_THIN ) | ECNTRL_OUTPUT
  398.        | ECNTRL_START, E33G_CNTRL);
  399.  
  400.     /* This is the byte copy loop: it should probably be tuned for
  401.        speed once everything is working.  I think it is possible
  402.        to output 8 bytes between each check of the status bit. */
  403.     for(i = 0; i < count; i++) {
  404.     if (i % 8 == 0)
  405.         while ((inb(E33G_STATUS) & ESTAT_DPRDY) == 0)
  406.         if (++boguscount > (i<<3) + 32) {
  407.             printk("%s: FIFO blocked in el2_block_output (at %d of %d, bc=%d).\n",
  408.                dev->name, i, count, boguscount);
  409.             return;
  410.         }
  411.     outb(buf[i], E33G_FIFOH);
  412.     }
  413.     outb_p(ei_status.interface_num==0 ? ECNTRL_THIN : ECNTRL_AUI, E33G_CNTRL);
  414.     return;
  415. }
  416.  
  417. /* Returns the new ring pointer. */
  418. static int
  419. el2_block_input(struct device *dev, int count, char *buf, int ring_offset)
  420. {
  421.     int boguscount = 0;
  422.     int end_of_ring = dev->rmem_end;
  423.     unsigned int i;
  424.  
  425.     /* Maybe enable shared memory just be to be safe... nahh.*/
  426.     if (dev->mem_start) {    /* Use the shared memory. */
  427.     ring_offset -= (EL2SM_START_PG<<8);
  428.     if (dev->mem_start + ring_offset + count > end_of_ring) {
  429.         /* We must wrap the input move. */
  430.         int semi_count = end_of_ring - (dev->mem_start + ring_offset);
  431.         memcpy(buf, (char *)dev->mem_start + ring_offset, semi_count);
  432.         count -= semi_count;
  433.         memcpy(buf + semi_count, (char *)dev->rmem_start, count);
  434.         return dev->rmem_start + count;
  435.     }
  436.     memcpy(buf, (char *)dev->mem_start + ring_offset, count);
  437.     return ring_offset + count;
  438.     }
  439.     /* No shared memory, use programmed I/O. */
  440.     outb(ring_offset & 0xff, E33G_DMAAL);
  441.     outb_p((ring_offset >> 8) & 0xff, E33G_DMAAH);
  442.     outb_p((ei_status.interface_num == 0 ? ECNTRL_THIN : ECNTRL_AUI) | ECNTRL_INPUT
  443.        | ECNTRL_START, E33G_CNTRL);
  444.  
  445.     /* This is the byte copy loop: it should probably be tuned for
  446.        speed once everything is working. */
  447.     for(i = 0; i < count; i++) {
  448.     if (i % 8 == 0)
  449.         while ((inb(E33G_STATUS) & ESTAT_DPRDY) == 0)
  450.         if (++boguscount > (i<<3) + 32) {
  451.             printk("%s: FIFO blocked in el2_block_input() (at %d of %d, bc=%d).\n",
  452.                dev->name, i, count, boguscount);
  453.             boguscount = 0;
  454.             break;
  455.         }
  456.     buf[i] = inb_p(E33G_FIFOH);
  457.     }
  458.     outb_p(ei_status.interface_num == 0 ? ECNTRL_THIN : ECNTRL_AUI, E33G_CNTRL);
  459.     return 0;
  460. }
  461.  
  462. /*
  463.  * Local variables:
  464.  *  version-control: t
  465.  *  kept-new-versions: 5
  466.  *  c-indent-level: 4
  467.  * End:
  468.  */
  469.