blob: 1b5c25a47b876194561490aba581d84ac5cd27cc [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
Joe Perches27c766a2012-02-15 15:06:19 -080015#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
16
Linus Torvalds1da177e2005-04-16 15:20:36 -070017#include <linux/module.h>
18#include <linux/moduleparam.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070019#include <linux/types.h>
20#include <linux/kernel.h>
21#include <linux/fs.h>
22#include <linux/mm.h>
23#include <linux/miscdevice.h>
24#include <linux/watchdog.h>
25#include <linux/notifier.h>
26#include <linux/reboot.h>
27#include <linux/init.h>
Alan Cox9b9dbcc2008-05-19 14:06:08 +010028#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070029#include <asm/sgi/mc.h>
30
Alan Cox9b9dbcc2008-05-19 14:06:08 +010031static unsigned long indydog_alive;
Axel Lin1334f322011-11-29 13:54:01 +080032static DEFINE_SPINLOCK(indydog_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070033
Linus Torvalds1da177e2005-04-16 15:20:36 -070034#define WATCHDOG_TIMEOUT 30 /* 30 sec default timeout */
35
Wim Van Sebroeck86a1e182012-03-05 16:51:11 +010036static bool nowayout = WATCHDOG_NOWAYOUT;
37module_param(nowayout, bool, 0);
Alan Cox9b9dbcc2008-05-19 14:06:08 +010038MODULE_PARM_DESC(nowayout,
39 "Watchdog cannot be stopped once started (default="
40 __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
Linus Torvalds1da177e2005-04-16 15:20:36 -070041
42static void indydog_start(void)
43{
Alan Cox9b9dbcc2008-05-19 14:06:08 +010044 u32 mc_ctrl0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070045
Alan Cox9b9dbcc2008-05-19 14:06:08 +010046 spin_lock(&indydog_lock);
47 mc_ctrl0 = sgimc->cpuctrl0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070048 mc_ctrl0 = sgimc->cpuctrl0 | SGIMC_CCTRL0_WDOG;
49 sgimc->cpuctrl0 = mc_ctrl0;
Alan Cox9b9dbcc2008-05-19 14:06:08 +010050 spin_unlock(&indydog_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070051}
52
53static void indydog_stop(void)
54{
Alan Cox9b9dbcc2008-05-19 14:06:08 +010055 u32 mc_ctrl0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070056
Alan Cox9b9dbcc2008-05-19 14:06:08 +010057 spin_lock(&indydog_lock);
58
59 mc_ctrl0 = sgimc->cpuctrl0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070060 mc_ctrl0 &= ~SGIMC_CCTRL0_WDOG;
61 sgimc->cpuctrl0 = mc_ctrl0;
Alan Cox9b9dbcc2008-05-19 14:06:08 +010062 spin_unlock(&indydog_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070063
Joe Perches27c766a2012-02-15 15:06:19 -080064 pr_info("Stopped watchdog timer\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070065}
66
67static void indydog_ping(void)
68{
69 sgimc->watchdogt = 0;
70}
71
72/*
73 * Allow only one person to hold it open
74 */
75static int indydog_open(struct inode *inode, struct file *file)
76{
Alan Cox9b9dbcc2008-05-19 14:06:08 +010077 if (test_and_set_bit(0, &indydog_alive))
Linus Torvalds1da177e2005-04-16 15:20:36 -070078 return -EBUSY;
79
80 if (nowayout)
81 __module_get(THIS_MODULE);
82
83 /* Activate timer */
84 indydog_start();
85 indydog_ping();
86
Joe Perches27c766a2012-02-15 15:06:19 -080087 pr_info("Started watchdog timer\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070088
89 return nonseekable_open(inode, file);
90}
91
92static int indydog_release(struct inode *inode, struct file *file)
93{
94 /* Shut off the timer.
95 * Lock it in if it's a module and we defined ...NOWAYOUT */
96 if (!nowayout)
97 indydog_stop(); /* Turn the WDT off */
Alan Cox9b9dbcc2008-05-19 14:06:08 +010098 clear_bit(0, &indydog_alive);
Linus Torvalds1da177e2005-04-16 15:20:36 -070099 return 0;
100}
101
Alan Cox9b9dbcc2008-05-19 14:06:08 +0100102static ssize_t indydog_write(struct file *file, const char *data,
103 size_t len, loff_t *ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104{
105 /* Refresh the timer. */
Alan Cox9b9dbcc2008-05-19 14:06:08 +0100106 if (len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107 indydog_ping();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108 return len;
109}
110
Alan Cox9b9dbcc2008-05-19 14:06:08 +0100111static long indydog_ioctl(struct file *file, unsigned int cmd,
112 unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113{
114 int options, retval = -EINVAL;
Wim Van Sebroeck42747d72009-12-26 18:55:22 +0000115 static const struct watchdog_info ident = {
Wim Van Sebroecke73a7802009-05-11 18:33:00 +0000116 .options = WDIOF_KEEPALIVEPING,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 .firmware_version = 0,
118 .identity = "Hardware Watchdog for SGI IP22",
119 };
120
121 switch (cmd) {
Alan Cox9b9dbcc2008-05-19 14:06:08 +0100122 case WDIOC_GETSUPPORT:
123 if (copy_to_user((struct watchdog_info *)arg,
124 &ident, sizeof(ident)))
125 return -EFAULT;
126 return 0;
127 case WDIOC_GETSTATUS:
128 case WDIOC_GETBOOTSTATUS:
129 return put_user(0, (int *)arg);
Alan Cox9b9dbcc2008-05-19 14:06:08 +0100130 case WDIOC_SETOPTIONS:
131 {
132 if (get_user(options, (int *)arg))
133 return -EFAULT;
134 if (options & WDIOS_DISABLECARD) {
135 indydog_stop();
136 retval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 }
Alan Cox9b9dbcc2008-05-19 14:06:08 +0100138 if (options & WDIOS_ENABLECARD) {
139 indydog_start();
140 retval = 0;
141 }
142 return retval;
143 }
Wim Van Sebroeck0c060902008-07-18 11:41:17 +0000144 case WDIOC_KEEPALIVE:
145 indydog_ping();
146 return 0;
147 case WDIOC_GETTIMEOUT:
148 return put_user(WATCHDOG_TIMEOUT, (int *)arg);
Alan Cox9b9dbcc2008-05-19 14:06:08 +0100149 default:
150 return -ENOTTY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151 }
152}
153
Alan Cox9b9dbcc2008-05-19 14:06:08 +0100154static int indydog_notify_sys(struct notifier_block *this,
155 unsigned long code, void *unused)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156{
157 if (code == SYS_DOWN || code == SYS_HALT)
158 indydog_stop(); /* Turn the WDT off */
159
160 return NOTIFY_DONE;
161}
162
Arjan van de Ven62322d22006-07-03 00:24:21 -0700163static const struct file_operations indydog_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164 .owner = THIS_MODULE,
165 .llseek = no_llseek,
166 .write = indydog_write,
Alan Cox9b9dbcc2008-05-19 14:06:08 +0100167 .unlocked_ioctl = indydog_ioctl,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 .open = indydog_open,
169 .release = indydog_release,
170};
171
172static struct miscdevice indydog_miscdev = {
173 .minor = WATCHDOG_MINOR,
174 .name = "watchdog",
175 .fops = &indydog_fops,
176};
177
178static struct notifier_block indydog_notifier = {
179 .notifier_call = indydog_notify_sys,
180};
181
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182static int __init watchdog_init(void)
183{
184 int ret;
185
186 ret = register_reboot_notifier(&indydog_notifier);
187 if (ret) {
Joe Perches27c766a2012-02-15 15:06:19 -0800188 pr_err("cannot register reboot notifier (err=%d)\n", ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 return ret;
190 }
191
192 ret = misc_register(&indydog_miscdev);
193 if (ret) {
Joe Perches27c766a2012-02-15 15:06:19 -0800194 pr_err("cannot register miscdev on minor=%d (err=%d)\n",
195 WATCHDOG_MINOR, ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 unregister_reboot_notifier(&indydog_notifier);
197 return ret;
198 }
199
Joe Perches27c766a2012-02-15 15:06:19 -0800200 pr_info("Hardware Watchdog Timer for SGI IP22: 0.3\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201
202 return 0;
203}
204
205static void __exit watchdog_exit(void)
206{
207 misc_deregister(&indydog_miscdev);
208 unregister_reboot_notifier(&indydog_notifier);
209}
210
211module_init(watchdog_init);
212module_exit(watchdog_exit);
213
214MODULE_AUTHOR("Guido Guenther <agx@sigxcpu.org>");
215MODULE_DESCRIPTION("Hardware Watchdog Device for SGI IP22");
216MODULE_LICENSE("GPL");