Commit | Line | Data |
---|---|---|
6f702fce AN |
1 | /* |
2 | * txx9wdt: A Hardware Watchdog Driver for TXx9 SoCs | |
3 | * | |
4 | * Copyright (C) 2007 Atsushi Nemoto <anemo@mba.ocn.ne.jp> | |
5 | * | |
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. | |
9 | */ | |
27c766aa JP |
10 | |
11 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt | |
12 | ||
6f702fce AN |
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> | |
18 | #include <linux/fs.h> | |
6f702fce AN |
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> | |
24 | #include <linux/io.h> | |
25 | #include <asm/txx9tmr.h> | |
26 | ||
27 | #define TIMER_MARGIN 60 /* Default is 60 seconds */ | |
28 | ||
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) ")"); | |
35 | ||
86a1e189 WVS |
36 | static bool nowayout = WATCHDOG_NOWAYOUT; |
37 | module_param(nowayout, bool, 0); | |
6f702fce AN |
38 | MODULE_PARM_DESC(nowayout, |
39 | "Watchdog cannot be stopped once started " | |
40 | "(default=" __MODULE_STRING(WATCHDOG_NOWAYOUT) ")"); | |
41 | ||
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) | |
45 | ||
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; | |
f8494e06 | 50 | static DEFINE_SPINLOCK(txx9_lock); |
6f702fce AN |
51 | |
52 | static void txx9wdt_ping(void) | |
53 | { | |
8dc244f7 | 54 | spin_lock(&txx9_lock); |
6f702fce | 55 | __raw_writel(TXx9_TMWTMR_TWIE | TXx9_TMWTMR_TWC, &txx9wdt_reg->wtmr); |
8dc244f7 | 56 | spin_unlock(&txx9_lock); |
6f702fce AN |
57 | } |
58 | ||
59 | static void txx9wdt_start(void) | |
60 | { | |
8dc244f7 | 61 | spin_lock(&txx9_lock); |
6f702fce AN |
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, | |
66 | &txx9wdt_reg->tcr); | |
67 | __raw_writel(TXx9_TMWTMR_TWIE | TXx9_TMWTMR_TWC, &txx9wdt_reg->wtmr); | |
8dc244f7 | 68 | spin_unlock(&txx9_lock); |
6f702fce AN |
69 | } |
70 | ||
71 | static void txx9wdt_stop(void) | |
72 | { | |
8dc244f7 | 73 | spin_lock(&txx9_lock); |
6f702fce AN |
74 | __raw_writel(TXx9_TMWTMR_WDIS, &txx9wdt_reg->wtmr); |
75 | __raw_writel(__raw_readl(&txx9wdt_reg->tcr) & ~TXx9_TMTCR_TCE, | |
76 | &txx9wdt_reg->tcr); | |
8dc244f7 | 77 | spin_unlock(&txx9_lock); |
6f702fce AN |
78 | } |
79 | ||
80 | static int txx9wdt_open(struct inode *inode, struct file *file) | |
81 | { | |
82 | if (test_and_set_bit(0, &txx9wdt_alive)) | |
83 | return -EBUSY; | |
84 | ||
85 | if (__raw_readl(&txx9wdt_reg->tcr) & TXx9_TMTCR_TCE) { | |
86 | clear_bit(0, &txx9wdt_alive); | |
87 | return -EBUSY; | |
88 | } | |
89 | ||
90 | if (nowayout) | |
91 | __module_get(THIS_MODULE); | |
92 | ||
93 | txx9wdt_start(); | |
94 | return nonseekable_open(inode, file); | |
95 | } | |
96 | ||
97 | static int txx9wdt_release(struct inode *inode, struct file *file) | |
98 | { | |
99 | if (expect_close) | |
100 | txx9wdt_stop(); | |
101 | else { | |
27c766aa | 102 | pr_crit("Unexpected close, not stopping watchdog!\n"); |
6f702fce AN |
103 | txx9wdt_ping(); |
104 | } | |
105 | clear_bit(0, &txx9wdt_alive); | |
106 | expect_close = 0; | |
107 | return 0; | |
108 | } | |
109 | ||
110 | static ssize_t txx9wdt_write(struct file *file, const char __user *data, | |
111 | size_t len, loff_t *ppos) | |
112 | { | |
113 | if (len) { | |
114 | if (!nowayout) { | |
115 | size_t i; | |
116 | ||
117 | expect_close = 0; | |
118 | for (i = 0; i != len; i++) { | |
119 | char c; | |
120 | if (get_user(c, data + i)) | |
121 | return -EFAULT; | |
122 | if (c == 'V') | |
123 | expect_close = 1; | |
124 | } | |
125 | } | |
126 | txx9wdt_ping(); | |
127 | } | |
128 | return len; | |
129 | } | |
130 | ||
8dc244f7 AC |
131 | static long txx9wdt_ioctl(struct file *file, unsigned int cmd, |
132 | unsigned long arg) | |
6f702fce AN |
133 | { |
134 | void __user *argp = (void __user *)arg; | |
135 | int __user *p = argp; | |
136 | int new_timeout; | |
8dc244f7 | 137 | static const struct watchdog_info ident = { |
6f702fce AN |
138 | .options = WDIOF_SETTIMEOUT | |
139 | WDIOF_KEEPALIVEPING | | |
140 | WDIOF_MAGICCLOSE, | |
141 | .firmware_version = 0, | |
142 | .identity = "Hardware Watchdog for TXx9", | |
143 | }; | |
144 | ||
145 | switch (cmd) { | |
6f702fce AN |
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: | |
152 | txx9wdt_ping(); | |
153 | return 0; | |
154 | case WDIOC_SETTIMEOUT: | |
155 | if (get_user(new_timeout, p)) | |
156 | return -EFAULT; | |
157 | if (new_timeout < 1 || new_timeout > WD_MAX_TIMEOUT) | |
158 | return -EINVAL; | |
159 | timeout = new_timeout; | |
160 | txx9wdt_stop(); | |
161 | txx9wdt_start(); | |
162 | /* Fall */ | |
163 | case WDIOC_GETTIMEOUT: | |
164 | return put_user(timeout, p); | |
0c06090c WVS |
165 | default: |
166 | return -ENOTTY; | |
6f702fce AN |
167 | } |
168 | } | |
169 | ||
6f702fce | 170 | static const struct file_operations txx9wdt_fops = { |
8dc244f7 AC |
171 | .owner = THIS_MODULE, |
172 | .llseek = no_llseek, | |
173 | .write = txx9wdt_write, | |
174 | .unlocked_ioctl = txx9wdt_ioctl, | |
175 | .open = txx9wdt_open, | |
176 | .release = txx9wdt_release, | |
6f702fce AN |
177 | }; |
178 | ||
179 | static struct miscdevice txx9wdt_miscdev = { | |
8dc244f7 AC |
180 | .minor = WATCHDOG_MINOR, |
181 | .name = "watchdog", | |
182 | .fops = &txx9wdt_fops, | |
6f702fce AN |
183 | }; |
184 | ||
6f702fce AN |
185 | static int __init txx9wdt_probe(struct platform_device *dev) |
186 | { | |
187 | struct resource *res; | |
188 | int ret; | |
189 | ||
190 | txx9_imclk = clk_get(NULL, "imbus_clk"); | |
191 | if (IS_ERR(txx9_imclk)) { | |
192 | ret = PTR_ERR(txx9_imclk); | |
193 | txx9_imclk = NULL; | |
194 | goto exit; | |
195 | } | |
196 | ret = clk_enable(txx9_imclk); | |
197 | if (ret) { | |
198 | clk_put(txx9_imclk); | |
199 | txx9_imclk = NULL; | |
200 | goto exit; | |
201 | } | |
202 | ||
203 | res = platform_get_resource(dev, IORESOURCE_MEM, 0); | |
204 | if (!res) | |
205 | goto exit_busy; | |
b782a563 | 206 | if (!devm_request_mem_region(&dev->dev, res->start, resource_size(res), |
6f702fce AN |
207 | "txx9wdt")) |
208 | goto exit_busy; | |
b782a563 | 209 | txx9wdt_reg = devm_ioremap(&dev->dev, res->start, resource_size(res)); |
6f702fce AN |
210 | if (!txx9wdt_reg) |
211 | goto exit_busy; | |
212 | ||
6f702fce AN |
213 | ret = misc_register(&txx9wdt_miscdev); |
214 | if (ret) { | |
6f702fce AN |
215 | goto exit; |
216 | } | |
217 | ||
27c766aa JP |
218 | pr_info("Hardware Watchdog Timer: timeout=%d sec (max %ld) (nowayout= %d)\n", |
219 | timeout, WD_MAX_TIMEOUT, nowayout); | |
6f702fce AN |
220 | |
221 | return 0; | |
222 | exit_busy: | |
223 | ret = -EBUSY; | |
224 | exit: | |
225 | if (txx9_imclk) { | |
226 | clk_disable(txx9_imclk); | |
227 | clk_put(txx9_imclk); | |
228 | } | |
229 | return ret; | |
230 | } | |
231 | ||
232 | static int __exit txx9wdt_remove(struct platform_device *dev) | |
233 | { | |
234 | misc_deregister(&txx9wdt_miscdev); | |
6f702fce AN |
235 | clk_disable(txx9_imclk); |
236 | clk_put(txx9_imclk); | |
237 | return 0; | |
238 | } | |
239 | ||
168b5251 WVS |
240 | static void txx9wdt_shutdown(struct platform_device *dev) |
241 | { | |
242 | txx9wdt_stop(); | |
243 | } | |
244 | ||
6f702fce AN |
245 | static struct platform_driver txx9wdt_driver = { |
246 | .remove = __exit_p(txx9wdt_remove), | |
168b5251 | 247 | .shutdown = txx9wdt_shutdown, |
6f702fce AN |
248 | .driver = { |
249 | .name = "txx9wdt", | |
250 | .owner = THIS_MODULE, | |
251 | }, | |
252 | }; | |
253 | ||
254 | static int __init watchdog_init(void) | |
255 | { | |
256 | return platform_driver_probe(&txx9wdt_driver, txx9wdt_probe); | |
257 | } | |
258 | ||
259 | static void __exit watchdog_exit(void) | |
260 | { | |
261 | platform_driver_unregister(&txx9wdt_driver); | |
262 | } | |
263 | ||
264 | module_init(watchdog_init); | |
265 | module_exit(watchdog_exit); | |
266 | ||
267 | MODULE_DESCRIPTION("TXx9 Watchdog Driver"); | |
268 | MODULE_LICENSE("GPL"); | |
269 | MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR); | |
f37d193c | 270 | MODULE_ALIAS("platform:txx9wdt"); |