/* ne2k-pci.c: A NE2000 clone on PCI bus driver for Linux. */ /* A Linux device driver for PCI NE2000 clones. Authorship and other copyrights: 1992-1998 by Donald Becker, NE2000 core and various modifications. 1995-1998 by Paul Gortmaker, core modifications and PCI support. Copyright 1993 assigned to the United States Government as represented by the Director, National Security Agency. This software may be used and distributed according to the terms of the GNU Public License, incorporated herein by reference. The author may be reached as becker@CESDIS.gsfc.nasa.gov, or C/O Center of Excellence in Space Data and Information Sciences Code 930.5, Goddard Space Flight Center, Greenbelt MD 20771 People are making PCI ne2000 clones! Oh the horror, the horror... Issues remaining: No full-duplex support. */ /* Our copyright info must remain in the binary. */ static const char *version = "ne2k-pci.c:vpre-1.00e 5/27/99 D. Becker/P. Gortmaker http://cesdis.gsfc.nasa.gov/linux/drivers/ne2k-pci.html\n"; #include #include #include #include #include #include #include #include #include #include #include #include "8390.h" #if defined(__powerpc__) #define inl_le(addr) le32_to_cpu(inl(addr)) #define inw_le(addr) le16_to_cpu(inw(addr)) #define insl insl_ns #define outsl outsl_ns #endif /* Set statically or when loading the driver module. */ static int debug = 1; /* Some defines that people can play with if so inclined. */ /* Use 32 bit data-movement operations instead of 16 bit. */ #define USE_LONGIO /* Do we implement the read before write bugfix ? */ /* #define NE_RW_BUGFIX */ /* Do we have a non std. amount of memory? (in units of 256 byte pages) */ /* #define PACKETBUF_MEMSIZE 0x40 */ #define ne2k_flags reg0 /* Rename an existing field to store flags! */ /* Only the low 8 bits are usable for non-init-time flags! */ enum { HOLTEK_FDX=1, /* Full duplex -> set 0x80 at offset 0x20. */ ONLY_16BIT_IO=2, ONLY_32BIT_IO=4, /* Chip can do only 16/32-bit xfers. */ STOP_PG_0x60=0x100, }; enum ne2k_pci_chipsets { CH_RealTek_RTL_8029 = 0, CH_Winbond_89C940, CH_Compex_RL2000, CH_KTI_ET32P2, CH_NetVin_NV5000SC, CH_Via_86C926, CH_SureCom_NE34, CH_Winbond_W89C940F, CH_Holtek_HT80232, CH_Holtek_HT80229, }; static struct { char *name; int flags; } pci_clone_list[] __devinitdata = { {"RealTek RTL-8029", 0}, {"Winbond 89C940", 0}, {"Compex RL2000", 0}, {"KTI ET32P2", 0}, {"NetVin NV5000SC", 0}, {"Via 86C926", ONLY_16BIT_IO}, {"SureCom NE34", 0}, {"Winbond W89C940F", 0}, {"Holtek HT80232", ONLY_16BIT_IO | HOLTEK_FDX}, {"Holtek HT80229", ONLY_32BIT_IO | HOLTEK_FDX | STOP_PG_0x60 }, {0,} }; static struct pci_device_id ne2k_pci_tbl[] __devinitdata = { { 0x10ec, 0x8029, PCI_ANY_ID, PCI_ANY_ID, 0, 0, CH_RealTek_RTL_8029 }, { 0x1050, 0x0940, PCI_ANY_ID, PCI_ANY_ID, 0, 0, CH_Winbond_89C940 }, { 0x11f6, 0x1401, PCI_ANY_ID, PCI_ANY_ID, 0, 0, CH_Compex_RL2000 }, { 0x8e2e, 0x3000, PCI_ANY_ID, PCI_ANY_ID, 0, 0, CH_KTI_ET32P2 }, { 0x4a14, 0x5000, PCI_ANY_ID, PCI_ANY_ID, 0, 0, CH_NetVin_NV5000SC }, { 0x1106, 0x0926, PCI_ANY_ID, PCI_ANY_ID, 0, 0, CH_Via_86C926 }, { 0x10bd, 0x0e34, PCI_ANY_ID, PCI_ANY_ID, 0, 0, CH_SureCom_NE34 }, { 0x1050, 0x5a5a, PCI_ANY_ID, PCI_ANY_ID, 0, 0, CH_Winbond_W89C940F }, { 0x12c3, 0x0058, PCI_ANY_ID, PCI_ANY_ID, 0, 0, CH_Holtek_HT80232 }, { 0x12c3, 0x5598, PCI_ANY_ID, PCI_ANY_ID, 0, 0, CH_Holtek_HT80229 }, { 0, }, }; MODULE_DEVICE_TABLE(pci, ne2k_pci_tbl); /* ---- No user-serviceable parts below ---- */ #define NE_BASE (dev->base_addr) #define NE_CMD 0x00 #define NE_DATAPORT 0x10 /* NatSemi-defined port window offset. */ #define NE_RESET 0x1f /* Issue a read to reset, a write to clear. */ #define NE_IO_EXTENT 0x20 #define NESM_START_PG 0x40 /* First page of TX buffer */ #define NESM_STOP_PG 0x80 /* Last page +1 of RX ring */ static int ne2k_pci_open(struct net_device *dev); static int ne2k_pci_close(struct net_device *dev); static void ne2k_pci_reset_8390(struct net_device *dev); static void ne2k_pci_get_8390_hdr(struct net_device *dev, struct e8390_pkt_hdr *hdr, int ring_page); static void ne2k_pci_block_input(struct net_device *dev, int count, struct sk_buff *skb, int ring_offset); static void ne2k_pci_block_output(struct net_device *dev, const int count, const unsigned char *buf, const int start_page); /* No room in the standard 8390 structure for extra info we need. */ struct ne2k_pci_card { struct net_device *dev; struct pci_dev *pci_dev; }; /* NEx000-clone boards have a Station Address (SA) PROM (SAPROM) in the packet buffer memory space. By-the-spec NE2000 clones have 0x57,0x57 in bytes 0x0e,0x0f of the SAPROM, while other supposed NE2000 clones must be detected by their SA prefix. Reading the SAPROM from a word-wide card with the 8390 set in byte-wide mode results in doubled values, which can be detected and compensated for. The probe is also responsible for initializing the card and filling in the 'dev' and 'ei_status' structures. */ static int __devinit ne2k_pci_init_one (struct pci_dev *pdev, const struct pci_device_id *ent) { struct net_device *dev; int i, irq, reg0, start_page, stop_page; unsigned char SA_prom[32]; int chip_idx = ent->driver_data; static unsigned version_printed = 0; long ioaddr; if (version_printed++ == 0) printk(KERN_INFO "%s", version); ioaddr = pci_resource_start (pdev, 0); irq = pdev->irq; if (!ioaddr || ((pci_resource_flags (pdev, 0) & IORESOURCE_IO) == 0)) { printk (KERN_ERR "ne2k-pci: no I/O resource at PCI BAR #0\n"); return -ENODEV; } if (pci_enable_device (pdev)) { printk (KERN_ERR "ne2k-pci: cannot enable device\n"); return -EIO; } if (request_region (ioaddr, NE_IO_EXTENT, "ne2k-pci") == NULL) { printk (KERN_ERR "ne2k-pci: I/O resource 0x%x @ 0x%lx busy\n", NE_IO_EXTENT, ioaddr); return -EBUSY; } reg0 = inb(ioaddr); if (reg0 == 0xFF) goto err_out_free_res; /* Do a preliminary verification that we have a 8390. */ { int regd; outb(E8390_NODMA+E8390_PAGE1+E8390_STOP, ioaddr + E8390_CMD); regd = inb(ioaddr + 0x0d); outb(0xff, ioaddr + 0x0d); outb(E8390_NODMA+E8390_PAGE0, ioaddr + E8390_CMD); inb(ioaddr + EN0_COUNTER0); /* Clear the counter by reading. */ if (inb(ioaddr + EN0_COUNTER0) != 0) { outb(reg0, ioaddr); outb(regd, ioaddr + 0x0d); /* Restore the old values. */ goto err_out_free_res; } } dev = init_etherdev(NULL, 0); if (!dev) { printk (KERN_ERR "ne2k-pci: cannot allocate ethernet device\n"); goto err_out_free_res; } /* Reset card. Who knows what dain-bramaged state it was left in. */ { unsigned long reset_start_time = jiffies; outb(inb(ioaddr + NE_RESET), ioaddr + NE_RESET); /* This looks like a horrible timing loop, but it should never take more than a few cycles. */ while ((inb(ioaddr + EN0_ISR) & ENISR_RESET) == 0) /* Limit wait: '2' avoids jiffy roll-over. */ if (jiffies - reset_start_time > 2) { printk("ne2k-pci: Card failure (no reset ack).\n"); goto err_out_free_netdev; } outb(0xff, ioaddr + EN0_ISR); /* Ack all intr. */ } if (load_8390_module("ne2k-pci.c")) { printk (KERN_ERR "ne2k-pci: cannot load 8390 module\n"); goto err_out_free_netdev; } /* Read the 16 bytes of station address PROM. We must first initialize registers, similar to NS8390_init(eifdev, 0). We can't reliably read the SAPROM address without this. (I learned the hard way!). */ { struct {unsigned char value, offset; } program_seq[] = { {E8390_NODMA+E8390_PAGE0+E8390_STOP, E8390_CMD}, /* Select page 0*/ {0x49, EN0_DCFG}, /* Set word-wide access. */ {0x00, EN0_RCNTLO}, /* Clear the count regs. */ {0x00, EN0_RCNTHI}, {0x00, EN0_IMR}, /* Mask completion irq. */ {0xFF, EN0_ISR}, {E8390_RXOFF, EN0_RXCR}, /* 0x20 Set to monitor */ {E8390_TXOFF, EN0_TXCR}, /* 0x02 and loopback mode. */ {32, EN0_RCNTLO}, {0x00, EN0_RCNTHI}, {0x00, EN0_RSARLO}, /* DMA starting at 0x0000. */ {0x00, EN0_RSARHI}, {E8390_RREAD+E8390_START, E8390_CMD}, }; for (i = 0; i < sizeof(program_seq)/sizeof(program_seq[0]); i++) outb(program_seq[i].value, ioaddr + program_seq[i].offset); } /* Note: all PCI cards have at least 16 bit access, so we don't have to check for 8 bit cards. Most cards permit 32 bit access. */ if (pci_clone_list[chip_idx].flags & ONLY_32BIT_IO) { for (i = 0; i < 4 ; i++) ((u32 *)SA_prom)[i] = le32_to_cpu(inl(ioaddr + NE_DATAPORT)); } else for(i = 0; i < 32 /*sizeof(SA_prom)*/; i++) SA_prom[i] = inb(ioaddr + NE_DATAPORT); /* We always set the 8390 registers for word mode. */ outb(0x49, ioaddr + EN0_DCFG); start_page = NESM_START_PG; stop_page = pci_clone_list[chip_idx].flags&STOP_PG_0x60 ? 0x60 : NESM_STOP_PG; /* Set up the rest of the parameters. */ dev->irq = irq; dev->base_addr = ioaddr; /* Allocate dev->priv and fill in 8390 specific dev fields. */ if (ethdev_init(dev)) { printk (KERN_ERR "%s: unable to get memory for dev->priv.\n", dev->name); goto err_out_free_netdev; } printk("%s: %s found at %#lx, IRQ %d, ", dev->name, pci_clone_list[chip_idx].name, ioaddr, dev->irq); for(i = 0; i < 6; i++) { printk("%2.2X%s", SA_prom[i], i == 5 ? ".\n": ":"); dev->dev_addr[i] = SA_prom[i]; } ei_status.name = pci_clone_list[chip_idx].name; ei_status.tx_start_page = start_page; ei_status.stop_page = stop_page; ei_status.word16 = 1; ei_status.ne2k_flags = pci_clone_list[chip_idx].flags; ei_status.rx_start_page = start_page + TX_PAGES; #ifdef PACKETBUF_MEMSIZE /* Allow the packet buffer size to be overridden by know-it-alls. */ ei_status.stop_page = ei_status.tx_start_page + PACKETBUF_MEMSIZE; #endif ei_status.reset_8390 = &ne2k_pci_reset_8390; ei_status.block_input = &ne2k_pci_block_input; ei_status.block_output = &ne2k_pci_block_output; ei_status.get_8390_hdr = &ne2k_pci_get_8390_hdr; dev->open = &ne2k_pci_open; dev->stop = &ne2k_pci_close; NS8390_init(dev, 0); return 0; err_out_free_netdev: unregister_netdev (dev); kfree (dev); err_out_free_res: release_region (ioaddr, NE_IO_EXTENT); return -ENODEV; } static int ne2k_pci_open(struct net_device *dev) { MOD_INC_USE_COUNT; if (request_irq(dev->irq, ei_interrupt, SA_SHIRQ, dev->name, dev)) { MOD_DEC_USE_COUNT; return -EAGAIN; } ei_open(dev); return 0; } static int ne2k_pci_close(struct net_device *dev) { ei_close(dev); free_irq(dev->irq, dev); MOD_DEC_USE_COUNT; return 0; } /* Hard reset the card. This used to pause for the same period that a 8390 reset command required, but that shouldn't be necessary. */ static void ne2k_pci_reset_8390(struct net_device *dev) { unsigned long reset_start_time = jiffies; if (debug > 1) printk("%s: Resetting the 8390 t=%ld...", dev->name, jiffies); outb(inb(NE_BASE + NE_RESET), NE_BASE + NE_RESET); ei_status.txing = 0; ei_status.dmaing = 0; /* This check _should_not_ be necessary, omit eventually. */ while ((inb(NE_BASE+EN0_ISR) & ENISR_RESET) == 0) if (jiffies - reset_start_time > 2) { printk("%s: ne2k_pci_reset_8390() did not complete.\n", dev->name); break; } outb(ENISR_RESET, NE_BASE + EN0_ISR); /* Ack intr. */ } /* Grab the 8390 specific header. Similar to the block_input routine, but we don't need to be concerned with ring wrap as the header will be at the start of a page, so we optimize accordingly. */ static void ne2k_pci_get_8390_hdr(struct net_device *dev, struct e8390_pkt_hdr *hdr, int ring_page) { long nic_base = dev->base_addr; /* This *shouldn't* happen. If it does, it's the last thing you'll see */ if (ei_status.dmaing) { printk("%s: DMAing conflict in ne2k_pci_get_8390_hdr " "[DMAstat:%d][irqlock:%d].\n", dev->name, ei_status.dmaing, ei_status.irqlock); return; } ei_status.dmaing |= 0x01; outb(E8390_NODMA+E8390_PAGE0+E8390_START, nic_base+ NE_CMD); outb(sizeof(struct e8390_pkt_hdr), nic_base + EN0_RCNTLO); outb(0, nic_base + EN0_RCNTHI); outb(0, nic_base + EN0_RSARLO); /* On page boundary */ outb(ring_page, nic_base + EN0_RSARHI); outb(E8390_RREAD+E8390_START, nic_base + NE_CMD); if (ei_status.ne2k_flags & ONLY_16BIT_IO) { insw(NE_BASE + NE_DATAPORT, hdr, sizeof(struct e8390_pkt_hdr)>>1); } else { *(u32*)hdr = le32_to_cpu(inl(NE_BASE + NE_DATAPORT)); le16_to_cpus(&hdr->count); } outb(ENISR_RDC, nic_base + EN0_ISR); /* Ack intr. */ ei_status.dmaing &= ~0x01; } /* Block input and output, similar to the Crynwr packet driver. If you are porting to a new ethercard, look at the packet driver source for hints. The NEx000 doesn't share the on-board packet memory -- you have to put the packet out through the "remote DMA" dataport using outb. */ static void ne2k_pci_block_input(struct net_device *dev, int count, struct sk_buff *skb, int ring_offset) { long nic_base = dev->base_addr; char *buf = skb->data; /* This *shouldn't* happen. If it does, it's the last thing you'll see */ if (ei_status.dmaing) { printk("%s: DMAing conflict in ne2k_pci_block_input " "[DMAstat:%d][irqlock:%d].\n", dev->name, ei_status.dmaing, ei_status.irqlock); return; } ei_status.dmaing |= 0x01; if (ei_status.ne2k_flags & ONLY_32BIT_IO) count = (count + 3) & 0xFFFC; outb(E8390_NODMA+E8390_PAGE0+E8390_START, nic_base+ NE_CMD); outb(count & 0xff, nic_base + EN0_RCNTLO); outb(count >> 8, nic_base + EN0_RCNTHI); outb(ring_offset & 0xff, nic_base + EN0_RSARLO); outb(ring_offset >> 8, nic_base + EN0_RSARHI); outb(E8390_RREAD+E8390_START, nic_base + NE_CMD); if (ei_status.ne2k_flags & ONLY_16BIT_IO) { insw(NE_BASE + NE_DATAPORT,buf,count>>1); if (count & 0x01) { buf[count-1] = inb(NE_BASE + NE_DATAPORT); } } else { insl(NE_BASE + NE_DATAPORT, buf, count>>2); if (count & 3) { buf += count & ~3; if (count & 2) *((u16*)buf)++ = le16_to_cpu(inw(NE_BASE + NE_DATAPORT)); if (count & 1) *buf = inb(NE_BASE + NE_DATAPORT); } } outb(ENISR_RDC, nic_base + EN0_ISR); /* Ack intr. */ ei_status.dmaing &= ~0x01; } static void ne2k_pci_block_output(struct net_device *dev, int count, const unsigned char *buf, const int start_page) { long nic_base = NE_BASE; unsigned long dma_start; /* On little-endian it's always safe to round the count up for word writes. */ if (ei_status.ne2k_flags & ONLY_32BIT_IO) count = (count + 3) & 0xFFFC; else if (count & 0x01) count++; /* This *shouldn't* happen. If it does, it's the last thing you'll see */ if (ei_status.dmaing) { printk("%s: DMAing conflict in ne2k_pci_block_output." "[DMAstat:%d][irqlock:%d]\n", dev->name, ei_status.dmaing, ei_status.irqlock); return; } ei_status.dmaing |= 0x01; /* We should already be in page 0, but to be safe... */ outb(E8390_PAGE0+E8390_START+E8390_NODMA, nic_base + NE_CMD); #ifdef NE8390_RW_BUGFIX /* Handle the read-before-write bug the same way as the Crynwr packet driver -- the NatSemi method doesn't work. Actually this doesn't always work either, but if you have problems with your NEx000 this is better than nothing! */ outb(0x42, nic_base + EN0_RCNTLO); outb(0x00, nic_base + EN0_RCNTHI); outb(0x42, nic_base + EN0_RSARLO); outb(0x00, nic_base + EN0_RSARHI); outb(E8390_RREAD+E8390_START, nic_base + NE_CMD); #endif outb(ENISR_RDC, nic_base + EN0_ISR); /* Now the normal output. */ outb(count & 0xff, nic_base + EN0_RCNTLO); outb(count >> 8, nic_base + EN0_RCNTHI); outb(0x00, nic_base + EN0_RSARLO); outb(start_page, nic_base + EN0_RSARHI); outb(E8390_RWRITE+E8390_START, nic_base + NE_CMD); if (ei_status.ne2k_flags & ONLY_16BIT_IO) { outsw(NE_BASE + NE_DATAPORT, buf, count>>1); } else { outsl(NE_BASE + NE_DATAPORT, buf, count>>2); if (count & 3) { buf += count & ~3; if (count & 2) outw(cpu_to_le16(*((u16*)buf)++), NE_BASE + NE_DATAPORT); } } dma_start = jiffies; while ((inb(nic_base + EN0_ISR) & ENISR_RDC) == 0) if (jiffies - dma_start > 2) { /* Avoid clock roll-over. */ printk("%s: timeout waiting for Tx RDC.\n", dev->name); ne2k_pci_reset_8390(dev); NS8390_init(dev,1); break; } outb(ENISR_RDC, nic_base + EN0_ISR); /* Ack intr. */ ei_status.dmaing &= ~0x01; return; } static void __devexit ne2k_pci_remove_one (struct pci_dev *pdev) { struct net_device *dev = pdev->driver_data; if (!dev) { printk (KERN_ERR "bug! ne2k_pci_remove_one called w/o net_device\n"); return; } unregister_netdev (dev); release_region (dev->base_addr, NE_IO_EXTENT); kfree (dev); } static struct pci_driver ne2k_driver = { name: "ne2k-pci", probe: ne2k_pci_init_one, remove: ne2k_pci_remove_one, id_table: ne2k_pci_tbl, }; static int __init ne2k_pci_init(void) { int rc; MOD_INC_USE_COUNT; lock_8390_module(); rc = pci_module_init (&ne2k_driver); /* XXX should this test CONFIG_HOTPLUG like pci_module_init? */ if (rc <= 0) unlock_8390_module(); MOD_DEC_USE_COUNT; return rc; } static void __exit ne2k_pci_cleanup(void) { pci_unregister_driver (&ne2k_driver); unlock_8390_module(); } module_init(ne2k_pci_init); module_exit(ne2k_pci_cleanup); /* * Local variables: * compile-command: "gcc -DMODVERSIONS -DMODULE -D__KERNEL__ -Wall -Wstrict-prototypes -O6 -fomit-frame-pointer -I/usr/src/linux/drivers/net/ -c ne2k-pci.c" * alt-compile-command: "gcc -DMODULE -D__KERNEL__ -Wall -Wstrict-prototypes -O6 -fomit-frame-pointer -I/usr/src/linux/drivers/net/ -c ne2k-pci.c" * c-indent-level: 4 * c-basic-offset: 4 * tab-width: 4 * version-control: t * kept-new-versions: 5 * End: */