fec.c

来自「Linux Kernel 2.6.9 for OMAP1710」· C语言 代码 · 共 1,975 行 · 第 1/4 页

C
1,975
字号
fec_enet_open(struct net_device *dev){	struct fec_enet_private *fep = netdev_priv(dev);	/* I should reset the ring buffers here, but I don't yet know	 * a simple way to do that.	 */	fec_set_mac_address(dev);	fep->sequence_done = 0;	fep->link = 0;	if (fep->phy) {		mii_do_cmd(dev, fep->phy->ack_int);		mii_do_cmd(dev, fep->phy->config);		mii_do_cmd(dev, phy_cmd_config);  /* display configuration */		/* FIXME: use netif_carrier_{on,off} ; this polls		 * until link is up which is wrong...  could be		 * 30 seconds or more we are trapped in here. -jgarzik		 */		while(!fep->sequence_done)			schedule();		mii_do_cmd(dev, fep->phy->startup);	} else {		fep->link = 1; /* lets just try it and see */		/* no phy,  go full duplex,  it's most likely a hub chip */		fec_restart(dev, 1);	}	netif_start_queue(dev);	opened = 1;	return 0;		/* Success */}static intfec_enet_close(struct net_device *dev){	/* Don't know what to do yet.	*/	opened = 0;	netif_stop_queue(dev);	fec_stop(dev);	return 0;}static struct net_device_stats *fec_enet_get_stats(struct net_device *dev){	struct fec_enet_private *fep = netdev_priv(dev);	return &fep->stats;}/* Set or clear the multicast filter for this adaptor. * Skeleton taken from sunlance driver. * The CPM Ethernet implementation allows Multicast as well as individual * MAC address filtering.  Some of the drivers check to make sure it is * a group multicast address, and discard those that are not.  I guess I * will do the same for now, but just remove the test if you want * individual filtering as well (do the upper net layers want or support * this kind of feature?). */#define HASH_BITS	6		/* #bits in hash */#define CRC32_POLY	0xEDB88320static void set_multicast_list(struct net_device *dev){	struct fec_enet_private *fep;	volatile fec_t *ep;	struct dev_mc_list *dmi;	unsigned int i, j, bit, data, crc;	unsigned char hash;	fep = netdev_priv(dev);	ep = fec_hwp;	if (dev->flags&IFF_PROMISC) {		/* Log any net taps. */		printk("%s: Promiscuous mode enabled.\n", dev->name);		ep->fec_r_cntrl |= 0x0008;	} else {		ep->fec_r_cntrl &= ~0x0008;		if (dev->flags & IFF_ALLMULTI) {			/* Catch all multicast addresses, so set the			 * filter to all 1's.			 */			ep->fec_hash_table_high = 0xffffffff;			ep->fec_hash_table_low = 0xffffffff;		} else {			/* Clear filter and add the addresses in hash register.			*/			ep->fec_hash_table_high = 0;			ep->fec_hash_table_low = 0;            			dmi = dev->mc_list;			for (j = 0; j < dev->mc_count; j++, dmi = dmi->next)			{				/* Only support group multicast for now.				*/				if (!(dmi->dmi_addr[0] & 1))					continue;							/* calculate crc32 value of mac address				*/				crc = 0xffffffff;				for (i = 0; i < dmi->dmi_addrlen; i++)				{					data = dmi->dmi_addr[i];					for (bit = 0; bit < 8; bit++, data >>= 1)					{						crc = (crc >> 1) ^						(((crc ^ data) & 1) ? CRC32_POLY : 0);					}				}				/* only upper 6 bits (HASH_BITS) are used				   which point to specific bit in he hash registers				*/				hash = (crc >> (32 - HASH_BITS)) & 0x3f;							if (hash > 31)					ep->fec_hash_table_high |= 1 << (hash - 32);				else					ep->fec_hash_table_low |= 1 << hash;			}		}	}}/* Set a MAC change in hardware. */static voidfec_set_mac_address(struct net_device *dev){	int i;	volatile fec_t *fecp;	fecp = fec_hwp;	/* Set our copy of the Ethernet address */	for (i = 0; i < (ETH_ALEN / 2); i++)		my_enet_addr[i] = (dev->dev_addr[i*2] << 8) | dev->dev_addr[i*2 + 1];	/* Set station address. */	fecp->fec_addr_low = (my_enet_addr[0] << 16) | my_enet_addr[1];	fecp->fec_addr_high = my_enet_addr[2] << 16;}/* Initialize the FEC Ethernet on 860T (or ColdFire 5272). */ /*  * XXX:  We need to clean up on failure exits here.  */int __init fec_enet_init(struct net_device *dev){	struct fec_enet_private *fep = netdev_priv(dev);	unsigned long	mem_addr;	volatile cbd_t	*bdp;	cbd_t		*cbd_base;	volatile fec_t	*fecp;	int 		i, j;	/* Only allow us to be probed once. */	if (found)		return(-ENXIO);	/* Create an Ethernet device instance.	*/	fecp = fec_hwp;	/* Whack a reset.  We should wait for this.	*/	fecp->fec_ecntrl = 1;	udelay(10);	/* Clear and enable interrupts */	fecp->fec_ievent = 0xffc0;	fecp->fec_imask = (FEC_ENET_TXF | FEC_ENET_TXB |		FEC_ENET_RXF | FEC_ENET_RXB | FEC_ENET_MII);	fecp->fec_hash_table_high = 0;	fecp->fec_hash_table_low = 0;	fecp->fec_r_buff_size = PKT_MAXBLR_SIZE;        fecp->fec_ecntrl = 2;        fecp->fec_r_des_active = 0x01000000;	/* Set the Ethernet address.  If using multiple Enets on the 8xx,	 * this needs some work to get unique addresses.	 *	 * This is our default MAC address unless the user changes	 * it via eth_mac_addr (our dev->set_mac_addr handler).	 */	fec_get_mac(dev, fep);	/* Allocate memory for buffer descriptors.	*/	if (((RX_RING_SIZE + TX_RING_SIZE) * sizeof(cbd_t)) > PAGE_SIZE) {		printk("FEC init error.  Need more space.\n");		printk("FEC initialization failed.\n");		return 1;	}	mem_addr = __get_free_page(GFP_KERNEL);	cbd_base = (cbd_t *)mem_addr;	/* XXX: missing check for allocation failure */	fec_uncache(mem_addr);	/* Set receive and transmit descriptor base.	*/	fep->rx_bd_base = cbd_base;	fep->tx_bd_base = cbd_base + RX_RING_SIZE;	fep->dirty_tx = fep->cur_tx = fep->tx_bd_base;	fep->cur_rx = fep->rx_bd_base;	fep->skb_cur = fep->skb_dirty = 0;	/* Initialize the receive buffer descriptors.	*/	bdp = fep->rx_bd_base;	for (i=0; i<FEC_ENET_RX_PAGES; i++) {		/* Allocate a page.		*/		mem_addr = __get_free_page(GFP_KERNEL);		/* XXX: missing check for allocation failure */		fec_uncache(mem_addr);		/* Initialize the BD for every fragment in the page.		*/		for (j=0; j<FEC_ENET_RX_FRPPG; j++) {			bdp->cbd_sc = BD_ENET_RX_EMPTY;			bdp->cbd_bufaddr = __pa(mem_addr);			mem_addr += FEC_ENET_RX_FRSIZE;			bdp++;		}	}	/* Set the last buffer to wrap.	*/	bdp--;	bdp->cbd_sc |= BD_SC_WRAP;	/* ...and the same for transmmit.	*/	bdp = fep->tx_bd_base;	for (i=0; i<TX_RING_SIZE; i++) {		/* Initialize the BD for every fragment in the page.		*/		bdp->cbd_sc = 0;		bdp->cbd_bufaddr = 0;		bdp++;	}	/* Set the last buffer to wrap.	*/	bdp--;	bdp->cbd_sc |= BD_SC_WRAP;	/* Set receive and transmit descriptor base.	*/	fecp->fec_r_des_start = __pa((uint)(fep->rx_bd_base));	fecp->fec_x_des_start = __pa((uint)(fep->tx_bd_base));	/* Install our interrupt handlers. This varies depending on	 * the architecture.	*/	fec_request_intrs(dev, fecp);	dev->base_addr = (unsigned long)fecp;	/* The FEC Ethernet specific entries in the device structure. */	dev->open = fec_enet_open;	dev->hard_start_xmit = fec_enet_start_xmit;	dev->tx_timeout = fec_timeout;	dev->watchdog_timeo = TX_TIMEOUT;	dev->stop = fec_enet_close;	dev->get_stats = fec_enet_get_stats;	dev->set_multicast_list = set_multicast_list;	for (i=0; i<NMII-1; i++)		mii_cmds[i].mii_next = &mii_cmds[i+1];	mii_free = mii_cmds;	/* setup MII interface */	fec_set_mii(dev, fep);	printk("%s: FEC ENET Version 0.2, ", dev->name);	for (i=0; i<5; i++)		printk("%02x:", dev->dev_addr[i]);	printk("%02x\n", dev->dev_addr[5]);	/* Queue up command to detect the PHY and initialize the	 * remainder of the interface.	 */	fep->phy_id_done = 0;	fep->phy_addr = 0;	mii_queue(dev, mk_mii_read(MII_REG_PHYIR1), mii_discover_phy);	found++;	return 0;}/* This function is called to start or restart the FEC during a link * change.  This only happens when switching between half and full * duplex. */static voidfec_restart(struct net_device *dev, int duplex){	struct fec_enet_private *fep;	int i;	unsigned char *eap;	volatile cbd_t *bdp;	volatile fec_t *fecp;	fecp = fec_hwp;	fep = netdev_priv(dev);	/* Whack a reset.  We should wait for this.	*/	fecp->fec_ecntrl = 1;	udelay(10);	/* Enable interrupts we wish to service.	*/	fecp->fec_imask = (FEC_ENET_TXF | FEC_ENET_TXB |				FEC_ENET_RXF | FEC_ENET_RXB | FEC_ENET_MII);	/* Clear any outstanding interrupt.	*/	fecp->fec_ievent = 0xffc0;	fec_enable_phy_intr();	/* Set station address.	*/	fecp->fec_addr_low = (my_enet_addr[0] << 16) | my_enet_addr[1];	fecp->fec_addr_high = (my_enet_addr[2] << 16);	eap = (unsigned char *)&my_enet_addr[0];	for (i=0; i<6; i++)		dev->dev_addr[i] = *eap++;	/* Reset all multicast.	*/	fecp->fec_hash_table_high = 0;	fecp->fec_hash_table_low = 0;	/* Set maximum receive buffer size.	*/	fecp->fec_r_buff_size = PKT_MAXBLR_SIZE;	fec_localhw_setup();	/* Set receive and transmit descriptor base.	*/	fecp->fec_r_des_start = __pa((uint)(fep->rx_bd_base));	fecp->fec_x_des_start = __pa((uint)(fep->tx_bd_base));	fep->dirty_tx = fep->cur_tx = fep->tx_bd_base;	fep->cur_rx = fep->rx_bd_base;	/* Reset SKB transmit buffers.	*/	fep->skb_cur = fep->skb_dirty = 0;	for (i=0; i<=TX_RING_MOD_MASK; i++) {		if (fep->tx_skbuff[i] != NULL) {			dev_kfree_skb_any(fep->tx_skbuff[i]);			fep->tx_skbuff[i] = NULL;		}	}	/* Initialize the receive buffer descriptors.	*/	bdp = fep->rx_bd_base;	for (i=0; i<RX_RING_SIZE; i++) {		/* Initialize the BD for every fragment in the page.		*/		bdp->cbd_sc = BD_ENET_RX_EMPTY;		bdp++;	}	/* Set the last buffer to wrap.	*/	bdp--;	bdp->cbd_sc |= BD_SC_WRAP;	/* ...and the same for transmmit.	*/	bdp = fep->tx_bd_base;	for (i=0; i<TX_RING_SIZE; i++) {		/* Initialize the BD for every fragment in the page.		*/		bdp->cbd_sc = 0;		bdp->cbd_bufaddr = 0;		bdp++;	}	/* Set the last buffer to wrap.	*/	bdp--;	bdp->cbd_sc |= BD_SC_WRAP;	/* Enable MII mode.	*/	if (duplex) {		fecp->fec_r_cntrl = 0x04;	/* MII enable */		fecp->fec_x_cntrl = 0x04;	/* FD enable */	}	else {		fecp->fec_r_cntrl = 0x06;	/* MII enable|No Rcv on Xmit */		fecp->fec_x_cntrl = 0x00;	}	fep->full_duplex = duplex;	/* Set MII speed.	*/	fecp->fec_mii_speed = fep->phy_speed;	/* And last, enable the transmit and receive processing.	*/	fecp->fec_ecntrl = 2;	fecp->fec_r_des_active = 0x01000000;}static voidfec_stop(struct net_device *dev){	volatile fec_t *fecp;	struct fec_enet_private *fep;	fecp = fec_hwp;	fep = netdev_priv(dev);	fecp->fec_x_cntrl = 0x01;	/* Graceful transmit stop */	while(!(fecp->fec_ievent & 0x10000000));	/* Whack a reset.  We should wait for this.	*/	fecp->fec_ecntrl = 1;	udelay(10);	/* Clear outstanding MII command interrupts.	*/	fecp->fec_ievent = FEC_ENET_MII;	fec_enable_phy_intr();	fecp->fec_imask = FEC_ENET_MII;	fecp->fec_mii_speed = fep->phy_speed;}static struct net_device *fec_dev;static int __init fec_enet_module_init(void){	struct net_device *dev;	int err;	dev = alloc_etherdev(sizeof(struct fec_enet_private));	if (!dev)		return -ENOMEM;	err = fec_enet_init(dev);	if (err) {		free_netdev(dev);		return err;	}	if (register_netdev(dev) != 0) {		/* XXX: missing cleanup here */		free_netdev(dev);		return -EIO;	}	fec_dev = dev;	return(0);}module_init(fec_enet_module_init);MODULE_LICENSE("GPL");

⌨️ 快捷键说明

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