1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /* Watchdog timer for machines with the CS5535/CS5536 companion chip
4 * Copyright (C) 2006-2007, Advanced Micro Devices, Inc.
5 * Copyright (C) 2009 Andres Salomon <dilinger@collabora.co.uk>
8 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
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>
16 #include <linux/platform_device.h>
17 #include <linux/reboot.h>
18 #include <linux/uaccess.h>
20 #include <linux/cs5535.h>
22 #define GEODEWDT_HZ 500
23 #define GEODEWDT_SCALE 6
24 #define GEODEWDT_MAX_SECONDS 131
26 #define WDT_FLAGS_OPEN 1
27 #define WDT_FLAGS_ORPHAN 2
29 #define DRV_NAME "geodewdt"
30 #define WATCHDOG_NAME "Geode GX/LX WDT"
31 #define WATCHDOG_TIMEOUT 60
33 static int timeout
= WATCHDOG_TIMEOUT
;
34 module_param(timeout
, int, 0);
35 MODULE_PARM_DESC(timeout
,
36 "Watchdog timeout in seconds. 1<= timeout <=131, default="
37 __MODULE_STRING(WATCHDOG_TIMEOUT
) ".");
39 static bool nowayout
= WATCHDOG_NOWAYOUT
;
40 module_param(nowayout
, bool, 0);
41 MODULE_PARM_DESC(nowayout
,
42 "Watchdog cannot be stopped once started (default="
43 __MODULE_STRING(WATCHDOG_NOWAYOUT
) ")");
45 static struct platform_device
*geodewdt_platform_device
;
46 static unsigned long wdt_flags
;
47 static struct cs5535_mfgpt_timer
*wdt_timer
;
48 static int safe_close
;
50 static void geodewdt_ping(void)
52 /* Stop the counter */
53 cs5535_mfgpt_write(wdt_timer
, MFGPT_REG_SETUP
, 0);
55 /* Reset the counter */
56 cs5535_mfgpt_write(wdt_timer
, MFGPT_REG_COUNTER
, 0);
58 /* Enable the counter */
59 cs5535_mfgpt_write(wdt_timer
, MFGPT_REG_SETUP
, MFGPT_SETUP_CNTEN
);
62 static void geodewdt_disable(void)
64 cs5535_mfgpt_write(wdt_timer
, MFGPT_REG_SETUP
, 0);
65 cs5535_mfgpt_write(wdt_timer
, MFGPT_REG_COUNTER
, 0);
68 static int geodewdt_set_heartbeat(int val
)
70 if (val
< 1 || val
> GEODEWDT_MAX_SECONDS
)
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
);
82 static int geodewdt_open(struct inode
*inode
, struct file
*file
)
84 if (test_and_set_bit(WDT_FLAGS_OPEN
, &wdt_flags
))
87 if (!test_and_clear_bit(WDT_FLAGS_ORPHAN
, &wdt_flags
))
88 __module_get(THIS_MODULE
);
91 return stream_open(inode
, file
);
94 static int geodewdt_release(struct inode
*inode
, struct file
*file
)
98 module_put(THIS_MODULE
);
100 pr_crit("Unexpected close - watchdog is not stopping\n");
103 set_bit(WDT_FLAGS_ORPHAN
, &wdt_flags
);
106 clear_bit(WDT_FLAGS_OPEN
, &wdt_flags
);
111 static ssize_t
geodewdt_write(struct file
*file
, const char __user
*data
,
112 size_t len
, loff_t
*ppos
)
119 for (i
= 0; i
!= len
; i
++) {
122 if (get_user(c
, data
+ i
))
135 static long geodewdt_ioctl(struct file
*file
, unsigned int cmd
,
138 void __user
*argp
= (void __user
*)arg
;
139 int __user
*p
= argp
;
142 static const struct watchdog_info ident
= {
143 .options
= WDIOF_SETTIMEOUT
| WDIOF_KEEPALIVEPING
145 .firmware_version
= 1,
146 .identity
= WATCHDOG_NAME
,
150 case WDIOC_GETSUPPORT
:
151 return copy_to_user(argp
, &ident
,
152 sizeof(ident
)) ? -EFAULT
: 0;
155 case WDIOC_GETSTATUS
:
156 case WDIOC_GETBOOTSTATUS
:
157 return put_user(0, p
);
159 case WDIOC_SETOPTIONS
:
161 int options
, ret
= -EINVAL
;
163 if (get_user(options
, p
))
166 if (options
& WDIOS_DISABLECARD
) {
171 if (options
& WDIOS_ENABLECARD
) {
178 case WDIOC_KEEPALIVE
:
182 case WDIOC_SETTIMEOUT
:
183 if (get_user(interval
, p
))
186 if (geodewdt_set_heartbeat(interval
))
189 case WDIOC_GETTIMEOUT
:
190 return put_user(timeout
, p
);
199 static const struct file_operations geodewdt_fops
= {
200 .owner
= THIS_MODULE
,
202 .write
= geodewdt_write
,
203 .unlocked_ioctl
= geodewdt_ioctl
,
204 .compat_ioctl
= compat_ptr_ioctl
,
205 .open
= geodewdt_open
,
206 .release
= geodewdt_release
,
209 static struct miscdevice geodewdt_miscdev
= {
210 .minor
= WATCHDOG_MINOR
,
212 .fops
= &geodewdt_fops
,
215 static int __init
geodewdt_probe(struct platform_device
*dev
)
219 wdt_timer
= cs5535_mfgpt_alloc_timer(MFGPT_TIMER_ANY
, MFGPT_DOMAIN_WORKING
);
221 pr_err("No timers were available\n");
225 /* Set up the timer */
227 cs5535_mfgpt_write(wdt_timer
, MFGPT_REG_SETUP
,
228 GEODEWDT_SCALE
| (3 << 8));
230 /* Set up comparator 2 to reset when the event fires */
231 cs5535_mfgpt_toggle_event(wdt_timer
, MFGPT_CMP2
, MFGPT_EVENT_RESET
, 1);
233 /* Set up the initial timeout */
235 cs5535_mfgpt_write(wdt_timer
, MFGPT_REG_CMP2
,
236 timeout
* GEODEWDT_HZ
);
238 ret
= misc_register(&geodewdt_miscdev
);
243 static int geodewdt_remove(struct platform_device
*dev
)
245 misc_deregister(&geodewdt_miscdev
);
249 static void geodewdt_shutdown(struct platform_device
*dev
)
254 static struct platform_driver geodewdt_driver
= {
255 .remove
= geodewdt_remove
,
256 .shutdown
= geodewdt_shutdown
,
262 static int __init
geodewdt_init(void)
266 geodewdt_platform_device
= platform_device_register_simple(DRV_NAME
,
268 if (IS_ERR(geodewdt_platform_device
))
269 return PTR_ERR(geodewdt_platform_device
);
271 ret
= platform_driver_probe(&geodewdt_driver
, geodewdt_probe
);
277 platform_device_unregister(geodewdt_platform_device
);
281 static void __exit
geodewdt_exit(void)
283 platform_device_unregister(geodewdt_platform_device
);
284 platform_driver_unregister(&geodewdt_driver
);
287 module_init(geodewdt_init
);
288 module_exit(geodewdt_exit
);
290 MODULE_AUTHOR("Advanced Micro Devices, Inc");
291 MODULE_DESCRIPTION("Geode GX/LX Watchdog Driver");
292 MODULE_LICENSE("GPL");