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