#ifndef _EMMC50_TUNING_H_
#define _EMMC50_TUNING_H_
#include "x_lint.h"
#include "x_os.h"
#include "x_assert.h"
#include "x_printf.h"
#include "x_typedef.h"
#include "x_ldr_env.h"

#include "drvcust_if.h"
#include "msdc_drv.h"

#define AUTOK_TUNING_INACCURACY         (3)  /*scan result may find xxxxooxxx */
#define AUTOK_MARGIN_THOLD  (5)
#define AUTOK_BD_WIDTH_REF  (3)
#define AUTOK_DATA_PIN_SEPARATE_TUNE_LEVEL 15

#define AUTOK_READ 0
#define AUTOK_WRITE 1

#define AUTOK_FINAL_CKGEN_SEL        (0)
#define SCALE_TA_CNTR                (8)
#define SCALE_CMD_RSP_TA_CNTR        (8)
#define SCALE_WDAT_CRC_TA_CNTR       (8)
#define SCALE_INT_DAT_LATCH_CK_SEL   (8)
#define SCALE_INTERNAL_DLY_CNTR      (32)
#define SCALE_PAD_DAT_DLY_CNTR       (32)

#define TUNING_INACCURACY (2)

#define AUTOK_CKGEN_VALUE                       (0)
#if defined(CC_MT5893) || defined(CC_MT5895)
#define AUTOK_CMD_LATCH_EN_HS400_VALUE          (0) //designer suggest to use this for husky
#define AUTOK_CMD_LATCH_EN_NON_HS400_VALUE      (1)
#define AUTOK_CRC_LATCH_EN_HS400_VALUE          (0)
#define AUTOK_CRC_LATCH_EN_NON_HS400_VALUE      (1)
#else
#define AUTOK_CMD_LATCH_EN_HS400_VALUE          (4)
#define AUTOK_CMD_LATCH_EN_NON_HS400_VALUE      (4)
#define AUTOK_CRC_LATCH_EN_HS400_VALUE          (2)
#define AUTOK_CRC_LATCH_EN_NON_HS400_VALUE      (2)
#endif

#define AUTOK_LATCH_CK_VALUE                    (1)
#if defined(CC_MT5893) || defined(CC_MT5895)
#define AUTOK_CMD_TX_VALUE                      (6-6) //for HQA
#define AUTOK_CRC_TA_VALUE                      (0)
#define AUTOK_CMD_TA_VALUE                      (0)
#else
#define AUTOK_CMD_TX_VALUE                      (0)
#define AUTOK_CRC_TA_VALUE                      (1)
#define AUTOK_CMD_TA_VALUE                      (1)
#endif
#define AUTOK_DAT_TX_VALUE                      (0)
#if defined(CC_MT5886) || defined(CC_MT5863) || \
	defined(CC_MT5893) || defined(CC_MT5865) || \
	defined(CC_MT5895)
#define AUTOK_CRC_MA_VALUE                      (0)
#else
#define AUTOK_CRC_MA_VALUE                      (1)
#endif
#define AUTOK_BUSY_MA_VALUE                     (1)


#define MMC_READ_SINGLE_BLOCK    17   /* adtc [31:0] data addr   R1  */
#define MMC_SEND_TUNING_BLOCK_HS200	21	/* adtc R1  */
#define MMC_SEND_TUNING_BLOCK    19   /* adtc                    R1  */


#define E_RESULT_PASS     (0)
#define E_RESULT_ERR      (1<<5)
#define E_RESULT_START    (1<<6)
#define E_RESULT_PW_SMALL (1<<7)
#define E_RESULT_KEEP_OLD (1<<8)
#define E_RESULT_CMP_ERR  (1<<9)
#define E_RESULT_FATAL_ERR  (1<<10)

#define E_RESULT_MAX

#ifndef NULL
    #define NULL                0
#endif  // NULL
#ifndef TRUE
    #define TRUE                (0 == 0)
#endif  // TRUE
#ifndef FALSE
    #define FALSE               (0 != 0)
#endif  // FALSE

#define AUTOK_DBG_OFF                             0
#define AUTOK_DBG_ERROR                           1
#define AUTOK_DBG_RES                             2
#define AUTOK_DBG_WARN                            3
#define AUTOK_DBG_TRACE                           4
#define AUTOK_DBG_LOUD                            5

extern UINT32 autok_debug_level;

#define AUTOK_DBGPRINT(_level, _fmt ...)        \
({                                               \
    if (autok_debug_level >= _level){           \
            Printf(_fmt);                        \
    }                                           \
})

/**********************************************************
 *                          Feature  Control Defination                                  *
 **********************************************************/
#define AUTOK_OFFLINE_TUNE_ENABLE 0
#define HS400_OFFLINE_TUNE_ENABLE 0
#define HS200_OFFLINE_TUNE_ENABLE 0
#define HS400_DSCLK_NEED_TUNING 1
#define AUTOK_PARAM_DUMP_ENABLE 0
#define AUTOK_OFFLINE_TUNE_TX_ENABLE  0
#define HS400_DDLSEL_SEPARATE_TUNING 1
#define HS400_REDUCE_DS_TUNE_TIME    1

/* #define CHIP_DENALI_3_DAT_TUNE */

/**********************************************************
 *                          Function Declaration                                         *
 **********************************************************/
extern VOID autok_tuning_parameter_init(msdc_cmd* host, unsigned char* res);
extern INT32 sdio_execute_tuning(msdc_cmd *host, unsigned char* res);
extern INT32 hs200_execute_tuning(msdc_cmd *host, unsigned char* res);
extern INT32 hs400_execute_tuning(msdc_cmd *host, unsigned char* res);
extern INT32 execute_online_tuning_HsMode(msdc_cmd* host, unsigned char* res);
extern INT32 execute_online_tuning_hs200(msdc_cmd* host, unsigned char* res);
extern INT32 execute_online_tuning_hs400(msdc_cmd* host, unsigned char* res);

#endif

