2 * RDC321x watchdog driver
4 * Copyright (C) 2007-2010 Florian Fainelli <florian@openwrt.org>
6 * This driver is highly inspired from the cpu5_wdt driver
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.
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.
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.
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>
30 #include <linux/ioport.h>
31 #include <linux/timer.h>
32 #include <linux/completion.h>
33 #include <linux/jiffies.h>
34 #include <linux/platform_device.h>
35 #include <linux/watchdog.h>
37 #include <linux/uaccess.h>
38 #include <linux/mfd/rdc321x.h>
40 #define RDC_WDT_MASK 0x80000000 /* Mask */
41 #define RDC_WDT_EN 0x00800000 /* Enable bit */
42 #define RDC_WDT_WTI 0x00200000 /* Generate CPU reset/NMI/WDT on timeout */
43 #define RDC_WDT_RST 0x00100000 /* Reset bit */
44 #define RDC_WDT_WIF 0x00040000 /* WDT IRQ Flag */
45 #define RDC_WDT_IRT 0x00000100 /* IRQ Routing table */
46 #define RDC_WDT_CNT 0x00000001 /* WDT count */
48 #define RDC_CLS_TMR 0x80003844 /* Clear timer */
50 #define RDC_WDT_INTERVAL (HZ/10+1)
52 static int ticks = 1000;
54 /* some device data */
57 struct completion stop;
59 struct timer_list timer;
64 struct pci_dev *sb_pdev;
68 /* generic helper functions */
70 static void rdc321x_wdt_trigger(unsigned long unused)
75 if (rdc321x_wdt_device.running)
78 /* keep watchdog alive */
79 spin_lock_irqsave(&rdc321x_wdt_device.lock, flags);
80 pci_read_config_dword(rdc321x_wdt_device.sb_pdev,
81 rdc321x_wdt_device.base_reg, &val);
83 pci_write_config_dword(rdc321x_wdt_device.sb_pdev,
84 rdc321x_wdt_device.base_reg, val);
85 spin_unlock_irqrestore(&rdc321x_wdt_device.lock, flags);
88 if (rdc321x_wdt_device.queue && ticks)
89 mod_timer(&rdc321x_wdt_device.timer,
90 jiffies + RDC_WDT_INTERVAL);
92 /* ticks doesn't matter anyway */
93 complete(&rdc321x_wdt_device.stop);
98 static void rdc321x_wdt_reset(void)
100 ticks = rdc321x_wdt_device.default_ticks;
103 static void rdc321x_wdt_start(void)
107 if (!rdc321x_wdt_device.queue) {
108 rdc321x_wdt_device.queue = 1;
110 /* Clear the timer */
111 spin_lock_irqsave(&rdc321x_wdt_device.lock, flags);
112 pci_write_config_dword(rdc321x_wdt_device.sb_pdev,
113 rdc321x_wdt_device.base_reg, RDC_CLS_TMR);
115 /* Enable watchdog and set the timeout to 81.92 us */
116 pci_write_config_dword(rdc321x_wdt_device.sb_pdev,
117 rdc321x_wdt_device.base_reg,
118 RDC_WDT_EN | RDC_WDT_CNT);
119 spin_unlock_irqrestore(&rdc321x_wdt_device.lock, flags);
121 mod_timer(&rdc321x_wdt_device.timer,
122 jiffies + RDC_WDT_INTERVAL);
125 /* if process dies, counter is not decremented */
126 rdc321x_wdt_device.running++;
129 static int rdc321x_wdt_stop(void)
131 if (rdc321x_wdt_device.running)
132 rdc321x_wdt_device.running = 0;
134 ticks = rdc321x_wdt_device.default_ticks;
139 /* filesystem operations */
140 static int rdc321x_wdt_open(struct inode *inode, struct file *file)
142 if (test_and_set_bit(0, &rdc321x_wdt_device.inuse))
145 return nonseekable_open(inode, file);
148 static int rdc321x_wdt_release(struct inode *inode, struct file *file)
150 clear_bit(0, &rdc321x_wdt_device.inuse);
154 static long rdc321x_wdt_ioctl(struct file *file, unsigned int cmd,
157 void __user *argp = (void __user *)arg;
159 static const struct watchdog_info ident = {
160 .options = WDIOF_CARDRESET,
161 .identity = "RDC321x WDT",
166 case WDIOC_KEEPALIVE:
169 case WDIOC_GETSTATUS:
170 /* Read the value from the DATA register */
171 spin_lock_irqsave(&rdc321x_wdt_device.lock, flags);
172 pci_read_config_dword(rdc321x_wdt_device.sb_pdev,
173 rdc321x_wdt_device.base_reg, &value);
174 spin_unlock_irqrestore(&rdc321x_wdt_device.lock, flags);
175 if (copy_to_user(argp, &value, sizeof(u32)))
178 case WDIOC_GETSUPPORT:
179 if (copy_to_user(argp, &ident, sizeof(ident)))
182 case WDIOC_SETOPTIONS:
183 if (copy_from_user(&value, argp, sizeof(int)))
186 case WDIOS_ENABLECARD:
189 case WDIOS_DISABLECARD:
190 return rdc321x_wdt_stop();
201 static ssize_t rdc321x_wdt_write(struct file *file, const char __user *buf,
202 size_t count, loff_t *ppos)
212 static const struct file_operations rdc321x_wdt_fops = {
213 .owner = THIS_MODULE,
215 .unlocked_ioctl = rdc321x_wdt_ioctl,
216 .open = rdc321x_wdt_open,
217 .write = rdc321x_wdt_write,
218 .release = rdc321x_wdt_release,
221 static struct miscdevice rdc321x_wdt_misc = {
222 .minor = WATCHDOG_MINOR,
224 .fops = &rdc321x_wdt_fops,
227 static int rdc321x_wdt_probe(struct platform_device *pdev)
231 struct rdc321x_wdt_pdata *pdata;
233 pdata = dev_get_platdata(&pdev->dev);
235 dev_err(&pdev->dev, "no platform data supplied\n");
239 r = platform_get_resource_byname(pdev, IORESOURCE_IO, "wdt-reg");
241 dev_err(&pdev->dev, "failed to get wdt-reg resource\n");
245 rdc321x_wdt_device.sb_pdev = pdata->sb_pdev;
246 rdc321x_wdt_device.base_reg = r->start;
248 err = misc_register(&rdc321x_wdt_misc);
250 dev_err(&pdev->dev, "misc_register failed\n");
254 spin_lock_init(&rdc321x_wdt_device.lock);
256 /* Reset the watchdog */
257 pci_write_config_dword(rdc321x_wdt_device.sb_pdev,
258 rdc321x_wdt_device.base_reg, RDC_WDT_RST);
260 init_completion(&rdc321x_wdt_device.stop);
261 rdc321x_wdt_device.queue = 0;
263 clear_bit(0, &rdc321x_wdt_device.inuse);
265 setup_timer(&rdc321x_wdt_device.timer, rdc321x_wdt_trigger, 0);
267 rdc321x_wdt_device.default_ticks = ticks;
269 dev_info(&pdev->dev, "watchdog init success\n");
274 static int rdc321x_wdt_remove(struct platform_device *pdev)
276 if (rdc321x_wdt_device.queue) {
277 rdc321x_wdt_device.queue = 0;
278 wait_for_completion(&rdc321x_wdt_device.stop);
281 misc_deregister(&rdc321x_wdt_misc);
286 static struct platform_driver rdc321x_wdt_driver = {
287 .probe = rdc321x_wdt_probe,
288 .remove = rdc321x_wdt_remove,
290 .name = "rdc321x-wdt",
294 module_platform_driver(rdc321x_wdt_driver);
296 MODULE_AUTHOR("Florian Fainelli <florian@openwrt.org>");
297 MODULE_DESCRIPTION("RDC321x watchdog driver");
298 MODULE_LICENSE("GPL");