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