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