/*-----------------------------------------------------------------------------------
!
!                              COPYRIGHT 2015 BY
!                            MegaChips Corporation
!
!------------------------------------------------------------------------------------
! These coded instructions, statements, and computer programs include
! intellectual property and know-how belonging to MegaChips
! Corporaiton ("MCC") and its partner companies.
! Therefore, do not use these specifications for any purpose other than
! the purpose
! expressly agreed to by MCCs customer and MCC.  Also, do not duplicate,
! copy,
! or reproduce these specifications, nor reveal these specifications to
! any third party
! without MCCs prior written consent.
!
*/

/*-----------------------------------------------------------------------------------
  FILE        : ctl.h
  DESCRIPTION : ctl.c header
  Revision    : $Rev: 4 $
  Author      : $Author: shirait $
  Date        : $Date: 2015-09-09 15:19:58 +0900 (豌ｴ, 09 9 2015) $
-----------------------------------------------------------------------------------*/
#ifndef __CTL_H__
#define __CTL_H__


/********************************************************************************************
include
********************************************************************************************/
#include <stdint.h>
#include "ctl_reg.h"

/********************************************************************************************
define
********************************************************************************************/
#define FWSEED_SIZE (16)
#define MMC_WRITE   (1)
#define MMC_READ    (0)



/********************************************************************************************
macro
********************************************************************************************/
//
//
// BootCodeサイズを抑えるため、一部マクロ化しています
//
//
/*----------------------------------------------------------------------------------
  function  : Lazyフラグのセット
  arguments : none
  return    : void
  detail    : none
-----------------------------------------------------------------------------------*/
#define CTL_SetLazyFlag(data) (CTLR.LOCALR.lazy = (data))


/*----------------------------------------------------------------------------------
  function  : SetOTP Data
  arguments : otpTestBlockFlag : I : テストモード禁止フラグ
              hwVersion        : I : HW Version
  return    : void
  detail    : none
-----------------------------------------------------------------------------------*/
#define UnsignedLongLongFlag2BitFlag(x)  (((x) == 0) ? 0 : 1)
#define CTL_SetOtpData(otpTestBlockFlag, hwVersion) \
	CTLR.LOCALR.otp_test_blk = UnsignedLongLongFlag2BitFlag((otpTestBlockFlag)); \
	CTLR.LTS3R.lts3_version = (hwVersion) 


/*----------------------------------------------------------------------------------
  function  : L1 FW Seedの取得
  arguments : none
  return    : L1 FW Seed
  detail    : none
-----------------------------------------------------------------------------------*/
#define CTL_GetL1FWSeed() (CTLR.L1R.fw_sk)


/*----------------------------------------------------------------------------------
  function  : L1 FW Seedの切断
  arguments : none
  return    : void
  detail    : none
-----------------------------------------------------------------------------------*/
#define  CTL_DisconnectL1FWSeed()  (CTLR.L1R.fw_sk_cut = CTLR_L1_FWSEED_CUT)


/*----------------------------------------------------------------------------------
  function  : RSA秘密鍵のセット
  arguments : pSkeyd : I  : RSA秘密鍵D
  return    : void
  detail    : none
-----------------------------------------------------------------------------------*/
#define CTL_SetRSAKey(pSkeyd) MemcpyP2P(CTLR.L1R.rsa_d, (pSkeyd), RSA_D_SIZE)


/*----------------------------------------------------------------------------------
  function  : SHAモジュールのリセット
  arguments : none
  return    : void
  detail    : none
-----------------------------------------------------------------------------------*/
#define CTL_ResetSHA() \
	CTLR.LOCALR.ip_resb = CTLR_LOCAL_RESET_MASK ^ CTLR_LOCAL_SHA_RESET_MASK; \
	CTLR.LOCALR.ip_resb = CTLR_LOCAL_RESET_MASK



/*----------------------------------------------------------------------------------
  function  : FIFOクリア
  arguments : none
  return    : void
  detail    : none
-----------------------------------------------------------------------------------*/
#define CTL_ClearFIFO() \
	CTLR.LOCALR.fifo_clr ^= CLTR_LOCALR_FIFO_CLERA; \
	CTLR.LOCALR.fifo_clr ^= CLTR_LOCALR_FIFO_CLERA

	

/*---------------------------------------------------------------------------
  function  : EMMCモジュールのCMD63割り込み出力停止
  arguments : none
  return    : void
  detail    : none
--------------------------------------------------------------------------*/
#define CTL_EMMC_CMD60IRQClear()  CTL_EMMC_Req(&CTLR.MMCR.ctl_ack60, &CTLR.MMCR.ctl_int60, INT_DISABLE)

/*---------------------------------------------------------------------------
  function  : EMMCモジュールのCMD63割り込み出力停止
  arguments : none
  return    : void
  detail    : none
--------------------------------------------------------------------------*/
#define CTL_EMMC_CMD63IRQClear() CTL_EMMC_Req(&CTLR.MMCR.ctl_ack63, &CTLR.MMCR.ctl_int63, INT_DISABLE)


/*---------------------------------------------------------------------------
  function  : EMMCモジュールへのBusy終了要求
  arguments : none
  return    : void
  detail    : none
--------------------------------------------------------------------------*/
#define CTL_EMMC_SetBusyFinish()  CTL_EMMC_Req(&CTLR.MMCR.busy_ack, &CTLR.MMCR.busy, BUSY_DISABLE)


/*---------------------------------------------------------------------------
  function  : DeviceStatus.Lotus3_ERRORの設定
  arguments : lotus3Error       : I  : Lotus3_ERROR
  return    : void
  detail    : none
--------------------------------------------------------------------------*/
#define CTL_EMMC_SetLotus3Error(lotus3Error) (CTLR.MMCR.lts3_error = (lotus3Error))



/*---------------------------------------------------------------------------
  function  : Detailの設定
  arguments : detail           : I  : Lotus3_ERROR_Detail
  return    : void
  detail    : none
--------------------------------------------------------------------------*/
#define CTL_EMMC_SetDetail(detail)  (CTLR.MMCR.lts3_error_detail = (detail))



/*---------------------------------------------------------------------------
  function  : DeviceStatus.CC_ERRORの設定
  arguments : none
  return    : void
  detail    : none
--------------------------------------------------------------------------*/
#define CTL_EMMC_SetCCError()  (CTLR.MMCR.cc_error = 1)



/*---------------------------------------------------------------------------
  function  : 中断フラグの取得
  arguments : none
  return    : void
  detail    : 中断フラグ
--------------------------------------------------------------------------*/
#define CTL_EMMC_GetStopFlag() ((int)CTLR.MMCR.stop)



/*---------------------------------------------------------------------------
  function  : State9フラグの取得
  arguments : none
  return    : void
  detail    : State9フラグ
--------------------------------------------------------------------------*/
#define CTL_EMMC_GetState9Flag()  (CTLR.MMCR.state9_now)


/*---------------------------------------------------------------------------
  function  : データ転送条件セット
  arguments : wrFlag     : I  : Write Flag
			  blockCount : I  : 転送ブロック数
  return    : void
  detail    : none
--------------------------------------------------------------------------*/
#define CTL_EMMC_SetDataTransfer(wrFlag, blockCount)  \
	CTLR.MMCR.rw = (wrFlag); \
	CTLR.MMCR.block_count = (blockCount)



/********************************************************************************************
prottype
********************************************************************************************/
void CTL_Init(void);
void CTL_EMMC_Req(volatile uint32_t* pActReg, volatile const uint32_t* pStReg, uint32_t waitStatus);
void CTL_EMMC_WaitBusy(void);


#endif // __CTL_H__
