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>
24 #define TIMEOUT_DEFAULT TIMEOUT_MAX
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) ")");
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) ")");
38 /* Watchdog registers and write/read macro */
41 #define WDT_CTRL_PSEL 8
42 #define WDT_CTRL_KEY 24
46 #define WDT_BIT(name) (1 << WDT_##name)
47 #define WDT_BF(name, value) ((value) << WDT_##name)
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)
54 struct wdt_at32ap700x {
58 struct miscdevice miscdev;
61 static struct wdt_at32ap700x *wdt;
62 static char expect_release;
65 * Disable the watchdog.
67 static inline void at32_wdt_stop(void)
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));
75 * Enable and reset the watchdog.
77 static inline void at32_wdt_start(void)
79 /* 0xf is 2^16 divider = 2 sec, 0xe is 2^15 divider = 1 sec */
80 unsigned long psel = (wdt->timeout > 1) ? 0xf : 0xe;
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));
91 * Pat the watchdog timer.
93 static inline void at32_wdt_pat(void)
95 wdt_writel(wdt, CLR, 0x42);
99 * Watchdog device is opened, and watchdog starts running.
101 static int at32_wdt_open(struct inode *inode, struct file *file)
103 if (test_and_set_bit(1, &wdt->users))
107 return nonseekable_open(inode, file);
111 * Close the watchdog device.
113 static int at32_wdt_close(struct inode *inode, struct file *file)
115 if (expect_release == 42) {
118 dev_dbg(wdt->miscdev.parent,
119 "Unexpected close, not stopping watchdog!\n");
122 clear_bit(1, &wdt->users);
128 * Change the watchdog time interval.
130 static int at32_wdt_settimeout(int time)
133 * All counting occurs at 1 / SLOW_CLOCK (32 kHz) and max prescaler is
134 * 2 ^ 16 allowing up to 2 seconds timeout.
136 if ((time < TIMEOUT_MIN) || (time > TIMEOUT_MAX))
140 * Set new watchdog time. It will be used when at32_wdt_start() is
147 static struct watchdog_info at32_wdt_info = {
148 .identity = "at32ap700x watchdog",
149 .options = WDIOF_SETTIMEOUT |
150 WDIOF_KEEPALIVEPING |
155 * Handle commands from user-space.
157 static int at32_wdt_ioctl(struct inode *inode, struct file *file,
158 unsigned int cmd, unsigned long arg)
162 void __user *argp = (void __user *)arg;
163 int __user *p = argp;
166 case WDIOC_KEEPALIVE:
170 case WDIOC_GETSUPPORT:
171 ret = copy_to_user(argp, &at32_wdt_info,
172 sizeof(at32_wdt_info)) ? -EFAULT : 0;
174 case WDIOC_SETTIMEOUT:
175 ret = get_user(time, p);
178 ret = at32_wdt_settimeout(time);
181 /* Enable new time value */
184 case WDIOC_GETTIMEOUT:
185 ret = put_user(wdt->timeout, p);
187 case WDIOC_GETSTATUS: /* fall through */
188 case WDIOC_GETBOOTSTATUS:
189 ret = put_user(0, p);
191 case WDIOC_SETOPTIONS:
192 ret = get_user(time, p);
195 if (time & WDIOS_DISABLECARD)
197 if (time & WDIOS_ENABLECARD)
206 static ssize_t at32_wdt_write(struct file *file, const char __user *data,
207 size_t len, loff_t *ppos)
209 /* See if we got the magic character 'V' and reload the timer */
215 * note: just in case someone wrote the magic
216 * character five months ago...
221 * scan to see whether or not we got the magic
224 for (i = 0; i != len; i++) {
226 if (get_user(c, data+i))
232 /* someone wrote to us, we should pat the watchdog */
238 static const struct file_operations at32_wdt_fops = {
239 .owner = THIS_MODULE,
241 .ioctl = at32_wdt_ioctl,
242 .open = at32_wdt_open,
243 .release = at32_wdt_close,
244 .write = at32_wdt_write,
247 static int __init at32_wdt_probe(struct platform_device *pdev)
249 struct resource *regs;
253 dev_dbg(&pdev->dev, "only 1 wdt instance supported.\n");
257 regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
259 dev_dbg(&pdev->dev, "missing mmio resource\n");
263 wdt = kzalloc(sizeof(struct wdt_at32ap700x), GFP_KERNEL);
265 dev_dbg(&pdev->dev, "no memory for wdt structure\n");
269 wdt->regs = ioremap(regs->start, regs->end - regs->start + 1);
272 dev_dbg(&pdev->dev, "could not map I/O memory\n");
276 wdt->miscdev.minor = WATCHDOG_MINOR;
277 wdt->miscdev.name = "watchdog";
278 wdt->miscdev.fops = &at32_wdt_fops;
280 if (at32_wdt_settimeout(timeout)) {
281 at32_wdt_settimeout(TIMEOUT_DEFAULT);
283 "default timeout invalid, set to %d sec.\n",
287 ret = misc_register(&wdt->miscdev);
289 dev_dbg(&pdev->dev, "failed to register wdt miscdev\n");
293 platform_set_drvdata(pdev, wdt);
294 wdt->miscdev.parent = &pdev->dev;
296 "AT32AP700X WDT at 0x%p, timeout %d sec (nowayout=%d)\n",
297 wdt->regs, wdt->timeout, nowayout);
309 static int __exit at32_wdt_remove(struct platform_device *pdev)
311 if (wdt && platform_get_drvdata(pdev) == wdt) {
312 /* Stop the timer before we leave */
316 misc_deregister(&wdt->miscdev);
320 platform_set_drvdata(pdev, NULL);
326 static void at32_wdt_shutdown(struct platform_device *pdev)
332 static int at32_wdt_suspend(struct platform_device *pdev, pm_message_t message)
338 static int at32_wdt_resume(struct platform_device *pdev)
345 #define at32_wdt_suspend NULL
346 #define at32_wdt_resume NULL
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,
355 .owner = THIS_MODULE,
357 .shutdown = at32_wdt_shutdown,
360 static int __init at32_wdt_init(void)
362 return platform_driver_probe(&at32_wdt_driver, at32_wdt_probe);
364 module_init(at32_wdt_init);
366 static void __exit at32_wdt_exit(void)
368 platform_driver_unregister(&at32_wdt_driver);
370 module_exit(at32_wdt_exit);
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);