⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 winbond-840.c

📁 Linux内核源代码 为压缩文件 是<<Linux内核>>一书中的源代码
💻 C
📖 第 1 页 / 共 3 页
字号:
/* winbond-840.c: A Linux PCI network adapter skeleton device driver. *//*	Written 1998-2000 by Donald Becker.	This software may be used and distributed according to the terms of	the GNU General Public License (GPL), incorporated herein by reference.	Drivers based on or derived from this code fall under the GPL and must	retain the authorship, copyright and license notice.  This file is not	a complete program and may only be used when the entire operating	system is licensed under the GPL.	The author may be reached as becker@scyld.com, or C/O	Scyld Computing Corporation	410 Severn Ave., Suite 210	Annapolis MD 21403	Support and updates available at	http://www.scyld.com/network/drivers.html	Do not remove the copyright infomation.	Do not change the version information unless an improvement has been made.	Merely removing my name, as Compex has done in the past, does not count	as an improvement.	Changelog:	* ported to 2.4		???	* spin lock update, memory barriers, new style dma mappings		limit each tx buffer to < 1024 bytes		remove DescIntr from Rx descriptors (that's an Tx flag)		remove next pointer from Tx descriptors		synchronize tx_q_bytes		software reset in tx_timeout			Copyright (C) 2000 Manfred Spraul	TODO:	* according to the documentation, the chip supports big endian		descriptors. Remove cpu_to_le32, enable BE descriptors.*//* These identify the driver base version and may not be removed. */static const char version1[] ="winbond-840.c:v1.01 (2.4 port) 5/15/2000  Donald Becker <becker@scyld.com>\n";static const char version2[] ="  http://www.scyld.com/network/drivers.html\n";/* Automatically extracted configuration info:probe-func: winbond840_probeconfig-in: tristate 'Winbond W89c840 Ethernet support' CONFIG_WINBOND_840c-help-name: Winbond W89c840 PCI Ethernet supportc-help-symbol: CONFIG_WINBOND_840c-help: This driver is for the Winbond W89c840 chip.  It also works withc-help: the TX9882 chip on the Compex RL100-ATX board.c-help: More specific information and updates are available from c-help: http://www.scyld.com/network/drivers.html*//* The user-configurable values.   These may be modified when a driver module is loaded.*/static int debug = 1;			/* 1 normal messages, 0 quiet .. 7 verbose. */static int max_interrupt_work = 20;/* Maximum number of multicast addresses to filter (vs. Rx-all-multicast).   The '840 uses a 64 element hash table based on the Ethernet CRC.  */static int multicast_filter_limit = 32;/* Set the copy breakpoint for the copy-only-tiny-frames scheme.   Setting to > 1518 effectively disables this feature. */static int rx_copybreak = 0;/* Used to pass the media type, etc.   Both 'options[]' and 'full_duplex[]' should exist for driver   interoperability.   The media type is usually passed in 'options[]'.*/#define MAX_UNITS 8		/* More are supported, limit only on options */static int options[MAX_UNITS] = {-1, -1, -1, -1, -1, -1, -1, -1};static int full_duplex[MAX_UNITS] = {-1, -1, -1, -1, -1, -1, -1, -1};/* Operational parameters that are set at compile time. *//* Keep the ring sizes a power of two for compile efficiency.   The compiler will convert <unsigned>'%'<2^N> into a bit mask.   Making the Tx ring too large decreases the effectiveness of channel   bonding and packet priority.   There are no ill effects from too-large receive rings. */#define TX_RING_SIZE	16#define TX_QUEUE_LEN	10		/* Limit ring entries actually used.  */#define RX_RING_SIZE	32/* The presumed FIFO size for working around the Tx-FIFO-overflow bug.   To avoid overflowing we don't queue again until we have room for a   full-size packet. */#define TX_FIFO_SIZE (2048)#define TX_BUG_FIFO_LIMIT (TX_FIFO_SIZE-1514-16)#define TX_BUFLIMIT	(1024-128)/* Operational parameters that usually are not changed. *//* Time in jiffies before concluding the transmitter is hung. */#define TX_TIMEOUT  (2*HZ)#define PKT_BUF_SZ		1536			/* Size of each temporary Rx buffer.*/#ifndef __KERNEL__#define __KERNEL__#endif#if !defined(__OPTIMIZE__)#warning  You must compile this file with the correct options!#warning  See the last lines of the source file.#error You must compile this driver with "-O".#endif/* Include files, designed to support most kernel versions 2.0.0 and later. */#include <linux/module.h>#include <linux/kernel.h>#include <linux/string.h>#include <linux/timer.h>#include <linux/errno.h>#include <linux/ioport.h>#include <linux/malloc.h>#include <linux/interrupt.h>#include <linux/pci.h>#include <linux/netdevice.h>#include <linux/etherdevice.h>#include <linux/skbuff.h>#include <linux/init.h>#include <asm/processor.h>		/* Processor type for cache alignment. */#include <asm/bitops.h>#include <asm/io.h>#include <asm/delay.h>MODULE_AUTHOR("Donald Becker <becker@scyld.com>");MODULE_DESCRIPTION("Winbond W89c840 Ethernet driver");MODULE_PARM(max_interrupt_work, "i");MODULE_PARM(debug, "i");MODULE_PARM(rx_copybreak, "i");MODULE_PARM(multicast_filter_limit, "i");MODULE_PARM(options, "1-" __MODULE_STRING(MAX_UNITS) "i");MODULE_PARM(full_duplex, "1-" __MODULE_STRING(MAX_UNITS) "i");/*				Theory of OperationI. Board CompatibilityThis driver is for the Winbond w89c840 chip.II. Board-specific settingsNone.III. Driver operationThis chip is very similar to the Digital 21*4* "Tulip" family.  The firsttwelve registers and the descriptor format are nearly identical.  Read aTulip manual for operational details.A significant difference is that the multicast filter and station address arestored in registers rather than loaded through a pseudo-transmit packet.Unlike the Tulip, transmit buffers are limited to 1KB.  To transmit afull-sized packet we must use both data buffers in a descriptor.  Thus thedriver uses ring mode where descriptors are implicitly sequential in memory,rather than using the second descriptor address as a chain pointer tosubsequent descriptors.IV. NotesIf you are going to almost clone a Tulip, why not go all the way and avoidthe need for a new driver?IVb. Referenceshttp://www.scyld.com/expert/100mbps.htmlhttp://www.scyld.com/expert/NWay.htmlhttp://www.winbond.com.tw/IVc. ErrataA horrible bug exists in the transmit FIFO.  Apparently the chip doesn'tcorrectly detect a full FIFO, and queuing more than 2048 bytes may result insilent data corruption.*//*  PCI probe table.*/enum pci_id_flags_bits {        /* Set PCI command register bits before calling probe1(). */        PCI_USES_IO=1, PCI_USES_MEM=2, PCI_USES_MASTER=4,        /* Read and map the single following PCI BAR. */        PCI_ADDR0=0<<4, PCI_ADDR1=1<<4, PCI_ADDR2=2<<4, PCI_ADDR3=3<<4,        PCI_ADDR_64BITS=0x100, PCI_NO_ACPI_WAKE=0x200, PCI_NO_MIN_LATENCY=0x400,};enum chip_capability_flags {CanHaveMII=1, HasBrokenTx=2};#ifdef USE_IO_OPS#define W840_FLAGS (PCI_USES_IO | PCI_ADDR0 | PCI_USES_MASTER)#else#define W840_FLAGS (PCI_USES_MEM | PCI_ADDR1 | PCI_USES_MASTER)#endifstatic struct pci_device_id w840_pci_tbl[] __devinitdata = {	{ 0x1050, 0x0840, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0 },	{ 0x11f6, 0x2011, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 1 },	{ 0, }};MODULE_DEVICE_TABLE(pci, w840_pci_tbl);struct pci_id_info {        const char *name;        struct match_info {                int     pci, pci_mask, subsystem, subsystem_mask;                int revision, revision_mask;                            /* Only 8 bits. */        } id;        enum pci_id_flags_bits pci_flags;        int io_size;                            /* Needed for I/O region check or ioremap(). */        int drv_flags;                          /* Driver use, intended as capability flags. */};static struct pci_id_info pci_id_tbl[] = {	{"Winbond W89c840", { 0x08401050, 0xffffffff, },	 W840_FLAGS, 128, CanHaveMII | HasBrokenTx},	{"Compex RL100-ATX", { 0x201111F6, 0xffffffff,},	 W840_FLAGS, 128, CanHaveMII | HasBrokenTx},	{0,},						/* 0 terminated list. */};/* This driver was written to use PCI memory space, however some x86 systems   work only with I/O space accesses.  Pass -DUSE_IO_OPS to use PCI I/O space   accesses instead of memory space. */#ifdef USE_IO_OPS#undef readb#undef readw#undef readl#undef writeb#undef writew#undef writel#define readb inb#define readw inw#define readl inl#define writeb outb#define writew outw#define writel outl#endif/* Offsets to the Command and Status Registers, "CSRs".   While similar to the Tulip, these registers are longword aligned.   Note: It's not useful to define symbolic names for every register bit in   the device.  The name can only partially document the semantics and make   the driver longer and more difficult to read.*/enum w840_offsets {	PCIBusCfg=0x00, TxStartDemand=0x04, RxStartDemand=0x08,	RxRingPtr=0x0C, TxRingPtr=0x10,	IntrStatus=0x14, NetworkConfig=0x18, IntrEnable=0x1C,	RxMissed=0x20, EECtrl=0x24, MIICtrl=0x24, BootRom=0x28, GPTimer=0x2C,	CurRxDescAddr=0x30, CurRxBufAddr=0x34,			/* Debug use */	MulticastFilter0=0x38, MulticastFilter1=0x3C, StationAddr=0x40,	CurTxDescAddr=0x4C, CurTxBufAddr=0x50,};/* Bits in the interrupt status/enable registers. *//* The bits in the Intr Status/Enable registers, mostly interrupt sources. */enum intr_status_bits {	NormalIntr=0x10000, AbnormalIntr=0x8000,	IntrPCIErr=0x2000, TimerInt=0x800,	IntrRxDied=0x100, RxNoBuf=0x80, IntrRxDone=0x40,	TxFIFOUnderflow=0x20, RxErrIntr=0x10,	TxIdle=0x04, IntrTxStopped=0x02, IntrTxDone=0x01,};/* Bits in the NetworkConfig register. */enum rx_mode_bits {	AcceptErr=0x80, AcceptRunt=0x40,	AcceptBroadcast=0x20, AcceptMulticast=0x10,	AcceptAllPhys=0x08, AcceptMyPhys=0x02,};enum mii_reg_bits {	MDIO_ShiftClk=0x10000, MDIO_DataIn=0x80000, MDIO_DataOut=0x20000,	MDIO_EnbOutput=0x40000, MDIO_EnbIn = 0x00000,};/* The Tulip Rx and Tx buffer descriptors. */struct w840_rx_desc {	s32 status;	s32 length;	u32 buffer1;	u32 buffer2;};struct w840_tx_desc {	s32 status;	s32 length;	u32 buffer1, buffer2;				/* We use only buffer 1.  */};/* Bits in network_desc.status */enum desc_status_bits {	DescOwn=0x80000000, DescEndRing=0x02000000, DescUseLink=0x01000000,	DescWholePkt=0x60000000, DescStartPkt=0x20000000, DescEndPkt=0x40000000,};/* Bits in w840_tx_desc.length */enum desc_length_bits {	DescIntr=0x80000000,};#define PRIV_ALIGN	15 	/* Required alignment mask */struct netdev_private {	struct w840_rx_desc *rx_ring;	dma_addr_t	rx_addr[RX_RING_SIZE];	struct w840_tx_desc *tx_ring;	dma_addr_t	tx_addr[RX_RING_SIZE];	dma_addr_t ring_dma_addr;	struct pci_dev *pdev;	/* The addresses of receive-in-place skbuffs. */	struct sk_buff* rx_skbuff[RX_RING_SIZE];	/* The saved address of a sent-in-place packet/buffer, for later free(). */	struct sk_buff* tx_skbuff[TX_RING_SIZE];	struct net_device_stats stats;	struct timer_list timer;	/* Media monitoring timer. */	/* Frequently used values: keep some adjacent for cache effect. */	spinlock_t lock;	int chip_id, drv_flags;	int csr6;	struct w840_rx_desc *rx_head_desc;	unsigned int cur_rx, dirty_rx;		/* Producer/consumer ring indices */	unsigned int rx_buf_sz;				/* Based on MTU+slack. */	unsigned int cur_tx, dirty_tx;	int tx_q_bytes;	unsigned int tx_full:1;				/* The Tx queue is full. */	/* These values are keep track of the transceiver/media in use. */	unsigned int full_duplex:1;			/* Full-duplex operation requested. */	unsigned int duplex_lock:1;	unsigned int medialock:1;			/* Do not sense media. */	unsigned int default_port:4;		/* Last dev->if_port value. */	/* MII transceiver section. */	int mii_cnt;						/* MII device addresses. */	u16 advertising;					/* NWay media advertisement */	unsigned char phys[2];				/* MII device addresses. */};static int  eeprom_read(long ioaddr, int location);static int  mdio_read(struct net_device *dev, int phy_id, int location);static void mdio_write(struct net_device *dev, int phy_id, int location, int value);static int  netdev_open(struct net_device *dev);static void check_duplex(struct net_device *dev);static void netdev_timer(unsigned long data);static void init_rxtx_rings(struct net_device *dev);static void free_rxtx_rings(struct netdev_private *np);static void init_registers(struct net_device *dev);static void tx_timeout(struct net_device *dev);static int alloc_ring(struct net_device *dev);static int  start_tx(struct sk_buff *skb, struct net_device *dev);static void intr_handler(int irq, void *dev_instance, struct pt_regs *regs);static void netdev_error(struct net_device *dev, int intr_status);static int  netdev_rx(struct net_device *dev);static inline unsigned ether_crc(int length, unsigned char *data);static void set_rx_mode(struct net_device *dev);static struct net_device_stats *get_stats(struct net_device *dev);static int mii_ioctl(struct net_device *dev, struct ifreq *rq, int cmd);static int  netdev_close(struct net_device *dev);static int __devinit w840_probe1 (struct pci_dev *pdev,				  const struct pci_device_id *ent){	struct net_device *dev;	struct netdev_private *np;	static int find_cnt;	int chip_idx = ent->driver_data;	int irq = pdev->irq;	int i, option = find_cnt < MAX_UNITS ? options[find_cnt] : 0;	long ioaddr;	if (pci_enable_device(pdev))		return -EIO;	pci_set_master(pdev);	if(!pci_dma_supported(pdev,0xFFFFffff)) {		printk(KERN_WARNING "Winbond-840: Device %s disabled due to DMA limitations.\n",				pdev->name);		return -EIO;	}	dev = init_etherdev(NULL, sizeof(*np));	if (!dev)		return -ENOMEM;	SET_MODULE_OWNER(dev);#ifdef USE_IO_OPS	ioaddr = pci_resource_start(pdev, 0);	if (!request_region(ioaddr, pci_id_tbl[chip_idx].io_size, dev->name))		goto err_out_netdev;#else	ioaddr = pci_resource_start(pdev, 1);	if (!request_mem_region(ioaddr, pci_id_tbl[chip_idx].io_size, dev->name))		goto err_out_netdev;	ioaddr = (long) ioremap (ioaddr, pci_id_tbl[chip_idx].io_size);	if (!ioaddr)		goto err_out_iomem;#endif	printk(KERN_INFO "%s: %s at 0x%lx, ",		   dev->name, pci_id_tbl[chip_idx].name, ioaddr);	/* Warning: broken for big-endian machines. */	for (i = 0; i < 3; i++)		((u16 *)dev->dev_addr)[i] = le16_to_cpu(eeprom_read(ioaddr, i));	for (i = 0; i < 5; i++)			printk("%2.2x:", dev->dev_addr[i]);	printk("%2.2x, IRQ %d.\n", dev->dev_addr[i], irq);	/* Reset the chip to erase previous misconfiguration.	   No hold time required! */	writel(0x00000001, ioaddr + PCIBusCfg);	dev->base_addr = ioaddr;	dev->irq = irq;	np = dev->priv;	np->chip_id = chip_idx;	np->drv_flags = pci_id_tbl[chip_idx].drv_flags;	np->pdev = pdev;	spin_lock_init(&np->lock);		pdev->driver_data = dev;	if (dev->mem_start)		option = dev->mem_start;	/* The lower four bits are the media type. */	if (option > 0) {		if (option & 0x200)			np->full_duplex = 1;		np->default_port = option & 15;		if (np->default_port)			np->medialock = 1;	}	if (find_cnt < MAX_UNITS  &&  full_duplex[find_cnt] > 0)		np->full_duplex = 1;	if (np->full_duplex)		np->duplex_lock = 1;	/* The chip-specific entries in the device structure. */	dev->open = &netdev_open;	dev->hard_start_xmit = &start_tx;	dev->stop = &netdev_close;	dev->get_stats = &get_stats;	dev->set_multicast_list = &set_rx_mode;	dev->do_ioctl = &mii_ioctl;	dev->tx_timeout = &tx_timeout;	dev->watchdog_timeo = TX_TIMEOUT;	if (np->drv_flags & CanHaveMII) {		int phy, phy_idx = 0;		for (phy = 1; phy < 32 && phy_idx < 4; phy++) {			int mii_status = mdio_read(dev, phy, 1);			if (mii_status != 0xffff  &&  mii_status != 0x0000) {				np->phys[phy_idx++] = phy;				np->advertising = mdio_read(dev, phy, 4);				printk(KERN_INFO "%s: MII PHY found at address %d, status "					   "0x%4.4x advertising %4.4x.\n",					   dev->name, phy, mii_status, np->advertising);			}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -