📄 nor.c
字号:
/* --------------------------------------------------------------------------
FILE : nor.c
PURPOSE : NOR driver file
PROJECT : DaVinci CCS NOR Flashing Utility
AUTHOR : Daniel Allred
DATE : 04-Jun-2007
HISTORY
v1.00 - DJA - 04-Jun-2007
Completion (with support for DM6441 and DM6441_LV)
----------------------------------------------------------------------------- */
#include "norwriter.h"
#include "dm644x.h"
#include "stdio.h"
#include "nor.h"
// Generic commands that will point to either AMD or Intel command set
Uint32 (* Flash_Write)(PNOR_INFO, Uint32, VUint32);
Uint32 (* Flash_BufferWrite)(PNOR_INFO, Uint32, VUint8[], Uint32);
Uint32 (* Flash_Erase)(PNOR_INFO, Uint32);
Uint32 (* Flash_ID)(PNOR_INFO);
// ----------------- Bus Width Agnostic commands -------------------
static VUint8 *flash_make_addr (PNOR_INFO pNorInfo, Uint32 blkAddr, Uint32 offset)
{
return ((VUint8 *) ( blkAddr + (offset * pNorInfo->maxTotalWidth)));
}
static void flash_make_cmd (PNOR_INFO pNorInfo, Uint8 cmd, void *cmdbuf)
{
Int32 i;
Uint8 *cp = (Uint8 *) cmdbuf;
for (i = pNorInfo->busWidth; i > 0; i--)
*cp++ = (i & (pNorInfo->chipOperatingWidth - 1)) ? 0x00 : cmd;
}
static void flash_write_cmd (PNOR_INFO pNorInfo, Uint32 blkAddr, Uint32 offset, Uint8 cmd)
{
volatile FLASHPtr addr;
FLASHData cmdword;
addr.cp = flash_make_addr (pNorInfo, blkAddr, offset);
flash_make_cmd ( pNorInfo, cmd, &cmdword);
switch (pNorInfo->busWidth)
{
case BUS_8BIT:
*addr.cp = cmdword.c;
break;
case BUS_16BIT:
*addr.wp = cmdword.w;
break;
}
}
static void flash_write_data(PNOR_INFO pNorInfo, Uint32 address, Uint32 data)
{
volatile FLASHPtr pAddr;
FLASHData dataword;
dataword.l = data;
pAddr.cp = (VUint8*) address;
switch (pNorInfo->busWidth)
{
case BUS_8BIT:
*pAddr.cp = dataword.c;
break;
case BUS_16BIT:
*pAddr.wp = dataword.w;
break;
}
}
/* Used only twice */
static void flash_write_databuffer(PNOR_INFO pNorInfo, Uint32* address, void* data, Uint32 numBytes)
{
volatile FLASHPtr pAddr, pData;
VUint8* endAddress;
pData.cp = (VUint8*) data;
pAddr.cp = (VUint8*) *address;
endAddress =(VUint8*)((*address)+numBytes);
while (pAddr.cp < endAddress)
{
switch (pNorInfo->busWidth)
{
case BUS_8BIT:
*pAddr.cp++ = *pData.cp++;
break;
case BUS_16BIT:
*pAddr.wp++ = *pData.wp++;
break;
}
}
// Put last data written at start of data buffer - For AMD verification
switch (pNorInfo->busWidth)
{
case BUS_8BIT:
*address = (Uint32)(endAddress-1);
break;
case BUS_16BIT:
*address = (Uint32)(endAddress-2);
break;
}
}
/* Used only once */
static Uint32 flash_verify_databuffer(PNOR_INFO pNorInfo, Uint32 address, void* data, Uint32 numBytes)
{
volatile FLASHPtr pAddr, pData;
VUint8* endAddress;
pData.cp = (VUint8*) data;
pAddr.cp = (VUint8*) address;
endAddress =(VUint8*)(address+numBytes);
while (pAddr.cp < endAddress)
{
switch (pNorInfo->busWidth)
{
case BUS_8BIT:
if ( (*pAddr.cp++) != (*pData.cp++) )
return E_FAIL;
break;
case BUS_16BIT:
if ( (*pAddr.wp++) != (*pData.wp++) )
return E_FAIL;
break;
}
}
return E_PASS;
}
static Uint32 flash_read_data(PNOR_INFO pNorInfo, Uint32 address, Uint32 offset)
{
volatile FLASHPtr pAddr;
FLASHData dataword;
dataword.l = 0x00000000;
pAddr.cp = flash_make_addr(pNorInfo, address, offset);
switch (pNorInfo->busWidth)
{
case BUS_8BIT:
dataword.c = *pAddr.cp;
break;
case BUS_16BIT:
dataword.w = *pAddr.wp;
break;
}
return dataword.l;
}
/* Used only once */
static Bool flash_data_isequal (PNOR_INFO pNorInfo, Uint32 blkAddr, Uint32 offset, Uint32 val)
{
FLASHData testword_a, testword_b;
Bool retval = FALSE;
testword_a.l = val;
testword_b.l = flash_read_data(pNorInfo, blkAddr, offset);
switch (pNorInfo->busWidth)
{
case BUS_8BIT:
retval = (testword_a.c == testword_b.c);
break;
case BUS_16BIT:
retval = (testword_a.w == testword_b.w);
break;
}
return retval;
}
static Bool flash_issetall (PNOR_INFO pNorInfo, Uint32 blkAddr, Uint32 offset, Uint8 mask)
{
volatile FLASHPtr addr;
FLASHData maskword;
Bool retval = TRUE;
maskword.l = 0x00000000;
addr.cp = flash_make_addr (pNorInfo, blkAddr, offset);
flash_make_cmd (pNorInfo, mask, &maskword);
switch (pNorInfo->busWidth)
{
case BUS_8BIT:
retval = ((maskword.c & *addr.cp) == maskword.c);
break;
case BUS_16BIT:
retval = ((maskword.w & *addr.wp) == maskword.w);
break;
}
return retval;
}
static Bool flash_issetsome (PNOR_INFO pNorInfo, Uint32 blkAddr, Uint32 offset, Uint8 mask)
{
volatile FLASHPtr addr;
FLASHData maskword;
Bool retval = TRUE;
addr.cp = flash_make_addr (pNorInfo, blkAddr, offset);
flash_make_cmd (pNorInfo, mask, &maskword);
switch (pNorInfo->busWidth)
{
case BUS_8BIT:
retval = (maskword.c & *addr.cp);
break;
case BUS_16BIT:
retval = (maskword.w & *addr.wp);
break;
}
return retval;
}
static FLASHData flash_read_CFI_bytes (PNOR_INFO pNorInfo, Uint32 offset, Uint8 numBytes)
{
Int32 i;
FLASHData readword;
Uint8* pReadword = &readword.c;
for (i = 0; i < numBytes; i++)
{
*pReadword++ = *(flash_make_addr (pNorInfo, pNorInfo->flashBase, offset+i));
}
return readword;
}
static Bool flash_CFI_isequal (PNOR_INFO pNorInfo, Uint32 offset, Uint8 val)
{
volatile FLASHPtr addr;
FLASHData testword;
Bool retval = TRUE;
addr.cp = flash_make_addr (pNorInfo, pNorInfo->flashBase, offset);
flash_make_cmd (pNorInfo, val, &testword);
switch (pNorInfo->busWidth)
{
case BUS_8BIT:
retval = (testword.c == *addr.cp);
break;
case BUS_16BIT:
retval = (testword.w == *addr.wp);
break;
}
return retval;
}
// Query the chip to check for CFI table and data
static Uint32 QueryCFI( PNOR_INFO pNorInfo )
{
Int32 i;
Uint32 blkVal;
// Six possible NOR Flash Configurations of DM644x
// 1) Bus in x8 mode, x8 only device
// 2) Bus in x8 mode, single x8/x16 flash operating in x8 mode
// 3) Bus in x16 mode, single x8/x16 or x16-only flash operating in x16 mode
// 4) Bus in x16 mode, two x8 flash operating in parallel.
// 5) Bus in x16 mode, two x8/x16 flash, each in x8 mode, operating in parallel
// 6) Bus in x16 mode, single x16/x32 flash operating in x16 mode
for (pNorInfo->chipOperatingWidth = BUS_8BIT; pNorInfo->chipOperatingWidth <= pNorInfo->busWidth; pNorInfo->chipOperatingWidth <<= 1)
{
for (pNorInfo->maxTotalWidth = pNorInfo->busWidth; pNorInfo->maxTotalWidth <= (pNorInfo->busWidth*2); pNorInfo->maxTotalWidth <<= 1)
{
// Specify number of devices
pNorInfo->numberDevices = 0;
while ( pNorInfo->numberDevices * pNorInfo->chipOperatingWidth < pNorInfo->busWidth)
pNorInfo->numberDevices++;
// Enter the CFI Query mode
flash_write_cmd (pNorInfo, pNorInfo->flashBase, 0, CFI_EXIT_CMD);
flash_write_cmd (pNorInfo, pNorInfo->flashBase, CFI_QRY_CMD_ADDR, CFI_QRY_CMD);
// Check for Query QRY values
if ( flash_CFI_isequal ( pNorInfo, CFI_Q, 'Q') &&
flash_CFI_isequal ( pNorInfo, CFI_R, 'R') &&
flash_CFI_isequal ( pNorInfo, CFI_Y, 'Y') )
{
pNorInfo->commandSet = (CMDSET) (flash_read_CFI_bytes(pNorInfo,CFI_CMDSET,2).w);
pNorInfo->flashSize = 0x1 << flash_read_CFI_bytes(pNorInfo,CFI_DEVICESIZE,1).c * pNorInfo->numberDevices;
pNorInfo->numberRegions = flash_read_CFI_bytes(pNorInfo,CFI_NUMBLKREGIONS,1).c;
pNorInfo->bufferSize = 0x1 << flash_read_CFI_bytes(pNorInfo,CFI_WRITESIZE,2).w * pNorInfo->numberDevices;
// Get info on sector sizes in each erase region of device
for (i = 0;i < pNorInfo->numberRegions; i++)
{
blkVal = flash_read_CFI_bytes(pNorInfo,(CFI_BLKREGIONS+i*CFI_BLKREGIONSIZE),4).l;
pNorInfo->numberBlocks[i] = (blkVal&0x0000FFFF) + 1;
pNorInfo->blockSize[i] = ((blkVal&0xFFFF0000) ? ( ((blkVal>>16)&0xFFFF) * 256) : 128) * pNorInfo->numberDevices;
}
// Exit CFI mode
flash_write_cmd (pNorInfo,pNorInfo->flashBase, 0, CFI_EXIT_CMD);
return E_PASS;
}
}
}
flash_write_cmd (pNorInfo,pNorInfo->flashBase, 0, CFI_EXIT_CMD);
return E_FAIL;
}
// ---------------------------------------------------------------------------
// ------------------------ Default empty commands ---------------------------
// ---------------------------------------------------------------------------
static Uint32 Unsupported_Write( PNOR_INFO pNorInfo, Uint32 address, VUint32 data)
{
return E_FAIL;
}
static Uint32 Unsupported_BufferWrite(PNOR_INFO pNorInfo, Uint32 address, VUint8 data[], Uint32 length )
{
return E_FAIL;
}
static Uint32 Unsupported_Erase(PNOR_INFO pNorInfo, Uint32 address)
{
return E_FAIL;
}
static Uint32 Unsupported_ID(PNOR_INFO pNorInfo)
{
return E_FAIL;
}
// -------------------------------------------------------------------------
// Manufacturer Specific Commands
// -------------------------------------------------------------------------
// ---------------------------------------------------------------------------
// ------------------- Begin of Intel specific commands ----------------------
// ---------------------------------------------------------------------------
//ID flash
Uint32 Intel_ID( PNOR_INFO pNorInfo )
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -