📄 nand.c
字号:
//====================================================================
// File Name : Nand.c
// Function : S3C2443 8-bit interface Nand Test program
// Date : May 22, 2006
// Version : 0.0
// History
// R0.0 (200305xx): Modified for 2440 from 2410. -> DonGo
// R0.x (2005. 05. 20): Modified by Jcs
//====================================================================
/**************** 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 "system.h"
#include "MLC.h"
#include "Nand.h"
#define BAD_CHECK (0)
#define ECC_CHECK (0)
#define ASM 1
#define C_LANG 2
#define DMA 3
#define TRANS_MODE 2
U8 NF8_Spare_Data[16];
U8 Adv_NF8_Spare_Data[64];
U32 srcAddress;
U32 targetBlock; // Block number (0 ~ 4095)
U32 targetSize; // Total byte size
volatile int NFConDone;
static U8 se8Buf[16]={
0xff,0xff,0xff,0xff,
0xff,0xff,0xff,0xff,
0xff,0xff,0xff,0xff,
0xff,0xff,0xff,0xff
};
static U8 Adv_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
};
void __irq NFCon_Int(void);
//*************************************************
void * n8_func[][2]=
{ // X-D Card (Normal 8bit)
(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 *)NF8_Program, "K9S1208 Program ",
(void *)SRAM_Test, "Stepping stone test ",
0,0
};
void * Adv_n8_func[][2]=
{
// K9F2g08 (Advanced 8bit)
(void *)NF8_Print_Id, "Read ID ",
(void *)Nand_Reset, "Nand reset ",
(void *)Test_Adv_NF8_Block_Erase, "Block erase ",
(void *)Test_Adv_NF8_Page_Read, "Page read ",
(void *)Test_Adv_NF8_Page_Write, "Page write ",
(void *)Test_Adv_NF8_Rw, "Nand R/W test ",
(void *)Adv_NF8_PrintBadBlockNum, "Check Badblock ",
(void *)Test_Adv_NF8_Lock, "Nand Block lock ",
(void *)Test_Adv_NF8_SoftUnLock, "Soft Unlock ",
(void *)Adv_NF8_Program, "K9F2G08 Program ",
0,0
};
void Test_NAND(void)
{
U8 ch;
printf("Nand test\n");
#ifdef __EVT1
;
#else
rGPACON = (rGPACON &~(0x3f<<17)) | (0x3f<<17);
// GPA 22 21 20 19 18 17
// nFCE nRSTOUT nFRE nFWE ALE CLE
rGPMCON = rGPMCON & ~(0x3<<2)|(0x2<<2);
// GPM 1
// FRnB
#endif
printf("Select Nand flash type!!!\n ");
printf("K9S1208: 8bit Small block (1) / K9F2G08: 8bit Large Block(2)\n");
printf("K9K2G16: 16bit Large Block(3) / K9HBG08: 8bit MLC Nand0(4): ");
ch=getchar();
// printf("%c\n\n", ch);
switch(ch) {
case '1':
Test_K9S1208(); // in Nand.c
break;
case '2':
Test_K9F2G08();
break;
case '3':
Test_K9K2G16();
break;
case '4':
Test_K9HBG08(); //MLC
break;
default:
break;
}
#ifdef __EVT1
;
#else
rGPACON = gpacon;
rGPMCON = gpmcon;
#endif
}
void Test_K9S1208(void)
{
int i;
printf("\nK9S1208 Nand flash test start.\n");
NF8_Init();
while(1) {
PrintSubMessage();
printf("\nSelect(-1 to exit): ");
i = GetIntNum();
if(i==-1) break;
if(i>=0 && (i<(sizeof(n8_func)/8)) )
( (void (*)(void)) (n8_func[i][0]) )(); // execute selected function.
}
}
void Test_K9F2G08(void)
{
int i;
printf("\nK9F2G08 Nand flash test start.\n");
NF8_Init();
while(1) {
Adv_PrintSubMessage();
printf("\nSelect(-1 to exit): ");
i = GetIntNum();
if(i==-1) break;
if(i>=0 && (i<(sizeof(Adv_n8_func)/8)) )
( (void (*)(void)) (Adv_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)
{
U16 id;
U8 maker, device;
// NF8_Init();
id = NF8_CheckId();
device = (U8)id;
maker = (U8)(id>>8);
printf("Maker:%x, Device:%x\n", maker, device);
}
void Test_NF8_Block_Erase(void)
{
U32 block=0;
printf("SMC(K9S1208V0M) NAND Block erase\n");
/*
if((Read_Status()&0x80)==0) {
printf("Write protected.\n");
return;
}
*/
printf("Block # to erase: ");
block = GetIntNum();
// NF8_Init();
if(NF8_EraseBlock(block)==FAIL) return;
printf("%d-block erased.\n", block);
}
void Test_Adv_NF8_Block_Erase(void)
{
U32 block=0;
printf("(K9F2G08U0M) NAND Block erase\n");
/*
if((Read_Status()&0x80)==0) {
printf("Write protected.\n");
return;
}
*/
printf("Block # to erase: ");
block = GetIntNum();
// NF8_Init();
if(Adv_NF8_EraseBlock(block)==FAIL) return;
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;
printf(" NAND(xD-Picture Card) Page Read.\n");
printf("Block # to read: ");
block = GetIntNum();
printf("Page # to read: ");
page = GetIntNum();
if(NF8_ReadPage(block, page, (U8 *)downPt )==FAIL) {
printf("Read error.\n");
} else {
printf("Read OK.\n");
}
// Print data.
printf("Read data(%d-block,%d-page)\n", block, page);
printf("NFMECC0: 0x%x\n", rNFMECC0);
for(i=0; i<512; i++) {
if((i%16)==0) printf("\n%4x: ", i);
printf("%02x ", *(U8 *)downPt++);
}
printf("\n");
printf("Spare:");
for(i=0; i<16; i++) {
printf("%02x ", NF8_Spare_Data[i]);
}
printf("\n");
}
/////////////// Advanced nand flash /////////////////////
void Test_Adv_NF8_Page_Read(void)
{
U32 block=0, page=0;
U32 i;
unsigned char * downPt;
downPt=(unsigned char *)_NONCACHE_STARTADDRESS;
rGPHCON=rGPHCON&~(0xf<<26)|(0x5<<26); // GPH13, 14 => OUTPUT
rGPHDAT&=~(0x3<<13);
printf("(K9F2G08) NAND Page Read.\n");
printf("Block # to read: ");
block = GetIntNum();
printf("Page # to read: ");
page = GetIntNum();
Adv_NF8_ReadPage(block, page, (U8 *)downPt);
if(Adv_NF8_ReadPage(block, page, (U8 *)downPt )==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 ", *(U8 *)downPt++);
}
printf("\n");
printf("Spare:");
for(i=0; i<64; i++) {
if((i%16)==0) printf("\n%4x: ", i);
printf("%02x ", Adv_NF8_Spare_Data[i]);
}
printf("\n");
}
void Test_NF8_Page_Write(void)
{
U32 block=0, page=0;
int i, offset;
unsigned char * srcPt;
srcPt=(unsigned char *)0x31100000;
printf("SMC(K9S1208V0M) NAND Page Write.\n");
printf("You must erase block before you write data!!! \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<512; i++) {
#if ADS10==TRUE
if(offset==-1) *srcPt++ = rand()%0xff;
#else
if(offset==-1) *srcPt++ = i%0xff;
#endif
else *srcPt++ = i+offset;
}
srcPt=(unsigned char *)0x31100000;
printf("Write data[%d block, %d page].\n", block, page);
if(NF8_WritePage(block, page, srcPt)==FAIL) {
printf("Write Error.\n");
} else {
printf("Write OK.\n");
}
printf("Write data is");
for(i=0; i<512; i++) {
if((i%16)==0) printf("\n%4x: ", i);
printf("%02x ", *srcPt++);
}
printf("\n");
printf("Spare:");
for(i=0; i<16; i++) {
printf("%02x ", NF8_Spare_Data[i]);
}
printf("\n\n");
}
///////////////////Advanced nand flash//////////////////////////////
void Test_Adv_NF8_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("SMC(K9K2G08U) NAND Page Write.\n");
Adv_NF8_WritePage(block, page, srcPt);
printf("You must erase block before you write data!!! \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()%0xff;
#else
if(offset==-1) *srcPt++ = i%0xff;
#endif
else *srcPt++ = i+offset;
}
srcPt=(unsigned char *)0x31100000;
printf("Write data[%d block, %d page].\n", block, page);
if(Adv_NF8_WritePage(block, page, 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 ", Adv_NF8_Spare_Data[i]);
}
printf("\n\n");
}
void Test_NF8_Rw(void)
{
U32 count=0;
U32 block=0, page=0;
U32 i,error;
int offset=0;
unsigned char *srcPt, *dstPt;
srcPt=(unsigned char *)0x31100000;
dstPt=(unsigned char *)0x31200000;
printf("Block number: ");
block = GetIntNum();
printf("Page nember: ");
page = GetIntNum();
printf("offset data(-1:random): ");
offset = GetIntNum();
#if ADS10==TRUE
srand(0);
#endif
// Init R/W data.
for(i=0; i<512; i++) *dstPt++=0x0;
for(i=0; i<512; i++) {
#if ADS10==TRUE
if(offset==-1) *srcPt++ = rand()%0xff;
#else
if(offset==-1) *srcPt++ = i%0xff;
#endif
else *srcPt++ = i+offset;
}
srcPt=(unsigned char *)0x31100000;
dstPt=(unsigned char *)0x31200000;
// Block erase
printf("%d block erase.\n", block);
if(NF8_EraseBlock(block)==FAIL) return;
srcPt=(unsigned char *)0x31100000;
dstPt=(unsigned char *)0x31200000;
printf("Write data[%d block, %d page].\n", block, page);
if(NF8_WritePage(block, page, srcPt)==FAIL) return;
printf("Read data.\n");
if(NF8_ReadPage(block, page, dstPt)==FAIL) return;
printf("Checking data.\n");
for(error=0, i=0; i<512; i++) {
if(*srcPt++!=*dstPt++) {
printf("Error:%d[W:%x,R:%x]\n", i, *srcPt, *dstPt);
error++;
}
}
if(error!=0)
{
printf("Fail to R/W test(%d).\n", error);
printf("Write data[%d block, %d page, 0x%x count].\n", block, page, count);
}
else printf(" %d pageR/W test OK.\n",page);
}
void Test_Adv_NF8_Rw(void)
{
U32 block=0, page=0;
U32 i,error;
int offset;
unsigned char *srcPt, *dstPt;
srcPt=(unsigned char *)0x31100000;
dstPt=(unsigned char *)0x31200000;
printf("K9F2G08 R/W test.\n");
printf("Block number: ");
block = GetIntNum();
printf("Page nember: ");
page = GetIntNum();
printf("offset data(-1:random): ");
offset = GetIntNum();
#if ADS10==TRUE
srand(0);
#endif
// Init R/W data.
for(i=0; i<2048; i++) *dstPt++=0x0;
for(i=0; i<2048; i++) {
#if ADS10==TRUE
if(offset==-1) *srcPt++ = rand()%0xff;
#else
if(offset==-1) *srcPt++ = i%0xff;
#endif
else *srcPt++ = i+offset;
}
srcPt=(unsigned char *)0x31100000;
dstPt=(unsigned char *)0x31200000;
// Block erase
printf("%d block erase.\n", block);
if(Adv_NF8_EraseBlock(block)==FAIL) return;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -