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

📄 cs8900a.c

📁 我自己编译的armv4i wince60模拟器的bps源文件,已经验证可以使用,欢迎下载
💻 C
📖 第 1 页 / 共 2 页
字号:
//
// Copyright (c) Microsoft Corporation.  All rights reserved.
//
//
// Use of this sample source code is subject to the terms of the Microsoft
// license agreement under which you licensed this sample source code. If
// you did not accept the terms of the license agreement, you are not
// authorized to use this sample source code. For the terms of the license,
// please see the license agreement between you and Microsoft or, if applicable,
// see the LICENSE.RTF on your install media or the root of your tools installation.
// THE SAMPLE SOURCE CODE IS PROVIDED "AS IS", WITH NO WARRANTIES.
//
//------------------------------------------------------------------------------
//
//  File:  cs8900a.c
//
#include <windows.h>
#include <ceddk.h>
#include <ethdbg.h>
#include <oal.h>
#include <nkintr.h>
#include <bsp.h>

//------------------------------------------------------------------------------

#define CS8900A_EISA_NUMBER         0x630e
#define RETRY_COUNT                 1000000

//------------------------------------------------------------------------------

//
// Make a stencil of the CS8900 device hardware. Use explicit types.
//
typedef struct {
    unsigned __int16 DATA0;
    unsigned __int16 DATA1;
    unsigned __int16 TXCMD;
    unsigned __int16 TXLENGTH;
    unsigned __int16 ISQ;
    unsigned __int16 PAGEIX;
    unsigned __int16 PAGE0;
    unsigned __int16 PAGE1;
} CS8900A_REGS;

static CS8900A_REGS *g_pCS8900;
static UINT16 g_hash[4];
static UINT32 g_filter;

//------------------------------------------------------------------------------

#define EISA_NUMBER                 0x0000
#define PRODUCT_ID_CODE             0x0002
#define IO_BASE_ADDRESS             0x0020
#define INTERRUPT_NUMBER            0x0022
#define DMA_CHANNEL_NUMBER          0x0024
#define DMA_START_OF_FRAME          0x0026
#define DMA_FRAME_COUNT             0x0028
#define RXDMA_BYTE_COUNT            0x002a
#define MEMORY_BASE_ADDR            0x002c
#define BOOT_PROM_BASE_ADDR         0x0030
#define BOOT_PROM_ADDR_MASK         0x0034
#define EEPROM_COMMAND              0x0040
#define EEPROM_DATA                 0x0042
#define RECEIVE_FRAME_BYTE_COUNT    0x0050

#define INT_SQ                      0x0120
#define RX_CFG                      0x0102
#define RX_EVENT                    0x0124
#define RX_CTL                      0x0104
#define TX_CFG                      0x0106
#define TX_EVENT                    0x0128
#define TX_CMD                      0x0108
#define BUF_CFG                     0x010A
#define BUF_EVENT                   0x012C
#define RX_MISS                     0x0130
#define TX_COL                      0x0132
#define LINE_CTL                    0x0112
#define LINE_ST                     0x0134
#define SELF_CTL                    0x0114
#define SELF_ST                     0x0136
#define BUS_CTL                     0x0116
#define BUS_ST                      0x0138
#define TEST_CTL                    0x0118
#define AUI_TIME_DOMAIN             0x013C
#define TX_CMD_REQUEST              0x0144
#define TX_CMD_LENGTH               0x0146

#define LOGICAL_ADDR_FILTER_BASE    0x0150
#define INDIVIDUAL_ADDRESS          0x0158

#define RX_STATUS                   0x0400
#define RX_LENGTH                   0x0402
#define RX_FRAME                    0x0404
#define TX_FRAME                    0x0a00

//------------------------------------------------------------------------------

#define ISQ_ID_MASK                 0x003F

#define SELF_CTL_RESET              (1 << 6)

#define SELF_ST_SIBUSY              (1 << 8)
#define SELF_ST_INITD               (1 << 7)

#define LINE_CTL_MOD_BACKOFF        (1 << 11)
#define LINE_CTL_AUI_ONLY           (1 << 8)
#define LINE_CTL_TX_ON              (1 << 7)
#define LINE_CTL_RX_ON              (1 << 6)

#define RX_CFG_RX_OK_IE             (1 << 8)
#define RX_CFG_SKIP_1               (1 << 6)

#define RX_CTL_BROADCAST            (1 << 11)
#define RX_CTL_INDIVIDUAL           (1 << 10)
#define RX_CTL_MULTICAST            (1 << 9)
#define RX_CTL_RX_OK                (1 << 8)
#define RX_CTL_PROMISCUOUS          (1 << 7)
#define RX_CTL_IAHASH               (1 << 6)

#define RX_EVENT_RX_OK              (1 << 8)
#define RX_EVENT_ID                 0x0004

#define TX_CMD_PAD_DIS              (1 << 13)
#define TX_CMD_INHIBIT_CRC          (1 << 12)
#define TX_CMD_ONECOLL              (1 << 9)
#define TX_CMD_FORCE                (1 << 8)
#define TX_CMD_START_5              (0 << 6)
#define TX_CMD_START_381            (1 << 6)
#define TX_CMD_START_1021           (2 << 6)
#define TX_CMD_START_ALL            (3 << 6)

#define BUS_ST_TX_RDY               (1 << 8)

#define BUS_CTL_ENABLE_IRQ          (1 << 15)

//------------------------------------------------------------------------------

#define pBSPArgs                    ((BSP_ARGS *) IMAGE_SHARE_ARGS_UA_START)

//------------------------------------------------------------------------------

static UINT16 ReadPacketPage(UINT16 address);
static VOID WritePacketPage(UINT16 address, UINT16 data);
static UINT32 ComputeCRC(UINT8 *pBuffer, UINT32 length);
extern BOOL CS8900DBG_Init(BYTE *iobase, ULONG membase, USHORT MacAddr[3]);

//------------------------------------------------------------------------------
//
//  Function:  CS8900AInit
//
BOOL
CS8900AInit(UINT8 *pAddress, UINT32 offset, UINT16 mac[3])
{
    UINT32 NumRetries;
    PBYTE  pBaseIOAddress = NULL;
    UINT32 MemoryBase = 0;  
    BOOL   rc = FALSE;

    OALMSGS(OAL_ETHER && OAL_FUNC, (
        L"+CS8900AInit(0x%08x, 0x%08x, %02x:%02x:%02x:%02x:%02x:%02x)\r\n",
        pAddress, offset, mac[0]&0xFF, mac[0]>>8, mac[1]&0xFF, mac[1]>>8,
        mac[2]&0xFF, mac[2]>>8
    ));

    // Save address
    g_pCS8900 = (CS8900A_REGS*)pAddress;

    // First check if there is chip
    if (ReadPacketPage(EISA_NUMBER) != CS8900A_EISA_NUMBER)
    {
        OALMSGS(OAL_ERROR, (L"ERROR: CS8900AInit: Failed detect chip\r\n"));
        goto Exit;
    }

    OALMSGS(OAL_INFO, (L"INFO: CS8900AInit chip detected\r\n"));

    // Initiate a reset sequence in software.
    WritePacketPage(SELF_CTL, SELF_CTL_RESET);

    // Wait for status to indicate initialization is complete.
    NumRetries = 0;
    while ((ReadPacketPage(SELF_ST) & SELF_ST_INITD) == 0)
    {
        if (NumRetries++ >= RETRY_COUNT)
        {
            OALMSGS(OAL_ERROR, (L"ERROR: CS8900AInit: Timed out during initialization\r\n"));
            goto Exit;
        }
    }

    // Wait for status to indicate the EEPROM is done being accessed/programmed.
    NumRetries = 0;
    while ((ReadPacketPage(SELF_ST) & SELF_ST_SIBUSY) != 0)
    {
        if (NumRetries++ >= RETRY_COUNT)
        {
            OALMSGS(OAL_ERROR, (L"ERROR: CS8900AInit: Timed out (EEPROM stayed busy)\r\n"));
            goto Exit;
        }
    }

    pBaseIOAddress   = (PBYTE)OALPAtoVA(pBSPArgs->kitl.devLoc.LogicalLoc, FALSE);
    MemoryBase       = (UINT32)OALPAtoVA(BSP_BASE_REG_PA_CS8900A_MEMBASE, FALSE);
    
    // Initialize the Ethernet controller, set MAC address
    //
    if (!CS8900DBG_Init((PBYTE)pBaseIOAddress, MemoryBase, mac))
    {
        OALMSG(OAL_ERROR, (TEXT("ERROR: InitEthDevice: Failed to initialize Ethernet controller.\r\n")));
        goto Exit;
    }

    // Enable receive
    WritePacketPage(RX_CTL, RX_CTL_RX_OK | RX_CTL_INDIVIDUAL | RX_CTL_BROADCAST);

    // Enable interrupt on receive
    WritePacketPage(RX_CFG, RX_CFG_RX_OK_IE);

    // Configure the hardware to use INTRQ0
    WritePacketPage(INTERRUPT_NUMBER, 0);

    // Enable
    WritePacketPage(LINE_CTL, LINE_CTL_RX_ON | LINE_CTL_TX_ON);

    // Make sure MAC address has been programmed.
    //
    if (!pBSPArgs->kitl.mac[0] && !pBSPArgs->kitl.mac[1] && !pBSPArgs->kitl.mac[2])
    {
        OALMSG(OAL_ERROR, (TEXT("ERROR: InitEthDevice: Invalid MAC address.\r\n")));
        goto Exit;
    }

    // Done
    rc = TRUE;

Exit:
    OALMSGS(OAL_ETHER && OAL_FUNC, (L"-CS8900AInit(rc = %d)\r\n", rc));
    return rc;
}

//------------------------------------------------------------------------------
//
//  Function:  CS8900ASendFrame
//
UINT16
CS8900ASendFrame(UINT8 *pData, UINT32 length)
{
    UINT32 NumRetries;
    BOOL   rc = TRUE;	// Default to failure code

    OALMSGS(OAL_ETHER && OAL_VERBOSE, (
        L"+CS8900ASendFrame(0x%08x, %d)\r\n", pData, length
    ));

    // Send Command
    OUTPORT16(&g_pCS8900->TXCMD, TX_CMD_START_ALL);
    OUTPORT16(&g_pCS8900->TXLENGTH, length);

    NumRetries = 0;
    while ((ReadPacketPage(BUS_ST) & BUS_ST_TX_RDY) == 0)
    {
        if (NumRetries++ >= RETRY_COUNT)
        {
            OALMSGS(OAL_ERROR, (L"ERROR: CS8900AInit: Not ready for TX\r\n"));
            goto Exit;
        }
    }

    length = (length + 1) >> 1;
    while (length-- > 0)

⌨️ 快捷键说明

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