2 * txx9wdt: A Hardware Watchdog Driver for TXx9 SoCs
4 * Copyright (C) 2007 Atsushi Nemoto <anemo@mba.ocn.ne.jp>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
11 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
13 #include <linux/module.h>
14 #include <linux/moduleparam.h>
15 #include <linux/types.h>
16 #include <linux/miscdevice.h>
17 #include <linux/watchdog.h>
19 #include <linux/init.h>
20 #include <linux/uaccess.h>
21 #include <linux/platform_device.h>
22 #include <linux/clk.h>
23 #include <linux/err.h>
25 #include <asm/txx9tmr.h>
27 #define TIMER_MARGIN 60 /* Default is 60 seconds */
29 static int timeout = TIMER_MARGIN; /* in seconds */
30 module_param(timeout, int, 0);
31 MODULE_PARM_DESC(timeout,
32 "Watchdog timeout in seconds. "
33 "(0<timeout<((2^" __MODULE_STRING(TXX9_TIMER_BITS) ")/(IMCLK/256)), "
34 "default=" __MODULE_STRING(TIMER_MARGIN) ")");
36 static bool nowayout = WATCHDOG_NOWAYOUT;
37 module_param(nowayout, bool, 0);
38 MODULE_PARM_DESC(nowayout,
39 "Watchdog cannot be stopped once started "
40 "(default=" __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
42 #define WD_TIMER_CCD 7 /* 1/256 */
43 #define WD_TIMER_CLK (clk_get_rate(txx9_imclk) / (2 << WD_TIMER_CCD))
44 #define WD_MAX_TIMEOUT ((0xffffffff >> (32 - TXX9_TIMER_BITS)) / WD_TIMER_CLK)
46 static unsigned long txx9wdt_alive;
47 static int expect_close;
48 static struct txx9_tmr_reg __iomem *txx9wdt_reg;
49 static struct clk *txx9_imclk;
50 static DEFINE_SPINLOCK(txx9_lock);
52 static void txx9wdt_ping(void)
54 spin_lock(&txx9_lock);
55 __raw_writel(TXx9_TMWTMR_TWIE | TXx9_TMWTMR_TWC, &txx9wdt_reg->wtmr);
56 spin_unlock(&txx9_lock);
59 static void txx9wdt_start(void)
61 spin_lock(&txx9_lock);
62 __raw_writel(WD_TIMER_CLK * timeout, &txx9wdt_reg->cpra);
63 __raw_writel(WD_TIMER_CCD, &txx9wdt_reg->ccdr);
64 __raw_writel(0, &txx9wdt_reg->tisr); /* clear pending interrupt */
65 __raw_writel(TXx9_TMTCR_TCE | TXx9_TMTCR_CCDE | TXx9_TMTCR_TMODE_WDOG,
67 __raw_writel(TXx9_TMWTMR_TWIE | TXx9_TMWTMR_TWC, &txx9wdt_reg->wtmr);
68 spin_unlock(&txx9_lock);
71 static void txx9wdt_stop(void)
73 spin_lock(&txx9_lock);
74 __raw_writel(TXx9_TMWTMR_WDIS, &txx9wdt_reg->wtmr);
75 __raw_writel(__raw_readl(&txx9wdt_reg->tcr) & ~TXx9_TMTCR_TCE,
77 spin_unlock(&txx9_lock);
80 static int txx9wdt_open(struct inode *inode, struct file *file)
82 if (test_and_set_bit(0, &txx9wdt_alive))
85 if (__raw_readl(&txx9wdt_reg->tcr) & TXx9_TMTCR_TCE) {
86 clear_bit(0, &txx9wdt_alive);
91 __module_get(THIS_MODULE);
94 return nonseekable_open(inode, file);
97 static int txx9wdt_release(struct inode *inode, struct file *file)
102 pr_crit("Unexpected close, not stopping watchdog!\n");
105 clear_bit(0, &txx9wdt_alive);
110 static ssize_t txx9wdt_write(struct file *file, const char __user *data,
111 size_t len, loff_t *ppos)
118 for (i = 0; i != len; i++) {
120 if (get_user(c, data + i))
131 static long txx9wdt_ioctl(struct file *file, unsigned int cmd,
134 void __user *argp = (void __user *)arg;
135 int __user *p = argp;
137 static const struct watchdog_info ident = {
138 .options = WDIOF_SETTIMEOUT |
139 WDIOF_KEEPALIVEPING |
141 .firmware_version = 0,
142 .identity = "Hardware Watchdog for TXx9",
146 case WDIOC_GETSUPPORT:
147 return copy_to_user(argp, &ident, sizeof(ident)) ? -EFAULT : 0;
148 case WDIOC_GETSTATUS:
149 case WDIOC_GETBOOTSTATUS:
150 return put_user(0, p);
151 case WDIOC_KEEPALIVE:
154 case WDIOC_SETTIMEOUT:
155 if (get_user(new_timeout, p))
157 if (new_timeout < 1 || new_timeout > WD_MAX_TIMEOUT)
159 timeout = new_timeout;
163 case WDIOC_GETTIMEOUT:
164 return put_user(timeout, p);
170 static const struct file_operations txx9wdt_fops = {
171 .owner = THIS_MODULE,
173 .write = txx9wdt_write,
174 .unlocked_ioctl = txx9wdt_ioctl,
175 .open = txx9wdt_open,
176 .release = txx9wdt_release,
179 static struct miscdevice txx9wdt_miscdev = {
180 .minor = WATCHDOG_MINOR,
182 .fops = &txx9wdt_fops,
185 static int __init txx9wdt_probe(struct platform_device *dev)
187 struct resource *res;
190 txx9_imclk = clk_get(NULL, "imbus_clk");
191 if (IS_ERR(txx9_imclk)) {
192 ret = PTR_ERR(txx9_imclk);
196 ret = clk_enable(txx9_imclk);
203 res = platform_get_resource(dev, IORESOURCE_MEM, 0);
206 if (!devm_request_mem_region(&dev->dev, res->start, resource_size(res),
209 txx9wdt_reg = devm_ioremap(&dev->dev, res->start, resource_size(res));
213 ret = misc_register(&txx9wdt_miscdev);
218 pr_info("Hardware Watchdog Timer: timeout=%d sec (max %ld) (nowayout= %d)\n",
219 timeout, WD_MAX_TIMEOUT, nowayout);
226 clk_disable(txx9_imclk);
232 static int __exit txx9wdt_remove(struct platform_device *dev)
234 misc_deregister(&txx9wdt_miscdev);
235 clk_disable(txx9_imclk);
240 static void txx9wdt_shutdown(struct platform_device *dev)
245 static struct platform_driver txx9wdt_driver = {
246 .remove = __exit_p(txx9wdt_remove),
247 .shutdown = txx9wdt_shutdown,
250 .owner = THIS_MODULE,
254 static int __init watchdog_init(void)
256 return platform_driver_probe(&txx9wdt_driver, txx9wdt_probe);
259 static void __exit watchdog_exit(void)
261 platform_driver_unregister(&txx9wdt_driver);
264 module_init(watchdog_init);
265 module_exit(watchdog_exit);
267 MODULE_DESCRIPTION("TXx9 Watchdog Driver");
268 MODULE_LICENSE("GPL");
269 MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR);
270 MODULE_ALIAS("platform:txx9wdt");