home *** CD-ROM | disk | FTP | other *** search
/ Plex 2 / Plex2.mdf / u_linux / e2100.c < prev    next >
C/C++ Source or Header  |  1994-05-28  |  11KB  |  363 lines

  1. From b63062.STUDENT.cwru.edu!root Sat Apr 16 15:19:29 1994
  2. /* e2100.c: A Cabletron E2100 series ethernet driver for linux. */
  3. /*
  4.     Written 1993 by Donald Becker.
  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 is a driver for the Cabletron E2100 series ethercards.
  11.  
  12.     The Author may be reached as becker@super.org or
  13.     C/O Supercomputing Research Ctr., 17100 Science Dr., Bowie MD 20715
  14.  
  15.     The E2100 series ethercard is a fairly generic shared memory 8390
  16.     implementation.  The only unusual aspect is the way the shared memory
  17.     registers are set: first you do an inb() in what is normally the
  18.     station address region, and the low four bits of next outb() is used
  19.     as the write value for that register.  Either someone wasn't too used
  20.     to dem bit en bites, or they were trying to obfusicate the programming
  21.     interface.
  22.  
  23.     There is an additional complication when setting the window on the packet
  24.     buffer.  You must first do a read into the packet buffer region with the
  25.     low 8 address bits the address setting the page for the start of the packet
  26.     buffer window, and then do the above operation.  See mem_on() for details.
  27.  
  28.     One bug on the chip is that even a hard reset won't disable the memory
  29.     window, usually resulting in a hung machine if mem_off() isn't called.
  30.     If this happens, you must power down the machine for about 30 seconds.
  31.  
  32.     Modified by
  33.         b63062.STUDENT.cwru.edu!root Apr 14 1994 for NET3
  34.  
  35.     THIS DRIVER IS NOT SUPPORTED BY DONALD BECKER.
  36.  
  37. */
  38.  
  39. static char *version =
  40.     "e2100.c:v0.01 11/21/93 Donald Becker (becker@super.org)\n";
  41.  
  42. #include <linux/config.h>
  43. #include <linux/kernel.h>
  44. #include <linux/sched.h>
  45. #include <linux/errno.h>
  46. #include <linux/string.h>
  47. #include <asm/io.h>
  48. #include <asm/system.h>
  49. #ifndef PRE_PL13
  50. #include <linux/ioport.h>       /* Delete if your kernel doesn't have it. */
  51. #endif
  52.  
  53. #include <linux/netdevice.h>
  54. #include "8390.h"
  55.  
  56. /* Compatibility definitions for earlier kernel versions. */
  57. #ifndef HAVE_PORTRESERVE
  58. #define check_region(ioaddr, size)              0
  59. #define snarf_region(ioaddr, size);             do ; while (0)
  60. #endif
  61. #ifndef HAVE_AUTOIRQ
  62. /* From auto_irq.c, in ioport.h for later versions. */
  63. extern void autoirq_setup(int waittime);
  64. extern int autoirq_report(int waittime);
  65. /* The map from IRQ number (as passed to the interrupt handler) to
  66.    'struct device'. */
  67. extern struct device *irq2dev_map[16];
  68. #endif
  69.  
  70. /* Offsets from the base_addr.
  71.    Read from the ASIC register, and the low 3(?) bits of the next outb() address
  72.    is used to set the cooresponding register. */
  73. #define E21_NIC_OFFSET  0       /* Offset to the 8390 NIC. */
  74. #define E21_ASIC        0x10
  75. #define E21_MEM_ENABLE    0x10
  76. #define  E21_MEM_ON        0x05    /* Enable memory in 16 bit mode. */
  77. #define  E21_MEM_ON_8    0x07    /* Enable memory in  8 bit mode. */
  78. #define E21_MEM_BASE    0x11    
  79. #define E21_IRQ_LOW        0x12    /* The low three bits of the IRQ number. */
  80. #define E21_IRQ_HIGH    0x14    /* The high IRQ bit, and ...  */
  81. #define  E21_ALT_IFPORT 0x02    /* Set to use the other (BNC,AUI) port. */
  82. #define  E21_BIG_MEM    0x04    /* Use a bigger (64K) buffer (we don't) */
  83. #define E21_SAPROM      0x10    /* Offset to station address data. */
  84. #define ETHERCARD_TOTAL_SIZE    0x20
  85.  
  86. extern inline void mem_on(short port, volatile char *mem_base,
  87.                           unsigned char start_page )
  88. {
  89.     /* This is a little weird: set the shared memory window by doing a
  90.        read.  The low address bits specify the starting page. */
  91.     mem_base[start_page];
  92.     inb(port + E21_MEM_ENABLE);
  93.     outb(E21_MEM_ON, port + E21_MEM_ENABLE + E21_MEM_ON);
  94. }
  95.  
  96. extern inline void mem_off(short port)
  97. {
  98.     inb(port + E21_MEM_ENABLE);
  99.     outb(0x00, port + E21_MEM_ENABLE);
  100. }
  101.  
  102. /* In other drivers I put the TX pages first, but the E2100 window circuitry
  103.    is designed to have a 4K Tx region last. The windowing circuitry wraps the
  104.    window at 0x2fff->0x0000 so that the packets at e.g. 0x2f00 in the RX ring
  105.    appear contiguously in the window. */
  106. #define E21_RX_START_PG    0x00    /* First page of RX buffer */
  107. #define E21_RX_STOP_PG     0x30    /* Last page +1 of RX ring */
  108. #define E21_BIG_RX_STOP_PG 0xF0    /* Last page +1 of RX ring */
  109. #define E21_TX_START_PG  E21_RX_STOP_PG    /* First page of TX buffer */
  110.  
  111. int e2100_probe(struct device *dev);
  112. int e21_probe1(struct device *dev, int ioaddr);
  113.  
  114. static int e21_open(struct device *dev);
  115. static void e21_reset_8390(struct device *dev);
  116. static int e21_block_input(struct device *dev, int count,
  117.                           char *buf, int ring_offset);
  118. static void e21_block_output(struct device *dev, int count,
  119.                             const unsigned char *buf, const start_page);
  120. static int e21_close(struct device *dev);
  121.  
  122.  
  123. /*  Probe for the E2100 series ethercards.  These cards have an 8390 at the
  124.     base address and the station address at both offset 0x10 and 0x18.  I read
  125.     the station address from offset 0x18 to avoid the dataport of NE2000
  126.     ethercards, and look for Ctron's unique ID (first three octets of the
  127.     station address).
  128.  */
  129.  
  130. int e2100_probe(struct device *dev)
  131. {
  132.     int *port, ports[] = {0x300, 0x280, 0x380, 0};
  133.     short base_addr = dev->base_addr;
  134.  
  135.     if (base_addr > 0x1ff)              /* Check a single specified location. */
  136.         return e21_probe1(dev, base_addr);
  137.     else if (base_addr > 0)             /* Don't probe at all. */
  138.         return ENXIO;
  139.  
  140.     for (port = &ports[0]; *port; port++) {
  141.         ushort ioaddr = *port;
  142.     int i;
  143.     
  144.         if (check_region(ioaddr, ETHERCARD_TOTAL_SIZE))
  145.             continue;
  146.  
  147. #ifdef testing_only
  148.     printk("%s: E21xx at %#3x: ", dev->name, ioaddr);
  149.     for (i = 0; i < 16; i++)
  150.         printk(" %02X", inb(ioaddr + E21_SAPROM + i));
  151.     printk("\n");
  152. #endif
  153.  
  154.  
  155.         if (inb(ioaddr + E21_SAPROM + 0) == 0x00
  156.             && inb(ioaddr + E21_SAPROM + 1) == 0x00
  157.             && inb(ioaddr + E21_SAPROM + 2) == 0x1d
  158.             && e21_probe1(dev, ioaddr) == 0)
  159.             return 0;
  160.     }
  161.     return -ENODEV;
  162. }
  163.  
  164. int e21_probe1(struct device *dev, int ioaddr)
  165. {
  166.     int i, status;
  167.     unsigned char *station_addr = dev->dev_addr;
  168.  
  169.     /* We've already checked the station address prefix, now verify by making
  170.        certain that there is a 8390 at the expected location. */
  171.     outb(E8390_NODMA + E8390_STOP, ioaddr);
  172.     SLOW_DOWN_IO;
  173.     status = inb(ioaddr);
  174.     if (status != 0x21 && status != 0x23)
  175.         return -ENODEV;
  176.  
  177. #ifdef testing_only
  178.     printk("%s: E21xx at %#3x (PAXI backwards): ", dev->name, ioaddr);
  179.     for (i = 0; i < 16; i++)
  180.         printk(" %02X", inb(ioaddr + 0x1f - i));
  181.     printk("\n");
  182. #endif
  183.  
  184.     /* Read the station address PROM.  */
  185.     for (i = 0; i < 6; i++)
  186.         station_addr[i] = inb(ioaddr + E21_SAPROM + i);
  187.  
  188.     /* Grab the region so we can find another board if needed . */
  189.     snarf_region(ioaddr, ETHERCARD_TOTAL_SIZE);
  190.  
  191.     printk("%s: E21xx at %#3x, ", dev->name, ioaddr);
  192.     for (i = 0; i < 6; i++)
  193.         printk(" %02X", station_addr[i]);
  194.  
  195.     if (dev->irq < 2) {
  196.         int irqlist[] = {9,15,11,10,12,5,3,4,0}, i;
  197.         for (i = 0; irqlist[i] ; i++)
  198.             if (request_irq (irqlist[i], NULL) != -EBUSY) {
  199.                 dev->irq = irqlist[i];
  200.                 break;
  201.             }
  202.     } else if (dev->irq == 2)     /* Fixup bogosity: IRQ2 is really IRQ9 */
  203.         dev->irq = 9;
  204.  
  205.     /* Snarf the interrupt now. */
  206.     if (irqaction (dev->irq, &ei_sigaction)) {
  207.         printk (" unable to get IRQ %d.\n", dev->irq);
  208.         return -EBUSY;
  209.     }
  210.  
  211.     /* The 8390 is at the base address. */
  212.     dev->base_addr = ioaddr;
  213.  
  214.     ethdev_init(dev);
  215.  
  216.     ei_status.name = "E2100";
  217.     ei_status.word16 = 1;
  218.     ei_status.tx_start_page = E21_TX_START_PG;
  219.     ei_status.rx_start_page = E21_RX_START_PG;
  220.     ei_status.stop_page = E21_RX_STOP_PG;
  221.  
  222.     /* Check the media port used.  The port can be passed in on the
  223.        low mem_end bits. */
  224.     if (dev->mem_end & 15)
  225.         dev->if_port = dev->mem_end & 7;
  226.     else {
  227.         dev->if_port = 0;
  228.         inb_p(ioaddr + E21_IRQ_HIGH);     /* Select if_port detect. */
  229.         for(i = 0; i < 6; i++)
  230.             if (station_addr[i] != inb(ioaddr + E21_SAPROM))
  231.                 dev->if_port = 1;
  232.     }
  233.  
  234.      dev->if_port = 0;
  235.      
  236.     /* Never map in the E21 shared memory unless you are actively using it.
  237.        Also, the shared memory has effective only one setting -- spread all
  238.        over the 128K region! */
  239.     if (dev->mem_start == 0)
  240.         dev->mem_start = 0xd0000;
  241.     
  242. #ifdef notdef
  243.     /* These values are unused.  The E2100 has a 2K window into the packet
  244.        buffer.  The window can be set to start on any page boundary. */
  245.     dev->rmem_start = dev->mem_start + TX_PAGES*256;
  246.     dev->mem_end = dev->rmem_end = dev->mem_start + 2*1024;
  247. #endif
  248.  
  249.     printk(" IRQ %d, %s interface,  memory at %#x-%#x.\n", dev->irq,
  250.            dev->if_port ? "secondary" : "primary", dev->mem_start,
  251.            dev->mem_start + 2*1024 - 1);
  252.  
  253.     if (ei_debug > 0)
  254.         printk(version);
  255.  
  256.     ei_status.reset_8390 = &e21_reset_8390;
  257.     ei_status.block_input = &e21_block_input;
  258.     ei_status.block_output = &e21_block_output;
  259.     dev->open = &e21_open;
  260.     dev->stop = &e21_close;
  261.     NS8390_init(dev, 0);
  262.  
  263.     return 0;
  264. }
  265.  
  266. static int
  267. e21_open(struct device *dev)
  268. {
  269.     short ioaddr = dev->base_addr;
  270.  
  271.     /* Set the interrupt line and memory base on the hardware. */
  272.     inb_p(ioaddr + E21_IRQ_LOW);
  273.     outb_p(0, ioaddr + E21_ASIC + (dev->irq & 7));
  274.     inb_p(ioaddr + E21_IRQ_HIGH);             /* High IRQ bit, and if_port. */
  275.     outb_p(0, ioaddr + E21_ASIC + (dev->irq > 7 ? 1:0)
  276.            + (dev->if_port ? E21_ALT_IFPORT : 0));
  277.     inb_p(ioaddr + E21_MEM_BASE);
  278.     outb_p(0, ioaddr + E21_ASIC + ((dev->mem_start >> 17) & 7));
  279.  
  280.     return ei_open(dev);
  281. }
  282.  
  283. static void
  284. e21_reset_8390(struct device *dev)
  285. {
  286.     short ioaddr = dev->base_addr;
  287.  
  288.     outb(0x01, ioaddr);
  289.     if (ei_debug > 1) printk("resetting the E2180x3 t=%d...", jiffies);
  290.     ei_status.txing = 0;
  291.  
  292.     /* Set up the ASIC registers, just in case something changed them. */
  293.  
  294.     if (ei_debug > 1) printk("reset done\n");
  295.     return;
  296. }
  297.  
  298. /* Block input and output are easy on shared memory ethercards.  The E21xx makes
  299.    block_input() especially easy by wrapping the top ring buffer to the bottom
  300.    automatically. */
  301. static int
  302. e21_block_input(struct device *dev, int count, char *buf, int ring_offset)
  303. {
  304.     short ioaddr = dev->base_addr;
  305.     char *shared_mem = (char *)dev->mem_start;
  306.     int start_page = (ring_offset>>8);
  307.  
  308.     mem_on(ioaddr, shared_mem, start_page);
  309.  
  310.     /* We'll always get a 4 byte header read first. */
  311.     if (count == 4)
  312.         ((int*)buf)[0] = ((int*)shared_mem)[0];
  313.     else
  314.         memcpy(buf, shared_mem + (ring_offset & 0xff), count);
  315.  
  316.     /* Turn off memory access: we would need to reprogram the window anyway. */
  317.     mem_off(ioaddr);
  318.  
  319.     return 0;
  320. }
  321.  
  322. static void
  323. e21_block_output(struct device *dev, int count, const unsigned char *buf,
  324.                 int start_page)
  325. {
  326.     short ioaddr = dev->base_addr;
  327.     volatile char *shared_mem = (char *)dev->mem_start;
  328.  
  329.     /* Set the shared memory window start by doing a read, with the low address
  330.        bits specifing the starting page. */
  331.     *(shared_mem + start_page);
  332.     mem_on(ioaddr, shared_mem, start_page);
  333.  
  334.     memcpy((char*)shared_mem, buf, count);
  335.     mem_off(ioaddr);
  336. }
  337.  
  338. static int
  339. e21_close(struct device *dev)
  340. {
  341.     short ioaddr = dev->base_addr;
  342.  
  343.     if (ei_debug > 1)
  344.         printk("%s: Shutting down ethercard.\n", dev->name);
  345.     NS8390_init(dev, 0);
  346.  
  347.     mem_off(ioaddr);
  348.  
  349.     return 0;
  350. }
  351.  
  352.  
  353. /*
  354.  * Local variables:
  355.  *  compile-command: "gcc -D__KERNEL__ -I/usr/src/linux/net/inet -Wall -Wstrict-prototypes -O6 -m486 -c e2100.c"
  356.  *  version-control: t
  357.  *  tab-width: 4
  358.  *  kept-new-versions: 5
  359.  * End:
  360.  */
  361.  
  362.  
  363.