2 +++ b/drivers/watchdog/cns3xxx_wdt.c
4 +/*******************************************************************************
6 + * drivers/watchdog/cns3xxx_wdt.c
8 + * Watchdog timer driver for the CNS3XXX SOCs
12 + * Copyright (c) 2008 Cavium Networks
14 + * This file is free software; you can redistribute it and/or modify
15 + * it under the terms of the GNU General Public License, Version 2, as
16 + * published by the Free Software Foundation.
18 + * This file is distributed in the hope that it will be useful,
19 + * but AS-IS and WITHOUT ANY WARRANTY; without even the implied warranty of
20 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, TITLE, or
21 + * NONINFRINGEMENT. See the GNU General Public License for more details.
23 + * You should have received a copy of the GNU General Public License
24 + * along with this file; if not, write to the Free Software
25 + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA or
26 + * visit http://www.gnu.org/licenses/.
28 + * This file may also be available under a different license from Cavium.
29 + * Contact Cavium Networks for more information
31 + ******************************************************************************/
33 +#include <linux/module.h>
34 +#include <linux/moduleparam.h>
35 +#include <linux/types.h>
36 +#include <linux/miscdevice.h>
37 +#include <linux/watchdog.h>
38 +#include <linux/fs.h>
39 +#include <linux/reboot.h>
40 +#include <linux/init.h>
41 +#include <linux/interrupt.h>
42 +#include <linux/platform_device.h>
43 +#include <linux/io.h>
44 +#include <linux/uaccess.h>
46 +#include <asm/hardware/arm_twd.h>
49 + unsigned long timer_alive;
53 + unsigned int perturb;
57 +static struct platform_device *cns3xxx_wdt_dev;
58 +extern unsigned int twd_timer_rate;
59 +static spinlock_t wdt_lock;
61 +#define TIMER_MARGIN 60
62 +static int cns3xxx_margin = TIMER_MARGIN;
63 +module_param(cns3xxx_margin, int, 0);
64 +MODULE_PARM_DESC(cns3xxx_margin,
65 + "CNS3XXX timer margin in seconds. (0 < cns3xxx_margin < 65536, default="
66 + __MODULE_STRING(TIMER_MARGIN) ")");
68 +static int nowayout = WATCHDOG_NOWAYOUT;
69 +module_param(nowayout, int, 0);
70 +MODULE_PARM_DESC(nowayout,
71 + "Watchdog cannot be stopped once started (default="
72 + __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
74 +#define ONLY_TESTING 0
75 +static int cns3xxx_noboot = ONLY_TESTING;
76 +module_param(cns3xxx_noboot, int, 0);
77 +MODULE_PARM_DESC(cns3xxx_noboot, "CNS3XXX watchdog action, "
78 + "set to 1 to ignore reboots, 0 to reboot (default="
79 + __MODULE_STRING(ONLY_TESTING) ")");
82 + * This is the interrupt handler. Note that we only use this
83 + * in testing mode, so don't actually do a reboot here.
85 +static irqreturn_t cns3xxx_wdt_fire(int irq, void *arg)
87 + struct cns3xxx_wdt *wdt = arg;
89 + /* Check it really was our interrupt */
90 + if (readl(wdt->base + TWD_WDOG_INTSTAT)) {
91 + dev_printk(KERN_CRIT, wdt->dev,
92 + "Triggered - Reboot ignored.\n");
93 + /* Clear the interrupt on the watchdog */
94 + writel(1, wdt->base + TWD_WDOG_INTSTAT);
101 + * cns3xxx_wdt_keepalive - reload the timer
103 + * Note that the spec says a DIFFERENT value must be written to the reload
104 + * register each time. The "perturb" variable deals with this by adding 1
105 + * to the count every other time the function is called.
107 +static void cns3xxx_wdt_keepalive(struct cns3xxx_wdt *wdt)
109 + unsigned int count;
111 + /* Assume prescale is set to 256 */
112 + count = (twd_timer_rate / 256) * cns3xxx_margin;
114 + /* Reload the counter */
115 + spin_lock(&wdt_lock);
116 + writel(count + wdt->perturb, wdt->base + TWD_WDOG_LOAD);
117 + wdt->perturb = wdt->perturb ? 0 : 1;
118 + spin_unlock(&wdt_lock);
121 +static void cns3xxx_wdt_stop(struct cns3xxx_wdt *wdt)
123 + spin_lock(&wdt_lock);
124 + writel(0x12345678, wdt->base + TWD_WDOG_DISABLE);
125 + writel(0x87654321, wdt->base + TWD_WDOG_DISABLE);
126 + writel(0x0, wdt->base + TWD_WDOG_CONTROL);
127 + spin_unlock(&wdt_lock);
130 +static void cns3xxx_wdt_start(struct cns3xxx_wdt *wdt)
132 + dev_printk(KERN_INFO, wdt->dev, "enabling watchdog.\n");
134 + //spin_lock(&wdt_lock);
135 + /* This loads the count register but does NOT start the count yet */
136 + cns3xxx_wdt_keepalive(wdt);
137 + spin_lock(&wdt_lock);
139 + if (cns3xxx_noboot) {
140 + /* Enable watchdog - prescale=256, watchdog mode=0, enable=1 */
141 + writel(0x0000FF01, wdt->base + TWD_WDOG_CONTROL);
143 + /* Enable watchdog - prescale=256, watchdog mode=1, enable=1 */
144 + writel(0x0000FF09, wdt->base + TWD_WDOG_CONTROL);
146 + spin_unlock(&wdt_lock);
149 +static int cns3xxx_wdt_set_heartbeat(int t)
151 + if (t < 0x0001 || t > 0xFFFF)
154 + cns3xxx_margin = t;
159 + * /dev/watchdog handling
161 +static int cns3xxx_wdt_open(struct inode *inode, struct file *file)
163 + struct cns3xxx_wdt *wdt = platform_get_drvdata(cns3xxx_wdt_dev);
165 + if (test_and_set_bit(0, &wdt->timer_alive))
169 + __module_get(THIS_MODULE);
171 + file->private_data = wdt;
176 + cns3xxx_wdt_start(wdt);
178 + return nonseekable_open(inode, file);
181 +static int cns3xxx_wdt_release(struct inode *inode, struct file *file)
183 + struct cns3xxx_wdt *wdt = file->private_data;
186 + * Shut off the timer.
187 + * Lock it in if it's a module and we set nowayout
189 + if (wdt->expect_close == 42)
190 + cns3xxx_wdt_stop(wdt);
192 + dev_printk(KERN_CRIT, wdt->dev,
193 + "unexpected close, not stopping watchdog!\n");
194 + cns3xxx_wdt_keepalive(wdt);
196 + clear_bit(0, &wdt->timer_alive);
197 + wdt->expect_close = 0;
201 +static ssize_t cns3xxx_wdt_write(struct file *file, const char *data,
202 + size_t len, loff_t *ppos)
204 + struct cns3xxx_wdt *wdt = file->private_data;
207 + * Refresh the timer.
213 + /* In case it was set long ago */
214 + wdt->expect_close = 0;
216 + for (i = 0; i != len; i++) {
219 + if (get_user(c, data + i))
222 + wdt->expect_close = 42;
225 + cns3xxx_wdt_keepalive(wdt);
230 +static struct watchdog_info ident = {
231 + .options = WDIOF_SETTIMEOUT |
232 + WDIOF_KEEPALIVEPING |
234 + .identity = "CNS3XXX Watchdog",
237 +static long cns3xxx_wdt_ioctl(struct file *file, unsigned int cmd,
240 + struct cns3xxx_wdt *wdt = file->private_data;
243 + struct watchdog_info ident;
247 + if (_IOC_DIR(cmd) && _IOC_SIZE(cmd) > sizeof(uarg))
250 + if (_IOC_DIR(cmd) & _IOC_WRITE) {
251 + ret = copy_from_user(&uarg, (void __user *)arg, _IOC_SIZE(cmd));
257 + case WDIOC_GETSUPPORT:
258 + uarg.ident = ident;
262 + case WDIOC_GETSTATUS:
263 + case WDIOC_GETBOOTSTATUS:
268 + case WDIOC_SETOPTIONS:
270 + if (uarg.i & WDIOS_DISABLECARD) {
271 + cns3xxx_wdt_stop(wdt);
274 + if (uarg.i & WDIOS_ENABLECARD) {
275 + cns3xxx_wdt_start(wdt);
280 + case WDIOC_KEEPALIVE:
281 + cns3xxx_wdt_keepalive(wdt);
285 + case WDIOC_SETTIMEOUT:
286 + ret = cns3xxx_wdt_set_heartbeat(uarg.i);
290 + cns3xxx_wdt_keepalive(wdt);
292 + case WDIOC_GETTIMEOUT:
293 + uarg.i = cns3xxx_margin;
301 + if (ret == 0 && _IOC_DIR(cmd) & _IOC_READ) {
302 + ret = copy_to_user((void __user *)arg, &uarg, _IOC_SIZE(cmd));
310 + * System shutdown handler. Turn off the watchdog if we're
311 + * restarting or halting the system.
313 +static void cns3xxx_wdt_shutdown(struct platform_device *dev)
315 + struct cns3xxx_wdt *wdt = platform_get_drvdata(dev);
317 + if (system_state == SYSTEM_RESTART || system_state == SYSTEM_HALT)
318 + cns3xxx_wdt_stop(wdt);
322 + * Kernel Interfaces
324 +static const struct file_operations cns3xxx_wdt_fops = {
325 + .owner = THIS_MODULE,
326 + .llseek = no_llseek,
327 + .write = cns3xxx_wdt_write,
328 + .unlocked_ioctl = cns3xxx_wdt_ioctl,
329 + .open = cns3xxx_wdt_open,
330 + .release = cns3xxx_wdt_release,
333 +static struct miscdevice cns3xxx_wdt_miscdev = {
334 + .minor = WATCHDOG_MINOR,
335 + .name = "watchdog",
336 + .fops = &cns3xxx_wdt_fops,
339 +static int __devinit cns3xxx_wdt_probe(struct platform_device *dev)
341 + struct cns3xxx_wdt *wdt;
342 + struct resource *res;
345 + /* We only accept one device, and it must have an id of -1 */
349 + res = platform_get_resource(dev, IORESOURCE_MEM, 0);
355 + wdt = kzalloc(sizeof(struct cns3xxx_wdt), GFP_KERNEL);
361 + wdt->dev = &dev->dev;
362 + wdt->irq = platform_get_irq(dev, 0);
363 + if (wdt->irq < 0) {
367 + wdt->base = ioremap(res->start, res->end - res->start + 1);
373 + cns3xxx_wdt_miscdev.parent = &dev->dev;
374 + ret = misc_register(&cns3xxx_wdt_miscdev);
376 + dev_printk(KERN_ERR, wdt->dev,
377 + "cannot register miscdev on minor=%d (err=%d)\n",
378 + WATCHDOG_MINOR, ret);
382 + ret = request_irq(wdt->irq, cns3xxx_wdt_fire, IRQF_DISABLED,
385 + dev_printk(KERN_ERR, wdt->dev,
386 + "cannot register IRQ%d for watchdog\n", wdt->irq);
390 + cns3xxx_wdt_stop(wdt);
391 + platform_set_drvdata(dev, wdt);
392 + cns3xxx_wdt_dev = dev;
397 + misc_deregister(&cns3xxx_wdt_miscdev);
399 + platform_set_drvdata(dev, NULL);
400 + iounmap(wdt->base);
407 +static int __devexit cns3xxx_wdt_remove(struct platform_device *dev)
409 + struct cns3xxx_wdt *wdt = platform_get_drvdata(dev);
411 + platform_set_drvdata(dev, NULL);
413 + misc_deregister(&cns3xxx_wdt_miscdev);
415 + cns3xxx_wdt_dev = NULL;
417 + free_irq(wdt->irq, wdt);
418 + iounmap(wdt->base);
424 +static struct platform_driver cns3xxx_wdt_driver = {
425 + .probe = cns3xxx_wdt_probe,
426 + .remove = __devexit_p(cns3xxx_wdt_remove),
427 + .shutdown = cns3xxx_wdt_shutdown,
429 + .owner = THIS_MODULE,
430 + .name = "cns3xxx-wdt",
434 +static char banner[] __initdata = KERN_INFO
435 + "CNS3XXX Watchdog Timer, noboot=%d margin=%d sec (nowayout= %d)\n";
437 +static int __init cns3xxx_wdt_init(void)
440 + * Check that the margin value is within it's range;
441 + * if not reset to the default
443 + if (cns3xxx_wdt_set_heartbeat(cns3xxx_margin)) {
444 + cns3xxx_wdt_set_heartbeat(TIMER_MARGIN);
445 + printk(KERN_INFO "cns3xxx_margin value must be 0 < cns3xxx_margin < 65536, using %d\n",
449 + printk(banner, cns3xxx_noboot, cns3xxx_margin, nowayout);
451 + spin_lock_init(&wdt_lock);
453 + return platform_driver_register(&cns3xxx_wdt_driver);
456 +static void __exit cns3xxx_wdt_exit(void)
458 + platform_driver_unregister(&cns3xxx_wdt_driver);
461 +module_init(cns3xxx_wdt_init);
462 +module_exit(cns3xxx_wdt_exit);
464 +MODULE_AUTHOR("Scott Shu");
465 +MODULE_DESCRIPTION("CNS3XXX Watchdog Device Driver");
466 +MODULE_LICENSE("GPL");
467 +MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR);
468 +MODULE_ALIAS("platform:cns3xxx-wdt");
469 --- a/drivers/watchdog/Kconfig
470 +++ b/drivers/watchdog/Kconfig
471 @@ -231,6 +231,15 @@ config DAVINCI_WATCHDOG
472 NOTE: once enabled, this timer cannot be disabled.
473 Say N if you are unsure.
475 +config CNS3XXX_WATCHDOG
476 + tristate "CNS3XXX watchdog"
477 + depends on ARCH_CNS3XXX && LOCAL_TIMERS
479 + Watchdog timer embedded into the CNS3XXX SoCs system.
481 + To compile this driver as a module, choose M here: the
482 + module will be called cns3xxx_wdt.
484 config ORION_WATCHDOG
485 tristate "Orion watchdog"
486 depends on ARCH_ORION5X || ARCH_KIRKWOOD
487 --- a/drivers/watchdog/Makefile
488 +++ b/drivers/watchdog/Makefile
489 @@ -41,6 +41,7 @@ obj-$(CONFIG_EP93XX_WATCHDOG) += ep93xx_
490 obj-$(CONFIG_PNX4008_WATCHDOG) += pnx4008_wdt.o
491 obj-$(CONFIG_IOP_WATCHDOG) += iop_wdt.o
492 obj-$(CONFIG_DAVINCI_WATCHDOG) += davinci_wdt.o
493 +obj-$(CONFIG_CNS3XXX_WATCHDOG) += cns3xxx_wdt.o
494 obj-$(CONFIG_ORION_WATCHDOG) += orion_wdt.o
495 obj-$(CONFIG_COH901327_WATCHDOG) += coh901327_wdt.o
496 obj-$(CONFIG_STMP3XXX_WATCHDOG) += stmp3xxx_wdt.o