/*-----------------------------------------------------------------------------------
!
!                              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        : wgn_reg.h
  DESCRIPTION : wgn Register
  Revision    : $Rev: 4 $
  Author      : $Author: shirait $
  Date        : $Date: 2015-09-09 15:19:58 +0900 (豌ｴ, 09 9 2015) $
-----------------------------------------------------------------------------------*/
#ifndef ___WGN_REG_H_INCLUDED___
#define ___WGN_REG_H_INCLUDED___

#include <stdint.h>

#define WGN_REG_BASE (0x40004000u)
#define WGNR_CMD_SIZE                (16)
#define WGN_AES_SK_SIZE  (16)
#define WGN_AES_IV_SIZE  (16)
#define WGN_BUFFER_SIZE  (1024)


typedef union {
	uint32_t u32;
	struct {
		uint32_t cdst      : 8;
		uint32_t crcstatus : 2;
		uint32_t reserve1  : 2;
		uint32_t toerr     : 1;
		uint32_t reserve2  : 2;
		uint32_t ltc       : 1;
		uint32_t lts3st    : 8;
		uint32_t lts3mode  : 2;
		uint32_t reserve3  : 2;
		uint32_t reserve4  : 4;
	}bit;	
}LTS3_STATUS;

typedef union {
	uint32_t u32;
	struct {
		uint32_t rdclk     : 3;
		uint32_t reserve1  : 1;
		uint32_t wrclk     : 3;
		uint32_t reserve2  : 1;
		uint32_t reserve3  : 8;
		uint32_t toset     : 1;
		uint32_t reserve4  : 3;
		uint32_t to        : 4;
		uint32_t reserve5  : 4;
		uint32_t reserve6  : 3;
		uint32_t clksel    : 1;
	}bit;	
}ACC_CTL1;


typedef struct _tagWgnReg {
	
	// 0000h
	LTS3_STATUS status;
	
	// 0004h
	ACC_CTL1 acc_ctl1;

	// 0008h
	uint32_t rd_wait1_time;

	// 000Ch
	uint32_t rd_wait2_time;

	// 0010h
	uint32_t wr_wait1_time;

	// 0014h
	uint32_t wr_wait2_time;

	// 0030h
	uint32_t page_remainder;
	
	// 001Ch - 002Ch
	uint32_t reserve1[5];
	
	// 0030h
	uint8_t reserve2;
	uint8_t page_length;
	uint8_t sel_rw;
	uint8_t reserve3;
	
	// 0034h
	uint32_t page_count;
	
	// 0038h - 0044h
	uint8_t cmd[WGNR_CMD_SIZE];
	
	// 0048h
	union {
		uint8_t byte;
		struct {
			uint8_t trnfin   : 1;
			uint8_t tmstrt   : 1;
			uint8_t tmend    : 1;
			uint8_t t1key    : 1;
			uint8_t reserved : 4;
		} bit;
	}inten;
	uint8_t reserve4[3];
	
	
	// 004Ch
	uint8_t bus_enable;
	uint8_t reserve5[3];

	// 0050h
	uint8_t mem_reset;
	uint8_t reserve6[3];
	
	// 0054h
	uint8_t dly_mode;
	uint8_t reserve7[3];
	
	// 0058h
	uint8_t set_sc_key_st;
	uint8_t reserve8[3];
	
	// 005Ch
	uint8_t rb_status;
	uint8_t reserve9[3];

	// 0060h
	uint8_t t_flag;
	uint8_t reserve10[3];

	// 0064h
	uint8_t t_kill;
	uint8_t reserve11[3];

} WgnReg;

#define WGNR (*(volatile WgnReg*)(WGN_REG_BASE))


#define WGNR_INTEN_T1KEY  (0x08)
#define WGNR_INTEN_TMEND  (0x04)
#define WGNR_INTEN_TMSTRT (0x02)
#define WGNR_INTEN_TRNFIN (0x01)

#define WGNR_CRCRSLT_ERR      (0x01)
#define WGNR_CRCRSLT_SUCCESS  (0x00)


#define WGNR_CARDPHASE_IDLE      (0x00)
#define WGNR_CARDPHASE_CEB_LOW   (0x01)  // CSBをLowにセット
#define WGNR_CARDPHASE_CMD       (0x02)  // コマンド
#define WGNR_CARDPHASE_CMDCRC    (0x03)  // コマンドのCRC
#define WGNR_CARDPHASE_CMDHIZ    (0x04)  // コマンド送信後のHi-z
#define WGNR_CARDPHASE_CS        (0x05)  // Card Status
#define WGNR_CARDPHASE_CSHIZ     (0x06)  // Card Status送信後のHi-Z
#define WGNR_CARDPHASE_WAIT1     (0x07)  // Wait1
#define WGNR_CARDPHASE_RB1       (0x08)  // Ready/Busy1
#define WGNR_CARDPHASE_RB1HIZ    (0x09)  // Ready/Busy1後のHi-z
#define WGNR_CARDPHASE_DATA      (0x0A)  // PageData
#define WGNR_CARDPHASE_DATACRC   (0x0B)  // PageDataのCRC
#define WGNR_CARDPHASE_DATAHIZ   (0x0C)  // PageData後のCRC

// WGN_CORE_STATUS
#define WGNR_CORESTATUS_BUSY     (0x00)
#define WGNR_CORESTATUS_READY    (0x01)

// WGN_SET_T1_KEY_ST
#define WGNR_SETT1KEYST_IDEL     (0x01)
#define WGNR_SETT1KEYST_BUSY     (0x00)

#define WGNR_SELSEC_T1               (0x00)
#define WGNR_SELSEC_T2               (0x01)

#define WGNR_PL_0                    (0x00)
#define WGNR_PL_4                    (0x01)
#define WGNR_PL_64                   (0x02)
#define WGNR_PL_512                  (0x03)

#define WGNR_SELRW_READ              (0x00)
#define WGNR_SELRW_WRITE             (0x01)

#define WGNR_SELBUF_FIFO             (0x00)
#define WGNR_SELBUF_DATA_BUFFER      (0x01)

#define WGNR_TFLG_EN                 (0x01)
#define WGNR_TKLL_EN                 (0x01)



#endif // ___WGN_REG_H_INCLUDED___
