blob: 504be461f992a9d5da18641c33cdda55b2ed7106 [file] [log] [blame]
Andrew Sharp75c752e2007-12-13 16:16:42 -08001/*
2 * Watchdog driver for SiByte SB1 SoCs
3 *
Andrew Sharp5b73a412009-09-23 16:03:20 -07004 * Copyright (C) 2007 OnStor, Inc. * Andrew Sharp <andy.sharp@lsi.com>
Andrew Sharp75c752e2007-12-13 16:16:42 -08005 *
6 * This driver is intended to make the second of two hardware watchdogs
7 * on the Sibyte 12XX and 11XX SoCs available to the user. There are two
8 * such devices available on the SoC, but it seems that there isn't an
9 * enumeration class for watchdogs in Linux like there is for RTCs.
10 * The second is used rather than the first because it uses IRQ 1,
11 * thereby avoiding all that IRQ 0 problematic nonsense.
12 *
13 * I have not tried this driver on a 1480 processor; it might work
14 * just well enough to really screw things up.
15 *
16 * It is a simple timer, and there is an interrupt that is raised the
17 * first time the timer expires. The second time it expires, the chip
18 * is reset and there is no way to redirect that NMI. Which could
19 * be problematic in some cases where this chip is sitting on the HT
20 * bus and has just taken responsibility for providing a cache block.
21 * Since the reset can't be redirected to the external reset pin, it is
22 * possible that other HT connected processors might hang and not reset.
23 * For Linux, a soft reset would probably be even worse than a hard reset.
24 * There you have it.
25 *
26 * The timer takes 23 bits of a 64 bit register (?) as a count value,
27 * and decrements the count every microsecond, for a max value of
28 * 0x7fffff usec or about 8.3ish seconds.
29 *
30 * This watchdog borrows some user semantics from the softdog driver,
31 * in that if you close the fd, it leaves the watchdog running, unless
32 * you previously wrote a 'V' to the fd, in which case it disables
33 * the watchdog when you close the fd like some other drivers.
34 *
35 * Based on various other watchdog drivers, which are probably all
36 * loosely based on something Alan Cox wrote years ago.
37 *
Alan Cox29fa0582008-10-27 15:17:56 +000038 * (c) Copyright 1996 Alan Cox <alan@lxorguk.ukuu.org.uk>,
39 * All Rights Reserved.
Andrew Sharp75c752e2007-12-13 16:16:42 -080040 *
41 * This program is free software; you can redistribute it and/or
42 * modify it under the terms of the GNU General Public License
43 * version 1 or 2 as published by the Free Software Foundation.
44 *
45 */
Joe Perches27c766a2012-02-15 15:06:19 -080046
47#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
48
Andrew Sharp75c752e2007-12-13 16:16:42 -080049#include <linux/module.h>
50#include <linux/io.h>
51#include <linux/uaccess.h>
52#include <linux/fs.h>
53#include <linux/reboot.h>
54#include <linux/miscdevice.h>
55#include <linux/watchdog.h>
56#include <linux/interrupt.h>
57
58#include <asm/sibyte/sb1250.h>
59#include <asm/sibyte/sb1250_regs.h>
60#include <asm/sibyte/sb1250_int.h>
61#include <asm/sibyte/sb1250_scd.h>
62
Alan Coxdf3c9de2008-05-19 14:08:33 +010063static DEFINE_SPINLOCK(sbwd_lock);
Andrew Sharp75c752e2007-12-13 16:16:42 -080064
65/*
66 * set the initial count value of a timer
67 *
68 * wdog is the iomem address of the cfg register
69 */
YueHaibinge2a4aed2019-03-20 21:42:50 +080070static void sbwdog_set(char __iomem *wdog, unsigned long t)
Andrew Sharp75c752e2007-12-13 16:16:42 -080071{
Alan Coxdf3c9de2008-05-19 14:08:33 +010072 spin_lock(&sbwd_lock);
Guenter Roeck86913312010-04-19 08:37:11 -070073 __raw_writeb(0, wdog);
74 __raw_writeq(t & 0x7fffffUL, wdog - 0x10);
Alan Coxdf3c9de2008-05-19 14:08:33 +010075 spin_unlock(&sbwd_lock);
Andrew Sharp75c752e2007-12-13 16:16:42 -080076}
77
78/*
79 * cause the timer to [re]load it's initial count and start counting
80 * all over again
81 *
82 * wdog is the iomem address of the cfg register
83 */
YueHaibinge2a4aed2019-03-20 21:42:50 +080084static void sbwdog_pet(char __iomem *wdog)
Andrew Sharp75c752e2007-12-13 16:16:42 -080085{
Alan Coxdf3c9de2008-05-19 14:08:33 +010086 spin_lock(&sbwd_lock);
Andrew Sharp75c752e2007-12-13 16:16:42 -080087 __raw_writeb(__raw_readb(wdog) | 1, wdog);
Alan Coxdf3c9de2008-05-19 14:08:33 +010088 spin_unlock(&sbwd_lock);
Andrew Sharp75c752e2007-12-13 16:16:42 -080089}
90
91static unsigned long sbwdog_gate; /* keeps it to one thread only */
92static char __iomem *kern_dog = (char __iomem *)(IO_BASE + (A_SCD_WDOG_CFG_0));
93static char __iomem *user_dog = (char __iomem *)(IO_BASE + (A_SCD_WDOG_CFG_1));
94static unsigned long timeout = 0x7fffffUL; /* useconds: 8.3ish secs. */
95static int expect_close;
96
Alan Coxdf3c9de2008-05-19 14:08:33 +010097static const struct watchdog_info ident = {
98 .options = WDIOF_CARDRESET | WDIOF_SETTIMEOUT |
Wim Van Sebroecke73a7802009-05-11 18:33:00 +000099 WDIOF_KEEPALIVEPING | WDIOF_MAGICCLOSE,
Andrew Sharp75c752e2007-12-13 16:16:42 -0800100 .identity = "SiByte Watchdog",
101};
102
103/*
104 * Allow only a single thread to walk the dog
105 */
106static int sbwdog_open(struct inode *inode, struct file *file)
107{
Kirill Smelkovc5bf68f2019-03-26 23:51:19 +0300108 stream_open(inode, file);
Alan Coxdf3c9de2008-05-19 14:08:33 +0100109 if (test_and_set_bit(0, &sbwdog_gate))
Andrew Sharp75c752e2007-12-13 16:16:42 -0800110 return -EBUSY;
Andrew Sharp75c752e2007-12-13 16:16:42 -0800111 __module_get(THIS_MODULE);
112
113 /*
114 * Activate the timer
115 */
116 sbwdog_set(user_dog, timeout);
117 __raw_writeb(1, user_dog);
118
119 return 0;
120}
121
122/*
123 * Put the dog back in the kennel.
124 */
125static int sbwdog_release(struct inode *inode, struct file *file)
126{
127 if (expect_close == 42) {
128 __raw_writeb(0, user_dog);
129 module_put(THIS_MODULE);
130 } else {
Joe Perches27c766a2012-02-15 15:06:19 -0800131 pr_crit("%s: Unexpected close, not stopping watchdog!\n",
132 ident.identity);
Andrew Sharp75c752e2007-12-13 16:16:42 -0800133 sbwdog_pet(user_dog);
134 }
135 clear_bit(0, &sbwdog_gate);
136 expect_close = 0;
137
138 return 0;
139}
140
141/*
142 * 42 - the answer
143 */
144static ssize_t sbwdog_write(struct file *file, const char __user *data,
145 size_t len, loff_t *ppos)
146{
147 int i;
148
149 if (len) {
150 /*
151 * restart the timer
152 */
153 expect_close = 0;
154
155 for (i = 0; i != len; i++) {
156 char c;
157
Alan Coxdf3c9de2008-05-19 14:08:33 +0100158 if (get_user(c, data + i))
Andrew Sharp75c752e2007-12-13 16:16:42 -0800159 return -EFAULT;
Alan Coxdf3c9de2008-05-19 14:08:33 +0100160 if (c == 'V')
Andrew Sharp75c752e2007-12-13 16:16:42 -0800161 expect_close = 42;
Andrew Sharp75c752e2007-12-13 16:16:42 -0800162 }
163 sbwdog_pet(user_dog);
164 }
165
166 return len;
167}
168
Alan Coxdf3c9de2008-05-19 14:08:33 +0100169static long sbwdog_ioctl(struct file *file, unsigned int cmd,
170 unsigned long arg)
Andrew Sharp75c752e2007-12-13 16:16:42 -0800171{
172 int ret = -ENOTTY;
173 unsigned long time;
174 void __user *argp = (void __user *)arg;
175 int __user *p = argp;
176
177 switch (cmd) {
178 case WDIOC_GETSUPPORT:
179 ret = copy_to_user(argp, &ident, sizeof(ident)) ? -EFAULT : 0;
180 break;
181
182 case WDIOC_GETSTATUS:
183 case WDIOC_GETBOOTSTATUS:
184 ret = put_user(0, p);
185 break;
186
Wim Van Sebroeck0c060902008-07-18 11:41:17 +0000187 case WDIOC_KEEPALIVE:
188 sbwdog_pet(user_dog);
189 ret = 0;
190 break;
191
Andrew Sharp75c752e2007-12-13 16:16:42 -0800192 case WDIOC_SETTIMEOUT:
193 ret = get_user(time, p);
Alan Coxdf3c9de2008-05-19 14:08:33 +0100194 if (ret)
Andrew Sharp75c752e2007-12-13 16:16:42 -0800195 break;
Andrew Sharp75c752e2007-12-13 16:16:42 -0800196
197 time *= 1000000;
198 if (time > 0x7fffffUL) {
199 ret = -EINVAL;
200 break;
201 }
202 timeout = time;
203 sbwdog_set(user_dog, timeout);
204 sbwdog_pet(user_dog);
Gustavo A. R. Silvabd490f82020-07-07 12:11:21 -0500205 fallthrough;
Andrew Sharp75c752e2007-12-13 16:16:42 -0800206
207 case WDIOC_GETTIMEOUT:
208 /*
209 * get the remaining count from the ... count register
210 * which is 1*8 before the config register
211 */
Ralf Baechlecd6cbde2013-06-19 10:57:33 +0200212 ret = put_user((u32)__raw_readq(user_dog - 8) / 1000000, p);
Andrew Sharp75c752e2007-12-13 16:16:42 -0800213 break;
Andrew Sharp75c752e2007-12-13 16:16:42 -0800214 }
215 return ret;
216}
217
218/*
219 * Notifier for system down
220 */
Wim Van Sebroeck7944d3a2008-08-06 20:19:41 +0000221static int sbwdog_notify_sys(struct notifier_block *this, unsigned long code,
222 void *erf)
Andrew Sharp75c752e2007-12-13 16:16:42 -0800223{
224 if (code == SYS_DOWN || code == SYS_HALT) {
225 /*
226 * sit and sit
227 */
228 __raw_writeb(0, user_dog);
229 __raw_writeb(0, kern_dog);
230 }
231
232 return NOTIFY_DONE;
233}
234
Alan Coxdf3c9de2008-05-19 14:08:33 +0100235static const struct file_operations sbwdog_fops = {
Andrew Sharp75c752e2007-12-13 16:16:42 -0800236 .owner = THIS_MODULE,
237 .llseek = no_llseek,
238 .write = sbwdog_write,
Alan Coxdf3c9de2008-05-19 14:08:33 +0100239 .unlocked_ioctl = sbwdog_ioctl,
Arnd Bergmannb6dfb242019-06-03 14:23:09 +0200240 .compat_ioctl = compat_ptr_ioctl,
Andrew Sharp75c752e2007-12-13 16:16:42 -0800241 .open = sbwdog_open,
242 .release = sbwdog_release,
243};
244
Alan Coxdf3c9de2008-05-19 14:08:33 +0100245static struct miscdevice sbwdog_miscdev = {
Andrew Sharp75c752e2007-12-13 16:16:42 -0800246 .minor = WATCHDOG_MINOR,
247 .name = "watchdog",
248 .fops = &sbwdog_fops,
249};
250
251static struct notifier_block sbwdog_notifier = {
252 .notifier_call = sbwdog_notify_sys,
253};
254
255/*
256 * interrupt handler
257 *
258 * doesn't do a whole lot for user, but oh so cleverly written so kernel
259 * code can use it to re-up the watchdog, thereby saving the kernel from
260 * having to create and maintain a timer, just to tickle another timer,
261 * which is just so wrong.
262 */
263irqreturn_t sbwdog_interrupt(int irq, void *addr)
264{
265 unsigned long wd_init;
266 char *wd_cfg_reg = (char *)addr;
267 u8 cfg;
268
269 cfg = __raw_readb(wd_cfg_reg);
270 wd_init = __raw_readq(wd_cfg_reg - 8) & 0x7fffff;
271
272 /*
273 * if it's the second watchdog timer, it's for those users
274 */
Alan Coxdf3c9de2008-05-19 14:08:33 +0100275 if (wd_cfg_reg == user_dog)
Joe Perches27c766a2012-02-15 15:06:19 -0800276 pr_crit("%s in danger of initiating system reset "
Wim Van Sebroecka77dba72009-04-14 20:20:07 +0000277 "in %ld.%01ld seconds\n",
278 ident.identity,
279 wd_init / 1000000, (wd_init / 100000) % 10);
Alan Coxdf3c9de2008-05-19 14:08:33 +0100280 else
Andrew Sharp75c752e2007-12-13 16:16:42 -0800281 cfg |= 1;
Andrew Sharp75c752e2007-12-13 16:16:42 -0800282
283 __raw_writeb(cfg, wd_cfg_reg);
284
285 return IRQ_HANDLED;
286}
287
288static int __init sbwdog_init(void)
289{
290 int ret;
291
292 /*
293 * register a reboot notifier
294 */
295 ret = register_reboot_notifier(&sbwdog_notifier);
296 if (ret) {
Joe Perches27c766a2012-02-15 15:06:19 -0800297 pr_err("%s: cannot register reboot notifier (err=%d)\n",
298 ident.identity, ret);
Andrew Sharp75c752e2007-12-13 16:16:42 -0800299 return ret;
300 }
301
302 /*
303 * get the resources
304 */
Andrew Sharp75c752e2007-12-13 16:16:42 -0800305
Yong Zhang86b59122011-09-07 16:10:55 +0800306 ret = request_irq(1, sbwdog_interrupt, IRQF_SHARED,
Andrew Sharp75c752e2007-12-13 16:16:42 -0800307 ident.identity, (void *)user_dog);
308 if (ret) {
Joe Perches27c766a2012-02-15 15:06:19 -0800309 pr_err("%s: failed to request irq 1 - %d\n",
310 ident.identity, ret);
Akinobu Mitaae448552010-08-21 18:27:50 +0900311 goto out;
Andrew Sharp75c752e2007-12-13 16:16:42 -0800312 }
313
Alan Coxdf3c9de2008-05-19 14:08:33 +0100314 ret = misc_register(&sbwdog_miscdev);
315 if (ret == 0) {
Joe Perches27c766a2012-02-15 15:06:19 -0800316 pr_info("%s: timeout is %ld.%ld secs\n",
317 ident.identity,
318 timeout / 1000000, (timeout / 100000) % 10);
Akinobu Mitaae448552010-08-21 18:27:50 +0900319 return 0;
320 }
321 free_irq(1, (void *)user_dog);
322out:
323 unregister_reboot_notifier(&sbwdog_notifier);
324
Andrew Sharp75c752e2007-12-13 16:16:42 -0800325 return ret;
326}
327
328static void __exit sbwdog_exit(void)
329{
330 misc_deregister(&sbwdog_miscdev);
Akinobu Mitaae448552010-08-21 18:27:50 +0900331 free_irq(1, (void *)user_dog);
332 unregister_reboot_notifier(&sbwdog_notifier);
Andrew Sharp75c752e2007-12-13 16:16:42 -0800333}
334
335module_init(sbwdog_init);
336module_exit(sbwdog_exit);
337
Andrew Sharp5b73a412009-09-23 16:03:20 -0700338MODULE_AUTHOR("Andrew Sharp <andy.sharp@lsi.com>");
Andrew Sharp75c752e2007-12-13 16:16:42 -0800339MODULE_DESCRIPTION("SiByte Watchdog");
340
341module_param(timeout, ulong, 0);
342MODULE_PARM_DESC(timeout,
Alan Coxdf3c9de2008-05-19 14:08:33 +0100343 "Watchdog timeout in microseconds (max/default 8388607 or 8.3ish secs)");
Andrew Sharp75c752e2007-12-13 16:16:42 -0800344
345MODULE_LICENSE("GPL");
Andrew Sharp75c752e2007-12-13 16:16:42 -0800346
347/*
348 * example code that can be put in a platform code area to utilize the
349 * first watchdog timer for the kernels own purpose.
350
Alan Coxdf3c9de2008-05-19 14:08:33 +0100351void platform_wd_setup(void)
Andrew Sharp75c752e2007-12-13 16:16:42 -0800352{
353 int ret;
354
Yong Zhang86b59122011-09-07 16:10:55 +0800355 ret = request_irq(1, sbwdog_interrupt, IRQF_SHARED,
Andrew Sharp75c752e2007-12-13 16:16:42 -0800356 "Kernel Watchdog", IOADDR(A_SCD_WDOG_CFG_0));
357 if (ret) {
Joe Perches27c766a2012-02-15 15:06:19 -0800358 pr_crit("Watchdog IRQ zero(0) failed to be requested - %d\n", ret);
Andrew Sharp75c752e2007-12-13 16:16:42 -0800359 }
360}
361
362
363 */