1/* 2 * Watchdog driver for Cirrus Logic EP93xx family of devices. 3 * 4 * Copyright (c) 2004 Ray Lehtiniemi 5 * Copyright (c) 2006 Tower Technologies 6 * Based on ep93xx driver, bits from alim7101_wdt.c 7 * 8 * Authors: Ray Lehtiniemi <rayl@mail.com>, 9 * Alessandro Zummo <a.zummo@towertech.it> 10 * 11 * This file is licensed under the terms of the GNU General Public 12 * License version 2. This program is licensed "as is" without any 13 * warranty of any kind, whether express or implied. 14 * 15 * This watchdog fires after 250msec, which is a too short interval 16 * for us to rely on the user space daemon alone. So we ping the 17 * wdt each ~200msec and eventually stop doing it if the user space 18 * daemon dies. 19 * 20 * TODO: 21 * 22 * - Test last reset from watchdog status 23 * - Add a few missing ioctls 24 */ 25 26#include <linux/module.h> 27#include <linux/fs.h> 28#include <linux/miscdevice.h> 29#include <linux/watchdog.h> 30#include <linux/timer.h> 31 32#include <asm/hardware.h> 33#include <asm/uaccess.h> 34 35#define WDT_VERSION "0.3" 36#define PFX "ep93xx_wdt: " 37 38/* default timeout (secs) */ 39#define WDT_TIMEOUT 30 40 41static int nowayout = WATCHDOG_NOWAYOUT; 42static int timeout = WDT_TIMEOUT; 43 44static struct timer_list timer; 45static unsigned long next_heartbeat; 46static unsigned long wdt_status; 47static unsigned long boot_status; 48 49#define WDT_IN_USE 0 50#define WDT_OK_TO_CLOSE 1 51 52#define EP93XX_WDT_REG(x) (EP93XX_WATCHDOG_BASE + (x)) 53#define EP93XX_WDT_WATCHDOG EP93XX_WDT_REG(0x00) 54#define EP93XX_WDT_WDSTATUS EP93XX_WDT_REG(0x04) 55 56/* reset the wdt every ~200ms */ 57#define WDT_INTERVAL (HZ/5) 58 59static void wdt_enable(void) 60{ 61 __raw_writew(0xaaaa, EP93XX_WDT_WATCHDOG); 62} 63 64static void wdt_disable(void) 65{ 66 __raw_writew(0xaa55, EP93XX_WDT_WATCHDOG); 67} 68 69static inline void wdt_ping(void) 70{ 71 __raw_writew(0x5555, EP93XX_WDT_WATCHDOG); 72} 73 74static void wdt_startup(void) 75{ 76 next_heartbeat = jiffies + (timeout * HZ); 77 78 wdt_enable(); 79 mod_timer(&timer, jiffies + WDT_INTERVAL); 80} 81 82static void wdt_shutdown(void) 83{ 84 del_timer_sync(&timer); 85 wdt_disable(); 86} 87 88static void wdt_keepalive(void) 89{ 90 /* user land ping */ 91 next_heartbeat = jiffies + (timeout * HZ); 92} 93 94static int ep93xx_wdt_open(struct inode *inode, struct file *file) 95{ 96 if (test_and_set_bit(WDT_IN_USE, &wdt_status)) 97 return -EBUSY; 98 99 clear_bit(WDT_OK_TO_CLOSE, &wdt_status); 100 101 wdt_startup(); 102 103 return nonseekable_open(inode, file); 104} 105 106static ssize_t 107ep93xx_wdt_write(struct file *file, const char __user *data, size_t len, 108 loff_t *ppos) 109{ 110 /* Can't seek (pwrite) on this device */ 111 if (*ppos != file->f_pos) 112 return -ESPIPE; 113 114 if (len) { 115 if (!nowayout) { 116 size_t i; 117 118 clear_bit(WDT_OK_TO_CLOSE, &wdt_status); 119 120 for (i = 0; i != len; i++) { 121 char c; 122 123 if (get_user(c, data + i)) 124 return -EFAULT; 125 126 if (c == 'V') 127 set_bit(WDT_OK_TO_CLOSE, &wdt_status); 128 else 129 clear_bit(WDT_OK_TO_CLOSE, &wdt_status); 130 } 131 } 132 wdt_keepalive(); 133 } 134 135 return len; 136} 137 138static struct watchdog_info ident = { 139 .options = WDIOF_CARDRESET | WDIOF_MAGICCLOSE, 140 .identity = "EP93xx Watchdog", 141}; 142 143static int 144ep93xx_wdt_ioctl(struct inode *inode, struct file *file, unsigned int cmd, 145 unsigned long arg) 146{ 147 int ret = -ENOTTY; 148 149 switch (cmd) { 150 case WDIOC_GETSUPPORT: 151 ret = copy_to_user((struct watchdog_info __user *)arg, &ident, 152 sizeof(ident)) ? -EFAULT : 0; 153 break; 154 155 case WDIOC_GETSTATUS: 156 ret = put_user(0, (int __user *)arg); 157 break; 158 159 case WDIOC_GETBOOTSTATUS: 160 ret = put_user(boot_status, (int __user *)arg); 161 break; 162 163 case WDIOC_GETTIMEOUT: 164 /* actually, it is 0.250 seconds.... */ 165 ret = put_user(1, (int __user *)arg); 166 break; 167 168 case WDIOC_KEEPALIVE: 169 wdt_keepalive(); 170 ret = 0; 171 break; 172 } 173 return ret; 174} 175 176static int ep93xx_wdt_release(struct inode *inode, struct file *file) 177{ 178 if (test_bit(WDT_OK_TO_CLOSE, &wdt_status)) 179 wdt_shutdown(); 180 else 181 printk(KERN_CRIT PFX "Device closed unexpectedly - " 182 "timer will not stop\n"); 183 184 clear_bit(WDT_IN_USE, &wdt_status); 185 clear_bit(WDT_OK_TO_CLOSE, &wdt_status); 186 187 return 0; 188} 189 190static const struct file_operations ep93xx_wdt_fops = { 191 .owner = THIS_MODULE, 192 .write = ep93xx_wdt_write, 193 .ioctl = ep93xx_wdt_ioctl, 194 .open = ep93xx_wdt_open, 195 .release = ep93xx_wdt_release, 196}; 197 198static struct miscdevice ep93xx_wdt_miscdev = { 199 .minor = WATCHDOG_MINOR, 200 .name = "watchdog", 201 .fops = &ep93xx_wdt_fops, 202}; 203 204static void ep93xx_timer_ping(unsigned long data) 205{ 206 if (time_before(jiffies, next_heartbeat)) 207 wdt_ping(); 208 209 /* Re-set the timer interval */ 210 mod_timer(&timer, jiffies + WDT_INTERVAL); 211} 212 213static int __init ep93xx_wdt_init(void) 214{ 215 int err; 216 217 err = misc_register(&ep93xx_wdt_miscdev); 218 219 boot_status = __raw_readl(EP93XX_WDT_WATCHDOG) & 0x01 ? 1 : 0; 220 221 printk(KERN_INFO PFX "EP93XX watchdog, driver version " 222 WDT_VERSION "%s\n", 223 (__raw_readl(EP93XX_WDT_WATCHDOG) & 0x08) 224 ? " (nCS1 disable detected)" : ""); 225 226 if (timeout < 1 || timeout > 3600) { 227 timeout = WDT_TIMEOUT; 228 printk(KERN_INFO PFX 229 "timeout value must be 1<=x<=3600, using %d\n", 230 timeout); 231 } 232 233 setup_timer(&timer, ep93xx_timer_ping, 1); 234 return err; 235} 236 237static void __exit ep93xx_wdt_exit(void) 238{ 239 wdt_shutdown(); 240 misc_deregister(&ep93xx_wdt_miscdev); 241} 242 243module_init(ep93xx_wdt_init); 244module_exit(ep93xx_wdt_exit); 245 246module_param(nowayout, int, 0); 247MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started"); 248 249module_param(timeout, int, 0); 250MODULE_PARM_DESC(timeout, "Watchdog timeout in seconds. (1<=timeout<=3600, default=" __MODULE_STRING(WATCHDOG_TIMEOUT) ")"); 251 252MODULE_AUTHOR("Ray Lehtiniemi <rayl@mail.com>," 253 "Alessandro Zummo <a.zummo@towertech.it>"); 254MODULE_DESCRIPTION("EP93xx Watchdog"); 255MODULE_LICENSE("GPL"); 256MODULE_VERSION(WDT_VERSION); 257MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR); 258