blob: 1475e09f9af214f5656e6d4ff9b1067555f5554f [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * IndyDog 0.3 A Hardware Watchdog Device for SGI IP22
3 *
Alan Cox9b9dbcc2008-05-19 14:06:08 +01004 * (c) Copyright 2002 Guido Guenther <agx@sigxcpu.org>,
5 * All Rights Reserved.
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version
10 * 2 of the License, or (at your option) any later version.
11 *
Alan Cox29fa0582008-10-27 15:17:56 +000012 * based on softdog.c by Alan Cox <alan@lxorguk.ukuu.org.uk>
Linus Torvalds1da177e2005-04-16 15:20:36 -070013 */
14
15#include <linux/module.h>
16#include <linux/moduleparam.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070017#include <linux/types.h>
18#include <linux/kernel.h>
19#include <linux/fs.h>
20#include <linux/mm.h>
21#include <linux/miscdevice.h>
22#include <linux/watchdog.h>
23#include <linux/notifier.h>
24#include <linux/reboot.h>
25#include <linux/init.h>
Alan Cox9b9dbcc2008-05-19 14:06:08 +010026#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#include <asm/sgi/mc.h>
28
29#define PFX "indydog: "
Alan Cox9b9dbcc2008-05-19 14:06:08 +010030static unsigned long indydog_alive;
Axel Lin1334f322011-11-29 13:54:01 +080031static DEFINE_SPINLOCK(indydog_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070032
Linus Torvalds1da177e2005-04-16 15:20:36 -070033#define WATCHDOG_TIMEOUT 30 /* 30 sec default timeout */
34
Andrey Panin4bfdf372005-07-27 11:43:58 -070035static int nowayout = WATCHDOG_NOWAYOUT;
Linus Torvalds1da177e2005-04-16 15:20:36 -070036module_param(nowayout, int, 0);
Alan Cox9b9dbcc2008-05-19 14:06:08 +010037MODULE_PARM_DESC(nowayout,
38 "Watchdog cannot be stopped once started (default="
39 __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
Linus Torvalds1da177e2005-04-16 15:20:36 -070040
41static void indydog_start(void)
42{
Alan Cox9b9dbcc2008-05-19 14:06:08 +010043 u32 mc_ctrl0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070044
Alan Cox9b9dbcc2008-05-19 14:06:08 +010045 spin_lock(&indydog_lock);
46 mc_ctrl0 = sgimc->cpuctrl0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070047 mc_ctrl0 = sgimc->cpuctrl0 | SGIMC_CCTRL0_WDOG;
48 sgimc->cpuctrl0 = mc_ctrl0;
Alan Cox9b9dbcc2008-05-19 14:06:08 +010049 spin_unlock(&indydog_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070050}
51
52static void indydog_stop(void)
53{
Alan Cox9b9dbcc2008-05-19 14:06:08 +010054 u32 mc_ctrl0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070055
Alan Cox9b9dbcc2008-05-19 14:06:08 +010056 spin_lock(&indydog_lock);
57
58 mc_ctrl0 = sgimc->cpuctrl0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070059 mc_ctrl0 &= ~SGIMC_CCTRL0_WDOG;
60 sgimc->cpuctrl0 = mc_ctrl0;
Alan Cox9b9dbcc2008-05-19 14:06:08 +010061 spin_unlock(&indydog_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070062
63 printk(KERN_INFO PFX "Stopped watchdog timer.\n");
64}
65
66static void indydog_ping(void)
67{
68 sgimc->watchdogt = 0;
69}
70
71/*
72 * Allow only one person to hold it open
73 */
74static int indydog_open(struct inode *inode, struct file *file)
75{
Alan Cox9b9dbcc2008-05-19 14:06:08 +010076 if (test_and_set_bit(0, &indydog_alive))
Linus Torvalds1da177e2005-04-16 15:20:36 -070077 return -EBUSY;
78
79 if (nowayout)
80 __module_get(THIS_MODULE);
81
82 /* Activate timer */
83 indydog_start();
84 indydog_ping();
85
Linus Torvalds1da177e2005-04-16 15:20:36 -070086 printk(KERN_INFO "Started watchdog timer.\n");
87
88 return nonseekable_open(inode, file);
89}
90
91static int indydog_release(struct inode *inode, struct file *file)
92{
93 /* Shut off the timer.
94 * Lock it in if it's a module and we defined ...NOWAYOUT */
95 if (!nowayout)
96 indydog_stop(); /* Turn the WDT off */
Alan Cox9b9dbcc2008-05-19 14:06:08 +010097 clear_bit(0, &indydog_alive);
Linus Torvalds1da177e2005-04-16 15:20:36 -070098 return 0;
99}
100
Alan Cox9b9dbcc2008-05-19 14:06:08 +0100101static ssize_t indydog_write(struct file *file, const char *data,
102 size_t len, loff_t *ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103{
104 /* Refresh the timer. */
Alan Cox9b9dbcc2008-05-19 14:06:08 +0100105 if (len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106 indydog_ping();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107 return len;
108}
109
Alan Cox9b9dbcc2008-05-19 14:06:08 +0100110static long indydog_ioctl(struct file *file, unsigned int cmd,
111 unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112{
113 int options, retval = -EINVAL;
Wim Van Sebroeck42747d72009-12-26 18:55:22 +0000114 static const struct watchdog_info ident = {
Wim Van Sebroecke73a7802009-05-11 18:33:00 +0000115 .options = WDIOF_KEEPALIVEPING,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116 .firmware_version = 0,
117 .identity = "Hardware Watchdog for SGI IP22",
118 };
119
120 switch (cmd) {
Alan Cox9b9dbcc2008-05-19 14:06:08 +0100121 case WDIOC_GETSUPPORT:
122 if (copy_to_user((struct watchdog_info *)arg,
123 &ident, sizeof(ident)))
124 return -EFAULT;
125 return 0;
126 case WDIOC_GETSTATUS:
127 case WDIOC_GETBOOTSTATUS:
128 return put_user(0, (int *)arg);
Alan Cox9b9dbcc2008-05-19 14:06:08 +0100129 case WDIOC_SETOPTIONS:
130 {
131 if (get_user(options, (int *)arg))
132 return -EFAULT;
133 if (options & WDIOS_DISABLECARD) {
134 indydog_stop();
135 retval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136 }
Alan Cox9b9dbcc2008-05-19 14:06:08 +0100137 if (options & WDIOS_ENABLECARD) {
138 indydog_start();
139 retval = 0;
140 }
141 return retval;
142 }
Wim Van Sebroeck0c060902008-07-18 11:41:17 +0000143 case WDIOC_KEEPALIVE:
144 indydog_ping();
145 return 0;
146 case WDIOC_GETTIMEOUT:
147 return put_user(WATCHDOG_TIMEOUT, (int *)arg);
Alan Cox9b9dbcc2008-05-19 14:06:08 +0100148 default:
149 return -ENOTTY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 }
151}
152
Alan Cox9b9dbcc2008-05-19 14:06:08 +0100153static int indydog_notify_sys(struct notifier_block *this,
154 unsigned long code, void *unused)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155{
156 if (code == SYS_DOWN || code == SYS_HALT)
157 indydog_stop(); /* Turn the WDT off */
158
159 return NOTIFY_DONE;
160}
161
Arjan van de Ven62322d22006-07-03 00:24:21 -0700162static const struct file_operations indydog_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163 .owner = THIS_MODULE,
164 .llseek = no_llseek,
165 .write = indydog_write,
Alan Cox9b9dbcc2008-05-19 14:06:08 +0100166 .unlocked_ioctl = indydog_ioctl,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 .open = indydog_open,
168 .release = indydog_release,
169};
170
171static struct miscdevice indydog_miscdev = {
172 .minor = WATCHDOG_MINOR,
173 .name = "watchdog",
174 .fops = &indydog_fops,
175};
176
177static struct notifier_block indydog_notifier = {
178 .notifier_call = indydog_notify_sys,
179};
180
181static char banner[] __initdata =
182 KERN_INFO PFX "Hardware Watchdog Timer for SGI IP22: 0.3\n";
183
184static int __init watchdog_init(void)
185{
186 int ret;
187
188 ret = register_reboot_notifier(&indydog_notifier);
189 if (ret) {
Alan Cox9b9dbcc2008-05-19 14:06:08 +0100190 printk(KERN_ERR PFX
191 "cannot register reboot notifier (err=%d)\n", ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 return ret;
193 }
194
195 ret = misc_register(&indydog_miscdev);
196 if (ret) {
Alan Cox9b9dbcc2008-05-19 14:06:08 +0100197 printk(KERN_ERR PFX
198 "cannot register miscdev on minor=%d (err=%d)\n",
199 WATCHDOG_MINOR, ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 unregister_reboot_notifier(&indydog_notifier);
201 return ret;
202 }
203
204 printk(banner);
205
206 return 0;
207}
208
209static void __exit watchdog_exit(void)
210{
211 misc_deregister(&indydog_miscdev);
212 unregister_reboot_notifier(&indydog_notifier);
213}
214
215module_init(watchdog_init);
216module_exit(watchdog_exit);
217
218MODULE_AUTHOR("Guido Guenther <agx@sigxcpu.org>");
219MODULE_DESCRIPTION("Hardware Watchdog Device for SGI IP22");
220MODULE_LICENSE("GPL");
221MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR);