FRV: Use generic show_interrupts()
[cris-mirror.git] / drivers / watchdog / sp5100_tco.c
blob1bc493848ed4f7bdd5cd0d2117c6238ecded933b
1 /*
2 * sp5100_tco : TCO timer driver for sp5100 chipsets
4 * (c) Copyright 2009 Google Inc., All Rights Reserved.
6 * Based on i8xx_tco.c:
7 * (c) Copyright 2000 kernel concepts <nils@kernelconcepts.de>, All Rights
8 * Reserved.
9 * http://www.kernelconcepts.de
11 * This program is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU General Public License
13 * as published by the Free Software Foundation; either version
14 * 2 of the License, or (at your option) any later version.
16 * See AMD Publication 43009 "AMD SB700/710/750 Register Reference Guide"
20 * Includes, defines, variables, module parameters, ...
23 #include <linux/module.h>
24 #include <linux/moduleparam.h>
25 #include <linux/types.h>
26 #include <linux/miscdevice.h>
27 #include <linux/watchdog.h>
28 #include <linux/init.h>
29 #include <linux/fs.h>
30 #include <linux/pci.h>
31 #include <linux/ioport.h>
32 #include <linux/platform_device.h>
33 #include <linux/uaccess.h>
34 #include <linux/io.h>
36 #include "sp5100_tco.h"
38 /* Module and version information */
39 #define TCO_VERSION "0.01"
40 #define TCO_MODULE_NAME "SP5100 TCO timer"
41 #define TCO_DRIVER_NAME TCO_MODULE_NAME ", v" TCO_VERSION
42 #define PFX TCO_MODULE_NAME ": "
44 /* internal variables */
45 static void __iomem *tcobase;
46 static unsigned int pm_iobase;
47 static DEFINE_SPINLOCK(tco_lock); /* Guards the hardware */
48 static unsigned long timer_alive;
49 static char tco_expect_close;
50 static struct pci_dev *sp5100_tco_pci;
52 /* the watchdog platform device */
53 static struct platform_device *sp5100_tco_platform_device;
55 /* module parameters */
57 #define WATCHDOG_HEARTBEAT 60 /* 60 sec default heartbeat. */
58 static int heartbeat = WATCHDOG_HEARTBEAT; /* in seconds */
59 module_param(heartbeat, int, 0);
60 MODULE_PARM_DESC(heartbeat, "Watchdog heartbeat in seconds. (default="
61 __MODULE_STRING(WATCHDOG_HEARTBEAT) ")");
63 static int nowayout = WATCHDOG_NOWAYOUT;
64 module_param(nowayout, int, 0);
65 MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started"
66 " (default=" __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
69 * Some TCO specific functions
71 static void tco_timer_start(void)
73 u32 val;
74 unsigned long flags;
76 spin_lock_irqsave(&tco_lock, flags);
77 val = readl(SP5100_WDT_CONTROL(tcobase));
78 val |= SP5100_WDT_START_STOP_BIT;
79 writel(val, SP5100_WDT_CONTROL(tcobase));
80 spin_unlock_irqrestore(&tco_lock, flags);
83 static void tco_timer_stop(void)
85 u32 val;
86 unsigned long flags;
88 spin_lock_irqsave(&tco_lock, flags);
89 val = readl(SP5100_WDT_CONTROL(tcobase));
90 val &= ~SP5100_WDT_START_STOP_BIT;
91 writel(val, SP5100_WDT_CONTROL(tcobase));
92 spin_unlock_irqrestore(&tco_lock, flags);
95 static void tco_timer_keepalive(void)
97 u32 val;
98 unsigned long flags;
100 spin_lock_irqsave(&tco_lock, flags);
101 val = readl(SP5100_WDT_CONTROL(tcobase));
102 val |= SP5100_WDT_TRIGGER_BIT;
103 writel(val, SP5100_WDT_CONTROL(tcobase));
104 spin_unlock_irqrestore(&tco_lock, flags);
107 static int tco_timer_set_heartbeat(int t)
109 unsigned long flags;
111 if (t < 0 || t > 0xffff)
112 return -EINVAL;
114 /* Write new heartbeat to watchdog */
115 spin_lock_irqsave(&tco_lock, flags);
116 writel(t, SP5100_WDT_COUNT(tcobase));
117 spin_unlock_irqrestore(&tco_lock, flags);
119 heartbeat = t;
120 return 0;
124 * /dev/watchdog handling
127 static int sp5100_tco_open(struct inode *inode, struct file *file)
129 /* /dev/watchdog can only be opened once */
130 if (test_and_set_bit(0, &timer_alive))
131 return -EBUSY;
133 /* Reload and activate timer */
134 tco_timer_start();
135 tco_timer_keepalive();
136 return nonseekable_open(inode, file);
139 static int sp5100_tco_release(struct inode *inode, struct file *file)
141 /* Shut off the timer. */
142 if (tco_expect_close == 42) {
143 tco_timer_stop();
144 } else {
145 printk(KERN_CRIT PFX
146 "Unexpected close, not stopping watchdog!\n");
147 tco_timer_keepalive();
149 clear_bit(0, &timer_alive);
150 tco_expect_close = 0;
151 return 0;
154 static ssize_t sp5100_tco_write(struct file *file, const char __user *data,
155 size_t len, loff_t *ppos)
157 /* See if we got the magic character 'V' and reload the timer */
158 if (len) {
159 if (!nowayout) {
160 size_t i;
162 /* note: just in case someone wrote the magic character
163 * five months ago... */
164 tco_expect_close = 0;
166 /* scan to see whether or not we got the magic character
168 for (i = 0; i != len; i++) {
169 char c;
170 if (get_user(c, data + i))
171 return -EFAULT;
172 if (c == 'V')
173 tco_expect_close = 42;
177 /* someone wrote to us, we should reload the timer */
178 tco_timer_keepalive();
180 return len;
183 static long sp5100_tco_ioctl(struct file *file, unsigned int cmd,
184 unsigned long arg)
186 int new_options, retval = -EINVAL;
187 int new_heartbeat;
188 void __user *argp = (void __user *)arg;
189 int __user *p = argp;
190 static const struct watchdog_info ident = {
191 .options = WDIOF_SETTIMEOUT |
192 WDIOF_KEEPALIVEPING |
193 WDIOF_MAGICCLOSE,
194 .firmware_version = 0,
195 .identity = TCO_MODULE_NAME,
198 switch (cmd) {
199 case WDIOC_GETSUPPORT:
200 return copy_to_user(argp, &ident,
201 sizeof(ident)) ? -EFAULT : 0;
202 case WDIOC_GETSTATUS:
203 case WDIOC_GETBOOTSTATUS:
204 return put_user(0, p);
205 case WDIOC_SETOPTIONS:
206 if (get_user(new_options, p))
207 return -EFAULT;
208 if (new_options & WDIOS_DISABLECARD) {
209 tco_timer_stop();
210 retval = 0;
212 if (new_options & WDIOS_ENABLECARD) {
213 tco_timer_start();
214 tco_timer_keepalive();
215 retval = 0;
217 return retval;
218 case WDIOC_KEEPALIVE:
219 tco_timer_keepalive();
220 return 0;
221 case WDIOC_SETTIMEOUT:
222 if (get_user(new_heartbeat, p))
223 return -EFAULT;
224 if (tco_timer_set_heartbeat(new_heartbeat))
225 return -EINVAL;
226 tco_timer_keepalive();
227 /* Fall through */
228 case WDIOC_GETTIMEOUT:
229 return put_user(heartbeat, p);
230 default:
231 return -ENOTTY;
236 * Kernel Interfaces
239 static const struct file_operations sp5100_tco_fops = {
240 .owner = THIS_MODULE,
241 .llseek = no_llseek,
242 .write = sp5100_tco_write,
243 .unlocked_ioctl = sp5100_tco_ioctl,
244 .open = sp5100_tco_open,
245 .release = sp5100_tco_release,
248 static struct miscdevice sp5100_tco_miscdev = {
249 .minor = WATCHDOG_MINOR,
250 .name = "watchdog",
251 .fops = &sp5100_tco_fops,
255 * Data for PCI driver interface
257 * This data only exists for exporting the supported
258 * PCI ids via MODULE_DEVICE_TABLE. We do not actually
259 * register a pci_driver, because someone else might
260 * want to register another driver on the same PCI id.
262 static DEFINE_PCI_DEVICE_TABLE(sp5100_tco_pci_tbl) = {
263 { PCI_VENDOR_ID_ATI, PCI_DEVICE_ID_ATI_SBX00_SMBUS, PCI_ANY_ID,
264 PCI_ANY_ID, },
265 { 0, }, /* End of list */
267 MODULE_DEVICE_TABLE(pci, sp5100_tco_pci_tbl);
270 * Init & exit routines
273 static unsigned char __devinit sp5100_tco_setupdevice(void)
275 struct pci_dev *dev = NULL;
276 u32 val;
278 /* Match the PCI device */
279 for_each_pci_dev(dev) {
280 if (pci_match_id(sp5100_tco_pci_tbl, dev) != NULL) {
281 sp5100_tco_pci = dev;
282 break;
286 if (!sp5100_tco_pci)
287 return 0;
289 /* Request the IO ports used by this driver */
290 pm_iobase = SP5100_IO_PM_INDEX_REG;
291 if (!request_region(pm_iobase, SP5100_PM_IOPORTS_SIZE, "SP5100 TCO")) {
292 printk(KERN_ERR PFX "I/O address 0x%04x already in use\n",
293 pm_iobase);
294 goto exit;
297 /* Find the watchdog base address. */
298 outb(SP5100_PM_WATCHDOG_BASE3, SP5100_IO_PM_INDEX_REG);
299 val = inb(SP5100_IO_PM_DATA_REG);
300 outb(SP5100_PM_WATCHDOG_BASE2, SP5100_IO_PM_INDEX_REG);
301 val = val << 8 | inb(SP5100_IO_PM_DATA_REG);
302 outb(SP5100_PM_WATCHDOG_BASE1, SP5100_IO_PM_INDEX_REG);
303 val = val << 8 | inb(SP5100_IO_PM_DATA_REG);
304 outb(SP5100_PM_WATCHDOG_BASE0, SP5100_IO_PM_INDEX_REG);
305 /* Low three bits of BASE0 are reserved. */
306 val = val << 8 | (inb(SP5100_IO_PM_DATA_REG) & 0xf8);
308 tcobase = ioremap(val, SP5100_WDT_MEM_MAP_SIZE);
309 if (tcobase == 0) {
310 printk(KERN_ERR PFX "failed to get tcobase address\n");
311 goto unreg_region;
314 /* Enable watchdog decode bit */
315 pci_read_config_dword(sp5100_tco_pci,
316 SP5100_PCI_WATCHDOG_MISC_REG,
317 &val);
319 val |= SP5100_PCI_WATCHDOG_DECODE_EN;
321 pci_write_config_dword(sp5100_tco_pci,
322 SP5100_PCI_WATCHDOG_MISC_REG,
323 val);
325 /* Enable Watchdog timer and set the resolution to 1 sec. */
326 outb(SP5100_PM_WATCHDOG_CONTROL, SP5100_IO_PM_INDEX_REG);
327 val = inb(SP5100_IO_PM_DATA_REG);
328 val |= SP5100_PM_WATCHDOG_SECOND_RES;
329 val &= ~SP5100_PM_WATCHDOG_DISABLE;
330 outb(val, SP5100_IO_PM_DATA_REG);
332 /* Check that the watchdog action is set to reset the system. */
333 val = readl(SP5100_WDT_CONTROL(tcobase));
334 val &= ~SP5100_PM_WATCHDOG_ACTION_RESET;
335 writel(val, SP5100_WDT_CONTROL(tcobase));
337 /* Set a reasonable heartbeat before we stop the timer */
338 tco_timer_set_heartbeat(heartbeat);
341 * Stop the TCO before we change anything so we don't race with
342 * a zeroed timer.
344 tco_timer_stop();
346 /* Done */
347 return 1;
349 iounmap(tcobase);
350 unreg_region:
351 release_region(pm_iobase, SP5100_PM_IOPORTS_SIZE);
352 exit:
353 return 0;
356 static int __devinit sp5100_tco_init(struct platform_device *dev)
358 int ret;
359 u32 val;
361 /* Check whether or not the hardware watchdog is there. If found, then
362 * set it up.
364 if (!sp5100_tco_setupdevice())
365 return -ENODEV;
367 /* Check to see if last reboot was due to watchdog timeout */
368 printk(KERN_INFO PFX "Watchdog reboot %sdetected.\n",
369 readl(SP5100_WDT_CONTROL(tcobase)) & SP5100_PM_WATCHDOG_FIRED ?
370 "" : "not ");
372 /* Clear out the old status */
373 val = readl(SP5100_WDT_CONTROL(tcobase));
374 val &= ~SP5100_PM_WATCHDOG_FIRED;
375 writel(val, SP5100_WDT_CONTROL(tcobase));
378 * Check that the heartbeat value is within it's range.
379 * If not, reset to the default.
381 if (tco_timer_set_heartbeat(heartbeat)) {
382 heartbeat = WATCHDOG_HEARTBEAT;
383 tco_timer_set_heartbeat(heartbeat);
386 ret = misc_register(&sp5100_tco_miscdev);
387 if (ret != 0) {
388 printk(KERN_ERR PFX "cannot register miscdev on minor="
389 "%d (err=%d)\n",
390 WATCHDOG_MINOR, ret);
391 goto exit;
394 clear_bit(0, &timer_alive);
396 printk(KERN_INFO PFX "initialized (0x%p). heartbeat=%d sec"
397 " (nowayout=%d)\n",
398 tcobase, heartbeat, nowayout);
400 return 0;
402 exit:
403 iounmap(tcobase);
404 release_region(pm_iobase, SP5100_PM_IOPORTS_SIZE);
405 return ret;
408 static void __devexit sp5100_tco_cleanup(void)
410 /* Stop the timer before we leave */
411 if (!nowayout)
412 tco_timer_stop();
414 /* Deregister */
415 misc_deregister(&sp5100_tco_miscdev);
416 iounmap(tcobase);
417 release_region(pm_iobase, SP5100_PM_IOPORTS_SIZE);
420 static int __devexit sp5100_tco_remove(struct platform_device *dev)
422 if (tcobase)
423 sp5100_tco_cleanup();
424 return 0;
427 static void sp5100_tco_shutdown(struct platform_device *dev)
429 tco_timer_stop();
432 static struct platform_driver sp5100_tco_driver = {
433 .probe = sp5100_tco_init,
434 .remove = __devexit_p(sp5100_tco_remove),
435 .shutdown = sp5100_tco_shutdown,
436 .driver = {
437 .owner = THIS_MODULE,
438 .name = TCO_MODULE_NAME,
442 static int __init sp5100_tco_init_module(void)
444 int err;
446 printk(KERN_INFO PFX "SP5100 TCO WatchDog Timer Driver v%s\n",
447 TCO_VERSION);
449 err = platform_driver_register(&sp5100_tco_driver);
450 if (err)
451 return err;
453 sp5100_tco_platform_device = platform_device_register_simple(
454 TCO_MODULE_NAME, -1, NULL, 0);
455 if (IS_ERR(sp5100_tco_platform_device)) {
456 err = PTR_ERR(sp5100_tco_platform_device);
457 goto unreg_platform_driver;
460 return 0;
462 unreg_platform_driver:
463 platform_driver_unregister(&sp5100_tco_driver);
464 return err;
467 static void __exit sp5100_tco_cleanup_module(void)
469 platform_device_unregister(sp5100_tco_platform_device);
470 platform_driver_unregister(&sp5100_tco_driver);
471 printk(KERN_INFO PFX "SP5100 TCO Watchdog Module Unloaded.\n");
474 module_init(sp5100_tco_init_module);
475 module_exit(sp5100_tco_cleanup_module);
477 MODULE_AUTHOR("Priyanka Gupta");
478 MODULE_DESCRIPTION("TCO timer driver for SP5100 chipset");
479 MODULE_LICENSE("GPL");
480 MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR);