?? nand.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 <math.h>
#include <string.h>
#include <stdlib.h>
#include "def.h"
#include "option.h"
#include "2440addr.h"
#include "2440lib.h"
#include "2440slib.h"
#include "Nand.h"
#include "k9k2g16.h"
#define BAD_CHECK (0)
#define ECC_CHECK (0)
#define ASM 1
#define C_LANG 2
#define DMA 3
#define TRANS_MODE 3
U8 NF8_Spare_Data[16];
U32 srcAddress;
U32 targetBlock; // Block number (0 ~ 4095)
U32 targetSize; // Total byte size
extern U32 downloadAddress;
extern U32 downloadProgramSize;
volatile int NFConDone;
//*************************************************
//*************************************************
//** H/W dependent functions **
//*************************************************
//*************************************************
//The code is made for bi-endian mode
// block0: reserved for boot strap
// block1~4095: used for OS image
// badblock SE: xx xx xx xx xx 00 ....
// good block SE: ECC0 ECC1 ECC2 FF FF FF ....
//============= spare area configuration ====================================
// 0 1 2 3 4 5 6 7 8 9 0xa ...
// Mecc0 Mecc1 Mecc2 FF FF Bad Block FF FF Secc0 Secc1 FF
//====================================================================
#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 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)));}
#define ID_K9S1208V0M 0xec76
#define ID_K9K2G16U0M 0xecca
// 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[16]={
0xff,0xff,0xff,0xff,
0xff,0xff,0xff,0xff,
0xff,0xff,0xff,0xff,
0xff,0xff,0xff,0xff
};
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 *)NF8_Program, "K9S1208 Program ",
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(2) : ");
ch=Uart_Getch();
Uart_Printf("%c\n\n", ch);
switch(ch) {
case '1':
Test_K9S1208(); // in Nand.c
break;
case '2':
Test_K9K2G16(); // in K9K2h16.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.
}
}
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);
Uart_Printf("Maker:%x, Device:%x\n", maker, device);
}
void Test_NF8_Block_Erase(void)
{
U32 block=0;
Uart_Printf("SMC(K9S1208V0M) 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(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;
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]);
}
Uart_Printf("\n");
}
void Test_NF8_Page_Write(void)
{
U32 block=0, page=0;
int i, offset;
unsigned char * srcPt;
srcPt=(unsigned char *)0x31100000;
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 = Uart_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;
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]);
}
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("SMC(K9S1208V0M) 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<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
Uart_Printf("%d block erase.\n", block);
if(NF8_EraseBlock(block)==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<512; 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 NF8_Program(void)
{
// unsigned long interrupt_reservoir;
int i;
int programError=0;
U8 *srcPt,*saveSrcPt;
U32 blockIndex;
Uart_Printf("\n[SMC(K9S1208V0M) 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(NF8_IsBadBlock(blockIndex)==FAIL) {
blockIndex++; // for next block
continue;
}
#endif
if(NF8_EraseBlock(blockIndex)==FAIL) {
blockIndex++; // for next block
continue;
}
// After 1-Block erase, Write 1-Block(32 pages).
for(i=0;i<32;i++) {
if(NF8_WritePage(blockIndex,i,srcPt)==FAIL) {// block num, page num, buffer
programError=1;
break;
}
#if ECC_CHECK
if(NF8_ReadPage(blockIndex,i,srcPt)==FAIL) {
Uart_Printf("ECC Error(block=%d,page=%d!!!\n",blockIndex,i);
}
#endif
srcPt+=512; // 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~4095\n");
Uart_Printf("Input target block number:");
targetBlock=Uart_GetIntNum(); // Block number(0~4095)
if(targetSize==0)
{
#if 0
Uart_Printf("Input target size(0x4000*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/512)/32);
no_page = (U32)((targetSize/512)%32);
no_byte = (U32)(targetSize%512);
Uart_Printf("File:%d[%d-block,%d-page,%d-bytes].\n", targetSize, no_block, no_page, no_byte);
}
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
}
void Test_NF8_Lock(void)
{
U32 num;
U32 S_block, E_block;
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -