1 This generic GPIO watchdog is used on Huawei E970 (brcm47xx)
3 Signed-off-by: Mathias Adam <m.adam--openwrt@adamis.de>
5 --- a/drivers/watchdog/Kconfig
6 +++ b/drivers/watchdog/Kconfig
7 @@ -1035,6 +1035,15 @@ config WDT_MTX1
8 Hardware driver for the MTX-1 boards. This is a watchdog timer that
9 will reboot the machine after a 100 seconds timer expired.
12 + tristate "GPIO Hardware Watchdog"
14 + Hardware driver for GPIO-controlled watchdogs. GPIO pin and
15 + toggle interval settings are platform-specific. The driver
16 + will stop toggling the GPIO (i.e. machine reboots) after a
17 + 100 second timer expired and no process has written to
18 + /dev/watchdog during that time.
21 tristate "PNX833x Hardware Watchdog"
22 depends on SOC_PNX8335
23 --- a/drivers/watchdog/Makefile
24 +++ b/drivers/watchdog/Makefile
25 @@ -127,6 +127,7 @@ obj-$(CONFIG_RC32434_WDT) += rc32434_wdt
26 obj-$(CONFIG_INDYDOG) += indydog.o
27 obj-$(CONFIG_JZ4740_WDT) += jz4740_wdt.o
28 obj-$(CONFIG_WDT_MTX1) += mtx-1_wdt.o
29 +obj-$(CONFIG_GPIO_WDT) += gpio_wdt.o
30 obj-$(CONFIG_PNX833X_WDT) += pnx833x_wdt.o
31 obj-$(CONFIG_SIBYTE_WDOG) += sb_wdog.o
32 obj-$(CONFIG_AR7_WDT) += ar7_wdt.o
34 +++ b/drivers/watchdog/gpio_wdt.c
37 + * Driver for GPIO-controlled Hardware Watchdogs.
39 + * Copyright (C) 2013 Mathias Adam <m.adam--linux@adamis.de>
41 + * Replaces mtx1_wdt (driver for the MTX-1 Watchdog):
43 + * (C) Copyright 2005 4G Systems <info@4g-systems.biz>,
44 + * All Rights Reserved.
45 + * http://www.4g-systems.biz
47 + * (C) Copyright 2007 OpenWrt.org, Florian Fainelli <florian@openwrt.org>
49 + * This program is free software; you can redistribute it and/or
50 + * modify it under the terms of the GNU General Public License
51 + * as published by the Free Software Foundation; either version
52 + * 2 of the License, or (at your option) any later version.
54 + * Neither Michael Stickel nor 4G Systems admit liability nor provide
55 + * warranty for any of this software. This material is provided
56 + * "AS-IS" and at no charge.
58 + * (c) Copyright 2005 4G Systems <info@4g-systems.biz>
61 + * Author: Michael Stickel michael.stickel@4g-systems.biz
64 + * Author: Florian Fainelli florian@openwrt.org
65 + * use the Linux watchdog/timer APIs
68 + * Author: Mathias Adam <m.adam--linux@adamis.de>
69 + * make it a generic gpio watchdog driver
71 + * The Watchdog is configured to reset the MTX-1
72 + * if it is not triggered for 100 seconds.
73 + * It should not be triggered more often than 1.6 seconds.
75 + * A timer triggers the watchdog every 5 seconds, until
76 + * it is opened for the first time. After the first open
77 + * it MUST be triggered every 2..95 seconds.
80 +#include <linux/module.h>
81 +#include <linux/moduleparam.h>
82 +#include <linux/types.h>
83 +#include <linux/errno.h>
84 +#include <linux/miscdevice.h>
85 +#include <linux/fs.h>
86 +#include <linux/init.h>
87 +#include <linux/ioport.h>
88 +#include <linux/timer.h>
89 +#include <linux/completion.h>
90 +#include <linux/jiffies.h>
91 +#include <linux/watchdog.h>
92 +#include <linux/platform_device.h>
93 +#include <linux/io.h>
94 +#include <linux/uaccess.h>
95 +#include <linux/gpio.h>
96 +#include <linux/gpio_wdt.h>
98 +static int ticks = 100 * HZ;
101 + struct completion stop;
104 + struct timer_list timer;
107 + unsigned long inuse;
109 + unsigned int gstate;
111 + int first_interval;
114 +static void gpio_wdt_trigger(unsigned long unused)
116 + spin_lock(&gpio_wdt_device.lock);
117 + if (gpio_wdt_device.running && ticks > 0)
118 + ticks -= gpio_wdt_device.interval;
120 + /* toggle wdt gpio */
121 + gpio_wdt_device.gstate = !gpio_wdt_device.gstate;
122 + gpio_set_value(gpio_wdt_device.gpio, gpio_wdt_device.gstate);
124 + if (gpio_wdt_device.queue && ticks > 0)
125 + mod_timer(&gpio_wdt_device.timer, jiffies + gpio_wdt_device.interval);
127 + complete(&gpio_wdt_device.stop);
128 + spin_unlock(&gpio_wdt_device.lock);
131 +static void gpio_wdt_reset(void)
133 + ticks = gpio_wdt_device.default_ticks;
137 +static void gpio_wdt_start(void)
139 + unsigned long flags;
141 + spin_lock_irqsave(&gpio_wdt_device.lock, flags);
142 + if (!gpio_wdt_device.queue) {
143 + gpio_wdt_device.queue = 1;
144 + gpio_wdt_device.gstate = 1;
145 + gpio_set_value(gpio_wdt_device.gpio, 1);
146 + mod_timer(&gpio_wdt_device.timer, jiffies + gpio_wdt_device.first_interval);
148 + gpio_wdt_device.running++;
149 + spin_unlock_irqrestore(&gpio_wdt_device.lock, flags);
152 +static int gpio_wdt_stop(void)
154 + unsigned long flags;
156 + spin_lock_irqsave(&gpio_wdt_device.lock, flags);
157 + if (gpio_wdt_device.queue) {
158 + gpio_wdt_device.queue = 0;
159 + gpio_wdt_device.gstate = 0;
160 + gpio_set_value(gpio_wdt_device.gpio, 0);
162 + ticks = gpio_wdt_device.default_ticks;
163 + spin_unlock_irqrestore(&gpio_wdt_device.lock, flags);
167 +/* Filesystem functions */
169 +static int gpio_wdt_open(struct inode *inode, struct file *file)
171 + if (test_and_set_bit(0, &gpio_wdt_device.inuse))
173 + return nonseekable_open(inode, file);
177 +static int gpio_wdt_release(struct inode *inode, struct file *file)
179 + clear_bit(0, &gpio_wdt_device.inuse);
183 +static long gpio_wdt_ioctl(struct file *file, unsigned int cmd,
186 + void __user *argp = (void __user *)arg;
187 + int __user *p = (int __user *)argp;
188 + unsigned int value;
189 + static const struct watchdog_info ident = {
190 + .options = WDIOF_CARDRESET,
191 + .identity = "GPIO WDT",
195 + case WDIOC_GETSUPPORT:
196 + if (copy_to_user(argp, &ident, sizeof(ident)))
199 + case WDIOC_GETSTATUS:
200 + case WDIOC_GETBOOTSTATUS:
203 + case WDIOC_SETOPTIONS:
204 + if (get_user(value, p))
206 + if (value & WDIOS_ENABLECARD)
208 + else if (value & WDIOS_DISABLECARD)
213 + case WDIOC_KEEPALIVE:
223 +static ssize_t gpio_wdt_write(struct file *file, const char *buf,
224 + size_t count, loff_t *ppos)
232 +static const struct file_operations gpio_wdt_fops = {
233 + .owner = THIS_MODULE,
234 + .llseek = no_llseek,
235 + .unlocked_ioctl = gpio_wdt_ioctl,
236 + .open = gpio_wdt_open,
237 + .write = gpio_wdt_write,
238 + .release = gpio_wdt_release,
242 +static struct miscdevice gpio_wdt_misc = {
243 + .minor = WATCHDOG_MINOR,
244 + .name = "watchdog",
245 + .fops = &gpio_wdt_fops,
249 +static int gpio_wdt_probe(struct platform_device *pdev)
252 + struct gpio_wdt_platform_data *gpio_wdt_data = pdev->dev.platform_data;
254 + gpio_wdt_device.gpio = gpio_wdt_data->gpio;
255 + gpio_wdt_device.interval = gpio_wdt_data->interval;
256 + gpio_wdt_device.first_interval = gpio_wdt_data->first_interval;
257 + if (gpio_wdt_device.first_interval <= 0) {
258 + gpio_wdt_device.first_interval = gpio_wdt_device.interval;
261 + ret = gpio_request(gpio_wdt_device.gpio, "gpio-wdt");
263 + dev_err(&pdev->dev, "failed to request gpio");
267 + spin_lock_init(&gpio_wdt_device.lock);
268 + init_completion(&gpio_wdt_device.stop);
269 + gpio_wdt_device.queue = 0;
270 + clear_bit(0, &gpio_wdt_device.inuse);
271 + setup_timer(&gpio_wdt_device.timer, gpio_wdt_trigger, 0L);
272 + gpio_wdt_device.default_ticks = ticks;
275 + dev_info(&pdev->dev, "GPIO Hardware Watchdog driver (gpio=%i interval=%i/%i)\n",
276 + gpio_wdt_data->gpio, gpio_wdt_data->first_interval, gpio_wdt_data->interval);
280 +static int gpio_wdt_remove(struct platform_device *pdev)
282 + /* FIXME: do we need to lock this test ? */
283 + if (gpio_wdt_device.queue) {
284 + gpio_wdt_device.queue = 0;
285 + wait_for_completion(&gpio_wdt_device.stop);
288 + gpio_free(gpio_wdt_device.gpio);
289 + misc_deregister(&gpio_wdt_misc);
293 +static struct platform_driver gpio_wdt_driver = {
294 + .probe = gpio_wdt_probe,
295 + .remove = gpio_wdt_remove,
296 + .driver.name = "gpio-wdt",
297 + .driver.owner = THIS_MODULE,
300 +static int __init gpio_wdt_init(void)
302 + return platform_driver_register(&gpio_wdt_driver);
304 +arch_initcall(gpio_wdt_init);
307 + * We do wdt initialization in two steps: arch_initcall probes the wdt
308 + * very early to start pinging the watchdog (misc devices are not yet
309 + * available), and later module_init() just registers the misc device.
311 +static int gpio_wdt_init_late(void)
315 + ret = misc_register(&gpio_wdt_misc);
317 + pr_err("GPIO_WDT: failed to register misc device\n");
323 +module_init(gpio_wdt_init_late);
326 +static void __exit gpio_wdt_exit(void)
328 + platform_driver_unregister(&gpio_wdt_driver);
330 +module_exit(gpio_wdt_exit);
332 +MODULE_AUTHOR("Michael Stickel, Florian Fainelli, Mathias Adam");
333 +MODULE_DESCRIPTION("Driver for GPIO hardware watchdogs");
334 +MODULE_LICENSE("GPL");
335 +MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR);
336 +MODULE_ALIAS("platform:gpio-wdt");
338 +++ b/include/linux/gpio_wdt.h
341 + * Definitions for the GPIO watchdog driver
343 + * Copyright (C) 2013 Mathias Adam <m.adam--linux@adamis.de>
345 + * This program is free software; you can redistribute it and/or modify
346 + * it under the terms of the GNU General Public License version 2 as
347 + * published by the Free Software Foundation.
351 +#ifndef _GPIO_WDT_H_
352 +#define _GPIO_WDT_H_
354 +struct gpio_wdt_platform_data {
355 + int gpio; /* GPIO line number */
356 + int interval; /* watchdog reset interval in system ticks */
357 + int first_interval; /* first wd reset interval in system ticks */
360 +#endif /* _GPIO_WDT_H_ */