Linux Audio

Check our new training course

Loading...
v6.8
  1// SPDX-License-Identifier: GPL-2.0-or-later
  2/* Watchdog timer for machines with the CS5535/CS5536 companion chip
  3 *
  4 * Copyright (C) 2006-2007, Advanced Micro Devices, Inc.
  5 * Copyright (C) 2009  Andres Salomon <dilinger@collabora.co.uk>
 
 
 
 
 
  6 */
  7
  8#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  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>
 18#include <linux/uaccess.h>
 19
 20#include <linux/cs5535.h>
 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);
 35MODULE_PARM_DESC(timeout,
 36	"Watchdog timeout in seconds. 1<= timeout <=131, default="
 37				__MODULE_STRING(WATCHDOG_TIMEOUT) ".");
 38
 39static bool nowayout = WATCHDOG_NOWAYOUT;
 40module_param(nowayout, bool, 0);
 41MODULE_PARM_DESC(nowayout,
 42	"Watchdog cannot be stopped once started (default="
 43				__MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
 44
 45static struct platform_device *geodewdt_platform_device;
 46static unsigned long wdt_flags;
 47static struct cs5535_mfgpt_timer *wdt_timer;
 48static int safe_close;
 49
 50static void geodewdt_ping(void)
 51{
 52	/* Stop the counter */
 53	cs5535_mfgpt_write(wdt_timer, MFGPT_REG_SETUP, 0);
 54
 55	/* Reset the counter */
 56	cs5535_mfgpt_write(wdt_timer, MFGPT_REG_COUNTER, 0);
 57
 58	/* Enable the counter */
 59	cs5535_mfgpt_write(wdt_timer, MFGPT_REG_SETUP, MFGPT_SETUP_CNTEN);
 60}
 61
 62static void geodewdt_disable(void)
 63{
 64	cs5535_mfgpt_write(wdt_timer, MFGPT_REG_SETUP, 0);
 65	cs5535_mfgpt_write(wdt_timer, MFGPT_REG_COUNTER, 0);
 66}
 67
 68static int geodewdt_set_heartbeat(int val)
 69{
 70	if (val < 1 || val > GEODEWDT_MAX_SECONDS)
 71		return -EINVAL;
 72
 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);
 77
 78	timeout = val;
 79	return 0;
 80}
 81
 82static int geodewdt_open(struct inode *inode, struct file *file)
 83{
 84	if (test_and_set_bit(WDT_FLAGS_OPEN, &wdt_flags))
 85		return -EBUSY;
 86
 87	if (!test_and_clear_bit(WDT_FLAGS_ORPHAN, &wdt_flags))
 88		__module_get(THIS_MODULE);
 89
 90	geodewdt_ping();
 91	return stream_open(inode, file);
 92}
 93
 94static int geodewdt_release(struct inode *inode, struct file *file)
 95{
 96	if (safe_close) {
 97		geodewdt_disable();
 98		module_put(THIS_MODULE);
 99	} else {
100		pr_crit("Unexpected close - watchdog is not stopping\n");
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
111static ssize_t geodewdt_write(struct file *file, const char __user *data,
112				size_t len, loff_t *ppos)
113{
114	if (len) {
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
135static long geodewdt_ioctl(struct file *file, unsigned int cmd,
136				unsigned long arg)
137{
138	void __user *argp = (void __user *)arg;
139	int __user *p = argp;
140	int interval;
141
142	static const struct watchdog_info ident = {
143		.options = WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING
144		| WDIOF_MAGICCLOSE,
145		.firmware_version =     1,
146		.identity =             WATCHDOG_NAME,
147	};
148
149	switch (cmd) {
150	case WDIOC_GETSUPPORT:
151		return copy_to_user(argp, &ident,
152				    sizeof(ident)) ? -EFAULT : 0;
 
 
153	case WDIOC_GETSTATUS:
154	case WDIOC_GETBOOTSTATUS:
155		return put_user(0, p);
156
157	case WDIOC_SETOPTIONS:
158	{
159		int options, ret = -EINVAL;
160
161		if (get_user(options, p))
162			return -EFAULT;
163
164		if (options & WDIOS_DISABLECARD) {
165			geodewdt_disable();
166			ret = 0;
167		}
168
169		if (options & WDIOS_ENABLECARD) {
170			geodewdt_ping();
171			ret = 0;
172		}
173
174		return ret;
175	}
176	case WDIOC_KEEPALIVE:
177		geodewdt_ping();
178		return 0;
179
180	case WDIOC_SETTIMEOUT:
181		if (get_user(interval, p))
182			return -EFAULT;
183
184		if (geodewdt_set_heartbeat(interval))
185			return -EINVAL;
186		fallthrough;
187	case WDIOC_GETTIMEOUT:
188		return put_user(timeout, p);
189
190	default:
191		return -ENOTTY;
192	}
193
194	return 0;
195}
196
197static const struct file_operations geodewdt_fops = {
198	.owner          = THIS_MODULE,
199	.llseek         = no_llseek,
200	.write          = geodewdt_write,
201	.unlocked_ioctl = geodewdt_ioctl,
202	.compat_ioctl	= compat_ptr_ioctl,
203	.open           = geodewdt_open,
204	.release        = geodewdt_release,
205};
206
207static struct miscdevice geodewdt_miscdev = {
208	.minor = WATCHDOG_MINOR,
209	.name = "watchdog",
210	.fops = &geodewdt_fops,
211};
212
213static int __init geodewdt_probe(struct platform_device *dev)
214{
215	int ret;
216
217	wdt_timer = cs5535_mfgpt_alloc_timer(MFGPT_TIMER_ANY, MFGPT_DOMAIN_WORKING);
218	if (!wdt_timer) {
219		pr_err("No timers were available\n");
220		return -ENODEV;
221	}
222
223	/* Set up the timer */
224
225	cs5535_mfgpt_write(wdt_timer, MFGPT_REG_SETUP,
226			  GEODEWDT_SCALE | (3 << 8));
227
228	/* Set up comparator 2 to reset when the event fires */
229	cs5535_mfgpt_toggle_event(wdt_timer, MFGPT_CMP2, MFGPT_EVENT_RESET, 1);
230
231	/* Set up the initial timeout */
232
233	cs5535_mfgpt_write(wdt_timer, MFGPT_REG_CMP2,
234		timeout * GEODEWDT_HZ);
235
236	ret = misc_register(&geodewdt_miscdev);
237
238	return ret;
239}
240
241static void geodewdt_remove(struct platform_device *dev)
242{
243	misc_deregister(&geodewdt_miscdev);
 
244}
245
246static void geodewdt_shutdown(struct platform_device *dev)
247{
248	geodewdt_disable();
249}
250
251static struct platform_driver geodewdt_driver = {
252	.remove_new	= geodewdt_remove,
 
253	.shutdown	= geodewdt_shutdown,
254	.driver		= {
 
255		.name	= DRV_NAME,
256	},
257};
258
259static int __init geodewdt_init(void)
260{
261	int ret;
262
 
 
 
 
263	geodewdt_platform_device = platform_device_register_simple(DRV_NAME,
264								-1, NULL, 0);
265	if (IS_ERR(geodewdt_platform_device))
266		return PTR_ERR(geodewdt_platform_device);
267
268	ret = platform_driver_probe(&geodewdt_driver, geodewdt_probe);
269	if (ret)
270		goto err;
 
271
272	return 0;
273err:
274	platform_device_unregister(geodewdt_platform_device);
275	return ret;
276}
277
278static void __exit geodewdt_exit(void)
279{
280	platform_device_unregister(geodewdt_platform_device);
281	platform_driver_unregister(&geodewdt_driver);
282}
283
284module_init(geodewdt_init);
285module_exit(geodewdt_exit);
286
287MODULE_AUTHOR("Advanced Micro Devices, Inc");
288MODULE_DESCRIPTION("Geode GX/LX Watchdog Driver");
289MODULE_LICENSE("GPL");
v3.5.6
 
  1/* Watchdog timer for machines with the CS5535/CS5536 companion chip
  2 *
  3 * Copyright (C) 2006-2007, Advanced Micro Devices, Inc.
  4 * Copyright (C) 2009  Andres Salomon <dilinger@collabora.co.uk>
  5 *
  6 * This program is free software; you can redistribute it and/or
  7 * modify it under the terms of the GNU General Public License
  8 * as published by the Free Software Foundation; either version
  9 * 2 of the License, or (at your option) any later version.
 10 */
 11
 12#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
 13
 14#include <linux/module.h>
 15#include <linux/moduleparam.h>
 16#include <linux/types.h>
 17#include <linux/miscdevice.h>
 18#include <linux/watchdog.h>
 19#include <linux/fs.h>
 20#include <linux/platform_device.h>
 21#include <linux/reboot.h>
 22#include <linux/uaccess.h>
 23
 24#include <linux/cs5535.h>
 25
 26#define GEODEWDT_HZ 500
 27#define GEODEWDT_SCALE 6
 28#define GEODEWDT_MAX_SECONDS 131
 29
 30#define WDT_FLAGS_OPEN 1
 31#define WDT_FLAGS_ORPHAN 2
 32
 33#define DRV_NAME "geodewdt"
 34#define WATCHDOG_NAME "Geode GX/LX WDT"
 35#define WATCHDOG_TIMEOUT 60
 36
 37static int timeout = WATCHDOG_TIMEOUT;
 38module_param(timeout, int, 0);
 39MODULE_PARM_DESC(timeout,
 40	"Watchdog timeout in seconds. 1<= timeout <=131, default="
 41				__MODULE_STRING(WATCHDOG_TIMEOUT) ".");
 42
 43static bool nowayout = WATCHDOG_NOWAYOUT;
 44module_param(nowayout, bool, 0);
 45MODULE_PARM_DESC(nowayout,
 46	"Watchdog cannot be stopped once started (default="
 47				__MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
 48
 49static struct platform_device *geodewdt_platform_device;
 50static unsigned long wdt_flags;
 51static struct cs5535_mfgpt_timer *wdt_timer;
 52static int safe_close;
 53
 54static void geodewdt_ping(void)
 55{
 56	/* Stop the counter */
 57	cs5535_mfgpt_write(wdt_timer, MFGPT_REG_SETUP, 0);
 58
 59	/* Reset the counter */
 60	cs5535_mfgpt_write(wdt_timer, MFGPT_REG_COUNTER, 0);
 61
 62	/* Enable the counter */
 63	cs5535_mfgpt_write(wdt_timer, MFGPT_REG_SETUP, MFGPT_SETUP_CNTEN);
 64}
 65
 66static void geodewdt_disable(void)
 67{
 68	cs5535_mfgpt_write(wdt_timer, MFGPT_REG_SETUP, 0);
 69	cs5535_mfgpt_write(wdt_timer, MFGPT_REG_COUNTER, 0);
 70}
 71
 72static int geodewdt_set_heartbeat(int val)
 73{
 74	if (val < 1 || val > GEODEWDT_MAX_SECONDS)
 75		return -EINVAL;
 76
 77	cs5535_mfgpt_write(wdt_timer, MFGPT_REG_SETUP, 0);
 78	cs5535_mfgpt_write(wdt_timer, MFGPT_REG_CMP2, val * GEODEWDT_HZ);
 79	cs5535_mfgpt_write(wdt_timer, MFGPT_REG_COUNTER, 0);
 80	cs5535_mfgpt_write(wdt_timer, MFGPT_REG_SETUP, MFGPT_SETUP_CNTEN);
 81
 82	timeout = val;
 83	return 0;
 84}
 85
 86static int geodewdt_open(struct inode *inode, struct file *file)
 87{
 88	if (test_and_set_bit(WDT_FLAGS_OPEN, &wdt_flags))
 89		return -EBUSY;
 90
 91	if (!test_and_clear_bit(WDT_FLAGS_ORPHAN, &wdt_flags))
 92		__module_get(THIS_MODULE);
 93
 94	geodewdt_ping();
 95	return nonseekable_open(inode, file);
 96}
 97
 98static int geodewdt_release(struct inode *inode, struct file *file)
 99{
100	if (safe_close) {
101		geodewdt_disable();
102		module_put(THIS_MODULE);
103	} else {
104		pr_crit("Unexpected close - watchdog is not stopping\n");
105		geodewdt_ping();
106
107		set_bit(WDT_FLAGS_ORPHAN, &wdt_flags);
108	}
109
110	clear_bit(WDT_FLAGS_OPEN, &wdt_flags);
111	safe_close = 0;
112	return 0;
113}
114
115static ssize_t geodewdt_write(struct file *file, const char __user *data,
116				size_t len, loff_t *ppos)
117{
118	if (len) {
119		if (!nowayout) {
120			size_t i;
121			safe_close = 0;
122
123			for (i = 0; i != len; i++) {
124				char c;
125
126				if (get_user(c, data + i))
127					return -EFAULT;
128
129				if (c == 'V')
130					safe_close = 1;
131			}
132		}
133
134		geodewdt_ping();
135	}
136	return len;
137}
138
139static long geodewdt_ioctl(struct file *file, unsigned int cmd,
140				unsigned long arg)
141{
142	void __user *argp = (void __user *)arg;
143	int __user *p = argp;
144	int interval;
145
146	static const struct watchdog_info ident = {
147		.options = WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING
148		| WDIOF_MAGICCLOSE,
149		.firmware_version =     1,
150		.identity =             WATCHDOG_NAME,
151	};
152
153	switch (cmd) {
154	case WDIOC_GETSUPPORT:
155		return copy_to_user(argp, &ident,
156				    sizeof(ident)) ? -EFAULT : 0;
157		break;
158
159	case WDIOC_GETSTATUS:
160	case WDIOC_GETBOOTSTATUS:
161		return put_user(0, p);
162
163	case WDIOC_SETOPTIONS:
164	{
165		int options, ret = -EINVAL;
166
167		if (get_user(options, p))
168			return -EFAULT;
169
170		if (options & WDIOS_DISABLECARD) {
171			geodewdt_disable();
172			ret = 0;
173		}
174
175		if (options & WDIOS_ENABLECARD) {
176			geodewdt_ping();
177			ret = 0;
178		}
179
180		return ret;
181	}
182	case WDIOC_KEEPALIVE:
183		geodewdt_ping();
184		return 0;
185
186	case WDIOC_SETTIMEOUT:
187		if (get_user(interval, p))
188			return -EFAULT;
189
190		if (geodewdt_set_heartbeat(interval))
191			return -EINVAL;
192	/* Fall through */
193	case WDIOC_GETTIMEOUT:
194		return put_user(timeout, p);
195
196	default:
197		return -ENOTTY;
198	}
199
200	return 0;
201}
202
203static const struct file_operations geodewdt_fops = {
204	.owner          = THIS_MODULE,
205	.llseek         = no_llseek,
206	.write          = geodewdt_write,
207	.unlocked_ioctl = geodewdt_ioctl,
 
208	.open           = geodewdt_open,
209	.release        = geodewdt_release,
210};
211
212static struct miscdevice geodewdt_miscdev = {
213	.minor = WATCHDOG_MINOR,
214	.name = "watchdog",
215	.fops = &geodewdt_fops,
216};
217
218static int __devinit geodewdt_probe(struct platform_device *dev)
219{
220	int ret;
221
222	wdt_timer = cs5535_mfgpt_alloc_timer(MFGPT_TIMER_ANY, MFGPT_DOMAIN_WORKING);
223	if (!wdt_timer) {
224		pr_err("No timers were available\n");
225		return -ENODEV;
226	}
227
228	/* Set up the timer */
229
230	cs5535_mfgpt_write(wdt_timer, MFGPT_REG_SETUP,
231			  GEODEWDT_SCALE | (3 << 8));
232
233	/* Set up comparator 2 to reset when the event fires */
234	cs5535_mfgpt_toggle_event(wdt_timer, MFGPT_CMP2, MFGPT_EVENT_RESET, 1);
235
236	/* Set up the initial timeout */
237
238	cs5535_mfgpt_write(wdt_timer, MFGPT_REG_CMP2,
239		timeout * GEODEWDT_HZ);
240
241	ret = misc_register(&geodewdt_miscdev);
242
243	return ret;
244}
245
246static int __devexit geodewdt_remove(struct platform_device *dev)
247{
248	misc_deregister(&geodewdt_miscdev);
249	return 0;
250}
251
252static void geodewdt_shutdown(struct platform_device *dev)
253{
254	geodewdt_disable();
255}
256
257static struct platform_driver geodewdt_driver = {
258	.probe		= geodewdt_probe,
259	.remove		= __devexit_p(geodewdt_remove),
260	.shutdown	= geodewdt_shutdown,
261	.driver		= {
262		.owner	= THIS_MODULE,
263		.name	= DRV_NAME,
264	},
265};
266
267static int __init geodewdt_init(void)
268{
269	int ret;
270
271	ret = platform_driver_register(&geodewdt_driver);
272	if (ret)
273		return ret;
274
275	geodewdt_platform_device = platform_device_register_simple(DRV_NAME,
276								-1, NULL, 0);
277	if (IS_ERR(geodewdt_platform_device)) {
278		ret = PTR_ERR(geodewdt_platform_device);
 
 
 
279		goto err;
280	}
281
282	return 0;
283err:
284	platform_driver_unregister(&geodewdt_driver);
285	return ret;
286}
287
288static void __exit geodewdt_exit(void)
289{
290	platform_device_unregister(geodewdt_platform_device);
291	platform_driver_unregister(&geodewdt_driver);
292}
293
294module_init(geodewdt_init);
295module_exit(geodewdt_exit);
296
297MODULE_AUTHOR("Advanced Micro Devices, Inc");
298MODULE_DESCRIPTION("Geode GX/LX Watchdog Driver");
299MODULE_LICENSE("GPL");
300MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR);