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

📄 mvflash.c

📁 此代码为烧制bios程序的源码
💻 C
📖 第 1 页 / 共 5 页
字号:
            }
            break;

    }
    /* Try to locate the device in the supported flashes list (FLASE_TYPE). 
       according to the keys: 
       1) mfrId - manufactor ID.
       2) devId - device ID.  
   */
    while(true)
    {

        if (mvFlashTypes[pArray] == 0)
        {
            mvFlashReset();
            return 0;  /* Device not in the list */
        }
        if( (mvFlashTypes[pArray] == mfrId) && 
             (mvFlashTypes[pArray+1] == devId))
        {
            FLASH_POINTER_TO_FLASH = pArray;
            for(counter = 0 ; counter < mvFlashTypes[FLASH_NUMBER_OF_SECTORS] ; 
                counter++)
            {
                flashSize = flashSize + 
                    mvFlashTypes[FLASH_FIRST_SECTOR_SIZE + counter];
            }
            if( FLASH_MODE != PURE8 )
            {
                mvFlashReset();
                return (flashSize * _1K * (FLASH_WIDTH / (FLASH_MODE / 8)) );
            }
            else
            {
                mvFlashReset();
                return (flashSize * _1K * FLASH_WIDTH);
            }
        }
        pArray += (3 + mvFlashTypes[pArray+2]); /* Move to next entry */
    }
}

bool mvFlashChipErase(void)
{unsigned long i;
 mvFlashReset();
 if( FLASH_MODE == PURE8 )
    {MV_WRITE_CHAR(FLASH_BASE_ADDR,  0x5555,0xAA);
     MV_WRITE_CHAR(FLASH_BASE_ADDR,  0x2aaa,0x55);
     MV_WRITE_CHAR(FLASH_BASE_ADDR,  0x5555,0x80);
     MV_WRITE_CHAR(FLASH_BASE_ADDR,  0x5555,0xAA);
     MV_WRITE_CHAR(FLASH_BASE_ADDR,  0x2aaa,0x55);
     MV_WRITE_CHAR(FLASH_BASE_ADDR,  0x5555,0x10);  /*chip erase*/
     i=10000000;
     while(i--); 
     i=1000000;
     while(i--); /*delay at least 10 seconds*/
    }
 mvFlashReset();
 return true;
}

/*******************************************************************************
* mvFlashEraseSector - Erases a specific sector in the flash memory.
*
* DESCRIPTION:
*       None.
*
* INPUT:
*       sectorNumber - the sector number to be erased.
*
* OUTPUT:
*       None.
*
* RETURN:
*       true on success,false on failure.
*
*******************************************************************************/
bool mvFlashEraseSector(unsigned long sectorNumber)
{ 
    unsigned long    regValue;
    unsigned long    sectorBaseAddress = 0;
    unsigned long    i;
    unsigned long    data20,dataD0,data70;
    unsigned long    dataPoll;
    unsigned long    FirstAddr,SecondAddr,ThirdAddr,FourthAddr,FifthAddr;
    unsigned long    FirstData,SecondData,ThirdData;
    unsigned long    FourthData,FifthData,SixthData;

    /* calculate the sector base Address according to the following parametrs:
       1: FLASH_WIDTH
       2: the size of each sector which it detailed in the table */
    
    /* checking the if the sectorNumber is legal.*/
    if ( sectorNumber > mvFlashTypes[FLASH_NUMBER_OF_SECTORS] - 1) 
        return false;
    /* now the calculation begining of the sector Address */
    for (i=0 ; i < sectorNumber ; i++)
    {
        sectorBaseAddress=sectorBaseAddress + 
            mvFlashTypes[FLASH_FIRST_SECTOR_SIZE+i];    
    }
    /* In case of X8 wide the address should be */
    if( FLASH_MODE == PURE8 )
        sectorBaseAddress = _1K * sectorBaseAddress;
    if( FLASH_MODE == X8 )
        sectorBaseAddress = _1K * sectorBaseAddress;
    /* In case of X16 wide the address should be */
    if( FLASH_MODE == X16 )
        sectorBaseAddress = _1K * sectorBaseAddress / 2;  
    mvFlashReset();
    if(mvFlashTypes[FLASH_POINTER_TO_FLASH] == ATMEL_FLASH )
	mvFlashChipErase();  /* ATMEL only support chip Erase */

    if ( (mvFlashTypes[FLASH_POINTER_TO_FLASH] == AMD_FLASH) ||   \
            (mvFlashTypes[FLASH_POINTER_TO_FLASH] == ST_FLASH) ||   \
			(mvFlashTypes[FLASH_POINTER_TO_FLASH] == SST_FLASH)  )
    {
        switch(FLASH_WIDTH)
        {
            case 1:
                if( FLASH_MODE == PURE8) /* Boot Flash PURE8*/
                {
                    FirstAddr  = 0x5555;
                    SecondAddr = 0x2aaa;
                    ThirdAddr  = 0x5555;
                    FourthAddr = 0x5555; 
                    FifthAddr  = 0x2aaa;
                }
                else
                {
                    FirstAddr  = 0xaaaa;
                    SecondAddr = 0x5555;
                    ThirdAddr  = 0xaaaa;
                    FourthAddr = 0xaaaa; 
                    FifthAddr  = 0x5555;
                }
                MV_WRITE_CHAR(FLASH_BASE_ADDR,  FirstAddr,0xAA);
                MV_WRITE_CHAR(FLASH_BASE_ADDR,  SecondAddr,0x55);
                MV_WRITE_CHAR(FLASH_BASE_ADDR,  ThirdAddr,0x80);
                MV_WRITE_CHAR(FLASH_BASE_ADDR,  FourthAddr,0xAA);
                MV_WRITE_CHAR(FLASH_BASE_ADDR,  FifthAddr,0x55);
                MV_WRITE_CHAR(FLASH_BASE_ADDR, ((sectorBaseAddress & 
                                                  0xffffff00)),0x30);
                /* Poll on the flash */
                do{
                    MV_READ_CHAR(FLASH_BASE_ADDR, sectorBaseAddress,
                                 &regValue); 
                } while((regValue & 0x80) != 0x80);

                break;
            case 2:
                if (FLASH_MODE == X16)
                {
                    FirstData  = 0xaa;      /* Data for the First  Cycle    */
                    SecondData = 0x55;      /* Data for the Second Cycle    */
                    ThirdData  = 0x80;      /* Data for the Third  Cycle    */
                    FourthData = 0xaa;      /* Data for the Fourth Cycle    */
                    FifthData  = 0x55;      /* Data for the Fifth  Cycle    */
                    SixthData  = 0x30;      /* Data for the Sixth  Cycle    */
                    FirstAddr  = 0x5555;    /* Address for the First  Cycle */
                    SecondAddr = 0x2aaa;    /* Address for the Second Cycle */
                    ThirdAddr  = 0x5555;    /* Address for the Third  Cycle */
                    FourthAddr = 0x5555;    /* Address for the Fourth Cycle */
                    FifthAddr  = 0x2aaa;    /* Address for the Fifth  Cycle */
                }
                else /* (FLASH_MODE = 8) */
                {
                    FirstData  = 0xaaaa;     /* Data for the First  Cycle    */
                    SecondData = 0x5555;     /* Data for the Second Cycle    */
                    ThirdData  = 0x8080;     /* Data for the Third  Cycle    */
                    FourthData = 0xaaaa;     /* Data for the Fourth Cycle    */
                    FifthData  = 0x5555;     /* Data for the Fifth  Cycle    */
                    SixthData  = 0x3030;     /* Data for the Sixth  Cycle    */
                    FirstAddr  = 0xaaaa;     /* Address for the First  Cycle */
                    SecondAddr = 0x5555;     /* Address for the Second Cycle */
                    ThirdAddr  = 0xaaaa;     /* Address for the Third  Cycle */
                    FourthAddr = 0xaaaa;     /* Address for the Fourth Cycle */
                    FifthAddr  = 0x5555;     /* Address for the Fifth  Cycle */
                }
                MV_WRITE_SHORT(FLASH_BASE_ADDR, FirstAddr * FLASH_WIDTH,
                            FirstData);
                MV_WRITE_SHORT(FLASH_BASE_ADDR, SecondAddr * FLASH_WIDTH,
                            SecondData);
                MV_WRITE_SHORT(FLASH_BASE_ADDR, ThirdAddr * FLASH_WIDTH,
                            ThirdData);
                MV_WRITE_SHORT(FLASH_BASE_ADDR, FourthAddr * FLASH_WIDTH,
                            FourthData);
                MV_WRITE_SHORT(FLASH_BASE_ADDR, FifthAddr * FLASH_WIDTH,
                            FifthData);
                MV_WRITE_SHORT(FLASH_BASE_ADDR, 
                      (sectorBaseAddress & 0xffffff00)* FLASH_WIDTH,SixthData);
                /* Poll on the flash */
                if (FLASH_MODE == X16) /* 1 device of 16 bit */
                {
                    dataPoll = 0x0080;
                }
                else /* (FLASH_MODE = 8) ==> 2 devices , 8 bit each => 16bit*/
                {
                    dataPoll = 0x8080;
                }
                do{
                    MV_READ_SHORT(FLASH_BASE_ADDR+sectorBaseAddress * 
                                  FLASH_WIDTH,0,&regValue); 
                } while((regValue & dataPoll) != dataPoll);
                break;
            case 4:
                if (FLASH_MODE == X16)
                {
                    FirstData  = 0x00aa00aa; /* Data for the First  Cycle    */
                    SecondData = 0x00550055; /* Data for the Second Cycle    */
                    ThirdData  = 0x00800080; /* Data for the Third  Cycle    */
                    FourthData = 0x00aa00aa; /* Data for the Fourth Cycle    */
                    FifthData  = 0x00550055; /* Data for the Fifth  Cycle    */
                    SixthData  = 0x00300030; /* Data for the Sixth  Cycle    */
                    FirstAddr  = 0x5555;     /* Address for the First  Cycle */
                    SecondAddr = 0x2aaa;     /* Address for the Second Cycle */
                    ThirdAddr  = 0x5555;     /* Address for the Third  Cycle */
                    FourthAddr = 0x5555;     /* Address for the Fourth Cycle */
                    FifthAddr  = 0x2aaa;     /* Address for the Fifth  Cycle */
                }
                else /* if (FLASH_MODE == 8) */
                {
                    FirstData  = 0xaaaaaaaa; /* Data for the First  Cycle    */
                    SecondData = 0x55555555; /* Data for the Second Cycle    */
                    ThirdData  = 0x80808080; /* Data for the Third  Cycle    */
                    FourthData = 0xAAAAAAAA; /* Data for the Fourth Cycle    */
                    FifthData  = 0x55555555; /* Data for the Fifth  Cycle    */
                    SixthData  = 0x30303030; /* Data for the Sixth  Cycle    */
                    FirstAddr  = 0xaaaa;     /* Address for the First  Cycle */
                    SecondAddr = 0x5555;     /* Address for the Second Cycle */
                    ThirdAddr  = 0xaaaa;     /* Address for the Third  Cycle */
                    FourthAddr = 0xaaaa;     /* Address for the Fourth Cycle */
                    FifthAddr  = 0x5555;     /* Address for the Fifth  Cycle */
                }
                MV_WRITE_WORD(FLASH_BASE_ADDR, FirstAddr * FLASH_WIDTH,
                           FirstData);
                MV_WRITE_WORD(FLASH_BASE_ADDR, SecondAddr * FLASH_WIDTH,
                           SecondData);
                MV_WRITE_WORD(FLASH_BASE_ADDR, ThirdAddr * FLASH_WIDTH,
                           ThirdData);
                MV_WRITE_WORD(FLASH_BASE_ADDR, FourthAddr * FLASH_WIDTH,
                           FourthData);
                MV_WRITE_WORD(FLASH_BASE_ADDR, FifthAddr * FLASH_WIDTH,
                           FifthData);
                MV_WRITE_WORD(FLASH_BASE_ADDR, 
                           (sectorBaseAddress & 0xffffff00) * FLASH_WIDTH,
                           SixthData);
                /* Poll on the flash */
                if (FLASH_MODE == X16) /* 4 devices , 16 bit each => 64bit */
                {
                    dataPoll = 0x00800080;
                }
                else /* (FLASH_MODE = 8) ==> 8 devices , 8 bit each => 64bit*/
                {
                    dataPoll = 0x80808080;
                }
                do{
                    MV_READ_WORD(FLASH_BASE_ADDR+sectorBaseAddress * 
                              FLASH_WIDTH,0,&regValue);
                } while((regValue & dataPoll) != dataPoll);
                break;
            case 8: /* In case of 64bit width the transformation is 1->8 */
                if (FLASH_MODE == X16)
                {
                    FirstData  = 0x00aa00aa; /* Data for the First  Cycle    */
                    SecondData = 0x00550055; /* Data for the Second Cycle    */
                    ThirdData  = 0x00800080; /* Data for the Third  Cycle    */
                    FourthData = 0x00aa00aa; /* Data for the Fourth Cycle    */
                    FifthData  = 0x00550055; /* Data for the Fifth  Cycle    */
                    SixthData  = 0x00300030; /* Data for the Sixth  Cycle    */
                    FirstAddr  = 0x5555;     /* Address for the First  Cycle */
                    SecondAddr = 0x2aaa;     /* Address for the Second Cycle */
                    ThirdAddr  = 0x5555;     /* Address for the Third  Cycle */
                    FourthAddr = 0x5555;     /* Address for the Fourth Cycle */
                    FifthAddr  = 0x2aaa;     /* Address for the Fifth  Cycle */
                }
                else /* (FLASH_MODE = 8 */
                {
                    FirstData  = 0xaaaaaaaa; /* Data for the First  Cycle    */
                    SecondData = 0x55555555; /* Data for the Second Cycle    */
                    ThirdData  = 0x80808080; /* Data for the Third  Cycle    */
                    FourthData = 0xAAAAAAAA; /* Data for the Fourth Cycle    */
                    FifthData  = 0x55555555; /* Data for the Fifth  Cycle    */
                    SixthData  = 0x30303030; /* Data for the Sixth  Cycle    */
                    FirstAddr  = 0xaaaa;     /* Address for the First  Cycle */
                    SecondAddr = 0x5555;     /* Address for the Second Cycle */
                    ThirdAddr  = 0xaaaa;     /* Address for the Third  Cycle */
                    FourthAddr = 0xaaaa;     /* Address for the Fourth Cycle */
                    FifthAddr  = 0x5555;     /* Address for the Fifth  Cycle */
                }

⌨️ 快捷键说明

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