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

📄 lan91c96_init.c

📁 ARM9基于WINDOWSCE的BSP源代码
💻 C
📖 第 1 页 / 共 3 页
字号:
/*
 *
 *Description:
 *             Contains initialization functions required for the driver.
 *
 *
 */
#include <Ndis.h>
#include "LAN91C96_Adapter.H"
#include "LAN91C96_Proto.H"
#include "platform.h"

volatile unsigned long *M68K_ADD;
volatile unsigned short *M68K_DATA;

/*
 Function Name : 	LAN91C96_MiniportInitialize
 Description   :	
					Called by the NDIS Wrapper to initialize the adapter. 
						0. Verify the Adapter v/s the driver
						1. Create and initilize the adapter structure
						2. Read and load the registry settings
						3. Initialize the chip
						4. Establish the link
 Parameters    :
					PNDIS_STATUS OpenErrorStatus - Additional error status, if return value is error
					       PUINT MediumIndex	 - specifies the medium type the driver or its network adapter uses
					PNDIS_MEDIUM MediumArray	 - Specifies an array of NdisMediumXXX values from which 
													MiniportInitialize selects one that its network adapter supports 
													or that the driver supports as an interface to higher-level drivers. 

					UINT MediumArraySize        - Specifies the number of elements at MediumArray
					NDIS_HANDLE AdapterHandle   - Specifies a handle identifying the miniport抯 network adapter, 
													which is assigned by the NDIS library
					NDIS_HANDLE ConfigurationContext - Specifies a handle used only during initialization for 
														 calls to NdisXXX configuration and initialization functions

 Return Value  :
					NDIS_STATUS		Status
			
*/

NDIS_STATUS LAN91C96_MiniportInitialize(
						
										PNDIS_STATUS	OpenErrorStatus,
											PUINT			MediumIndex,
											PNDIS_MEDIUM	MediumArray,
											UINT			MediumArraySize,
											NDIS_HANDLE		AdapterHandle,
											NDIS_HANDLE		ConfigurationContext
										)
{
    const NDIS_PHYSICAL_ADDRESS HighestAcceptedMax = NDIS_PHYSICAL_ADDRESS_CONST(-1,-1);
	NDIS_STATUS         Status=NDIS_STATUS_SUCCESS;
	UINT                ArrayIndex;
	PMINIPORT_ADAPTER   Adapter;
	USHORT				temp;

    
    RETAILMSG(ZONE_INIT, (TEXT("LAN91C96 ==> MiniportInitialize  \r\n")));

	//Check for the supported 802.3 media
	for(ArrayIndex = 0; ArrayIndex < MediumArraySize; ArrayIndex++)
           if(MediumArray[ArrayIndex] == NdisMedium802_3)   break;
    if(ArrayIndex == MediumArraySize)
    {
		RETAILMSG(ZONE_INIT, (TEXT("LAN91C96 :  ERROR - No Supported Media types \r\n")));
        RETAILMSG(ZONE_INIT, (TEXT("LAN91C96 <== MiniportInitialize  \r\n")));
        return(NDIS_STATUS_UNSUPPORTED_MEDIA);
    }
    *MediumIndex = ArrayIndex;
	
	//Allocate memory for the adapter structure
	temp = MINIPORT_ADAPTER_SIZE;
	Status = NdisAllocateMemory((PVOID *) &Adapter, MINIPORT_ADAPTER_SIZE, 0, HighestAcceptedMax);
    if(Status != NDIS_STATUS_SUCCESS)
    {
        RETAILMSG(ZONE_INIT, (TEXT("LAN91C96:   ERROR - No Memory for Adapter Structure!\r\n")));
		RETAILMSG(ZONE_INIT, (TEXT("LAN91C96 <== MiniPort Initialize\r\n")));
        return(NDIS_STATUS_RESOURCES);
    }
    NdisZeroMemory(Adapter, MINIPORT_ADAPTER_SIZE); //Clean it up
	Adapter->AdapterHandle = AdapterHandle;
	Adapter->State = INITIALIZING_STATE;

    //Get the adapter information from the register
	Status = GetRegistrySettings(Adapter, ConfigurationContext);
	if(Status != NDIS_STATUS_SUCCESS)
    {
        RETAILMSG(ZONE_INIT, (TEXT("LAN91C96: ERROR - Configure Adapter failed!\r\n")));
		RETAILMSG(ZONE_INIT, (TEXT("LAN91C96 <== MiniPort Initialize\r\n")));
        BackOut(Adapter);
        return(NDIS_STATUS_FAILURE);
    }
	
	//Register the IOSpace and the interrupt
	RETAILMSG(ZONE_INIT, (TEXT("LAN91C96 <== Before Calling SetAttribute\r\n")));
	NdisMSetAttributes(Adapter->AdapterHandle, (NDIS_HANDLE) Adapter, NULL, NdisInterfaceInternal);
	RETAILMSG(ZONE_INIT, (TEXT("LAN91C96 <== after Calling SetAttribute\r\n")));
#if 0
	Status = NdisMRegisterIoPortRange((PVOID *) &(Adapter->IOBase),
			Adapter->AdapterHandle,
			Adapter->IOBase,
			16);
	if(Status != NDIS_STATUS_SUCCESS)
	{
		RETAILMSG(1, (TEXT("LAN91C96 : ERROR - Can't Register I/O Port Range!\r\n")));
		RETAILMSG(ZONE_INIT, (TEXT("LAN91C96 <== MiniPort Initialize\r\n")));
        BackOut(Adapter);
		return(NDIS_STATUS_RESOURCES);
	}
//#else
	{
		PULONG BusBase;
		PHYSICAL_ADDRESS PhysicalIoBase;
		
		PhysicalIoBase.LowPart = Adapter->IOBase;
		PhysicalIoBase.HighPart = 0;
		BusBase = (PULONG) MmMapIoSpace( PhysicalIoBase,
			0x1000,
			FALSE );
		RETAILMSG(1,(TEXT("BusBase = 0x%x",BusBase)));
		if( BusBase == NULL )
		{
            RETAILMSG(1, (TEXT("Error!! Cannot map the address range 0x%08X\r\n"), Adapter->IOBase));
			RETAILMSG(ZONE_INIT, (TEXT("LAN91C96 <== MiniPort Initialize\r\n")));
			BackOut(Adapter);
			return(NDIS_STATUS_RESOURCES);
		}		
		Adapter->IOBase = (ULONG)BusBase;		
	}
#else
	{
//Because using 68K bus, we should disable ITU656 and SCI (close pin mux)		
		PHYSICAL_ADDRESS PhysicalIoBase;
		volatile unsigned long *SC_ADD;
		PhysicalIoBase.HighPart = 0;
		PhysicalIoBase.LowPart = 0x2002010C;
		SC_ADD = (unsigned long *)MmMapIoSpace( PhysicalIoBase, 0x100 ,FALSE );
		RETAILMSG(0,(TEXT("SC_ADD = 0x%x\r\n"),SC_ADD));

		*(volatile long *)SC_ADD	|= 0xC000;			//0x2002010c
 
	}
{
//********************************************************************

		PHYSICAL_ADDRESS PhysicalIoBase;

		PhysicalIoBase.HighPart = 0;
		PhysicalIoBase.LowPart = 0x20004000;
		M68K_ADD = (unsigned long *)MmMapIoSpace( PhysicalIoBase, 0x400 ,FALSE );
		RETAILMSG(0,(TEXT("M68K_ADD = 0x%x\r\n"),M68K_ADD));
		
		M68K_DATA = (unsigned short *)(M68K_ADD + 1);
		RETAILMSG(0,(TEXT("M68K_DATA = 0x%x\r\n"),M68K_DATA));
{
	unsigned short  data;

	//enter into 68k bus mode
	LAN91C96_Write(0x200,0x01);
	LAN91C96_Read(0x30E,&data);	//*M68K_DATA;

	RETAILMSG(1,(TEXT("BANK SELECT REGISTER = 0X%X\r\n"),data));
}
//********************************************************************		
}
#endif
	Adapter->IsPortRegistered = TRUE;

	Status = NdisMRegisterInterrupt(&Adapter->InterruptInfo,
				Adapter->AdapterHandle,
				Adapter->InterruptLevel,
				2,
				TRUE,
				FALSE,
				NdisInterruptLatched
				);

	if(Status != NDIS_STATUS_SUCCESS)
    {
        RETAILMSG(1, (TEXT("LAN91C96 : ERROR - Can't Attach to Interrupt!\r\n")));
		RETAILMSG(ZONE_INIT, (TEXT("LAN91C96:<== MiniPort Initialize\r\n")));
        BackOut(Adapter);		
        return(Status);
    }
	else
	RETAILMSG(ZONE_INIT, (TEXT("LAN91C96:Interrupt Registered !!!Adapter->InterruptLevel = 0x%x \r\n"),Adapter->InterruptLevel));

	Adapter->IsInterruptSet = TRUE;
	
	//Allocate the Adapter Interface Memory.
    Status = NdisAllocateMemory((PVOID *) &Adapter->LookAheadBuffer,
		LOOK_AHEAD_BUFFER_SIZE,
		0,
		HighestAcceptedMax);
	if(Status != NDIS_STATUS_SUCCESS)
    {
        RETAILMSG(1, (TEXT("LAN91C96:ERROR : Can't Allocate LookAhead Buffer!\r\n")));
        BackOut(Adapter);
		RETAILMSG(ZONE_INIT, (TEXT("LAN91C96:<== MiniPort Initialize\r\n")));
        return(NDIS_STATUS_FAILURE);
    }
	else
	{
		RETAILMSG(ZONE_INIT, (TEXT("LAN91C96:Allocated LookAhead Buffer Successfully!\r\n")));
	}
    
    //Allocate the TX Buffer memory
    Status = NdisAllocateMemory((PVOID *) &Adapter->TxBuffer,
        MAX_FRAME_SIZE,
        0,
        HighestAcceptedMax);
    if(Status != NDIS_STATUS_SUCCESS)
    {
        RETAILMSG(1, (TEXT("LAN91C96:ERROR : Can't Allocate Tx Buffer!\r\n")));
        BackOut(Adapter);
		RETAILMSG(ZONE_INIT, (TEXT("LAN91C96:<== MiniPort Initialize\r\n")));
        return(NDIS_STATUS_FAILURE);
    }
	else
	{
		RETAILMSG(ZONE_INIT, (TEXT("LAN91C96:Allocated Tx Buffer Successfully!\r\n")));
	}

    
	if(!AdapterVerify(Adapter))
	{
        RETAILMSG(1, (TEXT("LAN91C96:Adapter failed to Verify!\r\n")));
		RETAILMSG(ZONE_INIT, (TEXT("LAN91C96:<== MiniPort Initialize\r\n")));
        return(NDIS_STATUS_FAILURE);
    }

	//Disable all interrupts until the initzing is completed
	//This clears the mask and any generated interrupts !!
    LAN91C96_Write(	Adapter->IOBase + BANK_SELECT, 2 );
    LAN91C96_Write( Adapter->IOBase + BANK2_INT_STS, 0 );

	//Check for the MAC Address
	//If Adapter->MACAddress != NULL then we need to write the new mac address to the LAN91C96 register
	//Else read the LAN91C96 register and store the mac addr. in Adapter->MACAddress
	if (Adapter->MACAddress[0] == 0 &&
		Adapter->MACAddress[1] == 0 &&
		Adapter->MACAddress[2] == 0)
	{
		//Read the MAC address from the register
		LAN91C96_Write(Adapter->IOBase + BANK_SELECT, 1);
		LAN91C96_Read(Adapter->IOBase + BANK1_IA0, &temp);
		temp = ((HIBYTE(temp) >> 8) | (LOBYTE(temp) << 8));
		Adapter->MACAddress[0] = temp;
		LAN91C96_Read(Adapter->IOBase + BANK1_IA2, &temp);
		temp = ((HIBYTE(temp) >> 8) | (LOBYTE(temp) << 8));
		Adapter->MACAddress[1] = temp;
		LAN91C96_Read(Adapter->IOBase + BANK1_IA4, &temp);
		temp = ((HIBYTE(temp) >> 8) | (LOBYTE(temp) << 8));
		Adapter->MACAddress[2] = temp;
	}
	else
	{
		//Write the new MAC address to the register
		LAN91C96_Write(Adapter->IOBase + BANK_SELECT, 1);
		temp = ((HIBYTE(Adapter->MACAddress[0]) >> 8) | (LOBYTE(Adapter->MACAddress[0]) << 8));
		LAN91C96_Write(Adapter->IOBase + BANK1_IA0, temp);
		temp = ((HIBYTE(Adapter->MACAddress[1]) >> 8) | (LOBYTE(Adapter->MACAddress[1]) << 8));
		LAN91C96_Write(Adapter->IOBase + BANK1_IA2, temp);
		temp = ((HIBYTE(Adapter->MACAddress[2]) >> 8) | (LOBYTE(Adapter->MACAddress[2]) << 8));
		LAN91C96_Write(Adapter->IOBase + BANK1_IA4, temp);
	}
//fix MAC address****************
/*
		Adapter->MACAddress[0] =0x0002; 
		Adapter->MACAddress[1] =0x3333;
		Adapter->MACAddress[2] =0x2222;
*/
	{
		PULONG MAC;
		PHYSICAL_ADDRESS PhysicalIoBase;
		
		PhysicalIoBase.LowPart = 0xc40c001C;
		PhysicalIoBase.HighPart = 0;
		MAC = (PULONG) MmMapIoSpace( PhysicalIoBase,	0x1000,	FALSE );
		if( MAC == NULL )
		{
            		RETAILMSG(1, (TEXT("Error!! Cannot map the address range 0xc40c0018\r\n")));
			BackOut(Adapter);
			return(NDIS_STATUS_RESOURCES);
		}			

		Adapter->MACAddress[0] = (*(USHORT *)MAC)&0xFFFF;
		Adapter->MACAddress[1] =(USHORT)(((*(ULONG *)MAC)>>16)&0xFFFF);
		Adapter->MACAddress[2] =(*(USHORT *)(MAC+1))&0xFFFF;
		
//Write the new MAC address to the register
		LAN91C96_Write(Adapter->IOBase + BANK_SELECT, 1);
//		temp = ((HIBYTE(Adapter->MACAddress[0]) >> 8) | (LOBYTE(Adapter->MACAddress[0]) << 8));
		LAN91C96_Write(Adapter->IOBase + BANK1_IA0, Adapter->MACAddress[0]);
//		temp = ((HIBYTE(Adapter->MACAddress[1]) >> 8) | (LOBYTE(Adapter->MACAddress[1]) << 8));
		LAN91C96_Write(Adapter->IOBase + BANK1_IA2, Adapter->MACAddress[1]);
//		temp = ((HIBYTE(Adapter->MACAddress[2]) >> 8) | (LOBYTE(Adapter->MACAddress[2]) << 8));
		LAN91C96_Write(Adapter->IOBase + BANK1_IA4, Adapter->MACAddress[2]);
//***********************************
	}
	//Output the MAC Address
	LAN91C96_Write(Adapter->IOBase + BANK_SELECT, 1);
	LAN91C96_Read(Adapter->IOBase + BANK1_IA0, &temp);
	RETAILMSG(1, (TEXT("MAC Address : %02X-%02X"), LOBYTE(temp), HIBYTE(temp)));
	LAN91C96_Read(Adapter->IOBase + BANK1_IA2, &temp);
	RETAILMSG(1, (TEXT("-%02X-%02X"), LOBYTE(temp), HIBYTE(temp)));
	LAN91C96_Read(Adapter->IOBase + BANK1_IA4, &temp);

⌨️ 快捷键说明

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