2 * Broadcom BCM63xx SoC watchdog driver
4 * Copyright (C) 2007, Miguel Gaio <miguel.gaio@efixo.com>
5 * Copyright (C) 2008, Florian Fainelli <florian@openwrt.org>
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version
10 * 2 of the License, or (at your option) any later version.
13 #include <linux/module.h>
14 #include <linux/types.h>
15 #include <linux/kernel.h>
18 #include <linux/miscdevice.h>
19 #include <linux/watchdog.h>
20 #include <linux/reboot.h>
21 #include <linux/smp_lock.h>
22 #include <linux/init.h>
23 #include <linux/platform_device.h>
24 #include <linux/uaccess.h>
25 #include <linux/timer.h>
27 #include <bcm63xx_cpu.h>
28 #include <bcm63xx_io.h>
29 #include <bcm63xx_regs.h>
31 #define PFX KBUILD_MODNAME
33 #define WDT_HZ 50000000 /* Fclk */
34 #define WDT_INTERVAL (40) /* in seconds */
38 struct completion stop;
40 struct timer_list timer;
46 static int ticks = 100 * WDT_HZ;
48 static int expect_close;
51 static int nowayout = WATCHDOG_NOWAYOUT;
52 module_param(nowayout, int, 0);
53 MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started (default="
54 __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
57 static void bcm63xx_wdt_toggle(void)
59 bcm_writel(WDT_START_1, bcm63xx_wdt_device.regs + WDT_CTL_REG);
60 bcm_writel(WDT_START_2, bcm63xx_wdt_device.regs + WDT_CTL_REG);
63 static void bcm63xx_wdt_start(void)
65 if (!bcm63xx_wdt_device.inuse) {
67 mod_timer(&bcm63xx_wdt_device.timer, jiffies + WDT_INTERVAL);
70 bcm63xx_wdt_device.running++;
73 static void bcm63xx_wdt_stop(void)
75 if (bcm63xx_wdt_device.running) {
76 bcm_writel(WDT_STOP_1, bcm63xx_wdt_device.regs + WDT_CTL_REG);
77 bcm_writel(WDT_STOP_2, bcm63xx_wdt_device.regs + WDT_CTL_REG);
79 bcm63xx_wdt_device.running = 0;
83 static void bcm63xx_wdt_set(int new_timeout)
85 new_timeout *= WDT_HZ;
86 bcm_writel(new_timeout, bcm63xx_wdt_device.regs + WDT_DEFVAL_REG);
89 static void bcm63xx_wdt_reset(void)
91 ticks = bcm63xx_wdt_device.default_ticks;
94 static void bcm63xx_wdt_update(unsigned long unused)
96 if (bcm63xx_wdt_device.running)
101 if (bcm63xx_wdt_device.queue && ticks)
102 mod_timer(&bcm63xx_wdt_device.timer,
103 jiffies + WDT_INTERVAL);
105 complete(&bcm63xx_wdt_device.stop);
108 static int bcm63xx_wdt_open(struct inode *inode, struct file *file)
110 if (test_and_set_bit(0, &bcm63xx_wdt_device.inuse))
114 __module_get(THIS_MODULE);
116 return nonseekable_open(inode, file);
119 static int bcm63xx_wdt_release(struct inode *inode, struct file *file)
121 if (expect_close && nowayout == 0) {
123 printk(KERN_INFO PFX ": disabling watchdog timer\n");
124 module_put(THIS_MODULE);
127 ": device closed unexpectedly. WDT will not stop !\n");
129 clear_bit(0, &bcm63xx_wdt_device.inuse);
133 static ssize_t bcm63xx_wdt_write(struct file *file, const char *data,
134 size_t len, loff_t *ppos)
140 /* In case it was set long ago */
143 for (i = 0; i != len; i++) {
145 if (get_user(c, data + i))
151 bcm63xx_wdt_update(0);
157 static long bcm63xx_wdt_ioctl(struct file *file, unsigned int cmd,
160 void __user *argp = (void __user *)arg;
163 static struct watchdog_info ident = {
164 .options = WDIOF_SETTIMEOUT |
165 WDIOF_KEEPALIVEPING |
167 .identity = "BCM63xx Watchdog",
170 case WDIOC_KEEPALIVE:
173 case WDIOC_GETSTATUS:
174 case WDIOC_GETBOOTSTATUS:
175 value = bcm_readl(bcm63xx_wdt_device.regs + WDT_DEFVAL_REG);
176 if (copy_to_user(argp, &value, sizeof(int)))
179 case WDIOC_GETSUPPORT:
180 if (copy_to_user(argp, &ident, sizeof(ident)))
183 case WDIOC_SETOPTIONS:
184 if (copy_from_user(&value, argp, sizeof(int)))
187 case WDIOS_ENABLECARD:
190 case WDIOS_DISABLECARD:
196 case WDIOC_SETTIMEOUT:
197 if (copy_from_user(&new_timeout, argp, sizeof(int)))
201 if (new_timeout > 40)
203 bcm63xx_wdt_set(new_timeout);
204 bcm63xx_wdt_toggle();
205 case WDIOC_GETTIMEOUT:
206 return copy_to_user(argp, &timeout, sizeof(int));
214 static struct file_operations bcm63xx_wdt_fops = {
215 .owner = THIS_MODULE,
217 .write = bcm63xx_wdt_write,
218 .unlocked_ioctl = bcm63xx_wdt_ioctl,
219 .open = bcm63xx_wdt_open,
220 .release = bcm63xx_wdt_release,
223 static struct miscdevice bcm63xx_wdt_miscdev = {
224 .minor = WATCHDOG_MINOR,
226 .fops = &bcm63xx_wdt_fops,
229 static int bcm63xx_wdt_probe(struct platform_device *pdev)
234 r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
237 "failed to retrieve resources\n");
241 bcm63xx_wdt_device.regs = ioremap_nocache(r->start, r->end - r->start);
242 if (!bcm63xx_wdt_device.regs) {
244 "failed to remap I/O resources\n");
248 ret = misc_register(&bcm63xx_wdt_miscdev);
251 "failed to register watchdog device\n");
255 init_completion(&bcm63xx_wdt_device.stop);
256 bcm63xx_wdt_device.queue = 0;
258 clear_bit(0, &bcm63xx_wdt_device.inuse);
260 setup_timer(&bcm63xx_wdt_device.timer, bcm63xx_wdt_update, 0L);
262 bcm63xx_wdt_device.default_ticks = ticks;
263 bcm63xx_wdt_set(ticks);
266 printk(KERN_INFO PFX " started, timer margin: %d sec\n", WDT_INTERVAL);
271 iounmap(bcm63xx_wdt_device.regs);
275 static int bcm63xx_wdt_remove(struct platform_device *pdev)
277 if (bcm63xx_wdt_device.queue) {
278 bcm63xx_wdt_device.queue = 0;
279 wait_for_completion(&bcm63xx_wdt_device.stop);
282 misc_deregister(&bcm63xx_wdt_miscdev);
284 iounmap(bcm63xx_wdt_device.regs);
289 static struct platform_driver bcm63xx_wdt = {
290 .probe = bcm63xx_wdt_probe,
291 .remove = bcm63xx_wdt_remove,
293 .name = "bcm63xx-wdt",
297 static int __init bcm63xx_wdt_init(void)
299 return platform_driver_register(&bcm63xx_wdt);
302 static void __exit bcm63xx_wdt_exit(void)
304 platform_driver_unregister(&bcm63xx_wdt);
307 module_init(bcm63xx_wdt_init);
308 module_exit(bcm63xx_wdt_exit);
310 MODULE_AUTHOR("Miguel Gaio <miguel.gaio@efixo.com>");
311 MODULE_AUTHOR("Florian Fainelli <florian@openwrt.org>");
312 MODULE_DESCRIPTION("Driver for the Broadcom BCM63xx SoC watchdog");
313 MODULE_LICENSE("GPL");
314 MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR);
315 MODULE_ALIAS("platform:bcm63xx-wdt");