1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46#include <linux/module.h>
47#include <linux/io.h>
48#include <linux/uaccess.h>
49#include <linux/fs.h>
50#include <linux/reboot.h>
51#include <linux/miscdevice.h>
52#include <linux/watchdog.h>
53#include <linux/interrupt.h>
54
55#include <asm/sibyte/sb1250.h>
56#include <asm/sibyte/sb1250_regs.h>
57#include <asm/sibyte/sb1250_int.h>
58#include <asm/sibyte/sb1250_scd.h>
59
60static DEFINE_SPINLOCK(sbwd_lock);
61
62
63
64
65
66
67void sbwdog_set(char __iomem *wdog, unsigned long t)
68{
69 spin_lock(&sbwd_lock);
70 __raw_writeb(0, wdog);
71 __raw_writeq(t & 0x7fffffUL, wdog - 0x10);
72 spin_unlock(&sbwd_lock);
73}
74
75
76
77
78
79
80
81void sbwdog_pet(char __iomem *wdog)
82{
83 spin_lock(&sbwd_lock);
84 __raw_writeb(__raw_readb(wdog) | 1, wdog);
85 spin_unlock(&sbwd_lock);
86}
87
88static unsigned long sbwdog_gate;
89static char __iomem *kern_dog = (char __iomem *)(IO_BASE + (A_SCD_WDOG_CFG_0));
90static char __iomem *user_dog = (char __iomem *)(IO_BASE + (A_SCD_WDOG_CFG_1));
91static unsigned long timeout = 0x7fffffUL;
92static int expect_close;
93
94static const struct watchdog_info ident = {
95 .options = WDIOF_CARDRESET | WDIOF_SETTIMEOUT |
96 WDIOF_KEEPALIVEPING | WDIOF_MAGICCLOSE,
97 .identity = "SiByte Watchdog",
98};
99
100
101
102
103static int sbwdog_open(struct inode *inode, struct file *file)
104{
105 nonseekable_open(inode, file);
106 if (test_and_set_bit(0, &sbwdog_gate))
107 return -EBUSY;
108 __module_get(THIS_MODULE);
109
110
111
112
113 sbwdog_set(user_dog, timeout);
114 __raw_writeb(1, user_dog);
115
116 return 0;
117}
118
119
120
121
122static int sbwdog_release(struct inode *inode, struct file *file)
123{
124 if (expect_close == 42) {
125 __raw_writeb(0, user_dog);
126 module_put(THIS_MODULE);
127 } else {
128 printk(KERN_CRIT
129 "%s: Unexpected close, not stopping watchdog!\n",
130 ident.identity);
131 sbwdog_pet(user_dog);
132 }
133 clear_bit(0, &sbwdog_gate);
134 expect_close = 0;
135
136 return 0;
137}
138
139
140
141
142static ssize_t sbwdog_write(struct file *file, const char __user *data,
143 size_t len, loff_t *ppos)
144{
145 int i;
146
147 if (len) {
148
149
150
151 expect_close = 0;
152
153 for (i = 0; i != len; i++) {
154 char c;
155
156 if (get_user(c, data + i))
157 return -EFAULT;
158 if (c == 'V')
159 expect_close = 42;
160 }
161 sbwdog_pet(user_dog);
162 }
163
164 return len;
165}
166
167static long sbwdog_ioctl(struct file *file, unsigned int cmd,
168 unsigned long arg)
169{
170 int ret = -ENOTTY;
171 unsigned long time;
172 void __user *argp = (void __user *)arg;
173 int __user *p = argp;
174
175 switch (cmd) {
176 case WDIOC_GETSUPPORT:
177 ret = copy_to_user(argp, &ident, sizeof(ident)) ? -EFAULT : 0;
178 break;
179
180 case WDIOC_GETSTATUS:
181 case WDIOC_GETBOOTSTATUS:
182 ret = put_user(0, p);
183 break;
184
185 case WDIOC_KEEPALIVE:
186 sbwdog_pet(user_dog);
187 ret = 0;
188 break;
189
190 case WDIOC_SETTIMEOUT:
191 ret = get_user(time, p);
192 if (ret)
193 break;
194
195 time *= 1000000;
196 if (time > 0x7fffffUL) {
197 ret = -EINVAL;
198 break;
199 }
200 timeout = time;
201 sbwdog_set(user_dog, timeout);
202 sbwdog_pet(user_dog);
203
204 case WDIOC_GETTIMEOUT:
205
206
207
208
209 ret = put_user(__raw_readq(user_dog - 8) / 1000000, p);
210 break;
211 }
212 return ret;
213}
214
215
216
217
218static int sbwdog_notify_sys(struct notifier_block *this, unsigned long code,
219 void *erf)
220{
221 if (code == SYS_DOWN || code == SYS_HALT) {
222
223
224
225 __raw_writeb(0, user_dog);
226 __raw_writeb(0, kern_dog);
227 }
228
229 return NOTIFY_DONE;
230}
231
232static const struct file_operations sbwdog_fops = {
233 .owner = THIS_MODULE,
234 .llseek = no_llseek,
235 .write = sbwdog_write,
236 .unlocked_ioctl = sbwdog_ioctl,
237 .open = sbwdog_open,
238 .release = sbwdog_release,
239};
240
241static struct miscdevice sbwdog_miscdev = {
242 .minor = WATCHDOG_MINOR,
243 .name = "watchdog",
244 .fops = &sbwdog_fops,
245};
246
247static struct notifier_block sbwdog_notifier = {
248 .notifier_call = sbwdog_notify_sys,
249};
250
251
252
253
254
255
256
257
258
259irqreturn_t sbwdog_interrupt(int irq, void *addr)
260{
261 unsigned long wd_init;
262 char *wd_cfg_reg = (char *)addr;
263 u8 cfg;
264
265 cfg = __raw_readb(wd_cfg_reg);
266 wd_init = __raw_readq(wd_cfg_reg - 8) & 0x7fffff;
267
268
269
270
271 if (wd_cfg_reg == user_dog)
272 printk(KERN_CRIT "%s in danger of initiating system reset "
273 "in %ld.%01ld seconds\n",
274 ident.identity,
275 wd_init / 1000000, (wd_init / 100000) % 10);
276 else
277 cfg |= 1;
278
279 __raw_writeb(cfg, wd_cfg_reg);
280
281 return IRQ_HANDLED;
282}
283
284static int __init sbwdog_init(void)
285{
286 int ret;
287
288
289
290
291 ret = register_reboot_notifier(&sbwdog_notifier);
292 if (ret) {
293 printk(KERN_ERR
294 "%s: cannot register reboot notifier (err=%d)\n",
295 ident.identity, ret);
296 return ret;
297 }
298
299
300
301
302
303 ret = request_irq(1, sbwdog_interrupt, IRQF_DISABLED | IRQF_SHARED,
304 ident.identity, (void *)user_dog);
305 if (ret) {
306 printk(KERN_ERR "%s: failed to request irq 1 - %d\n",
307 ident.identity, ret);
308 goto out;
309 }
310
311 ret = misc_register(&sbwdog_miscdev);
312 if (ret == 0) {
313 printk(KERN_INFO "%s: timeout is %ld.%ld secs\n",
314 ident.identity,
315 timeout / 1000000, (timeout / 100000) % 10);
316 return 0;
317 }
318 free_irq(1, (void *)user_dog);
319out:
320 unregister_reboot_notifier(&sbwdog_notifier);
321
322 return ret;
323}
324
325static void __exit sbwdog_exit(void)
326{
327 misc_deregister(&sbwdog_miscdev);
328 free_irq(1, (void *)user_dog);
329 unregister_reboot_notifier(&sbwdog_notifier);
330}
331
332module_init(sbwdog_init);
333module_exit(sbwdog_exit);
334
335MODULE_AUTHOR("Andrew Sharp <andy.sharp@lsi.com>");
336MODULE_DESCRIPTION("SiByte Watchdog");
337
338module_param(timeout, ulong, 0);
339MODULE_PARM_DESC(timeout,
340 "Watchdog timeout in microseconds (max/default 8388607 or 8.3ish secs)");
341
342MODULE_LICENSE("GPL");
343MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR);
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363