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

📄 intel_c3_2x16.c

📁 Ep93XX TionProV2 BSP
💻 C
📖 第 1 页 / 共 2 页
字号:
//**********************************************************************
//                                                                      
// Filename: intel_c3_2x16.c
//                                                                      
// Description: Routines for programming the Intel J3 flash 2x16.
//
// THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF
// ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO
// THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
// PARTICULAR PURPOSE.
//
// Use of this source code is subject to the terms of the Cirrus end-user
// license agreement (EULA) under which you licensed this SOFTWARE PRODUCT.
// If you did not accept the terms of the EULA, you are not authorized to 
// use this source code. For a copy of the EULA, please see the 
// EULA.RTF on your install media.
//
// Copyright(c) Cirrus Logic Corporation 2005, All Rights Reserved                       
//                                                                      
//**********************************************************************
#include<windows.h>
#include<hwdefs.h>
#include<halether.h>
#include<debugtimer.h>
#include "flash.h"
//#include "smcdefs.h"
#include "intel_c3_2x16.h"

#define INTELC3_DEBUG	0
//
// Flash commands.
//
#define     FLASH_BLOCK_SIZE            0x40000
#define     FLASH_BLOCK_MASK            ~(FLASH_BLOCK_SIZE - 1)
#define     FLASH_BUFFER_SIZE           64
//
//
//
#define     FLASH_READ_MODE             0x00FF00FF
#define     FLASH_BLOCK_ERASE           0x00200020
#define     FLASH_CLEAR_STATUS          0x00500050
#define     FLASH_BLOCK_ERASE_RESUME    0x00d000d0
#define     FLASH_BLOCK_PROGRAM_RESUME  0x00d000d0
#define     FLASH_WRITE_TO_BUFFER       0x00E800E8
#define     FLASH_READ_STATUS           0x00700070
#define     FLASH_LOCK_BIT              0x00600060
#define     FLASH_LOCK_SET              0x00010001
#define     FLASH_LOCK_CLEAR            0x00d000d0


#define	  FLASH_READ_ID_CODES           0x00900090
#define     FLASH_READ_QUERY            0x00980098

#define     STATUS_WRITE_READY          0x00800080
#define     EXT_STATUS_WBUFFER_READY    0x00800080


//
//
//
static DWORD    gdwStartAddr            = 0;
static DWORD    gdwLength               = 0;
static volatile PULONG  gpulCurAddr     = 0;
static BOOL     gbFlashEraseComplete    = FALSE;
static BOOL     gbUnlocked              = TRUE;

extern DWORD				gdwFlashSize			;
static DWORD                gdwManufactureId		=0;
struct EraseBlockInfo       sBlockInfo[4];
static DWORD				gdwNumEraseRegion		=0;
static DWORD				gdwCurEraseRegion		=0;
static DWORD				gdwCurEraseBlock		=0;
static DWORD				gdwCurEraseBlockMask	=0;
static DWORD				gdwFlashBufferSize		=0;

//
// Function prototypes
//
static int  WaitForReady(volatile ULONG *pFlash, ULONG ulTimeoutInMsec, ULONG ulBitField);


BOOL  GotEraseFlashSectorIndex_C3_2X16(DWORD dwStartAddr)
{
   DWORD i,j,dwEraseLen;

   dwEraseLen = dwStartAddr-FLASH_VIRTUAL_MEMORY;
   
	for(i = 0; i <gdwNumEraseRegion; i++) 
	{
		for(j = 0; j< sBlockInfo[i].blocks; j++) 
		{
			if(dwEraseLen > sBlockInfo[i].block_size) 
			{
				dwEraseLen = dwEraseLen - sBlockInfo[i].block_size;
			} 
			else if(dwEraseLen ==sBlockInfo[i].block_size)
			{
				gdwCurEraseRegion = i;
				gdwCurEraseBlock    = j;
				return TRUE;
			}
			else 
			{
				gdwCurEraseRegion = i;
				gdwCurEraseBlock    = j;
				return FALSE;
			}
		}
	}
	return FALSE;
}

DWORD GetEraseFlashBlockSize_C3_2X16(void)
{
    DWORD dwBlockSize;

    dwBlockSize = sBlockInfo[gdwCurEraseRegion].block_size;

    return dwBlockSize;

}

BOOL QueryIntelFlashC3_2x16(void)
{
	DWORD iIdx = 0;
	
	//
	//re set the SMC setting
	//
	gpulCurAddr	= (unsigned long*)PHYSICAL_ADDR_FLASH;
	//
	// Query 16 bitwidth id.
	//
    *gpulCurAddr 		= FLASH_READ_ID_CODES;
    gdwManufactureId 	= *gpulCurAddr;

	//
	// Query 16 bitwidth flash.
	//
	*gpulCurAddr 			= FLASH_READ_QUERY;

	gdwFlashSize			= 1 << (*(gpulCurAddr + 0x27) & 0xFF);
	gdwFlashSize			=gdwFlashSize*2;

	gdwNumEraseRegion 	= *(gpulCurAddr + 0x2c) & 0xFFFF;

	for(iIdx = 0; iIdx < gdwNumEraseRegion; iIdx++)
	{
		sBlockInfo[iIdx].blocks = (*(gpulCurAddr + 0x2d + (iIdx<<2)) & 0xFFFF) + 1;

		sBlockInfo[iIdx].block_size =
			(((*(gpulCurAddr + 0x30 + (iIdx<<2)) & 0xFFFF) << 8) +
			(*(gpulCurAddr + 0x2f + (iIdx<<2)) & 0xFFFF)) << 8;

		sBlockInfo[iIdx].block_size =sBlockInfo[iIdx].block_size *2;
	}

	//
	// Put the FLASH into read array mode.
	//
	*gpulCurAddr = FLASH_READ_MODE;

	EdbgOutputDebugString
	(
		"Flash Query: Nor Flash ID %x,size %x \r\n",
		gdwManufactureId,gdwFlashSize
	);
	if((gdwManufactureId == 0x00890089)||((gdwManufactureId == 0x002c002c)))
	{
		return TRUE;
	}
	else
		return TRUE;
}



//****************************************************************************
//  StartEraseFlashJ3_2x16
//****************************************************************************
//  dwStartAddr - Start address, in flash, to start erasing from. The 
//                platform loader code needs to ensure this is a flash 
//                block-aligned address or needs to handle it specially if 
//                it is not. 
//
//  dwLength    - Number of bytes of flash to be erased. 
//
//  return      TRUE indicates success. FALSE indicates failure.
//
BOOL StartEraseFlashC3_2x16 (DWORD dwStartAddr, DWORD dwLength )
{
    //
    // Check to make sure that the flash is on a Block boundary.
    //
    if(GotEraseFlashSectorIndex_C3_2X16(dwStartAddr))
    {
        EdbgOutputDebugString
        (
            "StartErase %x is not on a flash block boundary.\r\n",
            dwStartAddr
        );
        return FALSE;
    }
      RETAILMSG(INTELC3_DEBUG,(L"StartEraseFlashC3_2x16 %x %x %x %x\r\n",
            gdwCurEraseRegion,gdwCurEraseRegion,gdwCurEraseBlock,sBlockInfo[gdwCurEraseRegion].block_size
        ));
    //
    // Save off the start address and the length.
    //
    gdwStartAddr    = dwStartAddr;
    gdwLength       = gdwFlashSize;//dwLength;
    gpulCurAddr     = (PULONG) dwStartAddr;
    //
    // Put the flash in read mode to make sure that we are not in the middle 
    // of a command.
    //
    *gpulCurAddr = FLASH_READ_MODE;

    //
    // Clear the lock bit.
    //
    *gpulCurAddr = FLASH_LOCK_BIT;
    *gpulCurAddr = FLASH_LOCK_CLEAR;
    *gpulCurAddr = FLASH_READ_STATUS;
    gbUnlocked  = TRUE;

    return TRUE;
}

//****************************************************************************
// ContinueEraseFlashJ3_2x16
//****************************************************************************
// Continues the flash erasing processes for J3 Flash.  See 
// OEMContinueEraseFlash for Parameter description.
// 
//
void ContinueEraseFlashC3_2x16(void)
{
    //
    // Check to see if the flash has been completely erased. If so just
    // return.
    //
    if(gbFlashEraseComplete)
    {
        return;
    }
    //
    // Check to see if the last block has been erased.
    //
    if((*gpulCurAddr & EXT_STATUS_WBUFFER_READY) != EXT_STATUS_WBUFFER_READY)
    {
        return;
    }
    //
    // Clear Status.
    //
    *gpulCurAddr  = FLASH_CLEAR_STATUS;

	RETAILMSG(INTELC3_DEBUG,(L"ContinueEraseFlashC3_2x16 0 %x \r\n",gbUnlocked ));
        
    if(gbUnlocked)
    {
        //
        // Erase the flash block
        //
        *gpulCurAddr = FLASH_BLOCK_ERASE;
        *gpulCurAddr = FLASH_BLOCK_ERASE_RESUME;

        //
        // Read erase status.
        //
        *gpulCurAddr = FLASH_READ_STATUS;
        gbUnlocked   = FALSE;
    }
    else
    {
        //
        // Check to see if we have finished erasing the flash memory.
        //
        if(gdwStartAddr + gdwLength < ((DWORD)gpulCurAddr) + sBlockInfo[gdwCurEraseRegion].block_size)
        {
            gbFlashEraseComplete = TRUE;
            return;
        }
        
		RETAILMSG(INTELC3_DEBUG,(TEXT("ContinueEraseFlashC3_2x16 1 %x \r\n"),
            sBlockInfo[gdwCurEraseRegion].block_size));

        //
        // Advance to the next block.
        //
        gpulCurAddr+= sBlockInfo[gdwCurEraseRegion].block_size>>2;
		//
		//go to the next bloxk
		//
		gdwCurEraseBlock = gdwCurEraseBlock+1;
		if(gdwCurEraseBlock>=sBlockInfo[gdwCurEraseRegion].blocks)
		{
			gdwCurEraseBlock=0;
			gdwCurEraseRegion=gdwCurEraseRegion+1;
			//
			//return error the current postiotn > the Flash MAX length
			//
			if(gdwCurEraseRegion>=gdwNumEraseRegion)
				return ;
		}


		RETAILMSG(INTELC3_DEBUG,(L"ContinueEraseFlashC3_2x16 3 %x %x %x %x\r\n",
			gdwCurEraseRegion,gdwCurEraseRegion,gdwCurEraseBlock,
			sBlockInfo[gdwCurEraseRegion].block_size));
        //
        // Clear the lock bits if the flash block was locked.
        //
        *gpulCurAddr = FLASH_LOCK_BIT;
        *gpulCurAddr = FLASH_LOCK_CLEAR;
        *gpulCurAddr = FLASH_READ_STATUS;

        gbUnlocked  = TRUE;
    }
}

//****************************************************************************
// FinishEraseFlashJ3_2x16
//****************************************************************************
// Finishes the flash eraseing processes for J3 Flash.  See 
// OEMFinishEraseFlash for paramter description.
//
BOOL  FinishEraseFlashC3_2x16 (void)
{
    BOOL    bSuccess;

⌨️ 快捷键说明

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