📄 amd_flash.c
字号:
/*
* Wait for program operation to finish
* (Data# Polling Algorithm)
*/
retry = 0;
while (1)
{
status = *dst;
if ((status & AMD_FLASH_CMD_DATA(0x80)) ==
(data & AMD_FLASH_CMD_DATA(0x80)))
{
break;
}
if (status & AMD_FLASH_CMD_DATA(0x20))
{
status = *dst;
if ((status & AMD_FLASH_CMD_DATA(0x80)) ==
(data & AMD_FLASH_CMD_DATA(0x80)))
{
break;
}
if (++retry > 1024)
{
break;
}
}
}
}
/********************************************************************/
#ifdef __MWERKS__
__relocate_code__
#endif
#if defined(__GNUC__)
int amd_flash_program(ADDRESS dest, ADDRESS source,
int bytes, int erase,
void (*func)(void),
void (*putchar)(char))
__attribute__ ((section(".code_relocation")));
#endif /* defined(__GNUC__) */
int
amd_flash_program(ADDRESS dest, ADDRESS source,
int bytes, int erase,
void (*func)(void),
void (*putchar)(char))
{
AMD_FLASH_CELL *src, *dst;
int hashi=1,hashj=0;
char hash[5];
hash[0]=8; /* Backspace */
hash[1]=124;/* "|" */
hash[2]=47; /* "/" */
hash[3]=45; /* "-" */
hash[4]=92; /* "\" */
src = (AMD_FLASH_CELL *)source;
dst = (AMD_FLASH_CELL *)dest;
/*
* Place device in read mode
*/
pFlash[0] = AMD_FLASH_CMD_DATA(0xAA);
pFlash[0] = AMD_FLASH_CMD_DATA(0x55);
pFlash[0] = AMD_FLASH_CMD_DATA(0xF0);
/*
* Erase device if necessary
*/
if (erase)
{
amd_flash_erase(dest, bytes, putchar);
}
/*
* Program device
*/
while (bytes > 0)
{
amd_flash_program_cell(dst,*src);
/* Verify Write */
if (*dst == *src)
{
bytes -= AMD_FLASH_CELL_BYTES;
*dst++, *src++;
if ((putchar != NULL))
{
/* Hash marks to indicate progress */
if (hashj == 0x1000)
{
hashj = -1;
putchar(hash[0]);
putchar(hash[hashi]);
hashi++;
if (hashi == 5)
{
hashi=1;
}
}
hashj++;
}
}
else
break;
}
/*
* Place device in read mode
*/
pFlash[0] = AMD_FLASH_CMD_DATA(0xAA);
pFlash[0] = AMD_FLASH_CMD_DATA(0x55);
pFlash[0] = AMD_FLASH_CMD_DATA(0xF0);
if (putchar != NULL)
{
putchar(hash[0]);
}
/*
* If a function was passed in, call it now
*/
if ((func != NULL))
{
func();
}
return ((int)src - (int)source);
}
/********************************************************************/
#ifdef __MWERKS__
__relocate_code__
#endif
#if defined(__GNUC__)
ADDRESS amd_flash_sector_start(ADDRESS addr)
__attribute__ ((section(".code_relocation")));
#endif /* defined(__GNUC__) */
ADDRESS
amd_flash_sector_start(ADDRESS addr)
{
/*
* Returns beginning of sector containing ADDRESS
*/
int i;
for (i = 0; i < AMD_FLASH_SECTORS; i++)
{
if (addr >= (ADDRESS)((ADDRESS)pFlash + SOFFSET(i)) &&
addr <= (ADDRESS)((ADDRESS)pFlash + SOFFSET(i) + (SSIZE(i) - 1)))
{
return (ADDRESS)((ADDRESS)pFlash + SOFFSET(i));
}
}
return NULL;
}
/********************************************************************/
#ifdef __MWERKS__
__relocate_code__
#endif
#if defined(__GNUC__)
ADDRESS amd_flash_sector_end(ADDRESS addr)
__attribute__ ((section(".code_relocation")));
#endif /* defined(__GNUC__) */
ADDRESS
amd_flash_sector_end(ADDRESS addr)
{
/* Returns end of sector containing ADDRESS */
int i;
for (i = 0; i < AMD_FLASH_SECTORS; i++)
{
if (addr >= (ADDRESS)((ADDRESS)pFlash + SOFFSET(i)) &&
addr <= (ADDRESS)((ADDRESS)pFlash + SOFFSET(i) + (SSIZE(i) - 1)))
{
return (ADDRESS)((ADDRESS)pFlash + SOFFSET(i) + (SSIZE(i) - 1));
}
}
return NULL;
}
/********************************************************************/
#if defined(AMD_FLASH_AM29BDD160GB_16BIT)
/********************************************************************/
#ifdef __MWERKS__
__relocate_code__
#endif
#if defined(__GNUC__)
int amd_flash_secsi_read(ADDRESS dest, ADDRESS source, int bytes )
__attribute__ ((section(".code_relocation")));
#endif /* defined(__GNUC__) */
int
amd_flash_secsi_read(ADDRESS dest, ADDRESS source, int bytes )
{
int i;
int boot_block = amd_flash_boot_get();
unsigned int secsi_base_addr =
(boot_block == BOOT_BLOCK_BOTTOM) ? 0x1FFFC0 : 0x0;
/* Enter the secured sector */
pFlash[ADDR1] = AMD_FLASH_CMD_DATA(0xAA);
pFlash[ADDR2] = AMD_FLASH_CMD_DATA(0x55);
pFlash[ADDR1] = AMD_FLASH_CMD_DATA(0x88);
/* Would use memcpy, but it checks for NULL srource. Which
* can be the case when using top boot block device */
for( i = 0; i < bytes; i++ ) {
*(unsigned char *)(dest + i) =
*(unsigned char *)(secsi_base_addr + source + i);
}
/* Exit the secured sector */
pFlash[ADDR1] = AMD_FLASH_CMD_DATA(0xAA);
pFlash[ADDR2] = AMD_FLASH_CMD_DATA(0x55);
pFlash[ADDR1] = AMD_FLASH_CMD_DATA(0x90);
pFlash[ADDR1] = AMD_FLASH_CMD_DATA(0x00);
return( bytes );
}
/********************************************************************/
#ifdef __MWERKS__
__relocate_code__
#endif
#if defined(__GNUC__)
int amd_flash_secsi_write(ADDRESS dest, ADDRESS source, int bytes )
__attribute__ ((section(".code_relocation")));
#endif /* defined(__GNUC__) */
int
amd_flash_secsi_write(ADDRESS dest, ADDRESS source, int bytes )
{
int bytes_prog;
int boot_block = amd_flash_boot_get();
unsigned int secsi_base_addr =
(boot_block == BOOT_BLOCK_BOTTOM) ? 0x1FFFC0 : 0x0;
/* Enter the secured sector */
pFlash[ADDR1] = AMD_FLASH_CMD_DATA(0xAA);
pFlash[ADDR2] = AMD_FLASH_CMD_DATA(0x55);
pFlash[ADDR1] = AMD_FLASH_CMD_DATA(0x88);
bytes_prog = amd_flash_program(
(secsi_base_addr + dest), source, bytes, 0, NULL, NULL );
/* Exit the secured sector */
pFlash[ADDR1] = AMD_FLASH_CMD_DATA(0xAA);
pFlash[ADDR2] = AMD_FLASH_CMD_DATA(0x55);
pFlash[ADDR1] = AMD_FLASH_CMD_DATA(0x90);
pFlash[ADDR1] = AMD_FLASH_CMD_DATA(0x00);
return( bytes_prog );
}
/********************************************************************/
#ifdef __MWERKS__
__relocate_code__
#endif
#if defined(__GNUC__)
int amd_flash_secsi_protect(void)
__attribute__ ((section(".code_relocation")));
#endif /* defined(__GNUC__) */
int
amd_flash_secsi_protect(void)
{
int i;
unsigned short protected;
pFlash[ADDR1] = AMD_FLASH_CMD_DATA(0xAA);
pFlash[ADDR2] = AMD_FLASH_CMD_DATA(0x55);
pFlash[ADDR1] = AMD_FLASH_CMD_DATA(0x60);
pFlash[SECSI_PROTECT_ADDR] = AMD_FLASH_CMD_DATA(0x68);
/* Not sure why this dealy is required, but without it
* the algorithm does not work */
for( i = 0; i < 100000; i++ ) ;
pFlash[SECSI_PROTECT_ADDR] = AMD_FLASH_CMD_DATA(0x48);
protected = pFlash[SECSI_PROTECT_ADDR];
pFlash[ADDR1] = AMD_FLASH_CMD_DATA(0xAA);
pFlash[ADDR2] = AMD_FLASH_CMD_DATA(0x55);
pFlash[ADDR1] = AMD_FLASH_CMD_DATA(0xF0);
if( protected & 0x01 ) {
return 1;
}
return 0;
}
/********************************************************************/
#ifdef __MWERKS__
__relocate_code__
#endif
#if defined(__GNUC__)
int amd_flash_boot_get(void)
__attribute__ ((section(".code_relocation")));
#endif /* defined(__GNUC__) */
int
amd_flash_boot_get(void)
{
unsigned short dev_id1, dev_id2, boot_block;
pFlash[ADDR1] = AMD_FLASH_CMD_DATA(0xAA);
pFlash[ADDR2] = AMD_FLASH_CMD_DATA(0x55);
pFlash[ADDR1] = AMD_FLASH_CMD_DATA(0x90);
dev_id1 = pFlash[DEVADDR1];
dev_id2 = pFlash[DEVADDR2];
boot_block = pFlash[DEVADDR3];
pFlash[ADDR1] = AMD_FLASH_CMD_DATA(0xAA);
pFlash[ADDR2] = AMD_FLASH_CMD_DATA(0x55);
pFlash[ADDR1] = AMD_FLASH_CMD_DATA(0xF0);
return( boot_block );
}
#endif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -