?? smc91113.c
字號(hào):
/*------------------------------------------------------------------------ . smc91113.c . This is a driver for SMSC's 91C113 single-chip Ethernet device. . "Features" of the SMC chip: . Integrated PHY/MAC for 10/100BaseT Operation . Supports internal and external MII . Integrated 8K packet memory . EEPROM interface for configuration . . Arguments: . io = for the base address . irq = for the IRQ . . Hardware multicast code from Peter Cammaert ( pc@denkart.be ) . . Sources: . o SMSC LAN91C111 databook (www.smsc.com) . o smc9194.c by Erik Stahlman . o skeleton.c by Donald Becker ( becker@cesdis.gsfc.nasa.gov ) . ----------------------------------------------------------------------------*///#include "armboot.h"//#include "command.h"#include "..\inc\smc91113.h"#include "..\inc\ethernet.h"#include "..\inc\mac.h"//#include "net.h"//#ifdef CONFIG_DRIVER_SMC91113 //xu// Use power-down feature of the chip#define POWER_DOWN 0#define NO_AUTOPROBEstatic const char version[] = " EV44B0II ethernet driver v1.0 2003/01/08 by Qinwei, MICETEK Shanghai\n";#define SMC_DEBUG 0/*------------------------------------------------------------------------ . . Configuration options, for the experienced user to change. . -------------------------------------------------------------------------*//* . Wait time for memory to be free. This probably shouldn't be . tuned that much, as waiting for this means nothing else happens . in the system*/#define MEMORY_WAIT_TIME 16#if (SMC_DEBUG > 2 )//#define PRINTK3(args...) printf(args)#else//#define PRINTK3(args...)#endif#if SMC_DEBUG > 1//#define PRINTK2(args...) printf(args)#else//#define PRINTK2(args...)#endif#ifdef SMC_DEBUG//#define PRINTK(args...) printf(args)#else//#define PRINTK(args...)#endif/*------------------------------------------------------------------------ . . The internal workings of the driver. If you are changing anything . here with the SMC stuff, you should have the datasheet and know . what you are doing. . -------------------------------------------------------------------------*/#define CARDNAME "LAN91C113"// Memory sizing constant#define LAN91C113_MEMORY_MULTIPLIER (1024*2)#ifndef CONFIG_SMC91113_BASE#define CONFIG_SMC91113_BASE 0x2000300#endif#define SMC_BASE_ADDRESS CONFIG_SMC91113_BASE #define SMC_DEV_NAME "SMC91113"#define SMC_PHY_ADDR 0x0000#define SMC_ALLOC_MAX_TRY 5//#define SMC_TX_TIMEOUT 30 //del by xu#define SMC_TX_TIMEOUT 50 // 原來(lái)是5;#define SMC_PHY_CLOCK_DELAY 1000#define ETH_ZLEN 60#ifdef CONFIG_SMC_USE_32_BIT#define USE_32_BIT #else#undef USE_32_BIT #endif#define MAX_LENGTH 1024/*----------------------------------------------------------------- . . The driver can be entered at any of the following entry points. . .------------------------------------------------------------------ */extern int eth_init(void);extern void eth_halt(void);extern int eth_rx(void);int smc_send_packet(volatile void *packet, int packet_length);//extern int eth_send(volatile void *packet, int length); //xu/* . This is called by register_netdev(). It is responsible for . checking the portlist for the SMC9000 series chipset. If it finds . one, then it will initialize the device, find the hardware information, . and sets up the appropriate device parameters. . NOTE: Interrupts are *OFF* when this procedure is called. . . NB:This shouldn't be static since it is referred to externally.*/int smc_init(void);/* . This is called by unregister_netdev(). It is responsible for . cleaning up before the driver is finally unregistered and discarded.*/void smc_destructor(void);/* . The kernel calls this function when someone wants to use the device, . typically 'ifconfig ethX up'.*/static int smc_open(void);/* . This is called by the kernel in response to 'ifconfig ethX down'. It . is responsible for cleaning up everything that the open routine . does, and maybe putting the card into a powerdown state.*/static int smc_close(void);/* . Configures the PHY through the MII Management interface*/static void smc_phy_configure(void);/* . This is a separate procedure to handle the receipt of a packet, to . leave the interrupt code looking slightly cleaner*/int smc_rcv(void);/* ------------------------------------------------------------ . . Internal routines . ------------------------------------------------------------*///static char smc_mac_addr[] = {0x02, 0x80, 0xad, 0x20, 0x31, 0xb8}; /* * This function must be called before smc_open() if you want to override * the default mac address. */void smc_set_mac_addr(const char *addr) { int i; for (i=0; i < sizeof(smc_mac_addr); i++){ smc_mac_addr[i] = addr[i]; }}/* * smc_get_macaddr is no longer used. If you want to override the default * mac address, call smc_get_mac_addr as a part of the board initialisation. */#if 0void smc_get_macaddr( byte *addr ) { /* MAC ADDRESS AT FLASHBLOCK 1 / OFFSET 0x10 */ unsigned char *dnp1110_mac = (unsigned char *) (0xE8000000 + 0x20010); int i; for (i=0; i<6; i++) { addr[0] = *(dnp1110_mac+0); addr[1] = *(dnp1110_mac+1); addr[2] = *(dnp1110_mac+2); addr[3] = *(dnp1110_mac+3); addr[4] = *(dnp1110_mac+4); addr[5] = *(dnp1110_mac+5); }}#endif/*********************************************** * Show available memory * ***********************************************/void dump_memory_info(void){ word mem_info; word old_bank; old_bank = SMC_inw(BANK_SELECT)&0xF; SMC_SELECT_BANK(0); mem_info = SMC_inw( MIR_REG );// PRINTK2("Memory: %4d available\n", (mem_info >> 8)*2048); SMC_SELECT_BANK(old_bank);}/* . A rather simple routine to print out a packet for debugging purposes.*/#if SMC_DEBUG > 2static void print_packet( byte *, int );#endif#define tx_done(dev) 1/* this does a soft reset on the device */static void smc_reset( void );/* Enable Interrupts, Receive, and Transmit */static void smc_enable( void );/* this puts the device in an inactive state */static void smc_shutdown( void );/* Routines to Read and Write the PHY Registers across the MII Management Interface*/static word smc_read_phy_register(byte phyreg);static void smc_write_phy_register(byte phyreg, word phydata);static int poll4int( byte mask, int timeout ) { //mask=IM_TX_INT.// int CFG_HZ; //add by xu // int tmo = get_timer(0) + timeout * CFG_HZ; //xu 要先測(cè)試延遲時(shí)間在給參數(shù);// int is_timeout = 0; int is_timeout; word old_bank = SMC_inw(BSR_REG);// PRINTK2("Polling...\n"); SMC_SELECT_BANK(2); while((SMC_inw(INT_REG) & mask) == 0) { is_timeout=is_timeout-1; if (is_timeout <= 1) //xu { // is_timeout = 1; break; } } /* restore old bank selection */ SMC_SELECT_BANK(old_bank); if (is_timeout) return 1; else return 0;}/* . Function: smc_reset( void ) . Purpose: . This sets the SMC91113 chip to its normal state, hopefully from whatever . mess that any other DOS driver has put it in. . . Maybe I should reset more registers to defaults in here? SOFTRST should . do that for me. . . Method: . 1. send a SOFT RESET . 2. wait for it to finish . 3. enable autorelease mode . 4. reset the memory management unit . 5. clear all interrupts .*/static void smc_reset( void ){ short temp = 0;// PRINTK2("%s:smc_reset\n", SMC_DEV_NAME); /* This resets the registers mostly to defaults, but doesn't affect EEPROM. That seems unnecessary 本程序不用EEPROM */ SMC_SELECT_BANK( 0 ); SMC_outw( RCR_SOFTRST, RCR_REG ); //Soft reset. set 1 start,and clear 0 stop. while(1) { temp ++; if ((SMC_inw(RCR_REG ) & RCR_SOFTRST) == 0) break; udelay (10000); /* Delay 10ms, try 5 times*/ if (temp >=5) { SMC_outw( (SMC_inw(RCR_REG ) & (~RCR_SOFTRST)) , RCR_REG ); //stop reset. break; } } /* Setup the Configuration Register */ /* This is necessary because the CONFIG_REG is not affected by a soft reset*/ SMC_SELECT_BANK( 1 );#if defined(CONFIG_SMC91113_EXT_PHY) SMC_outw( CONFIG_DEFAULT | CONFIG_EXT_PHY, CONFIG_REG); //這里不用外部#else SMC_outw( CONFIG_DEFAULT, CONFIG_REG); //設(shè)置為默認(rèn)值;#endif /* Release from possible power-down state */ /* Configuration register is not affected by Soft Reset */ SMC_outw( SMC_inw( CONFIG_REG ) | CONFIG_EPH_POWER_EN, CONFIG_REG ); SMC_SELECT_BANK( 0 ); /* this should pause enough for the chip to be happy */ udelay(5); /* Disable transmit and receive functionality */ SMC_outw( RCR_CLEAR, RCR_REG ); udelay(5); //add by xu. SMC_outw( TCR_CLEAR, TCR_REG ); udelay(5); //add by xu. /* set the control register */ SMC_SELECT_BANK( 1 ); udelay(5); //add by xu. SMC_outw( CTL_DEFAULT, CTL_REG ); udelay(5); //add by xu. /* Reset the MMU */ SMC_SELECT_BANK( 2 ); udelay(5); //add by xu. SMC_outw( MC_RESET, MMU_CMD_REG ); while ( SMC_inw( MMU_CMD_REG ) & MC_BUSY ) udelay(5); // Wait until not busy /* Note: It doesn't seem that waiting for the MMU busy is needed here, but this is a place where future chipsets _COULD_ break. Be wary of issuing another MMU command right after this */ /* Disable all interrupts */ SMC_outb( 0, IM_REG );}/* . Function: smc_enable . Purpose: let the chip talk to the outside work . Method: . 1. Enable the transmitter . 2. Enable the receiver . 3. Enable interrupts*/static void smc_enable(){ unsigned short i,j; unsigned char k;// PRINTK2("%s:smc_enable\n", SMC_DEV_NAME); SMC_SELECT_BANK( 0 ); /* see the header file for options in TCR/RCR DEFAULT*/// SMC_outw( TCR_DEFAULT, TCR_REG ); //xu 好像PAD 不成功; SMC_outw(0X81,TCR_REG); udelay(5);// i=SMC_inw( TCR_REG); udelay(5);// SMC_outw( RCR_DEFAULT, RCR_REG ); //strip CRC and enable RX.and rx all multicast. SMC_outw(0X304,RCR_REG); udelay(5);// j=SMC_inw( RCR_REG ); SMC_SELECT_BANK( 2 ); udelay(5); SMC_outb( 0x00, IM_REG ); //enable rx interrupt. add by xu. udelay(5);// k=SMC_inb( IM_REG ); udelay(5); /* clear MII_DIS */// smc_write_phy_register(PHY_CNTL_REG, 0x0000);}/* . Function: smc_shutdown . Purpose: closes down the SMC91xxx chip. . Method: . 1. zero the interrupt mask . 2. clear the enable receive flag . 3. clear the enable xmit flags . . TODO: . (1) maybe utilize power down mode. . Why not yet? Because while the chip will go into power down mode, . the manual says that it will wake up in response to any I/O requests . in the register space. Empirical results do not show this working.*/static void smc_shutdown(){// PRINTK2(CARDNAME ":smc_shutdown\n"); /* no more interrupts for me */ SMC_SELECT_BANK( 2 ); SMC_outb( 0, IM_REG ); /* and tell the card to stay away from that nasty outside world */ SMC_SELECT_BANK( 0 ); SMC_outb( RCR_CLEAR, RCR_REG ); SMC_outb( TCR_CLEAR, TCR_REG );}/* . Function: smc_hardware_send_packet(struct net_device * ) . Purpose: . This sends the actual packet to the SMC9xxx chip. . . Algorithm: . First, see if a saved_skb is available. . ( this should NOT be called if there is no 'saved_skb' . Now, find the packet number that the chip allocated . Point the data pointers at it in memory . Set the length word in the chip's memory . Dump the packet to chip memory . Check if a last byte is needed ( odd length packet ) . if so, set the control flag right . Tell the card to send it . Enable the transmit interrupt, so I know if it failed . Free the kernel data if I actually sent it.*/int smc_send_packet(volatile void *packet, int packet_length) //static is del by xu.//int smc_send_packet(volatile unsigned char *packet, int packet_length){ byte packet_no; //packet number unsigned long ioaddr; //io address byte * buf; //del by xu// unsigned short *buf; int length; int length16; int numPages; int try = 0; int time_out; byte status; unsigned char temp0; unsigned short temp1; // PRINTK3("%s:smc_hardware_send_packet\n", SMC_DEV_NAME); length = ETH_ZLEN < packet_length ? packet_length : ETH_ZLEN; //ETH_ZLEN = 60 min frame size. /* allocate memory ** The MMU wants the number of pages to be the number of 256 bytes ** 'pages', minus 1 ( since a packet can't ever have 0 pages :) ) ** ** The 91C111 ignores the size bits, but the code is left intact ** for backwards and future compatibility. ** ** Pkt size for allocating is data length +6 (for additional status ** words, length and ctl!) ** ** If odd size then last byte is included in this header. */ numPages = ((length & 0xfffe) + 6); //得到的是發(fā)送緩沖區(qū)需要的字節(jié)數(shù); numPages >>= 8; // Divide by 256 if (numPages > 7 ) {// printf("%s: Far too big packet error. \n", SMC_DEV_NAME); //要發(fā)送的報(bào)太長(zhǎng); return 0; } /* now, try to allocate the memory */ SMC_SELECT_BANK( 2 ); SMC_outw( MC_ALLOC | numPages, MMU_CMD_REG ); //SMC_outw(d,r); numPages寫(xiě)入保留區(qū),沒(méi)用?!again: try++; time_out = MEMORY_WAIT_TIME; //=16. do { status = SMC_inb( INT_REG ); if ( status & IM_ALLOC_INT ) { /* acknowledge the interrupt */ SMC_outb( IM_ALLOC_INT, INT_REG ); //not defined. break; } } while ( -- time_out ); if ( !time_out ) {// PRINTK2("%s: memory allocation, try %d failed ...\n",// SMC_DEV_NAME, try); if (try < SMC_ALLOC_MAX_TRY) //SMC_ALLOC_MAX_TRY=5. goto again; else return 0; } // PRINTK2("%s: memory allocation, try %d succeeded ...\n",// SMC_DEV_NAME,// try); /* I can send the packet now.. */ ioaddr = SMC_BASE_ADDRESS; buf = (byte *)packet; //del bu xu. /* If I get here, I _know_ there is a packet slot waiting for me */ packet_no = SMC_inb( AR_REG ); //allocation result. if ( packet_no & AR_FAILED ) { /* or isn't there? BAD CHIP! */// printf("%s: Memory allocation failed. \n",// SMC_DEV_NAME); return 0; } /* we have a packet address, so tell the card to use it */ SMC_outb( packet_no, PN_REG ); //packet no. /* point to the beginning of the packet */
?? 快捷鍵說(shuō)明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -