home *** CD-ROM | disk | FTP | other *** search
/ Il CD di internet / CD.iso / SOURCE / KERNEL-S / V1.0 / LINUX-1.0 / LINUX-1 / linux / drivers / net / 3c503.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-03-06  |  13.3 KB  |  429 lines

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