d5e1f8a3ca92282f595c72a8763a0b75baec9075
[openwrt.git] / target / linux / ar71xx / files / drivers / watchdog / ar71xx_wdt.c
1 /*
2 * Driver for the Atheros AR71xx SoC's built-in hardware watchdog timer.
3 *
4 * Copyright (C) 2010-2011 Jaiganesh Narayanan <jnarayanan@atheros.com>
5 * Copyright (C) 2008 Gabor Juhos <juhosg@openwrt.org>
6 * Copyright (C) 2008 Imre Kaloz <kaloz@openwrt.org>
7 *
8 * Parts of this file are based on Atheros 2.6.31 BSP
9 *
10 * This driver was based on: drivers/watchdog/ixp4xx_wdt.c
11 * Author: Deepak Saxena <dsaxena@plexity.net>
12 * Copyright 2004 (c) MontaVista, Software, Inc.
13 *
14 * which again was based on sa1100 driver,
15 * Copyright (C) 2000 Oleg Drokin <green@crimea.edu>
16 *
17 * This program is free software; you can redistribute it and/or modify it
18 * under the terms of the GNU General Public License version 2 as published
19 * by the Free Software Foundation.
20 *
21 */
22
23 #include <linux/bitops.h>
24 #include <linux/errno.h>
25 #include <linux/fs.h>
26 #include <linux/init.h>
27 #include <linux/kernel.h>
28 #include <linux/miscdevice.h>
29 #include <linux/module.h>
30 #include <linux/moduleparam.h>
31 #include <linux/platform_device.h>
32 #include <linux/types.h>
33 #include <linux/watchdog.h>
34 #include <linux/delay.h>
35
36 #include <asm/mach-ar71xx/ar71xx.h>
37
38 #define DRV_NAME "ar71xx-wdt"
39 #define DRV_DESC "Atheros AR71xx hardware watchdog driver"
40 #define DRV_VERSION "0.1.0"
41
42 #define WDT_TIMEOUT 15 /* seconds */
43
44 static int nowayout = WATCHDOG_NOWAYOUT;
45
46 #ifdef CONFIG_WATCHDOG_NOWAYOUT
47 module_param(nowayout, int, 0);
48 MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started "
49 "(default=" __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
50 #endif
51
52 static unsigned long wdt_flags;
53
54 #define WDT_FLAGS_BUSY 0
55 #define WDT_FLAGS_EXPECT_CLOSE 1
56
57 static int wdt_timeout = WDT_TIMEOUT;
58 static int boot_status;
59 static int max_timeout;
60 static u32 wdt_clk_freq;
61
62 static inline void ar71xx_wdt_keepalive(void)
63 {
64 ar71xx_reset_wr(AR71XX_RESET_REG_WDOG, wdt_clk_freq * wdt_timeout);
65 }
66
67 static inline void ar71xx_wdt_enable(void)
68 {
69 printk(KERN_DEBUG DRV_NAME ": enabling watchdog timer\n");
70 ar71xx_wdt_keepalive();
71 udelay(2);
72 ar71xx_reset_wr(AR71XX_RESET_REG_WDOG_CTRL, WDOG_CTRL_ACTION_FCR);
73 }
74
75 static inline void ar71xx_wdt_disable(void)
76 {
77 printk(KERN_DEBUG DRV_NAME ": disabling watchdog timer\n");
78 ar71xx_reset_wr(AR71XX_RESET_REG_WDOG_CTRL, WDOG_CTRL_ACTION_NONE);
79 }
80
81 static int ar71xx_wdt_set_timeout(int val)
82 {
83 if (val < 1 || val > max_timeout)
84 return -EINVAL;
85
86 wdt_timeout = val;
87 ar71xx_wdt_keepalive();
88
89 printk(KERN_DEBUG DRV_NAME ": timeout=%d secs\n", wdt_timeout);
90
91 return 0;
92 }
93
94 static int ar71xx_wdt_open(struct inode *inode, struct file *file)
95 {
96 if (test_and_set_bit(WDT_FLAGS_BUSY, &wdt_flags))
97 return -EBUSY;
98
99 clear_bit(WDT_FLAGS_EXPECT_CLOSE, &wdt_flags);
100
101 ar71xx_wdt_enable();
102
103 return nonseekable_open(inode, file);
104 }
105
106 static int ar71xx_wdt_release(struct inode *inode, struct file *file)
107 {
108 if (test_bit(WDT_FLAGS_EXPECT_CLOSE, &wdt_flags)) {
109 ar71xx_wdt_disable();
110 } else {
111 printk(KERN_CRIT DRV_NAME ": device closed unexpectedly, "
112 "watchdog timer will not stop!\n");
113 }
114
115 clear_bit(WDT_FLAGS_BUSY, &wdt_flags);
116 clear_bit(WDT_FLAGS_EXPECT_CLOSE, &wdt_flags);
117
118 return 0;
119 }
120
121 static ssize_t ar71xx_wdt_write(struct file *file, const char *data,
122 size_t len, loff_t *ppos)
123 {
124 if (len) {
125 if (!nowayout) {
126 size_t i;
127
128 clear_bit(WDT_FLAGS_EXPECT_CLOSE, &wdt_flags);
129
130 for (i = 0; i != len; i++) {
131 char c;
132
133 if (get_user(c, data + i))
134 return -EFAULT;
135
136 if (c == 'V')
137 set_bit(WDT_FLAGS_EXPECT_CLOSE,
138 &wdt_flags);
139 }
140 }
141
142 ar71xx_wdt_keepalive();
143 }
144
145 return len;
146 }
147
148 static struct watchdog_info ar71xx_wdt_info = {
149 .options = WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING |
150 WDIOF_MAGICCLOSE | WDIOF_CARDRESET,
151 .firmware_version = 0,
152 .identity = "AR71XX watchdog",
153 };
154
155 static long ar71xx_wdt_ioctl(struct file *file,
156 unsigned int cmd, unsigned long arg)
157 {
158 int t;
159 int ret;
160
161 switch (cmd) {
162 case WDIOC_GETSUPPORT:
163 ret = copy_to_user((struct watchdog_info *)arg,
164 &ar71xx_wdt_info,
165 sizeof(ar71xx_wdt_info)) ? -EFAULT : 0;
166 break;
167
168 case WDIOC_GETSTATUS:
169 ret = put_user(0, (int *)arg) ? -EFAULT : 0;
170 break;
171
172 case WDIOC_GETBOOTSTATUS:
173 ret = put_user(boot_status, (int *)arg) ? -EFAULT : 0;
174 break;
175
176 case WDIOC_KEEPALIVE:
177 ar71xx_wdt_keepalive();
178 ret = 0;
179 break;
180
181 case WDIOC_SETTIMEOUT:
182 ret = get_user(t, (int *)arg) ? -EFAULT : 0;
183 if (ret)
184 break;
185
186 ret = ar71xx_wdt_set_timeout(t);
187 if (ret)
188 break;
189
190 /* fallthrough */
191 case WDIOC_GETTIMEOUT:
192 ret = put_user(wdt_timeout, (int *)arg) ? -EFAULT : 0;
193 break;
194
195 default:
196 ret = -ENOTTY;
197 break;
198 }
199
200 return ret;
201 }
202
203 static const struct file_operations ar71xx_wdt_fops = {
204 .owner = THIS_MODULE,
205 .write = ar71xx_wdt_write,
206 .unlocked_ioctl = ar71xx_wdt_ioctl,
207 .open = ar71xx_wdt_open,
208 .release = ar71xx_wdt_release,
209 };
210
211 static struct miscdevice ar71xx_wdt_miscdev = {
212 .minor = WATCHDOG_MINOR,
213 .name = "watchdog",
214 .fops = &ar71xx_wdt_fops,
215 };
216
217 static int __devinit ar71xx_wdt_probe(struct platform_device *pdev)
218 {
219 int ret;
220
221 switch (ar71xx_soc) {
222 case AR71XX_SOC_AR7130:
223 case AR71XX_SOC_AR7141:
224 case AR71XX_SOC_AR7161:
225 case AR71XX_SOC_AR7240:
226 case AR71XX_SOC_AR7241:
227 case AR71XX_SOC_AR7242:
228 case AR71XX_SOC_AR9130:
229 case AR71XX_SOC_AR9132:
230 wdt_clk_freq = ar71xx_ahb_freq;
231 break;
232
233 case AR71XX_SOC_AR9330:
234 case AR71XX_SOC_AR9331:
235 case AR71XX_SOC_AR9341:
236 case AR71XX_SOC_AR9342:
237 case AR71XX_SOC_AR9344:
238 wdt_clk_freq = ar71xx_ref_freq;
239 break;
240
241 default:
242 BUG();
243 }
244
245 max_timeout = (0xfffffffful / wdt_clk_freq);
246 wdt_timeout = (max_timeout < WDT_TIMEOUT) ? max_timeout : WDT_TIMEOUT;
247
248 if (ar71xx_reset_rr(AR71XX_RESET_REG_WDOG_CTRL) & WDOG_CTRL_LAST_RESET)
249 boot_status = WDIOF_CARDRESET;
250
251 ret = misc_register(&ar71xx_wdt_miscdev);
252 if (ret)
253 goto err_out;
254
255 printk(KERN_INFO DRV_DESC " version " DRV_VERSION "\n");
256
257 printk(KERN_DEBUG DRV_NAME ": timeout=%d secs (max=%d)\n",
258 wdt_timeout, max_timeout);
259
260 return 0;
261
262 err_out:
263 return ret;
264 }
265
266 static int __devexit ar71xx_wdt_remove(struct platform_device *pdev)
267 {
268 misc_deregister(&ar71xx_wdt_miscdev);
269 return 0;
270 }
271
272 static void ar71xx_wdt_shutdown(struct platform_device *pdev)
273 {
274 ar71xx_wdt_disable();
275 }
276
277 static struct platform_driver ar71xx_wdt_driver = {
278 .probe = ar71xx_wdt_probe,
279 .remove = __devexit_p(ar71xx_wdt_remove),
280 .shutdown = ar71xx_wdt_shutdown,
281 .driver = {
282 .name = DRV_NAME,
283 .owner = THIS_MODULE,
284 },
285 };
286
287 static int __init ar71xx_wdt_init(void)
288 {
289 return platform_driver_register(&ar71xx_wdt_driver);
290 }
291 module_init(ar71xx_wdt_init);
292
293 static void __exit ar71xx_wdt_exit(void)
294 {
295 platform_driver_unregister(&ar71xx_wdt_driver);
296 }
297 module_exit(ar71xx_wdt_exit);
298
299 MODULE_DESCRIPTION(DRV_DESC);
300 MODULE_VERSION(DRV_VERSION);
301 MODULE_AUTHOR("Gabor Juhos <juhosg@openwrt.org");
302 MODULE_AUTHOR("Imre Kaloz <kaloz@openwrt.org");
303 MODULE_LICENSE("GPL v2");
304 MODULE_ALIAS("platform:" DRV_NAME);
305 MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR);
This page took 0.0543169999999999 seconds and 3 git commands to generate.