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

📄 ether.c

📁 PXA270的EBOOT源代码!可以启动wince内核
💻 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 Intel Mainstone II 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 <mainstoneii.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				0x20
#define PCMCIA_POWER			0x0F
#define PCMCIA_RESET			0x10
#define VS1						0x40
#define VS2						0x80
#define PCMCIA_VCC3V_VPP0V		0x08
#define PCMCIA_VCC5V_VPP0V		0x04
#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


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)
{
    UINT32 SlotVoltage = PCMCIA_VCC3V_VPP0V;
    volatile UINT32 *pSRCR = NULL;
    volatile MAINSTONEII_BLR_REGS *pBLRegs = (volatile MAINSTONEII_BLR_REGS *) OALPAtoVA(MAINSTONEII_BASE_REG_PA_FPGA, FALSE);
    volatile BULVERDE_MEMCTRL_REG *pMEMCTRLRegs = (volatile BULVERDE_MEMCTRL_REG *) OALPAtoVA(BULVERDE_BASE_REG_PA_MEMC, FALSE);

    switch (EthDevice)
    {
    case ETH_DEVICE_PCMCIA0:
        pSRCR = (volatile UINT32 *)&(pBLRegs->pcmcia0_srcr);
        break;
    case ETH_DEVICE_PCMCIA1:       
        pSRCR = (volatile UINT32 *)&(pBLRegs->pcmcia1_srcr);
        break;
    default:
        break;
    }

    if (!pSRCR)
    {
        return(FALSE);
    }

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

        // Configure the MAX1602EE power supply based on the voltage requirement of the card.
        //
        if (((*pSRCR & VS1) == VS1) && ((*pSRCR & VS2) == VS2))
        {
            // 5 volt card detected.
            //
            SlotVoltage = PCMCIA_VCC5V_VPP0V;
        }
        else
        {
            // 3.3 volt card detected.
            //
            SlotVoltage = PCMCIA_VCC3V_VPP0V;
        }

        *pSRCR &= ~(PCMCIA_POWER | PCMCIA_RESET);
        msWait(50);

        *pSRCR |= SlotVoltage;
        msWait(50);

        *pSRCR |= PCMCIA_RESET;
        msWait(50);

        *pSRCR &= ~PCMCIA_RESET;
        msWait(50);

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

        return (TRUE);
    }

    return (FALSE);
}


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


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

    //
    Attribute = *pAttribute;
    if (Attribute != (UINT8) 0x01)
    {
        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 PCMCIA Slot 1.
// EthDevice == 2 --> 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 MAINSTONEII_BLR_REGS *pBLRegs = (volatile MAINSTONEII_BLR_REGS *) OALPAtoVA(MAINSTONEII_BASE_REG_PA_FPGA, FALSE);

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

    // PCMCIA NIC?
    //
    if ((EthDevice == ETH_DEVICE_PCMCIA0) || (EthDevice == ETH_DEVICE_PCMCIA1))
    {
        // Locate the NIC card in the specified slot.
        //
        if (LocatePCMCIACard(EthDevice, SlotAddress))
        {
            // Determine the PCMCIA card type.
            //
            if (VerifyEthernetPCMCIACard(SlotAddress))
            {
                if ((((pBLRegs->pcmcia0_srcr & PCMCIA_POWER) == PCMCIA_VCC5V_VPP0V) && (EthDevice == ETH_DEVICE_PCMCIA0)) ||
                    (((pBLRegs->pcmcia1_srcr & PCMCIA_POWER) == PCMCIA_VCC5V_VPP0V) && (EthDevice == ETH_DEVICE_PCMCIA1)))
                {
                    EdbgOutputDebugString("INFO: 5.0V Card detected in slot %d.\r\n", ((EthDevice == ETH_DEVICE_PCMCIA0)?0:1));

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

                    uByte       = 0x1;            
                    Offset      = CIS_R0 + 2;
                    pAttribute  = (UINT8 *) SlotAddress;
                    pAttribute += Offset; 
                    *pAttribute = uByte;
                }
                else
                {
                    EdbgOutputDebugString("INFO: 3.3V Card detected in slot %d.\r\n", ((EthDevice == ETH_DEVICE_PCMCIA0)?0:1));

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

                EbootDeviceAddress = (EthDevice == ETH_DEVICE_PCMCIA0) ? (UINT32) OALPAtoVA(BULVERDE_BASE_REG_PA_PCMCIA_S0_IO, FALSE) + 0x300 : \
                                                                         (UINT32) OALPAtoVA(BULVERDE_BASE_REG_PA_PCMCIA_S1_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)((EthDevice == ETH_DEVICE_PCMCIA0) ? (BULVERDE_BASE_REG_PA_PCMCIA_S0_IO + 0x300) : (BULVERDE_BASE_REG_PA_PCMCIA_S1_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)(MAINSTONEII_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);
}

⌨️ 快捷键说明

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