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 #include <linux/init.h>
12 #include <linux/kernel.h>
13 #include <linux/module.h>
14 #include <linux/moduleparam.h>
15 #include <linux/miscdevice.h>
17 #include <linux/platform_device.h>
18 #include <linux/watchdog.h>
19 #include <linux/uaccess.h>
21 #include <linux/spinlock.h>
25 #define TIMEOUT_DEFAULT TIMEOUT_MAX
27 /* module parameters */
28 static int timeout = TIMEOUT_DEFAULT;
29 module_param(timeout, int, 0);
30 MODULE_PARM_DESC(timeout,
31 "Timeout value. Limited to be 1 or 2 seconds. (default="
32 __MODULE_STRING(TIMEOUT_DEFAULT) ")");
34 static int nowayout = WATCHDOG_NOWAYOUT;
35 module_param(nowayout, int, 0);
36 MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started (default="
37 __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
39 /* Watchdog registers and write/read macro */
42 #define WDT_CTRL_PSEL 8
43 #define WDT_CTRL_KEY 24
47 #define WDT_BIT(name) (1 << WDT_##name)
48 #define WDT_BF(name, value) ((value) << WDT_##name)
50 #define wdt_readl(dev, reg) \
51 __raw_readl((dev)->regs + WDT_##reg)
52 #define wdt_writel(dev, reg, value) \
53 __raw_writel((value), (dev)->regs + WDT_##reg)
55 struct wdt_at32ap700x {
60 struct miscdevice miscdev;
63 static struct wdt_at32ap700x *wdt;
64 static char expect_release;
67 * Disable the watchdog.
69 static inline void at32_wdt_stop(void)
71 spin_lock(&wdt->io_lock);
72 unsigned long psel = wdt_readl(wdt, CTRL) & WDT_BF(CTRL_PSEL, 0x0f);
73 wdt_writel(wdt, CTRL, psel | WDT_BF(CTRL_KEY, 0x55));
74 wdt_writel(wdt, CTRL, psel | WDT_BF(CTRL_KEY, 0xaa));
75 spin_unlock(&wdt->io_lock);
79 * Enable and reset the watchdog.
81 static inline void at32_wdt_start(void)
83 /* 0xf is 2^16 divider = 2 sec, 0xe is 2^15 divider = 1 sec */
84 unsigned long psel = (wdt->timeout > 1) ? 0xf : 0xe;
86 spin_lock(&wdt->io_lock);
87 wdt_writel(wdt, CTRL, WDT_BIT(CTRL_EN)
88 | WDT_BF(CTRL_PSEL, psel)
89 | WDT_BF(CTRL_KEY, 0x55));
90 wdt_writel(wdt, CTRL, WDT_BIT(CTRL_EN)
91 | WDT_BF(CTRL_PSEL, psel)
92 | WDT_BF(CTRL_KEY, 0xaa));
93 spin_unlock(&wdt->io_lock);
97 * Pat the watchdog timer.
99 static inline void at32_wdt_pat(void)
101 spin_lock(&wdt->io_lock);
102 wdt_writel(wdt, CLR, 0x42);
103 spin_unlock(&wdt->io_lock);
107 * Watchdog device is opened, and watchdog starts running.
109 static int at32_wdt_open(struct inode *inode, struct file *file)
111 if (test_and_set_bit(1, &wdt->users))
115 return nonseekable_open(inode, file);
119 * Close the watchdog device.
121 static int at32_wdt_close(struct inode *inode, struct file *file)
123 if (expect_release == 42) {
126 dev_dbg(wdt->miscdev.parent,
127 "Unexpected close, not stopping watchdog!\n");
130 clear_bit(1, &wdt->users);
136 * Change the watchdog time interval.
138 static int at32_wdt_settimeout(int time)
141 * All counting occurs at 1 / SLOW_CLOCK (32 kHz) and max prescaler is
142 * 2 ^ 16 allowing up to 2 seconds timeout.
144 if ((time < TIMEOUT_MIN) || (time > TIMEOUT_MAX))
148 * Set new watchdog time. It will be used when at32_wdt_start() is
155 static struct watchdog_info at32_wdt_info = {
156 .identity = "at32ap700x watchdog",
157 .options = WDIOF_SETTIMEOUT |
158 WDIOF_KEEPALIVEPING |
163 * Handle commands from user-space.
165 static int at32_wdt_ioctl(struct inode *inode, struct file *file,
166 unsigned int cmd, unsigned long arg)
170 void __user *argp = (void __user *)arg;
171 int __user *p = argp;
174 case WDIOC_KEEPALIVE:
178 case WDIOC_GETSUPPORT:
179 ret = copy_to_user(argp, &at32_wdt_info,
180 sizeof(at32_wdt_info)) ? -EFAULT : 0;
182 case WDIOC_SETTIMEOUT:
183 ret = get_user(time, p);
186 ret = at32_wdt_settimeout(time);
189 /* Enable new time value */
192 case WDIOC_GETTIMEOUT:
193 ret = put_user(wdt->timeout, p);
195 case WDIOC_GETSTATUS: /* fall through */
196 case WDIOC_GETBOOTSTATUS:
197 ret = put_user(0, p);
199 case WDIOC_SETOPTIONS:
200 ret = get_user(time, p);
203 if (time & WDIOS_DISABLECARD)
205 if (time & WDIOS_ENABLECARD)
214 static ssize_t at32_wdt_write(struct file *file, const char __user *data,
215 size_t len, loff_t *ppos)
217 /* See if we got the magic character 'V' and reload the timer */
223 * note: just in case someone wrote the magic
224 * character five months ago...
229 * scan to see whether or not we got the magic
232 for (i = 0; i != len; i++) {
234 if (get_user(c, data+i))
240 /* someone wrote to us, we should pat the watchdog */
246 static const struct file_operations at32_wdt_fops = {
247 .owner = THIS_MODULE,
249 .ioctl = at32_wdt_ioctl,
250 .open = at32_wdt_open,
251 .release = at32_wdt_close,
252 .write = at32_wdt_write,
255 static int __init at32_wdt_probe(struct platform_device *pdev)
257 struct resource *regs;
261 dev_dbg(&pdev->dev, "only 1 wdt instance supported.\n");
265 regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
267 dev_dbg(&pdev->dev, "missing mmio resource\n");
271 wdt = kzalloc(sizeof(struct wdt_at32ap700x), GFP_KERNEL);
273 dev_dbg(&pdev->dev, "no memory for wdt structure\n");
277 wdt->regs = ioremap(regs->start, regs->end - regs->start + 1);
280 dev_dbg(&pdev->dev, "could not map I/O memory\n");
283 spin_lock_init(&wdt->io_lock);
285 wdt->miscdev.minor = WATCHDOG_MINOR;
286 wdt->miscdev.name = "watchdog";
287 wdt->miscdev.fops = &at32_wdt_fops;
289 if (at32_wdt_settimeout(timeout)) {
290 at32_wdt_settimeout(TIMEOUT_DEFAULT);
292 "default timeout invalid, set to %d sec.\n",
296 ret = misc_register(&wdt->miscdev);
298 dev_dbg(&pdev->dev, "failed to register wdt miscdev\n");
302 platform_set_drvdata(pdev, wdt);
303 wdt->miscdev.parent = &pdev->dev;
305 "AT32AP700X WDT at 0x%p, timeout %d sec (nowayout=%d)\n",
306 wdt->regs, wdt->timeout, nowayout);
318 static int __exit at32_wdt_remove(struct platform_device *pdev)
320 if (wdt && platform_get_drvdata(pdev) == wdt) {
321 /* Stop the timer before we leave */
325 misc_deregister(&wdt->miscdev);
329 platform_set_drvdata(pdev, NULL);
335 static void at32_wdt_shutdown(struct platform_device *pdev)
341 static int at32_wdt_suspend(struct platform_device *pdev, pm_message_t message)
347 static int at32_wdt_resume(struct platform_device *pdev)
354 #define at32_wdt_suspend NULL
355 #define at32_wdt_resume NULL
358 static struct platform_driver at32_wdt_driver = {
359 .remove = __exit_p(at32_wdt_remove),
360 .suspend = at32_wdt_suspend,
361 .resume = at32_wdt_resume,
364 .owner = THIS_MODULE,
366 .shutdown = at32_wdt_shutdown,
369 static int __init at32_wdt_init(void)
371 return platform_driver_probe(&at32_wdt_driver, at32_wdt_probe);
373 module_init(at32_wdt_init);
375 static void __exit at32_wdt_exit(void)
377 platform_driver_unregister(&at32_wdt_driver);
379 module_exit(at32_wdt_exit);
381 MODULE_AUTHOR("Hans-Christian Egtvedt <hcegtvedt@atmel.com>");
382 MODULE_DESCRIPTION("Watchdog driver for Atmel AT32AP700X");
383 MODULE_LICENSE("GPL");
384 MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR);