blob: 9914a4283cb2a5582e5f1b00426e6ff708ba17ad [file] [log] [blame]
Thomas Gleixner2874c5f2019-05-27 08:55:01 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Andres Salomon9b0fd112009-12-18 13:02:38 -05002/* Watchdog timer for machines with the CS5535/CS5536 companion chip
Jordan Crouse0b360862008-01-21 10:07:00 -07003 *
4 * Copyright (C) 2006-2007, Advanced Micro Devices, Inc.
Andres Salomon9b0fd112009-12-18 13:02:38 -05005 * Copyright (C) 2009 Andres Salomon <dilinger@collabora.co.uk>
Jordan Crouse0b360862008-01-21 10:07:00 -07006 */
7
Joe Perches27c766a2012-02-15 15:06:19 -08008#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
Jordan Crouse0b360862008-01-21 10:07:00 -07009
10#include <linux/module.h>
11#include <linux/moduleparam.h>
12#include <linux/types.h>
13#include <linux/miscdevice.h>
14#include <linux/watchdog.h>
15#include <linux/fs.h>
16#include <linux/platform_device.h>
17#include <linux/reboot.h>
Wim Van Sebroeck089ab072008-07-15 11:46:11 +000018#include <linux/uaccess.h>
Jordan Crouse0b360862008-01-21 10:07:00 -070019
Andres Salomon9b0fd112009-12-18 13:02:38 -050020#include <linux/cs5535.h>
Jordan Crouse0b360862008-01-21 10:07:00 -070021
22#define GEODEWDT_HZ 500
23#define GEODEWDT_SCALE 6
24#define GEODEWDT_MAX_SECONDS 131
25
26#define WDT_FLAGS_OPEN 1
27#define WDT_FLAGS_ORPHAN 2
28
29#define DRV_NAME "geodewdt"
30#define WATCHDOG_NAME "Geode GX/LX WDT"
31#define WATCHDOG_TIMEOUT 60
32
33static int timeout = WATCHDOG_TIMEOUT;
34module_param(timeout, int, 0);
Wim Van Sebroeck143a2e52009-03-18 08:35:09 +000035MODULE_PARM_DESC(timeout,
36 "Watchdog timeout in seconds. 1<= timeout <=131, default="
37 __MODULE_STRING(WATCHDOG_TIMEOUT) ".");
Jordan Crouse0b360862008-01-21 10:07:00 -070038
Wim Van Sebroeck86a1e182012-03-05 16:51:11 +010039static bool nowayout = WATCHDOG_NOWAYOUT;
40module_param(nowayout, bool, 0);
Wim Van Sebroeck143a2e52009-03-18 08:35:09 +000041MODULE_PARM_DESC(nowayout,
42 "Watchdog cannot be stopped once started (default="
43 __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
Jordan Crouse0b360862008-01-21 10:07:00 -070044
45static struct platform_device *geodewdt_platform_device;
46static unsigned long wdt_flags;
Andres Salomon9b0fd112009-12-18 13:02:38 -050047static struct cs5535_mfgpt_timer *wdt_timer;
Jordan Crouse0b360862008-01-21 10:07:00 -070048static int safe_close;
49
50static void geodewdt_ping(void)
51{
52 /* Stop the counter */
Andres Salomon9b0fd112009-12-18 13:02:38 -050053 cs5535_mfgpt_write(wdt_timer, MFGPT_REG_SETUP, 0);
Jordan Crouse0b360862008-01-21 10:07:00 -070054
55 /* Reset the counter */
Andres Salomon9b0fd112009-12-18 13:02:38 -050056 cs5535_mfgpt_write(wdt_timer, MFGPT_REG_COUNTER, 0);
Jordan Crouse0b360862008-01-21 10:07:00 -070057
58 /* Enable the counter */
Andres Salomon9b0fd112009-12-18 13:02:38 -050059 cs5535_mfgpt_write(wdt_timer, MFGPT_REG_SETUP, MFGPT_SETUP_CNTEN);
Jordan Crouse0b360862008-01-21 10:07:00 -070060}
61
62static void geodewdt_disable(void)
63{
Andres Salomon9b0fd112009-12-18 13:02:38 -050064 cs5535_mfgpt_write(wdt_timer, MFGPT_REG_SETUP, 0);
65 cs5535_mfgpt_write(wdt_timer, MFGPT_REG_COUNTER, 0);
Jordan Crouse0b360862008-01-21 10:07:00 -070066}
67
68static int geodewdt_set_heartbeat(int val)
69{
70 if (val < 1 || val > GEODEWDT_MAX_SECONDS)
71 return -EINVAL;
72
Andres Salomon9b0fd112009-12-18 13:02:38 -050073 cs5535_mfgpt_write(wdt_timer, MFGPT_REG_SETUP, 0);
74 cs5535_mfgpt_write(wdt_timer, MFGPT_REG_CMP2, val * GEODEWDT_HZ);
75 cs5535_mfgpt_write(wdt_timer, MFGPT_REG_COUNTER, 0);
76 cs5535_mfgpt_write(wdt_timer, MFGPT_REG_SETUP, MFGPT_SETUP_CNTEN);
Jordan Crouse0b360862008-01-21 10:07:00 -070077
78 timeout = val;
79 return 0;
80}
81
Wim Van Sebroeck7944d3a2008-08-06 20:19:41 +000082static int geodewdt_open(struct inode *inode, struct file *file)
Jordan Crouse0b360862008-01-21 10:07:00 -070083{
Wim Van Sebroeck7944d3a2008-08-06 20:19:41 +000084 if (test_and_set_bit(WDT_FLAGS_OPEN, &wdt_flags))
85 return -EBUSY;
Jordan Crouse0b360862008-01-21 10:07:00 -070086
Wim Van Sebroeck7944d3a2008-08-06 20:19:41 +000087 if (!test_and_clear_bit(WDT_FLAGS_ORPHAN, &wdt_flags))
88 __module_get(THIS_MODULE);
Jordan Crouse0b360862008-01-21 10:07:00 -070089
90 geodewdt_ping();
Kirill Smelkovc5bf68f2019-03-26 23:51:19 +030091 return stream_open(inode, file);
Jordan Crouse0b360862008-01-21 10:07:00 -070092}
93
Wim Van Sebroeck7944d3a2008-08-06 20:19:41 +000094static int geodewdt_release(struct inode *inode, struct file *file)
Jordan Crouse0b360862008-01-21 10:07:00 -070095{
96 if (safe_close) {
97 geodewdt_disable();
98 module_put(THIS_MODULE);
Wim Van Sebroeck7944d3a2008-08-06 20:19:41 +000099 } else {
Joe Perches27c766a2012-02-15 15:06:19 -0800100 pr_crit("Unexpected close - watchdog is not stopping\n");
Jordan Crouse0b360862008-01-21 10:07:00 -0700101 geodewdt_ping();
102
103 set_bit(WDT_FLAGS_ORPHAN, &wdt_flags);
104 }
105
106 clear_bit(WDT_FLAGS_OPEN, &wdt_flags);
107 safe_close = 0;
108 return 0;
109}
110
Wim Van Sebroeck7944d3a2008-08-06 20:19:41 +0000111static ssize_t geodewdt_write(struct file *file, const char __user *data,
112 size_t len, loff_t *ppos)
Jordan Crouse0b360862008-01-21 10:07:00 -0700113{
Wim Van Sebroeck7944d3a2008-08-06 20:19:41 +0000114 if (len) {
Jordan Crouse0b360862008-01-21 10:07:00 -0700115 if (!nowayout) {
116 size_t i;
117 safe_close = 0;
118
119 for (i = 0; i != len; i++) {
120 char c;
121
122 if (get_user(c, data + i))
123 return -EFAULT;
124
125 if (c == 'V')
126 safe_close = 1;
127 }
128 }
129
130 geodewdt_ping();
131 }
132 return len;
133}
134
Wim Van Sebroeck7275fc82008-09-18 12:26:15 +0000135static long geodewdt_ioctl(struct file *file, unsigned int cmd,
136 unsigned long arg)
Jordan Crouse0b360862008-01-21 10:07:00 -0700137{
138 void __user *argp = (void __user *)arg;
139 int __user *p = argp;
140 int interval;
141
Wim Van Sebroeck42747d72009-12-26 18:55:22 +0000142 static const struct watchdog_info ident = {
Jordan Crouse0b360862008-01-21 10:07:00 -0700143 .options = WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING
144 | WDIOF_MAGICCLOSE,
145 .firmware_version = 1,
146 .identity = WATCHDOG_NAME,
Wim Van Sebroeck7944d3a2008-08-06 20:19:41 +0000147 };
Jordan Crouse0b360862008-01-21 10:07:00 -0700148
Wim Van Sebroeck5eb82492008-07-17 18:08:47 +0000149 switch (cmd) {
Jordan Crouse0b360862008-01-21 10:07:00 -0700150 case WDIOC_GETSUPPORT:
151 return copy_to_user(argp, &ident,
152 sizeof(ident)) ? -EFAULT : 0;
153 break;
154
155 case WDIOC_GETSTATUS:
156 case WDIOC_GETBOOTSTATUS:
157 return put_user(0, p);
158
Jordan Crouse0b360862008-01-21 10:07:00 -0700159 case WDIOC_SETOPTIONS:
160 {
161 int options, ret = -EINVAL;
162
163 if (get_user(options, p))
164 return -EFAULT;
165
166 if (options & WDIOS_DISABLECARD) {
167 geodewdt_disable();
168 ret = 0;
169 }
170
171 if (options & WDIOS_ENABLECARD) {
172 geodewdt_ping();
173 ret = 0;
174 }
175
176 return ret;
177 }
Wim Van Sebroeck0c060902008-07-18 11:41:17 +0000178 case WDIOC_KEEPALIVE:
179 geodewdt_ping();
180 return 0;
181
182 case WDIOC_SETTIMEOUT:
183 if (get_user(interval, p))
184 return -EFAULT;
185
186 if (geodewdt_set_heartbeat(interval))
187 return -EINVAL;
188 /* Fall through */
189 case WDIOC_GETTIMEOUT:
190 return put_user(timeout, p);
191
Jordan Crouse0b360862008-01-21 10:07:00 -0700192 default:
193 return -ENOTTY;
194 }
195
196 return 0;
197}
198
199static const struct file_operations geodewdt_fops = {
Wim Van Sebroeck7944d3a2008-08-06 20:19:41 +0000200 .owner = THIS_MODULE,
201 .llseek = no_llseek,
202 .write = geodewdt_write,
Wim Van Sebroeck7275fc82008-09-18 12:26:15 +0000203 .unlocked_ioctl = geodewdt_ioctl,
Arnd Bergmannb6dfb242019-06-03 14:23:09 +0200204 .compat_ioctl = compat_ptr_ioctl,
Wim Van Sebroeck7944d3a2008-08-06 20:19:41 +0000205 .open = geodewdt_open,
206 .release = geodewdt_release,
Jordan Crouse0b360862008-01-21 10:07:00 -0700207};
208
209static struct miscdevice geodewdt_miscdev = {
210 .minor = WATCHDOG_MINOR,
211 .name = "watchdog",
Wim Van Sebroeck7944d3a2008-08-06 20:19:41 +0000212 .fops = &geodewdt_fops,
Jordan Crouse0b360862008-01-21 10:07:00 -0700213};
214
Jean Delvare78411be2014-03-14 13:16:47 +0100215static int __init geodewdt_probe(struct platform_device *dev)
Jordan Crouse0b360862008-01-21 10:07:00 -0700216{
Andres Salomon9b0fd112009-12-18 13:02:38 -0500217 int ret;
Jordan Crouse0b360862008-01-21 10:07:00 -0700218
Andres Salomon9b0fd112009-12-18 13:02:38 -0500219 wdt_timer = cs5535_mfgpt_alloc_timer(MFGPT_TIMER_ANY, MFGPT_DOMAIN_WORKING);
220 if (!wdt_timer) {
Joe Perches27c766a2012-02-15 15:06:19 -0800221 pr_err("No timers were available\n");
Jordan Crouse0b360862008-01-21 10:07:00 -0700222 return -ENODEV;
223 }
224
Jordan Crouse0b360862008-01-21 10:07:00 -0700225 /* Set up the timer */
226
Andres Salomon9b0fd112009-12-18 13:02:38 -0500227 cs5535_mfgpt_write(wdt_timer, MFGPT_REG_SETUP,
Jordan Crouse0b360862008-01-21 10:07:00 -0700228 GEODEWDT_SCALE | (3 << 8));
229
230 /* Set up comparator 2 to reset when the event fires */
Andres Salomon9b0fd112009-12-18 13:02:38 -0500231 cs5535_mfgpt_toggle_event(wdt_timer, MFGPT_CMP2, MFGPT_EVENT_RESET, 1);
Jordan Crouse0b360862008-01-21 10:07:00 -0700232
233 /* Set up the initial timeout */
234
Andres Salomon9b0fd112009-12-18 13:02:38 -0500235 cs5535_mfgpt_write(wdt_timer, MFGPT_REG_CMP2,
Jordan Crouse0b360862008-01-21 10:07:00 -0700236 timeout * GEODEWDT_HZ);
237
238 ret = misc_register(&geodewdt_miscdev);
239
240 return ret;
241}
242
Bill Pemberton4b12b892012-11-19 13:26:24 -0500243static int geodewdt_remove(struct platform_device *dev)
Jordan Crouse0b360862008-01-21 10:07:00 -0700244{
245 misc_deregister(&geodewdt_miscdev);
246 return 0;
247}
248
Wim Van Sebroeck7944d3a2008-08-06 20:19:41 +0000249static void geodewdt_shutdown(struct platform_device *dev)
Jordan Crouse0b360862008-01-21 10:07:00 -0700250{
251 geodewdt_disable();
252}
253
254static struct platform_driver geodewdt_driver = {
Bill Pemberton82268712012-11-19 13:21:12 -0500255 .remove = geodewdt_remove,
Jordan Crouse0b360862008-01-21 10:07:00 -0700256 .shutdown = geodewdt_shutdown,
257 .driver = {
Jordan Crouse0b360862008-01-21 10:07:00 -0700258 .name = DRV_NAME,
259 },
260};
261
Wim Van Sebroeck7944d3a2008-08-06 20:19:41 +0000262static int __init geodewdt_init(void)
Jordan Crouse0b360862008-01-21 10:07:00 -0700263{
264 int ret;
265
Wim Van Sebroeck143a2e52009-03-18 08:35:09 +0000266 geodewdt_platform_device = platform_device_register_simple(DRV_NAME,
267 -1, NULL, 0);
Jean Delvare78411be2014-03-14 13:16:47 +0100268 if (IS_ERR(geodewdt_platform_device))
269 return PTR_ERR(geodewdt_platform_device);
270
271 ret = platform_driver_probe(&geodewdt_driver, geodewdt_probe);
272 if (ret)
Jordan Crouse0b360862008-01-21 10:07:00 -0700273 goto err;
Jordan Crouse0b360862008-01-21 10:07:00 -0700274
275 return 0;
276err:
Jean Delvare78411be2014-03-14 13:16:47 +0100277 platform_device_unregister(geodewdt_platform_device);
Jordan Crouse0b360862008-01-21 10:07:00 -0700278 return ret;
279}
280
Wim Van Sebroeck7944d3a2008-08-06 20:19:41 +0000281static void __exit geodewdt_exit(void)
Jordan Crouse0b360862008-01-21 10:07:00 -0700282{
283 platform_device_unregister(geodewdt_platform_device);
284 platform_driver_unregister(&geodewdt_driver);
285}
286
287module_init(geodewdt_init);
288module_exit(geodewdt_exit);
289
290MODULE_AUTHOR("Advanced Micro Devices, Inc");
291MODULE_DESCRIPTION("Geode GX/LX Watchdog Driver");
292MODULE_LICENSE("GPL");