 /*  keapiInit.c - KEAPI-compliant BSP initialization for MSMST */

/*
 * Copyright 2011 Kontron KGSC.
 */

/*
 modification history
 --------------------
  01d,07jul11,ivo MSMST support
  01c,11may11,ivo nanoETXsp support
  01b,18mar11,edv embed to BSP
  01b,15mar11,edv use Kontron 3rdparty interface
  01a,21feb11,edv initial version
 */

/*
DESCRIPTION:
    KEAPI v1.2.0.3 implementation. Initialization.
*/

#include <vxWorks.h>
#include <config.h>

#include <vxCpuLib.h>
#include <taskLib.h>
#include <cpuset.h>
#include <arch/i86/pentiumLib.h>
#include <hwif/util/vxbParamSys.h>	/* for vxbInstByNameFind() */

#include <stdio.h>

#include <../3rdparty/kontron/h/keapiVxW.h>
#include <../3rdparty/kontron/KEAPI/keapiBSPRtn.h>
#include "keapi/keapiBSPCommon.h"

struct KEAPI_Libinfo *pKEAPIInfo;

IMPORT STATUS keapiSMBusInitialize(void);

LOCAL BOOL keapiInitialized = FALSE;

LOCAL INT32 getCoreTemp()
    {
    LL_UNION msrValue;
    int coreTemp = 0;
	
    pentiumMsrGet (IA32_THERM_STATUS, &msrValue.i64);
    /*(printf("msr 0x%8.8x 0x%8.8x\n", msrValue.i32[0], msrValue.i32[1]);*/
    
    if (msrValue.i32[0] & 0x80000000)
    	{
		coreTemp = 100000 - (((msrValue.i32[0] >> 16) & 0x7f) * 1000);
    	}
    return coreTemp;
    }

IMPORT KEAPI_RETVAL keapiImpGetCoreTemp(int core, KEAPI_PINT32 pTemp)
    {
    pTemp[0] = getCoreTemp();
    return KEAPI_RET_SUCCESS;
    }

LOCAL void initRtn()
    {
    _keapiGetTempSensorCount_Rtn = keapiGetTempSensorCount;
    _keapiGetTempSensorList_Rtn = keapiGetTempSensorList;
    _keapiGetTempSensorValue_Rtn = keapiGetTempSensorValue;
    _keapiGetCpuTemp_Rtn = keapiGetCpuTemp;
    _keapiGetChipsetTemp_Rtn = keapiGetChipsetTemp;
    _keapiGetSystemTemp_Rtn = keapiGetSystemTemp;
   
    _keapiGetStorageCount_Rtn = keapiGetStorageCount;
    _keapiGetStorageSize_Rtn = keapiGetStorageSize;
    _keapiStorageRead_Rtn = keapiStorageRead;
    _keapiStorageWrite_Rtn = keapiStorageWrite;
       
    _keapiGetSmbusCount_Rtn = keapiGetSmbusCount; 
    _keapiSmbusQuickCommand_Rtn = keapiSmbusQuickCommand;
    _keapiSmbusSendByte_Rtn = keapiSmbusSendByte;
    _keapiSmbusReceiveByte_Rtn = keapiSmbusReceiveByte;
    _keapiSmbusWriteByte_Rtn = keapiSmbusWriteByte;
    _keapiSmbusReadByte_Rtn = keapiSmbusReadByte;
    _keapiSmbusWriteWord_Rtn = keapiSmbusWriteWord;  
    _keapiSmbusReadWord_Rtn = keapiSmbusReadWord;
    _keapiSmbusWriteBlock_Rtn = keapiSmbusWriteBlock;
    _keapiSmbusReadBlock_Rtn = keapiSmbusReadBlock;

    _keapiWatchdogEnable_Rtn = keapiWatchdogEnable;
    _keapiWatchdogDisable_Rtn = keapiWatchdogDisable;
    _keapiWatchdogTrigger_Rtn = keapiWatchdogTrigger;
    }

/**
* keapiLibInitialize - Initialization of Kontron EAPI.
* 
* RETURNS:          
* - KEAPI_RET_SUCCESS on success
* - KEAPI_RET_ERROR
* 
* ERRNO
* 
*/
KEAPI_RETVAL keapiLibInitialize(void)
    {
	
    if (keapiInitialized) 
        {
        return KEAPI_RET_SUCCESS;
        }
    
    pKEAPIInfo = calloc(sizeof(pKEAPIInfo[0]), 1);
    if (pKEAPIInfo == NULL)
    	{
		KEAPI_DBG("keapiLibInitialize failed \n", 0,0,0,0,0,0);
		return KEAPI_RET_ERROR;
    	}
    
    if (semMInit (&pKEAPIInfo->storageAccess,
    		SEM_Q_PRIORITY | SEM_DELETE_SAFE | SEM_INVERSION_SAFE) != OK)
    	{
		free(pKEAPIInfo);
    	pKEAPIInfo = NULL;
		KEAPI_DBG("keapiInstConnect ERROR: semMInit() failed \n", 0,0,0,0,0,0);
		return KEAPI_RET_ERROR;
    	}
    
    if (keapiSMBusInitialize() != OK)
    	{
		free(pKEAPIInfo);
		pKEAPIInfo = NULL;
	    KEAPI_DBG("keapiSMBusInitialize failed \n", 0,0,0,0,0,0);
	    return KEAPI_RET_ERROR;
    	}

    initRtn();

    keapiInitialized = TRUE;
    
    return KEAPI_RET_SUCCESS;
    }

