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

📄 uli526x.c

📁 U-boot源码 ARM7启动代码
💻 C
📖 第 1 页 / 共 2 页
字号:
	struct tx_desc *txptr;	u32 tdes0;	txptr = db->tx_remove_ptr;	while (db->tx_packet_cnt) {		tdes0 = le32_to_cpu(txptr->tdes0);		/* printf(DRV_NAME ": tdes0=%x\n", tdes0); */		if (tdes0 & 0x80000000)			break;		/* A packet sent completed */		db->tx_packet_cnt--;		if (tdes0 != 0x7fffffff) {#ifdef TX_DEBUG			printf("%s()tdes0=%x\n", __FUNCTION__, tdes0);#endif			if (tdes0 & TDES0_ERR_MASK) {				if (tdes0 & 0x0002) {	/* UnderRun */					if (!(db->cr6_data & CR6_SFT)) {						db->cr6_data = db->cr6_data |							CR6_SFT;						update_cr6(db->cr6_data,							db->ioaddr);					}				}			}		}		txptr = txptr->next_tx_desc;	}/* End of while */	/* Update TX remove pointer to next */	db->tx_remove_ptr = txptr;}/* *	Receive the come packet and pass to upper layer */static int uli526x_rx_packet(struct eth_device *dev){	struct uli526x_board_info *db = dev->priv;	struct rx_desc *rxptr;	int rxlen = 0;	u32 rdes0;	rxptr = db->rx_ready_ptr;	rdes0 = le32_to_cpu(rxptr->rdes0);#ifdef RX_DEBUG	printf("%s(): rxptr->rdes0=%x:%x\n", __FUNCTION__, rxptr->rdes0);#endif	if (!(rdes0 & 0x80000000)) {	/* packet owner check */		if ((rdes0 & 0x300) != 0x300) {			/* A packet without First/Last flag */			/* reuse this buf */			printf("A packet without First/Last flag");			uli526x_reuse_buf(rxptr);		} else {			/* A packet with First/Last flag */			rxlen = ((rdes0 >> 16) & 0x3fff) - 4;#ifdef RX_DEBUG			printf("%s(): rxlen =%x\n", __FUNCTION__, rxlen);#endif			/* error summary bit check */			if (rdes0 & 0x8000) {				/* This is a error packet */				printf("Eroor: rdes0: %lx\n", rdes0);			}			if (!(rdes0 & 0x8000) ||				((db->cr6_data & CR6_PM) && (rxlen > 6))) {#ifdef RX_DEBUG				printf("%s(): rx_skb_ptr =%x\n",					__FUNCTION__, rxptr->rx_buf_ptr);				printf("%s(): rxlen =%x\n",					__FUNCTION__, rxlen);				printf("%s(): buf addr =%x\n",					__FUNCTION__, rxptr->rx_buf_ptr);				printf("%s(): rxlen =%x\n",					__FUNCTION__, rxlen);				int i;				for (i = 0; i < 0x20; i++)					printf("%s(): data[%x] =%x\n",					__FUNCTION__, i, rxptr->rx_buf_ptr[i]);#endif				NetReceive(rxptr->rx_buf_ptr, rxlen);				uli526x_reuse_buf(rxptr);			} else {				/* Reuse SKB buffer when the packet is error */				printf("Reuse buffer, rdes0");				uli526x_reuse_buf(rxptr);			}		}		rxptr = rxptr->next_rx_desc;	}	db->rx_ready_ptr = rxptr;	return rxlen;}/* *	Reuse the RX buffer */static void uli526x_reuse_buf(struct rx_desc *rxptr){	if (!(rxptr->rdes0 & cpu_to_le32(0x80000000)))		rxptr->rdes0 = cpu_to_le32(0x80000000);	else		printf("Buffer reuse method error");}/* *	Initialize transmit/Receive descriptor *	Using Chain structure, and allocate Tx/Rx buffer */static void uli526x_descriptor_init(struct uli526x_board_info *db,	unsigned long ioaddr){	struct tx_desc *tmp_tx;	struct rx_desc *tmp_rx;	unsigned char *tmp_buf;	dma_addr_t tmp_tx_dma, tmp_rx_dma;	dma_addr_t tmp_buf_dma;	int i;	/* tx descriptor start pointer */	db->tx_insert_ptr = db->first_tx_desc;	db->tx_remove_ptr = db->first_tx_desc;	outl(db->first_tx_desc_dma, ioaddr + DCR4);     /* TX DESC address */	/* rx descriptor start pointer */	db->first_rx_desc = (void *)db->first_tx_desc +		sizeof(struct tx_desc) * TX_DESC_CNT;	db->first_rx_desc_dma =  db->first_tx_desc_dma +		sizeof(struct tx_desc) * TX_DESC_CNT;	db->rx_ready_ptr = db->first_rx_desc;	outl(db->first_rx_desc_dma, ioaddr + DCR3);	/* RX DESC address */#ifdef DEBUG	printf("%s(): db->first_tx_desc= 0x%x\n",		__FUNCTION__, db->first_tx_desc);	printf("%s(): db->first_rx_desc_dma= 0x%x\n",		__FUNCTION__, db->first_rx_desc_dma);#endif	/* Init Transmit chain */	tmp_buf = db->buf_pool_start;	tmp_buf_dma = db->buf_pool_dma_start;	tmp_tx_dma = db->first_tx_desc_dma;	for (tmp_tx = db->first_tx_desc, i = 0;			i < TX_DESC_CNT; i++, tmp_tx++) {		tmp_tx->tx_buf_ptr = tmp_buf;		tmp_tx->tdes0 = cpu_to_le32(0);		tmp_tx->tdes1 = cpu_to_le32(0x81000000);	/* IC, chain */		tmp_tx->tdes2 = cpu_to_le32(tmp_buf_dma);		tmp_tx_dma += sizeof(struct tx_desc);		tmp_tx->tdes3 = cpu_to_le32(tmp_tx_dma);		tmp_tx->next_tx_desc = tmp_tx + 1;		tmp_buf = tmp_buf + TX_BUF_ALLOC;		tmp_buf_dma = tmp_buf_dma + TX_BUF_ALLOC;	}	(--tmp_tx)->tdes3 = cpu_to_le32(db->first_tx_desc_dma);	tmp_tx->next_tx_desc = db->first_tx_desc;	 /* Init Receive descriptor chain */	tmp_rx_dma = db->first_rx_desc_dma;	for (tmp_rx = db->first_rx_desc, i = 0; i < RX_DESC_CNT;			i++, tmp_rx++) {		tmp_rx->rdes0 = cpu_to_le32(0);		tmp_rx->rdes1 = cpu_to_le32(0x01000600);		tmp_rx_dma += sizeof(struct rx_desc);		tmp_rx->rdes3 = cpu_to_le32(tmp_rx_dma);		tmp_rx->next_rx_desc = tmp_rx + 1;	}	(--tmp_rx)->rdes3 = cpu_to_le32(db->first_rx_desc_dma);	tmp_rx->next_rx_desc = db->first_rx_desc;	/* pre-allocate Rx buffer */	allocate_rx_buffer(db);}/* *	Update CR6 value *	Firstly stop ULI526X, then written value and start */static void update_cr6(u32 cr6_data, unsigned long ioaddr){	outl(cr6_data, ioaddr + DCR6);	udelay(5);}/* *	Allocate rx buffer, */static void allocate_rx_buffer(struct uli526x_board_info *db){	int index;	struct rx_desc *rxptr;	rxptr = db->first_rx_desc;	u32 addr;	for (index = 0; index < RX_DESC_CNT; index++) {		addr = (u32)NetRxPackets[index];		addr += (16 - (addr & 15));		rxptr->rx_buf_ptr = (char *) addr;		rxptr->rdes2 = cpu_to_le32(addr);		rxptr->rdes0 = cpu_to_le32(0x80000000);#ifdef DEBUG		printf("%s(): Number 0x%x:\n", __FUNCTION__, index);		printf("%s(): addr 0x%x:\n", __FUNCTION__, addr);		printf("%s(): rxptr address = 0x%x\n", __FUNCTION__, rxptr);		printf("%s(): rxptr buf address = 0x%x\n", \			__FUNCTION__, rxptr->rx_buf_ptr);		printf("%s(): rdes2  = 0x%x\n", __FUNCTION__, rxptr->rdes2);#endif		rxptr = rxptr->next_rx_desc;	}}/* *	Read one word data from the serial ROM */static u16 read_srom_word(long ioaddr, int offset){	int i;	u16 srom_data = 0;	long cr9_ioaddr = ioaddr + DCR9;	outl(CR9_SROM_READ, cr9_ioaddr);	outl(CR9_SROM_READ | CR9_SRCS, cr9_ioaddr);	/* Send the Read Command 110b */	SROM_CLK_WRITE(SROM_DATA_1, cr9_ioaddr);	SROM_CLK_WRITE(SROM_DATA_1, cr9_ioaddr);	SROM_CLK_WRITE(SROM_DATA_0, cr9_ioaddr);	/* Send the offset */	for (i = 5; i >= 0; i--) {		srom_data = (offset & (1 << i)) ? SROM_DATA_1 : SROM_DATA_0;		SROM_CLK_WRITE(srom_data, cr9_ioaddr);	}	outl(CR9_SROM_READ | CR9_SRCS, cr9_ioaddr);	for (i = 16; i > 0; i--) {		outl(CR9_SROM_READ | CR9_SRCS | CR9_SRCLK, cr9_ioaddr);		udelay(5);		srom_data = (srom_data << 1) | ((inl(cr9_ioaddr) & CR9_CRDOUT)			? 1 : 0);		outl(CR9_SROM_READ | CR9_SRCS, cr9_ioaddr);		udelay(5);	}	outl(CR9_SROM_READ, cr9_ioaddr);	return srom_data;}/* *	Set 10/100 phyxcer capability *	AUTO mode : phyxcer register4 is NIC capability *	Force mode: phyxcer register4 is the force media */static void uli526x_set_phyxcer(struct uli526x_board_info *db){	u16 phy_reg;	/* Phyxcer capability setting */	phy_reg = phy_read(db->ioaddr, db->phy_addr, 4, db->chip_id) & ~0x01e0;	if (db->media_mode & ULI526X_AUTO) {		/* AUTO Mode */		phy_reg |= db->PHY_reg4;	} else {		/* Force Mode */		switch (db->media_mode) {		case ULI526X_10MHF: phy_reg |= 0x20; break;		case ULI526X_10MFD: phy_reg |= 0x40; break;		case ULI526X_100MHF: phy_reg |= 0x80; break;		case ULI526X_100MFD: phy_reg |= 0x100; break;		}	}	/* Write new capability to Phyxcer Reg4 */	if (!(phy_reg & 0x01e0)) {		phy_reg |= db->PHY_reg4;		db->media_mode |= ULI526X_AUTO;	}	phy_write(db->ioaddr, db->phy_addr, 4, phy_reg, db->chip_id);	/* Restart Auto-Negotiation */	phy_write(db->ioaddr, db->phy_addr, 0, 0x1200, db->chip_id);	udelay(50);}/* *	Write a word to Phy register */static void phy_write(unsigned long iobase, u8 phy_addr, u8 offset,	u16 phy_data, u32 chip_id){	u16 i;	unsigned long ioaddr;	if (chip_id == PCI_ULI5263_ID) {		phy_writeby_cr10(iobase, phy_addr, offset, phy_data);		return;	}	/* M5261/M5263 Chip */	ioaddr = iobase + DCR9;	/* Send 33 synchronization clock to Phy controller */	for (i = 0; i < 35; i++)		phy_write_1bit(ioaddr, PHY_DATA_1, chip_id);	/* Send start command(01) to Phy */	phy_write_1bit(ioaddr, PHY_DATA_0, chip_id);	phy_write_1bit(ioaddr, PHY_DATA_1, chip_id);	/* Send write command(01) to Phy */	phy_write_1bit(ioaddr, PHY_DATA_0, chip_id);	phy_write_1bit(ioaddr, PHY_DATA_1, chip_id);	/* Send Phy address */	for (i = 0x10; i > 0; i = i >> 1)		phy_write_1bit(ioaddr, phy_addr & i ?			PHY_DATA_1 : PHY_DATA_0, chip_id);	/* Send register address */	for (i = 0x10; i > 0; i = i >> 1)		phy_write_1bit(ioaddr, offset & i ?			PHY_DATA_1 : PHY_DATA_0, chip_id);	/* written trasnition */	phy_write_1bit(ioaddr, PHY_DATA_1, chip_id);	phy_write_1bit(ioaddr, PHY_DATA_0, chip_id);	/* Write a word data to PHY controller */	for (i = 0x8000; i > 0; i >>= 1)		phy_write_1bit(ioaddr, phy_data & i ?			PHY_DATA_1 : PHY_DATA_0, chip_id);}/* *	Read a word data from phy register */static u16 phy_read(unsigned long iobase, u8 phy_addr, u8 offset, u32 chip_id){	int i;	u16 phy_data;	unsigned long ioaddr;	if (chip_id == PCI_ULI5263_ID)		return phy_readby_cr10(iobase, phy_addr, offset);	/* M5261/M5263 Chip */	ioaddr = iobase + DCR9;	/* Send 33 synchronization clock to Phy controller */	for (i = 0; i < 35; i++)		phy_write_1bit(ioaddr, PHY_DATA_1, chip_id);	/* Send start command(01) to Phy */	phy_write_1bit(ioaddr, PHY_DATA_0, chip_id);	phy_write_1bit(ioaddr, PHY_DATA_1, chip_id);	/* Send read command(10) to Phy */	phy_write_1bit(ioaddr, PHY_DATA_1, chip_id);	phy_write_1bit(ioaddr, PHY_DATA_0, chip_id);	/* Send Phy address */	for (i = 0x10; i > 0; i = i >> 1)		phy_write_1bit(ioaddr, phy_addr & i ?			PHY_DATA_1 : PHY_DATA_0, chip_id);	/* Send register address */	for (i = 0x10; i > 0; i = i >> 1)		phy_write_1bit(ioaddr, offset & i ?			PHY_DATA_1 : PHY_DATA_0, chip_id);	/* Skip transition state */	phy_read_1bit(ioaddr, chip_id);	/* read 16bit data */	for (phy_data = 0, i = 0; i < 16; i++) {		phy_data <<= 1;		phy_data |= phy_read_1bit(ioaddr, chip_id);	}	return phy_data;}static u16 phy_readby_cr10(unsigned long iobase, u8 phy_addr, u8 offset){	unsigned long ioaddr, cr10_value;	ioaddr = iobase + DCR10;	cr10_value = phy_addr;	cr10_value = (cr10_value<<5) + offset;	cr10_value = (cr10_value<<16) + 0x08000000;	outl(cr10_value, ioaddr);	udelay(1);	while (1) {		cr10_value = inl(ioaddr);		if (cr10_value & 0x10000000)			break;	}	return (cr10_value&0x0ffff);}static void phy_writeby_cr10(unsigned long iobase, u8 phy_addr,	u8 offset, u16 phy_data){	unsigned long ioaddr, cr10_value;	ioaddr = iobase + DCR10;	cr10_value = phy_addr;	cr10_value = (cr10_value<<5) + offset;	cr10_value = (cr10_value<<16) + 0x04000000 + phy_data;	outl(cr10_value, ioaddr);	udelay(1);}/* *	Write one bit data to Phy Controller */static void phy_write_1bit(unsigned long ioaddr, u32 phy_data, u32 chip_id){	outl(phy_data , ioaddr);			/* MII Clock Low */	udelay(1);	outl(phy_data  | MDCLKH, ioaddr);	/* MII Clock High */	udelay(1);	outl(phy_data , ioaddr);			/* MII Clock Low */	udelay(1);}/* *	Read one bit phy data from PHY controller */static u16 phy_read_1bit(unsigned long ioaddr, u32 chip_id){	u16 phy_data;	outl(0x50000 , ioaddr);	udelay(1);	phy_data = (inl(ioaddr) >> 19) & 0x1;	outl(0x40000 , ioaddr);	udelay(1);	return phy_data;}/* * Set MAC address to ID Table */static void set_mac_addr(struct eth_device *dev){	int i;	u16 addr;	struct uli526x_board_info *db = dev->priv;	outl(0x10000, db->ioaddr + DCR0);	/* Diagnosis mode */	/* Reset dianostic pointer port */	outl(0x1c0, db->ioaddr + DCR13);	outl(0, db->ioaddr + DCR14);	/* Clear reset port */	outl(0x10, db->ioaddr + DCR14);	/* Reset ID Table pointer */	outl(0, db->ioaddr + DCR14);	/* Clear reset port */	outl(0, db->ioaddr + DCR13);	/* Clear CR13 */	/* Select ID Table access port */	outl(0x1b0, db->ioaddr + DCR13);	/* Read MAC address from CR14 */	for (i = 0; i < 3; i++) {		addr = dev->enetaddr[2 * i] | (dev->enetaddr[2 * i + 1] << 8);		outl(addr, db->ioaddr + DCR14);	}	/* write end */	outl(0, db->ioaddr + DCR13);	/* Clear CR13 */	outl(0, db->ioaddr + DCR0);	/* Clear CR0 */	udelay(10);	return;}#endif

⌨️ 快捷键说明

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