1 /* wd.c: A WD80x3 ethernet driver for linux. */
3 Written 1993-94 by Donald Becker.
5 Copyright 1993 United States Government as represented by the
6 Director, National Security Agency.
8 This software may be used and distributed according to the terms
9 of the GNU Public License, incorporated herein by reference.
11 The author may be reached as becker@CESDIS.gsfc.nasa.gov, or C/O
12 Center of Excellence in Space Data and Information Sciences
13 Code 930.5, Goddard Space Flight Center, Greenbelt MD 20771
15 This is a driver for WD8003 and WD8013 "compatible" ethercards.
17 Thanks to Russ Nelson (nelson@crnwyr.com) for loaning me a WD8013.
21 Paul Gortmaker : multiple card support for module users, support
22 for non-standard memory sizes.
27 static const char *version
=
28 "wd.c:v1.10 9/23/94 Donald Becker (becker@cesdis.gsfc.nasa.gov)\n";
30 #include <linux/module.h>
32 #include <linux/kernel.h>
33 #include <linux/sched.h>
34 #include <linux/errno.h>
35 #include <linux/string.h>
36 #include <linux/init.h>
38 #include <asm/system.h>
40 #include <linux/netdevice.h>
41 #include <linux/etherdevice.h>
44 /* A zero-terminated list of I/O addresses to be probed. */
45 static unsigned int wd_portlist
[] __initdata
=
46 {0x300, 0x280, 0x380, 0x240, 0};
48 int wd_probe(struct device
*dev
);
49 int wd_probe1(struct device
*dev
, int ioaddr
);
51 static int wd_open(struct device
*dev
);
52 static void wd_reset_8390(struct device
*dev
);
53 static void wd_get_8390_hdr(struct device
*dev
, struct e8390_pkt_hdr
*hdr
,
55 static void wd_block_input(struct device
*dev
, int count
,
56 struct sk_buff
*skb
, int ring_offset
);
57 static void wd_block_output(struct device
*dev
, int count
,
58 const unsigned char *buf
, int start_page
);
59 static int wd_close(struct device
*dev
);
62 #define WD_START_PG 0x00 /* First page of TX buffer */
63 #define WD03_STOP_PG 0x20 /* Last page +1 of RX ring */
64 #define WD13_STOP_PG 0x40 /* Last page +1 of RX ring */
66 #define WD_CMDREG 0 /* Offset to ASIC command register. */
67 #define WD_RESET 0x80 /* Board reset, in WD_CMDREG. */
68 #define WD_MEMENB 0x40 /* Enable the shared memory. */
69 #define WD_CMDREG5 5 /* Offset to 16-bit-only ASIC register 5. */
70 #define ISA16 0x80 /* Enable 16 bit access from the ISA bus. */
71 #define NIC16 0x40 /* Enable 16 bit access from the 8390. */
72 #define WD_NIC_OFFSET 16 /* Offset to the 8390 from the base_addr. */
73 #define WD_IO_EXTENT 32
76 /* Probe for the WD8003 and WD8013. These cards have the station
77 address PROM at I/O ports <base>+8 to <base>+13, with a checksum
78 following. A Soundblaster can have the same checksum as an WDethercard,
79 so we have an extra exclusionary check for it.
81 The wd_probe1() routine initializes the card and fills the
82 station address field. */
85 struct netdev_entry wd_drv
=
86 {"wd", wd_probe1
, WD_IO_EXTENT
, wd_portlist
};
89 int __init
wd_probe(struct device
*dev
)
92 int base_addr
= dev
? dev
->base_addr
: 0;
94 if (base_addr
> 0x1ff) /* Check a single specified location. */
95 return wd_probe1(dev
, base_addr
);
96 else if (base_addr
!= 0) /* Don't probe at all. */
99 for (i
= 0; wd_portlist
[i
]; i
++) {
100 int ioaddr
= wd_portlist
[i
];
101 if (check_region(ioaddr
, WD_IO_EXTENT
))
103 if (wd_probe1(dev
, ioaddr
) == 0)
111 int __init
wd_probe1(struct device
*dev
, int ioaddr
)
115 int ancient
= 0; /* An old card without config registers. */
116 int word16
= 0; /* 0 = 8 bit, 1 = 16 bit */
117 const char *model_name
;
118 static unsigned version_printed
= 0;
120 for (i
= 0; i
< 8; i
++)
121 checksum
+= inb(ioaddr
+ 8 + i
);
122 if (inb(ioaddr
+ 8) == 0xff /* Extra check to avoid soundcard. */
123 || inb(ioaddr
+ 9) == 0xff
124 || (checksum
& 0xff) != 0xFF)
127 /* Looks like we have a card. Make sure 8390 support is available. */
128 if (load_8390_module("wd.c"))
131 /* We should have a "dev" from Space.c or the static module table. */
133 printk("wd.c: Passed a NULL device.\n");
134 dev
= init_etherdev(0, 0);
137 /* Check for semi-valid mem_start/end values if supplied. */
138 if ((dev
->mem_start
% 0x2000) || (dev
->mem_end
% 0x2000)) {
139 printk(KERN_WARNING
"wd.c: user supplied mem_start or mem_end not on 8kB boundary - ignored.\n");
144 if (ei_debug
&& version_printed
++ == 0)
147 printk("%s: WD80x3 at %#3x,", dev
->name
, ioaddr
);
148 for (i
= 0; i
< 6; i
++)
149 printk(" %2.2X", dev
->dev_addr
[i
] = inb(ioaddr
+ 8 + i
));
151 /* The following PureData probe code was contributed by
152 Mike Jagdis <jaggy@purplet.demon.co.uk>. Puredata does software
153 configuration differently from others so we have to check for them.
154 This detects an 8 bit, 16 bit or dumb (Toshiba, jumpered) card.
156 if (inb(ioaddr
+0) == 'P' && inb(ioaddr
+1) == 'D') {
157 unsigned char reg5
= inb(ioaddr
+5);
159 switch (inb(ioaddr
+2)) {
160 case 0x03: word16
= 0; model_name
= "PDI8023-8"; break;
161 case 0x05: word16
= 0; model_name
= "PDUC8023"; break;
162 case 0x0a: word16
= 1; model_name
= "PDI8023-16"; break;
163 /* Either 0x01 (dumb) or they've released a new version. */
164 default: word16
= 0; model_name
= "PDI8023"; break;
166 dev
->mem_start
= ((reg5
& 0x1c) + 0xc0) << 12;
167 dev
->irq
= (reg5
& 0xe0) == 0xe0 ? 10 : (reg5
>> 5) + 1;
168 } else { /* End of PureData probe */
169 /* This method of checking for a 16-bit board is borrowed from the
170 we.c driver. A simpler method is just to look in ASIC reg. 0x03.
171 I'm comparing the two method in alpha test to make certain they
172 return the same result. */
173 /* Check for the old 8 bit board - it has register 0/8 aliasing.
174 Do NOT check i>=6 here -- it hangs the old 8003 boards! */
175 for (i
= 0; i
< 6; i
++)
176 if (inb(ioaddr
+i
) != inb(ioaddr
+8+i
))
180 model_name
= "WD8003-old";
183 int tmp
= inb(ioaddr
+1); /* fiddle with 16bit bit */
184 outb( tmp
^ 0x01, ioaddr
+1 ); /* attempt to clear 16bit bit */
185 if (((inb( ioaddr
+1) & 0x01) == 0x01) /* A 16 bit card */
186 && (tmp
& 0x01) == 0x01 ) { /* In a 16 slot. */
187 int asic_reg5
= inb(ioaddr
+WD_CMDREG5
);
188 /* Magic to set ASIC to word-wide mode. */
189 outb( NIC16
| (asic_reg5
&0x1f), ioaddr
+WD_CMDREG5
);
191 model_name
= "WD8013";
192 word16
= 1; /* We have a 16bit board here! */
194 model_name
= "WD8003";
197 outb(tmp
, ioaddr
+1); /* Restore original reg1 value. */
199 #ifndef final_version
200 if ( !ancient
&& (inb(ioaddr
+1) & 0x01) != (word16
& 0x01))
201 printk("\nWD80?3: Bus width conflict, %d (probe) != %d (reg report).",
202 word16
? 16 : 8, (inb(ioaddr
+1) & 0x01) ? 16 : 8);
206 #if defined(WD_SHMEM) && WD_SHMEM > 0x80000
207 /* Allow a compile-time override. */
208 dev
->mem_start
= WD_SHMEM
;
210 if (dev
->mem_start
== 0) {
211 /* Sanity and old 8003 check */
212 int reg0
= inb(ioaddr
);
213 if (reg0
== 0xff || reg0
== 0) {
214 /* Future plan: this could check a few likely locations first. */
215 dev
->mem_start
= 0xd0000;
216 printk(" assigning address %#lx", dev
->mem_start
);
218 int high_addr_bits
= inb(ioaddr
+WD_CMDREG5
) & 0x1f;
219 /* Some boards don't have the register 5 -- it returns 0xff. */
220 if (high_addr_bits
== 0x1f || word16
== 0)
221 high_addr_bits
= 0x01;
222 dev
->mem_start
= ((reg0
&0x3f) << 13) + (high_addr_bits
<< 19);
227 /* The 8390 isn't at the base address -- the ASIC regs are there! */
228 dev
->base_addr
= ioaddr
+WD_NIC_OFFSET
;
231 int irqmap
[] = {9,3,5,7,10,11,15,4};
232 int reg1
= inb(ioaddr
+1);
233 int reg4
= inb(ioaddr
+4);
234 if (ancient
|| reg1
== 0xff) { /* Ack!! No way to read the IRQ! */
235 short nic_addr
= ioaddr
+WD_NIC_OFFSET
;
237 /* We have an old-style ethercard that doesn't report its IRQ
238 line. Do autoirq to find the IRQ line. Note that this IS NOT
239 a reliable way to trigger an interrupt. */
240 outb_p(E8390_NODMA
+ E8390_STOP
, nic_addr
);
241 outb(0x00, nic_addr
+EN0_IMR
); /* Disable all intrs. */
243 outb_p(0xff, nic_addr
+ EN0_IMR
); /* Enable all interrupts. */
244 outb_p(0x00, nic_addr
+ EN0_RCNTLO
);
245 outb_p(0x00, nic_addr
+ EN0_RCNTHI
);
246 outb(E8390_RREAD
+E8390_START
, nic_addr
); /* Trigger it... */
247 dev
->irq
= autoirq_report(2);
248 outb_p(0x00, nic_addr
+EN0_IMR
); /* Mask all intrs. again. */
251 printk(" autoirq is %d", dev
->irq
);
253 dev
->irq
= word16
? 10 : 5;
255 dev
->irq
= irqmap
[((reg4
>> 5) & 0x03) + (reg1
& 0x04)];
256 } else if (dev
->irq
== 2) /* Fixup bogosity: IRQ2 is really IRQ9 */
259 /* Allocate dev->priv and fill in 8390 specific dev fields. */
260 if (ethdev_init(dev
)) {
261 printk (" unable to get memory for dev->priv.\n");
265 /* Snarf the interrupt now. There's no point in waiting since we cannot
266 share and the board will usually be enabled. */
267 if (request_irq(dev
->irq
, ei_interrupt
, 0, model_name
, dev
)) {
268 printk (" unable to get IRQ %d.\n", dev
->irq
);
274 /* OK, were are certain this is going to work. Setup the device. */
275 request_region(ioaddr
, WD_IO_EXTENT
, model_name
);
277 ei_status
.name
= model_name
;
278 ei_status
.word16
= word16
;
279 ei_status
.tx_start_page
= WD_START_PG
;
280 ei_status
.rx_start_page
= WD_START_PG
+ TX_PAGES
;
282 /* Don't map in the shared memory until the board is actually opened. */
283 dev
->rmem_start
= dev
->mem_start
+ TX_PAGES
*256;
285 /* Some cards (eg WD8003EBT) can be jumpered for more (32k!) memory. */
286 if (dev
->mem_end
!= 0) {
287 ei_status
.stop_page
= (dev
->mem_end
- dev
->mem_start
)/256;
289 ei_status
.stop_page
= word16
? WD13_STOP_PG
: WD03_STOP_PG
;
290 dev
->mem_end
= dev
->mem_start
+ (ei_status
.stop_page
- WD_START_PG
)*256;
292 dev
->rmem_end
= dev
->mem_end
;
294 printk(" %s, IRQ %d, shared memory at %#lx-%#lx.\n",
295 model_name
, dev
->irq
, dev
->mem_start
, dev
->mem_end
-1);
297 ei_status
.reset_8390
= &wd_reset_8390
;
298 ei_status
.block_input
= &wd_block_input
;
299 ei_status
.block_output
= &wd_block_output
;
300 ei_status
.get_8390_hdr
= &wd_get_8390_hdr
;
301 dev
->open
= &wd_open
;
302 dev
->stop
= &wd_close
;
306 /* Enable interrupt generation on softconfig cards -- M.U */
307 /* .. but possibly potentially unsafe - Donald */
308 if (inb(ioaddr
+14) & 0x20)
309 outb(inb(ioaddr
+4)|0x80, ioaddr
+4);
316 wd_open(struct device
*dev
)
318 int ioaddr
= dev
->base_addr
- WD_NIC_OFFSET
; /* WD_CMDREG */
320 /* Map in the shared memory. Always set register 0 last to remain
321 compatible with very old boards. */
322 ei_status
.reg0
= ((dev
->mem_start
>>13) & 0x3f) | WD_MEMENB
;
323 ei_status
.reg5
= ((dev
->mem_start
>>19) & 0x1f) | NIC16
;
325 if (ei_status
.word16
)
326 outb(ei_status
.reg5
, ioaddr
+WD_CMDREG5
);
327 outb(ei_status
.reg0
, ioaddr
); /* WD_CMDREG */
335 wd_reset_8390(struct device
*dev
)
337 int wd_cmd_port
= dev
->base_addr
- WD_NIC_OFFSET
; /* WD_CMDREG */
339 outb(WD_RESET
, wd_cmd_port
);
340 if (ei_debug
> 1) printk("resetting the WD80x3 t=%lu...", jiffies
);
343 /* Set up the ASIC registers, just in case something changed them. */
344 outb((((dev
->mem_start
>>13) & 0x3f)|WD_MEMENB
), wd_cmd_port
);
345 if (ei_status
.word16
)
346 outb(NIC16
| ((dev
->mem_start
>>19) & 0x1f), wd_cmd_port
+WD_CMDREG5
);
348 if (ei_debug
> 1) printk("reset done\n");
352 /* Grab the 8390 specific header. Similar to the block_input routine, but
353 we don't need to be concerned with ring wrap as the header will be at
354 the start of a page, so we optimize accordingly. */
357 wd_get_8390_hdr(struct device
*dev
, struct e8390_pkt_hdr
*hdr
, int ring_page
)
360 int wd_cmdreg
= dev
->base_addr
- WD_NIC_OFFSET
; /* WD_CMDREG */
361 unsigned long hdr_start
= dev
->mem_start
+ ((ring_page
- WD_START_PG
)<<8);
363 /* We'll always get a 4 byte header read followed by a packet read, so
364 we enable 16 bit mode before the header, and disable after the body. */
365 if (ei_status
.word16
)
366 outb(ISA16
| ei_status
.reg5
, wd_cmdreg
+WD_CMDREG5
);
369 /* Officially this is what we are doing, but the readl() is faster */
370 memcpy_fromio(hdr
, hdr_start
, sizeof(struct e8390_pkt_hdr
));
372 ((unsigned int*)hdr
)[0] = readl(hdr_start
);
376 /* Block input and output are easy on shared memory ethercards, and trivial
377 on the Western digital card where there is no choice of how to do it.
378 The only complications are that the ring buffer wraps, and need to map
379 switch between 8- and 16-bit modes. */
382 wd_block_input(struct device
*dev
, int count
, struct sk_buff
*skb
, int ring_offset
)
384 int wd_cmdreg
= dev
->base_addr
- WD_NIC_OFFSET
; /* WD_CMDREG */
385 unsigned long xfer_start
= dev
->mem_start
+ ring_offset
- (WD_START_PG
<<8);
387 if (xfer_start
+ count
> dev
->rmem_end
) {
388 /* We must wrap the input move. */
389 int semi_count
= dev
->rmem_end
- xfer_start
;
390 memcpy_fromio(skb
->data
, xfer_start
, semi_count
);
392 memcpy_fromio(skb
->data
+ semi_count
, dev
->rmem_start
, count
);
394 /* Packet is in one chunk -- we can copy + cksum. */
395 eth_io_copy_and_sum(skb
, xfer_start
, count
, 0);
398 /* Turn off 16 bit access so that reboot works. ISA brain-damage */
399 if (ei_status
.word16
)
400 outb(ei_status
.reg5
, wd_cmdreg
+WD_CMDREG5
);
404 wd_block_output(struct device
*dev
, int count
, const unsigned char *buf
,
407 int wd_cmdreg
= dev
->base_addr
- WD_NIC_OFFSET
; /* WD_CMDREG */
408 long shmem
= dev
->mem_start
+ ((start_page
- WD_START_PG
)<<8);
411 if (ei_status
.word16
) {
412 /* Turn on and off 16 bit access so that reboot works. */
413 outb(ISA16
| ei_status
.reg5
, wd_cmdreg
+WD_CMDREG5
);
414 memcpy_toio(shmem
, buf
, count
);
415 outb(ei_status
.reg5
, wd_cmdreg
+WD_CMDREG5
);
417 memcpy_toio(shmem
, buf
, count
);
422 wd_close(struct device
*dev
)
424 int wd_cmdreg
= dev
->base_addr
- WD_NIC_OFFSET
; /* WD_CMDREG */
427 printk("%s: Shutting down ethercard.\n", dev
->name
);
430 /* Change from 16-bit to 8-bit shared memory so reboot works. */
431 if (ei_status
.word16
)
432 outb(ei_status
.reg5
, wd_cmdreg
+ WD_CMDREG5
);
434 /* And disable the shared memory. */
435 outb(ei_status
.reg0
& ~WD_MEMENB
, wd_cmdreg
);
444 #define MAX_WD_CARDS 4 /* Max number of wd cards per module */
445 #define NAMELEN 8 /* # of chars for storing dev->name */
446 static char namelist
[NAMELEN
* MAX_WD_CARDS
] = { 0, };
447 static struct device dev_wd
[MAX_WD_CARDS
] = {
449 NULL
, /* assign a chunk of namelist[] below */
456 static int io
[MAX_WD_CARDS
] = { 0, };
457 static int irq
[MAX_WD_CARDS
] = { 0, };
458 static int mem
[MAX_WD_CARDS
] = { 0, };
459 static int mem_end
[MAX_WD_CARDS
] = { 0, }; /* for non std. mem size */
461 MODULE_PARM(io
, "1-" __MODULE_STRING(MAX_WD_CARDS
) "i");
462 MODULE_PARM(irq
, "1-" __MODULE_STRING(MAX_WD_CARDS
) "i");
463 MODULE_PARM(mem
, "1-" __MODULE_STRING(MAX_WD_CARDS
) "i");
464 MODULE_PARM(mem_end
, "1-" __MODULE_STRING(MAX_WD_CARDS
) "i");
466 /* This is set up so that only a single autoprobe takes place per call.
467 ISA device autoprobes on a running machine are not recommended. */
471 int this_dev
, found
= 0;
473 for (this_dev
= 0; this_dev
< MAX_WD_CARDS
; this_dev
++) {
474 struct device
*dev
= &dev_wd
[this_dev
];
475 dev
->name
= namelist
+(NAMELEN
*this_dev
);
476 dev
->irq
= irq
[this_dev
];
477 dev
->base_addr
= io
[this_dev
];
478 dev
->mem_start
= mem
[this_dev
];
479 dev
->mem_end
= mem_end
[this_dev
];
480 dev
->init
= wd_probe
;
481 if (io
[this_dev
] == 0) {
482 if (this_dev
!= 0) break; /* only autoprobe 1st one */
483 printk(KERN_NOTICE
"wd.c: Presently autoprobing (not recommended) for a single card.\n");
485 if (register_netdev(dev
) != 0) {
486 printk(KERN_WARNING
"wd.c: No wd80x3 card found (i/o = 0x%x).\n", io
[this_dev
]);
487 if (found
!= 0) { /* Got at least one. */
504 for (this_dev
= 0; this_dev
< MAX_WD_CARDS
; this_dev
++) {
505 struct device
*dev
= &dev_wd
[this_dev
];
506 if (dev
->priv
!= NULL
) {
507 void *priv
= dev
->priv
;
508 int ioaddr
= dev
->base_addr
- WD_NIC_OFFSET
;
509 free_irq(dev
->irq
, dev
);
510 release_region(ioaddr
, WD_IO_EXTENT
);
511 unregister_netdev(dev
);
513 unlock_8390_module();
522 * compile-command: "gcc -D__KERNEL__ -I/usr/src/linux/net/inet -Wall -Wstrict-prototypes -O6 -m486 -c wd.c"
525 * kept-new-versions: 5