1 /* 3c503.c: A shared-memory NS8390 ethernet driver for linux. */
3 Written 1992-94 by Donald Becker.
5 Copyright 1993 United States Government as represented by the
6 Director, National Security Agency. This software may be used and
7 distributed according to the terms of the GNU Public License,
8 incorporated herein by reference.
10 The author may be reached as becker@CESDIS.gsfc.nasa.gov, or C/O
11 Center of Excellence in Space Data and Information Sciences
12 Code 930.5, Goddard Space Flight Center, Greenbelt MD 20771
14 This driver should work with the 3c503 and 3c503/16. It should be used
15 in shared memory mode for best performance, although it may also work
16 in programmed-I/O mode.
19 EtherLink II Technical Reference Manual,
20 EtherLink II/16 Technical Reference Manual Supplement,
21 3Com Corporation, 5400 Bayfront Plaza, Santa Clara CA 95052-8145
23 The Crynwr 3c503 packet driver.
27 Paul Gortmaker : add support for the 2nd 8kB of RAM on 16 bit cards.
28 Paul Gortmaker : multiple card support for module users.
29 rjohnson@analogic.com : Fix up PIO interface for efficient operation.
33 static const char *version
=
34 "3c503.c:v1.10 9/23/93 Donald Becker (becker@cesdis.gsfc.nasa.gov)\n";
36 #include <linux/module.h>
38 #include <linux/kernel.h>
39 #include <linux/sched.h>
40 #include <linux/errno.h>
41 #include <linux/string.h>
42 #include <linux/delay.h>
43 #include <linux/netdevice.h>
44 #include <linux/etherdevice.h>
45 #include <linux/init.h>
48 #include <asm/system.h>
49 #include <asm/byteorder.h>
55 int el2_probe(struct net_device
*dev
);
56 int el2_pio_probe(struct net_device
*dev
);
57 int el2_probe1(struct net_device
*dev
, int ioaddr
);
59 /* A zero-terminated list of I/O addresses to be probed in PIO mode. */
60 static unsigned int netcard_portlist
[] __initdata
=
61 { 0x300,0x310,0x330,0x350,0x250,0x280,0x2a0,0x2e0,0};
63 #define EL2_IO_EXTENT 16
65 static int el2_open(struct net_device
*dev
);
66 static int el2_close(struct net_device
*dev
);
67 static void el2_reset_8390(struct net_device
*dev
);
68 static void el2_init_card(struct net_device
*dev
);
69 static void el2_block_output(struct net_device
*dev
, int count
,
70 const unsigned char *buf
, int start_page
);
71 static void el2_block_input(struct net_device
*dev
, int count
, struct sk_buff
*skb
,
73 static void el2_get_8390_hdr(struct net_device
*dev
, struct e8390_pkt_hdr
*hdr
,
77 /* This routine probes for a memory-mapped 3c503 board by looking for
78 the "location register" at the end of the jumpered boot PROM space.
79 This works even if a PROM isn't there.
81 If the ethercard isn't found there is an optional probe for
82 ethercard jumpered to programmed-I/O mode.
85 el2_probe(struct net_device
*dev
)
87 int *addr
, addrs
[] = { 0xddffe, 0xd9ffe, 0xcdffe, 0xc9ffe, 0};
88 int base_addr
= dev
->base_addr
;
90 if (base_addr
> 0x1ff) /* Check a single specified location. */
91 return el2_probe1(dev
, base_addr
);
92 else if (base_addr
!= 0) /* Don't probe at all. */
95 for (addr
= addrs
; *addr
; addr
++) {
97 unsigned int base_bits
= isa_readb(*addr
);
98 /* Find first set bit. */
99 for(i
= 7; i
>= 0; i
--, base_bits
>>= 1)
104 if (el2_probe1(dev
, netcard_portlist
[i
]) == 0)
107 #if ! defined(no_probe_nonshared_memory)
108 return el2_pio_probe(dev
);
114 /* Try all of the locations that aren't obviously empty. This touches
115 a lot of locations, and is much riskier than the code above. */
117 el2_pio_probe(struct net_device
*dev
)
120 int base_addr
= dev
? dev
->base_addr
: 0;
122 if (base_addr
> 0x1ff) /* Check a single specified location. */
123 return el2_probe1(dev
, base_addr
);
124 else if (base_addr
!= 0) /* Don't probe at all. */
127 for (i
= 0; netcard_portlist
[i
]; i
++)
128 if (el2_probe1(dev
, netcard_portlist
[i
]) == 0)
134 /* Probe for the Etherlink II card at I/O port base IOADDR,
135 returning non-zero on success. If found, set the station
136 address and memory parameters in DEVICE. */
138 el2_probe1(struct net_device
*dev
, int ioaddr
)
140 int i
, iobase_reg
, membase_reg
, saved_406
, wordlength
, retval
;
141 static unsigned version_printed
;
142 unsigned long vendor_id
;
144 if (!request_region(ioaddr
, EL2_IO_EXTENT
, dev
->name
))
147 /* Reset and/or avoid any lurking NE2000 */
148 if (inb(ioaddr
+ 0x408) == 0xff) {
154 /* We verify that it's a 3C503 board by checking the first three octets
155 of its ethernet address. */
156 iobase_reg
= inb(ioaddr
+0x403);
157 membase_reg
= inb(ioaddr
+0x404);
158 /* ASIC location registers should be 0 or have only a single bit set. */
159 if ( (iobase_reg
& (iobase_reg
- 1))
160 || (membase_reg
& (membase_reg
- 1))) {
164 saved_406
= inb_p(ioaddr
+ 0x406);
165 outb_p(ECNTRL_RESET
|ECNTRL_THIN
, ioaddr
+ 0x406); /* Reset it... */
166 outb_p(ECNTRL_THIN
, ioaddr
+ 0x406);
167 /* Map the station addr PROM into the lower I/O ports. We now check
168 for both the old and new 3Com prefix */
169 outb(ECNTRL_SAPROM
|ECNTRL_THIN
, ioaddr
+ 0x406);
170 vendor_id
= inb(ioaddr
)*0x10000 + inb(ioaddr
+ 1)*0x100 + inb(ioaddr
+ 2);
171 if ((vendor_id
!= OLD_3COM_ID
) && (vendor_id
!= NEW_3COM_ID
)) {
172 /* Restore the register we frobbed. */
173 outb(saved_406
, ioaddr
+ 0x406);
178 if (ei_debug
&& version_printed
++ == 0)
181 dev
->base_addr
= ioaddr
;
182 /* Allocate dev->priv and fill in 8390 specific dev fields. */
183 if (ethdev_init(dev
)) {
184 printk ("3c503: unable to allocate memory for dev->priv.\n");
189 printk("%s: 3c503 at i/o base %#3x, node ", dev
->name
, ioaddr
);
191 /* Retrieve and print the ethernet address. */
192 for (i
= 0; i
< 6; i
++)
193 printk(" %2.2x", dev
->dev_addr
[i
] = inb(ioaddr
+ i
));
195 /* Map the 8390 back into the window. */
196 outb(ECNTRL_THIN
, ioaddr
+ 0x406);
198 /* Check for EL2/16 as described in tech. man. */
199 outb_p(E8390_PAGE0
, ioaddr
+ E8390_CMD
);
200 outb_p(0, ioaddr
+ EN0_DCFG
);
201 outb_p(E8390_PAGE2
, ioaddr
+ E8390_CMD
);
202 wordlength
= inb_p(ioaddr
+ EN0_DCFG
) & ENDCFG_WTS
;
203 outb_p(E8390_PAGE0
, ioaddr
+ E8390_CMD
);
205 /* Probe for, turn on and clear the board's shared memory. */
206 if (ei_debug
> 2) printk(" memory jumpers %2.2x ", membase_reg
);
207 outb(EGACFR_NORM
, ioaddr
+ 0x405); /* Enable RAM */
209 /* This should be probed for (or set via an ioctl()) at run-time.
210 Right now we use a sleazy hack to pass in the interface number
211 at boot-time via the low bits of the mem_end field. That value is
212 unused, and the low bits would be discarded even if it was used. */
213 #if defined(EI8390_THICK) || defined(EL2_AUI)
214 ei_status
.interface_num
= 1;
216 ei_status
.interface_num
= dev
->mem_end
& 0xf;
218 printk(", using %sternal xcvr.\n", ei_status
.interface_num
== 0 ? "in" : "ex");
220 if ((membase_reg
& 0xf0) == 0) {
222 ei_status
.name
= "3c503-PIO";
224 dev
->mem_start
= ((membase_reg
& 0xc0) ? 0xD8000 : 0xC8000) +
225 ((membase_reg
& 0xA0) ? 0x4000 : 0);
227 #define EL2_MEMSIZE (EL2_MB1_STOP_PG - EL2_MB1_START_PG)*256
229 /* This has never found an error, but someone might care.
230 Note that it only tests the 2nd 8kB on 16kB 3c503/16
231 cards between card addr. 0x2000 and 0x3fff. */
232 { /* Check the card's memory. */
233 unsigned long mem_base
= dev
->mem_start
;
234 unsigned int test_val
= 0xbbadf00d;
235 isa_writel(0xba5eba5e, mem_base
);
236 for (i
= sizeof(test_val
); i
< EL2_MEMSIZE
; i
+=sizeof(test_val
)) {
237 isa_writel(test_val
, mem_base
+ i
);
238 if (isa_readl(mem_base
) != 0xba5eba5e
239 || isa_readl(mem_base
+ i
) != test_val
) {
240 printk("3c503: memory failure or memory address conflict.\n");
242 ei_status
.name
= "3c503-PIO";
245 test_val
+= 0x55555555;
246 isa_writel(0, mem_base
+ i
);
249 #endif /* EL2MEMTEST */
251 dev
->mem_end
= dev
->rmem_end
= dev
->mem_start
+ EL2_MEMSIZE
;
253 if (wordlength
) { /* No Tx pages to skip over to get to Rx */
254 dev
->rmem_start
= dev
->mem_start
;
255 ei_status
.name
= "3c503/16";
257 dev
->rmem_start
= TX_PAGES
*256 + dev
->mem_start
;
258 ei_status
.name
= "3c503";
263 Divide up the memory on the card. This is the same regardless of
264 whether shared-mem or PIO is used. For 16 bit cards (16kB RAM),
265 we use the entire 8k of bank1 for an Rx ring. We only use 3k
266 of the bank0 for 2 full size Tx packet slots. For 8 bit cards,
267 (8kB RAM) we use 3kB of bank1 for two Tx slots, and the remaining
268 5kB for an Rx ring. */
271 ei_status
.tx_start_page
= EL2_MB0_START_PG
;
272 ei_status
.rx_start_page
= EL2_MB1_START_PG
;
274 ei_status
.tx_start_page
= EL2_MB1_START_PG
;
275 ei_status
.rx_start_page
= EL2_MB1_START_PG
+ TX_PAGES
;
278 /* Finish setting the board's parameters. */
279 ei_status
.stop_page
= EL2_MB1_STOP_PG
;
280 ei_status
.word16
= wordlength
;
281 ei_status
.reset_8390
= &el2_reset_8390
;
282 ei_status
.get_8390_hdr
= &el2_get_8390_hdr
;
283 ei_status
.block_input
= &el2_block_input
;
284 ei_status
.block_output
= &el2_block_output
;
288 else if (dev
->irq
> 5 && dev
->irq
!= 9) {
289 printk("3c503: configured interrupt %d invalid, will use autoIRQ.\n",
294 ei_status
.saved_irq
= dev
->irq
;
296 dev
->open
= &el2_open
;
297 dev
->stop
= &el2_close
;
300 printk("%s: %s - %dkB RAM, 8kB shared mem window at %#6lx-%#6lx.\n",
301 dev
->name
, ei_status
.name
, (wordlength
+1)<<3,
302 dev
->mem_start
, dev
->mem_end
-1);
306 ei_status
.tx_start_page
= EL2_MB1_START_PG
;
307 ei_status
.rx_start_page
= EL2_MB1_START_PG
+ TX_PAGES
;
308 printk("\n%s: %s, %dkB RAM, using programmed I/O (REJUMPER for SHARED MEMORY).\n",
309 dev
->name
, ei_status
.name
, (wordlength
+1)<<3);
313 release_region(ioaddr
, EL2_IO_EXTENT
);
318 el2_open(struct net_device
*dev
)
322 int irqlist
[] = {5, 9, 3, 4, 0};
325 outb(EGACFR_NORM
, E33G_GACFR
); /* Enable RAM and interrupts. */
327 if (request_irq (*irqp
, NULL
, 0, "bogus", dev
) != -EBUSY
) {
328 /* Twinkle the interrupt, and check if it's seen. */
329 unsigned long cookie
= probe_irq_on();
330 outb_p(0x04 << ((*irqp
== 9) ? 2 : *irqp
), E33G_IDCFR
);
331 outb_p(0x00, E33G_IDCFR
);
332 if (*irqp
== probe_irq_off(cookie
) /* It's a good IRQ line! */
333 && request_irq (dev
->irq
= *irqp
, ei_interrupt
, 0, ei_status
.name
, dev
) == 0)
338 outb(EGACFR_IRQOFF
, E33G_GACFR
); /* disable interrupts. */
342 if (request_irq(dev
->irq
, ei_interrupt
, 0, ei_status
.name
, dev
)) {
354 el2_close(struct net_device
*dev
)
356 free_irq(dev
->irq
, dev
);
357 dev
->irq
= ei_status
.saved_irq
;
358 outb(EGACFR_IRQOFF
, E33G_GACFR
); /* disable interrupts. */
365 /* This is called whenever we have a unrecoverable failure:
367 Bad ring buffer packet header
370 el2_reset_8390(struct net_device
*dev
)
373 printk("%s: Resetting the 3c503 board...", dev
->name
);
374 printk("%#lx=%#02x %#lx=%#02x %#lx=%#02x...", E33G_IDCFR
, inb(E33G_IDCFR
),
375 E33G_CNTRL
, inb(E33G_CNTRL
), E33G_GACFR
, inb(E33G_GACFR
));
377 outb_p(ECNTRL_RESET
|ECNTRL_THIN
, E33G_CNTRL
);
379 outb_p(ei_status
.interface_num
==0 ? ECNTRL_THIN
: ECNTRL_AUI
, E33G_CNTRL
);
381 if (ei_debug
> 1) printk("done\n");
384 /* Initialize the 3c503 GA registers after a reset. */
386 el2_init_card(struct net_device
*dev
)
388 /* Unmap the station PROM and select the DIX or BNC connector. */
389 outb_p(ei_status
.interface_num
==0 ? ECNTRL_THIN
: ECNTRL_AUI
, E33G_CNTRL
);
391 /* Set ASIC copy of rx's first and last+1 buffer pages */
392 /* These must be the same as in the 8390. */
393 outb(ei_status
.rx_start_page
, E33G_STARTPG
);
394 outb(ei_status
.stop_page
, E33G_STOPPG
);
396 /* Point the vector pointer registers somewhere ?harmless?. */
397 outb(0xff, E33G_VP2
); /* Point at the ROM restart location 0xffff0 */
398 outb(0xff, E33G_VP1
);
399 outb(0x00, E33G_VP0
);
400 /* Turn off all interrupts until we're opened. */
401 outb_p(0x00, dev
->base_addr
+ EN0_IMR
);
402 /* Enable IRQs iff started. */
403 outb(EGACFR_NORM
, E33G_GACFR
);
405 /* Set the interrupt line. */
406 outb_p((0x04 << (dev
->irq
== 9 ? 2 : dev
->irq
)), E33G_IDCFR
);
407 outb_p((WRD_COUNT
<< 1), E33G_DRQCNT
); /* Set burst size to 8 */
408 outb_p(0x20, E33G_DMAAH
); /* Put a valid addr in the GA DMA */
409 outb_p(0x00, E33G_DMAAL
);
410 return; /* We always succeed */
414 * Either use the shared memory (if enabled on the board) or put the packet
415 * out through the ASIC FIFO.
418 el2_block_output(struct net_device
*dev
, int count
,
419 const unsigned char *buf
, int start_page
)
421 unsigned short int *wrd
;
422 int boguscount
; /* timeout counter */
423 unsigned short word
; /* temporary for better machine code */
425 if (ei_status
.word16
) /* Tx packets go into bank 0 on EL2/16 card */
426 outb(EGACFR_RSEL
|EGACFR_TCM
, E33G_GACFR
);
428 outb(EGACFR_NORM
, E33G_GACFR
);
430 if (dev
->mem_start
) { /* Shared memory transfer */
431 unsigned long dest_addr
= dev
->mem_start
+
432 ((start_page
- ei_status
.tx_start_page
) << 8);
433 isa_memcpy_toio(dest_addr
, buf
, count
);
434 outb(EGACFR_NORM
, E33G_GACFR
); /* Back to bank1 in case on bank0 */
439 * No shared memory, put the packet out the other way.
440 * Set up then start the internal memory transfer to Tx Start Page
443 word
= (unsigned short)start_page
;
444 outb(word
&0xFF, E33G_DMAAH
);
445 outb(word
>>8, E33G_DMAAL
);
447 outb_p((ei_status
.interface_num
? ECNTRL_AUI
: ECNTRL_THIN
) | ECNTRL_OUTPUT
448 | ECNTRL_START
, E33G_CNTRL
);
451 * Here I am going to write data to the FIFO as quickly as possible.
452 * Note that E33G_FIFOH is defined incorrectly. It is really
453 * E33G_FIFOL, the lowest port address for both the byte and
454 * word write. Variable 'count' is NOT checked. Caller must supply a
455 * valid count. Note that I may write a harmless extra byte to the
456 * 8390 if the byte-count was not even.
458 wrd
= (unsigned short int *) buf
;
459 count
= (count
+ 1) >> 1;
463 while ((inb(E33G_STATUS
) & ESTAT_DPRDY
) == 0)
467 printk("%s: FIFO blocked in el2_block_output.\n", dev
->name
);
472 if(count
> WRD_COUNT
)
474 outsw(E33G_FIFOH
, wrd
, WRD_COUNT
);
480 outsw(E33G_FIFOH
, wrd
, count
);
485 outb_p(ei_status
.interface_num
==0 ? ECNTRL_THIN
: ECNTRL_AUI
, E33G_CNTRL
);
489 /* Read the 4 byte, page aligned 8390 specific header. */
491 el2_get_8390_hdr(struct net_device
*dev
, struct e8390_pkt_hdr
*hdr
, int ring_page
)
494 unsigned long hdr_start
= dev
->mem_start
+ ((ring_page
- EL2_MB1_START_PG
)<<8);
497 if (dev
->mem_start
) { /* Use the shared memory. */
498 isa_memcpy_fromio(hdr
, hdr_start
, sizeof(struct e8390_pkt_hdr
));
503 * No shared memory, use programmed I/O.
506 word
= (unsigned short)ring_page
;
507 outb(word
&0xFF, E33G_DMAAH
);
508 outb(word
>>8, E33G_DMAAL
);
510 outb_p((ei_status
.interface_num
== 0 ? ECNTRL_THIN
: ECNTRL_AUI
) | ECNTRL_INPUT
511 | ECNTRL_START
, E33G_CNTRL
);
513 while ((inb(E33G_STATUS
) & ESTAT_DPRDY
) == 0)
517 printk("%s: FIFO blocked in el2_get_8390_hdr.\n", dev
->name
);
518 memset(hdr
, 0x00, sizeof(struct e8390_pkt_hdr
));
523 insw(E33G_FIFOH
, hdr
, (sizeof(struct e8390_pkt_hdr
))>> 1);
525 outb_p(ei_status
.interface_num
== 0 ? ECNTRL_THIN
: ECNTRL_AUI
, E33G_CNTRL
);
530 el2_block_input(struct net_device
*dev
, int count
, struct sk_buff
*skb
, int ring_offset
)
533 unsigned short int *buf
;
536 int end_of_ring
= dev
->rmem_end
;
538 /* Maybe enable shared memory just be to be safe... nahh.*/
539 if (dev
->mem_start
) { /* Use the shared memory. */
540 ring_offset
-= (EL2_MB1_START_PG
<<8);
541 if (dev
->mem_start
+ ring_offset
+ count
> end_of_ring
) {
542 /* We must wrap the input move. */
543 int semi_count
= end_of_ring
- (dev
->mem_start
+ ring_offset
);
544 isa_memcpy_fromio(skb
->data
, dev
->mem_start
+ ring_offset
, semi_count
);
546 isa_memcpy_fromio(skb
->data
+ semi_count
, dev
->rmem_start
, count
);
548 /* Packet is in one chunk -- we can copy + cksum. */
549 isa_eth_io_copy_and_sum(skb
, dev
->mem_start
+ ring_offset
, count
, 0);
555 * No shared memory, use programmed I/O.
557 word
= (unsigned short) ring_offset
;
558 outb(word
>>8, E33G_DMAAH
);
559 outb(word
&0xFF, E33G_DMAAL
);
561 outb_p((ei_status
.interface_num
== 0 ? ECNTRL_THIN
: ECNTRL_AUI
) | ECNTRL_INPUT
562 | ECNTRL_START
, E33G_CNTRL
);
565 * Here I also try to get data as fast as possible. I am betting that I
566 * can read one extra byte without clobbering anything in the kernel because
567 * this would only occur on an odd byte-count and allocation of skb->data
568 * is word-aligned. Variable 'count' is NOT checked. Caller must check
570 * [This is currently quite safe.... but if one day the 3c503 explodes
571 * you know where to come looking ;)]
574 buf
= (unsigned short int *) skb
->data
;
575 count
= (count
+ 1) >> 1;
579 while ((inb(E33G_STATUS
) & ESTAT_DPRDY
) == 0)
583 printk("%s: FIFO blocked in el2_block_input.\n", dev
->name
);
588 if(count
> WRD_COUNT
)
590 insw(E33G_FIFOH
, buf
, WRD_COUNT
);
596 insw(E33G_FIFOH
, buf
, count
);
601 outb_p(ei_status
.interface_num
== 0 ? ECNTRL_THIN
: ECNTRL_AUI
, E33G_CNTRL
);
605 #define MAX_EL2_CARDS 4 /* Max number of EL2 cards per module */
607 static struct net_device dev_el2
[MAX_EL2_CARDS
] = {
616 static int io
[MAX_EL2_CARDS
] = { 0, };
617 static int irq
[MAX_EL2_CARDS
] = { 0, };
618 static int xcvr
[MAX_EL2_CARDS
] = { 0, }; /* choose int. or ext. xcvr */
619 MODULE_PARM(io
, "1-" __MODULE_STRING(MAX_EL2_CARDS
) "i");
620 MODULE_PARM(irq
, "1-" __MODULE_STRING(MAX_EL2_CARDS
) "i");
621 MODULE_PARM(xcvr
, "1-" __MODULE_STRING(MAX_EL2_CARDS
) "i");
623 /* This is set up so that only a single autoprobe takes place per call.
624 ISA device autoprobes on a running machine are not recommended. */
628 int this_dev
, found
= 0;
630 for (this_dev
= 0; this_dev
< MAX_EL2_CARDS
; this_dev
++) {
631 struct net_device
*dev
= &dev_el2
[this_dev
];
632 dev
->irq
= irq
[this_dev
];
633 dev
->base_addr
= io
[this_dev
];
634 dev
->mem_end
= xcvr
[this_dev
]; /* low 4bits = xcvr sel. */
635 dev
->init
= el2_probe
;
636 if (io
[this_dev
] == 0) {
637 if (this_dev
!= 0) break; /* only autoprobe 1st one */
638 printk(KERN_NOTICE
"3c503.c: Presently autoprobing (not recommended) for a single card.\n");
640 if (register_netdev(dev
) != 0) {
641 printk(KERN_WARNING
"3c503.c: No 3c503 card found (i/o = 0x%x).\n", io
[this_dev
]);
642 if (found
!= 0) { /* Got at least one. */
657 for (this_dev
= 0; this_dev
< MAX_EL2_CARDS
; this_dev
++) {
658 struct net_device
*dev
= &dev_el2
[this_dev
];
659 if (dev
->priv
!= NULL
) {
660 void *priv
= dev
->priv
;
661 /* NB: el2_close() handles free_irq */
662 release_region(dev
->base_addr
, EL2_IO_EXTENT
);
663 unregister_netdev(dev
);
673 * kept-new-versions: 5