/*  keapiBSP.h - KEAPI internal definitions for kontron_MSMST BSP  */

/*
 * Copyright 2011 Kontron KGSC.
 */

/*
 modification history
 --------------------
 01c,07jul11,ivo update for MSMST
 01b,12may11,ivo update for nanoETXsp
 01a,02may11,edv initial version
*/
/*
DESCRIPTION:
    this is internal definitions for each BSP module
    usage:
#define KEAPI_BSP_xxx
#include "keapi/keapiBSP.h"
#undef  KEAPI_BSP_xxx   
*/

#ifdef KEAPI_BSP_SENSORS
LOCAL const KEAPI_CHAR *tsensorNames[] =
    {
    	"SYS-Temp", "CPU-Temp",   
    	"Core-Temp"
    };

/* 3 for UP */
#define TSENSORS_COUNT NELEMENTS(tsensorNames)
#define SENSOR_TEMP_TYPE INT32

#define W83L771W_ADDR		(0x98/2)	/* HM Sbus address */
#define W83L771W_LT_REG		0x00		/* Local temperature */
#define W83L771W_RTHB_REG	0x01		/* Remote temperature high byte */
#define W83L771W_RTLB_REG	0x10		/* Remote temperature low byte */

#define SYSTEM_SENSOR 	(0)
#define CPU_SENSOR		(1)
#define CHIPSET_SENSOR 	(0)
#define CORE_SENSOR		(2)

LOCAL STATUS GET_TEMP_SENSOR(int n, SENSOR_TEMP_TYPE *value)
	{
	UINT8 lowByte, highByte;
	INT16 val;

	switch(n) {
	case SYSTEM_SENSOR: /* Local on W83L771W_ADDR */
		keapiSmbusReadByte(0, W83L771W_ADDR, W83L771W_LT_REG, &highByte);
		val = highByte << 8;
		break;
	case CPU_SENSOR: /* Remote on W83L771W_ADDR */
		keapiSmbusReadByte(0, W83L771W_ADDR, W83L771W_RTHB_REG, &highByte);
		keapiSmbusReadByte(0, W83L771W_ADDR, W83L771W_RTLB_REG, &lowByte);
		val = (highByte << 8) | lowByte;
		break;
	case CORE_SENSOR:
		return keapiImpGetCoreTemp(n - CORE_SENSOR, value);
	default:
		return ERROR; /* impossible */
	}
	*value = val;
	return OK;
	}

/* High byte contains degrees */
#define TEMP_SENSOR_TO_MILLIDEGREES(n, val) \
	(n == CORE_SENSOR ? val : val * 1000 / 256)
#define TEMP_SENSORS_INITED (pKEAPIInfo!=NULL)
#define TEMP_SENSORS_UPDATE

#endif /* KEAPI_BSP_SENSORS */

#ifdef KEAPI_BSP_WATCHDOG
/* Timeout value interval in milliseconds, new CPLD supports only
 * the values less than 0x7f (minutes/seconds).
*/
#define WATCHDOG_RESG_INITED	(pKEAPIInfo!=NULL)
#define KEAPI_MAXIMUM_WATCHDOG	(0x7f*60*1000)
#define WATCHDOG_MAX_VALUE		(0x7f)
#define WATCHDOG_ENABLE(timeout) /* milliseconds ! */ \
		{ \
		int tmp = timeout/1000; \
		if (tmp > WATCHDOG_MAX_VALUE) \
			{ \
			tmp = (tmp / 60) | CPLD_WATCHDOG_MINUTE; \
			} \
		pKEAPIInfo->watchdogValue = tmp; \
		sysOutByte(CPLD_WATCHDOG_ADDR, pKEAPIInfo->watchdogValue); \
		}
		
#define WATCHDOG_TRIGGER() \
	sysOutByte(CPLD_WATCHDOG_ADDR, pKEAPIInfo->watchdogValue)
		
#define WATCHDOG_DISABLE()	sysOutByte(CPLD_WATCHDOG_ADDR, 0)

#endif /* KEAPI_BSP_WATCHDOG */

#ifdef KEAPI_BSP_STORAGE
#define STORAGE_COUNT	1
/* The same solution as in the Windows */
#define USER_EEPROM_SIZE	(4096-1024)	/* 3k user bytes from 4Kb */
#define STORAGE_SIZE(n) USER_EEPROM_SIZE	
#define EEPROM_OFFSET	1024
#define EEPROM_ADDR		0x57

#define STORAGE_READ(n, offset, buffer, size, result) \
	/* We can use only byte read operations */ \
	do { \
		UINT32 _uoffset = offset; \
		INT32 _length = size; \
		KEAPI_RETVAL retval = KEAPI_RET_SUCCESS; \
		result = KEAPI_RET_PARAM_ERROR; \
		if ((size) + (_uoffset) > USER_EEPROM_SIZE) \
			break; \
		_uoffset += EEPROM_OFFSET; \
		semTake(&pKEAPIInfo->storageAccess, WAIT_FOREVER); \
		for (;_length > 0; buffer++, _uoffset++, _length--) { \
			retval = keapiSmbusWriteByte(0, EEPROM_ADDR, \
					(_uoffset >> 8) & 0xff, _uoffset & 0xff);\
			if (retval != KEAPI_RET_SUCCESS) \
				break; \
			vxbUsDelay(10); \
			retval = keapiSmbusReceiveByte(0,EEPROM_ADDR, buffer); \
			if (retval != KEAPI_RET_SUCCESS) \
				break; \
			vxbUsDelay(10); \
		} \
		semGive(&pKEAPIInfo->storageAccess); \
		result = retval; \
	} while(0)

#define STORAGE_WRITE(n, offset, buffer, size, result) \
	/* We can use only byte write operations */ \
	do { \
		UINT32 _uoffset = offset; \
		KEAPI_RETVAL retval = KEAPI_RET_SUCCESS; \
		INT32 _length = size; \
		result = KEAPI_RET_PARAM_ERROR; \
		if ((size) + (_uoffset) > USER_EEPROM_SIZE) \
			break; \
		_uoffset += EEPROM_OFFSET; \
		semTake(&pKEAPIInfo->storageAccess, WAIT_FOREVER); \
		for (;_length > 0; buffer++, _uoffset++, _length--) { \
			retval = keapiSmbusWriteWord(0, EEPROM_ADDR, \
							(_uoffset >> 8) & 0xff, \
							(_uoffset & 0xff) | (buffer[0] << 8)); \
			if (retval != KEAPI_RET_SUCCESS) \
				break; \
			vxbUsDelay(20); \
		} \
		semGive(&pKEAPIInfo->storageAccess); \
		result = retval; \
	} while(0)
		
#endif /* KEAPI_BSP_STORAGE */
		
