/* 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.
 *
 *****************************************************************************/

/*-----------------------------------------------------------------------------
 *
 * $Author: brianpc.huang $
 * $Date  $
 * $RCSfile: typedef.h,v $
 * $Revision: #1 $
 *
 *---------------------------------------------------------------------------*/

#ifndef _TYPEDEF_H_
#define _TYPEDEF_H_

#include "x_typedef.h"

/*=====================================================================
  Common macro definition
=====================================================================*/
#ifndef CC_UP8032_ATV
#define CONST   const
#ifndef CODE
/* 8032  C language extenstions removal */
#define CODE    code
#define code
#define large
#define reentrant
#define REMOVE_UNCALLED_FUNCTION 0
#endif
#endif
#define PUBLIC  extern
#define PRIVATE static

#ifndef TRUE
#define TRUE  1
#endif

#ifndef FALSE
#define FALSE 0
#endif

#ifndef NULL
#define NULL  0
#endif

#define ON   1
#define OFF  0

#ifndef CC_UP8032_ATV
#define _nop_()	Error_please_remove_all_NOP_use_UtDelayXX_instead
#endif

#ifndef CC_UP8032_ATV
/*=====================================================================
  MACRO : UINT8/UINT16 access (HIGH UINT8 FIRST)
=====================================================================*/
#define bLoByte(arg)      (*(UINT8*)&arg)
#define bHiByte(arg)      (*((UINT8*)&arg + 1))

#define wLoWord(arg)      (*(UINT16*)&arg)
#define wHiWord(arg)      (*((UINT16*)&arg + 1))

#define BYTE3(arg)        (*((UINT8 *)&(arg) + 3))
#define BYTE2(arg)        (*((UINT8 *)&(arg) + 2))
#define BYTE1(arg)        (*((UINT8 *)&(arg) + 1))
#define BYTE0(arg)        (* (UINT8 *)&(arg))

#define BYTE21(arg)      (*(UINT16*)((UINT8*)&arg))

#ifndef LOADL_DWRD
#define LOADL_DWRD(arg1, arg2)  BYTE3(arg2) = *((UINT8 *)(arg1) + 3); \
                                  BYTE2(arg2) = *((UINT8 *)(arg1) + 2); \
                                  BYTE1(arg2) = *((UINT8 *)(arg1) + 1); \
                                  BYTE0(arg2) = *((UINT8 *)(arg1))
#endif
#endif
/*=====================================================================
  MACRO : Number Comparison Macro
=====================================================================*/
#ifndef MAX
#define MAX(a,b) (((a) > (b)) ? (a) : (b))
#endif

#ifndef MIN
#define MIN(a,b) (((a) < (b)) ? (a) : (b))
#endif


/*=====================================================================
  MACRO : Expand Value to Meet Special Request
=====================================================================*/
#define MAKE_EVEN(val) ((((val) + 1) >> 1) << 1)	/* to be even */
#define MAKE_ODD(val) ((val) | 0x01)	/* to be odd */

  /* Expand given value to be multiple of 8 in upper alignment */
#define MAKE_8X(val) ((((val) + 0x07) >> 3) << 3)

  /* Expand given value to be multiple of 16 in upper alignment */
#define MAKE_16X(val) ((((val) + 0x0F) >> 4) << 4)

  /* Make given value to be multiple of 16 in center alignment */
#define MAKE_16X_EX(val) ((((val) + 0x08) >> 4) << 4)

  /* Expand given value to be multiple of 4 in center alignment */
#define  MAKE_4X(val)  ((((val) + 0x01) >> 2) << 2)

#ifndef CC_UP8032_ATV
/*=====================================================================
 *  Description:
 *    Make a item position with same step
 *  Parameters:
 *    start		base start position of all items
 *    index		index of the item to be calculated
 *    step		step of all items
=====================================================================*/
#define MAKE_ITEM_POS(start, index, step) ((start) + (index) * (step))
#endif

/*=====================================================================
  MACRO : Swap Integers Macro
=====================================================================*/
#define SWAP(a, b)     {(a) ^= (b); (b) ^= (a); (a) ^= (b);}

/*=====================================================================
  MACRO : Retrieve Value from Pointer
=====================================================================*/
#define dwGetPtrVal(ptr)  (*((UINT32 *) (ptr)))	/* Get DWORD value from a pointer */
#define wGetPtrVal(ptr)   (*((UINT16 *) (ptr)))	/* Get  UINT16 value from a pointer */
#define vSetPtrValDW(ptr, dwVal)  (*((UINT32 *)ptr) = dwVal)
#define vSetPtrValW(ptr, wVal)  (*((UINT16 *)ptr) = wVal)

#ifdef CC_COPLAT_MT82
#define CRIT_STATE_T UINT8
#ifdef CC_UP8032_ATV
#define x_crit_start() irq_save()
#define x_crit_end(x) irq_restore(x)
#endif
#endif
#endif
