blob: 3daa330ae436e8801d27b1f1cf56b4948d42ca88 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Intel 21285 watchdog driver
3 * Copyright (c) Phil Blundell <pb@nexus.co.uk>, 1998
4 *
5 * based on
6 *
7 * SoftDog 0.05: A Software Watchdog Device
8 *
Alan Cox29fa0582008-10-27 15:17:56 +00009 * (c) Copyright 1996 Alan Cox <alan@lxorguk.ukuu.org.uk>,
10 * All Rights Reserved.
Linus Torvalds1da177e2005-04-16 15:20:36 -070011 *
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation; either version
15 * 2 of the License, or (at your option) any later version.
16 *
17 */
18
Joe Perches27c766a2012-02-15 15:06:19 -080019#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
20
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#include <linux/module.h>
22#include <linux/moduleparam.h>
23#include <linux/types.h>
24#include <linux/kernel.h>
25#include <linux/fs.h>
26#include <linux/mm.h>
27#include <linux/miscdevice.h>
28#include <linux/watchdog.h>
29#include <linux/reboot.h>
30#include <linux/init.h>
31#include <linux/interrupt.h>
Alan Coxd0e58ee2008-05-19 14:09:51 +010032#include <linux/uaccess.h>
33#include <linux/irq.h>
Russell Kinga09e64f2008-08-05 16:14:15 +010034#include <mach/hardware.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070035
Linus Torvalds1da177e2005-04-16 15:20:36 -070036#include <asm/mach-types.h>
37#include <asm/hardware/dec21285.h>
38
39/*
40 * Define this to stop the watchdog actually rebooting the machine.
41 */
42#undef ONLY_TESTING
43
44static unsigned int soft_margin = 60; /* in seconds */
45static unsigned int reload;
46static unsigned long timer_alive;
47
48#ifdef ONLY_TESTING
49/*
50 * If the timer expires..
51 */
David Howells7d12e782006-10-05 14:55:46 +010052static void watchdog_fire(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -070053{
Joe Perches27c766a2012-02-15 15:06:19 -080054 pr_crit("Would Reboot\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070055 *CSR_TIMER4_CNTL = 0;
56 *CSR_TIMER4_CLR = 0;
57}
58#endif
59
60/*
61 * Refresh the timer.
62 */
63static void watchdog_ping(void)
64{
65 *CSR_TIMER4_LOAD = reload;
66}
67
68/*
69 * Allow only one person to hold it open
70 */
71static int watchdog_open(struct inode *inode, struct file *file)
72{
73 int ret;
74
75 if (*CSR_SA110_CNTL & (1 << 13))
76 return -EBUSY;
77
78 if (test_and_set_bit(1, &timer_alive))
79 return -EBUSY;
80
81 reload = soft_margin * (mem_fclk_21285 / 256);
82
83 *CSR_TIMER4_CLR = 0;
84 watchdog_ping();
85 *CSR_TIMER4_CNTL = TIMER_CNTL_ENABLE | TIMER_CNTL_AUTORELOAD
86 | TIMER_CNTL_DIV256;
87
88#ifdef ONLY_TESTING
89 ret = request_irq(IRQ_TIMER4, watchdog_fire, 0, "watchdog", NULL);
90 if (ret) {
91 *CSR_TIMER4_CNTL = 0;
92 clear_bit(1, &timer_alive);
93 }
94#else
95 /*
96 * Setting this bit is irreversible; once enabled, there is
97 * no way to disable the watchdog.
98 */
99 *CSR_SA110_CNTL |= 1 << 13;
100
101 ret = 0;
102#endif
103 nonseekable_open(inode, file);
104 return ret;
105}
106
107/*
108 * Shut off the timer.
109 * Note: if we really have enabled the watchdog, there
110 * is no way to turn off.
111 */
112static int watchdog_release(struct inode *inode, struct file *file)
113{
114#ifdef ONLY_TESTING
115 free_irq(IRQ_TIMER4, NULL);
116 clear_bit(1, &timer_alive);
117#endif
118 return 0;
119}
120
Ben Dooksedf86c92008-09-16 11:31:01 +0100121static ssize_t watchdog_write(struct file *file, const char __user *data,
122 size_t len, loff_t *ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123{
124 /*
125 * Refresh the timer.
126 */
127 if (len)
128 watchdog_ping();
129
130 return len;
131}
132
Alan Coxd0e58ee2008-05-19 14:09:51 +0100133static const struct watchdog_info ident = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134 .options = WDIOF_SETTIMEOUT,
135 .identity = "Footbridge Watchdog",
136};
137
Alan Coxd0e58ee2008-05-19 14:09:51 +0100138static long watchdog_ioctl(struct file *file, unsigned int cmd,
Ben Dooksedf86c92008-09-16 11:31:01 +0100139 unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140{
141 unsigned int new_margin;
Ben Dooksedf86c92008-09-16 11:31:01 +0100142 int __user *int_arg = (int __user *)arg;
Samuel Tardieu795b89d2006-09-09 17:34:31 +0200143 int ret = -ENOTTY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144
Alan Coxd0e58ee2008-05-19 14:09:51 +0100145 switch (cmd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146 case WDIOC_GETSUPPORT:
147 ret = 0;
Ben Dooksedf86c92008-09-16 11:31:01 +0100148 if (copy_to_user((void __user *)arg, &ident, sizeof(ident)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149 ret = -EFAULT;
150 break;
151
152 case WDIOC_GETSTATUS:
153 case WDIOC_GETBOOTSTATUS:
Ben Dooksedf86c92008-09-16 11:31:01 +0100154 ret = put_user(0, int_arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155 break;
156
157 case WDIOC_KEEPALIVE:
158 watchdog_ping();
159 ret = 0;
160 break;
161
162 case WDIOC_SETTIMEOUT:
Ben Dooksedf86c92008-09-16 11:31:01 +0100163 ret = get_user(new_margin, int_arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164 if (ret)
165 break;
166
167 /* Arbitrary, can't find the card's limits */
168 if (new_margin < 0 || new_margin > 60) {
169 ret = -EINVAL;
170 break;
171 }
172
173 soft_margin = new_margin;
174 reload = soft_margin * (mem_fclk_21285 / 256);
175 watchdog_ping();
176 /* Fall */
177 case WDIOC_GETTIMEOUT:
Ben Dooksedf86c92008-09-16 11:31:01 +0100178 ret = put_user(soft_margin, int_arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179 break;
180 }
181 return ret;
182}
183
Arjan van de Ven62322d22006-07-03 00:24:21 -0700184static const struct file_operations watchdog_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 .owner = THIS_MODULE,
186 .llseek = no_llseek,
187 .write = watchdog_write,
Alan Coxd0e58ee2008-05-19 14:09:51 +0100188 .unlocked_ioctl = watchdog_ioctl,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 .open = watchdog_open,
190 .release = watchdog_release,
191};
192
193static struct miscdevice watchdog_miscdev = {
194 .minor = WATCHDOG_MINOR,
195 .name = "watchdog",
196 .fops = &watchdog_fops,
197};
198
199static int __init footbridge_watchdog_init(void)
200{
201 int retval;
202
203 if (machine_is_netwinder())
204 return -ENODEV;
205
206 retval = misc_register(&watchdog_miscdev);
207 if (retval < 0)
208 return retval;
209
Joe Perches27c766a2012-02-15 15:06:19 -0800210 pr_info("Footbridge Watchdog Timer: 0.01, timer margin: %d sec\n",
211 soft_margin);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212
213 if (machine_is_cats())
Joe Perches27c766a2012-02-15 15:06:19 -0800214 pr_warn("Warning: Watchdog reset may not work on this machine\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 return 0;
216}
217
218static void __exit footbridge_watchdog_exit(void)
219{
220 misc_deregister(&watchdog_miscdev);
221}
222
223MODULE_AUTHOR("Phil Blundell <pb@nexus.co.uk>");
224MODULE_DESCRIPTION("Footbridge watchdog driver");
225MODULE_LICENSE("GPL");
226MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR);
227
228module_param(soft_margin, int, 0);
Alan Coxd0e58ee2008-05-19 14:09:51 +0100229MODULE_PARM_DESC(soft_margin, "Watchdog timeout in seconds");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230
231module_init(footbridge_watchdog_init);
232module_exit(footbridge_watchdog_exit);