Commit | Line | Data |
---|---|---|
b3e8f2c1 FF |
1 | /* |
2 | * RDC321x watchdog driver | |
3 | * | |
842102f3 | 4 | * Copyright (C) 2007-2010 Florian Fainelli <florian@openwrt.org> |
b3e8f2c1 FF |
5 | * |
6 | * This driver is highly inspired from the cpu5_wdt driver | |
7 | * | |
8 | * This program is free software; you can redistribute it and/or modify | |
9 | * it under the terms of the GNU General Public License as published by | |
10 | * the Free Software Foundation; either version 2 of the License, or | |
11 | * (at your option) any later version. | |
12 | * | |
13 | * This program is distributed in the hope that it will be useful, | |
14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
16 | * GNU General Public License for more details. | |
17 | * | |
18 | * You should have received a copy of the GNU General Public License | |
19 | * along with this program; if not, write to the Free Software | |
20 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. | |
21 | * | |
22 | */ | |
23 | ||
24 | #include <linux/module.h> | |
25 | #include <linux/moduleparam.h> | |
26 | #include <linux/types.h> | |
27 | #include <linux/errno.h> | |
28 | #include <linux/miscdevice.h> | |
29 | #include <linux/fs.h> | |
30 | #include <linux/init.h> | |
31 | #include <linux/ioport.h> | |
32 | #include <linux/timer.h> | |
33 | #include <linux/completion.h> | |
34 | #include <linux/jiffies.h> | |
35 | #include <linux/platform_device.h> | |
36 | #include <linux/watchdog.h> | |
37 | #include <linux/io.h> | |
38 | #include <linux/uaccess.h> | |
842102f3 | 39 | #include <linux/mfd/rdc321x.h> |
46673ed2 | 40 | #include <linux/mfd/core.h> |
b3e8f2c1 FF |
41 | |
42 | #define RDC_WDT_MASK 0x80000000 /* Mask */ | |
43 | #define RDC_WDT_EN 0x00800000 /* Enable bit */ | |
44 | #define RDC_WDT_WTI 0x00200000 /* Generate CPU reset/NMI/WDT on timeout */ | |
45 | #define RDC_WDT_RST 0x00100000 /* Reset bit */ | |
46 | #define RDC_WDT_WIF 0x00040000 /* WDT IRQ Flag */ | |
47 | #define RDC_WDT_IRT 0x00000100 /* IRQ Routing table */ | |
48 | #define RDC_WDT_CNT 0x00000001 /* WDT count */ | |
49 | ||
50 | #define RDC_CLS_TMR 0x80003844 /* Clear timer */ | |
51 | ||
52 | #define RDC_WDT_INTERVAL (HZ/10+1) | |
53 | ||
54 | static int ticks = 1000; | |
55 | ||
56 | /* some device data */ | |
57 | ||
58 | static struct { | |
59 | struct completion stop; | |
60 | int running; | |
61 | struct timer_list timer; | |
62 | int queue; | |
63 | int default_ticks; | |
64 | unsigned long inuse; | |
65 | spinlock_t lock; | |
842102f3 FF |
66 | struct pci_dev *sb_pdev; |
67 | int base_reg; | |
b3e8f2c1 FF |
68 | } rdc321x_wdt_device; |
69 | ||
70 | /* generic helper functions */ | |
71 | ||
72 | static void rdc321x_wdt_trigger(unsigned long unused) | |
73 | { | |
74 | unsigned long flags; | |
842102f3 | 75 | u32 val; |
b3e8f2c1 FF |
76 | |
77 | if (rdc321x_wdt_device.running) | |
78 | ticks--; | |
79 | ||
80 | /* keep watchdog alive */ | |
81 | spin_lock_irqsave(&rdc321x_wdt_device.lock, flags); | |
842102f3 FF |
82 | pci_read_config_dword(rdc321x_wdt_device.sb_pdev, |
83 | rdc321x_wdt_device.base_reg, &val); | |
84 | val |= RDC_WDT_EN; | |
85 | pci_write_config_dword(rdc321x_wdt_device.sb_pdev, | |
86 | rdc321x_wdt_device.base_reg, val); | |
b3e8f2c1 FF |
87 | spin_unlock_irqrestore(&rdc321x_wdt_device.lock, flags); |
88 | ||
89 | /* requeue?? */ | |
90 | if (rdc321x_wdt_device.queue && ticks) | |
91 | mod_timer(&rdc321x_wdt_device.timer, | |
92 | jiffies + RDC_WDT_INTERVAL); | |
93 | else { | |
94 | /* ticks doesn't matter anyway */ | |
95 | complete(&rdc321x_wdt_device.stop); | |
96 | } | |
97 | ||
98 | } | |
99 | ||
100 | static void rdc321x_wdt_reset(void) | |
101 | { | |
102 | ticks = rdc321x_wdt_device.default_ticks; | |
103 | } | |
104 | ||
105 | static void rdc321x_wdt_start(void) | |
106 | { | |
107 | unsigned long flags; | |
108 | ||
109 | if (!rdc321x_wdt_device.queue) { | |
110 | rdc321x_wdt_device.queue = 1; | |
111 | ||
112 | /* Clear the timer */ | |
113 | spin_lock_irqsave(&rdc321x_wdt_device.lock, flags); | |
842102f3 FF |
114 | pci_write_config_dword(rdc321x_wdt_device.sb_pdev, |
115 | rdc321x_wdt_device.base_reg, RDC_CLS_TMR); | |
b3e8f2c1 FF |
116 | |
117 | /* Enable watchdog and set the timeout to 81.92 us */ | |
842102f3 FF |
118 | pci_write_config_dword(rdc321x_wdt_device.sb_pdev, |
119 | rdc321x_wdt_device.base_reg, | |
120 | RDC_WDT_EN | RDC_WDT_CNT); | |
b3e8f2c1 FF |
121 | spin_unlock_irqrestore(&rdc321x_wdt_device.lock, flags); |
122 | ||
123 | mod_timer(&rdc321x_wdt_device.timer, | |
124 | jiffies + RDC_WDT_INTERVAL); | |
125 | } | |
126 | ||
127 | /* if process dies, counter is not decremented */ | |
128 | rdc321x_wdt_device.running++; | |
129 | } | |
130 | ||
131 | static int rdc321x_wdt_stop(void) | |
132 | { | |
133 | if (rdc321x_wdt_device.running) | |
134 | rdc321x_wdt_device.running = 0; | |
135 | ||
136 | ticks = rdc321x_wdt_device.default_ticks; | |
137 | ||
138 | return -EIO; | |
139 | } | |
140 | ||
141 | /* filesystem operations */ | |
142 | static int rdc321x_wdt_open(struct inode *inode, struct file *file) | |
143 | { | |
144 | if (test_and_set_bit(0, &rdc321x_wdt_device.inuse)) | |
145 | return -EBUSY; | |
146 | ||
147 | return nonseekable_open(inode, file); | |
148 | } | |
149 | ||
150 | static int rdc321x_wdt_release(struct inode *inode, struct file *file) | |
151 | { | |
152 | clear_bit(0, &rdc321x_wdt_device.inuse); | |
153 | return 0; | |
154 | } | |
155 | ||
7275fc8c WVS |
156 | static long rdc321x_wdt_ioctl(struct file *file, unsigned int cmd, |
157 | unsigned long arg) | |
b3e8f2c1 FF |
158 | { |
159 | void __user *argp = (void __user *)arg; | |
842102f3 | 160 | u32 value; |
42747d71 | 161 | static const struct watchdog_info ident = { |
b3e8f2c1 FF |
162 | .options = WDIOF_CARDRESET, |
163 | .identity = "RDC321x WDT", | |
164 | }; | |
165 | unsigned long flags; | |
166 | ||
167 | switch (cmd) { | |
168 | case WDIOC_KEEPALIVE: | |
169 | rdc321x_wdt_reset(); | |
170 | break; | |
171 | case WDIOC_GETSTATUS: | |
172 | /* Read the value from the DATA register */ | |
173 | spin_lock_irqsave(&rdc321x_wdt_device.lock, flags); | |
842102f3 FF |
174 | pci_read_config_dword(rdc321x_wdt_device.sb_pdev, |
175 | rdc321x_wdt_device.base_reg, &value); | |
b3e8f2c1 | 176 | spin_unlock_irqrestore(&rdc321x_wdt_device.lock, flags); |
842102f3 | 177 | if (copy_to_user(argp, &value, sizeof(u32))) |
b3e8f2c1 FF |
178 | return -EFAULT; |
179 | break; | |
180 | case WDIOC_GETSUPPORT: | |
181 | if (copy_to_user(argp, &ident, sizeof(ident))) | |
182 | return -EFAULT; | |
183 | break; | |
184 | case WDIOC_SETOPTIONS: | |
185 | if (copy_from_user(&value, argp, sizeof(int))) | |
186 | return -EFAULT; | |
187 | switch (value) { | |
188 | case WDIOS_ENABLECARD: | |
189 | rdc321x_wdt_start(); | |
190 | break; | |
191 | case WDIOS_DISABLECARD: | |
192 | return rdc321x_wdt_stop(); | |
193 | default: | |
194 | return -EINVAL; | |
195 | } | |
196 | break; | |
197 | default: | |
198 | return -ENOTTY; | |
199 | } | |
200 | return 0; | |
201 | } | |
202 | ||
203 | static ssize_t rdc321x_wdt_write(struct file *file, const char __user *buf, | |
204 | size_t count, loff_t *ppos) | |
205 | { | |
206 | if (!count) | |
207 | return -EIO; | |
208 | ||
209 | rdc321x_wdt_reset(); | |
210 | ||
211 | return count; | |
212 | } | |
213 | ||
214 | static const struct file_operations rdc321x_wdt_fops = { | |
215 | .owner = THIS_MODULE, | |
216 | .llseek = no_llseek, | |
7275fc8c | 217 | .unlocked_ioctl = rdc321x_wdt_ioctl, |
b3e8f2c1 FF |
218 | .open = rdc321x_wdt_open, |
219 | .write = rdc321x_wdt_write, | |
220 | .release = rdc321x_wdt_release, | |
221 | }; | |
222 | ||
223 | static struct miscdevice rdc321x_wdt_misc = { | |
224 | .minor = WATCHDOG_MINOR, | |
225 | .name = "watchdog", | |
226 | .fops = &rdc321x_wdt_fops, | |
227 | }; | |
228 | ||
229 | static int __devinit rdc321x_wdt_probe(struct platform_device *pdev) | |
230 | { | |
231 | int err; | |
842102f3 FF |
232 | struct resource *r; |
233 | struct rdc321x_wdt_pdata *pdata; | |
234 | ||
46673ed2 | 235 | pdata = mfd_get_data(pdev); |
842102f3 FF |
236 | if (!pdata) { |
237 | dev_err(&pdev->dev, "no platform data supplied\n"); | |
238 | return -ENODEV; | |
239 | } | |
240 | ||
8deca39e | 241 | r = platform_get_resource_byname(pdev, IORESOURCE_IO, "wdt-reg"); |
842102f3 FF |
242 | if (!r) { |
243 | dev_err(&pdev->dev, "failed to get wdt-reg resource\n"); | |
244 | return -ENODEV; | |
245 | } | |
246 | ||
247 | rdc321x_wdt_device.sb_pdev = pdata->sb_pdev; | |
248 | rdc321x_wdt_device.base_reg = r->start; | |
b3e8f2c1 FF |
249 | |
250 | err = misc_register(&rdc321x_wdt_misc); | |
251 | if (err < 0) { | |
842102f3 | 252 | dev_err(&pdev->dev, "misc_register failed\n"); |
b3e8f2c1 FF |
253 | return err; |
254 | } | |
255 | ||
256 | spin_lock_init(&rdc321x_wdt_device.lock); | |
257 | ||
258 | /* Reset the watchdog */ | |
842102f3 FF |
259 | pci_write_config_dword(rdc321x_wdt_device.sb_pdev, |
260 | rdc321x_wdt_device.base_reg, RDC_WDT_RST); | |
b3e8f2c1 FF |
261 | |
262 | init_completion(&rdc321x_wdt_device.stop); | |
263 | rdc321x_wdt_device.queue = 0; | |
264 | ||
265 | clear_bit(0, &rdc321x_wdt_device.inuse); | |
266 | ||
267 | setup_timer(&rdc321x_wdt_device.timer, rdc321x_wdt_trigger, 0); | |
268 | ||
269 | rdc321x_wdt_device.default_ticks = ticks; | |
270 | ||
842102f3 | 271 | dev_info(&pdev->dev, "watchdog init success\n"); |
b3e8f2c1 FF |
272 | |
273 | return 0; | |
274 | } | |
275 | ||
b6bf291f | 276 | static int __devexit rdc321x_wdt_remove(struct platform_device *pdev) |
b3e8f2c1 FF |
277 | { |
278 | if (rdc321x_wdt_device.queue) { | |
279 | rdc321x_wdt_device.queue = 0; | |
280 | wait_for_completion(&rdc321x_wdt_device.stop); | |
281 | } | |
282 | ||
283 | misc_deregister(&rdc321x_wdt_misc); | |
284 | ||
285 | return 0; | |
286 | } | |
287 | ||
288 | static struct platform_driver rdc321x_wdt_driver = { | |
289 | .probe = rdc321x_wdt_probe, | |
b6bf291f | 290 | .remove = __devexit_p(rdc321x_wdt_remove), |
b3e8f2c1 FF |
291 | .driver = { |
292 | .owner = THIS_MODULE, | |
293 | .name = "rdc321x-wdt", | |
294 | }, | |
295 | }; | |
296 | ||
297 | static int __init rdc321x_wdt_init(void) | |
298 | { | |
299 | return platform_driver_register(&rdc321x_wdt_driver); | |
300 | } | |
301 | ||
302 | static void __exit rdc321x_wdt_exit(void) | |
303 | { | |
304 | platform_driver_unregister(&rdc321x_wdt_driver); | |
305 | } | |
306 | ||
307 | module_init(rdc321x_wdt_init); | |
308 | module_exit(rdc321x_wdt_exit); | |
309 | ||
310 | MODULE_AUTHOR("Florian Fainelli <florian@openwrt.org>"); | |
311 | MODULE_DESCRIPTION("RDC321x watchdog driver"); | |
312 | MODULE_LICENSE("GPL"); | |
313 | MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR); |