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

📄 fmd.cpp

📁 2KB大页面NAND Flash在WinCE5.0操作系统中的驱动程序。
💻 CPP
📖 第 1 页 / 共 4 页
字号:
//
// 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 <s3c2440x.h>
#include "nand.h"
//#include <ethdbg.h>
//#if K9F1G08
//#define ADVANC	1
//#else
//#define ADVANC	0
//#endif
#define NAND_BASE 0xB0E00000
#define CLKPWR_BASE 0xB0C00000
static volatile S3C2440X_NAND_REG *s2440NAND = (S3C2440X_NAND_REG *)NAND_BASE;
static volatile S3C2440X_CLKPWR_REG *s2440CLKPWR = (S3C2440X_CLKPWR_REG *)CLKPWR_BASE;
//#if K9F1G08
extern "C" void RdPage2048(unsigned char *bufPt,PULONG pReg);
extern "C" void RdPage2048Unalign(unsigned char *bufPt,PULONG pReg);
extern "C" void WrPage2048(unsigned char *bufPt,PULONG pReg); 
extern "C" void WrPage2048Unalign(unsigned char *bufPt,PULONG pReg); 
//#endif
extern "C" void RdPage512(unsigned char *bufPt,PULONG pReg);
extern "C" void RdPage512Unalign(unsigned char *bufPt,PULONG pReg);
extern "C" void WrPage512(unsigned char *bufPt,PULONG pReg); 
extern "C" void WrPage512Unalign(unsigned char *bufPt,PULONG pReg); 
extern "C" void WrPageInfo(PBYTE pBuff,PULONG pReg);
extern "C" void RdPageInfo(PBYTE pBuff,PULONG pReg);
BOOL  FMD_ReadSector1208 (SECTOR_ADDR startSectorAddr, LPBYTE pSectorBuff, PSectorInfo pSectorInfoBuff, DWORD dwNumSectors);
BOOL  FMD_WriteSector1208(SECTOR_ADDR startSectorAddr, LPBYTE pSectorBuff, PSectorInfo pSectorInfoBuff, DWORD dwNumSectors);

BOOL  FMD_EraseBlock1208(BLOCK_ID blockID);
static BOOL IsBlockBad1208(BLOCK_ID blockID);
static BOOL MarkBlockBad1208(BLOCK_ID blockID);
#if (K9F1G08_SUPPORT||K9F2G08_SUPPORT||K9F4G08_SUPPORT||K9F8G08_SUPPORT)
BOOL  FMD_ReadSector1G08(SECTOR_ADDR startSectorAddr, LPBYTE pSectorBuff, PSectorInfo pSectorInfoBuff, DWORD dwNumSectors);
BOOL  FMD_WriteSector1G08(SECTOR_ADDR startSectorAddr, LPBYTE pSectorBuff, PSectorInfo pSectorInfoBuff, DWORD dwNumSectors);

BOOL  FMD_EraseBlock1G08(BLOCK_ID blockID);
static BOOL IsBlockBad1G08(BLOCK_ID blockID);
static BOOL MarkBlockBad1G08(BLOCK_ID blockID);
#endif
#if K9K2G16_SUPPORT
BOOL  FMD_ReadSector2G16(SECTOR_ADDR startSectorAddr, LPBYTE pSectorBuff, PSectorInfo pSectorInfoBuff, DWORD dwNumSectors);
BOOL  FMD_WriteSector2G16(SECTOR_ADDR startSectorAddr, LPBYTE pSectorBuff, PSectorInfo pSectorInfoBuff, DWORD dwNumSectors);

BOOL  FMD_EraseBlock2G16(BLOCK_ID blockID);
static BOOL IsBlockBad2G16(BLOCK_ID blockID);
static BOOL MarkBlockBad2G16(BLOCK_ID blockID);
#endif
//
typedef BOOL  (*PFN_READSECTOR)(SECTOR_ADDR startSectorAddr, LPBYTE pSectorBuff, PSectorInfo pSectorInfoBuff, DWORD dwNumSectors);
typedef BOOL  (*PFN_WRITESECTOR)(SECTOR_ADDR startSectorAddr, LPBYTE pSectorBuff, PSectorInfo pSectorInfoBuff, DWORD dwNumSectors);
typedef BOOL  (*PFN_ERASEBLOCK)(BLOCK_ID blockID);
typedef BOOL  (*PFN_ISBLOCKBAD)(BLOCK_ID blockID);
typedef BOOL  (*PFN_MARKBLOCKBAD)(BLOCK_ID blockID);
typedef struct {
	DWORD FlashID;
	BOOL	is16BitSize;
	FlashInfo tFlashInfo;
	PFN_READSECTOR pReadSector;
    PFN_WRITESECTOR pWriteSector;
    PFN_ERASEBLOCK pEraseBlock;
	PFN_ISBLOCKBAD pIsBlockBad;
	PFN_MARKBLOCKBAD pMarkBlockBad;
}NAND_Flash_Chip_t;
NAND_Flash_Chip_t g_NandFlash_Chip_set[]={
	{0xEC76,/*K9S1208*/
	FALSE,
		{NAND,4096,512*32,32,512},
		FMD_ReadSector1208,
		FMD_WriteSector1208,
		FMD_EraseBlock1208,
		IsBlockBad1208,
		MarkBlockBad1208
	},
#if K9F1G08_SUPPORT
	{0xECF1,/*K9F1G08U0M*/
	FALSE,
		{NAND,1024,2048*64,64,2048},
		FMD_ReadSector1G08,
		FMD_WriteSector1G08,
		FMD_EraseBlock1G08,
		IsBlockBad1G08,
		MarkBlockBad1G08
	},
	{0xECA1,/*K9F1G08Q0M*/
	FALSE,
		{NAND,1024,2048*64,64,2048},
		FMD_ReadSector1G08,
		FMD_WriteSector1G08,
		FMD_EraseBlock1G08,
		IsBlockBad1G08,
		MarkBlockBad1G08
	},
#endif
#if K9F2G08_SUPPORT
	{0xECDA,/*K9F2G08U0M*/
	FALSE,
		{NAND,2048,2048*64,64,2048},
		FMD_ReadSector1G08,
		FMD_WriteSector1G08,
		FMD_EraseBlock1G08,
		IsBlockBad1G08,
		MarkBlockBad1G08
	},
	{0xECAA,/*K9F2G08Q0M*/
	FALSE,
		{NAND,2048,2048*64,64,2048},
		FMD_ReadSector1G08,
		FMD_WriteSector1G08,
		FMD_EraseBlock1G08,
		IsBlockBad1G08,
		MarkBlockBad1G08
	},
#endif
#if K9F4G08_SUPPORT
	{0xECDC,/*K9F4G08U0M/K9K8G08U1M*/
	FALSE,
		{NAND,4096,2048*64,64,2048},
		FMD_ReadSector1G08,
		FMD_WriteSector1G08,
		FMD_EraseBlock1G08,
		IsBlockBad1G08,
		MarkBlockBad1G08
	},
#endif
#if K9F8G08_SUPPORT
	{0xECD3,/*K9F8G08U0M/K9K8G08U0M*/
	FALSE,
		{NAND,8192,2048*64,64,2048},
		FMD_ReadSector1G08,
		FMD_WriteSector1G08,
		FMD_EraseBlock1G08,
		IsBlockBad1G08,
		MarkBlockBad1G08
	},
#endif
#if K9K2G16_SUPPORT
	{0xECCA,/*K9K2G16U0M*/
	TRUE,
		{NAND,2048,2048*64,64,2048},
		FMD_ReadSector2G16,
		FMD_WriteSector2G16,
		FMD_EraseBlock2G16,
		IsBlockBad2G16,
		MarkBlockBad2G16
	},
#endif
	{
		0,0,{NAND,0,0,0,0},NULL,NULL,NULL,NULL,NULL
	}
};
NAND_Flash_Chip_t *g_pNandFlashChip=g_NandFlash_Chip_set;
#define FLASH_CHIP_COUNTS	sizeof(g_NandFlash_Chip_set)/sizeof(NAND_Flash_Chip_t)
WORD GetLog2PagesPerBlock(WORD pages_per_block)
{
	WORD i;
	for(i=0;;i++)
	{
		if(pages_per_block==1)
			break;
		pages_per_block=pages_per_block>>1;
	}
	return i;
}
DWORD GetNumBlocks()
{
	if(g_pNandFlashChip)
	return (g_pNandFlashChip->tFlashInfo.dwNumBlocks);
	return 0;
}
WORD GetSectorsPerBlock()
{
	if(g_pNandFlashChip)
	return (g_pNandFlashChip->tFlashInfo.wSectorsPerBlock);            /* Each Block has 32 Pages      */
	return 0;
}
WORD GetBytesPerSector()
{
	if(g_pNandFlashChip)
	return (g_pNandFlashChip->tFlashInfo.wDataBytesPerSector);
	return 0;
}

void NF_Reset()
{
	int i;

    NF_nFCE_L();

	NF_CLEAR_RB();
    NF_CMD(CMD_RESET);
	for(i=0;i<10;i++);  //tWB = 100ns. //??????
	NF_DETECT_RB();
    NF_nFCE_H();
}

/*
    @func   DWORD | ReadFlashID | Reads the flash manufacturer and device codes.
    @rdesc  Manufacturer and device codes.
    @comm    
    @xref   
*/

static DWORD ReadFlashID(void)
{
    BYTE Mfg, Dev;
	int i;
	NF_nFCE_L();
//    NF_CLEAR_RB();
    NF_CMD(CMD_READID);        // Send flash ID read command.
    NF_ADDR(0);                //
    for (i=0; i<1000; i++);
//    NF_DETECT_RB();	 // Wait tR(max 12us)
    Mfg    = NF_RDDATA();    // 
    Dev    = NF_RDDATA();    // 
	NF_nFCE_H();
#ifdef BOOT_LOADER
    EdbgOutputDebugString("FMD: ReadID (Mfg=%x, Dev=%x)\r\n", Mfg, Dev);
#else
    RETAILMSG(1,(TEXT("FMD: ReadID (Mfg=%x, Dev=%x)\r\n"), Mfg, Dev));
#endif
    return ((DWORD)Mfg*0x100+Dev);
}
#if K9K2G16_SUPPORT
//FIXME:this functin not test for no hardware.
static DWORD ReadFlashID16(void)
{
    BYTE Mfg, Dev;
	int i;
	NF_nFCE_L();
//    NF_CLEAR_RB();
    NF_CMD(CMD_READID);        // Send flash ID read command.
//     for (i=0; i<3; i++);
    NF_ADDR(0);                //
    for (i=0; i<1000; i++);
//    NF_DETECT_RB();	 // Wait tR(max 12us)
     Dev   = (BYTE)NF_RDDATA16();    // 
    Mfg    = (BYTE)NF_RDDATA16();    // 
	NF_nFCE_H();
    RETAILMSG(1,(TEXT("FMD: ReadID (Mfg=%x, Dev=%x)\r\n"), Mfg, Dev));

    return ((DWORD)Mfg*0x100+Dev);
}
#endif
/*
    @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.
    //
	int i;
	DWORD FlashId;
    BOOL bLastMode = SetKMode(TRUE);

	// Enable the clock to NAND controller
	s2440CLKPWR->CLKCON |= (1<<4);
    // Set up initial flash controller configuration.
    //
    s2440NAND->NFCONF =  (TACLS  <<  12) | /* CLE & ALE = HCLK * (TACLS  + 1)   */
                          (TWRPH0 <<  8) | /* TWRPH0    = HCLK * (TWRPH0 + 1)   */
                          (TWRPH1 <<  4) ;   /* TWRPH1    = HCLK * (TWRPH1 + 1)   */
    s2440NAND->NFCONT = (0<<13)|(0<<12)|(0<<10)|(0<<9)|(0<<8)|(0<<6)|(0<<5)|(1<<4)|(1<<1)|(1<<0);
    s2440NAND->NFSTAT = 0;
	NF_Reset();
//    NF_nFCE_L();            // Select the flash chip.
//    NF_CLEAR_RB();
//    NF_CMD(CMD_RESET);        // Send reset command.
    //NF_WAITRB();            // Wait for flash to complete command.
    for(i=0;i<100;i++);  //tWB = 100ns. //??????
//    NF_DETECT_RB();
	FlashId = ReadFlashID();
    // Get manufacturer and device codes.
//    g_pNandFlashChip = g_NandFlash_Chip_set;
    for(i=0;i<FLASH_CHIP_COUNTS;i++)
    {
    	if (FlashId == g_pNandFlashChip->FlashID)
    	{
		break;
    	}
	g_pNandFlashChip++;
    }
//    NF_nFCE_H();            // Deselect the flash chip.
	if(g_pNandFlashChip->FlashID == 0)
	{
		#if K9K2G16_SUPPORT
		s2440NAND->NFCONF|=(1<<0);
		NF_Reset();
		FlashId = ReadFlashID16();///???
		if(FlashId !=0)
		{
			g_pNandFlashChip--;
			for(i=FLASH_CHIP_COUNTS-1;i>=0;i--)
			{
				if (FlashId == g_pNandFlashChip->FlashID)
			    	{
					break;
			    	}
				g_pNandFlashChip--;
			}
		}
		if(i < 0)
		#endif
		g_pNandFlashChip = NULL;
	}
	 s2440NAND->NFCONF|=(g_pNandFlashChip->is16BitSize);
    SetKMode (bLastMode);
#ifdef BOOT_LOADER
    EdbgOutputDebugString("FMD_Init: finished successful\r\n");
#else
	RETAILMSG(1,(TEXT("FMD_Init: finished successful\r\n")));
#endif
    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)
{
    if(g_pNandFlashChip)
		return g_pNandFlashChip->pReadSector(startSectorAddr,pSectorBuff,pSectorInfoBuff,dwNumSectors);
	return FALSE;
}


/*
    @func   BOOL | FMD_EraseBlock | Erases the specified flash block.
    @rdesc  TRUE = Success, FALSE = Failure.
    @comm    
    @xref   
*/
BOOL FMD_EraseBlock(BLOCK_ID blockID)
{
    if(g_pNandFlashChip)
		return g_pNandFlashChip->pEraseBlock(blockID);
	return FALSE;
}


/*
    @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)
{
    if(g_pNandFlashChip)
		return g_pNandFlashChip->pWriteSector(startSectorAddr,pSectorBuff,pSectorInfoBuff,dwNumSectors);
	return FALSE;

⌨️ 快捷键说明

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