00329e1774504f883e4d1519bc52a04671015f48
[openwrt.git] / target / linux / generic / patches-3.14 / 990-gpio_wdt.patch
1 This generic GPIO watchdog is used on Huawei E970 (brcm47xx)
2
3 Signed-off-by: Mathias Adam <m.adam--openwrt@adamis.de>
4
5 --- a/drivers/watchdog/Kconfig
6 +++ b/drivers/watchdog/Kconfig
7 @@ -1084,6 +1084,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.
10  
11 +config GPIO_WDT
12 +       tristate "GPIO Hardware Watchdog"
13 +       help
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.
19 +
20  config PNX833X_WDT
21         tristate "PNX833x Hardware Watchdog"
22         depends on SOC_PNX8335
23 --- a/drivers/watchdog/Makefile
24 +++ b/drivers/watchdog/Makefile
25 @@ -130,6 +130,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) += old_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
33 --- /dev/null
34 +++ b/drivers/watchdog/old_gpio_wdt.c
35 @@ -0,0 +1,301 @@
36 +/*
37 + *      Driver for GPIO-controlled Hardware Watchdogs.
38 + *
39 + *      Copyright (C) 2013 Mathias Adam <m.adam--linux@adamis.de>
40 + *
41 + *      Replaces mtx1_wdt (driver for the MTX-1 Watchdog):
42 + *
43 + *      (C) Copyright 2005 4G Systems <info@4g-systems.biz>,
44 + *                              All Rights Reserved.
45 + *                              http://www.4g-systems.biz
46 + *
47 + *      (C) Copyright 2007 OpenWrt.org, Florian Fainelli <florian@openwrt.org>
48 + *
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.
53 + *
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.
57 + *
58 + *      (c) Copyright 2005    4G Systems <info@4g-systems.biz>
59 + *
60 + *      Release 0.01.
61 + *      Author: Michael Stickel  michael.stickel@4g-systems.biz
62 + *
63 + *      Release 0.02.
64 + *      Author: Florian Fainelli florian@openwrt.org
65 + *              use the Linux watchdog/timer APIs
66 + *
67 + *      Release 0.03.
68 + *      Author: Mathias Adam <m.adam--linux@adamis.de>
69 + *              make it a generic gpio watchdog driver
70 + *
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.
74 + *
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.
78 + */
79 +
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>
97 +
98 +static int ticks = 100 * HZ;
99 +
100 +static struct {
101 +       struct completion stop;
102 +       spinlock_t lock;
103 +       int running;
104 +       struct timer_list timer;
105 +       int queue;
106 +       int default_ticks;
107 +       unsigned long inuse;
108 +       unsigned gpio;
109 +       unsigned int gstate;
110 +       int interval;
111 +       int first_interval;
112 +} gpio_wdt_device;
113 +
114 +static void gpio_wdt_trigger(unsigned long unused)
115 +{
116 +       spin_lock(&gpio_wdt_device.lock);
117 +       if (gpio_wdt_device.running && ticks > 0)
118 +               ticks -= gpio_wdt_device.interval;
119 +
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);
123 +
124 +       if (gpio_wdt_device.queue && ticks > 0)
125 +               mod_timer(&gpio_wdt_device.timer, jiffies + gpio_wdt_device.interval);
126 +       else
127 +               complete(&gpio_wdt_device.stop);
128 +       spin_unlock(&gpio_wdt_device.lock);
129 +}
130 +
131 +static void gpio_wdt_reset(void)
132 +{
133 +       ticks = gpio_wdt_device.default_ticks;
134 +}
135 +
136 +
137 +static void gpio_wdt_start(void)
138 +{
139 +       unsigned long flags;
140 +
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);
147 +       }
148 +       gpio_wdt_device.running++;
149 +       spin_unlock_irqrestore(&gpio_wdt_device.lock, flags);
150 +}
151 +
152 +static int gpio_wdt_stop(void)
153 +{
154 +       unsigned long flags;
155 +
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);
161 +       }
162 +       ticks = gpio_wdt_device.default_ticks;
163 +       spin_unlock_irqrestore(&gpio_wdt_device.lock, flags);
164 +       return 0;
165 +}
166 +
167 +/* Filesystem functions */
168 +
169 +static int gpio_wdt_open(struct inode *inode, struct file *file)
170 +{
171 +       if (test_and_set_bit(0, &gpio_wdt_device.inuse))
172 +               return -EBUSY;
173 +       return nonseekable_open(inode, file);
174 +}
175 +
176 +
177 +static int gpio_wdt_release(struct inode *inode, struct file *file)
178 +{
179 +       clear_bit(0, &gpio_wdt_device.inuse);
180 +       return 0;
181 +}
182 +
183 +static long gpio_wdt_ioctl(struct file *file, unsigned int cmd,
184 +                                                       unsigned long arg)
185 +{
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",
192 +       };
193 +
194 +       switch (cmd) {
195 +       case WDIOC_GETSUPPORT:
196 +               if (copy_to_user(argp, &ident, sizeof(ident)))
197 +                       return -EFAULT;
198 +               break;
199 +       case WDIOC_GETSTATUS:
200 +       case WDIOC_GETBOOTSTATUS:
201 +               put_user(0, p);
202 +               break;
203 +       case WDIOC_SETOPTIONS:
204 +               if (get_user(value, p))
205 +                       return -EFAULT;
206 +               if (value & WDIOS_ENABLECARD)
207 +                       gpio_wdt_start();
208 +               else if (value & WDIOS_DISABLECARD)
209 +                       gpio_wdt_stop();
210 +               else
211 +                       return -EINVAL;
212 +               return 0;
213 +       case WDIOC_KEEPALIVE:
214 +               gpio_wdt_reset();
215 +               break;
216 +       default:
217 +               return -ENOTTY;
218 +       }
219 +       return 0;
220 +}
221 +
222 +
223 +static ssize_t gpio_wdt_write(struct file *file, const char *buf,
224 +                                               size_t count, loff_t *ppos)
225 +{
226 +       if (!count)
227 +               return -EIO;
228 +       gpio_wdt_reset();
229 +       return count;
230 +}
231 +
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,
239 +};
240 +
241 +
242 +static struct miscdevice gpio_wdt_misc = {
243 +       .minor  = WATCHDOG_MINOR,
244 +       .name   = "watchdog",
245 +       .fops   = &gpio_wdt_fops,
246 +};
247 +
248 +
249 +static int gpio_wdt_probe(struct platform_device *pdev)
250 +{
251 +       int ret;
252 +       struct gpio_wdt_platform_data *gpio_wdt_data = pdev->dev.platform_data;
253 +
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;
259 +       }
260 +
261 +       ret = gpio_request(gpio_wdt_device.gpio, "gpio-wdt");
262 +       if (ret < 0) {
263 +               dev_err(&pdev->dev, "failed to request gpio");
264 +               return ret;
265 +       }
266 +
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;
273 +
274 +       gpio_wdt_start();
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);
277 +       return 0;
278 +}
279 +
280 +static int gpio_wdt_remove(struct platform_device *pdev)
281 +{
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);
286 +       }
287 +
288 +       gpio_free(gpio_wdt_device.gpio);
289 +       misc_deregister(&gpio_wdt_misc);
290 +       return 0;
291 +}
292 +
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,
298 +};
299 +
300 +static int __init gpio_wdt_init(void)
301 +{
302 +       return platform_driver_register(&gpio_wdt_driver);
303 +}
304 +arch_initcall(gpio_wdt_init);
305 +
306 +/*
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.
310 + */
311 +static int gpio_wdt_init_late(void)
312 +{
313 +       int ret;
314 +
315 +       ret = misc_register(&gpio_wdt_misc);
316 +       if (ret < 0) {
317 +               pr_err("GPIO_WDT: failed to register misc device\n");
318 +               return ret;
319 +       }
320 +       return 0;
321 +}
322 +#ifndef MODULE
323 +module_init(gpio_wdt_init_late);
324 +#endif
325 +
326 +static void __exit gpio_wdt_exit(void)
327 +{
328 +       platform_driver_unregister(&gpio_wdt_driver);
329 +}
330 +module_exit(gpio_wdt_exit);
331 +
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");
337 --- /dev/null
338 +++ b/include/linux/old_gpio_wdt.h
339 @@ -0,0 +1,21 @@
340 +/*
341 + *  Definitions for the GPIO watchdog driver
342 + *
343 + *  Copyright (C) 2013 Mathias Adam <m.adam--linux@adamis.de>
344 + *
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.
348 + *
349 + */
350 +
351 +#ifndef _GPIO_WDT_H_
352 +#define _GPIO_WDT_H_
353 +
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 */
358 +};
359 +
360 +#endif /* _GPIO_WDT_H_ */