xref: /linux/drivers/watchdog/at91rm9200_wdt.c (revision d2c9a99135da931377240942d44f3dea104cedb8)
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Watchdog driver for Atmel AT91RM9200 (Thunder)
4  *
5  *  Copyright (C) 2003 SAN People (Pty) Ltd
6  *
7  */
8 
9 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
10 
11 #include <linux/bitops.h>
12 #include <linux/delay.h>
13 #include <linux/errno.h>
14 #include <linux/fs.h>
15 #include <linux/init.h>
16 #include <linux/io.h>
17 #include <linux/kernel.h>
18 #include <linux/mfd/syscon.h>
19 #include <linux/mfd/syscon/atmel-st.h>
20 #include <linux/miscdevice.h>
21 #include <linux/module.h>
22 #include <linux/moduleparam.h>
23 #include <linux/platform_device.h>
24 #include <linux/reboot.h>
25 #include <linux/regmap.h>
26 #include <linux/types.h>
27 #include <linux/watchdog.h>
28 #include <linux/uaccess.h>
29 
30 #define WDT_DEFAULT_TIME	5	/* seconds */
31 #define WDT_MAX_TIME		256	/* seconds */
32 
33 static int wdt_time = WDT_DEFAULT_TIME;
34 static bool nowayout = WATCHDOG_NOWAYOUT;
35 static struct regmap *regmap_st;
36 
37 module_param(wdt_time, int, 0);
38 MODULE_PARM_DESC(wdt_time, "Watchdog time in seconds. (default="
39 				__MODULE_STRING(WDT_DEFAULT_TIME) ")");
40 
41 #ifdef CONFIG_WATCHDOG_NOWAYOUT
42 module_param(nowayout, bool, 0);
43 MODULE_PARM_DESC(nowayout,
44 		"Watchdog cannot be stopped once started (default="
45 				__MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
46 #endif
47 
48 
49 static unsigned long at91wdt_busy;
50 
51 /* ......................................................................... */
52 
at91rm9200_restart(struct notifier_block * this,unsigned long mode,void * cmd)53 static int at91rm9200_restart(struct notifier_block *this,
54 					unsigned long mode, void *cmd)
55 {
56 	/*
57 	 * Perform a hardware reset with the use of the Watchdog timer.
58 	 */
59 	regmap_write(regmap_st, AT91_ST_WDMR,
60 		     AT91_ST_RSTEN | AT91_ST_EXTEN | 1);
61 	regmap_write(regmap_st, AT91_ST_CR, AT91_ST_WDRST);
62 
63 	mdelay(2000);
64 
65 	pr_emerg("Unable to restart system\n");
66 	return NOTIFY_DONE;
67 }
68 
69 static struct notifier_block at91rm9200_restart_nb = {
70 	.notifier_call = at91rm9200_restart,
71 	.priority = 192,
72 };
73 
74 /*
75  * Disable the watchdog.
76  */
at91_wdt_stop(void)77 static inline void at91_wdt_stop(void)
78 {
79 	regmap_write(regmap_st, AT91_ST_WDMR, AT91_ST_EXTEN);
80 }
81 
82 /*
83  * Enable and reset the watchdog.
84  */
at91_wdt_start(void)85 static inline void at91_wdt_start(void)
86 {
87 	regmap_write(regmap_st, AT91_ST_WDMR, AT91_ST_EXTEN | AT91_ST_RSTEN |
88 				(((65536 * wdt_time) >> 8) & AT91_ST_WDV));
89 	regmap_write(regmap_st, AT91_ST_CR, AT91_ST_WDRST);
90 }
91 
92 /*
93  * Reload the watchdog timer.  (ie, pat the watchdog)
94  */
at91_wdt_reload(void)95 static inline void at91_wdt_reload(void)
96 {
97 	regmap_write(regmap_st, AT91_ST_CR, AT91_ST_WDRST);
98 }
99 
100 /* ......................................................................... */
101 
102 /*
103  * Watchdog device is opened, and watchdog starts running.
104  */
at91_wdt_open(struct inode * inode,struct file * file)105 static int at91_wdt_open(struct inode *inode, struct file *file)
106 {
107 	if (test_and_set_bit(0, &at91wdt_busy))
108 		return -EBUSY;
109 
110 	at91_wdt_start();
111 	return stream_open(inode, file);
112 }
113 
114 /*
115  * Close the watchdog device.
116  * If CONFIG_WATCHDOG_NOWAYOUT is NOT defined then the watchdog is also
117  *  disabled.
118  */
at91_wdt_close(struct inode * inode,struct file * file)119 static int at91_wdt_close(struct inode *inode, struct file *file)
120 {
121 	/* Disable the watchdog when file is closed */
122 	if (!nowayout)
123 		at91_wdt_stop();
124 
125 	clear_bit(0, &at91wdt_busy);
126 	return 0;
127 }
128 
129 /*
130  * Change the watchdog time interval.
131  */
at91_wdt_settimeout(int new_time)132 static int at91_wdt_settimeout(int new_time)
133 {
134 	/*
135 	 * All counting occurs at SLOW_CLOCK / 128 = 256 Hz
136 	 *
137 	 * Since WDV is a 16-bit counter, the maximum period is
138 	 * 65536 / 256 = 256 seconds.
139 	 */
140 	if ((new_time <= 0) || (new_time > WDT_MAX_TIME))
141 		return -EINVAL;
142 
143 	/* Set new watchdog time. It will be used when
144 	   at91_wdt_start() is called. */
145 	wdt_time = new_time;
146 	return 0;
147 }
148 
149 static const struct watchdog_info at91_wdt_info = {
150 	.identity	= "at91 watchdog",
151 	.options	= WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING,
152 };
153 
154 /*
155  * Handle commands from user-space.
156  */
at91_wdt_ioctl(struct file * file,unsigned int cmd,unsigned long arg)157 static long at91_wdt_ioctl(struct file *file,
158 					unsigned int cmd, unsigned long arg)
159 {
160 	void __user *argp = (void __user *)arg;
161 	int __user *p = argp;
162 	int new_value;
163 
164 	switch (cmd) {
165 	case WDIOC_GETSUPPORT:
166 		return copy_to_user(argp, &at91_wdt_info,
167 				sizeof(at91_wdt_info)) ? -EFAULT : 0;
168 	case WDIOC_GETSTATUS:
169 	case WDIOC_GETBOOTSTATUS:
170 		return put_user(0, p);
171 	case WDIOC_SETOPTIONS:
172 		if (get_user(new_value, p))
173 			return -EFAULT;
174 		if (new_value & WDIOS_DISABLECARD)
175 			at91_wdt_stop();
176 		if (new_value & WDIOS_ENABLECARD)
177 			at91_wdt_start();
178 		return 0;
179 	case WDIOC_KEEPALIVE:
180 		at91_wdt_reload();	/* pat the watchdog */
181 		return 0;
182 	case WDIOC_SETTIMEOUT:
183 		if (get_user(new_value, p))
184 			return -EFAULT;
185 		if (at91_wdt_settimeout(new_value))
186 			return -EINVAL;
187 		/* Enable new time value */
188 		at91_wdt_start();
189 		/* Return current value */
190 		return put_user(wdt_time, p);
191 	case WDIOC_GETTIMEOUT:
192 		return put_user(wdt_time, p);
193 	default:
194 		return -ENOTTY;
195 	}
196 }
197 
198 /*
199  * Pat the watchdog whenever device is written to.
200  */
at91_wdt_write(struct file * file,const char * data,size_t len,loff_t * ppos)201 static ssize_t at91_wdt_write(struct file *file, const char *data,
202 						size_t len, loff_t *ppos)
203 {
204 	at91_wdt_reload();		/* pat the watchdog */
205 	return len;
206 }
207 
208 /* ......................................................................... */
209 
210 static const struct file_operations at91wdt_fops = {
211 	.owner		= THIS_MODULE,
212 	.unlocked_ioctl	= at91_wdt_ioctl,
213 	.compat_ioctl	= compat_ptr_ioctl,
214 	.open		= at91_wdt_open,
215 	.release	= at91_wdt_close,
216 	.write		= at91_wdt_write,
217 };
218 
219 static struct miscdevice at91wdt_miscdev = {
220 	.minor		= WATCHDOG_MINOR,
221 	.name		= "watchdog",
222 	.fops		= &at91wdt_fops,
223 };
224 
at91wdt_probe(struct platform_device * pdev)225 static int at91wdt_probe(struct platform_device *pdev)
226 {
227 	struct device *dev = &pdev->dev;
228 	struct device *parent;
229 	int res;
230 
231 	if (at91wdt_miscdev.parent)
232 		return -EBUSY;
233 	at91wdt_miscdev.parent = &pdev->dev;
234 
235 	parent = dev->parent;
236 	if (!parent) {
237 		dev_err(dev, "no parent\n");
238 		return -ENODEV;
239 	}
240 
241 	regmap_st = syscon_node_to_regmap(parent->of_node);
242 	if (IS_ERR(regmap_st))
243 		return -ENODEV;
244 
245 	res = misc_register(&at91wdt_miscdev);
246 	if (res)
247 		return res;
248 
249 	res = register_restart_handler(&at91rm9200_restart_nb);
250 	if (res)
251 		dev_warn(dev, "failed to register restart handler\n");
252 
253 	pr_info("AT91 Watchdog Timer enabled (%d seconds%s)\n",
254 		wdt_time, nowayout ? ", nowayout" : "");
255 	return 0;
256 }
257 
at91wdt_remove(struct platform_device * pdev)258 static void at91wdt_remove(struct platform_device *pdev)
259 {
260 	struct device *dev = &pdev->dev;
261 	int res;
262 
263 	res = unregister_restart_handler(&at91rm9200_restart_nb);
264 	if (res)
265 		dev_warn(dev, "failed to unregister restart handler\n");
266 
267 	misc_deregister(&at91wdt_miscdev);
268 	at91wdt_miscdev.parent = NULL;
269 }
270 
at91wdt_shutdown(struct platform_device * pdev)271 static void at91wdt_shutdown(struct platform_device *pdev)
272 {
273 	at91_wdt_stop();
274 }
275 
at91wdt_suspend(struct platform_device * pdev,pm_message_t message)276 static int at91wdt_suspend(struct platform_device *pdev, pm_message_t message)
277 {
278 	at91_wdt_stop();
279 	return 0;
280 }
281 
at91wdt_resume(struct platform_device * pdev)282 static int at91wdt_resume(struct platform_device *pdev)
283 {
284 	if (at91wdt_busy)
285 		at91_wdt_start();
286 	return 0;
287 }
288 
289 static const struct of_device_id at91_wdt_dt_ids[] = {
290 	{ .compatible = "atmel,at91rm9200-wdt" },
291 	{ /* sentinel */ }
292 };
293 MODULE_DEVICE_TABLE(of, at91_wdt_dt_ids);
294 
295 static struct platform_driver at91wdt_driver = {
296 	.probe		= at91wdt_probe,
297 	.remove		= at91wdt_remove,
298 	.shutdown	= at91wdt_shutdown,
299 	.suspend	= pm_ptr(at91wdt_suspend),
300 	.resume		= pm_ptr(at91wdt_resume),
301 	.driver		= {
302 		.name	= "atmel_st_watchdog",
303 		.of_match_table = at91_wdt_dt_ids,
304 	},
305 };
306 
at91_wdt_init(void)307 static int __init at91_wdt_init(void)
308 {
309 	/* Check that the heartbeat value is within range;
310 	   if not reset to the default */
311 	if (at91_wdt_settimeout(wdt_time)) {
312 		at91_wdt_settimeout(WDT_DEFAULT_TIME);
313 		pr_info("wdt_time value must be 1 <= wdt_time <= 256, using %d\n",
314 			wdt_time);
315 	}
316 
317 	return platform_driver_register(&at91wdt_driver);
318 }
319 
at91_wdt_exit(void)320 static void __exit at91_wdt_exit(void)
321 {
322 	platform_driver_unregister(&at91wdt_driver);
323 }
324 
325 module_init(at91_wdt_init);
326 module_exit(at91_wdt_exit);
327 
328 MODULE_AUTHOR("Andrew Victor");
329 MODULE_DESCRIPTION("Watchdog driver for Atmel AT91RM9200");
330 MODULE_LICENSE("GPL");
331 MODULE_ALIAS("platform:atmel_st_watchdog");
332