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

📄 fmd.cpp

📁 2KB大页面NAND Flash在WinCE5.0操作系统中的驱动程序。
💻 CPP
📖 第 1 页 / 共 4 页
字号:
	#else
		RETAILMSG(1,(TEXT("IsBlockBad(%X): TRUE\r\n"),blockID));
	#endif
        return(TRUE);
    }



    SetKMode (bLastMode);
   
    return(FALSE);
}

static BOOL MarkBlockBad1G08(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_CLEAR_RB();

	NF_CMD(0x80);   // Write 1st command
	
	NF_ADDR((2048+0)&0xff);			// 2060 = 0x080c
	NF_ADDR(((2048+0)>>8)&0xff);	// A[10:8]
	NF_ADDR((blockPage)&0xff);	// A[11;18]
	NF_ADDR((blockPage>>8)&0xff);	// A[26:19]
	#if (K9F2G08_SUPPORT||K9F4G08_SUPPORT||K9F8G08_SUPPORT)
	if((g_pNandFlashChip->FlashID!=0xECF1)&&(g_pNandFlashChip->FlashID!=0xECA1))
		NF_ADDR((blockPage>>16)&0xff);
	#endif	
	
	NF_WRDATA(0);	// Write Bad block information

	NF_CLEAR_RB();
	NF_CMD(CMD_WRITE2);        // Send write confirm command.
    //  Wait for RB
	NF_DETECT_RB();	 // Wait tR(max 12us)

    //  Get the status
    NF_CMD(CMD_STATUS);
    Status = NF_RDDATA();    // Read command status.
    NF_nFCE_H();            // Deassert the flash chip.

    SetKMode (bLastMode);
    return((Status & 1) ? FALSE : TRUE);
}
#endif
#if K9K2G16_SUPPORT
BOOL FMD_ReadSector2G16(SECTOR_ADDR startSectorAddr, LPBYTE pSectorBuff, PSectorInfo pSectorInfoBuff, DWORD dwNumSectors)
{
    ULONG SectorAddr = (ULONG)startSectorAddr;
	int i;
    BOOL bLastMode = SetKMode(TRUE);

//	EdbgOutputDebugString("FMD_ReadSector1G08(%d,%p,%p,%d)\n",startSectorAddr,pSectorBuff,pSectorInfoBuff,dwNumSectors);
    NF_RSTECC();                            // Initialize ECC.
    NF_nFCE_L();                            // Select the flash chip.
	NF_CLEAR_RB();
    NF_CMD(CMD_RESET);                        // Send reset command.
	for(i=0;i<10;i++); 
    	NF_DETECT_RB();

    while (dwNumSectors--)
    {
        ULONG blockPage = SectorAddr;//(((SectorAddr / NAND_PAGE_CNT) * NAND_PAGE_CNT) | (SectorAddr % NAND_PAGE_CNT));
 
	if(pSectorBuff)
	{
		NF_CLEAR_RB();
		NF_CMD(CMD_READ);                    // Send read command.
        	NF_ADDR(0); 	// Column (A[7:0]) = 0
		NF_ADDR(0); 	// A[10:8]
        	NF_ADDR((blockPage)&0xff);	// A[18:11]
    		NF_ADDR((blockPage>>8)&0xff);	// A[26:19]
    		NF_ADDR((blockPage>>16)&0xff);  // A27

       	NF_CLEAR_RB();
       	NF_CMD(0x30);	// 2'nd command
       	NF_DETECT_RB();
		if((DWORD) pSectorBuff&0x3)
		{
			DWORD temp_buf[2048/4];
			RdPage2048((PBYTE)temp_buf,(PULONG)&s2440NAND->NFDATA);
			memcpy(pSectorBuff,temp_buf,2048);
       	}
       	else 
       	{
             	RdPage2048(pSectorBuff,(PULONG)&s2440NAND->NFDATA);                // Read page/sector data.          
		}
	}
	if(pSectorInfoBuff) {
		DWORD tempinfo;
		if(!pSectorBuff)
        	{
        	NF_CLEAR_RB();
             NF_CMD(0x00);	// Read command
       	NF_ADDR((1024+0)&0xff);			// 2060 = 0x080c
		NF_ADDR(((1024+0)>>8)&0xff);
       	NF_ADDR((blockPage)&0xff);		// A[11;18]
		NF_ADDR((blockPage>>8)&0xff);	// A[26:19]
		NF_ADDR((blockPage>>16)&0xff);	// A27

      		NF_CLEAR_RB();
       	NF_CMD(0x30);	// 2'nd command
       	NF_DETECT_RB();	
		}
		tempinfo = NF_DATA_R4();
  	//  Read the bad block mark
        pSectorInfoBuff->bBadBlock = (BYTE) (tempinfo&0xff);//(NF_RDDATA());

	 //  Read the SectorInfo data (we only need to read first 8 bytes)
        pSectorInfoBuff->dwReserved1  = NF_DATA_R4();

        //  OEM byte
        pSectorInfoBuff->bOEMReserved = (BYTE) ((tempinfo>>8)&0xff);//NF_RDDATA();

        
        //  Second reserved field (WORD)
        pSectorInfoBuff->wReserved2 = (WORD)((tempinfo>>16)&0xffff);
//        pSectorInfoBuff->wReserved2 = ((BYTE) NF_RDDATA() << 8);
//        pSectorInfoBuff->wReserved2 |= ((BYTE) NF_RDDATA());
    }
//	    NF_nFCE_H();                             // Deselect the flash chip.
        ++SectorAddr;
        pSectorBuff += NAND_PAGE_SIZE;
    }
    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_EraseBlock2G16(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_CLEAR_RB();
    NF_CMD(CMD_ERASE);                    // Send block erase command.
	// Address 3-cycle
	NF_ADDR(blockPage&0xff);	    // A[18:11]
	NF_ADDR((blockPage>>8)&0xff);   // A[26:19]
	NF_ADDR((blockPage>>16)&0xff);	// A27

    NF_CMD(CMD_ERASE2);                    // Send block erase confirm command.
    //NF_WAITRB();                        // Wait for flash to complete command.
    NF_DETECT_RB();
    //  Check the status
    NF_CMD(CMD_STATUS);
    Status = NF_RDDATA();                // Read command status.
    NF_nFCE_H();                        // Deselect the flash chip.

    SetKMode (bLastMode);
    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_WriteSector2G16(SECTOR_ADDR startSectorAddr, LPBYTE pSectorBuff, PSectorInfo pSectorInfoBuff, DWORD dwNumSectors)
{
    BYTE Status;
    ULONG SectorAddr = (ULONG)startSectorAddr;
    DWORD i;
    BOOL bLastMode = SetKMode(TRUE);
	DWORD tempInfo;
 //   DWORD Mecc;
    if (!pSectorBuff && !pSectorInfoBuff)
        return(FALSE);
    NF_RSTECC();                            // Initialize ECC.
    NF_nFCE_L();                            // Select the flash chip.
	NF_CLEAR_RB();  
    NF_CMD(CMD_RESET);                        // Send reset command.
	for(i=0;i<10;i++); 
	NF_DETECT_RB();

    while (dwNumSectors--)
    {
        ULONG blockPage = SectorAddr;//(((SectorAddr / NAND_PAGE_CNT) * NAND_PAGE_CNT) | (SectorAddr % NAND_PAGE_CNT));
        if (!pSectorBuff)
        {
		NF_CLEAR_RB();    
		NF_CMD(CMD_WRITE);   // Write 1st command
		NF_ADDR((1024+0)&0xff); 	// Column (A[7:0]) = 0
		NF_ADDR(((1024+0)>>8)&0xff); 	// A[11:8]
		NF_ADDR((blockPage)&0xff);	// A[18:11]
		NF_ADDR((blockPage>>8)&0xff);	// A[26:19]
		NF_ADDR((blockPage>>16)&0xff);  // A7
		tempInfo=(pSectorInfoBuff->wReserved2<<16)|(pSectorInfoBuff->bOEMReserved<<8)|pSectorInfoBuff->bBadBlock;
		//  Write the bad block flag
		NF_DATA_W4(tempInfo);
//		NF_WRDATA( (pSectorInfoBuff->bBadBlock) );

        //  Write the first reserved field (DWORD)
        NF_DATA_W4(pSectorInfoBuff->dwReserved1);

        //  Write OEM reserved flag
//        NF_WRDATA( (pSectorInfoBuff->bOEMReserved) );

        //  Write the second reserved field
//        NF_WRDATA( (pSectorInfoBuff->wReserved2 >> 8) & 0xff );
//        NF_WRDATA( (pSectorInfoBuff->wReserved2) & 0xff );
            pSectorInfoBuff++;
        }
        else
        {
//	NF_CMD(CMD_READ);                     // Send read command.
	NF_CLEAR_RB();    
	NF_CMD(CMD_WRITE);   // Write 1st command
	NF_ADDR(0); 	// Column (A[7:0]) = 0
	NF_ADDR(0);		// A[10:8]
	NF_ADDR((blockPage)&0xff);	// A[18:11]
	NF_ADDR((blockPage>>8)&0xff);	// A[26:19]
	NF_ADDR((blockPage>>16)&0xff);  // A7
	
	
	if( ((DWORD) pSectorBuff) & 0x3) {
        //  Write the data
        	DWORD temp_buf[2048/4];
			memcpy(temp_buf,pSectorBuff,2048);
        	WrPage2048 ((PBYTE)temp_buf,(PULONG)&s2440NAND->NFDATA);
            }
            else
            {
                WrPage2048(pSectorBuff,(PULONG)&s2440NAND->NFDATA);                // Write page/sector data.
	}
	if(pSectorInfoBuff)
	{
		tempInfo=(pSectorInfoBuff->wReserved2<<16)|(pSectorInfoBuff->bOEMReserved<<8)|pSectorInfoBuff->bBadBlock;
		//  Write the bad block flag
		NF_DATA_W4(tempInfo);
//	        NF_WRDATA( (pSectorInfoBuff->bBadBlock) );

	        
	        //  Write OEM reserved flag
//	        NF_WRDATA( (pSectorInfoBuff->bOEMReserved) );

	        
	        //  Write the second reserved field
//	        NF_WRDATA( (pSectorInfoBuff->wReserved2 >> 8) & 0xff );
//	        NF_WRDATA( (pSectorInfoBuff->wReserved2) & 0xff );
		//  Write the first reserved field (DWORD)
	        NF_DATA_W4(pSectorInfoBuff->dwReserved1);

	}
            pSectorBuff += NAND_PAGE_SIZE;
        }
        NF_CLEAR_RB();    
        NF_CMD(CMD_WRITE2);                    // Send write confirm command.
         //  Wait for RB
				NF_DETECT_RB();	 // Wait tR(max 12us)

    		//  Check the status
    		NF_CMD(CMD_STATUS);
        Status = NF_RDDATA();
        if (Status & 1)
        {
        	    NF_nFCE_H();                            // Deselect the flash chip.
            SetKMode (bLastMode);
            #ifdef BOOT_LOADER
                 EdbgOutputDebugString("ERROR: FMD_WriteSector: failed sector write.\r\n");
			#else
				 RETAILMSG(1,(TEXT("ERROR: FMD_WriteSector: failed sector write.\r\n")));
			#endif
			return(FALSE);
        }
//	NF_nFCE_H();                            // Deselect the flash chip.
        ++SectorAddr;
    }

    NF_nFCE_H();                            // Deselect the flash chip.

    SetKMode (bLastMode);
    return(TRUE);
}
static BOOL IsBlockBad2G16(BLOCK_ID blockID)
{
    BYTE Data;
    SECTOR_ADDR blockPage = (blockID * NAND_PAGE_CNT);
    BOOL bLastMode = SetKMode(TRUE);

	NF_nFCE_L();

	NF_CLEAR_RB();

       NF_CMD(0x00);	// Read command
       NF_ADDR((1024+0)&0xff);			// 2060 = 0x080c
	NF_ADDR(((1024+0)>>8)&0xff);
	NF_ADDR((blockPage)&0xff);		// A[11;18]
	NF_ADDR((blockPage>>8)&0xff);	// A[26:19]
	NF_ADDR((blockPage>>16)&0xff);	// A27
      NF_CLEAR_RB();
       NF_CMD(0x30);	// 2'nd command
       NF_DETECT_RB();
	   
	
       Data=NF_RDDATA();

    NF_nFCE_H();            // Deassert the flash chip.
    if(0xff != Data)
    {
        SetKMode (bLastMode);
#ifdef BOOT_LOADER
		EdbgOutputDebugString("IsBlockBad(%X): TRUE\r\n",blockID);
#else
		RETAILMSG(1,(TEXT("IsBlockBad(%X): TRUE\r\n"),blockID));
#endif
        return(TRUE);
    }

    SetKMode (bLastMode);
   
    return(FALSE);
}

static BOOL MarkBlockBad2G16(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_CLEAR_RB();

	NF_CMD(0x80);   // Write 1st command
	
	NF_ADDR((1024+0)&0xff);			// 2060 = 0x080c
	NF_ADDR(((1024+0)>>8)&0xff);	// A[10:8]
	NF_ADDR((blockPage)&0xff);	// A[11;18]
	NF_ADDR((blockPage>>8)&0xff);	// A[26:19]
	NF_ADDR((blockPage>>16)&0xff);	// A[27]
	
	
	NF_WRDATA(0);	// Write Bad block information

	NF_CLEAR_RB();
	NF_CMD(CMD_WRITE2);        // Send write confirm command.
    //  Wait for RB
	NF_DETECT_RB();	 // Wait tR(max 12us)

    //  Get the status
    NF_CMD(CMD_STATUS);
    Status = NF_RDDATA();    // Read command status.
    NF_nFCE_H();            // Deassert the flash chip.

    SetKMode (bLastMode);
    return((Status & 1) ? FALSE : TRUE);
}
#endif

⌨️ 快捷键说明

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