ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / drivers / net / e2100.c
1 /* e2100.c: A Cabletron E2100 series ethernet driver for linux. */
2 /*
3         Written 1993-1994 by Donald Becker.
4
5         Copyright 1994 by Donald Becker.
6         Copyright 1993 United States Government as represented by the
7         Director, National Security Agency.  This software may be used and
8         distributed according to the terms of the GNU General Public License,
9         incorporated herein by reference.
10
11         This is a driver for the Cabletron E2100 series ethercards.
12
13         The Author may be reached as becker@scyld.com, or C/O
14         Scyld Computing Corporation
15         410 Severn Ave., Suite 210
16         Annapolis MD 21403
17
18         The E2100 series ethercard is a fairly generic shared memory 8390
19         implementation.  The only unusual aspect is the way the shared memory
20         registers are set: first you do an inb() in what is normally the
21         station address region, and the low three bits of next outb() *address*
22         is used as the write value for that register.  Either someone wasn't
23         too used to dem bit en bites, or they were trying to obfuscate the
24         programming interface.
25
26         There is an additional complication when setting the window on the packet
27         buffer.  You must first do a read into the packet buffer region with the
28         low 8 address bits the address setting the page for the start of the packet
29         buffer window, and then do the above operation.  See mem_on() for details.
30
31         One bug on the chip is that even a hard reset won't disable the memory
32         window, usually resulting in a hung machine if mem_off() isn't called.
33         If this happens, you must power down the machine for about 30 seconds.
34 */
35
36 static const char version[] =
37         "e2100.c:v1.01 7/21/94 Donald Becker (becker@cesdis.gsfc.nasa.gov)\n";
38
39 #include <linux/module.h>
40 #include <linux/kernel.h>
41 #include <linux/errno.h>
42 #include <linux/string.h>
43 #include <linux/ioport.h>
44 #include <linux/netdevice.h>
45 #include <linux/etherdevice.h>
46 #include <linux/init.h>
47 #include <linux/delay.h>
48
49 #include <asm/io.h>
50 #include <asm/system.h>
51
52 #include "8390.h"
53
54 static int e21_probe_list[] = {0x300, 0x280, 0x380, 0x220, 0};
55
56 /* Offsets from the base_addr.
57    Read from the ASIC register, and the low three bits of the next outb()
58    address is used to set the corresponding register. */
59 #define E21_NIC_OFFSET  0               /* Offset to the 8390 NIC. */
60 #define E21_ASIC                0x10
61 #define E21_MEM_ENABLE  0x10
62 #define  E21_MEM_ON             0x05    /* Enable memory in 16 bit mode. */
63 #define  E21_MEM_ON_8   0x07    /* Enable memory in  8 bit mode. */
64 #define E21_MEM_BASE    0x11
65 #define E21_IRQ_LOW             0x12    /* The low three bits of the IRQ number. */
66 #define E21_IRQ_HIGH    0x14    /* The high IRQ bit and media select ...  */
67 #define E21_MEDIA               0x14    /* (alias). */
68 #define  E21_ALT_IFPORT 0x02    /* Set to use the other (BNC,AUI) port. */
69 #define  E21_BIG_MEM    0x04    /* Use a bigger (64K) buffer (we don't) */
70 #define E21_SAPROM              0x10    /* Offset to station address data. */
71 #define E21_IO_EXTENT    0x20
72
73 static inline void mem_on(short port, volatile char *mem_base,
74                                                   unsigned char start_page )
75 {
76         /* This is a little weird: set the shared memory window by doing a
77            read.  The low address bits specify the starting page. */
78         readb(mem_base+start_page);
79         inb(port + E21_MEM_ENABLE);
80         outb(E21_MEM_ON, port + E21_MEM_ENABLE + E21_MEM_ON);
81 }
82
83 static inline void mem_off(short port)
84 {
85         inb(port + E21_MEM_ENABLE);
86         outb(0x00, port + E21_MEM_ENABLE);
87 }
88
89 /* In other drivers I put the TX pages first, but the E2100 window circuitry
90    is designed to have a 4K Tx region last. The windowing circuitry wraps the
91    window at 0x2fff->0x0000 so that the packets at e.g. 0x2f00 in the RX ring
92    appear contiguously in the window. */
93 #define E21_RX_START_PG         0x00    /* First page of RX buffer */
94 #define E21_RX_STOP_PG          0x30    /* Last page +1 of RX ring */
95 #define E21_BIG_RX_STOP_PG      0xF0    /* Last page +1 of RX ring */
96 #define E21_TX_START_PG         E21_RX_STOP_PG  /* First page of TX buffer */
97
98 static int e21_probe1(struct net_device *dev, int ioaddr);
99
100 static int e21_open(struct net_device *dev);
101 static void e21_reset_8390(struct net_device *dev);
102 static void e21_block_input(struct net_device *dev, int count,
103                                                    struct sk_buff *skb, int ring_offset);
104 static void e21_block_output(struct net_device *dev, int count,
105                                                          const unsigned char *buf, int start_page);
106 static void e21_get_8390_hdr(struct net_device *dev, struct e8390_pkt_hdr *hdr,
107                                                         int ring_page);
108
109 static int e21_close(struct net_device *dev);
110
111 \f
112 /*  Probe for the E2100 series ethercards.  These cards have an 8390 at the
113         base address and the station address at both offset 0x10 and 0x18.  I read
114         the station address from offset 0x18 to avoid the dataport of NE2000
115         ethercards, and look for Ctron's unique ID (first three octets of the
116         station address).
117  */
118
119 static int  __init do_e2100_probe(struct net_device *dev)
120 {
121         int *port;
122         int base_addr = dev->base_addr;
123         int irq = dev->irq;
124
125         SET_MODULE_OWNER(dev);
126
127         if (base_addr > 0x1ff)          /* Check a single specified location. */
128                 return e21_probe1(dev, base_addr);
129         else if (base_addr != 0)        /* Don't probe at all. */
130                 return -ENXIO;
131
132         for (port = e21_probe_list; *port; port++) {
133                 dev->irq = irq;
134                 if (e21_probe1(dev, *port) == 0)
135                         return 0;
136         }
137
138         return -ENODEV;
139 }
140
141 static void cleanup_card(struct net_device *dev)
142 {
143         /* NB: e21_close() handles free_irq */
144         release_region(dev->base_addr, E21_IO_EXTENT);
145 }
146
147 struct net_device * __init e2100_probe(int unit)
148 {
149         struct net_device *dev = alloc_ei_netdev();
150         int err;
151
152         if (!dev)
153                 return ERR_PTR(-ENOMEM);
154
155         sprintf(dev->name, "eth%d", unit);
156         netdev_boot_setup_check(dev);
157
158         err = do_e2100_probe(dev);
159         if (err)
160                 goto out;
161         err = register_netdev(dev);
162         if (err)
163                 goto out1;
164         return dev;
165 out1:
166         cleanup_card(dev);
167 out:
168         free_netdev(dev);
169         return ERR_PTR(err);
170 }
171
172 static int __init e21_probe1(struct net_device *dev, int ioaddr)
173 {
174         int i, status, retval;
175         unsigned char *station_addr = dev->dev_addr;
176         static unsigned version_printed;
177
178         if (!request_region(ioaddr, E21_IO_EXTENT, dev->name))
179                 return -EBUSY;
180
181         /* First check the station address for the Ctron prefix. */
182         if (inb(ioaddr + E21_SAPROM + 0) != 0x00
183                 || inb(ioaddr + E21_SAPROM + 1) != 0x00
184                 || inb(ioaddr + E21_SAPROM + 2) != 0x1d) {
185                 retval = -ENODEV;
186                 goto out;
187         }
188
189         /* Verify by making certain that there is a 8390 at there. */
190         outb(E8390_NODMA + E8390_STOP, ioaddr);
191         udelay(1);      /* we want to delay one I/O cycle - which is 2MHz */
192         status = inb(ioaddr);
193         if (status != 0x21 && status != 0x23) {
194                 retval = -ENODEV;
195                 goto out;
196         }
197
198         /* Read the station address PROM.  */
199         for (i = 0; i < 6; i++)
200                 station_addr[i] = inb(ioaddr + E21_SAPROM + i);
201
202         inb(ioaddr + E21_MEDIA);                /* Point to media selection. */
203         outb(0, ioaddr + E21_ASIC);     /* and disable the secondary interface. */
204
205         if (ei_debug  &&  version_printed++ == 0)
206                 printk(version);
207
208         for (i = 0; i < 6; i++)
209                 printk(" %02X", station_addr[i]);
210
211         if (dev->irq < 2) {
212                 int irqlist[] = {15,11,10,12,5,9,3,4}, i;
213                 for (i = 0; i < 8; i++)
214                         if (request_irq (irqlist[i], NULL, 0, "bogus", NULL) != -EBUSY) {
215                                 dev->irq = irqlist[i];
216                                 break;
217                         }
218                 if (i >= 8) {
219                         printk(" unable to get IRQ %d.\n", dev->irq);
220                         retval = -EAGAIN;
221                         goto out;
222                 }
223         } else if (dev->irq == 2)       /* Fixup luser bogosity: IRQ2 is really IRQ9 */
224                 dev->irq = 9;
225
226         /* The 8390 is at the base address. */
227         dev->base_addr = ioaddr;
228
229         ei_status.name = "E2100";
230         ei_status.word16 = 1;
231         ei_status.tx_start_page = E21_TX_START_PG;
232         ei_status.rx_start_page = E21_RX_START_PG;
233         ei_status.stop_page = E21_RX_STOP_PG;
234         ei_status.saved_irq = dev->irq;
235
236         /* Check the media port used.  The port can be passed in on the
237            low mem_end bits. */
238         if (dev->mem_end & 15)
239                 dev->if_port = dev->mem_end & 7;
240         else {
241                 dev->if_port = 0;
242                 inb(ioaddr + E21_MEDIA);        /* Turn automatic media detection on. */
243                 for(i = 0; i < 6; i++)
244                         if (station_addr[i] != inb(ioaddr + E21_SAPROM + 8 + i)) {
245                                 dev->if_port = 1;
246                                 break;
247                         }
248         }
249
250         /* Never map in the E21 shared memory unless you are actively using it.
251            Also, the shared memory has effective only one setting -- spread all
252            over the 128K region! */
253         if (dev->mem_start == 0)
254                 dev->mem_start = 0xd0000;
255
256 #ifdef notdef
257         /* These values are unused.  The E2100 has a 2K window into the packet
258            buffer.  The window can be set to start on any page boundary. */
259         ei_status.rmem_start = dev->mem_start + TX_PAGES*256;
260         dev->mem_end = ei_status.rmem_end = dev->mem_start + 2*1024;
261 #endif
262
263         printk(", IRQ %d, %s media, memory @ %#lx.\n", dev->irq,
264                    dev->if_port ? "secondary" : "primary", dev->mem_start);
265
266         ei_status.reset_8390 = &e21_reset_8390;
267         ei_status.block_input = &e21_block_input;
268         ei_status.block_output = &e21_block_output;
269         ei_status.get_8390_hdr = &e21_get_8390_hdr;
270         dev->open = &e21_open;
271         dev->stop = &e21_close;
272 #ifdef CONFIG_NET_POLL_CONTROLLER
273         dev->poll_controller = ei_poll;
274 #endif
275         NS8390_init(dev, 0);
276
277         return 0;
278 out:
279         release_region(ioaddr, E21_IO_EXTENT);
280         return retval;
281 }
282
283 static int
284 e21_open(struct net_device *dev)
285 {
286         short ioaddr = dev->base_addr;
287         int retval;
288
289         if ((retval = request_irq(dev->irq, ei_interrupt, 0, dev->name, dev)))
290                 return retval;
291
292         /* Set the interrupt line and memory base on the hardware. */
293         inb(ioaddr + E21_IRQ_LOW);
294         outb(0, ioaddr + E21_ASIC + (dev->irq & 7));
295         inb(ioaddr + E21_IRQ_HIGH);                     /* High IRQ bit, and if_port. */
296         outb(0, ioaddr + E21_ASIC + (dev->irq > 7 ? 1:0)
297                    + (dev->if_port ? E21_ALT_IFPORT : 0));
298         inb(ioaddr + E21_MEM_BASE);
299         outb(0, ioaddr + E21_ASIC + ((dev->mem_start >> 17) & 7));
300
301         ei_open(dev);
302         return 0;
303 }
304
305 static void
306 e21_reset_8390(struct net_device *dev)
307 {
308         short ioaddr = dev->base_addr;
309
310         outb(0x01, ioaddr);
311         if (ei_debug > 1) printk("resetting the E2180x3 t=%ld...", jiffies);
312         ei_status.txing = 0;
313
314         /* Set up the ASIC registers, just in case something changed them. */
315
316         if (ei_debug > 1) printk("reset done\n");
317         return;
318 }
319
320 /* Grab the 8390 specific header. We put the 2k window so the header page
321    appears at the start of the shared memory. */
322
323 static void
324 e21_get_8390_hdr(struct net_device *dev, struct e8390_pkt_hdr *hdr, int ring_page)
325 {
326
327         short ioaddr = dev->base_addr;
328         char *shared_mem = (char *)dev->mem_start;
329
330         mem_on(ioaddr, shared_mem, ring_page);
331
332 #ifdef notdef
333         /* Officially this is what we are doing, but the readl() is faster */
334         memcpy_fromio(hdr, shared_mem, sizeof(struct e8390_pkt_hdr));
335 #else
336         ((unsigned int*)hdr)[0] = readl(shared_mem);
337 #endif
338
339         /* Turn off memory access: we would need to reprogram the window anyway. */
340         mem_off(ioaddr);
341
342 }
343
344 /*  Block input and output are easy on shared memory ethercards.
345         The E21xx makes block_input() especially easy by wrapping the top
346         ring buffer to the bottom automatically. */
347 static void
348 e21_block_input(struct net_device *dev, int count, struct sk_buff *skb, int ring_offset)
349 {
350         short ioaddr = dev->base_addr;
351         char *shared_mem = (char *)dev->mem_start;
352
353         mem_on(ioaddr, shared_mem, (ring_offset>>8));
354
355         /* Packet is always in one chunk -- we can copy + cksum. */
356         eth_io_copy_and_sum(skb, dev->mem_start + (ring_offset & 0xff), count, 0);
357
358         mem_off(ioaddr);
359 }
360
361 static void
362 e21_block_output(struct net_device *dev, int count, const unsigned char *buf,
363                                  int start_page)
364 {
365         short ioaddr = dev->base_addr;
366         volatile char *shared_mem = (char *)dev->mem_start;
367
368         /* Set the shared memory window start by doing a read, with the low address
369            bits specifying the starting page. */
370         readb(shared_mem + start_page);
371         mem_on(ioaddr, shared_mem, start_page);
372
373         memcpy_toio(shared_mem, buf, count);
374         mem_off(ioaddr);
375 }
376
377 static int
378 e21_close(struct net_device *dev)
379 {
380         short ioaddr = dev->base_addr;
381
382         if (ei_debug > 1)
383                 printk("%s: Shutting down ethercard.\n", dev->name);
384
385         free_irq(dev->irq, dev);
386         dev->irq = ei_status.saved_irq;
387
388         /* Shut off the interrupt line and secondary interface. */
389         inb(ioaddr + E21_IRQ_LOW);
390         outb(0, ioaddr + E21_ASIC);
391         inb(ioaddr + E21_IRQ_HIGH);                     /* High IRQ bit, and if_port. */
392         outb(0, ioaddr + E21_ASIC);
393
394         ei_close(dev);
395
396         /* Double-check that the memory has been turned off, because really
397            really bad things happen if it isn't. */
398         mem_off(ioaddr);
399
400         return 0;
401 }
402
403 \f
404 #ifdef MODULE
405 #define MAX_E21_CARDS   4       /* Max number of E21 cards per module */
406 static struct net_device *dev_e21[MAX_E21_CARDS];
407 static int io[MAX_E21_CARDS];
408 static int irq[MAX_E21_CARDS];
409 static int mem[MAX_E21_CARDS];
410 static int xcvr[MAX_E21_CARDS];         /* choose int. or ext. xcvr */
411
412 MODULE_PARM(io, "1-" __MODULE_STRING(MAX_E21_CARDS) "i");
413 MODULE_PARM(irq, "1-" __MODULE_STRING(MAX_E21_CARDS) "i");
414 MODULE_PARM(mem, "1-" __MODULE_STRING(MAX_E21_CARDS) "i");
415 MODULE_PARM(xcvr, "1-" __MODULE_STRING(MAX_E21_CARDS) "i");
416 MODULE_PARM_DESC(io, "I/O base address(es)");
417 MODULE_PARM_DESC(irq, "IRQ number(s)");
418 MODULE_PARM_DESC(mem, " memory base address(es)");
419 MODULE_PARM_DESC(xcvr, "transceiver(s) (0=internal, 1=external)");
420 MODULE_DESCRIPTION("Cabletron E2100 ISA ethernet driver");
421 MODULE_LICENSE("GPL");
422
423 /* This is set up so that only a single autoprobe takes place per call.
424 ISA device autoprobes on a running machine are not recommended. */
425 int
426 init_module(void)
427 {
428         struct net_device *dev;
429         int this_dev, found = 0;
430
431         for (this_dev = 0; this_dev < MAX_E21_CARDS; this_dev++) {
432                 if (io[this_dev] == 0)  {
433                         if (this_dev != 0) break; /* only autoprobe 1st one */
434                         printk(KERN_NOTICE "e2100.c: Presently autoprobing (not recommended) for a single card.\n");
435                 }
436                 dev = alloc_ei_netdev();
437                 if (!dev)
438                         break;
439                 dev->irq = irq[this_dev];
440                 dev->base_addr = io[this_dev];
441                 dev->mem_start = mem[this_dev];
442                 dev->mem_end = xcvr[this_dev];  /* low 4bits = xcvr sel. */
443                 if (do_e2100_probe(dev) == 0) {
444                         if (register_netdev(dev) == 0) {
445                                 dev_e21[found++] = dev;
446                                 continue;
447                         }
448                         cleanup_card(dev);
449                 }
450                 free_netdev(dev);
451                 printk(KERN_WARNING "e2100.c: No E2100 card found (i/o = 0x%x).\n", io[this_dev]);
452                 break;
453         }
454         if (found)
455                 return 0;
456         return -ENXIO;
457 }
458
459 void
460 cleanup_module(void)
461 {
462         int this_dev;
463
464         for (this_dev = 0; this_dev < MAX_E21_CARDS; this_dev++) {
465                 struct net_device *dev = dev_e21[this_dev];
466                 if (dev) {
467                         unregister_netdev(dev);
468                         cleanup_card(dev);
469                         free_netdev(dev);
470                 }
471         }
472 }
473 #endif /* MODULE */