i28f128.c
资源名称:ixp425BSP.rar [点击查看]
上传用户:luoyougen
上传日期:2008-05-12
资源大小:23136k
文件大小:6k
源码类别:
VxWorks
开发平台:
C/C++
- #include <stdio.h>
- #include "tffs/flflash.h"
- #include "tffs/backgrnd.h"
- /* support 28f320 28f640 28f128*/
- #undef CFI_DEBUG
- #ifdef CFI_DEBUG
- #define DEBUG_PRINT printf
- #else
- #undef DEBUG_PRINT
- #endif
- /* JEDEC ids for this MTD */
- #define I28F128_FLASH 0x18 /* device code 28F128 */
- #define I28F640_FLASH 0x17 /* device code 28F640 */
- #define I28F320_FLASH 0x16 /* device code 28F320 */
- #define SETUP_ERASE 0x2020
- #define SETUP_WRITE 0x4040
- #define CLEAR_STATUS 0x5050
- #define READ_STATUS 0x7070
- #define READ_ID 0x9090
- #define SUSPEND_ERASE 0xb0b0
- #define CONFIRM_ERASE 0xd0d0
- #define RESUME_ERASE 0xd0d0
- #define READ_ARRAY 0xffff
- #define STAT_WSMS 0x0080
- /* Defintion for Bit macro */
- #ifndef BIT
- #define BIT(bitNumber) (1 << (bitNumber))
- #endif /* BIT */
- static FLStatus i28f128Write(FLFlash vol,
- CardAddress address,
- const void FAR1 *buffer,
- int length,
- FLBoolean overwrite)
- {
- unsigned long writeTimeout;
- int i;
- FlashWPTR flashPtr;
- FlashWPTR ptr;
- STATUS retVal;
- #ifdef DEBUG_PRINT
- DEBUG_PRINT("Debug: Entering i28f128Write function.n");
- DEBUG_PRINT("Debug: address=0x%x, length=%d, overwrite=%dn", address, length, overwrite);
- #endif
- if ( flWriteProtected(vol.socket) )
- return flWriteProtect;
- if ((length & 1) || (address & 1)) /* Only write words on word-boundary */
- return flBadParameter;
- flashPtr = (FlashWPTR) flMap(vol.socket, address);
- /* Set timeout to 5 seconds from now */
- writeTimeout = flMsecCounter + 5000;
- retVal = OK;
- ptr = flashPtr;
- for (i = 0; i < length ; i += 2, ptr++)
- {
- unsigned short value;
- value = *((FlashWPTR)(buffer + i));
- *ptr = SETUP_WRITE;
- *ptr = value;
- do
- {
- /*ptr = READ_STATUS;*/
- if (flMsecCounter > writeTimeout)
- {
- retVal = ERROR;
- *ptr = CLEAR_STATUS;
- break;
- }
- }while((*ptr & STAT_WSMS) != STAT_WSMS);
- if (retVal == ERROR)
- {
- *ptr = READ_ARRAY;
- break;
- }
- if (*ptr & (BIT(5) | BIT(4) | BIT(3) | BIT(1)))
- {
- retVal = ERROR;
- *ptr = CLEAR_STATUS;
- }
- if (retVal == ERROR)
- {
- *ptr = READ_ARRAY;
- break;
- }
- *ptr = READ_ARRAY;
- }
- if (retVal == ERROR)
- {
- #ifdef DEBUG_PRINT
- DEBUG_PRINT("Debug: In i28f128Write, write failedn");
- #endif
- return flWriteFault;
- }
- if (tffscmp((void FAR0 *)flashPtr, buffer, length))
- {
- #ifdef DEBUG_PRINT
- DEBUG_PRINT("Debug: In i28f128Write, write data errorn");
- {
- int i;
- /*dump data*/
- for (i = 0; i < length/2; i++)
- {
- unsigned short data;
- data = *((unsigned short*)(buffer + i * 2));
- DEBUG_PRINT("%x==>%xn", data, *(flashPtr+i));
- }
- }
- #endif
- return flWriteFault;
- }
- #ifdef DEBUG_PRINT
- DEBUG_PRINT("Debug: In i28f128Write, erase OKn");
- #endif
- return flOK;
- }
- static FLStatus i28f128Erase (FLFlash vol,
- int firstBlock,
- int numOfBlocks)
- {
- unsigned long writeTimeout;
- FlashWPTR ptr;
- int i;
- STATUS retVal;
- #ifdef DEBUG_PRINT
- DEBUG_PRINT("Debug: Entering i28f128Erase function.n");
- DEBUG_PRINT("Debug: firstblock=%d, numOfBlocks=%d.n", firstBlock, numOfBlocks);
- #endif
- if ( flWriteProtected(vol.socket) )
- return flWriteProtect;
- /* Set timeout to 5 seconds from now */
- writeTimeout = flMsecCounter + 5000;
- retVal = OK;
- for (i = 0; i < numOfBlocks; i++)
- {
- ptr = (FlashWPTR) flMap(vol.socket, (firstBlock + i) * vol.erasableBlockSize);
- #ifdef DEBUG_PRINT
- DEBUG_PRINT("Debug: Erase ptr=0x%x.n", ptr);
- #endif
- *ptr = SETUP_ERASE;
- *ptr = CONFIRM_ERASE;
- do
- {
- if (flMsecCounter > writeTimeout)
- {
- retVal = ERROR;
- *ptr = CLEAR_STATUS;
- break;
- }
- }while((*ptr & STAT_WSMS) != STAT_WSMS);
- if (retVal == ERROR)
- {
- *ptr = READ_ARRAY;
- break;
- }
- if (*ptr & (BIT(5) | BIT(4) | BIT(3) | BIT(1)))
- {
- retVal = ERROR;
- *ptr = CLEAR_STATUS;
- }
- if (retVal == ERROR)
- {
- *ptr = READ_ARRAY;
- break;
- }
- *ptr = READ_ARRAY;
- }
- if (retVal == ERROR)
- {
- #ifdef DEBUG_PRINT
- DEBUG_PRINT("Debug: In i28f128Erase, erase failedn");
- #endif
- return flWriteFault;
- }
- #ifdef DEBUG_PRINT
- DEBUG_PRINT("Debug: In i28f128Erase, erase OKn");
- #endif
- return flOK;
- }
- FLStatus i28f128Identify(FLFlash vol)
- {
- FlashWPTR flashPtr;
- #ifdef DEBUG_PRINT
- DEBUG_PRINT("Debug: Entering i28f128Identify.n");
- #endif
- flSetWindowBusWidth(vol.socket,16);/* use 16-bits */
- flSetWindowSpeed(vol.socket,120); /* 120 nsec. */
- flSetWindowSize(vol.socket,2); /* 8 KBytes */
- flashPtr = (FlashWPTR) flMap(vol.socket,0);
- flashPtr[0] = READ_ARRAY;
- flashPtr[1] = READ_ID;
- switch (flashPtr[1])
- {
- case I28F640_FLASH:
- vol.type = I28F640_FLASH;
- vol.chipSize = 0x800000;
- #ifdef DEBUG_PRINT
- DEBUG_PRINT("Debug: Detect Intel 28f640n");
- #endif
- break;
- case I28F320_FLASH:
- vol.type = I28F320_FLASH;
- vol.chipSize = 0x400000;
- #ifdef DEBUG_PRINT
- DEBUG_PRINT("Debug: Detect Intel 28f320n");
- #endif
- break;
- case I28F128_FLASH:
- vol.type = I28F128_FLASH;
- vol.chipSize = 0x1000000;
- #ifdef DEBUG_PRINT
- DEBUG_PRINT("Debug: Detect Intel 28f128n");
- #endif
- break;
- default:
- #ifdef DEBUG_PRINT
- DEBUG_PRINT("Debug: Can't detect flash typen");
- #endif
- vol.type = NOT_FLASH;
- #ifdef DEBUG_PRINT
- DEBUG_PRINT("Debug: failed to identify 16-bit Intel media.n");
- #endif
- return flUnknownMedia; /* not ours */
- }
- flashPtr[0] = READ_ARRAY;
- vol.interleaving = 1;
- vol.noOfChips = 1;
- vol.erasableBlockSize = 128*1024;
- /* Register our flash handlers */
- vol.write = i28f128Write;
- vol.erase = i28f128Erase;
- #ifdef DEBUG_PRINT
- DEBUG_PRINT("Debug: identified 16-bit Intel media.n");
- #endif
- return flOK;
- }