⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 flash_amd.c

📁 MIPS下的boottloader yamon 的源代码
💻 C
📖 第 1 页 / 共 5 页
字号:
/************************************************************************
*
*      FLASH_AMD.C
*
*      The 'FLASH_AMD' module implements the FLASH device driver
*      interface to be used via 'IO' device driver services:
*
*        1) init  device:  configure and initialize FLASH driver
*        2) open  device:  not used
*        3) close device:  not used
*        4) read  device:  not used
*        5) write device:  write FLASH device
*        6) ctrl  device:  a) ERASE_SYSTEMFLASH
*                          b) ERASE_FILEFLASH
*
*
*
* ######################################################################
*
* Copyright (c) 1999-2000 MIPS Technologies, Inc. All rights reserved. 
* 
* Unpublished rights reserved under the Copyright Laws of the United States of 
* America. 
* 
* This document contains information that is proprietary to MIPS Technologies, 
* Inc. ("MIPS Technologies"). Any copying, modifying or use of this information 
* (in whole or in part) which is not expressly permitted in writing by MIPS 
* Technologies or a contractually-authorized third party is strictly 
* prohibited. At a minimum, this information is protected under unfair 
* competition laws and the expression of the information contained herein is 
* protected under federal copyright laws. Violations thereof may result in 
* criminal penalties and fines. 
* MIPS Technologies or any contractually-authorized third party reserves the 
* right to change the information contained in this document to improve 
* function, design or otherwise. MIPS Technologies does not assume any 
* liability arising out of the application or use of this information. Any 
* license under patent rights or any other intellectual property rights owned 
* by MIPS Technologies or third parties shall be conveyed by MIPS Technologies 
* or any contractually-authorized third party in a separate license agreement 
* between the parties. 
* The information contained in this document constitutes one or more of the 
* following: commercial computer software, commercial computer software 
* documentation or other commercial items. If the user of this information, or 
* any related documentation of any kind, including related technical data or 
* manuals, is an agency, department, or other entity of the United States 
* government ("Government"), the use, duplication, reproduction, release, 
* modification, disclosure, or transfer of this information, or any related 
* documentation of any kind, is restricted in accordance with Federal 
* Acquisition Regulation 12.212 for civilian agencies and Defense Federal 
* Acquisition Regulation Supplement 227.7202 for military agencies. The use of 
* this information by the Government is further restricted in accordance with 
* the terms of the license agreement(s) and/or applicable contract terms and 
* conditions covering this information from MIPS Technologies or any 
* contractually-authorized third party. 
*
************************************************************************/

/************************************************************************
*      Include files
************************************************************************/

#include <sysdefs.h>
#include <syserror.h>
#include <sysdev.h>
#include <mips.h>
#include <io_api.h>
#include <syscon_api.h>
#include <flash_api.h>
#include <flash_AMD_api.h>
#include <sys_api.h>
#include <stdio.h>
#include <pb1000.h>
#include <string.h>

/************************************************************************
*      Definitions
************************************************************************/

/* FLASH-device, relative register address inside a block */
#define FLASH_BLOCKSTATUS_OFS				0x0008

/* field: LOCK */
#define FLASH_BLOCKSTATUS_LOCK_MSK          0x00010001

/* flash memory ID's */
#define INTEL_MNFCR_CODE					0x0089
#define AMD_DEV1_CODE						0x0014
#define AMD_DEV2_CODE						0x0015

#define FLASH_QUERYQCHAR_OFS				0x0040
#define FLASH_QUERYRCHAR_OFS				0x0044
#define FLASH_QUERYYCHAR_OFS				0x0048

#define FLASH_QUERYQCHAR                    0x00510051
#define FLASH_QUERYRCHAR                    0x00520052
#define FLASH_QUERYYCHAR                    0x00590059

/* status */
#define FLASH_READY							0x0000
#define FLASH_NOT_DETECTED					0x0001
#define FLASH_BUSY							0x0002
#define FLASH_ERROR							0x0003
#define FLASH_TIMEOUT						0x0004
#define FLASH_INVALID_SECTOR				0x0005
#define FLASH_SECTOR_LOCKED					0x0006

/* Commands and masks, etc... */
#define FLASH_STATUS_READY					0x00800080
#define FLASH_STATUS_MASK					0x00FF00FF
#define FLASH_STATUS_OK						0x00800080
#define FLASH_STATUS_ERASE					0x00600060
#define FLASH_STATUS_LOCK					0x00020002
#define FLASH_STATUS_LOW_VOLTAGE			0x00080008

#define FLASH_READ_COMMAND					0x00FF00FF
#define FLASH_ERASE_COMMAND					0x00200020
#define FLASH_CONFIRM_COMMAND		        0x00D000D0
#define FLASH_CLEAR_STATUS_COMMAND	        0x00500050
#define FLASH_WRITE_WORD_COMMAND	        0x00100010
#define FLASH_WRITE_BUFFER_COMMAND	        0x00E800E8
#define FLASH_STATUS_COMMAND		        0x00700070
#define FLASH_QUERY_COMMAND					0x00980098
#define FLASH_READ_ID_CODES_COMMAND			0x00900090
#define FLASH_CLEAR_LOCK_COMMAND			0x00600060
#define FLASH_LOCK_SECTOR					0x00010001

/* Timeout values */
#define FLASH_TMOUT_100MS					100
#define FLASH_TMOUT_250MS					250
#define FLASH_TMOUT_500MS					500
#define FLASH_TMOUT_1SEC					1000
#define FLASH_TMOUT_2SEC					2000
#define FLASH_TMOUT_2_5SEC					2500

/* Retry counts */
#define FLASH_MAX_LOOPS				0xFFFFFFFF
#define FLASH_RETRY_5                           5
#define FLASH_RETRY_10                          10
#define FLASH_RETRY_20                          20
#define FLASH_RETRY_50                          50

#if FLASH_16BIT
#define POLLING_MASK		0x00000080
#define TIMING_LIMIT_MASK	0x00000020
#else
#define POLLING_MASK		0x00800080
#define TIMING_LIMIT_MASK	0x00200020
#endif

#define ERASE_DATA			0xFFFFFFFF

/* Identification tags for memory devices */
typedef enum flash_device_id
{
    FLASH_SYSTEMFLASH_DEVICE = 0,
    FLASH_MONITORFLASH_DEVICE,
    FLASH_FILEFLASH_DEVICE,
    FLASH_BOOT_DEVICE,
    FLASH_UNKNOWN_DEVICE
} t_flash_device_id ;

/* union, to access bytes inside a word, independant of endianness */
typedef union flash_access
{
    volatile UINT32 d32 ;
    volatile UINT8  d8[4] ;
} t_flash_access ;

/************************************************************************
*  Macro Definitions
*************************************************************************/

#define WRITE_ENABLE   SYSCON_write( SYSCON_BOARD_SYSTEMFLASH_WRITE_ENABLE_ID, \
NULL, sizeof( UINT32 ) );

#define WRITE_DISABLE  SYSCON_write( SYSCON_BOARD_SYSTEMFLASH_WRITE_DISABLE_ID, \
NULL, sizeof( UINT32 ) );

/************************************************************************
*      Public variables
************************************************************************/



/************************************************************************
*      Static variables
************************************************************************/

static char* flash_error_string[] = 
{
    /* ERROR_FLASH_PROGRAM_ERROR      */ "Flash device failure",
    /* ERROR_FLASH_INVALID_ADDRESS    */ "Invalid address",
    /* ERROR_FLASH_INVALID_COMMAND    */ "Internal ERROR: Invalid control command",
    /* ERROR_FLASH_TIME_OUT           */ "Internal ERROR: Flash device timed out during operation",
    /* ERROR_FLASH_VERIFY_ERROR       */ "Data verify error",
    /* ERROR_FLASH_LOCKED             */ "Some sectors are locked",
    /* ERROR_FLASH_ERASE_ERROR        */ "Sector has erase error",
    /* ERROR_FLASH_LOW_VOLTAGE        */ "Low programming voltage detected",
    /* ERROR_FLASH_WRITE_PROTECTED    */ "Flash is write protected",
    /* ERROR_FLASH_FILE_FLASH_PROT    */ "Environment FLASH is write protected",
    /* ERROR_FLASH_FILE_FLASH_LOCK    */ "Environment FLASH is lock-bit protected",
    /* ERROR_FLASH_MONITOR_FLASH_LOCK */ "Some MONITOR FLASH sector(s) locked",
    /* ERROR_FLASH_QRY_NOT_FOUND      */ "CFI Query-ID string of FLASH not found",
    /* ERROR_FLASH_BOOT_WRITE_PROTECTED */ "Write access to this area not allowed"
} ;

static char* flash_error_hint_string[] = 
{
    /* ERROR_FLASH_PROGRAM_ERROR      */ NULL,
    /* ERROR_FLASH_INVALID_ADDRESS    */ NULL,
    /* ERROR_FLASH_INVALID_COMMAND    */ NULL,
    /* ERROR_FLASH_TIME_OUT           */ NULL,
    /* ERROR_FLASH_VERIFY_ERROR       */ "Check flash has been erased before programming",
    /* ERROR_FLASH_LOCKED             */ "Unlock sector(s) before programming",
    /* ERROR_FLASH_ERASE_ERROR        */ NULL,
    /* ERROR_FLASH_LOW_VOLTAGE        */ NULL,
    /* ERROR_FLASH_WRITE_PROTECTED    */ "Disable write protection: Switch S1-3",
    /* ERROR_FLASH_FILE_FLASH_PROT    */ "Check programming addresses",
    /* ERROR_FLASH_FILE_FLASH_LOCK    */ "Disable 'clear lock-bit' protection: (MFWR-jumper) must be fitted",
    /* ERROR_FLASH_MONITOR_FLASH_LOCK */ NULL,
    /* ERROR_FLASH_QRY_NOT_FOUND      */ NULL,
    /* ERROR_FLASH_BOOT_WRITE_PROTECTED */ NULL
} ;

static UINT32 flash_last_error ;
static char   flash_diag_msg[160] ;
static char   msg[160] ;


/* these variables are initialized at 'init' with the physical
address boundaries of:

 a) system FLASH
 b) monitor FLASH
 c) file FLASH.
 
  Following rule is to apply:
  'start' <= physical-device-address-space < 'end'
*/

static UINT32  systemflash_phys_start ;
static UINT32  systemflash_phys_end ;
static UINT32  systemflash_block_size ;
static UINT32  systemflash_bank_count ;
static UINT32  systemflash_block_count ;

static UINT32  monitorflash_phys_start ;
static UINT32  monitorflash_phys_end ;
static UINT32  monitorflash_block_size ;
static UINT32  monitorflash_bank_count ;
static UINT32  monitorflash_block_count ;

static UINT32  fileflash_phys_start ;
static UINT32  fileflash_phys_end ;
static UINT32  fileflash_block_size ;

static UINT32  boot_phys_start ;
static UINT32  boot_phys_end ;

/************************************************************************
*      function prototypes
************************************************************************/


static
INT32 FLASH_AMD_init(
                     UINT32 major,          /* IN: major device number             */
                     UINT32 minor,          /* IN: minor device number             */
                     void   *p_param ) ;    /* INOUT: device parameter block       */
                     
static
INT32 FLASH_AMD_write(
                      UINT32 major,          /* IN: major device number             */
                      UINT32 minor,          /* IN: minor device number             */
                      t_FLASH_write_descriptor *p_param ) ; /* IN: write data       */
                                           
static
INT32 FLASH_AMD_ctrl(
                     UINT32 major,          /* IN: major device number             */
                     UINT32 minor,          /* IN: minor device number             */
                     t_FLASH_ctrl_descriptor *p_param );  /* IN: write data       */
                     
static
bool FLASH_AMD_is_system_flash_write_protected( void );

static
bool FLASH_AMD_is_file_flash_write_protected( void );

static
bool FLASH_AMD_is_monitor_flash_write_protected( void );

static
t_flash_device_id FLASH_AMD_devicetype( UINT32 physadr ) ;

static
INT32 FLASH_AMD_program_systemram( t_FLASH_write_descriptor *p_param ) ;

static
INT32 FLASH_AMD_program_flash( t_FLASH_write_descriptor *p_param ) ;

static
INT32 FLASH_AMD_set_systemflash_read( void ) ;

static
INT32 FLASH_AMD_set_fileflash_read( void ) ;

static
INT32 FLASH_AMD_set_monitorflash_read( void ) ;

static
INT32 FLASH_AMD_erase_systemflash( void ) ;

static
INT32 FLASH_AMD_erase_fileflash( void ) ;

static
INT32 FLASH_AMD_erase_flasharea( t_FLASH_ctrl_descriptor *p_param ) ;

static
INT32 FLASH_AMD_inquire_flasharea( t_FLASH_ctrl_descriptor *p_param ) ;

static
INT32 FLASH_AMD_test_systemflash( void ) ;

static
INT32 FLASH_AMD_test_fileflash( void ) ;

static
INT32 FLASH_AMD_test_monitorflash( void ) ;

static
INT32 FLASH_AMD_wait_ready(void *pw, long data );

static
INT32 FLASH_AMD_error_lookup( t_sys_error_string *p_param ) ;


/************************************************************************
*      Implementation : Public functions
************************************************************************/


/************************************************************************
*
*                          FLASH_AMD_install
*  Description :
*  -------------
*
*  Installs the AMD FLASH device drivers services in 
*  the IO system at the reserved device slot, found in the
*  'sysdev.h' file, which defines all major device numbers.
*
*  Note:
*  This service is the only public declared interface function; all
*  provided device driver services are static declared, but this
*  function installs the function pointers in the io-system to
*  enable the provided public driver services.
*
*  Parameters :
*  ------------
*
*  -
*
*
*  Return values :
*  ---------------
*
*  'OK'(=0)
*  'ERROR_IO_ILLEGAL_MAJOR':  Illegal major device number
*  'ERROR_IO_NO_SPACE':       Device slot already allocated
*
************************************************************************/
INT32 FLASH_AMD_install( void )
{
    t_sys_error_lookup_registration registration ;

    /* register lookup syserror */
    registration.prefix = SYSERROR_DOMAIN( ERROR_FLASH ) ;
    registration.lookup = FLASH_AMD_error_lookup ;
    SYSCON_write( SYSCON_ERROR_REGISTER_LOOKUP_ID,
                  &registration,
                  sizeof( registration ) );

    /* Revise - may want to update with SYS_MAJOR_FLASH_AMD
        at this time too many places use SYS_MAJOR_FLASH_STRATA
        so this should work */

    /* pre-initialize local variables and install device services */
    return( IO_install( SYS_MAJOR_FLASH_STRATA, /* major device number */
        (t_io_service) FLASH_AMD_init,    /* 'init'  service     */
        NULL,                 /* 'open'  service  na */
        NULL,                 /* 'close' service  na */
        NULL,                 /* 'read'  service     */
        (t_io_service) FLASH_AMD_write,   /* 'write' service     */
        (t_io_service) FLASH_AMD_ctrl  ) ) ; /* 'ctrl'  service  */
}



/************************************************************************
*      Implementation : Static functions
************************************************************************/

/************************************************************************
*
*                          FLASH_AMD_init
*  Description :
*  -------------
*  This service initializes the FLASH_AMD driver.
*  
*
*  Parameters :
*  ------------
*
*  'major',     IN,    major device number
*  'minor',     IN,    not used
*  'p_param',   INOUT, not used
*
*
*  Return values :
*  ---------------
*
*  'OK'(=0)
*
*
*
************************************************************************/

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -