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

📄 mlc.c

📁 三星2443芯片
💻 C
📖 第 1 页 / 共 3 页
字号:
//====================================================================
// File Name : K9k2g16.c
// Function  : S3C2440 16-bit interface Nand Test program(this program used by nand.c).
// Date      : May xx, 2003
// Version   : 0.0
// History
//   R0.0 (200305xx): Modified for 2440 from 2410. -> DonGo
//====================================================================


/**************** K9s1206 NAND flash ********************/
// 1block=(NF8_PAGE_BYTE+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 "system.h"
#include "MLC.h"
#include "nand.h"

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

#define C_LANG	1
#define DMA		2
#define TRANS_MODE_MLC  1


extern void Nand_Reset(void);

extern U32 srcAddress;
extern U32 targetBlock;	    // Block number (0 ~ 4095)
extern U32 targetSize;	    // Total byte size 

U8 MLC_Spare_Data[64]={
	0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
	0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
	0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
	0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff
};
volatile int NFConDone_MLC;

volatile int NFConDone_MLC, NFECCEncDone, NFECCDecDone;


void __irq NFCon_Int_MLC(void);

static U8 MLC_Buf[64]={
	0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
	0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
	0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
	0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff
};


void * MLC_func[][2]=
{
	(void *)MLC_Print_Id,			"Read ID             ",
	(void *)Test_MLC_Block_Erase,	"Block erase         ",
	(void *)Test_MLC_Page_Read,		"Page read           ",
	(void *)Test_MLC_Page_Write,	"Page write          ",
	(void *)MLC_Program,			"K9HBG08_Program     ",
	(void *)Test_MLC_Adv_ECC,		"MLC ECC         ",
	0,0
};




void Test_K9HBG08(void)
{
	int i;
	printf("\nK9K2G16 Nand flash test start.\n");

	MLC_Init();
	
	while(1) {
		Print_MLC_SubMessage();
		printf("\nSelect(-1 to exit): ");
		i = GetIntNum();
		if(i==-1) break;
		
		if(i>=0 && (i<(sizeof(MLC_func)/8)) ) 
	    	( (void (*)(void)) (MLC_func[i][0]) )();	// execute selected function.
	}

}

void Print_MLC_SubMessage(void)
{
	int i;
	
	i=0;	
	printf("\n\n");
	while(1)
	{   //display menu
		printf("%2d:%s",i,MLC_func[i][1]);
	    i++;
	    if((int)(MLC_func[i][0])==0)
	    {
			printf("\n");
			break;
	    }
	    if((i%4)==0) printf("\n");
	}
}



void MLC_Print_Id(void)
{
	U16 id;
	U8 maker, device;

//	MLC_Init();
	
	id = MLC_CheckId();

	
	device = (U8)id;
	maker = (U8)(id>>8);
	
	printf(" Maker:%x, Device:%x\n", maker, device);
}



void Test_MLC_Block_Erase(void)
{
	U32 block=0;

	printf("MLC(K9HBG08) NAND Block erase\n");

	printf("Block # to erase: ");
	block = GetIntNum();
	


	if(MLC_EraseBlock(block)==FAIL) return;

	printf("%d-block erased.\n", block);

}






void Test_MLC_Page_Read(void)
{
	U32 block=0, page=0;
	U32 i;

    unsigned char * srcPt;
	srcPt=(unsigned char *)0x31000000;
	
	rGPHCON=rGPHCON&~(0xf<<26)|(0x5<<26);  // GPH13, 14 => OUTPUT
	rGPHDAT&=~(0x3<<13);	
	
	printf("MLC(K9HBG08) NAND Page Read.\n");

	
	printf("Block # to read: ");
	block = GetIntNum();
	printf("Page # to read: ");
	page = GetIntNum();

	if(MLC_ReadPage(block, page, (U8 *)srcPt)==FAIL) {
		printf("Read error.\n");
	} else {
		printf("Read OK.\n");
	};

	// Print data.
	printf("Read data(%d-block,%d-page)\n", block, page);
	for(i=0; i<2048; i++) {
		if((i%16)==0) printf("\n%4x: ", i);
		printf("%02x ", *srcPt++);
	}
	printf("\n");
	printf("Spare:");

	for(i=0; i<64; i++) {
		if((i%16)==0) printf("\n%4x: ", i);
	 	printf("%02x ", MLC_Spare_Data[i]);
	}
	printf("\n");

}



void Test_MLC_Page_Write(void)
{
	U32 block=0, page=0;
	int i, offset;
	unsigned char *srcPt;
	srcPt=(unsigned char *)0x31100000;
	
	rGPHCON=rGPHCON&~(0xf<<26)|(0x5<<26);  // GPH13, 14 => OUTPUT
	rGPHDAT&=~(0x3<<13);	
	printf("MLC(K9HBG08) NAND Page Write.\n");
	


	printf("Block # to write: ");
	block = GetIntNum();
	printf("Page # to write: ");
	page = GetIntNum();
	printf("offset data(-1:random): ");
	offset = GetIntNum();

#if ADS10==TRUE	
	srand(0);
#endif

	
	// Init wdata.
	for(i=0; i<2048; i++) {
	#if ADS10==TRUE
		if(offset==-1) *srcPt++ = rand()%0xffffffff;
	#else
		if(offset==-1) *srcPt++ = i;
	#endif
		else *srcPt++ =i+offset;
	}
     
    srcPt=(unsigned char *)0x31100000;
	printf("Write data[%d block, %d page].\n", block, page);
	
	
    printf("\n\n");

	
	if(MLC_WritePage(block, page, (U8 *)srcPt)==FAIL) {
		printf("Write Error.\n");
	} else {
		printf("Write OK.\n");
	    printf("Write data is");
	    	
		for(i=0; i<2048; i++) {
		if((i%16)==0) printf("\n%4x: ", i);
		printf("%02x ", *srcPt++);
		}
		printf("\n");

		printf("Spare:");
		for(i=0; i<64; i++) {
		if((i%16)==0) printf("\n%4x: ", i);
	 	printf("%02x ", MLC_Spare_Data[i]);
	}
	
		printf("\n\n");
	}
  
}






void MLC_Program(void)
{    
    int programError=0;
    U8 *srcPt,*saveSrcPt;
    U32 blockIndex, i;

    printf("\n[SOP(K9K2G16U0M) NAND Flash writing program]\n");
    printf("The program buffer: 0x30100000~0x31ffffff\n");
    
    

     rINTMSK = BIT_ALLMSK; 	
     srcAddress=0x30100000; 

     InputTargetBlock_MLC();

     srcPt=(U8 *)srcAddress;
     blockIndex=targetBlock;

     while(1) {
             saveSrcPt=srcPt;	
              
     	#if BAD_CHECK
     	  if(MLC_IsBadBlock(blockIndex)==FAIL) {
		    blockIndex++;   // for next block
		    continue;
	  }
     	#endif
		
     	if(MLC_EraseBlock(blockIndex)==FAIL) {
		    blockIndex++;   // for next block
		    continue;
     	}
   	// After 1-Block erase, Write 1-Block(64 pages).
	for(i=0;i<64;i++) {
		    if(MLC_WritePage(blockIndex,i,srcPt)==FAIL) {// block num, page num, buffer
		        programError=1;
		        break;
		    }

	#if ECC_CHECK
		    if(MLC_ReadPage(blockIndex,i,srcPt)==FAIL) {
			printf("ECC Error(block=%d,page=%d!!!\n",blockIndex,i);
		    }
	#endif

	srcPt+=2048;	// Increase buffer addr one pase size
    	if(i==0)  printf(".");
	if((U32)srcPt>=(srcAddress+targetSize)) // Check end of buffer
	break;	// Exit for loop
    }
		
	if(programError==1) {
	    blockIndex++;
	    srcPt=saveSrcPt;
	    programError=0;
	    continue;
	}

	if((U32)srcPt>=(srcAddress+targetSize)) break;	// Exit while loop

		blockIndex++;

    }

}


 
 

void InputTargetBlock_MLC(void)
{
	U32 no_block, no_page, no_byte;
	
	printf("\nAvailable target block number: 0~2048\n");
	printf("Input target block number:");
    targetBlock=GetIntNum();	// Block number(0~4095)
    if(targetSize==0)
    {
    #if 0
    	printf("Input target size(0x4000*n):");
    	targetSize=GetIntNum();	// Total byte size
	#else
   	printf("Input program file size(bytes): ");
    	targetSize=GetIntNum();	// Total byte size
    	#endif
    }
	
	no_block = (U32)((targetSize/2048)/128);
	no_page = (U32)((targetSize/2048)%128);
	no_byte = (U32)(targetSize%2048);
	printf("File:%d[%d-block,%d-page,%d-bytes].\n", targetSize, no_block, no_page, no_byte);
}


static int MLC_EraseBlock(U32 block)
{
    U32 blockPage=(block<<7);

    NFConDone_MLC=0;
    rNFCONT|=(1<<9); //Enable RnB Interrupt
    rNFCONT|=(1<<10); //Enable Illegal Access Interrupt
    pISR_NFCON= (unsigned)NFCon_Int_MLC;
    rSRCPND=BIT_NFCON;
    rINTMSK=~(BIT_NFCON);
	NF_nFCE_L();
    
	NF_CMD(0x60);   // Erase one block 1st command, Block Addr:A11-A27
	// 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_CLEAR_RB();
	NF_CMD(0xd0);	// Erase one blcok 2nd command
	
//	NF_DETECT_RB();
	 while(NFConDone_MLC==0);
	 rNFCONT&=~(1<<9);   // Disable RnB Interrupt
	 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();
		printf("[ERASE_ERROR:block#=%d]\n",block);
    	return FAIL;
    }
    else 
    {
    	NF_nFCE_H();
        return OK;
    }
}

void __irq NFCon_Int_MLC(void)
{
   
	
	 NFConDone_MLC=1;
    
    //printf("Interrupt is occurred!!!\n");

    //rINTMSK|=BIT_NFCON;
	ClearPending(BIT_NFCON);
	if(rNFSTAT&0x20) printf("Illegal Access is detected!!!\n");
	else if(rNFSTAT&0x40) 
	{
	       if ((rNFECCERR0&(0x7<<26)) == 0x0){
//	       printf("ECC OK!\n");
	 	   }
		   else {
//		   printf("ECC FAIL!\n");
//		   printf("status0:0x%x|status1:0x%x|bit:0x%x\n", rNFECCERR0, rNFECCERR1, rNFMLCBITPT);
		   }
	 
//		NFECCDecDone=1;
//		rNFSTAT|=0x40;	
//		printf("ECC decoding is completed!!!\n"); 
	}
	else if(rNFSTAT&0x10)
	{
		rNFSTAT|=0x10;
//		printf("RnB is Detected!!!\n");
	}
	 else if(rNFSTAT&0x80) 
	{
		NFECCEncDone=1;
//		rNFSTAT|=0x80;	
		printf("ECC Encoding is completed!!!\n"); 
	}
  }



static int MLC_ReadPage(U32 block,U32 page,U8 *buffer)
{
  	int i;
   	U32 blockPage;
	U8 *bufPt=buffer;
	
	
	NFConDone_MLC=0, NFECCDecDone=0;

	
  //  rNFCONT|=(1<<9); // Enable RnB Interrupt 
  //  rNFCONT|=(1<<10); // Enable Illegal Access Interrupt
    
    rNFCONT|=(1<<13); // Enable ECC encoding conpletion interrupt enable
    rNFCONT|=(1<<12); // Enable ECC decoding conpletion interrupt enable
   	rNFCONF = (rNFCONF & ~(1<<30))|(1<<24); // System Clock is more than 66Mhz, ECC type is MLC.
	rNFCONT &= ~(1<<18); //ECC for reading.
    
   
    pISR_NFCON= (unsigned)NFCon_Int_MLC;
    rSRCPND=BIT_NFCON;
    rINTMSK&=~(BIT_NFCON);
    
    
    /////////////////////////////////////////////////
    // block1, Spare Ecc check /
    /////////////////////////////////////////////////
    
    blockPage=(block<<7)+page;

	NF_RSTECC();    // Initialize ECC
	NF_MECC_UnLock();
    
   	NF_nFCE_L();
	NF_CLEAR_RB();
    
    NF_CMD(0x00);	// Read command
    NF_ADDR((2048+0)&0xff);			// 2060 = 0x080c
	NF_ADDR(((2048+0)>>8)&0xff);		// A[10:8]
    NF_ADDR((blockPage)&0xff);	// A[19:12]
    NF_ADDR((blockPage>>8)&0xff);	// A[27:20]
    NF_ADDR((blockPage>>16)&0xff);	// A[27:20]
    
    NF_CMD(0x30);	// 2'nd command
    
    NF_DETECT_RB();
    
    for(i=0;i<29;i++) {
	   NF_RDDATA8();	// Read one page
	}
	
	NF_nFCE_H(); // read dummy data
	for(i=29;i<512;i++) {
	   NF_WRDATA8(0xff);	// Read one page
	}
	
    NF_nFCE_L(); 
    for(i=0;i<7;i++) {
	   NF_RDDATA8();	// Read one page
	}
      
    while(!(rNFSTAT&(1<<6))) ;
//	while(NFECCDecDone==0);
    rNFSTAT|=(1<<6);
   	printf("Spare ECC check!\n");
 	if ((rNFECCERR0&(0x7<<26)) == 0x0){
	       printf("Spare ECC OK!\n");
//		return;
	 }
	else {
		printf("Spare ECC FAIL!\n");
		printf("status0:0x%x|status1:0x%x|bit:0x%x\n", rNFECCERR0, rNFECCERR1, rNFMLCBITPT);
//	    return;
	 }

	/////////////////////////////////////////////////
    // block1, page1 reading with invalid data, ecc /
    /////////////////////////////////////////////////
   
    
	NF_RSTECC();    // Initialize ECC
	NF_MECC_UnLock();
	
    NF_CMD(0x05);
    NF_ADDR((0)&0xff);			// 2060 = 0x080c
	NF_ADDR(((0)>>8)&0xff);		// A[10:8]
	NF_CMD(0xe0);
//	for(i=0;i<10;i++);

 //   rNFCONT=0x3045;
    
    #if TRANS_MODE_MLC==C_LANG
	    for(i=0;i<512;i++) {
	   	*bufPt++=NF_RDDATA8();	// Read one page
		 }
    #elif TRANS_MODE_MLC==DMA
		// Nand to memory dma setting
	    rSRCPND=BIT_DMA;	// Init DMA src pending.
	    rSUBSRCPND=BIT_SUB_DMA0;
	    rDISRC0=NFDATA; 	// Nand flash data register
	    rDISRCC0=(0<<1) | (1<<0); //arc=AHB,src_addr=fix
	    rDIDST0=(unsigned)bufPt;
	    rDIDSTC0=(0<<1) | (0<<0); //dst=AHB,dst_addr=inc;
	    rDCON0=(1<<31)|(1<<30)|(1<<29)|(1<<28)|(1<<27)|(0<<23)|(1<<22)|(2<<20)|(512/4/4);
		//Handshake,AHB,interrupt,(4-burst),whole,S/W,no_autoreload,word,count=128;

		// DMA on and start.
	    rDMASKTRIG0=(1<<1)|(1<<0);

	   	while(!(rSUBSRCPND & BIT_SUB_DMA0));	// Wait until Dma transfer is done.
		
	   	rSUBSRCPND=BIT_SUB_DMA0;

⌨️ 快捷键说明

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