📄 fmd.cpp
字号:
//
// 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.
//
#include <fmd.h>
#include <s3c2410x_nand.h>
#include "nand.h"
#include <ethdbg.h>
#include <kitl.h>
#define NAND_BASE 0xB0E00000
static volatile S3C2410X_NAND_REG *s2410NAND = (S3C2410X_NAND_REG *)NAND_BASE;
//#ifdef UNDER_BOOTLOADER
// Assume there will be no more than 16 regions
#define MAX_REGIONS 16
static FlashRegion g_pRegionTable[MAX_REGIONS];
//#else
// Dynamically allocate this under the OS
//static PFlashRegion g_pRegionTable;
//#endif
static DWORD g_dwNumRegions;
static FlashInfo g_flashInfo;
// TODO: Make sector size generic
static BYTE g_pFLSBuffer[NAND_PAGE_SIZE];
#define DEFAULT_COMPACTION_BLOCKS 4
extern "C" void RdPage512(unsigned char *bufPt);
extern "C" void RdPage512Unalign(unsigned char *bufPt);
extern "C" void WrPage512(unsigned char *bufPt);
extern "C" void WrPage512Unalign(unsigned char *bufPt);
extern "C" void WrPageInfo(PBYTE pBuff);
extern "C" void RdPageInfo(PBYTE pBuff);
static BOOL DefineLayout();
static BOOL ReadBlock (DWORD dwBlock, LPBYTE pbBlock, PSectorInfo pSectorInfoTable);
static BOOL WriteBlock (DWORD dwBlock, LPBYTE pbBlock, PSectorInfo pSectorInfoTable);
/*
@func DWORD | ReadFlashID | Reads the flash manufacturer and device codes.
@rdesc Manufacturer and device codes.
@comm
@xref
*/
static DWORD ReadFlashID(void)
{
BYTE Mfg, Dev;
NF_CMD(CMD_READID); // Send flash ID read command.
NF_ADDR(0); //
NF_WAITRB(); // Wait for flash to complete command.
Mfg = NF_RDDATA(); //
Dev = NF_RDDATA(); //
//RETAILMSG(1,(TEXT("FMD: ReadID (Mfg=%x, Dev=%x)\r\n"), Mfg, Dev));
return ((DWORD)Mfg*0x100+Dev);
}
/*
@func PVOID | FMD_Init | Initializes the Smart Media NAND flash controller.
@rdesc Pointer to S3C2410 NAND controller registers.
@comm
@xref
*/
PVOID FMD_Init(LPCTSTR lpActiveReg, PPCI_REG_INFO pRegIn, PPCI_REG_INFO pRegOut)
{
// Caller should have specified NAND controller address.
//
if (pRegIn && pRegIn->MemBase.Num && pRegIn->MemBase.Reg[0])
s2410NAND = (S3C2410X_NAND_REG *)(pRegIn->MemBase.Reg[0]);
else
s2410NAND = (S3C2410X_NAND_REG *)NAND_BASE;
// Set up initial flash controller configuration.
//
s2410NAND->NFCONF = (1 << 15) | /* Enable/Disable */
(1 << 14) | /* Page Size : 512Bytes */
(1 << 13) | /* 4 Step Address */
(1 << 12) | /* Initialize ECC */
(1 << 11) | /* nFCE control nFCE = HIGH */
(TACLS << 8) | /* CLE & ALE = HCLK * (TACLS + 1) */
(TWRPH0 << 4) | /* TWRPH0 = HCLK * (TWRPH0 + 1) */
(TWRPH1 << 0); /* TWRPH1 = HCLK * (TWRPH1 + 1) */
NF_nFCE_L(); // Select the flash chip.
NF_CMD(CMD_RESET); // Send reset command.
NF_WAITRB(); // Wait for flash to complete command.
// Get manufacturer and device codes.
if (ReadFlashID() != 0xEC76)
{
return(NULL);
}
NF_nFCE_H(); // Deselect the flash chip.
DefineLayout();
return((PVOID)s2410NAND);
}
/*
@func BOOL | FMD_ReadSector | Reads the specified sector(s) from NAND flash.
@rdesc TRUE = Success, FALSE = Failure.
@comm
@xref
*/
BOOL FMD_ReadSector(SECTOR_ADDR startSectorAddr, LPBYTE pSectorBuff, PSectorInfo pSectorInfoBuff, DWORD dwNumSectors)
{
ULONG SectorAddr = (ULONG)startSectorAddr;
NF_RSTECC(); // Initialize ECC.
NF_nFCE_L(); // Select the flash chip.
NF_CMD(CMD_RESET); // Send reset command.
while (dwNumSectors--)
{
ULONG blockPage = (((SectorAddr / NAND_PAGE_CNT) * NAND_PAGE_CNT) | (SectorAddr % NAND_PAGE_CNT));
NF_WAITRB(); // Wait for flash to complete command.
if (pSectorBuff)
{
NF_CMD(CMD_READ); // Send read command.
NF_ADDR(0); // Column = 0.
NF_ADDR(blockPage & 0xff); // Page address.
NF_ADDR((blockPage >> 8) & 0xff);
NF_ADDR((blockPage >> 16) & 0xff);
NF_WAITRB(); // Wait for command to complete.
// Handle unaligned buffer pointer
if( ((DWORD) pSectorBuff) & 0x3)
{
RdPage512Unalign (pSectorBuff);
}
else
{
RdPage512(pSectorBuff); // Read page/sector data.
}
NF_RDDATA(); // Read/clear status.
NF_RDDATA(); //
}
if (pSectorInfoBuff)
{
NF_CMD(CMD_READ2); // Send read confirm command.
NF_ADDR(0); // Column = 0.
NF_ADDR(blockPage & 0xff); // Page address.
NF_ADDR((blockPage >> 8) & 0xff);
NF_ADDR((blockPage >> 16) & 0xff);
NF_WAITRB(); // Wait for command to complete.
RdPageInfo((PBYTE)pSectorInfoBuff); // Read page/sector information.
NF_RDDATA(); // Read/clear status.
NF_RDDATA(); //
pSectorInfoBuff++;
}
++SectorAddr;
pSectorBuff += NAND_PAGE_SIZE;
}
NF_nFCE_H(); // Deselect the flash chip.
return(TRUE);
}
/*
@func BOOL | FMD_EraseBlock | Erases the specified flash block.
@rdesc TRUE = Success, FALSE = Failure.
@comm
@xref
*/
BOOL FMD_EraseBlock(BLOCK_ID blockID)
{
BYTE Status;
ULONG blockPage = (blockID * NAND_PAGE_CNT); // Convert block address to page address.
NF_nFCE_L(); // Select the flash chip.
NF_CMD(CMD_ERASE); // Send block erase command.
NF_ADDR(blockPage & 0xff); /* The mark of bad block is in 0 page */
NF_ADDR((blockPage >> 8) & 0xff); /* For block number A[24:17] */
NF_ADDR((blockPage >> 16) & 0xff); /* For block number A[25] */
NF_CMD(CMD_ERASE2); // Send block erase confirm command.
NF_WAITRB(); // Wait for flash to complete command.
// go into status mode
NF_CMD(CMD_STATUS);
Status = NF_RDDATA(); // Read command status.
NF_nFCE_H(); // Deselect the flash chip.
return((Status & 1) ? FALSE : TRUE);
}
/*
@func BOOL | FMD_WriteSector | Writes the specified data to the specified NAND flash sector/page.
@rdesc TRUE = Success, FALSE = Failure.
@comm
@xref
*/
BOOL FMD_WriteSector(SECTOR_ADDR startSectorAddr, LPBYTE pSectorBuff, PSectorInfo pSectorInfoBuff, DWORD dwNumSectors)
{
BYTE Status;
ULONG SectorAddr = (ULONG)startSectorAddr;
if (!pSectorBuff && !pSectorInfoBuff)
return(FALSE);
NF_RSTECC(); // Initialize ECC.
NF_nFCE_L(); // Select the flash chip.
while (dwNumSectors--)
{
ULONG blockPage = (((SectorAddr / NAND_PAGE_CNT) * NAND_PAGE_CNT) | (SectorAddr % NAND_PAGE_CNT));
if (!pSectorBuff)
{
// If we are asked just to write the SectorInfo, we will do that separately
NF_CMD(CMD_READ2); // Send read command.
NF_CMD(CMD_WRITE); // Send write command.
NF_ADDR(0); // Column = 0.
NF_ADDR(blockPage & 0xff); // Page address.
NF_ADDR((blockPage >> 8) & 0xff);
NF_ADDR((blockPage >> 16) & 0xff);
WrPageInfo((PBYTE)pSectorInfoBuff);
pSectorInfoBuff++;
}
else
{
NF_CMD(CMD_READ); // Send read command.
NF_CMD(CMD_WRITE); // Send write command.
NF_ADDR(0); // Column = 0.
NF_ADDR(blockPage & 0xff); // Page address.
NF_ADDR((blockPage >> 8) & 0xff);
NF_ADDR((blockPage >> 16) & 0xff);
// Special case to handle un-aligned buffer pointer.
if( ((DWORD) pSectorBuff) & 0x3)
{
WrPage512Unalign (pSectorBuff);
}
else
{
WrPage512(pSectorBuff); // Write page/sector data.
}
// Write the SectorInfo data to the media.
//
if(pSectorInfoBuff)
{
WrPageInfo((PBYTE)pSectorInfoBuff);
pSectorInfoBuff++;
}
else // Make sure we advance the Flash's write pointer (even though we aren't writing the SectorInfo data)
{
BYTE TempInfo[] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
WrPageInfo(TempInfo);
}
pSectorBuff += NAND_PAGE_SIZE;
}
NF_CMD(CMD_WRITE2); // Send write confirm command.
NF_WAITRB(); // Wait for command to complete.
// go into status mode
NF_CMD(CMD_STATUS);
Status = NF_RDDATA();
if (Status & 1)
{
NF_nFCE_H(); // Deselect the flash chip.
DEBUGMSG(TRUE, (TEXT("ERROR: FMD_WriteSector: failed sector write.\r\n")));
return(FALSE);
}
++SectorAddr;
}
NF_nFCE_H(); // Deselect the flash chip.
return(TRUE);
}
VOID FMD_PowerUp(VOID)
{
}
VOID FMD_PowerDown(VOID)
{
}
BOOL FMD_OEMIoControl(DWORD dwIoControlCode, PBYTE pInBuf, DWORD nInBufSize, PBYTE pOutBuf, DWORD nOutBufSize, PDWORD pBytesReturned)
{
switch(dwIoControlCode)
{
case IOCTL_FMD_GET_INTERFACE:
{
if (!pOutBuf || nOutBufSize < sizeof(FMDInterface))
{
DEBUGMSG(1, (TEXT("FMD_OEMIoControl: IOCTL_FMD_GET_INTERFACE bad parameter(s).\r\n")));
return(FALSE);
}
PFMDInterface pInterface = (PFMDInterface)pOutBuf;
pInterface->cbSize = sizeof(FMDInterface);
pInterface->pInit = FMD_Init;
pInterface->pDeInit = FMD_Deinit;
pInterface->pGetInfo = FMD_GetInfo;
pInterface->pGetInfoEx = FMD_GetInfoEx;
pInterface->pGetBlockStatus = FMD_GetBlockStatus;
pInterface->pSetBlockStatus = FMD_SetBlockStatus;
pInterface->pReadSector = FMD_ReadSector;
pInterface->pWriteSector = FMD_WriteSector;
pInterface->pEraseBlock = FMD_EraseBlock;
pInterface->pPowerUp = FMD_PowerUp;
pInterface->pPowerDown = FMD_PowerDown;
pInterface->pGetPhysSectorAddr = NULL;
break;
}
#if 0
case IOCTL_FMD_GET_REGION_TABLE:
{
// Get the reserved table.
if (!pOutBuf)
{
// If no buffer is provided, return the required size.
if (pBytesReturned)
*pBytesReturned = g_dwNumRegions * sizeof(FlashRegion);
return TRUE;
}
else
{
DWORD dwTableSize = g_dwNumRegions * sizeof(FlashRegion);
if (nOutBufSize < dwTableSize)
{
if (pBytesReturned)
*pBytesReturned = 0;
return FALSE;
}
else
{
if (pBytesReturned)
*pBytesReturned = dwTableSize;
if (g_pRegionTable)
memcpy (pOutBuf, g_pRegionTable, dwTableSize);
return TRUE;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -