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