aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/watchdog/txx9wdt.c
diff options
context:
space:
mode:
Diffstat (limited to 'drivers/watchdog/txx9wdt.c')
-rw-r--r--drivers/watchdog/txx9wdt.c184
1 files changed, 53 insertions, 131 deletions
diff --git a/drivers/watchdog/txx9wdt.c b/drivers/watchdog/txx9wdt.c
index 9e9ed7bfabcb..98e16373e640 100644
--- a/drivers/watchdog/txx9wdt.c
+++ b/drivers/watchdog/txx9wdt.c
@@ -7,177 +7,99 @@
7 * it under the terms of the GNU General Public License version 2 as 7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation. 8 * published by the Free Software Foundation.
9 */ 9 */
10
11#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
12
10#include <linux/module.h> 13#include <linux/module.h>
11#include <linux/moduleparam.h> 14#include <linux/moduleparam.h>
12#include <linux/types.h> 15#include <linux/types.h>
13#include <linux/miscdevice.h> 16#include <linux/miscdevice.h>
14#include <linux/watchdog.h> 17#include <linux/watchdog.h>
15#include <linux/fs.h>
16#include <linux/init.h> 18#include <linux/init.h>
17#include <linux/uaccess.h>
18#include <linux/platform_device.h> 19#include <linux/platform_device.h>
19#include <linux/clk.h> 20#include <linux/clk.h>
20#include <linux/err.h> 21#include <linux/err.h>
21#include <linux/io.h> 22#include <linux/io.h>
22#include <asm/txx9tmr.h> 23#include <asm/txx9tmr.h>
23 24
25#define WD_TIMER_CCD 7 /* 1/256 */
26#define WD_TIMER_CLK (clk_get_rate(txx9_imclk) / (2 << WD_TIMER_CCD))
27#define WD_MAX_TIMEOUT ((0xffffffff >> (32 - TXX9_TIMER_BITS)) / WD_TIMER_CLK)
24#define TIMER_MARGIN 60 /* Default is 60 seconds */ 28#define TIMER_MARGIN 60 /* Default is 60 seconds */
25 29
26static int timeout = TIMER_MARGIN; /* in seconds */ 30static unsigned int timeout = TIMER_MARGIN; /* in seconds */
27module_param(timeout, int, 0); 31module_param(timeout, uint, 0);
28MODULE_PARM_DESC(timeout, 32MODULE_PARM_DESC(timeout,
29 "Watchdog timeout in seconds. " 33 "Watchdog timeout in seconds. "
30 "(0<timeout<((2^" __MODULE_STRING(TXX9_TIMER_BITS) ")/(IMCLK/256)), " 34 "(0<timeout<((2^" __MODULE_STRING(TXX9_TIMER_BITS) ")/(IMCLK/256)), "
31 "default=" __MODULE_STRING(TIMER_MARGIN) ")"); 35 "default=" __MODULE_STRING(TIMER_MARGIN) ")");
32 36
33static int nowayout = WATCHDOG_NOWAYOUT; 37static bool nowayout = WATCHDOG_NOWAYOUT;
34module_param(nowayout, int, 0); 38module_param(nowayout, bool, 0);
35MODULE_PARM_DESC(nowayout, 39MODULE_PARM_DESC(nowayout,
36 "Watchdog cannot be stopped once started " 40 "Watchdog cannot be stopped once started "
37 "(default=" __MODULE_STRING(WATCHDOG_NOWAYOUT) ")"); 41 "(default=" __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
38 42
39#define WD_TIMER_CCD 7 /* 1/256 */
40#define WD_TIMER_CLK (clk_get_rate(txx9_imclk) / (2 << WD_TIMER_CCD))
41#define WD_MAX_TIMEOUT ((0xffffffff >> (32 - TXX9_TIMER_BITS)) / WD_TIMER_CLK)
42
43static unsigned long txx9wdt_alive;
44static int expect_close;
45static struct txx9_tmr_reg __iomem *txx9wdt_reg; 43static struct txx9_tmr_reg __iomem *txx9wdt_reg;
46static struct clk *txx9_imclk; 44static struct clk *txx9_imclk;
47static DEFINE_SPINLOCK(txx9_lock); 45static DEFINE_SPINLOCK(txx9_lock);
48 46
49static void txx9wdt_ping(void) 47static int txx9wdt_ping(struct watchdog_device *wdt_dev)
50{ 48{
51 spin_lock(&txx9_lock); 49 spin_lock(&txx9_lock);
52 __raw_writel(TXx9_TMWTMR_TWIE | TXx9_TMWTMR_TWC, &txx9wdt_reg->wtmr); 50 __raw_writel(TXx9_TMWTMR_TWIE | TXx9_TMWTMR_TWC, &txx9wdt_reg->wtmr);
53 spin_unlock(&txx9_lock); 51 spin_unlock(&txx9_lock);
52 return 0;
54} 53}
55 54
56static void txx9wdt_start(void) 55static int txx9wdt_start(struct watchdog_device *wdt_dev)
57{ 56{
58 spin_lock(&txx9_lock); 57 spin_lock(&txx9_lock);
59 __raw_writel(WD_TIMER_CLK * timeout, &txx9wdt_reg->cpra); 58 __raw_writel(WD_TIMER_CLK * wdt_dev->timeout, &txx9wdt_reg->cpra);
60 __raw_writel(WD_TIMER_CCD, &txx9wdt_reg->ccdr); 59 __raw_writel(WD_TIMER_CCD, &txx9wdt_reg->ccdr);
61 __raw_writel(0, &txx9wdt_reg->tisr); /* clear pending interrupt */ 60 __raw_writel(0, &txx9wdt_reg->tisr); /* clear pending interrupt */
62 __raw_writel(TXx9_TMTCR_TCE | TXx9_TMTCR_CCDE | TXx9_TMTCR_TMODE_WDOG, 61 __raw_writel(TXx9_TMTCR_TCE | TXx9_TMTCR_CCDE | TXx9_TMTCR_TMODE_WDOG,
63 &txx9wdt_reg->tcr); 62 &txx9wdt_reg->tcr);
64 __raw_writel(TXx9_TMWTMR_TWIE | TXx9_TMWTMR_TWC, &txx9wdt_reg->wtmr); 63 __raw_writel(TXx9_TMWTMR_TWIE | TXx9_TMWTMR_TWC, &txx9wdt_reg->wtmr);
65 spin_unlock(&txx9_lock); 64 spin_unlock(&txx9_lock);
65 return 0;
66} 66}
67 67
68static void txx9wdt_stop(void) 68static int txx9wdt_stop(struct watchdog_device *wdt_dev)
69{ 69{
70 spin_lock(&txx9_lock); 70 spin_lock(&txx9_lock);
71 __raw_writel(TXx9_TMWTMR_WDIS, &txx9wdt_reg->wtmr); 71 __raw_writel(TXx9_TMWTMR_WDIS, &txx9wdt_reg->wtmr);
72 __raw_writel(__raw_readl(&txx9wdt_reg->tcr) & ~TXx9_TMTCR_TCE, 72 __raw_writel(__raw_readl(&txx9wdt_reg->tcr) & ~TXx9_TMTCR_TCE,
73 &txx9wdt_reg->tcr); 73 &txx9wdt_reg->tcr);
74 spin_unlock(&txx9_lock); 74 spin_unlock(&txx9_lock);
75}
76
77static int txx9wdt_open(struct inode *inode, struct file *file)
78{
79 if (test_and_set_bit(0, &txx9wdt_alive))
80 return -EBUSY;
81
82 if (__raw_readl(&txx9wdt_reg->tcr) & TXx9_TMTCR_TCE) {
83 clear_bit(0, &txx9wdt_alive);
84 return -EBUSY;
85 }
86
87 if (nowayout)
88 __module_get(THIS_MODULE);
89
90 txx9wdt_start();
91 return nonseekable_open(inode, file);
92}
93
94static int txx9wdt_release(struct inode *inode, struct file *file)
95{
96 if (expect_close)
97 txx9wdt_stop();
98 else {
99 printk(KERN_CRIT "txx9wdt: "
100 "Unexpected close, not stopping watchdog!\n");
101 txx9wdt_ping();
102 }
103 clear_bit(0, &txx9wdt_alive);
104 expect_close = 0;
105 return 0; 75 return 0;
106} 76}
107 77
108static ssize_t txx9wdt_write(struct file *file, const char __user *data, 78static int txx9wdt_set_timeout(struct watchdog_device *wdt_dev,
109 size_t len, loff_t *ppos) 79 unsigned int new_timeout)
110{ 80{
111 if (len) { 81 wdt_dev->timeout = new_timeout;
112 if (!nowayout) { 82 txx9wdt_stop(wdt_dev);
113 size_t i; 83 txx9wdt_start(wdt_dev);
114 84 return 0;
115 expect_close = 0;
116 for (i = 0; i != len; i++) {
117 char c;
118 if (get_user(c, data + i))
119 return -EFAULT;
120 if (c == 'V')
121 expect_close = 1;
122 }
123 }
124 txx9wdt_ping();
125 }
126 return len;
127} 85}
128 86
129static long txx9wdt_ioctl(struct file *file, unsigned int cmd, 87static const struct watchdog_info txx9wdt_info = {
130 unsigned long arg) 88 .options = WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING | WDIOF_MAGICCLOSE,
131{ 89 .identity = "Hardware Watchdog for TXx9",
132 void __user *argp = (void __user *)arg; 90};
133 int __user *p = argp;
134 int new_timeout;
135 static const struct watchdog_info ident = {
136 .options = WDIOF_SETTIMEOUT |
137 WDIOF_KEEPALIVEPING |
138 WDIOF_MAGICCLOSE,
139 .firmware_version = 0,
140 .identity = "Hardware Watchdog for TXx9",
141 };
142
143 switch (cmd) {
144 case WDIOC_GETSUPPORT:
145 return copy_to_user(argp, &ident, sizeof(ident)) ? -EFAULT : 0;
146 case WDIOC_GETSTATUS:
147 case WDIOC_GETBOOTSTATUS:
148 return put_user(0, p);
149 case WDIOC_KEEPALIVE:
150 txx9wdt_ping();
151 return 0;
152 case WDIOC_SETTIMEOUT:
153 if (get_user(new_timeout, p))
154 return -EFAULT;
155 if (new_timeout < 1 || new_timeout > WD_MAX_TIMEOUT)
156 return -EINVAL;
157 timeout = new_timeout;
158 txx9wdt_stop();
159 txx9wdt_start();
160 /* Fall */
161 case WDIOC_GETTIMEOUT:
162 return put_user(timeout, p);
163 default:
164 return -ENOTTY;
165 }
166}
167 91
168static const struct file_operations txx9wdt_fops = { 92static const struct watchdog_ops txx9wdt_ops = {
169 .owner = THIS_MODULE, 93 .owner = THIS_MODULE,
170 .llseek = no_llseek, 94 .start = txx9wdt_start,
171 .write = txx9wdt_write, 95 .stop = txx9wdt_stop,
172 .unlocked_ioctl = txx9wdt_ioctl, 96 .ping = txx9wdt_ping,
173 .open = txx9wdt_open, 97 .set_timeout = txx9wdt_set_timeout,
174 .release = txx9wdt_release,
175}; 98};
176 99
177static struct miscdevice txx9wdt_miscdev = { 100static struct watchdog_device txx9wdt = {
178 .minor = WATCHDOG_MINOR, 101 .info = &txx9wdt_info,
179 .name = "watchdog", 102 .ops = &txx9wdt_ops,
180 .fops = &txx9wdt_fops,
181}; 103};
182 104
183static int __init txx9wdt_probe(struct platform_device *dev) 105static int __init txx9wdt_probe(struct platform_device *dev)
@@ -199,27 +121,27 @@ static int __init txx9wdt_probe(struct platform_device *dev)
199 } 121 }
200 122
201 res = platform_get_resource(dev, IORESOURCE_MEM, 0); 123 res = platform_get_resource(dev, IORESOURCE_MEM, 0);
202 if (!res) 124 txx9wdt_reg = devm_request_and_ioremap(&dev->dev, res);
203 goto exit_busy; 125 if (!txx9wdt_reg) {
204 if (!devm_request_mem_region(&dev->dev, res->start, resource_size(res), 126 ret = -EBUSY;
205 "txx9wdt"))
206 goto exit_busy;
207 txx9wdt_reg = devm_ioremap(&dev->dev, res->start, resource_size(res));
208 if (!txx9wdt_reg)
209 goto exit_busy;
210
211 ret = misc_register(&txx9wdt_miscdev);
212 if (ret) {
213 goto exit; 127 goto exit;
214 } 128 }
215 129
216 printk(KERN_INFO "Hardware Watchdog Timer for TXx9: " 130 if (timeout < 1 || timeout > WD_MAX_TIMEOUT)
217 "timeout=%d sec (max %ld) (nowayout= %d)\n", 131 timeout = TIMER_MARGIN;
218 timeout, WD_MAX_TIMEOUT, nowayout); 132 txx9wdt.timeout = timeout;
133 txx9wdt.min_timeout = 1;
134 txx9wdt.max_timeout = WD_MAX_TIMEOUT;
135 watchdog_set_nowayout(&txx9wdt, nowayout);
136
137 ret = watchdog_register_device(&txx9wdt);
138 if (ret)
139 goto exit;
140
141 pr_info("Hardware Watchdog Timer: timeout=%d sec (max %ld) (nowayout= %d)\n",
142 timeout, WD_MAX_TIMEOUT, nowayout);
219 143
220 return 0; 144 return 0;
221exit_busy:
222 ret = -EBUSY;
223exit: 145exit:
224 if (txx9_imclk) { 146 if (txx9_imclk) {
225 clk_disable(txx9_imclk); 147 clk_disable(txx9_imclk);
@@ -230,7 +152,7 @@ exit:
230 152
231static int __exit txx9wdt_remove(struct platform_device *dev) 153static int __exit txx9wdt_remove(struct platform_device *dev)
232{ 154{
233 misc_deregister(&txx9wdt_miscdev); 155 watchdog_unregister_device(&txx9wdt);
234 clk_disable(txx9_imclk); 156 clk_disable(txx9_imclk);
235 clk_put(txx9_imclk); 157 clk_put(txx9_imclk);
236 return 0; 158 return 0;
@@ -238,7 +160,7 @@ static int __exit txx9wdt_remove(struct platform_device *dev)
238 160
239static void txx9wdt_shutdown(struct platform_device *dev) 161static void txx9wdt_shutdown(struct platform_device *dev)
240{ 162{
241 txx9wdt_stop(); 163 txx9wdt_stop(&txx9wdt);
242} 164}
243 165
244static struct platform_driver txx9wdt_driver = { 166static struct platform_driver txx9wdt_driver = {