/* 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: dtvbm11 $
 * $Date: 2015/11/10 $
 * $RCSfile: x_mid.h,v $
 * $Revision: #1 $
 *
 *---------------------------------------------------------------------------*/

/** @file x_mid.h
 *  x_mid.h Memory intrusion detection module
 */

#ifndef X_MID_H
#define X_MID_H

#include "x_typedef.h"
#include "x_hal_5381.h"

//=====================================================================
// Constant definitions
#define MID_MAX_REGIONS                 (5)

#define MID_ADDR_ALIGN_SHIFT            2
#define MID_ADDR_ALIGNMENT              (1 << MID_ADDR_ALIGN_SHIFT)
#define GROUP2_FIRST_AGENT_ID           16
#define GROUP3_FIRST_AGENT_ID           23

#define MID_BASE				0x200
#define REGION					0x10

#define UPADDR					0x0
#define LOWADDR					0x4
#define AGENT					0x8
#define EN_MODE_RWPRT					0xC

#define MID_ENABLE				(((UINT32)1) << 31)
#define MID_MODE				(((UINT32)1) << 30)
#define MID_WRITEABLE			(((UINT32)1) << 29)
#define MID_READPROTECT			(((UINT32)1) << 28)

#define MID_GROUP2_SHIFT		(16)
#define MID_GROUP3_SHIFT		(24)

#define MID_CLEAR_STATUS		0x70
#define MID_CLEAR				(1)

#define MID_INTRUD_INFO			0x80
#define MID_INTRUD_INFO_OFFSET	0x4
#define MID_INTRUD_DETECTED		(((UINT32)1) << 31)

#define MID_INTRUD_AGENT_INFO	0xA0
#define MID_INTRUD_AGENT_INFO_OFFSET  0x4

//=====================================================================
// Type definitions
typedef enum
{
    // Group 1
    MID_AGENT_AUDIO = 0,
    MID_AGENT_DEMUX = 1,
    MID_AGENT_VDOIN = 2,
    MID_AGENT_OSD = 3,
    MID_AGENT_PSCAN = 4,
    MID_AGENT_B2R = 5,
    MID_AGENT_CPU = 6,
    MID_AGENT_SCPOS = 7,
    MID_AGENT_VDECMC = 8,
    MID_AGENT_VLD = 9,
    MID_AGENT_3DGFX = 10,
    MID_AGENT_JPEG = 11,
    MID_AGENT_VENC = 12,
    MID_AGENT_MJCIN = 13,
    MID_AGENT_MJCOUT = 14,
    MID_AGENT_TEST0 = 15,

    // Group 2
    MID_AGENT_USB = GROUP2_FIRST_AGENT_ID,
    MID_AGENT_ETHERNET = 17,
    MID_AGENT_DDI = 18,   
    MID_AGENT_USB3 = 19,        
    MID_AGENT_DSP0 = 20,
    MID_AGENT_MSDC = 21, 
    MID_AGENT_TEST1 = 22,
    MID_AGENT_GDMA = 30,
    //MID_AGENT_FCI = 21,
    //MID_AGENT_PCIE = 19,
    //MID_AGENT_GDMA_BIT = 23, 
    //MID_AGENT_NONE2 = 29,        // None, for read-only region
    //MID_AGENT_NONE = 31,        // None, for read-only region

    // Group 3
    MID_AGENT_GFXLO = GROUP3_FIRST_AGENT_ID,
    MID_AGENT_TCM = 24,
    MID_AGENT_ETHERNET_CHECKSUM = 25,
    MID_AGENT_RS232 = 26,
    MID_AGENT_SPIS  = 27,
    MID_AGENT_NONE2 = 28,        // None, for read-only region
    MID_AGENT_TEST2 = 29,
	MID_AGENT_NONE = 31,        // None, for read-only region

	MID_AGENT_GDMA_BIT = 23,
    MID_AGENT_GFXLO_BIT = 24,
    MID_AGENT_TCM_BIT = 25,
    MID_AGENT_ETHERNET_CHECKSUM_BIT = 26,
    MID_AGENT_RS232_BIT = 27,
    MID_AGENT_SPIS_BIT = 28,
    MID_AGENT_NONE2_BIT = 29,
    MID_AGENT_TEST2_BIT = 30,

    // Special IDs
    MID_AGENT_MAX = 32,
    MID_AGENT_INVALID = 33
} MID_AGENT_ID_T;

#define MID_FLAG_INVERSE    (1U << 0)
#define MID_FLAG_CPUACCESS  (1U << 1)
#define MID_FLAG_WRITEABLE  (1U << 2)
#define MID_FLAG_PROTRD     (1U << 3)
#define MID_FLAG_PROTWR     (1U << 4)

typedef void (*PFN_MID_CALLBACK)(UINT32 u4Region, MID_AGENT_ID_T eAgentId,
    UINT32 u4Addr);

#if defined(CC_MT5890)
struct AGENT_PRIORITY
{
    UINT32 u4prityL;                //priority agent0~7  group 1
	UINT32 u4prityH;                //priority agent8~15  group 1
	UINT32 u4prityjumpL;			//priority jump mode agent0~7  group 1
	UINT32 u4prityjumpH;			//priority jump mode agent8~15  group 1
};

#endif

//=====================================================================
// Porting issue. Define.
#define MID_AGENT_MPEG1 MID_AGENT_VDECMC

//=====================================================================
// Function declarations

EXTERN BOOL MID_Init(void);

EXTERN BOOL MID_Exit(void);

EXTERN BOOL MID_Reset(void);

EXTERN VOID MID_ResetStatus(UINT32 u4Vector);

EXTERN UINT32 MID_QueryIntrusionStatus(void);

EXTERN VOID MID_SetDRAMChannel(BOOL fgChannelA);

EXTERN VOID MID_SetDRAMChannelC(void);

EXTERN PFN_MID_CALLBACK MID_RegisterHandler(PFN_MID_CALLBACK pfnHandler);

EXTERN BOOL MID_SetRegion(UINT32 u4Region, MID_AGENT_ID_T eAllowedAgentId,
    UINT32 u4LowAddr, UINT32 u4HighAddr);

EXTERN BOOL MID_SetRegionEx(UINT32 u4Region, MID_AGENT_ID_T eAllowedAgentId,
    UINT32 u4LowAddr, UINT32 u4HighAddr, UINT32 u4Flags);

EXTERN BOOL MID_SetRegionEx2(UINT32 u4Region, MID_AGENT_ID_T eAllowedAgentId,
    UINT32 u4LowAddr, UINT32 u4HighAddr, UINT32 u4Flags, UINT32 u4AgentList);

EXTERN BOOL MID_ResetRegion(UINT32 u4Region);

EXTERN BOOL MID_AllocateFreeRegion(BOOL fgReadOnly, UINT32* pu4Region);

EXTERN BOOL MID_EnableRegionProtect(UINT32 u4Region);

EXTERN BOOL MID_DisableRegionProtect(UINT32 u4Region);

EXTERN void MID_ShowAgentList(void);

EXTERN const CHAR* MID_AgentIdToString(MID_AGENT_ID_T eAgentId);

EXTERN MID_AGENT_ID_T MID_StringToAgentId(const CHAR* szAgent);

EXTERN UINT32 MID_WarmBootRegionProtect(UINT32 onoff);
EXTERN UINT32 MID_ReturnIntrusionStatus(void);

#if defined(CC_MT5890)
EXTERN void DMARB_Dynamic_Priority_Enable(BOOL u4Enable);
EXTERN void DMARB_Dynamic_Priority_Init(void);
EXTERN UINT32 DMARB_Dynamic_Priority_Set(struct AGENT_PRIORITY u4priority);
EXTERN UINT32 DMARB_Dynamic_Priority_Reset(void);
#endif 

/*
EXTERN UINT32 MID_CntBandWidthSet(UINT32 fgEnable, const CHAR *szAgentName,
                UINT32 u4Clk);

EXTERN UINT32 MID_CntBandWidthGet(UINT32 u4GrpId, CHAR *szAgentName,
                UINT32 *pu4AgentId, UINT32 *pu4Enable);
*/                
//---------------------------------------------------------------------

#endif  // X_MID_H

