/* SPDX-License-Identifier: (GPL-2.0-only OR BSD-3-Clause) */
/*
 * Copyright (c) 2023 MediaTek Inc.
 */

/* This header is for efuse version control feature and must be associated with
 * Optee/UBoot/Sboot/Kernel
 * Please keep the file's consistency among these THREE modules.
 */

struct VERSION_INFO {
	unsigned long long ver_pmu;
	unsigned long long ver_vbmeta;
	unsigned long long ver_boot;
	unsigned long long ver_recovery;
};

//We use bit0 from EFUSE_RESERVED_FOR_USER_1 as the rollback enabling bit
#define EFUSR_ROLLBACK_ENABLING_BIT_MASK 0x00000001
enum EFUSE_BANK {
	//bank 0x43 is defined as E_SYS_QUERY_TVM_END_USER_32BIT_1 in efuse layout,
	//and we only use bit[0] as rollback enabling bit here
	EFUSE_RESERVED_FOR_USER_1 = 0x43, //For rollback enabling bit[0]
	EFUSE_H1L_VER = 0xE0, //size = EFUSE_ARRAY_32BIT_SIZE
	EFUSE_RL_VER = 0xE1, //size = EFUSE_ARRAY_32BIT_SIZE
	EFUSE_TL_VER = 0xE2, //size = EFUSE_ARRAY_32BIT_SIZE
	EFUSE_ATF_VER = 0xE3, //size = EFUSE_ARRAY_128BIT_SIZE
	EFUSE_OPTEE_VER = 0xE7, //size = EFUSE_ARRAY_128BIT_SIZE
	EFUSE_UBOOT_VER = 0xEB, //size = EFUSE_ARRAY_128BIT_SIZE
	EFUSE_PMU_VER = 0xEF, //size = EFUSE_ARRAY_64BIT_SIZE
	EFUSE_VBMETA_VER = 0xF1, //size = EFUSE_ARRAY_128BIT_SIZE
	EFUSE_BOOT_VER = 0xF5, //size = EFUSE_ARRAY_128BIT_SIZE
	EFUSE_RECOVERY_VER = 0xF9, //size = EFUSE_ARRAY_128BIT_SIZE
	EFUSE_RESERVED = 0xFD,
};

#define EFUSE_ARRAY_128BIT_SIZE 4
#define EFUSE_ARRAY_96BIT_SIZE 3
#define EFUSE_ARRAY_64BIT_SIZE 2
#define EFUSE_ARRAY_32BIT_SIZE 1

enum EFUSE_BANK_SIZE {
	EFUSE_H1L_SIZE = (EFUSE_RL_VER - EFUSE_H1L_VER) * EFUSE_ARRAY_32BIT_SIZE,
	EFUSE_RL_SIZE = (EFUSE_TL_VER - EFUSE_RL_VER) * EFUSE_ARRAY_32BIT_SIZE,
	EFUSE_TL_SIZE = (EFUSE_ATF_VER - EFUSE_TL_VER) * EFUSE_ARRAY_32BIT_SIZE,
	EFUSE_ATF_SIZE = (EFUSE_OPTEE_VER - EFUSE_ATF_VER) * EFUSE_ARRAY_32BIT_SIZE,
	EFUSE_OPTEE_SIZE = (EFUSE_UBOOT_VER - EFUSE_OPTEE_VER) * EFUSE_ARRAY_32BIT_SIZE,
	EFUSE_UBOOT_SIZE = (EFUSE_PMU_VER - EFUSE_UBOOT_VER) * EFUSE_ARRAY_32BIT_SIZE,
	EFUSE_PMU_SIZE = (EFUSE_VBMETA_VER - EFUSE_PMU_VER) * EFUSE_ARRAY_32BIT_SIZE,
	EFUSE_VBMETA_SIZE = (EFUSE_BOOT_VER - EFUSE_VBMETA_VER) * EFUSE_ARRAY_32BIT_SIZE,
	EFUSE_BOOT_SIZE = (EFUSE_RECOVERY_VER - EFUSE_BOOT_VER) * EFUSE_ARRAY_32BIT_SIZE,
	EFUSE_RECOVERY_SIZE = (EFUSE_RESERVED - EFUSE_RECOVERY_VER) * EFUSE_ARRAY_32BIT_SIZE,
};

// These definitions must be kept consistent with BoardConfigCommon.mk accordingly
#define VBMETA_ROLLBACK_INDEX_LOCATION 0
#define RECOVERY_ROLLBACK_INDEX_LOCATION 1
#define SYSTEM_ROLLBACK_INDEX_LOCATION 2
#define VENDOR_ROLLBACK_INDEX_LOCATION 3
#define DTBO_ROLLBACK_INDEX_LOCATION 4
#define BOOT_ROLLBACK_INDEX_LOCATION 5
#define OEM_ROLLBACK_INDEX_LOCATION 6
#define BOOTDATA_ROLLBACK_INDEX_LOCATION 7
