blob: 41cecb55766c049b600834d6ee021ef45f15332e [file] [log] [blame]
Andrew Victor853807f2006-03-14 11:11:04 +02001/*
2 * Watchdog driver for Atmel AT91RM9200 (Thunder)
3 *
4 * Copyright (C) 2003 SAN People (Pty) Ltd
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
Joe Perches27c766a2012-02-15 15:06:19 -080012#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
13
Jiri Slaby1977f032007-10-18 23:40:25 -070014#include <linux/bitops.h>
Alexandre Belloni9ab45f32015-03-12 13:07:28 +010015#include <linux/delay.h>
Andrew Victor853807f2006-03-14 11:11:04 +020016#include <linux/errno.h>
17#include <linux/fs.h>
18#include <linux/init.h>
Jean-Christop PLAGNIOL-VILLARD02e07462009-01-23 10:06:07 +010019#include <linux/io.h>
Andrew Victor853807f2006-03-14 11:11:04 +020020#include <linux/kernel.h>
Alexandre Belloni8432f9e2015-03-12 13:07:27 +010021#include <linux/mfd/syscon.h>
22#include <linux/mfd/syscon/atmel-st.h>
Andrew Victor853807f2006-03-14 11:11:04 +020023#include <linux/miscdevice.h>
24#include <linux/module.h>
25#include <linux/moduleparam.h>
Andrew Victordfc7bd92006-05-21 15:32:59 +020026#include <linux/platform_device.h>
Alexandre Belloni9ab45f32015-03-12 13:07:28 +010027#include <linux/reboot.h>
Alexandre Belloni8432f9e2015-03-12 13:07:27 +010028#include <linux/regmap.h>
Andrew Victor853807f2006-03-14 11:11:04 +020029#include <linux/types.h>
30#include <linux/watchdog.h>
Alan Cox27606002008-05-19 14:05:19 +010031#include <linux/uaccess.h>
Joachim Eastwooda6a1bcd2013-02-14 23:02:29 +010032#include <linux/of.h>
33#include <linux/of_device.h>
Andrew Victor853807f2006-03-14 11:11:04 +020034
Andrew Victordfc7bd92006-05-21 15:32:59 +020035#define WDT_DEFAULT_TIME 5 /* seconds */
36#define WDT_MAX_TIME 256 /* seconds */
Andrew Victor853807f2006-03-14 11:11:04 +020037
38static int wdt_time = WDT_DEFAULT_TIME;
Wim Van Sebroeck86a1e182012-03-05 16:51:11 +010039static bool nowayout = WATCHDOG_NOWAYOUT;
Alexandre Belloni8432f9e2015-03-12 13:07:27 +010040static struct regmap *regmap_st;
Andrew Victor853807f2006-03-14 11:11:04 +020041
42module_param(wdt_time, int, 0);
Alan Cox27606002008-05-19 14:05:19 +010043MODULE_PARM_DESC(wdt_time, "Watchdog time in seconds. (default="
44 __MODULE_STRING(WDT_DEFAULT_TIME) ")");
Andrew Victor853807f2006-03-14 11:11:04 +020045
Andrew Victordfc7bd92006-05-21 15:32:59 +020046#ifdef CONFIG_WATCHDOG_NOWAYOUT
Wim Van Sebroeck86a1e182012-03-05 16:51:11 +010047module_param(nowayout, bool, 0);
Alan Cox27606002008-05-19 14:05:19 +010048MODULE_PARM_DESC(nowayout,
49 "Watchdog cannot be stopped once started (default="
50 __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
Andrew Victordfc7bd92006-05-21 15:32:59 +020051#endif
Andrew Victor853807f2006-03-14 11:11:04 +020052
53
54static unsigned long at91wdt_busy;
55
56/* ......................................................................... */
57
Alexandre Belloni9ab45f32015-03-12 13:07:28 +010058static int at91rm9200_restart(struct notifier_block *this,
59 unsigned long mode, void *cmd)
60{
61 /*
62 * Perform a hardware reset with the use of the Watchdog timer.
63 */
64 regmap_write(regmap_st, AT91_ST_WDMR,
65 AT91_ST_RSTEN | AT91_ST_EXTEN | 1);
66 regmap_write(regmap_st, AT91_ST_CR, AT91_ST_WDRST);
67
68 mdelay(2000);
69
70 pr_emerg("Unable to restart system\n");
71 return NOTIFY_DONE;
72}
73
74static struct notifier_block at91rm9200_restart_nb = {
75 .notifier_call = at91rm9200_restart,
76 .priority = 192,
77};
78
Andrew Victor853807f2006-03-14 11:11:04 +020079/*
80 * Disable the watchdog.
81 */
Alan Cox27606002008-05-19 14:05:19 +010082static inline void at91_wdt_stop(void)
Andrew Victor853807f2006-03-14 11:11:04 +020083{
Alexandre Belloni8432f9e2015-03-12 13:07:27 +010084 regmap_write(regmap_st, AT91_ST_WDMR, AT91_ST_EXTEN);
Andrew Victor853807f2006-03-14 11:11:04 +020085}
86
87/*
88 * Enable and reset the watchdog.
89 */
Alan Cox27606002008-05-19 14:05:19 +010090static inline void at91_wdt_start(void)
Andrew Victor853807f2006-03-14 11:11:04 +020091{
Alexandre Belloni8432f9e2015-03-12 13:07:27 +010092 regmap_write(regmap_st, AT91_ST_WDMR, AT91_ST_EXTEN | AT91_ST_RSTEN |
Alan Cox27606002008-05-19 14:05:19 +010093 (((65536 * wdt_time) >> 8) & AT91_ST_WDV));
Alexandre Belloni8432f9e2015-03-12 13:07:27 +010094 regmap_write(regmap_st, AT91_ST_CR, AT91_ST_WDRST);
Andrew Victor853807f2006-03-14 11:11:04 +020095}
96
97/*
98 * Reload the watchdog timer. (ie, pat the watchdog)
99 */
Alan Cox27606002008-05-19 14:05:19 +0100100static inline void at91_wdt_reload(void)
Andrew Victor853807f2006-03-14 11:11:04 +0200101{
Alexandre Belloni8432f9e2015-03-12 13:07:27 +0100102 regmap_write(regmap_st, AT91_ST_CR, AT91_ST_WDRST);
Andrew Victor853807f2006-03-14 11:11:04 +0200103}
104
105/* ......................................................................... */
106
107/*
108 * Watchdog device is opened, and watchdog starts running.
109 */
110static int at91_wdt_open(struct inode *inode, struct file *file)
111{
112 if (test_and_set_bit(0, &at91wdt_busy))
113 return -EBUSY;
114
115 at91_wdt_start();
116 return nonseekable_open(inode, file);
117}
118
119/*
120 * Close the watchdog device.
121 * If CONFIG_WATCHDOG_NOWAYOUT is NOT defined then the watchdog is also
122 * disabled.
123 */
124static int at91_wdt_close(struct inode *inode, struct file *file)
125{
Alan Cox27606002008-05-19 14:05:19 +0100126 /* Disable the watchdog when file is closed */
Andrew Victor853807f2006-03-14 11:11:04 +0200127 if (!nowayout)
Alan Cox27606002008-05-19 14:05:19 +0100128 at91_wdt_stop();
Andrew Victor853807f2006-03-14 11:11:04 +0200129
130 clear_bit(0, &at91wdt_busy);
131 return 0;
132}
133
134/*
135 * Change the watchdog time interval.
136 */
137static int at91_wdt_settimeout(int new_time)
138{
139 /*
Andrew Victor2af29b72009-02-11 21:23:10 +0100140 * All counting occurs at SLOW_CLOCK / 128 = 256 Hz
Andrew Victor853807f2006-03-14 11:11:04 +0200141 *
142 * Since WDV is a 16-bit counter, the maximum period is
Andrew Victor2af29b72009-02-11 21:23:10 +0100143 * 65536 / 256 = 256 seconds.
Andrew Victor853807f2006-03-14 11:11:04 +0200144 */
145 if ((new_time <= 0) || (new_time > WDT_MAX_TIME))
146 return -EINVAL;
147
Alan Cox27606002008-05-19 14:05:19 +0100148 /* Set new watchdog time. It will be used when
149 at91_wdt_start() is called. */
Andrew Victor853807f2006-03-14 11:11:04 +0200150 wdt_time = new_time;
151 return 0;
152}
153
Wim Van Sebroeck42747d72009-12-26 18:55:22 +0000154static const struct watchdog_info at91_wdt_info = {
Andrew Victor853807f2006-03-14 11:11:04 +0200155 .identity = "at91 watchdog",
156 .options = WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING,
157};
158
159/*
160 * Handle commands from user-space.
161 */
Adrian Bunk3c4fafd2008-08-08 18:57:45 +0300162static long at91_wdt_ioctl(struct file *file,
Alan Cox27606002008-05-19 14:05:19 +0100163 unsigned int cmd, unsigned long arg)
Andrew Victor853807f2006-03-14 11:11:04 +0200164{
165 void __user *argp = (void __user *)arg;
166 int __user *p = argp;
167 int new_value;
168
Alan Cox27606002008-05-19 14:05:19 +0100169 switch (cmd) {
Alan Cox27606002008-05-19 14:05:19 +0100170 case WDIOC_GETSUPPORT:
171 return copy_to_user(argp, &at91_wdt_info,
172 sizeof(at91_wdt_info)) ? -EFAULT : 0;
Alan Cox27606002008-05-19 14:05:19 +0100173 case WDIOC_GETSTATUS:
174 case WDIOC_GETBOOTSTATUS:
175 return put_user(0, p);
176 case WDIOC_SETOPTIONS:
177 if (get_user(new_value, p))
178 return -EFAULT;
179 if (new_value & WDIOS_DISABLECARD)
180 at91_wdt_stop();
181 if (new_value & WDIOS_ENABLECARD)
Andrew Victor853807f2006-03-14 11:11:04 +0200182 at91_wdt_start();
Alan Cox27606002008-05-19 14:05:19 +0100183 return 0;
Wim Van Sebroeck0c06090c2008-07-18 11:41:17 +0000184 case WDIOC_KEEPALIVE:
185 at91_wdt_reload(); /* pat the watchdog */
186 return 0;
187 case WDIOC_SETTIMEOUT:
188 if (get_user(new_value, p))
189 return -EFAULT;
190 if (at91_wdt_settimeout(new_value))
191 return -EINVAL;
192 /* Enable new time value */
193 at91_wdt_start();
194 /* Return current value */
195 return put_user(wdt_time, p);
196 case WDIOC_GETTIMEOUT:
197 return put_user(wdt_time, p);
Alan Cox27606002008-05-19 14:05:19 +0100198 default:
199 return -ENOTTY;
Andrew Victor853807f2006-03-14 11:11:04 +0200200 }
201}
202
203/*
204 * Pat the watchdog whenever device is written to.
205 */
Alan Cox27606002008-05-19 14:05:19 +0100206static ssize_t at91_wdt_write(struct file *file, const char *data,
207 size_t len, loff_t *ppos)
Andrew Victor853807f2006-03-14 11:11:04 +0200208{
209 at91_wdt_reload(); /* pat the watchdog */
210 return len;
211}
212
213/* ......................................................................... */
214
Arjan van de Ven62322d22006-07-03 00:24:21 -0700215static const struct file_operations at91wdt_fops = {
Andrew Victor853807f2006-03-14 11:11:04 +0200216 .owner = THIS_MODULE,
217 .llseek = no_llseek,
Alan Cox27606002008-05-19 14:05:19 +0100218 .unlocked_ioctl = at91_wdt_ioctl,
Andrew Victor853807f2006-03-14 11:11:04 +0200219 .open = at91_wdt_open,
220 .release = at91_wdt_close,
221 .write = at91_wdt_write,
222};
223
224static struct miscdevice at91wdt_miscdev = {
225 .minor = WATCHDOG_MINOR,
226 .name = "watchdog",
227 .fops = &at91wdt_fops,
228};
229
Bill Pemberton2d991a12012-11-19 13:21:41 -0500230static int at91wdt_probe(struct platform_device *pdev)
Andrew Victor853807f2006-03-14 11:11:04 +0200231{
Alexandre Belloni8432f9e2015-03-12 13:07:27 +0100232 struct device *dev = &pdev->dev;
233 struct device *parent;
Andrew Victor853807f2006-03-14 11:11:04 +0200234 int res;
235
Andrew Victore0b79e02006-12-04 15:56:18 +0200236 if (at91wdt_miscdev.parent)
Andrew Victordfc7bd92006-05-21 15:32:59 +0200237 return -EBUSY;
Andrew Victore0b79e02006-12-04 15:56:18 +0200238 at91wdt_miscdev.parent = &pdev->dev;
Andrew Victor853807f2006-03-14 11:11:04 +0200239
Alexandre Belloni8432f9e2015-03-12 13:07:27 +0100240 parent = dev->parent;
241 if (!parent) {
242 dev_err(dev, "no parent\n");
243 return -ENODEV;
244 }
245
246 regmap_st = syscon_node_to_regmap(parent->of_node);
247 if (!regmap_st)
248 return -ENODEV;
249
Andrew Victor853807f2006-03-14 11:11:04 +0200250 res = misc_register(&at91wdt_miscdev);
251 if (res)
252 return res;
253
Alexandre Belloni9ab45f32015-03-12 13:07:28 +0100254 res = register_restart_handler(&at91rm9200_restart_nb);
255 if (res)
256 dev_warn(dev, "failed to register restart handler\n");
257
Joe Perches27c766a2012-02-15 15:06:19 -0800258 pr_info("AT91 Watchdog Timer enabled (%d seconds%s)\n",
259 wdt_time, nowayout ? ", nowayout" : "");
Andrew Victor853807f2006-03-14 11:11:04 +0200260 return 0;
261}
262
Bill Pemberton4b12b892012-11-19 13:26:24 -0500263static int at91wdt_remove(struct platform_device *pdev)
Andrew Victordfc7bd92006-05-21 15:32:59 +0200264{
Alexandre Belloni9ab45f32015-03-12 13:07:28 +0100265 struct device *dev = &pdev->dev;
Andrew Victordfc7bd92006-05-21 15:32:59 +0200266 int res;
267
Alexandre Belloni9ab45f32015-03-12 13:07:28 +0100268 res = unregister_restart_handler(&at91rm9200_restart_nb);
269 if (res)
270 dev_warn(dev, "failed to unregister restart handler\n");
271
Andrew Victordfc7bd92006-05-21 15:32:59 +0200272 res = misc_deregister(&at91wdt_miscdev);
273 if (!res)
Andrew Victore0b79e02006-12-04 15:56:18 +0200274 at91wdt_miscdev.parent = NULL;
Andrew Victordfc7bd92006-05-21 15:32:59 +0200275
276 return res;
277}
278
279static void at91wdt_shutdown(struct platform_device *pdev)
280{
281 at91_wdt_stop();
282}
283
284#ifdef CONFIG_PM
285
286static int at91wdt_suspend(struct platform_device *pdev, pm_message_t message)
287{
288 at91_wdt_stop();
289 return 0;
290}
291
292static int at91wdt_resume(struct platform_device *pdev)
293{
294 if (at91wdt_busy)
295 at91_wdt_start();
Ilpo Jarvinen95f62bd2008-08-19 09:01:14 +0300296 return 0;
Andrew Victordfc7bd92006-05-21 15:32:59 +0200297}
298
299#else
300#define at91wdt_suspend NULL
301#define at91wdt_resume NULL
302#endif
303
Joachim Eastwooda6a1bcd2013-02-14 23:02:29 +0100304static const struct of_device_id at91_wdt_dt_ids[] = {
305 { .compatible = "atmel,at91rm9200-wdt" },
306 { /* sentinel */ }
307};
308MODULE_DEVICE_TABLE(of, at91_wdt_dt_ids);
309
Andrew Victordfc7bd92006-05-21 15:32:59 +0200310static struct platform_driver at91wdt_driver = {
311 .probe = at91wdt_probe,
Bill Pemberton82268712012-11-19 13:21:12 -0500312 .remove = at91wdt_remove,
Andrew Victordfc7bd92006-05-21 15:32:59 +0200313 .shutdown = at91wdt_shutdown,
314 .suspend = at91wdt_suspend,
315 .resume = at91wdt_resume,
316 .driver = {
Alexandre Belloni8432f9e2015-03-12 13:07:27 +0100317 .name = "atmel_st_watchdog",
Sachin Kamat85eee812013-09-30 10:12:51 +0530318 .of_match_table = at91_wdt_dt_ids,
Andrew Victordfc7bd92006-05-21 15:32:59 +0200319 },
320};
321
322static int __init at91_wdt_init(void)
323{
Alan Cox27606002008-05-19 14:05:19 +0100324 /* Check that the heartbeat value is within range;
325 if not reset to the default */
Andrew Victordfc7bd92006-05-21 15:32:59 +0200326 if (at91_wdt_settimeout(wdt_time)) {
327 at91_wdt_settimeout(WDT_DEFAULT_TIME);
Joe Perches27c766a2012-02-15 15:06:19 -0800328 pr_info("wdt_time value must be 1 <= wdt_time <= 256, using %d\n",
329 wdt_time);
Andrew Victordfc7bd92006-05-21 15:32:59 +0200330 }
331
332 return platform_driver_register(&at91wdt_driver);
333}
334
Andrew Victor853807f2006-03-14 11:11:04 +0200335static void __exit at91_wdt_exit(void)
336{
Andrew Victordfc7bd92006-05-21 15:32:59 +0200337 platform_driver_unregister(&at91wdt_driver);
Andrew Victor853807f2006-03-14 11:11:04 +0200338}
339
340module_init(at91_wdt_init);
341module_exit(at91_wdt_exit);
342
343MODULE_AUTHOR("Andrew Victor");
344MODULE_DESCRIPTION("Watchdog driver for Atmel AT91RM9200");
345MODULE_LICENSE("GPL");
Alexandre Belloni8432f9e2015-03-12 13:07:27 +0100346MODULE_ALIAS("platform:atmel_st_watchdog");