Merge branch 'for-5.4' of https://git.kernel.org/pub/scm/linux/kernel/git/broonie...
[linux-2.6-block.git] / drivers / watchdog / geodewdt.c
CommitLineData
2874c5fd 1// SPDX-License-Identifier: GPL-2.0-or-later
9b0fd114 2/* Watchdog timer for machines with the CS5535/CS5536 companion chip
0b36086b
JC
3 *
4 * Copyright (C) 2006-2007, Advanced Micro Devices, Inc.
9b0fd114 5 * Copyright (C) 2009 Andres Salomon <dilinger@collabora.co.uk>
0b36086b
JC
6 */
7
27c766aa 8#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
0b36086b
JC
9
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>
089ab079 18#include <linux/uaccess.h>
0b36086b 19
9b0fd114 20#include <linux/cs5535.h>
0b36086b
JC
21
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);
143a2e54
WVS
35MODULE_PARM_DESC(timeout,
36 "Watchdog timeout in seconds. 1<= timeout <=131, default="
37 __MODULE_STRING(WATCHDOG_TIMEOUT) ".");
0b36086b 38
86a1e189
WVS
39static bool nowayout = WATCHDOG_NOWAYOUT;
40module_param(nowayout, bool, 0);
143a2e54
WVS
41MODULE_PARM_DESC(nowayout,
42 "Watchdog cannot be stopped once started (default="
43 __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
0b36086b
JC
44
45static struct platform_device *geodewdt_platform_device;
46static unsigned long wdt_flags;
9b0fd114 47static struct cs5535_mfgpt_timer *wdt_timer;
0b36086b
JC
48static int safe_close;
49
50static void geodewdt_ping(void)
51{
52 /* Stop the counter */
9b0fd114 53 cs5535_mfgpt_write(wdt_timer, MFGPT_REG_SETUP, 0);
0b36086b
JC
54
55 /* Reset the counter */
9b0fd114 56 cs5535_mfgpt_write(wdt_timer, MFGPT_REG_COUNTER, 0);
0b36086b
JC
57
58 /* Enable the counter */
9b0fd114 59 cs5535_mfgpt_write(wdt_timer, MFGPT_REG_SETUP, MFGPT_SETUP_CNTEN);
0b36086b
JC
60}
61
62static void geodewdt_disable(void)
63{
9b0fd114
AS
64 cs5535_mfgpt_write(wdt_timer, MFGPT_REG_SETUP, 0);
65 cs5535_mfgpt_write(wdt_timer, MFGPT_REG_COUNTER, 0);
0b36086b
JC
66}
67
68static int geodewdt_set_heartbeat(int val)
69{
70 if (val < 1 || val > GEODEWDT_MAX_SECONDS)
71 return -EINVAL;
72
9b0fd114
AS
73 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);
0b36086b
JC
77
78 timeout = val;
79 return 0;
80}
81
7944d3a5 82static int geodewdt_open(struct inode *inode, struct file *file)
0b36086b 83{
7944d3a5
WVS
84 if (test_and_set_bit(WDT_FLAGS_OPEN, &wdt_flags))
85 return -EBUSY;
0b36086b 86
7944d3a5
WVS
87 if (!test_and_clear_bit(WDT_FLAGS_ORPHAN, &wdt_flags))
88 __module_get(THIS_MODULE);
0b36086b
JC
89
90 geodewdt_ping();
c5bf68fe 91 return stream_open(inode, file);
0b36086b
JC
92}
93
7944d3a5 94static int geodewdt_release(struct inode *inode, struct file *file)
0b36086b
JC
95{
96 if (safe_close) {
97 geodewdt_disable();
98 module_put(THIS_MODULE);
7944d3a5 99 } else {
27c766aa 100 pr_crit("Unexpected close - watchdog is not stopping\n");
0b36086b
JC
101 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
7944d3a5
WVS
111static ssize_t geodewdt_write(struct file *file, const char __user *data,
112 size_t len, loff_t *ppos)
0b36086b 113{
7944d3a5 114 if (len) {
0b36086b
JC
115 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
7275fc8c
WVS
135static long geodewdt_ioctl(struct file *file, unsigned int cmd,
136 unsigned long arg)
0b36086b
JC
137{
138 void __user *argp = (void __user *)arg;
139 int __user *p = argp;
140 int interval;
141
42747d71 142 static const struct watchdog_info ident = {
0b36086b
JC
143 .options = WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING
144 | WDIOF_MAGICCLOSE,
145 .firmware_version = 1,
146 .identity = WATCHDOG_NAME,
7944d3a5 147 };
0b36086b 148
5eb82498 149 switch (cmd) {
0b36086b
JC
150 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
0b36086b
JC
159 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 }
0c06090c
WVS
178 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
0b36086b
JC
192 default:
193 return -ENOTTY;
194 }
195
196 return 0;
197}
198
199static const struct file_operations geodewdt_fops = {
7944d3a5
WVS
200 .owner = THIS_MODULE,
201 .llseek = no_llseek,
202 .write = geodewdt_write,
7275fc8c 203 .unlocked_ioctl = geodewdt_ioctl,
7944d3a5
WVS
204 .open = geodewdt_open,
205 .release = geodewdt_release,
0b36086b
JC
206};
207
208static struct miscdevice geodewdt_miscdev = {
209 .minor = WATCHDOG_MINOR,
210 .name = "watchdog",
7944d3a5 211 .fops = &geodewdt_fops,
0b36086b
JC
212};
213
78411be4 214static int __init geodewdt_probe(struct platform_device *dev)
0b36086b 215{
9b0fd114 216 int ret;
0b36086b 217
9b0fd114
AS
218 wdt_timer = cs5535_mfgpt_alloc_timer(MFGPT_TIMER_ANY, MFGPT_DOMAIN_WORKING);
219 if (!wdt_timer) {
27c766aa 220 pr_err("No timers were available\n");
0b36086b
JC
221 return -ENODEV;
222 }
223
0b36086b
JC
224 /* Set up the timer */
225
9b0fd114 226 cs5535_mfgpt_write(wdt_timer, MFGPT_REG_SETUP,
0b36086b
JC
227 GEODEWDT_SCALE | (3 << 8));
228
229 /* Set up comparator 2 to reset when the event fires */
9b0fd114 230 cs5535_mfgpt_toggle_event(wdt_timer, MFGPT_CMP2, MFGPT_EVENT_RESET, 1);
0b36086b
JC
231
232 /* Set up the initial timeout */
233
9b0fd114 234 cs5535_mfgpt_write(wdt_timer, MFGPT_REG_CMP2,
0b36086b
JC
235 timeout * GEODEWDT_HZ);
236
237 ret = misc_register(&geodewdt_miscdev);
238
239 return ret;
240}
241
4b12b896 242static int geodewdt_remove(struct platform_device *dev)
0b36086b
JC
243{
244 misc_deregister(&geodewdt_miscdev);
245 return 0;
246}
247
7944d3a5 248static void geodewdt_shutdown(struct platform_device *dev)
0b36086b
JC
249{
250 geodewdt_disable();
251}
252
253static struct platform_driver geodewdt_driver = {
82268714 254 .remove = geodewdt_remove,
0b36086b
JC
255 .shutdown = geodewdt_shutdown,
256 .driver = {
0b36086b
JC
257 .name = DRV_NAME,
258 },
259};
260
7944d3a5 261static int __init geodewdt_init(void)
0b36086b
JC
262{
263 int ret;
264
143a2e54
WVS
265 geodewdt_platform_device = platform_device_register_simple(DRV_NAME,
266 -1, NULL, 0);
78411be4
JD
267 if (IS_ERR(geodewdt_platform_device))
268 return PTR_ERR(geodewdt_platform_device);
269
270 ret = platform_driver_probe(&geodewdt_driver, geodewdt_probe);
271 if (ret)
0b36086b 272 goto err;
0b36086b
JC
273
274 return 0;
275err:
78411be4 276 platform_device_unregister(geodewdt_platform_device);
0b36086b
JC
277 return ret;
278}
279
7944d3a5 280static void __exit geodewdt_exit(void)
0b36086b
JC
281{
282 platform_device_unregister(geodewdt_platform_device);
283 platform_driver_unregister(&geodewdt_driver);
284}
285
286module_init(geodewdt_init);
287module_exit(geodewdt_exit);
288
289MODULE_AUTHOR("Advanced Micro Devices, Inc");
290MODULE_DESCRIPTION("Geode GX/LX Watchdog Driver");
291MODULE_LICENSE("GPL");