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

📄 fmd.cpp

📁 微软提供的Flash驱动程序
💻 CPP
📖 第 1 页 / 共 3 页
字号:
//
// 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.
//
/*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

Module Name:    FMD.CPP

Abstract:       FLASH Media Driver Interface for Intel XXXXXX StrataFlash Chip

Notes:          The FAL expects the Flash media to be broken up into Flash blocks
                which are then subdivided into physical sectors.  Some types of 
                Flash memory (i.e. NAND Flash) already have this layout.  NOR Flash,
                on the other hand, DOES NOT breakup each Flash block into physical
                sectors.  Instead, each byte is individually addressable (like RAM).
                Despite this characteristic, NOR Flash can still logically be broken
                up into discrete sectors as follows:

                                NOR Flash

                            Sector Data     SectorInfo
                    ---------------------------------
                    |Sector(0)              |       |
                    |Sector(1)              |       |
                    |Sector(2)              |       |       Block 0
                    |...                    |       |
                    |Sector(k)              |       |
                    |                       XXXXXXXXX
                     -------------------------------
                    |Sector(k+1)            |       |
                    |Sector(k+2)            |       |   
                    |Sector(k+3)            |       |       Block 1
                    |...                    |       |
                    |Sector(2k)             |       |
                    |                       XXXXXXXXX
                     -------------------------------
                    |                       |       |
                    |                       |       |
                    |                       |       |       Block 2
                    |                       |       |
                    |                       |       |
                    |                       XXXXXXXXX
                     -------------------------------        ...
                    |                       |       |
                    |                       |       |
                    |                       |       |       Block N
                    |                       |       |
                    |                       |       |
                    |                       XXXXXXXXX
                    ---------------------------------

                That is, each NOR Flash block is logically subdivided into a "page", where each page
                contains space for sector data and SectorInfo metadata.  Most often, Flash blocks are
                a power of 2 in size but the size of a page is not a power of 2 (i.e. 512 + 8 = 520 bytes).
                Thus, each Flash block DOES NOT evenly divide into an integral number of pages and some
                bytes in a block are left unused.  These unused bytes are denoted above by XXXXX's.

                To help clarify how this process works, consider the following example: suppose you have
                a NOR Flash memory device that contains 256 Flash blocks each 256K in size.  From these
                size characteristics, we find:

                    (256K / (512+8)) ==> 504 sectors + 64 unused bytes

                Therefore, each Flash block can map 504 sectors (including SectorInfo metadata) and leave
                64 unused bytes per block.  Notice that 8 bytes is used for the SectorInfo metadata although
                the SectorInfo structure is currently only 6 bytes.  The reason for this is to make sure that
                all sector addresses are DWORD aligned (i.e. 520 divides by 4 evenly while 518 doesn't divide 
                by 4 evenly).  Furthemore, we see that this NOR Flash memory can map (504*256)==>129,024 physical 
                sectors.

                -------

                Two other points are worth mentioning:

                    1) NOR Flash memory is guaranteed by manufacturing to ship with no bad Flash blocks.
                    2) NOR Flash memory doesn't suffer from electrical leakage currents (like NAND Flash) and
                       does not require error-correction codes (ECC) to insure data integrity.

  Environment:  As noted, this media driver works on behalf of the FAL to directly
                access the underlying FLASH hardware.  Consquently, this module 
                needs to be linked with FAL.LIB to produce the device driver 
                named FLASHDRV.DLL.

-----------------------------------------------------------------------------*/
#include <fmd.h>
#include <ceddk.h>
#ifdef READ_FROM_REGISTRY
#include <ddkreg.h>
#endif    // READ_FROM_REGISTRY.
#include "strata.h"

static BOOL g_bPairedFlash = TRUE;        // Indicates whether or not two flash parts are paired to create a 32-bit data interface.


BOOLEAN InitializeFlash(volatile PULONG pBaseAddress, ULONG FlashLength);

DWORD DoBufferedWrite(volatile ULONG ulBlockAddress,
                      volatile SECTOR_ADDR physicalSectorAddr,
                      PUCHAR pBuffer,
                      USHORT NumWriteBytes);

#define ZONE_ERROR      1

#define MIN(a, b) (a < b ? a : b) 

static BOOL g_bXIPMode = FALSE;
static FMD_FLASH_INFO g_FMDInfo;


/*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Function:       FMD_Init()

Description:    Initializes the Flash memory device.

Returns:        Boolean indicating success.
------------------------------------------------------------------------------*/
PVOID FMD_Init(LPCTSTR lpActiveReg, PPCI_REG_INFO pRegIn, PPCI_REG_INFO pRegOut)
{
    volatile PULONG pBaseAddress = NULL;
    ULONG FlashLength = 0;
    BOOL bLastMode;

#ifdef READ_FROM_REGISTRY
    if (lpActiveReg != NULL)
    {
        DDKWINDOWINFO dwi;
        HKEY hConfig;

        // Get flash information from the registry.
        hConfig = OpenDeviceKey((LPCTSTR)lpActiveReg);        
        if (hConfig == NULL)
        {
            DEBUGMSG(1, (TEXT("ERROR: FMD_Init: OpenDeviceKey failed.\r\n")));
            return(NULL);
        }
        dwi.cbSize = sizeof(dwi);
        if (DDKReg_GetWindowInfo(hConfig, &dwi) != ERROR_SUCCESS)
        {
            DEBUGMSG(1, (TEXT("ERROR: FMD_Init: DDKReg_GetWindowInfo() failed.\r\n")));
            return(NULL);
        }
        // The first memory window contains the base address and length of our flash part.
        if (dwi.dwNumMemWindows)
        {
            pBaseAddress = (volatile PULONG)(dwi.memWindows[0].dwBase);
            FlashLength  = (ULONG)(dwi.memWindows[0].dwLen);
        }
    }
#else
    // Get flash base address and length from caller.
    if (!pRegIn || !pRegIn->MemBase.Num || !pRegIn->MemLen.Num)
    {
        DEBUGMSG(1, (TEXT("ERROR: FMD_Init: invalid flash memory base and/or length specified by caller.\r\n")));
        return(NULL);
    }
    else
    {
        pBaseAddress = (volatile PULONG)pRegIn->MemBase.Reg[0];
        FlashLength  = pRegIn->MemLen.Reg[0];
    }
#endif    // READ_FROM_REGISTRY.

    // Run in kernel mode.
    bLastMode = SetKMode(TRUE);

    // Identify the flash part and collect device information.
    //
    if (!InitializeFlash(pBaseAddress, FlashLength))
    {
        DEBUGMSG(1, (TEXT("ERROR: FMD_Init: Failed to initialize flash.\r\n")));
        SetKMode(bLastMode);
        return(NULL);
    }

    DEBUGMSG(1, (TEXT("INFO: FMD_Init: Flash Address=0x%x  Length=0x%x.\r\n"), (ULONG)pBaseAddress, FlashLength));

    SetKMode(bLastMode);
    return(pBaseAddress);
}


/*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Function:       FMD_Deinit()

Description:    De-initializes the Flash memory device.

Returns:        Boolean indicating success.
------------------------------------------------------------------------------*/
BOOL  FMD_Deinit(PVOID pBaseAddress)
{
    return(TRUE);
}


/*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Function:       FMD_GetInfo()

Description:    Determines the size characteristics for the Flash memory device.

Notes:          Notice that although each byte of a NOR Flash block is individually
                addressable, the media is still logically broken up into sectors.
                To compute the number of sectors per block, you have to divide the
                total Flash block size by the number of bytes per sector and the number 
                bytes for the sector metadata.

Returns:        Boolean indicating success.
------------------------------------------------------------------------------*/
BOOL  FMD_GetInfo(PFlashInfo pFlashInfo)
{
    pFlashInfo->flashType           = NOR;
    pFlashInfo->dwNumBlocks         = g_FMDInfo.TotalFlashBlocks;
    pFlashInfo->dwBytesPerBlock     = g_FMDInfo.BlockSize;
    pFlashInfo->wDataBytesPerSector = SECTOR_SIZE;
    pFlashInfo->wSectorsPerBlock    = g_FMDInfo.SectorsPerBlock;  

    return(TRUE);
}


/*
    @func   BOOLEAN | UnlockBlock | Unlocks the specified flash block.
    @rdesc  TRUE returned on success, FALSE on failure.
    @comm    
    @xref   
*/
BOOLEAN SetBlockLock(BLOCK_ID blockID, ULONG NumBlocks, BOOL bLock)
{
    ULONG ulStatus = 0;
    volatile ULONG ulBlockAddress = 0;
    BOOL bLastMode = SetKMode(TRUE);

    while (NumBlocks--)
    {
        // Compute the block address.
        ulBlockAddress  =  g_FMDInfo.BaseAddress + (blockID * g_FMDInfo.BlockSize);

        // Clear the status register.
        WRITE_COMMAND(ulBlockAddress, CLEAR_STATUS_CMD);

        // Set or Clear the block lock bit and confirm.
        WRITE_COMMAND(ulBlockAddress, BLOCK_LOCK_CMD);
        if (bLock)
        {
            WRITE_COMMAND(ulBlockAddress, BLOCK_SETLOCK_CMD);
        }
        else
        {
            WRITE_COMMAND(ulBlockAddress, BLOCK_PROCEED_CMD);
        }

        // Wait for status...
        do
        {
            WRITE_COMMAND(ulBlockAddress, READ_STATUS_CMD);
        }
        while (!CHECK_STATUS(ulBlockAddress, STATUS_READY_MASK));

        // TODO
#if 0
        // Check status.
        if ((ulStatus & 0x00000008) == 0x00000008)
            DEBUGMSG(1, (TEXT("\r\nVoltage Range Error ... Lower flash.\r\n")));
        if ((ulStatus & 0x00080000) == 0x00080000)
            DEBUGMSG(1, (TEXT("\r\nVoltage Range Error ... Upper flash.\r\n")));
    
        if ((ulStatus & 0x00000030) == 0x00000030)
            DEBUGMSG(1, (TEXT("\r\nCommand Sequence Error ... Lower flash.\r\n")));
        if ((ulStatus & 0x00300000) == 0x00300000)
            DEBUGMSG(1, (TEXT("\r\nCommand Sequence Error ... Upper flash.\r\n")));

        if ((ulStatus & 0x00000020) == 0x00000020)
            DEBUGMSG(1, (TEXT("\r\nClear Lock Bits Error ... Lower flash.\r\n")));
        if ((ulStatus & 0x00200000) == 0x00200000)
            DEBUGMSG(1, (TEXT("\r\nClear Lock Bits Error ... Upper flash.\r\n")));

        if (ulStatus != STATUS_READY_MASK)
        {
            SetKMode(bLastMode);
            return(FALSE);
        }
#endif

        ++blockID;
    }

    SetKMode(bLastMode);
    return(TRUE);
}


/*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Function:       FMD_ReadSector()

Description:    Reads the requested sector data and/or sector metadata from the
                Flash media.

Notes:          Notice that although each byte of a NOR Flash block is individually
                addressable, the media is still logically broken up into sectors.  
                Thus, for each sector request, we must determine where this data
                resides in the respective Flash block (see note above).

                By default, the NOR Flash is configured in READ ARRAY MODE so there
                is no need to set any control lines to access the media.  The data
                can just be read directly from the media (like RAM).
                

⌨️ 快捷键说明

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