Linux 2.2.0
[davej-history.git] / drivers / net / ne2k-pci.c
blob1b48abd365796b4936b3ea28d00c07f64c9d3989
1 /* ne2k-pci.c: A NE2000 clone on PCI bus driver for Linux. */
2 /*
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...
21 Issues remaining:
22 No full-duplex support.
25 /* Our copyright info must remain in the binary. */
26 static const char *version =
27 "ne2k-pci.c:v0.99L 2/7/98 D. Becker/P. Gortmaker http://cesdis.gsfc.nasa.gov/linux/drivers/ne2k-pci.html\n";
29 #ifdef MODVERSIONS
30 #include <linux/modversions.h>
31 #endif
32 #include <linux/module.h>
33 #include <linux/kernel.h>
34 #include <linux/sched.h>
35 #include <linux/errno.h>
36 #include <linux/pci.h>
37 #include <linux/init.h>
39 #include <asm/system.h>
40 #include <asm/io.h>
41 #include <asm/irq.h>
43 #include <linux/netdevice.h>
44 #include <linux/etherdevice.h>
45 #include "8390.h"
47 /* Set statically or when loading the driver module. */
48 static int debug = 1;
50 /* Some defines that people can play with if so inclined. */
52 /* Use 32 bit data-movement operations instead of 16 bit. */
53 #define USE_LONGIO
55 /* Do we implement the read before write bugfix ? */
56 /* #define NE_RW_BUGFIX */
58 /* Do we have a non std. amount of memory? (in units of 256 byte pages) */
59 /* #define PACKETBUF_MEMSIZE 0x40 */
61 static struct {
62 unsigned short vendor, dev_id;
63 char *name;
65 pci_clone_list[] __initdata = {
66 {0x10ec, 0x8029, "RealTek RTL-8029"},
67 {0x1050, 0x0940, "Winbond 89C940"},
68 {0x11f6, 0x1401, "Compex RL2000"},
69 {0x8e2e, 0x3000, "KTI ET32P2"},
70 {0x4a14, 0x5000, "NetVin NV5000SC"},
71 {0x1106, 0x0926, "Via 82C926"},
72 {0x10bd, 0x0e34, "SureCom NE34"},
73 {0x1050, 0x5a5a, "Winbond"},
74 {0,}
77 /* ---- No user-serviceable parts below ---- */
79 #define NE_BASE (dev->base_addr)
80 #define NE_CMD 0x00
81 #define NE_DATAPORT 0x10 /* NatSemi-defined port window offset. */
82 #define NE_RESET 0x1f /* Issue a read to reset, a write to clear. */
83 #define NE_IO_EXTENT 0x20
85 #define NESM_START_PG 0x40 /* First page of TX buffer */
86 #define NESM_STOP_PG 0x80 /* Last page +1 of RX ring */
88 int ne2k_pci_probe(struct device *dev);
89 static struct device *ne2k_pci_probe1(struct device *dev, int ioaddr, int irq);
91 static int ne2k_pci_open(struct device *dev);
92 static int ne2k_pci_close(struct device *dev);
94 static void ne2k_pci_reset_8390(struct device *dev);
95 static void ne2k_pci_get_8390_hdr(struct device *dev, struct e8390_pkt_hdr *hdr,
96 int ring_page);
97 static void ne2k_pci_block_input(struct device *dev, int count,
98 struct sk_buff *skb, int ring_offset);
99 static void ne2k_pci_block_output(struct device *dev, const int count,
100 const unsigned char *buf, const int start_page);
104 /* No room in the standard 8390 structure for extra info we need. */
105 struct ne2k_pci_card {
106 struct ne2k_pci_card *next;
107 struct device *dev;
108 struct pci_dev *pci_dev;
110 /* A list of all installed devices, for removing the driver module. */
111 static struct ne2k_pci_card *ne2k_card_list = NULL;
113 #ifdef MODULE
116 init_module(void)
118 int retval;
120 /* We must emit version information. */
121 if (debug)
122 printk(KERN_INFO "%s", version);
124 retval = ne2k_pci_probe(0);
126 if (retval) {
127 printk(KERN_NOTICE "ne2k-pci.c: no (useable) cards found, driver NOT installed.\n");
128 return retval;
130 lock_8390_module();
131 return 0;
134 void
135 cleanup_module(void)
137 struct device *dev;
138 struct ne2k_pci_card *this_card;
140 /* No need to check MOD_IN_USE, as sys_delete_module() checks. */
141 while (ne2k_card_list) {
142 dev = ne2k_card_list->dev;
143 unregister_netdev(dev);
144 release_region(dev->base_addr, NE_IO_EXTENT);
145 kfree(dev);
146 this_card = ne2k_card_list;
147 ne2k_card_list = ne2k_card_list->next;
148 kfree(this_card);
150 unlock_8390_module();
153 #endif /* MODULE */
156 NEx000-clone boards have a Station Address (SA) PROM (SAPROM) in the packet
157 buffer memory space. By-the-spec NE2000 clones have 0x57,0x57 in bytes
158 0x0e,0x0f of the SAPROM, while other supposed NE2000 clones must be
159 detected by their SA prefix.
161 Reading the SAPROM from a word-wide card with the 8390 set in byte-wide
162 mode results in doubled values, which can be detected and compensated for.
164 The probe is also responsible for initializing the card and filling
165 in the 'dev' and 'ei_status' structures.
168 #ifdef HAVE_DEVLIST
169 struct netdev_entry netcard_drv =
170 {"ne2k_pci", ne2k_pci_probe1, NE_IO_EXTENT, 0};
171 #endif
173 __initfunc (int ne2k_pci_probe(struct device *dev))
175 struct pci_dev *pdev = NULL;
176 int cards_found = 0;
177 int i;
179 if ( ! pci_present())
180 return -ENODEV;
182 while ((pdev = pci_find_class(PCI_CLASS_NETWORK_ETHERNET << 8, pdev)) != NULL) {
183 u8 pci_irq_line;
184 u16 pci_command, new_command;
185 u32 pci_ioaddr;
187 /* Note: some vendor IDs (RealTek) have non-NE2k cards as well. */
188 for (i = 0; pci_clone_list[i].vendor != 0; i++)
189 if (pci_clone_list[i].vendor == pdev->vendor
190 && pci_clone_list[i].dev_id == pdev->device)
191 break;
192 if (pci_clone_list[i].vendor == 0)
193 continue;
195 pci_ioaddr = pdev->base_address[0] & PCI_BASE_ADDRESS_IO_MASK;
196 pci_irq_line = pdev->irq;
197 pci_read_config_word(pdev, PCI_COMMAND, &pci_command);
199 /* Avoid already found cards from previous calls */
200 if (check_region(pci_ioaddr, NE_IO_EXTENT))
201 continue;
203 #ifndef MODULE
205 static unsigned version_printed = 0;
206 if (version_printed++ == 0)
207 printk(KERN_INFO "%s", version);
209 #endif
211 /* Activate the card: fix for brain-damaged Win98 BIOSes. */
212 new_command = pci_command | PCI_COMMAND_IO;
213 if (pci_command != new_command) {
214 printk(KERN_INFO " The PCI BIOS has not enabled this"
215 " NE2k clone! Updating PCI command %4.4x->%4.4x.\n",
216 pci_command, new_command);
217 pci_write_config_word(pdev, PCI_COMMAND, new_command);
220 if (pci_irq_line <= 0 || pci_irq_line >= NR_IRQS)
221 printk(KERN_WARNING " WARNING: The PCI BIOS assigned this PCI NE2k"
222 " card to IRQ %d, which is unlikely to work!.\n"
223 KERN_WARNING " You should use the PCI BIOS setup to assign"
224 " a valid IRQ line.\n", pci_irq_line);
226 printk("ne2k-pci.c: PCI NE2000 clone '%s' at I/O %#x, IRQ %d.\n",
227 pci_clone_list[i].name, pci_ioaddr, pci_irq_line);
228 dev = ne2k_pci_probe1(dev, pci_ioaddr, pci_irq_line);
229 if (dev == 0) {
230 /* Should not happen. */
231 printk(KERN_ERR "ne2k-pci: Probe of PCI card at %#x failed.\n",
232 pci_ioaddr);
233 continue;
234 } else {
235 struct ne2k_pci_card *ne2k_card =
236 kmalloc(sizeof(struct ne2k_pci_card), GFP_KERNEL);
237 ne2k_card->next = ne2k_card_list;
238 ne2k_card_list = ne2k_card;
239 ne2k_card->dev = dev;
240 ne2k_card->pci_dev = pdev;
242 dev = 0;
244 cards_found++;
247 return cards_found ? 0 : -ENODEV;
250 __initfunc (static struct device *ne2k_pci_probe1(struct device *dev, int ioaddr, int irq))
252 int i;
253 unsigned char SA_prom[32];
254 const char *name = NULL;
255 int start_page, stop_page;
256 int reg0 = inb(ioaddr);
258 if (reg0 == 0xFF)
259 return 0;
261 /* Do a preliminary verification that we have a 8390. */
263 int regd;
264 outb(E8390_NODMA+E8390_PAGE1+E8390_STOP, ioaddr + E8390_CMD);
265 regd = inb(ioaddr + 0x0d);
266 outb(0xff, ioaddr + 0x0d);
267 outb(E8390_NODMA+E8390_PAGE0, ioaddr + E8390_CMD);
268 inb(ioaddr + EN0_COUNTER0); /* Clear the counter by reading. */
269 if (inb(ioaddr + EN0_COUNTER0) != 0) {
270 outb(reg0, ioaddr);
271 outb(regd, ioaddr + 0x0d); /* Restore the old values. */
272 return 0;
276 /* Reset card. Who knows what dain-bramaged state it was left in. */
278 unsigned long reset_start_time = jiffies;
280 outb(inb(ioaddr + NE_RESET), ioaddr + NE_RESET);
282 /* This looks like a horrible timing loop, but it should never take
283 more than a few cycles.
285 while ((inb(ioaddr + EN0_ISR) & ENISR_RESET) == 0)
286 /* Limit wait: '2' avoids jiffy roll-over. */
287 if (jiffies - reset_start_time > 2) {
288 printk("ne2k-pci: Card failure (no reset ack).\n");
289 return 0;
292 outb(0xff, ioaddr + EN0_ISR); /* Ack all intr. */
295 if (load_8390_module("ne2k-pci.c")) {
296 return 0;
299 /* Read the 16 bytes of station address PROM.
300 We must first initialize registers, similar to NS8390_init(eifdev, 0).
301 We can't reliably read the SAPROM address without this.
302 (I learned the hard way!). */
304 struct {unsigned char value, offset; } program_seq[] = {
305 {E8390_NODMA+E8390_PAGE0+E8390_STOP, E8390_CMD}, /* Select page 0*/
306 {0x49, EN0_DCFG}, /* Set word-wide access. */
307 {0x00, EN0_RCNTLO}, /* Clear the count regs. */
308 {0x00, EN0_RCNTHI},
309 {0x00, EN0_IMR}, /* Mask completion irq. */
310 {0xFF, EN0_ISR},
311 {E8390_RXOFF, EN0_RXCR}, /* 0x20 Set to monitor */
312 {E8390_TXOFF, EN0_TXCR}, /* 0x02 and loopback mode. */
313 {32, EN0_RCNTLO},
314 {0x00, EN0_RCNTHI},
315 {0x00, EN0_RSARLO}, /* DMA starting at 0x0000. */
316 {0x00, EN0_RSARHI},
317 {E8390_RREAD+E8390_START, E8390_CMD},
319 for (i = 0; i < sizeof(program_seq)/sizeof(program_seq[0]); i++)
320 outb(program_seq[i].value, ioaddr + program_seq[i].offset);
324 #ifdef notdef
325 /* Some broken PCI cards don't respect the byte-wide
326 request in program_seq above, and hence don't have doubled up values.
328 for(i = 0; i < 32 /*sizeof(SA_prom)*/; i+=2) {
329 SA_prom[i] = inb(ioaddr + NE_DATAPORT);
330 SA_prom[i+1] = inb(ioaddr + NE_DATAPORT);
331 if (SA_prom[i] != SA_prom[i+1])
332 sa_prom_doubled = 0;
335 if (sa_prom_doubled)
336 for (i = 0; i < 16; i++)
337 SA_prom[i] = SA_prom[i+i];
338 #else
339 for(i = 0; i < 32 /*sizeof(SA_prom)*/; i++)
340 SA_prom[i] = inb(ioaddr + NE_DATAPORT);
342 #endif
344 /* We always set the 8390 registers for word mode. */
345 outb(0x49, ioaddr + EN0_DCFG);
346 start_page = NESM_START_PG;
347 stop_page = NESM_STOP_PG;
349 /* Set up the rest of the parameters. */
350 name = "PCI NE2000";
352 dev = init_etherdev(dev, 0);
354 dev->irq = irq;
355 dev->base_addr = ioaddr;
357 /* Allocate dev->priv and fill in 8390 specific dev fields. */
358 if (ethdev_init(dev)) {
359 printk ("%s: unable to get memory for dev->priv.\n", dev->name);
360 kfree(dev);
361 return 0;
364 request_region(ioaddr, NE_IO_EXTENT, dev->name);
366 printk("%s: %s found at %#x, IRQ %d, ",
367 dev->name, name, ioaddr, dev->irq);
368 for(i = 0; i < 6; i++) {
369 printk("%2.2X%s", SA_prom[i], i == 5 ? ".\n": ":");
370 dev->dev_addr[i] = SA_prom[i];
373 ei_status.name = name;
374 ei_status.tx_start_page = start_page;
375 ei_status.stop_page = stop_page;
376 ei_status.word16 = 1;
378 ei_status.rx_start_page = start_page + TX_PAGES;
379 #ifdef PACKETBUF_MEMSIZE
380 /* Allow the packet buffer size to be overridden by know-it-alls. */
381 ei_status.stop_page = ei_status.tx_start_page + PACKETBUF_MEMSIZE;
382 #endif
384 ei_status.reset_8390 = &ne2k_pci_reset_8390;
385 ei_status.block_input = &ne2k_pci_block_input;
386 ei_status.block_output = &ne2k_pci_block_output;
387 ei_status.get_8390_hdr = &ne2k_pci_get_8390_hdr;
388 dev->open = &ne2k_pci_open;
389 dev->stop = &ne2k_pci_close;
390 NS8390_init(dev, 0);
391 return dev;
394 static int
395 ne2k_pci_open(struct device *dev)
397 if (request_irq(dev->irq, ei_interrupt, SA_SHIRQ, dev->name, dev))
398 return -EAGAIN;
399 ei_open(dev);
400 MOD_INC_USE_COUNT;
401 return 0;
404 static int
405 ne2k_pci_close(struct device *dev)
407 ei_close(dev);
408 free_irq(dev->irq, dev);
409 MOD_DEC_USE_COUNT;
410 return 0;
413 /* Hard reset the card. This used to pause for the same period that a
414 8390 reset command required, but that shouldn't be necessary. */
415 static void
416 ne2k_pci_reset_8390(struct device *dev)
418 unsigned long reset_start_time = jiffies;
420 if (debug > 1) printk("%s: Resetting the 8390 t=%ld...",
421 dev->name, jiffies);
423 outb(inb(NE_BASE + NE_RESET), NE_BASE + NE_RESET);
425 ei_status.txing = 0;
426 ei_status.dmaing = 0;
428 /* This check _should_not_ be necessary, omit eventually. */
429 while ((inb(NE_BASE+EN0_ISR) & ENISR_RESET) == 0)
430 if (jiffies - reset_start_time > 2) {
431 printk("%s: ne2k_pci_reset_8390() did not complete.\n", dev->name);
432 break;
434 outb(ENISR_RESET, NE_BASE + EN0_ISR); /* Ack intr. */
437 /* Grab the 8390 specific header. Similar to the block_input routine, but
438 we don't need to be concerned with ring wrap as the header will be at
439 the start of a page, so we optimize accordingly. */
441 static void
442 ne2k_pci_get_8390_hdr(struct device *dev, struct e8390_pkt_hdr *hdr, int ring_page)
445 int nic_base = dev->base_addr;
447 /* This *shouldn't* happen. If it does, it's the last thing you'll see */
448 if (ei_status.dmaing) {
449 printk("%s: DMAing conflict in ne2k_pci_get_8390_hdr "
450 "[DMAstat:%d][irqlock:%d][intr:%ld].\n",
451 dev->name, ei_status.dmaing, ei_status.irqlock,
452 dev->interrupt);
453 return;
456 ei_status.dmaing |= 0x01;
457 outb(E8390_NODMA+E8390_PAGE0+E8390_START, nic_base+ NE_CMD);
458 outb(sizeof(struct e8390_pkt_hdr), nic_base + EN0_RCNTLO);
459 outb(0, nic_base + EN0_RCNTHI);
460 outb(0, nic_base + EN0_RSARLO); /* On page boundary */
461 outb(ring_page, nic_base + EN0_RSARHI);
462 outb(E8390_RREAD+E8390_START, nic_base + NE_CMD);
464 #if defined(USE_LONGIO)
465 *(u32*)hdr = inl(NE_BASE + NE_DATAPORT);
466 #else
467 insw(NE_BASE + NE_DATAPORT, hdr, sizeof(struct e8390_pkt_hdr)>>1);
468 #endif
470 outb(ENISR_RDC, nic_base + EN0_ISR); /* Ack intr. */
471 ei_status.dmaing &= ~0x01;
474 /* Block input and output, similar to the Crynwr packet driver. If you
475 are porting to a new ethercard, look at the packet driver source for hints.
476 The NEx000 doesn't share the on-board packet memory -- you have to put
477 the packet out through the "remote DMA" dataport using outb. */
479 static void
480 ne2k_pci_block_input(struct device *dev, int count, struct sk_buff *skb, int ring_offset)
482 int nic_base = dev->base_addr;
483 char *buf = skb->data;
485 /* This *shouldn't* happen. If it does, it's the last thing you'll see */
486 if (ei_status.dmaing) {
487 printk("%s: DMAing conflict in ne2k_pci_block_input "
488 "[DMAstat:%d][irqlock:%d][intr:%ld].\n",
489 dev->name, ei_status.dmaing, ei_status.irqlock,
490 dev->interrupt);
491 return;
493 ei_status.dmaing |= 0x01;
494 outb(E8390_NODMA+E8390_PAGE0+E8390_START, nic_base+ NE_CMD);
495 outb(count & 0xff, nic_base + EN0_RCNTLO);
496 outb(count >> 8, nic_base + EN0_RCNTHI);
497 outb(ring_offset & 0xff, nic_base + EN0_RSARLO);
498 outb(ring_offset >> 8, nic_base + EN0_RSARHI);
499 outb(E8390_RREAD+E8390_START, nic_base + NE_CMD);
501 #if defined(USE_LONGIO)
502 insl(NE_BASE + NE_DATAPORT, buf, count>>2);
503 if (count & 3) {
504 buf += count & ~3;
505 if (count & 2)
506 *((u16*)buf)++ = inw(NE_BASE + NE_DATAPORT);
507 if (count & 1)
508 *buf = inb(NE_BASE + NE_DATAPORT);
510 #else
511 insw(NE_BASE + NE_DATAPORT,buf,count>>1);
512 if (count & 0x01) {
513 buf[count-1] = inb(NE_BASE + NE_DATAPORT);
515 #endif
517 outb(ENISR_RDC, nic_base + EN0_ISR); /* Ack intr. */
518 ei_status.dmaing &= ~0x01;
521 static void
522 ne2k_pci_block_output(struct device *dev, int count,
523 const unsigned char *buf, const int start_page)
525 int nic_base = NE_BASE;
526 unsigned long dma_start;
528 /* On little-endian it's always safe to round the count up for
529 word writes. */
530 if (count & 0x01)
531 count++;
533 /* This *shouldn't* happen. If it does, it's the last thing you'll see */
534 if (ei_status.dmaing) {
535 printk("%s: DMAing conflict in ne2k_pci_block_output."
536 "[DMAstat:%d][irqlock:%d][intr:%ld]\n",
537 dev->name, ei_status.dmaing, ei_status.irqlock,
538 dev->interrupt);
539 return;
541 ei_status.dmaing |= 0x01;
542 /* We should already be in page 0, but to be safe... */
543 outb(E8390_PAGE0+E8390_START+E8390_NODMA, nic_base + NE_CMD);
545 #ifdef NE8390_RW_BUGFIX
546 /* Handle the read-before-write bug the same way as the
547 Crynwr packet driver -- the NatSemi method doesn't work.
548 Actually this doesn't always work either, but if you have
549 problems with your NEx000 this is better than nothing! */
550 outb(0x42, nic_base + EN0_RCNTLO);
551 outb(0x00, nic_base + EN0_RCNTHI);
552 outb(0x42, nic_base + EN0_RSARLO);
553 outb(0x00, nic_base + EN0_RSARHI);
554 outb(E8390_RREAD+E8390_START, nic_base + NE_CMD);
555 #endif
556 outb(ENISR_RDC, nic_base + EN0_ISR);
558 /* Now the normal output. */
559 outb(count & 0xff, nic_base + EN0_RCNTLO);
560 outb(count >> 8, nic_base + EN0_RCNTHI);
561 outb(0x00, nic_base + EN0_RSARLO);
562 outb(start_page, nic_base + EN0_RSARHI);
563 outb(E8390_RWRITE+E8390_START, nic_base + NE_CMD);
564 #if defined(USE_LONGIO)
565 outsl(NE_BASE + NE_DATAPORT, buf, count>>2);
566 if (count & 3) {
567 buf += count & ~3;
568 if (count & 2)
569 outw(*((u16*)buf)++, NE_BASE + NE_DATAPORT);
571 #else
572 outsw(NE_BASE + NE_DATAPORT, buf, count>>1);
573 #endif
575 dma_start = jiffies;
577 while ((inb(nic_base + EN0_ISR) & ENISR_RDC) == 0)
578 if (jiffies - dma_start > 2) { /* Avoid clock roll-over. */
579 printk("%s: timeout waiting for Tx RDC.\n", dev->name);
580 ne2k_pci_reset_8390(dev);
581 NS8390_init(dev,1);
582 break;
585 outb(ENISR_RDC, nic_base + EN0_ISR); /* Ack intr. */
586 ei_status.dmaing &= ~0x01;
587 return;
592 * Local variables:
593 * compile-command: "gcc -DMODVERSIONS -DMODULE -D__KERNEL__ -Wall -Wstrict-prototypes -O6 -fomit-frame-pointer -I/usr/src/linux/drivers/net/ -c ne2k-pci.c"
594 * alt-compile-command: "gcc -DMODULE -D__KERNEL__ -Wall -Wstrict-prototypes -O6 -fomit-frame-pointer -I/usr/src/linux/drivers/net/ -c ne2k-pci.c"
595 * c-indent-level: 4
596 * c-basic-offset: 4
597 * tab-width: 4
598 * version-control: t
599 * kept-new-versions: 5
600 * End: