]> git.karo-electronics.de Git - mv-sheeva.git/blob - drivers/char/watchdog/at32ap700x_wdt.c
[WATCHDOG] at32ap700x_wdt.c - Add nowayout + MAGICCLOSE features
[mv-sheeva.git] / drivers / char / watchdog / at32ap700x_wdt.c
1 /*
2  * Watchdog driver for Atmel AT32AP700X devices
3  *
4  * Copyright (C) 2005-2006 Atmel Corporation
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  */
10
11 #include <linux/init.h>
12 #include <linux/kernel.h>
13 #include <linux/module.h>
14 #include <linux/moduleparam.h>
15 #include <linux/miscdevice.h>
16 #include <linux/fs.h>
17 #include <linux/platform_device.h>
18 #include <linux/watchdog.h>
19 #include <linux/uaccess.h>
20 #include <linux/io.h>
21
22 #define TIMEOUT_MIN             1
23 #define TIMEOUT_MAX             2
24 #define TIMEOUT_DEFAULT         TIMEOUT_MAX
25
26 /* module parameters */
27 static int timeout =  TIMEOUT_DEFAULT;
28 module_param(timeout, int, 0);
29 MODULE_PARM_DESC(timeout,
30                 "Timeout value. Limited to be 1 or 2 seconds. (default="
31                 __MODULE_STRING(TIMEOUT_DEFAULT) ")");
32
33 static int nowayout = WATCHDOG_NOWAYOUT;
34 module_param(nowayout, int, 0);
35 MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started (default="
36                 __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
37
38 /* Watchdog registers and write/read macro */
39 #define WDT_CTRL                0x00
40 #define WDT_CTRL_EN                0
41 #define WDT_CTRL_PSEL              8
42 #define WDT_CTRL_KEY              24
43
44 #define WDT_CLR                 0x04
45
46 #define WDT_BIT(name)           (1 << WDT_##name)
47 #define WDT_BF(name, value)     ((value) << WDT_##name)
48
49 #define wdt_readl(dev, reg)                             \
50         __raw_readl((dev)->regs + WDT_##reg)
51 #define wdt_writel(dev, reg, value)                     \
52         __raw_writel((value), (dev)->regs + WDT_##reg)
53
54 struct wdt_at32ap700x {
55         void __iomem            *regs;
56         int                     timeout;
57         int                     users;
58         struct miscdevice       miscdev;
59 };
60
61 static struct wdt_at32ap700x *wdt;
62 static char expect_release;
63
64 /*
65  * Disable the watchdog.
66  */
67 static inline void at32_wdt_stop(void)
68 {
69         unsigned long psel = wdt_readl(wdt, CTRL) & WDT_BF(CTRL_PSEL, 0x0f);
70         wdt_writel(wdt, CTRL, psel | WDT_BF(CTRL_KEY, 0x55));
71         wdt_writel(wdt, CTRL, psel | WDT_BF(CTRL_KEY, 0xaa));
72 }
73
74 /*
75  * Enable and reset the watchdog.
76  */
77 static inline void at32_wdt_start(void)
78 {
79         /* 0xf is 2^16 divider = 2 sec, 0xe is 2^15 divider = 1 sec */
80         unsigned long psel = (wdt->timeout > 1) ? 0xf : 0xe;
81
82         wdt_writel(wdt, CTRL, WDT_BIT(CTRL_EN)
83                         | WDT_BF(CTRL_PSEL, psel)
84                         | WDT_BF(CTRL_KEY, 0x55));
85         wdt_writel(wdt, CTRL, WDT_BIT(CTRL_EN)
86                         | WDT_BF(CTRL_PSEL, psel)
87                         | WDT_BF(CTRL_KEY, 0xaa));
88 }
89
90 /*
91  * Pat the watchdog timer.
92  */
93 static inline void at32_wdt_pat(void)
94 {
95         wdt_writel(wdt, CLR, 0x42);
96 }
97
98 /*
99  * Watchdog device is opened, and watchdog starts running.
100  */
101 static int at32_wdt_open(struct inode *inode, struct file *file)
102 {
103         if (test_and_set_bit(1, &wdt->users))
104                 return -EBUSY;
105
106         at32_wdt_start();
107         return nonseekable_open(inode, file);
108 }
109
110 /*
111  * Close the watchdog device.
112  */
113 static int at32_wdt_close(struct inode *inode, struct file *file)
114 {
115         if (expect_release == 42) {
116                 at32_wdt_stop();
117         } else {
118                 dev_dbg(wdt->miscdev.parent,
119                         "Unexpected close, not stopping watchdog!\n");
120                 at32_wdt_pat();
121         }
122         clear_bit(1, &wdt->users);
123         expect_release = 0;
124         return 0;
125 }
126
127 /*
128  * Change the watchdog time interval.
129  */
130 static int at32_wdt_settimeout(int time)
131 {
132         /*
133          * All counting occurs at 1 / SLOW_CLOCK (32 kHz) and max prescaler is
134          * 2 ^ 16 allowing up to 2 seconds timeout.
135          */
136         if ((time < TIMEOUT_MIN) || (time > TIMEOUT_MAX))
137                 return -EINVAL;
138
139         /*
140          * Set new watchdog time. It will be used when at32_wdt_start() is
141          * called.
142          */
143         wdt->timeout = time;
144         return 0;
145 }
146
147 static struct watchdog_info at32_wdt_info = {
148         .identity       = "at32ap700x watchdog",
149         .options        = WDIOF_SETTIMEOUT |
150                           WDIOF_KEEPALIVEPING |
151                           WDIOF_MAGICCLOSE,
152 };
153
154 /*
155  * Handle commands from user-space.
156  */
157 static int at32_wdt_ioctl(struct inode *inode, struct file *file,
158                 unsigned int cmd, unsigned long arg)
159 {
160         int ret = -ENOTTY;
161         int time;
162         void __user *argp = (void __user *)arg;
163         int __user *p = argp;
164
165         switch (cmd) {
166         case WDIOC_KEEPALIVE:
167                 at32_wdt_pat();
168                 ret = 0;
169                 break;
170         case WDIOC_GETSUPPORT:
171                 ret = copy_to_user(argp, &at32_wdt_info,
172                                 sizeof(at32_wdt_info)) ? -EFAULT : 0;
173                 break;
174         case WDIOC_SETTIMEOUT:
175                 ret = get_user(time, p);
176                 if (ret)
177                         break;
178                 ret = at32_wdt_settimeout(time);
179                 if (ret)
180                         break;
181                 /* Enable new time value */
182                 at32_wdt_start();
183                 /* fall through */
184         case WDIOC_GETTIMEOUT:
185                 ret = put_user(wdt->timeout, p);
186                 break;
187         case WDIOC_GETSTATUS: /* fall through */
188         case WDIOC_GETBOOTSTATUS:
189                 ret = put_user(0, p);
190                 break;
191         case WDIOC_SETOPTIONS:
192                 ret = get_user(time, p);
193                 if (ret)
194                         break;
195                 if (time & WDIOS_DISABLECARD)
196                         at32_wdt_stop();
197                 if (time & WDIOS_ENABLECARD)
198                         at32_wdt_start();
199                 ret = 0;
200                 break;
201         }
202
203         return ret;
204 }
205
206 static ssize_t at32_wdt_write(struct file *file, const char __user *data,
207                                 size_t len, loff_t *ppos)
208 {
209         /* See if we got the magic character 'V' and reload the timer */
210         if (len) {
211                 if (!nowayout) {
212                         size_t i;
213
214                         /*
215                          * note: just in case someone wrote the magic
216                          * character five months ago...
217                          */
218                         expect_release = 0;
219
220                         /*
221                          * scan to see whether or not we got the magic
222                          * character
223                          */
224                         for (i = 0; i != len; i++) {
225                                 char c;
226                                 if (get_user(c, data+i))
227                                         return -EFAULT;
228                                 if (c == 'V')
229                                         expect_release = 42;
230                         }
231                 }
232                 /* someone wrote to us, we should pat the watchdog */
233                 at32_wdt_pat();
234         }
235         return len;
236 }
237
238 static const struct file_operations at32_wdt_fops = {
239         .owner          = THIS_MODULE,
240         .llseek         = no_llseek,
241         .ioctl          = at32_wdt_ioctl,
242         .open           = at32_wdt_open,
243         .release        = at32_wdt_close,
244         .write          = at32_wdt_write,
245 };
246
247 static int __init at32_wdt_probe(struct platform_device *pdev)
248 {
249         struct resource *regs;
250         int ret;
251
252         if (wdt) {
253                 dev_dbg(&pdev->dev, "only 1 wdt instance supported.\n");
254                 return -EBUSY;
255         }
256
257         regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
258         if (!regs) {
259                 dev_dbg(&pdev->dev, "missing mmio resource\n");
260                 return -ENXIO;
261         }
262
263         wdt = kzalloc(sizeof(struct wdt_at32ap700x), GFP_KERNEL);
264         if (!wdt) {
265                 dev_dbg(&pdev->dev, "no memory for wdt structure\n");
266                 return -ENOMEM;
267         }
268
269         wdt->regs = ioremap(regs->start, regs->end - regs->start + 1);
270         if (!wdt->regs) {
271                 ret = -ENOMEM;
272                 dev_dbg(&pdev->dev, "could not map I/O memory\n");
273                 goto err_free;
274         }
275         wdt->users = 0;
276         wdt->miscdev.minor = WATCHDOG_MINOR;
277         wdt->miscdev.name = "watchdog";
278         wdt->miscdev.fops = &at32_wdt_fops;
279
280         if (at32_wdt_settimeout(timeout)) {
281                 at32_wdt_settimeout(TIMEOUT_DEFAULT);
282                 dev_dbg(&pdev->dev,
283                         "default timeout invalid, set to %d sec.\n",
284                         TIMEOUT_DEFAULT);
285         }
286
287         ret = misc_register(&wdt->miscdev);
288         if (ret) {
289                 dev_dbg(&pdev->dev, "failed to register wdt miscdev\n");
290                 goto err_iounmap;
291         }
292
293         platform_set_drvdata(pdev, wdt);
294         wdt->miscdev.parent = &pdev->dev;
295         dev_info(&pdev->dev,
296                 "AT32AP700X WDT at 0x%p, timeout %d sec (nowayout=%d)\n",
297                 wdt->regs, wdt->timeout, nowayout);
298
299         return 0;
300
301 err_iounmap:
302         iounmap(wdt->regs);
303 err_free:
304         kfree(wdt);
305         wdt = NULL;
306         return ret;
307 }
308
309 static int __exit at32_wdt_remove(struct platform_device *pdev)
310 {
311         if (wdt && platform_get_drvdata(pdev) == wdt) {
312                 /* Stop the timer before we leave */
313                 if (!nowayout)
314                         at32_wdt_stop();
315
316                 misc_deregister(&wdt->miscdev);
317                 iounmap(wdt->regs);
318                 kfree(wdt);
319                 wdt = NULL;
320                 platform_set_drvdata(pdev, NULL);
321         }
322
323         return 0;
324 }
325
326 static void at32_wdt_shutdown(struct platform_device *pdev)
327 {
328         at32_wdt_stop();
329 }
330
331 #ifdef CONFIG_PM
332 static int at32_wdt_suspend(struct platform_device *pdev, pm_message_t message)
333 {
334         at32_wdt_stop();
335         return 0;
336 }
337
338 static int at32_wdt_resume(struct platform_device *pdev)
339 {
340         if (wdt->users)
341                 at32_wdt_start();
342         return 0;
343 }
344 #else
345 #define at32_wdt_suspend NULL
346 #define at32_wdt_resume NULL
347 #endif
348
349 static struct platform_driver at32_wdt_driver = {
350         .remove         = __exit_p(at32_wdt_remove),
351         .suspend        = at32_wdt_suspend,
352         .resume         = at32_wdt_resume,
353         .driver         = {
354                 .name   = "at32_wdt",
355                 .owner  = THIS_MODULE,
356         },
357         .shutdown       = at32_wdt_shutdown,
358 };
359
360 static int __init at32_wdt_init(void)
361 {
362         return platform_driver_probe(&at32_wdt_driver, at32_wdt_probe);
363 }
364 module_init(at32_wdt_init);
365
366 static void __exit at32_wdt_exit(void)
367 {
368         platform_driver_unregister(&at32_wdt_driver);
369 }
370 module_exit(at32_wdt_exit);
371
372 MODULE_AUTHOR("Hans-Christian Egtvedt <hcegtvedt@atmel.com>");
373 MODULE_DESCRIPTION("Watchdog driver for Atmel AT32AP700X");
374 MODULE_LICENSE("GPL");
375 MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR);