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

📄 k9s1208.c

📁 arm9的bootloader大家下载解压缩即可看到
💻 C
📖 第 1 页 / 共 2 页
字号:
//====================================================================
// File Name : k9s1208.c
// Function  : SmartMedia SAMSUNG 64MB (K9S1208) Test Program
// Program   : Weon-Tark Kang (Tark)
// Modify    : On-Pil Shin (SOP)
// Date      : October 23, 2002
// Version   : 0.0
// History
//   0.0 : Programming start -> Tark
//       (20030324) : Added K9S1208 Erase -> SOP
//       (20040326) : Added K9F2808 (16MB) -> SOP
//====================================================================

#include <string.h>
#include "def.h"
#include "option.h"
#include "2410addr.h"
#include "2410lib.h"
#include "2410slib.h" 

#define BAD_CHECK   (1)
#define ECC_CHECK   (0)

//*************** H/W dependent functions ***************
void __RdPage528(U8 *pPage);
void __WrPage528(U8 *pPage);

static U16 NF_CheckId(void);
static int NF_EraseBlock(U32 blockNum);
//static int NF_ReadPage(U32 block,U32 page,U8 *buffer);
static int NF_WritePage(U32 block,U32 page,U8 *buffer);
    //buffer size is 512 bytes
static int NF_IsBadBlock(U32 block);
static int NF_MarkBadBlock(U32 block);
static void NF_Reset(void);
//static void NF_Init(void);
void NF_Init(void);
//*******************************************************

int DownloadData(void);

//static void InputTargetBlock(void);

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

U32 downloadAddress;
//U32 downloadProgramSize=0x0;
U32 downloadProgramSize=0x4000;//16k decide the space of downloadable address 

//======================================================================
void K9S1208_Program(size_t retlen)
{   
//    unsigned long interrupt_reservoir;
    int i,num,round;
    int programError=0;
    U8 *srcPt,*saveSrcPt;
    U16 id;
    U32 blockIndex;

    downloadAddress=0x30100000;
    Uart_Printf("\n[ NAND Flash writing program]\n");
    //Uart_Printf("The program buffer: 0x30100000~0x31ffffff\n");


//#if 0
//    if(!DownloadData())
//    {
//        Uart_Printf("Download Fail\n");
//        return;
//    }
//#endif

    NF_Init();

    id=NF_CheckId();
    Uart_Printf("ID=0x%x\n",id);	//03/26/2004 SOP
    if(id!=0xec73 && id!=0xec76)	//03/26/2004 SOP, 0xec73=16MB, 0xec76=64MB    
    {
    	Uart_Printf("This NAND flash are not 16MB or 64MB.\n");
    	return;
	}    	

    rINTMSK = BIT_ALLMSK;  
    num=retlen/(16*1024);
    if((retlen%(16*1024))>0)
    round=1;
    num=num+round;
    
    Uart_Printf("%d",num);
     Uart_Printf("%d",retlen);
    targetSize=downloadProgramSize*(num);
    Uart_Printf("targetSize is %x",targetSize);
    if(targetSize==0)
    {
        //srcAddress=0x30100000; 
        //Uart_Printf("the data must be downloaded using ICE from %xh\n",srcAddress);  
        Uart_Printf("the data must be downloaded using ICE ");        
    }
    else 
       // srcAddress=downloadAddress+4; //to discard the data head for the size
          srcAddress=0x30008000;
    //InputTargetBlock();

    //Uart_Printf("source base address(0x3010000x)=0x%x\n",srcAddress);
    //Uart_Printf("target start block number      =%d\n",targetBlock);
    //Uart_Printf("target size        (0x4000*n)  =0x%x\n",targetSize);

    srcPt=(U8 *)srcAddress;
    blockIndex=4044;
    while(1)
    {
        saveSrcPt=srcPt;    

        #if BAD_CHECK 
            Uart_Printf("\nstart checking bad block");
            //blockIndex=4000;      
            if(NF_IsBadBlock(blockIndex))   // 1:bad 0:good
            {
                blockIndex++;   // for next block
                Uart_Printf("deteck bad block");
                continue;
            }
        #endif
        
        if(!NF_EraseBlock(blockIndex))
        {   
            blockIndex++;   // for next block
            Uart_Printf("detect bad block when eracing");
            continue;
        }
        Uart_Printf("\nblock num=%x,page num=%x,buffer=%x",blockIndex,i,srcPt);
        for(i=0;i<32;i++)
        {   //Uart_Printf("\nblock num=%x,page num=%x,buffer=%x",blockIndex,i,srcPt);
            if(!NF_WritePage(blockIndex,i,srcPt))// block num, page num, buffer
            {
                programError=1;
                break;
            }
            #if ECC_CHECK    
                if(!NF_ReadPage(blockIndex,i,srcPt))
                {
                    Uart_Printf("ECC Error(block=%d,page=%d!!!\n",blockIndex,i);
                }
            #endif      
            srcPt+=512; // Increase buffer addr one pase size
            Uart_Printf(".");
            if((U32)srcPt>=(srcAddress+targetSize)) // Check end of buffer
                {Uart_Printf("\nthe address of end of buffer is %x",(U32)srcPt);
                Uart_Printf("\ncompleted");
                break;  // Exit for loop
                }
        }
        if(programError==1)
        {
            blockIndex++;
            srcPt=saveSrcPt;
            programError=0;
            continue;
        }
        if((U32)srcPt>=(srcAddress+targetSize))
            break;  // Exit while loop
        blockIndex++;
        Uart_Printf("%d",blockIndex);
    }
}

//==================================================================================
void K9S1208_Erase(void)
{
    int i,BlockSize=0;
    U16 id;
    U32 blockIndex=0;

    Uart_Printf("\n[ NAND Flash Erase Program ]\n");	//03/26/2004 SOP
//    Uart_Printf("\nNow NAND Flash Erasing ....\n");    

    NF_Init();
    
    id=NF_CheckId();
    Uart_Printf("ID=0x%x\n",id);	//03/26/2004 SOP    
    if(id!=0xec73 && id!=0xec76)	//03/26/2004 SOP, 0xec73=16MB, 0xec76=64MB    
    {
    	Uart_Printf("This NAND flash are not 16MB or 64MB.\n");
    	return;
	}    	

    rINTMSK = BIT_ALLMSK;   

	if(id==0xec73)					//03/26/2004 SOP
	{
		Uart_Printf("\n16MB NAND Flash Erasing ....\n");		
		BlockSize=1024;
	}		

	if(id==0xec76)					//03/26/2004 SOP
	{
		Uart_Printf("\n64MB NAND Flash Erasing ....\n");		
		BlockSize=4096;		
	}		

    for(blockIndex=0;blockIndex<BlockSize;blockIndex++)
    {
        #if BAD_CHECK       
            if(NF_IsBadBlock(blockIndex))   // 1:bad 0:good
            {
                blockIndex++;   // for next block
                continue;
            }
        #endif
        
        if(!NF_EraseBlock(blockIndex))
        {
            blockIndex++;   // for next block
            continue;
        }       
    }
    Uart_Printf("\nNAND Flash Erase Completed !!!\n");    
//InputTargetBlock}
}

//==================================================================================

//static void InputTargetBlock(void)
//{
//    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)
//    {
//        Uart_Printf("Input target size(0x4000*n):");
//        targetSize=Uart_GetIntNum();    // Total byte size
//    }
//}

//======================================================================
void K9S1208_PrintBadBlockNum(void)
{
    int i;
    U16 id;

    Uart_Printf("\n[ NAND Flash bad block check ]\n");		//03/26/2004 SOP
    
    NF_Init();
    id=NF_CheckId();
    Uart_Printf("ID=0x%x\n",id);	//03/26/2004 SOP
    if(id!=0xec73 && id!=0xec76)	//03/26/2004 SOP, 0xec73=16MB, 0xec76=64MB    
    {
    	Uart_Printf("This NAND flash are not 16MB or 64MB.\n");
    	return;
	}    	

    if(id==0xec73)    //16MB , 03/26/2004 SOP
    {
    	Uart_Printf("16MB NAND Flash Bad block Checking......\n");    	
    	for(i=0;i<1024;i++)
    		NF_IsBadBlock(i);   // Print bad block
    	Uart_Printf("16MB NAND Flash Bad block Check Completed.\n");     		
	}  
	
    if(id==0xec76)    //64MB , 03/26/2004 SOP
    {
    	Uart_Printf("64MB NAND Flash Bad block Checking......\n");    	
    	for(i=0;i<4096;i++)
    		NF_IsBadBlock(i);   // Print bad block
    	Uart_Printf("64MB NAND Flash Bad block Check Completed.\n");    		
	}   	  	
}

//======================================================================
/*void K9S1208_PrintBlock(void)// Printf one page
{
    int i,j;
    U16 id;
    U32 block,page;
    U8  buffer[512];

    Uart_Printf("\n[ NAND Flash block read ]\n"); 
    
    NF_Init();
    id=NF_CheckId();
    Uart_Printf("ID=0x%x\n",id);
    if(id!=0xec73 && id!=0xec76)	//03/26/2004 SOP, 0xec73=16MB, 0xec76=64MB    
    {
    	Uart_Printf("This NAND flash are not 16MB or 64MB.\n");
    	return;
	}    	

    Uart_Printf("Input target block number : ");
    block=Uart_GetIntNum();
    Uart_Printf("Input target page number : ");   
    page=Uart_GetIntNum();

    NF_ReadPage(block,page,buffer);
    Uart_Printf("block= %d,page= %d: ",block,page);
    for(j=0;j<512;j++)
    {
        if(j%16==0)
        	Uart_Printf("\n%3xh:",j);
        Uart_Printf("%02x ",buffer[j]);
    }
    Uart_Printf("\n");      
}*/


//*************************************************
//*************************************************
//**           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 WRITEVERIFY  (0)  //verifing is enable at writing.

/*
#define FC_CMD()    {rPDATA|=CLE;rPDATA&=~(ALE|CE);}
#define FC_ADDR()   {rPDATA|=ALE;rPDATA&=~(CLE|CE);}
#define FC_DATA()   {rPDATA&=~(ALE|CLE|CE);}
#define FC_INACTIVE() {rPDATA|=CE;rPDATA&=~(ALE|CLE);}
*/

#define NF_CMD(cmd) 	{rNFCMD=cmd;}
#define NF_ADDR(addr)   {rNFADDR=addr;} 
#define NF_nFCE_L() 	{rNFCONF&=~(1<<11);}
#define NF_nFCE_H() 	{rNFCONF|=(1<<11);}
#define NF_RSTECC() 	{rNFCONF|=(1<<12);}
#define NF_RDDATA()     (rNFDATA)
#define NF_WRDATA(data) {rNFDATA=data;}

#define NF_WAITRB()    {while(!(rNFSTAT&(1<<0)));} 
        //wait tWB and check F_RNB pin.   

#define ID_K9S1208V0M   0xec76

#if 1
// HCLK=135Mhz  //TACLS+TWRPH0+TWRPH1>= ? ns
#define TACLS       0  //1clk(7ns)  CLE & ALE duration setting value (0~7) Duration = HCLK * (TACLS + 1)
#define TWRPH0      5  //5clk(37ns) TWRPH0 duration setting value (0~7)    Duration = HCLK * (TWRPH0 + 1)
#define TWRPH1      0  //1clk(7ns) TWRPH1 duration setting value (0~7)    Duration = HCLK * (TWRPH1 + 1) 
#else
// HCLK=100Mhz  //TACLS+TWRPH0+TWRPH1>=50ns
#define TACLS       0  //1clk(0ns)  CLE & ALE duration setting value (0~7) Duration = HCLK * (TACLS + 1)
#define TWRPH0      3  //3clk(25ns) TWRPH0 duration setting value (0~7)    Duration = HCLK * (TWRPH0 + 1)
#define TWRPH1      0  //1clk(10ns) TWRPH1 duration setting value (0~7)    Duration = HCLK * (TWRPH1 + 1)
// HCLK=50Mhz
//#define TACLS       0  //1clk(0ns)
//#define TWRPH0      1  //2clk(25ns)
//#define TWRPH1      0  //1clk(10ns)
#endif


static U8 seBuf[16]={0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff};

// 1block=(512+16)bytes x 32pages
// 4096block

// A[23:14][13:9]
//  block   page

//======================================================================
static int NF_EraseBlock(U32 block)
{
    U32 blockPage=(block<<5);
    int i;

#if BAD_CHECK
    if(NF_IsBadBlock(block))
    	return 0;
#endif

    NF_nFCE_L();
    
    NF_CMD(0x60);   // Erase one block 1st command

    NF_ADDR(blockPage&0xff);        // Page number=0
    NF_ADDR((blockPage>>8)&0xff);   
    NF_ADDR((blockPage>>16)&0xff);

    NF_CMD(0xd0);   // Erase one blcok 2nd command
    
   for(i=0;i<10;i++); //wait tWB(100ns)//??????

    NF_WAITRB();    // Wait tBERS max 3ms.
    NF_CMD(0x70);   // Read status command

    if (NF_RDDATA()&0x1) // Erase error
    {   
        NF_nFCE_H();
    	Uart_Printf("[ERASE_ERROR:block#=%d]\n",block);
    	NF_MarkBadBlock(block);
    	return 0;
    }
    else 
    {
        NF_nFCE_H();
        Uart_Printf("erase block ok (%x)",block);
        return 1;
    }
}


//======================================================================
static int NF_IsBadBlock(U32 block)
{
    int i;
    unsigned int blockPage;
    U8 data;
    
    
    blockPage=(block<<5);   // For 2'nd cycle I/O[7:5] 
    
    NF_nFCE_L();    
    NF_CMD(0x50);       // Spare array read command
    NF_ADDR(517&0xf);       // Read the mark of bad block in spare array(M addr=5) 
    NF_ADDR(blockPage&0xff);    // The mark of bad block is in 0 page
    NF_ADDR((blockPage>>8)&0xff);   // For block number A[24:17]
    NF_ADDR((blockPage>>16)&0xff);  // For block number A[25]

    for(i=0;i<10;i++);   // wait tWB(100ns) //?????
    
    NF_WAITRB();    // Wait tR(max 12us)
    
    data=NF_RDDATA();

    NF_nFCE_H();    

    if(data!=0xff)
    {
        Uart_Printf("[block %d has been marked as a bad block(%x)]\n",block,data);
        return 1;
    }
    else
    {

⌨️ 快捷键说明

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