]> git.karo-electronics.de Git - karo-tx-linux.git/blob - drivers/net/ethernet/8390/ne2.c
Merge branch 'master' of git://git.kernel.org/pub/scm/linux/kernel/git/linville/wireless
[karo-tx-linux.git] / drivers / net / ethernet / 8390 / ne2.c
1 /* 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)
31    - added support for Compex ENET-16MC/P (experimental)
32
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)
47    - pass 'dev' as last parameter of request_irq in stead of 'NULL'
48
49    Wed Feb  7 21:24:00 CET 2001 (Alfred Arnold)
50    - added support for the D-Link DE-320CT
51
52    *    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
60 static 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/string.h>
70 #include <linux/errno.h>
71 #include <linux/init.h>
72 #include <linux/mca-legacy.h>
73 #include <linux/netdevice.h>
74 #include <linux/etherdevice.h>
75 #include <linux/skbuff.h>
76 #include <linux/bitops.h>
77 #include <linux/jiffies.h>
78
79 #include <asm/io.h>
80 #include <asm/dma.h>
81
82 #include "8390.h"
83
84 #define DRV_NAME "ne2"
85
86 /* Some defines that people can play with if so inclined. */
87
88 /* Do we perform extra sanity checks on stuff ? */
89 /* #define NE_SANITY_CHECK */
90
91 /* Do we implement the read before write bugfix ? */
92 /* #define NE_RW_BUGFIX */
93
94 /* Do we have a non std. amount of memory? (in units of 256 byte pages) */
95 /* #define PACKETBUF_MEMSIZE    0x40 */
96
97
98 /* ---- No user-serviceable parts below ---- */
99
100 #define NE_BASE  (dev->base_addr)
101 #define NE_CMD          0x00
102 #define NE_DATAPORT     0x10    /* NatSemi-defined port window offset. */
103 #define NE_RESET        0x20    /* Issue a read to reset, a write to clear. */
104 #define NE_IO_EXTENT    0x30
105
106 #define NE1SM_START_PG  0x20    /* First page of TX buffer */
107 #define NE1SM_STOP_PG   0x40    /* Last page +1 of RX ring */
108 #define NESM_START_PG   0x40    /* First page of TX buffer */
109 #define NESM_STOP_PG    0x80    /* Last page +1 of RX ring */
110
111 /* From the .ADF file: */
112 static unsigned int addresses[7] __initdata =
113                 {0x1000, 0x2020, 0x8020, 0xa0a0, 0xb0b0, 0xc0c0, 0xc3d0};
114 static int irqs[4] __initdata = {3, 4, 5, 9};
115
116 /* From the D-Link ADF file: */
117 static unsigned int dlink_addresses[4] __initdata =
118                 {0x300, 0x320, 0x340, 0x360};
119 static int dlink_irqs[8] __initdata = {3, 4, 5, 9, 10, 11, 14, 15};
120
121 struct ne2_adapters_t {
122         unsigned int    id;
123         char            *name;
124 };
125
126 static struct ne2_adapters_t ne2_adapters[] __initdata = {
127         { 0x6354, "Arco Ethernet Adapter AE/2" },
128         { 0x70DE, "Compex ENET-16 MC/P" },
129         { 0x7154, "Novell Ethernet Adapter NE/2" },
130         { 0x56ea, "D-Link DE-320CT" },
131         { 0x0000, NULL }
132 };
133
134 extern int netcard_probe(struct net_device *dev);
135
136 static int ne2_probe1(struct net_device *dev, int slot);
137
138 static void ne_reset_8390(struct net_device *dev);
139 static void ne_get_8390_hdr(struct net_device *dev, struct e8390_pkt_hdr *hdr,
140                 int ring_page);
141 static void ne_block_input(struct net_device *dev, int count,
142                 struct sk_buff *skb, int ring_offset);
143 static void ne_block_output(struct net_device *dev, const int count,
144                 const unsigned char *buf, const int start_page);
145
146
147 /*
148  * special code to read the DE-320's MAC address EEPROM.  In contrast to a
149  * standard NE design, this is a serial EEPROM (93C46) that has to be read
150  * bit by bit.  The EEPROM cotrol port at base + 0x1e has the following
151  * layout:
152  *
153  * Bit 0 = Data out (read from EEPROM)
154  * Bit 1 = Data in  (write to EEPROM)
155  * Bit 2 = Clock
156  * Bit 3 = Chip Select
157  * Bit 7 = ~50 kHz clock for defined delays
158  *
159  */
160
161 static void __init dlink_put_eeprom(unsigned char value, unsigned int addr)
162 {
163         int z;
164         unsigned char v1, v2;
165
166         /* write the value to the NIC EEPROM register */
167
168         outb(value, addr + 0x1e);
169
170         /* now wait the clock line to toggle twice.  Effectively, we are
171            waiting (at least) for one clock cycle */
172
173         for (z = 0; z < 2; z++) {
174                 do {
175                         v1 = inb(addr + 0x1e);
176                         v2 = inb(addr + 0x1e);
177                 }
178                 while (!((v1 ^ v2) & 0x80));
179         }
180 }
181
182 static void __init dlink_send_eeprom_bit(unsigned int bit, unsigned int addr)
183 {
184         /* shift data bit into correct position */
185
186         bit = bit << 1;
187
188         /* write value, keep clock line high for two cycles */
189
190         dlink_put_eeprom(0x09 | bit, addr);
191         dlink_put_eeprom(0x0d | bit, addr);
192         dlink_put_eeprom(0x0d | bit, addr);
193         dlink_put_eeprom(0x09 | bit, addr);
194 }
195
196 static void __init dlink_send_eeprom_word(unsigned int value, unsigned int len, unsigned int addr)
197 {
198         int z;
199
200         /* adjust bits so that they are left-aligned in a 16-bit-word */
201
202         value = value << (16 - len);
203
204         /* shift bits out to the EEPROM */
205
206         for (z = 0; z < len; z++) {
207                 dlink_send_eeprom_bit((value & 0x8000) >> 15, addr);
208                 value = value << 1;
209         }
210 }
211
212 static unsigned int __init dlink_get_eeprom(unsigned int eeaddr, unsigned int addr)
213 {
214         int z;
215         unsigned int value = 0;
216
217         /* pull the CS line low for a moment.  This resets the EEPROM-
218            internal logic, and makes it ready for a new command. */
219
220         dlink_put_eeprom(0x01, addr);
221         dlink_put_eeprom(0x09, addr);
222
223         /* send one start bit, read command (1 - 0), plus the address to
224            the EEPROM */
225
226         dlink_send_eeprom_word(0x0180 | (eeaddr & 0x3f), 9, addr);
227
228         /* get the data word.  We clock by sending 0s to the EEPROM, which
229            get ignored during the read process */
230
231         for (z = 0; z < 16; z++) {
232                 dlink_send_eeprom_bit(0, addr);
233                 value = (value << 1) | (inb(addr + 0x1e) & 0x01);
234         }
235
236         return value;
237 }
238
239 /*
240  * Note that at boot, this probe only picks up one card at a time.
241  */
242
243 static int __init do_ne2_probe(struct net_device *dev)
244 {
245         static int current_mca_slot = -1;
246         int i;
247         int adapter_found = 0;
248
249         /* Do not check any supplied i/o locations.
250            POS registers usually don't fail :) */
251
252         /* MCA cards have POS registers.
253            Autodetecting MCA cards is extremely simple.
254            Just search for the card. */
255
256         for(i = 0; (ne2_adapters[i].name != NULL) && !adapter_found; i++) {
257                 current_mca_slot =
258                         mca_find_unused_adapter(ne2_adapters[i].id, 0);
259
260                 if((current_mca_slot != MCA_NOTFOUND) && !adapter_found) {
261                         int res;
262                         mca_set_adapter_name(current_mca_slot,
263                                         ne2_adapters[i].name);
264                         mca_mark_as_used(current_mca_slot);
265
266                         res = ne2_probe1(dev, current_mca_slot);
267                         if (res)
268                                 mca_mark_as_unused(current_mca_slot);
269                         return res;
270                 }
271         }
272         return -ENODEV;
273 }
274
275 #ifndef MODULE
276 struct net_device * __init ne2_probe(int unit)
277 {
278         struct net_device *dev = alloc_eip_netdev();
279         int err;
280
281         if (!dev)
282                 return ERR_PTR(-ENOMEM);
283
284         sprintf(dev->name, "eth%d", unit);
285         netdev_boot_setup_check(dev);
286
287         err = do_ne2_probe(dev);
288         if (err)
289                 goto out;
290         return dev;
291 out:
292         free_netdev(dev);
293         return ERR_PTR(err);
294 }
295 #endif
296
297 static int ne2_procinfo(char *buf, int slot, struct net_device *dev)
298 {
299         int len=0;
300
301         len += sprintf(buf+len, "The NE/2 Ethernet Adapter\n" );
302         len += sprintf(buf+len, "Driver written by Wim Dumon ");
303         len += sprintf(buf+len, "<wimpie@kotnet.org>\n");
304         len += sprintf(buf+len, "Modified by ");
305         len += sprintf(buf+len, "David Weinehall <tao@acc.umu.se>\n");
306         len += sprintf(buf+len, "and by Magnus Jonsson <bigfoot@acc.umu.se>\n");
307         len += sprintf(buf+len, "Based on the original NE2000 drivers\n" );
308         len += sprintf(buf+len, "Base IO: %#x\n", (unsigned int)dev->base_addr);
309         len += sprintf(buf+len, "IRQ    : %d\n", dev->irq);
310         len += sprintf(buf+len, "HW addr : %pM\n", dev->dev_addr);
311
312         return len;
313 }
314
315 static int __init ne2_probe1(struct net_device *dev, int slot)
316 {
317         int i, base_addr, irq, retval;
318         unsigned char POS;
319         unsigned char SA_prom[32];
320         const char *name = "NE/2";
321         int start_page, stop_page;
322         static unsigned version_printed;
323
324         if (ei_debug && version_printed++ == 0)
325                 printk(version);
326
327         printk("NE/2 ethercard found in slot %d:", slot);
328
329         /* Read base IO and IRQ from the POS-registers */
330         POS = mca_read_stored_pos(slot, 2);
331         if(!(POS % 2)) {
332                 printk(" disabled.\n");
333                 return -ENODEV;
334         }
335
336         /* handle different POS register structure for D-Link card */
337
338         if (mca_read_stored_pos(slot, 0) == 0xea) {
339                 base_addr = dlink_addresses[(POS >> 5) & 0x03];
340                 irq = dlink_irqs[(POS >> 2) & 0x07];
341         }
342         else {
343                 i = (POS & 0xE)>>1;
344                 /* printk("Halleluja sdog, als er na de pijl een 1 staat is 1 - 1 == 0"
345                 " en zou het moeten werken -> %d\n", i);
346                 The above line was for remote testing, thanx to sdog ... */
347                 base_addr = addresses[i - 1];
348                 irq = irqs[(POS & 0x60)>>5];
349         }
350
351         if (!request_region(base_addr, NE_IO_EXTENT, DRV_NAME))
352                 return -EBUSY;
353
354 #ifdef DEBUG
355         printk("POS info : pos 2 = %#x ; base = %#x ; irq = %ld\n", POS,
356                         base_addr, irq);
357 #endif
358
359 #ifndef CRYNWR_WAY
360         /* Reset the card the way they do it in the Crynwr packet driver */
361         for (i=0; i<8; i++)
362                 outb(0x0, base_addr + NE_RESET);
363         inb(base_addr + NE_RESET);
364         outb(0x21, base_addr + NE_CMD);
365         if (inb(base_addr + NE_CMD) != 0x21) {
366                 printk("NE/2 adapter not responding\n");
367                 retval = -ENODEV;
368                 goto out;
369         }
370
371         /* In the crynwr sources they do a RAM-test here. I skip it. I suppose
372            my RAM is okay.  Suppose your memory is broken.  Then this test
373            should fail and you won't be able to use your card.  But if I do not
374            test, you won't be able to use your card, neither.  So this test
375            won't help you. */
376
377 #else  /* _I_ never tested it this way .. Go ahead and try ...*/
378         /* Reset card. Who knows what dain-bramaged state it was left in. */
379         {
380                 unsigned long reset_start_time = jiffies;
381
382                 /* DON'T change these to inb_p/outb_p or reset will fail on
383                    clones.. */
384                 outb(inb(base_addr + NE_RESET), base_addr + NE_RESET);
385
386                 while ((inb_p(base_addr + EN0_ISR) & ENISR_RESET) == 0)
387                         if (time_after(jiffies, reset_start_time + 2*HZ/100)) {
388                                 printk(" not found (no reset ack).\n");
389                                 retval = -ENODEV;
390                                 goto out;
391                         }
392
393                 outb_p(0xff, base_addr + EN0_ISR);         /* Ack all intr. */
394         }
395 #endif
396
397
398         /* Read the 16 bytes of station address PROM.
399            We must first initialize registers, similar to
400            NS8390p_init(eifdev, 0).
401            We can't reliably read the SAPROM address without this.
402            (I learned the hard way!). */
403         {
404                 struct {
405                         unsigned char value, offset;
406                 } program_seq[] = {
407                                                 /* Select page 0 */
408                         {E8390_NODMA+E8390_PAGE0+E8390_STOP, E8390_CMD},
409                         {0x49,  EN0_DCFG},  /* Set WORD-wide (0x49) access. */
410                         {0x00,  EN0_RCNTLO},  /* Clear the count regs. */
411                         {0x00,  EN0_RCNTHI},
412                         {0x00,  EN0_IMR},  /* Mask completion irq. */
413                         {0xFF,  EN0_ISR},
414                         {E8390_RXOFF, EN0_RXCR},  /* 0x20  Set to monitor */
415                         {E8390_TXOFF, EN0_TXCR},  /* 0x02  and loopback mode. */
416                         {32,    EN0_RCNTLO},
417                         {0x00,  EN0_RCNTHI},
418                         {0x00,  EN0_RSARLO},  /* DMA starting at 0x0000. */
419                         {0x00,  EN0_RSARHI},
420                         {E8390_RREAD+E8390_START, E8390_CMD},
421                 };
422
423                 for (i = 0; i < ARRAY_SIZE(program_seq); i++)
424                         outb_p(program_seq[i].value, base_addr +
425                                 program_seq[i].offset);
426
427         }
428         for(i = 0; i < 6 /*sizeof(SA_prom)*/; i+=1) {
429                 SA_prom[i] = inb(base_addr + NE_DATAPORT);
430         }
431
432         /* I don't know whether the previous sequence includes the general
433            board reset procedure, so better don't omit it and just overwrite
434            the garbage read from a DE-320 with correct stuff. */
435
436         if (mca_read_stored_pos(slot, 0) == 0xea) {
437                 unsigned int v;
438
439                 for (i = 0; i < 3; i++) {
440                         v = dlink_get_eeprom(i, base_addr);
441                         SA_prom[(i << 1)    ] = v & 0xff;
442                         SA_prom[(i << 1) + 1] = (v >> 8) & 0xff;
443                 }
444         }
445
446         start_page = NESM_START_PG;
447         stop_page = NESM_STOP_PG;
448
449         dev->irq=irq;
450
451         /* Snarf the interrupt now.  There's no point in waiting since we cannot
452            share and the board will usually be enabled. */
453         retval = request_irq(dev->irq, eip_interrupt, 0, DRV_NAME, dev);
454         if (retval) {
455                 printk (" unable to get IRQ %d (irqval=%d).\n",
456                                 dev->irq, retval);
457                 goto out;
458         }
459
460         dev->base_addr = base_addr;
461
462         for (i = 0; i < ETH_ALEN; i++)
463                 dev->dev_addr[i] = SA_prom[i];
464
465         printk(" %pM\n", dev->dev_addr);
466
467         printk("%s: %s found at %#x, using IRQ %d.\n",
468                         dev->name, name, base_addr, dev->irq);
469
470         mca_set_adapter_procfn(slot, (MCA_ProcFn) ne2_procinfo, dev);
471
472         ei_status.name = name;
473         ei_status.tx_start_page = start_page;
474         ei_status.stop_page = stop_page;
475         ei_status.word16 = (2 == 2);
476
477         ei_status.rx_start_page = start_page + TX_PAGES;
478 #ifdef PACKETBUF_MEMSIZE
479         /* Allow the packet buffer size to be overridden by know-it-alls. */
480         ei_status.stop_page = ei_status.tx_start_page + PACKETBUF_MEMSIZE;
481 #endif
482
483         ei_status.reset_8390 = &ne_reset_8390;
484         ei_status.block_input = &ne_block_input;
485         ei_status.block_output = &ne_block_output;
486         ei_status.get_8390_hdr = &ne_get_8390_hdr;
487
488         ei_status.priv = slot;
489
490         dev->netdev_ops = &eip_netdev_ops;
491         NS8390p_init(dev, 0);
492
493         retval = register_netdev(dev);
494         if (retval)
495                 goto out1;
496         return 0;
497 out1:
498         mca_set_adapter_procfn( ei_status.priv, NULL, NULL);
499         free_irq(dev->irq, dev);
500 out:
501         release_region(base_addr, NE_IO_EXTENT);
502         return retval;
503 }
504
505 /* Hard reset the card.  This used to pause for the same period that a
506    8390 reset command required, but that shouldn't be necessary. */
507 static void ne_reset_8390(struct net_device *dev)
508 {
509         unsigned long reset_start_time = jiffies;
510
511         if (ei_debug > 1)
512                 printk("resetting the 8390 t=%ld...", jiffies);
513
514         /* DON'T change these to inb_p/outb_p or reset will fail on clones. */
515         outb(inb(NE_BASE + NE_RESET), NE_BASE + NE_RESET);
516
517         ei_status.txing = 0;
518         ei_status.dmaing = 0;
519
520         /* This check _should_not_ be necessary, omit eventually. */
521         while ((inb_p(NE_BASE+EN0_ISR) & ENISR_RESET) == 0)
522                 if (time_after(jiffies, reset_start_time + 2*HZ/100)) {
523                         printk("%s: ne_reset_8390() did not complete.\n",
524                                         dev->name);
525                         break;
526                 }
527         outb_p(ENISR_RESET, NE_BASE + EN0_ISR); /* Ack intr. */
528 }
529
530 /* Grab the 8390 specific header. Similar to the block_input routine, but
531    we don't need to be concerned with ring wrap as the header will be at
532    the start of a page, so we optimize accordingly. */
533
534 static void ne_get_8390_hdr(struct net_device *dev, struct e8390_pkt_hdr *hdr,
535                 int ring_page)
536 {
537
538         int nic_base = dev->base_addr;
539
540         /* This *shouldn't* happen.
541            If it does, it's the last thing you'll see */
542         if (ei_status.dmaing) {
543                 printk("%s: DMAing conflict in ne_get_8390_hdr "
544                                 "[DMAstat:%d][irqlock:%d].\n",
545                                 dev->name, ei_status.dmaing, ei_status.irqlock);
546                 return;
547         }
548
549         ei_status.dmaing |= 0x01;
550         outb_p(E8390_NODMA+E8390_PAGE0+E8390_START, nic_base+ NE_CMD);
551         outb_p(sizeof(struct e8390_pkt_hdr), nic_base + EN0_RCNTLO);
552         outb_p(0, nic_base + EN0_RCNTHI);
553         outb_p(0, nic_base + EN0_RSARLO);               /* On page boundary */
554         outb_p(ring_page, nic_base + EN0_RSARHI);
555         outb_p(E8390_RREAD+E8390_START, nic_base + NE_CMD);
556
557         if (ei_status.word16)
558                 insw(NE_BASE + NE_DATAPORT, hdr,
559                                 sizeof(struct e8390_pkt_hdr)>>1);
560         else
561                 insb(NE_BASE + NE_DATAPORT, hdr,
562                                 sizeof(struct e8390_pkt_hdr));
563
564         outb_p(ENISR_RDC, nic_base + EN0_ISR);  /* Ack intr. */
565         ei_status.dmaing &= ~0x01;
566 }
567
568 /* Block input and output, similar to the Crynwr packet driver.  If you
569    are porting to a new ethercard, look at the packet driver source for
570    hints. The NEx000 doesn't share the on-board packet memory -- you have
571    to put the packet out through the "remote DMA" dataport using outb. */
572
573 static void ne_block_input(struct net_device *dev, int count, struct sk_buff *skb,
574                 int ring_offset)
575 {
576 #ifdef NE_SANITY_CHECK
577         int xfer_count = count;
578 #endif
579         int nic_base = dev->base_addr;
580         char *buf = skb->data;
581
582         /* This *shouldn't* happen.
583            If it does, it's the last thing you'll see */
584         if (ei_status.dmaing) {
585                 printk("%s: DMAing conflict in ne_block_input "
586                                 "[DMAstat:%d][irqlock:%d].\n",
587                                 dev->name, ei_status.dmaing, ei_status.irqlock);
588                 return;
589         }
590         ei_status.dmaing |= 0x01;
591         outb_p(E8390_NODMA+E8390_PAGE0+E8390_START, nic_base+ NE_CMD);
592         outb_p(count & 0xff, nic_base + EN0_RCNTLO);
593         outb_p(count >> 8, nic_base + EN0_RCNTHI);
594         outb_p(ring_offset & 0xff, nic_base + EN0_RSARLO);
595         outb_p(ring_offset >> 8, nic_base + EN0_RSARHI);
596         outb_p(E8390_RREAD+E8390_START, nic_base + NE_CMD);
597         if (ei_status.word16) {
598                 insw(NE_BASE + NE_DATAPORT,buf,count>>1);
599                 if (count & 0x01) {
600                         buf[count-1] = inb(NE_BASE + NE_DATAPORT);
601 #ifdef NE_SANITY_CHECK
602                         xfer_count++;
603 #endif
604                 }
605         } else {
606                 insb(NE_BASE + NE_DATAPORT, buf, count);
607         }
608
609 #ifdef NE_SANITY_CHECK
610         /* This was for the ALPHA version only, but enough people have
611            been encountering problems so it is still here.  If you see
612            this message you either 1) have a slightly incompatible clone
613            or 2) have noise/speed problems with your bus. */
614         if (ei_debug > 1) {     /* DMA termination address check... */
615                 int addr, tries = 20;
616                 do {
617                         /* DON'T check for 'inb_p(EN0_ISR) & ENISR_RDC' here
618                            -- it's broken for Rx on some cards! */
619                         int high = inb_p(nic_base + EN0_RSARHI);
620                         int low = inb_p(nic_base + EN0_RSARLO);
621                         addr = (high << 8) + low;
622                         if (((ring_offset + xfer_count) & 0xff) == low)
623                                 break;
624                 } while (--tries > 0);
625                 if (tries <= 0)
626                         printk("%s: RX transfer address mismatch,"
627                                 "%#4.4x (expected) vs. %#4.4x (actual).\n",
628                                 dev->name, ring_offset + xfer_count, addr);
629         }
630 #endif
631         outb_p(ENISR_RDC, nic_base + EN0_ISR);  /* Ack intr. */
632         ei_status.dmaing &= ~0x01;
633 }
634
635 static void ne_block_output(struct net_device *dev, int count,
636                 const unsigned char *buf, const int start_page)
637 {
638         int nic_base = NE_BASE;
639         unsigned long dma_start;
640 #ifdef NE_SANITY_CHECK
641         int retries = 0;
642 #endif
643
644         /* Round the count up for word writes. Do we need to do this?
645            What effect will an odd byte count have on the 8390?
646            I should check someday. */
647         if (ei_status.word16 && (count & 0x01))
648                 count++;
649
650         /* This *shouldn't* happen.
651            If it does, it's the last thing you'll see */
652         if (ei_status.dmaing) {
653                 printk("%s: DMAing conflict in ne_block_output."
654                                 "[DMAstat:%d][irqlock:%d]\n",
655                                 dev->name, ei_status.dmaing, ei_status.irqlock);
656                 return;
657         }
658         ei_status.dmaing |= 0x01;
659         /* We should already be in page 0, but to be safe... */
660         outb_p(E8390_PAGE0+E8390_START+E8390_NODMA, nic_base + NE_CMD);
661
662 #ifdef NE_SANITY_CHECK
663 retry:
664 #endif
665
666 #ifdef NE8390_RW_BUGFIX
667         /* Handle the read-before-write bug the same way as the
668            Crynwr packet driver -- the NatSemi method doesn't work.
669            Actually this doesn't always work either, but if you have
670            problems with your NEx000 this is better than nothing! */
671         outb_p(0x42, nic_base + EN0_RCNTLO);
672         outb_p(0x00, nic_base + EN0_RCNTHI);
673         outb_p(0x42, nic_base + EN0_RSARLO);
674         outb_p(0x00, nic_base + EN0_RSARHI);
675         outb_p(E8390_RREAD+E8390_START, nic_base + NE_CMD);
676         /* Make certain that the dummy read has occurred. */
677         SLOW_DOWN_IO;
678         SLOW_DOWN_IO;
679         SLOW_DOWN_IO;
680 #endif
681
682         outb_p(ENISR_RDC, nic_base + EN0_ISR);
683
684         /* Now the normal output. */
685         outb_p(count & 0xff, nic_base + EN0_RCNTLO);
686         outb_p(count >> 8,   nic_base + EN0_RCNTHI);
687         outb_p(0x00, nic_base + EN0_RSARLO);
688         outb_p(start_page, nic_base + EN0_RSARHI);
689
690         outb_p(E8390_RWRITE+E8390_START, nic_base + NE_CMD);
691         if (ei_status.word16) {
692                 outsw(NE_BASE + NE_DATAPORT, buf, count>>1);
693         } else {
694                 outsb(NE_BASE + NE_DATAPORT, buf, count);
695         }
696
697         dma_start = jiffies;
698
699 #ifdef NE_SANITY_CHECK
700         /* This was for the ALPHA version only, but enough people have
701            been encountering problems so it is still here. */
702
703         if (ei_debug > 1) {             /* DMA termination address check... */
704                 int addr, tries = 20;
705                 do {
706                         int high = inb_p(nic_base + EN0_RSARHI);
707                         int low = inb_p(nic_base + EN0_RSARLO);
708                         addr = (high << 8) + low;
709                         if ((start_page << 8) + count == addr)
710                                 break;
711                 } while (--tries > 0);
712                 if (tries <= 0) {
713                         printk("%s: Tx packet transfer address mismatch,"
714                                         "%#4.4x (expected) vs. %#4.4x (actual).\n",
715                                         dev->name, (start_page << 8) + count, addr);
716                         if (retries++ == 0)
717                                 goto retry;
718                 }
719         }
720 #endif
721
722         while ((inb_p(nic_base + EN0_ISR) & ENISR_RDC) == 0)
723                 if (time_after(jiffies, dma_start + 2*HZ/100)) {                /* 20ms */
724                         printk("%s: timeout waiting for Tx RDC.\n", dev->name);
725                         ne_reset_8390(dev);
726                         NS8390p_init(dev, 1);
727                         break;
728                 }
729
730         outb_p(ENISR_RDC, nic_base + EN0_ISR);  /* Ack intr. */
731         ei_status.dmaing &= ~0x01;
732 }
733
734
735 #ifdef MODULE
736 #define MAX_NE_CARDS    4       /* Max number of NE cards per module */
737 static struct net_device *dev_ne[MAX_NE_CARDS];
738 static int io[MAX_NE_CARDS];
739 static int irq[MAX_NE_CARDS];
740 static int bad[MAX_NE_CARDS];   /* 0xbad = bad sig or no reset ack */
741 MODULE_LICENSE("GPL");
742
743 module_param_array(io, int, NULL, 0);
744 module_param_array(irq, int, NULL, 0);
745 module_param_array(bad, int, NULL, 0);
746 MODULE_PARM_DESC(io, "(ignored)");
747 MODULE_PARM_DESC(irq, "(ignored)");
748 MODULE_PARM_DESC(bad, "(ignored)");
749
750 /* Module code fixed by David Weinehall */
751
752 int __init init_module(void)
753 {
754         struct net_device *dev;
755         int this_dev, found = 0;
756
757         for (this_dev = 0; this_dev < MAX_NE_CARDS; this_dev++) {
758                 dev = alloc_eip_netdev();
759                 if (!dev)
760                         break;
761                 dev->irq = irq[this_dev];
762                 dev->mem_end = bad[this_dev];
763                 dev->base_addr = io[this_dev];
764                 if (do_ne2_probe(dev) == 0) {
765                         dev_ne[found++] = dev;
766                         continue;
767                 }
768                 free_netdev(dev);
769                 break;
770         }
771         if (found)
772                 return 0;
773         printk(KERN_WARNING "ne2.c: No NE/2 card found\n");
774         return -ENXIO;
775 }
776
777 static void cleanup_card(struct net_device *dev)
778 {
779         mca_mark_as_unused(ei_status.priv);
780         mca_set_adapter_procfn( ei_status.priv, NULL, NULL);
781         free_irq(dev->irq, dev);
782         release_region(dev->base_addr, NE_IO_EXTENT);
783 }
784
785 void __exit cleanup_module(void)
786 {
787         int this_dev;
788
789         for (this_dev = 0; this_dev < MAX_NE_CARDS; this_dev++) {
790                 struct net_device *dev = dev_ne[this_dev];
791                 if (dev) {
792                         unregister_netdev(dev);
793                         cleanup_card(dev);
794                         free_netdev(dev);
795                 }
796         }
797 }
798 #endif /* MODULE */