//<MStar Software>
//******************************************************************************
// MStar Software
// Copyright (c) 2010 - 2012 MStar Semiconductor, Inc. All rights reserved.
// All software, firmware and related documentation herein ("MStar Software") are
// intellectual property of MStar Semiconductor, Inc. ("MStar") and protected by
// law, including, but not limited to, copyright law and international treaties.
// Any use, modification, reproduction, retransmission, or republication of all
// or part of MStar Software is expressly prohibited, unless prior written
// permission has been granted by MStar.
//
// By accessing, browsing and/or using MStar Software, you acknowledge that you
// have read, understood, and agree, to be bound by below terms ("Terms") and to
// comply with all applicable laws and regulations:
//
// 1. MStar shall retain any and all right, ownership and interest to MStar
//    Software and any modification/derivatives thereof.
//    No right, ownership, or interest to MStar Software and any
//    modification/derivatives thereof is transferred to you under Terms.
//
// 2. You understand that MStar Software might include, incorporate or be
//    supplied together with third party`s software and the use of MStar
//    Software may require additional licenses from third parties.
//    Therefore, you hereby agree it is your sole responsibility to separately
//    obtain any and all third party right and license necessary for your use of
//    such third party`s software.
//
// 3. MStar Software and any modification/derivatives thereof shall be deemed as
//    MStar`s confidential information and you agree to keep MStar`s
//    confidential information in strictest confidence and not disclose to any
//    third party.
//
// 4. MStar Software is provided on an "AS IS" basis without warranties of any
//    kind. Any warranties are hereby expressly disclaimed by MStar, including
//    without limitation, any warranties of merchantability, non-infringement of
//    intellectual property rights, fitness for a particular purpose, error free
//    and in conformity with any international standard.  You agree to waive any
//    claim against MStar for any loss, damage, cost or expense that you may
//    incur related to your use of MStar Software.
//    In no event shall MStar be liable for any direct, indirect, incidental or
//    consequential damages, including without limitation, lost of profit or
//    revenues, lost or damage of data, and unauthorized system use.
//    You agree that this Section 4 shall still apply without being affected
//    even if MStar Software has been modified by MStar in accordance with your
//    request or instruction for your use, except otherwise agreed by both
//    parties in writing.
//
// 5. If requested, MStar may from time to time provide technical supports or
//    services in relation with MStar Software to you for your use of
//    MStar Software in conjunction with your or your customer`s product
//    ("Services").
//    You understand and agree that, except otherwise agreed by both parties in
//    writing, Services are provided on an "AS IS" basis and the warranty
//    disclaimer set forth in Section 4 above shall apply.
//
// 6. Nothing contained herein shall be construed as by implication, estoppels
//    or otherwise:
//    (a) conferring any license or right to use MStar name, trademark, service
//        mark, symbol or any other identification;
//    (b) obligating MStar or any of its affiliates to furnish any person,
//        including without limitation, you and your customers, any assistance
//        of any kind whatsoever, or any information; or
//    (c) conferring any license or right under any intellectual property right.
//
// 7. These terms shall be governed by and construed in accordance with the laws
//    of Taiwan, R.O.C., excluding its conflict of law rules.
//    Any and all dispute arising out hereof or related hereto shall be finally
//    settled by arbitration referred to the Chinese Arbitration Association,
//    Taipei in accordance with the ROC Arbitration Law and the Arbitration
//    Rules of the Association by three (3) arbitrators appointed in accordance
//    with the said Rules.
//    The place of arbitration shall be in Taipei, Taiwan and the language shall
//    be English.
//    The arbitration award shall be final and binding to both parties.
//
//******************************************************************************
//<MStar Software>
#ifndef _HAL_SYS_H_
#define _HAL_SYS_H_

//-------------------------------------------------------------------------------------------------
//  Macro and Define
//-------------------------------------------------------------------------------------------------
#define UNUSED( var )               (void)((var) = (var))

#define CHIPREV_U01                 0x00UL
#define CHIPREV_U02                 0x01UL

/* MISC */
#define MAX_TIMEOUT_COUNT           100UL
#define EFUSE_SHIFT                 2UL

#define HASH_KEY_BROWSER_ENGINE_BIT_OFFSET       109
#define HASH_KEY_HBBTV_FVP_BROWSER_BIT_OFFSET    110

#define SYS_EFUSE_SUB_BANK_CNT_MAX  6
#define HASH_EFUSE_DATA_FILE_AN     "/vendor/tvconfig/config/vir_hashkey.txt"
#define HASH_EFUSE_DATA_FILE_MI     "/config/vir_hashkey.txt"

//-------------------------------------------------------------------------------------------------
//  Type and Structure
//-------------------------------------------------------------------------------------------------
typedef struct
{
    MS_U32  u32ChipTopBase;
    MS_U32  u32PmTopBase;      // REG_PM_BASE

} hal_sys_base_t;

typedef enum
{
    E_HALSYS_SOURCE_DTV             = 0x00,
    E_HALSYS_SOURCE_HDMI,
    E_HALSYS_SOURCE_MM,
    E_HALSYS_SOURCE_GOOGLE_CAST,
    E_HALSYS_SOURCE_MAX,
} E_HALSYS_SOURCE_TYPE;

typedef enum
{
    E_HALSYS_INPUT_FHD24_25_30      = 0x00,
    E_HALSYS_INPUT_FHD48,
    E_HALSYS_INPUT_FHD50_60,
    E_HALSYS_INPUT_4K2K24_25_30,
    E_HALSYS_INPUT_4K2K48,
    E_HALSYS_INPUT_4K2K50_60,
    E_HALSYS_INPUT_ALWAYS_ON,
    E_HALSYS_INPUT_ALWAYS_OFF,
    E_HALSYS_INPUT_FHD_UNKNOWN,
    E_HALSYS_INPUT_4K2K120,
    E_HALSYS_INPUT_4K2K_UNKNOWN,
    E_HALSYS_INPUT_MAX,
} E_HALSYS_INPUT_TIMING;

typedef struct _SYS_EFUSE_VIRTUAL_SUB_BANK
{
    MS_U8 u8SubBank;
    MS_U32 u32Value;
} SYS_EFUSE_VIRTUAL_SUB_BANK;

//-------------------------------------------------------------------------------------------------
//  Function and Variable
//-------------------------------------------------------------------------------------------------
/* SYS INIT */
void            SYS_Init(MS_VIRT virtBase);
void            HAL_SYS_SetExtRIUBase(MS_VIRT virtBase);
void            HAL_SYS_SetOtpIOMapBase(MS_VIRT virtBase);

/* DEBUG */
MS_BOOL         HAL_SYS_DisableDebugPort(void);
MS_BOOL         HAL_SYS_EnableDebugPort(void);


/* INFO */
MS_U8           SYS_GetChipVer(void);
MS_U8           SYS_GetChipRev(void);
MS_U16          SYS_GetChipID(void);
const SYS_Info* SYS_GetInfo(void);

/* CHIP TYPE */
E_SYS_CHIP_TYPE HAL_SYS_GetChipType(void);
void            HAL_SYS_SetChipType(E_SYS_CHIP_TYPE eType);

/* RESET */
void            HAL_SYS_ResetChip(void);
void            SYS_ResetCPU(void);

/* WOL */
void            HAL_SYS_EnableWkEventWOL(void);
void            HAL_SYS_DisableWkEventWOL(void);
void            HAL_SYS_ResetStatusWOL(void);
MS_BOOL         HAL_SYS_GetStatusWOL(void);

/* PAD MUX TABLE */
MS_BOOL         HAL_SYS_SetPadMux(SYS_PAD_MUX_SET ePadMuxType,SYS_PAD_SEL ePadSel);
MS_BOOL         HAL_SYS_PadMuxTableSuspend(void);
MS_BOOL         HAL_SYS_PadMuxTableResume(void);

/* TS CLOCK */
MS_BOOL         HAL_SYS_SetTSOutClockPhase(MS_U16 u16Val);
MS_BOOL         HAL_SYS_SetTSClockPhase(SYS_PAD_MUX_SET ePadMuxType,MS_U16 u16Val);

/* DOLBY */
MS_U32          HAL_SYS_QueryDolbyHashInfo(E_SYS_DOLBY_HASH_INFO index);
void            HAL_SYS_GetDolbyKeyCustomer(MS_U8 *pu8key);

/* BRICK TERMINATOR */
MS_U16          HAL_SYS_ReadBrickTerminatorStatus(void);
void            HAL_SYS_WriteBrickTerminatorStatus(MS_U16 u16Status);

/* EFUSE */
void            HAL_SYS_SetEfuseIOMapBase(MS_VIRT virtBase);
MS_U16          HAL_SYS_EfuseRead2Byte(MS_U32 u32RegAddr);
MS_U16          HAL_SYS_EfuseWrite2Byte(MS_U32 u32RegAddr, MS_U16 u16Val);
void            HAL_SYS_GetEfuseDid(MS_U16 *u16efuse_did);
MS_BOOL         HAL_SYS_ReadEfuseHDCPKey(MS_U16 u16ReadAddr, MS_U32 *u32HDCPKey);
E_SYS_PACKAGE_TYPE HAL_SYS_GetPackageType(void);

/* MISC */
MS_U32          HAL_SYS_GetMemcConfg(MS_U32 eSource, MS_U32 eTiming, MS_BOOL *retEnMemc);
MS_U32          HAL_SYS_GetMemcLevel(MS_U32 eSource, MS_U32 eTiming, MS_U32 *retSt);
MS_U32          HAL_SYS_GetXcByPartConfg(MS_U32 eSource, MS_U32 eInputTiming, MS_U32 eOutputTiming, MS_BOOL *retEn);
void            HAL_SYS_RFAGC_Tristate(MS_BOOL bEnable);
void            HAL_SYS_IFAGC_Tristate(MS_BOOL bEnable);
void            HAL_SYS_SetAGCPadMux(SYS_AGC_PAD_SET eAgcPadMux);
MS_BOOL         HAL_SYS_SetPCMCardDetectMode(SYS_PCM_CD_MODE ePCMCDMode);
MS_BOOL         HAL_SYS_Query(E_SYS_QUERY id);
MS_BOOL         HAL_SYS_VirtualInit(void);
MS_BOOL         HAL_SYS_VirtualQuery(E_SYS_QUERY id);
MS_U32          HAL_SYS_ReadRSAKey(MS_U16 u16ReadAddr);
#endif // _HAL_SYS_H_
