/* SPDX-License-Identifier: LGPL-2.1-only OR BSD-3-Clause */
/******************************************************************************
 *
 * This file is provided under a dual license.  When you use or
 * distribute this software, you may choose to be licensed under
 * version 2.1 of the GNU Lesser General Public License
 * ("LGPLv2.1 License") or BSD License.
 *
 * LGPLv2.1 License
 *
 * Copyright(C) 2019 MediaTek Inc.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of version 2.1 of the GNU Lesser General Public
 * License as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * See https://www.gnu.org/licenses/old-licenses/lgpl-2.1.html for more
 * details.
 *
 * BSD LICENSE
 *
 * Copyright(C) 2019 MediaTek Inc.
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 *  * Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 *  * Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in
 *    the documentation and/or other materials provided with the
 *    distribution.
 *  * Neither the name of the copyright holder nor the names of its
 *    contributors may be used to endorse or promote products derived
 *    from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 *****************************************************************************/

#ifndef __X_UTIL_H__
#define __X_UTIL_H__

#include "x_typedef.h"

#ifndef CC_MT5301A
#define NEW_COUNTRY_TYPE
#endif


#ifdef NEW_COUNTRY_TYPE

#define COUNTRY_AUDIO_POS    5
#define COUNTRY_VBI_POS     4 
#define COUNTRY_ATV_POS    3
#define COUNTRY_DTV_POS    0

#define COUNTRY_AUDIO_MASK      ((UINT32)(7 << COUNTRY_AUDIO_POS))
#define COUNTRY_VBI_MASK  ((UINT32)(1 << COUNTRY_VBI_POS))
#define COUNTRY_ATV_MASK   ((UINT32)(1 << COUNTRY_ATV_POS))
#define COUNTRY_DTV_MASK   ((UINT32)(7 << COUNTRY_DTV_POS))


typedef enum {
    COUNTRY_AUD_US = 0,
    COUNTRY_AUD_EU,
    COUNTRY_AUD_BZ,
    COUNTRY_AUD_AU,
    COUNTRY_AUD_CN,
    COUNTRY_AUD_JP,
    COUNTRY_AUD_TW,
    COUNTRY_AUD_AP
} COUNTRY_AUD;

typedef enum {
    COUNTRY_VBI_CC = 0,
    COUNTRY_VBI_TELETEXT
} COUNTRY_VBI; 

typedef enum {
    COUNTRY_ATV_US = 0,
    COUNTRY_ATV_EU
} COUNTRY_ATV;

typedef enum {
    COUNTRY_DTV_ATSC = 0,
    COUNTRY_DTV_DVBT,
    COUNTRY_DTV_DTMB,
    COUNTRY_DTV_ISDBT,
    COUNTRY_DTV_DUMMY
} COUNTRY_DTV;

#define MAKE_TARGET_COUNTRY(aud,vbi,atv,dtv) ((aud<<COUNTRY_AUDIO_POS) |(vbi<<COUNTRY_VBI_POS)|(atv<<COUNTRY_ATV_POS)|(dtv<<COUNTRY_DTV_POS)) 

#define COUNTRY_US  MAKE_TARGET_COUNTRY(COUNTRY_AUD_US, COUNTRY_VBI_CC, COUNTRY_ATV_US, COUNTRY_DTV_ATSC)
#define COUNTRY_EU  MAKE_TARGET_COUNTRY(COUNTRY_AUD_EU, COUNTRY_VBI_TELETEXT, COUNTRY_ATV_EU, COUNTRY_DTV_DVBT)
#define COUNTRY_JP  MAKE_TARGET_COUNTRY(COUNTRY_AUD_JP, COUNTRY_VBI_CC, COUNTRY_ATV_US, COUNTRY_DTV_ISDBT)
#define COUNTRY_TW  MAKE_TARGET_COUNTRY(COUNTRY_AUD_TW, COUNTRY_VBI_CC, COUNTRY_ATV_US, COUNTRY_DTV_DVBT)
#define COUNTRY_BZ  MAKE_TARGET_COUNTRY(COUNTRY_AUD_BZ, COUNTRY_VBI_CC, COUNTRY_ATV_US, COUNTRY_DTV_ATSC)
#define COUNTRY_PAA MAKE_TARGET_COUNTRY(COUNTRY_AUD_AP, COUNTRY_VBI_TELETEXT, COUNTRY_ATV_EU, COUNTRY_DTV_DUMMY)
#define COUNTRY_PAD MAKE_TARGET_COUNTRY(COUNTRY_AUD_EU, COUNTRY_VBI_TELETEXT, COUNTRY_ATV_EU, COUNTRY_DTV_DVBT)

#else

#define SCAN_ATV_POS    3
#define SCAN_DTV_POS    0

#define COUNTRY_CC_ENABLE_MASK  (UINT32)(1 << 7)
#define COUNTRY_AUDIO_MASK      (UINT32)(3 << 5)
#define COUNTRY_SCAN_ATV_MASK   (UINT32)(3 << SCAN_ATV_POS)
#define COUNTRY_SCAN_DTV_MASK   (UINT32)(7 << SCAN_DTV_POS)

typedef enum {
    COUNTRY_US = 0,
    COUNTRY_EU,
    COUNTRY_DUMMY,
    COUNTRY_JP,
    COUNTRY_TW,    
    COUNTRY_BZ,    
} TARGET_COUNTRY;
#endif


typedef enum {
    eNO_TYPE,
    eRD_ONLY,
    eWR_ONLY,
    eRD_WR
} REGTYPE;


typedef struct strucRegTest {
    UINT32     u4Addr;		// the register offset
    REGTYPE    eRegType;
    INT32      iRegLen;		// 1, 2, or 4; number of byte
	UINT32     u4Mask;		// the valid bit mask
	INT32      fgWDfV;		// set 0 if without reset value.
	UINT32     u4DfVal;		// the reset value.
} REG_TEST_T;

/* Log export function. */
extern INT32 UTIL_LogThreadInit(void);
extern INT32 UTIL_Log(const CHAR *szFmt, ...);
extern void UTIL_LogFlush(void);

/* Cli util functions. */
extern INT32 UTIL_MemOrder(UINT32 u4Addr, UINT32 u4ByteUnit, UINT32 u4Len, UINT32 u4Step);
extern INT32 UTIL_MemCmp(UINT32 u4Addr1, UINT32 u4Addr2, UINT32 u4Len);

/* Reg test functions. */
INT32 UTIL_AllSpaceRWTest(UINT32 u4BaseAddr, UINT32 u4Length);
INT32 UTIL_RegDefChk(UINT32 u4BaseAddr, REG_TEST_T *psrgtRegList);
INT32 UTIL_RegRWTest(UINT32 u4BaseAddr, REG_TEST_T *psrgtRegList);

#ifndef CC_MTK_LOADER
/*
 * Do u8Dividend / u8Divider.
 * u8Divider value should be < max 32 bit integer number.
 * If pu8Remainder is not NULL, remainder will be returned.
 * The function returns quotient.
 */
extern UINT64 u8Div6432(UINT64 u8Dividend, UINT64 u8Divider, UINT64 *pu8Remainder);
extern UINT64 u8Div6464(UINT64 u8Dividend, UINT64 u8Divider, UINT64 *pu8Remainder);
#endif

/**
 * Set country.
 *
 * @param u4Country country selection
 * @return void
 */
void SetTargetCountry(UINT32 u4Country);

/**
 * Get country setting.
 *
 * @param void
 * @return country selection
 */
UINT32 GetTargetCountry(void);

/**
 * Set HID KB country.
 *
 * @param u4Country country selection
 * @return void
 */
void SetHidKbCountry(UINT32 u4Country);

/**
 * Get HID KB country setting.
 *
 * @param void
 * @return country selection
 */
UINT32 GetHidKbCountry(void);
#ifdef NEW_COUNTRY_TYPE
/**
 * Get country setting of sub-module.
 *
 * @param u4Mask sub-module mask
 * @return country selection
 */
UINT32 GetTargetCountryMask(UINT32 u4Mask);
#endif

#ifndef CC_MTK_LOADER
EXTERN UINT32 u4Logcount_CLI;
EXTERN UINT32 u4Logcount_GFX ;  
EXTERN UINT32 u4Logcount_GFXSC ; 
EXTERN UINT32 u4Logcount_IMGRZ ;
EXTERN UINT32 u4Logcount_OSD ; 

EXTERN UINT32 u4Logcount_PMX ;
EXTERN UINT32 u4Logcount_VDP ;
EXTERN UINT32 u4Logcount_PRESCALE ;
EXTERN UINT32 u4Logcount_TVE ;
EXTERN UINT32 u4Logcount_NET ; 

EXTERN UINT32 u4Logcount_TVD ;
EXTERN UINT32 u4Logcount_MPV ;
EXTERN UINT32 u4Logcount_AUD ;
EXTERN UINT32 u4Logcount_SMC ; 
EXTERN UINT32 u4Logcount_IDE ; 

EXTERN UINT32 u4Logcount_DMX ; 
EXTERN UINT32 u4Logcount_PSR ;
EXTERN UINT32 u4Logcount_UART ; 
EXTERN UINT32 u4Logcount_FCI ; 
EXTERN UINT32 u4Logcount_IR ; 

EXTERN UINT32 u4Logcount_NAND ; 
EXTERN UINT32 u4Logcount_MSDC ;  
EXTERN UINT32 u4Logcount_NOR ; 
EXTERN UINT32 u4Logcount_FBM ;
EXTERN UINT32 u4Logcount_VIN ;

EXTERN UINT32 u4Logcount_SIF ; 
EXTERN UINT32 u4Logcount_PWM ;
EXTERN UINT32 u4Logcount_GPIO ;  
EXTERN UINT32 u4Logcount_EEPROM ;
EXTERN UINT32 u4Logcount_FAT ; 

EXTERN UINT32 u4Logcount_FEEDER ; 
EXTERN UINT32 u4Logcount_MID ;
EXTERN UINT32 u4Logcount_PVR ;
EXTERN UINT32 u4Logcount_PCMCIA ;
EXTERN UINT32 u4Logcount_DV ;

EXTERN UINT32 u4Logcount_DVMGR ;
EXTERN UINT32 u4Logcount_NPTV ;
EXTERN UINT32 u4Logcount_MJC ;
EXTERN UINT32 u4Logcount_I1394 ;
EXTERN UINT32 u4Logcount_MD ;

EXTERN UINT32 u4Logcount_BWT ;    
EXTERN UINT32 u4Logcount_MUSB ;
EXTERN UINT32 u4Logcount_RTC ;
EXTERN UINT32 u4Logcount_PDWNC ;
EXTERN UINT32 u4Logcount_NAV ; 

EXTERN UINT32 u4Logcount_PSIPSR ; 
EXTERN UINT32 u4Logcount_BLKMGR ;
EXTERN UINT32 u4Logcount_JPG ;
EXTERN UINT32 u4Logcount_PNG ; 
EXTERN UINT32 u4Logcount_WEBP ;

EXTERN UINT32 u4Logcount_SWDMX ;
EXTERN UINT32 u4Logcount_PLAYMGR ;
EXTERN UINT32 u4Logcount_EXTMJC ;
EXTERN UINT32 u4Logcount_GDMA ; 
EXTERN UINT32 u4Logcount_FVR ;

EXTERN UINT32 u4Logcount_VDEC ; 
EXTERN UINT32 u4Logcount_VOMX ;
EXTERN UINT32 u4Logcount_VENC ;
EXTERN UINT32 u4Logcount_MUXER ;
EXTERN UINT32 u4Logcount_NIM ; 

EXTERN UINT32 u4Logcount_LINUX ; 
EXTERN UINT32 u4Logcount_MEMTEST ;
EXTERN UINT32 u4Logcount_MUTE ; 
EXTERN UINT32 u4Logcount_SCPOS ;
EXTERN UINT32 u4Logcount_NR ;

EXTERN UINT32 u4Logcount_DI ;
EXTERN UINT32 u4Logcount_TECHNI ;
EXTERN UINT32 u4Logcount_OS ;
EXTERN UINT32 u4Logcount_EXTRA ;
EXTERN UINT32 u4Logcount_CEC ; 
EXTERN UINT32 u4Logcount_CBUS ; 

EXTERN UINT32 u4Logcount_HDMI ;
EXTERN UINT32 u4Logcount_DVI ;
EXTERN UINT32 u4Logcount_MHL ;
EXTERN UINT32 u4Logcount_GCPU ;
EXTERN UINT32 u4Logcount_CUST1 ;

EXTERN UINT32 u4Logcount_CUST2 ; 
EXTERN UINT32 u4Logcount_Snand ;
EXTERN UINT32 u4Logcount_IRT;
EXTERN UINT32 u4Logcount_MTAL;
EXTERN UINT32 u4Logcount_SELF;
#endif

#endif /* __X_UTIL_H__ */

