blob: 57187efeb86f1104d1cb0fe3283f2cb389700e7a [file] [log] [blame]
Thomas Gleixner74ba9202019-05-20 09:19:02 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Florian Fainellib3e8f2c2008-02-25 12:59:26 +01002/*
3 * RDC321x watchdog driver
4 *
Florian Fainelli842102f2010-03-22 22:56:24 -04005 * Copyright (C) 2007-2010 Florian Fainelli <florian@openwrt.org>
Florian Fainellib3e8f2c2008-02-25 12:59:26 +01006 *
7 * This driver is highly inspired from the cpu5_wdt driver
Florian Fainellib3e8f2c2008-02-25 12:59:26 +01008 */
9
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>
15#include <linux/fs.h>
Florian Fainellib3e8f2c2008-02-25 12:59:26 +010016#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>
22#include <linux/io.h>
23#include <linux/uaccess.h>
Florian Fainelli842102f2010-03-22 22:56:24 -040024#include <linux/mfd/rdc321x.h>
Florian Fainellib3e8f2c2008-02-25 12:59:26 +010025
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 */
33
34#define RDC_CLS_TMR 0x80003844 /* Clear timer */
35
36#define RDC_WDT_INTERVAL (HZ/10+1)
37
38static int ticks = 1000;
39
40/* some device data */
41
42static struct {
43 struct completion stop;
44 int running;
45 struct timer_list timer;
46 int queue;
47 int default_ticks;
48 unsigned long inuse;
49 spinlock_t lock;
Florian Fainelli842102f2010-03-22 22:56:24 -040050 struct pci_dev *sb_pdev;
51 int base_reg;
Florian Fainellib3e8f2c2008-02-25 12:59:26 +010052} rdc321x_wdt_device;
53
54/* generic helper functions */
55
Kees Cooke99e88a2017-10-16 14:43:17 -070056static void rdc321x_wdt_trigger(struct timer_list *unused)
Florian Fainellib3e8f2c2008-02-25 12:59:26 +010057{
58 unsigned long flags;
Florian Fainelli842102f2010-03-22 22:56:24 -040059 u32 val;
Florian Fainellib3e8f2c2008-02-25 12:59:26 +010060
61 if (rdc321x_wdt_device.running)
62 ticks--;
63
64 /* keep watchdog alive */
65 spin_lock_irqsave(&rdc321x_wdt_device.lock, flags);
Florian Fainelli842102f2010-03-22 22:56:24 -040066 pci_read_config_dword(rdc321x_wdt_device.sb_pdev,
67 rdc321x_wdt_device.base_reg, &val);
68 val |= RDC_WDT_EN;
69 pci_write_config_dword(rdc321x_wdt_device.sb_pdev,
70 rdc321x_wdt_device.base_reg, val);
Florian Fainellib3e8f2c2008-02-25 12:59:26 +010071 spin_unlock_irqrestore(&rdc321x_wdt_device.lock, flags);
72
73 /* requeue?? */
74 if (rdc321x_wdt_device.queue && ticks)
75 mod_timer(&rdc321x_wdt_device.timer,
76 jiffies + RDC_WDT_INTERVAL);
77 else {
78 /* ticks doesn't matter anyway */
79 complete(&rdc321x_wdt_device.stop);
80 }
81
82}
83
84static void rdc321x_wdt_reset(void)
85{
86 ticks = rdc321x_wdt_device.default_ticks;
87}
88
89static void rdc321x_wdt_start(void)
90{
91 unsigned long flags;
92
93 if (!rdc321x_wdt_device.queue) {
94 rdc321x_wdt_device.queue = 1;
95
96 /* Clear the timer */
97 spin_lock_irqsave(&rdc321x_wdt_device.lock, flags);
Florian Fainelli842102f2010-03-22 22:56:24 -040098 pci_write_config_dword(rdc321x_wdt_device.sb_pdev,
99 rdc321x_wdt_device.base_reg, RDC_CLS_TMR);
Florian Fainellib3e8f2c2008-02-25 12:59:26 +0100100
101 /* Enable watchdog and set the timeout to 81.92 us */
Florian Fainelli842102f2010-03-22 22:56:24 -0400102 pci_write_config_dword(rdc321x_wdt_device.sb_pdev,
103 rdc321x_wdt_device.base_reg,
104 RDC_WDT_EN | RDC_WDT_CNT);
Florian Fainellib3e8f2c2008-02-25 12:59:26 +0100105 spin_unlock_irqrestore(&rdc321x_wdt_device.lock, flags);
106
107 mod_timer(&rdc321x_wdt_device.timer,
108 jiffies + RDC_WDT_INTERVAL);
109 }
110
111 /* if process dies, counter is not decremented */
112 rdc321x_wdt_device.running++;
113}
114
115static int rdc321x_wdt_stop(void)
116{
117 if (rdc321x_wdt_device.running)
118 rdc321x_wdt_device.running = 0;
119
120 ticks = rdc321x_wdt_device.default_ticks;
121
122 return -EIO;
123}
124
125/* filesystem operations */
126static int rdc321x_wdt_open(struct inode *inode, struct file *file)
127{
128 if (test_and_set_bit(0, &rdc321x_wdt_device.inuse))
129 return -EBUSY;
130
Kirill Smelkovc5bf68f2019-03-26 23:51:19 +0300131 return stream_open(inode, file);
Florian Fainellib3e8f2c2008-02-25 12:59:26 +0100132}
133
134static int rdc321x_wdt_release(struct inode *inode, struct file *file)
135{
136 clear_bit(0, &rdc321x_wdt_device.inuse);
137 return 0;
138}
139
Wim Van Sebroeck7275fc82008-09-18 12:26:15 +0000140static long rdc321x_wdt_ioctl(struct file *file, unsigned int cmd,
141 unsigned long arg)
Florian Fainellib3e8f2c2008-02-25 12:59:26 +0100142{
143 void __user *argp = (void __user *)arg;
Florian Fainelli842102f2010-03-22 22:56:24 -0400144 u32 value;
Wim Van Sebroeck42747d72009-12-26 18:55:22 +0000145 static const struct watchdog_info ident = {
Florian Fainellib3e8f2c2008-02-25 12:59:26 +0100146 .options = WDIOF_CARDRESET,
147 .identity = "RDC321x WDT",
148 };
149 unsigned long flags;
150
151 switch (cmd) {
152 case WDIOC_KEEPALIVE:
153 rdc321x_wdt_reset();
154 break;
155 case WDIOC_GETSTATUS:
156 /* Read the value from the DATA register */
157 spin_lock_irqsave(&rdc321x_wdt_device.lock, flags);
Florian Fainelli842102f2010-03-22 22:56:24 -0400158 pci_read_config_dword(rdc321x_wdt_device.sb_pdev,
159 rdc321x_wdt_device.base_reg, &value);
Florian Fainellib3e8f2c2008-02-25 12:59:26 +0100160 spin_unlock_irqrestore(&rdc321x_wdt_device.lock, flags);
Florian Fainelli842102f2010-03-22 22:56:24 -0400161 if (copy_to_user(argp, &value, sizeof(u32)))
Florian Fainellib3e8f2c2008-02-25 12:59:26 +0100162 return -EFAULT;
163 break;
164 case WDIOC_GETSUPPORT:
165 if (copy_to_user(argp, &ident, sizeof(ident)))
166 return -EFAULT;
167 break;
168 case WDIOC_SETOPTIONS:
169 if (copy_from_user(&value, argp, sizeof(int)))
170 return -EFAULT;
171 switch (value) {
172 case WDIOS_ENABLECARD:
173 rdc321x_wdt_start();
174 break;
175 case WDIOS_DISABLECARD:
176 return rdc321x_wdt_stop();
177 default:
178 return -EINVAL;
179 }
180 break;
181 default:
182 return -ENOTTY;
183 }
184 return 0;
185}
186
187static ssize_t rdc321x_wdt_write(struct file *file, const char __user *buf,
188 size_t count, loff_t *ppos)
189{
190 if (!count)
191 return -EIO;
192
193 rdc321x_wdt_reset();
194
195 return count;
196}
197
198static const struct file_operations rdc321x_wdt_fops = {
199 .owner = THIS_MODULE,
200 .llseek = no_llseek,
Wim Van Sebroeck7275fc82008-09-18 12:26:15 +0000201 .unlocked_ioctl = rdc321x_wdt_ioctl,
Arnd Bergmannb6dfb242019-06-03 14:23:09 +0200202 .compat_ioctl = compat_ptr_ioctl,
Florian Fainellib3e8f2c2008-02-25 12:59:26 +0100203 .open = rdc321x_wdt_open,
204 .write = rdc321x_wdt_write,
205 .release = rdc321x_wdt_release,
206};
207
208static struct miscdevice rdc321x_wdt_misc = {
209 .minor = WATCHDOG_MINOR,
210 .name = "watchdog",
211 .fops = &rdc321x_wdt_fops,
212};
213
Bill Pemberton2d991a12012-11-19 13:21:41 -0500214static int rdc321x_wdt_probe(struct platform_device *pdev)
Florian Fainellib3e8f2c2008-02-25 12:59:26 +0100215{
216 int err;
Florian Fainelli842102f2010-03-22 22:56:24 -0400217 struct resource *r;
218 struct rdc321x_wdt_pdata *pdata;
219
Jingoo Hanbc8fdfb2013-07-30 19:58:51 +0900220 pdata = dev_get_platdata(&pdev->dev);
Florian Fainelli842102f2010-03-22 22:56:24 -0400221 if (!pdata) {
222 dev_err(&pdev->dev, "no platform data supplied\n");
223 return -ENODEV;
224 }
225
Florian Fainelli8deca39e2010-05-15 22:58:27 +0200226 r = platform_get_resource_byname(pdev, IORESOURCE_IO, "wdt-reg");
Florian Fainelli842102f2010-03-22 22:56:24 -0400227 if (!r) {
228 dev_err(&pdev->dev, "failed to get wdt-reg resource\n");
229 return -ENODEV;
230 }
231
232 rdc321x_wdt_device.sb_pdev = pdata->sb_pdev;
233 rdc321x_wdt_device.base_reg = r->start;
Florian Fainellib3e8f2c2008-02-25 12:59:26 +0100234
235 err = misc_register(&rdc321x_wdt_misc);
236 if (err < 0) {
Florian Fainelli842102f2010-03-22 22:56:24 -0400237 dev_err(&pdev->dev, "misc_register failed\n");
Florian Fainellib3e8f2c2008-02-25 12:59:26 +0100238 return err;
239 }
240
241 spin_lock_init(&rdc321x_wdt_device.lock);
242
243 /* Reset the watchdog */
Florian Fainelli842102f2010-03-22 22:56:24 -0400244 pci_write_config_dword(rdc321x_wdt_device.sb_pdev,
245 rdc321x_wdt_device.base_reg, RDC_WDT_RST);
Florian Fainellib3e8f2c2008-02-25 12:59:26 +0100246
247 init_completion(&rdc321x_wdt_device.stop);
248 rdc321x_wdt_device.queue = 0;
249
250 clear_bit(0, &rdc321x_wdt_device.inuse);
251
Kees Cooke99e88a2017-10-16 14:43:17 -0700252 timer_setup(&rdc321x_wdt_device.timer, rdc321x_wdt_trigger, 0);
Florian Fainellib3e8f2c2008-02-25 12:59:26 +0100253
254 rdc321x_wdt_device.default_ticks = ticks;
255
Florian Fainelli842102f2010-03-22 22:56:24 -0400256 dev_info(&pdev->dev, "watchdog init success\n");
Florian Fainellib3e8f2c2008-02-25 12:59:26 +0100257
258 return 0;
259}
260
Bill Pemberton4b12b892012-11-19 13:26:24 -0500261static int rdc321x_wdt_remove(struct platform_device *pdev)
Florian Fainellib3e8f2c2008-02-25 12:59:26 +0100262{
263 if (rdc321x_wdt_device.queue) {
264 rdc321x_wdt_device.queue = 0;
265 wait_for_completion(&rdc321x_wdt_device.stop);
266 }
267
268 misc_deregister(&rdc321x_wdt_misc);
269
270 return 0;
271}
272
273static struct platform_driver rdc321x_wdt_driver = {
274 .probe = rdc321x_wdt_probe,
Bill Pemberton82268712012-11-19 13:21:12 -0500275 .remove = rdc321x_wdt_remove,
Florian Fainellib3e8f2c2008-02-25 12:59:26 +0100276 .driver = {
Florian Fainellib3e8f2c2008-02-25 12:59:26 +0100277 .name = "rdc321x-wdt",
278 },
279};
280
Axel Linb8ec6112011-11-29 13:56:27 +0800281module_platform_driver(rdc321x_wdt_driver);
Florian Fainellib3e8f2c2008-02-25 12:59:26 +0100282
283MODULE_AUTHOR("Florian Fainelli <florian@openwrt.org>");
284MODULE_DESCRIPTION("RDC321x watchdog driver");
285MODULE_LICENSE("GPL");