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

📄 ether.c

📁 Lido PXA270平台开发板的最新BSP,包括源代码
💻 C
字号:
//
// Copyright (c) Microsoft Corporation.  All rights reserved.
//
//
// Use of this source code is subject to the terms of the Microsoft end-user
// license agreement (EULA) under which you licensed this SOFTWARE PRODUCT.
// If you did not accept the terms of the EULA, you are not authorized to use
// this source code. For a copy of the EULA, please see the LICENSE.RTF on your
// install media.
//
//------------------------------------------------------------------------------
//
//  File:  ether.c
//
//  Core Ethernet routines for the Emdoor XSBASE270_G bootloader.
//
//------------------------------------------------------------------------------
/* 
** INTEL CONFIDENTIAL
** Copyright 2000-2003 Intel Corporation All Rights Reserved.
**
** The source code contained or described herein and all documents
** related to the source code (Material) are owned by Intel Corporation
** or its suppliers or licensors.  Title to the Material remains with
** Intel Corporation or its suppliers and licensors. The Material contains
** trade secrets and proprietary and confidential information of Intel
** or its suppliers and licensors. The Material is protected by worldwide
** copyright and trade secret laws and treaty provisions. No part of the
** Material may be used, copied, reproduced, modified, published, uploaded,
** posted, transmitted, distributed, or disclosed in any way without Intel抯
** prior express written permission.

** No license under any patent, copyright, trade secret or other intellectual
** property right is granted to or conferred upon you by disclosure or
** delivery of the Materials, either expressly, by implication, inducement,
** estoppel or otherwise. Any license under such intellectual property rights
** must be express and approved by Intel in writing.
*/

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

// Miscellaneous definitions.
//
#define CARD_DETECT				0x01
#define PCMCIA_POWER			0x01
#define PCMCIA_RESET			0x02
#define PCMCIA_BUS				0x20
#define VS1						0x40
#define VS2						0x80
#define CIS_R0                  0x3F8
#define OSCR_OFFSET             0x10        // OSCR0


// 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

extern BOOL LAN91CsetMAC(UINT8 *pAddress, UINT16 mac[3]);

static void Wait(UINT32 microSeconds)
{
    volatile UINT32 *TimerOSCRAddress= (volatile UINT32 *) OALPAtoVA((BULVERDE_BASE_REG_PA_OST + OSCR_OFFSET), FALSE);
    UINT32 Value, Time;

    Time   = *TimerOSCRAddress;
    Value = Time + (microSeconds * 4);
    if (Value < Time)
    {  // test for wrap.
        while (Time < *TimerOSCRAddress);
    }
    while (*TimerOSCRAddress <= Value);

}


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


VOID OALStall(UINT32 microSeconds)
{
    Wait(microSeconds);
}


BOOL LocatePCMCIACard(ETH_DEVICE_TYPE EthDevice, UINT32 SlotAddress)
{
    volatile XSBASE270_G_CPLD_REGS *pCPLDRegs = (volatile XSBASE270_G_CPLD_REGS *) OALPAtoVA(XSBASE270_G_BASE_REG_PA_CPLD, FALSE);
    volatile BULVERDE_MEMCTRL_REG *pMEMCTRLRegs = (volatile BULVERDE_MEMCTRL_REG *) OALPAtoVA(BULVERDE_BASE_REG_PA_MEMC, FALSE);

    // Initialize the PCMCIA interface if a card is detected.
    //
    if ((pCPLDRegs->STATUS & CARD_DETECT) == CARD_DETECT)
    {
        EdbgOutputDebugString("INFO: Performing PCMCIA NIC initialization.  Please wait...\r\n");

        pCPLDRegs->BCR1 &= ~PCMCIA_RESET;
        pCPLDRegs->BCR1 |= PCMCIA_POWER;
        msWait(50);

        pCPLDRegs->BCR1 |= PCMCIA_RESET;
        msWait(50);

        pCPLDRegs->BCR1 &= ~(PCMCIA_RESET | PCMCIA_BUS);
        msWait(50);

        pMEMCTRLRegs->mecr = 0x3;          
        msWait(50);         

        return (TRUE);
    }

    return (FALSE);
}


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

    EdbgOutputDebugString("pAttribute = %x.\r\n", pAttribute);

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

    //
    Attribute = *pAttribute;
    if (Attribute != (UINT8) 0x1C)
    {
        EdbgOutputDebugString("ERROR: Invalid PCMCIA 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)
            {
                return (TRUE);
            }
        }
    }

    return (FALSE);

}


// EthDevice == 0 --> Try to initialize PCMCIA Slot 0.
// EthDevice == 1 --> Try to initialize SMSC.
UINT32 InitSpecifiedEthDevice(OAL_KITL_ARGS *pKITLArgs, UINT32 EthDevice)
{
    UINT32 EbootDeviceAddress = 0;
    UINT32 Offset;      
    UINT8 *pAttribute = NULL;
    UINT8 uByte;
    UINT32 SlotAddress = 0;

    volatile XSBASE270_G_CPLD_REGS *pCPLDRegs = (volatile XSBASE270_G_CPLD_REGS *) OALPAtoVA(XSBASE270_G_BASE_REG_PA_CPLD, FALSE);

    
    // Determine the NIC's address.
    //
    switch (EthDevice)
    {
    case ETH_DEVICE_PCMCIA0: 
        SlotAddress = (UINT32) OALPAtoVA(BULVERDE_BASE_REG_PA_PCMCIA_S0_ATTR, FALSE);
        EdbgOutputDebugString("\nINFO: Trying to locate/initialize PCMCIA NIC in slot 0...\r\n");
        break;
    case ETH_DEVICE_SMSC:
    default:
        SlotAddress = (UINT32) OALPAtoVA(XSBASE270_G_BASE_REG_PA_SMSC_ETHERNET, FALSE);
        EdbgOutputDebugString("\nINFO: Trying to initialize the built-in SMSC NIC...\r\n");
        break;
    }

    // PCMCIA NIC?
    //
    if (EthDevice == ETH_DEVICE_PCMCIA0)
    {
        // Locate the NIC card in the specified slot.
        //
        if (LocatePCMCIACard(EthDevice, SlotAddress))
        {
            // Determine the PCMCIA card type.
            //
            if (VerifyEthernetPCMCIACard(SlotAddress))
            {
                EdbgOutputDebugString("INFO: 3.3V Card detected in CF slot.\r\n");

                uByte       = 0x20;
                Offset      = CIS_R0;
                pAttribute  = (UINT8 *) SlotAddress;
                pAttribute += Offset; 
                *pAttribute = uByte;

                EbootDeviceAddress = (UINT32) OALPAtoVA(BULVERDE_BASE_REG_PA_PCMCIA_S0_IO, FALSE) + 0x300;

                // Only supported PCMCIA 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;
                    pKITLArgs->devLoc.PhysicalLoc = (PVOID)(BULVERDE_BASE_REG_PA_PCMCIA_S0_IO + 0x300);
                    pKITLArgs->devLoc.LogicalLoc  = (DWORD)pKITLArgs->devLoc.PhysicalLoc;

                    EdbgOutputDebugString("INFO: NE2000 Ethernet controller initialized.\n");
                    return(EDBG_ADAPTER_NE2000);
                }
                else
                {
                    EdbgOutputDebugString("ERROR: Failed to initialize NE2000 Ethernet controller.\n");
                }
            }
        }

        return (-1);
    }
    else
    {
        // Use the SMSC LAN91C111 NIC.
        //
        EbootDeviceAddress = (SlotAddress + 0x300);

        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.PhysicalLoc = (PVOID)(XSBASE270_G_BASE_REG_PA_SMSC_ETHERNET + 0x300);
            pKITLArgs->devLoc.LogicalLoc  = (DWORD)pKITLArgs->devLoc.PhysicalLoc;

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

    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);
}

UINT32 setMAC_Address(OAL_KITL_ARGS *pKITLArgs)
{
    UINT32 EbootDeviceAddress = 0;
    UINT32 SlotAddress = 0;

    SlotAddress = (UINT32) OALPAtoVA(XSBASE270_G_BASE_REG_PA_SMSC_ETHERNET, FALSE);

    EbootDeviceAddress = (SlotAddress + 0x300);

    if (LAN91CsetMAC((UINT8 *) EbootDeviceAddress, pKITLArgs->mac))
    {
        EdbgOutputDebugString("Set MAC address complete.\n");
    }
    else
    {
        EdbgOutputDebugString("ERROR: Failed to write MAC address.\n");
    }

    return (-1);
}

⌨️ 快捷键说明

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