ipg.c

来自「linux 内核源代码」· C语言 代码 · 共 2,341 行 · 第 1/5 页

C
2,341
字号
/* * ipg.c: Device Driver for the IP1000 Gigabit Ethernet Adapter * * Copyright (C) 2003, 2007  IC Plus Corp * * Original Author: * *   Craig Rich *   Sundance Technology, Inc. *   www.sundanceti.com *   craig_rich@sundanceti.com * * Current Maintainer: * *   Sorbica Shieh. *   http://www.icplus.com.tw *   sorbica@icplus.com.tw * *   Jesse Huang *   http://www.icplus.com.tw *   jesse@icplus.com.tw */#include <linux/crc32.h>#include <linux/ethtool.h>#include <linux/mii.h>#include <linux/mutex.h>#include <asm/div64.h>#define IPG_RX_RING_BYTES	(sizeof(struct ipg_rx) * IPG_RFDLIST_LENGTH)#define IPG_TX_RING_BYTES	(sizeof(struct ipg_tx) * IPG_TFDLIST_LENGTH)#define IPG_RESET_MASK \	(IPG_AC_GLOBAL_RESET | IPG_AC_RX_RESET | IPG_AC_TX_RESET | \	 IPG_AC_DMA | IPG_AC_FIFO | IPG_AC_NETWORK | IPG_AC_HOST | \	 IPG_AC_AUTO_INIT)#define ipg_w32(val32,reg)	iowrite32((val32), ioaddr + (reg))#define ipg_w16(val16,reg)	iowrite16((val16), ioaddr + (reg))#define ipg_w8(val8,reg)	iowrite8((val8), ioaddr + (reg))#define ipg_r32(reg)		ioread32(ioaddr + (reg))#define ipg_r16(reg)		ioread16(ioaddr + (reg))#define ipg_r8(reg)		ioread8(ioaddr + (reg))#define JUMBO_FRAME_4k_ONLYenum {	netdev_io_size = 128};#include "ipg.h"#define DRV_NAME	"ipg"MODULE_AUTHOR("IC Plus Corp. 2003");MODULE_DESCRIPTION("IC Plus IP1000 Gigabit Ethernet Adapter Linux Driver "		   DrvVer);MODULE_LICENSE("GPL");//variable record -- index by leading revision/length//Revision/Length(=N*4), Address1, Data1, Address2, Data2,...,AddressN,DataNstatic unsigned short DefaultPhyParam[] = {	// 11/12/03 IP1000A v1-3 rev=0x40	/*--------------------------------------------------------------------------	(0x4000|(15*4)), 31, 0x0001, 27, 0x01e0, 31, 0x0002, 22, 0x85bd, 24, 0xfff2,				 27, 0x0c10, 28, 0x0c10, 29, 0x2c10, 31, 0x0003, 23, 0x92f6,				 31, 0x0000, 23, 0x003d, 30, 0x00de, 20, 0x20e7,  9, 0x0700,	  --------------------------------------------------------------------------*/	// 12/17/03 IP1000A v1-4 rev=0x40	(0x4000 | (07 * 4)), 31, 0x0001, 27, 0x01e0, 31, 0x0002, 27, 0xeb8e, 31,	    0x0000,	30, 0x005e, 9, 0x0700,	// 01/09/04 IP1000A v1-5 rev=0x41	(0x4100 | (07 * 4)), 31, 0x0001, 27, 0x01e0, 31, 0x0002, 27, 0xeb8e, 31,	    0x0000,	30, 0x005e, 9, 0x0700,	0x0000};static const char *ipg_brand_name[] = {	"IC PLUS IP1000 1000/100/10 based NIC",	"Sundance Technology ST2021 based NIC",	"Tamarack Microelectronics TC9020/9021 based NIC",	"Tamarack Microelectronics TC9020/9021 based NIC",	"D-Link NIC",	"D-Link NIC IP1000A"};static struct pci_device_id ipg_pci_tbl[] __devinitdata = {	{ PCI_VDEVICE(SUNDANCE,	0x1023), 0 },	{ PCI_VDEVICE(SUNDANCE,	0x2021), 1 },	{ PCI_VDEVICE(SUNDANCE,	0x1021), 2 },	{ PCI_VDEVICE(DLINK,	0x9021), 3 },	{ PCI_VDEVICE(DLINK,	0x4000), 4 },	{ PCI_VDEVICE(DLINK,	0x4020), 5 },	{ 0, }};MODULE_DEVICE_TABLE(pci, ipg_pci_tbl);static inline void __iomem *ipg_ioaddr(struct net_device *dev){	struct ipg_nic_private *sp = netdev_priv(dev);	return sp->ioaddr;}#ifdef IPG_DEBUGstatic void ipg_dump_rfdlist(struct net_device *dev){	struct ipg_nic_private *sp = netdev_priv(dev);	void __iomem *ioaddr = sp->ioaddr;	unsigned int i;	u32 offset;	IPG_DEBUG_MSG("_dump_rfdlist\n");	printk(KERN_INFO "rx_current = %2.2x\n", sp->rx_current);	printk(KERN_INFO "rx_dirty   = %2.2x\n", sp->rx_dirty);	printk(KERN_INFO "RFDList start address = %16.16lx\n",	       (unsigned long) sp->rxd_map);	printk(KERN_INFO "RFDListPtr register   = %8.8x%8.8x\n",	       ipg_r32(IPG_RFDLISTPTR1), ipg_r32(IPG_RFDLISTPTR0));	for (i = 0; i < IPG_RFDLIST_LENGTH; i++) {		offset = (u32) &sp->rxd[i].next_desc - (u32) sp->rxd;		printk(KERN_INFO "%2.2x %4.4x RFDNextPtr = %16.16lx\n", i,		       offset, (unsigned long) sp->rxd[i].next_desc);		offset = (u32) &sp->rxd[i].rfs - (u32) sp->rxd;		printk(KERN_INFO "%2.2x %4.4x RFS        = %16.16lx\n", i,		       offset, (unsigned long) sp->rxd[i].rfs);		offset = (u32) &sp->rxd[i].frag_info - (u32) sp->rxd;		printk(KERN_INFO "%2.2x %4.4x frag_info   = %16.16lx\n", i,		       offset, (unsigned long) sp->rxd[i].frag_info);	}}static void ipg_dump_tfdlist(struct net_device *dev){	struct ipg_nic_private *sp = netdev_priv(dev);	void __iomem *ioaddr = sp->ioaddr;	unsigned int i;	u32 offset;	IPG_DEBUG_MSG("_dump_tfdlist\n");	printk(KERN_INFO "tx_current         = %2.2x\n", sp->tx_current);	printk(KERN_INFO "tx_dirty = %2.2x\n", sp->tx_dirty);	printk(KERN_INFO "TFDList start address = %16.16lx\n",	       (unsigned long) sp->txd_map);	printk(KERN_INFO "TFDListPtr register   = %8.8x%8.8x\n",	       ipg_r32(IPG_TFDLISTPTR1), ipg_r32(IPG_TFDLISTPTR0));	for (i = 0; i < IPG_TFDLIST_LENGTH; i++) {		offset = (u32) &sp->txd[i].next_desc - (u32) sp->txd;		printk(KERN_INFO "%2.2x %4.4x TFDNextPtr = %16.16lx\n", i,		       offset, (unsigned long) sp->txd[i].next_desc);		offset = (u32) &sp->txd[i].tfc - (u32) sp->txd;		printk(KERN_INFO "%2.2x %4.4x TFC        = %16.16lx\n", i,		       offset, (unsigned long) sp->txd[i].tfc);		offset = (u32) &sp->txd[i].frag_info - (u32) sp->txd;		printk(KERN_INFO "%2.2x %4.4x frag_info   = %16.16lx\n", i,		       offset, (unsigned long) sp->txd[i].frag_info);	}}#endifstatic void ipg_write_phy_ctl(void __iomem *ioaddr, u8 data){	ipg_w8(IPG_PC_RSVD_MASK & data, PHY_CTRL);	ndelay(IPG_PC_PHYCTRLWAIT_NS);}static void ipg_drive_phy_ctl_low_high(void __iomem *ioaddr, u8 data){	ipg_write_phy_ctl(ioaddr, IPG_PC_MGMTCLK_LO | data);	ipg_write_phy_ctl(ioaddr, IPG_PC_MGMTCLK_HI | data);}static void send_three_state(void __iomem *ioaddr, u8 phyctrlpolarity){	phyctrlpolarity |= (IPG_PC_MGMTDATA & 0) | IPG_PC_MGMTDIR;	ipg_drive_phy_ctl_low_high(ioaddr, phyctrlpolarity);}static void send_end(void __iomem *ioaddr, u8 phyctrlpolarity){	ipg_w8((IPG_PC_MGMTCLK_LO | (IPG_PC_MGMTDATA & 0) | IPG_PC_MGMTDIR |		phyctrlpolarity) & IPG_PC_RSVD_MASK, PHY_CTRL);}static u16 read_phy_bit(void __iomem * ioaddr, u8 phyctrlpolarity){	u16 bit_data;	ipg_write_phy_ctl(ioaddr, IPG_PC_MGMTCLK_LO | phyctrlpolarity);	bit_data = ((ipg_r8(PHY_CTRL) & IPG_PC_MGMTDATA) >> 1) & 1;	ipg_write_phy_ctl(ioaddr, IPG_PC_MGMTCLK_HI | phyctrlpolarity);	return bit_data;}/* * Read a register from the Physical Layer device located * on the IPG NIC, using the IPG PHYCTRL register. */static int mdio_read(struct net_device * dev, int phy_id, int phy_reg){	void __iomem *ioaddr = ipg_ioaddr(dev);	/*	 * The GMII mangement frame structure for a read is as follows:	 *	 * |Preamble|st|op|phyad|regad|ta|      data      |idle|	 * |< 32 1s>|01|10|AAAAA|RRRRR|z0|DDDDDDDDDDDDDDDD|z   |	 *	 * <32 1s> = 32 consecutive logic 1 values	 * A = bit of Physical Layer device address (MSB first)	 * R = bit of register address (MSB first)	 * z = High impedance state	 * D = bit of read data (MSB first)	 *	 * Transmission order is 'Preamble' field first, bits transmitted	 * left to right (first to last).	 */	struct {		u32 field;		unsigned int len;	} p[] = {		{ GMII_PREAMBLE,	32 },	/* Preamble */		{ GMII_ST,		2  },	/* ST */		{ GMII_READ,		2  },	/* OP */		{ phy_id,		5  },	/* PHYAD */		{ phy_reg,		5  },	/* REGAD */		{ 0x0000,		2  },	/* TA */		{ 0x0000,		16 },	/* DATA */		{ 0x0000,		1  }	/* IDLE */	};	unsigned int i, j;	u8 polarity, data;	polarity  = ipg_r8(PHY_CTRL);	polarity &= (IPG_PC_DUPLEX_POLARITY | IPG_PC_LINK_POLARITY);	/* Create the Preamble, ST, OP, PHYAD, and REGAD field. */	for (j = 0; j < 5; j++) {		for (i = 0; i < p[j].len; i++) {			/* For each variable length field, the MSB must be			 * transmitted first. Rotate through the field bits,			 * starting with the MSB, and move each bit into the			 * the 1st (2^1) bit position (this is the bit position			 * corresponding to the MgmtData bit of the PhyCtrl			 * register for the IPG).			 *			 * Example: ST = 01;			 *			 *          First write a '0' to bit 1 of the PhyCtrl			 *          register, then write a '1' to bit 1 of the			 *          PhyCtrl register.			 *			 * To do this, right shift the MSB of ST by the value:			 * [field length - 1 - #ST bits already written]			 * then left shift this result by 1.			 */			data  = (p[j].field >> (p[j].len - 1 - i)) << 1;			data &= IPG_PC_MGMTDATA;			data |= polarity | IPG_PC_MGMTDIR;			ipg_drive_phy_ctl_low_high(ioaddr, data);		}	}	send_three_state(ioaddr, polarity);	read_phy_bit(ioaddr, polarity);	/*	 * For a read cycle, the bits for the next two fields (TA and	 * DATA) are driven by the PHY (the IPG reads these bits).	 */	for (i = 0; i < p[6].len; i++) {		p[6].field |=		    (read_phy_bit(ioaddr, polarity) << (p[6].len - 1 - i));	}	send_three_state(ioaddr, polarity);	send_three_state(ioaddr, polarity);	send_three_state(ioaddr, polarity);	send_end(ioaddr, polarity);	/* Return the value of the DATA field. */	return p[6].field;}/* * Write to a register from the Physical Layer device located * on the IPG NIC, using the IPG PHYCTRL register. */static void mdio_write(struct net_device *dev, int phy_id, int phy_reg, int val){	void __iomem *ioaddr = ipg_ioaddr(dev);	/*	 * The GMII mangement frame structure for a read is as follows:	 *	 * |Preamble|st|op|phyad|regad|ta|      data      |idle|	 * |< 32 1s>|01|10|AAAAA|RRRRR|z0|DDDDDDDDDDDDDDDD|z   |	 *	 * <32 1s> = 32 consecutive logic 1 values	 * A = bit of Physical Layer device address (MSB first)	 * R = bit of register address (MSB first)	 * z = High impedance state	 * D = bit of write data (MSB first)	 *	 * Transmission order is 'Preamble' field first, bits transmitted	 * left to right (first to last).	 */	struct {		u32 field;		unsigned int len;	} p[] = {		{ GMII_PREAMBLE,	32 },	/* Preamble */		{ GMII_ST,		2  },	/* ST */		{ GMII_WRITE,		2  },	/* OP */		{ phy_id,		5  },	/* PHYAD */		{ phy_reg,		5  },	/* REGAD */		{ 0x0002,		2  },	/* TA */		{ val & 0xffff,		16 },	/* DATA */		{ 0x0000,		1  }	/* IDLE */	};	unsigned int i, j;	u8 polarity, data;	polarity  = ipg_r8(PHY_CTRL);	polarity &= (IPG_PC_DUPLEX_POLARITY | IPG_PC_LINK_POLARITY);	/* Create the Preamble, ST, OP, PHYAD, and REGAD field. */	for (j = 0; j < 7; j++) {		for (i = 0; i < p[j].len; i++) {			/* For each variable length field, the MSB must be			 * transmitted first. Rotate through the field bits,			 * starting with the MSB, and move each bit into the			 * the 1st (2^1) bit position (this is the bit position			 * corresponding to the MgmtData bit of the PhyCtrl			 * register for the IPG).			 *			 * Example: ST = 01;			 *			 *          First write a '0' to bit 1 of the PhyCtrl			 *          register, then write a '1' to bit 1 of the			 *          PhyCtrl register.			 *			 * To do this, right shift the MSB of ST by the value:			 * [field length - 1 - #ST bits already written]			 * then left shift this result by 1.			 */			data  = (p[j].field >> (p[j].len - 1 - i)) << 1;			data &= IPG_PC_MGMTDATA;			data |= polarity | IPG_PC_MGMTDIR;			ipg_drive_phy_ctl_low_high(ioaddr, data);		}	}	/* The last cycle is a tri-state, so read from the PHY. */	for (j = 7; j < 8; j++) {		for (i = 0; i < p[j].len; i++) {			ipg_write_phy_ctl(ioaddr, IPG_PC_MGMTCLK_LO | polarity);			p[j].field |= ((ipg_r8(PHY_CTRL) &				IPG_PC_MGMTDATA) >> 1) << (p[j].len - 1 - i);			ipg_write_phy_ctl(ioaddr, IPG_PC_MGMTCLK_HI | polarity);		}	}}/* Set LED_Mode JES20040127EEPROM */static void ipg_set_led_mode(struct net_device *dev){	struct ipg_nic_private *sp = netdev_priv(dev);	void __iomem *ioaddr = sp->ioaddr;	u32 mode;	mode = ipg_r32(ASIC_CTRL);	mode &= ~(IPG_AC_LED_MODE_BIT_1 | IPG_AC_LED_MODE | IPG_AC_LED_SPEED);	if ((sp->LED_Mode & 0x03) > 1)		mode |= IPG_AC_LED_MODE_BIT_1;	/* Write Asic Control Bit 29 */	if ((sp->LED_Mode & 0x01) == 1)		mode |= IPG_AC_LED_MODE;	/* Write Asic Control Bit 14 */	if ((sp->LED_Mode & 0x08) == 8)		mode |= IPG_AC_LED_SPEED;	/* Write Asic Control Bit 27 */	ipg_w32(mode, ASIC_CTRL);}/* Set PHYSet JES20040127EEPROM */static void ipg_set_phy_set(struct net_device *dev){	struct ipg_nic_private *sp = netdev_priv(dev);	void __iomem *ioaddr = sp->ioaddr;	int physet;	physet = ipg_r8(PHY_SET);	physet &= ~(IPG_PS_MEM_LENB9B | IPG_PS_MEM_LEN9 | IPG_PS_NON_COMPDET);	physet |= ((sp->LED_Mode & 0x70) >> 4);	ipg_w8(physet, PHY_SET);}static int ipg_reset(struct net_device *dev, u32 resetflags){	/* Assert functional resets via the IPG AsicCtrl	 * register as specified by the 'resetflags' input	 * parameter.	 */	void __iomem *ioaddr = ipg_ioaddr(dev);	//JES20040127EEPROM:	unsigned int timeout_count = 0;	IPG_DEBUG_MSG("_reset\n");	ipg_w32(ipg_r32(ASIC_CTRL) | resetflags, ASIC_CTRL);	/* Delay added to account for problem with 10Mbps reset. */	mdelay(IPG_AC_RESETWAIT);	while (IPG_AC_RESET_BUSY & ipg_r32(ASIC_CTRL)) {		mdelay(IPG_AC_RESETWAIT);		if (++timeout_count > IPG_AC_RESET_TIMEOUT)			return -ETIME;	}	/* Set LED Mode in Asic Control JES20040127EEPROM */	ipg_set_led_mode(dev);	/* Set PHYSet Register Value JES20040127EEPROM */	ipg_set_phy_set(dev);	return 0;}/* Find the GMII PHY address. */static int ipg_find_phyaddr(struct net_device *dev){	unsigned int phyaddr, i;	for (i = 0; i < 32; i++) {		u32 status;		/* Search for the correct PHY address among 32 possible. */		phyaddr = (IPG_NIC_PHY_ADDRESS + i) % 32;		/* 10/22/03 Grace change verify from GMII_PHY_STATUS to		   GMII_PHY_ID1		 */		status = mdio_read(dev, phyaddr, MII_BMSR);		if ((status != 0xFFFF) && (status != 0))			return phyaddr;	}	return 0x1f;}/* * Configure IPG based on result of IEEE 802.3 PHY * auto-negotiation. */static int ipg_config_autoneg(struct net_device *dev)

⌨️ 快捷键说明

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