?? flash_amd.c
字號:
/* * File: flash_amd.c * * An implementation of a flash utility specific to the AMD chipset * (AM29L640D). This implementation, while vendor dependent, exposes * the vendor independent flash.h interface allowing it to plug generically * into the bootloader. Please see flash.h for more info. * * See Also * flash.h * * This program is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License as published by the * Free Software Foundation; either version 2 of the License, or (at your * option) any later version. * * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN * NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * * You should have received a copy of the GNU General Public License along * with this program; if not, write to the Free Software Foundation, Inc., * 675 Mass Ave, Cambridge, MA 02139, USA. * */#include "flash.h"#include "types.h"#include "util.h"#include "io.h"#include "memconfig.h"#define NUM_CHIP_SECT 128#define NUM_CHIPS 1#define TOTAL_SECT (NUM_CHIPS*NUM_CHIP_SECT)// 128 sectors per AMD Am29LV640D (8 MBytes total bytes)static const int sect_sizes[NUM_CHIP_SECT] = { 0x10000, 0x10000, 0x10000, 0x10000, 0x10000, 0x10000, 0x10000, 0x10000, 0x10000, 0x10000, 0x10000, 0x10000, 0x10000, 0x10000, 0x10000, 0x10000, 0x10000, 0x10000, 0x10000, 0x10000, 0x10000, 0x10000, 0x10000, 0x10000, 0x10000, 0x10000, 0x10000, 0x10000, 0x10000, 0x10000, 0x10000, 0x10000, 0x10000, 0x10000, 0x10000, 0x10000, 0x10000, 0x10000, 0x10000, 0x10000, 0x10000, 0x10000, 0x10000, 0x10000, 0x10000, 0x10000, 0x10000, 0x10000, 0x10000, 0x10000, 0x10000, 0x10000, 0x10000, 0x10000, 0x10000, 0x10000, 0x10000, 0x10000, 0x10000, 0x10000, 0x10000, 0x10000, 0x10000, 0x10000, 0x10000, 0x10000, 0x10000, 0x10000, 0x10000, 0x10000, 0x10000, 0x10000, 0x10000, 0x10000, 0x10000, 0x10000, 0x10000, 0x10000, 0x10000, 0x10000, 0x10000, 0x10000, 0x10000, 0x10000, 0x10000, 0x10000, 0x10000, 0x10000, 0x10000, 0x10000, 0x10000, 0x10000, 0x10000, 0x10000, 0x10000, 0x10000, 0x10000, 0x10000, 0x10000, 0x10000, 0x10000, 0x10000, 0x10000, 0x10000, 0x10000, 0x10000, 0x10000, 0x10000, 0x10000, 0x10000, 0x10000, 0x10000, 0x10000, 0x10000, 0x10000, 0x10000, 0x10000, 0x10000, 0x10000, 0x10000, 0x10000, 0x10000, 0x10000, 0x10000, 0x10000, 0x10000, 0x10000, 0x10000};typedef struct { int start_addr; int end_addr;} sect_info_t;sect_info_t sect_info[TOTAL_SECT];void WagTheDog(void){ int i; for (i = 0; i < 100; i++);}void WaitForFlashEraseToStart( void ){ enum constants { _DQ3 = 0x08 }; unsigned short * addr = (unsigned short *)BSPCONF_FLASH_BASE; while (1) { if (addr[0] & _DQ3) { break; } // Operation could take a while WagTheDog(); }}unsigned char IsFlashOperationSuccessful( unsigned short * flashAddress ){ enum constants { _DQ6 = 0x40, _DQ5 = 0x20 }; volatile unsigned int toggleCheck; volatile unsigned int previousToggleCheck; previousToggleCheck = flashAddress[0]; while (1) { toggleCheck = flashAddress[0]; // Is the flash operation still going? (DQ6 toggling) if (!((previousToggleCheck & _DQ6) ^ (toggleCheck & _DQ6))) { // If DQ6 isn't toggling the flash operation is complete return 1; } else { // If DQ6 is toggling the operation is not done yet // Has the flash operation timed out? (DQ5 == 1) if (toggleCheck & _DQ5) { // If DQ5 is set, the operation has timed out // Make one more check to see if the operation is complete previousToggleCheck = flashAddress[0]; toggleCheck = flashAddress[0]; if (!((previousToggleCheck & _DQ6) ^ (toggleCheck & _DQ6))) { // If DQ6 isn't toggling the flash operation is complete return 1; } else { // If DQ6 is still toggling, an error has occured return 0; } } } previousToggleCheck = toggleCheck; // Operation could take a while WagTheDog( ); } // Can't get here return 0;}/****************************** Routine: Description: Returns the starting address for flash block containing block_address******************************/unsigned short *get_start_address(unsigned short *block_address){ int i; for (i=0; i<TOTAL_SECT; i++) { if (((unsigned short *)sect_info[i].start_addr >= block_address) && ((unsigned short *)sect_info[i].start_addr <= block_address)) { return((unsigned short *)sect_info[i].start_addr); } } return(0);}/****************************** Routine: Description: Returns the ending address for flash block containing block_address******************************/unsigned short *get_end_address(unsigned short *block_address){ int i; for (i=0; i<TOTAL_SECT; i++) { if (((unsigned short *)sect_info[i].start_addr >= block_address) && ((unsigned short *)sect_info[i].start_addr <= block_address)) { return((unsigned short *)sect_info[i].end_addr); } } return(0);}/****************************** Routine: Description: returns true if the block has been erased******************************/static int verify_block_erased(unsigned short *block_addr, int print_err){ volatile unsigned short *addr; int err=false; volatile unsigned short contents; unsigned short *start_addr; unsigned short *end_addr; start_addr=get_start_address(block_addr); end_addr=get_end_address(block_addr); for (addr=start_addr; addr<end_addr; addr++) { contents = *addr; if (contents != 0xFFFF) { err=true; break; } } if (err && print_err) { util_printf("Error: address range %X - %X not erased: %X=%x\n", start_addr, end_addr, addr, contents); } return(!err);}#ifdef BSPCONF_BTLDR_MEMMAP_DEBUG/****************************** Routine: Description:******************************/static void enable_read_mode(){ volatile unsigned short *addr; addr = (unsigned short *) BSPCONF_FLASH_BASE; *addr = 0xF0; io_delay(1);}/****************************** Routine: Description: Returns true if flash part supports CFI******************************/static int cfi_supported(void){ volatile unsigned short *addr; int i,j; int read_array[3]; addr = (unsigned short *) BSPCONF_FLASH_BASE; enable_read_mode(); addr[0x55] = 0x98; /* Enter read array mode */ j=0; for ( i=0x10; i <= 0x12; i++) { read_array[j++] = addr[i]; } enable_read_mode(); return( ( read_array[0] == 'Q' ) && ( read_array[1] == 'R' ) && ( read_array[2] == 'Y' ) );}/****************************** Routine: Description: returns true if device code read, false if chip doesn't support CFI******************************/int read_device_codes(unsigned long *Code){ unsigned short regval; volatile unsigned short *addr; addr = (unsigned short *) BSPCONF_FLASH_BASE; if ( ! cfi_supported() ) { *Code=0; return(0); } enable_read_mode(); addr[0x555] = 0xAA; addr[0x2AA] = 0x55; addr[0x555] = 0x90; regval = addr[0]; // mfg code. *Code = (regval<<16); regval = addr[1]; // device code. *Code = *Code | regval; enable_read_mode(); return(1);}/****************************** Routine: Description: returns true if chip supports CFI. array elements from CFI_START_ADDR to CFI_END_ADDR filled with CFI read array data. Returns false if chip doesn't support CFI.******************************/int read_cfi_array(unsigned short array[], int array_size){ volatile unsigned short *addr;
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -