/*
@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 "questapp_fuse_location.h"
#include "questapp_smdresult.h"

/* Bus frequency is required when calling the qfprom APIs, 
   but the value is not actually used.*/
#define BUS_FREQUENCY 0 
#define DEBUG_LOG_ENABLE 1

int set_smd_quest_result_fuse(uint64_t data)
{
	uint32 qfprom_status = 0;
	uint32 fuse_addr;
	uint64_t row_data_write = data;

	fuse_addr = QUEST_OEM_SPARE_REGION;
#ifdef DEBUG_LOG_ENABLE	
	qsee_log(QSEE_LOG_MSG_ERROR, "Fuse address [%x]", fuse_addr);
	qsee_log(QSEE_LOG_MSG_ERROR, "write data %x %x", (uint32_t)(row_data_write >> 32), (uint32_t)row_data_write);
#endif	

	/*Blow the fuse */
	qsee_fuse_write(fuse_addr, (uint32_t*)(uintnt)&row_data_write, BUS_FREQUENCY, &qfprom_status);
	if(qfprom_status != QFPROM_NO_ERR)
	{
		qsee_log(QSEE_LOG_MSG_ERROR, "QUEST Write returned error: %d", qfprom_status);
		return -1;
	}
	
	qsee_log(QSEE_LOG_MSG_ERROR, "set_smd_quest_result_fuse SUCCESS" );
	return 0;
}

int get_smd_quest_result_fuse(uint64_t *data)
{
	uint32 qfprom_status = 0;
	uint64_t row_data_read;
	uint32 fuse_addr;

	fuse_addr = QUEST_OEM_SPARE_REGION;
#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, (uint32_t*)(uintnt)(uintnt)&row_data_read, &qfprom_status);
	if(qfprom_status != QFPROM_NO_ERR)
	{
		qsee_log(QSEE_LOG_MSG_ERROR, "QUEST read returned error: %d", qfprom_status);
		return -1;
	}
#ifdef DEBUG_LOG_ENABLE	
	else
	{
		qsee_log(QSEE_LOG_MSG_ERROR, "QUEST read value: %x %x", (uint32_t)(row_data_read >> 32), (uint32_t)row_data_read);
	}
#endif

	*data = row_data_read;

	return 0;
}
