Linux-2.6.12-rc2
[linux-flexiantxendom0-natty.git] / drivers / net / wd.c
1 /* wd.c: A WD80x3 ethernet driver for linux. */
2 /*
3         Written 1993-94 by Donald Becker.
4
5         Copyright 1993 United States Government as represented by the
6         Director, National Security Agency.
7
8         This software may be used and distributed according to the terms
9         of the GNU General Public License, incorporated herein by reference.
10
11         The author may be reached as becker@scyld.com, or C/O
12         Scyld Computing Corporation
13         410 Severn Ave., Suite 210
14         Annapolis MD 21403
15
16         This is a driver for WD8003 and WD8013 "compatible" ethercards.
17
18         Thanks to Russ Nelson (nelson@crnwyr.com) for loaning me a WD8013.
19
20         Changelog:
21
22         Paul Gortmaker  : multiple card support for module users, support
23                           for non-standard memory sizes.
24
25
26 */
27
28 static const char version[] =
29         "wd.c:v1.10 9/23/94 Donald Becker (becker@cesdis.gsfc.nasa.gov)\n";
30
31 #include <linux/module.h>
32 #include <linux/kernel.h>
33 #include <linux/errno.h>
34 #include <linux/string.h>
35 #include <linux/init.h>
36 #include <linux/interrupt.h>
37 #include <linux/delay.h>
38 #include <linux/netdevice.h>
39 #include <linux/etherdevice.h>
40
41 #include <asm/io.h>
42 #include <asm/system.h>
43
44 #include "8390.h"
45
46 #define DRV_NAME "wd"
47
48 /* A zero-terminated list of I/O addresses to be probed. */
49 static unsigned int wd_portlist[] __initdata =
50 {0x300, 0x280, 0x380, 0x240, 0};
51
52 static int wd_probe1(struct net_device *dev, int ioaddr);
53
54 static int wd_open(struct net_device *dev);
55 static void wd_reset_8390(struct net_device *dev);
56 static void wd_get_8390_hdr(struct net_device *dev, struct e8390_pkt_hdr *hdr,
57                                                 int ring_page);
58 static void wd_block_input(struct net_device *dev, int count,
59                                                   struct sk_buff *skb, int ring_offset);
60 static void wd_block_output(struct net_device *dev, int count,
61                                                         const unsigned char *buf, int start_page);
62 static int wd_close(struct net_device *dev);
63
64 \f
65 #define WD_START_PG             0x00    /* First page of TX buffer */
66 #define WD03_STOP_PG    0x20    /* Last page +1 of RX ring */
67 #define WD13_STOP_PG    0x40    /* Last page +1 of RX ring */
68
69 #define WD_CMDREG               0               /* Offset to ASIC command register. */
70 #define  WD_RESET               0x80    /* Board reset, in WD_CMDREG. */
71 #define  WD_MEMENB              0x40    /* Enable the shared memory. */
72 #define WD_CMDREG5              5               /* Offset to 16-bit-only ASIC register 5. */
73 #define  ISA16                  0x80    /* Enable 16 bit access from the ISA bus. */
74 #define  NIC16                  0x40    /* Enable 16 bit access from the 8390. */
75 #define WD_NIC_OFFSET   16              /* Offset to the 8390 from the base_addr. */
76 #define WD_IO_EXTENT    32
77
78 \f
79 /*      Probe for the WD8003 and WD8013.  These cards have the station
80         address PROM at I/O ports <base>+8 to <base>+13, with a checksum
81         following. A Soundblaster can have the same checksum as an WDethercard,
82         so we have an extra exclusionary check for it.
83
84         The wd_probe1() routine initializes the card and fills the
85         station address field. */
86
87 static int __init do_wd_probe(struct net_device *dev)
88 {
89         int i;
90         struct resource *r;
91         int base_addr = dev->base_addr;
92         int irq = dev->irq;
93         int mem_start = dev->mem_start;
94         int mem_end = dev->mem_end;
95
96         SET_MODULE_OWNER(dev);
97
98         if (base_addr > 0x1ff) {        /* Check a user specified location. */
99                 r = request_region(base_addr, WD_IO_EXTENT, "wd-probe");
100                 if ( r == NULL)
101                         return -EBUSY;
102                 i = wd_probe1(dev, base_addr);
103                 if (i != 0)  
104                         release_region(base_addr, WD_IO_EXTENT);
105                 else
106                         r->name = dev->name;
107                 return i;
108         }
109         else if (base_addr != 0)        /* Don't probe at all. */
110                 return -ENXIO;
111
112         for (i = 0; wd_portlist[i]; i++) {
113                 int ioaddr = wd_portlist[i];
114                 r = request_region(ioaddr, WD_IO_EXTENT, "wd-probe");
115                 if (r == NULL)
116                         continue;
117                 if (wd_probe1(dev, ioaddr) == 0) {
118                         r->name = dev->name;
119                         return 0;
120                 }
121                 release_region(ioaddr, WD_IO_EXTENT);
122                 dev->irq = irq;
123                 dev->mem_start = mem_start;
124                 dev->mem_end = mem_end;
125         }
126
127         return -ENODEV;
128 }
129
130 static void cleanup_card(struct net_device *dev)
131 {
132         free_irq(dev->irq, dev);
133         release_region(dev->base_addr - WD_NIC_OFFSET, WD_IO_EXTENT);
134         iounmap(ei_status.mem);
135 }
136
137 #ifndef MODULE
138 struct net_device * __init wd_probe(int unit)
139 {
140         struct net_device *dev = alloc_ei_netdev();
141         int err;
142
143         if (!dev)
144                 return ERR_PTR(-ENOMEM);
145
146         sprintf(dev->name, "eth%d", unit);
147         netdev_boot_setup_check(dev);
148
149         err = do_wd_probe(dev);
150         if (err)
151                 goto out;
152         err = register_netdev(dev);
153         if (err)
154                 goto out1;
155         return dev;
156 out1:
157         cleanup_card(dev);
158 out:
159         free_netdev(dev);
160         return ERR_PTR(err);
161 }
162 #endif
163
164 static int __init wd_probe1(struct net_device *dev, int ioaddr)
165 {
166         int i;
167         int checksum = 0;
168         int ancient = 0;                        /* An old card without config registers. */
169         int word16 = 0;                         /* 0 = 8 bit, 1 = 16 bit */
170         const char *model_name;
171         static unsigned version_printed;
172
173         for (i = 0; i < 8; i++)
174                 checksum += inb(ioaddr + 8 + i);
175         if (inb(ioaddr + 8) == 0xff     /* Extra check to avoid soundcard. */
176                 || inb(ioaddr + 9) == 0xff
177                 || (checksum & 0xff) != 0xFF)
178                 return -ENODEV;
179
180         /* Check for semi-valid mem_start/end values if supplied. */
181         if ((dev->mem_start % 0x2000) || (dev->mem_end % 0x2000)) {
182                 printk(KERN_WARNING "wd.c: user supplied mem_start or mem_end not on 8kB boundary - ignored.\n");
183                 dev->mem_start = 0;
184                 dev->mem_end = 0;
185         }
186
187         if (ei_debug  &&  version_printed++ == 0)
188                 printk(version);
189
190         printk("%s: WD80x3 at %#3x,", dev->name, ioaddr);
191         for (i = 0; i < 6; i++)
192                 printk(" %2.2X", dev->dev_addr[i] = inb(ioaddr + 8 + i));
193
194         /* The following PureData probe code was contributed by
195            Mike Jagdis <jaggy@purplet.demon.co.uk>. Puredata does software
196            configuration differently from others so we have to check for them.
197            This detects an 8 bit, 16 bit or dumb (Toshiba, jumpered) card.
198            */
199         if (inb(ioaddr+0) == 'P' && inb(ioaddr+1) == 'D') {
200                 unsigned char reg5 = inb(ioaddr+5);
201
202                 switch (inb(ioaddr+2)) {
203                 case 0x03: word16 = 0; model_name = "PDI8023-8";        break;
204                 case 0x05: word16 = 0; model_name = "PDUC8023"; break;
205                 case 0x0a: word16 = 1; model_name = "PDI8023-16"; break;
206                         /* Either 0x01 (dumb) or they've released a new version. */
207                 default:         word16 = 0; model_name = "PDI8023";    break;
208                 }
209                 dev->mem_start = ((reg5 & 0x1c) + 0xc0) << 12;
210                 dev->irq = (reg5 & 0xe0) == 0xe0 ? 10 : (reg5 >> 5) + 1;
211         } else {                                                                /* End of PureData probe */
212                 /* This method of checking for a 16-bit board is borrowed from the
213                    we.c driver.  A simpler method is just to look in ASIC reg. 0x03.
214                    I'm comparing the two method in alpha test to make certain they
215                    return the same result. */
216                 /* Check for the old 8 bit board - it has register 0/8 aliasing.
217                    Do NOT check i>=6 here -- it hangs the old 8003 boards! */
218                 for (i = 0; i < 6; i++)
219                         if (inb(ioaddr+i) != inb(ioaddr+8+i))
220                                 break;
221                 if (i >= 6) {
222                         ancient = 1;
223                         model_name = "WD8003-old";
224                         word16 = 0;
225                 } else {
226                         int tmp = inb(ioaddr+1); /* fiddle with 16bit bit */
227                         outb( tmp ^ 0x01, ioaddr+1 ); /* attempt to clear 16bit bit */
228                         if (((inb( ioaddr+1) & 0x01) == 0x01) /* A 16 bit card */
229                                 && (tmp & 0x01) == 0x01 ) {                             /* In a 16 slot. */
230                                 int asic_reg5 = inb(ioaddr+WD_CMDREG5);
231                                 /* Magic to set ASIC to word-wide mode. */
232                                 outb( NIC16 | (asic_reg5&0x1f), ioaddr+WD_CMDREG5);
233                                 outb(tmp, ioaddr+1);
234                                 model_name = "WD8013";
235                                 word16 = 1;             /* We have a 16bit board here! */
236                         } else {
237                                 model_name = "WD8003";
238                                 word16 = 0;
239                         }
240                         outb(tmp, ioaddr+1);                    /* Restore original reg1 value. */
241                 }
242 #ifndef final_version
243                 if ( !ancient && (inb(ioaddr+1) & 0x01) != (word16 & 0x01))
244                         printk("\nWD80?3: Bus width conflict, %d (probe) != %d (reg report).",
245                                    word16 ? 16 : 8, (inb(ioaddr+1) & 0x01) ? 16 : 8);
246 #endif
247         }
248
249 #if defined(WD_SHMEM) && WD_SHMEM > 0x80000
250         /* Allow a compile-time override.        */
251         dev->mem_start = WD_SHMEM;
252 #else
253         if (dev->mem_start == 0) {
254                 /* Sanity and old 8003 check */
255                 int reg0 = inb(ioaddr);
256                 if (reg0 == 0xff || reg0 == 0) {
257                         /* Future plan: this could check a few likely locations first. */
258                         dev->mem_start = 0xd0000;
259                         printk(" assigning address %#lx", dev->mem_start);
260                 } else {
261                         int high_addr_bits = inb(ioaddr+WD_CMDREG5) & 0x1f;
262                         /* Some boards don't have the register 5 -- it returns 0xff. */
263                         if (high_addr_bits == 0x1f || word16 == 0)
264                                 high_addr_bits = 0x01;
265                         dev->mem_start = ((reg0&0x3f) << 13) + (high_addr_bits << 19);
266                 }
267         }
268 #endif
269
270         /* The 8390 isn't at the base address -- the ASIC regs are there! */
271         dev->base_addr = ioaddr+WD_NIC_OFFSET;
272
273         if (dev->irq < 2) {
274                 int irqmap[] = {9,3,5,7,10,11,15,4};
275                 int reg1 = inb(ioaddr+1);
276                 int reg4 = inb(ioaddr+4);
277                 if (ancient || reg1 == 0xff) {  /* Ack!! No way to read the IRQ! */
278                         short nic_addr = ioaddr+WD_NIC_OFFSET;
279                         unsigned long irq_mask;
280
281                         /* We have an old-style ethercard that doesn't report its IRQ
282                            line.  Do autoirq to find the IRQ line. Note that this IS NOT
283                            a reliable way to trigger an interrupt. */
284                         outb_p(E8390_NODMA + E8390_STOP, nic_addr);
285                         outb(0x00, nic_addr+EN0_IMR);   /* Disable all intrs. */
286                         
287                         irq_mask = probe_irq_on();
288                         outb_p(0xff, nic_addr + EN0_IMR);       /* Enable all interrupts. */
289                         outb_p(0x00, nic_addr + EN0_RCNTLO);
290                         outb_p(0x00, nic_addr + EN0_RCNTHI);
291                         outb(E8390_RREAD+E8390_START, nic_addr); /* Trigger it... */
292                         mdelay(20);
293                         dev->irq = probe_irq_off(irq_mask);
294                         
295                         outb_p(0x00, nic_addr+EN0_IMR); /* Mask all intrs. again. */
296
297                         if (ei_debug > 2)
298                                 printk(" autoirq is %d", dev->irq);
299                         if (dev->irq < 2)
300                                 dev->irq = word16 ? 10 : 5;
301                 } else
302                         dev->irq = irqmap[((reg4 >> 5) & 0x03) + (reg1 & 0x04)];
303         } else if (dev->irq == 2)               /* Fixup bogosity: IRQ2 is really IRQ9 */
304                 dev->irq = 9;
305
306         /* Snarf the interrupt now.  There's no point in waiting since we cannot
307            share and the board will usually be enabled. */
308         i = request_irq(dev->irq, ei_interrupt, 0, DRV_NAME, dev);
309         if (i) {
310                 printk (" unable to get IRQ %d.\n", dev->irq);
311                 return i;
312         }
313
314         /* OK, were are certain this is going to work.  Setup the device. */
315         ei_status.name = model_name;
316         ei_status.word16 = word16;
317         ei_status.tx_start_page = WD_START_PG;
318         ei_status.rx_start_page = WD_START_PG + TX_PAGES;
319
320         /* Don't map in the shared memory until the board is actually opened. */
321
322         /* Some cards (eg WD8003EBT) can be jumpered for more (32k!) memory. */
323         if (dev->mem_end != 0) {
324                 ei_status.stop_page = (dev->mem_end - dev->mem_start)/256;
325                 ei_status.priv = dev->mem_end - dev->mem_start;
326         } else {
327                 ei_status.stop_page = word16 ? WD13_STOP_PG : WD03_STOP_PG;
328                 dev->mem_end = dev->mem_start + (ei_status.stop_page - WD_START_PG)*256;
329                 ei_status.priv = (ei_status.stop_page - WD_START_PG)*256;
330         }
331
332         ei_status.mem = ioremap(dev->mem_start, ei_status.priv);
333         if (!ei_status.mem) {
334                 free_irq(dev->irq, dev);
335                 return -ENOMEM;
336         }
337
338         printk(" %s, IRQ %d, shared memory at %#lx-%#lx.\n",
339                    model_name, dev->irq, dev->mem_start, dev->mem_end-1);
340
341         ei_status.reset_8390 = &wd_reset_8390;
342         ei_status.block_input = &wd_block_input;
343         ei_status.block_output = &wd_block_output;
344         ei_status.get_8390_hdr = &wd_get_8390_hdr;
345         dev->open = &wd_open;
346         dev->stop = &wd_close;
347 #ifdef CONFIG_NET_POLL_CONTROLLER
348         dev->poll_controller = ei_poll;
349 #endif
350         NS8390_init(dev, 0);
351
352 #if 1
353         /* Enable interrupt generation on softconfig cards -- M.U */
354         /* .. but possibly potentially unsafe - Donald */
355         if (inb(ioaddr+14) & 0x20)
356                 outb(inb(ioaddr+4)|0x80, ioaddr+4);
357 #endif
358
359         return 0;
360 }
361
362 static int
363 wd_open(struct net_device *dev)
364 {
365   int ioaddr = dev->base_addr - WD_NIC_OFFSET; /* WD_CMDREG */
366
367   /* Map in the shared memory. Always set register 0 last to remain
368          compatible with very old boards. */
369   ei_status.reg0 = ((dev->mem_start>>13) & 0x3f) | WD_MEMENB;
370   ei_status.reg5 = ((dev->mem_start>>19) & 0x1f) | NIC16;
371
372   if (ei_status.word16)
373           outb(ei_status.reg5, ioaddr+WD_CMDREG5);
374   outb(ei_status.reg0, ioaddr); /* WD_CMDREG */
375
376   ei_open(dev);
377   return 0;
378 }
379
380 static void
381 wd_reset_8390(struct net_device *dev)
382 {
383         int wd_cmd_port = dev->base_addr - WD_NIC_OFFSET; /* WD_CMDREG */
384
385         outb(WD_RESET, wd_cmd_port);
386         if (ei_debug > 1) printk("resetting the WD80x3 t=%lu...", jiffies);
387         ei_status.txing = 0;
388
389         /* Set up the ASIC registers, just in case something changed them. */
390         outb((((dev->mem_start>>13) & 0x3f)|WD_MEMENB), wd_cmd_port);
391         if (ei_status.word16)
392                 outb(NIC16 | ((dev->mem_start>>19) & 0x1f), wd_cmd_port+WD_CMDREG5);
393
394         if (ei_debug > 1) printk("reset done\n");
395         return;
396 }
397
398 /* Grab the 8390 specific header. Similar to the block_input routine, but
399    we don't need to be concerned with ring wrap as the header will be at
400    the start of a page, so we optimize accordingly. */
401
402 static void
403 wd_get_8390_hdr(struct net_device *dev, struct e8390_pkt_hdr *hdr, int ring_page)
404 {
405
406         int wd_cmdreg = dev->base_addr - WD_NIC_OFFSET; /* WD_CMDREG */
407         void __iomem *hdr_start = ei_status.mem + ((ring_page - WD_START_PG)<<8);
408
409         /* We'll always get a 4 byte header read followed by a packet read, so
410            we enable 16 bit mode before the header, and disable after the body. */
411         if (ei_status.word16)
412                 outb(ISA16 | ei_status.reg5, wd_cmdreg+WD_CMDREG5);
413
414 #ifdef __BIG_ENDIAN
415         /* Officially this is what we are doing, but the readl() is faster */
416         /* unfortunately it isn't endian aware of the struct               */
417         memcpy_fromio(hdr, hdr_start, sizeof(struct e8390_pkt_hdr));
418         hdr->count = le16_to_cpu(hdr->count);
419 #else
420         ((unsigned int*)hdr)[0] = readl(hdr_start);
421 #endif
422 }
423
424 /* Block input and output are easy on shared memory ethercards, and trivial
425    on the Western digital card where there is no choice of how to do it.
426    The only complications are that the ring buffer wraps, and need to map
427    switch between 8- and 16-bit modes. */
428
429 static void
430 wd_block_input(struct net_device *dev, int count, struct sk_buff *skb, int ring_offset)
431 {
432         int wd_cmdreg = dev->base_addr - WD_NIC_OFFSET; /* WD_CMDREG */
433         unsigned long offset = ring_offset - (WD_START_PG<<8);
434         void __iomem *xfer_start = ei_status.mem + offset;
435
436         if (offset + count > ei_status.priv) {
437                 /* We must wrap the input move. */
438                 int semi_count = ei_status.priv - offset;
439                 memcpy_fromio(skb->data, xfer_start, semi_count);
440                 count -= semi_count;
441                 memcpy_fromio(skb->data + semi_count, ei_status.mem + TX_PAGES * 256, count);
442         } else {
443                 /* Packet is in one chunk -- we can copy + cksum. */
444                 eth_io_copy_and_sum(skb, xfer_start, count, 0);
445         }
446
447         /* Turn off 16 bit access so that reboot works.  ISA brain-damage */
448         if (ei_status.word16)
449                 outb(ei_status.reg5, wd_cmdreg+WD_CMDREG5);
450 }
451
452 static void
453 wd_block_output(struct net_device *dev, int count, const unsigned char *buf,
454                                 int start_page)
455 {
456         int wd_cmdreg = dev->base_addr - WD_NIC_OFFSET; /* WD_CMDREG */
457         void __iomem *shmem = ei_status.mem + ((start_page - WD_START_PG)<<8);
458
459
460         if (ei_status.word16) {
461                 /* Turn on and off 16 bit access so that reboot works. */
462                 outb(ISA16 | ei_status.reg5, wd_cmdreg+WD_CMDREG5);
463                 memcpy_toio(shmem, buf, count);
464                 outb(ei_status.reg5, wd_cmdreg+WD_CMDREG5);
465         } else
466                 memcpy_toio(shmem, buf, count);
467 }
468
469
470 static int
471 wd_close(struct net_device *dev)
472 {
473         int wd_cmdreg = dev->base_addr - WD_NIC_OFFSET; /* WD_CMDREG */
474
475         if (ei_debug > 1)
476                 printk("%s: Shutting down ethercard.\n", dev->name);
477         ei_close(dev);
478
479         /* Change from 16-bit to 8-bit shared memory so reboot works. */
480         if (ei_status.word16)
481                 outb(ei_status.reg5, wd_cmdreg + WD_CMDREG5 );
482
483         /* And disable the shared memory. */
484         outb(ei_status.reg0 & ~WD_MEMENB, wd_cmdreg);
485
486         return 0;
487 }
488
489 \f
490 #ifdef MODULE
491 #define MAX_WD_CARDS    4       /* Max number of wd cards per module */
492 static struct net_device *dev_wd[MAX_WD_CARDS];
493 static int io[MAX_WD_CARDS];
494 static int irq[MAX_WD_CARDS];
495 static int mem[MAX_WD_CARDS];
496 static int mem_end[MAX_WD_CARDS];       /* for non std. mem size */
497
498 module_param_array(io, int, NULL, 0);
499 module_param_array(irq, int, NULL, 0);
500 module_param_array(mem, int, NULL, 0);
501 module_param_array(mem_end, int, NULL, 0);
502 MODULE_PARM_DESC(io, "I/O base address(es)");
503 MODULE_PARM_DESC(irq, "IRQ number(s) (ignored for PureData boards)");
504 MODULE_PARM_DESC(mem, "memory base address(es)(ignored for PureData boards)");
505 MODULE_PARM_DESC(mem_end, "memory end address(es)");
506 MODULE_DESCRIPTION("ISA Western Digital wd8003/wd8013 ; SMC Elite, Elite16 ethernet driver");
507 MODULE_LICENSE("GPL");
508
509 /* This is set up so that only a single autoprobe takes place per call.
510 ISA device autoprobes on a running machine are not recommended. */
511 int
512 init_module(void)
513 {
514         struct net_device *dev;
515         int this_dev, found = 0;
516
517         for (this_dev = 0; this_dev < MAX_WD_CARDS; this_dev++) {
518                 if (io[this_dev] == 0)  {
519                         if (this_dev != 0) break; /* only autoprobe 1st one */
520                         printk(KERN_NOTICE "wd.c: Presently autoprobing (not recommended) for a single card.\n");
521                 }
522                 dev = alloc_ei_netdev();
523                 if (!dev)
524                         break;
525                 dev->irq = irq[this_dev];
526                 dev->base_addr = io[this_dev];
527                 dev->mem_start = mem[this_dev];
528                 dev->mem_end = mem_end[this_dev];
529                 if (do_wd_probe(dev) == 0) {
530                         if (register_netdev(dev) == 0) {
531                                 dev_wd[found++] = dev;
532                                 continue;
533                         }
534                         cleanup_card(dev);
535                 }
536                 free_netdev(dev);
537                 printk(KERN_WARNING "wd.c: No wd80x3 card found (i/o = 0x%x).\n", io[this_dev]);
538                 break;
539         }
540         if (found)
541                 return 0;
542         return -ENXIO;
543 }
544
545 void
546 cleanup_module(void)
547 {
548         int this_dev;
549
550         for (this_dev = 0; this_dev < MAX_WD_CARDS; this_dev++) {
551                 struct net_device *dev = dev_wd[this_dev];
552                 if (dev) {
553                         unregister_netdev(dev);
554                         cleanup_card(dev);
555                         free_netdev(dev);
556                 }
557         }
558 }
559 #endif /* MODULE */