
/******************************************************************************/
/**
*
* Fls_Private.h
*
* This file contains MACROs, Data structure declaration and function 
* declaration for intermediate functionalities.
* 
*
* <pre>
*
* MODIFICATION HISTORY:
*
* Ver   Who   Date    Changes
* ----- ---- -------- ----------------------------------------------------------
* 1.0.0 KPIT 01/12/17 First release..
* 1.0.1 KPIT 01/16/17 As per CR 10 banner is updated.
* 1.0.2 KPIT 04/11/17 As per CR11 and CR19
                       Parallel mode is supported.
                       Generic Implementation is Adapted for All Supported Flash
                       Devices.
                       Write Enable Command issued before Entering-4-ByteAdd
                       Mode.
                       Compiler abstraction added for asm and volatile.
* 1.0.3 KPIT 11/11/19  The FLS driver does not support writing less than a configurable 
*                      page size and does not guarantee that a write transfer does not 
*                      exceed a physical page
*                      Fls_MainFunction function will be executed even if Fls module is not initialized.
*                      FLS Supports Macronix flash as well.
*                      Version for external modules checks are removed. 
* </pre>
*
*******************************************************************************/

#ifndef FLS_PRIVATE_H
#define FLS_PRIVATE_H

#ifdef __cplusplus
extern "C"{
#endif
/*******************************************************************************
**                      INCLUDE FILES                                         **
*******************************************************************************/
#include <include/Fls.h>

/*******************************************************************************
**                      EXPORTED CONSTANTS and MACROS                         **
*******************************************************************************/
#define FLS_VENDOR_ID_PRIVATE_H         39u
#define FLS_MODULE_ID_PRIVATE_H         92u
#define FLS_INSTANCE_ID_PRIVATE_H       1u

#define FLS_AR_RELEASE_MAJOR_VERSION_PRIVATE_H        4u
#define FLS_AR_RELEASE_MINOR_VERSION_PRIVATE_H        2u
#define FLS_AR_RELEASE_REVISION_VERSION_PRIVATE_H     2u

#define FLS_SW_MAJOR_VERSION_PRIVATE_H       1u
#define FLS_SW_MINOR_VERSION_PRIVATE_H       0u
#define FLS_SW_PATCH_VERSION_PRIVATE_H       3u
/*******************************************************************************
**                      EXPORTED STRUCTURES, ENUMS, TYPEDEFS                  **
*******************************************************************************/
/**
 * Flash controller ID type
 */
typedef uint32 FLS_HwIDType;

/**
 * Flash address type
 */
typedef uint32 FLS_AddrType;

/**
 * Flash length type
 * This type shall be same as Flash address type as these will be used for
 * arithmetics
 */
typedef uint32 FLS_LenType;

/*Flash Type ID*/
typedef uint32 FLS_FlsIDType;
/**
 * Flash read lane type
 */
typedef uint32 FLS_ReadLaneType;
#define FLS_READ_LANE_SINGLE   (0x1UL)      /**< Flash single lane read mode */
#define FLS_READ_LANE_DUAL     (0x2UL)      /**< Flash dual lane read mode */
#define FLS_READ_LANE_QUAD     (0x3UL)      /**< Flash quad lane read mode */

/**
 * Flash SPI mode type
 */
typedef uint32 FLS_SPIModeType;
/** SPI mode-0 (CPOL = 0, CPHA = 0) */
#define FLS_SPI_MODE_0         (0x1UL)
/** SPI mode-1 (CPOL = 0, CPHA = 1) */
#define FLS_SPI_MODE_1         (0x2UL)
/** SPI mode-2 (CPOL = 1, CPHA = 0) */
#define FLS_SPI_MODE_2         (0x3UL)
/** SPI mode-3 (CPOL = 1, CPHA = 1) */
#define FLS_SPI_MODE_3         (0x4UL)

/**
 * Flash speed type
 */
typedef uint32 FLS_SpeedType;
#define FLS_SPI_SPEED_25M     (0x0UL)       /**< SPI speed = 25MHz */
#define FLS_SPI_SPEED_50M     (0x1UL)       /**< SPI speed = 50MHz */
#define FLS_SPI_SPEED_66M     (0x2UL)       /**< SPI speed = 66MHz */
#define FLS_SPI_SPEED_80M     (0x3UL)       /**< SPI speed = 80MHz */

/**
 * Flash mode type
 */
typedef uint32 FLS_ModeType;
#define FLS_MODE_FAST         (0x1UL)       /**< Flash fast read mode */
#define FLS_MODE_SLOW         (0x2UL)       /**< Flash slow read mode */

/**
 * Flash status type
 */
typedef uint32 FLS_StatusType;
#define FLS_STATUS_UNINIT     (0x0UL)       /**< Flash status uninitialised */
#define FLS_STATUS_IDLE       (0x1UL)       /**< Flash status idle */
#define FLS_STATUS_BUSY       (0x2UL)       /**< Flash status busy */

/**
 * Flash operation result type
 */
typedef uint32 FLS_OpsResultType;
#define FLS_OPS_RESULT_OK         (0x0UL)   /**< Operation result OK */
#define FLS_OPS_RESULT_FAIL       (0x1UL)   /**< Operation result failure */
#define FLS_OPS_RESULT_PENDING    (0x2UL)   /**< Operation result pending */
#define FLS_OPS_RESULT_CANCELLED  (0x3UL)   /**< Operation result cancelled */
#define FLS_OPS_RSLT_BLK_INCONSISTENT  (4UL)   /**< MEMIF_BLOCK_INCONSISTENT */


#define ERR_OK              (0UL)   /**< No error */
#define ERR_BUSY            (1UL)   /**< Device or resource busy */
#define ERR_NODEV           (2UL)   /**< No device found */
#define ERR_NOMEM           (3UL)   /**< Out of memory */
#define ERR_TIME_OUT        (4UL)   /**< Timeout */
#define ERR_INVAL_PARAMS    (5UL)   /**< Invalid argument */
#define ERR_EAGAIN          (6UL)   /**< Try again */
#define ERR_NOSUPPORT       (7UL)   /**< Not supported */
#define ERR_UNINIT          (9UL)   /**< Device or resource not initialized */
#define ERR_DATA_INTEG      (10UL)  /**< Data integrity error */
#define ERR_AUTH_FAILED     (11UL)  /**< Authentication error */
#define ERR_UNKNOWN         (12UL)  /**< Unknown error */
#define ERR_INVAL_MAGIC     (13UL)  /**< Invalid magic number */
#define ERR_CANCELLED       (14UL)  /**< Cancel request success */
#define ERR_INVAL_STATE     (15UL)  /**< Invalid state */
#define ERR_VERSION         (16UL)  /**< Wrong version of hw/sw */
#define ERR_INVAL_BUF_STATE (17UL)  /**< Invalid buffer state */
#define ERR_BUS_FAILURE     (18UL)  /**< Bus Failure */
#define ERR_NACK            (19UL)  /**< NACK error */
#define ERR_MAX_ATTEMPS     (20UL)  /**< Maximum num of attempts */


/* Flash IDs for supported flash */
#define WINBOND_FLASH       (0xEFUL)    /**< Winbond,flash ID */
#define MICRON_FLASH        (0x20UL)    /**< Micron,flash ID */
#define CYPRESS_FLASH       (0x01UL)    /**< Cypress,flash ID */
#define MACRONIX_FLASH      (0xC2UL)    /**< Macronix,flash ID */


/**
 * Flash writable sector list structure
 */
typedef struct {
  FLS_AddrType  startAddr;      /**< Start address of writable sectors */
  uint32          numSectors;     /**< Number of writable sectors */
} FLS_FlsableSecType;

/**
 * Flash config type structure.
 */
typedef struct {
  FLS_LenType       size;       /**< Total flash size in bytes */
  FLS_LenType       pageSize;   /**< Flash page size in bytes */
  FLS_LenType       sectorSize; /**< Flash sector size in bytes */
  FLS_SPIModeType   SPIMode;    /**< SPI mode */
  FLS_ReadLaneType  readLane;   /**< Number of lane of read operation */
  /** Size of flashable sector list */
  uint32            flsableSecListSize;
  /** Pointer to flashable sector list */
  P2CONST(FLS_FlsableSecType, AUTOMATIC, FLS_APPL_DATA) flsableSecList;
  /** Quad enable byte offset in flash status register */
  uint32           ucFlsQspiHwBaudRateDiv;
  /** Quad enable bit mask in flash status register */
  uint8            ucFlsQspiHwConnectionMode;
  FLS_FlsIDType     flsID;      /**< Flash ID */
} FLS_CfgType;


extern VAR(uint8, FLS_VAR_CLEARED) Fls_ApiStatus;
/*******************************************************************************
**                      EXPORTED FUNCTIONS                                    **
*******************************************************************************/
#define FLS_START_SEC_CODE_FAST_ASIL_C
#include "Fls_MemMap.h"
/** @brief Flash operation done indication
 *
 * Callback interface to indicate completion of last flash operation requested
 * by client. It is mandatory for clients to implement this function.
 *
 * @return      void
 */
extern FUNC(void, FLS_CODE_FAST) FLASH_OpsDoneInd(void);

/** @brief Flash operation error indication
 *
 * Callback interface to indicate error during last flash operation requested
 * by client. It is mandatory for clients to implement this function.
 *
 * @return      void
 */
extern FUNC(void, FLS_CODE_FAST) Fls_OpsErrInd(void);
#define FLS_STOP_SEC_CODE_FAST_ASIL_C
#include "Fls_MemMap.h"


#define FLS_START_SEC_CODE_SLOW_ASIL_C
#include "Fls_MemMap.h"

/** @brief Initialize Flash driver
 *
 * Interface to initializes Flash driver
 * @param[in]   aConfig     Pointer to configuration structure
 *
 * @return      void
 */
extern FUNC(void, FLS_CODE_SLOW) Fls_InterfaceInit(
                 CONSTP2CONST(FLS_CfgType, AUTOMATIC, FLS_APPL_DATA) aConfig);

/** @brief De-initialize Flash driver
 *
 * Interface to de-initializes Flash driver
 *
 * @param[in]   aID                 Index of the controller
 *
 * @return      void
 */
extern FUNC(void, FLS_CODE_SLOW) Fls_DeInit(void);

#define FLS_STOP_SEC_CODE_SLOW_ASIL_C
#include "Fls_MemMap.h"

#define FLS_START_SEC_CODE_ASIL_C
#include "Fls_MemMap.h"
/** @brief Flash cancel
 *
 * Interface to cancel current flash operation. This function is expected to
 * cancel an on-going flash operation if any.
 * FLASH_Cancel will raise an FLASH_OpsErrInd() to indicate cancellation of
 * an operation.
 * Note:
 *  It is possible that when FLASH_Cancel is called there is an actual flash
 *  operation in progress. Due to asynchronous behavior of flash hardware, it
 *  may not be possible to stop that operation. So, state of region of
 *  operation in flash is not guaranteed after successful completion of
 *  FLASH_Cancel.
 *
 * @retval      ERR_OK              Flash operation successfully completed
 * @retval      ERR_INVAL_PARAMS    Interface called with invalid parameter
 * @retval      ERR_INVAL_STATE     Interface called in FLASH_STATUS_UNINT
 */
extern FUNC(uint32, FLS_CODE) Fls_InterfaceCancel(void);

#define FLS_STOP_SEC_CODE_ASIL_C
#include "Fls_MemMap.h"

#define FLS_START_SEC_CODE_FAST_ASIL_C
#include "Fls_MemMap.h"

/** @brief Get flash driver status
 *
 * Interface to get status of the driver
 *
 * @param[out]  aStatus     Pointer to output flash driver status
 *
 * @retval      ERR_OK              Flash operation successfully completed
 * @retval      ERR_INVAL_PARAMS    Interface called with invalid parameter
 */
extern FUNC(uint32, FLS_CODE_FAST) Fls_InterfaceGetStatus(
        CONSTP2VAR(FLS_StatusType, AUTOMATIC, FLS_APPL_DATA) ulStatus);

/** @brief Get flash operation results
 *
 * Interface to get result of last requested flash operation
 *
 * @param[out]  aOpsRes     Pointer to output last flash operation result
 *
 * @retval      ERR_OK              Flash operation successfully completed
 * @retval      ERR_INVAL_PARAMS    Interface called with invalid parameter
 */
extern FUNC(uint32, FLS_CODE_FAST) Fls_GetOpsResult(
             CONSTP2VAR(FLS_OpsResultType, AUTOMATIC, FLS_APPL_DATA) ulOpsRes);

/** @brief Set flash mode
 *
 * Interface to set flash mode for fast/slow read.
 * Note:
 *  This interface is not currently supported.
 * @param[in]   aMode       Flash mode
 *
 * @retval      ERR_OK              Flash operation successfully completed
 * @retval      ERR_INVAL_PARAMS    Interface called with invalid parameter
 * @retval      ERR_INVAL_STATE     Interface called in FLASH_STATUS_UNINT
 * @retval      ERR_BUSY            Flash driver busy with last operation
 */
extern FUNC(uint32, FLS_CODE_FAST) Fls_InterfaceSetMode(void);


/** @brief Erase flash
 *
 * Interface to erase flash sectors. Number of sectors to be erased will be
 * determined using aAddr and aLen parameters.
 * This is an asynchronous interface. Successful completion of flash operation
 * will be indicated to client by raising FLASH_OpsDoneInd() callback. Failure
 * during flash operation will be indicated by raising FLASH_OpsErrInd()
 * callback.
 *
 * Note:
 *  If aAddr passed is not aligned to flash sector boundary, flash erase will
 *  start erasing form previous sector boundary.
 *  If (aAddr + aLen) is not aligned to multiple sector size, flash erase will
 *  erase till next sector boundary.
 *
 * @param[in]   aAddr       Flash address to start flash erase
 * @param[in]   aLen        Length to erase in bytes
 *
 * @retval      ERR_OK              Flash operation successfully initiated
 * @retval      ERR_INVAL_PARAMS    Interface called with invalid parameter
 * @retval      ERR_INVAL_STATE     Interface called in FLASH_STATUS_UNINT
 * @retval      ERR_BUSY            Flash driver busy with last operation
 */
extern FUNC(uint32, FLS_CODE_FAST) Fls_InterfaceErase(
                                   VAR(FLS_AddrType, AUTOMATIC) ulAddr,
                                   VAR(FLS_LenType, AUTOMATIC) ulLen);

/** @brief Flash write
 *
 * Interface to write flash from aAddr for aLen bytes. Flash write will
 * happen for one or more flash page sizes.
 * This is an asynchronous interface. Successful completion of flash operation
 * will be indicated to client by raising FLASH_OpsDoneInd() callback. Failure
 * during flash operation will be indicated by raising FLASH_OpsErrInd()
 * callback.
 *
 * @param[in]   aAddr       Flash start addrss for operation
 * @param[in]   aBuf        Pointer to output data buffer
 * @param[in]   aLen        Length to write in bytes
 *
 * @retval      ERR_OK              Flash operation successfully initiated
 * @retval      ERR_INVAL_PARAMS    Interface called with invalid parameter
 * @retval      ERR_INVAL_STATE     Interface called in FLASH_STATUS_UNINT
 * @retval      ERR_BUSY            Flash driver busy with last operation
 */
extern FUNC(uint32, FLS_CODE_FAST) Fls_InterfaceWrite(
                            VAR(FLS_AddrType, AUTOMATIC) ulAddr,
                            P2CONST(uint8, AUTOMATIC, FLS_APPL_DATA) pBuf,
                            VAR(FLS_LenType, AUTOMATIC) ulLen);

/** @brief Flash Read
 *
 * Interface to read flash from aAddr for aLen bytes.
 * This is an asynchronous interface. Successful completion of flash operation
 * will be indicated to client by raising FLASH_OpsDoneInd() callback. Failure
 * during flash operation will be indicated by raising FLASH_OpsErrInd()
 * callback.
 *
 * @param[in]   aAddr       Flash start address for read
 * @param[in]   aLen        Length to read in bytes
 * @param[out]   aBuf        Pointer to data buffer
 *
 * @retval      ERR_OK              Flash operation successfully initiated
 * @retval      ERR_INVAL_PARAMS    Interface called with invalid parameter
 * @retval      ERR_INVAL_STATE     Interface called in FLASH_STATUS_UNINT
 * @retval      ERR_BUSY            Flash driver busy with last operation
 */
extern FUNC(uint32, FLS_CODE_FAST) Fls_InterfaceRead(
                           VAR(FLS_AddrType, AUTOMATIC) ulAddr,
                           CONSTP2VAR(uint8, AUTOMATIC, FLS_APPL_DATA) pBuf,
                           VAR(FLS_LenType, AUTOMATIC) ulLen);

/** @brief Flash Compare
 *
 * Interface to compare data present in input buffer to the data in flash
 * region starting from aAddr for aLen bytes
 * This is an asynchronous interface. Successful completion of flash operation
 * will be indicated to client by raising FLASH_OpsDoneInd() callback. Failure
 * during flash operation will be indicated by raising FLASH_OpsErrInd()
 * callback.
 *
 * @param[in]   aAddr       Flash start address for comparison
 * @param[in]   aBuf        Pointer to data buffer
 * @param[in]   aLen        Length in bytes to compare
 *
 * @retval      ERR_OK              Flash operation successfully initiated
 * @retval      ERR_INVAL_PARAMS    Interface called with invalid parameter
 * @retval      ERR_INVAL_STATE     Interface called in FLASH_STATUS_UNINT
 * @retval      ERR_BUSY            Flash driver busy with last operation
 */
extern FUNC(uint32, FLS_CODE_FAST) Fls_InterfaceCompare(
                           VAR(FLS_AddrType, AUTOMATIC) ulAddr,
                           CONSTP2CONST(uint8, AUTOMATIC, FLS_APPL_DATA) pBuf,
                           VAR(FLS_LenType, AUTOMATIC) ulLen);

/** @brief Flash Blank Check
 *
 * Interface to check whether flash region starting from aAddr for aLen bytes
 * is erased and nothing is written after erase.
 * This is an asynchronous interface. Successful completion of flash operation
 * will be indicated to client by raising FLASH_OpsDoneInd() callback. Failure
 * during flash operation will be indicated by raising FLASH_OpsErrInd()
 * callback.
 *
 * @param[in]   aAddr       Flash address for blank check
 * @param[in]   aLen        Length to verify
 *
 * @retval      ERR_OK              Flash operation successfully initiated
 * @retval      ERR_INVAL_PARAMS    Interface called with invalid parameter
 * @retval      ERR_INVAL_STATE     Interface called in FLASH_STATUS_UNINT
 * @retval      ERR_BUSY            Flash driver busy with last operation
 */
extern FUNC(uint32, FLS_CODE_FAST) Fls_InterfaceBlankCheck(
                                        VAR(FLS_AddrType, AUTOMATIC) ulAddr,
                                        VAR(FLS_LenType, AUTOMATIC) ulLen);

/** @brief Flash IRQ Process
 *
 * Interface to be called from differed interrupt task for processing
 * flash operations.
 *
 *
 * @return      void
 */
extern FUNC(void, FLS_CODE_FAST) Fls_IRQProcess(void);

extern FUNC(void, FLS_CODE_FAST) Fls_MainProcess(void);


#define FLS_STOP_SEC_CODE_FAST_ASIL_C
#include "Fls_MemMap.h"

#ifdef __cplusplus
}
#endif

#endif /* FLS_PRIVATE_H */
/*******************************************************************************
**                               End of File                                  **
*******************************************************************************/
