blob: ea1bbf5ee528aa438b70ae2a866f858205a0e8f4 [file] [log] [blame]
Marcus Folkesson2e62c492018-03-16 16:14:11 +01001// SPDX-License-Identifier: GPL-2.0+
Florian Fainelli04bf3b42007-05-06 12:55:32 +02002/*
3 * Driver for the MTX-1 Watchdog.
4 *
Alan Coxed78c2d2008-05-19 14:07:21 +01005 * (C) Copyright 2005 4G Systems <info@4g-systems.biz>,
6 * All Rights Reserved.
Florian Fainelli04bf3b42007-05-06 12:55:32 +02007 * http://www.4g-systems.biz
8 *
Wim Van Sebroeck143a2e52009-03-18 08:35:09 +00009 * (C) Copyright 2007 OpenWrt.org, Florian Fainelli <florian@openwrt.org>
Florian Fainelli04bf3b42007-05-06 12:55:32 +020010 * (c) Copyright 2005 4G Systems <info@4g-systems.biz>
11 *
12 * Release 0.01.
13 * Author: Michael Stickel michael.stickel@4g-systems.biz
14 *
15 * Release 0.02.
16 * Author: Florian Fainelli florian@openwrt.org
17 * use the Linux watchdog/timer APIs
18 *
19 * The Watchdog is configured to reset the MTX-1
20 * if it is not triggered for 100 seconds.
21 * It should not be triggered more often than 1.6 seconds.
22 *
23 * A timer triggers the watchdog every 5 seconds, until
24 * it is opened for the first time. After the first open
25 * it MUST be triggered every 2..95 seconds.
26 */
27
28#include <linux/module.h>
29#include <linux/moduleparam.h>
30#include <linux/types.h>
31#include <linux/errno.h>
32#include <linux/miscdevice.h>
33#include <linux/fs.h>
Florian Fainelli04bf3b42007-05-06 12:55:32 +020034#include <linux/ioport.h>
35#include <linux/timer.h>
36#include <linux/completion.h>
37#include <linux/jiffies.h>
38#include <linux/watchdog.h>
Florian Fainelli6ea81152008-01-07 19:08:49 +010039#include <linux/platform_device.h>
Alan Coxed78c2d2008-05-19 14:07:21 +010040#include <linux/io.h>
41#include <linux/uaccess.h>
Linus Walleijd2b911d2018-12-22 11:12:31 +010042#include <linux/gpio/consumer.h>
Florian Fainelli04bf3b42007-05-06 12:55:32 +020043
Florian Fainelli04bf3b42007-05-06 12:55:32 +020044#define MTX1_WDT_INTERVAL (5 * HZ)
45
46static int ticks = 100 * HZ;
47
48static struct {
49 struct completion stop;
Alan Coxed78c2d2008-05-19 14:07:21 +010050 spinlock_t lock;
Florian Fainelli996d62d2008-02-25 13:39:57 +010051 int running;
Florian Fainelli04bf3b42007-05-06 12:55:32 +020052 struct timer_list timer;
Florian Fainelli996d62d2008-02-25 13:39:57 +010053 int queue;
Florian Fainelli04bf3b42007-05-06 12:55:32 +020054 int default_ticks;
55 unsigned long inuse;
Linus Walleijd2b911d2018-12-22 11:12:31 +010056 struct gpio_desc *gpiod;
Florian Fainelli2ea4e762011-06-15 19:15:41 +020057 unsigned int gstate;
Florian Fainelli04bf3b42007-05-06 12:55:32 +020058} mtx1_wdt_device;
59
Kees Cooke99e88a2017-10-16 14:43:17 -070060static void mtx1_wdt_trigger(struct timer_list *unused)
Florian Fainelli04bf3b42007-05-06 12:55:32 +020061{
Alan Coxed78c2d2008-05-19 14:07:21 +010062 spin_lock(&mtx1_wdt_device.lock);
Florian Fainelli04bf3b42007-05-06 12:55:32 +020063 if (mtx1_wdt_device.running)
64 ticks--;
Manuel Laussb7f720d2011-05-08 10:42:20 +020065
66 /* toggle wdt gpio */
Florian Fainelli2ea4e762011-06-15 19:15:41 +020067 mtx1_wdt_device.gstate = !mtx1_wdt_device.gstate;
Linus Walleijd2b911d2018-12-22 11:12:31 +010068 gpiod_set_value(mtx1_wdt_device.gpiod, mtx1_wdt_device.gstate);
Florian Fainelli04bf3b42007-05-06 12:55:32 +020069
70 if (mtx1_wdt_device.queue && ticks)
71 mod_timer(&mtx1_wdt_device.timer, jiffies + MTX1_WDT_INTERVAL);
Alan Coxed78c2d2008-05-19 14:07:21 +010072 else
Florian Fainelli04bf3b42007-05-06 12:55:32 +020073 complete(&mtx1_wdt_device.stop);
Alan Coxed78c2d2008-05-19 14:07:21 +010074 spin_unlock(&mtx1_wdt_device.lock);
Florian Fainelli04bf3b42007-05-06 12:55:32 +020075}
76
77static void mtx1_wdt_reset(void)
78{
79 ticks = mtx1_wdt_device.default_ticks;
80}
81
82
83static void mtx1_wdt_start(void)
84{
Florian Fainellif80e9192008-10-24 19:52:56 +020085 unsigned long flags;
86
Alan Coxed78c2d2008-05-19 14:07:21 +010087 spin_lock_irqsave(&mtx1_wdt_device.lock, flags);
Florian Fainelli04bf3b42007-05-06 12:55:32 +020088 if (!mtx1_wdt_device.queue) {
89 mtx1_wdt_device.queue = 1;
Manuel Laussb7f720d2011-05-08 10:42:20 +020090 mtx1_wdt_device.gstate = 1;
Linus Walleijd2b911d2018-12-22 11:12:31 +010091 gpiod_set_value(mtx1_wdt_device.gpiod, 1);
Florian Fainelli04bf3b42007-05-06 12:55:32 +020092 mod_timer(&mtx1_wdt_device.timer, jiffies + MTX1_WDT_INTERVAL);
93 }
94 mtx1_wdt_device.running++;
Alan Coxed78c2d2008-05-19 14:07:21 +010095 spin_unlock_irqrestore(&mtx1_wdt_device.lock, flags);
Florian Fainelli04bf3b42007-05-06 12:55:32 +020096}
97
98static int mtx1_wdt_stop(void)
99{
Florian Fainellif80e9192008-10-24 19:52:56 +0200100 unsigned long flags;
101
Alan Coxed78c2d2008-05-19 14:07:21 +0100102 spin_lock_irqsave(&mtx1_wdt_device.lock, flags);
Florian Fainelli04bf3b42007-05-06 12:55:32 +0200103 if (mtx1_wdt_device.queue) {
104 mtx1_wdt_device.queue = 0;
Manuel Laussb7f720d2011-05-08 10:42:20 +0200105 mtx1_wdt_device.gstate = 0;
Linus Walleijd2b911d2018-12-22 11:12:31 +0100106 gpiod_set_value(mtx1_wdt_device.gpiod, 0);
Florian Fainelli04bf3b42007-05-06 12:55:32 +0200107 }
Florian Fainelli04bf3b42007-05-06 12:55:32 +0200108 ticks = mtx1_wdt_device.default_ticks;
Alan Coxed78c2d2008-05-19 14:07:21 +0100109 spin_unlock_irqrestore(&mtx1_wdt_device.lock, flags);
Florian Fainelli04bf3b42007-05-06 12:55:32 +0200110 return 0;
111}
112
113/* Filesystem functions */
114
115static int mtx1_wdt_open(struct inode *inode, struct file *file)
116{
117 if (test_and_set_bit(0, &mtx1_wdt_device.inuse))
118 return -EBUSY;
Kirill Smelkovc5bf68f2019-03-26 23:51:19 +0300119 return stream_open(inode, file);
Florian Fainelli04bf3b42007-05-06 12:55:32 +0200120}
121
122
123static int mtx1_wdt_release(struct inode *inode, struct file *file)
124{
125 clear_bit(0, &mtx1_wdt_device.inuse);
126 return 0;
127}
128
Alan Coxed78c2d2008-05-19 14:07:21 +0100129static long mtx1_wdt_ioctl(struct file *file, unsigned int cmd,
130 unsigned long arg)
Florian Fainelli04bf3b42007-05-06 12:55:32 +0200131{
132 void __user *argp = (void __user *)arg;
Alan Coxed78c2d2008-05-19 14:07:21 +0100133 int __user *p = (int __user *)argp;
Florian Fainelli04bf3b42007-05-06 12:55:32 +0200134 unsigned int value;
Alan Coxed78c2d2008-05-19 14:07:21 +0100135 static const struct watchdog_info ident = {
Florian Fainelli04bf3b42007-05-06 12:55:32 +0200136 .options = WDIOF_CARDRESET,
137 .identity = "MTX-1 WDT",
138 };
139
Alan Coxed78c2d2008-05-19 14:07:21 +0100140 switch (cmd) {
Wim Van Sebroeck0c060902008-07-18 11:41:17 +0000141 case WDIOC_GETSUPPORT:
142 if (copy_to_user(argp, &ident, sizeof(ident)))
143 return -EFAULT;
Alan Coxed78c2d2008-05-19 14:07:21 +0100144 break;
145 case WDIOC_GETSTATUS:
146 case WDIOC_GETBOOTSTATUS:
147 put_user(0, p);
148 break;
Alan Coxed78c2d2008-05-19 14:07:21 +0100149 case WDIOC_SETOPTIONS:
150 if (get_user(value, p))
151 return -EFAULT;
152 if (value & WDIOS_ENABLECARD)
153 mtx1_wdt_start();
154 else if (value & WDIOS_DISABLECARD)
155 mtx1_wdt_stop();
156 else
157 return -EINVAL;
158 return 0;
Wim Van Sebroeck0c060902008-07-18 11:41:17 +0000159 case WDIOC_KEEPALIVE:
160 mtx1_wdt_reset();
161 break;
Alan Coxed78c2d2008-05-19 14:07:21 +0100162 default:
163 return -ENOTTY;
Florian Fainelli04bf3b42007-05-06 12:55:32 +0200164 }
165 return 0;
166}
167
168
Alan Coxed78c2d2008-05-19 14:07:21 +0100169static ssize_t mtx1_wdt_write(struct file *file, const char *buf,
170 size_t count, loff_t *ppos)
Florian Fainelli04bf3b42007-05-06 12:55:32 +0200171{
172 if (!count)
173 return -EIO;
Florian Fainelli04bf3b42007-05-06 12:55:32 +0200174 mtx1_wdt_reset();
175 return count;
176}
177
Jan Engelhardtb47a1662008-01-22 20:48:10 +0100178static const struct file_operations mtx1_wdt_fops = {
Wim Van Sebroeck5f3b2752011-02-23 20:04:38 +0000179 .owner = THIS_MODULE,
Florian Fainelli04bf3b42007-05-06 12:55:32 +0200180 .llseek = no_llseek,
Alan Coxed78c2d2008-05-19 14:07:21 +0100181 .unlocked_ioctl = mtx1_wdt_ioctl,
Arnd Bergmannb6dfb242019-06-03 14:23:09 +0200182 .compat_ioctl = compat_ptr_ioctl,
Wim Van Sebroeck5f3b2752011-02-23 20:04:38 +0000183 .open = mtx1_wdt_open,
184 .write = mtx1_wdt_write,
185 .release = mtx1_wdt_release,
Florian Fainelli04bf3b42007-05-06 12:55:32 +0200186};
187
188
189static struct miscdevice mtx1_wdt_misc = {
Wim Van Sebroeck5f3b2752011-02-23 20:04:38 +0000190 .minor = WATCHDOG_MINOR,
191 .name = "watchdog",
192 .fops = &mtx1_wdt_fops,
Florian Fainelli04bf3b42007-05-06 12:55:32 +0200193};
194
195
Bill Pemberton2d991a12012-11-19 13:21:41 -0500196static int mtx1_wdt_probe(struct platform_device *pdev)
Florian Fainelli04bf3b42007-05-06 12:55:32 +0200197{
198 int ret;
199
Linus Walleijd2b911d2018-12-22 11:12:31 +0100200 mtx1_wdt_device.gpiod = devm_gpiod_get(&pdev->dev,
201 NULL, GPIOD_OUT_HIGH);
202 if (IS_ERR(mtx1_wdt_device.gpiod)) {
Florian Fainelli9b19d402011-06-15 19:15:23 +0200203 dev_err(&pdev->dev, "failed to request gpio");
Linus Walleijd2b911d2018-12-22 11:12:31 +0100204 return PTR_ERR(mtx1_wdt_device.gpiod);
Florian Fainelli9b19d402011-06-15 19:15:23 +0200205 }
Florian Fainelli6ea81152008-01-07 19:08:49 +0100206
Alan Coxed78c2d2008-05-19 14:07:21 +0100207 spin_lock_init(&mtx1_wdt_device.lock);
208 init_completion(&mtx1_wdt_device.stop);
209 mtx1_wdt_device.queue = 0;
210 clear_bit(0, &mtx1_wdt_device.inuse);
Kees Cooke99e88a2017-10-16 14:43:17 -0700211 timer_setup(&mtx1_wdt_device.timer, mtx1_wdt_trigger, 0);
Alan Coxed78c2d2008-05-19 14:07:21 +0100212 mtx1_wdt_device.default_ticks = ticks;
213
214 ret = misc_register(&mtx1_wdt_misc);
215 if (ret < 0) {
Florian Fainellifad0a9d2011-06-15 19:15:09 +0200216 dev_err(&pdev->dev, "failed to register\n");
Florian Fainelli04bf3b42007-05-06 12:55:32 +0200217 return ret;
218 }
Florian Fainelli04bf3b42007-05-06 12:55:32 +0200219 mtx1_wdt_start();
Florian Fainellifad0a9d2011-06-15 19:15:09 +0200220 dev_info(&pdev->dev, "MTX-1 Watchdog driver\n");
Florian Fainelli04bf3b42007-05-06 12:55:32 +0200221 return 0;
222}
223
Bill Pemberton4b12b892012-11-19 13:26:24 -0500224static int mtx1_wdt_remove(struct platform_device *pdev)
Florian Fainelli04bf3b42007-05-06 12:55:32 +0200225{
Alan Coxed78c2d2008-05-19 14:07:21 +0100226 /* FIXME: do we need to lock this test ? */
Florian Fainelli04bf3b42007-05-06 12:55:32 +0200227 if (mtx1_wdt_device.queue) {
228 mtx1_wdt_device.queue = 0;
229 wait_for_completion(&mtx1_wdt_device.stop);
230 }
Florian Fainelli9b19d402011-06-15 19:15:23 +0200231
Florian Fainelli04bf3b42007-05-06 12:55:32 +0200232 misc_deregister(&mtx1_wdt_misc);
Florian Fainelli6ea81152008-01-07 19:08:49 +0100233 return 0;
234}
235
Florian Fainellidb98f89a2011-06-15 19:15:52 +0200236static struct platform_driver mtx1_wdt_driver = {
Florian Fainelli6ea81152008-01-07 19:08:49 +0100237 .probe = mtx1_wdt_probe,
Bill Pemberton82268712012-11-19 13:21:12 -0500238 .remove = mtx1_wdt_remove,
Florian Fainelli6ea81152008-01-07 19:08:49 +0100239 .driver.name = "mtx1-wdt",
Kay Sieversf37d1932008-04-10 21:29:23 -0700240 .driver.owner = THIS_MODULE,
Florian Fainelli6ea81152008-01-07 19:08:49 +0100241};
242
Axel Linb8ec6112011-11-29 13:56:27 +0800243module_platform_driver(mtx1_wdt_driver);
Florian Fainelli04bf3b42007-05-06 12:55:32 +0200244
245MODULE_AUTHOR("Michael Stickel, Florian Fainelli");
246MODULE_DESCRIPTION("Driver for the MTX-1 watchdog");
247MODULE_LICENSE("GPL");
Kay Sieversf37d1932008-04-10 21:29:23 -0700248MODULE_ALIAS("platform:mtx1-wdt");