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