1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * RDC321x watchdog driver
5 * Copyright (C) 2007-2010 Florian Fainelli <florian@openwrt.org>
7 * This driver is highly inspired from the cpu5_wdt driver
10 #include <linux/module.h>
11 #include <linux/moduleparam.h>
12 #include <linux/types.h>
13 #include <linux/errno.h>
14 #include <linux/miscdevice.h>
16 #include <linux/ioport.h>
17 #include <linux/timer.h>
18 #include <linux/completion.h>
19 #include <linux/jiffies.h>
20 #include <linux/platform_device.h>
21 #include <linux/watchdog.h>
23 #include <linux/uaccess.h>
24 #include <linux/mfd/rdc321x.h>
26 #define RDC_WDT_MASK 0x80000000 /* Mask */
27 #define RDC_WDT_EN 0x00800000 /* Enable bit */
28 #define RDC_WDT_WTI 0x00200000 /* Generate CPU reset/NMI/WDT on timeout */
29 #define RDC_WDT_RST 0x00100000 /* Reset bit */
30 #define RDC_WDT_WIF 0x00040000 /* WDT IRQ Flag */
31 #define RDC_WDT_IRT 0x00000100 /* IRQ Routing table */
32 #define RDC_WDT_CNT 0x00000001 /* WDT count */
34 #define RDC_CLS_TMR 0x80003844 /* Clear timer */
36 #define RDC_WDT_INTERVAL (HZ/10+1)
38 static int ticks = 1000;
40 /* some device data */
43 struct completion stop;
45 struct timer_list timer;
50 struct pci_dev *sb_pdev;
54 /* generic helper functions */
56 static void rdc321x_wdt_trigger(struct timer_list *unused)
61 if (rdc321x_wdt_device.running)
64 /* keep watchdog alive */
65 spin_lock_irqsave(&rdc321x_wdt_device.lock, flags);
66 pci_read_config_dword(rdc321x_wdt_device.sb_pdev,
67 rdc321x_wdt_device.base_reg, &val);
69 pci_write_config_dword(rdc321x_wdt_device.sb_pdev,
70 rdc321x_wdt_device.base_reg, val);
71 spin_unlock_irqrestore(&rdc321x_wdt_device.lock, flags);
74 if (rdc321x_wdt_device.queue && ticks)
75 mod_timer(&rdc321x_wdt_device.timer,
76 jiffies + RDC_WDT_INTERVAL);
78 /* ticks doesn't matter anyway */
79 complete(&rdc321x_wdt_device.stop);
84 static void rdc321x_wdt_reset(void)
86 ticks = rdc321x_wdt_device.default_ticks;
89 static void rdc321x_wdt_start(void)
93 if (!rdc321x_wdt_device.queue) {
94 rdc321x_wdt_device.queue = 1;
97 spin_lock_irqsave(&rdc321x_wdt_device.lock, flags);
98 pci_write_config_dword(rdc321x_wdt_device.sb_pdev,
99 rdc321x_wdt_device.base_reg, RDC_CLS_TMR);
101 /* Enable watchdog and set the timeout to 81.92 us */
102 pci_write_config_dword(rdc321x_wdt_device.sb_pdev,
103 rdc321x_wdt_device.base_reg,
104 RDC_WDT_EN | RDC_WDT_CNT);
105 spin_unlock_irqrestore(&rdc321x_wdt_device.lock, flags);
107 mod_timer(&rdc321x_wdt_device.timer,
108 jiffies + RDC_WDT_INTERVAL);
111 /* if process dies, counter is not decremented */
112 rdc321x_wdt_device.running++;
115 static int rdc321x_wdt_stop(void)
117 if (rdc321x_wdt_device.running)
118 rdc321x_wdt_device.running = 0;
120 ticks = rdc321x_wdt_device.default_ticks;
125 /* filesystem operations */
126 static int rdc321x_wdt_open(struct inode *inode, struct file *file)
128 if (test_and_set_bit(0, &rdc321x_wdt_device.inuse))
131 return stream_open(inode, file);
134 static int rdc321x_wdt_release(struct inode *inode, struct file *file)
136 clear_bit(0, &rdc321x_wdt_device.inuse);
140 static long rdc321x_wdt_ioctl(struct file *file, unsigned int cmd,
143 void __user *argp = (void __user *)arg;
145 static const struct watchdog_info ident = {
146 .options = WDIOF_CARDRESET,
147 .identity = "RDC321x WDT",
152 case WDIOC_KEEPALIVE:
155 case WDIOC_GETSTATUS:
156 /* Read the value from the DATA register */
157 spin_lock_irqsave(&rdc321x_wdt_device.lock, flags);
158 pci_read_config_dword(rdc321x_wdt_device.sb_pdev,
159 rdc321x_wdt_device.base_reg, &value);
160 spin_unlock_irqrestore(&rdc321x_wdt_device.lock, flags);
161 if (copy_to_user(argp, &value, sizeof(u32)))
164 case WDIOC_GETSUPPORT:
165 if (copy_to_user(argp, &ident, sizeof(ident)))
168 case WDIOC_SETOPTIONS:
169 if (copy_from_user(&value, argp, sizeof(int)))
172 case WDIOS_ENABLECARD:
175 case WDIOS_DISABLECARD:
176 return rdc321x_wdt_stop();
187 static ssize_t rdc321x_wdt_write(struct file *file, const char __user *buf,
188 size_t count, loff_t *ppos)
198 static const struct file_operations rdc321x_wdt_fops = {
199 .owner = THIS_MODULE,
201 .unlocked_ioctl = rdc321x_wdt_ioctl,
202 .compat_ioctl = compat_ptr_ioctl,
203 .open = rdc321x_wdt_open,
204 .write = rdc321x_wdt_write,
205 .release = rdc321x_wdt_release,
208 static struct miscdevice rdc321x_wdt_misc = {
209 .minor = WATCHDOG_MINOR,
211 .fops = &rdc321x_wdt_fops,
214 static int rdc321x_wdt_probe(struct platform_device *pdev)
218 struct rdc321x_wdt_pdata *pdata;
220 pdata = dev_get_platdata(&pdev->dev);
222 dev_err(&pdev->dev, "no platform data supplied\n");
226 r = platform_get_resource_byname(pdev, IORESOURCE_IO, "wdt-reg");
228 dev_err(&pdev->dev, "failed to get wdt-reg resource\n");
232 rdc321x_wdt_device.sb_pdev = pdata->sb_pdev;
233 rdc321x_wdt_device.base_reg = r->start;
234 rdc321x_wdt_device.queue = 0;
235 rdc321x_wdt_device.default_ticks = ticks;
237 err = misc_register(&rdc321x_wdt_misc);
239 dev_err(&pdev->dev, "misc_register failed\n");
243 spin_lock_init(&rdc321x_wdt_device.lock);
245 /* Reset the watchdog */
246 pci_write_config_dword(rdc321x_wdt_device.sb_pdev,
247 rdc321x_wdt_device.base_reg, RDC_WDT_RST);
249 init_completion(&rdc321x_wdt_device.stop);
251 clear_bit(0, &rdc321x_wdt_device.inuse);
253 timer_setup(&rdc321x_wdt_device.timer, rdc321x_wdt_trigger, 0);
255 dev_info(&pdev->dev, "watchdog init success\n");
260 static int rdc321x_wdt_remove(struct platform_device *pdev)
262 if (rdc321x_wdt_device.queue) {
263 rdc321x_wdt_device.queue = 0;
264 wait_for_completion(&rdc321x_wdt_device.stop);
267 misc_deregister(&rdc321x_wdt_misc);
272 static struct platform_driver rdc321x_wdt_driver = {
273 .probe = rdc321x_wdt_probe,
274 .remove = rdc321x_wdt_remove,
276 .name = "rdc321x-wdt",
280 module_platform_driver(rdc321x_wdt_driver);
282 MODULE_AUTHOR("Florian Fainelli <florian@openwrt.org>");
283 MODULE_DESCRIPTION("RDC321x watchdog driver");
284 MODULE_LICENSE("GPL");