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

📄 nand.c

📁 一份在S3c2440上实现NANDFlASH上读写的源代码程序
💻 C
字号:
//====================================================================
// File Name : Nand.c
// Function  : S3C2440 8-bit interface Nand Test program(this program use K9k2g16.c).
// Date      : May xx, 2003
// Version   : 0.0
// History
//   R0.0 (200305xx): Modified for 2440 from 2410. -> DonGo
//====================================================================


/**************** K9s1208 NAND flash ********************/
// 1block=(512+16)bytes x 32pages
// 4096block
// Block: A[23:14], Page: [13:9]
/**************** K9K2G16 NAND flash *******************/
// 1block=(2048+64)bytes x 64pages
// 2048block
// Block: A[23:14], page: [13:9]
/*****************************************************/
#include "def.h"
#include "option.h"
#include "2440addr.h"
#include "2440lib.h"
#include "Nand.h"


#define BAD_CHECK	(0)
#define ECC_CHECK	(0)

U8 NF8_Spare_Data[16];
U8  buf[512];
volatile int NFConDone;


#define NF_MECC_UnLock()    {rNFCONT&=~(1<<5);}  //Unlock Main data area ECC generation
#define NF_MECC_Lock()      {rNFCONT|=(1<<5);}   //Lock Main data area ECC generation
#define NF_SECC_UnLock()    {rNFCONT&=~(1<<6);}  //Unlock Spare ECC
#define NF_SECC_Lock()      {rNFCONT|=(1<<6);}   //Lock Spare ECC
#define NF_CMD(cmd)			{rNFCMD=cmd;}      //command to nand flash
#define NF_ADDR(addr)		{rNFADDR=addr;}    //address to nand flash	
#define NF_nFCE_L()			{rNFCONT&=~(1<<1);}  //Force nFCE to low(Enable chip select)
#define NF_nFCE_H()			{rNFCONT|=(1<<1);}   //Force nFCE to High(Disable chip select)
#define NF_RSTECC()			{rNFCONT|=(1<<4);}   //Initialize ECC decoder/encoder
#define NF_RDDATA() 		(rNFDATA)          //read data from nand
#define NF_RDDATA8() 		((*(volatile unsigned char*)0x4E000010) ) //read Byte from nand 
#define NF_WRDATA(data) 	{rNFDATA=data;}    //write data to nand
#define NF_WRDATA8(data) 	{rNFDATA8=data;}   //write Byte to nand
// RnB Signal
#define NF_CLEAR_RB()    		{rNFSTAT |= (1<<2);}	// Have write '1' to clear this bit.
#define NF_DETECT_RB()    		{while(!(rNFSTAT&(1<<2)));}//0: RnB transition is not detected

// HCLK=100Mhz
#define TACLS		7	// 1-clk(0ns) 
#define TWRPH0		7	// 3-clk(25ns)
#define TWRPH1		7	// 1-clk(10ns)  //TACLS+TWRPH0+TWRPH1>=50ns

void __irq NFCon_Int(void);

static U8 se8Buf[16]={
	0xff,0xff,0xff,0xff,
	0xff,0xff,0xff,0xff,
	0xff,0xff,0xff,0xff,
	0xff,0xff,0xff,0xff
};

//*************************************************

/**** nand flash 块擦除函数 ****/
int NF8_EraseBlock(U32 block)
{
	U32 blockPage;
	blockPage=(block<<5);
	
	if((Read_Status()&0x80)==0) 
	{
	    Uart_Printf("Write protected.\n");
	    return FAIL;
	}
	
  
    NFConDone=0;
    rNFCONT|=(1<<9);
    rNFCONT|=(1<<10);
    pISR_NFCON= (unsigned)NFCon_Int;
    rSRCPND=BIT_NFCON;
    rINTMSK=~(BIT_NFCON);

	NF_nFCE_L();
    
	NF_CMD(0x60);   // Erase one block 1st command, Block Addr:A9-A25
	// Address 3-cycle
	NF_ADDR(blockPage&0xff);	    // Page number=4095
	NF_ADDR((blockPage>>8)&0xff);   
	NF_ADDR((blockPage>>16)&0xff);


	NF_CLEAR_RB();
	NF_CMD(0xd0);	// Erase one blcok 2nd command
    
    while(NFConDone==0);
	
	rNFCONT&=~(1<<9);
	rNFCONT&=~(1<<10); // Disable Illegal Access Interrupt
	if(rNFSTAT&0x8) return FAIL;

	NF_CMD(0x70);   // Read status command

    if (NF_RDDATA()&0x1) // Erase error
    {	
        NF_nFCE_H();
	    Uart_Printf("[ERASE_ERROR:block#=%d]\n",block);
     	NF8_MarkBadBlock(block);
	return FAIL;
       }
       else 
       {
    	NF_nFCE_H();
       return OK;
       }
}


void __irq NFCon_Int(void)
{
       NFConDone=1;
	rINTMSK|=BIT_NFCON;
	ClearPending(BIT_NFCON);
	if(rNFSTAT&0x8) Uart_Printf("Illegal Access is detected!!!\n");
}


/**** nand flash 坏块屏蔽函数 ****/
int NF8_MarkBadBlock(U32 block)
{
    int i;
	U32 blockPage=(block<<5);
 
     se8Buf[0]=0xff;
     se8Buf[1]=0xff;    
     se8Buf[2]=0xff;    
     se8Buf[5]=0x44;   // Bad blcok mark=44
    
	NF_nFCE_L(); 
	NF_CMD(0x50);   //????
	NF_CMD(0x80);   // Write 1st command
    
	NF_ADDR(0x0);		    // The mark of bad block is 
	NF_ADDR(blockPage&0xff);	    // marked 5th spare array 
	NF_ADDR((blockPage>>8)&0xff);   // in the 1st page.
	NF_ADDR((blockPage>>16)&0xff);  //
    
	for(i=0;i<16;i++)
      {
	   NF_WRDATA(se8Buf[i]);	// Write spare array
      }

	NF_CLEAR_RB();
	NF_CMD(0x10);   // Write 2nd command
	NF_DETECT_RB();

	NF_CMD(0x70);
    
	for(i=0;i<3;i++);  //twhr=60ns////??????
    
      if (NF_RDDATA()&0x1) // Spare arrray write error
      {	
    	NF_nFCE_H();
    	Uart_Printf("[Program error is occurred but ignored]\n");
       }
      else 
      {
    	NF_nFCE_H();
      }

	Uart_Printf("[block #%d is marked as a bad block]\n",block);
       return OK;
}

/**** nand flash 页读取函数 ****/
int NF8_ReadPage(U32 block,U32 page,U8 *buffer)
{
    int i;
    unsigned int blockPage;
	U32 Mecc, Secc;
	    
    blockPage=(block<<5)+page;
	NF_RSTECC();    // Initialize ECC
	NF_MECC_UnLock();
    
	NF_nFCE_L();    

	NF_CLEAR_RB();
	NF_CMD(0x00);	// Read command
	NF_ADDR(0); 	// Column = 0
	NF_ADDR(blockPage&0xff);		//
	NF_ADDR((blockPage>>8)&0xff);	// Block & Page num.
	NF_ADDR((blockPage>>16)&0xff);	//
	NF_DETECT_RB();
	 
	for(i=0;i<512;i++)
	    	buffer[i] = NF_RDDATA8();	// Read one page
	     
	 NF_MECC_Lock();

	 NF_SECC_UnLock();
	 Mecc=NF_RDDATA();
	 rNFMECCD0=((Mecc&0xff00)<<8)|(Mecc&0xff);
	 rNFMECCD1=((Mecc&0xff000000)>>8)|((Mecc&0xff0000)>>16);
	
	 NF_SECC_Lock();
	 NF8_Spare_Data[0]=Mecc&0xff;
	 NF8_Spare_Data[1]=(Mecc&0xff00)>>8;
	 NF8_Spare_Data[2]=(Mecc&0xff0000)>>16;
	 NF8_Spare_Data[3]=(Mecc&0xff000000)>>24;
	 NF_RDDATA();  // read 4~7
	 Secc=NF_RDDATA();
	 rNFSECCD=((Secc&0xff00)<<8)|(Secc&0xff);
	 NF8_Spare_Data[8]=Secc&0xff;
	 NF8_Spare_Data[9]=(Secc&0xff00)>>8;
	 NF8_Spare_Data[10]=(Secc&0xff0000)>>16;
	 NF8_Spare_Data[11]=(Secc&0xff000000)>>24;
	 NF_nFCE_H();    

	 if ((rNFESTAT0&0xf) == 0x0){
	       Uart_Printf("ECC OK!\n");
		return OK;
	 }
	 else {
		Uart_Printf("ECC FAIL!\n");
	       return FAIL;
	 }


}

/**** nand flash 页写函数 ****/
int NF8_WritePage(U32 block,U32 page,U8 *buffer)
{
    int i;
	U32 blockPage, Mecc, Secc;
	
	NFConDone=0;
    
    rNFCONT|=(1<<9);
    rNFCONT|=(1<<10);
    pISR_NFCON= (unsigned)NFCon_Int;
    rSRCPND=BIT_NFCON;
    rINTMSK=~(BIT_NFCON);
	  
	NF_RSTECC();    // Initialize ECC
    NF_MECC_UnLock();
	
	blockPage=(block<<5)+page;

	NF_nFCE_L(); 
	NF_CMD(0x0);//
	NF_CMD(0x80);   // Write 1st command
	NF_ADDR(0);			    // Column 0
	NF_ADDR(blockPage&0xff);	    //
	NF_ADDR((blockPage>>8)&0xff);   // Block & page num.
	NF_ADDR((blockPage>>16)&0xff);  //
     
	for(i=0;i<512;i++)
		NF_WRDATA8(buffer[i]);	// Write one page to NFM from buffer
     

    NF_MECC_Lock();
	// Get ECC data.
	// Spare data for 8bit
	// byte  0     1    2     3     4          5               6      7            8         9
	// ecc  [0]  [1]  [2]  [3]    x   [Bad marking]                    SECC0  SECC1
	Mecc = rNFMECC0;
	se8Buf[0]=(U8)(Mecc&0xff);
	se8Buf[1]=(U8)((Mecc>>8) & 0xff);
	se8Buf[2]=(U8)((Mecc>>16) & 0xff);
	se8Buf[3]=(U8)((Mecc>>24) & 0xff);
	se8Buf[5]=0xff;		// Marking good block

	NF_SECC_UnLock();
	//Write extra data(ECC, bad marking)
	for(i=0;i<4;i++) {
		NF_WRDATA8(se8Buf[i]);	// Write spare array(Main ECC)
		NF8_Spare_Data[i]=se8Buf[i];
    	}  
    
    NF_SECC_Lock(); 
	Secc=rNFSECC; 
	se8Buf[8]=(U8)(Secc&0xff);
	se8Buf[9]=(U8)((Secc>>8) & 0xff);
	for(i=4;i<16;i++) {
		NF_WRDATA8(se8Buf[i]);  // Write spare array(Spare ECC and Mark)
		NF8_Spare_Data[i]=se8Buf[i];
	}  
 	NF_CLEAR_RB();
	NF_CMD(0x10);	 // Write 2nd command
	while(NFConDone==0);
	 rNFCONT&=~(1<<9);
	 rNFCONT&=~(1<<10); // Disable Illegal Access Interrupt
	 if(rNFSTAT&0x8) return FAIL;

	NF_CMD(0x70);   // Read status command   
    
	for(i=0;i<3;i++);  //twhr=60ns
    
       if (NF_RDDATA()&0x1) {// Page write error
    	       NF_nFCE_H();
		Uart_Printf("[PROGRAM_ERROR:block#=%d]\n",block);
		NF8_MarkBadBlock(block);
		return FAIL;
       } else {
    	      NF_nFCE_H();
	      return OK;
	}

}


/**** nand flash ID 读取函数 ****/
U16 NF8_CheckId(void)
{
    int i;
	U16 id;
    
	NF_nFCE_L();
    NF_CMD(0x90);
	NF_ADDR(0x0);
	for (i=0; i<10; i++);
	id=NF_RDDATA8()<<8;	// Maker code 0xec
    id|=NF_RDDATA8();	// Devide code(K9S1208V:0x76)

	NF_nFCE_H();
    return id;
}

/**** nand flash 复位 ****/
void Nand_Reset(void)
{
    int i;
   
	NF_nFCE_L();

	NF_CLEAR_RB();
	NF_CMD(0xFF);	//reset command
	for(i=0;i<10;i++);  //tWB = 100ns. 
	NF_DETECT_RB();
	
	NF_nFCE_H();

}

/**** 读nand flash 状态 ****/
U8 Read_Status(void)
{
	// Read status
	U8 ch;
	int i;
	
	NF_nFCE_L();

	NF_CMD(0x70);
	for(i=0; i<10; i++);
	ch = NF_RDDATA();
	
	NF_nFCE_H();
	return ch;
}

/**** nand flash 初始化 ****/
void NF8_Init(void)
{
	// for S3C2440
    rGPACON = (rGPACON &~(0x3f<<17)) | (0x3f<<17);
	
	rNFCONF = (TACLS<<12)|(TWRPH0<<8)|(TWRPH1<<4)|(0<<0);	
	// TACLS		[14:12]	CLE&ALE duration = HCLK*TACLS.
	// TWRPH0		[10:8]	TWRPH0 duration = HCLK*(TWRPH0+1)
	// TWRPH1		[6:4]	TWRPH1 duration = HCLK*(TWRPH1+1)
	// AdvFlash(R)	[3]		Advanced NAND, 0:256/512, 1:1024/2048
	// PageSize(R)	[2]		NAND memory page size
	//						when [3]==0, 0:256, 1:512 bytes/page.
	//						when [3]==1, 0:1024, 1:2048 bytes/page.
	// AddrCycle(R)	[1]		NAND flash addr size
	//						when [3]==0, 0:3-addr, 1:4-addr.
	//						when [3]==1, 0:4-addr, 1:5-addr.
	// BusWidth(R/W) [0]	NAND bus width. 0:8-bit, 1:16-bit.
	
	rNFCONT = (0<<13)|(0<<12)|(0<<10)|(0<<9)|(0<<8)|(1<<6)|(1<<5)|(1<<4)|(1<<1)|(1<<0);
	// Lock-tight	[13]	0:Disable lock, 1:Enable lock.
	// Soft Lock	[12]	0:Disable lock, 1:Enable lock.
	// EnablillegalAcINT[10]	Illegal access interupt control. 0:Disable, 1:Enable
	// EnbRnBINT	[9]		RnB interrupt. 0:Disable, 1:Enable
	// RnB_TrandMode[8]		RnB transition detection config. 0:Low to High, 1:High to Low
	// SpareECCLock	[6]		0:Unlock, 1:Lock
	// MainECCLock	[5]		0:Unlock, 1:Lock
	// InitECC(W)	[4]		1:Init ECC decoder/encoder.
	// Reg_nCE		[1]		0:nFCE=0, 1:nFCE=1.
	// NANDC Enable	[0]		operating mode. 0:Disable, 1:Enable.

}


/**** 串口输出nand坏块号 ****/
void NF8_PrintBadBlockNum(void)
{
    int i;
    U16 id;

    Uart_Printf("\n[SMC(K9S1208V0M) NAND Flash bad block check]\n");
    
    id=NF8_CheckId();
    Uart_Printf("ID=%x(0xec76)\n",id);
    if(id!=0xec76)
    return;
    for(i=0;i<4096;i++)  	NF8_IsBadBlock(i);   // Print bad block
}


/**** nand flash 坏块判断函数 ****/
int NF8_IsBadBlock(U32 block)
{
    unsigned int blockPage;
    U8 data;
    blockPage=(block<<5);	// For 2'nd cycle I/O[7:5] 
    
	NF_nFCE_L();
	NF_CLEAR_RB();

	NF_CMD(0x50);		 // Spare array read command
	NF_ADDR((512+5)&0xf);		 // Read the mark of bad block in spare array(M addr=5), A4-A7:Don't care
	NF_ADDR(blockPage&0xff);	 // The mark of bad block is in 0 page
	NF_ADDR((blockPage>>8)&0xff);	 // For block number A[24:17]
	NF_ADDR((blockPage>>16)&0xff);  // For block number A[25]
	 
	NF_DETECT_RB();	 // Wait tR(max 12us)

       data=NF_RDDATA();

	NF_nFCE_H();    

     if(data!=0xff)
     {
    	Uart_Printf("[block %d has been marked as a bad block(%x)]\n",block,data);
    	return FAIL;
     }
     else
     {
    	return OK;
     }
}



/**** nand flash 软件解锁函数 ****/
void Test_NF8_SoftUnLock(void)
{
	Uart_Printf("SMC(K9S1208V0M) NAND SoftUnLock Test !!!\n");
	
	rNFSBLK=0x0;
	rNFEBLK=0x0;

	rNFCONT&=~(1<<12);
	if(rNFCONT&(1<<13)){
		rNFCONT&=~(1<<13);
		Uart_Printf("Lock-tight\n ");
		Uart_Printf("You can't unlock Protected blocks !!!\n ");
		Uart_Printf("%d block ~ %d block are Programmable\n ", (rNFSBLK>>5), ((rNFEBLK>>5)-1));
	}
       else Uart_Printf("All blocks are Programmable\n ");
}

/**** nand flash 软件锁定试函数 ****/
void NF8_Block_Lock(void)
{
       U32 num;
       U32 S_block, E_block;
	Uart_Printf("SMC(K9S1208V0M) NAND Lock Test !!!\n");
	Uart_Printf("Select Lock type, Softlock(1)/Lock-tight(2) : ");

	num=Uart_GetIntNum();;
			
	Uart_Printf("\nEnter programmable start block address ");
	S_block = Uart_GetIntNum();
	Uart_Printf("Enter programmable end block address ");
    E_block = Uart_GetIntNum();

	rNFSBLK=(S_block<<5);
	rNFEBLK=(E_block<<5);
	
	if(num==1){
		rNFCONT|=(1<<12);
              Uart_Printf("Software Locked\n ");
	}
	if(num==2){
		rNFCONT|=(1<<13);
              Uart_Printf("Lock-tight: To clear Lock-tight, reset S3C2440!!!\n ");
	}
      Uart_Printf("%d block ~ %d block are Programmable\n ", S_block, (E_block-1));
}

⌨️ 快捷键说明

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