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

📄 ethif_cs8900_jz4740.c

📁 君正早期ucos系统(只有早期的才不没有打包成库),MPLAYER,文件系统,图片解码,浏览,电子书,录音,想学ucos,识货的人就下吧 russblock fmradio explore set
💻 C
📖 第 1 页 / 共 2 页
字号:
/* Jz ethernet support * *  Copyright (c) 2007 *  Ingenic Semiconductor, <jgao@ingenic.cn> * * 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., 59 Temple Place, Suite 330, Boston, * MA 02111-1307 USA *//* ********************************************************************* */#include "jz4740.h"#include "jz_cs8900.h"#include "ethif_cs8900_jz4740.h"#include "lwip/debug.h"#include "lwip/opt.h"#include "lwip/def.h"#include "lwip/mem.h"#include "lwip/pbuf.h"#include "lwip/stats.h"#include "lwip/sys.h"#include "netif/etharp.h"/* ********************************************************************* *//* File local definitions *//* Define those to better describe your network interface. */#define Print_Header#define IFNAME0 'e'#define IFNAME1 'n'/* ********************************************************************* */#define PKTSIZE_ALIGN 2048#define PKTALIGN      32#define PKTBUFSRX     16//#define MIN_PACKET_SIZE 76#define MIN_PACKET_SIZE 60static int next_rx;static int next_tx;volatile u8_t  PktBuf[(PKTBUFSRX+1) * PKTSIZE_ALIGN + PKTALIGN] __attribute__ ((aligned (PKTSIZE_ALIGN)));volatile u8_t *NetTxPacket = 0; volatile u8_t *NetRxPackets[PKTBUFSRX];#define ETH_GPIO 59/* packet page register access functions */#ifdef CS8900_BUS32/* we don't need 16 bit initialisation on 32 bit bus */#define get_reg_init_bus(x) get_reg((x))#elsestatic unsigned short get_reg_init_bus (int regno){	/* force 16 bit busmode */	volatile unsigned char c;    //c = CS8900_BUS16_0;	//c = CS8900_BUS16_1;	//c = CS8900_BUS16_0;	//c = CS8900_BUS16_1;	//c = CS8900_BUS16_0;	CS8900_PPTR = regno;//	printf("CS8900_PPTR:%08x\n",&CS8900_PPTR);	udelay(100);		return (unsigned short) CS8900_PDATA;}#endif//MAC #if 1#define ETHADDR0 0xff #define ETHADDR1 0xff #define ETHADDR2 0xff #define ETHADDR3 0xff #define ETHADDR4 0x20 #define ETHADDR5 0x10#endif #if 0#define ETHADDR0 0x68 #define ETHADDR1 0x68 #define ETHADDR2 0x68 #define ETHADDR3 0x68 #define ETHADDR4 0x68 #define ETHADDR5 0x68 #endif /////////////***************get timer******************/////////////////////////////#define TIMER_CHAN  0#define TIMER_FDATA 0xffff  /* Timer full data value */#define TIMER_HZ    (CFG_HZ/4)static ulong timestamp;static ulong lastdec;#define READ_TIMER  REG_TCU_TCNT(TIMER_CHAN)  /* macro to read the 16 bit timer */ulong get_timer(ulong base){	return get_timer_masked () - base;}ulong get_timer_masked (void){	ulong now = READ_TIMER;	if (lastdec <= now) {		/* normal mode */		timestamp += (now - lastdec);	} else {		/* we have an overflow ... */		timestamp += TIMER_FDATA + now - lastdec;	}	lastdec = now;	return timestamp;}static unsigned short get_reg (int regno){	CS8900_PPTR = regno;	return (unsigned short) CS8900_PDATA;}static void put_reg (int regno, unsigned short val){	CS8900_PPTR = regno;	CS8900_PDATA = val;}static void eth_reset (void){	int tmo,i;	unsigned short us;	/* reset NIC */	put_reg (PP_SelfCTL, get_reg (PP_SelfCTL) | PP_SelfCTL_Reset);	/* wait for 200ms */	for (i=0; i<200; i++) {		udelay (1000);	}	/* Wait until the chip is reset */	tmo = get_timer (0) + 1 * CFG_HZ;	while ((((us = get_reg_init_bus (PP_SelfSTAT)) & PP_SelfSTAT_InitD) == 0)		   && tmo < get_timer (0))		/*NOP*/;        if(tmo < get_timer(0))		printf("reset overtime!!\r\n");}static void eth_reginit (void){	int i;	put_reg (PP_LineCTL, (0x0013U | 0x0080U/*SerTxOn*/ | 0x0040U/*SerRxOn*/));  	put_reg (PP_RxCTL,(0x0005U | 0x0800U | 0x0400U | 0x0100U)); 	put_reg (PP_RxCFG,(0x0003U | 0x0100U));        // enable receive interrupt 	put_reg (PP_TxCFG,(0x0007U | 0x0000U));                    // disable transmit interrupt (is default) 	put_reg (PP_IntReg,0x0000U);                            // use interrupt number 0     	put_reg (PP_BufCFG,0x000bU);     	put_reg (PP_BusCTL,(0x0017U | 0x8000U));// enable interrupt generation 	/** 	 * @note You MUST wait 30 ms before accessing the CS8900 	 */         for (i = 0; i < 300; i++)	{		udelay(1000);  	}}static void cs8900_get_enetaddr (unsigned char * addr){	int i;	unsigned char env_enetaddr[6];	char *tmp = getenv ("ethaddr");	char *end;	for (i=0; i<6; i++) {//		env_enetaddr[i] = tmp ? simple_strtoul(tmp, &end, 16) : 0;		if (tmp)			tmp = (*end) ? end+1 : end;	}	/* verify chip id */	if (get_reg_init_bus (PP_ChipID) != 0x630e)		return;	eth_reset ();	if ((get_reg (PP_SelfST) & (PP_SelfSTAT_EEPROM | PP_SelfSTAT_EEPROM_OK)) ==			(PP_SelfSTAT_EEPROM | PP_SelfSTAT_EEPROM_OK)) {		/* Load the MAC from EEPROM */		for (i = 0; i < 6 / 2; i++) {			unsigned int Addr;			Addr = get_reg (PP_IA + i * 2);			addr[i * 2] = Addr & 0xFF;			addr[i * 2 + 1] = Addr >> 8;		}		if (memcmp(env_enetaddr, "\0\0\0\0\0\0", 6) != 0 &&		    memcmp(env_enetaddr, addr, 6) != 0) {			printf ("\nWarning: MAC addresses don't match:\n");			printf ("\tHW MAC address:  "				"%02X:%02X:%02X:%02X:%02X:%02X\n",				addr[0], addr[1],				addr[2], addr[3],				addr[4], addr[5] );			printf ("\t\"ethaddr\" value: "				"%02X:%02X:%02X:%02X:%02X:%02X\n",				env_enetaddr[0], env_enetaddr[1],				env_enetaddr[2], env_enetaddr[3],				env_enetaddr[4], env_enetaddr[5]) ;//				debug ("### Set MAC addr from environment\n");			memcpy (addr, env_enetaddr, 6);		}		if (!tmp) {			char ethaddr[20];			sprintf (ethaddr, "%02X:%02X:%02X:%02X:%02X:%02X",				 addr[0], addr[1],				 addr[2], addr[3],				 addr[4], addr[5]) ;//			debug ("### Set environment from HW MAC addr = \"%s\"\n",				ethaddr);//			setenv ("ethaddr", ethaddr);		}	}}static void jz_eth_halt (struct eth_device *dev){	/* disable transmitter/receiver mode */	put_reg (PP_LineCTL, 0);	/* "shutdown" to show ChipID or kernel wouldn't find he cs8900 ... */	get_reg_init_bus (PP_ChipID);}static int low_level_init(struct netif *netif){	u16 id,status;	int i;#define bi_enetaddr netif->hwaddr	/* verify chip id */	id = get_reg_init_bus (PP_ChipID);	if (id != 0x630e) {		printf ("CS8900 jz_eth_init error!\n");		return 0;	}	eth_reset ();	/* set the ethernet address */	put_reg (PP_IA + 0, bi_enetaddr[0] | (bi_enetaddr[1] << 8));	put_reg (PP_IA + 2, bi_enetaddr[2] | (bi_enetaddr[3] << 8));	put_reg (PP_IA + 4, bi_enetaddr[4] | (bi_enetaddr[5] << 8));	eth_reginit();	return 0;}/* Get a data block via Ethernet */static struct pbuf *ETH_Input(struct netif *netif){	struct pbuf *p = NULL, *q = NULL;	u16 *ptr;        int i;	unsigned short rxlen;	unsigned short *addr;	unsigned short status;	printf("input\n");	for(i=0;i<10;i++)	{		udelay(1000);	}	status = CS8900_RTDATA;  /* stat */	rxlen = CS8900_RTDATA;   /* len */	if (!(status & RxOK)) {		printf("error input\n");	}	if (rxlen > 0)	{		p = pbuf_alloc(PBUF_RAW, rxlen, PBUF_POOL);  //length - 4		{			if (p != 0)			{				for (q = p; q != 0; q = q->next) 				{ 					ptr = q->payload; 					// TODO: CHECK: what if q->len is odd? we don't use the last byte? 					for (i = 0; i < (q->len + 1)/ 2; i++) 					{ 						*ptr = CS8900_RTDATA; 						ptr++; 					} 				} 						}			else			{				put_reg(PP_RxCFG,(0x0003U|RxOKiE|0x0040U));				rxlen = 0;			}		}	}	return p;}/* Send a data block via Ethernet. */static err_t ETH_output(struct netif *netif, struct pbuf *p){	struct pbuf *q;	u16 *ptr =(u16 *)p->payload; 	u16_t length = p->tot_len; 	unsigned long sent_bytes; 	u16 status;

⌨️ 快捷键说明

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