/* 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: 2015/05/29 $
 * $RCSfile: nor_if.h,v $
 * $Revision: #1 $
 *
 *---------------------------------------------------------------------------*/

/** @file nor_if.h
 *  nor_if.h The NOR flash module export types/functions
 */


#ifndef NOR_IF_H
#define NOR_IF_H
#if (defined(CC_NAND_ENABLE) || defined(CC_MSDC_ENABLE)) && (!defined(__MODEL_slt__))
#ifndef CC_NOR_DISABLE
#define CC_NOR_DISABLE
#endif
#endif

#ifndef CC_NOR_DISABLE

//---------------------------------------------------------------------------
// Include files
//---------------------------------------------------------------------------
//#define BD_NOR_ENABLE

#include "x_lint.h"

LINT_EXT_HEADER_BEGIN
#include "x_typedef.h"
#include "x_os.h"
#include "x_assert.h"
#include "x_printf.h"
LINT_EXT_HEADER_END
#ifndef BD_NOR_ENABLE
#include "storage_if.h"
#else
typedef struct __rNorSegInfo {
	UINT32 u4SectNum;
	UINT32 u4SectSize;
} SEGINFO_T;

typedef struct __rNorSectTbl {
	UINT32 u4SegNum;
	SEGINFO_T *prSegInfo;
} SECTTBL_T;
#define DEFINE_IS_LOG	CLI_IsLog

#endif

;

//---------------------------------------------------------------------------
// Type definitions
//---------------------------------------------------------------------------
#define MAX_FLASHCOUNT  1
#define SFLASHNAME_LEN  48

#define NORPART_DYNAMIC_TYPE   (0)  // with wear leveling algorithm protection.
#define NORPART_STATIC_TYPE       (1) // raw data access device without wear leveling.
#define NORPART_UNUSED_TYPE      (0xFF) // unused partition.

#ifndef NORPART_PARTIAL_PROTECTION_ID
   #define NORPART_PARTIAL_PROTECTION_ID (2)
#endif
#define NORPART_PROTECTION_ENABLE TRUE
#define NORPART_PROTECTION_DISENABLE FALSE

#if 0 // move to storage_if.h
typedef struct __rNorSegInfo
{
    UINT32 u4SectNum;
    UINT32 u4SectSize;
} NORSEGINFO_T;

typedef struct __rNorSectTbl
{
    UINT32 u4SegNum;
    NORSEGINFO_T *prSegInfo;
} SECTTBL_T;
#endif
typedef struct
{
    UINT8   u1MenuID;
    UINT8   u1DevID1;
    UINT8   u1DevID2;
    UINT32  u4ChipSize;
    UINT32  u4SecSize;
    UINT32  u4SecCount;

    CHAR    pcFlashStr[SFLASHNAME_LEN];
} SFLASH_CHIPINFO_T;

typedef struct
{
    UINT8   u1FlashCount;
    SFLASH_CHIPINFO_T arFlashInfo[MAX_FLASHCOUNT];
} SFLASH_INFO_T;




//-----------------------------------------------------------------------------
//  Public functions
//-----------------------------------------------------------------------------

/* Implement at nor_part.c */
EXTERN INT32 NORPART_IoCtl(UINT32 u4IdType, UINT64 u8Data);
EXTERN UINT32 NORPART_PartStartOffset(UINT32 u4Pid);
EXTERN INT32 NORPART_GetMWPartNum(void);
EXTERN INT32 NORPART_GetDRVPartNum(void);
EXTERN UINT32 NORPART_GetPartSectorNum(UINT32 u4Pid);
EXTERN UINT32 NORPART_GetPartSize(UINT32 u4Pid);
EXTERN INT32 NORPART_GetSectorTbl(SECTTBL_T *pt_sector_tbl, UINT32 u4Pid);

// the following functions are used in storage.c.
EXTERN INT32 NORPART_Read(UINT64 u8Offset, UPTR u4MemPtr, UINT32 u4MemLen);
EXTERN INT32 NORPART_Write(UINT64 u8Offset, UPTR u4MemPtr, UINT32 u4MemLen);
EXTERN INT32 NORPART_Erase(UINT64 u8Offset, UINT32 u4SectNum);

// the following functions send requests to storage.c.
EXTERN INT32 NORSTG_Read(UINT32 u4Pid, UINT32 u4Offset, UPTR u4MemPtr, UINT32 u4MemLen);
EXTERN INT32 NORSTG_Write(UINT32 u4Pid, UINT32 u4Offset, UPTR u4MemPtr, UINT32 u4MemLen);
EXTERN INT32 NORSTG_Erase(UINT32 u4Pid);

// the following functions are used in nor_if.c
#ifdef REPLACE_EEPROM_WITH_NOR

EXTERN INT32 Get_NOR_EEPROM_INFO(VOID);
#endif

// the following functions are used in Eeprom_hw.c
#ifdef REPLACE_EEPROM_WITH_NOR
//NOR replace of EERPORM
EXTERN INT32 NOR_EEPROM_Read(UINT32 u4Offset, UPTR u4MemPtr, UINT32 u4MemLen);

EXTERN INT32 NOR_EEPROM_Write(UINT32 u4Offset, UPTR u4MemPtr, UINT32 u4MemLen);
#endif
/* Implement at nor_if.c */
EXTERN INT32 NOR_Init(void);
#ifdef REPLACE_EEPROM_WITH_NOR
EXTERN void SFLASHHW_GetFlashInfo(SFLASH_INFO_T *prInfo);

EXTERN INT32 NOR_GetInfo(SFLASH_INFO_T *prNOR);
#endif

EXTERN INT32 NOR_Read(UINT64 u8Offset, UPTR u4MemPtr, UINT32 u4MemLen);
EXTERN INT32 NOR_ReadProgram(UINT64 u8Offset, UPTR u4MemPtr, UINT32 u4MemLen);
EXTERN INT32 NOR_ReadRISCProgram(UINT64 u8Offset, UPTR u4MemPtr, UINT32 u4MemLen);
EXTERN INT32 NOR_Queue(void);

EXTERN INT32 NOR_Write(UINT64 u8Offset, UPTR u4MemPtr, UINT32 u4MemLen);
EXTERN INT32 NOR_WriteProgram(UINT64 u8Offset, UPTR u4MemPtr, UINT32 u4MemLen);
EXTERN INT32 NOR_WriteRISCProgram(UINT64 u8Offset, UPTR u4MemPtr, UINT32 u4MemLen);
EXTERN INT32 NOR_PageProgramWrite(UINT64 u8Offset, UPTR u4MemPtr, UINT32 u4MemLen);
EXTERN INT32 NOR_PageProgramCountWrite(UINT64 u8Offset, UPTR u4MemPtr, UINT32 u4MemLen, UINT8 u1PageCount);
EXTERN INT32 NOR_WriteByte(UINT32 u4Offset, UINT32 u4Data);
EXTERN INT32 NOR_WrNoE(UINT32 u4Offset, UPTR u4MemPtr, UINT32 u4MemLen);

EXTERN INT32 NOR_EraseChip(UINT32 u4FlashIndex);
EXTERN INT32 NOR_Erase(UINT32 u4SectIdx, UINT32 u4SectNum);
EXTERN INT32 NOR_EraseAddr(UINT32 u4Offset, UINT32 u4ByteCount);
EXTERN INT32 NOR_EraseBlk(UINT32 u4Offset);

EXTERN INT32 NOR_GetSize(void);
EXTERN INT32 NOR_EraseMap(SECTTBL_T *prBlkMap); // Get erase map

EXTERN INT32 NOR_CalCRC(UINT32 u4Addr, UINT32 u4Size, UINT32* pu4Checksum);

EXTERN INT32 NOR_EnableDMA(UINT32 u4SrcAddr, UINT32 u4DestAddr, UINT32 u4Size);

EXTERN INT32 NOR_Diag(void);


/* Implement at serialflash_func.c */
EXTERN INT32 SFLASH_Init(void);
EXTERN INT32 SFLASH_Diag(void);
EXTERN INT32 SFLASH_Reset(void);
EXTERN INT32 SFLASH_GetID(UINT32 u4Index, UINT8 *pu1MenuID, UINT8 *pu1DevID1, UINT8 *pu1DevID2);

EXTERN INT32 SFLASH_Read(UINT32 u4Offset, UPTR u4MemPtr, UINT32 u4ByteCount);
EXTERN INT32 SFLASH_ReadProgram(UINT32 u4Offset, UPTR u4MemPtr, UINT32 u4ByteCount);
EXTERN INT32 SFLASH_ReadRISCProgram(UINT32 u4Offset, UPTR u4MemPtr, UINT32 u4ByteCount);
EXTERN INT32 SFLASH_Write(UINT32 u4Offset, UPTR u4MemPtr, UINT32 u4ByteCount);
EXTERN INT32 SFLASH_WriteProgram(UINT32 u4Offset, UPTR u4MemPtr, UINT32 u4ByteCount);
EXTERN INT32 SFLASH_WriteRISCProgram(UINT32 u4Offset, UPTR u4MemPtr, UINT32 u4ByteCount);
EXTERN INT32 SFLASH_PagePragramWrite(UINT32 u4Offset, UPTR u4MemPtr, UINT32 u4ByteCount);
EXTERN INT32 SFLASH_PagePragramCountWrite(UINT32 u4Offset, UPTR u4MemPtr, UINT32 u4ByteCount, UINT8 u1PageCount);
EXTERN INT32 SFLASHHW_WriteCountMutilPP(UINT32 u4Index, UINT32 u4Addr, UINT32 u4Len, const UINT8* pu1Buf, UINT8 u1PageCount);
EXTERN INT32 SFLASH_WriteOneByte(UINT32 u4Addr, UINT8 u1Data);
EXTERN INT32 SFLASH_WrNoE(UINT32 u4Offset, UPTR u4MemPtr, UINT32 u4ByteCount);

EXTERN INT32 SFLASH_EraseAddr(UINT32 u4Offset, UINT32 u4ByteLen);
EXTERN INT32 SFLASH_EraseSectIdx(UINT32 u4SectIdx, UINT32 u4SectNum);
EXTERN INT32 SFLASH_EraseOneBlk(UINT32 u4Addr);
EXTERN INT32 SFLASH_EraseChip(UINT32 u4Index);

EXTERN INT32 SFLASH_CalCRC(UINT32 u4Addr, UINT32 u4Len, UINT32* pu4CheckSum);

EXTERN INT32 SFLASH_EraseBlockMap(SECTTBL_T *prBlkMap, UINT32 *pu4TotalNorSz);

EXTERN void SFLASH_Calibrate(UINT32 u4Addr, UINT32 u4ByteCount, const UINT8 *pu1Buf);

EXTERN INT32 SFLASH_ReadFlashStatus(UINT32 u4Index, UINT8* pu1Status);
EXTERN INT32 SFLASH_WriteProtect(UINT32 u4Index, BOOL fgEnable);

EXTERN CHAR* SFLASH_GetFlashName(UINT8 u1MenuID, UINT8 u1DevID1, UINT8 u1DevID2);
EXTERN UINT32 SFLASH_GetFlashSize(UINT8 u1MenuID, UINT8 u1DevID1, UINT8 u1DevID2);


EXTERN INT32 SFLASH_EnableDMA(UINT32 u4SrcAddr, UINT32 u4DestAddr, UINT32 u4Size);

EXTERN void SFLASH_PinMux(UINT32 u4Val);


/* Implement at serialflash_hw.c */
EXTERN void SFLASHHW_SetClk(UINT32 u4Val);
EXTERN void SFLASHHW_SetEnableClkDelay(BOOL fgEnable);
EXTERN void SFLASHHW_SetEnableDataDelay(BOOL fgEnable);
EXTERN void SFLASHHW_SetClkDelay(UINT32 u4Val);
EXTERN void SFLASHHW_SetDataDelay(UINT32 u4Val);
EXTERN UINT32 SFLASHHW_GetClk(void);
EXTERN UINT32 SFLASHHW_GetDataDelay(void);
EXTERN UINT32 SFLASHHW_GetClkDelay(void);
EXTERN void SFLASHHW_SetReadType(UINT32 u4Val);
EXTERN UINT32 SFLASHHW_GetReadType(void);
EXTERN void SFLASHHW_SetSampleEdge(UINT32 u4Val);
EXTERN UINT32 SFLASHHW_GetSampleEdge(void);
#ifdef BD_NOR_ENABLE
EXTERN INT32 NOR_Enter4B(void);
EXTERN INT32 NOR_Enter4BitRead(void);
EXTERN INT32 NOR_Exit4BitRead(void);
EXTERN INT32 NOR_Exit4B(void);
EXTERN INT32 NOR_4BitRead(UINT32 u4Offset, UPTR u4MemPtr, UINT32 u4ByteCount);
EXTERN INT32 NOR_4Byte4BitRead(UINT32 u4Offset, UPTR u4MemPtr, UINT32 u4ByteCount);
EXTERN INT32 NOR_4BWrite(UINT32 u4Offset, UPTR u4MemPtr, UINT32 u4ByteCount);
EXTERN INT32 NOR_4BEraseAddr(UINT32 u4Offset, UINT32 u4ByteLen);
EXTERN void  NOR_WriteCfg(UINT32 uAddr);

EXTERN INT32 SFLASH_Enter4B(void);
EXTERN INT32 SFLASH_Enter4BitRead(void);
EXTERN INT32 SFLASH_Exit4BitRead(void);
EXTERN INT32 SFLASH_Exit4B(void);
EXTERN INT32 SFLASH_4BitRead(UINT32 u4Offset, UPTR u4MemPtr, UINT32 u4ByteCount);
EXTERN INT32 SFLASH_4Byte4BitRead(UINT32 u4Offset, UPTR u4MemPtr, UINT32 u4ByteCount);
EXTERN INT32 SFLASH_4BWrite(UINT32 u4Offset, UPTR u4MemPtr, UINT32 u4ByteCount);
EXTERN INT32 SFLASH_4BEraseAddr(UINT32 u4Offset, UINT32 u4ByteLen);
EXTERN void  SFLASH_WriteCfg(UINT32 uAddr);
#endif
#else
#include "x_typedef.h"
#include "storage_if.h"
#define ERR_NOT_SUPPORT_NOR -1

#endif/*CC_NOR_DISABLE*/

#endif /* NOR_IF_H */

