2005-09-07 04:05:30 +04:00
/*
* W83977F Watchdog Timer Driver for Winbond W83977F I / O Chip
*
* ( c ) Copyright 2005 Jose Goncalves < jose . goncalves @ inov . pt >
*
* Based on w83877f_wdt . c by Scott Jennings ,
* and wdt977 . c by Woody Suwalski
*
* - - - - - - - - - - - - - - - - - - - - - - -
*
* 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 .
*
*/
2012-02-16 03:06:19 +04:00
# define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
2005-09-07 04:05:30 +04:00
# include <linux/module.h>
# include <linux/moduleparam.h>
# include <linux/types.h>
# include <linux/kernel.h>
# include <linux/fs.h>
# include <linux/miscdevice.h>
# include <linux/init.h>
# include <linux/ioport.h>
# include <linux/watchdog.h>
# include <linux/notifier.h>
# include <linux/reboot.h>
2008-05-19 17:09:34 +04:00
# include <linux/uaccess.h>
# include <linux/io.h>
2005-09-07 04:05:30 +04:00
# define WATCHDOG_VERSION "1.00"
# define WATCHDOG_NAME "W83977F WDT"
# define IO_INDEX_PORT 0x3F0
# define IO_DATA_PORT (IO_INDEX_PORT+1)
# define UNLOCK_DATA 0x87
# define LOCK_DATA 0xAA
# define DEVICE_REGISTER 0x07
# define DEFAULT_TIMEOUT 45 /* default timeout in seconds */
static int timeout = DEFAULT_TIMEOUT ;
static int timeoutW ; /* timeout in watchdog counter units */
static unsigned long timer_alive ;
static int testmode ;
static char expect_close ;
2007-11-02 02:27:08 +03:00
static DEFINE_SPINLOCK ( spinlock ) ;
2005-09-07 04:05:30 +04:00
module_param ( timeout , int , 0 ) ;
2008-05-19 17:09:34 +04:00
MODULE_PARM_DESC ( timeout ,
" Watchdog timeout in seconds (15..7635), default= "
__MODULE_STRING ( DEFAULT_TIMEOUT ) " ) " ) ;
2005-09-07 04:05:30 +04:00
module_param ( testmode , int , 0 ) ;
2008-05-19 17:09:34 +04:00
MODULE_PARM_DESC ( testmode , " Watchdog testmode (1 = no reboot), default=0 " ) ;
2005-09-07 04:05:30 +04:00
2012-03-05 19:51:11 +04:00
static bool nowayout = WATCHDOG_NOWAYOUT ;
module_param ( nowayout , bool , 0 ) ;
2008-05-19 17:09:34 +04:00
MODULE_PARM_DESC ( nowayout ,
" Watchdog cannot be stopped once started (default= "
__MODULE_STRING ( WATCHDOG_NOWAYOUT ) " ) " ) ;
2005-09-07 04:05:30 +04:00
/*
* Start the watchdog
*/
static int wdt_start ( void )
{
unsigned long flags ;
spin_lock_irqsave ( & spinlock , flags ) ;
/* Unlock the SuperIO chip */
2008-05-19 17:09:34 +04:00
outb_p ( UNLOCK_DATA , IO_INDEX_PORT ) ;
outb_p ( UNLOCK_DATA , IO_INDEX_PORT ) ;
2005-09-07 04:05:30 +04:00
/*
* Select device Aux2 ( device = 8 ) to set watchdog regs F2 , F3 and F4 .
* F2 has the timeout in watchdog counter units .
* F3 is set to enable watchdog LED blink at timeout .
* F4 is used to just clear the TIMEOUT ' ed state ( bit 0 ) .
*/
2008-05-19 17:09:34 +04:00
outb_p ( DEVICE_REGISTER , IO_INDEX_PORT ) ;
outb_p ( 0x08 , IO_DATA_PORT ) ;
outb_p ( 0xF2 , IO_INDEX_PORT ) ;
outb_p ( timeoutW , IO_DATA_PORT ) ;
outb_p ( 0xF3 , IO_INDEX_PORT ) ;
outb_p ( 0x08 , IO_DATA_PORT ) ;
outb_p ( 0xF4 , IO_INDEX_PORT ) ;
outb_p ( 0x00 , IO_DATA_PORT ) ;
2005-09-07 04:05:30 +04:00
/* Set device Aux2 active */
2008-05-19 17:09:34 +04:00
outb_p ( 0x30 , IO_INDEX_PORT ) ;
outb_p ( 0x01 , IO_DATA_PORT ) ;
2005-09-07 04:05:30 +04:00
2008-05-19 17:09:34 +04:00
/*
2005-09-07 04:05:30 +04:00
* Select device Aux1 ( dev = 7 ) to set GP16 as the watchdog output
* ( in reg E6 ) and GP13 as the watchdog LED output ( in reg E3 ) .
* Map GP16 at pin 119.
* In test mode watch the bit 0 on F4 to indicate " triggered " or
* check watchdog LED on SBC .
*/
2008-05-19 17:09:34 +04:00
outb_p ( DEVICE_REGISTER , IO_INDEX_PORT ) ;
outb_p ( 0x07 , IO_DATA_PORT ) ;
if ( ! testmode ) {
2005-09-07 04:05:30 +04:00
unsigned pin_map ;
2008-05-19 17:09:34 +04:00
outb_p ( 0xE6 , IO_INDEX_PORT ) ;
outb_p ( 0x0A , IO_DATA_PORT ) ;
outb_p ( 0x2C , IO_INDEX_PORT ) ;
2005-09-07 04:05:30 +04:00
pin_map = inb_p ( IO_DATA_PORT ) ;
pin_map | = 0x10 ;
pin_map & = ~ ( 0x20 ) ;
2008-05-19 17:09:34 +04:00
outb_p ( 0x2C , IO_INDEX_PORT ) ;
outb_p ( pin_map , IO_DATA_PORT ) ;
2005-09-07 04:05:30 +04:00
}
2008-05-19 17:09:34 +04:00
outb_p ( 0xE3 , IO_INDEX_PORT ) ;
outb_p ( 0x08 , IO_DATA_PORT ) ;
2005-09-07 04:05:30 +04:00
/* Set device Aux1 active */
2008-05-19 17:09:34 +04:00
outb_p ( 0x30 , IO_INDEX_PORT ) ;
outb_p ( 0x01 , IO_DATA_PORT ) ;
2005-09-07 04:05:30 +04:00
/* Lock the SuperIO chip */
2008-05-19 17:09:34 +04:00
outb_p ( LOCK_DATA , IO_INDEX_PORT ) ;
2005-09-07 04:05:30 +04:00
spin_unlock_irqrestore ( & spinlock , flags ) ;
2012-02-16 03:06:19 +04:00
pr_info ( " activated \n " ) ;
2005-09-07 04:05:30 +04:00
return 0 ;
}
/*
* Stop the watchdog
*/
static int wdt_stop ( void )
{
unsigned long flags ;
spin_lock_irqsave ( & spinlock , flags ) ;
/* Unlock the SuperIO chip */
2008-05-19 17:09:34 +04:00
outb_p ( UNLOCK_DATA , IO_INDEX_PORT ) ;
outb_p ( UNLOCK_DATA , IO_INDEX_PORT ) ;
2005-09-07 04:05:30 +04:00
2008-05-19 17:09:34 +04:00
/*
2005-09-07 04:05:30 +04:00
* Select device Aux2 ( device = 8 ) to set watchdog regs F2 , F3 and F4 .
* F2 is reset to its default value ( watchdog timer disabled ) .
* F3 is reset to its default state .
* F4 clears the TIMEOUT ' ed state ( bit 0 ) - back to default .
*/
2008-05-19 17:09:34 +04:00
outb_p ( DEVICE_REGISTER , IO_INDEX_PORT ) ;
outb_p ( 0x08 , IO_DATA_PORT ) ;
outb_p ( 0xF2 , IO_INDEX_PORT ) ;
outb_p ( 0xFF , IO_DATA_PORT ) ;
outb_p ( 0xF3 , IO_INDEX_PORT ) ;
outb_p ( 0x00 , IO_DATA_PORT ) ;
outb_p ( 0xF4 , IO_INDEX_PORT ) ;
outb_p ( 0x00 , IO_DATA_PORT ) ;
outb_p ( 0xF2 , IO_INDEX_PORT ) ;
outb_p ( 0x00 , IO_DATA_PORT ) ;
2005-09-07 04:05:30 +04:00
/*
2008-05-19 17:09:34 +04:00
* Select device Aux1 ( dev = 7 ) to set GP16 ( in reg E6 ) and
2005-09-07 04:05:30 +04:00
* Gp13 ( in reg E3 ) as inputs .
*/
2008-05-19 17:09:34 +04:00
outb_p ( DEVICE_REGISTER , IO_INDEX_PORT ) ;
outb_p ( 0x07 , IO_DATA_PORT ) ;
if ( ! testmode ) {
outb_p ( 0xE6 , IO_INDEX_PORT ) ;
outb_p ( 0x01 , IO_DATA_PORT ) ;
2005-09-07 04:05:30 +04:00
}
2008-05-19 17:09:34 +04:00
outb_p ( 0xE3 , IO_INDEX_PORT ) ;
outb_p ( 0x01 , IO_DATA_PORT ) ;
2005-09-07 04:05:30 +04:00
/* Lock the SuperIO chip */
2008-05-19 17:09:34 +04:00
outb_p ( LOCK_DATA , IO_INDEX_PORT ) ;
2005-09-07 04:05:30 +04:00
spin_unlock_irqrestore ( & spinlock , flags ) ;
2012-02-16 03:06:19 +04:00
pr_info ( " shutdown \n " ) ;
2005-09-07 04:05:30 +04:00
return 0 ;
}
/*
* Send a keepalive ping to the watchdog
* This is done by simply re - writing the timeout to reg . 0xF2
*/
static int wdt_keepalive ( void )
{
unsigned long flags ;
spin_lock_irqsave ( & spinlock , flags ) ;
/* Unlock the SuperIO chip */
2008-05-19 17:09:34 +04:00
outb_p ( UNLOCK_DATA , IO_INDEX_PORT ) ;
outb_p ( UNLOCK_DATA , IO_INDEX_PORT ) ;
2005-09-07 04:05:30 +04:00
/* Select device Aux2 (device=8) to kick watchdog reg F2 */
2008-05-19 17:09:34 +04:00
outb_p ( DEVICE_REGISTER , IO_INDEX_PORT ) ;
outb_p ( 0x08 , IO_DATA_PORT ) ;
outb_p ( 0xF2 , IO_INDEX_PORT ) ;
outb_p ( timeoutW , IO_DATA_PORT ) ;
2005-09-07 04:05:30 +04:00
/* Lock the SuperIO chip */
2008-05-19 17:09:34 +04:00
outb_p ( LOCK_DATA , IO_INDEX_PORT ) ;
2005-09-07 04:05:30 +04:00
spin_unlock_irqrestore ( & spinlock , flags ) ;
return 0 ;
}
/*
* Set the watchdog timeout value
*/
static int wdt_set_timeout ( int t )
{
int tmrval ;
/*
* Convert seconds to watchdog counter time units , rounding up .
2008-05-19 17:09:34 +04:00
* On PCM - 5335 watchdog units are 30 seconds / step with 15 sec startup
2005-09-07 04:05:30 +04:00
* value . This information is supplied in the PCM - 5335 manual and was
* checked by me on a real board . This is a bit strange because W83977f
* datasheet says counter unit is in minutes !
*/
if ( t < 15 )
return - EINVAL ;
tmrval = ( ( t + 15 ) + 29 ) / 30 ;
if ( tmrval > 255 )
return - EINVAL ;
/*
2008-05-19 17:09:34 +04:00
* timeout is the timeout in seconds ,
2005-09-07 04:05:30 +04:00
* timeoutW is the timeout in watchdog counter units .
*/
timeoutW = tmrval ;
timeout = ( timeoutW * 30 ) - 15 ;
return 0 ;
}
/*
* Get the watchdog status
*/
static int wdt_get_status ( int * status )
{
int new_status ;
unsigned long flags ;
spin_lock_irqsave ( & spinlock , flags ) ;
/* Unlock the SuperIO chip */
2008-05-19 17:09:34 +04:00
outb_p ( UNLOCK_DATA , IO_INDEX_PORT ) ;
outb_p ( UNLOCK_DATA , IO_INDEX_PORT ) ;
2005-09-07 04:05:30 +04:00
/* Select device Aux2 (device=8) to read watchdog reg F4 */
2008-05-19 17:09:34 +04:00
outb_p ( DEVICE_REGISTER , IO_INDEX_PORT ) ;
outb_p ( 0x08 , IO_DATA_PORT ) ;
outb_p ( 0xF4 , IO_INDEX_PORT ) ;
2005-09-07 04:05:30 +04:00
new_status = inb_p ( IO_DATA_PORT ) ;
/* Lock the SuperIO chip */
2008-05-19 17:09:34 +04:00
outb_p ( LOCK_DATA , IO_INDEX_PORT ) ;
2005-09-07 04:05:30 +04:00
spin_unlock_irqrestore ( & spinlock , flags ) ;
* status = 0 ;
if ( new_status & 1 )
* status | = WDIOF_CARDRESET ;
return 0 ;
}
/*
* / dev / watchdog handling
*/
static int wdt_open ( struct inode * inode , struct file * file )
{
/* If the watchdog is alive we don't need to start it again */
2008-05-19 17:09:34 +04:00
if ( test_and_set_bit ( 0 , & timer_alive ) )
2005-09-07 04:05:30 +04:00
return - EBUSY ;
if ( nowayout )
__module_get ( THIS_MODULE ) ;
wdt_start ( ) ;
return nonseekable_open ( inode , file ) ;
}
static int wdt_release ( struct inode * inode , struct file * file )
{
/*
* Shut off the timer .
* Lock it in if it ' s a module and we set nowayout
*/
2008-05-19 17:09:34 +04:00
if ( expect_close = = 42 ) {
2005-09-07 04:05:30 +04:00
wdt_stop ( ) ;
clear_bit ( 0 , & timer_alive ) ;
} else {
wdt_keepalive ( ) ;
2012-02-16 03:06:19 +04:00
pr_crit ( " unexpected close, not stopping watchdog! \n " ) ;
2005-09-07 04:05:30 +04:00
}
expect_close = 0 ;
return 0 ;
}
/*
* wdt_write :
* @ file : file handle to the watchdog
* @ buf : buffer to write ( unused as data does not matter here
* @ count : count of bytes
* @ ppos : pointer to the position to write . No seeks allowed
*
* A write to a watchdog device is defined as a keepalive signal . Any
* write of data will do , as we we don ' t define content meaning .
*/
static ssize_t wdt_write ( struct file * file , const char __user * buf ,
size_t count , loff_t * ppos )
{
/* See if we got the magic character 'V' and reload the timer */
2008-05-19 17:09:34 +04:00
if ( count ) {
if ( ! nowayout ) {
2005-09-07 04:05:30 +04:00
size_t ofs ;
2008-05-19 17:09:34 +04:00
/* note: just in case someone wrote the
magic character long ago */
2005-09-07 04:05:30 +04:00
expect_close = 0 ;
2008-05-19 17:09:34 +04:00
/* scan to see whether or not we got the
magic character */
for ( ofs = 0 ; ofs ! = count ; ofs + + ) {
2005-09-07 04:05:30 +04:00
char c ;
if ( get_user ( c , buf + ofs ) )
return - EFAULT ;
2008-05-19 17:09:34 +04:00
if ( c = = ' V ' )
2005-09-07 04:05:30 +04:00
expect_close = 42 ;
}
}
/* someone wrote to us, we should restart timer */
wdt_keepalive ( ) ;
}
return count ;
}
/*
* wdt_ioctl :
* @ inode : inode of the device
* @ file : file handle to the device
* @ cmd : watchdog command
* @ arg : argument pointer
*
* The watchdog API defines a common set of functions for all watchdogs
* according to their available features .
*/
2009-12-26 21:55:22 +03:00
static const struct watchdog_info ident = {
2005-09-07 04:05:30 +04:00
. options = WDIOF_SETTIMEOUT | WDIOF_MAGICCLOSE | WDIOF_KEEPALIVEPING ,
. firmware_version = 1 ,
. identity = WATCHDOG_NAME ,
} ;
2008-05-19 17:09:34 +04:00
static long wdt_ioctl ( struct file * file , unsigned int cmd , unsigned long arg )
2005-09-07 04:05:30 +04:00
{
int status ;
int new_options , retval = - EINVAL ;
int new_timeout ;
union {
struct watchdog_info __user * ident ;
int __user * i ;
} uarg ;
uarg . i = ( int __user * ) arg ;
2008-05-19 17:09:34 +04:00
switch ( cmd ) {
2005-09-07 04:05:30 +04:00
case WDIOC_GETSUPPORT :
2008-05-19 17:09:34 +04:00
return copy_to_user ( uarg . ident , & ident ,
sizeof ( ident ) ) ? - EFAULT : 0 ;
2005-09-07 04:05:30 +04:00
case WDIOC_GETSTATUS :
wdt_get_status ( & status ) ;
return put_user ( status , uarg . i ) ;
case WDIOC_GETBOOTSTATUS :
return put_user ( 0 , uarg . i ) ;
case WDIOC_SETOPTIONS :
2008-05-19 17:09:34 +04:00
if ( get_user ( new_options , uarg . i ) )
2005-09-07 04:05:30 +04:00
return - EFAULT ;
if ( new_options & WDIOS_DISABLECARD ) {
wdt_stop ( ) ;
retval = 0 ;
}
if ( new_options & WDIOS_ENABLECARD ) {
wdt_start ( ) ;
retval = 0 ;
}
return retval ;
2008-07-18 15:41:17 +04:00
case WDIOC_KEEPALIVE :
wdt_keepalive ( ) ;
return 0 ;
2005-09-07 04:05:30 +04:00
case WDIOC_SETTIMEOUT :
if ( get_user ( new_timeout , uarg . i ) )
return - EFAULT ;
if ( wdt_set_timeout ( new_timeout ) )
2009-03-18 11:35:09 +03:00
return - EINVAL ;
2005-09-07 04:05:30 +04:00
wdt_keepalive ( ) ;
/* Fall */
case WDIOC_GETTIMEOUT :
return put_user ( timeout , uarg . i ) ;
2008-07-18 15:41:17 +04:00
default :
return - ENOTTY ;
2005-09-07 04:05:30 +04:00
}
}
static int wdt_notify_sys ( struct notifier_block * this , unsigned long code ,
void * unused )
{
2008-05-19 17:09:34 +04:00
if ( code = = SYS_DOWN | | code = = SYS_HALT )
2005-09-07 04:05:30 +04:00
wdt_stop ( ) ;
return NOTIFY_DONE ;
}
2008-05-19 17:09:34 +04:00
static const struct file_operations wdt_fops = {
2005-09-07 04:05:30 +04:00
. owner = THIS_MODULE ,
. llseek = no_llseek ,
. write = wdt_write ,
2008-05-19 17:09:34 +04:00
. unlocked_ioctl = wdt_ioctl ,
2005-09-07 04:05:30 +04:00
. open = wdt_open ,
. release = wdt_release ,
} ;
2008-05-19 17:09:34 +04:00
static struct miscdevice wdt_miscdev = {
2005-09-07 04:05:30 +04:00
. minor = WATCHDOG_MINOR ,
. name = " watchdog " ,
. fops = & wdt_fops ,
} ;
static struct notifier_block wdt_notifier = {
. notifier_call = wdt_notify_sys ,
} ;
static int __init w83977f_wdt_init ( void )
{
int rc ;
2012-02-16 03:06:19 +04:00
pr_info ( " driver v%s \n " , WATCHDOG_VERSION ) ;
2005-09-07 04:05:30 +04:00
/*
2008-05-19 17:09:34 +04:00
* Check that the timeout value is within it ' s range ;
2005-09-07 04:05:30 +04:00
* if not reset to the default
*/
if ( wdt_set_timeout ( timeout ) ) {
wdt_set_timeout ( DEFAULT_TIMEOUT ) ;
2012-02-16 03:06:19 +04:00
pr_info ( " timeout value must be 15 <= timeout <= 7635, using %d \n " ,
DEFAULT_TIMEOUT ) ;
2005-09-07 04:05:30 +04:00
}
2008-05-19 17:09:34 +04:00
if ( ! request_region ( IO_INDEX_PORT , 2 , WATCHDOG_NAME ) ) {
2012-02-16 03:06:19 +04:00
pr_err ( " I/O address 0x%04x already in use \n " , IO_INDEX_PORT ) ;
2005-09-07 04:05:30 +04:00
rc = - EIO ;
goto err_out ;
}
2007-12-26 23:32:51 +03:00
rc = register_reboot_notifier ( & wdt_notifier ) ;
2008-05-19 17:09:34 +04:00
if ( rc ) {
2012-02-16 03:06:19 +04:00
pr_err ( " cannot register reboot notifier (err=%d) \n " , rc ) ;
2005-09-07 04:05:30 +04:00
goto err_out_region ;
}
2007-12-26 23:32:51 +03:00
rc = misc_register ( & wdt_miscdev ) ;
2008-05-19 17:09:34 +04:00
if ( rc ) {
2012-02-16 03:06:19 +04:00
pr_err ( " cannot register miscdev on minor=%d (err=%d) \n " ,
wdt_miscdev . minor , rc ) ;
2007-12-26 23:32:51 +03:00
goto err_out_reboot ;
2005-09-07 04:05:30 +04:00
}
2012-02-16 03:06:19 +04:00
pr_info ( " initialized. timeout=%d sec (nowayout=%d testmode=%d) \n " ,
timeout , nowayout , testmode ) ;
2005-09-07 04:05:30 +04:00
return 0 ;
2007-12-26 23:32:51 +03:00
err_out_reboot :
unregister_reboot_notifier ( & wdt_notifier ) ;
2005-09-07 04:05:30 +04:00
err_out_region :
2008-05-19 17:09:34 +04:00
release_region ( IO_INDEX_PORT , 2 ) ;
2005-09-07 04:05:30 +04:00
err_out :
return rc ;
}
static void __exit w83977f_wdt_exit ( void )
{
wdt_stop ( ) ;
misc_deregister ( & wdt_miscdev ) ;
unregister_reboot_notifier ( & wdt_notifier ) ;
2008-05-19 17:09:34 +04:00
release_region ( IO_INDEX_PORT , 2 ) ;
2005-09-07 04:05:30 +04:00
}
module_init ( w83977f_wdt_init ) ;
module_exit ( w83977f_wdt_exit ) ;
MODULE_AUTHOR ( " Jose Goncalves <jose.goncalves@inov.pt> " ) ;
MODULE_DESCRIPTION ( " Driver for watchdog timer in W83977F I/O chip " ) ;
MODULE_LICENSE ( " GPL " ) ;
MODULE_ALIAS_MISCDEV ( WATCHDOG_MINOR ) ;