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

📄 ne2000.c

📁 arm920t内核linux操作系统下U-boot引导程序中
💻 C
📖 第 1 页 / 共 2 页
字号:
/*Ported to U-Boot  by Christian Pellegrin <chri@ascensit.com>Based on sources from the Linux kernel (pcnet_cs.c, 8390.h) andeCOS(if_dp83902a.c, if_dp83902a.h). Both of these 2 wonderful worldare GPL, so this is, of course, GPL.==========================================================================dev/if_dp83902a.cEthernet device driver for NS DP83902a ethernet controller==========================================================================####ECOSGPLCOPYRIGHTBEGIN####-------------------------------------------This file is part of eCos, the Embedded Configurable Operating System.Copyright (C) 1998, 1999, 2000, 2001, 2002 Red Hat, Inc.eCos is free software; you can redistribute it and/or modify it underthe terms of the GNU General Public License as published by the FreeSoftware Foundation; either version 2 or (at your option) any later version.eCos is distributed in the hope that it will be useful, but WITHOUT ANYWARRANTY; without even the implied warranty of MERCHANTABILITY orFITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public Licensefor more details.You should have received a copy of the GNU General Public License alongwith eCos; if not, write to the Free Software Foundation, Inc.,59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.As a special exception, if other files instantiate templates or use macrosor inline functions from this file, or you compile this file and link itwith other works to produce a work based on this file, this file does notby itself cause the resulting work to be covered by the GNU General PublicLicense. However the source code for this file must still be made availablein accordance with section (3) of the GNU General Public License.This exception does not invalidate any other reasons why a work based onthis file might be covered by the GNU General Public License.Alternative licenses for eCos may be arranged by contacting Red Hat, Inc.at http://sources.redhat.com/ecos/ecos-license/-------------------------------------------####ECOSGPLCOPYRIGHTEND########BSDCOPYRIGHTBEGIN####-------------------------------------------Portions of this software may have been derived from OpenBSD or other sources,and are covered by the appropriate copyright disclaimers included herein.-------------------------------------------####BSDCOPYRIGHTEND####==========================================================================#####DESCRIPTIONBEGIN####Author(s):    gthomasContributors: gthomas, jskov, rsandifoDate:	      2001-06-13Purpose:Description:FIXME:	      Will fail if pinged with large packets (1520 bytes)Add promisc configAdd SNMP####DESCRIPTIONEND####==========================================================================*/#include <common.h>#include <command.h>#include <net.h>#include <malloc.h>#ifdef CONFIG_DRIVER_NE2000/* wor around udelay resetting OCR */static void my_udelay(long us) {	long tmo;	tmo = get_timer (0) + us * CFG_HZ / 1000000; /* will this be much greater than 0 ? */	while (get_timer (0) < tmo);}#define mdelay(n)       my_udelay((n)*1000)/* forward definition of function used for the uboot interface */void uboot_push_packet_len(int len);void uboot_push_tx_done(int key, int val);/* timeout for tx/rx in s */#define TOUT 5//#define Tekkaman 1#define ETHER_ADDR_LEN 6/*  ------------------------------------------------------------------------  Debugging details  Set to perms of:  0 disables all debug output  1 for process debug output  2 for added data IO output: get_reg, put_reg  4 for packet allocation/free output  8 for only startup status, so we can tell we're installed OK*//*#define DEBUG 0xf*/#define DEBUG 0#if DEBUG & 1#define DEBUG_FUNCTION() do { printf("%s\n", __FUNCTION__); } while (0)#define DEBUG_LINE() do { printf("%d\n", __LINE__); } while (0)#else#define DEBUG_FUNCTION() do {} while(0)#define DEBUG_LINE() do {} while(0)#endif#include "ne2000.h"#include "8390.h"#if DEBUG & 1#define PRINTK(args...) printf(args)#else#define PRINTK(args...)#endifstatic dp83902a_priv_data_t nic; /* just one instance of the card supported */static intne2000_read_mac_addr(unsigned char * enaddr){//	dp83902a_priv_data_t *dp = &nic;	int ii;	char *s, *e;	s = getenv ("ethaddr");	if (s == NULL){		return -1;	}	else{		for(ii = 0; ii < 12; ii+=2) {			enaddr[ii] =enaddr[ii+1]= s ? simple_strtoul (s, &e, 16) : 0;			if (s){				s = (*e) ? e + 1 : e;			}		}	}	return 0;}static booldp83902a_init(void){	dp83902a_priv_data_t *dp = &nic;	cyg_uint8* base;	int i;	DEBUG_FUNCTION();	base = dp->base;	if (!base) return false;  /* No device found */	DEBUG_LINE();	/* Prepare ESA */	DP_OUT(base, DP_CR, DP_CR_NODMA | DP_CR_PAGE1);  /* Select page 1 */	/* Use the address from the serial EEPROM */	for (i = 0; i < 6; i++)		DP_IN(base, DP_P1_PAR0+i, dp->esa[i]);	DP_OUT(base, DP_CR, DP_CR_NODMA | DP_CR_PAGE0);  /* Select page 0 */	printf("AX88796 - %s ESA: %02x:%02x:%02x:%02x:%02x:%02x\n",	       "tekkmana",	       dp->esa[0],	       dp->esa[1],	       dp->esa[2],	       dp->esa[3],	       dp->esa[4],	       dp->esa[5] );	return true;}static voiddp83902a_stop(void){	dp83902a_priv_data_t *dp = &nic;	cyg_uint8 *base = dp->base;	DEBUG_FUNCTION();	DP_OUT(base, DP_CR, DP_CR_PAGE0 | DP_CR_NODMA | DP_CR_STOP);  /* Brutal */	DP_OUT(base, DP_ISR, 0xFF);		/* Clear any pending interrupts */	DP_OUT(base, DP_IMR, 0x00);		/* Disable all interrupts */	dp->running = false;}/*  This function is called to "start up" the interface.  It may be called  multiple times, even when the hardware is already running.  It will be  called whenever something "hardware oriented" changes and should leave  the hardware ready to send/receive packets.*/static voiddp83902a_start(unsigned char * enaddr){	dp83902a_priv_data_t *dp = &nic;	cyg_uint8 *base = dp->base;	int i;	DEBUG_FUNCTION();	DP_OUT(base, DP_CR, DP_CR_PAGE0 | DP_CR_NODMA | DP_CR_STOP); /* Brutal */	DP_OUT(base, DP_DCR, 0x49);	DP_OUT(base, DP_RBCH, 0);		/* Remote byte count */	DP_OUT(base, DP_RBCL, 0);	DP_OUT(base, DP_RCR, DP_RCR_MON);	/* Accept no packets */	DP_OUT(base, DP_TCR, DP_TCR_LOCAL);	/* Transmitter [virtually] off */	DP_OUT(base, DP_TPSR, dp->tx_buf1);	/* Transmitter start page */	dp->tx1 = dp->tx2 = 0;	dp->tx_next = dp->tx_buf1;	dp->tx_started = false;	DP_OUT(base, DP_PSTART, dp->rx_buf_start); /* Receive ring start page */	DP_OUT(base, DP_BNDRY, dp->rx_buf_end-1); /* Receive ring boundary */	DP_OUT(base, DP_PSTOP, dp->rx_buf_end);	/* Receive ring end page */	dp->rx_next = dp->rx_buf_start-1;	DP_OUT(base, DP_ISR, 0xFF);		/* Clear any pending interrupts */	DP_OUT(base, DP_IMR, DP_IMR_All);	/* Enable all interrupts */	DP_OUT(base, DP_CR, DP_CR_NODMA | DP_CR_PAGE1 | DP_CR_STOP);  /* Select page 1 */	DP_OUT(base, DP_P1_CURP, dp->rx_buf_start);   /* Current page - next free page for Rx */	for (i = 0;  i < ETHER_ADDR_LEN;  i++) {		DP_OUT(base, DP_P1_PAR0+i, enaddr[i]);	}	#ifdef  Tekkaman			printf("PAGE1\n");		for (i = 0; i <= 0x0f; i++)		printf("%02X==%02X\n",i,n2k_inb((unsigned char)(i)));	#endif	/* Enable and start device */	DP_OUT(base, DP_CR, DP_CR_PAGE0 | DP_CR_NODMA | DP_CR_STOP);	DP_OUT(base, DP_TCR, DP_TCR_NORMAL); /* Normal transmit operations */	DP_OUT(base, DP_RCR, DP_RCR_AB);  /* Accept broadcast, no errors, no multicast *///	dp->running = true;		#ifdef  Tekkaman			printf("PAGE0\n");		for (i = 0; i < 0x0f; i++)		printf("%02X==%02X\n",i,n2k_inb(i));	#endif	DP_OUT(base, DP_CR, DP_CR_PAGE0 | DP_CR_NODMA | DP_CR_START);	dp->running = true;}/*  This routine is called to start the transmitter.  It is split out from the  data handling routine so it may be called either when data becomes first  available or when an Tx interrupt occurs*/static voiddp83902a_start_xmit(int start_page, int len){	dp83902a_priv_data_t *dp = (dp83902a_priv_data_t *) &nic;	cyg_uint8 *base = dp->base;	DEBUG_FUNCTION();#ifdef Tekkaman	printf("Tx pkt %d len %d\n", start_page, len);	if (dp->tx_started)		printf("TX already started?!?\n");#endif	DP_OUT(base, DP_ISR, (DP_ISR_TxP | DP_ISR_TxE));	DP_OUT(base, DP_CR, DP_CR_PAGE0 | DP_CR_NODMA | DP_CR_START);	DP_OUT(base, DP_TBCL, len & 0xFF);	DP_OUT(base, DP_TBCH, len >> 8);	DP_OUT(base, DP_TPSR, start_page);	DP_OUT(base, DP_CR, DP_CR_NODMA | DP_CR_TXPKT | DP_CR_START);	dp->tx_started = true;}/*  This routine is called to send data to the hardware.  It is known a-priori  that there is free buffer space (dp->tx_next).*/static voiddp83902a_send(unsigned short *data, int total_len, unsigned long key){	struct dp83902a_priv_data *dp = (struct dp83902a_priv_data *) &nic;	cyg_uint8 *base = dp->base;	int len, start_page, pkt_len, i, isr;#ifdef Tekkaman	int dx=0;#endif	DEBUG_FUNCTION();	len = pkt_len = total_len;	if (pkt_len < IEEE_8023_MIN_FRAME) pkt_len = IEEE_8023_MIN_FRAME;	start_page = dp->tx_next;	if (dp->tx_next == dp->tx_buf1) {		dp->tx1 = start_page;		dp->tx1_len = pkt_len;		dp->tx1_key = key;		dp->tx_next = dp->tx_buf2;	} else {		dp->tx2 = start_page;		dp->tx2_len = pkt_len;		dp->tx2_key = key;		dp->tx_next = dp->tx_buf1;	}#if DEBUG & 5	printf("TX prep page %d len %d\n", start_page, pkt_len);#endif	DP_OUT(base, DP_ISR, DP_ISR_RDC);  /* Clear end of DMA */	{		/* Dummy read. The manual sez something slightly different, */		/* but the code is extended a bit to do what Hitachi's monitor */		/* does (i.e., also read data). *//*		cyg_uint16 tmp;		int len = 1;		DP_OUT(base, DP_RSAL, 0x100-len);		DP_OUT(base, DP_RSAH, (start_page-1) & 0xff);		DP_OUT(base, DP_RBCL, len);		DP_OUT(base, DP_RBCH, 0);		DP_OUT(base, DP_CR, DP_CR_PAGE0 | DP_CR_RDMA | DP_CR_START);		DP_IN_DATA(dp->data, tmp);*/	}#ifdef CYGHWR_NS_DP83902A_PLF_BROKEN_TX_DMA	/* Stall for a bit before continuing to work around random data */	/* corruption problems on some platforms. */	CYGACC_CALL_IF_DELAY_US(1);#endif	/* Send data to device buffer(s) */	DP_OUT(base, DP_RSAL, 0);	DP_OUT(base, DP_RSAH, start_page);	DP_OUT(base, DP_RBCL, pkt_len & 0xFF);	DP_OUT(base, DP_RBCH, pkt_len >> 8);	DP_OUT(base, DP_CR, DP_CR_WDMA | DP_CR_START);	/* Put data into buffer */#if DEBUG & 4	printf(" sg buf %08lx len %08x\n ", (unsigned long) data, len);	dx = 0;#endif	while (len > 1) {#ifdef Tekkaman		printf(" %04x", *data);		if (0 == (++dx % 16)) printf("\n ");#endif		DP_OUT_DATA(dp->data, *data++);		len-=2;	}#ifdef Tekkaman		if (len==1) 	printf(" %04x", (*data)&0xff);	printf("\n");#endif		if (len==1) 	{DP_OUT_DATA(dp->data, (*data++)&0xff);	total_len++;	}	if (total_len < pkt_len) {#ifdef Tekkaman		printf("  + %d bytes of padding\n", pkt_len - total_len);#endif		/* Padding to 802.3 length was required */		for (i = total_len;  i < pkt_len;i+=2) {						DP_OUT_DATA(dp->data, 0);		}	}#ifdef CYGHWR_NS_DP83902A_PLF_BROKEN_TX_DMA	/* After last data write, delay for a bit before accessing the */	/* device again, or we may get random data corruption in the last */	/* datum (on some platforms). */	CYGACC_CALL_IF_DELAY_US(1);#endif	/* Wait for DMA to complete */	do {		DP_IN(base, DP_ISR, isr);	} while ((isr & DP_ISR_RDC) == 0);	/* Then disable DMA */	DP_OUT(base, DP_CR, DP_CR_PAGE0 | DP_CR_NODMA | DP_CR_START);	/* Start transmit if not already going */	if (!dp->tx_started) {		if (start_page == dp->tx1) {			dp->tx_int = 1;  /* Expecting interrupt from BUF1 */		} else {			dp->tx_int = 2;  /* Expecting interrupt from BUF2 */		}		dp83902a_start_xmit(start_page, pkt_len);	}}/*  This function is called when a packet has been received.  It's job is  to prepare to unload the packet from the hardware.  Once the length of  the packet is known, the upper layer of the driver can be told.  When  the upper layer is ready to unload the packet, the internal function  'dp83902a_recv' will be called to actually fetch it from the hardware.*/static voiddp83902a_RxEvent(void){	struct dp83902a_priv_data *dp = (struct dp83902a_priv_data *) &nic;	cyg_uint8 *base = dp->base;	unsigned char rsr;	unsigned short rcv_hdr[2];	int i, len, pkt, cur;	DEBUG_FUNCTION();	DP_IN(base, DP_RSR, rsr);	while (true) {		/* Read incoming packet header */		DP_OUT(base, DP_CR, DP_CR_PAGE1 | DP_CR_NODMA | DP_CR_START);		DP_IN(base, DP_P1_CURP, cur);		DP_OUT(base, DP_P1_CR, DP_CR_PAGE0 | DP_CR_NODMA | DP_CR_START);		DP_IN(base, DP_BNDRY, pkt);		pkt += 1;		if (pkt == dp->rx_buf_end)			pkt = dp->rx_buf_start;		if (pkt == cur) {			break;		}		DP_OUT(base, DP_RBCL, 4);		DP_OUT(base, DP_RBCH, 0);		DP_OUT(base, DP_RSAL, 0);		DP_OUT(base, DP_RSAH, pkt);		if (dp->rx_next == pkt) {			if (cur == dp->rx_buf_start)				DP_OUT(base, DP_BNDRY, dp->rx_buf_end-1);			else				DP_OUT(base, DP_BNDRY, cur-1); /* Update pointer */			return;		}		dp->rx_next = pkt;		DP_OUT(base, DP_ISR, DP_ISR_RDC); /* Clear end of DMA */		DP_OUT(base, DP_CR, DP_CR_RDMA | DP_CR_START);#ifdef CYGHWR_NS_DP83902A_PLF_BROKEN_RX_DMA		CYGACC_CALL_IF_DELAY_US(10);#endif		for (i = 0;  i < sizeof(rcv_hdr);) {			DP_IN_DATA(dp->data, rcv_hdr[i++]);		}#ifdef Tekkaman		printf("rx hdr %04x %04x \n",		       rcv_hdr[0], rcv_hdr[1]);#endif		len = rcv_hdr[1] - 4;		uboot_push_packet_len(len);		if (((rcv_hdr[0] >>8)&0xff) == dp->rx_buf_start)			DP_OUT(base, DP_BNDRY, dp->rx_buf_end-1);		else			DP_OUT(base, DP_BNDRY, ((rcv_hdr[0] >>8)&0xff)-1); /* Update pointer */	}}/*  This function is called as a result of the "eth_drv_recv()" call above.  It's job is to actually fetch data for a packet from the hardware once  memory buffers have been allocated for the packet.  Note that the buffers  may come in pieces, using a scatter-gather list.  This allows for more  efficient processing in the upper layers of the stack.*/static voiddp83902a_recv(unsigned short *data, int len){	struct dp83902a_priv_data *dp = (struct dp83902a_priv_data *) &nic;	cyg_uint8 *base = dp->base;	int i, mlen;	cyg_uint8 saved_char = 0;	bool saved;#ifdef Tekkaman	int dx=0;#endif	DEBUG_FUNCTION();#if DEBUG & 5	printf("Rx packet %d length %d\n", dp->rx_next, len);#endif	/* Read incoming packet data */	DP_OUT(base, DP_CR, DP_CR_PAGE0 | DP_CR_NODMA | DP_CR_START);	DP_OUT(base, DP_RBCL, len & 0xFF);	DP_OUT(base, DP_RBCH, (len >> 8)& 0xFF);	DP_OUT(base, DP_RSAL, 4);		/* Past header */	DP_OUT(base, DP_RSAH, dp->rx_next);	DP_OUT(base, DP_ISR, DP_ISR_RDC); /* Clear end of DMA */	DP_OUT(base, DP_CR, DP_CR_RDMA | DP_CR_START);#ifdef CYGHWR_NS_DP83902A_PLF_BROKEN_RX_DMA	CYGACC_CALL_IF_DELAY_US(10);#endif

⌨️ 快捷键说明

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