4 * Watchdog driver for the TI OMAP 16xx & 24xx/34xx 32KHz (non-secure) watchdog
6 * Author: MontaVista Software, Inc.
7 * <gdavis@mvista.com> or <source@mvista.com>
9 * 2003 (c) MontaVista Software, Inc. This file is licensed under the
10 * terms of the GNU General Public License version 2. This program is
11 * licensed "as is" without any warranty of any kind, whether express
16 * 20030527: George G. Davis <gdavis@mvista.com>
17 * Initially based on linux-2.4.19-rmk7-pxa1/drivers/char/sa1100_wdt.c
18 * (c) Copyright 2000 Oleg Drokin <green@crimea.edu>
19 * Based on SoftDog driver by Alan Cox <alan@lxorguk.ukuu.org.uk>
21 * Copyright (c) 2004 Texas Instruments.
22 * 1. Modified to support OMAP1610 32-KHz watchdog timer
23 * 2. Ported to 2.6 kernel
25 * Copyright (c) 2005 David Brownell
26 * Use the driver model and standard identifiers; handle bigger timeouts.
29 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
31 #include <linux/module.h>
32 #include <linux/types.h>
33 #include <linux/kernel.h>
36 #include <linux/miscdevice.h>
37 #include <linux/watchdog.h>
38 #include <linux/reboot.h>
39 #include <linux/init.h>
40 #include <linux/err.h>
41 #include <linux/platform_device.h>
42 #include <linux/moduleparam.h>
43 #include <linux/bitops.h>
45 #include <linux/uaccess.h>
46 #include <linux/slab.h>
47 #include <linux/pm_runtime.h>
48 #include <linux/platform_data/omap-wd-timer.h>
52 static struct platform_device *omap_wdt_dev;
54 static unsigned timer_margin;
55 module_param(timer_margin, uint, 0);
56 MODULE_PARM_DESC(timer_margin, "initial watchdog timeout (in seconds)");
58 static unsigned int wdt_trgr_pattern = 0x1234;
59 static DEFINE_SPINLOCK(wdt_lock);
62 void __iomem *base; /* physical */
66 struct miscdevice omap_wdt_miscdev;
69 static void omap_wdt_ping(struct omap_wdt_dev *wdev)
71 void __iomem *base = wdev->base;
73 /* wait for posted write to complete */
74 while ((__raw_readl(base + OMAP_WATCHDOG_WPS)) & 0x08)
77 wdt_trgr_pattern = ~wdt_trgr_pattern;
78 __raw_writel(wdt_trgr_pattern, (base + OMAP_WATCHDOG_TGR));
80 /* wait for posted write to complete */
81 while ((__raw_readl(base + OMAP_WATCHDOG_WPS)) & 0x08)
83 /* reloaded WCRR from WLDR */
86 static void omap_wdt_enable(struct omap_wdt_dev *wdev)
88 void __iomem *base = wdev->base;
90 /* Sequence to enable the watchdog */
91 __raw_writel(0xBBBB, base + OMAP_WATCHDOG_SPR);
92 while ((__raw_readl(base + OMAP_WATCHDOG_WPS)) & 0x10)
95 __raw_writel(0x4444, base + OMAP_WATCHDOG_SPR);
96 while ((__raw_readl(base + OMAP_WATCHDOG_WPS)) & 0x10)
100 static void omap_wdt_disable(struct omap_wdt_dev *wdev)
102 void __iomem *base = wdev->base;
104 /* sequence required to disable watchdog */
105 __raw_writel(0xAAAA, base + OMAP_WATCHDOG_SPR); /* TIMER_MODE */
106 while (__raw_readl(base + OMAP_WATCHDOG_WPS) & 0x10)
109 __raw_writel(0x5555, base + OMAP_WATCHDOG_SPR); /* TIMER_MODE */
110 while (__raw_readl(base + OMAP_WATCHDOG_WPS) & 0x10)
114 static void omap_wdt_adjust_timeout(unsigned new_timeout)
116 if (new_timeout < TIMER_MARGIN_MIN)
117 new_timeout = TIMER_MARGIN_DEFAULT;
118 if (new_timeout > TIMER_MARGIN_MAX)
119 new_timeout = TIMER_MARGIN_MAX;
120 timer_margin = new_timeout;
123 static void omap_wdt_set_timeout(struct omap_wdt_dev *wdev)
125 u32 pre_margin = GET_WLDR_VAL(timer_margin);
126 void __iomem *base = wdev->base;
128 /* just count up at 32 KHz */
129 while (__raw_readl(base + OMAP_WATCHDOG_WPS) & 0x04)
132 __raw_writel(pre_margin, base + OMAP_WATCHDOG_LDR);
133 while (__raw_readl(base + OMAP_WATCHDOG_WPS) & 0x04)
138 * Allow only one task to hold it open
140 static int omap_wdt_open(struct inode *inode, struct file *file)
142 struct omap_wdt_dev *wdev = platform_get_drvdata(omap_wdt_dev);
143 void __iomem *base = wdev->base;
145 if (test_and_set_bit(1, (unsigned long *)&(wdev->omap_wdt_users)))
148 pm_runtime_get_sync(wdev->dev);
150 /* initialize prescaler */
151 while (__raw_readl(base + OMAP_WATCHDOG_WPS) & 0x01)
154 __raw_writel((1 << 5) | (PTV << 2), base + OMAP_WATCHDOG_CNTRL);
155 while (__raw_readl(base + OMAP_WATCHDOG_WPS) & 0x01)
158 file->private_data = (void *) wdev;
160 omap_wdt_set_timeout(wdev);
161 omap_wdt_ping(wdev); /* trigger loading of new timeout value */
162 omap_wdt_enable(wdev);
164 return nonseekable_open(inode, file);
167 static int omap_wdt_release(struct inode *inode, struct file *file)
169 struct omap_wdt_dev *wdev = file->private_data;
172 * Shut off the timer unless NOWAYOUT is defined.
174 #ifndef CONFIG_WATCHDOG_NOWAYOUT
175 omap_wdt_disable(wdev);
177 pm_runtime_put_sync(wdev->dev);
179 pr_crit("Unexpected close, not stopping!\n");
181 wdev->omap_wdt_users = 0;
186 static ssize_t omap_wdt_write(struct file *file, const char __user *data,
187 size_t len, loff_t *ppos)
189 struct omap_wdt_dev *wdev = file->private_data;
191 /* Refresh LOAD_TIME. */
193 spin_lock(&wdt_lock);
195 spin_unlock(&wdt_lock);
200 static long omap_wdt_ioctl(struct file *file, unsigned int cmd,
203 struct omap_wd_timer_platform_data *pdata;
204 struct omap_wdt_dev *wdev;
207 static const struct watchdog_info ident = {
208 .identity = "OMAP Watchdog",
209 .options = WDIOF_SETTIMEOUT,
210 .firmware_version = 0,
213 wdev = file->private_data;
214 pdata = wdev->dev->platform_data;
217 case WDIOC_GETSUPPORT:
218 return copy_to_user((struct watchdog_info __user *)arg, &ident,
220 case WDIOC_GETSTATUS:
221 return put_user(0, (int __user *)arg);
222 case WDIOC_GETBOOTSTATUS:
223 if (!pdata || !pdata->read_reset_sources)
224 return put_user(0, (int __user *)arg);
225 rs = pdata->read_reset_sources();
226 bs = (rs & (1 << OMAP_MPU_WD_RST_SRC_ID_SHIFT)) ?
228 return put_user(bs, (int __user *)arg);
229 case WDIOC_KEEPALIVE:
230 spin_lock(&wdt_lock);
232 spin_unlock(&wdt_lock);
234 case WDIOC_SETTIMEOUT:
235 if (get_user(new_margin, (int __user *)arg))
237 omap_wdt_adjust_timeout(new_margin);
239 spin_lock(&wdt_lock);
240 omap_wdt_disable(wdev);
241 omap_wdt_set_timeout(wdev);
242 omap_wdt_enable(wdev);
245 spin_unlock(&wdt_lock);
247 case WDIOC_GETTIMEOUT:
248 return put_user(timer_margin, (int __user *)arg);
254 static const struct file_operations omap_wdt_fops = {
255 .owner = THIS_MODULE,
256 .write = omap_wdt_write,
257 .unlocked_ioctl = omap_wdt_ioctl,
258 .open = omap_wdt_open,
259 .release = omap_wdt_release,
263 static int omap_wdt_probe(struct platform_device *pdev)
265 struct resource *res, *mem;
266 struct omap_wdt_dev *wdev;
269 /* reserve static register mappings */
270 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
273 goto err_get_resource;
281 mem = request_mem_region(res->start, resource_size(res), pdev->name);
287 wdev = kzalloc(sizeof(struct omap_wdt_dev), GFP_KERNEL);
293 wdev->omap_wdt_users = 0;
295 wdev->dev = &pdev->dev;
297 wdev->base = ioremap(res->start, resource_size(res));
303 platform_set_drvdata(pdev, wdev);
305 pm_runtime_enable(wdev->dev);
306 pm_runtime_get_sync(wdev->dev);
308 omap_wdt_disable(wdev);
309 omap_wdt_adjust_timeout(timer_margin);
311 wdev->omap_wdt_miscdev.parent = &pdev->dev;
312 wdev->omap_wdt_miscdev.minor = WATCHDOG_MINOR;
313 wdev->omap_wdt_miscdev.name = "watchdog";
314 wdev->omap_wdt_miscdev.fops = &omap_wdt_fops;
316 ret = misc_register(&(wdev->omap_wdt_miscdev));
320 pr_info("OMAP Watchdog Timer Rev 0x%02x: initial timeout %d sec\n",
321 __raw_readl(wdev->base + OMAP_WATCHDOG_REV) & 0xFF,
324 pm_runtime_put_sync(wdev->dev);
331 pm_runtime_disable(wdev->dev);
332 platform_set_drvdata(pdev, NULL);
340 release_mem_region(res->start, resource_size(res));
348 static void omap_wdt_shutdown(struct platform_device *pdev)
350 struct omap_wdt_dev *wdev = platform_get_drvdata(pdev);
352 if (wdev->omap_wdt_users) {
353 omap_wdt_disable(wdev);
354 pm_runtime_put_sync(wdev->dev);
358 static int omap_wdt_remove(struct platform_device *pdev)
360 struct omap_wdt_dev *wdev = platform_get_drvdata(pdev);
361 struct resource *res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
363 pm_runtime_disable(wdev->dev);
367 misc_deregister(&(wdev->omap_wdt_miscdev));
368 release_mem_region(res->start, resource_size(res));
369 platform_set_drvdata(pdev, NULL);
381 /* REVISIT ... not clear this is the best way to handle system suspend; and
382 * it's very inappropriate for selective device suspend (e.g. suspending this
383 * through sysfs rather than by stopping the watchdog daemon). Also, this
384 * may not play well enough with NOWAYOUT...
387 static int omap_wdt_suspend(struct platform_device *pdev, pm_message_t state)
389 struct omap_wdt_dev *wdev = platform_get_drvdata(pdev);
391 if (wdev->omap_wdt_users) {
392 omap_wdt_disable(wdev);
393 pm_runtime_put_sync(wdev->dev);
399 static int omap_wdt_resume(struct platform_device *pdev)
401 struct omap_wdt_dev *wdev = platform_get_drvdata(pdev);
403 if (wdev->omap_wdt_users) {
404 pm_runtime_get_sync(wdev->dev);
405 omap_wdt_enable(wdev);
413 #define omap_wdt_suspend NULL
414 #define omap_wdt_resume NULL
417 static const struct of_device_id omap_wdt_of_match[] = {
418 { .compatible = "ti,omap3-wdt", },
421 MODULE_DEVICE_TABLE(of, omap_wdt_of_match);
423 static struct platform_driver omap_wdt_driver = {
424 .probe = omap_wdt_probe,
425 .remove = omap_wdt_remove,
426 .shutdown = omap_wdt_shutdown,
427 .suspend = omap_wdt_suspend,
428 .resume = omap_wdt_resume,
430 .owner = THIS_MODULE,
432 .of_match_table = omap_wdt_of_match,
436 module_platform_driver(omap_wdt_driver);
438 MODULE_AUTHOR("George G. Davis");
439 MODULE_LICENSE("GPL");
440 MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR);
441 MODULE_ALIAS("platform:omap_wdt");