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

📄 fmd.cpp

📁 It s wince for ARM9 evaluation board
💻 CPP
📖 第 1 页 / 共 3 页
字号:
   RETAILMSG(1, (TEXT("FMD::FMD_Init: WARNING !!SMC CARD IS NOT INSERTED OR SMC  CARD IS CORRUPTED \n FMD DRIVER WILL NOT BE LOADED !!\r\n")));	
   goto ErrExit;    
   	}
   RELEASEMUTEX();

   NF_Reset(); 
   return (PVOID)pNFCONF;    

ErrExit:
    FMD_Deinit((PVOID)pNFCONF);
    return 0;
}

//  FMD_Deinit
//
//  De-initialize the flash chip
//
BOOL    FMD_Deinit(PVOID hFMD)
{
	if((DWORD)hFMD != (DWORD)pNFCONF)
	{
		return FALSE;
	}

	// destroy the mutex
	if (g_hMutex)
	{
#ifdef NOSYSCALL
#ifndef BOOT_LOADER
        SC_CloseHandle(g_hMutex);
#endif
#else
        CloseHandle(g_hMutex);
#endif
	}

#ifndef NOSYSCALL
#ifdef NOBINFS
    //  We don't have to free pNFReg and clock register.
#ifdef CEDAR_ONLY
    // ++ CE 3.0 Specific Code. Not needed for 4.x +
    if (g_tblFastCall.UnRegisterBlockDrv) {
        g_tblFastCall.UnRegisterBlockDrv(g_tblFastCall.pContext, PowerOnCallback);
    }
    // -- CE 3.0 Specific Code. Not needed for 4.x +
#endif // CEDAR_ONLY

    //  Close the handle
    if (g_hUTLObject) {
        CloseHandle(g_hUTLObject);
    }
#else
    if(pNFReg) {
        VirtualFree(pNFReg, 0, MEM_RELEASE);
    }
#endif 
#endif
    return TRUE;
}


//  FMD_GetInfo
//
//  Return the Flash information
//
BOOL    FMD_GetInfo(PFlashInfo pFlashInfo)
{
    pFlashInfo->flashType = NAND;

    //  OK, instead of reading it from the chip, we use the hardcoded
    //  numbers here.

    pFlashInfo->dwNumBlocks		  = NUM_BLOCKS;
    pFlashInfo->wSectorsPerBlock	  = PAGES_PER_BLOCK;
    pFlashInfo->wDataBytesPerSector  = SECTOR_SIZE;

    return TRUE;
}

//  FMD_ReadSector
//
//  Read the content of the sector.
//
//  startSectorAddr: Starting page address
//  pSectorBuff  : Buffer for the data portion
//  pSectorInfoBuff: Buffer for Sector Info structure
//  dwNumSectors : Number of sectors
//

typedef union _ECCRegVal
{
    DWORD   dwECCVal;
    BYTE    bECCBuf[4];
} ECCRegVal;

BOOL FMD_ReadSector(SECTOR_ADDR startSectorAddr, LPBYTE pSectorBuff,
                        PSectorInfo pSectorInfoBuff, DWORD dwNumSectors)
{
    DWORD       i;
    BYTE        eccBuf[8];
    UINT8    *buff;
    buff=(UINT8 *)0xa8000000;
    ECCRegVal   eccRegVal;

    DEBUGMSG(1, (TEXT("FMD_ReadSector_IN: StartSector, %d numSectors   %d\r\n"),startSectorAddr,dwNumSectors));

//  Sanity check
    if (!pSectorBuff && !pSectorInfoBuff || dwNumSectors > 1) {
#ifdef BOOT_LOADER
        EdbgOutputDebugString("Invalid parameters!\r\n");
#else
        RETAILMSG(1, (TEXT("Invalid parameters!\n")));
#endif
#ifndef NOSYSCALL
        SetLastError(ERROR_INVALID_PARAMETER);
#endif
        return FALSE;
    }
   
	 NF_Reset();

    if(!pSectorBuff) {
        //  We are reading spare only		
        NAND_ReadSectorInfo(startSectorAddr, pSectorInfoBuff);
        //  There is no ECC for the sector info, so the read always succeed.
        DEBUGMSG(1, (TEXT("FMD_ReadSector_OUT1:  \r\n")));
        return TRUE;
    }

	DEBUGMSG(1, (TEXT("FMD_ReadSector_IN: block, %d startSectorAddr   %d\r\n"),startSectorAddr>>5,startSectorAddr));

    GRABMUTEX();

    //  Initialize ECC register
    NF_LOCK_DISABLE();
    NF_RSTECC();
    NF_MECC_UnLock();

    //  Enable the chip
    NF_CE_L();
    NF_CLEAR_RB();

#ifdef  AUTOLOAD

  NF_DISABLE_ALLMODE(); 
  NF_LDSTR_ADD(0);
  NF_LDSTR_SIZE(1);
  
  //  Issue command
  NF_CMD(CMD_READ);

 //  Set up address
  NF_ADDR_(startSectorAddr<<8);	 
    
  NF_LD_START();
  NF_LDDONE_CHECK();
  NF_LDDONE_CLEAR();
  NF_DISABLE_ALLMODE(); 
  NF_SW_MODE();

  //copy data to Stepping stone 
     for(i=0; i<SECTOR_SIZE; i++) {
            		pSectorBuff[i] = *buff++;
        	}
#else

{
    //  Issue command
    NF_CMD(CMD_READ);

    //  Set up address
    NF_ADDR(0x00);
    NF_ADDR((startSectorAddr) & 0xff);
    NF_ADDR((startSectorAddr >> 8) & 0xff);

    if (NEED_EXT_ADDR) {
        NF_ADDR((startSectorAddr >> 16) & 0xff);
    }

   	NF_DETECT_RB();

    //  BUGBUG, because Media Player for Pocket PC sometimes pass us un-aligned buffer
    //  we have to waste cycle here to work around this problem
    if( ((DWORD) pSectorBuff) & 0x3) {
        for(i=0; i<SECTOR_SIZE; i++) {
       	pSectorBuff[i] = (BYTE) NF_DATA_R();
	}
    }	
    else {
    //  The right way.
        ReadPage512(pSectorBuff, pNFDATA);

    }

}
#endif	
	 
        //	for ( i = 0 ; i < 512; i++ )
        //EdbgOutputDebugString("%x ", *(pSectorBuff+i));
        //  Do the ECC thing here
        //  We read the ECC value from the ECC register pFNECC
     NF_MECC_Lock();
     eccRegVal.dwECCVal = NF_ECC();//$ New fresh ECC 

        //  Read the SectorInfo data
    if(pSectorInfoBuff) {
        //  Read the SectorInfo data (we only need to read first 8 bytes)  //$ ??TBD
        pSectorInfoBuff->dwReserved1  = (DWORD) ((BYTE) NF_DATA_R()) << 24;
        pSectorInfoBuff->dwReserved1 |= (DWORD) ((BYTE) NF_DATA_R()) << 16;
        pSectorInfoBuff->dwReserved1 |= (DWORD) ((BYTE) NF_DATA_R()) << 8;
        pSectorInfoBuff->dwReserved1 |= (DWORD) ((BYTE) NF_DATA_R());

        //  OEM byte
        pSectorInfoBuff->bOEMReserved = (BYTE) NF_DATA_R();

        //  Read the bad block mark
        pSectorInfoBuff->bBadBlock = (BYTE) NF_DATA_R();//$ It's 517th byte having information about validity of block

        //  Second reserved field (WORD)
        pSectorInfoBuff->wReserved2 = ((BYTE) NF_DATA_R() << 8);
        pSectorInfoBuff->wReserved2 |= ((BYTE) NF_DATA_R());
    }
    else {
        //  Advance the read pointer
        for(i=0; i<sizeof(SectorInfo); i++) {
            eccBuf[i] = (BYTE) NF_DATA_R();
        }
    }

    //  Verify the ECC values
    //
    //  Read the ECC buffer,$The old ECC value for the data in that sector,which was written there while writting that sector.
    for(i=0; i<3; i++) {
        eccBuf[i] = (BYTE) NF_DATA_R();
    }

    NF_CE_H();

    //  Copmare with the ECC generated from the HW
   // 520th,521st,522nd byte.
    if(eccBuf[0] != eccRegVal.bECCBuf[0] ||
       eccBuf[1] != eccRegVal.bECCBuf[1]  ||
       eccBuf[2] != eccRegVal.bECCBuf[2] ) {

#ifdef BOOT_LOADER
        EdbgOutputDebugString("FMD: ECC ERROR - Page #: %d\r\n", startSectorAddr);
        EdbgOutputDebugString("(%x:%x %x:%x %x:%x)\r\n", eccBuf[0], eccRegVal.bECCBuf[0], eccBuf[1], eccRegVal.bECCBuf[1], eccBuf[2], eccRegVal.bECCBuf[2]);
#else
        RETAILMSG(1, (TEXT("FMD: ECC ERROR - Page #: %d\r\n"), startSectorAddr));
#endif

        //  Now try to correct them
        if(!ECC_CorrectData(pSectorBuff, eccBuf, eccRegVal.bECCBuf)) {
            RETAILMSG(1, (TEXT("FMD: Unable to correct the ECC error - Page #: %d\r\n"),startSectorAddr));
            RELEASEMUTEX();
	    DEBUGMSG(1, (TEXT("FMD_ReadSector_OUT:  \r\n")));	
	    return FALSE;
        }
    }

    RELEASEMUTEX();
    DEBUGMSG(1, (TEXT("FMD_ReadSector_OUT2:  \r\n")));	
    return TRUE;
}

//
//  IsBlockBad
//
//  Check to see if the given block is bad. A block is bad if the 517th byte on
//  the first or second page is not 0xff.
//
//  blockID:    The block address. We need to convert this to page address
//
//
BOOL IsBlockBad(BLOCK_ID blockID)
{
    DWORD   dwPageID = blockID << 5;
    BOOL    bRet = FALSE;
    BYTE    wFlag;


    DEBUGMSG(1, (TEXT(" IsBlockBad\r\n")));

    GRABMUTEX();

    //  Enable the chip
    NF_CE_L();
    NF_CLEAR_RB();

    //  Issue the command
    NF_CMD(CMD_READ2);

    //  Set up address
    NF_ADDR(VALIDADDR);
    NF_ADDR((dwPageID) & 0xff);
    NF_ADDR((dwPageID >> 8) & 0xff);

     if (NEED_EXT_ADDR) {
        NF_ADDR((dwPageID >> 16) & 0xff);
    }

    //  Wait for Ready bit
	NF_DETECT_RB();	 // Wait tR(max 12us)
    //for(i=0;i<1000;i++);

    //  Now get the byte we want
    wFlag = (BYTE) NF_DATA_R();

    if(wFlag != 0xff) {
        bRet = TRUE;
    }

    //  Disable the chip
    NF_CE_H();

    RELEASEMUTEX();
    return bRet;
}

//
//  FMD_GetBlockStatus
//
//  Returns the status of a block.  The status information is stored in the spare area of the first sector for
//  the respective block.
//
//  A block is BAD if the bBadBlock byte on the first page is not equal to 0xff.
//
DWORD FMD_GetBlockStatus(BLOCK_ID blockID)
{
    SECTOR_ADDR sectorAddr = blockID << LOG_2_PAGES_PER_BLOCK;
    SectorInfo SI;
    DWORD dwResult = 0;


       DEBUGMSG(1, (TEXT("FMD_GetBlockStatus_IN : Block No :- %d\r\n"),blockID));

	if(!FMD_ReadSector(sectorAddr, NULL, &SI, 1))
	{
        return BLOCK_STATUS_UNKNOWN;
	}

    if(!(SI.bOEMReserved & OEM_BLOCK_READONLY))
	{
        dwResult |= BLOCK_STATUS_READONLY;
	}

    if(SI.bBadBlock != 0xFF)
	{
        dwResult |= BLOCK_STATUS_BAD;
	}	
   DEBUGMSG(1, (TEXT("FMD_GetBlockStatus_OUT : Block No :- %d\r\n"),blockID));
    return dwResult;
}




//  FMD_EraseBlock
//
//  Erase the given block
//
BOOL FMD_EraseBlock(BLOCK_ID blockID)
{
    BOOL    bRet = TRUE;
    DWORD   dwPageID = blockID << 5;

    DEBUGMSG(1, (TEXT("FMD_EraseBlock %d\r\n"),blockID));
  
    GRABMUTEX();
   
#ifndef NOSYSCALL

	if(blockID < IMAGE_START_BLOCK)
	{
		bRet = FALSE;
		RELEASEMUTEX();
		return bRet;
	}
#endif

     //IO Port Setting
	 s24A0IOP->rGPCON_L=(s24A0IOP->rGPCON_L & ~(0x3<<2))|(0x1<<2);// Setting as OUTPUT Mode
        s24A0IOP->rGPDAT |=(1<<1);

    //Lock Disable
    NF_LOCK_DISABLE();
   
    //  Enable the chip
    NF_CE_L();
    NF_CLEAR_RB();

    //  Issue command
    NF_CMD(CMD_ERASE);

    //  Set up address
    NF_ADDR((dwPageID) & 0xff);
    NF_ADDR((dwPageID >> 8) & 0xff);

     if (NEED_EXT_ADDR) {
    NF_ADDR((dwPageID >> 16) & 0xff);
    }

    //  Complete erase operation
    NF_CMD(CMD_ERASE2);

    //  Wait for ready bit
    NF_DETECT_RB();	 // Wait tR(max 12us)
    //for(i=0;i<1000;i++);

    s24A0IOP->rGPDAT &=~(1<<1);
    //  Check the status
    NF_CMD(CMD_STATUS);

    if(NF_DATA_R() & STATUS_ERROR) {
        RETAILMSG(1, (TEXT("######## Error Erasing block %d!\n"), blockID));
	bRet = FALSE;
        
    }

    NF_CE_H();
    RELEASEMUTEX();

  DEBUGMSG(1, (TEXT("FMD_EraseBlock  Done %d\r\n"),bRet));	
    return bRet;
}



//  FMD_WriteSector
//
//  Write dwNumPages pages to the startSectorAddr
//
BOOL FMD_WriteSector(SECTOR_ADDR startSectorAddr, LPBYTE pSectorBuff, PSectorInfo pSectorInfoBuff,
                        DWORD dwNumSectors)
{
    DWORD   i;
    BOOL    bRet = TRUE;
    DWORD   dwECCVal;
    BYTE    eccBuf[4];
    UINT8    *sbuff;
    sbuff=(UINT8 *)0xA8000000;
    //UINT8 	rbuff[512];


   DEBUGMSG(1, (TEXT("FMD_WriteSector: \r\n startSectorAddr %d , NumSectors %d\r\n"),startSectorAddr,dwNumSectors));
    //  Sanity check
    //  BUGBUGBUG: I need to come back to support dwNumSectors > 1
    //
    if((!pSectorBuff && !pSectorInfoBuff) || dwNumSectors != 1) {
#ifdef BOOT_LOADER
        EdbgOutputDebugString("Invalid parameters!\r\n");
#else
        RETAILMSG(1, (TEXT("Invalid parameters!\n")));
#endif
#ifndef NOSYSCALL
        SetLastError(ERROR_INVALID_PARAMETER);
#endif
        return FALSE;
    }

    if(!pSectorBuff) {
        //  If we are asked just to write the SectorInfo, we will do that separately
        bRet = NAND_WriteSectorInfo(startSectorAddr, pSectorInfoBuff);
		return bRet;			// Do not write the actual sector information...
    }


#if 0
   // NF_LOCK_DISABLE();
  if(!FMD_EraseBlock(startSectorAddr>>5))    
        RETAILMSG(1, (TEXT("FMD_ReadSector_AUTOSTORE:Block Erase_Fail!!! \r\n")));
   else   
   	RETAILMSG(1, (TEXT("FMD_ReadSector_AUTOSTORE:Block Erase_OK \r\n")));

 //  s24A0IOP->rGPCON_L=(s24A0IOP->rGPCON_L & ~(0x3<<2))|(0x1<<2);// Setting as OUTPUT Mode
 //  s24A0IOP->rGPDAT |=(1<<1);
#endif

    GRABMUTEX();
	
   s24A0IOP->rGPCON_L=(s24A0IOP->rGPCON_L & ~(0x3<<2))|(0x1<<2);// Setting as OUTPUT Mode
   s24A0IOP->rGPDAT |=(1<<1);


    //  Initialize ECC register
    NF_RSTECC();
    NF_MECC_UnLock();
    
    //  Enable Chip
    NF_CE_L();


#ifdef AUTOSTORE

 RETAILMSG(1, (TEXT("FMD_WriteSector_AUTOSTORE:  \r\n")));

 for(i=0; i<SECTOR_SIZE; i++) {
            	sbuff[i]=pSectorBuff[i];		     		
      	}
 
  NF_DISABLE_ALLMODE(); 
  NF_DISABLE_HWCE();
  NF_CE_L();
  NF_CLEAR_RB();
  NF_LDSTR_ADD(0);
  
     //  Issue command     
      NF_ADDR_(0);	
      NF_ADDR_(startSectorAddr<<8);
 
     //NF_CMD(CMD_READ);	
     NF_CMD(CMD_WRITE);	

⌨️ 快捷键说明

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