blob: f73073b1218a1573b6fbea1909af55aac0f3283a [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/* ne2.c: A NE/2 Ethernet Driver for Linux. */
2/*
3 Based on the NE2000 driver written by Donald Becker (1992-94).
4 modified by Wim Dumon (Apr 1996)
5
6 This software may be used and distributed according to the terms
7 of the GNU General Public License, incorporated herein by reference.
8
9 The author may be reached as wimpie@linux.cc.kuleuven.ac.be
10
11 Currently supported: NE/2
12 This patch was never tested on other MCA-ethernet adapters, but it
13 might work. Just give it a try and let me know if you have problems.
14 Also mail me if it really works, please!
15
16 Changelog:
17 Mon Feb 3 16:26:02 MET 1997
18 - adapted the driver to work with the 2.1.25 kernel
19 - multiple ne2 support (untested)
20 - module support (untested)
21
22 Fri Aug 28 00:18:36 CET 1998 (David Weinehall)
23 - fixed a few minor typos
24 - made the MODULE_PARM conditional (it only works with the v2.1.x kernels)
25 - fixed the module support (Now it's working...)
26
27 Mon Sep 7 19:01:44 CET 1998 (David Weinehall)
28 - added support for Arco Electronics AE/2-card (experimental)
29
30 Mon Sep 14 09:53:42 CET 1998 (David Weinehall)
Jeff Garzik6aa20a22006-09-13 13:24:59 -040031 - added support for Compex ENET-16MC/P (experimental)
Linus Torvalds1da177e2005-04-16 15:20:36 -070032
33 Tue Sep 15 16:21:12 CET 1998 (David Weinehall, Magnus Jonsson, Tomas Ogren)
34 - Miscellaneous bugfixes
35
36 Tue Sep 19 16:21:12 CET 1998 (Magnus Jonsson)
37 - Cleanup
38
39 Wed Sep 23 14:33:34 CET 1998 (David Weinehall)
40 - Restructuring and rewriting for v2.1.x compliance
41
42 Wed Oct 14 17:19:21 CET 1998 (David Weinehall)
43 - Added code that unregisters irq and proc-info
44 - Version# bump
45
46 Mon Nov 16 15:28:23 CET 1998 (Wim Dumon)
Jeff Garzik6aa20a22006-09-13 13:24:59 -040047 - pass 'dev' as last parameter of request_irq in stead of 'NULL'
Linus Torvalds1da177e2005-04-16 15:20:36 -070048
49 Wed Feb 7 21:24:00 CET 2001 (Alfred Arnold)
50 - added support for the D-Link DE-320CT
Jeff Garzik6aa20a22006-09-13 13:24:59 -040051
Linus Torvalds1da177e2005-04-16 15:20:36 -070052 * WARNING
53 -------
54 This is alpha-test software. It is not guaranteed to work. As a
55 matter of fact, I'm quite sure there are *LOTS* of bugs in here. I
56 would like to hear from you if you use this driver, even if it works.
57 If it doesn't work, be sure to send me a mail with the problems !
58*/
59
60static const char *version = "ne2.c:v0.91 Nov 16 1998 Wim Dumon <wimpie@kotnet.org>\n";
61
62#include <linux/module.h>
63#include <linux/kernel.h>
64#include <linux/types.h>
65#include <linux/fcntl.h>
66#include <linux/interrupt.h>
67#include <linux/ioport.h>
68#include <linux/in.h>
69#include <linux/slab.h>
70#include <linux/string.h>
71#include <linux/errno.h>
72#include <linux/init.h>
73#include <linux/mca-legacy.h>
74#include <linux/netdevice.h>
75#include <linux/etherdevice.h>
76#include <linux/skbuff.h>
77#include <linux/bitops.h>
Marcelo Feitoza Parisiff5688a2006-01-09 18:37:15 -080078#include <linux/jiffies.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070079
80#include <asm/system.h>
81#include <asm/io.h>
82#include <asm/dma.h>
83
84#include "8390.h"
85
86#define DRV_NAME "ne2"
87
88/* Some defines that people can play with if so inclined. */
89
90/* Do we perform extra sanity checks on stuff ? */
91/* #define NE_SANITY_CHECK */
92
93/* Do we implement the read before write bugfix ? */
94/* #define NE_RW_BUGFIX */
95
96/* Do we have a non std. amount of memory? (in units of 256 byte pages) */
97/* #define PACKETBUF_MEMSIZE 0x40 */
98
99
100/* ---- No user-serviceable parts below ---- */
101
102#define NE_BASE (dev->base_addr)
103#define NE_CMD 0x00
104#define NE_DATAPORT 0x10 /* NatSemi-defined port window offset. */
105#define NE_RESET 0x20 /* Issue a read to reset, a write to clear. */
106#define NE_IO_EXTENT 0x30
107
108#define NE1SM_START_PG 0x20 /* First page of TX buffer */
109#define NE1SM_STOP_PG 0x40 /* Last page +1 of RX ring */
110#define NESM_START_PG 0x40 /* First page of TX buffer */
111#define NESM_STOP_PG 0x80 /* Last page +1 of RX ring */
112
113/* From the .ADF file: */
114static unsigned int addresses[7] __initdata =
115 {0x1000, 0x2020, 0x8020, 0xa0a0, 0xb0b0, 0xc0c0, 0xc3d0};
116static int irqs[4] __initdata = {3, 4, 5, 9};
117
118/* From the D-Link ADF file: */
119static unsigned int dlink_addresses[4] __initdata =
120 {0x300, 0x320, 0x340, 0x360};
121static int dlink_irqs[8] __initdata = {3, 4, 5, 9, 10, 11, 14, 15};
122
123struct ne2_adapters_t {
124 unsigned int id;
125 char *name;
126};
127
128static struct ne2_adapters_t ne2_adapters[] __initdata = {
129 { 0x6354, "Arco Ethernet Adapter AE/2" },
130 { 0x70DE, "Compex ENET-16 MC/P" },
131 { 0x7154, "Novell Ethernet Adapter NE/2" },
132 { 0x56ea, "D-Link DE-320CT" },
133 { 0x0000, NULL }
134};
135
136extern int netcard_probe(struct net_device *dev);
137
138static int ne2_probe1(struct net_device *dev, int slot);
139
140static int ne_open(struct net_device *dev);
141static int ne_close(struct net_device *dev);
142
143static void ne_reset_8390(struct net_device *dev);
144static void ne_get_8390_hdr(struct net_device *dev, struct e8390_pkt_hdr *hdr,
145 int ring_page);
146static void ne_block_input(struct net_device *dev, int count,
147 struct sk_buff *skb, int ring_offset);
148static void ne_block_output(struct net_device *dev, const int count,
149 const unsigned char *buf, const int start_page);
150
151
152/*
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400153 * special code to read the DE-320's MAC address EEPROM. In contrast to a
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154 * standard NE design, this is a serial EEPROM (93C46) that has to be read
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400155 * bit by bit. The EEPROM cotrol port at base + 0x1e has the following
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156 * layout:
157 *
158 * Bit 0 = Data out (read from EEPROM)
159 * Bit 1 = Data in (write to EEPROM)
160 * Bit 2 = Clock
161 * Bit 3 = Chip Select
162 * Bit 7 = ~50 kHz clock for defined delays
163 *
164 */
165
166static void __init dlink_put_eeprom(unsigned char value, unsigned int addr)
167{
168 int z;
169 unsigned char v1, v2;
170
171 /* write the value to the NIC EEPROM register */
172
173 outb(value, addr + 0x1e);
174
175 /* now wait the clock line to toggle twice. Effectively, we are
176 waiting (at least) for one clock cycle */
177
178 for (z = 0; z < 2; z++) {
179 do {
180 v1 = inb(addr + 0x1e);
181 v2 = inb(addr + 0x1e);
182 }
183 while (!((v1 ^ v2) & 0x80));
184 }
185}
186
187static void __init dlink_send_eeprom_bit(unsigned int bit, unsigned int addr)
188{
189 /* shift data bit into correct position */
190
191 bit = bit << 1;
192
193 /* write value, keep clock line high for two cycles */
194
195 dlink_put_eeprom(0x09 | bit, addr);
196 dlink_put_eeprom(0x0d | bit, addr);
197 dlink_put_eeprom(0x0d | bit, addr);
198 dlink_put_eeprom(0x09 | bit, addr);
199}
200
201static void __init dlink_send_eeprom_word(unsigned int value, unsigned int len, unsigned int addr)
202{
203 int z;
204
205 /* adjust bits so that they are left-aligned in a 16-bit-word */
206
207 value = value << (16 - len);
208
209 /* shift bits out to the EEPROM */
210
211 for (z = 0; z < len; z++) {
212 dlink_send_eeprom_bit((value & 0x8000) >> 15, addr);
213 value = value << 1;
214 }
215}
216
217static unsigned int __init dlink_get_eeprom(unsigned int eeaddr, unsigned int addr)
218{
219 int z;
220 unsigned int value = 0;
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400221
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 /* pull the CS line low for a moment. This resets the EEPROM-
223 internal logic, and makes it ready for a new command. */
224
225 dlink_put_eeprom(0x01, addr);
226 dlink_put_eeprom(0x09, addr);
227
228 /* send one start bit, read command (1 - 0), plus the address to
229 the EEPROM */
230
231 dlink_send_eeprom_word(0x0180 | (eeaddr & 0x3f), 9, addr);
232
233 /* get the data word. We clock by sending 0s to the EEPROM, which
234 get ignored during the read process */
235
236 for (z = 0; z < 16; z++) {
237 dlink_send_eeprom_bit(0, addr);
238 value = (value << 1) | (inb(addr + 0x1e) & 0x01);
239 }
240
241 return value;
242}
243
244/*
245 * Note that at boot, this probe only picks up one card at a time.
246 */
247
248static int __init do_ne2_probe(struct net_device *dev)
249{
250 static int current_mca_slot = -1;
251 int i;
252 int adapter_found = 0;
253
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400254 /* Do not check any supplied i/o locations.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 POS registers usually don't fail :) */
256
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400257 /* MCA cards have POS registers.
258 Autodetecting MCA cards is extremely simple.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 Just search for the card. */
260
261 for(i = 0; (ne2_adapters[i].name != NULL) && !adapter_found; i++) {
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400262 current_mca_slot =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263 mca_find_unused_adapter(ne2_adapters[i].id, 0);
264
265 if((current_mca_slot != MCA_NOTFOUND) && !adapter_found) {
266 int res;
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400267 mca_set_adapter_name(current_mca_slot,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 ne2_adapters[i].name);
269 mca_mark_as_used(current_mca_slot);
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400270
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 res = ne2_probe1(dev, current_mca_slot);
272 if (res)
273 mca_mark_as_unused(current_mca_slot);
274 return res;
275 }
276 }
277 return -ENODEV;
278}
279
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280#ifndef MODULE
281struct net_device * __init ne2_probe(int unit)
282{
283 struct net_device *dev = alloc_ei_netdev();
284 int err;
285
286 if (!dev)
287 return ERR_PTR(-ENOMEM);
288
289 sprintf(dev->name, "eth%d", unit);
290 netdev_boot_setup_check(dev);
291
292 err = do_ne2_probe(dev);
293 if (err)
294 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 return dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296out:
297 free_netdev(dev);
298 return ERR_PTR(err);
299}
300#endif
301
302static int ne2_procinfo(char *buf, int slot, struct net_device *dev)
303{
304 int len=0;
305
306 len += sprintf(buf+len, "The NE/2 Ethernet Adapter\n" );
307 len += sprintf(buf+len, "Driver written by Wim Dumon ");
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400308 len += sprintf(buf+len, "<wimpie@kotnet.org>\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309 len += sprintf(buf+len, "Modified by ");
310 len += sprintf(buf+len, "David Weinehall <tao@acc.umu.se>\n");
311 len += sprintf(buf+len, "and by Magnus Jonsson <bigfoot@acc.umu.se>\n");
312 len += sprintf(buf+len, "Based on the original NE2000 drivers\n" );
313 len += sprintf(buf+len, "Base IO: %#x\n", (unsigned int)dev->base_addr);
314 len += sprintf(buf+len, "IRQ : %d\n", dev->irq);
315
316#define HW_ADDR(i) dev->dev_addr[i]
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400317 len += sprintf(buf+len, "HW addr : %x:%x:%x:%x:%x:%x\n",
318 HW_ADDR(0), HW_ADDR(1), HW_ADDR(2),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 HW_ADDR(3), HW_ADDR(4), HW_ADDR(5) );
320#undef HW_ADDR
321
322 return len;
323}
324
325static int __init ne2_probe1(struct net_device *dev, int slot)
326{
327 int i, base_addr, irq, retval;
328 unsigned char POS;
329 unsigned char SA_prom[32];
330 const char *name = "NE/2";
331 int start_page, stop_page;
332 static unsigned version_printed;
333
334 if (ei_debug && version_printed++ == 0)
335 printk(version);
336
337 printk("NE/2 ethercard found in slot %d:", slot);
338
339 /* Read base IO and IRQ from the POS-registers */
340 POS = mca_read_stored_pos(slot, 2);
341 if(!(POS % 2)) {
342 printk(" disabled.\n");
343 return -ENODEV;
344 }
345
346 /* handle different POS register structure for D-Link card */
347
348 if (mca_read_stored_pos(slot, 0) == 0xea) {
349 base_addr = dlink_addresses[(POS >> 5) & 0x03];
350 irq = dlink_irqs[(POS >> 2) & 0x07];
351 }
352 else {
353 i = (POS & 0xE)>>1;
354 /* printk("Halleluja sdog, als er na de pijl een 1 staat is 1 - 1 == 0"
355 " en zou het moeten werken -> %d\n", i);
356 The above line was for remote testing, thanx to sdog ... */
357 base_addr = addresses[i - 1];
358 irq = irqs[(POS & 0x60)>>5];
359 }
360
361 if (!request_region(base_addr, NE_IO_EXTENT, DRV_NAME))
362 return -EBUSY;
363
364#ifdef DEBUG
365 printk("POS info : pos 2 = %#x ; base = %#x ; irq = %ld\n", POS,
366 base_addr, irq);
367#endif
368
369#ifndef CRYNWR_WAY
370 /* Reset the card the way they do it in the Crynwr packet driver */
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400371 for (i=0; i<8; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 outb(0x0, base_addr + NE_RESET);
373 inb(base_addr + NE_RESET);
374 outb(0x21, base_addr + NE_CMD);
375 if (inb(base_addr + NE_CMD) != 0x21) {
376 printk("NE/2 adapter not responding\n");
377 retval = -ENODEV;
378 goto out;
379 }
380
381 /* In the crynwr sources they do a RAM-test here. I skip it. I suppose
382 my RAM is okay. Suppose your memory is broken. Then this test
383 should fail and you won't be able to use your card. But if I do not
384 test, you won't be able to use your card, neither. So this test
385 won't help you. */
386
387#else /* _I_ never tested it this way .. Go ahead and try ...*/
388 /* Reset card. Who knows what dain-bramaged state it was left in. */
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400389 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 unsigned long reset_start_time = jiffies;
391
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400392 /* DON'T change these to inb_p/outb_p or reset will fail on
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393 clones.. */
394 outb(inb(base_addr + NE_RESET), base_addr + NE_RESET);
395
396 while ((inb_p(base_addr + EN0_ISR) & ENISR_RESET) == 0)
Marcelo Feitoza Parisiff5688a2006-01-09 18:37:15 -0800397 if (time_after(jiffies, reset_start_time + 2*HZ/100)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 printk(" not found (no reset ack).\n");
399 retval = -ENODEV;
400 goto out;
401 }
402
403 outb_p(0xff, base_addr + EN0_ISR); /* Ack all intr. */
404 }
405#endif
406
407
408 /* Read the 16 bytes of station address PROM.
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400409 We must first initialize registers, similar to
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 NS8390_init(eifdev, 0).
411 We can't reliably read the SAPROM address without this.
412 (I learned the hard way!). */
413 {
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400414 struct {
415 unsigned char value, offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 } program_seq[] = {
417 /* Select page 0 */
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400418 {E8390_NODMA+E8390_PAGE0+E8390_STOP, E8390_CMD},
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 {0x49, EN0_DCFG}, /* Set WORD-wide (0x49) access. */
420 {0x00, EN0_RCNTLO}, /* Clear the count regs. */
421 {0x00, EN0_RCNTHI},
422 {0x00, EN0_IMR}, /* Mask completion irq. */
423 {0xFF, EN0_ISR},
424 {E8390_RXOFF, EN0_RXCR}, /* 0x20 Set to monitor */
425 {E8390_TXOFF, EN0_TXCR}, /* 0x02 and loopback mode. */
426 {32, EN0_RCNTLO},
427 {0x00, EN0_RCNTHI},
428 {0x00, EN0_RSARLO}, /* DMA starting at 0x0000. */
429 {0x00, EN0_RSARHI},
430 {E8390_RREAD+E8390_START, E8390_CMD},
431 };
432
Denis Chengff8ac602007-09-02 18:30:18 +0800433 for (i = 0; i < ARRAY_SIZE(program_seq); i++)
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400434 outb_p(program_seq[i].value, base_addr +
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435 program_seq[i].offset);
436
437 }
438 for(i = 0; i < 6 /*sizeof(SA_prom)*/; i+=1) {
439 SA_prom[i] = inb(base_addr + NE_DATAPORT);
440 }
441
442 /* I don't know whether the previous sequence includes the general
443 board reset procedure, so better don't omit it and just overwrite
444 the garbage read from a DE-320 with correct stuff. */
445
446 if (mca_read_stored_pos(slot, 0) == 0xea) {
447 unsigned int v;
448
449 for (i = 0; i < 3; i++) {
450 v = dlink_get_eeprom(i, base_addr);
451 SA_prom[(i << 1) ] = v & 0xff;
452 SA_prom[(i << 1) + 1] = (v >> 8) & 0xff;
453 }
454 }
455
456 start_page = NESM_START_PG;
457 stop_page = NESM_STOP_PG;
458
459 dev->irq=irq;
460
461 /* Snarf the interrupt now. There's no point in waiting since we cannot
462 share and the board will usually be enabled. */
463 retval = request_irq(dev->irq, ei_interrupt, 0, DRV_NAME, dev);
464 if (retval) {
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400465 printk (" unable to get IRQ %d (irqval=%d).\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 dev->irq, retval);
467 goto out;
468 }
469
470 dev->base_addr = base_addr;
471
472 for(i = 0; i < ETHER_ADDR_LEN; i++) {
473 printk(" %2.2x", SA_prom[i]);
474 dev->dev_addr[i] = SA_prom[i];
475 }
476
477 printk("\n%s: %s found at %#x, using IRQ %d.\n",
478 dev->name, name, base_addr, dev->irq);
479
480 mca_set_adapter_procfn(slot, (MCA_ProcFn) ne2_procinfo, dev);
481
482 ei_status.name = name;
483 ei_status.tx_start_page = start_page;
484 ei_status.stop_page = stop_page;
485 ei_status.word16 = (2 == 2);
486
487 ei_status.rx_start_page = start_page + TX_PAGES;
488#ifdef PACKETBUF_MEMSIZE
489 /* Allow the packet buffer size to be overridden by know-it-alls. */
490 ei_status.stop_page = ei_status.tx_start_page + PACKETBUF_MEMSIZE;
491#endif
492
493 ei_status.reset_8390 = &ne_reset_8390;
494 ei_status.block_input = &ne_block_input;
495 ei_status.block_output = &ne_block_output;
496 ei_status.get_8390_hdr = &ne_get_8390_hdr;
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400497
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 ei_status.priv = slot;
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400499
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 dev->open = &ne_open;
501 dev->stop = &ne_close;
502#ifdef CONFIG_NET_POLL_CONTROLLER
503 dev->poll_controller = ei_poll;
504#endif
505 NS8390_init(dev, 0);
b1fc5502005-05-12 20:11:55 -0400506
507 retval = register_netdev(dev);
508 if (retval)
509 goto out1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 return 0;
b1fc5502005-05-12 20:11:55 -0400511out1:
512 mca_set_adapter_procfn( ei_status.priv, NULL, NULL);
513 free_irq(dev->irq, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514out:
515 release_region(base_addr, NE_IO_EXTENT);
516 return retval;
517}
518
519static int ne_open(struct net_device *dev)
520{
521 ei_open(dev);
522 return 0;
523}
524
525static int ne_close(struct net_device *dev)
526{
527 if (ei_debug > 1)
528 printk("%s: Shutting down ethercard.\n", dev->name);
529 ei_close(dev);
530 return 0;
531}
532
533/* Hard reset the card. This used to pause for the same period that a
534 8390 reset command required, but that shouldn't be necessary. */
535static void ne_reset_8390(struct net_device *dev)
536{
537 unsigned long reset_start_time = jiffies;
538
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400539 if (ei_debug > 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540 printk("resetting the 8390 t=%ld...", jiffies);
541
542 /* DON'T change these to inb_p/outb_p or reset will fail on clones. */
543 outb(inb(NE_BASE + NE_RESET), NE_BASE + NE_RESET);
544
545 ei_status.txing = 0;
546 ei_status.dmaing = 0;
547
548 /* This check _should_not_ be necessary, omit eventually. */
549 while ((inb_p(NE_BASE+EN0_ISR) & ENISR_RESET) == 0)
Marcelo Feitoza Parisiff5688a2006-01-09 18:37:15 -0800550 if (time_after(jiffies, reset_start_time + 2*HZ/100)) {
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400551 printk("%s: ne_reset_8390() did not complete.\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552 dev->name);
553 break;
554 }
555 outb_p(ENISR_RESET, NE_BASE + EN0_ISR); /* Ack intr. */
556}
557
558/* Grab the 8390 specific header. Similar to the block_input routine, but
559 we don't need to be concerned with ring wrap as the header will be at
560 the start of a page, so we optimize accordingly. */
561
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400562static void ne_get_8390_hdr(struct net_device *dev, struct e8390_pkt_hdr *hdr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563 int ring_page)
564{
565
566 int nic_base = dev->base_addr;
567
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400568 /* This *shouldn't* happen.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569 If it does, it's the last thing you'll see */
570 if (ei_status.dmaing) {
571 printk("%s: DMAing conflict in ne_get_8390_hdr "
572 "[DMAstat:%d][irqlock:%d].\n",
573 dev->name, ei_status.dmaing, ei_status.irqlock);
574 return;
575 }
576
577 ei_status.dmaing |= 0x01;
578 outb_p(E8390_NODMA+E8390_PAGE0+E8390_START, nic_base+ NE_CMD);
579 outb_p(sizeof(struct e8390_pkt_hdr), nic_base + EN0_RCNTLO);
580 outb_p(0, nic_base + EN0_RCNTHI);
581 outb_p(0, nic_base + EN0_RSARLO); /* On page boundary */
582 outb_p(ring_page, nic_base + EN0_RSARHI);
583 outb_p(E8390_RREAD+E8390_START, nic_base + NE_CMD);
584
585 if (ei_status.word16)
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400586 insw(NE_BASE + NE_DATAPORT, hdr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587 sizeof(struct e8390_pkt_hdr)>>1);
588 else
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400589 insb(NE_BASE + NE_DATAPORT, hdr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590 sizeof(struct e8390_pkt_hdr));
591
592 outb_p(ENISR_RDC, nic_base + EN0_ISR); /* Ack intr. */
593 ei_status.dmaing &= ~0x01;
594}
595
596/* Block input and output, similar to the Crynwr packet driver. If you
597 are porting to a new ethercard, look at the packet driver source for
598 hints. The NEx000 doesn't share the on-board packet memory -- you have
599 to put the packet out through the "remote DMA" dataport using outb. */
600
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400601static void ne_block_input(struct net_device *dev, int count, struct sk_buff *skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602 int ring_offset)
603{
604#ifdef NE_SANITY_CHECK
605 int xfer_count = count;
606#endif
607 int nic_base = dev->base_addr;
608 char *buf = skb->data;
609
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400610 /* This *shouldn't* happen.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611 If it does, it's the last thing you'll see */
612 if (ei_status.dmaing) {
613 printk("%s: DMAing conflict in ne_block_input "
614 "[DMAstat:%d][irqlock:%d].\n",
615 dev->name, ei_status.dmaing, ei_status.irqlock);
616 return;
617 }
618 ei_status.dmaing |= 0x01;
619 outb_p(E8390_NODMA+E8390_PAGE0+E8390_START, nic_base+ NE_CMD);
620 outb_p(count & 0xff, nic_base + EN0_RCNTLO);
621 outb_p(count >> 8, nic_base + EN0_RCNTHI);
622 outb_p(ring_offset & 0xff, nic_base + EN0_RSARLO);
623 outb_p(ring_offset >> 8, nic_base + EN0_RSARHI);
624 outb_p(E8390_RREAD+E8390_START, nic_base + NE_CMD);
625 if (ei_status.word16) {
626 insw(NE_BASE + NE_DATAPORT,buf,count>>1);
627 if (count & 0x01) {
628 buf[count-1] = inb(NE_BASE + NE_DATAPORT);
629#ifdef NE_SANITY_CHECK
630 xfer_count++;
631#endif
632 }
633 } else {
634 insb(NE_BASE + NE_DATAPORT, buf, count);
635 }
636
637#ifdef NE_SANITY_CHECK
638 /* This was for the ALPHA version only, but enough people have
639 been encountering problems so it is still here. If you see
640 this message you either 1) have a slightly incompatible clone
641 or 2) have noise/speed problems with your bus. */
642 if (ei_debug > 1) { /* DMA termination address check... */
643 int addr, tries = 20;
644 do {
645 /* DON'T check for 'inb_p(EN0_ISR) & ENISR_RDC' here
646 -- it's broken for Rx on some cards! */
647 int high = inb_p(nic_base + EN0_RSARHI);
648 int low = inb_p(nic_base + EN0_RSARLO);
649 addr = (high << 8) + low;
650 if (((ring_offset + xfer_count) & 0xff) == low)
651 break;
652 } while (--tries > 0);
653 if (tries <= 0)
654 printk("%s: RX transfer address mismatch,"
655 "%#4.4x (expected) vs. %#4.4x (actual).\n",
656 dev->name, ring_offset + xfer_count, addr);
657 }
658#endif
659 outb_p(ENISR_RDC, nic_base + EN0_ISR); /* Ack intr. */
660 ei_status.dmaing &= ~0x01;
661}
662
663static void ne_block_output(struct net_device *dev, int count,
664 const unsigned char *buf, const int start_page)
665{
666 int nic_base = NE_BASE;
667 unsigned long dma_start;
668#ifdef NE_SANITY_CHECK
669 int retries = 0;
670#endif
671
672 /* Round the count up for word writes. Do we need to do this?
673 What effect will an odd byte count have on the 8390?
674 I should check someday. */
675 if (ei_status.word16 && (count & 0x01))
676 count++;
677
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400678 /* This *shouldn't* happen.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679 If it does, it's the last thing you'll see */
680 if (ei_status.dmaing) {
681 printk("%s: DMAing conflict in ne_block_output."
682 "[DMAstat:%d][irqlock:%d]\n",
683 dev->name, ei_status.dmaing, ei_status.irqlock);
684 return;
685 }
686 ei_status.dmaing |= 0x01;
687 /* We should already be in page 0, but to be safe... */
688 outb_p(E8390_PAGE0+E8390_START+E8390_NODMA, nic_base + NE_CMD);
689
690#ifdef NE_SANITY_CHECK
691retry:
692#endif
693
694#ifdef NE8390_RW_BUGFIX
695 /* Handle the read-before-write bug the same way as the
696 Crynwr packet driver -- the NatSemi method doesn't work.
697 Actually this doesn't always work either, but if you have
698 problems with your NEx000 this is better than nothing! */
699 outb_p(0x42, nic_base + EN0_RCNTLO);
700 outb_p(0x00, nic_base + EN0_RCNTHI);
701 outb_p(0x42, nic_base + EN0_RSARLO);
702 outb_p(0x00, nic_base + EN0_RSARHI);
703 outb_p(E8390_RREAD+E8390_START, nic_base + NE_CMD);
704 /* Make certain that the dummy read has occurred. */
705 SLOW_DOWN_IO;
706 SLOW_DOWN_IO;
707 SLOW_DOWN_IO;
708#endif
709
710 outb_p(ENISR_RDC, nic_base + EN0_ISR);
711
712 /* Now the normal output. */
713 outb_p(count & 0xff, nic_base + EN0_RCNTLO);
714 outb_p(count >> 8, nic_base + EN0_RCNTHI);
715 outb_p(0x00, nic_base + EN0_RSARLO);
716 outb_p(start_page, nic_base + EN0_RSARHI);
717
718 outb_p(E8390_RWRITE+E8390_START, nic_base + NE_CMD);
719 if (ei_status.word16) {
720 outsw(NE_BASE + NE_DATAPORT, buf, count>>1);
721 } else {
722 outsb(NE_BASE + NE_DATAPORT, buf, count);
723 }
724
725 dma_start = jiffies;
726
727#ifdef NE_SANITY_CHECK
728 /* This was for the ALPHA version only, but enough people have
729 been encountering problems so it is still here. */
730
731 if (ei_debug > 1) { /* DMA termination address check... */
732 int addr, tries = 20;
733 do {
734 int high = inb_p(nic_base + EN0_RSARHI);
735 int low = inb_p(nic_base + EN0_RSARLO);
736 addr = (high << 8) + low;
737 if ((start_page << 8) + count == addr)
738 break;
739 } while (--tries > 0);
740 if (tries <= 0) {
741 printk("%s: Tx packet transfer address mismatch,"
742 "%#4.4x (expected) vs. %#4.4x (actual).\n",
743 dev->name, (start_page << 8) + count, addr);
744 if (retries++ == 0)
745 goto retry;
746 }
747 }
748#endif
749
750 while ((inb_p(nic_base + EN0_ISR) & ENISR_RDC) == 0)
Marcelo Feitoza Parisiff5688a2006-01-09 18:37:15 -0800751 if (time_after(jiffies, dma_start + 2*HZ/100)) { /* 20ms */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752 printk("%s: timeout waiting for Tx RDC.\n", dev->name);
753 ne_reset_8390(dev);
754 NS8390_init(dev,1);
755 break;
756 }
757
758 outb_p(ENISR_RDC, nic_base + EN0_ISR); /* Ack intr. */
759 ei_status.dmaing &= ~0x01;
760 return;
761}
762
763
764#ifdef MODULE
765#define MAX_NE_CARDS 4 /* Max number of NE cards per module */
766static struct net_device *dev_ne[MAX_NE_CARDS];
767static int io[MAX_NE_CARDS];
768static int irq[MAX_NE_CARDS];
769static int bad[MAX_NE_CARDS]; /* 0xbad = bad sig or no reset ack */
770MODULE_LICENSE("GPL");
771
772module_param_array(io, int, NULL, 0);
773module_param_array(irq, int, NULL, 0);
774module_param_array(bad, int, NULL, 0);
775MODULE_PARM_DESC(io, "(ignored)");
776MODULE_PARM_DESC(irq, "(ignored)");
777MODULE_PARM_DESC(bad, "(ignored)");
778
779/* Module code fixed by David Weinehall */
780
Randy.Dunlap5d1f16c2006-06-10 12:13:35 -0700781int __init init_module(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782{
783 struct net_device *dev;
784 int this_dev, found = 0;
785
786 for (this_dev = 0; this_dev < MAX_NE_CARDS; this_dev++) {
787 dev = alloc_ei_netdev();
788 if (!dev)
789 break;
790 dev->irq = irq[this_dev];
791 dev->mem_end = bad[this_dev];
792 dev->base_addr = io[this_dev];
793 if (do_ne2_probe(dev) == 0) {
b1fc5502005-05-12 20:11:55 -0400794 dev_ne[found++] = dev;
795 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796 }
797 free_netdev(dev);
798 break;
799 }
800 if (found)
801 return 0;
802 printk(KERN_WARNING "ne2.c: No NE/2 card found\n");
803 return -ENXIO;
804}
805
Denis Vlasenko64916f12006-01-05 22:45:47 -0800806static void cleanup_card(struct net_device *dev)
807{
808 mca_mark_as_unused(ei_status.priv);
809 mca_set_adapter_procfn( ei_status.priv, NULL, NULL);
810 free_irq(dev->irq, dev);
811 release_region(dev->base_addr, NE_IO_EXTENT);
812}
813
Al Viroafc8eb42006-06-14 18:50:53 -0400814void __exit cleanup_module(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815{
816 int this_dev;
817
818 for (this_dev = 0; this_dev < MAX_NE_CARDS; this_dev++) {
819 struct net_device *dev = dev_ne[this_dev];
820 if (dev) {
821 unregister_netdev(dev);
822 cleanup_card(dev);
823 free_netdev(dev);
824 }
825 }
826}
827#endif /* MODULE */