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

📄 flashx9.c

📁 MPC5200 BSP 支持ATA,USB, I2C,扩展网口
💻 C
字号:
/*
 * flash.c  - BOOT ROM Flash
 * DESCRIPTION
 *      This file provides the driver for BOOT ROM Flash.
 */

#include <vxWorks.h>
#include <stdio.h>
#include "gflash.h"
#include "flashx9.h"
#include "config.h" /* for #define of ROM_BASE_ADRS */
#include "vmLib.h"
#ifndef ROM_BASE_ADRS   /* should be #define in config.h */
	#error "ROM_BASE_ADRS not defined!"
#endif

static FlashConf bromFlashConf =
{
    BaseAddr:           ROM_BASE_ADRS,
    PortSizePerChip:    1,  /* x8 */
    ChipCount:          1,
    Options:            1   /* None */ 
};

static FL_DEV_ID	s_brom_devid=0xff;
static FL_PART_ID	s_brom_partid=0xffff;

static FlashInfo   bromFlashInfo;
static FlashInfo   *pInfo=NULL;
static int bromSkipSize = 0;

/* If the gflash lib is init'd, it will be set to 1. */
static UINT8 bromFlashInitFlag=0;
int isFlashProtected ();
extern int  getBurnBootFlag(void);
IMPORT void taskDelay(UINT32);
#ifndef my_Assert
#define myAssert(p)    \
    {                   \
        if(!(p)) printf( "Assert Failed : File=%s Line=%d\n", __FILE__, __LINE__ );    \
    }
#endif

static int gFlash_BootRomInit()
{
	UINT8 	ucRetVal=0,ucRet=0;
	UINT32	uiBlkSize=0;
	UINT32		uiBlkCnt=0;
	UINT32		uiSkipSize=0;
	UINT8   tffsBlkRegNo=0,ucRegCnt;  
	 FAST        UINT32 i;  	
	s_brom_devid=gFlash_OpenDevice(&bromFlashConf,&ucRetVal);
    if(s_brom_devid >= MAX_BANK_NO)
    {
    	myAssert(0);
		return FLASH_ERR;
	}
	pInfo=&bromFlashInfo;
	ucRet=gFlash_GetDeviceInfo(s_brom_devid,pInfo);
	if(ucRet)
	{
		myAssert(0);
		return FLASH_ERR;
	}
	ucRegCnt=pInfo->RegCnt;
  for(i=0;i<ucRegCnt;i++)
  {        
     if(pInfo->EBlk[i].Size > uiBlkSize)
     {
         tffsBlkRegNo=i;
         uiBlkSize=pInfo->EBlk[i].Size;            
         uiBlkCnt=pInfo->EBlk[i].Count;
     }
  }
  if(tffsBlkRegNo>0)
  {
  	 for(i=0;i<tffsBlkRegNo;i++)
     {
        uiSkipSize += pInfo->EBlk[i].Size * pInfo->EBlk[i].Count;
     }
  }
  bromSkipSize = uiSkipSize;
	s_brom_partid=gFlash_CreatePart(s_brom_devid,uiSkipSize,uiBlkSize,uiBlkCnt,&ucRet);  
	if(ucRet)
	{
		myAssert(0);
		return FLASH_ERR;
	}
	
	bromFlashInitFlag=1;	
	return FLASH_OK;
}

/*
 * devX9EraseChip -  Erases the whole Flash Chip(SST39VF040 or AMD29LV040)
 *
 * This routine is to erase the whole Flash chip.
 *
 * RETURNS: FLASH_OK, or FLASH_ERR if erase opt is successful or unsuccessful.
 */
int devX9EraseChip(void)
{
 
   if (getBurnBootFlag()==0)
   {
      #if defined(_MKBOOT) && ((CPU==PPCEC603))
   	vmStateSet(NULL,ROM_BASE_ADRS,ROM_SIZE,VM_STATE_MASK_WRITABLE,VM_STATE_WRITABLE);
      #endif
   } 
   else {
    myAssert(0);
    return -1;
   }
 
    if(!bromFlashInitFlag)
    {
    	if(gFlash_BootRomInit()==FLASH_ERR)
    	{
    		myAssert(0);
            return FLASH_ERR;
        }
    }
    
    if (isFlashProtected ())
    {
        printf ("flash is protected!\n");
        return FLASH_ERR;
    }
        
    if(gFlash_EraseAll(s_brom_partid))
    {
    	myAssert(0);
        return FLASH_ERR;
    }
 
   if (getBurnBootFlag()==0)
   {   
   	 #if defined(_MKBOOT) && ((CPU==PPCEC603))
   	vmStateSet(NULL,ROM_BASE_ADRS,ROM_SIZE,VM_STATE_MASK_WRITABLE,VM_STATE_WRITABLE_NOT);
   	#endif
   } 
   else {
    myAssert(0);
    return -1;
   }
 
    return FLASH_OK;
}

#define fio_write08(a,v)    *((volatile unsigned char *)(a+ROM_BASE_ADRS)) =(unsigned char)(v)
#define fio_read08(a)       (*((volatile unsigned char  *)(a+ROM_BASE_ADRS)))

int isFlashProtected ()
{
    unsigned char   ucVenderID, ucDeviceID, ucBeProtected;
   

    *(unsigned char *)(ROM_BASE_ADRS + 0x2AAA) = 0xAA;
    *(unsigned char *)(ROM_BASE_ADRS + 0x5555) = 0x55;
    *(unsigned char *)(ROM_BASE_ADRS + 0x2AAA) = 0x90;

    taskDelay (1); /* delay 1 tick */

    ucVenderID = *(unsigned char *)(ROM_BASE_ADRS + 0x00);
    ucDeviceID = *(unsigned char *)(ROM_BASE_ADRS + 0x01);

    ucBeProtected = *(unsigned char *)(ROM_BASE_ADRS + 0x02);
/*    printf ("ucBeProtected = %d\n", ucBeProtected); */

/*
    ucBeProtected = *(unsigned char *)(ROM_BASE_ADRS + 0x10000 + 0x02);
    printf ("ucBeProtected = %d\n", ucBeProtected);

    ucBeProtected = *(unsigned char *)(ROM_BASE_ADRS + 0x20000 + 0x02);
    printf ("ucBeProtected = %d\n", ucBeProtected);

    ucBeProtected = *(unsigned char *)(ROM_BASE_ADRS + 0x30000 + 0x02);
    printf ("ucBeProtected = %d\n", ucBeProtected);

    ucBeProtected = *(unsigned char *)(ROM_BASE_ADRS + 0x40000 + 0x02);
    printf ("ucBeProtected = %d\n", ucBeProtected);

    ucBeProtected = *(unsigned char *)(ROM_BASE_ADRS + 0x50000 + 0x02);
    printf ("ucBeProtected = %d\n", ucBeProtected);

    ucBeProtected = *(unsigned char *)(ROM_BASE_ADRS + 0x60000 + 0x02);
    printf ("ucBeProtected = %d\n", ucBeProtected);

    ucBeProtected = *(unsigned char *)(ROM_BASE_ADRS + 0x70000 + 0x02);
    printf ("ucBeProtected = %d\n", ucBeProtected);
*/
    
    *(unsigned char *)(ROM_BASE_ADRS + 0x0000) = 0xF0; /* reset to "reading array data" */
    
    /* printf ("ucVenderID = %d, ucDeviceID = %d\n", ucVenderID, ucDeviceID); */
  
    if ((0x01 == ucVenderID || 0x04 == ucVenderID) && 0x7e == ucDeviceID)
    {
        /* S29GL128N */
        /* printf ("this is S29GL128N\n"); */
        if (1 == ucBeProtected)
            return 1;
        else
            return 0;
    }
    else
    {
        return 0;
    }
}

/*
 * devX9FlashPrg - write the specified content to Flash
 *
 * This routine is to write the fash with specified content and address.
 *
 * RETURNS: FLASH_OK, or FLASH_ERR if the write opt is successful or unsuccessful.
 */
int devX9FlashPrg
    (
    unsigned long offset,     /* the offset from flash base address */
    VBYTE *bufAddr,   /* the source content address */
    unsigned long dataLen     /* the length will be writen */
    )
{
    if(!bromFlashInitFlag)
    {
    	if(gFlash_BootRomInit()==FLASH_ERR)
    	{
    		myAssert(0);    	
            return FLASH_ERR;
        }
    }

   if (getBurnBootFlag()==0)
   {
   	#if defined(_MKBOOT) && ((CPU==PPCEC603) )
   	vmStateSet(NULL,ROM_BASE_ADRS,ROM_SIZE,VM_STATE_MASK_WRITABLE,VM_STATE_WRITABLE);
   	#endif
   } 
   else {
    myAssert(0);
    return -1;
   }
 
    if (isFlashProtected ())
    {
        printf ("flash is protected!\n");
        return FLASH_ERR;
    }
		offset = offset - bromSkipSize;
    if(gFlash_Write(s_brom_partid,offset,bufAddr,dataLen))
    {
    	myAssert(0);
        return FLASH_ERR;
    }

   if (getBurnBootFlag()==0)
   {
   	 #if defined(_MKBOOT) && ((CPU==PPCEC603) )
   	vmStateSet(NULL,ROM_BASE_ADRS,ROM_SIZE,VM_STATE_MASK_WRITABLE,VM_STATE_WRITABLE_NOT);
   	#endif
   } 
   else {
    myAssert(0);
    return -1;
   }

    return FLASH_OK;
}

void testFlash()
{
    char ttt[10];
    int i;

    ttt[0] = 0x55;
    ttt[1] = 0xAA;
    if(devX9EraseChip())
    {
        printf("Erase chip error!\n");
    }
    for(i=0; i<0x80000 ; i=i+2)
    {
        if(devX9FlashPrg(i,ttt,2))
        {
            printf("\nBoot Flash Programming test fails.\n");
        }
    }
}

⌨️ 快捷键说明

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