2 +++ b/drivers/watchdog/bcm2708_wdog.c
5 + * Broadcom BCM2708 watchdog driver.
7 + * (c) Copyright 2010 Broadcom Europe Ltd
9 + * This program is free software; you can redistribute it and/or
10 + * modify it under the terms of the GNU General Public License
11 + * as published by the Free Software Foundation; either version
12 + * 2 of the License, or (at your option) any later version.
14 + * BCM2708 watchdog driver. Loosely based on wdt driver.
17 +#include <linux/interrupt.h>
18 +#include <linux/module.h>
19 +#include <linux/moduleparam.h>
20 +#include <linux/types.h>
21 +#include <linux/miscdevice.h>
22 +#include <linux/watchdog.h>
23 +#include <linux/fs.h>
24 +#include <linux/ioport.h>
25 +#include <linux/notifier.h>
26 +#include <linux/reboot.h>
27 +#include <linux/init.h>
28 +#include <linux/io.h>
29 +#include <linux/uaccess.h>
30 +#include <mach/platform.h>
32 +#include <asm/system.h>
34 +#define SECS_TO_WDOG_TICKS(x) ((x) << 16)
35 +#define WDOG_TICKS_TO_SECS(x) ((x) >> 16)
37 +static unsigned long wdog_is_open;
38 +static uint32_t wdog_ticks; /* Ticks to load into wdog timer */
39 +static char expect_close;
45 +#define WD_TIMO 10 /* Default heartbeat = 60 seconds */
46 +static int heartbeat = WD_TIMO; /* Heartbeat in seconds */
48 +module_param(heartbeat, int, 0);
49 +MODULE_PARM_DESC(heartbeat,
50 + "Watchdog heartbeat in seconds. (0 < heartbeat < 65536, default="
51 + __MODULE_STRING(WD_TIMO) ")");
53 +static int nowayout = WATCHDOG_NOWAYOUT;
54 +module_param(nowayout, int, 0);
55 +MODULE_PARM_DESC(nowayout,
56 + "Watchdog cannot be stopped once started (default="
57 + __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
59 +static DEFINE_SPINLOCK(wdog_lock);
62 + * Start the watchdog driver.
65 +static int wdog_start(unsigned long timeout)
68 + unsigned long flags;
69 + spin_lock_irqsave(&wdog_lock, flags);
71 + /* enable the watchdog */
72 + iowrite32(PM_PASSWORD | (timeout & PM_WDOG_TIME_SET),
73 + __io_address(PM_WDOG));
74 + cur = ioread32(__io_address(PM_RSTC));
75 + iowrite32(PM_PASSWORD | (cur & PM_RSTC_WRCFG_CLR) |
76 + PM_RSTC_WRCFG_FULL_RESET, __io_address(PM_RSTC));
78 + spin_unlock_irqrestore(&wdog_lock, flags);
83 + * Stop the watchdog driver.
86 +static int wdog_stop(void)
88 + iowrite32(PM_PASSWORD | PM_RSTC_RESET, __io_address(PM_RSTC));
89 + printk(KERN_INFO "watchdog stopped\n");
94 + * Reload counter one with the watchdog heartbeat. We don't bother
95 + * reloading the cascade counter.
98 +static void wdog_ping(void)
100 + wdog_start(wdog_ticks);
104 + * @t: the new heartbeat value that needs to be set.
106 + * Set a new heartbeat value for the watchdog device. If the heartbeat
107 + * value is incorrect we keep the old value and return -EINVAL. If
108 + * successful we return 0.
111 +static int wdog_set_heartbeat(int t)
113 + if (t < 1 || t > WDOG_TICKS_TO_SECS(PM_WDOG_TIME_SET))
117 + wdog_ticks = SECS_TO_WDOG_TICKS(t);
122 + * @file: file handle to the watchdog
123 + * @buf: buffer to write (unused as data does not matter here
124 + * @count: count of bytes
125 + * @ppos: pointer to the position to write. No seeks allowed
127 + * A write to a watchdog device is defined as a keepalive signal.
129 + * if 'nowayout' is set then normally a close() is ignored. But
130 + * if you write 'V' first then the close() will stop the timer.
133 +static ssize_t wdog_write(struct file *file, const char __user *buf,
134 + size_t count, loff_t *ppos)
140 + /* In case it was set long ago */
143 + for (i = 0; i != count; i++) {
145 + if (get_user(c, buf + i))
156 +static int wdog_get_status(void)
158 + unsigned long flags;
160 + spin_lock_irqsave(&wdog_lock, flags);
161 + /* FIXME: readback reset reason */
162 + spin_unlock_irqrestore(&wdog_lock, flags);
166 +static uint32_t wdog_get_remaining(void)
168 + uint32_t ret = ioread32(__io_address(PM_WDOG));
169 + return ret & PM_WDOG_TIME_SET;
173 + * @file: file handle to the device
174 + * @cmd: watchdog command
175 + * @arg: argument pointer
177 + * The watchdog API defines a common set of functions for all watchdogs
178 + * according to their available features. We only actually usefully support
179 + * querying capabilities and current status.
182 +static long wdog_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
184 + void __user *argp = (void __user *)arg;
185 + int __user *p = argp;
189 + uint32_t remaining;
191 + struct watchdog_info ident = {
192 + .options = WDIOF_SETTIMEOUT|
194 + WDIOF_KEEPALIVEPING,
195 + .firmware_version = 1,
196 + .identity = "BCM2708",
200 + case WDIOC_GETSUPPORT:
201 + return copy_to_user(argp, &ident, sizeof(ident)) ? -EFAULT : 0;
202 + case WDIOC_GETSTATUS:
203 + status = wdog_get_status();
204 + return put_user(status, p);
205 + case WDIOC_GETBOOTSTATUS:
206 + return put_user(0, p);
207 + case WDIOC_KEEPALIVE:
210 + case WDIOC_SETTIMEOUT:
211 + if (get_user(new_heartbeat, p))
213 + if (wdog_set_heartbeat(new_heartbeat))
217 + case WDIOC_GETTIMEOUT:
218 + return put_user(heartbeat, p);
219 + case WDIOC_GETTIMELEFT:
220 + remaining = WDOG_TICKS_TO_SECS(wdog_get_remaining());
221 + return put_user(remaining, p);
222 + case WDIOC_SETOPTIONS:
223 + if (get_user(options, p))
225 + if (options & WDIOS_DISABLECARD)
227 + if (options & WDIOS_ENABLECARD)
228 + wdog_start(wdog_ticks);
236 + * @inode: inode of device
237 + * @file: file handle to device
239 + * The watchdog device has been opened. The watchdog device is single
240 + * open and on opening we load the counters.
243 +static int wdog_open(struct inode *inode, struct file *file)
245 + if (test_and_set_bit(0, &wdog_is_open))
250 + wdog_start(wdog_ticks);
251 + return nonseekable_open(inode, file);
255 + * @inode: inode to board
256 + * @file: file handle to board
258 + * The watchdog has a configurable API. There is a religious dispute
259 + * between people who want their watchdog to be able to shut down and
260 + * those who want to be sure if the watchdog manager dies the machine
261 + * reboots. In the former case we disable the counters, in the latter
262 + * case you have to open it again very soon.
265 +static int wdog_release(struct inode *inode, struct file *file)
267 + if (expect_close == 42) {
271 + "wdt: WDT device closed unexpectedly. WDT will not stop!\n");
274 + clear_bit(0, &wdog_is_open);
280 + * @this: our notifier block
281 + * @code: the event being reported
284 + * Our notifier is called on system shutdowns. Turn the watchdog
285 + * off so that it does not fire during the next reboot.
288 +static int wdog_notify_sys(struct notifier_block *this, unsigned long code,
291 + if (code == SYS_DOWN || code == SYS_HALT)
293 + return NOTIFY_DONE;
297 + * Kernel Interfaces
301 +static const struct file_operations wdog_fops = {
302 + .owner = THIS_MODULE,
303 + .llseek = no_llseek,
304 + .write = wdog_write,
305 + .unlocked_ioctl = wdog_ioctl,
307 + .release = wdog_release,
310 +static struct miscdevice wdog_miscdev = {
311 + .minor = WATCHDOG_MINOR,
312 + .name = "watchdog",
313 + .fops = &wdog_fops,
317 + * The WDT card needs to learn about soft shutdowns in order to
318 + * turn the timebomb registers off.
321 +static struct notifier_block wdog_notifier = {
322 + .notifier_call = wdog_notify_sys,
328 + * Unload the watchdog. You cannot do this with any file handles open.
329 + * If your watchdog is set to continue ticking on close and you unload
330 + * it, well it keeps ticking. We won't get the interrupt but the board
331 + * will not touch PC memory so all is fine. You just have to load a new
332 + * module in 60 seconds or reboot.
335 +static void __exit wdog_exit(void)
337 + misc_deregister(&wdog_miscdev);
338 + unregister_reboot_notifier(&wdog_notifier);
341 +static int __init wdog_init(void)
345 + /* Check that the heartbeat value is within it's range;
346 + if not reset to the default */
347 + if (wdog_set_heartbeat(heartbeat)) {
348 + wdog_set_heartbeat(WD_TIMO);
349 + printk(KERN_INFO "bcm2708_wdog: heartbeat value must be "
350 + "0 < heartbeat < %d, using %d\n",
351 + WDOG_TICKS_TO_SECS(PM_WDOG_TIME_SET),
355 + ret = register_reboot_notifier(&wdog_notifier);
358 + "wdt: cannot register reboot notifier (err=%d)\n", ret);
362 + ret = misc_register(&wdog_miscdev);
365 + "wdt: cannot register miscdev on minor=%d (err=%d)\n",
366 + WATCHDOG_MINOR, ret);
370 + printk(KERN_INFO "bcm2708 watchdog, heartbeat=%d sec (nowayout=%d)\n",
371 + heartbeat, nowayout);
375 + unregister_reboot_notifier(&wdog_notifier);
380 +module_init(wdog_init);
381 +module_exit(wdog_exit);
383 +MODULE_AUTHOR("Luke Diamand");
384 +MODULE_DESCRIPTION("Driver for BCM2708 watchdog");
385 +MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR);
386 +MODULE_ALIAS_MISCDEV(TEMP_MINOR);
387 +MODULE_LICENSE("GPL");
389 --- a/drivers/watchdog/Kconfig
390 +++ b/drivers/watchdog/Kconfig
391 @@ -391,6 +391,12 @@ config RETU_WATCHDOG
392 To compile this driver as a module, choose M here: the
393 module will be called retu_wdt.
396 + tristate "BCM2708 Watchdog"
397 + depends on ARCH_BCM2708
399 + Enables BCM2708 watchdog support.
403 config AT32AP700X_WDT
404 --- a/drivers/watchdog/Makefile
405 +++ b/drivers/watchdog/Makefile
406 @@ -54,6 +54,7 @@ obj-$(CONFIG_TS72XX_WATCHDOG) += ts72xx_
407 obj-$(CONFIG_IMX2_WDT) += imx2_wdt.o
408 obj-$(CONFIG_UX500_WATCHDOG) += ux500_wdt.o
409 obj-$(CONFIG_RETU_WATCHDOG) += retu_wdt.o
410 +obj-$(CONFIG_BCM2708_WDT) += bcm2708_wdog.o
413 obj-$(CONFIG_AT32AP700X_WDT) += at32ap700x_wdt.o