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

📄 nand_tool.c

📁 Nand Flash (SAMSUNG_K9K1G08U0B) boot loader for i.Mx31 (freescale).
💻 C
字号:
#include <common/message.h>
#include <nand/mx_nand512.h>

void NAND_Info(void)
{
  u32 i;
  u32 j = 0;

#ifdef DEBUG
  Message("\n------------------------------------\n");
  
  Message("NAND Flash Information:\n");
  
  Message("Num of Blocks = %d\n",          NAND_NUM_OF_BLOCKS );
  Message("Num of Page per Block = %d \n", NAND_NUM_OF_PAGES);
  Message("Main Page Size = %d Byte\n",    NAND_MAIN_BYTESIZE );
  Message("Spare Page Size = %d Byte\n",   NAND_SPARE_BYTESIZE);
  Message("Bus Width = %d bit \n",         NAND_IO_WIDTH);

  Message("\n");
#endif
  
  // Scan for Bad Block
   for(i=0; i<gNAND->num_of_blocks; i++) {
     if(  !NAND_BlockIsGood(i) ) {
#ifdef DEBUG      
       Message("BLK# %d is bad\n", i);
#endif
       j++;
     }
   }

#ifdef DEBUG
  Message("Num of Bad Blocks = %d\n", j);
  Message("\n------------------------------------\n");
#endif
}

void NAND_ConditionalEraseAll()
{

}

void NAND_Erase_Conditional(u32 StartAddress, u32 EndAddress)
{
   u32 i;
   u32 j=0;
  
#ifdef DEBUG   
   Message("Erase: Start Block # = %d\n", StartAddress/(NAND_NUM_OF_PAGES*NAND_MAIN_BYTESIZE) );
   Message("Erase: End   Block # = %d\n", EndAddress/(NAND_NUM_OF_PAGES*NAND_MAIN_BYTESIZE)   );
#endif
   
   i=StartAddress/(NAND_NUM_OF_PAGES*NAND_MAIN_BYTESIZE);
   
   for(; i<=(EndAddress/(NAND_NUM_OF_PAGES*NAND_MAIN_BYTESIZE)); i++) 
     NAND_Wait();
     if(NAND_BlockIsGood(i)) {
       NAND_Wait();
       if(NAND_EraseBlock(i))
         FlashReport_Error(REPORT_ERROR_ERASE, i*NAND_MAIN_BYTESIZE*NAND_NUM_OF_PAGES);
       else {
         j++;
         FlashReport_EraseByteDone(j*NAND_NUM_OF_PAGES*NAND_MAIN_BYTESIZE);
       }
     }
 
   FlashReport_EraseComplete();
}


void NAND_UnconditionalEraseAll(void)
{
  u32 i;
 
#ifdef DEBUG 
  Message("Erase\n");
#endif
  
  for(i=0; i<NAND_NUM_OF_BLOCKS; i++) {
  	NAND_Wait();
    if(NAND_EraseBlock(i))
      FlashReport_Error(REPORT_ERROR_ERASE, i*NAND_MAIN_BYTESIZE*NAND_NUM_OF_PAGES);
#ifdef DEBUG
    Message("Erasing Block# %d\n", i+1);
#endif
  }
}

void NAND_Test(void)
{
  u32 i;
  u8  j;
  u32 ByteSize = 1*1024*1024;
  u32 FlashAddress = 20*1024*1024;
  u32 SourceAddress = 0xC2000000;
  u32 TargetAddress = SourceAddress + ByteSize;


  NAND_Init();
  
#ifdef DEBUG
  Message("Manufacturer ID=0x%X, Device ID=0x%X\n", NAND_MAN_ID, NAND_DEV_ID);
#endif

  NAND_Info();

//  NAND_BlockEraseAll();

  j=0;
  for(i=0; i<ByteSize; i++)
  {
    if(j==0)
      j=1;
      
    *(u8 *)((u8 *)SourceAddress + i) = j;
    j++; 
  }

  NAND_Write(FlashAddress, SourceAddress, ByteSize);
  NAND_Read (FlashAddress, TargetAddress, ByteSize);

  j=0;
  for(i=0; i<ByteSize; i++)
    if( *(u8 *)(SourceAddress+i) != *(u8 *)(TargetAddress+i) ) {
#ifdef DEBUG      	
      	Message("Error at i=%d, SourceAddress=0x%X, TargetAddress=0x%X\n", i, SourceAddress+i, TargetAddress+i);
#endif      	
      	j++;
    }
    
  NAND_Info();

#ifdef DEBUG
  Message("Number of Errors = %d\n", j);
#endif  
  if(j)
#ifdef DEBUG
    Message("Error !!!\n");
#else
	;
#endif
  else
#ifdef DEBUG
    Message("Pass !!!\n");  
#else
	;
#endif
}

void NAND_Loader(u32 FlashAddress, u32 SourceAddress, u32 ByteSize)
{
   NAND_Write(FlashAddress, SourceAddress, ByteSize);
   if( NAND_Compare(FlashAddress, SourceAddress, ByteSize)==NAND_ERROR_NO ) //if Error
     FlashReport_ProgramComplete();
}

⌨️ 快捷键说明

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