/*
 * @file hv_drv_Flash.h
 * @brief Header file of misc module.
 *
 * @verbatim
 * ==============================================================================
 *                        ##### How to use #####
 * ==============================================================================
 * (+) Use ()
 *
 * @endverbatim
 * @author HiView SoC Software Team
 * @version 1.0.0
 * @date 2023-05-11
 */

#ifndef _HV_DRV_FLASH_H
#define _HV_DRV_FLASH_H

typedef enum _FlashAddressWidth
{
    FLASH_ADDRESS_WIDTH_24 = 24,
    FLASH_ADDRESS_WIDTH_32 = 32,
} FlashAddressWidth;

typedef enum FlashRateMode
{
    FLASH_STANDARD = 0,
    FLASH_DUAL,
    FLASH_QUAD,
    FLASH_4XIO,
    FLASH_QPI,
} FlashRateMode;

typedef enum _FlashMultiIOType
{
    /*!< In Dual and quad mode, instruction and address send  by one line*/
    FLASH_MULTIIO_FAST_TYPE = 0,
    /*!< instruction and address send by 4 line */
    FLASH_INSTRUCT_ADDR_4LINE,
    FLASH_ADDR_4LINE,
} FlashMultiIOType;


typedef enum _FlashWorkModeSel
{
    FLASH_USE_POLLING = 0,
    FLASH_USE_INT,
    FLASH_USE_DMA,
} FlashWorkModeSel;

typedef enum _FlashDataWidth
{
     /*! <The DataSize for transmition is 8bits    */
    FLASH_DATAWIDTH_8 = 8,
    /*!< The DataSize for transmition is 16bits */
    FLASH_DATAWIDTH_16 = 16,
     /*!< The DataSize for transmition is 32bits */
    FLASH_DATAWIDTH_32 = 32,
} FlashDataWidth;

typedef enum _FlashEraseType
{
    FLASH_ERASE_SECTOR = 0,
    FLASH_ERASE_MULTI_SECTOR,
    FLASH_ERASE_CHIP
} FlashEraseType;

typedef enum _FlashTransRW
{
    FLASH_WRITE = 0,
    FLASH_READ,
} FlashTransRW;

typedef enum _FlashCSSel
{
    FLASH_CS_HARDWARE_SELF = 0,
    FLASH_CS_BY_GPIO,
} FlashCSSel;

typedef struct _FlashTxMem
{
    UCHAR8 cmdAddr[20];
    UCHAR8 txData[256];
} FlashTxMem;


typedef Status (*Flash_CpltCallback)(FlashTransRW transType, void *arg);

typedef struct _FlashInitParam
{
    /*!< Specifies flash adress is 24 or 32   bits */
    FlashAddressWidth       AddrWidth;
    FlashWorkModeSel        WorkModeSel;
    FlashRateMode           RateMode;
    /*!< Specifies the SPI data size.                       */
    FlashDataWidth          DataSize;
    FlashMultiIOType TransType;
    Flash_CpltCallback      FlashCpltCallback;
    FlashModel              FlashModel;
    FlashCSSel              CsSel;
    void*                   FlashCallback;
}FlashInitParam;

typedef struct  _FlashSelf  FlashSelf;

/** @defgroup flash flash
 *  flash rd/rw functions
 *  @{
 */

VOID Hv_Drv_Flash_ProtectDisable(VOID);

/**
 * @brief write pm data to flash.
 *  @param uiAddress write offset at flash
 *  @param pucData   address write data to
 *  @param uiDataSize write data size
 *  @retval Status
 */
Status Hv_Drv_Flash_WritePMConfigData(UINT32 uiIndex, UINT32 uiData);

/**
 * @brief write data to flash.
 *  @param uiAddress write offset at flash. only support monitordata partition
 *  @param pucData   address write data to
 *  @param uiDataSize write data size
 *  @retval Status
 */
Status Hv_Drv_Flash_WriteMonitorData(UINT32 uiAddress, UCHAR8 *pucData, UINT32 uiDataSize);

/**
 * @brief read data from flash in xip mode.
 *  @param uiAddress read offset at flash
 *  @param pucData   address for read data
 *  @param uiDataSize size to read data
 *  @retval Status
 */
Status Hv_Drv_Flash_ReadXIP(UINT32 uiAddress, UCHAR8 *pucData, UINT32 uiDataSize);

/**
 * @brief read data from flash.
 *  @param uiAddress read offset at flash. only support monitordata partition
 *  @param pucData   address for read data
 *  @param uiDataSize size to read data
 *  @retval Status
 */
Status Hv_Drv_Flash_ReadMonitorData(UINT32 uiAddress, UCHAR8 *pucData, UINT32 uiDataSize);

/**
 * @brief int flash interface.
 */
void Hv_Drv_Flash_SetQspiMode(FlashRateMode RateMode, FlashWorkModeSel workMode);

/**
 * @brief deint flash interface.
 */
void Hv_Drv_Flash_SetXipMode(void);

/**
 * @brief config logo data and pq etc data partition flag
 */
void Hv_Drv_Flash_ConfigPartFlag(void);

/**
 * @brief erase flash.
 * @param[in] uiWriteAddr Address of flash to do erase.
 * @param[in] uiLength Date length to be written.
 * @return None
 */
void Hv_Drv_Flash_OnlyErase(UINT32 uiReadAddr, UINT32 uiLength);

/**
 * @brief Erase flash by a 64K Bytes(Multi Sector) length.
          Warning: Always erase 64K bytes, please be sure to keep your data safe.
 * @param[in] uiAddr Address of flash to do erase.
 * @return None
 */
void Hv_Drv_Flash_OnlyEraseMultiSector(UINT32 uiAddr);

/**
 * @brief Write flash.
 * @param[in] uiWriteAddr Address of flash to do write.
 * @param[in] pucWriteBuf Date buffer to be written.
 * @param[in] uiLength Date length to be written.
 * @return Status
 */
Status Hv_Drv_Flash_OnlyWrite(UINT32 uiWriteAddr, UCHAR8 *pucWriteBuf, UINT32 uiLength);

/**
 * @brief Write flash.
 * @param[in] uiReadAddr Address of flash to do read.
 * @param[in] pucReadBuf Buffer for saving read back data.
 * @param[in] uiLength Date length to be read.
 * @return None
 */
void Hv_Drv_Flash_OnlyRead(UINT32 uiReadAddr, UCHAR8* pucReadBuf, UINT32 uiLength);

/**
 * @brief whether w/r is ongoing.
 * @return w/r state
 */
UCHAR8 Hv_Drv_Flash_InWritting(void);

/**
 * @brief Flash chip erase.
 * @param[in] None.
 * @return None
 */
void Hv_Drv_Flash_EraseChip(void);

/** @} */
#endif




