📄 fmd.cpp
字号:
//
// 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.
//
#include <fmd.h>
#include <s3c2440a_nand.h>
#include "nand.h"
#include <ethdbg.h>
#define NAND_BASE 0xB0E00000
BOOL NEED_EXT_ADDR = TRUE;
static volatile S3C2440A_NAND_REG *s2440NAND = (S3C2440A_NAND_REG *)NAND_BASE;
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);
/*
@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_WAITRB(); // Wait for flash to complete command.
NF_ADDR(0); //
NF_WAITRB(); // Wait for flash to complete command.
Mfg = NF_RDDATA(); //
Dev = NF_RDDATA(); //
return ((DWORD)(Mfg<<8)+Dev);
}
/*
@func PVOID | FMD_Init | Initializes the Smart Media NAND flash controller.
@rdesc Pointer to S3C2440 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.
//
BOOL bLastMode = SetKMode(TRUE);
if (pRegIn && pRegIn->MemBase.Num && pRegIn->MemBase.Reg[0])
s2440NAND = (S3C2440A_NAND_REG *)(pRegIn->MemBase.Reg[0]);
else
s2440NAND = (S3C2440A_NAND_REG *)NAND_BASE;
// Set up initial flash controller configuration.
//
s2440NAND->NFCONF = (TACLS << 12) | /* CLE & ALE = HCLK * (TACLS + 1) */
(TWRPH0 << 8) | /* TWRPH0 = HCLK * (TWRPH0 + 1) */
(TWRPH1 << 4);
s2440NAND->NFCONT = (0<<13)|(0<<12)|(0<<10)|(0<<9)|(0<<8)|(0<<6)|(0<<5)|(1<<4)|(1<<1)|(1<<0);
s2440NAND->NFSTAT = 0x4;
NF_nFCE_L(); // Select the flash chip.
NF_WAITRB(); // Wait for flash to complete command.
NF_CMD(CMD_RESET); // Send reset command.
NF_WAITRB(); // Wait for flash to complete command.
// Get manufacturer and device codes.
if (ReadFlashID() != 0xEC76)
{
NF_nFCE_H(); // Deselect the flash chip.
SetKMode (bLastMode);
return(NULL);
}
NF_nFCE_H(); // Deselect the flash chip.
SetKMode (bLastMode);
return((PVOID)s2440NAND);
}
/*
@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;
ULONG MECC;
if (!pSectorBuff && !pSectorInfoBuff)
return(FALSE);
BOOL bLastMode = SetKMode(TRUE);
NF_nFCE_L(); // Select the flash chip.
NF_WAITRB(); // Wait for flash to complete command.
NF_CMD(CMD_RESET); // Send reset command.
NF_WAITRB(); // Wait for flash to complete command.
while (dwNumSectors--)
{
ULONG blockPage = (((SectorAddr / NAND_PAGE_CNT) * NAND_PAGE_CNT) | (SectorAddr % NAND_PAGE_CNT));
if (!pSectorBuff)
{
NF_CMD(CMD_READ2); // Send read confirm command.
NF_WAITRB(); // Wait for command to complete.
NF_ADDR(0); // Column = 0.
NF_ADDR(blockPage & 0xff); // Page address.
NF_ADDR((blockPage >> 8) & 0xff);
if (NEED_EXT_ADDR)
NF_ADDR((blockPage >> 16) & 0xff);
NF_WAITRB(); // Wait for command to complete.
RdPageInfo((PBYTE)pSectorInfoBuff); // Read page/sector information.
pSectorInfoBuff++;
}
else
{
NF_CMD(CMD_READ); // Send read command.
NF_WAITRB(); // Wait for command to complete.
NF_ADDR(0); // Column = 0.
NF_ADDR(blockPage & 0xff); // Page address.
NF_ADDR((blockPage >> 8) & 0xff);
if (NEED_EXT_ADDR)
NF_ADDR((blockPage >> 16) & 0xff);
NF_WAITRB(); // Wait for command to complete.
// Handle unaligned buffer pointer
NF_RSTECC();
NF_MECC_UnLock();
if( ((DWORD) pSectorBuff) & 0x3)
{
RdPage512Unalign (pSectorBuff);
}
else
{
RdPage512(pSectorBuff); // Read page/sector data.
}
NF_MECC_Lock();
if (pSectorInfoBuff)
{
RdPageInfo((PBYTE)pSectorInfoBuff); // Read page/sector information.
pSectorInfoBuff ++;
}
else
{
BYTE TempInfo[8];
RdPageInfo(TempInfo); // Read page/sector information.
}
MECC = NF_RDDATA() << 0;
MECC |= NF_RDDATA() << 8;
MECC |= NF_RDDATA() << 16;
MECC |= NF_RDDATA() << 24;
NF_WRMECCD0( ((MECC&0xff00)<<8)|(MECC&0xff) );
NF_WRMECCD1( ((MECC&0xff000000)>>8)|((MECC&0xff0000)>>16) );
if (NF_RDESTST0 & 0x3)
{
RETAILMSG(1,(TEXT("ecc error %x %x \r\n"),NF_RDMECC0(),MECC));
NF_nFCE_H(); // Deselect the flash chip.
SetKMode (bLastMode);
return FALSE;
}
pSectorBuff += NAND_PAGE_SIZE;
}
++SectorAddr;
}
NF_nFCE_H(); // Deselect the flash chip.
SetKMode (bLastMode);
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.
BOOL bLastMode = SetKMode(TRUE);
NF_nFCE_L(); // Select the flash chip.
NF_WAITRB(); // Wait for flash to complete command.
NF_CMD(CMD_RESET); // Send reset command.
NF_WAITRB(); // Wait for flash to complete command.
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] */
if (NEED_EXT_ADDR)
NF_ADDR((blockPage >> 16) & 0xff); /* For block number A[25] */
NF_WAITRB(); // Wait for flash to complete command.
NF_CMD(CMD_ERASE2); // Send block erase confirm command.
NF_WAITRB(); // Wait for flash to complete command.
do
{
NF_CMD(CMD_STATUS);
Status = NF_RDDATA(); // Read command status.
}while(!(Status & STATUS_READY));
NF_nFCE_H(); // Deselect the flash chip.
SetKMode (bLastMode);
return((Status & STATUS_ERROR) ? 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;
ULONG MECC;
if (!pSectorBuff && !pSectorInfoBuff)
return(FALSE);
BOOL bLastMode = SetKMode(TRUE);
NF_nFCE_L(); // Select the flash chip.
NF_WAITRB(); // Wait for flash to complete command.
NF_CMD(CMD_RESET); // Send reset command.
NF_WAITRB(); // Wait for flash to complete command.
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_WAITRB(); // Wait for flash to complete command.
NF_CMD(CMD_WRITE); // Send write command.
NF_ADDR(0); // Column = 0.
NF_ADDR(blockPage & 0xff); // Page address.
NF_ADDR((blockPage >> 8) & 0xff);
if (NEED_EXT_ADDR)
NF_ADDR((blockPage >> 16) & 0xff);
NF_WAITRB(); // Wait for flash to complete command.
WrPageInfo((PBYTE)pSectorInfoBuff);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -