382 lines
		
	
	
		
			9.9 KiB
		
	
	
	
		
			C
		
	
	
	
	
	
			
		
		
	
	
			382 lines
		
	
	
		
			9.9 KiB
		
	
	
	
		
			C
		
	
	
	
	
	
| /****************************************************************************
 | ||
| 
 | ||
| Copyright(c) 2019 by Aerospace C.Power (Chongqing) Microelectronics. ALL RIGHTS RESERVED.
 | ||
| 
 | ||
| This Information is proprietary to Aerospace C.Power (Chongqing) Microelectronics and MAY NOT
 | ||
| be copied by any method or incorporated into another program without
 | ||
| the express written consent of Aerospace C.Power. This Information or any portion
 | ||
| thereof remains the property of Aerospace C.Power. The Information contained herein
 | ||
| is believed to be accurate and Aerospace C.Power assumes no responsibility or
 | ||
| liability for its use in any way and conveys no license or title under
 | ||
| any patent or copyright and makes no representation or warranty that this
 | ||
| Information is free from patent or copyright infringement.
 | ||
| 
 | ||
| ****************************************************************************/
 | ||
| #ifndef FLASH_H
 | ||
| #define FLASH_H
 | ||
| 
 | ||
| #include "sfc.h"
 | ||
| 
 | ||
| #ifdef __cplusplus
 | ||
| extern "C" {
 | ||
| #endif
 | ||
| extern sfc_ctrl_t *g_sfc_ctrl;
 | ||
| 
 | ||
| #define PAGE_PROGRAM_MASK           0xFF
 | ||
| #define SECTOR_ERASE_MASK           0xFFF
 | ||
| #define BLOCK_ERASE_32K_MASK        0x1FFFF
 | ||
| #define BLOCK_ERASE_64K_MASK        0xFFFF
 | ||
| 
 | ||
| #define PAGE_NUM_PER_SECTOR         0x10
 | ||
| #define PAGE_PROGRAM_SIZE           0x100
 | ||
| #define PAGE_ERASE_SIZE             0x100
 | ||
| #define SECTOR_ERASE_SIZE           0x1000
 | ||
| #define BLOCK_ERASE_32K_SIZE        0x8000
 | ||
| #define BLOCK_ERASE_64K_SIZE        0x10000
 | ||
| 
 | ||
| #define STS_WIP_BIT_S0              0x01
 | ||
| #define STS_WEL_BIT_S1              0x02
 | ||
| #define QUAD_ENA_BIT_S9             0x02
 | ||
| #define ADDR_DEFAULT_VAL_ZERO       0x00
 | ||
| 
 | ||
| #define SFDP_DEVICE_ID_ADDR         0x10
 | ||
| #define SFDP_DEVICE_ID_POS          0x03
 | ||
| 
 | ||
| #define FLASH_SUSPEND_ERASE         0
 | ||
| #define FLASH_SUSPEND_PROG          1
 | ||
| #define FLASH_CHIP_ID_LEN           16
 | ||
| 
 | ||
| /** \defgproup Driver_APIs FLASH APIs
 | ||
|  *  @brief FLASH APIs
 | ||
|  *
 | ||
|  *
 | ||
|  *
 | ||
|  */
 | ||
| 
 | ||
| /** @addtogroup Driver_APIs
 | ||
|  * @{
 | ||
|  *
 | ||
|  */
 | ||
| 
 | ||
| typedef enum _flash_size {
 | ||
|     FLASH_1M = 1,
 | ||
|     FLASH_2M = 2,
 | ||
|     FLASH_4M = 3,
 | ||
|     FLASH_8M = 4,
 | ||
|     FLASH_16M = 5,
 | ||
|     FLASH_MAX,
 | ||
| } flash_size_t;
 | ||
| 
 | ||
| typedef enum {
 | ||
|     SFC_MAP_STS_INIT = 0,
 | ||
|     SFC_MAP_STS_DONE = 1,
 | ||
|     SFC_MAP_STS_ERR = 2,
 | ||
| } sfc_map_sts_t;
 | ||
| 
 | ||
| typedef enum {
 | ||
|     MODE_ERASE_MIN      = 0x00U,
 | ||
|     MODE_ERASE_SECTOR   = 0x00U,
 | ||
|     MODE_ERASE_PAGE     = 0x01U,
 | ||
|     MODE_ERASE_BLOCK64  = 0x02U,
 | ||
|     MODE_ERASE_BLOCK32  = 0x03U,
 | ||
|     MODE_ERASE_CHIP     = 0x04U,
 | ||
|     MODE_ERASE_MAX,
 | ||
| } mode_erase_t;
 | ||
| 
 | ||
| typedef enum {
 | ||
|     MODE_WRITE_STS_REG0 = 0x00U,
 | ||
|     MODE_WRITE_STS_REG1 = 0x01U
 | ||
| } mode_write_sts_t;
 | ||
| 
 | ||
| typedef enum {
 | ||
|     MOD_SFC_READ_MIN                = 0x00U,
 | ||
|     MOD_SFC_READ_SIG                = 0x00U,
 | ||
|     MOD_SFC_READ_HIGH_SPD           = 0x01U,
 | ||
|     MOD_SFC_READ_DUAL_FAST          = 0x02U,
 | ||
|     MOD_SFC_READ_QUAD_FAST          = 0x03U,
 | ||
|     MOD_SFC_READ_DUAL_IO_FAST       = 0x04U,
 | ||
|     MOD_SFC_READ_QUAD_IO_FAST       = 0x05U,
 | ||
|     MOD_SFC_READ_QUAD_IO_WORD_FAST  = 0x06U,
 | ||
|     MOD_SFC_READ_MAX,
 | ||
| } flash_read_mode_t;
 | ||
| 
 | ||
| typedef enum {
 | ||
|     MOD_PAGE_PROGRAM_MIN    = 0x00U,
 | ||
|     MOD_PAGE_PROGRAM_STAND  = 0x01U,
 | ||
|     MOD_PAGE_PROGRAM_QUAD   = 0x02U,
 | ||
|     MOD_PAGE_PROGRAM_FAST   = 0x03U,
 | ||
|     MOD_PAGE_PROGRAM_MAX,
 | ||
| } flash_page_prog_t;
 | ||
| 
 | ||
| typedef enum {
 | ||
|     HAL_OK       = 0x00U,
 | ||
|     HAL_ERROR    = 0x01U,
 | ||
|     HAL_BUSY     = 0x02U,
 | ||
|     HAL_TIMEOUT  = 0x03U,
 | ||
|     HAL_NOT_SUPPORTED = 0x04U
 | ||
| } flash_rt_sts_t;
 | ||
| 
 | ||
| enum {
 | ||
|     FLASH_OS_DISABLE    = 0,
 | ||
|     FLASH_OS_ENABLE     = 1,
 | ||
| };
 | ||
| 
 | ||
| typedef struct _flash_os_cb {
 | ||
|     uint32_t is_sw_sus_resume;
 | ||
|     uint32_t (*get_gp_timer_val)(uint8_t id);
 | ||
|     void (*task_suspend_all)(void);
 | ||
|     void (*task_resume_all)(void);
 | ||
| } flash_os_cb_t;
 | ||
| 
 | ||
| typedef struct {
 | ||
|     uint8_t is_erase;
 | ||
|     uint8_t read_mode;
 | ||
|     uint8_t write_mode;
 | ||
|     uint8_t erase_mode;
 | ||
|     uint8_t sw_mode;
 | ||
|     flash_os_cb_t *cb;
 | ||
| } flash_write_param_t;
 | ||
| 
 | ||
| #define FLASH_PARAM_CHECK(a) \
 | ||
|     if (!(a)) { \
 | ||
|         return HAL_ERROR; \
 | ||
|     } \
 | ||
| 
 | ||
| 
 | ||
| #define REGION_ADDR_OF_ID(id)          ((id + 1)*0x1000)
 | ||
| 
 | ||
| /**
 | ||
|  * @brief flash_probe() - function used to probe device by reading device ID
 | ||
|  * @param data:         the pointer of data where the manufacturer ID is saved
 | ||
|  *
 | ||
|  * @return              HAL_OK -- succeed
 | ||
|  * @return              HAL_ERROR -- fail
 | ||
|  *
 | ||
|  */
 | ||
| uint8_t flash_probe(void *data);
 | ||
| 
 | ||
| /**
 | ||
|  * @brief flash_write() - function used to program data info flash memory
 | ||
|  * @param data:         the pointer of data to be written
 | ||
|  * @param addr:         the start address
 | ||
|  * @param size:         the data length(flash page size,unit is byte)
 | ||
|  * @param param:        the pointer of parameters - flash read/write/erase mode
 | ||
|  *
 | ||
|  * @return              HAL_OK - succeed
 | ||
|  * @return              HAL_ERROR - fail
 | ||
|  *
 | ||
|  */
 | ||
| uint8_t flash_write(const void* data, uint32_t addr,
 | ||
|                     uint32_t size, void *param);
 | ||
| 
 | ||
| /**
 | ||
|  * @brief flash_read() - function used to read data from flash memory
 | ||
|  * @param data:         the read value will be saved in this pointer
 | ||
|  * @param addr:         the starting address
 | ||
|  * @param size:         the data length(flash page size,unit is byte)
 | ||
|  * @param mode:         read flash data mode
 | ||
|  *
 | ||
|  * @return              HAL_OK - succeed
 | ||
|  * @return              HAL_ERROR - fail
 | ||
|  *
 | ||
|  */
 | ||
| uint8_t flash_read(void* data, uint32_t addr, int size, uint8_t mode);
 | ||
| 
 | ||
| /**
 | ||
|  * @brief flash_erase() - function used to erase flash memory data
 | ||
|  * @param addr:         the starting address
 | ||
|  * @param uint32_t mode : the erase type
 | ||
|  * @param param:        the pointer of parameters - flash read/write/erase mode
 | ||
|  *
 | ||
|  * @return              HAL_OK - succeed
 | ||
|  * @return              HAL_ERROR - fail
 | ||
|  *
 | ||
|  */
 | ||
| uint8_t flash_erase(uint32_t addr, void *param);
 | ||
| 
 | ||
| /**
 | ||
|  * @brief flash_get_dev_id() -function used to get flash device ID
 | ||
|  * @param data:         the manufacturer ID will be saved in this pointer
 | ||
|  *
 | ||
|  * @return              HAL_OK - succeed
 | ||
|  * @return              HAL_ERROR - fail
 | ||
|  *
 | ||
|  */
 | ||
| uint8_t flash_get_dev_id(void* data);
 | ||
| 
 | ||
| /**
 | ||
|  * @brief flash_get_chip_id() -function used to get flash chip ID
 | ||
|  * @param data:         the chip ID will be saved in this pointer
 | ||
|  *
 | ||
|  * @return              HAL_OK - succeed
 | ||
|  * @return              HAL_ERROR - fail
 | ||
|  *
 | ||
|  */
 | ||
| uint8_t flash_get_chip_id(void* data);
 | ||
| 
 | ||
| 
 | ||
| /**
 | ||
|  * @brief flash_get_dev_size() -function used to get flash device size
 | ||
|  *
 | ||
|  * @return              FLASH_1M
 | ||
|  * @return              FLASH_2M
 | ||
|  *
 | ||
|  */
 | ||
| uint32_t flash_get_dev_size(void);
 | ||
| 
 | ||
| 
 | ||
| /**
 | ||
|  * @brief flash_get_dev_base() -function used to get flash device's base address.
 | ||
|  *
 | ||
|  * @return              base address
 | ||
|  *
 | ||
|  */
 | ||
| uint32_t flash_get_dev_base(void);
 | ||
| 
 | ||
| /**
 | ||
|  * @brief flash_get_pe_mode() -function used to get flash device's program/erase mode.
 | ||
|  *
 | ||
|  * @return              MOD_SW_MODE_ENA - SW SUPEND/RESUME
 | ||
|  * @return              MOD_SW_MODE_DIS - HW SUPEND/RESUME
 | ||
|  *
 | ||
|  */
 | ||
| uint32_t flash_get_pe_mode(void);
 | ||
| 
 | ||
| /**
 | ||
|  * @brief flash_get_sts_reg() - function used ot get flash status register
 | ||
|  * @param data:         the status register will be saved in this pointer
 | ||
|  * @param reg_num:      register number is 0/1
 | ||
|  *
 | ||
|  * @return              HAL_OK - succeed
 | ||
|  * @return              HAL_ERROR - fail
 | ||
|  *
 | ||
|  */
 | ||
| uint8_t flash_get_sts_reg(void* data, int reg_num);
 | ||
| 
 | ||
| /**
 | ||
|  * @brief flash_set_quad_mode() - function used to set flash quad mode
 | ||
|  *
 | ||
|  * @return              HAL_OK - succeed
 | ||
|  * @return              HAL_ERROR - fail
 | ||
|  *
 | ||
|  */
 | ||
| uint8_t flash_set_quad_mode();
 | ||
| 
 | ||
| /**
 | ||
|  * @brief flash_addr_mapping() - function used to exchange mapping flash address
 | ||
|  * @param offset1:      the address to exchanged
 | ||
|  * @param offset2:      the address to exchanged
 | ||
|  * @param size:         the range of address
 | ||
|  *
 | ||
|  * @return              HAL_OK - succeed
 | ||
|  * @return              HAL_ERROR - fail
 | ||
|  *
 | ||
|  */
 | ||
| uint8_t flash_addr_mapping(uint32_t offset1, uint32_t offset2, uint32_t size);
 | ||
| 
 | ||
| 
 | ||
| /**
 | ||
|  * @brief flash_get_mapping_sts() - function used to get mapping status
 | ||
|  *
 | ||
|  * @return              HAL_OK - succeed
 | ||
|  * @return              HAL_ERROR - fail
 | ||
|  *
 | ||
|  */
 | ||
| uint8_t flash_get_mapping_sts(uint32_t *status);
 | ||
| 
 | ||
| /**
 | ||
|  * @brief flash_clk_div_set() - function used to set flash controller's clock div.
 | ||
|  *
 | ||
|  * @return
 | ||
|  *
 | ||
|  */
 | ||
| void flash_clk_div_set(uint8_t div);
 | ||
| 
 | ||
| /**
 | ||
|  * @brief flash_clk_div_set() - function used to get flash controller's clock div.
 | ||
|  *
 | ||
|  * @return            clock div
 | ||
|  *
 | ||
|  */
 | ||
| uint8_t flash_clk_div_get();
 | ||
| 
 | ||
| 
 | ||
| /**
 | ||
|  * @brief flash_init() - function used to initiate flash controller
 | ||
|  * @param mode:         init degree, 1: full init 0: part init
 | ||
|  *
 | ||
|  * @return              HAL_OK - succeed
 | ||
|  * @return              HAL_ERROR - fail
 | ||
|  *
 | ||
|  */
 | ||
| uint8_t flash_init(int mode);
 | ||
| 
 | ||
| /**
 | ||
|  * @brief flash_data_reinforce() - read data and write twice to reinforce
 | ||
|     flash data, this function is only applicable to Puya specific flash chip.
 | ||
|  * @param offset: flash offset address
 | ||
|  * @return              HAL_OK - succeed
 | ||
|  * @return              HAL_ERROR - fail
 | ||
|  */
 | ||
| uint8_t flash_data_reinforce(uint32_t offset, uint32_t len);
 | ||
| 
 | ||
| /**
 | ||
|  * @brief flash_set_os_ena() - set g_flash_os_ena = TRUE
 | ||
|  * @return              void
 | ||
|  */
 | ||
| void flash_set_os_ena();
 | ||
| 
 | ||
| /**
 | ||
|  * @brief flash_set_os_dis() - set g_flash_os_ena = FALSE
 | ||
|  * @return              void
 | ||
|  */
 | ||
| void flash_set_os_dis();
 | ||
| 
 | ||
| /**
 | ||
|  * @brief flash_os_is_enable() - get g_flash_os_ena value
 | ||
|  * @return              void
 | ||
|  */
 | ||
| uint8_t flash_os_is_enable();
 | ||
| 
 | ||
| /**
 | ||
|  * @brief flash_otp_write() - write flash otp (Security) register
 | ||
|  * @return              void
 | ||
|  */
 | ||
| uint8_t flash_otp_write(FLASH_OTP_REGION_ID id,
 | ||
|                          uint32_t addr, const void *buf, size_t count);
 | ||
| 
 | ||
| /**
 | ||
|  * @brief flash_otp_read() - read flash otp (Security) register
 | ||
|  * @return              void
 | ||
|  */
 | ||
| uint8_t flash_otp_read(FLASH_OTP_REGION_ID id,
 | ||
|                         uint32_t addr, void *buf, size_t count);
 | ||
| 
 | ||
| /**
 | ||
|  * @brief flash_otp_erase() - erase flash otp (Security) register
 | ||
|  * @return              void
 | ||
|  */
 | ||
| uint8_t flash_otp_erase(FLASH_OTP_REGION_ID id);
 | ||
| 
 | ||
| /**
 | ||
|  * @brief flash_otp_lock() - lock flash otp (Security) register ,
 | ||
|    once it’s lock, the corresponding 256-Byte Security Register will become
 | ||
|    read-only permanently.
 | ||
|  * @return              void
 | ||
|  */
 | ||
| void flash_otp_lock(FLASH_OTP_REGION_ID id);
 | ||
| 
 | ||
| /**
 | ||
|  * @brief flash_otp_get_size() - get flash otp (Security) register size(bytes)
 | ||
|  * @return              void
 | ||
|  */
 | ||
| uint32_t flash_otp_get_size(void);
 | ||
| 
 | ||
| 
 | ||
| #ifdef __cplusplus
 | ||
| }
 | ||
| #endif
 | ||
| 
 | ||
| #endif // FLASH_H
 | ||
| 
 |