PM / sleep: Asynchronous threads for suspend_noirq
[linux/fpc-iii.git] / drivers / watchdog / at32ap700x_wdt.c
blobafe7d17e677634f1fd447a147deeb133ac366f36
1 /*
2 * Watchdog driver for Atmel AT32AP700X devices
4 * Copyright (C) 2005-2006 Atmel Corporation
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.
11 * Errata: WDT Clear is blocked after WDT Reset
13 * A watchdog timer event will, after reset, block writes to the WDT_CLEAR
14 * register, preventing the program to clear the next Watchdog Timer Reset.
16 * If you still want to use the WDT after a WDT reset a small code can be
17 * insterted at the startup checking the AVR32_PM.rcause register for WDT reset
18 * and use a GPIO pin to reset the system. This method requires that one of the
19 * GPIO pins are available and connected externally to the RESET_N pin. After
20 * the GPIO pin has pulled down the reset line the GPIO will be reset and leave
21 * the pin tristated with pullup.
24 #include <linux/init.h>
25 #include <linux/kernel.h>
26 #include <linux/module.h>
27 #include <linux/moduleparam.h>
28 #include <linux/miscdevice.h>
29 #include <linux/fs.h>
30 #include <linux/platform_device.h>
31 #include <linux/watchdog.h>
32 #include <linux/uaccess.h>
33 #include <linux/io.h>
34 #include <linux/spinlock.h>
35 #include <linux/slab.h>
37 #define TIMEOUT_MIN 1
38 #define TIMEOUT_MAX 2
39 #define TIMEOUT_DEFAULT TIMEOUT_MAX
41 /* module parameters */
42 static int timeout = TIMEOUT_DEFAULT;
43 module_param(timeout, int, 0);
44 MODULE_PARM_DESC(timeout,
45 "Timeout value. Limited to be 1 or 2 seconds. (default="
46 __MODULE_STRING(TIMEOUT_DEFAULT) ")");
48 static bool nowayout = WATCHDOG_NOWAYOUT;
49 module_param(nowayout, bool, 0);
50 MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started (default="
51 __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
53 /* Watchdog registers and write/read macro */
54 #define WDT_CTRL 0x00
55 #define WDT_CTRL_EN 0
56 #define WDT_CTRL_PSEL 8
57 #define WDT_CTRL_KEY 24
59 #define WDT_CLR 0x04
61 #define WDT_RCAUSE 0x10
62 #define WDT_RCAUSE_POR 0
63 #define WDT_RCAUSE_EXT 2
64 #define WDT_RCAUSE_WDT 3
65 #define WDT_RCAUSE_JTAG 4
66 #define WDT_RCAUSE_SERP 5
68 #define WDT_BIT(name) (1 << WDT_##name)
69 #define WDT_BF(name, value) ((value) << WDT_##name)
71 #define wdt_readl(dev, reg) \
72 __raw_readl((dev)->regs + WDT_##reg)
73 #define wdt_writel(dev, reg, value) \
74 __raw_writel((value), (dev)->regs + WDT_##reg)
76 struct wdt_at32ap700x {
77 void __iomem *regs;
78 spinlock_t io_lock;
79 int timeout;
80 int boot_status;
81 unsigned long users;
82 struct miscdevice miscdev;
85 static struct wdt_at32ap700x *wdt;
86 static char expect_release;
89 * Disable the watchdog.
91 static inline void at32_wdt_stop(void)
93 unsigned long psel;
95 spin_lock(&wdt->io_lock);
96 psel = wdt_readl(wdt, CTRL) & WDT_BF(CTRL_PSEL, 0x0f);
97 wdt_writel(wdt, CTRL, psel | WDT_BF(CTRL_KEY, 0x55));
98 wdt_writel(wdt, CTRL, psel | WDT_BF(CTRL_KEY, 0xaa));
99 spin_unlock(&wdt->io_lock);
103 * Enable and reset the watchdog.
105 static inline void at32_wdt_start(void)
107 /* 0xf is 2^16 divider = 2 sec, 0xe is 2^15 divider = 1 sec */
108 unsigned long psel = (wdt->timeout > 1) ? 0xf : 0xe;
110 spin_lock(&wdt->io_lock);
111 wdt_writel(wdt, CTRL, WDT_BIT(CTRL_EN)
112 | WDT_BF(CTRL_PSEL, psel)
113 | WDT_BF(CTRL_KEY, 0x55));
114 wdt_writel(wdt, CTRL, WDT_BIT(CTRL_EN)
115 | WDT_BF(CTRL_PSEL, psel)
116 | WDT_BF(CTRL_KEY, 0xaa));
117 spin_unlock(&wdt->io_lock);
121 * Pat the watchdog timer.
123 static inline void at32_wdt_pat(void)
125 spin_lock(&wdt->io_lock);
126 wdt_writel(wdt, CLR, 0x42);
127 spin_unlock(&wdt->io_lock);
131 * Watchdog device is opened, and watchdog starts running.
133 static int at32_wdt_open(struct inode *inode, struct file *file)
135 if (test_and_set_bit(1, &wdt->users))
136 return -EBUSY;
138 at32_wdt_start();
139 return nonseekable_open(inode, file);
143 * Close the watchdog device.
145 static int at32_wdt_close(struct inode *inode, struct file *file)
147 if (expect_release == 42) {
148 at32_wdt_stop();
149 } else {
150 dev_dbg(wdt->miscdev.parent,
151 "unexpected close, not stopping watchdog!\n");
152 at32_wdt_pat();
154 clear_bit(1, &wdt->users);
155 expect_release = 0;
156 return 0;
160 * Change the watchdog time interval.
162 static int at32_wdt_settimeout(int time)
165 * All counting occurs at 1 / SLOW_CLOCK (32 kHz) and max prescaler is
166 * 2 ^ 16 allowing up to 2 seconds timeout.
168 if ((time < TIMEOUT_MIN) || (time > TIMEOUT_MAX))
169 return -EINVAL;
172 * Set new watchdog time. It will be used when at32_wdt_start() is
173 * called.
175 wdt->timeout = time;
176 return 0;
180 * Get the watchdog status.
182 static int at32_wdt_get_status(void)
184 int rcause;
185 int status = 0;
187 rcause = wdt_readl(wdt, RCAUSE);
189 switch (rcause) {
190 case WDT_BIT(RCAUSE_EXT):
191 status = WDIOF_EXTERN1;
192 break;
193 case WDT_BIT(RCAUSE_WDT):
194 status = WDIOF_CARDRESET;
195 break;
196 case WDT_BIT(RCAUSE_POR): /* fall through */
197 case WDT_BIT(RCAUSE_JTAG): /* fall through */
198 case WDT_BIT(RCAUSE_SERP): /* fall through */
199 default:
200 break;
203 return status;
206 static const struct watchdog_info at32_wdt_info = {
207 .identity = "at32ap700x watchdog",
208 .options = WDIOF_SETTIMEOUT |
209 WDIOF_KEEPALIVEPING |
210 WDIOF_MAGICCLOSE,
214 * Handle commands from user-space.
216 static long at32_wdt_ioctl(struct file *file,
217 unsigned int cmd, unsigned long arg)
219 int ret = -ENOTTY;
220 int time;
221 void __user *argp = (void __user *)arg;
222 int __user *p = argp;
224 switch (cmd) {
225 case WDIOC_GETSUPPORT:
226 ret = copy_to_user(argp, &at32_wdt_info,
227 sizeof(at32_wdt_info)) ? -EFAULT : 0;
228 break;
229 case WDIOC_GETSTATUS:
230 ret = put_user(0, p);
231 break;
232 case WDIOC_GETBOOTSTATUS:
233 ret = put_user(wdt->boot_status, p);
234 break;
235 case WDIOC_SETOPTIONS:
236 ret = get_user(time, p);
237 if (ret)
238 break;
239 if (time & WDIOS_DISABLECARD)
240 at32_wdt_stop();
241 if (time & WDIOS_ENABLECARD)
242 at32_wdt_start();
243 ret = 0;
244 break;
245 case WDIOC_KEEPALIVE:
246 at32_wdt_pat();
247 ret = 0;
248 break;
249 case WDIOC_SETTIMEOUT:
250 ret = get_user(time, p);
251 if (ret)
252 break;
253 ret = at32_wdt_settimeout(time);
254 if (ret)
255 break;
256 /* Enable new time value */
257 at32_wdt_start();
258 /* fall through */
259 case WDIOC_GETTIMEOUT:
260 ret = put_user(wdt->timeout, p);
261 break;
264 return ret;
267 static ssize_t at32_wdt_write(struct file *file, const char __user *data,
268 size_t len, loff_t *ppos)
270 /* See if we got the magic character 'V' and reload the timer */
271 if (len) {
272 if (!nowayout) {
273 size_t i;
276 * note: just in case someone wrote the magic
277 * character five months ago...
279 expect_release = 0;
282 * scan to see whether or not we got the magic
283 * character
285 for (i = 0; i != len; i++) {
286 char c;
287 if (get_user(c, data + i))
288 return -EFAULT;
289 if (c == 'V')
290 expect_release = 42;
293 /* someone wrote to us, we should pat the watchdog */
294 at32_wdt_pat();
296 return len;
299 static const struct file_operations at32_wdt_fops = {
300 .owner = THIS_MODULE,
301 .llseek = no_llseek,
302 .unlocked_ioctl = at32_wdt_ioctl,
303 .open = at32_wdt_open,
304 .release = at32_wdt_close,
305 .write = at32_wdt_write,
308 static int __init at32_wdt_probe(struct platform_device *pdev)
310 struct resource *regs;
311 int ret;
313 if (wdt) {
314 dev_dbg(&pdev->dev, "only 1 wdt instance supported.\n");
315 return -EBUSY;
318 regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
319 if (!regs) {
320 dev_dbg(&pdev->dev, "missing mmio resource\n");
321 return -ENXIO;
324 wdt = devm_kzalloc(&pdev->dev, sizeof(struct wdt_at32ap700x),
325 GFP_KERNEL);
326 if (!wdt) {
327 dev_dbg(&pdev->dev, "no memory for wdt structure\n");
328 return -ENOMEM;
331 wdt->regs = devm_ioremap(&pdev->dev, regs->start, resource_size(regs));
332 if (!wdt->regs) {
333 ret = -ENOMEM;
334 dev_dbg(&pdev->dev, "could not map I/O memory\n");
335 goto err_free;
338 spin_lock_init(&wdt->io_lock);
339 wdt->boot_status = at32_wdt_get_status();
341 /* Work-around for watchdog silicon errata. */
342 if (wdt->boot_status & WDIOF_CARDRESET) {
343 dev_info(&pdev->dev, "CPU must be reset with external "
344 "reset or POR due to silicon errata.\n");
345 ret = -EIO;
346 goto err_free;
347 } else {
348 wdt->users = 0;
351 wdt->miscdev.minor = WATCHDOG_MINOR;
352 wdt->miscdev.name = "watchdog";
353 wdt->miscdev.fops = &at32_wdt_fops;
354 wdt->miscdev.parent = &pdev->dev;
356 platform_set_drvdata(pdev, wdt);
358 if (at32_wdt_settimeout(timeout)) {
359 at32_wdt_settimeout(TIMEOUT_DEFAULT);
360 dev_dbg(&pdev->dev,
361 "default timeout invalid, set to %d sec.\n",
362 TIMEOUT_DEFAULT);
365 ret = misc_register(&wdt->miscdev);
366 if (ret) {
367 dev_dbg(&pdev->dev, "failed to register wdt miscdev\n");
368 goto err_free;
371 dev_info(&pdev->dev,
372 "AT32AP700X WDT at 0x%p, timeout %d sec (nowayout=%d)\n",
373 wdt->regs, wdt->timeout, nowayout);
375 return 0;
377 err_free:
378 wdt = NULL;
379 return ret;
382 static int __exit at32_wdt_remove(struct platform_device *pdev)
384 if (wdt && platform_get_drvdata(pdev) == wdt) {
385 /* Stop the timer before we leave */
386 if (!nowayout)
387 at32_wdt_stop();
389 misc_deregister(&wdt->miscdev);
390 wdt = NULL;
392 return 0;
395 static void at32_wdt_shutdown(struct platform_device *pdev)
397 at32_wdt_stop();
400 #ifdef CONFIG_PM
401 static int at32_wdt_suspend(struct platform_device *pdev, pm_message_t message)
403 at32_wdt_stop();
404 return 0;
407 static int at32_wdt_resume(struct platform_device *pdev)
409 if (wdt->users)
410 at32_wdt_start();
411 return 0;
413 #else
414 #define at32_wdt_suspend NULL
415 #define at32_wdt_resume NULL
416 #endif
418 /* work with hotplug and coldplug */
419 MODULE_ALIAS("platform:at32_wdt");
421 static struct platform_driver at32_wdt_driver = {
422 .remove = __exit_p(at32_wdt_remove),
423 .suspend = at32_wdt_suspend,
424 .resume = at32_wdt_resume,
425 .driver = {
426 .name = "at32_wdt",
427 .owner = THIS_MODULE,
429 .shutdown = at32_wdt_shutdown,
432 module_platform_driver_probe(at32_wdt_driver, at32_wdt_probe);
434 MODULE_AUTHOR("Hans-Christian Egtvedt <egtvedt@samfundet.no>");
435 MODULE_DESCRIPTION("Watchdog driver for Atmel AT32AP700X");
436 MODULE_LICENSE("GPL");