am29lv800.c

来自「S3C24A0的完整BSP包,对开发此芯片的开发者很有用.」· C语言 代码 · 共 294 行

C
294
字号
#include <windows.h>
#include "loader.h"

// AMD AM29LV800BB flash manufacturer and device IDs.
//
#define AMD_MFG_CODE			0x01
#define AMD_DEV_CODE			0x225B	// We only support the AMD29LV800BB.

// AMD flash commands.
//
#define AMD_CMD_RESET			0xF0F0
#define AMD_CMD_AUTOSEL			0x9090
#define AMD_CMD_PROGRAM			0xA0A0
#define AMD_CMD_UNLOCK1			0xAAAA
#define AMD_CMD_UNLOCK2			0x5555
#define AMD_CMD_SECTERASE		0x8080
#define AMD_CMD_SECTERASE_CONFM 0x3030

// AMD flash access macros.
//
#define AMD_WRITE_USHORT(addr, data)	*((volatile unsigned short *)(addr)) = (data)
#define AMD_READ_USHORT(addr)           *((volatile unsigned short *)(addr))

#define AMD_CMD_ADDR					(AMD_FLASH_START + (0x5555 << 1))
#define AMD_UNLOCK_ADDR1				(AMD_FLASH_START + (0x5555 << 1))
#define AMD_UNLOCK_ADDR2				(AMD_FLASH_START + (0x2AAA << 1))

#define AMD_UNLOCK_CHIP()				AMD_WRITE_USHORT(AMD_UNLOCK_ADDR1, AMD_CMD_UNLOCK1); \
										AMD_WRITE_USHORT(AMD_UNLOCK_ADDR2, AMD_CMD_UNLOCK2)

#define AMD_WRITE_CMD(cmd)      		AMD_WRITE_USHORT(AMD_CMD_ADDR, cmd)


/*
    @func   BOOL | AMD28LV800_Wait | Checks for BIT6 to toggle (indicates flash has completed command).
    @rdesc  TRUE = Bit6 toggled, FASE = Bit6 didn't toggle.
    @comm    
    @xref   
*/
static BOOL AM29LV800_Wait(void) //Check if the bit6 toggle ends.
{
	volatile USHORT Status, OldStatus;

	OldStatus = *((volatile unsigned short *)AMD_FLASH_START);

	while(1)
	{
		Status = *((volatile unsigned short *)AMD_FLASH_START);
		if ((OldStatus & 0x40) == (Status & 0x40))
			break;
		if(Status & 0x20)
		{
			OldStatus = *((volatile unsigned short *)AMD_FLASH_START);
			Status    = *((volatile unsigned short *)AMD_FLASH_START);
			if((OldStatus & 0x40) == (Status & 0x40))
				return(FALSE);
			else
				return(TRUE);
		}
		OldStatus = Status;
	}
	return(TRUE);
}


/*
    @func   BOOL | AM29LV800_Init | Initialize the AMD AM29LV800BB flash.
    @rdesc  TRUE = Success, FALSE = Failure.
    @comm    
    @xref   
*/
BOOL AM29LV800_Init(DWORD dwAddr)
{
    USHORT MfgCode = 0;
    USHORT DevCode = 0;

    // Put part in autoselect mode so we can identify it.
    //
    AMD_WRITE_CMD(AMD_CMD_RESET);
    AMD_UNLOCK_CHIP();
    AMD_WRITE_CMD(AMD_CMD_AUTOSEL);

    MfgCode = AMD_READ_USHORT(dwAddr);
    DevCode = AMD_READ_USHORT(dwAddr + 2);
    if (MfgCode != AMD_MFG_CODE || DevCode != AMD_DEV_CODE)
    {
        RETAILMSG(1, (TEXT("ERROR: AM29LV800_Init: Bad manufacturer or device code: Mfg=0x%x, Dev=0x%x.\r\n"), MfgCode, DevCode));
        return(FALSE);
    }

    AMD_WRITE_CMD(AMD_CMD_RESET);
    return(TRUE);
}


/*
    @func   DWORD | GetSectorSize | Returns the size of the specified sector.  
	                                Note that the AMD AM29LV800BB is a "bottom" boot block non-uniform sector size part.
    @rdesc  Sector size for the specified sector.
    @comm    
    @xref   
*/
static DWORD GetSectorSize(BYTE SectorNumber)
{
    switch(SectorNumber)
    {
    case 0:
        return(16 * 1024);
    case 1:
        return(8 * 1024);
    case 2:
        return(8 * 1024);
    case 3:
        return(32 * 1024);
    default:
        return(64 * 1024);
    }

    return(0);
}


/*
    @func   DWORD | GetSectorAddress | Returns the starting address (flash byte offset) for the specified sector number.
	                                   Note that the AMD AM29LV800BB is a "bottom" boot block non-uniform sector size part.
    @rdesc  Starting address of the specified sector.
    @comm    
    @xref   
*/
static DWORD GetSectorAddress(BYTE SectorNumber)
{
	DWORD dwSectorAddr = 0;

    switch(SectorNumber)
    {
    case 0:
        dwSectorAddr = 0;
		break;
    case 1:
        dwSectorAddr = 0x4000;
		break;
    case 2:
        dwSectorAddr = 0x6000;
		break;
    case 3:
        dwSectorAddr = 0x8000;
		break;
    default:
        dwSectorAddr = ((SectorNumber - 3) * 0x10000);
		break;
    }

    return(AMD_FLASH_START + dwSectorAddr);
}


/*
    @func   BYTE | GetSectorNumber | Returns the sector number corresponding to the flash address specified.
	                                   Note that the AMD AM29LV800BB is a "bottom" boot block non-uniform sector size part.
    @rdesc  Sector number.
    @comm    
    @xref   
*/
static BYTE GetSectorNumber(DWORD dwOffset)
{
	BYTE SectorNumber = 0;

	if (dwOffset < 0x4000)
	{
		SectorNumber = 0;
	}
	else if (dwOffset < 0x6000)
	{
		SectorNumber = 1;
	}
	else if (dwOffset < 0x8000)
	{
		SectorNumber = 2;
	}
	else if (dwOffset < 0x10000)
	{
		SectorNumber = 3;
	}
	else
	{
		SectorNumber = 4 + (UCHAR)((dwOffset - 0x10000) / (64 * 1024));
	}

    return(SectorNumber);
}


/*
    @func   BOOL | EraseSector | Erases the specified flash sector.
    @rdesc  TRUE = Success, FALSE = Failure.
    @comm    
    @xref   
*/
static BOOL EraseSector(BYTE dwSect)
{
    AMD_UNLOCK_CHIP();
    AMD_WRITE_CMD(AMD_CMD_SECTERASE);
    AMD_UNLOCK_CHIP();
    AMD_WRITE_USHORT(GetSectorAddress(dwSect), AMD_CMD_SECTERASE_CONFM);
	AM29LV800_Wait();

	return(TRUE);
}


/*
    @func   BOOL | AM29LV800_WriteFlash | Writes the specified data to AMD AM29LV800 flash starting at the sector offset specified.
    @rdesc  TRUE = Success, FALSE = Failure.
    @comm    
    @xref   
*/
BOOL AM29LV800_WriteFlash(DWORD dwOffset, PBYTE pData, DWORD dwLength)
{
    DWORD dwByteOffset;
    
	RETAILMSG(1, (TEXT("AM29LV800_WriteFlash: Writing 0x%x bytes to flash (offset 0x%x bytes)...\r\n"), dwLength, dwOffset));

    for (dwByteOffset = 0 ; dwByteOffset < dwLength ; dwByteOffset += sizeof(USHORT))
    {
		AMD_UNLOCK_CHIP();
		AMD_WRITE_CMD(AMD_CMD_PROGRAM);
        AMD_WRITE_USHORT((AMD_FLASH_START + dwOffset + dwByteOffset), *(PUSHORT)(pData + dwByteOffset));
		AM29LV800_Wait();
    }

	return(TRUE);
}


/*
    @func   BOOL | AM29LV800_ReadFlash | Reads the specified number of bytes from flash starting at the specified offset.
    @rdesc  TRUE = Success, FALSE = Failure.
    @comm    
    @xref   
*/
BOOL AM29LV800_ReadFlash(DWORD dwOffset, PBYTE pData, DWORD dwLength)
{
	PBYTE pReadPtr = (PBYTE)(AMD_FLASH_START + dwOffset);
    
	//RETAILMSG(1, (TEXT("AM29LV800_ReadFlash: Reading 0x%x bytes from flash (offset 0x%x bytes)...\r\n"), dwLength, dwOffset));

	memcpy(pData, pReadPtr, dwLength);

	return(TRUE);
}


/*
    @func   BOOL | AM29LV800_EraseFlash | Erases AMD AM29LV800 flash starting at the sector offset specified.
    @rdesc  TRUE = Success, FALSE = Failure.
    @comm    
    @xref   
*/
BOOL AM29LV800_EraseFlash(DWORD dwOffset, DWORD dwLength)
{
    BYTE NumSects = 0;
    DWORD dwEraseLength = 0;
	BYTE StartSector = GetSectorNumber(dwOffset);

    while(dwEraseLength < dwLength)
    {
        dwEraseLength += GetSectorSize(StartSector + NumSects);
        ++NumSects;
    }

	if (!NumSects)
	{
		NumSects = 1;
        dwEraseLength = GetSectorSize(StartSector);
	}

    RETAILMSG(1, (TEXT("AM29LV800_EraseFlash: Requested 0x%x bytes, Erasing 0x%x sectors (0x%x bytes) (offset 0x%x bytes)...\r\n"), dwLength, NumSects, dwEraseLength, dwOffset)); 

    AMD_WRITE_CMD(AMD_CMD_RESET);

    while(NumSects--)
    {
        if (!EraseSector(StartSector + NumSects))
        {
            RETAILMSG(1, (TEXT("ERROR: AM29LV800_EraseFlash: EraseSector failed on sector 0x%x!\r\n"), NumSects));
            return(FALSE);
        }
    }
    AMD_WRITE_CMD(AMD_CMD_RESET);

	return(TRUE);
}

⌨️ 快捷键说明

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