📄 k9f1g08.c
字号:
//====================================================================
// File Name : Nand.c
// Function : S3C2442 8-bit interface Nand Test program(this program use K9F1G08.c).
// Date : Nov xx, 2004
// Version : 0.0
// History
// R0.0 (200305xx): Modified for 2442 from 2410. -> DonGo
//====================================================================
/**************** K9s1208 NAND flash ********************/
// 1block=(512+16)bytes x 32pages
// 4096block
/**************** K9F1G08 NAND flash *******************/
// 1block=(2048+64)bytes x 64pages
// 1024block
/*****************************************************/
#include <math.h>
#include <string.h>
#include <stdlib.h>
#include "def.h"
#include "option.h"
#include "2442addr.h"
#include "2442lib.h"
#include "2442slib.h"
//#include "Nand.h"
#include "K9F1G08.h"
#define BAD_CHECK (0)
#define ECC_CHECK (0)
#define C_LANG 1
#define DMA 2
#define TRANS_MODE 2
U8 NF1G08_Spare_Data[64];
extern U32 srcAddress;
extern U32 targetBlock; // Block number (0 ~ 4095)
extern U32 targetSize; // Total byte size
extern U32 downloadAddress;
extern U32 downloadProgramSize;
volatile int NFConDone_1G08;
//*************************************************
//*************************************************
//** H/W dependent functions **
//*************************************************
//*************************************************
//The code is made for bi-endian mode
#define NF_MECC_UnLock() {rNFCONT&=~(1<<5);}
#define NF_MECC_Lock() {rNFCONT|=(1<<5);}
#define NF_SECC_UnLock() {rNFCONT&=~(1<<6);}
#define NF_SECC_Lock() {rNFCONT|=(1<<6);}
#define NF_CMD(cmd) {rNFCMD=cmd;}
#define NF_ADDR(addr) {rNFADDR=addr;}
#define NF_nFCE_L() {rNFCONT&=~(1<<1);}
#define NF_nFCE_H() {rNFCONT|=(1<<1);}
#define EXT_NF_nFCE_L() {rGPADAT&=~(1<<16);}
#define EXT_NF_nFCE_H() {rGPADAT|=(1<<16);}
#define NF_RSTECC() {rNFCONT|=(1<<4);}
#define NF_RDDATA() (rNFDATA)
#define NF_RDDATA8() ((*(volatile unsigned char*)0x4E000010) )
#define NF_WRDATA(data) {rNFDATA=data;}
#define NF_WRDATA8(data) {rNFDATA8=data;}
// RnB Signal
#define NF_CLEAR_RB() {rNFSTAT |= (1<<2);} // Have write '1' to clear this bit.
#define NF_DETECT_RB() {while(!(rNFSTAT&(1<<2)));}
// HCLK=100Mhz
#define TACLS 7 // 1-clk(0ns)
#define TWRPH0 7 // 3-clk(25ns)
#define TWRPH1 7 // 1-clk(10ns) //TACLS+TWRPH0+TWRPH1>=50ns
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
};
void __irq NFCon_Int_1G08(void);
//*************************************************
void * n1G08_func[][2]=
{
(void *)NF1G08_Print_Id, "Read ID ",
(void *)Nand_Reset, "Nand reset ",
(void *)Test_NF1G08_Block_Erase, "Block erase ",
(void *)Test_NF1G08_Page_Read, "Page read ",
(void *)Test_NF1G08_Page_Write, "Page write ",
(void *)Test_NF1G08_Rw, "Nand R/W test ",
(void *) NF1G08_PrintBadBlockNum, "Check Badblock ",
(void *)Test_NF1G08_Lock, "Nand Block lock ",
(void *)Test_NF1G08_SoftUnLock, "Soft Unlock ",
(void *)NF1G08_Program, "K9F1G08 Program ",
0,0
};
/*
void Test_Nand(void)
{
U8 ch;
U32 gpacon;
Uart_Printf("Nand test\n");
rGPADAT|=(1<<16); //
rGPACON=rGPACON&~(0x1<<16); // nGCS[5] -> OUTPUT
gpacon = rGPACON;
rGPACON = (rGPACON &~(0x3f<<17)) | (0x3f<<17);
// GPA 22 21 20 19 18 17
// nFCE nRSTOUT nFRE nFWE ALE CLE
Uart_Printf("NFSTAT: 0x%x\n", rNFSTAT);
Uart_Printf("Select Nand flash type, Normal(1)/Advanced(2) : ");
ch=Uart_Getch();
Uart_Printf("%c\n\n", ch);
switch(ch) {
case '1':
Test_K9S1208(); // in Nand.c
break;
case '2':
Test_K9F1G08(); // in K9F1G08.c
break;
default:
break;
}
rGPACON = gpacon;
}
*/
void Test_K9F1G08(void)
{
int i;
U8 ch;
Uart_Printf("\nK9F1G08 Nand flash test start.\n");
NF1G08_Init();
while(1) {
PrintSubMessage();
Uart_Printf("\nSelect(-1 to exit): ");
i = Uart_GetIntNum();
if(i==-1) break;
if(i>=0 && (i<(sizeof(n1G08_func)/8)) )
( (void (*)(void)) (n1G08_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();
EXT_NF_nFCE_H();
return ch;
}
*/
void NF1G08_Print_Id(void)
{
U16 id;
U8 maker, device;
// NF1G08_Init();
id = NF1G08_CheckId();
device = (U8)id;
maker = (U8)(id>>8);
Uart_Printf("Maker:%x, Device:%x\n", maker, device);
}
void Test_NF1G08_Block_Erase(void)
{
U32 block=0;
Uart_Printf("(K9F1G08) NAND Block erase\n");
if((Read_Status()&0x80)==0) {
Uart_Printf("Write protected.\n");
return;
}
Uart_Printf("Block # to erase: ");
block = Uart_GetIntNum();
// NF8_Init();
if(NF1G08_EraseBlock(block)==FAIL) return;
Uart_Printf("%d-block erased.\n", block);
}
void Test_NF1G08_Page_Read(void)
{
U32 block=0, page=0;
U32 i;
unsigned char * downPt;
downPt=(unsigned char *)_NONCACHE_STARTADDRESS;
Uart_Printf("(K9F1G08) NAND Page Read.\n");
Uart_Printf("Block # to read: ");
block = Uart_GetIntNum();
Uart_Printf("Page # to read: ");
page = Uart_GetIntNum();
if(NF1G08_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++) {
if((i%16)==0) Uart_Printf("\n%4x: ", i);
Uart_Printf("%02x ", NF1G08_Spare_Data[i]);
}
Uart_Printf("\n");
}
void Test_NF1G08_Page_Write(void)
{
U32 block=0, page=0;
int i, offset;
unsigned char * srcPt;
srcPt=(unsigned char *)0x31100000;
Uart_Printf("(K9F1G08) 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 = Uart_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;
Uart_Printf("Write data[%d block, %d page].\n", block, page);
if(NF1G08_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++) {
if((i%16)==0) Uart_Printf("\n%4x: ", i);
Uart_Printf("%02x ", NF1G08_Spare_Data[i]);
}
Uart_Printf("\n\n");
}
void Test_NF1G08_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("(K9F1G08) 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();
#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
Uart_Printf("%d block erase.\n", block);
if(NF1G08_EraseBlock(block)==FAIL) return;
Uart_Printf("Write data[%d block, %d page].\n", block, page);
if(NF1G08_WritePage(block, page, srcPt)==FAIL) return;
Uart_Printf("Read data.\n");
if(NF1G08_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 NF1G08_Program(void)
{
// unsigned long interrupt_reservoir;
int i;
int programError=0;
U8 *srcPt,*saveSrcPt;
U32 blockIndex;
Uart_Printf("\n[(K9F1G08) NAND Flash writing program]\n");
Uart_Printf("The program buffer: 0x30100000~0x31ffffff\n");
// NF8_Init();
rINTMSK = BIT_ALLMSK;
srcAddress=0x30100000;
InputTargetBlock();
srcPt=(U8 *)srcAddress;
blockIndex=targetBlock;
while(1) {
saveSrcPt=srcPt;
#if BAD_CHECK
if(NF1G08_IsBadBlock(blockIndex)==FAIL) {
blockIndex++; // for next block
continue;
}
#endif
if(NF1G08_EraseBlock(blockIndex)==FAIL) {
blockIndex++; // for next block
continue;
}
// After 1-Block erase, Write 1-Block(32 pages).
for(i=0;i<64;i++) {
if(NF1G08_WritePage(blockIndex,i,srcPt)==FAIL) {// block num, page num, buffer
programError=1;
break;
}
#if ECC_CHECK
if(NF1G08_ReadPage(blockIndex,i,srcPt)==FAIL) {
Uart_Printf("ECC Error(block=%d,page=%d!!!\n",blockIndex,i);
}
#endif
srcPt+=2048; // Increase buffer addr one pase size
if(i==0) Uart_Printf(".");
//Uart_Printf("\b\b\b\b\b\b\b\b%04d,%02d]", blockIndex, i);
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(void)
{
U32 no_block, no_page, no_byte;
Uart_Printf("\nSource size:0h~%xh\n",downloadProgramSize);
Uart_Printf("\nAvailable target block number: 0~1023\n");
Uart_Printf("Input target block number:");
targetBlock=Uart_GetIntNum(); // Block number(0~4095)
if(targetSize==0)
{
#if 0
Uart_Printf("Input target size(0x20000*n):");
targetSize=Uart_GetIntNum(); // Total byte size
#else
Uart_Printf("Input program file size(bytes): ");
targetSize=Uart_GetIntNum(); // Total byte size
#endif
}
no_block = (U32)((targetSize/2048)/64);
no_page = (U32)((targetSize/2048)%64);
no_byte = (U32)(targetSize%2048);
Uart_Printf("File:%d[%d-block,%d-page,%d-bytes].\n", targetSize, no_block, no_page, no_byte);
}
*/
void NF1G08_PrintBadBlockNum(void)
{
int i;
U16 id;
Uart_Printf("\n[(K9F1G08) NAND Flash bad block check]\n");
id=NF1G08_CheckId();
Uart_Printf("ID=%x(0xeca1)\n",id);
if(id!=0xeca1)
return;
for(i=0;i<1024;i++) NF1G08_IsBadBlock(i); // Print bad block
}
void Test_NF1G08_Lock(void)
{
U32 num;
U32 S_block, E_block;
Uart_Printf("(K9F1G08) 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<<6);
rNFEBLK=(E_block<<6);
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 S3C2442!!!\n ");
}
Uart_Printf("%d block ~ %d block are Programmable\n ", S_block, (E_block-1));
}
void Test_NF1G08_SoftUnLock(void)
{
U32 S_block, E_block;
Uart_Printf("(K9F1G08) NAND SoftUnLock Test !!!\n");
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>>6), ((rNFEBLK>>6)-1));
}
else Uart_Printf("All blocks are Programmable\n ");
rNFSBLK=0x0;
rNFEBLK=0x0;
}
/*
void PrintSubMessage(void)
{
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -