/* Copyright Statement:
 *
 * This software/firmware and related documentation ("AutoChips Software") are
 * protected under relevant copyright laws. The information contained herein is
 * confidential and proprietary to AutoChips Inc. and/or its licensors. Without
 * the prior written permission of AutoChips inc. and/or its licensors, any
 * reproduction, modification, use or disclosure of AutoChips Software, and
 * information contained herein, in whole or in part, shall be strictly
 * prohibited.
 *
 * AutoChips Inc. (C) 2022. All rights reserved.
 *
 * BY OPENING THIS FILE, RECEIVER HEREBY UNEQUIVOCALLY ACKNOWLEDGES AND AGREES
 * THAT THE SOFTWARE/FIRMWARE AND ITS DOCUMENTATIONS ("AUTOCHIPS SOFTWARE")
 * RECEIVED FROM AUTOCHIPS AND/OR ITS REPRESENTATIVES ARE PROVIDED TO RECEIVER
 * ON AN "AS-IS" BASIS ONLY. AUTOCHIPS EXPRESSLY DISCLAIMS ANY AND ALL
 * WARRANTIES, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR
 * NONINFRINGEMENT. NEITHER DOES AUTOCHIPS PROVIDE ANY WARRANTY WHATSOEVER WITH
 * RESPECT TO THE SOFTWARE OF ANY THIRD PARTY WHICH MAY BE USED BY,
 * INCORPORATED IN, OR SUPPLIED WITH THE AUTOCHIPS SOFTWARE, AND RECEIVER AGREES
 * TO LOOK ONLY TO SUCH THIRD PARTY FOR ANY WARRANTY CLAIM RELATING THERETO.
 * RECEIVER EXPRESSLY ACKNOWLEDGES THAT IT IS RECEIVER'S SOLE RESPONSIBILITY TO
 * OBTAIN FROM ANY THIRD PARTY ALL PROPER LICENSES CONTAINED IN AUTOCHIPS
 * SOFTWARE. AUTOCHIPS SHALL ALSO NOT BE RESPONSIBLE FOR ANY AUTOCHIPS SOFTWARE
 * RELEASES MADE TO RECEIVER'S SPECIFICATION OR TO CONFORM TO A PARTICULAR
 * STANDARD OR OPEN FORUM. RECEIVER'S SOLE AND EXCLUSIVE REMEDY AND AUTOCHIPS'S
 * ENTIRE AND CUMULATIVE LIABILITY WITH RESPECT TO THE AUTOCHIPS SOFTWARE
 * RELEASED HEREUNDER WILL BE, AT AUTOCHIPS'S OPTION, TO REVISE OR REPLACE THE
 * AUTOCHIPS SOFTWARE AT ISSUE, OR REFUND ANY SOFTWARE LICENSE FEES OR SERVICE
 * CHARGE PAID BY RECEIVER TO AUTOCHIPS FOR SUCH AUTOCHIPS SOFTWARE AT ISSUE.
 */

#ifndef _AC780X_EFLASH_H
#define _AC780X_EFLASH_H
/*!
 * @file ac780x_eflash.h
 *
 * @brief This file provides EFLASH integration functions interface.
 *
 */
#ifdef __cplusplus
extern "C" {
#endif

/* ===========================================  Includes  =========================================== */
#include "ac7802x.h"

/* ============================================  Define  ============================================ */
#define PFLASH_BASE_ADDRESS             (0x08000000UL)                       /*!< Based address of P-Flash user area */
#define PFLASH_SIZE                     (0x00008000UL)                       /*!< Total size for P-Flash user area */
#define PFLASH_PAGE_SIZE                (0x00000200UL)                       /*!< P-Flash page size */
#define PFLASH_END_ADDRESS              (PFLASH_BASE_ADDRESS + PFLASH_SIZE)  /*!< End address of P-Flash user area */
#define PFLASH_PAGE_AMOUNT              (PFLASH_SIZE / PFLASH_PAGE_SIZE)     /*!< P-Flash page total page amount */

#define DFLASH_BASE_ADDRESS             (0x08020000UL)                       /*!< Based address of D-Flash user area */
#define DFLASH_SIZE                     (0x00000800UL)                       /*!< Total size for D-Flash user area */
#define DFLASH_PAGE_SIZE                (0x00000008UL)                       /*!< D-Flash page size */
#define DFLASH_BLOCK_SIZE               (0x00000010UL)                       /*!< D-Flash block size */
#define DFLASH_END_ADDRESS              (DFLASH_BASE_ADDRESS+DFLASH_SIZE)    /*!< End address of D-Flash user area */
#define DFLASH_PAGE_AMOUNT              (DFLASH_SIZE/DFLASH_PAGE_SIZE)       /*!< D-flash page total page amount */

#define OPTION_BASE                     (0x08040000UL)                       /*!< Optoion byte base addresses */
#define OPTION_END                      (0x08040200UL)                       /*!< Optoion byte end addresses */
#define OPTION_PAGE_SIZE                (0x80UL)                             /*!< Optoion byte page size */

#define PFLASH_WRITE_PROTECT_BASE       (OPTION_BASE + OPTION_PAGE_SIZE + 4U)     /*!< D-Flash write protection Optoion byte base address */
#define DFLASH_WRITE_PROTECT_BASE       (OPTION_BASE + 3 * OPTION_PAGE_SIZE + 4U) /*!< D-Flash write protection Optoion byte base address */

#define EFLASH_MAIN_STAT_ERROR_BITS     (EFLASH_CSR_PWVIO_Msk  | \
                                         EFLASH_CSR_PRVIO_Msk  | \
                                         EFLASH_CSR_DWVIO_Msk  | \
                                         EFLASH_CSR_DRVIO_Msk  | \
                                         EFLASH_CSR_PGMERR_Msk | \
                                         EFLASH_CSR_ERSERR_Msk | \
                                         EFLASH_CSR_VRFERR_Msk)

#define EFLASH_OPTION_STAT_ERROR_BITS   (EFLASH_CSR_OPTRERR_Msk  | \
                                         EFLASH_CSR_OPTPWERR_Msk | \
                                         EFLASH_CSR_OPTDWERR_Msk | \
                                         EFLASH_CSR_OPTDATERR_Msk)

#define EFLASH_CLEAR_STAT_ERROR_BITS()  (EFLASH->CSR |= (\
                                         EFLASH_CSR_PWVIO_Msk | \
                                         EFLASH_CSR_PRVIO_Msk | \
                                         EFLASH_CSR_DWVIO_Msk | \
                                         EFLASH_CSR_DRVIO_Msk))

/* ===========================================  Typedef  ============================================ */
typedef enum
{
    EFLASH_CMD_IDLE = 0U,               /*!< Idle command */
    EFLASH_CMD_PAGERASE,                /*!< Page erase command */
    EFLASH_CMD_PROTECTERASE,            /*!< Protect byte page erase command */
    EFLASH_CMD_MASSRASE,                /*!< Mass erase command */
    EFLASH_CMD_BLOCKERASE,              /*!< Block erase command */
    EFLASH_CMD_PAGEPROGRAM,             /*!< Page program command */
    EFLASH_CMD_PROTECTROGRAM,           /*!< Protect byte page program command */
    EFLASH_CMD_PAGERASEVERIFY,          /*!< Page erase verify command */
    EFLASH_CMD_MASSRASEVERIFY,          /*!< Mass erase verify command */
    EFLASH_CMD_INVALID,                 /*!< Invalid command */
}
EFLASH_CmdType;                         /*!< eflash command */

typedef enum
{
    EFLASH_STATUS_SUCCESS = 1U,          /*!< Success status */
    EFLASH_STATUS_BUSY,                 /*!< Error status for busy */
    EFLASH_STATUS_TIMEOUT,              /*!< Error status for timeout */
    EFLASH_STATUS_ERROR,                /*!< other error status */
} EFLASH_StatusType;                    /*!< eflash status */

/* ====================================  Functions declaration  ===================================== */
/*!
 * @brief Unlock the eflash controller
 *
 * @param[in] none
 * @return operation status
 *        - EFLASH_STATUS_SUCCESS: operation successful
 *        - EFLASH_STATUS_TIMEOUT: operation timeout
 */
EFLASH_StatusType EFLASH_UnlockCtrl(void);

/*!
 * @brief Lock the eflash control
 *
 * @param[in] none
 * @return none
 */
void EFLASH_LockCtrl(void);

/*!
 * @brief Read a specified length of data from the eflash specified address
 *
 * @param[in] addr: eflash address
 * @param[in] buffer: Point to the data bufffer for saving the read data
 * @param[in] len: read length(the unit is word)
 * @return operation status
 *        - EFLASH_STATUS_SUCCESS: Operation was successful
 *        - EFLASH_STATUS_ERROR:   Operation was failure
 */
EFLASH_StatusType EFLASH_Read(uint32_t addr, uint32_t *buffer, uint32_t len);

/*!
 * @brief Erase specified P-Flash or D-Flash user page
 *
 * @param[in] addr: P-Flash or D-Flash page addrees to be erased (should be 4-aligned)
 * @return operation status
 *        - EFLASH_STATUS_SUCCESS:  Operation successful
 *        - EFLASH_STATUS_BUSY:     Operation was busy
 *        - EFLASH_STATUS_ERROR:    Operation was failure
 */
EFLASH_StatusType EFLASH_PageErase(uint32_t addr);

/*!
 * @brief Mass erase P-Flash user area
 *
 * @param[in] operation status
 *        - EFLASH_STATUS_SUCCESS:  Operation successful
 *        - EFLASH_STATUS_BUSY:     Operation was busy
 *        - EFLASH_STATUS_ERROR:    Operation was failure
 */
EFLASH_StatusType EFLASH_MassErase(void);

/*!
 * @brief P-Flash or D-Flash program
 *
 * @param[in] addr: eflash addrees to be programed (should be 4-aligned)
 * @param[in] buffer: Point to the data to be programed
 * @param[in] len: Data length to be programed (the unit is word)
 * @return operation status
 *        - EFLASH_STATUS_SUCCESS:  Operation successful
 *        - EFLASH_STATUS_BUSY:     Operation was busy
 *        - EFLASH_STATUS_ERROR:    Operation was failure
 */
EFLASH_StatusType EFLASH_PageProgram(uint32_t addr, uint32_t *buffer, uint32_t len);

/*!
 * @brief Verify whether the specified area is erased. not support D-Flash verify
 *
 * @param[in] addr: Specified eflash addrees to be verified (should be 4-aligned)
 * @param[in] len: Specified page erase verify length(the unit is word)
 * @return operation status
 *        - EFLASH_STATUS_SUCCESS:  Operation successful
 *        - EFLASH_STATUS_BUSY:     Operation was busy
 *        - EFLASH_STATUS_ERROR:    Operation was failure
 */
EFLASH_StatusType EFLASH_PageEraseVerify(uint32_t addr, uint32_t len);

/*!
 * @brief Verify whether all the P-Flash is erased
 *
 * @param[in] none
 * @return operation status
 *        - EFLASH_STATUS_SUCCESS:  Operation successful
 *        - EFLASH_STATUS_BUSY:     Operation was busy
 *        - EFLASH_STATUS_ERROR:    Operation was failure
 */
EFLASH_StatusType EFLASH_MassEraseVerify(void);

/*!
 * @brief Set P-Flash write protection
 *
 * @param[in] protectStatus: write protect status value, this parameter pointer
 *            to the start address of the defined space of two words
 * @return operation status
 *        - EFLASH_STATUS_SUCCESS:  Operation successful
 *        - EFLASH_STATUS_BUSY:     Operation was busy
 *        - EFLASH_STATUS_ERROR:    Operation was failure
 */
EFLASH_StatusType EFLASH_SetWriteProtection(uint32_t *protectStatus);

/*!
 * @brief Enable eflash read protection
 *
 * This API is used to set(enable) eflash read protection. Set(enable) read
 * protection will take effect after reset and the all EFLASH main memory(user
 * pages include D-flash) will return with 0x0 when read by JTAG/SWD. EFLASH main memory
 * can not be erased or programed by code/JTAG/SWD when in read protection status.
 *
 * @param[in] none
 * @return operation status
 *        - EFLASH_STATUS_SUCCESS:  Operation successful
 *        - EFLASH_STATUS_BUSY:     Operation was busy
 *        - EFLASH_STATUS_ERROR:    Operation was failure
 */
EFLASH_StatusType EFLASH_EnableReadProtection(void);

/*!
 * @brief Disable EFLASH read protection
 *
 * This API is used to clear(disable) EFLASH read protection. Clear(disable) read
 * protection will take effect after reset and the EFLASH main memory(include D-Flash)
 * will be erased to 0xFFFFFFFF automatically.
 *
 * @param[in] none
 * @return operation status
 *        - EFLASH_STATUS_SUCCESS:  Operation successful
 *        - EFLASH_STATUS_BUSY:     Operation was busy
 *        - EFLASH_STATUS_ERROR:    Operation was failure
 */
EFLASH_StatusType EFLASH_DisableReadProtection(void);

/*!
 * @brief Block erase D-Flash user area
 *
 * @param[in] addr: D-flash addrees to be erased (should be 4-aligned)
 * @return operation status
 *        - EFLASH_STATUS_SUCCESS:  Operation successful
 *        - EFLASH_STATUS_BUSY:     Operation was busy
 *        - EFLASH_STATUS_ERROR:    Operation was failure
 */
EFLASH_StatusType EFLASH_DFBlockErase(uint32_t addr);

/*!
 * @brief Mass erase D-Flash user area
 *
 * @param[in] none
 * @return operation status
 *        - EFLASH_STATUS_SUCCESS:  Operation successful
 *        - EFLASH_STATUS_BUSY:     Operation was busy
 *        - EFLASH_STATUS_ERROR:    Operation was failure
 */
EFLASH_StatusType EFLASH_DFMassErase(void);

/*!
 * @brief Verify whether the mass erase operation is performed in D-Flash successfully
 *
 * @param[in] none
 * @return operation status
 *        - EFLASH_STATUS_SUCCESS:  Operation successful
 *        - EFLASH_STATUS_BUSY:     Operation was busy
 *        - EFLASH_STATUS_ERROR:    Operation was failure
 */
EFLASH_StatusType EFLASH_DFMassEraseVerify(void);

/*!
 * @brief Set D-Flash write protection
 *
 * @param[in] protectStatus: write protect status value, this parameter pointer
 *            to the start address of the defined space of 1 words
 *
 * @return operation status
 *        - EFLASH_STATUS_SUCCESS:  Operation successful
 *        - EFLASH_STATUS_BUSY:     Operation was busy
 *        - EFLASH_STATUS_ERROR:    Operation was failure
 */
EFLASH_StatusType EFLASH_DFSetWriteProtection(uint32_t *protectStatus);

#ifdef __cplusplus
}
#endif

#endif /* _AC780X_EFLASH_H */

/* =============================================  EOF  ============================================== */
