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

📄 nand.c

📁 Embest IDE下s3c2440的测试工程
💻 C
📖 第 1 页 / 共 4 页
字号:
//====================================================================
// 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]
/*****************************************************/
/**************** K9F1G08 NAND flash *******************/
// 1block=(2048+64)bytes x 64pages
// 1024Block
// Block: A[23:14], page: [13:9]
/*****************************************************/
#include <math.h>
#include <string.h>
#include <stdlib.h>
#include "def.h"
#include "option.h"
#include "2440addr.h"
#include "2440lib.h"

#include "loader.h"
#include "Nand.h"


U8 NF8_Spare_Data[64];

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

extern U32 downloadAddress;
U32 downloadProgramSize;
volatile int NFConDone;


static U8 se8Buf[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
};
static U8 vectors[] = {
	0x6f, 0x00, 0x00, 0xea,
	0x4d, 0x00, 0x00, 0xea,
	0x52, 0x00, 0x00, 0xea,
	0x5d, 0x00, 0x00, 0xea,
	0x56, 0x00, 0x00, 0xea,
	0xfe, 0xff, 0xff, 0xea,
	0x42, 0x00, 0x00, 0xea,
	0x3b, 0x00, 0x00, 0xea,
	0x08, 0x00, 0x00, 0xea
};

static U32 se16Buf[32/2]={
	0xffffffff,0xffffffff,0xffffffff,0xffffffff,
	0xffffffff,0xffffffff,0xffffffff,0xffffffff,
	0xffffffff,0xffffffff,0xffffffff,0xffffffff,
	0xffffffff,0xffffffff,0xffffffff,0xffffffff
};

void __irq NFCon_Int(void);


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



void * n8_func[][2]=
{
	(void *)NF8_Print_Id,			"Read ID         ",
	(void *)Nand_Reset,				"Nand reset      ",
	(void *)Test_NF8_Block_Erase,	"Block erase     ",
	(void *)Test_NF8_Page_Read,		"Page read       ",
	(void *)Test_NF8_Page_Write,	"Page write      ",
	(void *)Test_NF8_Rw,			"Nand R/W test   ",
	(void *) NF8_PrintBadBlockNum,	"Check Badblock  ",
	(void *)Test_NF8_Lock,			"Nand Block lock ",
	(void *)Test_NF8_SoftUnLock,	"Soft Unlock     ",
	(void *)NandFlashProgram,			"Nandflash Program ",	
//	(void *)NandFlashFormatForFAT,	"NandFlash Format for FAT",
	0,0
};

void Test_Nand(void)
{
	U8 ch;
	U32 gpacon;
	
	Uart_Printf("Nand test\n");

	gpacon = rGPACON;

	rGPACON = (rGPACON &~(0x3f<<17)) | (0x3f<<17);
	// GPA     22         21           20      19     18     17
	//           nFCE   nRSTOUT  nFRE  nFWE   ALE   CLE

	Uart_Printf("Select Nand flash type, Normal(1)/Advanced:K9F1G08(2) K9K8G08(3): ");
#if 1
	ch=Uart_Getch();
#else
	ch = '3';
#endif
	Uart_Printf("%c\n\n", ch);
	
	switch(ch) {
		case '1':
			Test_K9S1208();		// in Nand.c
		break;
		case '2':
			Test_K9F1G08();		//in Nand.c
		break;
		case '3':
			Test_K9K8G08();		//in K9K8G08.c
		break;
		default:
		break;
	}

	rGPACON = gpacon;
}


void Test_K9S1208(void)
{
	int i;
	U8 ch;

	Uart_Printf("\nK9S1208 Nand flash test start.\n");

	NF8_Init();

	while(1) {
		PrintSubMessage();
		Uart_Printf("\nSelect(-1 to exit): ");
		i = Uart_GetIntNum();
		if(i==-1) break;
		
		if(i>=0 && (i<(sizeof(n8_func)/8)) ) 
	    	( (void (*)(void)) (n8_func[i][0]) )();	// execute selected function.
	}

}

void Test_K9F1G08(void)
{
	int i;
	U8 ch;

	Uart_Printf("\nTest_K9F1G08 Nand flash test start.\n");

	NF8_Init();

	while(1) {
#if 0
        	PrintSubMessage();
		Uart_Printf("\nSelect(-1 to exit): ");
		i = Uart_GetIntNum();
#else
		i = 9;
#endif
	if(i==-1) break;
		
		if(i>=0 && (i<(sizeof(n8_func)/8)) ) 
	    	( (void (*)(void)) (n8_func[i][0]) )();	// execute selected function.
	}

}


void Test_K9K8G08(void)
{
	int i;
	U8 ch;

	Uart_Printf("\nTest_K9K8G08 Nand flash test start.\n");

	NF8_Init();

	while(1) {
#if 1
        	PrintSubMessage();
		Uart_Printf("\nSelect(-1 to exit): ");
		i = Uart_GetIntNum();
#else
		i = 9;
#endif
	if(i==-1) break;
		
		if(i>=0 && (i<(sizeof(n8_func)/8)) ) 
	    	( (void (*)(void)) (n8_func[i][0]) )();	// execute selected function.
	}

}

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;
}

void NF8_Print_Id(void) //NF16_Print_Id
{
	U16 id;
	U8 maker, device;

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


void Test_NF8_Block_Erase(void)
{
	U32 block=0;
#if ID_K9F1G08U0A
	Uart_Printf("SMC(K9F1G08U0A) NAND Block erase\n");
#elif ID_K9S1208V0M
	Uart_Printf("SMC(K9S1208V0M) NAND Block erase\n");
#elif ID_NAND == ID_K9K8G08U0A
	Uart_Printf("SMC(ID_K9K8G08U0A) NAND Block erase\n");
#endif
	if((Read_Status()&0x80)==0) {
		Uart_Printf("Write protected.\n");
		return;
	}

	Uart_Printf("Block # to erase: ");
	block = Uart_GetIntNum();


//	NF8_Init();
	
	if(NF8_EraseBlock(block)==FAIL) return;

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

}

void Test_NF8_Page_Read(void)
{
	U32 block=0, page=0;
	U32 i;
	unsigned char * downPt;
	
	downPt=(unsigned char *)_NONCACHE_STARTADDRESS;
#if ID_NAND == ID_K9F1G08U0A
	Uart_Printf("SMC(K9F1G08U0A) NAND Page Read.\n");

	Uart_Printf("Block # to read: ");
	block = Uart_GetIntNum();
	Uart_Printf("Page # to read: ");
	page = Uart_GetIntNum();

	if(NF8_ReadPage(block, page, (U8 *)downPt )==FAIL) {
		Uart_Printf("Read error.\n");
	} else {
		Uart_Printf("Read OK.\n");
	}
	// Print data.
	Uart_Printf("Read data(%d-block,%d-page)\n", block, page);
	
	for(i=0; i<2048; i++) {
		if((i%16)==0) Uart_Printf("\n%4x: ", i);
		Uart_Printf("%02x ", *(U8 *)downPt++);
		}
	Uart_Printf("\n");
	Uart_Printf("Spare:");
	for(i=0; i<64; i++) {
	 	Uart_Printf("%02x ", NF8_Spare_Data[i]);
	}
#elif ID_NAND == ID_K9K8G08U0A
	Uart_Printf("SMC(K9K8G08U0A) NAND Page Read.\n");

	Uart_Printf("Block # to read: ");
	block = Uart_GetIntNum();
	Uart_Printf("Page # to read: ");
	page = Uart_GetIntNum();

	if(NF8_ReadPage(block, page, (U8 *)downPt )==FAIL) {
		Uart_Printf("Read error.\n");
	} else {
		Uart_Printf("Read OK.\n");
	}
	// Print data.
	Uart_Printf("Read data(%d-block,%d-page)\n", block, page);
	
	for(i=0; i<2048; i++) {
		if((i%16)==0) Uart_Printf("\n%4x: ", i);
		Uart_Printf("%02x ", *(U8 *)downPt++);
		}
	Uart_Printf("\n");
	Uart_Printf("Spare:");
	for(i=0; i<64; i++) {
	 	Uart_Printf("%02x ", NF8_Spare_Data[i]);
	}
#elif ID_NAND == ID_K9S1208V0M
	Uart_Printf("SMC(K9S1208V0M) NAND Page Read.\n");

	Uart_Printf("Block # to read: ");
	block = Uart_GetIntNum();
	Uart_Printf("Page # to read: ");
	page = Uart_GetIntNum();

	if(NF8_ReadPage(block, page, (U8 *)downPt )==FAIL) {
		Uart_Printf("Read error.\n");
	} else {
		Uart_Printf("Read OK.\n");
	}
	// Print data.
	Uart_Printf("Read data(%d-block,%d-page)\n", block, page);
	
	for(i=0; i<512; i++) {
		if((i%16)==0) Uart_Printf("\n%4x: ", i);
		Uart_Printf("%02x ", *(U8 *)downPt++);
		}
	Uart_Printf("\n");
	Uart_Printf("Spare:");
	for(i=0; i<16; i++) {
	 	Uart_Printf("%02x ", NF8_Spare_Data[i]);
	}
#endif
	Uart_Printf("\n");

}

void Test_NF8_Page_Write(void)
{
	U32 block=0, page=0;
	int i, offset;
	unsigned char * srcPt;
	srcPt=(unsigned char *)0x31100000;
#if ID_NAND == ID_K9F1G08U0A
	Uart_Printf("SMC(K9F1G08U0A) NAND Page Write.\n");
	Uart_Printf("You must erase block before you write data!!! \n");
	Uart_Printf("Block # to write: ");
	block = Uart_GetIntNum();
	Uart_Printf("Page # to write: ");
	page = Uart_GetIntNum();
	Uart_Printf("offset data(-1:random): ");
	offset = 0;//Uart_GetIntNum();

	// Init wdata.
	for(i=0; i<2048; i++) {

		if(offset==-1) *srcPt++ = i%0xff;

		else *srcPt++ =  i+offset;
	}
       srcPt=(unsigned char *)0x31100000;
	Uart_Printf("Write data[%d block, %d page].\n", block, page);
	if(NF8_WritePage(block, page, srcPt)==FAIL) {
		Uart_Printf("Write Error.\n");
	} else {
		Uart_Printf("Write OK.\n");
	}

 	Uart_Printf("Write data is");
	for(i=0; i<2048; i++) {
		if((i%16)==0) Uart_Printf("\n%4x: ", i);
		Uart_Printf("%02x ", *srcPt++);
	}
	Uart_Printf("\n");

	Uart_Printf("Spare:");
	for(i=0; i<64; i++) {
	 	Uart_Printf("%02x ", NF8_Spare_Data[i]);
	}
#elif ID_NAND == ID_K9K8G08U0A
	Uart_Printf("SMC(K9K8G08U0A) NAND Page Write.\n");
	Uart_Printf("You must erase block before you write data!!! \n");
	Uart_Printf("Block # to write: ");
	block = Uart_GetIntNum();
	Uart_Printf("Page # to write: ");
	page = Uart_GetIntNum();
	Uart_Printf("offset data(-1:random): ");
	offset = 0;//Uart_GetIntNum();

	// Init wdata.
	for(i=0; i<2048; i++) {

		if(offset==-1) *srcPt++ = i%0xff;

		else *srcPt++ =  i+offset;
	}
       srcPt=(unsigned char *)0x31100000;
	Uart_Printf("Write data[%d block, %d page].\n", block, page);
	if(NF8_WritePage(block, page, srcPt)==FAIL) {
		Uart_Printf("Write Error.\n");
	} else {
		Uart_Printf("Write OK.\n");
	}

 	Uart_Printf("Write data is");
	for(i=0; i<2048; i++) {
		if((i%16)==0) Uart_Printf("\n%4x: ", i);
		Uart_Printf("%02x ", *srcPt++);
	}
	Uart_Printf("\n");

	Uart_Printf("Spare:");
	for(i=0; i<64; i++) {
	 	Uart_Printf("%02x ", NF8_Spare_Data[i]);
	}
#elif ID_NAND == ID_K9S1208V0M		
	Uart_Printf("SMC(K9S1208V0M) NAND Page Write.\n");
	Uart_Printf("You must erase block before you write data!!! \n");

	Uart_Printf("Block # to write: ");
	block = Uart_GetIntNum();
	Uart_Printf("Page # to write: ");
	page = Uart_GetIntNum();
	Uart_Printf("offset data(-1:random): ");
	offset = 0;//Uart_GetIntNum();

	// Init wdata.
	for(i=0; i<512; i++) {

		if(offset==-1) *srcPt++ = i%0xff;

		else *srcPt++ =  i+offset;
	}
       srcPt=(unsigned char *)0x31100000;
	Uart_Printf("Write data[%d block, %d page].\n", block, page);
	if(NF8_WritePage(block, page, srcPt)==FAIL) {
		Uart_Printf("Write Error.\n");
	} else {
		Uart_Printf("Write OK.\n");
	}

 	Uart_Printf("Write data is");
	for(i=0; i<512; i++) {
		if((i%16)==0) Uart_Printf("\n%4x: ", i);
		Uart_Printf("%02x ", *srcPt++);
	}
	Uart_Printf("\n");

	Uart_Printf("Spare:");
	for(i=0; i<16; i++) {
	 	Uart_Printf("%02x ", NF8_Spare_Data[i]);
	}
#endif
	Uart_Printf("\n\n");

}


void Test_NF8_Rw(void)
{
	U32 block=0, page=0;
	U32 i, status=FAIL, error, offset;
	unsigned char *srcPt, *dstPt;
	srcPt=(unsigned char *)0x31100000;
	dstPt=(unsigned char *)0x31200000;
		
	Uart_Printf("NAND Flash R/W test.\n");

	Uart_Printf("Block number: ");
	block = Uart_GetIntNum();
	Uart_Printf("Page nember: ");
	page = Uart_GetIntNum();
	Uart_Printf("offset data(-1:random): ");
	offset = Uart_GetIntNum();
	
	// Init R/W data.
	for(i=0; i<2048; i++) *dstPt++=0x0;

	for(i =0; i<36; i++){
	*srcPt++ =0;// vectors[i];
	}
	for(i=36; i<2048; i++) {

		if(offset==-1) *srcPt++ = i%0xff;

		else *srcPt++ =0;// i+offset;
	}
	


	srcPt=(unsigned char *)0x31100000;
	dstPt=(unsigned char *)0x31200000;
	// Block erase
	Uart_Printf("%d block erase.\n", block);
	if(NF8_EraseBlock(block)==FAIL) return;

	Uart_Printf("Read data.\n");
	if(NF8_ReadPage(block, page, dstPt)==FAIL) return;

	Uart_Printf("Write data[%d block, %d page].\n", block, page);
	if(NF8_WritePage(block, page, srcPt)==FAIL) return;
	
	Uart_Printf("Read data.\n");
	if(NF8_ReadPage(block, page, dstPt)==FAIL) return;

	Uart_Printf("Checking data.\n");
	for(error=0, i=0; i<2048; i++) {
		if(*srcPt++!=*dstPt++) {
			Uart_Printf("Error:%d[W:%x,R:%x]\n", i, *srcPt, *dstPt);
			error++;
		}
	}
	if(error!=0) 
		Uart_Printf("Fail to R/W test(%d).\n", error);
	else
		Uart_Printf("R/W test OK.\n");
}


void NandFlashProgram(U32 block, U32 filesize)
{
//    unsigned long interrupt_reservoir;
    int i;
    int programError=0;
	U8 *srcPt,*saveSrcPt;
	U32 blockIndex;
		U32 no_block, no_page, no_byte;

  	Uart_Printf("\n[SMC NAND Flash writing program]\n");
       Uart_Printf("The program buffer: 0x30200000~0x32ffffff\n");

⌨️ 快捷键说明

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