/*
@file qsee_warranty.c
@brief Contains test code for most of the QSEE fuse APIs.

*/
/*===========================================================================
	 Copyright (c) 2011 by Qualcomm Technologies, Incorporated.	All Rights Reserved.
===========================================================================*/

/*===========================================================================

														EDIT HISTORY FOR FILE
	$Header: 
	$DateTime: 
	$Author: pwbldsvc $

# when			 who		 what, where, why
# 2015-11-16 bogil.jang first implement

===========================================================================*/

#include <stdbool.h>
#include "qsee_log.h"
#include "qsee_fuse.h"
#include "qfprom.h"
#include "bksecapp_common.h"

/* Bus frequency is required when calling the qfprom APIs, 
	 but the value is not actually used.*/
#define BUS_FREQUENCY 0 

int32_t set_warranty_bit_fuse(void)
{
	uint32_t qfprom_status = 0;
	uint32_t row_data_write[2] = {0x1, 0x0};
	uint32_t fuse_addr;
	int32_t ret = 0;

	fuse_addr = BK_OEM_SPARE_REGION_SECURE_BOOT_SOLUTION;
#ifdef DEBUG_LOG_ENABLE	
	qsee_log(QSEE_LOG_MSG_ERROR, "Fuse address [%x]", fuse_addr);
	qsee_log(QSEE_LOG_MSG_ERROR, "Set Warranty bit [%x, %x]", row_data_write[0], row_data_write[1]);
#endif	

	/*Blow the fuse */
	ret = qsee_fuse_write(fuse_addr, row_data_write, BUS_FREQUENCY, &qfprom_status);
	if ((ret != 0) || (qfprom_status != QFPROM_NO_ERR))
	{
		qsee_log(QSEE_LOG_MSG_ERROR, "Write returned error: %d, %d",ret, qfprom_status);
		if (ret)
			return BKERR_WARRNATY_WRITE_QFPROM_ERROR;
		else
			return BKERR_WARRNATY_WRITE_QFPROM_API_ERROR;
	}
	
	qsee_log(QSEE_LOG_MSG_ERROR, "WB fuse blow SUCCESS" );
	return BKERR_WARRNATY_WRITE_SUCCESS;
}

int32_t get_warranty_bit_fuse(void)
{
	uint32_t qfprom_status = 0;
	uint32_t row_data_read[2] = {0x0, 0x0};
	uint32_t fuse_addr;
	int32_t ret = 0;

	fuse_addr = BK_OEM_SPARE_REGION_SECURE_BOOT_SOLUTION;
#ifdef DEBUG_LOG_ENABLE	
	qsee_log(QSEE_LOG_MSG_ERROR, "Read Fuse address [%x]", fuse_addr);
#endif	
	
	/*Read the fuse value*/
	ret = qsee_fuse_read(fuse_addr, QFPROM_ADDR_SPACE_RAW, row_data_read, &qfprom_status);

	if ((ret != 0) || (qfprom_status != QFPROM_NO_ERR))
	{
		qsee_log(QSEE_LOG_MSG_ERROR, "Initial read returned error: %d, %d",ret, qfprom_status);
		if (ret)
			return BKERR_WARRNATY_READ_QFPROM_ERROR;
		else
			return BKERR_WARRNATY_READ_QFPROM_API_ERROR;
	}
#ifdef DEBUG_LOG_ENABLE	
	else
	{
		qsee_log(QSEE_LOG_MSG_ERROR, "Initial read value: %x %x",row_data_read[0], row_data_read[1]);
	}
#endif
	
	if ((row_data_read[0] & 0x1) == 0x0)
	{
		qsee_log(QSEE_LOG_MSG_ERROR, "WB 0");
		return 0;
	}
	else 
	{
		qsee_log(QSEE_LOG_MSG_ERROR, "WB 1");
		return 1;
	}

	return BKERR_WARRNATY_READ_QFPROM_END;
}
