/* 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: nandhw_if.h,v $
 * $Revision: #1 $
 *
 *---------------------------------------------------------------------------*/

/** @file nandhw_if.h
 *  nandhw_if.h The NAND low level function delcaration.
 */


#ifndef NANDHW_HW_IF_H
#define NANDHW_HW_IF_H


//-----------------------------------------------------------------------------
// Include files
//-----------------------------------------------------------------------------

#include "x_typedef.h"
#include "nand_if.h"

//
// Device information 
//

#define NAND_BBMODE_BBMARK	0x0000FF00
#define NAND_BBMODE_ECCBIT	0x000000FF

#ifdef NAND_DEV_INFO_REFINE
#define NAND_NEEDMORECHK_ENABLE 0x80
#define NAND_NEEDMORECHK_EXTID  0x02
#define NAND_NEEDMORECHK_ONFI   0x01
#define NAND_FEATURESPT_ONFI    0x01
#define NAND_BBPAGECHK_PAGE0    0x01
#define NAND_BBPAGECHK_PAGE1    0x02
#define NAND_BBPAGECHK_3RDLAST  0x40
#define NAND_BBPAGECHK_LAST     0x80
#endif

typedef struct
{
    CHAR acName[NAND_NAME_MAX_SIZE];
    UINT8  u1AddrCycle;

    #ifdef NAND_DEV_INFO_REFINE
    //only if u1NeedMoreChk[7] is 1, u1FeatureSupport, u2ExtID, u2ExtIDMask are significant.
    UINT8  u1NeedMoreChk; //[0]: check ONFI signature; [1]: check ExtID; [7]: whether need more checking.
    UINT8  u1FeatureSupport; //[0]: support ONFI;
    UINT16 u2ExtID; //extend ID;
    UINT16 u2ExtIDMask; //the extend ID mask;
    //u8BBPageChk is used to describe which pages we should check to find Bad Block.
    UINT8 u1BBPageChk; //[0]: page 0; [1]: page 1; [6]: (last -2)th page; [7]: the last page; 
    #endif
    
    UINT32 u4IDVal;
    UINT32 u4IDMask;
    UINT32 u4PageSize;
    UINT32 u4OOBSize;
    UINT32 u4BBMode;
    UINT32 u4BlkSize;    
    UINT32 u4BlkPgCount;
    UINT32 u4ChipSize;
    UINT32 u4AccCon;
    UINT32 u4RdAcc;
} NAND_FLASH_DEV_T;

//-----------------------------------------------------------------------------
// Inter-file functions
//-----------------------------------------------------------------------------
EXTERN void NANDHW_EnablePinmux(BOOL fgEnable);
EXTERN INT32 NANDHW_Init(void);
EXTERN void NANDHW_ReadID(UINT32 *pu4ID);

EXTERN void NANDHW_GetInfo(NANDDEV_T *prNand);
EXTERN char* NANDHW_GetName(void);
EXTERN void NANDHW_SetControl(UINT32 u4Flag, NAND_CONTROL_T* prCtrl);
EXTERN void NANDHW_GetControl(UINT32 u4Flag, NAND_CONTROL_T* prCtrl);

EXTERN void NANDHW_Reset(void);
EXTERN UINT32 NANDHW_ReadStatus(void);
EXTERN INT32 NANDHW_ReadPageAttr(UINT32 *pu4Buf, UINT32 *pu4Spare, UINT32 u4PgIdx);
EXTERN INT32 NANDHW_WritePageAttr(UINT32 *pu4Buf, UINT32 *pu4Spare, UINT32 u4PgIdx);
EXTERN INT32 NANDHW_VerifyPageAttr(UINT32 u4PgIdx);

EXTERN BOOL NANDHW_IsBadBlk(UINT32 u4BlkIdx);
EXTERN BOOL NANDHW_MarkBadBlk(UINT32 u4BlkIdx);
EXTERN INT32 NANDHW_EraseBlock(UINT32 u4BlkIdx);

#if NAND_MULTIPAGE_ACCESS
EXTERN INT32 NANDHW_ReadMultiPageAttr(UINT32 *pu4Buf , UINT32 *pu4Spare, UINT32 u4PgIdx, UINT32 u4PgCnt);
EXTERN INT32 NANDHW_WriteMultiPageAttr(UINT32 *pu4Buf, UINT32 *pu4Spare, UINT32 u4PgIdx, UINT32 u4PgCnt);
EXTERN INT32 NANDHW_EraseMultiBlock(UINT32 u4BlkIdx, UINT32 u4BlkCnt);
#endif

#ifdef CC_SUPPORT_STR
EXTERN void NFI_pm_resume(void);
EXTERN void NFI_pm_suspend(void);
#endif


// Nand_dev.c
EXTERN UINT32 NANDDev_GetDevCount(VOID);
EXTERN NAND_FLASH_DEV_T* NANDDev_GetDev(UINT32 u4Idx);
EXTERN void NANDDev_ShowDevList(void);

#endif /* NANDHW_FUNC_H */

