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

📄 ether.c

📁 老外的一个开源项目
💻 C
字号:
// Copyright (c) David Vescovi.  All rights reserved.
// Part of Project DrumStix
// Windows Embedded Developers Interest Group (WE-DIG) community project.
// http://www.we-dig.org
// Copyright (c) Microsoft Corporation.  All rights reserved.
//------------------------------------------------------------------------------
//
//  File:  ether.c
//
//  Core Ethernet routines for the bootloader.
//
//------------------------------------------------------------------------------

#include <windows.h>
#include <ethdbg.h>
#include <halether.h>
#include <oal_memory.h>
#include <oal_ethdrv.h>
#include <ceddk.h>
#include "bsp.h"
#include "loader.h"

// Miscellaneous definitions.
//
#define CIS_R0                  0x3F8


// Function pointers to the support library functions of the currently installed debug ethernet controller.
//
PFN_EDBG_INIT                     pfnEDbgInit;
PFN_EDBG_GET_FRAME                pfnEDbgGetFrame;
PFN_EDBG_SEND_FRAME               pfnEDbgSendFrame;
PFN_EDBG_ENABLE_INTS              pfnEDbgEnableInts;
PFN_EDBG_DISABLE_INTS             pfnEDbgDisableInts;
#ifdef IMGSHAREETH
PFN_EDBG_CURRENT_PACKET_FILTER    pfnCurrentPacketFilter;
PFN_EDBG_MULTICAST_LIST           pfnMulticastList;
#endif


static void msWait(UINT32 msVal) 
{
    OALStall(msVal*1000);
}



static BOOL VerifyEthernetCFCard(UINT32 SlotAddress)
{
    UINT8 Attribute;
    UINT8 *pAttribute = (UINT8 *) SlotAddress;
    UINT8 *pLastAttribute;


    // End of the slot attribute list.
    //
    pLastAttribute = pAttribute + 0x200;

    //
	Attribute = *pAttribute;
	
	if (Attribute != (UINT8) 0x01)
    {
        EdbgOutputDebugString("ERROR: Invalid slot configuration data 0x%x.\r\n", Attribute);
        return (FALSE);
    }

    // Look for tuple.
    //
    while ((Attribute != (UINT8) 0x21) && (pAttribute < pLastAttribute))
    {
        pAttribute += sizeof(UINT16);                      // Step to tuple length.
        Attribute = *pAttribute;
        pAttribute += sizeof(UINT16) + (sizeof(UINT16) * Attribute); // Step over tuple.
        Attribute = *pAttribute;
    }

    // Find a valid tuple?
    //
    if (pAttribute < pLastAttribute)
    {
        pAttribute += sizeof(UINT16);                     // Defined tuple length.
        if ((Attribute = *pAttribute) == 2)
        {
            pAttribute += sizeof(UINT16);
            Attribute = *pAttribute;

            // Found an Ethernet card...
            //
            if (Attribute == 0x06)
            {

				EdbgOutputDebugString("INFO: 3.3V Ethernet Card detected in CF slot.\r\n");

                pAttribute  = (UINT8 *) SlotAddress;
                pAttribute += CIS_R0; 
                *pAttribute = 0x20;
				return (TRUE);
            }
        }
    }

    return (FALSE);
}


UINT32 InitSpecifiedEthDevice(OAL_KITL_ARGS *pKITLArgs, UINT32 EthDevice, EBOOT_CFG *pEbootCfg)
{
	UINT32 EbootDeviceAddress = 0;
    UINT32 SlotAddress = 0;


    // Determine the NIC's address.
    //
    switch (EthDevice)
    {
    case BOOT_DEVICE_CF: 
		if (!(pEbootCfg->dwPlatformHardware & GUMCFG_CF))
		{
			EdbgOutputDebugString("INFO: CF hardware not defined!\r\n");
			return(-1);
		}
		if (pEbootCfg->dwPlatformHardware & GUMCFG_WIFI)
		{
			SlotAddress = (UINT32) OALPAtoVA(PXA255_BASE_REG_PA_PCMCIA_S1_ATTR, FALSE);
			EdbgOutputDebugString("INFO: Trying to locate/initialize CF NIC in slot 1...\r\n");
		}
		else
		{
			SlotAddress = (UINT32) OALPAtoVA(PXA255_BASE_REG_PA_PCMCIA_S0_ATTR, FALSE);
			EdbgOutputDebugString("INFO: Trying to locate/initialize CF NIC in slot 0...\r\n");
		}

        break;
	case BOOT_DEVICE_STUART:
		if (!(pEbootCfg->dwPlatformHardware & GUMCFG_STUART))
		{
			EdbgOutputDebugString("INFO: STUART hardware not defined!\r\n");
			return(-1);
		}
        EdbgOutputDebugString("INFO: Trying to initialize STUART...\r\n");
        break;
	case BOOT_DEVICE_HWUART:
		if (!(pEbootCfg->dwPlatformHardware & GUMCFG_HWUART))
		{
			EdbgOutputDebugString("INFO: HWUART hardware not defined!\r\n");
			return(-1);
		}
        EdbgOutputDebugString("INFO: Trying to initialize HWUART...\r\n");
        break;
	case BOOT_DEVICE_BTUART:
		if (!(pEbootCfg->dwPlatformHardware & GUMCFG_BTUART))
		{
			EdbgOutputDebugString("INFO: BTUART hardware not defined!\r\n");
			return(-1);
		}
        EdbgOutputDebugString("INFO: Trying to initialize BTUART...\r\n");
        break;
	case BOOT_DEVICE_ETH2:
		if (!(pEbootCfg->dwPlatformHardware & GUMCFG_ETH2))
		{
			EdbgOutputDebugString("INFO: ETH2 hardware not defined!\r\n");
			return(-1);
		}
        SlotAddress = (UINT32) OALPAtoVA(SMSC_ETH2_PA_BASE_REG, FALSE);
        EdbgOutputDebugString("INFO: Trying to initialize ETH2 NIC...\r\n");
        break;
	case BOOT_DEVICE_ETH1:
    default:
		if (!(pEbootCfg->dwPlatformHardware & GUMCFG_ETH1))
		{
			EdbgOutputDebugString("INFO: ETH1 hardware not defined!\r\n");
			return(-1);
		}
        SlotAddress = (UINT32) OALPAtoVA(SMSC_ETH1_PA_BASE_REG, FALSE);
        EdbgOutputDebugString("INFO: Trying to initialize ETH1 NIC...\r\n");
        break;
    }

	switch (EthDevice)
	{
    case BOOT_DEVICE_CF: 
        // Locate and determine the CF card type.
        if (VerifyEthernetCFCard(SlotAddress))
        {

			if (pEbootCfg->dwPlatformHardware & GUMCFG_WIFI)
			{
				EbootDeviceAddress = (UINT32) OALPAtoVA(PXA255_BASE_REG_PA_PCMCIA_S1_IO, FALSE) + 0x300;
			}
			else
			{
				EbootDeviceAddress = (UINT32) OALPAtoVA(PXA255_BASE_REG_PA_PCMCIA_S0_IO, FALSE) + 0x300;
			}
            // Only supported CF NIC is an NE2000-compatible NIC.  Initialize it.
            //
            if (NE2000Init((BYTE *) EbootDeviceAddress, 1, pKITLArgs->mac))
            {
                pfnEDbgInit            = NE2000Init;
                pfnEDbgGetFrame        = NE2000GetFrame;       
                pfnEDbgSendFrame       = NE2000SendFrame;      
                pfnEDbgEnableInts      = NE2000EnableInts;     
                pfnEDbgDisableInts     = NE2000DisableInts;    
#ifdef IMGSHAREETH
                pfnCurrentPacketFilter = NULL;
                pfnMulticastList       = NULL;
#endif

                // Save the device location information for later use.
                //
                pKITLArgs->devLoc.IfcType     = Internal;
                pKITLArgs->devLoc.BusNumber   = 0;
				if (pEbootCfg->dwPlatformHardware & GUMCFG_WIFI)
				{
					pKITLArgs->devLoc.LogicalLoc = (DWORD)(PXA255_BASE_REG_PA_PCMCIA_S1_IO + 0x300);
				}
				else
				{
					pKITLArgs->devLoc.LogicalLoc = (DWORD)(PXA255_BASE_REG_PA_PCMCIA_S0_IO + 0x300);
				}
                pKITLArgs->devLoc.PhysicalLoc  = (PVOID)pKITLArgs->devLoc.LogicalLoc;

                EdbgOutputDebugString("INFO: NE2000 Ethernet controller initialized.\n");
                return(EDBG_ADAPTER_NE2000);
            }
            else
            {
                EdbgOutputDebugString("ERROR: Failed to initialize NE2000 Ethernet controller.\n");
            }
		}
        break;
	case BOOT_DEVICE_STUART:
		// STUART device
		pKITLArgs->baudRate = pEbootCfg->downloadBaud;
		pKITLArgs->dataBits = 8;
		pKITLArgs->stopBits = 1;
		pKITLArgs->parity = 0;

		// Save the device location information for later use.
		//
		pKITLArgs->devLoc.IfcType     = Internal;
		pKITLArgs->devLoc.BusNumber   = 0;
		pKITLArgs->devLoc.LogicalLoc = (DWORD)(PXA255_BASE_REG_PA_STUART);
		pKITLArgs->devLoc.PhysicalLoc  = (PVOID)pKITLArgs->devLoc.LogicalLoc;

		EdbgOutputDebugString("INFO: STUART initialized at %d.\r\n",pEbootCfg->downloadBaud);
		return(BOOT_DEVICE_STUART);
        break;
	case BOOT_DEVICE_HWUART:
		// HWUART device
		pKITLArgs->baudRate = pEbootCfg->downloadBaud;
		pKITLArgs->dataBits = 8;
		pKITLArgs->stopBits = 1;
		pKITLArgs->parity = 0;

		// Save the device location information for later use.
		//
		pKITLArgs->devLoc.IfcType     = Internal;
		pKITLArgs->devLoc.BusNumber   = 0;
		pKITLArgs->devLoc.LogicalLoc = (DWORD)(PXA255_BASE_REG_PA_HWUART);
		pKITLArgs->devLoc.PhysicalLoc  = (PVOID)pKITLArgs->devLoc.LogicalLoc;

		EdbgOutputDebugString("INFO: HWUART initialized at %d.\r\n",pEbootCfg->downloadBaud);
		return(BOOT_DEVICE_HWUART);
        break;
	case BOOT_DEVICE_BTUART:
		// HWUART device
		pKITLArgs->baudRate = pEbootCfg->downloadBaud;
		pKITLArgs->dataBits = 8;
		pKITLArgs->stopBits = 1;
		pKITLArgs->parity = 0;

		// Save the device location information for later use.
		//
		pKITLArgs->devLoc.IfcType     = Internal;
		pKITLArgs->devLoc.BusNumber   = 0;
		pKITLArgs->devLoc.LogicalLoc = (DWORD)(PXA255_BASE_REG_PA_BTUART);
		pKITLArgs->devLoc.PhysicalLoc  = (PVOID)pKITLArgs->devLoc.LogicalLoc;

		EdbgOutputDebugString("INFO: BTUART initialized at %d.\r\n",pEbootCfg->downloadBaud);
		return(BOOT_DEVICE_BTUART);
        break;
	case BOOT_DEVICE_ETH2:
		// Use the ETH2 SMSC LAN91C111 NIC.
        //
        EbootDeviceAddress = (SlotAddress + 0x300);
		memcpy(pKITLArgs->mac, pEbootCfg->Eth2mac, (sizeof(UINT16) * 3));
        if (LAN91CInit((UINT8 *) EbootDeviceAddress, 0, pKITLArgs->mac))
        {
            pfnEDbgInit            = (PFN_EDBG_INIT)        LAN91CInit;
            pfnEDbgGetFrame        = (PFN_EDBG_GET_FRAME)   LAN91CGetFrame;       
            pfnEDbgSendFrame       = (PFN_EDBG_SEND_FRAME)  LAN91CSendFrame;      
            pfnEDbgEnableInts      = (PFN_EDBG_ENABLE_INTS) LAN91CEnableInts;     
            pfnEDbgDisableInts     = (PFN_EDBG_DISABLE_INTS)LAN91CDisableInts;    
#ifdef IMGSHAREETH
            pfnCurrentPacketFilter = (PFN_EDBG_CURRENT_PACKET_FILTER) LAN91CCurrentPacketFilter;
            pfnMulticastList       = (PFN_EDBG_MULTICAST_LIST) LAN91CMulticastList;
#endif

            // Save the device location information for later use.
            //
            pKITLArgs->devLoc.IfcType     = Internal;
            pKITLArgs->devLoc.BusNumber   = 0;
            pKITLArgs->devLoc.LogicalLoc = (DWORD)(SMSC_ETH2_PA_BASE_REG + 0x300);
			EdbgOutputDebugString("INFO: SMSC LAN91C111 Ethernet controller #2 initialized.\r\n");
            pKITLArgs->devLoc.PhysicalLoc  = (PVOID)pKITLArgs->devLoc.LogicalLoc;

            return(EDBG_ADAPTER_SMC9000);
        }
        else
        {
            EdbgOutputDebugString("ERROR: Failed to initialize ETH1 SMSC LAN91C111 Ethernet controller.\n");
        }
		break;
	case BOOT_DEVICE_ETH1:
    default:
		// Use the ETH1 SMSC LAN91C111 NIC.
        //
        EbootDeviceAddress = (SlotAddress + 0x300);
		memcpy(pKITLArgs->mac, pEbootCfg->Eth1mac, (sizeof(UINT16) * 3));
        if (LAN91CInit((UINT8 *) EbootDeviceAddress, 0, pKITLArgs->mac))
        {
            pfnEDbgInit            = (PFN_EDBG_INIT)        LAN91CInit;
            pfnEDbgGetFrame        = (PFN_EDBG_GET_FRAME)   LAN91CGetFrame;       
            pfnEDbgSendFrame       = (PFN_EDBG_SEND_FRAME)  LAN91CSendFrame;      
            pfnEDbgEnableInts      = (PFN_EDBG_ENABLE_INTS) LAN91CEnableInts;     
            pfnEDbgDisableInts     = (PFN_EDBG_DISABLE_INTS)LAN91CDisableInts;    
#ifdef IMGSHAREETH
            pfnCurrentPacketFilter = (PFN_EDBG_CURRENT_PACKET_FILTER) LAN91CCurrentPacketFilter;
            pfnMulticastList       = (PFN_EDBG_MULTICAST_LIST) LAN91CMulticastList;
#endif

            // Save the device location information for later use.
            //
            pKITLArgs->devLoc.IfcType     = Internal;
            pKITLArgs->devLoc.BusNumber   = 0;
            pKITLArgs->devLoc.LogicalLoc = (DWORD)(SMSC_ETH1_PA_BASE_REG + 0x300);
			EdbgOutputDebugString("INFO: SMSC LAN91C111 Ethernet controller #1 initialized.\r\n");
            pKITLArgs->devLoc.PhysicalLoc  = (PVOID)pKITLArgs->devLoc.LogicalLoc;

            return(EDBG_ADAPTER_SMC9000);
        }
        else
        {
            EdbgOutputDebugString("ERROR: Failed to initialize ETH1 SMSC LAN91C111 Ethernet controller.\n");
        }
		break;
    }

    return (-1);

}


BOOL OEMEthGetFrame(BYTE *pData,       // OUT - Receives frame data
                    UINT16 *pwLength)  // IN  - Length of Rx buffer
{
    return (pfnEDbgGetFrame(pData, pwLength));
}


BOOL OEMEthSendFrame(BYTE *pData,     // IN - Data buffer
                     DWORD dwLength)  // IN - Length of buffer
{
    int retries = 0;

    // Let's be persistant here
    while (retries++ < 4)
    {
        if (!pfnEDbgSendFrame(pData, dwLength))
            return (TRUE);
        EdbgOutputDebugString("!OEMEthSendFrame failure, retry %u\n",retries);
    }
    return (FALSE);
}

⌨️ 快捷键说明

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