/** @file watchdog-kapi.c
 
   Contains watchdog functions.
 
   @par CREATION  
   @author Krauth Julien
   @date   03.04.2007
   
   @par VERSION
   @verbatim
   $LastChangedRevision:$
   $LastChangedDate:$
   @endverbatim   
   
   @par LICENCE
   @verbatim
   Copyright (C) 2004,2005  ADDI-DATA GmbH for the source code of this module.
        
        ADDI-DATA GmbH 
        Dieselstrasse 3 
        D-77833 Ottersweier 
        Tel: +19(0)7223/9493-0 
        Fax: +49(0)7223/9493-92 
        http://www.addi-data-com 
        info@addi-data.com 
        
   This program is free software; you can redistribute it and/or modify it under 
   the terms of the GNU General Public License as published by the Free Software 
   Foundation; either version 2 of the License, or (at your option) any later version.

   This program is distributed in the hope that it will be useful, 
   but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 
   or FITNESS FOR A PARTICULAR PURPOSE. 
   See the GNU General Public License for more details.

   You should have received a copy of the GNU General Public License along with 
   this program; if not, write to the Free Software Foundation, 
   Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA

   You shoud also find the complete GPL in the COPYING file 
   accompanying this source code.
   @endverbatim   
 */ 

#include "apci2200-private.h"

EXPORT_SYMBOL(i_APCI2200_InitWatchdog);
EXPORT_SYMBOL(i_APCI2200_StartWatchdog);
EXPORT_SYMBOL(i_APCI2200_StopWatchdog);
EXPORT_SYMBOL(i_APCI2200_TriggerWatchdog);
EXPORT_SYMBOL(i_APCI2200_GetWatchdogStatus);
EXPORT_SYMBOL(i_APCI2200_GetWatchdogSoftwareTriggerStatus);

//----------------------------------------------------------------------------
//----------------------------------------------------------------------------

/** Initialise the watchdog.                                                 
 * 
 * @param [in] pdev         : The device to initialize.
 * @param [in] l_DelayValue : Watchdog delay value in step of 20ms (1 to 255).
 *
 * @retval 0: No error.
 * @retval 1: The handle parameter of the board is wrong.
 * @retval 2: Wrong watchdog delay value (has to be 1 to 255).
 */
int i_APCI2200_InitWatchdog (struct pci_dev *pdev, uint32_t l_DelayValue)
	{

    if (!pdev) return 1;
    
    if ((l_DelayValue < 0) || (l_DelayValue > 255)) 
    	return 2;
    
    outl(l_DelayValue, (GET_BAR1(pdev) + APCI2200_WATCHDOG + 4));

	return 0;
}    

//----------------------------------------------------------------------------

/** Start the watchdog.                                                 
 * 
 * @param [in] pdev         : The device to initialize.
 *
 * @retval 0: No error.
 * @retval 1: The handle parameter of the board is wrong.
 * @retval 2: Watchdog already started.
 */
int i_APCI2200_StartWatchdog (struct pci_dev *pdev)
	{
	uint32_t tmp = 0;

    if (!pdev) return 1;
    
    tmp = inl((GET_BAR1(pdev) + APCI2200_WATCHDOG + 12));
    
    if ((tmp & 1) == 1)
		return 2;
       
    outl(0x201, (GET_BAR1(pdev) + APCI2200_WATCHDOG + 12));

	return 0;
}  

//----------------------------------------------------------------------------

/** Stop the watchdog.                                                 
 * 
 * @param [in] pdev         : The device to initialize.
 *
 * @retval 0: No error.
 * @retval 1: The handle parameter of the board is wrong.
 * @retval 2: Watchdog not started.
 */
int i_APCI2200_StopWatchdog (struct pci_dev *pdev)
	{
	uint32_t tmp = 0;
	
    if (!pdev) return 1;
    
    tmp = inl((GET_BAR1(pdev) + APCI2200_WATCHDOG + 12));

    if ((tmp & 1) == 0)
		return 2;
		    
    outl(0, (GET_BAR1(pdev) + APCI2200_WATCHDOG + 12));

	return 0;
} 

//----------------------------------------------------------------------------

/** Trigger the watchdog.                                                 
 * 
 * @param [in] pdev         : The device to initialize.
 *
 * @retval 0: No error.
 * @retval 1: The handle parameter of the board is wrong.
 * @retval 2: Watchdog not started.
 */
int i_APCI2200_TriggerWatchdog (struct pci_dev *pdev)
	{
	uint32_t tmp = 0;
	
    if (!pdev) return 1;
    
    tmp = inl((GET_BAR1(pdev) + APCI2200_WATCHDOG + 12));

    if ((tmp & 1) == 0)
		return 2;
    		    
    outl(0x201, (GET_BAR1(pdev) + APCI2200_WATCHDOG + 12));

	return 0;
}  

//----------------------------------------------------------------------------

/** Get the watchdog status.                                                 
 * 
 * @param [in] pdev         : The device to initialize.
 * @param [out] b_Status    : Watchdog status:<br>
 *								0 : No overlow occur<br>
 *								1 : Overlow occur
 * 
 * @retval 0: No error.
 * @retval 1: The handle parameter of the board is wrong.
 * @retval 2: Watchdog not started.
 */
int i_APCI2200_GetWatchdogStatus (struct pci_dev *pdev, uint8_t *b_Status)
	{
	uint32_t tmp = 0;
	
    if (!pdev) return 1;
    
    tmp = inl((GET_BAR1(pdev) + APCI2200_WATCHDOG + 12));

    if ((tmp & 1) == 0)
		return 2;
		    
    tmp = inl((GET_BAR1(pdev) + APCI2200_WATCHDOG + 16));
    
    *b_Status = (uint8_t) (tmp & 1); 

	return 0;
} 

//----------------------------------------------------------------------------

/** Get the watchdog software trigger status.                                                 
 * 
 * @param [in] pdev         : The device to initialize.
 * @param [out] b_Status    : Watchdog software trigger status:<br>
 *								0 : No software trigger occur <br>
 *								1 : Software trigger occur
 * 
 * @retval 0: No error.
 * @retval 1: The handle parameter of the board is wrong.
 * @retval 2: Watchdog not started.
 */
int i_APCI2200_GetWatchdogSoftwareTriggerStatus (struct pci_dev *pdev, uint8_t *b_Status)
	{
	uint32_t tmp = 0;
	
    if (!pdev) return 1;
    
    tmp = inl((GET_BAR1(pdev) + APCI2200_WATCHDOG + 12));

    if ((tmp & 1) == 0)
		return 2;
		    
    tmp = inl((GET_BAR1(pdev) + APCI2200_WATCHDOG + 16));
    
    *b_Status = (uint8_t) ((tmp & 2) >> 1); 

	return 0;
} 

//----------------------------------------------------------------------------
//----------------------------------------------------------------------------



