/*
@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_fuse_location.h"
#include "bksecapp_warranty.h"

/* Bus frequency is required when calling the qfprom APIs, 
   but the value is not actually used.*/
#define BUS_FREQUENCY 0 

int set_warranty_bit_fuse()
{
	uint32 qfprom_status = 0;
	uint32 row_data_write[2] = {0x1, 0x0};
	uint32 fuse_addr;

	fuse_addr = HWIO_QFPROM_RAW_OEM_SPARE_REGn_ROW0_LSB_ADDR(QFPROM_RAW_OEM_SPARE_REG_WARRANTY_BIT);
#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 */
	qsee_fuse_write(fuse_addr, row_data_write, BUS_FREQUENCY, &qfprom_status);
	if(qfprom_status != QFPROM_NO_ERR)
	{
		qsee_log(QSEE_LOG_MSG_ERROR, "Write returned error: %d", qfprom_status);
		return -1;
	}
	
	qsee_log(QSEE_LOG_MSG_ERROR, "WB fuse blow SUCCESS" );
	return 0;
}

int get_warranty_bit_fuse()
{
	uint32 qfprom_status = 0;
	uint32 row_data_read[2] = {0x0, 0x0};
	uint32 fuse_addr;

	fuse_addr = HWIO_QFPROM_RAW_OEM_SPARE_REGn_ROW0_LSB_ADDR(QFPROM_RAW_OEM_SPARE_REG_WARRANTY_BIT);
#ifdef DEBUG_LOG_ENABLE	
	qsee_log(QSEE_LOG_MSG_ERROR, "Read Fuse address [%x]", fuse_addr);
#endif	
	
	/*Read the fuse value*/
	qsee_fuse_read(fuse_addr, QFPROM_ADDR_SPACE_RAW, row_data_read, &qfprom_status);
	if(qfprom_status != QFPROM_NO_ERR)
	{
		qsee_log(QSEE_LOG_MSG_ERROR, "Initial read returned error: %d", qfprom_status);
		return -1;
	}
#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 -1;
}
