/* SPDX-License-Identifier: BSD-3-Clause */

#ifndef __EMI_H__
#define __EMI_H__

#include <dramc_top.h>

#if __ETT__
#define emi_log	printf
#elif __FLASH_TOOL_DA__
#define emi_log	LOGD
#else
#define emi_log	print
#endif

struct isu_info_t {
	unsigned int buf_size;
	unsigned long long buf_addr;
	unsigned long long ver_addr;
	unsigned long long con_addr;
};

#define EMI_ISU_BUF_SIZE	0x800000
#define LAST_EMI_MAGIC_PATTERN 0x19870611
typedef struct {
	unsigned int isu_magic;
	unsigned int isu_ctrl;
	unsigned int isu_dram_type;
	unsigned int isu_diff_us;
	unsigned int isu_buf_l;
	unsigned int isu_buf_h;
	unsigned int isu_version;
	unsigned int snst_last;
	unsigned int snst_past;
	unsigned int os_flag_sspm;
	unsigned int os_flag_ap;
} LAST_EMI_INFO_T;
#define DEF_LAST_EMI LAST_EMI_INFO_T

typedef struct {
	unsigned int dram_type;
	unsigned int ch_num;
	unsigned int rk_num;
	unsigned int bank_width[DRAMC_MAX_RK];
	unsigned int row_width[DRAMC_MAX_RK];
	unsigned int col_width[DRAMC_MAX_RK];
	U64 rank_size[DRAMC_MAX_RK];
} EMI_INFO_T;

void emi_init(void);
void emi_init2(void);
void clr_emi_mpu_prot(void);
void dis_emi_apb_prot(void);
int get_row_width_by_emi(unsigned int rank);
int get_channel_nr_by_emi(void);
int get_rank_nr_by_emi(void);
void get_rank_size_by_emi(unsigned long long dram_rank_size[DRAMC_MAX_RK]);
void set_cen_emi_cona(unsigned int cona_val);
void set_cen_emi_conf(unsigned int conf_val);
void set_cen_emi_conh(unsigned int conh_val);
void set_chn_emi_cona(unsigned int cona_val);
void set_chn_emi_conc(unsigned int conc_val);
unsigned int get_cen_emi_cona(void);
unsigned int get_chn_emi_cona(void);
void enable_infra_emi_broadcast(unsigned int enable);
void phy_addr_to_dram_addr(dram_addr_t *dram_addr, unsigned long long phy_addr);
unsigned int set_emi_before_rank1_mem_test(void);
void restore_emi_after_rank1_mem_test(void);
void get_emi_isu_info(struct isu_info_t *isu_info_ptr);
void reserve_emi_isu_buf(void);
void reserve_emi_mbist_buf(void);
void record_emi_snst(void);
unsigned long long platform_memory_size(void);
int update_emi_setting(EMI_SETTINGS *default_emi_setting, EMI_INFO_T *emi_info);
//int mt_mem_init();

#endif /* __EMI_H__ */