1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
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/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/system.h>
80#include <asm/io.h>
81#include <asm/dma.h>
82
83#include "8390.h"
84
85#define DRV_NAME "ne2"
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101#define NE_BASE (dev->base_addr)
102#define NE_CMD 0x00
103#define NE_DATAPORT 0x10
104#define NE_RESET 0x20
105#define NE_IO_EXTENT 0x30
106
107#define NE1SM_START_PG 0x20
108#define NE1SM_STOP_PG 0x40
109#define NESM_START_PG 0x40
110#define NESM_STOP_PG 0x80
111
112
113static unsigned int addresses[7] __initdata =
114 {0x1000, 0x2020, 0x8020, 0xa0a0, 0xb0b0, 0xc0c0, 0xc3d0};
115static int irqs[4] __initdata = {3, 4, 5, 9};
116
117
118static unsigned int dlink_addresses[4] __initdata =
119 {0x300, 0x320, 0x340, 0x360};
120static int dlink_irqs[8] __initdata = {3, 4, 5, 9, 10, 11, 14, 15};
121
122struct ne2_adapters_t {
123 unsigned int id;
124 char *name;
125};
126
127static struct ne2_adapters_t ne2_adapters[] __initdata = {
128 { 0x6354, "Arco Ethernet Adapter AE/2" },
129 { 0x70DE, "Compex ENET-16 MC/P" },
130 { 0x7154, "Novell Ethernet Adapter NE/2" },
131 { 0x56ea, "D-Link DE-320CT" },
132 { 0x0000, NULL }
133};
134
135extern int netcard_probe(struct net_device *dev);
136
137static int ne2_probe1(struct net_device *dev, int slot);
138
139static void ne_reset_8390(struct net_device *dev);
140static void ne_get_8390_hdr(struct net_device *dev, struct e8390_pkt_hdr *hdr,
141 int ring_page);
142static void ne_block_input(struct net_device *dev, int count,
143 struct sk_buff *skb, int ring_offset);
144static void ne_block_output(struct net_device *dev, const int count,
145 const unsigned char *buf, const int start_page);
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162static void __init dlink_put_eeprom(unsigned char value, unsigned int addr)
163{
164 int z;
165 unsigned char v1, v2;
166
167
168
169 outb(value, addr + 0x1e);
170
171
172
173
174 for (z = 0; z < 2; z++) {
175 do {
176 v1 = inb(addr + 0x1e);
177 v2 = inb(addr + 0x1e);
178 }
179 while (!((v1 ^ v2) & 0x80));
180 }
181}
182
183static void __init dlink_send_eeprom_bit(unsigned int bit, unsigned int addr)
184{
185
186
187 bit = bit << 1;
188
189
190
191 dlink_put_eeprom(0x09 | bit, addr);
192 dlink_put_eeprom(0x0d | bit, addr);
193 dlink_put_eeprom(0x0d | bit, addr);
194 dlink_put_eeprom(0x09 | bit, addr);
195}
196
197static void __init dlink_send_eeprom_word(unsigned int value, unsigned int len, unsigned int addr)
198{
199 int z;
200
201
202
203 value = value << (16 - len);
204
205
206
207 for (z = 0; z < len; z++) {
208 dlink_send_eeprom_bit((value & 0x8000) >> 15, addr);
209 value = value << 1;
210 }
211}
212
213static unsigned int __init dlink_get_eeprom(unsigned int eeaddr, unsigned int addr)
214{
215 int z;
216 unsigned int value = 0;
217
218
219
220
221 dlink_put_eeprom(0x01, addr);
222 dlink_put_eeprom(0x09, addr);
223
224
225
226
227 dlink_send_eeprom_word(0x0180 | (eeaddr & 0x3f), 9, addr);
228
229
230
231
232 for (z = 0; z < 16; z++) {
233 dlink_send_eeprom_bit(0, addr);
234 value = (value << 1) | (inb(addr + 0x1e) & 0x01);
235 }
236
237 return value;
238}
239
240
241
242
243
244static int __init do_ne2_probe(struct net_device *dev)
245{
246 static int current_mca_slot = -1;
247 int i;
248 int adapter_found = 0;
249
250
251
252
253
254
255
256
257 for(i = 0; (ne2_adapters[i].name != NULL) && !adapter_found; i++) {
258 current_mca_slot =
259 mca_find_unused_adapter(ne2_adapters[i].id, 0);
260
261 if((current_mca_slot != MCA_NOTFOUND) && !adapter_found) {
262 int res;
263 mca_set_adapter_name(current_mca_slot,
264 ne2_adapters[i].name);
265 mca_mark_as_used(current_mca_slot);
266
267 res = ne2_probe1(dev, current_mca_slot);
268 if (res)
269 mca_mark_as_unused(current_mca_slot);
270 return res;
271 }
272 }
273 return -ENODEV;
274}
275
276#ifndef MODULE
277struct net_device * __init ne2_probe(int unit)
278{
279 struct net_device *dev = alloc_eip_netdev();
280 int err;
281
282 if (!dev)
283 return ERR_PTR(-ENOMEM);
284
285 sprintf(dev->name, "eth%d", unit);
286 netdev_boot_setup_check(dev);
287
288 err = do_ne2_probe(dev);
289 if (err)
290 goto out;
291 return dev;
292out:
293 free_netdev(dev);
294 return ERR_PTR(err);
295}
296#endif
297
298static int ne2_procinfo(char *buf, int slot, struct net_device *dev)
299{
300 int len=0;
301
302 len += sprintf(buf+len, "The NE/2 Ethernet Adapter\n" );
303 len += sprintf(buf+len, "Driver written by Wim Dumon ");
304 len += sprintf(buf+len, "<wimpie@kotnet.org>\n");
305 len += sprintf(buf+len, "Modified by ");
306 len += sprintf(buf+len, "David Weinehall <tao@acc.umu.se>\n");
307 len += sprintf(buf+len, "and by Magnus Jonsson <bigfoot@acc.umu.se>\n");
308 len += sprintf(buf+len, "Based on the original NE2000 drivers\n" );
309 len += sprintf(buf+len, "Base IO: %#x\n", (unsigned int)dev->base_addr);
310 len += sprintf(buf+len, "IRQ : %d\n", dev->irq);
311 len += sprintf(buf+len, "HW addr : %pM\n", dev->dev_addr);
312
313 return len;
314}
315
316static int __init ne2_probe1(struct net_device *dev, int slot)
317{
318 int i, base_addr, irq, retval;
319 unsigned char POS;
320 unsigned char SA_prom[32];
321 const char *name = "NE/2";
322 int start_page, stop_page;
323 static unsigned version_printed;
324
325 if (ei_debug && version_printed++ == 0)
326 printk(version);
327
328 printk("NE/2 ethercard found in slot %d:", slot);
329
330
331 POS = mca_read_stored_pos(slot, 2);
332 if(!(POS % 2)) {
333 printk(" disabled.\n");
334 return -ENODEV;
335 }
336
337
338
339 if (mca_read_stored_pos(slot, 0) == 0xea) {
340 base_addr = dlink_addresses[(POS >> 5) & 0x03];
341 irq = dlink_irqs[(POS >> 2) & 0x07];
342 }
343 else {
344 i = (POS & 0xE)>>1;
345
346
347
348 base_addr = addresses[i - 1];
349 irq = irqs[(POS & 0x60)>>5];
350 }
351
352 if (!request_region(base_addr, NE_IO_EXTENT, DRV_NAME))
353 return -EBUSY;
354
355#ifdef DEBUG
356 printk("POS info : pos 2 = %#x ; base = %#x ; irq = %ld\n", POS,
357 base_addr, irq);
358#endif
359
360#ifndef CRYNWR_WAY
361
362 for (i=0; i<8; i++)
363 outb(0x0, base_addr + NE_RESET);
364 inb(base_addr + NE_RESET);
365 outb(0x21, base_addr + NE_CMD);
366 if (inb(base_addr + NE_CMD) != 0x21) {
367 printk("NE/2 adapter not responding\n");
368 retval = -ENODEV;
369 goto out;
370 }
371
372
373
374
375
376
377
378#else
379
380 {
381 unsigned long reset_start_time = jiffies;
382
383
384
385 outb(inb(base_addr + NE_RESET), base_addr + NE_RESET);
386
387 while ((inb_p(base_addr + EN0_ISR) & ENISR_RESET) == 0)
388 if (time_after(jiffies, reset_start_time + 2*HZ/100)) {
389 printk(" not found (no reset ack).\n");
390 retval = -ENODEV;
391 goto out;
392 }
393
394 outb_p(0xff, base_addr + EN0_ISR);
395 }
396#endif
397
398
399
400
401
402
403
404 {
405 struct {
406 unsigned char value, offset;
407 } program_seq[] = {
408
409 {E8390_NODMA+E8390_PAGE0+E8390_STOP, E8390_CMD},
410 {0x49, EN0_DCFG},
411 {0x00, EN0_RCNTLO},
412 {0x00, EN0_RCNTHI},
413 {0x00, EN0_IMR},
414 {0xFF, EN0_ISR},
415 {E8390_RXOFF, EN0_RXCR},
416 {E8390_TXOFF, EN0_TXCR},
417 {32, EN0_RCNTLO},
418 {0x00, EN0_RCNTHI},
419 {0x00, EN0_RSARLO},
420 {0x00, EN0_RSARHI},
421 {E8390_RREAD+E8390_START, E8390_CMD},
422 };
423
424 for (i = 0; i < ARRAY_SIZE(program_seq); i++)
425 outb_p(program_seq[i].value, base_addr +
426 program_seq[i].offset);
427
428 }
429 for(i = 0; i < 6 ; i+=1) {
430 SA_prom[i] = inb(base_addr + NE_DATAPORT);
431 }
432
433
434
435
436
437 if (mca_read_stored_pos(slot, 0) == 0xea) {
438 unsigned int v;
439
440 for (i = 0; i < 3; i++) {
441 v = dlink_get_eeprom(i, base_addr);
442 SA_prom[(i << 1) ] = v & 0xff;
443 SA_prom[(i << 1) + 1] = (v >> 8) & 0xff;
444 }
445 }
446
447 start_page = NESM_START_PG;
448 stop_page = NESM_STOP_PG;
449
450 dev->irq=irq;
451
452
453
454 retval = request_irq(dev->irq, eip_interrupt, 0, DRV_NAME, dev);
455 if (retval) {
456 printk (" unable to get IRQ %d (irqval=%d).\n",
457 dev->irq, retval);
458 goto out;
459 }
460
461 dev->base_addr = base_addr;
462
463 for (i = 0; i < ETH_ALEN; i++)
464 dev->dev_addr[i] = SA_prom[i];
465
466 printk(" %pM\n", dev->dev_addr);
467
468 printk("%s: %s found at %#x, using IRQ %d.\n",
469 dev->name, name, base_addr, dev->irq);
470
471 mca_set_adapter_procfn(slot, (MCA_ProcFn) ne2_procinfo, dev);
472
473 ei_status.name = name;
474 ei_status.tx_start_page = start_page;
475 ei_status.stop_page = stop_page;
476 ei_status.word16 = (2 == 2);
477
478 ei_status.rx_start_page = start_page + TX_PAGES;
479#ifdef PACKETBUF_MEMSIZE
480
481 ei_status.stop_page = ei_status.tx_start_page + PACKETBUF_MEMSIZE;
482#endif
483
484 ei_status.reset_8390 = &ne_reset_8390;
485 ei_status.block_input = &ne_block_input;
486 ei_status.block_output = &ne_block_output;
487 ei_status.get_8390_hdr = &ne_get_8390_hdr;
488
489 ei_status.priv = slot;
490
491 dev->netdev_ops = &eip_netdev_ops;
492 NS8390p_init(dev, 0);
493
494 retval = register_netdev(dev);
495 if (retval)
496 goto out1;
497 return 0;
498out1:
499 mca_set_adapter_procfn( ei_status.priv, NULL, NULL);
500 free_irq(dev->irq, dev);
501out:
502 release_region(base_addr, NE_IO_EXTENT);
503 return retval;
504}
505
506
507
508static void ne_reset_8390(struct net_device *dev)
509{
510 unsigned long reset_start_time = jiffies;
511
512 if (ei_debug > 1)
513 printk("resetting the 8390 t=%ld...", jiffies);
514
515
516 outb(inb(NE_BASE + NE_RESET), NE_BASE + NE_RESET);
517
518 ei_status.txing = 0;
519 ei_status.dmaing = 0;
520
521
522 while ((inb_p(NE_BASE+EN0_ISR) & ENISR_RESET) == 0)
523 if (time_after(jiffies, reset_start_time + 2*HZ/100)) {
524 printk("%s: ne_reset_8390() did not complete.\n",
525 dev->name);
526 break;
527 }
528 outb_p(ENISR_RESET, NE_BASE + EN0_ISR);
529}
530
531
532
533
534
535static void ne_get_8390_hdr(struct net_device *dev, struct e8390_pkt_hdr *hdr,
536 int ring_page)
537{
538
539 int nic_base = dev->base_addr;
540
541
542
543 if (ei_status.dmaing) {
544 printk("%s: DMAing conflict in ne_get_8390_hdr "
545 "[DMAstat:%d][irqlock:%d].\n",
546 dev->name, ei_status.dmaing, ei_status.irqlock);
547 return;
548 }
549
550 ei_status.dmaing |= 0x01;
551 outb_p(E8390_NODMA+E8390_PAGE0+E8390_START, nic_base+ NE_CMD);
552 outb_p(sizeof(struct e8390_pkt_hdr), nic_base + EN0_RCNTLO);
553 outb_p(0, nic_base + EN0_RCNTHI);
554 outb_p(0, nic_base + EN0_RSARLO);
555 outb_p(ring_page, nic_base + EN0_RSARHI);
556 outb_p(E8390_RREAD+E8390_START, nic_base + NE_CMD);
557
558 if (ei_status.word16)
559 insw(NE_BASE + NE_DATAPORT, hdr,
560 sizeof(struct e8390_pkt_hdr)>>1);
561 else
562 insb(NE_BASE + NE_DATAPORT, hdr,
563 sizeof(struct e8390_pkt_hdr));
564
565 outb_p(ENISR_RDC, nic_base + EN0_ISR);
566 ei_status.dmaing &= ~0x01;
567}
568
569
570
571
572
573
574static void ne_block_input(struct net_device *dev, int count, struct sk_buff *skb,
575 int ring_offset)
576{
577#ifdef NE_SANITY_CHECK
578 int xfer_count = count;
579#endif
580 int nic_base = dev->base_addr;
581 char *buf = skb->data;
582
583
584
585 if (ei_status.dmaing) {
586 printk("%s: DMAing conflict in ne_block_input "
587 "[DMAstat:%d][irqlock:%d].\n",
588 dev->name, ei_status.dmaing, ei_status.irqlock);
589 return;
590 }
591 ei_status.dmaing |= 0x01;
592 outb_p(E8390_NODMA+E8390_PAGE0+E8390_START, nic_base+ NE_CMD);
593 outb_p(count & 0xff, nic_base + EN0_RCNTLO);
594 outb_p(count >> 8, nic_base + EN0_RCNTHI);
595 outb_p(ring_offset & 0xff, nic_base + EN0_RSARLO);
596 outb_p(ring_offset >> 8, nic_base + EN0_RSARHI);
597 outb_p(E8390_RREAD+E8390_START, nic_base + NE_CMD);
598 if (ei_status.word16) {
599 insw(NE_BASE + NE_DATAPORT,buf,count>>1);
600 if (count & 0x01) {
601 buf[count-1] = inb(NE_BASE + NE_DATAPORT);
602#ifdef NE_SANITY_CHECK
603 xfer_count++;
604#endif
605 }
606 } else {
607 insb(NE_BASE + NE_DATAPORT, buf, count);
608 }
609
610#ifdef NE_SANITY_CHECK
611
612
613
614
615 if (ei_debug > 1) {
616 int addr, tries = 20;
617 do {
618
619
620 int high = inb_p(nic_base + EN0_RSARHI);
621 int low = inb_p(nic_base + EN0_RSARLO);
622 addr = (high << 8) + low;
623 if (((ring_offset + xfer_count) & 0xff) == low)
624 break;
625 } while (--tries > 0);
626 if (tries <= 0)
627 printk("%s: RX transfer address mismatch,"
628 "%#4.4x (expected) vs. %#4.4x (actual).\n",
629 dev->name, ring_offset + xfer_count, addr);
630 }
631#endif
632 outb_p(ENISR_RDC, nic_base + EN0_ISR);
633 ei_status.dmaing &= ~0x01;
634}
635
636static void ne_block_output(struct net_device *dev, int count,
637 const unsigned char *buf, const int start_page)
638{
639 int nic_base = NE_BASE;
640 unsigned long dma_start;
641#ifdef NE_SANITY_CHECK
642 int retries = 0;
643#endif
644
645
646
647
648 if (ei_status.word16 && (count & 0x01))
649 count++;
650
651
652
653 if (ei_status.dmaing) {
654 printk("%s: DMAing conflict in ne_block_output."
655 "[DMAstat:%d][irqlock:%d]\n",
656 dev->name, ei_status.dmaing, ei_status.irqlock);
657 return;
658 }
659 ei_status.dmaing |= 0x01;
660
661 outb_p(E8390_PAGE0+E8390_START+E8390_NODMA, nic_base + NE_CMD);
662
663#ifdef NE_SANITY_CHECK
664retry:
665#endif
666
667#ifdef NE8390_RW_BUGFIX
668
669
670
671
672 outb_p(0x42, nic_base + EN0_RCNTLO);
673 outb_p(0x00, nic_base + EN0_RCNTHI);
674 outb_p(0x42, nic_base + EN0_RSARLO);
675 outb_p(0x00, nic_base + EN0_RSARHI);
676 outb_p(E8390_RREAD+E8390_START, nic_base + NE_CMD);
677
678 SLOW_DOWN_IO;
679 SLOW_DOWN_IO;
680 SLOW_DOWN_IO;
681#endif
682
683 outb_p(ENISR_RDC, nic_base + EN0_ISR);
684
685
686 outb_p(count & 0xff, nic_base + EN0_RCNTLO);
687 outb_p(count >> 8, nic_base + EN0_RCNTHI);
688 outb_p(0x00, nic_base + EN0_RSARLO);
689 outb_p(start_page, nic_base + EN0_RSARHI);
690
691 outb_p(E8390_RWRITE+E8390_START, nic_base + NE_CMD);
692 if (ei_status.word16) {
693 outsw(NE_BASE + NE_DATAPORT, buf, count>>1);
694 } else {
695 outsb(NE_BASE + NE_DATAPORT, buf, count);
696 }
697
698 dma_start = jiffies;
699
700#ifdef NE_SANITY_CHECK
701
702
703
704 if (ei_debug > 1) {
705 int addr, tries = 20;
706 do {
707 int high = inb_p(nic_base + EN0_RSARHI);
708 int low = inb_p(nic_base + EN0_RSARLO);
709 addr = (high << 8) + low;
710 if ((start_page << 8) + count == addr)
711 break;
712 } while (--tries > 0);
713 if (tries <= 0) {
714 printk("%s: Tx packet transfer address mismatch,"
715 "%#4.4x (expected) vs. %#4.4x (actual).\n",
716 dev->name, (start_page << 8) + count, addr);
717 if (retries++ == 0)
718 goto retry;
719 }
720 }
721#endif
722
723 while ((inb_p(nic_base + EN0_ISR) & ENISR_RDC) == 0)
724 if (time_after(jiffies, dma_start + 2*HZ/100)) {
725 printk("%s: timeout waiting for Tx RDC.\n", dev->name);
726 ne_reset_8390(dev);
727 NS8390p_init(dev, 1);
728 break;
729 }
730
731 outb_p(ENISR_RDC, nic_base + EN0_ISR);
732 ei_status.dmaing &= ~0x01;
733}
734
735
736#ifdef MODULE
737#define MAX_NE_CARDS 4
738static struct net_device *dev_ne[MAX_NE_CARDS];
739static int io[MAX_NE_CARDS];
740static int irq[MAX_NE_CARDS];
741static int bad[MAX_NE_CARDS];
742MODULE_LICENSE("GPL");
743
744module_param_array(io, int, NULL, 0);
745module_param_array(irq, int, NULL, 0);
746module_param_array(bad, int, NULL, 0);
747MODULE_PARM_DESC(io, "(ignored)");
748MODULE_PARM_DESC(irq, "(ignored)");
749MODULE_PARM_DESC(bad, "(ignored)");
750
751
752
753int __init init_module(void)
754{
755 struct net_device *dev;
756 int this_dev, found = 0;
757
758 for (this_dev = 0; this_dev < MAX_NE_CARDS; this_dev++) {
759 dev = alloc_eip_netdev();
760 if (!dev)
761 break;
762 dev->irq = irq[this_dev];
763 dev->mem_end = bad[this_dev];
764 dev->base_addr = io[this_dev];
765 if (do_ne2_probe(dev) == 0) {
766 dev_ne[found++] = dev;
767 continue;
768 }
769 free_netdev(dev);
770 break;
771 }
772 if (found)
773 return 0;
774 printk(KERN_WARNING "ne2.c: No NE/2 card found\n");
775 return -ENXIO;
776}
777
778static void cleanup_card(struct net_device *dev)
779{
780 mca_mark_as_unused(ei_status.priv);
781 mca_set_adapter_procfn( ei_status.priv, NULL, NULL);
782 free_irq(dev->irq, dev);
783 release_region(dev->base_addr, NE_IO_EXTENT);
784}
785
786void __exit cleanup_module(void)
787{
788 int this_dev;
789
790 for (this_dev = 0; this_dev < MAX_NE_CARDS; this_dev++) {
791 struct net_device *dev = dev_ne[this_dev];
792 if (dev) {
793 unregister_netdev(dev);
794 cleanup_card(dev);
795 free_netdev(dev);
796 }
797 }
798}
799#endif
800