1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0+
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun * Driver for the MTX-1 Watchdog.
4*4882a593Smuzhiyun *
5*4882a593Smuzhiyun * (C) Copyright 2005 4G Systems <info@4g-systems.biz>,
6*4882a593Smuzhiyun * All Rights Reserved.
7*4882a593Smuzhiyun * http://www.4g-systems.biz
8*4882a593Smuzhiyun *
9*4882a593Smuzhiyun * (C) Copyright 2007 OpenWrt.org, Florian Fainelli <florian@openwrt.org>
10*4882a593Smuzhiyun * (c) Copyright 2005 4G Systems <info@4g-systems.biz>
11*4882a593Smuzhiyun *
12*4882a593Smuzhiyun * Release 0.01.
13*4882a593Smuzhiyun * Author: Michael Stickel michael.stickel@4g-systems.biz
14*4882a593Smuzhiyun *
15*4882a593Smuzhiyun * Release 0.02.
16*4882a593Smuzhiyun * Author: Florian Fainelli florian@openwrt.org
17*4882a593Smuzhiyun * use the Linux watchdog/timer APIs
18*4882a593Smuzhiyun *
19*4882a593Smuzhiyun * The Watchdog is configured to reset the MTX-1
20*4882a593Smuzhiyun * if it is not triggered for 100 seconds.
21*4882a593Smuzhiyun * It should not be triggered more often than 1.6 seconds.
22*4882a593Smuzhiyun *
23*4882a593Smuzhiyun * A timer triggers the watchdog every 5 seconds, until
24*4882a593Smuzhiyun * it is opened for the first time. After the first open
25*4882a593Smuzhiyun * it MUST be triggered every 2..95 seconds.
26*4882a593Smuzhiyun */
27*4882a593Smuzhiyun
28*4882a593Smuzhiyun #include <linux/module.h>
29*4882a593Smuzhiyun #include <linux/moduleparam.h>
30*4882a593Smuzhiyun #include <linux/types.h>
31*4882a593Smuzhiyun #include <linux/errno.h>
32*4882a593Smuzhiyun #include <linux/miscdevice.h>
33*4882a593Smuzhiyun #include <linux/fs.h>
34*4882a593Smuzhiyun #include <linux/ioport.h>
35*4882a593Smuzhiyun #include <linux/timer.h>
36*4882a593Smuzhiyun #include <linux/completion.h>
37*4882a593Smuzhiyun #include <linux/jiffies.h>
38*4882a593Smuzhiyun #include <linux/watchdog.h>
39*4882a593Smuzhiyun #include <linux/platform_device.h>
40*4882a593Smuzhiyun #include <linux/io.h>
41*4882a593Smuzhiyun #include <linux/uaccess.h>
42*4882a593Smuzhiyun #include <linux/gpio/consumer.h>
43*4882a593Smuzhiyun
44*4882a593Smuzhiyun #include <asm/mach-au1x00/au1000.h>
45*4882a593Smuzhiyun
46*4882a593Smuzhiyun #define MTX1_WDT_INTERVAL (5 * HZ)
47*4882a593Smuzhiyun
48*4882a593Smuzhiyun static int ticks = 100 * HZ;
49*4882a593Smuzhiyun
50*4882a593Smuzhiyun static struct {
51*4882a593Smuzhiyun struct completion stop;
52*4882a593Smuzhiyun spinlock_t lock;
53*4882a593Smuzhiyun int running;
54*4882a593Smuzhiyun struct timer_list timer;
55*4882a593Smuzhiyun int queue;
56*4882a593Smuzhiyun int default_ticks;
57*4882a593Smuzhiyun unsigned long inuse;
58*4882a593Smuzhiyun struct gpio_desc *gpiod;
59*4882a593Smuzhiyun unsigned int gstate;
60*4882a593Smuzhiyun } mtx1_wdt_device;
61*4882a593Smuzhiyun
mtx1_wdt_trigger(struct timer_list * unused)62*4882a593Smuzhiyun static void mtx1_wdt_trigger(struct timer_list *unused)
63*4882a593Smuzhiyun {
64*4882a593Smuzhiyun spin_lock(&mtx1_wdt_device.lock);
65*4882a593Smuzhiyun if (mtx1_wdt_device.running)
66*4882a593Smuzhiyun ticks--;
67*4882a593Smuzhiyun
68*4882a593Smuzhiyun /* toggle wdt gpio */
69*4882a593Smuzhiyun mtx1_wdt_device.gstate = !mtx1_wdt_device.gstate;
70*4882a593Smuzhiyun gpiod_set_value(mtx1_wdt_device.gpiod, mtx1_wdt_device.gstate);
71*4882a593Smuzhiyun
72*4882a593Smuzhiyun if (mtx1_wdt_device.queue && ticks)
73*4882a593Smuzhiyun mod_timer(&mtx1_wdt_device.timer, jiffies + MTX1_WDT_INTERVAL);
74*4882a593Smuzhiyun else
75*4882a593Smuzhiyun complete(&mtx1_wdt_device.stop);
76*4882a593Smuzhiyun spin_unlock(&mtx1_wdt_device.lock);
77*4882a593Smuzhiyun }
78*4882a593Smuzhiyun
mtx1_wdt_reset(void)79*4882a593Smuzhiyun static void mtx1_wdt_reset(void)
80*4882a593Smuzhiyun {
81*4882a593Smuzhiyun ticks = mtx1_wdt_device.default_ticks;
82*4882a593Smuzhiyun }
83*4882a593Smuzhiyun
84*4882a593Smuzhiyun
mtx1_wdt_start(void)85*4882a593Smuzhiyun static void mtx1_wdt_start(void)
86*4882a593Smuzhiyun {
87*4882a593Smuzhiyun unsigned long flags;
88*4882a593Smuzhiyun
89*4882a593Smuzhiyun spin_lock_irqsave(&mtx1_wdt_device.lock, flags);
90*4882a593Smuzhiyun if (!mtx1_wdt_device.queue) {
91*4882a593Smuzhiyun mtx1_wdt_device.queue = 1;
92*4882a593Smuzhiyun mtx1_wdt_device.gstate = 1;
93*4882a593Smuzhiyun gpiod_set_value(mtx1_wdt_device.gpiod, 1);
94*4882a593Smuzhiyun mod_timer(&mtx1_wdt_device.timer, jiffies + MTX1_WDT_INTERVAL);
95*4882a593Smuzhiyun }
96*4882a593Smuzhiyun mtx1_wdt_device.running++;
97*4882a593Smuzhiyun spin_unlock_irqrestore(&mtx1_wdt_device.lock, flags);
98*4882a593Smuzhiyun }
99*4882a593Smuzhiyun
mtx1_wdt_stop(void)100*4882a593Smuzhiyun static int mtx1_wdt_stop(void)
101*4882a593Smuzhiyun {
102*4882a593Smuzhiyun unsigned long flags;
103*4882a593Smuzhiyun
104*4882a593Smuzhiyun spin_lock_irqsave(&mtx1_wdt_device.lock, flags);
105*4882a593Smuzhiyun if (mtx1_wdt_device.queue) {
106*4882a593Smuzhiyun mtx1_wdt_device.queue = 0;
107*4882a593Smuzhiyun mtx1_wdt_device.gstate = 0;
108*4882a593Smuzhiyun gpiod_set_value(mtx1_wdt_device.gpiod, 0);
109*4882a593Smuzhiyun }
110*4882a593Smuzhiyun ticks = mtx1_wdt_device.default_ticks;
111*4882a593Smuzhiyun spin_unlock_irqrestore(&mtx1_wdt_device.lock, flags);
112*4882a593Smuzhiyun return 0;
113*4882a593Smuzhiyun }
114*4882a593Smuzhiyun
115*4882a593Smuzhiyun /* Filesystem functions */
116*4882a593Smuzhiyun
mtx1_wdt_open(struct inode * inode,struct file * file)117*4882a593Smuzhiyun static int mtx1_wdt_open(struct inode *inode, struct file *file)
118*4882a593Smuzhiyun {
119*4882a593Smuzhiyun if (test_and_set_bit(0, &mtx1_wdt_device.inuse))
120*4882a593Smuzhiyun return -EBUSY;
121*4882a593Smuzhiyun return stream_open(inode, file);
122*4882a593Smuzhiyun }
123*4882a593Smuzhiyun
124*4882a593Smuzhiyun
mtx1_wdt_release(struct inode * inode,struct file * file)125*4882a593Smuzhiyun static int mtx1_wdt_release(struct inode *inode, struct file *file)
126*4882a593Smuzhiyun {
127*4882a593Smuzhiyun clear_bit(0, &mtx1_wdt_device.inuse);
128*4882a593Smuzhiyun return 0;
129*4882a593Smuzhiyun }
130*4882a593Smuzhiyun
mtx1_wdt_ioctl(struct file * file,unsigned int cmd,unsigned long arg)131*4882a593Smuzhiyun static long mtx1_wdt_ioctl(struct file *file, unsigned int cmd,
132*4882a593Smuzhiyun unsigned long arg)
133*4882a593Smuzhiyun {
134*4882a593Smuzhiyun void __user *argp = (void __user *)arg;
135*4882a593Smuzhiyun int __user *p = (int __user *)argp;
136*4882a593Smuzhiyun unsigned int value;
137*4882a593Smuzhiyun static const struct watchdog_info ident = {
138*4882a593Smuzhiyun .options = WDIOF_CARDRESET,
139*4882a593Smuzhiyun .identity = "MTX-1 WDT",
140*4882a593Smuzhiyun };
141*4882a593Smuzhiyun
142*4882a593Smuzhiyun switch (cmd) {
143*4882a593Smuzhiyun case WDIOC_GETSUPPORT:
144*4882a593Smuzhiyun if (copy_to_user(argp, &ident, sizeof(ident)))
145*4882a593Smuzhiyun return -EFAULT;
146*4882a593Smuzhiyun break;
147*4882a593Smuzhiyun case WDIOC_GETSTATUS:
148*4882a593Smuzhiyun case WDIOC_GETBOOTSTATUS:
149*4882a593Smuzhiyun put_user(0, p);
150*4882a593Smuzhiyun break;
151*4882a593Smuzhiyun case WDIOC_SETOPTIONS:
152*4882a593Smuzhiyun if (get_user(value, p))
153*4882a593Smuzhiyun return -EFAULT;
154*4882a593Smuzhiyun if (value & WDIOS_ENABLECARD)
155*4882a593Smuzhiyun mtx1_wdt_start();
156*4882a593Smuzhiyun else if (value & WDIOS_DISABLECARD)
157*4882a593Smuzhiyun mtx1_wdt_stop();
158*4882a593Smuzhiyun else
159*4882a593Smuzhiyun return -EINVAL;
160*4882a593Smuzhiyun return 0;
161*4882a593Smuzhiyun case WDIOC_KEEPALIVE:
162*4882a593Smuzhiyun mtx1_wdt_reset();
163*4882a593Smuzhiyun break;
164*4882a593Smuzhiyun default:
165*4882a593Smuzhiyun return -ENOTTY;
166*4882a593Smuzhiyun }
167*4882a593Smuzhiyun return 0;
168*4882a593Smuzhiyun }
169*4882a593Smuzhiyun
170*4882a593Smuzhiyun
mtx1_wdt_write(struct file * file,const char * buf,size_t count,loff_t * ppos)171*4882a593Smuzhiyun static ssize_t mtx1_wdt_write(struct file *file, const char *buf,
172*4882a593Smuzhiyun size_t count, loff_t *ppos)
173*4882a593Smuzhiyun {
174*4882a593Smuzhiyun if (!count)
175*4882a593Smuzhiyun return -EIO;
176*4882a593Smuzhiyun mtx1_wdt_reset();
177*4882a593Smuzhiyun return count;
178*4882a593Smuzhiyun }
179*4882a593Smuzhiyun
180*4882a593Smuzhiyun static const struct file_operations mtx1_wdt_fops = {
181*4882a593Smuzhiyun .owner = THIS_MODULE,
182*4882a593Smuzhiyun .llseek = no_llseek,
183*4882a593Smuzhiyun .unlocked_ioctl = mtx1_wdt_ioctl,
184*4882a593Smuzhiyun .compat_ioctl = compat_ptr_ioctl,
185*4882a593Smuzhiyun .open = mtx1_wdt_open,
186*4882a593Smuzhiyun .write = mtx1_wdt_write,
187*4882a593Smuzhiyun .release = mtx1_wdt_release,
188*4882a593Smuzhiyun };
189*4882a593Smuzhiyun
190*4882a593Smuzhiyun
191*4882a593Smuzhiyun static struct miscdevice mtx1_wdt_misc = {
192*4882a593Smuzhiyun .minor = WATCHDOG_MINOR,
193*4882a593Smuzhiyun .name = "watchdog",
194*4882a593Smuzhiyun .fops = &mtx1_wdt_fops,
195*4882a593Smuzhiyun };
196*4882a593Smuzhiyun
197*4882a593Smuzhiyun
mtx1_wdt_probe(struct platform_device * pdev)198*4882a593Smuzhiyun static int mtx1_wdt_probe(struct platform_device *pdev)
199*4882a593Smuzhiyun {
200*4882a593Smuzhiyun int ret;
201*4882a593Smuzhiyun
202*4882a593Smuzhiyun mtx1_wdt_device.gpiod = devm_gpiod_get(&pdev->dev,
203*4882a593Smuzhiyun NULL, GPIOD_OUT_HIGH);
204*4882a593Smuzhiyun if (IS_ERR(mtx1_wdt_device.gpiod)) {
205*4882a593Smuzhiyun dev_err(&pdev->dev, "failed to request gpio");
206*4882a593Smuzhiyun return PTR_ERR(mtx1_wdt_device.gpiod);
207*4882a593Smuzhiyun }
208*4882a593Smuzhiyun
209*4882a593Smuzhiyun spin_lock_init(&mtx1_wdt_device.lock);
210*4882a593Smuzhiyun init_completion(&mtx1_wdt_device.stop);
211*4882a593Smuzhiyun mtx1_wdt_device.queue = 0;
212*4882a593Smuzhiyun clear_bit(0, &mtx1_wdt_device.inuse);
213*4882a593Smuzhiyun timer_setup(&mtx1_wdt_device.timer, mtx1_wdt_trigger, 0);
214*4882a593Smuzhiyun mtx1_wdt_device.default_ticks = ticks;
215*4882a593Smuzhiyun
216*4882a593Smuzhiyun ret = misc_register(&mtx1_wdt_misc);
217*4882a593Smuzhiyun if (ret < 0) {
218*4882a593Smuzhiyun dev_err(&pdev->dev, "failed to register\n");
219*4882a593Smuzhiyun return ret;
220*4882a593Smuzhiyun }
221*4882a593Smuzhiyun mtx1_wdt_start();
222*4882a593Smuzhiyun dev_info(&pdev->dev, "MTX-1 Watchdog driver\n");
223*4882a593Smuzhiyun return 0;
224*4882a593Smuzhiyun }
225*4882a593Smuzhiyun
mtx1_wdt_remove(struct platform_device * pdev)226*4882a593Smuzhiyun static int mtx1_wdt_remove(struct platform_device *pdev)
227*4882a593Smuzhiyun {
228*4882a593Smuzhiyun /* FIXME: do we need to lock this test ? */
229*4882a593Smuzhiyun if (mtx1_wdt_device.queue) {
230*4882a593Smuzhiyun mtx1_wdt_device.queue = 0;
231*4882a593Smuzhiyun wait_for_completion(&mtx1_wdt_device.stop);
232*4882a593Smuzhiyun }
233*4882a593Smuzhiyun
234*4882a593Smuzhiyun misc_deregister(&mtx1_wdt_misc);
235*4882a593Smuzhiyun return 0;
236*4882a593Smuzhiyun }
237*4882a593Smuzhiyun
238*4882a593Smuzhiyun static struct platform_driver mtx1_wdt_driver = {
239*4882a593Smuzhiyun .probe = mtx1_wdt_probe,
240*4882a593Smuzhiyun .remove = mtx1_wdt_remove,
241*4882a593Smuzhiyun .driver.name = "mtx1-wdt",
242*4882a593Smuzhiyun .driver.owner = THIS_MODULE,
243*4882a593Smuzhiyun };
244*4882a593Smuzhiyun
245*4882a593Smuzhiyun module_platform_driver(mtx1_wdt_driver);
246*4882a593Smuzhiyun
247*4882a593Smuzhiyun MODULE_AUTHOR("Michael Stickel, Florian Fainelli");
248*4882a593Smuzhiyun MODULE_DESCRIPTION("Driver for the MTX-1 watchdog");
249*4882a593Smuzhiyun MODULE_LICENSE("GPL");
250*4882a593Smuzhiyun MODULE_ALIAS("platform:mtx1-wdt");
251