📄 flashutil.c
字号:
/* flashUtil.c - flash memory utility *//* Copyright 1997-1998 Wind River Systems, Inc.; Copyright 1999 Intel Corp. *//*modification history--------------------01b,12sep01,scm add flashModeSwap conditional...01a,08jul99,jdg Created from version 01i of flashMem.c*//*DESCRIPTIONThis provides a series of routines for writing flash parts.It is aimed primarily at the Intel 28F800 parts, but it mightalso work on other Intel 28F and 29F parts.*/#include "flashUtil.h"#define FLASH28CMD_READ_MEM 0x00000000#define FLASH28CMD_READ_ID 0x90909090#define FLASH28CMD_ERASE_SETUP 0x20202020#define FLASH28CMD_ERASE 0x20202020#define FLASH28CMD_ERASE_CONFIRM 0xd0d0d0d0#define FLASH28CMD_ERASE_VERIFY 0xa0a0a0a0#define FLASH28CMD_PROG_SETUP 0x40404040#define FLASH28CMD_PROG_VERIFY 0xc0c0c0c0#define FLASH28CMD_RESET 0xffffffff#define FLASH28CMD_CFGSETUP 0x60606060#define FLASH28CMD_CFGUNLOCK 0xd0d0d0d0#define FTYPE_28F800B 0x93 /* devide code 28F800-B */#ifdef INCLUDE_EXTRA#define FTYPE_28F800T 0x92 /* device code 28F800-T */#define FTYPE_28F160T 0x90 /* device code 28F160-T */#define FTYPE_28F160B 0x91 /* devide code 28F160-B */#define FTYPE_28F320T 0x96 /* device code 28F320-T */#define FTYPE_28F320C3T 0xc4 /* device code 28F320C3-T */#endif#define FTYPE_28F320B 0x97 /* device code 28F320-B */#define FTYPE_28F320C3B 0xc5 /* device code 28F320C3-B */#define CAST_FLASH (FLASH_TYPE*)#ifndef SYS_FLASH_WRITE_ENABLE_RTN#define SYS_FLASH_WRITE_ENABLE_RTN()#endif#ifndef SYS_FLASH_WRITE_DISABLE_RTN#define SYS_FLASH_WRITE_DISABLE_RTN()#endif#ifndef FLASH_MEM_TYPE#define FLASH_MEM_TYPE FTYPE_NONE#endif#define BIT(i) (1 << (i))UINT8 flashType = FLASH_MEM_TYPE;UINT32 flashBase = FLASH_BASE;/* * flash32BitMode is a boolean indicating whether we should program the * flash in 32-bit mode or 16-bit mode. It is normally set to whatever * mode the HW is in. If it is set to the mode the HW is not in, i.e. the * HW is in 16-bit mode and we want to program in 32-bit mode, or vice versa, * then flash32BitMode should be set to the new mode and flashModeSwap should * be set to 1. That is: flash32BitMode should be set to the mode we want * to program in, and the actual mode is (flash32BitMode ^ flashModeSwap). */extern int flash32BitMode;int flashModeSwap = 0;#define WRITE_ADDR(offset) (flash32BitMode ? (offset) : ((offset)*2))#define READ_ADDR(offset) \ (flashModeSwap ? (flash32BitMode ? ((offset)/2) : ((offset)*2)) : (offset))/******************************************************************************** flashTypeGet - determine the device type of on-board flash memory ** This routine uses the `autoselect' command to determine the device type of* on-board flash memory for flash 28F devices.** RETURNS: An integer indicating the device type of on-board flash memory.*/UINT8 flashTypeGet (UINT32 flash_offset) { volatile FLASH_TYPE* pFR = CAST_FLASH (flashBase + READ_ADDR(flash_offset)); volatile FLASH_TYPE* pFW = CAST_FLASH (flashBase + WRITE_ADDR(flash_offset)); UINT8 retVal; SYS_FLASH_WRITE_ENABLE_RTN (); /* enable writes */#ifdef ENABLE_29 *(CAST_FLASH FLASH29_REG_FIRST_CYCLE) = FLASH29_CMD_FIRST; *(CAST_FLASH FLASH29_REG_SECOND_CYCLE) = FLASH29_CMD_SECOND; *(CAST_FLASH FLASH29_REG_FIRST_CYCLE) = FLASH29_CMD_AUTOSELECT;#else *(pFW) = FLASH28CMD_READ_ID;#endif#if defined(IXM1200) /* KLUDGE @@ We need to do this right. JDG */ pFR = CAST_FLASH(flashBase); if (flash32BitMode ^ flashModeSwap) { /* 32-bit mode */ retVal = (UINT8) (pFR[1]); } else { /* 16-bit mode */ retVal = (UINT8) (pFR[0] >> 16); }#else retVal = (UINT8) ((pFR[1]) >> 16);#endif#ifdef ENABLE_29 *(CAST_FLASH FLASH29_REG_FIRST_CYCLE) = FLASH29_CMD_FIRST; *(CAST_FLASH FLASH29_REG_SECOND_CYCLE) = FLASH29_CMD_SECOND; *(CAST_FLASH FLASH29_REG_FIRST_CYCLE) = FLASH29_CMD_READ_RESET;#else *(pFW) = FLASH28CMD_RESET;#endif SYS_FLASH_WRITE_DISABLE_RTN (); /* disable writes */ return (retVal); }/******************************************************************************** flashErase - erase the contents of flash memory** This routine clears the contents of flash memory.** Flash 28F\f2xxx\f1 devices are erased by writing a flash erase command to* the device and verifying that each flash location is set to a high value* (0xFF).* * Flash 29F\f2xxx\f1 devices are erased by writing the six-byte erase code* into specific address locations, which sets all byte locations to a high* value (0xFF).** RETURNS: OK, or ERROR if the contents of flash memory cannot be erased.*/STATUS flashErase ( UINT32 flash_offset ) { volatile FLASH_TYPE* pFR = CAST_FLASH (flashBase + READ_ADDR(flash_offset)); volatile FLASH_TYPE* pFW = CAST_FLASH (flashBase + WRITE_ADDR(flash_offset)); STATUS retVal = OK; int ix, status; if (flashType == FTYPE_NONE) flashType = flashTypeGet(flash_offset); switch (flashType) {#ifdef INCLUDE_EXTRA case (FTYPE_28F320C3T):#endif case (FTYPE_28F320C3B): *pFW = FLASH28CMD_CFGSETUP; *pFW = FLASH28CMD_CFGUNLOCK; /* Note: no break at end of block; fall through to code below */ case (FTYPE_28F800B):#ifdef INCLUDE_EXTRA case (FTYPE_28F800T): case (FTYPE_28F160B): case (FTYPE_28F160T): case (FTYPE_28F320T):#endif case (FTYPE_28F320B): { SYS_FLASH_WRITE_ENABLE_RTN (); /* raise Vpp */ *pFW = FLASH28CMD_ERASE_SETUP; /* setup */ *pFW = FLASH28CMD_ERASE_CONFIRM; /* erase */ /* wait for completion */ for (ix = (FLASH_WIDTH/2)-1; ix >= 0; ix--) { for (;;) { status = (*pFR) >> (ix * 16); if (status & BIT(7)) break; } if (status & (BIT(5) | BIT(4) | BIT(3) | BIT(1))) { retVal = ERROR; break; } } *pFW = FLASH28CMD_RESET; /* read array */ SYS_FLASH_WRITE_DISABLE_RTN (); /* disable enable */ break; } default: retVal = ERROR; } return (retVal); }/******************************************************************************** flashWrite - write data to flash memory** This routine copies specified data of a specified length, <size>, into a * specified offset, <offset>, in the flash memory. Data is passed as a string,* <pFB>, if not NULL. If NULL, data is taken as a repeated sequence of* <value>.* * RETURNS: OK, or ERROR if the write operation fails.* * SEE ALSO: sysFlashSet()*/STATUS flashWrite ( FLASH_TYPE * pFB, /* string to be copied; use <value> if NULL */ int size, /* size to program in bytes */ int flash_offset, /* byte offset into flash memory */ FLASH_TYPE value /* value to program */ ) { volatile FLASH_TYPE * pFW; /* flash address */ volatile FLASH_TYPE * pFR; /* flash address */ STATUS retVal = OK; int i, ix, status, pass; FLASH_TYPE valtmp; if (flashType == FTYPE_NONE) flashType = flashTypeGet(flash_offset); pFW = CAST_FLASH(flashBase + WRITE_ADDR(flash_offset)); switch (flashType) {#ifdef INCLUDE_EXTRA case (FTYPE_28F320C3T):#endif case (FTYPE_28F320C3B): *pFW = FLASH28CMD_CFGSETUP; *pFW = FLASH28CMD_CFGUNLOCK; /* Note: no break at end of block; fall through to code below */ case (FTYPE_28F800B):#ifdef INCLUDE_EXTRA case (FTYPE_28F800T): case (FTYPE_28F160B): case (FTYPE_28F160T): case (FTYPE_28F320T):#endif case (FTYPE_28F320B): { SYS_FLASH_WRITE_ENABLE_RTN (); /* raise Vpp */ pFR = CAST_FLASH(flashBase + READ_ADDR(flash_offset));
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -