1 /* ne2k-pci.c: A NE2000 clone on PCI bus driver for Linux. */
3 A Linux device driver for PCI NE2000 clones.
5 Authorship and other copyrights:
6 1992-1998 by Donald Becker, NE2000 core and various modifications.
7 1995-1998 by Paul Gortmaker, core modifications and PCI support.
9 Copyright 1993 assigned to the United States Government as represented
10 by the Director, National Security Agency.
12 This software may be used and distributed according to the terms
13 of the GNU Public License, incorporated herein by reference.
15 The author may be reached as becker@CESDIS.gsfc.nasa.gov, or C/O
16 Center of Excellence in Space Data and Information Sciences
17 Code 930.5, Goddard Space Flight Center, Greenbelt MD 20771
19 People are making PCI ne2000 clones! Oh the horror, the horror...
22 No full-duplex support.
25 /* Our copyright info must remain in the binary. */
26 static const char *version
=
27 "ne2k-pci.c:vpre-1.00e 5/27/99 D. Becker/P. Gortmaker http://cesdis.gsfc.nasa.gov/linux/drivers/ne2k-pci.html\n";
29 #include <linux/module.h>
30 #include <linux/kernel.h>
31 #include <linux/sched.h>
32 #include <linux/errno.h>
33 #include <linux/pci.h>
34 #include <linux/init.h>
36 #include <asm/system.h>
40 #include <linux/netdevice.h>
41 #include <linux/etherdevice.h>
44 #if defined(__powerpc__)
45 #define inl_le(addr) le32_to_cpu(inl(addr))
46 #define inw_le(addr) le16_to_cpu(inw(addr))
48 #define outsl outsl_ns
51 /* Set statically or when loading the driver module. */
54 /* Some defines that people can play with if so inclined. */
56 /* Use 32 bit data-movement operations instead of 16 bit. */
59 /* Do we implement the read before write bugfix ? */
60 /* #define NE_RW_BUGFIX */
62 /* Do we have a non std. amount of memory? (in units of 256 byte pages) */
63 /* #define PACKETBUF_MEMSIZE 0x40 */
65 #define ne2k_flags reg0 /* Rename an existing field to store flags! */
67 /* Only the low 8 bits are usable for non-init-time flags! */
69 HOLTEK_FDX
=1, /* Full duplex -> set 0x80 at offset 0x20. */
70 ONLY_16BIT_IO
=2, ONLY_32BIT_IO
=4, /* Chip can do only 16/32-bit xfers. */
74 /* This will eventually be converted to the standard PCI probe table. */
77 unsigned short vendor
, dev_id
;
81 pci_clone_list
[] __initdata
= {
82 {0x10ec, 0x8029, "RealTek RTL-8029", 0},
83 {0x1050, 0x0940, "Winbond 89C940", 0},
84 {0x11f6, 0x1401, "Compex RL2000", 0},
85 {0x8e2e, 0x3000, "KTI ET32P2", 0},
86 {0x4a14, 0x5000, "NetVin NV5000SC", 0},
87 {0x1106, 0x0926, "Via 86C926", ONLY_16BIT_IO
},
88 {0x10bd, 0x0e34, "SureCom NE34", 0},
89 {0x1050, 0x5a5a, "Winbond W89C940F", 0},
90 {0x12c3, 0x0058, "Holtek HT80232", ONLY_16BIT_IO
| HOLTEK_FDX
},
91 {0x12c3, 0x5598, "Holtek HT80229",
92 ONLY_32BIT_IO
| HOLTEK_FDX
| STOP_PG_0x60
},
96 /* ---- No user-serviceable parts below ---- */
98 #define NE_BASE (dev->base_addr)
100 #define NE_DATAPORT 0x10 /* NatSemi-defined port window offset. */
101 #define NE_RESET 0x1f /* Issue a read to reset, a write to clear. */
102 #define NE_IO_EXTENT 0x20
104 #define NESM_START_PG 0x40 /* First page of TX buffer */
105 #define NESM_STOP_PG 0x80 /* Last page +1 of RX ring */
107 int ne2k_pci_probe(struct net_device
*dev
);
108 static struct net_device
*ne2k_pci_probe1(struct net_device
*dev
, long ioaddr
, int irq
,
111 static int ne2k_pci_open(struct net_device
*dev
);
112 static int ne2k_pci_close(struct net_device
*dev
);
114 static void ne2k_pci_reset_8390(struct net_device
*dev
);
115 static void ne2k_pci_get_8390_hdr(struct net_device
*dev
, struct e8390_pkt_hdr
*hdr
,
117 static void ne2k_pci_block_input(struct net_device
*dev
, int count
,
118 struct sk_buff
*skb
, int ring_offset
);
119 static void ne2k_pci_block_output(struct net_device
*dev
, const int count
,
120 const unsigned char *buf
, const int start_page
);
124 /* No room in the standard 8390 structure for extra info we need. */
125 struct ne2k_pci_card
{
126 struct ne2k_pci_card
*next
;
127 struct net_device
*dev
;
128 struct pci_dev
*pci_dev
;
130 /* A list of all installed devices, for removing the driver module. */
131 static struct ne2k_pci_card
*ne2k_card_list
= NULL
;
138 /* We must emit version information. */
140 printk(KERN_INFO
"%s", version
);
142 if (ne2k_pci_probe(0)) {
143 printk(KERN_NOTICE
"ne2k-pci.c: No useable cards found, driver NOT installed.\n");
153 struct net_device
*dev
;
154 struct ne2k_pci_card
*this_card
;
156 /* No need to check MOD_IN_USE, as sys_delete_module() checks. */
157 while (ne2k_card_list
) {
158 dev
= ne2k_card_list
->dev
;
159 unregister_netdev(dev
);
160 release_region(dev
->base_addr
, NE_IO_EXTENT
);
162 this_card
= ne2k_card_list
;
163 ne2k_card_list
= ne2k_card_list
->next
;
166 unlock_8390_module();
172 NEx000-clone boards have a Station Address (SA) PROM (SAPROM) in the packet
173 buffer memory space. By-the-spec NE2000 clones have 0x57,0x57 in bytes
174 0x0e,0x0f of the SAPROM, while other supposed NE2000 clones must be
175 detected by their SA prefix.
177 Reading the SAPROM from a word-wide card with the 8390 set in byte-wide
178 mode results in doubled values, which can be detected and compensated for.
180 The probe is also responsible for initializing the card and filling
181 in the 'dev' and 'ei_status' structures.
185 struct netdev_entry netcard_drv
=
186 {"ne2k_pci", ne2k_pci_probe1
, NE_IO_EXTENT
, 0};
189 int __init
ne2k_pci_probe(struct net_device
*dev
)
191 struct pci_dev
*pdev
= NULL
;
195 if ( ! pci_present())
198 while ((pdev
= pci_find_class(PCI_CLASS_NETWORK_ETHERNET
<< 8, pdev
)) != NULL
) {
200 u16 pci_command
, new_command
;
201 unsigned long pci_ioaddr
;
203 /* Note: some vendor IDs (RealTek) have non-NE2k cards as well. */
204 for (i
= 0; pci_clone_list
[i
].vendor
!= 0; i
++)
205 if (pci_clone_list
[i
].vendor
== pdev
->vendor
206 && pci_clone_list
[i
].dev_id
== pdev
->device
)
208 if (pci_clone_list
[i
].vendor
== 0)
211 pci_ioaddr
= pdev
->resource
[0].start
;
212 pci_irq_line
= pdev
->irq
;
213 pci_read_config_word(pdev
, PCI_COMMAND
, &pci_command
);
215 /* Avoid already found cards from previous calls */
216 if (check_region(pci_ioaddr
, NE_IO_EXTENT
))
221 static unsigned version_printed
= 0;
222 if (version_printed
++ == 0)
223 printk(KERN_INFO
"%s", version
);
227 /* Activate the card: fix for brain-damaged Win98 BIOSes. */
228 new_command
= pci_command
| PCI_COMMAND_IO
;
229 if (pci_command
!= new_command
) {
230 printk(KERN_INFO
" The PCI BIOS has not enabled this"
231 " NE2k clone! Updating PCI command %4.4x->%4.4x.\n",
232 pci_command
, new_command
);
233 pci_write_config_word(pdev
, PCI_COMMAND
, new_command
);
236 if (pci_irq_line
<= 0 || pci_irq_line
>= NR_IRQS
)
237 printk(KERN_WARNING
" WARNING: The PCI BIOS assigned this PCI NE2k"
238 " card to IRQ %d, which is unlikely to work!.\n"
239 KERN_WARNING
" You should use the PCI BIOS setup to assign"
240 " a valid IRQ line.\n", pci_irq_line
);
242 printk("ne2k-pci.c: PCI NE2000 clone '%s' at I/O %#lx, IRQ %d.\n",
243 pci_clone_list
[i
].name
, pci_ioaddr
, pci_irq_line
);
244 dev
= ne2k_pci_probe1(dev
, pci_ioaddr
, pci_irq_line
, i
);
246 /* Should not happen. */
247 printk(KERN_ERR
"ne2k-pci: Probe of PCI card at %#lx failed.\n",
251 struct ne2k_pci_card
*ne2k_card
=
252 kmalloc(sizeof(struct ne2k_pci_card
), GFP_KERNEL
);
253 ne2k_card
->next
= ne2k_card_list
;
254 ne2k_card_list
= ne2k_card
;
255 ne2k_card
->dev
= dev
;
256 ne2k_card
->pci_dev
= pdev
;
263 return cards_found
? 0 : -ENODEV
;
266 static struct net_device __init
*ne2k_pci_probe1(struct net_device
*dev
, long ioaddr
, int irq
, int chip_idx
)
269 unsigned char SA_prom
[32];
270 int start_page
, stop_page
;
271 int reg0
= inb(ioaddr
);
276 /* Do a preliminary verification that we have a 8390. */
279 outb(E8390_NODMA
+E8390_PAGE1
+E8390_STOP
, ioaddr
+ E8390_CMD
);
280 regd
= inb(ioaddr
+ 0x0d);
281 outb(0xff, ioaddr
+ 0x0d);
282 outb(E8390_NODMA
+E8390_PAGE0
, ioaddr
+ E8390_CMD
);
283 inb(ioaddr
+ EN0_COUNTER0
); /* Clear the counter by reading. */
284 if (inb(ioaddr
+ EN0_COUNTER0
) != 0) {
286 outb(regd
, ioaddr
+ 0x0d); /* Restore the old values. */
291 dev
= init_etherdev(dev
, 0);
293 /* Reset card. Who knows what dain-bramaged state it was left in. */
295 unsigned long reset_start_time
= jiffies
;
297 outb(inb(ioaddr
+ NE_RESET
), ioaddr
+ NE_RESET
);
299 /* This looks like a horrible timing loop, but it should never take
300 more than a few cycles.
302 while ((inb(ioaddr
+ EN0_ISR
) & ENISR_RESET
) == 0)
303 /* Limit wait: '2' avoids jiffy roll-over. */
304 if (jiffies
- reset_start_time
> 2) {
305 printk("ne2k-pci: Card failure (no reset ack).\n");
309 outb(0xff, ioaddr
+ EN0_ISR
); /* Ack all intr. */
312 if (load_8390_module("ne2k-pci.c")) {
316 /* Read the 16 bytes of station address PROM.
317 We must first initialize registers, similar to NS8390_init(eifdev, 0).
318 We can't reliably read the SAPROM address without this.
319 (I learned the hard way!). */
321 struct {unsigned char value
, offset
; } program_seq
[] = {
322 {E8390_NODMA
+E8390_PAGE0
+E8390_STOP
, E8390_CMD
}, /* Select page 0*/
323 {0x49, EN0_DCFG
}, /* Set word-wide access. */
324 {0x00, EN0_RCNTLO
}, /* Clear the count regs. */
326 {0x00, EN0_IMR
}, /* Mask completion irq. */
328 {E8390_RXOFF
, EN0_RXCR
}, /* 0x20 Set to monitor */
329 {E8390_TXOFF
, EN0_TXCR
}, /* 0x02 and loopback mode. */
332 {0x00, EN0_RSARLO
}, /* DMA starting at 0x0000. */
334 {E8390_RREAD
+E8390_START
, E8390_CMD
},
336 for (i
= 0; i
< sizeof(program_seq
)/sizeof(program_seq
[0]); i
++)
337 outb(program_seq
[i
].value
, ioaddr
+ program_seq
[i
].offset
);
341 /* Note: all PCI cards have at least 16 bit access, so we don't have
342 to check for 8 bit cards. Most cards permit 32 bit access. */
343 if (pci_clone_list
[chip_idx
].flags
& ONLY_32BIT_IO
) {
344 for (i
= 0; i
< 4 ; i
++)
345 ((u32
*)SA_prom
)[i
] = le32_to_cpu(inl(ioaddr
+ NE_DATAPORT
));
347 for(i
= 0; i
< 32 /*sizeof(SA_prom)*/; i
++)
348 SA_prom
[i
] = inb(ioaddr
+ NE_DATAPORT
);
350 /* We always set the 8390 registers for word mode. */
351 outb(0x49, ioaddr
+ EN0_DCFG
);
352 start_page
= NESM_START_PG
;
355 pci_clone_list
[chip_idx
].flags
&STOP_PG_0x60
? 0x60 : NESM_STOP_PG
;
357 /* Set up the rest of the parameters. */
359 dev
->base_addr
= ioaddr
;
361 /* Allocate dev->priv and fill in 8390 specific dev fields. */
362 if (ethdev_init(dev
)) {
363 printk ("%s: unable to get memory for dev->priv.\n", dev
->name
);
367 request_region(ioaddr
, NE_IO_EXTENT
, dev
->name
);
369 printk("%s: %s found at %#lx, IRQ %d, ",
370 dev
->name
, pci_clone_list
[chip_idx
].name
, ioaddr
, dev
->irq
);
371 for(i
= 0; i
< 6; i
++) {
372 printk("%2.2X%s", SA_prom
[i
], i
== 5 ? ".\n": ":");
373 dev
->dev_addr
[i
] = SA_prom
[i
];
376 ei_status
.name
= pci_clone_list
[chip_idx
].name
;
377 ei_status
.tx_start_page
= start_page
;
378 ei_status
.stop_page
= stop_page
;
379 ei_status
.word16
= 1;
380 ei_status
.ne2k_flags
= pci_clone_list
[chip_idx
].flags
;
382 ei_status
.rx_start_page
= start_page
+ TX_PAGES
;
383 #ifdef PACKETBUF_MEMSIZE
384 /* Allow the packet buffer size to be overridden by know-it-alls. */
385 ei_status
.stop_page
= ei_status
.tx_start_page
+ PACKETBUF_MEMSIZE
;
388 ei_status
.reset_8390
= &ne2k_pci_reset_8390
;
389 ei_status
.block_input
= &ne2k_pci_block_input
;
390 ei_status
.block_output
= &ne2k_pci_block_output
;
391 ei_status
.get_8390_hdr
= &ne2k_pci_get_8390_hdr
;
392 dev
->open
= &ne2k_pci_open
;
393 dev
->stop
= &ne2k_pci_close
;
399 ne2k_pci_open(struct net_device
*dev
)
401 if (request_irq(dev
->irq
, ei_interrupt
, SA_SHIRQ
, dev
->name
, dev
))
409 ne2k_pci_close(struct net_device
*dev
)
412 free_irq(dev
->irq
, dev
);
417 /* Hard reset the card. This used to pause for the same period that a
418 8390 reset command required, but that shouldn't be necessary. */
420 ne2k_pci_reset_8390(struct net_device
*dev
)
422 unsigned long reset_start_time
= jiffies
;
424 if (debug
> 1) printk("%s: Resetting the 8390 t=%ld...",
427 outb(inb(NE_BASE
+ NE_RESET
), NE_BASE
+ NE_RESET
);
430 ei_status
.dmaing
= 0;
432 /* This check _should_not_ be necessary, omit eventually. */
433 while ((inb(NE_BASE
+EN0_ISR
) & ENISR_RESET
) == 0)
434 if (jiffies
- reset_start_time
> 2) {
435 printk("%s: ne2k_pci_reset_8390() did not complete.\n", dev
->name
);
438 outb(ENISR_RESET
, NE_BASE
+ EN0_ISR
); /* Ack intr. */
441 /* Grab the 8390 specific header. Similar to the block_input routine, but
442 we don't need to be concerned with ring wrap as the header will be at
443 the start of a page, so we optimize accordingly. */
446 ne2k_pci_get_8390_hdr(struct net_device
*dev
, struct e8390_pkt_hdr
*hdr
, int ring_page
)
449 long nic_base
= dev
->base_addr
;
451 /* This *shouldn't* happen. If it does, it's the last thing you'll see */
452 if (ei_status
.dmaing
) {
453 printk("%s: DMAing conflict in ne2k_pci_get_8390_hdr "
454 "[DMAstat:%d][irqlock:%d][intr:%d].\n",
455 dev
->name
, ei_status
.dmaing
, ei_status
.irqlock
,
456 (int)dev
->interrupt
);
460 ei_status
.dmaing
|= 0x01;
461 outb(E8390_NODMA
+E8390_PAGE0
+E8390_START
, nic_base
+ NE_CMD
);
462 outb(sizeof(struct e8390_pkt_hdr
), nic_base
+ EN0_RCNTLO
);
463 outb(0, nic_base
+ EN0_RCNTHI
);
464 outb(0, nic_base
+ EN0_RSARLO
); /* On page boundary */
465 outb(ring_page
, nic_base
+ EN0_RSARHI
);
466 outb(E8390_RREAD
+E8390_START
, nic_base
+ NE_CMD
);
468 if (ei_status
.ne2k_flags
& ONLY_16BIT_IO
) {
469 insw(NE_BASE
+ NE_DATAPORT
, hdr
, sizeof(struct e8390_pkt_hdr
)>>1);
471 *(u32
*)hdr
= le32_to_cpu(inl(NE_BASE
+ NE_DATAPORT
));
472 le16_to_cpus(&hdr
->count
);
475 outb(ENISR_RDC
, nic_base
+ EN0_ISR
); /* Ack intr. */
476 ei_status
.dmaing
&= ~0x01;
479 /* Block input and output, similar to the Crynwr packet driver. If you
480 are porting to a new ethercard, look at the packet driver source for hints.
481 The NEx000 doesn't share the on-board packet memory -- you have to put
482 the packet out through the "remote DMA" dataport using outb. */
485 ne2k_pci_block_input(struct net_device
*dev
, int count
, struct sk_buff
*skb
, int ring_offset
)
487 long nic_base
= dev
->base_addr
;
488 char *buf
= skb
->data
;
490 /* This *shouldn't* happen. If it does, it's the last thing you'll see */
491 if (ei_status
.dmaing
) {
492 printk("%s: DMAing conflict in ne2k_pci_block_input "
493 "[DMAstat:%d][irqlock:%d][intr:%d].\n",
494 dev
->name
, ei_status
.dmaing
, ei_status
.irqlock
,
495 (int)dev
->interrupt
);
498 ei_status
.dmaing
|= 0x01;
499 if (ei_status
.ne2k_flags
& ONLY_32BIT_IO
)
500 count
= (count
+ 3) & 0xFFFC;
501 outb(E8390_NODMA
+E8390_PAGE0
+E8390_START
, nic_base
+ NE_CMD
);
502 outb(count
& 0xff, nic_base
+ EN0_RCNTLO
);
503 outb(count
>> 8, nic_base
+ EN0_RCNTHI
);
504 outb(ring_offset
& 0xff, nic_base
+ EN0_RSARLO
);
505 outb(ring_offset
>> 8, nic_base
+ EN0_RSARHI
);
506 outb(E8390_RREAD
+E8390_START
, nic_base
+ NE_CMD
);
508 if (ei_status
.ne2k_flags
& ONLY_16BIT_IO
) {
509 insw(NE_BASE
+ NE_DATAPORT
,buf
,count
>>1);
511 buf
[count
-1] = inb(NE_BASE
+ NE_DATAPORT
);
514 insl(NE_BASE
+ NE_DATAPORT
, buf
, count
>>2);
518 *((u16
*)buf
)++ = le16_to_cpu(inw(NE_BASE
+ NE_DATAPORT
));
520 *buf
= inb(NE_BASE
+ NE_DATAPORT
);
524 outb(ENISR_RDC
, nic_base
+ EN0_ISR
); /* Ack intr. */
525 ei_status
.dmaing
&= ~0x01;
529 ne2k_pci_block_output(struct net_device
*dev
, int count
,
530 const unsigned char *buf
, const int start_page
)
532 long nic_base
= NE_BASE
;
533 unsigned long dma_start
;
535 /* On little-endian it's always safe to round the count up for
537 if (ei_status
.ne2k_flags
& ONLY_32BIT_IO
)
538 count
= (count
+ 3) & 0xFFFC;
543 /* This *shouldn't* happen. If it does, it's the last thing you'll see */
544 if (ei_status
.dmaing
) {
545 printk("%s: DMAing conflict in ne2k_pci_block_output."
546 "[DMAstat:%d][irqlock:%d][intr:%d]\n",
547 dev
->name
, ei_status
.dmaing
, ei_status
.irqlock
,
548 (int)dev
->interrupt
);
551 ei_status
.dmaing
|= 0x01;
552 /* We should already be in page 0, but to be safe... */
553 outb(E8390_PAGE0
+E8390_START
+E8390_NODMA
, nic_base
+ NE_CMD
);
555 #ifdef NE8390_RW_BUGFIX
556 /* Handle the read-before-write bug the same way as the
557 Crynwr packet driver -- the NatSemi method doesn't work.
558 Actually this doesn't always work either, but if you have
559 problems with your NEx000 this is better than nothing! */
560 outb(0x42, nic_base
+ EN0_RCNTLO
);
561 outb(0x00, nic_base
+ EN0_RCNTHI
);
562 outb(0x42, nic_base
+ EN0_RSARLO
);
563 outb(0x00, nic_base
+ EN0_RSARHI
);
564 outb(E8390_RREAD
+E8390_START
, nic_base
+ NE_CMD
);
566 outb(ENISR_RDC
, nic_base
+ EN0_ISR
);
568 /* Now the normal output. */
569 outb(count
& 0xff, nic_base
+ EN0_RCNTLO
);
570 outb(count
>> 8, nic_base
+ EN0_RCNTHI
);
571 outb(0x00, nic_base
+ EN0_RSARLO
);
572 outb(start_page
, nic_base
+ EN0_RSARHI
);
573 outb(E8390_RWRITE
+E8390_START
, nic_base
+ NE_CMD
);
574 if (ei_status
.ne2k_flags
& ONLY_16BIT_IO
) {
575 outsw(NE_BASE
+ NE_DATAPORT
, buf
, count
>>1);
577 outsl(NE_BASE
+ NE_DATAPORT
, buf
, count
>>2);
581 outw(cpu_to_le16(*((u16
*)buf
)++), NE_BASE
+ NE_DATAPORT
);
587 while ((inb(nic_base
+ EN0_ISR
) & ENISR_RDC
) == 0)
588 if (jiffies
- dma_start
> 2) { /* Avoid clock roll-over. */
589 printk("%s: timeout waiting for Tx RDC.\n", dev
->name
);
590 ne2k_pci_reset_8390(dev
);
595 outb(ENISR_RDC
, nic_base
+ EN0_ISR
); /* Ack intr. */
596 ei_status
.dmaing
&= ~0x01;
603 * compile-command: "gcc -DMODVERSIONS -DMODULE -D__KERNEL__ -Wall -Wstrict-prototypes -O6 -fomit-frame-pointer -I/usr/src/linux/drivers/net/ -c ne2k-pci.c"
604 * alt-compile-command: "gcc -DMODULE -D__KERNEL__ -Wall -Wstrict-prototypes -O6 -fomit-frame-pointer -I/usr/src/linux/drivers/net/ -c ne2k-pci.c"
609 * kept-new-versions: 5