📄 amd.cpp
字号:
//**********************************************************************
//
// Filename: amd.cpp
//
// Description: FLASH Media Driver Interface for AMD StrataFlash Chip
//
// THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF
// ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO
// THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
// PARTICULAR PURPOSE.
//
// Use of this source code is subject to the terms of the Cirrus 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
// EULA.RTF on your install media.
//
// Copyright(c) Cirrus Logic Corporation 2005, All Rights Reserved
//
//**********************************************************************
#include <fmd.h>
#include <ceddk.h>
#ifdef READ_FROM_REGISTRY
#include <ddkreg.h>
#endif // READ_FROM_REGISTRY.
#include <hwdefs.h>
#include "strata.h"
/*----------------------------------------------------------------------------------------------------*/
static BOOL g_bPairedFlash = TRUE; // Indicates whether or not two flash parts are paired to create a 32-bit data interface.
static BOOL g_bXIPMode = FALSE;
static SysRegionInfo_t g_FlashRegion[IntelRegionNUM];
/*---------------------------------------------------------------------------------------------------*/
BOOLEAN InitializeFlash(volatile ULONG * pChipBaseAddress, ULONG ChipFlashLength,volatile ULONG * pBaseAddress, ULONG FlashLength);
DWORD DoBufferedWrite(volatile ULONG ulBlockAddress,
volatile SECTOR_ADDR physicalSectorAddr,
PUCHAR pBuffer,
USHORT NumWriteBytes);
BOOL GetEraseFlashSectorIndex(ULONG dwStartAddr)
{
DWORD i,j,dwEraseLen;
dwEraseLen = dwStartAddr;
if(dwEraseLen==0)
{
return TRUE;
}
for(i = 0; i <g_FMDInfo.Geometry.NumEraseBlocks; i++)
{
for(j = 0; j< g_FlashRegion[i].blocks; j++)
{
if(dwEraseLen > g_FlashRegion[i].block_size)
{
dwEraseLen = dwEraseLen - g_FlashRegion[i].block_size;
}
else if(dwEraseLen ==g_FlashRegion[i].block_size)
{
g_FMDInfo.gdwCurEraseRegion = i;
g_FMDInfo.gdwCurEraseBlock = j;
//RETAILMSG(ZONE_TEST, (TEXT(" = block_size =%x.i=%x,j=%x\r\n"),g_FlashRegion[i].block_size,i,j));
return TRUE;
}
else
{
g_FMDInfo.gdwCurEraseRegion = i;
g_FMDInfo.gdwCurEraseBlock = j;
RETAILMSG(ZONE_TEST, (TEXT(" < block_size =%x.i=%x,j=%x\r\n"),g_FlashRegion[i].block_size,i,j));
return FALSE;
}
}
}
return FALSE;
}
/*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
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 ULONG * pBaseAddress = NULL;
volatile ULONG * pChipBaseAddress = NULL;
ULONG FlashLength = 0,ChipFlashLength=0;
BOOL bLastMode;
RETAILMSG(1, (TEXT("INFO: FMD_Init. amd like\r\n")));
#ifdef READ_FROM_REGISTRY
if (lpActiveReg != NULL)
{
DDKWINDOWINFO dwi;
HKEY hConfig;
// Get flash information from the registry.
hConfig = OpenDeviceKey((LPCTSTR)lpActiveReg);
if (hConfig == NULL)
{
RETAILMSG(ZONE_TEST, (TEXT("ERROR: FMD_Init: OpenDeviceKey failed.\r\n")));
return(NULL);
}
dwi.cbSize = sizeof(dwi);
if (DDKReg_GetWindowInfo(hConfig, &dwi) != ERROR_SUCCESS)
{
RETAILMSG(ZONE_TEST, (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 ULONG *)(dwi.memWindows[1].dwBase);
FlashLength = (ULONG)(dwi.memWindows[1].dwLen);
pChipBaseAddress = (volatile ULONG *)(dwi.memWindows[0].dwBase);
ChipFlashLength = (ULONG)(dwi.memWindows[0].dwLen);
}
}
#else
// Get flash base address and length from caller.
if (!pRegIn || !pRegIn->MemBase.Num || !pRegIn->MemLen.Num)
{
RETAILMSG(ZONE_TEST, (TEXT("ERROR: FMD_Init: invalid flash memory base and/or length specified by caller.\r\n")));
return(NULL);
}
else
{
pBaseAddress = (volatile ULONG *)pRegIn->MemBase.Reg[1];
FlashLength = pRegIn->MemLen.Reg[1];
pChipBaseAddress = (volatile ULONG *)pRegIn->MemBase.Reg[0];
ChipFlashLength = pRegIn->MemLen.Reg[0];
}
#endif // READ_FROM_REGISTRY.
// Run in kernel mode.
bLastMode = SetKMode(TRUE);
//
//resetting the smc interface
//
// SetSMCInterface();
// Identify the flash part and collect device information.
//
if (!InitializeFlash(pChipBaseAddress,ChipFlashLength,pBaseAddress, FlashLength))
{
RETAILMSG(ZONE_TEST, (TEXT("ERROR: FMD_Init: Failed to initialize flash.\r\n")));
SetKMode(bLastMode);
return(NULL);
}
RETAILMSG(ZONE_TEST, (TEXT("INFO: FMD_Init: Flash Address=0x%x Length=0x%x.\r\n"), (ULONG)pBaseAddress, FlashLength));
SetKMode(bLastMode);
return((PVOID)pBaseAddress);
}
/*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
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).
Returns: Boolean indicating success.
------------------------------------------------------------------------------*/
BOOL FMD_ReadSector(SECTOR_ADDR startSectorAddr, LPBYTE pSectorBuff, PSectorInfo pSectorInfoBuff, DWORD dwNumSectors)
{
volatile SECTOR_ADDR physicalSectorAddr = 0;
volatile ULONG ulBlockAddress = 0;
BLOCK_ID blockID = 0;
DWORD i = 0;
unsigned int j;
BOOL bLastMode;
RETAILMSG(ZONE_TEST, (TEXT("FMD_ReadSector: (startAdd =%x num=%x) sectot_addr=%x,sector_info %x.\r\n"),startSectorAddr,dwNumSectors,
pSectorBuff,pSectorInfoBuff));
//----- 1. Check the input parameters -----
// NOTE: The FAL insures that the starting sector address is in the allowable range.
if((dwNumSectors == 0) || ((pSectorBuff == NULL) && (pSectorInfoBuff == NULL)))
{
return(FALSE);
}
//----- 2. Process the read request(s)... -----
bLastMode = SetKMode(TRUE);
for(i = startSectorAddr ; i < (startSectorAddr + dwNumSectors) ; i++)
{
//----- Determine the block this physical sector resides in -----
blockID = (i / g_FMDInfo.SectorsPerBlock);
//----- Compute the physical address for the requested -----
// Note we do this differently based on whether the caller wants us to read the sector information structure as well. Since we're
// dealing with a NOR flash which is XIP-able, one might want to use this function to read from an XIP region (i.e., no sector information
// structures in flash).
//
if (!g_bXIPMode)
physicalSectorAddr = g_FMDInfo.BaseAddress + i*(SECTOR_SIZE + sizeof(SectorInfo)) + (blockID * g_FMDInfo.UnusedBytesPerBlock);
else
physicalSectorAddr = g_FMDInfo.BaseAddress + (i*SECTOR_SIZE);
ulBlockAddress = (ULONG)(physicalSectorAddr - (physicalSectorAddr % g_FMDInfo.BlockSize));
if(GetEraseFlashSectorIndex(ulBlockAddress-g_FMDInfo.ChipBaseAddress)==FALSE)
{
RETAILMSG(ZONE_TEST, (TEXT("ERASE Block error,not aligned\r\n")));
return FALSE;
}
RETAILMSG(ZONE_TEST, (TEXT("blockID=%x,physicalSectorAddr=%x,g_bXIPMode=%x,ulBlockAddress=%x,g_FMDInfo.gdwCurEraseRegion=%x,gdwCurEraseBlock=%x.\r\n"),
blockID,physicalSectorAddr,g_bXIPMode,ulBlockAddress,g_FMDInfo.gdwCurEraseRegion,g_FMDInfo.gdwCurEraseBlock));
if(g_FlashRegion[g_FMDInfo.gdwCurEraseRegion].block_size==g_FMDInfo.BlockSize)
{
RETAILMSG(ZONE_TEST, (TEXT("1 \r\n")));
WRITE_COMMAND(g_FMDInfo.ChipBaseAddress, AMD_RESET);
RETAILMSG(ZONE_TEST, (TEXT("2 \r\n")));
//----- Read the necessary sector data -----
if(pSectorBuff)
{
memcpy(pSectorBuff, (CONST PVOID)physicalSectorAddr, SECTOR_SIZE);
pSectorBuff += SECTOR_SIZE;
}
RETAILMSG(ZONE_TEST, (TEXT("3 \r\n")));
//----- Read the necessary SectorInfo data (metadata) -----
if(!g_bXIPMode && pSectorInfoBuff)
{
// The metadata bytes are read directly into the SectorInfo structure...
memcpy(pSectorInfoBuff, (CONST PVOID)(physicalSectorAddr+SECTOR_SIZE), sizeof(SectorInfo));
pSectorInfoBuff += sizeof(SectorInfo);
}
RETAILMSG(ZONE_TEST, (TEXT("read sector %x\r\n"),physicalSectorAddr,ulBlockAddress));
}
else
{
for(j = g_FMDInfo.gdwCurEraseBlock; j< g_FlashRegion[g_FMDInfo.gdwCurEraseRegion].blocks; j++)
{
WRITE_COMMAND(g_FMDInfo.ChipBaseAddress, AMD_RESET);
ulBlockAddress = ulBlockAddress +g_FlashRegion[g_FMDInfo.gdwCurEraseRegion].block_size;
}
//----- Read the necessary sector data -----
if(pSectorBuff)
{
memcpy(pSectorBuff, (CONST PVOID)physicalSectorAddr, SECTOR_SIZE);
pSectorBuff += SECTOR_SIZE;
}
//----- Read the necessary SectorInfo data (metadata) -----
if(!g_bXIPMode && pSectorInfoBuff)
{
// The metadata bytes are read directly into the SectorInfo structure...
memcpy(pSectorInfoBuff, (CONST PVOID)(physicalSectorAddr+SECTOR_SIZE), sizeof(SectorInfo));
pSectorInfoBuff += sizeof(SectorInfo);
}
}
}
SetKMode(bLastMode);
RETAILMSG(ZONE_TEST, (TEXT("FMD_ReadSector: over.\r\n") ));
return(TRUE);
}
/*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Function: FMD_WriteSector()
Description: Writes the requested sector data and/or sector metadata to 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 to put the
data in each respective Flash block (see note above).
By default, the NOR Flash is configured in READ ARRAY MODE we need
to set some control lines to prepare for the WRITE operation.
Returns: Boolean indicating success.
------------------------------------------------------------------------------*/
BOOL FMD_WriteSector(SECTOR_ADDR startSectorAddr, LPBYTE pSectorBuff, PSectorInfo pSectorInfoBuff, DWORD dwNumSectors)
{
volatile SECTOR_ADDR physicalSectorAddr = 0;
BLOCK_ID blockID = 0;
DWORD i = 0;
DWORD j = 0;
DWORD k = 0;
volatile ULONG ulBlockAddress = 0;
BOOL bLastMode;
DWORD dwBusWidth = g_bPairedFlash ? sizeof(ULONG) : sizeof(USHORT);
LPBYTE pBuffer = pSectorBuff;
USHORT usBufferSize = g_bPairedFlash ? (1 << g_FMDInfo.Geometry.WriteBuffSize) * 2 : (1 << g_FMDInfo.Geometry.WriteBuffSize);
BOOL fRet = FALSE;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -