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

📄 rtl8169.c

📁 F:worksip2440a board可启动u-boot-like.tar.gz F:worksip2440a board可启动u-boot-like.tar.gz
💻 C
📖 第 1 页 / 共 2 页
字号:
/* * rtl8169.c : U-Boot driver for the RealTek RTL8169 * * Masami Komiya (mkomiya@sonare.it) * * Most part is taken from r8169.c of etherboot * *//***************************************************************************    r8169.c: Etherboot device driver for the RealTek RTL-8169 Gigabit*    Written 2003 by Timothy Legge <tlegge@rogers.com>**    This program is free software; you can redistribute it and/or modify*    it under the terms of the GNU General Public License as published by*    the Free Software Foundation; either version 2 of the License, or*    (at your option) any later version.**    This program is distributed in the hope that it will be useful,*    but WITHOUT ANY WARRANTY; without even the implied warranty of*    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the*    GNU General Public License for more details.**    You should have received a copy of the GNU General Public License*    along with this program; if not, write to the Free Software*    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.**    Portions of this code based on:*	r8169.c: A RealTek RTL-8169 Gigabit Ethernet driver*		for Linux kernel 2.4.x.**    Written 2002 ShuChen <shuchen@realtek.com.tw>*	  See Linux Driver for full information**    Linux Driver Version 1.27a, 10.02.2002**    Thanks to:*	Jean Chen of RealTek Semiconductor Corp. for*	providing the evaluation NIC used to develop*	this driver.  RealTek's support for Etherboot*	is appreciated.**    REVISION HISTORY:*    ================**    v1.0	11-26-2003	timlegge	Initial port of Linux driver*    v1.5	01-17-2004	timlegge	Initial driver output cleanup**    Indent Options: indent -kr -i8***************************************************************************/#include <common.h>#include <malloc.h>#include <net.h>#include <asm/io.h>#include <pci.h>#if (CONFIG_COMMANDS & CFG_CMD_NET) && defined(CONFIG_NET_MULTI) && \	defined(CONFIG_RTL8169)#undef DEBUG_RTL8169#undef DEBUG_RTL8169_TX#undef DEBUG_RTL8169_RX#define drv_version "v1.5"#define drv_date "01-17-2004"static u32 ioaddr;/* Condensed operations for readability. */#define virt_to_le32desc(addr)	cpu_to_le32(virt_to_bus(addr))#define le32desc_to_virt(addr)	bus_to_virt(le32_to_cpu(addr))#define currticks()	get_timer(0)#define bus_to_phys(a)	pci_mem_to_phys((pci_dev_t)dev->priv, a)#define phys_to_bus(a)	pci_phys_to_mem((pci_dev_t)dev->priv, a)/* media options */#define MAX_UNITS 8static int media[MAX_UNITS] = { -1, -1, -1, -1, -1, -1, -1, -1 };/* MAC address length*/#define MAC_ADDR_LEN	6/* max supported gigabit ethernet frame size -- must be at least (dev->mtu+14+4).*/#define MAX_ETH_FRAME_SIZE	1536#define TX_FIFO_THRESH 256	/* In bytes */#define RX_FIFO_THRESH	7	/* 7 means NO threshold, Rx buffer level before first PCI xfer.	 */#define RX_DMA_BURST	6	/* Maximum PCI burst, '6' is 1024 */#define TX_DMA_BURST	6	/* Maximum PCI burst, '6' is 1024 */#define EarlyTxThld	0x3F	/* 0x3F means NO early transmit */#define RxPacketMaxSize 0x0800	/* Maximum size supported is 16K-1 */#define InterFrameGap	0x03	/* 3 means InterFrameGap = the shortest one */#define NUM_TX_DESC	1	/* Number of Tx descriptor registers */#define NUM_RX_DESC	4	/* Number of Rx descriptor registers */#define RX_BUF_SIZE	1536	/* Rx Buffer size */#define RX_BUF_LEN	8192#define RTL_MIN_IO_SIZE 0x80#define TX_TIMEOUT  (6*HZ)/* write/read MMIO register */#define RTL_W8(reg, val8)	writeb ((val8), ioaddr + (reg))#define RTL_W16(reg, val16)	writew ((val16), ioaddr + (reg))#define RTL_W32(reg, val32)	writel ((val32), ioaddr + (reg))#define RTL_R8(reg)		readb (ioaddr + (reg))#define RTL_R16(reg)		readw (ioaddr + (reg))#define RTL_R32(reg)		((unsigned long) readl (ioaddr + (reg)))#define ETH_FRAME_LEN	MAX_ETH_FRAME_SIZE#define ETH_ALEN	MAC_ADDR_LEN#define ETH_ZLEN	60enum RTL8169_registers {	MAC0 = 0,		/* Ethernet hardware address. */	MAR0 = 8,		/* Multicast filter. */	TxDescStartAddr = 0x20,	TxHDescStartAddr = 0x28,	FLASH = 0x30,	ERSR = 0x36,	ChipCmd = 0x37,	TxPoll = 0x38,	IntrMask = 0x3C,	IntrStatus = 0x3E,	TxConfig = 0x40,	RxConfig = 0x44,	RxMissed = 0x4C,	Cfg9346 = 0x50,	Config0 = 0x51,	Config1 = 0x52,	Config2 = 0x53,	Config3 = 0x54,	Config4 = 0x55,	Config5 = 0x56,	MultiIntr = 0x5C,	PHYAR = 0x60,	TBICSR = 0x64,	TBI_ANAR = 0x68,	TBI_LPAR = 0x6A,	PHYstatus = 0x6C,	RxMaxSize = 0xDA,	CPlusCmd = 0xE0,	RxDescStartAddr = 0xE4,	EarlyTxThres = 0xEC,	FuncEvent = 0xF0,	FuncEventMask = 0xF4,	FuncPresetState = 0xF8,	FuncForceEvent = 0xFC,};enum RTL8169_register_content {	/*InterruptStatusBits */	SYSErr = 0x8000,	PCSTimeout = 0x4000,	SWInt = 0x0100,	TxDescUnavail = 0x80,	RxFIFOOver = 0x40,	RxUnderrun = 0x20,	RxOverflow = 0x10,	TxErr = 0x08,	TxOK = 0x04,	RxErr = 0x02,	RxOK = 0x01,	/*RxStatusDesc */	RxRES = 0x00200000,	RxCRC = 0x00080000,	RxRUNT = 0x00100000,	RxRWT = 0x00400000,	/*ChipCmdBits */	CmdReset = 0x10,	CmdRxEnb = 0x08,	CmdTxEnb = 0x04,	RxBufEmpty = 0x01,	/*Cfg9346Bits */	Cfg9346_Lock = 0x00,	Cfg9346_Unlock = 0xC0,	/*rx_mode_bits */	AcceptErr = 0x20,	AcceptRunt = 0x10,	AcceptBroadcast = 0x08,	AcceptMulticast = 0x04,	AcceptMyPhys = 0x02,	AcceptAllPhys = 0x01,	/*RxConfigBits */	RxCfgFIFOShift = 13,	RxCfgDMAShift = 8,	/*TxConfigBits */	TxInterFrameGapShift = 24,	TxDMAShift = 8,		/* DMA burst value (0-7) is shift this many bits */	/*rtl8169_PHYstatus */	TBI_Enable = 0x80,	TxFlowCtrl = 0x40,	RxFlowCtrl = 0x20,	_1000bpsF = 0x10,	_100bps = 0x08,	_10bps = 0x04,	LinkStatus = 0x02,	FullDup = 0x01,	/*GIGABIT_PHY_registers */	PHY_CTRL_REG = 0,	PHY_STAT_REG = 1,	PHY_AUTO_NEGO_REG = 4,	PHY_1000_CTRL_REG = 9,	/*GIGABIT_PHY_REG_BIT */	PHY_Restart_Auto_Nego = 0x0200,	PHY_Enable_Auto_Nego = 0x1000,	/* PHY_STAT_REG = 1; */	PHY_Auto_Neco_Comp = 0x0020,	/* PHY_AUTO_NEGO_REG = 4; */	PHY_Cap_10_Half = 0x0020,	PHY_Cap_10_Full = 0x0040,	PHY_Cap_100_Half = 0x0080,	PHY_Cap_100_Full = 0x0100,	/* PHY_1000_CTRL_REG = 9; */	PHY_Cap_1000_Full = 0x0200,	PHY_Cap_Null = 0x0,	/*_MediaType*/	_10_Half = 0x01,	_10_Full = 0x02,	_100_Half = 0x04,	_100_Full = 0x08,	_1000_Full = 0x10,	/*_TBICSRBit*/	TBILinkOK = 0x02000000,};static struct {	const char *name;	u8 version;		/* depend on RTL8169 docs */	u32 RxConfigMask;	/* should clear the bits supported by this chip */} rtl_chip_info[] = {	{"RTL-8169", 0x00, 0xff7e1880,},	{"RTL-8169", 0x04, 0xff7e1880,},};enum _DescStatusBit {	OWNbit = 0x80000000,	EORbit = 0x40000000,	FSbit = 0x20000000,	LSbit = 0x10000000,};struct TxDesc {	u32 status;	u32 vlan_tag;	u32 buf_addr;	u32 buf_Haddr;};struct RxDesc {	u32 status;	u32 vlan_tag;	u32 buf_addr;	u32 buf_Haddr;};/* Define the TX Descriptor */static u8 tx_ring[NUM_TX_DESC * sizeof(struct TxDesc) + 256];/*	__attribute__ ((aligned(256))); *//* Create a static buffer of size RX_BUF_SZ for eachTX Descriptor.	All descriptors point to apart of this buffer */static unsigned char txb[NUM_TX_DESC * RX_BUF_SIZE];/* Define the RX Descriptor */static u8 rx_ring[NUM_RX_DESC * sizeof(struct TxDesc) + 256];  /*  __attribute__ ((aligned(256))); *//* Create a static buffer of size RX_BUF_SZ for eachRX Descriptor	All descriptors point to apart of this buffer */static unsigned char rxb[NUM_RX_DESC * RX_BUF_SIZE];struct rtl8169_private {	void *mmio_addr;	/* memory map physical address */	int chipset;	unsigned long cur_rx;	/* Index into the Rx descriptor buffer of next Rx pkt. */	unsigned long cur_tx;	/* Index into the Tx descriptor buffer of next Rx pkt. */	unsigned long dirty_tx;	unsigned char *TxDescArrays;	/* Index of Tx Descriptor buffer */	unsigned char *RxDescArrays;	/* Index of Rx Descriptor buffer */	struct TxDesc *TxDescArray;	/* Index of 256-alignment Tx Descriptor buffer */	struct RxDesc *RxDescArray;	/* Index of 256-alignment Rx Descriptor buffer */	unsigned char *RxBufferRings;	/* Index of Rx Buffer  */	unsigned char *RxBufferRing[NUM_RX_DESC];	/* Index of Rx Buffer array */	unsigned char *Tx_skbuff[NUM_TX_DESC];} tpx;static struct rtl8169_private *tpc;static const u16 rtl8169_intr_mask =    SYSErr | PCSTimeout | RxUnderrun | RxOverflow | RxFIFOOver | TxErr |    TxOK | RxErr | RxOK;static const unsigned int rtl8169_rx_config =    (RX_FIFO_THRESH << RxCfgFIFOShift) | (RX_DMA_BURST << RxCfgDMAShift);static struct pci_device_id supported[] = {	{PCI_VENDOR_ID_REALTEK, 0x8169},	{}};void mdio_write(int RegAddr, int value){	int i;	RTL_W32(PHYAR, 0x80000000 | (RegAddr & 0xFF) << 16 | value);	udelay(1000);	for (i = 2000; i > 0; i--) {		/* Check if the RTL8169 has completed writing to the specified MII register */		if (!(RTL_R32(PHYAR) & 0x80000000)) {			break;		} else {			udelay(100);		}	}}int mdio_read(int RegAddr){	int i, value = -1;	RTL_W32(PHYAR, 0x0 | (RegAddr & 0xFF) << 16);	udelay(1000);	for (i = 2000; i > 0; i--) {		/* Check if the RTL8169 has completed retrieving data from the specified MII register */		if (RTL_R32(PHYAR) & 0x80000000) {			value = (int) (RTL_R32(PHYAR) & 0xFFFF);			break;		} else {			udelay(100);		}	}	return value;}#define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))static int rtl8169_init_board(struct eth_device *dev){	int i;	u32 tmp;#ifdef DEBUG_RTL8169	printf ("%s\n", __FUNCTION__);#endif	ioaddr = dev->iobase;	/* Soft reset the chip. */	RTL_W8(ChipCmd, CmdReset);	/* Check that the chip has finished the reset. */	for (i = 1000; i > 0; i--)		if ((RTL_R8(ChipCmd) & CmdReset) == 0)			break;		else			udelay(10);	/* identify chip attached to board */	tmp = RTL_R32(TxConfig);	tmp = ((tmp & 0x7c000000) + ((tmp & 0x00800000) << 2)) >> 24;	for (i = ARRAY_SIZE(rtl_chip_info) - 1; i >= 0; i--){		if (tmp == rtl_chip_info[i].version) {			tpc->chipset = i;			goto match;		}	}	/* if unknown chip, assume array element #0, original RTL-8169 in this case */	printf("PCI device %s: unknown chip version, assuming RTL-8169\n", dev->name);	printf("PCI device: TxConfig = 0x%hX\n", (unsigned long) RTL_R32(TxConfig));	tpc->chipset = 0;match:	return 0;}/**************************************************************************RECV - Receive a frame***************************************************************************/static int rtl_recv(struct eth_device *dev){	/* return true if there's an ethernet packet ready to read */	/* nic->packet should contain data on return */	/* nic->packetlen should contain length of data */	int cur_rx;	int length = 0;#ifdef DEBUG_RTL8169_RX	printf ("%s\n", __FUNCTION__);#endif	ioaddr = dev->iobase;	cur_rx = tpc->cur_rx;	if ((tpc->RxDescArray[cur_rx].status & OWNbit) == 0) {		if (!(tpc->RxDescArray[cur_rx].status & RxRES)) {			unsigned char rxdata[RX_BUF_LEN];			length = (int) (tpc->RxDescArray[cur_rx].						status & 0x00001FFF) - 4;			memcpy(rxdata, tpc->RxBufferRing[cur_rx], length);			NetReceive(rxdata, length);			if (cur_rx == NUM_RX_DESC - 1)				tpc->RxDescArray[cur_rx].status =				    (OWNbit | EORbit) + RX_BUF_SIZE;			else				tpc->RxDescArray[cur_rx].status =				    OWNbit + RX_BUF_SIZE;			tpc->RxDescArray[cur_rx].buf_addr =			    virt_to_bus(tpc->RxBufferRing[cur_rx]);		} else {			puts("Error Rx");		}		cur_rx = (cur_rx + 1) % NUM_RX_DESC;		tpc->cur_rx = cur_rx;		return 1;	}	tpc->cur_rx = cur_rx;	return (0);		/* initially as this is called to flush the input */}#define HZ 1000

⌨️ 快捷键说明

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