📄 drv.c
字号:
ffsdrv_intel_write_halfword((uint16 *) mydst,
mysrc[0] | (mysrc[1] << 8));
size -= 2;
mysrc += 2;
mydst += 2;
}
if (size == 1)
ffsdrv_write_byte(mydst++, *mysrc++);
}
}
#endif
/******************************************************************************
* RAM Family Functions
******************************************************************************/
void ffsdrv_ram_write_halfword(volatile uint16 *dst, uint16 value)
{
*dst = value;
}
void ffsdrv_ram_write(void *dst, const void *src, uint16 size)
{
uint8 *mydst = dst;
const uint8 *mysrc = src;
if (size == 0)
return;
else if (size == 1)
ffsdrv_write_byte(mydst, *mysrc);
else {
if ((int) mydst & 1) {
ffsdrv_write_byte(mydst++, *mysrc++);
size--;
}
while (size >= 2) {
ffsdrv_ram_write_halfword((uint16 *) mydst, mysrc[0]|(mysrc[1] << 8));
size -= 2;
mysrc += 2;
mydst += 2;
}
if (size == 1)
ffsdrv_write_byte(mydst++, *mysrc++);
}
}
void ffsdrv_ram_erase(uint8 block)
{
int i;
char *addr;
addr = (char *)block2addr(block);;
for (i = 0; i < 1 << dev.binfo[block].size_ld; i++)
{
*addr++ = 0xFF;
}
}
void ffsdrv_ram_erase_sector(void *dst)
{
uint8 block;
for(block=0; block<dev.numblocks; block++)
{
uint8 *start = (uint8 *)block2addr(block);
uint8 *end = start + dev.binfo[block].size_ld;
if ((start <= (uint8 *)dst) && ((uint8 *)dst < end))
{
ffsdrv.erase(block);
break;
}
}
}
int ffsdrv_amd_mb_init(void)
{
int i =0;
UINT32 addr = 0;
UINT32 read_value = 0;
UINT32 base_addr = (UINT32)dev.base;
for (i = 0; i < dev.numblocks; i++)
{
addr = dev.binfo[i].offset;
//temp_addr = (volatile uint16 *)(base_addr + addr);
//base_addr = (volatile UINT16*)dev.base + addr;
/* Command Set Entry */
FLASH_WRITE_HALFWORD((base_addr+ addr + 0xAAA),0xAA);
FLASH_WRITE_HALFWORD((base_addr+ addr + 0x555),0x55);
FLASH_WRITE_HALFWORD((base_addr+ addr + 0xAAA),0xE0);
/* DYB Clear */
//temp_addr = base_addr + addr;
FLASH_WRITE_HALFWORD((base_addr + addr), 0xA0);
FLASH_WRITE_HALFWORD((base_addr + addr), 0x01);
/* Exit command sequence*/
FLASH_WRITE_HALFWORD(base_addr, 0x90);
FLASH_WRITE_HALFWORD(base_addr, 0x00);
}
return EFFS_OK;
}
/******************************************************************************
* Void Functions
******************************************************************************/
int ffsdrv_null_init(void)
{
ttw(ttr(TTrDrvOther, "ffsdrv_null_init()" NL));
return 0;
}
void ffsdrv_null_erase(uint8 block)
{
ttw(ttr(TTrDrvErase, "ffsdrv_null_erase(%d)" NL, block));
}
void ffsdrv_null_erase_sector(void *dst)
{
ttw(ttr(TTrDrvErase, "ffsdrv_null_erase_sector(0x%x)" NL, dst));
}
void ffsdrv_null_write_buffer(volatile uint16 *addr, volatile uint16 *src,
uint16 words2write)
{
ttw(ttr(TTrDrvWrite, "ffsdrv_null_write_buffer(0x%x, 0x%x)"
NL, addr, words2write));
}
void ffsdrv_null_write_halfword(volatile uint16 *addr, uint16 value)
{
ttw(ttr(TTrDrvWrite, "ffsdrv_null_write_halfword(0x%x, 0x%x)" NL, addr, value));
}
void ffsdrv_null_write_suspend(void)
{
ttw(ttr(TTrDrvWrite, "ffsdrv_null_write_suspend()" NL));
}
void ffsdrv_null_write_resume(void)
{
ttw(ttr(TTrDrvWrite, "ffsdrv_null_write_resume()" NL));
}
void ffsdrv_null_write(void *dst, const void *src, uint16 size)
{
ttw(ttr(TTrDrvWrite, "ffsdrv_null_write(0x%x, 0x%x, %d)" NL, dst, src, size));
}
void ffsdrv_null_erase_suspend(void)
{
ttw(str(TTrDrvErase, "ffsdrv_null_erase_suspend()" NL));
}
void ffsdrv_null_erase_resume(void)
{
ttw(str(TTrDrvErase, "ffsdrv_null_erase_resume()" NL));
}
void ffsdrv_null_write_end(void)
{
ttw(str(TTrDrvWrite, "ffsdrv_null_write_end()" NL));
}
void ffsdrv_null_erase_end(void)
{
ttw(str(TTrDrvErase, "ffsdrv_null_erase_end()" NL));
}
/******************************************************************************
* Test Driver Functions
******************************************************************************/
#if (TARGET == 1)
// Range check. NOTEME rename to ffsdrv_range_check()?
int ffsdrv_write_check(char *addr, int size)
{
offset_t offset, last;
offset = addr2offset(addr);
last = dev.binfo[dev.numblocks-1].offset
+ (1 << dev.binfo[dev.numblocks-1].size_ld);
if (offset < 0 || (offset + size) > last) {
ttw(ttr(TTrAll, "ffsdrv_write_check() failed (addr = 0x%x, size = %d)"
NL, (int) addr, size));
return -1;
}
return EFFS_OK;
}
// FIXME update this to work in target
void ffsdrv_write_error(uint16 old, uint16 new) {}
#else
static char *image_addr = 0;
static int image_size = 0;
#ifdef WIN32
HANDLE image_fd, map_fd;
#else //WIN32
static int image_fd = 0;
#endif //WIN32
/*Modified by ZhangTing for PC simulator 2006-08-26*/
#ifndef _LEGEND_EMULATOR_
extern int arg_removeimage;
extern char *arg_imagename;
#else
int arg_removeimage;
char *arg_imagename = "ffs.bin";
#endif
/*End*/
extern void test_fatal_printf(char *format, ...);
int ffsdrv_write_check(char *addr, int size)
{
offset_t offset, last;
offset = addr2offset(addr);
last = dev.binfo[dev.numblocks-1].offset
+ (1 << dev.binfo[dev.numblocks-1].size_ld);
if (offset < 0 || (offset + size) > last) {
fprintf(stderr, "ffsdrv_write_check() failed (addr = 0x%x, size = %d)\n",
(int) addr, size);
fprintf(stdout, "ffsdrv_write_check() failed (addr = 0x%x, size = %d)\n",
(int) addr, size);
exit (1);
}
return 0;
}
void ffsdrv_write_error(uint16 old, uint16 new)
{
#ifndef _LEGEND_EMULATOR_ /*Modified by ZhangTing for PC simulator 2006-06-15*/
test_fatal_printf("FATAL: Attempt to rewrite 0 to 1 bit "
"(old:0x%x/%c new:0x%x/%c)\n",
old, (old < ' ' ? '?' : old),
new, (new < ' ' ? '?' : new));
#endif
}
void ffsdrv_test_write_halfword(volatile uint16 *addr, uint16 value)
{
tw(tr(TR_FUNC, TrDrvWrite, "test_write_halfword(0x%05x, 0x%x)\n",
addr2offset(addr), value));
if (POWERFAIL_ENABLED)
POWERFAIL_WRITE(addr, value);
ffsdrv_write_check((uint8 *) addr, 2);
if (~*addr & value)
ffsdrv_write_error(*addr, value);
*addr = value;
}
void ffsdrv_test_write_buffer(volatile uint16 *addr, volatile uint16 *src,
uint16 words2write)
{
int i , num_bytes = words2write * 2;
uint8 *mysrc = (uint8 *) src;
uint8 *mydst = (uint8 *) addr;
tw(tr(TR_FUNC, TrDrvWrite, "test_write_buffer(0x%x, 0x%05x, 0x%x)\n",
addr2offset(addr), src, words2write));
if (words2write <= 1 || words2write * 2 > dev.write_buffersize)
tw(tr(TR_FUNC, TrAll, "ERROR wrong number of words (%d)\n", words2write));
ffsdrv_write_check((uint8 *) addr, words2write * 2);
// NOTEME this can be made a lot more simpel as the last input arg have
// changed from num_bytes to words2write
for (i = 0; i < num_bytes; i++) {
if (~*(mydst + i) & *(mysrc + i))
ffsdrv_write_error(*(mydst + i), *(mysrc + i));
// Trigger only the powerfail framework at halfword aligned addresses
if (POWERFAIL_ENABLED && !((int) (mydst + i) & 1))
// Note POWERFAIL_WRITE() write halfwords!
POWERFAIL_WRITE((uint16 *) mydst + i/2,
*(mysrc + i/2) | *(mysrc + i/2 + 1) << 8);
*(mydst + i) = *(mysrc + i);
}
}
void ffsdrv_test_write(void *dst, const void *src, uint16 size)
{
uint8 *mydst = dst;
const uint8 *mysrc = src;
tw(tr(TR_FUNC, TrDrvWrite, "test_write(0x%05x, 0x%x, %d)\n",
addr2offset(mydst), mysrc, size));
if (size > 0)
{
if ((int) mydst & 1) {
ffsdrv_write_byte(mydst++, *mysrc++);
size--;
}
while (size >= 2) {
ffsdrv_test_write_halfword((uint16 *) mydst, mysrc[0]|(mysrc[1] << 8));
size -= 2;
mysrc += 2;
mydst += 2;
}
if (size == 1)
ffsdrv_write_byte(mydst++, *mysrc++);
}
}
void ffsdrv_test_erase(uint8 block)
{
int i;
uint8 *addr;
if (POWERFAIL_ENABLED)
POWERFAIL_ERASE(block);
addr = (uint8 *)block2addr(block);
tw(tr(TR_FUNC, TrDrvErase, "ffsdrv_test_erase(%d)\n", block));
for (i = 0; i < 1 << dev.binfo[block].size_ld; i++) {
*addr++ = 0xFF;
}
}
void ffsdrv_test_erase_sector(void *dst)
{
uint8 block;
for(block=0; block<dev.numblocks; block++)
{
uint8 *start = (uint8 *)block2addr(block);
uint8 *end = start + dev.binfo[block].size_ld;
if ((start <= (uint8 *)dst) && ((uint8 *)dst < end))
{
ffsdrv.erase(block);
break;
}
}
}
char *ffsdrv_test_create(void)
{
// If flash image file already exists, open the file, and mmap it.
// Otherwise, create file, fill file with 1's, then mmap it.
int i;
struct stat statbuf;
#ifdef WIN32
OFSTRUCT lpReOpenBuff;
DWORD last_error;
SECURITY_ATTRIBUTES lpAttributes;
lpAttributes.nLength = sizeof (lpAttributes);
lpAttributes.lpSecurityDescriptor = NULL;
lpAttributes.bInheritHandle = TRUE;
#endif
image_size = (int) dev.binfo[dev.numblocks - 1].offset +
(1 << dev.binfo[dev.numblocks - 1].size_ld);
tw(tr(TR_BEGIN, TrDrvInit, "ffsdrv_test_create() {\n"));
tw(tr(TR_FUNC, TrDrvInit, "%s image: '%s', size = %d\n",
arg_removeimage ? "new" : "current", arg_imagename, image_size));
// create file if it does not exist
#ifdef WIN32
if( arg_removeimage || OpenFile( arg_imagename, &lpReOpenBuff, OF_EXIST) == HFILE_ERROR )
#else //WIN32
if (arg_removeimage || lstat(arg_imagename, &statbuf) == -1)
#endif //WIN32
{
char data[64];
#ifdef WIN32
DWORD bwritten;
#endif
// only the first run should remove the flash image file
arg_removeimage = 0;
tw(tr(TR_FUNC, TrDrvInit, "creating new flash image file '%s'\n",
arg_imagename));
#ifdef WIN32
image_fd = CreateFile(arg_imagename,
GENERIC_WRITE | GENERIC_READ,
0,
NULL,
OPEN_ALWAYS,
FILE_ATTRIBUTE_NORMAL,
NULL );
#else //WIN32
image_fd = open(arg_imagename , O_RDWR|O_CREAT,
(S_IRWXU & ~S_IXUSR) |
( S_IRWXG & ~S_IXGRP) | (S_IRWXO & ~S_IXOTH));
#endif //WIN32
if (image_fd == -1) {
perror("Failed to create flash image");
exit(1);
}
// write 1's to the file.
for (i = 0; i < 64; i++)
data[i] = 0xff;
#ifdef WIN32
for (i = 0; i < image_size/64; i++)
WriteFile(image_fd, data, 64, &bwritten, NULL);
CloseHandle(image_fd);
#else
for (i = 0; i < image_size/64; i++)
write(image_fd, data, 64);
close(image_fd);
#endif
image_fd = 0;
tw(tr(TR_FUNC, TrDrvInit, "flash image file created\n"));
}
// only open image file if this is the first initialization.
if (image_fd > 0) {
tw(tr(TR_FUNC, TrDrvInit, "re-opening '%s' file of size %d\n",
arg_imagename, image_size));
}
else {
tw(tr(TR_FUNC, TrDrvInit, "opening '%s' file of size %d\n",
arg_imagename, image_size));
#ifdef WIN32
image_fd = OpenFile( arg_imagename, &lpReOpenBuff, OF_READWRITE);
map_fd = CreateFileMapping (image_fd,
&lpAttributes,
PAGE_READWRITE,
0,
0,
arg_imagename);
#else //WIN32
image_fd = open(arg_imagename, O_RDWR, S_IRWXU|S_IRWXG|S_IRWXO);
#endif //WIN32
if (image_fd == -1) {
perror("Failed to open flash image");
exit(1);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -