blob: 8abe1c77ac1f10ba9aff36a5fa0838131cd28420 [file] [log] [blame]
Hans-Christian Egtvedta9cb3952007-06-07 16:06:41 -07001/*
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>
Andrew Mortonc37f2712007-06-07 16:06:43 -070019#include <linux/uaccess.h>
20#include <linux/io.h>
Hans-Christian Egtvedta9cb3952007-06-07 16:06:41 -070021
22#define TIMEOUT_MIN 1
23#define TIMEOUT_DEFAULT CONFIG_AT32AP700X_WDT_TIMEOUT
24#define TIMEOUT_MAX 2
25
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
31
32#define WDT_CLR 0x04
33
34#define WDT_BIT(name) (1 << WDT_##name)
Wim Van Sebroeckc0ead7e2007-06-17 19:34:23 +000035#define WDT_BF(name, value) ((value) << WDT_##name)
Hans-Christian Egtvedta9cb3952007-06-07 16:06:41 -070036
Wim Van Sebroeckc0ead7e2007-06-17 19:34:23 +000037#define wdt_readl(dev, reg) \
Hans-Christian Egtvedta9cb3952007-06-07 16:06:41 -070038 __raw_readl((dev)->regs + WDT_##reg)
Wim Van Sebroeckc0ead7e2007-06-17 19:34:23 +000039#define wdt_writel(dev, reg, value) \
Hans-Christian Egtvedta9cb3952007-06-07 16:06:41 -070040 __raw_writel((value), (dev)->regs + WDT_##reg)
41
42struct wdt_at32ap700x {
43 void __iomem *regs;
44 int timeout;
45 int users;
46 struct miscdevice miscdev;
47};
48
49static struct wdt_at32ap700x *wdt;
50
51/*
52 * Disable the watchdog.
53 */
Wim Van Sebroeckc0ead7e2007-06-17 19:34:23 +000054static inline void at32_wdt_stop(void)
Hans-Christian Egtvedta9cb3952007-06-07 16:06:41 -070055{
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));
59}
60
61/*
62 * Enable and reset the watchdog.
63 */
Wim Van Sebroeckc0ead7e2007-06-17 19:34:23 +000064static inline void at32_wdt_start(void)
Hans-Christian Egtvedta9cb3952007-06-07 16:06:41 -070065{
66 /* 0xf is 2^16 divider = 2 sec, 0xe is 2^15 divider = 1 sec */
67 unsigned long psel = (wdt->timeout > 1) ? 0xf : 0xe;
68
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));
75}
76
77/*
78 * Pat the watchdog timer.
79 */
Wim Van Sebroeckc0ead7e2007-06-17 19:34:23 +000080static inline void at32_wdt_pat(void)
Hans-Christian Egtvedta9cb3952007-06-07 16:06:41 -070081{
82 wdt_writel(wdt, CLR, 0x42);
83}
84
85/*
86 * Watchdog device is opened, and watchdog starts running.
87 */
88static int at32_wdt_open(struct inode *inode, struct file *file)
89{
90 if (test_and_set_bit(1, &wdt->users))
91 return -EBUSY;
92
93 at32_wdt_start();
94 return nonseekable_open(inode, file);
95}
96
97/*
98 * Close the watchdog device. If CONFIG_WATCHDOG_NOWAYOUT is _not_ defined then
99 * the watchdog is also disabled.
100 */
101static int at32_wdt_close(struct inode *inode, struct file *file)
102{
103#ifndef CONFIG_WATCHDOG_NOWAYOUT
104 at32_wdt_stop();
105#endif
106 clear_bit(1, &wdt->users);
107 return 0;
108}
109
110/*
111 * Change the watchdog time interval.
112 */
113static int at32_wdt_settimeout(int time)
114{
115 /*
116 * All counting occurs at 1 / SLOW_CLOCK (32 kHz) and max prescaler is
117 * 2 ^ 16 allowing up to 2 seconds timeout.
118 */
119 if ((time < TIMEOUT_MIN) || (time > TIMEOUT_MAX))
120 return -EINVAL;
121
Andrew Mortonc37f2712007-06-07 16:06:43 -0700122 /*
123 * Set new watchdog time. It will be used when at32_wdt_start() is
124 * called.
125 */
Hans-Christian Egtvedta9cb3952007-06-07 16:06:41 -0700126 wdt->timeout = time;
127 return 0;
128}
129
130static struct watchdog_info at32_wdt_info = {
131 .identity = "at32ap700x watchdog",
132 .options = WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING,
133};
134
135/*
136 * Handle commands from user-space.
137 */
138static int at32_wdt_ioctl(struct inode *inode, struct file *file,
139 unsigned int cmd, unsigned long arg)
140{
141 int ret = -ENOTTY;
142 int time;
143 void __user *argp = (void __user *)arg;
144 int __user *p = argp;
145
Andrew Mortonc37f2712007-06-07 16:06:43 -0700146 switch (cmd) {
Hans-Christian Egtvedta9cb3952007-06-07 16:06:41 -0700147 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;
182 }
183
184 return ret;
185}
186
Andrew Mortonc37f2712007-06-07 16:06:43 -0700187static ssize_t at32_wdt_write(struct file *file, const char *data, size_t len,
188 loff_t *ppos)
Hans-Christian Egtvedta9cb3952007-06-07 16:06:41 -0700189{
190 at32_wdt_pat();
191 return len;
192}
193
194static 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,
201};
202
203static int __init at32_wdt_probe(struct platform_device *pdev)
204{
205 struct resource *regs;
206 int ret;
207
208 if (wdt) {
209 dev_dbg(&pdev->dev, "only 1 wdt instance supported.\n");
210 return -EBUSY;
211 }
212
213 regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
214 if (!regs) {
215 dev_dbg(&pdev->dev, "missing mmio resource\n");
216 return -ENXIO;
217 }
218
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;
223 }
224
225 wdt->regs = ioremap(regs->start, regs->end - regs->start + 1);
Hans-Christian Egtvedt47d17762007-06-08 11:03:01 -0700226 if (!wdt->regs) {
227 ret = -ENOMEM;
228 dev_dbg(&pdev->dev, "could not map I/O memory\n");
229 goto err_free;
230 }
Hans-Christian Egtvedta9cb3952007-06-07 16:06:41 -0700231 wdt->users = 0;
232 wdt->miscdev.minor = WATCHDOG_MINOR;
233 wdt->miscdev.name = "watchdog";
234 wdt->miscdev.fops = &at32_wdt_fops;
235
236 if (at32_wdt_settimeout(TIMEOUT_DEFAULT)) {
237 at32_wdt_settimeout(TIMEOUT_MAX);
238 dev_dbg(&pdev->dev,
239 "default timeout invalid, set to %d sec.\n",
240 TIMEOUT_MAX);
241 }
242
243 ret = misc_register(&wdt->miscdev);
244 if (ret) {
245 dev_dbg(&pdev->dev, "failed to register wdt miscdev\n");
Hans-Christian Egtvedt47d17762007-06-08 11:03:01 -0700246 goto err_iounmap;
Hans-Christian Egtvedta9cb3952007-06-07 16:06:41 -0700247 }
248
249 platform_set_drvdata(pdev, wdt);
250 wdt->miscdev.parent = &pdev->dev;
251 dev_info(&pdev->dev, "AT32AP700X WDT at 0x%p\n", wdt->regs);
252
253 return 0;
254
Hans-Christian Egtvedt47d17762007-06-08 11:03:01 -0700255err_iounmap:
256 iounmap(wdt->regs);
257err_free:
Hans-Christian Egtvedta9cb3952007-06-07 16:06:41 -0700258 kfree(wdt);
259 wdt = NULL;
260 return ret;
261}
262
263static int __exit at32_wdt_remove(struct platform_device *pdev)
264{
265 if (wdt && platform_get_drvdata(pdev) == wdt) {
266 misc_deregister(&wdt->miscdev);
Hans-Christian Egtvedtff732312007-06-08 11:01:47 -0700267 iounmap(wdt->regs);
Hans-Christian Egtvedta9cb3952007-06-07 16:06:41 -0700268 kfree(wdt);
269 wdt = NULL;
270 platform_set_drvdata(pdev, NULL);
271 }
272
273 return 0;
274}
275
276static void at32_wdt_shutdown(struct platform_device *pdev)
277{
278 at32_wdt_stop();
279}
280
281#ifdef CONFIG_PM
282static int at32_wdt_suspend(struct platform_device *pdev, pm_message_t message)
283{
284 at32_wdt_stop();
285 return 0;
286}
287
288static int at32_wdt_resume(struct platform_device *pdev)
289{
290 if (wdt->users)
291 at32_wdt_start();
292 return 0;
293}
Andrew Morton97a2a2e2007-06-07 16:08:53 -0700294#else
295#define at32_wdt_suspend NULL
296#define at32_wdt_resume NULL
Hans-Christian Egtvedta9cb3952007-06-07 16:06:41 -0700297#endif
298
299static struct platform_driver at32_wdt_driver = {
300 .remove = __exit_p(at32_wdt_remove),
Hans-Christian Egtvedta9cb3952007-06-07 16:06:41 -0700301 .suspend = at32_wdt_suspend,
302 .resume = at32_wdt_resume,
Hans-Christian Egtvedta9cb3952007-06-07 16:06:41 -0700303 .driver = {
304 .name = "at32_wdt",
305 .owner = THIS_MODULE,
306 },
307 .shutdown = at32_wdt_shutdown,
308};
309
310static int __init at32_wdt_init(void)
311{
312 return platform_driver_probe(&at32_wdt_driver, at32_wdt_probe);
313}
314module_init(at32_wdt_init);
315
316static void __exit at32_wdt_exit(void)
317{
318 platform_driver_unregister(&at32_wdt_driver);
319}
320module_exit(at32_wdt_exit);
321
322MODULE_AUTHOR("Hans-Christian Egtvedt <hcegtvedt@atmel.com>");
323MODULE_DESCRIPTION("Watchdog driver for Atmel AT32AP700X");
324MODULE_LICENSE("GPL");
325MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR);