blob: d6d53014cb689ba11694377469916db34ce887c0 [file] [log] [blame]
Thomas Gleixner74ba9202019-05-20 09:19:02 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
3 * sma cpu5 watchdog driver
4 *
5 * Copyright (C) 2003 Heiko Ronsdorf <hero@ihg.uni-duisburg.de>
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 */
7
Joe Perches27c766a2012-02-15 15:06:19 -08008#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
9
Linus Torvalds1da177e2005-04-16 15:20:36 -070010#include <linux/module.h>
11#include <linux/moduleparam.h>
12#include <linux/types.h>
13#include <linux/errno.h>
14#include <linux/miscdevice.h>
15#include <linux/fs.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070016#include <linux/ioport.h>
17#include <linux/timer.h>
Steven Rostedt3fe0c272006-01-09 15:59:26 -080018#include <linux/completion.h>
Tim Schmielau4e57b682005-10-30 15:03:48 -080019#include <linux/jiffies.h>
Alan Cox6f932f12008-05-19 14:05:24 +010020#include <linux/io.h>
21#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include <linux/watchdog.h>
23
24/* adjustable parameters */
25
Alan Cox6f932f12008-05-19 14:05:24 +010026static int verbose;
Linus Torvalds1da177e2005-04-16 15:20:36 -070027static int port = 0x91;
28static int ticks = 10000;
Axel Lin1334f322011-11-29 13:54:01 +080029static DEFINE_SPINLOCK(cpu5wdt_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070030
31#define PFX "cpu5wdt: "
32
33#define CPU5WDT_EXTENT 0x0A
34
35#define CPU5WDT_STATUS_REG 0x00
36#define CPU5WDT_TIME_A_REG 0x02
37#define CPU5WDT_TIME_B_REG 0x03
38#define CPU5WDT_MODE_REG 0x04
39#define CPU5WDT_TRIGGER_REG 0x07
40#define CPU5WDT_ENABLE_REG 0x08
41#define CPU5WDT_RESET_REG 0x09
42
43#define CPU5WDT_INTERVAL (HZ/10+1)
44
45/* some device data */
46
47static struct {
Steven Rostedt3fe0c272006-01-09 15:59:26 -080048 struct completion stop;
Florian Fainelli996d62d2008-02-25 13:39:57 +010049 int running;
Linus Torvalds1da177e2005-04-16 15:20:36 -070050 struct timer_list timer;
Florian Fainelli996d62d2008-02-25 13:39:57 +010051 int queue;
Linus Torvalds1da177e2005-04-16 15:20:36 -070052 int default_ticks;
53 unsigned long inuse;
54} cpu5wdt_device;
55
56/* generic helper functions */
57
Kees Cooke99e88a2017-10-16 14:43:17 -070058static void cpu5wdt_trigger(struct timer_list *unused)
Linus Torvalds1da177e2005-04-16 15:20:36 -070059{
Alan Cox6f932f12008-05-19 14:05:24 +010060 if (verbose > 2)
Joe Perches27c766a2012-02-15 15:06:19 -080061 pr_debug("trigger at %i ticks\n", ticks);
Linus Torvalds1da177e2005-04-16 15:20:36 -070062
Alan Cox6f932f12008-05-19 14:05:24 +010063 if (cpu5wdt_device.running)
Linus Torvalds1da177e2005-04-16 15:20:36 -070064 ticks--;
65
Alan Cox6f932f12008-05-19 14:05:24 +010066 spin_lock(&cpu5wdt_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070067 /* keep watchdog alive */
68 outb(1, port + CPU5WDT_TRIGGER_REG);
69
70 /* requeue?? */
Jiri Slaby82eb7c52007-02-08 18:39:36 +010071 if (cpu5wdt_device.queue && ticks)
72 mod_timer(&cpu5wdt_device.timer, jiffies + CPU5WDT_INTERVAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -070073 else {
74 /* ticks doesn't matter anyway */
Steven Rostedt3fe0c272006-01-09 15:59:26 -080075 complete(&cpu5wdt_device.stop);
Linus Torvalds1da177e2005-04-16 15:20:36 -070076 }
Alan Cox6f932f12008-05-19 14:05:24 +010077 spin_unlock(&cpu5wdt_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070078
79}
80
81static void cpu5wdt_reset(void)
82{
83 ticks = cpu5wdt_device.default_ticks;
84
Alan Cox6f932f12008-05-19 14:05:24 +010085 if (verbose)
Joe Perches27c766a2012-02-15 15:06:19 -080086 pr_debug("reset (%i ticks)\n", (int) ticks);
Linus Torvalds1da177e2005-04-16 15:20:36 -070087
88}
89
90static void cpu5wdt_start(void)
91{
Alan Cox6f932f12008-05-19 14:05:24 +010092 unsigned long flags;
93
94 spin_lock_irqsave(&cpu5wdt_lock, flags);
95 if (!cpu5wdt_device.queue) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070096 cpu5wdt_device.queue = 1;
97 outb(0, port + CPU5WDT_TIME_A_REG);
98 outb(0, port + CPU5WDT_TIME_B_REG);
99 outb(1, port + CPU5WDT_MODE_REG);
100 outb(0, port + CPU5WDT_RESET_REG);
101 outb(0, port + CPU5WDT_ENABLE_REG);
Jiri Slaby82eb7c52007-02-08 18:39:36 +0100102 mod_timer(&cpu5wdt_device.timer, jiffies + CPU5WDT_INTERVAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103 }
104 /* if process dies, counter is not decremented */
105 cpu5wdt_device.running++;
Alan Cox6f932f12008-05-19 14:05:24 +0100106 spin_unlock_irqrestore(&cpu5wdt_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107}
108
109static int cpu5wdt_stop(void)
110{
Alan Cox6f932f12008-05-19 14:05:24 +0100111 unsigned long flags;
112
113 spin_lock_irqsave(&cpu5wdt_lock, flags);
114 if (cpu5wdt_device.running)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115 cpu5wdt_device.running = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116 ticks = cpu5wdt_device.default_ticks;
Alan Cox6f932f12008-05-19 14:05:24 +0100117 spin_unlock_irqrestore(&cpu5wdt_lock, flags);
118 if (verbose)
Joe Perches27c766a2012-02-15 15:06:19 -0800119 pr_crit("stop not possible\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120 return -EIO;
121}
122
123/* filesystem operations */
124
125static int cpu5wdt_open(struct inode *inode, struct file *file)
126{
Alan Cox6f932f12008-05-19 14:05:24 +0100127 if (test_and_set_bit(0, &cpu5wdt_device.inuse))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128 return -EBUSY;
Kirill Smelkovc5bf68f2019-03-26 23:51:19 +0300129 return stream_open(inode, file);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130}
131
132static int cpu5wdt_release(struct inode *inode, struct file *file)
133{
134 clear_bit(0, &cpu5wdt_device.inuse);
135 return 0;
136}
137
Alan Cox6f932f12008-05-19 14:05:24 +0100138static long cpu5wdt_ioctl(struct file *file, unsigned int cmd,
139 unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140{
141 void __user *argp = (void __user *)arg;
Alan Cox6f932f12008-05-19 14:05:24 +0100142 int __user *p = argp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143 unsigned int value;
Wim Van Sebroeck42747d72009-12-26 18:55:22 +0000144 static const struct watchdog_info ident = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145 .options = WDIOF_CARDRESET,
146 .identity = "CPU5 WDT",
147 };
148
Alan Cox6f932f12008-05-19 14:05:24 +0100149 switch (cmd) {
Wim Van Sebroeck0c060902008-07-18 11:41:17 +0000150 case WDIOC_GETSUPPORT:
151 if (copy_to_user(argp, &ident, sizeof(ident)))
152 return -EFAULT;
Alan Cox6f932f12008-05-19 14:05:24 +0100153 break;
154 case WDIOC_GETSTATUS:
155 value = inb(port + CPU5WDT_STATUS_REG);
156 value = (value >> 2) & 1;
157 return put_user(value, p);
158 case WDIOC_GETBOOTSTATUS:
159 return put_user(0, p);
Alan Cox6f932f12008-05-19 14:05:24 +0100160 case WDIOC_SETOPTIONS:
161 if (get_user(value, p))
162 return -EFAULT;
163 if (value & WDIOS_ENABLECARD)
164 cpu5wdt_start();
165 if (value & WDIOS_DISABLECARD)
166 cpu5wdt_stop();
167 break;
Wim Van Sebroeck0c060902008-07-18 11:41:17 +0000168 case WDIOC_KEEPALIVE:
169 cpu5wdt_reset();
170 break;
Alan Cox6f932f12008-05-19 14:05:24 +0100171 default:
172 return -ENOTTY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173 }
174 return 0;
175}
176
Alan Cox6f932f12008-05-19 14:05:24 +0100177static ssize_t cpu5wdt_write(struct file *file, const char __user *buf,
178 size_t count, loff_t *ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179{
Alan Cox6f932f12008-05-19 14:05:24 +0100180 if (!count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 cpu5wdt_reset();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 return count;
184}
185
Arjan van de Ven62322d22006-07-03 00:24:21 -0700186static const struct file_operations cpu5wdt_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 .owner = THIS_MODULE,
188 .llseek = no_llseek,
Alan Cox6f932f12008-05-19 14:05:24 +0100189 .unlocked_ioctl = cpu5wdt_ioctl,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 .open = cpu5wdt_open,
191 .write = cpu5wdt_write,
192 .release = cpu5wdt_release,
193};
194
195static struct miscdevice cpu5wdt_misc = {
196 .minor = WATCHDOG_MINOR,
197 .name = "watchdog",
198 .fops = &cpu5wdt_fops,
199};
200
201/* init/exit function */
202
Bill Pemberton2d991a12012-11-19 13:21:41 -0500203static int cpu5wdt_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204{
205 unsigned int val;
206 int err;
207
Alan Cox6f932f12008-05-19 14:05:24 +0100208 if (verbose)
Joe Perches27c766a2012-02-15 15:06:19 -0800209 pr_debug("port=0x%x, verbose=%i\n", port, verbose);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210
Alan Cox6f932f12008-05-19 14:05:24 +0100211 init_completion(&cpu5wdt_device.stop);
Alan Cox6f932f12008-05-19 14:05:24 +0100212 cpu5wdt_device.queue = 0;
Kees Cooke99e88a2017-10-16 14:43:17 -0700213 timer_setup(&cpu5wdt_device.timer, cpu5wdt_trigger, 0);
Alan Cox6f932f12008-05-19 14:05:24 +0100214 cpu5wdt_device.default_ticks = ticks;
215
216 if (!request_region(port, CPU5WDT_EXTENT, PFX)) {
Joe Perches27c766a2012-02-15 15:06:19 -0800217 pr_err("request_region failed\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 err = -EBUSY;
219 goto no_port;
220 }
221
Alan Cox6f932f12008-05-19 14:05:24 +0100222 /* watchdog reboot? */
223 val = inb(port + CPU5WDT_STATUS_REG);
224 val = (val >> 2) & 1;
225 if (!val)
Joe Perches27c766a2012-02-15 15:06:19 -0800226 pr_info("sorry, was my fault\n");
Alan Cox6f932f12008-05-19 14:05:24 +0100227
228 err = misc_register(&cpu5wdt_misc);
229 if (err < 0) {
Joe Perches27c766a2012-02-15 15:06:19 -0800230 pr_err("misc_register failed\n");
Alexey Dobriyanfb8f7ba2007-03-24 15:58:12 +0300231 goto no_misc;
232 }
233
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234
Joe Perches27c766a2012-02-15 15:06:19 -0800235 pr_info("init success\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236 return 0;
237
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238no_misc:
Alexey Dobriyanfb8f7ba2007-03-24 15:58:12 +0300239 release_region(port, CPU5WDT_EXTENT);
240no_port:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 return err;
242}
243
Bill Pemberton2d991a12012-11-19 13:21:41 -0500244static int cpu5wdt_init_module(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245{
246 return cpu5wdt_init();
247}
248
Bill Pemberton4b12b892012-11-19 13:26:24 -0500249static void cpu5wdt_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250{
Alan Cox6f932f12008-05-19 14:05:24 +0100251 if (cpu5wdt_device.queue) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 cpu5wdt_device.queue = 0;
Steven Rostedt3fe0c272006-01-09 15:59:26 -0800253 wait_for_completion(&cpu5wdt_device.stop);
devendra.aarue09d9c32012-06-26 14:48:26 +0530254 del_timer(&cpu5wdt_device.timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 }
256
257 misc_deregister(&cpu5wdt_misc);
258
259 release_region(port, CPU5WDT_EXTENT);
260
261}
262
Bill Pemberton4b12b892012-11-19 13:26:24 -0500263static void cpu5wdt_exit_module(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264{
265 cpu5wdt_exit();
266}
267
268/* module entry points */
269
270module_init(cpu5wdt_init_module);
271module_exit(cpu5wdt_exit_module);
272
273MODULE_AUTHOR("Heiko Ronsdorf <hero@ihg.uni-duisburg.de>");
274MODULE_DESCRIPTION("sma cpu5 watchdog driver");
275MODULE_SUPPORTED_DEVICE("sma cpu5 watchdog");
276MODULE_LICENSE("GPL");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277
David Howells5d1c93c2017-04-04 16:54:29 +0100278module_param_hw(port, int, ioport, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279MODULE_PARM_DESC(port, "base address of watchdog card, default is 0x91");
280
281module_param(verbose, int, 0);
282MODULE_PARM_DESC(verbose, "be verbose, default is 0 (no)");
283
284module_param(ticks, int, 0);
285MODULE_PARM_DESC(ticks, "count down ticks, default is 10000");