📄 k9k2g16.c
字号:
//====================================================================
// 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 <string.h>
#include <stdlib.h>
#include "def.h"
#include "option.h"
#include "2440addr.h"
#include "2440lib.h"
#include "2440slib.h"
#include "K9K2G16.h"
#include "nand.h"
#define BAD_CHECK (0)
#define ECC_CHECK (0)
#define C_LANG 1
#define DMA 2
#define TRANS_MODE16 2
extern void Nand_Reset(void);
extern U32 srcAddress;
extern U32 targetBlock; // Block number (0 ~ 4095)
extern U32 targetSize; // Total byte size
extern U32 downloadAddress;
extern U32 downloadProgramSize;
U32 NF16_Spare_Data[32/2];
volatile int NFConDone16;
void __irq NFCon_Int16(void);
void * n16_func[][2]=
{
// (void *)Nand_Reset, "NF reset ",
(void *)Test_NF16_Block_Erase, "Block erase ",
(void *)Test_NF16_Page_Read, "Page read ",
(void *)Test_NF16_Page_Write, "Page write ",
(void *)Test_NF16_Rw, "Nand R/W test ",
(void *)NF16_Program, "K9K2G16_Program ",
(void *)NF16_Print_Id, "Read ID ",
(void *)K9K2G16_PrintBadBlockNum, "Check bad block ",
(void *)Test_NF16_Lock, "Nand Block lock test",
(void *)Test_NF16_SoftUnLock, "Soft Unlock test ",
0,0
};
void Test_K9K2G16(void)
{
int i;
U32 blocknum, pagenum;
Uart_Printf("\nK9K2G16 Nand flash test start.\n");
NF16_Init();
while(1) {
Print16SubMessage();
Uart_Printf("\nSelect(-1 to exit): ");
i = Uart_GetIntNum();
//Uart_Printf("IN:%d.\n\n", i);
if(i==-1) break;
if(i>=0 && (i<(sizeof(n16_func)/8)) )
( (void (*)(void)) (n16_func[i][0]) )(); // execute selected function.
}
}
void Print16SubMessage(void)
{
int i;
i=0;
Uart_Printf("\n\n");
while(1)
{ //display menu
Uart_Printf("%2d:%s",i,n16_func[i][1]);
i++;
if((int)(n16_func[i][0])==0)
{
Uart_Printf("\n");
break;
}
if((i%4)==0) Uart_Printf("\n");
}
}
void NF16_Print_Id(void)
{
U32 id;
U8 maker, device, id4th;
// NF16_Init();
id = NF16_CheckId();
device = (U8)id;
maker = (U8)(id>>8);
Uart_Printf("Maker:%x, Device:%x\n", maker, device);
}
void Test_NF16_Block_Erase(void)
{
U32 block=0;
Uart_Printf("SOP(K9K2G160UM) NAND Block erase\n");
Uart_Printf("Block # to erase: ");
block = Uart_GetIntNum();
if(NF16_EraseBlock(block)==FAIL) return;
Uart_Printf("%d-block erased.\n", block);
}
void Test_NF16_Lock(void)
{
U32 num;
U32 S_block, E_block;
Uart_Printf("SOP(K9K2G16U0M) 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 S3C2440!!!\n ");
}
Uart_Printf("%d block ~ %d block are Programmable\n ", S_block, (E_block-1));
}
void Test_NF16_SoftUnLock(void)
{
U32 S_block, E_block;
Uart_Printf("SOP(K9K2G16U0M) NAND SoftUnLock Test !!!\n");
rNFSBLK=0x0;
rNFEBLK=0x0;
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 ");
}
void Test_NF16_Page_Read(void)
{
U32 block=0, page=0;
U32 i;
unsigned int * srcPt;
srcPt=(unsigned int *)0x31000000;
Uart_Printf("SOP(K9K2G16U0M) NAND Page Read.\n");
Uart_Printf("Block # to read: ");
block = Uart_GetIntNum();
Uart_Printf("Page # to read: ");
page = Uart_GetIntNum();
if(NF16_ReadPage(block, page, srcPt)==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%4)==0) Uart_Printf("\n%2x: ", i);
Uart_Printf("%08x ", *srcPt++);
}
Uart_Printf("\n");
Uart_Printf("Spare:");
for(i=0; i<16; i++) {
Uart_Printf("%08x ", NF16_Spare_Data[i]);
}
Uart_Printf("\n");
}
void Test_NF16_Page_Write(void)
{
U32 block=0, page=0;
int i, offset;
unsigned int *srcPt;
srcPt=(unsigned int *)0x31100000;
Uart_Printf("SOP(K9K2G16U0M) NAND Page Write.\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()%0xffffffff;
#else
if(offset==-1) *srcPt++ = i;
#endif
else *srcPt++ =i+offset;
}
srcPt=(unsigned int *)0x31100000;
Uart_Printf("Write data[%d block, %d page].\n", block, page);
if(NF16_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%4)==0) Uart_Printf("\n%2x: ", i);
Uart_Printf("%08x ", *srcPt++);
}
Uart_Printf("\n");
Uart_Printf("Spare:");
for(i=0; i<32/2; i++) {
Uart_Printf("%08x ", NF16_Spare_Data[i]);
}
Uart_Printf("\n\n");
}
}
void Test_NF16_Rw(void)
{
U32 block=0, page=0;
U32 i, status=FAIL, error, offset;
unsigned int *srcPt, *dstPt;
srcPt=(unsigned int *)0x31100000;
dstPt=(unsigned int *)0x31200000;
Uart_Printf("SOP(K9K2G16U0M) 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()%0xffffffff;
#else
if(offset==-1) *srcPt++= i;
#endif
else *srcPt++ = i+offset;
}
srcPt=(unsigned int *)0x31100000;
dstPt=(unsigned int *)0x31200000;
// Block erase
Uart_Printf("%d block erase.\n", block);
if(NF16_EraseBlock(block)==FAIL) return;
Uart_Printf("Write data[%d block, %d page].\n", block, page);
if(NF16_WritePage(block, page, srcPt)==FAIL) return;
Uart_Printf("Read data.\n");
if(NF16_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 NF16_Program(void)
{
int i, page_num;
int programError=0;
U32 *srcPt,*saveSrcPt;
U32 blockIndex;
Uart_Printf("\n[SOP(K9K2G16U0M) NAND Flash writing program]\n");
Uart_Printf("The program buffer: 0x30100000~0x31ffffff\n");
// NF16_Init();
rINTMSK = BIT_ALLMSK;
srcAddress=0x30100000;
InputTargetBlock16();
srcPt=(U32 *)srcAddress;
blockIndex=targetBlock;
while(1) {
saveSrcPt=srcPt;
#if BAD_CHECK
if(NF16_IsBadBlock(blockIndex)==FAIL) {
blockIndex++; // for next block
continue;
}
#endif
if(NF16_EraseBlock(blockIndex)==FAIL) {
blockIndex++; // for next block
continue;
}
// After 1-Block erase, Write 1-Block(64 pages).
for(i=0;i<64;i++) {
if(NF16_WritePage(blockIndex,i,srcPt)==FAIL) {// block num, page num, buffer
programError=1;
break;
}
#if ECC_CHECK
if(NF16_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(".");
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 K9K2G16_PrintBadBlockNum(void)
{
int i;
U32 id;
Uart_Printf("\n[SMC(K9S1208V0M) NAND Flash bad block check]\n");
// NF16_Init();
for(i=0;i<2048;i++)
{
NF16_IsBadBlock(i); // Print bad block
}
}
//*************************************************
//*************************************************
//** 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 ....
#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_RDDATA16() ((*(volatile unsigned short*)0x4E000010) )
#define NF_WRDATA(data) {rNFDATA=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)));} // edge detect.
#define ID_K9S1208V0M 0xec76
#define ID_K9K2G16U0M 0xecca
#define BAD_MARK (0xffff4444)
#if 1
// HCLK=100Mhz
#define TACLS 0 // 1-clk(0ns)
#define TWRPH0 6 // 3-clk(25ns)
#define TWRPH1 0 // 1-clk(10ns) //TACLS+TWRPH0+TWRPH1>=50ns
#else
// HCLK=50Mhz
#define TACLS 0 //1clk(0ns)
#define TWRPH0 1 //2clk(25ns)
#define TWRPH1 0 //1clk(10ns)
#endif
static U32 se16Buf[32/2]={
0xffffffff,0xffffffff,0xffffffff,0xffffffff,
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -