blob: 76fa724930ca562fd5e35dfef7c4bfe2fbfbdb7d [file] [log] [blame]
Wolfram Sangbb2fd8a2010-04-29 10:03:17 +02001/*
2 * Watchdog driver for IMX2 and later processors
3 *
4 * Copyright (C) 2010 Wolfram Sang, Pengutronix e.K. <w.sang@pengutronix.de>
Anson Huang1a9c5ef2014-01-13 19:58:34 +08005 * Copyright (C) 2014 Freescale Semiconductor, Inc.
Wolfram Sangbb2fd8a2010-04-29 10:03:17 +02006 *
7 * some parts adapted by similar drivers from Darius Augulis and Vladimir
8 * Zapolskiy, additional improvements by Wim Van Sebroeck.
9 *
10 * This program is free software; you can redistribute it and/or modify it
11 * under the terms of the GNU General Public License version 2 as published by
12 * the Free Software Foundation.
13 *
14 * NOTE: MX1 has a slightly different Watchdog than MX2 and later:
15 *
16 * MX1: MX2+:
17 * ---- -----
18 * Registers: 32-bit 16-bit
19 * Stopable timer: Yes No
20 * Need to enable clk: No Yes
21 * Halt on suspend: Manual Can be automatic
22 */
23
Xiubo Li30cb0422014-04-04 09:33:24 +080024#include <linux/clk.h>
25#include <linux/fs.h>
Wolfram Sangbb2fd8a2010-04-29 10:03:17 +020026#include <linux/init.h>
Xiubo Li30cb0422014-04-04 09:33:24 +080027#include <linux/io.h>
28#include <linux/jiffies.h>
Wolfram Sangbb2fd8a2010-04-29 10:03:17 +020029#include <linux/kernel.h>
30#include <linux/miscdevice.h>
31#include <linux/module.h>
32#include <linux/moduleparam.h>
33#include <linux/platform_device.h>
Xiubo Lia7977002014-04-04 09:33:25 +080034#include <linux/regmap.h>
Wolfram Sangbb2fd8a2010-04-29 10:03:17 +020035#include <linux/timer.h>
Xiubo Li30cb0422014-04-04 09:33:24 +080036#include <linux/uaccess.h>
37#include <linux/watchdog.h>
Wolfram Sangbb2fd8a2010-04-29 10:03:17 +020038
39#define DRIVER_NAME "imx2-wdt"
40
41#define IMX2_WDT_WCR 0x00 /* Control Register */
42#define IMX2_WDT_WCR_WT (0xFF << 8) /* -> Watchdog Timeout Field */
43#define IMX2_WDT_WCR_WRE (1 << 3) /* -> WDOG Reset Enable */
44#define IMX2_WDT_WCR_WDE (1 << 2) /* -> Watchdog Enable */
Anson Huang1a9c5ef2014-01-13 19:58:34 +080045#define IMX2_WDT_WCR_WDZST (1 << 0) /* -> Watchdog timer Suspend */
Wolfram Sangbb2fd8a2010-04-29 10:03:17 +020046
47#define IMX2_WDT_WSR 0x02 /* Service Register */
48#define IMX2_WDT_SEQ1 0x5555 /* -> service sequence 1 */
49#define IMX2_WDT_SEQ2 0xAAAA /* -> service sequence 2 */
50
Oskar Schirmer474ef122012-02-16 12:17:45 +000051#define IMX2_WDT_WRSR 0x04 /* Reset Status Register */
52#define IMX2_WDT_WRSR_TOUT (1 << 1) /* -> Reset due to Timeout */
53
Wolfram Sangbb2fd8a2010-04-29 10:03:17 +020054#define IMX2_WDT_MAX_TIME 128
55#define IMX2_WDT_DEFAULT_TIME 60 /* in seconds */
56
57#define WDOG_SEC_TO_COUNT(s) ((s * 2 - 1) << 8)
58
59#define IMX2_WDT_STATUS_OPEN 0
60#define IMX2_WDT_STATUS_STARTED 1
61#define IMX2_WDT_EXPECT_CLOSE 2
62
63static struct {
64 struct clk *clk;
Xiubo Lia7977002014-04-04 09:33:25 +080065 struct regmap *regmap;
Wolfram Sangbb2fd8a2010-04-29 10:03:17 +020066 unsigned timeout;
67 unsigned long status;
68 struct timer_list timer; /* Pings the watchdog when closed */
69} imx2_wdt;
70
71static struct miscdevice imx2_wdt_miscdev;
72
Wim Van Sebroeck86a1e182012-03-05 16:51:11 +010073static bool nowayout = WATCHDOG_NOWAYOUT;
74module_param(nowayout, bool, 0);
Wolfram Sangbb2fd8a2010-04-29 10:03:17 +020075MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started (default="
76 __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
77
78
79static unsigned timeout = IMX2_WDT_DEFAULT_TIME;
80module_param(timeout, uint, 0);
81MODULE_PARM_DESC(timeout, "Watchdog timeout in seconds (default="
82 __MODULE_STRING(IMX2_WDT_DEFAULT_TIME) ")");
83
84static const struct watchdog_info imx2_wdt_info = {
85 .identity = "imx2+ watchdog",
86 .options = WDIOF_KEEPALIVEPING | WDIOF_SETTIMEOUT | WDIOF_MAGICCLOSE,
87};
88
89static inline void imx2_wdt_setup(void)
90{
Xiubo Lia7977002014-04-04 09:33:25 +080091 u32 val;
92
93 regmap_read(imx2_wdt.regmap, IMX2_WDT_WCR, &val);
Wolfram Sangbb2fd8a2010-04-29 10:03:17 +020094
Anson Huang1a9c5ef2014-01-13 19:58:34 +080095 /* Suspend timer in low power mode, write once-only */
96 val |= IMX2_WDT_WCR_WDZST;
Wolfram Sangbb2fd8a2010-04-29 10:03:17 +020097 /* Strip the old watchdog Time-Out value */
98 val &= ~IMX2_WDT_WCR_WT;
99 /* Generate reset if WDOG times out */
100 val &= ~IMX2_WDT_WCR_WRE;
101 /* Keep Watchdog Disabled */
102 val &= ~IMX2_WDT_WCR_WDE;
103 /* Set the watchdog's Time-Out value */
104 val |= WDOG_SEC_TO_COUNT(imx2_wdt.timeout);
105
Xiubo Lia7977002014-04-04 09:33:25 +0800106 regmap_write(imx2_wdt.regmap, IMX2_WDT_WCR, val);
Wolfram Sangbb2fd8a2010-04-29 10:03:17 +0200107
108 /* enable the watchdog */
109 val |= IMX2_WDT_WCR_WDE;
Xiubo Lia7977002014-04-04 09:33:25 +0800110 regmap_write(imx2_wdt.regmap, IMX2_WDT_WCR, val);
Wolfram Sangbb2fd8a2010-04-29 10:03:17 +0200111}
112
113static inline void imx2_wdt_ping(void)
114{
Xiubo Lia7977002014-04-04 09:33:25 +0800115 regmap_write(imx2_wdt.regmap, IMX2_WDT_WSR, IMX2_WDT_SEQ1);
116 regmap_write(imx2_wdt.regmap, IMX2_WDT_WSR, IMX2_WDT_SEQ2);
Wolfram Sangbb2fd8a2010-04-29 10:03:17 +0200117}
118
119static void imx2_wdt_timer_ping(unsigned long arg)
120{
121 /* ping it every imx2_wdt.timeout / 2 seconds to prevent reboot */
122 imx2_wdt_ping();
123 mod_timer(&imx2_wdt.timer, jiffies + imx2_wdt.timeout * HZ / 2);
124}
125
126static void imx2_wdt_start(void)
127{
128 if (!test_and_set_bit(IMX2_WDT_STATUS_STARTED, &imx2_wdt.status)) {
129 /* at our first start we enable clock and do initialisations */
Sascha Hauer4e7b6c92012-04-03 12:34:57 +0200130 clk_prepare_enable(imx2_wdt.clk);
Wolfram Sangbb2fd8a2010-04-29 10:03:17 +0200131
132 imx2_wdt_setup();
133 } else /* delete the timer that pings the watchdog after close */
134 del_timer_sync(&imx2_wdt.timer);
135
136 /* Watchdog is enabled - time to reload the timeout value */
137 imx2_wdt_ping();
138}
139
140static void imx2_wdt_stop(void)
141{
142 /* we don't need a clk_disable, it cannot be disabled once started.
143 * We use a timer to ping the watchdog while /dev/watchdog is closed */
144 imx2_wdt_timer_ping(0);
145}
146
147static void imx2_wdt_set_timeout(int new_timeout)
148{
Xiubo Lia7977002014-04-04 09:33:25 +0800149 regmap_update_bits(imx2_wdt.regmap, IMX2_WDT_WCR, IMX2_WDT_WCR_WT,
150 WDOG_SEC_TO_COUNT(new_timeout));
Wolfram Sangbb2fd8a2010-04-29 10:03:17 +0200151}
152
153static int imx2_wdt_open(struct inode *inode, struct file *file)
154{
155 if (test_and_set_bit(IMX2_WDT_STATUS_OPEN, &imx2_wdt.status))
156 return -EBUSY;
157
158 imx2_wdt_start();
159 return nonseekable_open(inode, file);
160}
161
162static int imx2_wdt_close(struct inode *inode, struct file *file)
163{
164 if (test_bit(IMX2_WDT_EXPECT_CLOSE, &imx2_wdt.status) && !nowayout)
165 imx2_wdt_stop();
166 else {
167 dev_crit(imx2_wdt_miscdev.parent,
168 "Unexpected close: Expect reboot!\n");
169 imx2_wdt_ping();
170 }
171
172 clear_bit(IMX2_WDT_EXPECT_CLOSE, &imx2_wdt.status);
173 clear_bit(IMX2_WDT_STATUS_OPEN, &imx2_wdt.status);
174 return 0;
175}
176
177static long imx2_wdt_ioctl(struct file *file, unsigned int cmd,
178 unsigned long arg)
179{
180 void __user *argp = (void __user *)arg;
181 int __user *p = argp;
182 int new_value;
Xiubo Lia7977002014-04-04 09:33:25 +0800183 u32 val;
Wolfram Sangbb2fd8a2010-04-29 10:03:17 +0200184
185 switch (cmd) {
186 case WDIOC_GETSUPPORT:
187 return copy_to_user(argp, &imx2_wdt_info,
188 sizeof(struct watchdog_info)) ? -EFAULT : 0;
189
190 case WDIOC_GETSTATUS:
Wolfram Sangbb2fd8a2010-04-29 10:03:17 +0200191 return put_user(0, p);
192
Oskar Schirmer474ef122012-02-16 12:17:45 +0000193 case WDIOC_GETBOOTSTATUS:
Xiubo Lia7977002014-04-04 09:33:25 +0800194 regmap_read(imx2_wdt.regmap, IMX2_WDT_WRSR, &val);
Oskar Schirmer474ef122012-02-16 12:17:45 +0000195 new_value = val & IMX2_WDT_WRSR_TOUT ? WDIOF_CARDRESET : 0;
196 return put_user(new_value, p);
197
Wolfram Sangbb2fd8a2010-04-29 10:03:17 +0200198 case WDIOC_KEEPALIVE:
199 imx2_wdt_ping();
200 return 0;
201
202 case WDIOC_SETTIMEOUT:
203 if (get_user(new_value, p))
204 return -EFAULT;
205 if ((new_value < 1) || (new_value > IMX2_WDT_MAX_TIME))
206 return -EINVAL;
207 imx2_wdt_set_timeout(new_value);
208 imx2_wdt.timeout = new_value;
209 imx2_wdt_ping();
210
211 /* Fallthrough to return current value */
212 case WDIOC_GETTIMEOUT:
213 return put_user(imx2_wdt.timeout, p);
214
215 default:
216 return -ENOTTY;
217 }
218}
219
220static ssize_t imx2_wdt_write(struct file *file, const char __user *data,
221 size_t len, loff_t *ppos)
222{
223 size_t i;
224 char c;
225
226 if (len == 0) /* Can we see this even ? */
227 return 0;
228
229 clear_bit(IMX2_WDT_EXPECT_CLOSE, &imx2_wdt.status);
230 /* scan to see whether or not we got the magic character */
231 for (i = 0; i != len; i++) {
232 if (get_user(c, data + i))
233 return -EFAULT;
234 if (c == 'V')
235 set_bit(IMX2_WDT_EXPECT_CLOSE, &imx2_wdt.status);
236 }
237
238 imx2_wdt_ping();
239 return len;
240}
241
242static const struct file_operations imx2_wdt_fops = {
243 .owner = THIS_MODULE,
244 .llseek = no_llseek,
245 .unlocked_ioctl = imx2_wdt_ioctl,
246 .open = imx2_wdt_open,
247 .release = imx2_wdt_close,
248 .write = imx2_wdt_write,
249};
250
251static struct miscdevice imx2_wdt_miscdev = {
252 .minor = WATCHDOG_MINOR,
253 .name = "watchdog",
254 .fops = &imx2_wdt_fops,
255};
256
Xiubo Lia7977002014-04-04 09:33:25 +0800257static struct regmap_config imx2_wdt_regmap_config = {
258 .reg_bits = 16,
259 .reg_stride = 2,
260 .val_bits = 16,
261 .max_register = 0x8,
262};
263
Wolfram Sangbb2fd8a2010-04-29 10:03:17 +0200264static int __init imx2_wdt_probe(struct platform_device *pdev)
265{
Wolfram Sangbb2fd8a2010-04-29 10:03:17 +0200266 struct resource *res;
Xiubo Lia7977002014-04-04 09:33:25 +0800267 void __iomem *base;
268 int ret;
Wolfram Sangbb2fd8a2010-04-29 10:03:17 +0200269
270 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Xiubo Lia7977002014-04-04 09:33:25 +0800271 base = devm_ioremap_resource(&pdev->dev, res);
272 if (IS_ERR(base))
273 return PTR_ERR(base);
274
275 imx2_wdt.regmap = devm_regmap_init_mmio_clk(&pdev->dev, NULL, base,
276 &imx2_wdt_regmap_config);
277 if (IS_ERR(imx2_wdt.regmap)) {
278 dev_err(&pdev->dev, "regmap init failed\n");
279 return PTR_ERR(imx2_wdt.regmap);
280 }
Wolfram Sangbb2fd8a2010-04-29 10:03:17 +0200281
Jingoo Hanbdf49572013-04-29 18:15:53 +0900282 imx2_wdt.clk = devm_clk_get(&pdev->dev, NULL);
Wolfram Sangbb2fd8a2010-04-29 10:03:17 +0200283 if (IS_ERR(imx2_wdt.clk)) {
284 dev_err(&pdev->dev, "can't get Watchdog clock\n");
285 return PTR_ERR(imx2_wdt.clk);
286 }
287
288 imx2_wdt.timeout = clamp_t(unsigned, timeout, 1, IMX2_WDT_MAX_TIME);
289 if (imx2_wdt.timeout != timeout)
290 dev_warn(&pdev->dev, "Initial timeout out of range! "
291 "Clamped from %u to %u\n", timeout, imx2_wdt.timeout);
292
293 setup_timer(&imx2_wdt.timer, imx2_wdt_timer_ping, 0);
294
295 imx2_wdt_miscdev.parent = &pdev->dev;
296 ret = misc_register(&imx2_wdt_miscdev);
297 if (ret)
298 goto fail;
299
300 dev_info(&pdev->dev,
301 "IMX2+ Watchdog Timer enabled. timeout=%ds (nowayout=%d)\n",
302 imx2_wdt.timeout, nowayout);
303 return 0;
304
305fail:
306 imx2_wdt_miscdev.parent = NULL;
Wolfram Sangbb2fd8a2010-04-29 10:03:17 +0200307 return ret;
308}
309
310static int __exit imx2_wdt_remove(struct platform_device *pdev)
311{
312 misc_deregister(&imx2_wdt_miscdev);
313
314 if (test_bit(IMX2_WDT_STATUS_STARTED, &imx2_wdt.status)) {
315 del_timer_sync(&imx2_wdt.timer);
316
317 dev_crit(imx2_wdt_miscdev.parent,
318 "Device removed: Expect reboot!\n");
Jingoo Hanbdf49572013-04-29 18:15:53 +0900319 }
Wolfram Sangbb2fd8a2010-04-29 10:03:17 +0200320
321 imx2_wdt_miscdev.parent = NULL;
322 return 0;
323}
324
325static void imx2_wdt_shutdown(struct platform_device *pdev)
326{
327 if (test_bit(IMX2_WDT_STATUS_STARTED, &imx2_wdt.status)) {
328 /* we are running, we need to delete the timer but will give
329 * max timeout before reboot will take place */
330 del_timer_sync(&imx2_wdt.timer);
331 imx2_wdt_set_timeout(IMX2_WDT_MAX_TIME);
332 imx2_wdt_ping();
333
334 dev_crit(imx2_wdt_miscdev.parent,
335 "Device shutdown: Expect reboot!\n");
336 }
337}
338
Shawn Guof5a427e2011-07-18 11:15:21 +0800339static const struct of_device_id imx2_wdt_dt_ids[] = {
340 { .compatible = "fsl,imx21-wdt", },
341 { /* sentinel */ }
342};
Niels de Vos813296a2013-07-29 09:38:18 +0200343MODULE_DEVICE_TABLE(of, imx2_wdt_dt_ids);
Shawn Guof5a427e2011-07-18 11:15:21 +0800344
Wolfram Sangbb2fd8a2010-04-29 10:03:17 +0200345static struct platform_driver imx2_wdt_driver = {
Wolfram Sangbb2fd8a2010-04-29 10:03:17 +0200346 .remove = __exit_p(imx2_wdt_remove),
347 .shutdown = imx2_wdt_shutdown,
348 .driver = {
349 .name = DRIVER_NAME,
350 .owner = THIS_MODULE,
Shawn Guof5a427e2011-07-18 11:15:21 +0800351 .of_match_table = imx2_wdt_dt_ids,
Wolfram Sangbb2fd8a2010-04-29 10:03:17 +0200352 },
353};
354
Fabio Porcedda1cb92042013-01-09 12:15:27 +0100355module_platform_driver_probe(imx2_wdt_driver, imx2_wdt_probe);
Wolfram Sangbb2fd8a2010-04-29 10:03:17 +0200356
357MODULE_AUTHOR("Wolfram Sang");
358MODULE_DESCRIPTION("Watchdog driver for IMX2 and later");
359MODULE_LICENSE("GPL v2");
Wolfram Sangbb2fd8a2010-04-29 10:03:17 +0200360MODULE_ALIAS("platform:" DRIVER_NAME);