]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - drivers/char/watchdog/alim7101_wdt.c
[PATCH] vt: Decrement ref count of the VT backend on deallocation
[mirror_ubuntu-bionic-kernel.git] / drivers / char / watchdog / alim7101_wdt.c
CommitLineData
1da177e4
LT
1/*
2 * ALi M7101 PMU Computer Watchdog Timer driver
3 *
4 * Based on w83877f_wdt.c by Scott Jennings <linuxdrivers@oro.net>
5 * and the Cobalt kernel WDT timer driver by Tim Hockin
6 * <thockin@cobaltnet.com>
7 *
8 * (c)2002 Steve Hill <steve@navaho.co.uk>
9 *
10 * This WDT driver is different from most other Linux WDT
11 * drivers in that the driver will ping the watchdog by itself,
12 * because this particular WDT has a very short timeout (1.6
13 * seconds) and it would be insane to count on any userspace
14 * daemon always getting scheduled within that time frame.
15 *
16 * Additions:
17 * Aug 23, 2004 - Added use_gpio module parameter for use on revision a1d PMUs
18 * found on very old cobalt hardware.
19 * -- Mike Waychison <michael.waychison@sun.com>
20 */
21
22#include <linux/module.h>
23#include <linux/moduleparam.h>
24#include <linux/types.h>
25#include <linux/timer.h>
26#include <linux/miscdevice.h>
27#include <linux/watchdog.h>
28#include <linux/ioport.h>
29#include <linux/notifier.h>
30#include <linux/reboot.h>
31#include <linux/init.h>
32#include <linux/fs.h>
33#include <linux/pci.h>
34
35#include <asm/io.h>
36#include <asm/uaccess.h>
37#include <asm/system.h>
38
39#define OUR_NAME "alim7101_wdt"
40#define PFX OUR_NAME ": "
41
42#define WDT_ENABLE 0x9C
43#define WDT_DISABLE 0x8C
44
45#define ALI_7101_WDT 0x92
46#define ALI_7101_GPIO 0x7D
47#define ALI_7101_GPIO_O 0x7E
48#define ALI_WDT_ARM 0x01
49
50/*
51 * We're going to use a 1 second timeout.
52 * If we reset the watchdog every ~250ms we should be safe. */
53
54#define WDT_INTERVAL (HZ/4+1)
55
56/*
57 * We must not require too good response from the userspace daemon.
58 * Here we require the userspace daemon to send us a heartbeat
59 * char to /dev/watchdog every 30 seconds.
60 */
61
62#define WATCHDOG_TIMEOUT 30 /* 30 sec default timeout */
63static int timeout = WATCHDOG_TIMEOUT; /* in seconds, will be multiplied by HZ to get seconds to wait for a ping */
64module_param(timeout, int, 0);
65MODULE_PARM_DESC(timeout, "Watchdog timeout in seconds. (1<=timeout<=3600, default=" __MODULE_STRING(WATCHDOG_TIMEOUT) ")");
66
67static int use_gpio = 0; /* Use the pic (for a1d revision alim7101) */
68module_param(use_gpio, int, 0);
69MODULE_PARM_DESC(use_gpio, "Use the gpio watchdog. (required by old cobalt boards)");
70
71static void wdt_timer_ping(unsigned long);
72static struct timer_list timer;
73static unsigned long next_heartbeat;
74static unsigned long wdt_is_open;
75static char wdt_expect_close;
76static struct pci_dev *alim7101_pmu;
77
4bfdf378 78static int nowayout = WATCHDOG_NOWAYOUT;
1da177e4
LT
79module_param(nowayout, int, 0);
80MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started (default=CONFIG_WATCHDOG_NOWAYOUT)");
81
82/*
83 * Whack the dog
84 */
85
86static void wdt_timer_ping(unsigned long data)
87{
88 /* If we got a heartbeat pulse within the WDT_US_INTERVAL
89 * we agree to ping the WDT
90 */
91 char tmp;
92
93 if(time_before(jiffies, next_heartbeat))
94 {
95 /* Ping the WDT (this is actually a disarm/arm sequence) */
96 pci_read_config_byte(alim7101_pmu, 0x92, &tmp);
97 pci_write_config_byte(alim7101_pmu, ALI_7101_WDT, (tmp & ~ALI_WDT_ARM));
98 pci_write_config_byte(alim7101_pmu, ALI_7101_WDT, (tmp | ALI_WDT_ARM));
99 if (use_gpio) {
100 pci_read_config_byte(alim7101_pmu, ALI_7101_GPIO_O, &tmp);
101 pci_write_config_byte(alim7101_pmu, ALI_7101_GPIO_O, tmp
102 | 0x20);
103 pci_write_config_byte(alim7101_pmu, ALI_7101_GPIO_O, tmp
104 & ~0x20);
105 }
106 } else {
107 printk(KERN_WARNING PFX "Heartbeat lost! Will not ping the watchdog\n");
108 }
109 /* Re-set the timer interval */
110 timer.expires = jiffies + WDT_INTERVAL;
111 add_timer(&timer);
112}
113
114/*
115 * Utility routines
116 */
117
118static void wdt_change(int writeval)
119{
120 char tmp;
121
122 pci_read_config_byte(alim7101_pmu, ALI_7101_WDT, &tmp);
123 if (writeval == WDT_ENABLE) {
124 pci_write_config_byte(alim7101_pmu, ALI_7101_WDT, (tmp | ALI_WDT_ARM));
125 if (use_gpio) {
126 pci_read_config_byte(alim7101_pmu, ALI_7101_GPIO_O, &tmp);
127 pci_write_config_byte(alim7101_pmu, ALI_7101_GPIO_O, tmp & ~0x20);
128 }
129
130 } else {
131 pci_write_config_byte(alim7101_pmu, ALI_7101_WDT, (tmp & ~ALI_WDT_ARM));
132 if (use_gpio) {
133 pci_read_config_byte(alim7101_pmu, ALI_7101_GPIO_O, &tmp);
134 pci_write_config_byte(alim7101_pmu, ALI_7101_GPIO_O, tmp | 0x20);
135 }
136 }
137}
138
139static void wdt_startup(void)
140{
141 next_heartbeat = jiffies + (timeout * HZ);
142
143 /* We must enable before we kick off the timer in case the timer
144 occurs as we ping it */
145
146 wdt_change(WDT_ENABLE);
147
148 /* Start the timer */
149 timer.expires = jiffies + WDT_INTERVAL;
150 add_timer(&timer);
151
152
153 printk(KERN_INFO PFX "Watchdog timer is now enabled.\n");
154}
155
156static void wdt_turnoff(void)
157{
158 /* Stop the timer */
159 del_timer_sync(&timer);
160 wdt_change(WDT_DISABLE);
161 printk(KERN_INFO PFX "Watchdog timer is now disabled...\n");
162}
163
164static void wdt_keepalive(void)
165{
166 /* user land ping */
167 next_heartbeat = jiffies + (timeout * HZ);
168}
169
170/*
171 * /dev/watchdog handling
172 */
173
174static ssize_t fop_write(struct file * file, const char __user * buf, size_t count, loff_t * ppos)
175{
176 /* See if we got the magic character 'V' and reload the timer */
177 if(count) {
178 if (!nowayout) {
179 size_t ofs;
180
181 /* note: just in case someone wrote the magic character
182 * five months ago... */
183 wdt_expect_close = 0;
184
185 /* now scan */
186 for (ofs = 0; ofs != count; ofs++) {
187 char c;
188 if (get_user(c, buf+ofs))
189 return -EFAULT;
190 if (c == 'V')
191 wdt_expect_close = 42;
192 }
193 }
194 /* someone wrote to us, we should restart timer */
195 wdt_keepalive();
196 }
197 return count;
198}
199
200static int fop_open(struct inode * inode, struct file * file)
201{
202 /* Just in case we're already talking to someone... */
203 if(test_and_set_bit(0, &wdt_is_open))
204 return -EBUSY;
205 /* Good, fire up the show */
206 wdt_startup();
207 return nonseekable_open(inode, file);
208}
209
210static int fop_close(struct inode * inode, struct file * file)
211{
212 if(wdt_expect_close == 42)
213 wdt_turnoff();
214 else {
215 /* wim: shouldn't there be a: del_timer(&timer); */
216 printk(KERN_CRIT PFX "device file closed unexpectedly. Will not stop the WDT!\n");
217 }
218 clear_bit(0, &wdt_is_open);
219 wdt_expect_close = 0;
220 return 0;
221}
222
223static int fop_ioctl(struct inode *inode, struct file *file, unsigned int cmd, unsigned long arg)
224{
225 void __user *argp = (void __user *)arg;
226 int __user *p = argp;
227 static struct watchdog_info ident =
228 {
229 .options = WDIOF_KEEPALIVEPING | WDIOF_SETTIMEOUT | WDIOF_MAGICCLOSE,
230 .firmware_version = 1,
231 .identity = "ALiM7101",
232 };
233
234 switch(cmd)
235 {
236 case WDIOC_GETSUPPORT:
237 return copy_to_user(argp, &ident, sizeof(ident))?-EFAULT:0;
238 case WDIOC_GETSTATUS:
239 case WDIOC_GETBOOTSTATUS:
240 return put_user(0, p);
241 case WDIOC_KEEPALIVE:
242 wdt_keepalive();
243 return 0;
244 case WDIOC_SETOPTIONS:
245 {
246 int new_options, retval = -EINVAL;
247
248 if(get_user(new_options, p))
249 return -EFAULT;
250
251 if(new_options & WDIOS_DISABLECARD) {
252 wdt_turnoff();
253 retval = 0;
254 }
255
256 if(new_options & WDIOS_ENABLECARD) {
257 wdt_startup();
258 retval = 0;
259 }
260
261 return retval;
262 }
263 case WDIOC_SETTIMEOUT:
264 {
265 int new_timeout;
266
267 if(get_user(new_timeout, p))
268 return -EFAULT;
269
270 if(new_timeout < 1 || new_timeout > 3600) /* arbitrary upper limit */
271 return -EINVAL;
272
273 timeout = new_timeout;
274 wdt_keepalive();
275 /* Fall through */
276 }
277 case WDIOC_GETTIMEOUT:
278 return put_user(timeout, p);
279 default:
280 return -ENOIOCTLCMD;
281 }
282}
283
284static struct file_operations wdt_fops = {
285 .owner= THIS_MODULE,
286 .llseek= no_llseek,
287 .write= fop_write,
288 .open= fop_open,
289 .release= fop_close,
290 .ioctl= fop_ioctl,
291};
292
293static struct miscdevice wdt_miscdev = {
294 .minor=WATCHDOG_MINOR,
295 .name="watchdog",
296 .fops=&wdt_fops,
297};
298
299/*
300 * Notifier for system down
301 */
302
303static int wdt_notify_sys(struct notifier_block *this, unsigned long code, void *unused)
304{
305 if (code==SYS_DOWN || code==SYS_HALT)
306 wdt_turnoff();
307
308 if (code==SYS_RESTART) {
309 /*
310 * Cobalt devices have no way of rebooting themselves other than
311 * getting the watchdog to pull reset, so we restart the watchdog on
312 * reboot with no heartbeat
313 */
314 wdt_change(WDT_ENABLE);
315 printk(KERN_INFO PFX "Watchdog timer is now enabled with no heartbeat - should reboot in ~1 second.\n");
316 }
317 return NOTIFY_DONE;
318}
319
320/*
321 * The WDT needs to learn about soft shutdowns in order to
322 * turn the timebomb registers off.
323 */
324
325static struct notifier_block wdt_notifier=
326{
327 .notifier_call = wdt_notify_sys,
328};
329
330static void __exit alim7101_wdt_unload(void)
331{
332 wdt_turnoff();
333 /* Deregister */
334 misc_deregister(&wdt_miscdev);
335 unregister_reboot_notifier(&wdt_notifier);
336}
337
338static int __init alim7101_wdt_init(void)
339{
340 int rc = -EBUSY;
341 struct pci_dev *ali1543_south;
342 char tmp;
343
344 printk(KERN_INFO PFX "Steve Hill <steve@navaho.co.uk>.\n");
345 alim7101_pmu = pci_find_device(PCI_VENDOR_ID_AL, PCI_DEVICE_ID_AL_M7101,NULL);
346 if (!alim7101_pmu) {
347 printk(KERN_INFO PFX "ALi M7101 PMU not present - WDT not set\n");
348 return -EBUSY;
349 }
350
351 /* Set the WDT in the PMU to 1 second */
352 pci_write_config_byte(alim7101_pmu, ALI_7101_WDT, 0x02);
353
354 ali1543_south = pci_find_device(PCI_VENDOR_ID_AL, PCI_DEVICE_ID_AL_M1533, NULL);
355 if (!ali1543_south) {
356 printk(KERN_INFO PFX "ALi 1543 South-Bridge not present - WDT not set\n");
357 return -EBUSY;
358 }
359 pci_read_config_byte(ali1543_south, 0x5e, &tmp);
360 if ((tmp & 0x1e) == 0x00) {
361 if (!use_gpio) {
362 printk(KERN_INFO PFX "Detected old alim7101 revision 'a1d'. If this is a cobalt board, set the 'use_gpio' module parameter.\n");
363 return -EBUSY;
364 }
365 nowayout = 1;
366 } else if ((tmp & 0x1e) != 0x12 && (tmp & 0x1e) != 0x00) {
367 printk(KERN_INFO PFX "ALi 1543 South-Bridge does not have the correct revision number (???1001?) - WDT not set\n");
368 return -EBUSY;
369 }
370
371 if(timeout < 1 || timeout > 3600) /* arbitrary upper limit */
372 {
373 timeout = WATCHDOG_TIMEOUT;
374 printk(KERN_INFO PFX "timeout value must be 1<=x<=3600, using %d\n",
375 timeout);
376 }
377
378 init_timer(&timer);
379 timer.function = wdt_timer_ping;
380 timer.data = 1;
381
382 rc = misc_register(&wdt_miscdev);
383 if (rc) {
384 printk(KERN_ERR PFX "cannot register miscdev on minor=%d (err=%d)\n",
385 wdt_miscdev.minor, rc);
386 goto err_out;
387 }
388
389 rc = register_reboot_notifier(&wdt_notifier);
390 if (rc) {
391 printk(KERN_ERR PFX "cannot register reboot notifier (err=%d)\n",
392 rc);
393 goto err_out_miscdev;
394 }
395
396 if (nowayout) {
397 __module_get(THIS_MODULE);
398 }
399
400 printk(KERN_INFO PFX "WDT driver for ALi M7101 initialised. timeout=%d sec (nowayout=%d)\n",
401 timeout, nowayout);
402 return 0;
403
404err_out_miscdev:
405 misc_deregister(&wdt_miscdev);
406err_out:
407 return rc;
408}
409
410module_init(alim7101_wdt_init);
411module_exit(alim7101_wdt_unload);
412
413MODULE_AUTHOR("Steve Hill");
414MODULE_DESCRIPTION("ALi M7101 PMU Computer Watchdog Timer driver");
415MODULE_LICENSE("GPL");
416MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR);