2 * linux/drivers/watchdog/omap_wdt.c
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@redhat.com>
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 #include <linux/module.h>
30 #include <linux/types.h>
31 #include <linux/kernel.h>
34 #include <linux/miscdevice.h>
35 #include <linux/watchdog.h>
36 #include <linux/reboot.h>
37 #include <linux/init.h>
38 #include <linux/err.h>
39 #include <linux/platform_device.h>
40 #include <linux/moduleparam.h>
41 #include <linux/clk.h>
44 #include <asm/uaccess.h>
45 #include <asm/hardware.h>
46 #include <asm/bitops.h>
48 #include <asm/arch/prcm.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;
60 void __iomem *base; /* physical */
63 struct clk *armwdt_ck;
64 struct clk *mpu_wdt_ick;
65 struct clk *mpu_wdt_fck;
67 struct miscdevice omap_wdt_miscdev;
70 static void omap_wdt_ping(struct omap_wdt_dev *wdev)
72 void __iomem *base = wdev->base;
73 /* wait for posted write to complete */
74 while ((omap_readl(base + OMAP_WATCHDOG_WPS)) & 0x08)
76 wdt_trgr_pattern = ~wdt_trgr_pattern;
77 omap_writel(wdt_trgr_pattern, (base + OMAP_WATCHDOG_TGR));
78 /* wait for posted write to complete */
79 while ((omap_readl(base + OMAP_WATCHDOG_WPS)) & 0x08)
81 /* reloaded WCRR from WLDR */
84 static void omap_wdt_enable(struct omap_wdt_dev *wdev)
88 /* Sequence to enable the watchdog */
89 omap_writel(0xBBBB, base + OMAP_WATCHDOG_SPR);
90 while ((omap_readl(base + OMAP_WATCHDOG_WPS)) & 0x10)
92 omap_writel(0x4444, base + OMAP_WATCHDOG_SPR);
93 while ((omap_readl(base + OMAP_WATCHDOG_WPS)) & 0x10)
97 static void omap_wdt_disable(struct omap_wdt_dev *wdev)
101 /* sequence required to disable watchdog */
102 omap_writel(0xAAAA, base + OMAP_WATCHDOG_SPR); /* TIMER_MODE */
103 while (omap_readl(base + OMAP_WATCHDOG_WPS) & 0x10)
105 omap_writel(0x5555, base + OMAP_WATCHDOG_SPR); /* TIMER_MODE */
106 while (omap_readl(base + OMAP_WATCHDOG_WPS) & 0x10)
110 static void omap_wdt_adjust_timeout(unsigned new_timeout)
112 if (new_timeout < TIMER_MARGIN_MIN)
113 new_timeout = TIMER_MARGIN_DEFAULT;
114 if (new_timeout > TIMER_MARGIN_MAX)
115 new_timeout = TIMER_MARGIN_MAX;
116 timer_margin = new_timeout;
119 static void omap_wdt_set_timeout(struct omap_wdt_dev *wdev)
121 u32 pre_margin = GET_WLDR_VAL(timer_margin);
125 /* just count up at 32 KHz */
126 while (omap_readl(base + OMAP_WATCHDOG_WPS) & 0x04)
128 omap_writel(pre_margin, base + OMAP_WATCHDOG_LDR);
129 while (omap_readl(base + OMAP_WATCHDOG_WPS) & 0x04)
134 * Allow only one task to hold it open
137 static int omap_wdt_open(struct inode *inode, struct file *file)
139 struct omap_wdt_dev *wdev;
141 wdev = platform_get_drvdata(omap_wdt_dev);
143 if (test_and_set_bit(1, (unsigned long *)&(wdev->omap_wdt_users)))
146 if (cpu_is_omap16xx())
147 clk_enable(wdev->armwdt_ck); /* Enable the clock */
149 if (cpu_is_omap24xx() || cpu_is_omap34xx()) {
150 clk_enable(wdev->mpu_wdt_ick); /* Enable the interface clock */
151 clk_enable(wdev->mpu_wdt_fck); /* Enable the functional clock */
154 /* initialize prescaler */
155 while (omap_readl(base + OMAP_WATCHDOG_WPS) & 0x01)
157 omap_writel((1 << 5) | (PTV << 2), base + OMAP_WATCHDOG_CNTRL);
158 while (omap_readl(base + OMAP_WATCHDOG_WPS) & 0x01)
161 file->private_data = (void *) wdev;
163 omap_wdt_set_timeout(wdev);
164 omap_wdt_enable(wdev);
165 return nonseekable_open(inode, file);
168 static int omap_wdt_release(struct inode *inode, struct file *file)
170 struct omap_wdt_dev *wdev;
171 wdev = file->private_data;
173 * Shut off the timer unless NOWAYOUT is defined.
175 #ifndef CONFIG_WATCHDOG_NOWAYOUT
177 omap_wdt_disable(wdev);
179 if (cpu_is_omap16xx())
180 clk_disable(wdev->armwdt_ck); /* Disable the clock */
182 if (cpu_is_omap24xx() || cpu_is_omap34xx()) {
183 clk_disable(wdev->mpu_wdt_ick); /* Disable the clock */
184 clk_disable(wdev->mpu_wdt_fck); /* Disable the clock */
187 printk(KERN_CRIT "omap_wdt: Unexpected close, not stopping!\n");
189 wdev->omap_wdt_users = 0;
194 omap_wdt_write(struct file *file, const char __user *data,
195 size_t len, loff_t *ppos)
197 struct omap_wdt_dev *wdev;
198 wdev = file->private_data;
199 /* Refresh LOAD_TIME. */
206 omap_wdt_ioctl(struct inode *inode, struct file *file,
207 unsigned int cmd, unsigned long arg)
209 struct omap_wdt_dev *wdev;
211 static struct watchdog_info ident = {
212 .identity = "OMAP Watchdog",
213 .options = WDIOF_SETTIMEOUT,
214 .firmware_version = 0,
216 wdev = file->private_data;
221 case WDIOC_GETSUPPORT:
222 return copy_to_user((struct watchdog_info __user *)arg, &ident,
224 case WDIOC_GETSTATUS:
225 return put_user(0, (int __user *)arg);
226 case WDIOC_GETBOOTSTATUS:
227 if (cpu_is_omap16xx())
228 return put_user(omap_readw(ARM_SYSST),
230 if (cpu_is_omap24xx())
231 return put_user(omap_prcm_get_reset_sources(),
233 case WDIOC_KEEPALIVE:
236 case WDIOC_SETTIMEOUT:
237 if (get_user(new_margin, (int __user *)arg))
239 omap_wdt_adjust_timeout(new_margin);
241 omap_wdt_disable(wdev);
242 omap_wdt_set_timeout(wdev);
243 omap_wdt_enable(wdev);
247 case WDIOC_GETTIMEOUT:
248 return put_user(timer_margin, (int __user *)arg);
253 static const struct file_operations omap_wdt_fops = {
254 .owner = THIS_MODULE,
255 .write = omap_wdt_write,
256 .ioctl = omap_wdt_ioctl,
257 .open = omap_wdt_open,
258 .release = omap_wdt_release,
262 static int __init omap_wdt_probe(struct platform_device *pdev)
264 struct resource *res, *mem;
266 struct omap_wdt_dev *wdev;
268 /* reserve static register mappings */
269 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
276 mem = request_mem_region(res->start, res->end - res->start + 1,
281 wdev = kzalloc(sizeof(struct omap_wdt_dev), GFP_KERNEL);
286 wdev->omap_wdt_users = 0;
289 if (cpu_is_omap16xx()) {
290 wdev->armwdt_ck = clk_get(&pdev->dev, "armwdt_ck");
291 if (IS_ERR(wdev->armwdt_ck)) {
292 ret = PTR_ERR(wdev->armwdt_ck);
293 wdev->armwdt_ck = NULL;
298 if (cpu_is_omap24xx()) {
299 wdev->mpu_wdt_ick = clk_get(&pdev->dev, "mpu_wdt_ick");
300 if (IS_ERR(wdev->mpu_wdt_ick)) {
301 ret = PTR_ERR(wdev->mpu_wdt_ick);
302 wdev->mpu_wdt_ick = NULL;
305 wdev->mpu_wdt_fck = clk_get(&pdev->dev, "mpu_wdt_fck");
306 if (IS_ERR(wdev->mpu_wdt_fck)) {
307 ret = PTR_ERR(wdev->mpu_wdt_fck);
308 wdev->mpu_wdt_fck = NULL;
313 if (cpu_is_omap34xx()) {
314 wdev->mpu_wdt_ick = clk_get(&pdev->dev, "wdt2_ick");
315 if (IS_ERR(wdev->mpu_wdt_ick)) {
316 ret = PTR_ERR(wdev->mpu_wdt_ick);
317 wdev->mpu_wdt_ick = NULL;
320 wdev->mpu_wdt_fck = clk_get(&pdev->dev, "wdt2_fck");
321 if (IS_ERR(wdev->mpu_wdt_fck)) {
322 ret = PTR_ERR(wdev->mpu_wdt_fck);
323 wdev->mpu_wdt_fck = NULL;
327 wdev->base = (void __iomem *) (mem->start);
328 platform_set_drvdata(pdev, wdev);
330 omap_wdt_disable(wdev);
331 omap_wdt_adjust_timeout(timer_margin);
333 wdev->omap_wdt_miscdev.parent = &pdev->dev;
334 wdev->omap_wdt_miscdev.minor = WATCHDOG_MINOR;
335 wdev->omap_wdt_miscdev.name = "watchdog";
336 wdev->omap_wdt_miscdev.fops = &omap_wdt_fops;
338 ret = misc_register(&(wdev->omap_wdt_miscdev));
342 pr_info("OMAP Watchdog Timer Rev 0x%02x: initial timeout %d sec\n",
343 omap_readl(wdev->base + OMAP_WATCHDOG_REV) & 0xFF,
346 /* autogate OCP interface clock */
347 omap_writel(0x01, wdev->base + OMAP_WATCHDOG_SYS_CONFIG);
355 platform_set_drvdata(pdev, NULL);
357 clk_put(wdev->armwdt_ck);
358 if (wdev->mpu_wdt_ick)
359 clk_put(wdev->mpu_wdt_ick);
360 if (wdev->mpu_wdt_fck)
361 clk_put(wdev->mpu_wdt_fck);
365 release_resource(mem);
370 static void omap_wdt_shutdown(struct platform_device *pdev)
372 struct omap_wdt_dev *wdev;
373 wdev = platform_get_drvdata(pdev);
375 if (wdev->omap_wdt_users)
376 omap_wdt_disable(wdev);
379 static int omap_wdt_remove(struct platform_device *pdev)
381 struct omap_wdt_dev *wdev;
382 wdev = platform_get_drvdata(pdev);
384 misc_deregister(&(wdev->omap_wdt_miscdev));
385 release_resource(wdev->mem);
386 platform_set_drvdata(pdev, NULL);
387 if (wdev->armwdt_ck) {
388 clk_put(wdev->armwdt_ck);
389 wdev->armwdt_ck = NULL;
391 if (wdev->mpu_wdt_ick) {
392 clk_put(wdev->mpu_wdt_ick);
393 wdev->mpu_wdt_ick = NULL;
395 if (wdev->mpu_wdt_fck) {
396 clk_put(wdev->mpu_wdt_fck);
397 wdev->mpu_wdt_fck = NULL;
406 /* REVISIT ... not clear this is the best way to handle system suspend; and
407 * it's very inappropriate for selective device suspend (e.g. suspending this
408 * through sysfs rather than by stopping the watchdog daemon). Also, this
409 * may not play well enough with NOWAYOUT...
412 static int omap_wdt_suspend(struct platform_device *pdev, pm_message_t state)
414 struct omap_wdt_dev *wdev;
415 wdev = platform_get_drvdata(pdev);
416 if (wdev->omap_wdt_users)
417 omap_wdt_disable(wdev);
421 static int omap_wdt_resume(struct platform_device *pdev)
423 struct omap_wdt_dev *wdev;
424 wdev = platform_get_drvdata(pdev);
425 if (wdev->omap_wdt_users) {
426 omap_wdt_enable(wdev);
433 #define omap_wdt_suspend NULL
434 #define omap_wdt_resume NULL
437 static struct platform_driver omap_wdt_driver = {
438 .probe = omap_wdt_probe,
439 .remove = omap_wdt_remove,
440 .shutdown = omap_wdt_shutdown,
441 .suspend = omap_wdt_suspend,
442 .resume = omap_wdt_resume,
444 .owner = THIS_MODULE,
449 static int __init omap_wdt_init(void)
451 return platform_driver_register(&omap_wdt_driver);
454 static void __exit omap_wdt_exit(void)
456 platform_driver_unregister(&omap_wdt_driver);
459 module_init(omap_wdt_init);
460 module_exit(omap_wdt_exit);
462 MODULE_AUTHOR("George G. Davis");
463 MODULE_LICENSE("GPL");
464 MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR);
465 MODULE_ALIAS("platform:omap_wdt");