[WATCHDOG] watchdog-driver-for-at32ap700x-devices-fix-2
[linux-2.6/libata-dev.git] / drivers / char / watchdog / at32ap700x_wdt.c
blobf08ea9493d2fbf38597c3a010700a320ce5a79b4
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.
9 */
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>
22 #define TIMEOUT_MIN 1
23 #define TIMEOUT_DEFAULT CONFIG_AT32AP700X_WDT_TIMEOUT
24 #define TIMEOUT_MAX 2
26 /* Watchdog registers and write/read macro */
27 #define WDT_CTRL 0x00
28 #define WDT_CTRL_EN 0
29 #define WDT_CTRL_PSEL 8
30 #define WDT_CTRL_KEY 24
32 #define WDT_CLR 0x04
34 #define WDT_BIT(name) (1 << WDT_##name)
35 #define WDT_BF(name,value) ((value) << WDT_##name)
37 #define wdt_readl(dev,reg) \
38 __raw_readl((dev)->regs + WDT_##reg)
39 #define wdt_writel(dev,reg,value) \
40 __raw_writel((value), (dev)->regs + WDT_##reg)
42 struct wdt_at32ap700x {
43 void __iomem *regs;
44 int timeout;
45 int users;
46 struct miscdevice miscdev;
49 static struct wdt_at32ap700x *wdt;
52 * Disable the watchdog.
54 static void inline at32_wdt_stop(void)
56 unsigned long psel = wdt_readl(wdt, CTRL) & WDT_BF(CTRL_PSEL, 0x0f);
57 wdt_writel(wdt, CTRL, psel | WDT_BF(CTRL_KEY, 0x55));
58 wdt_writel(wdt, CTRL, psel | WDT_BF(CTRL_KEY, 0xaa));
62 * Enable and reset the watchdog.
64 static void inline at32_wdt_start(void)
66 /* 0xf is 2^16 divider = 2 sec, 0xe is 2^15 divider = 1 sec */
67 unsigned long psel = (wdt->timeout > 1) ? 0xf : 0xe;
69 wdt_writel(wdt, CTRL, WDT_BIT(CTRL_EN)
70 | WDT_BF(CTRL_PSEL, psel)
71 | WDT_BF(CTRL_KEY, 0x55));
72 wdt_writel(wdt, CTRL, WDT_BIT(CTRL_EN)
73 | WDT_BF(CTRL_PSEL, psel)
74 | WDT_BF(CTRL_KEY, 0xaa));
78 * Pat the watchdog timer.
80 static void inline at32_wdt_pat(void)
82 wdt_writel(wdt, CLR, 0x42);
86 * Watchdog device is opened, and watchdog starts running.
88 static int at32_wdt_open(struct inode *inode, struct file *file)
90 if (test_and_set_bit(1, &wdt->users))
91 return -EBUSY;
93 at32_wdt_start();
94 return nonseekable_open(inode, file);
98 * Close the watchdog device. If CONFIG_WATCHDOG_NOWAYOUT is _not_ defined then
99 * the watchdog is also disabled.
101 static int at32_wdt_close(struct inode *inode, struct file *file)
103 #ifndef CONFIG_WATCHDOG_NOWAYOUT
104 at32_wdt_stop();
105 #endif
106 clear_bit(1, &wdt->users);
107 return 0;
111 * Change the watchdog time interval.
113 static int at32_wdt_settimeout(int time)
116 * All counting occurs at 1 / SLOW_CLOCK (32 kHz) and max prescaler is
117 * 2 ^ 16 allowing up to 2 seconds timeout.
119 if ((time < TIMEOUT_MIN) || (time > TIMEOUT_MAX))
120 return -EINVAL;
123 * Set new watchdog time. It will be used when at32_wdt_start() is
124 * called.
126 wdt->timeout = time;
127 return 0;
130 static struct watchdog_info at32_wdt_info = {
131 .identity = "at32ap700x watchdog",
132 .options = WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING,
136 * Handle commands from user-space.
138 static int at32_wdt_ioctl(struct inode *inode, struct file *file,
139 unsigned int cmd, unsigned long arg)
141 int ret = -ENOTTY;
142 int time;
143 void __user *argp = (void __user *)arg;
144 int __user *p = argp;
146 switch (cmd) {
147 case WDIOC_KEEPALIVE:
148 at32_wdt_pat();
149 ret = 0;
150 break;
151 case WDIOC_GETSUPPORT:
152 ret = copy_to_user(argp, &at32_wdt_info,
153 sizeof(at32_wdt_info)) ? -EFAULT : 0;
154 break;
155 case WDIOC_SETTIMEOUT:
156 ret = get_user(time, p);
157 if (ret)
158 break;
159 ret = at32_wdt_settimeout(time);
160 if (ret)
161 break;
162 /* Enable new time value */
163 at32_wdt_start();
164 /* fall through */
165 case WDIOC_GETTIMEOUT:
166 ret = put_user(wdt->timeout, p);
167 break;
168 case WDIOC_GETSTATUS: /* fall through */
169 case WDIOC_GETBOOTSTATUS:
170 ret = put_user(0, p);
171 break;
172 case WDIOC_SETOPTIONS:
173 ret = get_user(time, p);
174 if (ret)
175 break;
176 if (time & WDIOS_DISABLECARD)
177 at32_wdt_stop();
178 if (time & WDIOS_ENABLECARD)
179 at32_wdt_start();
180 ret = 0;
181 break;
184 return ret;
187 static ssize_t at32_wdt_write(struct file *file, const char *data, size_t len,
188 loff_t *ppos)
190 at32_wdt_pat();
191 return len;
194 static const struct file_operations at32_wdt_fops = {
195 .owner = THIS_MODULE,
196 .llseek = no_llseek,
197 .ioctl = at32_wdt_ioctl,
198 .open = at32_wdt_open,
199 .release = at32_wdt_close,
200 .write = at32_wdt_write,
203 static int __init at32_wdt_probe(struct platform_device *pdev)
205 struct resource *regs;
206 int ret;
208 if (wdt) {
209 dev_dbg(&pdev->dev, "only 1 wdt instance supported.\n");
210 return -EBUSY;
213 regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
214 if (!regs) {
215 dev_dbg(&pdev->dev, "missing mmio resource\n");
216 return -ENXIO;
219 wdt = kzalloc(sizeof(struct wdt_at32ap700x), GFP_KERNEL);
220 if (!wdt) {
221 dev_dbg(&pdev->dev, "no memory for wdt structure\n");
222 return -ENOMEM;
225 wdt->regs = ioremap(regs->start, regs->end - regs->start + 1);
226 wdt->users = 0;
227 wdt->miscdev.minor = WATCHDOG_MINOR;
228 wdt->miscdev.name = "watchdog";
229 wdt->miscdev.fops = &at32_wdt_fops;
231 if (at32_wdt_settimeout(TIMEOUT_DEFAULT)) {
232 at32_wdt_settimeout(TIMEOUT_MAX);
233 dev_dbg(&pdev->dev,
234 "default timeout invalid, set to %d sec.\n",
235 TIMEOUT_MAX);
238 ret = misc_register(&wdt->miscdev);
239 if (ret) {
240 dev_dbg(&pdev->dev, "failed to register wdt miscdev\n");
241 goto err_register;
244 platform_set_drvdata(pdev, wdt);
245 wdt->miscdev.parent = &pdev->dev;
246 dev_info(&pdev->dev, "AT32AP700X WDT at 0x%p\n", wdt->regs);
248 return 0;
250 err_register:
251 kfree(wdt);
252 wdt = NULL;
253 return ret;
256 static int __exit at32_wdt_remove(struct platform_device *pdev)
258 if (wdt && platform_get_drvdata(pdev) == wdt) {
259 misc_deregister(&wdt->miscdev);
260 kfree(wdt);
261 wdt = NULL;
262 platform_set_drvdata(pdev, NULL);
265 return 0;
268 static void at32_wdt_shutdown(struct platform_device *pdev)
270 at32_wdt_stop();
273 #ifdef CONFIG_PM
274 static int at32_wdt_suspend(struct platform_device *pdev, pm_message_t message)
276 at32_wdt_stop();
277 return 0;
280 static int at32_wdt_resume(struct platform_device *pdev)
282 if (wdt->users)
283 at32_wdt_start();
284 return 0;
286 #else
287 #define at32_wdt_suspend NULL
288 #define at32_wdt_resume NULL
289 #endif
291 static struct platform_driver at32_wdt_driver = {
292 .remove = __exit_p(at32_wdt_remove),
293 .suspend = at32_wdt_suspend,
294 .resume = at32_wdt_resume,
295 .driver = {
296 .name = "at32_wdt",
297 .owner = THIS_MODULE,
299 .shutdown = at32_wdt_shutdown,
302 static int __init at32_wdt_init(void)
304 return platform_driver_probe(&at32_wdt_driver, at32_wdt_probe);
306 module_init(at32_wdt_init);
308 static void __exit at32_wdt_exit(void)
310 platform_driver_unregister(&at32_wdt_driver);
312 module_exit(at32_wdt_exit);
314 MODULE_AUTHOR("Hans-Christian Egtvedt <hcegtvedt@atmel.com>");
315 MODULE_DESCRIPTION("Watchdog driver for Atmel AT32AP700X");
316 MODULE_LICENSE("GPL");
317 MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR);