drivers/firmware/dmi_scan.c: fetch dmi version from SMBIOS if it exists
[linux/fpc-iii.git] / drivers / watchdog / imx2_wdt.c
blob9a45d0294cf48f21397f43909680f2f9349c6d86
1 /*
2 * Watchdog driver for IMX2 and later processors
4 * Copyright (C) 2010 Wolfram Sang, Pengutronix e.K. <w.sang@pengutronix.de>
6 * some parts adapted by similar drivers from Darius Augulis and Vladimir
7 * Zapolskiy, additional improvements by Wim Van Sebroeck.
9 * This program is free software; you can redistribute it and/or modify it
10 * under the terms of the GNU General Public License version 2 as published by
11 * the Free Software Foundation.
13 * NOTE: MX1 has a slightly different Watchdog than MX2 and later:
15 * MX1: MX2+:
16 * ---- -----
17 * Registers: 32-bit 16-bit
18 * Stopable timer: Yes No
19 * Need to enable clk: No Yes
20 * Halt on suspend: Manual Can be automatic
23 #include <linux/init.h>
24 #include <linux/kernel.h>
25 #include <linux/miscdevice.h>
26 #include <linux/module.h>
27 #include <linux/moduleparam.h>
28 #include <linux/platform_device.h>
29 #include <linux/watchdog.h>
30 #include <linux/clk.h>
31 #include <linux/fs.h>
32 #include <linux/io.h>
33 #include <linux/uaccess.h>
34 #include <linux/timer.h>
35 #include <linux/jiffies.h>
37 #define DRIVER_NAME "imx2-wdt"
39 #define IMX2_WDT_WCR 0x00 /* Control Register */
40 #define IMX2_WDT_WCR_WT (0xFF << 8) /* -> Watchdog Timeout Field */
41 #define IMX2_WDT_WCR_WRE (1 << 3) /* -> WDOG Reset Enable */
42 #define IMX2_WDT_WCR_WDE (1 << 2) /* -> Watchdog Enable */
44 #define IMX2_WDT_WSR 0x02 /* Service Register */
45 #define IMX2_WDT_SEQ1 0x5555 /* -> service sequence 1 */
46 #define IMX2_WDT_SEQ2 0xAAAA /* -> service sequence 2 */
48 #define IMX2_WDT_WRSR 0x04 /* Reset Status Register */
49 #define IMX2_WDT_WRSR_TOUT (1 << 1) /* -> Reset due to Timeout */
51 #define IMX2_WDT_MAX_TIME 128
52 #define IMX2_WDT_DEFAULT_TIME 60 /* in seconds */
54 #define WDOG_SEC_TO_COUNT(s) ((s * 2 - 1) << 8)
56 #define IMX2_WDT_STATUS_OPEN 0
57 #define IMX2_WDT_STATUS_STARTED 1
58 #define IMX2_WDT_EXPECT_CLOSE 2
60 static struct {
61 struct clk *clk;
62 void __iomem *base;
63 unsigned timeout;
64 unsigned long status;
65 struct timer_list timer; /* Pings the watchdog when closed */
66 } imx2_wdt;
68 static struct miscdevice imx2_wdt_miscdev;
70 static bool nowayout = WATCHDOG_NOWAYOUT;
71 module_param(nowayout, bool, 0);
72 MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started (default="
73 __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
76 static unsigned timeout = IMX2_WDT_DEFAULT_TIME;
77 module_param(timeout, uint, 0);
78 MODULE_PARM_DESC(timeout, "Watchdog timeout in seconds (default="
79 __MODULE_STRING(IMX2_WDT_DEFAULT_TIME) ")");
81 static const struct watchdog_info imx2_wdt_info = {
82 .identity = "imx2+ watchdog",
83 .options = WDIOF_KEEPALIVEPING | WDIOF_SETTIMEOUT | WDIOF_MAGICCLOSE,
86 static inline void imx2_wdt_setup(void)
88 u16 val = __raw_readw(imx2_wdt.base + IMX2_WDT_WCR);
90 /* Strip the old watchdog Time-Out value */
91 val &= ~IMX2_WDT_WCR_WT;
92 /* Generate reset if WDOG times out */
93 val &= ~IMX2_WDT_WCR_WRE;
94 /* Keep Watchdog Disabled */
95 val &= ~IMX2_WDT_WCR_WDE;
96 /* Set the watchdog's Time-Out value */
97 val |= WDOG_SEC_TO_COUNT(imx2_wdt.timeout);
99 __raw_writew(val, imx2_wdt.base + IMX2_WDT_WCR);
101 /* enable the watchdog */
102 val |= IMX2_WDT_WCR_WDE;
103 __raw_writew(val, imx2_wdt.base + IMX2_WDT_WCR);
106 static inline void imx2_wdt_ping(void)
108 __raw_writew(IMX2_WDT_SEQ1, imx2_wdt.base + IMX2_WDT_WSR);
109 __raw_writew(IMX2_WDT_SEQ2, imx2_wdt.base + IMX2_WDT_WSR);
112 static void imx2_wdt_timer_ping(unsigned long arg)
114 /* ping it every imx2_wdt.timeout / 2 seconds to prevent reboot */
115 imx2_wdt_ping();
116 mod_timer(&imx2_wdt.timer, jiffies + imx2_wdt.timeout * HZ / 2);
119 static void imx2_wdt_start(void)
121 if (!test_and_set_bit(IMX2_WDT_STATUS_STARTED, &imx2_wdt.status)) {
122 /* at our first start we enable clock and do initialisations */
123 clk_prepare_enable(imx2_wdt.clk);
125 imx2_wdt_setup();
126 } else /* delete the timer that pings the watchdog after close */
127 del_timer_sync(&imx2_wdt.timer);
129 /* Watchdog is enabled - time to reload the timeout value */
130 imx2_wdt_ping();
133 static void imx2_wdt_stop(void)
135 /* we don't need a clk_disable, it cannot be disabled once started.
136 * We use a timer to ping the watchdog while /dev/watchdog is closed */
137 imx2_wdt_timer_ping(0);
140 static void imx2_wdt_set_timeout(int new_timeout)
142 u16 val = __raw_readw(imx2_wdt.base + IMX2_WDT_WCR);
144 /* set the new timeout value in the WSR */
145 val &= ~IMX2_WDT_WCR_WT;
146 val |= WDOG_SEC_TO_COUNT(new_timeout);
147 __raw_writew(val, imx2_wdt.base + IMX2_WDT_WCR);
150 static int imx2_wdt_open(struct inode *inode, struct file *file)
152 if (test_and_set_bit(IMX2_WDT_STATUS_OPEN, &imx2_wdt.status))
153 return -EBUSY;
155 imx2_wdt_start();
156 return nonseekable_open(inode, file);
159 static int imx2_wdt_close(struct inode *inode, struct file *file)
161 if (test_bit(IMX2_WDT_EXPECT_CLOSE, &imx2_wdt.status) && !nowayout)
162 imx2_wdt_stop();
163 else {
164 dev_crit(imx2_wdt_miscdev.parent,
165 "Unexpected close: Expect reboot!\n");
166 imx2_wdt_ping();
169 clear_bit(IMX2_WDT_EXPECT_CLOSE, &imx2_wdt.status);
170 clear_bit(IMX2_WDT_STATUS_OPEN, &imx2_wdt.status);
171 return 0;
174 static long imx2_wdt_ioctl(struct file *file, unsigned int cmd,
175 unsigned long arg)
177 void __user *argp = (void __user *)arg;
178 int __user *p = argp;
179 int new_value;
180 u16 val;
182 switch (cmd) {
183 case WDIOC_GETSUPPORT:
184 return copy_to_user(argp, &imx2_wdt_info,
185 sizeof(struct watchdog_info)) ? -EFAULT : 0;
187 case WDIOC_GETSTATUS:
188 return put_user(0, p);
190 case WDIOC_GETBOOTSTATUS:
191 val = __raw_readw(imx2_wdt.base + IMX2_WDT_WRSR);
192 new_value = val & IMX2_WDT_WRSR_TOUT ? WDIOF_CARDRESET : 0;
193 return put_user(new_value, p);
195 case WDIOC_KEEPALIVE:
196 imx2_wdt_ping();
197 return 0;
199 case WDIOC_SETTIMEOUT:
200 if (get_user(new_value, p))
201 return -EFAULT;
202 if ((new_value < 1) || (new_value > IMX2_WDT_MAX_TIME))
203 return -EINVAL;
204 imx2_wdt_set_timeout(new_value);
205 imx2_wdt.timeout = new_value;
206 imx2_wdt_ping();
208 /* Fallthrough to return current value */
209 case WDIOC_GETTIMEOUT:
210 return put_user(imx2_wdt.timeout, p);
212 default:
213 return -ENOTTY;
217 static ssize_t imx2_wdt_write(struct file *file, const char __user *data,
218 size_t len, loff_t *ppos)
220 size_t i;
221 char c;
223 if (len == 0) /* Can we see this even ? */
224 return 0;
226 clear_bit(IMX2_WDT_EXPECT_CLOSE, &imx2_wdt.status);
227 /* scan to see whether or not we got the magic character */
228 for (i = 0; i != len; i++) {
229 if (get_user(c, data + i))
230 return -EFAULT;
231 if (c == 'V')
232 set_bit(IMX2_WDT_EXPECT_CLOSE, &imx2_wdt.status);
235 imx2_wdt_ping();
236 return len;
239 static const struct file_operations imx2_wdt_fops = {
240 .owner = THIS_MODULE,
241 .llseek = no_llseek,
242 .unlocked_ioctl = imx2_wdt_ioctl,
243 .open = imx2_wdt_open,
244 .release = imx2_wdt_close,
245 .write = imx2_wdt_write,
248 static struct miscdevice imx2_wdt_miscdev = {
249 .minor = WATCHDOG_MINOR,
250 .name = "watchdog",
251 .fops = &imx2_wdt_fops,
254 static int __init imx2_wdt_probe(struct platform_device *pdev)
256 int ret;
257 struct resource *res;
259 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
260 if (!res) {
261 dev_err(&pdev->dev, "can't get device resources\n");
262 return -ENODEV;
265 imx2_wdt.base = devm_request_and_ioremap(&pdev->dev, res);
266 if (!imx2_wdt.base) {
267 dev_err(&pdev->dev, "ioremap failed\n");
268 return -ENOMEM;
271 imx2_wdt.clk = clk_get(&pdev->dev, NULL);
272 if (IS_ERR(imx2_wdt.clk)) {
273 dev_err(&pdev->dev, "can't get Watchdog clock\n");
274 return PTR_ERR(imx2_wdt.clk);
277 imx2_wdt.timeout = clamp_t(unsigned, timeout, 1, IMX2_WDT_MAX_TIME);
278 if (imx2_wdt.timeout != timeout)
279 dev_warn(&pdev->dev, "Initial timeout out of range! "
280 "Clamped from %u to %u\n", timeout, imx2_wdt.timeout);
282 setup_timer(&imx2_wdt.timer, imx2_wdt_timer_ping, 0);
284 imx2_wdt_miscdev.parent = &pdev->dev;
285 ret = misc_register(&imx2_wdt_miscdev);
286 if (ret)
287 goto fail;
289 dev_info(&pdev->dev,
290 "IMX2+ Watchdog Timer enabled. timeout=%ds (nowayout=%d)\n",
291 imx2_wdt.timeout, nowayout);
292 return 0;
294 fail:
295 imx2_wdt_miscdev.parent = NULL;
296 clk_put(imx2_wdt.clk);
297 return ret;
300 static int __exit imx2_wdt_remove(struct platform_device *pdev)
302 misc_deregister(&imx2_wdt_miscdev);
304 if (test_bit(IMX2_WDT_STATUS_STARTED, &imx2_wdt.status)) {
305 del_timer_sync(&imx2_wdt.timer);
307 dev_crit(imx2_wdt_miscdev.parent,
308 "Device removed: Expect reboot!\n");
309 } else
310 clk_put(imx2_wdt.clk);
312 imx2_wdt_miscdev.parent = NULL;
313 return 0;
316 static void imx2_wdt_shutdown(struct platform_device *pdev)
318 if (test_bit(IMX2_WDT_STATUS_STARTED, &imx2_wdt.status)) {
319 /* we are running, we need to delete the timer but will give
320 * max timeout before reboot will take place */
321 del_timer_sync(&imx2_wdt.timer);
322 imx2_wdt_set_timeout(IMX2_WDT_MAX_TIME);
323 imx2_wdt_ping();
325 dev_crit(imx2_wdt_miscdev.parent,
326 "Device shutdown: Expect reboot!\n");
330 static const struct of_device_id imx2_wdt_dt_ids[] = {
331 { .compatible = "fsl,imx21-wdt", },
332 { /* sentinel */ }
335 static struct platform_driver imx2_wdt_driver = {
336 .remove = __exit_p(imx2_wdt_remove),
337 .shutdown = imx2_wdt_shutdown,
338 .driver = {
339 .name = DRIVER_NAME,
340 .owner = THIS_MODULE,
341 .of_match_table = imx2_wdt_dt_ids,
345 static int __init imx2_wdt_init(void)
347 return platform_driver_probe(&imx2_wdt_driver, imx2_wdt_probe);
349 module_init(imx2_wdt_init);
351 static void __exit imx2_wdt_exit(void)
353 platform_driver_unregister(&imx2_wdt_driver);
355 module_exit(imx2_wdt_exit);
357 MODULE_AUTHOR("Wolfram Sang");
358 MODULE_DESCRIPTION("Watchdog driver for IMX2 and later");
359 MODULE_LICENSE("GPL v2");
360 MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR);
361 MODULE_ALIAS("platform:" DRIVER_NAME);