isp1301_omap: remove IRQF_SAMPLE_RANDOM which is now a no-op
[linux-2.6.git] / drivers / watchdog / sp5100_tco.c
blobae5e82cb83fa80bb897aaf4131aee42616add6c1
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 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
25 #include <linux/module.h>
26 #include <linux/moduleparam.h>
27 #include <linux/types.h>
28 #include <linux/miscdevice.h>
29 #include <linux/watchdog.h>
30 #include <linux/init.h>
31 #include <linux/fs.h>
32 #include <linux/pci.h>
33 #include <linux/ioport.h>
34 #include <linux/platform_device.h>
35 #include <linux/uaccess.h>
36 #include <linux/io.h>
38 #include "sp5100_tco.h"
40 /* Module and version information */
41 #define TCO_VERSION "0.01"
42 #define TCO_MODULE_NAME "SP5100 TCO timer"
43 #define TCO_DRIVER_NAME TCO_MODULE_NAME ", v" TCO_VERSION
45 /* internal variables */
46 static u32 tcobase_phys;
47 static void __iomem *tcobase;
48 static unsigned int pm_iobase;
49 static DEFINE_SPINLOCK(tco_lock); /* Guards the hardware */
50 static unsigned long timer_alive;
51 static char tco_expect_close;
52 static struct pci_dev *sp5100_tco_pci;
54 /* the watchdog platform device */
55 static struct platform_device *sp5100_tco_platform_device;
57 /* module parameters */
59 #define WATCHDOG_HEARTBEAT 60 /* 60 sec default heartbeat. */
60 static int heartbeat = WATCHDOG_HEARTBEAT; /* in seconds */
61 module_param(heartbeat, int, 0);
62 MODULE_PARM_DESC(heartbeat, "Watchdog heartbeat in seconds. (default="
63 __MODULE_STRING(WATCHDOG_HEARTBEAT) ")");
65 static bool nowayout = WATCHDOG_NOWAYOUT;
66 module_param(nowayout, bool, 0);
67 MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started"
68 " (default=" __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
71 * Some TCO specific functions
73 static void tco_timer_start(void)
75 u32 val;
76 unsigned long flags;
78 spin_lock_irqsave(&tco_lock, flags);
79 val = readl(SP5100_WDT_CONTROL(tcobase));
80 val |= SP5100_WDT_START_STOP_BIT;
81 writel(val, SP5100_WDT_CONTROL(tcobase));
82 spin_unlock_irqrestore(&tco_lock, flags);
85 static void tco_timer_stop(void)
87 u32 val;
88 unsigned long flags;
90 spin_lock_irqsave(&tco_lock, flags);
91 val = readl(SP5100_WDT_CONTROL(tcobase));
92 val &= ~SP5100_WDT_START_STOP_BIT;
93 writel(val, SP5100_WDT_CONTROL(tcobase));
94 spin_unlock_irqrestore(&tco_lock, flags);
97 static void tco_timer_keepalive(void)
99 u32 val;
100 unsigned long flags;
102 spin_lock_irqsave(&tco_lock, flags);
103 val = readl(SP5100_WDT_CONTROL(tcobase));
104 val |= SP5100_WDT_TRIGGER_BIT;
105 writel(val, SP5100_WDT_CONTROL(tcobase));
106 spin_unlock_irqrestore(&tco_lock, flags);
109 static int tco_timer_set_heartbeat(int t)
111 unsigned long flags;
113 if (t < 0 || t > 0xffff)
114 return -EINVAL;
116 /* Write new heartbeat to watchdog */
117 spin_lock_irqsave(&tco_lock, flags);
118 writel(t, SP5100_WDT_COUNT(tcobase));
119 spin_unlock_irqrestore(&tco_lock, flags);
121 heartbeat = t;
122 return 0;
126 * /dev/watchdog handling
129 static int sp5100_tco_open(struct inode *inode, struct file *file)
131 /* /dev/watchdog can only be opened once */
132 if (test_and_set_bit(0, &timer_alive))
133 return -EBUSY;
135 /* Reload and activate timer */
136 tco_timer_start();
137 tco_timer_keepalive();
138 return nonseekable_open(inode, file);
141 static int sp5100_tco_release(struct inode *inode, struct file *file)
143 /* Shut off the timer. */
144 if (tco_expect_close == 42) {
145 tco_timer_stop();
146 } else {
147 pr_crit("Unexpected close, not stopping watchdog!\n");
148 tco_timer_keepalive();
150 clear_bit(0, &timer_alive);
151 tco_expect_close = 0;
152 return 0;
155 static ssize_t sp5100_tco_write(struct file *file, const char __user *data,
156 size_t len, loff_t *ppos)
158 /* See if we got the magic character 'V' and reload the timer */
159 if (len) {
160 if (!nowayout) {
161 size_t i;
163 /* note: just in case someone wrote the magic character
164 * five months ago... */
165 tco_expect_close = 0;
167 /* scan to see whether or not we got the magic character
169 for (i = 0; i != len; i++) {
170 char c;
171 if (get_user(c, data + i))
172 return -EFAULT;
173 if (c == 'V')
174 tco_expect_close = 42;
178 /* someone wrote to us, we should reload the timer */
179 tco_timer_keepalive();
181 return len;
184 static long sp5100_tco_ioctl(struct file *file, unsigned int cmd,
185 unsigned long arg)
187 int new_options, retval = -EINVAL;
188 int new_heartbeat;
189 void __user *argp = (void __user *)arg;
190 int __user *p = argp;
191 static const struct watchdog_info ident = {
192 .options = WDIOF_SETTIMEOUT |
193 WDIOF_KEEPALIVEPING |
194 WDIOF_MAGICCLOSE,
195 .firmware_version = 0,
196 .identity = TCO_MODULE_NAME,
199 switch (cmd) {
200 case WDIOC_GETSUPPORT:
201 return copy_to_user(argp, &ident,
202 sizeof(ident)) ? -EFAULT : 0;
203 case WDIOC_GETSTATUS:
204 case WDIOC_GETBOOTSTATUS:
205 return put_user(0, p);
206 case WDIOC_SETOPTIONS:
207 if (get_user(new_options, p))
208 return -EFAULT;
209 if (new_options & WDIOS_DISABLECARD) {
210 tco_timer_stop();
211 retval = 0;
213 if (new_options & WDIOS_ENABLECARD) {
214 tco_timer_start();
215 tco_timer_keepalive();
216 retval = 0;
218 return retval;
219 case WDIOC_KEEPALIVE:
220 tco_timer_keepalive();
221 return 0;
222 case WDIOC_SETTIMEOUT:
223 if (get_user(new_heartbeat, p))
224 return -EFAULT;
225 if (tco_timer_set_heartbeat(new_heartbeat))
226 return -EINVAL;
227 tco_timer_keepalive();
228 /* Fall through */
229 case WDIOC_GETTIMEOUT:
230 return put_user(heartbeat, p);
231 default:
232 return -ENOTTY;
237 * Kernel Interfaces
240 static const struct file_operations sp5100_tco_fops = {
241 .owner = THIS_MODULE,
242 .llseek = no_llseek,
243 .write = sp5100_tco_write,
244 .unlocked_ioctl = sp5100_tco_ioctl,
245 .open = sp5100_tco_open,
246 .release = sp5100_tco_release,
249 static struct miscdevice sp5100_tco_miscdev = {
250 .minor = WATCHDOG_MINOR,
251 .name = "watchdog",
252 .fops = &sp5100_tco_fops,
256 * Data for PCI driver interface
258 * This data only exists for exporting the supported
259 * PCI ids via MODULE_DEVICE_TABLE. We do not actually
260 * register a pci_driver, because someone else might
261 * want to register another driver on the same PCI id.
263 static DEFINE_PCI_DEVICE_TABLE(sp5100_tco_pci_tbl) = {
264 { PCI_VENDOR_ID_ATI, PCI_DEVICE_ID_ATI_SBX00_SMBUS, PCI_ANY_ID,
265 PCI_ANY_ID, },
266 { 0, }, /* End of list */
268 MODULE_DEVICE_TABLE(pci, sp5100_tco_pci_tbl);
271 * Init & exit routines
274 static unsigned char __devinit sp5100_tco_setupdevice(void)
276 struct pci_dev *dev = NULL;
277 u32 val;
279 /* Match the PCI device */
280 for_each_pci_dev(dev) {
281 if (pci_match_id(sp5100_tco_pci_tbl, dev) != NULL) {
282 sp5100_tco_pci = dev;
283 break;
287 if (!sp5100_tco_pci)
288 return 0;
290 /* Request the IO ports used by this driver */
291 pm_iobase = SP5100_IO_PM_INDEX_REG;
292 if (!request_region(pm_iobase, SP5100_PM_IOPORTS_SIZE, "SP5100 TCO")) {
293 pr_err("I/O address 0x%04x already in use\n", 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 if (!request_mem_region_exclusive(val, SP5100_WDT_MEM_MAP_SIZE,
309 "SP5100 TCO")) {
310 pr_err("mmio address 0x%04x already in use\n", val);
311 goto unreg_region;
313 tcobase_phys = val;
315 tcobase = ioremap(val, SP5100_WDT_MEM_MAP_SIZE);
316 if (!tcobase) {
317 pr_err("failed to get tcobase address\n");
318 goto unreg_mem_region;
321 /* Enable watchdog decode bit */
322 pci_read_config_dword(sp5100_tco_pci,
323 SP5100_PCI_WATCHDOG_MISC_REG,
324 &val);
326 val |= SP5100_PCI_WATCHDOG_DECODE_EN;
328 pci_write_config_dword(sp5100_tco_pci,
329 SP5100_PCI_WATCHDOG_MISC_REG,
330 val);
332 /* Enable Watchdog timer and set the resolution to 1 sec. */
333 outb(SP5100_PM_WATCHDOG_CONTROL, SP5100_IO_PM_INDEX_REG);
334 val = inb(SP5100_IO_PM_DATA_REG);
335 val |= SP5100_PM_WATCHDOG_SECOND_RES;
336 val &= ~SP5100_PM_WATCHDOG_DISABLE;
337 outb(val, SP5100_IO_PM_DATA_REG);
339 /* Check that the watchdog action is set to reset the system. */
340 val = readl(SP5100_WDT_CONTROL(tcobase));
341 val &= ~SP5100_PM_WATCHDOG_ACTION_RESET;
342 writel(val, SP5100_WDT_CONTROL(tcobase));
344 /* Set a reasonable heartbeat before we stop the timer */
345 tco_timer_set_heartbeat(heartbeat);
348 * Stop the TCO before we change anything so we don't race with
349 * a zeroed timer.
351 tco_timer_stop();
353 /* Done */
354 return 1;
356 unreg_mem_region:
357 release_mem_region(tcobase_phys, SP5100_WDT_MEM_MAP_SIZE);
358 unreg_region:
359 release_region(pm_iobase, SP5100_PM_IOPORTS_SIZE);
360 exit:
361 return 0;
364 static int __devinit sp5100_tco_init(struct platform_device *dev)
366 int ret;
367 u32 val;
369 /* Check whether or not the hardware watchdog is there. If found, then
370 * set it up.
372 if (!sp5100_tco_setupdevice())
373 return -ENODEV;
375 /* Check to see if last reboot was due to watchdog timeout */
376 pr_info("Watchdog reboot %sdetected\n",
377 readl(SP5100_WDT_CONTROL(tcobase)) & SP5100_PM_WATCHDOG_FIRED ?
378 "" : "not ");
380 /* Clear out the old status */
381 val = readl(SP5100_WDT_CONTROL(tcobase));
382 val &= ~SP5100_PM_WATCHDOG_FIRED;
383 writel(val, SP5100_WDT_CONTROL(tcobase));
386 * Check that the heartbeat value is within it's range.
387 * If not, reset to the default.
389 if (tco_timer_set_heartbeat(heartbeat)) {
390 heartbeat = WATCHDOG_HEARTBEAT;
391 tco_timer_set_heartbeat(heartbeat);
394 ret = misc_register(&sp5100_tco_miscdev);
395 if (ret != 0) {
396 pr_err("cannot register miscdev on minor=%d (err=%d)\n",
397 WATCHDOG_MINOR, ret);
398 goto exit;
401 clear_bit(0, &timer_alive);
403 pr_info("initialized (0x%p). heartbeat=%d sec (nowayout=%d)\n",
404 tcobase, heartbeat, nowayout);
406 return 0;
408 exit:
409 iounmap(tcobase);
410 release_mem_region(tcobase_phys, SP5100_WDT_MEM_MAP_SIZE);
411 release_region(pm_iobase, SP5100_PM_IOPORTS_SIZE);
412 return ret;
415 static void __devexit sp5100_tco_cleanup(void)
417 /* Stop the timer before we leave */
418 if (!nowayout)
419 tco_timer_stop();
421 /* Deregister */
422 misc_deregister(&sp5100_tco_miscdev);
423 iounmap(tcobase);
424 release_mem_region(tcobase_phys, SP5100_WDT_MEM_MAP_SIZE);
425 release_region(pm_iobase, SP5100_PM_IOPORTS_SIZE);
428 static int __devexit sp5100_tco_remove(struct platform_device *dev)
430 if (tcobase)
431 sp5100_tco_cleanup();
432 return 0;
435 static void sp5100_tco_shutdown(struct platform_device *dev)
437 tco_timer_stop();
440 static struct platform_driver sp5100_tco_driver = {
441 .probe = sp5100_tco_init,
442 .remove = __devexit_p(sp5100_tco_remove),
443 .shutdown = sp5100_tco_shutdown,
444 .driver = {
445 .owner = THIS_MODULE,
446 .name = TCO_MODULE_NAME,
450 static int __init sp5100_tco_init_module(void)
452 int err;
454 pr_info("SP5100 TCO WatchDog Timer Driver v%s\n", TCO_VERSION);
456 err = platform_driver_register(&sp5100_tco_driver);
457 if (err)
458 return err;
460 sp5100_tco_platform_device = platform_device_register_simple(
461 TCO_MODULE_NAME, -1, NULL, 0);
462 if (IS_ERR(sp5100_tco_platform_device)) {
463 err = PTR_ERR(sp5100_tco_platform_device);
464 goto unreg_platform_driver;
467 return 0;
469 unreg_platform_driver:
470 platform_driver_unregister(&sp5100_tco_driver);
471 return err;
474 static void __exit sp5100_tco_cleanup_module(void)
476 platform_device_unregister(sp5100_tco_platform_device);
477 platform_driver_unregister(&sp5100_tco_driver);
478 pr_info("SP5100 TCO Watchdog Module Unloaded\n");
481 module_init(sp5100_tco_init_module);
482 module_exit(sp5100_tco_cleanup_module);
484 MODULE_AUTHOR("Priyanka Gupta");
485 MODULE_DESCRIPTION("TCO timer driver for SP5100 chipset");
486 MODULE_LICENSE("GPL");
487 MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR);