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

📄 ethernet.c

📁 上传linux-jx2410的源代码
💻 C
📖 第 1 页 / 共 3 页
字号:
/* $Id: ethernet.c,v 1.1.1.1 2004/02/04 12:55:28 laputa Exp $ * * e100net.c: A network driver for the ETRAX 100LX network controller. * * Copyright (c) 1998-2001 Axis Communications AB. * * The outline of this driver comes from skeleton.c. * * $Log: ethernet.c,v $ * Revision 1.1.1.1  2004/02/04 12:55:28  laputa * rel-1-0-0 laputa: s3c2410 smdk *                   - the file for default configuration is "arch/arm/def-configs/smdk2410" *                   - the default lcd controller is set the aiji board base *                   - if you need to support the meritech board you should be *                     changed aiji item off of menuconfig * * Revision 1.1.1.1  2004/01/14 04:41:38  laputa * dev-0-0-1 laputa: initial import of 2410 kernel  *                   - the file for default configuration is "arch/arm/def-configs/smdk2410" *                   - the default lcd controller is set the aiji board base * * Revision 1.22  2002/01/30 07:48:22  matsfg * Initiate R_NETWORK_TR_CTRL * * Revision 1.21  2001/11/23 11:54:49  starvik * Added IFF_PROMISC and IFF_ALLMULTI handling in set_multicast_list * Removed compiler warnings * * Revision 1.20  2001/11/12 19:26:00  pkj * * Corrected e100_negotiate() to not assign half to current_duplex when *   it was supposed to compare them... * * Cleaned up failure handling in e100_open(). * * Fixed compiler warnings. * * Revision 1.19  2001/11/09 07:43:09  starvik * Added full duplex support * Added ioctl to set speed and duplex * Clear LED timer only runs when LED is lit * * Revision 1.18  2001/10/03 14:40:43  jonashg * Update rx_bytes counter. * * Revision 1.17  2001/06/11 12:43:46  olof * Modified defines for network LED behavior * * Revision 1.16  2001/05/30 06:12:46  markusl * TxDesc.next should not be set to NULL * * Revision 1.15  2001/05/29 10:27:04  markusl * Updated after review remarks: * +Use IO_EXTRACT * +Handle underrun * * Revision 1.14  2001/05/29 09:20:14  jonashg * Use driver name on printk output so one can tell which driver that complains. * * Revision 1.13  2001/05/09 12:35:59  johana * Use DMA_NBR and IRQ_NBR defines from dma.h and irq.h * * Revision 1.12  2001/04/05 11:43:11  tobiasa * Check dev before panic. * * Revision 1.11  2001/04/04 11:21:05  markusl * Updated according to review remarks * * Revision 1.10  2001/03/26 16:03:06  bjornw * Needs linux/config.h * * Revision 1.9  2001/03/19 14:47:48  pkj * * Make sure there is always a pause after the network LEDs are *   changed so they will not look constantly lit during heavy traffic. * * Always use HZ when setting times relative to jiffies. * * Use LED_NETWORK_SET() when setting the network LEDs. * * Revision 1.8  2001/02/27 13:52:48  bjornw * malloc.h -> slab.h * * Revision 1.7  2001/02/23 13:46:38  bjornw * Spellling check * * Revision 1.6  2001/01/26 15:21:04  starvik * Don't disable interrupts while reading MDIO registers (MDIO is slow) * Corrected promiscuous mode * Improved deallocation of IRQs ("ifconfig eth0 down" now works) * * Revision 1.5  2000/11/29 17:22:22  bjornw * Get rid of the udword types legacy stuff * * Revision 1.4  2000/11/22 16:36:09  bjornw * Please marketing by using the correct case when spelling Etrax. * * Revision 1.3  2000/11/21 16:43:04  bjornw * Minor short->int change * * Revision 1.2  2000/11/08 14:27:57  bjornw * 2.4 port * * Revision 1.1  2000/11/06 13:56:00  bjornw * Verbatim copy of the 1.24 version of e100net.c from elinux * * Revision 1.24  2000/10/04 15:55:23  bjornw * * Use virt_to_phys etc. for DMA addresses * * Removed bogus CHECKSUM_UNNECESSARY * * */#include <linux/config.h>#include <linux/module.h>#include <linux/kernel.h>#include <linux/sched.h>#include <linux/delay.h>#include <linux/types.h>#include <linux/fcntl.h>#include <linux/interrupt.h>#include <linux/ptrace.h>#include <linux/ioport.h>#include <linux/in.h>#include <linux/slab.h>#include <linux/string.h>#include <linux/spinlock.h>#include <linux/errno.h>#include <linux/init.h>#include <linux/netdevice.h>#include <linux/etherdevice.h>#include <linux/skbuff.h>#include <asm/svinto.h>     /* DMA and register descriptions */#include <asm/io.h>         /* LED_* I/O functions */#include <asm/irq.h>#include <asm/dma.h>#include <asm/system.h>#include <asm/bitops.h>#include <asm/ethernet.h>//#define ETHDEBUG#define D(x)/* * The name of the card. Is used for messages and in the requests for * io regions, irqs and dma channels */static const char* cardname = "ETRAX 100LX built-in ethernet controller";/* A default ethernet address. Highlevel SW will set the real one later */static struct sockaddr default_mac = {	0,	{ 0x00, 0x40, 0x8C, 0xCD, 0x00, 0x00 }};/* Information that need to be kept for each board. */struct net_local {	struct net_device_stats stats;	/* Tx control lock.  This protects the transmit buffer ring	 * state along with the "tx full" state of the driver.  This	 * means all netif_queue flow control actions are protected	 * by this lock as well.	 */	spinlock_t lock;};/* Duplex settings */enum duplex{	half,	full,	autoneg};/* Dma descriptors etc. */#define RX_BUF_SIZE 32768#define MAX_MEDIA_DATA_SIZE 1518#define MIN_PACKET_LEN      46#define ETHER_HEAD_LEN      14/* ** MDIO constants.*/#define MDIO_BASE_STATUS_REG                0x1#define MDIO_BASE_CONTROL_REG               0x0#define MDIO_BC_NEGOTIATE                0x0200#define MDIO_BC_FULL_DUPLEX_MASK         0x0100#define MDIO_BC_AUTO_NEG_MASK            0x1000#define MDIO_BC_SPEED_SELECT_MASK        0x2000#define MDIO_ADVERTISMENT_REG               0x4#define MDIO_ADVERT_100_FD                0x100#define MDIO_ADVERT_100_HD                0x080#define MDIO_ADVERT_10_FD                 0x040#define MDIO_ADVERT_10_HD                 0x020#define MDIO_LINK_UP_MASK                   0x4#define MDIO_START                          0x1#define MDIO_READ                           0x2#define MDIO_WRITE                          0x1#define MDIO_PREAMBLE              0xfffffffful/* Broadcom specific */#define MDIO_AUX_CTRL_STATUS_REG           0x18#define MDIO_FULL_DUPLEX_IND                0x1#define MDIO_SPEED                          0x2#define MDIO_PHYS_ADDR                      0x0/* Network flash constants */#define NET_FLASH_TIME                  (HZ/50) /* 20 ms */#define NET_FLASH_PAUSE                (HZ/100) /* 10 ms */#define NET_LINK_UP_CHECK_INTERVAL       (2*HZ) /* 2 s   */#define NET_DUPLEX_CHECK_INTERVAL        (2*HZ) /* 2 s   */#define NO_NETWORK_ACTIVITY 0#define NETWORK_ACTIVITY    1#define RX_DESC_BUF_SIZE   256#define NBR_OF_RX_DESC     (RX_BUF_SIZE / \                            RX_DESC_BUF_SIZE)#define GET_BIT(bit,val)   (((val) >> (bit)) & 0x01)/* Define some macros to access ETRAX 100 registers */#define SETF(var, reg, field, val) var = (var & ~IO_MASK(##reg##, field)) | \					  IO_FIELD(##reg##, field, val)#define SETS(var, reg, field, val) var = (var & ~IO_MASK(##reg##, field)) | \					  IO_STATE(##reg##, field, val)static etrax_dma_descr *myNextRxDesc;  /* Points to the next descriptor to                                          to be processed */static etrax_dma_descr *myLastRxDesc;  /* The last processed descriptor */static etrax_dma_descr *myPrevRxDesc;  /* The descriptor right before myNextRxDesc */static unsigned char RxBuf[RX_BUF_SIZE];static etrax_dma_descr RxDescList[NBR_OF_RX_DESC] __attribute__ ((aligned(4)));static etrax_dma_descr TxDesc __attribute__ ((aligned(4)));static struct sk_buff *tx_skb;static unsigned int network_rec_config_shadow = 0;/* Network speed indication. */static struct timer_list speed_timer;static struct timer_list clear_led_timer;static int current_speed; /* Speed read from tranceiver */static int current_speed_selection; /* Speed selected by user */static int led_next_time;static int led_active;/* Duplex */static struct timer_list duplex_timer;static int full_duplex;static enum duplex current_duplex;/* Index to functions, as function prototypes. */static int etrax_ethernet_init(struct net_device *dev);static int e100_open(struct net_device *dev);static int e100_set_mac_address(struct net_device *dev, void *addr);static int e100_send_packet(struct sk_buff *skb, struct net_device *dev);static void e100rx_interrupt(int irq, void *dev_id, struct pt_regs *regs);static void e100tx_interrupt(int irq, void *dev_id, struct pt_regs *regs);static void e100nw_interrupt(int irq, void *dev_id, struct pt_regs *regs);static void e100_rx(struct net_device *dev);static int e100_close(struct net_device *dev);static int e100_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd);static void e100_tx_timeout(struct net_device *dev);static struct net_device_stats *e100_get_stats(struct net_device *dev);static void set_multicast_list(struct net_device *dev);static void e100_hardware_send_packet(char *buf, int length);static void update_rx_stats(struct net_device_stats *);static void update_tx_stats(struct net_device_stats *);static void e100_check_speed(unsigned long dummy);static void e100_set_speed(unsigned long speed);static void e100_check_duplex(unsigned long dummy);static void e100_set_duplex(enum duplex);static void e100_negotiate(void);static unsigned short e100_get_mdio_reg(unsigned char reg_num);static void e100_send_mdio_cmd(unsigned short cmd, int write_cmd);static void e100_send_mdio_bit(unsigned char bit);static unsigned char e100_receive_mdio_bit(void);static void e100_reset_tranceiver(void);static void e100_clear_network_leds(unsigned long dummy);static void e100_set_network_leds(int active);#define tx_done(dev) (*R_DMA_CH0_CMD == 0)/* * Check for a network adaptor of this type, and return '0' if one exists. * If dev->base_addr == 0, probe all likely locations. * If dev->base_addr == 1, always return failure. * If dev->base_addr == 2, allocate space for the device and return success * (detachable devices only). */static int __initetrax_ethernet_init(struct net_device *dev){	int i;	int anOffset = 0;	printk("ETRAX 100LX 10/100MBit ethernet v2.0 (c) 2000-2001 Axis Communications AB\n");	dev->base_addr = (unsigned int)R_NETWORK_SA_0; /* just to have something to show */	printk("%s initialized\n", dev->name);	/* make Linux aware of the new hardware  */	if (!dev) {		printk(KERN_WARNING "%s: dev == NULL. Should this happen?\n",		       cardname);		dev = init_etherdev(dev, sizeof(struct net_local));		if (!dev)			panic("init_etherdev failed\n");	}	/* setup generic handlers and stuff in the dev struct */	ether_setup(dev);	/* make room for the local structure containing stats etc */	dev->priv = kmalloc(sizeof(struct net_local), GFP_KERNEL);	if (dev->priv == NULL)		return -ENOMEM;	memset(dev->priv, 0, sizeof(struct net_local));	/* now setup our etrax specific stuff */	dev->irq = NETWORK_DMA_RX_IRQ_NBR; /* we really use DMATX as well... */	dev->dma = NETWORK_RX_DMA_NBR;	/* fill in our handlers so the network layer can talk to us in the future */	dev->open               = e100_open;	dev->hard_start_xmit    = e100_send_packet;	dev->stop               = e100_close;	dev->get_stats          = e100_get_stats;	dev->set_multicast_list = set_multicast_list;	dev->set_mac_address    = e100_set_mac_address;	dev->do_ioctl           = e100_ioctl;	dev->tx_timeout         = e100_tx_timeout;	/* set the default MAC address */	e100_set_mac_address(dev, &default_mac);	/* Initialise the list of Etrax DMA-descriptors */	/* Initialise receive descriptors */	for (i = 0; i < (NBR_OF_RX_DESC - 1); i++) {		RxDescList[i].ctrl   = 0;		RxDescList[i].sw_len = RX_DESC_BUF_SIZE;		RxDescList[i].next   = virt_to_phys(&RxDescList[i + 1]);		RxDescList[i].buf    = virt_to_phys(RxBuf + anOffset);		RxDescList[i].status = 0;		RxDescList[i].hw_len = 0;		anOffset += RX_DESC_BUF_SIZE;	}	RxDescList[i].ctrl   = d_eol;	RxDescList[i].sw_len = RX_DESC_BUF_SIZE;	RxDescList[i].next   = virt_to_phys(&RxDescList[0]);	RxDescList[i].buf    = virt_to_phys(RxBuf + anOffset);	RxDescList[i].status = 0;	RxDescList[i].hw_len = 0;	/* Initialise initial pointers */	myNextRxDesc = &RxDescList[0];	myLastRxDesc = &RxDescList[NBR_OF_RX_DESC - 1];	myPrevRxDesc = &RxDescList[NBR_OF_RX_DESC - 1];	/* Initialize speed indicator stuff. */	current_speed = 10;	current_speed_selection = 0; /* Auto */	speed_timer.expires = jiffies + NET_LINK_UP_CHECK_INTERVAL;	speed_timer.function = e100_check_speed;	add_timer(&speed_timer);        	clear_led_timer.function = e100_clear_network_leds;        	full_duplex = 0;	current_duplex = autoneg;	duplex_timer.expires = jiffies + NET_DUPLEX_CHECK_INTERVAL;			duplex_timer.function = e100_check_duplex;	add_timer(&duplex_timer);	return 0;}/* set MAC address of the interface. called from the core after a * SIOCSIFADDR ioctl, and from the bootup above. */static inte100_set_mac_address(struct net_device *dev, void *p){	struct sockaddr *addr = p;	int i;	/* remember it */	memcpy(dev->dev_addr, addr->sa_data, dev->addr_len);	/* Write it to the hardware.	 * Note the way the address is wrapped:	 * *R_NETWORK_SA_0 = a0_0 | (a0_1 << 8) | (a0_2 << 16) | (a0_3 << 24);	 * *R_NETWORK_SA_1 = a0_4 | (a0_5 << 8);	 */		*R_NETWORK_SA_0 = dev->dev_addr[0] | (dev->dev_addr[1] << 8) |		(dev->dev_addr[2] << 16) | (dev->dev_addr[3] << 24);	*R_NETWORK_SA_1 = dev->dev_addr[4] | (dev->dev_addr[5] << 8);	*R_NETWORK_SA_2 = 0;	/* show it in the log as well */	printk("%s: changed MAC to ", dev->name);	for (i = 0; i < 5; i++)		printk("%02X:", dev->dev_addr[i]);	printk("%02X\n", dev->dev_addr[i]);	return 0;}/* * Open/initialize the board. This is called (in the current kernel) * sometime after booting when the 'ifconfig' program is run. * * This routine should set everything up anew at each open, even * registers that "should" only need to be set once at boot, so that * there is non-reboot way to recover if something goes wrong. */static inte100_open(struct net_device *dev){	unsigned long flags;	/* disable the ethernet interface while we configure it */	*R_NETWORK_GEN_CONFIG =		IO_STATE(R_NETWORK_GEN_CONFIG, phy,    mii_clk) |		IO_STATE(R_NETWORK_GEN_CONFIG, enable, off);	/* enable the MDIO output pin */

⌨️ 快捷键说明

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